GNU Linux-libre 4.19.314-gnu1
[releases.git] / drivers / crypto / sahara.c
1 /*
2  * Cryptographic API.
3  *
4  * Support for SAHARA cryptographic accelerator.
5  *
6  * Copyright (c) 2014 Steffen Trumtrar <s.trumtrar@pengutronix.de>
7  * Copyright (c) 2013 Vista Silicon S.L.
8  * Author: Javier Martin <javier.martin@vista-silicon.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as published
12  * by the Free Software Foundation.
13  *
14  * Based on omap-aes.c and tegra-aes.c
15  */
16
17 #include <crypto/aes.h>
18 #include <crypto/internal/hash.h>
19 #include <crypto/internal/skcipher.h>
20 #include <crypto/scatterwalk.h>
21 #include <crypto/sha.h>
22
23 #include <linux/clk.h>
24 #include <linux/crypto.h>
25 #include <linux/interrupt.h>
26 #include <linux/io.h>
27 #include <linux/irq.h>
28 #include <linux/kernel.h>
29 #include <linux/kthread.h>
30 #include <linux/module.h>
31 #include <linux/mutex.h>
32 #include <linux/of.h>
33 #include <linux/of_device.h>
34 #include <linux/platform_device.h>
35
36 #define SHA_BUFFER_LEN          PAGE_SIZE
37 #define SAHARA_MAX_SHA_BLOCK_SIZE       SHA256_BLOCK_SIZE
38
39 #define SAHARA_NAME "sahara"
40 #define SAHARA_VERSION_3        3
41 #define SAHARA_VERSION_4        4
42 #define SAHARA_TIMEOUT_MS       1000
43 #define SAHARA_MAX_HW_DESC      2
44 #define SAHARA_MAX_HW_LINK      20
45
46 #define FLAGS_MODE_MASK         0x000f
47 #define FLAGS_ENCRYPT           BIT(0)
48 #define FLAGS_CBC               BIT(1)
49
50 #define SAHARA_HDR_BASE                 0x00800000
51 #define SAHARA_HDR_SKHA_ALG_AES 0
52 #define SAHARA_HDR_SKHA_OP_ENC          (1 << 2)
53 #define SAHARA_HDR_SKHA_MODE_ECB        (0 << 3)
54 #define SAHARA_HDR_SKHA_MODE_CBC        (1 << 3)
55 #define SAHARA_HDR_FORM_DATA            (5 << 16)
56 #define SAHARA_HDR_FORM_KEY             (8 << 16)
57 #define SAHARA_HDR_LLO                  (1 << 24)
58 #define SAHARA_HDR_CHA_SKHA             (1 << 28)
59 #define SAHARA_HDR_CHA_MDHA             (2 << 28)
60 #define SAHARA_HDR_PARITY_BIT           (1 << 31)
61
62 #define SAHARA_HDR_MDHA_SET_MODE_MD_KEY 0x20880000
63 #define SAHARA_HDR_MDHA_SET_MODE_HASH   0x208D0000
64 #define SAHARA_HDR_MDHA_HASH            0xA0850000
65 #define SAHARA_HDR_MDHA_STORE_DIGEST    0x20820000
66 #define SAHARA_HDR_MDHA_ALG_SHA1        0
67 #define SAHARA_HDR_MDHA_ALG_MD5         1
68 #define SAHARA_HDR_MDHA_ALG_SHA256      2
69 #define SAHARA_HDR_MDHA_ALG_SHA224      3
70 #define SAHARA_HDR_MDHA_PDATA           (1 << 2)
71 #define SAHARA_HDR_MDHA_HMAC            (1 << 3)
72 #define SAHARA_HDR_MDHA_INIT            (1 << 5)
73 #define SAHARA_HDR_MDHA_IPAD            (1 << 6)
74 #define SAHARA_HDR_MDHA_OPAD            (1 << 7)
75 #define SAHARA_HDR_MDHA_SWAP            (1 << 8)
76 #define SAHARA_HDR_MDHA_MAC_FULL        (1 << 9)
77 #define SAHARA_HDR_MDHA_SSL             (1 << 10)
78
79 /* SAHARA can only process one request at a time */
80 #define SAHARA_QUEUE_LENGTH     1
81
82 #define SAHARA_REG_VERSION      0x00
83 #define SAHARA_REG_DAR          0x04
84 #define SAHARA_REG_CONTROL      0x08
85 #define         SAHARA_CONTROL_SET_THROTTLE(x)  (((x) & 0xff) << 24)
86 #define         SAHARA_CONTROL_SET_MAXBURST(x)  (((x) & 0xff) << 16)
87 #define         SAHARA_CONTROL_RNG_AUTORSD      (1 << 7)
88 #define         SAHARA_CONTROL_ENABLE_INT       (1 << 4)
89 #define SAHARA_REG_CMD          0x0C
90 #define         SAHARA_CMD_RESET                (1 << 0)
91 #define         SAHARA_CMD_CLEAR_INT            (1 << 8)
92 #define         SAHARA_CMD_CLEAR_ERR            (1 << 9)
93 #define         SAHARA_CMD_SINGLE_STEP          (1 << 10)
94 #define         SAHARA_CMD_MODE_BATCH           (1 << 16)
95 #define         SAHARA_CMD_MODE_DEBUG           (1 << 18)
96 #define SAHARA_REG_STATUS       0x10
97 #define         SAHARA_STATUS_GET_STATE(x)      ((x) & 0x7)
98 #define                 SAHARA_STATE_IDLE       0
99 #define                 SAHARA_STATE_BUSY       1
100 #define                 SAHARA_STATE_ERR        2
101 #define                 SAHARA_STATE_FAULT      3
102 #define                 SAHARA_STATE_COMPLETE   4
103 #define                 SAHARA_STATE_COMP_FLAG  (1 << 2)
104 #define         SAHARA_STATUS_DAR_FULL          (1 << 3)
105 #define         SAHARA_STATUS_ERROR             (1 << 4)
106 #define         SAHARA_STATUS_SECURE            (1 << 5)
107 #define         SAHARA_STATUS_FAIL              (1 << 6)
108 #define         SAHARA_STATUS_INIT              (1 << 7)
109 #define         SAHARA_STATUS_RNG_RESEED        (1 << 8)
110 #define         SAHARA_STATUS_ACTIVE_RNG        (1 << 9)
111 #define         SAHARA_STATUS_ACTIVE_MDHA       (1 << 10)
112 #define         SAHARA_STATUS_ACTIVE_SKHA       (1 << 11)
113 #define         SAHARA_STATUS_MODE_BATCH        (1 << 16)
114 #define         SAHARA_STATUS_MODE_DEDICATED    (1 << 17)
115 #define         SAHARA_STATUS_MODE_DEBUG        (1 << 18)
116 #define         SAHARA_STATUS_GET_ISTATE(x)     (((x) >> 24) & 0xff)
117 #define SAHARA_REG_ERRSTATUS    0x14
118 #define         SAHARA_ERRSTATUS_GET_SOURCE(x)  ((x) & 0xf)
119 #define                 SAHARA_ERRSOURCE_CHA    14
120 #define                 SAHARA_ERRSOURCE_DMA    15
121 #define         SAHARA_ERRSTATUS_DMA_DIR        (1 << 8)
122 #define         SAHARA_ERRSTATUS_GET_DMASZ(x)(((x) >> 9) & 0x3)
123 #define         SAHARA_ERRSTATUS_GET_DMASRC(x) (((x) >> 13) & 0x7)
124 #define         SAHARA_ERRSTATUS_GET_CHASRC(x)  (((x) >> 16) & 0xfff)
125 #define         SAHARA_ERRSTATUS_GET_CHAERR(x)  (((x) >> 28) & 0x3)
126 #define SAHARA_REG_FADDR        0x18
127 #define SAHARA_REG_CDAR         0x1C
128 #define SAHARA_REG_IDAR         0x20
129
130 struct sahara_hw_desc {
131         u32     hdr;
132         u32     len1;
133         u32     p1;
134         u32     len2;
135         u32     p2;
136         u32     next;
137 };
138
139 struct sahara_hw_link {
140         u32     len;
141         u32     p;
142         u32     next;
143 };
144
145 struct sahara_ctx {
146         /* AES-specific context */
147         int keylen;
148         u8 key[AES_KEYSIZE_128];
149         struct crypto_skcipher *fallback;
150 };
151
152 struct sahara_aes_reqctx {
153         unsigned long mode;
154 };
155
156 /*
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
173  */
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];
178         unsigned int            mode;
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];
185         size_t                  total;
186         unsigned int            last;
187         unsigned int            first;
188         unsigned int            active;
189 };
190
191 struct sahara_dev {
192         struct device           *device;
193         unsigned int            version;
194         void __iomem            *regs_base;
195         struct clk              *clk_ipg;
196         struct clk              *clk_ahb;
197         struct mutex            queue_mutex;
198         struct task_struct      *kthread;
199         struct completion       dma_completion;
200
201         struct sahara_ctx       *ctx;
202         struct crypto_queue     queue;
203         unsigned long           flags;
204
205         struct sahara_hw_desc   *hw_desc[SAHARA_MAX_HW_DESC];
206         dma_addr_t              hw_phys_desc[SAHARA_MAX_HW_DESC];
207
208         u8                      *key_base;
209         dma_addr_t              key_phys_base;
210
211         u8                      *iv_base;
212         dma_addr_t              iv_phys_base;
213
214         u8                      *context_base;
215         dma_addr_t              context_phys_base;
216
217         struct sahara_hw_link   *hw_link[SAHARA_MAX_HW_LINK];
218         dma_addr_t              hw_phys_link[SAHARA_MAX_HW_LINK];
219
220         size_t                  total;
221         struct scatterlist      *in_sg;
222         int             nb_in_sg;
223         struct scatterlist      *out_sg;
224         int             nb_out_sg;
225
226         u32                     error;
227 };
228
229 static struct sahara_dev *dev_ptr;
230
231 static inline void sahara_write(struct sahara_dev *dev, u32 data, u32 reg)
232 {
233         writel(data, dev->regs_base + reg);
234 }
235
236 static inline unsigned int sahara_read(struct sahara_dev *dev, u32 reg)
237 {
238         return readl(dev->regs_base + reg);
239 }
240
241 static u32 sahara_aes_key_hdr(struct sahara_dev *dev)
242 {
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;
246
247         if (dev->flags & FLAGS_CBC) {
248                 hdr |= SAHARA_HDR_SKHA_MODE_CBC;
249                 hdr ^= SAHARA_HDR_PARITY_BIT;
250         }
251
252         if (dev->flags & FLAGS_ENCRYPT) {
253                 hdr |= SAHARA_HDR_SKHA_OP_ENC;
254                 hdr ^= SAHARA_HDR_PARITY_BIT;
255         }
256
257         return hdr;
258 }
259
260 static u32 sahara_aes_data_link_hdr(struct sahara_dev *dev)
261 {
262         return SAHARA_HDR_BASE | SAHARA_HDR_FORM_DATA |
263                         SAHARA_HDR_CHA_SKHA | SAHARA_HDR_PARITY_BIT;
264 }
265
266 static const char *sahara_err_src[16] = {
267         "No error",
268         "Header error",
269         "Descriptor length error",
270         "Descriptor length or pointer error",
271         "Link length 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",
278         "Reserved",
279         "Descriptor address error",
280         "Link address error",
281         "CHA error",
282         "DMA error"
283 };
284
285 static const char *sahara_err_dmasize[4] = {
286         "Byte transfer",
287         "Half-word transfer",
288         "Word transfer",
289         "Reserved"
290 };
291
292 static const char *sahara_err_dmasrc[8] = {
293         "No error",
294         "AHB bus error",
295         "Internal IP bus error",
296         "Parity error",
297         "DMA crosses 256 byte boundary",
298         "DMA is busy",
299         "Reserved",
300         "DMA HW error"
301 };
302
303 static const char *sahara_cha_errsrc[12] = {
304         "Input buffer non-empty",
305         "Illegal address",
306         "Illegal mode",
307         "Illegal data size",
308         "Illegal key size",
309         "Write during processing",
310         "CTX read during processing",
311         "HW error",
312         "Input buffer disabled/underflow",
313         "Output buffer disabled/overflow",
314         "DES key parity error",
315         "Reserved"
316 };
317
318 static const char *sahara_cha_err[4] = { "No error", "SKHA", "MDHA", "RNG" };
319
320 static void sahara_decode_error(struct sahara_dev *dev, unsigned int error)
321 {
322         u8 source = SAHARA_ERRSTATUS_GET_SOURCE(error);
323         u16 chasrc = ffs(SAHARA_ERRSTATUS_GET_CHASRC(error));
324
325         dev_err(dev->device, "%s: Error Register = 0x%08x\n", __func__, error);
326
327         dev_err(dev->device, "  - %s.\n", sahara_err_src[source]);
328
329         if (source == SAHARA_ERRSOURCE_DMA) {
330                 if (error & SAHARA_ERRSTATUS_DMA_DIR)
331                         dev_err(dev->device, "          * DMA read.\n");
332                 else
333                         dev_err(dev->device, "          * DMA write.\n");
334
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)]);
344         }
345         dev_err(dev->device, "\n");
346 }
347
348 static const char *sahara_state[4] = { "Idle", "Busy", "Error", "HW Fault" };
349
350 static void sahara_decode_status(struct sahara_dev *dev, unsigned int status)
351 {
352         u8 state;
353
354         if (!IS_ENABLED(DEBUG))
355                 return;
356
357         state = SAHARA_STATUS_GET_STATE(status);
358
359         dev_dbg(dev->device, "%s: Status Register = 0x%08x\n",
360                 __func__, status);
361
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");
365
366         dev_dbg(dev->device, "          * %s.\n",
367                sahara_state[state & ~SAHARA_STATE_COMP_FLAG]);
368
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");
385
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");
392
393         dev_dbg(dev->device, "  - Internal state = 0x%02x\n",
394                SAHARA_STATUS_GET_ISTATE(status));
395
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));
400 }
401
402 static void sahara_dump_descriptors(struct sahara_dev *dev)
403 {
404         int i;
405
406         if (!IS_ENABLED(DEBUG))
407                 return;
408
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);
419         }
420         dev_dbg(dev->device, "\n");
421 }
422
423 static void sahara_dump_links(struct sahara_dev *dev)
424 {
425         int i;
426
427         if (!IS_ENABLED(DEBUG))
428                 return;
429
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);
437         }
438         dev_dbg(dev->device, "\n");
439 }
440
441 static int sahara_hw_descriptor_create(struct sahara_dev *dev)
442 {
443         struct sahara_ctx *ctx = dev->ctx;
444         struct scatterlist *sg;
445         int ret;
446         int i, j;
447         int idx = 0;
448         u32 len;
449
450         memcpy(dev->key_base, ctx->key, ctx->keylen);
451
452         if (dev->flags & FLAGS_CBC) {
453                 dev->hw_desc[idx]->len1 = AES_BLOCK_SIZE;
454                 dev->hw_desc[idx]->p1 = dev->iv_phys_base;
455         } else {
456                 dev->hw_desc[idx]->len1 = 0;
457                 dev->hw_desc[idx]->p1 = 0;
458         }
459         dev->hw_desc[idx]->len2 = ctx->keylen;
460         dev->hw_desc[idx]->p2 = dev->key_phys_base;
461         dev->hw_desc[idx]->next = dev->hw_phys_desc[1];
462         dev->hw_desc[idx]->hdr = sahara_aes_key_hdr(dev);
463
464         idx++;
465
466
467         dev->nb_in_sg = sg_nents_for_len(dev->in_sg, dev->total);
468         if (dev->nb_in_sg < 0) {
469                 dev_err(dev->device, "Invalid numbers of src SG.\n");
470                 return dev->nb_in_sg;
471         }
472         dev->nb_out_sg = sg_nents_for_len(dev->out_sg, dev->total);
473         if (dev->nb_out_sg < 0) {
474                 dev_err(dev->device, "Invalid numbers of dst SG.\n");
475                 return dev->nb_out_sg;
476         }
477         if ((dev->nb_in_sg + dev->nb_out_sg) > SAHARA_MAX_HW_LINK) {
478                 dev_err(dev->device, "not enough hw links (%d)\n",
479                         dev->nb_in_sg + dev->nb_out_sg);
480                 return -EINVAL;
481         }
482
483         ret = dma_map_sg(dev->device, dev->in_sg, dev->nb_in_sg,
484                          DMA_TO_DEVICE);
485         if (ret != dev->nb_in_sg) {
486                 dev_err(dev->device, "couldn't map in sg\n");
487                 return -EINVAL;
488         }
489
490         ret = dma_map_sg(dev->device, dev->out_sg, dev->nb_out_sg,
491                          DMA_FROM_DEVICE);
492         if (ret != dev->nb_out_sg) {
493                 dev_err(dev->device, "couldn't map out sg\n");
494                 goto unmap_in;
495         }
496
497         /* Create input links */
498         dev->hw_desc[idx]->p1 = dev->hw_phys_link[0];
499         sg = dev->in_sg;
500         len = dev->total;
501         for (i = 0; i < dev->nb_in_sg; i++) {
502                 dev->hw_link[i]->len = min(len, sg->length);
503                 dev->hw_link[i]->p = sg->dma_address;
504                 if (i == (dev->nb_in_sg - 1)) {
505                         dev->hw_link[i]->next = 0;
506                 } else {
507                         len -= min(len, sg->length);
508                         dev->hw_link[i]->next = dev->hw_phys_link[i + 1];
509                         sg = sg_next(sg);
510                 }
511         }
512
513         /* Create output links */
514         dev->hw_desc[idx]->p2 = dev->hw_phys_link[i];
515         sg = dev->out_sg;
516         len = dev->total;
517         for (j = i; j < dev->nb_out_sg + i; j++) {
518                 dev->hw_link[j]->len = min(len, sg->length);
519                 dev->hw_link[j]->p = sg->dma_address;
520                 if (j == (dev->nb_out_sg + i - 1)) {
521                         dev->hw_link[j]->next = 0;
522                 } else {
523                         len -= min(len, sg->length);
524                         dev->hw_link[j]->next = dev->hw_phys_link[j + 1];
525                         sg = sg_next(sg);
526                 }
527         }
528
529         /* Fill remaining fields of hw_desc[1] */
530         dev->hw_desc[idx]->hdr = sahara_aes_data_link_hdr(dev);
531         dev->hw_desc[idx]->len1 = dev->total;
532         dev->hw_desc[idx]->len2 = dev->total;
533         dev->hw_desc[idx]->next = 0;
534
535         sahara_dump_descriptors(dev);
536         sahara_dump_links(dev);
537
538         sahara_write(dev, dev->hw_phys_desc[0], SAHARA_REG_DAR);
539
540         return 0;
541
542 unmap_in:
543         dma_unmap_sg(dev->device, dev->in_sg, dev->nb_in_sg,
544                 DMA_TO_DEVICE);
545
546         return -EINVAL;
547 }
548
549 static int sahara_aes_process(struct ablkcipher_request *req)
550 {
551         struct sahara_dev *dev = dev_ptr;
552         struct sahara_ctx *ctx;
553         struct sahara_aes_reqctx *rctx;
554         int ret;
555         unsigned long timeout;
556
557         /* Request is ready to be dispatched by the device */
558         dev_dbg(dev->device,
559                 "dispatch request (nbytes=%d, src=%p, dst=%p)\n",
560                 req->nbytes, req->src, req->dst);
561
562         /* assign new request to device */
563         dev->total = req->nbytes;
564         dev->in_sg = req->src;
565         dev->out_sg = req->dst;
566
567         rctx = ablkcipher_request_ctx(req);
568         ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
569         rctx->mode &= FLAGS_MODE_MASK;
570         dev->flags = (dev->flags & ~FLAGS_MODE_MASK) | rctx->mode;
571
572         if ((dev->flags & FLAGS_CBC) && req->info)
573                 memcpy(dev->iv_base, req->info, AES_KEYSIZE_128);
574
575         /* assign new context to device */
576         dev->ctx = ctx;
577
578         reinit_completion(&dev->dma_completion);
579
580         ret = sahara_hw_descriptor_create(dev);
581         if (ret)
582                 return -EINVAL;
583
584         timeout = wait_for_completion_timeout(&dev->dma_completion,
585                                 msecs_to_jiffies(SAHARA_TIMEOUT_MS));
586
587         dma_unmap_sg(dev->device, dev->out_sg, dev->nb_out_sg,
588                 DMA_FROM_DEVICE);
589         dma_unmap_sg(dev->device, dev->in_sg, dev->nb_in_sg,
590                 DMA_TO_DEVICE);
591
592         if (!timeout) {
593                 dev_err(dev->device, "AES timeout\n");
594                 return -ETIMEDOUT;
595         }
596
597         return 0;
598 }
599
600 static int sahara_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
601                              unsigned int keylen)
602 {
603         struct sahara_ctx *ctx = crypto_ablkcipher_ctx(tfm);
604         int ret;
605
606         ctx->keylen = keylen;
607
608         /* SAHARA only supports 128bit keys */
609         if (keylen == AES_KEYSIZE_128) {
610                 memcpy(ctx->key, key, keylen);
611                 return 0;
612         }
613
614         if (keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_256)
615                 return -EINVAL;
616
617         /*
618          * The requested key size is not supported by HW, do a fallback.
619          */
620         crypto_skcipher_clear_flags(ctx->fallback, CRYPTO_TFM_REQ_MASK);
621         crypto_skcipher_set_flags(ctx->fallback, tfm->base.crt_flags &
622                                                  CRYPTO_TFM_REQ_MASK);
623
624         ret = crypto_skcipher_setkey(ctx->fallback, key, keylen);
625
626         tfm->base.crt_flags &= ~CRYPTO_TFM_RES_MASK;
627         tfm->base.crt_flags |= crypto_skcipher_get_flags(ctx->fallback) &
628                                CRYPTO_TFM_RES_MASK;
629         return ret;
630 }
631
632 static int sahara_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
633 {
634         struct sahara_aes_reqctx *rctx = ablkcipher_request_ctx(req);
635         struct sahara_dev *dev = dev_ptr;
636         int err = 0;
637
638         dev_dbg(dev->device, "nbytes: %d, enc: %d, cbc: %d\n",
639                 req->nbytes, !!(mode & FLAGS_ENCRYPT), !!(mode & FLAGS_CBC));
640
641         if (!IS_ALIGNED(req->nbytes, AES_BLOCK_SIZE)) {
642                 dev_err(dev->device,
643                         "request size is not exact amount of AES blocks\n");
644                 return -EINVAL;
645         }
646
647         rctx->mode = mode;
648
649         mutex_lock(&dev->queue_mutex);
650         err = ablkcipher_enqueue_request(&dev->queue, req);
651         mutex_unlock(&dev->queue_mutex);
652
653         wake_up_process(dev->kthread);
654
655         return err;
656 }
657
658 static int sahara_aes_ecb_encrypt(struct ablkcipher_request *req)
659 {
660         struct sahara_ctx *ctx = crypto_ablkcipher_ctx(
661                 crypto_ablkcipher_reqtfm(req));
662         int err;
663
664         if (unlikely(ctx->keylen != AES_KEYSIZE_128)) {
665                 SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
666
667                 skcipher_request_set_tfm(subreq, ctx->fallback);
668                 skcipher_request_set_callback(subreq, req->base.flags,
669                                               NULL, NULL);
670                 skcipher_request_set_crypt(subreq, req->src, req->dst,
671                                            req->nbytes, req->info);
672                 err = crypto_skcipher_encrypt(subreq);
673                 skcipher_request_zero(subreq);
674                 return err;
675         }
676
677         return sahara_aes_crypt(req, FLAGS_ENCRYPT);
678 }
679
680 static int sahara_aes_ecb_decrypt(struct ablkcipher_request *req)
681 {
682         struct sahara_ctx *ctx = crypto_ablkcipher_ctx(
683                 crypto_ablkcipher_reqtfm(req));
684         int err;
685
686         if (unlikely(ctx->keylen != AES_KEYSIZE_128)) {
687                 SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
688
689                 skcipher_request_set_tfm(subreq, ctx->fallback);
690                 skcipher_request_set_callback(subreq, req->base.flags,
691                                               NULL, NULL);
692                 skcipher_request_set_crypt(subreq, req->src, req->dst,
693                                            req->nbytes, req->info);
694                 err = crypto_skcipher_decrypt(subreq);
695                 skcipher_request_zero(subreq);
696                 return err;
697         }
698
699         return sahara_aes_crypt(req, 0);
700 }
701
702 static int sahara_aes_cbc_encrypt(struct ablkcipher_request *req)
703 {
704         struct sahara_ctx *ctx = crypto_ablkcipher_ctx(
705                 crypto_ablkcipher_reqtfm(req));
706         int err;
707
708         if (unlikely(ctx->keylen != AES_KEYSIZE_128)) {
709                 SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
710
711                 skcipher_request_set_tfm(subreq, ctx->fallback);
712                 skcipher_request_set_callback(subreq, req->base.flags,
713                                               NULL, NULL);
714                 skcipher_request_set_crypt(subreq, req->src, req->dst,
715                                            req->nbytes, req->info);
716                 err = crypto_skcipher_encrypt(subreq);
717                 skcipher_request_zero(subreq);
718                 return err;
719         }
720
721         return sahara_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
722 }
723
724 static int sahara_aes_cbc_decrypt(struct ablkcipher_request *req)
725 {
726         struct sahara_ctx *ctx = crypto_ablkcipher_ctx(
727                 crypto_ablkcipher_reqtfm(req));
728         int err;
729
730         if (unlikely(ctx->keylen != AES_KEYSIZE_128)) {
731                 SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
732
733                 skcipher_request_set_tfm(subreq, ctx->fallback);
734                 skcipher_request_set_callback(subreq, req->base.flags,
735                                               NULL, NULL);
736                 skcipher_request_set_crypt(subreq, req->src, req->dst,
737                                            req->nbytes, req->info);
738                 err = crypto_skcipher_decrypt(subreq);
739                 skcipher_request_zero(subreq);
740                 return err;
741         }
742
743         return sahara_aes_crypt(req, FLAGS_CBC);
744 }
745
746 static int sahara_aes_cra_init(struct crypto_tfm *tfm)
747 {
748         const char *name = crypto_tfm_alg_name(tfm);
749         struct sahara_ctx *ctx = crypto_tfm_ctx(tfm);
750
751         ctx->fallback = crypto_alloc_skcipher(name, 0,
752                                               CRYPTO_ALG_ASYNC |
753                                               CRYPTO_ALG_NEED_FALLBACK);
754         if (IS_ERR(ctx->fallback)) {
755                 pr_err("Error allocating fallback algo %s\n", name);
756                 return PTR_ERR(ctx->fallback);
757         }
758
759         tfm->crt_ablkcipher.reqsize = sizeof(struct sahara_aes_reqctx);
760
761         return 0;
762 }
763
764 static void sahara_aes_cra_exit(struct crypto_tfm *tfm)
765 {
766         struct sahara_ctx *ctx = crypto_tfm_ctx(tfm);
767
768         crypto_free_skcipher(ctx->fallback);
769 }
770
771 static u32 sahara_sha_init_hdr(struct sahara_dev *dev,
772                               struct sahara_sha_reqctx *rctx)
773 {
774         u32 hdr = 0;
775
776         hdr = rctx->mode;
777
778         if (rctx->first) {
779                 hdr |= SAHARA_HDR_MDHA_SET_MODE_HASH;
780                 hdr |= SAHARA_HDR_MDHA_INIT;
781         } else {
782                 hdr |= SAHARA_HDR_MDHA_SET_MODE_MD_KEY;
783         }
784
785         if (rctx->last)
786                 hdr |= SAHARA_HDR_MDHA_PDATA;
787
788         if (hweight_long(hdr) % 2 == 0)
789                 hdr |= SAHARA_HDR_PARITY_BIT;
790
791         return hdr;
792 }
793
794 static int sahara_sha_hw_links_create(struct sahara_dev *dev,
795                                        struct sahara_sha_reqctx *rctx,
796                                        int start)
797 {
798         struct scatterlist *sg;
799         unsigned int len;
800         unsigned int i;
801         int ret;
802
803         dev->in_sg = rctx->in_sg;
804
805         dev->nb_in_sg = sg_nents_for_len(dev->in_sg, rctx->total);
806         if (dev->nb_in_sg < 0) {
807                 dev_err(dev->device, "Invalid numbers of src SG.\n");
808                 return dev->nb_in_sg;
809         }
810         if ((dev->nb_in_sg) > SAHARA_MAX_HW_LINK) {
811                 dev_err(dev->device, "not enough hw links (%d)\n",
812                         dev->nb_in_sg + dev->nb_out_sg);
813                 return -EINVAL;
814         }
815
816         sg = dev->in_sg;
817         ret = dma_map_sg(dev->device, dev->in_sg, dev->nb_in_sg, DMA_TO_DEVICE);
818         if (!ret)
819                 return -EFAULT;
820
821         len = rctx->total;
822         for (i = start; i < dev->nb_in_sg + start; i++) {
823                 dev->hw_link[i]->len = min(len, sg->length);
824                 dev->hw_link[i]->p = sg->dma_address;
825                 if (i == (dev->nb_in_sg + start - 1)) {
826                         dev->hw_link[i]->next = 0;
827                 } else {
828                         len -= min(len, sg->length);
829                         dev->hw_link[i]->next = dev->hw_phys_link[i + 1];
830                         sg = sg_next(sg);
831                 }
832         }
833
834         return i;
835 }
836
837 static int sahara_sha_hw_data_descriptor_create(struct sahara_dev *dev,
838                                                 struct sahara_sha_reqctx *rctx,
839                                                 struct ahash_request *req,
840                                                 int index)
841 {
842         unsigned result_len;
843         int i = index;
844
845         if (rctx->first)
846                 /* Create initial descriptor: #8*/
847                 dev->hw_desc[index]->hdr = sahara_sha_init_hdr(dev, rctx);
848         else
849                 /* Create hash descriptor: #10. Must follow #6. */
850                 dev->hw_desc[index]->hdr = SAHARA_HDR_MDHA_HASH;
851
852         dev->hw_desc[index]->len1 = rctx->total;
853         if (dev->hw_desc[index]->len1 == 0) {
854                 /* if len1 is 0, p1 must be 0, too */
855                 dev->hw_desc[index]->p1 = 0;
856                 rctx->sg_in_idx = 0;
857         } else {
858                 /* Create input links */
859                 dev->hw_desc[index]->p1 = dev->hw_phys_link[index];
860                 i = sahara_sha_hw_links_create(dev, rctx, index);
861
862                 rctx->sg_in_idx = index;
863                 if (i < 0)
864                         return i;
865         }
866
867         dev->hw_desc[index]->p2 = dev->hw_phys_link[i];
868
869         /* Save the context for the next operation */
870         result_len = rctx->context_size;
871         dev->hw_link[i]->p = dev->context_phys_base;
872
873         dev->hw_link[i]->len = result_len;
874         dev->hw_desc[index]->len2 = result_len;
875
876         dev->hw_link[i]->next = 0;
877
878         return 0;
879 }
880
881 /*
882  * Load descriptor aka #6
883  *
884  * To load a previously saved context back to the MDHA unit
885  *
886  * p1: Saved Context
887  * p2: NULL
888  *
889  */
890 static int sahara_sha_hw_context_descriptor_create(struct sahara_dev *dev,
891                                                 struct sahara_sha_reqctx *rctx,
892                                                 struct ahash_request *req,
893                                                 int index)
894 {
895         dev->hw_desc[index]->hdr = sahara_sha_init_hdr(dev, rctx);
896
897         dev->hw_desc[index]->len1 = rctx->context_size;
898         dev->hw_desc[index]->p1 = dev->hw_phys_link[index];
899         dev->hw_desc[index]->len2 = 0;
900         dev->hw_desc[index]->p2 = 0;
901
902         dev->hw_link[index]->len = rctx->context_size;
903         dev->hw_link[index]->p = dev->context_phys_base;
904         dev->hw_link[index]->next = 0;
905
906         return 0;
907 }
908
909 static int sahara_sha_prepare_request(struct ahash_request *req)
910 {
911         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
912         struct sahara_sha_reqctx *rctx = ahash_request_ctx(req);
913         unsigned int hash_later;
914         unsigned int block_size;
915         unsigned int len;
916
917         block_size = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
918
919         /* append bytes from previous operation */
920         len = rctx->buf_cnt + req->nbytes;
921
922         /* only the last transfer can be padded in hardware */
923         if (!rctx->last && (len < block_size)) {
924                 /* to few data, save for next operation */
925                 scatterwalk_map_and_copy(rctx->buf + rctx->buf_cnt, req->src,
926                                          0, req->nbytes, 0);
927                 rctx->buf_cnt += req->nbytes;
928
929                 return 0;
930         }
931
932         /* add data from previous operation first */
933         if (rctx->buf_cnt)
934                 memcpy(rctx->rembuf, rctx->buf, rctx->buf_cnt);
935
936         /* data must always be a multiple of block_size */
937         hash_later = rctx->last ? 0 : len & (block_size - 1);
938         if (hash_later) {
939                 unsigned int offset = req->nbytes - hash_later;
940                 /* Save remaining bytes for later use */
941                 scatterwalk_map_and_copy(rctx->buf, req->src, offset,
942                                         hash_later, 0);
943         }
944
945         rctx->total = len - hash_later;
946         /* have data from previous operation and current */
947         if (rctx->buf_cnt && req->nbytes) {
948                 sg_init_table(rctx->in_sg_chain, 2);
949                 sg_set_buf(rctx->in_sg_chain, rctx->rembuf, rctx->buf_cnt);
950                 sg_chain(rctx->in_sg_chain, 2, req->src);
951                 rctx->in_sg = rctx->in_sg_chain;
952         /* only data from previous operation */
953         } else if (rctx->buf_cnt) {
954                 rctx->in_sg = rctx->in_sg_chain;
955                 sg_init_one(rctx->in_sg, rctx->rembuf, rctx->buf_cnt);
956         /* no data from previous operation */
957         } else {
958                 rctx->in_sg = req->src;
959         }
960
961         /* on next call, we only have the remaining data in the buffer */
962         rctx->buf_cnt = hash_later;
963
964         return -EINPROGRESS;
965 }
966
967 static int sahara_sha_process(struct ahash_request *req)
968 {
969         struct sahara_dev *dev = dev_ptr;
970         struct sahara_sha_reqctx *rctx = ahash_request_ctx(req);
971         int ret;
972         unsigned long timeout;
973
974         ret = sahara_sha_prepare_request(req);
975         if (!ret)
976                 return ret;
977
978         if (rctx->first) {
979                 ret = sahara_sha_hw_data_descriptor_create(dev, rctx, req, 0);
980                 if (ret)
981                         return ret;
982
983                 dev->hw_desc[0]->next = 0;
984                 rctx->first = 0;
985         } else {
986                 memcpy(dev->context_base, rctx->context, rctx->context_size);
987
988                 sahara_sha_hw_context_descriptor_create(dev, rctx, req, 0);
989                 dev->hw_desc[0]->next = dev->hw_phys_desc[1];
990                 ret = sahara_sha_hw_data_descriptor_create(dev, rctx, req, 1);
991                 if (ret)
992                         return ret;
993
994                 dev->hw_desc[1]->next = 0;
995         }
996
997         sahara_dump_descriptors(dev);
998         sahara_dump_links(dev);
999
1000         reinit_completion(&dev->dma_completion);
1001
1002         sahara_write(dev, dev->hw_phys_desc[0], SAHARA_REG_DAR);
1003
1004         timeout = wait_for_completion_timeout(&dev->dma_completion,
1005                                 msecs_to_jiffies(SAHARA_TIMEOUT_MS));
1006
1007         if (rctx->sg_in_idx)
1008                 dma_unmap_sg(dev->device, dev->in_sg, dev->nb_in_sg,
1009                              DMA_TO_DEVICE);
1010
1011         if (!timeout) {
1012                 dev_err(dev->device, "SHA timeout\n");
1013                 return -ETIMEDOUT;
1014         }
1015
1016         memcpy(rctx->context, dev->context_base, rctx->context_size);
1017
1018         if (req->result && rctx->last)
1019                 memcpy(req->result, rctx->context, rctx->digest_size);
1020
1021         return 0;
1022 }
1023
1024 static int sahara_queue_manage(void *data)
1025 {
1026         struct sahara_dev *dev = (struct sahara_dev *)data;
1027         struct crypto_async_request *async_req;
1028         struct crypto_async_request *backlog;
1029         int ret = 0;
1030
1031         do {
1032                 __set_current_state(TASK_INTERRUPTIBLE);
1033
1034                 mutex_lock(&dev->queue_mutex);
1035                 backlog = crypto_get_backlog(&dev->queue);
1036                 async_req = crypto_dequeue_request(&dev->queue);
1037                 mutex_unlock(&dev->queue_mutex);
1038
1039                 if (backlog)
1040                         backlog->complete(backlog, -EINPROGRESS);
1041
1042                 if (async_req) {
1043                         if (crypto_tfm_alg_type(async_req->tfm) ==
1044                             CRYPTO_ALG_TYPE_AHASH) {
1045                                 struct ahash_request *req =
1046                                         ahash_request_cast(async_req);
1047
1048                                 ret = sahara_sha_process(req);
1049                         } else {
1050                                 struct ablkcipher_request *req =
1051                                         ablkcipher_request_cast(async_req);
1052
1053                                 ret = sahara_aes_process(req);
1054                         }
1055
1056                         async_req->complete(async_req, ret);
1057
1058                         continue;
1059                 }
1060
1061                 schedule();
1062         } while (!kthread_should_stop());
1063
1064         return 0;
1065 }
1066
1067 static int sahara_sha_enqueue(struct ahash_request *req, int last)
1068 {
1069         struct sahara_sha_reqctx *rctx = ahash_request_ctx(req);
1070         struct sahara_dev *dev = dev_ptr;
1071         int ret;
1072
1073         if (!req->nbytes && !last)
1074                 return 0;
1075
1076         rctx->last = last;
1077
1078         if (!rctx->active) {
1079                 rctx->active = 1;
1080                 rctx->first = 1;
1081         }
1082
1083         mutex_lock(&dev->queue_mutex);
1084         ret = crypto_enqueue_request(&dev->queue, &req->base);
1085         mutex_unlock(&dev->queue_mutex);
1086
1087         wake_up_process(dev->kthread);
1088
1089         return ret;
1090 }
1091
1092 static int sahara_sha_init(struct ahash_request *req)
1093 {
1094         struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1095         struct sahara_sha_reqctx *rctx = ahash_request_ctx(req);
1096
1097         memset(rctx, 0, sizeof(*rctx));
1098
1099         switch (crypto_ahash_digestsize(tfm)) {
1100         case SHA1_DIGEST_SIZE:
1101                 rctx->mode |= SAHARA_HDR_MDHA_ALG_SHA1;
1102                 rctx->digest_size = SHA1_DIGEST_SIZE;
1103                 break;
1104         case SHA256_DIGEST_SIZE:
1105                 rctx->mode |= SAHARA_HDR_MDHA_ALG_SHA256;
1106                 rctx->digest_size = SHA256_DIGEST_SIZE;
1107                 break;
1108         default:
1109                 return -EINVAL;
1110         }
1111
1112         rctx->context_size = rctx->digest_size + 4;
1113         rctx->active = 0;
1114
1115         return 0;
1116 }
1117
1118 static int sahara_sha_update(struct ahash_request *req)
1119 {
1120         return sahara_sha_enqueue(req, 0);
1121 }
1122
1123 static int sahara_sha_final(struct ahash_request *req)
1124 {
1125         req->nbytes = 0;
1126         return sahara_sha_enqueue(req, 1);
1127 }
1128
1129 static int sahara_sha_finup(struct ahash_request *req)
1130 {
1131         return sahara_sha_enqueue(req, 1);
1132 }
1133
1134 static int sahara_sha_digest(struct ahash_request *req)
1135 {
1136         sahara_sha_init(req);
1137
1138         return sahara_sha_finup(req);
1139 }
1140
1141 static int sahara_sha_export(struct ahash_request *req, void *out)
1142 {
1143         struct sahara_sha_reqctx *rctx = ahash_request_ctx(req);
1144
1145         memcpy(out, rctx, sizeof(struct sahara_sha_reqctx));
1146
1147         return 0;
1148 }
1149
1150 static int sahara_sha_import(struct ahash_request *req, const void *in)
1151 {
1152         struct sahara_sha_reqctx *rctx = ahash_request_ctx(req);
1153
1154         memcpy(rctx, in, sizeof(struct sahara_sha_reqctx));
1155
1156         return 0;
1157 }
1158
1159 static int sahara_sha_cra_init(struct crypto_tfm *tfm)
1160 {
1161         crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1162                                  sizeof(struct sahara_sha_reqctx));
1163
1164         return 0;
1165 }
1166
1167 static struct crypto_alg aes_algs[] = {
1168 {
1169         .cra_name               = "ecb(aes)",
1170         .cra_driver_name        = "sahara-ecb-aes",
1171         .cra_priority           = 300,
1172         .cra_flags              = CRYPTO_ALG_TYPE_ABLKCIPHER |
1173                         CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
1174         .cra_blocksize          = AES_BLOCK_SIZE,
1175         .cra_ctxsize            = sizeof(struct sahara_ctx),
1176         .cra_alignmask          = 0x0,
1177         .cra_type               = &crypto_ablkcipher_type,
1178         .cra_module             = THIS_MODULE,
1179         .cra_init               = sahara_aes_cra_init,
1180         .cra_exit               = sahara_aes_cra_exit,
1181         .cra_u.ablkcipher = {
1182                 .min_keysize    = AES_MIN_KEY_SIZE ,
1183                 .max_keysize    = AES_MAX_KEY_SIZE,
1184                 .setkey         = sahara_aes_setkey,
1185                 .encrypt        = sahara_aes_ecb_encrypt,
1186                 .decrypt        = sahara_aes_ecb_decrypt,
1187         }
1188 }, {
1189         .cra_name               = "cbc(aes)",
1190         .cra_driver_name        = "sahara-cbc-aes",
1191         .cra_priority           = 300,
1192         .cra_flags              = CRYPTO_ALG_TYPE_ABLKCIPHER |
1193                         CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
1194         .cra_blocksize          = AES_BLOCK_SIZE,
1195         .cra_ctxsize            = sizeof(struct sahara_ctx),
1196         .cra_alignmask          = 0x0,
1197         .cra_type               = &crypto_ablkcipher_type,
1198         .cra_module             = THIS_MODULE,
1199         .cra_init               = sahara_aes_cra_init,
1200         .cra_exit               = sahara_aes_cra_exit,
1201         .cra_u.ablkcipher = {
1202                 .min_keysize    = AES_MIN_KEY_SIZE ,
1203                 .max_keysize    = AES_MAX_KEY_SIZE,
1204                 .ivsize         = AES_BLOCK_SIZE,
1205                 .setkey         = sahara_aes_setkey,
1206                 .encrypt        = sahara_aes_cbc_encrypt,
1207                 .decrypt        = sahara_aes_cbc_decrypt,
1208         }
1209 }
1210 };
1211
1212 static struct ahash_alg sha_v3_algs[] = {
1213 {
1214         .init           = sahara_sha_init,
1215         .update         = sahara_sha_update,
1216         .final          = sahara_sha_final,
1217         .finup          = sahara_sha_finup,
1218         .digest         = sahara_sha_digest,
1219         .export         = sahara_sha_export,
1220         .import         = sahara_sha_import,
1221         .halg.digestsize        = SHA1_DIGEST_SIZE,
1222         .halg.statesize         = sizeof(struct sahara_sha_reqctx),
1223         .halg.base      = {
1224                 .cra_name               = "sha1",
1225                 .cra_driver_name        = "sahara-sha1",
1226                 .cra_priority           = 300,
1227                 .cra_flags              = CRYPTO_ALG_ASYNC |
1228                                                 CRYPTO_ALG_NEED_FALLBACK,
1229                 .cra_blocksize          = SHA1_BLOCK_SIZE,
1230                 .cra_ctxsize            = sizeof(struct sahara_ctx),
1231                 .cra_alignmask          = 0,
1232                 .cra_module             = THIS_MODULE,
1233                 .cra_init               = sahara_sha_cra_init,
1234         }
1235 },
1236 };
1237
1238 static struct ahash_alg sha_v4_algs[] = {
1239 {
1240         .init           = sahara_sha_init,
1241         .update         = sahara_sha_update,
1242         .final          = sahara_sha_final,
1243         .finup          = sahara_sha_finup,
1244         .digest         = sahara_sha_digest,
1245         .export         = sahara_sha_export,
1246         .import         = sahara_sha_import,
1247         .halg.digestsize        = SHA256_DIGEST_SIZE,
1248         .halg.statesize         = sizeof(struct sahara_sha_reqctx),
1249         .halg.base      = {
1250                 .cra_name               = "sha256",
1251                 .cra_driver_name        = "sahara-sha256",
1252                 .cra_priority           = 300,
1253                 .cra_flags              = CRYPTO_ALG_ASYNC |
1254                                                 CRYPTO_ALG_NEED_FALLBACK,
1255                 .cra_blocksize          = SHA256_BLOCK_SIZE,
1256                 .cra_ctxsize            = sizeof(struct sahara_ctx),
1257                 .cra_alignmask          = 0,
1258                 .cra_module             = THIS_MODULE,
1259                 .cra_init               = sahara_sha_cra_init,
1260         }
1261 },
1262 };
1263
1264 static irqreturn_t sahara_irq_handler(int irq, void *data)
1265 {
1266         struct sahara_dev *dev = (struct sahara_dev *)data;
1267         unsigned int stat = sahara_read(dev, SAHARA_REG_STATUS);
1268         unsigned int err = sahara_read(dev, SAHARA_REG_ERRSTATUS);
1269
1270         sahara_write(dev, SAHARA_CMD_CLEAR_INT | SAHARA_CMD_CLEAR_ERR,
1271                      SAHARA_REG_CMD);
1272
1273         sahara_decode_status(dev, stat);
1274
1275         if (SAHARA_STATUS_GET_STATE(stat) == SAHARA_STATE_BUSY) {
1276                 return IRQ_NONE;
1277         } else if (SAHARA_STATUS_GET_STATE(stat) == SAHARA_STATE_COMPLETE) {
1278                 dev->error = 0;
1279         } else {
1280                 sahara_decode_error(dev, err);
1281                 dev->error = -EINVAL;
1282         }
1283
1284         complete(&dev->dma_completion);
1285
1286         return IRQ_HANDLED;
1287 }
1288
1289
1290 static int sahara_register_algs(struct sahara_dev *dev)
1291 {
1292         int err;
1293         unsigned int i, j, k, l;
1294
1295         for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
1296                 INIT_LIST_HEAD(&aes_algs[i].cra_list);
1297                 err = crypto_register_alg(&aes_algs[i]);
1298                 if (err)
1299                         goto err_aes_algs;
1300         }
1301
1302         for (k = 0; k < ARRAY_SIZE(sha_v3_algs); k++) {
1303                 err = crypto_register_ahash(&sha_v3_algs[k]);
1304                 if (err)
1305                         goto err_sha_v3_algs;
1306         }
1307
1308         if (dev->version > SAHARA_VERSION_3)
1309                 for (l = 0; l < ARRAY_SIZE(sha_v4_algs); l++) {
1310                         err = crypto_register_ahash(&sha_v4_algs[l]);
1311                         if (err)
1312                                 goto err_sha_v4_algs;
1313                 }
1314
1315         return 0;
1316
1317 err_sha_v4_algs:
1318         for (j = 0; j < l; j++)
1319                 crypto_unregister_ahash(&sha_v4_algs[j]);
1320
1321 err_sha_v3_algs:
1322         for (j = 0; j < k; j++)
1323                 crypto_unregister_ahash(&sha_v3_algs[j]);
1324
1325 err_aes_algs:
1326         for (j = 0; j < i; j++)
1327                 crypto_unregister_alg(&aes_algs[j]);
1328
1329         return err;
1330 }
1331
1332 static void sahara_unregister_algs(struct sahara_dev *dev)
1333 {
1334         unsigned int i;
1335
1336         for (i = 0; i < ARRAY_SIZE(aes_algs); i++)
1337                 crypto_unregister_alg(&aes_algs[i]);
1338
1339         for (i = 0; i < ARRAY_SIZE(sha_v3_algs); i++)
1340                 crypto_unregister_ahash(&sha_v3_algs[i]);
1341
1342         if (dev->version > SAHARA_VERSION_3)
1343                 for (i = 0; i < ARRAY_SIZE(sha_v4_algs); i++)
1344                         crypto_unregister_ahash(&sha_v4_algs[i]);
1345 }
1346
1347 static const struct platform_device_id sahara_platform_ids[] = {
1348         { .name = "sahara-imx27" },
1349         { /* sentinel */ }
1350 };
1351 MODULE_DEVICE_TABLE(platform, sahara_platform_ids);
1352
1353 static const struct of_device_id sahara_dt_ids[] = {
1354         { .compatible = "fsl,imx53-sahara" },
1355         { .compatible = "fsl,imx27-sahara" },
1356         { /* sentinel */ }
1357 };
1358 MODULE_DEVICE_TABLE(of, sahara_dt_ids);
1359
1360 static int sahara_probe(struct platform_device *pdev)
1361 {
1362         struct sahara_dev *dev;
1363         struct resource *res;
1364         u32 version;
1365         int irq;
1366         int err;
1367         int i;
1368
1369         dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1370         if (!dev)
1371                 return -ENOMEM;
1372
1373         dev->device = &pdev->dev;
1374         platform_set_drvdata(pdev, dev);
1375
1376         /* Get the base address */
1377         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1378         dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
1379         if (IS_ERR(dev->regs_base))
1380                 return PTR_ERR(dev->regs_base);
1381
1382         /* Get the IRQ */
1383         irq = platform_get_irq(pdev,  0);
1384         if (irq < 0) {
1385                 dev_err(&pdev->dev, "failed to get irq resource\n");
1386                 return irq;
1387         }
1388
1389         err = devm_request_irq(&pdev->dev, irq, sahara_irq_handler,
1390                                0, dev_name(&pdev->dev), dev);
1391         if (err) {
1392                 dev_err(&pdev->dev, "failed to request irq\n");
1393                 return err;
1394         }
1395
1396         /* clocks */
1397         dev->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1398         if (IS_ERR(dev->clk_ipg)) {
1399                 dev_err(&pdev->dev, "Could not get ipg clock\n");
1400                 return PTR_ERR(dev->clk_ipg);
1401         }
1402
1403         dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1404         if (IS_ERR(dev->clk_ahb)) {
1405                 dev_err(&pdev->dev, "Could not get ahb clock\n");
1406                 return PTR_ERR(dev->clk_ahb);
1407         }
1408
1409         /* Allocate HW descriptors */
1410         dev->hw_desc[0] = dmam_alloc_coherent(&pdev->dev,
1411                         SAHARA_MAX_HW_DESC * sizeof(struct sahara_hw_desc),
1412                         &dev->hw_phys_desc[0], GFP_KERNEL);
1413         if (!dev->hw_desc[0]) {
1414                 dev_err(&pdev->dev, "Could not allocate hw descriptors\n");
1415                 return -ENOMEM;
1416         }
1417         dev->hw_desc[1] = dev->hw_desc[0] + 1;
1418         dev->hw_phys_desc[1] = dev->hw_phys_desc[0] +
1419                                 sizeof(struct sahara_hw_desc);
1420
1421         /* Allocate space for iv and key */
1422         dev->key_base = dmam_alloc_coherent(&pdev->dev, 2 * AES_KEYSIZE_128,
1423                                 &dev->key_phys_base, GFP_KERNEL);
1424         if (!dev->key_base) {
1425                 dev_err(&pdev->dev, "Could not allocate memory for key\n");
1426                 return -ENOMEM;
1427         }
1428         dev->iv_base = dev->key_base + AES_KEYSIZE_128;
1429         dev->iv_phys_base = dev->key_phys_base + AES_KEYSIZE_128;
1430
1431         /* Allocate space for context: largest digest + message length field */
1432         dev->context_base = dmam_alloc_coherent(&pdev->dev,
1433                                         SHA256_DIGEST_SIZE + 4,
1434                                         &dev->context_phys_base, GFP_KERNEL);
1435         if (!dev->context_base) {
1436                 dev_err(&pdev->dev, "Could not allocate memory for MDHA context\n");
1437                 return -ENOMEM;
1438         }
1439
1440         /* Allocate space for HW links */
1441         dev->hw_link[0] = dmam_alloc_coherent(&pdev->dev,
1442                         SAHARA_MAX_HW_LINK * sizeof(struct sahara_hw_link),
1443                         &dev->hw_phys_link[0], GFP_KERNEL);
1444         if (!dev->hw_link[0]) {
1445                 dev_err(&pdev->dev, "Could not allocate hw links\n");
1446                 return -ENOMEM;
1447         }
1448         for (i = 1; i < SAHARA_MAX_HW_LINK; i++) {
1449                 dev->hw_phys_link[i] = dev->hw_phys_link[i - 1] +
1450                                         sizeof(struct sahara_hw_link);
1451                 dev->hw_link[i] = dev->hw_link[i - 1] + 1;
1452         }
1453
1454         crypto_init_queue(&dev->queue, SAHARA_QUEUE_LENGTH);
1455
1456         mutex_init(&dev->queue_mutex);
1457
1458         dev_ptr = dev;
1459
1460         dev->kthread = kthread_run(sahara_queue_manage, dev, "sahara_crypto");
1461         if (IS_ERR(dev->kthread)) {
1462                 return PTR_ERR(dev->kthread);
1463         }
1464
1465         init_completion(&dev->dma_completion);
1466
1467         err = clk_prepare_enable(dev->clk_ipg);
1468         if (err)
1469                 return err;
1470         err = clk_prepare_enable(dev->clk_ahb);
1471         if (err)
1472                 goto clk_ipg_disable;
1473
1474         version = sahara_read(dev, SAHARA_REG_VERSION);
1475         if (of_device_is_compatible(pdev->dev.of_node, "fsl,imx27-sahara")) {
1476                 if (version != SAHARA_VERSION_3)
1477                         err = -ENODEV;
1478         } else if (of_device_is_compatible(pdev->dev.of_node,
1479                         "fsl,imx53-sahara")) {
1480                 if (((version >> 8) & 0xff) != SAHARA_VERSION_4)
1481                         err = -ENODEV;
1482                 version = (version >> 8) & 0xff;
1483         }
1484         if (err == -ENODEV) {
1485                 dev_err(&pdev->dev, "SAHARA version %d not supported\n",
1486                                 version);
1487                 goto err_algs;
1488         }
1489
1490         dev->version = version;
1491
1492         sahara_write(dev, SAHARA_CMD_RESET | SAHARA_CMD_MODE_BATCH,
1493                      SAHARA_REG_CMD);
1494         sahara_write(dev, SAHARA_CONTROL_SET_THROTTLE(0) |
1495                         SAHARA_CONTROL_SET_MAXBURST(8) |
1496                         SAHARA_CONTROL_RNG_AUTORSD |
1497                         SAHARA_CONTROL_ENABLE_INT,
1498                         SAHARA_REG_CONTROL);
1499
1500         err = sahara_register_algs(dev);
1501         if (err)
1502                 goto err_algs;
1503
1504         dev_info(&pdev->dev, "SAHARA version %d initialized\n", version);
1505
1506         return 0;
1507
1508 err_algs:
1509         kthread_stop(dev->kthread);
1510         dev_ptr = NULL;
1511         clk_disable_unprepare(dev->clk_ahb);
1512 clk_ipg_disable:
1513         clk_disable_unprepare(dev->clk_ipg);
1514
1515         return err;
1516 }
1517
1518 static int sahara_remove(struct platform_device *pdev)
1519 {
1520         struct sahara_dev *dev = platform_get_drvdata(pdev);
1521
1522         kthread_stop(dev->kthread);
1523
1524         sahara_unregister_algs(dev);
1525
1526         clk_disable_unprepare(dev->clk_ipg);
1527         clk_disable_unprepare(dev->clk_ahb);
1528
1529         dev_ptr = NULL;
1530
1531         return 0;
1532 }
1533
1534 static struct platform_driver sahara_driver = {
1535         .probe          = sahara_probe,
1536         .remove         = sahara_remove,
1537         .driver         = {
1538                 .name   = SAHARA_NAME,
1539                 .of_match_table = sahara_dt_ids,
1540         },
1541         .id_table = sahara_platform_ids,
1542 };
1543
1544 module_platform_driver(sahara_driver);
1545
1546 MODULE_LICENSE("GPL");
1547 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>");
1548 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
1549 MODULE_DESCRIPTION("SAHARA2 HW crypto accelerator");