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