Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / crypto / allwinner / sun8i-ce / sun8i-ce.h
blobcec781d5063c15675feff2dd0cfd7d63148c2cef
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * sun8i-ce.h - hardware cryptographic offloader for
4 * Allwinner H3/A64/H5/H2+/H6 SoC
6 * Copyright (C) 2016-2019 Corentin LABBE <clabbe.montjoie@gmail.com>
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>
22 /* CE Registers */
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
36 /* Used in struct ce_task */
37 /* ce_task common */
38 #define CE_ENCRYPTION 0
39 #define CE_DECRYPTION BIT(8)
41 #define CE_COMM_INT BIT(31)
43 /* ce_task symmetric */
44 #define CE_AES_128BITS 0
45 #define CE_AES_192BITS 1
46 #define CE_AES_256BITS 2
48 #define CE_OP_ECB 0
49 #define CE_OP_CBC (1 << 8)
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
65 /* Used in ce_variant */
66 #define CE_ID_NOTSUPP 0xFF
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
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
81 #define CE_ID_OP_ECB 0
82 #define CE_ID_OP_CBC 1
83 #define CE_ID_OP_MAX 2
85 /* Used in CE registers */
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)
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
98 #define PRNG_DATA_SIZE (160 / 8)
99 #define PRNG_SEED_SIZE DIV_ROUND_UP(175, 8)
100 #define PRNG_LD BIT(17)
102 #define CE_DIE_ID_SHIFT 16
103 #define CE_DIE_ID_MASK 0x07
105 #define MAX_SG 8
107 #define CE_MAX_CLOCKS 3
109 #define MAXFLOW 4
112 * struct ce_clock - Describe clocks used by sun8i-ce
113 * @name: Name of clock needed by this variant
114 * @freq: Frequency to set for each clock
115 * @max_freq: Maximum frequency for each clock (generally given by datasheet)
117 struct ce_clock {
118 const char *name;
119 unsigned long freq;
120 unsigned long max_freq;
124 * struct ce_variant - Describe CE capability for each variant hardware
125 * @alg_cipher: list of supported ciphers. for each CE_ID_ this will give the
126 * coresponding CE_ALG_XXX value
127 * @alg_hash: list of supported hashes. for each CE_ID_ this will give the
128 * corresponding CE_ALG_XXX value
129 * @op_mode: list of supported block modes
130 * @cipher_t_dlen_in_bytes: Does the request size for cipher is in
131 * bytes or words
132 * @hash_t_dlen_in_bytes: Does the request size for hash is in
133 * bits or words
134 * @prng_t_dlen_in_bytes: Does the request size for PRNG is in
135 * bytes or words
136 * @trng_t_dlen_in_bytes: Does the request size for TRNG is in
137 * bytes or words
138 * @ce_clks: list of clocks needed by this variant
139 * @esr: The type of error register
140 * @prng: The CE_ALG_XXX value for the PRNG
141 * @trng: The CE_ALG_XXX value for the TRNG
143 struct 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;
157 struct sginfo {
158 __le32 addr;
159 __le32 len;
160 } __packed;
163 * struct ce_task - CE Task descriptor
164 * The structure of this descriptor could be found in the datasheet
166 struct 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);
182 * struct sun8i_ce_flow - Information used by each flow
183 * @engine: ptr to the crypto_engine for this flow
184 * @complete: completion for the current task on this flow
185 * @status: set to 1 by interrupt if task is done
186 * @t_phy: Physical address of task
187 * @tl: pointer to the current ce_task for this flow
188 * @stat_req: number of request done by this flow
190 struct 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
203 * struct sun8i_ce_dev - main container for all this driver information
204 * @base: base address of CE
205 * @ceclks: clocks used by CE
206 * @reset: pointer to reset controller
207 * @dev: the platform device
208 * @mlock: Control access to device registers
209 * @rnglock: Control access to the RNG (dedicated channel 3)
210 * @chanlist: array of all flow
211 * @flow: flow to use in next request
212 * @variant: pointer to variant specific data
213 * @dbgfs_dir: Debugfs dentry for statistic directory
214 * @dbgfs_stats: Debugfs dentry for statistic counters
216 struct 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
240 * struct sun8i_cipher_req_ctx - context for a skcipher request
241 * @op_dir: direction (encrypt vs decrypt) for this request
242 * @flow: the flow to use for this request
243 * @backup_iv: buffer which contain the next IV to store
244 * @bounce_iv: buffer which contain the IV
245 * @ivlen: size of bounce_iv
246 * @nr_sgs: The number of source SG (as given by dma_map_sg())
247 * @nr_sgd: The number of destination SG (as given by dma_map_sg())
248 * @addr_iv: The IV addr returned by dma_map_single, need to unmap later
249 * @addr_key: The key addr returned by dma_map_single, need to unmap later
250 * @fallback_req: request struct for invoking the fallback skcipher TFM
252 struct 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; // keep at the end
266 * struct sun8i_cipher_tfm_ctx - context for a skcipher TFM
267 * @enginectx: crypto_engine used by this TFM
268 * @key: pointer to key data
269 * @keylen: len of the key
270 * @ce: pointer to the private data of driver handling this TFM
271 * @fallback_tfm: pointer to the fallback TFM
273 struct 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;
282 * struct sun8i_ce_hash_tfm_ctx - context for an ahash TFM
283 * @enginectx: crypto_engine used by this TFM
284 * @ce: pointer to the private data of driver handling this TFM
285 * @fallback_tfm: pointer to the fallback TFM
287 struct sun8i_ce_hash_tfm_ctx {
288 struct crypto_engine_ctx enginectx;
289 struct sun8i_ce_dev *ce;
290 struct crypto_ahash *fallback_tfm;
294 * struct sun8i_ce_hash_reqctx - context for an ahash request
295 * @fallback_req: pre-allocated fallback request
296 * @flow: the flow to use for this request
298 struct sun8i_ce_hash_reqctx {
299 struct ahash_request fallback_req;
300 int flow;
304 * struct sun8i_ce_prng_ctx - context for PRNG TFM
305 * @seed: The seed to use
306 * @slen: The size of the seed
308 struct sun8i_ce_rng_tfm_ctx {
309 void *seed;
310 unsigned int slen;
314 * struct sun8i_ce_alg_template - crypto_alg template
315 * @type: the CRYPTO_ALG_TYPE for this template
316 * @ce_algo_id: the CE_ID for this template
317 * @ce_blockmode: the type of block operation CE_ID
318 * @ce: pointer to the sun8i_ce_dev structure associated with
319 * this template
320 * @alg: one of sub struct must be used
321 * @stat_req: number of request done on this template
322 * @stat_fb: number of request which has fallbacked
323 * @stat_bytes: total data size done by this template
325 struct 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
342 int sun8i_ce_enqueue(struct crypto_async_request *areq, u32 type);
344 int sun8i_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
345 unsigned int keylen);
346 int sun8i_ce_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
347 unsigned int keylen);
348 int sun8i_ce_cipher_init(struct crypto_tfm *tfm);
349 void sun8i_ce_cipher_exit(struct crypto_tfm *tfm);
350 int sun8i_ce_skdecrypt(struct skcipher_request *areq);
351 int sun8i_ce_skencrypt(struct skcipher_request *areq);
353 int sun8i_ce_get_engine_number(struct sun8i_ce_dev *ce);
355 int sun8i_ce_run_task(struct sun8i_ce_dev *ce, int flow, const char *name);
357 int sun8i_ce_hash_crainit(struct crypto_tfm *tfm);
358 void sun8i_ce_hash_craexit(struct crypto_tfm *tfm);
359 int sun8i_ce_hash_init(struct ahash_request *areq);
360 int sun8i_ce_hash_export(struct ahash_request *areq, void *out);
361 int sun8i_ce_hash_import(struct ahash_request *areq, const void *in);
362 int sun8i_ce_hash(struct ahash_request *areq);
363 int sun8i_ce_hash_final(struct ahash_request *areq);
364 int sun8i_ce_hash_update(struct ahash_request *areq);
365 int sun8i_ce_hash_finup(struct ahash_request *areq);
366 int sun8i_ce_hash_digest(struct ahash_request *areq);
367 int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq);
369 int sun8i_ce_prng_generate(struct crypto_rng *tfm, const u8 *src,
370 unsigned int slen, u8 *dst, unsigned int dlen);
371 int sun8i_ce_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
372 void sun8i_ce_prng_exit(struct crypto_tfm *tfm);
373 int sun8i_ce_prng_init(struct crypto_tfm *tfm);
375 int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce);
376 void sun8i_ce_hwrng_unregister(struct sun8i_ce_dev *ce);