1 // SPDX-License-Identifier: GPL-2.0
5 * Support for ATMEL SHA1/SHA256 HW acceleration.
7 * Copyright (c) 2012 Eukréa Electromatique - ATMEL
8 * Author: Nicolas Royer <nicolas@eukrea.com>
10 * Some ideas are from omap-sham.c drivers.
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>
20 #include <linux/hw_random.h>
21 #include <linux/platform_device.h>
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/mod_devicetable.h>
32 #include <linux/delay.h>
33 #include <linux/crypto.h>
34 #include <crypto/scatterwalk.h>
35 #include <crypto/algapi.h>
36 #include <crypto/sha1.h>
37 #include <crypto/sha2.h>
38 #include <crypto/hash.h>
39 #include <crypto/internal/hash.h>
40 #include "atmel-sha-regs.h"
41 #include "atmel-authenc.h"
43 #define ATMEL_SHA_PRIORITY 300
46 #define SHA_FLAGS_BUSY BIT(0)
47 #define SHA_FLAGS_FINAL BIT(1)
48 #define SHA_FLAGS_DMA_ACTIVE BIT(2)
49 #define SHA_FLAGS_OUTPUT_READY BIT(3)
50 #define SHA_FLAGS_INIT BIT(4)
51 #define SHA_FLAGS_CPU BIT(5)
52 #define SHA_FLAGS_DMA_READY BIT(6)
53 #define SHA_FLAGS_DUMP_REG BIT(7)
55 /* bits[11:8] are reserved. */
57 #define SHA_FLAGS_FINUP BIT(16)
58 #define SHA_FLAGS_SG BIT(17)
59 #define SHA_FLAGS_ERROR BIT(23)
60 #define SHA_FLAGS_PAD BIT(24)
61 #define SHA_FLAGS_RESTORE BIT(25)
62 #define SHA_FLAGS_IDATAR0 BIT(26)
63 #define SHA_FLAGS_WAIT_DATARDY BIT(27)
66 #define SHA_OP_UPDATE 1
67 #define SHA_OP_FINAL 2
68 #define SHA_OP_DIGEST 3
70 #define SHA_BUFFER_LEN (PAGE_SIZE / 16)
72 #define ATMEL_SHA_DMA_THRESHOLD 56
74 struct atmel_sha_caps {
86 * .statesize = sizeof(struct atmel_sha_reqctx) must be <= PAGE_SIZE / 8 as
87 * tested by the ahash_prepare_alg() function.
89 struct atmel_sha_reqctx {
90 struct atmel_sha_dev *dd;
94 u8 digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32));
101 struct scatterlist *sg;
102 unsigned int offset; /* offset in current sg */
103 unsigned int total; /* total request */
108 u8 buffer[SHA_BUFFER_LEN + SHA512_BLOCK_SIZE] __aligned(sizeof(u32));
111 typedef int (*atmel_sha_fn_t)(struct atmel_sha_dev *);
113 struct atmel_sha_ctx {
114 struct atmel_sha_dev *dd;
115 atmel_sha_fn_t start;
120 #define ATMEL_SHA_QUEUE_LENGTH 50
122 struct atmel_sha_dma {
123 struct dma_chan *chan;
124 struct dma_slave_config dma_conf;
125 struct scatterlist *sg;
127 unsigned int last_sg_length;
130 struct atmel_sha_dev {
131 struct list_head list;
132 unsigned long phys_base;
136 void __iomem *io_base;
139 struct tasklet_struct done_task;
140 struct tasklet_struct queue_task;
143 struct crypto_queue queue;
144 struct ahash_request *req;
147 atmel_sha_fn_t resume;
148 atmel_sha_fn_t cpu_transfer_complete;
150 struct atmel_sha_dma dma_lch_in;
152 struct atmel_sha_caps caps;
154 struct scatterlist tmp;
159 struct atmel_sha_drv {
160 struct list_head dev_list;
164 static struct atmel_sha_drv atmel_sha = {
165 .dev_list = LIST_HEAD_INIT(atmel_sha.dev_list),
166 .lock = __SPIN_LOCK_UNLOCKED(atmel_sha.lock),
170 static const char *atmel_sha_reg_name(u32 offset, char *tmp, size_t sz, bool wr)
207 case SHA_REG_DIN(10):
208 case SHA_REG_DIN(11):
209 case SHA_REG_DIN(12):
210 case SHA_REG_DIN(13):
211 case SHA_REG_DIN(14):
212 case SHA_REG_DIN(15):
213 snprintf(tmp, sz, "IDATAR[%u]", (offset - SHA_REG_DIN(0)) >> 2);
216 case SHA_REG_DIGEST(0):
217 case SHA_REG_DIGEST(1):
218 case SHA_REG_DIGEST(2):
219 case SHA_REG_DIGEST(3):
220 case SHA_REG_DIGEST(4):
221 case SHA_REG_DIGEST(5):
222 case SHA_REG_DIGEST(6):
223 case SHA_REG_DIGEST(7):
224 case SHA_REG_DIGEST(8):
225 case SHA_REG_DIGEST(9):
226 case SHA_REG_DIGEST(10):
227 case SHA_REG_DIGEST(11):
228 case SHA_REG_DIGEST(12):
229 case SHA_REG_DIGEST(13):
230 case SHA_REG_DIGEST(14):
231 case SHA_REG_DIGEST(15):
233 snprintf(tmp, sz, "IDATAR[%u]",
234 16u + ((offset - SHA_REG_DIGEST(0)) >> 2));
236 snprintf(tmp, sz, "ODATAR[%u]",
237 (offset - SHA_REG_DIGEST(0)) >> 2);
244 snprintf(tmp, sz, "0x%02x", offset);
251 #endif /* VERBOSE_DEBUG */
253 static inline u32 atmel_sha_read(struct atmel_sha_dev *dd, u32 offset)
255 u32 value = readl_relaxed(dd->io_base + offset);
258 if (dd->flags & SHA_FLAGS_DUMP_REG) {
261 dev_vdbg(dd->dev, "read 0x%08x from %s\n", value,
262 atmel_sha_reg_name(offset, tmp, sizeof(tmp), false));
264 #endif /* VERBOSE_DEBUG */
269 static inline void atmel_sha_write(struct atmel_sha_dev *dd,
270 u32 offset, u32 value)
273 if (dd->flags & SHA_FLAGS_DUMP_REG) {
276 dev_vdbg(dd->dev, "write 0x%08x into %s\n", value,
277 atmel_sha_reg_name(offset, tmp, sizeof(tmp), true));
279 #endif /* VERBOSE_DEBUG */
281 writel_relaxed(value, dd->io_base + offset);
284 static inline int atmel_sha_complete(struct atmel_sha_dev *dd, int err)
286 struct ahash_request *req = dd->req;
288 dd->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL | SHA_FLAGS_CPU |
289 SHA_FLAGS_DMA_READY | SHA_FLAGS_OUTPUT_READY |
292 clk_disable(dd->iclk);
294 if ((dd->is_async || dd->force_complete) && req->base.complete)
295 ahash_request_complete(req, err);
297 /* handle new request */
298 tasklet_schedule(&dd->queue_task);
303 static size_t atmel_sha_append_sg(struct atmel_sha_reqctx *ctx)
307 while ((ctx->bufcnt < ctx->buflen) && ctx->total) {
308 count = min(ctx->sg->length - ctx->offset, ctx->total);
309 count = min(count, ctx->buflen - ctx->bufcnt);
313 * Check if count <= 0 because the buffer is full or
314 * because the sg length is 0. In the latest case,
315 * check if there is another sg in the list, a 0 length
316 * sg doesn't necessarily mean the end of the sg list.
318 if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) {
319 ctx->sg = sg_next(ctx->sg);
326 scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg,
327 ctx->offset, count, 0);
329 ctx->bufcnt += count;
330 ctx->offset += count;
333 if (ctx->offset == ctx->sg->length) {
334 ctx->sg = sg_next(ctx->sg);
346 * The purpose of this padding is to ensure that the padded message is a
347 * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
348 * The bit "1" is appended at the end of the message followed by
349 * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
350 * 128 bits block (SHA384/SHA512) equals to the message length in bits
353 * For SHA1/SHA224/SHA256, padlen is calculated as followed:
354 * - if message length < 56 bytes then padlen = 56 - message length
355 * - else padlen = 64 + 56 - message length
357 * For SHA384/SHA512, padlen is calculated as followed:
358 * - if message length < 112 bytes then padlen = 112 - message length
359 * - else padlen = 128 + 112 - message length
361 static void atmel_sha_fill_padding(struct atmel_sha_reqctx *ctx, int length)
363 unsigned int index, padlen;
367 size[0] = ctx->digcnt[0];
368 size[1] = ctx->digcnt[1];
370 size[0] += ctx->bufcnt;
371 if (size[0] < ctx->bufcnt)
375 if (size[0] < length)
378 bits[1] = cpu_to_be64(size[0] << 3);
379 bits[0] = cpu_to_be64(size[1] << 3 | size[0] >> 61);
381 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
382 case SHA_FLAGS_SHA384:
383 case SHA_FLAGS_SHA512:
384 index = ctx->bufcnt & 0x7f;
385 padlen = (index < 112) ? (112 - index) : ((128+112) - index);
386 *(ctx->buffer + ctx->bufcnt) = 0x80;
387 memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
388 memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16);
389 ctx->bufcnt += padlen + 16;
390 ctx->flags |= SHA_FLAGS_PAD;
394 index = ctx->bufcnt & 0x3f;
395 padlen = (index < 56) ? (56 - index) : ((64+56) - index);
396 *(ctx->buffer + ctx->bufcnt) = 0x80;
397 memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1);
398 memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8);
399 ctx->bufcnt += padlen + 8;
400 ctx->flags |= SHA_FLAGS_PAD;
405 static struct atmel_sha_dev *atmel_sha_find_dev(struct atmel_sha_ctx *tctx)
407 struct atmel_sha_dev *dd = NULL;
408 struct atmel_sha_dev *tmp;
410 spin_lock_bh(&atmel_sha.lock);
412 list_for_each_entry(tmp, &atmel_sha.dev_list, list) {
421 spin_unlock_bh(&atmel_sha.lock);
426 static int atmel_sha_init(struct ahash_request *req)
428 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
429 struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
430 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
431 struct atmel_sha_dev *dd = atmel_sha_find_dev(tctx);
437 dev_dbg(dd->dev, "init: digest size: %u\n",
438 crypto_ahash_digestsize(tfm));
440 switch (crypto_ahash_digestsize(tfm)) {
441 case SHA1_DIGEST_SIZE:
442 ctx->flags |= SHA_FLAGS_SHA1;
443 ctx->block_size = SHA1_BLOCK_SIZE;
445 case SHA224_DIGEST_SIZE:
446 ctx->flags |= SHA_FLAGS_SHA224;
447 ctx->block_size = SHA224_BLOCK_SIZE;
449 case SHA256_DIGEST_SIZE:
450 ctx->flags |= SHA_FLAGS_SHA256;
451 ctx->block_size = SHA256_BLOCK_SIZE;
453 case SHA384_DIGEST_SIZE:
454 ctx->flags |= SHA_FLAGS_SHA384;
455 ctx->block_size = SHA384_BLOCK_SIZE;
457 case SHA512_DIGEST_SIZE:
458 ctx->flags |= SHA_FLAGS_SHA512;
459 ctx->block_size = SHA512_BLOCK_SIZE;
468 ctx->buflen = SHA_BUFFER_LEN;
473 static void atmel_sha_write_ctrl(struct atmel_sha_dev *dd, int dma)
475 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
476 u32 valmr = SHA_MR_MODE_AUTO;
477 unsigned int i, hashsize = 0;
480 if (!dd->caps.has_dma)
481 atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE);
482 valmr = SHA_MR_MODE_PDC;
483 if (dd->caps.has_dualbuff)
484 valmr |= SHA_MR_DUALBUFF;
486 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
489 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
491 valmr |= SHA_MR_ALGO_SHA1;
492 hashsize = SHA1_DIGEST_SIZE;
495 case SHA_FLAGS_SHA224:
496 valmr |= SHA_MR_ALGO_SHA224;
497 hashsize = SHA256_DIGEST_SIZE;
500 case SHA_FLAGS_SHA256:
501 valmr |= SHA_MR_ALGO_SHA256;
502 hashsize = SHA256_DIGEST_SIZE;
505 case SHA_FLAGS_SHA384:
506 valmr |= SHA_MR_ALGO_SHA384;
507 hashsize = SHA512_DIGEST_SIZE;
510 case SHA_FLAGS_SHA512:
511 valmr |= SHA_MR_ALGO_SHA512;
512 hashsize = SHA512_DIGEST_SIZE;
519 /* Setting CR_FIRST only for the first iteration */
520 if (!(ctx->digcnt[0] || ctx->digcnt[1])) {
521 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
522 } else if (dd->caps.has_uihv && (ctx->flags & SHA_FLAGS_RESTORE)) {
523 const u32 *hash = (const u32 *)ctx->digest;
526 * Restore the hardware context: update the User Initialize
527 * Hash Value (UIHV) with the value saved when the latest
528 * 'update' operation completed on this very same crypto
531 ctx->flags &= ~SHA_FLAGS_RESTORE;
532 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
533 for (i = 0; i < hashsize / sizeof(u32); ++i)
534 atmel_sha_write(dd, SHA_REG_DIN(i), hash[i]);
535 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
536 valmr |= SHA_MR_UIHV;
539 * WARNING: If the UIHV feature is not available, the hardware CANNOT
540 * process concurrent requests: the internal registers used to store
541 * the hash/digest are still set to the partial digest output values
542 * computed during the latest round.
545 atmel_sha_write(dd, SHA_MR, valmr);
548 static inline int atmel_sha_wait_for_data_ready(struct atmel_sha_dev *dd,
549 atmel_sha_fn_t resume)
551 u32 isr = atmel_sha_read(dd, SHA_ISR);
553 if (unlikely(isr & SHA_INT_DATARDY))
557 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
561 static int atmel_sha_xmit_cpu(struct atmel_sha_dev *dd, const u8 *buf,
562 size_t length, int final)
564 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
566 const u32 *buffer = (const u32 *)buf;
568 dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
569 ctx->digcnt[1], ctx->digcnt[0], length, final);
571 atmel_sha_write_ctrl(dd, 0);
573 /* should be non-zero before next lines to disable clocks later */
574 ctx->digcnt[0] += length;
575 if (ctx->digcnt[0] < length)
579 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
581 len32 = DIV_ROUND_UP(length, sizeof(u32));
583 dd->flags |= SHA_FLAGS_CPU;
585 for (count = 0; count < len32; count++)
586 atmel_sha_write(dd, SHA_REG_DIN(count), buffer[count]);
591 static int atmel_sha_xmit_pdc(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
592 size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
594 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
597 dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
598 ctx->digcnt[1], ctx->digcnt[0], length1, final);
600 len32 = DIV_ROUND_UP(length1, sizeof(u32));
601 atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS);
602 atmel_sha_write(dd, SHA_TPR, dma_addr1);
603 atmel_sha_write(dd, SHA_TCR, len32);
605 len32 = DIV_ROUND_UP(length2, sizeof(u32));
606 atmel_sha_write(dd, SHA_TNPR, dma_addr2);
607 atmel_sha_write(dd, SHA_TNCR, len32);
609 atmel_sha_write_ctrl(dd, 1);
611 /* should be non-zero before next lines to disable clocks later */
612 ctx->digcnt[0] += length1;
613 if (ctx->digcnt[0] < length1)
617 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
619 dd->flags |= SHA_FLAGS_DMA_ACTIVE;
621 /* Start DMA transfer */
622 atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTEN);
627 static void atmel_sha_dma_callback(void *data)
629 struct atmel_sha_dev *dd = data;
633 /* dma_lch_in - completed - wait DATRDY */
634 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
637 static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
638 size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
640 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
641 struct dma_async_tx_descriptor *in_desc;
642 struct scatterlist sg[2];
644 dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
645 ctx->digcnt[1], ctx->digcnt[0], length1, final);
647 dd->dma_lch_in.dma_conf.src_maxburst = 16;
648 dd->dma_lch_in.dma_conf.dst_maxburst = 16;
650 dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf);
653 sg_init_table(sg, 2);
654 sg_dma_address(&sg[0]) = dma_addr1;
655 sg_dma_len(&sg[0]) = length1;
656 sg_dma_address(&sg[1]) = dma_addr2;
657 sg_dma_len(&sg[1]) = length2;
658 in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2,
659 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
661 sg_init_table(sg, 1);
662 sg_dma_address(&sg[0]) = dma_addr1;
663 sg_dma_len(&sg[0]) = length1;
664 in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1,
665 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
668 return atmel_sha_complete(dd, -EINVAL);
670 in_desc->callback = atmel_sha_dma_callback;
671 in_desc->callback_param = dd;
673 atmel_sha_write_ctrl(dd, 1);
675 /* should be non-zero before next lines to disable clocks later */
676 ctx->digcnt[0] += length1;
677 if (ctx->digcnt[0] < length1)
681 dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */
683 dd->flags |= SHA_FLAGS_DMA_ACTIVE;
685 /* Start DMA transfer */
686 dmaengine_submit(in_desc);
687 dma_async_issue_pending(dd->dma_lch_in.chan);
692 static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1,
693 size_t length1, dma_addr_t dma_addr2, size_t length2, int final)
695 if (dd->caps.has_dma)
696 return atmel_sha_xmit_dma(dd, dma_addr1, length1,
697 dma_addr2, length2, final);
699 return atmel_sha_xmit_pdc(dd, dma_addr1, length1,
700 dma_addr2, length2, final);
703 static int atmel_sha_update_cpu(struct atmel_sha_dev *dd)
705 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
708 atmel_sha_append_sg(ctx);
709 atmel_sha_fill_padding(ctx, 0);
710 bufcnt = ctx->bufcnt;
713 return atmel_sha_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
716 static int atmel_sha_xmit_dma_map(struct atmel_sha_dev *dd,
717 struct atmel_sha_reqctx *ctx,
718 size_t length, int final)
720 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
721 ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
722 if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
723 dev_err(dd->dev, "dma %zu bytes error\n", ctx->buflen +
725 return atmel_sha_complete(dd, -EINVAL);
728 ctx->flags &= ~SHA_FLAGS_SG;
730 /* next call does not fail... so no unmap in the case of error */
731 return atmel_sha_xmit_start(dd, ctx->dma_addr, length, 0, 0, final);
734 static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd)
736 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
740 atmel_sha_append_sg(ctx);
742 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
744 dev_dbg(dd->dev, "slow: bufcnt: %zu, digcnt: 0x%llx 0x%llx, final: %d\n",
745 ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final);
748 atmel_sha_fill_padding(ctx, 0);
750 if (final || (ctx->bufcnt == ctx->buflen)) {
753 return atmel_sha_xmit_dma_map(dd, ctx, count, final);
759 static int atmel_sha_update_dma_start(struct atmel_sha_dev *dd)
761 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
762 unsigned int length, final, tail;
763 struct scatterlist *sg;
769 if (ctx->bufcnt || ctx->offset)
770 return atmel_sha_update_dma_slow(dd);
772 dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %zd, total: %u\n",
773 ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total);
777 if (!IS_ALIGNED(sg->offset, sizeof(u32)))
778 return atmel_sha_update_dma_slow(dd);
780 if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size))
781 /* size is not ctx->block_size aligned */
782 return atmel_sha_update_dma_slow(dd);
784 length = min(ctx->total, sg->length);
786 if (sg_is_last(sg)) {
787 if (!(ctx->flags & SHA_FLAGS_FINUP)) {
788 /* not last sg must be ctx->block_size aligned */
789 tail = length & (ctx->block_size - 1);
794 ctx->total -= length;
795 ctx->offset = length; /* offset where to start slow */
797 final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total;
801 tail = length & (ctx->block_size - 1);
804 ctx->offset = length; /* offset where to start slow */
807 atmel_sha_append_sg(ctx);
809 atmel_sha_fill_padding(ctx, length);
811 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer,
812 ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
813 if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
814 dev_err(dd->dev, "dma %zu bytes error\n",
815 ctx->buflen + ctx->block_size);
816 return atmel_sha_complete(dd, -EINVAL);
820 ctx->flags &= ~SHA_FLAGS_SG;
823 return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0,
827 if (!dma_map_sg(dd->dev, ctx->sg, 1,
829 dev_err(dd->dev, "dma_map_sg error\n");
830 return atmel_sha_complete(dd, -EINVAL);
833 ctx->flags |= SHA_FLAGS_SG;
837 return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg),
838 length, ctx->dma_addr, count, final);
842 if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
843 dev_err(dd->dev, "dma_map_sg error\n");
844 return atmel_sha_complete(dd, -EINVAL);
847 ctx->flags |= SHA_FLAGS_SG;
849 /* next call does not fail... so no unmap in the case of error */
850 return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), length, 0,
854 static void atmel_sha_update_dma_stop(struct atmel_sha_dev *dd)
856 struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req);
858 if (ctx->flags & SHA_FLAGS_SG) {
859 dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
860 if (ctx->sg->length == ctx->offset) {
861 ctx->sg = sg_next(ctx->sg);
865 if (ctx->flags & SHA_FLAGS_PAD) {
866 dma_unmap_single(dd->dev, ctx->dma_addr,
867 ctx->buflen + ctx->block_size, DMA_TO_DEVICE);
870 dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen +
871 ctx->block_size, DMA_TO_DEVICE);
875 static int atmel_sha_update_req(struct atmel_sha_dev *dd)
877 struct ahash_request *req = dd->req;
878 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
881 dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n",
882 ctx->total, ctx->digcnt[1], ctx->digcnt[0]);
884 if (ctx->flags & SHA_FLAGS_CPU)
885 err = atmel_sha_update_cpu(dd);
887 err = atmel_sha_update_dma_start(dd);
889 /* wait for dma completion before can take more data */
890 dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx\n",
891 err, ctx->digcnt[1], ctx->digcnt[0]);
896 static int atmel_sha_final_req(struct atmel_sha_dev *dd)
898 struct ahash_request *req = dd->req;
899 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
903 if (ctx->bufcnt >= ATMEL_SHA_DMA_THRESHOLD) {
904 atmel_sha_fill_padding(ctx, 0);
907 err = atmel_sha_xmit_dma_map(dd, ctx, count, 1);
909 /* faster to handle last block with cpu */
911 atmel_sha_fill_padding(ctx, 0);
914 err = atmel_sha_xmit_cpu(dd, ctx->buffer, count, 1);
917 dev_dbg(dd->dev, "final_req: err: %d\n", err);
922 static void atmel_sha_copy_hash(struct ahash_request *req)
924 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
925 u32 *hash = (u32 *)ctx->digest;
926 unsigned int i, hashsize;
928 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
930 hashsize = SHA1_DIGEST_SIZE;
933 case SHA_FLAGS_SHA224:
934 case SHA_FLAGS_SHA256:
935 hashsize = SHA256_DIGEST_SIZE;
938 case SHA_FLAGS_SHA384:
939 case SHA_FLAGS_SHA512:
940 hashsize = SHA512_DIGEST_SIZE;
944 /* Should not happen... */
948 for (i = 0; i < hashsize / sizeof(u32); ++i)
949 hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i));
950 ctx->flags |= SHA_FLAGS_RESTORE;
953 static void atmel_sha_copy_ready_hash(struct ahash_request *req)
955 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
960 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
963 memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE);
966 case SHA_FLAGS_SHA224:
967 memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE);
970 case SHA_FLAGS_SHA256:
971 memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE);
974 case SHA_FLAGS_SHA384:
975 memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE);
978 case SHA_FLAGS_SHA512:
979 memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE);
984 static int atmel_sha_finish(struct ahash_request *req)
986 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
987 struct atmel_sha_dev *dd = ctx->dd;
989 if (ctx->digcnt[0] || ctx->digcnt[1])
990 atmel_sha_copy_ready_hash(req);
992 dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %zd\n", ctx->digcnt[1],
993 ctx->digcnt[0], ctx->bufcnt);
998 static void atmel_sha_finish_req(struct ahash_request *req, int err)
1000 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1001 struct atmel_sha_dev *dd = ctx->dd;
1004 atmel_sha_copy_hash(req);
1005 if (SHA_FLAGS_FINAL & dd->flags)
1006 err = atmel_sha_finish(req);
1008 ctx->flags |= SHA_FLAGS_ERROR;
1011 /* atomic operation is not needed here */
1012 (void)atmel_sha_complete(dd, err);
1015 static int atmel_sha_hw_init(struct atmel_sha_dev *dd)
1019 err = clk_enable(dd->iclk);
1023 if (!(SHA_FLAGS_INIT & dd->flags)) {
1024 atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST);
1025 dd->flags |= SHA_FLAGS_INIT;
1031 static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd)
1033 return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff;
1036 static int atmel_sha_hw_version_init(struct atmel_sha_dev *dd)
1040 err = atmel_sha_hw_init(dd);
1044 dd->hw_version = atmel_sha_get_version(dd);
1047 "version: 0x%x\n", dd->hw_version);
1049 clk_disable(dd->iclk);
1054 static int atmel_sha_handle_queue(struct atmel_sha_dev *dd,
1055 struct ahash_request *req)
1057 struct crypto_async_request *async_req, *backlog;
1058 struct atmel_sha_ctx *ctx;
1059 unsigned long flags;
1061 int err = 0, ret = 0;
1063 spin_lock_irqsave(&dd->lock, flags);
1065 ret = ahash_enqueue_request(&dd->queue, req);
1067 if (SHA_FLAGS_BUSY & dd->flags) {
1068 spin_unlock_irqrestore(&dd->lock, flags);
1072 backlog = crypto_get_backlog(&dd->queue);
1073 async_req = crypto_dequeue_request(&dd->queue);
1075 dd->flags |= SHA_FLAGS_BUSY;
1077 spin_unlock_irqrestore(&dd->lock, flags);
1083 crypto_request_complete(backlog, -EINPROGRESS);
1085 ctx = crypto_tfm_ctx(async_req->tfm);
1087 dd->req = ahash_request_cast(async_req);
1088 start_async = (dd->req != req);
1089 dd->is_async = start_async;
1090 dd->force_complete = false;
1092 /* WARNING: ctx->start() MAY change dd->is_async. */
1093 err = ctx->start(dd);
1094 return (start_async) ? ret : err;
1097 static int atmel_sha_done(struct atmel_sha_dev *dd);
1099 static int atmel_sha_start(struct atmel_sha_dev *dd)
1101 struct ahash_request *req = dd->req;
1102 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1105 dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %u\n",
1106 ctx->op, req->nbytes);
1108 err = atmel_sha_hw_init(dd);
1110 return atmel_sha_complete(dd, err);
1113 * atmel_sha_update_req() and atmel_sha_final_req() can return either:
1114 * -EINPROGRESS: the hardware is busy and the SHA driver will resume
1115 * its job later in the done_task.
1116 * This is the main path.
1118 * 0: the SHA driver can continue its job then release the hardware
1119 * later, if needed, with atmel_sha_finish_req().
1120 * This is the alternate path.
1122 * < 0: an error has occurred so atmel_sha_complete(dd, err) has already
1123 * been called, hence the hardware has been released.
1124 * The SHA driver must stop its job without calling
1125 * atmel_sha_finish_req(), otherwise atmel_sha_complete() would be
1126 * called a second time.
1128 * Please note that currently, atmel_sha_final_req() never returns 0.
1131 dd->resume = atmel_sha_done;
1132 if (ctx->op == SHA_OP_UPDATE) {
1133 err = atmel_sha_update_req(dd);
1134 if (!err && (ctx->flags & SHA_FLAGS_FINUP))
1135 /* no final() after finup() */
1136 err = atmel_sha_final_req(dd);
1137 } else if (ctx->op == SHA_OP_FINAL) {
1138 err = atmel_sha_final_req(dd);
1142 /* done_task will not finish it, so do it here */
1143 atmel_sha_finish_req(req, err);
1145 dev_dbg(dd->dev, "exit, err: %d\n", err);
1150 static int atmel_sha_enqueue(struct ahash_request *req, unsigned int op)
1152 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1153 struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1154 struct atmel_sha_dev *dd = tctx->dd;
1158 return atmel_sha_handle_queue(dd, req);
1161 static int atmel_sha_update(struct ahash_request *req)
1163 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1168 ctx->total = req->nbytes;
1172 if (ctx->flags & SHA_FLAGS_FINUP) {
1173 if (ctx->bufcnt + ctx->total < ATMEL_SHA_DMA_THRESHOLD)
1174 /* faster to use CPU for short transfers */
1175 ctx->flags |= SHA_FLAGS_CPU;
1176 } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
1177 atmel_sha_append_sg(ctx);
1180 return atmel_sha_enqueue(req, SHA_OP_UPDATE);
1183 static int atmel_sha_final(struct ahash_request *req)
1185 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1187 ctx->flags |= SHA_FLAGS_FINUP;
1189 if (ctx->flags & SHA_FLAGS_ERROR)
1190 return 0; /* uncompleted hash is not needed */
1192 if (ctx->flags & SHA_FLAGS_PAD)
1193 /* copy ready hash (+ finalize hmac) */
1194 return atmel_sha_finish(req);
1196 return atmel_sha_enqueue(req, SHA_OP_FINAL);
1199 static int atmel_sha_finup(struct ahash_request *req)
1201 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1204 ctx->flags |= SHA_FLAGS_FINUP;
1206 err1 = atmel_sha_update(req);
1207 if (err1 == -EINPROGRESS ||
1208 (err1 == -EBUSY && (ahash_request_flags(req) &
1209 CRYPTO_TFM_REQ_MAY_BACKLOG)))
1213 * final() has to be always called to cleanup resources
1214 * even if udpate() failed, except EINPROGRESS
1216 err2 = atmel_sha_final(req);
1218 return err1 ?: err2;
1221 static int atmel_sha_digest(struct ahash_request *req)
1223 return atmel_sha_init(req) ?: atmel_sha_finup(req);
1227 static int atmel_sha_export(struct ahash_request *req, void *out)
1229 const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1231 memcpy(out, ctx, sizeof(*ctx));
1235 static int atmel_sha_import(struct ahash_request *req, const void *in)
1237 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1239 memcpy(ctx, in, sizeof(*ctx));
1243 static int atmel_sha_cra_init(struct crypto_tfm *tfm)
1245 struct atmel_sha_ctx *ctx = crypto_tfm_ctx(tfm);
1247 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1248 sizeof(struct atmel_sha_reqctx));
1249 ctx->start = atmel_sha_start;
1254 static void atmel_sha_alg_init(struct ahash_alg *alg)
1256 alg->halg.base.cra_priority = ATMEL_SHA_PRIORITY;
1257 alg->halg.base.cra_flags = CRYPTO_ALG_ASYNC;
1258 alg->halg.base.cra_ctxsize = sizeof(struct atmel_sha_ctx);
1259 alg->halg.base.cra_module = THIS_MODULE;
1260 alg->halg.base.cra_init = atmel_sha_cra_init;
1262 alg->halg.statesize = sizeof(struct atmel_sha_reqctx);
1264 alg->init = atmel_sha_init;
1265 alg->update = atmel_sha_update;
1266 alg->final = atmel_sha_final;
1267 alg->finup = atmel_sha_finup;
1268 alg->digest = atmel_sha_digest;
1269 alg->export = atmel_sha_export;
1270 alg->import = atmel_sha_import;
1273 static struct ahash_alg sha_1_256_algs[] = {
1275 .halg.base.cra_name = "sha1",
1276 .halg.base.cra_driver_name = "atmel-sha1",
1277 .halg.base.cra_blocksize = SHA1_BLOCK_SIZE,
1279 .halg.digestsize = SHA1_DIGEST_SIZE,
1282 .halg.base.cra_name = "sha256",
1283 .halg.base.cra_driver_name = "atmel-sha256",
1284 .halg.base.cra_blocksize = SHA256_BLOCK_SIZE,
1286 .halg.digestsize = SHA256_DIGEST_SIZE,
1290 static struct ahash_alg sha_224_alg = {
1291 .halg.base.cra_name = "sha224",
1292 .halg.base.cra_driver_name = "atmel-sha224",
1293 .halg.base.cra_blocksize = SHA224_BLOCK_SIZE,
1295 .halg.digestsize = SHA224_DIGEST_SIZE,
1298 static struct ahash_alg sha_384_512_algs[] = {
1300 .halg.base.cra_name = "sha384",
1301 .halg.base.cra_driver_name = "atmel-sha384",
1302 .halg.base.cra_blocksize = SHA384_BLOCK_SIZE,
1303 .halg.base.cra_alignmask = 0x3,
1305 .halg.digestsize = SHA384_DIGEST_SIZE,
1308 .halg.base.cra_name = "sha512",
1309 .halg.base.cra_driver_name = "atmel-sha512",
1310 .halg.base.cra_blocksize = SHA512_BLOCK_SIZE,
1311 .halg.base.cra_alignmask = 0x3,
1313 .halg.digestsize = SHA512_DIGEST_SIZE,
1317 static void atmel_sha_queue_task(unsigned long data)
1319 struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
1321 atmel_sha_handle_queue(dd, NULL);
1324 static int atmel_sha_done(struct atmel_sha_dev *dd)
1328 if (SHA_FLAGS_CPU & dd->flags) {
1329 if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
1330 dd->flags &= ~SHA_FLAGS_OUTPUT_READY;
1333 } else if (SHA_FLAGS_DMA_READY & dd->flags) {
1334 if (SHA_FLAGS_DMA_ACTIVE & dd->flags) {
1335 dd->flags &= ~SHA_FLAGS_DMA_ACTIVE;
1336 atmel_sha_update_dma_stop(dd);
1338 if (SHA_FLAGS_OUTPUT_READY & dd->flags) {
1339 /* hash or semi-hash ready */
1340 dd->flags &= ~(SHA_FLAGS_DMA_READY |
1341 SHA_FLAGS_OUTPUT_READY);
1342 err = atmel_sha_update_dma_start(dd);
1343 if (err != -EINPROGRESS)
1350 /* finish curent request */
1351 atmel_sha_finish_req(dd->req, err);
1356 static void atmel_sha_done_task(unsigned long data)
1358 struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data;
1360 dd->is_async = true;
1361 (void)dd->resume(dd);
1364 static irqreturn_t atmel_sha_irq(int irq, void *dev_id)
1366 struct atmel_sha_dev *sha_dd = dev_id;
1369 reg = atmel_sha_read(sha_dd, SHA_ISR);
1370 if (reg & atmel_sha_read(sha_dd, SHA_IMR)) {
1371 atmel_sha_write(sha_dd, SHA_IDR, reg);
1372 if (SHA_FLAGS_BUSY & sha_dd->flags) {
1373 sha_dd->flags |= SHA_FLAGS_OUTPUT_READY;
1374 if (!(SHA_FLAGS_CPU & sha_dd->flags))
1375 sha_dd->flags |= SHA_FLAGS_DMA_READY;
1376 tasklet_schedule(&sha_dd->done_task);
1378 dev_warn(sha_dd->dev, "SHA interrupt when no active requests.\n");
1387 /* DMA transfer functions */
1389 static bool atmel_sha_dma_check_aligned(struct atmel_sha_dev *dd,
1390 struct scatterlist *sg,
1393 struct atmel_sha_dma *dma = &dd->dma_lch_in;
1394 struct ahash_request *req = dd->req;
1395 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1396 size_t bs = ctx->block_size;
1399 for (nents = 0; sg; sg = sg_next(sg), ++nents) {
1400 if (!IS_ALIGNED(sg->offset, sizeof(u32)))
1404 * This is the last sg, the only one that is allowed to
1405 * have an unaligned length.
1407 if (len <= sg->length) {
1408 dma->nents = nents + 1;
1409 dma->last_sg_length = sg->length;
1410 sg->length = ALIGN(len, sizeof(u32));
1414 /* All other sg lengths MUST be aligned to the block size. */
1415 if (!IS_ALIGNED(sg->length, bs))
1424 static void atmel_sha_dma_callback2(void *data)
1426 struct atmel_sha_dev *dd = data;
1427 struct atmel_sha_dma *dma = &dd->dma_lch_in;
1428 struct scatterlist *sg;
1431 dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
1434 for (nents = 0; nents < dma->nents - 1; ++nents)
1436 sg->length = dma->last_sg_length;
1438 dd->is_async = true;
1439 (void)atmel_sha_wait_for_data_ready(dd, dd->resume);
1442 static int atmel_sha_dma_start(struct atmel_sha_dev *dd,
1443 struct scatterlist *src,
1445 atmel_sha_fn_t resume)
1447 struct atmel_sha_dma *dma = &dd->dma_lch_in;
1448 struct dma_slave_config *config = &dma->dma_conf;
1449 struct dma_chan *chan = dma->chan;
1450 struct dma_async_tx_descriptor *desc;
1451 dma_cookie_t cookie;
1452 unsigned int sg_len;
1455 dd->resume = resume;
1458 * dma->nents has already been initialized by
1459 * atmel_sha_dma_check_aligned().
1462 sg_len = dma_map_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
1468 config->src_maxburst = 16;
1469 config->dst_maxburst = 16;
1470 err = dmaengine_slave_config(chan, config);
1474 desc = dmaengine_prep_slave_sg(chan, dma->sg, sg_len, DMA_MEM_TO_DEV,
1475 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1481 desc->callback = atmel_sha_dma_callback2;
1482 desc->callback_param = dd;
1483 cookie = dmaengine_submit(desc);
1484 err = dma_submit_error(cookie);
1488 dma_async_issue_pending(chan);
1490 return -EINPROGRESS;
1493 dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE);
1495 return atmel_sha_complete(dd, err);
1499 /* CPU transfer functions */
1501 static int atmel_sha_cpu_transfer(struct atmel_sha_dev *dd)
1503 struct ahash_request *req = dd->req;
1504 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1505 const u32 *words = (const u32 *)ctx->buffer;
1506 size_t i, num_words;
1507 u32 isr, din, din_inc;
1509 din_inc = (ctx->flags & SHA_FLAGS_IDATAR0) ? 0 : 1;
1511 /* Write data into the Input Data Registers. */
1512 num_words = DIV_ROUND_UP(ctx->bufcnt, sizeof(u32));
1513 for (i = 0, din = 0; i < num_words; ++i, din += din_inc)
1514 atmel_sha_write(dd, SHA_REG_DIN(din), words[i]);
1516 ctx->offset += ctx->bufcnt;
1517 ctx->total -= ctx->bufcnt;
1523 * Prepare next block:
1524 * Fill ctx->buffer now with the next data to be written into
1525 * IDATARx: it gives time for the SHA hardware to process
1526 * the current data so the SHA_INT_DATARDY flag might be set
1527 * in SHA_ISR when polling this register at the beginning of
1530 ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
1531 scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
1532 ctx->offset, ctx->bufcnt, 0);
1534 /* Wait for hardware to be ready again. */
1535 isr = atmel_sha_read(dd, SHA_ISR);
1536 if (!(isr & SHA_INT_DATARDY)) {
1537 /* Not ready yet. */
1538 dd->resume = atmel_sha_cpu_transfer;
1539 atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY);
1540 return -EINPROGRESS;
1544 if (unlikely(!(ctx->flags & SHA_FLAGS_WAIT_DATARDY)))
1545 return dd->cpu_transfer_complete(dd);
1547 return atmel_sha_wait_for_data_ready(dd, dd->cpu_transfer_complete);
1550 static int atmel_sha_cpu_start(struct atmel_sha_dev *dd,
1551 struct scatterlist *sg,
1554 bool wait_data_ready,
1555 atmel_sha_fn_t resume)
1557 struct ahash_request *req = dd->req;
1558 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1563 ctx->flags &= ~(SHA_FLAGS_IDATAR0 | SHA_FLAGS_WAIT_DATARDY);
1566 ctx->flags |= SHA_FLAGS_IDATAR0;
1568 if (wait_data_ready)
1569 ctx->flags |= SHA_FLAGS_WAIT_DATARDY;
1575 /* Prepare the first block to be written. */
1576 ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total);
1577 scatterwalk_map_and_copy(ctx->buffer, ctx->sg,
1578 ctx->offset, ctx->bufcnt, 0);
1580 dd->cpu_transfer_complete = resume;
1581 return atmel_sha_cpu_transfer(dd);
1584 static int atmel_sha_cpu_hash(struct atmel_sha_dev *dd,
1585 const void *data, unsigned int datalen,
1587 atmel_sha_fn_t resume)
1589 struct ahash_request *req = dd->req;
1590 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1591 u32 msglen = (auto_padding) ? datalen : 0;
1592 u32 mr = SHA_MR_MODE_AUTO;
1594 if (!(IS_ALIGNED(datalen, ctx->block_size) || auto_padding))
1595 return atmel_sha_complete(dd, -EINVAL);
1597 mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
1598 atmel_sha_write(dd, SHA_MR, mr);
1599 atmel_sha_write(dd, SHA_MSR, msglen);
1600 atmel_sha_write(dd, SHA_BCR, msglen);
1601 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
1603 sg_init_one(&dd->tmp, data, datalen);
1604 return atmel_sha_cpu_start(dd, &dd->tmp, datalen, false, true, resume);
1608 /* hmac functions */
1610 struct atmel_sha_hmac_key {
1612 unsigned int keylen;
1613 u8 buffer[SHA512_BLOCK_SIZE];
1617 static inline void atmel_sha_hmac_key_init(struct atmel_sha_hmac_key *hkey)
1619 memset(hkey, 0, sizeof(*hkey));
1622 static inline void atmel_sha_hmac_key_release(struct atmel_sha_hmac_key *hkey)
1624 kfree(hkey->keydup);
1625 memset(hkey, 0, sizeof(*hkey));
1628 static inline int atmel_sha_hmac_key_set(struct atmel_sha_hmac_key *hkey,
1630 unsigned int keylen)
1632 atmel_sha_hmac_key_release(hkey);
1634 if (keylen > sizeof(hkey->buffer)) {
1635 hkey->keydup = kmemdup(key, keylen, GFP_KERNEL);
1640 memcpy(hkey->buffer, key, keylen);
1644 hkey->keylen = keylen;
1648 static inline bool atmel_sha_hmac_key_get(const struct atmel_sha_hmac_key *hkey,
1650 unsigned int *keylen)
1655 *keylen = hkey->keylen;
1656 *key = (hkey->keydup) ? hkey->keydup : hkey->buffer;
1661 struct atmel_sha_hmac_ctx {
1662 struct atmel_sha_ctx base;
1664 struct atmel_sha_hmac_key hkey;
1665 u32 ipad[SHA512_BLOCK_SIZE / sizeof(u32)];
1666 u32 opad[SHA512_BLOCK_SIZE / sizeof(u32)];
1667 atmel_sha_fn_t resume;
1670 static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
1671 atmel_sha_fn_t resume);
1672 static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
1673 const u8 *key, unsigned int keylen);
1674 static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd);
1675 static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd);
1676 static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd);
1677 static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd);
1679 static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd);
1680 static int atmel_sha_hmac_final(struct atmel_sha_dev *dd);
1681 static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd);
1682 static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd);
1684 static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd,
1685 atmel_sha_fn_t resume)
1687 struct ahash_request *req = dd->req;
1688 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1689 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1690 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1691 unsigned int keylen;
1695 hmac->resume = resume;
1696 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
1697 case SHA_FLAGS_SHA1:
1698 ctx->block_size = SHA1_BLOCK_SIZE;
1699 ctx->hash_size = SHA1_DIGEST_SIZE;
1702 case SHA_FLAGS_SHA224:
1703 ctx->block_size = SHA224_BLOCK_SIZE;
1704 ctx->hash_size = SHA256_DIGEST_SIZE;
1707 case SHA_FLAGS_SHA256:
1708 ctx->block_size = SHA256_BLOCK_SIZE;
1709 ctx->hash_size = SHA256_DIGEST_SIZE;
1712 case SHA_FLAGS_SHA384:
1713 ctx->block_size = SHA384_BLOCK_SIZE;
1714 ctx->hash_size = SHA512_DIGEST_SIZE;
1717 case SHA_FLAGS_SHA512:
1718 ctx->block_size = SHA512_BLOCK_SIZE;
1719 ctx->hash_size = SHA512_DIGEST_SIZE;
1723 return atmel_sha_complete(dd, -EINVAL);
1725 bs = ctx->block_size;
1727 if (likely(!atmel_sha_hmac_key_get(&hmac->hkey, &key, &keylen)))
1730 /* Compute K' from K. */
1731 if (unlikely(keylen > bs))
1732 return atmel_sha_hmac_prehash_key(dd, key, keylen);
1735 memcpy((u8 *)hmac->ipad, key, keylen);
1736 memset((u8 *)hmac->ipad + keylen, 0, bs - keylen);
1737 return atmel_sha_hmac_compute_ipad_hash(dd);
1740 static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd,
1741 const u8 *key, unsigned int keylen)
1743 return atmel_sha_cpu_hash(dd, key, keylen, true,
1744 atmel_sha_hmac_prehash_key_done);
1747 static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd)
1749 struct ahash_request *req = dd->req;
1750 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1751 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1752 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1753 size_t ds = crypto_ahash_digestsize(tfm);
1754 size_t bs = ctx->block_size;
1755 size_t i, num_words = ds / sizeof(u32);
1758 for (i = 0; i < num_words; ++i)
1759 hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
1760 memset((u8 *)hmac->ipad + ds, 0, bs - ds);
1761 return atmel_sha_hmac_compute_ipad_hash(dd);
1764 static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd)
1766 struct ahash_request *req = dd->req;
1767 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1768 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1769 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1770 size_t bs = ctx->block_size;
1771 size_t i, num_words = bs / sizeof(u32);
1773 unsafe_memcpy(hmac->opad, hmac->ipad, bs,
1774 "fortified memcpy causes -Wrestrict warning");
1775 for (i = 0; i < num_words; ++i) {
1776 hmac->ipad[i] ^= 0x36363636;
1777 hmac->opad[i] ^= 0x5c5c5c5c;
1780 return atmel_sha_cpu_hash(dd, hmac->ipad, bs, false,
1781 atmel_sha_hmac_compute_opad_hash);
1784 static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd)
1786 struct ahash_request *req = dd->req;
1787 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1788 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1789 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1790 size_t bs = ctx->block_size;
1791 size_t hs = ctx->hash_size;
1792 size_t i, num_words = hs / sizeof(u32);
1794 for (i = 0; i < num_words; ++i)
1795 hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
1796 return atmel_sha_cpu_hash(dd, hmac->opad, bs, false,
1797 atmel_sha_hmac_setup_done);
1800 static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd)
1802 struct ahash_request *req = dd->req;
1803 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1804 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1805 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1806 size_t hs = ctx->hash_size;
1807 size_t i, num_words = hs / sizeof(u32);
1809 for (i = 0; i < num_words; ++i)
1810 hmac->opad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
1811 atmel_sha_hmac_key_release(&hmac->hkey);
1812 return hmac->resume(dd);
1815 static int atmel_sha_hmac_start(struct atmel_sha_dev *dd)
1817 struct ahash_request *req = dd->req;
1818 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1821 err = atmel_sha_hw_init(dd);
1823 return atmel_sha_complete(dd, err);
1827 err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_init_done);
1831 dd->resume = atmel_sha_done;
1832 err = atmel_sha_update_req(dd);
1836 dd->resume = atmel_sha_hmac_final;
1837 err = atmel_sha_final_req(dd);
1841 err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_digest2);
1845 return atmel_sha_complete(dd, -EINVAL);
1851 static int atmel_sha_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
1852 unsigned int keylen)
1854 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1856 return atmel_sha_hmac_key_set(&hmac->hkey, key, keylen);
1859 static int atmel_sha_hmac_init(struct ahash_request *req)
1863 err = atmel_sha_init(req);
1867 return atmel_sha_enqueue(req, SHA_OP_INIT);
1870 static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd)
1872 struct ahash_request *req = dd->req;
1873 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1874 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1875 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1876 size_t bs = ctx->block_size;
1877 size_t hs = ctx->hash_size;
1880 ctx->digcnt[0] = bs;
1882 ctx->flags |= SHA_FLAGS_RESTORE;
1883 memcpy(ctx->digest, hmac->ipad, hs);
1884 return atmel_sha_complete(dd, 0);
1887 static int atmel_sha_hmac_final(struct atmel_sha_dev *dd)
1889 struct ahash_request *req = dd->req;
1890 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1891 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1892 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1893 u32 *digest = (u32 *)ctx->digest;
1894 size_t ds = crypto_ahash_digestsize(tfm);
1895 size_t bs = ctx->block_size;
1896 size_t hs = ctx->hash_size;
1897 size_t i, num_words;
1900 /* Save d = SHA((K' + ipad) | msg). */
1901 num_words = ds / sizeof(u32);
1902 for (i = 0; i < num_words; ++i)
1903 digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
1905 /* Restore context to finish computing SHA((K' + opad) | d). */
1906 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
1907 num_words = hs / sizeof(u32);
1908 for (i = 0; i < num_words; ++i)
1909 atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
1911 mr = SHA_MR_MODE_AUTO | SHA_MR_UIHV;
1912 mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK);
1913 atmel_sha_write(dd, SHA_MR, mr);
1914 atmel_sha_write(dd, SHA_MSR, bs + ds);
1915 atmel_sha_write(dd, SHA_BCR, ds);
1916 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
1918 sg_init_one(&dd->tmp, digest, ds);
1919 return atmel_sha_cpu_start(dd, &dd->tmp, ds, false, true,
1920 atmel_sha_hmac_final_done);
1923 static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd)
1926 * req->result might not be sizeof(u32) aligned, so copy the
1927 * digest into ctx->digest[] before memcpy() the data into
1930 atmel_sha_copy_hash(dd->req);
1931 atmel_sha_copy_ready_hash(dd->req);
1932 return atmel_sha_complete(dd, 0);
1935 static int atmel_sha_hmac_digest(struct ahash_request *req)
1939 err = atmel_sha_init(req);
1943 return atmel_sha_enqueue(req, SHA_OP_DIGEST);
1946 static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd)
1948 struct ahash_request *req = dd->req;
1949 struct atmel_sha_reqctx *ctx = ahash_request_ctx(req);
1950 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1951 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
1952 struct scatterlist *sgbuf;
1953 size_t hs = ctx->hash_size;
1954 size_t i, num_words = hs / sizeof(u32);
1955 bool use_dma = false;
1958 /* Special case for empty message. */
1964 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
1965 case SHA_FLAGS_SHA1:
1966 case SHA_FLAGS_SHA224:
1967 case SHA_FLAGS_SHA256:
1968 atmel_sha_fill_padding(ctx, 64);
1971 case SHA_FLAGS_SHA384:
1972 case SHA_FLAGS_SHA512:
1973 atmel_sha_fill_padding(ctx, 128);
1976 sg_init_one(&dd->tmp, ctx->buffer, ctx->bufcnt);
1979 /* Check DMA threshold and alignment. */
1980 if (req->nbytes > ATMEL_SHA_DMA_THRESHOLD &&
1981 atmel_sha_dma_check_aligned(dd, req->src, req->nbytes))
1984 /* Write both initial hash values to compute a HMAC. */
1985 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
1986 for (i = 0; i < num_words; ++i)
1987 atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
1989 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
1990 for (i = 0; i < num_words; ++i)
1991 atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
1993 /* Write the Mode, Message Size, Bytes Count then Control Registers. */
1994 mr = (SHA_MR_HMAC | SHA_MR_DUALBUFF);
1995 mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
1997 mr |= SHA_MR_MODE_IDATAR0;
1999 mr |= SHA_MR_MODE_AUTO;
2000 atmel_sha_write(dd, SHA_MR, mr);
2002 atmel_sha_write(dd, SHA_MSR, req->nbytes);
2003 atmel_sha_write(dd, SHA_BCR, req->nbytes);
2005 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
2007 /* Special case for empty message. */
2010 req->nbytes = ctx->bufcnt;
2017 return atmel_sha_dma_start(dd, sgbuf, req->nbytes,
2018 atmel_sha_hmac_final_done);
2020 return atmel_sha_cpu_start(dd, sgbuf, req->nbytes, false, true,
2021 atmel_sha_hmac_final_done);
2024 static int atmel_sha_hmac_cra_init(struct crypto_tfm *tfm)
2026 struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
2028 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2029 sizeof(struct atmel_sha_reqctx));
2030 hmac->base.start = atmel_sha_hmac_start;
2031 atmel_sha_hmac_key_init(&hmac->hkey);
2036 static void atmel_sha_hmac_cra_exit(struct crypto_tfm *tfm)
2038 struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm);
2040 atmel_sha_hmac_key_release(&hmac->hkey);
2043 static void atmel_sha_hmac_alg_init(struct ahash_alg *alg)
2045 alg->halg.base.cra_priority = ATMEL_SHA_PRIORITY;
2046 alg->halg.base.cra_flags = CRYPTO_ALG_ASYNC;
2047 alg->halg.base.cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx);
2048 alg->halg.base.cra_module = THIS_MODULE;
2049 alg->halg.base.cra_init = atmel_sha_hmac_cra_init;
2050 alg->halg.base.cra_exit = atmel_sha_hmac_cra_exit;
2052 alg->halg.statesize = sizeof(struct atmel_sha_reqctx);
2054 alg->init = atmel_sha_hmac_init;
2055 alg->update = atmel_sha_update;
2056 alg->final = atmel_sha_final;
2057 alg->digest = atmel_sha_hmac_digest;
2058 alg->setkey = atmel_sha_hmac_setkey;
2059 alg->export = atmel_sha_export;
2060 alg->import = atmel_sha_import;
2063 static struct ahash_alg sha_hmac_algs[] = {
2065 .halg.base.cra_name = "hmac(sha1)",
2066 .halg.base.cra_driver_name = "atmel-hmac-sha1",
2067 .halg.base.cra_blocksize = SHA1_BLOCK_SIZE,
2069 .halg.digestsize = SHA1_DIGEST_SIZE,
2072 .halg.base.cra_name = "hmac(sha224)",
2073 .halg.base.cra_driver_name = "atmel-hmac-sha224",
2074 .halg.base.cra_blocksize = SHA224_BLOCK_SIZE,
2076 .halg.digestsize = SHA224_DIGEST_SIZE,
2079 .halg.base.cra_name = "hmac(sha256)",
2080 .halg.base.cra_driver_name = "atmel-hmac-sha256",
2081 .halg.base.cra_blocksize = SHA256_BLOCK_SIZE,
2083 .halg.digestsize = SHA256_DIGEST_SIZE,
2086 .halg.base.cra_name = "hmac(sha384)",
2087 .halg.base.cra_driver_name = "atmel-hmac-sha384",
2088 .halg.base.cra_blocksize = SHA384_BLOCK_SIZE,
2090 .halg.digestsize = SHA384_DIGEST_SIZE,
2093 .halg.base.cra_name = "hmac(sha512)",
2094 .halg.base.cra_driver_name = "atmel-hmac-sha512",
2095 .halg.base.cra_blocksize = SHA512_BLOCK_SIZE,
2097 .halg.digestsize = SHA512_DIGEST_SIZE,
2101 #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
2102 /* authenc functions */
2104 static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd);
2105 static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd);
2106 static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd);
2109 struct atmel_sha_authenc_ctx {
2110 struct crypto_ahash *tfm;
2113 struct atmel_sha_authenc_reqctx {
2114 struct atmel_sha_reqctx base;
2116 atmel_aes_authenc_fn_t cb;
2117 struct atmel_aes_dev *aes_dev;
2119 /* _init() parameters. */
2120 struct scatterlist *assoc;
2124 /* _final() parameters. */
2126 unsigned int digestlen;
2129 static void atmel_sha_authenc_complete(void *data, int err)
2131 struct ahash_request *req = data;
2132 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2134 authctx->cb(authctx->aes_dev, err, authctx->base.dd->is_async);
2137 static int atmel_sha_authenc_start(struct atmel_sha_dev *dd)
2139 struct ahash_request *req = dd->req;
2140 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2144 * Force atmel_sha_complete() to call req->base.complete(), ie
2145 * atmel_sha_authenc_complete(), which in turn calls authctx->cb().
2147 dd->force_complete = true;
2149 err = atmel_sha_hw_init(dd);
2150 return authctx->cb(authctx->aes_dev, err, dd->is_async);
2153 bool atmel_sha_authenc_is_ready(void)
2155 struct atmel_sha_ctx dummy;
2158 return (atmel_sha_find_dev(&dummy) != NULL);
2160 EXPORT_SYMBOL_GPL(atmel_sha_authenc_is_ready);
2162 unsigned int atmel_sha_authenc_get_reqsize(void)
2164 return sizeof(struct atmel_sha_authenc_reqctx);
2166 EXPORT_SYMBOL_GPL(atmel_sha_authenc_get_reqsize);
2168 struct atmel_sha_authenc_ctx *atmel_sha_authenc_spawn(unsigned long mode)
2170 struct atmel_sha_authenc_ctx *auth;
2171 struct crypto_ahash *tfm;
2172 struct atmel_sha_ctx *tctx;
2176 switch (mode & SHA_FLAGS_MODE_MASK) {
2177 case SHA_FLAGS_HMAC_SHA1:
2178 name = "atmel-hmac-sha1";
2181 case SHA_FLAGS_HMAC_SHA224:
2182 name = "atmel-hmac-sha224";
2185 case SHA_FLAGS_HMAC_SHA256:
2186 name = "atmel-hmac-sha256";
2189 case SHA_FLAGS_HMAC_SHA384:
2190 name = "atmel-hmac-sha384";
2193 case SHA_FLAGS_HMAC_SHA512:
2194 name = "atmel-hmac-sha512";
2201 tfm = crypto_alloc_ahash(name, 0, 0);
2206 tctx = crypto_ahash_ctx(tfm);
2207 tctx->start = atmel_sha_authenc_start;
2210 auth = kzalloc(sizeof(*auth), GFP_KERNEL);
2213 goto err_free_ahash;
2220 crypto_free_ahash(tfm);
2222 return ERR_PTR(err);
2224 EXPORT_SYMBOL_GPL(atmel_sha_authenc_spawn);
2226 void atmel_sha_authenc_free(struct atmel_sha_authenc_ctx *auth)
2229 crypto_free_ahash(auth->tfm);
2232 EXPORT_SYMBOL_GPL(atmel_sha_authenc_free);
2234 int atmel_sha_authenc_setkey(struct atmel_sha_authenc_ctx *auth,
2235 const u8 *key, unsigned int keylen, u32 flags)
2237 struct crypto_ahash *tfm = auth->tfm;
2239 crypto_ahash_clear_flags(tfm, CRYPTO_TFM_REQ_MASK);
2240 crypto_ahash_set_flags(tfm, flags & CRYPTO_TFM_REQ_MASK);
2241 return crypto_ahash_setkey(tfm, key, keylen);
2243 EXPORT_SYMBOL_GPL(atmel_sha_authenc_setkey);
2245 int atmel_sha_authenc_schedule(struct ahash_request *req,
2246 struct atmel_sha_authenc_ctx *auth,
2247 atmel_aes_authenc_fn_t cb,
2248 struct atmel_aes_dev *aes_dev)
2250 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2251 struct atmel_sha_reqctx *ctx = &authctx->base;
2252 struct crypto_ahash *tfm = auth->tfm;
2253 struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm);
2254 struct atmel_sha_dev *dd;
2256 /* Reset request context (MUST be done first). */
2257 memset(authctx, 0, sizeof(*authctx));
2259 /* Get SHA device. */
2260 dd = atmel_sha_find_dev(tctx);
2262 return cb(aes_dev, -ENODEV, false);
2264 /* Init request context. */
2266 ctx->buflen = SHA_BUFFER_LEN;
2268 authctx->aes_dev = aes_dev;
2269 ahash_request_set_tfm(req, tfm);
2270 ahash_request_set_callback(req, 0, atmel_sha_authenc_complete, req);
2272 return atmel_sha_handle_queue(dd, req);
2274 EXPORT_SYMBOL_GPL(atmel_sha_authenc_schedule);
2276 int atmel_sha_authenc_init(struct ahash_request *req,
2277 struct scatterlist *assoc, unsigned int assoclen,
2278 unsigned int textlen,
2279 atmel_aes_authenc_fn_t cb,
2280 struct atmel_aes_dev *aes_dev)
2282 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2283 struct atmel_sha_reqctx *ctx = &authctx->base;
2284 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2285 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
2286 struct atmel_sha_dev *dd = ctx->dd;
2288 if (unlikely(!IS_ALIGNED(assoclen, sizeof(u32))))
2289 return atmel_sha_complete(dd, -EINVAL);
2292 authctx->aes_dev = aes_dev;
2293 authctx->assoc = assoc;
2294 authctx->assoclen = assoclen;
2295 authctx->textlen = textlen;
2297 ctx->flags = hmac->base.flags;
2298 return atmel_sha_hmac_setup(dd, atmel_sha_authenc_init2);
2300 EXPORT_SYMBOL_GPL(atmel_sha_authenc_init);
2302 static int atmel_sha_authenc_init2(struct atmel_sha_dev *dd)
2304 struct ahash_request *req = dd->req;
2305 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2306 struct atmel_sha_reqctx *ctx = &authctx->base;
2307 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2308 struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm);
2309 size_t hs = ctx->hash_size;
2310 size_t i, num_words = hs / sizeof(u32);
2313 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV);
2314 for (i = 0; i < num_words; ++i)
2315 atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]);
2317 atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV);
2318 for (i = 0; i < num_words; ++i)
2319 atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]);
2321 mr = (SHA_MR_MODE_IDATAR0 |
2324 mr |= ctx->flags & SHA_FLAGS_ALGO_MASK;
2325 atmel_sha_write(dd, SHA_MR, mr);
2327 msg_size = authctx->assoclen + authctx->textlen;
2328 atmel_sha_write(dd, SHA_MSR, msg_size);
2329 atmel_sha_write(dd, SHA_BCR, msg_size);
2331 atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST);
2333 /* Process assoc data. */
2334 return atmel_sha_cpu_start(dd, authctx->assoc, authctx->assoclen,
2336 atmel_sha_authenc_init_done);
2339 static int atmel_sha_authenc_init_done(struct atmel_sha_dev *dd)
2341 struct ahash_request *req = dd->req;
2342 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2344 return authctx->cb(authctx->aes_dev, 0, dd->is_async);
2347 int atmel_sha_authenc_final(struct ahash_request *req,
2348 u32 *digest, unsigned int digestlen,
2349 atmel_aes_authenc_fn_t cb,
2350 struct atmel_aes_dev *aes_dev)
2352 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2353 struct atmel_sha_reqctx *ctx = &authctx->base;
2354 struct atmel_sha_dev *dd = ctx->dd;
2356 switch (ctx->flags & SHA_FLAGS_ALGO_MASK) {
2357 case SHA_FLAGS_SHA1:
2358 authctx->digestlen = SHA1_DIGEST_SIZE;
2361 case SHA_FLAGS_SHA224:
2362 authctx->digestlen = SHA224_DIGEST_SIZE;
2365 case SHA_FLAGS_SHA256:
2366 authctx->digestlen = SHA256_DIGEST_SIZE;
2369 case SHA_FLAGS_SHA384:
2370 authctx->digestlen = SHA384_DIGEST_SIZE;
2373 case SHA_FLAGS_SHA512:
2374 authctx->digestlen = SHA512_DIGEST_SIZE;
2378 return atmel_sha_complete(dd, -EINVAL);
2380 if (authctx->digestlen > digestlen)
2381 authctx->digestlen = digestlen;
2384 authctx->aes_dev = aes_dev;
2385 authctx->digest = digest;
2386 return atmel_sha_wait_for_data_ready(dd,
2387 atmel_sha_authenc_final_done);
2389 EXPORT_SYMBOL_GPL(atmel_sha_authenc_final);
2391 static int atmel_sha_authenc_final_done(struct atmel_sha_dev *dd)
2393 struct ahash_request *req = dd->req;
2394 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2395 size_t i, num_words = authctx->digestlen / sizeof(u32);
2397 for (i = 0; i < num_words; ++i)
2398 authctx->digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i));
2400 return atmel_sha_complete(dd, 0);
2403 void atmel_sha_authenc_abort(struct ahash_request *req)
2405 struct atmel_sha_authenc_reqctx *authctx = ahash_request_ctx(req);
2406 struct atmel_sha_reqctx *ctx = &authctx->base;
2407 struct atmel_sha_dev *dd = ctx->dd;
2409 /* Prevent atmel_sha_complete() from calling req->base.complete(). */
2410 dd->is_async = false;
2411 dd->force_complete = false;
2412 (void)atmel_sha_complete(dd, 0);
2414 EXPORT_SYMBOL_GPL(atmel_sha_authenc_abort);
2416 #endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
2419 static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd)
2423 if (dd->caps.has_hmac)
2424 for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++)
2425 crypto_unregister_ahash(&sha_hmac_algs[i]);
2427 for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++)
2428 crypto_unregister_ahash(&sha_1_256_algs[i]);
2430 if (dd->caps.has_sha224)
2431 crypto_unregister_ahash(&sha_224_alg);
2433 if (dd->caps.has_sha_384_512) {
2434 for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++)
2435 crypto_unregister_ahash(&sha_384_512_algs[i]);
2439 static int atmel_sha_register_algs(struct atmel_sha_dev *dd)
2443 for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) {
2444 atmel_sha_alg_init(&sha_1_256_algs[i]);
2446 err = crypto_register_ahash(&sha_1_256_algs[i]);
2448 goto err_sha_1_256_algs;
2451 if (dd->caps.has_sha224) {
2452 atmel_sha_alg_init(&sha_224_alg);
2454 err = crypto_register_ahash(&sha_224_alg);
2456 goto err_sha_224_algs;
2459 if (dd->caps.has_sha_384_512) {
2460 for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) {
2461 atmel_sha_alg_init(&sha_384_512_algs[i]);
2463 err = crypto_register_ahash(&sha_384_512_algs[i]);
2465 goto err_sha_384_512_algs;
2469 if (dd->caps.has_hmac) {
2470 for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++) {
2471 atmel_sha_hmac_alg_init(&sha_hmac_algs[i]);
2473 err = crypto_register_ahash(&sha_hmac_algs[i]);
2475 goto err_sha_hmac_algs;
2481 /*i = ARRAY_SIZE(sha_hmac_algs);*/
2483 for (j = 0; j < i; j++)
2484 crypto_unregister_ahash(&sha_hmac_algs[j]);
2485 i = ARRAY_SIZE(sha_384_512_algs);
2486 err_sha_384_512_algs:
2487 for (j = 0; j < i; j++)
2488 crypto_unregister_ahash(&sha_384_512_algs[j]);
2489 crypto_unregister_ahash(&sha_224_alg);
2491 i = ARRAY_SIZE(sha_1_256_algs);
2493 for (j = 0; j < i; j++)
2494 crypto_unregister_ahash(&sha_1_256_algs[j]);
2499 static int atmel_sha_dma_init(struct atmel_sha_dev *dd)
2501 dd->dma_lch_in.chan = dma_request_chan(dd->dev, "tx");
2502 if (IS_ERR(dd->dma_lch_in.chan)) {
2503 return dev_err_probe(dd->dev, PTR_ERR(dd->dma_lch_in.chan),
2504 "DMA channel is not available\n");
2507 dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base +
2509 dd->dma_lch_in.dma_conf.src_maxburst = 1;
2510 dd->dma_lch_in.dma_conf.src_addr_width =
2511 DMA_SLAVE_BUSWIDTH_4_BYTES;
2512 dd->dma_lch_in.dma_conf.dst_maxburst = 1;
2513 dd->dma_lch_in.dma_conf.dst_addr_width =
2514 DMA_SLAVE_BUSWIDTH_4_BYTES;
2515 dd->dma_lch_in.dma_conf.device_fc = false;
2520 static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd)
2522 dma_release_channel(dd->dma_lch_in.chan);
2525 static void atmel_sha_get_cap(struct atmel_sha_dev *dd)
2528 dd->caps.has_dma = 0;
2529 dd->caps.has_dualbuff = 0;
2530 dd->caps.has_sha224 = 0;
2531 dd->caps.has_sha_384_512 = 0;
2532 dd->caps.has_uihv = 0;
2533 dd->caps.has_hmac = 0;
2535 /* keep only major version number */
2536 switch (dd->hw_version & 0xff0) {
2540 dd->caps.has_dma = 1;
2541 dd->caps.has_dualbuff = 1;
2542 dd->caps.has_sha224 = 1;
2543 dd->caps.has_sha_384_512 = 1;
2544 dd->caps.has_uihv = 1;
2545 dd->caps.has_hmac = 1;
2548 dd->caps.has_dma = 1;
2549 dd->caps.has_dualbuff = 1;
2550 dd->caps.has_sha224 = 1;
2551 dd->caps.has_sha_384_512 = 1;
2552 dd->caps.has_uihv = 1;
2555 dd->caps.has_dma = 1;
2556 dd->caps.has_dualbuff = 1;
2557 dd->caps.has_sha224 = 1;
2558 dd->caps.has_sha_384_512 = 1;
2561 dd->caps.has_dma = 1;
2562 dd->caps.has_dualbuff = 1;
2563 dd->caps.has_sha224 = 1;
2569 "Unmanaged sha version, set minimum capabilities\n");
2574 static const struct of_device_id atmel_sha_dt_ids[] = {
2575 { .compatible = "atmel,at91sam9g46-sha" },
2579 MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids);
2581 static int atmel_sha_probe(struct platform_device *pdev)
2583 struct atmel_sha_dev *sha_dd;
2584 struct device *dev = &pdev->dev;
2585 struct resource *sha_res;
2588 sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL);
2594 platform_set_drvdata(pdev, sha_dd);
2596 INIT_LIST_HEAD(&sha_dd->list);
2597 spin_lock_init(&sha_dd->lock);
2599 tasklet_init(&sha_dd->done_task, atmel_sha_done_task,
2600 (unsigned long)sha_dd);
2601 tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task,
2602 (unsigned long)sha_dd);
2604 crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH);
2606 sha_dd->io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &sha_res);
2607 if (IS_ERR(sha_dd->io_base)) {
2608 err = PTR_ERR(sha_dd->io_base);
2609 goto err_tasklet_kill;
2611 sha_dd->phys_base = sha_res->start;
2614 sha_dd->irq = platform_get_irq(pdev, 0);
2615 if (sha_dd->irq < 0) {
2617 goto err_tasklet_kill;
2620 err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq,
2621 IRQF_SHARED, "atmel-sha", sha_dd);
2623 dev_err(dev, "unable to request sha irq.\n");
2624 goto err_tasklet_kill;
2627 /* Initializing the clock */
2628 sha_dd->iclk = devm_clk_get(&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;
2635 err = clk_prepare(sha_dd->iclk);
2637 goto err_tasklet_kill;
2639 err = atmel_sha_hw_version_init(sha_dd);
2641 goto err_iclk_unprepare;
2643 atmel_sha_get_cap(sha_dd);
2645 if (sha_dd->caps.has_dma) {
2646 err = atmel_sha_dma_init(sha_dd);
2648 goto err_iclk_unprepare;
2650 dev_info(dev, "using %s for DMA transfers\n",
2651 dma_chan_name(sha_dd->dma_lch_in.chan));
2654 spin_lock(&atmel_sha.lock);
2655 list_add_tail(&sha_dd->list, &atmel_sha.dev_list);
2656 spin_unlock(&atmel_sha.lock);
2658 err = atmel_sha_register_algs(sha_dd);
2662 dev_info(dev, "Atmel SHA1/SHA256%s%s\n",
2663 sha_dd->caps.has_sha224 ? "/SHA224" : "",
2664 sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : "");
2669 spin_lock(&atmel_sha.lock);
2670 list_del(&sha_dd->list);
2671 spin_unlock(&atmel_sha.lock);
2672 if (sha_dd->caps.has_dma)
2673 atmel_sha_dma_cleanup(sha_dd);
2675 clk_unprepare(sha_dd->iclk);
2677 tasklet_kill(&sha_dd->queue_task);
2678 tasklet_kill(&sha_dd->done_task);
2683 static int atmel_sha_remove(struct platform_device *pdev)
2685 struct atmel_sha_dev *sha_dd = platform_get_drvdata(pdev);
2687 spin_lock(&atmel_sha.lock);
2688 list_del(&sha_dd->list);
2689 spin_unlock(&atmel_sha.lock);
2691 atmel_sha_unregister_algs(sha_dd);
2693 tasklet_kill(&sha_dd->queue_task);
2694 tasklet_kill(&sha_dd->done_task);
2696 if (sha_dd->caps.has_dma)
2697 atmel_sha_dma_cleanup(sha_dd);
2699 clk_unprepare(sha_dd->iclk);
2704 static struct platform_driver atmel_sha_driver = {
2705 .probe = atmel_sha_probe,
2706 .remove = atmel_sha_remove,
2708 .name = "atmel_sha",
2709 .of_match_table = atmel_sha_dt_ids,
2713 module_platform_driver(atmel_sha_driver);
2715 MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
2716 MODULE_LICENSE("GPL v2");
2717 MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");