4 * Support for ATMEL SHA1/SHA256 HW acceleration.
6 * Copyright (c) 2012 Eukréa Electromatique - ATMEL
7 * Author: Nicolas Royer <nicolas@eukrea.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
13 * Some ideas are from omap-sham.c drivers.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/err.h>
21 #include <linux/clk.h>
23 #include <linux/hw_random.h>
24 #include <linux/platform_device.h>
26 #include <linux/device.h>
27 #include <linux/init.h>
28 #include <linux/errno.h>
29 #include <linux/interrupt.h>
30 #include <linux/irq.h>
31 #include <linux/scatterlist.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/of_device.h>
34 #include <linux/delay.h>
35 #include <linux/crypto.h>
36 #include <linux/cryptohash.h>
37 #include <crypto/scatterwalk.h>
38 #include <crypto/algapi.h>
39 #include <crypto/sha.h>
40 #include <crypto/hash.h>
41 #include <crypto/internal/hash.h>
42 #include <linux/platform_data/crypto-atmel.h>
43 #include "atmel-sha-regs.h"
44 #include "atmel-authenc.h"
47 #define SHA_FLAGS_BUSY BIT(0)
48 #define SHA_FLAGS_FINAL BIT(1)
49 #define SHA_FLAGS_DMA_ACTIVE BIT(2)
50 #define SHA_FLAGS_OUTPUT_READY BIT(3)
51 #define SHA_FLAGS_INIT BIT(4)
52 #define SHA_FLAGS_CPU BIT(5)
53 #define SHA_FLAGS_DMA_READY BIT(6)
54 #define SHA_FLAGS_DUMP_REG BIT(7)
56 /* bits[11:8] are reserved. */
58 #define SHA_FLAGS_FINUP BIT(16)
59 #define SHA_FLAGS_SG BIT(17)
60 #define SHA_FLAGS_ERROR BIT(23)
61 #define SHA_FLAGS_PAD BIT(24)
62 #define SHA_FLAGS_RESTORE BIT(25)
63 #define SHA_FLAGS_IDATAR0 BIT(26)
64 #define SHA_FLAGS_WAIT_DATARDY BIT(27)
67 #define SHA_OP_UPDATE 1
68 #define SHA_OP_FINAL 2
69 #define SHA_OP_DIGEST 3
71 #define SHA_BUFFER_LEN (PAGE_SIZE / 16)
73 #define ATMEL_SHA_DMA_THRESHOLD 56
75 struct atmel_sha_caps
{
87 * .statesize = sizeof(struct atmel_sha_reqctx) must be <= PAGE_SIZE / 8 as
88 * tested by the ahash_prepare_alg() function.
90 struct atmel_sha_reqctx
{
91 struct atmel_sha_dev
*dd
;
95 u8 digest
[SHA512_DIGEST_SIZE
] __aligned(sizeof(u32
));
102 struct scatterlist
*sg
;
103 unsigned int offset
; /* offset in current sg */
104 unsigned int total
; /* total request */
109 u8 buffer
[SHA_BUFFER_LEN
+ SHA512_BLOCK_SIZE
] __aligned(sizeof(u32
));
112 typedef int (*atmel_sha_fn_t
)(struct atmel_sha_dev
*);
114 struct atmel_sha_ctx
{
115 struct atmel_sha_dev
*dd
;
116 atmel_sha_fn_t start
;
121 #define ATMEL_SHA_QUEUE_LENGTH 50
123 struct atmel_sha_dma
{
124 struct dma_chan
*chan
;
125 struct dma_slave_config dma_conf
;
126 struct scatterlist
*sg
;
128 unsigned int last_sg_length
;
131 struct atmel_sha_dev
{
132 struct list_head list
;
133 unsigned long phys_base
;
137 void __iomem
*io_base
;
141 struct tasklet_struct done_task
;
142 struct tasklet_struct queue_task
;
145 struct crypto_queue queue
;
146 struct ahash_request
*req
;
149 atmel_sha_fn_t resume
;
150 atmel_sha_fn_t cpu_transfer_complete
;
152 struct atmel_sha_dma dma_lch_in
;
154 struct atmel_sha_caps caps
;
156 struct scatterlist tmp
;
161 struct atmel_sha_drv
{
162 struct list_head dev_list
;
166 static struct atmel_sha_drv atmel_sha
= {
167 .dev_list
= LIST_HEAD_INIT(atmel_sha
.dev_list
),
168 .lock
= __SPIN_LOCK_UNLOCKED(atmel_sha
.lock
),
172 static const char *atmel_sha_reg_name(u32 offset
, char *tmp
, size_t sz
, bool wr
)
209 case SHA_REG_DIN(10):
210 case SHA_REG_DIN(11):
211 case SHA_REG_DIN(12):
212 case SHA_REG_DIN(13):
213 case SHA_REG_DIN(14):
214 case SHA_REG_DIN(15):
215 snprintf(tmp
, sz
, "IDATAR[%u]", (offset
- SHA_REG_DIN(0)) >> 2);
218 case SHA_REG_DIGEST(0):
219 case SHA_REG_DIGEST(1):
220 case SHA_REG_DIGEST(2):
221 case SHA_REG_DIGEST(3):
222 case SHA_REG_DIGEST(4):
223 case SHA_REG_DIGEST(5):
224 case SHA_REG_DIGEST(6):
225 case SHA_REG_DIGEST(7):
226 case SHA_REG_DIGEST(8):
227 case SHA_REG_DIGEST(9):
228 case SHA_REG_DIGEST(10):
229 case SHA_REG_DIGEST(11):
230 case SHA_REG_DIGEST(12):
231 case SHA_REG_DIGEST(13):
232 case SHA_REG_DIGEST(14):
233 case SHA_REG_DIGEST(15):
235 snprintf(tmp
, sz
, "IDATAR[%u]",
236 16u + ((offset
- SHA_REG_DIGEST(0)) >> 2));
238 snprintf(tmp
, sz
, "ODATAR[%u]",
239 (offset
- SHA_REG_DIGEST(0)) >> 2);
246 snprintf(tmp
, sz
, "0x%02x", offset
);
253 #endif /* VERBOSE_DEBUG */
255 static inline u32
atmel_sha_read(struct atmel_sha_dev
*dd
, u32 offset
)
257 u32 value
= readl_relaxed(dd
->io_base
+ offset
);
260 if (dd
->flags
& SHA_FLAGS_DUMP_REG
) {
263 dev_vdbg(dd
->dev
, "read 0x%08x from %s\n", value
,
264 atmel_sha_reg_name(offset
, tmp
, sizeof(tmp
), false));
266 #endif /* VERBOSE_DEBUG */
271 static inline void atmel_sha_write(struct atmel_sha_dev
*dd
,
272 u32 offset
, u32 value
)
275 if (dd
->flags
& SHA_FLAGS_DUMP_REG
) {
278 dev_vdbg(dd
->dev
, "write 0x%08x into %s\n", value
,
279 atmel_sha_reg_name(offset
, tmp
, sizeof(tmp
), true));
281 #endif /* VERBOSE_DEBUG */
283 writel_relaxed(value
, dd
->io_base
+ offset
);
286 static inline int atmel_sha_complete(struct atmel_sha_dev
*dd
, int err
)
288 struct ahash_request
*req
= dd
->req
;
290 dd
->flags
&= ~(SHA_FLAGS_BUSY
| SHA_FLAGS_FINAL
| SHA_FLAGS_CPU
|
291 SHA_FLAGS_DMA_READY
| SHA_FLAGS_OUTPUT_READY
|
294 clk_disable(dd
->iclk
);
296 if ((dd
->is_async
|| dd
->force_complete
) && req
->base
.complete
)
297 req
->base
.complete(&req
->base
, err
);
299 /* handle new request */
300 tasklet_schedule(&dd
->queue_task
);
305 static size_t atmel_sha_append_sg(struct atmel_sha_reqctx
*ctx
)
309 while ((ctx
->bufcnt
< ctx
->buflen
) && ctx
->total
) {
310 count
= min(ctx
->sg
->length
- ctx
->offset
, ctx
->total
);
311 count
= min(count
, ctx
->buflen
- ctx
->bufcnt
);
315 * Check if count <= 0 because the buffer is full or
316 * because the sg length is 0. In the latest case,
317 * check if there is another sg in the list, a 0 length
318 * sg doesn't necessarily mean the end of the sg list.
320 if ((ctx
->sg
->length
== 0) && !sg_is_last(ctx
->sg
)) {
321 ctx
->sg
= sg_next(ctx
->sg
);
328 scatterwalk_map_and_copy(ctx
->buffer
+ ctx
->bufcnt
, ctx
->sg
,
329 ctx
->offset
, count
, 0);
331 ctx
->bufcnt
+= count
;
332 ctx
->offset
+= count
;
335 if (ctx
->offset
== ctx
->sg
->length
) {
336 ctx
->sg
= sg_next(ctx
->sg
);
348 * The purpose of this padding is to ensure that the padded message is a
349 * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512).
350 * The bit "1" is appended at the end of the message followed by
351 * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or
352 * 128 bits block (SHA384/SHA512) equals to the message length in bits
355 * For SHA1/SHA224/SHA256, padlen is calculated as followed:
356 * - if message length < 56 bytes then padlen = 56 - message length
357 * - else padlen = 64 + 56 - message length
359 * For SHA384/SHA512, padlen is calculated as followed:
360 * - if message length < 112 bytes then padlen = 112 - message length
361 * - else padlen = 128 + 112 - message length
363 static void atmel_sha_fill_padding(struct atmel_sha_reqctx
*ctx
, int length
)
365 unsigned int index
, padlen
;
369 size
[0] = ctx
->digcnt
[0];
370 size
[1] = ctx
->digcnt
[1];
372 size
[0] += ctx
->bufcnt
;
373 if (size
[0] < ctx
->bufcnt
)
377 if (size
[0] < length
)
380 bits
[1] = cpu_to_be64(size
[0] << 3);
381 bits
[0] = cpu_to_be64(size
[1] << 3 | size
[0] >> 61);
383 switch (ctx
->flags
& SHA_FLAGS_ALGO_MASK
) {
384 case SHA_FLAGS_SHA384
:
385 case SHA_FLAGS_SHA512
:
386 index
= ctx
->bufcnt
& 0x7f;
387 padlen
= (index
< 112) ? (112 - index
) : ((128+112) - index
);
388 *(ctx
->buffer
+ ctx
->bufcnt
) = 0x80;
389 memset(ctx
->buffer
+ ctx
->bufcnt
+ 1, 0, padlen
-1);
390 memcpy(ctx
->buffer
+ ctx
->bufcnt
+ padlen
, bits
, 16);
391 ctx
->bufcnt
+= padlen
+ 16;
392 ctx
->flags
|= SHA_FLAGS_PAD
;
396 index
= ctx
->bufcnt
& 0x3f;
397 padlen
= (index
< 56) ? (56 - index
) : ((64+56) - index
);
398 *(ctx
->buffer
+ ctx
->bufcnt
) = 0x80;
399 memset(ctx
->buffer
+ ctx
->bufcnt
+ 1, 0, padlen
-1);
400 memcpy(ctx
->buffer
+ ctx
->bufcnt
+ padlen
, &bits
[1], 8);
401 ctx
->bufcnt
+= padlen
+ 8;
402 ctx
->flags
|= SHA_FLAGS_PAD
;
407 static struct atmel_sha_dev
*atmel_sha_find_dev(struct atmel_sha_ctx
*tctx
)
409 struct atmel_sha_dev
*dd
= NULL
;
410 struct atmel_sha_dev
*tmp
;
412 spin_lock_bh(&atmel_sha
.lock
);
414 list_for_each_entry(tmp
, &atmel_sha
.dev_list
, list
) {
423 spin_unlock_bh(&atmel_sha
.lock
);
428 static int atmel_sha_init(struct ahash_request
*req
)
430 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
431 struct atmel_sha_ctx
*tctx
= crypto_ahash_ctx(tfm
);
432 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
433 struct atmel_sha_dev
*dd
= atmel_sha_find_dev(tctx
);
439 dev_dbg(dd
->dev
, "init: digest size: %d\n",
440 crypto_ahash_digestsize(tfm
));
442 switch (crypto_ahash_digestsize(tfm
)) {
443 case SHA1_DIGEST_SIZE
:
444 ctx
->flags
|= SHA_FLAGS_SHA1
;
445 ctx
->block_size
= SHA1_BLOCK_SIZE
;
447 case SHA224_DIGEST_SIZE
:
448 ctx
->flags
|= SHA_FLAGS_SHA224
;
449 ctx
->block_size
= SHA224_BLOCK_SIZE
;
451 case SHA256_DIGEST_SIZE
:
452 ctx
->flags
|= SHA_FLAGS_SHA256
;
453 ctx
->block_size
= SHA256_BLOCK_SIZE
;
455 case SHA384_DIGEST_SIZE
:
456 ctx
->flags
|= SHA_FLAGS_SHA384
;
457 ctx
->block_size
= SHA384_BLOCK_SIZE
;
459 case SHA512_DIGEST_SIZE
:
460 ctx
->flags
|= SHA_FLAGS_SHA512
;
461 ctx
->block_size
= SHA512_BLOCK_SIZE
;
471 ctx
->buflen
= SHA_BUFFER_LEN
;
476 static void atmel_sha_write_ctrl(struct atmel_sha_dev
*dd
, int dma
)
478 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
479 u32 valmr
= SHA_MR_MODE_AUTO
;
480 unsigned int i
, hashsize
= 0;
483 if (!dd
->caps
.has_dma
)
484 atmel_sha_write(dd
, SHA_IER
, SHA_INT_TXBUFE
);
485 valmr
= SHA_MR_MODE_PDC
;
486 if (dd
->caps
.has_dualbuff
)
487 valmr
|= SHA_MR_DUALBUFF
;
489 atmel_sha_write(dd
, SHA_IER
, SHA_INT_DATARDY
);
492 switch (ctx
->flags
& SHA_FLAGS_ALGO_MASK
) {
494 valmr
|= SHA_MR_ALGO_SHA1
;
495 hashsize
= SHA1_DIGEST_SIZE
;
498 case SHA_FLAGS_SHA224
:
499 valmr
|= SHA_MR_ALGO_SHA224
;
500 hashsize
= SHA256_DIGEST_SIZE
;
503 case SHA_FLAGS_SHA256
:
504 valmr
|= SHA_MR_ALGO_SHA256
;
505 hashsize
= SHA256_DIGEST_SIZE
;
508 case SHA_FLAGS_SHA384
:
509 valmr
|= SHA_MR_ALGO_SHA384
;
510 hashsize
= SHA512_DIGEST_SIZE
;
513 case SHA_FLAGS_SHA512
:
514 valmr
|= SHA_MR_ALGO_SHA512
;
515 hashsize
= SHA512_DIGEST_SIZE
;
522 /* Setting CR_FIRST only for the first iteration */
523 if (!(ctx
->digcnt
[0] || ctx
->digcnt
[1])) {
524 atmel_sha_write(dd
, SHA_CR
, SHA_CR_FIRST
);
525 } else if (dd
->caps
.has_uihv
&& (ctx
->flags
& SHA_FLAGS_RESTORE
)) {
526 const u32
*hash
= (const u32
*)ctx
->digest
;
529 * Restore the hardware context: update the User Initialize
530 * Hash Value (UIHV) with the value saved when the latest
531 * 'update' operation completed on this very same crypto
534 ctx
->flags
&= ~SHA_FLAGS_RESTORE
;
535 atmel_sha_write(dd
, SHA_CR
, SHA_CR_WUIHV
);
536 for (i
= 0; i
< hashsize
/ sizeof(u32
); ++i
)
537 atmel_sha_write(dd
, SHA_REG_DIN(i
), hash
[i
]);
538 atmel_sha_write(dd
, SHA_CR
, SHA_CR_FIRST
);
539 valmr
|= SHA_MR_UIHV
;
542 * WARNING: If the UIHV feature is not available, the hardware CANNOT
543 * process concurrent requests: the internal registers used to store
544 * the hash/digest are still set to the partial digest output values
545 * computed during the latest round.
548 atmel_sha_write(dd
, SHA_MR
, valmr
);
551 static inline int atmel_sha_wait_for_data_ready(struct atmel_sha_dev
*dd
,
552 atmel_sha_fn_t resume
)
554 u32 isr
= atmel_sha_read(dd
, SHA_ISR
);
556 if (unlikely(isr
& SHA_INT_DATARDY
))
560 atmel_sha_write(dd
, SHA_IER
, SHA_INT_DATARDY
);
564 static int atmel_sha_xmit_cpu(struct atmel_sha_dev
*dd
, const u8
*buf
,
565 size_t length
, int final
)
567 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
569 const u32
*buffer
= (const u32
*)buf
;
571 dev_dbg(dd
->dev
, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
572 ctx
->digcnt
[1], ctx
->digcnt
[0], length
, final
);
574 atmel_sha_write_ctrl(dd
, 0);
576 /* should be non-zero before next lines to disable clocks later */
577 ctx
->digcnt
[0] += length
;
578 if (ctx
->digcnt
[0] < length
)
582 dd
->flags
|= SHA_FLAGS_FINAL
; /* catch last interrupt */
584 len32
= DIV_ROUND_UP(length
, sizeof(u32
));
586 dd
->flags
|= SHA_FLAGS_CPU
;
588 for (count
= 0; count
< len32
; count
++)
589 atmel_sha_write(dd
, SHA_REG_DIN(count
), buffer
[count
]);
594 static int atmel_sha_xmit_pdc(struct atmel_sha_dev
*dd
, dma_addr_t dma_addr1
,
595 size_t length1
, dma_addr_t dma_addr2
, size_t length2
, int final
)
597 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
600 dev_dbg(dd
->dev
, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
601 ctx
->digcnt
[1], ctx
->digcnt
[0], length1
, final
);
603 len32
= DIV_ROUND_UP(length1
, sizeof(u32
));
604 atmel_sha_write(dd
, SHA_PTCR
, SHA_PTCR_TXTDIS
);
605 atmel_sha_write(dd
, SHA_TPR
, dma_addr1
);
606 atmel_sha_write(dd
, SHA_TCR
, len32
);
608 len32
= DIV_ROUND_UP(length2
, sizeof(u32
));
609 atmel_sha_write(dd
, SHA_TNPR
, dma_addr2
);
610 atmel_sha_write(dd
, SHA_TNCR
, len32
);
612 atmel_sha_write_ctrl(dd
, 1);
614 /* should be non-zero before next lines to disable clocks later */
615 ctx
->digcnt
[0] += length1
;
616 if (ctx
->digcnt
[0] < length1
)
620 dd
->flags
|= SHA_FLAGS_FINAL
; /* catch last interrupt */
622 dd
->flags
|= SHA_FLAGS_DMA_ACTIVE
;
624 /* Start DMA transfer */
625 atmel_sha_write(dd
, SHA_PTCR
, SHA_PTCR_TXTEN
);
630 static void atmel_sha_dma_callback(void *data
)
632 struct atmel_sha_dev
*dd
= data
;
636 /* dma_lch_in - completed - wait DATRDY */
637 atmel_sha_write(dd
, SHA_IER
, SHA_INT_DATARDY
);
640 static int atmel_sha_xmit_dma(struct atmel_sha_dev
*dd
, dma_addr_t dma_addr1
,
641 size_t length1
, dma_addr_t dma_addr2
, size_t length2
, int final
)
643 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
644 struct dma_async_tx_descriptor
*in_desc
;
645 struct scatterlist sg
[2];
647 dev_dbg(dd
->dev
, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %zd, final: %d\n",
648 ctx
->digcnt
[1], ctx
->digcnt
[0], length1
, final
);
650 dd
->dma_lch_in
.dma_conf
.src_maxburst
= 16;
651 dd
->dma_lch_in
.dma_conf
.dst_maxburst
= 16;
653 dmaengine_slave_config(dd
->dma_lch_in
.chan
, &dd
->dma_lch_in
.dma_conf
);
656 sg_init_table(sg
, 2);
657 sg_dma_address(&sg
[0]) = dma_addr1
;
658 sg_dma_len(&sg
[0]) = length1
;
659 sg_dma_address(&sg
[1]) = dma_addr2
;
660 sg_dma_len(&sg
[1]) = length2
;
661 in_desc
= dmaengine_prep_slave_sg(dd
->dma_lch_in
.chan
, sg
, 2,
662 DMA_MEM_TO_DEV
, DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
664 sg_init_table(sg
, 1);
665 sg_dma_address(&sg
[0]) = dma_addr1
;
666 sg_dma_len(&sg
[0]) = length1
;
667 in_desc
= dmaengine_prep_slave_sg(dd
->dma_lch_in
.chan
, sg
, 1,
668 DMA_MEM_TO_DEV
, DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
671 return atmel_sha_complete(dd
, -EINVAL
);
673 in_desc
->callback
= atmel_sha_dma_callback
;
674 in_desc
->callback_param
= dd
;
676 atmel_sha_write_ctrl(dd
, 1);
678 /* should be non-zero before next lines to disable clocks later */
679 ctx
->digcnt
[0] += length1
;
680 if (ctx
->digcnt
[0] < length1
)
684 dd
->flags
|= SHA_FLAGS_FINAL
; /* catch last interrupt */
686 dd
->flags
|= SHA_FLAGS_DMA_ACTIVE
;
688 /* Start DMA transfer */
689 dmaengine_submit(in_desc
);
690 dma_async_issue_pending(dd
->dma_lch_in
.chan
);
695 static int atmel_sha_xmit_start(struct atmel_sha_dev
*dd
, dma_addr_t dma_addr1
,
696 size_t length1
, dma_addr_t dma_addr2
, size_t length2
, int final
)
698 if (dd
->caps
.has_dma
)
699 return atmel_sha_xmit_dma(dd
, dma_addr1
, length1
,
700 dma_addr2
, length2
, final
);
702 return atmel_sha_xmit_pdc(dd
, dma_addr1
, length1
,
703 dma_addr2
, length2
, final
);
706 static int atmel_sha_update_cpu(struct atmel_sha_dev
*dd
)
708 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
711 atmel_sha_append_sg(ctx
);
712 atmel_sha_fill_padding(ctx
, 0);
713 bufcnt
= ctx
->bufcnt
;
716 return atmel_sha_xmit_cpu(dd
, ctx
->buffer
, bufcnt
, 1);
719 static int atmel_sha_xmit_dma_map(struct atmel_sha_dev
*dd
,
720 struct atmel_sha_reqctx
*ctx
,
721 size_t length
, int final
)
723 ctx
->dma_addr
= dma_map_single(dd
->dev
, ctx
->buffer
,
724 ctx
->buflen
+ ctx
->block_size
, DMA_TO_DEVICE
);
725 if (dma_mapping_error(dd
->dev
, ctx
->dma_addr
)) {
726 dev_err(dd
->dev
, "dma %zu bytes error\n", ctx
->buflen
+
728 return atmel_sha_complete(dd
, -EINVAL
);
731 ctx
->flags
&= ~SHA_FLAGS_SG
;
733 /* next call does not fail... so no unmap in the case of error */
734 return atmel_sha_xmit_start(dd
, ctx
->dma_addr
, length
, 0, 0, final
);
737 static int atmel_sha_update_dma_slow(struct atmel_sha_dev
*dd
)
739 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
743 atmel_sha_append_sg(ctx
);
745 final
= (ctx
->flags
& SHA_FLAGS_FINUP
) && !ctx
->total
;
747 dev_dbg(dd
->dev
, "slow: bufcnt: %zu, digcnt: 0x%llx 0x%llx, final: %d\n",
748 ctx
->bufcnt
, ctx
->digcnt
[1], ctx
->digcnt
[0], final
);
751 atmel_sha_fill_padding(ctx
, 0);
753 if (final
|| (ctx
->bufcnt
== ctx
->buflen
)) {
756 return atmel_sha_xmit_dma_map(dd
, ctx
, count
, final
);
762 static int atmel_sha_update_dma_start(struct atmel_sha_dev
*dd
)
764 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
765 unsigned int length
, final
, tail
;
766 struct scatterlist
*sg
;
772 if (ctx
->bufcnt
|| ctx
->offset
)
773 return atmel_sha_update_dma_slow(dd
);
775 dev_dbg(dd
->dev
, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %zd, total: %u\n",
776 ctx
->digcnt
[1], ctx
->digcnt
[0], ctx
->bufcnt
, ctx
->total
);
780 if (!IS_ALIGNED(sg
->offset
, sizeof(u32
)))
781 return atmel_sha_update_dma_slow(dd
);
783 if (!sg_is_last(sg
) && !IS_ALIGNED(sg
->length
, ctx
->block_size
))
784 /* size is not ctx->block_size aligned */
785 return atmel_sha_update_dma_slow(dd
);
787 length
= min(ctx
->total
, sg
->length
);
789 if (sg_is_last(sg
)) {
790 if (!(ctx
->flags
& SHA_FLAGS_FINUP
)) {
791 /* not last sg must be ctx->block_size aligned */
792 tail
= length
& (ctx
->block_size
- 1);
797 ctx
->total
-= length
;
798 ctx
->offset
= length
; /* offset where to start slow */
800 final
= (ctx
->flags
& SHA_FLAGS_FINUP
) && !ctx
->total
;
804 tail
= length
& (ctx
->block_size
- 1);
807 ctx
->offset
= length
; /* offset where to start slow */
810 atmel_sha_append_sg(ctx
);
812 atmel_sha_fill_padding(ctx
, length
);
814 ctx
->dma_addr
= dma_map_single(dd
->dev
, ctx
->buffer
,
815 ctx
->buflen
+ ctx
->block_size
, DMA_TO_DEVICE
);
816 if (dma_mapping_error(dd
->dev
, ctx
->dma_addr
)) {
817 dev_err(dd
->dev
, "dma %zu bytes error\n",
818 ctx
->buflen
+ ctx
->block_size
);
819 return atmel_sha_complete(dd
, -EINVAL
);
823 ctx
->flags
&= ~SHA_FLAGS_SG
;
826 return atmel_sha_xmit_start(dd
, ctx
->dma_addr
, count
, 0,
830 if (!dma_map_sg(dd
->dev
, ctx
->sg
, 1,
832 dev_err(dd
->dev
, "dma_map_sg error\n");
833 return atmel_sha_complete(dd
, -EINVAL
);
836 ctx
->flags
|= SHA_FLAGS_SG
;
840 return atmel_sha_xmit_start(dd
, sg_dma_address(ctx
->sg
),
841 length
, ctx
->dma_addr
, count
, final
);
845 if (!dma_map_sg(dd
->dev
, ctx
->sg
, 1, DMA_TO_DEVICE
)) {
846 dev_err(dd
->dev
, "dma_map_sg error\n");
847 return atmel_sha_complete(dd
, -EINVAL
);
850 ctx
->flags
|= SHA_FLAGS_SG
;
852 /* next call does not fail... so no unmap in the case of error */
853 return atmel_sha_xmit_start(dd
, sg_dma_address(ctx
->sg
), length
, 0,
857 static int atmel_sha_update_dma_stop(struct atmel_sha_dev
*dd
)
859 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(dd
->req
);
861 if (ctx
->flags
& SHA_FLAGS_SG
) {
862 dma_unmap_sg(dd
->dev
, ctx
->sg
, 1, DMA_TO_DEVICE
);
863 if (ctx
->sg
->length
== ctx
->offset
) {
864 ctx
->sg
= sg_next(ctx
->sg
);
868 if (ctx
->flags
& SHA_FLAGS_PAD
) {
869 dma_unmap_single(dd
->dev
, ctx
->dma_addr
,
870 ctx
->buflen
+ ctx
->block_size
, DMA_TO_DEVICE
);
873 dma_unmap_single(dd
->dev
, ctx
->dma_addr
, ctx
->buflen
+
874 ctx
->block_size
, DMA_TO_DEVICE
);
880 static int atmel_sha_update_req(struct atmel_sha_dev
*dd
)
882 struct ahash_request
*req
= dd
->req
;
883 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
886 dev_dbg(dd
->dev
, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n",
887 ctx
->total
, ctx
->digcnt
[1], ctx
->digcnt
[0]);
889 if (ctx
->flags
& SHA_FLAGS_CPU
)
890 err
= atmel_sha_update_cpu(dd
);
892 err
= atmel_sha_update_dma_start(dd
);
894 /* wait for dma completion before can take more data */
895 dev_dbg(dd
->dev
, "update: err: %d, digcnt: 0x%llx 0%llx\n",
896 err
, ctx
->digcnt
[1], ctx
->digcnt
[0]);
901 static int atmel_sha_final_req(struct atmel_sha_dev
*dd
)
903 struct ahash_request
*req
= dd
->req
;
904 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
908 if (ctx
->bufcnt
>= ATMEL_SHA_DMA_THRESHOLD
) {
909 atmel_sha_fill_padding(ctx
, 0);
912 err
= atmel_sha_xmit_dma_map(dd
, ctx
, count
, 1);
914 /* faster to handle last block with cpu */
916 atmel_sha_fill_padding(ctx
, 0);
919 err
= atmel_sha_xmit_cpu(dd
, ctx
->buffer
, count
, 1);
922 dev_dbg(dd
->dev
, "final_req: err: %d\n", err
);
927 static void atmel_sha_copy_hash(struct ahash_request
*req
)
929 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
930 u32
*hash
= (u32
*)ctx
->digest
;
931 unsigned int i
, hashsize
;
933 switch (ctx
->flags
& SHA_FLAGS_ALGO_MASK
) {
935 hashsize
= SHA1_DIGEST_SIZE
;
938 case SHA_FLAGS_SHA224
:
939 case SHA_FLAGS_SHA256
:
940 hashsize
= SHA256_DIGEST_SIZE
;
943 case SHA_FLAGS_SHA384
:
944 case SHA_FLAGS_SHA512
:
945 hashsize
= SHA512_DIGEST_SIZE
;
949 /* Should not happen... */
953 for (i
= 0; i
< hashsize
/ sizeof(u32
); ++i
)
954 hash
[i
] = atmel_sha_read(ctx
->dd
, SHA_REG_DIGEST(i
));
955 ctx
->flags
|= SHA_FLAGS_RESTORE
;
958 static void atmel_sha_copy_ready_hash(struct ahash_request
*req
)
960 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
965 switch (ctx
->flags
& SHA_FLAGS_ALGO_MASK
) {
968 memcpy(req
->result
, ctx
->digest
, SHA1_DIGEST_SIZE
);
971 case SHA_FLAGS_SHA224
:
972 memcpy(req
->result
, ctx
->digest
, SHA224_DIGEST_SIZE
);
975 case SHA_FLAGS_SHA256
:
976 memcpy(req
->result
, ctx
->digest
, SHA256_DIGEST_SIZE
);
979 case SHA_FLAGS_SHA384
:
980 memcpy(req
->result
, ctx
->digest
, SHA384_DIGEST_SIZE
);
983 case SHA_FLAGS_SHA512
:
984 memcpy(req
->result
, ctx
->digest
, SHA512_DIGEST_SIZE
);
989 static int atmel_sha_finish(struct ahash_request
*req
)
991 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
992 struct atmel_sha_dev
*dd
= ctx
->dd
;
994 if (ctx
->digcnt
[0] || ctx
->digcnt
[1])
995 atmel_sha_copy_ready_hash(req
);
997 dev_dbg(dd
->dev
, "digcnt: 0x%llx 0x%llx, bufcnt: %zd\n", ctx
->digcnt
[1],
998 ctx
->digcnt
[0], ctx
->bufcnt
);
1003 static void atmel_sha_finish_req(struct ahash_request
*req
, int err
)
1005 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1006 struct atmel_sha_dev
*dd
= ctx
->dd
;
1009 atmel_sha_copy_hash(req
);
1010 if (SHA_FLAGS_FINAL
& dd
->flags
)
1011 err
= atmel_sha_finish(req
);
1013 ctx
->flags
|= SHA_FLAGS_ERROR
;
1016 /* atomic operation is not needed here */
1017 (void)atmel_sha_complete(dd
, err
);
1020 static int atmel_sha_hw_init(struct atmel_sha_dev
*dd
)
1024 err
= clk_enable(dd
->iclk
);
1028 if (!(SHA_FLAGS_INIT
& dd
->flags
)) {
1029 atmel_sha_write(dd
, SHA_CR
, SHA_CR_SWRST
);
1030 dd
->flags
|= SHA_FLAGS_INIT
;
1037 static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev
*dd
)
1039 return atmel_sha_read(dd
, SHA_HW_VERSION
) & 0x00000fff;
1042 static void atmel_sha_hw_version_init(struct atmel_sha_dev
*dd
)
1044 atmel_sha_hw_init(dd
);
1046 dd
->hw_version
= atmel_sha_get_version(dd
);
1049 "version: 0x%x\n", dd
->hw_version
);
1051 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 backlog
->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: %d\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 struct ahash_alg sha_1_256_algs
[] = {
1256 .init
= atmel_sha_init
,
1257 .update
= atmel_sha_update
,
1258 .final
= atmel_sha_final
,
1259 .finup
= atmel_sha_finup
,
1260 .digest
= atmel_sha_digest
,
1261 .export
= atmel_sha_export
,
1262 .import
= atmel_sha_import
,
1264 .digestsize
= SHA1_DIGEST_SIZE
,
1265 .statesize
= sizeof(struct atmel_sha_reqctx
),
1268 .cra_driver_name
= "atmel-sha1",
1269 .cra_priority
= 100,
1270 .cra_flags
= CRYPTO_ALG_ASYNC
,
1271 .cra_blocksize
= SHA1_BLOCK_SIZE
,
1272 .cra_ctxsize
= sizeof(struct atmel_sha_ctx
),
1274 .cra_module
= THIS_MODULE
,
1275 .cra_init
= atmel_sha_cra_init
,
1280 .init
= atmel_sha_init
,
1281 .update
= atmel_sha_update
,
1282 .final
= atmel_sha_final
,
1283 .finup
= atmel_sha_finup
,
1284 .digest
= atmel_sha_digest
,
1285 .export
= atmel_sha_export
,
1286 .import
= atmel_sha_import
,
1288 .digestsize
= SHA256_DIGEST_SIZE
,
1289 .statesize
= sizeof(struct atmel_sha_reqctx
),
1291 .cra_name
= "sha256",
1292 .cra_driver_name
= "atmel-sha256",
1293 .cra_priority
= 100,
1294 .cra_flags
= CRYPTO_ALG_ASYNC
,
1295 .cra_blocksize
= SHA256_BLOCK_SIZE
,
1296 .cra_ctxsize
= sizeof(struct atmel_sha_ctx
),
1298 .cra_module
= THIS_MODULE
,
1299 .cra_init
= atmel_sha_cra_init
,
1305 static struct ahash_alg sha_224_alg
= {
1306 .init
= atmel_sha_init
,
1307 .update
= atmel_sha_update
,
1308 .final
= atmel_sha_final
,
1309 .finup
= atmel_sha_finup
,
1310 .digest
= atmel_sha_digest
,
1311 .export
= atmel_sha_export
,
1312 .import
= atmel_sha_import
,
1314 .digestsize
= SHA224_DIGEST_SIZE
,
1315 .statesize
= sizeof(struct atmel_sha_reqctx
),
1317 .cra_name
= "sha224",
1318 .cra_driver_name
= "atmel-sha224",
1319 .cra_priority
= 100,
1320 .cra_flags
= CRYPTO_ALG_ASYNC
,
1321 .cra_blocksize
= SHA224_BLOCK_SIZE
,
1322 .cra_ctxsize
= sizeof(struct atmel_sha_ctx
),
1324 .cra_module
= THIS_MODULE
,
1325 .cra_init
= atmel_sha_cra_init
,
1330 static struct ahash_alg sha_384_512_algs
[] = {
1332 .init
= atmel_sha_init
,
1333 .update
= atmel_sha_update
,
1334 .final
= atmel_sha_final
,
1335 .finup
= atmel_sha_finup
,
1336 .digest
= atmel_sha_digest
,
1337 .export
= atmel_sha_export
,
1338 .import
= atmel_sha_import
,
1340 .digestsize
= SHA384_DIGEST_SIZE
,
1341 .statesize
= sizeof(struct atmel_sha_reqctx
),
1343 .cra_name
= "sha384",
1344 .cra_driver_name
= "atmel-sha384",
1345 .cra_priority
= 100,
1346 .cra_flags
= CRYPTO_ALG_ASYNC
,
1347 .cra_blocksize
= SHA384_BLOCK_SIZE
,
1348 .cra_ctxsize
= sizeof(struct atmel_sha_ctx
),
1349 .cra_alignmask
= 0x3,
1350 .cra_module
= THIS_MODULE
,
1351 .cra_init
= atmel_sha_cra_init
,
1356 .init
= atmel_sha_init
,
1357 .update
= atmel_sha_update
,
1358 .final
= atmel_sha_final
,
1359 .finup
= atmel_sha_finup
,
1360 .digest
= atmel_sha_digest
,
1361 .export
= atmel_sha_export
,
1362 .import
= atmel_sha_import
,
1364 .digestsize
= SHA512_DIGEST_SIZE
,
1365 .statesize
= sizeof(struct atmel_sha_reqctx
),
1367 .cra_name
= "sha512",
1368 .cra_driver_name
= "atmel-sha512",
1369 .cra_priority
= 100,
1370 .cra_flags
= CRYPTO_ALG_ASYNC
,
1371 .cra_blocksize
= SHA512_BLOCK_SIZE
,
1372 .cra_ctxsize
= sizeof(struct atmel_sha_ctx
),
1373 .cra_alignmask
= 0x3,
1374 .cra_module
= THIS_MODULE
,
1375 .cra_init
= atmel_sha_cra_init
,
1381 static void atmel_sha_queue_task(unsigned long data
)
1383 struct atmel_sha_dev
*dd
= (struct atmel_sha_dev
*)data
;
1385 atmel_sha_handle_queue(dd
, NULL
);
1388 static int atmel_sha_done(struct atmel_sha_dev
*dd
)
1392 if (SHA_FLAGS_CPU
& dd
->flags
) {
1393 if (SHA_FLAGS_OUTPUT_READY
& dd
->flags
) {
1394 dd
->flags
&= ~SHA_FLAGS_OUTPUT_READY
;
1397 } else if (SHA_FLAGS_DMA_READY
& dd
->flags
) {
1398 if (SHA_FLAGS_DMA_ACTIVE
& dd
->flags
) {
1399 dd
->flags
&= ~SHA_FLAGS_DMA_ACTIVE
;
1400 atmel_sha_update_dma_stop(dd
);
1406 if (SHA_FLAGS_OUTPUT_READY
& dd
->flags
) {
1407 /* hash or semi-hash ready */
1408 dd
->flags
&= ~(SHA_FLAGS_DMA_READY
|
1409 SHA_FLAGS_OUTPUT_READY
);
1410 err
= atmel_sha_update_dma_start(dd
);
1411 if (err
!= -EINPROGRESS
)
1418 /* finish curent request */
1419 atmel_sha_finish_req(dd
->req
, err
);
1424 static void atmel_sha_done_task(unsigned long data
)
1426 struct atmel_sha_dev
*dd
= (struct atmel_sha_dev
*)data
;
1428 dd
->is_async
= true;
1429 (void)dd
->resume(dd
);
1432 static irqreturn_t
atmel_sha_irq(int irq
, void *dev_id
)
1434 struct atmel_sha_dev
*sha_dd
= dev_id
;
1437 reg
= atmel_sha_read(sha_dd
, SHA_ISR
);
1438 if (reg
& atmel_sha_read(sha_dd
, SHA_IMR
)) {
1439 atmel_sha_write(sha_dd
, SHA_IDR
, reg
);
1440 if (SHA_FLAGS_BUSY
& sha_dd
->flags
) {
1441 sha_dd
->flags
|= SHA_FLAGS_OUTPUT_READY
;
1442 if (!(SHA_FLAGS_CPU
& sha_dd
->flags
))
1443 sha_dd
->flags
|= SHA_FLAGS_DMA_READY
;
1444 tasklet_schedule(&sha_dd
->done_task
);
1446 dev_warn(sha_dd
->dev
, "SHA interrupt when no active requests.\n");
1455 /* DMA transfer functions */
1457 static bool atmel_sha_dma_check_aligned(struct atmel_sha_dev
*dd
,
1458 struct scatterlist
*sg
,
1461 struct atmel_sha_dma
*dma
= &dd
->dma_lch_in
;
1462 struct ahash_request
*req
= dd
->req
;
1463 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1464 size_t bs
= ctx
->block_size
;
1467 for (nents
= 0; sg
; sg
= sg_next(sg
), ++nents
) {
1468 if (!IS_ALIGNED(sg
->offset
, sizeof(u32
)))
1472 * This is the last sg, the only one that is allowed to
1473 * have an unaligned length.
1475 if (len
<= sg
->length
) {
1476 dma
->nents
= nents
+ 1;
1477 dma
->last_sg_length
= sg
->length
;
1478 sg
->length
= ALIGN(len
, sizeof(u32
));
1482 /* All other sg lengths MUST be aligned to the block size. */
1483 if (!IS_ALIGNED(sg
->length
, bs
))
1492 static void atmel_sha_dma_callback2(void *data
)
1494 struct atmel_sha_dev
*dd
= data
;
1495 struct atmel_sha_dma
*dma
= &dd
->dma_lch_in
;
1496 struct scatterlist
*sg
;
1499 dmaengine_terminate_all(dma
->chan
);
1500 dma_unmap_sg(dd
->dev
, dma
->sg
, dma
->nents
, DMA_TO_DEVICE
);
1503 for (nents
= 0; nents
< dma
->nents
- 1; ++nents
)
1505 sg
->length
= dma
->last_sg_length
;
1507 dd
->is_async
= true;
1508 (void)atmel_sha_wait_for_data_ready(dd
, dd
->resume
);
1511 static int atmel_sha_dma_start(struct atmel_sha_dev
*dd
,
1512 struct scatterlist
*src
,
1514 atmel_sha_fn_t resume
)
1516 struct atmel_sha_dma
*dma
= &dd
->dma_lch_in
;
1517 struct dma_slave_config
*config
= &dma
->dma_conf
;
1518 struct dma_chan
*chan
= dma
->chan
;
1519 struct dma_async_tx_descriptor
*desc
;
1520 dma_cookie_t cookie
;
1521 unsigned int sg_len
;
1524 dd
->resume
= resume
;
1527 * dma->nents has already been initialized by
1528 * atmel_sha_dma_check_aligned().
1531 sg_len
= dma_map_sg(dd
->dev
, dma
->sg
, dma
->nents
, DMA_TO_DEVICE
);
1537 config
->src_maxburst
= 16;
1538 config
->dst_maxburst
= 16;
1539 err
= dmaengine_slave_config(chan
, config
);
1543 desc
= dmaengine_prep_slave_sg(chan
, dma
->sg
, sg_len
, DMA_MEM_TO_DEV
,
1544 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
1550 desc
->callback
= atmel_sha_dma_callback2
;
1551 desc
->callback_param
= dd
;
1552 cookie
= dmaengine_submit(desc
);
1553 err
= dma_submit_error(cookie
);
1557 dma_async_issue_pending(chan
);
1559 return -EINPROGRESS
;
1562 dma_unmap_sg(dd
->dev
, dma
->sg
, dma
->nents
, DMA_TO_DEVICE
);
1564 return atmel_sha_complete(dd
, err
);
1568 /* CPU transfer functions */
1570 static int atmel_sha_cpu_transfer(struct atmel_sha_dev
*dd
)
1572 struct ahash_request
*req
= dd
->req
;
1573 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1574 const u32
*words
= (const u32
*)ctx
->buffer
;
1575 size_t i
, num_words
;
1576 u32 isr
, din
, din_inc
;
1578 din_inc
= (ctx
->flags
& SHA_FLAGS_IDATAR0
) ? 0 : 1;
1580 /* Write data into the Input Data Registers. */
1581 num_words
= DIV_ROUND_UP(ctx
->bufcnt
, sizeof(u32
));
1582 for (i
= 0, din
= 0; i
< num_words
; ++i
, din
+= din_inc
)
1583 atmel_sha_write(dd
, SHA_REG_DIN(din
), words
[i
]);
1585 ctx
->offset
+= ctx
->bufcnt
;
1586 ctx
->total
-= ctx
->bufcnt
;
1592 * Prepare next block:
1593 * Fill ctx->buffer now with the next data to be written into
1594 * IDATARx: it gives time for the SHA hardware to process
1595 * the current data so the SHA_INT_DATARDY flag might be set
1596 * in SHA_ISR when polling this register at the beginning of
1599 ctx
->bufcnt
= min_t(size_t, ctx
->block_size
, ctx
->total
);
1600 scatterwalk_map_and_copy(ctx
->buffer
, ctx
->sg
,
1601 ctx
->offset
, ctx
->bufcnt
, 0);
1603 /* Wait for hardware to be ready again. */
1604 isr
= atmel_sha_read(dd
, SHA_ISR
);
1605 if (!(isr
& SHA_INT_DATARDY
)) {
1606 /* Not ready yet. */
1607 dd
->resume
= atmel_sha_cpu_transfer
;
1608 atmel_sha_write(dd
, SHA_IER
, SHA_INT_DATARDY
);
1609 return -EINPROGRESS
;
1613 if (unlikely(!(ctx
->flags
& SHA_FLAGS_WAIT_DATARDY
)))
1614 return dd
->cpu_transfer_complete(dd
);
1616 return atmel_sha_wait_for_data_ready(dd
, dd
->cpu_transfer_complete
);
1619 static int atmel_sha_cpu_start(struct atmel_sha_dev
*dd
,
1620 struct scatterlist
*sg
,
1623 bool wait_data_ready
,
1624 atmel_sha_fn_t resume
)
1626 struct ahash_request
*req
= dd
->req
;
1627 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1632 ctx
->flags
&= ~(SHA_FLAGS_IDATAR0
| SHA_FLAGS_WAIT_DATARDY
);
1635 ctx
->flags
|= SHA_FLAGS_IDATAR0
;
1637 if (wait_data_ready
)
1638 ctx
->flags
|= SHA_FLAGS_WAIT_DATARDY
;
1644 /* Prepare the first block to be written. */
1645 ctx
->bufcnt
= min_t(size_t, ctx
->block_size
, ctx
->total
);
1646 scatterwalk_map_and_copy(ctx
->buffer
, ctx
->sg
,
1647 ctx
->offset
, ctx
->bufcnt
, 0);
1649 dd
->cpu_transfer_complete
= resume
;
1650 return atmel_sha_cpu_transfer(dd
);
1653 static int atmel_sha_cpu_hash(struct atmel_sha_dev
*dd
,
1654 const void *data
, unsigned int datalen
,
1656 atmel_sha_fn_t resume
)
1658 struct ahash_request
*req
= dd
->req
;
1659 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1660 u32 msglen
= (auto_padding
) ? datalen
: 0;
1661 u32 mr
= SHA_MR_MODE_AUTO
;
1663 if (!(IS_ALIGNED(datalen
, ctx
->block_size
) || auto_padding
))
1664 return atmel_sha_complete(dd
, -EINVAL
);
1666 mr
|= (ctx
->flags
& SHA_FLAGS_ALGO_MASK
);
1667 atmel_sha_write(dd
, SHA_MR
, mr
);
1668 atmel_sha_write(dd
, SHA_MSR
, msglen
);
1669 atmel_sha_write(dd
, SHA_BCR
, msglen
);
1670 atmel_sha_write(dd
, SHA_CR
, SHA_CR_FIRST
);
1672 sg_init_one(&dd
->tmp
, data
, datalen
);
1673 return atmel_sha_cpu_start(dd
, &dd
->tmp
, datalen
, false, true, resume
);
1677 /* hmac functions */
1679 struct atmel_sha_hmac_key
{
1681 unsigned int keylen
;
1682 u8 buffer
[SHA512_BLOCK_SIZE
];
1686 static inline void atmel_sha_hmac_key_init(struct atmel_sha_hmac_key
*hkey
)
1688 memset(hkey
, 0, sizeof(*hkey
));
1691 static inline void atmel_sha_hmac_key_release(struct atmel_sha_hmac_key
*hkey
)
1693 kfree(hkey
->keydup
);
1694 memset(hkey
, 0, sizeof(*hkey
));
1697 static inline int atmel_sha_hmac_key_set(struct atmel_sha_hmac_key
*hkey
,
1699 unsigned int keylen
)
1701 atmel_sha_hmac_key_release(hkey
);
1703 if (keylen
> sizeof(hkey
->buffer
)) {
1704 hkey
->keydup
= kmemdup(key
, keylen
, GFP_KERNEL
);
1709 memcpy(hkey
->buffer
, key
, keylen
);
1713 hkey
->keylen
= keylen
;
1717 static inline bool atmel_sha_hmac_key_get(const struct atmel_sha_hmac_key
*hkey
,
1719 unsigned int *keylen
)
1724 *keylen
= hkey
->keylen
;
1725 *key
= (hkey
->keydup
) ? hkey
->keydup
: hkey
->buffer
;
1730 struct atmel_sha_hmac_ctx
{
1731 struct atmel_sha_ctx base
;
1733 struct atmel_sha_hmac_key hkey
;
1734 u32 ipad
[SHA512_BLOCK_SIZE
/ sizeof(u32
)];
1735 u32 opad
[SHA512_BLOCK_SIZE
/ sizeof(u32
)];
1736 atmel_sha_fn_t resume
;
1739 static int atmel_sha_hmac_setup(struct atmel_sha_dev
*dd
,
1740 atmel_sha_fn_t resume
);
1741 static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev
*dd
,
1742 const u8
*key
, unsigned int keylen
);
1743 static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev
*dd
);
1744 static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev
*dd
);
1745 static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev
*dd
);
1746 static int atmel_sha_hmac_setup_done(struct atmel_sha_dev
*dd
);
1748 static int atmel_sha_hmac_init_done(struct atmel_sha_dev
*dd
);
1749 static int atmel_sha_hmac_final(struct atmel_sha_dev
*dd
);
1750 static int atmel_sha_hmac_final_done(struct atmel_sha_dev
*dd
);
1751 static int atmel_sha_hmac_digest2(struct atmel_sha_dev
*dd
);
1753 static int atmel_sha_hmac_setup(struct atmel_sha_dev
*dd
,
1754 atmel_sha_fn_t resume
)
1756 struct ahash_request
*req
= dd
->req
;
1757 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1758 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
1759 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
1760 unsigned int keylen
;
1764 hmac
->resume
= resume
;
1765 switch (ctx
->flags
& SHA_FLAGS_ALGO_MASK
) {
1766 case SHA_FLAGS_SHA1
:
1767 ctx
->block_size
= SHA1_BLOCK_SIZE
;
1768 ctx
->hash_size
= SHA1_DIGEST_SIZE
;
1771 case SHA_FLAGS_SHA224
:
1772 ctx
->block_size
= SHA224_BLOCK_SIZE
;
1773 ctx
->hash_size
= SHA256_DIGEST_SIZE
;
1776 case SHA_FLAGS_SHA256
:
1777 ctx
->block_size
= SHA256_BLOCK_SIZE
;
1778 ctx
->hash_size
= SHA256_DIGEST_SIZE
;
1781 case SHA_FLAGS_SHA384
:
1782 ctx
->block_size
= SHA384_BLOCK_SIZE
;
1783 ctx
->hash_size
= SHA512_DIGEST_SIZE
;
1786 case SHA_FLAGS_SHA512
:
1787 ctx
->block_size
= SHA512_BLOCK_SIZE
;
1788 ctx
->hash_size
= SHA512_DIGEST_SIZE
;
1792 return atmel_sha_complete(dd
, -EINVAL
);
1794 bs
= ctx
->block_size
;
1796 if (likely(!atmel_sha_hmac_key_get(&hmac
->hkey
, &key
, &keylen
)))
1799 /* Compute K' from K. */
1800 if (unlikely(keylen
> bs
))
1801 return atmel_sha_hmac_prehash_key(dd
, key
, keylen
);
1804 memcpy((u8
*)hmac
->ipad
, key
, keylen
);
1805 memset((u8
*)hmac
->ipad
+ keylen
, 0, bs
- keylen
);
1806 return atmel_sha_hmac_compute_ipad_hash(dd
);
1809 static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev
*dd
,
1810 const u8
*key
, unsigned int keylen
)
1812 return atmel_sha_cpu_hash(dd
, key
, keylen
, true,
1813 atmel_sha_hmac_prehash_key_done
);
1816 static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev
*dd
)
1818 struct ahash_request
*req
= dd
->req
;
1819 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
1820 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
1821 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1822 size_t ds
= crypto_ahash_digestsize(tfm
);
1823 size_t bs
= ctx
->block_size
;
1824 size_t i
, num_words
= ds
/ sizeof(u32
);
1827 for (i
= 0; i
< num_words
; ++i
)
1828 hmac
->ipad
[i
] = atmel_sha_read(dd
, SHA_REG_DIGEST(i
));
1829 memset((u8
*)hmac
->ipad
+ ds
, 0, bs
- ds
);
1830 return atmel_sha_hmac_compute_ipad_hash(dd
);
1833 static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev
*dd
)
1835 struct ahash_request
*req
= dd
->req
;
1836 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
1837 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
1838 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1839 size_t bs
= ctx
->block_size
;
1840 size_t i
, num_words
= bs
/ sizeof(u32
);
1842 memcpy(hmac
->opad
, hmac
->ipad
, bs
);
1843 for (i
= 0; i
< num_words
; ++i
) {
1844 hmac
->ipad
[i
] ^= 0x36363636;
1845 hmac
->opad
[i
] ^= 0x5c5c5c5c;
1848 return atmel_sha_cpu_hash(dd
, hmac
->ipad
, bs
, false,
1849 atmel_sha_hmac_compute_opad_hash
);
1852 static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev
*dd
)
1854 struct ahash_request
*req
= dd
->req
;
1855 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
1856 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
1857 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1858 size_t bs
= ctx
->block_size
;
1859 size_t hs
= ctx
->hash_size
;
1860 size_t i
, num_words
= hs
/ sizeof(u32
);
1862 for (i
= 0; i
< num_words
; ++i
)
1863 hmac
->ipad
[i
] = atmel_sha_read(dd
, SHA_REG_DIGEST(i
));
1864 return atmel_sha_cpu_hash(dd
, hmac
->opad
, bs
, false,
1865 atmel_sha_hmac_setup_done
);
1868 static int atmel_sha_hmac_setup_done(struct atmel_sha_dev
*dd
)
1870 struct ahash_request
*req
= dd
->req
;
1871 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
1872 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
1873 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1874 size_t hs
= ctx
->hash_size
;
1875 size_t i
, num_words
= hs
/ sizeof(u32
);
1877 for (i
= 0; i
< num_words
; ++i
)
1878 hmac
->opad
[i
] = atmel_sha_read(dd
, SHA_REG_DIGEST(i
));
1879 atmel_sha_hmac_key_release(&hmac
->hkey
);
1880 return hmac
->resume(dd
);
1883 static int atmel_sha_hmac_start(struct atmel_sha_dev
*dd
)
1885 struct ahash_request
*req
= dd
->req
;
1886 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1889 err
= atmel_sha_hw_init(dd
);
1891 return atmel_sha_complete(dd
, err
);
1895 err
= atmel_sha_hmac_setup(dd
, atmel_sha_hmac_init_done
);
1899 dd
->resume
= atmel_sha_done
;
1900 err
= atmel_sha_update_req(dd
);
1904 dd
->resume
= atmel_sha_hmac_final
;
1905 err
= atmel_sha_final_req(dd
);
1909 err
= atmel_sha_hmac_setup(dd
, atmel_sha_hmac_digest2
);
1913 return atmel_sha_complete(dd
, -EINVAL
);
1919 static int atmel_sha_hmac_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
1920 unsigned int keylen
)
1922 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
1924 return atmel_sha_hmac_key_set(&hmac
->hkey
, key
, keylen
);
1927 static int atmel_sha_hmac_init(struct ahash_request
*req
)
1931 err
= atmel_sha_init(req
);
1935 return atmel_sha_enqueue(req
, SHA_OP_INIT
);
1938 static int atmel_sha_hmac_init_done(struct atmel_sha_dev
*dd
)
1940 struct ahash_request
*req
= dd
->req
;
1941 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1942 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
1943 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
1944 size_t bs
= ctx
->block_size
;
1945 size_t hs
= ctx
->hash_size
;
1948 ctx
->digcnt
[0] = bs
;
1950 ctx
->flags
|= SHA_FLAGS_RESTORE
;
1951 memcpy(ctx
->digest
, hmac
->ipad
, hs
);
1952 return atmel_sha_complete(dd
, 0);
1955 static int atmel_sha_hmac_final(struct atmel_sha_dev
*dd
)
1957 struct ahash_request
*req
= dd
->req
;
1958 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
1959 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
1960 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
1961 u32
*digest
= (u32
*)ctx
->digest
;
1962 size_t ds
= crypto_ahash_digestsize(tfm
);
1963 size_t bs
= ctx
->block_size
;
1964 size_t hs
= ctx
->hash_size
;
1965 size_t i
, num_words
;
1968 /* Save d = SHA((K' + ipad) | msg). */
1969 num_words
= ds
/ sizeof(u32
);
1970 for (i
= 0; i
< num_words
; ++i
)
1971 digest
[i
] = atmel_sha_read(dd
, SHA_REG_DIGEST(i
));
1973 /* Restore context to finish computing SHA((K' + opad) | d). */
1974 atmel_sha_write(dd
, SHA_CR
, SHA_CR_WUIHV
);
1975 num_words
= hs
/ sizeof(u32
);
1976 for (i
= 0; i
< num_words
; ++i
)
1977 atmel_sha_write(dd
, SHA_REG_DIN(i
), hmac
->opad
[i
]);
1979 mr
= SHA_MR_MODE_AUTO
| SHA_MR_UIHV
;
1980 mr
|= (ctx
->flags
& SHA_FLAGS_ALGO_MASK
);
1981 atmel_sha_write(dd
, SHA_MR
, mr
);
1982 atmel_sha_write(dd
, SHA_MSR
, bs
+ ds
);
1983 atmel_sha_write(dd
, SHA_BCR
, ds
);
1984 atmel_sha_write(dd
, SHA_CR
, SHA_CR_FIRST
);
1986 sg_init_one(&dd
->tmp
, digest
, ds
);
1987 return atmel_sha_cpu_start(dd
, &dd
->tmp
, ds
, false, true,
1988 atmel_sha_hmac_final_done
);
1991 static int atmel_sha_hmac_final_done(struct atmel_sha_dev
*dd
)
1994 * req->result might not be sizeof(u32) aligned, so copy the
1995 * digest into ctx->digest[] before memcpy() the data into
1998 atmel_sha_copy_hash(dd
->req
);
1999 atmel_sha_copy_ready_hash(dd
->req
);
2000 return atmel_sha_complete(dd
, 0);
2003 static int atmel_sha_hmac_digest(struct ahash_request
*req
)
2007 err
= atmel_sha_init(req
);
2011 return atmel_sha_enqueue(req
, SHA_OP_DIGEST
);
2014 static int atmel_sha_hmac_digest2(struct atmel_sha_dev
*dd
)
2016 struct ahash_request
*req
= dd
->req
;
2017 struct atmel_sha_reqctx
*ctx
= ahash_request_ctx(req
);
2018 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
2019 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
2020 size_t hs
= ctx
->hash_size
;
2021 size_t i
, num_words
= hs
/ sizeof(u32
);
2022 bool use_dma
= false;
2025 /* Special case for empty message. */
2027 return atmel_sha_complete(dd
, -EINVAL
); // TODO:
2029 /* Check DMA threshold and alignment. */
2030 if (req
->nbytes
> ATMEL_SHA_DMA_THRESHOLD
&&
2031 atmel_sha_dma_check_aligned(dd
, req
->src
, req
->nbytes
))
2034 /* Write both initial hash values to compute a HMAC. */
2035 atmel_sha_write(dd
, SHA_CR
, SHA_CR_WUIHV
);
2036 for (i
= 0; i
< num_words
; ++i
)
2037 atmel_sha_write(dd
, SHA_REG_DIN(i
), hmac
->ipad
[i
]);
2039 atmel_sha_write(dd
, SHA_CR
, SHA_CR_WUIEHV
);
2040 for (i
= 0; i
< num_words
; ++i
)
2041 atmel_sha_write(dd
, SHA_REG_DIN(i
), hmac
->opad
[i
]);
2043 /* Write the Mode, Message Size, Bytes Count then Control Registers. */
2044 mr
= (SHA_MR_HMAC
| SHA_MR_DUALBUFF
);
2045 mr
|= ctx
->flags
& SHA_FLAGS_ALGO_MASK
;
2047 mr
|= SHA_MR_MODE_IDATAR0
;
2049 mr
|= SHA_MR_MODE_AUTO
;
2050 atmel_sha_write(dd
, SHA_MR
, mr
);
2052 atmel_sha_write(dd
, SHA_MSR
, req
->nbytes
);
2053 atmel_sha_write(dd
, SHA_BCR
, req
->nbytes
);
2055 atmel_sha_write(dd
, SHA_CR
, SHA_CR_FIRST
);
2059 return atmel_sha_dma_start(dd
, req
->src
, req
->nbytes
,
2060 atmel_sha_hmac_final_done
);
2062 return atmel_sha_cpu_start(dd
, req
->src
, req
->nbytes
, false, true,
2063 atmel_sha_hmac_final_done
);
2066 static int atmel_sha_hmac_cra_init(struct crypto_tfm
*tfm
)
2068 struct atmel_sha_hmac_ctx
*hmac
= crypto_tfm_ctx(tfm
);
2070 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
2071 sizeof(struct atmel_sha_reqctx
));
2072 hmac
->base
.start
= atmel_sha_hmac_start
;
2073 atmel_sha_hmac_key_init(&hmac
->hkey
);
2078 static void atmel_sha_hmac_cra_exit(struct crypto_tfm
*tfm
)
2080 struct atmel_sha_hmac_ctx
*hmac
= crypto_tfm_ctx(tfm
);
2082 atmel_sha_hmac_key_release(&hmac
->hkey
);
2085 static struct ahash_alg sha_hmac_algs
[] = {
2087 .init
= atmel_sha_hmac_init
,
2088 .update
= atmel_sha_update
,
2089 .final
= atmel_sha_final
,
2090 .digest
= atmel_sha_hmac_digest
,
2091 .setkey
= atmel_sha_hmac_setkey
,
2092 .export
= atmel_sha_export
,
2093 .import
= atmel_sha_import
,
2095 .digestsize
= SHA1_DIGEST_SIZE
,
2096 .statesize
= sizeof(struct atmel_sha_reqctx
),
2098 .cra_name
= "hmac(sha1)",
2099 .cra_driver_name
= "atmel-hmac-sha1",
2100 .cra_priority
= 100,
2101 .cra_flags
= CRYPTO_ALG_ASYNC
,
2102 .cra_blocksize
= SHA1_BLOCK_SIZE
,
2103 .cra_ctxsize
= sizeof(struct atmel_sha_hmac_ctx
),
2105 .cra_module
= THIS_MODULE
,
2106 .cra_init
= atmel_sha_hmac_cra_init
,
2107 .cra_exit
= atmel_sha_hmac_cra_exit
,
2112 .init
= atmel_sha_hmac_init
,
2113 .update
= atmel_sha_update
,
2114 .final
= atmel_sha_final
,
2115 .digest
= atmel_sha_hmac_digest
,
2116 .setkey
= atmel_sha_hmac_setkey
,
2117 .export
= atmel_sha_export
,
2118 .import
= atmel_sha_import
,
2120 .digestsize
= SHA224_DIGEST_SIZE
,
2121 .statesize
= sizeof(struct atmel_sha_reqctx
),
2123 .cra_name
= "hmac(sha224)",
2124 .cra_driver_name
= "atmel-hmac-sha224",
2125 .cra_priority
= 100,
2126 .cra_flags
= CRYPTO_ALG_ASYNC
,
2127 .cra_blocksize
= SHA224_BLOCK_SIZE
,
2128 .cra_ctxsize
= sizeof(struct atmel_sha_hmac_ctx
),
2130 .cra_module
= THIS_MODULE
,
2131 .cra_init
= atmel_sha_hmac_cra_init
,
2132 .cra_exit
= atmel_sha_hmac_cra_exit
,
2137 .init
= atmel_sha_hmac_init
,
2138 .update
= atmel_sha_update
,
2139 .final
= atmel_sha_final
,
2140 .digest
= atmel_sha_hmac_digest
,
2141 .setkey
= atmel_sha_hmac_setkey
,
2142 .export
= atmel_sha_export
,
2143 .import
= atmel_sha_import
,
2145 .digestsize
= SHA256_DIGEST_SIZE
,
2146 .statesize
= sizeof(struct atmel_sha_reqctx
),
2148 .cra_name
= "hmac(sha256)",
2149 .cra_driver_name
= "atmel-hmac-sha256",
2150 .cra_priority
= 100,
2151 .cra_flags
= CRYPTO_ALG_ASYNC
,
2152 .cra_blocksize
= SHA256_BLOCK_SIZE
,
2153 .cra_ctxsize
= sizeof(struct atmel_sha_hmac_ctx
),
2155 .cra_module
= THIS_MODULE
,
2156 .cra_init
= atmel_sha_hmac_cra_init
,
2157 .cra_exit
= atmel_sha_hmac_cra_exit
,
2162 .init
= atmel_sha_hmac_init
,
2163 .update
= atmel_sha_update
,
2164 .final
= atmel_sha_final
,
2165 .digest
= atmel_sha_hmac_digest
,
2166 .setkey
= atmel_sha_hmac_setkey
,
2167 .export
= atmel_sha_export
,
2168 .import
= atmel_sha_import
,
2170 .digestsize
= SHA384_DIGEST_SIZE
,
2171 .statesize
= sizeof(struct atmel_sha_reqctx
),
2173 .cra_name
= "hmac(sha384)",
2174 .cra_driver_name
= "atmel-hmac-sha384",
2175 .cra_priority
= 100,
2176 .cra_flags
= CRYPTO_ALG_ASYNC
,
2177 .cra_blocksize
= SHA384_BLOCK_SIZE
,
2178 .cra_ctxsize
= sizeof(struct atmel_sha_hmac_ctx
),
2180 .cra_module
= THIS_MODULE
,
2181 .cra_init
= atmel_sha_hmac_cra_init
,
2182 .cra_exit
= atmel_sha_hmac_cra_exit
,
2187 .init
= atmel_sha_hmac_init
,
2188 .update
= atmel_sha_update
,
2189 .final
= atmel_sha_final
,
2190 .digest
= atmel_sha_hmac_digest
,
2191 .setkey
= atmel_sha_hmac_setkey
,
2192 .export
= atmel_sha_export
,
2193 .import
= atmel_sha_import
,
2195 .digestsize
= SHA512_DIGEST_SIZE
,
2196 .statesize
= sizeof(struct atmel_sha_reqctx
),
2198 .cra_name
= "hmac(sha512)",
2199 .cra_driver_name
= "atmel-hmac-sha512",
2200 .cra_priority
= 100,
2201 .cra_flags
= CRYPTO_ALG_ASYNC
,
2202 .cra_blocksize
= SHA512_BLOCK_SIZE
,
2203 .cra_ctxsize
= sizeof(struct atmel_sha_hmac_ctx
),
2205 .cra_module
= THIS_MODULE
,
2206 .cra_init
= atmel_sha_hmac_cra_init
,
2207 .cra_exit
= atmel_sha_hmac_cra_exit
,
2213 #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
2214 /* authenc functions */
2216 static int atmel_sha_authenc_init2(struct atmel_sha_dev
*dd
);
2217 static int atmel_sha_authenc_init_done(struct atmel_sha_dev
*dd
);
2218 static int atmel_sha_authenc_final_done(struct atmel_sha_dev
*dd
);
2221 struct atmel_sha_authenc_ctx
{
2222 struct crypto_ahash
*tfm
;
2225 struct atmel_sha_authenc_reqctx
{
2226 struct atmel_sha_reqctx base
;
2228 atmel_aes_authenc_fn_t cb
;
2229 struct atmel_aes_dev
*aes_dev
;
2231 /* _init() parameters. */
2232 struct scatterlist
*assoc
;
2236 /* _final() parameters. */
2238 unsigned int digestlen
;
2241 static void atmel_sha_authenc_complete(struct crypto_async_request
*areq
,
2244 struct ahash_request
*req
= areq
->data
;
2245 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2247 authctx
->cb(authctx
->aes_dev
, err
, authctx
->base
.dd
->is_async
);
2250 static int atmel_sha_authenc_start(struct atmel_sha_dev
*dd
)
2252 struct ahash_request
*req
= dd
->req
;
2253 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2257 * Force atmel_sha_complete() to call req->base.complete(), ie
2258 * atmel_sha_authenc_complete(), which in turn calls authctx->cb().
2260 dd
->force_complete
= true;
2262 err
= atmel_sha_hw_init(dd
);
2263 return authctx
->cb(authctx
->aes_dev
, err
, dd
->is_async
);
2266 bool atmel_sha_authenc_is_ready(void)
2268 struct atmel_sha_ctx dummy
;
2271 return (atmel_sha_find_dev(&dummy
) != NULL
);
2273 EXPORT_SYMBOL_GPL(atmel_sha_authenc_is_ready
);
2275 unsigned int atmel_sha_authenc_get_reqsize(void)
2277 return sizeof(struct atmel_sha_authenc_reqctx
);
2279 EXPORT_SYMBOL_GPL(atmel_sha_authenc_get_reqsize
);
2281 struct atmel_sha_authenc_ctx
*atmel_sha_authenc_spawn(unsigned long mode
)
2283 struct atmel_sha_authenc_ctx
*auth
;
2284 struct crypto_ahash
*tfm
;
2285 struct atmel_sha_ctx
*tctx
;
2289 switch (mode
& SHA_FLAGS_MODE_MASK
) {
2290 case SHA_FLAGS_HMAC_SHA1
:
2291 name
= "atmel-hmac-sha1";
2294 case SHA_FLAGS_HMAC_SHA224
:
2295 name
= "atmel-hmac-sha224";
2298 case SHA_FLAGS_HMAC_SHA256
:
2299 name
= "atmel-hmac-sha256";
2302 case SHA_FLAGS_HMAC_SHA384
:
2303 name
= "atmel-hmac-sha384";
2306 case SHA_FLAGS_HMAC_SHA512
:
2307 name
= "atmel-hmac-sha512";
2314 tfm
= crypto_alloc_ahash(name
, 0, 0);
2319 tctx
= crypto_ahash_ctx(tfm
);
2320 tctx
->start
= atmel_sha_authenc_start
;
2323 auth
= kzalloc(sizeof(*auth
), GFP_KERNEL
);
2326 goto err_free_ahash
;
2333 crypto_free_ahash(tfm
);
2335 return ERR_PTR(err
);
2337 EXPORT_SYMBOL_GPL(atmel_sha_authenc_spawn
);
2339 void atmel_sha_authenc_free(struct atmel_sha_authenc_ctx
*auth
)
2342 crypto_free_ahash(auth
->tfm
);
2345 EXPORT_SYMBOL_GPL(atmel_sha_authenc_free
);
2347 int atmel_sha_authenc_setkey(struct atmel_sha_authenc_ctx
*auth
,
2348 const u8
*key
, unsigned int keylen
,
2351 struct crypto_ahash
*tfm
= auth
->tfm
;
2354 crypto_ahash_clear_flags(tfm
, CRYPTO_TFM_REQ_MASK
);
2355 crypto_ahash_set_flags(tfm
, *flags
& CRYPTO_TFM_REQ_MASK
);
2356 err
= crypto_ahash_setkey(tfm
, key
, keylen
);
2357 *flags
= crypto_ahash_get_flags(tfm
);
2361 EXPORT_SYMBOL_GPL(atmel_sha_authenc_setkey
);
2363 int atmel_sha_authenc_schedule(struct ahash_request
*req
,
2364 struct atmel_sha_authenc_ctx
*auth
,
2365 atmel_aes_authenc_fn_t cb
,
2366 struct atmel_aes_dev
*aes_dev
)
2368 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2369 struct atmel_sha_reqctx
*ctx
= &authctx
->base
;
2370 struct crypto_ahash
*tfm
= auth
->tfm
;
2371 struct atmel_sha_ctx
*tctx
= crypto_ahash_ctx(tfm
);
2372 struct atmel_sha_dev
*dd
;
2374 /* Reset request context (MUST be done first). */
2375 memset(authctx
, 0, sizeof(*authctx
));
2377 /* Get SHA device. */
2378 dd
= atmel_sha_find_dev(tctx
);
2380 return cb(aes_dev
, -ENODEV
, false);
2382 /* Init request context. */
2384 ctx
->buflen
= SHA_BUFFER_LEN
;
2386 authctx
->aes_dev
= aes_dev
;
2387 ahash_request_set_tfm(req
, tfm
);
2388 ahash_request_set_callback(req
, 0, atmel_sha_authenc_complete
, req
);
2390 return atmel_sha_handle_queue(dd
, req
);
2392 EXPORT_SYMBOL_GPL(atmel_sha_authenc_schedule
);
2394 int atmel_sha_authenc_init(struct ahash_request
*req
,
2395 struct scatterlist
*assoc
, unsigned int assoclen
,
2396 unsigned int textlen
,
2397 atmel_aes_authenc_fn_t cb
,
2398 struct atmel_aes_dev
*aes_dev
)
2400 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2401 struct atmel_sha_reqctx
*ctx
= &authctx
->base
;
2402 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
2403 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
2404 struct atmel_sha_dev
*dd
= ctx
->dd
;
2406 if (unlikely(!IS_ALIGNED(assoclen
, sizeof(u32
))))
2407 return atmel_sha_complete(dd
, -EINVAL
);
2410 authctx
->aes_dev
= aes_dev
;
2411 authctx
->assoc
= assoc
;
2412 authctx
->assoclen
= assoclen
;
2413 authctx
->textlen
= textlen
;
2415 ctx
->flags
= hmac
->base
.flags
;
2416 return atmel_sha_hmac_setup(dd
, atmel_sha_authenc_init2
);
2418 EXPORT_SYMBOL_GPL(atmel_sha_authenc_init
);
2420 static int atmel_sha_authenc_init2(struct atmel_sha_dev
*dd
)
2422 struct ahash_request
*req
= dd
->req
;
2423 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2424 struct atmel_sha_reqctx
*ctx
= &authctx
->base
;
2425 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
2426 struct atmel_sha_hmac_ctx
*hmac
= crypto_ahash_ctx(tfm
);
2427 size_t hs
= ctx
->hash_size
;
2428 size_t i
, num_words
= hs
/ sizeof(u32
);
2431 atmel_sha_write(dd
, SHA_CR
, SHA_CR_WUIHV
);
2432 for (i
= 0; i
< num_words
; ++i
)
2433 atmel_sha_write(dd
, SHA_REG_DIN(i
), hmac
->ipad
[i
]);
2435 atmel_sha_write(dd
, SHA_CR
, SHA_CR_WUIEHV
);
2436 for (i
= 0; i
< num_words
; ++i
)
2437 atmel_sha_write(dd
, SHA_REG_DIN(i
), hmac
->opad
[i
]);
2439 mr
= (SHA_MR_MODE_IDATAR0
|
2442 mr
|= ctx
->flags
& SHA_FLAGS_ALGO_MASK
;
2443 atmel_sha_write(dd
, SHA_MR
, mr
);
2445 msg_size
= authctx
->assoclen
+ authctx
->textlen
;
2446 atmel_sha_write(dd
, SHA_MSR
, msg_size
);
2447 atmel_sha_write(dd
, SHA_BCR
, msg_size
);
2449 atmel_sha_write(dd
, SHA_CR
, SHA_CR_FIRST
);
2451 /* Process assoc data. */
2452 return atmel_sha_cpu_start(dd
, authctx
->assoc
, authctx
->assoclen
,
2454 atmel_sha_authenc_init_done
);
2457 static int atmel_sha_authenc_init_done(struct atmel_sha_dev
*dd
)
2459 struct ahash_request
*req
= dd
->req
;
2460 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2462 return authctx
->cb(authctx
->aes_dev
, 0, dd
->is_async
);
2465 int atmel_sha_authenc_final(struct ahash_request
*req
,
2466 u32
*digest
, unsigned int digestlen
,
2467 atmel_aes_authenc_fn_t cb
,
2468 struct atmel_aes_dev
*aes_dev
)
2470 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2471 struct atmel_sha_reqctx
*ctx
= &authctx
->base
;
2472 struct atmel_sha_dev
*dd
= ctx
->dd
;
2474 switch (ctx
->flags
& SHA_FLAGS_ALGO_MASK
) {
2475 case SHA_FLAGS_SHA1
:
2476 authctx
->digestlen
= SHA1_DIGEST_SIZE
;
2479 case SHA_FLAGS_SHA224
:
2480 authctx
->digestlen
= SHA224_DIGEST_SIZE
;
2483 case SHA_FLAGS_SHA256
:
2484 authctx
->digestlen
= SHA256_DIGEST_SIZE
;
2487 case SHA_FLAGS_SHA384
:
2488 authctx
->digestlen
= SHA384_DIGEST_SIZE
;
2491 case SHA_FLAGS_SHA512
:
2492 authctx
->digestlen
= SHA512_DIGEST_SIZE
;
2496 return atmel_sha_complete(dd
, -EINVAL
);
2498 if (authctx
->digestlen
> digestlen
)
2499 authctx
->digestlen
= digestlen
;
2502 authctx
->aes_dev
= aes_dev
;
2503 authctx
->digest
= digest
;
2504 return atmel_sha_wait_for_data_ready(dd
,
2505 atmel_sha_authenc_final_done
);
2507 EXPORT_SYMBOL_GPL(atmel_sha_authenc_final
);
2509 static int atmel_sha_authenc_final_done(struct atmel_sha_dev
*dd
)
2511 struct ahash_request
*req
= dd
->req
;
2512 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2513 size_t i
, num_words
= authctx
->digestlen
/ sizeof(u32
);
2515 for (i
= 0; i
< num_words
; ++i
)
2516 authctx
->digest
[i
] = atmel_sha_read(dd
, SHA_REG_DIGEST(i
));
2518 return atmel_sha_complete(dd
, 0);
2521 void atmel_sha_authenc_abort(struct ahash_request
*req
)
2523 struct atmel_sha_authenc_reqctx
*authctx
= ahash_request_ctx(req
);
2524 struct atmel_sha_reqctx
*ctx
= &authctx
->base
;
2525 struct atmel_sha_dev
*dd
= ctx
->dd
;
2527 /* Prevent atmel_sha_complete() from calling req->base.complete(). */
2528 dd
->is_async
= false;
2529 dd
->force_complete
= false;
2530 (void)atmel_sha_complete(dd
, 0);
2532 EXPORT_SYMBOL_GPL(atmel_sha_authenc_abort
);
2534 #endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
2537 static void atmel_sha_unregister_algs(struct atmel_sha_dev
*dd
)
2541 if (dd
->caps
.has_hmac
)
2542 for (i
= 0; i
< ARRAY_SIZE(sha_hmac_algs
); i
++)
2543 crypto_unregister_ahash(&sha_hmac_algs
[i
]);
2545 for (i
= 0; i
< ARRAY_SIZE(sha_1_256_algs
); i
++)
2546 crypto_unregister_ahash(&sha_1_256_algs
[i
]);
2548 if (dd
->caps
.has_sha224
)
2549 crypto_unregister_ahash(&sha_224_alg
);
2551 if (dd
->caps
.has_sha_384_512
) {
2552 for (i
= 0; i
< ARRAY_SIZE(sha_384_512_algs
); i
++)
2553 crypto_unregister_ahash(&sha_384_512_algs
[i
]);
2557 static int atmel_sha_register_algs(struct atmel_sha_dev
*dd
)
2561 for (i
= 0; i
< ARRAY_SIZE(sha_1_256_algs
); i
++) {
2562 err
= crypto_register_ahash(&sha_1_256_algs
[i
]);
2564 goto err_sha_1_256_algs
;
2567 if (dd
->caps
.has_sha224
) {
2568 err
= crypto_register_ahash(&sha_224_alg
);
2570 goto err_sha_224_algs
;
2573 if (dd
->caps
.has_sha_384_512
) {
2574 for (i
= 0; i
< ARRAY_SIZE(sha_384_512_algs
); i
++) {
2575 err
= crypto_register_ahash(&sha_384_512_algs
[i
]);
2577 goto err_sha_384_512_algs
;
2581 if (dd
->caps
.has_hmac
) {
2582 for (i
= 0; i
< ARRAY_SIZE(sha_hmac_algs
); i
++) {
2583 err
= crypto_register_ahash(&sha_hmac_algs
[i
]);
2585 goto err_sha_hmac_algs
;
2591 /*i = ARRAY_SIZE(sha_hmac_algs);*/
2593 for (j
= 0; j
< i
; j
++)
2594 crypto_unregister_ahash(&sha_hmac_algs
[j
]);
2595 i
= ARRAY_SIZE(sha_384_512_algs
);
2596 err_sha_384_512_algs
:
2597 for (j
= 0; j
< i
; j
++)
2598 crypto_unregister_ahash(&sha_384_512_algs
[j
]);
2599 crypto_unregister_ahash(&sha_224_alg
);
2601 i
= ARRAY_SIZE(sha_1_256_algs
);
2603 for (j
= 0; j
< i
; j
++)
2604 crypto_unregister_ahash(&sha_1_256_algs
[j
]);
2609 static bool atmel_sha_filter(struct dma_chan
*chan
, void *slave
)
2611 struct at_dma_slave
*sl
= slave
;
2613 if (sl
&& sl
->dma_dev
== chan
->device
->dev
) {
2621 static int atmel_sha_dma_init(struct atmel_sha_dev
*dd
,
2622 struct crypto_platform_data
*pdata
)
2624 dma_cap_mask_t mask_in
;
2626 /* Try to grab DMA channel */
2627 dma_cap_zero(mask_in
);
2628 dma_cap_set(DMA_SLAVE
, mask_in
);
2630 dd
->dma_lch_in
.chan
= dma_request_slave_channel_compat(mask_in
,
2631 atmel_sha_filter
, &pdata
->dma_slave
->rxdata
, dd
->dev
, "tx");
2632 if (!dd
->dma_lch_in
.chan
) {
2633 dev_warn(dd
->dev
, "no DMA channel available\n");
2637 dd
->dma_lch_in
.dma_conf
.direction
= DMA_MEM_TO_DEV
;
2638 dd
->dma_lch_in
.dma_conf
.dst_addr
= dd
->phys_base
+
2640 dd
->dma_lch_in
.dma_conf
.src_maxburst
= 1;
2641 dd
->dma_lch_in
.dma_conf
.src_addr_width
=
2642 DMA_SLAVE_BUSWIDTH_4_BYTES
;
2643 dd
->dma_lch_in
.dma_conf
.dst_maxburst
= 1;
2644 dd
->dma_lch_in
.dma_conf
.dst_addr_width
=
2645 DMA_SLAVE_BUSWIDTH_4_BYTES
;
2646 dd
->dma_lch_in
.dma_conf
.device_fc
= false;
2651 static void atmel_sha_dma_cleanup(struct atmel_sha_dev
*dd
)
2653 dma_release_channel(dd
->dma_lch_in
.chan
);
2656 static void atmel_sha_get_cap(struct atmel_sha_dev
*dd
)
2659 dd
->caps
.has_dma
= 0;
2660 dd
->caps
.has_dualbuff
= 0;
2661 dd
->caps
.has_sha224
= 0;
2662 dd
->caps
.has_sha_384_512
= 0;
2663 dd
->caps
.has_uihv
= 0;
2664 dd
->caps
.has_hmac
= 0;
2666 /* keep only major version number */
2667 switch (dd
->hw_version
& 0xff0) {
2669 dd
->caps
.has_dma
= 1;
2670 dd
->caps
.has_dualbuff
= 1;
2671 dd
->caps
.has_sha224
= 1;
2672 dd
->caps
.has_sha_384_512
= 1;
2673 dd
->caps
.has_uihv
= 1;
2674 dd
->caps
.has_hmac
= 1;
2677 dd
->caps
.has_dma
= 1;
2678 dd
->caps
.has_dualbuff
= 1;
2679 dd
->caps
.has_sha224
= 1;
2680 dd
->caps
.has_sha_384_512
= 1;
2681 dd
->caps
.has_uihv
= 1;
2684 dd
->caps
.has_dma
= 1;
2685 dd
->caps
.has_dualbuff
= 1;
2686 dd
->caps
.has_sha224
= 1;
2687 dd
->caps
.has_sha_384_512
= 1;
2690 dd
->caps
.has_dma
= 1;
2691 dd
->caps
.has_dualbuff
= 1;
2692 dd
->caps
.has_sha224
= 1;
2698 "Unmanaged sha version, set minimum capabilities\n");
2703 #if defined(CONFIG_OF)
2704 static const struct of_device_id atmel_sha_dt_ids
[] = {
2705 { .compatible
= "atmel,at91sam9g46-sha" },
2709 MODULE_DEVICE_TABLE(of
, atmel_sha_dt_ids
);
2711 static struct crypto_platform_data
*atmel_sha_of_init(struct platform_device
*pdev
)
2713 struct device_node
*np
= pdev
->dev
.of_node
;
2714 struct crypto_platform_data
*pdata
;
2717 dev_err(&pdev
->dev
, "device node not found\n");
2718 return ERR_PTR(-EINVAL
);
2721 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
2723 return ERR_PTR(-ENOMEM
);
2725 pdata
->dma_slave
= devm_kzalloc(&pdev
->dev
,
2726 sizeof(*(pdata
->dma_slave
)),
2728 if (!pdata
->dma_slave
)
2729 return ERR_PTR(-ENOMEM
);
2733 #else /* CONFIG_OF */
2734 static inline struct crypto_platform_data
*atmel_sha_of_init(struct platform_device
*dev
)
2736 return ERR_PTR(-EINVAL
);
2740 static int atmel_sha_probe(struct platform_device
*pdev
)
2742 struct atmel_sha_dev
*sha_dd
;
2743 struct crypto_platform_data
*pdata
;
2744 struct device
*dev
= &pdev
->dev
;
2745 struct resource
*sha_res
;
2748 sha_dd
= devm_kzalloc(&pdev
->dev
, sizeof(*sha_dd
), GFP_KERNEL
);
2749 if (sha_dd
== NULL
) {
2756 platform_set_drvdata(pdev
, sha_dd
);
2758 INIT_LIST_HEAD(&sha_dd
->list
);
2759 spin_lock_init(&sha_dd
->lock
);
2761 tasklet_init(&sha_dd
->done_task
, atmel_sha_done_task
,
2762 (unsigned long)sha_dd
);
2763 tasklet_init(&sha_dd
->queue_task
, atmel_sha_queue_task
,
2764 (unsigned long)sha_dd
);
2766 crypto_init_queue(&sha_dd
->queue
, ATMEL_SHA_QUEUE_LENGTH
);
2768 /* Get the base address */
2769 sha_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2771 dev_err(dev
, "no MEM resource info\n");
2775 sha_dd
->phys_base
= sha_res
->start
;
2778 sha_dd
->irq
= platform_get_irq(pdev
, 0);
2779 if (sha_dd
->irq
< 0) {
2780 dev_err(dev
, "no IRQ resource info\n");
2785 err
= devm_request_irq(&pdev
->dev
, sha_dd
->irq
, atmel_sha_irq
,
2786 IRQF_SHARED
, "atmel-sha", sha_dd
);
2788 dev_err(dev
, "unable to request sha irq.\n");
2792 /* Initializing the clock */
2793 sha_dd
->iclk
= devm_clk_get(&pdev
->dev
, "sha_clk");
2794 if (IS_ERR(sha_dd
->iclk
)) {
2795 dev_err(dev
, "clock initialization failed.\n");
2796 err
= PTR_ERR(sha_dd
->iclk
);
2800 sha_dd
->io_base
= devm_ioremap_resource(&pdev
->dev
, sha_res
);
2801 if (IS_ERR(sha_dd
->io_base
)) {
2802 dev_err(dev
, "can't ioremap\n");
2803 err
= PTR_ERR(sha_dd
->io_base
);
2807 err
= clk_prepare(sha_dd
->iclk
);
2811 atmel_sha_hw_version_init(sha_dd
);
2813 atmel_sha_get_cap(sha_dd
);
2815 if (sha_dd
->caps
.has_dma
) {
2816 pdata
= pdev
->dev
.platform_data
;
2818 pdata
= atmel_sha_of_init(pdev
);
2819 if (IS_ERR(pdata
)) {
2820 dev_err(&pdev
->dev
, "platform data not available\n");
2821 err
= PTR_ERR(pdata
);
2822 goto iclk_unprepare
;
2825 if (!pdata
->dma_slave
) {
2827 goto iclk_unprepare
;
2829 err
= atmel_sha_dma_init(sha_dd
, pdata
);
2833 dev_info(dev
, "using %s for DMA transfers\n",
2834 dma_chan_name(sha_dd
->dma_lch_in
.chan
));
2837 spin_lock(&atmel_sha
.lock
);
2838 list_add_tail(&sha_dd
->list
, &atmel_sha
.dev_list
);
2839 spin_unlock(&atmel_sha
.lock
);
2841 err
= atmel_sha_register_algs(sha_dd
);
2845 dev_info(dev
, "Atmel SHA1/SHA256%s%s\n",
2846 sha_dd
->caps
.has_sha224
? "/SHA224" : "",
2847 sha_dd
->caps
.has_sha_384_512
? "/SHA384/SHA512" : "");
2852 spin_lock(&atmel_sha
.lock
);
2853 list_del(&sha_dd
->list
);
2854 spin_unlock(&atmel_sha
.lock
);
2855 if (sha_dd
->caps
.has_dma
)
2856 atmel_sha_dma_cleanup(sha_dd
);
2859 clk_unprepare(sha_dd
->iclk
);
2861 tasklet_kill(&sha_dd
->queue_task
);
2862 tasklet_kill(&sha_dd
->done_task
);
2864 dev_err(dev
, "initialization failed.\n");
2869 static int atmel_sha_remove(struct platform_device
*pdev
)
2871 struct atmel_sha_dev
*sha_dd
;
2873 sha_dd
= platform_get_drvdata(pdev
);
2876 spin_lock(&atmel_sha
.lock
);
2877 list_del(&sha_dd
->list
);
2878 spin_unlock(&atmel_sha
.lock
);
2880 atmel_sha_unregister_algs(sha_dd
);
2882 tasklet_kill(&sha_dd
->queue_task
);
2883 tasklet_kill(&sha_dd
->done_task
);
2885 if (sha_dd
->caps
.has_dma
)
2886 atmel_sha_dma_cleanup(sha_dd
);
2888 clk_unprepare(sha_dd
->iclk
);
2893 static struct platform_driver atmel_sha_driver
= {
2894 .probe
= atmel_sha_probe
,
2895 .remove
= atmel_sha_remove
,
2897 .name
= "atmel_sha",
2898 .of_match_table
= of_match_ptr(atmel_sha_dt_ids
),
2902 module_platform_driver(atmel_sha_driver
);
2904 MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support.");
2905 MODULE_LICENSE("GPL v2");
2906 MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");