1 // SPDX-License-Identifier: GPL-2.0-only
5 * Support for SAHARA cryptographic accelerator.
7 * Copyright (c) 2014 Steffen Trumtrar <s.trumtrar@pengutronix.de>
8 * Copyright (c) 2013 Vista Silicon S.L.
9 * Author: Javier Martin <javier.martin@vista-silicon.com>
11 * Based on omap-aes.c and tegra-aes.c
14 #include <crypto/aes.h>
15 #include <crypto/internal/hash.h>
16 #include <crypto/internal/skcipher.h>
17 #include <crypto/scatterwalk.h>
18 #include <crypto/sha.h>
20 #include <linux/clk.h>
21 #include <linux/crypto.h>
22 #include <linux/interrupt.h>
24 #include <linux/irq.h>
25 #include <linux/kernel.h>
26 #include <linux/kthread.h>
27 #include <linux/module.h>
28 #include <linux/mutex.h>
30 #include <linux/of_device.h>
31 #include <linux/platform_device.h>
33 #define SHA_BUFFER_LEN PAGE_SIZE
34 #define SAHARA_MAX_SHA_BLOCK_SIZE SHA256_BLOCK_SIZE
36 #define SAHARA_NAME "sahara"
37 #define SAHARA_VERSION_3 3
38 #define SAHARA_VERSION_4 4
39 #define SAHARA_TIMEOUT_MS 1000
40 #define SAHARA_MAX_HW_DESC 2
41 #define SAHARA_MAX_HW_LINK 20
43 #define FLAGS_MODE_MASK 0x000f
44 #define FLAGS_ENCRYPT BIT(0)
45 #define FLAGS_CBC BIT(1)
46 #define FLAGS_NEW_KEY BIT(3)
48 #define SAHARA_HDR_BASE 0x00800000
49 #define SAHARA_HDR_SKHA_ALG_AES 0
50 #define SAHARA_HDR_SKHA_OP_ENC (1 << 2)
51 #define SAHARA_HDR_SKHA_MODE_ECB (0 << 3)
52 #define SAHARA_HDR_SKHA_MODE_CBC (1 << 3)
53 #define SAHARA_HDR_FORM_DATA (5 << 16)
54 #define SAHARA_HDR_FORM_KEY (8 << 16)
55 #define SAHARA_HDR_LLO (1 << 24)
56 #define SAHARA_HDR_CHA_SKHA (1 << 28)
57 #define SAHARA_HDR_CHA_MDHA (2 << 28)
58 #define SAHARA_HDR_PARITY_BIT (1 << 31)
60 #define SAHARA_HDR_MDHA_SET_MODE_MD_KEY 0x20880000
61 #define SAHARA_HDR_MDHA_SET_MODE_HASH 0x208D0000
62 #define SAHARA_HDR_MDHA_HASH 0xA0850000
63 #define SAHARA_HDR_MDHA_STORE_DIGEST 0x20820000
64 #define SAHARA_HDR_MDHA_ALG_SHA1 0
65 #define SAHARA_HDR_MDHA_ALG_MD5 1
66 #define SAHARA_HDR_MDHA_ALG_SHA256 2
67 #define SAHARA_HDR_MDHA_ALG_SHA224 3
68 #define SAHARA_HDR_MDHA_PDATA (1 << 2)
69 #define SAHARA_HDR_MDHA_HMAC (1 << 3)
70 #define SAHARA_HDR_MDHA_INIT (1 << 5)
71 #define SAHARA_HDR_MDHA_IPAD (1 << 6)
72 #define SAHARA_HDR_MDHA_OPAD (1 << 7)
73 #define SAHARA_HDR_MDHA_SWAP (1 << 8)
74 #define SAHARA_HDR_MDHA_MAC_FULL (1 << 9)
75 #define SAHARA_HDR_MDHA_SSL (1 << 10)
77 /* SAHARA can only process one request at a time */
78 #define SAHARA_QUEUE_LENGTH 1
80 #define SAHARA_REG_VERSION 0x00
81 #define SAHARA_REG_DAR 0x04
82 #define SAHARA_REG_CONTROL 0x08
83 #define SAHARA_CONTROL_SET_THROTTLE(x) (((x) & 0xff) << 24)
84 #define SAHARA_CONTROL_SET_MAXBURST(x) (((x) & 0xff) << 16)
85 #define SAHARA_CONTROL_RNG_AUTORSD (1 << 7)
86 #define SAHARA_CONTROL_ENABLE_INT (1 << 4)
87 #define SAHARA_REG_CMD 0x0C
88 #define SAHARA_CMD_RESET (1 << 0)
89 #define SAHARA_CMD_CLEAR_INT (1 << 8)
90 #define SAHARA_CMD_CLEAR_ERR (1 << 9)
91 #define SAHARA_CMD_SINGLE_STEP (1 << 10)
92 #define SAHARA_CMD_MODE_BATCH (1 << 16)
93 #define SAHARA_CMD_MODE_DEBUG (1 << 18)
94 #define SAHARA_REG_STATUS 0x10
95 #define SAHARA_STATUS_GET_STATE(x) ((x) & 0x7)
96 #define SAHARA_STATE_IDLE 0
97 #define SAHARA_STATE_BUSY 1
98 #define SAHARA_STATE_ERR 2
99 #define SAHARA_STATE_FAULT 3
100 #define SAHARA_STATE_COMPLETE 4
101 #define SAHARA_STATE_COMP_FLAG (1 << 2)
102 #define SAHARA_STATUS_DAR_FULL (1 << 3)
103 #define SAHARA_STATUS_ERROR (1 << 4)
104 #define SAHARA_STATUS_SECURE (1 << 5)
105 #define SAHARA_STATUS_FAIL (1 << 6)
106 #define SAHARA_STATUS_INIT (1 << 7)
107 #define SAHARA_STATUS_RNG_RESEED (1 << 8)
108 #define SAHARA_STATUS_ACTIVE_RNG (1 << 9)
109 #define SAHARA_STATUS_ACTIVE_MDHA (1 << 10)
110 #define SAHARA_STATUS_ACTIVE_SKHA (1 << 11)
111 #define SAHARA_STATUS_MODE_BATCH (1 << 16)
112 #define SAHARA_STATUS_MODE_DEDICATED (1 << 17)
113 #define SAHARA_STATUS_MODE_DEBUG (1 << 18)
114 #define SAHARA_STATUS_GET_ISTATE(x) (((x) >> 24) & 0xff)
115 #define SAHARA_REG_ERRSTATUS 0x14
116 #define SAHARA_ERRSTATUS_GET_SOURCE(x) ((x) & 0xf)
117 #define SAHARA_ERRSOURCE_CHA 14
118 #define SAHARA_ERRSOURCE_DMA 15
119 #define SAHARA_ERRSTATUS_DMA_DIR (1 << 8)
120 #define SAHARA_ERRSTATUS_GET_DMASZ(x)(((x) >> 9) & 0x3)
121 #define SAHARA_ERRSTATUS_GET_DMASRC(x) (((x) >> 13) & 0x7)
122 #define SAHARA_ERRSTATUS_GET_CHASRC(x) (((x) >> 16) & 0xfff)
123 #define SAHARA_ERRSTATUS_GET_CHAERR(x) (((x) >> 28) & 0x3)
124 #define SAHARA_REG_FADDR 0x18
125 #define SAHARA_REG_CDAR 0x1C
126 #define SAHARA_REG_IDAR 0x20
128 struct sahara_hw_desc
{
137 struct sahara_hw_link
{
146 /* AES-specific context */
148 u8 key
[AES_KEYSIZE_128
];
149 struct crypto_sync_skcipher
*fallback
;
152 struct sahara_aes_reqctx
{
157 * struct sahara_sha_reqctx - private data per request
158 * @buf: holds data for requests smaller than block_size
159 * @rembuf: used to prepare one block_size-aligned request
160 * @context: hw-specific context for request. Digest is extracted from this
161 * @mode: specifies what type of hw-descriptor needs to be built
162 * @digest_size: length of digest for this request
163 * @context_size: length of hw-context for this request.
164 * Always digest_size + 4
165 * @buf_cnt: number of bytes saved in buf
166 * @sg_in_idx: number of hw links
167 * @in_sg: scatterlist for input data
168 * @in_sg_chain: scatterlists for chained input data
169 * @total: total number of bytes for transfer
170 * @last: is this the last block
171 * @first: is this the first block
172 * @active: inside a transfer
174 struct sahara_sha_reqctx
{
175 u8 buf
[SAHARA_MAX_SHA_BLOCK_SIZE
];
176 u8 rembuf
[SAHARA_MAX_SHA_BLOCK_SIZE
];
177 u8 context
[SHA256_DIGEST_SIZE
+ 4];
179 unsigned int digest_size
;
180 unsigned int context_size
;
181 unsigned int buf_cnt
;
182 unsigned int sg_in_idx
;
183 struct scatterlist
*in_sg
;
184 struct scatterlist in_sg_chain
[2];
192 struct device
*device
;
193 unsigned int version
;
194 void __iomem
*regs_base
;
197 struct mutex queue_mutex
;
198 struct task_struct
*kthread
;
199 struct completion dma_completion
;
201 struct sahara_ctx
*ctx
;
202 struct crypto_queue queue
;
205 struct sahara_hw_desc
*hw_desc
[SAHARA_MAX_HW_DESC
];
206 dma_addr_t hw_phys_desc
[SAHARA_MAX_HW_DESC
];
209 dma_addr_t key_phys_base
;
212 dma_addr_t iv_phys_base
;
215 dma_addr_t context_phys_base
;
217 struct sahara_hw_link
*hw_link
[SAHARA_MAX_HW_LINK
];
218 dma_addr_t hw_phys_link
[SAHARA_MAX_HW_LINK
];
221 struct scatterlist
*in_sg
;
223 struct scatterlist
*out_sg
;
229 static struct sahara_dev
*dev_ptr
;
231 static inline void sahara_write(struct sahara_dev
*dev
, u32 data
, u32 reg
)
233 writel(data
, dev
->regs_base
+ reg
);
236 static inline unsigned int sahara_read(struct sahara_dev
*dev
, u32 reg
)
238 return readl(dev
->regs_base
+ reg
);
241 static u32
sahara_aes_key_hdr(struct sahara_dev
*dev
)
243 u32 hdr
= SAHARA_HDR_BASE
| SAHARA_HDR_SKHA_ALG_AES
|
244 SAHARA_HDR_FORM_KEY
| SAHARA_HDR_LLO
|
245 SAHARA_HDR_CHA_SKHA
| SAHARA_HDR_PARITY_BIT
;
247 if (dev
->flags
& FLAGS_CBC
) {
248 hdr
|= SAHARA_HDR_SKHA_MODE_CBC
;
249 hdr
^= SAHARA_HDR_PARITY_BIT
;
252 if (dev
->flags
& FLAGS_ENCRYPT
) {
253 hdr
|= SAHARA_HDR_SKHA_OP_ENC
;
254 hdr
^= SAHARA_HDR_PARITY_BIT
;
260 static u32
sahara_aes_data_link_hdr(struct sahara_dev
*dev
)
262 return SAHARA_HDR_BASE
| SAHARA_HDR_FORM_DATA
|
263 SAHARA_HDR_CHA_SKHA
| SAHARA_HDR_PARITY_BIT
;
266 static const char *sahara_err_src
[16] = {
269 "Descriptor length error",
270 "Descriptor length or pointer error",
272 "Link pointer error",
273 "Input buffer error",
274 "Output buffer error",
275 "Output buffer starvation",
276 "Internal state fault",
277 "General descriptor problem",
279 "Descriptor address error",
280 "Link address error",
285 static const char *sahara_err_dmasize
[4] = {
287 "Half-word transfer",
292 static const char *sahara_err_dmasrc
[8] = {
295 "Internal IP bus error",
297 "DMA crosses 256 byte boundary",
303 static const char *sahara_cha_errsrc
[12] = {
304 "Input buffer non-empty",
309 "Write during processing",
310 "CTX read during processing",
312 "Input buffer disabled/underflow",
313 "Output buffer disabled/overflow",
314 "DES key parity error",
318 static const char *sahara_cha_err
[4] = { "No error", "SKHA", "MDHA", "RNG" };
320 static void sahara_decode_error(struct sahara_dev
*dev
, unsigned int error
)
322 u8 source
= SAHARA_ERRSTATUS_GET_SOURCE(error
);
323 u16 chasrc
= ffs(SAHARA_ERRSTATUS_GET_CHASRC(error
));
325 dev_err(dev
->device
, "%s: Error Register = 0x%08x\n", __func__
, error
);
327 dev_err(dev
->device
, " - %s.\n", sahara_err_src
[source
]);
329 if (source
== SAHARA_ERRSOURCE_DMA
) {
330 if (error
& SAHARA_ERRSTATUS_DMA_DIR
)
331 dev_err(dev
->device
, " * DMA read.\n");
333 dev_err(dev
->device
, " * DMA write.\n");
335 dev_err(dev
->device
, " * %s.\n",
336 sahara_err_dmasize
[SAHARA_ERRSTATUS_GET_DMASZ(error
)]);
337 dev_err(dev
->device
, " * %s.\n",
338 sahara_err_dmasrc
[SAHARA_ERRSTATUS_GET_DMASRC(error
)]);
339 } else if (source
== SAHARA_ERRSOURCE_CHA
) {
340 dev_err(dev
->device
, " * %s.\n",
341 sahara_cha_errsrc
[chasrc
]);
342 dev_err(dev
->device
, " * %s.\n",
343 sahara_cha_err
[SAHARA_ERRSTATUS_GET_CHAERR(error
)]);
345 dev_err(dev
->device
, "\n");
348 static const char *sahara_state
[4] = { "Idle", "Busy", "Error", "HW Fault" };
350 static void sahara_decode_status(struct sahara_dev
*dev
, unsigned int status
)
354 if (!__is_defined(DEBUG
))
357 state
= SAHARA_STATUS_GET_STATE(status
);
359 dev_dbg(dev
->device
, "%s: Status Register = 0x%08x\n",
362 dev_dbg(dev
->device
, " - State = %d:\n", state
);
363 if (state
& SAHARA_STATE_COMP_FLAG
)
364 dev_dbg(dev
->device
, " * Descriptor completed. IRQ pending.\n");
366 dev_dbg(dev
->device
, " * %s.\n",
367 sahara_state
[state
& ~SAHARA_STATE_COMP_FLAG
]);
369 if (status
& SAHARA_STATUS_DAR_FULL
)
370 dev_dbg(dev
->device
, " - DAR Full.\n");
371 if (status
& SAHARA_STATUS_ERROR
)
372 dev_dbg(dev
->device
, " - Error.\n");
373 if (status
& SAHARA_STATUS_SECURE
)
374 dev_dbg(dev
->device
, " - Secure.\n");
375 if (status
& SAHARA_STATUS_FAIL
)
376 dev_dbg(dev
->device
, " - Fail.\n");
377 if (status
& SAHARA_STATUS_RNG_RESEED
)
378 dev_dbg(dev
->device
, " - RNG Reseed Request.\n");
379 if (status
& SAHARA_STATUS_ACTIVE_RNG
)
380 dev_dbg(dev
->device
, " - RNG Active.\n");
381 if (status
& SAHARA_STATUS_ACTIVE_MDHA
)
382 dev_dbg(dev
->device
, " - MDHA Active.\n");
383 if (status
& SAHARA_STATUS_ACTIVE_SKHA
)
384 dev_dbg(dev
->device
, " - SKHA Active.\n");
386 if (status
& SAHARA_STATUS_MODE_BATCH
)
387 dev_dbg(dev
->device
, " - Batch Mode.\n");
388 else if (status
& SAHARA_STATUS_MODE_DEDICATED
)
389 dev_dbg(dev
->device
, " - Dedicated Mode.\n");
390 else if (status
& SAHARA_STATUS_MODE_DEBUG
)
391 dev_dbg(dev
->device
, " - Debug Mode.\n");
393 dev_dbg(dev
->device
, " - Internal state = 0x%02x\n",
394 SAHARA_STATUS_GET_ISTATE(status
));
396 dev_dbg(dev
->device
, "Current DAR: 0x%08x\n",
397 sahara_read(dev
, SAHARA_REG_CDAR
));
398 dev_dbg(dev
->device
, "Initial DAR: 0x%08x\n\n",
399 sahara_read(dev
, SAHARA_REG_IDAR
));
402 static void sahara_dump_descriptors(struct sahara_dev
*dev
)
406 if (!__is_defined(DEBUG
))
409 for (i
= 0; i
< SAHARA_MAX_HW_DESC
; i
++) {
410 dev_dbg(dev
->device
, "Descriptor (%d) (%pad):\n",
411 i
, &dev
->hw_phys_desc
[i
]);
412 dev_dbg(dev
->device
, "\thdr = 0x%08x\n", dev
->hw_desc
[i
]->hdr
);
413 dev_dbg(dev
->device
, "\tlen1 = %u\n", dev
->hw_desc
[i
]->len1
);
414 dev_dbg(dev
->device
, "\tp1 = 0x%08x\n", dev
->hw_desc
[i
]->p1
);
415 dev_dbg(dev
->device
, "\tlen2 = %u\n", dev
->hw_desc
[i
]->len2
);
416 dev_dbg(dev
->device
, "\tp2 = 0x%08x\n", dev
->hw_desc
[i
]->p2
);
417 dev_dbg(dev
->device
, "\tnext = 0x%08x\n",
418 dev
->hw_desc
[i
]->next
);
420 dev_dbg(dev
->device
, "\n");
423 static void sahara_dump_links(struct sahara_dev
*dev
)
427 if (!__is_defined(DEBUG
))
430 for (i
= 0; i
< SAHARA_MAX_HW_LINK
; i
++) {
431 dev_dbg(dev
->device
, "Link (%d) (%pad):\n",
432 i
, &dev
->hw_phys_link
[i
]);
433 dev_dbg(dev
->device
, "\tlen = %u\n", dev
->hw_link
[i
]->len
);
434 dev_dbg(dev
->device
, "\tp = 0x%08x\n", dev
->hw_link
[i
]->p
);
435 dev_dbg(dev
->device
, "\tnext = 0x%08x\n",
436 dev
->hw_link
[i
]->next
);
438 dev_dbg(dev
->device
, "\n");
441 static int sahara_hw_descriptor_create(struct sahara_dev
*dev
)
443 struct sahara_ctx
*ctx
= dev
->ctx
;
444 struct scatterlist
*sg
;
449 /* Copy new key if necessary */
450 if (ctx
->flags
& FLAGS_NEW_KEY
) {
451 memcpy(dev
->key_base
, ctx
->key
, ctx
->keylen
);
452 ctx
->flags
&= ~FLAGS_NEW_KEY
;
454 if (dev
->flags
& FLAGS_CBC
) {
455 dev
->hw_desc
[idx
]->len1
= AES_BLOCK_SIZE
;
456 dev
->hw_desc
[idx
]->p1
= dev
->iv_phys_base
;
458 dev
->hw_desc
[idx
]->len1
= 0;
459 dev
->hw_desc
[idx
]->p1
= 0;
461 dev
->hw_desc
[idx
]->len2
= ctx
->keylen
;
462 dev
->hw_desc
[idx
]->p2
= dev
->key_phys_base
;
463 dev
->hw_desc
[idx
]->next
= dev
->hw_phys_desc
[1];
465 dev
->hw_desc
[idx
]->hdr
= sahara_aes_key_hdr(dev
);
470 dev
->nb_in_sg
= sg_nents_for_len(dev
->in_sg
, dev
->total
);
471 if (dev
->nb_in_sg
< 0) {
472 dev_err(dev
->device
, "Invalid numbers of src SG.\n");
473 return dev
->nb_in_sg
;
475 dev
->nb_out_sg
= sg_nents_for_len(dev
->out_sg
, dev
->total
);
476 if (dev
->nb_out_sg
< 0) {
477 dev_err(dev
->device
, "Invalid numbers of dst SG.\n");
478 return dev
->nb_out_sg
;
480 if ((dev
->nb_in_sg
+ dev
->nb_out_sg
) > SAHARA_MAX_HW_LINK
) {
481 dev_err(dev
->device
, "not enough hw links (%d)\n",
482 dev
->nb_in_sg
+ dev
->nb_out_sg
);
486 ret
= dma_map_sg(dev
->device
, dev
->in_sg
, dev
->nb_in_sg
,
488 if (ret
!= dev
->nb_in_sg
) {
489 dev_err(dev
->device
, "couldn't map in sg\n");
492 ret
= dma_map_sg(dev
->device
, dev
->out_sg
, dev
->nb_out_sg
,
494 if (ret
!= dev
->nb_out_sg
) {
495 dev_err(dev
->device
, "couldn't map out sg\n");
499 /* Create input links */
500 dev
->hw_desc
[idx
]->p1
= dev
->hw_phys_link
[0];
502 for (i
= 0; i
< dev
->nb_in_sg
; i
++) {
503 dev
->hw_link
[i
]->len
= sg
->length
;
504 dev
->hw_link
[i
]->p
= sg
->dma_address
;
505 if (i
== (dev
->nb_in_sg
- 1)) {
506 dev
->hw_link
[i
]->next
= 0;
508 dev
->hw_link
[i
]->next
= dev
->hw_phys_link
[i
+ 1];
513 /* Create output links */
514 dev
->hw_desc
[idx
]->p2
= dev
->hw_phys_link
[i
];
516 for (j
= i
; j
< dev
->nb_out_sg
+ i
; j
++) {
517 dev
->hw_link
[j
]->len
= sg
->length
;
518 dev
->hw_link
[j
]->p
= sg
->dma_address
;
519 if (j
== (dev
->nb_out_sg
+ i
- 1)) {
520 dev
->hw_link
[j
]->next
= 0;
522 dev
->hw_link
[j
]->next
= dev
->hw_phys_link
[j
+ 1];
527 /* Fill remaining fields of hw_desc[1] */
528 dev
->hw_desc
[idx
]->hdr
= sahara_aes_data_link_hdr(dev
);
529 dev
->hw_desc
[idx
]->len1
= dev
->total
;
530 dev
->hw_desc
[idx
]->len2
= dev
->total
;
531 dev
->hw_desc
[idx
]->next
= 0;
533 sahara_dump_descriptors(dev
);
534 sahara_dump_links(dev
);
536 sahara_write(dev
, dev
->hw_phys_desc
[0], SAHARA_REG_DAR
);
541 dma_unmap_sg(dev
->device
, dev
->out_sg
, dev
->nb_out_sg
,
544 dma_unmap_sg(dev
->device
, dev
->in_sg
, dev
->nb_in_sg
,
550 static int sahara_aes_process(struct skcipher_request
*req
)
552 struct sahara_dev
*dev
= dev_ptr
;
553 struct sahara_ctx
*ctx
;
554 struct sahara_aes_reqctx
*rctx
;
556 unsigned long timeout
;
558 /* Request is ready to be dispatched by the device */
560 "dispatch request (nbytes=%d, src=%p, dst=%p)\n",
561 req
->cryptlen
, req
->src
, req
->dst
);
563 /* assign new request to device */
564 dev
->total
= req
->cryptlen
;
565 dev
->in_sg
= req
->src
;
566 dev
->out_sg
= req
->dst
;
568 rctx
= skcipher_request_ctx(req
);
569 ctx
= crypto_skcipher_ctx(crypto_skcipher_reqtfm(req
));
570 rctx
->mode
&= FLAGS_MODE_MASK
;
571 dev
->flags
= (dev
->flags
& ~FLAGS_MODE_MASK
) | rctx
->mode
;
573 if ((dev
->flags
& FLAGS_CBC
) && req
->iv
)
574 memcpy(dev
->iv_base
, req
->iv
, AES_KEYSIZE_128
);
576 /* assign new context to device */
579 reinit_completion(&dev
->dma_completion
);
581 ret
= sahara_hw_descriptor_create(dev
);
585 timeout
= wait_for_completion_timeout(&dev
->dma_completion
,
586 msecs_to_jiffies(SAHARA_TIMEOUT_MS
));
588 dev_err(dev
->device
, "AES timeout\n");
592 dma_unmap_sg(dev
->device
, dev
->out_sg
, dev
->nb_out_sg
,
594 dma_unmap_sg(dev
->device
, dev
->in_sg
, dev
->nb_in_sg
,
600 static int sahara_aes_setkey(struct crypto_skcipher
*tfm
, const u8
*key
,
603 struct sahara_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
605 ctx
->keylen
= keylen
;
607 /* SAHARA only supports 128bit keys */
608 if (keylen
== AES_KEYSIZE_128
) {
609 memcpy(ctx
->key
, key
, keylen
);
610 ctx
->flags
|= FLAGS_NEW_KEY
;
614 if (keylen
!= AES_KEYSIZE_192
&& keylen
!= AES_KEYSIZE_256
)
618 * The requested key size is not supported by HW, do a fallback.
620 crypto_sync_skcipher_clear_flags(ctx
->fallback
, CRYPTO_TFM_REQ_MASK
);
621 crypto_sync_skcipher_set_flags(ctx
->fallback
, tfm
->base
.crt_flags
&
622 CRYPTO_TFM_REQ_MASK
);
623 return crypto_sync_skcipher_setkey(ctx
->fallback
, key
, keylen
);
626 static int sahara_aes_crypt(struct skcipher_request
*req
, unsigned long mode
)
628 struct sahara_aes_reqctx
*rctx
= skcipher_request_ctx(req
);
629 struct sahara_dev
*dev
= dev_ptr
;
632 dev_dbg(dev
->device
, "nbytes: %d, enc: %d, cbc: %d\n",
633 req
->cryptlen
, !!(mode
& FLAGS_ENCRYPT
), !!(mode
& FLAGS_CBC
));
635 if (!IS_ALIGNED(req
->cryptlen
, AES_BLOCK_SIZE
)) {
637 "request size is not exact amount of AES blocks\n");
643 mutex_lock(&dev
->queue_mutex
);
644 err
= crypto_enqueue_request(&dev
->queue
, &req
->base
);
645 mutex_unlock(&dev
->queue_mutex
);
647 wake_up_process(dev
->kthread
);
652 static int sahara_aes_ecb_encrypt(struct skcipher_request
*req
)
654 struct sahara_ctx
*ctx
= crypto_skcipher_ctx(
655 crypto_skcipher_reqtfm(req
));
658 if (unlikely(ctx
->keylen
!= AES_KEYSIZE_128
)) {
659 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq
, ctx
->fallback
);
661 skcipher_request_set_sync_tfm(subreq
, ctx
->fallback
);
662 skcipher_request_set_callback(subreq
, req
->base
.flags
,
664 skcipher_request_set_crypt(subreq
, req
->src
, req
->dst
,
665 req
->cryptlen
, req
->iv
);
666 err
= crypto_skcipher_encrypt(subreq
);
667 skcipher_request_zero(subreq
);
671 return sahara_aes_crypt(req
, FLAGS_ENCRYPT
);
674 static int sahara_aes_ecb_decrypt(struct skcipher_request
*req
)
676 struct sahara_ctx
*ctx
= crypto_skcipher_ctx(
677 crypto_skcipher_reqtfm(req
));
680 if (unlikely(ctx
->keylen
!= AES_KEYSIZE_128
)) {
681 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq
, ctx
->fallback
);
683 skcipher_request_set_sync_tfm(subreq
, ctx
->fallback
);
684 skcipher_request_set_callback(subreq
, req
->base
.flags
,
686 skcipher_request_set_crypt(subreq
, req
->src
, req
->dst
,
687 req
->cryptlen
, req
->iv
);
688 err
= crypto_skcipher_decrypt(subreq
);
689 skcipher_request_zero(subreq
);
693 return sahara_aes_crypt(req
, 0);
696 static int sahara_aes_cbc_encrypt(struct skcipher_request
*req
)
698 struct sahara_ctx
*ctx
= crypto_skcipher_ctx(
699 crypto_skcipher_reqtfm(req
));
702 if (unlikely(ctx
->keylen
!= AES_KEYSIZE_128
)) {
703 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq
, ctx
->fallback
);
705 skcipher_request_set_sync_tfm(subreq
, ctx
->fallback
);
706 skcipher_request_set_callback(subreq
, req
->base
.flags
,
708 skcipher_request_set_crypt(subreq
, req
->src
, req
->dst
,
709 req
->cryptlen
, req
->iv
);
710 err
= crypto_skcipher_encrypt(subreq
);
711 skcipher_request_zero(subreq
);
715 return sahara_aes_crypt(req
, FLAGS_ENCRYPT
| FLAGS_CBC
);
718 static int sahara_aes_cbc_decrypt(struct skcipher_request
*req
)
720 struct sahara_ctx
*ctx
= crypto_skcipher_ctx(
721 crypto_skcipher_reqtfm(req
));
724 if (unlikely(ctx
->keylen
!= AES_KEYSIZE_128
)) {
725 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq
, ctx
->fallback
);
727 skcipher_request_set_sync_tfm(subreq
, ctx
->fallback
);
728 skcipher_request_set_callback(subreq
, req
->base
.flags
,
730 skcipher_request_set_crypt(subreq
, req
->src
, req
->dst
,
731 req
->cryptlen
, req
->iv
);
732 err
= crypto_skcipher_decrypt(subreq
);
733 skcipher_request_zero(subreq
);
737 return sahara_aes_crypt(req
, FLAGS_CBC
);
740 static int sahara_aes_init_tfm(struct crypto_skcipher
*tfm
)
742 const char *name
= crypto_tfm_alg_name(&tfm
->base
);
743 struct sahara_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
745 ctx
->fallback
= crypto_alloc_sync_skcipher(name
, 0,
746 CRYPTO_ALG_NEED_FALLBACK
);
747 if (IS_ERR(ctx
->fallback
)) {
748 pr_err("Error allocating fallback algo %s\n", name
);
749 return PTR_ERR(ctx
->fallback
);
752 crypto_skcipher_set_reqsize(tfm
, sizeof(struct sahara_aes_reqctx
));
757 static void sahara_aes_exit_tfm(struct crypto_skcipher
*tfm
)
759 struct sahara_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
761 crypto_free_sync_skcipher(ctx
->fallback
);
764 static u32
sahara_sha_init_hdr(struct sahara_dev
*dev
,
765 struct sahara_sha_reqctx
*rctx
)
772 hdr
|= SAHARA_HDR_MDHA_SET_MODE_HASH
;
773 hdr
|= SAHARA_HDR_MDHA_INIT
;
775 hdr
|= SAHARA_HDR_MDHA_SET_MODE_MD_KEY
;
779 hdr
|= SAHARA_HDR_MDHA_PDATA
;
781 if (hweight_long(hdr
) % 2 == 0)
782 hdr
|= SAHARA_HDR_PARITY_BIT
;
787 static int sahara_sha_hw_links_create(struct sahara_dev
*dev
,
788 struct sahara_sha_reqctx
*rctx
,
791 struct scatterlist
*sg
;
795 dev
->in_sg
= rctx
->in_sg
;
797 dev
->nb_in_sg
= sg_nents_for_len(dev
->in_sg
, rctx
->total
);
798 if (dev
->nb_in_sg
< 0) {
799 dev_err(dev
->device
, "Invalid numbers of src SG.\n");
800 return dev
->nb_in_sg
;
802 if ((dev
->nb_in_sg
) > SAHARA_MAX_HW_LINK
) {
803 dev_err(dev
->device
, "not enough hw links (%d)\n",
804 dev
->nb_in_sg
+ dev
->nb_out_sg
);
809 ret
= dma_map_sg(dev
->device
, dev
->in_sg
, dev
->nb_in_sg
, DMA_TO_DEVICE
);
813 for (i
= start
; i
< dev
->nb_in_sg
+ start
; i
++) {
814 dev
->hw_link
[i
]->len
= sg
->length
;
815 dev
->hw_link
[i
]->p
= sg
->dma_address
;
816 if (i
== (dev
->nb_in_sg
+ start
- 1)) {
817 dev
->hw_link
[i
]->next
= 0;
819 dev
->hw_link
[i
]->next
= dev
->hw_phys_link
[i
+ 1];
827 static int sahara_sha_hw_data_descriptor_create(struct sahara_dev
*dev
,
828 struct sahara_sha_reqctx
*rctx
,
829 struct ahash_request
*req
,
836 /* Create initial descriptor: #8*/
837 dev
->hw_desc
[index
]->hdr
= sahara_sha_init_hdr(dev
, rctx
);
839 /* Create hash descriptor: #10. Must follow #6. */
840 dev
->hw_desc
[index
]->hdr
= SAHARA_HDR_MDHA_HASH
;
842 dev
->hw_desc
[index
]->len1
= rctx
->total
;
843 if (dev
->hw_desc
[index
]->len1
== 0) {
844 /* if len1 is 0, p1 must be 0, too */
845 dev
->hw_desc
[index
]->p1
= 0;
848 /* Create input links */
849 dev
->hw_desc
[index
]->p1
= dev
->hw_phys_link
[index
];
850 i
= sahara_sha_hw_links_create(dev
, rctx
, index
);
852 rctx
->sg_in_idx
= index
;
857 dev
->hw_desc
[index
]->p2
= dev
->hw_phys_link
[i
];
859 /* Save the context for the next operation */
860 result_len
= rctx
->context_size
;
861 dev
->hw_link
[i
]->p
= dev
->context_phys_base
;
863 dev
->hw_link
[i
]->len
= result_len
;
864 dev
->hw_desc
[index
]->len2
= result_len
;
866 dev
->hw_link
[i
]->next
= 0;
872 * Load descriptor aka #6
874 * To load a previously saved context back to the MDHA unit
880 static int sahara_sha_hw_context_descriptor_create(struct sahara_dev
*dev
,
881 struct sahara_sha_reqctx
*rctx
,
882 struct ahash_request
*req
,
885 dev
->hw_desc
[index
]->hdr
= sahara_sha_init_hdr(dev
, rctx
);
887 dev
->hw_desc
[index
]->len1
= rctx
->context_size
;
888 dev
->hw_desc
[index
]->p1
= dev
->hw_phys_link
[index
];
889 dev
->hw_desc
[index
]->len2
= 0;
890 dev
->hw_desc
[index
]->p2
= 0;
892 dev
->hw_link
[index
]->len
= rctx
->context_size
;
893 dev
->hw_link
[index
]->p
= dev
->context_phys_base
;
894 dev
->hw_link
[index
]->next
= 0;
899 static int sahara_walk_and_recalc(struct scatterlist
*sg
, unsigned int nbytes
)
901 if (!sg
|| !sg
->length
)
904 while (nbytes
&& sg
) {
905 if (nbytes
<= sg
->length
) {
910 nbytes
-= sg
->length
;
917 static int sahara_sha_prepare_request(struct ahash_request
*req
)
919 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
920 struct sahara_sha_reqctx
*rctx
= ahash_request_ctx(req
);
921 unsigned int hash_later
;
922 unsigned int block_size
;
925 block_size
= crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm
));
927 /* append bytes from previous operation */
928 len
= rctx
->buf_cnt
+ req
->nbytes
;
930 /* only the last transfer can be padded in hardware */
931 if (!rctx
->last
&& (len
< block_size
)) {
932 /* to few data, save for next operation */
933 scatterwalk_map_and_copy(rctx
->buf
+ rctx
->buf_cnt
, req
->src
,
935 rctx
->buf_cnt
+= req
->nbytes
;
940 /* add data from previous operation first */
942 memcpy(rctx
->rembuf
, rctx
->buf
, rctx
->buf_cnt
);
944 /* data must always be a multiple of block_size */
945 hash_later
= rctx
->last
? 0 : len
& (block_size
- 1);
947 unsigned int offset
= req
->nbytes
- hash_later
;
948 /* Save remaining bytes for later use */
949 scatterwalk_map_and_copy(rctx
->buf
, req
->src
, offset
,
953 /* nbytes should now be multiple of blocksize */
954 req
->nbytes
= req
->nbytes
- hash_later
;
956 sahara_walk_and_recalc(req
->src
, req
->nbytes
);
958 /* have data from previous operation and current */
959 if (rctx
->buf_cnt
&& req
->nbytes
) {
960 sg_init_table(rctx
->in_sg_chain
, 2);
961 sg_set_buf(rctx
->in_sg_chain
, rctx
->rembuf
, rctx
->buf_cnt
);
963 sg_chain(rctx
->in_sg_chain
, 2, req
->src
);
965 rctx
->total
= req
->nbytes
+ rctx
->buf_cnt
;
966 rctx
->in_sg
= rctx
->in_sg_chain
;
968 req
->src
= rctx
->in_sg_chain
;
969 /* only data from previous operation */
970 } else if (rctx
->buf_cnt
) {
972 rctx
->in_sg
= req
->src
;
974 rctx
->in_sg
= rctx
->in_sg_chain
;
975 /* buf was copied into rembuf above */
976 sg_init_one(rctx
->in_sg
, rctx
->rembuf
, rctx
->buf_cnt
);
977 rctx
->total
= rctx
->buf_cnt
;
978 /* no data from previous operation */
980 rctx
->in_sg
= req
->src
;
981 rctx
->total
= req
->nbytes
;
982 req
->src
= rctx
->in_sg
;
985 /* on next call, we only have the remaining data in the buffer */
986 rctx
->buf_cnt
= hash_later
;
991 static int sahara_sha_process(struct ahash_request
*req
)
993 struct sahara_dev
*dev
= dev_ptr
;
994 struct sahara_sha_reqctx
*rctx
= ahash_request_ctx(req
);
996 unsigned long timeout
;
998 ret
= sahara_sha_prepare_request(req
);
1003 sahara_sha_hw_data_descriptor_create(dev
, rctx
, req
, 0);
1004 dev
->hw_desc
[0]->next
= 0;
1007 memcpy(dev
->context_base
, rctx
->context
, rctx
->context_size
);
1009 sahara_sha_hw_context_descriptor_create(dev
, rctx
, req
, 0);
1010 dev
->hw_desc
[0]->next
= dev
->hw_phys_desc
[1];
1011 sahara_sha_hw_data_descriptor_create(dev
, rctx
, req
, 1);
1012 dev
->hw_desc
[1]->next
= 0;
1015 sahara_dump_descriptors(dev
);
1016 sahara_dump_links(dev
);
1018 reinit_completion(&dev
->dma_completion
);
1020 sahara_write(dev
, dev
->hw_phys_desc
[0], SAHARA_REG_DAR
);
1022 timeout
= wait_for_completion_timeout(&dev
->dma_completion
,
1023 msecs_to_jiffies(SAHARA_TIMEOUT_MS
));
1025 dev_err(dev
->device
, "SHA timeout\n");
1029 if (rctx
->sg_in_idx
)
1030 dma_unmap_sg(dev
->device
, dev
->in_sg
, dev
->nb_in_sg
,
1033 memcpy(rctx
->context
, dev
->context_base
, rctx
->context_size
);
1036 memcpy(req
->result
, rctx
->context
, rctx
->digest_size
);
1041 static int sahara_queue_manage(void *data
)
1043 struct sahara_dev
*dev
= (struct sahara_dev
*)data
;
1044 struct crypto_async_request
*async_req
;
1045 struct crypto_async_request
*backlog
;
1049 __set_current_state(TASK_INTERRUPTIBLE
);
1051 mutex_lock(&dev
->queue_mutex
);
1052 backlog
= crypto_get_backlog(&dev
->queue
);
1053 async_req
= crypto_dequeue_request(&dev
->queue
);
1054 mutex_unlock(&dev
->queue_mutex
);
1057 backlog
->complete(backlog
, -EINPROGRESS
);
1060 if (crypto_tfm_alg_type(async_req
->tfm
) ==
1061 CRYPTO_ALG_TYPE_AHASH
) {
1062 struct ahash_request
*req
=
1063 ahash_request_cast(async_req
);
1065 ret
= sahara_sha_process(req
);
1067 struct skcipher_request
*req
=
1068 skcipher_request_cast(async_req
);
1070 ret
= sahara_aes_process(req
);
1073 async_req
->complete(async_req
, ret
);
1079 } while (!kthread_should_stop());
1084 static int sahara_sha_enqueue(struct ahash_request
*req
, int last
)
1086 struct sahara_sha_reqctx
*rctx
= ahash_request_ctx(req
);
1087 struct sahara_dev
*dev
= dev_ptr
;
1090 if (!req
->nbytes
&& !last
)
1095 if (!rctx
->active
) {
1100 mutex_lock(&dev
->queue_mutex
);
1101 ret
= crypto_enqueue_request(&dev
->queue
, &req
->base
);
1102 mutex_unlock(&dev
->queue_mutex
);
1104 wake_up_process(dev
->kthread
);
1109 static int sahara_sha_init(struct ahash_request
*req
)
1111 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
1112 struct sahara_sha_reqctx
*rctx
= ahash_request_ctx(req
);
1114 memset(rctx
, 0, sizeof(*rctx
));
1116 switch (crypto_ahash_digestsize(tfm
)) {
1117 case SHA1_DIGEST_SIZE
:
1118 rctx
->mode
|= SAHARA_HDR_MDHA_ALG_SHA1
;
1119 rctx
->digest_size
= SHA1_DIGEST_SIZE
;
1121 case SHA256_DIGEST_SIZE
:
1122 rctx
->mode
|= SAHARA_HDR_MDHA_ALG_SHA256
;
1123 rctx
->digest_size
= SHA256_DIGEST_SIZE
;
1129 rctx
->context_size
= rctx
->digest_size
+ 4;
1135 static int sahara_sha_update(struct ahash_request
*req
)
1137 return sahara_sha_enqueue(req
, 0);
1140 static int sahara_sha_final(struct ahash_request
*req
)
1143 return sahara_sha_enqueue(req
, 1);
1146 static int sahara_sha_finup(struct ahash_request
*req
)
1148 return sahara_sha_enqueue(req
, 1);
1151 static int sahara_sha_digest(struct ahash_request
*req
)
1153 sahara_sha_init(req
);
1155 return sahara_sha_finup(req
);
1158 static int sahara_sha_export(struct ahash_request
*req
, void *out
)
1160 struct sahara_sha_reqctx
*rctx
= ahash_request_ctx(req
);
1162 memcpy(out
, rctx
, sizeof(struct sahara_sha_reqctx
));
1167 static int sahara_sha_import(struct ahash_request
*req
, const void *in
)
1169 struct sahara_sha_reqctx
*rctx
= ahash_request_ctx(req
);
1171 memcpy(rctx
, in
, sizeof(struct sahara_sha_reqctx
));
1176 static int sahara_sha_cra_init(struct crypto_tfm
*tfm
)
1178 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
1179 sizeof(struct sahara_sha_reqctx
) +
1180 SHA_BUFFER_LEN
+ SHA256_BLOCK_SIZE
);
1185 static struct skcipher_alg aes_algs
[] = {
1187 .base
.cra_name
= "ecb(aes)",
1188 .base
.cra_driver_name
= "sahara-ecb-aes",
1189 .base
.cra_priority
= 300,
1190 .base
.cra_flags
= CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
,
1191 .base
.cra_blocksize
= AES_BLOCK_SIZE
,
1192 .base
.cra_ctxsize
= sizeof(struct sahara_ctx
),
1193 .base
.cra_alignmask
= 0x0,
1194 .base
.cra_module
= THIS_MODULE
,
1196 .init
= sahara_aes_init_tfm
,
1197 .exit
= sahara_aes_exit_tfm
,
1198 .min_keysize
= AES_MIN_KEY_SIZE
,
1199 .max_keysize
= AES_MAX_KEY_SIZE
,
1200 .setkey
= sahara_aes_setkey
,
1201 .encrypt
= sahara_aes_ecb_encrypt
,
1202 .decrypt
= sahara_aes_ecb_decrypt
,
1204 .base
.cra_name
= "cbc(aes)",
1205 .base
.cra_driver_name
= "sahara-cbc-aes",
1206 .base
.cra_priority
= 300,
1207 .base
.cra_flags
= CRYPTO_ALG_ASYNC
| CRYPTO_ALG_NEED_FALLBACK
,
1208 .base
.cra_blocksize
= AES_BLOCK_SIZE
,
1209 .base
.cra_ctxsize
= sizeof(struct sahara_ctx
),
1210 .base
.cra_alignmask
= 0x0,
1211 .base
.cra_module
= THIS_MODULE
,
1213 .init
= sahara_aes_init_tfm
,
1214 .exit
= sahara_aes_exit_tfm
,
1215 .min_keysize
= AES_MIN_KEY_SIZE
,
1216 .max_keysize
= AES_MAX_KEY_SIZE
,
1217 .ivsize
= AES_BLOCK_SIZE
,
1218 .setkey
= sahara_aes_setkey
,
1219 .encrypt
= sahara_aes_cbc_encrypt
,
1220 .decrypt
= sahara_aes_cbc_decrypt
,
1224 static struct ahash_alg sha_v3_algs
[] = {
1226 .init
= sahara_sha_init
,
1227 .update
= sahara_sha_update
,
1228 .final
= sahara_sha_final
,
1229 .finup
= sahara_sha_finup
,
1230 .digest
= sahara_sha_digest
,
1231 .export
= sahara_sha_export
,
1232 .import
= sahara_sha_import
,
1233 .halg
.digestsize
= SHA1_DIGEST_SIZE
,
1234 .halg
.statesize
= sizeof(struct sahara_sha_reqctx
),
1237 .cra_driver_name
= "sahara-sha1",
1238 .cra_priority
= 300,
1239 .cra_flags
= CRYPTO_ALG_ASYNC
|
1240 CRYPTO_ALG_NEED_FALLBACK
,
1241 .cra_blocksize
= SHA1_BLOCK_SIZE
,
1242 .cra_ctxsize
= sizeof(struct sahara_ctx
),
1244 .cra_module
= THIS_MODULE
,
1245 .cra_init
= sahara_sha_cra_init
,
1250 static struct ahash_alg sha_v4_algs
[] = {
1252 .init
= sahara_sha_init
,
1253 .update
= sahara_sha_update
,
1254 .final
= sahara_sha_final
,
1255 .finup
= sahara_sha_finup
,
1256 .digest
= sahara_sha_digest
,
1257 .export
= sahara_sha_export
,
1258 .import
= sahara_sha_import
,
1259 .halg
.digestsize
= SHA256_DIGEST_SIZE
,
1260 .halg
.statesize
= sizeof(struct sahara_sha_reqctx
),
1262 .cra_name
= "sha256",
1263 .cra_driver_name
= "sahara-sha256",
1264 .cra_priority
= 300,
1265 .cra_flags
= CRYPTO_ALG_ASYNC
|
1266 CRYPTO_ALG_NEED_FALLBACK
,
1267 .cra_blocksize
= SHA256_BLOCK_SIZE
,
1268 .cra_ctxsize
= sizeof(struct sahara_ctx
),
1270 .cra_module
= THIS_MODULE
,
1271 .cra_init
= sahara_sha_cra_init
,
1276 static irqreturn_t
sahara_irq_handler(int irq
, void *data
)
1278 struct sahara_dev
*dev
= (struct sahara_dev
*)data
;
1279 unsigned int stat
= sahara_read(dev
, SAHARA_REG_STATUS
);
1280 unsigned int err
= sahara_read(dev
, SAHARA_REG_ERRSTATUS
);
1282 sahara_write(dev
, SAHARA_CMD_CLEAR_INT
| SAHARA_CMD_CLEAR_ERR
,
1285 sahara_decode_status(dev
, stat
);
1287 if (SAHARA_STATUS_GET_STATE(stat
) == SAHARA_STATE_BUSY
) {
1289 } else if (SAHARA_STATUS_GET_STATE(stat
) == SAHARA_STATE_COMPLETE
) {
1292 sahara_decode_error(dev
, err
);
1293 dev
->error
= -EINVAL
;
1296 complete(&dev
->dma_completion
);
1302 static int sahara_register_algs(struct sahara_dev
*dev
)
1305 unsigned int i
, j
, k
, l
;
1307 for (i
= 0; i
< ARRAY_SIZE(aes_algs
); i
++) {
1308 err
= crypto_register_skcipher(&aes_algs
[i
]);
1313 for (k
= 0; k
< ARRAY_SIZE(sha_v3_algs
); k
++) {
1314 err
= crypto_register_ahash(&sha_v3_algs
[k
]);
1316 goto err_sha_v3_algs
;
1319 if (dev
->version
> SAHARA_VERSION_3
)
1320 for (l
= 0; l
< ARRAY_SIZE(sha_v4_algs
); l
++) {
1321 err
= crypto_register_ahash(&sha_v4_algs
[l
]);
1323 goto err_sha_v4_algs
;
1329 for (j
= 0; j
< l
; j
++)
1330 crypto_unregister_ahash(&sha_v4_algs
[j
]);
1333 for (j
= 0; j
< k
; j
++)
1334 crypto_unregister_ahash(&sha_v3_algs
[j
]);
1337 for (j
= 0; j
< i
; j
++)
1338 crypto_unregister_skcipher(&aes_algs
[j
]);
1343 static void sahara_unregister_algs(struct sahara_dev
*dev
)
1347 for (i
= 0; i
< ARRAY_SIZE(aes_algs
); i
++)
1348 crypto_unregister_skcipher(&aes_algs
[i
]);
1350 for (i
= 0; i
< ARRAY_SIZE(sha_v3_algs
); i
++)
1351 crypto_unregister_ahash(&sha_v3_algs
[i
]);
1353 if (dev
->version
> SAHARA_VERSION_3
)
1354 for (i
= 0; i
< ARRAY_SIZE(sha_v4_algs
); i
++)
1355 crypto_unregister_ahash(&sha_v4_algs
[i
]);
1358 static const struct platform_device_id sahara_platform_ids
[] = {
1359 { .name
= "sahara-imx27" },
1362 MODULE_DEVICE_TABLE(platform
, sahara_platform_ids
);
1364 static const struct of_device_id sahara_dt_ids
[] = {
1365 { .compatible
= "fsl,imx53-sahara" },
1366 { .compatible
= "fsl,imx27-sahara" },
1369 MODULE_DEVICE_TABLE(of
, sahara_dt_ids
);
1371 static int sahara_probe(struct platform_device
*pdev
)
1373 struct sahara_dev
*dev
;
1379 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
1383 dev
->device
= &pdev
->dev
;
1384 platform_set_drvdata(pdev
, dev
);
1386 /* Get the base address */
1387 dev
->regs_base
= devm_platform_ioremap_resource(pdev
, 0);
1388 if (IS_ERR(dev
->regs_base
))
1389 return PTR_ERR(dev
->regs_base
);
1392 irq
= platform_get_irq(pdev
, 0);
1396 err
= devm_request_irq(&pdev
->dev
, irq
, sahara_irq_handler
,
1397 0, dev_name(&pdev
->dev
), dev
);
1399 dev_err(&pdev
->dev
, "failed to request irq\n");
1404 dev
->clk_ipg
= devm_clk_get(&pdev
->dev
, "ipg");
1405 if (IS_ERR(dev
->clk_ipg
)) {
1406 dev_err(&pdev
->dev
, "Could not get ipg clock\n");
1407 return PTR_ERR(dev
->clk_ipg
);
1410 dev
->clk_ahb
= devm_clk_get(&pdev
->dev
, "ahb");
1411 if (IS_ERR(dev
->clk_ahb
)) {
1412 dev_err(&pdev
->dev
, "Could not get ahb clock\n");
1413 return PTR_ERR(dev
->clk_ahb
);
1416 /* Allocate HW descriptors */
1417 dev
->hw_desc
[0] = dmam_alloc_coherent(&pdev
->dev
,
1418 SAHARA_MAX_HW_DESC
* sizeof(struct sahara_hw_desc
),
1419 &dev
->hw_phys_desc
[0], GFP_KERNEL
);
1420 if (!dev
->hw_desc
[0]) {
1421 dev_err(&pdev
->dev
, "Could not allocate hw descriptors\n");
1424 dev
->hw_desc
[1] = dev
->hw_desc
[0] + 1;
1425 dev
->hw_phys_desc
[1] = dev
->hw_phys_desc
[0] +
1426 sizeof(struct sahara_hw_desc
);
1428 /* Allocate space for iv and key */
1429 dev
->key_base
= dmam_alloc_coherent(&pdev
->dev
, 2 * AES_KEYSIZE_128
,
1430 &dev
->key_phys_base
, GFP_KERNEL
);
1431 if (!dev
->key_base
) {
1432 dev_err(&pdev
->dev
, "Could not allocate memory for key\n");
1435 dev
->iv_base
= dev
->key_base
+ AES_KEYSIZE_128
;
1436 dev
->iv_phys_base
= dev
->key_phys_base
+ AES_KEYSIZE_128
;
1438 /* Allocate space for context: largest digest + message length field */
1439 dev
->context_base
= dmam_alloc_coherent(&pdev
->dev
,
1440 SHA256_DIGEST_SIZE
+ 4,
1441 &dev
->context_phys_base
, GFP_KERNEL
);
1442 if (!dev
->context_base
) {
1443 dev_err(&pdev
->dev
, "Could not allocate memory for MDHA context\n");
1447 /* Allocate space for HW links */
1448 dev
->hw_link
[0] = dmam_alloc_coherent(&pdev
->dev
,
1449 SAHARA_MAX_HW_LINK
* sizeof(struct sahara_hw_link
),
1450 &dev
->hw_phys_link
[0], GFP_KERNEL
);
1451 if (!dev
->hw_link
[0]) {
1452 dev_err(&pdev
->dev
, "Could not allocate hw links\n");
1455 for (i
= 1; i
< SAHARA_MAX_HW_LINK
; i
++) {
1456 dev
->hw_phys_link
[i
] = dev
->hw_phys_link
[i
- 1] +
1457 sizeof(struct sahara_hw_link
);
1458 dev
->hw_link
[i
] = dev
->hw_link
[i
- 1] + 1;
1461 crypto_init_queue(&dev
->queue
, SAHARA_QUEUE_LENGTH
);
1463 mutex_init(&dev
->queue_mutex
);
1467 dev
->kthread
= kthread_run(sahara_queue_manage
, dev
, "sahara_crypto");
1468 if (IS_ERR(dev
->kthread
)) {
1469 return PTR_ERR(dev
->kthread
);
1472 init_completion(&dev
->dma_completion
);
1474 err
= clk_prepare_enable(dev
->clk_ipg
);
1477 err
= clk_prepare_enable(dev
->clk_ahb
);
1479 goto clk_ipg_disable
;
1481 version
= sahara_read(dev
, SAHARA_REG_VERSION
);
1482 if (of_device_is_compatible(pdev
->dev
.of_node
, "fsl,imx27-sahara")) {
1483 if (version
!= SAHARA_VERSION_3
)
1485 } else if (of_device_is_compatible(pdev
->dev
.of_node
,
1486 "fsl,imx53-sahara")) {
1487 if (((version
>> 8) & 0xff) != SAHARA_VERSION_4
)
1489 version
= (version
>> 8) & 0xff;
1491 if (err
== -ENODEV
) {
1492 dev_err(&pdev
->dev
, "SAHARA version %d not supported\n",
1497 dev
->version
= version
;
1499 sahara_write(dev
, SAHARA_CMD_RESET
| SAHARA_CMD_MODE_BATCH
,
1501 sahara_write(dev
, SAHARA_CONTROL_SET_THROTTLE(0) |
1502 SAHARA_CONTROL_SET_MAXBURST(8) |
1503 SAHARA_CONTROL_RNG_AUTORSD
|
1504 SAHARA_CONTROL_ENABLE_INT
,
1505 SAHARA_REG_CONTROL
);
1507 err
= sahara_register_algs(dev
);
1511 dev_info(&pdev
->dev
, "SAHARA version %d initialized\n", version
);
1516 kthread_stop(dev
->kthread
);
1518 clk_disable_unprepare(dev
->clk_ahb
);
1520 clk_disable_unprepare(dev
->clk_ipg
);
1525 static int sahara_remove(struct platform_device
*pdev
)
1527 struct sahara_dev
*dev
= platform_get_drvdata(pdev
);
1529 kthread_stop(dev
->kthread
);
1531 sahara_unregister_algs(dev
);
1533 clk_disable_unprepare(dev
->clk_ipg
);
1534 clk_disable_unprepare(dev
->clk_ahb
);
1541 static struct platform_driver sahara_driver
= {
1542 .probe
= sahara_probe
,
1543 .remove
= sahara_remove
,
1545 .name
= SAHARA_NAME
,
1546 .of_match_table
= sahara_dt_ids
,
1548 .id_table
= sahara_platform_ids
,
1551 module_platform_driver(sahara_driver
);
1553 MODULE_LICENSE("GPL");
1554 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
1555 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
1556 MODULE_DESCRIPTION("SAHARA2 HW crypto accelerator");