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/init.h>
25 #include <linux/errno.h>
26 #include <linux/interrupt.h>
27 #include <linux/irq.h>
28 #include <linux/scatterlist.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/of_device.h>
31 #include <linux/delay.h>
32 #include <linux/crypto.h>
33 #include <linux/cryptohash.h>
34 #include <crypto/scatterwalk.h>
35 #include <crypto/algapi.h>
36 #include <crypto/sha.h>
37 #include <crypto/hash.h>
38 #include <crypto/internal/hash.h>
39 #include <linux/platform_data/crypto-atmel.h>
40 #include "atmel-sha-regs.h"
41 #include "atmel-authenc.h"
44 #define SHA_FLAGS_BUSY BIT(0)
45 #define SHA_FLAGS_FINAL BIT(1)
46 #define SHA_FLAGS_DMA_ACTIVE BIT(2)
47 #define SHA_FLAGS_OUTPUT_READY BIT(3)
48 #define SHA_FLAGS_INIT BIT(4)
49 #define SHA_FLAGS_CPU BIT(5)
50 #define SHA_FLAGS_DMA_READY BIT(6)
51 #define SHA_FLAGS_DUMP_REG BIT(7)
53 /* bits[11:8] are reserved. */
55 #define SHA_FLAGS_FINUP BIT(16)
56 #define SHA_FLAGS_SG BIT(17)
57 #define SHA_FLAGS_ERROR BIT(23)
58 #define SHA_FLAGS_PAD BIT(24)
59 #define SHA_FLAGS_RESTORE BIT(25)
60 #define SHA_FLAGS_IDATAR0 BIT(26)
61 #define SHA_FLAGS_WAIT_DATARDY BIT(27)
64 #define SHA_OP_UPDATE 1
65 #define SHA_OP_FINAL 2
66 #define SHA_OP_DIGEST 3
68 #define SHA_BUFFER_LEN (PAGE_SIZE / 16)
70 #define ATMEL_SHA_DMA_THRESHOLD 56
72 struct atmel_sha_caps
{
84 * .statesize = sizeof(struct atmel_sha_reqctx) must be <= PAGE_SIZE / 8 as
85 * tested by the ahash_prepare_alg() function.
87 struct atmel_sha_reqctx
{
88 struct atmel_sha_dev
*dd
;
92 u8 digest
[SHA512_DIGEST_SIZE
] __aligned(sizeof(u32
));
99 struct scatterlist
*sg
;
100 unsigned int offset
; /* offset in current sg */
101 unsigned int total
; /* total request */
106 u8 buffer
[SHA_BUFFER_LEN
+ SHA512_BLOCK_SIZE
] __aligned(sizeof(u32
));
109 typedef int (*atmel_sha_fn_t
)(struct atmel_sha_dev
*);
111 struct atmel_sha_ctx
{
112 struct atmel_sha_dev
*dd
;
113 atmel_sha_fn_t start
;
118 #define ATMEL_SHA_QUEUE_LENGTH 50
120 struct atmel_sha_dma
{
121 struct dma_chan
*chan
;
122 struct dma_slave_config dma_conf
;
123 struct scatterlist
*sg
;
125 unsigned int last_sg_length
;
128 struct atmel_sha_dev
{
129 struct list_head list
;
130 unsigned long phys_base
;
134 void __iomem
*io_base
;
138 struct tasklet_struct done_task
;
139 struct tasklet_struct queue_task
;
142 struct crypto_queue queue
;
143 struct ahash_request
*req
;
146 atmel_sha_fn_t resume
;
147 atmel_sha_fn_t cpu_transfer_complete
;
149 struct atmel_sha_dma dma_lch_in
;
151 struct atmel_sha_caps caps
;
153 struct scatterlist tmp
;
158 struct atmel_sha_drv
{
159 struct list_head dev_list
;
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
),
169 static const char *atmel_sha_reg_name(u32 offset
, char *tmp
, size_t sz
, bool wr
)
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);
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):
232 snprintf(tmp
, sz
, "IDATAR[%u]",
233 16u + ((offset
- SHA_REG_DIGEST(0)) >> 2));
235 snprintf(tmp
, sz
, "ODATAR[%u]",
236 (offset
- SHA_REG_DIGEST(0)) >> 2);
243 snprintf(tmp
, sz
, "0x%02x", offset
);
250 #endif /* VERBOSE_DEBUG */
252 static inline u32
atmel_sha_read(struct atmel_sha_dev
*dd
, u32 offset
)
254 u32 value
= readl_relaxed(dd
->io_base
+ offset
);
257 if (dd
->flags
& SHA_FLAGS_DUMP_REG
) {
260 dev_vdbg(dd
->dev
, "read 0x%08x from %s\n", value
,
261 atmel_sha_reg_name(offset
, tmp
, sizeof(tmp
), false));
263 #endif /* VERBOSE_DEBUG */
268 static inline void atmel_sha_write(struct atmel_sha_dev
*dd
,
269 u32 offset
, u32 value
)
272 if (dd
->flags
& SHA_FLAGS_DUMP_REG
) {
275 dev_vdbg(dd
->dev
, "write 0x%08x into %s\n", value
,
276 atmel_sha_reg_name(offset
, tmp
, sizeof(tmp
), true));
278 #endif /* VERBOSE_DEBUG */
280 writel_relaxed(value
, dd
->io_base
+ offset
);
283 static inline int atmel_sha_complete(struct atmel_sha_dev
*dd
, int err
)
285 struct ahash_request
*req
= dd
->req
;
287 dd
->flags
&= ~(SHA_FLAGS_BUSY
| SHA_FLAGS_FINAL
| SHA_FLAGS_CPU
|
288 SHA_FLAGS_DMA_READY
| SHA_FLAGS_OUTPUT_READY
|
291 clk_disable(dd
->iclk
);
293 if ((dd
->is_async
|| dd
->force_complete
) && req
->base
.complete
)
294 req
->base
.complete(&req
->base
, err
);
296 /* handle new request */
297 tasklet_schedule(&dd
->queue_task
);
302 static size_t atmel_sha_append_sg(struct atmel_sha_reqctx
*ctx
)
306 while ((ctx
->bufcnt
< ctx
->buflen
) && ctx
->total
) {
307 count
= min(ctx
->sg
->length
- ctx
->offset
, ctx
->total
);
308 count
= min(count
, ctx
->buflen
- ctx
->bufcnt
);
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.
317 if ((ctx
->sg
->length
== 0) && !sg_is_last(ctx
->sg
)) {
318 ctx
->sg
= sg_next(ctx
->sg
);
325 scatterwalk_map_and_copy(ctx
->buffer
+ ctx
->bufcnt
, ctx
->sg
,
326 ctx
->offset
, count
, 0);
328 ctx
->bufcnt
+= count
;
329 ctx
->offset
+= count
;
332 if (ctx
->offset
== ctx
->sg
->length
) {
333 ctx
->sg
= sg_next(ctx
->sg
);
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
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
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
360 static void atmel_sha_fill_padding(struct atmel_sha_reqctx
*ctx
, int length
)
362 unsigned int index
, padlen
;
366 size
[0] = ctx
->digcnt
[0];
367 size
[1] = ctx
->digcnt
[1];
369 size
[0] += ctx
->bufcnt
;
370 if (size
[0] < ctx
->bufcnt
)
374 if (size
[0] < length
)
377 bits
[1] = cpu_to_be64(size
[0] << 3);
378 bits
[0] = cpu_to_be64(size
[1] << 3 | size
[0] >> 61);
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
;
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
;
404 static struct atmel_sha_dev
*atmel_sha_find_dev(struct atmel_sha_ctx
*tctx
)
406 struct atmel_sha_dev
*dd
= NULL
;
407 struct atmel_sha_dev
*tmp
;
409 spin_lock_bh(&atmel_sha
.lock
);
411 list_for_each_entry(tmp
, &atmel_sha
.dev_list
, list
) {
420 spin_unlock_bh(&atmel_sha
.lock
);
425 static int atmel_sha_init(struct ahash_request
*req
)
427 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
428 struct atmel_sha_ctx
*tctx
= crypto_ahash_ctx(tfm
);
429 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
430 struct atmel_sha_dev
*dd
= atmel_sha_find_dev(tctx
);
436 dev_dbg(dd
->dev
, "init: digest size: %d\n",
437 crypto_ahash_digestsize(tfm
));
439 switch (crypto_ahash_digestsize(tfm
)) {
440 case SHA1_DIGEST_SIZE
:
441 ctx
->flags
|= SHA_FLAGS_SHA1
;
442 ctx
->block_size
= SHA1_BLOCK_SIZE
;
444 case SHA224_DIGEST_SIZE
:
445 ctx
->flags
|= SHA_FLAGS_SHA224
;
446 ctx
->block_size
= SHA224_BLOCK_SIZE
;
448 case SHA256_DIGEST_SIZE
:
449 ctx
->flags
|= SHA_FLAGS_SHA256
;
450 ctx
->block_size
= SHA256_BLOCK_SIZE
;
452 case SHA384_DIGEST_SIZE
:
453 ctx
->flags
|= SHA_FLAGS_SHA384
;
454 ctx
->block_size
= SHA384_BLOCK_SIZE
;
456 case SHA512_DIGEST_SIZE
:
457 ctx
->flags
|= SHA_FLAGS_SHA512
;
458 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 int 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
);
877 static int atmel_sha_update_req(struct atmel_sha_dev
*dd
)
879 struct ahash_request
*req
= dd
->req
;
880 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
883 dev_dbg(dd
->dev
, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n",
884 ctx
->total
, ctx
->digcnt
[1], ctx
->digcnt
[0]);
886 if (ctx
->flags
& SHA_FLAGS_CPU
)
887 err
= atmel_sha_update_cpu(dd
);
889 err
= atmel_sha_update_dma_start(dd
);
891 /* wait for dma completion before can take more data */
892 dev_dbg(dd
->dev
, "update: err: %d, digcnt: 0x%llx 0%llx\n",
893 err
, ctx
->digcnt
[1], ctx
->digcnt
[0]);
898 static int atmel_sha_final_req(struct atmel_sha_dev
*dd
)
900 struct ahash_request
*req
= dd
->req
;
901 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
905 if (ctx
->bufcnt
>= ATMEL_SHA_DMA_THRESHOLD
) {
906 atmel_sha_fill_padding(ctx
, 0);
909 err
= atmel_sha_xmit_dma_map(dd
, ctx
, count
, 1);
911 /* faster to handle last block with cpu */
913 atmel_sha_fill_padding(ctx
, 0);
916 err
= atmel_sha_xmit_cpu(dd
, ctx
->buffer
, count
, 1);
919 dev_dbg(dd
->dev
, "final_req: err: %d\n", err
);
924 static void atmel_sha_copy_hash(struct ahash_request
*req
)
926 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
927 u32
*hash
= (u32
*)ctx
->digest
;
928 unsigned int i
, hashsize
;
930 switch (ctx
->flags
& SHA_FLAGS_ALGO_MASK
) {
932 hashsize
= SHA1_DIGEST_SIZE
;
935 case SHA_FLAGS_SHA224
:
936 case SHA_FLAGS_SHA256
:
937 hashsize
= SHA256_DIGEST_SIZE
;
940 case SHA_FLAGS_SHA384
:
941 case SHA_FLAGS_SHA512
:
942 hashsize
= SHA512_DIGEST_SIZE
;
946 /* Should not happen... */
950 for (i
= 0; i
< hashsize
/ sizeof(u32
); ++i
)
951 hash
[i
] = atmel_sha_read(ctx
->dd
, SHA_REG_DIGEST(i
));
952 ctx
->flags
|= SHA_FLAGS_RESTORE
;
955 static void atmel_sha_copy_ready_hash(struct ahash_request
*req
)
957 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
962 switch (ctx
->flags
& SHA_FLAGS_ALGO_MASK
) {
965 memcpy(req
->result
, ctx
->digest
, SHA1_DIGEST_SIZE
);
968 case SHA_FLAGS_SHA224
:
969 memcpy(req
->result
, ctx
->digest
, SHA224_DIGEST_SIZE
);
972 case SHA_FLAGS_SHA256
:
973 memcpy(req
->result
, ctx
->digest
, SHA256_DIGEST_SIZE
);
976 case SHA_FLAGS_SHA384
:
977 memcpy(req
->result
, ctx
->digest
, SHA384_DIGEST_SIZE
);
980 case SHA_FLAGS_SHA512
:
981 memcpy(req
->result
, ctx
->digest
, SHA512_DIGEST_SIZE
);
986 static int atmel_sha_finish(struct ahash_request
*req
)
988 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
989 struct atmel_sha_dev
*dd
= ctx
->dd
;
991 if (ctx
->digcnt
[0] || ctx
->digcnt
[1])
992 atmel_sha_copy_ready_hash(req
);
994 dev_dbg(dd
->dev
, "digcnt: 0x%llx 0x%llx, bufcnt: %zd\n", ctx
->digcnt
[1],
995 ctx
->digcnt
[0], ctx
->bufcnt
);
1000 static void atmel_sha_finish_req(struct ahash_request
*req
, int err
)
1002 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1003 struct atmel_sha_dev
*dd
= ctx
->dd
;
1006 atmel_sha_copy_hash(req
);
1007 if (SHA_FLAGS_FINAL
& dd
->flags
)
1008 err
= atmel_sha_finish(req
);
1010 ctx
->flags
|= SHA_FLAGS_ERROR
;
1013 /* atomic operation is not needed here */
1014 (void)atmel_sha_complete(dd
, err
);
1017 static int atmel_sha_hw_init(struct atmel_sha_dev
*dd
)
1021 err
= clk_enable(dd
->iclk
);
1025 if (!(SHA_FLAGS_INIT
& dd
->flags
)) {
1026 atmel_sha_write(dd
, SHA_CR
, SHA_CR_SWRST
);
1027 dd
->flags
|= SHA_FLAGS_INIT
;
1034 static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev
*dd
)
1036 return atmel_sha_read(dd
, SHA_HW_VERSION
) & 0x00000fff;
1039 static void atmel_sha_hw_version_init(struct atmel_sha_dev
*dd
)
1041 atmel_sha_hw_init(dd
);
1043 dd
->hw_version
= atmel_sha_get_version(dd
);
1046 "version: 0x%x\n", dd
->hw_version
);
1048 clk_disable(dd
->iclk
);
1051 static int atmel_sha_handle_queue(struct atmel_sha_dev
*dd
,
1052 struct ahash_request
*req
)
1054 struct crypto_async_request
*async_req
, *backlog
;
1055 struct atmel_sha_ctx
*ctx
;
1056 unsigned long flags
;
1058 int err
= 0, ret
= 0;
1060 spin_lock_irqsave(&dd
->lock
, flags
);
1062 ret
= ahash_enqueue_request(&dd
->queue
, req
);
1064 if (SHA_FLAGS_BUSY
& dd
->flags
) {
1065 spin_unlock_irqrestore(&dd
->lock
, flags
);
1069 backlog
= crypto_get_backlog(&dd
->queue
);
1070 async_req
= crypto_dequeue_request(&dd
->queue
);
1072 dd
->flags
|= SHA_FLAGS_BUSY
;
1074 spin_unlock_irqrestore(&dd
->lock
, flags
);
1080 backlog
->complete(backlog
, -EINPROGRESS
);
1082 ctx
= crypto_tfm_ctx(async_req
->tfm
);
1084 dd
->req
= ahash_request_cast(async_req
);
1085 start_async
= (dd
->req
!= req
);
1086 dd
->is_async
= start_async
;
1087 dd
->force_complete
= false;
1089 /* WARNING: ctx->start() MAY change dd->is_async. */
1090 err
= ctx
->start(dd
);
1091 return (start_async
) ? ret
: err
;
1094 static int atmel_sha_done(struct atmel_sha_dev
*dd
);
1096 static int atmel_sha_start(struct atmel_sha_dev
*dd
)
1098 struct ahash_request
*req
= dd
->req
;
1099 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1102 dev_dbg(dd
->dev
, "handling new req, op: %lu, nbytes: %d\n",
1103 ctx
->op
, req
->nbytes
);
1105 err
= atmel_sha_hw_init(dd
);
1107 return atmel_sha_complete(dd
, err
);
1110 * atmel_sha_update_req() and atmel_sha_final_req() can return either:
1111 * -EINPROGRESS: the hardware is busy and the SHA driver will resume
1112 * its job later in the done_task.
1113 * This is the main path.
1115 * 0: the SHA driver can continue its job then release the hardware
1116 * later, if needed, with atmel_sha_finish_req().
1117 * This is the alternate path.
1119 * < 0: an error has occurred so atmel_sha_complete(dd, err) has already
1120 * been called, hence the hardware has been released.
1121 * The SHA driver must stop its job without calling
1122 * atmel_sha_finish_req(), otherwise atmel_sha_complete() would be
1123 * called a second time.
1125 * Please note that currently, atmel_sha_final_req() never returns 0.
1128 dd
->resume
= atmel_sha_done
;
1129 if (ctx
->op
== SHA_OP_UPDATE
) {
1130 err
= atmel_sha_update_req(dd
);
1131 if (!err
&& (ctx
->flags
& SHA_FLAGS_FINUP
))
1132 /* no final() after finup() */
1133 err
= atmel_sha_final_req(dd
);
1134 } else if (ctx
->op
== SHA_OP_FINAL
) {
1135 err
= atmel_sha_final_req(dd
);
1139 /* done_task will not finish it, so do it here */
1140 atmel_sha_finish_req(req
, err
);
1142 dev_dbg(dd
->dev
, "exit, err: %d\n", err
);
1147 static int atmel_sha_enqueue(struct ahash_request
*req
, unsigned int op
)
1149 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1150 struct atmel_sha_ctx
*tctx
= crypto_tfm_ctx(req
->base
.tfm
);
1151 struct atmel_sha_dev
*dd
= tctx
->dd
;
1155 return atmel_sha_handle_queue(dd
, req
);
1158 static int atmel_sha_update(struct ahash_request
*req
)
1160 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1165 ctx
->total
= req
->nbytes
;
1169 if (ctx
->flags
& SHA_FLAGS_FINUP
) {
1170 if (ctx
->bufcnt
+ ctx
->total
< ATMEL_SHA_DMA_THRESHOLD
)
1171 /* faster to use CPU for short transfers */
1172 ctx
->flags
|= SHA_FLAGS_CPU
;
1173 } else if (ctx
->bufcnt
+ ctx
->total
< ctx
->buflen
) {
1174 atmel_sha_append_sg(ctx
);
1177 return atmel_sha_enqueue(req
, SHA_OP_UPDATE
);
1180 static int atmel_sha_final(struct ahash_request
*req
)
1182 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1184 ctx
->flags
|= SHA_FLAGS_FINUP
;
1186 if (ctx
->flags
& SHA_FLAGS_ERROR
)
1187 return 0; /* uncompleted hash is not needed */
1189 if (ctx
->flags
& SHA_FLAGS_PAD
)
1190 /* copy ready hash (+ finalize hmac) */
1191 return atmel_sha_finish(req
);
1193 return atmel_sha_enqueue(req
, SHA_OP_FINAL
);
1196 static int atmel_sha_finup(struct ahash_request
*req
)
1198 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1201 ctx
->flags
|= SHA_FLAGS_FINUP
;
1203 err1
= atmel_sha_update(req
);
1204 if (err1
== -EINPROGRESS
||
1205 (err1
== -EBUSY
&& (ahash_request_flags(req
) &
1206 CRYPTO_TFM_REQ_MAY_BACKLOG
)))
1210 * final() has to be always called to cleanup resources
1211 * even if udpate() failed, except EINPROGRESS
1213 err2
= atmel_sha_final(req
);
1215 return err1
?: err2
;
1218 static int atmel_sha_digest(struct ahash_request
*req
)
1220 return atmel_sha_init(req
) ?: atmel_sha_finup(req
);
1224 static int atmel_sha_export(struct ahash_request
*req
, void *out
)
1226 const struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1228 memcpy(out
, ctx
, sizeof(*ctx
));
1232 static int atmel_sha_import(struct ahash_request
*req
, const void *in
)
1234 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1236 memcpy(ctx
, in
, sizeof(*ctx
));
1240 static int atmel_sha_cra_init(struct crypto_tfm
*tfm
)
1242 struct atmel_sha_ctx
*ctx
= crypto_tfm_ctx(tfm
);
1244 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
1245 sizeof(struct atmel_sha_reqctx
));
1246 ctx
->start
= atmel_sha_start
;
1251 static struct ahash_alg sha_1_256_algs
[] = {
1253 .init
= atmel_sha_init
,
1254 .update
= atmel_sha_update
,
1255 .final
= atmel_sha_final
,
1256 .finup
= atmel_sha_finup
,
1257 .digest
= atmel_sha_digest
,
1258 .export
= atmel_sha_export
,
1259 .import
= atmel_sha_import
,
1261 .digestsize
= SHA1_DIGEST_SIZE
,
1262 .statesize
= sizeof(struct atmel_sha_reqctx
),
1265 .cra_driver_name
= "atmel-sha1",
1266 .cra_priority
= 100,
1267 .cra_flags
= CRYPTO_ALG_ASYNC
,
1268 .cra_blocksize
= SHA1_BLOCK_SIZE
,
1269 .cra_ctxsize
= sizeof(struct atmel_sha_ctx
),
1271 .cra_module
= THIS_MODULE
,
1272 .cra_init
= atmel_sha_cra_init
,
1277 .init
= atmel_sha_init
,
1278 .update
= atmel_sha_update
,
1279 .final
= atmel_sha_final
,
1280 .finup
= atmel_sha_finup
,
1281 .digest
= atmel_sha_digest
,
1282 .export
= atmel_sha_export
,
1283 .import
= atmel_sha_import
,
1285 .digestsize
= SHA256_DIGEST_SIZE
,
1286 .statesize
= sizeof(struct atmel_sha_reqctx
),
1288 .cra_name
= "sha256",
1289 .cra_driver_name
= "atmel-sha256",
1290 .cra_priority
= 100,
1291 .cra_flags
= CRYPTO_ALG_ASYNC
,
1292 .cra_blocksize
= SHA256_BLOCK_SIZE
,
1293 .cra_ctxsize
= sizeof(struct atmel_sha_ctx
),
1295 .cra_module
= THIS_MODULE
,
1296 .cra_init
= atmel_sha_cra_init
,
1302 static struct ahash_alg sha_224_alg
= {
1303 .init
= atmel_sha_init
,
1304 .update
= atmel_sha_update
,
1305 .final
= atmel_sha_final
,
1306 .finup
= atmel_sha_finup
,
1307 .digest
= atmel_sha_digest
,
1308 .export
= atmel_sha_export
,
1309 .import
= atmel_sha_import
,
1311 .digestsize
= SHA224_DIGEST_SIZE
,
1312 .statesize
= sizeof(struct atmel_sha_reqctx
),
1314 .cra_name
= "sha224",
1315 .cra_driver_name
= "atmel-sha224",
1316 .cra_priority
= 100,
1317 .cra_flags
= CRYPTO_ALG_ASYNC
,
1318 .cra_blocksize
= SHA224_BLOCK_SIZE
,
1319 .cra_ctxsize
= sizeof(struct atmel_sha_ctx
),
1321 .cra_module
= THIS_MODULE
,
1322 .cra_init
= atmel_sha_cra_init
,
1327 static struct ahash_alg sha_384_512_algs
[] = {
1329 .init
= atmel_sha_init
,
1330 .update
= atmel_sha_update
,
1331 .final
= atmel_sha_final
,
1332 .finup
= atmel_sha_finup
,
1333 .digest
= atmel_sha_digest
,
1334 .export
= atmel_sha_export
,
1335 .import
= atmel_sha_import
,
1337 .digestsize
= SHA384_DIGEST_SIZE
,
1338 .statesize
= sizeof(struct atmel_sha_reqctx
),
1340 .cra_name
= "sha384",
1341 .cra_driver_name
= "atmel-sha384",
1342 .cra_priority
= 100,
1343 .cra_flags
= CRYPTO_ALG_ASYNC
,
1344 .cra_blocksize
= SHA384_BLOCK_SIZE
,
1345 .cra_ctxsize
= sizeof(struct atmel_sha_ctx
),
1346 .cra_alignmask
= 0x3,
1347 .cra_module
= THIS_MODULE
,
1348 .cra_init
= atmel_sha_cra_init
,
1353 .init
= atmel_sha_init
,
1354 .update
= atmel_sha_update
,
1355 .final
= atmel_sha_final
,
1356 .finup
= atmel_sha_finup
,
1357 .digest
= atmel_sha_digest
,
1358 .export
= atmel_sha_export
,
1359 .import
= atmel_sha_import
,
1361 .digestsize
= SHA512_DIGEST_SIZE
,
1362 .statesize
= sizeof(struct atmel_sha_reqctx
),
1364 .cra_name
= "sha512",
1365 .cra_driver_name
= "atmel-sha512",
1366 .cra_priority
= 100,
1367 .cra_flags
= CRYPTO_ALG_ASYNC
,
1368 .cra_blocksize
= SHA512_BLOCK_SIZE
,
1369 .cra_ctxsize
= sizeof(struct atmel_sha_ctx
),
1370 .cra_alignmask
= 0x3,
1371 .cra_module
= THIS_MODULE
,
1372 .cra_init
= atmel_sha_cra_init
,
1378 static void atmel_sha_queue_task(unsigned long data
)
1380 struct atmel_sha_dev
*dd
= (struct atmel_sha_dev
*)data
;
1382 atmel_sha_handle_queue(dd
, NULL
);
1385 static int atmel_sha_done(struct atmel_sha_dev
*dd
)
1389 if (SHA_FLAGS_CPU
& dd
->flags
) {
1390 if (SHA_FLAGS_OUTPUT_READY
& dd
->flags
) {
1391 dd
->flags
&= ~SHA_FLAGS_OUTPUT_READY
;
1394 } else if (SHA_FLAGS_DMA_READY
& dd
->flags
) {
1395 if (SHA_FLAGS_DMA_ACTIVE
& dd
->flags
) {
1396 dd
->flags
&= ~SHA_FLAGS_DMA_ACTIVE
;
1397 atmel_sha_update_dma_stop(dd
);
1403 if (SHA_FLAGS_OUTPUT_READY
& dd
->flags
) {
1404 /* hash or semi-hash ready */
1405 dd
->flags
&= ~(SHA_FLAGS_DMA_READY
|
1406 SHA_FLAGS_OUTPUT_READY
);
1407 err
= atmel_sha_update_dma_start(dd
);
1408 if (err
!= -EINPROGRESS
)
1415 /* finish curent request */
1416 atmel_sha_finish_req(dd
->req
, err
);
1421 static void atmel_sha_done_task(unsigned long data
)
1423 struct atmel_sha_dev
*dd
= (struct atmel_sha_dev
*)data
;
1425 dd
->is_async
= true;
1426 (void)dd
->resume(dd
);
1429 static irqreturn_t
atmel_sha_irq(int irq
, void *dev_id
)
1431 struct atmel_sha_dev
*sha_dd
= dev_id
;
1434 reg
= atmel_sha_read(sha_dd
, SHA_ISR
);
1435 if (reg
& atmel_sha_read(sha_dd
, SHA_IMR
)) {
1436 atmel_sha_write(sha_dd
, SHA_IDR
, reg
);
1437 if (SHA_FLAGS_BUSY
& sha_dd
->flags
) {
1438 sha_dd
->flags
|= SHA_FLAGS_OUTPUT_READY
;
1439 if (!(SHA_FLAGS_CPU
& sha_dd
->flags
))
1440 sha_dd
->flags
|= SHA_FLAGS_DMA_READY
;
1441 tasklet_schedule(&sha_dd
->done_task
);
1443 dev_warn(sha_dd
->dev
, "SHA interrupt when no active requests.\n");
1452 /* DMA transfer functions */
1454 static bool atmel_sha_dma_check_aligned(struct atmel_sha_dev
*dd
,
1455 struct scatterlist
*sg
,
1458 struct atmel_sha_dma
*dma
= &dd
->dma_lch_in
;
1459 struct ahash_request
*req
= dd
->req
;
1460 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1461 size_t bs
= ctx
->block_size
;
1464 for (nents
= 0; sg
; sg
= sg_next(sg
), ++nents
) {
1465 if (!IS_ALIGNED(sg
->offset
, sizeof(u32
)))
1469 * This is the last sg, the only one that is allowed to
1470 * have an unaligned length.
1472 if (len
<= sg
->length
) {
1473 dma
->nents
= nents
+ 1;
1474 dma
->last_sg_length
= sg
->length
;
1475 sg
->length
= ALIGN(len
, sizeof(u32
));
1479 /* All other sg lengths MUST be aligned to the block size. */
1480 if (!IS_ALIGNED(sg
->length
, bs
))
1489 static void atmel_sha_dma_callback2(void *data
)
1491 struct atmel_sha_dev
*dd
= data
;
1492 struct atmel_sha_dma
*dma
= &dd
->dma_lch_in
;
1493 struct scatterlist
*sg
;
1496 dmaengine_terminate_all(dma
->chan
);
1497 dma_unmap_sg(dd
->dev
, dma
->sg
, dma
->nents
, DMA_TO_DEVICE
);
1500 for (nents
= 0; nents
< dma
->nents
- 1; ++nents
)
1502 sg
->length
= dma
->last_sg_length
;
1504 dd
->is_async
= true;
1505 (void)atmel_sha_wait_for_data_ready(dd
, dd
->resume
);
1508 static int atmel_sha_dma_start(struct atmel_sha_dev
*dd
,
1509 struct scatterlist
*src
,
1511 atmel_sha_fn_t resume
)
1513 struct atmel_sha_dma
*dma
= &dd
->dma_lch_in
;
1514 struct dma_slave_config
*config
= &dma
->dma_conf
;
1515 struct dma_chan
*chan
= dma
->chan
;
1516 struct dma_async_tx_descriptor
*desc
;
1517 dma_cookie_t cookie
;
1518 unsigned int sg_len
;
1521 dd
->resume
= resume
;
1524 * dma->nents has already been initialized by
1525 * atmel_sha_dma_check_aligned().
1528 sg_len
= dma_map_sg(dd
->dev
, dma
->sg
, dma
->nents
, DMA_TO_DEVICE
);
1534 config
->src_maxburst
= 16;
1535 config
->dst_maxburst
= 16;
1536 err
= dmaengine_slave_config(chan
, config
);
1540 desc
= dmaengine_prep_slave_sg(chan
, dma
->sg
, sg_len
, DMA_MEM_TO_DEV
,
1541 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
1547 desc
->callback
= atmel_sha_dma_callback2
;
1548 desc
->callback_param
= dd
;
1549 cookie
= dmaengine_submit(desc
);
1550 err
= dma_submit_error(cookie
);
1554 dma_async_issue_pending(chan
);
1556 return -EINPROGRESS
;
1559 dma_unmap_sg(dd
->dev
, dma
->sg
, dma
->nents
, DMA_TO_DEVICE
);
1561 return atmel_sha_complete(dd
, err
);
1565 /* CPU transfer functions */
1567 static int atmel_sha_cpu_transfer(struct atmel_sha_dev
*dd
)
1569 struct ahash_request
*req
= dd
->req
;
1570 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1571 const u32
*words
= (const u32
*)ctx
->buffer
;
1572 size_t i
, num_words
;
1573 u32 isr
, din
, din_inc
;
1575 din_inc
= (ctx
->flags
& SHA_FLAGS_IDATAR0
) ? 0 : 1;
1577 /* Write data into the Input Data Registers. */
1578 num_words
= DIV_ROUND_UP(ctx
->bufcnt
, sizeof(u32
));
1579 for (i
= 0, din
= 0; i
< num_words
; ++i
, din
+= din_inc
)
1580 atmel_sha_write(dd
, SHA_REG_DIN(din
), words
[i
]);
1582 ctx
->offset
+= ctx
->bufcnt
;
1583 ctx
->total
-= ctx
->bufcnt
;
1589 * Prepare next block:
1590 * Fill ctx->buffer now with the next data to be written into
1591 * IDATARx: it gives time for the SHA hardware to process
1592 * the current data so the SHA_INT_DATARDY flag might be set
1593 * in SHA_ISR when polling this register at the beginning of
1596 ctx
->bufcnt
= min_t(size_t, ctx
->block_size
, ctx
->total
);
1597 scatterwalk_map_and_copy(ctx
->buffer
, ctx
->sg
,
1598 ctx
->offset
, ctx
->bufcnt
, 0);
1600 /* Wait for hardware to be ready again. */
1601 isr
= atmel_sha_read(dd
, SHA_ISR
);
1602 if (!(isr
& SHA_INT_DATARDY
)) {
1603 /* Not ready yet. */
1604 dd
->resume
= atmel_sha_cpu_transfer
;
1605 atmel_sha_write(dd
, SHA_IER
, SHA_INT_DATARDY
);
1606 return -EINPROGRESS
;
1610 if (unlikely(!(ctx
->flags
& SHA_FLAGS_WAIT_DATARDY
)))
1611 return dd
->cpu_transfer_complete(dd
);
1613 return atmel_sha_wait_for_data_ready(dd
, dd
->cpu_transfer_complete
);
1616 static int atmel_sha_cpu_start(struct atmel_sha_dev
*dd
,
1617 struct scatterlist
*sg
,
1620 bool wait_data_ready
,
1621 atmel_sha_fn_t resume
)
1623 struct ahash_request
*req
= dd
->req
;
1624 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1629 ctx
->flags
&= ~(SHA_FLAGS_IDATAR0
| SHA_FLAGS_WAIT_DATARDY
);
1632 ctx
->flags
|= SHA_FLAGS_IDATAR0
;
1634 if (wait_data_ready
)
1635 ctx
->flags
|= SHA_FLAGS_WAIT_DATARDY
;
1641 /* Prepare the first block to be written. */
1642 ctx
->bufcnt
= min_t(size_t, ctx
->block_size
, ctx
->total
);
1643 scatterwalk_map_and_copy(ctx
->buffer
, ctx
->sg
,
1644 ctx
->offset
, ctx
->bufcnt
, 0);
1646 dd
->cpu_transfer_complete
= resume
;
1647 return atmel_sha_cpu_transfer(dd
);
1650 static int atmel_sha_cpu_hash(struct atmel_sha_dev
*dd
,
1651 const void *data
, unsigned int datalen
,
1653 atmel_sha_fn_t resume
)
1655 struct ahash_request
*req
= dd
->req
;
1656 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1657 u32 msglen
= (auto_padding
) ? datalen
: 0;
1658 u32 mr
= SHA_MR_MODE_AUTO
;
1660 if (!(IS_ALIGNED(datalen
, ctx
->block_size
) || auto_padding
))
1661 return atmel_sha_complete(dd
, -EINVAL
);
1663 mr
|= (ctx
->flags
& SHA_FLAGS_ALGO_MASK
);
1664 atmel_sha_write(dd
, SHA_MR
, mr
);
1665 atmel_sha_write(dd
, SHA_MSR
, msglen
);
1666 atmel_sha_write(dd
, SHA_BCR
, msglen
);
1667 atmel_sha_write(dd
, SHA_CR
, SHA_CR_FIRST
);
1669 sg_init_one(&dd
->tmp
, data
, datalen
);
1670 return atmel_sha_cpu_start(dd
, &dd
->tmp
, datalen
, false, true, resume
);
1674 /* hmac functions */
1676 struct atmel_sha_hmac_key
{
1678 unsigned int keylen
;
1679 u8 buffer
[SHA512_BLOCK_SIZE
];
1683 static inline void atmel_sha_hmac_key_init(struct atmel_sha_hmac_key
*hkey
)
1685 memset(hkey
, 0, sizeof(*hkey
));
1688 static inline void atmel_sha_hmac_key_release(struct atmel_sha_hmac_key
*hkey
)
1690 kfree(hkey
->keydup
);
1691 memset(hkey
, 0, sizeof(*hkey
));
1694 static inline int atmel_sha_hmac_key_set(struct atmel_sha_hmac_key
*hkey
,
1696 unsigned int keylen
)
1698 atmel_sha_hmac_key_release(hkey
);
1700 if (keylen
> sizeof(hkey
->buffer
)) {
1701 hkey
->keydup
= kmemdup(key
, keylen
, GFP_KERNEL
);
1706 memcpy(hkey
->buffer
, key
, keylen
);
1710 hkey
->keylen
= keylen
;
1714 static inline bool atmel_sha_hmac_key_get(const struct atmel_sha_hmac_key
*hkey
,
1716 unsigned int *keylen
)
1721 *keylen
= hkey
->keylen
;
1722 *key
= (hkey
->keydup
) ? hkey
->keydup
: hkey
->buffer
;
1727 struct atmel_sha_hmac_ctx
{
1728 struct atmel_sha_ctx base
;
1730 struct atmel_sha_hmac_key hkey
;
1731 u32 ipad
[SHA512_BLOCK_SIZE
/ sizeof(u32
)];
1732 u32 opad
[SHA512_BLOCK_SIZE
/ sizeof(u32
)];
1733 atmel_sha_fn_t resume
;
1736 static int atmel_sha_hmac_setup(struct atmel_sha_dev
*dd
,
1737 atmel_sha_fn_t resume
);
1738 static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev
*dd
,
1739 const u8
*key
, unsigned int keylen
);
1740 static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev
*dd
);
1741 static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev
*dd
);
1742 static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev
*dd
);
1743 static int atmel_sha_hmac_setup_done(struct atmel_sha_dev
*dd
);
1745 static int atmel_sha_hmac_init_done(struct atmel_sha_dev
*dd
);
1746 static int atmel_sha_hmac_final(struct atmel_sha_dev
*dd
);
1747 static int atmel_sha_hmac_final_done(struct atmel_sha_dev
*dd
);
1748 static int atmel_sha_hmac_digest2(struct atmel_sha_dev
*dd
);
1750 static int atmel_sha_hmac_setup(struct atmel_sha_dev
*dd
,
1751 atmel_sha_fn_t resume
)
1753 struct ahash_request
*req
= dd
->req
;
1754 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1755 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
1756 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
1757 unsigned int keylen
;
1761 hmac
->resume
= resume
;
1762 switch (ctx
->flags
& SHA_FLAGS_ALGO_MASK
) {
1763 case SHA_FLAGS_SHA1
:
1764 ctx
->block_size
= SHA1_BLOCK_SIZE
;
1765 ctx
->hash_size
= SHA1_DIGEST_SIZE
;
1768 case SHA_FLAGS_SHA224
:
1769 ctx
->block_size
= SHA224_BLOCK_SIZE
;
1770 ctx
->hash_size
= SHA256_DIGEST_SIZE
;
1773 case SHA_FLAGS_SHA256
:
1774 ctx
->block_size
= SHA256_BLOCK_SIZE
;
1775 ctx
->hash_size
= SHA256_DIGEST_SIZE
;
1778 case SHA_FLAGS_SHA384
:
1779 ctx
->block_size
= SHA384_BLOCK_SIZE
;
1780 ctx
->hash_size
= SHA512_DIGEST_SIZE
;
1783 case SHA_FLAGS_SHA512
:
1784 ctx
->block_size
= SHA512_BLOCK_SIZE
;
1785 ctx
->hash_size
= SHA512_DIGEST_SIZE
;
1789 return atmel_sha_complete(dd
, -EINVAL
);
1791 bs
= ctx
->block_size
;
1793 if (likely(!atmel_sha_hmac_key_get(&hmac
->hkey
, &key
, &keylen
)))
1796 /* Compute K' from K. */
1797 if (unlikely(keylen
> bs
))
1798 return atmel_sha_hmac_prehash_key(dd
, key
, keylen
);
1801 memcpy((u8
*)hmac
->ipad
, key
, keylen
);
1802 memset((u8
*)hmac
->ipad
+ keylen
, 0, bs
- keylen
);
1803 return atmel_sha_hmac_compute_ipad_hash(dd
);
1806 static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev
*dd
,
1807 const u8
*key
, unsigned int keylen
)
1809 return atmel_sha_cpu_hash(dd
, key
, keylen
, true,
1810 atmel_sha_hmac_prehash_key_done
);
1813 static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev
*dd
)
1815 struct ahash_request
*req
= dd
->req
;
1816 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
1817 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
1818 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1819 size_t ds
= crypto_ahash_digestsize(tfm
);
1820 size_t bs
= ctx
->block_size
;
1821 size_t i
, num_words
= ds
/ sizeof(u32
);
1824 for (i
= 0; i
< num_words
; ++i
)
1825 hmac
->ipad
[i
] = atmel_sha_read(dd
, SHA_REG_DIGEST(i
));
1826 memset((u8
*)hmac
->ipad
+ ds
, 0, bs
- ds
);
1827 return atmel_sha_hmac_compute_ipad_hash(dd
);
1830 static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev
*dd
)
1832 struct ahash_request
*req
= dd
->req
;
1833 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
1834 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
1835 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1836 size_t bs
= ctx
->block_size
;
1837 size_t i
, num_words
= bs
/ sizeof(u32
);
1839 memcpy(hmac
->opad
, hmac
->ipad
, bs
);
1840 for (i
= 0; i
< num_words
; ++i
) {
1841 hmac
->ipad
[i
] ^= 0x36363636;
1842 hmac
->opad
[i
] ^= 0x5c5c5c5c;
1845 return atmel_sha_cpu_hash(dd
, hmac
->ipad
, bs
, false,
1846 atmel_sha_hmac_compute_opad_hash
);
1849 static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev
*dd
)
1851 struct ahash_request
*req
= dd
->req
;
1852 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
1853 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
1854 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1855 size_t bs
= ctx
->block_size
;
1856 size_t hs
= ctx
->hash_size
;
1857 size_t i
, num_words
= hs
/ sizeof(u32
);
1859 for (i
= 0; i
< num_words
; ++i
)
1860 hmac
->ipad
[i
] = atmel_sha_read(dd
, SHA_REG_DIGEST(i
));
1861 return atmel_sha_cpu_hash(dd
, hmac
->opad
, bs
, false,
1862 atmel_sha_hmac_setup_done
);
1865 static int atmel_sha_hmac_setup_done(struct atmel_sha_dev
*dd
)
1867 struct ahash_request
*req
= dd
->req
;
1868 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
1869 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
1870 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1871 size_t hs
= ctx
->hash_size
;
1872 size_t i
, num_words
= hs
/ sizeof(u32
);
1874 for (i
= 0; i
< num_words
; ++i
)
1875 hmac
->opad
[i
] = atmel_sha_read(dd
, SHA_REG_DIGEST(i
));
1876 atmel_sha_hmac_key_release(&hmac
->hkey
);
1877 return hmac
->resume(dd
);
1880 static int atmel_sha_hmac_start(struct atmel_sha_dev
*dd
)
1882 struct ahash_request
*req
= dd
->req
;
1883 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1886 err
= atmel_sha_hw_init(dd
);
1888 return atmel_sha_complete(dd
, err
);
1892 err
= atmel_sha_hmac_setup(dd
, atmel_sha_hmac_init_done
);
1896 dd
->resume
= atmel_sha_done
;
1897 err
= atmel_sha_update_req(dd
);
1901 dd
->resume
= atmel_sha_hmac_final
;
1902 err
= atmel_sha_final_req(dd
);
1906 err
= atmel_sha_hmac_setup(dd
, atmel_sha_hmac_digest2
);
1910 return atmel_sha_complete(dd
, -EINVAL
);
1916 static int atmel_sha_hmac_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
1917 unsigned int keylen
)
1919 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
1921 if (atmel_sha_hmac_key_set(&hmac
->hkey
, key
, keylen
)) {
1922 crypto_ahash_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
1929 static int atmel_sha_hmac_init(struct ahash_request
*req
)
1933 err
= atmel_sha_init(req
);
1937 return atmel_sha_enqueue(req
, SHA_OP_INIT
);
1940 static int atmel_sha_hmac_init_done(struct atmel_sha_dev
*dd
)
1942 struct ahash_request
*req
= dd
->req
;
1943 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1944 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
1945 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
1946 size_t bs
= ctx
->block_size
;
1947 size_t hs
= ctx
->hash_size
;
1950 ctx
->digcnt
[0] = bs
;
1952 ctx
->flags
|= SHA_FLAGS_RESTORE
;
1953 memcpy(ctx
->digest
, hmac
->ipad
, hs
);
1954 return atmel_sha_complete(dd
, 0);
1957 static int atmel_sha_hmac_final(struct atmel_sha_dev
*dd
)
1959 struct ahash_request
*req
= dd
->req
;
1960 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1961 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
1962 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
1963 u32
*digest
= (u32
*)ctx
->digest
;
1964 size_t ds
= crypto_ahash_digestsize(tfm
);
1965 size_t bs
= ctx
->block_size
;
1966 size_t hs
= ctx
->hash_size
;
1967 size_t i
, num_words
;
1970 /* Save d = SHA((K' + ipad) | msg). */
1971 num_words
= ds
/ sizeof(u32
);
1972 for (i
= 0; i
< num_words
; ++i
)
1973 digest
[i
] = atmel_sha_read(dd
, SHA_REG_DIGEST(i
));
1975 /* Restore context to finish computing SHA((K' + opad) | d). */
1976 atmel_sha_write(dd
, SHA_CR
, SHA_CR_WUIHV
);
1977 num_words
= hs
/ sizeof(u32
);
1978 for (i
= 0; i
< num_words
; ++i
)
1979 atmel_sha_write(dd
, SHA_REG_DIN(i
), hmac
->opad
[i
]);
1981 mr
= SHA_MR_MODE_AUTO
| SHA_MR_UIHV
;
1982 mr
|= (ctx
->flags
& SHA_FLAGS_ALGO_MASK
);
1983 atmel_sha_write(dd
, SHA_MR
, mr
);
1984 atmel_sha_write(dd
, SHA_MSR
, bs
+ ds
);
1985 atmel_sha_write(dd
, SHA_BCR
, ds
);
1986 atmel_sha_write(dd
, SHA_CR
, SHA_CR_FIRST
);
1988 sg_init_one(&dd
->tmp
, digest
, ds
);
1989 return atmel_sha_cpu_start(dd
, &dd
->tmp
, ds
, false, true,
1990 atmel_sha_hmac_final_done
);
1993 static int atmel_sha_hmac_final_done(struct atmel_sha_dev
*dd
)
1996 * req->result might not be sizeof(u32) aligned, so copy the
1997 * digest into ctx->digest[] before memcpy() the data into
2000 atmel_sha_copy_hash(dd
->req
);
2001 atmel_sha_copy_ready_hash(dd
->req
);
2002 return atmel_sha_complete(dd
, 0);
2005 static int atmel_sha_hmac_digest(struct ahash_request
*req
)
2009 err
= atmel_sha_init(req
);
2013 return atmel_sha_enqueue(req
, SHA_OP_DIGEST
);
2016 static int atmel_sha_hmac_digest2(struct atmel_sha_dev
*dd
)
2018 struct ahash_request
*req
= dd
->req
;
2019 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
2020 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
2021 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
2022 size_t hs
= ctx
->hash_size
;
2023 size_t i
, num_words
= hs
/ sizeof(u32
);
2024 bool use_dma
= false;
2027 /* Special case for empty message. */
2029 return atmel_sha_complete(dd
, -EINVAL
); // TODO:
2031 /* Check DMA threshold and alignment. */
2032 if (req
->nbytes
> ATMEL_SHA_DMA_THRESHOLD
&&
2033 atmel_sha_dma_check_aligned(dd
, req
->src
, req
->nbytes
))
2036 /* Write both initial hash values to compute a HMAC. */
2037 atmel_sha_write(dd
, SHA_CR
, SHA_CR_WUIHV
);
2038 for (i
= 0; i
< num_words
; ++i
)
2039 atmel_sha_write(dd
, SHA_REG_DIN(i
), hmac
->ipad
[i
]);
2041 atmel_sha_write(dd
, SHA_CR
, SHA_CR_WUIEHV
);
2042 for (i
= 0; i
< num_words
; ++i
)
2043 atmel_sha_write(dd
, SHA_REG_DIN(i
), hmac
->opad
[i
]);
2045 /* Write the Mode, Message Size, Bytes Count then Control Registers. */
2046 mr
= (SHA_MR_HMAC
| SHA_MR_DUALBUFF
);
2047 mr
|= ctx
->flags
& SHA_FLAGS_ALGO_MASK
;
2049 mr
|= SHA_MR_MODE_IDATAR0
;
2051 mr
|= SHA_MR_MODE_AUTO
;
2052 atmel_sha_write(dd
, SHA_MR
, mr
);
2054 atmel_sha_write(dd
, SHA_MSR
, req
->nbytes
);
2055 atmel_sha_write(dd
, SHA_BCR
, req
->nbytes
);
2057 atmel_sha_write(dd
, SHA_CR
, SHA_CR_FIRST
);
2061 return atmel_sha_dma_start(dd
, req
->src
, req
->nbytes
,
2062 atmel_sha_hmac_final_done
);
2064 return atmel_sha_cpu_start(dd
, req
->src
, req
->nbytes
, false, true,
2065 atmel_sha_hmac_final_done
);
2068 static int atmel_sha_hmac_cra_init(struct crypto_tfm
*tfm
)
2070 struct atmel_sha_hmac_ctx
*hmac
= crypto_tfm_ctx(tfm
);
2072 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
2073 sizeof(struct atmel_sha_reqctx
));
2074 hmac
->base
.start
= atmel_sha_hmac_start
;
2075 atmel_sha_hmac_key_init(&hmac
->hkey
);
2080 static void atmel_sha_hmac_cra_exit(struct crypto_tfm
*tfm
)
2082 struct atmel_sha_hmac_ctx
*hmac
= crypto_tfm_ctx(tfm
);
2084 atmel_sha_hmac_key_release(&hmac
->hkey
);
2087 static struct ahash_alg sha_hmac_algs
[] = {
2089 .init
= atmel_sha_hmac_init
,
2090 .update
= atmel_sha_update
,
2091 .final
= atmel_sha_final
,
2092 .digest
= atmel_sha_hmac_digest
,
2093 .setkey
= atmel_sha_hmac_setkey
,
2094 .export
= atmel_sha_export
,
2095 .import
= atmel_sha_import
,
2097 .digestsize
= SHA1_DIGEST_SIZE
,
2098 .statesize
= sizeof(struct atmel_sha_reqctx
),
2100 .cra_name
= "hmac(sha1)",
2101 .cra_driver_name
= "atmel-hmac-sha1",
2102 .cra_priority
= 100,
2103 .cra_flags
= CRYPTO_ALG_ASYNC
,
2104 .cra_blocksize
= SHA1_BLOCK_SIZE
,
2105 .cra_ctxsize
= sizeof(struct atmel_sha_hmac_ctx
),
2107 .cra_module
= THIS_MODULE
,
2108 .cra_init
= atmel_sha_hmac_cra_init
,
2109 .cra_exit
= atmel_sha_hmac_cra_exit
,
2114 .init
= atmel_sha_hmac_init
,
2115 .update
= atmel_sha_update
,
2116 .final
= atmel_sha_final
,
2117 .digest
= atmel_sha_hmac_digest
,
2118 .setkey
= atmel_sha_hmac_setkey
,
2119 .export
= atmel_sha_export
,
2120 .import
= atmel_sha_import
,
2122 .digestsize
= SHA224_DIGEST_SIZE
,
2123 .statesize
= sizeof(struct atmel_sha_reqctx
),
2125 .cra_name
= "hmac(sha224)",
2126 .cra_driver_name
= "atmel-hmac-sha224",
2127 .cra_priority
= 100,
2128 .cra_flags
= CRYPTO_ALG_ASYNC
,
2129 .cra_blocksize
= SHA224_BLOCK_SIZE
,
2130 .cra_ctxsize
= sizeof(struct atmel_sha_hmac_ctx
),
2132 .cra_module
= THIS_MODULE
,
2133 .cra_init
= atmel_sha_hmac_cra_init
,
2134 .cra_exit
= atmel_sha_hmac_cra_exit
,
2139 .init
= atmel_sha_hmac_init
,
2140 .update
= atmel_sha_update
,
2141 .final
= atmel_sha_final
,
2142 .digest
= atmel_sha_hmac_digest
,
2143 .setkey
= atmel_sha_hmac_setkey
,
2144 .export
= atmel_sha_export
,
2145 .import
= atmel_sha_import
,
2147 .digestsize
= SHA256_DIGEST_SIZE
,
2148 .statesize
= sizeof(struct atmel_sha_reqctx
),
2150 .cra_name
= "hmac(sha256)",
2151 .cra_driver_name
= "atmel-hmac-sha256",
2152 .cra_priority
= 100,
2153 .cra_flags
= CRYPTO_ALG_ASYNC
,
2154 .cra_blocksize
= SHA256_BLOCK_SIZE
,
2155 .cra_ctxsize
= sizeof(struct atmel_sha_hmac_ctx
),
2157 .cra_module
= THIS_MODULE
,
2158 .cra_init
= atmel_sha_hmac_cra_init
,
2159 .cra_exit
= atmel_sha_hmac_cra_exit
,
2164 .init
= atmel_sha_hmac_init
,
2165 .update
= atmel_sha_update
,
2166 .final
= atmel_sha_final
,
2167 .digest
= atmel_sha_hmac_digest
,
2168 .setkey
= atmel_sha_hmac_setkey
,
2169 .export
= atmel_sha_export
,
2170 .import
= atmel_sha_import
,
2172 .digestsize
= SHA384_DIGEST_SIZE
,
2173 .statesize
= sizeof(struct atmel_sha_reqctx
),
2175 .cra_name
= "hmac(sha384)",
2176 .cra_driver_name
= "atmel-hmac-sha384",
2177 .cra_priority
= 100,
2178 .cra_flags
= CRYPTO_ALG_ASYNC
,
2179 .cra_blocksize
= SHA384_BLOCK_SIZE
,
2180 .cra_ctxsize
= sizeof(struct atmel_sha_hmac_ctx
),
2182 .cra_module
= THIS_MODULE
,
2183 .cra_init
= atmel_sha_hmac_cra_init
,
2184 .cra_exit
= atmel_sha_hmac_cra_exit
,
2189 .init
= atmel_sha_hmac_init
,
2190 .update
= atmel_sha_update
,
2191 .final
= atmel_sha_final
,
2192 .digest
= atmel_sha_hmac_digest
,
2193 .setkey
= atmel_sha_hmac_setkey
,
2194 .export
= atmel_sha_export
,
2195 .import
= atmel_sha_import
,
2197 .digestsize
= SHA512_DIGEST_SIZE
,
2198 .statesize
= sizeof(struct atmel_sha_reqctx
),
2200 .cra_name
= "hmac(sha512)",
2201 .cra_driver_name
= "atmel-hmac-sha512",
2202 .cra_priority
= 100,
2203 .cra_flags
= CRYPTO_ALG_ASYNC
,
2204 .cra_blocksize
= SHA512_BLOCK_SIZE
,
2205 .cra_ctxsize
= sizeof(struct atmel_sha_hmac_ctx
),
2207 .cra_module
= THIS_MODULE
,
2208 .cra_init
= atmel_sha_hmac_cra_init
,
2209 .cra_exit
= atmel_sha_hmac_cra_exit
,
2215 #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
2216 /* authenc functions */
2218 static int atmel_sha_authenc_init2(struct atmel_sha_dev
*dd
);
2219 static int atmel_sha_authenc_init_done(struct atmel_sha_dev
*dd
);
2220 static int atmel_sha_authenc_final_done(struct atmel_sha_dev
*dd
);
2223 struct atmel_sha_authenc_ctx
{
2224 struct crypto_ahash
*tfm
;
2227 struct atmel_sha_authenc_reqctx
{
2228 struct atmel_sha_reqctx base
;
2230 atmel_aes_authenc_fn_t cb
;
2231 struct atmel_aes_dev
*aes_dev
;
2233 /* _init() parameters. */
2234 struct scatterlist
*assoc
;
2238 /* _final() parameters. */
2240 unsigned int digestlen
;
2243 static void atmel_sha_authenc_complete(struct crypto_async_request
*areq
,
2246 struct ahash_request
*req
= areq
->data
;
2247 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2249 authctx
->cb(authctx
->aes_dev
, err
, authctx
->base
.dd
->is_async
);
2252 static int atmel_sha_authenc_start(struct atmel_sha_dev
*dd
)
2254 struct ahash_request
*req
= dd
->req
;
2255 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2259 * Force atmel_sha_complete() to call req->base.complete(), ie
2260 * atmel_sha_authenc_complete(), which in turn calls authctx->cb().
2262 dd
->force_complete
= true;
2264 err
= atmel_sha_hw_init(dd
);
2265 return authctx
->cb(authctx
->aes_dev
, err
, dd
->is_async
);
2268 bool atmel_sha_authenc_is_ready(void)
2270 struct atmel_sha_ctx dummy
;
2273 return (atmel_sha_find_dev(&dummy
) != NULL
);
2275 EXPORT_SYMBOL_GPL(atmel_sha_authenc_is_ready
);
2277 unsigned int atmel_sha_authenc_get_reqsize(void)
2279 return sizeof(struct atmel_sha_authenc_reqctx
);
2281 EXPORT_SYMBOL_GPL(atmel_sha_authenc_get_reqsize
);
2283 struct atmel_sha_authenc_ctx
*atmel_sha_authenc_spawn(unsigned long mode
)
2285 struct atmel_sha_authenc_ctx
*auth
;
2286 struct crypto_ahash
*tfm
;
2287 struct atmel_sha_ctx
*tctx
;
2291 switch (mode
& SHA_FLAGS_MODE_MASK
) {
2292 case SHA_FLAGS_HMAC_SHA1
:
2293 name
= "atmel-hmac-sha1";
2296 case SHA_FLAGS_HMAC_SHA224
:
2297 name
= "atmel-hmac-sha224";
2300 case SHA_FLAGS_HMAC_SHA256
:
2301 name
= "atmel-hmac-sha256";
2304 case SHA_FLAGS_HMAC_SHA384
:
2305 name
= "atmel-hmac-sha384";
2308 case SHA_FLAGS_HMAC_SHA512
:
2309 name
= "atmel-hmac-sha512";
2316 tfm
= crypto_alloc_ahash(name
, 0, 0);
2321 tctx
= crypto_ahash_ctx(tfm
);
2322 tctx
->start
= atmel_sha_authenc_start
;
2325 auth
= kzalloc(sizeof(*auth
), GFP_KERNEL
);
2328 goto err_free_ahash
;
2335 crypto_free_ahash(tfm
);
2337 return ERR_PTR(err
);
2339 EXPORT_SYMBOL_GPL(atmel_sha_authenc_spawn
);
2341 void atmel_sha_authenc_free(struct atmel_sha_authenc_ctx
*auth
)
2344 crypto_free_ahash(auth
->tfm
);
2347 EXPORT_SYMBOL_GPL(atmel_sha_authenc_free
);
2349 int atmel_sha_authenc_setkey(struct atmel_sha_authenc_ctx
*auth
,
2350 const u8
*key
, unsigned int keylen
,
2353 struct crypto_ahash
*tfm
= auth
->tfm
;
2356 crypto_ahash_clear_flags(tfm
, CRYPTO_TFM_REQ_MASK
);
2357 crypto_ahash_set_flags(tfm
, *flags
& CRYPTO_TFM_REQ_MASK
);
2358 err
= crypto_ahash_setkey(tfm
, key
, keylen
);
2359 *flags
= crypto_ahash_get_flags(tfm
);
2363 EXPORT_SYMBOL_GPL(atmel_sha_authenc_setkey
);
2365 int atmel_sha_authenc_schedule(struct ahash_request
*req
,
2366 struct atmel_sha_authenc_ctx
*auth
,
2367 atmel_aes_authenc_fn_t cb
,
2368 struct atmel_aes_dev
*aes_dev
)
2370 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2371 struct atmel_sha_reqctx
*ctx
= &authctx
->base
;
2372 struct crypto_ahash
*tfm
= auth
->tfm
;
2373 struct atmel_sha_ctx
*tctx
= crypto_ahash_ctx(tfm
);
2374 struct atmel_sha_dev
*dd
;
2376 /* Reset request context (MUST be done first). */
2377 memset(authctx
, 0, sizeof(*authctx
));
2379 /* Get SHA device. */
2380 dd
= atmel_sha_find_dev(tctx
);
2382 return cb(aes_dev
, -ENODEV
, false);
2384 /* Init request context. */
2386 ctx
->buflen
= SHA_BUFFER_LEN
;
2388 authctx
->aes_dev
= aes_dev
;
2389 ahash_request_set_tfm(req
, tfm
);
2390 ahash_request_set_callback(req
, 0, atmel_sha_authenc_complete
, req
);
2392 return atmel_sha_handle_queue(dd
, req
);
2394 EXPORT_SYMBOL_GPL(atmel_sha_authenc_schedule
);
2396 int atmel_sha_authenc_init(struct ahash_request
*req
,
2397 struct scatterlist
*assoc
, unsigned int assoclen
,
2398 unsigned int textlen
,
2399 atmel_aes_authenc_fn_t cb
,
2400 struct atmel_aes_dev
*aes_dev
)
2402 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2403 struct atmel_sha_reqctx
*ctx
= &authctx
->base
;
2404 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
2405 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
2406 struct atmel_sha_dev
*dd
= ctx
->dd
;
2408 if (unlikely(!IS_ALIGNED(assoclen
, sizeof(u32
))))
2409 return atmel_sha_complete(dd
, -EINVAL
);
2412 authctx
->aes_dev
= aes_dev
;
2413 authctx
->assoc
= assoc
;
2414 authctx
->assoclen
= assoclen
;
2415 authctx
->textlen
= textlen
;
2417 ctx
->flags
= hmac
->base
.flags
;
2418 return atmel_sha_hmac_setup(dd
, atmel_sha_authenc_init2
);
2420 EXPORT_SYMBOL_GPL(atmel_sha_authenc_init
);
2422 static int atmel_sha_authenc_init2(struct atmel_sha_dev
*dd
)
2424 struct ahash_request
*req
= dd
->req
;
2425 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2426 struct atmel_sha_reqctx
*ctx
= &authctx
->base
;
2427 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
2428 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
2429 size_t hs
= ctx
->hash_size
;
2430 size_t i
, num_words
= hs
/ sizeof(u32
);
2433 atmel_sha_write(dd
, SHA_CR
, SHA_CR_WUIHV
);
2434 for (i
= 0; i
< num_words
; ++i
)
2435 atmel_sha_write(dd
, SHA_REG_DIN(i
), hmac
->ipad
[i
]);
2437 atmel_sha_write(dd
, SHA_CR
, SHA_CR_WUIEHV
);
2438 for (i
= 0; i
< num_words
; ++i
)
2439 atmel_sha_write(dd
, SHA_REG_DIN(i
), hmac
->opad
[i
]);
2441 mr
= (SHA_MR_MODE_IDATAR0
|
2444 mr
|= ctx
->flags
& SHA_FLAGS_ALGO_MASK
;
2445 atmel_sha_write(dd
, SHA_MR
, mr
);
2447 msg_size
= authctx
->assoclen
+ authctx
->textlen
;
2448 atmel_sha_write(dd
, SHA_MSR
, msg_size
);
2449 atmel_sha_write(dd
, SHA_BCR
, msg_size
);
2451 atmel_sha_write(dd
, SHA_CR
, SHA_CR_FIRST
);
2453 /* Process assoc data. */
2454 return atmel_sha_cpu_start(dd
, authctx
->assoc
, authctx
->assoclen
,
2456 atmel_sha_authenc_init_done
);
2459 static int atmel_sha_authenc_init_done(struct atmel_sha_dev
*dd
)
2461 struct ahash_request
*req
= dd
->req
;
2462 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2464 return authctx
->cb(authctx
->aes_dev
, 0, dd
->is_async
);
2467 int atmel_sha_authenc_final(struct ahash_request
*req
,
2468 u32
*digest
, unsigned int digestlen
,
2469 atmel_aes_authenc_fn_t cb
,
2470 struct atmel_aes_dev
*aes_dev
)
2472 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2473 struct atmel_sha_reqctx
*ctx
= &authctx
->base
;
2474 struct atmel_sha_dev
*dd
= ctx
->dd
;
2476 switch (ctx
->flags
& SHA_FLAGS_ALGO_MASK
) {
2477 case SHA_FLAGS_SHA1
:
2478 authctx
->digestlen
= SHA1_DIGEST_SIZE
;
2481 case SHA_FLAGS_SHA224
:
2482 authctx
->digestlen
= SHA224_DIGEST_SIZE
;
2485 case SHA_FLAGS_SHA256
:
2486 authctx
->digestlen
= SHA256_DIGEST_SIZE
;
2489 case SHA_FLAGS_SHA384
:
2490 authctx
->digestlen
= SHA384_DIGEST_SIZE
;
2493 case SHA_FLAGS_SHA512
:
2494 authctx
->digestlen
= SHA512_DIGEST_SIZE
;
2498 return atmel_sha_complete(dd
, -EINVAL
);
2500 if (authctx
->digestlen
> digestlen
)
2501 authctx
->digestlen
= digestlen
;
2504 authctx
->aes_dev
= aes_dev
;
2505 authctx
->digest
= digest
;
2506 return atmel_sha_wait_for_data_ready(dd
,
2507 atmel_sha_authenc_final_done
);
2509 EXPORT_SYMBOL_GPL(atmel_sha_authenc_final
);
2511 static int atmel_sha_authenc_final_done(struct atmel_sha_dev
*dd
)
2513 struct ahash_request
*req
= dd
->req
;
2514 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2515 size_t i
, num_words
= authctx
->digestlen
/ sizeof(u32
);
2517 for (i
= 0; i
< num_words
; ++i
)
2518 authctx
->digest
[i
] = atmel_sha_read(dd
, SHA_REG_DIGEST(i
));
2520 return atmel_sha_complete(dd
, 0);
2523 void atmel_sha_authenc_abort(struct ahash_request
*req
)
2525 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2526 struct atmel_sha_reqctx
*ctx
= &authctx
->base
;
2527 struct atmel_sha_dev
*dd
= ctx
->dd
;
2529 /* Prevent atmel_sha_complete() from calling req->base.complete(). */
2530 dd
->is_async
= false;
2531 dd
->force_complete
= false;
2532 (void)atmel_sha_complete(dd
, 0);
2534 EXPORT_SYMBOL_GPL(atmel_sha_authenc_abort
);
2536 #endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
2539 static void atmel_sha_unregister_algs(struct atmel_sha_dev
*dd
)
2543 if (dd
->caps
.has_hmac
)
2544 for (i
= 0; i
< ARRAY_SIZE(sha_hmac_algs
); i
++)
2545 crypto_unregister_ahash(&sha_hmac_algs
[i
]);
2547 for (i
= 0; i
< ARRAY_SIZE(sha_1_256_algs
); i
++)
2548 crypto_unregister_ahash(&sha_1_256_algs
[i
]);
2550 if (dd
->caps
.has_sha224
)
2551 crypto_unregister_ahash(&sha_224_alg
);
2553 if (dd
->caps
.has_sha_384_512
) {
2554 for (i
= 0; i
< ARRAY_SIZE(sha_384_512_algs
); i
++)
2555 crypto_unregister_ahash(&sha_384_512_algs
[i
]);
2559 static int atmel_sha_register_algs(struct atmel_sha_dev
*dd
)
2563 for (i
= 0; i
< ARRAY_SIZE(sha_1_256_algs
); i
++) {
2564 err
= crypto_register_ahash(&sha_1_256_algs
[i
]);
2566 goto err_sha_1_256_algs
;
2569 if (dd
->caps
.has_sha224
) {
2570 err
= crypto_register_ahash(&sha_224_alg
);
2572 goto err_sha_224_algs
;
2575 if (dd
->caps
.has_sha_384_512
) {
2576 for (i
= 0; i
< ARRAY_SIZE(sha_384_512_algs
); i
++) {
2577 err
= crypto_register_ahash(&sha_384_512_algs
[i
]);
2579 goto err_sha_384_512_algs
;
2583 if (dd
->caps
.has_hmac
) {
2584 for (i
= 0; i
< ARRAY_SIZE(sha_hmac_algs
); i
++) {
2585 err
= crypto_register_ahash(&sha_hmac_algs
[i
]);
2587 goto err_sha_hmac_algs
;
2593 /*i = ARRAY_SIZE(sha_hmac_algs);*/
2595 for (j
= 0; j
< i
; j
++)
2596 crypto_unregister_ahash(&sha_hmac_algs
[j
]);
2597 i
= ARRAY_SIZE(sha_384_512_algs
);
2598 err_sha_384_512_algs
:
2599 for (j
= 0; j
< i
; j
++)
2600 crypto_unregister_ahash(&sha_384_512_algs
[j
]);
2601 crypto_unregister_ahash(&sha_224_alg
);
2603 i
= ARRAY_SIZE(sha_1_256_algs
);
2605 for (j
= 0; j
< i
; j
++)
2606 crypto_unregister_ahash(&sha_1_256_algs
[j
]);
2611 static bool atmel_sha_filter(struct dma_chan
*chan
, void *slave
)
2613 struct at_dma_slave
*sl
= slave
;
2615 if (sl
&& sl
->dma_dev
== chan
->device
->dev
) {
2623 static int atmel_sha_dma_init(struct atmel_sha_dev
*dd
,
2624 struct crypto_platform_data
*pdata
)
2626 dma_cap_mask_t mask_in
;
2628 /* Try to grab DMA channel */
2629 dma_cap_zero(mask_in
);
2630 dma_cap_set(DMA_SLAVE
, mask_in
);
2632 dd
->dma_lch_in
.chan
= dma_request_slave_channel_compat(mask_in
,
2633 atmel_sha_filter
, &pdata
->dma_slave
->rxdata
, dd
->dev
, "tx");
2634 if (!dd
->dma_lch_in
.chan
) {
2635 dev_warn(dd
->dev
, "no DMA channel available\n");
2639 dd
->dma_lch_in
.dma_conf
.direction
= DMA_MEM_TO_DEV
;
2640 dd
->dma_lch_in
.dma_conf
.dst_addr
= dd
->phys_base
+
2642 dd
->dma_lch_in
.dma_conf
.src_maxburst
= 1;
2643 dd
->dma_lch_in
.dma_conf
.src_addr_width
=
2644 DMA_SLAVE_BUSWIDTH_4_BYTES
;
2645 dd
->dma_lch_in
.dma_conf
.dst_maxburst
= 1;
2646 dd
->dma_lch_in
.dma_conf
.dst_addr_width
=
2647 DMA_SLAVE_BUSWIDTH_4_BYTES
;
2648 dd
->dma_lch_in
.dma_conf
.device_fc
= false;
2653 static void atmel_sha_dma_cleanup(struct atmel_sha_dev
*dd
)
2655 dma_release_channel(dd
->dma_lch_in
.chan
);
2658 static void atmel_sha_get_cap(struct atmel_sha_dev
*dd
)
2661 dd
->caps
.has_dma
= 0;
2662 dd
->caps
.has_dualbuff
= 0;
2663 dd
->caps
.has_sha224
= 0;
2664 dd
->caps
.has_sha_384_512
= 0;
2665 dd
->caps
.has_uihv
= 0;
2666 dd
->caps
.has_hmac
= 0;
2668 /* keep only major version number */
2669 switch (dd
->hw_version
& 0xff0) {
2671 dd
->caps
.has_dma
= 1;
2672 dd
->caps
.has_dualbuff
= 1;
2673 dd
->caps
.has_sha224
= 1;
2674 dd
->caps
.has_sha_384_512
= 1;
2675 dd
->caps
.has_uihv
= 1;
2676 dd
->caps
.has_hmac
= 1;
2679 dd
->caps
.has_dma
= 1;
2680 dd
->caps
.has_dualbuff
= 1;
2681 dd
->caps
.has_sha224
= 1;
2682 dd
->caps
.has_sha_384_512
= 1;
2683 dd
->caps
.has_uihv
= 1;
2686 dd
->caps
.has_dma
= 1;
2687 dd
->caps
.has_dualbuff
= 1;
2688 dd
->caps
.has_sha224
= 1;
2689 dd
->caps
.has_sha_384_512
= 1;
2692 dd
->caps
.has_dma
= 1;
2693 dd
->caps
.has_dualbuff
= 1;
2694 dd
->caps
.has_sha224
= 1;
2700 "Unmanaged sha version, set minimum capabilities\n");
2705 #if defined(CONFIG_OF)
2706 static const struct of_device_id atmel_sha_dt_ids
[] = {
2707 { .compatible
= "atmel,at91sam9g46-sha" },
2711 MODULE_DEVICE_TABLE(of
, atmel_sha_dt_ids
);
2713 static struct crypto_platform_data
*atmel_sha_of_init(struct platform_device
*pdev
)
2715 struct device_node
*np
= pdev
->dev
.of_node
;
2716 struct crypto_platform_data
*pdata
;
2719 dev_err(&pdev
->dev
, "device node not found\n");
2720 return ERR_PTR(-EINVAL
);
2723 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
2725 return ERR_PTR(-ENOMEM
);
2727 pdata
->dma_slave
= devm_kzalloc(&pdev
->dev
,
2728 sizeof(*(pdata
->dma_slave
)),
2730 if (!pdata
->dma_slave
)
2731 return ERR_PTR(-ENOMEM
);
2735 #else /* CONFIG_OF */
2736 static inline struct crypto_platform_data
*atmel_sha_of_init(struct platform_device
*dev
)
2738 return ERR_PTR(-EINVAL
);
2742 static int atmel_sha_probe(struct platform_device
*pdev
)
2744 struct atmel_sha_dev
*sha_dd
;
2745 struct crypto_platform_data
*pdata
;
2746 struct device
*dev
= &pdev
->dev
;
2747 struct resource
*sha_res
;
2750 sha_dd
= devm_kzalloc(&pdev
->dev
, sizeof(*sha_dd
), GFP_KERNEL
);
2751 if (sha_dd
== NULL
) {
2758 platform_set_drvdata(pdev
, sha_dd
);
2760 INIT_LIST_HEAD(&sha_dd
->list
);
2761 spin_lock_init(&sha_dd
->lock
);
2763 tasklet_init(&sha_dd
->done_task
, atmel_sha_done_task
,
2764 (unsigned long)sha_dd
);
2765 tasklet_init(&sha_dd
->queue_task
, atmel_sha_queue_task
,
2766 (unsigned long)sha_dd
);
2768 crypto_init_queue(&sha_dd
->queue
, ATMEL_SHA_QUEUE_LENGTH
);
2770 /* Get the base address */
2771 sha_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2773 dev_err(dev
, "no MEM resource info\n");
2777 sha_dd
->phys_base
= sha_res
->start
;
2780 sha_dd
->irq
= platform_get_irq(pdev
, 0);
2781 if (sha_dd
->irq
< 0) {
2786 err
= devm_request_irq(&pdev
->dev
, sha_dd
->irq
, atmel_sha_irq
,
2787 IRQF_SHARED
, "atmel-sha", sha_dd
);
2789 dev_err(dev
, "unable to request sha irq.\n");
2793 /* Initializing the clock */
2794 sha_dd
->iclk
= devm_clk_get(&pdev
->dev
, "sha_clk");
2795 if (IS_ERR(sha_dd
->iclk
)) {
2796 dev_err(dev
, "clock initialization failed.\n");
2797 err
= PTR_ERR(sha_dd
->iclk
);
2801 sha_dd
->io_base
= devm_ioremap_resource(&pdev
->dev
, sha_res
);
2802 if (IS_ERR(sha_dd
->io_base
)) {
2803 dev_err(dev
, "can't ioremap\n");
2804 err
= PTR_ERR(sha_dd
->io_base
);
2808 err
= clk_prepare(sha_dd
->iclk
);
2812 atmel_sha_hw_version_init(sha_dd
);
2814 atmel_sha_get_cap(sha_dd
);
2816 if (sha_dd
->caps
.has_dma
) {
2817 pdata
= pdev
->dev
.platform_data
;
2819 pdata
= atmel_sha_of_init(pdev
);
2820 if (IS_ERR(pdata
)) {
2821 dev_err(&pdev
->dev
, "platform data not available\n");
2822 err
= PTR_ERR(pdata
);
2823 goto iclk_unprepare
;
2826 if (!pdata
->dma_slave
) {
2828 goto iclk_unprepare
;
2830 err
= atmel_sha_dma_init(sha_dd
, pdata
);
2834 dev_info(dev
, "using %s for DMA transfers\n",
2835 dma_chan_name(sha_dd
->dma_lch_in
.chan
));
2838 spin_lock(&atmel_sha
.lock
);
2839 list_add_tail(&sha_dd
->list
, &atmel_sha
.dev_list
);
2840 spin_unlock(&atmel_sha
.lock
);
2842 err
= atmel_sha_register_algs(sha_dd
);
2846 dev_info(dev
, "Atmel SHA1/SHA256%s%s\n",
2847 sha_dd
->caps
.has_sha224
? "/SHA224" : "",
2848 sha_dd
->caps
.has_sha_384_512
? "/SHA384/SHA512" : "");
2853 spin_lock(&atmel_sha
.lock
);
2854 list_del(&sha_dd
->list
);
2855 spin_unlock(&atmel_sha
.lock
);
2856 if (sha_dd
->caps
.has_dma
)
2857 atmel_sha_dma_cleanup(sha_dd
);
2860 clk_unprepare(sha_dd
->iclk
);
2862 tasklet_kill(&sha_dd
->queue_task
);
2863 tasklet_kill(&sha_dd
->done_task
);
2865 dev_err(dev
, "initialization failed.\n");
2870 static int atmel_sha_remove(struct platform_device
*pdev
)
2872 struct atmel_sha_dev
*sha_dd
;
2874 sha_dd
= platform_get_drvdata(pdev
);
2877 spin_lock(&atmel_sha
.lock
);
2878 list_del(&sha_dd
->list
);
2879 spin_unlock(&atmel_sha
.lock
);
2881 atmel_sha_unregister_algs(sha_dd
);
2883 tasklet_kill(&sha_dd
->queue_task
);
2884 tasklet_kill(&sha_dd
->done_task
);
2886 if (sha_dd
->caps
.has_dma
)
2887 atmel_sha_dma_cleanup(sha_dd
);
2889 clk_unprepare(sha_dd
->iclk
);
2894 static struct platform_driver atmel_sha_driver
= {
2895 .probe
= atmel_sha_probe
,
2896 .remove
= atmel_sha_remove
,
2898 .name
= "atmel_sha",
2899 .of_match_table
= of_match_ptr(atmel_sha_dt_ids
),
2903 module_platform_driver(atmel_sha_driver
);
2905 MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
2906 MODULE_LICENSE("GPL v2");
2907 MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");