sun8i-ss.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * sun8i-ss.h - hardware cryptographic offloader for
  4. * Allwinner A80/A83T 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/rng.h>
  12. #include <crypto/skcipher.h>
  13. #include <linux/atomic.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/crypto.h>
  16. #include <crypto/internal/hash.h>
  17. #include <crypto/md5.h>
  18. #include <crypto/sha1.h>
  19. #include <crypto/sha2.h>
  20. #define SS_START 1
  21. #define SS_ENCRYPTION 0
  22. #define SS_DECRYPTION BIT(6)
  23. #define SS_ALG_AES 0
  24. #define SS_ALG_DES (1 << 2)
  25. #define SS_ALG_3DES (2 << 2)
  26. #define SS_ALG_MD5 (3 << 2)
  27. #define SS_ALG_PRNG (4 << 2)
  28. #define SS_ALG_SHA1 (6 << 2)
  29. #define SS_ALG_SHA224 (7 << 2)
  30. #define SS_ALG_SHA256 (8 << 2)
  31. #define SS_CTL_REG 0x00
  32. #define SS_INT_CTL_REG 0x04
  33. #define SS_INT_STA_REG 0x08
  34. #define SS_KEY_ADR_REG 0x10
  35. #define SS_IV_ADR_REG 0x18
  36. #define SS_SRC_ADR_REG 0x20
  37. #define SS_DST_ADR_REG 0x28
  38. #define SS_LEN_ADR_REG 0x30
  39. #define SS_ID_NOTSUPP 0xFF
  40. #define SS_ID_CIPHER_AES 0
  41. #define SS_ID_CIPHER_DES 1
  42. #define SS_ID_CIPHER_DES3 2
  43. #define SS_ID_CIPHER_MAX 3
  44. #define SS_ID_OP_ECB 0
  45. #define SS_ID_OP_CBC 1
  46. #define SS_ID_OP_MAX 2
  47. #define SS_AES_128BITS 0
  48. #define SS_AES_192BITS 1
  49. #define SS_AES_256BITS 2
  50. #define SS_OP_ECB 0
  51. #define SS_OP_CBC (1 << 13)
  52. #define SS_ID_HASH_MD5 0
  53. #define SS_ID_HASH_SHA1 1
  54. #define SS_ID_HASH_SHA224 2
  55. #define SS_ID_HASH_SHA256 3
  56. #define SS_ID_HASH_MAX 4
  57. #define SS_FLOW0 BIT(30)
  58. #define SS_FLOW1 BIT(31)
  59. #define SS_PRNG_CONTINUE BIT(18)
  60. #define MAX_SG 8
  61. #define MAXFLOW 2
  62. #define SS_MAX_CLOCKS 2
  63. #define SS_DIE_ID_SHIFT 20
  64. #define SS_DIE_ID_MASK 0x07
  65. #define PRNG_DATA_SIZE (160 / 8)
  66. #define PRNG_SEED_SIZE DIV_ROUND_UP(175, 8)
  67. #define MAX_PAD_SIZE 4096
  68. /*
  69. * struct ss_clock - Describe clocks used by sun8i-ss
  70. * @name: Name of clock needed by this variant
  71. * @freq: Frequency to set for each clock
  72. * @max_freq: Maximum frequency for each clock
  73. */
  74. struct ss_clock {
  75. const char *name;
  76. unsigned long freq;
  77. unsigned long max_freq;
  78. };
  79. /*
  80. * struct ss_variant - Describe SS capability for each variant hardware
  81. * @alg_cipher: list of supported ciphers. for each SS_ID_ this will give the
  82. * coresponding SS_ALG_XXX value
  83. * @alg_hash: list of supported hashes. for each SS_ID_ this will give the
  84. * corresponding SS_ALG_XXX value
  85. * @op_mode: list of supported block modes
  86. * @ss_clks: list of clock needed by this variant
  87. */
  88. struct ss_variant {
  89. char alg_cipher[SS_ID_CIPHER_MAX];
  90. char alg_hash[SS_ID_HASH_MAX];
  91. u32 op_mode[SS_ID_OP_MAX];
  92. struct ss_clock ss_clks[SS_MAX_CLOCKS];
  93. };
  94. struct sginfo {
  95. u32 addr;
  96. u32 len;
  97. };
  98. /*
  99. * struct sun8i_ss_flow - Information used by each flow
  100. * @engine: ptr to the crypto_engine for this flow
  101. * @complete: completion for the current task on this flow
  102. * @status: set to 1 by interrupt if task is done
  103. * @stat_req: number of request done by this flow
  104. * @iv: list of IV to use for each step
  105. * @biv: buffer which contain the backuped IV
  106. * @pad: padding buffer for hash operations
  107. * @result: buffer for storing the result of hash operations
  108. */
  109. struct sun8i_ss_flow {
  110. struct crypto_engine *engine;
  111. struct completion complete;
  112. int status;
  113. u8 *iv[MAX_SG];
  114. u8 *biv;
  115. void *pad;
  116. void *result;
  117. #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
  118. unsigned long stat_req;
  119. #endif
  120. };
  121. /*
  122. * struct sun8i_ss_dev - main container for all this driver information
  123. * @base: base address of SS
  124. * @ssclks: clocks used by SS
  125. * @reset: pointer to reset controller
  126. * @dev: the platform device
  127. * @mlock: Control access to device registers
  128. * @flows: array of all flow
  129. * @flow: flow to use in next request
  130. * @variant: pointer to variant specific data
  131. * @dbgfs_dir: Debugfs dentry for statistic directory
  132. * @dbgfs_stats: Debugfs dentry for statistic counters
  133. */
  134. struct sun8i_ss_dev {
  135. void __iomem *base;
  136. struct clk *ssclks[SS_MAX_CLOCKS];
  137. struct reset_control *reset;
  138. struct device *dev;
  139. struct mutex mlock;
  140. struct sun8i_ss_flow *flows;
  141. atomic_t flow;
  142. const struct ss_variant *variant;
  143. #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG
  144. struct dentry *dbgfs_dir;
  145. struct dentry *dbgfs_stats;
  146. #endif
  147. };
  148. /*
  149. * struct sun8i_cipher_req_ctx - context for a skcipher request
  150. * @t_src: list of mapped SGs with their size
  151. * @t_dst: list of mapped SGs with their size
  152. * @p_key: DMA address of the key
  153. * @p_iv: DMA address of the IVs
  154. * @niv: Number of IVs DMA mapped
  155. * @method: current algorithm for this request
  156. * @op_mode: op_mode for this request
  157. * @op_dir: direction (encrypt vs decrypt) for this request
  158. * @flow: the flow to use for this request
  159. * @ivlen: size of IVs
  160. * @keylen: keylen for this request
  161. * @fallback_req: request struct for invoking the fallback skcipher TFM
  162. */
  163. struct sun8i_cipher_req_ctx {
  164. struct sginfo t_src[MAX_SG];
  165. struct sginfo t_dst[MAX_SG];
  166. u32 p_key;
  167. u32 p_iv[MAX_SG];
  168. int niv;
  169. u32 method;
  170. u32 op_mode;
  171. u32 op_dir;
  172. int flow;
  173. unsigned int ivlen;
  174. unsigned int keylen;
  175. struct skcipher_request fallback_req; // keep at the end
  176. };
  177. /*
  178. * struct sun8i_cipher_tfm_ctx - context for a skcipher TFM
  179. * @enginectx: crypto_engine used by this TFM
  180. * @key: pointer to key data
  181. * @keylen: len of the key
  182. * @ss: pointer to the private data of driver handling this TFM
  183. * @fallback_tfm: pointer to the fallback TFM
  184. *
  185. * enginectx must be the first element
  186. */
  187. struct sun8i_cipher_tfm_ctx {
  188. struct crypto_engine_ctx enginectx;
  189. u32 *key;
  190. u32 keylen;
  191. struct sun8i_ss_dev *ss;
  192. struct crypto_skcipher *fallback_tfm;
  193. };
  194. /*
  195. * struct sun8i_ss_prng_ctx - context for PRNG TFM
  196. * @seed: The seed to use
  197. * @slen: The size of the seed
  198. */
  199. struct sun8i_ss_rng_tfm_ctx {
  200. void *seed;
  201. unsigned int slen;
  202. };
  203. /*
  204. * struct sun8i_ss_hash_tfm_ctx - context for an ahash TFM
  205. * @enginectx: crypto_engine used by this TFM
  206. * @fallback_tfm: pointer to the fallback TFM
  207. * @ss: pointer to the private data of driver handling this TFM
  208. *
  209. * enginectx must be the first element
  210. */
  211. struct sun8i_ss_hash_tfm_ctx {
  212. struct crypto_engine_ctx enginectx;
  213. struct crypto_ahash *fallback_tfm;
  214. struct sun8i_ss_dev *ss;
  215. u8 *ipad;
  216. u8 *opad;
  217. u8 key[SHA256_BLOCK_SIZE];
  218. int keylen;
  219. };
  220. /*
  221. * struct sun8i_ss_hash_reqctx - context for an ahash request
  222. * @t_src: list of DMA address and size for source SGs
  223. * @t_dst: list of DMA address and size for destination SGs
  224. * @fallback_req: pre-allocated fallback request
  225. * @method: the register value for the algorithm used by this request
  226. * @flow: the flow to use for this request
  227. */
  228. struct sun8i_ss_hash_reqctx {
  229. struct sginfo t_src[MAX_SG];
  230. struct sginfo t_dst[MAX_SG];
  231. struct ahash_request fallback_req;
  232. u32 method;
  233. int flow;
  234. };
  235. /*
  236. * struct sun8i_ss_alg_template - crypto_alg template
  237. * @type: the CRYPTO_ALG_TYPE for this template
  238. * @ss_algo_id: the SS_ID for this template
  239. * @ss_blockmode: the type of block operation SS_ID
  240. * @ss: pointer to the sun8i_ss_dev structure associated with
  241. * this template
  242. * @alg: one of sub struct must be used
  243. * @stat_req: number of request done on this template
  244. * @stat_fb: number of request which has fallbacked
  245. * @stat_bytes: total data size done by this template
  246. */
  247. struct sun8i_ss_alg_template {
  248. u32 type;
  249. u32 ss_algo_id;
  250. u32 ss_blockmode;
  251. struct sun8i_ss_dev *ss;
  252. union {
  253. struct skcipher_alg skcipher;
  254. struct rng_alg rng;
  255. struct ahash_alg hash;
  256. } alg;
  257. unsigned long stat_req;
  258. unsigned long stat_fb;
  259. unsigned long stat_bytes;
  260. unsigned long stat_fb_len;
  261. unsigned long stat_fb_sglen;
  262. unsigned long stat_fb_align;
  263. unsigned long stat_fb_sgnum;
  264. char fbname[CRYPTO_MAX_ALG_NAME];
  265. };
  266. int sun8i_ss_enqueue(struct crypto_async_request *areq, u32 type);
  267. int sun8i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
  268. unsigned int keylen);
  269. int sun8i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
  270. unsigned int keylen);
  271. int sun8i_ss_cipher_init(struct crypto_tfm *tfm);
  272. void sun8i_ss_cipher_exit(struct crypto_tfm *tfm);
  273. int sun8i_ss_skdecrypt(struct skcipher_request *areq);
  274. int sun8i_ss_skencrypt(struct skcipher_request *areq);
  275. int sun8i_ss_get_engine_number(struct sun8i_ss_dev *ss);
  276. int sun8i_ss_run_task(struct sun8i_ss_dev *ss, struct sun8i_cipher_req_ctx *rctx, const char *name);
  277. int sun8i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
  278. unsigned int slen, u8 *dst, unsigned int dlen);
  279. int sun8i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);
  280. int sun8i_ss_prng_init(struct crypto_tfm *tfm);
  281. void sun8i_ss_prng_exit(struct crypto_tfm *tfm);
  282. int sun8i_ss_hash_crainit(struct crypto_tfm *tfm);
  283. void sun8i_ss_hash_craexit(struct crypto_tfm *tfm);
  284. int sun8i_ss_hash_init(struct ahash_request *areq);
  285. int sun8i_ss_hash_export(struct ahash_request *areq, void *out);
  286. int sun8i_ss_hash_import(struct ahash_request *areq, const void *in);
  287. int sun8i_ss_hash_final(struct ahash_request *areq);
  288. int sun8i_ss_hash_update(struct ahash_request *areq);
  289. int sun8i_ss_hash_finup(struct ahash_request *areq);
  290. int sun8i_ss_hash_digest(struct ahash_request *areq);
  291. int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq);
  292. int sun8i_ss_hmac_setkey(struct crypto_ahash *ahash, const u8 *key,
  293. unsigned int keylen);