117513547SCorentin Labbe // SPDX-License-Identifier: GPL-2.0-or-later
217513547SCorentin Labbe /*
317513547SCorentin Labbe * sun4i-ss-cipher.c - hardware cryptographic accelerator for Allwinner A20 SoC
417513547SCorentin Labbe *
517513547SCorentin Labbe * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com>
617513547SCorentin Labbe *
717513547SCorentin Labbe * This file add support for AES cipher with 128,192,256 bits
817513547SCorentin Labbe * keysize in CBC and ECB mode.
917513547SCorentin Labbe * Add support also for DES and 3DES in CBC and ECB mode.
1017513547SCorentin Labbe *
11*39db3f15SJonathan Corbet * You could find the datasheet in Documentation/arch/arm/sunxi.rst
1217513547SCorentin Labbe */
1317513547SCorentin Labbe #include "sun4i-ss.h"
1417513547SCorentin Labbe
sun4i_ss_opti_poll(struct skcipher_request * areq)1517513547SCorentin Labbe static int noinline_for_stack sun4i_ss_opti_poll(struct skcipher_request *areq)
1617513547SCorentin Labbe {
1717513547SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
1817513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
1917513547SCorentin Labbe struct sun4i_ss_ctx *ss = op->ss;
2017513547SCorentin Labbe unsigned int ivsize = crypto_skcipher_ivsize(tfm);
2117513547SCorentin Labbe struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
2217513547SCorentin Labbe u32 mode = ctx->mode;
2317513547SCorentin Labbe /* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
2417513547SCorentin Labbe u32 rx_cnt = SS_RX_DEFAULT;
2517513547SCorentin Labbe u32 tx_cnt = 0;
2617513547SCorentin Labbe u32 spaces;
2717513547SCorentin Labbe u32 v;
2817513547SCorentin Labbe int err = 0;
2917513547SCorentin Labbe unsigned int i;
3017513547SCorentin Labbe unsigned int ileft = areq->cryptlen;
3117513547SCorentin Labbe unsigned int oleft = areq->cryptlen;
3217513547SCorentin Labbe unsigned int todo;
339bc3dd24SCorentin Labbe unsigned long pi = 0, po = 0; /* progress for in and out */
349bc3dd24SCorentin Labbe bool miter_err;
3517513547SCorentin Labbe struct sg_mapping_iter mi, mo;
3617513547SCorentin Labbe unsigned int oi, oo; /* offset for in and out */
3717513547SCorentin Labbe unsigned long flags;
38b1f578b8SCorentin Labbe struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
39b1f578b8SCorentin Labbe struct sun4i_ss_alg_template *algt;
4017513547SCorentin Labbe
4117513547SCorentin Labbe if (!areq->cryptlen)
4217513547SCorentin Labbe return 0;
4317513547SCorentin Labbe
4417513547SCorentin Labbe if (!areq->src || !areq->dst) {
4517513547SCorentin Labbe dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");
4617513547SCorentin Labbe return -EINVAL;
4717513547SCorentin Labbe }
4817513547SCorentin Labbe
49b756f1c8SCorentin Labbe if (areq->iv && ivsize > 0 && mode & SS_DECRYPTION) {
5022d03a0aSCorentin Labbe scatterwalk_map_and_copy(ctx->backup_iv, areq->src,
5122d03a0aSCorentin Labbe areq->cryptlen - ivsize, ivsize, 0);
52b756f1c8SCorentin Labbe }
53b756f1c8SCorentin Labbe
54b1f578b8SCorentin Labbe if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG)) {
55b1f578b8SCorentin Labbe algt = container_of(alg, struct sun4i_ss_alg_template, alg.crypto);
56b1f578b8SCorentin Labbe algt->stat_opti++;
57b1f578b8SCorentin Labbe algt->stat_bytes += areq->cryptlen;
58b1f578b8SCorentin Labbe }
59b1f578b8SCorentin Labbe
6017513547SCorentin Labbe spin_lock_irqsave(&ss->slock, flags);
6117513547SCorentin Labbe
625ab6177fSCorentin Labbe for (i = 0; i < op->keylen / 4; i++)
635ab6177fSCorentin Labbe writesl(ss->base + SS_KEY0 + i * 4, &op->key[i], 1);
6417513547SCorentin Labbe
6517513547SCorentin Labbe if (areq->iv) {
6617513547SCorentin Labbe for (i = 0; i < 4 && i < ivsize / 4; i++) {
6717513547SCorentin Labbe v = *(u32 *)(areq->iv + i * 4);
685ab6177fSCorentin Labbe writesl(ss->base + SS_IV0 + i * 4, &v, 1);
6917513547SCorentin Labbe }
7017513547SCorentin Labbe }
7117513547SCorentin Labbe writel(mode, ss->base + SS_CTL);
7217513547SCorentin Labbe
7317513547SCorentin Labbe
7417513547SCorentin Labbe ileft = areq->cryptlen / 4;
7517513547SCorentin Labbe oleft = areq->cryptlen / 4;
7617513547SCorentin Labbe oi = 0;
7717513547SCorentin Labbe oo = 0;
7817513547SCorentin Labbe do {
799bc3dd24SCorentin Labbe if (ileft) {
809bc3dd24SCorentin Labbe sg_miter_start(&mi, areq->src, sg_nents(areq->src),
819bc3dd24SCorentin Labbe SG_MITER_FROM_SG | SG_MITER_ATOMIC);
829bc3dd24SCorentin Labbe if (pi)
839bc3dd24SCorentin Labbe sg_miter_skip(&mi, pi);
849bc3dd24SCorentin Labbe miter_err = sg_miter_next(&mi);
859bc3dd24SCorentin Labbe if (!miter_err || !mi.addr) {
869bc3dd24SCorentin Labbe dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
879bc3dd24SCorentin Labbe err = -EINVAL;
889bc3dd24SCorentin Labbe goto release_ss;
899bc3dd24SCorentin Labbe }
90d6e9da21SHerbert Xu todo = min(rx_cnt, ileft);
91d6e9da21SHerbert Xu todo = min_t(size_t, todo, (mi.length - oi) / 4);
9217513547SCorentin Labbe if (todo) {
9317513547SCorentin Labbe ileft -= todo;
9417513547SCorentin Labbe writesl(ss->base + SS_RXFIFO, mi.addr + oi, todo);
9517513547SCorentin Labbe oi += todo * 4;
9617513547SCorentin Labbe }
9717513547SCorentin Labbe if (oi == mi.length) {
989bc3dd24SCorentin Labbe pi += mi.length;
9917513547SCorentin Labbe oi = 0;
10017513547SCorentin Labbe }
1019bc3dd24SCorentin Labbe sg_miter_stop(&mi);
1029bc3dd24SCorentin Labbe }
10317513547SCorentin Labbe
10417513547SCorentin Labbe spaces = readl(ss->base + SS_FCSR);
10517513547SCorentin Labbe rx_cnt = SS_RXFIFO_SPACES(spaces);
10617513547SCorentin Labbe tx_cnt = SS_TXFIFO_SPACES(spaces);
10717513547SCorentin Labbe
1089bc3dd24SCorentin Labbe sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
1099bc3dd24SCorentin Labbe SG_MITER_TO_SG | SG_MITER_ATOMIC);
1109bc3dd24SCorentin Labbe if (po)
1119bc3dd24SCorentin Labbe sg_miter_skip(&mo, po);
1129bc3dd24SCorentin Labbe miter_err = sg_miter_next(&mo);
1139bc3dd24SCorentin Labbe if (!miter_err || !mo.addr) {
1149bc3dd24SCorentin Labbe dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
1159bc3dd24SCorentin Labbe err = -EINVAL;
1169bc3dd24SCorentin Labbe goto release_ss;
1179bc3dd24SCorentin Labbe }
118d6e9da21SHerbert Xu todo = min(tx_cnt, oleft);
119d6e9da21SHerbert Xu todo = min_t(size_t, todo, (mo.length - oo) / 4);
12017513547SCorentin Labbe if (todo) {
12117513547SCorentin Labbe oleft -= todo;
12217513547SCorentin Labbe readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);
12317513547SCorentin Labbe oo += todo * 4;
12417513547SCorentin Labbe }
12517513547SCorentin Labbe if (oo == mo.length) {
12617513547SCorentin Labbe oo = 0;
1279bc3dd24SCorentin Labbe po += mo.length;
12817513547SCorentin Labbe }
1299bc3dd24SCorentin Labbe sg_miter_stop(&mo);
13017513547SCorentin Labbe } while (oleft);
13117513547SCorentin Labbe
13217513547SCorentin Labbe if (areq->iv) {
133b756f1c8SCorentin Labbe if (mode & SS_DECRYPTION) {
13422d03a0aSCorentin Labbe memcpy(areq->iv, ctx->backup_iv, ivsize);
13522d03a0aSCorentin Labbe memzero_explicit(ctx->backup_iv, ivsize);
136b756f1c8SCorentin Labbe } else {
137b756f1c8SCorentin Labbe scatterwalk_map_and_copy(areq->iv, areq->dst, areq->cryptlen - ivsize,
138b756f1c8SCorentin Labbe ivsize, 0);
13917513547SCorentin Labbe }
14017513547SCorentin Labbe }
14117513547SCorentin Labbe
14217513547SCorentin Labbe release_ss:
14317513547SCorentin Labbe writel(0, ss->base + SS_CTL);
14417513547SCorentin Labbe spin_unlock_irqrestore(&ss->slock, flags);
14517513547SCorentin Labbe return err;
14617513547SCorentin Labbe }
14717513547SCorentin Labbe
sun4i_ss_cipher_poll_fallback(struct skcipher_request * areq)14817513547SCorentin Labbe static int noinline_for_stack sun4i_ss_cipher_poll_fallback(struct skcipher_request *areq)
14917513547SCorentin Labbe {
15017513547SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
15117513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
15217513547SCorentin Labbe struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
15317513547SCorentin Labbe int err;
154b1f578b8SCorentin Labbe struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
155b1f578b8SCorentin Labbe struct sun4i_ss_alg_template *algt;
156b1f578b8SCorentin Labbe
157b1f578b8SCorentin Labbe if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG)) {
158b1f578b8SCorentin Labbe algt = container_of(alg, struct sun4i_ss_alg_template, alg.crypto);
159b1f578b8SCorentin Labbe algt->stat_fb++;
160b1f578b8SCorentin Labbe }
16117513547SCorentin Labbe
16289fb00f2SArd Biesheuvel skcipher_request_set_tfm(&ctx->fallback_req, op->fallback_tfm);
16389fb00f2SArd Biesheuvel skcipher_request_set_callback(&ctx->fallback_req, areq->base.flags,
16489fb00f2SArd Biesheuvel areq->base.complete, areq->base.data);
16589fb00f2SArd Biesheuvel skcipher_request_set_crypt(&ctx->fallback_req, areq->src, areq->dst,
16617513547SCorentin Labbe areq->cryptlen, areq->iv);
16717513547SCorentin Labbe if (ctx->mode & SS_DECRYPTION)
16889fb00f2SArd Biesheuvel err = crypto_skcipher_decrypt(&ctx->fallback_req);
16917513547SCorentin Labbe else
17089fb00f2SArd Biesheuvel err = crypto_skcipher_encrypt(&ctx->fallback_req);
17117513547SCorentin Labbe
17217513547SCorentin Labbe return err;
17317513547SCorentin Labbe }
17417513547SCorentin Labbe
17517513547SCorentin Labbe /* Generic function that support SG with size not multiple of 4 */
sun4i_ss_cipher_poll(struct skcipher_request * areq)17617513547SCorentin Labbe static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
17717513547SCorentin Labbe {
17817513547SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
17917513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
18017513547SCorentin Labbe struct sun4i_ss_ctx *ss = op->ss;
18117513547SCorentin Labbe int no_chunk = 1;
18217513547SCorentin Labbe struct scatterlist *in_sg = areq->src;
18317513547SCorentin Labbe struct scatterlist *out_sg = areq->dst;
18417513547SCorentin Labbe unsigned int ivsize = crypto_skcipher_ivsize(tfm);
18517513547SCorentin Labbe struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
18617513547SCorentin Labbe struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
18717513547SCorentin Labbe struct sun4i_ss_alg_template *algt;
18817513547SCorentin Labbe u32 mode = ctx->mode;
18917513547SCorentin Labbe /* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
19017513547SCorentin Labbe u32 rx_cnt = SS_RX_DEFAULT;
19117513547SCorentin Labbe u32 tx_cnt = 0;
19217513547SCorentin Labbe u32 v;
19317513547SCorentin Labbe u32 spaces;
19417513547SCorentin Labbe int err = 0;
19517513547SCorentin Labbe unsigned int i;
19617513547SCorentin Labbe unsigned int ileft = areq->cryptlen;
19717513547SCorentin Labbe unsigned int oleft = areq->cryptlen;
19817513547SCorentin Labbe unsigned int todo;
19917513547SCorentin Labbe struct sg_mapping_iter mi, mo;
2009bc3dd24SCorentin Labbe unsigned long pi = 0, po = 0; /* progress for in and out */
2019bc3dd24SCorentin Labbe bool miter_err;
20217513547SCorentin Labbe unsigned int oi, oo; /* offset for in and out */
20317513547SCorentin Labbe unsigned int ob = 0; /* offset in buf */
20417513547SCorentin Labbe unsigned int obo = 0; /* offset in bufo*/
20517513547SCorentin Labbe unsigned int obl = 0; /* length of data in bufo */
20617513547SCorentin Labbe unsigned long flags;
2074ec8977bSCorentin Labbe bool need_fallback = false;
20817513547SCorentin Labbe
20917513547SCorentin Labbe if (!areq->cryptlen)
21017513547SCorentin Labbe return 0;
21117513547SCorentin Labbe
21217513547SCorentin Labbe if (!areq->src || !areq->dst) {
21317513547SCorentin Labbe dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");
21417513547SCorentin Labbe return -EINVAL;
21517513547SCorentin Labbe }
21617513547SCorentin Labbe
21717513547SCorentin Labbe algt = container_of(alg, struct sun4i_ss_alg_template, alg.crypto);
21817513547SCorentin Labbe if (areq->cryptlen % algt->alg.crypto.base.cra_blocksize)
21917513547SCorentin Labbe need_fallback = true;
22017513547SCorentin Labbe
22117513547SCorentin Labbe /*
22217513547SCorentin Labbe * if we have only SGs with size multiple of 4,
22317513547SCorentin Labbe * we can use the SS optimized function
22417513547SCorentin Labbe */
22517513547SCorentin Labbe while (in_sg && no_chunk == 1) {
2267bdcd851SCorentin Labbe if ((in_sg->length | in_sg->offset) & 3u)
22717513547SCorentin Labbe no_chunk = 0;
22817513547SCorentin Labbe in_sg = sg_next(in_sg);
22917513547SCorentin Labbe }
23017513547SCorentin Labbe while (out_sg && no_chunk == 1) {
2317bdcd851SCorentin Labbe if ((out_sg->length | out_sg->offset) & 3u)
23217513547SCorentin Labbe no_chunk = 0;
23317513547SCorentin Labbe out_sg = sg_next(out_sg);
23417513547SCorentin Labbe }
23517513547SCorentin Labbe
23617513547SCorentin Labbe if (no_chunk == 1 && !need_fallback)
23717513547SCorentin Labbe return sun4i_ss_opti_poll(areq);
23817513547SCorentin Labbe
23917513547SCorentin Labbe if (need_fallback)
24017513547SCorentin Labbe return sun4i_ss_cipher_poll_fallback(areq);
24117513547SCorentin Labbe
242b756f1c8SCorentin Labbe if (areq->iv && ivsize > 0 && mode & SS_DECRYPTION) {
24322d03a0aSCorentin Labbe scatterwalk_map_and_copy(ctx->backup_iv, areq->src,
24422d03a0aSCorentin Labbe areq->cryptlen - ivsize, ivsize, 0);
245b756f1c8SCorentin Labbe }
246b756f1c8SCorentin Labbe
247b1f578b8SCorentin Labbe if (IS_ENABLED(CONFIG_CRYPTO_DEV_SUN4I_SS_DEBUG)) {
248b1f578b8SCorentin Labbe algt->stat_req++;
249b1f578b8SCorentin Labbe algt->stat_bytes += areq->cryptlen;
250b1f578b8SCorentin Labbe }
251b1f578b8SCorentin Labbe
25217513547SCorentin Labbe spin_lock_irqsave(&ss->slock, flags);
25317513547SCorentin Labbe
2545ab6177fSCorentin Labbe for (i = 0; i < op->keylen / 4; i++)
2555ab6177fSCorentin Labbe writesl(ss->base + SS_KEY0 + i * 4, &op->key[i], 1);
25617513547SCorentin Labbe
25717513547SCorentin Labbe if (areq->iv) {
25817513547SCorentin Labbe for (i = 0; i < 4 && i < ivsize / 4; i++) {
25917513547SCorentin Labbe v = *(u32 *)(areq->iv + i * 4);
2605ab6177fSCorentin Labbe writesl(ss->base + SS_IV0 + i * 4, &v, 1);
26117513547SCorentin Labbe }
26217513547SCorentin Labbe }
26317513547SCorentin Labbe writel(mode, ss->base + SS_CTL);
26417513547SCorentin Labbe
26517513547SCorentin Labbe ileft = areq->cryptlen;
26617513547SCorentin Labbe oleft = areq->cryptlen;
26717513547SCorentin Labbe oi = 0;
26817513547SCorentin Labbe oo = 0;
26917513547SCorentin Labbe
27017513547SCorentin Labbe while (oleft) {
27117513547SCorentin Labbe if (ileft) {
2729bc3dd24SCorentin Labbe sg_miter_start(&mi, areq->src, sg_nents(areq->src),
2739bc3dd24SCorentin Labbe SG_MITER_FROM_SG | SG_MITER_ATOMIC);
2749bc3dd24SCorentin Labbe if (pi)
2759bc3dd24SCorentin Labbe sg_miter_skip(&mi, pi);
2769bc3dd24SCorentin Labbe miter_err = sg_miter_next(&mi);
2779bc3dd24SCorentin Labbe if (!miter_err || !mi.addr) {
2789bc3dd24SCorentin Labbe dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
2799bc3dd24SCorentin Labbe err = -EINVAL;
2809bc3dd24SCorentin Labbe goto release_ss;
2819bc3dd24SCorentin Labbe }
28217513547SCorentin Labbe /*
28317513547SCorentin Labbe * todo is the number of consecutive 4byte word that we
28417513547SCorentin Labbe * can read from current SG
28517513547SCorentin Labbe */
286d6e9da21SHerbert Xu todo = min(rx_cnt, ileft / 4);
287d6e9da21SHerbert Xu todo = min_t(size_t, todo, (mi.length - oi) / 4);
28817513547SCorentin Labbe if (todo && !ob) {
28917513547SCorentin Labbe writesl(ss->base + SS_RXFIFO, mi.addr + oi,
29017513547SCorentin Labbe todo);
29117513547SCorentin Labbe ileft -= todo * 4;
29217513547SCorentin Labbe oi += todo * 4;
29317513547SCorentin Labbe } else {
29417513547SCorentin Labbe /*
29517513547SCorentin Labbe * not enough consecutive bytes, so we need to
29617513547SCorentin Labbe * linearize in buf. todo is in bytes
29717513547SCorentin Labbe * After that copy, if we have a multiple of 4
29817513547SCorentin Labbe * we need to be able to write all buf in one
29917513547SCorentin Labbe * pass, so it is why we min() with rx_cnt
30017513547SCorentin Labbe */
301d6e9da21SHerbert Xu todo = min(rx_cnt * 4 - ob, ileft);
302d6e9da21SHerbert Xu todo = min_t(size_t, todo, mi.length - oi);
30358351351SCorentin Labbe memcpy(ss->buf + ob, mi.addr + oi, todo);
30417513547SCorentin Labbe ileft -= todo;
30517513547SCorentin Labbe oi += todo;
30617513547SCorentin Labbe ob += todo;
30717513547SCorentin Labbe if (!(ob % 4)) {
30858351351SCorentin Labbe writesl(ss->base + SS_RXFIFO, ss->buf,
30917513547SCorentin Labbe ob / 4);
31017513547SCorentin Labbe ob = 0;
31117513547SCorentin Labbe }
31217513547SCorentin Labbe }
31317513547SCorentin Labbe if (oi == mi.length) {
3149bc3dd24SCorentin Labbe pi += mi.length;
31517513547SCorentin Labbe oi = 0;
31617513547SCorentin Labbe }
3179bc3dd24SCorentin Labbe sg_miter_stop(&mi);
31817513547SCorentin Labbe }
31917513547SCorentin Labbe
32017513547SCorentin Labbe spaces = readl(ss->base + SS_FCSR);
32117513547SCorentin Labbe rx_cnt = SS_RXFIFO_SPACES(spaces);
32217513547SCorentin Labbe tx_cnt = SS_TXFIFO_SPACES(spaces);
32317513547SCorentin Labbe
32417513547SCorentin Labbe if (!tx_cnt)
32517513547SCorentin Labbe continue;
3269bc3dd24SCorentin Labbe sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
3279bc3dd24SCorentin Labbe SG_MITER_TO_SG | SG_MITER_ATOMIC);
3289bc3dd24SCorentin Labbe if (po)
3299bc3dd24SCorentin Labbe sg_miter_skip(&mo, po);
3309bc3dd24SCorentin Labbe miter_err = sg_miter_next(&mo);
3319bc3dd24SCorentin Labbe if (!miter_err || !mo.addr) {
3329bc3dd24SCorentin Labbe dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
3339bc3dd24SCorentin Labbe err = -EINVAL;
3349bc3dd24SCorentin Labbe goto release_ss;
3359bc3dd24SCorentin Labbe }
33617513547SCorentin Labbe /* todo in 4bytes word */
337d6e9da21SHerbert Xu todo = min(tx_cnt, oleft / 4);
338d6e9da21SHerbert Xu todo = min_t(size_t, todo, (mo.length - oo) / 4);
3399bc3dd24SCorentin Labbe
34017513547SCorentin Labbe if (todo) {
34117513547SCorentin Labbe readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);
34217513547SCorentin Labbe oleft -= todo * 4;
34317513547SCorentin Labbe oo += todo * 4;
34417513547SCorentin Labbe if (oo == mo.length) {
3459bc3dd24SCorentin Labbe po += mo.length;
34617513547SCorentin Labbe oo = 0;
34717513547SCorentin Labbe }
34817513547SCorentin Labbe } else {
34917513547SCorentin Labbe /*
35017513547SCorentin Labbe * read obl bytes in bufo, we read at maximum for
35117513547SCorentin Labbe * emptying the device
35217513547SCorentin Labbe */
35358351351SCorentin Labbe readsl(ss->base + SS_TXFIFO, ss->bufo, tx_cnt);
35417513547SCorentin Labbe obl = tx_cnt * 4;
35517513547SCorentin Labbe obo = 0;
35617513547SCorentin Labbe do {
35717513547SCorentin Labbe /*
35817513547SCorentin Labbe * how many bytes we can copy ?
35917513547SCorentin Labbe * no more than remaining SG size
36017513547SCorentin Labbe * no more than remaining buffer
36117513547SCorentin Labbe * no need to test against oleft
36217513547SCorentin Labbe */
363d6e9da21SHerbert Xu todo = min_t(size_t,
364d6e9da21SHerbert Xu mo.length - oo, obl - obo);
36558351351SCorentin Labbe memcpy(mo.addr + oo, ss->bufo + obo, todo);
36617513547SCorentin Labbe oleft -= todo;
36717513547SCorentin Labbe obo += todo;
36817513547SCorentin Labbe oo += todo;
36917513547SCorentin Labbe if (oo == mo.length) {
3709bc3dd24SCorentin Labbe po += mo.length;
37117513547SCorentin Labbe sg_miter_next(&mo);
37217513547SCorentin Labbe oo = 0;
37317513547SCorentin Labbe }
37417513547SCorentin Labbe } while (obo < obl);
37517513547SCorentin Labbe /* bufo must be fully used here */
37617513547SCorentin Labbe }
3779bc3dd24SCorentin Labbe sg_miter_stop(&mo);
37817513547SCorentin Labbe }
37917513547SCorentin Labbe if (areq->iv) {
380b756f1c8SCorentin Labbe if (mode & SS_DECRYPTION) {
38122d03a0aSCorentin Labbe memcpy(areq->iv, ctx->backup_iv, ivsize);
38222d03a0aSCorentin Labbe memzero_explicit(ctx->backup_iv, ivsize);
383b756f1c8SCorentin Labbe } else {
384b756f1c8SCorentin Labbe scatterwalk_map_and_copy(areq->iv, areq->dst, areq->cryptlen - ivsize,
385b756f1c8SCorentin Labbe ivsize, 0);
38617513547SCorentin Labbe }
38717513547SCorentin Labbe }
38817513547SCorentin Labbe
38917513547SCorentin Labbe release_ss:
39017513547SCorentin Labbe writel(0, ss->base + SS_CTL);
39117513547SCorentin Labbe spin_unlock_irqrestore(&ss->slock, flags);
39217513547SCorentin Labbe
39317513547SCorentin Labbe return err;
39417513547SCorentin Labbe }
39517513547SCorentin Labbe
39617513547SCorentin Labbe /* CBC AES */
sun4i_ss_cbc_aes_encrypt(struct skcipher_request * areq)39717513547SCorentin Labbe int sun4i_ss_cbc_aes_encrypt(struct skcipher_request *areq)
39817513547SCorentin Labbe {
39917513547SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
40017513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
40117513547SCorentin Labbe struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
40217513547SCorentin Labbe
40317513547SCorentin Labbe rctx->mode = SS_OP_AES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
40417513547SCorentin Labbe op->keymode;
40517513547SCorentin Labbe return sun4i_ss_cipher_poll(areq);
40617513547SCorentin Labbe }
40717513547SCorentin Labbe
sun4i_ss_cbc_aes_decrypt(struct skcipher_request * areq)40817513547SCorentin Labbe int sun4i_ss_cbc_aes_decrypt(struct skcipher_request *areq)
40917513547SCorentin Labbe {
41017513547SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
41117513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
41217513547SCorentin Labbe struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
41317513547SCorentin Labbe
41417513547SCorentin Labbe rctx->mode = SS_OP_AES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
41517513547SCorentin Labbe op->keymode;
41617513547SCorentin Labbe return sun4i_ss_cipher_poll(areq);
41717513547SCorentin Labbe }
41817513547SCorentin Labbe
41917513547SCorentin Labbe /* ECB AES */
sun4i_ss_ecb_aes_encrypt(struct skcipher_request * areq)42017513547SCorentin Labbe int sun4i_ss_ecb_aes_encrypt(struct skcipher_request *areq)
42117513547SCorentin Labbe {
42217513547SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
42317513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
42417513547SCorentin Labbe struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
42517513547SCorentin Labbe
42617513547SCorentin Labbe rctx->mode = SS_OP_AES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
42717513547SCorentin Labbe op->keymode;
42817513547SCorentin Labbe return sun4i_ss_cipher_poll(areq);
42917513547SCorentin Labbe }
43017513547SCorentin Labbe
sun4i_ss_ecb_aes_decrypt(struct skcipher_request * areq)43117513547SCorentin Labbe int sun4i_ss_ecb_aes_decrypt(struct skcipher_request *areq)
43217513547SCorentin Labbe {
43317513547SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
43417513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
43517513547SCorentin Labbe struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
43617513547SCorentin Labbe
43717513547SCorentin Labbe rctx->mode = SS_OP_AES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
43817513547SCorentin Labbe op->keymode;
43917513547SCorentin Labbe return sun4i_ss_cipher_poll(areq);
44017513547SCorentin Labbe }
44117513547SCorentin Labbe
44217513547SCorentin Labbe /* CBC DES */
sun4i_ss_cbc_des_encrypt(struct skcipher_request * areq)44317513547SCorentin Labbe int sun4i_ss_cbc_des_encrypt(struct skcipher_request *areq)
44417513547SCorentin Labbe {
44517513547SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
44617513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
44717513547SCorentin Labbe struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
44817513547SCorentin Labbe
44917513547SCorentin Labbe rctx->mode = SS_OP_DES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
45017513547SCorentin Labbe op->keymode;
45117513547SCorentin Labbe return sun4i_ss_cipher_poll(areq);
45217513547SCorentin Labbe }
45317513547SCorentin Labbe
sun4i_ss_cbc_des_decrypt(struct skcipher_request * areq)45417513547SCorentin Labbe int sun4i_ss_cbc_des_decrypt(struct skcipher_request *areq)
45517513547SCorentin Labbe {
45617513547SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
45717513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
45817513547SCorentin Labbe struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
45917513547SCorentin Labbe
46017513547SCorentin Labbe rctx->mode = SS_OP_DES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
46117513547SCorentin Labbe op->keymode;
46217513547SCorentin Labbe return sun4i_ss_cipher_poll(areq);
46317513547SCorentin Labbe }
46417513547SCorentin Labbe
46517513547SCorentin Labbe /* ECB DES */
sun4i_ss_ecb_des_encrypt(struct skcipher_request * areq)46617513547SCorentin Labbe int sun4i_ss_ecb_des_encrypt(struct skcipher_request *areq)
46717513547SCorentin Labbe {
46817513547SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
46917513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
47017513547SCorentin Labbe struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
47117513547SCorentin Labbe
47217513547SCorentin Labbe rctx->mode = SS_OP_DES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
47317513547SCorentin Labbe op->keymode;
47417513547SCorentin Labbe return sun4i_ss_cipher_poll(areq);
47517513547SCorentin Labbe }
47617513547SCorentin Labbe
sun4i_ss_ecb_des_decrypt(struct skcipher_request * areq)47717513547SCorentin Labbe int sun4i_ss_ecb_des_decrypt(struct skcipher_request *areq)
47817513547SCorentin Labbe {
47917513547SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
48017513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
48117513547SCorentin Labbe struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
48217513547SCorentin Labbe
48317513547SCorentin Labbe rctx->mode = SS_OP_DES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
48417513547SCorentin Labbe op->keymode;
48517513547SCorentin Labbe return sun4i_ss_cipher_poll(areq);
48617513547SCorentin Labbe }
48717513547SCorentin Labbe
48817513547SCorentin Labbe /* CBC 3DES */
sun4i_ss_cbc_des3_encrypt(struct skcipher_request * areq)48917513547SCorentin Labbe int sun4i_ss_cbc_des3_encrypt(struct skcipher_request *areq)
49017513547SCorentin Labbe {
49117513547SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
49217513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
49317513547SCorentin Labbe struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
49417513547SCorentin Labbe
49517513547SCorentin Labbe rctx->mode = SS_OP_3DES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
49617513547SCorentin Labbe op->keymode;
49717513547SCorentin Labbe return sun4i_ss_cipher_poll(areq);
49817513547SCorentin Labbe }
49917513547SCorentin Labbe
sun4i_ss_cbc_des3_decrypt(struct skcipher_request * areq)50017513547SCorentin Labbe int sun4i_ss_cbc_des3_decrypt(struct skcipher_request *areq)
50117513547SCorentin Labbe {
50217513547SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
50317513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
50417513547SCorentin Labbe struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
50517513547SCorentin Labbe
50617513547SCorentin Labbe rctx->mode = SS_OP_3DES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
50717513547SCorentin Labbe op->keymode;
50817513547SCorentin Labbe return sun4i_ss_cipher_poll(areq);
50917513547SCorentin Labbe }
51017513547SCorentin Labbe
51117513547SCorentin Labbe /* ECB 3DES */
sun4i_ss_ecb_des3_encrypt(struct skcipher_request * areq)51217513547SCorentin Labbe int sun4i_ss_ecb_des3_encrypt(struct skcipher_request *areq)
51317513547SCorentin Labbe {
51417513547SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
51517513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
51617513547SCorentin Labbe struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
51717513547SCorentin Labbe
51817513547SCorentin Labbe rctx->mode = SS_OP_3DES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
51917513547SCorentin Labbe op->keymode;
52017513547SCorentin Labbe return sun4i_ss_cipher_poll(areq);
52117513547SCorentin Labbe }
52217513547SCorentin Labbe
sun4i_ss_ecb_des3_decrypt(struct skcipher_request * areq)52317513547SCorentin Labbe int sun4i_ss_ecb_des3_decrypt(struct skcipher_request *areq)
52417513547SCorentin Labbe {
52517513547SCorentin Labbe struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
52617513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
52717513547SCorentin Labbe struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
52817513547SCorentin Labbe
52917513547SCorentin Labbe rctx->mode = SS_OP_3DES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
53017513547SCorentin Labbe op->keymode;
53117513547SCorentin Labbe return sun4i_ss_cipher_poll(areq);
53217513547SCorentin Labbe }
53317513547SCorentin Labbe
sun4i_ss_cipher_init(struct crypto_tfm * tfm)53417513547SCorentin Labbe int sun4i_ss_cipher_init(struct crypto_tfm *tfm)
53517513547SCorentin Labbe {
53617513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
53717513547SCorentin Labbe struct sun4i_ss_alg_template *algt;
53817513547SCorentin Labbe const char *name = crypto_tfm_alg_name(tfm);
53917513547SCorentin Labbe int err;
54017513547SCorentin Labbe
54117513547SCorentin Labbe memset(op, 0, sizeof(struct sun4i_tfm_ctx));
54217513547SCorentin Labbe
54317513547SCorentin Labbe algt = container_of(tfm->__crt_alg, struct sun4i_ss_alg_template,
54417513547SCorentin Labbe alg.crypto.base);
54517513547SCorentin Labbe op->ss = algt->ss;
54617513547SCorentin Labbe
54789fb00f2SArd Biesheuvel op->fallback_tfm = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
54817513547SCorentin Labbe if (IS_ERR(op->fallback_tfm)) {
54917513547SCorentin Labbe dev_err(op->ss->dev, "ERROR: Cannot allocate fallback for %s %ld\n",
55017513547SCorentin Labbe name, PTR_ERR(op->fallback_tfm));
55117513547SCorentin Labbe return PTR_ERR(op->fallback_tfm);
55217513547SCorentin Labbe }
55317513547SCorentin Labbe
55489fb00f2SArd Biesheuvel crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
55589fb00f2SArd Biesheuvel sizeof(struct sun4i_cipher_req_ctx) +
55689fb00f2SArd Biesheuvel crypto_skcipher_reqsize(op->fallback_tfm));
55789fb00f2SArd Biesheuvel
558ac98fc5eSShixin Liu err = pm_runtime_resume_and_get(op->ss->dev);
55917513547SCorentin Labbe if (err < 0)
56017513547SCorentin Labbe goto error_pm;
56117513547SCorentin Labbe
56217513547SCorentin Labbe return 0;
56317513547SCorentin Labbe error_pm:
56489fb00f2SArd Biesheuvel crypto_free_skcipher(op->fallback_tfm);
56517513547SCorentin Labbe return err;
56617513547SCorentin Labbe }
56717513547SCorentin Labbe
sun4i_ss_cipher_exit(struct crypto_tfm * tfm)56817513547SCorentin Labbe void sun4i_ss_cipher_exit(struct crypto_tfm *tfm)
56917513547SCorentin Labbe {
57017513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
57117513547SCorentin Labbe
57289fb00f2SArd Biesheuvel crypto_free_skcipher(op->fallback_tfm);
57317513547SCorentin Labbe pm_runtime_put(op->ss->dev);
57417513547SCorentin Labbe }
57517513547SCorentin Labbe
57617513547SCorentin Labbe /* check and set the AES key, prepare the mode to be used */
sun4i_ss_aes_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int keylen)57717513547SCorentin Labbe int sun4i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
57817513547SCorentin Labbe unsigned int keylen)
57917513547SCorentin Labbe {
58017513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
58117513547SCorentin Labbe struct sun4i_ss_ctx *ss = op->ss;
58217513547SCorentin Labbe
58317513547SCorentin Labbe switch (keylen) {
58417513547SCorentin Labbe case 128 / 8:
58517513547SCorentin Labbe op->keymode = SS_AES_128BITS;
58617513547SCorentin Labbe break;
58717513547SCorentin Labbe case 192 / 8:
58817513547SCorentin Labbe op->keymode = SS_AES_192BITS;
58917513547SCorentin Labbe break;
59017513547SCorentin Labbe case 256 / 8:
59117513547SCorentin Labbe op->keymode = SS_AES_256BITS;
59217513547SCorentin Labbe break;
59317513547SCorentin Labbe default:
5942edf8641SCorentin Labbe dev_dbg(ss->dev, "ERROR: Invalid keylen %u\n", keylen);
59517513547SCorentin Labbe return -EINVAL;
59617513547SCorentin Labbe }
59717513547SCorentin Labbe op->keylen = keylen;
59817513547SCorentin Labbe memcpy(op->key, key, keylen);
59917513547SCorentin Labbe
60089fb00f2SArd Biesheuvel crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
60189fb00f2SArd Biesheuvel crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
60217513547SCorentin Labbe
60389fb00f2SArd Biesheuvel return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
60417513547SCorentin Labbe }
60517513547SCorentin Labbe
60617513547SCorentin Labbe /* check and set the DES key, prepare the mode to be used */
sun4i_ss_des_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int keylen)60717513547SCorentin Labbe int sun4i_ss_des_setkey(struct crypto_skcipher *tfm, const u8 *key,
60817513547SCorentin Labbe unsigned int keylen)
60917513547SCorentin Labbe {
61017513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
61117513547SCorentin Labbe int err;
61217513547SCorentin Labbe
61317513547SCorentin Labbe err = verify_skcipher_des_key(tfm, key);
61417513547SCorentin Labbe if (err)
61517513547SCorentin Labbe return err;
61617513547SCorentin Labbe
61717513547SCorentin Labbe op->keylen = keylen;
61817513547SCorentin Labbe memcpy(op->key, key, keylen);
61917513547SCorentin Labbe
62089fb00f2SArd Biesheuvel crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
62189fb00f2SArd Biesheuvel crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
62217513547SCorentin Labbe
62389fb00f2SArd Biesheuvel return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
62417513547SCorentin Labbe }
62517513547SCorentin Labbe
62617513547SCorentin Labbe /* check and set the 3DES key, prepare the mode to be used */
sun4i_ss_des3_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int keylen)62717513547SCorentin Labbe int sun4i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
62817513547SCorentin Labbe unsigned int keylen)
62917513547SCorentin Labbe {
63017513547SCorentin Labbe struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
63117513547SCorentin Labbe int err;
63217513547SCorentin Labbe
63317513547SCorentin Labbe err = verify_skcipher_des3_key(tfm, key);
63417513547SCorentin Labbe if (err)
63517513547SCorentin Labbe return err;
63617513547SCorentin Labbe
63717513547SCorentin Labbe op->keylen = keylen;
63817513547SCorentin Labbe memcpy(op->key, key, keylen);
63917513547SCorentin Labbe
64089fb00f2SArd Biesheuvel crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
64189fb00f2SArd Biesheuvel crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
64217513547SCorentin Labbe
64389fb00f2SArd Biesheuvel return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
64417513547SCorentin Labbe }
645