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