sun8i-ce.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * sun8i-ce.h - hardware cryptographic offloader for
  4. * Allwinner H3/A64/H5/H2+/H6 SoC
  5. *
  6. * Copyright (C) 2016-2019 Corentin LABBE <[email protected]>
  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. /* CE Registers */
  22. #define CE_TDQ 0x00
  23. #define CE_CTR 0x04
  24. #define CE_ICR 0x08
  25. #define CE_ISR 0x0C
  26. #define CE_TLR 0x10
  27. #define CE_TSR 0x14
  28. #define CE_ESR 0x18
  29. #define CE_CSSGR 0x1C
  30. #define CE_CDSGR 0x20
  31. #define CE_CSAR 0x24
  32. #define CE_CDAR 0x28
  33. #define CE_TPR 0x2C
  34. /* Used in struct ce_task */
  35. /* ce_task common */
  36. #define CE_ENCRYPTION 0
  37. #define CE_DECRYPTION BIT(8)
  38. #define CE_COMM_INT BIT(31)
  39. /* ce_task symmetric */
  40. #define CE_AES_128BITS 0
  41. #define CE_AES_192BITS 1
  42. #define CE_AES_256BITS 2
  43. #define CE_OP_ECB 0
  44. #define CE_OP_CBC (1 << 8)
  45. #define CE_ALG_AES 0
  46. #define CE_ALG_DES 1
  47. #define CE_ALG_3DES 2
  48. #define CE_ALG_MD5 16
  49. #define CE_ALG_SHA1 17
  50. #define CE_ALG_SHA224 18
  51. #define CE_ALG_SHA256 19
  52. #define CE_ALG_SHA384 20
  53. #define CE_ALG_SHA512 21
  54. #define CE_ALG_TRNG 48
  55. #define CE_ALG_PRNG 49
  56. #define CE_ALG_TRNG_V2 0x1c
  57. #define CE_ALG_PRNG_V2 0x1d
  58. /* Used in ce_variant */
  59. #define CE_ID_NOTSUPP 0xFF
  60. #define CE_ID_CIPHER_AES 0
  61. #define CE_ID_CIPHER_DES 1
  62. #define CE_ID_CIPHER_DES3 2
  63. #define CE_ID_CIPHER_MAX 3
  64. #define CE_ID_HASH_MD5 0
  65. #define CE_ID_HASH_SHA1 1
  66. #define CE_ID_HASH_SHA224 2
  67. #define CE_ID_HASH_SHA256 3
  68. #define CE_ID_HASH_SHA384 4
  69. #define CE_ID_HASH_SHA512 5
  70. #define CE_ID_HASH_MAX 6
  71. #define CE_ID_OP_ECB 0
  72. #define CE_ID_OP_CBC 1
  73. #define CE_ID_OP_MAX 2
  74. /* Used in CE registers */
  75. #define CE_ERR_ALGO_NOTSUP BIT(0)
  76. #define CE_ERR_DATALEN BIT(1)
  77. #define CE_ERR_KEYSRAM BIT(2)
  78. #define CE_ERR_ADDR_INVALID BIT(5)
  79. #define CE_ERR_KEYLADDER BIT(6)
  80. #define ESR_H3 0
  81. #define ESR_A64 1
  82. #define ESR_R40 2
  83. #define ESR_H5 3
  84. #define ESR_H6 4
  85. #define ESR_D1 5
  86. #define PRNG_DATA_SIZE (160 / 8)
  87. #define PRNG_SEED_SIZE DIV_ROUND_UP(175, 8)
  88. #define PRNG_LD BIT(17)
  89. #define CE_DIE_ID_SHIFT 16
  90. #define CE_DIE_ID_MASK 0x07
  91. #define MAX_SG 8
  92. #define CE_MAX_CLOCKS 3
  93. #define MAXFLOW 4
  94. /*
  95. * struct ce_clock - Describe clocks used by sun8i-ce
  96. * @name: Name of clock needed by this variant
  97. * @freq: Frequency to set for each clock
  98. * @max_freq: Maximum frequency for each clock (generally given by datasheet)
  99. */
  100. struct ce_clock {
  101. const char *name;
  102. unsigned long freq;
  103. unsigned long max_freq;
  104. };
  105. /*
  106. * struct ce_variant - Describe CE capability for each variant hardware
  107. * @alg_cipher: list of supported ciphers. for each CE_ID_ this will give the
  108. * coresponding CE_ALG_XXX value
  109. * @alg_hash: list of supported hashes. for each CE_ID_ this will give the
  110. * corresponding CE_ALG_XXX value
  111. * @op_mode: list of supported block modes
  112. * @cipher_t_dlen_in_bytes: Does the request size for cipher is in
  113. * bytes or words
  114. * @hash_t_dlen_in_bytes: Does the request size for hash is in
  115. * bits or words
  116. * @prng_t_dlen_in_bytes: Does the request size for PRNG is in
  117. * bytes or words
  118. * @trng_t_dlen_in_bytes: Does the request size for TRNG is in
  119. * bytes or words
  120. * @ce_clks: list of clocks needed by this variant
  121. * @esr: The type of error register
  122. * @prng: The CE_ALG_XXX value for the PRNG
  123. * @trng: The CE_ALG_XXX value for the TRNG
  124. */
  125. struct ce_variant {
  126. char alg_cipher[CE_ID_CIPHER_MAX];
  127. char alg_hash[CE_ID_HASH_MAX];
  128. u32 op_mode[CE_ID_OP_MAX];
  129. bool cipher_t_dlen_in_bytes;
  130. bool hash_t_dlen_in_bits;
  131. bool prng_t_dlen_in_bytes;
  132. bool trng_t_dlen_in_bytes;
  133. struct ce_clock ce_clks[CE_MAX_CLOCKS];
  134. int esr;
  135. unsigned char prng;
  136. unsigned char trng;
  137. };
  138. struct sginfo {
  139. __le32 addr;
  140. __le32 len;
  141. } __packed;
  142. /*
  143. * struct ce_task - CE Task descriptor
  144. * The structure of this descriptor could be found in the datasheet
  145. */
  146. struct ce_task {
  147. __le32 t_id;
  148. __le32 t_common_ctl;
  149. __le32 t_sym_ctl;
  150. __le32 t_asym_ctl;
  151. __le32 t_key;
  152. __le32 t_iv;
  153. __le32 t_ctr;
  154. __le32 t_dlen;
  155. struct sginfo t_src[MAX_SG];
  156. struct sginfo t_dst[MAX_SG];
  157. __le32 next;
  158. __le32 reserved[3];
  159. } __packed __aligned(8);
  160. /*
  161. * struct sun8i_ce_flow - Information used by each flow
  162. * @engine: ptr to the crypto_engine for this flow
  163. * @complete: completion for the current task on this flow
  164. * @status: set to 1 by interrupt if task is done
  165. * @t_phy: Physical address of task
  166. * @tl: pointer to the current ce_task for this flow
  167. * @backup_iv: buffer which contain the next IV to store
  168. * @bounce_iv: buffer which contain the IV
  169. * @stat_req: number of request done by this flow
  170. */
  171. struct sun8i_ce_flow {
  172. struct crypto_engine *engine;
  173. struct completion complete;
  174. int status;
  175. dma_addr_t t_phy;
  176. int timeout;
  177. struct ce_task *tl;
  178. void *backup_iv;
  179. void *bounce_iv;
  180. #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
  181. unsigned long stat_req;
  182. #endif
  183. };
  184. /*
  185. * struct sun8i_ce_dev - main container for all this driver information
  186. * @base: base address of CE
  187. * @ceclks: clocks used by CE
  188. * @reset: pointer to reset controller
  189. * @dev: the platform device
  190. * @mlock: Control access to device registers
  191. * @rnglock: Control access to the RNG (dedicated channel 3)
  192. * @chanlist: array of all flow
  193. * @flow: flow to use in next request
  194. * @variant: pointer to variant specific data
  195. * @dbgfs_dir: Debugfs dentry for statistic directory
  196. * @dbgfs_stats: Debugfs dentry for statistic counters
  197. */
  198. struct sun8i_ce_dev {
  199. void __iomem *base;
  200. struct clk *ceclks[CE_MAX_CLOCKS];
  201. struct reset_control *reset;
  202. struct device *dev;
  203. struct mutex mlock;
  204. struct mutex rnglock;
  205. struct sun8i_ce_flow *chanlist;
  206. atomic_t flow;
  207. const struct ce_variant *variant;
  208. #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
  209. struct dentry *dbgfs_dir;
  210. struct dentry *dbgfs_stats;
  211. #endif
  212. #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_TRNG
  213. struct hwrng trng;
  214. #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
  215. unsigned long hwrng_stat_req;
  216. unsigned long hwrng_stat_bytes;
  217. #endif
  218. #endif
  219. };
  220. /*
  221. * struct sun8i_cipher_req_ctx - context for a skcipher request
  222. * @op_dir: direction (encrypt vs decrypt) for this request
  223. * @flow: the flow to use for this request
  224. * @ivlen: size of bounce_iv
  225. * @nr_sgs: The number of source SG (as given by dma_map_sg())
  226. * @nr_sgd: The number of destination SG (as given by dma_map_sg())
  227. * @addr_iv: The IV addr returned by dma_map_single, need to unmap later
  228. * @addr_key: The key addr returned by dma_map_single, need to unmap later
  229. * @fallback_req: request struct for invoking the fallback skcipher TFM
  230. */
  231. struct sun8i_cipher_req_ctx {
  232. u32 op_dir;
  233. int flow;
  234. unsigned int ivlen;
  235. int nr_sgs;
  236. int nr_sgd;
  237. dma_addr_t addr_iv;
  238. dma_addr_t addr_key;
  239. struct skcipher_request fallback_req; // keep at the end
  240. };
  241. /*
  242. * struct sun8i_cipher_tfm_ctx - context for a skcipher TFM
  243. * @enginectx: crypto_engine used by this TFM
  244. * @key: pointer to key data
  245. * @keylen: len of the key
  246. * @ce: pointer to the private data of driver handling this TFM
  247. * @fallback_tfm: pointer to the fallback TFM
  248. */
  249. struct sun8i_cipher_tfm_ctx {
  250. struct crypto_engine_ctx enginectx;
  251. u32 *key;
  252. u32 keylen;
  253. struct sun8i_ce_dev *ce;
  254. struct crypto_skcipher *fallback_tfm;
  255. };
  256. /*
  257. * struct sun8i_ce_hash_tfm_ctx - context for an ahash TFM
  258. * @enginectx: crypto_engine used by this TFM
  259. * @ce: pointer to the private data of driver handling this TFM
  260. * @fallback_tfm: pointer to the fallback TFM
  261. */
  262. struct sun8i_ce_hash_tfm_ctx {
  263. struct crypto_engine_ctx enginectx;
  264. struct sun8i_ce_dev *ce;
  265. struct crypto_ahash *fallback_tfm;
  266. };
  267. /*
  268. * struct sun8i_ce_hash_reqctx - context for an ahash request
  269. * @fallback_req: pre-allocated fallback request
  270. * @flow: the flow to use for this request
  271. */
  272. struct sun8i_ce_hash_reqctx {
  273. struct ahash_request fallback_req;
  274. int flow;
  275. };
  276. /*
  277. * struct sun8i_ce_prng_ctx - context for PRNG TFM
  278. * @seed: The seed to use
  279. * @slen: The size of the seed
  280. */
  281. struct sun8i_ce_rng_tfm_ctx {
  282. void *seed;
  283. unsigned int slen;
  284. };
  285. /*
  286. * struct sun8i_ce_alg_template - crypto_alg template
  287. * @type: the CRYPTO_ALG_TYPE for this template
  288. * @ce_algo_id: the CE_ID for this template
  289. * @ce_blockmode: the type of block operation CE_ID
  290. * @ce: pointer to the sun8i_ce_dev structure associated with
  291. * this template
  292. * @alg: one of sub struct must be used
  293. * @stat_req: number of request done on this template
  294. * @stat_fb: number of request which has fallbacked
  295. * @stat_bytes: total data size done by this template
  296. */
  297. struct sun8i_ce_alg_template {
  298. u32 type;
  299. u32 ce_algo_id;
  300. u32 ce_blockmode;
  301. struct sun8i_ce_dev *ce;
  302. union {
  303. struct skcipher_alg skcipher;
  304. struct ahash_alg hash;
  305. struct rng_alg rng;
  306. } alg;
  307. unsigned long stat_req;
  308. unsigned long stat_fb;
  309. unsigned long stat_bytes;
  310. unsigned long stat_fb_maxsg;
  311. unsigned long stat_fb_leniv;
  312. unsigned long stat_fb_len0;
  313. unsigned long stat_fb_mod16;
  314. unsigned long stat_fb_srcali;
  315. unsigned long stat_fb_srclen;
  316. unsigned long stat_fb_dstali;
  317. unsigned long stat_fb_dstlen;
  318. char fbname[CRYPTO_MAX_ALG_NAME];
  319. };
  320. int sun8i_ce_enqueue(struct crypto_async_request *areq, u32 type);
  321. int sun8i_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
  322. unsigned int keylen);
  323. int sun8i_ce_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
  324. unsigned int keylen);
  325. int sun8i_ce_cipher_init(struct crypto_tfm *tfm);
  326. void sun8i_ce_cipher_exit(struct crypto_tfm *tfm);
  327. int sun8i_ce_skdecrypt(struct skcipher_request *areq);
  328. int sun8i_ce_skencrypt(struct skcipher_request *areq);
  329. int sun8i_ce_get_engine_number(struct sun8i_ce_dev *ce);
  330. int sun8i_ce_run_task(struct sun8i_ce_dev *ce, int flow, const char *name);
  331. int sun8i_ce_hash_crainit(struct crypto_tfm *tfm);
  332. void sun8i_ce_hash_craexit(struct crypto_tfm *tfm);
  333. int sun8i_ce_hash_init(struct ahash_request *areq);
  334. int sun8i_ce_hash_export(struct ahash_request *areq, void *out);
  335. int sun8i_ce_hash_import(struct ahash_request *areq, const void *in);
  336. int sun8i_ce_hash(struct ahash_request *areq);
  337. int sun8i_ce_hash_final(struct ahash_request *areq);
  338. int sun8i_ce_hash_update(struct ahash_request *areq);
  339. int sun8i_ce_hash_finup(struct ahash_request *areq);
  340. int sun8i_ce_hash_digest(struct ahash_request *areq);
  341. int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq);
  342. int sun8i_ce_prng_generate(struct crypto_rng *tfm, const u8 *src,
  343. unsigned int slen, u8 *dst, unsigned int dlen);
  344. int sun8i_ce_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
  345. void sun8i_ce_prng_exit(struct crypto_tfm *tfm);
  346. int sun8i_ce_prng_init(struct crypto_tfm *tfm);
  347. int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce);
  348. void sun8i_ce_hwrng_unregister(struct sun8i_ce_dev *ce);