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