xref: /OK3568_Linux_fs/kernel/drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * sun8i-ss.h - hardware cryptographic offloader for
4*4882a593Smuzhiyun  * Allwinner A80/A83T SoC
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 2016-2019 Corentin LABBE <clabbe.montjoie@gmail.com>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun #include <crypto/aes.h>
9*4882a593Smuzhiyun #include <crypto/des.h>
10*4882a593Smuzhiyun #include <crypto/engine.h>
11*4882a593Smuzhiyun #include <crypto/rng.h>
12*4882a593Smuzhiyun #include <crypto/skcipher.h>
13*4882a593Smuzhiyun #include <linux/atomic.h>
14*4882a593Smuzhiyun #include <linux/debugfs.h>
15*4882a593Smuzhiyun #include <linux/crypto.h>
16*4882a593Smuzhiyun #include <crypto/internal/hash.h>
17*4882a593Smuzhiyun #include <crypto/md5.h>
18*4882a593Smuzhiyun #include <crypto/sha.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #define SS_START	1
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define SS_ENCRYPTION		0
23*4882a593Smuzhiyun #define SS_DECRYPTION		BIT(6)
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #define SS_ALG_AES		0
26*4882a593Smuzhiyun #define SS_ALG_DES		(1 << 2)
27*4882a593Smuzhiyun #define SS_ALG_3DES		(2 << 2)
28*4882a593Smuzhiyun #define SS_ALG_MD5		(3 << 2)
29*4882a593Smuzhiyun #define SS_ALG_PRNG		(4 << 2)
30*4882a593Smuzhiyun #define SS_ALG_SHA1		(6 << 2)
31*4882a593Smuzhiyun #define SS_ALG_SHA224		(7 << 2)
32*4882a593Smuzhiyun #define SS_ALG_SHA256		(8 << 2)
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #define SS_CTL_REG		0x00
35*4882a593Smuzhiyun #define SS_INT_CTL_REG		0x04
36*4882a593Smuzhiyun #define SS_INT_STA_REG		0x08
37*4882a593Smuzhiyun #define SS_KEY_ADR_REG		0x10
38*4882a593Smuzhiyun #define SS_IV_ADR_REG		0x18
39*4882a593Smuzhiyun #define SS_SRC_ADR_REG		0x20
40*4882a593Smuzhiyun #define SS_DST_ADR_REG		0x28
41*4882a593Smuzhiyun #define SS_LEN_ADR_REG		0x30
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #define SS_ID_NOTSUPP		0xFF
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun #define SS_ID_CIPHER_AES	0
46*4882a593Smuzhiyun #define SS_ID_CIPHER_DES	1
47*4882a593Smuzhiyun #define SS_ID_CIPHER_DES3	2
48*4882a593Smuzhiyun #define SS_ID_CIPHER_MAX	3
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun #define SS_ID_OP_ECB	0
51*4882a593Smuzhiyun #define SS_ID_OP_CBC	1
52*4882a593Smuzhiyun #define SS_ID_OP_MAX	2
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun #define SS_AES_128BITS 0
55*4882a593Smuzhiyun #define SS_AES_192BITS 1
56*4882a593Smuzhiyun #define SS_AES_256BITS 2
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun #define SS_OP_ECB	0
59*4882a593Smuzhiyun #define SS_OP_CBC	(1 << 13)
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun #define SS_ID_HASH_MD5	0
62*4882a593Smuzhiyun #define SS_ID_HASH_SHA1	1
63*4882a593Smuzhiyun #define SS_ID_HASH_SHA224	2
64*4882a593Smuzhiyun #define SS_ID_HASH_SHA256	3
65*4882a593Smuzhiyun #define SS_ID_HASH_MAX	4
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun #define SS_FLOW0	BIT(30)
68*4882a593Smuzhiyun #define SS_FLOW1	BIT(31)
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #define SS_PRNG_CONTINUE	BIT(18)
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun #define MAX_SG 8
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun #define MAXFLOW 2
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun #define SS_MAX_CLOCKS 2
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun #define SS_DIE_ID_SHIFT	20
79*4882a593Smuzhiyun #define SS_DIE_ID_MASK	0x07
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun #define PRNG_DATA_SIZE (160 / 8)
82*4882a593Smuzhiyun #define PRNG_SEED_SIZE DIV_ROUND_UP(175, 8)
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /*
85*4882a593Smuzhiyun  * struct ss_clock - Describe clocks used by sun8i-ss
86*4882a593Smuzhiyun  * @name:       Name of clock needed by this variant
87*4882a593Smuzhiyun  * @freq:       Frequency to set for each clock
88*4882a593Smuzhiyun  * @max_freq:   Maximum frequency for each clock
89*4882a593Smuzhiyun  */
90*4882a593Smuzhiyun struct ss_clock {
91*4882a593Smuzhiyun 	const char *name;
92*4882a593Smuzhiyun 	unsigned long freq;
93*4882a593Smuzhiyun 	unsigned long max_freq;
94*4882a593Smuzhiyun };
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /*
97*4882a593Smuzhiyun  * struct ss_variant - Describe SS capability for each variant hardware
98*4882a593Smuzhiyun  * @alg_cipher:	list of supported ciphers. for each SS_ID_ this will give the
99*4882a593Smuzhiyun  *              coresponding SS_ALG_XXX value
100*4882a593Smuzhiyun  * @alg_hash:	list of supported hashes. for each SS_ID_ this will give the
101*4882a593Smuzhiyun  *              corresponding SS_ALG_XXX value
102*4882a593Smuzhiyun  * @op_mode:	list of supported block modes
103*4882a593Smuzhiyun  * @ss_clks:	list of clock needed by this variant
104*4882a593Smuzhiyun  */
105*4882a593Smuzhiyun struct ss_variant {
106*4882a593Smuzhiyun 	char alg_cipher[SS_ID_CIPHER_MAX];
107*4882a593Smuzhiyun 	char alg_hash[SS_ID_HASH_MAX];
108*4882a593Smuzhiyun 	u32 op_mode[SS_ID_OP_MAX];
109*4882a593Smuzhiyun 	struct ss_clock ss_clks[SS_MAX_CLOCKS];
110*4882a593Smuzhiyun };
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun struct sginfo {
113*4882a593Smuzhiyun 	u32 addr;
114*4882a593Smuzhiyun 	u32 len;
115*4882a593Smuzhiyun };
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun /*
118*4882a593Smuzhiyun  * struct sun8i_ss_flow - Information used by each flow
119*4882a593Smuzhiyun  * @engine:	ptr to the crypto_engine for this flow
120*4882a593Smuzhiyun  * @complete:	completion for the current task on this flow
121*4882a593Smuzhiyun  * @status:	set to 1 by interrupt if task is done
122*4882a593Smuzhiyun  * @stat_req:	number of request done by this flow
123*4882a593Smuzhiyun  * @iv:		list of IV to use for each step
124*4882a593Smuzhiyun  * @biv:	buffer which contain the backuped IV
125*4882a593Smuzhiyun  * @pad:	padding buffer for hash operations
126*4882a593Smuzhiyun  * @result:	buffer for storing the result of hash operations
127*4882a593Smuzhiyun  */
128*4882a593Smuzhiyun struct sun8i_ss_flow {
129*4882a593Smuzhiyun 	struct crypto_engine *engine;
130*4882a593Smuzhiyun 	struct completion complete;
131*4882a593Smuzhiyun 	int status;
132*4882a593Smuzhiyun 	u8 *iv[MAX_SG];
133*4882a593Smuzhiyun 	u8 *biv;
134*4882a593Smuzhiyun 	void *pad;
135*4882a593Smuzhiyun 	void *result;
136*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
137*4882a593Smuzhiyun 	unsigned long stat_req;
138*4882a593Smuzhiyun #endif
139*4882a593Smuzhiyun };
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun /*
142*4882a593Smuzhiyun  * struct sun8i_ss_dev - main container for all this driver information
143*4882a593Smuzhiyun  * @base:	base address of SS
144*4882a593Smuzhiyun  * @ssclks:	clocks used by SS
145*4882a593Smuzhiyun  * @reset:	pointer to reset controller
146*4882a593Smuzhiyun  * @dev:	the platform device
147*4882a593Smuzhiyun  * @mlock:	Control access to device registers
148*4882a593Smuzhiyun  * @flows:	array of all flow
149*4882a593Smuzhiyun  * @flow:	flow to use in next request
150*4882a593Smuzhiyun  * @variant:	pointer to variant specific data
151*4882a593Smuzhiyun  * @dbgfs_dir:	Debugfs dentry for statistic directory
152*4882a593Smuzhiyun  * @dbgfs_stats: Debugfs dentry for statistic counters
153*4882a593Smuzhiyun  */
154*4882a593Smuzhiyun struct sun8i_ss_dev {
155*4882a593Smuzhiyun 	void __iomem *base;
156*4882a593Smuzhiyun 	struct clk *ssclks[SS_MAX_CLOCKS];
157*4882a593Smuzhiyun 	struct reset_control *reset;
158*4882a593Smuzhiyun 	struct device *dev;
159*4882a593Smuzhiyun 	struct mutex mlock;
160*4882a593Smuzhiyun 	struct sun8i_ss_flow *flows;
161*4882a593Smuzhiyun 	atomic_t flow;
162*4882a593Smuzhiyun 	const struct ss_variant *variant;
163*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
164*4882a593Smuzhiyun 	struct dentry *dbgfs_dir;
165*4882a593Smuzhiyun 	struct dentry *dbgfs_stats;
166*4882a593Smuzhiyun #endif
167*4882a593Smuzhiyun };
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun /*
170*4882a593Smuzhiyun  * struct sun8i_cipher_req_ctx - context for a skcipher request
171*4882a593Smuzhiyun  * @t_src:		list of mapped SGs with their size
172*4882a593Smuzhiyun  * @t_dst:		list of mapped SGs with their size
173*4882a593Smuzhiyun  * @p_key:		DMA address of the key
174*4882a593Smuzhiyun  * @p_iv:		DMA address of the IVs
175*4882a593Smuzhiyun  * @niv:		Number of IVs DMA mapped
176*4882a593Smuzhiyun  * @method:		current algorithm for this request
177*4882a593Smuzhiyun  * @op_mode:		op_mode for this request
178*4882a593Smuzhiyun  * @op_dir:		direction (encrypt vs decrypt) for this request
179*4882a593Smuzhiyun  * @flow:		the flow to use for this request
180*4882a593Smuzhiyun  * @ivlen:		size of IVs
181*4882a593Smuzhiyun  * @keylen:		keylen for this request
182*4882a593Smuzhiyun  * @fallback_req:	request struct for invoking the fallback skcipher TFM
183*4882a593Smuzhiyun  */
184*4882a593Smuzhiyun struct sun8i_cipher_req_ctx {
185*4882a593Smuzhiyun 	struct sginfo t_src[MAX_SG];
186*4882a593Smuzhiyun 	struct sginfo t_dst[MAX_SG];
187*4882a593Smuzhiyun 	u32 p_key;
188*4882a593Smuzhiyun 	u32 p_iv[MAX_SG];
189*4882a593Smuzhiyun 	int niv;
190*4882a593Smuzhiyun 	u32 method;
191*4882a593Smuzhiyun 	u32 op_mode;
192*4882a593Smuzhiyun 	u32 op_dir;
193*4882a593Smuzhiyun 	int flow;
194*4882a593Smuzhiyun 	unsigned int ivlen;
195*4882a593Smuzhiyun 	unsigned int keylen;
196*4882a593Smuzhiyun 	struct skcipher_request fallback_req;   // keep at the end
197*4882a593Smuzhiyun };
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun /*
200*4882a593Smuzhiyun  * struct sun8i_cipher_tfm_ctx - context for a skcipher TFM
201*4882a593Smuzhiyun  * @enginectx:		crypto_engine used by this TFM
202*4882a593Smuzhiyun  * @key:		pointer to key data
203*4882a593Smuzhiyun  * @keylen:		len of the key
204*4882a593Smuzhiyun  * @ss:			pointer to the private data of driver handling this TFM
205*4882a593Smuzhiyun  * @fallback_tfm:	pointer to the fallback TFM
206*4882a593Smuzhiyun  *
207*4882a593Smuzhiyun  * enginectx must be the first element
208*4882a593Smuzhiyun  */
209*4882a593Smuzhiyun struct sun8i_cipher_tfm_ctx {
210*4882a593Smuzhiyun 	struct crypto_engine_ctx enginectx;
211*4882a593Smuzhiyun 	u32 *key;
212*4882a593Smuzhiyun 	u32 keylen;
213*4882a593Smuzhiyun 	struct sun8i_ss_dev *ss;
214*4882a593Smuzhiyun 	struct crypto_skcipher *fallback_tfm;
215*4882a593Smuzhiyun };
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun /*
218*4882a593Smuzhiyun  * struct sun8i_ss_prng_ctx - context for PRNG TFM
219*4882a593Smuzhiyun  * @seed:	The seed to use
220*4882a593Smuzhiyun  * @slen:	The size of the seed
221*4882a593Smuzhiyun  */
222*4882a593Smuzhiyun struct sun8i_ss_rng_tfm_ctx {
223*4882a593Smuzhiyun 	void *seed;
224*4882a593Smuzhiyun 	unsigned int slen;
225*4882a593Smuzhiyun };
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun /*
228*4882a593Smuzhiyun  * struct sun8i_ss_hash_tfm_ctx - context for an ahash TFM
229*4882a593Smuzhiyun  * @enginectx:		crypto_engine used by this TFM
230*4882a593Smuzhiyun  * @fallback_tfm:	pointer to the fallback TFM
231*4882a593Smuzhiyun  * @ss:			pointer to the private data of driver handling this TFM
232*4882a593Smuzhiyun  *
233*4882a593Smuzhiyun  * enginectx must be the first element
234*4882a593Smuzhiyun  */
235*4882a593Smuzhiyun struct sun8i_ss_hash_tfm_ctx {
236*4882a593Smuzhiyun 	struct crypto_engine_ctx enginectx;
237*4882a593Smuzhiyun 	struct crypto_ahash *fallback_tfm;
238*4882a593Smuzhiyun 	struct sun8i_ss_dev *ss;
239*4882a593Smuzhiyun };
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun /*
242*4882a593Smuzhiyun  * struct sun8i_ss_hash_reqctx - context for an ahash request
243*4882a593Smuzhiyun  * @t_src:	list of DMA address and size for source SGs
244*4882a593Smuzhiyun  * @t_dst:	list of DMA address and size for destination SGs
245*4882a593Smuzhiyun  * @fallback_req:	pre-allocated fallback request
246*4882a593Smuzhiyun  * @method:	the register value for the algorithm used by this request
247*4882a593Smuzhiyun  * @flow:	the flow to use for this request
248*4882a593Smuzhiyun  */
249*4882a593Smuzhiyun struct sun8i_ss_hash_reqctx {
250*4882a593Smuzhiyun 	struct sginfo t_src[MAX_SG];
251*4882a593Smuzhiyun 	struct sginfo t_dst[MAX_SG];
252*4882a593Smuzhiyun 	struct ahash_request fallback_req;
253*4882a593Smuzhiyun 	u32 method;
254*4882a593Smuzhiyun 	int flow;
255*4882a593Smuzhiyun };
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun /*
258*4882a593Smuzhiyun  * struct sun8i_ss_alg_template - crypto_alg template
259*4882a593Smuzhiyun  * @type:		the CRYPTO_ALG_TYPE for this template
260*4882a593Smuzhiyun  * @ss_algo_id:		the SS_ID for this template
261*4882a593Smuzhiyun  * @ss_blockmode:	the type of block operation SS_ID
262*4882a593Smuzhiyun  * @ss:			pointer to the sun8i_ss_dev structure associated with
263*4882a593Smuzhiyun  *			this template
264*4882a593Smuzhiyun  * @alg:		one of sub struct must be used
265*4882a593Smuzhiyun  * @stat_req:		number of request done on this template
266*4882a593Smuzhiyun  * @stat_fb:		number of request which has fallbacked
267*4882a593Smuzhiyun  * @stat_bytes:		total data size done by this template
268*4882a593Smuzhiyun  */
269*4882a593Smuzhiyun struct sun8i_ss_alg_template {
270*4882a593Smuzhiyun 	u32 type;
271*4882a593Smuzhiyun 	u32 ss_algo_id;
272*4882a593Smuzhiyun 	u32 ss_blockmode;
273*4882a593Smuzhiyun 	struct sun8i_ss_dev *ss;
274*4882a593Smuzhiyun 	union {
275*4882a593Smuzhiyun 		struct skcipher_alg skcipher;
276*4882a593Smuzhiyun 		struct rng_alg rng;
277*4882a593Smuzhiyun 		struct ahash_alg hash;
278*4882a593Smuzhiyun 	} alg;
279*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
280*4882a593Smuzhiyun 	unsigned long stat_req;
281*4882a593Smuzhiyun 	unsigned long stat_fb;
282*4882a593Smuzhiyun 	unsigned long stat_bytes;
283*4882a593Smuzhiyun #endif
284*4882a593Smuzhiyun };
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun int sun8i_ss_enqueue(struct crypto_async_request *areq, u32 type);
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun int sun8i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
289*4882a593Smuzhiyun 			unsigned int keylen);
290*4882a593Smuzhiyun int sun8i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
291*4882a593Smuzhiyun 			 unsigned int keylen);
292*4882a593Smuzhiyun int sun8i_ss_cipher_init(struct crypto_tfm *tfm);
293*4882a593Smuzhiyun void sun8i_ss_cipher_exit(struct crypto_tfm *tfm);
294*4882a593Smuzhiyun int sun8i_ss_skdecrypt(struct skcipher_request *areq);
295*4882a593Smuzhiyun int sun8i_ss_skencrypt(struct skcipher_request *areq);
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun int sun8i_ss_get_engine_number(struct sun8i_ss_dev *ss);
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun int sun8i_ss_run_task(struct sun8i_ss_dev *ss, struct sun8i_cipher_req_ctx *rctx, const char *name);
300*4882a593Smuzhiyun int sun8i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
301*4882a593Smuzhiyun 			   unsigned int slen, u8 *dst, unsigned int dlen);
302*4882a593Smuzhiyun int sun8i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
303*4882a593Smuzhiyun int sun8i_ss_prng_init(struct crypto_tfm *tfm);
304*4882a593Smuzhiyun void sun8i_ss_prng_exit(struct crypto_tfm *tfm);
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun int sun8i_ss_hash_crainit(struct crypto_tfm *tfm);
307*4882a593Smuzhiyun void sun8i_ss_hash_craexit(struct crypto_tfm *tfm);
308*4882a593Smuzhiyun int sun8i_ss_hash_init(struct ahash_request *areq);
309*4882a593Smuzhiyun int sun8i_ss_hash_export(struct ahash_request *areq, void *out);
310*4882a593Smuzhiyun int sun8i_ss_hash_import(struct ahash_request *areq, const void *in);
311*4882a593Smuzhiyun int sun8i_ss_hash_final(struct ahash_request *areq);
312*4882a593Smuzhiyun int sun8i_ss_hash_update(struct ahash_request *areq);
313*4882a593Smuzhiyun int sun8i_ss_hash_finup(struct ahash_request *areq);
314*4882a593Smuzhiyun int sun8i_ss_hash_digest(struct ahash_request *areq);
315*4882a593Smuzhiyun int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq);
316