4 * Support for ATMEL AES 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-aes.c driver.
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 <crypto/scatterwalk.h>
37 #include <crypto/algapi.h>
38 #include <crypto/aes.h>
39 #include <crypto/gcm.h>
40 #include <crypto/xts.h>
41 #include <crypto/internal/aead.h>
42 #include <linux/platform_data/crypto-atmel.h>
43 #include <dt-bindings/dma/at91.h>
44 #include "atmel-aes-regs.h"
45 #include "atmel-authenc.h"
47 #define ATMEL_AES_PRIORITY 300
49 #define ATMEL_AES_BUFFER_ORDER 2
50 #define ATMEL_AES_BUFFER_SIZE (PAGE_SIZE << ATMEL_AES_BUFFER_ORDER)
52 #define CFB8_BLOCK_SIZE 1
53 #define CFB16_BLOCK_SIZE 2
54 #define CFB32_BLOCK_SIZE 4
55 #define CFB64_BLOCK_SIZE 8
57 #define SIZE_IN_WORDS(x) ((x) >> 2)
60 /* Reserve bits [18:16] [14:12] [1:0] for mode (same as for AES_MR) */
61 #define AES_FLAGS_ENCRYPT AES_MR_CYPHER_ENC
62 #define AES_FLAGS_GTAGEN AES_MR_GTAGEN
63 #define AES_FLAGS_OPMODE_MASK (AES_MR_OPMOD_MASK | AES_MR_CFBS_MASK)
64 #define AES_FLAGS_ECB AES_MR_OPMOD_ECB
65 #define AES_FLAGS_CBC AES_MR_OPMOD_CBC
66 #define AES_FLAGS_OFB AES_MR_OPMOD_OFB
67 #define AES_FLAGS_CFB128 (AES_MR_OPMOD_CFB | AES_MR_CFBS_128b)
68 #define AES_FLAGS_CFB64 (AES_MR_OPMOD_CFB | AES_MR_CFBS_64b)
69 #define AES_FLAGS_CFB32 (AES_MR_OPMOD_CFB | AES_MR_CFBS_32b)
70 #define AES_FLAGS_CFB16 (AES_MR_OPMOD_CFB | AES_MR_CFBS_16b)
71 #define AES_FLAGS_CFB8 (AES_MR_OPMOD_CFB | AES_MR_CFBS_8b)
72 #define AES_FLAGS_CTR AES_MR_OPMOD_CTR
73 #define AES_FLAGS_GCM AES_MR_OPMOD_GCM
74 #define AES_FLAGS_XTS AES_MR_OPMOD_XTS
76 #define AES_FLAGS_MODE_MASK (AES_FLAGS_OPMODE_MASK | \
80 #define AES_FLAGS_BUSY BIT(3)
81 #define AES_FLAGS_DUMP_REG BIT(4)
82 #define AES_FLAGS_OWN_SHA BIT(5)
84 #define AES_FLAGS_PERSISTENT AES_FLAGS_BUSY
86 #define ATMEL_AES_QUEUE_LENGTH 50
88 #define ATMEL_AES_DMA_THRESHOLD 256
91 struct atmel_aes_caps
{
101 struct atmel_aes_dev
;
104 typedef int (*atmel_aes_fn_t
)(struct atmel_aes_dev
*);
107 struct atmel_aes_base_ctx
{
108 struct atmel_aes_dev
*dd
;
109 atmel_aes_fn_t start
;
111 u32 key
[AES_KEYSIZE_256
/ sizeof(u32
)];
116 struct atmel_aes_ctx
{
117 struct atmel_aes_base_ctx base
;
120 struct atmel_aes_ctr_ctx
{
121 struct atmel_aes_base_ctx base
;
123 u32 iv
[AES_BLOCK_SIZE
/ sizeof(u32
)];
125 struct scatterlist src
[2];
126 struct scatterlist dst
[2];
129 struct atmel_aes_gcm_ctx
{
130 struct atmel_aes_base_ctx base
;
132 struct scatterlist src
[2];
133 struct scatterlist dst
[2];
135 u32 j0
[AES_BLOCK_SIZE
/ sizeof(u32
)];
136 u32 tag
[AES_BLOCK_SIZE
/ sizeof(u32
)];
137 u32 ghash
[AES_BLOCK_SIZE
/ sizeof(u32
)];
142 atmel_aes_fn_t ghash_resume
;
145 struct atmel_aes_xts_ctx
{
146 struct atmel_aes_base_ctx base
;
148 u32 key2
[AES_KEYSIZE_256
/ sizeof(u32
)];
151 #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
152 struct atmel_aes_authenc_ctx
{
153 struct atmel_aes_base_ctx base
;
154 struct atmel_sha_authenc_ctx
*auth
;
158 struct atmel_aes_reqctx
{
160 u32 lastc
[AES_BLOCK_SIZE
/ sizeof(u32
)];
163 #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
164 struct atmel_aes_authenc_reqctx
{
165 struct atmel_aes_reqctx base
;
167 struct scatterlist src
[2];
168 struct scatterlist dst
[2];
170 u32 digest
[SHA512_DIGEST_SIZE
/ sizeof(u32
)];
172 /* auth_req MUST be place last. */
173 struct ahash_request auth_req
;
177 struct atmel_aes_dma
{
178 struct dma_chan
*chan
;
179 struct scatterlist
*sg
;
181 unsigned int remainder
;
185 struct atmel_aes_dev
{
186 struct list_head list
;
187 unsigned long phys_base
;
188 void __iomem
*io_base
;
190 struct crypto_async_request
*areq
;
191 struct atmel_aes_base_ctx
*ctx
;
194 atmel_aes_fn_t resume
;
195 atmel_aes_fn_t cpu_transfer_complete
;
204 struct crypto_queue queue
;
206 struct tasklet_struct done_task
;
207 struct tasklet_struct queue_task
;
213 struct atmel_aes_dma src
;
214 struct atmel_aes_dma dst
;
218 struct scatterlist aligned_sg
;
219 struct scatterlist
*real_dst
;
221 struct atmel_aes_caps caps
;
226 struct atmel_aes_drv
{
227 struct list_head dev_list
;
231 static struct atmel_aes_drv atmel_aes
= {
232 .dev_list
= LIST_HEAD_INIT(atmel_aes
.dev_list
),
233 .lock
= __SPIN_LOCK_UNLOCKED(atmel_aes
.lock
),
237 static const char *atmel_aes_reg_name(u32 offset
, char *tmp
, size_t sz
)
266 snprintf(tmp
, sz
, "KEYWR[%u]", (offset
- AES_KEYWR(0)) >> 2);
273 snprintf(tmp
, sz
, "IDATAR[%u]", (offset
- AES_IDATAR(0)) >> 2);
280 snprintf(tmp
, sz
, "ODATAR[%u]", (offset
- AES_ODATAR(0)) >> 2);
287 snprintf(tmp
, sz
, "IVR[%u]", (offset
- AES_IVR(0)) >> 2);
300 snprintf(tmp
, sz
, "GHASHR[%u]", (offset
- AES_GHASHR(0)) >> 2);
307 snprintf(tmp
, sz
, "TAGR[%u]", (offset
- AES_TAGR(0)) >> 2);
317 snprintf(tmp
, sz
, "GCMHR[%u]", (offset
- AES_GCMHR(0)) >> 2);
327 snprintf(tmp
, sz
, "TWR[%u]", (offset
- AES_TWR(0)) >> 2);
334 snprintf(tmp
, sz
, "ALPHAR[%u]", (offset
- AES_ALPHAR(0)) >> 2);
338 snprintf(tmp
, sz
, "0x%02x", offset
);
344 #endif /* VERBOSE_DEBUG */
346 /* Shared functions */
348 static inline u32
atmel_aes_read(struct atmel_aes_dev
*dd
, u32 offset
)
350 u32 value
= readl_relaxed(dd
->io_base
+ offset
);
353 if (dd
->flags
& AES_FLAGS_DUMP_REG
) {
356 dev_vdbg(dd
->dev
, "read 0x%08x from %s\n", value
,
357 atmel_aes_reg_name(offset
, tmp
, sizeof(tmp
)));
359 #endif /* VERBOSE_DEBUG */
364 static inline void atmel_aes_write(struct atmel_aes_dev
*dd
,
365 u32 offset
, u32 value
)
368 if (dd
->flags
& AES_FLAGS_DUMP_REG
) {
371 dev_vdbg(dd
->dev
, "write 0x%08x into %s\n", value
,
372 atmel_aes_reg_name(offset
, tmp
, sizeof(tmp
)));
374 #endif /* VERBOSE_DEBUG */
376 writel_relaxed(value
, dd
->io_base
+ offset
);
379 static void atmel_aes_read_n(struct atmel_aes_dev
*dd
, u32 offset
,
380 u32
*value
, int count
)
382 for (; count
--; value
++, offset
+= 4)
383 *value
= atmel_aes_read(dd
, offset
);
386 static void atmel_aes_write_n(struct atmel_aes_dev
*dd
, u32 offset
,
387 const u32
*value
, int count
)
389 for (; count
--; value
++, offset
+= 4)
390 atmel_aes_write(dd
, offset
, *value
);
393 static inline void atmel_aes_read_block(struct atmel_aes_dev
*dd
, u32 offset
,
396 atmel_aes_read_n(dd
, offset
, value
, SIZE_IN_WORDS(AES_BLOCK_SIZE
));
399 static inline void atmel_aes_write_block(struct atmel_aes_dev
*dd
, u32 offset
,
402 atmel_aes_write_n(dd
, offset
, value
, SIZE_IN_WORDS(AES_BLOCK_SIZE
));
405 static inline int atmel_aes_wait_for_data_ready(struct atmel_aes_dev
*dd
,
406 atmel_aes_fn_t resume
)
408 u32 isr
= atmel_aes_read(dd
, AES_ISR
);
410 if (unlikely(isr
& AES_INT_DATARDY
))
414 atmel_aes_write(dd
, AES_IER
, AES_INT_DATARDY
);
418 static inline size_t atmel_aes_padlen(size_t len
, size_t block_size
)
420 len
&= block_size
- 1;
421 return len
? block_size
- len
: 0;
424 static struct atmel_aes_dev
*atmel_aes_find_dev(struct atmel_aes_base_ctx
*ctx
)
426 struct atmel_aes_dev
*aes_dd
= NULL
;
427 struct atmel_aes_dev
*tmp
;
429 spin_lock_bh(&atmel_aes
.lock
);
431 list_for_each_entry(tmp
, &atmel_aes
.dev_list
, list
) {
440 spin_unlock_bh(&atmel_aes
.lock
);
445 static int atmel_aes_hw_init(struct atmel_aes_dev
*dd
)
449 err
= clk_enable(dd
->iclk
);
453 atmel_aes_write(dd
, AES_CR
, AES_CR_SWRST
);
454 atmel_aes_write(dd
, AES_MR
, 0xE << AES_MR_CKEY_OFFSET
);
459 static inline unsigned int atmel_aes_get_version(struct atmel_aes_dev
*dd
)
461 return atmel_aes_read(dd
, AES_HW_VERSION
) & 0x00000fff;
464 static int atmel_aes_hw_version_init(struct atmel_aes_dev
*dd
)
468 err
= atmel_aes_hw_init(dd
);
472 dd
->hw_version
= atmel_aes_get_version(dd
);
474 dev_info(dd
->dev
, "version: 0x%x\n", dd
->hw_version
);
476 clk_disable(dd
->iclk
);
480 static inline void atmel_aes_set_mode(struct atmel_aes_dev
*dd
,
481 const struct atmel_aes_reqctx
*rctx
)
483 /* Clear all but persistent flags and set request flags. */
484 dd
->flags
= (dd
->flags
& AES_FLAGS_PERSISTENT
) | rctx
->mode
;
487 static inline bool atmel_aes_is_encrypt(const struct atmel_aes_dev
*dd
)
489 return (dd
->flags
& AES_FLAGS_ENCRYPT
);
492 #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
493 static void atmel_aes_authenc_complete(struct atmel_aes_dev
*dd
, int err
);
496 static inline int atmel_aes_complete(struct atmel_aes_dev
*dd
, int err
)
498 #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
499 if (dd
->ctx
->is_aead
)
500 atmel_aes_authenc_complete(dd
, err
);
503 clk_disable(dd
->iclk
);
504 dd
->flags
&= ~AES_FLAGS_BUSY
;
506 if (!dd
->ctx
->is_aead
) {
507 struct ablkcipher_request
*req
=
508 ablkcipher_request_cast(dd
->areq
);
509 struct atmel_aes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
510 struct crypto_ablkcipher
*ablkcipher
=
511 crypto_ablkcipher_reqtfm(req
);
512 int ivsize
= crypto_ablkcipher_ivsize(ablkcipher
);
514 if (rctx
->mode
& AES_FLAGS_ENCRYPT
) {
515 scatterwalk_map_and_copy(req
->info
, req
->dst
,
516 req
->nbytes
- ivsize
, ivsize
, 0);
518 if (req
->src
== req
->dst
) {
519 memcpy(req
->info
, rctx
->lastc
, ivsize
);
521 scatterwalk_map_and_copy(req
->info
, req
->src
,
522 req
->nbytes
- ivsize
, ivsize
, 0);
528 dd
->areq
->complete(dd
->areq
, err
);
530 tasklet_schedule(&dd
->queue_task
);
535 static void atmel_aes_write_ctrl_key(struct atmel_aes_dev
*dd
, bool use_dma
,
536 const u32
*iv
, const u32
*key
, int keylen
)
540 /* MR register must be set before IV registers */
541 if (keylen
== AES_KEYSIZE_128
)
542 valmr
|= AES_MR_KEYSIZE_128
;
543 else if (keylen
== AES_KEYSIZE_192
)
544 valmr
|= AES_MR_KEYSIZE_192
;
546 valmr
|= AES_MR_KEYSIZE_256
;
548 valmr
|= dd
->flags
& AES_FLAGS_MODE_MASK
;
551 valmr
|= AES_MR_SMOD_IDATAR0
;
552 if (dd
->caps
.has_dualbuff
)
553 valmr
|= AES_MR_DUALBUFF
;
555 valmr
|= AES_MR_SMOD_AUTO
;
558 atmel_aes_write(dd
, AES_MR
, valmr
);
560 atmel_aes_write_n(dd
, AES_KEYWR(0), key
, SIZE_IN_WORDS(keylen
));
562 if (iv
&& (valmr
& AES_MR_OPMOD_MASK
) != AES_MR_OPMOD_ECB
)
563 atmel_aes_write_block(dd
, AES_IVR(0), iv
);
566 static inline void atmel_aes_write_ctrl(struct atmel_aes_dev
*dd
, bool use_dma
,
570 atmel_aes_write_ctrl_key(dd
, use_dma
, iv
,
571 dd
->ctx
->key
, dd
->ctx
->keylen
);
576 static int atmel_aes_cpu_transfer(struct atmel_aes_dev
*dd
)
582 atmel_aes_read_block(dd
, AES_ODATAR(0), dd
->data
);
584 dd
->datalen
-= AES_BLOCK_SIZE
;
586 if (dd
->datalen
< AES_BLOCK_SIZE
)
589 atmel_aes_write_block(dd
, AES_IDATAR(0), dd
->data
);
591 isr
= atmel_aes_read(dd
, AES_ISR
);
592 if (!(isr
& AES_INT_DATARDY
)) {
593 dd
->resume
= atmel_aes_cpu_transfer
;
594 atmel_aes_write(dd
, AES_IER
, AES_INT_DATARDY
);
599 if (!sg_copy_from_buffer(dd
->real_dst
, sg_nents(dd
->real_dst
),
604 return atmel_aes_complete(dd
, err
);
606 return dd
->cpu_transfer_complete(dd
);
609 static int atmel_aes_cpu_start(struct atmel_aes_dev
*dd
,
610 struct scatterlist
*src
,
611 struct scatterlist
*dst
,
613 atmel_aes_fn_t resume
)
615 size_t padlen
= atmel_aes_padlen(len
, AES_BLOCK_SIZE
);
617 if (unlikely(len
== 0))
620 sg_copy_to_buffer(src
, sg_nents(src
), dd
->buf
, len
);
624 dd
->cpu_transfer_complete
= resume
;
625 dd
->datalen
= len
+ padlen
;
626 dd
->data
= (u32
*)dd
->buf
;
627 atmel_aes_write_block(dd
, AES_IDATAR(0), dd
->data
);
628 return atmel_aes_wait_for_data_ready(dd
, atmel_aes_cpu_transfer
);
634 static void atmel_aes_dma_callback(void *data
);
636 static bool atmel_aes_check_aligned(struct atmel_aes_dev
*dd
,
637 struct scatterlist
*sg
,
639 struct atmel_aes_dma
*dma
)
643 if (!IS_ALIGNED(len
, dd
->ctx
->block_size
))
646 for (nents
= 0; sg
; sg
= sg_next(sg
), ++nents
) {
647 if (!IS_ALIGNED(sg
->offset
, sizeof(u32
)))
650 if (len
<= sg
->length
) {
651 if (!IS_ALIGNED(len
, dd
->ctx
->block_size
))
654 dma
->nents
= nents
+1;
655 dma
->remainder
= sg
->length
- len
;
660 if (!IS_ALIGNED(sg
->length
, dd
->ctx
->block_size
))
669 static inline void atmel_aes_restore_sg(const struct atmel_aes_dma
*dma
)
671 struct scatterlist
*sg
= dma
->sg
;
672 int nents
= dma
->nents
;
677 while (--nents
> 0 && sg
)
683 sg
->length
+= dma
->remainder
;
686 static int atmel_aes_map(struct atmel_aes_dev
*dd
,
687 struct scatterlist
*src
,
688 struct scatterlist
*dst
,
691 bool src_aligned
, dst_aligned
;
699 src_aligned
= atmel_aes_check_aligned(dd
, src
, len
, &dd
->src
);
701 dst_aligned
= src_aligned
;
703 dst_aligned
= atmel_aes_check_aligned(dd
, dst
, len
, &dd
->dst
);
704 if (!src_aligned
|| !dst_aligned
) {
705 padlen
= atmel_aes_padlen(len
, dd
->ctx
->block_size
);
707 if (dd
->buflen
< len
+ padlen
)
711 sg_copy_to_buffer(src
, sg_nents(src
), dd
->buf
, len
);
712 dd
->src
.sg
= &dd
->aligned_sg
;
714 dd
->src
.remainder
= 0;
718 dd
->dst
.sg
= &dd
->aligned_sg
;
720 dd
->dst
.remainder
= 0;
723 sg_init_table(&dd
->aligned_sg
, 1);
724 sg_set_buf(&dd
->aligned_sg
, dd
->buf
, len
+ padlen
);
727 if (dd
->src
.sg
== dd
->dst
.sg
) {
728 dd
->src
.sg_len
= dma_map_sg(dd
->dev
, dd
->src
.sg
, dd
->src
.nents
,
730 dd
->dst
.sg_len
= dd
->src
.sg_len
;
734 dd
->src
.sg_len
= dma_map_sg(dd
->dev
, dd
->src
.sg
, dd
->src
.nents
,
739 dd
->dst
.sg_len
= dma_map_sg(dd
->dev
, dd
->dst
.sg
, dd
->dst
.nents
,
741 if (!dd
->dst
.sg_len
) {
742 dma_unmap_sg(dd
->dev
, dd
->src
.sg
, dd
->src
.nents
,
751 static void atmel_aes_unmap(struct atmel_aes_dev
*dd
)
753 if (dd
->src
.sg
== dd
->dst
.sg
) {
754 dma_unmap_sg(dd
->dev
, dd
->src
.sg
, dd
->src
.nents
,
757 if (dd
->src
.sg
!= &dd
->aligned_sg
)
758 atmel_aes_restore_sg(&dd
->src
);
760 dma_unmap_sg(dd
->dev
, dd
->dst
.sg
, dd
->dst
.nents
,
763 if (dd
->dst
.sg
!= &dd
->aligned_sg
)
764 atmel_aes_restore_sg(&dd
->dst
);
766 dma_unmap_sg(dd
->dev
, dd
->src
.sg
, dd
->src
.nents
,
769 if (dd
->src
.sg
!= &dd
->aligned_sg
)
770 atmel_aes_restore_sg(&dd
->src
);
773 if (dd
->dst
.sg
== &dd
->aligned_sg
)
774 sg_copy_from_buffer(dd
->real_dst
, sg_nents(dd
->real_dst
),
778 static int atmel_aes_dma_transfer_start(struct atmel_aes_dev
*dd
,
779 enum dma_slave_buswidth addr_width
,
780 enum dma_transfer_direction dir
,
783 struct dma_async_tx_descriptor
*desc
;
784 struct dma_slave_config config
;
785 dma_async_tx_callback callback
;
786 struct atmel_aes_dma
*dma
;
789 memset(&config
, 0, sizeof(config
));
790 config
.direction
= dir
;
791 config
.src_addr_width
= addr_width
;
792 config
.dst_addr_width
= addr_width
;
793 config
.src_maxburst
= maxburst
;
794 config
.dst_maxburst
= maxburst
;
800 config
.dst_addr
= dd
->phys_base
+ AES_IDATAR(0);
805 callback
= atmel_aes_dma_callback
;
806 config
.src_addr
= dd
->phys_base
+ AES_ODATAR(0);
813 err
= dmaengine_slave_config(dma
->chan
, &config
);
817 desc
= dmaengine_prep_slave_sg(dma
->chan
, dma
->sg
, dma
->sg_len
, dir
,
818 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
822 desc
->callback
= callback
;
823 desc
->callback_param
= dd
;
824 dmaengine_submit(desc
);
825 dma_async_issue_pending(dma
->chan
);
830 static void atmel_aes_dma_transfer_stop(struct atmel_aes_dev
*dd
,
831 enum dma_transfer_direction dir
)
833 struct atmel_aes_dma
*dma
;
848 dmaengine_terminate_all(dma
->chan
);
851 static int atmel_aes_dma_start(struct atmel_aes_dev
*dd
,
852 struct scatterlist
*src
,
853 struct scatterlist
*dst
,
855 atmel_aes_fn_t resume
)
857 enum dma_slave_buswidth addr_width
;
861 switch (dd
->ctx
->block_size
) {
862 case CFB8_BLOCK_SIZE
:
863 addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
867 case CFB16_BLOCK_SIZE
:
868 addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
872 case CFB32_BLOCK_SIZE
:
873 case CFB64_BLOCK_SIZE
:
874 addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
879 addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
880 maxburst
= dd
->caps
.max_burst_size
;
888 err
= atmel_aes_map(dd
, src
, dst
, len
);
894 /* Set output DMA transfer first */
895 err
= atmel_aes_dma_transfer_start(dd
, addr_width
, DMA_DEV_TO_MEM
,
900 /* Then set input DMA transfer */
901 err
= atmel_aes_dma_transfer_start(dd
, addr_width
, DMA_MEM_TO_DEV
,
904 goto output_transfer_stop
;
908 output_transfer_stop
:
909 atmel_aes_dma_transfer_stop(dd
, DMA_DEV_TO_MEM
);
913 return atmel_aes_complete(dd
, err
);
916 static void atmel_aes_dma_stop(struct atmel_aes_dev
*dd
)
918 atmel_aes_dma_transfer_stop(dd
, DMA_MEM_TO_DEV
);
919 atmel_aes_dma_transfer_stop(dd
, DMA_DEV_TO_MEM
);
923 static void atmel_aes_dma_callback(void *data
)
925 struct atmel_aes_dev
*dd
= data
;
927 atmel_aes_dma_stop(dd
);
929 (void)dd
->resume(dd
);
932 static int atmel_aes_handle_queue(struct atmel_aes_dev
*dd
,
933 struct crypto_async_request
*new_areq
)
935 struct crypto_async_request
*areq
, *backlog
;
936 struct atmel_aes_base_ctx
*ctx
;
941 spin_lock_irqsave(&dd
->lock
, flags
);
943 ret
= crypto_enqueue_request(&dd
->queue
, new_areq
);
944 if (dd
->flags
& AES_FLAGS_BUSY
) {
945 spin_unlock_irqrestore(&dd
->lock
, flags
);
948 backlog
= crypto_get_backlog(&dd
->queue
);
949 areq
= crypto_dequeue_request(&dd
->queue
);
951 dd
->flags
|= AES_FLAGS_BUSY
;
952 spin_unlock_irqrestore(&dd
->lock
, flags
);
958 backlog
->complete(backlog
, -EINPROGRESS
);
960 ctx
= crypto_tfm_ctx(areq
->tfm
);
964 start_async
= (areq
!= new_areq
);
965 dd
->is_async
= start_async
;
967 /* WARNING: ctx->start() MAY change dd->is_async. */
968 err
= ctx
->start(dd
);
969 return (start_async
) ? ret
: err
;
973 /* AES async block ciphers */
975 static int atmel_aes_transfer_complete(struct atmel_aes_dev
*dd
)
977 return atmel_aes_complete(dd
, 0);
980 static int atmel_aes_start(struct atmel_aes_dev
*dd
)
982 struct ablkcipher_request
*req
= ablkcipher_request_cast(dd
->areq
);
983 struct atmel_aes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
984 bool use_dma
= (req
->nbytes
>= ATMEL_AES_DMA_THRESHOLD
||
985 dd
->ctx
->block_size
!= AES_BLOCK_SIZE
);
988 atmel_aes_set_mode(dd
, rctx
);
990 err
= atmel_aes_hw_init(dd
);
992 return atmel_aes_complete(dd
, err
);
994 atmel_aes_write_ctrl(dd
, use_dma
, req
->info
);
996 return atmel_aes_dma_start(dd
, req
->src
, req
->dst
, req
->nbytes
,
997 atmel_aes_transfer_complete
);
999 return atmel_aes_cpu_start(dd
, req
->src
, req
->dst
, req
->nbytes
,
1000 atmel_aes_transfer_complete
);
1003 static inline struct atmel_aes_ctr_ctx
*
1004 atmel_aes_ctr_ctx_cast(struct atmel_aes_base_ctx
*ctx
)
1006 return container_of(ctx
, struct atmel_aes_ctr_ctx
, base
);
1009 static int atmel_aes_ctr_transfer(struct atmel_aes_dev
*dd
)
1011 struct atmel_aes_ctr_ctx
*ctx
= atmel_aes_ctr_ctx_cast(dd
->ctx
);
1012 struct ablkcipher_request
*req
= ablkcipher_request_cast(dd
->areq
);
1013 struct scatterlist
*src
, *dst
;
1016 bool use_dma
, fragmented
= false;
1018 /* Check for transfer completion. */
1019 ctx
->offset
+= dd
->total
;
1020 if (ctx
->offset
>= req
->nbytes
)
1021 return atmel_aes_transfer_complete(dd
);
1023 /* Compute data length. */
1024 datalen
= req
->nbytes
- ctx
->offset
;
1025 blocks
= DIV_ROUND_UP(datalen
, AES_BLOCK_SIZE
);
1026 ctr
= be32_to_cpu(ctx
->iv
[3]);
1027 if (dd
->caps
.has_ctr32
) {
1028 /* Check 32bit counter overflow. */
1030 u32 end
= start
+ blocks
- 1;
1034 datalen
= AES_BLOCK_SIZE
* -start
;
1038 /* Check 16bit counter overflow. */
1039 u16 start
= ctr
& 0xffff;
1040 u16 end
= start
+ (u16
)blocks
- 1;
1042 if (blocks
>> 16 || end
< start
) {
1044 datalen
= AES_BLOCK_SIZE
* (0x10000-start
);
1048 use_dma
= (datalen
>= ATMEL_AES_DMA_THRESHOLD
);
1050 /* Jump to offset. */
1051 src
= scatterwalk_ffwd(ctx
->src
, req
->src
, ctx
->offset
);
1052 dst
= ((req
->src
== req
->dst
) ? src
:
1053 scatterwalk_ffwd(ctx
->dst
, req
->dst
, ctx
->offset
));
1055 /* Configure hardware. */
1056 atmel_aes_write_ctrl(dd
, use_dma
, ctx
->iv
);
1057 if (unlikely(fragmented
)) {
1059 * Increment the counter manually to cope with the hardware
1062 ctx
->iv
[3] = cpu_to_be32(ctr
);
1063 crypto_inc((u8
*)ctx
->iv
, AES_BLOCK_SIZE
);
1067 return atmel_aes_dma_start(dd
, src
, dst
, datalen
,
1068 atmel_aes_ctr_transfer
);
1070 return atmel_aes_cpu_start(dd
, src
, dst
, datalen
,
1071 atmel_aes_ctr_transfer
);
1074 static int atmel_aes_ctr_start(struct atmel_aes_dev
*dd
)
1076 struct atmel_aes_ctr_ctx
*ctx
= atmel_aes_ctr_ctx_cast(dd
->ctx
);
1077 struct ablkcipher_request
*req
= ablkcipher_request_cast(dd
->areq
);
1078 struct atmel_aes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
1081 atmel_aes_set_mode(dd
, rctx
);
1083 err
= atmel_aes_hw_init(dd
);
1085 return atmel_aes_complete(dd
, err
);
1087 memcpy(ctx
->iv
, req
->info
, AES_BLOCK_SIZE
);
1090 return atmel_aes_ctr_transfer(dd
);
1093 static int atmel_aes_crypt(struct ablkcipher_request
*req
, unsigned long mode
)
1095 struct crypto_ablkcipher
*ablkcipher
= crypto_ablkcipher_reqtfm(req
);
1096 struct atmel_aes_base_ctx
*ctx
= crypto_ablkcipher_ctx(ablkcipher
);
1097 struct atmel_aes_reqctx
*rctx
;
1098 struct atmel_aes_dev
*dd
;
1100 switch (mode
& AES_FLAGS_OPMODE_MASK
) {
1101 case AES_FLAGS_CFB8
:
1102 ctx
->block_size
= CFB8_BLOCK_SIZE
;
1105 case AES_FLAGS_CFB16
:
1106 ctx
->block_size
= CFB16_BLOCK_SIZE
;
1109 case AES_FLAGS_CFB32
:
1110 ctx
->block_size
= CFB32_BLOCK_SIZE
;
1113 case AES_FLAGS_CFB64
:
1114 ctx
->block_size
= CFB64_BLOCK_SIZE
;
1118 ctx
->block_size
= AES_BLOCK_SIZE
;
1121 ctx
->is_aead
= false;
1123 dd
= atmel_aes_find_dev(ctx
);
1127 rctx
= ablkcipher_request_ctx(req
);
1130 if (!(mode
& AES_FLAGS_ENCRYPT
) && (req
->src
== req
->dst
)) {
1131 int ivsize
= crypto_ablkcipher_ivsize(ablkcipher
);
1133 scatterwalk_map_and_copy(rctx
->lastc
, req
->src
,
1134 (req
->nbytes
- ivsize
), ivsize
, 0);
1137 return atmel_aes_handle_queue(dd
, &req
->base
);
1140 static int atmel_aes_setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
1141 unsigned int keylen
)
1143 struct atmel_aes_base_ctx
*ctx
= crypto_ablkcipher_ctx(tfm
);
1145 if (keylen
!= AES_KEYSIZE_128
&&
1146 keylen
!= AES_KEYSIZE_192
&&
1147 keylen
!= AES_KEYSIZE_256
) {
1148 crypto_ablkcipher_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
1152 memcpy(ctx
->key
, key
, keylen
);
1153 ctx
->keylen
= keylen
;
1158 static int atmel_aes_ecb_encrypt(struct ablkcipher_request
*req
)
1160 return atmel_aes_crypt(req
, AES_FLAGS_ECB
| AES_FLAGS_ENCRYPT
);
1163 static int atmel_aes_ecb_decrypt(struct ablkcipher_request
*req
)
1165 return atmel_aes_crypt(req
, AES_FLAGS_ECB
);
1168 static int atmel_aes_cbc_encrypt(struct ablkcipher_request
*req
)
1170 return atmel_aes_crypt(req
, AES_FLAGS_CBC
| AES_FLAGS_ENCRYPT
);
1173 static int atmel_aes_cbc_decrypt(struct ablkcipher_request
*req
)
1175 return atmel_aes_crypt(req
, AES_FLAGS_CBC
);
1178 static int atmel_aes_ofb_encrypt(struct ablkcipher_request
*req
)
1180 return atmel_aes_crypt(req
, AES_FLAGS_OFB
| AES_FLAGS_ENCRYPT
);
1183 static int atmel_aes_ofb_decrypt(struct ablkcipher_request
*req
)
1185 return atmel_aes_crypt(req
, AES_FLAGS_OFB
);
1188 static int atmel_aes_cfb_encrypt(struct ablkcipher_request
*req
)
1190 return atmel_aes_crypt(req
, AES_FLAGS_CFB128
| AES_FLAGS_ENCRYPT
);
1193 static int atmel_aes_cfb_decrypt(struct ablkcipher_request
*req
)
1195 return atmel_aes_crypt(req
, AES_FLAGS_CFB128
);
1198 static int atmel_aes_cfb64_encrypt(struct ablkcipher_request
*req
)
1200 return atmel_aes_crypt(req
, AES_FLAGS_CFB64
| AES_FLAGS_ENCRYPT
);
1203 static int atmel_aes_cfb64_decrypt(struct ablkcipher_request
*req
)
1205 return atmel_aes_crypt(req
, AES_FLAGS_CFB64
);
1208 static int atmel_aes_cfb32_encrypt(struct ablkcipher_request
*req
)
1210 return atmel_aes_crypt(req
, AES_FLAGS_CFB32
| AES_FLAGS_ENCRYPT
);
1213 static int atmel_aes_cfb32_decrypt(struct ablkcipher_request
*req
)
1215 return atmel_aes_crypt(req
, AES_FLAGS_CFB32
);
1218 static int atmel_aes_cfb16_encrypt(struct ablkcipher_request
*req
)
1220 return atmel_aes_crypt(req
, AES_FLAGS_CFB16
| AES_FLAGS_ENCRYPT
);
1223 static int atmel_aes_cfb16_decrypt(struct ablkcipher_request
*req
)
1225 return atmel_aes_crypt(req
, AES_FLAGS_CFB16
);
1228 static int atmel_aes_cfb8_encrypt(struct ablkcipher_request
*req
)
1230 return atmel_aes_crypt(req
, AES_FLAGS_CFB8
| AES_FLAGS_ENCRYPT
);
1233 static int atmel_aes_cfb8_decrypt(struct ablkcipher_request
*req
)
1235 return atmel_aes_crypt(req
, AES_FLAGS_CFB8
);
1238 static int atmel_aes_ctr_encrypt(struct ablkcipher_request
*req
)
1240 return atmel_aes_crypt(req
, AES_FLAGS_CTR
| AES_FLAGS_ENCRYPT
);
1243 static int atmel_aes_ctr_decrypt(struct ablkcipher_request
*req
)
1245 return atmel_aes_crypt(req
, AES_FLAGS_CTR
);
1248 static int atmel_aes_cra_init(struct crypto_tfm
*tfm
)
1250 struct atmel_aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
1252 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct atmel_aes_reqctx
);
1253 ctx
->base
.start
= atmel_aes_start
;
1258 static int atmel_aes_ctr_cra_init(struct crypto_tfm
*tfm
)
1260 struct atmel_aes_ctx
*ctx
= crypto_tfm_ctx(tfm
);
1262 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct atmel_aes_reqctx
);
1263 ctx
->base
.start
= atmel_aes_ctr_start
;
1268 static struct crypto_alg aes_algs
[] = {
1270 .cra_name
= "ecb(aes)",
1271 .cra_driver_name
= "atmel-ecb-aes",
1272 .cra_priority
= ATMEL_AES_PRIORITY
,
1273 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1274 .cra_blocksize
= AES_BLOCK_SIZE
,
1275 .cra_ctxsize
= sizeof(struct atmel_aes_ctx
),
1276 .cra_alignmask
= 0xf,
1277 .cra_type
= &crypto_ablkcipher_type
,
1278 .cra_module
= THIS_MODULE
,
1279 .cra_init
= atmel_aes_cra_init
,
1280 .cra_u
.ablkcipher
= {
1281 .min_keysize
= AES_MIN_KEY_SIZE
,
1282 .max_keysize
= AES_MAX_KEY_SIZE
,
1283 .setkey
= atmel_aes_setkey
,
1284 .encrypt
= atmel_aes_ecb_encrypt
,
1285 .decrypt
= atmel_aes_ecb_decrypt
,
1289 .cra_name
= "cbc(aes)",
1290 .cra_driver_name
= "atmel-cbc-aes",
1291 .cra_priority
= ATMEL_AES_PRIORITY
,
1292 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1293 .cra_blocksize
= AES_BLOCK_SIZE
,
1294 .cra_ctxsize
= sizeof(struct atmel_aes_ctx
),
1295 .cra_alignmask
= 0xf,
1296 .cra_type
= &crypto_ablkcipher_type
,
1297 .cra_module
= THIS_MODULE
,
1298 .cra_init
= atmel_aes_cra_init
,
1299 .cra_u
.ablkcipher
= {
1300 .min_keysize
= AES_MIN_KEY_SIZE
,
1301 .max_keysize
= AES_MAX_KEY_SIZE
,
1302 .ivsize
= AES_BLOCK_SIZE
,
1303 .setkey
= atmel_aes_setkey
,
1304 .encrypt
= atmel_aes_cbc_encrypt
,
1305 .decrypt
= atmel_aes_cbc_decrypt
,
1309 .cra_name
= "ofb(aes)",
1310 .cra_driver_name
= "atmel-ofb-aes",
1311 .cra_priority
= ATMEL_AES_PRIORITY
,
1312 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1313 .cra_blocksize
= AES_BLOCK_SIZE
,
1314 .cra_ctxsize
= sizeof(struct atmel_aes_ctx
),
1315 .cra_alignmask
= 0xf,
1316 .cra_type
= &crypto_ablkcipher_type
,
1317 .cra_module
= THIS_MODULE
,
1318 .cra_init
= atmel_aes_cra_init
,
1319 .cra_u
.ablkcipher
= {
1320 .min_keysize
= AES_MIN_KEY_SIZE
,
1321 .max_keysize
= AES_MAX_KEY_SIZE
,
1322 .ivsize
= AES_BLOCK_SIZE
,
1323 .setkey
= atmel_aes_setkey
,
1324 .encrypt
= atmel_aes_ofb_encrypt
,
1325 .decrypt
= atmel_aes_ofb_decrypt
,
1329 .cra_name
= "cfb(aes)",
1330 .cra_driver_name
= "atmel-cfb-aes",
1331 .cra_priority
= ATMEL_AES_PRIORITY
,
1332 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1333 .cra_blocksize
= AES_BLOCK_SIZE
,
1334 .cra_ctxsize
= sizeof(struct atmel_aes_ctx
),
1335 .cra_alignmask
= 0xf,
1336 .cra_type
= &crypto_ablkcipher_type
,
1337 .cra_module
= THIS_MODULE
,
1338 .cra_init
= atmel_aes_cra_init
,
1339 .cra_u
.ablkcipher
= {
1340 .min_keysize
= AES_MIN_KEY_SIZE
,
1341 .max_keysize
= AES_MAX_KEY_SIZE
,
1342 .ivsize
= AES_BLOCK_SIZE
,
1343 .setkey
= atmel_aes_setkey
,
1344 .encrypt
= atmel_aes_cfb_encrypt
,
1345 .decrypt
= atmel_aes_cfb_decrypt
,
1349 .cra_name
= "cfb32(aes)",
1350 .cra_driver_name
= "atmel-cfb32-aes",
1351 .cra_priority
= ATMEL_AES_PRIORITY
,
1352 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1353 .cra_blocksize
= CFB32_BLOCK_SIZE
,
1354 .cra_ctxsize
= sizeof(struct atmel_aes_ctx
),
1355 .cra_alignmask
= 0x3,
1356 .cra_type
= &crypto_ablkcipher_type
,
1357 .cra_module
= THIS_MODULE
,
1358 .cra_init
= atmel_aes_cra_init
,
1359 .cra_u
.ablkcipher
= {
1360 .min_keysize
= AES_MIN_KEY_SIZE
,
1361 .max_keysize
= AES_MAX_KEY_SIZE
,
1362 .ivsize
= AES_BLOCK_SIZE
,
1363 .setkey
= atmel_aes_setkey
,
1364 .encrypt
= atmel_aes_cfb32_encrypt
,
1365 .decrypt
= atmel_aes_cfb32_decrypt
,
1369 .cra_name
= "cfb16(aes)",
1370 .cra_driver_name
= "atmel-cfb16-aes",
1371 .cra_priority
= ATMEL_AES_PRIORITY
,
1372 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1373 .cra_blocksize
= CFB16_BLOCK_SIZE
,
1374 .cra_ctxsize
= sizeof(struct atmel_aes_ctx
),
1375 .cra_alignmask
= 0x1,
1376 .cra_type
= &crypto_ablkcipher_type
,
1377 .cra_module
= THIS_MODULE
,
1378 .cra_init
= atmel_aes_cra_init
,
1379 .cra_u
.ablkcipher
= {
1380 .min_keysize
= AES_MIN_KEY_SIZE
,
1381 .max_keysize
= AES_MAX_KEY_SIZE
,
1382 .ivsize
= AES_BLOCK_SIZE
,
1383 .setkey
= atmel_aes_setkey
,
1384 .encrypt
= atmel_aes_cfb16_encrypt
,
1385 .decrypt
= atmel_aes_cfb16_decrypt
,
1389 .cra_name
= "cfb8(aes)",
1390 .cra_driver_name
= "atmel-cfb8-aes",
1391 .cra_priority
= ATMEL_AES_PRIORITY
,
1392 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1393 .cra_blocksize
= CFB8_BLOCK_SIZE
,
1394 .cra_ctxsize
= sizeof(struct atmel_aes_ctx
),
1395 .cra_alignmask
= 0x0,
1396 .cra_type
= &crypto_ablkcipher_type
,
1397 .cra_module
= THIS_MODULE
,
1398 .cra_init
= atmel_aes_cra_init
,
1399 .cra_u
.ablkcipher
= {
1400 .min_keysize
= AES_MIN_KEY_SIZE
,
1401 .max_keysize
= AES_MAX_KEY_SIZE
,
1402 .ivsize
= AES_BLOCK_SIZE
,
1403 .setkey
= atmel_aes_setkey
,
1404 .encrypt
= atmel_aes_cfb8_encrypt
,
1405 .decrypt
= atmel_aes_cfb8_decrypt
,
1409 .cra_name
= "ctr(aes)",
1410 .cra_driver_name
= "atmel-ctr-aes",
1411 .cra_priority
= ATMEL_AES_PRIORITY
,
1412 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1414 .cra_ctxsize
= sizeof(struct atmel_aes_ctr_ctx
),
1415 .cra_alignmask
= 0xf,
1416 .cra_type
= &crypto_ablkcipher_type
,
1417 .cra_module
= THIS_MODULE
,
1418 .cra_init
= atmel_aes_ctr_cra_init
,
1419 .cra_u
.ablkcipher
= {
1420 .min_keysize
= AES_MIN_KEY_SIZE
,
1421 .max_keysize
= AES_MAX_KEY_SIZE
,
1422 .ivsize
= AES_BLOCK_SIZE
,
1423 .setkey
= atmel_aes_setkey
,
1424 .encrypt
= atmel_aes_ctr_encrypt
,
1425 .decrypt
= atmel_aes_ctr_decrypt
,
1430 static struct crypto_alg aes_cfb64_alg
= {
1431 .cra_name
= "cfb64(aes)",
1432 .cra_driver_name
= "atmel-cfb64-aes",
1433 .cra_priority
= ATMEL_AES_PRIORITY
,
1434 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1435 .cra_blocksize
= CFB64_BLOCK_SIZE
,
1436 .cra_ctxsize
= sizeof(struct atmel_aes_ctx
),
1437 .cra_alignmask
= 0x7,
1438 .cra_type
= &crypto_ablkcipher_type
,
1439 .cra_module
= THIS_MODULE
,
1440 .cra_init
= atmel_aes_cra_init
,
1441 .cra_u
.ablkcipher
= {
1442 .min_keysize
= AES_MIN_KEY_SIZE
,
1443 .max_keysize
= AES_MAX_KEY_SIZE
,
1444 .ivsize
= AES_BLOCK_SIZE
,
1445 .setkey
= atmel_aes_setkey
,
1446 .encrypt
= atmel_aes_cfb64_encrypt
,
1447 .decrypt
= atmel_aes_cfb64_decrypt
,
1452 /* gcm aead functions */
1454 static int atmel_aes_gcm_ghash(struct atmel_aes_dev
*dd
,
1455 const u32
*data
, size_t datalen
,
1456 const u32
*ghash_in
, u32
*ghash_out
,
1457 atmel_aes_fn_t resume
);
1458 static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev
*dd
);
1459 static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev
*dd
);
1461 static int atmel_aes_gcm_start(struct atmel_aes_dev
*dd
);
1462 static int atmel_aes_gcm_process(struct atmel_aes_dev
*dd
);
1463 static int atmel_aes_gcm_length(struct atmel_aes_dev
*dd
);
1464 static int atmel_aes_gcm_data(struct atmel_aes_dev
*dd
);
1465 static int atmel_aes_gcm_tag_init(struct atmel_aes_dev
*dd
);
1466 static int atmel_aes_gcm_tag(struct atmel_aes_dev
*dd
);
1467 static int atmel_aes_gcm_finalize(struct atmel_aes_dev
*dd
);
1469 static inline struct atmel_aes_gcm_ctx
*
1470 atmel_aes_gcm_ctx_cast(struct atmel_aes_base_ctx
*ctx
)
1472 return container_of(ctx
, struct atmel_aes_gcm_ctx
, base
);
1475 static int atmel_aes_gcm_ghash(struct atmel_aes_dev
*dd
,
1476 const u32
*data
, size_t datalen
,
1477 const u32
*ghash_in
, u32
*ghash_out
,
1478 atmel_aes_fn_t resume
)
1480 struct atmel_aes_gcm_ctx
*ctx
= atmel_aes_gcm_ctx_cast(dd
->ctx
);
1482 dd
->data
= (u32
*)data
;
1483 dd
->datalen
= datalen
;
1484 ctx
->ghash_in
= ghash_in
;
1485 ctx
->ghash_out
= ghash_out
;
1486 ctx
->ghash_resume
= resume
;
1488 atmel_aes_write_ctrl(dd
, false, NULL
);
1489 return atmel_aes_wait_for_data_ready(dd
, atmel_aes_gcm_ghash_init
);
1492 static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev
*dd
)
1494 struct atmel_aes_gcm_ctx
*ctx
= atmel_aes_gcm_ctx_cast(dd
->ctx
);
1496 /* Set the data length. */
1497 atmel_aes_write(dd
, AES_AADLENR
, dd
->total
);
1498 atmel_aes_write(dd
, AES_CLENR
, 0);
1500 /* If needed, overwrite the GCM Intermediate Hash Word Registers */
1502 atmel_aes_write_block(dd
, AES_GHASHR(0), ctx
->ghash_in
);
1504 return atmel_aes_gcm_ghash_finalize(dd
);
1507 static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev
*dd
)
1509 struct atmel_aes_gcm_ctx
*ctx
= atmel_aes_gcm_ctx_cast(dd
->ctx
);
1512 /* Write data into the Input Data Registers. */
1513 while (dd
->datalen
> 0) {
1514 atmel_aes_write_block(dd
, AES_IDATAR(0), dd
->data
);
1516 dd
->datalen
-= AES_BLOCK_SIZE
;
1518 isr
= atmel_aes_read(dd
, AES_ISR
);
1519 if (!(isr
& AES_INT_DATARDY
)) {
1520 dd
->resume
= atmel_aes_gcm_ghash_finalize
;
1521 atmel_aes_write(dd
, AES_IER
, AES_INT_DATARDY
);
1522 return -EINPROGRESS
;
1526 /* Read the computed hash from GHASHRx. */
1527 atmel_aes_read_block(dd
, AES_GHASHR(0), ctx
->ghash_out
);
1529 return ctx
->ghash_resume(dd
);
1533 static int atmel_aes_gcm_start(struct atmel_aes_dev
*dd
)
1535 struct atmel_aes_gcm_ctx
*ctx
= atmel_aes_gcm_ctx_cast(dd
->ctx
);
1536 struct aead_request
*req
= aead_request_cast(dd
->areq
);
1537 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1538 struct atmel_aes_reqctx
*rctx
= aead_request_ctx(req
);
1539 size_t ivsize
= crypto_aead_ivsize(tfm
);
1540 size_t datalen
, padlen
;
1541 const void *iv
= req
->iv
;
1545 atmel_aes_set_mode(dd
, rctx
);
1547 err
= atmel_aes_hw_init(dd
);
1549 return atmel_aes_complete(dd
, err
);
1551 if (likely(ivsize
== GCM_AES_IV_SIZE
)) {
1552 memcpy(ctx
->j0
, iv
, ivsize
);
1553 ctx
->j0
[3] = cpu_to_be32(1);
1554 return atmel_aes_gcm_process(dd
);
1557 padlen
= atmel_aes_padlen(ivsize
, AES_BLOCK_SIZE
);
1558 datalen
= ivsize
+ padlen
+ AES_BLOCK_SIZE
;
1559 if (datalen
> dd
->buflen
)
1560 return atmel_aes_complete(dd
, -EINVAL
);
1562 memcpy(data
, iv
, ivsize
);
1563 memset(data
+ ivsize
, 0, padlen
+ sizeof(u64
));
1564 ((u64
*)(data
+ datalen
))[-1] = cpu_to_be64(ivsize
* 8);
1566 return atmel_aes_gcm_ghash(dd
, (const u32
*)data
, datalen
,
1567 NULL
, ctx
->j0
, atmel_aes_gcm_process
);
1570 static int atmel_aes_gcm_process(struct atmel_aes_dev
*dd
)
1572 struct atmel_aes_gcm_ctx
*ctx
= atmel_aes_gcm_ctx_cast(dd
->ctx
);
1573 struct aead_request
*req
= aead_request_cast(dd
->areq
);
1574 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1575 bool enc
= atmel_aes_is_encrypt(dd
);
1578 /* Compute text length. */
1579 authsize
= crypto_aead_authsize(tfm
);
1580 ctx
->textlen
= req
->cryptlen
- (enc
? 0 : authsize
);
1583 * According to tcrypt test suite, the GCM Automatic Tag Generation
1584 * fails when both the message and its associated data are empty.
1586 if (likely(req
->assoclen
!= 0 || ctx
->textlen
!= 0))
1587 dd
->flags
|= AES_FLAGS_GTAGEN
;
1589 atmel_aes_write_ctrl(dd
, false, NULL
);
1590 return atmel_aes_wait_for_data_ready(dd
, atmel_aes_gcm_length
);
1593 static int atmel_aes_gcm_length(struct atmel_aes_dev
*dd
)
1595 struct atmel_aes_gcm_ctx
*ctx
= atmel_aes_gcm_ctx_cast(dd
->ctx
);
1596 struct aead_request
*req
= aead_request_cast(dd
->areq
);
1597 u32 j0_lsw
, *j0
= ctx
->j0
;
1600 /* Write incr32(J0) into IV. */
1602 j0
[3] = cpu_to_be32(be32_to_cpu(j0
[3]) + 1);
1603 atmel_aes_write_block(dd
, AES_IVR(0), j0
);
1606 /* Set aad and text lengths. */
1607 atmel_aes_write(dd
, AES_AADLENR
, req
->assoclen
);
1608 atmel_aes_write(dd
, AES_CLENR
, ctx
->textlen
);
1610 /* Check whether AAD are present. */
1611 if (unlikely(req
->assoclen
== 0)) {
1613 return atmel_aes_gcm_data(dd
);
1616 /* Copy assoc data and add padding. */
1617 padlen
= atmel_aes_padlen(req
->assoclen
, AES_BLOCK_SIZE
);
1618 if (unlikely(req
->assoclen
+ padlen
> dd
->buflen
))
1619 return atmel_aes_complete(dd
, -EINVAL
);
1620 sg_copy_to_buffer(req
->src
, sg_nents(req
->src
), dd
->buf
, req
->assoclen
);
1622 /* Write assoc data into the Input Data register. */
1623 dd
->data
= (u32
*)dd
->buf
;
1624 dd
->datalen
= req
->assoclen
+ padlen
;
1625 return atmel_aes_gcm_data(dd
);
1628 static int atmel_aes_gcm_data(struct atmel_aes_dev
*dd
)
1630 struct atmel_aes_gcm_ctx
*ctx
= atmel_aes_gcm_ctx_cast(dd
->ctx
);
1631 struct aead_request
*req
= aead_request_cast(dd
->areq
);
1632 bool use_dma
= (ctx
->textlen
>= ATMEL_AES_DMA_THRESHOLD
);
1633 struct scatterlist
*src
, *dst
;
1636 /* Write AAD first. */
1637 while (dd
->datalen
> 0) {
1638 atmel_aes_write_block(dd
, AES_IDATAR(0), dd
->data
);
1640 dd
->datalen
-= AES_BLOCK_SIZE
;
1642 isr
= atmel_aes_read(dd
, AES_ISR
);
1643 if (!(isr
& AES_INT_DATARDY
)) {
1644 dd
->resume
= atmel_aes_gcm_data
;
1645 atmel_aes_write(dd
, AES_IER
, AES_INT_DATARDY
);
1646 return -EINPROGRESS
;
1651 if (unlikely(ctx
->textlen
== 0))
1652 return atmel_aes_gcm_tag_init(dd
);
1654 /* Prepare src and dst scatter lists to transfer cipher/plain texts */
1655 src
= scatterwalk_ffwd(ctx
->src
, req
->src
, req
->assoclen
);
1656 dst
= ((req
->src
== req
->dst
) ? src
:
1657 scatterwalk_ffwd(ctx
->dst
, req
->dst
, req
->assoclen
));
1660 /* Update the Mode Register for DMA transfers. */
1661 mr
= atmel_aes_read(dd
, AES_MR
);
1662 mr
&= ~(AES_MR_SMOD_MASK
| AES_MR_DUALBUFF
);
1663 mr
|= AES_MR_SMOD_IDATAR0
;
1664 if (dd
->caps
.has_dualbuff
)
1665 mr
|= AES_MR_DUALBUFF
;
1666 atmel_aes_write(dd
, AES_MR
, mr
);
1668 return atmel_aes_dma_start(dd
, src
, dst
, ctx
->textlen
,
1669 atmel_aes_gcm_tag_init
);
1672 return atmel_aes_cpu_start(dd
, src
, dst
, ctx
->textlen
,
1673 atmel_aes_gcm_tag_init
);
1676 static int atmel_aes_gcm_tag_init(struct atmel_aes_dev
*dd
)
1678 struct atmel_aes_gcm_ctx
*ctx
= atmel_aes_gcm_ctx_cast(dd
->ctx
);
1679 struct aead_request
*req
= aead_request_cast(dd
->areq
);
1680 u64
*data
= dd
->buf
;
1682 if (likely(dd
->flags
& AES_FLAGS_GTAGEN
)) {
1683 if (!(atmel_aes_read(dd
, AES_ISR
) & AES_INT_TAGRDY
)) {
1684 dd
->resume
= atmel_aes_gcm_tag_init
;
1685 atmel_aes_write(dd
, AES_IER
, AES_INT_TAGRDY
);
1686 return -EINPROGRESS
;
1689 return atmel_aes_gcm_finalize(dd
);
1692 /* Read the GCM Intermediate Hash Word Registers. */
1693 atmel_aes_read_block(dd
, AES_GHASHR(0), ctx
->ghash
);
1695 data
[0] = cpu_to_be64(req
->assoclen
* 8);
1696 data
[1] = cpu_to_be64(ctx
->textlen
* 8);
1698 return atmel_aes_gcm_ghash(dd
, (const u32
*)data
, AES_BLOCK_SIZE
,
1699 ctx
->ghash
, ctx
->ghash
, atmel_aes_gcm_tag
);
1702 static int atmel_aes_gcm_tag(struct atmel_aes_dev
*dd
)
1704 struct atmel_aes_gcm_ctx
*ctx
= atmel_aes_gcm_ctx_cast(dd
->ctx
);
1705 unsigned long flags
;
1708 * Change mode to CTR to complete the tag generation.
1709 * Use J0 as Initialization Vector.
1712 dd
->flags
&= ~(AES_FLAGS_OPMODE_MASK
| AES_FLAGS_GTAGEN
);
1713 dd
->flags
|= AES_FLAGS_CTR
;
1714 atmel_aes_write_ctrl(dd
, false, ctx
->j0
);
1717 atmel_aes_write_block(dd
, AES_IDATAR(0), ctx
->ghash
);
1718 return atmel_aes_wait_for_data_ready(dd
, atmel_aes_gcm_finalize
);
1721 static int atmel_aes_gcm_finalize(struct atmel_aes_dev
*dd
)
1723 struct atmel_aes_gcm_ctx
*ctx
= atmel_aes_gcm_ctx_cast(dd
->ctx
);
1724 struct aead_request
*req
= aead_request_cast(dd
->areq
);
1725 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1726 bool enc
= atmel_aes_is_encrypt(dd
);
1727 u32 offset
, authsize
, itag
[4], *otag
= ctx
->tag
;
1730 /* Read the computed tag. */
1731 if (likely(dd
->flags
& AES_FLAGS_GTAGEN
))
1732 atmel_aes_read_block(dd
, AES_TAGR(0), ctx
->tag
);
1734 atmel_aes_read_block(dd
, AES_ODATAR(0), ctx
->tag
);
1736 offset
= req
->assoclen
+ ctx
->textlen
;
1737 authsize
= crypto_aead_authsize(tfm
);
1739 scatterwalk_map_and_copy(otag
, req
->dst
, offset
, authsize
, 1);
1742 scatterwalk_map_and_copy(itag
, req
->src
, offset
, authsize
, 0);
1743 err
= crypto_memneq(itag
, otag
, authsize
) ? -EBADMSG
: 0;
1746 return atmel_aes_complete(dd
, err
);
1749 static int atmel_aes_gcm_crypt(struct aead_request
*req
,
1752 struct atmel_aes_base_ctx
*ctx
;
1753 struct atmel_aes_reqctx
*rctx
;
1754 struct atmel_aes_dev
*dd
;
1756 ctx
= crypto_aead_ctx(crypto_aead_reqtfm(req
));
1757 ctx
->block_size
= AES_BLOCK_SIZE
;
1758 ctx
->is_aead
= true;
1760 dd
= atmel_aes_find_dev(ctx
);
1764 rctx
= aead_request_ctx(req
);
1765 rctx
->mode
= AES_FLAGS_GCM
| mode
;
1767 return atmel_aes_handle_queue(dd
, &req
->base
);
1770 static int atmel_aes_gcm_setkey(struct crypto_aead
*tfm
, const u8
*key
,
1771 unsigned int keylen
)
1773 struct atmel_aes_base_ctx
*ctx
= crypto_aead_ctx(tfm
);
1775 if (keylen
!= AES_KEYSIZE_256
&&
1776 keylen
!= AES_KEYSIZE_192
&&
1777 keylen
!= AES_KEYSIZE_128
) {
1778 crypto_aead_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
1782 memcpy(ctx
->key
, key
, keylen
);
1783 ctx
->keylen
= keylen
;
1788 static int atmel_aes_gcm_setauthsize(struct crypto_aead
*tfm
,
1789 unsigned int authsize
)
1791 /* Same as crypto_gcm_authsize() from crypto/gcm.c */
1808 static int atmel_aes_gcm_encrypt(struct aead_request
*req
)
1810 return atmel_aes_gcm_crypt(req
, AES_FLAGS_ENCRYPT
);
1813 static int atmel_aes_gcm_decrypt(struct aead_request
*req
)
1815 return atmel_aes_gcm_crypt(req
, 0);
1818 static int atmel_aes_gcm_init(struct crypto_aead
*tfm
)
1820 struct atmel_aes_gcm_ctx
*ctx
= crypto_aead_ctx(tfm
);
1822 crypto_aead_set_reqsize(tfm
, sizeof(struct atmel_aes_reqctx
));
1823 ctx
->base
.start
= atmel_aes_gcm_start
;
1828 static struct aead_alg aes_gcm_alg
= {
1829 .setkey
= atmel_aes_gcm_setkey
,
1830 .setauthsize
= atmel_aes_gcm_setauthsize
,
1831 .encrypt
= atmel_aes_gcm_encrypt
,
1832 .decrypt
= atmel_aes_gcm_decrypt
,
1833 .init
= atmel_aes_gcm_init
,
1834 .ivsize
= GCM_AES_IV_SIZE
,
1835 .maxauthsize
= AES_BLOCK_SIZE
,
1838 .cra_name
= "gcm(aes)",
1839 .cra_driver_name
= "atmel-gcm-aes",
1840 .cra_priority
= ATMEL_AES_PRIORITY
,
1841 .cra_flags
= CRYPTO_ALG_ASYNC
,
1843 .cra_ctxsize
= sizeof(struct atmel_aes_gcm_ctx
),
1844 .cra_alignmask
= 0xf,
1845 .cra_module
= THIS_MODULE
,
1852 static inline struct atmel_aes_xts_ctx
*
1853 atmel_aes_xts_ctx_cast(struct atmel_aes_base_ctx
*ctx
)
1855 return container_of(ctx
, struct atmel_aes_xts_ctx
, base
);
1858 static int atmel_aes_xts_process_data(struct atmel_aes_dev
*dd
);
1860 static int atmel_aes_xts_start(struct atmel_aes_dev
*dd
)
1862 struct atmel_aes_xts_ctx
*ctx
= atmel_aes_xts_ctx_cast(dd
->ctx
);
1863 struct ablkcipher_request
*req
= ablkcipher_request_cast(dd
->areq
);
1864 struct atmel_aes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
1865 unsigned long flags
;
1868 atmel_aes_set_mode(dd
, rctx
);
1870 err
= atmel_aes_hw_init(dd
);
1872 return atmel_aes_complete(dd
, err
);
1874 /* Compute the tweak value from req->info with ecb(aes). */
1876 dd
->flags
&= ~AES_FLAGS_MODE_MASK
;
1877 dd
->flags
|= (AES_FLAGS_ECB
| AES_FLAGS_ENCRYPT
);
1878 atmel_aes_write_ctrl_key(dd
, false, NULL
,
1879 ctx
->key2
, ctx
->base
.keylen
);
1882 atmel_aes_write_block(dd
, AES_IDATAR(0), req
->info
);
1883 return atmel_aes_wait_for_data_ready(dd
, atmel_aes_xts_process_data
);
1886 static int atmel_aes_xts_process_data(struct atmel_aes_dev
*dd
)
1888 struct ablkcipher_request
*req
= ablkcipher_request_cast(dd
->areq
);
1889 bool use_dma
= (req
->nbytes
>= ATMEL_AES_DMA_THRESHOLD
);
1890 u32 tweak
[AES_BLOCK_SIZE
/ sizeof(u32
)];
1891 static const u32 one
[AES_BLOCK_SIZE
/ sizeof(u32
)] = {cpu_to_le32(1), };
1892 u8
*tweak_bytes
= (u8
*)tweak
;
1895 /* Read the computed ciphered tweak value. */
1896 atmel_aes_read_block(dd
, AES_ODATAR(0), tweak
);
1899 * the order of the ciphered tweak bytes need to be reversed before
1900 * writing them into the ODATARx registers.
1902 for (i
= 0; i
< AES_BLOCK_SIZE
/2; ++i
) {
1903 u8 tmp
= tweak_bytes
[AES_BLOCK_SIZE
- 1 - i
];
1905 tweak_bytes
[AES_BLOCK_SIZE
- 1 - i
] = tweak_bytes
[i
];
1906 tweak_bytes
[i
] = tmp
;
1909 /* Process the data. */
1910 atmel_aes_write_ctrl(dd
, use_dma
, NULL
);
1911 atmel_aes_write_block(dd
, AES_TWR(0), tweak
);
1912 atmel_aes_write_block(dd
, AES_ALPHAR(0), one
);
1914 return atmel_aes_dma_start(dd
, req
->src
, req
->dst
, req
->nbytes
,
1915 atmel_aes_transfer_complete
);
1917 return atmel_aes_cpu_start(dd
, req
->src
, req
->dst
, req
->nbytes
,
1918 atmel_aes_transfer_complete
);
1921 static int atmel_aes_xts_setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
1922 unsigned int keylen
)
1924 struct atmel_aes_xts_ctx
*ctx
= crypto_ablkcipher_ctx(tfm
);
1927 err
= xts_check_key(crypto_ablkcipher_tfm(tfm
), key
, keylen
);
1931 memcpy(ctx
->base
.key
, key
, keylen
/2);
1932 memcpy(ctx
->key2
, key
+ keylen
/2, keylen
/2);
1933 ctx
->base
.keylen
= keylen
/2;
1938 static int atmel_aes_xts_encrypt(struct ablkcipher_request
*req
)
1940 return atmel_aes_crypt(req
, AES_FLAGS_XTS
| AES_FLAGS_ENCRYPT
);
1943 static int atmel_aes_xts_decrypt(struct ablkcipher_request
*req
)
1945 return atmel_aes_crypt(req
, AES_FLAGS_XTS
);
1948 static int atmel_aes_xts_cra_init(struct crypto_tfm
*tfm
)
1950 struct atmel_aes_xts_ctx
*ctx
= crypto_tfm_ctx(tfm
);
1952 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct atmel_aes_reqctx
);
1953 ctx
->base
.start
= atmel_aes_xts_start
;
1958 static struct crypto_alg aes_xts_alg
= {
1959 .cra_name
= "xts(aes)",
1960 .cra_driver_name
= "atmel-xts-aes",
1961 .cra_priority
= ATMEL_AES_PRIORITY
,
1962 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
| CRYPTO_ALG_ASYNC
,
1963 .cra_blocksize
= AES_BLOCK_SIZE
,
1964 .cra_ctxsize
= sizeof(struct atmel_aes_xts_ctx
),
1965 .cra_alignmask
= 0xf,
1966 .cra_type
= &crypto_ablkcipher_type
,
1967 .cra_module
= THIS_MODULE
,
1968 .cra_init
= atmel_aes_xts_cra_init
,
1969 .cra_u
.ablkcipher
= {
1970 .min_keysize
= 2 * AES_MIN_KEY_SIZE
,
1971 .max_keysize
= 2 * AES_MAX_KEY_SIZE
,
1972 .ivsize
= AES_BLOCK_SIZE
,
1973 .setkey
= atmel_aes_xts_setkey
,
1974 .encrypt
= atmel_aes_xts_encrypt
,
1975 .decrypt
= atmel_aes_xts_decrypt
,
1979 #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
1980 /* authenc aead functions */
1982 static int atmel_aes_authenc_start(struct atmel_aes_dev
*dd
);
1983 static int atmel_aes_authenc_init(struct atmel_aes_dev
*dd
, int err
,
1985 static int atmel_aes_authenc_transfer(struct atmel_aes_dev
*dd
, int err
,
1987 static int atmel_aes_authenc_digest(struct atmel_aes_dev
*dd
);
1988 static int atmel_aes_authenc_final(struct atmel_aes_dev
*dd
, int err
,
1991 static void atmel_aes_authenc_complete(struct atmel_aes_dev
*dd
, int err
)
1993 struct aead_request
*req
= aead_request_cast(dd
->areq
);
1994 struct atmel_aes_authenc_reqctx
*rctx
= aead_request_ctx(req
);
1996 if (err
&& (dd
->flags
& AES_FLAGS_OWN_SHA
))
1997 atmel_sha_authenc_abort(&rctx
->auth_req
);
1998 dd
->flags
&= ~AES_FLAGS_OWN_SHA
;
2001 static int atmel_aes_authenc_start(struct atmel_aes_dev
*dd
)
2003 struct aead_request
*req
= aead_request_cast(dd
->areq
);
2004 struct atmel_aes_authenc_reqctx
*rctx
= aead_request_ctx(req
);
2005 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
2006 struct atmel_aes_authenc_ctx
*ctx
= crypto_aead_ctx(tfm
);
2009 atmel_aes_set_mode(dd
, &rctx
->base
);
2011 err
= atmel_aes_hw_init(dd
);
2013 return atmel_aes_complete(dd
, err
);
2015 return atmel_sha_authenc_schedule(&rctx
->auth_req
, ctx
->auth
,
2016 atmel_aes_authenc_init
, dd
);
2019 static int atmel_aes_authenc_init(struct atmel_aes_dev
*dd
, int err
,
2022 struct aead_request
*req
= aead_request_cast(dd
->areq
);
2023 struct atmel_aes_authenc_reqctx
*rctx
= aead_request_ctx(req
);
2026 dd
->is_async
= true;
2028 return atmel_aes_complete(dd
, err
);
2030 /* If here, we've got the ownership of the SHA device. */
2031 dd
->flags
|= AES_FLAGS_OWN_SHA
;
2033 /* Configure the SHA device. */
2034 return atmel_sha_authenc_init(&rctx
->auth_req
,
2035 req
->src
, req
->assoclen
,
2037 atmel_aes_authenc_transfer
, dd
);
2040 static int atmel_aes_authenc_transfer(struct atmel_aes_dev
*dd
, int err
,
2043 struct aead_request
*req
= aead_request_cast(dd
->areq
);
2044 struct atmel_aes_authenc_reqctx
*rctx
= aead_request_ctx(req
);
2045 bool enc
= atmel_aes_is_encrypt(dd
);
2046 struct scatterlist
*src
, *dst
;
2047 u32 iv
[AES_BLOCK_SIZE
/ sizeof(u32
)];
2051 dd
->is_async
= true;
2053 return atmel_aes_complete(dd
, err
);
2055 /* Prepare src and dst scatter-lists to transfer cipher/plain texts. */
2056 src
= scatterwalk_ffwd(rctx
->src
, req
->src
, req
->assoclen
);
2059 if (req
->src
!= req
->dst
)
2060 dst
= scatterwalk_ffwd(rctx
->dst
, req
->dst
, req
->assoclen
);
2062 /* Configure the AES device. */
2063 memcpy(iv
, req
->iv
, sizeof(iv
));
2066 * Here we always set the 2nd parameter of atmel_aes_write_ctrl() to
2067 * 'true' even if the data transfer is actually performed by the CPU (so
2068 * not by the DMA) because we must force the AES_MR_SMOD bitfield to the
2069 * value AES_MR_SMOD_IDATAR0. Indeed, both AES_MR_SMOD and SHA_MR_SMOD
2070 * must be set to *_MR_SMOD_IDATAR0.
2072 atmel_aes_write_ctrl(dd
, true, iv
);
2073 emr
= AES_EMR_PLIPEN
;
2075 emr
|= AES_EMR_PLIPD
;
2076 atmel_aes_write(dd
, AES_EMR
, emr
);
2078 /* Transfer data. */
2079 return atmel_aes_dma_start(dd
, src
, dst
, rctx
->textlen
,
2080 atmel_aes_authenc_digest
);
2083 static int atmel_aes_authenc_digest(struct atmel_aes_dev
*dd
)
2085 struct aead_request
*req
= aead_request_cast(dd
->areq
);
2086 struct atmel_aes_authenc_reqctx
*rctx
= aead_request_ctx(req
);
2088 /* atmel_sha_authenc_final() releases the SHA device. */
2089 dd
->flags
&= ~AES_FLAGS_OWN_SHA
;
2090 return atmel_sha_authenc_final(&rctx
->auth_req
,
2091 rctx
->digest
, sizeof(rctx
->digest
),
2092 atmel_aes_authenc_final
, dd
);
2095 static int atmel_aes_authenc_final(struct atmel_aes_dev
*dd
, int err
,
2098 struct aead_request
*req
= aead_request_cast(dd
->areq
);
2099 struct atmel_aes_authenc_reqctx
*rctx
= aead_request_ctx(req
);
2100 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
2101 bool enc
= atmel_aes_is_encrypt(dd
);
2102 u32 idigest
[SHA512_DIGEST_SIZE
/ sizeof(u32
)], *odigest
= rctx
->digest
;
2106 dd
->is_async
= true;
2110 offs
= req
->assoclen
+ rctx
->textlen
;
2111 authsize
= crypto_aead_authsize(tfm
);
2113 scatterwalk_map_and_copy(odigest
, req
->dst
, offs
, authsize
, 1);
2115 scatterwalk_map_and_copy(idigest
, req
->src
, offs
, authsize
, 0);
2116 if (crypto_memneq(idigest
, odigest
, authsize
))
2121 return atmel_aes_complete(dd
, err
);
2124 static int atmel_aes_authenc_setkey(struct crypto_aead
*tfm
, const u8
*key
,
2125 unsigned int keylen
)
2127 struct atmel_aes_authenc_ctx
*ctx
= crypto_aead_ctx(tfm
);
2128 struct crypto_authenc_keys keys
;
2132 if (crypto_authenc_extractkeys(&keys
, key
, keylen
) != 0)
2135 if (keys
.enckeylen
> sizeof(ctx
->base
.key
))
2138 /* Save auth key. */
2139 flags
= crypto_aead_get_flags(tfm
);
2140 err
= atmel_sha_authenc_setkey(ctx
->auth
,
2141 keys
.authkey
, keys
.authkeylen
,
2143 crypto_aead_set_flags(tfm
, flags
& CRYPTO_TFM_RES_MASK
);
2145 memzero_explicit(&keys
, sizeof(keys
));
2150 ctx
->base
.keylen
= keys
.enckeylen
;
2151 memcpy(ctx
->base
.key
, keys
.enckey
, keys
.enckeylen
);
2153 memzero_explicit(&keys
, sizeof(keys
));
2157 crypto_aead_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
2158 memzero_explicit(&key
, sizeof(keys
));
2162 static int atmel_aes_authenc_init_tfm(struct crypto_aead
*tfm
,
2163 unsigned long auth_mode
)
2165 struct atmel_aes_authenc_ctx
*ctx
= crypto_aead_ctx(tfm
);
2166 unsigned int auth_reqsize
= atmel_sha_authenc_get_reqsize();
2168 ctx
->auth
= atmel_sha_authenc_spawn(auth_mode
);
2169 if (IS_ERR(ctx
->auth
))
2170 return PTR_ERR(ctx
->auth
);
2172 crypto_aead_set_reqsize(tfm
, (sizeof(struct atmel_aes_authenc_reqctx
) +
2174 ctx
->base
.start
= atmel_aes_authenc_start
;
2179 static int atmel_aes_authenc_hmac_sha1_init_tfm(struct crypto_aead
*tfm
)
2181 return atmel_aes_authenc_init_tfm(tfm
, SHA_FLAGS_HMAC_SHA1
);
2184 static int atmel_aes_authenc_hmac_sha224_init_tfm(struct crypto_aead
*tfm
)
2186 return atmel_aes_authenc_init_tfm(tfm
, SHA_FLAGS_HMAC_SHA224
);
2189 static int atmel_aes_authenc_hmac_sha256_init_tfm(struct crypto_aead
*tfm
)
2191 return atmel_aes_authenc_init_tfm(tfm
, SHA_FLAGS_HMAC_SHA256
);
2194 static int atmel_aes_authenc_hmac_sha384_init_tfm(struct crypto_aead
*tfm
)
2196 return atmel_aes_authenc_init_tfm(tfm
, SHA_FLAGS_HMAC_SHA384
);
2199 static int atmel_aes_authenc_hmac_sha512_init_tfm(struct crypto_aead
*tfm
)
2201 return atmel_aes_authenc_init_tfm(tfm
, SHA_FLAGS_HMAC_SHA512
);
2204 static void atmel_aes_authenc_exit_tfm(struct crypto_aead
*tfm
)
2206 struct atmel_aes_authenc_ctx
*ctx
= crypto_aead_ctx(tfm
);
2208 atmel_sha_authenc_free(ctx
->auth
);
2211 static int atmel_aes_authenc_crypt(struct aead_request
*req
,
2214 struct atmel_aes_authenc_reqctx
*rctx
= aead_request_ctx(req
);
2215 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
2216 struct atmel_aes_base_ctx
*ctx
= crypto_aead_ctx(tfm
);
2217 u32 authsize
= crypto_aead_authsize(tfm
);
2218 bool enc
= (mode
& AES_FLAGS_ENCRYPT
);
2219 struct atmel_aes_dev
*dd
;
2221 /* Compute text length. */
2222 if (!enc
&& req
->cryptlen
< authsize
)
2224 rctx
->textlen
= req
->cryptlen
- (enc
? 0 : authsize
);
2227 * Currently, empty messages are not supported yet:
2228 * the SHA auto-padding can be used only on non-empty messages.
2229 * Hence a special case needs to be implemented for empty message.
2231 if (!rctx
->textlen
&& !req
->assoclen
)
2234 rctx
->base
.mode
= mode
;
2235 ctx
->block_size
= AES_BLOCK_SIZE
;
2236 ctx
->is_aead
= true;
2238 dd
= atmel_aes_find_dev(ctx
);
2242 return atmel_aes_handle_queue(dd
, &req
->base
);
2245 static int atmel_aes_authenc_cbc_aes_encrypt(struct aead_request
*req
)
2247 return atmel_aes_authenc_crypt(req
, AES_FLAGS_CBC
| AES_FLAGS_ENCRYPT
);
2250 static int atmel_aes_authenc_cbc_aes_decrypt(struct aead_request
*req
)
2252 return atmel_aes_authenc_crypt(req
, AES_FLAGS_CBC
);
2255 static struct aead_alg aes_authenc_algs
[] = {
2257 .setkey
= atmel_aes_authenc_setkey
,
2258 .encrypt
= atmel_aes_authenc_cbc_aes_encrypt
,
2259 .decrypt
= atmel_aes_authenc_cbc_aes_decrypt
,
2260 .init
= atmel_aes_authenc_hmac_sha1_init_tfm
,
2261 .exit
= atmel_aes_authenc_exit_tfm
,
2262 .ivsize
= AES_BLOCK_SIZE
,
2263 .maxauthsize
= SHA1_DIGEST_SIZE
,
2266 .cra_name
= "authenc(hmac(sha1),cbc(aes))",
2267 .cra_driver_name
= "atmel-authenc-hmac-sha1-cbc-aes",
2268 .cra_priority
= ATMEL_AES_PRIORITY
,
2269 .cra_flags
= CRYPTO_ALG_ASYNC
,
2270 .cra_blocksize
= AES_BLOCK_SIZE
,
2271 .cra_ctxsize
= sizeof(struct atmel_aes_authenc_ctx
),
2272 .cra_alignmask
= 0xf,
2273 .cra_module
= THIS_MODULE
,
2277 .setkey
= atmel_aes_authenc_setkey
,
2278 .encrypt
= atmel_aes_authenc_cbc_aes_encrypt
,
2279 .decrypt
= atmel_aes_authenc_cbc_aes_decrypt
,
2280 .init
= atmel_aes_authenc_hmac_sha224_init_tfm
,
2281 .exit
= atmel_aes_authenc_exit_tfm
,
2282 .ivsize
= AES_BLOCK_SIZE
,
2283 .maxauthsize
= SHA224_DIGEST_SIZE
,
2286 .cra_name
= "authenc(hmac(sha224),cbc(aes))",
2287 .cra_driver_name
= "atmel-authenc-hmac-sha224-cbc-aes",
2288 .cra_priority
= ATMEL_AES_PRIORITY
,
2289 .cra_flags
= CRYPTO_ALG_ASYNC
,
2290 .cra_blocksize
= AES_BLOCK_SIZE
,
2291 .cra_ctxsize
= sizeof(struct atmel_aes_authenc_ctx
),
2292 .cra_alignmask
= 0xf,
2293 .cra_module
= THIS_MODULE
,
2297 .setkey
= atmel_aes_authenc_setkey
,
2298 .encrypt
= atmel_aes_authenc_cbc_aes_encrypt
,
2299 .decrypt
= atmel_aes_authenc_cbc_aes_decrypt
,
2300 .init
= atmel_aes_authenc_hmac_sha256_init_tfm
,
2301 .exit
= atmel_aes_authenc_exit_tfm
,
2302 .ivsize
= AES_BLOCK_SIZE
,
2303 .maxauthsize
= SHA256_DIGEST_SIZE
,
2306 .cra_name
= "authenc(hmac(sha256),cbc(aes))",
2307 .cra_driver_name
= "atmel-authenc-hmac-sha256-cbc-aes",
2308 .cra_priority
= ATMEL_AES_PRIORITY
,
2309 .cra_flags
= CRYPTO_ALG_ASYNC
,
2310 .cra_blocksize
= AES_BLOCK_SIZE
,
2311 .cra_ctxsize
= sizeof(struct atmel_aes_authenc_ctx
),
2312 .cra_alignmask
= 0xf,
2313 .cra_module
= THIS_MODULE
,
2317 .setkey
= atmel_aes_authenc_setkey
,
2318 .encrypt
= atmel_aes_authenc_cbc_aes_encrypt
,
2319 .decrypt
= atmel_aes_authenc_cbc_aes_decrypt
,
2320 .init
= atmel_aes_authenc_hmac_sha384_init_tfm
,
2321 .exit
= atmel_aes_authenc_exit_tfm
,
2322 .ivsize
= AES_BLOCK_SIZE
,
2323 .maxauthsize
= SHA384_DIGEST_SIZE
,
2326 .cra_name
= "authenc(hmac(sha384),cbc(aes))",
2327 .cra_driver_name
= "atmel-authenc-hmac-sha384-cbc-aes",
2328 .cra_priority
= ATMEL_AES_PRIORITY
,
2329 .cra_flags
= CRYPTO_ALG_ASYNC
,
2330 .cra_blocksize
= AES_BLOCK_SIZE
,
2331 .cra_ctxsize
= sizeof(struct atmel_aes_authenc_ctx
),
2332 .cra_alignmask
= 0xf,
2333 .cra_module
= THIS_MODULE
,
2337 .setkey
= atmel_aes_authenc_setkey
,
2338 .encrypt
= atmel_aes_authenc_cbc_aes_encrypt
,
2339 .decrypt
= atmel_aes_authenc_cbc_aes_decrypt
,
2340 .init
= atmel_aes_authenc_hmac_sha512_init_tfm
,
2341 .exit
= atmel_aes_authenc_exit_tfm
,
2342 .ivsize
= AES_BLOCK_SIZE
,
2343 .maxauthsize
= SHA512_DIGEST_SIZE
,
2346 .cra_name
= "authenc(hmac(sha512),cbc(aes))",
2347 .cra_driver_name
= "atmel-authenc-hmac-sha512-cbc-aes",
2348 .cra_priority
= ATMEL_AES_PRIORITY
,
2349 .cra_flags
= CRYPTO_ALG_ASYNC
,
2350 .cra_blocksize
= AES_BLOCK_SIZE
,
2351 .cra_ctxsize
= sizeof(struct atmel_aes_authenc_ctx
),
2352 .cra_alignmask
= 0xf,
2353 .cra_module
= THIS_MODULE
,
2357 #endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
2359 /* Probe functions */
2361 static int atmel_aes_buff_init(struct atmel_aes_dev
*dd
)
2363 dd
->buf
= (void *)__get_free_pages(GFP_KERNEL
, ATMEL_AES_BUFFER_ORDER
);
2364 dd
->buflen
= ATMEL_AES_BUFFER_SIZE
;
2365 dd
->buflen
&= ~(AES_BLOCK_SIZE
- 1);
2368 dev_err(dd
->dev
, "unable to alloc pages.\n");
2375 static void atmel_aes_buff_cleanup(struct atmel_aes_dev
*dd
)
2377 free_page((unsigned long)dd
->buf
);
2380 static bool atmel_aes_filter(struct dma_chan
*chan
, void *slave
)
2382 struct at_dma_slave
*sl
= slave
;
2384 if (sl
&& sl
->dma_dev
== chan
->device
->dev
) {
2392 static int atmel_aes_dma_init(struct atmel_aes_dev
*dd
,
2393 struct crypto_platform_data
*pdata
)
2395 struct at_dma_slave
*slave
;
2396 dma_cap_mask_t mask
;
2399 dma_cap_set(DMA_SLAVE
, mask
);
2401 /* Try to grab 2 DMA channels */
2402 slave
= &pdata
->dma_slave
->rxdata
;
2403 dd
->src
.chan
= dma_request_slave_channel_compat(mask
, atmel_aes_filter
,
2404 slave
, dd
->dev
, "tx");
2408 slave
= &pdata
->dma_slave
->txdata
;
2409 dd
->dst
.chan
= dma_request_slave_channel_compat(mask
, atmel_aes_filter
,
2410 slave
, dd
->dev
, "rx");
2417 dma_release_channel(dd
->src
.chan
);
2419 dev_warn(dd
->dev
, "no DMA channel available\n");
2423 static void atmel_aes_dma_cleanup(struct atmel_aes_dev
*dd
)
2425 dma_release_channel(dd
->dst
.chan
);
2426 dma_release_channel(dd
->src
.chan
);
2429 static void atmel_aes_queue_task(unsigned long data
)
2431 struct atmel_aes_dev
*dd
= (struct atmel_aes_dev
*)data
;
2433 atmel_aes_handle_queue(dd
, NULL
);
2436 static void atmel_aes_done_task(unsigned long data
)
2438 struct atmel_aes_dev
*dd
= (struct atmel_aes_dev
*)data
;
2440 dd
->is_async
= true;
2441 (void)dd
->resume(dd
);
2444 static irqreturn_t
atmel_aes_irq(int irq
, void *dev_id
)
2446 struct atmel_aes_dev
*aes_dd
= dev_id
;
2449 reg
= atmel_aes_read(aes_dd
, AES_ISR
);
2450 if (reg
& atmel_aes_read(aes_dd
, AES_IMR
)) {
2451 atmel_aes_write(aes_dd
, AES_IDR
, reg
);
2452 if (AES_FLAGS_BUSY
& aes_dd
->flags
)
2453 tasklet_schedule(&aes_dd
->done_task
);
2455 dev_warn(aes_dd
->dev
, "AES interrupt when no active requests.\n");
2462 static void atmel_aes_unregister_algs(struct atmel_aes_dev
*dd
)
2466 #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
2467 if (dd
->caps
.has_authenc
)
2468 for (i
= 0; i
< ARRAY_SIZE(aes_authenc_algs
); i
++)
2469 crypto_unregister_aead(&aes_authenc_algs
[i
]);
2472 if (dd
->caps
.has_xts
)
2473 crypto_unregister_alg(&aes_xts_alg
);
2475 if (dd
->caps
.has_gcm
)
2476 crypto_unregister_aead(&aes_gcm_alg
);
2478 if (dd
->caps
.has_cfb64
)
2479 crypto_unregister_alg(&aes_cfb64_alg
);
2481 for (i
= 0; i
< ARRAY_SIZE(aes_algs
); i
++)
2482 crypto_unregister_alg(&aes_algs
[i
]);
2485 static int atmel_aes_register_algs(struct atmel_aes_dev
*dd
)
2489 for (i
= 0; i
< ARRAY_SIZE(aes_algs
); i
++) {
2490 err
= crypto_register_alg(&aes_algs
[i
]);
2495 if (dd
->caps
.has_cfb64
) {
2496 err
= crypto_register_alg(&aes_cfb64_alg
);
2498 goto err_aes_cfb64_alg
;
2501 if (dd
->caps
.has_gcm
) {
2502 err
= crypto_register_aead(&aes_gcm_alg
);
2504 goto err_aes_gcm_alg
;
2507 if (dd
->caps
.has_xts
) {
2508 err
= crypto_register_alg(&aes_xts_alg
);
2510 goto err_aes_xts_alg
;
2513 #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
2514 if (dd
->caps
.has_authenc
) {
2515 for (i
= 0; i
< ARRAY_SIZE(aes_authenc_algs
); i
++) {
2516 err
= crypto_register_aead(&aes_authenc_algs
[i
]);
2518 goto err_aes_authenc_alg
;
2525 #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
2526 /* i = ARRAY_SIZE(aes_authenc_algs); */
2527 err_aes_authenc_alg
:
2528 for (j
= 0; j
< i
; j
++)
2529 crypto_unregister_aead(&aes_authenc_algs
[j
]);
2530 crypto_unregister_alg(&aes_xts_alg
);
2533 crypto_unregister_aead(&aes_gcm_alg
);
2535 crypto_unregister_alg(&aes_cfb64_alg
);
2537 i
= ARRAY_SIZE(aes_algs
);
2539 for (j
= 0; j
< i
; j
++)
2540 crypto_unregister_alg(&aes_algs
[j
]);
2545 static void atmel_aes_get_cap(struct atmel_aes_dev
*dd
)
2547 dd
->caps
.has_dualbuff
= 0;
2548 dd
->caps
.has_cfb64
= 0;
2549 dd
->caps
.has_ctr32
= 0;
2550 dd
->caps
.has_gcm
= 0;
2551 dd
->caps
.has_xts
= 0;
2552 dd
->caps
.has_authenc
= 0;
2553 dd
->caps
.max_burst_size
= 1;
2555 /* keep only major version number */
2556 switch (dd
->hw_version
& 0xff0) {
2558 dd
->caps
.has_dualbuff
= 1;
2559 dd
->caps
.has_cfb64
= 1;
2560 dd
->caps
.has_ctr32
= 1;
2561 dd
->caps
.has_gcm
= 1;
2562 dd
->caps
.has_xts
= 1;
2563 dd
->caps
.has_authenc
= 1;
2564 dd
->caps
.max_burst_size
= 4;
2567 dd
->caps
.has_dualbuff
= 1;
2568 dd
->caps
.has_cfb64
= 1;
2569 dd
->caps
.has_ctr32
= 1;
2570 dd
->caps
.has_gcm
= 1;
2571 dd
->caps
.max_burst_size
= 4;
2574 dd
->caps
.has_dualbuff
= 1;
2575 dd
->caps
.has_cfb64
= 1;
2576 dd
->caps
.max_burst_size
= 4;
2582 "Unmanaged aes version, set minimum capabilities\n");
2587 #if defined(CONFIG_OF)
2588 static const struct of_device_id atmel_aes_dt_ids
[] = {
2589 { .compatible
= "atmel,at91sam9g46-aes" },
2592 MODULE_DEVICE_TABLE(of
, atmel_aes_dt_ids
);
2594 static struct crypto_platform_data
*atmel_aes_of_init(struct platform_device
*pdev
)
2596 struct device_node
*np
= pdev
->dev
.of_node
;
2597 struct crypto_platform_data
*pdata
;
2600 dev_err(&pdev
->dev
, "device node not found\n");
2601 return ERR_PTR(-EINVAL
);
2604 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
2606 dev_err(&pdev
->dev
, "could not allocate memory for pdata\n");
2607 return ERR_PTR(-ENOMEM
);
2610 pdata
->dma_slave
= devm_kzalloc(&pdev
->dev
,
2611 sizeof(*(pdata
->dma_slave
)),
2613 if (!pdata
->dma_slave
) {
2614 dev_err(&pdev
->dev
, "could not allocate memory for dma_slave\n");
2615 devm_kfree(&pdev
->dev
, pdata
);
2616 return ERR_PTR(-ENOMEM
);
2622 static inline struct crypto_platform_data
*atmel_aes_of_init(struct platform_device
*pdev
)
2624 return ERR_PTR(-EINVAL
);
2628 static int atmel_aes_probe(struct platform_device
*pdev
)
2630 struct atmel_aes_dev
*aes_dd
;
2631 struct crypto_platform_data
*pdata
;
2632 struct device
*dev
= &pdev
->dev
;
2633 struct resource
*aes_res
;
2636 pdata
= pdev
->dev
.platform_data
;
2638 pdata
= atmel_aes_of_init(pdev
);
2639 if (IS_ERR(pdata
)) {
2640 err
= PTR_ERR(pdata
);
2645 if (!pdata
->dma_slave
) {
2650 aes_dd
= devm_kzalloc(&pdev
->dev
, sizeof(*aes_dd
), GFP_KERNEL
);
2651 if (aes_dd
== NULL
) {
2652 dev_err(dev
, "unable to alloc data struct.\n");
2659 platform_set_drvdata(pdev
, aes_dd
);
2661 INIT_LIST_HEAD(&aes_dd
->list
);
2662 spin_lock_init(&aes_dd
->lock
);
2664 tasklet_init(&aes_dd
->done_task
, atmel_aes_done_task
,
2665 (unsigned long)aes_dd
);
2666 tasklet_init(&aes_dd
->queue_task
, atmel_aes_queue_task
,
2667 (unsigned long)aes_dd
);
2669 crypto_init_queue(&aes_dd
->queue
, ATMEL_AES_QUEUE_LENGTH
);
2671 /* Get the base address */
2672 aes_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2674 dev_err(dev
, "no MEM resource info\n");
2678 aes_dd
->phys_base
= aes_res
->start
;
2681 aes_dd
->irq
= platform_get_irq(pdev
, 0);
2682 if (aes_dd
->irq
< 0) {
2683 dev_err(dev
, "no IRQ resource info\n");
2688 err
= devm_request_irq(&pdev
->dev
, aes_dd
->irq
, atmel_aes_irq
,
2689 IRQF_SHARED
, "atmel-aes", aes_dd
);
2691 dev_err(dev
, "unable to request aes irq.\n");
2695 /* Initializing the clock */
2696 aes_dd
->iclk
= devm_clk_get(&pdev
->dev
, "aes_clk");
2697 if (IS_ERR(aes_dd
->iclk
)) {
2698 dev_err(dev
, "clock initialization failed.\n");
2699 err
= PTR_ERR(aes_dd
->iclk
);
2703 aes_dd
->io_base
= devm_ioremap_resource(&pdev
->dev
, aes_res
);
2704 if (IS_ERR(aes_dd
->io_base
)) {
2705 dev_err(dev
, "can't ioremap\n");
2706 err
= PTR_ERR(aes_dd
->io_base
);
2710 err
= clk_prepare(aes_dd
->iclk
);
2714 err
= atmel_aes_hw_version_init(aes_dd
);
2716 goto iclk_unprepare
;
2718 atmel_aes_get_cap(aes_dd
);
2720 #ifdef CONFIG_CRYPTO_DEV_ATMEL_AUTHENC
2721 if (aes_dd
->caps
.has_authenc
&& !atmel_sha_authenc_is_ready()) {
2722 err
= -EPROBE_DEFER
;
2723 goto iclk_unprepare
;
2727 err
= atmel_aes_buff_init(aes_dd
);
2731 err
= atmel_aes_dma_init(aes_dd
, pdata
);
2735 spin_lock(&atmel_aes
.lock
);
2736 list_add_tail(&aes_dd
->list
, &atmel_aes
.dev_list
);
2737 spin_unlock(&atmel_aes
.lock
);
2739 err
= atmel_aes_register_algs(aes_dd
);
2743 dev_info(dev
, "Atmel AES - Using %s, %s for DMA transfers\n",
2744 dma_chan_name(aes_dd
->src
.chan
),
2745 dma_chan_name(aes_dd
->dst
.chan
));
2750 spin_lock(&atmel_aes
.lock
);
2751 list_del(&aes_dd
->list
);
2752 spin_unlock(&atmel_aes
.lock
);
2753 atmel_aes_dma_cleanup(aes_dd
);
2755 atmel_aes_buff_cleanup(aes_dd
);
2758 clk_unprepare(aes_dd
->iclk
);
2760 tasklet_kill(&aes_dd
->done_task
);
2761 tasklet_kill(&aes_dd
->queue_task
);
2763 if (err
!= -EPROBE_DEFER
)
2764 dev_err(dev
, "initialization failed.\n");
2769 static int atmel_aes_remove(struct platform_device
*pdev
)
2771 struct atmel_aes_dev
*aes_dd
;
2773 aes_dd
= platform_get_drvdata(pdev
);
2776 spin_lock(&atmel_aes
.lock
);
2777 list_del(&aes_dd
->list
);
2778 spin_unlock(&atmel_aes
.lock
);
2780 atmel_aes_unregister_algs(aes_dd
);
2782 tasklet_kill(&aes_dd
->done_task
);
2783 tasklet_kill(&aes_dd
->queue_task
);
2785 atmel_aes_dma_cleanup(aes_dd
);
2786 atmel_aes_buff_cleanup(aes_dd
);
2788 clk_unprepare(aes_dd
->iclk
);
2793 static struct platform_driver atmel_aes_driver
= {
2794 .probe
= atmel_aes_probe
,
2795 .remove
= atmel_aes_remove
,
2797 .name
= "atmel_aes",
2798 .of_match_table
= of_match_ptr(atmel_aes_dt_ids
),
2802 module_platform_driver(atmel_aes_driver
);
2804 MODULE_DESCRIPTION("Atmel AES hw acceleration support.");
2805 MODULE_LICENSE("GPL v2");
2806 MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");