1
2
3
4
5
6
7
8#include <crypto/aes.h>
9#include <crypto/des.h>
10#include <crypto/engine.h>
11#include <crypto/skcipher.h>
12#include <linux/atomic.h>
13#include <linux/debugfs.h>
14#include <linux/crypto.h>
15#include <linux/hw_random.h>
16#include <crypto/internal/hash.h>
17#include <crypto/md5.h>
18#include <crypto/rng.h>
19#include <crypto/sha1.h>
20#include <crypto/sha2.h>
21
22
23#define CE_TDQ 0x00
24#define CE_CTR 0x04
25#define CE_ICR 0x08
26#define CE_ISR 0x0C
27#define CE_TLR 0x10
28#define CE_TSR 0x14
29#define CE_ESR 0x18
30#define CE_CSSGR 0x1C
31#define CE_CDSGR 0x20
32#define CE_CSAR 0x24
33#define CE_CDAR 0x28
34#define CE_TPR 0x2C
35
36
37
38#define CE_ENCRYPTION 0
39#define CE_DECRYPTION BIT(8)
40
41#define CE_COMM_INT BIT(31)
42
43
44#define CE_AES_128BITS 0
45#define CE_AES_192BITS 1
46#define CE_AES_256BITS 2
47
48#define CE_OP_ECB 0
49#define CE_OP_CBC (1 << 8)
50
51#define CE_ALG_AES 0
52#define CE_ALG_DES 1
53#define CE_ALG_3DES 2
54#define CE_ALG_MD5 16
55#define CE_ALG_SHA1 17
56#define CE_ALG_SHA224 18
57#define CE_ALG_SHA256 19
58#define CE_ALG_SHA384 20
59#define CE_ALG_SHA512 21
60#define CE_ALG_TRNG 48
61#define CE_ALG_PRNG 49
62#define CE_ALG_TRNG_V2 0x1c
63#define CE_ALG_PRNG_V2 0x1d
64
65
66#define CE_ID_NOTSUPP 0xFF
67
68#define CE_ID_CIPHER_AES 0
69#define CE_ID_CIPHER_DES 1
70#define CE_ID_CIPHER_DES3 2
71#define CE_ID_CIPHER_MAX 3
72
73#define CE_ID_HASH_MD5 0
74#define CE_ID_HASH_SHA1 1
75#define CE_ID_HASH_SHA224 2
76#define CE_ID_HASH_SHA256 3
77#define CE_ID_HASH_SHA384 4
78#define CE_ID_HASH_SHA512 5
79#define CE_ID_HASH_MAX 6
80
81#define CE_ID_OP_ECB 0
82#define CE_ID_OP_CBC 1
83#define CE_ID_OP_MAX 2
84
85
86#define CE_ERR_ALGO_NOTSUP BIT(0)
87#define CE_ERR_DATALEN BIT(1)
88#define CE_ERR_KEYSRAM BIT(2)
89#define CE_ERR_ADDR_INVALID BIT(5)
90#define CE_ERR_KEYLADDER BIT(6)
91
92#define ESR_H3 0
93#define ESR_A64 1
94#define ESR_R40 2
95#define ESR_H5 3
96#define ESR_H6 4
97#define ESR_D1 5
98
99#define PRNG_DATA_SIZE (160 / 8)
100#define PRNG_SEED_SIZE DIV_ROUND_UP(175, 8)
101#define PRNG_LD BIT(17)
102
103#define CE_DIE_ID_SHIFT 16
104#define CE_DIE_ID_MASK 0x07
105
106#define MAX_SG 8
107
108#define CE_MAX_CLOCKS 3
109
110#define MAXFLOW 4
111
112
113
114
115
116
117
118struct ce_clock {
119 const char *name;
120 unsigned long freq;
121 unsigned long max_freq;
122};
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144struct ce_variant {
145 char alg_cipher[CE_ID_CIPHER_MAX];
146 char alg_hash[CE_ID_HASH_MAX];
147 u32 op_mode[CE_ID_OP_MAX];
148 bool cipher_t_dlen_in_bytes;
149 bool hash_t_dlen_in_bits;
150 bool prng_t_dlen_in_bytes;
151 bool trng_t_dlen_in_bytes;
152 struct ce_clock ce_clks[CE_MAX_CLOCKS];
153 int esr;
154 unsigned char prng;
155 unsigned char trng;
156};
157
158struct sginfo {
159 __le32 addr;
160 __le32 len;
161} __packed;
162
163
164
165
166
167struct ce_task {
168 __le32 t_id;
169 __le32 t_common_ctl;
170 __le32 t_sym_ctl;
171 __le32 t_asym_ctl;
172 __le32 t_key;
173 __le32 t_iv;
174 __le32 t_ctr;
175 __le32 t_dlen;
176 struct sginfo t_src[MAX_SG];
177 struct sginfo t_dst[MAX_SG];
178 __le32 next;
179 __le32 reserved[3];
180} __packed __aligned(8);
181
182
183
184
185
186
187
188
189
190
191
192
193struct sun8i_ce_flow {
194 struct crypto_engine *engine;
195 struct completion complete;
196 int status;
197 dma_addr_t t_phy;
198 int timeout;
199 struct ce_task *tl;
200 void *backup_iv;
201 void *bounce_iv;
202#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
203 unsigned long stat_req;
204#endif
205};
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221struct sun8i_ce_dev {
222 void __iomem *base;
223 struct clk *ceclks[CE_MAX_CLOCKS];
224 struct reset_control *reset;
225 struct device *dev;
226 struct mutex mlock;
227 struct mutex rnglock;
228 struct sun8i_ce_flow *chanlist;
229 atomic_t flow;
230 const struct ce_variant *variant;
231#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
232 struct dentry *dbgfs_dir;
233 struct dentry *dbgfs_stats;
234#endif
235#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_TRNG
236 struct hwrng trng;
237#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
238 unsigned long hwrng_stat_req;
239 unsigned long hwrng_stat_bytes;
240#endif
241#endif
242};
243
244
245
246
247
248
249
250
251
252
253
254
255struct sun8i_cipher_req_ctx {
256 u32 op_dir;
257 int flow;
258 unsigned int ivlen;
259 int nr_sgs;
260 int nr_sgd;
261 dma_addr_t addr_iv;
262 dma_addr_t addr_key;
263 struct skcipher_request fallback_req;
264};
265
266
267
268
269
270
271
272
273
274struct sun8i_cipher_tfm_ctx {
275 struct crypto_engine_ctx enginectx;
276 u32 *key;
277 u32 keylen;
278 struct sun8i_ce_dev *ce;
279 struct crypto_skcipher *fallback_tfm;
280};
281
282
283
284
285
286
287
288struct sun8i_ce_hash_tfm_ctx {
289 struct crypto_engine_ctx enginectx;
290 struct sun8i_ce_dev *ce;
291 struct crypto_ahash *fallback_tfm;
292};
293
294
295
296
297
298
299struct sun8i_ce_hash_reqctx {
300 struct ahash_request fallback_req;
301 int flow;
302};
303
304
305
306
307
308
309struct sun8i_ce_rng_tfm_ctx {
310 void *seed;
311 unsigned int slen;
312};
313
314
315
316
317
318
319
320
321
322
323
324
325
326struct sun8i_ce_alg_template {
327 u32 type;
328 u32 ce_algo_id;
329 u32 ce_blockmode;
330 struct sun8i_ce_dev *ce;
331 union {
332 struct skcipher_alg skcipher;
333 struct ahash_alg hash;
334 struct rng_alg rng;
335 } alg;
336 unsigned long stat_req;
337 unsigned long stat_fb;
338 unsigned long stat_bytes;
339 unsigned long stat_fb_maxsg;
340 unsigned long stat_fb_leniv;
341 unsigned long stat_fb_len0;
342 unsigned long stat_fb_mod16;
343 unsigned long stat_fb_srcali;
344 unsigned long stat_fb_srclen;
345 unsigned long stat_fb_dstali;
346 unsigned long stat_fb_dstlen;
347 char fbname[CRYPTO_MAX_ALG_NAME];
348};
349
350int sun8i_ce_enqueue(struct crypto_async_request *areq, u32 type);
351
352int sun8i_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
353 unsigned int keylen);
354int sun8i_ce_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
355 unsigned int keylen);
356int sun8i_ce_cipher_init(struct crypto_tfm *tfm);
357void sun8i_ce_cipher_exit(struct crypto_tfm *tfm);
358int sun8i_ce_skdecrypt(struct skcipher_request *areq);
359int sun8i_ce_skencrypt(struct skcipher_request *areq);
360
361int sun8i_ce_get_engine_number(struct sun8i_ce_dev *ce);
362
363int sun8i_ce_run_task(struct sun8i_ce_dev *ce, int flow, const char *name);
364
365int sun8i_ce_hash_crainit(struct crypto_tfm *tfm);
366void sun8i_ce_hash_craexit(struct crypto_tfm *tfm);
367int sun8i_ce_hash_init(struct ahash_request *areq);
368int sun8i_ce_hash_export(struct ahash_request *areq, void *out);
369int sun8i_ce_hash_import(struct ahash_request *areq, const void *in);
370int sun8i_ce_hash(struct ahash_request *areq);
371int sun8i_ce_hash_final(struct ahash_request *areq);
372int sun8i_ce_hash_update(struct ahash_request *areq);
373int sun8i_ce_hash_finup(struct ahash_request *areq);
374int sun8i_ce_hash_digest(struct ahash_request *areq);
375int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq);
376
377int sun8i_ce_prng_generate(struct crypto_rng *tfm, const u8 *src,
378 unsigned int slen, u8 *dst, unsigned int dlen);
379int sun8i_ce_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
380void sun8i_ce_prng_exit(struct crypto_tfm *tfm);
381int sun8i_ce_prng_init(struct crypto_tfm *tfm);
382
383int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce);
384void sun8i_ce_hwrng_unregister(struct sun8i_ce_dev *ce);
385