1 /*
2 * Driver for ARTPEC-6 crypto block using the kernel asynchronous crypto api.
3 *
4 * Copyright (C) 2014-2017 Axis Communications AB
5 */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
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>
19 #include <linux/of.h>
20 #include <linux/platform_device.h>
21 #include <linux/scatterlist.h>
22 #include <linux/slab.h>
23
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>
32
33 /* Max length of a line in all cache levels for Artpec SoCs. */
34 #define ARTPEC_CACHE_LINE_MAX 32
35
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
41
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
51
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
61
62 #define PDMA_OUT_CFG_EN BIT(0)
63
64 #define PDMA_OUT_BUF_CFG_DATA_BUF_SIZE GENMASK(4, 0)
65 #define PDMA_OUT_BUF_CFG_DESCR_BUF_SIZE GENMASK(9, 5)
66
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)
70
71 #define PDMA_OUT_DESCRQ_PUSH_LEN GENMASK(5, 0)
72 #define PDMA_OUT_DESCRQ_PUSH_ADDR GENMASK(31, 6)
73
74 #define PDMA_OUT_DESCRQ_STAT_LEVEL GENMASK(3, 0)
75 #define PDMA_OUT_DESCRQ_STAT_SIZE GENMASK(7, 4)
76
77 #define PDMA_IN_CFG_EN BIT(0)
78
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)
82
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)
88
89 #define PDMA_IN_STATQ_PUSH_LEN GENMASK(5, 0)
90 #define PDMA_IN_STATQ_PUSH_ADDR GENMASK(31, 6)
91
92 #define PDMA_IN_DESCRQ_PUSH_LEN GENMASK(5, 0)
93 #define PDMA_IN_DESCRQ_PUSH_ADDR GENMASK(31, 6)
94
95 #define PDMA_IN_DESCRQ_STAT_LEVEL GENMASK(3, 0)
96 #define PDMA_IN_DESCRQ_STAT_SIZE GENMASK(7, 4)
97
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)
101
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)
105
106 #define A6_CRY_MD_OPER GENMASK(19, 16)
107
108 #define A6_CRY_MD_HASH_SEL_CTX GENMASK(21, 20)
109 #define A6_CRY_MD_HASH_HMAC_FIN BIT(23)
110
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)
115
116 #define A7_CRY_MD_OPER GENMASK(11, 8)
117
118 #define A7_CRY_MD_HASH_SEL_CTX GENMASK(13, 12)
119 #define A7_CRY_MD_HASH_HMAC_FIN BIT(15)
120
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)
125
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
149
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;
158 } __packed;
159
160 struct pdma_data_descr {
161 unsigned int len : 24;
162 unsigned int buf : 32;
163 } __packed;
164
165 struct pdma_short_descr {
166 unsigned char data[7];
167 } __packed;
168
169 struct pdma_descr {
170 struct pdma_descr_ctrl ctrl;
171 union {
172 struct pdma_data_descr data;
173 struct pdma_short_descr shrt;
174 };
175 };
176
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;
183 };
184
185 /* Each descriptor array can hold max 64 entries */
186 #define PDMA_DESCR_COUNT 64
187
188 #define MODULE_NAME "Artpec-6 CA"
189
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
195
196 /* Crypto modes */
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
201
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.
205 *
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.
208 *
209 * A dma packet is a sequence of descriptors terminated by setting the .eop
210 * field in the final descriptor of the packet.
211 *
212 * Multiple packets are used for providing context data, key data and
213 * the plain/ciphertext.
214 *
215 * PDMA Descriptors (Array)
216 * +------+------+------+~~+-------+------+----
217 * | 0 | 1 | 2 |~~| 11 EOP| 12 | ....
218 * +--+---+--+---+----+-+~~+-------+----+-+----
219 * | | | | |
220 * | | | | |
221 * __|__ +-------++-------++-------+ +----+
222 * | MD | |Payload||Payload||Payload| | MD |
223 * +-----+ +-------++-------++-------+ +----+
224 */
225
226 struct artpec6_crypto_bounce_buffer {
227 struct list_head list;
228 size_t length;
229 struct scatterlist *sg;
230 size_t offset;
231 /* buf is aligned to ARTPEC_CACHE_LINE_MAX and
232 * holds up to ARTPEC_CACHE_LINE_MAX bytes data.
233 */
234 void *buf;
235 };
236
237 struct artpec6_crypto_dma_map {
238 dma_addr_t dma_addr;
239 size_t size;
240 enum dma_data_direction dir;
241 };
242
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;
253 size_t out_cnt;
254 size_t in_cnt;
255 size_t map_count;
256 };
257
258 enum artpec6_crypto_variant {
259 ARTPEC6_CRYPTO,
260 ARTPEC7_CRYPTO,
261 };
262
263 struct artpec6_crypto {
264 void __iomem *base;
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;
270 int pending_count;
271 struct timer_list timer;
272 enum artpec6_crypto_variant variant;
273 void *pad_buffer; /* cache-aligned block padding buffer */
274 void *zero_buffer;
275 };
276
277 enum artpec6_crypto_hash_flags {
278 HASH_FLAG_INIT_CTX = 2,
279 HASH_FLAG_UPDATE = 4,
280 HASH_FLAG_FINALIZE = 8,
281 HASH_FLAG_HMAC = 16,
282 HASH_FLAG_UPDATE_KEY = 32,
283 };
284
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);
291 gfp_t gfp_flags;
292 };
293
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;
301 u64 digcnt;
302 u32 key_md;
303 u32 hash_md;
304 enum artpec6_crypto_hash_flags hash_flags;
305 struct artpec6_crypto_req_common common;
306 };
307
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;
312 u64 digcnt;
313 int oper;
314 unsigned int hash_flags;
315 };
316
317 struct artpec6_hashalg_context {
318 char hmac_key[SHA512_BLOCK_SIZE];
319 size_t hmac_key_length;
320 struct crypto_shash *child_hash;
321 };
322
323 struct artpec6_crypto_request_context {
324 u32 cipher_md;
325 bool decrypt;
326 struct artpec6_crypto_req_common common;
327 };
328
329 struct artpec6_cryptotfm_context {
330 unsigned char aes_key[2*AES_MAX_KEY_SIZE];
331 size_t key_length;
332 u32 key_md;
333 int crypto_type;
334 struct crypto_skcipher *fallback;
335 };
336
337 struct artpec6_crypto_aead_hw_ctx {
338 __be64 aad_length_bits;
339 __be64 text_length_bits;
340 __u8 J0[AES_BLOCK_SIZE];
341 };
342
343 struct artpec6_crypto_aead_req_ctx {
344 struct artpec6_crypto_aead_hw_ctx hw_ctx;
345 u32 cipher_md;
346 bool decrypt;
347 struct artpec6_crypto_req_common common;
348 __u8 decryption_tag[AES_BLOCK_SIZE] ____cacheline_aligned;
349 };
350
351 /* The crypto framework makes it hard to avoid this global. */
352 static struct device *artpec6_crypto_dev;
353
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);
357 #endif
358
359 enum {
360 ARTPEC6_CRYPTO_PREPARE_HASH_NO_START,
361 ARTPEC6_CRYPTO_PREPARE_HASH_START,
362 };
363
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);
367
368 static void
369 artpec6_crypto_complete_crypto(struct crypto_async_request *req);
370 static void
371 artpec6_crypto_complete_cbc_encrypt(struct crypto_async_request *req);
372 static void
373 artpec6_crypto_complete_cbc_decrypt(struct crypto_async_request *req);
374 static void
375 artpec6_crypto_complete_aead(struct crypto_async_request *req);
376 static void
377 artpec6_crypto_complete_hash(struct crypto_async_request *req);
378
379 static int
380 artpec6_crypto_common_destroy(struct artpec6_crypto_req_common *common);
381
382 static void
383 artpec6_crypto_start_dma(struct artpec6_crypto_req_common *common);
384
385 struct artpec6_crypto_walk {
386 struct scatterlist *sg;
387 size_t offset;
388 };
389
artpec6_crypto_walk_init(struct artpec6_crypto_walk * awalk,struct scatterlist * sg)390 static void artpec6_crypto_walk_init(struct artpec6_crypto_walk *awalk,
391 struct scatterlist *sg)
392 {
393 awalk->sg = sg;
394 awalk->offset = 0;
395 }
396
artpec6_crypto_walk_advance(struct artpec6_crypto_walk * awalk,size_t nbytes)397 static size_t artpec6_crypto_walk_advance(struct artpec6_crypto_walk *awalk,
398 size_t nbytes)
399 {
400 while (nbytes && awalk->sg) {
401 size_t piece;
402
403 WARN_ON(awalk->offset > awalk->sg->length);
404
405 piece = min(nbytes, (size_t)awalk->sg->length - awalk->offset);
406 nbytes -= piece;
407 awalk->offset += piece;
408 if (awalk->offset == awalk->sg->length) {
409 awalk->sg = sg_next(awalk->sg);
410 awalk->offset = 0;
411 }
412
413 }
414
415 return nbytes;
416 }
417
418 static size_t
artpec6_crypto_walk_chunklen(const struct artpec6_crypto_walk * awalk)419 artpec6_crypto_walk_chunklen(const struct artpec6_crypto_walk *awalk)
420 {
421 WARN_ON(awalk->sg->length == awalk->offset);
422
423 return awalk->sg->length - awalk->offset;
424 }
425
426 static dma_addr_t
artpec6_crypto_walk_chunk_phys(const struct artpec6_crypto_walk * awalk)427 artpec6_crypto_walk_chunk_phys(const struct artpec6_crypto_walk *awalk)
428 {
429 return sg_phys(awalk->sg) + awalk->offset;
430 }
431
432 static void
artpec6_crypto_copy_bounce_buffers(struct artpec6_crypto_req_common * common)433 artpec6_crypto_copy_bounce_buffers(struct artpec6_crypto_req_common *common)
434 {
435 struct artpec6_crypto_dma_descriptors *dma = common->dma;
436 struct artpec6_crypto_bounce_buffer *b;
437 struct artpec6_crypto_bounce_buffer *next;
438
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,
443 1,
444 b->buf,
445 b->length,
446 b->offset);
447
448 list_del(&b->list);
449 kfree(b);
450 }
451 }
452
artpec6_crypto_busy(void)453 static inline bool artpec6_crypto_busy(void)
454 {
455 struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
456 int fifo_count = ac->pending_count;
457
458 return fifo_count > 6;
459 }
460
artpec6_crypto_submit(struct artpec6_crypto_req_common * req)461 static int artpec6_crypto_submit(struct artpec6_crypto_req_common *req)
462 {
463 struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
464 int ret = -EBUSY;
465
466 spin_lock_bh(&ac->queue_lock);
467
468 if (!artpec6_crypto_busy()) {
469 list_add_tail(&req->list, &ac->pending);
470 artpec6_crypto_start_dma(req);
471 ret = -EINPROGRESS;
472 } else if (req->req->flags & CRYPTO_TFM_REQ_MAY_BACKLOG) {
473 list_add_tail(&req->list, &ac->queue);
474 } else {
475 artpec6_crypto_common_destroy(req);
476 }
477
478 spin_unlock_bh(&ac->queue_lock);
479
480 return ret;
481 }
482
artpec6_crypto_start_dma(struct artpec6_crypto_req_common * common)483 static void artpec6_crypto_start_dma(struct artpec6_crypto_req_common *common)
484 {
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;
490
491 /* Make descriptor content visible to the DMA before starting it. */
492 wmb();
493
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);
496
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);
499
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);
502
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);
507 } else {
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);
511 }
512
513 writel_relaxed(outd, base + PDMA_OUT_DESCRQ_PUSH);
514 writel_relaxed(PDMA_OUT_CMD_START, base + PDMA_OUT_CMD);
515
516 ac->pending_count++;
517 }
518
519 static void
artpec6_crypto_init_dma_operation(struct artpec6_crypto_req_common * common)520 artpec6_crypto_init_dma_operation(struct artpec6_crypto_req_common *common)
521 {
522 struct artpec6_crypto_dma_descriptors *dma = common->dma;
523
524 dma->out_cnt = 0;
525 dma->in_cnt = 0;
526 dma->map_count = 0;
527 INIT_LIST_HEAD(&dma->bounce_buffers);
528 }
529
fault_inject_dma_descr(void)530 static bool fault_inject_dma_descr(void)
531 {
532 #ifdef CONFIG_FAULT_INJECTION
533 return should_fail(&artpec6_crypto_fail_dma_array_full, 1);
534 #else
535 return false;
536 #endif
537 }
538
539 /** artpec6_crypto_setup_out_descr_phys - Setup an out channel with a
540 * physical address
541 *
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
545 *
546 * @return 0 on success or -ENOSPC if there are no more descriptors available
547 */
548 static int
artpec6_crypto_setup_out_descr_phys(struct artpec6_crypto_req_common * common,dma_addr_t addr,size_t len,bool eop)549 artpec6_crypto_setup_out_descr_phys(struct artpec6_crypto_req_common *common,
550 dma_addr_t addr, size_t len, bool eop)
551 {
552 struct artpec6_crypto_dma_descriptors *dma = common->dma;
553 struct pdma_descr *d;
554
555 if (dma->out_cnt >= PDMA_DESCR_COUNT ||
556 fault_inject_dma_descr()) {
557 pr_err("No free OUT DMA descriptors available!\n");
558 return -ENOSPC;
559 }
560
561 d = &dma->out[dma->out_cnt++];
562 memset(d, 0, sizeof(*d));
563
564 d->ctrl.short_descr = 0;
565 d->ctrl.eop = eop;
566 d->data.len = len;
567 d->data.buf = addr;
568 return 0;
569 }
570
571 /** artpec6_crypto_setup_out_descr_short - Setup a short out descriptor
572 *
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
576 *
577 * @return 0 on success
578 * -ENOSPC if no more descriptors are available
579 * -EINVAL if the data length exceeds 7 bytes
580 */
581 static int
artpec6_crypto_setup_out_descr_short(struct artpec6_crypto_req_common * common,void * dst,unsigned int len,bool eop)582 artpec6_crypto_setup_out_descr_short(struct artpec6_crypto_req_common *common,
583 void *dst, unsigned int len, bool eop)
584 {
585 struct artpec6_crypto_dma_descriptors *dma = common->dma;
586 struct pdma_descr *d;
587
588 if (dma->out_cnt >= PDMA_DESCR_COUNT ||
589 fault_inject_dma_descr()) {
590 pr_err("No free OUT DMA descriptors available!\n");
591 return -ENOSPC;
592 } else if (len > 7 || len < 1) {
593 return -EINVAL;
594 }
595 d = &dma->out[dma->out_cnt++];
596 memset(d, 0, sizeof(*d));
597
598 d->ctrl.short_descr = 1;
599 d->ctrl.short_len = len;
600 d->ctrl.eop = eop;
601 memcpy(d->shrt.data, dst, len);
602 return 0;
603 }
604
artpec6_crypto_dma_map_page(struct artpec6_crypto_req_common * common,struct page * page,size_t offset,size_t size,enum dma_data_direction dir,dma_addr_t * dma_addr_out)605 static int artpec6_crypto_dma_map_page(struct artpec6_crypto_req_common *common,
606 struct page *page, size_t offset,
607 size_t size,
608 enum dma_data_direction dir,
609 dma_addr_t *dma_addr_out)
610 {
611 struct artpec6_crypto_dma_descriptors *dma = common->dma;
612 struct device *dev = artpec6_crypto_dev;
613 struct artpec6_crypto_dma_map *map;
614 dma_addr_t dma_addr;
615
616 *dma_addr_out = 0;
617
618 if (dma->map_count >= ARRAY_SIZE(dma->maps))
619 return -ENOMEM;
620
621 dma_addr = dma_map_page(dev, page, offset, size, dir);
622 if (dma_mapping_error(dev, dma_addr))
623 return -ENOMEM;
624
625 map = &dma->maps[dma->map_count++];
626 map->size = size;
627 map->dma_addr = dma_addr;
628 map->dir = dir;
629
630 *dma_addr_out = dma_addr;
631
632 return 0;
633 }
634
635 static int
artpec6_crypto_dma_map_single(struct artpec6_crypto_req_common * common,void * ptr,size_t size,enum dma_data_direction dir,dma_addr_t * dma_addr_out)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)
640 {
641 struct page *page = virt_to_page(ptr);
642 size_t offset = (uintptr_t)ptr & ~PAGE_MASK;
643
644 return artpec6_crypto_dma_map_page(common, page, offset, size, dir,
645 dma_addr_out);
646 }
647
648 static int
artpec6_crypto_dma_map_descs(struct artpec6_crypto_req_common * common)649 artpec6_crypto_dma_map_descs(struct artpec6_crypto_req_common *common)
650 {
651 struct artpec6_crypto_dma_descriptors *dma = common->dma;
652 int ret;
653
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);
657 if (ret)
658 return ret;
659
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);
663 if (ret)
664 return ret;
665
666 /* We only read one stat descriptor */
667 dma->stat[dma->in_cnt - 1] = 0;
668
669 /*
670 * DMA_BIDIRECTIONAL since we need our zeroing of the stat descriptor
671 * to be written.
672 */
673 return artpec6_crypto_dma_map_single(common,
674 dma->stat + dma->in_cnt - 1,
675 sizeof(dma->stat[0]),
676 DMA_BIDIRECTIONAL,
677 &dma->stat_dma_addr);
678 }
679
680 static void
artpec6_crypto_dma_unmap_all(struct artpec6_crypto_req_common * common)681 artpec6_crypto_dma_unmap_all(struct artpec6_crypto_req_common *common)
682 {
683 struct artpec6_crypto_dma_descriptors *dma = common->dma;
684 struct device *dev = artpec6_crypto_dev;
685 int i;
686
687 for (i = 0; i < dma->map_count; i++) {
688 struct artpec6_crypto_dma_map *map = &dma->maps[i];
689
690 dma_unmap_page(dev, map->dma_addr, map->size, map->dir);
691 }
692
693 dma->map_count = 0;
694 }
695
696 /** artpec6_crypto_setup_out_descr - Setup an out descriptor
697 *
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
703 *
704 * @return 0 on success
705 * Any errors from artpec6_crypto_setup_out_descr_short() or
706 * setup_out_descr_phys()
707 */
708 static int
artpec6_crypto_setup_out_descr(struct artpec6_crypto_req_common * common,void * dst,unsigned int len,bool eop,bool use_short)709 artpec6_crypto_setup_out_descr(struct artpec6_crypto_req_common *common,
710 void *dst, unsigned int len, bool eop,
711 bool use_short)
712 {
713 if (use_short && len < 7) {
714 return artpec6_crypto_setup_out_descr_short(common, dst, len,
715 eop);
716 } else {
717 int ret;
718 dma_addr_t dma_addr;
719
720 ret = artpec6_crypto_dma_map_single(common, dst, len,
721 DMA_TO_DEVICE,
722 &dma_addr);
723 if (ret)
724 return ret;
725
726 return artpec6_crypto_setup_out_descr_phys(common, dma_addr,
727 len, eop);
728 }
729 }
730
731 /** artpec6_crypto_setup_in_descr_phys - Setup an in channel with a
732 * physical address
733 *
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
737 * descriptor
738 *
739 */
740 static int
artpec6_crypto_setup_in_descr_phys(struct artpec6_crypto_req_common * common,dma_addr_t addr,unsigned int len,bool intr)741 artpec6_crypto_setup_in_descr_phys(struct artpec6_crypto_req_common *common,
742 dma_addr_t addr, unsigned int len, bool intr)
743 {
744 struct artpec6_crypto_dma_descriptors *dma = common->dma;
745 struct pdma_descr *d;
746
747 if (dma->in_cnt >= PDMA_DESCR_COUNT ||
748 fault_inject_dma_descr()) {
749 pr_err("No free IN DMA descriptors available!\n");
750 return -ENOSPC;
751 }
752 d = &dma->in[dma->in_cnt++];
753 memset(d, 0, sizeof(*d));
754
755 d->ctrl.intr = intr;
756 d->data.len = len;
757 d->data.buf = addr;
758 return 0;
759 }
760
761 /** artpec6_crypto_setup_in_descr - Setup an in channel descriptor
762 *
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
766 * is needed
767 *
768 * Short descriptors are not used for the in channel
769 */
770 static int
artpec6_crypto_setup_in_descr(struct artpec6_crypto_req_common * common,void * buffer,unsigned int len,bool last)771 artpec6_crypto_setup_in_descr(struct artpec6_crypto_req_common *common,
772 void *buffer, unsigned int len, bool last)
773 {
774 dma_addr_t dma_addr;
775 int ret;
776
777 ret = artpec6_crypto_dma_map_single(common, buffer, len,
778 DMA_FROM_DEVICE, &dma_addr);
779 if (ret)
780 return ret;
781
782 return artpec6_crypto_setup_in_descr_phys(common, dma_addr, len, last);
783 }
784
785 static struct artpec6_crypto_bounce_buffer *
artpec6_crypto_alloc_bounce(gfp_t flags)786 artpec6_crypto_alloc_bounce(gfp_t flags)
787 {
788 void *base;
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);
792
793 if (!bbuf)
794 return NULL;
795
796 base = bbuf + 1;
797 bbuf->buf = PTR_ALIGN(base, ARTPEC_CACHE_LINE_MAX);
798 return bbuf;
799 }
800
setup_bounce_buffer_in(struct artpec6_crypto_req_common * common,struct artpec6_crypto_walk * walk,size_t size)801 static int setup_bounce_buffer_in(struct artpec6_crypto_req_common *common,
802 struct artpec6_crypto_walk *walk, size_t size)
803 {
804 struct artpec6_crypto_bounce_buffer *bbuf;
805 int ret;
806
807 bbuf = artpec6_crypto_alloc_bounce(common->gfp_flags);
808 if (!bbuf)
809 return -ENOMEM;
810
811 bbuf->length = size;
812 bbuf->sg = walk->sg;
813 bbuf->offset = walk->offset;
814
815 ret = artpec6_crypto_setup_in_descr(common, bbuf->buf, size, false);
816 if (ret) {
817 kfree(bbuf);
818 return ret;
819 }
820
821 pr_debug("BOUNCE %zu offset %zu\n", size, walk->offset);
822 list_add_tail(&bbuf->list, &common->dma->bounce_buffers);
823 return 0;
824 }
825
826 static int
artpec6_crypto_setup_sg_descrs_in(struct artpec6_crypto_req_common * common,struct artpec6_crypto_walk * walk,size_t count)827 artpec6_crypto_setup_sg_descrs_in(struct artpec6_crypto_req_common *common,
828 struct artpec6_crypto_walk *walk,
829 size_t count)
830 {
831 size_t chunk;
832 int ret;
833 dma_addr_t addr;
834
835 while (walk->sg && count) {
836 chunk = min(count, artpec6_crypto_walk_chunklen(walk));
837 addr = artpec6_crypto_walk_chunk_phys(walk);
838
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.
843 */
844 if (!IS_ALIGNED(addr, ARTPEC_CACHE_LINE_MAX)) {
845 chunk = min_t(dma_addr_t, chunk,
846 ALIGN(addr, ARTPEC_CACHE_LINE_MAX) -
847 addr);
848
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);
854 } else {
855 dma_addr_t dma_addr;
856
857 chunk = chunk & ~(ARTPEC_CACHE_LINE_MAX-1);
858
859 pr_debug("CHUNK %pad:%zu\n", &addr, chunk);
860
861 ret = artpec6_crypto_dma_map_page(common,
862 sg_page(walk->sg),
863 walk->sg->offset +
864 walk->offset,
865 chunk,
866 DMA_FROM_DEVICE,
867 &dma_addr);
868 if (ret)
869 return ret;
870
871 ret = artpec6_crypto_setup_in_descr_phys(common,
872 dma_addr,
873 chunk, false);
874 }
875
876 if (ret)
877 return ret;
878
879 count = count - chunk;
880 artpec6_crypto_walk_advance(walk, chunk);
881 }
882
883 if (count)
884 pr_err("EOL unexpected %zu bytes left\n", count);
885
886 return count ? -EINVAL : 0;
887 }
888
889 static int
artpec6_crypto_setup_sg_descrs_out(struct artpec6_crypto_req_common * common,struct artpec6_crypto_walk * walk,size_t count)890 artpec6_crypto_setup_sg_descrs_out(struct artpec6_crypto_req_common *common,
891 struct artpec6_crypto_walk *walk,
892 size_t count)
893 {
894 size_t chunk;
895 int ret;
896 dma_addr_t addr;
897
898 while (walk->sg && count) {
899 chunk = min(count, artpec6_crypto_walk_chunklen(walk));
900 addr = artpec6_crypto_walk_chunk_phys(walk);
901
902 pr_debug("OUT-CHUNK %pad:%zu\n", &addr, chunk);
903
904 if (addr & 3) {
905 char buf[3];
906
907 chunk = min_t(size_t, chunk, (4-(addr&3)));
908
909 sg_pcopy_to_buffer(walk->sg, 1, buf, chunk,
910 walk->offset);
911
912 ret = artpec6_crypto_setup_out_descr_short(common, buf,
913 chunk,
914 false);
915 } else {
916 dma_addr_t dma_addr;
917
918 ret = artpec6_crypto_dma_map_page(common,
919 sg_page(walk->sg),
920 walk->sg->offset +
921 walk->offset,
922 chunk,
923 DMA_TO_DEVICE,
924 &dma_addr);
925 if (ret)
926 return ret;
927
928 ret = artpec6_crypto_setup_out_descr_phys(common,
929 dma_addr,
930 chunk, false);
931 }
932
933 if (ret)
934 return ret;
935
936 count = count - chunk;
937 artpec6_crypto_walk_advance(walk, chunk);
938 }
939
940 if (count)
941 pr_err("EOL unexpected %zu bytes left\n", count);
942
943 return count ? -EINVAL : 0;
944 }
945
946
947 /** artpec6_crypto_terminate_out_descrs - Set the EOP on the last out descriptor
948 *
949 * If the out descriptor list is non-empty, then the eop flag on the
950 * last used out descriptor will be set.
951 *
952 * @return 0 on success
953 * -EINVAL if the out descriptor is empty or has overflown
954 */
955 static int
artpec6_crypto_terminate_out_descrs(struct artpec6_crypto_req_common * common)956 artpec6_crypto_terminate_out_descrs(struct artpec6_crypto_req_common *common)
957 {
958 struct artpec6_crypto_dma_descriptors *dma = common->dma;
959 struct pdma_descr *d;
960
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");
964 return -EINVAL;
965
966 }
967
968 d = &dma->out[dma->out_cnt-1];
969 d->ctrl.eop = 1;
970
971 return 0;
972 }
973
974 /** artpec6_crypto_terminate_in_descrs - Set the interrupt flag on the last
975 * in descriptor
976 *
977 * See artpec6_crypto_terminate_out_descrs() for return values
978 */
979 static int
artpec6_crypto_terminate_in_descrs(struct artpec6_crypto_req_common * common)980 artpec6_crypto_terminate_in_descrs(struct artpec6_crypto_req_common *common)
981 {
982 struct artpec6_crypto_dma_descriptors *dma = common->dma;
983 struct pdma_descr *d;
984
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");
988 return -EINVAL;
989 }
990
991 d = &dma->in[dma->in_cnt-1];
992 d->ctrl.intr = 1;
993 return 0;
994 }
995
996 /** create_hash_pad - Create a Secure Hash conformant pad
997 *
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
1001 *
1002 * @return The total number of padding bytes written to @dst
1003 */
1004 static size_t
create_hash_pad(int oper,unsigned char * dst,u64 dgstlen,u64 bitcount)1005 create_hash_pad(int oper, unsigned char *dst, u64 dgstlen, u64 bitcount)
1006 {
1007 unsigned int mod, target, diff, pad_bytes, size_bytes;
1008 __be64 bits = __cpu_to_be64(bitcount);
1009
1010 switch (oper) {
1011 case regk_crypto_sha1:
1012 case regk_crypto_sha256:
1013 case regk_crypto_hmac_sha1:
1014 case regk_crypto_hmac_sha256:
1015 target = 448 / 8;
1016 mod = 512 / 8;
1017 size_bytes = 8;
1018 break;
1019 default:
1020 target = 896 / 8;
1021 mod = 1024 / 8;
1022 size_bytes = 16;
1023 break;
1024 }
1025
1026 target -= 1;
1027 diff = dgstlen & (mod - 1);
1028 pad_bytes = diff > target ? target + mod - diff : target - diff;
1029
1030 memset(dst + 1, 0, pad_bytes);
1031 dst[0] = 0x80;
1032
1033 if (size_bytes == 16) {
1034 memset(dst + 1 + pad_bytes, 0, 8);
1035 memcpy(dst + 1 + pad_bytes + 8, &bits, 8);
1036 } else {
1037 memcpy(dst + 1 + pad_bytes, &bits, 8);
1038 }
1039
1040 return pad_bytes + size_bytes + 1;
1041 }
1042
artpec6_crypto_common_init(struct artpec6_crypto_req_common * common,struct crypto_async_request * parent,void (* complete)(struct crypto_async_request * req),struct scatterlist * dstsg,unsigned int nbytes)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)
1047 {
1048 gfp_t flags;
1049 struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
1050
1051 flags = (parent->flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
1052 GFP_KERNEL : GFP_ATOMIC;
1053
1054 common->gfp_flags = flags;
1055 common->dma = kmem_cache_alloc(ac->dma_cache, flags);
1056 if (!common->dma)
1057 return -ENOMEM;
1058
1059 common->req = parent;
1060 common->complete = complete;
1061 return 0;
1062 }
1063
1064 static void
artpec6_crypto_bounce_destroy(struct artpec6_crypto_dma_descriptors * dma)1065 artpec6_crypto_bounce_destroy(struct artpec6_crypto_dma_descriptors *dma)
1066 {
1067 struct artpec6_crypto_bounce_buffer *b;
1068 struct artpec6_crypto_bounce_buffer *next;
1069
1070 list_for_each_entry_safe(b, next, &dma->bounce_buffers, list) {
1071 kfree(b);
1072 }
1073 }
1074
1075 static int
artpec6_crypto_common_destroy(struct artpec6_crypto_req_common * common)1076 artpec6_crypto_common_destroy(struct artpec6_crypto_req_common *common)
1077 {
1078 struct artpec6_crypto *ac = dev_get_drvdata(artpec6_crypto_dev);
1079
1080 artpec6_crypto_dma_unmap_all(common);
1081 artpec6_crypto_bounce_destroy(common->dma);
1082 kmem_cache_free(ac->dma_cache, common->dma);
1083 common->dma = NULL;
1084 return 0;
1085 }
1086
1087 /*
1088 * Ciphering functions.
1089 */
artpec6_crypto_encrypt(struct skcipher_request * req)1090 static int artpec6_crypto_encrypt(struct skcipher_request *req)
1091 {
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);
1096 int ret;
1097
1098 req_ctx = skcipher_request_ctx(req);
1099
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;
1105 break;
1106 default:
1107 break;
1108 }
1109
1110 switch (ctx->crypto_type) {
1111 case ARTPEC6_CRYPTO_CIPHER_AES_CBC:
1112 complete = artpec6_crypto_complete_cbc_encrypt;
1113 break;
1114 default:
1115 complete = artpec6_crypto_complete_crypto;
1116 break;
1117 }
1118
1119 ret = artpec6_crypto_common_init(&req_ctx->common,
1120 &req->base,
1121 complete,
1122 req->dst, req->cryptlen);
1123 if (ret)
1124 return ret;
1125
1126 ret = artpec6_crypto_prepare_crypto(req);
1127 if (ret) {
1128 artpec6_crypto_common_destroy(&req_ctx->common);
1129 return ret;
1130 }
1131
1132 return artpec6_crypto_submit(&req_ctx->common);
1133 }
1134
artpec6_crypto_decrypt(struct skcipher_request * req)1135 static int artpec6_crypto_decrypt(struct skcipher_request *req)
1136 {
1137 int ret;
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);
1142
1143 req_ctx = skcipher_request_ctx(req);
1144
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;
1150 break;
1151 default:
1152 break;
1153 }
1154
1155
1156 switch (ctx->crypto_type) {
1157 case ARTPEC6_CRYPTO_CIPHER_AES_CBC:
1158 complete = artpec6_crypto_complete_cbc_decrypt;
1159 break;
1160 default:
1161 complete = artpec6_crypto_complete_crypto;
1162 break;
1163 }
1164
1165 ret = artpec6_crypto_common_init(&req_ctx->common, &req->base,
1166 complete,
1167 req->dst, req->cryptlen);
1168 if (ret)
1169 return ret;
1170
1171 ret = artpec6_crypto_prepare_crypto(req);
1172 if (ret) {
1173 artpec6_crypto_common_destroy(&req_ctx->common);
1174 return ret;
1175 }
1176
1177 return artpec6_crypto_submit(&req_ctx->common);
1178 }
1179
1180 static int
artpec6_crypto_ctr_crypt(struct skcipher_request * req,bool encrypt)1181 artpec6_crypto_ctr_crypt(struct skcipher_request *req, bool encrypt)
1182 {
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) /
1189 AES_BLOCK_SIZE;
1190
1191 /*
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
1195 * overlow.
1196 */
1197 if (counter + nblks < counter) {
1198 int ret;
1199
1200 pr_debug("counter %x will overflow (nblks %u), falling back\n",
1201 counter, counter + nblks);
1202
1203 ret = crypto_skcipher_setkey(ctx->fallback, ctx->aes_key,
1204 ctx->key_length);
1205 if (ret)
1206 return ret;
1207
1208 {
1209 SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
1210
1211 skcipher_request_set_tfm(subreq, ctx->fallback);
1212 skcipher_request_set_callback(subreq, req->base.flags,
1213 NULL, NULL);
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);
1219 }
1220 return ret;
1221 }
1222
1223 return encrypt ? artpec6_crypto_encrypt(req)
1224 : artpec6_crypto_decrypt(req);
1225 }
1226
artpec6_crypto_ctr_encrypt(struct skcipher_request * req)1227 static int artpec6_crypto_ctr_encrypt(struct skcipher_request *req)
1228 {
1229 return artpec6_crypto_ctr_crypt(req, true);
1230 }
1231
artpec6_crypto_ctr_decrypt(struct skcipher_request * req)1232 static int artpec6_crypto_ctr_decrypt(struct skcipher_request *req)
1233 {
1234 return artpec6_crypto_ctr_crypt(req, false);
1235 }
1236
1237 /*
1238 * AEAD functions
1239 */
artpec6_crypto_aead_init(struct crypto_aead * tfm)1240 static int artpec6_crypto_aead_init(struct crypto_aead *tfm)
1241 {
1242 struct artpec6_cryptotfm_context *tfm_ctx = crypto_aead_ctx(tfm);
1243
1244 memset(tfm_ctx, 0, sizeof(*tfm_ctx));
1245
1246 crypto_aead_set_reqsize(tfm,
1247 sizeof(struct artpec6_crypto_aead_req_ctx));
1248
1249 return 0;
1250 }
1251
artpec6_crypto_aead_set_key(struct crypto_aead * tfm,const u8 * key,unsigned int len)1252 static int artpec6_crypto_aead_set_key(struct crypto_aead *tfm, const u8 *key,
1253 unsigned int len)
1254 {
1255 struct artpec6_cryptotfm_context *ctx = crypto_tfm_ctx(&tfm->base);
1256
1257 if (len != 16 && len != 24 && len != 32) {
1258 crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1259 return -EINVAL;
1260 }
1261
1262 ctx->key_length = len;
1263
1264 memcpy(ctx->aes_key, key, len);
1265 return 0;
1266 }
1267
artpec6_crypto_aead_encrypt(struct aead_request * req)1268 static int artpec6_crypto_aead_encrypt(struct aead_request *req)
1269 {
1270 int ret;
1271 struct artpec6_crypto_aead_req_ctx *req_ctx = aead_request_ctx(req);
1272
1273 req_ctx->decrypt = false;
1274 ret = artpec6_crypto_common_init(&req_ctx->common, &req->base,
1275 artpec6_crypto_complete_aead,
1276 NULL, 0);
1277 if (ret)
1278 return ret;
1279
1280 ret = artpec6_crypto_prepare_aead(req);
1281 if (ret) {
1282 artpec6_crypto_common_destroy(&req_ctx->common);
1283 return ret;
1284 }
1285
1286 return artpec6_crypto_submit(&req_ctx->common);
1287 }
1288
artpec6_crypto_aead_decrypt(struct aead_request * req)1289 static int artpec6_crypto_aead_decrypt(struct aead_request *req)
1290 {
1291 int ret;
1292 struct artpec6_crypto_aead_req_ctx *req_ctx = aead_request_ctx(req);
1293
1294 req_ctx->decrypt = true;
1295 if (req->cryptlen < AES_BLOCK_SIZE)
1296 return -EINVAL;
1297
1298 ret = artpec6_crypto_common_init(&req_ctx->common,
1299 &req->base,
1300 artpec6_crypto_complete_aead,
1301 NULL, 0);
1302 if (ret)
1303 return ret;
1304
1305 ret = artpec6_crypto_prepare_aead(req);
1306 if (ret) {
1307 artpec6_crypto_common_destroy(&req_ctx->common);
1308 return ret;
1309 }
1310
1311 return artpec6_crypto_submit(&req_ctx->common);
1312 }
1313
artpec6_crypto_prepare_hash(struct ahash_request * areq)1314 static int artpec6_crypto_prepare_hash(struct ahash_request *areq)
1315 {
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;
1326 u32 sel_ctx;
1327 bool ext_ctx = false;
1328 bool run_hw = false;
1329 int error = 0;
1330
1331 artpec6_crypto_init_dma_operation(common);
1332
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);
1338 } else {
1339 req_ctx->key_md = FIELD_PREP(A7_CRY_MD_OPER,
1340 a7_regk_crypto_dlkey);
1341 }
1342
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);
1348
1349 error = artpec6_crypto_setup_out_descr(common,
1350 (void *)&req_ctx->key_md,
1351 sizeof(req_ctx->key_md), false, false);
1352 if (error)
1353 return error;
1354
1355 error = artpec6_crypto_setup_out_descr(common,
1356 req_ctx->key_buffer, blocksize,
1357 true, false);
1358 if (error)
1359 return error;
1360 }
1361
1362 if (!(req_ctx->hash_flags & HASH_FLAG_INIT_CTX)) {
1363 /* Restore context */
1364 sel_ctx = regk_crypto_ext;
1365 ext_ctx = true;
1366 } else {
1367 sel_ctx = regk_crypto_init;
1368 }
1369
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);
1373
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;
1377 } else {
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);
1380
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;
1384 }
1385
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);
1390 if (error)
1391 return error;
1392
1393 error = artpec6_crypto_setup_in_descr(common, ac->pad_buffer, 4, false);
1394 if (error)
1395 return error;
1396
1397 if (ext_ctx) {
1398 error = artpec6_crypto_setup_out_descr(common,
1399 req_ctx->digeststate,
1400 contextsize, false, false);
1401
1402 if (error)
1403 return error;
1404 }
1405
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;
1411
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
1416 * the SG lists
1417 */
1418 memcpy(req_ctx->partial_buffer_out,
1419 req_ctx->partial_buffer,
1420 req_ctx->partial_bytes);
1421
1422 error = artpec6_crypto_setup_out_descr(common,
1423 req_ctx->partial_buffer_out,
1424 req_ctx->partial_bytes,
1425 false, true);
1426 if (error)
1427 return error;
1428
1429 /* Reset partial buffer */
1430 done_bytes += req_ctx->partial_bytes;
1431 req_ctx->partial_bytes = 0;
1432 }
1433
1434 artpec6_crypto_walk_init(&walk, areq->src);
1435
1436 error = artpec6_crypto_setup_sg_descrs_out(common, &walk,
1437 ready_bytes -
1438 done_bytes);
1439 if (error)
1440 return error;
1441
1442 if (walk.sg) {
1443 size_t sg_skip = ready_bytes - done_bytes;
1444 size_t sg_rem = areq->nbytes - sg_skip;
1445
1446 sg_pcopy_to_buffer(areq->src, sg_nents(areq->src),
1447 req_ctx->partial_buffer +
1448 req_ctx->partial_bytes,
1449 sg_rem, sg_skip);
1450
1451 req_ctx->partial_bytes += sg_rem;
1452 }
1453
1454 req_ctx->digcnt += ready_bytes;
1455 req_ctx->hash_flags &= ~(HASH_FLAG_UPDATE);
1456 }
1457
1458 /* Finalize */
1459 if (req_ctx->hash_flags & HASH_FLAG_FINALIZE) {
1460 bool needtrim = contextsize != digestsize;
1461 size_t hash_pad_len;
1462 u64 digest_bits;
1463 u32 oper;
1464
1465 if (variant == ARTPEC6_CRYPTO)
1466 oper = FIELD_GET(A6_CRY_MD_OPER, req_ctx->hash_md);
1467 else
1468 oper = FIELD_GET(A7_CRY_MD_OPER, req_ctx->hash_md);
1469
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,
1478 false, true);
1479 if (error)
1480 return error;
1481
1482 req_ctx->digcnt += req_ctx->partial_bytes;
1483 req_ctx->partial_bytes = 0;
1484 }
1485
1486 if (req_ctx->hash_flags & HASH_FLAG_HMAC)
1487 digest_bits = 8 * (req_ctx->digcnt + blocksize);
1488 else
1489 digest_bits = 8 * req_ctx->digcnt;
1490
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,
1497 true);
1498 req_ctx->digcnt = 0;
1499
1500 if (error)
1501 return error;
1502
1503 /* Descriptor for the final result */
1504 error = artpec6_crypto_setup_in_descr(common, areq->result,
1505 digestsize,
1506 !needtrim);
1507 if (error)
1508 return error;
1509
1510 if (needtrim) {
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);
1515 if (error)
1516 return error;
1517 }
1518
1519 } else { /* This is not the final operation for this request */
1520 if (!run_hw)
1521 return ARTPEC6_CRYPTO_PREPARE_HASH_NO_START;
1522
1523 /* Save the result to the context */
1524 error = artpec6_crypto_setup_in_descr(common,
1525 req_ctx->digeststate,
1526 contextsize, false);
1527 if (error)
1528 return error;
1529 /* fall through */
1530 }
1531
1532 req_ctx->hash_flags &= ~(HASH_FLAG_INIT_CTX | HASH_FLAG_UPDATE |
1533 HASH_FLAG_FINALIZE);
1534
1535 error = artpec6_crypto_terminate_in_descrs(common);
1536 if (error)
1537 return error;
1538
1539 error = artpec6_crypto_terminate_out_descrs(common);
1540 if (error)
1541 return error;
1542
1543 error = artpec6_crypto_dma_map_descs(common);
1544 if (error)
1545 return error;
1546
1547 return ARTPEC6_CRYPTO_PREPARE_HASH_START;
1548 }
1549
1550
artpec6_crypto_aes_ecb_init(struct crypto_skcipher * tfm)1551 static int artpec6_crypto_aes_ecb_init(struct crypto_skcipher *tfm)
1552 {
1553 struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
1554
1555 tfm->reqsize = sizeof(struct artpec6_crypto_request_context);
1556 ctx->crypto_type = ARTPEC6_CRYPTO_CIPHER_AES_ECB;
1557
1558 return 0;
1559 }
1560
artpec6_crypto_aes_ctr_init(struct crypto_skcipher * tfm)1561 static int artpec6_crypto_aes_ctr_init(struct crypto_skcipher *tfm)
1562 {
1563 struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
1564
1565 ctx->fallback = crypto_alloc_skcipher(crypto_tfm_alg_name(&tfm->base),
1566 0,
1567 CRYPTO_ALG_ASYNC |
1568 CRYPTO_ALG_NEED_FALLBACK);
1569 if (IS_ERR(ctx->fallback))
1570 return PTR_ERR(ctx->fallback);
1571
1572 tfm->reqsize = sizeof(struct artpec6_crypto_request_context);
1573 ctx->crypto_type = ARTPEC6_CRYPTO_CIPHER_AES_CTR;
1574
1575 return 0;
1576 }
1577
artpec6_crypto_aes_cbc_init(struct crypto_skcipher * tfm)1578 static int artpec6_crypto_aes_cbc_init(struct crypto_skcipher *tfm)
1579 {
1580 struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
1581
1582 tfm->reqsize = sizeof(struct artpec6_crypto_request_context);
1583 ctx->crypto_type = ARTPEC6_CRYPTO_CIPHER_AES_CBC;
1584
1585 return 0;
1586 }
1587
artpec6_crypto_aes_xts_init(struct crypto_skcipher * tfm)1588 static int artpec6_crypto_aes_xts_init(struct crypto_skcipher *tfm)
1589 {
1590 struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
1591
1592 tfm->reqsize = sizeof(struct artpec6_crypto_request_context);
1593 ctx->crypto_type = ARTPEC6_CRYPTO_CIPHER_AES_XTS;
1594
1595 return 0;
1596 }
1597
artpec6_crypto_aes_exit(struct crypto_skcipher * tfm)1598 static void artpec6_crypto_aes_exit(struct crypto_skcipher *tfm)
1599 {
1600 struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
1601
1602 memset(ctx, 0, sizeof(*ctx));
1603 }
1604
artpec6_crypto_aes_ctr_exit(struct crypto_skcipher * tfm)1605 static void artpec6_crypto_aes_ctr_exit(struct crypto_skcipher *tfm)
1606 {
1607 struct artpec6_cryptotfm_context *ctx = crypto_skcipher_ctx(tfm);
1608
1609 crypto_free_skcipher(ctx->fallback);
1610 artpec6_crypto_aes_exit(tfm);
1611 }
1612
1613 static int
artpec6_crypto_cipher_set_key(struct crypto_skcipher * cipher,const u8 * key,unsigned int keylen)1614 artpec6_crypto_cipher_set_key(struct crypto_skcipher *cipher, const u8 *key,
1615 unsigned int keylen)
1616 {
1617 struct artpec6_cryptotfm_context *ctx =
1618 crypto_skcipher_ctx(cipher);
1619
1620 switch (keylen) {
1621 case 16:
1622 case 24:
1623 case 32:
1624 break;
1625 default:
1626 crypto_skcipher_set_flags(cipher,
1627 CRYPTO_TFM_RES_BAD_KEY_LEN);
1628 return -EINVAL;
1629 }
1630
1631 memcpy(ctx->aes_key, key, keylen);
1632 ctx->key_length = keylen;
1633 return 0;
1634 }
1635
1636 static int
artpec6_crypto_xts_set_key(struct crypto_skcipher * cipher,const u8 * key,unsigned int keylen)1637 artpec6_crypto_xts_set_key(struct crypto_skcipher *cipher, const u8 *key,
1638 unsigned int keylen)
1639 {
1640 struct artpec6_cryptotfm_context *ctx =
1641 crypto_skcipher_ctx(cipher);
1642 int ret;
1643
1644 ret = xts_check_key(&cipher->base, key, keylen);
1645 if (ret)
1646 return ret;
1647
1648 switch (keylen) {
1649 case 32:
1650 case 48:
1651 case 64:
1652 break;
1653 default:
1654 crypto_skcipher_set_flags(cipher,
1655 CRYPTO_TFM_RES_BAD_KEY_LEN);
1656 return -EINVAL;
1657 }
1658
1659 memcpy(ctx->aes_key, key, keylen);
1660 ctx->key_length = keylen;
1661 return 0;
1662 }
1663
1664 /** artpec6_crypto_process_crypto - Prepare an async block cipher crypto request
1665 *
1666 * @req: The asynch request to process
1667 *
1668 * @return 0 if the dma job was successfully prepared
1669 * <0 on error
1670 *
1671 * This function sets up the PDMA descriptors for a block cipher request.
1672 *
1673 * The required padding is added for AES-CTR using a statically defined
1674 * buffer.
1675 *
1676 * The PDMA descriptor list will be as follows:
1677 *
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>
1680 *
1681 */
artpec6_crypto_prepare_crypto(struct skcipher_request * areq)1682 static int artpec6_crypto_prepare_crypto(struct skcipher_request *areq)
1683 {
1684 int ret;
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;
1694 size_t cipher_klen;
1695 u32 cipher_len = 0; /* Same as regk_crypto_key_128 for NULL crypto */
1696 u32 oper;
1697
1698 req_ctx = skcipher_request_ctx(areq);
1699 common = &req_ctx->common;
1700
1701 artpec6_crypto_init_dma_operation(common);
1702
1703 if (variant == ARTPEC6_CRYPTO)
1704 ctx->key_md = FIELD_PREP(A6_CRY_MD_OPER, a6_regk_crypto_dlkey);
1705 else
1706 ctx->key_md = FIELD_PREP(A7_CRY_MD_OPER, a7_regk_crypto_dlkey);
1707
1708 ret = artpec6_crypto_setup_out_descr(common, (void *)&ctx->key_md,
1709 sizeof(ctx->key_md), false, false);
1710 if (ret)
1711 return ret;
1712
1713 ret = artpec6_crypto_setup_out_descr(common, ctx->aes_key,
1714 ctx->key_length, true, false);
1715 if (ret)
1716 return ret;
1717
1718 req_ctx->cipher_md = 0;
1719
1720 if (ctx->crypto_type == ARTPEC6_CRYPTO_CIPHER_AES_XTS)
1721 cipher_klen = ctx->key_length/2;
1722 else
1723 cipher_klen = ctx->key_length;
1724
1725 /* Metadata */
1726 switch (cipher_klen) {
1727 case 16:
1728 cipher_len = regk_crypto_key_128;
1729 break;
1730 case 24:
1731 cipher_len = regk_crypto_key_192;
1732 break;
1733 case 32:
1734 cipher_len = regk_crypto_key_256;
1735 break;
1736 default:
1737 pr_err("%s: Invalid key length %d!\n",
1738 MODULE_NAME, ctx->key_length);
1739 return -EINVAL;
1740 }
1741
1742 switch (ctx->crypto_type) {
1743 case ARTPEC6_CRYPTO_CIPHER_AES_ECB:
1744 oper = regk_crypto_aes_ecb;
1745 cipher_decr = req_ctx->decrypt;
1746 break;
1747
1748 case ARTPEC6_CRYPTO_CIPHER_AES_CBC:
1749 oper = regk_crypto_aes_cbc;
1750 cipher_decr = req_ctx->decrypt;
1751 break;
1752
1753 case ARTPEC6_CRYPTO_CIPHER_AES_CTR:
1754 oper = regk_crypto_aes_ctr;
1755 cipher_decr = false;
1756 break;
1757
1758 case ARTPEC6_CRYPTO_CIPHER_AES_XTS:
1759 oper = regk_crypto_aes_xts;
1760 cipher_decr = req_ctx->decrypt;
1761
1762 if (variant == ARTPEC6_CRYPTO)
1763 req_ctx->cipher_md |= A6_CRY_MD_CIPHER_DSEQ;
1764 else
1765 req_ctx->cipher_md |= A7_CRY_MD_CIPHER_DSEQ;
1766 break;
1767
1768 default:
1769 pr_err("%s: Invalid cipher mode %d!\n",
1770 MODULE_NAME, ctx->crypto_type);
1771 return -EINVAL;
1772 }
1773
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,
1777 cipher_len);
1778 if (cipher_decr)
1779 req_ctx->cipher_md |= A6_CRY_MD_CIPHER_DECR;
1780 } else {
1781 req_ctx->cipher_md |= FIELD_PREP(A7_CRY_MD_OPER, oper);
1782 req_ctx->cipher_md |= FIELD_PREP(A7_CRY_MD_CIPHER_LEN,
1783 cipher_len);
1784 if (cipher_decr)
1785 req_ctx->cipher_md |= A7_CRY_MD_CIPHER_DECR;
1786 }
1787
1788 ret = artpec6_crypto_setup_out_descr(common,
1789 &req_ctx->cipher_md,
1790 sizeof(req_ctx->cipher_md),
1791 false, false);
1792 if (ret)
1793 return ret;
1794
1795 ret = artpec6_crypto_setup_in_descr(common, ac->pad_buffer, 4, false);
1796 if (ret)
1797 return ret;
1798
1799 if (iv_len) {
1800 ret = artpec6_crypto_setup_out_descr(common, areq->iv, iv_len,
1801 false, false);
1802 if (ret)
1803 return ret;
1804 }
1805 /* Data out */
1806 artpec6_crypto_walk_init(&walk, areq->src);
1807 ret = artpec6_crypto_setup_sg_descrs_out(common, &walk, areq->cryptlen);
1808 if (ret)
1809 return ret;
1810
1811 /* Data in */
1812 artpec6_crypto_walk_init(&walk, areq->dst);
1813 ret = artpec6_crypto_setup_sg_descrs_in(common, &walk, areq->cryptlen);
1814 if (ret)
1815 return ret;
1816
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) -
1821 areq->cryptlen;
1822
1823 if (pad) {
1824 ret = artpec6_crypto_setup_out_descr(common,
1825 ac->pad_buffer,
1826 pad, false, false);
1827 if (ret)
1828 return ret;
1829
1830 ret = artpec6_crypto_setup_in_descr(common,
1831 ac->pad_buffer, pad,
1832 false);
1833 if (ret)
1834 return ret;
1835 }
1836 }
1837
1838 ret = artpec6_crypto_terminate_out_descrs(common);
1839 if (ret)
1840 return ret;
1841
1842 ret = artpec6_crypto_terminate_in_descrs(common);
1843 if (ret)
1844 return ret;
1845
1846 return artpec6_crypto_dma_map_descs(common);
1847 }
1848
artpec6_crypto_prepare_aead(struct aead_request * areq)1849 static int artpec6_crypto_prepare_aead(struct aead_request *areq)
1850 {
1851 size_t count;
1852 int ret;
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;
1860 u32 md_cipher_len;
1861
1862 artpec6_crypto_init_dma_operation(common);
1863
1864 /* Key */
1865 if (variant == ARTPEC6_CRYPTO) {
1866 ctx->key_md = FIELD_PREP(A6_CRY_MD_OPER,
1867 a6_regk_crypto_dlkey);
1868 } else {
1869 ctx->key_md = FIELD_PREP(A7_CRY_MD_OPER,
1870 a7_regk_crypto_dlkey);
1871 }
1872 ret = artpec6_crypto_setup_out_descr(common, (void *)&ctx->key_md,
1873 sizeof(ctx->key_md), false, false);
1874 if (ret)
1875 return ret;
1876
1877 ret = artpec6_crypto_setup_out_descr(common, ctx->aes_key,
1878 ctx->key_length, true, false);
1879 if (ret)
1880 return ret;
1881
1882 req_ctx->cipher_md = 0;
1883
1884 switch (ctx->key_length) {
1885 case 16:
1886 md_cipher_len = regk_crypto_key_128;
1887 break;
1888 case 24:
1889 md_cipher_len = regk_crypto_key_192;
1890 break;
1891 case 32:
1892 md_cipher_len = regk_crypto_key_256;
1893 break;
1894 default:
1895 return -EINVAL;
1896 }
1897
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,
1902 md_cipher_len);
1903 if (req_ctx->decrypt)
1904 req_ctx->cipher_md |= A6_CRY_MD_CIPHER_DECR;
1905 } else {
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,
1909 md_cipher_len);
1910 if (req_ctx->decrypt)
1911 req_ctx->cipher_md |= A7_CRY_MD_CIPHER_DECR;
1912 }
1913
1914 ret = artpec6_crypto_setup_out_descr(common,
1915 (void *) &req_ctx->cipher_md,
1916 sizeof(req_ctx->cipher_md), false,
1917 false);
1918 if (ret)
1919 return ret;
1920
1921 ret = artpec6_crypto_setup_in_descr(common, ac->pad_buffer, 4, false);
1922 if (ret)
1923 return ret;
1924
1925 /* For the decryption, cryptlen includes the tag. */
1926 input_length = areq->cryptlen;
1927 if (req_ctx->decrypt)
1928 input_length -= AES_BLOCK_SIZE;
1929
1930 /* Prepare the context buffer */
1931 req_ctx->hw_ctx.aad_length_bits =
1932 __cpu_to_be64(8*areq->assoclen);
1933
1934 req_ctx->hw_ctx.text_length_bits =
1935 __cpu_to_be64(8*input_length);
1936
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);
1940
1941 ret = artpec6_crypto_setup_out_descr(common, &req_ctx->hw_ctx,
1942 sizeof(struct artpec6_crypto_aead_hw_ctx), false, false);
1943 if (ret)
1944 return ret;
1945
1946 {
1947 struct artpec6_crypto_walk walk;
1948
1949 artpec6_crypto_walk_init(&walk, areq->src);
1950
1951 /* Associated data */
1952 count = areq->assoclen;
1953 ret = artpec6_crypto_setup_sg_descrs_out(common, &walk, count);
1954 if (ret)
1955 return ret;
1956
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,
1961 ac->zero_buffer,
1962 assoc_pad, false,
1963 false);
1964 if (ret)
1965 return ret;
1966 }
1967
1968 /* Data to crypto */
1969 count = input_length;
1970 ret = artpec6_crypto_setup_sg_descrs_out(common, &walk, count);
1971 if (ret)
1972 return ret;
1973
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,
1978 ac->zero_buffer,
1979 crypto_pad,
1980 false,
1981 false);
1982 if (ret)
1983 return ret;
1984 }
1985 }
1986
1987 /* Data from crypto */
1988 {
1989 struct artpec6_crypto_walk walk;
1990 size_t output_len = areq->cryptlen;
1991
1992 if (req_ctx->decrypt)
1993 output_len -= AES_BLOCK_SIZE;
1994
1995 artpec6_crypto_walk_init(&walk, areq->dst);
1996
1997 /* skip associated data in the output */
1998 count = artpec6_crypto_walk_advance(&walk, areq->assoclen);
1999 if (count)
2000 return -EINVAL;
2001
2002 count = output_len;
2003 ret = artpec6_crypto_setup_sg_descrs_in(common, &walk, count);
2004 if (ret)
2005 return ret;
2006
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);
2010
2011 ret = artpec6_crypto_setup_in_descr(common,
2012 ac->pad_buffer,
2013 crypto_pad, false);
2014 if (ret)
2015 return ret;
2016 }
2017
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.
2021 */
2022 count = AES_BLOCK_SIZE;
2023
2024 if (req_ctx->decrypt) {
2025 ret = artpec6_crypto_setup_in_descr(common,
2026 req_ctx->decryption_tag, count, false);
2027 if (ret)
2028 return ret;
2029
2030 } else {
2031 ret = artpec6_crypto_setup_sg_descrs_in(common, &walk,
2032 count);
2033 if (ret)
2034 return ret;
2035 }
2036
2037 }
2038
2039 ret = artpec6_crypto_terminate_in_descrs(common);
2040 if (ret)
2041 return ret;
2042
2043 ret = artpec6_crypto_terminate_out_descrs(common);
2044 if (ret)
2045 return ret;
2046
2047 return artpec6_crypto_dma_map_descs(common);
2048 }
2049
artpec6_crypto_process_queue(struct artpec6_crypto * ac,struct list_head * completions)2050 static void artpec6_crypto_process_queue(struct artpec6_crypto *ac,
2051 struct list_head *completions)
2052 {
2053 struct artpec6_crypto_req_common *req;
2054
2055 while (!list_empty(&ac->queue) && !artpec6_crypto_busy()) {
2056 req = list_first_entry(&ac->queue,
2057 struct artpec6_crypto_req_common,
2058 list);
2059 list_move_tail(&req->list, &ac->pending);
2060 artpec6_crypto_start_dma(req);
2061
2062 list_add_tail(&req->complete_in_progress, completions);
2063 }
2064
2065 /*
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.
2071 */
2072 if (ac->pending_count)
2073 mod_timer(&ac->timer, jiffies + msecs_to_jiffies(100));
2074 else
2075 del_timer(&ac->timer);
2076 }
2077
artpec6_crypto_timeout(unsigned long data)2078 static void artpec6_crypto_timeout(unsigned long data)
2079 {
2080 struct artpec6_crypto *ac = (struct artpec6_crypto *) data;
2081
2082 dev_info_ratelimited(artpec6_crypto_dev, "timeout\n");
2083
2084 tasklet_schedule(&ac->task);
2085 }
2086
artpec6_crypto_task(unsigned long data)2087 static void artpec6_crypto_task(unsigned long data)
2088 {
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;
2094
2095 INIT_LIST_HEAD(&complete_done);
2096 INIT_LIST_HEAD(&complete_in_progress);
2097
2098 if (list_empty(&ac->pending)) {
2099 pr_debug("Spurious IRQ\n");
2100 return;
2101 }
2102
2103 spin_lock_bh(&ac->queue_lock);
2104
2105 list_for_each_entry_safe(req, n, &ac->pending, list) {
2106 struct artpec6_crypto_dma_descriptors *dma = req->dma;
2107 u32 stat;
2108
2109 dma_sync_single_for_cpu(artpec6_crypto_dev, dma->stat_dma_addr,
2110 sizeof(dma->stat[0]),
2111 DMA_BIDIRECTIONAL);
2112
2113 stat = req->dma->stat[req->dma->in_cnt-1];
2114
2115 /* A non-zero final status descriptor indicates
2116 * this job has finished.
2117 */
2118 pr_debug("Request %p status is %X\n", req, stat);
2119 if (!stat)
2120 break;
2121
2122 /* Allow testing of timeout handling with fault injection */
2123 #ifdef CONFIG_FAULT_INJECTION
2124 if (should_fail(&artpec6_crypto_fail_status_read, 1))
2125 continue;
2126 #endif
2127
2128 pr_debug("Completing request %p\n", req);
2129
2130 list_move_tail(&req->list, &complete_done);
2131
2132 artpec6_crypto_dma_unmap_all(req);
2133 artpec6_crypto_copy_bounce_buffers(req);
2134
2135 ac->pending_count--;
2136 artpec6_crypto_common_destroy(req);
2137 }
2138
2139 artpec6_crypto_process_queue(ac, &complete_in_progress);
2140
2141 spin_unlock_bh(&ac->queue_lock);
2142
2143 /* Perform the completion callbacks without holding the queue lock
2144 * to allow new request submissions from the callbacks.
2145 */
2146 list_for_each_entry_safe(req, n, &complete_done, list) {
2147 req->complete(req->req);
2148 }
2149
2150 list_for_each_entry_safe(req, n, &complete_in_progress,
2151 complete_in_progress) {
2152 req->req->complete(req->req, -EINPROGRESS);
2153 }
2154 }
2155
artpec6_crypto_complete_crypto(struct crypto_async_request * req)2156 static void artpec6_crypto_complete_crypto(struct crypto_async_request *req)
2157 {
2158 req->complete(req, 0);
2159 }
2160
2161 static void
artpec6_crypto_complete_cbc_decrypt(struct crypto_async_request * req)2162 artpec6_crypto_complete_cbc_decrypt(struct crypto_async_request *req)
2163 {
2164 struct skcipher_request *cipher_req = container_of(req,
2165 struct skcipher_request, base);
2166
2167 scatterwalk_map_and_copy(cipher_req->iv, cipher_req->src,
2168 cipher_req->cryptlen - AES_BLOCK_SIZE,
2169 AES_BLOCK_SIZE, 0);
2170 req->complete(req, 0);
2171 }
2172
2173 static void
artpec6_crypto_complete_cbc_encrypt(struct crypto_async_request * req)2174 artpec6_crypto_complete_cbc_encrypt(struct crypto_async_request *req)
2175 {
2176 struct skcipher_request *cipher_req = container_of(req,
2177 struct skcipher_request, base);
2178
2179 scatterwalk_map_and_copy(cipher_req->iv, cipher_req->dst,
2180 cipher_req->cryptlen - AES_BLOCK_SIZE,
2181 AES_BLOCK_SIZE, 0);
2182 req->complete(req, 0);
2183 }
2184
artpec6_crypto_complete_aead(struct crypto_async_request * req)2185 static void artpec6_crypto_complete_aead(struct crypto_async_request *req)
2186 {
2187 int result = 0;
2188
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);
2193
2194 if (req_ctx->decrypt) {
2195 u8 input_tag[AES_BLOCK_SIZE];
2196
2197 sg_pcopy_to_buffer(areq->src,
2198 sg_nents(areq->src),
2199 input_tag,
2200 AES_BLOCK_SIZE,
2201 areq->assoclen + areq->cryptlen -
2202 AES_BLOCK_SIZE);
2203
2204 if (memcmp(req_ctx->decryption_tag,
2205 input_tag,
2206 AES_BLOCK_SIZE)) {
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);
2213
2214 result = -EBADMSG;
2215 }
2216 }
2217
2218 req->complete(req, result);
2219 }
2220
artpec6_crypto_complete_hash(struct crypto_async_request * req)2221 static void artpec6_crypto_complete_hash(struct crypto_async_request *req)
2222 {
2223 req->complete(req, 0);
2224 }
2225
2226
2227 /*------------------- Hash functions -----------------------------------------*/
2228 static int
artpec6_crypto_hash_set_key(struct crypto_ahash * tfm,const u8 * key,unsigned int keylen)2229 artpec6_crypto_hash_set_key(struct crypto_ahash *tfm,
2230 const u8 *key, unsigned int keylen)
2231 {
2232 struct artpec6_hashalg_context *tfm_ctx = crypto_tfm_ctx(&tfm->base);
2233 size_t blocksize;
2234 int ret;
2235
2236 if (!keylen) {
2237 pr_err("Invalid length (%d) of HMAC key\n",
2238 keylen);
2239 return -EINVAL;
2240 }
2241
2242 memset(tfm_ctx->hmac_key, 0, sizeof(tfm_ctx->hmac_key));
2243
2244 blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2245
2246 if (keylen > blocksize) {
2247 SHASH_DESC_ON_STACK(hdesc, tfm_ctx->child_hash);
2248
2249 hdesc->tfm = tfm_ctx->child_hash;
2250 hdesc->flags = crypto_ahash_get_flags(tfm) &
2251 CRYPTO_TFM_REQ_MAY_SLEEP;
2252
2253 tfm_ctx->hmac_key_length = blocksize;
2254 ret = crypto_shash_digest(hdesc, key, keylen,
2255 tfm_ctx->hmac_key);
2256 if (ret)
2257 return ret;
2258
2259 } else {
2260 memcpy(tfm_ctx->hmac_key, key, keylen);
2261 tfm_ctx->hmac_key_length = keylen;
2262 }
2263
2264 return 0;
2265 }
2266
2267 static int
artpec6_crypto_init_hash(struct ahash_request * req,u8 type,int hmac)2268 artpec6_crypto_init_hash(struct ahash_request *req, u8 type, int hmac)
2269 {
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);
2273 u32 oper;
2274
2275 memset(req_ctx, 0, sizeof(*req_ctx));
2276
2277 req_ctx->hash_flags = HASH_FLAG_INIT_CTX;
2278 if (hmac)
2279 req_ctx->hash_flags |= (HASH_FLAG_HMAC | HASH_FLAG_UPDATE_KEY);
2280
2281 switch (type) {
2282 case ARTPEC6_CRYPTO_HASH_SHA1:
2283 oper = hmac ? regk_crypto_hmac_sha1 : regk_crypto_sha1;
2284 break;
2285 case ARTPEC6_CRYPTO_HASH_SHA256:
2286 oper = hmac ? regk_crypto_hmac_sha256 : regk_crypto_sha256;
2287 break;
2288 case ARTPEC6_CRYPTO_HASH_SHA384:
2289 oper = hmac ? regk_crypto_hmac_sha384 : regk_crypto_sha384;
2290 break;
2291 case ARTPEC6_CRYPTO_HASH_SHA512:
2292 oper = hmac ? regk_crypto_hmac_sha512 : regk_crypto_sha512;
2293 break;
2294
2295 default:
2296 pr_err("%s: Unsupported hash type 0x%x\n", MODULE_NAME, type);
2297 return -EINVAL;
2298 }
2299
2300 if (variant == ARTPEC6_CRYPTO)
2301 req_ctx->hash_md = FIELD_PREP(A6_CRY_MD_OPER, oper);
2302 else
2303 req_ctx->hash_md = FIELD_PREP(A7_CRY_MD_OPER, oper);
2304
2305 return 0;
2306 }
2307
artpec6_crypto_prepare_submit_hash(struct ahash_request * req)2308 static int artpec6_crypto_prepare_submit_hash(struct ahash_request *req)
2309 {
2310 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2311 int ret;
2312
2313 if (!req_ctx->common.dma) {
2314 ret = artpec6_crypto_common_init(&req_ctx->common,
2315 &req->base,
2316 artpec6_crypto_complete_hash,
2317 NULL, 0);
2318
2319 if (ret)
2320 return ret;
2321 }
2322
2323 ret = artpec6_crypto_prepare_hash(req);
2324 switch (ret) {
2325 case ARTPEC6_CRYPTO_PREPARE_HASH_START:
2326 ret = artpec6_crypto_submit(&req_ctx->common);
2327 break;
2328
2329 case ARTPEC6_CRYPTO_PREPARE_HASH_NO_START:
2330 ret = 0;
2331 /* Fallthrough */
2332
2333 default:
2334 artpec6_crypto_common_destroy(&req_ctx->common);
2335 break;
2336 }
2337
2338 return ret;
2339 }
2340
artpec6_crypto_hash_final(struct ahash_request * req)2341 static int artpec6_crypto_hash_final(struct ahash_request *req)
2342 {
2343 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2344
2345 req_ctx->hash_flags |= HASH_FLAG_FINALIZE;
2346
2347 return artpec6_crypto_prepare_submit_hash(req);
2348 }
2349
artpec6_crypto_hash_update(struct ahash_request * req)2350 static int artpec6_crypto_hash_update(struct ahash_request *req)
2351 {
2352 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2353
2354 req_ctx->hash_flags |= HASH_FLAG_UPDATE;
2355
2356 return artpec6_crypto_prepare_submit_hash(req);
2357 }
2358
artpec6_crypto_sha1_init(struct ahash_request * req)2359 static int artpec6_crypto_sha1_init(struct ahash_request *req)
2360 {
2361 return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA1, 0);
2362 }
2363
artpec6_crypto_sha1_digest(struct ahash_request * req)2364 static int artpec6_crypto_sha1_digest(struct ahash_request *req)
2365 {
2366 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2367
2368 artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA1, 0);
2369
2370 req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
2371
2372 return artpec6_crypto_prepare_submit_hash(req);
2373 }
2374
artpec6_crypto_sha256_init(struct ahash_request * req)2375 static int artpec6_crypto_sha256_init(struct ahash_request *req)
2376 {
2377 return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA256, 0);
2378 }
2379
artpec6_crypto_sha256_digest(struct ahash_request * req)2380 static int artpec6_crypto_sha256_digest(struct ahash_request *req)
2381 {
2382 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2383
2384 artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA256, 0);
2385 req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
2386
2387 return artpec6_crypto_prepare_submit_hash(req);
2388 }
2389
artpec6_crypto_sha384_init(struct ahash_request * req)2390 static int __maybe_unused artpec6_crypto_sha384_init(struct ahash_request *req)
2391 {
2392 return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA384, 0);
2393 }
2394
2395 static int __maybe_unused
artpec6_crypto_sha384_digest(struct ahash_request * req)2396 artpec6_crypto_sha384_digest(struct ahash_request *req)
2397 {
2398 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2399
2400 artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA384, 0);
2401 req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
2402
2403 return artpec6_crypto_prepare_submit_hash(req);
2404 }
2405
artpec6_crypto_sha512_init(struct ahash_request * req)2406 static int artpec6_crypto_sha512_init(struct ahash_request *req)
2407 {
2408 return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA512, 0);
2409 }
2410
artpec6_crypto_sha512_digest(struct ahash_request * req)2411 static int artpec6_crypto_sha512_digest(struct ahash_request *req)
2412 {
2413 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2414
2415 artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA512, 0);
2416 req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
2417
2418 return artpec6_crypto_prepare_submit_hash(req);
2419 }
2420
artpec6_crypto_hmac_sha256_init(struct ahash_request * req)2421 static int artpec6_crypto_hmac_sha256_init(struct ahash_request *req)
2422 {
2423 return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA256, 1);
2424 }
2425
2426 static int __maybe_unused
artpec6_crypto_hmac_sha384_init(struct ahash_request * req)2427 artpec6_crypto_hmac_sha384_init(struct ahash_request *req)
2428 {
2429 return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA384, 1);
2430 }
2431
artpec6_crypto_hmac_sha512_init(struct ahash_request * req)2432 static int artpec6_crypto_hmac_sha512_init(struct ahash_request *req)
2433 {
2434 return artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA512, 1);
2435 }
2436
artpec6_crypto_hmac_sha256_digest(struct ahash_request * req)2437 static int artpec6_crypto_hmac_sha256_digest(struct ahash_request *req)
2438 {
2439 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2440
2441 artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA256, 1);
2442 req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
2443
2444 return artpec6_crypto_prepare_submit_hash(req);
2445 }
2446
2447 static int __maybe_unused
artpec6_crypto_hmac_sha384_digest(struct ahash_request * req)2448 artpec6_crypto_hmac_sha384_digest(struct ahash_request *req)
2449 {
2450 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2451
2452 artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA384, 1);
2453 req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
2454
2455 return artpec6_crypto_prepare_submit_hash(req);
2456 }
2457
artpec6_crypto_hmac_sha512_digest(struct ahash_request * req)2458 static int artpec6_crypto_hmac_sha512_digest(struct ahash_request *req)
2459 {
2460 struct artpec6_hash_request_context *req_ctx = ahash_request_ctx(req);
2461
2462 artpec6_crypto_init_hash(req, ARTPEC6_CRYPTO_HASH_SHA512, 1);
2463 req_ctx->hash_flags |= HASH_FLAG_UPDATE | HASH_FLAG_FINALIZE;
2464
2465 return artpec6_crypto_prepare_submit_hash(req);
2466 }
2467
artpec6_crypto_ahash_init_common(struct crypto_tfm * tfm,const char * base_hash_name)2468 static int artpec6_crypto_ahash_init_common(struct crypto_tfm *tfm,
2469 const char *base_hash_name)
2470 {
2471 struct artpec6_hashalg_context *tfm_ctx = crypto_tfm_ctx(tfm);
2472
2473 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2474 sizeof(struct artpec6_hash_request_context));
2475 memset(tfm_ctx, 0, sizeof(*tfm_ctx));
2476
2477 if (base_hash_name) {
2478 struct crypto_shash *child;
2479
2480 child = crypto_alloc_shash(base_hash_name, 0,
2481 CRYPTO_ALG_NEED_FALLBACK);
2482
2483 if (IS_ERR(child))
2484 return PTR_ERR(child);
2485
2486 tfm_ctx->child_hash = child;
2487 }
2488
2489 return 0;
2490 }
2491
artpec6_crypto_ahash_init(struct crypto_tfm * tfm)2492 static int artpec6_crypto_ahash_init(struct crypto_tfm *tfm)
2493 {
2494 return artpec6_crypto_ahash_init_common(tfm, NULL);
2495 }
2496
artpec6_crypto_ahash_init_hmac_sha256(struct crypto_tfm * tfm)2497 static int artpec6_crypto_ahash_init_hmac_sha256(struct crypto_tfm *tfm)
2498 {
2499 return artpec6_crypto_ahash_init_common(tfm, "sha256");
2500 }
2501
2502 static int __maybe_unused
artpec6_crypto_ahash_init_hmac_sha384(struct crypto_tfm * tfm)2503 artpec6_crypto_ahash_init_hmac_sha384(struct crypto_tfm *tfm)
2504 {
2505 return artpec6_crypto_ahash_init_common(tfm, "sha384");
2506 }
2507
artpec6_crypto_ahash_init_hmac_sha512(struct crypto_tfm * tfm)2508 static int artpec6_crypto_ahash_init_hmac_sha512(struct crypto_tfm *tfm)
2509 {
2510 return artpec6_crypto_ahash_init_common(tfm, "sha512");
2511 }
2512
artpec6_crypto_ahash_exit(struct crypto_tfm * tfm)2513 static void artpec6_crypto_ahash_exit(struct crypto_tfm *tfm)
2514 {
2515 struct artpec6_hashalg_context *tfm_ctx = crypto_tfm_ctx(tfm);
2516
2517 if (tfm_ctx->child_hash)
2518 crypto_free_shash(tfm_ctx->child_hash);
2519
2520 memset(tfm_ctx->hmac_key, 0, sizeof(tfm_ctx->hmac_key));
2521 tfm_ctx->hmac_key_length = 0;
2522 }
2523
artpec6_crypto_hash_export(struct ahash_request * req,void * out)2524 static int artpec6_crypto_hash_export(struct ahash_request *req, void *out)
2525 {
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;
2530
2531 BUILD_BUG_ON(sizeof(state->partial_buffer) !=
2532 sizeof(ctx->partial_buffer));
2533 BUILD_BUG_ON(sizeof(state->digeststate) != sizeof(ctx->digeststate));
2534
2535 state->digcnt = ctx->digcnt;
2536 state->partial_bytes = ctx->partial_bytes;
2537 state->hash_flags = ctx->hash_flags;
2538
2539 if (variant == ARTPEC6_CRYPTO)
2540 state->oper = FIELD_GET(A6_CRY_MD_OPER, ctx->hash_md);
2541 else
2542 state->oper = FIELD_GET(A7_CRY_MD_OPER, ctx->hash_md);
2543
2544 memcpy(state->partial_buffer, ctx->partial_buffer,
2545 sizeof(state->partial_buffer));
2546 memcpy(state->digeststate, ctx->digeststate,
2547 sizeof(state->digeststate));
2548
2549 return 0;
2550 }
2551
artpec6_crypto_hash_import(struct ahash_request * req,const void * in)2552 static int artpec6_crypto_hash_import(struct ahash_request *req, const void *in)
2553 {
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;
2558
2559 memset(ctx, 0, sizeof(*ctx));
2560
2561 ctx->digcnt = state->digcnt;
2562 ctx->partial_bytes = state->partial_bytes;
2563 ctx->hash_flags = state->hash_flags;
2564
2565 if (variant == ARTPEC6_CRYPTO)
2566 ctx->hash_md = FIELD_PREP(A6_CRY_MD_OPER, state->oper);
2567 else
2568 ctx->hash_md = FIELD_PREP(A7_CRY_MD_OPER, state->oper);
2569
2570 memcpy(ctx->partial_buffer, state->partial_buffer,
2571 sizeof(state->partial_buffer));
2572 memcpy(ctx->digeststate, state->digeststate,
2573 sizeof(state->digeststate));
2574
2575 return 0;
2576 }
2577
init_crypto_hw(struct artpec6_crypto * ac)2578 static int init_crypto_hw(struct artpec6_crypto *ac)
2579 {
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;
2587 u32 in, out;
2588
2589 /*
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.
2595 */
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 */
2601
2602 BUILD_BUG_ON_MSG((out_data_buf_size
2603 + out_descr_buf_size) * 64 > 1984,
2604 "Invalid OUT configuration");
2605
2606 BUILD_BUG_ON_MSG((in_data_buf_size
2607 + in_descr_buf_size
2608 + in_stat_buf_size) * 64 > 1024,
2609 "Invalid IN configuration");
2610
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);
2614
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);
2617
2618 writel_relaxed(out, base + PDMA_OUT_BUF_CFG);
2619 writel_relaxed(PDMA_OUT_CFG_EN, base + PDMA_OUT_CFG);
2620
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);
2627 } else {
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);
2633 }
2634
2635 return 0;
2636 }
2637
artpec6_crypto_disable_hw(struct artpec6_crypto * ac)2638 static void artpec6_crypto_disable_hw(struct artpec6_crypto *ac)
2639 {
2640 enum artpec6_crypto_variant variant = ac->variant;
2641 void __iomem *base = ac->base;
2642
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);
2647 } else {
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);
2651 }
2652
2653 writel_relaxed(0, base + PDMA_OUT_CFG);
2654
2655 }
2656
artpec6_crypto_irq(int irq,void * dev_id)2657 static irqreturn_t artpec6_crypto_irq(int irq, void *dev_id)
2658 {
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;
2664 u32 ack_intr_reg;
2665 u32 ack = 0;
2666 u32 intr;
2667
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;
2675 } else {
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;
2682 }
2683
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.
2690 */
2691 if (intr & mask_in_data)
2692 ack |= mask_in_data;
2693
2694 if (intr & mask_in_eop_flush)
2695 ack |= mask_in_eop_flush;
2696 else
2697 writel_relaxed(in_cmd_flush_stat, base + in_cmd_reg);
2698
2699 writel_relaxed(ack, base + ack_intr_reg);
2700
2701 if (intr & mask_in_eop_flush)
2702 tasklet_schedule(&ac->task);
2703
2704 return IRQ_HANDLED;
2705 }
2706
2707 /*------------------- Algorithm definitions ----------------------------------*/
2708
2709 /* Hashes */
2710 static struct ahash_alg hash_algos[] = {
2711 /* SHA-1 */
2712 {
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),
2721 .halg.base = {
2722 .cra_name = "sha1",
2723 .cra_driver_name = "artpec-sha1",
2724 .cra_priority = 300,
2725 .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
2726 .cra_blocksize = SHA1_BLOCK_SIZE,
2727 .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
2728 .cra_alignmask = 3,
2729 .cra_module = THIS_MODULE,
2730 .cra_init = artpec6_crypto_ahash_init,
2731 .cra_exit = artpec6_crypto_ahash_exit,
2732 }
2733 },
2734 /* SHA-256 */
2735 {
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),
2744 .halg.base = {
2745 .cra_name = "sha256",
2746 .cra_driver_name = "artpec-sha256",
2747 .cra_priority = 300,
2748 .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
2749 .cra_blocksize = SHA256_BLOCK_SIZE,
2750 .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
2751 .cra_alignmask = 3,
2752 .cra_module = THIS_MODULE,
2753 .cra_init = artpec6_crypto_ahash_init,
2754 .cra_exit = artpec6_crypto_ahash_exit,
2755 }
2756 },
2757 /* HMAC SHA-256 */
2758 {
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),
2768 .halg.base = {
2769 .cra_name = "hmac(sha256)",
2770 .cra_driver_name = "artpec-hmac-sha256",
2771 .cra_priority = 300,
2772 .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
2773 .cra_blocksize = SHA256_BLOCK_SIZE,
2774 .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
2775 .cra_alignmask = 3,
2776 .cra_module = THIS_MODULE,
2777 .cra_init = artpec6_crypto_ahash_init_hmac_sha256,
2778 .cra_exit = artpec6_crypto_ahash_exit,
2779 }
2780 },
2781 };
2782
2783 static struct ahash_alg artpec7_hash_algos[] = {
2784 /* SHA-384 */
2785 {
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),
2794 .halg.base = {
2795 .cra_name = "sha384",
2796 .cra_driver_name = "artpec-sha384",
2797 .cra_priority = 300,
2798 .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
2799 .cra_blocksize = SHA384_BLOCK_SIZE,
2800 .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
2801 .cra_alignmask = 3,
2802 .cra_module = THIS_MODULE,
2803 .cra_init = artpec6_crypto_ahash_init,
2804 .cra_exit = artpec6_crypto_ahash_exit,
2805 }
2806 },
2807 /* HMAC SHA-384 */
2808 {
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),
2818 .halg.base = {
2819 .cra_name = "hmac(sha384)",
2820 .cra_driver_name = "artpec-hmac-sha384",
2821 .cra_priority = 300,
2822 .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
2823 .cra_blocksize = SHA384_BLOCK_SIZE,
2824 .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
2825 .cra_alignmask = 3,
2826 .cra_module = THIS_MODULE,
2827 .cra_init = artpec6_crypto_ahash_init_hmac_sha384,
2828 .cra_exit = artpec6_crypto_ahash_exit,
2829 }
2830 },
2831 /* SHA-512 */
2832 {
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),
2841 .halg.base = {
2842 .cra_name = "sha512",
2843 .cra_driver_name = "artpec-sha512",
2844 .cra_priority = 300,
2845 .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
2846 .cra_blocksize = SHA512_BLOCK_SIZE,
2847 .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
2848 .cra_alignmask = 3,
2849 .cra_module = THIS_MODULE,
2850 .cra_init = artpec6_crypto_ahash_init,
2851 .cra_exit = artpec6_crypto_ahash_exit,
2852 }
2853 },
2854 /* HMAC SHA-512 */
2855 {
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),
2865 .halg.base = {
2866 .cra_name = "hmac(sha512)",
2867 .cra_driver_name = "artpec-hmac-sha512",
2868 .cra_priority = 300,
2869 .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
2870 .cra_blocksize = SHA512_BLOCK_SIZE,
2871 .cra_ctxsize = sizeof(struct artpec6_hashalg_context),
2872 .cra_alignmask = 3,
2873 .cra_module = THIS_MODULE,
2874 .cra_init = artpec6_crypto_ahash_init_hmac_sha512,
2875 .cra_exit = artpec6_crypto_ahash_exit,
2876 }
2877 },
2878 };
2879
2880 /* Crypto */
2881 static struct skcipher_alg crypto_algos[] = {
2882 /* AES - ECB */
2883 {
2884 .base = {
2885 .cra_name = "ecb(aes)",
2886 .cra_driver_name = "artpec6-ecb-aes",
2887 .cra_priority = 300,
2888 .cra_flags = CRYPTO_ALG_TYPE_SKCIPHER |
2889 CRYPTO_ALG_ASYNC,
2890 .cra_blocksize = AES_BLOCK_SIZE,
2891 .cra_ctxsize = sizeof(struct artpec6_cryptotfm_context),
2892 .cra_alignmask = 3,
2893 .cra_module = THIS_MODULE,
2894 },
2895 .min_keysize = AES_MIN_KEY_SIZE,
2896 .max_keysize = AES_MAX_KEY_SIZE,
2897 .setkey = artpec6_crypto_cipher_set_key,
2898 .encrypt = artpec6_crypto_encrypt,
2899 .decrypt = artpec6_crypto_decrypt,
2900 .init = artpec6_crypto_aes_ecb_init,
2901 .exit = artpec6_crypto_aes_exit,
2902 },
2903 /* AES - CTR */
2904 {
2905 .base = {
2906 .cra_name = "ctr(aes)",
2907 .cra_driver_name = "artpec6-ctr-aes",
2908 .cra_priority = 300,
2909 .cra_flags = CRYPTO_ALG_TYPE_SKCIPHER |
2910 CRYPTO_ALG_ASYNC |
2911 CRYPTO_ALG_NEED_FALLBACK,
2912 .cra_blocksize = 1,
2913 .cra_ctxsize = sizeof(struct artpec6_cryptotfm_context),
2914 .cra_alignmask = 3,
2915 .cra_module = THIS_MODULE,
2916 },
2917 .min_keysize = AES_MIN_KEY_SIZE,
2918 .max_keysize = AES_MAX_KEY_SIZE,
2919 .ivsize = AES_BLOCK_SIZE,
2920 .setkey = artpec6_crypto_cipher_set_key,
2921 .encrypt = artpec6_crypto_ctr_encrypt,
2922 .decrypt = artpec6_crypto_ctr_decrypt,
2923 .init = artpec6_crypto_aes_ctr_init,
2924 .exit = artpec6_crypto_aes_ctr_exit,
2925 },
2926 /* AES - CBC */
2927 {
2928 .base = {
2929 .cra_name = "cbc(aes)",
2930 .cra_driver_name = "artpec6-cbc-aes",
2931 .cra_priority = 300,
2932 .cra_flags = CRYPTO_ALG_TYPE_SKCIPHER |
2933 CRYPTO_ALG_ASYNC,
2934 .cra_blocksize = AES_BLOCK_SIZE,
2935 .cra_ctxsize = sizeof(struct artpec6_cryptotfm_context),
2936 .cra_alignmask = 3,
2937 .cra_module = THIS_MODULE,
2938 },
2939 .min_keysize = AES_MIN_KEY_SIZE,
2940 .max_keysize = AES_MAX_KEY_SIZE,
2941 .ivsize = AES_BLOCK_SIZE,
2942 .setkey = artpec6_crypto_cipher_set_key,
2943 .encrypt = artpec6_crypto_encrypt,
2944 .decrypt = artpec6_crypto_decrypt,
2945 .init = artpec6_crypto_aes_cbc_init,
2946 .exit = artpec6_crypto_aes_exit
2947 },
2948 /* AES - XTS */
2949 {
2950 .base = {
2951 .cra_name = "xts(aes)",
2952 .cra_driver_name = "artpec6-xts-aes",
2953 .cra_priority = 300,
2954 .cra_flags = CRYPTO_ALG_TYPE_SKCIPHER |
2955 CRYPTO_ALG_ASYNC,
2956 .cra_blocksize = 1,
2957 .cra_ctxsize = sizeof(struct artpec6_cryptotfm_context),
2958 .cra_alignmask = 3,
2959 .cra_module = THIS_MODULE,
2960 },
2961 .min_keysize = 2*AES_MIN_KEY_SIZE,
2962 .max_keysize = 2*AES_MAX_KEY_SIZE,
2963 .ivsize = 16,
2964 .setkey = artpec6_crypto_xts_set_key,
2965 .encrypt = artpec6_crypto_encrypt,
2966 .decrypt = artpec6_crypto_decrypt,
2967 .init = artpec6_crypto_aes_xts_init,
2968 .exit = artpec6_crypto_aes_exit,
2969 },
2970 };
2971
2972 static struct aead_alg aead_algos[] = {
2973 {
2974 .init = artpec6_crypto_aead_init,
2975 .setkey = artpec6_crypto_aead_set_key,
2976 .encrypt = artpec6_crypto_aead_encrypt,
2977 .decrypt = artpec6_crypto_aead_decrypt,
2978 .ivsize = GCM_AES_IV_SIZE,
2979 .maxauthsize = AES_BLOCK_SIZE,
2980
2981 .base = {
2982 .cra_name = "gcm(aes)",
2983 .cra_driver_name = "artpec-gcm-aes",
2984 .cra_priority = 300,
2985 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC |
2986 CRYPTO_ALG_KERN_DRIVER_ONLY,
2987 .cra_blocksize = 1,
2988 .cra_ctxsize = sizeof(struct artpec6_cryptotfm_context),
2989 .cra_alignmask = 3,
2990 .cra_module = THIS_MODULE,
2991 },
2992 }
2993 };
2994
2995 #ifdef CONFIG_DEBUG_FS
2996
2997 struct dbgfs_u32 {
2998 char *name;
2999 mode_t mode;
3000 u32 *flag;
3001 char *desc;
3002 };
3003
3004 static struct dentry *dbgfs_root;
3005
artpec6_crypto_init_debugfs(void)3006 static void artpec6_crypto_init_debugfs(void)
3007 {
3008 dbgfs_root = debugfs_create_dir("artpec6_crypto", NULL);
3009
3010 if (!dbgfs_root || IS_ERR(dbgfs_root)) {
3011 dbgfs_root = NULL;
3012 pr_err("%s: Could not initialise debugfs!\n", MODULE_NAME);
3013 return;
3014 }
3015
3016 #ifdef CONFIG_FAULT_INJECTION
3017 fault_create_debugfs_attr("fail_status_read", dbgfs_root,
3018 &artpec6_crypto_fail_status_read);
3019
3020 fault_create_debugfs_attr("fail_dma_array_full", dbgfs_root,
3021 &artpec6_crypto_fail_dma_array_full);
3022 #endif
3023 }
3024
artpec6_crypto_free_debugfs(void)3025 static void artpec6_crypto_free_debugfs(void)
3026 {
3027 if (!dbgfs_root)
3028 return;
3029
3030 debugfs_remove_recursive(dbgfs_root);
3031 dbgfs_root = NULL;
3032 }
3033 #endif
3034
3035 static const struct of_device_id artpec6_crypto_of_match[] = {
3036 { .compatible = "axis,artpec6-crypto", .data = (void *)ARTPEC6_CRYPTO },
3037 { .compatible = "axis,artpec7-crypto", .data = (void *)ARTPEC7_CRYPTO },
3038 {}
3039 };
3040 MODULE_DEVICE_TABLE(of, artpec6_crypto_of_match);
3041
artpec6_crypto_probe(struct platform_device * pdev)3042 static int artpec6_crypto_probe(struct platform_device *pdev)
3043 {
3044 const struct of_device_id *match;
3045 enum artpec6_crypto_variant variant;
3046 struct artpec6_crypto *ac;
3047 struct device *dev = &pdev->dev;
3048 void __iomem *base;
3049 struct resource *res;
3050 int irq;
3051 int err;
3052
3053 if (artpec6_crypto_dev)
3054 return -ENODEV;
3055
3056 match = of_match_node(artpec6_crypto_of_match, dev->of_node);
3057 if (!match)
3058 return -EINVAL;
3059
3060 variant = (enum artpec6_crypto_variant)match->data;
3061
3062 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3063 if (!res)
3064 return -ENODEV;
3065
3066 base = devm_ioremap_resource(&pdev->dev, res);
3067 if (IS_ERR(base))
3068 return PTR_ERR(base);
3069
3070 irq = platform_get_irq(pdev, 0);
3071 if (irq < 0)
3072 return -ENODEV;
3073
3074 ac = devm_kzalloc(&pdev->dev, sizeof(struct artpec6_crypto),
3075 GFP_KERNEL);
3076 if (!ac)
3077 return -ENOMEM;
3078
3079 platform_set_drvdata(pdev, ac);
3080 ac->variant = variant;
3081
3082 spin_lock_init(&ac->queue_lock);
3083 INIT_LIST_HEAD(&ac->queue);
3084 INIT_LIST_HEAD(&ac->pending);
3085 setup_timer(&ac->timer, artpec6_crypto_timeout, (unsigned long) ac);
3086
3087 ac->base = base;
3088
3089 ac->dma_cache = kmem_cache_create("artpec6_crypto_dma",
3090 sizeof(struct artpec6_crypto_dma_descriptors),
3091 64,
3092 0,
3093 NULL);
3094 if (!ac->dma_cache)
3095 return -ENOMEM;
3096
3097 #ifdef CONFIG_DEBUG_FS
3098 artpec6_crypto_init_debugfs();
3099 #endif
3100
3101 tasklet_init(&ac->task, artpec6_crypto_task,
3102 (unsigned long)ac);
3103
3104 ac->pad_buffer = devm_kzalloc(&pdev->dev, 2 * ARTPEC_CACHE_LINE_MAX,
3105 GFP_KERNEL);
3106 if (!ac->pad_buffer)
3107 return -ENOMEM;
3108 ac->pad_buffer = PTR_ALIGN(ac->pad_buffer, ARTPEC_CACHE_LINE_MAX);
3109
3110 ac->zero_buffer = devm_kzalloc(&pdev->dev, 2 * ARTPEC_CACHE_LINE_MAX,
3111 GFP_KERNEL);
3112 if (!ac->zero_buffer)
3113 return -ENOMEM;
3114 ac->zero_buffer = PTR_ALIGN(ac->zero_buffer, ARTPEC_CACHE_LINE_MAX);
3115
3116 err = init_crypto_hw(ac);
3117 if (err)
3118 goto free_cache;
3119
3120 err = devm_request_irq(&pdev->dev, irq, artpec6_crypto_irq, 0,
3121 "artpec6-crypto", ac);
3122 if (err)
3123 goto disable_hw;
3124
3125 artpec6_crypto_dev = &pdev->dev;
3126
3127 err = crypto_register_ahashes(hash_algos, ARRAY_SIZE(hash_algos));
3128 if (err) {
3129 dev_err(dev, "Failed to register ahashes\n");
3130 goto disable_hw;
3131 }
3132
3133 if (variant != ARTPEC6_CRYPTO) {
3134 err = crypto_register_ahashes(artpec7_hash_algos,
3135 ARRAY_SIZE(artpec7_hash_algos));
3136 if (err) {
3137 dev_err(dev, "Failed to register ahashes\n");
3138 goto unregister_ahashes;
3139 }
3140 }
3141
3142 err = crypto_register_skciphers(crypto_algos, ARRAY_SIZE(crypto_algos));
3143 if (err) {
3144 dev_err(dev, "Failed to register ciphers\n");
3145 goto unregister_a7_ahashes;
3146 }
3147
3148 err = crypto_register_aeads(aead_algos, ARRAY_SIZE(aead_algos));
3149 if (err) {
3150 dev_err(dev, "Failed to register aeads\n");
3151 goto unregister_algs;
3152 }
3153
3154 return 0;
3155
3156 unregister_algs:
3157 crypto_unregister_skciphers(crypto_algos, ARRAY_SIZE(crypto_algos));
3158 unregister_a7_ahashes:
3159 if (variant != ARTPEC6_CRYPTO)
3160 crypto_unregister_ahashes(artpec7_hash_algos,
3161 ARRAY_SIZE(artpec7_hash_algos));
3162 unregister_ahashes:
3163 crypto_unregister_ahashes(hash_algos, ARRAY_SIZE(hash_algos));
3164 disable_hw:
3165 artpec6_crypto_disable_hw(ac);
3166 free_cache:
3167 kmem_cache_destroy(ac->dma_cache);
3168 return err;
3169 }
3170
artpec6_crypto_remove(struct platform_device * pdev)3171 static int artpec6_crypto_remove(struct platform_device *pdev)
3172 {
3173 struct artpec6_crypto *ac = platform_get_drvdata(pdev);
3174 int irq = platform_get_irq(pdev, 0);
3175
3176 crypto_unregister_ahashes(hash_algos, ARRAY_SIZE(hash_algos));
3177 if (ac->variant != ARTPEC6_CRYPTO)
3178 crypto_unregister_ahashes(artpec7_hash_algos,
3179 ARRAY_SIZE(artpec7_hash_algos));
3180 crypto_unregister_skciphers(crypto_algos, ARRAY_SIZE(crypto_algos));
3181 crypto_unregister_aeads(aead_algos, ARRAY_SIZE(aead_algos));
3182
3183 tasklet_disable(&ac->task);
3184 devm_free_irq(&pdev->dev, irq, ac);
3185 tasklet_kill(&ac->task);
3186 del_timer_sync(&ac->timer);
3187
3188 artpec6_crypto_disable_hw(ac);
3189
3190 kmem_cache_destroy(ac->dma_cache);
3191 #ifdef CONFIG_DEBUG_FS
3192 artpec6_crypto_free_debugfs();
3193 #endif
3194 return 0;
3195 }
3196
3197 static struct platform_driver artpec6_crypto_driver = {
3198 .probe = artpec6_crypto_probe,
3199 .remove = artpec6_crypto_remove,
3200 .driver = {
3201 .name = "artpec6-crypto",
3202 .owner = THIS_MODULE,
3203 .of_match_table = artpec6_crypto_of_match,
3204 },
3205 };
3206
3207 module_platform_driver(artpec6_crypto_driver);
3208
3209 MODULE_AUTHOR("Axis Communications AB");
3210 MODULE_DESCRIPTION("ARTPEC-6 Crypto driver");
3211 MODULE_LICENSE("GPL");
3212