sl3516-ce.h 9.9 KB

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