blob: b926098f70ffdb213bbfb1267cf6e473cfd880b4 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * Freescale i.MX23/i.MX28 Data Co-Processor driver
3 *
4 * Copyright (C) 2013 Marek Vasut <marex@denx.de>
5 *
6 * The code contained herein is licensed under the GNU General Public
7 * License. You may obtain a copy of the GNU General Public License
8 * Version 2 or later at the following locations:
9 *
10 * http://www.opensource.org/licenses/gpl-license.html
11 * http://www.gnu.org/copyleft/gpl.html
12 */
13
14#include <linux/dma-mapping.h>
15#include <linux/interrupt.h>
16#include <linux/io.h>
17#include <linux/kernel.h>
18#include <linux/kthread.h>
19#include <linux/module.h>
20#include <linux/of.h>
21#include <linux/platform_device.h>
22#include <linux/stmp_device.h>
23
24#include <crypto/aes.h>
25#include <crypto/sha.h>
26#include <crypto/internal/hash.h>
27#include <crypto/internal/skcipher.h>
28
29#define DCP_MAX_CHANS 4
30#define DCP_BUF_SZ PAGE_SIZE
31#define DCP_SHA_PAY_SZ 64
32
33#define DCP_ALIGNMENT 64
34
35/*
36 * Null hashes to align with hw behavior on imx6sl and ull
37 * these are flipped for consistency with hw output
38 */
39const uint8_t sha1_null_hash[] =
40 "\x09\x07\xd8\xaf\x90\x18\x60\x95\xef\xbf"
41 "\x55\x32\x0d\x4b\x6b\x5e\xee\xa3\x39\xda";
42
43const uint8_t sha256_null_hash[] =
44 "\x55\xb8\x52\x78\x1b\x99\x95\xa4"
45 "\x4c\x93\x9b\x64\xe4\x41\xae\x27"
46 "\x24\xb9\x6f\x99\xc8\xf4\xfb\x9a"
47 "\x14\x1c\xfc\x98\x42\xc4\xb0\xe3";
48
49/* DCP DMA descriptor. */
50struct dcp_dma_desc {
51 uint32_t next_cmd_addr;
52 uint32_t control0;
53 uint32_t control1;
54 uint32_t source;
55 uint32_t destination;
56 uint32_t size;
57 uint32_t payload;
58 uint32_t status;
59};
60
61/* Coherent aligned block for bounce buffering. */
62struct dcp_coherent_block {
63 uint8_t aes_in_buf[DCP_BUF_SZ];
64 uint8_t aes_out_buf[DCP_BUF_SZ];
65 uint8_t sha_in_buf[DCP_BUF_SZ];
66 uint8_t sha_out_buf[DCP_SHA_PAY_SZ];
67
68 uint8_t aes_key[2 * AES_KEYSIZE_128];
69
70 struct dcp_dma_desc desc[DCP_MAX_CHANS];
71};
72
73struct dcp {
74 struct device *dev;
75 void __iomem *base;
76
77 uint32_t caps;
78
79 struct dcp_coherent_block *coh;
80
81 struct completion completion[DCP_MAX_CHANS];
82 spinlock_t lock[DCP_MAX_CHANS];
83 struct task_struct *thread[DCP_MAX_CHANS];
84 struct crypto_queue queue[DCP_MAX_CHANS];
85};
86
87enum dcp_chan {
88 DCP_CHAN_HASH_SHA = 0,
89 DCP_CHAN_CRYPTO = 2,
90};
91
92struct dcp_async_ctx {
93 /* Common context */
94 enum dcp_chan chan;
95 uint32_t fill;
96
97 /* SHA Hash-specific context */
98 struct mutex mutex;
99 uint32_t alg;
100 unsigned int hot:1;
101
102 /* Crypto-specific context */
103 struct crypto_skcipher *fallback;
104 unsigned int key_len;
105 uint8_t key[AES_KEYSIZE_128];
106};
107
108struct dcp_aes_req_ctx {
109 unsigned int enc:1;
110 unsigned int ecb:1;
111};
112
113struct dcp_sha_req_ctx {
114 unsigned int init:1;
115 unsigned int fini:1;
116};
117
118/*
119 * There can even be only one instance of the MXS DCP due to the
120 * design of Linux Crypto API.
121 */
122static struct dcp *global_sdcp;
123
124/* DCP register layout. */
125#define MXS_DCP_CTRL 0x00
126#define MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES (1 << 23)
127#define MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING (1 << 22)
128
129#define MXS_DCP_STAT 0x10
130#define MXS_DCP_STAT_CLR 0x18
131#define MXS_DCP_STAT_IRQ_MASK 0xf
132
133#define MXS_DCP_CHANNELCTRL 0x20
134#define MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK 0xff
135
136#define MXS_DCP_CAPABILITY1 0x40
137#define MXS_DCP_CAPABILITY1_SHA256 (4 << 16)
138#define MXS_DCP_CAPABILITY1_SHA1 (1 << 16)
139#define MXS_DCP_CAPABILITY1_AES128 (1 << 0)
140
141#define MXS_DCP_CONTEXT 0x50
142
143#define MXS_DCP_CH_N_CMDPTR(n) (0x100 + ((n) * 0x40))
144
145#define MXS_DCP_CH_N_SEMA(n) (0x110 + ((n) * 0x40))
146
147#define MXS_DCP_CH_N_STAT(n) (0x120 + ((n) * 0x40))
148#define MXS_DCP_CH_N_STAT_CLR(n) (0x128 + ((n) * 0x40))
149
150/* DMA descriptor bits. */
151#define MXS_DCP_CONTROL0_HASH_TERM (1 << 13)
152#define MXS_DCP_CONTROL0_HASH_INIT (1 << 12)
153#define MXS_DCP_CONTROL0_PAYLOAD_KEY (1 << 11)
154#define MXS_DCP_CONTROL0_CIPHER_ENCRYPT (1 << 8)
155#define MXS_DCP_CONTROL0_CIPHER_INIT (1 << 9)
156#define MXS_DCP_CONTROL0_ENABLE_HASH (1 << 6)
157#define MXS_DCP_CONTROL0_ENABLE_CIPHER (1 << 5)
158#define MXS_DCP_CONTROL0_DECR_SEMAPHORE (1 << 1)
159#define MXS_DCP_CONTROL0_INTERRUPT (1 << 0)
160
161#define MXS_DCP_CONTROL1_HASH_SELECT_SHA256 (2 << 16)
162#define MXS_DCP_CONTROL1_HASH_SELECT_SHA1 (0 << 16)
163#define MXS_DCP_CONTROL1_CIPHER_MODE_CBC (1 << 4)
164#define MXS_DCP_CONTROL1_CIPHER_MODE_ECB (0 << 4)
165#define MXS_DCP_CONTROL1_CIPHER_SELECT_AES128 (0 << 0)
166
167static int mxs_dcp_start_dma(struct dcp_async_ctx *actx)
168{
169 struct dcp *sdcp = global_sdcp;
170 const int chan = actx->chan;
171 uint32_t stat;
172 unsigned long ret;
173 struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
174
175 dma_addr_t desc_phys = dma_map_single(sdcp->dev, desc, sizeof(*desc),
176 DMA_TO_DEVICE);
177
178 reinit_completion(&sdcp->completion[chan]);
179
180 /* Clear status register. */
181 writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(chan));
182
183 /* Load the DMA descriptor. */
184 writel(desc_phys, sdcp->base + MXS_DCP_CH_N_CMDPTR(chan));
185
186 /* Increment the semaphore to start the DMA transfer. */
187 writel(1, sdcp->base + MXS_DCP_CH_N_SEMA(chan));
188
189 ret = wait_for_completion_timeout(&sdcp->completion[chan],
190 msecs_to_jiffies(1000));
191 if (!ret) {
192 dev_err(sdcp->dev, "Channel %i timeout (DCP_STAT=0x%08x)\n",
193 chan, readl(sdcp->base + MXS_DCP_STAT));
194 return -ETIMEDOUT;
195 }
196
197 stat = readl(sdcp->base + MXS_DCP_CH_N_STAT(chan));
198 if (stat & 0xff) {
199 dev_err(sdcp->dev, "Channel %i error (CH_STAT=0x%08x)\n",
200 chan, stat);
201 return -EINVAL;
202 }
203
204 dma_unmap_single(sdcp->dev, desc_phys, sizeof(*desc), DMA_TO_DEVICE);
205
206 return 0;
207}
208
209/*
210 * Encryption (AES128)
211 */
212static int mxs_dcp_run_aes(struct dcp_async_ctx *actx,
213 struct ablkcipher_request *req, int init)
214{
215 struct dcp *sdcp = global_sdcp;
216 struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
217 struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
218 int ret;
219
220 dma_addr_t key_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_key,
221 2 * AES_KEYSIZE_128,
222 DMA_TO_DEVICE);
223 dma_addr_t src_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_in_buf,
224 DCP_BUF_SZ, DMA_TO_DEVICE);
225 dma_addr_t dst_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_out_buf,
226 DCP_BUF_SZ, DMA_FROM_DEVICE);
227
228 if (actx->fill % AES_BLOCK_SIZE) {
229 dev_err(sdcp->dev, "Invalid block size!\n");
230 ret = -EINVAL;
231 goto aes_done_run;
232 }
233
234 /* Fill in the DMA descriptor. */
235 desc->control0 = MXS_DCP_CONTROL0_DECR_SEMAPHORE |
236 MXS_DCP_CONTROL0_INTERRUPT |
237 MXS_DCP_CONTROL0_ENABLE_CIPHER;
238
239 /* Payload contains the key. */
240 desc->control0 |= MXS_DCP_CONTROL0_PAYLOAD_KEY;
241
242 if (rctx->enc)
243 desc->control0 |= MXS_DCP_CONTROL0_CIPHER_ENCRYPT;
244 if (init)
245 desc->control0 |= MXS_DCP_CONTROL0_CIPHER_INIT;
246
247 desc->control1 = MXS_DCP_CONTROL1_CIPHER_SELECT_AES128;
248
249 if (rctx->ecb)
250 desc->control1 |= MXS_DCP_CONTROL1_CIPHER_MODE_ECB;
251 else
252 desc->control1 |= MXS_DCP_CONTROL1_CIPHER_MODE_CBC;
253
254 desc->next_cmd_addr = 0;
255 desc->source = src_phys;
256 desc->destination = dst_phys;
257 desc->size = actx->fill;
258 desc->payload = key_phys;
259 desc->status = 0;
260
261 ret = mxs_dcp_start_dma(actx);
262
263aes_done_run:
264 dma_unmap_single(sdcp->dev, key_phys, 2 * AES_KEYSIZE_128,
265 DMA_TO_DEVICE);
266 dma_unmap_single(sdcp->dev, src_phys, DCP_BUF_SZ, DMA_TO_DEVICE);
267 dma_unmap_single(sdcp->dev, dst_phys, DCP_BUF_SZ, DMA_FROM_DEVICE);
268
269 return ret;
270}
271
272static int mxs_dcp_aes_block_crypt(struct crypto_async_request *arq)
273{
274 struct dcp *sdcp = global_sdcp;
275
276 struct ablkcipher_request *req = ablkcipher_request_cast(arq);
277 struct dcp_async_ctx *actx = crypto_tfm_ctx(arq->tfm);
278 struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
279
280 struct scatterlist *dst = req->dst;
281 struct scatterlist *src = req->src;
282 const int nents = sg_nents(req->src);
283
284 const int out_off = DCP_BUF_SZ;
285 uint8_t *in_buf = sdcp->coh->aes_in_buf;
286 uint8_t *out_buf = sdcp->coh->aes_out_buf;
287
288 uint8_t *out_tmp, *src_buf, *dst_buf = NULL;
289 uint32_t dst_off = 0;
290 uint32_t last_out_len = 0;
291
292 uint8_t *key = sdcp->coh->aes_key;
293
294 int ret = 0;
295 int split = 0;
296 unsigned int i, len, clen, rem = 0, tlen = 0;
297 int init = 0;
298 bool limit_hit = false;
299
300 actx->fill = 0;
301
302 /* Copy the key from the temporary location. */
303 memcpy(key, actx->key, actx->key_len);
304
305 if (!rctx->ecb) {
306 /* Copy the CBC IV just past the key. */
307 memcpy(key + AES_KEYSIZE_128, req->info, AES_KEYSIZE_128);
308 /* CBC needs the INIT set. */
309 init = 1;
310 } else {
311 memset(key + AES_KEYSIZE_128, 0, AES_KEYSIZE_128);
312 }
313
314 for_each_sg(req->src, src, nents, i) {
315 src_buf = sg_virt(src);
316 len = sg_dma_len(src);
317 tlen += len;
318 limit_hit = tlen > req->nbytes;
319
320 if (limit_hit)
321 len = req->nbytes - (tlen - len);
322
323 do {
324 if (actx->fill + len > out_off)
325 clen = out_off - actx->fill;
326 else
327 clen = len;
328
329 memcpy(in_buf + actx->fill, src_buf, clen);
330 len -= clen;
331 src_buf += clen;
332 actx->fill += clen;
333
334 /*
335 * If we filled the buffer or this is the last SG,
336 * submit the buffer.
337 */
338 if (actx->fill == out_off || sg_is_last(src) ||
339 limit_hit) {
340 ret = mxs_dcp_run_aes(actx, req, init);
341 if (ret)
342 return ret;
343 init = 0;
344
345 out_tmp = out_buf;
346 last_out_len = actx->fill;
347 while (dst && actx->fill) {
348 if (!split) {
349 dst_buf = sg_virt(dst);
350 dst_off = 0;
351 }
352 rem = min(sg_dma_len(dst) - dst_off,
353 actx->fill);
354
355 memcpy(dst_buf + dst_off, out_tmp, rem);
356 out_tmp += rem;
357 dst_off += rem;
358 actx->fill -= rem;
359
360 if (dst_off == sg_dma_len(dst)) {
361 dst = sg_next(dst);
362 split = 0;
363 } else {
364 split = 1;
365 }
366 }
367 }
368 } while (len);
369
370 if (limit_hit)
371 break;
372 }
373
374 /* Copy the IV for CBC for chaining */
375 if (!rctx->ecb) {
376 if (rctx->enc)
377 memcpy(req->info, out_buf+(last_out_len-AES_BLOCK_SIZE),
378 AES_BLOCK_SIZE);
379 else
380 memcpy(req->info, in_buf+(last_out_len-AES_BLOCK_SIZE),
381 AES_BLOCK_SIZE);
382 }
383
384 return ret;
385}
386
387static int dcp_chan_thread_aes(void *data)
388{
389 struct dcp *sdcp = global_sdcp;
390 const int chan = DCP_CHAN_CRYPTO;
391
392 struct crypto_async_request *backlog;
393 struct crypto_async_request *arq;
394
395 int ret;
396
397 while (!kthread_should_stop()) {
398 set_current_state(TASK_INTERRUPTIBLE);
399
400 spin_lock(&sdcp->lock[chan]);
401 backlog = crypto_get_backlog(&sdcp->queue[chan]);
402 arq = crypto_dequeue_request(&sdcp->queue[chan]);
403 spin_unlock(&sdcp->lock[chan]);
404
405 if (!backlog && !arq) {
406 schedule();
407 continue;
408 }
409
410 set_current_state(TASK_RUNNING);
411
412 if (backlog)
413 backlog->complete(backlog, -EINPROGRESS);
414
415 if (arq) {
416 ret = mxs_dcp_aes_block_crypt(arq);
417 arq->complete(arq, ret);
418 }
419 }
420
421 return 0;
422}
423
424static int mxs_dcp_block_fallback(struct ablkcipher_request *req, int enc)
425{
426 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
427 struct dcp_async_ctx *ctx = crypto_ablkcipher_ctx(tfm);
428 SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
429 int ret;
430
431 skcipher_request_set_tfm(subreq, ctx->fallback);
432 skcipher_request_set_callback(subreq, req->base.flags, NULL, NULL);
433 skcipher_request_set_crypt(subreq, req->src, req->dst,
434 req->nbytes, req->info);
435
436 if (enc)
437 ret = crypto_skcipher_encrypt(subreq);
438 else
439 ret = crypto_skcipher_decrypt(subreq);
440
441 skcipher_request_zero(subreq);
442
443 return ret;
444}
445
446static int mxs_dcp_aes_enqueue(struct ablkcipher_request *req, int enc, int ecb)
447{
448 struct dcp *sdcp = global_sdcp;
449 struct crypto_async_request *arq = &req->base;
450 struct dcp_async_ctx *actx = crypto_tfm_ctx(arq->tfm);
451 struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
452 int ret;
453
454 if (unlikely(actx->key_len != AES_KEYSIZE_128))
455 return mxs_dcp_block_fallback(req, enc);
456
457 rctx->enc = enc;
458 rctx->ecb = ecb;
459 actx->chan = DCP_CHAN_CRYPTO;
460
461 spin_lock(&sdcp->lock[actx->chan]);
462 ret = crypto_enqueue_request(&sdcp->queue[actx->chan], &req->base);
463 spin_unlock(&sdcp->lock[actx->chan]);
464
465 wake_up_process(sdcp->thread[actx->chan]);
466
467 return -EINPROGRESS;
468}
469
470static int mxs_dcp_aes_ecb_decrypt(struct ablkcipher_request *req)
471{
472 return mxs_dcp_aes_enqueue(req, 0, 1);
473}
474
475static int mxs_dcp_aes_ecb_encrypt(struct ablkcipher_request *req)
476{
477 return mxs_dcp_aes_enqueue(req, 1, 1);
478}
479
480static int mxs_dcp_aes_cbc_decrypt(struct ablkcipher_request *req)
481{
482 return mxs_dcp_aes_enqueue(req, 0, 0);
483}
484
485static int mxs_dcp_aes_cbc_encrypt(struct ablkcipher_request *req)
486{
487 return mxs_dcp_aes_enqueue(req, 1, 0);
488}
489
490static int mxs_dcp_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
491 unsigned int len)
492{
493 struct dcp_async_ctx *actx = crypto_ablkcipher_ctx(tfm);
494 unsigned int ret;
495
496 /*
497 * AES 128 is supposed by the hardware, store key into temporary
498 * buffer and exit. We must use the temporary buffer here, since
499 * there can still be an operation in progress.
500 */
501 actx->key_len = len;
502 if (len == AES_KEYSIZE_128) {
503 memcpy(actx->key, key, len);
504 return 0;
505 }
506
507 /*
508 * If the requested AES key size is not supported by the hardware,
509 * but is supported by in-kernel software implementation, we use
510 * software fallback.
511 */
512 crypto_skcipher_clear_flags(actx->fallback, CRYPTO_TFM_REQ_MASK);
513 crypto_skcipher_set_flags(actx->fallback,
514 tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
515
516 ret = crypto_skcipher_setkey(actx->fallback, key, len);
517 if (!ret)
518 return 0;
519
520 tfm->base.crt_flags &= ~CRYPTO_TFM_RES_MASK;
521 tfm->base.crt_flags |= crypto_skcipher_get_flags(actx->fallback) &
522 CRYPTO_TFM_RES_MASK;
523
524 return ret;
525}
526
527static int mxs_dcp_aes_fallback_init(struct crypto_tfm *tfm)
528{
529 const char *name = crypto_tfm_alg_name(tfm);
530 const uint32_t flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK;
531 struct dcp_async_ctx *actx = crypto_tfm_ctx(tfm);
532 struct crypto_skcipher *blk;
533
534 blk = crypto_alloc_skcipher(name, 0, flags);
535 if (IS_ERR(blk))
536 return PTR_ERR(blk);
537
538 actx->fallback = blk;
539 tfm->crt_ablkcipher.reqsize = sizeof(struct dcp_aes_req_ctx);
540 return 0;
541}
542
543static void mxs_dcp_aes_fallback_exit(struct crypto_tfm *tfm)
544{
545 struct dcp_async_ctx *actx = crypto_tfm_ctx(tfm);
546
547 crypto_free_skcipher(actx->fallback);
548}
549
550/*
551 * Hashing (SHA1/SHA256)
552 */
553static int mxs_dcp_run_sha(struct ahash_request *req)
554{
555 struct dcp *sdcp = global_sdcp;
556 int ret;
557
558 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
559 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
560 struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
561 struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
562
563 dma_addr_t digest_phys = 0;
564 dma_addr_t buf_phys = dma_map_single(sdcp->dev, sdcp->coh->sha_in_buf,
565 DCP_BUF_SZ, DMA_TO_DEVICE);
566
567 /* Fill in the DMA descriptor. */
568 desc->control0 = MXS_DCP_CONTROL0_DECR_SEMAPHORE |
569 MXS_DCP_CONTROL0_INTERRUPT |
570 MXS_DCP_CONTROL0_ENABLE_HASH;
571 if (rctx->init)
572 desc->control0 |= MXS_DCP_CONTROL0_HASH_INIT;
573
574 desc->control1 = actx->alg;
575 desc->next_cmd_addr = 0;
576 desc->source = buf_phys;
577 desc->destination = 0;
578 desc->size = actx->fill;
579 desc->payload = 0;
580 desc->status = 0;
581
582 /*
583 * Align driver with hw behavior when generating null hashes
584 */
585 if (rctx->init && rctx->fini && desc->size == 0) {
586 struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
587 const uint8_t *sha_buf =
588 (actx->alg == MXS_DCP_CONTROL1_HASH_SELECT_SHA1) ?
589 sha1_null_hash : sha256_null_hash;
590 memcpy(sdcp->coh->sha_out_buf, sha_buf, halg->digestsize);
591 ret = 0;
592 goto done_run;
593 }
594
595 /* Set HASH_TERM bit for last transfer block. */
596 if (rctx->fini) {
597 digest_phys = dma_map_single(sdcp->dev, sdcp->coh->sha_out_buf,
598 DCP_SHA_PAY_SZ, DMA_FROM_DEVICE);
599 desc->control0 |= MXS_DCP_CONTROL0_HASH_TERM;
600 desc->payload = digest_phys;
601 }
602
603 ret = mxs_dcp_start_dma(actx);
604
605 if (rctx->fini)
606 dma_unmap_single(sdcp->dev, digest_phys, DCP_SHA_PAY_SZ,
607 DMA_FROM_DEVICE);
608
609done_run:
610 dma_unmap_single(sdcp->dev, buf_phys, DCP_BUF_SZ, DMA_TO_DEVICE);
611
612 return ret;
613}
614
615static int dcp_sha_req_to_buf(struct crypto_async_request *arq)
616{
617 struct dcp *sdcp = global_sdcp;
618
619 struct ahash_request *req = ahash_request_cast(arq);
620 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
621 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
622 struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
623 struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
624 const int nents = sg_nents(req->src);
625
626 uint8_t *in_buf = sdcp->coh->sha_in_buf;
627 uint8_t *out_buf = sdcp->coh->sha_out_buf;
628
629 uint8_t *src_buf;
630
631 struct scatterlist *src;
632
633 unsigned int i, len, clen;
634 int ret;
635
636 int fin = rctx->fini;
637 if (fin)
638 rctx->fini = 0;
639
640 for_each_sg(req->src, src, nents, i) {
641 src_buf = sg_virt(src);
642 len = sg_dma_len(src);
643
644 do {
645 if (actx->fill + len > DCP_BUF_SZ)
646 clen = DCP_BUF_SZ - actx->fill;
647 else
648 clen = len;
649
650 memcpy(in_buf + actx->fill, src_buf, clen);
651 len -= clen;
652 src_buf += clen;
653 actx->fill += clen;
654
655 /*
656 * If we filled the buffer and still have some
657 * more data, submit the buffer.
658 */
659 if (len && actx->fill == DCP_BUF_SZ) {
660 ret = mxs_dcp_run_sha(req);
661 if (ret)
662 return ret;
663 actx->fill = 0;
664 rctx->init = 0;
665 }
666 } while (len);
667 }
668
669 if (fin) {
670 rctx->fini = 1;
671
672 /* Submit whatever is left. */
673 if (!req->result)
674 return -EINVAL;
675
676 ret = mxs_dcp_run_sha(req);
677 if (ret)
678 return ret;
679
680 actx->fill = 0;
681
682 /* For some reason the result is flipped */
683 for (i = 0; i < halg->digestsize; i++)
684 req->result[i] = out_buf[halg->digestsize - i - 1];
685 }
686
687 return 0;
688}
689
690static int dcp_chan_thread_sha(void *data)
691{
692 struct dcp *sdcp = global_sdcp;
693 const int chan = DCP_CHAN_HASH_SHA;
694
695 struct crypto_async_request *backlog;
696 struct crypto_async_request *arq;
697
698 struct dcp_sha_req_ctx *rctx;
699
700 struct ahash_request *req;
701 int ret, fini;
702
703 while (!kthread_should_stop()) {
704 set_current_state(TASK_INTERRUPTIBLE);
705
706 spin_lock(&sdcp->lock[chan]);
707 backlog = crypto_get_backlog(&sdcp->queue[chan]);
708 arq = crypto_dequeue_request(&sdcp->queue[chan]);
709 spin_unlock(&sdcp->lock[chan]);
710
711 if (!backlog && !arq) {
712 schedule();
713 continue;
714 }
715
716 set_current_state(TASK_RUNNING);
717
718 if (backlog)
719 backlog->complete(backlog, -EINPROGRESS);
720
721 if (arq) {
722 req = ahash_request_cast(arq);
723 rctx = ahash_request_ctx(req);
724
725 ret = dcp_sha_req_to_buf(arq);
726 fini = rctx->fini;
727 arq->complete(arq, ret);
728 }
729 }
730
731 return 0;
732}
733
734static int dcp_sha_init(struct ahash_request *req)
735{
736 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
737 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
738
739 struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
740
741 /*
742 * Start hashing session. The code below only inits the
743 * hashing session context, nothing more.
744 */
745 memset(actx, 0, sizeof(*actx));
746
747 if (strcmp(halg->base.cra_name, "sha1") == 0)
748 actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA1;
749 else
750 actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA256;
751
752 actx->fill = 0;
753 actx->hot = 0;
754 actx->chan = DCP_CHAN_HASH_SHA;
755
756 mutex_init(&actx->mutex);
757
758 return 0;
759}
760
761static int dcp_sha_update_fx(struct ahash_request *req, int fini)
762{
763 struct dcp *sdcp = global_sdcp;
764
765 struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
766 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
767 struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
768
769 int ret;
770
771 /*
772 * Ignore requests that have no data in them and are not
773 * the trailing requests in the stream of requests.
774 */
775 if (!req->nbytes && !fini)
776 return 0;
777
778 mutex_lock(&actx->mutex);
779
780 rctx->fini = fini;
781
782 if (!actx->hot) {
783 actx->hot = 1;
784 rctx->init = 1;
785 }
786
787 spin_lock(&sdcp->lock[actx->chan]);
788 ret = crypto_enqueue_request(&sdcp->queue[actx->chan], &req->base);
789 spin_unlock(&sdcp->lock[actx->chan]);
790
791 wake_up_process(sdcp->thread[actx->chan]);
792 mutex_unlock(&actx->mutex);
793
794 return -EINPROGRESS;
795}
796
797static int dcp_sha_update(struct ahash_request *req)
798{
799 return dcp_sha_update_fx(req, 0);
800}
801
802static int dcp_sha_final(struct ahash_request *req)
803{
804 ahash_request_set_crypt(req, NULL, req->result, 0);
805 req->nbytes = 0;
806 return dcp_sha_update_fx(req, 1);
807}
808
809static int dcp_sha_finup(struct ahash_request *req)
810{
811 return dcp_sha_update_fx(req, 1);
812}
813
814static int dcp_sha_digest(struct ahash_request *req)
815{
816 int ret;
817
818 ret = dcp_sha_init(req);
819 if (ret)
820 return ret;
821
822 return dcp_sha_finup(req);
823}
824
825static int dcp_sha_noimport(struct ahash_request *req, const void *in)
826{
827 return -ENOSYS;
828}
829
830static int dcp_sha_noexport(struct ahash_request *req, void *out)
831{
832 return -ENOSYS;
833}
834
835static int dcp_sha_cra_init(struct crypto_tfm *tfm)
836{
837 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
838 sizeof(struct dcp_sha_req_ctx));
839 return 0;
840}
841
842static void dcp_sha_cra_exit(struct crypto_tfm *tfm)
843{
844}
845
846/* AES 128 ECB and AES 128 CBC */
847static struct crypto_alg dcp_aes_algs[] = {
848 {
849 .cra_name = "ecb(aes)",
850 .cra_driver_name = "ecb-aes-dcp",
851 .cra_priority = 400,
852 .cra_alignmask = 15,
853 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
854 CRYPTO_ALG_ASYNC |
855 CRYPTO_ALG_NEED_FALLBACK,
856 .cra_init = mxs_dcp_aes_fallback_init,
857 .cra_exit = mxs_dcp_aes_fallback_exit,
858 .cra_blocksize = AES_BLOCK_SIZE,
859 .cra_ctxsize = sizeof(struct dcp_async_ctx),
860 .cra_type = &crypto_ablkcipher_type,
861 .cra_module = THIS_MODULE,
862 .cra_u = {
863 .ablkcipher = {
864 .min_keysize = AES_MIN_KEY_SIZE,
865 .max_keysize = AES_MAX_KEY_SIZE,
866 .setkey = mxs_dcp_aes_setkey,
867 .encrypt = mxs_dcp_aes_ecb_encrypt,
868 .decrypt = mxs_dcp_aes_ecb_decrypt
869 },
870 },
871 }, {
872 .cra_name = "cbc(aes)",
873 .cra_driver_name = "cbc-aes-dcp",
874 .cra_priority = 400,
875 .cra_alignmask = 15,
876 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
877 CRYPTO_ALG_ASYNC |
878 CRYPTO_ALG_NEED_FALLBACK,
879 .cra_init = mxs_dcp_aes_fallback_init,
880 .cra_exit = mxs_dcp_aes_fallback_exit,
881 .cra_blocksize = AES_BLOCK_SIZE,
882 .cra_ctxsize = sizeof(struct dcp_async_ctx),
883 .cra_type = &crypto_ablkcipher_type,
884 .cra_module = THIS_MODULE,
885 .cra_u = {
886 .ablkcipher = {
887 .min_keysize = AES_MIN_KEY_SIZE,
888 .max_keysize = AES_MAX_KEY_SIZE,
889 .setkey = mxs_dcp_aes_setkey,
890 .encrypt = mxs_dcp_aes_cbc_encrypt,
891 .decrypt = mxs_dcp_aes_cbc_decrypt,
892 .ivsize = AES_BLOCK_SIZE,
893 },
894 },
895 },
896};
897
898/* SHA1 */
899static struct ahash_alg dcp_sha1_alg = {
900 .init = dcp_sha_init,
901 .update = dcp_sha_update,
902 .final = dcp_sha_final,
903 .finup = dcp_sha_finup,
904 .digest = dcp_sha_digest,
905 .import = dcp_sha_noimport,
906 .export = dcp_sha_noexport,
907 .halg = {
908 .digestsize = SHA1_DIGEST_SIZE,
909 .base = {
910 .cra_name = "sha1",
911 .cra_driver_name = "sha1-dcp",
912 .cra_priority = 400,
913 .cra_alignmask = 63,
914 .cra_flags = CRYPTO_ALG_ASYNC,
915 .cra_blocksize = SHA1_BLOCK_SIZE,
916 .cra_ctxsize = sizeof(struct dcp_async_ctx),
917 .cra_module = THIS_MODULE,
918 .cra_init = dcp_sha_cra_init,
919 .cra_exit = dcp_sha_cra_exit,
920 },
921 },
922};
923
924/* SHA256 */
925static struct ahash_alg dcp_sha256_alg = {
926 .init = dcp_sha_init,
927 .update = dcp_sha_update,
928 .final = dcp_sha_final,
929 .finup = dcp_sha_finup,
930 .digest = dcp_sha_digest,
931 .import = dcp_sha_noimport,
932 .export = dcp_sha_noexport,
933 .halg = {
934 .digestsize = SHA256_DIGEST_SIZE,
935 .base = {
936 .cra_name = "sha256",
937 .cra_driver_name = "sha256-dcp",
938 .cra_priority = 400,
939 .cra_alignmask = 63,
940 .cra_flags = CRYPTO_ALG_ASYNC,
941 .cra_blocksize = SHA256_BLOCK_SIZE,
942 .cra_ctxsize = sizeof(struct dcp_async_ctx),
943 .cra_module = THIS_MODULE,
944 .cra_init = dcp_sha_cra_init,
945 .cra_exit = dcp_sha_cra_exit,
946 },
947 },
948};
949
950static irqreturn_t mxs_dcp_irq(int irq, void *context)
951{
952 struct dcp *sdcp = context;
953 uint32_t stat;
954 int i;
955
956 stat = readl(sdcp->base + MXS_DCP_STAT);
957 stat &= MXS_DCP_STAT_IRQ_MASK;
958 if (!stat)
959 return IRQ_NONE;
960
961 /* Clear the interrupts. */
962 writel(stat, sdcp->base + MXS_DCP_STAT_CLR);
963
964 /* Complete the DMA requests that finished. */
965 for (i = 0; i < DCP_MAX_CHANS; i++)
966 if (stat & (1 << i))
967 complete(&sdcp->completion[i]);
968
969 return IRQ_HANDLED;
970}
971
972static int mxs_dcp_probe(struct platform_device *pdev)
973{
974 struct device *dev = &pdev->dev;
975 struct dcp *sdcp = NULL;
976 int i, ret;
977
978 struct resource *iores;
979 int dcp_vmi_irq, dcp_irq;
980
981 if (global_sdcp) {
982 dev_err(dev, "Only one DCP instance allowed!\n");
983 return -ENODEV;
984 }
985
986 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
987 dcp_vmi_irq = platform_get_irq(pdev, 0);
988 if (dcp_vmi_irq < 0) {
989 dev_err(dev, "Failed to get IRQ: (%d)!\n", dcp_vmi_irq);
990 return dcp_vmi_irq;
991 }
992
993 dcp_irq = platform_get_irq(pdev, 1);
994 if (dcp_irq < 0) {
995 dev_err(dev, "Failed to get IRQ: (%d)!\n", dcp_irq);
996 return dcp_irq;
997 }
998
999 sdcp = devm_kzalloc(dev, sizeof(*sdcp), GFP_KERNEL);
1000 if (!sdcp)
1001 return -ENOMEM;
1002
1003 sdcp->dev = dev;
1004 sdcp->base = devm_ioremap_resource(dev, iores);
1005 if (IS_ERR(sdcp->base))
1006 return PTR_ERR(sdcp->base);
1007
1008
1009 ret = devm_request_irq(dev, dcp_vmi_irq, mxs_dcp_irq, 0,
1010 "dcp-vmi-irq", sdcp);
1011 if (ret) {
1012 dev_err(dev, "Failed to claim DCP VMI IRQ!\n");
1013 return ret;
1014 }
1015
1016 ret = devm_request_irq(dev, dcp_irq, mxs_dcp_irq, 0,
1017 "dcp-irq", sdcp);
1018 if (ret) {
1019 dev_err(dev, "Failed to claim DCP IRQ!\n");
1020 return ret;
1021 }
1022
1023 /* Allocate coherent helper block. */
1024 sdcp->coh = devm_kzalloc(dev, sizeof(*sdcp->coh) + DCP_ALIGNMENT,
1025 GFP_KERNEL);
1026 if (!sdcp->coh)
1027 return -ENOMEM;
1028
1029 /* Re-align the structure so it fits the DCP constraints. */
1030 sdcp->coh = PTR_ALIGN(sdcp->coh, DCP_ALIGNMENT);
1031
1032 /* Restart the DCP block. */
1033 ret = stmp_reset_block(sdcp->base);
1034 if (ret)
1035 return ret;
1036
1037 /* Initialize control register. */
1038 writel(MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES |
1039 MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING | 0xf,
1040 sdcp->base + MXS_DCP_CTRL);
1041
1042 /* Enable all DCP DMA channels. */
1043 writel(MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK,
1044 sdcp->base + MXS_DCP_CHANNELCTRL);
1045
1046 /*
1047 * We do not enable context switching. Give the context buffer a
1048 * pointer to an illegal address so if context switching is
1049 * inadvertantly enabled, the DCP will return an error instead of
1050 * trashing good memory. The DCP DMA cannot access ROM, so any ROM
1051 * address will do.
1052 */
1053 writel(0xffff0000, sdcp->base + MXS_DCP_CONTEXT);
1054 for (i = 0; i < DCP_MAX_CHANS; i++)
1055 writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(i));
1056 writel(0xffffffff, sdcp->base + MXS_DCP_STAT_CLR);
1057
1058 global_sdcp = sdcp;
1059
1060 platform_set_drvdata(pdev, sdcp);
1061
1062 for (i = 0; i < DCP_MAX_CHANS; i++) {
1063 spin_lock_init(&sdcp->lock[i]);
1064 init_completion(&sdcp->completion[i]);
1065 crypto_init_queue(&sdcp->queue[i], 50);
1066 }
1067
1068 /* Create the SHA and AES handler threads. */
1069 sdcp->thread[DCP_CHAN_HASH_SHA] = kthread_run(dcp_chan_thread_sha,
1070 NULL, "mxs_dcp_chan/sha");
1071 if (IS_ERR(sdcp->thread[DCP_CHAN_HASH_SHA])) {
1072 dev_err(dev, "Error starting SHA thread!\n");
1073 return PTR_ERR(sdcp->thread[DCP_CHAN_HASH_SHA]);
1074 }
1075
1076 sdcp->thread[DCP_CHAN_CRYPTO] = kthread_run(dcp_chan_thread_aes,
1077 NULL, "mxs_dcp_chan/aes");
1078 if (IS_ERR(sdcp->thread[DCP_CHAN_CRYPTO])) {
1079 dev_err(dev, "Error starting SHA thread!\n");
1080 ret = PTR_ERR(sdcp->thread[DCP_CHAN_CRYPTO]);
1081 goto err_destroy_sha_thread;
1082 }
1083
1084 /* Register the various crypto algorithms. */
1085 sdcp->caps = readl(sdcp->base + MXS_DCP_CAPABILITY1);
1086
1087 if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128) {
1088 ret = crypto_register_algs(dcp_aes_algs,
1089 ARRAY_SIZE(dcp_aes_algs));
1090 if (ret) {
1091 /* Failed to register algorithm. */
1092 dev_err(dev, "Failed to register AES crypto!\n");
1093 goto err_destroy_aes_thread;
1094 }
1095 }
1096
1097 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1) {
1098 ret = crypto_register_ahash(&dcp_sha1_alg);
1099 if (ret) {
1100 dev_err(dev, "Failed to register %s hash!\n",
1101 dcp_sha1_alg.halg.base.cra_name);
1102 goto err_unregister_aes;
1103 }
1104 }
1105
1106 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256) {
1107 ret = crypto_register_ahash(&dcp_sha256_alg);
1108 if (ret) {
1109 dev_err(dev, "Failed to register %s hash!\n",
1110 dcp_sha256_alg.halg.base.cra_name);
1111 goto err_unregister_sha1;
1112 }
1113 }
1114
1115 return 0;
1116
1117err_unregister_sha1:
1118 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1)
1119 crypto_unregister_ahash(&dcp_sha1_alg);
1120
1121err_unregister_aes:
1122 if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128)
1123 crypto_unregister_algs(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs));
1124
1125err_destroy_aes_thread:
1126 kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]);
1127
1128err_destroy_sha_thread:
1129 kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]);
1130 return ret;
1131}
1132
1133static int mxs_dcp_remove(struct platform_device *pdev)
1134{
1135 struct dcp *sdcp = platform_get_drvdata(pdev);
1136
1137 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256)
1138 crypto_unregister_ahash(&dcp_sha256_alg);
1139
1140 if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1)
1141 crypto_unregister_ahash(&dcp_sha1_alg);
1142
1143 if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128)
1144 crypto_unregister_algs(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs));
1145
1146 kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]);
1147 kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]);
1148
1149 platform_set_drvdata(pdev, NULL);
1150
1151 global_sdcp = NULL;
1152
1153 return 0;
1154}
1155
1156static const struct of_device_id mxs_dcp_dt_ids[] = {
1157 { .compatible = "fsl,imx23-dcp", .data = NULL, },
1158 { .compatible = "fsl,imx28-dcp", .data = NULL, },
1159 { /* sentinel */ }
1160};
1161
1162MODULE_DEVICE_TABLE(of, mxs_dcp_dt_ids);
1163
1164static struct platform_driver mxs_dcp_driver = {
1165 .probe = mxs_dcp_probe,
1166 .remove = mxs_dcp_remove,
1167 .driver = {
1168 .name = "mxs-dcp",
1169 .of_match_table = mxs_dcp_dt_ids,
1170 },
1171};
1172
1173module_platform_driver(mxs_dcp_driver);
1174
1175MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
1176MODULE_DESCRIPTION("Freescale MXS DCP Driver");
1177MODULE_LICENSE("GPL");
1178MODULE_ALIAS("platform:mxs-dcp");