d5275d914d095e86f5483c213bacbfb6a7c074dd
[linux-2.6-microblaze.git] / drivers / crypto / allwinner / sun4i-ss / sun4i-ss-cipher.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * sun4i-ss-cipher.c - hardware cryptographic accelerator for Allwinner A20 SoC
4  *
5  * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com>
6  *
7  * This file add support for AES cipher with 128,192,256 bits
8  * keysize in CBC and ECB mode.
9  * Add support also for DES and 3DES in CBC and ECB mode.
10  *
11  * You could find the datasheet in Documentation/arm/sunxi.rst
12  */
13 #include "sun4i-ss.h"
14
15 static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
16 {
17         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
18         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
19         struct sun4i_ss_ctx *ss = op->ss;
20         unsigned int ivsize = crypto_skcipher_ivsize(tfm);
21         struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
22         u32 mode = ctx->mode;
23         void *backup_iv = NULL;
24         /* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
25         u32 rx_cnt = SS_RX_DEFAULT;
26         u32 tx_cnt = 0;
27         u32 spaces;
28         u32 v;
29         int err = 0;
30         unsigned int i;
31         unsigned int ileft = areq->cryptlen;
32         unsigned int oleft = areq->cryptlen;
33         unsigned int todo;
34         unsigned long pi = 0, po = 0; /* progress for in and out */
35         bool miter_err;
36         struct sg_mapping_iter mi, mo;
37         unsigned int oi, oo; /* offset for in and out */
38         unsigned long flags;
39         struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
40         struct sun4i_ss_alg_template *algt;
41
42         if (!areq->cryptlen)
43                 return 0;
44
45         if (!areq->src || !areq->dst) {
46                 dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");
47                 return -EINVAL;
48         }
49
50         if (areq->iv && ivsize > 0 && mode & SS_DECRYPTION) {
51                 backup_iv = kzalloc(ivsize, GFP_KERNEL);
52                 if (!backup_iv)
53                         return -ENOMEM;
54                 scatterwalk_map_and_copy(backup_iv, areq->src, areq->cryptlen - ivsize, ivsize, 0);
55         }
56
57         if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG)) {
58                 algt = container_of(alg, struct sun4i_ss_alg_template, alg.crypto);
59                 algt->stat_opti++;
60                 algt->stat_bytes += areq->cryptlen;
61         }
62
63         spin_lock_irqsave(&ss->slock, flags);
64
65         for (i = 0; i < op->keylen / 4; i++)
66                 writesl(ss->base + SS_KEY0 + i * 4, &op->key[i], 1);
67
68         if (areq->iv) {
69                 for (i = 0; i < 4 && i < ivsize / 4; i++) {
70                         v = *(u32 *)(areq->iv + i * 4);
71                         writesl(ss->base + SS_IV0 + i * 4, &v, 1);
72                 }
73         }
74         writel(mode, ss->base + SS_CTL);
75
76
77         ileft = areq->cryptlen / 4;
78         oleft = areq->cryptlen / 4;
79         oi = 0;
80         oo = 0;
81         do {
82                 if (ileft) {
83                         sg_miter_start(&mi, areq->src, sg_nents(areq->src),
84                                         SG_MITER_FROM_SG | SG_MITER_ATOMIC);
85                         if (pi)
86                                 sg_miter_skip(&mi, pi);
87                         miter_err = sg_miter_next(&mi);
88                         if (!miter_err || !mi.addr) {
89                                 dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
90                                 err = -EINVAL;
91                                 goto release_ss;
92                         }
93                         todo = min(rx_cnt, ileft);
94                         todo = min_t(size_t, todo, (mi.length - oi) / 4);
95                         if (todo) {
96                                 ileft -= todo;
97                                 writesl(ss->base + SS_RXFIFO, mi.addr + oi, todo);
98                                 oi += todo * 4;
99                         }
100                         if (oi == mi.length) {
101                                 pi += mi.length;
102                                 oi = 0;
103                         }
104                         sg_miter_stop(&mi);
105                 }
106
107                 spaces = readl(ss->base + SS_FCSR);
108                 rx_cnt = SS_RXFIFO_SPACES(spaces);
109                 tx_cnt = SS_TXFIFO_SPACES(spaces);
110
111                 sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
112                                SG_MITER_TO_SG | SG_MITER_ATOMIC);
113                 if (po)
114                         sg_miter_skip(&mo, po);
115                 miter_err = sg_miter_next(&mo);
116                 if (!miter_err || !mo.addr) {
117                         dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
118                         err = -EINVAL;
119                         goto release_ss;
120                 }
121                 todo = min(tx_cnt, oleft);
122                 todo = min_t(size_t, todo, (mo.length - oo) / 4);
123                 if (todo) {
124                         oleft -= todo;
125                         readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);
126                         oo += todo * 4;
127                 }
128                 if (oo == mo.length) {
129                         oo = 0;
130                         po += mo.length;
131                 }
132                 sg_miter_stop(&mo);
133         } while (oleft);
134
135         if (areq->iv) {
136                 if (mode & SS_DECRYPTION) {
137                         memcpy(areq->iv, backup_iv, ivsize);
138                         kfree_sensitive(backup_iv);
139                 } else {
140                         scatterwalk_map_and_copy(areq->iv, areq->dst, areq->cryptlen - ivsize,
141                                                  ivsize, 0);
142                 }
143         }
144
145 release_ss:
146         writel(0, ss->base + SS_CTL);
147         spin_unlock_irqrestore(&ss->slock, flags);
148         return err;
149 }
150
151
152 static int noinline_for_stack sun4i_ss_cipher_poll_fallback(struct skcipher_request *areq)
153 {
154         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
155         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
156         struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
157         int err;
158         struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
159         struct sun4i_ss_alg_template *algt;
160
161         if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG)) {
162                 algt = container_of(alg, struct sun4i_ss_alg_template, alg.crypto);
163                 algt->stat_fb++;
164         }
165
166         skcipher_request_set_tfm(&ctx->fallback_req, op->fallback_tfm);
167         skcipher_request_set_callback(&ctx->fallback_req, areq->base.flags,
168                                       areq->base.complete, areq->base.data);
169         skcipher_request_set_crypt(&ctx->fallback_req, areq->src, areq->dst,
170                                    areq->cryptlen, areq->iv);
171         if (ctx->mode & SS_DECRYPTION)
172                 err = crypto_skcipher_decrypt(&ctx->fallback_req);
173         else
174                 err = crypto_skcipher_encrypt(&ctx->fallback_req);
175
176         return err;
177 }
178
179 /* Generic function that support SG with size not multiple of 4 */
180 static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
181 {
182         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
183         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
184         struct sun4i_ss_ctx *ss = op->ss;
185         int no_chunk = 1;
186         struct scatterlist *in_sg = areq->src;
187         struct scatterlist *out_sg = areq->dst;
188         unsigned int ivsize = crypto_skcipher_ivsize(tfm);
189         struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
190         struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
191         struct sun4i_ss_alg_template *algt;
192         u32 mode = ctx->mode;
193         /* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
194         u32 rx_cnt = SS_RX_DEFAULT;
195         u32 tx_cnt = 0;
196         u32 v;
197         u32 spaces;
198         int err = 0;
199         unsigned int i;
200         unsigned int ileft = areq->cryptlen;
201         unsigned int oleft = areq->cryptlen;
202         unsigned int todo;
203         void *backup_iv = NULL;
204         struct sg_mapping_iter mi, mo;
205         unsigned long pi = 0, po = 0; /* progress for in and out */
206         bool miter_err;
207         unsigned int oi, oo;    /* offset for in and out */
208         unsigned int ob = 0;    /* offset in buf */
209         unsigned int obo = 0;   /* offset in bufo*/
210         unsigned int obl = 0;   /* length of data in bufo */
211         unsigned long flags;
212         bool need_fallback = false;
213
214         if (!areq->cryptlen)
215                 return 0;
216
217         if (!areq->src || !areq->dst) {
218                 dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");
219                 return -EINVAL;
220         }
221
222         algt = container_of(alg, struct sun4i_ss_alg_template, alg.crypto);
223         if (areq->cryptlen % algt->alg.crypto.base.cra_blocksize)
224                 need_fallback = true;
225
226         /*
227          * if we have only SGs with size multiple of 4,
228          * we can use the SS optimized function
229          */
230         while (in_sg && no_chunk == 1) {
231                 if ((in_sg->length | in_sg->offset) & 3u)
232                         no_chunk = 0;
233                 in_sg = sg_next(in_sg);
234         }
235         while (out_sg && no_chunk == 1) {
236                 if ((out_sg->length | out_sg->offset) & 3u)
237                         no_chunk = 0;
238                 out_sg = sg_next(out_sg);
239         }
240
241         if (no_chunk == 1 && !need_fallback)
242                 return sun4i_ss_opti_poll(areq);
243
244         if (need_fallback)
245                 return sun4i_ss_cipher_poll_fallback(areq);
246
247         if (areq->iv && ivsize > 0 && mode & SS_DECRYPTION) {
248                 backup_iv = kzalloc(ivsize, GFP_KERNEL);
249                 if (!backup_iv)
250                         return -ENOMEM;
251                 scatterwalk_map_and_copy(backup_iv, areq->src, areq->cryptlen - ivsize, ivsize, 0);
252         }
253
254         if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG)) {
255                 algt->stat_req++;
256                 algt->stat_bytes += areq->cryptlen;
257         }
258
259         spin_lock_irqsave(&ss->slock, flags);
260
261         for (i = 0; i < op->keylen / 4; i++)
262                 writesl(ss->base + SS_KEY0 + i * 4, &op->key[i], 1);
263
264         if (areq->iv) {
265                 for (i = 0; i < 4 && i < ivsize / 4; i++) {
266                         v = *(u32 *)(areq->iv + i * 4);
267                         writesl(ss->base + SS_IV0 + i * 4, &v, 1);
268                 }
269         }
270         writel(mode, ss->base + SS_CTL);
271
272         ileft = areq->cryptlen;
273         oleft = areq->cryptlen;
274         oi = 0;
275         oo = 0;
276
277         while (oleft) {
278                 if (ileft) {
279                         sg_miter_start(&mi, areq->src, sg_nents(areq->src),
280                                        SG_MITER_FROM_SG | SG_MITER_ATOMIC);
281                         if (pi)
282                                 sg_miter_skip(&mi, pi);
283                         miter_err = sg_miter_next(&mi);
284                         if (!miter_err || !mi.addr) {
285                                 dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
286                                 err = -EINVAL;
287                                 goto release_ss;
288                         }
289                         /*
290                          * todo is the number of consecutive 4byte word that we
291                          * can read from current SG
292                          */
293                         todo = min(rx_cnt, ileft / 4);
294                         todo = min_t(size_t, todo, (mi.length - oi) / 4);
295                         if (todo && !ob) {
296                                 writesl(ss->base + SS_RXFIFO, mi.addr + oi,
297                                         todo);
298                                 ileft -= todo * 4;
299                                 oi += todo * 4;
300                         } else {
301                                 /*
302                                  * not enough consecutive bytes, so we need to
303                                  * linearize in buf. todo is in bytes
304                                  * After that copy, if we have a multiple of 4
305                                  * we need to be able to write all buf in one
306                                  * pass, so it is why we min() with rx_cnt
307                                  */
308                                 todo = min(rx_cnt * 4 - ob, ileft);
309                                 todo = min_t(size_t, todo, mi.length - oi);
310                                 memcpy(ss->buf + ob, mi.addr + oi, todo);
311                                 ileft -= todo;
312                                 oi += todo;
313                                 ob += todo;
314                                 if (!(ob % 4)) {
315                                         writesl(ss->base + SS_RXFIFO, ss->buf,
316                                                 ob / 4);
317                                         ob = 0;
318                                 }
319                         }
320                         if (oi == mi.length) {
321                                 pi += mi.length;
322                                 oi = 0;
323                         }
324                         sg_miter_stop(&mi);
325                 }
326
327                 spaces = readl(ss->base + SS_FCSR);
328                 rx_cnt = SS_RXFIFO_SPACES(spaces);
329                 tx_cnt = SS_TXFIFO_SPACES(spaces);
330
331                 if (!tx_cnt)
332                         continue;
333                 sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
334                                SG_MITER_TO_SG | SG_MITER_ATOMIC);
335                 if (po)
336                         sg_miter_skip(&mo, po);
337                 miter_err = sg_miter_next(&mo);
338                 if (!miter_err || !mo.addr) {
339                         dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
340                         err = -EINVAL;
341                         goto release_ss;
342                 }
343                 /* todo in 4bytes word */
344                 todo = min(tx_cnt, oleft / 4);
345                 todo = min_t(size_t, todo, (mo.length - oo) / 4);
346
347                 if (todo) {
348                         readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);
349                         oleft -= todo * 4;
350                         oo += todo * 4;
351                         if (oo == mo.length) {
352                                 po += mo.length;
353                                 oo = 0;
354                         }
355                 } else {
356                         /*
357                          * read obl bytes in bufo, we read at maximum for
358                          * emptying the device
359                          */
360                         readsl(ss->base + SS_TXFIFO, ss->bufo, tx_cnt);
361                         obl = tx_cnt * 4;
362                         obo = 0;
363                         do {
364                                 /*
365                                  * how many bytes we can copy ?
366                                  * no more than remaining SG size
367                                  * no more than remaining buffer
368                                  * no need to test against oleft
369                                  */
370                                 todo = min_t(size_t,
371                                              mo.length - oo, obl - obo);
372                                 memcpy(mo.addr + oo, ss->bufo + obo, todo);
373                                 oleft -= todo;
374                                 obo += todo;
375                                 oo += todo;
376                                 if (oo == mo.length) {
377                                         po += mo.length;
378                                         sg_miter_next(&mo);
379                                         oo = 0;
380                                 }
381                         } while (obo < obl);
382                         /* bufo must be fully used here */
383                 }
384                 sg_miter_stop(&mo);
385         }
386         if (areq->iv) {
387                 if (mode & SS_DECRYPTION) {
388                         memcpy(areq->iv, backup_iv, ivsize);
389                         kfree_sensitive(backup_iv);
390                 } else {
391                         scatterwalk_map_and_copy(areq->iv, areq->dst, areq->cryptlen - ivsize,
392                                                  ivsize, 0);
393                 }
394         }
395
396 release_ss:
397         writel(0, ss->base + SS_CTL);
398         spin_unlock_irqrestore(&ss->slock, flags);
399
400         return err;
401 }
402
403 /* CBC AES */
404 int sun4i_ss_cbc_aes_encrypt(struct skcipher_request *areq)
405 {
406         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
407         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
408         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
409
410         rctx->mode = SS_OP_AES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
411                 op->keymode;
412         return sun4i_ss_cipher_poll(areq);
413 }
414
415 int sun4i_ss_cbc_aes_decrypt(struct skcipher_request *areq)
416 {
417         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
418         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
419         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
420
421         rctx->mode = SS_OP_AES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
422                 op->keymode;
423         return sun4i_ss_cipher_poll(areq);
424 }
425
426 /* ECB AES */
427 int sun4i_ss_ecb_aes_encrypt(struct skcipher_request *areq)
428 {
429         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
430         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
431         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
432
433         rctx->mode = SS_OP_AES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
434                 op->keymode;
435         return sun4i_ss_cipher_poll(areq);
436 }
437
438 int sun4i_ss_ecb_aes_decrypt(struct skcipher_request *areq)
439 {
440         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
441         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
442         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
443
444         rctx->mode = SS_OP_AES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
445                 op->keymode;
446         return sun4i_ss_cipher_poll(areq);
447 }
448
449 /* CBC DES */
450 int sun4i_ss_cbc_des_encrypt(struct skcipher_request *areq)
451 {
452         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
453         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
454         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
455
456         rctx->mode = SS_OP_DES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
457                 op->keymode;
458         return sun4i_ss_cipher_poll(areq);
459 }
460
461 int sun4i_ss_cbc_des_decrypt(struct skcipher_request *areq)
462 {
463         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
464         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
465         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
466
467         rctx->mode = SS_OP_DES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
468                 op->keymode;
469         return sun4i_ss_cipher_poll(areq);
470 }
471
472 /* ECB DES */
473 int sun4i_ss_ecb_des_encrypt(struct skcipher_request *areq)
474 {
475         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
476         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
477         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
478
479         rctx->mode = SS_OP_DES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
480                 op->keymode;
481         return sun4i_ss_cipher_poll(areq);
482 }
483
484 int sun4i_ss_ecb_des_decrypt(struct skcipher_request *areq)
485 {
486         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
487         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
488         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
489
490         rctx->mode = SS_OP_DES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
491                 op->keymode;
492         return sun4i_ss_cipher_poll(areq);
493 }
494
495 /* CBC 3DES */
496 int sun4i_ss_cbc_des3_encrypt(struct skcipher_request *areq)
497 {
498         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
499         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
500         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
501
502         rctx->mode = SS_OP_3DES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
503                 op->keymode;
504         return sun4i_ss_cipher_poll(areq);
505 }
506
507 int sun4i_ss_cbc_des3_decrypt(struct skcipher_request *areq)
508 {
509         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
510         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
511         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
512
513         rctx->mode = SS_OP_3DES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
514                 op->keymode;
515         return sun4i_ss_cipher_poll(areq);
516 }
517
518 /* ECB 3DES */
519 int sun4i_ss_ecb_des3_encrypt(struct skcipher_request *areq)
520 {
521         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
522         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
523         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
524
525         rctx->mode = SS_OP_3DES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
526                 op->keymode;
527         return sun4i_ss_cipher_poll(areq);
528 }
529
530 int sun4i_ss_ecb_des3_decrypt(struct skcipher_request *areq)
531 {
532         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
533         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
534         struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
535
536         rctx->mode = SS_OP_3DES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
537                 op->keymode;
538         return sun4i_ss_cipher_poll(areq);
539 }
540
541 int sun4i_ss_cipher_init(struct crypto_tfm *tfm)
542 {
543         struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
544         struct sun4i_ss_alg_template *algt;
545         const char *name = crypto_tfm_alg_name(tfm);
546         int err;
547
548         memset(op, 0, sizeof(struct sun4i_tfm_ctx));
549
550         algt = container_of(tfm->__crt_alg, struct sun4i_ss_alg_template,
551                             alg.crypto.base);
552         op->ss = algt->ss;
553
554         op->fallback_tfm = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
555         if (IS_ERR(op->fallback_tfm)) {
556                 dev_err(op->ss->dev, "ERROR: Cannot allocate fallback for %s %ld\n",
557                         name, PTR_ERR(op->fallback_tfm));
558                 return PTR_ERR(op->fallback_tfm);
559         }
560
561         crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
562                                     sizeof(struct sun4i_cipher_req_ctx) +
563                                     crypto_skcipher_reqsize(op->fallback_tfm));
564
565
566         err = pm_runtime_get_sync(op->ss->dev);
567         if (err < 0)
568                 goto error_pm;
569
570         return 0;
571 error_pm:
572         crypto_free_skcipher(op->fallback_tfm);
573         return err;
574 }
575
576 void sun4i_ss_cipher_exit(struct crypto_tfm *tfm)
577 {
578         struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
579
580         crypto_free_skcipher(op->fallback_tfm);
581         pm_runtime_put(op->ss->dev);
582 }
583
584 /* check and set the AES key, prepare the mode to be used */
585 int sun4i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
586                         unsigned int keylen)
587 {
588         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
589         struct sun4i_ss_ctx *ss = op->ss;
590
591         switch (keylen) {
592         case 128 / 8:
593                 op->keymode = SS_AES_128BITS;
594                 break;
595         case 192 / 8:
596                 op->keymode = SS_AES_192BITS;
597                 break;
598         case 256 / 8:
599                 op->keymode = SS_AES_256BITS;
600                 break;
601         default:
602                 dev_dbg(ss->dev, "ERROR: Invalid keylen %u\n", keylen);
603                 return -EINVAL;
604         }
605         op->keylen = keylen;
606         memcpy(op->key, key, keylen);
607
608         crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
609         crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
610
611         return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
612 }
613
614 /* check and set the DES key, prepare the mode to be used */
615 int sun4i_ss_des_setkey(struct crypto_skcipher *tfm, const u8 *key,
616                         unsigned int keylen)
617 {
618         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
619         int err;
620
621         err = verify_skcipher_des_key(tfm, key);
622         if (err)
623                 return err;
624
625         op->keylen = keylen;
626         memcpy(op->key, key, keylen);
627
628         crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
629         crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
630
631         return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
632 }
633
634 /* check and set the 3DES key, prepare the mode to be used */
635 int sun4i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
636                          unsigned int keylen)
637 {
638         struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
639         int err;
640
641         err = verify_skcipher_des3_key(tfm, key);
642         if (err)
643                 return err;
644
645         op->keylen = keylen;
646         memcpy(op->key, key, keylen);
647
648         crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
649         crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
650
651         return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
652
653 }