]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/crypto/marvell/hash.c
Merge tag 'armsoc-soc' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[karo-tx-linux.git] / drivers / crypto / marvell / hash.c
1 /*
2  * Hash algorithms supported by the CESA: MD5, SHA1 and SHA256.
3  *
4  * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
5  * Author: Arnaud Ebalard <arno@natisbad.org>
6  *
7  * This work is based on an initial version written by
8  * Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License version 2 as published
12  * by the Free Software Foundation.
13  */
14
15 #include <crypto/md5.h>
16 #include <crypto/sha.h>
17
18 #include "cesa.h"
19
20 struct mv_cesa_ahash_dma_iter {
21         struct mv_cesa_dma_iter base;
22         struct mv_cesa_sg_dma_iter src;
23 };
24
25 static inline void
26 mv_cesa_ahash_req_iter_init(struct mv_cesa_ahash_dma_iter *iter,
27                             struct ahash_request *req)
28 {
29         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
30         unsigned int len = req->nbytes + creq->cache_ptr;
31
32         if (!creq->last_req)
33                 len &= ~CESA_HASH_BLOCK_SIZE_MSK;
34
35         mv_cesa_req_dma_iter_init(&iter->base, len);
36         mv_cesa_sg_dma_iter_init(&iter->src, req->src, DMA_TO_DEVICE);
37         iter->src.op_offset = creq->cache_ptr;
38 }
39
40 static inline bool
41 mv_cesa_ahash_req_iter_next_op(struct mv_cesa_ahash_dma_iter *iter)
42 {
43         iter->src.op_offset = 0;
44
45         return mv_cesa_req_dma_iter_next_op(&iter->base);
46 }
47
48 static inline int
49 mv_cesa_ahash_dma_alloc_cache(struct mv_cesa_ahash_dma_req *req, gfp_t flags)
50 {
51         req->cache = dma_pool_alloc(cesa_dev->dma->cache_pool, flags,
52                                     &req->cache_dma);
53         if (!req->cache)
54                 return -ENOMEM;
55
56         return 0;
57 }
58
59 static inline void
60 mv_cesa_ahash_dma_free_cache(struct mv_cesa_ahash_dma_req *req)
61 {
62         if (!req->cache)
63                 return;
64
65         dma_pool_free(cesa_dev->dma->cache_pool, req->cache,
66                       req->cache_dma);
67 }
68
69 static int mv_cesa_ahash_dma_alloc_padding(struct mv_cesa_ahash_dma_req *req,
70                                            gfp_t flags)
71 {
72         if (req->padding)
73                 return 0;
74
75         req->padding = dma_pool_alloc(cesa_dev->dma->padding_pool, flags,
76                                       &req->padding_dma);
77         if (!req->padding)
78                 return -ENOMEM;
79
80         return 0;
81 }
82
83 static void mv_cesa_ahash_dma_free_padding(struct mv_cesa_ahash_dma_req *req)
84 {
85         if (!req->padding)
86                 return;
87
88         dma_pool_free(cesa_dev->dma->padding_pool, req->padding,
89                       req->padding_dma);
90         req->padding = NULL;
91 }
92
93 static inline void mv_cesa_ahash_dma_last_cleanup(struct ahash_request *req)
94 {
95         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
96
97         mv_cesa_ahash_dma_free_padding(&creq->req.dma);
98 }
99
100 static inline void mv_cesa_ahash_dma_cleanup(struct ahash_request *req)
101 {
102         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
103
104         dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents, DMA_TO_DEVICE);
105         mv_cesa_ahash_dma_free_cache(&creq->req.dma);
106         mv_cesa_dma_cleanup(&creq->base);
107 }
108
109 static inline void mv_cesa_ahash_cleanup(struct ahash_request *req)
110 {
111         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
112
113         if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
114                 mv_cesa_ahash_dma_cleanup(req);
115 }
116
117 static void mv_cesa_ahash_last_cleanup(struct ahash_request *req)
118 {
119         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
120
121         if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
122                 mv_cesa_ahash_dma_last_cleanup(req);
123 }
124
125 static int mv_cesa_ahash_pad_len(struct mv_cesa_ahash_req *creq)
126 {
127         unsigned int index, padlen;
128
129         index = creq->len & CESA_HASH_BLOCK_SIZE_MSK;
130         padlen = (index < 56) ? (56 - index) : (64 + 56 - index);
131
132         return padlen;
133 }
134
135 static int mv_cesa_ahash_pad_req(struct mv_cesa_ahash_req *creq, u8 *buf)
136 {
137         unsigned int index, padlen;
138
139         buf[0] = 0x80;
140         /* Pad out to 56 mod 64 */
141         index = creq->len & CESA_HASH_BLOCK_SIZE_MSK;
142         padlen = mv_cesa_ahash_pad_len(creq);
143         memset(buf + 1, 0, padlen - 1);
144
145         if (creq->algo_le) {
146                 __le64 bits = cpu_to_le64(creq->len << 3);
147                 memcpy(buf + padlen, &bits, sizeof(bits));
148         } else {
149                 __be64 bits = cpu_to_be64(creq->len << 3);
150                 memcpy(buf + padlen, &bits, sizeof(bits));
151         }
152
153         return padlen + 8;
154 }
155
156 static void mv_cesa_ahash_std_step(struct ahash_request *req)
157 {
158         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
159         struct mv_cesa_ahash_std_req *sreq = &creq->req.std;
160         struct mv_cesa_engine *engine = creq->base.engine;
161         struct mv_cesa_op_ctx *op;
162         unsigned int new_cache_ptr = 0;
163         u32 frag_mode;
164         size_t  len;
165         unsigned int digsize;
166         int i;
167
168         mv_cesa_adjust_op(engine, &creq->op_tmpl);
169         memcpy_toio(engine->sram, &creq->op_tmpl, sizeof(creq->op_tmpl));
170
171         if (!sreq->offset) {
172                 digsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
173                 for (i = 0; i < digsize / 4; i++)
174                         writel_relaxed(creq->state[i], engine->regs + CESA_IVDIG(i));
175         }
176
177         if (creq->cache_ptr)
178                 memcpy_toio(engine->sram + CESA_SA_DATA_SRAM_OFFSET,
179                             creq->cache, creq->cache_ptr);
180
181         len = min_t(size_t, req->nbytes + creq->cache_ptr - sreq->offset,
182                     CESA_SA_SRAM_PAYLOAD_SIZE);
183
184         if (!creq->last_req) {
185                 new_cache_ptr = len & CESA_HASH_BLOCK_SIZE_MSK;
186                 len &= ~CESA_HASH_BLOCK_SIZE_MSK;
187         }
188
189         if (len - creq->cache_ptr)
190                 sreq->offset += sg_pcopy_to_buffer(req->src, creq->src_nents,
191                                                    engine->sram +
192                                                    CESA_SA_DATA_SRAM_OFFSET +
193                                                    creq->cache_ptr,
194                                                    len - creq->cache_ptr,
195                                                    sreq->offset);
196
197         op = &creq->op_tmpl;
198
199         frag_mode = mv_cesa_get_op_cfg(op) & CESA_SA_DESC_CFG_FRAG_MSK;
200
201         if (creq->last_req && sreq->offset == req->nbytes &&
202             creq->len <= CESA_SA_DESC_MAC_SRC_TOTAL_LEN_MAX) {
203                 if (frag_mode == CESA_SA_DESC_CFG_FIRST_FRAG)
204                         frag_mode = CESA_SA_DESC_CFG_NOT_FRAG;
205                 else if (frag_mode == CESA_SA_DESC_CFG_MID_FRAG)
206                         frag_mode = CESA_SA_DESC_CFG_LAST_FRAG;
207         }
208
209         if (frag_mode == CESA_SA_DESC_CFG_NOT_FRAG ||
210             frag_mode == CESA_SA_DESC_CFG_LAST_FRAG) {
211                 if (len &&
212                     creq->len <= CESA_SA_DESC_MAC_SRC_TOTAL_LEN_MAX) {
213                         mv_cesa_set_mac_op_total_len(op, creq->len);
214                 } else {
215                         int trailerlen = mv_cesa_ahash_pad_len(creq) + 8;
216
217                         if (len + trailerlen > CESA_SA_SRAM_PAYLOAD_SIZE) {
218                                 len &= CESA_HASH_BLOCK_SIZE_MSK;
219                                 new_cache_ptr = 64 - trailerlen;
220                                 memcpy_fromio(creq->cache,
221                                               engine->sram +
222                                               CESA_SA_DATA_SRAM_OFFSET + len,
223                                               new_cache_ptr);
224                         } else {
225                                 len += mv_cesa_ahash_pad_req(creq,
226                                                 engine->sram + len +
227                                                 CESA_SA_DATA_SRAM_OFFSET);
228                         }
229
230                         if (frag_mode == CESA_SA_DESC_CFG_LAST_FRAG)
231                                 frag_mode = CESA_SA_DESC_CFG_MID_FRAG;
232                         else
233                                 frag_mode = CESA_SA_DESC_CFG_FIRST_FRAG;
234                 }
235         }
236
237         mv_cesa_set_mac_op_frag_len(op, len);
238         mv_cesa_update_op_cfg(op, frag_mode, CESA_SA_DESC_CFG_FRAG_MSK);
239
240         /* FIXME: only update enc_len field */
241         memcpy_toio(engine->sram, op, sizeof(*op));
242
243         if (frag_mode == CESA_SA_DESC_CFG_FIRST_FRAG)
244                 mv_cesa_update_op_cfg(op, CESA_SA_DESC_CFG_MID_FRAG,
245                                       CESA_SA_DESC_CFG_FRAG_MSK);
246
247         creq->cache_ptr = new_cache_ptr;
248
249         mv_cesa_set_int_mask(engine, CESA_SA_INT_ACCEL0_DONE);
250         writel_relaxed(CESA_SA_CFG_PARA_DIS, engine->regs + CESA_SA_CFG);
251         BUG_ON(readl(engine->regs + CESA_SA_CMD) &
252                CESA_SA_CMD_EN_CESA_SA_ACCL0);
253         writel(CESA_SA_CMD_EN_CESA_SA_ACCL0, engine->regs + CESA_SA_CMD);
254 }
255
256 static int mv_cesa_ahash_std_process(struct ahash_request *req, u32 status)
257 {
258         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
259         struct mv_cesa_ahash_std_req *sreq = &creq->req.std;
260
261         if (sreq->offset < (req->nbytes - creq->cache_ptr))
262                 return -EINPROGRESS;
263
264         return 0;
265 }
266
267 static inline void mv_cesa_ahash_dma_prepare(struct ahash_request *req)
268 {
269         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
270         struct mv_cesa_req *basereq = &creq->base;
271
272         mv_cesa_dma_prepare(basereq, basereq->engine);
273 }
274
275 static void mv_cesa_ahash_std_prepare(struct ahash_request *req)
276 {
277         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
278         struct mv_cesa_ahash_std_req *sreq = &creq->req.std;
279
280         sreq->offset = 0;
281 }
282
283 static void mv_cesa_ahash_step(struct crypto_async_request *req)
284 {
285         struct ahash_request *ahashreq = ahash_request_cast(req);
286         struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
287
288         if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
289                 mv_cesa_dma_step(&creq->base);
290         else
291                 mv_cesa_ahash_std_step(ahashreq);
292 }
293
294 static int mv_cesa_ahash_process(struct crypto_async_request *req, u32 status)
295 {
296         struct ahash_request *ahashreq = ahash_request_cast(req);
297         struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
298
299         if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
300                 return mv_cesa_dma_process(&creq->base, status);
301
302         return mv_cesa_ahash_std_process(ahashreq, status);
303 }
304
305 static void mv_cesa_ahash_complete(struct crypto_async_request *req)
306 {
307         struct ahash_request *ahashreq = ahash_request_cast(req);
308         struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
309         struct mv_cesa_engine *engine = creq->base.engine;
310         unsigned int digsize;
311         int i;
312
313         digsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(ahashreq));
314
315         if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ &&
316             (creq->base.chain.last->flags & CESA_TDMA_TYPE_MSK) == CESA_TDMA_RESULT) {
317                 __le32 *data = NULL;
318
319                 /*
320                  * Result is already in the correct endianess when the SA is
321                  * used
322                  */
323                 data = creq->base.chain.last->op->ctx.hash.hash;
324                 for (i = 0; i < digsize / 4; i++)
325                         creq->state[i] = cpu_to_le32(data[i]);
326
327                 memcpy(ahashreq->result, data, digsize);
328         } else {
329                 for (i = 0; i < digsize / 4; i++)
330                         creq->state[i] = readl_relaxed(engine->regs +
331                                                        CESA_IVDIG(i));
332                 if (creq->last_req) {
333                         /*
334                         * Hardware's MD5 digest is in little endian format, but
335                         * SHA in big endian format
336                         */
337                         if (creq->algo_le) {
338                                 __le32 *result = (void *)ahashreq->result;
339
340                                 for (i = 0; i < digsize / 4; i++)
341                                         result[i] = cpu_to_le32(creq->state[i]);
342                         } else {
343                                 __be32 *result = (void *)ahashreq->result;
344
345                                 for (i = 0; i < digsize / 4; i++)
346                                         result[i] = cpu_to_be32(creq->state[i]);
347                         }
348                 }
349         }
350
351         atomic_sub(ahashreq->nbytes, &engine->load);
352 }
353
354 static void mv_cesa_ahash_prepare(struct crypto_async_request *req,
355                                   struct mv_cesa_engine *engine)
356 {
357         struct ahash_request *ahashreq = ahash_request_cast(req);
358         struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
359
360         creq->base.engine = engine;
361
362         if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
363                 mv_cesa_ahash_dma_prepare(ahashreq);
364         else
365                 mv_cesa_ahash_std_prepare(ahashreq);
366 }
367
368 static void mv_cesa_ahash_req_cleanup(struct crypto_async_request *req)
369 {
370         struct ahash_request *ahashreq = ahash_request_cast(req);
371         struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
372
373         if (creq->last_req)
374                 mv_cesa_ahash_last_cleanup(ahashreq);
375
376         mv_cesa_ahash_cleanup(ahashreq);
377
378         if (creq->cache_ptr)
379                 sg_pcopy_to_buffer(ahashreq->src, creq->src_nents,
380                                    creq->cache,
381                                    creq->cache_ptr,
382                                    ahashreq->nbytes - creq->cache_ptr);
383 }
384
385 static const struct mv_cesa_req_ops mv_cesa_ahash_req_ops = {
386         .step = mv_cesa_ahash_step,
387         .process = mv_cesa_ahash_process,
388         .cleanup = mv_cesa_ahash_req_cleanup,
389         .complete = mv_cesa_ahash_complete,
390 };
391
392 static void mv_cesa_ahash_init(struct ahash_request *req,
393                               struct mv_cesa_op_ctx *tmpl, bool algo_le)
394 {
395         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
396
397         memset(creq, 0, sizeof(*creq));
398         mv_cesa_update_op_cfg(tmpl,
399                               CESA_SA_DESC_CFG_OP_MAC_ONLY |
400                               CESA_SA_DESC_CFG_FIRST_FRAG,
401                               CESA_SA_DESC_CFG_OP_MSK |
402                               CESA_SA_DESC_CFG_FRAG_MSK);
403         mv_cesa_set_mac_op_total_len(tmpl, 0);
404         mv_cesa_set_mac_op_frag_len(tmpl, 0);
405         creq->op_tmpl = *tmpl;
406         creq->len = 0;
407         creq->algo_le = algo_le;
408 }
409
410 static inline int mv_cesa_ahash_cra_init(struct crypto_tfm *tfm)
411 {
412         struct mv_cesa_hash_ctx *ctx = crypto_tfm_ctx(tfm);
413
414         ctx->base.ops = &mv_cesa_ahash_req_ops;
415
416         crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
417                                  sizeof(struct mv_cesa_ahash_req));
418         return 0;
419 }
420
421 static bool mv_cesa_ahash_cache_req(struct ahash_request *req)
422 {
423         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
424         bool cached = false;
425
426         if (creq->cache_ptr + req->nbytes < CESA_MAX_HASH_BLOCK_SIZE && !creq->last_req) {
427                 cached = true;
428
429                 if (!req->nbytes)
430                         return cached;
431
432                 sg_pcopy_to_buffer(req->src, creq->src_nents,
433                                    creq->cache + creq->cache_ptr,
434                                    req->nbytes, 0);
435
436                 creq->cache_ptr += req->nbytes;
437         }
438
439         return cached;
440 }
441
442 static struct mv_cesa_op_ctx *
443 mv_cesa_dma_add_frag(struct mv_cesa_tdma_chain *chain,
444                      struct mv_cesa_op_ctx *tmpl, unsigned int frag_len,
445                      gfp_t flags)
446 {
447         struct mv_cesa_op_ctx *op;
448         int ret;
449
450         op = mv_cesa_dma_add_op(chain, tmpl, false, flags);
451         if (IS_ERR(op))
452                 return op;
453
454         /* Set the operation block fragment length. */
455         mv_cesa_set_mac_op_frag_len(op, frag_len);
456
457         /* Append dummy desc to launch operation */
458         ret = mv_cesa_dma_add_dummy_launch(chain, flags);
459         if (ret)
460                 return ERR_PTR(ret);
461
462         if (mv_cesa_mac_op_is_first_frag(tmpl))
463                 mv_cesa_update_op_cfg(tmpl,
464                                       CESA_SA_DESC_CFG_MID_FRAG,
465                                       CESA_SA_DESC_CFG_FRAG_MSK);
466
467         return op;
468 }
469
470 static int
471 mv_cesa_ahash_dma_add_cache(struct mv_cesa_tdma_chain *chain,
472                             struct mv_cesa_ahash_req *creq,
473                             gfp_t flags)
474 {
475         struct mv_cesa_ahash_dma_req *ahashdreq = &creq->req.dma;
476         int ret;
477
478         if (!creq->cache_ptr)
479                 return 0;
480
481         ret = mv_cesa_ahash_dma_alloc_cache(ahashdreq, flags);
482         if (ret)
483                 return ret;
484
485         memcpy(ahashdreq->cache, creq->cache, creq->cache_ptr);
486
487         return mv_cesa_dma_add_data_transfer(chain,
488                                              CESA_SA_DATA_SRAM_OFFSET,
489                                              ahashdreq->cache_dma,
490                                              creq->cache_ptr,
491                                              CESA_TDMA_DST_IN_SRAM,
492                                              flags);
493 }
494
495 static struct mv_cesa_op_ctx *
496 mv_cesa_ahash_dma_last_req(struct mv_cesa_tdma_chain *chain,
497                            struct mv_cesa_ahash_dma_iter *dma_iter,
498                            struct mv_cesa_ahash_req *creq,
499                            unsigned int frag_len, gfp_t flags)
500 {
501         struct mv_cesa_ahash_dma_req *ahashdreq = &creq->req.dma;
502         unsigned int len, trailerlen, padoff = 0;
503         struct mv_cesa_op_ctx *op;
504         int ret;
505
506         /*
507          * If the transfer is smaller than our maximum length, and we have
508          * some data outstanding, we can ask the engine to finish the hash.
509          */
510         if (creq->len <= CESA_SA_DESC_MAC_SRC_TOTAL_LEN_MAX && frag_len) {
511                 op = mv_cesa_dma_add_frag(chain, &creq->op_tmpl, frag_len,
512                                           flags);
513                 if (IS_ERR(op))
514                         return op;
515
516                 mv_cesa_set_mac_op_total_len(op, creq->len);
517                 mv_cesa_update_op_cfg(op, mv_cesa_mac_op_is_first_frag(op) ?
518                                                 CESA_SA_DESC_CFG_NOT_FRAG :
519                                                 CESA_SA_DESC_CFG_LAST_FRAG,
520                                       CESA_SA_DESC_CFG_FRAG_MSK);
521
522                 ret = mv_cesa_dma_add_result_op(chain,
523                                                 CESA_SA_CFG_SRAM_OFFSET,
524                                                 CESA_SA_DATA_SRAM_OFFSET,
525                                                 CESA_TDMA_SRC_IN_SRAM, flags);
526                 if (ret)
527                         return ERR_PTR(-ENOMEM);
528                 return op;
529         }
530
531         /*
532          * The request is longer than the engine can handle, or we have
533          * no data outstanding. Manually generate the padding, adding it
534          * as a "mid" fragment.
535          */
536         ret = mv_cesa_ahash_dma_alloc_padding(ahashdreq, flags);
537         if (ret)
538                 return ERR_PTR(ret);
539
540         trailerlen = mv_cesa_ahash_pad_req(creq, ahashdreq->padding);
541
542         len = min(CESA_SA_SRAM_PAYLOAD_SIZE - frag_len, trailerlen);
543         if (len) {
544                 ret = mv_cesa_dma_add_data_transfer(chain,
545                                                 CESA_SA_DATA_SRAM_OFFSET +
546                                                 frag_len,
547                                                 ahashdreq->padding_dma,
548                                                 len, CESA_TDMA_DST_IN_SRAM,
549                                                 flags);
550                 if (ret)
551                         return ERR_PTR(ret);
552
553                 op = mv_cesa_dma_add_frag(chain, &creq->op_tmpl, frag_len + len,
554                                           flags);
555                 if (IS_ERR(op))
556                         return op;
557
558                 if (len == trailerlen)
559                         return op;
560
561                 padoff += len;
562         }
563
564         ret = mv_cesa_dma_add_data_transfer(chain,
565                                             CESA_SA_DATA_SRAM_OFFSET,
566                                             ahashdreq->padding_dma +
567                                             padoff,
568                                             trailerlen - padoff,
569                                             CESA_TDMA_DST_IN_SRAM,
570                                             flags);
571         if (ret)
572                 return ERR_PTR(ret);
573
574         return mv_cesa_dma_add_frag(chain, &creq->op_tmpl, trailerlen - padoff,
575                                     flags);
576 }
577
578 static int mv_cesa_ahash_dma_req_init(struct ahash_request *req)
579 {
580         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
581         gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
582                       GFP_KERNEL : GFP_ATOMIC;
583         struct mv_cesa_req *basereq = &creq->base;
584         struct mv_cesa_ahash_dma_iter iter;
585         struct mv_cesa_op_ctx *op = NULL;
586         unsigned int frag_len;
587         int ret;
588         u32 type;
589
590         basereq->chain.first = NULL;
591         basereq->chain.last = NULL;
592
593         if (creq->src_nents) {
594                 ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents,
595                                  DMA_TO_DEVICE);
596                 if (!ret) {
597                         ret = -ENOMEM;
598                         goto err;
599                 }
600         }
601
602         mv_cesa_tdma_desc_iter_init(&basereq->chain);
603         mv_cesa_ahash_req_iter_init(&iter, req);
604
605         /*
606          * Add the cache (left-over data from a previous block) first.
607          * This will never overflow the SRAM size.
608          */
609         ret = mv_cesa_ahash_dma_add_cache(&basereq->chain, creq, flags);
610         if (ret)
611                 goto err_free_tdma;
612
613         if (iter.src.sg) {
614                 /*
615                  * Add all the new data, inserting an operation block and
616                  * launch command between each full SRAM block-worth of
617                  * data. We intentionally do not add the final op block.
618                  */
619                 while (true) {
620                         ret = mv_cesa_dma_add_op_transfers(&basereq->chain,
621                                                            &iter.base,
622                                                            &iter.src, flags);
623                         if (ret)
624                                 goto err_free_tdma;
625
626                         frag_len = iter.base.op_len;
627
628                         if (!mv_cesa_ahash_req_iter_next_op(&iter))
629                                 break;
630
631                         op = mv_cesa_dma_add_frag(&basereq->chain, &creq->op_tmpl,
632                                                   frag_len, flags);
633                         if (IS_ERR(op)) {
634                                 ret = PTR_ERR(op);
635                                 goto err_free_tdma;
636                         }
637                 }
638         } else {
639                 /* Account for the data that was in the cache. */
640                 frag_len = iter.base.op_len;
641         }
642
643         /*
644          * At this point, frag_len indicates whether we have any data
645          * outstanding which needs an operation.  Queue up the final
646          * operation, which depends whether this is the final request.
647          */
648         if (creq->last_req)
649                 op = mv_cesa_ahash_dma_last_req(&basereq->chain, &iter, creq,
650                                                 frag_len, flags);
651         else if (frag_len)
652                 op = mv_cesa_dma_add_frag(&basereq->chain, &creq->op_tmpl,
653                                           frag_len, flags);
654
655         if (IS_ERR(op)) {
656                 ret = PTR_ERR(op);
657                 goto err_free_tdma;
658         }
659
660         /*
661          * If results are copied via DMA, this means that this
662          * request can be directly processed by the engine,
663          * without partial updates. So we can chain it at the
664          * DMA level with other requests.
665          */
666         type = basereq->chain.last->flags & CESA_TDMA_TYPE_MSK;
667
668         if (op && type != CESA_TDMA_RESULT) {
669                 /* Add dummy desc to wait for crypto operation end */
670                 ret = mv_cesa_dma_add_dummy_end(&basereq->chain, flags);
671                 if (ret)
672                         goto err_free_tdma;
673         }
674
675         if (!creq->last_req)
676                 creq->cache_ptr = req->nbytes + creq->cache_ptr -
677                                   iter.base.len;
678         else
679                 creq->cache_ptr = 0;
680
681         basereq->chain.last->flags |= CESA_TDMA_END_OF_REQ;
682
683         if (type != CESA_TDMA_RESULT)
684                 basereq->chain.last->flags |= CESA_TDMA_BREAK_CHAIN;
685
686         return 0;
687
688 err_free_tdma:
689         mv_cesa_dma_cleanup(basereq);
690         dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents, DMA_TO_DEVICE);
691
692 err:
693         mv_cesa_ahash_last_cleanup(req);
694
695         return ret;
696 }
697
698 static int mv_cesa_ahash_req_init(struct ahash_request *req, bool *cached)
699 {
700         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
701
702         creq->src_nents = sg_nents_for_len(req->src, req->nbytes);
703         if (creq->src_nents < 0) {
704                 dev_err(cesa_dev->dev, "Invalid number of src SG");
705                 return creq->src_nents;
706         }
707
708         *cached = mv_cesa_ahash_cache_req(req);
709
710         if (*cached)
711                 return 0;
712
713         if (cesa_dev->caps->has_tdma)
714                 return mv_cesa_ahash_dma_req_init(req);
715         else
716                 return 0;
717 }
718
719 static int mv_cesa_ahash_queue_req(struct ahash_request *req)
720 {
721         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
722         struct mv_cesa_engine *engine;
723         bool cached = false;
724         int ret;
725
726         ret = mv_cesa_ahash_req_init(req, &cached);
727         if (ret)
728                 return ret;
729
730         if (cached)
731                 return 0;
732
733         engine = mv_cesa_select_engine(req->nbytes);
734         mv_cesa_ahash_prepare(&req->base, engine);
735
736         ret = mv_cesa_queue_req(&req->base, &creq->base);
737
738         if (mv_cesa_req_needs_cleanup(&req->base, ret))
739                 mv_cesa_ahash_cleanup(req);
740
741         return ret;
742 }
743
744 static int mv_cesa_ahash_update(struct ahash_request *req)
745 {
746         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
747
748         creq->len += req->nbytes;
749
750         return mv_cesa_ahash_queue_req(req);
751 }
752
753 static int mv_cesa_ahash_final(struct ahash_request *req)
754 {
755         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
756         struct mv_cesa_op_ctx *tmpl = &creq->op_tmpl;
757
758         mv_cesa_set_mac_op_total_len(tmpl, creq->len);
759         creq->last_req = true;
760         req->nbytes = 0;
761
762         return mv_cesa_ahash_queue_req(req);
763 }
764
765 static int mv_cesa_ahash_finup(struct ahash_request *req)
766 {
767         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
768         struct mv_cesa_op_ctx *tmpl = &creq->op_tmpl;
769
770         creq->len += req->nbytes;
771         mv_cesa_set_mac_op_total_len(tmpl, creq->len);
772         creq->last_req = true;
773
774         return mv_cesa_ahash_queue_req(req);
775 }
776
777 static int mv_cesa_ahash_export(struct ahash_request *req, void *hash,
778                                 u64 *len, void *cache)
779 {
780         struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
781         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
782         unsigned int digsize = crypto_ahash_digestsize(ahash);
783         unsigned int blocksize;
784
785         blocksize = crypto_ahash_blocksize(ahash);
786
787         *len = creq->len;
788         memcpy(hash, creq->state, digsize);
789         memset(cache, 0, blocksize);
790         memcpy(cache, creq->cache, creq->cache_ptr);
791
792         return 0;
793 }
794
795 static int mv_cesa_ahash_import(struct ahash_request *req, const void *hash,
796                                 u64 len, const void *cache)
797 {
798         struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
799         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
800         unsigned int digsize = crypto_ahash_digestsize(ahash);
801         unsigned int blocksize;
802         unsigned int cache_ptr;
803         int ret;
804
805         ret = crypto_ahash_init(req);
806         if (ret)
807                 return ret;
808
809         blocksize = crypto_ahash_blocksize(ahash);
810         if (len >= blocksize)
811                 mv_cesa_update_op_cfg(&creq->op_tmpl,
812                                       CESA_SA_DESC_CFG_MID_FRAG,
813                                       CESA_SA_DESC_CFG_FRAG_MSK);
814
815         creq->len = len;
816         memcpy(creq->state, hash, digsize);
817         creq->cache_ptr = 0;
818
819         cache_ptr = do_div(len, blocksize);
820         if (!cache_ptr)
821                 return 0;
822
823         memcpy(creq->cache, cache, cache_ptr);
824         creq->cache_ptr = cache_ptr;
825
826         return 0;
827 }
828
829 static int mv_cesa_md5_init(struct ahash_request *req)
830 {
831         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
832         struct mv_cesa_op_ctx tmpl = { };
833
834         mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_MD5);
835
836         mv_cesa_ahash_init(req, &tmpl, true);
837
838         creq->state[0] = MD5_H0;
839         creq->state[1] = MD5_H1;
840         creq->state[2] = MD5_H2;
841         creq->state[3] = MD5_H3;
842
843         return 0;
844 }
845
846 static int mv_cesa_md5_export(struct ahash_request *req, void *out)
847 {
848         struct md5_state *out_state = out;
849
850         return mv_cesa_ahash_export(req, out_state->hash,
851                                     &out_state->byte_count, out_state->block);
852 }
853
854 static int mv_cesa_md5_import(struct ahash_request *req, const void *in)
855 {
856         const struct md5_state *in_state = in;
857
858         return mv_cesa_ahash_import(req, in_state->hash, in_state->byte_count,
859                                     in_state->block);
860 }
861
862 static int mv_cesa_md5_digest(struct ahash_request *req)
863 {
864         int ret;
865
866         ret = mv_cesa_md5_init(req);
867         if (ret)
868                 return ret;
869
870         return mv_cesa_ahash_finup(req);
871 }
872
873 struct ahash_alg mv_md5_alg = {
874         .init = mv_cesa_md5_init,
875         .update = mv_cesa_ahash_update,
876         .final = mv_cesa_ahash_final,
877         .finup = mv_cesa_ahash_finup,
878         .digest = mv_cesa_md5_digest,
879         .export = mv_cesa_md5_export,
880         .import = mv_cesa_md5_import,
881         .halg = {
882                 .digestsize = MD5_DIGEST_SIZE,
883                 .statesize = sizeof(struct md5_state),
884                 .base = {
885                         .cra_name = "md5",
886                         .cra_driver_name = "mv-md5",
887                         .cra_priority = 300,
888                         .cra_flags = CRYPTO_ALG_ASYNC |
889                                      CRYPTO_ALG_KERN_DRIVER_ONLY,
890                         .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
891                         .cra_ctxsize = sizeof(struct mv_cesa_hash_ctx),
892                         .cra_init = mv_cesa_ahash_cra_init,
893                         .cra_module = THIS_MODULE,
894                  }
895         }
896 };
897
898 static int mv_cesa_sha1_init(struct ahash_request *req)
899 {
900         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
901         struct mv_cesa_op_ctx tmpl = { };
902
903         mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_SHA1);
904
905         mv_cesa_ahash_init(req, &tmpl, false);
906
907         creq->state[0] = SHA1_H0;
908         creq->state[1] = SHA1_H1;
909         creq->state[2] = SHA1_H2;
910         creq->state[3] = SHA1_H3;
911         creq->state[4] = SHA1_H4;
912
913         return 0;
914 }
915
916 static int mv_cesa_sha1_export(struct ahash_request *req, void *out)
917 {
918         struct sha1_state *out_state = out;
919
920         return mv_cesa_ahash_export(req, out_state->state, &out_state->count,
921                                     out_state->buffer);
922 }
923
924 static int mv_cesa_sha1_import(struct ahash_request *req, const void *in)
925 {
926         const struct sha1_state *in_state = in;
927
928         return mv_cesa_ahash_import(req, in_state->state, in_state->count,
929                                     in_state->buffer);
930 }
931
932 static int mv_cesa_sha1_digest(struct ahash_request *req)
933 {
934         int ret;
935
936         ret = mv_cesa_sha1_init(req);
937         if (ret)
938                 return ret;
939
940         return mv_cesa_ahash_finup(req);
941 }
942
943 struct ahash_alg mv_sha1_alg = {
944         .init = mv_cesa_sha1_init,
945         .update = mv_cesa_ahash_update,
946         .final = mv_cesa_ahash_final,
947         .finup = mv_cesa_ahash_finup,
948         .digest = mv_cesa_sha1_digest,
949         .export = mv_cesa_sha1_export,
950         .import = mv_cesa_sha1_import,
951         .halg = {
952                 .digestsize = SHA1_DIGEST_SIZE,
953                 .statesize = sizeof(struct sha1_state),
954                 .base = {
955                         .cra_name = "sha1",
956                         .cra_driver_name = "mv-sha1",
957                         .cra_priority = 300,
958                         .cra_flags = CRYPTO_ALG_ASYNC |
959                                      CRYPTO_ALG_KERN_DRIVER_ONLY,
960                         .cra_blocksize = SHA1_BLOCK_SIZE,
961                         .cra_ctxsize = sizeof(struct mv_cesa_hash_ctx),
962                         .cra_init = mv_cesa_ahash_cra_init,
963                         .cra_module = THIS_MODULE,
964                  }
965         }
966 };
967
968 static int mv_cesa_sha256_init(struct ahash_request *req)
969 {
970         struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
971         struct mv_cesa_op_ctx tmpl = { };
972
973         mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_SHA256);
974
975         mv_cesa_ahash_init(req, &tmpl, false);
976
977         creq->state[0] = SHA256_H0;
978         creq->state[1] = SHA256_H1;
979         creq->state[2] = SHA256_H2;
980         creq->state[3] = SHA256_H3;
981         creq->state[4] = SHA256_H4;
982         creq->state[5] = SHA256_H5;
983         creq->state[6] = SHA256_H6;
984         creq->state[7] = SHA256_H7;
985
986         return 0;
987 }
988
989 static int mv_cesa_sha256_digest(struct ahash_request *req)
990 {
991         int ret;
992
993         ret = mv_cesa_sha256_init(req);
994         if (ret)
995                 return ret;
996
997         return mv_cesa_ahash_finup(req);
998 }
999
1000 static int mv_cesa_sha256_export(struct ahash_request *req, void *out)
1001 {
1002         struct sha256_state *out_state = out;
1003
1004         return mv_cesa_ahash_export(req, out_state->state, &out_state->count,
1005                                     out_state->buf);
1006 }
1007
1008 static int mv_cesa_sha256_import(struct ahash_request *req, const void *in)
1009 {
1010         const struct sha256_state *in_state = in;
1011
1012         return mv_cesa_ahash_import(req, in_state->state, in_state->count,
1013                                     in_state->buf);
1014 }
1015
1016 struct ahash_alg mv_sha256_alg = {
1017         .init = mv_cesa_sha256_init,
1018         .update = mv_cesa_ahash_update,
1019         .final = mv_cesa_ahash_final,
1020         .finup = mv_cesa_ahash_finup,
1021         .digest = mv_cesa_sha256_digest,
1022         .export = mv_cesa_sha256_export,
1023         .import = mv_cesa_sha256_import,
1024         .halg = {
1025                 .digestsize = SHA256_DIGEST_SIZE,
1026                 .statesize = sizeof(struct sha256_state),
1027                 .base = {
1028                         .cra_name = "sha256",
1029                         .cra_driver_name = "mv-sha256",
1030                         .cra_priority = 300,
1031                         .cra_flags = CRYPTO_ALG_ASYNC |
1032                                      CRYPTO_ALG_KERN_DRIVER_ONLY,
1033                         .cra_blocksize = SHA256_BLOCK_SIZE,
1034                         .cra_ctxsize = sizeof(struct mv_cesa_hash_ctx),
1035                         .cra_init = mv_cesa_ahash_cra_init,
1036                         .cra_module = THIS_MODULE,
1037                  }
1038         }
1039 };
1040
1041 struct mv_cesa_ahash_result {
1042         struct completion completion;
1043         int error;
1044 };
1045
1046 static void mv_cesa_hmac_ahash_complete(struct crypto_async_request *req,
1047                                         int error)
1048 {
1049         struct mv_cesa_ahash_result *result = req->data;
1050
1051         if (error == -EINPROGRESS)
1052                 return;
1053
1054         result->error = error;
1055         complete(&result->completion);
1056 }
1057
1058 static int mv_cesa_ahmac_iv_state_init(struct ahash_request *req, u8 *pad,
1059                                        void *state, unsigned int blocksize)
1060 {
1061         struct mv_cesa_ahash_result result;
1062         struct scatterlist sg;
1063         int ret;
1064
1065         ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1066                                    mv_cesa_hmac_ahash_complete, &result);
1067         sg_init_one(&sg, pad, blocksize);
1068         ahash_request_set_crypt(req, &sg, pad, blocksize);
1069         init_completion(&result.completion);
1070
1071         ret = crypto_ahash_init(req);
1072         if (ret)
1073                 return ret;
1074
1075         ret = crypto_ahash_update(req);
1076         if (ret && ret != -EINPROGRESS)
1077                 return ret;
1078
1079         wait_for_completion_interruptible(&result.completion);
1080         if (result.error)
1081                 return result.error;
1082
1083         ret = crypto_ahash_export(req, state);
1084         if (ret)
1085                 return ret;
1086
1087         return 0;
1088 }
1089
1090 static int mv_cesa_ahmac_pad_init(struct ahash_request *req,
1091                                   const u8 *key, unsigned int keylen,
1092                                   u8 *ipad, u8 *opad,
1093                                   unsigned int blocksize)
1094 {
1095         struct mv_cesa_ahash_result result;
1096         struct scatterlist sg;
1097         int ret;
1098         int i;
1099
1100         if (keylen <= blocksize) {
1101                 memcpy(ipad, key, keylen);
1102         } else {
1103                 u8 *keydup = kmemdup(key, keylen, GFP_KERNEL);
1104
1105                 if (!keydup)
1106                         return -ENOMEM;
1107
1108                 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1109                                            mv_cesa_hmac_ahash_complete,
1110                                            &result);
1111                 sg_init_one(&sg, keydup, keylen);
1112                 ahash_request_set_crypt(req, &sg, ipad, keylen);
1113                 init_completion(&result.completion);
1114
1115                 ret = crypto_ahash_digest(req);
1116                 if (ret == -EINPROGRESS) {
1117                         wait_for_completion_interruptible(&result.completion);
1118                         ret = result.error;
1119                 }
1120
1121                 /* Set the memory region to 0 to avoid any leak. */
1122                 memset(keydup, 0, keylen);
1123                 kfree(keydup);
1124
1125                 if (ret)
1126                         return ret;
1127
1128                 keylen = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
1129         }
1130
1131         memset(ipad + keylen, 0, blocksize - keylen);
1132         memcpy(opad, ipad, blocksize);
1133
1134         for (i = 0; i < blocksize; i++) {
1135                 ipad[i] ^= 0x36;
1136                 opad[i] ^= 0x5c;
1137         }
1138
1139         return 0;
1140 }
1141
1142 static int mv_cesa_ahmac_setkey(const char *hash_alg_name,
1143                                 const u8 *key, unsigned int keylen,
1144                                 void *istate, void *ostate)
1145 {
1146         struct ahash_request *req;
1147         struct crypto_ahash *tfm;
1148         unsigned int blocksize;
1149         u8 *ipad = NULL;
1150         u8 *opad;
1151         int ret;
1152
1153         tfm = crypto_alloc_ahash(hash_alg_name, CRYPTO_ALG_TYPE_AHASH,
1154                                  CRYPTO_ALG_TYPE_AHASH_MASK);
1155         if (IS_ERR(tfm))
1156                 return PTR_ERR(tfm);
1157
1158         req = ahash_request_alloc(tfm, GFP_KERNEL);
1159         if (!req) {
1160                 ret = -ENOMEM;
1161                 goto free_ahash;
1162         }
1163
1164         crypto_ahash_clear_flags(tfm, ~0);
1165
1166         blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1167
1168         ipad = kzalloc(2 * blocksize, GFP_KERNEL);
1169         if (!ipad) {
1170                 ret = -ENOMEM;
1171                 goto free_req;
1172         }
1173
1174         opad = ipad + blocksize;
1175
1176         ret = mv_cesa_ahmac_pad_init(req, key, keylen, ipad, opad, blocksize);
1177         if (ret)
1178                 goto free_ipad;
1179
1180         ret = mv_cesa_ahmac_iv_state_init(req, ipad, istate, blocksize);
1181         if (ret)
1182                 goto free_ipad;
1183
1184         ret = mv_cesa_ahmac_iv_state_init(req, opad, ostate, blocksize);
1185
1186 free_ipad:
1187         kfree(ipad);
1188 free_req:
1189         ahash_request_free(req);
1190 free_ahash:
1191         crypto_free_ahash(tfm);
1192
1193         return ret;
1194 }
1195
1196 static int mv_cesa_ahmac_cra_init(struct crypto_tfm *tfm)
1197 {
1198         struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(tfm);
1199
1200         ctx->base.ops = &mv_cesa_ahash_req_ops;
1201
1202         crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
1203                                  sizeof(struct mv_cesa_ahash_req));
1204         return 0;
1205 }
1206
1207 static int mv_cesa_ahmac_md5_init(struct ahash_request *req)
1208 {
1209         struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
1210         struct mv_cesa_op_ctx tmpl = { };
1211
1212         mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_HMAC_MD5);
1213         memcpy(tmpl.ctx.hash.iv, ctx->iv, sizeof(ctx->iv));
1214
1215         mv_cesa_ahash_init(req, &tmpl, true);
1216
1217         return 0;
1218 }
1219
1220 static int mv_cesa_ahmac_md5_setkey(struct crypto_ahash *tfm, const u8 *key,
1221                                     unsigned int keylen)
1222 {
1223         struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1224         struct md5_state istate, ostate;
1225         int ret, i;
1226
1227         ret = mv_cesa_ahmac_setkey("mv-md5", key, keylen, &istate, &ostate);
1228         if (ret)
1229                 return ret;
1230
1231         for (i = 0; i < ARRAY_SIZE(istate.hash); i++)
1232                 ctx->iv[i] = be32_to_cpu(istate.hash[i]);
1233
1234         for (i = 0; i < ARRAY_SIZE(ostate.hash); i++)
1235                 ctx->iv[i + 8] = be32_to_cpu(ostate.hash[i]);
1236
1237         return 0;
1238 }
1239
1240 static int mv_cesa_ahmac_md5_digest(struct ahash_request *req)
1241 {
1242         int ret;
1243
1244         ret = mv_cesa_ahmac_md5_init(req);
1245         if (ret)
1246                 return ret;
1247
1248         return mv_cesa_ahash_finup(req);
1249 }
1250
1251 struct ahash_alg mv_ahmac_md5_alg = {
1252         .init = mv_cesa_ahmac_md5_init,
1253         .update = mv_cesa_ahash_update,
1254         .final = mv_cesa_ahash_final,
1255         .finup = mv_cesa_ahash_finup,
1256         .digest = mv_cesa_ahmac_md5_digest,
1257         .setkey = mv_cesa_ahmac_md5_setkey,
1258         .export = mv_cesa_md5_export,
1259         .import = mv_cesa_md5_import,
1260         .halg = {
1261                 .digestsize = MD5_DIGEST_SIZE,
1262                 .statesize = sizeof(struct md5_state),
1263                 .base = {
1264                         .cra_name = "hmac(md5)",
1265                         .cra_driver_name = "mv-hmac-md5",
1266                         .cra_priority = 300,
1267                         .cra_flags = CRYPTO_ALG_ASYNC |
1268                                      CRYPTO_ALG_KERN_DRIVER_ONLY,
1269                         .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
1270                         .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx),
1271                         .cra_init = mv_cesa_ahmac_cra_init,
1272                         .cra_module = THIS_MODULE,
1273                  }
1274         }
1275 };
1276
1277 static int mv_cesa_ahmac_sha1_init(struct ahash_request *req)
1278 {
1279         struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
1280         struct mv_cesa_op_ctx tmpl = { };
1281
1282         mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_HMAC_SHA1);
1283         memcpy(tmpl.ctx.hash.iv, ctx->iv, sizeof(ctx->iv));
1284
1285         mv_cesa_ahash_init(req, &tmpl, false);
1286
1287         return 0;
1288 }
1289
1290 static int mv_cesa_ahmac_sha1_setkey(struct crypto_ahash *tfm, const u8 *key,
1291                                      unsigned int keylen)
1292 {
1293         struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1294         struct sha1_state istate, ostate;
1295         int ret, i;
1296
1297         ret = mv_cesa_ahmac_setkey("mv-sha1", key, keylen, &istate, &ostate);
1298         if (ret)
1299                 return ret;
1300
1301         for (i = 0; i < ARRAY_SIZE(istate.state); i++)
1302                 ctx->iv[i] = be32_to_cpu(istate.state[i]);
1303
1304         for (i = 0; i < ARRAY_SIZE(ostate.state); i++)
1305                 ctx->iv[i + 8] = be32_to_cpu(ostate.state[i]);
1306
1307         return 0;
1308 }
1309
1310 static int mv_cesa_ahmac_sha1_digest(struct ahash_request *req)
1311 {
1312         int ret;
1313
1314         ret = mv_cesa_ahmac_sha1_init(req);
1315         if (ret)
1316                 return ret;
1317
1318         return mv_cesa_ahash_finup(req);
1319 }
1320
1321 struct ahash_alg mv_ahmac_sha1_alg = {
1322         .init = mv_cesa_ahmac_sha1_init,
1323         .update = mv_cesa_ahash_update,
1324         .final = mv_cesa_ahash_final,
1325         .finup = mv_cesa_ahash_finup,
1326         .digest = mv_cesa_ahmac_sha1_digest,
1327         .setkey = mv_cesa_ahmac_sha1_setkey,
1328         .export = mv_cesa_sha1_export,
1329         .import = mv_cesa_sha1_import,
1330         .halg = {
1331                 .digestsize = SHA1_DIGEST_SIZE,
1332                 .statesize = sizeof(struct sha1_state),
1333                 .base = {
1334                         .cra_name = "hmac(sha1)",
1335                         .cra_driver_name = "mv-hmac-sha1",
1336                         .cra_priority = 300,
1337                         .cra_flags = CRYPTO_ALG_ASYNC |
1338                                      CRYPTO_ALG_KERN_DRIVER_ONLY,
1339                         .cra_blocksize = SHA1_BLOCK_SIZE,
1340                         .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx),
1341                         .cra_init = mv_cesa_ahmac_cra_init,
1342                         .cra_module = THIS_MODULE,
1343                  }
1344         }
1345 };
1346
1347 static int mv_cesa_ahmac_sha256_setkey(struct crypto_ahash *tfm, const u8 *key,
1348                                        unsigned int keylen)
1349 {
1350         struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1351         struct sha256_state istate, ostate;
1352         int ret, i;
1353
1354         ret = mv_cesa_ahmac_setkey("mv-sha256", key, keylen, &istate, &ostate);
1355         if (ret)
1356                 return ret;
1357
1358         for (i = 0; i < ARRAY_SIZE(istate.state); i++)
1359                 ctx->iv[i] = be32_to_cpu(istate.state[i]);
1360
1361         for (i = 0; i < ARRAY_SIZE(ostate.state); i++)
1362                 ctx->iv[i + 8] = be32_to_cpu(ostate.state[i]);
1363
1364         return 0;
1365 }
1366
1367 static int mv_cesa_ahmac_sha256_init(struct ahash_request *req)
1368 {
1369         struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
1370         struct mv_cesa_op_ctx tmpl = { };
1371
1372         mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_HMAC_SHA256);
1373         memcpy(tmpl.ctx.hash.iv, ctx->iv, sizeof(ctx->iv));
1374
1375         mv_cesa_ahash_init(req, &tmpl, false);
1376
1377         return 0;
1378 }
1379
1380 static int mv_cesa_ahmac_sha256_digest(struct ahash_request *req)
1381 {
1382         int ret;
1383
1384         ret = mv_cesa_ahmac_sha256_init(req);
1385         if (ret)
1386                 return ret;
1387
1388         return mv_cesa_ahash_finup(req);
1389 }
1390
1391 struct ahash_alg mv_ahmac_sha256_alg = {
1392         .init = mv_cesa_ahmac_sha256_init,
1393         .update = mv_cesa_ahash_update,
1394         .final = mv_cesa_ahash_final,
1395         .finup = mv_cesa_ahash_finup,
1396         .digest = mv_cesa_ahmac_sha256_digest,
1397         .setkey = mv_cesa_ahmac_sha256_setkey,
1398         .export = mv_cesa_sha256_export,
1399         .import = mv_cesa_sha256_import,
1400         .halg = {
1401                 .digestsize = SHA256_DIGEST_SIZE,
1402                 .statesize = sizeof(struct sha256_state),
1403                 .base = {
1404                         .cra_name = "hmac(sha256)",
1405                         .cra_driver_name = "mv-hmac-sha256",
1406                         .cra_priority = 300,
1407                         .cra_flags = CRYPTO_ALG_ASYNC |
1408                                      CRYPTO_ALG_KERN_DRIVER_ONLY,
1409                         .cra_blocksize = SHA256_BLOCK_SIZE,
1410                         .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx),
1411                         .cra_init = mv_cesa_ahmac_cra_init,
1412                         .cra_module = THIS_MODULE,
1413                  }
1414         }
1415 };