2 * Driver for ARTPEC-6 crypto block using the kernel asynchronous crypto api.
4 * Copyright (C) 2014-2017 Axis Communications AB
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/bitfield.h>
9 #include <linux/crypto.h>
10 #include <linux/debugfs.h>
11 #include <linux/delay.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/fault-inject.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/scatterlist.h>
22 #include <linux/slab.h>
24 #include <crypto/aes.h>
25 #include <crypto/gcm.h>
26 #include <crypto/internal/aead.h>
27 #include <crypto/internal/hash.h>
28 #include <crypto/internal/skcipher.h>
29 #include <crypto/scatterwalk.h>
30 #include <crypto/sha.h>
31 #include <crypto/xts.h>
33 /* Max length of a line in all cache levels for Artpec SoCs. */
34 #define ARTPEC_CACHE_LINE_MAX 32
36 #define PDMA_OUT_CFG 0x0000
37 #define PDMA_OUT_BUF_CFG 0x0004
38 #define PDMA_OUT_CMD 0x0008
39 #define PDMA_OUT_DESCRQ_PUSH 0x0010
40 #define PDMA_OUT_DESCRQ_STAT 0x0014
42 #define A6_PDMA_IN_CFG 0x0028
43 #define A6_PDMA_IN_BUF_CFG 0x002c
44 #define A6_PDMA_IN_CMD 0x0030
45 #define A6_PDMA_IN_STATQ_PUSH 0x0038
46 #define A6_PDMA_IN_DESCRQ_PUSH 0x0044
47 #define A6_PDMA_IN_DESCRQ_STAT 0x0048
48 #define A6_PDMA_INTR_MASK 0x0068
49 #define A6_PDMA_ACK_INTR 0x006c
50 #define A6_PDMA_MASKED_INTR 0x0074
52 #define A7_PDMA_IN_CFG 0x002c
53 #define A7_PDMA_IN_BUF_CFG 0x0030
54 #define A7_PDMA_IN_CMD 0x0034
55 #define A7_PDMA_IN_STATQ_PUSH 0x003c
56 #define A7_PDMA_IN_DESCRQ_PUSH 0x0048
57 #define A7_PDMA_IN_DESCRQ_STAT 0x004C
58 #define A7_PDMA_INTR_MASK 0x006c
59 #define A7_PDMA_ACK_INTR 0x0070
60 #define A7_PDMA_MASKED_INTR 0x0078
62 #define PDMA_OUT_CFG_EN BIT(0)
64 #define PDMA_OUT_BUF_CFG_DATA_BUF_SIZE GENMASK(4, 0)
65 #define PDMA_OUT_BUF_CFG_DESCR_BUF_SIZE GENMASK(9, 5)
67 #define PDMA_OUT_CMD_START BIT(0)
68 #define A6_PDMA_OUT_CMD_STOP BIT(3)
69 #define A7_PDMA_OUT_CMD_STOP BIT(2)
71 #define PDMA_OUT_DESCRQ_PUSH_LEN GENMASK(5, 0)
72 #define PDMA_OUT_DESCRQ_PUSH_ADDR GENMASK(31, 6)
74 #define PDMA_OUT_DESCRQ_STAT_LEVEL GENMASK(3, 0)
75 #define PDMA_OUT_DESCRQ_STAT_SIZE GENMASK(7, 4)
77 #define PDMA_IN_CFG_EN BIT(0)
79 #define PDMA_IN_BUF_CFG_DATA_BUF_SIZE GENMASK(4, 0)
80 #define PDMA_IN_BUF_CFG_DESCR_BUF_SIZE GENMASK(9, 5)
81 #define PDMA_IN_BUF_CFG_STAT_BUF_SIZE GENMASK(14, 10)
83 #define PDMA_IN_CMD_START BIT(0)
84 #define A6_PDMA_IN_CMD_FLUSH_STAT BIT(2)
85 #define A6_PDMA_IN_CMD_STOP BIT(3)
86 #define A7_PDMA_IN_CMD_FLUSH_STAT BIT(1)
87 #define A7_PDMA_IN_CMD_STOP BIT(2)
89 #define PDMA_IN_STATQ_PUSH_LEN GENMASK(5, 0)
90 #define PDMA_IN_STATQ_PUSH_ADDR GENMASK(31, 6)
92 #define PDMA_IN_DESCRQ_PUSH_LEN GENMASK(5, 0)
93 #define PDMA_IN_DESCRQ_PUSH_ADDR GENMASK(31, 6)
95 #define PDMA_IN_DESCRQ_STAT_LEVEL GENMASK(3, 0)
96 #define PDMA_IN_DESCRQ_STAT_SIZE GENMASK(7, 4)
98 #define A6_PDMA_INTR_MASK_IN_DATA BIT(2)
99 #define A6_PDMA_INTR_MASK_IN_EOP BIT(3)
100 #define A6_PDMA_INTR_MASK_IN_EOP_FLUSH BIT(4)
102 #define A7_PDMA_INTR_MASK_IN_DATA BIT(3)
103 #define A7_PDMA_INTR_MASK_IN_EOP BIT(4)
104 #define A7_PDMA_INTR_MASK_IN_EOP_FLUSH BIT(5)
106 #define A6_CRY_MD_OPER GENMASK(19, 16)
108 #define A6_CRY_MD_HASH_SEL_CTX GENMASK(21, 20)
109 #define A6_CRY_MD_HASH_HMAC_FIN BIT(23)
111 #define A6_CRY_MD_CIPHER_LEN GENMASK(21, 20)
112 #define A6_CRY_MD_CIPHER_DECR BIT(22)
113 #define A6_CRY_MD_CIPHER_TWEAK BIT(23)
114 #define A6_CRY_MD_CIPHER_DSEQ BIT(24)
116 #define A7_CRY_MD_OPER GENMASK(11, 8)
118 #define A7_CRY_MD_HASH_SEL_CTX GENMASK(13, 12)
119 #define A7_CRY_MD_HASH_HMAC_FIN BIT(15)
121 #define A7_CRY_MD_CIPHER_LEN GENMASK(13, 12)
122 #define A7_CRY_MD_CIPHER_DECR BIT(14)
123 #define A7_CRY_MD_CIPHER_TWEAK BIT(15)
124 #define A7_CRY_MD_CIPHER_DSEQ BIT(16)
126 /* DMA metadata constants */
127 #define regk_crypto_aes_cbc 0x00000002
128 #define regk_crypto_aes_ctr 0x00000003
129 #define regk_crypto_aes_ecb 0x00000001
130 #define regk_crypto_aes_gcm 0x00000004
131 #define regk_crypto_aes_xts 0x00000005
132 #define regk_crypto_cache 0x00000002
133 #define a6_regk_crypto_dlkey 0x0000000a
134 #define a7_regk_crypto_dlkey 0x0000000e
135 #define regk_crypto_ext 0x00000001
136 #define regk_crypto_hmac_sha1 0x00000007
137 #define regk_crypto_hmac_sha256 0x00000009
138 #define regk_crypto_hmac_sha384 0x0000000b
139 #define regk_crypto_hmac_sha512 0x0000000d
140 #define regk_crypto_init 0x00000000
141 #define regk_crypto_key_128 0x00000000
142 #define regk_crypto_key_192 0x00000001
143 #define regk_crypto_key_256 0x00000002
144 #define regk_crypto_null 0x00000000
145 #define regk_crypto_sha1 0x00000006
146 #define regk_crypto_sha256 0x00000008
147 #define regk_crypto_sha384 0x0000000a
148 #define regk_crypto_sha512 0x0000000c
150 /* DMA descriptor structures */
151 struct pdma_descr_ctrl {
152 unsigned char short_descr : 1;
153 unsigned char pad1 : 1;
154 unsigned char eop : 1;
155 unsigned char intr : 1;
156 unsigned char short_len : 3;
157 unsigned char pad2 : 1;
160 struct pdma_data_descr {
161 unsigned int len : 24;
162 unsigned int buf : 32;
165 struct pdma_short_descr {
166 unsigned char data[7];
170 struct pdma_descr_ctrl ctrl;
172 struct pdma_data_descr data;
173 struct pdma_short_descr shrt;
177 struct pdma_stat_descr {
178 unsigned char pad1 : 1;
179 unsigned char pad2 : 1;
180 unsigned char eop : 1;
181 unsigned char pad3 : 5;
182 unsigned int len : 24;
185 /* Each descriptor array can hold max 64 entries */
186 #define PDMA_DESCR_COUNT 64
188 #define MODULE_NAME "Artpec-6 CA"
190 /* Hash modes (including HMAC variants) */
191 #define ARTPEC6_CRYPTO_HASH_SHA1 1
192 #define ARTPEC6_CRYPTO_HASH_SHA256 2
193 #define ARTPEC6_CRYPTO_HASH_SHA384 3
194 #define ARTPEC6_CRYPTO_HASH_SHA512 4
197 #define ARTPEC6_CRYPTO_CIPHER_AES_ECB 1
198 #define ARTPEC6_CRYPTO_CIPHER_AES_CBC 2
199 #define ARTPEC6_CRYPTO_CIPHER_AES_CTR 3
200 #define ARTPEC6_CRYPTO_CIPHER_AES_XTS 5
202 /* The PDMA is a DMA-engine tightly coupled with a ciphering engine.
203 * It operates on a descriptor array with up to 64 descriptor entries.
204 * The arrays must be 64 byte aligned in memory.
206 * The ciphering unit has no registers and is completely controlled by
207 * a 4-byte metadata that is inserted at the beginning of each dma packet.
209 * A dma packet is a sequence of descriptors terminated by setting the .eop
210 * field in the final descriptor of the packet.
212 * Multiple packets are used for providing context data, key data and
213 * the plain/ciphertext.
215 * PDMA Descriptors (Array)
216 * +------+------+------+~~+-------+------+----
217 * | 0 | 1 | 2 |~~| 11 EOP| 12 | ....
218 * +--+---+--+---+----+-+~~+-------+----+-+----
221 * __|__ +-------++-------++-------+ +----+
222 * | MD | |Payload||Payload||Payload| | MD |
223 * +-----+ +-------++-------++-------+ +----+
226 struct artpec6_crypto_bounce_buffer {
227 struct list_head list;
229 struct scatterlist *sg;
231 /* buf is aligned to ARTPEC_CACHE_LINE_MAX and
232 * holds up to ARTPEC_CACHE_LINE_MAX bytes data.
237 struct artpec6_crypto_dma_map {
240 enum dma_data_direction dir;
243 struct artpec6_crypto_dma_descriptors {
244 struct pdma_descr out[PDMA_DESCR_COUNT] __aligned(64);
245 struct pdma_descr in[PDMA_DESCR_COUNT] __aligned(64);
246 u32 stat[PDMA_DESCR_COUNT] __aligned(64);
247 struct list_head bounce_buffers;
248 /* Enough maps for all out/in buffers, and all three descr. arrays */
249 struct artpec6_crypto_dma_map maps[PDMA_DESCR_COUNT * 2 + 2];
250 dma_addr_t out_dma_addr;
251 dma_addr_t in_dma_addr;
252 dma_addr_t stat_dma_addr;
258 enum artpec6_crypto_variant {
263 struct artpec6_crypto {
265 spinlock_t queue_lock;
266 struct list_head queue; /* waiting for pdma fifo space */
267 struct list_head pending; /* submitted to pdma fifo */
268 struct tasklet_struct task;
269 struct kmem_cache *dma_cache;
271 struct timer_list timer;
272 enum artpec6_crypto_variant variant;
273 void *pad_buffer; /* cache-aligned block padding buffer */
277 enum artpec6_crypto_hash_flags {
278 HASH_FLAG_INIT_CTX = 2,
279 HASH_FLAG_UPDATE = 4,
280 HASH_FLAG_FINALIZE = 8,
282 HASH_FLAG_UPDATE_KEY = 32,
285 struct artpec6_crypto_req_common {
286 struct list_head list;
287 struct list_head complete_in_progress;
288 struct artpec6_crypto_dma_descriptors *dma;
289 struct crypto_async_request *req;
290 void (*complete)(struct crypto_async_request *req);
294 struct artpec6_hash_request_context {
295 char partial_buffer[SHA512_BLOCK_SIZE];
296 char partial_buffer_out[SHA512_BLOCK_SIZE];
297 char key_buffer[SHA512_BLOCK_SIZE];
298 char pad_buffer[SHA512_BLOCK_SIZE + 32];
299 unsigned char digeststate[SHA512_DIGEST_SIZE];
300 size_t partial_bytes;
304 enum artpec6_crypto_hash_flags hash_flags;
305 struct artpec6_crypto_req_common common;
308 struct artpec6_hash_export_state {
309 char partial_buffer[SHA512_BLOCK_SIZE];
310 unsigned char digeststate[SHA512_DIGEST_SIZE];
311 size_t partial_bytes;
314 unsigned int hash_flags;
317 struct artpec6_hashalg_context {
318 char hmac_key[SHA512_BLOCK_SIZE];
319 size_t hmac_key_length;
320 struct crypto_shash *child_hash;
323 struct artpec6_crypto_request_context {
326 struct artpec6_crypto_req_common common;
329 struct artpec6_cryptotfm_context {
330 unsigned char aes_key[2*AES_MAX_KEY_SIZE];
334 struct crypto_skcipher *fallback;
337 struct artpec6_crypto_aead_hw_ctx {
338 __be64 aad_length_bits;
339 __be64 text_length_bits;
340 __u8 J0[AES_BLOCK_SIZE];
343 struct artpec6_crypto_aead_req_ctx {
344 struct artpec6_crypto_aead_hw_ctx hw_ctx;
347 struct artpec6_crypto_req_common common;
348 __u8 decryption_tag[AES_BLOCK_SIZE] ____cacheline_aligned;
351 /* The crypto framework makes it hard to avoid this global. */
352 static struct device *artpec6_crypto_dev;
354 #ifdef CONFIG_FAULT_INJECTION
355 static DECLARE_FAULT_ATTR(artpec6_crypto_fail_status_read);
356 static DECLARE_FAULT_ATTR(artpec6_crypto_fail_dma_array_full);
360 ARTPEC6_CRYPTO_PREPARE_HASH_NO_START,
361 ARTPEC6_CRYPTO_PREPARE_HASH_START,
364 static int artpec6_crypto_prepare_aead(struct aead_request *areq);
365 static int artpec6_crypto_prepare_crypto(struct skcipher_request *areq);
366 static int artpec6_crypto_prepare_hash(struct ahash_request *areq);
369 artpec6_crypto_complete_crypto(struct crypto_async_request *req);
371 artpec6_crypto_complete_cbc_encrypt(struct crypto_async_request *req);
373 artpec6_crypto_complete_cbc_decrypt(struct crypto_async_request *req);
375 artpec6_crypto_complete_aead(struct crypto_async_request *req);
377 artpec6_crypto_complete_hash(struct crypto_async_request *req);
380 artpec6_crypto_common_destroy(struct artpec6_crypto_req_common *common);
383 artpec6_crypto_start_dma(struct artpec6_crypto_req_common *common);
385 struct artpec6_crypto_walk {
386 struct scatterlist *sg;
390 static void artpec6_crypto_walk_init(struct artpec6_crypto_walk *awalk,
391 struct scatterlist *sg)
397 static size_t artpec6_crypto_walk_advance(struct artpec6_crypto_walk *awalk,
400 while (nbytes && awalk->sg) {
403 WARN_ON(awalk->offset > awalk->sg->length);
405 piece = min(nbytes, (size_t)awalk->sg->length - awalk->offset);
407 awalk->offset += piece;
408 if (awalk->offset == awalk->sg->length) {
409 awalk->sg = sg_next(awalk->sg);
419 artpec6_crypto_walk_chunklen(const struct artpec6_crypto_walk *awalk)
421 WARN_ON(awalk->sg->length == awalk->offset);
423 return awalk->sg->length - awalk->offset;
427 artpec6_crypto_walk_chunk_phys(const struct artpec6_crypto_walk *awalk)
429 return sg_phys(awalk->sg) + awalk->offset;
433 artpec6_crypto_copy_bounce_buffers(struct artpec6_crypto_req_common *common)
435 struct artpec6_crypto_dma_descriptors *dma = common->dma;
436 struct artpec6_crypto_bounce_buffer *b;
437 struct artpec6_crypto_bounce_buffer *next;
439 list_for_each_entry_safe(b, next, &dma->bounce_buffers, list) {
440 pr_debug("bounce entry %p: %zu bytes @ %zu from %p\n",
441 b, b->length, b->offset, b->buf);
442 sg_pcopy_from_buffer(b->sg,
453 static inline bool artpec6_crypto_busy(void)
455 struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
456 int fifo_count = ac->pending_count;
458 return fifo_count > 6;
461 static int artpec6_crypto_submit(struct artpec6_crypto_req_common *req)
463 struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
466 spin_lock_bh(&ac->queue_lock);
468 if (!artpec6_crypto_busy()) {
469 list_add_tail(&req->list, &ac->pending);
470 artpec6_crypto_start_dma(req);
472 } else if (req->req->flags & CRYPTO_TFM_REQ_MAY_BACKLOG) {
473 list_add_tail(&req->list, &ac->queue);
475 artpec6_crypto_common_destroy(req);
478 spin_unlock_bh(&ac->queue_lock);
483 static void artpec6_crypto_start_dma(struct artpec6_crypto_req_common *common)
485 struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
486 enum artpec6_crypto_variant variant = ac->variant;
487 void __iomem *base = ac->base;
488 struct artpec6_crypto_dma_descriptors *dma = common->dma;
489 u32 ind, statd, outd;
491 /* Make descriptor content visible to the DMA before starting it. */
494 ind = FIELD_PREP(PDMA_IN_DESCRQ_PUSH_LEN, dma->in_cnt - 1) |
495 FIELD_PREP(PDMA_IN_DESCRQ_PUSH_ADDR, dma->in_dma_addr >> 6);
497 statd = FIELD_PREP(PDMA_IN_STATQ_PUSH_LEN, dma->in_cnt - 1) |
498 FIELD_PREP(PDMA_IN_STATQ_PUSH_ADDR, dma->stat_dma_addr >> 6);
500 outd = FIELD_PREP(PDMA_OUT_DESCRQ_PUSH_LEN, dma->out_cnt - 1) |
501 FIELD_PREP(PDMA_OUT_DESCRQ_PUSH_ADDR, dma->out_dma_addr >> 6);
503 if (variant == ARTPEC6_CRYPTO) {
504 writel_relaxed(ind, base + A6_PDMA_IN_DESCRQ_PUSH);
505 writel_relaxed(statd, base + A6_PDMA_IN_STATQ_PUSH);
506 writel_relaxed(PDMA_IN_CMD_START, base + A6_PDMA_IN_CMD);
508 writel_relaxed(ind, base + A7_PDMA_IN_DESCRQ_PUSH);
509 writel_relaxed(statd, base + A7_PDMA_IN_STATQ_PUSH);
510 writel_relaxed(PDMA_IN_CMD_START, base + A7_PDMA_IN_CMD);
513 writel_relaxed(outd, base + PDMA_OUT_DESCRQ_PUSH);
514 writel_relaxed(PDMA_OUT_CMD_START, base + PDMA_OUT_CMD);
520 artpec6_crypto_init_dma_operation(struct artpec6_crypto_req_common *common)
522 struct artpec6_crypto_dma_descriptors *dma = common->dma;
527 INIT_LIST_HEAD(&dma->bounce_buffers);
530 static bool fault_inject_dma_descr(void)
532 #ifdef CONFIG_FAULT_INJECTION
533 return should_fail(&artpec6_crypto_fail_dma_array_full, 1);
539 /** artpec6_crypto_setup_out_descr_phys - Setup an out channel with a
542 * @addr: The physical address of the data buffer
543 * @len: The length of the data buffer
544 * @eop: True if this is the last buffer in the packet
546 * @return 0 on success or -ENOSPC if there are no more descriptors available
549 artpec6_crypto_setup_out_descr_phys(struct artpec6_crypto_req_common *common,
550 dma_addr_t addr, size_t len, bool eop)
552 struct artpec6_crypto_dma_descriptors *dma = common->dma;
553 struct pdma_descr *d;
555 if (dma->out_cnt >= PDMA_DESCR_COUNT ||
556 fault_inject_dma_descr()) {
557 pr_err("No free OUT DMA descriptors available!\n");
561 d = &dma->out[dma->out_cnt++];
562 memset(d, 0, sizeof(*d));
564 d->ctrl.short_descr = 0;
571 /** artpec6_crypto_setup_out_descr_short - Setup a short out descriptor
573 * @dst: The virtual address of the data
574 * @len: The length of the data, must be between 1 to 7 bytes
575 * @eop: True if this is the last buffer in the packet
577 * @return 0 on success
578 * -ENOSPC if no more descriptors are available
579 * -EINVAL if the data length exceeds 7 bytes
582 artpec6_crypto_setup_out_descr_short(struct artpec6_crypto_req_common *common,
583 void *dst, unsigned int len, bool eop)
585 struct artpec6_crypto_dma_descriptors *dma = common->dma;
586 struct pdma_descr *d;
588 if (dma->out_cnt >= PDMA_DESCR_COUNT ||
589 fault_inject_dma_descr()) {
590 pr_err("No free OUT DMA descriptors available!\n");
592 } else if (len > 7 || len < 1) {
595 d = &dma->out[dma->out_cnt++];
596 memset(d, 0, sizeof(*d));
598 d->ctrl.short_descr = 1;
599 d->ctrl.short_len = len;
601 memcpy(d->shrt.data, dst, len);
605 static int artpec6_crypto_dma_map_page(struct artpec6_crypto_req_common *common,
606 struct page *page, size_t offset,
608 enum dma_data_direction dir,
609 dma_addr_t *dma_addr_out)
611 struct artpec6_crypto_dma_descriptors *dma = common->dma;
612 struct device *dev = artpec6_crypto_dev;
613 struct artpec6_crypto_dma_map *map;
618 if (dma->map_count >= ARRAY_SIZE(dma->maps))
621 dma_addr = dma_map_page(dev, page, offset, size, dir);
622 if (dma_mapping_error(dev, dma_addr))
625 map = &dma->maps[dma->map_count++];
627 map->dma_addr = dma_addr;
630 *dma_addr_out = dma_addr;
636 artpec6_crypto_dma_map_single(struct artpec6_crypto_req_common *common,
637 void *ptr, size_t size,
638 enum dma_data_direction dir,
639 dma_addr_t *dma_addr_out)
641 struct page *page = virt_to_page(ptr);
642 size_t offset = (uintptr_t)ptr & ~PAGE_MASK;
644 return artpec6_crypto_dma_map_page(common, page, offset, size, dir,
649 artpec6_crypto_dma_map_descs(struct artpec6_crypto_req_common *common)
651 struct artpec6_crypto_dma_descriptors *dma = common->dma;
654 ret = artpec6_crypto_dma_map_single(common, dma->in,
655 sizeof(dma->in[0]) * dma->in_cnt,
656 DMA_TO_DEVICE, &dma->in_dma_addr);
660 ret = artpec6_crypto_dma_map_single(common, dma->out,
661 sizeof(dma->out[0]) * dma->out_cnt,
662 DMA_TO_DEVICE, &dma->out_dma_addr);
666 /* We only read one stat descriptor */
667 dma->stat[dma->in_cnt - 1] = 0;
670 * DMA_BIDIRECTIONAL since we need our zeroing of the stat descriptor
673 return artpec6_crypto_dma_map_single(common,
674 dma->stat + dma->in_cnt - 1,
675 sizeof(dma->stat[0]),
677 &dma->stat_dma_addr);
681 artpec6_crypto_dma_unmap_all(struct artpec6_crypto_req_common *common)
683 struct artpec6_crypto_dma_descriptors *dma = common->dma;
684 struct device *dev = artpec6_crypto_dev;
687 for (i = 0; i < dma->map_count; i++) {
688 struct artpec6_crypto_dma_map *map = &dma->maps[i];
690 dma_unmap_page(dev, map->dma_addr, map->size, map->dir);
696 /** artpec6_crypto_setup_out_descr - Setup an out descriptor
698 * @dst: The virtual address of the data
699 * @len: The length of the data
700 * @eop: True if this is the last buffer in the packet
701 * @use_short: If this is true and the data length is 7 bytes or less then
702 * a short descriptor will be used
704 * @return 0 on success
705 * Any errors from artpec6_crypto_setup_out_descr_short() or
706 * setup_out_descr_phys()
709 artpec6_crypto_setup_out_descr(struct artpec6_crypto_req_common *common,
710 void *dst, unsigned int len, bool eop,
713 if (use_short && len < 7) {
714 return artpec6_crypto_setup_out_descr_short(common, dst, len,
720 ret = artpec6_crypto_dma_map_single(common, dst, len,
726 return artpec6_crypto_setup_out_descr_phys(common, dma_addr,
731 /** artpec6_crypto_setup_in_descr_phys - Setup an in channel with a
734 * @addr: The physical address of the data buffer
735 * @len: The length of the data buffer
736 * @intr: True if an interrupt should be fired after HW processing of this
741 artpec6_crypto_setup_in_descr_phys(struct artpec6_crypto_req_common *common,
742 dma_addr_t addr, unsigned int len, bool intr)
744 struct artpec6_crypto_dma_descriptors *dma = common->dma;
745 struct pdma_descr *d;
747 if (dma->in_cnt >= PDMA_DESCR_COUNT ||
748 fault_inject_dma_descr()) {
749 pr_err("No free IN DMA descriptors available!\n");
752 d = &dma->in[dma->in_cnt++];
753 memset(d, 0, sizeof(*d));
761 /** artpec6_crypto_setup_in_descr - Setup an in channel descriptor
763 * @buffer: The virtual address to of the data buffer
764 * @len: The length of the data buffer
765 * @last: If this is the last data buffer in the request (i.e. an interrupt
768 * Short descriptors are not used for the in channel
771 artpec6_crypto_setup_in_descr(struct artpec6_crypto_req_common *common,
772 void *buffer, unsigned int len, bool last)
777 ret = artpec6_crypto_dma_map_single(common, buffer, len,
778 DMA_FROM_DEVICE, &dma_addr);
782 return artpec6_crypto_setup_in_descr_phys(common, dma_addr, len, last);
785 static struct artpec6_crypto_bounce_buffer *
786 artpec6_crypto_alloc_bounce(gfp_t flags)
789 size_t alloc_size = sizeof(struct artpec6_crypto_bounce_buffer) +
790 2 * ARTPEC_CACHE_LINE_MAX;
791 struct artpec6_crypto_bounce_buffer *bbuf = kzalloc(alloc_size, flags);
797 bbuf->buf = PTR_ALIGN(base, ARTPEC_CACHE_LINE_MAX);
801 static int setup_bounce_buffer_in(struct artpec6_crypto_req_common *common,
802 struct artpec6_crypto_walk *walk, size_t size)
804 struct artpec6_crypto_bounce_buffer *bbuf;
807 bbuf = artpec6_crypto_alloc_bounce(common->gfp_flags);
813 bbuf->offset = walk->offset;
815 ret = artpec6_crypto_setup_in_descr(common, bbuf->buf, size, false);
821 pr_debug("BOUNCE %zu offset %zu\n", size, walk->offset);
822 list_add_tail(&bbuf->list, &common->dma->bounce_buffers);
827 artpec6_crypto_setup_sg_descrs_in(struct artpec6_crypto_req_common *common,
828 struct artpec6_crypto_walk *walk,
835 while (walk->sg && count) {
836 chunk = min(count, artpec6_crypto_walk_chunklen(walk));
837 addr = artpec6_crypto_walk_chunk_phys(walk);
839 /* When destination buffers are not aligned to the cache line
840 * size we need bounce buffers. The DMA-API requires that the
841 * entire line is owned by the DMA buffer and this holds also
842 * for the case when coherent DMA is used.
844 if (!IS_ALIGNED(addr, ARTPEC_CACHE_LINE_MAX)) {
845 chunk = min_t(dma_addr_t, chunk,
846 ALIGN(addr, ARTPEC_CACHE_LINE_MAX) -
849 pr_debug("CHUNK-b %pad:%zu\n", &addr, chunk);
850 ret = setup_bounce_buffer_in(common, walk, chunk);
851 } else if (chunk < ARTPEC_CACHE_LINE_MAX) {
852 pr_debug("CHUNK-b %pad:%zu\n", &addr, chunk);
853 ret = setup_bounce_buffer_in(common, walk, chunk);
857 chunk = chunk & ~(ARTPEC_CACHE_LINE_MAX-1);
859 pr_debug("CHUNK %pad:%zu\n", &addr, chunk);
861 ret = artpec6_crypto_dma_map_page(common,
871 ret = artpec6_crypto_setup_in_descr_phys(common,
879 count = count - chunk;
880 artpec6_crypto_walk_advance(walk, chunk);
884 pr_err("EOL unexpected %zu bytes left\n", count);
886 return count ? -EINVAL : 0;
890 artpec6_crypto_setup_sg_descrs_out(struct artpec6_crypto_req_common *common,
891 struct artpec6_crypto_walk *walk,
898 while (walk->sg && count) {
899 chunk = min(count, artpec6_crypto_walk_chunklen(walk));
900 addr = artpec6_crypto_walk_chunk_phys(walk);
902 pr_debug("OUT-CHUNK %pad:%zu\n", &addr, chunk);
907 chunk = min_t(size_t, chunk, (4-(addr&3)));
909 sg_pcopy_to_buffer(walk->sg, 1, buf, chunk,
912 ret = artpec6_crypto_setup_out_descr_short(common, buf,
918 ret = artpec6_crypto_dma_map_page(common,
928 ret = artpec6_crypto_setup_out_descr_phys(common,
936 count = count - chunk;
937 artpec6_crypto_walk_advance(walk, chunk);
941 pr_err("EOL unexpected %zu bytes left\n", count);
943 return count ? -EINVAL : 0;
947 /** artpec6_crypto_terminate_out_descrs - Set the EOP on the last out descriptor
949 * If the out descriptor list is non-empty, then the eop flag on the
950 * last used out descriptor will be set.
952 * @return 0 on success
953 * -EINVAL if the out descriptor is empty or has overflown
956 artpec6_crypto_terminate_out_descrs(struct artpec6_crypto_req_common *common)
958 struct artpec6_crypto_dma_descriptors *dma = common->dma;
959 struct pdma_descr *d;
961 if (!dma->out_cnt || dma->out_cnt > PDMA_DESCR_COUNT) {
962 pr_err("%s: OUT descriptor list is %s\n",
963 MODULE_NAME, dma->out_cnt ? "empty" : "full");
968 d = &dma->out[dma->out_cnt-1];
974 /** artpec6_crypto_terminate_in_descrs - Set the interrupt flag on the last
977 * See artpec6_crypto_terminate_out_descrs() for return values
980 artpec6_crypto_terminate_in_descrs(struct artpec6_crypto_req_common *common)
982 struct artpec6_crypto_dma_descriptors *dma = common->dma;
983 struct pdma_descr *d;
985 if (!dma->in_cnt || dma->in_cnt > PDMA_DESCR_COUNT) {
986 pr_err("%s: IN descriptor list is %s\n",
987 MODULE_NAME, dma->in_cnt ? "empty" : "full");
991 d = &dma->in[dma->in_cnt-1];
996 /** create_hash_pad - Create a Secure Hash conformant pad
998 * @dst: The destination buffer to write the pad. Must be at least 64 bytes
999 * @dgstlen: The total length of the hash digest in bytes
1000 * @bitcount: The total length of the digest in bits
1002 * @return The total number of padding bytes written to @dst
1005 create_hash_pad(int oper, unsigned char *dst, u64 dgstlen, u64 bitcount)
1007 unsigned int mod, target, diff, pad_bytes, size_bytes;
1008 __be64 bits = __cpu_to_be64(bitcount);
1011 case regk_crypto_sha1:
1012 case regk_crypto_sha256:
1013 case regk_crypto_hmac_sha1:
1014 case regk_crypto_hmac_sha256:
1027 diff = dgstlen & (mod - 1);
1028 pad_bytes = diff > target ? target + mod - diff : target - diff;
1030 memset(dst + 1, 0, pad_bytes);
1033 if (size_bytes == 16) {
1034 memset(dst + 1 + pad_bytes, 0, 8);
1035 memcpy(dst + 1 + pad_bytes + 8, &bits, 8);
1037 memcpy(dst + 1 + pad_bytes, &bits, 8);
1040 return pad_bytes + size_bytes + 1;
1043 static int artpec6_crypto_common_init(struct artpec6_crypto_req_common *common,
1044 struct crypto_async_request *parent,
1045 void (*complete)(struct crypto_async_request *req),
1046 struct scatterlist *dstsg, unsigned int nbytes)
1049 struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
1051 flags = (parent->flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1052 GFP_KERNEL : GFP_ATOMIC;
1054 common->gfp_flags = flags;
1055 common->dma = kmem_cache_alloc(ac->dma_cache, flags);
1059 common->req = parent;
1060 common->complete = complete;
1065 artpec6_crypto_bounce_destroy(struct artpec6_crypto_dma_descriptors *dma)
1067 struct artpec6_crypto_bounce_buffer *b;
1068 struct artpec6_crypto_bounce_buffer *next;
1070 list_for_each_entry_safe(b, next, &dma->bounce_buffers, list) {
1076 artpec6_crypto_common_destroy(struct artpec6_crypto_req_common *common)
1078 struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
1080 artpec6_crypto_dma_unmap_all(common);
1081 artpec6_crypto_bounce_destroy(common->dma);
1082 kmem_cache_free(ac->dma_cache, common->dma);
1088 * Ciphering functions.
1090 static int artpec6_crypto_encrypt(struct skcipher_request *req)
1092 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
1093 struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(cipher);
1094 struct artpec6_crypto_request_context *req_ctx = NULL;
1095 void (*complete)(struct crypto_async_request *req);
1098 req_ctx = skcipher_request_ctx(req);
1100 switch (ctx->crypto_type) {
1101 case ARTPEC6_CRYPTO_CIPHER_AES_CBC:
1102 case ARTPEC6_CRYPTO_CIPHER_AES_ECB:
1103 case ARTPEC6_CRYPTO_CIPHER_AES_XTS:
1104 req_ctx->decrypt = 0;
1110 switch (ctx->crypto_type) {
1111 case ARTPEC6_CRYPTO_CIPHER_AES_CBC:
1112 complete = artpec6_crypto_complete_cbc_encrypt;
1115 complete = artpec6_crypto_complete_crypto;
1119 ret = artpec6_crypto_common_init(&req_ctx->common,
1122 req->dst, req->cryptlen);
1126 ret = artpec6_crypto_prepare_crypto(req);
1128 artpec6_crypto_common_destroy(&req_ctx->common);
1132 return artpec6_crypto_submit(&req_ctx->common);
1135 static int artpec6_crypto_decrypt(struct skcipher_request *req)
1138 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
1139 struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(cipher);
1140 struct artpec6_crypto_request_context *req_ctx = NULL;
1141 void (*complete)(struct crypto_async_request *req);
1143 req_ctx = skcipher_request_ctx(req);
1145 switch (ctx->crypto_type) {
1146 case ARTPEC6_CRYPTO_CIPHER_AES_CBC:
1147 case ARTPEC6_CRYPTO_CIPHER_AES_ECB:
1148 case ARTPEC6_CRYPTO_CIPHER_AES_XTS:
1149 req_ctx->decrypt = 1;
1156 switch (ctx->crypto_type) {
1157 case ARTPEC6_CRYPTO_CIPHER_AES_CBC:
1158 complete = artpec6_crypto_complete_cbc_decrypt;
1161 complete = artpec6_crypto_complete_crypto;
1165 ret = artpec6_crypto_common_init(&req_ctx->common, &req->base,
1167 req->dst, req->cryptlen);
1171 ret = artpec6_crypto_prepare_crypto(req);
1173 artpec6_crypto_common_destroy(&req_ctx->common);
1177 return artpec6_crypto_submit(&req_ctx->common);
1181 artpec6_crypto_ctr_crypt(struct skcipher_request *req, bool encrypt)
1183 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
1184 struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(cipher);
1185 size_t iv_len = crypto_skcipher_ivsize(cipher);
1186 unsigned int counter = be32_to_cpup((__be32 *)
1187 (req->iv + iv_len - 4));
1188 unsigned int nblks = ALIGN(req->cryptlen, AES_BLOCK_SIZE) /
1192 * The hardware uses only the last 32-bits as the counter while the
1193 * kernel tests (aes_ctr_enc_tv_template[4] for example) expect that
1194 * the whole IV is a counter. So fallback if the counter is going to
1197 if (counter + nblks < counter) {
1200 pr_debug("counter %x will overflow (nblks %u), falling back\n",
1201 counter, counter + nblks);
1203 ret = crypto_skcipher_setkey(ctx->fallback, ctx->aes_key,
1209 SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
1211 skcipher_request_set_tfm(subreq, ctx->fallback);
1212 skcipher_request_set_callback(subreq, req->base.flags,
1214 skcipher_request_set_crypt(subreq, req->src, req->dst,
1215 req->cryptlen, req->iv);
1216 ret = encrypt ? crypto_skcipher_encrypt(subreq)
1217 : crypto_skcipher_decrypt(subreq);
1218 skcipher_request_zero(subreq);
1223 return encrypt ? artpec6_crypto_encrypt(req)
1224 : artpec6_crypto_decrypt(req);
1227 static int artpec6_crypto_ctr_encrypt(struct skcipher_request *req)
1229 return artpec6_crypto_ctr_crypt(req, true);
1232 static int artpec6_crypto_ctr_decrypt(struct skcipher_request *req)
1234 return artpec6_crypto_ctr_crypt(req, false);
1240 static int artpec6_crypto_aead_init(struct crypto_aead *tfm)
1242 struct artpec6_cryptotfm_context *tfm_ctx = crypto_aead_ctx(tfm);
1244 memset(tfm_ctx, 0, sizeof(*tfm_ctx));
1246 crypto_aead_set_reqsize(tfm,
1247 sizeof(struct artpec6_crypto_aead_req_ctx));
1252 static int artpec6_crypto_aead_set_key(struct crypto_aead *tfm, const u8 *key,
1255 struct artpec6_cryptotfm_context *ctx = crypto_tfm_ctx(&tfm->base);
1257 if (len != 16 && len != 24 && len != 32) {
1258 crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1262 ctx->key_length = len;
1264 memcpy(ctx->aes_key, key, len);
1268 static int artpec6_crypto_aead_encrypt(struct aead_request *req)
1271 struct artpec6_crypto_aead_req_ctx *req_ctx = aead_request_ctx(req);
1273 req_ctx->decrypt = false;
1274 ret = artpec6_crypto_common_init(&req_ctx->common, &req->base,
1275 artpec6_crypto_complete_aead,
1280 ret = artpec6_crypto_prepare_aead(req);
1282 artpec6_crypto_common_destroy(&req_ctx->common);
1286 return artpec6_crypto_submit(&req_ctx->common);
1289 static int artpec6_crypto_aead_decrypt(struct aead_request *req)
1292 struct artpec6_crypto_aead_req_ctx *req_ctx = aead_request_ctx(req);
1294 req_ctx->decrypt = true;
1295 if (req->cryptlen < AES_BLOCK_SIZE)
1298 ret = artpec6_crypto_common_init(&req_ctx->common,
1300 artpec6_crypto_complete_aead,
1305 ret = artpec6_crypto_prepare_aead(req);
1307 artpec6_crypto_common_destroy(&req_ctx->common);
1311 return artpec6_crypto_submit(&req_ctx->common);
1314 static int artpec6_crypto_prepare_hash(struct ahash_request *areq)
1316 struct artpec6_hashalg_context *ctx = crypto_tfm_ctx(areq->base.tfm);
1317 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(areq);
1318 size_t digestsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(areq));
1319 size_t contextsize = digestsize == SHA384_DIGEST_SIZE ?
1320 SHA512_DIGEST_SIZE : digestsize;
1321 size_t blocksize = crypto_tfm_alg_blocksize(
1322 crypto_ahash_tfm(crypto_ahash_reqtfm(areq)));
1323 struct artpec6_crypto_req_common *common = &req_ctx->common;
1324 struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
1325 enum artpec6_crypto_variant variant = ac->variant;
1327 bool ext_ctx = false;
1328 bool run_hw = false;
1331 artpec6_crypto_init_dma_operation(common);
1333 /* Upload HMAC key, must be first the first packet */
1334 if (req_ctx->hash_flags & HASH_FLAG_HMAC) {
1335 if (variant == ARTPEC6_CRYPTO) {
1336 req_ctx->key_md = FIELD_PREP(A6_CRY_MD_OPER,
1337 a6_regk_crypto_dlkey);
1339 req_ctx->key_md = FIELD_PREP(A7_CRY_MD_OPER,
1340 a7_regk_crypto_dlkey);
1343 /* Copy and pad up the key */
1344 memcpy(req_ctx->key_buffer, ctx->hmac_key,
1345 ctx->hmac_key_length);
1346 memset(req_ctx->key_buffer + ctx->hmac_key_length, 0,
1347 blocksize - ctx->hmac_key_length);
1349 error = artpec6_crypto_setup_out_descr(common,
1350 (void *)&req_ctx->key_md,
1351 sizeof(req_ctx->key_md), false, false);
1355 error = artpec6_crypto_setup_out_descr(common,
1356 req_ctx->key_buffer, blocksize,
1362 if (!(req_ctx->hash_flags & HASH_FLAG_INIT_CTX)) {
1363 /* Restore context */
1364 sel_ctx = regk_crypto_ext;
1367 sel_ctx = regk_crypto_init;
1370 if (variant == ARTPEC6_CRYPTO) {
1371 req_ctx->hash_md &= ~A6_CRY_MD_HASH_SEL_CTX;
1372 req_ctx->hash_md |= FIELD_PREP(A6_CRY_MD_HASH_SEL_CTX, sel_ctx);
1374 /* If this is the final round, set the final flag */
1375 if (req_ctx->hash_flags & HASH_FLAG_FINALIZE)
1376 req_ctx->hash_md |= A6_CRY_MD_HASH_HMAC_FIN;
1378 req_ctx->hash_md &= ~A7_CRY_MD_HASH_SEL_CTX;
1379 req_ctx->hash_md |= FIELD_PREP(A7_CRY_MD_HASH_SEL_CTX, sel_ctx);
1381 /* If this is the final round, set the final flag */
1382 if (req_ctx->hash_flags & HASH_FLAG_FINALIZE)
1383 req_ctx->hash_md |= A7_CRY_MD_HASH_HMAC_FIN;
1386 /* Setup up metadata descriptors */
1387 error = artpec6_crypto_setup_out_descr(common,
1388 (void *)&req_ctx->hash_md,
1389 sizeof(req_ctx->hash_md), false, false);
1393 error = artpec6_crypto_setup_in_descr(common, ac->pad_buffer, 4, false);
1398 error = artpec6_crypto_setup_out_descr(common,
1399 req_ctx->digeststate,
1400 contextsize, false, false);
1406 if (req_ctx->hash_flags & HASH_FLAG_UPDATE) {
1407 size_t done_bytes = 0;
1408 size_t total_bytes = areq->nbytes + req_ctx->partial_bytes;
1409 size_t ready_bytes = round_down(total_bytes, blocksize);
1410 struct artpec6_crypto_walk walk;
1412 run_hw = ready_bytes > 0;
1413 if (req_ctx->partial_bytes && ready_bytes) {
1414 /* We have a partial buffer and will at least some bytes
1415 * to the HW. Empty this partial buffer before tackling
1418 memcpy(req_ctx->partial_buffer_out,
1419 req_ctx->partial_buffer,
1420 req_ctx->partial_bytes);
1422 error = artpec6_crypto_setup_out_descr(common,
1423 req_ctx->partial_buffer_out,
1424 req_ctx->partial_bytes,
1429 /* Reset partial buffer */
1430 done_bytes += req_ctx->partial_bytes;
1431 req_ctx->partial_bytes = 0;
1434 artpec6_crypto_walk_init(&walk, areq->src);
1436 error = artpec6_crypto_setup_sg_descrs_out(common, &walk,
1443 size_t sg_skip = ready_bytes - done_bytes;
1444 size_t sg_rem = areq->nbytes - sg_skip;
1446 sg_pcopy_to_buffer(areq->src, sg_nents(areq->src),
1447 req_ctx->partial_buffer +
1448 req_ctx->partial_bytes,
1451 req_ctx->partial_bytes += sg_rem;
1454 req_ctx->digcnt += ready_bytes;
1455 req_ctx->hash_flags &= ~(HASH_FLAG_UPDATE);
1459 if (req_ctx->hash_flags & HASH_FLAG_FINALIZE) {
1460 bool needtrim = contextsize != digestsize;
1461 size_t hash_pad_len;
1465 if (variant == ARTPEC6_CRYPTO)
1466 oper = FIELD_GET(A6_CRY_MD_OPER, req_ctx->hash_md);
1468 oper = FIELD_GET(A7_CRY_MD_OPER, req_ctx->hash_md);
1470 /* Write out the partial buffer if present */
1471 if (req_ctx->partial_bytes) {
1472 memcpy(req_ctx->partial_buffer_out,
1473 req_ctx->partial_buffer,
1474 req_ctx->partial_bytes);
1475 error = artpec6_crypto_setup_out_descr(common,
1476 req_ctx->partial_buffer_out,
1477 req_ctx->partial_bytes,
1482 req_ctx->digcnt += req_ctx->partial_bytes;
1483 req_ctx->partial_bytes = 0;
1486 if (req_ctx->hash_flags & HASH_FLAG_HMAC)
1487 digest_bits = 8 * (req_ctx->digcnt + blocksize);
1489 digest_bits = 8 * req_ctx->digcnt;
1491 /* Add the hash pad */
1492 hash_pad_len = create_hash_pad(oper, req_ctx->pad_buffer,
1493 req_ctx->digcnt, digest_bits);
1494 error = artpec6_crypto_setup_out_descr(common,
1495 req_ctx->pad_buffer,
1496 hash_pad_len, false,
1498 req_ctx->digcnt = 0;
1503 /* Descriptor for the final result */
1504 error = artpec6_crypto_setup_in_descr(common, areq->result,
1511 /* Discard the extra context bytes for SHA-384 */
1512 error = artpec6_crypto_setup_in_descr(common,
1513 req_ctx->partial_buffer,
1514 digestsize - contextsize, true);
1519 } else { /* This is not the final operation for this request */
1521 return ARTPEC6_CRYPTO_PREPARE_HASH_NO_START;
1523 /* Save the result to the context */
1524 error = artpec6_crypto_setup_in_descr(common,
1525 req_ctx->digeststate,
1526 contextsize, false);
1532 req_ctx->hash_flags &= ~(HASH_FLAG_INIT_CTX | HASH_FLAG_UPDATE |
1533 HASH_FLAG_FINALIZE);
1535 error = artpec6_crypto_terminate_in_descrs(common);
1539 error = artpec6_crypto_terminate_out_descrs(common);
1543 error = artpec6_crypto_dma_map_descs(common);
1547 return ARTPEC6_CRYPTO_PREPARE_HASH_START;
1551 static int artpec6_crypto_aes_ecb_init(struct crypto_skcipher *tfm)
1553 struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
1555 tfm->reqsize = sizeof(struct artpec6_crypto_request_context);
1556 ctx->crypto_type = ARTPEC6_CRYPTO_CIPHER_AES_ECB;
1561 static int artpec6_crypto_aes_ctr_init(struct crypto_skcipher *tfm)
1563 struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
1565 ctx->fallback = crypto_alloc_skcipher(crypto_tfm_alg_name(&tfm->base),
1568 CRYPTO_ALG_NEED_FALLBACK);
1569 if (IS_ERR(ctx->fallback))
1570 return PTR_ERR(ctx->fallback);
1572 tfm->reqsize = sizeof(struct artpec6_crypto_request_context);
1573 ctx->crypto_type = ARTPEC6_CRYPTO_CIPHER_AES_CTR;
1578 static int artpec6_crypto_aes_cbc_init(struct crypto_skcipher *tfm)
1580 struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
1582 tfm->reqsize = sizeof(struct artpec6_crypto_request_context);
1583 ctx->crypto_type = ARTPEC6_CRYPTO_CIPHER_AES_CBC;
1588 static int artpec6_crypto_aes_xts_init(struct crypto_skcipher *tfm)
1590 struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
1592 tfm->reqsize = sizeof(struct artpec6_crypto_request_context);
1593 ctx->crypto_type = ARTPEC6_CRYPTO_CIPHER_AES_XTS;
1598 static void artpec6_crypto_aes_exit(struct crypto_skcipher *tfm)
1600 struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
1602 memset(ctx, 0, sizeof(*ctx));
1605 static void artpec6_crypto_aes_ctr_exit(struct crypto_skcipher *tfm)
1607 struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
1609 crypto_free_skcipher(ctx->fallback);
1610 artpec6_crypto_aes_exit(tfm);
1614 artpec6_crypto_cipher_set_key(struct crypto_skcipher *cipher, const u8 *key,
1615 unsigned int keylen)
1617 struct artpec6_cryptotfm_context *ctx =
1618 crypto_skcipher_ctx(cipher);
1626 crypto_skcipher_set_flags(cipher,
1627 CRYPTO_TFM_RES_BAD_KEY_LEN);
1631 memcpy(ctx->aes_key, key, keylen);
1632 ctx->key_length = keylen;
1637 artpec6_crypto_xts_set_key(struct crypto_skcipher *cipher, const u8 *key,
1638 unsigned int keylen)
1640 struct artpec6_cryptotfm_context *ctx =
1641 crypto_skcipher_ctx(cipher);
1644 ret = xts_check_key(&cipher->base, key, keylen);
1654 crypto_skcipher_set_flags(cipher,
1655 CRYPTO_TFM_RES_BAD_KEY_LEN);
1659 memcpy(ctx->aes_key, key, keylen);
1660 ctx->key_length = keylen;
1664 /** artpec6_crypto_process_crypto - Prepare an async block cipher crypto request
1666 * @req: The asynch request to process
1668 * @return 0 if the dma job was successfully prepared
1671 * This function sets up the PDMA descriptors for a block cipher request.
1673 * The required padding is added for AES-CTR using a statically defined
1676 * The PDMA descriptor list will be as follows:
1678 * OUT: [KEY_MD][KEY][EOP]<CIPHER_MD>[IV]<data_0>...[data_n][AES-CTR_pad]<eop>
1679 * IN: <CIPHER_MD><data_0>...[data_n]<intr>
1682 static int artpec6_crypto_prepare_crypto(struct skcipher_request *areq)
1685 struct artpec6_crypto_walk walk;
1686 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(areq);
1687 struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(cipher);
1688 struct artpec6_crypto_request_context *req_ctx = NULL;
1689 size_t iv_len = crypto_skcipher_ivsize(cipher);
1690 struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
1691 enum artpec6_crypto_variant variant = ac->variant;
1692 struct artpec6_crypto_req_common *common;
1693 bool cipher_decr = false;
1695 u32 cipher_len = 0; /* Same as regk_crypto_key_128 for NULL crypto */
1698 req_ctx = skcipher_request_ctx(areq);
1699 common = &req_ctx->common;
1701 artpec6_crypto_init_dma_operation(common);
1703 if (variant == ARTPEC6_CRYPTO)
1704 ctx->key_md = FIELD_PREP(A6_CRY_MD_OPER, a6_regk_crypto_dlkey);
1706 ctx->key_md = FIELD_PREP(A7_CRY_MD_OPER, a7_regk_crypto_dlkey);
1708 ret = artpec6_crypto_setup_out_descr(common, (void *)&ctx->key_md,
1709 sizeof(ctx->key_md), false, false);
1713 ret = artpec6_crypto_setup_out_descr(common, ctx->aes_key,
1714 ctx->key_length, true, false);
1718 req_ctx->cipher_md = 0;
1720 if (ctx->crypto_type == ARTPEC6_CRYPTO_CIPHER_AES_XTS)
1721 cipher_klen = ctx->key_length/2;
1723 cipher_klen = ctx->key_length;
1726 switch (cipher_klen) {
1728 cipher_len = regk_crypto_key_128;
1731 cipher_len = regk_crypto_key_192;
1734 cipher_len = regk_crypto_key_256;
1737 pr_err("%s: Invalid key length %d!\n",
1738 MODULE_NAME, ctx->key_length);
1742 switch (ctx->crypto_type) {
1743 case ARTPEC6_CRYPTO_CIPHER_AES_ECB:
1744 oper = regk_crypto_aes_ecb;
1745 cipher_decr = req_ctx->decrypt;
1748 case ARTPEC6_CRYPTO_CIPHER_AES_CBC:
1749 oper = regk_crypto_aes_cbc;
1750 cipher_decr = req_ctx->decrypt;
1753 case ARTPEC6_CRYPTO_CIPHER_AES_CTR:
1754 oper = regk_crypto_aes_ctr;
1755 cipher_decr = false;
1758 case ARTPEC6_CRYPTO_CIPHER_AES_XTS:
1759 oper = regk_crypto_aes_xts;
1760 cipher_decr = req_ctx->decrypt;
1762 if (variant == ARTPEC6_CRYPTO)
1763 req_ctx->cipher_md |= A6_CRY_MD_CIPHER_DSEQ;
1765 req_ctx->cipher_md |= A7_CRY_MD_CIPHER_DSEQ;
1769 pr_err("%s: Invalid cipher mode %d!\n",
1770 MODULE_NAME, ctx->crypto_type);
1774 if (variant == ARTPEC6_CRYPTO) {
1775 req_ctx->cipher_md |= FIELD_PREP(A6_CRY_MD_OPER, oper);
1776 req_ctx->cipher_md |= FIELD_PREP(A6_CRY_MD_CIPHER_LEN,
1779 req_ctx->cipher_md |= A6_CRY_MD_CIPHER_DECR;
1781 req_ctx->cipher_md |= FIELD_PREP(A7_CRY_MD_OPER, oper);
1782 req_ctx->cipher_md |= FIELD_PREP(A7_CRY_MD_CIPHER_LEN,
1785 req_ctx->cipher_md |= A7_CRY_MD_CIPHER_DECR;
1788 ret = artpec6_crypto_setup_out_descr(common,
1789 &req_ctx->cipher_md,
1790 sizeof(req_ctx->cipher_md),
1795 ret = artpec6_crypto_setup_in_descr(common, ac->pad_buffer, 4, false);
1800 ret = artpec6_crypto_setup_out_descr(common, areq->iv, iv_len,
1806 artpec6_crypto_walk_init(&walk, areq->src);
1807 ret = artpec6_crypto_setup_sg_descrs_out(common, &walk, areq->cryptlen);
1812 artpec6_crypto_walk_init(&walk, areq->dst);
1813 ret = artpec6_crypto_setup_sg_descrs_in(common, &walk, areq->cryptlen);
1817 /* CTR-mode padding required by the HW. */
1818 if (ctx->crypto_type == ARTPEC6_CRYPTO_CIPHER_AES_CTR ||
1819 ctx->crypto_type == ARTPEC6_CRYPTO_CIPHER_AES_XTS) {
1820 size_t pad = ALIGN(areq->cryptlen, AES_BLOCK_SIZE) -
1824 ret = artpec6_crypto_setup_out_descr(common,
1830 ret = artpec6_crypto_setup_in_descr(common,
1831 ac->pad_buffer, pad,
1838 ret = artpec6_crypto_terminate_out_descrs(common);
1842 ret = artpec6_crypto_terminate_in_descrs(common);
1846 return artpec6_crypto_dma_map_descs(common);
1849 static int artpec6_crypto_prepare_aead(struct aead_request *areq)
1853 size_t input_length;
1854 struct artpec6_cryptotfm_context *ctx = crypto_tfm_ctx(areq->base.tfm);
1855 struct artpec6_crypto_aead_req_ctx *req_ctx = aead_request_ctx(areq);
1856 struct crypto_aead *cipher = crypto_aead_reqtfm(areq);
1857 struct artpec6_crypto_req_common *common = &req_ctx->common;
1858 struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
1859 enum artpec6_crypto_variant variant = ac->variant;
1862 artpec6_crypto_init_dma_operation(common);
1865 if (variant == ARTPEC6_CRYPTO) {
1866 ctx->key_md = FIELD_PREP(A6_CRY_MD_OPER,
1867 a6_regk_crypto_dlkey);
1869 ctx->key_md = FIELD_PREP(A7_CRY_MD_OPER,
1870 a7_regk_crypto_dlkey);
1872 ret = artpec6_crypto_setup_out_descr(common, (void *)&ctx->key_md,
1873 sizeof(ctx->key_md), false, false);
1877 ret = artpec6_crypto_setup_out_descr(common, ctx->aes_key,
1878 ctx->key_length, true, false);
1882 req_ctx->cipher_md = 0;
1884 switch (ctx->key_length) {
1886 md_cipher_len = regk_crypto_key_128;
1889 md_cipher_len = regk_crypto_key_192;
1892 md_cipher_len = regk_crypto_key_256;
1898 if (variant == ARTPEC6_CRYPTO) {
1899 req_ctx->cipher_md |= FIELD_PREP(A6_CRY_MD_OPER,
1900 regk_crypto_aes_gcm);
1901 req_ctx->cipher_md |= FIELD_PREP(A6_CRY_MD_CIPHER_LEN,
1903 if (req_ctx->decrypt)
1904 req_ctx->cipher_md |= A6_CRY_MD_CIPHER_DECR;
1906 req_ctx->cipher_md |= FIELD_PREP(A7_CRY_MD_OPER,
1907 regk_crypto_aes_gcm);
1908 req_ctx->cipher_md |= FIELD_PREP(A7_CRY_MD_CIPHER_LEN,
1910 if (req_ctx->decrypt)
1911 req_ctx->cipher_md |= A7_CRY_MD_CIPHER_DECR;
1914 ret = artpec6_crypto_setup_out_descr(common,
1915 (void *) &req_ctx->cipher_md,
1916 sizeof(req_ctx->cipher_md), false,
1921 ret = artpec6_crypto_setup_in_descr(common, ac->pad_buffer, 4, false);
1925 /* For the decryption, cryptlen includes the tag. */
1926 input_length = areq->cryptlen;
1927 if (req_ctx->decrypt)
1928 input_length -= AES_BLOCK_SIZE;
1930 /* Prepare the context buffer */
1931 req_ctx->hw_ctx.aad_length_bits =
1932 __cpu_to_be64(8*areq->assoclen);
1934 req_ctx->hw_ctx.text_length_bits =
1935 __cpu_to_be64(8*input_length);
1937 memcpy(req_ctx->hw_ctx.J0, areq->iv, crypto_aead_ivsize(cipher));
1938 // The HW omits the initial increment of the counter field.
1939 memcpy(req_ctx->hw_ctx.J0 + GCM_AES_IV_SIZE, "\x00\x00\x00\x01", 4);
1941 ret = artpec6_crypto_setup_out_descr(common, &req_ctx->hw_ctx,
1942 sizeof(struct artpec6_crypto_aead_hw_ctx), false, false);
1947 struct artpec6_crypto_walk walk;
1949 artpec6_crypto_walk_init(&walk, areq->src);
1951 /* Associated data */
1952 count = areq->assoclen;
1953 ret = artpec6_crypto_setup_sg_descrs_out(common, &walk, count);
1957 if (!IS_ALIGNED(areq->assoclen, 16)) {
1958 size_t assoc_pad = 16 - (areq->assoclen % 16);
1959 /* The HW mandates zero padding here */
1960 ret = artpec6_crypto_setup_out_descr(common,
1968 /* Data to crypto */
1969 count = input_length;
1970 ret = artpec6_crypto_setup_sg_descrs_out(common, &walk, count);
1974 if (!IS_ALIGNED(input_length, 16)) {
1975 size_t crypto_pad = 16 - (input_length % 16);
1976 /* The HW mandates zero padding here */
1977 ret = artpec6_crypto_setup_out_descr(common,
1987 /* Data from crypto */
1989 struct artpec6_crypto_walk walk;
1990 size_t output_len = areq->cryptlen;
1992 if (req_ctx->decrypt)
1993 output_len -= AES_BLOCK_SIZE;
1995 artpec6_crypto_walk_init(&walk, areq->dst);
1997 /* skip associated data in the output */
1998 count = artpec6_crypto_walk_advance(&walk, areq->assoclen);
2003 ret = artpec6_crypto_setup_sg_descrs_in(common, &walk, count);
2007 /* Put padding between the cryptotext and the auth tag */
2008 if (!IS_ALIGNED(output_len, 16)) {
2009 size_t crypto_pad = 16 - (output_len % 16);
2011 ret = artpec6_crypto_setup_in_descr(common,
2018 /* The authentication tag shall follow immediately after
2019 * the output ciphertext. For decryption it is put in a context
2020 * buffer for later compare against the input tag.
2022 count = AES_BLOCK_SIZE;
2024 if (req_ctx->decrypt) {
2025 ret = artpec6_crypto_setup_in_descr(common,
2026 req_ctx->decryption_tag, count, false);
2031 ret = artpec6_crypto_setup_sg_descrs_in(common, &walk,
2039 ret = artpec6_crypto_terminate_in_descrs(common);
2043 ret = artpec6_crypto_terminate_out_descrs(common);
2047 return artpec6_crypto_dma_map_descs(common);
2050 static void artpec6_crypto_process_queue(struct artpec6_crypto *ac,
2051 struct list_head *completions)
2053 struct artpec6_crypto_req_common *req;
2055 while (!list_empty(&ac->queue) && !artpec6_crypto_busy()) {
2056 req = list_first_entry(&ac->queue,
2057 struct artpec6_crypto_req_common,
2059 list_move_tail(&req->list, &ac->pending);
2060 artpec6_crypto_start_dma(req);
2062 list_add_tail(&req->complete_in_progress, completions);
2066 * In some cases, the hardware can raise an in_eop_flush interrupt
2067 * before actually updating the status, so we have an timer which will
2068 * recheck the status on timeout. Since the cases are expected to be
2069 * very rare, we use a relatively large timeout value. There should be
2070 * no noticeable negative effect if we timeout spuriously.
2072 if (ac->pending_count)
2073 mod_timer(&ac->timer, jiffies + msecs_to_jiffies(100));
2075 del_timer(&ac->timer);
2078 static void artpec6_crypto_timeout(struct timer_list *t)
2080 struct artpec6_crypto *ac = from_timer(ac, t, timer);
2082 dev_info_ratelimited(artpec6_crypto_dev, "timeout\n");
2084 tasklet_schedule(&ac->task);
2087 static void artpec6_crypto_task(unsigned long data)
2089 struct artpec6_crypto *ac = (struct artpec6_crypto *)data;
2090 struct artpec6_crypto_req_common *req;
2091 struct artpec6_crypto_req_common *n;
2092 struct list_head complete_done;
2093 struct list_head complete_in_progress;
2095 INIT_LIST_HEAD(&complete_done);
2096 INIT_LIST_HEAD(&complete_in_progress);
2098 if (list_empty(&ac->pending)) {
2099 pr_debug("Spurious IRQ\n");
2103 spin_lock_bh(&ac->queue_lock);
2105 list_for_each_entry_safe(req, n, &ac->pending, list) {
2106 struct artpec6_crypto_dma_descriptors *dma = req->dma;
2109 dma_sync_single_for_cpu(artpec6_crypto_dev, dma->stat_dma_addr,
2110 sizeof(dma->stat[0]),
2113 stat = req->dma->stat[req->dma->in_cnt-1];
2115 /* A non-zero final status descriptor indicates
2116 * this job has finished.
2118 pr_debug("Request %p status is %X\n", req, stat);
2122 /* Allow testing of timeout handling with fault injection */
2123 #ifdef CONFIG_FAULT_INJECTION
2124 if (should_fail(&artpec6_crypto_fail_status_read, 1))
2128 pr_debug("Completing request %p\n", req);
2130 list_move_tail(&req->list, &complete_done);
2132 artpec6_crypto_dma_unmap_all(req);
2133 artpec6_crypto_copy_bounce_buffers(req);
2135 ac->pending_count--;
2136 artpec6_crypto_common_destroy(req);
2139 artpec6_crypto_process_queue(ac, &complete_in_progress);
2141 spin_unlock_bh(&ac->queue_lock);
2143 /* Perform the completion callbacks without holding the queue lock
2144 * to allow new request submissions from the callbacks.
2146 list_for_each_entry_safe(req, n, &complete_done, list) {
2147 req->complete(req->req);
2150 list_for_each_entry_safe(req, n, &complete_in_progress,
2151 complete_in_progress) {
2152 req->req->complete(req->req, -EINPROGRESS);
2156 static void artpec6_crypto_complete_crypto(struct crypto_async_request *req)
2158 req->complete(req, 0);
2162 artpec6_crypto_complete_cbc_decrypt(struct crypto_async_request *req)
2164 struct skcipher_request *cipher_req = container_of(req,
2165 struct skcipher_request, base);
2167 scatterwalk_map_and_copy(cipher_req->iv, cipher_req->src,
2168 cipher_req->cryptlen - AES_BLOCK_SIZE,
2170 req->complete(req, 0);
2174 artpec6_crypto_complete_cbc_encrypt(struct crypto_async_request *req)
2176 struct skcipher_request *cipher_req = container_of(req,
2177 struct skcipher_request, base);
2179 scatterwalk_map_and_copy(cipher_req->iv, cipher_req->dst,
2180 cipher_req->cryptlen - AES_BLOCK_SIZE,
2182 req->complete(req, 0);
2185 static void artpec6_crypto_complete_aead(struct crypto_async_request *req)
2189 /* Verify GCM hashtag. */
2190 struct aead_request *areq = container_of(req,
2191 struct aead_request, base);
2192 struct artpec6_crypto_aead_req_ctx *req_ctx = aead_request_ctx(areq);
2194 if (req_ctx->decrypt) {
2195 u8 input_tag[AES_BLOCK_SIZE];
2197 sg_pcopy_to_buffer(areq->src,
2198 sg_nents(areq->src),
2201 areq->assoclen + areq->cryptlen -
2204 if (memcmp(req_ctx->decryption_tag,
2207 pr_debug("***EBADMSG:\n");
2208 print_hex_dump_debug("ref:", DUMP_PREFIX_ADDRESS, 32, 1,
2209 input_tag, AES_BLOCK_SIZE, true);
2210 print_hex_dump_debug("out:", DUMP_PREFIX_ADDRESS, 32, 1,
2211 req_ctx->decryption_tag,
2212 AES_BLOCK_SIZE, true);
2218 req->complete(req, result);
2221 static void artpec6_crypto_complete_hash(struct crypto_async_request *req)
2223 req->complete(req, 0);
2227 /*------------------- Hash functions -----------------------------------------*/
2229 artpec6_crypto_hash_set_key(struct crypto_ahash *tfm,
2230 const u8 *key, unsigned int keylen)
2232 struct artpec6_hashalg_context *tfm_ctx = crypto_tfm_ctx(&tfm->base);
2237 pr_err("Invalid length (%d) of HMAC key\n",
2242 memset(tfm_ctx->hmac_key, 0, sizeof(tfm_ctx->hmac_key));
2244 blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2246 if (keylen > blocksize) {
2247 SHASH_DESC_ON_STACK(hdesc, tfm_ctx->child_hash);
2249 hdesc->tfm = tfm_ctx->child_hash;
2250 hdesc->flags = crypto_ahash_get_flags(tfm) &
2251 CRYPTO_TFM_REQ_MAY_SLEEP;
2253 tfm_ctx->hmac_key_length = blocksize;
2254 ret = crypto_shash_digest(hdesc, key, keylen,
2260 memcpy(tfm_ctx->hmac_key, key, keylen);
2261 tfm_ctx->hmac_key_length = keylen;
2268 artpec6_crypto_init_hash(struct ahash_request *req, u8 type, int hmac)
2270 struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
2271 enum artpec6_crypto_variant variant = ac->variant;
2272 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2275 memset(req_ctx, 0, sizeof(*req_ctx));
2277 req_ctx->hash_flags = HASH_FLAG_INIT_CTX;
2279 req_ctx->hash_flags |= (HASH_FLAG_HMAC | HASH_FLAG_UPDATE_KEY);
2282 case ARTPEC6_CRYPTO_HASH_SHA1:
2283 oper = hmac ? regk_crypto_hmac_sha1 : regk_crypto_sha1;
2285 case ARTPEC6_CRYPTO_HASH_SHA256:
2286 oper = hmac ? regk_crypto_hmac_sha256 : regk_crypto_sha256;
2288 case ARTPEC6_CRYPTO_HASH_SHA384:
2289 oper = hmac ? regk_crypto_hmac_sha384 : regk_crypto_sha384;
2291 case ARTPEC6_CRYPTO_HASH_SHA512:
2292 oper = hmac ? regk_crypto_hmac_sha512 : regk_crypto_sha512;
2296 pr_err("%s: Unsupported hash type 0x%x\n", MODULE_NAME, type);
2300 if (variant == ARTPEC6_CRYPTO)
2301 req_ctx->hash_md = FIELD_PREP(A6_CRY_MD_OPER, oper);
2303 req_ctx->hash_md = FIELD_PREP(A7_CRY_MD_OPER, oper);
2308 static int artpec6_crypto_prepare_submit_hash(struct ahash_request *req)
2310 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2313 if (!req_ctx->common.dma) {
2314 ret = artpec6_crypto_common_init(&req_ctx->common,
2316 artpec6_crypto_complete_hash,
2323 ret = artpec6_crypto_prepare_hash(req);
2325 case ARTPEC6_CRYPTO_PREPARE_HASH_START:
2326 ret = artpec6_crypto_submit(&req_ctx->common);
2329 case ARTPEC6_CRYPTO_PREPARE_HASH_NO_START:
2334 artpec6_crypto_common_destroy(&req_ctx->common);
2341 static int artpec6_crypto_hash_final(struct ahash_request *req)
2343 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2345 req_ctx->hash_flags |= HASH_FLAG_FINALIZE;
2347 return artpec6_crypto_prepare_submit_hash(req);
2350 static int artpec6_crypto_hash_update(struct ahash_request *req)
2352 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2354 req_ctx->hash_flags |= HASH_FLAG_UPDATE;
2356 return artpec6_crypto_prepare_submit_hash(req);
2359 static int artpec6_crypto_sha1_init(struct ahash_request *req)
2361 return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA1, 0);
2364 static int artpec6_crypto_sha1_digest(struct ahash_request *req)
2366 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2368 artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA1, 0);
2370 req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
2372 return artpec6_crypto_prepare_submit_hash(req);
2375 static int artpec6_crypto_sha256_init(struct ahash_request *req)
2377 return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA256, 0);
2380 static int artpec6_crypto_sha256_digest(struct ahash_request *req)
2382 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2384 artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA256, 0);
2385 req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
2387 return artpec6_crypto_prepare_submit_hash(req);
2390 static int __maybe_unused artpec6_crypto_sha384_init(struct ahash_request *req)
2392 return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA384, 0);
2395 static int __maybe_unused
2396 artpec6_crypto_sha384_digest(struct ahash_request *req)
2398 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2400 artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA384, 0);
2401 req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
2403 return artpec6_crypto_prepare_submit_hash(req);
2406 static int artpec6_crypto_sha512_init(struct ahash_request *req)
2408 return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA512, 0);
2411 static int artpec6_crypto_sha512_digest(struct ahash_request *req)
2413 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2415 artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA512, 0);
2416 req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
2418 return artpec6_crypto_prepare_submit_hash(req);
2421 static int artpec6_crypto_hmac_sha256_init(struct ahash_request *req)
2423 return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA256, 1);
2426 static int __maybe_unused
2427 artpec6_crypto_hmac_sha384_init(struct ahash_request *req)
2429 return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA384, 1);
2432 static int artpec6_crypto_hmac_sha512_init(struct ahash_request *req)
2434 return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA512, 1);
2437 static int artpec6_crypto_hmac_sha256_digest(struct ahash_request *req)
2439 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2441 artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA256, 1);
2442 req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
2444 return artpec6_crypto_prepare_submit_hash(req);
2447 static int __maybe_unused
2448 artpec6_crypto_hmac_sha384_digest(struct ahash_request *req)
2450 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2452 artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA384, 1);
2453 req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
2455 return artpec6_crypto_prepare_submit_hash(req);
2458 static int artpec6_crypto_hmac_sha512_digest(struct ahash_request *req)
2460 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2462 artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA512, 1);
2463 req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
2465 return artpec6_crypto_prepare_submit_hash(req);
2468 static int artpec6_crypto_ahash_init_common(struct crypto_tfm *tfm,
2469 const char *base_hash_name)
2471 struct artpec6_hashalg_context *tfm_ctx = crypto_tfm_ctx(tfm);
2473 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2474 sizeof(struct artpec6_hash_request_context));
2475 memset(tfm_ctx, 0, sizeof(*tfm_ctx));
2477 if (base_hash_name) {
2478 struct crypto_shash *child;
2480 child = crypto_alloc_shash(base_hash_name, 0,
2481 CRYPTO_ALG_NEED_FALLBACK);
2484 return PTR_ERR(child);
2486 tfm_ctx->child_hash = child;
2492 static int artpec6_crypto_ahash_init(struct crypto_tfm *tfm)
2494 return artpec6_crypto_ahash_init_common(tfm, NULL);
2497 static int artpec6_crypto_ahash_init_hmac_sha256(struct crypto_tfm *tfm)
2499 return artpec6_crypto_ahash_init_common(tfm, "sha256");
2502 static int __maybe_unused
2503 artpec6_crypto_ahash_init_hmac_sha384(struct crypto_tfm *tfm)
2505 return artpec6_crypto_ahash_init_common(tfm, "sha384");
2508 static int artpec6_crypto_ahash_init_hmac_sha512(struct crypto_tfm *tfm)
2510 return artpec6_crypto_ahash_init_common(tfm, "sha512");
2513 static void artpec6_crypto_ahash_exit(struct crypto_tfm *tfm)
2515 struct artpec6_hashalg_context *tfm_ctx = crypto_tfm_ctx(tfm);
2517 if (tfm_ctx->child_hash)
2518 crypto_free_shash(tfm_ctx->child_hash);
2520 memset(tfm_ctx->hmac_key, 0, sizeof(tfm_ctx->hmac_key));
2521 tfm_ctx->hmac_key_length = 0;
2524 static int artpec6_crypto_hash_export(struct ahash_request *req, void *out)
2526 const struct artpec6_hash_request_context *ctx = ahash_request_ctx(req);
2527 struct artpec6_hash_export_state *state = out;
2528 struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
2529 enum artpec6_crypto_variant variant = ac->variant;
2531 BUILD_BUG_ON(sizeof(state->partial_buffer) !=
2532 sizeof(ctx->partial_buffer));
2533 BUILD_BUG_ON(sizeof(state->digeststate) != sizeof(ctx->digeststate));
2535 state->digcnt = ctx->digcnt;
2536 state->partial_bytes = ctx->partial_bytes;
2537 state->hash_flags = ctx->hash_flags;
2539 if (variant == ARTPEC6_CRYPTO)
2540 state->oper = FIELD_GET(A6_CRY_MD_OPER, ctx->hash_md);
2542 state->oper = FIELD_GET(A7_CRY_MD_OPER, ctx->hash_md);
2544 memcpy(state->partial_buffer, ctx->partial_buffer,
2545 sizeof(state->partial_buffer));
2546 memcpy(state->digeststate, ctx->digeststate,
2547 sizeof(state->digeststate));
2552 static int artpec6_crypto_hash_import(struct ahash_request *req, const void *in)
2554 struct artpec6_hash_request_context *ctx = ahash_request_ctx(req);
2555 const struct artpec6_hash_export_state *state = in;
2556 struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
2557 enum artpec6_crypto_variant variant = ac->variant;
2559 memset(ctx, 0, sizeof(*ctx));
2561 ctx->digcnt = state->digcnt;
2562 ctx->partial_bytes = state->partial_bytes;
2563 ctx->hash_flags = state->hash_flags;
2565 if (variant == ARTPEC6_CRYPTO)
2566 ctx->hash_md = FIELD_PREP(A6_CRY_MD_OPER, state->oper);
2568 ctx->hash_md = FIELD_PREP(A7_CRY_MD_OPER, state->oper);
2570 memcpy(ctx->partial_buffer, state->partial_buffer,
2571 sizeof(state->partial_buffer));
2572 memcpy(ctx->digeststate, state->digeststate,
2573 sizeof(state->digeststate));
2578 static int init_crypto_hw(struct artpec6_crypto *ac)
2580 enum artpec6_crypto_variant variant = ac->variant;
2581 void __iomem *base = ac->base;
2582 u32 out_descr_buf_size;
2583 u32 out_data_buf_size;
2584 u32 in_data_buf_size;
2585 u32 in_descr_buf_size;
2586 u32 in_stat_buf_size;
2590 * The PDMA unit contains 1984 bytes of internal memory for the OUT
2591 * channels and 1024 bytes for the IN channel. This is an elastic
2592 * memory used to internally store the descriptors and data. The values
2593 * ares specified in 64 byte incremements. Trustzone buffers are not
2594 * used at this stage.
2596 out_data_buf_size = 16; /* 1024 bytes for data */
2597 out_descr_buf_size = 15; /* 960 bytes for descriptors */
2598 in_data_buf_size = 8; /* 512 bytes for data */
2599 in_descr_buf_size = 4; /* 256 bytes for descriptors */
2600 in_stat_buf_size = 4; /* 256 bytes for stat descrs */
2602 BUILD_BUG_ON_MSG((out_data_buf_size
2603 + out_descr_buf_size) * 64 > 1984,
2604 "Invalid OUT configuration");
2606 BUILD_BUG_ON_MSG((in_data_buf_size
2608 + in_stat_buf_size) * 64 > 1024,
2609 "Invalid IN configuration");
2611 in = FIELD_PREP(PDMA_IN_BUF_CFG_DATA_BUF_SIZE, in_data_buf_size) |
2612 FIELD_PREP(PDMA_IN_BUF_CFG_DESCR_BUF_SIZE, in_descr_buf_size) |
2613 FIELD_PREP(PDMA_IN_BUF_CFG_STAT_BUF_SIZE, in_stat_buf_size);
2615 out = FIELD_PREP(PDMA_OUT_BUF_CFG_DATA_BUF_SIZE, out_data_buf_size) |
2616 FIELD_PREP(PDMA_OUT_BUF_CFG_DESCR_BUF_SIZE, out_descr_buf_size);
2618 writel_relaxed(out, base + PDMA_OUT_BUF_CFG);
2619 writel_relaxed(PDMA_OUT_CFG_EN, base + PDMA_OUT_CFG);
2621 if (variant == ARTPEC6_CRYPTO) {
2622 writel_relaxed(in, base + A6_PDMA_IN_BUF_CFG);
2623 writel_relaxed(PDMA_IN_CFG_EN, base + A6_PDMA_IN_CFG);
2624 writel_relaxed(A6_PDMA_INTR_MASK_IN_DATA |
2625 A6_PDMA_INTR_MASK_IN_EOP_FLUSH,
2626 base + A6_PDMA_INTR_MASK);
2628 writel_relaxed(in, base + A7_PDMA_IN_BUF_CFG);
2629 writel_relaxed(PDMA_IN_CFG_EN, base + A7_PDMA_IN_CFG);
2630 writel_relaxed(A7_PDMA_INTR_MASK_IN_DATA |
2631 A7_PDMA_INTR_MASK_IN_EOP_FLUSH,
2632 base + A7_PDMA_INTR_MASK);
2638 static void artpec6_crypto_disable_hw(struct artpec6_crypto *ac)
2640 enum artpec6_crypto_variant variant = ac->variant;
2641 void __iomem *base = ac->base;
2643 if (variant == ARTPEC6_CRYPTO) {
2644 writel_relaxed(A6_PDMA_IN_CMD_STOP, base + A6_PDMA_IN_CMD);
2645 writel_relaxed(0, base + A6_PDMA_IN_CFG);
2646 writel_relaxed(A6_PDMA_OUT_CMD_STOP, base + PDMA_OUT_CMD);
2648 writel_relaxed(A7_PDMA_IN_CMD_STOP, base + A7_PDMA_IN_CMD);
2649 writel_relaxed(0, base + A7_PDMA_IN_CFG);
2650 writel_relaxed(A7_PDMA_OUT_CMD_STOP, base + PDMA_OUT_CMD);
2653 writel_relaxed(0, base + PDMA_OUT_CFG);
2657 static irqreturn_t artpec6_crypto_irq(int irq, void *dev_id)
2659 struct artpec6_crypto *ac = dev_id;
2660 enum artpec6_crypto_variant variant = ac->variant;
2661 void __iomem *base = ac->base;
2662 u32 mask_in_data, mask_in_eop_flush;
2663 u32 in_cmd_flush_stat, in_cmd_reg;
2668 if (variant == ARTPEC6_CRYPTO) {
2669 intr = readl_relaxed(base + A6_PDMA_MASKED_INTR);
2670 mask_in_data = A6_PDMA_INTR_MASK_IN_DATA;
2671 mask_in_eop_flush = A6_PDMA_INTR_MASK_IN_EOP_FLUSH;
2672 in_cmd_flush_stat = A6_PDMA_IN_CMD_FLUSH_STAT;
2673 in_cmd_reg = A6_PDMA_IN_CMD;
2674 ack_intr_reg = A6_PDMA_ACK_INTR;
2676 intr = readl_relaxed(base + A7_PDMA_MASKED_INTR);
2677 mask_in_data = A7_PDMA_INTR_MASK_IN_DATA;
2678 mask_in_eop_flush = A7_PDMA_INTR_MASK_IN_EOP_FLUSH;
2679 in_cmd_flush_stat = A7_PDMA_IN_CMD_FLUSH_STAT;
2680 in_cmd_reg = A7_PDMA_IN_CMD;
2681 ack_intr_reg = A7_PDMA_ACK_INTR;
2684 /* We get two interrupt notifications from each job.
2685 * The in_data means all data was sent to memory and then
2686 * we request a status flush command to write the per-job
2687 * status to its status vector. This ensures that the
2688 * tasklet can detect exactly how many submitted jobs
2689 * that have finished.
2691 if (intr & mask_in_data)
2692 ack |= mask_in_data;
2694 if (intr & mask_in_eop_flush)
2695 ack |= mask_in_eop_flush;
2697 writel_relaxed(in_cmd_flush_stat, base + in_cmd_reg);
2699 writel_relaxed(ack, base + ack_intr_reg);
2701 if (intr & mask_in_eop_flush)
2702 tasklet_schedule(&ac->task);
2707 /*------------------- Algorithm definitions ----------------------------------*/
2710 static struct ahash_alg hash_algos[] = {
2713 .init = artpec6_crypto_sha1_init,
2714 .update = artpec6_crypto_hash_update,
2715 .final = artpec6_crypto_hash_final,
2716 .digest = artpec6_crypto_sha1_digest,
2717 .import = artpec6_crypto_hash_import,
2718 .export = artpec6_crypto_hash_export,
2719 .halg.digestsize = SHA1_DIGEST_SIZE,
2720 .halg.statesize = sizeof(struct artpec6_hash_export_state),
2723 .cra_driver_name = "artpec-sha1",
2724 .cra_priority = 300,
2725 .cra_flags = CRYPTO_ALG_ASYNC,
2726 .cra_blocksize = SHA1_BLOCK_SIZE,
2727 .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
2729 .cra_module = THIS_MODULE,
2730 .cra_init = artpec6_crypto_ahash_init,
2731 .cra_exit = artpec6_crypto_ahash_exit,
2736 .init = artpec6_crypto_sha256_init,
2737 .update = artpec6_crypto_hash_update,
2738 .final = artpec6_crypto_hash_final,
2739 .digest = artpec6_crypto_sha256_digest,
2740 .import = artpec6_crypto_hash_import,
2741 .export = artpec6_crypto_hash_export,
2742 .halg.digestsize = SHA256_DIGEST_SIZE,
2743 .halg.statesize = sizeof(struct artpec6_hash_export_state),
2745 .cra_name = "sha256",
2746 .cra_driver_name = "artpec-sha256",
2747 .cra_priority = 300,
2748 .cra_flags = CRYPTO_ALG_ASYNC,
2749 .cra_blocksize = SHA256_BLOCK_SIZE,
2750 .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
2752 .cra_module = THIS_MODULE,
2753 .cra_init = artpec6_crypto_ahash_init,
2754 .cra_exit = artpec6_crypto_ahash_exit,
2759 .init = artpec6_crypto_hmac_sha256_init,
2760 .update = artpec6_crypto_hash_update,
2761 .final = artpec6_crypto_hash_final,
2762 .digest = artpec6_crypto_hmac_sha256_digest,
2763 .import = artpec6_crypto_hash_import,
2764 .export = artpec6_crypto_hash_export,
2765 .setkey = artpec6_crypto_hash_set_key,
2766 .halg.digestsize = SHA256_DIGEST_SIZE,
2767 .halg.statesize = sizeof(struct artpec6_hash_export_state),
2769 .cra_name = "hmac(sha256)",
2770 .cra_driver_name = "artpec-hmac-sha256",
2771 .cra_priority = 300,
2772 .cra_flags = CRYPTO_ALG_ASYNC,
2773 .cra_blocksize = SHA256_BLOCK_SIZE,
2774 .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
2776 .cra_module = THIS_MODULE,
2777 .cra_init = artpec6_crypto_ahash_init_hmac_sha256,
2778 .cra_exit = artpec6_crypto_ahash_exit,
2783 static struct ahash_alg artpec7_hash_algos[] = {
2786 .init = artpec6_crypto_sha384_init,
2787 .update = artpec6_crypto_hash_update,
2788 .final = artpec6_crypto_hash_final,
2789 .digest = artpec6_crypto_sha384_digest,
2790 .import = artpec6_crypto_hash_import,
2791 .export = artpec6_crypto_hash_export,
2792 .halg.digestsize = SHA384_DIGEST_SIZE,
2793 .halg.statesize = sizeof(struct artpec6_hash_export_state),
2795 .cra_name = "sha384",
2796 .cra_driver_name = "artpec-sha384",
2797 .cra_priority = 300,
2798 .cra_flags = CRYPTO_ALG_ASYNC,
2799 .cra_blocksize = SHA384_BLOCK_SIZE,
2800 .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
2802 .cra_module = THIS_MODULE,
2803 .cra_init = artpec6_crypto_ahash_init,
2804 .cra_exit = artpec6_crypto_ahash_exit,
2809 .init = artpec6_crypto_hmac_sha384_init,
2810 .update = artpec6_crypto_hash_update,
2811 .final = artpec6_crypto_hash_final,
2812 .digest = artpec6_crypto_hmac_sha384_digest,
2813 .import = artpec6_crypto_hash_import,
2814 .export = artpec6_crypto_hash_export,
2815 .setkey = artpec6_crypto_hash_set_key,
2816 .halg.digestsize = SHA384_DIGEST_SIZE,
2817 .halg.statesize = sizeof(struct artpec6_hash_export_state),
2819 .cra_name = "hmac(sha384)",
2820 .cra_driver_name = "artpec-hmac-sha384",
2821 .cra_priority = 300,
2822 .cra_flags = CRYPTO_ALG_ASYNC,
2823 .cra_blocksize = SHA384_BLOCK_SIZE,
2824 .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
2826 .cra_module = THIS_MODULE,
2827 .cra_init = artpec6_crypto_ahash_init_hmac_sha384,
2828 .cra_exit = artpec6_crypto_ahash_exit,
2833 .init = artpec6_crypto_sha512_init,
2834 .update = artpec6_crypto_hash_update,
2835 .final = artpec6_crypto_hash_final,
2836 .digest = artpec6_crypto_sha512_digest,
2837 .import = artpec6_crypto_hash_import,
2838 .export = artpec6_crypto_hash_export,
2839 .halg.digestsize = SHA512_DIGEST_SIZE,
2840 .halg.statesize = sizeof(struct artpec6_hash_export_state),
2842 .cra_name = "sha512",
2843 .cra_driver_name = "artpec-sha512",
2844 .cra_priority = 300,
2845 .cra_flags = CRYPTO_ALG_ASYNC,
2846 .cra_blocksize = SHA512_BLOCK_SIZE,
2847 .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
2849 .cra_module = THIS_MODULE,
2850 .cra_init = artpec6_crypto_ahash_init,
2851 .cra_exit = artpec6_crypto_ahash_exit,
2856 .init = artpec6_crypto_hmac_sha512_init,
2857 .update = artpec6_crypto_hash_update,
2858 .final = artpec6_crypto_hash_final,
2859 .digest = artpec6_crypto_hmac_sha512_digest,
2860 .import = artpec6_crypto_hash_import,
2861 .export = artpec6_crypto_hash_export,
2862 .setkey = artpec6_crypto_hash_set_key,
2863 .halg.digestsize = SHA512_DIGEST_SIZE,
2864 .halg.statesize = sizeof(struct artpec6_hash_export_state),
2866 .cra_name = "hmac(sha512)",
2867 .cra_driver_name = "artpec-hmac-sha512",
2868 .cra_priority = 300,
2869 .cra_flags = CRYPTO_ALG_ASYNC,
2870 .cra_blocksize = SHA512_BLOCK_SIZE,
2871 .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
2873 .cra_module = THIS_MODULE,
2874 .cra_init = artpec6_crypto_ahash_init_hmac_sha512,
2875 .cra_exit = artpec6_crypto_ahash_exit,
2881 static struct skcipher_alg crypto_algos[] = {
2885 .cra_name = "ecb(aes)",
2886 .cra_driver_name = "artpec6-ecb-aes",
2887 .cra_priority = 300,
2888 .cra_flags = CRYPTO_ALG_ASYNC,
2889 .cra_blocksize = AES_BLOCK_SIZE,
2890 .cra_ctxsize = sizeof(struct artpec6_cryptotfm_context),
2892 .cra_module = THIS_MODULE,
2894 .min_keysize = AES_MIN_KEY_SIZE,
2895 .max_keysize = AES_MAX_KEY_SIZE,
2896 .setkey = artpec6_crypto_cipher_set_key,
2897 .encrypt = artpec6_crypto_encrypt,
2898 .decrypt = artpec6_crypto_decrypt,
2899 .init = artpec6_crypto_aes_ecb_init,
2900 .exit = artpec6_crypto_aes_exit,
2905 .cra_name = "ctr(aes)",
2906 .cra_driver_name = "artpec6-ctr-aes",
2907 .cra_priority = 300,
2908 .cra_flags = CRYPTO_ALG_ASYNC |
2909 CRYPTO_ALG_NEED_FALLBACK,
2911 .cra_ctxsize = sizeof(struct artpec6_cryptotfm_context),
2913 .cra_module = THIS_MODULE,
2915 .min_keysize = AES_MIN_KEY_SIZE,
2916 .max_keysize = AES_MAX_KEY_SIZE,
2917 .ivsize = AES_BLOCK_SIZE,
2918 .setkey = artpec6_crypto_cipher_set_key,
2919 .encrypt = artpec6_crypto_ctr_encrypt,
2920 .decrypt = artpec6_crypto_ctr_decrypt,
2921 .init = artpec6_crypto_aes_ctr_init,
2922 .exit = artpec6_crypto_aes_ctr_exit,
2927 .cra_name = "cbc(aes)",
2928 .cra_driver_name = "artpec6-cbc-aes",
2929 .cra_priority = 300,
2930 .cra_flags = CRYPTO_ALG_ASYNC,
2931 .cra_blocksize = AES_BLOCK_SIZE,
2932 .cra_ctxsize = sizeof(struct artpec6_cryptotfm_context),
2934 .cra_module = THIS_MODULE,
2936 .min_keysize = AES_MIN_KEY_SIZE,
2937 .max_keysize = AES_MAX_KEY_SIZE,
2938 .ivsize = AES_BLOCK_SIZE,
2939 .setkey = artpec6_crypto_cipher_set_key,
2940 .encrypt = artpec6_crypto_encrypt,
2941 .decrypt = artpec6_crypto_decrypt,
2942 .init = artpec6_crypto_aes_cbc_init,
2943 .exit = artpec6_crypto_aes_exit
2948 .cra_name = "xts(aes)",
2949 .cra_driver_name = "artpec6-xts-aes",
2950 .cra_priority = 300,
2951 .cra_flags = CRYPTO_ALG_ASYNC,
2953 .cra_ctxsize = sizeof(struct artpec6_cryptotfm_context),
2955 .cra_module = THIS_MODULE,
2957 .min_keysize = 2*AES_MIN_KEY_SIZE,
2958 .max_keysize = 2*AES_MAX_KEY_SIZE,
2960 .setkey = artpec6_crypto_xts_set_key,
2961 .encrypt = artpec6_crypto_encrypt,
2962 .decrypt = artpec6_crypto_decrypt,
2963 .init = artpec6_crypto_aes_xts_init,
2964 .exit = artpec6_crypto_aes_exit,
2968 static struct aead_alg aead_algos[] = {
2970 .init = artpec6_crypto_aead_init,
2971 .setkey = artpec6_crypto_aead_set_key,
2972 .encrypt = artpec6_crypto_aead_encrypt,
2973 .decrypt = artpec6_crypto_aead_decrypt,
2974 .ivsize = GCM_AES_IV_SIZE,
2975 .maxauthsize = AES_BLOCK_SIZE,
2978 .cra_name = "gcm(aes)",
2979 .cra_driver_name = "artpec-gcm-aes",
2980 .cra_priority = 300,
2981 .cra_flags = CRYPTO_ALG_ASYNC |
2982 CRYPTO_ALG_KERN_DRIVER_ONLY,
2984 .cra_ctxsize = sizeof(struct artpec6_cryptotfm_context),
2986 .cra_module = THIS_MODULE,
2991 #ifdef CONFIG_DEBUG_FS
3000 static struct dentry *dbgfs_root;
3002 static void artpec6_crypto_init_debugfs(void)
3004 dbgfs_root = debugfs_create_dir("artpec6_crypto", NULL);
3006 if (!dbgfs_root || IS_ERR(dbgfs_root)) {
3008 pr_err("%s: Could not initialise debugfs!\n", MODULE_NAME);
3012 #ifdef CONFIG_FAULT_INJECTION
3013 fault_create_debugfs_attr("fail_status_read", dbgfs_root,
3014 &artpec6_crypto_fail_status_read);
3016 fault_create_debugfs_attr("fail_dma_array_full", dbgfs_root,
3017 &artpec6_crypto_fail_dma_array_full);
3021 static void artpec6_crypto_free_debugfs(void)
3026 debugfs_remove_recursive(dbgfs_root);
3031 static const struct of_device_id artpec6_crypto_of_match[] = {
3032 { .compatible = "axis,artpec6-crypto", .data = (void *)ARTPEC6_CRYPTO },
3033 { .compatible = "axis,artpec7-crypto", .data = (void *)ARTPEC7_CRYPTO },
3036 MODULE_DEVICE_TABLE(of, artpec6_crypto_of_match);
3038 static int artpec6_crypto_probe(struct platform_device *pdev)
3040 const struct of_device_id *match;
3041 enum artpec6_crypto_variant variant;
3042 struct artpec6_crypto *ac;
3043 struct device *dev = &pdev->dev;
3045 struct resource *res;
3049 if (artpec6_crypto_dev)
3052 match = of_match_node(artpec6_crypto_of_match, dev->of_node);
3056 variant = (enum artpec6_crypto_variant)match->data;
3058 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3059 base = devm_ioremap_resource(&pdev->dev, res);
3061 return PTR_ERR(base);
3063 irq = platform_get_irq(pdev, 0);
3067 ac = devm_kzalloc(&pdev->dev, sizeof(struct artpec6_crypto),
3072 platform_set_drvdata(pdev, ac);
3073 ac->variant = variant;
3075 spin_lock_init(&ac->queue_lock);
3076 INIT_LIST_HEAD(&ac->queue);
3077 INIT_LIST_HEAD(&ac->pending);
3078 timer_setup(&ac->timer, artpec6_crypto_timeout, 0);
3082 ac->dma_cache = kmem_cache_create("artpec6_crypto_dma",
3083 sizeof(struct artpec6_crypto_dma_descriptors),
3090 #ifdef CONFIG_DEBUG_FS
3091 artpec6_crypto_init_debugfs();
3094 tasklet_init(&ac->task, artpec6_crypto_task,
3097 ac->pad_buffer = devm_kzalloc(&pdev->dev, 2 * ARTPEC_CACHE_LINE_MAX,
3099 if (!ac->pad_buffer)
3101 ac->pad_buffer = PTR_ALIGN(ac->pad_buffer, ARTPEC_CACHE_LINE_MAX);
3103 ac->zero_buffer = devm_kzalloc(&pdev->dev, 2 * ARTPEC_CACHE_LINE_MAX,
3105 if (!ac->zero_buffer)
3107 ac->zero_buffer = PTR_ALIGN(ac->zero_buffer, ARTPEC_CACHE_LINE_MAX);
3109 err = init_crypto_hw(ac);
3113 err = devm_request_irq(&pdev->dev, irq, artpec6_crypto_irq, 0,
3114 "artpec6-crypto", ac);
3118 artpec6_crypto_dev = &pdev->dev;
3120 err = crypto_register_ahashes(hash_algos, ARRAY_SIZE(hash_algos));
3122 dev_err(dev, "Failed to register ahashes\n");
3126 if (variant != ARTPEC6_CRYPTO) {
3127 err = crypto_register_ahashes(artpec7_hash_algos,
3128 ARRAY_SIZE(artpec7_hash_algos));
3130 dev_err(dev, "Failed to register ahashes\n");
3131 goto unregister_ahashes;
3135 err = crypto_register_skciphers(crypto_algos, ARRAY_SIZE(crypto_algos));
3137 dev_err(dev, "Failed to register ciphers\n");
3138 goto unregister_a7_ahashes;
3141 err = crypto_register_aeads(aead_algos, ARRAY_SIZE(aead_algos));
3143 dev_err(dev, "Failed to register aeads\n");
3144 goto unregister_algs;
3150 crypto_unregister_skciphers(crypto_algos, ARRAY_SIZE(crypto_algos));
3151 unregister_a7_ahashes:
3152 if (variant != ARTPEC6_CRYPTO)
3153 crypto_unregister_ahashes(artpec7_hash_algos,
3154 ARRAY_SIZE(artpec7_hash_algos));
3156 crypto_unregister_ahashes(hash_algos, ARRAY_SIZE(hash_algos));
3158 artpec6_crypto_disable_hw(ac);
3160 kmem_cache_destroy(ac->dma_cache);
3164 static int artpec6_crypto_remove(struct platform_device *pdev)
3166 struct artpec6_crypto *ac = platform_get_drvdata(pdev);
3167 int irq = platform_get_irq(pdev, 0);
3169 crypto_unregister_ahashes(hash_algos, ARRAY_SIZE(hash_algos));
3170 if (ac->variant != ARTPEC6_CRYPTO)
3171 crypto_unregister_ahashes(artpec7_hash_algos,
3172 ARRAY_SIZE(artpec7_hash_algos));
3173 crypto_unregister_skciphers(crypto_algos, ARRAY_SIZE(crypto_algos));
3174 crypto_unregister_aeads(aead_algos, ARRAY_SIZE(aead_algos));
3176 tasklet_disable(&ac->task);
3177 devm_free_irq(&pdev->dev, irq, ac);
3178 tasklet_kill(&ac->task);
3179 del_timer_sync(&ac->timer);
3181 artpec6_crypto_disable_hw(ac);
3183 kmem_cache_destroy(ac->dma_cache);
3184 #ifdef CONFIG_DEBUG_FS
3185 artpec6_crypto_free_debugfs();
3190 static struct platform_driver artpec6_crypto_driver = {
3191 .probe = artpec6_crypto_probe,
3192 .remove = artpec6_crypto_remove,
3194 .name = "artpec6-crypto",
3195 .owner = THIS_MODULE,
3196 .of_match_table = artpec6_crypto_of_match,
3200 module_platform_driver(artpec6_crypto_driver);
3202 MODULE_AUTHOR("Axis Communications AB");
3203 MODULE_DESCRIPTION("ARTPEC-6 Crypto driver");
3204 MODULE_LICENSE("GPL");