xref: /OK3568_Linux_fs/kernel/drivers/crypto/qce/common.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/err.h>
7*4882a593Smuzhiyun #include <linux/interrupt.h>
8*4882a593Smuzhiyun #include <linux/types.h>
9*4882a593Smuzhiyun #include <crypto/scatterwalk.h>
10*4882a593Smuzhiyun #include <crypto/sha.h>
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include "cipher.h"
13*4882a593Smuzhiyun #include "common.h"
14*4882a593Smuzhiyun #include "core.h"
15*4882a593Smuzhiyun #include "regs-v5.h"
16*4882a593Smuzhiyun #include "sha.h"
17*4882a593Smuzhiyun 
qce_read(struct qce_device * qce,u32 offset)18*4882a593Smuzhiyun static inline u32 qce_read(struct qce_device *qce, u32 offset)
19*4882a593Smuzhiyun {
20*4882a593Smuzhiyun 	return readl(qce->base + offset);
21*4882a593Smuzhiyun }
22*4882a593Smuzhiyun 
qce_write(struct qce_device * qce,u32 offset,u32 val)23*4882a593Smuzhiyun static inline void qce_write(struct qce_device *qce, u32 offset, u32 val)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun 	writel(val, qce->base + offset);
26*4882a593Smuzhiyun }
27*4882a593Smuzhiyun 
qce_write_array(struct qce_device * qce,u32 offset,const u32 * val,unsigned int len)28*4882a593Smuzhiyun static inline void qce_write_array(struct qce_device *qce, u32 offset,
29*4882a593Smuzhiyun 				   const u32 *val, unsigned int len)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun 	int i;
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	for (i = 0; i < len; i++)
34*4882a593Smuzhiyun 		qce_write(qce, offset + i * sizeof(u32), val[i]);
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun static inline void
qce_clear_array(struct qce_device * qce,u32 offset,unsigned int len)38*4882a593Smuzhiyun qce_clear_array(struct qce_device *qce, u32 offset, unsigned int len)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	int i;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	for (i = 0; i < len; i++)
43*4882a593Smuzhiyun 		qce_write(qce, offset + i * sizeof(u32), 0);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun 
qce_config_reg(struct qce_device * qce,int little)46*4882a593Smuzhiyun static u32 qce_config_reg(struct qce_device *qce, int little)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	u32 beats = (qce->burst_size >> 3) - 1;
49*4882a593Smuzhiyun 	u32 pipe_pair = qce->pipe_pair_id;
50*4882a593Smuzhiyun 	u32 config;
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	config = (beats << REQ_SIZE_SHIFT) & REQ_SIZE_MASK;
53*4882a593Smuzhiyun 	config |= BIT(MASK_DOUT_INTR_SHIFT) | BIT(MASK_DIN_INTR_SHIFT) |
54*4882a593Smuzhiyun 		  BIT(MASK_OP_DONE_INTR_SHIFT) | BIT(MASK_ERR_INTR_SHIFT);
55*4882a593Smuzhiyun 	config |= (pipe_pair << PIPE_SET_SELECT_SHIFT) & PIPE_SET_SELECT_MASK;
56*4882a593Smuzhiyun 	config &= ~HIGH_SPD_EN_N_SHIFT;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	if (little)
59*4882a593Smuzhiyun 		config |= BIT(LITTLE_ENDIAN_MODE_SHIFT);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	return config;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun 
qce_cpu_to_be32p_array(__be32 * dst,const u8 * src,unsigned int len)64*4882a593Smuzhiyun void qce_cpu_to_be32p_array(__be32 *dst, const u8 *src, unsigned int len)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	__be32 *d = dst;
67*4882a593Smuzhiyun 	const u8 *s = src;
68*4882a593Smuzhiyun 	unsigned int n;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	n = len / sizeof(u32);
71*4882a593Smuzhiyun 	for (; n > 0; n--) {
72*4882a593Smuzhiyun 		*d = cpu_to_be32p((const __u32 *) s);
73*4882a593Smuzhiyun 		s += sizeof(__u32);
74*4882a593Smuzhiyun 		d++;
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
qce_setup_config(struct qce_device * qce)78*4882a593Smuzhiyun static void qce_setup_config(struct qce_device *qce)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	u32 config;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	/* get big endianness */
83*4882a593Smuzhiyun 	config = qce_config_reg(qce, 0);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	/* clear status */
86*4882a593Smuzhiyun 	qce_write(qce, REG_STATUS, 0);
87*4882a593Smuzhiyun 	qce_write(qce, REG_CONFIG, config);
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
qce_crypto_go(struct qce_device * qce)90*4882a593Smuzhiyun static inline void qce_crypto_go(struct qce_device *qce)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	qce_write(qce, REG_GOPROC, BIT(GO_SHIFT) | BIT(RESULTS_DUMP_SHIFT));
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DEV_QCE_SHA
qce_auth_cfg(unsigned long flags,u32 key_size)96*4882a593Smuzhiyun static u32 qce_auth_cfg(unsigned long flags, u32 key_size)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	u32 cfg = 0;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	if (IS_AES(flags) && (IS_CCM(flags) || IS_CMAC(flags)))
101*4882a593Smuzhiyun 		cfg |= AUTH_ALG_AES << AUTH_ALG_SHIFT;
102*4882a593Smuzhiyun 	else
103*4882a593Smuzhiyun 		cfg |= AUTH_ALG_SHA << AUTH_ALG_SHIFT;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	if (IS_CCM(flags) || IS_CMAC(flags)) {
106*4882a593Smuzhiyun 		if (key_size == AES_KEYSIZE_128)
107*4882a593Smuzhiyun 			cfg |= AUTH_KEY_SZ_AES128 << AUTH_KEY_SIZE_SHIFT;
108*4882a593Smuzhiyun 		else if (key_size == AES_KEYSIZE_256)
109*4882a593Smuzhiyun 			cfg |= AUTH_KEY_SZ_AES256 << AUTH_KEY_SIZE_SHIFT;
110*4882a593Smuzhiyun 	}
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	if (IS_SHA1(flags) || IS_SHA1_HMAC(flags))
113*4882a593Smuzhiyun 		cfg |= AUTH_SIZE_SHA1 << AUTH_SIZE_SHIFT;
114*4882a593Smuzhiyun 	else if (IS_SHA256(flags) || IS_SHA256_HMAC(flags))
115*4882a593Smuzhiyun 		cfg |= AUTH_SIZE_SHA256 << AUTH_SIZE_SHIFT;
116*4882a593Smuzhiyun 	else if (IS_CMAC(flags))
117*4882a593Smuzhiyun 		cfg |= AUTH_SIZE_ENUM_16_BYTES << AUTH_SIZE_SHIFT;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	if (IS_SHA1(flags) || IS_SHA256(flags))
120*4882a593Smuzhiyun 		cfg |= AUTH_MODE_HASH << AUTH_MODE_SHIFT;
121*4882a593Smuzhiyun 	else if (IS_SHA1_HMAC(flags) || IS_SHA256_HMAC(flags) ||
122*4882a593Smuzhiyun 		 IS_CBC(flags) || IS_CTR(flags))
123*4882a593Smuzhiyun 		cfg |= AUTH_MODE_HMAC << AUTH_MODE_SHIFT;
124*4882a593Smuzhiyun 	else if (IS_AES(flags) && IS_CCM(flags))
125*4882a593Smuzhiyun 		cfg |= AUTH_MODE_CCM << AUTH_MODE_SHIFT;
126*4882a593Smuzhiyun 	else if (IS_AES(flags) && IS_CMAC(flags))
127*4882a593Smuzhiyun 		cfg |= AUTH_MODE_CMAC << AUTH_MODE_SHIFT;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	if (IS_SHA(flags) || IS_SHA_HMAC(flags))
130*4882a593Smuzhiyun 		cfg |= AUTH_POS_BEFORE << AUTH_POS_SHIFT;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	if (IS_CCM(flags))
133*4882a593Smuzhiyun 		cfg |= QCE_MAX_NONCE_WORDS << AUTH_NONCE_NUM_WORDS_SHIFT;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	if (IS_CBC(flags) || IS_CTR(flags) || IS_CCM(flags) ||
136*4882a593Smuzhiyun 	    IS_CMAC(flags))
137*4882a593Smuzhiyun 		cfg |= BIT(AUTH_LAST_SHIFT) | BIT(AUTH_FIRST_SHIFT);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	return cfg;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
qce_setup_regs_ahash(struct crypto_async_request * async_req,u32 totallen,u32 offset)142*4882a593Smuzhiyun static int qce_setup_regs_ahash(struct crypto_async_request *async_req,
143*4882a593Smuzhiyun 				u32 totallen, u32 offset)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	struct ahash_request *req = ahash_request_cast(async_req);
146*4882a593Smuzhiyun 	struct crypto_ahash *ahash = __crypto_ahash_cast(async_req->tfm);
147*4882a593Smuzhiyun 	struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
148*4882a593Smuzhiyun 	struct qce_alg_template *tmpl = to_ahash_tmpl(async_req->tfm);
149*4882a593Smuzhiyun 	struct qce_device *qce = tmpl->qce;
150*4882a593Smuzhiyun 	unsigned int digestsize = crypto_ahash_digestsize(ahash);
151*4882a593Smuzhiyun 	unsigned int blocksize = crypto_tfm_alg_blocksize(async_req->tfm);
152*4882a593Smuzhiyun 	__be32 auth[SHA256_DIGEST_SIZE / sizeof(__be32)] = {0};
153*4882a593Smuzhiyun 	__be32 mackey[QCE_SHA_HMAC_KEY_SIZE / sizeof(__be32)] = {0};
154*4882a593Smuzhiyun 	u32 auth_cfg = 0, config;
155*4882a593Smuzhiyun 	unsigned int iv_words;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	/* if not the last, the size has to be on the block boundary */
158*4882a593Smuzhiyun 	if (!rctx->last_blk && req->nbytes % blocksize)
159*4882a593Smuzhiyun 		return -EINVAL;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	qce_setup_config(qce);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	if (IS_CMAC(rctx->flags)) {
164*4882a593Smuzhiyun 		qce_write(qce, REG_AUTH_SEG_CFG, 0);
165*4882a593Smuzhiyun 		qce_write(qce, REG_ENCR_SEG_CFG, 0);
166*4882a593Smuzhiyun 		qce_write(qce, REG_ENCR_SEG_SIZE, 0);
167*4882a593Smuzhiyun 		qce_clear_array(qce, REG_AUTH_IV0, 16);
168*4882a593Smuzhiyun 		qce_clear_array(qce, REG_AUTH_KEY0, 16);
169*4882a593Smuzhiyun 		qce_clear_array(qce, REG_AUTH_BYTECNT0, 4);
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 		auth_cfg = qce_auth_cfg(rctx->flags, rctx->authklen);
172*4882a593Smuzhiyun 	}
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	if (IS_SHA_HMAC(rctx->flags) || IS_CMAC(rctx->flags)) {
175*4882a593Smuzhiyun 		u32 authkey_words = rctx->authklen / sizeof(u32);
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 		qce_cpu_to_be32p_array(mackey, rctx->authkey, rctx->authklen);
178*4882a593Smuzhiyun 		qce_write_array(qce, REG_AUTH_KEY0, (u32 *)mackey,
179*4882a593Smuzhiyun 				authkey_words);
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	if (IS_CMAC(rctx->flags))
183*4882a593Smuzhiyun 		goto go_proc;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	if (rctx->first_blk)
186*4882a593Smuzhiyun 		memcpy(auth, rctx->digest, digestsize);
187*4882a593Smuzhiyun 	else
188*4882a593Smuzhiyun 		qce_cpu_to_be32p_array(auth, rctx->digest, digestsize);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	iv_words = (IS_SHA1(rctx->flags) || IS_SHA1_HMAC(rctx->flags)) ? 5 : 8;
191*4882a593Smuzhiyun 	qce_write_array(qce, REG_AUTH_IV0, (u32 *)auth, iv_words);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	if (rctx->first_blk)
194*4882a593Smuzhiyun 		qce_clear_array(qce, REG_AUTH_BYTECNT0, 4);
195*4882a593Smuzhiyun 	else
196*4882a593Smuzhiyun 		qce_write_array(qce, REG_AUTH_BYTECNT0,
197*4882a593Smuzhiyun 				(u32 *)rctx->byte_count, 2);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	auth_cfg = qce_auth_cfg(rctx->flags, 0);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	if (rctx->last_blk)
202*4882a593Smuzhiyun 		auth_cfg |= BIT(AUTH_LAST_SHIFT);
203*4882a593Smuzhiyun 	else
204*4882a593Smuzhiyun 		auth_cfg &= ~BIT(AUTH_LAST_SHIFT);
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	if (rctx->first_blk)
207*4882a593Smuzhiyun 		auth_cfg |= BIT(AUTH_FIRST_SHIFT);
208*4882a593Smuzhiyun 	else
209*4882a593Smuzhiyun 		auth_cfg &= ~BIT(AUTH_FIRST_SHIFT);
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun go_proc:
212*4882a593Smuzhiyun 	qce_write(qce, REG_AUTH_SEG_CFG, auth_cfg);
213*4882a593Smuzhiyun 	qce_write(qce, REG_AUTH_SEG_SIZE, req->nbytes);
214*4882a593Smuzhiyun 	qce_write(qce, REG_AUTH_SEG_START, 0);
215*4882a593Smuzhiyun 	qce_write(qce, REG_ENCR_SEG_CFG, 0);
216*4882a593Smuzhiyun 	qce_write(qce, REG_SEG_SIZE, req->nbytes);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	/* get little endianness */
219*4882a593Smuzhiyun 	config = qce_config_reg(qce, 1);
220*4882a593Smuzhiyun 	qce_write(qce, REG_CONFIG, config);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	qce_crypto_go(qce);
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	return 0;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun #endif
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DEV_QCE_SKCIPHER
qce_encr_cfg(unsigned long flags,u32 aes_key_size)229*4882a593Smuzhiyun static u32 qce_encr_cfg(unsigned long flags, u32 aes_key_size)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun 	u32 cfg = 0;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	if (IS_AES(flags)) {
234*4882a593Smuzhiyun 		if (aes_key_size == AES_KEYSIZE_128)
235*4882a593Smuzhiyun 			cfg |= ENCR_KEY_SZ_AES128 << ENCR_KEY_SZ_SHIFT;
236*4882a593Smuzhiyun 		else if (aes_key_size == AES_KEYSIZE_256)
237*4882a593Smuzhiyun 			cfg |= ENCR_KEY_SZ_AES256 << ENCR_KEY_SZ_SHIFT;
238*4882a593Smuzhiyun 	}
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	if (IS_AES(flags))
241*4882a593Smuzhiyun 		cfg |= ENCR_ALG_AES << ENCR_ALG_SHIFT;
242*4882a593Smuzhiyun 	else if (IS_DES(flags) || IS_3DES(flags))
243*4882a593Smuzhiyun 		cfg |= ENCR_ALG_DES << ENCR_ALG_SHIFT;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	if (IS_DES(flags))
246*4882a593Smuzhiyun 		cfg |= ENCR_KEY_SZ_DES << ENCR_KEY_SZ_SHIFT;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	if (IS_3DES(flags))
249*4882a593Smuzhiyun 		cfg |= ENCR_KEY_SZ_3DES << ENCR_KEY_SZ_SHIFT;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	switch (flags & QCE_MODE_MASK) {
252*4882a593Smuzhiyun 	case QCE_MODE_ECB:
253*4882a593Smuzhiyun 		cfg |= ENCR_MODE_ECB << ENCR_MODE_SHIFT;
254*4882a593Smuzhiyun 		break;
255*4882a593Smuzhiyun 	case QCE_MODE_CBC:
256*4882a593Smuzhiyun 		cfg |= ENCR_MODE_CBC << ENCR_MODE_SHIFT;
257*4882a593Smuzhiyun 		break;
258*4882a593Smuzhiyun 	case QCE_MODE_CTR:
259*4882a593Smuzhiyun 		cfg |= ENCR_MODE_CTR << ENCR_MODE_SHIFT;
260*4882a593Smuzhiyun 		break;
261*4882a593Smuzhiyun 	case QCE_MODE_XTS:
262*4882a593Smuzhiyun 		cfg |= ENCR_MODE_XTS << ENCR_MODE_SHIFT;
263*4882a593Smuzhiyun 		break;
264*4882a593Smuzhiyun 	case QCE_MODE_CCM:
265*4882a593Smuzhiyun 		cfg |= ENCR_MODE_CCM << ENCR_MODE_SHIFT;
266*4882a593Smuzhiyun 		cfg |= LAST_CCM_XFR << LAST_CCM_SHIFT;
267*4882a593Smuzhiyun 		break;
268*4882a593Smuzhiyun 	default:
269*4882a593Smuzhiyun 		return ~0;
270*4882a593Smuzhiyun 	}
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	return cfg;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
qce_xts_swapiv(__be32 * dst,const u8 * src,unsigned int ivsize)275*4882a593Smuzhiyun static void qce_xts_swapiv(__be32 *dst, const u8 *src, unsigned int ivsize)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun 	u8 swap[QCE_AES_IV_LENGTH];
278*4882a593Smuzhiyun 	u32 i, j;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	if (ivsize > QCE_AES_IV_LENGTH)
281*4882a593Smuzhiyun 		return;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	memset(swap, 0, QCE_AES_IV_LENGTH);
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	for (i = (QCE_AES_IV_LENGTH - ivsize), j = ivsize - 1;
286*4882a593Smuzhiyun 	     i < QCE_AES_IV_LENGTH; i++, j--)
287*4882a593Smuzhiyun 		swap[i] = src[j];
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	qce_cpu_to_be32p_array(dst, swap, QCE_AES_IV_LENGTH);
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun 
qce_xtskey(struct qce_device * qce,const u8 * enckey,unsigned int enckeylen,unsigned int cryptlen)292*4882a593Smuzhiyun static void qce_xtskey(struct qce_device *qce, const u8 *enckey,
293*4882a593Smuzhiyun 		       unsigned int enckeylen, unsigned int cryptlen)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun 	u32 xtskey[QCE_MAX_CIPHER_KEY_SIZE / sizeof(u32)] = {0};
296*4882a593Smuzhiyun 	unsigned int xtsklen = enckeylen / (2 * sizeof(u32));
297*4882a593Smuzhiyun 	unsigned int xtsdusize;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	qce_cpu_to_be32p_array((__be32 *)xtskey, enckey + enckeylen / 2,
300*4882a593Smuzhiyun 			       enckeylen / 2);
301*4882a593Smuzhiyun 	qce_write_array(qce, REG_ENCR_XTS_KEY0, xtskey, xtsklen);
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	/* xts du size 512B */
304*4882a593Smuzhiyun 	xtsdusize = min_t(u32, QCE_SECTOR_SIZE, cryptlen);
305*4882a593Smuzhiyun 	qce_write(qce, REG_ENCR_XTS_DU_SIZE, xtsdusize);
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun 
qce_setup_regs_skcipher(struct crypto_async_request * async_req,u32 totallen,u32 offset)308*4882a593Smuzhiyun static int qce_setup_regs_skcipher(struct crypto_async_request *async_req,
309*4882a593Smuzhiyun 				     u32 totallen, u32 offset)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun 	struct skcipher_request *req = skcipher_request_cast(async_req);
312*4882a593Smuzhiyun 	struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req);
313*4882a593Smuzhiyun 	struct qce_cipher_ctx *ctx = crypto_tfm_ctx(async_req->tfm);
314*4882a593Smuzhiyun 	struct qce_alg_template *tmpl = to_cipher_tmpl(crypto_skcipher_reqtfm(req));
315*4882a593Smuzhiyun 	struct qce_device *qce = tmpl->qce;
316*4882a593Smuzhiyun 	__be32 enckey[QCE_MAX_CIPHER_KEY_SIZE / sizeof(__be32)] = {0};
317*4882a593Smuzhiyun 	__be32 enciv[QCE_MAX_IV_SIZE / sizeof(__be32)] = {0};
318*4882a593Smuzhiyun 	unsigned int enckey_words, enciv_words;
319*4882a593Smuzhiyun 	unsigned int keylen;
320*4882a593Smuzhiyun 	u32 encr_cfg = 0, auth_cfg = 0, config;
321*4882a593Smuzhiyun 	unsigned int ivsize = rctx->ivsize;
322*4882a593Smuzhiyun 	unsigned long flags = rctx->flags;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	qce_setup_config(qce);
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	if (IS_XTS(flags))
327*4882a593Smuzhiyun 		keylen = ctx->enc_keylen / 2;
328*4882a593Smuzhiyun 	else
329*4882a593Smuzhiyun 		keylen = ctx->enc_keylen;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	qce_cpu_to_be32p_array(enckey, ctx->enc_key, keylen);
332*4882a593Smuzhiyun 	enckey_words = keylen / sizeof(u32);
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	qce_write(qce, REG_AUTH_SEG_CFG, auth_cfg);
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	encr_cfg = qce_encr_cfg(flags, keylen);
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	if (IS_DES(flags)) {
339*4882a593Smuzhiyun 		enciv_words = 2;
340*4882a593Smuzhiyun 		enckey_words = 2;
341*4882a593Smuzhiyun 	} else if (IS_3DES(flags)) {
342*4882a593Smuzhiyun 		enciv_words = 2;
343*4882a593Smuzhiyun 		enckey_words = 6;
344*4882a593Smuzhiyun 	} else if (IS_AES(flags)) {
345*4882a593Smuzhiyun 		if (IS_XTS(flags))
346*4882a593Smuzhiyun 			qce_xtskey(qce, ctx->enc_key, ctx->enc_keylen,
347*4882a593Smuzhiyun 				   rctx->cryptlen);
348*4882a593Smuzhiyun 		enciv_words = 4;
349*4882a593Smuzhiyun 	} else {
350*4882a593Smuzhiyun 		return -EINVAL;
351*4882a593Smuzhiyun 	}
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	qce_write_array(qce, REG_ENCR_KEY0, (u32 *)enckey, enckey_words);
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	if (!IS_ECB(flags)) {
356*4882a593Smuzhiyun 		if (IS_XTS(flags))
357*4882a593Smuzhiyun 			qce_xts_swapiv(enciv, rctx->iv, ivsize);
358*4882a593Smuzhiyun 		else
359*4882a593Smuzhiyun 			qce_cpu_to_be32p_array(enciv, rctx->iv, ivsize);
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 		qce_write_array(qce, REG_CNTR0_IV0, (u32 *)enciv, enciv_words);
362*4882a593Smuzhiyun 	}
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	if (IS_ENCRYPT(flags))
365*4882a593Smuzhiyun 		encr_cfg |= BIT(ENCODE_SHIFT);
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	qce_write(qce, REG_ENCR_SEG_CFG, encr_cfg);
368*4882a593Smuzhiyun 	qce_write(qce, REG_ENCR_SEG_SIZE, rctx->cryptlen);
369*4882a593Smuzhiyun 	qce_write(qce, REG_ENCR_SEG_START, offset & 0xffff);
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	if (IS_CTR(flags)) {
372*4882a593Smuzhiyun 		qce_write(qce, REG_CNTR_MASK, ~0);
373*4882a593Smuzhiyun 		qce_write(qce, REG_CNTR_MASK0, ~0);
374*4882a593Smuzhiyun 		qce_write(qce, REG_CNTR_MASK1, ~0);
375*4882a593Smuzhiyun 		qce_write(qce, REG_CNTR_MASK2, ~0);
376*4882a593Smuzhiyun 	}
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	qce_write(qce, REG_SEG_SIZE, totallen);
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	/* get little endianness */
381*4882a593Smuzhiyun 	config = qce_config_reg(qce, 1);
382*4882a593Smuzhiyun 	qce_write(qce, REG_CONFIG, config);
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	qce_crypto_go(qce);
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	return 0;
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun #endif
389*4882a593Smuzhiyun 
qce_start(struct crypto_async_request * async_req,u32 type,u32 totallen,u32 offset)390*4882a593Smuzhiyun int qce_start(struct crypto_async_request *async_req, u32 type, u32 totallen,
391*4882a593Smuzhiyun 	      u32 offset)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun 	switch (type) {
394*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DEV_QCE_SKCIPHER
395*4882a593Smuzhiyun 	case CRYPTO_ALG_TYPE_SKCIPHER:
396*4882a593Smuzhiyun 		return qce_setup_regs_skcipher(async_req, totallen, offset);
397*4882a593Smuzhiyun #endif
398*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DEV_QCE_SHA
399*4882a593Smuzhiyun 	case CRYPTO_ALG_TYPE_AHASH:
400*4882a593Smuzhiyun 		return qce_setup_regs_ahash(async_req, totallen, offset);
401*4882a593Smuzhiyun #endif
402*4882a593Smuzhiyun 	default:
403*4882a593Smuzhiyun 		return -EINVAL;
404*4882a593Smuzhiyun 	}
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun #define STATUS_ERRORS	\
408*4882a593Smuzhiyun 		(BIT(SW_ERR_SHIFT) | BIT(AXI_ERR_SHIFT) | BIT(HSD_ERR_SHIFT))
409*4882a593Smuzhiyun 
qce_check_status(struct qce_device * qce,u32 * status)410*4882a593Smuzhiyun int qce_check_status(struct qce_device *qce, u32 *status)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun 	int ret = 0;
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	*status = qce_read(qce, REG_STATUS);
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	/*
417*4882a593Smuzhiyun 	 * Don't use result dump status. The operation may not be complete.
418*4882a593Smuzhiyun 	 * Instead, use the status we just read from device. In case, we need to
419*4882a593Smuzhiyun 	 * use result_status from result dump the result_status needs to be byte
420*4882a593Smuzhiyun 	 * swapped, since we set the device to little endian.
421*4882a593Smuzhiyun 	 */
422*4882a593Smuzhiyun 	if (*status & STATUS_ERRORS || !(*status & BIT(OPERATION_DONE_SHIFT)))
423*4882a593Smuzhiyun 		ret = -ENXIO;
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	return ret;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun 
qce_get_version(struct qce_device * qce,u32 * major,u32 * minor,u32 * step)428*4882a593Smuzhiyun void qce_get_version(struct qce_device *qce, u32 *major, u32 *minor, u32 *step)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun 	u32 val;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	val = qce_read(qce, REG_VERSION);
433*4882a593Smuzhiyun 	*major = (val & CORE_MAJOR_REV_MASK) >> CORE_MAJOR_REV_SHIFT;
434*4882a593Smuzhiyun 	*minor = (val & CORE_MINOR_REV_MASK) >> CORE_MINOR_REV_SHIFT;
435*4882a593Smuzhiyun 	*step = (val & CORE_STEP_REV_MASK) >> CORE_STEP_REV_SHIFT;
436*4882a593Smuzhiyun }
437