sa2ul.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * AM6 SA2UL crypto accelerator driver
  4. *
  5. * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com
  6. *
  7. * Authors: Keerthy
  8. * Vitaly Andrianov
  9. */
  10. #ifndef _K3_SA2UL_
  11. #define _K3_SA2UL_
  12. #include <linux/interrupt.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/hw_random.h>
  15. #include <crypto/aes.h>
  16. #define SA_ENGINE_ENABLE_CONTROL 0x1000
  17. struct sa_tfm_ctx;
  18. /*
  19. * SA_ENGINE_ENABLE_CONTROL register bits
  20. */
  21. #define SA_EEC_ENCSS_EN 0x00000001
  22. #define SA_EEC_AUTHSS_EN 0x00000002
  23. #define SA_EEC_TRNG_EN 0x00000008
  24. #define SA_EEC_PKA_EN 0x00000010
  25. #define SA_EEC_CTXCACH_EN 0x00000080
  26. #define SA_EEC_CPPI_PORT_IN_EN 0x00000200
  27. #define SA_EEC_CPPI_PORT_OUT_EN 0x00000800
  28. /*
  29. * Encoding used to identify the typo of crypto operation
  30. * performed on the packet when the packet is returned
  31. * by SA
  32. */
  33. #define SA_REQ_SUBTYPE_ENC 0x0001
  34. #define SA_REQ_SUBTYPE_DEC 0x0002
  35. #define SA_REQ_SUBTYPE_SHIFT 16
  36. #define SA_REQ_SUBTYPE_MASK 0xffff
  37. /* Number of 32 bit words in EPIB */
  38. #define SA_DMA_NUM_EPIB_WORDS 4
  39. /* Number of 32 bit words in PS data */
  40. #define SA_DMA_NUM_PS_WORDS 16
  41. #define NKEY_SZ 3
  42. #define MCI_SZ 27
  43. /*
  44. * Maximum number of simultaeneous security contexts
  45. * supported by the driver
  46. */
  47. #define SA_MAX_NUM_CTX 512
  48. /*
  49. * Assumption: CTX size is multiple of 32
  50. */
  51. #define SA_CTX_SIZE_TO_DMA_SIZE(ctx_sz) \
  52. ((ctx_sz) ? ((ctx_sz) / 32 - 1) : 0)
  53. #define SA_CTX_ENC_KEY_OFFSET 32
  54. #define SA_CTX_ENC_AUX1_OFFSET 64
  55. #define SA_CTX_ENC_AUX2_OFFSET 96
  56. #define SA_CTX_ENC_AUX3_OFFSET 112
  57. #define SA_CTX_ENC_AUX4_OFFSET 128
  58. /* Next Engine Select code in CP_ACE */
  59. #define SA_ENG_ID_EM1 2 /* Enc/Dec engine with AES/DEC core */
  60. #define SA_ENG_ID_EM2 3 /* Encryption/Decryption enginefor pass 2 */
  61. #define SA_ENG_ID_AM1 4 /* Auth. engine with SHA1/MD5/SHA2 core */
  62. #define SA_ENG_ID_AM2 5 /* Authentication engine for pass 2 */
  63. #define SA_ENG_ID_OUTPORT2 20 /* Egress module 2 */
  64. #define SA_ENG_ID_NONE 0xff
  65. /*
  66. * Command Label Definitions
  67. */
  68. #define SA_CMDL_OFFSET_NESC 0 /* Next Engine Select Code */
  69. #define SA_CMDL_OFFSET_LABEL_LEN 1 /* Engine Command Label Length */
  70. /* 16-bit Length of Data to be processed */
  71. #define SA_CMDL_OFFSET_DATA_LEN 2
  72. #define SA_CMDL_OFFSET_DATA_OFFSET 4 /* Stat Data Offset */
  73. #define SA_CMDL_OFFSET_OPTION_CTRL1 5 /* Option Control Byte 1 */
  74. #define SA_CMDL_OFFSET_OPTION_CTRL2 6 /* Option Control Byte 2 */
  75. #define SA_CMDL_OFFSET_OPTION_CTRL3 7 /* Option Control Byte 3 */
  76. #define SA_CMDL_OFFSET_OPTION_BYTE 8
  77. #define SA_CMDL_HEADER_SIZE_BYTES 8
  78. #define SA_CMDL_OPTION_BYTES_MAX_SIZE 72
  79. #define SA_CMDL_MAX_SIZE_BYTES (SA_CMDL_HEADER_SIZE_BYTES + \
  80. SA_CMDL_OPTION_BYTES_MAX_SIZE)
  81. /* SWINFO word-0 flags */
  82. #define SA_SW_INFO_FLAG_EVICT 0x0001
  83. #define SA_SW_INFO_FLAG_TEAR 0x0002
  84. #define SA_SW_INFO_FLAG_NOPD 0x0004
  85. /*
  86. * This type represents the various packet types to be processed
  87. * by the PHP engine in SA.
  88. * It is used to identify the corresponding PHP processing function.
  89. */
  90. #define SA_CTX_PE_PKT_TYPE_3GPP_AIR 0 /* 3GPP Air Cipher */
  91. #define SA_CTX_PE_PKT_TYPE_SRTP 1 /* SRTP */
  92. #define SA_CTX_PE_PKT_TYPE_IPSEC_AH 2 /* IPSec Authentication Header */
  93. /* IPSec Encapsulating Security Payload */
  94. #define SA_CTX_PE_PKT_TYPE_IPSEC_ESP 3
  95. /* Indicates that it is in data mode, It may not be used by PHP */
  96. #define SA_CTX_PE_PKT_TYPE_NONE 4
  97. #define SA_CTX_ENC_TYPE1_SZ 64 /* Encryption SC with Key only */
  98. #define SA_CTX_ENC_TYPE2_SZ 96 /* Encryption SC with Key and Aux1 */
  99. #define SA_CTX_AUTH_TYPE1_SZ 64 /* Auth SC with Key only */
  100. #define SA_CTX_AUTH_TYPE2_SZ 96 /* Auth SC with Key and Aux1 */
  101. /* Size of security context for PHP engine */
  102. #define SA_CTX_PHP_PE_CTX_SZ 64
  103. #define SA_CTX_MAX_SZ (64 + SA_CTX_ENC_TYPE2_SZ + SA_CTX_AUTH_TYPE2_SZ)
  104. /*
  105. * Encoding of F/E control in SCCTL
  106. * Bit 0-1: Fetch PHP Bytes
  107. * Bit 2-3: Fetch Encryption/Air Ciphering Bytes
  108. * Bit 4-5: Fetch Authentication Bytes or Encr pass 2
  109. * Bit 6-7: Evict PHP Bytes
  110. *
  111. * where 00 = 0 bytes
  112. * 01 = 64 bytes
  113. * 10 = 96 bytes
  114. * 11 = 128 bytes
  115. */
  116. #define SA_CTX_DMA_SIZE_0 0
  117. #define SA_CTX_DMA_SIZE_64 1
  118. #define SA_CTX_DMA_SIZE_96 2
  119. #define SA_CTX_DMA_SIZE_128 3
  120. /*
  121. * Byte offset of the owner word in SCCTL
  122. * in the security context
  123. */
  124. #define SA_CTX_SCCTL_OWNER_OFFSET 0
  125. #define SA_CTX_ENC_KEY_OFFSET 32
  126. #define SA_CTX_ENC_AUX1_OFFSET 64
  127. #define SA_CTX_ENC_AUX2_OFFSET 96
  128. #define SA_CTX_ENC_AUX3_OFFSET 112
  129. #define SA_CTX_ENC_AUX4_OFFSET 128
  130. #define SA_SCCTL_FE_AUTH_ENC 0x65
  131. #define SA_SCCTL_FE_ENC 0x8D
  132. #define SA_ALIGN_MASK (sizeof(u32) - 1)
  133. #define SA_ALIGNED __aligned(32)
  134. /**
  135. * struct sa_crypto_data - Crypto driver instance data
  136. * @base: Base address of the register space
  137. * @pdev: Platform device pointer
  138. * @sc_pool: security context pool
  139. * @dev: Device pointer
  140. * @scid_lock: secure context ID lock
  141. * @sc_id_start: starting index for SC ID
  142. * @sc_id_end: Ending index for SC ID
  143. * @sc_id: Security Context ID
  144. * @ctx_bm: Bitmap to keep track of Security context ID's
  145. * @ctx: SA tfm context pointer
  146. * @dma_rx1: Pointer to DMA rx channel for sizes < 256 Bytes
  147. * @dma_rx2: Pointer to DMA rx channel for sizes > 256 Bytes
  148. * @dma_tx: Pointer to DMA TX channel
  149. */
  150. struct sa_crypto_data {
  151. void __iomem *base;
  152. struct platform_device *pdev;
  153. struct dma_pool *sc_pool;
  154. struct device *dev;
  155. spinlock_t scid_lock; /* lock for SC-ID allocation */
  156. /* Security context data */
  157. u16 sc_id_start;
  158. u16 sc_id_end;
  159. u16 sc_id;
  160. unsigned long ctx_bm[DIV_ROUND_UP(SA_MAX_NUM_CTX,
  161. BITS_PER_LONG)];
  162. struct sa_tfm_ctx *ctx;
  163. struct dma_chan *dma_rx1;
  164. struct dma_chan *dma_rx2;
  165. struct dma_chan *dma_tx;
  166. };
  167. /**
  168. * struct sa_cmdl_param_info: Command label parameters info
  169. * @index: Index of the parameter in the command label format
  170. * @offset: the offset of the parameter
  171. * @size: Size of the parameter
  172. */
  173. struct sa_cmdl_param_info {
  174. u16 index;
  175. u16 offset;
  176. u16 size;
  177. };
  178. /* Maximum length of Auxiliary data in 32bit words */
  179. #define SA_MAX_AUX_DATA_WORDS 8
  180. /**
  181. * struct sa_cmdl_upd_info: Command label updation info
  182. * @flags: flags in command label
  183. * @submode: Encryption submodes
  184. * @enc_size: Size of first pass encryption size
  185. * @enc_size2: Size of second pass encryption size
  186. * @enc_offset: Encryption payload offset in the packet
  187. * @enc_iv: Encryption initialization vector for pass2
  188. * @enc_iv2: Encryption initialization vector for pass2
  189. * @aad: Associated data
  190. * @payload: Payload info
  191. * @auth_size: Authentication size for pass 1
  192. * @auth_size2: Authentication size for pass 2
  193. * @auth_offset: Authentication payload offset
  194. * @auth_iv: Authentication initialization vector
  195. * @aux_key_info: Authentication aux key information
  196. * @aux_key: Aux key for authentication
  197. */
  198. struct sa_cmdl_upd_info {
  199. u16 flags;
  200. u16 submode;
  201. struct sa_cmdl_param_info enc_size;
  202. struct sa_cmdl_param_info enc_size2;
  203. struct sa_cmdl_param_info enc_offset;
  204. struct sa_cmdl_param_info enc_iv;
  205. struct sa_cmdl_param_info enc_iv2;
  206. struct sa_cmdl_param_info aad;
  207. struct sa_cmdl_param_info payload;
  208. struct sa_cmdl_param_info auth_size;
  209. struct sa_cmdl_param_info auth_size2;
  210. struct sa_cmdl_param_info auth_offset;
  211. struct sa_cmdl_param_info auth_iv;
  212. struct sa_cmdl_param_info aux_key_info;
  213. u32 aux_key[SA_MAX_AUX_DATA_WORDS];
  214. };
  215. /*
  216. * Number of 32bit words appended after the command label
  217. * in PSDATA to identify the crypto request context.
  218. * word-0: Request type
  219. * word-1: pointer to request
  220. */
  221. #define SA_PSDATA_CTX_WORDS 4
  222. /* Maximum size of Command label in 32 words */
  223. #define SA_MAX_CMDL_WORDS (SA_DMA_NUM_PS_WORDS - SA_PSDATA_CTX_WORDS)
  224. /**
  225. * struct sa_ctx_info: SA context information
  226. * @sc: Pointer to security context
  227. * @sc_phys: Security context physical address that is passed on to SA2UL
  228. * @cmdl_size: Command label size
  229. * @cmdl: Command label for a particular iteration
  230. * @cmdl_upd_info: structure holding command label updation info
  231. * @epib: Extended protocol information block words
  232. */
  233. struct sa_ctx_info {
  234. u8 *sc;
  235. dma_addr_t sc_phys;
  236. u16 sc_id;
  237. u16 cmdl_size;
  238. u32 cmdl[SA_MAX_CMDL_WORDS];
  239. struct sa_cmdl_upd_info cmdl_upd_info;
  240. /* Store Auxiliary data such as K2/K3 subkeys in AES-XCBC */
  241. u32 epib[SA_DMA_NUM_EPIB_WORDS];
  242. };
  243. struct sa_sham_hmac_ctx {
  244. struct crypto_shash *shash;
  245. u8 ipad[SHA512_BLOCK_SIZE] SA_ALIGNED;
  246. u8 opad[SHA512_BLOCK_SIZE] SA_ALIGNED;
  247. };
  248. /**
  249. * struct sa_tfm_ctx: TFM context structure
  250. * @dev_data: struct sa_crypto_data pointer
  251. * @enc: struct sa_ctx_info for encryption
  252. * @dec: struct sa_ctx_info for decryption
  253. * @auth: struct sa_ctx_info for authentication
  254. * @keylen: encrption/decryption keylength
  255. * @key: encryption key
  256. * @shash: software hash crypto_hash
  257. * @authkey: authentication key
  258. * @fallback_tfm: SW fallback ahash algorithm
  259. */
  260. struct sa_tfm_ctx {
  261. struct sa_crypto_data *dev_data;
  262. struct sa_ctx_info enc;
  263. struct sa_ctx_info dec;
  264. struct sa_ctx_info auth;
  265. int keylen;
  266. u32 key[AES_KEYSIZE_256 / sizeof(u32)];
  267. struct sa_sham_hmac_ctx base[0];
  268. struct crypto_shash *shash;
  269. u8 authkey[SHA512_BLOCK_SIZE];
  270. /* for fallback */
  271. struct crypto_ahash *fallback_tfm;
  272. };
  273. /**
  274. * struct sa_dma_req_ctx: Structure used for tx dma request
  275. * @dev_data: struct sa_crypto_data pointer
  276. * @cmdl: Complete command label with psdata and epib included
  277. * @src: source payload scatterlist pointer
  278. * @src_nents: Number of nodes in source scatterlist
  279. * @pkt: packet dma
  280. */
  281. struct sa_dma_req_ctx {
  282. struct sa_crypto_data *dev_data;
  283. u32 cmdl[SA_MAX_CMDL_WORDS + SA_PSDATA_CTX_WORDS];
  284. struct scatterlist *src;
  285. unsigned int src_nents;
  286. bool pkt;
  287. struct ahash_request fallback_req;
  288. u32 mode;
  289. };
  290. enum sa_submode {
  291. SA_MODE_GEN = 0,
  292. SA_MODE_CCM,
  293. SA_MODE_GCM,
  294. SA_MODE_GMAC
  295. };
  296. /* Encryption algorithms */
  297. enum sa_ealg_id {
  298. SA_EALG_ID_NONE = 0, /* No encryption */
  299. SA_EALG_ID_NULL, /* NULL encryption */
  300. SA_EALG_ID_AES_CTR, /* AES Counter mode */
  301. SA_EALG_ID_AES_F8, /* AES F8 mode */
  302. SA_EALG_ID_AES_CBC, /* AES CBC mode */
  303. SA_EALG_ID_DES_CBC, /* DES CBC mode */
  304. SA_EALG_ID_3DES_CBC, /* 3DES CBC mode */
  305. SA_EALG_ID_CCM, /* Counter with CBC-MAC mode */
  306. SA_EALG_ID_GCM, /* Galois Counter mode */
  307. SA_EALG_ID_AES_ECB,
  308. SA_EALG_ID_LAST
  309. };
  310. /* Authentication algorithms */
  311. enum sa_aalg_id {
  312. SA_AALG_ID_NONE = 0, /* No Authentication */
  313. SA_AALG_ID_NULL = SA_EALG_ID_LAST, /* NULL Authentication */
  314. SA_AALG_ID_MD5, /* MD5 mode */
  315. SA_AALG_ID_SHA1, /* SHA1 mode */
  316. SA_AALG_ID_SHA2_224, /* 224-bit SHA2 mode */
  317. SA_AALG_ID_SHA2_256, /* 256-bit SHA2 mode */
  318. SA_AALG_ID_SHA2_512, /* 512-bit SHA2 mode */
  319. SA_AALG_ID_HMAC_MD5, /* HMAC with MD5 mode */
  320. SA_AALG_ID_HMAC_SHA1, /* HMAC with SHA1 mode */
  321. SA_AALG_ID_HMAC_SHA2_224, /* HMAC with 224-bit SHA2 mode */
  322. SA_AALG_ID_HMAC_SHA2_256, /* HMAC with 256-bit SHA2 mode */
  323. SA_AALG_ID_GMAC, /* Galois Message Auth. Code mode */
  324. SA_AALG_ID_CMAC, /* Cipher-based Mes. Auth. Code mode */
  325. SA_AALG_ID_CBC_MAC, /* Cipher Block Chaining */
  326. SA_AALG_ID_AES_XCBC /* AES Extended Cipher Block Chaining */
  327. };
  328. /*
  329. * Mode control engine algorithms used to index the
  330. * mode control instruction tables
  331. */
  332. enum sa_eng_algo_id {
  333. SA_ENG_ALGO_ECB = 0,
  334. SA_ENG_ALGO_CBC,
  335. SA_ENG_ALGO_CFB,
  336. SA_ENG_ALGO_OFB,
  337. SA_ENG_ALGO_CTR,
  338. SA_ENG_ALGO_F8,
  339. SA_ENG_ALGO_F8F9,
  340. SA_ENG_ALGO_GCM,
  341. SA_ENG_ALGO_GMAC,
  342. SA_ENG_ALGO_CCM,
  343. SA_ENG_ALGO_CMAC,
  344. SA_ENG_ALGO_CBCMAC,
  345. SA_NUM_ENG_ALGOS
  346. };
  347. struct sa_eng_info {
  348. u8 eng_id;
  349. u16 sc_size;
  350. };
  351. extern struct device *sa_ks2_dev;
  352. #endif /* _K3_SA2UL_ */