xref: /linux/drivers/crypto/allwinner/sun4i-ss/sun4i-ss-cipher.c (revision d6e9da21ee8246b5e556b3b153401ab045adb986)
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;
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;
3317513547SCorentin Labbe 	struct sg_mapping_iter mi, mo;
3417513547SCorentin Labbe 	unsigned int oi, oo; /* offset for in and out */
3517513547SCorentin Labbe 	unsigned long flags;
3617513547SCorentin Labbe 
3717513547SCorentin Labbe 	if (!areq->cryptlen)
3817513547SCorentin Labbe 		return 0;
3917513547SCorentin Labbe 
4017513547SCorentin Labbe 	if (!areq->src || !areq->dst) {
4117513547SCorentin Labbe 		dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");
4217513547SCorentin Labbe 		return -EINVAL;
4317513547SCorentin Labbe 	}
4417513547SCorentin Labbe 
4517513547SCorentin Labbe 	spin_lock_irqsave(&ss->slock, flags);
4617513547SCorentin Labbe 
4717513547SCorentin Labbe 	for (i = 0; i < op->keylen; i += 4)
4817513547SCorentin Labbe 		writel(*(op->key + i / 4), ss->base + SS_KEY0 + i);
4917513547SCorentin Labbe 
5017513547SCorentin Labbe 	if (areq->iv) {
5117513547SCorentin Labbe 		for (i = 0; i < 4 && i < ivsize / 4; i++) {
5217513547SCorentin Labbe 			v = *(u32 *)(areq->iv + i * 4);
5317513547SCorentin Labbe 			writel(v, ss->base + SS_IV0 + i * 4);
5417513547SCorentin Labbe 		}
5517513547SCorentin Labbe 	}
5617513547SCorentin Labbe 	writel(mode, ss->base + SS_CTL);
5717513547SCorentin Labbe 
5817513547SCorentin Labbe 	sg_miter_start(&mi, areq->src, sg_nents(areq->src),
5917513547SCorentin Labbe 		       SG_MITER_FROM_SG | SG_MITER_ATOMIC);
6017513547SCorentin Labbe 	sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
6117513547SCorentin Labbe 		       SG_MITER_TO_SG | SG_MITER_ATOMIC);
6217513547SCorentin Labbe 	sg_miter_next(&mi);
6317513547SCorentin Labbe 	sg_miter_next(&mo);
6417513547SCorentin Labbe 	if (!mi.addr || !mo.addr) {
6517513547SCorentin Labbe 		dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
6617513547SCorentin Labbe 		err = -EINVAL;
6717513547SCorentin Labbe 		goto release_ss;
6817513547SCorentin Labbe 	}
6917513547SCorentin Labbe 
7017513547SCorentin Labbe 	ileft = areq->cryptlen / 4;
7117513547SCorentin Labbe 	oleft = areq->cryptlen / 4;
7217513547SCorentin Labbe 	oi = 0;
7317513547SCorentin Labbe 	oo = 0;
7417513547SCorentin Labbe 	do {
75*d6e9da21SHerbert Xu 		todo = min(rx_cnt, ileft);
76*d6e9da21SHerbert Xu 		todo = min_t(size_t, todo, (mi.length - oi) / 4);
7717513547SCorentin Labbe 		if (todo) {
7817513547SCorentin Labbe 			ileft -= todo;
7917513547SCorentin Labbe 			writesl(ss->base + SS_RXFIFO, mi.addr + oi, todo);
8017513547SCorentin Labbe 			oi += todo * 4;
8117513547SCorentin Labbe 		}
8217513547SCorentin Labbe 		if (oi == mi.length) {
8317513547SCorentin Labbe 			sg_miter_next(&mi);
8417513547SCorentin Labbe 			oi = 0;
8517513547SCorentin Labbe 		}
8617513547SCorentin Labbe 
8717513547SCorentin Labbe 		spaces = readl(ss->base + SS_FCSR);
8817513547SCorentin Labbe 		rx_cnt = SS_RXFIFO_SPACES(spaces);
8917513547SCorentin Labbe 		tx_cnt = SS_TXFIFO_SPACES(spaces);
9017513547SCorentin Labbe 
91*d6e9da21SHerbert Xu 		todo = min(tx_cnt, oleft);
92*d6e9da21SHerbert Xu 		todo = min_t(size_t, todo, (mo.length - oo) / 4);
9317513547SCorentin Labbe 		if (todo) {
9417513547SCorentin Labbe 			oleft -= todo;
9517513547SCorentin Labbe 			readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);
9617513547SCorentin Labbe 			oo += todo * 4;
9717513547SCorentin Labbe 		}
9817513547SCorentin Labbe 		if (oo == mo.length) {
9917513547SCorentin Labbe 			sg_miter_next(&mo);
10017513547SCorentin Labbe 			oo = 0;
10117513547SCorentin Labbe 		}
10217513547SCorentin Labbe 	} while (oleft);
10317513547SCorentin Labbe 
10417513547SCorentin Labbe 	if (areq->iv) {
10517513547SCorentin Labbe 		for (i = 0; i < 4 && i < ivsize / 4; i++) {
10617513547SCorentin Labbe 			v = readl(ss->base + SS_IV0 + i * 4);
10717513547SCorentin Labbe 			*(u32 *)(areq->iv + i * 4) = v;
10817513547SCorentin Labbe 		}
10917513547SCorentin Labbe 	}
11017513547SCorentin Labbe 
11117513547SCorentin Labbe release_ss:
11217513547SCorentin Labbe 	sg_miter_stop(&mi);
11317513547SCorentin Labbe 	sg_miter_stop(&mo);
11417513547SCorentin Labbe 	writel(0, ss->base + SS_CTL);
11517513547SCorentin Labbe 	spin_unlock_irqrestore(&ss->slock, flags);
11617513547SCorentin Labbe 	return err;
11717513547SCorentin Labbe }
11817513547SCorentin Labbe 
11917513547SCorentin Labbe 
12017513547SCorentin Labbe static int noinline_for_stack sun4i_ss_cipher_poll_fallback(struct skcipher_request *areq)
12117513547SCorentin Labbe {
12217513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
12317513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
12417513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
12517513547SCorentin Labbe 	SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, op->fallback_tfm);
12617513547SCorentin Labbe 	int err;
12717513547SCorentin Labbe 
12817513547SCorentin Labbe 	skcipher_request_set_sync_tfm(subreq, op->fallback_tfm);
12917513547SCorentin Labbe 	skcipher_request_set_callback(subreq, areq->base.flags, NULL,
13017513547SCorentin Labbe 				      NULL);
13117513547SCorentin Labbe 	skcipher_request_set_crypt(subreq, areq->src, areq->dst,
13217513547SCorentin Labbe 				   areq->cryptlen, areq->iv);
13317513547SCorentin Labbe 	if (ctx->mode & SS_DECRYPTION)
13417513547SCorentin Labbe 		err = crypto_skcipher_decrypt(subreq);
13517513547SCorentin Labbe 	else
13617513547SCorentin Labbe 		err = crypto_skcipher_encrypt(subreq);
13717513547SCorentin Labbe 	skcipher_request_zero(subreq);
13817513547SCorentin Labbe 
13917513547SCorentin Labbe 	return err;
14017513547SCorentin Labbe }
14117513547SCorentin Labbe 
14217513547SCorentin Labbe /* Generic function that support SG with size not multiple of 4 */
14317513547SCorentin Labbe static int sun4i_ss_cipher_poll(struct skcipher_request *areq)
14417513547SCorentin Labbe {
14517513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
14617513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
14717513547SCorentin Labbe 	struct sun4i_ss_ctx *ss = op->ss;
14817513547SCorentin Labbe 	int no_chunk = 1;
14917513547SCorentin Labbe 	struct scatterlist *in_sg = areq->src;
15017513547SCorentin Labbe 	struct scatterlist *out_sg = areq->dst;
15117513547SCorentin Labbe 	unsigned int ivsize = crypto_skcipher_ivsize(tfm);
15217513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *ctx = skcipher_request_ctx(areq);
15317513547SCorentin Labbe 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
15417513547SCorentin Labbe 	struct sun4i_ss_alg_template *algt;
15517513547SCorentin Labbe 	u32 mode = ctx->mode;
15617513547SCorentin Labbe 	/* when activating SS, the default FIFO space is SS_RX_DEFAULT(32) */
15717513547SCorentin Labbe 	u32 rx_cnt = SS_RX_DEFAULT;
15817513547SCorentin Labbe 	u32 tx_cnt = 0;
15917513547SCorentin Labbe 	u32 v;
16017513547SCorentin Labbe 	u32 spaces;
16117513547SCorentin Labbe 	int err = 0;
16217513547SCorentin Labbe 	unsigned int i;
16317513547SCorentin Labbe 	unsigned int ileft = areq->cryptlen;
16417513547SCorentin Labbe 	unsigned int oleft = areq->cryptlen;
16517513547SCorentin Labbe 	unsigned int todo;
16617513547SCorentin Labbe 	struct sg_mapping_iter mi, mo;
16717513547SCorentin Labbe 	unsigned int oi, oo;	/* offset for in and out */
16817513547SCorentin Labbe 	unsigned int ob = 0;	/* offset in buf */
16917513547SCorentin Labbe 	unsigned int obo = 0;	/* offset in bufo*/
17017513547SCorentin Labbe 	unsigned int obl = 0;	/* length of data in bufo */
17117513547SCorentin Labbe 	unsigned long flags;
17217513547SCorentin Labbe 	bool need_fallback;
17317513547SCorentin Labbe 
17417513547SCorentin Labbe 	if (!areq->cryptlen)
17517513547SCorentin Labbe 		return 0;
17617513547SCorentin Labbe 
17717513547SCorentin Labbe 	if (!areq->src || !areq->dst) {
17817513547SCorentin Labbe 		dev_err_ratelimited(ss->dev, "ERROR: Some SGs are NULL\n");
17917513547SCorentin Labbe 		return -EINVAL;
18017513547SCorentin Labbe 	}
18117513547SCorentin Labbe 
18217513547SCorentin Labbe 	algt = container_of(alg, struct sun4i_ss_alg_template, alg.crypto);
18317513547SCorentin Labbe 	if (areq->cryptlen % algt->alg.crypto.base.cra_blocksize)
18417513547SCorentin Labbe 		need_fallback = true;
18517513547SCorentin Labbe 
18617513547SCorentin Labbe 	/*
18717513547SCorentin Labbe 	 * if we have only SGs with size multiple of 4,
18817513547SCorentin Labbe 	 * we can use the SS optimized function
18917513547SCorentin Labbe 	 */
19017513547SCorentin Labbe 	while (in_sg && no_chunk == 1) {
19117513547SCorentin Labbe 		if (in_sg->length % 4)
19217513547SCorentin Labbe 			no_chunk = 0;
19317513547SCorentin Labbe 		in_sg = sg_next(in_sg);
19417513547SCorentin Labbe 	}
19517513547SCorentin Labbe 	while (out_sg && no_chunk == 1) {
19617513547SCorentin Labbe 		if (out_sg->length % 4)
19717513547SCorentin Labbe 			no_chunk = 0;
19817513547SCorentin Labbe 		out_sg = sg_next(out_sg);
19917513547SCorentin Labbe 	}
20017513547SCorentin Labbe 
20117513547SCorentin Labbe 	if (no_chunk == 1 && !need_fallback)
20217513547SCorentin Labbe 		return sun4i_ss_opti_poll(areq);
20317513547SCorentin Labbe 
20417513547SCorentin Labbe 	if (need_fallback)
20517513547SCorentin Labbe 		return sun4i_ss_cipher_poll_fallback(areq);
20617513547SCorentin Labbe 
20717513547SCorentin Labbe 	spin_lock_irqsave(&ss->slock, flags);
20817513547SCorentin Labbe 
20917513547SCorentin Labbe 	for (i = 0; i < op->keylen; i += 4)
21017513547SCorentin Labbe 		writel(*(op->key + i / 4), ss->base + SS_KEY0 + i);
21117513547SCorentin Labbe 
21217513547SCorentin Labbe 	if (areq->iv) {
21317513547SCorentin Labbe 		for (i = 0; i < 4 && i < ivsize / 4; i++) {
21417513547SCorentin Labbe 			v = *(u32 *)(areq->iv + i * 4);
21517513547SCorentin Labbe 			writel(v, ss->base + SS_IV0 + i * 4);
21617513547SCorentin Labbe 		}
21717513547SCorentin Labbe 	}
21817513547SCorentin Labbe 	writel(mode, ss->base + SS_CTL);
21917513547SCorentin Labbe 
22017513547SCorentin Labbe 	sg_miter_start(&mi, areq->src, sg_nents(areq->src),
22117513547SCorentin Labbe 		       SG_MITER_FROM_SG | SG_MITER_ATOMIC);
22217513547SCorentin Labbe 	sg_miter_start(&mo, areq->dst, sg_nents(areq->dst),
22317513547SCorentin Labbe 		       SG_MITER_TO_SG | SG_MITER_ATOMIC);
22417513547SCorentin Labbe 	sg_miter_next(&mi);
22517513547SCorentin Labbe 	sg_miter_next(&mo);
22617513547SCorentin Labbe 	if (!mi.addr || !mo.addr) {
22717513547SCorentin Labbe 		dev_err_ratelimited(ss->dev, "ERROR: sg_miter return null\n");
22817513547SCorentin Labbe 		err = -EINVAL;
22917513547SCorentin Labbe 		goto release_ss;
23017513547SCorentin Labbe 	}
23117513547SCorentin Labbe 	ileft = areq->cryptlen;
23217513547SCorentin Labbe 	oleft = areq->cryptlen;
23317513547SCorentin Labbe 	oi = 0;
23417513547SCorentin Labbe 	oo = 0;
23517513547SCorentin Labbe 
23617513547SCorentin Labbe 	while (oleft) {
23717513547SCorentin Labbe 		if (ileft) {
23817513547SCorentin Labbe 			char buf[4 * SS_RX_MAX];/* buffer for linearize SG src */
23917513547SCorentin Labbe 
24017513547SCorentin Labbe 			/*
24117513547SCorentin Labbe 			 * todo is the number of consecutive 4byte word that we
24217513547SCorentin Labbe 			 * can read from current SG
24317513547SCorentin Labbe 			 */
244*d6e9da21SHerbert Xu 			todo = min(rx_cnt, ileft / 4);
245*d6e9da21SHerbert Xu 			todo = min_t(size_t, todo, (mi.length - oi) / 4);
24617513547SCorentin Labbe 			if (todo && !ob) {
24717513547SCorentin Labbe 				writesl(ss->base + SS_RXFIFO, mi.addr + oi,
24817513547SCorentin Labbe 					todo);
24917513547SCorentin Labbe 				ileft -= todo * 4;
25017513547SCorentin Labbe 				oi += todo * 4;
25117513547SCorentin Labbe 			} else {
25217513547SCorentin Labbe 				/*
25317513547SCorentin Labbe 				 * not enough consecutive bytes, so we need to
25417513547SCorentin Labbe 				 * linearize in buf. todo is in bytes
25517513547SCorentin Labbe 				 * After that copy, if we have a multiple of 4
25617513547SCorentin Labbe 				 * we need to be able to write all buf in one
25717513547SCorentin Labbe 				 * pass, so it is why we min() with rx_cnt
25817513547SCorentin Labbe 				 */
259*d6e9da21SHerbert Xu 				todo = min(rx_cnt * 4 - ob, ileft);
260*d6e9da21SHerbert Xu 				todo = min_t(size_t, todo, mi.length - oi);
26117513547SCorentin Labbe 				memcpy(buf + ob, mi.addr + oi, todo);
26217513547SCorentin Labbe 				ileft -= todo;
26317513547SCorentin Labbe 				oi += todo;
26417513547SCorentin Labbe 				ob += todo;
26517513547SCorentin Labbe 				if (!(ob % 4)) {
26617513547SCorentin Labbe 					writesl(ss->base + SS_RXFIFO, buf,
26717513547SCorentin Labbe 						ob / 4);
26817513547SCorentin Labbe 					ob = 0;
26917513547SCorentin Labbe 				}
27017513547SCorentin Labbe 			}
27117513547SCorentin Labbe 			if (oi == mi.length) {
27217513547SCorentin Labbe 				sg_miter_next(&mi);
27317513547SCorentin Labbe 				oi = 0;
27417513547SCorentin Labbe 			}
27517513547SCorentin Labbe 		}
27617513547SCorentin Labbe 
27717513547SCorentin Labbe 		spaces = readl(ss->base + SS_FCSR);
27817513547SCorentin Labbe 		rx_cnt = SS_RXFIFO_SPACES(spaces);
27917513547SCorentin Labbe 		tx_cnt = SS_TXFIFO_SPACES(spaces);
280*d6e9da21SHerbert Xu 		dev_dbg(ss->dev,
281*d6e9da21SHerbert Xu 			"%x %u/%zu %u/%u cnt=%u %u/%zu %u/%u cnt=%u %u\n",
28217513547SCorentin Labbe 			mode,
28317513547SCorentin Labbe 			oi, mi.length, ileft, areq->cryptlen, rx_cnt,
28417513547SCorentin Labbe 			oo, mo.length, oleft, areq->cryptlen, tx_cnt, ob);
28517513547SCorentin Labbe 
28617513547SCorentin Labbe 		if (!tx_cnt)
28717513547SCorentin Labbe 			continue;
28817513547SCorentin Labbe 		/* todo in 4bytes word */
289*d6e9da21SHerbert Xu 		todo = min(tx_cnt, oleft / 4);
290*d6e9da21SHerbert Xu 		todo = min_t(size_t, todo, (mo.length - oo) / 4);
29117513547SCorentin Labbe 		if (todo) {
29217513547SCorentin Labbe 			readsl(ss->base + SS_TXFIFO, mo.addr + oo, todo);
29317513547SCorentin Labbe 			oleft -= todo * 4;
29417513547SCorentin Labbe 			oo += todo * 4;
29517513547SCorentin Labbe 			if (oo == mo.length) {
29617513547SCorentin Labbe 				sg_miter_next(&mo);
29717513547SCorentin Labbe 				oo = 0;
29817513547SCorentin Labbe 			}
29917513547SCorentin Labbe 		} else {
30017513547SCorentin Labbe 			char bufo[4 * SS_TX_MAX]; /* buffer for linearize SG dst */
30117513547SCorentin Labbe 
30217513547SCorentin Labbe 			/*
30317513547SCorentin Labbe 			 * read obl bytes in bufo, we read at maximum for
30417513547SCorentin Labbe 			 * emptying the device
30517513547SCorentin Labbe 			 */
30617513547SCorentin Labbe 			readsl(ss->base + SS_TXFIFO, bufo, tx_cnt);
30717513547SCorentin Labbe 			obl = tx_cnt * 4;
30817513547SCorentin Labbe 			obo = 0;
30917513547SCorentin Labbe 			do {
31017513547SCorentin Labbe 				/*
31117513547SCorentin Labbe 				 * how many bytes we can copy ?
31217513547SCorentin Labbe 				 * no more than remaining SG size
31317513547SCorentin Labbe 				 * no more than remaining buffer
31417513547SCorentin Labbe 				 * no need to test against oleft
31517513547SCorentin Labbe 				 */
316*d6e9da21SHerbert Xu 				todo = min_t(size_t,
317*d6e9da21SHerbert Xu 					     mo.length - oo, obl - obo);
31817513547SCorentin Labbe 				memcpy(mo.addr + oo, bufo + obo, todo);
31917513547SCorentin Labbe 				oleft -= todo;
32017513547SCorentin Labbe 				obo += todo;
32117513547SCorentin Labbe 				oo += todo;
32217513547SCorentin Labbe 				if (oo == mo.length) {
32317513547SCorentin Labbe 					sg_miter_next(&mo);
32417513547SCorentin Labbe 					oo = 0;
32517513547SCorentin Labbe 				}
32617513547SCorentin Labbe 			} while (obo < obl);
32717513547SCorentin Labbe 			/* bufo must be fully used here */
32817513547SCorentin Labbe 		}
32917513547SCorentin Labbe 	}
33017513547SCorentin Labbe 	if (areq->iv) {
33117513547SCorentin Labbe 		for (i = 0; i < 4 && i < ivsize / 4; i++) {
33217513547SCorentin Labbe 			v = readl(ss->base + SS_IV0 + i * 4);
33317513547SCorentin Labbe 			*(u32 *)(areq->iv + i * 4) = v;
33417513547SCorentin Labbe 		}
33517513547SCorentin Labbe 	}
33617513547SCorentin Labbe 
33717513547SCorentin Labbe release_ss:
33817513547SCorentin Labbe 	sg_miter_stop(&mi);
33917513547SCorentin Labbe 	sg_miter_stop(&mo);
34017513547SCorentin Labbe 	writel(0, ss->base + SS_CTL);
34117513547SCorentin Labbe 	spin_unlock_irqrestore(&ss->slock, flags);
34217513547SCorentin Labbe 
34317513547SCorentin Labbe 	return err;
34417513547SCorentin Labbe }
34517513547SCorentin Labbe 
34617513547SCorentin Labbe /* CBC AES */
34717513547SCorentin Labbe int sun4i_ss_cbc_aes_encrypt(struct skcipher_request *areq)
34817513547SCorentin Labbe {
34917513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
35017513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
35117513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
35217513547SCorentin Labbe 
35317513547SCorentin Labbe 	rctx->mode = SS_OP_AES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
35417513547SCorentin Labbe 		op->keymode;
35517513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
35617513547SCorentin Labbe }
35717513547SCorentin Labbe 
35817513547SCorentin Labbe int sun4i_ss_cbc_aes_decrypt(struct skcipher_request *areq)
35917513547SCorentin Labbe {
36017513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
36117513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
36217513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
36317513547SCorentin Labbe 
36417513547SCorentin Labbe 	rctx->mode = SS_OP_AES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
36517513547SCorentin Labbe 		op->keymode;
36617513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
36717513547SCorentin Labbe }
36817513547SCorentin Labbe 
36917513547SCorentin Labbe /* ECB AES */
37017513547SCorentin Labbe int sun4i_ss_ecb_aes_encrypt(struct skcipher_request *areq)
37117513547SCorentin Labbe {
37217513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
37317513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
37417513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
37517513547SCorentin Labbe 
37617513547SCorentin Labbe 	rctx->mode = SS_OP_AES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
37717513547SCorentin Labbe 		op->keymode;
37817513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
37917513547SCorentin Labbe }
38017513547SCorentin Labbe 
38117513547SCorentin Labbe int sun4i_ss_ecb_aes_decrypt(struct skcipher_request *areq)
38217513547SCorentin Labbe {
38317513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
38417513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
38517513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
38617513547SCorentin Labbe 
38717513547SCorentin Labbe 	rctx->mode = SS_OP_AES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
38817513547SCorentin Labbe 		op->keymode;
38917513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
39017513547SCorentin Labbe }
39117513547SCorentin Labbe 
39217513547SCorentin Labbe /* CBC DES */
39317513547SCorentin Labbe int sun4i_ss_cbc_des_encrypt(struct skcipher_request *areq)
39417513547SCorentin Labbe {
39517513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
39617513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
39717513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
39817513547SCorentin Labbe 
39917513547SCorentin Labbe 	rctx->mode = SS_OP_DES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
40017513547SCorentin Labbe 		op->keymode;
40117513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
40217513547SCorentin Labbe }
40317513547SCorentin Labbe 
40417513547SCorentin Labbe int sun4i_ss_cbc_des_decrypt(struct skcipher_request *areq)
40517513547SCorentin Labbe {
40617513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
40717513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
40817513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
40917513547SCorentin Labbe 
41017513547SCorentin Labbe 	rctx->mode = SS_OP_DES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
41117513547SCorentin Labbe 		op->keymode;
41217513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
41317513547SCorentin Labbe }
41417513547SCorentin Labbe 
41517513547SCorentin Labbe /* ECB DES */
41617513547SCorentin Labbe int sun4i_ss_ecb_des_encrypt(struct skcipher_request *areq)
41717513547SCorentin Labbe {
41817513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
41917513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
42017513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
42117513547SCorentin Labbe 
42217513547SCorentin Labbe 	rctx->mode = SS_OP_DES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
42317513547SCorentin Labbe 		op->keymode;
42417513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
42517513547SCorentin Labbe }
42617513547SCorentin Labbe 
42717513547SCorentin Labbe int sun4i_ss_ecb_des_decrypt(struct skcipher_request *areq)
42817513547SCorentin Labbe {
42917513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
43017513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
43117513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
43217513547SCorentin Labbe 
43317513547SCorentin Labbe 	rctx->mode = SS_OP_DES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
43417513547SCorentin Labbe 		op->keymode;
43517513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
43617513547SCorentin Labbe }
43717513547SCorentin Labbe 
43817513547SCorentin Labbe /* CBC 3DES */
43917513547SCorentin Labbe int sun4i_ss_cbc_des3_encrypt(struct skcipher_request *areq)
44017513547SCorentin Labbe {
44117513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
44217513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
44317513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
44417513547SCorentin Labbe 
44517513547SCorentin Labbe 	rctx->mode = SS_OP_3DES | SS_CBC | SS_ENABLED | SS_ENCRYPTION |
44617513547SCorentin Labbe 		op->keymode;
44717513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
44817513547SCorentin Labbe }
44917513547SCorentin Labbe 
45017513547SCorentin Labbe int sun4i_ss_cbc_des3_decrypt(struct skcipher_request *areq)
45117513547SCorentin Labbe {
45217513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
45317513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
45417513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
45517513547SCorentin Labbe 
45617513547SCorentin Labbe 	rctx->mode = SS_OP_3DES | SS_CBC | SS_ENABLED | SS_DECRYPTION |
45717513547SCorentin Labbe 		op->keymode;
45817513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
45917513547SCorentin Labbe }
46017513547SCorentin Labbe 
46117513547SCorentin Labbe /* ECB 3DES */
46217513547SCorentin Labbe int sun4i_ss_ecb_des3_encrypt(struct skcipher_request *areq)
46317513547SCorentin Labbe {
46417513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
46517513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
46617513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
46717513547SCorentin Labbe 
46817513547SCorentin Labbe 	rctx->mode = SS_OP_3DES | SS_ECB | SS_ENABLED | SS_ENCRYPTION |
46917513547SCorentin Labbe 		op->keymode;
47017513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
47117513547SCorentin Labbe }
47217513547SCorentin Labbe 
47317513547SCorentin Labbe int sun4i_ss_ecb_des3_decrypt(struct skcipher_request *areq)
47417513547SCorentin Labbe {
47517513547SCorentin Labbe 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
47617513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
47717513547SCorentin Labbe 	struct sun4i_cipher_req_ctx *rctx = skcipher_request_ctx(areq);
47817513547SCorentin Labbe 
47917513547SCorentin Labbe 	rctx->mode = SS_OP_3DES | SS_ECB | SS_ENABLED | SS_DECRYPTION |
48017513547SCorentin Labbe 		op->keymode;
48117513547SCorentin Labbe 	return sun4i_ss_cipher_poll(areq);
48217513547SCorentin Labbe }
48317513547SCorentin Labbe 
48417513547SCorentin Labbe int sun4i_ss_cipher_init(struct crypto_tfm *tfm)
48517513547SCorentin Labbe {
48617513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
48717513547SCorentin Labbe 	struct sun4i_ss_alg_template *algt;
48817513547SCorentin Labbe 	const char *name = crypto_tfm_alg_name(tfm);
48917513547SCorentin Labbe 	int err;
49017513547SCorentin Labbe 
49117513547SCorentin Labbe 	memset(op, 0, sizeof(struct sun4i_tfm_ctx));
49217513547SCorentin Labbe 
49317513547SCorentin Labbe 	algt = container_of(tfm->__crt_alg, struct sun4i_ss_alg_template,
49417513547SCorentin Labbe 			    alg.crypto.base);
49517513547SCorentin Labbe 	op->ss = algt->ss;
49617513547SCorentin Labbe 
49717513547SCorentin Labbe 	crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
49817513547SCorentin Labbe 				    sizeof(struct sun4i_cipher_req_ctx));
49917513547SCorentin Labbe 
50017513547SCorentin Labbe 	op->fallback_tfm = crypto_alloc_sync_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
50117513547SCorentin Labbe 	if (IS_ERR(op->fallback_tfm)) {
50217513547SCorentin Labbe 		dev_err(op->ss->dev, "ERROR: Cannot allocate fallback for %s %ld\n",
50317513547SCorentin Labbe 			name, PTR_ERR(op->fallback_tfm));
50417513547SCorentin Labbe 		return PTR_ERR(op->fallback_tfm);
50517513547SCorentin Labbe 	}
50617513547SCorentin Labbe 
50717513547SCorentin Labbe 	err = pm_runtime_get_sync(op->ss->dev);
50817513547SCorentin Labbe 	if (err < 0)
50917513547SCorentin Labbe 		goto error_pm;
51017513547SCorentin Labbe 
51117513547SCorentin Labbe 	return 0;
51217513547SCorentin Labbe error_pm:
51317513547SCorentin Labbe 	crypto_free_sync_skcipher(op->fallback_tfm);
51417513547SCorentin Labbe 	return err;
51517513547SCorentin Labbe }
51617513547SCorentin Labbe 
51717513547SCorentin Labbe void sun4i_ss_cipher_exit(struct crypto_tfm *tfm)
51817513547SCorentin Labbe {
51917513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
52017513547SCorentin Labbe 
52117513547SCorentin Labbe 	crypto_free_sync_skcipher(op->fallback_tfm);
52217513547SCorentin Labbe 	pm_runtime_put(op->ss->dev);
52317513547SCorentin Labbe }
52417513547SCorentin Labbe 
52517513547SCorentin Labbe /* check and set the AES key, prepare the mode to be used */
52617513547SCorentin Labbe int sun4i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
52717513547SCorentin Labbe 			unsigned int keylen)
52817513547SCorentin Labbe {
52917513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
53017513547SCorentin Labbe 	struct sun4i_ss_ctx *ss = op->ss;
53117513547SCorentin Labbe 
53217513547SCorentin Labbe 	switch (keylen) {
53317513547SCorentin Labbe 	case 128 / 8:
53417513547SCorentin Labbe 		op->keymode = SS_AES_128BITS;
53517513547SCorentin Labbe 		break;
53617513547SCorentin Labbe 	case 192 / 8:
53717513547SCorentin Labbe 		op->keymode = SS_AES_192BITS;
53817513547SCorentin Labbe 		break;
53917513547SCorentin Labbe 	case 256 / 8:
54017513547SCorentin Labbe 		op->keymode = SS_AES_256BITS;
54117513547SCorentin Labbe 		break;
54217513547SCorentin Labbe 	default:
54317513547SCorentin Labbe 		dev_err(ss->dev, "ERROR: Invalid keylen %u\n", keylen);
54417513547SCorentin Labbe 		crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
54517513547SCorentin Labbe 		return -EINVAL;
54617513547SCorentin Labbe 	}
54717513547SCorentin Labbe 	op->keylen = keylen;
54817513547SCorentin Labbe 	memcpy(op->key, key, keylen);
54917513547SCorentin Labbe 
55017513547SCorentin Labbe 	crypto_sync_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
55117513547SCorentin Labbe 	crypto_sync_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
55217513547SCorentin Labbe 
55317513547SCorentin Labbe 	return crypto_sync_skcipher_setkey(op->fallback_tfm, key, keylen);
55417513547SCorentin Labbe }
55517513547SCorentin Labbe 
55617513547SCorentin Labbe /* check and set the DES key, prepare the mode to be used */
55717513547SCorentin Labbe int sun4i_ss_des_setkey(struct crypto_skcipher *tfm, const u8 *key,
55817513547SCorentin Labbe 			unsigned int keylen)
55917513547SCorentin Labbe {
56017513547SCorentin Labbe 	struct sun4i_tfm_ctx *op = crypto_skcipher_ctx(tfm);
56117513547SCorentin Labbe 	int err;
56217513547SCorentin Labbe 
56317513547SCorentin Labbe 	err = verify_skcipher_des_key(tfm, key);
56417513547SCorentin Labbe 	if (err)
56517513547SCorentin Labbe 		return err;
56617513547SCorentin Labbe 
56717513547SCorentin Labbe 	op->keylen = keylen;
56817513547SCorentin Labbe 	memcpy(op->key, key, keylen);
56917513547SCorentin Labbe 
57017513547SCorentin Labbe 	crypto_sync_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
57117513547SCorentin Labbe 	crypto_sync_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
57217513547SCorentin Labbe 
57317513547SCorentin Labbe 	return crypto_sync_skcipher_setkey(op->fallback_tfm, key, keylen);
57417513547SCorentin Labbe }
57517513547SCorentin Labbe 
57617513547SCorentin Labbe /* check and set the 3DES key, prepare the mode to be used */
57717513547SCorentin Labbe int sun4i_ss_des3_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 	int err;
58217513547SCorentin Labbe 
58317513547SCorentin Labbe 	err = verify_skcipher_des3_key(tfm, key);
58417513547SCorentin Labbe 	if (err)
58517513547SCorentin Labbe 		return err;
58617513547SCorentin Labbe 
58717513547SCorentin Labbe 	op->keylen = keylen;
58817513547SCorentin Labbe 	memcpy(op->key, key, keylen);
58917513547SCorentin Labbe 
59017513547SCorentin Labbe 	crypto_sync_skcipher_clear_flags(op->fallback_tfm, CRYPTO_TFM_REQ_MASK);
59117513547SCorentin Labbe 	crypto_sync_skcipher_set_flags(op->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
59217513547SCorentin Labbe 
59317513547SCorentin Labbe 	return crypto_sync_skcipher_setkey(op->fallback_tfm, key, keylen);
59417513547SCorentin Labbe 
59517513547SCorentin Labbe }
596