1 /* SPDX-License-Identifier: GPL-2.0 */
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>
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/sha.h>
35 /* Used in struct ce_task */
37 #define CE_ENCRYPTION 0
38 #define CE_DECRYPTION BIT(8)
40 #define CE_COMM_INT BIT(31)
42 /* ce_task symmetric */
43 #define CE_AES_128BITS 0
44 #define CE_AES_192BITS 1
45 #define CE_AES_256BITS 2
48 #define CE_OP_CBC (1 << 8)
54 #define CE_ALG_SHA1 17
55 #define CE_ALG_SHA224 18
56 #define CE_ALG_SHA256 19
57 #define CE_ALG_SHA384 20
58 #define CE_ALG_SHA512 21
59 #define CE_ALG_TRNG 48
60 #define CE_ALG_PRNG 49
61 #define CE_ALG_TRNG_V2 0x1c
62 #define CE_ALG_PRNG_V2 0x1d
64 /* Used in ce_variant */
65 #define CE_ID_NOTSUPP 0xFF
67 #define CE_ID_CIPHER_AES 0
68 #define CE_ID_CIPHER_DES 1
69 #define CE_ID_CIPHER_DES3 2
70 #define CE_ID_CIPHER_MAX 3
72 #define CE_ID_HASH_MD5 0
73 #define CE_ID_HASH_SHA1 1
74 #define CE_ID_HASH_SHA224 2
75 #define CE_ID_HASH_SHA256 3
76 #define CE_ID_HASH_SHA384 4
77 #define CE_ID_HASH_SHA512 5
78 #define CE_ID_HASH_MAX 6
80 #define CE_ID_OP_ECB 0
81 #define CE_ID_OP_CBC 1
82 #define CE_ID_OP_MAX 2
84 /* Used in CE registers */
85 #define CE_ERR_ALGO_NOTSUP BIT(0)
86 #define CE_ERR_DATALEN BIT(1)
87 #define CE_ERR_KEYSRAM BIT(2)
88 #define CE_ERR_ADDR_INVALID BIT(5)
89 #define CE_ERR_KEYLADDER BIT(6)
97 #define PRNG_DATA_SIZE (160 / 8)
98 #define PRNG_SEED_SIZE DIV_ROUND_UP(175, 8)
99 #define PRNG_LD BIT(17)
101 #define CE_DIE_ID_SHIFT 16
102 #define CE_DIE_ID_MASK 0x07
106 #define CE_MAX_CLOCKS 3
111 * struct ce_clock - Describe clocks used by sun8i-ce
112 * @name: Name of clock needed by this variant
113 * @freq: Frequency to set for each clock
114 * @max_freq: Maximum frequency for each clock (generally given by datasheet)
119 unsigned long max_freq;
123 * struct ce_variant - Describe CE capability for each variant hardware
124 * @alg_cipher: list of supported ciphers. for each CE_ID_ this will give the
125 * coresponding CE_ALG_XXX value
126 * @alg_hash: list of supported hashes. for each CE_ID_ this will give the
127 * corresponding CE_ALG_XXX value
128 * @op_mode: list of supported block modes
129 * @cipher_t_dlen_in_bytes: Does the request size for cipher is in
131 * @hash_t_dlen_in_bytes: Does the request size for hash is in
133 * @prng_t_dlen_in_bytes: Does the request size for PRNG is in
135 * @trng_t_dlen_in_bytes: Does the request size for TRNG is in
137 * @ce_clks: list of clocks needed by this variant
138 * @esr: The type of error register
139 * @prng: The CE_ALG_XXX value for the PRNG
140 * @trng: The CE_ALG_XXX value for the TRNG
143 char alg_cipher[CE_ID_CIPHER_MAX];
144 char alg_hash[CE_ID_HASH_MAX];
145 u32 op_mode[CE_ID_OP_MAX];
146 bool cipher_t_dlen_in_bytes;
147 bool hash_t_dlen_in_bits;
148 bool prng_t_dlen_in_bytes;
149 bool trng_t_dlen_in_bytes;
150 struct ce_clock ce_clks[CE_MAX_CLOCKS];
162 * struct ce_task - CE Task descriptor
163 * The structure of this descriptor could be found in the datasheet
174 struct sginfo t_src[MAX_SG];
175 struct sginfo t_dst[MAX_SG];
178 } __packed __aligned(8);
181 * struct sun8i_ce_flow - Information used by each flow
182 * @engine: ptr to the crypto_engine for this flow
183 * @complete: completion for the current task on this flow
184 * @status: set to 1 by interrupt if task is done
185 * @t_phy: Physical address of task
186 * @tl: pointer to the current ce_task for this flow
187 * @stat_req: number of request done by this flow
189 struct sun8i_ce_flow {
190 struct crypto_engine *engine;
191 struct completion complete;
196 #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
197 unsigned long stat_req;
202 * struct sun8i_ce_dev - main container for all this driver information
203 * @base: base address of CE
204 * @ceclks: clocks used by CE
205 * @reset: pointer to reset controller
206 * @dev: the platform device
207 * @mlock: Control access to device registers
208 * @rnglock: Control access to the RNG (dedicated channel 3)
209 * @chanlist: array of all flow
210 * @flow: flow to use in next request
211 * @variant: pointer to variant specific data
212 * @dbgfs_dir: Debugfs dentry for statistic directory
213 * @dbgfs_stats: Debugfs dentry for statistic counters
215 struct sun8i_ce_dev {
217 struct clk *ceclks[CE_MAX_CLOCKS];
218 struct reset_control *reset;
221 struct mutex rnglock;
222 struct sun8i_ce_flow *chanlist;
224 const struct ce_variant *variant;
225 #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
226 struct dentry *dbgfs_dir;
227 struct dentry *dbgfs_stats;
229 #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_TRNG
231 #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
232 unsigned long hwrng_stat_req;
233 unsigned long hwrng_stat_bytes;
239 * struct sun8i_cipher_req_ctx - context for a skcipher request
240 * @op_dir: direction (encrypt vs decrypt) for this request
241 * @flow: the flow to use for this request
242 * @backup_iv: buffer which contain the next IV to store
243 * @bounce_iv: buffer which contain the IV
244 * @ivlen: size of bounce_iv
245 * @nr_sgs: The number of source SG (as given by dma_map_sg())
246 * @nr_sgd: The number of destination SG (as given by dma_map_sg())
247 * @addr_iv: The IV addr returned by dma_map_single, need to unmap later
248 * @addr_key: The key addr returned by dma_map_single, need to unmap later
249 * @fallback_req: request struct for invoking the fallback skcipher TFM
251 struct sun8i_cipher_req_ctx {
261 struct skcipher_request fallback_req; // keep at the end
265 * struct sun8i_cipher_tfm_ctx - context for a skcipher TFM
266 * @enginectx: crypto_engine used by this TFM
267 * @key: pointer to key data
268 * @keylen: len of the key
269 * @ce: pointer to the private data of driver handling this TFM
270 * @fallback_tfm: pointer to the fallback TFM
272 struct sun8i_cipher_tfm_ctx {
273 struct crypto_engine_ctx enginectx;
276 struct sun8i_ce_dev *ce;
277 struct crypto_skcipher *fallback_tfm;
281 * struct sun8i_ce_hash_tfm_ctx - context for an ahash TFM
282 * @enginectx: crypto_engine used by this TFM
283 * @ce: pointer to the private data of driver handling this TFM
284 * @fallback_tfm: pointer to the fallback TFM
286 struct sun8i_ce_hash_tfm_ctx {
287 struct crypto_engine_ctx enginectx;
288 struct sun8i_ce_dev *ce;
289 struct crypto_ahash *fallback_tfm;
293 * struct sun8i_ce_hash_reqctx - context for an ahash request
294 * @fallback_req: pre-allocated fallback request
295 * @flow: the flow to use for this request
297 struct sun8i_ce_hash_reqctx {
298 struct ahash_request fallback_req;
303 * struct sun8i_ce_prng_ctx - context for PRNG TFM
304 * @seed: The seed to use
305 * @slen: The size of the seed
307 struct sun8i_ce_rng_tfm_ctx {
313 * struct sun8i_ce_alg_template - crypto_alg template
314 * @type: the CRYPTO_ALG_TYPE for this template
315 * @ce_algo_id: the CE_ID for this template
316 * @ce_blockmode: the type of block operation CE_ID
317 * @ce: pointer to the sun8i_ce_dev structure associated with
319 * @alg: one of sub struct must be used
320 * @stat_req: number of request done on this template
321 * @stat_fb: number of request which has fallbacked
322 * @stat_bytes: total data size done by this template
324 struct sun8i_ce_alg_template {
328 struct sun8i_ce_dev *ce;
330 struct skcipher_alg skcipher;
331 struct ahash_alg hash;
334 #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
335 unsigned long stat_req;
336 unsigned long stat_fb;
337 unsigned long stat_bytes;
341 int sun8i_ce_enqueue(struct crypto_async_request *areq, u32 type);
343 int sun8i_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
344 unsigned int keylen);
345 int sun8i_ce_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
346 unsigned int keylen);
347 int sun8i_ce_cipher_init(struct crypto_tfm *tfm);
348 void sun8i_ce_cipher_exit(struct crypto_tfm *tfm);
349 int sun8i_ce_skdecrypt(struct skcipher_request *areq);
350 int sun8i_ce_skencrypt(struct skcipher_request *areq);
352 int sun8i_ce_get_engine_number(struct sun8i_ce_dev *ce);
354 int sun8i_ce_run_task(struct sun8i_ce_dev *ce, int flow, const char *name);
356 int sun8i_ce_hash_crainit(struct crypto_tfm *tfm);
357 void sun8i_ce_hash_craexit(struct crypto_tfm *tfm);
358 int sun8i_ce_hash_init(struct ahash_request *areq);
359 int sun8i_ce_hash_export(struct ahash_request *areq, void *out);
360 int sun8i_ce_hash_import(struct ahash_request *areq, const void *in);
361 int sun8i_ce_hash(struct ahash_request *areq);
362 int sun8i_ce_hash_final(struct ahash_request *areq);
363 int sun8i_ce_hash_update(struct ahash_request *areq);
364 int sun8i_ce_hash_finup(struct ahash_request *areq);
365 int sun8i_ce_hash_digest(struct ahash_request *areq);
366 int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq);
368 int sun8i_ce_prng_generate(struct crypto_rng *tfm, const u8 *src,
369 unsigned int slen, u8 *dst, unsigned int dlen);
370 int sun8i_ce_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
371 void sun8i_ce_prng_exit(struct crypto_tfm *tfm);
372 int sun8i_ce_prng_init(struct crypto_tfm *tfm);
374 int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce);
375 void sun8i_ce_hwrng_unregister(struct sun8i_ce_dev *ce);