1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Cryptographic API. 4 * 5 * Support for ATMEL SHA1/SHA256 HW acceleration. 6 * 7 * Copyright (c) 2012 Eukréa Electromatique - ATMEL 8 * Author: Nicolas Royer <nicolas@eukrea.com> 9 * 10 * Some ideas are from omap-sham.c drivers. 11 */ 12 13 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/slab.h> 17 #include <linux/err.h> 18 #include <linux/clk.h> 19 #include <linux/io.h> 20 #include <linux/hw_random.h> 21 #include <linux/platform_device.h> 22 23 #include <linux/device.h> 24 #include <linux/dmaengine.h> 25 #include <linux/init.h> 26 #include <linux/errno.h> 27 #include <linux/interrupt.h> 28 #include <linux/irq.h> 29 #include <linux/scatterlist.h> 30 #include <linux/dma-mapping.h> 31 #include <linux/delay.h> 32 #include <linux/crypto.h> 33 #include <crypto/scatterwalk.h> 34 #include <crypto/algapi.h> 35 #include <crypto/sha1.h> 36 #include <crypto/sha2.h> 37 #include <crypto/hash.h> 38 #include <crypto/internal/hash.h> 39 #include "atmel-sha-regs.h" 40 #include "atmel-authenc.h" 41 42 #define ATMEL_SHA_PRIORITY 300 43 44 /* SHA flags */ 45 #define SHA_FLAGS_BUSY BIT(0) 46 #define SHA_FLAGS_FINAL BIT(1) 47 #define SHA_FLAGS_DMA_ACTIVE BIT(2) 48 #define SHA_FLAGS_OUTPUT_READY BIT(3) 49 #define SHA_FLAGS_INIT BIT(4) 50 #define SHA_FLAGS_CPU BIT(5) 51 #define SHA_FLAGS_DMA_READY BIT(6) 52 #define SHA_FLAGS_DUMP_REG BIT(7) 53 54 /* bits[11:8] are reserved. */ 55 56 #define SHA_FLAGS_FINUP BIT(16) 57 #define SHA_FLAGS_SG BIT(17) 58 #define SHA_FLAGS_ERROR BIT(23) 59 #define SHA_FLAGS_PAD BIT(24) 60 #define SHA_FLAGS_RESTORE BIT(25) 61 #define SHA_FLAGS_IDATAR0 BIT(26) 62 #define SHA_FLAGS_WAIT_DATARDY BIT(27) 63 64 #define SHA_OP_INIT 0 65 #define SHA_OP_UPDATE 1 66 #define SHA_OP_FINAL 2 67 #define SHA_OP_DIGEST 3 68 69 #define SHA_BUFFER_LEN (PAGE_SIZE / 16) 70 71 #define ATMEL_SHA_DMA_THRESHOLD 56 72 73 struct atmel_sha_caps { 74 bool has_dma; 75 bool has_dualbuff; 76 bool has_sha224; 77 bool has_sha_384_512; 78 bool has_uihv; 79 bool has_hmac; 80 }; 81 82 struct atmel_sha_dev; 83 84 /* 85 * .statesize = sizeof(struct atmel_sha_reqctx) must be <= PAGE_SIZE / 8 as 86 * tested by the ahash_prepare_alg() function. 87 */ 88 struct atmel_sha_reqctx { 89 struct atmel_sha_dev *dd; 90 unsigned long flags; 91 unsigned long op; 92 93 u8 digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32)); 94 u64 digcnt[2]; 95 size_t bufcnt; 96 size_t buflen; 97 dma_addr_t dma_addr; 98 99 /* walk state */ 100 struct scatterlist *sg; 101 unsigned int offset; /* offset in current sg */ 102 unsigned int total; /* total request */ 103 104 size_t block_size; 105 size_t hash_size; 106 107 u8 buffer[SHA_BUFFER_LEN + SHA512_BLOCK_SIZE] __aligned(sizeof(u32)); 108 }; 109 110 typedef int (*atmel_sha_fn_t)(struct atmel_sha_dev *); 111 112 struct atmel_sha_ctx { 113 struct atmel_sha_dev *dd; 114 atmel_sha_fn_t start; 115 116 unsigned long flags; 117 }; 118 119 #define ATMEL_SHA_QUEUE_LENGTH 50 120 121 struct atmel_sha_dma { 122 struct dma_chan *chan; 123 struct dma_slave_config dma_conf; 124 struct scatterlist *sg; 125 int nents; 126 unsigned int last_sg_length; 127 }; 128 129 struct atmel_sha_dev { 130 struct list_head list; 131 unsigned long phys_base; 132 struct device *dev; 133 struct clk *iclk; 134 int irq; 135 void __iomem *io_base; 136 137 spinlock_t lock; 138 struct tasklet_struct done_task; 139 struct tasklet_struct queue_task; 140 141 unsigned long flags; 142 struct crypto_queue queue; 143 struct ahash_request *req; 144 bool is_async; 145 bool force_complete; 146 atmel_sha_fn_t resume; 147 atmel_sha_fn_t cpu_transfer_complete; 148 149 struct atmel_sha_dma dma_lch_in; 150 151 struct atmel_sha_caps caps; 152 153 struct scatterlist tmp; 154 155 u32 hw_version; 156 }; 157 158 struct atmel_sha_drv { 159 struct list_head dev_list; 160 spinlock_t lock; 161 }; 162 163 static struct atmel_sha_drv atmel_sha = { 164 .dev_list = LIST_HEAD_INIT(atmel_sha.dev_list), 165 .lock = __SPIN_LOCK_UNLOCKED(atmel_sha.lock), 166 }; 167 168 #ifdef VERBOSE_DEBUG 169 static const char *atmel_sha_reg_name(u32 offset, char *tmp, size_t sz, bool wr) 170 { 171 switch (offset) { 172 case SHA_CR: 173 return "CR"; 174 175 case SHA_MR: 176 return "MR"; 177 178 case SHA_IER: 179 return "IER"; 180 181 case SHA_IDR: 182 return "IDR"; 183 184 case SHA_IMR: 185 return "IMR"; 186 187 case SHA_ISR: 188 return "ISR"; 189 190 case SHA_MSR: 191 return "MSR"; 192 193 case SHA_BCR: 194 return "BCR"; 195 196 case SHA_REG_DIN(0): 197 case SHA_REG_DIN(1): 198 case SHA_REG_DIN(2): 199 case SHA_REG_DIN(3): 200 case SHA_REG_DIN(4): 201 case SHA_REG_DIN(5): 202 case SHA_REG_DIN(6): 203 case SHA_REG_DIN(7): 204 case SHA_REG_DIN(8): 205 case SHA_REG_DIN(9): 206 case SHA_REG_DIN(10): 207 case SHA_REG_DIN(11): 208 case SHA_REG_DIN(12): 209 case SHA_REG_DIN(13): 210 case SHA_REG_DIN(14): 211 case SHA_REG_DIN(15): 212 snprintf(tmp, sz, "IDATAR[%u]", (offset - SHA_REG_DIN(0)) >> 2); 213 break; 214 215 case SHA_REG_DIGEST(0): 216 case SHA_REG_DIGEST(1): 217 case SHA_REG_DIGEST(2): 218 case SHA_REG_DIGEST(3): 219 case SHA_REG_DIGEST(4): 220 case SHA_REG_DIGEST(5): 221 case SHA_REG_DIGEST(6): 222 case SHA_REG_DIGEST(7): 223 case SHA_REG_DIGEST(8): 224 case SHA_REG_DIGEST(9): 225 case SHA_REG_DIGEST(10): 226 case SHA_REG_DIGEST(11): 227 case SHA_REG_DIGEST(12): 228 case SHA_REG_DIGEST(13): 229 case SHA_REG_DIGEST(14): 230 case SHA_REG_DIGEST(15): 231 if (wr) 232 snprintf(tmp, sz, "IDATAR[%u]", 233 16u + ((offset - SHA_REG_DIGEST(0)) >> 2)); 234 else 235 snprintf(tmp, sz, "ODATAR[%u]", 236 (offset - SHA_REG_DIGEST(0)) >> 2); 237 break; 238 239 case SHA_HW_VERSION: 240 return "HWVER"; 241 242 default: 243 snprintf(tmp, sz, "0x%02x", offset); 244 break; 245 } 246 247 return tmp; 248 } 249 250 #endif /* VERBOSE_DEBUG */ 251 252 static inline u32 atmel_sha_read(struct atmel_sha_dev *dd, u32 offset) 253 { 254 u32 value = readl_relaxed(dd->io_base + offset); 255 256 #ifdef VERBOSE_DEBUG 257 if (dd->flags & SHA_FLAGS_DUMP_REG) { 258 char tmp[16]; 259 260 dev_vdbg(dd->dev, "read 0x%08x from %s\n", value, 261 atmel_sha_reg_name(offset, tmp, sizeof(tmp), false)); 262 } 263 #endif /* VERBOSE_DEBUG */ 264 265 return value; 266 } 267 268 static inline void atmel_sha_write(struct atmel_sha_dev *dd, 269 u32 offset, u32 value) 270 { 271 #ifdef VERBOSE_DEBUG 272 if (dd->flags & SHA_FLAGS_DUMP_REG) { 273 char tmp[16]; 274 275 dev_vdbg(dd->dev, "write 0x%08x into %s\n", value, 276 atmel_sha_reg_name(offset, tmp, sizeof(tmp), true)); 277 } 278 #endif /* VERBOSE_DEBUG */ 279 280 writel_relaxed(value, dd->io_base + offset); 281 } 282 283 static inline int atmel_sha_complete(struct atmel_sha_dev *dd, int err) 284 { 285 struct ahash_request *req = dd->req; 286 287 dd->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL | SHA_FLAGS_CPU | 288 SHA_FLAGS_DMA_READY | SHA_FLAGS_OUTPUT_READY | 289 SHA_FLAGS_DUMP_REG); 290 291 clk_disable(dd->iclk); 292 293 if ((dd->is_async || dd->force_complete) && req->base.complete) 294 ahash_request_complete(req, err); 295 296 /* handle new request */ 297 tasklet_schedule(&dd->queue_task); 298 299 return err; 300 } 301 302 static size_t atmel_sha_append_sg(struct atmel_sha_reqctx *ctx) 303 { 304 size_t count; 305 306 while ((ctx->bufcnt < ctx->buflen) && ctx->total) { 307 count = min3(ctx->sg->length - ctx->offset, ctx->total, 308 ctx->buflen - ctx->bufcnt); 309 310 if (count == 0) { 311 /* 312 * Check if count == 0 because the buffer is full or 313 * because the sg length is 0. In the latest case, 314 * check if there is another sg in the list, a 0 length 315 * sg doesn't necessarily mean the end of the sg list. 316 */ 317 if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) { 318 ctx->sg = sg_next(ctx->sg); 319 continue; 320 } else { 321 break; 322 } 323 } 324 325 scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg, 326 ctx->offset, count, 0); 327 328 ctx->bufcnt += count; 329 ctx->offset += count; 330 ctx->total -= count; 331 332 if (ctx->offset == ctx->sg->length) { 333 ctx->sg = sg_next(ctx->sg); 334 if (ctx->sg) 335 ctx->offset = 0; 336 else 337 ctx->total = 0; 338 } 339 } 340 341 return 0; 342 } 343 344 /* 345 * The purpose of this padding is to ensure that the padded message is a 346 * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512). 347 * The bit "1" is appended at the end of the message followed by 348 * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or 349 * 128 bits block (SHA384/SHA512) equals to the message length in bits 350 * is appended. 351 * 352 * For SHA1/SHA224/SHA256, padlen is calculated as followed: 353 * - if message length < 56 bytes then padlen = 56 - message length 354 * - else padlen = 64 + 56 - message length 355 * 356 * For SHA384/SHA512, padlen is calculated as followed: 357 * - if message length < 112 bytes then padlen = 112 - message length 358 * - else padlen = 128 + 112 - message length 359 */ 360 static void atmel_sha_fill_padding(struct atmel_sha_reqctx *ctx, int length) 361 { 362 unsigned int index, padlen; 363 __be64 bits[2]; 364 u64 size[2]; 365 366 size[0] = ctx->digcnt[0]; 367 size[1] = ctx->digcnt[1]; 368 369 size[0] += ctx->bufcnt; 370 if (size[0] < ctx->bufcnt) 371 size[1]++; 372 373 size[0] += length; 374 if (size[0] < length) 375 size[1]++; 376 377 bits[1] = cpu_to_be64(size[0] << 3); 378 bits[0] = cpu_to_be64(size[1] << 3 | size[0] >> 61); 379 380 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) { 381 case SHA_FLAGS_SHA384: 382 case SHA_FLAGS_SHA512: 383 index = ctx->bufcnt & 0x7f; 384 padlen = (index < 112) ? (112 - index) : ((128+112) - index); 385 *(ctx->buffer + ctx->bufcnt) = 0x80; 386 memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1); 387 memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16); 388 ctx->bufcnt += padlen + 16; 389 ctx->flags |= SHA_FLAGS_PAD; 390 break; 391 392 default: 393 index = ctx->bufcnt & 0x3f; 394 padlen = (index < 56) ? (56 - index) : ((64+56) - index); 395 *(ctx->buffer + ctx->bufcnt) = 0x80; 396 memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1); 397 memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8); 398 ctx->bufcnt += padlen + 8; 399 ctx->flags |= SHA_FLAGS_PAD; 400 break; 401 } 402 } 403 404 static struct atmel_sha_dev *atmel_sha_find_dev(struct atmel_sha_ctx *tctx) 405 { 406 struct atmel_sha_dev *dd; 407 408 spin_lock_bh(&atmel_sha.lock); 409 if (!tctx->dd) 410 tctx->dd = list_first_entry_or_null(&atmel_sha.dev_list, 411 struct atmel_sha_dev, list); 412 dd = tctx->dd; 413 spin_unlock_bh(&atmel_sha.lock); 414 415 return dd; 416 } 417 418 static int atmel_sha_init(struct ahash_request *req) 419 { 420 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 421 struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm); 422 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 423 struct atmel_sha_dev *dd = atmel_sha_find_dev(tctx); 424 425 if (!dd) 426 return -ENODEV; 427 428 ctx->dd = dd; 429 430 ctx->flags = 0; 431 432 dev_dbg(dd->dev, "init: digest size: %u\n", 433 crypto_ahash_digestsize(tfm)); 434 435 switch (crypto_ahash_digestsize(tfm)) { 436 case SHA1_DIGEST_SIZE: 437 ctx->flags |= SHA_FLAGS_SHA1; 438 ctx->block_size = SHA1_BLOCK_SIZE; 439 break; 440 case SHA224_DIGEST_SIZE: 441 ctx->flags |= SHA_FLAGS_SHA224; 442 ctx->block_size = SHA224_BLOCK_SIZE; 443 break; 444 case SHA256_DIGEST_SIZE: 445 ctx->flags |= SHA_FLAGS_SHA256; 446 ctx->block_size = SHA256_BLOCK_SIZE; 447 break; 448 case SHA384_DIGEST_SIZE: 449 ctx->flags |= SHA_FLAGS_SHA384; 450 ctx->block_size = SHA384_BLOCK_SIZE; 451 break; 452 case SHA512_DIGEST_SIZE: 453 ctx->flags |= SHA_FLAGS_SHA512; 454 ctx->block_size = SHA512_BLOCK_SIZE; 455 break; 456 default: 457 return -EINVAL; 458 } 459 460 ctx->bufcnt = 0; 461 ctx->digcnt[0] = 0; 462 ctx->digcnt[1] = 0; 463 ctx->buflen = SHA_BUFFER_LEN; 464 465 return 0; 466 } 467 468 static void atmel_sha_write_ctrl(struct atmel_sha_dev *dd, int dma) 469 { 470 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); 471 u32 valmr = SHA_MR_MODE_AUTO; 472 unsigned int i, hashsize = 0; 473 474 if (likely(dma)) { 475 if (!dd->caps.has_dma) 476 atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE); 477 valmr = SHA_MR_MODE_PDC; 478 if (dd->caps.has_dualbuff) 479 valmr |= SHA_MR_DUALBUFF; 480 } else { 481 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY); 482 } 483 484 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) { 485 case SHA_FLAGS_SHA1: 486 valmr |= SHA_MR_ALGO_SHA1; 487 hashsize = SHA1_DIGEST_SIZE; 488 break; 489 490 case SHA_FLAGS_SHA224: 491 valmr |= SHA_MR_ALGO_SHA224; 492 hashsize = SHA256_DIGEST_SIZE; 493 break; 494 495 case SHA_FLAGS_SHA256: 496 valmr |= SHA_MR_ALGO_SHA256; 497 hashsize = SHA256_DIGEST_SIZE; 498 break; 499 500 case SHA_FLAGS_SHA384: 501 valmr |= SHA_MR_ALGO_SHA384; 502 hashsize = SHA512_DIGEST_SIZE; 503 break; 504 505 case SHA_FLAGS_SHA512: 506 valmr |= SHA_MR_ALGO_SHA512; 507 hashsize = SHA512_DIGEST_SIZE; 508 break; 509 510 default: 511 break; 512 } 513 514 /* Setting CR_FIRST only for the first iteration */ 515 if (!(ctx->digcnt[0] || ctx->digcnt[1])) { 516 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST); 517 } else if (dd->caps.has_uihv && (ctx->flags & SHA_FLAGS_RESTORE)) { 518 const u32 *hash = (const u32 *)ctx->digest; 519 520 /* 521 * Restore the hardware context: update the User Initialize 522 * Hash Value (UIHV) with the value saved when the latest 523 * 'update' operation completed on this very same crypto 524 * request. 525 */ 526 ctx->flags &= ~SHA_FLAGS_RESTORE; 527 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV); 528 for (i = 0; i < hashsize / sizeof(u32); ++i) 529 atmel_sha_write(dd, SHA_REG_DIN(i), hash[i]); 530 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST); 531 valmr |= SHA_MR_UIHV; 532 } 533 /* 534 * WARNING: If the UIHV feature is not available, the hardware CANNOT 535 * process concurrent requests: the internal registers used to store 536 * the hash/digest are still set to the partial digest output values 537 * computed during the latest round. 538 */ 539 540 atmel_sha_write(dd, SHA_MR, valmr); 541 } 542 543 static inline int atmel_sha_wait_for_data_ready(struct atmel_sha_dev *dd, 544 atmel_sha_fn_t resume) 545 { 546 u32 isr = atmel_sha_read(dd, SHA_ISR); 547 548 if (unlikely(isr & SHA_INT_DATARDY)) 549 return resume(dd); 550 551 dd->resume = resume; 552 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY); 553 return -EINPROGRESS; 554 } 555 556 static int atmel_sha_xmit_cpu(struct atmel_sha_dev *dd, const u8 *buf, 557 size_t length, int final) 558 { 559 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); 560 int count, len32; 561 const u32 *buffer = (const u32 *)buf; 562 563 dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n", 564 ctx->digcnt[1], ctx->digcnt[0], length, final); 565 566 atmel_sha_write_ctrl(dd, 0); 567 568 /* should be non-zero before next lines to disable clocks later */ 569 ctx->digcnt[0] += length; 570 if (ctx->digcnt[0] < length) 571 ctx->digcnt[1]++; 572 573 if (final) 574 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */ 575 576 len32 = DIV_ROUND_UP(length, sizeof(u32)); 577 578 dd->flags |= SHA_FLAGS_CPU; 579 580 for (count = 0; count < len32; count++) 581 atmel_sha_write(dd, SHA_REG_DIN(count), buffer[count]); 582 583 return -EINPROGRESS; 584 } 585 586 static int atmel_sha_xmit_pdc(struct atmel_sha_dev *dd, dma_addr_t dma_addr1, 587 size_t length1, dma_addr_t dma_addr2, size_t length2, int final) 588 { 589 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); 590 int len32; 591 592 dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n", 593 ctx->digcnt[1], ctx->digcnt[0], length1, final); 594 595 len32 = DIV_ROUND_UP(length1, sizeof(u32)); 596 atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS); 597 atmel_sha_write(dd, SHA_TPR, dma_addr1); 598 atmel_sha_write(dd, SHA_TCR, len32); 599 600 len32 = DIV_ROUND_UP(length2, sizeof(u32)); 601 atmel_sha_write(dd, SHA_TNPR, dma_addr2); 602 atmel_sha_write(dd, SHA_TNCR, len32); 603 604 atmel_sha_write_ctrl(dd, 1); 605 606 /* should be non-zero before next lines to disable clocks later */ 607 ctx->digcnt[0] += length1; 608 if (ctx->digcnt[0] < length1) 609 ctx->digcnt[1]++; 610 611 if (final) 612 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */ 613 614 dd->flags |= SHA_FLAGS_DMA_ACTIVE; 615 616 /* Start DMA transfer */ 617 atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTEN); 618 619 return -EINPROGRESS; 620 } 621 622 static void atmel_sha_dma_callback(void *data) 623 { 624 struct atmel_sha_dev *dd = data; 625 626 dd->is_async = true; 627 628 /* dma_lch_in - completed - wait DATRDY */ 629 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY); 630 } 631 632 static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1, 633 size_t length1, dma_addr_t dma_addr2, size_t length2, int final) 634 { 635 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); 636 struct dma_async_tx_descriptor *in_desc; 637 struct scatterlist sg[2]; 638 639 dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n", 640 ctx->digcnt[1], ctx->digcnt[0], length1, final); 641 642 dd->dma_lch_in.dma_conf.src_maxburst = 16; 643 dd->dma_lch_in.dma_conf.dst_maxburst = 16; 644 645 dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf); 646 647 if (length2) { 648 sg_init_table(sg, 2); 649 sg_dma_address(&sg[0]) = dma_addr1; 650 sg_dma_len(&sg[0]) = length1; 651 sg_dma_address(&sg[1]) = dma_addr2; 652 sg_dma_len(&sg[1]) = length2; 653 in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2, 654 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 655 } else { 656 sg_init_table(sg, 1); 657 sg_dma_address(&sg[0]) = dma_addr1; 658 sg_dma_len(&sg[0]) = length1; 659 in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1, 660 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 661 } 662 if (!in_desc) 663 return atmel_sha_complete(dd, -EINVAL); 664 665 in_desc->callback = atmel_sha_dma_callback; 666 in_desc->callback_param = dd; 667 668 atmel_sha_write_ctrl(dd, 1); 669 670 /* should be non-zero before next lines to disable clocks later */ 671 ctx->digcnt[0] += length1; 672 if (ctx->digcnt[0] < length1) 673 ctx->digcnt[1]++; 674 675 if (final) 676 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */ 677 678 dd->flags |= SHA_FLAGS_DMA_ACTIVE; 679 680 /* Start DMA transfer */ 681 dmaengine_submit(in_desc); 682 dma_async_issue_pending(dd->dma_lch_in.chan); 683 684 return -EINPROGRESS; 685 } 686 687 static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1, 688 size_t length1, dma_addr_t dma_addr2, size_t length2, int final) 689 { 690 if (dd->caps.has_dma) 691 return atmel_sha_xmit_dma(dd, dma_addr1, length1, 692 dma_addr2, length2, final); 693 else 694 return atmel_sha_xmit_pdc(dd, dma_addr1, length1, 695 dma_addr2, length2, final); 696 } 697 698 static int atmel_sha_update_cpu(struct atmel_sha_dev *dd) 699 { 700 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); 701 int bufcnt; 702 703 atmel_sha_append_sg(ctx); 704 atmel_sha_fill_padding(ctx, 0); 705 bufcnt = ctx->bufcnt; 706 ctx->bufcnt = 0; 707 708 return atmel_sha_xmit_cpu(dd, ctx->buffer, bufcnt, 1); 709 } 710 711 static int atmel_sha_xmit_dma_map(struct atmel_sha_dev *dd, 712 struct atmel_sha_reqctx *ctx, 713 size_t length, int final) 714 { 715 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer, 716 ctx->buflen + ctx->block_size, DMA_TO_DEVICE); 717 if (dma_mapping_error(dd->dev, ctx->dma_addr)) { 718 dev_err(dd->dev, "dma %zu bytes error\n", ctx->buflen + 719 ctx->block_size); 720 return atmel_sha_complete(dd, -EINVAL); 721 } 722 723 ctx->flags &= ~SHA_FLAGS_SG; 724 725 /* next call does not fail... so no unmap in the case of error */ 726 return atmel_sha_xmit_start(dd, ctx->dma_addr, length, 0, 0, final); 727 } 728 729 static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd) 730 { 731 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); 732 unsigned int final; 733 size_t count; 734 735 atmel_sha_append_sg(ctx); 736 737 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total; 738 739 dev_dbg(dd->dev, "slow: bufcnt: %zu, digcnt: 0x%llx 0x%llx, final: %d\n", 740 ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final); 741 742 if (final) 743 atmel_sha_fill_padding(ctx, 0); 744 745 if (final || (ctx->bufcnt == ctx->buflen)) { 746 count = ctx->bufcnt; 747 ctx->bufcnt = 0; 748 return atmel_sha_xmit_dma_map(dd, ctx, count, final); 749 } 750 751 return 0; 752 } 753 754 static int atmel_sha_update_dma_start(struct atmel_sha_dev *dd) 755 { 756 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); 757 unsigned int length, final, tail; 758 struct scatterlist *sg; 759 unsigned int count; 760 761 if (!ctx->total) 762 return 0; 763 764 if (ctx->bufcnt || ctx->offset) 765 return atmel_sha_update_dma_slow(dd); 766 767 dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %zd, total: %u\n", 768 ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total); 769 770 sg = ctx->sg; 771 772 if (!IS_ALIGNED(sg->offset, sizeof(u32))) 773 return atmel_sha_update_dma_slow(dd); 774 775 if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size)) 776 /* size is not ctx->block_size aligned */ 777 return atmel_sha_update_dma_slow(dd); 778 779 length = min(ctx->total, sg->length); 780 781 if (sg_is_last(sg)) { 782 if (!(ctx->flags & SHA_FLAGS_FINUP)) { 783 /* not last sg must be ctx->block_size aligned */ 784 tail = length & (ctx->block_size - 1); 785 length -= tail; 786 } 787 } 788 789 ctx->total -= length; 790 ctx->offset = length; /* offset where to start slow */ 791 792 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total; 793 794 /* Add padding */ 795 if (final) { 796 tail = length & (ctx->block_size - 1); 797 length -= tail; 798 ctx->total += tail; 799 ctx->offset = length; /* offset where to start slow */ 800 801 sg = ctx->sg; 802 atmel_sha_append_sg(ctx); 803 804 atmel_sha_fill_padding(ctx, length); 805 806 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer, 807 ctx->buflen + ctx->block_size, DMA_TO_DEVICE); 808 if (dma_mapping_error(dd->dev, ctx->dma_addr)) { 809 dev_err(dd->dev, "dma %zu bytes error\n", 810 ctx->buflen + ctx->block_size); 811 return atmel_sha_complete(dd, -EINVAL); 812 } 813 814 if (length == 0) { 815 ctx->flags &= ~SHA_FLAGS_SG; 816 count = ctx->bufcnt; 817 ctx->bufcnt = 0; 818 return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0, 819 0, final); 820 } else { 821 ctx->sg = sg; 822 if (!dma_map_sg(dd->dev, ctx->sg, 1, 823 DMA_TO_DEVICE)) { 824 dev_err(dd->dev, "dma_map_sg error\n"); 825 return atmel_sha_complete(dd, -EINVAL); 826 } 827 828 ctx->flags |= SHA_FLAGS_SG; 829 830 count = ctx->bufcnt; 831 ctx->bufcnt = 0; 832 return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), 833 length, ctx->dma_addr, count, final); 834 } 835 } 836 837 if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) { 838 dev_err(dd->dev, "dma_map_sg error\n"); 839 return atmel_sha_complete(dd, -EINVAL); 840 } 841 842 ctx->flags |= SHA_FLAGS_SG; 843 844 /* next call does not fail... so no unmap in the case of error */ 845 return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), length, 0, 846 0, final); 847 } 848 849 static void atmel_sha_update_dma_stop(struct atmel_sha_dev *dd) 850 { 851 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); 852 853 if (ctx->flags & SHA_FLAGS_SG) { 854 dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE); 855 if (ctx->sg->length == ctx->offset) { 856 ctx->sg = sg_next(ctx->sg); 857 if (ctx->sg) 858 ctx->offset = 0; 859 } 860 if (ctx->flags & SHA_FLAGS_PAD) { 861 dma_unmap_single(dd->dev, ctx->dma_addr, 862 ctx->buflen + ctx->block_size, DMA_TO_DEVICE); 863 } 864 } else { 865 dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen + 866 ctx->block_size, DMA_TO_DEVICE); 867 } 868 } 869 870 static int atmel_sha_update_req(struct atmel_sha_dev *dd) 871 { 872 struct ahash_request *req = dd->req; 873 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 874 int err; 875 876 dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n", 877 ctx->total, ctx->digcnt[1], ctx->digcnt[0]); 878 879 if (ctx->flags & SHA_FLAGS_CPU) 880 err = atmel_sha_update_cpu(dd); 881 else 882 err = atmel_sha_update_dma_start(dd); 883 884 /* wait for dma completion before can take more data */ 885 dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx\n", 886 err, ctx->digcnt[1], ctx->digcnt[0]); 887 888 return err; 889 } 890 891 static int atmel_sha_final_req(struct atmel_sha_dev *dd) 892 { 893 struct ahash_request *req = dd->req; 894 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 895 int err = 0; 896 int count; 897 898 if (ctx->bufcnt >= ATMEL_SHA_DMA_THRESHOLD) { 899 atmel_sha_fill_padding(ctx, 0); 900 count = ctx->bufcnt; 901 ctx->bufcnt = 0; 902 err = atmel_sha_xmit_dma_map(dd, ctx, count, 1); 903 } 904 /* faster to handle last block with cpu */ 905 else { 906 atmel_sha_fill_padding(ctx, 0); 907 count = ctx->bufcnt; 908 ctx->bufcnt = 0; 909 err = atmel_sha_xmit_cpu(dd, ctx->buffer, count, 1); 910 } 911 912 dev_dbg(dd->dev, "final_req: err: %d\n", err); 913 914 return err; 915 } 916 917 static void atmel_sha_copy_hash(struct ahash_request *req) 918 { 919 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 920 u32 *hash = (u32 *)ctx->digest; 921 unsigned int i, hashsize; 922 923 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) { 924 case SHA_FLAGS_SHA1: 925 hashsize = SHA1_DIGEST_SIZE; 926 break; 927 928 case SHA_FLAGS_SHA224: 929 case SHA_FLAGS_SHA256: 930 hashsize = SHA256_DIGEST_SIZE; 931 break; 932 933 case SHA_FLAGS_SHA384: 934 case SHA_FLAGS_SHA512: 935 hashsize = SHA512_DIGEST_SIZE; 936 break; 937 938 default: 939 /* Should not happen... */ 940 return; 941 } 942 943 for (i = 0; i < hashsize / sizeof(u32); ++i) 944 hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i)); 945 ctx->flags |= SHA_FLAGS_RESTORE; 946 } 947 948 static void atmel_sha_copy_ready_hash(struct ahash_request *req) 949 { 950 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 951 952 if (!req->result) 953 return; 954 955 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) { 956 default: 957 case SHA_FLAGS_SHA1: 958 memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE); 959 break; 960 961 case SHA_FLAGS_SHA224: 962 memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE); 963 break; 964 965 case SHA_FLAGS_SHA256: 966 memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE); 967 break; 968 969 case SHA_FLAGS_SHA384: 970 memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE); 971 break; 972 973 case SHA_FLAGS_SHA512: 974 memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE); 975 break; 976 } 977 } 978 979 static int atmel_sha_finish(struct ahash_request *req) 980 { 981 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 982 struct atmel_sha_dev *dd = ctx->dd; 983 984 if (ctx->digcnt[0] || ctx->digcnt[1]) 985 atmel_sha_copy_ready_hash(req); 986 987 dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %zd\n", ctx->digcnt[1], 988 ctx->digcnt[0], ctx->bufcnt); 989 990 return 0; 991 } 992 993 static void atmel_sha_finish_req(struct ahash_request *req, int err) 994 { 995 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 996 struct atmel_sha_dev *dd = ctx->dd; 997 998 if (!err) { 999 atmel_sha_copy_hash(req); 1000 if (SHA_FLAGS_FINAL & dd->flags) 1001 err = atmel_sha_finish(req); 1002 } else { 1003 ctx->flags |= SHA_FLAGS_ERROR; 1004 } 1005 1006 /* atomic operation is not needed here */ 1007 (void)atmel_sha_complete(dd, err); 1008 } 1009 1010 static int atmel_sha_hw_init(struct atmel_sha_dev *dd) 1011 { 1012 int err; 1013 1014 err = clk_enable(dd->iclk); 1015 if (err) 1016 return err; 1017 1018 if (!(SHA_FLAGS_INIT & dd->flags)) { 1019 atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST); 1020 dd->flags |= SHA_FLAGS_INIT; 1021 } 1022 1023 return 0; 1024 } 1025 1026 static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd) 1027 { 1028 return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff; 1029 } 1030 1031 static int atmel_sha_hw_version_init(struct atmel_sha_dev *dd) 1032 { 1033 int err; 1034 1035 err = atmel_sha_hw_init(dd); 1036 if (err) 1037 return err; 1038 1039 dd->hw_version = atmel_sha_get_version(dd); 1040 1041 dev_info(dd->dev, 1042 "version: 0x%x\n", dd->hw_version); 1043 1044 clk_disable(dd->iclk); 1045 1046 return 0; 1047 } 1048 1049 static int atmel_sha_handle_queue(struct atmel_sha_dev *dd, 1050 struct ahash_request *req) 1051 { 1052 struct crypto_async_request *async_req, *backlog; 1053 struct atmel_sha_ctx *ctx; 1054 unsigned long flags; 1055 bool start_async; 1056 int err = 0, ret = 0; 1057 1058 spin_lock_irqsave(&dd->lock, flags); 1059 if (req) 1060 ret = ahash_enqueue_request(&dd->queue, req); 1061 1062 if (SHA_FLAGS_BUSY & dd->flags) { 1063 spin_unlock_irqrestore(&dd->lock, flags); 1064 return ret; 1065 } 1066 1067 backlog = crypto_get_backlog(&dd->queue); 1068 async_req = crypto_dequeue_request(&dd->queue); 1069 if (async_req) 1070 dd->flags |= SHA_FLAGS_BUSY; 1071 1072 spin_unlock_irqrestore(&dd->lock, flags); 1073 1074 if (!async_req) 1075 return ret; 1076 1077 if (backlog) 1078 crypto_request_complete(backlog, -EINPROGRESS); 1079 1080 ctx = crypto_tfm_ctx(async_req->tfm); 1081 1082 dd->req = ahash_request_cast(async_req); 1083 start_async = (dd->req != req); 1084 dd->is_async = start_async; 1085 dd->force_complete = false; 1086 1087 /* WARNING: ctx->start() MAY change dd->is_async. */ 1088 err = ctx->start(dd); 1089 return (start_async) ? ret : err; 1090 } 1091 1092 static int atmel_sha_done(struct atmel_sha_dev *dd); 1093 1094 static int atmel_sha_start(struct atmel_sha_dev *dd) 1095 { 1096 struct ahash_request *req = dd->req; 1097 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1098 int err; 1099 1100 dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %u\n", 1101 ctx->op, req->nbytes); 1102 1103 err = atmel_sha_hw_init(dd); 1104 if (err) 1105 return atmel_sha_complete(dd, err); 1106 1107 /* 1108 * atmel_sha_update_req() and atmel_sha_final_req() can return either: 1109 * -EINPROGRESS: the hardware is busy and the SHA driver will resume 1110 * its job later in the done_task. 1111 * This is the main path. 1112 * 1113 * 0: the SHA driver can continue its job then release the hardware 1114 * later, if needed, with atmel_sha_finish_req(). 1115 * This is the alternate path. 1116 * 1117 * < 0: an error has occurred so atmel_sha_complete(dd, err) has already 1118 * been called, hence the hardware has been released. 1119 * The SHA driver must stop its job without calling 1120 * atmel_sha_finish_req(), otherwise atmel_sha_complete() would be 1121 * called a second time. 1122 * 1123 * Please note that currently, atmel_sha_final_req() never returns 0. 1124 */ 1125 1126 dd->resume = atmel_sha_done; 1127 if (ctx->op == SHA_OP_UPDATE) { 1128 err = atmel_sha_update_req(dd); 1129 if (!err && (ctx->flags & SHA_FLAGS_FINUP)) 1130 /* no final() after finup() */ 1131 err = atmel_sha_final_req(dd); 1132 } else if (ctx->op == SHA_OP_FINAL) { 1133 err = atmel_sha_final_req(dd); 1134 } 1135 1136 if (!err) 1137 /* done_task will not finish it, so do it here */ 1138 atmel_sha_finish_req(req, err); 1139 1140 dev_dbg(dd->dev, "exit, err: %d\n", err); 1141 1142 return err; 1143 } 1144 1145 static int atmel_sha_enqueue(struct ahash_request *req, unsigned int op) 1146 { 1147 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1148 struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm); 1149 struct atmel_sha_dev *dd = tctx->dd; 1150 1151 ctx->op = op; 1152 1153 return atmel_sha_handle_queue(dd, req); 1154 } 1155 1156 static int atmel_sha_update(struct ahash_request *req) 1157 { 1158 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1159 1160 if (!req->nbytes) 1161 return 0; 1162 1163 ctx->total = req->nbytes; 1164 ctx->sg = req->src; 1165 ctx->offset = 0; 1166 1167 if (ctx->flags & SHA_FLAGS_FINUP) { 1168 if (ctx->bufcnt + ctx->total < ATMEL_SHA_DMA_THRESHOLD) 1169 /* faster to use CPU for short transfers */ 1170 ctx->flags |= SHA_FLAGS_CPU; 1171 } else if (ctx->bufcnt + ctx->total < ctx->buflen) { 1172 atmel_sha_append_sg(ctx); 1173 return 0; 1174 } 1175 return atmel_sha_enqueue(req, SHA_OP_UPDATE); 1176 } 1177 1178 static int atmel_sha_final(struct ahash_request *req) 1179 { 1180 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1181 1182 ctx->flags |= SHA_FLAGS_FINUP; 1183 1184 if (ctx->flags & SHA_FLAGS_ERROR) 1185 return 0; /* uncompleted hash is not needed */ 1186 1187 if (ctx->flags & SHA_FLAGS_PAD) 1188 /* copy ready hash (+ finalize hmac) */ 1189 return atmel_sha_finish(req); 1190 1191 return atmel_sha_enqueue(req, SHA_OP_FINAL); 1192 } 1193 1194 static int atmel_sha_finup(struct ahash_request *req) 1195 { 1196 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1197 int err1, err2; 1198 1199 ctx->flags |= SHA_FLAGS_FINUP; 1200 1201 err1 = atmel_sha_update(req); 1202 if (err1 == -EINPROGRESS || 1203 (err1 == -EBUSY && (ahash_request_flags(req) & 1204 CRYPTO_TFM_REQ_MAY_BACKLOG))) 1205 return err1; 1206 1207 /* 1208 * final() has to be always called to cleanup resources 1209 * even if update() failed, except EINPROGRESS 1210 */ 1211 err2 = atmel_sha_final(req); 1212 1213 return err1 ?: err2; 1214 } 1215 1216 static int atmel_sha_digest(struct ahash_request *req) 1217 { 1218 return atmel_sha_init(req) ?: atmel_sha_finup(req); 1219 } 1220 1221 1222 static int atmel_sha_export(struct ahash_request *req, void *out) 1223 { 1224 const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1225 1226 memcpy(out, ctx, sizeof(*ctx)); 1227 return 0; 1228 } 1229 1230 static int atmel_sha_import(struct ahash_request *req, const void *in) 1231 { 1232 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1233 1234 memcpy(ctx, in, sizeof(*ctx)); 1235 return 0; 1236 } 1237 1238 static int atmel_sha_cra_init(struct crypto_tfm *tfm) 1239 { 1240 struct atmel_sha_ctx *ctx = crypto_tfm_ctx(tfm); 1241 1242 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), 1243 sizeof(struct atmel_sha_reqctx)); 1244 ctx->start = atmel_sha_start; 1245 1246 return 0; 1247 } 1248 1249 static void atmel_sha_alg_init(struct ahash_alg *alg) 1250 { 1251 alg->halg.base.cra_priority = ATMEL_SHA_PRIORITY; 1252 alg->halg.base.cra_flags = CRYPTO_ALG_ASYNC | 1253 CRYPTO_ALG_KERN_DRIVER_ONLY; 1254 alg->halg.base.cra_ctxsize = sizeof(struct atmel_sha_ctx); 1255 alg->halg.base.cra_module = THIS_MODULE; 1256 alg->halg.base.cra_init = atmel_sha_cra_init; 1257 1258 alg->halg.statesize = sizeof(struct atmel_sha_reqctx); 1259 1260 alg->init = atmel_sha_init; 1261 alg->update = atmel_sha_update; 1262 alg->final = atmel_sha_final; 1263 alg->finup = atmel_sha_finup; 1264 alg->digest = atmel_sha_digest; 1265 alg->export = atmel_sha_export; 1266 alg->import = atmel_sha_import; 1267 } 1268 1269 static struct ahash_alg sha_1_256_algs[] = { 1270 { 1271 .halg.base.cra_name = "sha1", 1272 .halg.base.cra_driver_name = "atmel-sha1", 1273 .halg.base.cra_blocksize = SHA1_BLOCK_SIZE, 1274 1275 .halg.digestsize = SHA1_DIGEST_SIZE, 1276 }, 1277 { 1278 .halg.base.cra_name = "sha256", 1279 .halg.base.cra_driver_name = "atmel-sha256", 1280 .halg.base.cra_blocksize = SHA256_BLOCK_SIZE, 1281 1282 .halg.digestsize = SHA256_DIGEST_SIZE, 1283 }, 1284 }; 1285 1286 static struct ahash_alg sha_224_alg = { 1287 .halg.base.cra_name = "sha224", 1288 .halg.base.cra_driver_name = "atmel-sha224", 1289 .halg.base.cra_blocksize = SHA224_BLOCK_SIZE, 1290 1291 .halg.digestsize = SHA224_DIGEST_SIZE, 1292 }; 1293 1294 static struct ahash_alg sha_384_512_algs[] = { 1295 { 1296 .halg.base.cra_name = "sha384", 1297 .halg.base.cra_driver_name = "atmel-sha384", 1298 .halg.base.cra_blocksize = SHA384_BLOCK_SIZE, 1299 1300 .halg.digestsize = SHA384_DIGEST_SIZE, 1301 }, 1302 { 1303 .halg.base.cra_name = "sha512", 1304 .halg.base.cra_driver_name = "atmel-sha512", 1305 .halg.base.cra_blocksize = SHA512_BLOCK_SIZE, 1306 1307 .halg.digestsize = SHA512_DIGEST_SIZE, 1308 }, 1309 }; 1310 1311 static void atmel_sha_queue_task(unsigned long data) 1312 { 1313 struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data; 1314 1315 atmel_sha_handle_queue(dd, NULL); 1316 } 1317 1318 static int atmel_sha_done(struct atmel_sha_dev *dd) 1319 { 1320 int err = 0; 1321 1322 if (SHA_FLAGS_CPU & dd->flags) { 1323 if (SHA_FLAGS_OUTPUT_READY & dd->flags) { 1324 dd->flags &= ~SHA_FLAGS_OUTPUT_READY; 1325 goto finish; 1326 } 1327 } else if (SHA_FLAGS_DMA_READY & dd->flags) { 1328 if (SHA_FLAGS_DMA_ACTIVE & dd->flags) { 1329 dd->flags &= ~SHA_FLAGS_DMA_ACTIVE; 1330 atmel_sha_update_dma_stop(dd); 1331 } 1332 if (SHA_FLAGS_OUTPUT_READY & dd->flags) { 1333 /* hash or semi-hash ready */ 1334 dd->flags &= ~(SHA_FLAGS_DMA_READY | 1335 SHA_FLAGS_OUTPUT_READY); 1336 err = atmel_sha_update_dma_start(dd); 1337 if (err != -EINPROGRESS) 1338 goto finish; 1339 } 1340 } 1341 return err; 1342 1343 finish: 1344 /* finish current request */ 1345 atmel_sha_finish_req(dd->req, err); 1346 1347 return err; 1348 } 1349 1350 static void atmel_sha_done_task(unsigned long data) 1351 { 1352 struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data; 1353 1354 dd->is_async = true; 1355 (void)dd->resume(dd); 1356 } 1357 1358 static irqreturn_t atmel_sha_irq(int irq, void *dev_id) 1359 { 1360 struct atmel_sha_dev *sha_dd = dev_id; 1361 u32 reg; 1362 1363 reg = atmel_sha_read(sha_dd, SHA_ISR); 1364 if (reg & atmel_sha_read(sha_dd, SHA_IMR)) { 1365 atmel_sha_write(sha_dd, SHA_IDR, reg); 1366 if (SHA_FLAGS_BUSY & sha_dd->flags) { 1367 sha_dd->flags |= SHA_FLAGS_OUTPUT_READY; 1368 if (!(SHA_FLAGS_CPU & sha_dd->flags)) 1369 sha_dd->flags |= SHA_FLAGS_DMA_READY; 1370 tasklet_schedule(&sha_dd->done_task); 1371 } else { 1372 dev_warn(sha_dd->dev, "SHA interrupt when no active requests.\n"); 1373 } 1374 return IRQ_HANDLED; 1375 } 1376 1377 return IRQ_NONE; 1378 } 1379 1380 1381 /* DMA transfer functions */ 1382 1383 static bool atmel_sha_dma_check_aligned(struct atmel_sha_dev *dd, 1384 struct scatterlist *sg, 1385 size_t len) 1386 { 1387 struct atmel_sha_dma *dma = &dd->dma_lch_in; 1388 struct ahash_request *req = dd->req; 1389 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1390 size_t bs = ctx->block_size; 1391 int nents; 1392 1393 for (nents = 0; sg; sg = sg_next(sg), ++nents) { 1394 if (!IS_ALIGNED(sg->offset, sizeof(u32))) 1395 return false; 1396 1397 /* 1398 * This is the last sg, the only one that is allowed to 1399 * have an unaligned length. 1400 */ 1401 if (len <= sg->length) { 1402 dma->nents = nents + 1; 1403 dma->last_sg_length = sg->length; 1404 sg->length = ALIGN(len, sizeof(u32)); 1405 return true; 1406 } 1407 1408 /* All other sg lengths MUST be aligned to the block size. */ 1409 if (!IS_ALIGNED(sg->length, bs)) 1410 return false; 1411 1412 len -= sg->length; 1413 } 1414 1415 return false; 1416 } 1417 1418 static void atmel_sha_dma_callback2(void *data) 1419 { 1420 struct atmel_sha_dev *dd = data; 1421 struct atmel_sha_dma *dma = &dd->dma_lch_in; 1422 struct scatterlist *sg; 1423 int nents; 1424 1425 dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE); 1426 1427 sg = dma->sg; 1428 for (nents = 0; nents < dma->nents - 1; ++nents) 1429 sg = sg_next(sg); 1430 sg->length = dma->last_sg_length; 1431 1432 dd->is_async = true; 1433 (void)atmel_sha_wait_for_data_ready(dd, dd->resume); 1434 } 1435 1436 static int atmel_sha_dma_start(struct atmel_sha_dev *dd, 1437 struct scatterlist *src, 1438 size_t len, 1439 atmel_sha_fn_t resume) 1440 { 1441 struct atmel_sha_dma *dma = &dd->dma_lch_in; 1442 struct dma_slave_config *config = &dma->dma_conf; 1443 struct dma_chan *chan = dma->chan; 1444 struct dma_async_tx_descriptor *desc; 1445 dma_cookie_t cookie; 1446 unsigned int sg_len; 1447 int err; 1448 1449 dd->resume = resume; 1450 1451 /* 1452 * dma->nents has already been initialized by 1453 * atmel_sha_dma_check_aligned(). 1454 */ 1455 dma->sg = src; 1456 sg_len = dma_map_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE); 1457 if (!sg_len) { 1458 err = -ENOMEM; 1459 goto exit; 1460 } 1461 1462 config->src_maxburst = 16; 1463 config->dst_maxburst = 16; 1464 err = dmaengine_slave_config(chan, config); 1465 if (err) 1466 goto unmap_sg; 1467 1468 desc = dmaengine_prep_slave_sg(chan, dma->sg, sg_len, DMA_MEM_TO_DEV, 1469 DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 1470 if (!desc) { 1471 err = -ENOMEM; 1472 goto unmap_sg; 1473 } 1474 1475 desc->callback = atmel_sha_dma_callback2; 1476 desc->callback_param = dd; 1477 cookie = dmaengine_submit(desc); 1478 err = dma_submit_error(cookie); 1479 if (err) 1480 goto unmap_sg; 1481 1482 dma_async_issue_pending(chan); 1483 1484 return -EINPROGRESS; 1485 1486 unmap_sg: 1487 dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE); 1488 exit: 1489 return atmel_sha_complete(dd, err); 1490 } 1491 1492 1493 /* CPU transfer functions */ 1494 1495 static int atmel_sha_cpu_transfer(struct atmel_sha_dev *dd) 1496 { 1497 struct ahash_request *req = dd->req; 1498 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1499 const u32 *words = (const u32 *)ctx->buffer; 1500 size_t i, num_words; 1501 u32 isr, din, din_inc; 1502 1503 din_inc = (ctx->flags & SHA_FLAGS_IDATAR0) ? 0 : 1; 1504 for (;;) { 1505 /* Write data into the Input Data Registers. */ 1506 num_words = DIV_ROUND_UP(ctx->bufcnt, sizeof(u32)); 1507 for (i = 0, din = 0; i < num_words; ++i, din += din_inc) 1508 atmel_sha_write(dd, SHA_REG_DIN(din), words[i]); 1509 1510 ctx->offset += ctx->bufcnt; 1511 ctx->total -= ctx->bufcnt; 1512 1513 if (!ctx->total) 1514 break; 1515 1516 /* 1517 * Prepare next block: 1518 * Fill ctx->buffer now with the next data to be written into 1519 * IDATARx: it gives time for the SHA hardware to process 1520 * the current data so the SHA_INT_DATARDY flag might be set 1521 * in SHA_ISR when polling this register at the beginning of 1522 * the next loop. 1523 */ 1524 ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total); 1525 scatterwalk_map_and_copy(ctx->buffer, ctx->sg, 1526 ctx->offset, ctx->bufcnt, 0); 1527 1528 /* Wait for hardware to be ready again. */ 1529 isr = atmel_sha_read(dd, SHA_ISR); 1530 if (!(isr & SHA_INT_DATARDY)) { 1531 /* Not ready yet. */ 1532 dd->resume = atmel_sha_cpu_transfer; 1533 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY); 1534 return -EINPROGRESS; 1535 } 1536 } 1537 1538 if (unlikely(!(ctx->flags & SHA_FLAGS_WAIT_DATARDY))) 1539 return dd->cpu_transfer_complete(dd); 1540 1541 return atmel_sha_wait_for_data_ready(dd, dd->cpu_transfer_complete); 1542 } 1543 1544 static int atmel_sha_cpu_start(struct atmel_sha_dev *dd, 1545 struct scatterlist *sg, 1546 unsigned int len, 1547 bool idatar0_only, 1548 bool wait_data_ready, 1549 atmel_sha_fn_t resume) 1550 { 1551 struct ahash_request *req = dd->req; 1552 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1553 1554 if (!len) 1555 return resume(dd); 1556 1557 ctx->flags &= ~(SHA_FLAGS_IDATAR0 | SHA_FLAGS_WAIT_DATARDY); 1558 1559 if (idatar0_only) 1560 ctx->flags |= SHA_FLAGS_IDATAR0; 1561 1562 if (wait_data_ready) 1563 ctx->flags |= SHA_FLAGS_WAIT_DATARDY; 1564 1565 ctx->sg = sg; 1566 ctx->total = len; 1567 ctx->offset = 0; 1568 1569 /* Prepare the first block to be written. */ 1570 ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total); 1571 scatterwalk_map_and_copy(ctx->buffer, ctx->sg, 1572 ctx->offset, ctx->bufcnt, 0); 1573 1574 dd->cpu_transfer_complete = resume; 1575 return atmel_sha_cpu_transfer(dd); 1576 } 1577 1578 static int atmel_sha_cpu_hash(struct atmel_sha_dev *dd, 1579 const void *data, unsigned int datalen, 1580 bool auto_padding, 1581 atmel_sha_fn_t resume) 1582 { 1583 struct ahash_request *req = dd->req; 1584 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1585 u32 msglen = (auto_padding) ? datalen : 0; 1586 u32 mr = SHA_MR_MODE_AUTO; 1587 1588 if (!(IS_ALIGNED(datalen, ctx->block_size) || auto_padding)) 1589 return atmel_sha_complete(dd, -EINVAL); 1590 1591 mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK); 1592 atmel_sha_write(dd, SHA_MR, mr); 1593 atmel_sha_write(dd, SHA_MSR, msglen); 1594 atmel_sha_write(dd, SHA_BCR, msglen); 1595 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST); 1596 1597 sg_init_one(&dd->tmp, data, datalen); 1598 return atmel_sha_cpu_start(dd, &dd->tmp, datalen, false, true, resume); 1599 } 1600 1601 1602 /* hmac functions */ 1603 1604 struct atmel_sha_hmac_key { 1605 bool valid; 1606 unsigned int keylen; 1607 u8 buffer[SHA512_BLOCK_SIZE]; 1608 u8 *keydup; 1609 }; 1610 1611 static inline void atmel_sha_hmac_key_init(struct atmel_sha_hmac_key *hkey) 1612 { 1613 memset(hkey, 0, sizeof(*hkey)); 1614 } 1615 1616 static inline void atmel_sha_hmac_key_release(struct atmel_sha_hmac_key *hkey) 1617 { 1618 kfree(hkey->keydup); 1619 memset(hkey, 0, sizeof(*hkey)); 1620 } 1621 1622 static inline int atmel_sha_hmac_key_set(struct atmel_sha_hmac_key *hkey, 1623 const u8 *key, 1624 unsigned int keylen) 1625 { 1626 atmel_sha_hmac_key_release(hkey); 1627 1628 if (keylen > sizeof(hkey->buffer)) { 1629 hkey->keydup = kmemdup(key, keylen, GFP_KERNEL); 1630 if (!hkey->keydup) 1631 return -ENOMEM; 1632 1633 } else { 1634 memcpy(hkey->buffer, key, keylen); 1635 } 1636 1637 hkey->valid = true; 1638 hkey->keylen = keylen; 1639 return 0; 1640 } 1641 1642 static inline bool atmel_sha_hmac_key_get(const struct atmel_sha_hmac_key *hkey, 1643 const u8 **key, 1644 unsigned int *keylen) 1645 { 1646 if (!hkey->valid) 1647 return false; 1648 1649 *keylen = hkey->keylen; 1650 *key = (hkey->keydup) ? hkey->keydup : hkey->buffer; 1651 return true; 1652 } 1653 1654 1655 struct atmel_sha_hmac_ctx { 1656 struct atmel_sha_ctx base; 1657 1658 struct atmel_sha_hmac_key hkey; 1659 u32 ipad[SHA512_BLOCK_SIZE / sizeof(u32)]; 1660 u32 opad[SHA512_BLOCK_SIZE / sizeof(u32)]; 1661 atmel_sha_fn_t resume; 1662 }; 1663 1664 static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd, 1665 atmel_sha_fn_t resume); 1666 static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd, 1667 const u8 *key, unsigned int keylen); 1668 static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd); 1669 static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd); 1670 static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd); 1671 static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd); 1672 1673 static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd); 1674 static int atmel_sha_hmac_final(struct atmel_sha_dev *dd); 1675 static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd); 1676 static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd); 1677 1678 static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd, 1679 atmel_sha_fn_t resume) 1680 { 1681 struct ahash_request *req = dd->req; 1682 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1683 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 1684 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); 1685 unsigned int keylen; 1686 const u8 *key; 1687 size_t bs; 1688 1689 hmac->resume = resume; 1690 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) { 1691 case SHA_FLAGS_SHA1: 1692 ctx->block_size = SHA1_BLOCK_SIZE; 1693 ctx->hash_size = SHA1_DIGEST_SIZE; 1694 break; 1695 1696 case SHA_FLAGS_SHA224: 1697 ctx->block_size = SHA224_BLOCK_SIZE; 1698 ctx->hash_size = SHA256_DIGEST_SIZE; 1699 break; 1700 1701 case SHA_FLAGS_SHA256: 1702 ctx->block_size = SHA256_BLOCK_SIZE; 1703 ctx->hash_size = SHA256_DIGEST_SIZE; 1704 break; 1705 1706 case SHA_FLAGS_SHA384: 1707 ctx->block_size = SHA384_BLOCK_SIZE; 1708 ctx->hash_size = SHA512_DIGEST_SIZE; 1709 break; 1710 1711 case SHA_FLAGS_SHA512: 1712 ctx->block_size = SHA512_BLOCK_SIZE; 1713 ctx->hash_size = SHA512_DIGEST_SIZE; 1714 break; 1715 1716 default: 1717 return atmel_sha_complete(dd, -EINVAL); 1718 } 1719 bs = ctx->block_size; 1720 1721 if (likely(!atmel_sha_hmac_key_get(&hmac->hkey, &key, &keylen))) 1722 return resume(dd); 1723 1724 /* Compute K' from K. */ 1725 if (unlikely(keylen > bs)) 1726 return atmel_sha_hmac_prehash_key(dd, key, keylen); 1727 1728 /* Prepare ipad. */ 1729 memcpy_and_pad(hmac->ipad, bs, key, keylen, 0); 1730 return atmel_sha_hmac_compute_ipad_hash(dd); 1731 } 1732 1733 static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd, 1734 const u8 *key, unsigned int keylen) 1735 { 1736 return atmel_sha_cpu_hash(dd, key, keylen, true, 1737 atmel_sha_hmac_prehash_key_done); 1738 } 1739 1740 static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd) 1741 { 1742 struct ahash_request *req = dd->req; 1743 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 1744 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); 1745 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1746 size_t ds = crypto_ahash_digestsize(tfm); 1747 size_t bs = ctx->block_size; 1748 size_t i, num_words = ds / sizeof(u32); 1749 1750 /* Prepare ipad. */ 1751 for (i = 0; i < num_words; ++i) 1752 hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i)); 1753 memset((u8 *)hmac->ipad + ds, 0, bs - ds); 1754 return atmel_sha_hmac_compute_ipad_hash(dd); 1755 } 1756 1757 static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd) 1758 { 1759 struct ahash_request *req = dd->req; 1760 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 1761 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); 1762 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1763 size_t bs = ctx->block_size; 1764 size_t i, num_words = bs / sizeof(u32); 1765 1766 unsafe_memcpy(hmac->opad, hmac->ipad, bs, 1767 "fortified memcpy causes -Wrestrict warning"); 1768 for (i = 0; i < num_words; ++i) { 1769 hmac->ipad[i] ^= 0x36363636; 1770 hmac->opad[i] ^= 0x5c5c5c5c; 1771 } 1772 1773 return atmel_sha_cpu_hash(dd, hmac->ipad, bs, false, 1774 atmel_sha_hmac_compute_opad_hash); 1775 } 1776 1777 static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd) 1778 { 1779 struct ahash_request *req = dd->req; 1780 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 1781 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); 1782 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1783 size_t bs = ctx->block_size; 1784 size_t hs = ctx->hash_size; 1785 size_t i, num_words = hs / sizeof(u32); 1786 1787 for (i = 0; i < num_words; ++i) 1788 hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i)); 1789 return atmel_sha_cpu_hash(dd, hmac->opad, bs, false, 1790 atmel_sha_hmac_setup_done); 1791 } 1792 1793 static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd) 1794 { 1795 struct ahash_request *req = dd->req; 1796 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 1797 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); 1798 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1799 size_t hs = ctx->hash_size; 1800 size_t i, num_words = hs / sizeof(u32); 1801 1802 for (i = 0; i < num_words; ++i) 1803 hmac->opad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i)); 1804 atmel_sha_hmac_key_release(&hmac->hkey); 1805 return hmac->resume(dd); 1806 } 1807 1808 static int atmel_sha_hmac_start(struct atmel_sha_dev *dd) 1809 { 1810 struct ahash_request *req = dd->req; 1811 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1812 int err; 1813 1814 err = atmel_sha_hw_init(dd); 1815 if (err) 1816 return atmel_sha_complete(dd, err); 1817 1818 switch (ctx->op) { 1819 case SHA_OP_INIT: 1820 err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_init_done); 1821 break; 1822 1823 case SHA_OP_UPDATE: 1824 dd->resume = atmel_sha_done; 1825 err = atmel_sha_update_req(dd); 1826 break; 1827 1828 case SHA_OP_FINAL: 1829 dd->resume = atmel_sha_hmac_final; 1830 err = atmel_sha_final_req(dd); 1831 break; 1832 1833 case SHA_OP_DIGEST: 1834 err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_digest2); 1835 break; 1836 1837 default: 1838 return atmel_sha_complete(dd, -EINVAL); 1839 } 1840 1841 return err; 1842 } 1843 1844 static int atmel_sha_hmac_setkey(struct crypto_ahash *tfm, const u8 *key, 1845 unsigned int keylen) 1846 { 1847 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); 1848 1849 return atmel_sha_hmac_key_set(&hmac->hkey, key, keylen); 1850 } 1851 1852 static int atmel_sha_hmac_init(struct ahash_request *req) 1853 { 1854 int err; 1855 1856 err = atmel_sha_init(req); 1857 if (err) 1858 return err; 1859 1860 return atmel_sha_enqueue(req, SHA_OP_INIT); 1861 } 1862 1863 static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd) 1864 { 1865 struct ahash_request *req = dd->req; 1866 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1867 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 1868 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); 1869 size_t bs = ctx->block_size; 1870 size_t hs = ctx->hash_size; 1871 1872 ctx->bufcnt = 0; 1873 ctx->digcnt[0] = bs; 1874 ctx->digcnt[1] = 0; 1875 ctx->flags |= SHA_FLAGS_RESTORE; 1876 memcpy(ctx->digest, hmac->ipad, hs); 1877 return atmel_sha_complete(dd, 0); 1878 } 1879 1880 static int atmel_sha_hmac_final(struct atmel_sha_dev *dd) 1881 { 1882 struct ahash_request *req = dd->req; 1883 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1884 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 1885 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); 1886 u32 *digest = (u32 *)ctx->digest; 1887 size_t ds = crypto_ahash_digestsize(tfm); 1888 size_t bs = ctx->block_size; 1889 size_t hs = ctx->hash_size; 1890 size_t i, num_words; 1891 u32 mr; 1892 1893 /* Save d = SHA((K' + ipad) | msg). */ 1894 num_words = ds / sizeof(u32); 1895 for (i = 0; i < num_words; ++i) 1896 digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i)); 1897 1898 /* Restore context to finish computing SHA((K' + opad) | d). */ 1899 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV); 1900 num_words = hs / sizeof(u32); 1901 for (i = 0; i < num_words; ++i) 1902 atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]); 1903 1904 mr = SHA_MR_MODE_AUTO | SHA_MR_UIHV; 1905 mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK); 1906 atmel_sha_write(dd, SHA_MR, mr); 1907 atmel_sha_write(dd, SHA_MSR, bs + ds); 1908 atmel_sha_write(dd, SHA_BCR, ds); 1909 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST); 1910 1911 sg_init_one(&dd->tmp, digest, ds); 1912 return atmel_sha_cpu_start(dd, &dd->tmp, ds, false, true, 1913 atmel_sha_hmac_final_done); 1914 } 1915 1916 static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd) 1917 { 1918 /* 1919 * req->result might not be sizeof(u32) aligned, so copy the 1920 * digest into ctx->digest[] before memcpy() the data into 1921 * req->result. 1922 */ 1923 atmel_sha_copy_hash(dd->req); 1924 atmel_sha_copy_ready_hash(dd->req); 1925 return atmel_sha_complete(dd, 0); 1926 } 1927 1928 static int atmel_sha_hmac_digest(struct ahash_request *req) 1929 { 1930 int err; 1931 1932 err = atmel_sha_init(req); 1933 if (err) 1934 return err; 1935 1936 return atmel_sha_enqueue(req, SHA_OP_DIGEST); 1937 } 1938 1939 static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd) 1940 { 1941 struct ahash_request *req = dd->req; 1942 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); 1943 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 1944 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); 1945 struct scatterlist *sgbuf; 1946 size_t hs = ctx->hash_size; 1947 size_t i, num_words = hs / sizeof(u32); 1948 bool use_dma = false; 1949 u32 mr; 1950 1951 /* Special case for empty message. */ 1952 if (!req->nbytes) { 1953 req->nbytes = 0; 1954 ctx->bufcnt = 0; 1955 ctx->digcnt[0] = 0; 1956 ctx->digcnt[1] = 0; 1957 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) { 1958 case SHA_FLAGS_SHA1: 1959 case SHA_FLAGS_SHA224: 1960 case SHA_FLAGS_SHA256: 1961 atmel_sha_fill_padding(ctx, 64); 1962 break; 1963 1964 case SHA_FLAGS_SHA384: 1965 case SHA_FLAGS_SHA512: 1966 atmel_sha_fill_padding(ctx, 128); 1967 break; 1968 } 1969 sg_init_one(&dd->tmp, ctx->buffer, ctx->bufcnt); 1970 } 1971 1972 /* Check DMA threshold and alignment. */ 1973 if (req->nbytes > ATMEL_SHA_DMA_THRESHOLD && 1974 atmel_sha_dma_check_aligned(dd, req->src, req->nbytes)) 1975 use_dma = true; 1976 1977 /* Write both initial hash values to compute a HMAC. */ 1978 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV); 1979 for (i = 0; i < num_words; ++i) 1980 atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]); 1981 1982 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV); 1983 for (i = 0; i < num_words; ++i) 1984 atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]); 1985 1986 /* Write the Mode, Message Size, Bytes Count then Control Registers. */ 1987 mr = (SHA_MR_HMAC | SHA_MR_DUALBUFF); 1988 mr |= ctx->flags & SHA_FLAGS_ALGO_MASK; 1989 if (use_dma) 1990 mr |= SHA_MR_MODE_IDATAR0; 1991 else 1992 mr |= SHA_MR_MODE_AUTO; 1993 atmel_sha_write(dd, SHA_MR, mr); 1994 1995 atmel_sha_write(dd, SHA_MSR, req->nbytes); 1996 atmel_sha_write(dd, SHA_BCR, req->nbytes); 1997 1998 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST); 1999 2000 /* Special case for empty message. */ 2001 if (!req->nbytes) { 2002 sgbuf = &dd->tmp; 2003 req->nbytes = ctx->bufcnt; 2004 } else { 2005 sgbuf = req->src; 2006 } 2007 2008 /* Process data. */ 2009 if (use_dma) 2010 return atmel_sha_dma_start(dd, sgbuf, req->nbytes, 2011 atmel_sha_hmac_final_done); 2012 2013 return atmel_sha_cpu_start(dd, sgbuf, req->nbytes, false, true, 2014 atmel_sha_hmac_final_done); 2015 } 2016 2017 static int atmel_sha_hmac_cra_init(struct crypto_tfm *tfm) 2018 { 2019 struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm); 2020 2021 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), 2022 sizeof(struct atmel_sha_reqctx)); 2023 hmac->base.start = atmel_sha_hmac_start; 2024 atmel_sha_hmac_key_init(&hmac->hkey); 2025 2026 return 0; 2027 } 2028 2029 static void atmel_sha_hmac_cra_exit(struct crypto_tfm *tfm) 2030 { 2031 struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm); 2032 2033 atmel_sha_hmac_key_release(&hmac->hkey); 2034 } 2035 2036 static void atmel_sha_hmac_alg_init(struct ahash_alg *alg) 2037 { 2038 alg->halg.base.cra_priority = ATMEL_SHA_PRIORITY; 2039 alg->halg.base.cra_flags = CRYPTO_ALG_ASYNC | 2040 CRYPTO_ALG_KERN_DRIVER_ONLY; 2041 alg->halg.base.cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx); 2042 alg->halg.base.cra_module = THIS_MODULE; 2043 alg->halg.base.cra_init = atmel_sha_hmac_cra_init; 2044 alg->halg.base.cra_exit = atmel_sha_hmac_cra_exit; 2045 2046 alg->halg.statesize = sizeof(struct atmel_sha_reqctx); 2047 2048 alg->init = atmel_sha_hmac_init; 2049 alg->update = atmel_sha_update; 2050 alg->final = atmel_sha_final; 2051 alg->digest = atmel_sha_hmac_digest; 2052 alg->setkey = atmel_sha_hmac_setkey; 2053 alg->export = atmel_sha_export; 2054 alg->import = atmel_sha_import; 2055 } 2056 2057 static struct ahash_alg sha_hmac_algs[] = { 2058 { 2059 .halg.base.cra_name = "hmac(sha1)", 2060 .halg.base.cra_driver_name = "atmel-hmac-sha1", 2061 .halg.base.cra_blocksize = SHA1_BLOCK_SIZE, 2062 2063 .halg.digestsize = SHA1_DIGEST_SIZE, 2064 }, 2065 { 2066 .halg.base.cra_name = "hmac(sha224)", 2067 .halg.base.cra_driver_name = "atmel-hmac-sha224", 2068 .halg.base.cra_blocksize = SHA224_BLOCK_SIZE, 2069 2070 .halg.digestsize = SHA224_DIGEST_SIZE, 2071 }, 2072 { 2073 .halg.base.cra_name = "hmac(sha256)", 2074 .halg.base.cra_driver_name = "atmel-hmac-sha256", 2075 .halg.base.cra_blocksize = SHA256_BLOCK_SIZE, 2076 2077 .halg.digestsize = SHA256_DIGEST_SIZE, 2078 }, 2079 { 2080 .halg.base.cra_name = "hmac(sha384)", 2081 .halg.base.cra_driver_name = "atmel-hmac-sha384", 2082 .halg.base.cra_blocksize = SHA384_BLOCK_SIZE, 2083 2084 .halg.digestsize = SHA384_DIGEST_SIZE, 2085 }, 2086 { 2087 .halg.base.cra_name = "hmac(sha512)", 2088 .halg.base.cra_driver_name = "atmel-hmac-sha512", 2089 .halg.base.cra_blocksize = SHA512_BLOCK_SIZE, 2090 2091 .halg.digestsize = SHA512_DIGEST_SIZE, 2092 }, 2093 }; 2094 2095 #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC) 2096 /* authenc functions */ 2097 2098 static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd); 2099 static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd); 2100 static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd); 2101 2102 2103 struct atmel_sha_authenc_ctx { 2104 struct crypto_ahash *tfm; 2105 }; 2106 2107 struct atmel_sha_authenc_reqctx { 2108 struct atmel_sha_reqctx base; 2109 2110 atmel_aes_authenc_fn_t cb; 2111 struct atmel_aes_dev *aes_dev; 2112 2113 /* _init() parameters. */ 2114 struct scatterlist *assoc; 2115 u32 assoclen; 2116 u32 textlen; 2117 2118 /* _final() parameters. */ 2119 u32 *digest; 2120 unsigned int digestlen; 2121 }; 2122 2123 static void atmel_sha_authenc_complete(void *data, int err) 2124 { 2125 struct ahash_request *req = data; 2126 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req); 2127 2128 authctx->cb(authctx->aes_dev, err, authctx->base.dd->is_async); 2129 } 2130 2131 static int atmel_sha_authenc_start(struct atmel_sha_dev *dd) 2132 { 2133 struct ahash_request *req = dd->req; 2134 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req); 2135 int err; 2136 2137 /* 2138 * Force atmel_sha_complete() to call req->base.complete(), ie 2139 * atmel_sha_authenc_complete(), which in turn calls authctx->cb(). 2140 */ 2141 dd->force_complete = true; 2142 2143 err = atmel_sha_hw_init(dd); 2144 return authctx->cb(authctx->aes_dev, err, dd->is_async); 2145 } 2146 2147 bool atmel_sha_authenc_is_ready(void) 2148 { 2149 struct atmel_sha_ctx dummy; 2150 2151 dummy.dd = NULL; 2152 return (atmel_sha_find_dev(&dummy) != NULL); 2153 } 2154 EXPORT_SYMBOL_GPL(atmel_sha_authenc_is_ready); 2155 2156 unsigned int atmel_sha_authenc_get_reqsize(void) 2157 { 2158 return sizeof(struct atmel_sha_authenc_reqctx); 2159 } 2160 EXPORT_SYMBOL_GPL(atmel_sha_authenc_get_reqsize); 2161 2162 struct atmel_sha_authenc_ctx *atmel_sha_authenc_spawn(unsigned long mode) 2163 { 2164 struct atmel_sha_authenc_ctx *auth; 2165 struct crypto_ahash *tfm; 2166 struct atmel_sha_ctx *tctx; 2167 const char *name; 2168 int err = -EINVAL; 2169 2170 switch (mode & SHA_FLAGS_MODE_MASK) { 2171 case SHA_FLAGS_HMAC_SHA1: 2172 name = "atmel-hmac-sha1"; 2173 break; 2174 2175 case SHA_FLAGS_HMAC_SHA224: 2176 name = "atmel-hmac-sha224"; 2177 break; 2178 2179 case SHA_FLAGS_HMAC_SHA256: 2180 name = "atmel-hmac-sha256"; 2181 break; 2182 2183 case SHA_FLAGS_HMAC_SHA384: 2184 name = "atmel-hmac-sha384"; 2185 break; 2186 2187 case SHA_FLAGS_HMAC_SHA512: 2188 name = "atmel-hmac-sha512"; 2189 break; 2190 2191 default: 2192 goto error; 2193 } 2194 2195 tfm = crypto_alloc_ahash(name, 0, 0); 2196 if (IS_ERR(tfm)) { 2197 err = PTR_ERR(tfm); 2198 goto error; 2199 } 2200 tctx = crypto_ahash_ctx(tfm); 2201 tctx->start = atmel_sha_authenc_start; 2202 tctx->flags = mode; 2203 2204 auth = kzalloc_obj(*auth); 2205 if (!auth) { 2206 err = -ENOMEM; 2207 goto err_free_ahash; 2208 } 2209 auth->tfm = tfm; 2210 2211 return auth; 2212 2213 err_free_ahash: 2214 crypto_free_ahash(tfm); 2215 error: 2216 return ERR_PTR(err); 2217 } 2218 EXPORT_SYMBOL_GPL(atmel_sha_authenc_spawn); 2219 2220 void atmel_sha_authenc_free(struct atmel_sha_authenc_ctx *auth) 2221 { 2222 if (auth) 2223 crypto_free_ahash(auth->tfm); 2224 kfree(auth); 2225 } 2226 EXPORT_SYMBOL_GPL(atmel_sha_authenc_free); 2227 2228 int atmel_sha_authenc_setkey(struct atmel_sha_authenc_ctx *auth, 2229 const u8 *key, unsigned int keylen, u32 flags) 2230 { 2231 struct crypto_ahash *tfm = auth->tfm; 2232 2233 crypto_ahash_clear_flags(tfm, CRYPTO_TFM_REQ_MASK); 2234 crypto_ahash_set_flags(tfm, flags & CRYPTO_TFM_REQ_MASK); 2235 return crypto_ahash_setkey(tfm, key, keylen); 2236 } 2237 EXPORT_SYMBOL_GPL(atmel_sha_authenc_setkey); 2238 2239 int atmel_sha_authenc_schedule(struct ahash_request *req, 2240 struct atmel_sha_authenc_ctx *auth, 2241 atmel_aes_authenc_fn_t cb, 2242 struct atmel_aes_dev *aes_dev) 2243 { 2244 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req); 2245 struct atmel_sha_reqctx *ctx = &authctx->base; 2246 struct crypto_ahash *tfm = auth->tfm; 2247 struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm); 2248 struct atmel_sha_dev *dd; 2249 2250 /* Reset request context (MUST be done first). */ 2251 memset(authctx, 0, sizeof(*authctx)); 2252 2253 /* Get SHA device. */ 2254 dd = atmel_sha_find_dev(tctx); 2255 if (!dd) 2256 return cb(aes_dev, -ENODEV, false); 2257 2258 /* Init request context. */ 2259 ctx->dd = dd; 2260 ctx->buflen = SHA_BUFFER_LEN; 2261 authctx->cb = cb; 2262 authctx->aes_dev = aes_dev; 2263 ahash_request_set_tfm(req, tfm); 2264 ahash_request_set_callback(req, 0, atmel_sha_authenc_complete, req); 2265 2266 return atmel_sha_handle_queue(dd, req); 2267 } 2268 EXPORT_SYMBOL_GPL(atmel_sha_authenc_schedule); 2269 2270 int atmel_sha_authenc_init(struct ahash_request *req, 2271 struct scatterlist *assoc, unsigned int assoclen, 2272 unsigned int textlen, 2273 atmel_aes_authenc_fn_t cb, 2274 struct atmel_aes_dev *aes_dev) 2275 { 2276 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req); 2277 struct atmel_sha_reqctx *ctx = &authctx->base; 2278 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 2279 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); 2280 struct atmel_sha_dev *dd = ctx->dd; 2281 2282 if (unlikely(!IS_ALIGNED(assoclen, sizeof(u32)))) 2283 return atmel_sha_complete(dd, -EINVAL); 2284 2285 authctx->cb = cb; 2286 authctx->aes_dev = aes_dev; 2287 authctx->assoc = assoc; 2288 authctx->assoclen = assoclen; 2289 authctx->textlen = textlen; 2290 2291 ctx->flags = hmac->base.flags; 2292 return atmel_sha_hmac_setup(dd, atmel_sha_authenc_init2); 2293 } 2294 EXPORT_SYMBOL_GPL(atmel_sha_authenc_init); 2295 2296 static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd) 2297 { 2298 struct ahash_request *req = dd->req; 2299 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req); 2300 struct atmel_sha_reqctx *ctx = &authctx->base; 2301 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); 2302 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); 2303 size_t hs = ctx->hash_size; 2304 size_t i, num_words = hs / sizeof(u32); 2305 u32 mr, msg_size; 2306 2307 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV); 2308 for (i = 0; i < num_words; ++i) 2309 atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]); 2310 2311 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV); 2312 for (i = 0; i < num_words; ++i) 2313 atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]); 2314 2315 mr = (SHA_MR_MODE_IDATAR0 | 2316 SHA_MR_HMAC | 2317 SHA_MR_DUALBUFF); 2318 mr |= ctx->flags & SHA_FLAGS_ALGO_MASK; 2319 atmel_sha_write(dd, SHA_MR, mr); 2320 2321 msg_size = authctx->assoclen + authctx->textlen; 2322 atmel_sha_write(dd, SHA_MSR, msg_size); 2323 atmel_sha_write(dd, SHA_BCR, msg_size); 2324 2325 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST); 2326 2327 /* Process assoc data. */ 2328 return atmel_sha_cpu_start(dd, authctx->assoc, authctx->assoclen, 2329 true, false, 2330 atmel_sha_authenc_init_done); 2331 } 2332 2333 static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd) 2334 { 2335 struct ahash_request *req = dd->req; 2336 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req); 2337 2338 return authctx->cb(authctx->aes_dev, 0, dd->is_async); 2339 } 2340 2341 int atmel_sha_authenc_final(struct ahash_request *req, 2342 u32 *digest, unsigned int digestlen, 2343 atmel_aes_authenc_fn_t cb, 2344 struct atmel_aes_dev *aes_dev) 2345 { 2346 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req); 2347 struct atmel_sha_reqctx *ctx = &authctx->base; 2348 struct atmel_sha_dev *dd = ctx->dd; 2349 2350 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) { 2351 case SHA_FLAGS_SHA1: 2352 authctx->digestlen = SHA1_DIGEST_SIZE; 2353 break; 2354 2355 case SHA_FLAGS_SHA224: 2356 authctx->digestlen = SHA224_DIGEST_SIZE; 2357 break; 2358 2359 case SHA_FLAGS_SHA256: 2360 authctx->digestlen = SHA256_DIGEST_SIZE; 2361 break; 2362 2363 case SHA_FLAGS_SHA384: 2364 authctx->digestlen = SHA384_DIGEST_SIZE; 2365 break; 2366 2367 case SHA_FLAGS_SHA512: 2368 authctx->digestlen = SHA512_DIGEST_SIZE; 2369 break; 2370 2371 default: 2372 return atmel_sha_complete(dd, -EINVAL); 2373 } 2374 if (authctx->digestlen > digestlen) 2375 authctx->digestlen = digestlen; 2376 2377 authctx->cb = cb; 2378 authctx->aes_dev = aes_dev; 2379 authctx->digest = digest; 2380 return atmel_sha_wait_for_data_ready(dd, 2381 atmel_sha_authenc_final_done); 2382 } 2383 EXPORT_SYMBOL_GPL(atmel_sha_authenc_final); 2384 2385 static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd) 2386 { 2387 struct ahash_request *req = dd->req; 2388 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req); 2389 atmel_aes_authenc_fn_t cb; 2390 struct atmel_aes_dev *aes_dev; 2391 size_t i, num_words = authctx->digestlen / sizeof(u32); 2392 2393 for (i = 0; i < num_words; ++i) 2394 authctx->digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i)); 2395 2396 if (!dd->is_async) { 2397 /* 2398 * Return the AES finalizer's status directly for synchronous requests. 2399 * The ahash callback cannot propagate it. 2400 */ 2401 cb = authctx->cb; 2402 aes_dev = authctx->aes_dev; 2403 dd->force_complete = false; 2404 (void)atmel_sha_complete(dd, 0); 2405 return cb(aes_dev, 0, false); 2406 } 2407 2408 return atmel_sha_complete(dd, 0); 2409 } 2410 2411 void atmel_sha_authenc_abort(struct ahash_request *req) 2412 { 2413 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req); 2414 struct atmel_sha_reqctx *ctx = &authctx->base; 2415 struct atmel_sha_dev *dd = ctx->dd; 2416 2417 /* Prevent atmel_sha_complete() from calling req->base.complete(). */ 2418 dd->is_async = false; 2419 dd->force_complete = false; 2420 (void)atmel_sha_complete(dd, 0); 2421 } 2422 EXPORT_SYMBOL_GPL(atmel_sha_authenc_abort); 2423 2424 #endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */ 2425 2426 2427 static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd) 2428 { 2429 if (dd->caps.has_hmac) 2430 crypto_unregister_ahashes(sha_hmac_algs, 2431 ARRAY_SIZE(sha_hmac_algs)); 2432 2433 crypto_unregister_ahashes(sha_1_256_algs, ARRAY_SIZE(sha_1_256_algs)); 2434 2435 if (dd->caps.has_sha224) 2436 crypto_unregister_ahash(&sha_224_alg); 2437 2438 if (dd->caps.has_sha_384_512) 2439 crypto_unregister_ahashes(sha_384_512_algs, 2440 ARRAY_SIZE(sha_384_512_algs)); 2441 } 2442 2443 static int atmel_sha_register_algs(struct atmel_sha_dev *dd) 2444 { 2445 int err, i; 2446 2447 for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) { 2448 atmel_sha_alg_init(&sha_1_256_algs[i]); 2449 2450 err = crypto_register_ahash(&sha_1_256_algs[i]); 2451 if (err) 2452 goto err_sha_1_256_algs; 2453 } 2454 2455 if (dd->caps.has_sha224) { 2456 atmel_sha_alg_init(&sha_224_alg); 2457 2458 err = crypto_register_ahash(&sha_224_alg); 2459 if (err) 2460 goto err_sha_224_algs; 2461 } 2462 2463 if (dd->caps.has_sha_384_512) { 2464 for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) { 2465 atmel_sha_alg_init(&sha_384_512_algs[i]); 2466 2467 err = crypto_register_ahash(&sha_384_512_algs[i]); 2468 if (err) 2469 goto err_sha_384_512_algs; 2470 } 2471 } 2472 2473 if (dd->caps.has_hmac) { 2474 for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++) { 2475 atmel_sha_hmac_alg_init(&sha_hmac_algs[i]); 2476 2477 err = crypto_register_ahash(&sha_hmac_algs[i]); 2478 if (err) 2479 goto err_sha_hmac_algs; 2480 } 2481 } 2482 2483 return 0; 2484 2485 /*i = ARRAY_SIZE(sha_hmac_algs);*/ 2486 err_sha_hmac_algs: 2487 crypto_unregister_ahashes(sha_hmac_algs, i); 2488 i = ARRAY_SIZE(sha_384_512_algs); 2489 err_sha_384_512_algs: 2490 crypto_unregister_ahashes(sha_384_512_algs, i); 2491 crypto_unregister_ahash(&sha_224_alg); 2492 err_sha_224_algs: 2493 i = ARRAY_SIZE(sha_1_256_algs); 2494 err_sha_1_256_algs: 2495 crypto_unregister_ahashes(sha_1_256_algs, i); 2496 2497 return err; 2498 } 2499 2500 static int atmel_sha_dma_init(struct atmel_sha_dev *dd) 2501 { 2502 dd->dma_lch_in.chan = dma_request_chan(dd->dev, "tx"); 2503 if (IS_ERR(dd->dma_lch_in.chan)) { 2504 return dev_err_probe(dd->dev, PTR_ERR(dd->dma_lch_in.chan), 2505 "DMA channel is not available\n"); 2506 } 2507 2508 dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base + 2509 SHA_REG_DIN(0); 2510 dd->dma_lch_in.dma_conf.src_maxburst = 1; 2511 dd->dma_lch_in.dma_conf.src_addr_width = 2512 DMA_SLAVE_BUSWIDTH_4_BYTES; 2513 dd->dma_lch_in.dma_conf.dst_maxburst = 1; 2514 dd->dma_lch_in.dma_conf.dst_addr_width = 2515 DMA_SLAVE_BUSWIDTH_4_BYTES; 2516 dd->dma_lch_in.dma_conf.device_fc = false; 2517 2518 return 0; 2519 } 2520 2521 static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd) 2522 { 2523 dma_release_channel(dd->dma_lch_in.chan); 2524 } 2525 2526 static void atmel_sha_get_cap(struct atmel_sha_dev *dd) 2527 { 2528 2529 dd->caps.has_dma = 0; 2530 dd->caps.has_dualbuff = 0; 2531 dd->caps.has_sha224 = 0; 2532 dd->caps.has_sha_384_512 = 0; 2533 dd->caps.has_uihv = 0; 2534 dd->caps.has_hmac = 0; 2535 2536 /* keep only major version number */ 2537 switch (dd->hw_version & 0xff0) { 2538 case 0x800: 2539 case 0x700: 2540 case 0x600: 2541 case 0x510: 2542 dd->caps.has_dma = 1; 2543 dd->caps.has_dualbuff = 1; 2544 dd->caps.has_sha224 = 1; 2545 dd->caps.has_sha_384_512 = 1; 2546 dd->caps.has_uihv = 1; 2547 dd->caps.has_hmac = 1; 2548 break; 2549 case 0x420: 2550 dd->caps.has_dma = 1; 2551 dd->caps.has_dualbuff = 1; 2552 dd->caps.has_sha224 = 1; 2553 dd->caps.has_sha_384_512 = 1; 2554 dd->caps.has_uihv = 1; 2555 break; 2556 case 0x410: 2557 dd->caps.has_dma = 1; 2558 dd->caps.has_dualbuff = 1; 2559 dd->caps.has_sha224 = 1; 2560 dd->caps.has_sha_384_512 = 1; 2561 break; 2562 case 0x400: 2563 dd->caps.has_dma = 1; 2564 dd->caps.has_dualbuff = 1; 2565 dd->caps.has_sha224 = 1; 2566 break; 2567 case 0x320: 2568 break; 2569 default: 2570 dev_warn(dd->dev, 2571 "Unmanaged sha version, set minimum capabilities\n"); 2572 break; 2573 } 2574 } 2575 2576 static const struct of_device_id atmel_sha_dt_ids[] = { 2577 { .compatible = "atmel,at91sam9g46-sha" }, 2578 { /* sentinel */ } 2579 }; 2580 2581 MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids); 2582 2583 static int atmel_sha_probe(struct platform_device *pdev) 2584 { 2585 struct atmel_sha_dev *sha_dd; 2586 struct device *dev = &pdev->dev; 2587 struct resource *sha_res; 2588 int err; 2589 2590 sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL); 2591 if (!sha_dd) 2592 return -ENOMEM; 2593 2594 sha_dd->dev = dev; 2595 2596 platform_set_drvdata(pdev, sha_dd); 2597 2598 INIT_LIST_HEAD(&sha_dd->list); 2599 spin_lock_init(&sha_dd->lock); 2600 2601 tasklet_init(&sha_dd->done_task, atmel_sha_done_task, 2602 (unsigned long)sha_dd); 2603 tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task, 2604 (unsigned long)sha_dd); 2605 2606 crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH); 2607 2608 sha_dd->io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &sha_res); 2609 if (IS_ERR(sha_dd->io_base)) { 2610 err = PTR_ERR(sha_dd->io_base); 2611 goto err_tasklet_kill; 2612 } 2613 sha_dd->phys_base = sha_res->start; 2614 2615 /* Get the IRQ */ 2616 sha_dd->irq = platform_get_irq(pdev, 0); 2617 if (sha_dd->irq < 0) { 2618 err = sha_dd->irq; 2619 goto err_tasklet_kill; 2620 } 2621 2622 err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq, 2623 IRQF_SHARED, "atmel-sha", sha_dd); 2624 if (err) 2625 goto err_tasklet_kill; 2626 2627 /* Initializing the clock */ 2628 sha_dd->iclk = devm_clk_get_prepared(&pdev->dev, "sha_clk"); 2629 if (IS_ERR(sha_dd->iclk)) { 2630 dev_err(dev, "clock initialization failed.\n"); 2631 err = PTR_ERR(sha_dd->iclk); 2632 goto err_tasklet_kill; 2633 } 2634 2635 err = atmel_sha_hw_version_init(sha_dd); 2636 if (err) 2637 goto err_tasklet_kill; 2638 2639 atmel_sha_get_cap(sha_dd); 2640 2641 if (sha_dd->caps.has_dma) { 2642 err = atmel_sha_dma_init(sha_dd); 2643 if (err) 2644 goto err_tasklet_kill; 2645 2646 dev_info(dev, "using %s for DMA transfers\n", 2647 dma_chan_name(sha_dd->dma_lch_in.chan)); 2648 } 2649 2650 spin_lock(&atmel_sha.lock); 2651 list_add_tail(&sha_dd->list, &atmel_sha.dev_list); 2652 spin_unlock(&atmel_sha.lock); 2653 2654 err = atmel_sha_register_algs(sha_dd); 2655 if (err) 2656 goto err_algs; 2657 2658 dev_info(dev, "Atmel SHA1/SHA256%s%s\n", 2659 sha_dd->caps.has_sha224 ? "/SHA224" : "", 2660 sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : ""); 2661 2662 return 0; 2663 2664 err_algs: 2665 spin_lock(&atmel_sha.lock); 2666 list_del(&sha_dd->list); 2667 spin_unlock(&atmel_sha.lock); 2668 if (sha_dd->caps.has_dma) 2669 atmel_sha_dma_cleanup(sha_dd); 2670 err_tasklet_kill: 2671 tasklet_kill(&sha_dd->queue_task); 2672 tasklet_kill(&sha_dd->done_task); 2673 2674 return err; 2675 } 2676 2677 static void atmel_sha_remove(struct platform_device *pdev) 2678 { 2679 struct atmel_sha_dev *sha_dd = platform_get_drvdata(pdev); 2680 2681 spin_lock(&atmel_sha.lock); 2682 list_del(&sha_dd->list); 2683 spin_unlock(&atmel_sha.lock); 2684 2685 atmel_sha_unregister_algs(sha_dd); 2686 2687 tasklet_kill(&sha_dd->queue_task); 2688 tasklet_kill(&sha_dd->done_task); 2689 2690 if (sha_dd->caps.has_dma) 2691 atmel_sha_dma_cleanup(sha_dd); 2692 } 2693 2694 static struct platform_driver atmel_sha_driver = { 2695 .probe = atmel_sha_probe, 2696 .remove = atmel_sha_remove, 2697 .driver = { 2698 .name = "atmel_sha", 2699 .of_match_table = atmel_sha_dt_ids, 2700 }, 2701 }; 2702 2703 module_platform_driver(atmel_sha_driver); 2704 2705 MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support."); 2706 MODULE_LICENSE("GPL v2"); 2707 MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique"); 2708