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