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