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