root/drivers/crypto/ux500/hash/hash_core.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. release_hash_device
  2. hash_dma_setup_channel
  3. hash_dma_callback
  4. hash_set_dma_transfer
  5. hash_dma_done
  6. hash_dma_write
  7. get_empty_message_digest
  8. hash_disable_power
  9. hash_enable_power
  10. hash_get_device_data
  11. hash_hw_write_key
  12. init_hash_hw
  13. hash_get_nents
  14. hash_dma_valid_data
  15. hash_init
  16. hash_processblock
  17. hash_messagepad
  18. hash_incrementlength
  19. hash_setconfiguration
  20. hash_begin
  21. hash_process_data
  22. hash_dma_final
  23. hash_hw_final
  24. hash_hw_update
  25. hash_resume_state
  26. hash_save_state
  27. hash_check_hw
  28. hash_get_digest
  29. ahash_update
  30. ahash_final
  31. hash_setkey
  32. ahash_sha1_init
  33. ahash_sha256_init
  34. ahash_sha1_digest
  35. ahash_sha256_digest
  36. ahash_noimport
  37. ahash_noexport
  38. hmac_sha1_init
  39. hmac_sha256_init
  40. hmac_sha1_digest
  41. hmac_sha256_digest
  42. hmac_sha1_setkey
  43. hmac_sha256_setkey
  44. hash_cra_init
  45. ahash_algs_register_all
  46. ahash_algs_unregister_all
  47. ux500_hash_probe
  48. ux500_hash_remove
  49. ux500_hash_shutdown
  50. ux500_hash_suspend
  51. ux500_hash_resume
  52. ux500_hash_mod_init
  53. ux500_hash_mod_fini

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * Cryptographic API.
   4  * Support for Nomadik hardware crypto engine.
   5 
   6  * Copyright (C) ST-Ericsson SA 2010
   7  * Author: Shujuan Chen <shujuan.chen@stericsson.com> for ST-Ericsson
   8  * Author: Joakim Bech <joakim.xx.bech@stericsson.com> for ST-Ericsson
   9  * Author: Berne Hebark <berne.herbark@stericsson.com> for ST-Ericsson.
  10  * Author: Niklas Hernaeus <niklas.hernaeus@stericsson.com> for ST-Ericsson.
  11  * Author: Andreas Westin <andreas.westin@stericsson.com> for ST-Ericsson.
  12  */
  13 
  14 #define pr_fmt(fmt) "hashX hashX: " fmt
  15 
  16 #include <linux/clk.h>
  17 #include <linux/device.h>
  18 #include <linux/err.h>
  19 #include <linux/init.h>
  20 #include <linux/io.h>
  21 #include <linux/klist.h>
  22 #include <linux/kernel.h>
  23 #include <linux/module.h>
  24 #include <linux/mod_devicetable.h>
  25 #include <linux/platform_device.h>
  26 #include <linux/crypto.h>
  27 
  28 #include <linux/regulator/consumer.h>
  29 #include <linux/dmaengine.h>
  30 #include <linux/bitops.h>
  31 
  32 #include <crypto/internal/hash.h>
  33 #include <crypto/sha.h>
  34 #include <crypto/scatterwalk.h>
  35 #include <crypto/algapi.h>
  36 
  37 #include <linux/platform_data/crypto-ux500.h>
  38 
  39 #include "hash_alg.h"
  40 
  41 static int hash_mode;
  42 module_param(hash_mode, int, 0);
  43 MODULE_PARM_DESC(hash_mode, "CPU or DMA mode. CPU = 0 (default), DMA = 1");
  44 
  45 /* HMAC-SHA1, no key */
  46 static const u8 zero_message_hmac_sha1[SHA1_DIGEST_SIZE] = {
  47         0xfb, 0xdb, 0x1d, 0x1b, 0x18, 0xaa, 0x6c, 0x08,
  48         0x32, 0x4b, 0x7d, 0x64, 0xb7, 0x1f, 0xb7, 0x63,
  49         0x70, 0x69, 0x0e, 0x1d
  50 };
  51 
  52 /* HMAC-SHA256, no key */
  53 static const u8 zero_message_hmac_sha256[SHA256_DIGEST_SIZE] = {
  54         0xb6, 0x13, 0x67, 0x9a, 0x08, 0x14, 0xd9, 0xec,
  55         0x77, 0x2f, 0x95, 0xd7, 0x78, 0xc3, 0x5f, 0xc5,
  56         0xff, 0x16, 0x97, 0xc4, 0x93, 0x71, 0x56, 0x53,
  57         0xc6, 0xc7, 0x12, 0x14, 0x42, 0x92, 0xc5, 0xad
  58 };
  59 
  60 /**
  61  * struct hash_driver_data - data specific to the driver.
  62  *
  63  * @device_list:        A list of registered devices to choose from.
  64  * @device_allocation:  A semaphore initialized with number of devices.
  65  */
  66 struct hash_driver_data {
  67         struct klist            device_list;
  68         struct semaphore        device_allocation;
  69 };
  70 
  71 static struct hash_driver_data  driver_data;
  72 
  73 /* Declaration of functions */
  74 /**
  75  * hash_messagepad - Pads a message and write the nblw bits.
  76  * @device_data:        Structure for the hash device.
  77  * @message:            Last word of a message
  78  * @index_bytes:        The number of bytes in the last message
  79  *
  80  * This function manages the final part of the digest calculation, when less
  81  * than 512 bits (64 bytes) remain in message. This means index_bytes < 64.
  82  *
  83  */
  84 static void hash_messagepad(struct hash_device_data *device_data,
  85                             const u32 *message, u8 index_bytes);
  86 
  87 /**
  88  * release_hash_device - Releases a previously allocated hash device.
  89  * @device_data:        Structure for the hash device.
  90  *
  91  */
  92 static void release_hash_device(struct hash_device_data *device_data)
  93 {
  94         spin_lock(&device_data->ctx_lock);
  95         device_data->current_ctx->device = NULL;
  96         device_data->current_ctx = NULL;
  97         spin_unlock(&device_data->ctx_lock);
  98 
  99         /*
 100          * The down_interruptible part for this semaphore is called in
 101          * cryp_get_device_data.
 102          */
 103         up(&driver_data.device_allocation);
 104 }
 105 
 106 static void hash_dma_setup_channel(struct hash_device_data *device_data,
 107                                    struct device *dev)
 108 {
 109         struct hash_platform_data *platform_data = dev->platform_data;
 110         struct dma_slave_config conf = {
 111                 .direction = DMA_MEM_TO_DEV,
 112                 .dst_addr = device_data->phybase + HASH_DMA_FIFO,
 113                 .dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES,
 114                 .dst_maxburst = 16,
 115         };
 116 
 117         dma_cap_zero(device_data->dma.mask);
 118         dma_cap_set(DMA_SLAVE, device_data->dma.mask);
 119 
 120         device_data->dma.cfg_mem2hash = platform_data->mem_to_engine;
 121         device_data->dma.chan_mem2hash =
 122                 dma_request_channel(device_data->dma.mask,
 123                                     platform_data->dma_filter,
 124                                     device_data->dma.cfg_mem2hash);
 125 
 126         dmaengine_slave_config(device_data->dma.chan_mem2hash, &conf);
 127 
 128         init_completion(&device_data->dma.complete);
 129 }
 130 
 131 static void hash_dma_callback(void *data)
 132 {
 133         struct hash_ctx *ctx = data;
 134 
 135         complete(&ctx->device->dma.complete);
 136 }
 137 
 138 static int hash_set_dma_transfer(struct hash_ctx *ctx, struct scatterlist *sg,
 139                                  int len, enum dma_data_direction direction)
 140 {
 141         struct dma_async_tx_descriptor *desc = NULL;
 142         struct dma_chan *channel = NULL;
 143         dma_cookie_t cookie;
 144 
 145         if (direction != DMA_TO_DEVICE) {
 146                 dev_err(ctx->device->dev, "%s: Invalid DMA direction\n",
 147                         __func__);
 148                 return -EFAULT;
 149         }
 150 
 151         sg->length = ALIGN(sg->length, HASH_DMA_ALIGN_SIZE);
 152 
 153         channel = ctx->device->dma.chan_mem2hash;
 154         ctx->device->dma.sg = sg;
 155         ctx->device->dma.sg_len = dma_map_sg(channel->device->dev,
 156                         ctx->device->dma.sg, ctx->device->dma.nents,
 157                         direction);
 158 
 159         if (!ctx->device->dma.sg_len) {
 160                 dev_err(ctx->device->dev, "%s: Could not map the sg list (TO_DEVICE)\n",
 161                         __func__);
 162                 return -EFAULT;
 163         }
 164 
 165         dev_dbg(ctx->device->dev, "%s: Setting up DMA for buffer (TO_DEVICE)\n",
 166                 __func__);
 167         desc = dmaengine_prep_slave_sg(channel,
 168                         ctx->device->dma.sg, ctx->device->dma.sg_len,
 169                         DMA_MEM_TO_DEV, DMA_CTRL_ACK | DMA_PREP_INTERRUPT);
 170         if (!desc) {
 171                 dev_err(ctx->device->dev,
 172                         "%s: dmaengine_prep_slave_sg() failed!\n", __func__);
 173                 return -EFAULT;
 174         }
 175 
 176         desc->callback = hash_dma_callback;
 177         desc->callback_param = ctx;
 178 
 179         cookie = dmaengine_submit(desc);
 180         dma_async_issue_pending(channel);
 181 
 182         return 0;
 183 }
 184 
 185 static void hash_dma_done(struct hash_ctx *ctx)
 186 {
 187         struct dma_chan *chan;
 188 
 189         chan = ctx->device->dma.chan_mem2hash;
 190         dmaengine_terminate_all(chan);
 191         dma_unmap_sg(chan->device->dev, ctx->device->dma.sg,
 192                      ctx->device->dma.sg_len, DMA_TO_DEVICE);
 193 }
 194 
 195 static int hash_dma_write(struct hash_ctx *ctx,
 196                           struct scatterlist *sg, int len)
 197 {
 198         int error = hash_set_dma_transfer(ctx, sg, len, DMA_TO_DEVICE);
 199         if (error) {
 200                 dev_dbg(ctx->device->dev,
 201                         "%s: hash_set_dma_transfer() failed\n", __func__);
 202                 return error;
 203         }
 204 
 205         return len;
 206 }
 207 
 208 /**
 209  * get_empty_message_digest - Returns a pre-calculated digest for
 210  * the empty message.
 211  * @device_data:        Structure for the hash device.
 212  * @zero_hash:          Buffer to return the empty message digest.
 213  * @zero_hash_size:     Hash size of the empty message digest.
 214  * @zero_digest:        True if zero_digest returned.
 215  */
 216 static int get_empty_message_digest(
 217                 struct hash_device_data *device_data,
 218                 u8 *zero_hash, u32 *zero_hash_size, bool *zero_digest)
 219 {
 220         int ret = 0;
 221         struct hash_ctx *ctx = device_data->current_ctx;
 222         *zero_digest = false;
 223 
 224         /**
 225          * Caller responsible for ctx != NULL.
 226          */
 227 
 228         if (HASH_OPER_MODE_HASH == ctx->config.oper_mode) {
 229                 if (HASH_ALGO_SHA1 == ctx->config.algorithm) {
 230                         memcpy(zero_hash, &sha1_zero_message_hash[0],
 231                                SHA1_DIGEST_SIZE);
 232                         *zero_hash_size = SHA1_DIGEST_SIZE;
 233                         *zero_digest = true;
 234                 } else if (HASH_ALGO_SHA256 ==
 235                                 ctx->config.algorithm) {
 236                         memcpy(zero_hash, &sha256_zero_message_hash[0],
 237                                SHA256_DIGEST_SIZE);
 238                         *zero_hash_size = SHA256_DIGEST_SIZE;
 239                         *zero_digest = true;
 240                 } else {
 241                         dev_err(device_data->dev, "%s: Incorrect algorithm!\n",
 242                                 __func__);
 243                         ret = -EINVAL;
 244                         goto out;
 245                 }
 246         } else if (HASH_OPER_MODE_HMAC == ctx->config.oper_mode) {
 247                 if (!ctx->keylen) {
 248                         if (HASH_ALGO_SHA1 == ctx->config.algorithm) {
 249                                 memcpy(zero_hash, &zero_message_hmac_sha1[0],
 250                                        SHA1_DIGEST_SIZE);
 251                                 *zero_hash_size = SHA1_DIGEST_SIZE;
 252                                 *zero_digest = true;
 253                         } else if (HASH_ALGO_SHA256 == ctx->config.algorithm) {
 254                                 memcpy(zero_hash, &zero_message_hmac_sha256[0],
 255                                        SHA256_DIGEST_SIZE);
 256                                 *zero_hash_size = SHA256_DIGEST_SIZE;
 257                                 *zero_digest = true;
 258                         } else {
 259                                 dev_err(device_data->dev, "%s: Incorrect algorithm!\n",
 260                                         __func__);
 261                                 ret = -EINVAL;
 262                                 goto out;
 263                         }
 264                 } else {
 265                         dev_dbg(device_data->dev,
 266                                 "%s: Continue hash calculation, since hmac key available\n",
 267                                 __func__);
 268                 }
 269         }
 270 out:
 271 
 272         return ret;
 273 }
 274 
 275 /**
 276  * hash_disable_power - Request to disable power and clock.
 277  * @device_data:        Structure for the hash device.
 278  * @save_device_state:  If true, saves the current hw state.
 279  *
 280  * This function request for disabling power (regulator) and clock,
 281  * and could also save current hw state.
 282  */
 283 static int hash_disable_power(struct hash_device_data *device_data,
 284                               bool save_device_state)
 285 {
 286         int ret = 0;
 287         struct device *dev = device_data->dev;
 288 
 289         spin_lock(&device_data->power_state_lock);
 290         if (!device_data->power_state)
 291                 goto out;
 292 
 293         if (save_device_state) {
 294                 hash_save_state(device_data,
 295                                 &device_data->state);
 296                 device_data->restore_dev_state = true;
 297         }
 298 
 299         clk_disable(device_data->clk);
 300         ret = regulator_disable(device_data->regulator);
 301         if (ret)
 302                 dev_err(dev, "%s: regulator_disable() failed!\n", __func__);
 303 
 304         device_data->power_state = false;
 305 
 306 out:
 307         spin_unlock(&device_data->power_state_lock);
 308 
 309         return ret;
 310 }
 311 
 312 /**
 313  * hash_enable_power - Request to enable power and clock.
 314  * @device_data:                Structure for the hash device.
 315  * @restore_device_state:       If true, restores a previous saved hw state.
 316  *
 317  * This function request for enabling power (regulator) and clock,
 318  * and could also restore a previously saved hw state.
 319  */
 320 static int hash_enable_power(struct hash_device_data *device_data,
 321                              bool restore_device_state)
 322 {
 323         int ret = 0;
 324         struct device *dev = device_data->dev;
 325 
 326         spin_lock(&device_data->power_state_lock);
 327         if (!device_data->power_state) {
 328                 ret = regulator_enable(device_data->regulator);
 329                 if (ret) {
 330                         dev_err(dev, "%s: regulator_enable() failed!\n",
 331                                 __func__);
 332                         goto out;
 333                 }
 334                 ret = clk_enable(device_data->clk);
 335                 if (ret) {
 336                         dev_err(dev, "%s: clk_enable() failed!\n", __func__);
 337                         ret = regulator_disable(
 338                                         device_data->regulator);
 339                         goto out;
 340                 }
 341                 device_data->power_state = true;
 342         }
 343 
 344         if (device_data->restore_dev_state) {
 345                 if (restore_device_state) {
 346                         device_data->restore_dev_state = false;
 347                         hash_resume_state(device_data, &device_data->state);
 348                 }
 349         }
 350 out:
 351         spin_unlock(&device_data->power_state_lock);
 352 
 353         return ret;
 354 }
 355 
 356 /**
 357  * hash_get_device_data - Checks for an available hash device and return it.
 358  * @hash_ctx:           Structure for the hash context.
 359  * @device_data:        Structure for the hash device.
 360  *
 361  * This function check for an available hash device and return it to
 362  * the caller.
 363  * Note! Caller need to release the device, calling up().
 364  */
 365 static int hash_get_device_data(struct hash_ctx *ctx,
 366                                 struct hash_device_data **device_data)
 367 {
 368         int                     ret;
 369         struct klist_iter       device_iterator;
 370         struct klist_node       *device_node;
 371         struct hash_device_data *local_device_data = NULL;
 372 
 373         /* Wait until a device is available */
 374         ret = down_interruptible(&driver_data.device_allocation);
 375         if (ret)
 376                 return ret;  /* Interrupted */
 377 
 378         /* Select a device */
 379         klist_iter_init(&driver_data.device_list, &device_iterator);
 380         device_node = klist_next(&device_iterator);
 381         while (device_node) {
 382                 local_device_data = container_of(device_node,
 383                                            struct hash_device_data, list_node);
 384                 spin_lock(&local_device_data->ctx_lock);
 385                 /* current_ctx allocates a device, NULL = unallocated */
 386                 if (local_device_data->current_ctx) {
 387                         device_node = klist_next(&device_iterator);
 388                 } else {
 389                         local_device_data->current_ctx = ctx;
 390                         ctx->device = local_device_data;
 391                         spin_unlock(&local_device_data->ctx_lock);
 392                         break;
 393                 }
 394                 spin_unlock(&local_device_data->ctx_lock);
 395         }
 396         klist_iter_exit(&device_iterator);
 397 
 398         if (!device_node) {
 399                 /**
 400                  * No free device found.
 401                  * Since we allocated a device with down_interruptible, this
 402                  * should not be able to happen.
 403                  * Number of available devices, which are contained in
 404                  * device_allocation, is therefore decremented by not doing
 405                  * an up(device_allocation).
 406                  */
 407                 return -EBUSY;
 408         }
 409 
 410         *device_data = local_device_data;
 411 
 412         return 0;
 413 }
 414 
 415 /**
 416  * hash_hw_write_key - Writes the key to the hardware registries.
 417  *
 418  * @device_data:        Structure for the hash device.
 419  * @key:                Key to be written.
 420  * @keylen:             The lengt of the key.
 421  *
 422  * Note! This function DOES NOT write to the NBLW registry, even though
 423  * specified in the the hw design spec. Either due to incorrect info in the
 424  * spec or due to a bug in the hw.
 425  */
 426 static void hash_hw_write_key(struct hash_device_data *device_data,
 427                               const u8 *key, unsigned int keylen)
 428 {
 429         u32 word = 0;
 430         int nwords = 1;
 431 
 432         HASH_CLEAR_BITS(&device_data->base->str, HASH_STR_NBLW_MASK);
 433 
 434         while (keylen >= 4) {
 435                 u32 *key_word = (u32 *)key;
 436 
 437                 HASH_SET_DIN(key_word, nwords);
 438                 keylen -= 4;
 439                 key += 4;
 440         }
 441 
 442         /* Take care of the remaining bytes in the last word */
 443         if (keylen) {
 444                 word = 0;
 445                 while (keylen) {
 446                         word |= (key[keylen - 1] << (8 * (keylen - 1)));
 447                         keylen--;
 448                 }
 449 
 450                 HASH_SET_DIN(&word, nwords);
 451         }
 452 
 453         while (readl(&device_data->base->str) & HASH_STR_DCAL_MASK)
 454                 cpu_relax();
 455 
 456         HASH_SET_DCAL;
 457 
 458         while (readl(&device_data->base->str) & HASH_STR_DCAL_MASK)
 459                 cpu_relax();
 460 }
 461 
 462 /**
 463  * init_hash_hw - Initialise the hash hardware for a new calculation.
 464  * @device_data:        Structure for the hash device.
 465  * @ctx:                The hash context.
 466  *
 467  * This function will enable the bits needed to clear and start a new
 468  * calculation.
 469  */
 470 static int init_hash_hw(struct hash_device_data *device_data,
 471                         struct hash_ctx *ctx)
 472 {
 473         int ret = 0;
 474 
 475         ret = hash_setconfiguration(device_data, &ctx->config);
 476         if (ret) {
 477                 dev_err(device_data->dev, "%s: hash_setconfiguration() failed!\n",
 478                         __func__);
 479                 return ret;
 480         }
 481 
 482         hash_begin(device_data, ctx);
 483 
 484         if (ctx->config.oper_mode == HASH_OPER_MODE_HMAC)
 485                 hash_hw_write_key(device_data, ctx->key, ctx->keylen);
 486 
 487         return ret;
 488 }
 489 
 490 /**
 491  * hash_get_nents - Return number of entries (nents) in scatterlist (sg).
 492  *
 493  * @sg:         Scatterlist.
 494  * @size:       Size in bytes.
 495  * @aligned:    True if sg data aligned to work in DMA mode.
 496  *
 497  */
 498 static int hash_get_nents(struct scatterlist *sg, int size, bool *aligned)
 499 {
 500         int nents = 0;
 501         bool aligned_data = true;
 502 
 503         while (size > 0 && sg) {
 504                 nents++;
 505                 size -= sg->length;
 506 
 507                 /* hash_set_dma_transfer will align last nent */
 508                 if ((aligned && !IS_ALIGNED(sg->offset, HASH_DMA_ALIGN_SIZE)) ||
 509                     (!IS_ALIGNED(sg->length, HASH_DMA_ALIGN_SIZE) && size > 0))
 510                         aligned_data = false;
 511 
 512                 sg = sg_next(sg);
 513         }
 514 
 515         if (aligned)
 516                 *aligned = aligned_data;
 517 
 518         if (size != 0)
 519                 return -EFAULT;
 520 
 521         return nents;
 522 }
 523 
 524 /**
 525  * hash_dma_valid_data - checks for dma valid sg data.
 526  * @sg:         Scatterlist.
 527  * @datasize:   Datasize in bytes.
 528  *
 529  * NOTE! This function checks for dma valid sg data, since dma
 530  * only accept datasizes of even wordsize.
 531  */
 532 static bool hash_dma_valid_data(struct scatterlist *sg, int datasize)
 533 {
 534         bool aligned;
 535 
 536         /* Need to include at least one nent, else error */
 537         if (hash_get_nents(sg, datasize, &aligned) < 1)
 538                 return false;
 539 
 540         return aligned;
 541 }
 542 
 543 /**
 544  * hash_init - Common hash init function for SHA1/SHA2 (SHA256).
 545  * @req: The hash request for the job.
 546  *
 547  * Initialize structures.
 548  */
 549 static int hash_init(struct ahash_request *req)
 550 {
 551         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
 552         struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
 553         struct hash_req_ctx *req_ctx = ahash_request_ctx(req);
 554 
 555         if (!ctx->key)
 556                 ctx->keylen = 0;
 557 
 558         memset(&req_ctx->state, 0, sizeof(struct hash_state));
 559         req_ctx->updated = 0;
 560         if (hash_mode == HASH_MODE_DMA) {
 561                 if (req->nbytes < HASH_DMA_ALIGN_SIZE) {
 562                         req_ctx->dma_mode = false; /* Don't use DMA */
 563 
 564                         pr_debug("%s: DMA mode, but direct to CPU mode for data size < %d\n",
 565                                  __func__, HASH_DMA_ALIGN_SIZE);
 566                 } else {
 567                         if (req->nbytes >= HASH_DMA_PERFORMANCE_MIN_SIZE &&
 568                             hash_dma_valid_data(req->src, req->nbytes)) {
 569                                 req_ctx->dma_mode = true;
 570                         } else {
 571                                 req_ctx->dma_mode = false;
 572                                 pr_debug("%s: DMA mode, but use CPU mode for datalength < %d or non-aligned data, except in last nent\n",
 573                                          __func__,
 574                                          HASH_DMA_PERFORMANCE_MIN_SIZE);
 575                         }
 576                 }
 577         }
 578         return 0;
 579 }
 580 
 581 /**
 582  * hash_processblock - This function processes a single block of 512 bits (64
 583  *                     bytes), word aligned, starting at message.
 584  * @device_data:        Structure for the hash device.
 585  * @message:            Block (512 bits) of message to be written to
 586  *                      the HASH hardware.
 587  *
 588  */
 589 static void hash_processblock(struct hash_device_data *device_data,
 590                               const u32 *message, int length)
 591 {
 592         int len = length / HASH_BYTES_PER_WORD;
 593         /*
 594          * NBLW bits. Reset the number of bits in last word (NBLW).
 595          */
 596         HASH_CLEAR_BITS(&device_data->base->str, HASH_STR_NBLW_MASK);
 597 
 598         /*
 599          * Write message data to the HASH_DIN register.
 600          */
 601         HASH_SET_DIN(message, len);
 602 }
 603 
 604 /**
 605  * hash_messagepad - Pads a message and write the nblw bits.
 606  * @device_data:        Structure for the hash device.
 607  * @message:            Last word of a message.
 608  * @index_bytes:        The number of bytes in the last message.
 609  *
 610  * This function manages the final part of the digest calculation, when less
 611  * than 512 bits (64 bytes) remain in message. This means index_bytes < 64.
 612  *
 613  */
 614 static void hash_messagepad(struct hash_device_data *device_data,
 615                             const u32 *message, u8 index_bytes)
 616 {
 617         int nwords = 1;
 618 
 619         /*
 620          * Clear hash str register, only clear NBLW
 621          * since DCAL will be reset by hardware.
 622          */
 623         HASH_CLEAR_BITS(&device_data->base->str, HASH_STR_NBLW_MASK);
 624 
 625         /* Main loop */
 626         while (index_bytes >= 4) {
 627                 HASH_SET_DIN(message, nwords);
 628                 index_bytes -= 4;
 629                 message++;
 630         }
 631 
 632         if (index_bytes)
 633                 HASH_SET_DIN(message, nwords);
 634 
 635         while (readl(&device_data->base->str) & HASH_STR_DCAL_MASK)
 636                 cpu_relax();
 637 
 638         /* num_of_bytes == 0 => NBLW <- 0 (32 bits valid in DATAIN) */
 639         HASH_SET_NBLW(index_bytes * 8);
 640         dev_dbg(device_data->dev, "%s: DIN=0x%08x NBLW=%lu\n",
 641                 __func__, readl_relaxed(&device_data->base->din),
 642                 readl_relaxed(&device_data->base->str) & HASH_STR_NBLW_MASK);
 643         HASH_SET_DCAL;
 644         dev_dbg(device_data->dev, "%s: after dcal -> DIN=0x%08x NBLW=%lu\n",
 645                 __func__, readl_relaxed(&device_data->base->din),
 646                 readl_relaxed(&device_data->base->str) & HASH_STR_NBLW_MASK);
 647 
 648         while (readl(&device_data->base->str) & HASH_STR_DCAL_MASK)
 649                 cpu_relax();
 650 }
 651 
 652 /**
 653  * hash_incrementlength - Increments the length of the current message.
 654  * @ctx: Hash context
 655  * @incr: Length of message processed already
 656  *
 657  * Overflow cannot occur, because conditions for overflow are checked in
 658  * hash_hw_update.
 659  */
 660 static void hash_incrementlength(struct hash_req_ctx *ctx, u32 incr)
 661 {
 662         ctx->state.length.low_word += incr;
 663 
 664         /* Check for wrap-around */
 665         if (ctx->state.length.low_word < incr)
 666                 ctx->state.length.high_word++;
 667 }
 668 
 669 /**
 670  * hash_setconfiguration - Sets the required configuration for the hash
 671  *                         hardware.
 672  * @device_data:        Structure for the hash device.
 673  * @config:             Pointer to a configuration structure.
 674  */
 675 int hash_setconfiguration(struct hash_device_data *device_data,
 676                           struct hash_config *config)
 677 {
 678         int ret = 0;
 679 
 680         if (config->algorithm != HASH_ALGO_SHA1 &&
 681             config->algorithm != HASH_ALGO_SHA256)
 682                 return -EPERM;
 683 
 684         /*
 685          * DATAFORM bits. Set the DATAFORM bits to 0b11, which means the data
 686          * to be written to HASH_DIN is considered as 32 bits.
 687          */
 688         HASH_SET_DATA_FORMAT(config->data_format);
 689 
 690         /*
 691          * ALGO bit. Set to 0b1 for SHA-1 and 0b0 for SHA-256
 692          */
 693         switch (config->algorithm) {
 694         case HASH_ALGO_SHA1:
 695                 HASH_SET_BITS(&device_data->base->cr, HASH_CR_ALGO_MASK);
 696                 break;
 697 
 698         case HASH_ALGO_SHA256:
 699                 HASH_CLEAR_BITS(&device_data->base->cr, HASH_CR_ALGO_MASK);
 700                 break;
 701 
 702         default:
 703                 dev_err(device_data->dev, "%s: Incorrect algorithm\n",
 704                         __func__);
 705                 return -EPERM;
 706         }
 707 
 708         /*
 709          * MODE bit. This bit selects between HASH or HMAC mode for the
 710          * selected algorithm. 0b0 = HASH and 0b1 = HMAC.
 711          */
 712         if (HASH_OPER_MODE_HASH == config->oper_mode)
 713                 HASH_CLEAR_BITS(&device_data->base->cr,
 714                                 HASH_CR_MODE_MASK);
 715         else if (HASH_OPER_MODE_HMAC == config->oper_mode) {
 716                 HASH_SET_BITS(&device_data->base->cr, HASH_CR_MODE_MASK);
 717                 if (device_data->current_ctx->keylen > HASH_BLOCK_SIZE) {
 718                         /* Truncate key to blocksize */
 719                         dev_dbg(device_data->dev, "%s: LKEY set\n", __func__);
 720                         HASH_SET_BITS(&device_data->base->cr,
 721                                       HASH_CR_LKEY_MASK);
 722                 } else {
 723                         dev_dbg(device_data->dev, "%s: LKEY cleared\n",
 724                                 __func__);
 725                         HASH_CLEAR_BITS(&device_data->base->cr,
 726                                         HASH_CR_LKEY_MASK);
 727                 }
 728         } else {        /* Wrong hash mode */
 729                 ret = -EPERM;
 730                 dev_err(device_data->dev, "%s: HASH_INVALID_PARAMETER!\n",
 731                         __func__);
 732         }
 733         return ret;
 734 }
 735 
 736 /**
 737  * hash_begin - This routine resets some globals and initializes the hash
 738  *              hardware.
 739  * @device_data:        Structure for the hash device.
 740  * @ctx:                Hash context.
 741  */
 742 void hash_begin(struct hash_device_data *device_data, struct hash_ctx *ctx)
 743 {
 744         /* HW and SW initializations */
 745         /* Note: there is no need to initialize buffer and digest members */
 746 
 747         while (readl(&device_data->base->str) & HASH_STR_DCAL_MASK)
 748                 cpu_relax();
 749 
 750         /*
 751          * INIT bit. Set this bit to 0b1 to reset the HASH processor core and
 752          * prepare the initialize the HASH accelerator to compute the message
 753          * digest of a new message.
 754          */
 755         HASH_INITIALIZE;
 756 
 757         /*
 758          * NBLW bits. Reset the number of bits in last word (NBLW).
 759          */
 760         HASH_CLEAR_BITS(&device_data->base->str, HASH_STR_NBLW_MASK);
 761 }
 762 
 763 static int hash_process_data(struct hash_device_data *device_data,
 764                              struct hash_ctx *ctx, struct hash_req_ctx *req_ctx,
 765                              int msg_length, u8 *data_buffer, u8 *buffer,
 766                              u8 *index)
 767 {
 768         int ret = 0;
 769         u32 count;
 770 
 771         do {
 772                 if ((*index + msg_length) < HASH_BLOCK_SIZE) {
 773                         for (count = 0; count < msg_length; count++) {
 774                                 buffer[*index + count] =
 775                                         *(data_buffer + count);
 776                         }
 777                         *index += msg_length;
 778                         msg_length = 0;
 779                 } else {
 780                         if (req_ctx->updated) {
 781                                 ret = hash_resume_state(device_data,
 782                                                 &device_data->state);
 783                                 memmove(req_ctx->state.buffer,
 784                                         device_data->state.buffer,
 785                                         HASH_BLOCK_SIZE);
 786                                 if (ret) {
 787                                         dev_err(device_data->dev,
 788                                                 "%s: hash_resume_state() failed!\n",
 789                                                 __func__);
 790                                         goto out;
 791                                 }
 792                         } else {
 793                                 ret = init_hash_hw(device_data, ctx);
 794                                 if (ret) {
 795                                         dev_err(device_data->dev,
 796                                                 "%s: init_hash_hw() failed!\n",
 797                                                 __func__);
 798                                         goto out;
 799                                 }
 800                                 req_ctx->updated = 1;
 801                         }
 802                         /*
 803                          * If 'data_buffer' is four byte aligned and
 804                          * local buffer does not have any data, we can
 805                          * write data directly from 'data_buffer' to
 806                          * HW peripheral, otherwise we first copy data
 807                          * to a local buffer
 808                          */
 809                         if (IS_ALIGNED((unsigned long)data_buffer, 4) &&
 810                             (0 == *index))
 811                                 hash_processblock(device_data,
 812                                                   (const u32 *)data_buffer,
 813                                                   HASH_BLOCK_SIZE);
 814                         else {
 815                                 for (count = 0;
 816                                      count < (u32)(HASH_BLOCK_SIZE - *index);
 817                                      count++) {
 818                                         buffer[*index + count] =
 819                                                 *(data_buffer + count);
 820                                 }
 821                                 hash_processblock(device_data,
 822                                                   (const u32 *)buffer,
 823                                                   HASH_BLOCK_SIZE);
 824                         }
 825                         hash_incrementlength(req_ctx, HASH_BLOCK_SIZE);
 826                         data_buffer += (HASH_BLOCK_SIZE - *index);
 827 
 828                         msg_length -= (HASH_BLOCK_SIZE - *index);
 829                         *index = 0;
 830 
 831                         ret = hash_save_state(device_data,
 832                                         &device_data->state);
 833 
 834                         memmove(device_data->state.buffer,
 835                                 req_ctx->state.buffer,
 836                                 HASH_BLOCK_SIZE);
 837                         if (ret) {
 838                                 dev_err(device_data->dev, "%s: hash_save_state() failed!\n",
 839                                         __func__);
 840                                 goto out;
 841                         }
 842                 }
 843         } while (msg_length != 0);
 844 out:
 845 
 846         return ret;
 847 }
 848 
 849 /**
 850  * hash_dma_final - The hash dma final function for SHA1/SHA256.
 851  * @req:        The hash request for the job.
 852  */
 853 static int hash_dma_final(struct ahash_request *req)
 854 {
 855         int ret = 0;
 856         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
 857         struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
 858         struct hash_req_ctx *req_ctx = ahash_request_ctx(req);
 859         struct hash_device_data *device_data;
 860         u8 digest[SHA256_DIGEST_SIZE];
 861         int bytes_written = 0;
 862 
 863         ret = hash_get_device_data(ctx, &device_data);
 864         if (ret)
 865                 return ret;
 866 
 867         dev_dbg(device_data->dev, "%s: (ctx=0x%lx)!\n", __func__,
 868                 (unsigned long)ctx);
 869 
 870         if (req_ctx->updated) {
 871                 ret = hash_resume_state(device_data, &device_data->state);
 872 
 873                 if (ret) {
 874                         dev_err(device_data->dev, "%s: hash_resume_state() failed!\n",
 875                                 __func__);
 876                         goto out;
 877                 }
 878         }
 879 
 880         if (!req_ctx->updated) {
 881                 ret = hash_setconfiguration(device_data, &ctx->config);
 882                 if (ret) {
 883                         dev_err(device_data->dev,
 884                                 "%s: hash_setconfiguration() failed!\n",
 885                                 __func__);
 886                         goto out;
 887                 }
 888 
 889                 /* Enable DMA input */
 890                 if (hash_mode != HASH_MODE_DMA || !req_ctx->dma_mode) {
 891                         HASH_CLEAR_BITS(&device_data->base->cr,
 892                                         HASH_CR_DMAE_MASK);
 893                 } else {
 894                         HASH_SET_BITS(&device_data->base->cr,
 895                                       HASH_CR_DMAE_MASK);
 896                         HASH_SET_BITS(&device_data->base->cr,
 897                                       HASH_CR_PRIVN_MASK);
 898                 }
 899 
 900                 HASH_INITIALIZE;
 901 
 902                 if (ctx->config.oper_mode == HASH_OPER_MODE_HMAC)
 903                         hash_hw_write_key(device_data, ctx->key, ctx->keylen);
 904 
 905                 /* Number of bits in last word = (nbytes * 8) % 32 */
 906                 HASH_SET_NBLW((req->nbytes * 8) % 32);
 907                 req_ctx->updated = 1;
 908         }
 909 
 910         /* Store the nents in the dma struct. */
 911         ctx->device->dma.nents = hash_get_nents(req->src, req->nbytes, NULL);
 912         if (!ctx->device->dma.nents) {
 913                 dev_err(device_data->dev, "%s: ctx->device->dma.nents = 0\n",
 914                         __func__);
 915                 ret = ctx->device->dma.nents;
 916                 goto out;
 917         }
 918 
 919         bytes_written = hash_dma_write(ctx, req->src, req->nbytes);
 920         if (bytes_written != req->nbytes) {
 921                 dev_err(device_data->dev, "%s: hash_dma_write() failed!\n",
 922                         __func__);
 923                 ret = bytes_written;
 924                 goto out;
 925         }
 926 
 927         wait_for_completion(&ctx->device->dma.complete);
 928         hash_dma_done(ctx);
 929 
 930         while (readl(&device_data->base->str) & HASH_STR_DCAL_MASK)
 931                 cpu_relax();
 932 
 933         if (ctx->config.oper_mode == HASH_OPER_MODE_HMAC && ctx->key) {
 934                 unsigned int keylen = ctx->keylen;
 935                 u8 *key = ctx->key;
 936 
 937                 dev_dbg(device_data->dev, "%s: keylen: %d\n",
 938                         __func__, ctx->keylen);
 939                 hash_hw_write_key(device_data, key, keylen);
 940         }
 941 
 942         hash_get_digest(device_data, digest, ctx->config.algorithm);
 943         memcpy(req->result, digest, ctx->digestsize);
 944 
 945 out:
 946         release_hash_device(device_data);
 947 
 948         /**
 949          * Allocated in setkey, and only used in HMAC.
 950          */
 951         kfree(ctx->key);
 952 
 953         return ret;
 954 }
 955 
 956 /**
 957  * hash_hw_final - The final hash calculation function
 958  * @req:        The hash request for the job.
 959  */
 960 static int hash_hw_final(struct ahash_request *req)
 961 {
 962         int ret = 0;
 963         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
 964         struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
 965         struct hash_req_ctx *req_ctx = ahash_request_ctx(req);
 966         struct hash_device_data *device_data;
 967         u8 digest[SHA256_DIGEST_SIZE];
 968 
 969         ret = hash_get_device_data(ctx, &device_data);
 970         if (ret)
 971                 return ret;
 972 
 973         dev_dbg(device_data->dev, "%s: (ctx=0x%lx)!\n", __func__,
 974                 (unsigned long)ctx);
 975 
 976         if (req_ctx->updated) {
 977                 ret = hash_resume_state(device_data, &device_data->state);
 978 
 979                 if (ret) {
 980                         dev_err(device_data->dev,
 981                                 "%s: hash_resume_state() failed!\n", __func__);
 982                         goto out;
 983                 }
 984         } else if (req->nbytes == 0 && ctx->keylen == 0) {
 985                 u8 zero_hash[SHA256_DIGEST_SIZE];
 986                 u32 zero_hash_size = 0;
 987                 bool zero_digest = false;
 988                 /**
 989                  * Use a pre-calculated empty message digest
 990                  * (workaround since hw return zeroes, hw bug!?)
 991                  */
 992                 ret = get_empty_message_digest(device_data, &zero_hash[0],
 993                                 &zero_hash_size, &zero_digest);
 994                 if (!ret && likely(zero_hash_size == ctx->digestsize) &&
 995                     zero_digest) {
 996                         memcpy(req->result, &zero_hash[0], ctx->digestsize);
 997                         goto out;
 998                 } else if (!ret && !zero_digest) {
 999                         dev_dbg(device_data->dev,
1000                                 "%s: HMAC zero msg with key, continue...\n",
1001                                 __func__);
1002                 } else {
1003                         dev_err(device_data->dev,
1004                                 "%s: ret=%d, or wrong digest size? %s\n",
1005                                 __func__, ret,
1006                                 zero_hash_size == ctx->digestsize ?
1007                                 "true" : "false");
1008                         /* Return error */
1009                         goto out;
1010                 }
1011         } else if (req->nbytes == 0 && ctx->keylen > 0) {
1012                 dev_err(device_data->dev, "%s: Empty message with keylength > 0, NOT supported\n",
1013                         __func__);
1014                 goto out;
1015         }
1016 
1017         if (!req_ctx->updated) {
1018                 ret = init_hash_hw(device_data, ctx);
1019                 if (ret) {
1020                         dev_err(device_data->dev,
1021                                 "%s: init_hash_hw() failed!\n", __func__);
1022                         goto out;
1023                 }
1024         }
1025 
1026         if (req_ctx->state.index) {
1027                 hash_messagepad(device_data, req_ctx->state.buffer,
1028                                 req_ctx->state.index);
1029         } else {
1030                 HASH_SET_DCAL;
1031                 while (readl(&device_data->base->str) & HASH_STR_DCAL_MASK)
1032                         cpu_relax();
1033         }
1034 
1035         if (ctx->config.oper_mode == HASH_OPER_MODE_HMAC && ctx->key) {
1036                 unsigned int keylen = ctx->keylen;
1037                 u8 *key = ctx->key;
1038 
1039                 dev_dbg(device_data->dev, "%s: keylen: %d\n",
1040                         __func__, ctx->keylen);
1041                 hash_hw_write_key(device_data, key, keylen);
1042         }
1043 
1044         hash_get_digest(device_data, digest, ctx->config.algorithm);
1045         memcpy(req->result, digest, ctx->digestsize);
1046 
1047 out:
1048         release_hash_device(device_data);
1049 
1050         /**
1051          * Allocated in setkey, and only used in HMAC.
1052          */
1053         kfree(ctx->key);
1054 
1055         return ret;
1056 }
1057 
1058 /**
1059  * hash_hw_update - Updates current HASH computation hashing another part of
1060  *                  the message.
1061  * @req:        Byte array containing the message to be hashed (caller
1062  *              allocated).
1063  */
1064 int hash_hw_update(struct ahash_request *req)
1065 {
1066         int ret = 0;
1067         u8 index = 0;
1068         u8 *buffer;
1069         struct hash_device_data *device_data;
1070         u8 *data_buffer;
1071         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1072         struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
1073         struct hash_req_ctx *req_ctx = ahash_request_ctx(req);
1074         struct crypto_hash_walk walk;
1075         int msg_length = crypto_hash_walk_first(req, &walk);
1076 
1077         /* Empty message ("") is correct indata */
1078         if (msg_length == 0)
1079                 return ret;
1080 
1081         index = req_ctx->state.index;
1082         buffer = (u8 *)req_ctx->state.buffer;
1083 
1084         /* Check if ctx->state.length + msg_length
1085            overflows */
1086         if (msg_length > (req_ctx->state.length.low_word + msg_length) &&
1087             HASH_HIGH_WORD_MAX_VAL == req_ctx->state.length.high_word) {
1088                 pr_err("%s: HASH_MSG_LENGTH_OVERFLOW!\n", __func__);
1089                 return -EPERM;
1090         }
1091 
1092         ret = hash_get_device_data(ctx, &device_data);
1093         if (ret)
1094                 return ret;
1095 
1096         /* Main loop */
1097         while (0 != msg_length) {
1098                 data_buffer = walk.data;
1099                 ret = hash_process_data(device_data, ctx, req_ctx, msg_length,
1100                                 data_buffer, buffer, &index);
1101 
1102                 if (ret) {
1103                         dev_err(device_data->dev, "%s: hash_internal_hw_update() failed!\n",
1104                                 __func__);
1105                         goto out;
1106                 }
1107 
1108                 msg_length = crypto_hash_walk_done(&walk, 0);
1109         }
1110 
1111         req_ctx->state.index = index;
1112         dev_dbg(device_data->dev, "%s: indata length=%d, bin=%d\n",
1113                 __func__, req_ctx->state.index, req_ctx->state.bit_index);
1114 
1115 out:
1116         release_hash_device(device_data);
1117 
1118         return ret;
1119 }
1120 
1121 /**
1122  * hash_resume_state - Function that resumes the state of an calculation.
1123  * @device_data:        Pointer to the device structure.
1124  * @device_state:       The state to be restored in the hash hardware
1125  */
1126 int hash_resume_state(struct hash_device_data *device_data,
1127                       const struct hash_state *device_state)
1128 {
1129         u32 temp_cr;
1130         s32 count;
1131         int hash_mode = HASH_OPER_MODE_HASH;
1132 
1133         if (NULL == device_state) {
1134                 dev_err(device_data->dev, "%s: HASH_INVALID_PARAMETER!\n",
1135                         __func__);
1136                 return -EPERM;
1137         }
1138 
1139         /* Check correctness of index and length members */
1140         if (device_state->index > HASH_BLOCK_SIZE ||
1141             (device_state->length.low_word % HASH_BLOCK_SIZE) != 0) {
1142                 dev_err(device_data->dev, "%s: HASH_INVALID_PARAMETER!\n",
1143                         __func__);
1144                 return -EPERM;
1145         }
1146 
1147         /*
1148          * INIT bit. Set this bit to 0b1 to reset the HASH processor core and
1149          * prepare the initialize the HASH accelerator to compute the message
1150          * digest of a new message.
1151          */
1152         HASH_INITIALIZE;
1153 
1154         temp_cr = device_state->temp_cr;
1155         writel_relaxed(temp_cr & HASH_CR_RESUME_MASK, &device_data->base->cr);
1156 
1157         if (readl(&device_data->base->cr) & HASH_CR_MODE_MASK)
1158                 hash_mode = HASH_OPER_MODE_HMAC;
1159         else
1160                 hash_mode = HASH_OPER_MODE_HASH;
1161 
1162         for (count = 0; count < HASH_CSR_COUNT; count++) {
1163                 if ((count >= 36) && (hash_mode == HASH_OPER_MODE_HASH))
1164                         break;
1165 
1166                 writel_relaxed(device_state->csr[count],
1167                                &device_data->base->csrx[count]);
1168         }
1169 
1170         writel_relaxed(device_state->csfull, &device_data->base->csfull);
1171         writel_relaxed(device_state->csdatain, &device_data->base->csdatain);
1172 
1173         writel_relaxed(device_state->str_reg, &device_data->base->str);
1174         writel_relaxed(temp_cr, &device_data->base->cr);
1175 
1176         return 0;
1177 }
1178 
1179 /**
1180  * hash_save_state - Function that saves the state of hardware.
1181  * @device_data:        Pointer to the device structure.
1182  * @device_state:       The strucure where the hardware state should be saved.
1183  */
1184 int hash_save_state(struct hash_device_data *device_data,
1185                     struct hash_state *device_state)
1186 {
1187         u32 temp_cr;
1188         u32 count;
1189         int hash_mode = HASH_OPER_MODE_HASH;
1190 
1191         if (NULL == device_state) {
1192                 dev_err(device_data->dev, "%s: HASH_INVALID_PARAMETER!\n",
1193                         __func__);
1194                 return -ENOTSUPP;
1195         }
1196 
1197         /* Write dummy value to force digest intermediate calculation. This
1198          * actually makes sure that there isn't any ongoing calculation in the
1199          * hardware.
1200          */
1201         while (readl(&device_data->base->str) & HASH_STR_DCAL_MASK)
1202                 cpu_relax();
1203 
1204         temp_cr = readl_relaxed(&device_data->base->cr);
1205 
1206         device_state->str_reg = readl_relaxed(&device_data->base->str);
1207 
1208         device_state->din_reg = readl_relaxed(&device_data->base->din);
1209 
1210         if (readl(&device_data->base->cr) & HASH_CR_MODE_MASK)
1211                 hash_mode = HASH_OPER_MODE_HMAC;
1212         else
1213                 hash_mode = HASH_OPER_MODE_HASH;
1214 
1215         for (count = 0; count < HASH_CSR_COUNT; count++) {
1216                 if ((count >= 36) && (hash_mode == HASH_OPER_MODE_HASH))
1217                         break;
1218 
1219                 device_state->csr[count] =
1220                         readl_relaxed(&device_data->base->csrx[count]);
1221         }
1222 
1223         device_state->csfull = readl_relaxed(&device_data->base->csfull);
1224         device_state->csdatain = readl_relaxed(&device_data->base->csdatain);
1225 
1226         device_state->temp_cr = temp_cr;
1227 
1228         return 0;
1229 }
1230 
1231 /**
1232  * hash_check_hw - This routine checks for peripheral Ids and PCell Ids.
1233  * @device_data:
1234  *
1235  */
1236 int hash_check_hw(struct hash_device_data *device_data)
1237 {
1238         /* Checking Peripheral Ids  */
1239         if (HASH_P_ID0 == readl_relaxed(&device_data->base->periphid0) &&
1240             HASH_P_ID1 == readl_relaxed(&device_data->base->periphid1) &&
1241             HASH_P_ID2 == readl_relaxed(&device_data->base->periphid2) &&
1242             HASH_P_ID3 == readl_relaxed(&device_data->base->periphid3) &&
1243             HASH_CELL_ID0 == readl_relaxed(&device_data->base->cellid0) &&
1244             HASH_CELL_ID1 == readl_relaxed(&device_data->base->cellid1) &&
1245             HASH_CELL_ID2 == readl_relaxed(&device_data->base->cellid2) &&
1246             HASH_CELL_ID3 == readl_relaxed(&device_data->base->cellid3)) {
1247                 return 0;
1248         }
1249 
1250         dev_err(device_data->dev, "%s: HASH_UNSUPPORTED_HW!\n", __func__);
1251         return -ENOTSUPP;
1252 }
1253 
1254 /**
1255  * hash_get_digest - Gets the digest.
1256  * @device_data:        Pointer to the device structure.
1257  * @digest:             User allocated byte array for the calculated digest.
1258  * @algorithm:          The algorithm in use.
1259  */
1260 void hash_get_digest(struct hash_device_data *device_data,
1261                      u8 *digest, int algorithm)
1262 {
1263         u32 temp_hx_val, count;
1264         int loop_ctr;
1265 
1266         if (algorithm != HASH_ALGO_SHA1 && algorithm != HASH_ALGO_SHA256) {
1267                 dev_err(device_data->dev, "%s: Incorrect algorithm %d\n",
1268                         __func__, algorithm);
1269                 return;
1270         }
1271 
1272         if (algorithm == HASH_ALGO_SHA1)
1273                 loop_ctr = SHA1_DIGEST_SIZE / sizeof(u32);
1274         else
1275                 loop_ctr = SHA256_DIGEST_SIZE / sizeof(u32);
1276 
1277         dev_dbg(device_data->dev, "%s: digest array:(0x%lx)\n",
1278                 __func__, (unsigned long)digest);
1279 
1280         /* Copy result into digest array */
1281         for (count = 0; count < loop_ctr; count++) {
1282                 temp_hx_val = readl_relaxed(&device_data->base->hx[count]);
1283                 digest[count * 4] = (u8) ((temp_hx_val >> 24) & 0xFF);
1284                 digest[count * 4 + 1] = (u8) ((temp_hx_val >> 16) & 0xFF);
1285                 digest[count * 4 + 2] = (u8) ((temp_hx_val >> 8) & 0xFF);
1286                 digest[count * 4 + 3] = (u8) ((temp_hx_val >> 0) & 0xFF);
1287         }
1288 }
1289 
1290 /**
1291  * hash_update - The hash update function for SHA1/SHA2 (SHA256).
1292  * @req: The hash request for the job.
1293  */
1294 static int ahash_update(struct ahash_request *req)
1295 {
1296         int ret = 0;
1297         struct hash_req_ctx *req_ctx = ahash_request_ctx(req);
1298 
1299         if (hash_mode != HASH_MODE_DMA || !req_ctx->dma_mode)
1300                 ret = hash_hw_update(req);
1301         /* Skip update for DMA, all data will be passed to DMA in final */
1302 
1303         if (ret) {
1304                 pr_err("%s: hash_hw_update() failed!\n", __func__);
1305         }
1306 
1307         return ret;
1308 }
1309 
1310 /**
1311  * hash_final - The hash final function for SHA1/SHA2 (SHA256).
1312  * @req:        The hash request for the job.
1313  */
1314 static int ahash_final(struct ahash_request *req)
1315 {
1316         int ret = 0;
1317         struct hash_req_ctx *req_ctx = ahash_request_ctx(req);
1318 
1319         pr_debug("%s: data size: %d\n", __func__, req->nbytes);
1320 
1321         if ((hash_mode == HASH_MODE_DMA) && req_ctx->dma_mode)
1322                 ret = hash_dma_final(req);
1323         else
1324                 ret = hash_hw_final(req);
1325 
1326         if (ret) {
1327                 pr_err("%s: hash_hw/dma_final() failed\n", __func__);
1328         }
1329 
1330         return ret;
1331 }
1332 
1333 static int hash_setkey(struct crypto_ahash *tfm,
1334                        const u8 *key, unsigned int keylen, int alg)
1335 {
1336         int ret = 0;
1337         struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
1338 
1339         /**
1340          * Freed in final.
1341          */
1342         ctx->key = kmemdup(key, keylen, GFP_KERNEL);
1343         if (!ctx->key) {
1344                 pr_err("%s: Failed to allocate ctx->key for %d\n",
1345                        __func__, alg);
1346                 return -ENOMEM;
1347         }
1348         ctx->keylen = keylen;
1349 
1350         return ret;
1351 }
1352 
1353 static int ahash_sha1_init(struct ahash_request *req)
1354 {
1355         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1356         struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
1357 
1358         ctx->config.data_format = HASH_DATA_8_BITS;
1359         ctx->config.algorithm = HASH_ALGO_SHA1;
1360         ctx->config.oper_mode = HASH_OPER_MODE_HASH;
1361         ctx->digestsize = SHA1_DIGEST_SIZE;
1362 
1363         return hash_init(req);
1364 }
1365 
1366 static int ahash_sha256_init(struct ahash_request *req)
1367 {
1368         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1369         struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
1370 
1371         ctx->config.data_format = HASH_DATA_8_BITS;
1372         ctx->config.algorithm = HASH_ALGO_SHA256;
1373         ctx->config.oper_mode = HASH_OPER_MODE_HASH;
1374         ctx->digestsize = SHA256_DIGEST_SIZE;
1375 
1376         return hash_init(req);
1377 }
1378 
1379 static int ahash_sha1_digest(struct ahash_request *req)
1380 {
1381         int ret2, ret1;
1382 
1383         ret1 = ahash_sha1_init(req);
1384         if (ret1)
1385                 goto out;
1386 
1387         ret1 = ahash_update(req);
1388         ret2 = ahash_final(req);
1389 
1390 out:
1391         return ret1 ? ret1 : ret2;
1392 }
1393 
1394 static int ahash_sha256_digest(struct ahash_request *req)
1395 {
1396         int ret2, ret1;
1397 
1398         ret1 = ahash_sha256_init(req);
1399         if (ret1)
1400                 goto out;
1401 
1402         ret1 = ahash_update(req);
1403         ret2 = ahash_final(req);
1404 
1405 out:
1406         return ret1 ? ret1 : ret2;
1407 }
1408 
1409 static int ahash_noimport(struct ahash_request *req, const void *in)
1410 {
1411         return -ENOSYS;
1412 }
1413 
1414 static int ahash_noexport(struct ahash_request *req, void *out)
1415 {
1416         return -ENOSYS;
1417 }
1418 
1419 static int hmac_sha1_init(struct ahash_request *req)
1420 {
1421         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1422         struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
1423 
1424         ctx->config.data_format = HASH_DATA_8_BITS;
1425         ctx->config.algorithm   = HASH_ALGO_SHA1;
1426         ctx->config.oper_mode   = HASH_OPER_MODE_HMAC;
1427         ctx->digestsize         = SHA1_DIGEST_SIZE;
1428 
1429         return hash_init(req);
1430 }
1431 
1432 static int hmac_sha256_init(struct ahash_request *req)
1433 {
1434         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1435         struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
1436 
1437         ctx->config.data_format = HASH_DATA_8_BITS;
1438         ctx->config.algorithm   = HASH_ALGO_SHA256;
1439         ctx->config.oper_mode   = HASH_OPER_MODE_HMAC;
1440         ctx->digestsize         = SHA256_DIGEST_SIZE;
1441 
1442         return hash_init(req);
1443 }
1444 
1445 static int hmac_sha1_digest(struct ahash_request *req)
1446 {
1447         int ret2, ret1;
1448 
1449         ret1 = hmac_sha1_init(req);
1450         if (ret1)
1451                 goto out;
1452 
1453         ret1 = ahash_update(req);
1454         ret2 = ahash_final(req);
1455 
1456 out:
1457         return ret1 ? ret1 : ret2;
1458 }
1459 
1460 static int hmac_sha256_digest(struct ahash_request *req)
1461 {
1462         int ret2, ret1;
1463 
1464         ret1 = hmac_sha256_init(req);
1465         if (ret1)
1466                 goto out;
1467 
1468         ret1 = ahash_update(req);
1469         ret2 = ahash_final(req);
1470 
1471 out:
1472         return ret1 ? ret1 : ret2;
1473 }
1474 
1475 static int hmac_sha1_setkey(struct crypto_ahash *tfm,
1476                             const u8 *key, unsigned int keylen)
1477 {
1478         return hash_setkey(tfm, key, keylen, HASH_ALGO_SHA1);
1479 }
1480 
1481 static int hmac_sha256_setkey(struct crypto_ahash *tfm,
1482                               const u8 *key, unsigned int keylen)
1483 {
1484         return hash_setkey(tfm, key, keylen, HASH_ALGO_SHA256);
1485 }
1486 
1487 struct hash_algo_template {
1488         struct hash_config conf;
1489         struct ahash_alg hash;
1490 };
1491 
1492 static int hash_cra_init(struct crypto_tfm *tfm)
1493 {
1494         struct hash_ctx *ctx = crypto_tfm_ctx(tfm);
1495         struct crypto_alg *alg = tfm->__crt_alg;
1496         struct hash_algo_template *hash_alg;
1497 
1498         hash_alg = container_of(__crypto_ahash_alg(alg),
1499                         struct hash_algo_template,
1500                         hash);
1501 
1502         crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1503                                  sizeof(struct hash_req_ctx));
1504 
1505         ctx->config.data_format = HASH_DATA_8_BITS;
1506         ctx->config.algorithm = hash_alg->conf.algorithm;
1507         ctx->config.oper_mode = hash_alg->conf.oper_mode;
1508 
1509         ctx->digestsize = hash_alg->hash.halg.digestsize;
1510 
1511         return 0;
1512 }
1513 
1514 static struct hash_algo_template hash_algs[] = {
1515         {
1516                 .conf.algorithm = HASH_ALGO_SHA1,
1517                 .conf.oper_mode = HASH_OPER_MODE_HASH,
1518                 .hash = {
1519                         .init = hash_init,
1520                         .update = ahash_update,
1521                         .final = ahash_final,
1522                         .digest = ahash_sha1_digest,
1523                         .export = ahash_noexport,
1524                         .import = ahash_noimport,
1525                         .halg.digestsize = SHA1_DIGEST_SIZE,
1526                         .halg.statesize = sizeof(struct hash_ctx),
1527                         .halg.base = {
1528                                 .cra_name = "sha1",
1529                                 .cra_driver_name = "sha1-ux500",
1530                                 .cra_flags = CRYPTO_ALG_ASYNC,
1531                                 .cra_blocksize = SHA1_BLOCK_SIZE,
1532                                 .cra_ctxsize = sizeof(struct hash_ctx),
1533                                 .cra_init = hash_cra_init,
1534                                 .cra_module = THIS_MODULE,
1535                         }
1536                 }
1537         },
1538         {
1539                 .conf.algorithm = HASH_ALGO_SHA256,
1540                 .conf.oper_mode = HASH_OPER_MODE_HASH,
1541                 .hash = {
1542                         .init = hash_init,
1543                         .update = ahash_update,
1544                         .final = ahash_final,
1545                         .digest = ahash_sha256_digest,
1546                         .export = ahash_noexport,
1547                         .import = ahash_noimport,
1548                         .halg.digestsize = SHA256_DIGEST_SIZE,
1549                         .halg.statesize = sizeof(struct hash_ctx),
1550                         .halg.base = {
1551                                 .cra_name = "sha256",
1552                                 .cra_driver_name = "sha256-ux500",
1553                                 .cra_flags = CRYPTO_ALG_ASYNC,
1554                                 .cra_blocksize = SHA256_BLOCK_SIZE,
1555                                 .cra_ctxsize = sizeof(struct hash_ctx),
1556                                 .cra_init = hash_cra_init,
1557                                 .cra_module = THIS_MODULE,
1558                         }
1559                 }
1560         },
1561         {
1562                 .conf.algorithm = HASH_ALGO_SHA1,
1563                 .conf.oper_mode = HASH_OPER_MODE_HMAC,
1564                         .hash = {
1565                         .init = hash_init,
1566                         .update = ahash_update,
1567                         .final = ahash_final,
1568                         .digest = hmac_sha1_digest,
1569                         .setkey = hmac_sha1_setkey,
1570                         .export = ahash_noexport,
1571                         .import = ahash_noimport,
1572                         .halg.digestsize = SHA1_DIGEST_SIZE,
1573                         .halg.statesize = sizeof(struct hash_ctx),
1574                         .halg.base = {
1575                                 .cra_name = "hmac(sha1)",
1576                                 .cra_driver_name = "hmac-sha1-ux500",
1577                                 .cra_flags = CRYPTO_ALG_ASYNC,
1578                                 .cra_blocksize = SHA1_BLOCK_SIZE,
1579                                 .cra_ctxsize = sizeof(struct hash_ctx),
1580                                 .cra_init = hash_cra_init,
1581                                 .cra_module = THIS_MODULE,
1582                         }
1583                 }
1584         },
1585         {
1586                 .conf.algorithm = HASH_ALGO_SHA256,
1587                 .conf.oper_mode = HASH_OPER_MODE_HMAC,
1588                 .hash = {
1589                         .init = hash_init,
1590                         .update = ahash_update,
1591                         .final = ahash_final,
1592                         .digest = hmac_sha256_digest,
1593                         .setkey = hmac_sha256_setkey,
1594                         .export = ahash_noexport,
1595                         .import = ahash_noimport,
1596                         .halg.digestsize = SHA256_DIGEST_SIZE,
1597                         .halg.statesize = sizeof(struct hash_ctx),
1598                         .halg.base = {
1599                                 .cra_name = "hmac(sha256)",
1600                                 .cra_driver_name = "hmac-sha256-ux500",
1601                                 .cra_flags = CRYPTO_ALG_ASYNC,
1602                                 .cra_blocksize = SHA256_BLOCK_SIZE,
1603                                 .cra_ctxsize = sizeof(struct hash_ctx),
1604                                 .cra_init = hash_cra_init,
1605                                 .cra_module = THIS_MODULE,
1606                         }
1607                 }
1608         }
1609 };
1610 
1611 /**
1612  * hash_algs_register_all -
1613  */
1614 static int ahash_algs_register_all(struct hash_device_data *device_data)
1615 {
1616         int ret;
1617         int i;
1618         int count;
1619 
1620         for (i = 0; i < ARRAY_SIZE(hash_algs); i++) {
1621                 ret = crypto_register_ahash(&hash_algs[i].hash);
1622                 if (ret) {
1623                         count = i;
1624                         dev_err(device_data->dev, "%s: alg registration failed\n",
1625                                 hash_algs[i].hash.halg.base.cra_driver_name);
1626                         goto unreg;
1627                 }
1628         }
1629         return 0;
1630 unreg:
1631         for (i = 0; i < count; i++)
1632                 crypto_unregister_ahash(&hash_algs[i].hash);
1633         return ret;
1634 }
1635 
1636 /**
1637  * hash_algs_unregister_all -
1638  */
1639 static void ahash_algs_unregister_all(struct hash_device_data *device_data)
1640 {
1641         int i;
1642 
1643         for (i = 0; i < ARRAY_SIZE(hash_algs); i++)
1644                 crypto_unregister_ahash(&hash_algs[i].hash);
1645 }
1646 
1647 /**
1648  * ux500_hash_probe - Function that probes the hash hardware.
1649  * @pdev: The platform device.
1650  */
1651 static int ux500_hash_probe(struct platform_device *pdev)
1652 {
1653         int                     ret = 0;
1654         struct resource         *res = NULL;
1655         struct hash_device_data *device_data;
1656         struct device           *dev = &pdev->dev;
1657 
1658         device_data = devm_kzalloc(dev, sizeof(*device_data), GFP_ATOMIC);
1659         if (!device_data) {
1660                 ret = -ENOMEM;
1661                 goto out;
1662         }
1663 
1664         device_data->dev = dev;
1665         device_data->current_ctx = NULL;
1666 
1667         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1668         if (!res) {
1669                 dev_dbg(dev, "%s: platform_get_resource() failed!\n", __func__);
1670                 ret = -ENODEV;
1671                 goto out;
1672         }
1673 
1674         device_data->phybase = res->start;
1675         device_data->base = devm_ioremap_resource(dev, res);
1676         if (IS_ERR(device_data->base)) {
1677                 dev_err(dev, "%s: ioremap() failed!\n", __func__);
1678                 ret = PTR_ERR(device_data->base);
1679                 goto out;
1680         }
1681         spin_lock_init(&device_data->ctx_lock);
1682         spin_lock_init(&device_data->power_state_lock);
1683 
1684         /* Enable power for HASH1 hardware block */
1685         device_data->regulator = regulator_get(dev, "v-ape");
1686         if (IS_ERR(device_data->regulator)) {
1687                 dev_err(dev, "%s: regulator_get() failed!\n", __func__);
1688                 ret = PTR_ERR(device_data->regulator);
1689                 device_data->regulator = NULL;
1690                 goto out;
1691         }
1692 
1693         /* Enable the clock for HASH1 hardware block */
1694         device_data->clk = devm_clk_get(dev, NULL);
1695         if (IS_ERR(device_data->clk)) {
1696                 dev_err(dev, "%s: clk_get() failed!\n", __func__);
1697                 ret = PTR_ERR(device_data->clk);
1698                 goto out_regulator;
1699         }
1700 
1701         ret = clk_prepare(device_data->clk);
1702         if (ret) {
1703                 dev_err(dev, "%s: clk_prepare() failed!\n", __func__);
1704                 goto out_regulator;
1705         }
1706 
1707         /* Enable device power (and clock) */
1708         ret = hash_enable_power(device_data, false);
1709         if (ret) {
1710                 dev_err(dev, "%s: hash_enable_power() failed!\n", __func__);
1711                 goto out_clk_unprepare;
1712         }
1713 
1714         ret = hash_check_hw(device_data);
1715         if (ret) {
1716                 dev_err(dev, "%s: hash_check_hw() failed!\n", __func__);
1717                 goto out_power;
1718         }
1719 
1720         if (hash_mode == HASH_MODE_DMA)
1721                 hash_dma_setup_channel(device_data, dev);
1722 
1723         platform_set_drvdata(pdev, device_data);
1724 
1725         /* Put the new device into the device list... */
1726         klist_add_tail(&device_data->list_node, &driver_data.device_list);
1727         /* ... and signal that a new device is available. */
1728         up(&driver_data.device_allocation);
1729 
1730         ret = ahash_algs_register_all(device_data);
1731         if (ret) {
1732                 dev_err(dev, "%s: ahash_algs_register_all() failed!\n",
1733                         __func__);
1734                 goto out_power;
1735         }
1736 
1737         dev_info(dev, "successfully registered\n");
1738         return 0;
1739 
1740 out_power:
1741         hash_disable_power(device_data, false);
1742 
1743 out_clk_unprepare:
1744         clk_unprepare(device_data->clk);
1745 
1746 out_regulator:
1747         regulator_put(device_data->regulator);
1748 
1749 out:
1750         return ret;
1751 }
1752 
1753 /**
1754  * ux500_hash_remove - Function that removes the hash device from the platform.
1755  * @pdev: The platform device.
1756  */
1757 static int ux500_hash_remove(struct platform_device *pdev)
1758 {
1759         struct hash_device_data *device_data;
1760         struct device           *dev = &pdev->dev;
1761 
1762         device_data = platform_get_drvdata(pdev);
1763         if (!device_data) {
1764                 dev_err(dev, "%s: platform_get_drvdata() failed!\n", __func__);
1765                 return -ENOMEM;
1766         }
1767 
1768         /* Try to decrease the number of available devices. */
1769         if (down_trylock(&driver_data.device_allocation))
1770                 return -EBUSY;
1771 
1772         /* Check that the device is free */
1773         spin_lock(&device_data->ctx_lock);
1774         /* current_ctx allocates a device, NULL = unallocated */
1775         if (device_data->current_ctx) {
1776                 /* The device is busy */
1777                 spin_unlock(&device_data->ctx_lock);
1778                 /* Return the device to the pool. */
1779                 up(&driver_data.device_allocation);
1780                 return -EBUSY;
1781         }
1782 
1783         spin_unlock(&device_data->ctx_lock);
1784 
1785         /* Remove the device from the list */
1786         if (klist_node_attached(&device_data->list_node))
1787                 klist_remove(&device_data->list_node);
1788 
1789         /* If this was the last device, remove the services */
1790         if (list_empty(&driver_data.device_list.k_list))
1791                 ahash_algs_unregister_all(device_data);
1792 
1793         if (hash_disable_power(device_data, false))
1794                 dev_err(dev, "%s: hash_disable_power() failed\n",
1795                         __func__);
1796 
1797         clk_unprepare(device_data->clk);
1798         regulator_put(device_data->regulator);
1799 
1800         return 0;
1801 }
1802 
1803 /**
1804  * ux500_hash_shutdown - Function that shutdown the hash device.
1805  * @pdev: The platform device
1806  */
1807 static void ux500_hash_shutdown(struct platform_device *pdev)
1808 {
1809         struct hash_device_data *device_data;
1810 
1811         device_data = platform_get_drvdata(pdev);
1812         if (!device_data) {
1813                 dev_err(&pdev->dev, "%s: platform_get_drvdata() failed!\n",
1814                         __func__);
1815                 return;
1816         }
1817 
1818         /* Check that the device is free */
1819         spin_lock(&device_data->ctx_lock);
1820         /* current_ctx allocates a device, NULL = unallocated */
1821         if (!device_data->current_ctx) {
1822                 if (down_trylock(&driver_data.device_allocation))
1823                         dev_dbg(&pdev->dev, "%s: Cryp still in use! Shutting down anyway...\n",
1824                                 __func__);
1825                 /**
1826                  * (Allocate the device)
1827                  * Need to set this to non-null (dummy) value,
1828                  * to avoid usage if context switching.
1829                  */
1830                 device_data->current_ctx++;
1831         }
1832         spin_unlock(&device_data->ctx_lock);
1833 
1834         /* Remove the device from the list */
1835         if (klist_node_attached(&device_data->list_node))
1836                 klist_remove(&device_data->list_node);
1837 
1838         /* If this was the last device, remove the services */
1839         if (list_empty(&driver_data.device_list.k_list))
1840                 ahash_algs_unregister_all(device_data);
1841 
1842         if (hash_disable_power(device_data, false))
1843                 dev_err(&pdev->dev, "%s: hash_disable_power() failed\n",
1844                         __func__);
1845 }
1846 
1847 #ifdef CONFIG_PM_SLEEP
1848 /**
1849  * ux500_hash_suspend - Function that suspends the hash device.
1850  * @dev:        Device to suspend.
1851  */
1852 static int ux500_hash_suspend(struct device *dev)
1853 {
1854         int ret;
1855         struct hash_device_data *device_data;
1856         struct hash_ctx *temp_ctx = NULL;
1857 
1858         device_data = dev_get_drvdata(dev);
1859         if (!device_data) {
1860                 dev_err(dev, "%s: platform_get_drvdata() failed!\n", __func__);
1861                 return -ENOMEM;
1862         }
1863 
1864         spin_lock(&device_data->ctx_lock);
1865         if (!device_data->current_ctx)
1866                 device_data->current_ctx++;
1867         spin_unlock(&device_data->ctx_lock);
1868 
1869         if (device_data->current_ctx == ++temp_ctx) {
1870                 if (down_interruptible(&driver_data.device_allocation))
1871                         dev_dbg(dev, "%s: down_interruptible() failed\n",
1872                                 __func__);
1873                 ret = hash_disable_power(device_data, false);
1874 
1875         } else {
1876                 ret = hash_disable_power(device_data, true);
1877         }
1878 
1879         if (ret)
1880                 dev_err(dev, "%s: hash_disable_power()\n", __func__);
1881 
1882         return ret;
1883 }
1884 
1885 /**
1886  * ux500_hash_resume - Function that resume the hash device.
1887  * @dev:        Device to resume.
1888  */
1889 static int ux500_hash_resume(struct device *dev)
1890 {
1891         int ret = 0;
1892         struct hash_device_data *device_data;
1893         struct hash_ctx *temp_ctx = NULL;
1894 
1895         device_data = dev_get_drvdata(dev);
1896         if (!device_data) {
1897                 dev_err(dev, "%s: platform_get_drvdata() failed!\n", __func__);
1898                 return -ENOMEM;
1899         }
1900 
1901         spin_lock(&device_data->ctx_lock);
1902         if (device_data->current_ctx == ++temp_ctx)
1903                 device_data->current_ctx = NULL;
1904         spin_unlock(&device_data->ctx_lock);
1905 
1906         if (!device_data->current_ctx)
1907                 up(&driver_data.device_allocation);
1908         else
1909                 ret = hash_enable_power(device_data, true);
1910 
1911         if (ret)
1912                 dev_err(dev, "%s: hash_enable_power() failed!\n", __func__);
1913 
1914         return ret;
1915 }
1916 #endif
1917 
1918 static SIMPLE_DEV_PM_OPS(ux500_hash_pm, ux500_hash_suspend, ux500_hash_resume);
1919 
1920 static const struct of_device_id ux500_hash_match[] = {
1921         { .compatible = "stericsson,ux500-hash" },
1922         { },
1923 };
1924 MODULE_DEVICE_TABLE(of, ux500_hash_match);
1925 
1926 static struct platform_driver hash_driver = {
1927         .probe  = ux500_hash_probe,
1928         .remove = ux500_hash_remove,
1929         .shutdown = ux500_hash_shutdown,
1930         .driver = {
1931                 .name  = "hash1",
1932                 .of_match_table = ux500_hash_match,
1933                 .pm    = &ux500_hash_pm,
1934         }
1935 };
1936 
1937 /**
1938  * ux500_hash_mod_init - The kernel module init function.
1939  */
1940 static int __init ux500_hash_mod_init(void)
1941 {
1942         klist_init(&driver_data.device_list, NULL, NULL);
1943         /* Initialize the semaphore to 0 devices (locked state) */
1944         sema_init(&driver_data.device_allocation, 0);
1945 
1946         return platform_driver_register(&hash_driver);
1947 }
1948 
1949 /**
1950  * ux500_hash_mod_fini - The kernel module exit function.
1951  */
1952 static void __exit ux500_hash_mod_fini(void)
1953 {
1954         platform_driver_unregister(&hash_driver);
1955 }
1956 
1957 module_init(ux500_hash_mod_init);
1958 module_exit(ux500_hash_mod_fini);
1959 
1960 MODULE_DESCRIPTION("Driver for ST-Ericsson UX500 HASH engine.");
1961 MODULE_LICENSE("GPL");
1962 
1963 MODULE_ALIAS_CRYPTO("sha1-all");
1964 MODULE_ALIAS_CRYPTO("sha256-all");
1965 MODULE_ALIAS_CRYPTO("hmac-sha1-all");
1966 MODULE_ALIAS_CRYPTO("hmac-sha256-all");

/* [<][>][^][v][top][bottom][index][help] */