sec_drv.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2016-2017 Hisilicon Limited. */
  3. #ifndef _SEC_DRV_H_
  4. #define _SEC_DRV_H_
  5. #include <crypto/algapi.h>
  6. #include <linux/kfifo.h>
  7. #define SEC_MAX_SGE_NUM 64
  8. #define SEC_HW_RING_NUM 3
  9. #define SEC_CMD_RING 0
  10. #define SEC_OUTORDER_RING 1
  11. #define SEC_DBG_RING 2
  12. /* A reasonable length to balance memory use against flexibility */
  13. #define SEC_QUEUE_LEN 512
  14. #define SEC_MAX_SGE_NUM 64
  15. struct sec_bd_info {
  16. #define SEC_BD_W0_T_LEN_M GENMASK(4, 0)
  17. #define SEC_BD_W0_T_LEN_S 0
  18. #define SEC_BD_W0_C_WIDTH_M GENMASK(6, 5)
  19. #define SEC_BD_W0_C_WIDTH_S 5
  20. #define SEC_C_WIDTH_AES_128BIT 0
  21. #define SEC_C_WIDTH_AES_8BIT 1
  22. #define SEC_C_WIDTH_AES_1BIT 2
  23. #define SEC_C_WIDTH_DES_64BIT 0
  24. #define SEC_C_WIDTH_DES_8BIT 1
  25. #define SEC_C_WIDTH_DES_1BIT 2
  26. #define SEC_BD_W0_C_MODE_M GENMASK(9, 7)
  27. #define SEC_BD_W0_C_MODE_S 7
  28. #define SEC_C_MODE_ECB 0
  29. #define SEC_C_MODE_CBC 1
  30. #define SEC_C_MODE_CTR 4
  31. #define SEC_C_MODE_CCM 5
  32. #define SEC_C_MODE_GCM 6
  33. #define SEC_C_MODE_XTS 7
  34. #define SEC_BD_W0_SEQ BIT(10)
  35. #define SEC_BD_W0_DE BIT(11)
  36. #define SEC_BD_W0_DAT_SKIP_M GENMASK(13, 12)
  37. #define SEC_BD_W0_DAT_SKIP_S 12
  38. #define SEC_BD_W0_C_GRAN_SIZE_19_16_M GENMASK(17, 14)
  39. #define SEC_BD_W0_C_GRAN_SIZE_19_16_S 14
  40. #define SEC_BD_W0_CIPHER_M GENMASK(19, 18)
  41. #define SEC_BD_W0_CIPHER_S 18
  42. #define SEC_CIPHER_NULL 0
  43. #define SEC_CIPHER_ENCRYPT 1
  44. #define SEC_CIPHER_DECRYPT 2
  45. #define SEC_BD_W0_AUTH_M GENMASK(21, 20)
  46. #define SEC_BD_W0_AUTH_S 20
  47. #define SEC_AUTH_NULL 0
  48. #define SEC_AUTH_MAC 1
  49. #define SEC_AUTH_VERIF 2
  50. #define SEC_BD_W0_AI_GEN BIT(22)
  51. #define SEC_BD_W0_CI_GEN BIT(23)
  52. #define SEC_BD_W0_NO_HPAD BIT(24)
  53. #define SEC_BD_W0_HM_M GENMASK(26, 25)
  54. #define SEC_BD_W0_HM_S 25
  55. #define SEC_BD_W0_ICV_OR_SKEY_EN_M GENMASK(28, 27)
  56. #define SEC_BD_W0_ICV_OR_SKEY_EN_S 27
  57. /* Multi purpose field - gran size bits for send, flag for recv */
  58. #define SEC_BD_W0_FLAG_M GENMASK(30, 29)
  59. #define SEC_BD_W0_C_GRAN_SIZE_21_20_M GENMASK(30, 29)
  60. #define SEC_BD_W0_FLAG_S 29
  61. #define SEC_BD_W0_C_GRAN_SIZE_21_20_S 29
  62. #define SEC_BD_W0_DONE BIT(31)
  63. u32 w0;
  64. #define SEC_BD_W1_AUTH_GRAN_SIZE_M GENMASK(21, 0)
  65. #define SEC_BD_W1_AUTH_GRAN_SIZE_S 0
  66. #define SEC_BD_W1_M_KEY_EN BIT(22)
  67. #define SEC_BD_W1_BD_INVALID BIT(23)
  68. #define SEC_BD_W1_ADDR_TYPE BIT(24)
  69. #define SEC_BD_W1_A_ALG_M GENMASK(28, 25)
  70. #define SEC_BD_W1_A_ALG_S 25
  71. #define SEC_A_ALG_SHA1 0
  72. #define SEC_A_ALG_SHA256 1
  73. #define SEC_A_ALG_MD5 2
  74. #define SEC_A_ALG_SHA224 3
  75. #define SEC_A_ALG_HMAC_SHA1 8
  76. #define SEC_A_ALG_HMAC_SHA224 10
  77. #define SEC_A_ALG_HMAC_SHA256 11
  78. #define SEC_A_ALG_HMAC_MD5 12
  79. #define SEC_A_ALG_AES_XCBC 13
  80. #define SEC_A_ALG_AES_CMAC 14
  81. #define SEC_BD_W1_C_ALG_M GENMASK(31, 29)
  82. #define SEC_BD_W1_C_ALG_S 29
  83. #define SEC_C_ALG_DES 0
  84. #define SEC_C_ALG_3DES 1
  85. #define SEC_C_ALG_AES 2
  86. u32 w1;
  87. #define SEC_BD_W2_C_GRAN_SIZE_15_0_M GENMASK(15, 0)
  88. #define SEC_BD_W2_C_GRAN_SIZE_15_0_S 0
  89. #define SEC_BD_W2_GRAN_NUM_M GENMASK(31, 16)
  90. #define SEC_BD_W2_GRAN_NUM_S 16
  91. u32 w2;
  92. #define SEC_BD_W3_AUTH_LEN_OFFSET_M GENMASK(9, 0)
  93. #define SEC_BD_W3_AUTH_LEN_OFFSET_S 0
  94. #define SEC_BD_W3_CIPHER_LEN_OFFSET_M GENMASK(19, 10)
  95. #define SEC_BD_W3_CIPHER_LEN_OFFSET_S 10
  96. #define SEC_BD_W3_MAC_LEN_M GENMASK(24, 20)
  97. #define SEC_BD_W3_MAC_LEN_S 20
  98. #define SEC_BD_W3_A_KEY_LEN_M GENMASK(29, 25)
  99. #define SEC_BD_W3_A_KEY_LEN_S 25
  100. #define SEC_BD_W3_C_KEY_LEN_M GENMASK(31, 30)
  101. #define SEC_BD_W3_C_KEY_LEN_S 30
  102. #define SEC_KEY_LEN_AES_128 0
  103. #define SEC_KEY_LEN_AES_192 1
  104. #define SEC_KEY_LEN_AES_256 2
  105. #define SEC_KEY_LEN_DES 1
  106. #define SEC_KEY_LEN_3DES_3_KEY 1
  107. #define SEC_KEY_LEN_3DES_2_KEY 3
  108. u32 w3;
  109. /* W4,5 */
  110. union {
  111. u32 authkey_addr_lo;
  112. u32 authiv_addr_lo;
  113. };
  114. union {
  115. u32 authkey_addr_hi;
  116. u32 authiv_addr_hi;
  117. };
  118. /* W6,7 */
  119. u32 cipher_key_addr_lo;
  120. u32 cipher_key_addr_hi;
  121. /* W8,9 */
  122. u32 cipher_iv_addr_lo;
  123. u32 cipher_iv_addr_hi;
  124. /* W10,11 */
  125. u32 data_addr_lo;
  126. u32 data_addr_hi;
  127. /* W12,13 */
  128. u32 mac_addr_lo;
  129. u32 mac_addr_hi;
  130. /* W14,15 */
  131. u32 cipher_destin_addr_lo;
  132. u32 cipher_destin_addr_hi;
  133. };
  134. enum sec_mem_region {
  135. SEC_COMMON = 0,
  136. SEC_SAA,
  137. SEC_NUM_ADDR_REGIONS
  138. };
  139. #define SEC_NAME_SIZE 64
  140. #define SEC_Q_NUM 16
  141. /**
  142. * struct sec_queue_ring_cmd - store information about a SEC HW cmd ring
  143. * @used: Local counter used to cheaply establish if the ring is empty.
  144. * @lock: Protect against simultaneous adjusting of the read and write pointers.
  145. * @vaddr: Virtual address for the ram pages used for the ring.
  146. * @paddr: Physical address of the dma mapped region of ram used for the ring.
  147. * @callback: Callback function called on a ring element completing.
  148. */
  149. struct sec_queue_ring_cmd {
  150. atomic_t used;
  151. struct mutex lock;
  152. struct sec_bd_info *vaddr;
  153. dma_addr_t paddr;
  154. void (*callback)(struct sec_bd_info *resp, void *ctx);
  155. };
  156. struct sec_debug_bd_info;
  157. struct sec_queue_ring_db {
  158. struct sec_debug_bd_info *vaddr;
  159. dma_addr_t paddr;
  160. };
  161. struct sec_out_bd_info;
  162. struct sec_queue_ring_cq {
  163. struct sec_out_bd_info *vaddr;
  164. dma_addr_t paddr;
  165. };
  166. struct sec_dev_info;
  167. enum sec_cipher_alg {
  168. SEC_C_DES_ECB_64,
  169. SEC_C_DES_CBC_64,
  170. SEC_C_3DES_ECB_192_3KEY,
  171. SEC_C_3DES_ECB_192_2KEY,
  172. SEC_C_3DES_CBC_192_3KEY,
  173. SEC_C_3DES_CBC_192_2KEY,
  174. SEC_C_AES_ECB_128,
  175. SEC_C_AES_ECB_192,
  176. SEC_C_AES_ECB_256,
  177. SEC_C_AES_CBC_128,
  178. SEC_C_AES_CBC_192,
  179. SEC_C_AES_CBC_256,
  180. SEC_C_AES_CTR_128,
  181. SEC_C_AES_CTR_192,
  182. SEC_C_AES_CTR_256,
  183. SEC_C_AES_XTS_128,
  184. SEC_C_AES_XTS_256,
  185. SEC_C_NULL,
  186. };
  187. /**
  188. * struct sec_alg_tfm_ctx - hardware specific tranformation context
  189. * @cipher_alg: Cipher algorithm enabled include encryption mode.
  190. * @key: Key storage if required.
  191. * @pkey: DMA address for the key storage.
  192. * @req_template: Request template to save time on setup.
  193. * @queue: The hardware queue associated with this tfm context.
  194. * @lock: Protect key and pkey to ensure they are consistent
  195. * @auth_buf: Current context buffer for auth operations.
  196. * @backlog: The backlog queue used for cases where our buffers aren't
  197. * large enough.
  198. */
  199. struct sec_alg_tfm_ctx {
  200. enum sec_cipher_alg cipher_alg;
  201. u8 *key;
  202. dma_addr_t pkey;
  203. struct sec_bd_info req_template;
  204. struct sec_queue *queue;
  205. struct mutex lock;
  206. u8 *auth_buf;
  207. struct list_head backlog;
  208. };
  209. /**
  210. * struct sec_request - data associate with a single crypto request
  211. * @elements: List of subparts of this request (hardware size restriction)
  212. * @num_elements: The number of subparts (used as an optimization)
  213. * @lock: Protect elements of this structure against concurrent change.
  214. * @tfm_ctx: hardware specific context.
  215. * @len_in: length of in sgl from upper layers
  216. * @len_out: length of out sgl from upper layers
  217. * @dma_iv: initialization vector - phsyical address
  218. * @err: store used to track errors across subelements of this request.
  219. * @req_base: pointer to base element of associate crypto context.
  220. * This is needed to allow shared handling skcipher, ahash etc.
  221. * @cb: completion callback.
  222. * @backlog_head: list head to allow backlog maintenance.
  223. *
  224. * The hardware is limited in the maximum size of data that it can
  225. * process from a single BD. Typically this is fairly large (32MB)
  226. * but still requires the complexity of splitting the incoming
  227. * skreq up into a number of elements complete with appropriate
  228. * iv chaining.
  229. */
  230. struct sec_request {
  231. struct list_head elements;
  232. int num_elements;
  233. struct mutex lock;
  234. struct sec_alg_tfm_ctx *tfm_ctx;
  235. int len_in;
  236. int len_out;
  237. dma_addr_t dma_iv;
  238. int err;
  239. struct crypto_async_request *req_base;
  240. void (*cb)(struct sec_bd_info *resp, struct crypto_async_request *req);
  241. struct list_head backlog_head;
  242. };
  243. /**
  244. * struct sec_request_el - A subpart of a request.
  245. * @head: allow us to attach this to the list in the sec_request
  246. * @req: hardware block descriptor corresponding to this request subpart
  247. * @in: hardware sgl for input - virtual address
  248. * @dma_in: hardware sgl for input - physical address
  249. * @sgl_in: scatterlist for this request subpart
  250. * @out: hardware sgl for output - virtual address
  251. * @dma_out: hardware sgl for output - physical address
  252. * @sgl_out: scatterlist for this request subpart
  253. * @sec_req: The request which this subpart forms a part of
  254. * @el_length: Number of bytes in this subpart. Needed to locate
  255. * last ivsize chunk for iv chaining.
  256. */
  257. struct sec_request_el {
  258. struct list_head head;
  259. struct sec_bd_info req;
  260. struct sec_hw_sgl *in;
  261. dma_addr_t dma_in;
  262. struct scatterlist *sgl_in;
  263. struct sec_hw_sgl *out;
  264. dma_addr_t dma_out;
  265. struct scatterlist *sgl_out;
  266. struct sec_request *sec_req;
  267. size_t el_length;
  268. };
  269. /**
  270. * struct sec_queue - All the information about a HW queue
  271. * @dev_info: The parent SEC device to which this queue belongs.
  272. * @task_irq: Completion interrupt for the queue.
  273. * @name: Human readable queue description also used as irq name.
  274. * @ring: The several HW rings associated with one queue.
  275. * @regs: The iomapped device registers
  276. * @queue_id: Index of the queue used for naming and resource selection.
  277. * @in_use: Flag to say if the queue is in use.
  278. * @expected: The next expected element to finish assuming we were in order.
  279. * @uprocessed: A bitmap to track which OoO elements are done but not handled.
  280. * @softqueue: A software queue used when chaining requirements prevent direct
  281. * use of the hardware queues.
  282. * @havesoftqueue: A flag to say we have a queues - as we may need one for the
  283. * current mode.
  284. * @queuelock: Protect the soft queue from concurrent changes to avoid some
  285. * potential loss of data races.
  286. * @shadow: Pointers back to the shadow copy of the hardware ring element
  287. * need because we can't store any context reference in the bd element.
  288. */
  289. struct sec_queue {
  290. struct sec_dev_info *dev_info;
  291. int task_irq;
  292. char name[SEC_NAME_SIZE];
  293. struct sec_queue_ring_cmd ring_cmd;
  294. struct sec_queue_ring_cq ring_cq;
  295. struct sec_queue_ring_db ring_db;
  296. void __iomem *regs;
  297. u32 queue_id;
  298. bool in_use;
  299. int expected;
  300. DECLARE_BITMAP(unprocessed, SEC_QUEUE_LEN);
  301. DECLARE_KFIFO_PTR(softqueue, typeof(struct sec_request_el *));
  302. bool havesoftqueue;
  303. struct mutex queuelock;
  304. void *shadow[SEC_QUEUE_LEN];
  305. };
  306. /**
  307. * struct sec_hw_sge: Track each of the 64 element SEC HW SGL entries
  308. * @buf: The IOV dma address for this entry.
  309. * @len: Length of this IOV.
  310. * @pad: Reserved space.
  311. */
  312. struct sec_hw_sge {
  313. dma_addr_t buf;
  314. unsigned int len;
  315. unsigned int pad;
  316. };
  317. /**
  318. * struct sec_hw_sgl: One hardware SGL entry.
  319. * @next_sgl: The next entry if we need to chain dma address. Null if last.
  320. * @entry_sum_in_chain: The full count of SGEs - only matters for first SGL.
  321. * @entry_sum_in_sgl: The number of SGEs in this SGL element.
  322. * @flag: Unused in skciphers.
  323. * @serial_num: Unsued in skciphers.
  324. * @cpuid: Currently unused.
  325. * @data_bytes_in_sgl: Count of bytes from all SGEs in this SGL.
  326. * @next: Virtual address used to stash the next sgl - useful in completion.
  327. * @reserved: A reserved field not currently used.
  328. * @sge_entries: The (up to) 64 Scatter Gather Entries, representing IOVs.
  329. * @node: Currently unused.
  330. */
  331. struct sec_hw_sgl {
  332. dma_addr_t next_sgl;
  333. u16 entry_sum_in_chain;
  334. u16 entry_sum_in_sgl;
  335. u32 flag;
  336. u64 serial_num;
  337. u32 cpuid;
  338. u32 data_bytes_in_sgl;
  339. struct sec_hw_sgl *next;
  340. u64 reserved;
  341. struct sec_hw_sge sge_entries[SEC_MAX_SGE_NUM];
  342. u8 node[16];
  343. };
  344. struct dma_pool;
  345. /**
  346. * struct sec_dev_info: The full SEC unit comprising queues and processors.
  347. * @sec_id: Index used to track which SEC this is when more than one is present.
  348. * @num_saas: The number of backed processors enabled.
  349. * @regs: iomapped register regions shared by whole SEC unit.
  350. * @dev_lock: Protects concurrent queue allocation / freeing for the SEC.
  351. * @queues: The 16 queues that this SEC instance provides.
  352. * @dev: Device pointer.
  353. * @hw_sgl_pool: DMA pool used to mimise mapping for the scatter gather lists.
  354. */
  355. struct sec_dev_info {
  356. int sec_id;
  357. int num_saas;
  358. void __iomem *regs[SEC_NUM_ADDR_REGIONS];
  359. struct mutex dev_lock;
  360. int queues_in_use;
  361. struct sec_queue queues[SEC_Q_NUM];
  362. struct device *dev;
  363. struct dma_pool *hw_sgl_pool;
  364. };
  365. int sec_queue_send(struct sec_queue *queue, struct sec_bd_info *msg, void *ctx);
  366. bool sec_queue_can_enqueue(struct sec_queue *queue, int num);
  367. int sec_queue_stop_release(struct sec_queue *queue);
  368. struct sec_queue *sec_queue_alloc_start_safe(void);
  369. bool sec_queue_empty(struct sec_queue *queue);
  370. /* Algorithm specific elements from sec_algs.c */
  371. void sec_alg_callback(struct sec_bd_info *resp, void *ctx);
  372. int sec_algs_register(void);
  373. void sec_algs_unregister(void);
  374. #endif /* _SEC_DRV_H_ */