sec.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (c) 2019 HiSilicon Limited. */
  3. #ifndef __HISI_SEC_V2_H
  4. #define __HISI_SEC_V2_H
  5. #include <linux/hisi_acc_qm.h>
  6. #include "sec_crypto.h"
  7. /* Algorithm resource per hardware SEC queue */
  8. struct sec_alg_res {
  9. u8 *pbuf;
  10. dma_addr_t pbuf_dma;
  11. u8 *c_ivin;
  12. dma_addr_t c_ivin_dma;
  13. u8 *a_ivin;
  14. dma_addr_t a_ivin_dma;
  15. u8 *out_mac;
  16. dma_addr_t out_mac_dma;
  17. u16 depth;
  18. };
  19. /* Cipher request of SEC private */
  20. struct sec_cipher_req {
  21. struct hisi_acc_hw_sgl *c_out;
  22. dma_addr_t c_out_dma;
  23. u8 *c_ivin;
  24. dma_addr_t c_ivin_dma;
  25. struct skcipher_request *sk_req;
  26. u32 c_len;
  27. bool encrypt;
  28. };
  29. struct sec_aead_req {
  30. u8 *out_mac;
  31. dma_addr_t out_mac_dma;
  32. u8 *a_ivin;
  33. dma_addr_t a_ivin_dma;
  34. struct aead_request *aead_req;
  35. };
  36. /* SEC request of Crypto */
  37. struct sec_req {
  38. union {
  39. struct sec_sqe sec_sqe;
  40. struct sec_sqe3 sec_sqe3;
  41. };
  42. struct sec_ctx *ctx;
  43. struct sec_qp_ctx *qp_ctx;
  44. /**
  45. * Common parameter of the SEC request.
  46. */
  47. struct hisi_acc_hw_sgl *in;
  48. dma_addr_t in_dma;
  49. struct sec_cipher_req c_req;
  50. struct sec_aead_req aead_req;
  51. struct list_head backlog_head;
  52. int err_type;
  53. int req_id;
  54. u32 flag;
  55. /* Status of the SEC request */
  56. bool fake_busy;
  57. bool use_pbuf;
  58. };
  59. /**
  60. * struct sec_req_op - Operations for SEC request
  61. * @buf_map: DMA map the SGL buffers of the request
  62. * @buf_unmap: DMA unmap the SGL buffers of the request
  63. * @bd_fill: Fill the SEC queue BD
  64. * @bd_send: Send the SEC BD into the hardware queue
  65. * @callback: Call back for the request
  66. * @process: Main processing logic of Skcipher
  67. */
  68. struct sec_req_op {
  69. int (*buf_map)(struct sec_ctx *ctx, struct sec_req *req);
  70. void (*buf_unmap)(struct sec_ctx *ctx, struct sec_req *req);
  71. void (*do_transfer)(struct sec_ctx *ctx, struct sec_req *req);
  72. int (*bd_fill)(struct sec_ctx *ctx, struct sec_req *req);
  73. int (*bd_send)(struct sec_ctx *ctx, struct sec_req *req);
  74. void (*callback)(struct sec_ctx *ctx, struct sec_req *req, int err);
  75. int (*process)(struct sec_ctx *ctx, struct sec_req *req);
  76. };
  77. /* SEC auth context */
  78. struct sec_auth_ctx {
  79. dma_addr_t a_key_dma;
  80. u8 *a_key;
  81. u8 a_key_len;
  82. u8 mac_len;
  83. u8 a_alg;
  84. bool fallback;
  85. struct crypto_shash *hash_tfm;
  86. struct crypto_aead *fallback_aead_tfm;
  87. };
  88. /* SEC cipher context which cipher's relatives */
  89. struct sec_cipher_ctx {
  90. u8 *c_key;
  91. dma_addr_t c_key_dma;
  92. sector_t iv_offset;
  93. u32 c_gran_size;
  94. u32 ivsize;
  95. u8 c_mode;
  96. u8 c_alg;
  97. u8 c_key_len;
  98. /* add software support */
  99. bool fallback;
  100. struct crypto_sync_skcipher *fbtfm;
  101. };
  102. /* SEC queue context which defines queue's relatives */
  103. struct sec_qp_ctx {
  104. struct hisi_qp *qp;
  105. struct sec_req **req_list;
  106. struct idr req_idr;
  107. struct sec_alg_res *res;
  108. struct sec_ctx *ctx;
  109. spinlock_t req_lock;
  110. struct list_head backlog;
  111. struct hisi_acc_sgl_pool *c_in_pool;
  112. struct hisi_acc_sgl_pool *c_out_pool;
  113. };
  114. enum sec_alg_type {
  115. SEC_SKCIPHER,
  116. SEC_AEAD
  117. };
  118. /* SEC Crypto TFM context which defines queue and cipher .etc relatives */
  119. struct sec_ctx {
  120. struct sec_qp_ctx *qp_ctx;
  121. struct sec_dev *sec;
  122. const struct sec_req_op *req_op;
  123. struct hisi_qp **qps;
  124. /* Half queues for encipher, and half for decipher */
  125. u32 hlf_q_num;
  126. /* Threshold for fake busy, trigger to return -EBUSY to user */
  127. u32 fake_req_limit;
  128. /* Current cyclic index to select a queue for encipher */
  129. atomic_t enc_qcyclic;
  130. /* Current cyclic index to select a queue for decipher */
  131. atomic_t dec_qcyclic;
  132. enum sec_alg_type alg_type;
  133. bool pbuf_supported;
  134. struct sec_cipher_ctx c_ctx;
  135. struct sec_auth_ctx a_ctx;
  136. u8 type_supported;
  137. struct device *dev;
  138. };
  139. enum sec_debug_file_index {
  140. SEC_CLEAR_ENABLE,
  141. SEC_DEBUG_FILE_NUM,
  142. };
  143. struct sec_debug_file {
  144. enum sec_debug_file_index index;
  145. spinlock_t lock;
  146. struct hisi_qm *qm;
  147. };
  148. struct sec_dfx {
  149. atomic64_t send_cnt;
  150. atomic64_t recv_cnt;
  151. atomic64_t send_busy_cnt;
  152. atomic64_t recv_busy_cnt;
  153. atomic64_t err_bd_cnt;
  154. atomic64_t invalid_req_cnt;
  155. atomic64_t done_flag_cnt;
  156. };
  157. struct sec_debug {
  158. struct sec_dfx dfx;
  159. struct sec_debug_file files[SEC_DEBUG_FILE_NUM];
  160. };
  161. struct sec_dev {
  162. struct hisi_qm qm;
  163. struct sec_debug debug;
  164. u32 ctx_q_num;
  165. bool iommu_used;
  166. };
  167. enum sec_cap_type {
  168. SEC_QM_NFE_MASK_CAP = 0x0,
  169. SEC_QM_RESET_MASK_CAP,
  170. SEC_QM_OOO_SHUTDOWN_MASK_CAP,
  171. SEC_QM_CE_MASK_CAP,
  172. SEC_NFE_MASK_CAP,
  173. SEC_RESET_MASK_CAP,
  174. SEC_OOO_SHUTDOWN_MASK_CAP,
  175. SEC_CE_MASK_CAP,
  176. SEC_CLUSTER_NUM_CAP,
  177. SEC_CORE_TYPE_NUM_CAP,
  178. SEC_CORE_NUM_CAP,
  179. SEC_CORES_PER_CLUSTER_NUM_CAP,
  180. SEC_CORE_ENABLE_BITMAP,
  181. SEC_DRV_ALG_BITMAP_LOW,
  182. SEC_DRV_ALG_BITMAP_HIGH,
  183. SEC_DEV_ALG_BITMAP_LOW,
  184. SEC_DEV_ALG_BITMAP_HIGH,
  185. SEC_CORE1_ALG_BITMAP_LOW,
  186. SEC_CORE1_ALG_BITMAP_HIGH,
  187. SEC_CORE2_ALG_BITMAP_LOW,
  188. SEC_CORE2_ALG_BITMAP_HIGH,
  189. SEC_CORE3_ALG_BITMAP_LOW,
  190. SEC_CORE3_ALG_BITMAP_HIGH,
  191. SEC_CORE4_ALG_BITMAP_LOW,
  192. SEC_CORE4_ALG_BITMAP_HIGH,
  193. };
  194. void sec_destroy_qps(struct hisi_qp **qps, int qp_num);
  195. struct hisi_qp **sec_create_qps(void);
  196. int sec_register_to_crypto(struct hisi_qm *qm);
  197. void sec_unregister_from_crypto(struct hisi_qm *qm);
  198. u64 sec_get_alg_bitmap(struct hisi_qm *qm, u32 high, u32 low);
  199. #endif