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