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