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
98#define PRNG_DATA_SIZE (160 / 8)
99#define PRNG_SEED_SIZE DIV_ROUND_UP(175, 8)
100#define PRNG_LD BIT(17)
101
102#define CE_DIE_ID_SHIFT 16
103#define CE_DIE_ID_MASK 0x07
104
105#define MAX_SG 8
106
107#define CE_MAX_CLOCKS 3
108
109#define MAXFLOW 4
110
111
112
113
114
115
116
117struct ce_clock {
118 const char *name;
119 unsigned long freq;
120 unsigned long max_freq;
121};
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143struct ce_variant {
144 char alg_cipher[CE_ID_CIPHER_MAX];
145 char alg_hash[CE_ID_HASH_MAX];
146 u32 op_mode[CE_ID_OP_MAX];
147 bool cipher_t_dlen_in_bytes;
148 bool hash_t_dlen_in_bits;
149 bool prng_t_dlen_in_bytes;
150 bool trng_t_dlen_in_bytes;
151 struct ce_clock ce_clks[CE_MAX_CLOCKS];
152 int esr;
153 unsigned char prng;
154 unsigned char trng;
155};
156
157struct sginfo {
158 __le32 addr;
159 __le32 len;
160} __packed;
161
162
163
164
165
166struct ce_task {
167 __le32 t_id;
168 __le32 t_common_ctl;
169 __le32 t_sym_ctl;
170 __le32 t_asym_ctl;
171 __le32 t_key;
172 __le32 t_iv;
173 __le32 t_ctr;
174 __le32 t_dlen;
175 struct sginfo t_src[MAX_SG];
176 struct sginfo t_dst[MAX_SG];
177 __le32 next;
178 __le32 reserved[3];
179} __packed __aligned(8);
180
181
182
183
184
185
186
187
188
189
190struct sun8i_ce_flow {
191 struct crypto_engine *engine;
192 struct completion complete;
193 int status;
194 dma_addr_t t_phy;
195 int timeout;
196 struct ce_task *tl;
197#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
198 unsigned long stat_req;
199#endif
200};
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216struct sun8i_ce_dev {
217 void __iomem *base;
218 struct clk *ceclks[CE_MAX_CLOCKS];
219 struct reset_control *reset;
220 struct device *dev;
221 struct mutex mlock;
222 struct mutex rnglock;
223 struct sun8i_ce_flow *chanlist;
224 atomic_t flow;
225 const struct ce_variant *variant;
226#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
227 struct dentry *dbgfs_dir;
228 struct dentry *dbgfs_stats;
229#endif
230#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_TRNG
231 struct hwrng trng;
232#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
233 unsigned long hwrng_stat_req;
234 unsigned long hwrng_stat_bytes;
235#endif
236#endif
237};
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252struct sun8i_cipher_req_ctx {
253 u32 op_dir;
254 int flow;
255 void *backup_iv;
256 void *bounce_iv;
257 unsigned int ivlen;
258 int nr_sgs;
259 int nr_sgd;
260 dma_addr_t addr_iv;
261 dma_addr_t addr_key;
262 struct skcipher_request fallback_req;
263};
264
265
266
267
268
269
270
271
272
273struct sun8i_cipher_tfm_ctx {
274 struct crypto_engine_ctx enginectx;
275 u32 *key;
276 u32 keylen;
277 struct sun8i_ce_dev *ce;
278 struct crypto_skcipher *fallback_tfm;
279};
280
281
282
283
284
285
286
287struct sun8i_ce_hash_tfm_ctx {
288 struct crypto_engine_ctx enginectx;
289 struct sun8i_ce_dev *ce;
290 struct crypto_ahash *fallback_tfm;
291};
292
293
294
295
296
297
298struct sun8i_ce_hash_reqctx {
299 struct ahash_request fallback_req;
300 int flow;
301};
302
303
304
305
306
307
308struct sun8i_ce_rng_tfm_ctx {
309 void *seed;
310 unsigned int slen;
311};
312
313
314
315
316
317
318
319
320
321
322
323
324
325struct sun8i_ce_alg_template {
326 u32 type;
327 u32 ce_algo_id;
328 u32 ce_blockmode;
329 struct sun8i_ce_dev *ce;
330 union {
331 struct skcipher_alg skcipher;
332 struct ahash_alg hash;
333 struct rng_alg rng;
334 } alg;
335#ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
336 unsigned long stat_req;
337 unsigned long stat_fb;
338 unsigned long stat_bytes;
339#endif
340};
341
342int sun8i_ce_enqueue(struct crypto_async_request *areq, u32 type);
343
344int sun8i_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
345 unsigned int keylen);
346int sun8i_ce_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
347 unsigned int keylen);
348int sun8i_ce_cipher_init(struct crypto_tfm *tfm);
349void sun8i_ce_cipher_exit(struct crypto_tfm *tfm);
350int sun8i_ce_skdecrypt(struct skcipher_request *areq);
351int sun8i_ce_skencrypt(struct skcipher_request *areq);
352
353int sun8i_ce_get_engine_number(struct sun8i_ce_dev *ce);
354
355int sun8i_ce_run_task(struct sun8i_ce_dev *ce, int flow, const char *name);
356
357int sun8i_ce_hash_crainit(struct crypto_tfm *tfm);
358void sun8i_ce_hash_craexit(struct crypto_tfm *tfm);
359int sun8i_ce_hash_init(struct ahash_request *areq);
360int sun8i_ce_hash_export(struct ahash_request *areq, void *out);
361int sun8i_ce_hash_import(struct ahash_request *areq, const void *in);
362int sun8i_ce_hash(struct ahash_request *areq);
363int sun8i_ce_hash_final(struct ahash_request *areq);
364int sun8i_ce_hash_update(struct ahash_request *areq);
365int sun8i_ce_hash_finup(struct ahash_request *areq);
366int sun8i_ce_hash_digest(struct ahash_request *areq);
367int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq);
368
369int sun8i_ce_prng_generate(struct crypto_rng *tfm, const u8 *src,
370 unsigned int slen, u8 *dst, unsigned int dlen);
371int sun8i_ce_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
372void sun8i_ce_prng_exit(struct crypto_tfm *tfm);
373int sun8i_ce_prng_init(struct crypto_tfm *tfm);
374
375int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce);
376void sun8i_ce_hwrng_unregister(struct sun8i_ce_dev *ce);
377