xref: /linux/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c (revision b756f1c8fc9d84e3f546d7ffe056c5352f4aab05)
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  *
1117513547SCorentin Labbe  * You could find the datasheet in Documentation/arm/sunxi.rst
1217513547SCorentin Labbe  */
1317513547SCorentin Labbe #include "sun4i-ss.h"
1417513547SCorentin Labbe 
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;
23*b756f1c8SCorentin Labbe 	void *backup_iv = NULL;
2417513547SCorentin Labbe 	/* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
2517513547SCorentin Labbe 	u32 rx_cnt = SS_RX_DEFAULT;
2617513547SCorentin Labbe 	u32 tx_cnt = 0;
2717513547SCorentin Labbe 	u32 spaces;
2817513547SCorentin Labbe 	u32 v;
2917513547SCorentin Labbe 	int err = 0;
3017513547SCorentin Labbe 	unsigned int i;
3117513547SCorentin Labbe 	unsigned int ileft = areq->cryptlen;
3217513547SCorentin Labbe 	unsigned int oleft = areq->cryptlen;
3317513547SCorentin Labbe 	unsigned int todo;
3417513547SCorentin Labbe 	struct sg_mapping_iter mi, mo;
3517513547SCorentin Labbe 	unsigned int oi, oo; /* offset for in and out */
3617513547SCorentin Labbe 	unsigned long flags;
3717513547SCorentin Labbe 
3817513547SCorentin Labbe 	if (!areq->cryptlen)
3917513547SCorentin Labbe 		return 0;
4017513547SCorentin Labbe 
4117513547SCorentin Labbe 	if (!areq->src || !areq->dst) {
4217513547SCorentin Labbe 		dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");
4317513547SCorentin Labbe 		return -EINVAL;
4417513547SCorentin Labbe 	}
4517513547SCorentin Labbe 
46*b756f1c8SCorentin Labbe 	if (areq->iv && ivsize > 0 && mode & SS_DECRYPTION) {
47*b756f1c8SCorentin Labbe 		backup_iv = kzalloc(ivsize, GFP_KERNEL);
48*b756f1c8SCorentin Labbe 		if (!backup_iv)
49*b756f1c8SCorentin Labbe 			return -ENOMEM;
50*b756f1c8SCorentin Labbe 		scatterwalk_map_and_copy(backup_iv, areq->src, areq->cryptlen - ivsize, ivsize, 0);
51*b756f1c8SCorentin Labbe 	}
52*b756f1c8SCorentin Labbe 
5317513547SCorentin Labbe 	spin_lock_irqsave(&ss->slock, flags);
5417513547SCorentin Labbe 
5517513547SCorentin Labbe 	for (i = 0; i < op->keylen; i += 4)
5617513547SCorentin Labbe 		writel(*(op->key + i / 4), ss->base + SS_KEY0 + i);
5717513547SCorentin Labbe 
5817513547SCorentin Labbe 	if (areq->iv) {
5917513547SCorentin Labbe 		for (i = 0; i < 4 && i < ivsize / 4; i++) {
6017513547SCorentin Labbe 			v = *(u32 *)(areq->iv + i * 4);
6117513547SCorentin Labbe 			writel(v, ss->base + SS_IV0 + i * 4);
6217513547SCorentin Labbe 		}
6317513547SCorentin Labbe 	}
6417513547SCorentin Labbe 	writel(mode, ss->base + SS_CTL);
6517513547SCorentin Labbe 
6617513547SCorentin Labbe 	sg_miter_start(&mi, areq->src, sg_nents(areq->src),
6717513547SCorentin Labbe 		       SG_MITER_FROM_SG | SG_MITER_ATOMIC);
6817513547SCorentin Labbe 	sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
6917513547SCorentin Labbe 		       SG_MITER_TO_SG | SG_MITER_ATOMIC);
7017513547SCorentin Labbe 	sg_miter_next(&mi);
7117513547SCorentin Labbe 	sg_miter_next(&mo);
7217513547SCorentin Labbe 	if (!mi.addr || !mo.addr) {
7317513547SCorentin Labbe 		dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
7417513547SCorentin Labbe 		err = -EINVAL;
7517513547SCorentin Labbe 		goto release_ss;
7617513547SCorentin Labbe 	}
7717513547SCorentin Labbe 
7817513547SCorentin Labbe 	ileft = areq->cryptlen / 4;
7917513547SCorentin Labbe 	oleft = areq->cryptlen / 4;
8017513547SCorentin Labbe 	oi = 0;
8117513547SCorentin Labbe 	oo = 0;
8217513547SCorentin Labbe 	do {
83d6e9da21SHerbert Xu 		todo = min(rx_cnt, ileft);
84d6e9da21SHerbert Xu 		todo = min_t(size_t, todo, (mi.length - oi) / 4);
8517513547SCorentin Labbe 		if (todo) {
8617513547SCorentin Labbe 			ileft -= todo;
8717513547SCorentin Labbe 			writesl(ss->base + SS_RXFIFO, mi.addr + oi, todo);
8817513547SCorentin Labbe 			oi += todo * 4;
8917513547SCorentin Labbe 		}
9017513547SCorentin Labbe 		if (oi == mi.length) {
9117513547SCorentin Labbe 			sg_miter_next(&mi);
9217513547SCorentin Labbe 			oi = 0;
9317513547SCorentin Labbe 		}
9417513547SCorentin Labbe 
9517513547SCorentin Labbe 		spaces = readl(ss->base + SS_FCSR);
9617513547SCorentin Labbe 		rx_cnt = SS_RXFIFO_SPACES(spaces);
9717513547SCorentin Labbe 		tx_cnt = SS_TXFIFO_SPACES(spaces);
9817513547SCorentin Labbe 
99d6e9da21SHerbert Xu 		todo = min(tx_cnt, oleft);
100d6e9da21SHerbert Xu 		todo = min_t(size_t, todo, (mo.length - oo) / 4);
10117513547SCorentin Labbe 		if (todo) {
10217513547SCorentin Labbe 			oleft -= todo;
10317513547SCorentin Labbe 			readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);
10417513547SCorentin Labbe 			oo += todo * 4;
10517513547SCorentin Labbe 		}
10617513547SCorentin Labbe 		if (oo == mo.length) {
10717513547SCorentin Labbe 			sg_miter_next(&mo);
10817513547SCorentin Labbe 			oo = 0;
10917513547SCorentin Labbe 		}
11017513547SCorentin Labbe 	} while (oleft);
11117513547SCorentin Labbe 
11217513547SCorentin Labbe 	if (areq->iv) {
113*b756f1c8SCorentin Labbe 		if (mode & SS_DECRYPTION) {
114*b756f1c8SCorentin Labbe 			memcpy(areq->iv, backup_iv, ivsize);
115*b756f1c8SCorentin Labbe 			kfree_sensitive(backup_iv);
116*b756f1c8SCorentin Labbe 		} else {
117*b756f1c8SCorentin Labbe 			scatterwalk_map_and_copy(areq->iv, areq->dst, areq->cryptlen - ivsize,
118*b756f1c8SCorentin Labbe 						 ivsize, 0);
11917513547SCorentin Labbe 		}
12017513547SCorentin Labbe 	}
12117513547SCorentin Labbe 
12217513547SCorentin Labbe release_ss:
12317513547SCorentin Labbe 	sg_miter_stop(&mi);
12417513547SCorentin Labbe 	sg_miter_stop(&mo);
12517513547SCorentin Labbe 	writel(0, ss->base + SS_CTL);
12617513547SCorentin Labbe 	spin_unlock_irqrestore(&ss->slock, flags);
12717513547SCorentin Labbe 	return err;
12817513547SCorentin Labbe }
12917513547SCorentin Labbe 
13017513547SCorentin Labbe 
13117513547SCorentin Labbe static int noinline_for_stack sun4i_ss_cipher_poll_fallback(struct skcipher_request *areq)
13217513547SCorentin Labbe {
13317513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
13417513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
13517513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
13617513547SCorentin Labbe 	int err;
13717513547SCorentin Labbe 
13889fb00f2SArd Biesheuvel 	skcipher_request_set_tfm(&ctx->fallback_req, op->fallback_tfm);
13989fb00f2SArd Biesheuvel 	skcipher_request_set_callback(&ctx->fallback_req, areq->base.flags,
14089fb00f2SArd Biesheuvel 				      areq->base.complete, areq->base.data);
14189fb00f2SArd Biesheuvel 	skcipher_request_set_crypt(&ctx->fallback_req, areq->src, areq->dst,
14217513547SCorentin Labbe 				   areq->cryptlen, areq->iv);
14317513547SCorentin Labbe 	if (ctx->mode & SS_DECRYPTION)
14489fb00f2SArd Biesheuvel 		err = crypto_skcipher_decrypt(&ctx->fallback_req);
14517513547SCorentin Labbe 	else
14689fb00f2SArd Biesheuvel 		err = crypto_skcipher_encrypt(&ctx->fallback_req);
14717513547SCorentin Labbe 
14817513547SCorentin Labbe 	return err;
14917513547SCorentin Labbe }
15017513547SCorentin Labbe 
15117513547SCorentin Labbe /* Generic function that support SG with size not multiple of 4 */
15217513547SCorentin Labbe static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
15317513547SCorentin Labbe {
15417513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
15517513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
15617513547SCorentin Labbe 	struct sun4i_ss_ctx *ss = op->ss;
15717513547SCorentin Labbe 	int no_chunk = 1;
15817513547SCorentin Labbe 	struct scatterlist *in_sg = areq->src;
15917513547SCorentin Labbe 	struct scatterlist *out_sg = areq->dst;
16017513547SCorentin Labbe 	unsigned int ivsize = crypto_skcipher_ivsize(tfm);
16117513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
16217513547SCorentin Labbe 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
16317513547SCorentin Labbe 	struct sun4i_ss_alg_template *algt;
16417513547SCorentin Labbe 	u32 mode = ctx->mode;
16517513547SCorentin Labbe 	/* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
16617513547SCorentin Labbe 	u32 rx_cnt = SS_RX_DEFAULT;
16717513547SCorentin Labbe 	u32 tx_cnt = 0;
16817513547SCorentin Labbe 	u32 v;
16917513547SCorentin Labbe 	u32 spaces;
17017513547SCorentin Labbe 	int err = 0;
17117513547SCorentin Labbe 	unsigned int i;
17217513547SCorentin Labbe 	unsigned int ileft = areq->cryptlen;
17317513547SCorentin Labbe 	unsigned int oleft = areq->cryptlen;
17417513547SCorentin Labbe 	unsigned int todo;
175*b756f1c8SCorentin Labbe 	void *backup_iv = NULL;
17617513547SCorentin Labbe 	struct sg_mapping_iter mi, mo;
17717513547SCorentin Labbe 	unsigned int oi, oo;	/* offset for in and out */
17817513547SCorentin Labbe 	unsigned int ob = 0;	/* offset in buf */
17917513547SCorentin Labbe 	unsigned int obo = 0;	/* offset in bufo*/
18017513547SCorentin Labbe 	unsigned int obl = 0;	/* length of data in bufo */
18117513547SCorentin Labbe 	unsigned long flags;
18217513547SCorentin Labbe 	bool need_fallback;
18317513547SCorentin Labbe 
18417513547SCorentin Labbe 	if (!areq->cryptlen)
18517513547SCorentin Labbe 		return 0;
18617513547SCorentin Labbe 
18717513547SCorentin Labbe 	if (!areq->src || !areq->dst) {
18817513547SCorentin Labbe 		dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");
18917513547SCorentin Labbe 		return -EINVAL;
19017513547SCorentin Labbe 	}
19117513547SCorentin Labbe 
19217513547SCorentin Labbe 	algt = container_of(alg, struct sun4i_ss_alg_template, alg.crypto);
19317513547SCorentin Labbe 	if (areq->cryptlen % algt->alg.crypto.base.cra_blocksize)
19417513547SCorentin Labbe 		need_fallback = true;
19517513547SCorentin Labbe 
19617513547SCorentin Labbe 	/*
19717513547SCorentin Labbe 	 * if we have only SGs with size multiple of 4,
19817513547SCorentin Labbe 	 * we can use the SS optimized function
19917513547SCorentin Labbe 	 */
20017513547SCorentin Labbe 	while (in_sg && no_chunk == 1) {
2017bdcd851SCorentin Labbe 		if ((in_sg->length | in_sg->offset) & 3u)
20217513547SCorentin Labbe 			no_chunk = 0;
20317513547SCorentin Labbe 		in_sg = sg_next(in_sg);
20417513547SCorentin Labbe 	}
20517513547SCorentin Labbe 	while (out_sg && no_chunk == 1) {
2067bdcd851SCorentin Labbe 		if ((out_sg->length | out_sg->offset) & 3u)
20717513547SCorentin Labbe 			no_chunk = 0;
20817513547SCorentin Labbe 		out_sg = sg_next(out_sg);
20917513547SCorentin Labbe 	}
21017513547SCorentin Labbe 
21117513547SCorentin Labbe 	if (no_chunk == 1 && !need_fallback)
21217513547SCorentin Labbe 		return sun4i_ss_opti_poll(areq);
21317513547SCorentin Labbe 
21417513547SCorentin Labbe 	if (need_fallback)
21517513547SCorentin Labbe 		return sun4i_ss_cipher_poll_fallback(areq);
21617513547SCorentin Labbe 
217*b756f1c8SCorentin Labbe 	if (areq->iv && ivsize > 0 && mode & SS_DECRYPTION) {
218*b756f1c8SCorentin Labbe 		backup_iv = kzalloc(ivsize, GFP_KERNEL);
219*b756f1c8SCorentin Labbe 		if (!backup_iv)
220*b756f1c8SCorentin Labbe 			return -ENOMEM;
221*b756f1c8SCorentin Labbe 		scatterwalk_map_and_copy(backup_iv, areq->src, areq->cryptlen - ivsize, ivsize, 0);
222*b756f1c8SCorentin Labbe 	}
223*b756f1c8SCorentin Labbe 
22417513547SCorentin Labbe 	spin_lock_irqsave(&ss->slock, flags);
22517513547SCorentin Labbe 
22617513547SCorentin Labbe 	for (i = 0; i < op->keylen; i += 4)
22717513547SCorentin Labbe 		writel(*(op->key + i / 4), ss->base + SS_KEY0 + i);
22817513547SCorentin Labbe 
22917513547SCorentin Labbe 	if (areq->iv) {
23017513547SCorentin Labbe 		for (i = 0; i < 4 && i < ivsize / 4; i++) {
23117513547SCorentin Labbe 			v = *(u32 *)(areq->iv + i * 4);
23217513547SCorentin Labbe 			writel(v, ss->base + SS_IV0 + i * 4);
23317513547SCorentin Labbe 		}
23417513547SCorentin Labbe 	}
23517513547SCorentin Labbe 	writel(mode, ss->base + SS_CTL);
23617513547SCorentin Labbe 
23717513547SCorentin Labbe 	sg_miter_start(&mi, areq->src, sg_nents(areq->src),
23817513547SCorentin Labbe 		       SG_MITER_FROM_SG | SG_MITER_ATOMIC);
23917513547SCorentin Labbe 	sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
24017513547SCorentin Labbe 		       SG_MITER_TO_SG | SG_MITER_ATOMIC);
24117513547SCorentin Labbe 	sg_miter_next(&mi);
24217513547SCorentin Labbe 	sg_miter_next(&mo);
24317513547SCorentin Labbe 	if (!mi.addr || !mo.addr) {
24417513547SCorentin Labbe 		dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
24517513547SCorentin Labbe 		err = -EINVAL;
24617513547SCorentin Labbe 		goto release_ss;
24717513547SCorentin Labbe 	}
24817513547SCorentin Labbe 	ileft = areq->cryptlen;
24917513547SCorentin Labbe 	oleft = areq->cryptlen;
25017513547SCorentin Labbe 	oi = 0;
25117513547SCorentin Labbe 	oo = 0;
25217513547SCorentin Labbe 
25317513547SCorentin Labbe 	while (oleft) {
25417513547SCorentin Labbe 		if (ileft) {
25517513547SCorentin Labbe 			/*
25617513547SCorentin Labbe 			 * todo is the number of consecutive 4byte word that we
25717513547SCorentin Labbe 			 * can read from current SG
25817513547SCorentin Labbe 			 */
259d6e9da21SHerbert Xu 			todo = min(rx_cnt, ileft / 4);
260d6e9da21SHerbert Xu 			todo = min_t(size_t, todo, (mi.length - oi) / 4);
26117513547SCorentin Labbe 			if (todo && !ob) {
26217513547SCorentin Labbe 				writesl(ss->base + SS_RXFIFO, mi.addr + oi,
26317513547SCorentin Labbe 					todo);
26417513547SCorentin Labbe 				ileft -= todo * 4;
26517513547SCorentin Labbe 				oi += todo * 4;
26617513547SCorentin Labbe 			} else {
26717513547SCorentin Labbe 				/*
26817513547SCorentin Labbe 				 * not enough consecutive bytes, so we need to
26917513547SCorentin Labbe 				 * linearize in buf. todo is in bytes
27017513547SCorentin Labbe 				 * After that copy, if we have a multiple of 4
27117513547SCorentin Labbe 				 * we need to be able to write all buf in one
27217513547SCorentin Labbe 				 * pass, so it is why we min() with rx_cnt
27317513547SCorentin Labbe 				 */
274d6e9da21SHerbert Xu 				todo = min(rx_cnt * 4 - ob, ileft);
275d6e9da21SHerbert Xu 				todo = min_t(size_t, todo, mi.length - oi);
27658351351SCorentin Labbe 				memcpy(ss->buf + ob, mi.addr + oi, todo);
27717513547SCorentin Labbe 				ileft -= todo;
27817513547SCorentin Labbe 				oi += todo;
27917513547SCorentin Labbe 				ob += todo;
28017513547SCorentin Labbe 				if (!(ob % 4)) {
28158351351SCorentin Labbe 					writesl(ss->base + SS_RXFIFO, ss->buf,
28217513547SCorentin Labbe 						ob / 4);
28317513547SCorentin Labbe 					ob = 0;
28417513547SCorentin Labbe 				}
28517513547SCorentin Labbe 			}
28617513547SCorentin Labbe 			if (oi == mi.length) {
28717513547SCorentin Labbe 				sg_miter_next(&mi);
28817513547SCorentin Labbe 				oi = 0;
28917513547SCorentin Labbe 			}
29017513547SCorentin Labbe 		}
29117513547SCorentin Labbe 
29217513547SCorentin Labbe 		spaces = readl(ss->base + SS_FCSR);
29317513547SCorentin Labbe 		rx_cnt = SS_RXFIFO_SPACES(spaces);
29417513547SCorentin Labbe 		tx_cnt = SS_TXFIFO_SPACES(spaces);
295d6e9da21SHerbert Xu 		dev_dbg(ss->dev,
296d6e9da21SHerbert Xu 			"%x %u/%zu %u/%u cnt=%u %u/%zu %u/%u cnt=%u %u\n",
29717513547SCorentin Labbe 			mode,
29817513547SCorentin Labbe 			oi, mi.length, ileft, areq->cryptlen, rx_cnt,
29917513547SCorentin Labbe 			oo, mo.length, oleft, areq->cryptlen, tx_cnt, ob);
30017513547SCorentin Labbe 
30117513547SCorentin Labbe 		if (!tx_cnt)
30217513547SCorentin Labbe 			continue;
30317513547SCorentin Labbe 		/* todo in 4bytes word */
304d6e9da21SHerbert Xu 		todo = min(tx_cnt, oleft / 4);
305d6e9da21SHerbert Xu 		todo = min_t(size_t, todo, (mo.length - oo) / 4);
30617513547SCorentin Labbe 		if (todo) {
30717513547SCorentin Labbe 			readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);
30817513547SCorentin Labbe 			oleft -= todo * 4;
30917513547SCorentin Labbe 			oo += todo * 4;
31017513547SCorentin Labbe 			if (oo == mo.length) {
31117513547SCorentin Labbe 				sg_miter_next(&mo);
31217513547SCorentin Labbe 				oo = 0;
31317513547SCorentin Labbe 			}
31417513547SCorentin Labbe 		} else {
31517513547SCorentin Labbe 			/*
31617513547SCorentin Labbe 			 * read obl bytes in bufo, we read at maximum for
31717513547SCorentin Labbe 			 * emptying the device
31817513547SCorentin Labbe 			 */
31958351351SCorentin Labbe 			readsl(ss->base + SS_TXFIFO, ss->bufo, tx_cnt);
32017513547SCorentin Labbe 			obl = tx_cnt * 4;
32117513547SCorentin Labbe 			obo = 0;
32217513547SCorentin Labbe 			do {
32317513547SCorentin Labbe 				/*
32417513547SCorentin Labbe 				 * how many bytes we can copy ?
32517513547SCorentin Labbe 				 * no more than remaining SG size
32617513547SCorentin Labbe 				 * no more than remaining buffer
32717513547SCorentin Labbe 				 * no need to test against oleft
32817513547SCorentin Labbe 				 */
329d6e9da21SHerbert Xu 				todo = min_t(size_t,
330d6e9da21SHerbert Xu 					     mo.length - oo, obl - obo);
33158351351SCorentin Labbe 				memcpy(mo.addr + oo, ss->bufo + obo, todo);
33217513547SCorentin Labbe 				oleft -= todo;
33317513547SCorentin Labbe 				obo += todo;
33417513547SCorentin Labbe 				oo += todo;
33517513547SCorentin Labbe 				if (oo == mo.length) {
33617513547SCorentin Labbe 					sg_miter_next(&mo);
33717513547SCorentin Labbe 					oo = 0;
33817513547SCorentin Labbe 				}
33917513547SCorentin Labbe 			} while (obo < obl);
34017513547SCorentin Labbe 			/* bufo must be fully used here */
34117513547SCorentin Labbe 		}
34217513547SCorentin Labbe 	}
34317513547SCorentin Labbe 	if (areq->iv) {
344*b756f1c8SCorentin Labbe 		if (mode & SS_DECRYPTION) {
345*b756f1c8SCorentin Labbe 			memcpy(areq->iv, backup_iv, ivsize);
346*b756f1c8SCorentin Labbe 			kfree_sensitive(backup_iv);
347*b756f1c8SCorentin Labbe 		} else {
348*b756f1c8SCorentin Labbe 			scatterwalk_map_and_copy(areq->iv, areq->dst, areq->cryptlen - ivsize,
349*b756f1c8SCorentin Labbe 						 ivsize, 0);
35017513547SCorentin Labbe 		}
35117513547SCorentin Labbe 	}
35217513547SCorentin Labbe 
35317513547SCorentin Labbe release_ss:
35417513547SCorentin Labbe 	sg_miter_stop(&mi);
35517513547SCorentin Labbe 	sg_miter_stop(&mo);
35617513547SCorentin Labbe 	writel(0, ss->base + SS_CTL);
35717513547SCorentin Labbe 	spin_unlock_irqrestore(&ss->slock, flags);
35817513547SCorentin Labbe 
35917513547SCorentin Labbe 	return err;
36017513547SCorentin Labbe }
36117513547SCorentin Labbe 
36217513547SCorentin Labbe /* CBC AES */
36317513547SCorentin Labbe int sun4i_ss_cbc_aes_encrypt(struct skcipher_request *areq)
36417513547SCorentin Labbe {
36517513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
36617513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
36717513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
36817513547SCorentin Labbe 
36917513547SCorentin Labbe 	rctx->mode = SS_OP_AES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
37017513547SCorentin Labbe 		op->keymode;
37117513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
37217513547SCorentin Labbe }
37317513547SCorentin Labbe 
37417513547SCorentin Labbe int sun4i_ss_cbc_aes_decrypt(struct skcipher_request *areq)
37517513547SCorentin Labbe {
37617513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
37717513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
37817513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
37917513547SCorentin Labbe 
38017513547SCorentin Labbe 	rctx->mode = SS_OP_AES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
38117513547SCorentin Labbe 		op->keymode;
38217513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
38317513547SCorentin Labbe }
38417513547SCorentin Labbe 
38517513547SCorentin Labbe /* ECB AES */
38617513547SCorentin Labbe int sun4i_ss_ecb_aes_encrypt(struct skcipher_request *areq)
38717513547SCorentin Labbe {
38817513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
38917513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
39017513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
39117513547SCorentin Labbe 
39217513547SCorentin Labbe 	rctx->mode = SS_OP_AES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
39317513547SCorentin Labbe 		op->keymode;
39417513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
39517513547SCorentin Labbe }
39617513547SCorentin Labbe 
39717513547SCorentin Labbe int sun4i_ss_ecb_aes_decrypt(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_ECB | SS_ENABLED | SS_DECRYPTION |
40417513547SCorentin Labbe 		op->keymode;
40517513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
40617513547SCorentin Labbe }
40717513547SCorentin Labbe 
40817513547SCorentin Labbe /* CBC DES */
40917513547SCorentin Labbe int sun4i_ss_cbc_des_encrypt(struct skcipher_request *areq)
41017513547SCorentin Labbe {
41117513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
41217513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
41317513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
41417513547SCorentin Labbe 
41517513547SCorentin Labbe 	rctx->mode = SS_OP_DES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
41617513547SCorentin Labbe 		op->keymode;
41717513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
41817513547SCorentin Labbe }
41917513547SCorentin Labbe 
42017513547SCorentin Labbe int sun4i_ss_cbc_des_decrypt(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_DES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
42717513547SCorentin Labbe 		op->keymode;
42817513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
42917513547SCorentin Labbe }
43017513547SCorentin Labbe 
43117513547SCorentin Labbe /* ECB DES */
43217513547SCorentin Labbe int sun4i_ss_ecb_des_encrypt(struct skcipher_request *areq)
43317513547SCorentin Labbe {
43417513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
43517513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
43617513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
43717513547SCorentin Labbe 
43817513547SCorentin Labbe 	rctx->mode = SS_OP_DES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
43917513547SCorentin Labbe 		op->keymode;
44017513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
44117513547SCorentin Labbe }
44217513547SCorentin Labbe 
44317513547SCorentin Labbe int sun4i_ss_ecb_des_decrypt(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_ECB | SS_ENABLED | SS_DECRYPTION |
45017513547SCorentin Labbe 		op->keymode;
45117513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
45217513547SCorentin Labbe }
45317513547SCorentin Labbe 
45417513547SCorentin Labbe /* CBC 3DES */
45517513547SCorentin Labbe int sun4i_ss_cbc_des3_encrypt(struct skcipher_request *areq)
45617513547SCorentin Labbe {
45717513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
45817513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
45917513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
46017513547SCorentin Labbe 
46117513547SCorentin Labbe 	rctx->mode = SS_OP_3DES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
46217513547SCorentin Labbe 		op->keymode;
46317513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
46417513547SCorentin Labbe }
46517513547SCorentin Labbe 
46617513547SCorentin Labbe int sun4i_ss_cbc_des3_decrypt(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_3DES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
47317513547SCorentin Labbe 		op->keymode;
47417513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
47517513547SCorentin Labbe }
47617513547SCorentin Labbe 
47717513547SCorentin Labbe /* ECB 3DES */
47817513547SCorentin Labbe int sun4i_ss_ecb_des3_encrypt(struct skcipher_request *areq)
47917513547SCorentin Labbe {
48017513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
48117513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
48217513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
48317513547SCorentin Labbe 
48417513547SCorentin Labbe 	rctx->mode = SS_OP_3DES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
48517513547SCorentin Labbe 		op->keymode;
48617513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
48717513547SCorentin Labbe }
48817513547SCorentin Labbe 
48917513547SCorentin Labbe int sun4i_ss_ecb_des3_decrypt(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_ECB | SS_ENABLED | SS_DECRYPTION |
49617513547SCorentin Labbe 		op->keymode;
49717513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
49817513547SCorentin Labbe }
49917513547SCorentin Labbe 
50017513547SCorentin Labbe int sun4i_ss_cipher_init(struct crypto_tfm *tfm)
50117513547SCorentin Labbe {
50217513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
50317513547SCorentin Labbe 	struct sun4i_ss_alg_template *algt;
50417513547SCorentin Labbe 	const char *name = crypto_tfm_alg_name(tfm);
50517513547SCorentin Labbe 	int err;
50617513547SCorentin Labbe 
50717513547SCorentin Labbe 	memset(op, 0, sizeof(struct sun4i_tfm_ctx));
50817513547SCorentin Labbe 
50917513547SCorentin Labbe 	algt = container_of(tfm->__crt_alg, struct sun4i_ss_alg_template,
51017513547SCorentin Labbe 			    alg.crypto.base);
51117513547SCorentin Labbe 	op->ss = algt->ss;
51217513547SCorentin Labbe 
51389fb00f2SArd Biesheuvel 	op->fallback_tfm = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
51417513547SCorentin Labbe 	if (IS_ERR(op->fallback_tfm)) {
51517513547SCorentin Labbe 		dev_err(op->ss->dev, "ERROR: Cannot allocate fallback for %s %ld\n",
51617513547SCorentin Labbe 			name, PTR_ERR(op->fallback_tfm));
51717513547SCorentin Labbe 		return PTR_ERR(op->fallback_tfm);
51817513547SCorentin Labbe 	}
51917513547SCorentin Labbe 
52089fb00f2SArd Biesheuvel 	crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
52189fb00f2SArd Biesheuvel 				    sizeof(struct sun4i_cipher_req_ctx) +
52289fb00f2SArd Biesheuvel 				    crypto_skcipher_reqsize(op->fallback_tfm));
52389fb00f2SArd Biesheuvel 
52489fb00f2SArd Biesheuvel 
52517513547SCorentin Labbe 	err = pm_runtime_get_sync(op->ss->dev);
52617513547SCorentin Labbe 	if (err < 0)
52717513547SCorentin Labbe 		goto error_pm;
52817513547SCorentin Labbe 
52917513547SCorentin Labbe 	return 0;
53017513547SCorentin Labbe error_pm:
53189fb00f2SArd Biesheuvel 	crypto_free_skcipher(op->fallback_tfm);
53217513547SCorentin Labbe 	return err;
53317513547SCorentin Labbe }
53417513547SCorentin Labbe 
53517513547SCorentin Labbe void sun4i_ss_cipher_exit(struct crypto_tfm *tfm)
53617513547SCorentin Labbe {
53717513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
53817513547SCorentin Labbe 
53989fb00f2SArd Biesheuvel 	crypto_free_skcipher(op->fallback_tfm);
54017513547SCorentin Labbe 	pm_runtime_put(op->ss->dev);
54117513547SCorentin Labbe }
54217513547SCorentin Labbe 
54317513547SCorentin Labbe /* check and set the AES key, prepare the mode to be used */
54417513547SCorentin Labbe int sun4i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
54517513547SCorentin Labbe 			unsigned int keylen)
54617513547SCorentin Labbe {
54717513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
54817513547SCorentin Labbe 	struct sun4i_ss_ctx *ss = op->ss;
54917513547SCorentin Labbe 
55017513547SCorentin Labbe 	switch (keylen) {
55117513547SCorentin Labbe 	case 128 / 8:
55217513547SCorentin Labbe 		op->keymode = SS_AES_128BITS;
55317513547SCorentin Labbe 		break;
55417513547SCorentin Labbe 	case 192 / 8:
55517513547SCorentin Labbe 		op->keymode = SS_AES_192BITS;
55617513547SCorentin Labbe 		break;
55717513547SCorentin Labbe 	case 256 / 8:
55817513547SCorentin Labbe 		op->keymode = SS_AES_256BITS;
55917513547SCorentin Labbe 		break;
56017513547SCorentin Labbe 	default:
5612edf8641SCorentin Labbe 		dev_dbg(ss->dev, "ERROR: Invalid keylen %u\n", keylen);
56217513547SCorentin Labbe 		return -EINVAL;
56317513547SCorentin Labbe 	}
56417513547SCorentin Labbe 	op->keylen = keylen;
56517513547SCorentin Labbe 	memcpy(op->key, key, keylen);
56617513547SCorentin Labbe 
56789fb00f2SArd Biesheuvel 	crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
56889fb00f2SArd Biesheuvel 	crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
56917513547SCorentin Labbe 
57089fb00f2SArd Biesheuvel 	return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
57117513547SCorentin Labbe }
57217513547SCorentin Labbe 
57317513547SCorentin Labbe /* check and set the DES key, prepare the mode to be used */
57417513547SCorentin Labbe int sun4i_ss_des_setkey(struct crypto_skcipher *tfm, const u8 *key,
57517513547SCorentin Labbe 			unsigned int keylen)
57617513547SCorentin Labbe {
57717513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
57817513547SCorentin Labbe 	int err;
57917513547SCorentin Labbe 
58017513547SCorentin Labbe 	err = verify_skcipher_des_key(tfm, key);
58117513547SCorentin Labbe 	if (err)
58217513547SCorentin Labbe 		return err;
58317513547SCorentin Labbe 
58417513547SCorentin Labbe 	op->keylen = keylen;
58517513547SCorentin Labbe 	memcpy(op->key, key, keylen);
58617513547SCorentin Labbe 
58789fb00f2SArd Biesheuvel 	crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
58889fb00f2SArd Biesheuvel 	crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
58917513547SCorentin Labbe 
59089fb00f2SArd Biesheuvel 	return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
59117513547SCorentin Labbe }
59217513547SCorentin Labbe 
59317513547SCorentin Labbe /* check and set the 3DES key, prepare the mode to be used */
59417513547SCorentin Labbe int sun4i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
59517513547SCorentin Labbe 			 unsigned int keylen)
59617513547SCorentin Labbe {
59717513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
59817513547SCorentin Labbe 	int err;
59917513547SCorentin Labbe 
60017513547SCorentin Labbe 	err = verify_skcipher_des3_key(tfm, key);
60117513547SCorentin Labbe 	if (err)
60217513547SCorentin Labbe 		return err;
60317513547SCorentin Labbe 
60417513547SCorentin Labbe 	op->keylen = keylen;
60517513547SCorentin Labbe 	memcpy(op->key, key, keylen);
60617513547SCorentin Labbe 
60789fb00f2SArd Biesheuvel 	crypto_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
60889fb00f2SArd Biesheuvel 	crypto_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
60917513547SCorentin Labbe 
61089fb00f2SArd Biesheuvel 	return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
61117513547SCorentin Labbe 
61217513547SCorentin Labbe }
613