4 * Support for SAHARA cryptographic accelerator.
6 * Copyright (c) 2014 Steffen Trumtrar <s.trumtrar@pengutronix.de>
7 * Copyright (c) 2013 Vista Silicon S.L.
8 * Author: Javier Martin <javier.martin@vista-silicon.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
14 * Based on omap-aes.c and tegra-aes.c
17 #include <crypto/algapi.h>
18 #include <crypto/aes.h>
19 #include <crypto/hash.h>
20 #include <crypto/internal/hash.h>
21 #include <crypto/scatterwalk.h>
22 #include <crypto/sha.h>
24 #include <linux/clk.h>
25 #include <linux/crypto.h>
26 #include <linux/interrupt.h>
28 #include <linux/irq.h>
29 #include <linux/kernel.h>
30 #include <linux/kthread.h>
31 #include <linux/module.h>
32 #include <linux/mutex.h>
34 #include <linux/of_device.h>
35 #include <linux/platform_device.h>
37 #define SHA_BUFFER_LEN PAGE_SIZE
38 #define SAHARA_MAX_SHA_BLOCK_SIZE SHA256_BLOCK_SIZE
40 #define SAHARA_NAME "sahara"
41 #define SAHARA_VERSION_3 3
42 #define SAHARA_VERSION_4 4
43 #define SAHARA_TIMEOUT_MS 1000
44 #define SAHARA_MAX_HW_DESC 2
45 #define SAHARA_MAX_HW_LINK 20
47 #define FLAGS_MODE_MASK 0x000f
48 #define FLAGS_ENCRYPT BIT(0)
49 #define FLAGS_CBC BIT(1)
50 #define FLAGS_NEW_KEY BIT(3)
52 #define SAHARA_HDR_BASE 0x00800000
53 #define SAHARA_HDR_SKHA_ALG_AES 0
54 #define SAHARA_HDR_SKHA_OP_ENC (1 << 2)
55 #define SAHARA_HDR_SKHA_MODE_ECB (0 << 3)
56 #define SAHARA_HDR_SKHA_MODE_CBC (1 << 3)
57 #define SAHARA_HDR_FORM_DATA (5 << 16)
58 #define SAHARA_HDR_FORM_KEY (8 << 16)
59 #define SAHARA_HDR_LLO (1 << 24)
60 #define SAHARA_HDR_CHA_SKHA (1 << 28)
61 #define SAHARA_HDR_CHA_MDHA (2 << 28)
62 #define SAHARA_HDR_PARITY_BIT (1 << 31)
64 #define SAHARA_HDR_MDHA_SET_MODE_MD_KEY 0x20880000
65 #define SAHARA_HDR_MDHA_SET_MODE_HASH 0x208D0000
66 #define SAHARA_HDR_MDHA_HASH 0xA0850000
67 #define SAHARA_HDR_MDHA_STORE_DIGEST 0x20820000
68 #define SAHARA_HDR_MDHA_ALG_SHA1 0
69 #define SAHARA_HDR_MDHA_ALG_MD5 1
70 #define SAHARA_HDR_MDHA_ALG_SHA256 2
71 #define SAHARA_HDR_MDHA_ALG_SHA224 3
72 #define SAHARA_HDR_MDHA_PDATA (1 << 2)
73 #define SAHARA_HDR_MDHA_HMAC (1 << 3)
74 #define SAHARA_HDR_MDHA_INIT (1 << 5)
75 #define SAHARA_HDR_MDHA_IPAD (1 << 6)
76 #define SAHARA_HDR_MDHA_OPAD (1 << 7)
77 #define SAHARA_HDR_MDHA_SWAP (1 << 8)
78 #define SAHARA_HDR_MDHA_MAC_FULL (1 << 9)
79 #define SAHARA_HDR_MDHA_SSL (1 << 10)
81 /* SAHARA can only process one request at a time */
82 #define SAHARA_QUEUE_LENGTH 1
84 #define SAHARA_REG_VERSION 0x00
85 #define SAHARA_REG_DAR 0x04
86 #define SAHARA_REG_CONTROL 0x08
87 #define SAHARA_CONTROL_SET_THROTTLE(x) (((x) & 0xff) << 24)
88 #define SAHARA_CONTROL_SET_MAXBURST(x) (((x) & 0xff) << 16)
89 #define SAHARA_CONTROL_RNG_AUTORSD (1 << 7)
90 #define SAHARA_CONTROL_ENABLE_INT (1 << 4)
91 #define SAHARA_REG_CMD 0x0C
92 #define SAHARA_CMD_RESET (1 << 0)
93 #define SAHARA_CMD_CLEAR_INT (1 << 8)
94 #define SAHARA_CMD_CLEAR_ERR (1 << 9)
95 #define SAHARA_CMD_SINGLE_STEP (1 << 10)
96 #define SAHARA_CMD_MODE_BATCH (1 << 16)
97 #define SAHARA_CMD_MODE_DEBUG (1 << 18)
98 #define SAHARA_REG_STATUS 0x10
99 #define SAHARA_STATUS_GET_STATE(x) ((x) & 0x7)
100 #define SAHARA_STATE_IDLE 0
101 #define SAHARA_STATE_BUSY 1
102 #define SAHARA_STATE_ERR 2
103 #define SAHARA_STATE_FAULT 3
104 #define SAHARA_STATE_COMPLETE 4
105 #define SAHARA_STATE_COMP_FLAG (1 << 2)
106 #define SAHARA_STATUS_DAR_FULL (1 << 3)
107 #define SAHARA_STATUS_ERROR (1 << 4)
108 #define SAHARA_STATUS_SECURE (1 << 5)
109 #define SAHARA_STATUS_FAIL (1 << 6)
110 #define SAHARA_STATUS_INIT (1 << 7)
111 #define SAHARA_STATUS_RNG_RESEED (1 << 8)
112 #define SAHARA_STATUS_ACTIVE_RNG (1 << 9)
113 #define SAHARA_STATUS_ACTIVE_MDHA (1 << 10)
114 #define SAHARA_STATUS_ACTIVE_SKHA (1 << 11)
115 #define SAHARA_STATUS_MODE_BATCH (1 << 16)
116 #define SAHARA_STATUS_MODE_DEDICATED (1 << 17)
117 #define SAHARA_STATUS_MODE_DEBUG (1 << 18)
118 #define SAHARA_STATUS_GET_ISTATE(x) (((x) >> 24) & 0xff)
119 #define SAHARA_REG_ERRSTATUS 0x14
120 #define SAHARA_ERRSTATUS_GET_SOURCE(x) ((x) & 0xf)
121 #define SAHARA_ERRSOURCE_CHA 14
122 #define SAHARA_ERRSOURCE_DMA 15
123 #define SAHARA_ERRSTATUS_DMA_DIR (1 << 8)
124 #define SAHARA_ERRSTATUS_GET_DMASZ(x)(((x) >> 9) & 0x3)
125 #define SAHARA_ERRSTATUS_GET_DMASRC(x) (((x) >> 13) & 0x7)
126 #define SAHARA_ERRSTATUS_GET_CHASRC(x) (((x) >> 16) & 0xfff)
127 #define SAHARA_ERRSTATUS_GET_CHAERR(x) (((x) >> 28) & 0x3)
128 #define SAHARA_REG_FADDR 0x18
129 #define SAHARA_REG_CDAR 0x1C
130 #define SAHARA_REG_IDAR 0x20
132 struct sahara_hw_desc
{
141 struct sahara_hw_link
{
150 /* AES-specific context */
152 u8 key
[AES_KEYSIZE_128
];
153 struct crypto_ablkcipher
*fallback
;
155 /* SHA-specific context */
156 struct crypto_shash
*shash_fallback
;
159 struct sahara_aes_reqctx
{
164 * struct sahara_sha_reqctx - private data per request
165 * @buf: holds data for requests smaller than block_size
166 * @rembuf: used to prepare one block_size-aligned request
167 * @context: hw-specific context for request. Digest is extracted from this
168 * @mode: specifies what type of hw-descriptor needs to be built
169 * @digest_size: length of digest for this request
170 * @context_size: length of hw-context for this request.
171 * Always digest_size + 4
172 * @buf_cnt: number of bytes saved in buf
173 * @sg_in_idx: number of hw links
174 * @in_sg: scatterlist for input data
175 * @in_sg_chain: scatterlists for chained input data
176 * @in_sg_chained: specifies if chained scatterlists are used or not
177 * @total: total number of bytes for transfer
178 * @last: is this the last block
179 * @first: is this the first block
180 * @active: inside a transfer
182 struct sahara_sha_reqctx
{
183 u8 buf
[SAHARA_MAX_SHA_BLOCK_SIZE
];
184 u8 rembuf
[SAHARA_MAX_SHA_BLOCK_SIZE
];
185 u8 context
[SHA256_DIGEST_SIZE
+ 4];
188 unsigned int digest_size
;
189 unsigned int context_size
;
190 unsigned int buf_cnt
;
191 unsigned int sg_in_idx
;
192 struct scatterlist
*in_sg
;
193 struct scatterlist in_sg_chain
[2];
202 struct device
*device
;
203 unsigned int version
;
204 void __iomem
*regs_base
;
207 struct mutex queue_mutex
;
208 struct task_struct
*kthread
;
209 struct completion dma_completion
;
211 struct sahara_ctx
*ctx
;
213 struct crypto_queue queue
;
216 struct sahara_hw_desc
*hw_desc
[SAHARA_MAX_HW_DESC
];
217 dma_addr_t hw_phys_desc
[SAHARA_MAX_HW_DESC
];
220 dma_addr_t key_phys_base
;
223 dma_addr_t iv_phys_base
;
226 dma_addr_t context_phys_base
;
228 struct sahara_hw_link
*hw_link
[SAHARA_MAX_HW_LINK
];
229 dma_addr_t hw_phys_link
[SAHARA_MAX_HW_LINK
];
232 struct scatterlist
*in_sg
;
233 unsigned int nb_in_sg
;
234 struct scatterlist
*out_sg
;
235 unsigned int nb_out_sg
;
240 static struct sahara_dev
*dev_ptr
;
242 static inline void sahara_write(struct sahara_dev
*dev
, u32 data
, u32 reg
)
244 writel(data
, dev
->regs_base
+ reg
);
247 static inline unsigned int sahara_read(struct sahara_dev
*dev
, u32 reg
)
249 return readl(dev
->regs_base
+ reg
);
252 static u32
sahara_aes_key_hdr(struct sahara_dev
*dev
)
254 u32 hdr
= SAHARA_HDR_BASE
| SAHARA_HDR_SKHA_ALG_AES
|
255 SAHARA_HDR_FORM_KEY
| SAHARA_HDR_LLO
|
256 SAHARA_HDR_CHA_SKHA
| SAHARA_HDR_PARITY_BIT
;
258 if (dev
->flags
& FLAGS_CBC
) {
259 hdr
|= SAHARA_HDR_SKHA_MODE_CBC
;
260 hdr
^= SAHARA_HDR_PARITY_BIT
;
263 if (dev
->flags
& FLAGS_ENCRYPT
) {
264 hdr
|= SAHARA_HDR_SKHA_OP_ENC
;
265 hdr
^= SAHARA_HDR_PARITY_BIT
;
271 static u32
sahara_aes_data_link_hdr(struct sahara_dev
*dev
)
273 return SAHARA_HDR_BASE
| SAHARA_HDR_FORM_DATA
|
274 SAHARA_HDR_CHA_SKHA
| SAHARA_HDR_PARITY_BIT
;
277 static int sahara_sg_length(struct scatterlist
*sg
,
282 struct scatterlist
*sg_list
;
288 len
= min(sg_list
->length
, total
);
293 sg_list
= sg_next(sg_list
);
301 static char *sahara_err_src
[16] = {
304 "Descriptor length error",
305 "Descriptor length or pointer error",
307 "Link pointer error",
308 "Input buffer error",
309 "Output buffer error",
310 "Output buffer starvation",
311 "Internal state fault",
312 "General descriptor problem",
314 "Descriptor address error",
315 "Link address error",
320 static char *sahara_err_dmasize
[4] = {
322 "Half-word transfer",
327 static char *sahara_err_dmasrc
[8] = {
330 "Internal IP bus error",
332 "DMA crosses 256 byte boundary",
338 static char *sahara_cha_errsrc
[12] = {
339 "Input buffer non-empty",
344 "Write during processing",
345 "CTX read during processing",
347 "Input buffer disabled/underflow",
348 "Output buffer disabled/overflow",
349 "DES key parity error",
353 static char *sahara_cha_err
[4] = { "No error", "SKHA", "MDHA", "RNG" };
355 static void sahara_decode_error(struct sahara_dev
*dev
, unsigned int error
)
357 u8 source
= SAHARA_ERRSTATUS_GET_SOURCE(error
);
358 u16 chasrc
= ffs(SAHARA_ERRSTATUS_GET_CHASRC(error
));
360 dev_err(dev
->device
, "%s: Error Register = 0x%08x\n", __func__
, error
);
362 dev_err(dev
->device
, " - %s.\n", sahara_err_src
[source
]);
364 if (source
== SAHARA_ERRSOURCE_DMA
) {
365 if (error
& SAHARA_ERRSTATUS_DMA_DIR
)
366 dev_err(dev
->device
, " * DMA read.\n");
368 dev_err(dev
->device
, " * DMA write.\n");
370 dev_err(dev
->device
, " * %s.\n",
371 sahara_err_dmasize
[SAHARA_ERRSTATUS_GET_DMASZ(error
)]);
372 dev_err(dev
->device
, " * %s.\n",
373 sahara_err_dmasrc
[SAHARA_ERRSTATUS_GET_DMASRC(error
)]);
374 } else if (source
== SAHARA_ERRSOURCE_CHA
) {
375 dev_err(dev
->device
, " * %s.\n",
376 sahara_cha_errsrc
[chasrc
]);
377 dev_err(dev
->device
, " * %s.\n",
378 sahara_cha_err
[SAHARA_ERRSTATUS_GET_CHAERR(error
)]);
380 dev_err(dev
->device
, "\n");
383 static char *sahara_state
[4] = { "Idle", "Busy", "Error", "HW Fault" };
385 static void sahara_decode_status(struct sahara_dev
*dev
, unsigned int status
)
389 if (!IS_ENABLED(DEBUG
))
392 state
= SAHARA_STATUS_GET_STATE(status
);
394 dev_dbg(dev
->device
, "%s: Status Register = 0x%08x\n",
397 dev_dbg(dev
->device
, " - State = %d:\n", state
);
398 if (state
& SAHARA_STATE_COMP_FLAG
)
399 dev_dbg(dev
->device
, " * Descriptor completed. IRQ pending.\n");
401 dev_dbg(dev
->device
, " * %s.\n",
402 sahara_state
[state
& ~SAHARA_STATE_COMP_FLAG
]);
404 if (status
& SAHARA_STATUS_DAR_FULL
)
405 dev_dbg(dev
->device
, " - DAR Full.\n");
406 if (status
& SAHARA_STATUS_ERROR
)
407 dev_dbg(dev
->device
, " - Error.\n");
408 if (status
& SAHARA_STATUS_SECURE
)
409 dev_dbg(dev
->device
, " - Secure.\n");
410 if (status
& SAHARA_STATUS_FAIL
)
411 dev_dbg(dev
->device
, " - Fail.\n");
412 if (status
& SAHARA_STATUS_RNG_RESEED
)
413 dev_dbg(dev
->device
, " - RNG Reseed Request.\n");
414 if (status
& SAHARA_STATUS_ACTIVE_RNG
)
415 dev_dbg(dev
->device
, " - RNG Active.\n");
416 if (status
& SAHARA_STATUS_ACTIVE_MDHA
)
417 dev_dbg(dev
->device
, " - MDHA Active.\n");
418 if (status
& SAHARA_STATUS_ACTIVE_SKHA
)
419 dev_dbg(dev
->device
, " - SKHA Active.\n");
421 if (status
& SAHARA_STATUS_MODE_BATCH
)
422 dev_dbg(dev
->device
, " - Batch Mode.\n");
423 else if (status
& SAHARA_STATUS_MODE_DEDICATED
)
424 dev_dbg(dev
->device
, " - Decidated Mode.\n");
425 else if (status
& SAHARA_STATUS_MODE_DEBUG
)
426 dev_dbg(dev
->device
, " - Debug Mode.\n");
428 dev_dbg(dev
->device
, " - Internal state = 0x%02x\n",
429 SAHARA_STATUS_GET_ISTATE(status
));
431 dev_dbg(dev
->device
, "Current DAR: 0x%08x\n",
432 sahara_read(dev
, SAHARA_REG_CDAR
));
433 dev_dbg(dev
->device
, "Initial DAR: 0x%08x\n\n",
434 sahara_read(dev
, SAHARA_REG_IDAR
));
437 static void sahara_dump_descriptors(struct sahara_dev
*dev
)
441 if (!IS_ENABLED(DEBUG
))
444 for (i
= 0; i
< SAHARA_MAX_HW_DESC
; i
++) {
445 dev_dbg(dev
->device
, "Descriptor (%d) (0x%08x):\n",
446 i
, dev
->hw_phys_desc
[i
]);
447 dev_dbg(dev
->device
, "\thdr = 0x%08x\n", dev
->hw_desc
[i
]->hdr
);
448 dev_dbg(dev
->device
, "\tlen1 = %u\n", dev
->hw_desc
[i
]->len1
);
449 dev_dbg(dev
->device
, "\tp1 = 0x%08x\n", dev
->hw_desc
[i
]->p1
);
450 dev_dbg(dev
->device
, "\tlen2 = %u\n", dev
->hw_desc
[i
]->len2
);
451 dev_dbg(dev
->device
, "\tp2 = 0x%08x\n", dev
->hw_desc
[i
]->p2
);
452 dev_dbg(dev
->device
, "\tnext = 0x%08x\n",
453 dev
->hw_desc
[i
]->next
);
455 dev_dbg(dev
->device
, "\n");
458 static void sahara_dump_links(struct sahara_dev
*dev
)
462 if (!IS_ENABLED(DEBUG
))
465 for (i
= 0; i
< SAHARA_MAX_HW_LINK
; i
++) {
466 dev_dbg(dev
->device
, "Link (%d) (0x%08x):\n",
467 i
, dev
->hw_phys_link
[i
]);
468 dev_dbg(dev
->device
, "\tlen = %u\n", dev
->hw_link
[i
]->len
);
469 dev_dbg(dev
->device
, "\tp = 0x%08x\n", dev
->hw_link
[i
]->p
);
470 dev_dbg(dev
->device
, "\tnext = 0x%08x\n",
471 dev
->hw_link
[i
]->next
);
473 dev_dbg(dev
->device
, "\n");
476 static int sahara_hw_descriptor_create(struct sahara_dev
*dev
)
478 struct sahara_ctx
*ctx
= dev
->ctx
;
479 struct scatterlist
*sg
;
483 /* Copy new key if necessary */
484 if (ctx
->flags
& FLAGS_NEW_KEY
) {
485 memcpy(dev
->key_base
, ctx
->key
, ctx
->keylen
);
486 ctx
->flags
&= ~FLAGS_NEW_KEY
;
488 if (dev
->flags
& FLAGS_CBC
) {
489 dev
->hw_desc
[0]->len1
= AES_BLOCK_SIZE
;
490 dev
->hw_desc
[0]->p1
= dev
->iv_phys_base
;
492 dev
->hw_desc
[0]->len1
= 0;
493 dev
->hw_desc
[0]->p1
= 0;
495 dev
->hw_desc
[0]->len2
= ctx
->keylen
;
496 dev
->hw_desc
[0]->p2
= dev
->key_phys_base
;
497 dev
->hw_desc
[0]->next
= dev
->hw_phys_desc
[1];
499 dev
->hw_desc
[0]->hdr
= sahara_aes_key_hdr(dev
);
501 dev
->nb_in_sg
= sahara_sg_length(dev
->in_sg
, dev
->total
);
502 dev
->nb_out_sg
= sahara_sg_length(dev
->out_sg
, dev
->total
);
503 if ((dev
->nb_in_sg
+ dev
->nb_out_sg
) > SAHARA_MAX_HW_LINK
) {
504 dev_err(dev
->device
, "not enough hw links (%d)\n",
505 dev
->nb_in_sg
+ dev
->nb_out_sg
);
509 ret
= dma_map_sg(dev
->device
, dev
->in_sg
, dev
->nb_in_sg
,
511 if (ret
!= dev
->nb_in_sg
) {
512 dev_err(dev
->device
, "couldn't map in sg\n");
515 ret
= dma_map_sg(dev
->device
, dev
->out_sg
, dev
->nb_out_sg
,
517 if (ret
!= dev
->nb_out_sg
) {
518 dev_err(dev
->device
, "couldn't map out sg\n");
522 /* Create input links */
523 dev
->hw_desc
[1]->p1
= dev
->hw_phys_link
[0];
525 for (i
= 0; i
< dev
->nb_in_sg
; i
++) {
526 dev
->hw_link
[i
]->len
= sg
->length
;
527 dev
->hw_link
[i
]->p
= sg
->dma_address
;
528 if (i
== (dev
->nb_in_sg
- 1)) {
529 dev
->hw_link
[i
]->next
= 0;
531 dev
->hw_link
[i
]->next
= dev
->hw_phys_link
[i
+ 1];
536 /* Create output links */
537 dev
->hw_desc
[1]->p2
= dev
->hw_phys_link
[i
];
539 for (j
= i
; j
< dev
->nb_out_sg
+ i
; j
++) {
540 dev
->hw_link
[j
]->len
= sg
->length
;
541 dev
->hw_link
[j
]->p
= sg
->dma_address
;
542 if (j
== (dev
->nb_out_sg
+ i
- 1)) {
543 dev
->hw_link
[j
]->next
= 0;
545 dev
->hw_link
[j
]->next
= dev
->hw_phys_link
[j
+ 1];
550 /* Fill remaining fields of hw_desc[1] */
551 dev
->hw_desc
[1]->hdr
= sahara_aes_data_link_hdr(dev
);
552 dev
->hw_desc
[1]->len1
= dev
->total
;
553 dev
->hw_desc
[1]->len2
= dev
->total
;
554 dev
->hw_desc
[1]->next
= 0;
556 sahara_dump_descriptors(dev
);
557 sahara_dump_links(dev
);
559 sahara_write(dev
, dev
->hw_phys_desc
[0], SAHARA_REG_DAR
);
564 dma_unmap_sg(dev
->device
, dev
->out_sg
, dev
->nb_out_sg
,
567 dma_unmap_sg(dev
->device
, dev
->in_sg
, dev
->nb_in_sg
,
573 static int sahara_aes_process(struct ablkcipher_request
*req
)
575 struct sahara_dev
*dev
= dev_ptr
;
576 struct sahara_ctx
*ctx
;
577 struct sahara_aes_reqctx
*rctx
;
580 /* Request is ready to be dispatched by the device */
582 "dispatch request (nbytes=%d, src=%p, dst=%p)\n",
583 req
->nbytes
, req
->src
, req
->dst
);
585 /* assign new request to device */
586 dev
->total
= req
->nbytes
;
587 dev
->in_sg
= req
->src
;
588 dev
->out_sg
= req
->dst
;
590 rctx
= ablkcipher_request_ctx(req
);
591 ctx
= crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req
));
592 rctx
->mode
&= FLAGS_MODE_MASK
;
593 dev
->flags
= (dev
->flags
& ~FLAGS_MODE_MASK
) | rctx
->mode
;
595 if ((dev
->flags
& FLAGS_CBC
) && req
->info
)
596 memcpy(dev
->iv_base
, req
->info
, AES_KEYSIZE_128
);
598 /* assign new context to device */
601 reinit_completion(&dev
->dma_completion
);
603 ret
= sahara_hw_descriptor_create(dev
);
605 ret
= wait_for_completion_timeout(&dev
->dma_completion
,
606 msecs_to_jiffies(SAHARA_TIMEOUT_MS
));
608 dev_err(dev
->device
, "AES timeout\n");
612 dma_unmap_sg(dev
->device
, dev
->out_sg
, dev
->nb_out_sg
,
614 dma_unmap_sg(dev
->device
, dev
->in_sg
, dev
->nb_in_sg
,
620 static int sahara_aes_setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
623 struct sahara_ctx
*ctx
= crypto_ablkcipher_ctx(tfm
);
626 ctx
->keylen
= keylen
;
628 /* SAHARA only supports 128bit keys */
629 if (keylen
== AES_KEYSIZE_128
) {
630 memcpy(ctx
->key
, key
, keylen
);
631 ctx
->flags
|= FLAGS_NEW_KEY
;
635 if (keylen
!= AES_KEYSIZE_128
&&
636 keylen
!= AES_KEYSIZE_192
&& keylen
!= AES_KEYSIZE_256
)
640 * The requested key size is not supported by HW, do a fallback.
642 ctx
->fallback
->base
.crt_flags
&= ~CRYPTO_TFM_REQ_MASK
;
643 ctx
->fallback
->base
.crt_flags
|=
644 (tfm
->base
.crt_flags
& CRYPTO_TFM_REQ_MASK
);
646 ret
= crypto_ablkcipher_setkey(ctx
->fallback
, key
, keylen
);
648 struct crypto_tfm
*tfm_aux
= crypto_ablkcipher_tfm(tfm
);
650 tfm_aux
->crt_flags
&= ~CRYPTO_TFM_RES_MASK
;
651 tfm_aux
->crt_flags
|=
652 (ctx
->fallback
->base
.crt_flags
& CRYPTO_TFM_RES_MASK
);
657 static int sahara_aes_crypt(struct ablkcipher_request
*req
, unsigned long mode
)
659 struct sahara_aes_reqctx
*rctx
= ablkcipher_request_ctx(req
);
660 struct sahara_dev
*dev
= dev_ptr
;
663 dev_dbg(dev
->device
, "nbytes: %d, enc: %d, cbc: %d\n",
664 req
->nbytes
, !!(mode
& FLAGS_ENCRYPT
), !!(mode
& FLAGS_CBC
));
666 if (!IS_ALIGNED(req
->nbytes
, AES_BLOCK_SIZE
)) {
668 "request size is not exact amount of AES blocks\n");
674 mutex_lock(&dev
->queue_mutex
);
675 err
= ablkcipher_enqueue_request(&dev
->queue
, req
);
676 mutex_unlock(&dev
->queue_mutex
);
678 wake_up_process(dev
->kthread
);
683 static int sahara_aes_ecb_encrypt(struct ablkcipher_request
*req
)
685 struct crypto_tfm
*tfm
=
686 crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req
));
687 struct sahara_ctx
*ctx
= crypto_ablkcipher_ctx(
688 crypto_ablkcipher_reqtfm(req
));
691 if (unlikely(ctx
->keylen
!= AES_KEYSIZE_128
)) {
692 ablkcipher_request_set_tfm(req
, ctx
->fallback
);
693 err
= crypto_ablkcipher_encrypt(req
);
694 ablkcipher_request_set_tfm(req
, __crypto_ablkcipher_cast(tfm
));
698 return sahara_aes_crypt(req
, FLAGS_ENCRYPT
);
701 static int sahara_aes_ecb_decrypt(struct ablkcipher_request
*req
)
703 struct crypto_tfm
*tfm
=
704 crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req
));
705 struct sahara_ctx
*ctx
= crypto_ablkcipher_ctx(
706 crypto_ablkcipher_reqtfm(req
));
709 if (unlikely(ctx
->keylen
!= AES_KEYSIZE_128
)) {
710 ablkcipher_request_set_tfm(req
, ctx
->fallback
);
711 err
= crypto_ablkcipher_decrypt(req
);
712 ablkcipher_request_set_tfm(req
, __crypto_ablkcipher_cast(tfm
));
716 return sahara_aes_crypt(req
, 0);
719 static int sahara_aes_cbc_encrypt(struct ablkcipher_request
*req
)
721 struct crypto_tfm
*tfm
=
722 crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req
));
723 struct sahara_ctx
*ctx
= crypto_ablkcipher_ctx(
724 crypto_ablkcipher_reqtfm(req
));
727 if (unlikely(ctx
->keylen
!= AES_KEYSIZE_128
)) {
728 ablkcipher_request_set_tfm(req
, ctx
->fallback
);
729 err
= crypto_ablkcipher_encrypt(req
);
730 ablkcipher_request_set_tfm(req
, __crypto_ablkcipher_cast(tfm
));
734 return sahara_aes_crypt(req
, FLAGS_ENCRYPT
| FLAGS_CBC
);
737 static int sahara_aes_cbc_decrypt(struct ablkcipher_request
*req
)
739 struct crypto_tfm
*tfm
=
740 crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req
));
741 struct sahara_ctx
*ctx
= crypto_ablkcipher_ctx(
742 crypto_ablkcipher_reqtfm(req
));
745 if (unlikely(ctx
->keylen
!= AES_KEYSIZE_128
)) {
746 ablkcipher_request_set_tfm(req
, ctx
->fallback
);
747 err
= crypto_ablkcipher_decrypt(req
);
748 ablkcipher_request_set_tfm(req
, __crypto_ablkcipher_cast(tfm
));
752 return sahara_aes_crypt(req
, FLAGS_CBC
);
755 static int sahara_aes_cra_init(struct crypto_tfm
*tfm
)
757 const char *name
= crypto_tfm_alg_name(tfm
);
758 struct sahara_ctx
*ctx
= crypto_tfm_ctx(tfm
);
760 ctx
->fallback
= crypto_alloc_ablkcipher(name
, 0,
761 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
);
762 if (IS_ERR(ctx
->fallback
)) {
763 pr_err("Error allocating fallback algo %s\n", name
);
764 return PTR_ERR(ctx
->fallback
);
767 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct sahara_aes_reqctx
);
772 static void sahara_aes_cra_exit(struct crypto_tfm
*tfm
)
774 struct sahara_ctx
*ctx
= crypto_tfm_ctx(tfm
);
777 crypto_free_ablkcipher(ctx
->fallback
);
778 ctx
->fallback
= NULL
;
781 static u32
sahara_sha_init_hdr(struct sahara_dev
*dev
,
782 struct sahara_sha_reqctx
*rctx
)
789 hdr
|= SAHARA_HDR_MDHA_SET_MODE_HASH
;
790 hdr
|= SAHARA_HDR_MDHA_INIT
;
792 hdr
|= SAHARA_HDR_MDHA_SET_MODE_MD_KEY
;
796 hdr
|= SAHARA_HDR_MDHA_PDATA
;
798 if (hweight_long(hdr
) % 2 == 0)
799 hdr
|= SAHARA_HDR_PARITY_BIT
;
804 static int sahara_sha_hw_links_create(struct sahara_dev
*dev
,
805 struct sahara_sha_reqctx
*rctx
,
808 struct scatterlist
*sg
;
812 dev
->in_sg
= rctx
->in_sg
;
814 dev
->nb_in_sg
= sahara_sg_length(dev
->in_sg
, rctx
->total
);
815 if ((dev
->nb_in_sg
) > SAHARA_MAX_HW_LINK
) {
816 dev_err(dev
->device
, "not enough hw links (%d)\n",
817 dev
->nb_in_sg
+ dev
->nb_out_sg
);
821 if (rctx
->in_sg_chained
) {
825 ret
= dma_map_sg(dev
->device
, sg
, 1,
830 dev
->hw_link
[i
]->len
= sg
->length
;
831 dev
->hw_link
[i
]->p
= sg
->dma_address
;
832 dev
->hw_link
[i
]->next
= dev
->hw_phys_link
[i
+ 1];
836 dev
->hw_link
[i
-1]->next
= 0;
839 ret
= dma_map_sg(dev
->device
, dev
->in_sg
, dev
->nb_in_sg
,
844 for (i
= start
; i
< dev
->nb_in_sg
+ start
; i
++) {
845 dev
->hw_link
[i
]->len
= sg
->length
;
846 dev
->hw_link
[i
]->p
= sg
->dma_address
;
847 if (i
== (dev
->nb_in_sg
+ start
- 1)) {
848 dev
->hw_link
[i
]->next
= 0;
850 dev
->hw_link
[i
]->next
= dev
->hw_phys_link
[i
+ 1];
859 static int sahara_sha_hw_data_descriptor_create(struct sahara_dev
*dev
,
860 struct sahara_sha_reqctx
*rctx
,
861 struct ahash_request
*req
,
868 /* Create initial descriptor: #8*/
869 dev
->hw_desc
[index
]->hdr
= sahara_sha_init_hdr(dev
, rctx
);
871 /* Create hash descriptor: #10. Must follow #6. */
872 dev
->hw_desc
[index
]->hdr
= SAHARA_HDR_MDHA_HASH
;
874 dev
->hw_desc
[index
]->len1
= rctx
->total
;
875 if (dev
->hw_desc
[index
]->len1
== 0) {
876 /* if len1 is 0, p1 must be 0, too */
877 dev
->hw_desc
[index
]->p1
= 0;
880 /* Create input links */
881 dev
->hw_desc
[index
]->p1
= dev
->hw_phys_link
[index
];
882 i
= sahara_sha_hw_links_create(dev
, rctx
, index
);
884 rctx
->sg_in_idx
= index
;
889 dev
->hw_desc
[index
]->p2
= dev
->hw_phys_link
[i
];
891 /* Save the context for the next operation */
892 result_len
= rctx
->context_size
;
893 dev
->hw_link
[i
]->p
= dev
->context_phys_base
;
895 dev
->hw_link
[i
]->len
= result_len
;
896 dev
->hw_desc
[index
]->len2
= result_len
;
898 dev
->hw_link
[i
]->next
= 0;
904 * Load descriptor aka #6
906 * To load a previously saved context back to the MDHA unit
912 static int sahara_sha_hw_context_descriptor_create(struct sahara_dev
*dev
,
913 struct sahara_sha_reqctx
*rctx
,
914 struct ahash_request
*req
,
917 dev
->hw_desc
[index
]->hdr
= sahara_sha_init_hdr(dev
, rctx
);
919 dev
->hw_desc
[index
]->len1
= rctx
->context_size
;
920 dev
->hw_desc
[index
]->p1
= dev
->hw_phys_link
[index
];
921 dev
->hw_desc
[index
]->len2
= 0;
922 dev
->hw_desc
[index
]->p2
= 0;
924 dev
->hw_link
[index
]->len
= rctx
->context_size
;
925 dev
->hw_link
[index
]->p
= dev
->context_phys_base
;
926 dev
->hw_link
[index
]->next
= 0;
931 static int sahara_walk_and_recalc(struct scatterlist
*sg
, unsigned int nbytes
)
933 if (!sg
|| !sg
->length
)
936 while (nbytes
&& sg
) {
937 if (nbytes
<= sg
->length
) {
942 nbytes
-= sg
->length
;
949 static int sahara_sha_prepare_request(struct ahash_request
*req
)
951 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
952 struct sahara_sha_reqctx
*rctx
= ahash_request_ctx(req
);
953 unsigned int hash_later
;
954 unsigned int block_size
;
957 block_size
= crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm
));
959 /* append bytes from previous operation */
960 len
= rctx
->buf_cnt
+ req
->nbytes
;
962 /* only the last transfer can be padded in hardware */
963 if (!rctx
->last
&& (len
< block_size
)) {
964 /* to few data, save for next operation */
965 scatterwalk_map_and_copy(rctx
->buf
+ rctx
->buf_cnt
, req
->src
,
967 rctx
->buf_cnt
+= req
->nbytes
;
972 /* add data from previous operation first */
974 memcpy(rctx
->rembuf
, rctx
->buf
, rctx
->buf_cnt
);
976 /* data must always be a multiple of block_size */
977 hash_later
= rctx
->last
? 0 : len
& (block_size
- 1);
979 unsigned int offset
= req
->nbytes
- hash_later
;
980 /* Save remaining bytes for later use */
981 scatterwalk_map_and_copy(rctx
->buf
, req
->src
, offset
,
985 /* nbytes should now be multiple of blocksize */
986 req
->nbytes
= req
->nbytes
- hash_later
;
988 sahara_walk_and_recalc(req
->src
, req
->nbytes
);
990 /* have data from previous operation and current */
991 if (rctx
->buf_cnt
&& req
->nbytes
) {
992 sg_init_table(rctx
->in_sg_chain
, 2);
993 sg_set_buf(rctx
->in_sg_chain
, rctx
->rembuf
, rctx
->buf_cnt
);
995 scatterwalk_sg_chain(rctx
->in_sg_chain
, 2, req
->src
);
997 rctx
->total
= req
->nbytes
+ rctx
->buf_cnt
;
998 rctx
->in_sg
= rctx
->in_sg_chain
;
1000 rctx
->in_sg_chained
= true;
1001 req
->src
= rctx
->in_sg_chain
;
1002 /* only data from previous operation */
1003 } else if (rctx
->buf_cnt
) {
1005 rctx
->in_sg
= req
->src
;
1007 rctx
->in_sg
= rctx
->in_sg_chain
;
1008 /* buf was copied into rembuf above */
1009 sg_init_one(rctx
->in_sg
, rctx
->rembuf
, rctx
->buf_cnt
);
1010 rctx
->total
= rctx
->buf_cnt
;
1011 rctx
->in_sg_chained
= false;
1012 /* no data from previous operation */
1014 rctx
->in_sg
= req
->src
;
1015 rctx
->total
= req
->nbytes
;
1016 req
->src
= rctx
->in_sg
;
1017 rctx
->in_sg_chained
= false;
1020 /* on next call, we only have the remaining data in the buffer */
1021 rctx
->buf_cnt
= hash_later
;
1023 return -EINPROGRESS
;
1026 static void sahara_sha_unmap_sg(struct sahara_dev
*dev
,
1027 struct sahara_sha_reqctx
*rctx
)
1029 struct scatterlist
*sg
;
1031 if (rctx
->in_sg_chained
) {
1034 dma_unmap_sg(dev
->device
, sg
, 1, DMA_TO_DEVICE
);
1038 dma_unmap_sg(dev
->device
, dev
->in_sg
, dev
->nb_in_sg
,
1043 static int sahara_sha_process(struct ahash_request
*req
)
1045 struct sahara_dev
*dev
= dev_ptr
;
1046 struct sahara_sha_reqctx
*rctx
= ahash_request_ctx(req
);
1047 int ret
= -EINPROGRESS
;
1049 ret
= sahara_sha_prepare_request(req
);
1054 sahara_sha_hw_data_descriptor_create(dev
, rctx
, req
, 0);
1055 dev
->hw_desc
[0]->next
= 0;
1058 memcpy(dev
->context_base
, rctx
->context
, rctx
->context_size
);
1060 sahara_sha_hw_context_descriptor_create(dev
, rctx
, req
, 0);
1061 dev
->hw_desc
[0]->next
= dev
->hw_phys_desc
[1];
1062 sahara_sha_hw_data_descriptor_create(dev
, rctx
, req
, 1);
1063 dev
->hw_desc
[1]->next
= 0;
1066 sahara_dump_descriptors(dev
);
1067 sahara_dump_links(dev
);
1069 reinit_completion(&dev
->dma_completion
);
1071 sahara_write(dev
, dev
->hw_phys_desc
[0], SAHARA_REG_DAR
);
1073 ret
= wait_for_completion_timeout(&dev
->dma_completion
,
1074 msecs_to_jiffies(SAHARA_TIMEOUT_MS
));
1076 dev_err(dev
->device
, "SHA timeout\n");
1080 if (rctx
->sg_in_idx
)
1081 sahara_sha_unmap_sg(dev
, rctx
);
1083 memcpy(rctx
->context
, dev
->context_base
, rctx
->context_size
);
1086 memcpy(req
->result
, rctx
->context
, rctx
->digest_size
);
1091 static int sahara_queue_manage(void *data
)
1093 struct sahara_dev
*dev
= (struct sahara_dev
*)data
;
1094 struct crypto_async_request
*async_req
;
1098 __set_current_state(TASK_INTERRUPTIBLE
);
1100 mutex_lock(&dev
->queue_mutex
);
1101 async_req
= crypto_dequeue_request(&dev
->queue
);
1102 mutex_unlock(&dev
->queue_mutex
);
1105 if (crypto_tfm_alg_type(async_req
->tfm
) ==
1106 CRYPTO_ALG_TYPE_AHASH
) {
1107 struct ahash_request
*req
=
1108 ahash_request_cast(async_req
);
1110 ret
= sahara_sha_process(req
);
1112 struct ablkcipher_request
*req
=
1113 ablkcipher_request_cast(async_req
);
1115 ret
= sahara_aes_process(req
);
1118 async_req
->complete(async_req
, ret
);
1124 } while (!kthread_should_stop());
1129 static int sahara_sha_enqueue(struct ahash_request
*req
, int last
)
1131 struct sahara_sha_reqctx
*rctx
= ahash_request_ctx(req
);
1132 struct sahara_dev
*dev
= dev_ptr
;
1135 if (!req
->nbytes
&& !last
)
1138 mutex_lock(&rctx
->mutex
);
1141 if (!rctx
->active
) {
1146 mutex_lock(&dev
->queue_mutex
);
1147 ret
= crypto_enqueue_request(&dev
->queue
, &req
->base
);
1148 mutex_unlock(&dev
->queue_mutex
);
1150 wake_up_process(dev
->kthread
);
1151 mutex_unlock(&rctx
->mutex
);
1156 static int sahara_sha_init(struct ahash_request
*req
)
1158 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
1159 struct sahara_sha_reqctx
*rctx
= ahash_request_ctx(req
);
1161 memset(rctx
, 0, sizeof(*rctx
));
1163 switch (crypto_ahash_digestsize(tfm
)) {
1164 case SHA1_DIGEST_SIZE
:
1165 rctx
->mode
|= SAHARA_HDR_MDHA_ALG_SHA1
;
1166 rctx
->digest_size
= SHA1_DIGEST_SIZE
;
1168 case SHA256_DIGEST_SIZE
:
1169 rctx
->mode
|= SAHARA_HDR_MDHA_ALG_SHA256
;
1170 rctx
->digest_size
= SHA256_DIGEST_SIZE
;
1176 rctx
->context_size
= rctx
->digest_size
+ 4;
1179 mutex_init(&rctx
->mutex
);
1184 static int sahara_sha_update(struct ahash_request
*req
)
1186 return sahara_sha_enqueue(req
, 0);
1189 static int sahara_sha_final(struct ahash_request
*req
)
1192 return sahara_sha_enqueue(req
, 1);
1195 static int sahara_sha_finup(struct ahash_request
*req
)
1197 return sahara_sha_enqueue(req
, 1);
1200 static int sahara_sha_digest(struct ahash_request
*req
)
1202 sahara_sha_init(req
);
1204 return sahara_sha_finup(req
);
1207 static int sahara_sha_export(struct ahash_request
*req
, void *out
)
1209 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(req
);
1210 struct sahara_ctx
*ctx
= crypto_ahash_ctx(ahash
);
1211 struct sahara_sha_reqctx
*rctx
= ahash_request_ctx(req
);
1213 memcpy(out
, ctx
, sizeof(struct sahara_ctx
));
1214 memcpy(out
+ sizeof(struct sahara_sha_reqctx
), rctx
,
1215 sizeof(struct sahara_sha_reqctx
));
1220 static int sahara_sha_import(struct ahash_request
*req
, const void *in
)
1222 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(req
);
1223 struct sahara_ctx
*ctx
= crypto_ahash_ctx(ahash
);
1224 struct sahara_sha_reqctx
*rctx
= ahash_request_ctx(req
);
1226 memcpy(ctx
, in
, sizeof(struct sahara_ctx
));
1227 memcpy(rctx
, in
+ sizeof(struct sahara_sha_reqctx
),
1228 sizeof(struct sahara_sha_reqctx
));
1233 static int sahara_sha_cra_init(struct crypto_tfm
*tfm
)
1235 const char *name
= crypto_tfm_alg_name(tfm
);
1236 struct sahara_ctx
*ctx
= crypto_tfm_ctx(tfm
);
1238 ctx
->shash_fallback
= crypto_alloc_shash(name
, 0,
1239 CRYPTO_ALG_NEED_FALLBACK
);
1240 if (IS_ERR(ctx
->shash_fallback
)) {
1241 pr_err("Error allocating fallback algo %s\n", name
);
1242 return PTR_ERR(ctx
->shash_fallback
);
1244 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
1245 sizeof(struct sahara_sha_reqctx
) +
1246 SHA_BUFFER_LEN
+ SHA256_BLOCK_SIZE
);
1251 static void sahara_sha_cra_exit(struct crypto_tfm
*tfm
)
1253 struct sahara_ctx
*ctx
= crypto_tfm_ctx(tfm
);
1255 crypto_free_shash(ctx
->shash_fallback
);
1256 ctx
->shash_fallback
= NULL
;
1259 static struct crypto_alg aes_algs
[] = {
1261 .cra_name
= "ecb(aes)",
1262 .cra_driver_name
= "sahara-ecb-aes",
1263 .cra_priority
= 300,
1264 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1265 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
,
1266 .cra_blocksize
= AES_BLOCK_SIZE
,
1267 .cra_ctxsize
= sizeof(struct sahara_ctx
),
1268 .cra_alignmask
= 0x0,
1269 .cra_type
= &crypto_ablkcipher_type
,
1270 .cra_module
= THIS_MODULE
,
1271 .cra_init
= sahara_aes_cra_init
,
1272 .cra_exit
= sahara_aes_cra_exit
,
1273 .cra_u
.ablkcipher
= {
1274 .min_keysize
= AES_MIN_KEY_SIZE
,
1275 .max_keysize
= AES_MAX_KEY_SIZE
,
1276 .setkey
= sahara_aes_setkey
,
1277 .encrypt
= sahara_aes_ecb_encrypt
,
1278 .decrypt
= sahara_aes_ecb_decrypt
,
1281 .cra_name
= "cbc(aes)",
1282 .cra_driver_name
= "sahara-cbc-aes",
1283 .cra_priority
= 300,
1284 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1285 CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
,
1286 .cra_blocksize
= AES_BLOCK_SIZE
,
1287 .cra_ctxsize
= sizeof(struct sahara_ctx
),
1288 .cra_alignmask
= 0x0,
1289 .cra_type
= &crypto_ablkcipher_type
,
1290 .cra_module
= THIS_MODULE
,
1291 .cra_init
= sahara_aes_cra_init
,
1292 .cra_exit
= sahara_aes_cra_exit
,
1293 .cra_u
.ablkcipher
= {
1294 .min_keysize
= AES_MIN_KEY_SIZE
,
1295 .max_keysize
= AES_MAX_KEY_SIZE
,
1296 .ivsize
= AES_BLOCK_SIZE
,
1297 .setkey
= sahara_aes_setkey
,
1298 .encrypt
= sahara_aes_cbc_encrypt
,
1299 .decrypt
= sahara_aes_cbc_decrypt
,
1304 static struct ahash_alg sha_v3_algs
[] = {
1306 .init
= sahara_sha_init
,
1307 .update
= sahara_sha_update
,
1308 .final
= sahara_sha_final
,
1309 .finup
= sahara_sha_finup
,
1310 .digest
= sahara_sha_digest
,
1311 .export
= sahara_sha_export
,
1312 .import
= sahara_sha_import
,
1313 .halg
.digestsize
= SHA1_DIGEST_SIZE
,
1316 .cra_driver_name
= "sahara-sha1",
1317 .cra_priority
= 300,
1318 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
1320 CRYPTO_ALG_NEED_FALLBACK
,
1321 .cra_blocksize
= SHA1_BLOCK_SIZE
,
1322 .cra_ctxsize
= sizeof(struct sahara_ctx
),
1324 .cra_module
= THIS_MODULE
,
1325 .cra_init
= sahara_sha_cra_init
,
1326 .cra_exit
= sahara_sha_cra_exit
,
1331 static struct ahash_alg sha_v4_algs
[] = {
1333 .init
= sahara_sha_init
,
1334 .update
= sahara_sha_update
,
1335 .final
= sahara_sha_final
,
1336 .finup
= sahara_sha_finup
,
1337 .digest
= sahara_sha_digest
,
1338 .export
= sahara_sha_export
,
1339 .import
= sahara_sha_import
,
1340 .halg
.digestsize
= SHA256_DIGEST_SIZE
,
1342 .cra_name
= "sha256",
1343 .cra_driver_name
= "sahara-sha256",
1344 .cra_priority
= 300,
1345 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
1347 CRYPTO_ALG_NEED_FALLBACK
,
1348 .cra_blocksize
= SHA256_BLOCK_SIZE
,
1349 .cra_ctxsize
= sizeof(struct sahara_ctx
),
1351 .cra_module
= THIS_MODULE
,
1352 .cra_init
= sahara_sha_cra_init
,
1353 .cra_exit
= sahara_sha_cra_exit
,
1358 static irqreturn_t
sahara_irq_handler(int irq
, void *data
)
1360 struct sahara_dev
*dev
= (struct sahara_dev
*)data
;
1361 unsigned int stat
= sahara_read(dev
, SAHARA_REG_STATUS
);
1362 unsigned int err
= sahara_read(dev
, SAHARA_REG_ERRSTATUS
);
1364 sahara_write(dev
, SAHARA_CMD_CLEAR_INT
| SAHARA_CMD_CLEAR_ERR
,
1367 sahara_decode_status(dev
, stat
);
1369 if (SAHARA_STATUS_GET_STATE(stat
) == SAHARA_STATE_BUSY
) {
1371 } else if (SAHARA_STATUS_GET_STATE(stat
) == SAHARA_STATE_COMPLETE
) {
1374 sahara_decode_error(dev
, err
);
1375 dev
->error
= -EINVAL
;
1378 complete(&dev
->dma_completion
);
1384 static int sahara_register_algs(struct sahara_dev
*dev
)
1387 unsigned int i
, j
, k
, l
;
1389 for (i
= 0; i
< ARRAY_SIZE(aes_algs
); i
++) {
1390 INIT_LIST_HEAD(&aes_algs
[i
].cra_list
);
1391 err
= crypto_register_alg(&aes_algs
[i
]);
1396 for (k
= 0; k
< ARRAY_SIZE(sha_v3_algs
); k
++) {
1397 err
= crypto_register_ahash(&sha_v3_algs
[k
]);
1399 goto err_sha_v3_algs
;
1402 if (dev
->version
> SAHARA_VERSION_3
)
1403 for (l
= 0; l
< ARRAY_SIZE(sha_v4_algs
); l
++) {
1404 err
= crypto_register_ahash(&sha_v4_algs
[l
]);
1406 goto err_sha_v4_algs
;
1412 for (j
= 0; j
< l
; j
++)
1413 crypto_unregister_ahash(&sha_v4_algs
[j
]);
1416 for (j
= 0; j
< k
; j
++)
1417 crypto_unregister_ahash(&sha_v4_algs
[j
]);
1420 for (j
= 0; j
< i
; j
++)
1421 crypto_unregister_alg(&aes_algs
[j
]);
1426 static void sahara_unregister_algs(struct sahara_dev
*dev
)
1430 for (i
= 0; i
< ARRAY_SIZE(aes_algs
); i
++)
1431 crypto_unregister_alg(&aes_algs
[i
]);
1433 for (i
= 0; i
< ARRAY_SIZE(sha_v4_algs
); i
++)
1434 crypto_unregister_ahash(&sha_v3_algs
[i
]);
1436 if (dev
->version
> SAHARA_VERSION_3
)
1437 for (i
= 0; i
< ARRAY_SIZE(sha_v4_algs
); i
++)
1438 crypto_unregister_ahash(&sha_v4_algs
[i
]);
1441 static struct platform_device_id sahara_platform_ids
[] = {
1442 { .name
= "sahara-imx27" },
1445 MODULE_DEVICE_TABLE(platform
, sahara_platform_ids
);
1447 static struct of_device_id sahara_dt_ids
[] = {
1448 { .compatible
= "fsl,imx53-sahara" },
1449 { .compatible
= "fsl,imx27-sahara" },
1452 MODULE_DEVICE_TABLE(of
, sahara_dt_ids
);
1454 static int sahara_probe(struct platform_device
*pdev
)
1456 struct sahara_dev
*dev
;
1457 struct resource
*res
;
1463 dev
= devm_kzalloc(&pdev
->dev
, sizeof(struct sahara_dev
), GFP_KERNEL
);
1465 dev_err(&pdev
->dev
, "unable to alloc data struct.\n");
1469 dev
->device
= &pdev
->dev
;
1470 platform_set_drvdata(pdev
, dev
);
1472 /* Get the base address */
1473 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1474 dev
->regs_base
= devm_ioremap_resource(&pdev
->dev
, res
);
1475 if (IS_ERR(dev
->regs_base
))
1476 return PTR_ERR(dev
->regs_base
);
1479 irq
= platform_get_irq(pdev
, 0);
1481 dev_err(&pdev
->dev
, "failed to get irq resource\n");
1485 err
= devm_request_irq(&pdev
->dev
, irq
, sahara_irq_handler
,
1486 0, dev_name(&pdev
->dev
), dev
);
1488 dev_err(&pdev
->dev
, "failed to request irq\n");
1493 dev
->clk_ipg
= devm_clk_get(&pdev
->dev
, "ipg");
1494 if (IS_ERR(dev
->clk_ipg
)) {
1495 dev_err(&pdev
->dev
, "Could not get ipg clock\n");
1496 return PTR_ERR(dev
->clk_ipg
);
1499 dev
->clk_ahb
= devm_clk_get(&pdev
->dev
, "ahb");
1500 if (IS_ERR(dev
->clk_ahb
)) {
1501 dev_err(&pdev
->dev
, "Could not get ahb clock\n");
1502 return PTR_ERR(dev
->clk_ahb
);
1505 /* Allocate HW descriptors */
1506 dev
->hw_desc
[0] = dma_alloc_coherent(&pdev
->dev
,
1507 SAHARA_MAX_HW_DESC
* sizeof(struct sahara_hw_desc
),
1508 &dev
->hw_phys_desc
[0], GFP_KERNEL
);
1509 if (!dev
->hw_desc
[0]) {
1510 dev_err(&pdev
->dev
, "Could not allocate hw descriptors\n");
1513 dev
->hw_desc
[1] = dev
->hw_desc
[0] + 1;
1514 dev
->hw_phys_desc
[1] = dev
->hw_phys_desc
[0] +
1515 sizeof(struct sahara_hw_desc
);
1517 /* Allocate space for iv and key */
1518 dev
->key_base
= dma_alloc_coherent(&pdev
->dev
, 2 * AES_KEYSIZE_128
,
1519 &dev
->key_phys_base
, GFP_KERNEL
);
1520 if (!dev
->key_base
) {
1521 dev_err(&pdev
->dev
, "Could not allocate memory for key\n");
1525 dev
->iv_base
= dev
->key_base
+ AES_KEYSIZE_128
;
1526 dev
->iv_phys_base
= dev
->key_phys_base
+ AES_KEYSIZE_128
;
1528 /* Allocate space for context: largest digest + message length field */
1529 dev
->context_base
= dma_alloc_coherent(&pdev
->dev
,
1530 SHA256_DIGEST_SIZE
+ 4,
1531 &dev
->context_phys_base
, GFP_KERNEL
);
1532 if (!dev
->context_base
) {
1533 dev_err(&pdev
->dev
, "Could not allocate memory for MDHA context\n");
1538 /* Allocate space for HW links */
1539 dev
->hw_link
[0] = dma_alloc_coherent(&pdev
->dev
,
1540 SAHARA_MAX_HW_LINK
* sizeof(struct sahara_hw_link
),
1541 &dev
->hw_phys_link
[0], GFP_KERNEL
);
1542 if (!dev
->hw_link
[0]) {
1543 dev_err(&pdev
->dev
, "Could not allocate hw links\n");
1547 for (i
= 1; i
< SAHARA_MAX_HW_LINK
; i
++) {
1548 dev
->hw_phys_link
[i
] = dev
->hw_phys_link
[i
- 1] +
1549 sizeof(struct sahara_hw_link
);
1550 dev
->hw_link
[i
] = dev
->hw_link
[i
- 1] + 1;
1553 crypto_init_queue(&dev
->queue
, SAHARA_QUEUE_LENGTH
);
1555 spin_lock_init(&dev
->lock
);
1556 mutex_init(&dev
->queue_mutex
);
1560 dev
->kthread
= kthread_run(sahara_queue_manage
, dev
, "sahara_crypto");
1561 if (IS_ERR(dev
->kthread
)) {
1562 err
= PTR_ERR(dev
->kthread
);
1566 init_completion(&dev
->dma_completion
);
1568 clk_prepare_enable(dev
->clk_ipg
);
1569 clk_prepare_enable(dev
->clk_ahb
);
1571 version
= sahara_read(dev
, SAHARA_REG_VERSION
);
1572 if (of_device_is_compatible(pdev
->dev
.of_node
, "fsl,imx27-sahara")) {
1573 if (version
!= SAHARA_VERSION_3
)
1575 } else if (of_device_is_compatible(pdev
->dev
.of_node
,
1576 "fsl,imx53-sahara")) {
1577 if (((version
>> 8) & 0xff) != SAHARA_VERSION_4
)
1579 version
= (version
>> 8) & 0xff;
1581 if (err
== -ENODEV
) {
1582 dev_err(&pdev
->dev
, "SAHARA version %d not supported\n",
1587 dev
->version
= version
;
1589 sahara_write(dev
, SAHARA_CMD_RESET
| SAHARA_CMD_MODE_BATCH
,
1591 sahara_write(dev
, SAHARA_CONTROL_SET_THROTTLE(0) |
1592 SAHARA_CONTROL_SET_MAXBURST(8) |
1593 SAHARA_CONTROL_RNG_AUTORSD
|
1594 SAHARA_CONTROL_ENABLE_INT
,
1595 SAHARA_REG_CONTROL
);
1597 err
= sahara_register_algs(dev
);
1601 dev_info(&pdev
->dev
, "SAHARA version %d initialized\n", version
);
1606 dma_free_coherent(&pdev
->dev
,
1607 SAHARA_MAX_HW_LINK
* sizeof(struct sahara_hw_link
),
1608 dev
->hw_link
[0], dev
->hw_phys_link
[0]);
1609 clk_disable_unprepare(dev
->clk_ipg
);
1610 clk_disable_unprepare(dev
->clk_ahb
);
1611 kthread_stop(dev
->kthread
);
1614 dma_free_coherent(&pdev
->dev
,
1615 2 * AES_KEYSIZE_128
,
1616 dev
->key_base
, dev
->key_phys_base
);
1617 dma_free_coherent(&pdev
->dev
,
1619 dev
->context_base
, dev
->context_phys_base
);
1621 dma_free_coherent(&pdev
->dev
,
1622 SAHARA_MAX_HW_DESC
* sizeof(struct sahara_hw_desc
),
1623 dev
->hw_desc
[0], dev
->hw_phys_desc
[0]);
1628 static int sahara_remove(struct platform_device
*pdev
)
1630 struct sahara_dev
*dev
= platform_get_drvdata(pdev
);
1632 dma_free_coherent(&pdev
->dev
,
1633 SAHARA_MAX_HW_LINK
* sizeof(struct sahara_hw_link
),
1634 dev
->hw_link
[0], dev
->hw_phys_link
[0]);
1635 dma_free_coherent(&pdev
->dev
,
1636 2 * AES_KEYSIZE_128
,
1637 dev
->key_base
, dev
->key_phys_base
);
1638 dma_free_coherent(&pdev
->dev
,
1639 SAHARA_MAX_HW_DESC
* sizeof(struct sahara_hw_desc
),
1640 dev
->hw_desc
[0], dev
->hw_phys_desc
[0]);
1642 kthread_stop(dev
->kthread
);
1644 sahara_unregister_algs(dev
);
1646 clk_disable_unprepare(dev
->clk_ipg
);
1647 clk_disable_unprepare(dev
->clk_ahb
);
1654 static struct platform_driver sahara_driver
= {
1655 .probe
= sahara_probe
,
1656 .remove
= sahara_remove
,
1658 .name
= SAHARA_NAME
,
1659 .of_match_table
= sahara_dt_ids
,
1661 .id_table
= sahara_platform_ids
,
1664 module_platform_driver(sahara_driver
);
1666 MODULE_LICENSE("GPL");
1667 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
1668 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
1669 MODULE_DESCRIPTION("SAHARA2 HW crypto accelerator");