sl3516-ce.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * sl3516-ce.h - hardware cryptographic offloader for cortina/gemini SoC
  4. *
  5. * Copyright (C) 2021 Corentin LABBE <[email protected]>
  6. *
  7. * General notes on this driver:
  8. * Called either Crypto Acceleration Engine Module, Security Acceleration Engine
  9. * or IPSEC module in the datasheet, it will be called Crypto Engine for short
  10. * in this driver.
  11. * The CE was designed to handle IPSEC and wifi(TKIP WEP) protocol.
  12. * It can handle AES, DES, 3DES, MD5, WEP, TKIP, SHA1, HMAC(MD5), HMAC(SHA1),
  13. * Michael cipher/digest suites.
  14. * It acts the same as a network hw, with both RX and TX chained descriptors.
  15. */
  16. #include <crypto/aes.h>
  17. #include <crypto/engine.h>
  18. #include <crypto/scatterwalk.h>
  19. #include <crypto/skcipher.h>
  20. #include <linux/crypto.h>
  21. #include <linux/debugfs.h>
  22. #include <linux/hw_random.h>
  23. #define TQ0_TYPE_DATA 0
  24. #define TQ0_TYPE_CTRL BIT(0)
  25. #define TQ1_CIPHER BIT(1)
  26. #define TQ2_AUTH BIT(2)
  27. #define TQ3_IV BIT(3)
  28. #define TQ4_KEY0 BIT(4)
  29. #define TQ5_KEY4 BIT(5)
  30. #define TQ6_KEY6 BIT(6)
  31. #define TQ7_AKEY0 BIT(7)
  32. #define TQ8_AKEY2 BIT(8)
  33. #define TQ9_AKEY2 BIT(9)
  34. #define ECB_AES 0x2
  35. #define DESC_LAST 0x01
  36. #define DESC_FIRST 0x02
  37. #define IPSEC_ID 0x0000
  38. #define IPSEC_STATUS_REG 0x00a8
  39. #define IPSEC_RAND_NUM_REG 0x00ac
  40. #define IPSEC_DMA_DEVICE_ID 0xff00
  41. #define IPSEC_DMA_STATUS 0xff04
  42. #define IPSEC_TXDMA_CTRL 0xff08
  43. #define IPSEC_TXDMA_FIRST_DESC 0xff0c
  44. #define IPSEC_TXDMA_CURR_DESC 0xff10
  45. #define IPSEC_RXDMA_CTRL 0xff14
  46. #define IPSEC_RXDMA_FIRST_DESC 0xff18
  47. #define IPSEC_RXDMA_CURR_DESC 0xff1c
  48. #define IPSEC_TXDMA_BUF_ADDR 0xff28
  49. #define IPSEC_RXDMA_BUF_ADDR 0xff38
  50. #define IPSEC_RXDMA_BUF_SIZE 0xff30
  51. #define CE_ENCRYPTION 0x01
  52. #define CE_DECRYPTION 0x03
  53. #define MAXDESC 6
  54. #define DMA_STATUS_RS_EOFI BIT(22)
  55. #define DMA_STATUS_RS_PERR BIT(24)
  56. #define DMA_STATUS_RS_DERR BIT(25)
  57. #define DMA_STATUS_TS_EOFI BIT(27)
  58. #define DMA_STATUS_TS_PERR BIT(29)
  59. #define DMA_STATUS_TS_DERR BIT(30)
  60. #define TXDMA_CTRL_START BIT(31)
  61. #define TXDMA_CTRL_CONTINUE BIT(30)
  62. #define TXDMA_CTRL_CHAIN_MODE BIT(29)
  63. /* the burst value is not documented in the datasheet */
  64. #define TXDMA_CTRL_BURST_UNK BIT(22)
  65. #define TXDMA_CTRL_INT_FAIL BIT(17)
  66. #define TXDMA_CTRL_INT_PERR BIT(16)
  67. #define RXDMA_CTRL_START BIT(31)
  68. #define RXDMA_CTRL_CONTINUE BIT(30)
  69. #define RXDMA_CTRL_CHAIN_MODE BIT(29)
  70. /* the burst value is not documented in the datasheet */
  71. #define RXDMA_CTRL_BURST_UNK BIT(22)
  72. #define RXDMA_CTRL_INT_FINISH BIT(18)
  73. #define RXDMA_CTRL_INT_FAIL BIT(17)
  74. #define RXDMA_CTRL_INT_PERR BIT(16)
  75. #define RXDMA_CTRL_INT_EOD BIT(15)
  76. #define RXDMA_CTRL_INT_EOF BIT(14)
  77. #define CE_CPU 0
  78. #define CE_DMA 1
  79. /*
  80. * struct sl3516_ce_descriptor - descriptor for CE operations
  81. * @frame_ctrl: Information for the current descriptor
  82. * @flag_status: For send packet, describe flag of operations.
  83. * @buf_adr: pointer to a send/recv buffer for data packet
  84. * @next_desc: control linking to other descriptors
  85. */
  86. struct descriptor {
  87. union {
  88. u32 raw;
  89. /*
  90. * struct desc_frame_ctrl - Information for the current descriptor
  91. * @buffer_size: the size of buffer at buf_adr
  92. * @desc_count: Upon completion of a DMA operation, DMA
  93. * write the number of descriptors used
  94. * for the current frame
  95. * @checksum: unknown
  96. * @authcomp: unknown
  97. * @perr: Protocol error during processing this descriptor
  98. * @derr: Data error during processing this descriptor
  99. * @own: 0 if owned by CPU, 1 for DMA
  100. */
  101. struct desc_frame_ctrl {
  102. u32 buffer_size :16;
  103. u32 desc_count :6;
  104. u32 checksum :6;
  105. u32 authcomp :1;
  106. u32 perr :1;
  107. u32 derr :1;
  108. u32 own :1;
  109. } bits;
  110. } frame_ctrl;
  111. union {
  112. u32 raw;
  113. /*
  114. * struct desc_flag_status - flag for this descriptor
  115. * @tqflag: list of flag describing the type of operation
  116. * to be performed.
  117. */
  118. struct desc_tx_flag_status {
  119. u32 tqflag :10;
  120. u32 unused :22;
  121. } tx_flag;
  122. } flag_status;
  123. u32 buf_adr;
  124. union {
  125. u32 next_descriptor;
  126. /*
  127. * struct desc_next - describe chaining of descriptors
  128. * @sof_eof: does the descriptor is first (0x11),
  129. * the last (0x01), middle of a chan (0x00)
  130. * or the only one (0x11)
  131. * @dec: AHB bus address increase (0), decrease (1)
  132. * @eofie: End of frame interrupt enable
  133. * @ndar: Next descriptor address
  134. */
  135. struct desc_next {
  136. u32 sof_eof :2;
  137. u32 dec :1;
  138. u32 eofie :1;
  139. u32 ndar :28;
  140. } bits;
  141. } next_desc;
  142. };
  143. /*
  144. * struct control - The value of this register is used to set the
  145. * operation mode of the IPSec Module.
  146. * @process_id: Used to identify the process. The number will be copied
  147. * to the descriptor status of the received packet.
  148. * @auth_check_len: Number of 32-bit words to be checked or appended by the
  149. * authentication module
  150. * @auth_algorithm:
  151. * @auth_mode: 0:append 1:Check Authentication Result
  152. * @fcs_stream_copy: 0:enable 1:disable authentication stream copy
  153. * @mix_key_sel: 0:use rCipherKey0-3 1:use Key Mixer
  154. * @aesnk: AES Key Size
  155. * @cipher_algorithm: choice of CBC/ECE and AES/DES/3DES
  156. * @op_mode: Operation Mode for the IPSec Module
  157. */
  158. struct pkt_control_header {
  159. u32 process_id :8;
  160. u32 auth_check_len :3;
  161. u32 un1 :1;
  162. u32 auth_algorithm :3;
  163. u32 auth_mode :1;
  164. u32 fcs_stream_copy :1;
  165. u32 un2 :2;
  166. u32 mix_key_sel :1;
  167. u32 aesnk :4;
  168. u32 cipher_algorithm :3;
  169. u32 un3 :1;
  170. u32 op_mode :4;
  171. };
  172. struct pkt_control_cipher {
  173. u32 algorithm_len :16;
  174. u32 header_len :16;
  175. };
  176. /*
  177. * struct pkt_control_ecb - control packet for ECB
  178. */
  179. struct pkt_control_ecb {
  180. struct pkt_control_header control;
  181. struct pkt_control_cipher cipher;
  182. unsigned char key[AES_MAX_KEY_SIZE];
  183. };
  184. /*
  185. * struct sl3516_ce_dev - main container for all this driver information
  186. * @base: base address
  187. * @clks: clocks used
  188. * @reset: pointer to reset controller
  189. * @dev: the platform device
  190. * @engine: ptr to the crypto/crypto_engine
  191. * @complete: completion for the current task on this flow
  192. * @status: set to 1 by interrupt if task is done
  193. * @dtx: base DMA address for TX descriptors
  194. * @tx base address of TX descriptors
  195. * @drx: base DMA address for RX descriptors
  196. * @rx base address of RX descriptors
  197. * @ctx current used TX descriptor
  198. * @crx current used RX descriptor
  199. * @trng hw_random structure for RNG
  200. * @hwrng_stat_req number of HWRNG requests
  201. * @hwrng_stat_bytes total number of bytes generated by RNG
  202. * @stat_irq number of IRQ handled by CE
  203. * @stat_irq_tx number of TX IRQ handled by CE
  204. * @stat_irq_rx number of RX IRQ handled by CE
  205. * @stat_req number of requests handled by CE
  206. * @fallbak_sg_count_tx number of fallback due to destination SG count
  207. * @fallbak_sg_count_rx number of fallback due to source SG count
  208. * @fallbak_not_same_len number of fallback due to difference in SG length
  209. * @dbgfs_dir: Debugfs dentry for statistic directory
  210. * @dbgfs_stats: Debugfs dentry for statistic counters
  211. */
  212. struct sl3516_ce_dev {
  213. void __iomem *base;
  214. struct clk *clks;
  215. struct reset_control *reset;
  216. struct device *dev;
  217. struct crypto_engine *engine;
  218. struct completion complete;
  219. int status;
  220. dma_addr_t dtx;
  221. struct descriptor *tx;
  222. dma_addr_t drx;
  223. struct descriptor *rx;
  224. int ctx;
  225. int crx;
  226. struct hwrng trng;
  227. unsigned long hwrng_stat_req;
  228. unsigned long hwrng_stat_bytes;
  229. unsigned long stat_irq;
  230. unsigned long stat_irq_tx;
  231. unsigned long stat_irq_rx;
  232. unsigned long stat_req;
  233. unsigned long fallback_sg_count_tx;
  234. unsigned long fallback_sg_count_rx;
  235. unsigned long fallback_not_same_len;
  236. unsigned long fallback_mod16;
  237. unsigned long fallback_align16;
  238. #ifdef CONFIG_CRYPTO_DEV_SL3516_DEBUG
  239. struct dentry *dbgfs_dir;
  240. struct dentry *dbgfs_stats;
  241. #endif
  242. void *pctrl;
  243. dma_addr_t dctrl;
  244. };
  245. struct sginfo {
  246. u32 addr;
  247. u32 len;
  248. };
  249. /*
  250. * struct sl3516_ce_cipher_req_ctx - context for a skcipher request
  251. * @t_src: list of mapped SGs with their size
  252. * @t_dst: list of mapped SGs with their size
  253. * @op_dir: direction (encrypt vs decrypt) for this request
  254. * @pctrllen: the length of the ctrl packet
  255. * @tqflag: the TQflag to set in data packet
  256. * @h pointer to the pkt_control_cipher header
  257. * @nr_sgs: number of source SG
  258. * @nr_sgd: number of destination SG
  259. * @fallback_req: request struct for invoking the fallback skcipher TFM
  260. */
  261. struct sl3516_ce_cipher_req_ctx {
  262. struct sginfo t_src[MAXDESC];
  263. struct sginfo t_dst[MAXDESC];
  264. u32 op_dir;
  265. unsigned int pctrllen;
  266. u32 tqflag;
  267. struct pkt_control_cipher *h;
  268. int nr_sgs;
  269. int nr_sgd;
  270. struct skcipher_request fallback_req; // keep at the end
  271. };
  272. /*
  273. * struct sl3516_ce_cipher_tfm_ctx - context for a skcipher TFM
  274. * @enginectx: crypto_engine used by this TFM
  275. * @key: pointer to key data
  276. * @keylen: len of the key
  277. * @ce: pointer to the private data of driver handling this TFM
  278. * @fallback_tfm: pointer to the fallback TFM
  279. *
  280. * enginectx must be the first element
  281. */
  282. struct sl3516_ce_cipher_tfm_ctx {
  283. struct crypto_engine_ctx enginectx;
  284. u32 *key;
  285. u32 keylen;
  286. struct sl3516_ce_dev *ce;
  287. struct crypto_skcipher *fallback_tfm;
  288. };
  289. /*
  290. * struct sl3516_ce_alg_template - crypto_alg template
  291. * @type: the CRYPTO_ALG_TYPE for this template
  292. * @mode: value to be used in control packet for this algorithm
  293. * @ce: pointer to the sl3516_ce_dev structure associated with
  294. * this template
  295. * @alg: one of sub struct must be used
  296. * @stat_req: number of request done on this template
  297. * @stat_fb: number of request which has fallbacked
  298. * @stat_bytes: total data size done by this template
  299. */
  300. struct sl3516_ce_alg_template {
  301. u32 type;
  302. u32 mode;
  303. struct sl3516_ce_dev *ce;
  304. union {
  305. struct skcipher_alg skcipher;
  306. } alg;
  307. unsigned long stat_req;
  308. unsigned long stat_fb;
  309. unsigned long stat_bytes;
  310. };
  311. int sl3516_ce_enqueue(struct crypto_async_request *areq, u32 type);
  312. int sl3516_ce_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
  313. unsigned int keylen);
  314. int sl3516_ce_cipher_init(struct crypto_tfm *tfm);
  315. void sl3516_ce_cipher_exit(struct crypto_tfm *tfm);
  316. int sl3516_ce_skdecrypt(struct skcipher_request *areq);
  317. int sl3516_ce_skencrypt(struct skcipher_request *areq);
  318. int sl3516_ce_run_task(struct sl3516_ce_dev *ce,
  319. struct sl3516_ce_cipher_req_ctx *rctx, const char *name);
  320. int sl3516_ce_rng_register(struct sl3516_ce_dev *ce);
  321. void sl3516_ce_rng_unregister(struct sl3516_ce_dev *ce);