e6865b49b1df6f568c16a7f9d00989c0fa39aed6
[platform/kernel/linux-starfive.git] / drivers / crypto / allwinner / sun8i-ss / sun8i-ss-hash.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * sun8i-ss-hash.c - hardware cryptographic offloader for
4  * Allwinner A80/A83T SoC
5  *
6  * Copyright (C) 2015-2020 Corentin Labbe <clabbe@baylibre.com>
7  *
8  * This file add support for MD5 and SHA1/SHA224/SHA256.
9  *
10  * You could find the datasheet in Documentation/arch/arm/sunxi.rst
11  */
12 #include <linux/bottom_half.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/scatterlist.h>
16 #include <crypto/internal/hash.h>
17 #include <crypto/hmac.h>
18 #include <crypto/scatterwalk.h>
19 #include <crypto/sha1.h>
20 #include <crypto/sha2.h>
21 #include <crypto/md5.h>
22 #include "sun8i-ss.h"
23
24 static int sun8i_ss_hashkey(struct sun8i_ss_hash_tfm_ctx *tfmctx, const u8 *key,
25                             unsigned int keylen)
26 {
27         struct crypto_shash *xtfm;
28         struct shash_desc *sdesc;
29         size_t len;
30         int ret = 0;
31
32         xtfm = crypto_alloc_shash("sha1", 0, CRYPTO_ALG_NEED_FALLBACK);
33         if (IS_ERR(xtfm))
34                 return PTR_ERR(xtfm);
35
36         len = sizeof(*sdesc) + crypto_shash_descsize(xtfm);
37         sdesc = kmalloc(len, GFP_KERNEL);
38         if (!sdesc) {
39                 ret = -ENOMEM;
40                 goto err_hashkey_sdesc;
41         }
42         sdesc->tfm = xtfm;
43
44         ret = crypto_shash_init(sdesc);
45         if (ret) {
46                 dev_err(tfmctx->ss->dev, "shash init error ret=%d\n", ret);
47                 goto err_hashkey;
48         }
49         ret = crypto_shash_finup(sdesc, key, keylen, tfmctx->key);
50         if (ret)
51                 dev_err(tfmctx->ss->dev, "shash finup error\n");
52 err_hashkey:
53         kfree(sdesc);
54 err_hashkey_sdesc:
55         crypto_free_shash(xtfm);
56         return ret;
57 }
58
59 int sun8i_ss_hmac_setkey(struct crypto_ahash *ahash, const u8 *key,
60                          unsigned int keylen)
61 {
62         struct sun8i_ss_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(ahash);
63         struct ahash_alg *alg = __crypto_ahash_alg(ahash->base.__crt_alg);
64         struct sun8i_ss_alg_template *algt;
65         int digestsize, i;
66         int bs = crypto_ahash_blocksize(ahash);
67         int ret;
68
69         algt = container_of(alg, struct sun8i_ss_alg_template, alg.hash);
70         digestsize = algt->alg.hash.halg.digestsize;
71
72         if (keylen > bs) {
73                 ret = sun8i_ss_hashkey(tfmctx, key, keylen);
74                 if (ret)
75                         return ret;
76                 tfmctx->keylen = digestsize;
77         } else {
78                 tfmctx->keylen = keylen;
79                 memcpy(tfmctx->key, key, keylen);
80         }
81
82         tfmctx->ipad = kzalloc(bs, GFP_KERNEL);
83         if (!tfmctx->ipad)
84                 return -ENOMEM;
85         tfmctx->opad = kzalloc(bs, GFP_KERNEL);
86         if (!tfmctx->opad) {
87                 ret = -ENOMEM;
88                 goto err_opad;
89         }
90
91         memset(tfmctx->key + tfmctx->keylen, 0, bs - tfmctx->keylen);
92         memcpy(tfmctx->ipad, tfmctx->key, tfmctx->keylen);
93         memcpy(tfmctx->opad, tfmctx->key, tfmctx->keylen);
94         for (i = 0; i < bs; i++) {
95                 tfmctx->ipad[i] ^= HMAC_IPAD_VALUE;
96                 tfmctx->opad[i] ^= HMAC_OPAD_VALUE;
97         }
98
99         ret = crypto_ahash_setkey(tfmctx->fallback_tfm, key, keylen);
100         if (!ret)
101                 return 0;
102
103         memzero_explicit(tfmctx->key, keylen);
104         kfree_sensitive(tfmctx->opad);
105 err_opad:
106         kfree_sensitive(tfmctx->ipad);
107         return ret;
108 }
109
110 int sun8i_ss_hash_crainit(struct crypto_tfm *tfm)
111 {
112         struct sun8i_ss_hash_tfm_ctx *op = crypto_tfm_ctx(tfm);
113         struct ahash_alg *alg = __crypto_ahash_alg(tfm->__crt_alg);
114         struct sun8i_ss_alg_template *algt;
115         int err;
116
117         memset(op, 0, sizeof(struct sun8i_ss_hash_tfm_ctx));
118
119         algt = container_of(alg, struct sun8i_ss_alg_template, alg.hash);
120         op->ss = algt->ss;
121
122         op->enginectx.op.do_one_request = sun8i_ss_hash_run;
123
124         /* FALLBACK */
125         op->fallback_tfm = crypto_alloc_ahash(crypto_tfm_alg_name(tfm), 0,
126                                               CRYPTO_ALG_NEED_FALLBACK);
127         if (IS_ERR(op->fallback_tfm)) {
128                 dev_err(algt->ss->dev, "Fallback driver could no be loaded\n");
129                 return PTR_ERR(op->fallback_tfm);
130         }
131
132         if (algt->alg.hash.halg.statesize < crypto_ahash_statesize(op->fallback_tfm))
133                 algt->alg.hash.halg.statesize = crypto_ahash_statesize(op->fallback_tfm);
134
135         crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
136                                  sizeof(struct sun8i_ss_hash_reqctx) +
137                                  crypto_ahash_reqsize(op->fallback_tfm));
138
139         memcpy(algt->fbname, crypto_tfm_alg_driver_name(&op->fallback_tfm->base), CRYPTO_MAX_ALG_NAME);
140
141         err = pm_runtime_get_sync(op->ss->dev);
142         if (err < 0)
143                 goto error_pm;
144         return 0;
145 error_pm:
146         pm_runtime_put_noidle(op->ss->dev);
147         crypto_free_ahash(op->fallback_tfm);
148         return err;
149 }
150
151 void sun8i_ss_hash_craexit(struct crypto_tfm *tfm)
152 {
153         struct sun8i_ss_hash_tfm_ctx *tfmctx = crypto_tfm_ctx(tfm);
154
155         kfree_sensitive(tfmctx->ipad);
156         kfree_sensitive(tfmctx->opad);
157
158         crypto_free_ahash(tfmctx->fallback_tfm);
159         pm_runtime_put_sync_suspend(tfmctx->ss->dev);
160 }
161
162 int sun8i_ss_hash_init(struct ahash_request *areq)
163 {
164         struct sun8i_ss_hash_reqctx *rctx = ahash_request_ctx(areq);
165         struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
166         struct sun8i_ss_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
167
168         memset(rctx, 0, sizeof(struct sun8i_ss_hash_reqctx));
169
170         ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
171         rctx->fallback_req.base.flags = areq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
172
173         return crypto_ahash_init(&rctx->fallback_req);
174 }
175
176 int sun8i_ss_hash_export(struct ahash_request *areq, void *out)
177 {
178         struct sun8i_ss_hash_reqctx *rctx = ahash_request_ctx(areq);
179         struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
180         struct sun8i_ss_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
181
182         ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
183         rctx->fallback_req.base.flags = areq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
184
185         return crypto_ahash_export(&rctx->fallback_req, out);
186 }
187
188 int sun8i_ss_hash_import(struct ahash_request *areq, const void *in)
189 {
190         struct sun8i_ss_hash_reqctx *rctx = ahash_request_ctx(areq);
191         struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
192         struct sun8i_ss_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
193
194         ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
195         rctx->fallback_req.base.flags = areq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
196
197         return crypto_ahash_import(&rctx->fallback_req, in);
198 }
199
200 int sun8i_ss_hash_final(struct ahash_request *areq)
201 {
202         struct sun8i_ss_hash_reqctx *rctx = ahash_request_ctx(areq);
203         struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
204         struct sun8i_ss_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
205 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
206         struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
207         struct sun8i_ss_alg_template *algt;
208 #endif
209
210         ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
211         rctx->fallback_req.base.flags = areq->base.flags &
212                                         CRYPTO_TFM_REQ_MAY_SLEEP;
213         rctx->fallback_req.result = areq->result;
214
215 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
216         algt = container_of(alg, struct sun8i_ss_alg_template, alg.hash);
217         algt->stat_fb++;
218 #endif
219
220         return crypto_ahash_final(&rctx->fallback_req);
221 }
222
223 int sun8i_ss_hash_update(struct ahash_request *areq)
224 {
225         struct sun8i_ss_hash_reqctx *rctx = ahash_request_ctx(areq);
226         struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
227         struct sun8i_ss_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
228
229         ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
230         rctx->fallback_req.base.flags = areq->base.flags &
231                                         CRYPTO_TFM_REQ_MAY_SLEEP;
232         rctx->fallback_req.nbytes = areq->nbytes;
233         rctx->fallback_req.src = areq->src;
234
235         return crypto_ahash_update(&rctx->fallback_req);
236 }
237
238 int sun8i_ss_hash_finup(struct ahash_request *areq)
239 {
240         struct sun8i_ss_hash_reqctx *rctx = ahash_request_ctx(areq);
241         struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
242         struct sun8i_ss_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
243 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
244         struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
245         struct sun8i_ss_alg_template *algt;
246 #endif
247
248         ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
249         rctx->fallback_req.base.flags = areq->base.flags &
250                                         CRYPTO_TFM_REQ_MAY_SLEEP;
251
252         rctx->fallback_req.nbytes = areq->nbytes;
253         rctx->fallback_req.src = areq->src;
254         rctx->fallback_req.result = areq->result;
255 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
256         algt = container_of(alg, struct sun8i_ss_alg_template, alg.hash);
257         algt->stat_fb++;
258 #endif
259
260         return crypto_ahash_finup(&rctx->fallback_req);
261 }
262
263 static int sun8i_ss_hash_digest_fb(struct ahash_request *areq)
264 {
265         struct sun8i_ss_hash_reqctx *rctx = ahash_request_ctx(areq);
266         struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
267         struct sun8i_ss_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
268 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
269         struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
270         struct sun8i_ss_alg_template *algt;
271 #endif
272
273         ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
274         rctx->fallback_req.base.flags = areq->base.flags &
275                                         CRYPTO_TFM_REQ_MAY_SLEEP;
276
277         rctx->fallback_req.nbytes = areq->nbytes;
278         rctx->fallback_req.src = areq->src;
279         rctx->fallback_req.result = areq->result;
280 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
281         algt = container_of(alg, struct sun8i_ss_alg_template, alg.hash);
282         algt->stat_fb++;
283 #endif
284
285         return crypto_ahash_digest(&rctx->fallback_req);
286 }
287
288 static int sun8i_ss_run_hash_task(struct sun8i_ss_dev *ss,
289                                   struct sun8i_ss_hash_reqctx *rctx,
290                                   const char *name)
291 {
292         int flow = rctx->flow;
293         u32 v = SS_START;
294         int i;
295
296 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
297         ss->flows[flow].stat_req++;
298 #endif
299
300         /* choose between stream0/stream1 */
301         if (flow)
302                 v |= SS_FLOW1;
303         else
304                 v |= SS_FLOW0;
305
306         v |= rctx->method;
307
308         for (i = 0; i < MAX_SG; i++) {
309                 if (!rctx->t_dst[i].addr)
310                         break;
311
312                 mutex_lock(&ss->mlock);
313                 if (i > 0) {
314                         v |= BIT(17);
315                         writel(rctx->t_dst[i - 1].addr, ss->base + SS_KEY_ADR_REG);
316                         writel(rctx->t_dst[i - 1].addr, ss->base + SS_IV_ADR_REG);
317                 }
318
319                 dev_dbg(ss->dev,
320                         "Processing SG %d on flow %d %s ctl=%x %d to %d method=%x src=%x dst=%x\n",
321                         i, flow, name, v,
322                         rctx->t_src[i].len, rctx->t_dst[i].len,
323                         rctx->method, rctx->t_src[i].addr, rctx->t_dst[i].addr);
324
325                 writel(rctx->t_src[i].addr, ss->base + SS_SRC_ADR_REG);
326                 writel(rctx->t_dst[i].addr, ss->base + SS_DST_ADR_REG);
327                 writel(rctx->t_src[i].len, ss->base + SS_LEN_ADR_REG);
328                 writel(BIT(0) | BIT(1), ss->base + SS_INT_CTL_REG);
329
330                 reinit_completion(&ss->flows[flow].complete);
331                 ss->flows[flow].status = 0;
332                 wmb();
333
334                 writel(v, ss->base + SS_CTL_REG);
335                 mutex_unlock(&ss->mlock);
336                 wait_for_completion_interruptible_timeout(&ss->flows[flow].complete,
337                                                           msecs_to_jiffies(2000));
338                 if (ss->flows[flow].status == 0) {
339                         dev_err(ss->dev, "DMA timeout for %s\n", name);
340                         return -EFAULT;
341                 }
342         }
343
344         return 0;
345 }
346
347 static bool sun8i_ss_hash_need_fallback(struct ahash_request *areq)
348 {
349         struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
350         struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
351         struct sun8i_ss_alg_template *algt;
352         struct scatterlist *sg;
353
354         algt = container_of(alg, struct sun8i_ss_alg_template, alg.hash);
355
356         if (areq->nbytes == 0) {
357                 algt->stat_fb_len++;
358                 return true;
359         }
360
361         if (areq->nbytes >= MAX_PAD_SIZE - 64) {
362                 algt->stat_fb_len++;
363                 return true;
364         }
365
366         /* we need to reserve one SG for the padding one */
367         if (sg_nents(areq->src) > MAX_SG - 1) {
368                 algt->stat_fb_sgnum++;
369                 return true;
370         }
371
372         sg = areq->src;
373         while (sg) {
374                 /* SS can operate hash only on full block size
375                  * since SS support only MD5,sha1,sha224 and sha256, blocksize
376                  * is always 64
377                  */
378                 /* Only the last block could be bounced to the pad buffer */
379                 if (sg->length % 64 && sg_next(sg)) {
380                         algt->stat_fb_sglen++;
381                         return true;
382                 }
383                 if (!IS_ALIGNED(sg->offset, sizeof(u32))) {
384                         algt->stat_fb_align++;
385                         return true;
386                 }
387                 if (sg->length % 4) {
388                         algt->stat_fb_sglen++;
389                         return true;
390                 }
391                 sg = sg_next(sg);
392         }
393         return false;
394 }
395
396 int sun8i_ss_hash_digest(struct ahash_request *areq)
397 {
398         struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
399         struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
400         struct sun8i_ss_hash_reqctx *rctx = ahash_request_ctx(areq);
401         struct sun8i_ss_alg_template *algt;
402         struct sun8i_ss_dev *ss;
403         struct crypto_engine *engine;
404         int e;
405
406         if (sun8i_ss_hash_need_fallback(areq))
407                 return sun8i_ss_hash_digest_fb(areq);
408
409         algt = container_of(alg, struct sun8i_ss_alg_template, alg.hash);
410         ss = algt->ss;
411
412         e = sun8i_ss_get_engine_number(ss);
413         rctx->flow = e;
414         engine = ss->flows[e].engine;
415
416         return crypto_transfer_hash_request_to_engine(engine, areq);
417 }
418
419 static u64 hash_pad(__le32 *buf, unsigned int bufsize, u64 padi, u64 byte_count, bool le, int bs)
420 {
421         u64 fill, min_fill, j, k;
422         __be64 *bebits;
423         __le64 *lebits;
424
425         j = padi;
426         buf[j++] = cpu_to_le32(0x80);
427
428         if (bs == 64) {
429                 fill = 64 - (byte_count % 64);
430                 min_fill = 2 * sizeof(u32) + sizeof(u32);
431         } else {
432                 fill = 128 - (byte_count % 128);
433                 min_fill = 4 * sizeof(u32) + sizeof(u32);
434         }
435
436         if (fill < min_fill)
437                 fill += bs;
438
439         k = j;
440         j += (fill - min_fill) / sizeof(u32);
441         if (j * 4 > bufsize) {
442                 pr_err("%s OVERFLOW %llu\n", __func__, j);
443                 return 0;
444         }
445         for (; k < j; k++)
446                 buf[k] = 0;
447
448         if (le) {
449                 /* MD5 */
450                 lebits = (__le64 *)&buf[j];
451                 *lebits = cpu_to_le64(byte_count << 3);
452                 j += 2;
453         } else {
454                 if (bs == 64) {
455                         /* sha1 sha224 sha256 */
456                         bebits = (__be64 *)&buf[j];
457                         *bebits = cpu_to_be64(byte_count << 3);
458                         j += 2;
459                 } else {
460                         /* sha384 sha512*/
461                         bebits = (__be64 *)&buf[j];
462                         *bebits = cpu_to_be64(byte_count >> 61);
463                         j += 2;
464                         bebits = (__be64 *)&buf[j];
465                         *bebits = cpu_to_be64(byte_count << 3);
466                         j += 2;
467                 }
468         }
469         if (j * 4 > bufsize) {
470                 pr_err("%s OVERFLOW %llu\n", __func__, j);
471                 return 0;
472         }
473
474         return j;
475 }
476
477 /* sun8i_ss_hash_run - run an ahash request
478  * Send the data of the request to the SS along with an extra SG with padding
479  */
480 int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq)
481 {
482         struct ahash_request *areq = container_of(breq, struct ahash_request, base);
483         struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
484         struct sun8i_ss_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm);
485         struct ahash_alg *alg = __crypto_ahash_alg(tfm->base.__crt_alg);
486         struct sun8i_ss_hash_reqctx *rctx = ahash_request_ctx(areq);
487         struct sun8i_ss_alg_template *algt;
488         struct sun8i_ss_dev *ss;
489         struct scatterlist *sg;
490         int bs = crypto_ahash_blocksize(tfm);
491         int nr_sgs, err, digestsize;
492         unsigned int len;
493         u64 byte_count;
494         void *pad, *result;
495         int j, i, k, todo;
496         dma_addr_t addr_res, addr_pad, addr_xpad;
497         __le32 *bf;
498         /* HMAC step:
499          * 0: normal hashing
500          * 1: IPAD
501          * 2: OPAD
502          */
503         int hmac = 0;
504
505         algt = container_of(alg, struct sun8i_ss_alg_template, alg.hash);
506         ss = algt->ss;
507
508         digestsize = algt->alg.hash.halg.digestsize;
509         if (digestsize == SHA224_DIGEST_SIZE)
510                 digestsize = SHA256_DIGEST_SIZE;
511
512         result = ss->flows[rctx->flow].result;
513         pad = ss->flows[rctx->flow].pad;
514         bf = (__le32 *)pad;
515
516         for (i = 0; i < MAX_SG; i++) {
517                 rctx->t_dst[i].addr = 0;
518                 rctx->t_dst[i].len = 0;
519         }
520
521 #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
522         algt->stat_req++;
523 #endif
524
525         rctx->method = ss->variant->alg_hash[algt->ss_algo_id];
526
527         nr_sgs = dma_map_sg(ss->dev, areq->src, sg_nents(areq->src), DMA_TO_DEVICE);
528         if (nr_sgs <= 0 || nr_sgs > MAX_SG) {
529                 dev_err(ss->dev, "Invalid sg number %d\n", nr_sgs);
530                 err = -EINVAL;
531                 goto theend;
532         }
533
534         addr_res = dma_map_single(ss->dev, result, digestsize, DMA_FROM_DEVICE);
535         if (dma_mapping_error(ss->dev, addr_res)) {
536                 dev_err(ss->dev, "DMA map dest\n");
537                 err = -EINVAL;
538                 goto err_dma_result;
539         }
540
541         j = 0;
542         len = areq->nbytes;
543         sg = areq->src;
544         i = 0;
545         while (len > 0 && sg) {
546                 if (sg_dma_len(sg) == 0) {
547                         sg = sg_next(sg);
548                         continue;
549                 }
550                 todo = min(len, sg_dma_len(sg));
551                 /* only the last SG could be with a size not modulo64 */
552                 if (todo % 64 == 0) {
553                         rctx->t_src[i].addr = sg_dma_address(sg);
554                         rctx->t_src[i].len = todo / 4;
555                         rctx->t_dst[i].addr = addr_res;
556                         rctx->t_dst[i].len = digestsize / 4;
557                         len -= todo;
558                 } else {
559                         scatterwalk_map_and_copy(bf, sg, 0, todo, 0);
560                         j += todo / 4;
561                         len -= todo;
562                 }
563                 sg = sg_next(sg);
564                 i++;
565         }
566         if (len > 0) {
567                 dev_err(ss->dev, "remaining len %d\n", len);
568                 err = -EINVAL;
569                 goto theend;
570         }
571
572         if (j > 0)
573                 i--;
574
575 retry:
576         byte_count = areq->nbytes;
577         if (tfmctx->keylen && hmac == 0) {
578                 hmac = 1;
579                 /* shift all SG one slot up, to free slot 0 for IPAD */
580                 for (k = 6; k >= 0; k--) {
581                         rctx->t_src[k + 1].addr = rctx->t_src[k].addr;
582                         rctx->t_src[k + 1].len = rctx->t_src[k].len;
583                         rctx->t_dst[k + 1].addr = rctx->t_dst[k].addr;
584                         rctx->t_dst[k + 1].len = rctx->t_dst[k].len;
585                 }
586                 addr_xpad = dma_map_single(ss->dev, tfmctx->ipad, bs, DMA_TO_DEVICE);
587                 err = dma_mapping_error(ss->dev, addr_xpad);
588                 if (err) {
589                         dev_err(ss->dev, "Fail to create DMA mapping of ipad\n");
590                         goto err_dma_xpad;
591                 }
592                 rctx->t_src[0].addr = addr_xpad;
593                 rctx->t_src[0].len = bs / 4;
594                 rctx->t_dst[0].addr = addr_res;
595                 rctx->t_dst[0].len = digestsize / 4;
596                 i++;
597                 byte_count = areq->nbytes + bs;
598         }
599         if (tfmctx->keylen && hmac == 2) {
600                 for (i = 0; i < MAX_SG; i++) {
601                         rctx->t_src[i].addr = 0;
602                         rctx->t_src[i].len = 0;
603                         rctx->t_dst[i].addr = 0;
604                         rctx->t_dst[i].len = 0;
605                 }
606
607                 addr_res = dma_map_single(ss->dev, result, digestsize, DMA_FROM_DEVICE);
608                 if (dma_mapping_error(ss->dev, addr_res)) {
609                         dev_err(ss->dev, "Fail to create DMA mapping of result\n");
610                         err = -EINVAL;
611                         goto err_dma_result;
612                 }
613                 addr_xpad = dma_map_single(ss->dev, tfmctx->opad, bs, DMA_TO_DEVICE);
614                 err = dma_mapping_error(ss->dev, addr_xpad);
615                 if (err) {
616                         dev_err(ss->dev, "Fail to create DMA mapping of opad\n");
617                         goto err_dma_xpad;
618                 }
619                 rctx->t_src[0].addr = addr_xpad;
620                 rctx->t_src[0].len = bs / 4;
621
622                 memcpy(bf, result, digestsize);
623                 j = digestsize / 4;
624                 i = 1;
625                 byte_count = digestsize + bs;
626
627                 rctx->t_dst[0].addr = addr_res;
628                 rctx->t_dst[0].len = digestsize / 4;
629         }
630
631         switch (algt->ss_algo_id) {
632         case SS_ID_HASH_MD5:
633                 j = hash_pad(bf, 4096, j, byte_count, true, bs);
634                 break;
635         case SS_ID_HASH_SHA1:
636         case SS_ID_HASH_SHA224:
637         case SS_ID_HASH_SHA256:
638                 j = hash_pad(bf, 4096, j, byte_count, false, bs);
639                 break;
640         }
641         if (!j) {
642                 err = -EINVAL;
643                 goto theend;
644         }
645
646         addr_pad = dma_map_single(ss->dev, pad, j * 4, DMA_TO_DEVICE);
647         if (dma_mapping_error(ss->dev, addr_pad)) {
648                 dev_err(ss->dev, "DMA error on padding SG\n");
649                 err = -EINVAL;
650                 goto err_dma_pad;
651         }
652         rctx->t_src[i].addr = addr_pad;
653         rctx->t_src[i].len = j;
654         rctx->t_dst[i].addr = addr_res;
655         rctx->t_dst[i].len = digestsize / 4;
656
657         err = sun8i_ss_run_hash_task(ss, rctx, crypto_tfm_alg_name(areq->base.tfm));
658
659         /*
660          * mini helper for checking dma map/unmap
661          * flow start for hmac = 0 (and HMAC = 1)
662          * HMAC = 0
663          * MAP src
664          * MAP res
665          *
666          * retry:
667          * if hmac then hmac = 1
668          *      MAP xpad (ipad)
669          * if hmac == 2
670          *      MAP res
671          *      MAP xpad (opad)
672          * MAP pad
673          * ACTION!
674          * UNMAP pad
675          * if hmac
676          *      UNMAP xpad
677          * UNMAP res
678          * if hmac < 2
679          *      UNMAP SRC
680          *
681          * if hmac = 1 then hmac = 2 goto retry
682          */
683
684         dma_unmap_single(ss->dev, addr_pad, j * 4, DMA_TO_DEVICE);
685
686 err_dma_pad:
687         if (hmac > 0)
688                 dma_unmap_single(ss->dev, addr_xpad, bs, DMA_TO_DEVICE);
689 err_dma_xpad:
690         dma_unmap_single(ss->dev, addr_res, digestsize, DMA_FROM_DEVICE);
691 err_dma_result:
692         if (hmac < 2)
693                 dma_unmap_sg(ss->dev, areq->src, sg_nents(areq->src),
694                              DMA_TO_DEVICE);
695         if (hmac == 1 && !err) {
696                 hmac = 2;
697                 goto retry;
698         }
699
700         if (!err)
701                 memcpy(areq->result, result, algt->alg.hash.halg.digestsize);
702 theend:
703         local_bh_disable();
704         crypto_finalize_hash_request(engine, breq, err);
705         local_bh_enable();
706         return 0;
707 }