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