crypto: hisilicon/qm - set the total number of queues
[linux-block.git] / drivers / crypto / hisilicon / qm.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2019 HiSilicon Limited. */
3 #ifndef HISI_ACC_QM_H
4 #define HISI_ACC_QM_H
5
6 #include <linux/bitfield.h>
7 #include <linux/iopoll.h>
8 #include <linux/module.h>
9 #include <linux/pci.h>
10
11 #define QM_QNUM_V1                      4096
12 #define QM_QNUM_V2                      1024
13 #define QM_MAX_VFS_NUM_V2               63
14
15 /* qm user domain */
16 #define QM_ARUSER_M_CFG_1               0x100088
17 #define AXUSER_SNOOP_ENABLE             BIT(30)
18 #define AXUSER_CMD_TYPE                 GENMASK(14, 12)
19 #define AXUSER_CMD_SMMU_NORMAL          1
20 #define AXUSER_NS                       BIT(6)
21 #define AXUSER_NO                       BIT(5)
22 #define AXUSER_FP                       BIT(4)
23 #define AXUSER_SSV                      BIT(0)
24 #define AXUSER_BASE                     (AXUSER_SNOOP_ENABLE |          \
25                                         FIELD_PREP(AXUSER_CMD_TYPE,     \
26                                         AXUSER_CMD_SMMU_NORMAL) |       \
27                                         AXUSER_NS | AXUSER_NO | AXUSER_FP)
28 #define QM_ARUSER_M_CFG_ENABLE          0x100090
29 #define ARUSER_M_CFG_ENABLE             0xfffffffe
30 #define QM_AWUSER_M_CFG_1               0x100098
31 #define QM_AWUSER_M_CFG_ENABLE          0x1000a0
32 #define AWUSER_M_CFG_ENABLE             0xfffffffe
33 #define QM_WUSER_M_CFG_ENABLE           0x1000a8
34 #define WUSER_M_CFG_ENABLE              0xffffffff
35
36 /* qm cache */
37 #define QM_CACHE_CTL                    0x100050
38 #define SQC_CACHE_ENABLE                BIT(0)
39 #define CQC_CACHE_ENABLE                BIT(1)
40 #define SQC_CACHE_WB_ENABLE             BIT(4)
41 #define SQC_CACHE_WB_THRD               GENMASK(10, 5)
42 #define CQC_CACHE_WB_ENABLE             BIT(11)
43 #define CQC_CACHE_WB_THRD               GENMASK(17, 12)
44 #define QM_AXI_M_CFG                    0x1000ac
45 #define AXI_M_CFG                       0xffff
46 #define QM_AXI_M_CFG_ENABLE             0x1000b0
47 #define AM_CFG_SINGLE_PORT_MAX_TRANS    0x300014
48 #define AXI_M_CFG_ENABLE                0xffffffff
49 #define QM_PEH_AXUSER_CFG               0x1000cc
50 #define QM_PEH_AXUSER_CFG_ENABLE        0x1000d0
51 #define PEH_AXUSER_CFG                  0x401001
52 #define PEH_AXUSER_CFG_ENABLE           0xffffffff
53
54 #define QM_DFX_MB_CNT_VF                0x104010
55 #define QM_DFX_DB_CNT_VF                0x104020
56 #define QM_DFX_SQE_CNT_VF_SQN           0x104030
57 #define QM_DFX_CQE_CNT_VF_CQN           0x104040
58 #define QM_DFX_QN_SHIFT                 16
59 #define CURRENT_FUN_MASK                GENMASK(5, 0)
60 #define CURRENT_Q_MASK                  GENMASK(31, 16)
61
62 #define QM_AXI_RRESP                    BIT(0)
63 #define QM_AXI_BRESP                    BIT(1)
64 #define QM_ECC_MBIT                     BIT(2)
65 #define QM_ECC_1BIT                     BIT(3)
66 #define QM_ACC_GET_TASK_TIMEOUT         BIT(4)
67 #define QM_ACC_DO_TASK_TIMEOUT          BIT(5)
68 #define QM_ACC_WB_NOT_READY_TIMEOUT     BIT(6)
69 #define QM_SQ_CQ_VF_INVALID             BIT(7)
70 #define QM_CQ_VF_INVALID                BIT(8)
71 #define QM_SQ_VF_INVALID                BIT(9)
72 #define QM_DB_TIMEOUT                   BIT(10)
73 #define QM_OF_FIFO_OF                   BIT(11)
74 #define QM_DB_RANDOM_INVALID            BIT(12)
75
76 #define QM_BASE_NFE     (QM_AXI_RRESP | QM_AXI_BRESP | QM_ECC_MBIT | \
77                          QM_ACC_GET_TASK_TIMEOUT | QM_DB_TIMEOUT | \
78                          QM_OF_FIFO_OF | QM_DB_RANDOM_INVALID)
79 #define QM_BASE_CE                      QM_ECC_1BIT
80
81 #define QM_Q_DEPTH                      1024
82 #define QM_MIN_QNUM                     2
83 #define HISI_ACC_SGL_SGE_NR_MAX         255
84
85 /* page number for queue file region */
86 #define QM_DOORBELL_PAGE_NR             1
87
88 /* uacce mode of the driver */
89 #define UACCE_MODE_NOUACCE              0 /* don't use uacce */
90 #define UACCE_MODE_SVA                  1 /* use uacce sva mode */
91 #define UACCE_MODE_DESC "0(default) means only register to crypto, 1 means both register to crypto and uacce"
92
93 enum qm_stop_reason {
94         QM_NORMAL,
95         QM_SOFT_RESET,
96         QM_FLR,
97 };
98
99 enum qm_state {
100         QM_INIT = 0,
101         QM_START,
102         QM_CLOSE,
103         QM_STOP,
104 };
105
106 enum qp_state {
107         QP_INIT = 1,
108         QP_START,
109         QP_STOP,
110         QP_CLOSE,
111 };
112
113 enum qm_hw_ver {
114         QM_HW_UNKNOWN = -1,
115         QM_HW_V1 = 0x20,
116         QM_HW_V2 = 0x21,
117         QM_HW_V3 = 0x30,
118 };
119
120 enum qm_fun_type {
121         QM_HW_PF,
122         QM_HW_VF,
123 };
124
125 enum qm_debug_file {
126         CURRENT_Q,
127         CLEAR_ENABLE,
128         DEBUG_FILE_NUM,
129 };
130
131 struct qm_dfx {
132         atomic64_t err_irq_cnt;
133         atomic64_t aeq_irq_cnt;
134         atomic64_t abnormal_irq_cnt;
135         atomic64_t create_qp_err_cnt;
136         atomic64_t mb_err_cnt;
137 };
138
139 struct debugfs_file {
140         enum qm_debug_file index;
141         struct mutex lock;
142         struct qm_debug *debug;
143 };
144
145 struct qm_debug {
146         u32 curr_qm_qp_num;
147         u32 sqe_mask_offset;
148         u32 sqe_mask_len;
149         struct qm_dfx dfx;
150         struct dentry *debug_root;
151         struct dentry *qm_d;
152         struct debugfs_file files[DEBUG_FILE_NUM];
153 };
154
155 struct qm_dma {
156         void *va;
157         dma_addr_t dma;
158         size_t size;
159 };
160
161 struct hisi_qm_status {
162         u32 eq_head;
163         bool eqc_phase;
164         u32 aeq_head;
165         bool aeqc_phase;
166         atomic_t flags;
167         int stop_reason;
168 };
169
170 struct hisi_qm;
171
172 struct hisi_qm_err_info {
173         char *acpi_rst;
174         u32 msi_wr_port;
175         u32 ecc_2bits_mask;
176         u32 dev_ce_mask;
177         u32 ce;
178         u32 nfe;
179         u32 fe;
180 };
181
182 struct hisi_qm_err_status {
183         u32 is_qm_ecc_mbit;
184         u32 is_dev_ecc_mbit;
185 };
186
187 struct hisi_qm_err_ini {
188         int (*hw_init)(struct hisi_qm *qm);
189         void (*hw_err_enable)(struct hisi_qm *qm);
190         void (*hw_err_disable)(struct hisi_qm *qm);
191         u32 (*get_dev_hw_err_status)(struct hisi_qm *qm);
192         void (*clear_dev_hw_err_status)(struct hisi_qm *qm, u32 err_sts);
193         void (*open_axi_master_ooo)(struct hisi_qm *qm);
194         void (*close_axi_master_ooo)(struct hisi_qm *qm);
195         void (*log_dev_hw_err)(struct hisi_qm *qm, u32 err_sts);
196         struct hisi_qm_err_info err_info;
197 };
198
199 struct hisi_qm_list {
200         struct mutex lock;
201         struct list_head list;
202         int (*register_to_crypto)(struct hisi_qm *qm);
203         void (*unregister_from_crypto)(struct hisi_qm *qm);
204 };
205
206 struct hisi_qm {
207         enum qm_hw_ver ver;
208         enum qm_fun_type fun_type;
209         const char *dev_name;
210         struct pci_dev *pdev;
211         void __iomem *io_base;
212         u32 sqe_size;
213         u32 qp_base;
214         u32 qp_num;
215         u32 qp_in_used;
216         u32 ctrl_qp_num;
217         u32 vfs_num;
218         struct list_head list;
219         struct hisi_qm_list *qm_list;
220
221         struct qm_dma qdma;
222         struct qm_sqc *sqc;
223         struct qm_cqc *cqc;
224         struct qm_eqe *eqe;
225         struct qm_aeqe *aeqe;
226         dma_addr_t sqc_dma;
227         dma_addr_t cqc_dma;
228         dma_addr_t eqe_dma;
229         dma_addr_t aeqe_dma;
230
231         struct hisi_qm_status status;
232         const struct hisi_qm_err_ini *err_ini;
233         struct hisi_qm_err_status err_status;
234         unsigned long misc_ctl; /* driver removing and reset sched */
235
236         struct rw_semaphore qps_lock;
237         struct idr qp_idr;
238         struct hisi_qp *qp_array;
239
240         struct mutex mailbox_lock;
241
242         const struct hisi_qm_hw_ops *ops;
243
244         struct qm_debug debug;
245
246         u32 error_mask;
247
248         struct workqueue_struct *wq;
249         struct work_struct work;
250         struct work_struct rst_work;
251
252         const char *algs;
253         bool use_sva;
254         bool is_frozen;
255         resource_size_t phys_base;
256         resource_size_t phys_size;
257         struct uacce_device *uacce;
258         int mode;
259 };
260
261 struct hisi_qp_status {
262         atomic_t used;
263         u16 sq_tail;
264         u16 cq_head;
265         bool cqc_phase;
266         atomic_t flags;
267 };
268
269 struct hisi_qp_ops {
270         int (*fill_sqe)(void *sqe, void *q_parm, void *d_parm);
271 };
272
273 struct hisi_qp {
274         u32 qp_id;
275         u8 alg_type;
276         u8 req_type;
277
278         struct qm_dma qdma;
279         void *sqe;
280         struct qm_cqe *cqe;
281         dma_addr_t sqe_dma;
282         dma_addr_t cqe_dma;
283
284         struct hisi_qp_status qp_status;
285         struct hisi_qp_ops *hw_ops;
286         void *qp_ctx;
287         void (*req_cb)(struct hisi_qp *qp, void *data);
288         void (*event_cb)(struct hisi_qp *qp);
289
290         struct hisi_qm *qm;
291         bool is_resetting;
292         bool is_in_kernel;
293         u16 pasid;
294         struct uacce_queue *uacce_q;
295 };
296
297 static inline int q_num_set(const char *val, const struct kernel_param *kp,
298                             unsigned int device)
299 {
300         struct pci_dev *pdev = pci_get_device(PCI_VENDOR_ID_HUAWEI,
301                                               device, NULL);
302         u32 n, q_num;
303         int ret;
304
305         if (!val)
306                 return -EINVAL;
307
308         if (!pdev) {
309                 q_num = min_t(u32, QM_QNUM_V1, QM_QNUM_V2);
310                 pr_info("No device found currently, suppose queue number is %u\n",
311                         q_num);
312         } else {
313                 if (pdev->revision == QM_HW_V1)
314                         q_num = QM_QNUM_V1;
315                 else
316                         q_num = QM_QNUM_V2;
317         }
318
319         ret = kstrtou32(val, 10, &n);
320         if (ret || n < QM_MIN_QNUM || n > q_num)
321                 return -EINVAL;
322
323         return param_set_int(val, kp);
324 }
325
326 static inline int vfs_num_set(const char *val, const struct kernel_param *kp)
327 {
328         u32 n;
329         int ret;
330
331         if (!val)
332                 return -EINVAL;
333
334         ret = kstrtou32(val, 10, &n);
335         if (ret < 0)
336                 return ret;
337
338         if (n > QM_MAX_VFS_NUM_V2)
339                 return -EINVAL;
340
341         return param_set_int(val, kp);
342 }
343
344 static inline int mode_set(const char *val, const struct kernel_param *kp)
345 {
346         u32 n;
347         int ret;
348
349         if (!val)
350                 return -EINVAL;
351
352         ret = kstrtou32(val, 10, &n);
353         if (ret != 0 || (n != UACCE_MODE_SVA &&
354                          n != UACCE_MODE_NOUACCE))
355                 return -EINVAL;
356
357         return param_set_int(val, kp);
358 }
359
360 static inline int uacce_mode_set(const char *val, const struct kernel_param *kp)
361 {
362         return mode_set(val, kp);
363 }
364
365 static inline void hisi_qm_init_list(struct hisi_qm_list *qm_list)
366 {
367         INIT_LIST_HEAD(&qm_list->list);
368         mutex_init(&qm_list->lock);
369 }
370
371 int hisi_qm_init(struct hisi_qm *qm);
372 void hisi_qm_uninit(struct hisi_qm *qm);
373 int hisi_qm_start(struct hisi_qm *qm);
374 int hisi_qm_stop(struct hisi_qm *qm, enum qm_stop_reason r);
375 struct hisi_qp *hisi_qm_create_qp(struct hisi_qm *qm, u8 alg_type);
376 int hisi_qm_start_qp(struct hisi_qp *qp, unsigned long arg);
377 int hisi_qm_stop_qp(struct hisi_qp *qp);
378 void hisi_qm_release_qp(struct hisi_qp *qp);
379 int hisi_qp_send(struct hisi_qp *qp, const void *msg);
380 int hisi_qm_get_free_qp_num(struct hisi_qm *qm);
381 int hisi_qm_get_vft(struct hisi_qm *qm, u32 *base, u32 *number);
382 void hisi_qm_debug_init(struct hisi_qm *qm);
383 enum qm_hw_ver hisi_qm_get_hw_version(struct pci_dev *pdev);
384 void hisi_qm_debug_regs_clear(struct hisi_qm *qm);
385 int hisi_qm_sriov_enable(struct pci_dev *pdev, int max_vfs);
386 int hisi_qm_sriov_disable(struct pci_dev *pdev, bool is_frozen);
387 int hisi_qm_sriov_configure(struct pci_dev *pdev, int num_vfs);
388 void hisi_qm_dev_err_init(struct hisi_qm *qm);
389 void hisi_qm_dev_err_uninit(struct hisi_qm *qm);
390 pci_ers_result_t hisi_qm_dev_err_detected(struct pci_dev *pdev,
391                                           pci_channel_state_t state);
392 pci_ers_result_t hisi_qm_dev_slot_reset(struct pci_dev *pdev);
393 void hisi_qm_reset_prepare(struct pci_dev *pdev);
394 void hisi_qm_reset_done(struct pci_dev *pdev);
395
396 struct hisi_acc_sgl_pool;
397 struct hisi_acc_hw_sgl *hisi_acc_sg_buf_map_to_hw_sgl(struct device *dev,
398         struct scatterlist *sgl, struct hisi_acc_sgl_pool *pool,
399         u32 index, dma_addr_t *hw_sgl_dma);
400 void hisi_acc_sg_buf_unmap(struct device *dev, struct scatterlist *sgl,
401                            struct hisi_acc_hw_sgl *hw_sgl);
402 struct hisi_acc_sgl_pool *hisi_acc_create_sgl_pool(struct device *dev,
403                                                    u32 count, u32 sge_nr);
404 void hisi_acc_free_sgl_pool(struct device *dev,
405                             struct hisi_acc_sgl_pool *pool);
406 int hisi_qm_alloc_qps_node(struct hisi_qm_list *qm_list, int qp_num,
407                            u8 alg_type, int node, struct hisi_qp **qps);
408 void hisi_qm_free_qps(struct hisi_qp **qps, int qp_num);
409 void hisi_qm_dev_shutdown(struct pci_dev *pdev);
410 void hisi_qm_wait_task_finish(struct hisi_qm *qm, struct hisi_qm_list *qm_list);
411 int hisi_qm_alg_register(struct hisi_qm *qm, struct hisi_qm_list *qm_list);
412 void hisi_qm_alg_unregister(struct hisi_qm *qm, struct hisi_qm_list *qm_list);
413 #endif