xref: /openbmc/linux/drivers/crypto/hisilicon/qm.c (revision 31eeb6b0)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 HiSilicon Limited. */
3 #include <asm/page.h>
4 #include <linux/acpi.h>
5 #include <linux/aer.h>
6 #include <linux/bitmap.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/idr.h>
9 #include <linux/io.h>
10 #include <linux/irqreturn.h>
11 #include <linux/log2.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/seq_file.h>
14 #include <linux/slab.h>
15 #include <linux/uacce.h>
16 #include <linux/uaccess.h>
17 #include <uapi/misc/uacce/hisi_qm.h>
18 #include "qm.h"
19 
20 /* eq/aeq irq enable */
21 #define QM_VF_AEQ_INT_SOURCE		0x0
22 #define QM_VF_AEQ_INT_MASK		0x4
23 #define QM_VF_EQ_INT_SOURCE		0x8
24 #define QM_VF_EQ_INT_MASK		0xc
25 #define QM_IRQ_NUM_V1			1
26 #define QM_IRQ_NUM_PF_V2		4
27 #define QM_IRQ_NUM_VF_V2		2
28 #define QM_IRQ_NUM_VF_V3		3
29 
30 #define QM_EQ_EVENT_IRQ_VECTOR		0
31 #define QM_AEQ_EVENT_IRQ_VECTOR		1
32 #define QM_CMD_EVENT_IRQ_VECTOR		2
33 #define QM_ABNORMAL_EVENT_IRQ_VECTOR	3
34 
35 /* mailbox */
36 #define QM_MB_CMD_SQC			0x0
37 #define QM_MB_CMD_CQC			0x1
38 #define QM_MB_CMD_EQC			0x2
39 #define QM_MB_CMD_AEQC			0x3
40 #define QM_MB_CMD_SQC_BT		0x4
41 #define QM_MB_CMD_CQC_BT		0x5
42 #define QM_MB_CMD_SQC_VFT_V2		0x6
43 #define QM_MB_CMD_STOP_QP		0x8
44 #define QM_MB_CMD_SRC			0xc
45 #define QM_MB_CMD_DST			0xd
46 
47 #define QM_MB_CMD_SEND_BASE		0x300
48 #define QM_MB_EVENT_SHIFT		8
49 #define QM_MB_BUSY_SHIFT		13
50 #define QM_MB_OP_SHIFT			14
51 #define QM_MB_CMD_DATA_ADDR_L		0x304
52 #define QM_MB_CMD_DATA_ADDR_H		0x308
53 #define QM_MB_PING_ALL_VFS		0xffff
54 #define QM_MB_CMD_DATA_SHIFT		32
55 #define QM_MB_CMD_DATA_MASK		GENMASK(31, 0)
56 
57 /* sqc shift */
58 #define QM_SQ_HOP_NUM_SHIFT		0
59 #define QM_SQ_PAGE_SIZE_SHIFT		4
60 #define QM_SQ_BUF_SIZE_SHIFT		8
61 #define QM_SQ_SQE_SIZE_SHIFT		12
62 #define QM_SQ_PRIORITY_SHIFT		0
63 #define QM_SQ_ORDERS_SHIFT		4
64 #define QM_SQ_TYPE_SHIFT		8
65 #define QM_QC_PASID_ENABLE		0x1
66 #define QM_QC_PASID_ENABLE_SHIFT	7
67 
68 #define QM_SQ_TYPE_MASK			GENMASK(3, 0)
69 #define QM_SQ_TAIL_IDX(sqc)		((le16_to_cpu((sqc)->w11) >> 6) & 0x1)
70 
71 /* cqc shift */
72 #define QM_CQ_HOP_NUM_SHIFT		0
73 #define QM_CQ_PAGE_SIZE_SHIFT		4
74 #define QM_CQ_BUF_SIZE_SHIFT		8
75 #define QM_CQ_CQE_SIZE_SHIFT		12
76 #define QM_CQ_PHASE_SHIFT		0
77 #define QM_CQ_FLAG_SHIFT		1
78 
79 #define QM_CQE_PHASE(cqe)		(le16_to_cpu((cqe)->w7) & 0x1)
80 #define QM_QC_CQE_SIZE			4
81 #define QM_CQ_TAIL_IDX(cqc)		((le16_to_cpu((cqc)->w11) >> 6) & 0x1)
82 
83 /* eqc shift */
84 #define QM_EQE_AEQE_SIZE		(2UL << 12)
85 #define QM_EQC_PHASE_SHIFT		16
86 
87 #define QM_EQE_PHASE(eqe)		((le32_to_cpu((eqe)->dw0) >> 16) & 0x1)
88 #define QM_EQE_CQN_MASK			GENMASK(15, 0)
89 
90 #define QM_AEQE_PHASE(aeqe)		((le32_to_cpu((aeqe)->dw0) >> 16) & 0x1)
91 #define QM_AEQE_TYPE_SHIFT		17
92 #define QM_AEQE_CQN_MASK		GENMASK(15, 0)
93 #define QM_CQ_OVERFLOW			0
94 #define QM_EQ_OVERFLOW			1
95 #define QM_CQE_ERROR			2
96 
97 #define QM_DOORBELL_CMD_SQ		0
98 #define QM_DOORBELL_CMD_CQ		1
99 #define QM_DOORBELL_CMD_EQ		2
100 #define QM_DOORBELL_CMD_AEQ		3
101 
102 #define QM_DOORBELL_BASE_V1		0x340
103 #define QM_DB_CMD_SHIFT_V1		16
104 #define QM_DB_INDEX_SHIFT_V1		32
105 #define QM_DB_PRIORITY_SHIFT_V1		48
106 #define QM_DOORBELL_SQ_CQ_BASE_V2	0x1000
107 #define QM_DOORBELL_EQ_AEQ_BASE_V2	0x2000
108 #define QM_QUE_ISO_CFG_V		0x0030
109 #define QM_PAGE_SIZE			0x0034
110 #define QM_QUE_ISO_EN			0x100154
111 #define QM_CAPBILITY			0x100158
112 #define QM_QP_NUN_MASK			GENMASK(10, 0)
113 #define QM_QP_DB_INTERVAL		0x10000
114 #define QM_QP_MAX_NUM_SHIFT		11
115 #define QM_DB_CMD_SHIFT_V2		12
116 #define QM_DB_RAND_SHIFT_V2		16
117 #define QM_DB_INDEX_SHIFT_V2		32
118 #define QM_DB_PRIORITY_SHIFT_V2		48
119 
120 #define QM_MEM_START_INIT		0x100040
121 #define QM_MEM_INIT_DONE		0x100044
122 #define QM_VFT_CFG_RDY			0x10006c
123 #define QM_VFT_CFG_OP_WR		0x100058
124 #define QM_VFT_CFG_TYPE			0x10005c
125 #define QM_SQC_VFT			0x0
126 #define QM_CQC_VFT			0x1
127 #define QM_VFT_CFG			0x100060
128 #define QM_VFT_CFG_OP_ENABLE		0x100054
129 #define QM_PM_CTRL			0x100148
130 #define QM_IDLE_DISABLE			BIT(9)
131 
132 #define QM_VFT_CFG_DATA_L		0x100064
133 #define QM_VFT_CFG_DATA_H		0x100068
134 #define QM_SQC_VFT_BUF_SIZE		(7ULL << 8)
135 #define QM_SQC_VFT_SQC_SIZE		(5ULL << 12)
136 #define QM_SQC_VFT_INDEX_NUMBER		(1ULL << 16)
137 #define QM_SQC_VFT_START_SQN_SHIFT	28
138 #define QM_SQC_VFT_VALID		(1ULL << 44)
139 #define QM_SQC_VFT_SQN_SHIFT		45
140 #define QM_CQC_VFT_BUF_SIZE		(7ULL << 8)
141 #define QM_CQC_VFT_SQC_SIZE		(5ULL << 12)
142 #define QM_CQC_VFT_INDEX_NUMBER		(1ULL << 16)
143 #define QM_CQC_VFT_VALID		(1ULL << 28)
144 
145 #define QM_SQC_VFT_BASE_SHIFT_V2	28
146 #define QM_SQC_VFT_BASE_MASK_V2		GENMASK(15, 0)
147 #define QM_SQC_VFT_NUM_SHIFT_V2		45
148 #define QM_SQC_VFT_NUM_MASK_v2		GENMASK(9, 0)
149 
150 #define QM_DFX_CNT_CLR_CE		0x100118
151 
152 #define QM_ABNORMAL_INT_SOURCE		0x100000
153 #define QM_ABNORMAL_INT_SOURCE_CLR	GENMASK(14, 0)
154 #define QM_ABNORMAL_INT_MASK		0x100004
155 #define QM_ABNORMAL_INT_MASK_VALUE	0x7fff
156 #define QM_ABNORMAL_INT_STATUS		0x100008
157 #define QM_ABNORMAL_INT_SET		0x10000c
158 #define QM_ABNORMAL_INF00		0x100010
159 #define QM_FIFO_OVERFLOW_TYPE		0xc0
160 #define QM_FIFO_OVERFLOW_TYPE_SHIFT	6
161 #define QM_FIFO_OVERFLOW_VF		0x3f
162 #define QM_ABNORMAL_INF01		0x100014
163 #define QM_DB_TIMEOUT_TYPE		0xc0
164 #define QM_DB_TIMEOUT_TYPE_SHIFT	6
165 #define QM_DB_TIMEOUT_VF		0x3f
166 #define QM_RAS_CE_ENABLE		0x1000ec
167 #define QM_RAS_FE_ENABLE		0x1000f0
168 #define QM_RAS_NFE_ENABLE		0x1000f4
169 #define QM_RAS_CE_THRESHOLD		0x1000f8
170 #define QM_RAS_CE_TIMES_PER_IRQ		1
171 #define QM_RAS_MSI_INT_SEL		0x1040f4
172 #define QM_OOO_SHUTDOWN_SEL		0x1040f8
173 
174 #define QM_RESET_WAIT_TIMEOUT		400
175 #define QM_PEH_VENDOR_ID		0x1000d8
176 #define ACC_VENDOR_ID_VALUE		0x5a5a
177 #define QM_PEH_DFX_INFO0		0x1000fc
178 #define QM_PEH_DFX_INFO1		0x100100
179 #define QM_PEH_DFX_MASK			(BIT(0) | BIT(2))
180 #define QM_PEH_MSI_FINISH_MASK		GENMASK(19, 16)
181 #define ACC_PEH_SRIOV_CTRL_VF_MSE_SHIFT	3
182 #define ACC_PEH_MSI_DISABLE		GENMASK(31, 0)
183 #define ACC_MASTER_GLOBAL_CTRL_SHUTDOWN	0x1
184 #define ACC_MASTER_TRANS_RETURN_RW	3
185 #define ACC_MASTER_TRANS_RETURN		0x300150
186 #define ACC_MASTER_GLOBAL_CTRL		0x300000
187 #define ACC_AM_CFG_PORT_WR_EN		0x30001c
188 #define QM_RAS_NFE_MBIT_DISABLE		~QM_ECC_MBIT
189 #define ACC_AM_ROB_ECC_INT_STS		0x300104
190 #define ACC_ROB_ECC_ERR_MULTPL		BIT(1)
191 #define QM_MSI_CAP_ENABLE		BIT(16)
192 
193 /* interfunction communication */
194 #define QM_IFC_READY_STATUS		0x100128
195 #define QM_IFC_C_STS_M			0x10012C
196 #define QM_IFC_INT_SET_P		0x100130
197 #define QM_IFC_INT_CFG			0x100134
198 #define QM_IFC_INT_SOURCE_P		0x100138
199 #define QM_IFC_INT_SOURCE_V		0x0020
200 #define QM_IFC_INT_MASK			0x0024
201 #define QM_IFC_INT_STATUS		0x0028
202 #define QM_IFC_INT_SET_V		0x002C
203 #define QM_IFC_SEND_ALL_VFS		GENMASK(6, 0)
204 #define QM_IFC_INT_SOURCE_CLR		GENMASK(63, 0)
205 #define QM_IFC_INT_SOURCE_MASK		BIT(0)
206 #define QM_IFC_INT_DISABLE		BIT(0)
207 #define QM_IFC_INT_STATUS_MASK		BIT(0)
208 #define QM_IFC_INT_SET_MASK		BIT(0)
209 #define QM_WAIT_DST_ACK			10
210 #define QM_MAX_PF_WAIT_COUNT		10
211 #define QM_MAX_VF_WAIT_COUNT		40
212 #define QM_VF_RESET_WAIT_US            20000
213 #define QM_VF_RESET_WAIT_CNT           3000
214 #define QM_VF_RESET_WAIT_TIMEOUT_US    \
215 	(QM_VF_RESET_WAIT_US * QM_VF_RESET_WAIT_CNT)
216 
217 #define QM_DFX_MB_CNT_VF		0x104010
218 #define QM_DFX_DB_CNT_VF		0x104020
219 #define QM_DFX_SQE_CNT_VF_SQN		0x104030
220 #define QM_DFX_CQE_CNT_VF_CQN		0x104040
221 #define QM_DFX_QN_SHIFT			16
222 #define CURRENT_FUN_MASK		GENMASK(5, 0)
223 #define CURRENT_Q_MASK			GENMASK(31, 16)
224 
225 #define POLL_PERIOD			10
226 #define POLL_TIMEOUT			1000
227 #define WAIT_PERIOD_US_MAX		200
228 #define WAIT_PERIOD_US_MIN		100
229 #define MAX_WAIT_COUNTS			1000
230 #define QM_CACHE_WB_START		0x204
231 #define QM_CACHE_WB_DONE		0x208
232 
233 #define PCI_BAR_2			2
234 #define PCI_BAR_4			4
235 #define QM_SQE_DATA_ALIGN_MASK		GENMASK(6, 0)
236 #define QMC_ALIGN(sz)			ALIGN(sz, 32)
237 
238 #define QM_DBG_READ_LEN		256
239 #define QM_DBG_WRITE_LEN		1024
240 #define QM_DBG_TMP_BUF_LEN		22
241 #define QM_PCI_COMMAND_INVALID		~0
242 #define QM_RESET_STOP_TX_OFFSET		1
243 #define QM_RESET_STOP_RX_OFFSET		2
244 
245 #define WAIT_PERIOD			20
246 #define REMOVE_WAIT_DELAY		10
247 #define QM_SQE_ADDR_MASK		GENMASK(7, 0)
248 #define QM_EQ_DEPTH			(1024 * 2)
249 
250 #define QM_DRIVER_REMOVING		0
251 #define QM_RST_SCHED			1
252 #define QM_RESETTING			2
253 #define QM_QOS_PARAM_NUM		2
254 #define QM_QOS_VAL_NUM			1
255 #define QM_QOS_BDF_PARAM_NUM		4
256 #define QM_QOS_MAX_VAL			1000
257 #define QM_QOS_RATE			100
258 #define QM_QOS_EXPAND_RATE		1000
259 #define QM_SHAPER_CIR_B_MASK		GENMASK(7, 0)
260 #define QM_SHAPER_CIR_U_MASK		GENMASK(10, 8)
261 #define QM_SHAPER_CIR_S_MASK		GENMASK(14, 11)
262 #define QM_SHAPER_FACTOR_CIR_U_SHIFT	8
263 #define QM_SHAPER_FACTOR_CIR_S_SHIFT	11
264 #define QM_SHAPER_FACTOR_CBS_B_SHIFT	15
265 #define QM_SHAPER_FACTOR_CBS_S_SHIFT	19
266 #define QM_SHAPER_CBS_B			1
267 #define QM_SHAPER_CBS_S			16
268 #define QM_SHAPER_VFT_OFFSET		6
269 #define WAIT_FOR_QOS_VF			100
270 #define QM_QOS_MIN_ERROR_RATE		5
271 #define QM_QOS_TYPICAL_NUM		8
272 #define QM_SHAPER_MIN_CBS_S		8
273 #define QM_QOS_TICK			0x300U
274 #define QM_QOS_DIVISOR_CLK		0x1f40U
275 #define QM_QOS_MAX_CIR_B		200
276 #define QM_QOS_MIN_CIR_B		100
277 #define QM_QOS_MAX_CIR_U		6
278 #define QM_QOS_MAX_CIR_S		11
279 #define QM_QOS_VAL_MAX_LEN		32
280 
281 #define QM_AUTOSUSPEND_DELAY		3000
282 
283 #define QM_MK_CQC_DW3_V1(hop_num, pg_sz, buf_sz, cqe_sz) \
284 	(((hop_num) << QM_CQ_HOP_NUM_SHIFT)	| \
285 	((pg_sz) << QM_CQ_PAGE_SIZE_SHIFT)	| \
286 	((buf_sz) << QM_CQ_BUF_SIZE_SHIFT)	| \
287 	((cqe_sz) << QM_CQ_CQE_SIZE_SHIFT))
288 
289 #define QM_MK_CQC_DW3_V2(cqe_sz) \
290 	((QM_Q_DEPTH - 1) | ((cqe_sz) << QM_CQ_CQE_SIZE_SHIFT))
291 
292 #define QM_MK_SQC_W13(priority, orders, alg_type) \
293 	(((priority) << QM_SQ_PRIORITY_SHIFT)	| \
294 	((orders) << QM_SQ_ORDERS_SHIFT)	| \
295 	(((alg_type) & QM_SQ_TYPE_MASK) << QM_SQ_TYPE_SHIFT))
296 
297 #define QM_MK_SQC_DW3_V1(hop_num, pg_sz, buf_sz, sqe_sz) \
298 	(((hop_num) << QM_SQ_HOP_NUM_SHIFT)	| \
299 	((pg_sz) << QM_SQ_PAGE_SIZE_SHIFT)	| \
300 	((buf_sz) << QM_SQ_BUF_SIZE_SHIFT)	| \
301 	((u32)ilog2(sqe_sz) << QM_SQ_SQE_SIZE_SHIFT))
302 
303 #define QM_MK_SQC_DW3_V2(sqe_sz) \
304 	((QM_Q_DEPTH - 1) | ((u32)ilog2(sqe_sz) << QM_SQ_SQE_SIZE_SHIFT))
305 
306 #define INIT_QC_COMMON(qc, base, pasid) do {			\
307 	(qc)->head = 0;						\
308 	(qc)->tail = 0;						\
309 	(qc)->base_l = cpu_to_le32(lower_32_bits(base));	\
310 	(qc)->base_h = cpu_to_le32(upper_32_bits(base));	\
311 	(qc)->dw3 = 0;						\
312 	(qc)->w8 = 0;						\
313 	(qc)->rsvd0 = 0;					\
314 	(qc)->pasid = cpu_to_le16(pasid);			\
315 	(qc)->w11 = 0;						\
316 	(qc)->rsvd1 = 0;					\
317 } while (0)
318 
319 enum vft_type {
320 	SQC_VFT = 0,
321 	CQC_VFT,
322 	SHAPER_VFT,
323 };
324 
325 enum acc_err_result {
326 	ACC_ERR_NONE,
327 	ACC_ERR_NEED_RESET,
328 	ACC_ERR_RECOVERED,
329 };
330 
331 enum qm_alg_type {
332 	ALG_TYPE_0,
333 	ALG_TYPE_1,
334 };
335 
336 enum qm_mb_cmd {
337 	QM_PF_FLR_PREPARE = 0x01,
338 	QM_PF_SRST_PREPARE,
339 	QM_PF_RESET_DONE,
340 	QM_VF_PREPARE_DONE,
341 	QM_VF_PREPARE_FAIL,
342 	QM_VF_START_DONE,
343 	QM_VF_START_FAIL,
344 	QM_PF_SET_QOS,
345 	QM_VF_GET_QOS,
346 };
347 
348 struct qm_cqe {
349 	__le32 rsvd0;
350 	__le16 cmd_id;
351 	__le16 rsvd1;
352 	__le16 sq_head;
353 	__le16 sq_num;
354 	__le16 rsvd2;
355 	__le16 w7;
356 };
357 
358 struct qm_eqe {
359 	__le32 dw0;
360 };
361 
362 struct qm_aeqe {
363 	__le32 dw0;
364 };
365 
366 struct qm_sqc {
367 	__le16 head;
368 	__le16 tail;
369 	__le32 base_l;
370 	__le32 base_h;
371 	__le32 dw3;
372 	__le16 w8;
373 	__le16 rsvd0;
374 	__le16 pasid;
375 	__le16 w11;
376 	__le16 cq_num;
377 	__le16 w13;
378 	__le32 rsvd1;
379 };
380 
381 struct qm_cqc {
382 	__le16 head;
383 	__le16 tail;
384 	__le32 base_l;
385 	__le32 base_h;
386 	__le32 dw3;
387 	__le16 w8;
388 	__le16 rsvd0;
389 	__le16 pasid;
390 	__le16 w11;
391 	__le32 dw6;
392 	__le32 rsvd1;
393 };
394 
395 struct qm_eqc {
396 	__le16 head;
397 	__le16 tail;
398 	__le32 base_l;
399 	__le32 base_h;
400 	__le32 dw3;
401 	__le32 rsvd[2];
402 	__le32 dw6;
403 };
404 
405 struct qm_aeqc {
406 	__le16 head;
407 	__le16 tail;
408 	__le32 base_l;
409 	__le32 base_h;
410 	__le32 dw3;
411 	__le32 rsvd[2];
412 	__le32 dw6;
413 };
414 
415 struct qm_mailbox {
416 	__le16 w0;
417 	__le16 queue_num;
418 	__le32 base_l;
419 	__le32 base_h;
420 	__le32 rsvd;
421 };
422 
423 struct qm_doorbell {
424 	__le16 queue_num;
425 	__le16 cmd;
426 	__le16 index;
427 	__le16 priority;
428 };
429 
430 struct hisi_qm_resource {
431 	struct hisi_qm *qm;
432 	int distance;
433 	struct list_head list;
434 };
435 
436 struct hisi_qm_hw_ops {
437 	int (*get_vft)(struct hisi_qm *qm, u32 *base, u32 *number);
438 	void (*qm_db)(struct hisi_qm *qm, u16 qn,
439 		      u8 cmd, u16 index, u8 priority);
440 	u32 (*get_irq_num)(struct hisi_qm *qm);
441 	int (*debug_init)(struct hisi_qm *qm);
442 	void (*hw_error_init)(struct hisi_qm *qm, u32 ce, u32 nfe, u32 fe);
443 	void (*hw_error_uninit)(struct hisi_qm *qm);
444 	enum acc_err_result (*hw_error_handle)(struct hisi_qm *qm);
445 	int (*stop_qp)(struct hisi_qp *qp);
446 	int (*set_msi)(struct hisi_qm *qm, bool set);
447 	int (*ping_all_vfs)(struct hisi_qm *qm, u64 cmd);
448 	int (*ping_pf)(struct hisi_qm *qm, u64 cmd);
449 };
450 
451 struct qm_dfx_item {
452 	const char *name;
453 	u32 offset;
454 };
455 
456 static struct qm_dfx_item qm_dfx_files[] = {
457 	{"err_irq", offsetof(struct qm_dfx, err_irq_cnt)},
458 	{"aeq_irq", offsetof(struct qm_dfx, aeq_irq_cnt)},
459 	{"abnormal_irq", offsetof(struct qm_dfx, abnormal_irq_cnt)},
460 	{"create_qp_err", offsetof(struct qm_dfx, create_qp_err_cnt)},
461 	{"mb_err", offsetof(struct qm_dfx, mb_err_cnt)},
462 };
463 
464 static const char * const qm_debug_file_name[] = {
465 	[CURRENT_QM]   = "current_qm",
466 	[CURRENT_Q]    = "current_q",
467 	[CLEAR_ENABLE] = "clear_enable",
468 };
469 
470 struct hisi_qm_hw_error {
471 	u32 int_msk;
472 	const char *msg;
473 };
474 
475 static const struct hisi_qm_hw_error qm_hw_error[] = {
476 	{ .int_msk = BIT(0), .msg = "qm_axi_rresp" },
477 	{ .int_msk = BIT(1), .msg = "qm_axi_bresp" },
478 	{ .int_msk = BIT(2), .msg = "qm_ecc_mbit" },
479 	{ .int_msk = BIT(3), .msg = "qm_ecc_1bit" },
480 	{ .int_msk = BIT(4), .msg = "qm_acc_get_task_timeout" },
481 	{ .int_msk = BIT(5), .msg = "qm_acc_do_task_timeout" },
482 	{ .int_msk = BIT(6), .msg = "qm_acc_wb_not_ready_timeout" },
483 	{ .int_msk = BIT(7), .msg = "qm_sq_cq_vf_invalid" },
484 	{ .int_msk = BIT(8), .msg = "qm_cq_vf_invalid" },
485 	{ .int_msk = BIT(9), .msg = "qm_sq_vf_invalid" },
486 	{ .int_msk = BIT(10), .msg = "qm_db_timeout" },
487 	{ .int_msk = BIT(11), .msg = "qm_of_fifo_of" },
488 	{ .int_msk = BIT(12), .msg = "qm_db_random_invalid" },
489 	{ .int_msk = BIT(13), .msg = "qm_mailbox_timeout" },
490 	{ .int_msk = BIT(14), .msg = "qm_flr_timeout" },
491 	{ /* sentinel */ }
492 };
493 
494 static const char * const qm_db_timeout[] = {
495 	"sq", "cq", "eq", "aeq",
496 };
497 
498 static const char * const qm_fifo_overflow[] = {
499 	"cq", "eq", "aeq",
500 };
501 
502 static const char * const qm_s[] = {
503 	"init", "start", "close", "stop",
504 };
505 
506 static const char * const qp_s[] = {
507 	"none", "init", "start", "stop", "close",
508 };
509 
510 struct qm_typical_qos_table {
511 	u32 start;
512 	u32 end;
513 	u32 val;
514 };
515 
516 /* the qos step is 100 */
517 static struct qm_typical_qos_table shaper_cir_s[] = {
518 	{100, 100, 4},
519 	{200, 200, 3},
520 	{300, 500, 2},
521 	{600, 1000, 1},
522 	{1100, 100000, 0},
523 };
524 
525 static struct qm_typical_qos_table shaper_cbs_s[] = {
526 	{100, 200, 9},
527 	{300, 500, 11},
528 	{600, 1000, 12},
529 	{1100, 10000, 16},
530 	{10100, 25000, 17},
531 	{25100, 50000, 18},
532 	{50100, 100000, 19}
533 };
534 
535 static bool qm_avail_state(struct hisi_qm *qm, enum qm_state new)
536 {
537 	enum qm_state curr = atomic_read(&qm->status.flags);
538 	bool avail = false;
539 
540 	switch (curr) {
541 	case QM_INIT:
542 		if (new == QM_START || new == QM_CLOSE)
543 			avail = true;
544 		break;
545 	case QM_START:
546 		if (new == QM_STOP)
547 			avail = true;
548 		break;
549 	case QM_STOP:
550 		if (new == QM_CLOSE || new == QM_START)
551 			avail = true;
552 		break;
553 	default:
554 		break;
555 	}
556 
557 	dev_dbg(&qm->pdev->dev, "change qm state from %s to %s\n",
558 		qm_s[curr], qm_s[new]);
559 
560 	if (!avail)
561 		dev_warn(&qm->pdev->dev, "Can not change qm state from %s to %s\n",
562 			 qm_s[curr], qm_s[new]);
563 
564 	return avail;
565 }
566 
567 static bool qm_qp_avail_state(struct hisi_qm *qm, struct hisi_qp *qp,
568 			      enum qp_state new)
569 {
570 	enum qm_state qm_curr = atomic_read(&qm->status.flags);
571 	enum qp_state qp_curr = 0;
572 	bool avail = false;
573 
574 	if (qp)
575 		qp_curr = atomic_read(&qp->qp_status.flags);
576 
577 	switch (new) {
578 	case QP_INIT:
579 		if (qm_curr == QM_START || qm_curr == QM_INIT)
580 			avail = true;
581 		break;
582 	case QP_START:
583 		if ((qm_curr == QM_START && qp_curr == QP_INIT) ||
584 		    (qm_curr == QM_START && qp_curr == QP_STOP))
585 			avail = true;
586 		break;
587 	case QP_STOP:
588 		if ((qm_curr == QM_START && qp_curr == QP_START) ||
589 		    (qp_curr == QP_INIT))
590 			avail = true;
591 		break;
592 	case QP_CLOSE:
593 		if ((qm_curr == QM_START && qp_curr == QP_INIT) ||
594 		    (qm_curr == QM_START && qp_curr == QP_STOP) ||
595 		    (qm_curr == QM_STOP && qp_curr == QP_STOP)  ||
596 		    (qm_curr == QM_STOP && qp_curr == QP_INIT))
597 			avail = true;
598 		break;
599 	default:
600 		break;
601 	}
602 
603 	dev_dbg(&qm->pdev->dev, "change qp state from %s to %s in QM %s\n",
604 		qp_s[qp_curr], qp_s[new], qm_s[qm_curr]);
605 
606 	if (!avail)
607 		dev_warn(&qm->pdev->dev,
608 			 "Can not change qp state from %s to %s in QM %s\n",
609 			 qp_s[qp_curr], qp_s[new], qm_s[qm_curr]);
610 
611 	return avail;
612 }
613 
614 static u32 qm_get_hw_error_status(struct hisi_qm *qm)
615 {
616 	return readl(qm->io_base + QM_ABNORMAL_INT_STATUS);
617 }
618 
619 static u32 qm_get_dev_err_status(struct hisi_qm *qm)
620 {
621 	return qm->err_ini->get_dev_hw_err_status(qm);
622 }
623 
624 /* Check if the error causes the master ooo block */
625 static int qm_check_dev_error(struct hisi_qm *qm)
626 {
627 	u32 val, dev_val;
628 
629 	if (qm->fun_type == QM_HW_VF)
630 		return 0;
631 
632 	val = qm_get_hw_error_status(qm);
633 	dev_val = qm_get_dev_err_status(qm);
634 
635 	if (qm->ver < QM_HW_V3)
636 		return (val & QM_ECC_MBIT) ||
637 		       (dev_val & qm->err_info.ecc_2bits_mask);
638 
639 	return (val & readl(qm->io_base + QM_OOO_SHUTDOWN_SEL)) ||
640 	       (dev_val & (~qm->err_info.dev_ce_mask));
641 }
642 
643 static int qm_wait_reset_finish(struct hisi_qm *qm)
644 {
645 	int delay = 0;
646 
647 	/* All reset requests need to be queued for processing */
648 	while (test_and_set_bit(QM_RESETTING, &qm->misc_ctl)) {
649 		msleep(++delay);
650 		if (delay > QM_RESET_WAIT_TIMEOUT)
651 			return -EBUSY;
652 	}
653 
654 	return 0;
655 }
656 
657 static int qm_reset_prepare_ready(struct hisi_qm *qm)
658 {
659 	struct pci_dev *pdev = qm->pdev;
660 	struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(pdev));
661 
662 	/*
663 	 * PF and VF on host doesnot support resetting at the
664 	 * same time on Kunpeng920.
665 	 */
666 	if (qm->ver < QM_HW_V3)
667 		return qm_wait_reset_finish(pf_qm);
668 
669 	return qm_wait_reset_finish(qm);
670 }
671 
672 static void qm_reset_bit_clear(struct hisi_qm *qm)
673 {
674 	struct pci_dev *pdev = qm->pdev;
675 	struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(pdev));
676 
677 	if (qm->ver < QM_HW_V3)
678 		clear_bit(QM_RESETTING, &pf_qm->misc_ctl);
679 
680 	clear_bit(QM_RESETTING, &qm->misc_ctl);
681 }
682 
683 static void qm_mb_pre_init(struct qm_mailbox *mailbox, u8 cmd,
684 			   u64 base, u16 queue, bool op)
685 {
686 	mailbox->w0 = cpu_to_le16((cmd) |
687 		((op) ? 0x1 << QM_MB_OP_SHIFT : 0) |
688 		(0x1 << QM_MB_BUSY_SHIFT));
689 	mailbox->queue_num = cpu_to_le16(queue);
690 	mailbox->base_l = cpu_to_le32(lower_32_bits(base));
691 	mailbox->base_h = cpu_to_le32(upper_32_bits(base));
692 	mailbox->rsvd = 0;
693 }
694 
695 /* return 0 mailbox ready, -ETIMEDOUT hardware timeout */
696 static int qm_wait_mb_ready(struct hisi_qm *qm)
697 {
698 	u32 val;
699 
700 	return readl_relaxed_poll_timeout(qm->io_base + QM_MB_CMD_SEND_BASE,
701 					  val, !((val >> QM_MB_BUSY_SHIFT) &
702 					  0x1), POLL_PERIOD, POLL_TIMEOUT);
703 }
704 
705 /* 128 bit should be written to hardware at one time to trigger a mailbox */
706 static void qm_mb_write(struct hisi_qm *qm, const void *src)
707 {
708 	void __iomem *fun_base = qm->io_base + QM_MB_CMD_SEND_BASE;
709 	unsigned long tmp0 = 0, tmp1 = 0;
710 
711 	if (!IS_ENABLED(CONFIG_ARM64)) {
712 		memcpy_toio(fun_base, src, 16);
713 		wmb();
714 		return;
715 	}
716 
717 	asm volatile("ldp %0, %1, %3\n"
718 		     "stp %0, %1, %2\n"
719 		     "dsb sy\n"
720 		     : "=&r" (tmp0),
721 		       "=&r" (tmp1),
722 		       "+Q" (*((char __iomem *)fun_base))
723 		     : "Q" (*((char *)src))
724 		     : "memory");
725 }
726 
727 static int qm_mb_nolock(struct hisi_qm *qm, struct qm_mailbox *mailbox)
728 {
729 	if (unlikely(qm_wait_mb_ready(qm))) {
730 		dev_err(&qm->pdev->dev, "QM mailbox is busy to start!\n");
731 		goto mb_busy;
732 	}
733 
734 	qm_mb_write(qm, mailbox);
735 
736 	if (unlikely(qm_wait_mb_ready(qm))) {
737 		dev_err(&qm->pdev->dev, "QM mailbox operation timeout!\n");
738 		goto mb_busy;
739 	}
740 
741 	return 0;
742 
743 mb_busy:
744 	atomic64_inc(&qm->debug.dfx.mb_err_cnt);
745 	return -EBUSY;
746 }
747 
748 static int qm_mb(struct hisi_qm *qm, u8 cmd, dma_addr_t dma_addr, u16 queue,
749 		 bool op)
750 {
751 	struct qm_mailbox mailbox;
752 	int ret;
753 
754 	dev_dbg(&qm->pdev->dev, "QM mailbox request to q%u: %u-%llx\n",
755 		queue, cmd, (unsigned long long)dma_addr);
756 
757 	qm_mb_pre_init(&mailbox, cmd, dma_addr, queue, op);
758 
759 	mutex_lock(&qm->mailbox_lock);
760 	ret = qm_mb_nolock(qm, &mailbox);
761 	mutex_unlock(&qm->mailbox_lock);
762 
763 	return ret;
764 }
765 
766 static void qm_db_v1(struct hisi_qm *qm, u16 qn, u8 cmd, u16 index, u8 priority)
767 {
768 	u64 doorbell;
769 
770 	doorbell = qn | ((u64)cmd << QM_DB_CMD_SHIFT_V1) |
771 		   ((u64)index << QM_DB_INDEX_SHIFT_V1)  |
772 		   ((u64)priority << QM_DB_PRIORITY_SHIFT_V1);
773 
774 	writeq(doorbell, qm->io_base + QM_DOORBELL_BASE_V1);
775 }
776 
777 static void qm_db_v2(struct hisi_qm *qm, u16 qn, u8 cmd, u16 index, u8 priority)
778 {
779 	void __iomem *io_base = qm->io_base;
780 	u16 randata = 0;
781 	u64 doorbell;
782 
783 	if (cmd == QM_DOORBELL_CMD_SQ || cmd == QM_DOORBELL_CMD_CQ)
784 		io_base = qm->db_io_base + (u64)qn * qm->db_interval +
785 			  QM_DOORBELL_SQ_CQ_BASE_V2;
786 	else
787 		io_base += QM_DOORBELL_EQ_AEQ_BASE_V2;
788 
789 	doorbell = qn | ((u64)cmd << QM_DB_CMD_SHIFT_V2) |
790 		   ((u64)randata << QM_DB_RAND_SHIFT_V2) |
791 		   ((u64)index << QM_DB_INDEX_SHIFT_V2)	 |
792 		   ((u64)priority << QM_DB_PRIORITY_SHIFT_V2);
793 
794 	writeq(doorbell, io_base);
795 }
796 
797 static void qm_db(struct hisi_qm *qm, u16 qn, u8 cmd, u16 index, u8 priority)
798 {
799 	dev_dbg(&qm->pdev->dev, "QM doorbell request: qn=%u, cmd=%u, index=%u\n",
800 		qn, cmd, index);
801 
802 	qm->ops->qm_db(qm, qn, cmd, index, priority);
803 }
804 
805 static void qm_disable_clock_gate(struct hisi_qm *qm)
806 {
807 	u32 val;
808 
809 	/* if qm enables clock gating in Kunpeng930, qos will be inaccurate. */
810 	if (qm->ver < QM_HW_V3)
811 		return;
812 
813 	val = readl(qm->io_base + QM_PM_CTRL);
814 	val |= QM_IDLE_DISABLE;
815 	writel(val, qm->io_base +  QM_PM_CTRL);
816 }
817 
818 static int qm_dev_mem_reset(struct hisi_qm *qm)
819 {
820 	u32 val;
821 
822 	writel(0x1, qm->io_base + QM_MEM_START_INIT);
823 	return readl_relaxed_poll_timeout(qm->io_base + QM_MEM_INIT_DONE, val,
824 					  val & BIT(0), POLL_PERIOD,
825 					  POLL_TIMEOUT);
826 }
827 
828 static u32 qm_get_irq_num_v1(struct hisi_qm *qm)
829 {
830 	return QM_IRQ_NUM_V1;
831 }
832 
833 static u32 qm_get_irq_num_v2(struct hisi_qm *qm)
834 {
835 	if (qm->fun_type == QM_HW_PF)
836 		return QM_IRQ_NUM_PF_V2;
837 	else
838 		return QM_IRQ_NUM_VF_V2;
839 }
840 
841 static u32 qm_get_irq_num_v3(struct hisi_qm *qm)
842 {
843 	if (qm->fun_type == QM_HW_PF)
844 		return QM_IRQ_NUM_PF_V2;
845 
846 	return QM_IRQ_NUM_VF_V3;
847 }
848 
849 static int qm_pm_get_sync(struct hisi_qm *qm)
850 {
851 	struct device *dev = &qm->pdev->dev;
852 	int ret;
853 
854 	if (qm->fun_type == QM_HW_VF || qm->ver < QM_HW_V3)
855 		return 0;
856 
857 	ret = pm_runtime_resume_and_get(dev);
858 	if (ret < 0) {
859 		dev_err(dev, "failed to get_sync(%d).\n", ret);
860 		return ret;
861 	}
862 
863 	return 0;
864 }
865 
866 static void qm_pm_put_sync(struct hisi_qm *qm)
867 {
868 	struct device *dev = &qm->pdev->dev;
869 
870 	if (qm->fun_type == QM_HW_VF || qm->ver < QM_HW_V3)
871 		return;
872 
873 	pm_runtime_mark_last_busy(dev);
874 	pm_runtime_put_autosuspend(dev);
875 }
876 
877 static struct hisi_qp *qm_to_hisi_qp(struct hisi_qm *qm, struct qm_eqe *eqe)
878 {
879 	u16 cqn = le32_to_cpu(eqe->dw0) & QM_EQE_CQN_MASK;
880 
881 	return &qm->qp_array[cqn];
882 }
883 
884 static void qm_cq_head_update(struct hisi_qp *qp)
885 {
886 	if (qp->qp_status.cq_head == QM_Q_DEPTH - 1) {
887 		qp->qp_status.cqc_phase = !qp->qp_status.cqc_phase;
888 		qp->qp_status.cq_head = 0;
889 	} else {
890 		qp->qp_status.cq_head++;
891 	}
892 }
893 
894 static void qm_poll_qp(struct hisi_qp *qp, struct hisi_qm *qm)
895 {
896 	if (unlikely(atomic_read(&qp->qp_status.flags) == QP_STOP))
897 		return;
898 
899 	if (qp->event_cb) {
900 		qp->event_cb(qp);
901 		return;
902 	}
903 
904 	if (qp->req_cb) {
905 		struct qm_cqe *cqe = qp->cqe + qp->qp_status.cq_head;
906 
907 		while (QM_CQE_PHASE(cqe) == qp->qp_status.cqc_phase) {
908 			dma_rmb();
909 			qp->req_cb(qp, qp->sqe + qm->sqe_size *
910 				   le16_to_cpu(cqe->sq_head));
911 			qm_cq_head_update(qp);
912 			cqe = qp->cqe + qp->qp_status.cq_head;
913 			qm_db(qm, qp->qp_id, QM_DOORBELL_CMD_CQ,
914 			      qp->qp_status.cq_head, 0);
915 			atomic_dec(&qp->qp_status.used);
916 		}
917 
918 		/* set c_flag */
919 		qm_db(qm, qp->qp_id, QM_DOORBELL_CMD_CQ,
920 		      qp->qp_status.cq_head, 1);
921 	}
922 }
923 
924 static void qm_work_process(struct work_struct *work)
925 {
926 	struct hisi_qm *qm = container_of(work, struct hisi_qm, work);
927 	struct qm_eqe *eqe = qm->eqe + qm->status.eq_head;
928 	struct hisi_qp *qp;
929 	int eqe_num = 0;
930 
931 	while (QM_EQE_PHASE(eqe) == qm->status.eqc_phase) {
932 		eqe_num++;
933 		qp = qm_to_hisi_qp(qm, eqe);
934 		qm_poll_qp(qp, qm);
935 
936 		if (qm->status.eq_head == QM_EQ_DEPTH - 1) {
937 			qm->status.eqc_phase = !qm->status.eqc_phase;
938 			eqe = qm->eqe;
939 			qm->status.eq_head = 0;
940 		} else {
941 			eqe++;
942 			qm->status.eq_head++;
943 		}
944 
945 		if (eqe_num == QM_EQ_DEPTH / 2 - 1) {
946 			eqe_num = 0;
947 			qm_db(qm, 0, QM_DOORBELL_CMD_EQ, qm->status.eq_head, 0);
948 		}
949 	}
950 
951 	qm_db(qm, 0, QM_DOORBELL_CMD_EQ, qm->status.eq_head, 0);
952 }
953 
954 static irqreturn_t do_qm_irq(int irq, void *data)
955 {
956 	struct hisi_qm *qm = (struct hisi_qm *)data;
957 
958 	/* the workqueue created by device driver of QM */
959 	if (qm->wq)
960 		queue_work(qm->wq, &qm->work);
961 	else
962 		schedule_work(&qm->work);
963 
964 	return IRQ_HANDLED;
965 }
966 
967 static irqreturn_t qm_irq(int irq, void *data)
968 {
969 	struct hisi_qm *qm = data;
970 
971 	if (readl(qm->io_base + QM_VF_EQ_INT_SOURCE))
972 		return do_qm_irq(irq, data);
973 
974 	atomic64_inc(&qm->debug.dfx.err_irq_cnt);
975 	dev_err(&qm->pdev->dev, "invalid int source\n");
976 	qm_db(qm, 0, QM_DOORBELL_CMD_EQ, qm->status.eq_head, 0);
977 
978 	return IRQ_NONE;
979 }
980 
981 static irqreturn_t qm_mb_cmd_irq(int irq, void *data)
982 {
983 	struct hisi_qm *qm = data;
984 	u32 val;
985 
986 	val = readl(qm->io_base + QM_IFC_INT_STATUS);
987 	val &= QM_IFC_INT_STATUS_MASK;
988 	if (!val)
989 		return IRQ_NONE;
990 
991 	schedule_work(&qm->cmd_process);
992 
993 	return IRQ_HANDLED;
994 }
995 
996 static void qm_set_qp_disable(struct hisi_qp *qp, int offset)
997 {
998 	u32 *addr;
999 
1000 	if (qp->is_in_kernel)
1001 		return;
1002 
1003 	addr = (u32 *)(qp->qdma.va + qp->qdma.size) - offset;
1004 	*addr = 1;
1005 
1006 	/* make sure setup is completed */
1007 	mb();
1008 }
1009 
1010 static void qm_disable_qp(struct hisi_qm *qm, u32 qp_id)
1011 {
1012 	struct hisi_qp *qp = &qm->qp_array[qp_id];
1013 
1014 	qm_set_qp_disable(qp, QM_RESET_STOP_TX_OFFSET);
1015 	hisi_qm_stop_qp(qp);
1016 	qm_set_qp_disable(qp, QM_RESET_STOP_RX_OFFSET);
1017 }
1018 
1019 static void qm_reset_function(struct hisi_qm *qm)
1020 {
1021 	struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(qm->pdev));
1022 	struct device *dev = &qm->pdev->dev;
1023 	int ret;
1024 
1025 	if (qm_check_dev_error(pf_qm))
1026 		return;
1027 
1028 	ret = qm_reset_prepare_ready(qm);
1029 	if (ret) {
1030 		dev_err(dev, "reset function not ready\n");
1031 		return;
1032 	}
1033 
1034 	ret = hisi_qm_stop(qm, QM_FLR);
1035 	if (ret) {
1036 		dev_err(dev, "failed to stop qm when reset function\n");
1037 		goto clear_bit;
1038 	}
1039 
1040 	ret = hisi_qm_start(qm);
1041 	if (ret)
1042 		dev_err(dev, "failed to start qm when reset function\n");
1043 
1044 clear_bit:
1045 	qm_reset_bit_clear(qm);
1046 }
1047 
1048 static irqreturn_t qm_aeq_thread(int irq, void *data)
1049 {
1050 	struct hisi_qm *qm = data;
1051 	struct qm_aeqe *aeqe = qm->aeqe + qm->status.aeq_head;
1052 	u32 type, qp_id;
1053 
1054 	while (QM_AEQE_PHASE(aeqe) == qm->status.aeqc_phase) {
1055 		type = le32_to_cpu(aeqe->dw0) >> QM_AEQE_TYPE_SHIFT;
1056 		qp_id = le32_to_cpu(aeqe->dw0) & QM_AEQE_CQN_MASK;
1057 
1058 		switch (type) {
1059 		case QM_EQ_OVERFLOW:
1060 			dev_err(&qm->pdev->dev, "eq overflow, reset function\n");
1061 			qm_reset_function(qm);
1062 			return IRQ_HANDLED;
1063 		case QM_CQ_OVERFLOW:
1064 			dev_err(&qm->pdev->dev, "cq overflow, stop qp(%u)\n",
1065 				qp_id);
1066 			fallthrough;
1067 		case QM_CQE_ERROR:
1068 			qm_disable_qp(qm, qp_id);
1069 			break;
1070 		default:
1071 			dev_err(&qm->pdev->dev, "unknown error type %u\n",
1072 				type);
1073 			break;
1074 		}
1075 
1076 		if (qm->status.aeq_head == QM_Q_DEPTH - 1) {
1077 			qm->status.aeqc_phase = !qm->status.aeqc_phase;
1078 			aeqe = qm->aeqe;
1079 			qm->status.aeq_head = 0;
1080 		} else {
1081 			aeqe++;
1082 			qm->status.aeq_head++;
1083 		}
1084 	}
1085 
1086 	qm_db(qm, 0, QM_DOORBELL_CMD_AEQ, qm->status.aeq_head, 0);
1087 
1088 	return IRQ_HANDLED;
1089 }
1090 
1091 static irqreturn_t qm_aeq_irq(int irq, void *data)
1092 {
1093 	struct hisi_qm *qm = data;
1094 
1095 	atomic64_inc(&qm->debug.dfx.aeq_irq_cnt);
1096 	if (!readl(qm->io_base + QM_VF_AEQ_INT_SOURCE))
1097 		return IRQ_NONE;
1098 
1099 	return IRQ_WAKE_THREAD;
1100 }
1101 
1102 static void qm_irq_unregister(struct hisi_qm *qm)
1103 {
1104 	struct pci_dev *pdev = qm->pdev;
1105 
1106 	free_irq(pci_irq_vector(pdev, QM_EQ_EVENT_IRQ_VECTOR), qm);
1107 
1108 	if (qm->ver > QM_HW_V1) {
1109 		free_irq(pci_irq_vector(pdev, QM_AEQ_EVENT_IRQ_VECTOR), qm);
1110 
1111 		if (qm->fun_type == QM_HW_PF)
1112 			free_irq(pci_irq_vector(pdev,
1113 				 QM_ABNORMAL_EVENT_IRQ_VECTOR), qm);
1114 	}
1115 
1116 	if (qm->ver > QM_HW_V2)
1117 		free_irq(pci_irq_vector(pdev, QM_CMD_EVENT_IRQ_VECTOR), qm);
1118 }
1119 
1120 static void qm_init_qp_status(struct hisi_qp *qp)
1121 {
1122 	struct hisi_qp_status *qp_status = &qp->qp_status;
1123 
1124 	qp_status->sq_tail = 0;
1125 	qp_status->cq_head = 0;
1126 	qp_status->cqc_phase = true;
1127 	atomic_set(&qp_status->used, 0);
1128 }
1129 
1130 static void qm_init_prefetch(struct hisi_qm *qm)
1131 {
1132 	struct device *dev = &qm->pdev->dev;
1133 	u32 page_type = 0x0;
1134 
1135 	if (qm->ver < QM_HW_V3)
1136 		return;
1137 
1138 	switch (PAGE_SIZE) {
1139 	case SZ_4K:
1140 		page_type = 0x0;
1141 		break;
1142 	case SZ_16K:
1143 		page_type = 0x1;
1144 		break;
1145 	case SZ_64K:
1146 		page_type = 0x2;
1147 		break;
1148 	default:
1149 		dev_err(dev, "system page size is not support: %lu, default set to 4KB",
1150 			PAGE_SIZE);
1151 	}
1152 
1153 	writel(page_type, qm->io_base + QM_PAGE_SIZE);
1154 }
1155 
1156 /*
1157  * acc_shaper_para_calc() Get the IR value by the qos formula, the return value
1158  * is the expected qos calculated.
1159  * the formula:
1160  * IR = X Mbps if ir = 1 means IR = 100 Mbps, if ir = 10000 means = 10Gbps
1161  *
1162  *		IR_b * (2 ^ IR_u) * 8000
1163  * IR(Mbps) = -------------------------
1164  *		  Tick * (2 ^ IR_s)
1165  */
1166 static u32 acc_shaper_para_calc(u64 cir_b, u64 cir_u, u64 cir_s)
1167 {
1168 	return ((cir_b * QM_QOS_DIVISOR_CLK) * (1 << cir_u)) /
1169 					(QM_QOS_TICK * (1 << cir_s));
1170 }
1171 
1172 static u32 acc_shaper_calc_cbs_s(u32 ir)
1173 {
1174 	int table_size = ARRAY_SIZE(shaper_cbs_s);
1175 	int i;
1176 
1177 	for (i = 0; i < table_size; i++) {
1178 		if (ir >= shaper_cbs_s[i].start && ir <= shaper_cbs_s[i].end)
1179 			return shaper_cbs_s[i].val;
1180 	}
1181 
1182 	return QM_SHAPER_MIN_CBS_S;
1183 }
1184 
1185 static u32 acc_shaper_calc_cir_s(u32 ir)
1186 {
1187 	int table_size = ARRAY_SIZE(shaper_cir_s);
1188 	int i;
1189 
1190 	for (i = 0; i < table_size; i++) {
1191 		if (ir >= shaper_cir_s[i].start && ir <= shaper_cir_s[i].end)
1192 			return shaper_cir_s[i].val;
1193 	}
1194 
1195 	return 0;
1196 }
1197 
1198 static int qm_get_shaper_para(u32 ir, struct qm_shaper_factor *factor)
1199 {
1200 	u32 cir_b, cir_u, cir_s, ir_calc;
1201 	u32 error_rate;
1202 
1203 	factor->cbs_s = acc_shaper_calc_cbs_s(ir);
1204 	cir_s = acc_shaper_calc_cir_s(ir);
1205 
1206 	for (cir_b = QM_QOS_MIN_CIR_B; cir_b <= QM_QOS_MAX_CIR_B; cir_b++) {
1207 		for (cir_u = 0; cir_u <= QM_QOS_MAX_CIR_U; cir_u++) {
1208 			ir_calc = acc_shaper_para_calc(cir_b, cir_u, cir_s);
1209 
1210 			error_rate = QM_QOS_EXPAND_RATE * (u32)abs(ir_calc - ir) / ir;
1211 			if (error_rate <= QM_QOS_MIN_ERROR_RATE) {
1212 				factor->cir_b = cir_b;
1213 				factor->cir_u = cir_u;
1214 				factor->cir_s = cir_s;
1215 				return 0;
1216 			}
1217 		}
1218 	}
1219 
1220 	return -EINVAL;
1221 }
1222 
1223 static void qm_vft_data_cfg(struct hisi_qm *qm, enum vft_type type, u32 base,
1224 			    u32 number, struct qm_shaper_factor *factor)
1225 {
1226 	u64 tmp = 0;
1227 
1228 	if (number > 0) {
1229 		switch (type) {
1230 		case SQC_VFT:
1231 			if (qm->ver == QM_HW_V1) {
1232 				tmp = QM_SQC_VFT_BUF_SIZE	|
1233 				      QM_SQC_VFT_SQC_SIZE	|
1234 				      QM_SQC_VFT_INDEX_NUMBER	|
1235 				      QM_SQC_VFT_VALID		|
1236 				      (u64)base << QM_SQC_VFT_START_SQN_SHIFT;
1237 			} else {
1238 				tmp = (u64)base << QM_SQC_VFT_START_SQN_SHIFT |
1239 				      QM_SQC_VFT_VALID |
1240 				      (u64)(number - 1) << QM_SQC_VFT_SQN_SHIFT;
1241 			}
1242 			break;
1243 		case CQC_VFT:
1244 			if (qm->ver == QM_HW_V1) {
1245 				tmp = QM_CQC_VFT_BUF_SIZE	|
1246 				      QM_CQC_VFT_SQC_SIZE	|
1247 				      QM_CQC_VFT_INDEX_NUMBER	|
1248 				      QM_CQC_VFT_VALID;
1249 			} else {
1250 				tmp = QM_CQC_VFT_VALID;
1251 			}
1252 			break;
1253 		case SHAPER_VFT:
1254 			if (qm->ver >= QM_HW_V3) {
1255 				tmp = factor->cir_b |
1256 				(factor->cir_u << QM_SHAPER_FACTOR_CIR_U_SHIFT) |
1257 				(factor->cir_s << QM_SHAPER_FACTOR_CIR_S_SHIFT) |
1258 				(QM_SHAPER_CBS_B << QM_SHAPER_FACTOR_CBS_B_SHIFT) |
1259 				(factor->cbs_s << QM_SHAPER_FACTOR_CBS_S_SHIFT);
1260 			}
1261 			break;
1262 		}
1263 	}
1264 
1265 	writel(lower_32_bits(tmp), qm->io_base + QM_VFT_CFG_DATA_L);
1266 	writel(upper_32_bits(tmp), qm->io_base + QM_VFT_CFG_DATA_H);
1267 }
1268 
1269 static int qm_set_vft_common(struct hisi_qm *qm, enum vft_type type,
1270 			     u32 fun_num, u32 base, u32 number)
1271 {
1272 	struct qm_shaper_factor *factor = &qm->factor[fun_num];
1273 	unsigned int val;
1274 	int ret;
1275 
1276 	ret = readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val,
1277 					 val & BIT(0), POLL_PERIOD,
1278 					 POLL_TIMEOUT);
1279 	if (ret)
1280 		return ret;
1281 
1282 	writel(0x0, qm->io_base + QM_VFT_CFG_OP_WR);
1283 	writel(type, qm->io_base + QM_VFT_CFG_TYPE);
1284 	if (type == SHAPER_VFT)
1285 		fun_num |= base << QM_SHAPER_VFT_OFFSET;
1286 
1287 	writel(fun_num, qm->io_base + QM_VFT_CFG);
1288 
1289 	qm_vft_data_cfg(qm, type, base, number, factor);
1290 
1291 	writel(0x0, qm->io_base + QM_VFT_CFG_RDY);
1292 	writel(0x1, qm->io_base + QM_VFT_CFG_OP_ENABLE);
1293 
1294 	return readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val,
1295 					  val & BIT(0), POLL_PERIOD,
1296 					  POLL_TIMEOUT);
1297 }
1298 
1299 static int qm_shaper_init_vft(struct hisi_qm *qm, u32 fun_num)
1300 {
1301 	u32 qos = qm->factor[fun_num].func_qos;
1302 	int ret, i;
1303 
1304 	ret = qm_get_shaper_para(qos * QM_QOS_RATE, &qm->factor[fun_num]);
1305 	if (ret) {
1306 		dev_err(&qm->pdev->dev, "failed to calculate shaper parameter!\n");
1307 		return ret;
1308 	}
1309 	writel(qm->type_rate, qm->io_base + QM_SHAPER_CFG);
1310 	for (i = ALG_TYPE_0; i <= ALG_TYPE_1; i++) {
1311 		/* The base number of queue reuse for different alg type */
1312 		ret = qm_set_vft_common(qm, SHAPER_VFT, fun_num, i, 1);
1313 		if (ret)
1314 			return ret;
1315 	}
1316 
1317 	return 0;
1318 }
1319 
1320 /* The config should be conducted after qm_dev_mem_reset() */
1321 static int qm_set_sqc_cqc_vft(struct hisi_qm *qm, u32 fun_num, u32 base,
1322 			      u32 number)
1323 {
1324 	int ret, i;
1325 
1326 	for (i = SQC_VFT; i <= CQC_VFT; i++) {
1327 		ret = qm_set_vft_common(qm, i, fun_num, base, number);
1328 		if (ret)
1329 			return ret;
1330 	}
1331 
1332 	/* init default shaper qos val */
1333 	if (qm->ver >= QM_HW_V3) {
1334 		ret = qm_shaper_init_vft(qm, fun_num);
1335 		if (ret)
1336 			goto back_sqc_cqc;
1337 	}
1338 
1339 	return 0;
1340 back_sqc_cqc:
1341 	for (i = SQC_VFT; i <= CQC_VFT; i++) {
1342 		ret = qm_set_vft_common(qm, i, fun_num, 0, 0);
1343 		if (ret)
1344 			return ret;
1345 	}
1346 	return ret;
1347 }
1348 
1349 static int qm_get_vft_v2(struct hisi_qm *qm, u32 *base, u32 *number)
1350 {
1351 	u64 sqc_vft;
1352 	int ret;
1353 
1354 	ret = qm_mb(qm, QM_MB_CMD_SQC_VFT_V2, 0, 0, 1);
1355 	if (ret)
1356 		return ret;
1357 
1358 	sqc_vft = readl(qm->io_base + QM_MB_CMD_DATA_ADDR_L) |
1359 		  ((u64)readl(qm->io_base + QM_MB_CMD_DATA_ADDR_H) << 32);
1360 	*base = QM_SQC_VFT_BASE_MASK_V2 & (sqc_vft >> QM_SQC_VFT_BASE_SHIFT_V2);
1361 	*number = (QM_SQC_VFT_NUM_MASK_v2 &
1362 		   (sqc_vft >> QM_SQC_VFT_NUM_SHIFT_V2)) + 1;
1363 
1364 	return 0;
1365 }
1366 
1367 static int qm_get_vf_qp_num(struct hisi_qm *qm, u32 fun_num)
1368 {
1369 	u32 remain_q_num, vfq_num;
1370 	u32 num_vfs = qm->vfs_num;
1371 
1372 	vfq_num = (qm->ctrl_qp_num - qm->qp_num) / num_vfs;
1373 	if (vfq_num >= qm->max_qp_num)
1374 		return qm->max_qp_num;
1375 
1376 	remain_q_num = (qm->ctrl_qp_num - qm->qp_num) % num_vfs;
1377 	if (vfq_num + remain_q_num <= qm->max_qp_num)
1378 		return fun_num == num_vfs ? vfq_num + remain_q_num : vfq_num;
1379 
1380 	/*
1381 	 * if vfq_num + remain_q_num > max_qp_num, the last VFs,
1382 	 * each with one more queue.
1383 	 */
1384 	return fun_num + remain_q_num > num_vfs ? vfq_num + 1 : vfq_num;
1385 }
1386 
1387 static struct hisi_qm *file_to_qm(struct debugfs_file *file)
1388 {
1389 	struct qm_debug *debug = file->debug;
1390 
1391 	return container_of(debug, struct hisi_qm, debug);
1392 }
1393 
1394 static u32 current_q_read(struct hisi_qm *qm)
1395 {
1396 	return readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) >> QM_DFX_QN_SHIFT;
1397 }
1398 
1399 static int current_q_write(struct hisi_qm *qm, u32 val)
1400 {
1401 	u32 tmp;
1402 
1403 	if (val >= qm->debug.curr_qm_qp_num)
1404 		return -EINVAL;
1405 
1406 	tmp = val << QM_DFX_QN_SHIFT |
1407 	      (readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) & CURRENT_FUN_MASK);
1408 	writel(tmp, qm->io_base + QM_DFX_SQE_CNT_VF_SQN);
1409 
1410 	tmp = val << QM_DFX_QN_SHIFT |
1411 	      (readl(qm->io_base + QM_DFX_CQE_CNT_VF_CQN) & CURRENT_FUN_MASK);
1412 	writel(tmp, qm->io_base + QM_DFX_CQE_CNT_VF_CQN);
1413 
1414 	return 0;
1415 }
1416 
1417 static u32 clear_enable_read(struct hisi_qm *qm)
1418 {
1419 	return readl(qm->io_base + QM_DFX_CNT_CLR_CE);
1420 }
1421 
1422 /* rd_clr_ctrl 1 enable read clear, otherwise 0 disable it */
1423 static int clear_enable_write(struct hisi_qm *qm, u32 rd_clr_ctrl)
1424 {
1425 	if (rd_clr_ctrl > 1)
1426 		return -EINVAL;
1427 
1428 	writel(rd_clr_ctrl, qm->io_base + QM_DFX_CNT_CLR_CE);
1429 
1430 	return 0;
1431 }
1432 
1433 static u32 current_qm_read(struct hisi_qm *qm)
1434 {
1435 	return readl(qm->io_base + QM_DFX_MB_CNT_VF);
1436 }
1437 
1438 static int current_qm_write(struct hisi_qm *qm, u32 val)
1439 {
1440 	u32 tmp;
1441 
1442 	if (val > qm->vfs_num)
1443 		return -EINVAL;
1444 
1445 	/* According PF or VF Dev ID to calculation curr_qm_qp_num and store */
1446 	if (!val)
1447 		qm->debug.curr_qm_qp_num = qm->qp_num;
1448 	else
1449 		qm->debug.curr_qm_qp_num = qm_get_vf_qp_num(qm, val);
1450 
1451 	writel(val, qm->io_base + QM_DFX_MB_CNT_VF);
1452 	writel(val, qm->io_base + QM_DFX_DB_CNT_VF);
1453 
1454 	tmp = val |
1455 	      (readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) & CURRENT_Q_MASK);
1456 	writel(tmp, qm->io_base + QM_DFX_SQE_CNT_VF_SQN);
1457 
1458 	tmp = val |
1459 	      (readl(qm->io_base + QM_DFX_CQE_CNT_VF_CQN) & CURRENT_Q_MASK);
1460 	writel(tmp, qm->io_base + QM_DFX_CQE_CNT_VF_CQN);
1461 
1462 	return 0;
1463 }
1464 
1465 static ssize_t qm_debug_read(struct file *filp, char __user *buf,
1466 			     size_t count, loff_t *pos)
1467 {
1468 	struct debugfs_file *file = filp->private_data;
1469 	enum qm_debug_file index = file->index;
1470 	struct hisi_qm *qm = file_to_qm(file);
1471 	char tbuf[QM_DBG_TMP_BUF_LEN];
1472 	u32 val;
1473 	int ret;
1474 
1475 	ret = hisi_qm_get_dfx_access(qm);
1476 	if (ret)
1477 		return ret;
1478 
1479 	mutex_lock(&file->lock);
1480 	switch (index) {
1481 	case CURRENT_QM:
1482 		val = current_qm_read(qm);
1483 		break;
1484 	case CURRENT_Q:
1485 		val = current_q_read(qm);
1486 		break;
1487 	case CLEAR_ENABLE:
1488 		val = clear_enable_read(qm);
1489 		break;
1490 	default:
1491 		goto err_input;
1492 	}
1493 	mutex_unlock(&file->lock);
1494 
1495 	hisi_qm_put_dfx_access(qm);
1496 	ret = scnprintf(tbuf, QM_DBG_TMP_BUF_LEN, "%u\n", val);
1497 	return simple_read_from_buffer(buf, count, pos, tbuf, ret);
1498 
1499 err_input:
1500 	mutex_unlock(&file->lock);
1501 	hisi_qm_put_dfx_access(qm);
1502 	return -EINVAL;
1503 }
1504 
1505 static ssize_t qm_debug_write(struct file *filp, const char __user *buf,
1506 			      size_t count, loff_t *pos)
1507 {
1508 	struct debugfs_file *file = filp->private_data;
1509 	enum qm_debug_file index = file->index;
1510 	struct hisi_qm *qm = file_to_qm(file);
1511 	unsigned long val;
1512 	char tbuf[QM_DBG_TMP_BUF_LEN];
1513 	int len, ret;
1514 
1515 	if (*pos != 0)
1516 		return 0;
1517 
1518 	if (count >= QM_DBG_TMP_BUF_LEN)
1519 		return -ENOSPC;
1520 
1521 	len = simple_write_to_buffer(tbuf, QM_DBG_TMP_BUF_LEN - 1, pos, buf,
1522 				     count);
1523 	if (len < 0)
1524 		return len;
1525 
1526 	tbuf[len] = '\0';
1527 	if (kstrtoul(tbuf, 0, &val))
1528 		return -EFAULT;
1529 
1530 	ret = hisi_qm_get_dfx_access(qm);
1531 	if (ret)
1532 		return ret;
1533 
1534 	mutex_lock(&file->lock);
1535 	switch (index) {
1536 	case CURRENT_QM:
1537 		ret = current_qm_write(qm, val);
1538 		break;
1539 	case CURRENT_Q:
1540 		ret = current_q_write(qm, val);
1541 		break;
1542 	case CLEAR_ENABLE:
1543 		ret = clear_enable_write(qm, val);
1544 		break;
1545 	default:
1546 		ret = -EINVAL;
1547 	}
1548 	mutex_unlock(&file->lock);
1549 
1550 	hisi_qm_put_dfx_access(qm);
1551 
1552 	if (ret)
1553 		return ret;
1554 
1555 	return count;
1556 }
1557 
1558 static const struct file_operations qm_debug_fops = {
1559 	.owner = THIS_MODULE,
1560 	.open = simple_open,
1561 	.read = qm_debug_read,
1562 	.write = qm_debug_write,
1563 };
1564 
1565 #define CNT_CYC_REGS_NUM		10
1566 static const struct debugfs_reg32 qm_dfx_regs[] = {
1567 	/* XXX_CNT are reading clear register */
1568 	{"QM_ECC_1BIT_CNT               ",  0x104000ull},
1569 	{"QM_ECC_MBIT_CNT               ",  0x104008ull},
1570 	{"QM_DFX_MB_CNT                 ",  0x104018ull},
1571 	{"QM_DFX_DB_CNT                 ",  0x104028ull},
1572 	{"QM_DFX_SQE_CNT                ",  0x104038ull},
1573 	{"QM_DFX_CQE_CNT                ",  0x104048ull},
1574 	{"QM_DFX_SEND_SQE_TO_ACC_CNT    ",  0x104050ull},
1575 	{"QM_DFX_WB_SQE_FROM_ACC_CNT    ",  0x104058ull},
1576 	{"QM_DFX_ACC_FINISH_CNT         ",  0x104060ull},
1577 	{"QM_DFX_CQE_ERR_CNT            ",  0x1040b4ull},
1578 	{"QM_DFX_FUNS_ACTIVE_ST         ",  0x200ull},
1579 	{"QM_ECC_1BIT_INF               ",  0x104004ull},
1580 	{"QM_ECC_MBIT_INF               ",  0x10400cull},
1581 	{"QM_DFX_ACC_RDY_VLD0           ",  0x1040a0ull},
1582 	{"QM_DFX_ACC_RDY_VLD1           ",  0x1040a4ull},
1583 	{"QM_DFX_AXI_RDY_VLD            ",  0x1040a8ull},
1584 	{"QM_DFX_FF_ST0                 ",  0x1040c8ull},
1585 	{"QM_DFX_FF_ST1                 ",  0x1040ccull},
1586 	{"QM_DFX_FF_ST2                 ",  0x1040d0ull},
1587 	{"QM_DFX_FF_ST3                 ",  0x1040d4ull},
1588 	{"QM_DFX_FF_ST4                 ",  0x1040d8ull},
1589 	{"QM_DFX_FF_ST5                 ",  0x1040dcull},
1590 	{"QM_DFX_FF_ST6                 ",  0x1040e0ull},
1591 	{"QM_IN_IDLE_ST                 ",  0x1040e4ull},
1592 };
1593 
1594 static const struct debugfs_reg32 qm_vf_dfx_regs[] = {
1595 	{"QM_DFX_FUNS_ACTIVE_ST         ",  0x200ull},
1596 };
1597 
1598 /**
1599  * hisi_qm_regs_dump() - Dump registers's value.
1600  * @s: debugfs file handle.
1601  * @regset: accelerator registers information.
1602  *
1603  * Dump accelerator registers.
1604  */
1605 void hisi_qm_regs_dump(struct seq_file *s, struct debugfs_regset32 *regset)
1606 {
1607 	struct pci_dev *pdev = to_pci_dev(regset->dev);
1608 	struct hisi_qm *qm = pci_get_drvdata(pdev);
1609 	const struct debugfs_reg32 *regs = regset->regs;
1610 	int regs_len = regset->nregs;
1611 	int i, ret;
1612 	u32 val;
1613 
1614 	ret = hisi_qm_get_dfx_access(qm);
1615 	if (ret)
1616 		return;
1617 
1618 	for (i = 0; i < regs_len; i++) {
1619 		val = readl(regset->base + regs[i].offset);
1620 		seq_printf(s, "%s= 0x%08x\n", regs[i].name, val);
1621 	}
1622 
1623 	hisi_qm_put_dfx_access(qm);
1624 }
1625 EXPORT_SYMBOL_GPL(hisi_qm_regs_dump);
1626 
1627 static int qm_regs_show(struct seq_file *s, void *unused)
1628 {
1629 	struct hisi_qm *qm = s->private;
1630 	struct debugfs_regset32 regset;
1631 
1632 	if (qm->fun_type == QM_HW_PF) {
1633 		regset.regs = qm_dfx_regs;
1634 		regset.nregs = ARRAY_SIZE(qm_dfx_regs);
1635 	} else {
1636 		regset.regs = qm_vf_dfx_regs;
1637 		regset.nregs = ARRAY_SIZE(qm_vf_dfx_regs);
1638 	}
1639 
1640 	regset.base = qm->io_base;
1641 	regset.dev = &qm->pdev->dev;
1642 
1643 	hisi_qm_regs_dump(s, &regset);
1644 
1645 	return 0;
1646 }
1647 
1648 DEFINE_SHOW_ATTRIBUTE(qm_regs);
1649 
1650 static ssize_t qm_cmd_read(struct file *filp, char __user *buffer,
1651 			   size_t count, loff_t *pos)
1652 {
1653 	char buf[QM_DBG_READ_LEN];
1654 	int len;
1655 
1656 	len = scnprintf(buf, QM_DBG_READ_LEN, "%s\n",
1657 			"Please echo help to cmd to get help information");
1658 
1659 	return simple_read_from_buffer(buffer, count, pos, buf, len);
1660 }
1661 
1662 static void *qm_ctx_alloc(struct hisi_qm *qm, size_t ctx_size,
1663 			  dma_addr_t *dma_addr)
1664 {
1665 	struct device *dev = &qm->pdev->dev;
1666 	void *ctx_addr;
1667 
1668 	ctx_addr = kzalloc(ctx_size, GFP_KERNEL);
1669 	if (!ctx_addr)
1670 		return ERR_PTR(-ENOMEM);
1671 
1672 	*dma_addr = dma_map_single(dev, ctx_addr, ctx_size, DMA_FROM_DEVICE);
1673 	if (dma_mapping_error(dev, *dma_addr)) {
1674 		dev_err(dev, "DMA mapping error!\n");
1675 		kfree(ctx_addr);
1676 		return ERR_PTR(-ENOMEM);
1677 	}
1678 
1679 	return ctx_addr;
1680 }
1681 
1682 static void qm_ctx_free(struct hisi_qm *qm, size_t ctx_size,
1683 			const void *ctx_addr, dma_addr_t *dma_addr)
1684 {
1685 	struct device *dev = &qm->pdev->dev;
1686 
1687 	dma_unmap_single(dev, *dma_addr, ctx_size, DMA_FROM_DEVICE);
1688 	kfree(ctx_addr);
1689 }
1690 
1691 static int dump_show(struct hisi_qm *qm, void *info,
1692 		     unsigned int info_size, char *info_name)
1693 {
1694 	struct device *dev = &qm->pdev->dev;
1695 	u8 *info_buf, *info_curr = info;
1696 	u32 i;
1697 #define BYTE_PER_DW	4
1698 
1699 	info_buf = kzalloc(info_size, GFP_KERNEL);
1700 	if (!info_buf)
1701 		return -ENOMEM;
1702 
1703 	for (i = 0; i < info_size; i++, info_curr++) {
1704 		if (i % BYTE_PER_DW == 0)
1705 			info_buf[i + 3UL] = *info_curr;
1706 		else if (i % BYTE_PER_DW == 1)
1707 			info_buf[i + 1UL] = *info_curr;
1708 		else if (i % BYTE_PER_DW == 2)
1709 			info_buf[i - 1] = *info_curr;
1710 		else if (i % BYTE_PER_DW == 3)
1711 			info_buf[i - 3] = *info_curr;
1712 	}
1713 
1714 	dev_info(dev, "%s DUMP\n", info_name);
1715 	for (i = 0; i < info_size; i += BYTE_PER_DW) {
1716 		pr_info("DW%u: %02X%02X %02X%02X\n", i / BYTE_PER_DW,
1717 			info_buf[i], info_buf[i + 1UL],
1718 			info_buf[i + 2UL], info_buf[i + 3UL]);
1719 	}
1720 
1721 	kfree(info_buf);
1722 
1723 	return 0;
1724 }
1725 
1726 static int qm_dump_sqc_raw(struct hisi_qm *qm, dma_addr_t dma_addr, u16 qp_id)
1727 {
1728 	return qm_mb(qm, QM_MB_CMD_SQC, dma_addr, qp_id, 1);
1729 }
1730 
1731 static int qm_dump_cqc_raw(struct hisi_qm *qm, dma_addr_t dma_addr, u16 qp_id)
1732 {
1733 	return qm_mb(qm, QM_MB_CMD_CQC, dma_addr, qp_id, 1);
1734 }
1735 
1736 static int qm_sqc_dump(struct hisi_qm *qm, const char *s)
1737 {
1738 	struct device *dev = &qm->pdev->dev;
1739 	struct qm_sqc *sqc, *sqc_curr;
1740 	dma_addr_t sqc_dma;
1741 	u32 qp_id;
1742 	int ret;
1743 
1744 	if (!s)
1745 		return -EINVAL;
1746 
1747 	ret = kstrtou32(s, 0, &qp_id);
1748 	if (ret || qp_id >= qm->qp_num) {
1749 		dev_err(dev, "Please input qp num (0-%u)", qm->qp_num - 1);
1750 		return -EINVAL;
1751 	}
1752 
1753 	sqc = qm_ctx_alloc(qm, sizeof(*sqc), &sqc_dma);
1754 	if (IS_ERR(sqc))
1755 		return PTR_ERR(sqc);
1756 
1757 	ret = qm_dump_sqc_raw(qm, sqc_dma, qp_id);
1758 	if (ret) {
1759 		down_read(&qm->qps_lock);
1760 		if (qm->sqc) {
1761 			sqc_curr = qm->sqc + qp_id;
1762 
1763 			ret = dump_show(qm, sqc_curr, sizeof(*sqc),
1764 					"SOFT SQC");
1765 			if (ret)
1766 				dev_info(dev, "Show soft sqc failed!\n");
1767 		}
1768 		up_read(&qm->qps_lock);
1769 
1770 		goto err_free_ctx;
1771 	}
1772 
1773 	ret = dump_show(qm, sqc, sizeof(*sqc), "SQC");
1774 	if (ret)
1775 		dev_info(dev, "Show hw sqc failed!\n");
1776 
1777 err_free_ctx:
1778 	qm_ctx_free(qm, sizeof(*sqc), sqc, &sqc_dma);
1779 	return ret;
1780 }
1781 
1782 static int qm_cqc_dump(struct hisi_qm *qm, const char *s)
1783 {
1784 	struct device *dev = &qm->pdev->dev;
1785 	struct qm_cqc *cqc, *cqc_curr;
1786 	dma_addr_t cqc_dma;
1787 	u32 qp_id;
1788 	int ret;
1789 
1790 	if (!s)
1791 		return -EINVAL;
1792 
1793 	ret = kstrtou32(s, 0, &qp_id);
1794 	if (ret || qp_id >= qm->qp_num) {
1795 		dev_err(dev, "Please input qp num (0-%u)", qm->qp_num - 1);
1796 		return -EINVAL;
1797 	}
1798 
1799 	cqc = qm_ctx_alloc(qm, sizeof(*cqc), &cqc_dma);
1800 	if (IS_ERR(cqc))
1801 		return PTR_ERR(cqc);
1802 
1803 	ret = qm_dump_cqc_raw(qm, cqc_dma, qp_id);
1804 	if (ret) {
1805 		down_read(&qm->qps_lock);
1806 		if (qm->cqc) {
1807 			cqc_curr = qm->cqc + qp_id;
1808 
1809 			ret = dump_show(qm, cqc_curr, sizeof(*cqc),
1810 					"SOFT CQC");
1811 			if (ret)
1812 				dev_info(dev, "Show soft cqc failed!\n");
1813 		}
1814 		up_read(&qm->qps_lock);
1815 
1816 		goto err_free_ctx;
1817 	}
1818 
1819 	ret = dump_show(qm, cqc, sizeof(*cqc), "CQC");
1820 	if (ret)
1821 		dev_info(dev, "Show hw cqc failed!\n");
1822 
1823 err_free_ctx:
1824 	qm_ctx_free(qm, sizeof(*cqc), cqc, &cqc_dma);
1825 	return ret;
1826 }
1827 
1828 static int qm_eqc_aeqc_dump(struct hisi_qm *qm, char *s, size_t size,
1829 			    int cmd, char *name)
1830 {
1831 	struct device *dev = &qm->pdev->dev;
1832 	dma_addr_t xeqc_dma;
1833 	void *xeqc;
1834 	int ret;
1835 
1836 	if (strsep(&s, " ")) {
1837 		dev_err(dev, "Please do not input extra characters!\n");
1838 		return -EINVAL;
1839 	}
1840 
1841 	xeqc = qm_ctx_alloc(qm, size, &xeqc_dma);
1842 	if (IS_ERR(xeqc))
1843 		return PTR_ERR(xeqc);
1844 
1845 	ret = qm_mb(qm, cmd, xeqc_dma, 0, 1);
1846 	if (ret)
1847 		goto err_free_ctx;
1848 
1849 	ret = dump_show(qm, xeqc, size, name);
1850 	if (ret)
1851 		dev_info(dev, "Show hw %s failed!\n", name);
1852 
1853 err_free_ctx:
1854 	qm_ctx_free(qm, size, xeqc, &xeqc_dma);
1855 	return ret;
1856 }
1857 
1858 static int q_dump_param_parse(struct hisi_qm *qm, char *s,
1859 			      u32 *e_id, u32 *q_id)
1860 {
1861 	struct device *dev = &qm->pdev->dev;
1862 	unsigned int qp_num = qm->qp_num;
1863 	char *presult;
1864 	int ret;
1865 
1866 	presult = strsep(&s, " ");
1867 	if (!presult) {
1868 		dev_err(dev, "Please input qp number!\n");
1869 		return -EINVAL;
1870 	}
1871 
1872 	ret = kstrtou32(presult, 0, q_id);
1873 	if (ret || *q_id >= qp_num) {
1874 		dev_err(dev, "Please input qp num (0-%u)", qp_num - 1);
1875 		return -EINVAL;
1876 	}
1877 
1878 	presult = strsep(&s, " ");
1879 	if (!presult) {
1880 		dev_err(dev, "Please input sqe number!\n");
1881 		return -EINVAL;
1882 	}
1883 
1884 	ret = kstrtou32(presult, 0, e_id);
1885 	if (ret || *e_id >= QM_Q_DEPTH) {
1886 		dev_err(dev, "Please input sqe num (0-%d)", QM_Q_DEPTH - 1);
1887 		return -EINVAL;
1888 	}
1889 
1890 	if (strsep(&s, " ")) {
1891 		dev_err(dev, "Please do not input extra characters!\n");
1892 		return -EINVAL;
1893 	}
1894 
1895 	return 0;
1896 }
1897 
1898 static int qm_sq_dump(struct hisi_qm *qm, char *s)
1899 {
1900 	struct device *dev = &qm->pdev->dev;
1901 	void *sqe, *sqe_curr;
1902 	struct hisi_qp *qp;
1903 	u32 qp_id, sqe_id;
1904 	int ret;
1905 
1906 	ret = q_dump_param_parse(qm, s, &sqe_id, &qp_id);
1907 	if (ret)
1908 		return ret;
1909 
1910 	sqe = kzalloc(qm->sqe_size * QM_Q_DEPTH, GFP_KERNEL);
1911 	if (!sqe)
1912 		return -ENOMEM;
1913 
1914 	qp = &qm->qp_array[qp_id];
1915 	memcpy(sqe, qp->sqe, qm->sqe_size * QM_Q_DEPTH);
1916 	sqe_curr = sqe + (u32)(sqe_id * qm->sqe_size);
1917 	memset(sqe_curr + qm->debug.sqe_mask_offset, QM_SQE_ADDR_MASK,
1918 	       qm->debug.sqe_mask_len);
1919 
1920 	ret = dump_show(qm, sqe_curr, qm->sqe_size, "SQE");
1921 	if (ret)
1922 		dev_info(dev, "Show sqe failed!\n");
1923 
1924 	kfree(sqe);
1925 
1926 	return ret;
1927 }
1928 
1929 static int qm_cq_dump(struct hisi_qm *qm, char *s)
1930 {
1931 	struct device *dev = &qm->pdev->dev;
1932 	struct qm_cqe *cqe_curr;
1933 	struct hisi_qp *qp;
1934 	u32 qp_id, cqe_id;
1935 	int ret;
1936 
1937 	ret = q_dump_param_parse(qm, s, &cqe_id, &qp_id);
1938 	if (ret)
1939 		return ret;
1940 
1941 	qp = &qm->qp_array[qp_id];
1942 	cqe_curr = qp->cqe + cqe_id;
1943 	ret = dump_show(qm, cqe_curr, sizeof(struct qm_cqe), "CQE");
1944 	if (ret)
1945 		dev_info(dev, "Show cqe failed!\n");
1946 
1947 	return ret;
1948 }
1949 
1950 static int qm_eq_aeq_dump(struct hisi_qm *qm, const char *s,
1951 			  size_t size, char *name)
1952 {
1953 	struct device *dev = &qm->pdev->dev;
1954 	void *xeqe;
1955 	u32 xeqe_id;
1956 	int ret;
1957 
1958 	if (!s)
1959 		return -EINVAL;
1960 
1961 	ret = kstrtou32(s, 0, &xeqe_id);
1962 	if (ret)
1963 		return -EINVAL;
1964 
1965 	if (!strcmp(name, "EQE") && xeqe_id >= QM_EQ_DEPTH) {
1966 		dev_err(dev, "Please input eqe num (0-%d)", QM_EQ_DEPTH - 1);
1967 		return -EINVAL;
1968 	} else if (!strcmp(name, "AEQE") && xeqe_id >= QM_Q_DEPTH) {
1969 		dev_err(dev, "Please input aeqe num (0-%d)", QM_Q_DEPTH - 1);
1970 		return -EINVAL;
1971 	}
1972 
1973 	down_read(&qm->qps_lock);
1974 
1975 	if (qm->eqe && !strcmp(name, "EQE")) {
1976 		xeqe = qm->eqe + xeqe_id;
1977 	} else if (qm->aeqe && !strcmp(name, "AEQE")) {
1978 		xeqe = qm->aeqe + xeqe_id;
1979 	} else {
1980 		ret = -EINVAL;
1981 		goto err_unlock;
1982 	}
1983 
1984 	ret = dump_show(qm, xeqe, size, name);
1985 	if (ret)
1986 		dev_info(dev, "Show %s failed!\n", name);
1987 
1988 err_unlock:
1989 	up_read(&qm->qps_lock);
1990 	return ret;
1991 }
1992 
1993 static int qm_dbg_help(struct hisi_qm *qm, char *s)
1994 {
1995 	struct device *dev = &qm->pdev->dev;
1996 
1997 	if (strsep(&s, " ")) {
1998 		dev_err(dev, "Please do not input extra characters!\n");
1999 		return -EINVAL;
2000 	}
2001 
2002 	dev_info(dev, "available commands:\n");
2003 	dev_info(dev, "sqc <num>\n");
2004 	dev_info(dev, "cqc <num>\n");
2005 	dev_info(dev, "eqc\n");
2006 	dev_info(dev, "aeqc\n");
2007 	dev_info(dev, "sq <num> <e>\n");
2008 	dev_info(dev, "cq <num> <e>\n");
2009 	dev_info(dev, "eq <e>\n");
2010 	dev_info(dev, "aeq <e>\n");
2011 
2012 	return 0;
2013 }
2014 
2015 static int qm_cmd_write_dump(struct hisi_qm *qm, const char *cmd_buf)
2016 {
2017 	struct device *dev = &qm->pdev->dev;
2018 	char *presult, *s, *s_tmp;
2019 	int ret;
2020 
2021 	s = kstrdup(cmd_buf, GFP_KERNEL);
2022 	if (!s)
2023 		return -ENOMEM;
2024 
2025 	s_tmp = s;
2026 	presult = strsep(&s, " ");
2027 	if (!presult) {
2028 		ret = -EINVAL;
2029 		goto err_buffer_free;
2030 	}
2031 
2032 	if (!strcmp(presult, "sqc"))
2033 		ret = qm_sqc_dump(qm, s);
2034 	else if (!strcmp(presult, "cqc"))
2035 		ret = qm_cqc_dump(qm, s);
2036 	else if (!strcmp(presult, "eqc"))
2037 		ret = qm_eqc_aeqc_dump(qm, s, sizeof(struct qm_eqc),
2038 				       QM_MB_CMD_EQC, "EQC");
2039 	else if (!strcmp(presult, "aeqc"))
2040 		ret = qm_eqc_aeqc_dump(qm, s, sizeof(struct qm_aeqc),
2041 				       QM_MB_CMD_AEQC, "AEQC");
2042 	else if (!strcmp(presult, "sq"))
2043 		ret = qm_sq_dump(qm, s);
2044 	else if (!strcmp(presult, "cq"))
2045 		ret = qm_cq_dump(qm, s);
2046 	else if (!strcmp(presult, "eq"))
2047 		ret = qm_eq_aeq_dump(qm, s, sizeof(struct qm_eqe), "EQE");
2048 	else if (!strcmp(presult, "aeq"))
2049 		ret = qm_eq_aeq_dump(qm, s, sizeof(struct qm_aeqe), "AEQE");
2050 	else if (!strcmp(presult, "help"))
2051 		ret = qm_dbg_help(qm, s);
2052 	else
2053 		ret = -EINVAL;
2054 
2055 	if (ret)
2056 		dev_info(dev, "Please echo help\n");
2057 
2058 err_buffer_free:
2059 	kfree(s_tmp);
2060 
2061 	return ret;
2062 }
2063 
2064 static ssize_t qm_cmd_write(struct file *filp, const char __user *buffer,
2065 			    size_t count, loff_t *pos)
2066 {
2067 	struct hisi_qm *qm = filp->private_data;
2068 	char *cmd_buf, *cmd_buf_tmp;
2069 	int ret;
2070 
2071 	if (*pos)
2072 		return 0;
2073 
2074 	ret = hisi_qm_get_dfx_access(qm);
2075 	if (ret)
2076 		return ret;
2077 
2078 	/* Judge if the instance is being reset. */
2079 	if (unlikely(atomic_read(&qm->status.flags) == QM_STOP))
2080 		return 0;
2081 
2082 	if (count > QM_DBG_WRITE_LEN) {
2083 		ret = -ENOSPC;
2084 		goto put_dfx_access;
2085 	}
2086 
2087 	cmd_buf = memdup_user_nul(buffer, count);
2088 	if (IS_ERR(cmd_buf)) {
2089 		ret = PTR_ERR(cmd_buf);
2090 		goto put_dfx_access;
2091 	}
2092 
2093 	cmd_buf_tmp = strchr(cmd_buf, '\n');
2094 	if (cmd_buf_tmp) {
2095 		*cmd_buf_tmp = '\0';
2096 		count = cmd_buf_tmp - cmd_buf + 1;
2097 	}
2098 
2099 	ret = qm_cmd_write_dump(qm, cmd_buf);
2100 	if (ret) {
2101 		kfree(cmd_buf);
2102 		goto put_dfx_access;
2103 	}
2104 
2105 	kfree(cmd_buf);
2106 
2107 	ret = count;
2108 
2109 put_dfx_access:
2110 	hisi_qm_put_dfx_access(qm);
2111 	return ret;
2112 }
2113 
2114 static const struct file_operations qm_cmd_fops = {
2115 	.owner = THIS_MODULE,
2116 	.open = simple_open,
2117 	.read = qm_cmd_read,
2118 	.write = qm_cmd_write,
2119 };
2120 
2121 static void qm_create_debugfs_file(struct hisi_qm *qm, struct dentry *dir,
2122 				   enum qm_debug_file index)
2123 {
2124 	struct debugfs_file *file = qm->debug.files + index;
2125 
2126 	debugfs_create_file(qm_debug_file_name[index], 0600, dir, file,
2127 			    &qm_debug_fops);
2128 
2129 	file->index = index;
2130 	mutex_init(&file->lock);
2131 	file->debug = &qm->debug;
2132 }
2133 
2134 static void qm_hw_error_init_v1(struct hisi_qm *qm, u32 ce, u32 nfe, u32 fe)
2135 {
2136 	writel(QM_ABNORMAL_INT_MASK_VALUE, qm->io_base + QM_ABNORMAL_INT_MASK);
2137 }
2138 
2139 static void qm_hw_error_cfg(struct hisi_qm *qm, u32 ce, u32 nfe, u32 fe)
2140 {
2141 	qm->error_mask = ce | nfe | fe;
2142 	/* clear QM hw residual error source */
2143 	writel(QM_ABNORMAL_INT_SOURCE_CLR,
2144 	       qm->io_base + QM_ABNORMAL_INT_SOURCE);
2145 
2146 	/* configure error type */
2147 	writel(ce, qm->io_base + QM_RAS_CE_ENABLE);
2148 	writel(QM_RAS_CE_TIMES_PER_IRQ, qm->io_base + QM_RAS_CE_THRESHOLD);
2149 	writel(nfe, qm->io_base + QM_RAS_NFE_ENABLE);
2150 	writel(fe, qm->io_base + QM_RAS_FE_ENABLE);
2151 }
2152 
2153 static void qm_hw_error_init_v2(struct hisi_qm *qm, u32 ce, u32 nfe, u32 fe)
2154 {
2155 	u32 irq_enable = ce | nfe | fe;
2156 	u32 irq_unmask = ~irq_enable;
2157 
2158 	qm_hw_error_cfg(qm, ce, nfe, fe);
2159 
2160 	irq_unmask &= readl(qm->io_base + QM_ABNORMAL_INT_MASK);
2161 	writel(irq_unmask, qm->io_base + QM_ABNORMAL_INT_MASK);
2162 }
2163 
2164 static void qm_hw_error_uninit_v2(struct hisi_qm *qm)
2165 {
2166 	writel(QM_ABNORMAL_INT_MASK_VALUE, qm->io_base + QM_ABNORMAL_INT_MASK);
2167 }
2168 
2169 static void qm_hw_error_init_v3(struct hisi_qm *qm, u32 ce, u32 nfe, u32 fe)
2170 {
2171 	u32 irq_enable = ce | nfe | fe;
2172 	u32 irq_unmask = ~irq_enable;
2173 
2174 	qm_hw_error_cfg(qm, ce, nfe, fe);
2175 
2176 	/* enable close master ooo when hardware error happened */
2177 	writel(nfe & (~QM_DB_RANDOM_INVALID), qm->io_base + QM_OOO_SHUTDOWN_SEL);
2178 
2179 	irq_unmask &= readl(qm->io_base + QM_ABNORMAL_INT_MASK);
2180 	writel(irq_unmask, qm->io_base + QM_ABNORMAL_INT_MASK);
2181 }
2182 
2183 static void qm_hw_error_uninit_v3(struct hisi_qm *qm)
2184 {
2185 	writel(QM_ABNORMAL_INT_MASK_VALUE, qm->io_base + QM_ABNORMAL_INT_MASK);
2186 
2187 	/* disable close master ooo when hardware error happened */
2188 	writel(0x0, qm->io_base + QM_OOO_SHUTDOWN_SEL);
2189 }
2190 
2191 static void qm_log_hw_error(struct hisi_qm *qm, u32 error_status)
2192 {
2193 	const struct hisi_qm_hw_error *err;
2194 	struct device *dev = &qm->pdev->dev;
2195 	u32 reg_val, type, vf_num;
2196 	int i;
2197 
2198 	for (i = 0; i < ARRAY_SIZE(qm_hw_error); i++) {
2199 		err = &qm_hw_error[i];
2200 		if (!(err->int_msk & error_status))
2201 			continue;
2202 
2203 		dev_err(dev, "%s [error status=0x%x] found\n",
2204 			err->msg, err->int_msk);
2205 
2206 		if (err->int_msk & QM_DB_TIMEOUT) {
2207 			reg_val = readl(qm->io_base + QM_ABNORMAL_INF01);
2208 			type = (reg_val & QM_DB_TIMEOUT_TYPE) >>
2209 			       QM_DB_TIMEOUT_TYPE_SHIFT;
2210 			vf_num = reg_val & QM_DB_TIMEOUT_VF;
2211 			dev_err(dev, "qm %s doorbell timeout in function %u\n",
2212 				qm_db_timeout[type], vf_num);
2213 		} else if (err->int_msk & QM_OF_FIFO_OF) {
2214 			reg_val = readl(qm->io_base + QM_ABNORMAL_INF00);
2215 			type = (reg_val & QM_FIFO_OVERFLOW_TYPE) >>
2216 			       QM_FIFO_OVERFLOW_TYPE_SHIFT;
2217 			vf_num = reg_val & QM_FIFO_OVERFLOW_VF;
2218 
2219 			if (type < ARRAY_SIZE(qm_fifo_overflow))
2220 				dev_err(dev, "qm %s fifo overflow in function %u\n",
2221 					qm_fifo_overflow[type], vf_num);
2222 			else
2223 				dev_err(dev, "unknown error type\n");
2224 		}
2225 	}
2226 }
2227 
2228 static enum acc_err_result qm_hw_error_handle_v2(struct hisi_qm *qm)
2229 {
2230 	u32 error_status, tmp, val;
2231 
2232 	/* read err sts */
2233 	tmp = readl(qm->io_base + QM_ABNORMAL_INT_STATUS);
2234 	error_status = qm->error_mask & tmp;
2235 
2236 	if (error_status) {
2237 		if (error_status & QM_ECC_MBIT)
2238 			qm->err_status.is_qm_ecc_mbit = true;
2239 
2240 		qm_log_hw_error(qm, error_status);
2241 		val = error_status | QM_DB_RANDOM_INVALID | QM_BASE_CE;
2242 		/* ce error does not need to be reset */
2243 		if (val == (QM_DB_RANDOM_INVALID | QM_BASE_CE)) {
2244 			writel(error_status, qm->io_base +
2245 			       QM_ABNORMAL_INT_SOURCE);
2246 			writel(qm->err_info.nfe,
2247 			       qm->io_base + QM_RAS_NFE_ENABLE);
2248 			return ACC_ERR_RECOVERED;
2249 		}
2250 
2251 		return ACC_ERR_NEED_RESET;
2252 	}
2253 
2254 	return ACC_ERR_RECOVERED;
2255 }
2256 
2257 static int qm_get_mb_cmd(struct hisi_qm *qm, u64 *msg, u16 fun_num)
2258 {
2259 	struct qm_mailbox mailbox;
2260 	int ret;
2261 
2262 	qm_mb_pre_init(&mailbox, QM_MB_CMD_DST, 0, fun_num, 0);
2263 	mutex_lock(&qm->mailbox_lock);
2264 	ret = qm_mb_nolock(qm, &mailbox);
2265 	if (ret)
2266 		goto err_unlock;
2267 
2268 	*msg = readl(qm->io_base + QM_MB_CMD_DATA_ADDR_L) |
2269 		  ((u64)readl(qm->io_base + QM_MB_CMD_DATA_ADDR_H) << 32);
2270 
2271 err_unlock:
2272 	mutex_unlock(&qm->mailbox_lock);
2273 	return ret;
2274 }
2275 
2276 static void qm_clear_cmd_interrupt(struct hisi_qm *qm, u64 vf_mask)
2277 {
2278 	u32 val;
2279 
2280 	if (qm->fun_type == QM_HW_PF)
2281 		writeq(vf_mask, qm->io_base + QM_IFC_INT_SOURCE_P);
2282 
2283 	val = readl(qm->io_base + QM_IFC_INT_SOURCE_V);
2284 	val |= QM_IFC_INT_SOURCE_MASK;
2285 	writel(val, qm->io_base + QM_IFC_INT_SOURCE_V);
2286 }
2287 
2288 static void qm_handle_vf_msg(struct hisi_qm *qm, u32 vf_id)
2289 {
2290 	struct device *dev = &qm->pdev->dev;
2291 	u32 cmd;
2292 	u64 msg;
2293 	int ret;
2294 
2295 	ret = qm_get_mb_cmd(qm, &msg, vf_id);
2296 	if (ret) {
2297 		dev_err(dev, "failed to get msg from VF(%u)!\n", vf_id);
2298 		return;
2299 	}
2300 
2301 	cmd = msg & QM_MB_CMD_DATA_MASK;
2302 	switch (cmd) {
2303 	case QM_VF_PREPARE_FAIL:
2304 		dev_err(dev, "failed to stop VF(%u)!\n", vf_id);
2305 		break;
2306 	case QM_VF_START_FAIL:
2307 		dev_err(dev, "failed to start VF(%u)!\n", vf_id);
2308 		break;
2309 	case QM_VF_PREPARE_DONE:
2310 	case QM_VF_START_DONE:
2311 		break;
2312 	default:
2313 		dev_err(dev, "unsupported cmd %u sent by VF(%u)!\n", cmd, vf_id);
2314 		break;
2315 	}
2316 }
2317 
2318 static int qm_wait_vf_prepare_finish(struct hisi_qm *qm)
2319 {
2320 	struct device *dev = &qm->pdev->dev;
2321 	u32 vfs_num = qm->vfs_num;
2322 	int cnt = 0;
2323 	int ret = 0;
2324 	u64 val;
2325 	u32 i;
2326 
2327 	if (!qm->vfs_num || qm->ver < QM_HW_V3)
2328 		return 0;
2329 
2330 	while (true) {
2331 		val = readq(qm->io_base + QM_IFC_INT_SOURCE_P);
2332 		/* All VFs send command to PF, break */
2333 		if ((val & GENMASK(vfs_num, 1)) == GENMASK(vfs_num, 1))
2334 			break;
2335 
2336 		if (++cnt > QM_MAX_PF_WAIT_COUNT) {
2337 			ret = -EBUSY;
2338 			break;
2339 		}
2340 
2341 		msleep(QM_WAIT_DST_ACK);
2342 	}
2343 
2344 	/* PF check VFs msg */
2345 	for (i = 1; i <= vfs_num; i++) {
2346 		if (val & BIT(i))
2347 			qm_handle_vf_msg(qm, i);
2348 		else
2349 			dev_err(dev, "VF(%u) not ping PF!\n", i);
2350 	}
2351 
2352 	/* PF clear interrupt to ack VFs */
2353 	qm_clear_cmd_interrupt(qm, val);
2354 
2355 	return ret;
2356 }
2357 
2358 static void qm_trigger_vf_interrupt(struct hisi_qm *qm, u32 fun_num)
2359 {
2360 	u32 val;
2361 
2362 	val = readl(qm->io_base + QM_IFC_INT_CFG);
2363 	val &= ~QM_IFC_SEND_ALL_VFS;
2364 	val |= fun_num;
2365 	writel(val, qm->io_base + QM_IFC_INT_CFG);
2366 
2367 	val = readl(qm->io_base + QM_IFC_INT_SET_P);
2368 	val |= QM_IFC_INT_SET_MASK;
2369 	writel(val, qm->io_base + QM_IFC_INT_SET_P);
2370 }
2371 
2372 static void qm_trigger_pf_interrupt(struct hisi_qm *qm)
2373 {
2374 	u32 val;
2375 
2376 	val = readl(qm->io_base + QM_IFC_INT_SET_V);
2377 	val |= QM_IFC_INT_SET_MASK;
2378 	writel(val, qm->io_base + QM_IFC_INT_SET_V);
2379 }
2380 
2381 static int qm_ping_single_vf(struct hisi_qm *qm, u64 cmd, u32 fun_num)
2382 {
2383 	struct device *dev = &qm->pdev->dev;
2384 	struct qm_mailbox mailbox;
2385 	int cnt = 0;
2386 	u64 val;
2387 	int ret;
2388 
2389 	qm_mb_pre_init(&mailbox, QM_MB_CMD_SRC, cmd, fun_num, 0);
2390 	mutex_lock(&qm->mailbox_lock);
2391 	ret = qm_mb_nolock(qm, &mailbox);
2392 	if (ret) {
2393 		dev_err(dev, "failed to send command to vf(%u)!\n", fun_num);
2394 		goto err_unlock;
2395 	}
2396 
2397 	qm_trigger_vf_interrupt(qm, fun_num);
2398 	while (true) {
2399 		msleep(QM_WAIT_DST_ACK);
2400 		val = readq(qm->io_base + QM_IFC_READY_STATUS);
2401 		/* if VF respond, PF notifies VF successfully. */
2402 		if (!(val & BIT(fun_num)))
2403 			goto err_unlock;
2404 
2405 		if (++cnt > QM_MAX_PF_WAIT_COUNT) {
2406 			dev_err(dev, "failed to get response from VF(%u)!\n", fun_num);
2407 			ret = -ETIMEDOUT;
2408 			break;
2409 		}
2410 	}
2411 
2412 err_unlock:
2413 	mutex_unlock(&qm->mailbox_lock);
2414 	return ret;
2415 }
2416 
2417 static int qm_ping_all_vfs(struct hisi_qm *qm, u64 cmd)
2418 {
2419 	struct device *dev = &qm->pdev->dev;
2420 	u32 vfs_num = qm->vfs_num;
2421 	struct qm_mailbox mailbox;
2422 	u64 val = 0;
2423 	int cnt = 0;
2424 	int ret;
2425 	u32 i;
2426 
2427 	qm_mb_pre_init(&mailbox, QM_MB_CMD_SRC, cmd, QM_MB_PING_ALL_VFS, 0);
2428 	mutex_lock(&qm->mailbox_lock);
2429 	/* PF sends command to all VFs by mailbox */
2430 	ret = qm_mb_nolock(qm, &mailbox);
2431 	if (ret) {
2432 		dev_err(dev, "failed to send command to VFs!\n");
2433 		mutex_unlock(&qm->mailbox_lock);
2434 		return ret;
2435 	}
2436 
2437 	qm_trigger_vf_interrupt(qm, QM_IFC_SEND_ALL_VFS);
2438 	while (true) {
2439 		msleep(QM_WAIT_DST_ACK);
2440 		val = readq(qm->io_base + QM_IFC_READY_STATUS);
2441 		/* If all VFs acked, PF notifies VFs successfully. */
2442 		if (!(val & GENMASK(vfs_num, 1))) {
2443 			mutex_unlock(&qm->mailbox_lock);
2444 			return 0;
2445 		}
2446 
2447 		if (++cnt > QM_MAX_PF_WAIT_COUNT)
2448 			break;
2449 	}
2450 
2451 	mutex_unlock(&qm->mailbox_lock);
2452 
2453 	/* Check which vf respond timeout. */
2454 	for (i = 1; i <= vfs_num; i++) {
2455 		if (val & BIT(i))
2456 			dev_err(dev, "failed to get response from VF(%u)!\n", i);
2457 	}
2458 
2459 	return -ETIMEDOUT;
2460 }
2461 
2462 static int qm_ping_pf(struct hisi_qm *qm, u64 cmd)
2463 {
2464 	struct qm_mailbox mailbox;
2465 	int cnt = 0;
2466 	u32 val;
2467 	int ret;
2468 
2469 	qm_mb_pre_init(&mailbox, QM_MB_CMD_SRC, cmd, 0, 0);
2470 	mutex_lock(&qm->mailbox_lock);
2471 	ret = qm_mb_nolock(qm, &mailbox);
2472 	if (ret) {
2473 		dev_err(&qm->pdev->dev, "failed to send command to PF!\n");
2474 		goto unlock;
2475 	}
2476 
2477 	qm_trigger_pf_interrupt(qm);
2478 	/* Waiting for PF response */
2479 	while (true) {
2480 		msleep(QM_WAIT_DST_ACK);
2481 		val = readl(qm->io_base + QM_IFC_INT_SET_V);
2482 		if (!(val & QM_IFC_INT_STATUS_MASK))
2483 			break;
2484 
2485 		if (++cnt > QM_MAX_VF_WAIT_COUNT) {
2486 			ret = -ETIMEDOUT;
2487 			break;
2488 		}
2489 	}
2490 
2491 unlock:
2492 	mutex_unlock(&qm->mailbox_lock);
2493 	return ret;
2494 }
2495 
2496 static int qm_stop_qp(struct hisi_qp *qp)
2497 {
2498 	return qm_mb(qp->qm, QM_MB_CMD_STOP_QP, 0, qp->qp_id, 0);
2499 }
2500 
2501 static int qm_set_msi(struct hisi_qm *qm, bool set)
2502 {
2503 	struct pci_dev *pdev = qm->pdev;
2504 
2505 	if (set) {
2506 		pci_write_config_dword(pdev, pdev->msi_cap + PCI_MSI_MASK_64,
2507 				       0);
2508 	} else {
2509 		pci_write_config_dword(pdev, pdev->msi_cap + PCI_MSI_MASK_64,
2510 				       ACC_PEH_MSI_DISABLE);
2511 		if (qm->err_status.is_qm_ecc_mbit ||
2512 		    qm->err_status.is_dev_ecc_mbit)
2513 			return 0;
2514 
2515 		mdelay(1);
2516 		if (readl(qm->io_base + QM_PEH_DFX_INFO0))
2517 			return -EFAULT;
2518 	}
2519 
2520 	return 0;
2521 }
2522 
2523 static void qm_wait_msi_finish(struct hisi_qm *qm)
2524 {
2525 	struct pci_dev *pdev = qm->pdev;
2526 	u32 cmd = ~0;
2527 	int cnt = 0;
2528 	u32 val;
2529 	int ret;
2530 
2531 	while (true) {
2532 		pci_read_config_dword(pdev, pdev->msi_cap +
2533 				      PCI_MSI_PENDING_64, &cmd);
2534 		if (!cmd)
2535 			break;
2536 
2537 		if (++cnt > MAX_WAIT_COUNTS) {
2538 			pci_warn(pdev, "failed to empty MSI PENDING!\n");
2539 			break;
2540 		}
2541 
2542 		udelay(1);
2543 	}
2544 
2545 	ret = readl_relaxed_poll_timeout(qm->io_base + QM_PEH_DFX_INFO0,
2546 					 val, !(val & QM_PEH_DFX_MASK),
2547 					 POLL_PERIOD, POLL_TIMEOUT);
2548 	if (ret)
2549 		pci_warn(pdev, "failed to empty PEH MSI!\n");
2550 
2551 	ret = readl_relaxed_poll_timeout(qm->io_base + QM_PEH_DFX_INFO1,
2552 					 val, !(val & QM_PEH_MSI_FINISH_MASK),
2553 					 POLL_PERIOD, POLL_TIMEOUT);
2554 	if (ret)
2555 		pci_warn(pdev, "failed to finish MSI operation!\n");
2556 }
2557 
2558 static int qm_set_msi_v3(struct hisi_qm *qm, bool set)
2559 {
2560 	struct pci_dev *pdev = qm->pdev;
2561 	int ret = -ETIMEDOUT;
2562 	u32 cmd, i;
2563 
2564 	pci_read_config_dword(pdev, pdev->msi_cap, &cmd);
2565 	if (set)
2566 		cmd |= QM_MSI_CAP_ENABLE;
2567 	else
2568 		cmd &= ~QM_MSI_CAP_ENABLE;
2569 
2570 	pci_write_config_dword(pdev, pdev->msi_cap, cmd);
2571 	if (set) {
2572 		for (i = 0; i < MAX_WAIT_COUNTS; i++) {
2573 			pci_read_config_dword(pdev, pdev->msi_cap, &cmd);
2574 			if (cmd & QM_MSI_CAP_ENABLE)
2575 				return 0;
2576 
2577 			udelay(1);
2578 		}
2579 	} else {
2580 		udelay(WAIT_PERIOD_US_MIN);
2581 		qm_wait_msi_finish(qm);
2582 		ret = 0;
2583 	}
2584 
2585 	return ret;
2586 }
2587 
2588 static const struct hisi_qm_hw_ops qm_hw_ops_v1 = {
2589 	.qm_db = qm_db_v1,
2590 	.get_irq_num = qm_get_irq_num_v1,
2591 	.hw_error_init = qm_hw_error_init_v1,
2592 	.set_msi = qm_set_msi,
2593 };
2594 
2595 static const struct hisi_qm_hw_ops qm_hw_ops_v2 = {
2596 	.get_vft = qm_get_vft_v2,
2597 	.qm_db = qm_db_v2,
2598 	.get_irq_num = qm_get_irq_num_v2,
2599 	.hw_error_init = qm_hw_error_init_v2,
2600 	.hw_error_uninit = qm_hw_error_uninit_v2,
2601 	.hw_error_handle = qm_hw_error_handle_v2,
2602 	.set_msi = qm_set_msi,
2603 };
2604 
2605 static const struct hisi_qm_hw_ops qm_hw_ops_v3 = {
2606 	.get_vft = qm_get_vft_v2,
2607 	.qm_db = qm_db_v2,
2608 	.get_irq_num = qm_get_irq_num_v3,
2609 	.hw_error_init = qm_hw_error_init_v3,
2610 	.hw_error_uninit = qm_hw_error_uninit_v3,
2611 	.hw_error_handle = qm_hw_error_handle_v2,
2612 	.stop_qp = qm_stop_qp,
2613 	.set_msi = qm_set_msi_v3,
2614 	.ping_all_vfs = qm_ping_all_vfs,
2615 	.ping_pf = qm_ping_pf,
2616 };
2617 
2618 static void *qm_get_avail_sqe(struct hisi_qp *qp)
2619 {
2620 	struct hisi_qp_status *qp_status = &qp->qp_status;
2621 	u16 sq_tail = qp_status->sq_tail;
2622 
2623 	if (unlikely(atomic_read(&qp->qp_status.used) == QM_Q_DEPTH - 1))
2624 		return NULL;
2625 
2626 	return qp->sqe + sq_tail * qp->qm->sqe_size;
2627 }
2628 
2629 static void hisi_qm_unset_hw_reset(struct hisi_qp *qp)
2630 {
2631 	u64 *addr;
2632 
2633 	/* Use last 64 bits of DUS to reset status. */
2634 	addr = (u64 *)(qp->qdma.va + qp->qdma.size) - QM_RESET_STOP_TX_OFFSET;
2635 	*addr = 0;
2636 }
2637 
2638 static struct hisi_qp *qm_create_qp_nolock(struct hisi_qm *qm, u8 alg_type)
2639 {
2640 	struct device *dev = &qm->pdev->dev;
2641 	struct hisi_qp *qp;
2642 	int qp_id;
2643 
2644 	if (!qm_qp_avail_state(qm, NULL, QP_INIT))
2645 		return ERR_PTR(-EPERM);
2646 
2647 	if (qm->qp_in_used == qm->qp_num) {
2648 		dev_info_ratelimited(dev, "All %u queues of QM are busy!\n",
2649 				     qm->qp_num);
2650 		atomic64_inc(&qm->debug.dfx.create_qp_err_cnt);
2651 		return ERR_PTR(-EBUSY);
2652 	}
2653 
2654 	qp_id = idr_alloc_cyclic(&qm->qp_idr, NULL, 0, qm->qp_num, GFP_ATOMIC);
2655 	if (qp_id < 0) {
2656 		dev_info_ratelimited(dev, "All %u queues of QM are busy!\n",
2657 				    qm->qp_num);
2658 		atomic64_inc(&qm->debug.dfx.create_qp_err_cnt);
2659 		return ERR_PTR(-EBUSY);
2660 	}
2661 
2662 	qp = &qm->qp_array[qp_id];
2663 	hisi_qm_unset_hw_reset(qp);
2664 	memset(qp->cqe, 0, sizeof(struct qm_cqe) * QM_Q_DEPTH);
2665 
2666 	qp->event_cb = NULL;
2667 	qp->req_cb = NULL;
2668 	qp->qp_id = qp_id;
2669 	qp->alg_type = alg_type;
2670 	qp->is_in_kernel = true;
2671 	qm->qp_in_used++;
2672 	atomic_set(&qp->qp_status.flags, QP_INIT);
2673 
2674 	return qp;
2675 }
2676 
2677 /**
2678  * hisi_qm_create_qp() - Create a queue pair from qm.
2679  * @qm: The qm we create a qp from.
2680  * @alg_type: Accelerator specific algorithm type in sqc.
2681  *
2682  * return created qp, -EBUSY if all qps in qm allocated, -ENOMEM if allocating
2683  * qp memory fails.
2684  */
2685 struct hisi_qp *hisi_qm_create_qp(struct hisi_qm *qm, u8 alg_type)
2686 {
2687 	struct hisi_qp *qp;
2688 	int ret;
2689 
2690 	ret = qm_pm_get_sync(qm);
2691 	if (ret)
2692 		return ERR_PTR(ret);
2693 
2694 	down_write(&qm->qps_lock);
2695 	qp = qm_create_qp_nolock(qm, alg_type);
2696 	up_write(&qm->qps_lock);
2697 
2698 	if (IS_ERR(qp))
2699 		qm_pm_put_sync(qm);
2700 
2701 	return qp;
2702 }
2703 EXPORT_SYMBOL_GPL(hisi_qm_create_qp);
2704 
2705 /**
2706  * hisi_qm_release_qp() - Release a qp back to its qm.
2707  * @qp: The qp we want to release.
2708  *
2709  * This function releases the resource of a qp.
2710  */
2711 void hisi_qm_release_qp(struct hisi_qp *qp)
2712 {
2713 	struct hisi_qm *qm = qp->qm;
2714 
2715 	down_write(&qm->qps_lock);
2716 
2717 	if (!qm_qp_avail_state(qm, qp, QP_CLOSE)) {
2718 		up_write(&qm->qps_lock);
2719 		return;
2720 	}
2721 
2722 	qm->qp_in_used--;
2723 	idr_remove(&qm->qp_idr, qp->qp_id);
2724 
2725 	up_write(&qm->qps_lock);
2726 
2727 	qm_pm_put_sync(qm);
2728 }
2729 EXPORT_SYMBOL_GPL(hisi_qm_release_qp);
2730 
2731 static int qm_sq_ctx_cfg(struct hisi_qp *qp, int qp_id, u32 pasid)
2732 {
2733 	struct hisi_qm *qm = qp->qm;
2734 	struct device *dev = &qm->pdev->dev;
2735 	enum qm_hw_ver ver = qm->ver;
2736 	struct qm_sqc *sqc;
2737 	dma_addr_t sqc_dma;
2738 	int ret;
2739 
2740 	sqc = kzalloc(sizeof(struct qm_sqc), GFP_KERNEL);
2741 	if (!sqc)
2742 		return -ENOMEM;
2743 
2744 	INIT_QC_COMMON(sqc, qp->sqe_dma, pasid);
2745 	if (ver == QM_HW_V1) {
2746 		sqc->dw3 = cpu_to_le32(QM_MK_SQC_DW3_V1(0, 0, 0, qm->sqe_size));
2747 		sqc->w8 = cpu_to_le16(QM_Q_DEPTH - 1);
2748 	} else {
2749 		sqc->dw3 = cpu_to_le32(QM_MK_SQC_DW3_V2(qm->sqe_size));
2750 		sqc->w8 = 0; /* rand_qc */
2751 	}
2752 	sqc->cq_num = cpu_to_le16(qp_id);
2753 	sqc->w13 = cpu_to_le16(QM_MK_SQC_W13(0, 1, qp->alg_type));
2754 
2755 	if (ver >= QM_HW_V3 && qm->use_sva && !qp->is_in_kernel)
2756 		sqc->w11 = cpu_to_le16(QM_QC_PASID_ENABLE <<
2757 				       QM_QC_PASID_ENABLE_SHIFT);
2758 
2759 	sqc_dma = dma_map_single(dev, sqc, sizeof(struct qm_sqc),
2760 				 DMA_TO_DEVICE);
2761 	if (dma_mapping_error(dev, sqc_dma)) {
2762 		kfree(sqc);
2763 		return -ENOMEM;
2764 	}
2765 
2766 	ret = qm_mb(qm, QM_MB_CMD_SQC, sqc_dma, qp_id, 0);
2767 	dma_unmap_single(dev, sqc_dma, sizeof(struct qm_sqc), DMA_TO_DEVICE);
2768 	kfree(sqc);
2769 
2770 	return ret;
2771 }
2772 
2773 static int qm_cq_ctx_cfg(struct hisi_qp *qp, int qp_id, u32 pasid)
2774 {
2775 	struct hisi_qm *qm = qp->qm;
2776 	struct device *dev = &qm->pdev->dev;
2777 	enum qm_hw_ver ver = qm->ver;
2778 	struct qm_cqc *cqc;
2779 	dma_addr_t cqc_dma;
2780 	int ret;
2781 
2782 	cqc = kzalloc(sizeof(struct qm_cqc), GFP_KERNEL);
2783 	if (!cqc)
2784 		return -ENOMEM;
2785 
2786 	INIT_QC_COMMON(cqc, qp->cqe_dma, pasid);
2787 	if (ver == QM_HW_V1) {
2788 		cqc->dw3 = cpu_to_le32(QM_MK_CQC_DW3_V1(0, 0, 0,
2789 							QM_QC_CQE_SIZE));
2790 		cqc->w8 = cpu_to_le16(QM_Q_DEPTH - 1);
2791 	} else {
2792 		cqc->dw3 = cpu_to_le32(QM_MK_CQC_DW3_V2(QM_QC_CQE_SIZE));
2793 		cqc->w8 = 0; /* rand_qc */
2794 	}
2795 	cqc->dw6 = cpu_to_le32(1 << QM_CQ_PHASE_SHIFT | 1 << QM_CQ_FLAG_SHIFT);
2796 
2797 	if (ver >= QM_HW_V3 && qm->use_sva && !qp->is_in_kernel)
2798 		cqc->w11 = cpu_to_le16(QM_QC_PASID_ENABLE);
2799 
2800 	cqc_dma = dma_map_single(dev, cqc, sizeof(struct qm_cqc),
2801 				 DMA_TO_DEVICE);
2802 	if (dma_mapping_error(dev, cqc_dma)) {
2803 		kfree(cqc);
2804 		return -ENOMEM;
2805 	}
2806 
2807 	ret = qm_mb(qm, QM_MB_CMD_CQC, cqc_dma, qp_id, 0);
2808 	dma_unmap_single(dev, cqc_dma, sizeof(struct qm_cqc), DMA_TO_DEVICE);
2809 	kfree(cqc);
2810 
2811 	return ret;
2812 }
2813 
2814 static int qm_qp_ctx_cfg(struct hisi_qp *qp, int qp_id, u32 pasid)
2815 {
2816 	int ret;
2817 
2818 	qm_init_qp_status(qp);
2819 
2820 	ret = qm_sq_ctx_cfg(qp, qp_id, pasid);
2821 	if (ret)
2822 		return ret;
2823 
2824 	return qm_cq_ctx_cfg(qp, qp_id, pasid);
2825 }
2826 
2827 static int qm_start_qp_nolock(struct hisi_qp *qp, unsigned long arg)
2828 {
2829 	struct hisi_qm *qm = qp->qm;
2830 	struct device *dev = &qm->pdev->dev;
2831 	int qp_id = qp->qp_id;
2832 	u32 pasid = arg;
2833 	int ret;
2834 
2835 	if (!qm_qp_avail_state(qm, qp, QP_START))
2836 		return -EPERM;
2837 
2838 	ret = qm_qp_ctx_cfg(qp, qp_id, pasid);
2839 	if (ret)
2840 		return ret;
2841 
2842 	atomic_set(&qp->qp_status.flags, QP_START);
2843 	dev_dbg(dev, "queue %d started\n", qp_id);
2844 
2845 	return 0;
2846 }
2847 
2848 /**
2849  * hisi_qm_start_qp() - Start a qp into running.
2850  * @qp: The qp we want to start to run.
2851  * @arg: Accelerator specific argument.
2852  *
2853  * After this function, qp can receive request from user. Return 0 if
2854  * successful, Return -EBUSY if failed.
2855  */
2856 int hisi_qm_start_qp(struct hisi_qp *qp, unsigned long arg)
2857 {
2858 	struct hisi_qm *qm = qp->qm;
2859 	int ret;
2860 
2861 	down_write(&qm->qps_lock);
2862 	ret = qm_start_qp_nolock(qp, arg);
2863 	up_write(&qm->qps_lock);
2864 
2865 	return ret;
2866 }
2867 EXPORT_SYMBOL_GPL(hisi_qm_start_qp);
2868 
2869 /**
2870  * qp_stop_fail_cb() - call request cb.
2871  * @qp: stopped failed qp.
2872  *
2873  * Callback function should be called whether task completed or not.
2874  */
2875 static void qp_stop_fail_cb(struct hisi_qp *qp)
2876 {
2877 	int qp_used = atomic_read(&qp->qp_status.used);
2878 	u16 cur_tail = qp->qp_status.sq_tail;
2879 	u16 cur_head = (cur_tail + QM_Q_DEPTH - qp_used) % QM_Q_DEPTH;
2880 	struct hisi_qm *qm = qp->qm;
2881 	u16 pos;
2882 	int i;
2883 
2884 	for (i = 0; i < qp_used; i++) {
2885 		pos = (i + cur_head) % QM_Q_DEPTH;
2886 		qp->req_cb(qp, qp->sqe + (u32)(qm->sqe_size * pos));
2887 		atomic_dec(&qp->qp_status.used);
2888 	}
2889 }
2890 
2891 /**
2892  * qm_drain_qp() - Drain a qp.
2893  * @qp: The qp we want to drain.
2894  *
2895  * Determine whether the queue is cleared by judging the tail pointers of
2896  * sq and cq.
2897  */
2898 static int qm_drain_qp(struct hisi_qp *qp)
2899 {
2900 	size_t size = sizeof(struct qm_sqc) + sizeof(struct qm_cqc);
2901 	struct hisi_qm *qm = qp->qm;
2902 	struct device *dev = &qm->pdev->dev;
2903 	struct qm_sqc *sqc;
2904 	struct qm_cqc *cqc;
2905 	dma_addr_t dma_addr;
2906 	int ret = 0, i = 0;
2907 	void *addr;
2908 
2909 	/* No need to judge if master OOO is blocked. */
2910 	if (qm_check_dev_error(qm))
2911 		return 0;
2912 
2913 	/* Kunpeng930 supports drain qp by device */
2914 	if (qm->ops->stop_qp) {
2915 		ret = qm->ops->stop_qp(qp);
2916 		if (ret)
2917 			dev_err(dev, "Failed to stop qp(%u)!\n", qp->qp_id);
2918 		return ret;
2919 	}
2920 
2921 	addr = qm_ctx_alloc(qm, size, &dma_addr);
2922 	if (IS_ERR(addr)) {
2923 		dev_err(dev, "Failed to alloc ctx for sqc and cqc!\n");
2924 		return -ENOMEM;
2925 	}
2926 
2927 	while (++i) {
2928 		ret = qm_dump_sqc_raw(qm, dma_addr, qp->qp_id);
2929 		if (ret) {
2930 			dev_err_ratelimited(dev, "Failed to dump sqc!\n");
2931 			break;
2932 		}
2933 		sqc = addr;
2934 
2935 		ret = qm_dump_cqc_raw(qm, (dma_addr + sizeof(struct qm_sqc)),
2936 				      qp->qp_id);
2937 		if (ret) {
2938 			dev_err_ratelimited(dev, "Failed to dump cqc!\n");
2939 			break;
2940 		}
2941 		cqc = addr + sizeof(struct qm_sqc);
2942 
2943 		if ((sqc->tail == cqc->tail) &&
2944 		    (QM_SQ_TAIL_IDX(sqc) == QM_CQ_TAIL_IDX(cqc)))
2945 			break;
2946 
2947 		if (i == MAX_WAIT_COUNTS) {
2948 			dev_err(dev, "Fail to empty queue %u!\n", qp->qp_id);
2949 			ret = -EBUSY;
2950 			break;
2951 		}
2952 
2953 		usleep_range(WAIT_PERIOD_US_MIN, WAIT_PERIOD_US_MAX);
2954 	}
2955 
2956 	qm_ctx_free(qm, size, addr, &dma_addr);
2957 
2958 	return ret;
2959 }
2960 
2961 static int qm_stop_qp_nolock(struct hisi_qp *qp)
2962 {
2963 	struct device *dev = &qp->qm->pdev->dev;
2964 	int ret;
2965 
2966 	/*
2967 	 * It is allowed to stop and release qp when reset, If the qp is
2968 	 * stopped when reset but still want to be released then, the
2969 	 * is_resetting flag should be set negative so that this qp will not
2970 	 * be restarted after reset.
2971 	 */
2972 	if (atomic_read(&qp->qp_status.flags) == QP_STOP) {
2973 		qp->is_resetting = false;
2974 		return 0;
2975 	}
2976 
2977 	if (!qm_qp_avail_state(qp->qm, qp, QP_STOP))
2978 		return -EPERM;
2979 
2980 	atomic_set(&qp->qp_status.flags, QP_STOP);
2981 
2982 	ret = qm_drain_qp(qp);
2983 	if (ret)
2984 		dev_err(dev, "Failed to drain out data for stopping!\n");
2985 
2986 	if (qp->qm->wq)
2987 		flush_workqueue(qp->qm->wq);
2988 	else
2989 		flush_work(&qp->qm->work);
2990 
2991 	if (unlikely(qp->is_resetting && atomic_read(&qp->qp_status.used)))
2992 		qp_stop_fail_cb(qp);
2993 
2994 	dev_dbg(dev, "stop queue %u!", qp->qp_id);
2995 
2996 	return 0;
2997 }
2998 
2999 /**
3000  * hisi_qm_stop_qp() - Stop a qp in qm.
3001  * @qp: The qp we want to stop.
3002  *
3003  * This function is reverse of hisi_qm_start_qp. Return 0 if successful.
3004  */
3005 int hisi_qm_stop_qp(struct hisi_qp *qp)
3006 {
3007 	int ret;
3008 
3009 	down_write(&qp->qm->qps_lock);
3010 	ret = qm_stop_qp_nolock(qp);
3011 	up_write(&qp->qm->qps_lock);
3012 
3013 	return ret;
3014 }
3015 EXPORT_SYMBOL_GPL(hisi_qm_stop_qp);
3016 
3017 /**
3018  * hisi_qp_send() - Queue up a task in the hardware queue.
3019  * @qp: The qp in which to put the message.
3020  * @msg: The message.
3021  *
3022  * This function will return -EBUSY if qp is currently full, and -EAGAIN
3023  * if qp related qm is resetting.
3024  *
3025  * Note: This function may run with qm_irq_thread and ACC reset at same time.
3026  *       It has no race with qm_irq_thread. However, during hisi_qp_send, ACC
3027  *       reset may happen, we have no lock here considering performance. This
3028  *       causes current qm_db sending fail or can not receive sended sqe. QM
3029  *       sync/async receive function should handle the error sqe. ACC reset
3030  *       done function should clear used sqe to 0.
3031  */
3032 int hisi_qp_send(struct hisi_qp *qp, const void *msg)
3033 {
3034 	struct hisi_qp_status *qp_status = &qp->qp_status;
3035 	u16 sq_tail = qp_status->sq_tail;
3036 	u16 sq_tail_next = (sq_tail + 1) % QM_Q_DEPTH;
3037 	void *sqe = qm_get_avail_sqe(qp);
3038 
3039 	if (unlikely(atomic_read(&qp->qp_status.flags) == QP_STOP ||
3040 		     atomic_read(&qp->qm->status.flags) == QM_STOP ||
3041 		     qp->is_resetting)) {
3042 		dev_info_ratelimited(&qp->qm->pdev->dev, "QP is stopped or resetting\n");
3043 		return -EAGAIN;
3044 	}
3045 
3046 	if (!sqe)
3047 		return -EBUSY;
3048 
3049 	memcpy(sqe, msg, qp->qm->sqe_size);
3050 
3051 	qm_db(qp->qm, qp->qp_id, QM_DOORBELL_CMD_SQ, sq_tail_next, 0);
3052 	atomic_inc(&qp->qp_status.used);
3053 	qp_status->sq_tail = sq_tail_next;
3054 
3055 	return 0;
3056 }
3057 EXPORT_SYMBOL_GPL(hisi_qp_send);
3058 
3059 static void hisi_qm_cache_wb(struct hisi_qm *qm)
3060 {
3061 	unsigned int val;
3062 
3063 	if (qm->ver == QM_HW_V1)
3064 		return;
3065 
3066 	writel(0x1, qm->io_base + QM_CACHE_WB_START);
3067 	if (readl_relaxed_poll_timeout(qm->io_base + QM_CACHE_WB_DONE,
3068 				       val, val & BIT(0), POLL_PERIOD,
3069 				       POLL_TIMEOUT))
3070 		dev_err(&qm->pdev->dev, "QM writeback sqc cache fail!\n");
3071 }
3072 
3073 static void qm_qp_event_notifier(struct hisi_qp *qp)
3074 {
3075 	wake_up_interruptible(&qp->uacce_q->wait);
3076 }
3077 
3078 static int hisi_qm_get_available_instances(struct uacce_device *uacce)
3079 {
3080 	return hisi_qm_get_free_qp_num(uacce->priv);
3081 }
3082 
3083 static void hisi_qm_set_hw_reset(struct hisi_qm *qm, int offset)
3084 {
3085 	int i;
3086 
3087 	for (i = 0; i < qm->qp_num; i++)
3088 		qm_set_qp_disable(&qm->qp_array[i], offset);
3089 }
3090 
3091 static int hisi_qm_uacce_get_queue(struct uacce_device *uacce,
3092 				   unsigned long arg,
3093 				   struct uacce_queue *q)
3094 {
3095 	struct hisi_qm *qm = uacce->priv;
3096 	struct hisi_qp *qp;
3097 	u8 alg_type = 0;
3098 
3099 	qp = hisi_qm_create_qp(qm, alg_type);
3100 	if (IS_ERR(qp))
3101 		return PTR_ERR(qp);
3102 
3103 	q->priv = qp;
3104 	q->uacce = uacce;
3105 	qp->uacce_q = q;
3106 	qp->event_cb = qm_qp_event_notifier;
3107 	qp->pasid = arg;
3108 	qp->is_in_kernel = false;
3109 
3110 	return 0;
3111 }
3112 
3113 static void hisi_qm_uacce_put_queue(struct uacce_queue *q)
3114 {
3115 	struct hisi_qp *qp = q->priv;
3116 
3117 	hisi_qm_cache_wb(qp->qm);
3118 	hisi_qm_release_qp(qp);
3119 }
3120 
3121 /* map sq/cq/doorbell to user space */
3122 static int hisi_qm_uacce_mmap(struct uacce_queue *q,
3123 			      struct vm_area_struct *vma,
3124 			      struct uacce_qfile_region *qfr)
3125 {
3126 	struct hisi_qp *qp = q->priv;
3127 	struct hisi_qm *qm = qp->qm;
3128 	resource_size_t phys_base = qm->db_phys_base +
3129 				    qp->qp_id * qm->db_interval;
3130 	size_t sz = vma->vm_end - vma->vm_start;
3131 	struct pci_dev *pdev = qm->pdev;
3132 	struct device *dev = &pdev->dev;
3133 	unsigned long vm_pgoff;
3134 	int ret;
3135 
3136 	switch (qfr->type) {
3137 	case UACCE_QFRT_MMIO:
3138 		if (qm->ver == QM_HW_V1) {
3139 			if (sz > PAGE_SIZE * QM_DOORBELL_PAGE_NR)
3140 				return -EINVAL;
3141 		} else if (qm->ver == QM_HW_V2 || !qm->use_db_isolation) {
3142 			if (sz > PAGE_SIZE * (QM_DOORBELL_PAGE_NR +
3143 			    QM_DOORBELL_SQ_CQ_BASE_V2 / PAGE_SIZE))
3144 				return -EINVAL;
3145 		} else {
3146 			if (sz > qm->db_interval)
3147 				return -EINVAL;
3148 		}
3149 
3150 		vma->vm_flags |= VM_IO;
3151 
3152 		return remap_pfn_range(vma, vma->vm_start,
3153 				       phys_base >> PAGE_SHIFT,
3154 				       sz, pgprot_noncached(vma->vm_page_prot));
3155 	case UACCE_QFRT_DUS:
3156 		if (sz != qp->qdma.size)
3157 			return -EINVAL;
3158 
3159 		/*
3160 		 * dma_mmap_coherent() requires vm_pgoff as 0
3161 		 * restore vm_pfoff to initial value for mmap()
3162 		 */
3163 		vm_pgoff = vma->vm_pgoff;
3164 		vma->vm_pgoff = 0;
3165 		ret = dma_mmap_coherent(dev, vma, qp->qdma.va,
3166 					qp->qdma.dma, sz);
3167 		vma->vm_pgoff = vm_pgoff;
3168 		return ret;
3169 
3170 	default:
3171 		return -EINVAL;
3172 	}
3173 }
3174 
3175 static int hisi_qm_uacce_start_queue(struct uacce_queue *q)
3176 {
3177 	struct hisi_qp *qp = q->priv;
3178 
3179 	return hisi_qm_start_qp(qp, qp->pasid);
3180 }
3181 
3182 static void hisi_qm_uacce_stop_queue(struct uacce_queue *q)
3183 {
3184 	hisi_qm_stop_qp(q->priv);
3185 }
3186 
3187 static int hisi_qm_is_q_updated(struct uacce_queue *q)
3188 {
3189 	struct hisi_qp *qp = q->priv;
3190 	struct qm_cqe *cqe = qp->cqe + qp->qp_status.cq_head;
3191 	int updated = 0;
3192 
3193 	while (QM_CQE_PHASE(cqe) == qp->qp_status.cqc_phase) {
3194 		/* make sure to read data from memory */
3195 		dma_rmb();
3196 		qm_cq_head_update(qp);
3197 		cqe = qp->cqe + qp->qp_status.cq_head;
3198 		updated = 1;
3199 	}
3200 
3201 	return updated;
3202 }
3203 
3204 static void qm_set_sqctype(struct uacce_queue *q, u16 type)
3205 {
3206 	struct hisi_qm *qm = q->uacce->priv;
3207 	struct hisi_qp *qp = q->priv;
3208 
3209 	down_write(&qm->qps_lock);
3210 	qp->alg_type = type;
3211 	up_write(&qm->qps_lock);
3212 }
3213 
3214 static long hisi_qm_uacce_ioctl(struct uacce_queue *q, unsigned int cmd,
3215 				unsigned long arg)
3216 {
3217 	struct hisi_qp *qp = q->priv;
3218 	struct hisi_qp_ctx qp_ctx;
3219 
3220 	if (cmd == UACCE_CMD_QM_SET_QP_CTX) {
3221 		if (copy_from_user(&qp_ctx, (void __user *)arg,
3222 				   sizeof(struct hisi_qp_ctx)))
3223 			return -EFAULT;
3224 
3225 		if (qp_ctx.qc_type != 0 && qp_ctx.qc_type != 1)
3226 			return -EINVAL;
3227 
3228 		qm_set_sqctype(q, qp_ctx.qc_type);
3229 		qp_ctx.id = qp->qp_id;
3230 
3231 		if (copy_to_user((void __user *)arg, &qp_ctx,
3232 				 sizeof(struct hisi_qp_ctx)))
3233 			return -EFAULT;
3234 	} else {
3235 		return -EINVAL;
3236 	}
3237 
3238 	return 0;
3239 }
3240 
3241 static const struct uacce_ops uacce_qm_ops = {
3242 	.get_available_instances = hisi_qm_get_available_instances,
3243 	.get_queue = hisi_qm_uacce_get_queue,
3244 	.put_queue = hisi_qm_uacce_put_queue,
3245 	.start_queue = hisi_qm_uacce_start_queue,
3246 	.stop_queue = hisi_qm_uacce_stop_queue,
3247 	.mmap = hisi_qm_uacce_mmap,
3248 	.ioctl = hisi_qm_uacce_ioctl,
3249 	.is_q_updated = hisi_qm_is_q_updated,
3250 };
3251 
3252 static int qm_alloc_uacce(struct hisi_qm *qm)
3253 {
3254 	struct pci_dev *pdev = qm->pdev;
3255 	struct uacce_device *uacce;
3256 	unsigned long mmio_page_nr;
3257 	unsigned long dus_page_nr;
3258 	struct uacce_interface interface = {
3259 		.flags = UACCE_DEV_SVA,
3260 		.ops = &uacce_qm_ops,
3261 	};
3262 	int ret;
3263 
3264 	ret = strscpy(interface.name, dev_driver_string(&pdev->dev),
3265 		      sizeof(interface.name));
3266 	if (ret < 0)
3267 		return -ENAMETOOLONG;
3268 
3269 	uacce = uacce_alloc(&pdev->dev, &interface);
3270 	if (IS_ERR(uacce))
3271 		return PTR_ERR(uacce);
3272 
3273 	if (uacce->flags & UACCE_DEV_SVA) {
3274 		qm->use_sva = true;
3275 	} else {
3276 		/* only consider sva case */
3277 		uacce_remove(uacce);
3278 		qm->uacce = NULL;
3279 		return -EINVAL;
3280 	}
3281 
3282 	uacce->is_vf = pdev->is_virtfn;
3283 	uacce->priv = qm;
3284 	uacce->algs = qm->algs;
3285 
3286 	if (qm->ver == QM_HW_V1)
3287 		uacce->api_ver = HISI_QM_API_VER_BASE;
3288 	else if (qm->ver == QM_HW_V2)
3289 		uacce->api_ver = HISI_QM_API_VER2_BASE;
3290 	else
3291 		uacce->api_ver = HISI_QM_API_VER3_BASE;
3292 
3293 	if (qm->ver == QM_HW_V1)
3294 		mmio_page_nr = QM_DOORBELL_PAGE_NR;
3295 	else if (qm->ver == QM_HW_V2 || !qm->use_db_isolation)
3296 		mmio_page_nr = QM_DOORBELL_PAGE_NR +
3297 			QM_DOORBELL_SQ_CQ_BASE_V2 / PAGE_SIZE;
3298 	else
3299 		mmio_page_nr = qm->db_interval / PAGE_SIZE;
3300 
3301 	/* Add one more page for device or qp status */
3302 	dus_page_nr = (PAGE_SIZE - 1 + qm->sqe_size * QM_Q_DEPTH +
3303 		       sizeof(struct qm_cqe) * QM_Q_DEPTH  + PAGE_SIZE) >>
3304 					 PAGE_SHIFT;
3305 
3306 	uacce->qf_pg_num[UACCE_QFRT_MMIO] = mmio_page_nr;
3307 	uacce->qf_pg_num[UACCE_QFRT_DUS]  = dus_page_nr;
3308 
3309 	qm->uacce = uacce;
3310 
3311 	return 0;
3312 }
3313 
3314 /**
3315  * qm_frozen() - Try to froze QM to cut continuous queue request. If
3316  * there is user on the QM, return failure without doing anything.
3317  * @qm: The qm needed to be fronzen.
3318  *
3319  * This function frozes QM, then we can do SRIOV disabling.
3320  */
3321 static int qm_frozen(struct hisi_qm *qm)
3322 {
3323 	if (test_bit(QM_DRIVER_REMOVING, &qm->misc_ctl))
3324 		return 0;
3325 
3326 	down_write(&qm->qps_lock);
3327 
3328 	if (!qm->qp_in_used) {
3329 		qm->qp_in_used = qm->qp_num;
3330 		up_write(&qm->qps_lock);
3331 		set_bit(QM_DRIVER_REMOVING, &qm->misc_ctl);
3332 		return 0;
3333 	}
3334 
3335 	up_write(&qm->qps_lock);
3336 
3337 	return -EBUSY;
3338 }
3339 
3340 static int qm_try_frozen_vfs(struct pci_dev *pdev,
3341 			     struct hisi_qm_list *qm_list)
3342 {
3343 	struct hisi_qm *qm, *vf_qm;
3344 	struct pci_dev *dev;
3345 	int ret = 0;
3346 
3347 	if (!qm_list || !pdev)
3348 		return -EINVAL;
3349 
3350 	/* Try to frozen all the VFs as disable SRIOV */
3351 	mutex_lock(&qm_list->lock);
3352 	list_for_each_entry(qm, &qm_list->list, list) {
3353 		dev = qm->pdev;
3354 		if (dev == pdev)
3355 			continue;
3356 		if (pci_physfn(dev) == pdev) {
3357 			vf_qm = pci_get_drvdata(dev);
3358 			ret = qm_frozen(vf_qm);
3359 			if (ret)
3360 				goto frozen_fail;
3361 		}
3362 	}
3363 
3364 frozen_fail:
3365 	mutex_unlock(&qm_list->lock);
3366 
3367 	return ret;
3368 }
3369 
3370 /**
3371  * hisi_qm_wait_task_finish() - Wait until the task is finished
3372  * when removing the driver.
3373  * @qm: The qm needed to wait for the task to finish.
3374  * @qm_list: The list of all available devices.
3375  */
3376 void hisi_qm_wait_task_finish(struct hisi_qm *qm, struct hisi_qm_list *qm_list)
3377 {
3378 	while (qm_frozen(qm) ||
3379 	       ((qm->fun_type == QM_HW_PF) &&
3380 	       qm_try_frozen_vfs(qm->pdev, qm_list))) {
3381 		msleep(WAIT_PERIOD);
3382 	}
3383 
3384 	while (test_bit(QM_RST_SCHED, &qm->misc_ctl) ||
3385 	       test_bit(QM_RESETTING, &qm->misc_ctl))
3386 		msleep(WAIT_PERIOD);
3387 
3388 	udelay(REMOVE_WAIT_DELAY);
3389 }
3390 EXPORT_SYMBOL_GPL(hisi_qm_wait_task_finish);
3391 
3392 /**
3393  * hisi_qm_get_free_qp_num() - Get free number of qp in qm.
3394  * @qm: The qm which want to get free qp.
3395  *
3396  * This function return free number of qp in qm.
3397  */
3398 int hisi_qm_get_free_qp_num(struct hisi_qm *qm)
3399 {
3400 	int ret;
3401 
3402 	down_read(&qm->qps_lock);
3403 	ret = qm->qp_num - qm->qp_in_used;
3404 	up_read(&qm->qps_lock);
3405 
3406 	return ret;
3407 }
3408 EXPORT_SYMBOL_GPL(hisi_qm_get_free_qp_num);
3409 
3410 static void hisi_qp_memory_uninit(struct hisi_qm *qm, int num)
3411 {
3412 	struct device *dev = &qm->pdev->dev;
3413 	struct qm_dma *qdma;
3414 	int i;
3415 
3416 	for (i = num - 1; i >= 0; i--) {
3417 		qdma = &qm->qp_array[i].qdma;
3418 		dma_free_coherent(dev, qdma->size, qdma->va, qdma->dma);
3419 	}
3420 
3421 	kfree(qm->qp_array);
3422 }
3423 
3424 static int hisi_qp_memory_init(struct hisi_qm *qm, size_t dma_size, int id)
3425 {
3426 	struct device *dev = &qm->pdev->dev;
3427 	size_t off = qm->sqe_size * QM_Q_DEPTH;
3428 	struct hisi_qp *qp;
3429 
3430 	qp = &qm->qp_array[id];
3431 	qp->qdma.va = dma_alloc_coherent(dev, dma_size, &qp->qdma.dma,
3432 					 GFP_KERNEL);
3433 	if (!qp->qdma.va)
3434 		return -ENOMEM;
3435 
3436 	qp->sqe = qp->qdma.va;
3437 	qp->sqe_dma = qp->qdma.dma;
3438 	qp->cqe = qp->qdma.va + off;
3439 	qp->cqe_dma = qp->qdma.dma + off;
3440 	qp->qdma.size = dma_size;
3441 	qp->qm = qm;
3442 	qp->qp_id = id;
3443 
3444 	return 0;
3445 }
3446 
3447 static void hisi_qm_pre_init(struct hisi_qm *qm)
3448 {
3449 	struct pci_dev *pdev = qm->pdev;
3450 
3451 	if (qm->ver == QM_HW_V1)
3452 		qm->ops = &qm_hw_ops_v1;
3453 	else if (qm->ver == QM_HW_V2)
3454 		qm->ops = &qm_hw_ops_v2;
3455 	else
3456 		qm->ops = &qm_hw_ops_v3;
3457 
3458 	pci_set_drvdata(pdev, qm);
3459 	mutex_init(&qm->mailbox_lock);
3460 	init_rwsem(&qm->qps_lock);
3461 	qm->qp_in_used = 0;
3462 	qm->misc_ctl = false;
3463 	if (qm->fun_type == QM_HW_PF && qm->ver > QM_HW_V2) {
3464 		if (!acpi_device_power_manageable(ACPI_COMPANION(&pdev->dev)))
3465 			dev_info(&pdev->dev, "_PS0 and _PR0 are not defined");
3466 	}
3467 }
3468 
3469 static void qm_cmd_uninit(struct hisi_qm *qm)
3470 {
3471 	u32 val;
3472 
3473 	if (qm->ver < QM_HW_V3)
3474 		return;
3475 
3476 	val = readl(qm->io_base + QM_IFC_INT_MASK);
3477 	val |= QM_IFC_INT_DISABLE;
3478 	writel(val, qm->io_base + QM_IFC_INT_MASK);
3479 }
3480 
3481 static void qm_cmd_init(struct hisi_qm *qm)
3482 {
3483 	u32 val;
3484 
3485 	if (qm->ver < QM_HW_V3)
3486 		return;
3487 
3488 	/* Clear communication interrupt source */
3489 	qm_clear_cmd_interrupt(qm, QM_IFC_INT_SOURCE_CLR);
3490 
3491 	/* Enable pf to vf communication reg. */
3492 	val = readl(qm->io_base + QM_IFC_INT_MASK);
3493 	val &= ~QM_IFC_INT_DISABLE;
3494 	writel(val, qm->io_base + QM_IFC_INT_MASK);
3495 }
3496 
3497 static void qm_put_pci_res(struct hisi_qm *qm)
3498 {
3499 	struct pci_dev *pdev = qm->pdev;
3500 
3501 	if (qm->use_db_isolation)
3502 		iounmap(qm->db_io_base);
3503 
3504 	iounmap(qm->io_base);
3505 	pci_release_mem_regions(pdev);
3506 }
3507 
3508 static void hisi_qm_pci_uninit(struct hisi_qm *qm)
3509 {
3510 	struct pci_dev *pdev = qm->pdev;
3511 
3512 	pci_free_irq_vectors(pdev);
3513 	qm_put_pci_res(qm);
3514 	pci_disable_device(pdev);
3515 }
3516 
3517 /**
3518  * hisi_qm_uninit() - Uninitialize qm.
3519  * @qm: The qm needed uninit.
3520  *
3521  * This function uninits qm related device resources.
3522  */
3523 void hisi_qm_uninit(struct hisi_qm *qm)
3524 {
3525 	struct pci_dev *pdev = qm->pdev;
3526 	struct device *dev = &pdev->dev;
3527 
3528 	qm_cmd_uninit(qm);
3529 	kfree(qm->factor);
3530 	down_write(&qm->qps_lock);
3531 
3532 	if (!qm_avail_state(qm, QM_CLOSE)) {
3533 		up_write(&qm->qps_lock);
3534 		return;
3535 	}
3536 
3537 	hisi_qp_memory_uninit(qm, qm->qp_num);
3538 	idr_destroy(&qm->qp_idr);
3539 
3540 	if (qm->qdma.va) {
3541 		hisi_qm_cache_wb(qm);
3542 		dma_free_coherent(dev, qm->qdma.size,
3543 				  qm->qdma.va, qm->qdma.dma);
3544 	}
3545 	up_write(&qm->qps_lock);
3546 
3547 	qm_irq_unregister(qm);
3548 	hisi_qm_pci_uninit(qm);
3549 	if (qm->use_sva) {
3550 		uacce_remove(qm->uacce);
3551 		qm->uacce = NULL;
3552 	}
3553 }
3554 EXPORT_SYMBOL_GPL(hisi_qm_uninit);
3555 
3556 /**
3557  * hisi_qm_get_vft() - Get vft from a qm.
3558  * @qm: The qm we want to get its vft.
3559  * @base: The base number of queue in vft.
3560  * @number: The number of queues in vft.
3561  *
3562  * We can allocate multiple queues to a qm by configuring virtual function
3563  * table. We get related configures by this function. Normally, we call this
3564  * function in VF driver to get the queue information.
3565  *
3566  * qm hw v1 does not support this interface.
3567  */
3568 int hisi_qm_get_vft(struct hisi_qm *qm, u32 *base, u32 *number)
3569 {
3570 	if (!base || !number)
3571 		return -EINVAL;
3572 
3573 	if (!qm->ops->get_vft) {
3574 		dev_err(&qm->pdev->dev, "Don't support vft read!\n");
3575 		return -EINVAL;
3576 	}
3577 
3578 	return qm->ops->get_vft(qm, base, number);
3579 }
3580 EXPORT_SYMBOL_GPL(hisi_qm_get_vft);
3581 
3582 /**
3583  * hisi_qm_set_vft() - Set vft to a qm.
3584  * @qm: The qm we want to set its vft.
3585  * @fun_num: The function number.
3586  * @base: The base number of queue in vft.
3587  * @number: The number of queues in vft.
3588  *
3589  * This function is alway called in PF driver, it is used to assign queues
3590  * among PF and VFs.
3591  *
3592  * Assign queues A~B to PF: hisi_qm_set_vft(qm, 0, A, B - A + 1)
3593  * Assign queues A~B to VF: hisi_qm_set_vft(qm, 2, A, B - A + 1)
3594  * (VF function number 0x2)
3595  */
3596 static int hisi_qm_set_vft(struct hisi_qm *qm, u32 fun_num, u32 base,
3597 		    u32 number)
3598 {
3599 	u32 max_q_num = qm->ctrl_qp_num;
3600 
3601 	if (base >= max_q_num || number > max_q_num ||
3602 	    (base + number) > max_q_num)
3603 		return -EINVAL;
3604 
3605 	return qm_set_sqc_cqc_vft(qm, fun_num, base, number);
3606 }
3607 
3608 static void qm_init_eq_aeq_status(struct hisi_qm *qm)
3609 {
3610 	struct hisi_qm_status *status = &qm->status;
3611 
3612 	status->eq_head = 0;
3613 	status->aeq_head = 0;
3614 	status->eqc_phase = true;
3615 	status->aeqc_phase = true;
3616 }
3617 
3618 static void qm_enable_eq_aeq_interrupts(struct hisi_qm *qm)
3619 {
3620 	/* Clear eq/aeq interrupt source */
3621 	qm_db(qm, 0, QM_DOORBELL_CMD_AEQ, qm->status.aeq_head, 0);
3622 	qm_db(qm, 0, QM_DOORBELL_CMD_EQ, qm->status.eq_head, 0);
3623 
3624 	writel(0x0, qm->io_base + QM_VF_EQ_INT_MASK);
3625 	writel(0x0, qm->io_base + QM_VF_AEQ_INT_MASK);
3626 }
3627 
3628 static void qm_disable_eq_aeq_interrupts(struct hisi_qm *qm)
3629 {
3630 	writel(0x1, qm->io_base + QM_VF_EQ_INT_MASK);
3631 	writel(0x1, qm->io_base + QM_VF_AEQ_INT_MASK);
3632 }
3633 
3634 static int qm_eq_ctx_cfg(struct hisi_qm *qm)
3635 {
3636 	struct device *dev = &qm->pdev->dev;
3637 	struct qm_eqc *eqc;
3638 	dma_addr_t eqc_dma;
3639 	int ret;
3640 
3641 	eqc = kzalloc(sizeof(struct qm_eqc), GFP_KERNEL);
3642 	if (!eqc)
3643 		return -ENOMEM;
3644 
3645 	eqc->base_l = cpu_to_le32(lower_32_bits(qm->eqe_dma));
3646 	eqc->base_h = cpu_to_le32(upper_32_bits(qm->eqe_dma));
3647 	if (qm->ver == QM_HW_V1)
3648 		eqc->dw3 = cpu_to_le32(QM_EQE_AEQE_SIZE);
3649 	eqc->dw6 = cpu_to_le32((QM_EQ_DEPTH - 1) | (1 << QM_EQC_PHASE_SHIFT));
3650 
3651 	eqc_dma = dma_map_single(dev, eqc, sizeof(struct qm_eqc),
3652 				 DMA_TO_DEVICE);
3653 	if (dma_mapping_error(dev, eqc_dma)) {
3654 		kfree(eqc);
3655 		return -ENOMEM;
3656 	}
3657 
3658 	ret = qm_mb(qm, QM_MB_CMD_EQC, eqc_dma, 0, 0);
3659 	dma_unmap_single(dev, eqc_dma, sizeof(struct qm_eqc), DMA_TO_DEVICE);
3660 	kfree(eqc);
3661 
3662 	return ret;
3663 }
3664 
3665 static int qm_aeq_ctx_cfg(struct hisi_qm *qm)
3666 {
3667 	struct device *dev = &qm->pdev->dev;
3668 	struct qm_aeqc *aeqc;
3669 	dma_addr_t aeqc_dma;
3670 	int ret;
3671 
3672 	aeqc = kzalloc(sizeof(struct qm_aeqc), GFP_KERNEL);
3673 	if (!aeqc)
3674 		return -ENOMEM;
3675 
3676 	aeqc->base_l = cpu_to_le32(lower_32_bits(qm->aeqe_dma));
3677 	aeqc->base_h = cpu_to_le32(upper_32_bits(qm->aeqe_dma));
3678 	aeqc->dw6 = cpu_to_le32((QM_Q_DEPTH - 1) | (1 << QM_EQC_PHASE_SHIFT));
3679 
3680 	aeqc_dma = dma_map_single(dev, aeqc, sizeof(struct qm_aeqc),
3681 				  DMA_TO_DEVICE);
3682 	if (dma_mapping_error(dev, aeqc_dma)) {
3683 		kfree(aeqc);
3684 		return -ENOMEM;
3685 	}
3686 
3687 	ret = qm_mb(qm, QM_MB_CMD_AEQC, aeqc_dma, 0, 0);
3688 	dma_unmap_single(dev, aeqc_dma, sizeof(struct qm_aeqc), DMA_TO_DEVICE);
3689 	kfree(aeqc);
3690 
3691 	return ret;
3692 }
3693 
3694 static int qm_eq_aeq_ctx_cfg(struct hisi_qm *qm)
3695 {
3696 	struct device *dev = &qm->pdev->dev;
3697 	int ret;
3698 
3699 	qm_init_eq_aeq_status(qm);
3700 
3701 	ret = qm_eq_ctx_cfg(qm);
3702 	if (ret) {
3703 		dev_err(dev, "Set eqc failed!\n");
3704 		return ret;
3705 	}
3706 
3707 	return qm_aeq_ctx_cfg(qm);
3708 }
3709 
3710 static int __hisi_qm_start(struct hisi_qm *qm)
3711 {
3712 	int ret;
3713 
3714 	WARN_ON(!qm->qdma.va);
3715 
3716 	if (qm->fun_type == QM_HW_PF) {
3717 		ret = hisi_qm_set_vft(qm, 0, qm->qp_base, qm->qp_num);
3718 		if (ret)
3719 			return ret;
3720 	}
3721 
3722 	ret = qm_eq_aeq_ctx_cfg(qm);
3723 	if (ret)
3724 		return ret;
3725 
3726 	ret = qm_mb(qm, QM_MB_CMD_SQC_BT, qm->sqc_dma, 0, 0);
3727 	if (ret)
3728 		return ret;
3729 
3730 	ret = qm_mb(qm, QM_MB_CMD_CQC_BT, qm->cqc_dma, 0, 0);
3731 	if (ret)
3732 		return ret;
3733 
3734 	qm_init_prefetch(qm);
3735 	qm_enable_eq_aeq_interrupts(qm);
3736 
3737 	return 0;
3738 }
3739 
3740 /**
3741  * hisi_qm_start() - start qm
3742  * @qm: The qm to be started.
3743  *
3744  * This function starts a qm, then we can allocate qp from this qm.
3745  */
3746 int hisi_qm_start(struct hisi_qm *qm)
3747 {
3748 	struct device *dev = &qm->pdev->dev;
3749 	int ret = 0;
3750 
3751 	down_write(&qm->qps_lock);
3752 
3753 	if (!qm_avail_state(qm, QM_START)) {
3754 		up_write(&qm->qps_lock);
3755 		return -EPERM;
3756 	}
3757 
3758 	dev_dbg(dev, "qm start with %u queue pairs\n", qm->qp_num);
3759 
3760 	if (!qm->qp_num) {
3761 		dev_err(dev, "qp_num should not be 0\n");
3762 		ret = -EINVAL;
3763 		goto err_unlock;
3764 	}
3765 
3766 	ret = __hisi_qm_start(qm);
3767 	if (!ret)
3768 		atomic_set(&qm->status.flags, QM_START);
3769 
3770 err_unlock:
3771 	up_write(&qm->qps_lock);
3772 	return ret;
3773 }
3774 EXPORT_SYMBOL_GPL(hisi_qm_start);
3775 
3776 static int qm_restart(struct hisi_qm *qm)
3777 {
3778 	struct device *dev = &qm->pdev->dev;
3779 	struct hisi_qp *qp;
3780 	int ret, i;
3781 
3782 	ret = hisi_qm_start(qm);
3783 	if (ret < 0)
3784 		return ret;
3785 
3786 	down_write(&qm->qps_lock);
3787 	for (i = 0; i < qm->qp_num; i++) {
3788 		qp = &qm->qp_array[i];
3789 		if (atomic_read(&qp->qp_status.flags) == QP_STOP &&
3790 		    qp->is_resetting == true) {
3791 			ret = qm_start_qp_nolock(qp, 0);
3792 			if (ret < 0) {
3793 				dev_err(dev, "Failed to start qp%d!\n", i);
3794 
3795 				up_write(&qm->qps_lock);
3796 				return ret;
3797 			}
3798 			qp->is_resetting = false;
3799 		}
3800 	}
3801 	up_write(&qm->qps_lock);
3802 
3803 	return 0;
3804 }
3805 
3806 /* Stop started qps in reset flow */
3807 static int qm_stop_started_qp(struct hisi_qm *qm)
3808 {
3809 	struct device *dev = &qm->pdev->dev;
3810 	struct hisi_qp *qp;
3811 	int i, ret;
3812 
3813 	for (i = 0; i < qm->qp_num; i++) {
3814 		qp = &qm->qp_array[i];
3815 		if (qp && atomic_read(&qp->qp_status.flags) == QP_START) {
3816 			qp->is_resetting = true;
3817 			ret = qm_stop_qp_nolock(qp);
3818 			if (ret < 0) {
3819 				dev_err(dev, "Failed to stop qp%d!\n", i);
3820 				return ret;
3821 			}
3822 		}
3823 	}
3824 
3825 	return 0;
3826 }
3827 
3828 
3829 /**
3830  * qm_clear_queues() - Clear all queues memory in a qm.
3831  * @qm: The qm in which the queues will be cleared.
3832  *
3833  * This function clears all queues memory in a qm. Reset of accelerator can
3834  * use this to clear queues.
3835  */
3836 static void qm_clear_queues(struct hisi_qm *qm)
3837 {
3838 	struct hisi_qp *qp;
3839 	int i;
3840 
3841 	for (i = 0; i < qm->qp_num; i++) {
3842 		qp = &qm->qp_array[i];
3843 		if (qp->is_resetting)
3844 			memset(qp->qdma.va, 0, qp->qdma.size);
3845 	}
3846 
3847 	memset(qm->qdma.va, 0, qm->qdma.size);
3848 }
3849 
3850 /**
3851  * hisi_qm_stop() - Stop a qm.
3852  * @qm: The qm which will be stopped.
3853  * @r: The reason to stop qm.
3854  *
3855  * This function stops qm and its qps, then qm can not accept request.
3856  * Related resources are not released at this state, we can use hisi_qm_start
3857  * to let qm start again.
3858  */
3859 int hisi_qm_stop(struct hisi_qm *qm, enum qm_stop_reason r)
3860 {
3861 	struct device *dev = &qm->pdev->dev;
3862 	int ret = 0;
3863 
3864 	down_write(&qm->qps_lock);
3865 
3866 	qm->status.stop_reason = r;
3867 	if (!qm_avail_state(qm, QM_STOP)) {
3868 		ret = -EPERM;
3869 		goto err_unlock;
3870 	}
3871 
3872 	if (qm->status.stop_reason == QM_SOFT_RESET ||
3873 	    qm->status.stop_reason == QM_FLR) {
3874 		hisi_qm_set_hw_reset(qm, QM_RESET_STOP_TX_OFFSET);
3875 		ret = qm_stop_started_qp(qm);
3876 		if (ret < 0) {
3877 			dev_err(dev, "Failed to stop started qp!\n");
3878 			goto err_unlock;
3879 		}
3880 		hisi_qm_set_hw_reset(qm, QM_RESET_STOP_RX_OFFSET);
3881 	}
3882 
3883 	qm_disable_eq_aeq_interrupts(qm);
3884 	if (qm->fun_type == QM_HW_PF) {
3885 		ret = hisi_qm_set_vft(qm, 0, 0, 0);
3886 		if (ret < 0) {
3887 			dev_err(dev, "Failed to set vft!\n");
3888 			ret = -EBUSY;
3889 			goto err_unlock;
3890 		}
3891 	}
3892 
3893 	qm_clear_queues(qm);
3894 	atomic_set(&qm->status.flags, QM_STOP);
3895 
3896 err_unlock:
3897 	up_write(&qm->qps_lock);
3898 	return ret;
3899 }
3900 EXPORT_SYMBOL_GPL(hisi_qm_stop);
3901 
3902 static ssize_t qm_status_read(struct file *filp, char __user *buffer,
3903 			      size_t count, loff_t *pos)
3904 {
3905 	struct hisi_qm *qm = filp->private_data;
3906 	char buf[QM_DBG_READ_LEN];
3907 	int val, len;
3908 
3909 	val = atomic_read(&qm->status.flags);
3910 	len = scnprintf(buf, QM_DBG_READ_LEN, "%s\n", qm_s[val]);
3911 
3912 	return simple_read_from_buffer(buffer, count, pos, buf, len);
3913 }
3914 
3915 static const struct file_operations qm_status_fops = {
3916 	.owner = THIS_MODULE,
3917 	.open = simple_open,
3918 	.read = qm_status_read,
3919 };
3920 
3921 static int qm_debugfs_atomic64_set(void *data, u64 val)
3922 {
3923 	if (val)
3924 		return -EINVAL;
3925 
3926 	atomic64_set((atomic64_t *)data, 0);
3927 
3928 	return 0;
3929 }
3930 
3931 static int qm_debugfs_atomic64_get(void *data, u64 *val)
3932 {
3933 	*val = atomic64_read((atomic64_t *)data);
3934 
3935 	return 0;
3936 }
3937 
3938 DEFINE_DEBUGFS_ATTRIBUTE(qm_atomic64_ops, qm_debugfs_atomic64_get,
3939 			 qm_debugfs_atomic64_set, "%llu\n");
3940 
3941 static void qm_hw_error_init(struct hisi_qm *qm)
3942 {
3943 	struct hisi_qm_err_info *err_info = &qm->err_info;
3944 
3945 	if (!qm->ops->hw_error_init) {
3946 		dev_err(&qm->pdev->dev, "QM doesn't support hw error handling!\n");
3947 		return;
3948 	}
3949 
3950 	qm->ops->hw_error_init(qm, err_info->ce, err_info->nfe, err_info->fe);
3951 }
3952 
3953 static void qm_hw_error_uninit(struct hisi_qm *qm)
3954 {
3955 	if (!qm->ops->hw_error_uninit) {
3956 		dev_err(&qm->pdev->dev, "Unexpected QM hw error uninit!\n");
3957 		return;
3958 	}
3959 
3960 	qm->ops->hw_error_uninit(qm);
3961 }
3962 
3963 static enum acc_err_result qm_hw_error_handle(struct hisi_qm *qm)
3964 {
3965 	if (!qm->ops->hw_error_handle) {
3966 		dev_err(&qm->pdev->dev, "QM doesn't support hw error report!\n");
3967 		return ACC_ERR_NONE;
3968 	}
3969 
3970 	return qm->ops->hw_error_handle(qm);
3971 }
3972 
3973 /**
3974  * hisi_qm_dev_err_init() - Initialize device error configuration.
3975  * @qm: The qm for which we want to do error initialization.
3976  *
3977  * Initialize QM and device error related configuration.
3978  */
3979 void hisi_qm_dev_err_init(struct hisi_qm *qm)
3980 {
3981 	if (qm->fun_type == QM_HW_VF)
3982 		return;
3983 
3984 	qm_hw_error_init(qm);
3985 
3986 	if (!qm->err_ini->hw_err_enable) {
3987 		dev_err(&qm->pdev->dev, "Device doesn't support hw error init!\n");
3988 		return;
3989 	}
3990 	qm->err_ini->hw_err_enable(qm);
3991 }
3992 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_init);
3993 
3994 /**
3995  * hisi_qm_dev_err_uninit() - Uninitialize device error configuration.
3996  * @qm: The qm for which we want to do error uninitialization.
3997  *
3998  * Uninitialize QM and device error related configuration.
3999  */
4000 void hisi_qm_dev_err_uninit(struct hisi_qm *qm)
4001 {
4002 	if (qm->fun_type == QM_HW_VF)
4003 		return;
4004 
4005 	qm_hw_error_uninit(qm);
4006 
4007 	if (!qm->err_ini->hw_err_disable) {
4008 		dev_err(&qm->pdev->dev, "Unexpected device hw error uninit!\n");
4009 		return;
4010 	}
4011 	qm->err_ini->hw_err_disable(qm);
4012 }
4013 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_uninit);
4014 
4015 /**
4016  * hisi_qm_free_qps() - free multiple queue pairs.
4017  * @qps: The queue pairs need to be freed.
4018  * @qp_num: The num of queue pairs.
4019  */
4020 void hisi_qm_free_qps(struct hisi_qp **qps, int qp_num)
4021 {
4022 	int i;
4023 
4024 	if (!qps || qp_num <= 0)
4025 		return;
4026 
4027 	for (i = qp_num - 1; i >= 0; i--)
4028 		hisi_qm_release_qp(qps[i]);
4029 }
4030 EXPORT_SYMBOL_GPL(hisi_qm_free_qps);
4031 
4032 static void free_list(struct list_head *head)
4033 {
4034 	struct hisi_qm_resource *res, *tmp;
4035 
4036 	list_for_each_entry_safe(res, tmp, head, list) {
4037 		list_del(&res->list);
4038 		kfree(res);
4039 	}
4040 }
4041 
4042 static int hisi_qm_sort_devices(int node, struct list_head *head,
4043 				struct hisi_qm_list *qm_list)
4044 {
4045 	struct hisi_qm_resource *res, *tmp;
4046 	struct hisi_qm *qm;
4047 	struct list_head *n;
4048 	struct device *dev;
4049 	int dev_node = 0;
4050 
4051 	list_for_each_entry(qm, &qm_list->list, list) {
4052 		dev = &qm->pdev->dev;
4053 
4054 		if (IS_ENABLED(CONFIG_NUMA)) {
4055 			dev_node = dev_to_node(dev);
4056 			if (dev_node < 0)
4057 				dev_node = 0;
4058 		}
4059 
4060 		res = kzalloc(sizeof(*res), GFP_KERNEL);
4061 		if (!res)
4062 			return -ENOMEM;
4063 
4064 		res->qm = qm;
4065 		res->distance = node_distance(dev_node, node);
4066 		n = head;
4067 		list_for_each_entry(tmp, head, list) {
4068 			if (res->distance < tmp->distance) {
4069 				n = &tmp->list;
4070 				break;
4071 			}
4072 		}
4073 		list_add_tail(&res->list, n);
4074 	}
4075 
4076 	return 0;
4077 }
4078 
4079 /**
4080  * hisi_qm_alloc_qps_node() - Create multiple queue pairs.
4081  * @qm_list: The list of all available devices.
4082  * @qp_num: The number of queue pairs need created.
4083  * @alg_type: The algorithm type.
4084  * @node: The numa node.
4085  * @qps: The queue pairs need created.
4086  *
4087  * This function will sort all available device according to numa distance.
4088  * Then try to create all queue pairs from one device, if all devices do
4089  * not meet the requirements will return error.
4090  */
4091 int hisi_qm_alloc_qps_node(struct hisi_qm_list *qm_list, int qp_num,
4092 			   u8 alg_type, int node, struct hisi_qp **qps)
4093 {
4094 	struct hisi_qm_resource *tmp;
4095 	int ret = -ENODEV;
4096 	LIST_HEAD(head);
4097 	int i;
4098 
4099 	if (!qps || !qm_list || qp_num <= 0)
4100 		return -EINVAL;
4101 
4102 	mutex_lock(&qm_list->lock);
4103 	if (hisi_qm_sort_devices(node, &head, qm_list)) {
4104 		mutex_unlock(&qm_list->lock);
4105 		goto err;
4106 	}
4107 
4108 	list_for_each_entry(tmp, &head, list) {
4109 		for (i = 0; i < qp_num; i++) {
4110 			qps[i] = hisi_qm_create_qp(tmp->qm, alg_type);
4111 			if (IS_ERR(qps[i])) {
4112 				hisi_qm_free_qps(qps, i);
4113 				break;
4114 			}
4115 		}
4116 
4117 		if (i == qp_num) {
4118 			ret = 0;
4119 			break;
4120 		}
4121 	}
4122 
4123 	mutex_unlock(&qm_list->lock);
4124 	if (ret)
4125 		pr_info("Failed to create qps, node[%d], alg[%u], qp[%d]!\n",
4126 			node, alg_type, qp_num);
4127 
4128 err:
4129 	free_list(&head);
4130 	return ret;
4131 }
4132 EXPORT_SYMBOL_GPL(hisi_qm_alloc_qps_node);
4133 
4134 static int qm_vf_q_assign(struct hisi_qm *qm, u32 num_vfs)
4135 {
4136 	u32 remain_q_num, vfs_q_num, act_q_num, q_num, i, j;
4137 	u32 max_qp_num = qm->max_qp_num;
4138 	u32 q_base = qm->qp_num;
4139 	int ret;
4140 
4141 	if (!num_vfs)
4142 		return -EINVAL;
4143 
4144 	vfs_q_num = qm->ctrl_qp_num - qm->qp_num;
4145 
4146 	/* If vfs_q_num is less than num_vfs, return error. */
4147 	if (vfs_q_num < num_vfs)
4148 		return -EINVAL;
4149 
4150 	q_num = vfs_q_num / num_vfs;
4151 	remain_q_num = vfs_q_num % num_vfs;
4152 
4153 	for (i = num_vfs; i > 0; i--) {
4154 		/*
4155 		 * if q_num + remain_q_num > max_qp_num in last vf, divide the
4156 		 * remaining queues equally.
4157 		 */
4158 		if (i == num_vfs && q_num + remain_q_num <= max_qp_num) {
4159 			act_q_num = q_num + remain_q_num;
4160 			remain_q_num = 0;
4161 		} else if (remain_q_num > 0) {
4162 			act_q_num = q_num + 1;
4163 			remain_q_num--;
4164 		} else {
4165 			act_q_num = q_num;
4166 		}
4167 
4168 		act_q_num = min_t(int, act_q_num, max_qp_num);
4169 		ret = hisi_qm_set_vft(qm, i, q_base, act_q_num);
4170 		if (ret) {
4171 			for (j = num_vfs; j > i; j--)
4172 				hisi_qm_set_vft(qm, j, 0, 0);
4173 			return ret;
4174 		}
4175 		q_base += act_q_num;
4176 	}
4177 
4178 	return 0;
4179 }
4180 
4181 static int qm_clear_vft_config(struct hisi_qm *qm)
4182 {
4183 	int ret;
4184 	u32 i;
4185 
4186 	for (i = 1; i <= qm->vfs_num; i++) {
4187 		ret = hisi_qm_set_vft(qm, i, 0, 0);
4188 		if (ret)
4189 			return ret;
4190 	}
4191 	qm->vfs_num = 0;
4192 
4193 	return 0;
4194 }
4195 
4196 static int qm_func_shaper_enable(struct hisi_qm *qm, u32 fun_index, u32 qos)
4197 {
4198 	struct device *dev = &qm->pdev->dev;
4199 	u32 ir = qos * QM_QOS_RATE;
4200 	int ret, total_vfs, i;
4201 
4202 	total_vfs = pci_sriov_get_totalvfs(qm->pdev);
4203 	if (fun_index > total_vfs)
4204 		return -EINVAL;
4205 
4206 	qm->factor[fun_index].func_qos = qos;
4207 
4208 	ret = qm_get_shaper_para(ir, &qm->factor[fun_index]);
4209 	if (ret) {
4210 		dev_err(dev, "failed to calculate shaper parameter!\n");
4211 		return -EINVAL;
4212 	}
4213 
4214 	for (i = ALG_TYPE_0; i <= ALG_TYPE_1; i++) {
4215 		/* The base number of queue reuse for different alg type */
4216 		ret = qm_set_vft_common(qm, SHAPER_VFT, fun_index, i, 1);
4217 		if (ret) {
4218 			dev_err(dev, "type: %d, failed to set shaper vft!\n", i);
4219 			return -EINVAL;
4220 		}
4221 	}
4222 
4223 	return 0;
4224 }
4225 
4226 static u32 qm_get_shaper_vft_qos(struct hisi_qm *qm, u32 fun_index)
4227 {
4228 	u64 cir_u = 0, cir_b = 0, cir_s = 0;
4229 	u64 shaper_vft, ir_calc, ir;
4230 	unsigned int val;
4231 	u32 error_rate;
4232 	int ret;
4233 
4234 	ret = readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val,
4235 					 val & BIT(0), POLL_PERIOD,
4236 					 POLL_TIMEOUT);
4237 	if (ret)
4238 		return 0;
4239 
4240 	writel(0x1, qm->io_base + QM_VFT_CFG_OP_WR);
4241 	writel(SHAPER_VFT, qm->io_base + QM_VFT_CFG_TYPE);
4242 	writel(fun_index, qm->io_base + QM_VFT_CFG);
4243 
4244 	writel(0x0, qm->io_base + QM_VFT_CFG_RDY);
4245 	writel(0x1, qm->io_base + QM_VFT_CFG_OP_ENABLE);
4246 
4247 	ret = readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val,
4248 					 val & BIT(0), POLL_PERIOD,
4249 					 POLL_TIMEOUT);
4250 	if (ret)
4251 		return 0;
4252 
4253 	shaper_vft = readl(qm->io_base + QM_VFT_CFG_DATA_L) |
4254 		  ((u64)readl(qm->io_base + QM_VFT_CFG_DATA_H) << 32);
4255 
4256 	cir_b = shaper_vft & QM_SHAPER_CIR_B_MASK;
4257 	cir_u = shaper_vft & QM_SHAPER_CIR_U_MASK;
4258 	cir_u = cir_u >> QM_SHAPER_FACTOR_CIR_U_SHIFT;
4259 
4260 	cir_s = shaper_vft & QM_SHAPER_CIR_S_MASK;
4261 	cir_s = cir_s >> QM_SHAPER_FACTOR_CIR_S_SHIFT;
4262 
4263 	ir_calc = acc_shaper_para_calc(cir_b, cir_u, cir_s);
4264 
4265 	ir = qm->factor[fun_index].func_qos * QM_QOS_RATE;
4266 
4267 	error_rate = QM_QOS_EXPAND_RATE * (u32)abs(ir_calc - ir) / ir;
4268 	if (error_rate > QM_QOS_MIN_ERROR_RATE) {
4269 		pci_err(qm->pdev, "error_rate: %u, get function qos is error!\n", error_rate);
4270 		return 0;
4271 	}
4272 
4273 	return ir;
4274 }
4275 
4276 static void qm_vf_get_qos(struct hisi_qm *qm, u32 fun_num)
4277 {
4278 	struct device *dev = &qm->pdev->dev;
4279 	u64 mb_cmd;
4280 	u32 qos;
4281 	int ret;
4282 
4283 	qos = qm_get_shaper_vft_qos(qm, fun_num);
4284 	if (!qos) {
4285 		dev_err(dev, "function(%u) failed to get qos by PF!\n", fun_num);
4286 		return;
4287 	}
4288 
4289 	mb_cmd = QM_PF_SET_QOS | (u64)qos << QM_MB_CMD_DATA_SHIFT;
4290 	ret = qm_ping_single_vf(qm, mb_cmd, fun_num);
4291 	if (ret)
4292 		dev_err(dev, "failed to send cmd to VF(%u)!\n", fun_num);
4293 }
4294 
4295 static int qm_vf_read_qos(struct hisi_qm *qm)
4296 {
4297 	int cnt = 0;
4298 	int ret;
4299 
4300 	/* reset mailbox qos val */
4301 	qm->mb_qos = 0;
4302 
4303 	/* vf ping pf to get function qos */
4304 	if (qm->ops->ping_pf) {
4305 		ret = qm->ops->ping_pf(qm, QM_VF_GET_QOS);
4306 		if (ret) {
4307 			pci_err(qm->pdev, "failed to send cmd to PF to get qos!\n");
4308 			return ret;
4309 		}
4310 	}
4311 
4312 	while (true) {
4313 		msleep(QM_WAIT_DST_ACK);
4314 		if (qm->mb_qos)
4315 			break;
4316 
4317 		if (++cnt > QM_MAX_VF_WAIT_COUNT) {
4318 			pci_err(qm->pdev, "PF ping VF timeout!\n");
4319 			return  -ETIMEDOUT;
4320 		}
4321 	}
4322 
4323 	return ret;
4324 }
4325 
4326 static ssize_t qm_algqos_read(struct file *filp, char __user *buf,
4327 			       size_t count, loff_t *pos)
4328 {
4329 	struct hisi_qm *qm = filp->private_data;
4330 	char tbuf[QM_DBG_READ_LEN];
4331 	u32 qos_val, ir;
4332 	int ret;
4333 
4334 	ret = hisi_qm_get_dfx_access(qm);
4335 	if (ret)
4336 		return ret;
4337 
4338 	/* Mailbox and reset cannot be operated at the same time */
4339 	if (test_and_set_bit(QM_RESETTING, &qm->misc_ctl)) {
4340 		pci_err(qm->pdev, "dev resetting, read alg qos failed!\n");
4341 		ret = -EAGAIN;
4342 		goto err_put_dfx_access;
4343 	}
4344 
4345 	if (qm->fun_type == QM_HW_PF) {
4346 		ir = qm_get_shaper_vft_qos(qm, 0);
4347 	} else {
4348 		ret = qm_vf_read_qos(qm);
4349 		if (ret)
4350 			goto err_get_status;
4351 		ir = qm->mb_qos;
4352 	}
4353 
4354 	qos_val = ir / QM_QOS_RATE;
4355 	ret = scnprintf(tbuf, QM_DBG_READ_LEN, "%u\n", qos_val);
4356 
4357 	ret =  simple_read_from_buffer(buf, count, pos, tbuf, ret);
4358 
4359 err_get_status:
4360 	clear_bit(QM_RESETTING, &qm->misc_ctl);
4361 err_put_dfx_access:
4362 	hisi_qm_put_dfx_access(qm);
4363 	return ret;
4364 }
4365 
4366 static ssize_t qm_qos_value_init(const char *buf, unsigned long *val)
4367 {
4368 	int buflen = strlen(buf);
4369 	int ret, i;
4370 
4371 	for (i = 0; i < buflen; i++) {
4372 		if (!isdigit(buf[i]))
4373 			return -EINVAL;
4374 	}
4375 
4376 	ret = sscanf(buf, "%lu", val);
4377 	if (ret != QM_QOS_VAL_NUM)
4378 		return -EINVAL;
4379 
4380 	return 0;
4381 }
4382 
4383 static ssize_t qm_get_qos_value(struct hisi_qm *qm, const char *buf,
4384 			       unsigned long *val,
4385 			       unsigned int *fun_index)
4386 {
4387 	char tbuf_bdf[QM_DBG_READ_LEN] = {0};
4388 	char val_buf[QM_QOS_VAL_MAX_LEN] = {0};
4389 	u32 tmp1, device, function;
4390 	int ret, bus;
4391 
4392 	ret = sscanf(buf, "%s %s", tbuf_bdf, val_buf);
4393 	if (ret != QM_QOS_PARAM_NUM)
4394 		return -EINVAL;
4395 
4396 	ret = qm_qos_value_init(val_buf, val);
4397 	if (ret || *val == 0 || *val > QM_QOS_MAX_VAL) {
4398 		pci_err(qm->pdev, "input qos value is error, please set 1~1000!\n");
4399 		return -EINVAL;
4400 	}
4401 
4402 	ret = sscanf(tbuf_bdf, "%u:%x:%u.%u", &tmp1, &bus, &device, &function);
4403 	if (ret != QM_QOS_BDF_PARAM_NUM) {
4404 		pci_err(qm->pdev, "input pci bdf value is error!\n");
4405 		return -EINVAL;
4406 	}
4407 
4408 	*fun_index = PCI_DEVFN(device, function);
4409 
4410 	return 0;
4411 }
4412 
4413 static ssize_t qm_algqos_write(struct file *filp, const char __user *buf,
4414 			       size_t count, loff_t *pos)
4415 {
4416 	struct hisi_qm *qm = filp->private_data;
4417 	char tbuf[QM_DBG_READ_LEN];
4418 	unsigned int fun_index;
4419 	unsigned long val;
4420 	int len, ret;
4421 
4422 	if (qm->fun_type == QM_HW_VF)
4423 		return -EINVAL;
4424 
4425 	if (*pos != 0)
4426 		return 0;
4427 
4428 	if (count >= QM_DBG_READ_LEN)
4429 		return -ENOSPC;
4430 
4431 	len = simple_write_to_buffer(tbuf, QM_DBG_READ_LEN - 1, pos, buf, count);
4432 	if (len < 0)
4433 		return len;
4434 
4435 	tbuf[len] = '\0';
4436 	ret = qm_get_qos_value(qm, tbuf, &val, &fun_index);
4437 	if (ret)
4438 		return ret;
4439 
4440 	/* Mailbox and reset cannot be operated at the same time */
4441 	if (test_and_set_bit(QM_RESETTING, &qm->misc_ctl)) {
4442 		pci_err(qm->pdev, "dev resetting, write alg qos failed!\n");
4443 		return -EAGAIN;
4444 	}
4445 
4446 	ret = qm_pm_get_sync(qm);
4447 	if (ret) {
4448 		ret = -EINVAL;
4449 		goto err_get_status;
4450 	}
4451 
4452 	ret = qm_func_shaper_enable(qm, fun_index, val);
4453 	if (ret) {
4454 		pci_err(qm->pdev, "failed to enable function shaper!\n");
4455 		ret = -EINVAL;
4456 		goto err_put_sync;
4457 	}
4458 
4459 	pci_info(qm->pdev, "the qos value of function%u is set to %lu.\n",
4460 		 fun_index, val);
4461 	ret = count;
4462 
4463 err_put_sync:
4464 	qm_pm_put_sync(qm);
4465 err_get_status:
4466 	clear_bit(QM_RESETTING, &qm->misc_ctl);
4467 	return ret;
4468 }
4469 
4470 static const struct file_operations qm_algqos_fops = {
4471 	.owner = THIS_MODULE,
4472 	.open = simple_open,
4473 	.read = qm_algqos_read,
4474 	.write = qm_algqos_write,
4475 };
4476 
4477 /**
4478  * hisi_qm_set_algqos_init() - Initialize function qos debugfs files.
4479  * @qm: The qm for which we want to add debugfs files.
4480  *
4481  * Create function qos debugfs files.
4482  */
4483 static void hisi_qm_set_algqos_init(struct hisi_qm *qm)
4484 {
4485 	if (qm->fun_type == QM_HW_PF)
4486 		debugfs_create_file("alg_qos", 0644, qm->debug.debug_root,
4487 				    qm, &qm_algqos_fops);
4488 	else
4489 		debugfs_create_file("alg_qos", 0444, qm->debug.debug_root,
4490 				    qm, &qm_algqos_fops);
4491 }
4492 
4493 /**
4494  * hisi_qm_debug_init() - Initialize qm related debugfs files.
4495  * @qm: The qm for which we want to add debugfs files.
4496  *
4497  * Create qm related debugfs files.
4498  */
4499 void hisi_qm_debug_init(struct hisi_qm *qm)
4500 {
4501 	struct qm_dfx *dfx = &qm->debug.dfx;
4502 	struct dentry *qm_d;
4503 	void *data;
4504 	int i;
4505 
4506 	qm_d = debugfs_create_dir("qm", qm->debug.debug_root);
4507 	qm->debug.qm_d = qm_d;
4508 
4509 	/* only show this in PF */
4510 	if (qm->fun_type == QM_HW_PF) {
4511 		qm_create_debugfs_file(qm, qm->debug.debug_root, CURRENT_QM);
4512 		for (i = CURRENT_Q; i < DEBUG_FILE_NUM; i++)
4513 			qm_create_debugfs_file(qm, qm->debug.qm_d, i);
4514 	}
4515 
4516 	debugfs_create_file("regs", 0444, qm->debug.qm_d, qm, &qm_regs_fops);
4517 
4518 	debugfs_create_file("cmd", 0600, qm->debug.qm_d, qm, &qm_cmd_fops);
4519 
4520 	debugfs_create_file("status", 0444, qm->debug.qm_d, qm,
4521 			&qm_status_fops);
4522 	for (i = 0; i < ARRAY_SIZE(qm_dfx_files); i++) {
4523 		data = (atomic64_t *)((uintptr_t)dfx + qm_dfx_files[i].offset);
4524 		debugfs_create_file(qm_dfx_files[i].name,
4525 			0644,
4526 			qm_d,
4527 			data,
4528 			&qm_atomic64_ops);
4529 	}
4530 
4531 	if (qm->ver >= QM_HW_V3)
4532 		hisi_qm_set_algqos_init(qm);
4533 }
4534 EXPORT_SYMBOL_GPL(hisi_qm_debug_init);
4535 
4536 /**
4537  * hisi_qm_debug_regs_clear() - clear qm debug related registers.
4538  * @qm: The qm for which we want to clear its debug registers.
4539  */
4540 void hisi_qm_debug_regs_clear(struct hisi_qm *qm)
4541 {
4542 	const struct debugfs_reg32 *regs;
4543 	int i;
4544 
4545 	/* clear current_qm */
4546 	writel(0x0, qm->io_base + QM_DFX_MB_CNT_VF);
4547 	writel(0x0, qm->io_base + QM_DFX_DB_CNT_VF);
4548 
4549 	/* clear current_q */
4550 	writel(0x0, qm->io_base + QM_DFX_SQE_CNT_VF_SQN);
4551 	writel(0x0, qm->io_base + QM_DFX_CQE_CNT_VF_CQN);
4552 
4553 	/*
4554 	 * these registers are reading and clearing, so clear them after
4555 	 * reading them.
4556 	 */
4557 	writel(0x1, qm->io_base + QM_DFX_CNT_CLR_CE);
4558 
4559 	regs = qm_dfx_regs;
4560 	for (i = 0; i < CNT_CYC_REGS_NUM; i++) {
4561 		readl(qm->io_base + regs->offset);
4562 		regs++;
4563 	}
4564 
4565 	/* clear clear_enable */
4566 	writel(0x0, qm->io_base + QM_DFX_CNT_CLR_CE);
4567 }
4568 EXPORT_SYMBOL_GPL(hisi_qm_debug_regs_clear);
4569 
4570 /**
4571  * hisi_qm_sriov_enable() - enable virtual functions
4572  * @pdev: the PCIe device
4573  * @max_vfs: the number of virtual functions to enable
4574  *
4575  * Returns the number of enabled VFs. If there are VFs enabled already or
4576  * max_vfs is more than the total number of device can be enabled, returns
4577  * failure.
4578  */
4579 int hisi_qm_sriov_enable(struct pci_dev *pdev, int max_vfs)
4580 {
4581 	struct hisi_qm *qm = pci_get_drvdata(pdev);
4582 	int pre_existing_vfs, num_vfs, total_vfs, ret;
4583 
4584 	ret = qm_pm_get_sync(qm);
4585 	if (ret)
4586 		return ret;
4587 
4588 	total_vfs = pci_sriov_get_totalvfs(pdev);
4589 	pre_existing_vfs = pci_num_vf(pdev);
4590 	if (pre_existing_vfs) {
4591 		pci_err(pdev, "%d VFs already enabled. Please disable pre-enabled VFs!\n",
4592 			pre_existing_vfs);
4593 		goto err_put_sync;
4594 	}
4595 
4596 	num_vfs = min_t(int, max_vfs, total_vfs);
4597 	ret = qm_vf_q_assign(qm, num_vfs);
4598 	if (ret) {
4599 		pci_err(pdev, "Can't assign queues for VF!\n");
4600 		goto err_put_sync;
4601 	}
4602 
4603 	qm->vfs_num = num_vfs;
4604 
4605 	ret = pci_enable_sriov(pdev, num_vfs);
4606 	if (ret) {
4607 		pci_err(pdev, "Can't enable VF!\n");
4608 		qm_clear_vft_config(qm);
4609 		goto err_put_sync;
4610 	}
4611 
4612 	pci_info(pdev, "VF enabled, vfs_num(=%d)!\n", num_vfs);
4613 
4614 	return num_vfs;
4615 
4616 err_put_sync:
4617 	qm_pm_put_sync(qm);
4618 	return ret;
4619 }
4620 EXPORT_SYMBOL_GPL(hisi_qm_sriov_enable);
4621 
4622 /**
4623  * hisi_qm_sriov_disable - disable virtual functions
4624  * @pdev: the PCI device.
4625  * @is_frozen: true when all the VFs are frozen.
4626  *
4627  * Return failure if there are VFs assigned already or VF is in used.
4628  */
4629 int hisi_qm_sriov_disable(struct pci_dev *pdev, bool is_frozen)
4630 {
4631 	struct hisi_qm *qm = pci_get_drvdata(pdev);
4632 	int total_vfs = pci_sriov_get_totalvfs(qm->pdev);
4633 	int ret;
4634 
4635 	if (pci_vfs_assigned(pdev)) {
4636 		pci_err(pdev, "Failed to disable VFs as VFs are assigned!\n");
4637 		return -EPERM;
4638 	}
4639 
4640 	/* While VF is in used, SRIOV cannot be disabled. */
4641 	if (!is_frozen && qm_try_frozen_vfs(pdev, qm->qm_list)) {
4642 		pci_err(pdev, "Task is using its VF!\n");
4643 		return -EBUSY;
4644 	}
4645 
4646 	pci_disable_sriov(pdev);
4647 	/* clear vf function shaper configure array */
4648 	memset(qm->factor + 1, 0, sizeof(struct qm_shaper_factor) * total_vfs);
4649 	ret = qm_clear_vft_config(qm);
4650 	if (ret)
4651 		return ret;
4652 
4653 	qm_pm_put_sync(qm);
4654 
4655 	return 0;
4656 }
4657 EXPORT_SYMBOL_GPL(hisi_qm_sriov_disable);
4658 
4659 /**
4660  * hisi_qm_sriov_configure - configure the number of VFs
4661  * @pdev: The PCI device
4662  * @num_vfs: The number of VFs need enabled
4663  *
4664  * Enable SR-IOV according to num_vfs, 0 means disable.
4665  */
4666 int hisi_qm_sriov_configure(struct pci_dev *pdev, int num_vfs)
4667 {
4668 	if (num_vfs == 0)
4669 		return hisi_qm_sriov_disable(pdev, false);
4670 	else
4671 		return hisi_qm_sriov_enable(pdev, num_vfs);
4672 }
4673 EXPORT_SYMBOL_GPL(hisi_qm_sriov_configure);
4674 
4675 static enum acc_err_result qm_dev_err_handle(struct hisi_qm *qm)
4676 {
4677 	u32 err_sts;
4678 
4679 	if (!qm->err_ini->get_dev_hw_err_status) {
4680 		dev_err(&qm->pdev->dev, "Device doesn't support get hw error status!\n");
4681 		return ACC_ERR_NONE;
4682 	}
4683 
4684 	/* get device hardware error status */
4685 	err_sts = qm->err_ini->get_dev_hw_err_status(qm);
4686 	if (err_sts) {
4687 		if (err_sts & qm->err_info.ecc_2bits_mask)
4688 			qm->err_status.is_dev_ecc_mbit = true;
4689 
4690 		if (qm->err_ini->log_dev_hw_err)
4691 			qm->err_ini->log_dev_hw_err(qm, err_sts);
4692 
4693 		/* ce error does not need to be reset */
4694 		if ((err_sts | qm->err_info.dev_ce_mask) ==
4695 		     qm->err_info.dev_ce_mask) {
4696 			if (qm->err_ini->clear_dev_hw_err_status)
4697 				qm->err_ini->clear_dev_hw_err_status(qm,
4698 								err_sts);
4699 
4700 			return ACC_ERR_RECOVERED;
4701 		}
4702 
4703 		return ACC_ERR_NEED_RESET;
4704 	}
4705 
4706 	return ACC_ERR_RECOVERED;
4707 }
4708 
4709 static enum acc_err_result qm_process_dev_error(struct hisi_qm *qm)
4710 {
4711 	enum acc_err_result qm_ret, dev_ret;
4712 
4713 	/* log qm error */
4714 	qm_ret = qm_hw_error_handle(qm);
4715 
4716 	/* log device error */
4717 	dev_ret = qm_dev_err_handle(qm);
4718 
4719 	return (qm_ret == ACC_ERR_NEED_RESET ||
4720 		dev_ret == ACC_ERR_NEED_RESET) ?
4721 		ACC_ERR_NEED_RESET : ACC_ERR_RECOVERED;
4722 }
4723 
4724 /**
4725  * hisi_qm_dev_err_detected() - Get device and qm error status then log it.
4726  * @pdev: The PCI device which need report error.
4727  * @state: The connectivity between CPU and device.
4728  *
4729  * We register this function into PCIe AER handlers, It will report device or
4730  * qm hardware error status when error occur.
4731  */
4732 pci_ers_result_t hisi_qm_dev_err_detected(struct pci_dev *pdev,
4733 					  pci_channel_state_t state)
4734 {
4735 	struct hisi_qm *qm = pci_get_drvdata(pdev);
4736 	enum acc_err_result ret;
4737 
4738 	if (pdev->is_virtfn)
4739 		return PCI_ERS_RESULT_NONE;
4740 
4741 	pci_info(pdev, "PCI error detected, state(=%u)!!\n", state);
4742 	if (state == pci_channel_io_perm_failure)
4743 		return PCI_ERS_RESULT_DISCONNECT;
4744 
4745 	ret = qm_process_dev_error(qm);
4746 	if (ret == ACC_ERR_NEED_RESET)
4747 		return PCI_ERS_RESULT_NEED_RESET;
4748 
4749 	return PCI_ERS_RESULT_RECOVERED;
4750 }
4751 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_detected);
4752 
4753 static int qm_check_req_recv(struct hisi_qm *qm)
4754 {
4755 	struct pci_dev *pdev = qm->pdev;
4756 	int ret;
4757 	u32 val;
4758 
4759 	if (qm->ver >= QM_HW_V3)
4760 		return 0;
4761 
4762 	writel(ACC_VENDOR_ID_VALUE, qm->io_base + QM_PEH_VENDOR_ID);
4763 	ret = readl_relaxed_poll_timeout(qm->io_base + QM_PEH_VENDOR_ID, val,
4764 					 (val == ACC_VENDOR_ID_VALUE),
4765 					 POLL_PERIOD, POLL_TIMEOUT);
4766 	if (ret) {
4767 		dev_err(&pdev->dev, "Fails to read QM reg!\n");
4768 		return ret;
4769 	}
4770 
4771 	writel(PCI_VENDOR_ID_HUAWEI, qm->io_base + QM_PEH_VENDOR_ID);
4772 	ret = readl_relaxed_poll_timeout(qm->io_base + QM_PEH_VENDOR_ID, val,
4773 					 (val == PCI_VENDOR_ID_HUAWEI),
4774 					 POLL_PERIOD, POLL_TIMEOUT);
4775 	if (ret)
4776 		dev_err(&pdev->dev, "Fails to read QM reg in the second time!\n");
4777 
4778 	return ret;
4779 }
4780 
4781 static int qm_set_pf_mse(struct hisi_qm *qm, bool set)
4782 {
4783 	struct pci_dev *pdev = qm->pdev;
4784 	u16 cmd;
4785 	int i;
4786 
4787 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
4788 	if (set)
4789 		cmd |= PCI_COMMAND_MEMORY;
4790 	else
4791 		cmd &= ~PCI_COMMAND_MEMORY;
4792 
4793 	pci_write_config_word(pdev, PCI_COMMAND, cmd);
4794 	for (i = 0; i < MAX_WAIT_COUNTS; i++) {
4795 		pci_read_config_word(pdev, PCI_COMMAND, &cmd);
4796 		if (set == ((cmd & PCI_COMMAND_MEMORY) >> 1))
4797 			return 0;
4798 
4799 		udelay(1);
4800 	}
4801 
4802 	return -ETIMEDOUT;
4803 }
4804 
4805 static int qm_set_vf_mse(struct hisi_qm *qm, bool set)
4806 {
4807 	struct pci_dev *pdev = qm->pdev;
4808 	u16 sriov_ctrl;
4809 	int pos;
4810 	int i;
4811 
4812 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4813 	pci_read_config_word(pdev, pos + PCI_SRIOV_CTRL, &sriov_ctrl);
4814 	if (set)
4815 		sriov_ctrl |= PCI_SRIOV_CTRL_MSE;
4816 	else
4817 		sriov_ctrl &= ~PCI_SRIOV_CTRL_MSE;
4818 	pci_write_config_word(pdev, pos + PCI_SRIOV_CTRL, sriov_ctrl);
4819 
4820 	for (i = 0; i < MAX_WAIT_COUNTS; i++) {
4821 		pci_read_config_word(pdev, pos + PCI_SRIOV_CTRL, &sriov_ctrl);
4822 		if (set == (sriov_ctrl & PCI_SRIOV_CTRL_MSE) >>
4823 		    ACC_PEH_SRIOV_CTRL_VF_MSE_SHIFT)
4824 			return 0;
4825 
4826 		udelay(1);
4827 	}
4828 
4829 	return -ETIMEDOUT;
4830 }
4831 
4832 static int qm_vf_reset_prepare(struct hisi_qm *qm,
4833 			       enum qm_stop_reason stop_reason)
4834 {
4835 	struct hisi_qm_list *qm_list = qm->qm_list;
4836 	struct pci_dev *pdev = qm->pdev;
4837 	struct pci_dev *virtfn;
4838 	struct hisi_qm *vf_qm;
4839 	int ret = 0;
4840 
4841 	mutex_lock(&qm_list->lock);
4842 	list_for_each_entry(vf_qm, &qm_list->list, list) {
4843 		virtfn = vf_qm->pdev;
4844 		if (virtfn == pdev)
4845 			continue;
4846 
4847 		if (pci_physfn(virtfn) == pdev) {
4848 			/* save VFs PCIE BAR configuration */
4849 			pci_save_state(virtfn);
4850 
4851 			ret = hisi_qm_stop(vf_qm, stop_reason);
4852 			if (ret)
4853 				goto stop_fail;
4854 		}
4855 	}
4856 
4857 stop_fail:
4858 	mutex_unlock(&qm_list->lock);
4859 	return ret;
4860 }
4861 
4862 static int qm_try_stop_vfs(struct hisi_qm *qm, u64 cmd,
4863 			   enum qm_stop_reason stop_reason)
4864 {
4865 	struct pci_dev *pdev = qm->pdev;
4866 	int ret;
4867 
4868 	if (!qm->vfs_num)
4869 		return 0;
4870 
4871 	/* Kunpeng930 supports to notify VFs to stop before PF reset */
4872 	if (qm->ops->ping_all_vfs) {
4873 		ret = qm->ops->ping_all_vfs(qm, cmd);
4874 		if (ret)
4875 			pci_err(pdev, "failed to send cmd to all VFs before PF reset!\n");
4876 	} else {
4877 		ret = qm_vf_reset_prepare(qm, stop_reason);
4878 		if (ret)
4879 			pci_err(pdev, "failed to prepare reset, ret = %d.\n", ret);
4880 	}
4881 
4882 	return ret;
4883 }
4884 
4885 static int qm_controller_reset_prepare(struct hisi_qm *qm)
4886 {
4887 	struct pci_dev *pdev = qm->pdev;
4888 	int ret;
4889 
4890 	ret = qm_reset_prepare_ready(qm);
4891 	if (ret) {
4892 		pci_err(pdev, "Controller reset not ready!\n");
4893 		return ret;
4894 	}
4895 
4896 	/* PF obtains the information of VF by querying the register. */
4897 	qm_cmd_uninit(qm);
4898 
4899 	/* Whether VFs stop successfully, soft reset will continue. */
4900 	ret = qm_try_stop_vfs(qm, QM_PF_SRST_PREPARE, QM_SOFT_RESET);
4901 	if (ret)
4902 		pci_err(pdev, "failed to stop vfs by pf in soft reset.\n");
4903 
4904 	ret = hisi_qm_stop(qm, QM_SOFT_RESET);
4905 	if (ret) {
4906 		pci_err(pdev, "Fails to stop QM!\n");
4907 		qm_reset_bit_clear(qm);
4908 		return ret;
4909 	}
4910 
4911 	ret = qm_wait_vf_prepare_finish(qm);
4912 	if (ret)
4913 		pci_err(pdev, "failed to stop by vfs in soft reset!\n");
4914 
4915 	clear_bit(QM_RST_SCHED, &qm->misc_ctl);
4916 
4917 	return 0;
4918 }
4919 
4920 static void qm_dev_ecc_mbit_handle(struct hisi_qm *qm)
4921 {
4922 	u32 nfe_enb = 0;
4923 
4924 	/* Kunpeng930 hardware automatically close master ooo when NFE occurs */
4925 	if (qm->ver >= QM_HW_V3)
4926 		return;
4927 
4928 	if (!qm->err_status.is_dev_ecc_mbit &&
4929 	    qm->err_status.is_qm_ecc_mbit &&
4930 	    qm->err_ini->close_axi_master_ooo) {
4931 
4932 		qm->err_ini->close_axi_master_ooo(qm);
4933 
4934 	} else if (qm->err_status.is_dev_ecc_mbit &&
4935 		   !qm->err_status.is_qm_ecc_mbit &&
4936 		   !qm->err_ini->close_axi_master_ooo) {
4937 
4938 		nfe_enb = readl(qm->io_base + QM_RAS_NFE_ENABLE);
4939 		writel(nfe_enb & QM_RAS_NFE_MBIT_DISABLE,
4940 		       qm->io_base + QM_RAS_NFE_ENABLE);
4941 		writel(QM_ECC_MBIT, qm->io_base + QM_ABNORMAL_INT_SET);
4942 	}
4943 }
4944 
4945 static int qm_soft_reset(struct hisi_qm *qm)
4946 {
4947 	struct pci_dev *pdev = qm->pdev;
4948 	int ret;
4949 	u32 val;
4950 
4951 	/* Ensure all doorbells and mailboxes received by QM */
4952 	ret = qm_check_req_recv(qm);
4953 	if (ret)
4954 		return ret;
4955 
4956 	if (qm->vfs_num) {
4957 		ret = qm_set_vf_mse(qm, false);
4958 		if (ret) {
4959 			pci_err(pdev, "Fails to disable vf MSE bit.\n");
4960 			return ret;
4961 		}
4962 	}
4963 
4964 	ret = qm->ops->set_msi(qm, false);
4965 	if (ret) {
4966 		pci_err(pdev, "Fails to disable PEH MSI bit.\n");
4967 		return ret;
4968 	}
4969 
4970 	qm_dev_ecc_mbit_handle(qm);
4971 
4972 	/* OOO register set and check */
4973 	writel(ACC_MASTER_GLOBAL_CTRL_SHUTDOWN,
4974 	       qm->io_base + ACC_MASTER_GLOBAL_CTRL);
4975 
4976 	/* If bus lock, reset chip */
4977 	ret = readl_relaxed_poll_timeout(qm->io_base + ACC_MASTER_TRANS_RETURN,
4978 					 val,
4979 					 (val == ACC_MASTER_TRANS_RETURN_RW),
4980 					 POLL_PERIOD, POLL_TIMEOUT);
4981 	if (ret) {
4982 		pci_emerg(pdev, "Bus lock! Please reset system.\n");
4983 		return ret;
4984 	}
4985 
4986 	if (qm->err_ini->close_sva_prefetch)
4987 		qm->err_ini->close_sva_prefetch(qm);
4988 
4989 	ret = qm_set_pf_mse(qm, false);
4990 	if (ret) {
4991 		pci_err(pdev, "Fails to disable pf MSE bit.\n");
4992 		return ret;
4993 	}
4994 
4995 	/* The reset related sub-control registers are not in PCI BAR */
4996 	if (ACPI_HANDLE(&pdev->dev)) {
4997 		unsigned long long value = 0;
4998 		acpi_status s;
4999 
5000 		s = acpi_evaluate_integer(ACPI_HANDLE(&pdev->dev),
5001 					  qm->err_info.acpi_rst,
5002 					  NULL, &value);
5003 		if (ACPI_FAILURE(s)) {
5004 			pci_err(pdev, "NO controller reset method!\n");
5005 			return -EIO;
5006 		}
5007 
5008 		if (value) {
5009 			pci_err(pdev, "Reset step %llu failed!\n", value);
5010 			return -EIO;
5011 		}
5012 	} else {
5013 		pci_err(pdev, "No reset method!\n");
5014 		return -EINVAL;
5015 	}
5016 
5017 	return 0;
5018 }
5019 
5020 static int qm_vf_reset_done(struct hisi_qm *qm)
5021 {
5022 	struct hisi_qm_list *qm_list = qm->qm_list;
5023 	struct pci_dev *pdev = qm->pdev;
5024 	struct pci_dev *virtfn;
5025 	struct hisi_qm *vf_qm;
5026 	int ret = 0;
5027 
5028 	mutex_lock(&qm_list->lock);
5029 	list_for_each_entry(vf_qm, &qm_list->list, list) {
5030 		virtfn = vf_qm->pdev;
5031 		if (virtfn == pdev)
5032 			continue;
5033 
5034 		if (pci_physfn(virtfn) == pdev) {
5035 			/* enable VFs PCIE BAR configuration */
5036 			pci_restore_state(virtfn);
5037 
5038 			ret = qm_restart(vf_qm);
5039 			if (ret)
5040 				goto restart_fail;
5041 		}
5042 	}
5043 
5044 restart_fail:
5045 	mutex_unlock(&qm_list->lock);
5046 	return ret;
5047 }
5048 
5049 static int qm_try_start_vfs(struct hisi_qm *qm, enum qm_mb_cmd cmd)
5050 {
5051 	struct pci_dev *pdev = qm->pdev;
5052 	int ret;
5053 
5054 	if (!qm->vfs_num)
5055 		return 0;
5056 
5057 	ret = qm_vf_q_assign(qm, qm->vfs_num);
5058 	if (ret) {
5059 		pci_err(pdev, "failed to assign VFs, ret = %d.\n", ret);
5060 		return ret;
5061 	}
5062 
5063 	/* Kunpeng930 supports to notify VFs to start after PF reset. */
5064 	if (qm->ops->ping_all_vfs) {
5065 		ret = qm->ops->ping_all_vfs(qm, cmd);
5066 		if (ret)
5067 			pci_warn(pdev, "failed to send cmd to all VFs after PF reset!\n");
5068 	} else {
5069 		ret = qm_vf_reset_done(qm);
5070 		if (ret)
5071 			pci_warn(pdev, "failed to start vfs, ret = %d.\n", ret);
5072 	}
5073 
5074 	return ret;
5075 }
5076 
5077 static int qm_dev_hw_init(struct hisi_qm *qm)
5078 {
5079 	return qm->err_ini->hw_init(qm);
5080 }
5081 
5082 static void qm_restart_prepare(struct hisi_qm *qm)
5083 {
5084 	u32 value;
5085 
5086 	if (qm->err_ini->open_sva_prefetch)
5087 		qm->err_ini->open_sva_prefetch(qm);
5088 
5089 	if (qm->ver >= QM_HW_V3)
5090 		return;
5091 
5092 	if (!qm->err_status.is_qm_ecc_mbit &&
5093 	    !qm->err_status.is_dev_ecc_mbit)
5094 		return;
5095 
5096 	/* temporarily close the OOO port used for PEH to write out MSI */
5097 	value = readl(qm->io_base + ACC_AM_CFG_PORT_WR_EN);
5098 	writel(value & ~qm->err_info.msi_wr_port,
5099 	       qm->io_base + ACC_AM_CFG_PORT_WR_EN);
5100 
5101 	/* clear dev ecc 2bit error source if having */
5102 	value = qm_get_dev_err_status(qm) & qm->err_info.ecc_2bits_mask;
5103 	if (value && qm->err_ini->clear_dev_hw_err_status)
5104 		qm->err_ini->clear_dev_hw_err_status(qm, value);
5105 
5106 	/* clear QM ecc mbit error source */
5107 	writel(QM_ECC_MBIT, qm->io_base + QM_ABNORMAL_INT_SOURCE);
5108 
5109 	/* clear AM Reorder Buffer ecc mbit source */
5110 	writel(ACC_ROB_ECC_ERR_MULTPL, qm->io_base + ACC_AM_ROB_ECC_INT_STS);
5111 }
5112 
5113 static void qm_restart_done(struct hisi_qm *qm)
5114 {
5115 	u32 value;
5116 
5117 	if (qm->ver >= QM_HW_V3)
5118 		goto clear_flags;
5119 
5120 	if (!qm->err_status.is_qm_ecc_mbit &&
5121 	    !qm->err_status.is_dev_ecc_mbit)
5122 		return;
5123 
5124 	/* open the OOO port for PEH to write out MSI */
5125 	value = readl(qm->io_base + ACC_AM_CFG_PORT_WR_EN);
5126 	value |= qm->err_info.msi_wr_port;
5127 	writel(value, qm->io_base + ACC_AM_CFG_PORT_WR_EN);
5128 
5129 clear_flags:
5130 	qm->err_status.is_qm_ecc_mbit = false;
5131 	qm->err_status.is_dev_ecc_mbit = false;
5132 }
5133 
5134 static int qm_controller_reset_done(struct hisi_qm *qm)
5135 {
5136 	struct pci_dev *pdev = qm->pdev;
5137 	int ret;
5138 
5139 	ret = qm->ops->set_msi(qm, true);
5140 	if (ret) {
5141 		pci_err(pdev, "Fails to enable PEH MSI bit!\n");
5142 		return ret;
5143 	}
5144 
5145 	ret = qm_set_pf_mse(qm, true);
5146 	if (ret) {
5147 		pci_err(pdev, "Fails to enable pf MSE bit!\n");
5148 		return ret;
5149 	}
5150 
5151 	if (qm->vfs_num) {
5152 		ret = qm_set_vf_mse(qm, true);
5153 		if (ret) {
5154 			pci_err(pdev, "Fails to enable vf MSE bit!\n");
5155 			return ret;
5156 		}
5157 	}
5158 
5159 	ret = qm_dev_hw_init(qm);
5160 	if (ret) {
5161 		pci_err(pdev, "Failed to init device\n");
5162 		return ret;
5163 	}
5164 
5165 	qm_restart_prepare(qm);
5166 	hisi_qm_dev_err_init(qm);
5167 	if (qm->err_ini->open_axi_master_ooo)
5168 		qm->err_ini->open_axi_master_ooo(qm);
5169 
5170 	ret = qm_dev_mem_reset(qm);
5171 	if (ret) {
5172 		pci_err(pdev, "failed to reset device memory\n");
5173 		return ret;
5174 	}
5175 
5176 	ret = qm_restart(qm);
5177 	if (ret) {
5178 		pci_err(pdev, "Failed to start QM!\n");
5179 		return ret;
5180 	}
5181 
5182 	ret = qm_try_start_vfs(qm, QM_PF_RESET_DONE);
5183 	if (ret)
5184 		pci_err(pdev, "failed to start vfs by pf in soft reset.\n");
5185 
5186 	ret = qm_wait_vf_prepare_finish(qm);
5187 	if (ret)
5188 		pci_err(pdev, "failed to start by vfs in soft reset!\n");
5189 
5190 	qm_cmd_init(qm);
5191 	qm_restart_done(qm);
5192 
5193 	qm_reset_bit_clear(qm);
5194 
5195 	return 0;
5196 }
5197 
5198 static int qm_controller_reset(struct hisi_qm *qm)
5199 {
5200 	struct pci_dev *pdev = qm->pdev;
5201 	int ret;
5202 
5203 	pci_info(pdev, "Controller resetting...\n");
5204 
5205 	ret = qm_controller_reset_prepare(qm);
5206 	if (ret) {
5207 		hisi_qm_set_hw_reset(qm, QM_RESET_STOP_TX_OFFSET);
5208 		hisi_qm_set_hw_reset(qm, QM_RESET_STOP_RX_OFFSET);
5209 		clear_bit(QM_RST_SCHED, &qm->misc_ctl);
5210 		return ret;
5211 	}
5212 
5213 	ret = qm_soft_reset(qm);
5214 	if (ret) {
5215 		pci_err(pdev, "Controller reset failed (%d)\n", ret);
5216 		qm_reset_bit_clear(qm);
5217 		return ret;
5218 	}
5219 
5220 	ret = qm_controller_reset_done(qm);
5221 	if (ret) {
5222 		qm_reset_bit_clear(qm);
5223 		return ret;
5224 	}
5225 
5226 	pci_info(pdev, "Controller reset complete\n");
5227 
5228 	return 0;
5229 }
5230 
5231 /**
5232  * hisi_qm_dev_slot_reset() - slot reset
5233  * @pdev: the PCIe device
5234  *
5235  * This function offers QM relate PCIe device reset interface. Drivers which
5236  * use QM can use this function as slot_reset in its struct pci_error_handlers.
5237  */
5238 pci_ers_result_t hisi_qm_dev_slot_reset(struct pci_dev *pdev)
5239 {
5240 	struct hisi_qm *qm = pci_get_drvdata(pdev);
5241 	int ret;
5242 
5243 	if (pdev->is_virtfn)
5244 		return PCI_ERS_RESULT_RECOVERED;
5245 
5246 	pci_aer_clear_nonfatal_status(pdev);
5247 
5248 	/* reset pcie device controller */
5249 	ret = qm_controller_reset(qm);
5250 	if (ret) {
5251 		pci_err(pdev, "Controller reset failed (%d)\n", ret);
5252 		return PCI_ERS_RESULT_DISCONNECT;
5253 	}
5254 
5255 	return PCI_ERS_RESULT_RECOVERED;
5256 }
5257 EXPORT_SYMBOL_GPL(hisi_qm_dev_slot_reset);
5258 
5259 void hisi_qm_reset_prepare(struct pci_dev *pdev)
5260 {
5261 	struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(pdev));
5262 	struct hisi_qm *qm = pci_get_drvdata(pdev);
5263 	u32 delay = 0;
5264 	int ret;
5265 
5266 	hisi_qm_dev_err_uninit(pf_qm);
5267 
5268 	/*
5269 	 * Check whether there is an ECC mbit error, If it occurs, need to
5270 	 * wait for soft reset to fix it.
5271 	 */
5272 	while (qm_check_dev_error(pf_qm)) {
5273 		msleep(++delay);
5274 		if (delay > QM_RESET_WAIT_TIMEOUT)
5275 			return;
5276 	}
5277 
5278 	ret = qm_reset_prepare_ready(qm);
5279 	if (ret) {
5280 		pci_err(pdev, "FLR not ready!\n");
5281 		return;
5282 	}
5283 
5284 	/* PF obtains the information of VF by querying the register. */
5285 	if (qm->fun_type == QM_HW_PF)
5286 		qm_cmd_uninit(qm);
5287 
5288 	ret = qm_try_stop_vfs(qm, QM_PF_FLR_PREPARE, QM_FLR);
5289 	if (ret)
5290 		pci_err(pdev, "failed to stop vfs by pf in FLR.\n");
5291 
5292 	ret = hisi_qm_stop(qm, QM_FLR);
5293 	if (ret) {
5294 		pci_err(pdev, "Failed to stop QM, ret = %d.\n", ret);
5295 		hisi_qm_set_hw_reset(qm, QM_RESET_STOP_TX_OFFSET);
5296 		hisi_qm_set_hw_reset(qm, QM_RESET_STOP_RX_OFFSET);
5297 		return;
5298 	}
5299 
5300 	ret = qm_wait_vf_prepare_finish(qm);
5301 	if (ret)
5302 		pci_err(pdev, "failed to stop by vfs in FLR!\n");
5303 
5304 	pci_info(pdev, "FLR resetting...\n");
5305 }
5306 EXPORT_SYMBOL_GPL(hisi_qm_reset_prepare);
5307 
5308 static bool qm_flr_reset_complete(struct pci_dev *pdev)
5309 {
5310 	struct pci_dev *pf_pdev = pci_physfn(pdev);
5311 	struct hisi_qm *qm = pci_get_drvdata(pf_pdev);
5312 	u32 id;
5313 
5314 	pci_read_config_dword(qm->pdev, PCI_COMMAND, &id);
5315 	if (id == QM_PCI_COMMAND_INVALID) {
5316 		pci_err(pdev, "Device can not be used!\n");
5317 		return false;
5318 	}
5319 
5320 	return true;
5321 }
5322 
5323 void hisi_qm_reset_done(struct pci_dev *pdev)
5324 {
5325 	struct hisi_qm *pf_qm = pci_get_drvdata(pci_physfn(pdev));
5326 	struct hisi_qm *qm = pci_get_drvdata(pdev);
5327 	int ret;
5328 
5329 	if (qm->fun_type == QM_HW_PF) {
5330 		ret = qm_dev_hw_init(qm);
5331 		if (ret) {
5332 			pci_err(pdev, "Failed to init PF, ret = %d.\n", ret);
5333 			goto flr_done;
5334 		}
5335 	}
5336 
5337 	hisi_qm_dev_err_init(pf_qm);
5338 
5339 	ret = qm_restart(qm);
5340 	if (ret) {
5341 		pci_err(pdev, "Failed to start QM, ret = %d.\n", ret);
5342 		goto flr_done;
5343 	}
5344 
5345 	ret = qm_try_start_vfs(qm, QM_PF_RESET_DONE);
5346 	if (ret)
5347 		pci_err(pdev, "failed to start vfs by pf in FLR.\n");
5348 
5349 	ret = qm_wait_vf_prepare_finish(qm);
5350 	if (ret)
5351 		pci_err(pdev, "failed to start by vfs in FLR!\n");
5352 
5353 flr_done:
5354 	if (qm->fun_type == QM_HW_PF)
5355 		qm_cmd_init(qm);
5356 
5357 	if (qm_flr_reset_complete(pdev))
5358 		pci_info(pdev, "FLR reset complete\n");
5359 
5360 	qm_reset_bit_clear(qm);
5361 }
5362 EXPORT_SYMBOL_GPL(hisi_qm_reset_done);
5363 
5364 static irqreturn_t qm_abnormal_irq(int irq, void *data)
5365 {
5366 	struct hisi_qm *qm = data;
5367 	enum acc_err_result ret;
5368 
5369 	atomic64_inc(&qm->debug.dfx.abnormal_irq_cnt);
5370 	ret = qm_process_dev_error(qm);
5371 	if (ret == ACC_ERR_NEED_RESET &&
5372 	    !test_bit(QM_DRIVER_REMOVING, &qm->misc_ctl) &&
5373 	    !test_and_set_bit(QM_RST_SCHED, &qm->misc_ctl))
5374 		schedule_work(&qm->rst_work);
5375 
5376 	return IRQ_HANDLED;
5377 }
5378 
5379 static int qm_irq_register(struct hisi_qm *qm)
5380 {
5381 	struct pci_dev *pdev = qm->pdev;
5382 	int ret;
5383 
5384 	ret = request_irq(pci_irq_vector(pdev, QM_EQ_EVENT_IRQ_VECTOR),
5385 			  qm_irq, 0, qm->dev_name, qm);
5386 	if (ret)
5387 		return ret;
5388 
5389 	if (qm->ver > QM_HW_V1) {
5390 		ret = request_threaded_irq(pci_irq_vector(pdev,
5391 					   QM_AEQ_EVENT_IRQ_VECTOR),
5392 					   qm_aeq_irq, qm_aeq_thread,
5393 					   0, qm->dev_name, qm);
5394 		if (ret)
5395 			goto err_aeq_irq;
5396 
5397 		if (qm->fun_type == QM_HW_PF) {
5398 			ret = request_irq(pci_irq_vector(pdev,
5399 					  QM_ABNORMAL_EVENT_IRQ_VECTOR),
5400 					  qm_abnormal_irq, 0, qm->dev_name, qm);
5401 			if (ret)
5402 				goto err_abonormal_irq;
5403 		}
5404 	}
5405 
5406 	if (qm->ver > QM_HW_V2) {
5407 		ret = request_irq(pci_irq_vector(pdev, QM_CMD_EVENT_IRQ_VECTOR),
5408 				qm_mb_cmd_irq, 0, qm->dev_name, qm);
5409 		if (ret)
5410 			goto err_mb_cmd_irq;
5411 	}
5412 
5413 	return 0;
5414 
5415 err_mb_cmd_irq:
5416 	if (qm->fun_type == QM_HW_PF)
5417 		free_irq(pci_irq_vector(pdev, QM_ABNORMAL_EVENT_IRQ_VECTOR), qm);
5418 err_abonormal_irq:
5419 	free_irq(pci_irq_vector(pdev, QM_AEQ_EVENT_IRQ_VECTOR), qm);
5420 err_aeq_irq:
5421 	free_irq(pci_irq_vector(pdev, QM_EQ_EVENT_IRQ_VECTOR), qm);
5422 	return ret;
5423 }
5424 
5425 /**
5426  * hisi_qm_dev_shutdown() - Shutdown device.
5427  * @pdev: The device will be shutdown.
5428  *
5429  * This function will stop qm when OS shutdown or rebooting.
5430  */
5431 void hisi_qm_dev_shutdown(struct pci_dev *pdev)
5432 {
5433 	struct hisi_qm *qm = pci_get_drvdata(pdev);
5434 	int ret;
5435 
5436 	ret = hisi_qm_stop(qm, QM_NORMAL);
5437 	if (ret)
5438 		dev_err(&pdev->dev, "Fail to stop qm in shutdown!\n");
5439 }
5440 EXPORT_SYMBOL_GPL(hisi_qm_dev_shutdown);
5441 
5442 static void hisi_qm_controller_reset(struct work_struct *rst_work)
5443 {
5444 	struct hisi_qm *qm = container_of(rst_work, struct hisi_qm, rst_work);
5445 	int ret;
5446 
5447 	ret = qm_pm_get_sync(qm);
5448 	if (ret) {
5449 		clear_bit(QM_RST_SCHED, &qm->misc_ctl);
5450 		return;
5451 	}
5452 
5453 	/* reset pcie device controller */
5454 	ret = qm_controller_reset(qm);
5455 	if (ret)
5456 		dev_err(&qm->pdev->dev, "controller reset failed (%d)\n", ret);
5457 
5458 	qm_pm_put_sync(qm);
5459 }
5460 
5461 static void qm_pf_reset_vf_prepare(struct hisi_qm *qm,
5462 				   enum qm_stop_reason stop_reason)
5463 {
5464 	enum qm_mb_cmd cmd = QM_VF_PREPARE_DONE;
5465 	struct pci_dev *pdev = qm->pdev;
5466 	int ret;
5467 
5468 	ret = qm_reset_prepare_ready(qm);
5469 	if (ret) {
5470 		dev_err(&pdev->dev, "reset prepare not ready!\n");
5471 		atomic_set(&qm->status.flags, QM_STOP);
5472 		cmd = QM_VF_PREPARE_FAIL;
5473 		goto err_prepare;
5474 	}
5475 
5476 	ret = hisi_qm_stop(qm, stop_reason);
5477 	if (ret) {
5478 		dev_err(&pdev->dev, "failed to stop QM, ret = %d.\n", ret);
5479 		atomic_set(&qm->status.flags, QM_STOP);
5480 		cmd = QM_VF_PREPARE_FAIL;
5481 		goto err_prepare;
5482 	} else {
5483 		goto out;
5484 	}
5485 
5486 err_prepare:
5487 	hisi_qm_set_hw_reset(qm, QM_RESET_STOP_TX_OFFSET);
5488 	hisi_qm_set_hw_reset(qm, QM_RESET_STOP_RX_OFFSET);
5489 out:
5490 	pci_save_state(pdev);
5491 	ret = qm->ops->ping_pf(qm, cmd);
5492 	if (ret)
5493 		dev_warn(&pdev->dev, "PF responds timeout in reset prepare!\n");
5494 }
5495 
5496 static void qm_pf_reset_vf_done(struct hisi_qm *qm)
5497 {
5498 	enum qm_mb_cmd cmd = QM_VF_START_DONE;
5499 	struct pci_dev *pdev = qm->pdev;
5500 	int ret;
5501 
5502 	pci_restore_state(pdev);
5503 	ret = hisi_qm_start(qm);
5504 	if (ret) {
5505 		dev_err(&pdev->dev, "failed to start QM, ret = %d.\n", ret);
5506 		cmd = QM_VF_START_FAIL;
5507 	}
5508 
5509 	ret = qm->ops->ping_pf(qm, cmd);
5510 	if (ret)
5511 		dev_warn(&pdev->dev, "PF responds timeout in reset done!\n");
5512 
5513 	qm_reset_bit_clear(qm);
5514 }
5515 
5516 static int qm_wait_pf_reset_finish(struct hisi_qm *qm)
5517 {
5518 	struct device *dev = &qm->pdev->dev;
5519 	u32 val, cmd;
5520 	u64 msg;
5521 	int ret;
5522 
5523 	/* Wait for reset to finish */
5524 	ret = readl_relaxed_poll_timeout(qm->io_base + QM_IFC_INT_SOURCE_V, val,
5525 					 val == BIT(0), QM_VF_RESET_WAIT_US,
5526 					 QM_VF_RESET_WAIT_TIMEOUT_US);
5527 	/* hardware completion status should be available by this time */
5528 	if (ret) {
5529 		dev_err(dev, "couldn't get reset done status from PF, timeout!\n");
5530 		return -ETIMEDOUT;
5531 	}
5532 
5533 	/*
5534 	 * Whether message is got successfully,
5535 	 * VF needs to ack PF by clearing the interrupt.
5536 	 */
5537 	ret = qm_get_mb_cmd(qm, &msg, 0);
5538 	qm_clear_cmd_interrupt(qm, 0);
5539 	if (ret) {
5540 		dev_err(dev, "failed to get msg from PF in reset done!\n");
5541 		return ret;
5542 	}
5543 
5544 	cmd = msg & QM_MB_CMD_DATA_MASK;
5545 	if (cmd != QM_PF_RESET_DONE) {
5546 		dev_err(dev, "the cmd(%u) is not reset done!\n", cmd);
5547 		ret = -EINVAL;
5548 	}
5549 
5550 	return ret;
5551 }
5552 
5553 static void qm_pf_reset_vf_process(struct hisi_qm *qm,
5554 				   enum qm_stop_reason stop_reason)
5555 {
5556 	struct device *dev = &qm->pdev->dev;
5557 	int ret;
5558 
5559 	dev_info(dev, "device reset start...\n");
5560 
5561 	/* The message is obtained by querying the register during resetting */
5562 	qm_cmd_uninit(qm);
5563 	qm_pf_reset_vf_prepare(qm, stop_reason);
5564 
5565 	ret = qm_wait_pf_reset_finish(qm);
5566 	if (ret)
5567 		goto err_get_status;
5568 
5569 	qm_pf_reset_vf_done(qm);
5570 	qm_cmd_init(qm);
5571 
5572 	dev_info(dev, "device reset done.\n");
5573 
5574 	return;
5575 
5576 err_get_status:
5577 	qm_cmd_init(qm);
5578 	qm_reset_bit_clear(qm);
5579 }
5580 
5581 static void qm_handle_cmd_msg(struct hisi_qm *qm, u32 fun_num)
5582 {
5583 	struct device *dev = &qm->pdev->dev;
5584 	u64 msg;
5585 	u32 cmd;
5586 	int ret;
5587 
5588 	/*
5589 	 * Get the msg from source by sending mailbox. Whether message is got
5590 	 * successfully, destination needs to ack source by clearing the interrupt.
5591 	 */
5592 	ret = qm_get_mb_cmd(qm, &msg, fun_num);
5593 	qm_clear_cmd_interrupt(qm, BIT(fun_num));
5594 	if (ret) {
5595 		dev_err(dev, "failed to get msg from source!\n");
5596 		return;
5597 	}
5598 
5599 	cmd = msg & QM_MB_CMD_DATA_MASK;
5600 	switch (cmd) {
5601 	case QM_PF_FLR_PREPARE:
5602 		qm_pf_reset_vf_process(qm, QM_FLR);
5603 		break;
5604 	case QM_PF_SRST_PREPARE:
5605 		qm_pf_reset_vf_process(qm, QM_SOFT_RESET);
5606 		break;
5607 	case QM_VF_GET_QOS:
5608 		qm_vf_get_qos(qm, fun_num);
5609 		break;
5610 	case QM_PF_SET_QOS:
5611 		qm->mb_qos = msg >> QM_MB_CMD_DATA_SHIFT;
5612 		break;
5613 	default:
5614 		dev_err(dev, "unsupported cmd %u sent by function(%u)!\n", cmd, fun_num);
5615 		break;
5616 	}
5617 }
5618 
5619 static void qm_cmd_process(struct work_struct *cmd_process)
5620 {
5621 	struct hisi_qm *qm = container_of(cmd_process,
5622 					struct hisi_qm, cmd_process);
5623 	u32 vfs_num = qm->vfs_num;
5624 	u64 val;
5625 	u32 i;
5626 
5627 	if (qm->fun_type == QM_HW_PF) {
5628 		val = readq(qm->io_base + QM_IFC_INT_SOURCE_P);
5629 		if (!val)
5630 			return;
5631 
5632 		for (i = 1; i <= vfs_num; i++) {
5633 			if (val & BIT(i))
5634 				qm_handle_cmd_msg(qm, i);
5635 		}
5636 
5637 		return;
5638 	}
5639 
5640 	qm_handle_cmd_msg(qm, 0);
5641 }
5642 
5643 /**
5644  * hisi_qm_alg_register() - Register alg to crypto and add qm to qm_list.
5645  * @qm: The qm needs add.
5646  * @qm_list: The qm list.
5647  *
5648  * This function adds qm to qm list, and will register algorithm to
5649  * crypto when the qm list is empty.
5650  */
5651 int hisi_qm_alg_register(struct hisi_qm *qm, struct hisi_qm_list *qm_list)
5652 {
5653 	struct device *dev = &qm->pdev->dev;
5654 	int flag = 0;
5655 	int ret = 0;
5656 
5657 	mutex_lock(&qm_list->lock);
5658 	if (list_empty(&qm_list->list))
5659 		flag = 1;
5660 	list_add_tail(&qm->list, &qm_list->list);
5661 	mutex_unlock(&qm_list->lock);
5662 
5663 	if (qm->ver <= QM_HW_V2 && qm->use_sva) {
5664 		dev_info(dev, "HW V2 not both use uacce sva mode and hardware crypto algs.\n");
5665 		return 0;
5666 	}
5667 
5668 	if (flag) {
5669 		ret = qm_list->register_to_crypto(qm);
5670 		if (ret) {
5671 			mutex_lock(&qm_list->lock);
5672 			list_del(&qm->list);
5673 			mutex_unlock(&qm_list->lock);
5674 		}
5675 	}
5676 
5677 	return ret;
5678 }
5679 EXPORT_SYMBOL_GPL(hisi_qm_alg_register);
5680 
5681 /**
5682  * hisi_qm_alg_unregister() - Unregister alg from crypto and delete qm from
5683  * qm list.
5684  * @qm: The qm needs delete.
5685  * @qm_list: The qm list.
5686  *
5687  * This function deletes qm from qm list, and will unregister algorithm
5688  * from crypto when the qm list is empty.
5689  */
5690 void hisi_qm_alg_unregister(struct hisi_qm *qm, struct hisi_qm_list *qm_list)
5691 {
5692 	mutex_lock(&qm_list->lock);
5693 	list_del(&qm->list);
5694 	mutex_unlock(&qm_list->lock);
5695 
5696 	if (qm->ver <= QM_HW_V2 && qm->use_sva)
5697 		return;
5698 
5699 	if (list_empty(&qm_list->list))
5700 		qm_list->unregister_from_crypto(qm);
5701 }
5702 EXPORT_SYMBOL_GPL(hisi_qm_alg_unregister);
5703 
5704 static int qm_get_qp_num(struct hisi_qm *qm)
5705 {
5706 	if (qm->ver == QM_HW_V1)
5707 		qm->ctrl_qp_num = QM_QNUM_V1;
5708 	else if (qm->ver == QM_HW_V2)
5709 		qm->ctrl_qp_num = QM_QNUM_V2;
5710 	else
5711 		qm->ctrl_qp_num = readl(qm->io_base + QM_CAPBILITY) &
5712 					QM_QP_NUN_MASK;
5713 
5714 	if (qm->use_db_isolation)
5715 		qm->max_qp_num = (readl(qm->io_base + QM_CAPBILITY) >>
5716 				  QM_QP_MAX_NUM_SHIFT) & QM_QP_NUN_MASK;
5717 	else
5718 		qm->max_qp_num = qm->ctrl_qp_num;
5719 
5720 	/* check if qp number is valid */
5721 	if (qm->qp_num > qm->max_qp_num) {
5722 		dev_err(&qm->pdev->dev, "qp num(%u) is more than max qp num(%u)!\n",
5723 			qm->qp_num, qm->max_qp_num);
5724 		return -EINVAL;
5725 	}
5726 
5727 	return 0;
5728 }
5729 
5730 static int qm_get_pci_res(struct hisi_qm *qm)
5731 {
5732 	struct pci_dev *pdev = qm->pdev;
5733 	struct device *dev = &pdev->dev;
5734 	int ret;
5735 
5736 	ret = pci_request_mem_regions(pdev, qm->dev_name);
5737 	if (ret < 0) {
5738 		dev_err(dev, "Failed to request mem regions!\n");
5739 		return ret;
5740 	}
5741 
5742 	qm->phys_base = pci_resource_start(pdev, PCI_BAR_2);
5743 	qm->io_base = ioremap(qm->phys_base, pci_resource_len(pdev, PCI_BAR_2));
5744 	if (!qm->io_base) {
5745 		ret = -EIO;
5746 		goto err_request_mem_regions;
5747 	}
5748 
5749 	if (qm->ver > QM_HW_V2) {
5750 		if (qm->fun_type == QM_HW_PF)
5751 			qm->use_db_isolation = readl(qm->io_base +
5752 						     QM_QUE_ISO_EN) & BIT(0);
5753 		else
5754 			qm->use_db_isolation = readl(qm->io_base +
5755 						     QM_QUE_ISO_CFG_V) & BIT(0);
5756 	}
5757 
5758 	if (qm->use_db_isolation) {
5759 		qm->db_interval = QM_QP_DB_INTERVAL;
5760 		qm->db_phys_base = pci_resource_start(pdev, PCI_BAR_4);
5761 		qm->db_io_base = ioremap(qm->db_phys_base,
5762 					 pci_resource_len(pdev, PCI_BAR_4));
5763 		if (!qm->db_io_base) {
5764 			ret = -EIO;
5765 			goto err_ioremap;
5766 		}
5767 	} else {
5768 		qm->db_phys_base = qm->phys_base;
5769 		qm->db_io_base = qm->io_base;
5770 		qm->db_interval = 0;
5771 	}
5772 
5773 	if (qm->fun_type == QM_HW_PF) {
5774 		ret = qm_get_qp_num(qm);
5775 		if (ret)
5776 			goto err_db_ioremap;
5777 	}
5778 
5779 	return 0;
5780 
5781 err_db_ioremap:
5782 	if (qm->use_db_isolation)
5783 		iounmap(qm->db_io_base);
5784 err_ioremap:
5785 	iounmap(qm->io_base);
5786 err_request_mem_regions:
5787 	pci_release_mem_regions(pdev);
5788 	return ret;
5789 }
5790 
5791 static int hisi_qm_pci_init(struct hisi_qm *qm)
5792 {
5793 	struct pci_dev *pdev = qm->pdev;
5794 	struct device *dev = &pdev->dev;
5795 	unsigned int num_vec;
5796 	int ret;
5797 
5798 	ret = pci_enable_device_mem(pdev);
5799 	if (ret < 0) {
5800 		dev_err(dev, "Failed to enable device mem!\n");
5801 		return ret;
5802 	}
5803 
5804 	ret = qm_get_pci_res(qm);
5805 	if (ret)
5806 		goto err_disable_pcidev;
5807 
5808 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
5809 	if (ret < 0)
5810 		goto err_get_pci_res;
5811 	pci_set_master(pdev);
5812 
5813 	if (!qm->ops->get_irq_num) {
5814 		ret = -EOPNOTSUPP;
5815 		goto err_get_pci_res;
5816 	}
5817 	num_vec = qm->ops->get_irq_num(qm);
5818 	ret = pci_alloc_irq_vectors(pdev, num_vec, num_vec, PCI_IRQ_MSI);
5819 	if (ret < 0) {
5820 		dev_err(dev, "Failed to enable MSI vectors!\n");
5821 		goto err_get_pci_res;
5822 	}
5823 
5824 	return 0;
5825 
5826 err_get_pci_res:
5827 	qm_put_pci_res(qm);
5828 err_disable_pcidev:
5829 	pci_disable_device(pdev);
5830 	return ret;
5831 }
5832 
5833 static void hisi_qm_init_work(struct hisi_qm *qm)
5834 {
5835 	INIT_WORK(&qm->work, qm_work_process);
5836 	if (qm->fun_type == QM_HW_PF)
5837 		INIT_WORK(&qm->rst_work, hisi_qm_controller_reset);
5838 
5839 	if (qm->ver > QM_HW_V2)
5840 		INIT_WORK(&qm->cmd_process, qm_cmd_process);
5841 }
5842 
5843 static int hisi_qp_alloc_memory(struct hisi_qm *qm)
5844 {
5845 	struct device *dev = &qm->pdev->dev;
5846 	size_t qp_dma_size;
5847 	int i, ret;
5848 
5849 	qm->qp_array = kcalloc(qm->qp_num, sizeof(struct hisi_qp), GFP_KERNEL);
5850 	if (!qm->qp_array)
5851 		return -ENOMEM;
5852 
5853 	/* one more page for device or qp statuses */
5854 	qp_dma_size = qm->sqe_size * QM_Q_DEPTH +
5855 		      sizeof(struct qm_cqe) * QM_Q_DEPTH;
5856 	qp_dma_size = PAGE_ALIGN(qp_dma_size) + PAGE_SIZE;
5857 	for (i = 0; i < qm->qp_num; i++) {
5858 		ret = hisi_qp_memory_init(qm, qp_dma_size, i);
5859 		if (ret)
5860 			goto err_init_qp_mem;
5861 
5862 		dev_dbg(dev, "allocate qp dma buf size=%zx)\n", qp_dma_size);
5863 	}
5864 
5865 	return 0;
5866 err_init_qp_mem:
5867 	hisi_qp_memory_uninit(qm, i);
5868 
5869 	return ret;
5870 }
5871 
5872 static int hisi_qm_memory_init(struct hisi_qm *qm)
5873 {
5874 	struct device *dev = &qm->pdev->dev;
5875 	int ret, total_func, i;
5876 	size_t off = 0;
5877 
5878 	total_func = pci_sriov_get_totalvfs(qm->pdev) + 1;
5879 	qm->factor = kcalloc(total_func, sizeof(struct qm_shaper_factor), GFP_KERNEL);
5880 	if (!qm->factor)
5881 		return -ENOMEM;
5882 	for (i = 0; i < total_func; i++)
5883 		qm->factor[i].func_qos = QM_QOS_MAX_VAL;
5884 
5885 #define QM_INIT_BUF(qm, type, num) do { \
5886 	(qm)->type = ((qm)->qdma.va + (off)); \
5887 	(qm)->type##_dma = (qm)->qdma.dma + (off); \
5888 	off += QMC_ALIGN(sizeof(struct qm_##type) * (num)); \
5889 } while (0)
5890 
5891 	idr_init(&qm->qp_idr);
5892 	qm->qdma.size = QMC_ALIGN(sizeof(struct qm_eqe) * QM_EQ_DEPTH) +
5893 			QMC_ALIGN(sizeof(struct qm_aeqe) * QM_Q_DEPTH) +
5894 			QMC_ALIGN(sizeof(struct qm_sqc) * qm->qp_num) +
5895 			QMC_ALIGN(sizeof(struct qm_cqc) * qm->qp_num);
5896 	qm->qdma.va = dma_alloc_coherent(dev, qm->qdma.size, &qm->qdma.dma,
5897 					 GFP_ATOMIC);
5898 	dev_dbg(dev, "allocate qm dma buf size=%zx)\n", qm->qdma.size);
5899 	if (!qm->qdma.va) {
5900 		ret =  -ENOMEM;
5901 		goto err_alloc_qdma;
5902 	}
5903 
5904 	QM_INIT_BUF(qm, eqe, QM_EQ_DEPTH);
5905 	QM_INIT_BUF(qm, aeqe, QM_Q_DEPTH);
5906 	QM_INIT_BUF(qm, sqc, qm->qp_num);
5907 	QM_INIT_BUF(qm, cqc, qm->qp_num);
5908 
5909 	ret = hisi_qp_alloc_memory(qm);
5910 	if (ret)
5911 		goto err_alloc_qp_array;
5912 
5913 	return 0;
5914 
5915 err_alloc_qp_array:
5916 	dma_free_coherent(dev, qm->qdma.size, qm->qdma.va, qm->qdma.dma);
5917 err_alloc_qdma:
5918 	kfree(qm->factor);
5919 
5920 	return ret;
5921 }
5922 
5923 /**
5924  * hisi_qm_init() - Initialize configures about qm.
5925  * @qm: The qm needing init.
5926  *
5927  * This function init qm, then we can call hisi_qm_start to put qm into work.
5928  */
5929 int hisi_qm_init(struct hisi_qm *qm)
5930 {
5931 	struct pci_dev *pdev = qm->pdev;
5932 	struct device *dev = &pdev->dev;
5933 	int ret;
5934 
5935 	hisi_qm_pre_init(qm);
5936 
5937 	ret = hisi_qm_pci_init(qm);
5938 	if (ret)
5939 		return ret;
5940 
5941 	ret = qm_irq_register(qm);
5942 	if (ret)
5943 		goto err_pci_init;
5944 
5945 	if (qm->fun_type == QM_HW_VF && qm->ver != QM_HW_V1) {
5946 		/* v2 starts to support get vft by mailbox */
5947 		ret = hisi_qm_get_vft(qm, &qm->qp_base, &qm->qp_num);
5948 		if (ret)
5949 			goto err_irq_register;
5950 	}
5951 
5952 	if (qm->fun_type == QM_HW_PF) {
5953 		qm_disable_clock_gate(qm);
5954 		ret = qm_dev_mem_reset(qm);
5955 		if (ret) {
5956 			dev_err(dev, "failed to reset device memory\n");
5957 			goto err_irq_register;
5958 		}
5959 	}
5960 
5961 	if (qm->mode == UACCE_MODE_SVA) {
5962 		ret = qm_alloc_uacce(qm);
5963 		if (ret < 0)
5964 			dev_warn(dev, "fail to alloc uacce (%d)\n", ret);
5965 	}
5966 
5967 	ret = hisi_qm_memory_init(qm);
5968 	if (ret)
5969 		goto err_alloc_uacce;
5970 
5971 	hisi_qm_init_work(qm);
5972 	qm_cmd_init(qm);
5973 	atomic_set(&qm->status.flags, QM_INIT);
5974 
5975 	return 0;
5976 
5977 err_alloc_uacce:
5978 	if (qm->use_sva) {
5979 		uacce_remove(qm->uacce);
5980 		qm->uacce = NULL;
5981 	}
5982 err_irq_register:
5983 	qm_irq_unregister(qm);
5984 err_pci_init:
5985 	hisi_qm_pci_uninit(qm);
5986 	return ret;
5987 }
5988 EXPORT_SYMBOL_GPL(hisi_qm_init);
5989 
5990 /**
5991  * hisi_qm_get_dfx_access() - Try to get dfx access.
5992  * @qm: pointer to accelerator device.
5993  *
5994  * Try to get dfx access, then user can get message.
5995  *
5996  * If device is in suspended, return failure, otherwise
5997  * bump up the runtime PM usage counter.
5998  */
5999 int hisi_qm_get_dfx_access(struct hisi_qm *qm)
6000 {
6001 	struct device *dev = &qm->pdev->dev;
6002 
6003 	if (pm_runtime_suspended(dev)) {
6004 		dev_info(dev, "can not read/write - device in suspended.\n");
6005 		return -EAGAIN;
6006 	}
6007 
6008 	return qm_pm_get_sync(qm);
6009 }
6010 EXPORT_SYMBOL_GPL(hisi_qm_get_dfx_access);
6011 
6012 /**
6013  * hisi_qm_put_dfx_access() - Put dfx access.
6014  * @qm: pointer to accelerator device.
6015  *
6016  * Put dfx access, drop runtime PM usage counter.
6017  */
6018 void hisi_qm_put_dfx_access(struct hisi_qm *qm)
6019 {
6020 	qm_pm_put_sync(qm);
6021 }
6022 EXPORT_SYMBOL_GPL(hisi_qm_put_dfx_access);
6023 
6024 /**
6025  * hisi_qm_pm_init() - Initialize qm runtime PM.
6026  * @qm: pointer to accelerator device.
6027  *
6028  * Function that initialize qm runtime PM.
6029  */
6030 void hisi_qm_pm_init(struct hisi_qm *qm)
6031 {
6032 	struct device *dev = &qm->pdev->dev;
6033 
6034 	if (qm->fun_type == QM_HW_VF || qm->ver < QM_HW_V3)
6035 		return;
6036 
6037 	pm_runtime_set_autosuspend_delay(dev, QM_AUTOSUSPEND_DELAY);
6038 	pm_runtime_use_autosuspend(dev);
6039 	pm_runtime_put_noidle(dev);
6040 }
6041 EXPORT_SYMBOL_GPL(hisi_qm_pm_init);
6042 
6043 /**
6044  * hisi_qm_pm_uninit() - Uninitialize qm runtime PM.
6045  * @qm: pointer to accelerator device.
6046  *
6047  * Function that uninitialize qm runtime PM.
6048  */
6049 void hisi_qm_pm_uninit(struct hisi_qm *qm)
6050 {
6051 	struct device *dev = &qm->pdev->dev;
6052 
6053 	if (qm->fun_type == QM_HW_VF || qm->ver < QM_HW_V3)
6054 		return;
6055 
6056 	pm_runtime_get_noresume(dev);
6057 	pm_runtime_dont_use_autosuspend(dev);
6058 }
6059 EXPORT_SYMBOL_GPL(hisi_qm_pm_uninit);
6060 
6061 static int qm_prepare_for_suspend(struct hisi_qm *qm)
6062 {
6063 	struct pci_dev *pdev = qm->pdev;
6064 	int ret;
6065 	u32 val;
6066 
6067 	ret = qm->ops->set_msi(qm, false);
6068 	if (ret) {
6069 		pci_err(pdev, "failed to disable MSI before suspending!\n");
6070 		return ret;
6071 	}
6072 
6073 	/* shutdown OOO register */
6074 	writel(ACC_MASTER_GLOBAL_CTRL_SHUTDOWN,
6075 	       qm->io_base + ACC_MASTER_GLOBAL_CTRL);
6076 
6077 	ret = readl_relaxed_poll_timeout(qm->io_base + ACC_MASTER_TRANS_RETURN,
6078 					 val,
6079 					 (val == ACC_MASTER_TRANS_RETURN_RW),
6080 					 POLL_PERIOD, POLL_TIMEOUT);
6081 	if (ret) {
6082 		pci_emerg(pdev, "Bus lock! Please reset system.\n");
6083 		return ret;
6084 	}
6085 
6086 	ret = qm_set_pf_mse(qm, false);
6087 	if (ret)
6088 		pci_err(pdev, "failed to disable MSE before suspending!\n");
6089 
6090 	return ret;
6091 }
6092 
6093 static int qm_rebuild_for_resume(struct hisi_qm *qm)
6094 {
6095 	struct pci_dev *pdev = qm->pdev;
6096 	int ret;
6097 
6098 	ret = qm_set_pf_mse(qm, true);
6099 	if (ret) {
6100 		pci_err(pdev, "failed to enable MSE after resuming!\n");
6101 		return ret;
6102 	}
6103 
6104 	ret = qm->ops->set_msi(qm, true);
6105 	if (ret) {
6106 		pci_err(pdev, "failed to enable MSI after resuming!\n");
6107 		return ret;
6108 	}
6109 
6110 	ret = qm_dev_hw_init(qm);
6111 	if (ret) {
6112 		pci_err(pdev, "failed to init device after resuming\n");
6113 		return ret;
6114 	}
6115 
6116 	qm_cmd_init(qm);
6117 	hisi_qm_dev_err_init(qm);
6118 	qm_disable_clock_gate(qm);
6119 	ret = qm_dev_mem_reset(qm);
6120 	if (ret)
6121 		pci_err(pdev, "failed to reset device memory\n");
6122 
6123 	return ret;
6124 }
6125 
6126 /**
6127  * hisi_qm_suspend() - Runtime suspend of given device.
6128  * @dev: device to suspend.
6129  *
6130  * Function that suspend the device.
6131  */
6132 int hisi_qm_suspend(struct device *dev)
6133 {
6134 	struct pci_dev *pdev = to_pci_dev(dev);
6135 	struct hisi_qm *qm = pci_get_drvdata(pdev);
6136 	int ret;
6137 
6138 	pci_info(pdev, "entering suspended state\n");
6139 
6140 	ret = hisi_qm_stop(qm, QM_NORMAL);
6141 	if (ret) {
6142 		pci_err(pdev, "failed to stop qm(%d)\n", ret);
6143 		return ret;
6144 	}
6145 
6146 	ret = qm_prepare_for_suspend(qm);
6147 	if (ret)
6148 		pci_err(pdev, "failed to prepare suspended(%d)\n", ret);
6149 
6150 	return ret;
6151 }
6152 EXPORT_SYMBOL_GPL(hisi_qm_suspend);
6153 
6154 /**
6155  * hisi_qm_resume() - Runtime resume of given device.
6156  * @dev: device to resume.
6157  *
6158  * Function that resume the device.
6159  */
6160 int hisi_qm_resume(struct device *dev)
6161 {
6162 	struct pci_dev *pdev = to_pci_dev(dev);
6163 	struct hisi_qm *qm = pci_get_drvdata(pdev);
6164 	int ret;
6165 
6166 	pci_info(pdev, "resuming from suspend state\n");
6167 
6168 	ret = qm_rebuild_for_resume(qm);
6169 	if (ret) {
6170 		pci_err(pdev, "failed to rebuild resume(%d)\n", ret);
6171 		return ret;
6172 	}
6173 
6174 	ret = hisi_qm_start(qm);
6175 	if (ret)
6176 		pci_err(pdev, "failed to start qm(%d)\n", ret);
6177 
6178 	return ret;
6179 }
6180 EXPORT_SYMBOL_GPL(hisi_qm_resume);
6181 
6182 MODULE_LICENSE("GPL v2");
6183 MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>");
6184 MODULE_DESCRIPTION("HiSilicon Accelerator queue manager driver");
6185