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