1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2021, HiSilicon Ltd.
4 */
5
6 #include <linux/device.h>
7 #include <linux/eventfd.h>
8 #include <linux/file.h>
9 #include <linux/hisi_acc_qm.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/vfio.h>
14 #include <linux/vfio_pci_core.h>
15 #include <linux/anon_inodes.h>
16
17 #include "hisi_acc_vfio_pci.h"
18
19 /* Return 0 on VM acc device ready, -ETIMEDOUT hardware timeout */
qm_wait_dev_not_ready(struct hisi_qm * qm)20 static int qm_wait_dev_not_ready(struct hisi_qm *qm)
21 {
22 u32 val;
23
24 return readl_relaxed_poll_timeout(qm->io_base + QM_VF_STATE,
25 val, !(val & 0x1), MB_POLL_PERIOD_US,
26 MB_POLL_TIMEOUT_US);
27 }
28
29 /*
30 * Each state Reg is checked 100 times,
31 * with a delay of 100 microseconds after each check
32 */
qm_check_reg_state(struct hisi_qm * qm,u32 regs)33 static u32 qm_check_reg_state(struct hisi_qm *qm, u32 regs)
34 {
35 int check_times = 0;
36 u32 state;
37
38 state = readl(qm->io_base + regs);
39 while (state && check_times < ERROR_CHECK_TIMEOUT) {
40 udelay(CHECK_DELAY_TIME);
41 state = readl(qm->io_base + regs);
42 check_times++;
43 }
44
45 return state;
46 }
47
qm_read_regs(struct hisi_qm * qm,u32 reg_addr,u32 * data,u8 nums)48 static int qm_read_regs(struct hisi_qm *qm, u32 reg_addr,
49 u32 *data, u8 nums)
50 {
51 int i;
52
53 if (nums < 1 || nums > QM_REGS_MAX_LEN)
54 return -EINVAL;
55
56 for (i = 0; i < nums; i++) {
57 data[i] = readl(qm->io_base + reg_addr);
58 reg_addr += QM_REG_ADDR_OFFSET;
59 }
60
61 return 0;
62 }
63
qm_write_regs(struct hisi_qm * qm,u32 reg,u32 * data,u8 nums)64 static int qm_write_regs(struct hisi_qm *qm, u32 reg,
65 u32 *data, u8 nums)
66 {
67 int i;
68
69 if (nums < 1 || nums > QM_REGS_MAX_LEN)
70 return -EINVAL;
71
72 for (i = 0; i < nums; i++)
73 writel(data[i], qm->io_base + reg + i * QM_REG_ADDR_OFFSET);
74
75 return 0;
76 }
77
qm_get_vft(struct hisi_qm * qm,u32 * base)78 static int qm_get_vft(struct hisi_qm *qm, u32 *base)
79 {
80 u64 sqc_vft;
81 u32 qp_num;
82 int ret;
83
84 ret = hisi_qm_mb(qm, QM_MB_CMD_SQC_VFT_V2, 0, 0, 1);
85 if (ret)
86 return ret;
87
88 sqc_vft = readl(qm->io_base + QM_MB_CMD_DATA_ADDR_L) |
89 ((u64)readl(qm->io_base + QM_MB_CMD_DATA_ADDR_H) <<
90 QM_XQC_ADDR_OFFSET);
91 *base = QM_SQC_VFT_BASE_MASK_V2 & (sqc_vft >> QM_SQC_VFT_BASE_SHIFT_V2);
92 qp_num = (QM_SQC_VFT_NUM_MASK_V2 &
93 (sqc_vft >> QM_SQC_VFT_NUM_SHIFT_V2)) + 1;
94
95 return qp_num;
96 }
97
qm_get_sqc(struct hisi_qm * qm,u64 * addr)98 static int qm_get_sqc(struct hisi_qm *qm, u64 *addr)
99 {
100 int ret;
101
102 ret = hisi_qm_mb(qm, QM_MB_CMD_SQC_BT, 0, 0, 1);
103 if (ret)
104 return ret;
105
106 *addr = readl(qm->io_base + QM_MB_CMD_DATA_ADDR_L) |
107 ((u64)readl(qm->io_base + QM_MB_CMD_DATA_ADDR_H) <<
108 QM_XQC_ADDR_OFFSET);
109
110 return 0;
111 }
112
qm_get_cqc(struct hisi_qm * qm,u64 * addr)113 static int qm_get_cqc(struct hisi_qm *qm, u64 *addr)
114 {
115 int ret;
116
117 ret = hisi_qm_mb(qm, QM_MB_CMD_CQC_BT, 0, 0, 1);
118 if (ret)
119 return ret;
120
121 *addr = readl(qm->io_base + QM_MB_CMD_DATA_ADDR_L) |
122 ((u64)readl(qm->io_base + QM_MB_CMD_DATA_ADDR_H) <<
123 QM_XQC_ADDR_OFFSET);
124
125 return 0;
126 }
127
qm_xqc_reg_offsets(struct hisi_qm * qm,u32 * eqc_addr,u32 * aeqc_addr)128 static void qm_xqc_reg_offsets(struct hisi_qm *qm,
129 u32 *eqc_addr, u32 *aeqc_addr)
130 {
131 struct hisi_acc_vf_core_device *hisi_acc_vdev =
132 container_of(qm, struct hisi_acc_vf_core_device, vf_qm);
133
134 if (hisi_acc_vdev->drv_mode == HW_ACC_MIG_VF_CTRL) {
135 *eqc_addr = QM_EQC_VF_DW0;
136 *aeqc_addr = QM_AEQC_VF_DW0;
137 } else {
138 *eqc_addr = QM_EQC_PF_DW0;
139 *aeqc_addr = QM_AEQC_PF_DW0;
140 }
141 }
142
qm_get_regs(struct hisi_qm * qm,struct acc_vf_data * vf_data)143 static int qm_get_regs(struct hisi_qm *qm, struct acc_vf_data *vf_data)
144 {
145 struct device *dev = &qm->pdev->dev;
146 u32 eqc_addr, aeqc_addr;
147 int ret;
148
149 ret = qm_read_regs(qm, QM_VF_AEQ_INT_MASK, &vf_data->aeq_int_mask, 1);
150 if (ret) {
151 dev_err(dev, "failed to read QM_VF_AEQ_INT_MASK\n");
152 return ret;
153 }
154
155 ret = qm_read_regs(qm, QM_VF_EQ_INT_MASK, &vf_data->eq_int_mask, 1);
156 if (ret) {
157 dev_err(dev, "failed to read QM_VF_EQ_INT_MASK\n");
158 return ret;
159 }
160
161 ret = qm_read_regs(qm, QM_IFC_INT_SOURCE_V,
162 &vf_data->ifc_int_source, 1);
163 if (ret) {
164 dev_err(dev, "failed to read QM_IFC_INT_SOURCE_V\n");
165 return ret;
166 }
167
168 ret = qm_read_regs(qm, QM_IFC_INT_MASK, &vf_data->ifc_int_mask, 1);
169 if (ret) {
170 dev_err(dev, "failed to read QM_IFC_INT_MASK\n");
171 return ret;
172 }
173
174 ret = qm_read_regs(qm, QM_IFC_INT_SET_V, &vf_data->ifc_int_set, 1);
175 if (ret) {
176 dev_err(dev, "failed to read QM_IFC_INT_SET_V\n");
177 return ret;
178 }
179
180 ret = qm_read_regs(qm, QM_PAGE_SIZE, &vf_data->page_size, 1);
181 if (ret) {
182 dev_err(dev, "failed to read QM_PAGE_SIZE\n");
183 return ret;
184 }
185
186 qm_xqc_reg_offsets(qm, &eqc_addr, &aeqc_addr);
187 /* QM_EQC_DW has 7 regs */
188 ret = qm_read_regs(qm, eqc_addr, vf_data->qm_eqc_dw, 7);
189 if (ret) {
190 dev_err(dev, "failed to read QM_EQC_DW\n");
191 return ret;
192 }
193
194 /* QM_AEQC_DW has 7 regs */
195 ret = qm_read_regs(qm, aeqc_addr, vf_data->qm_aeqc_dw, 7);
196 if (ret) {
197 dev_err(dev, "failed to read QM_AEQC_DW\n");
198 return ret;
199 }
200
201 return 0;
202 }
203
qm_set_regs(struct hisi_qm * qm,struct acc_vf_data * vf_data)204 static int qm_set_regs(struct hisi_qm *qm, struct acc_vf_data *vf_data)
205 {
206 struct device *dev = &qm->pdev->dev;
207 u32 eqc_addr, aeqc_addr;
208 int ret;
209
210 /* Check VF state */
211 ret = hisi_qm_wait_mb_ready(qm);
212 if (unlikely(ret)) {
213 dev_err(&qm->pdev->dev, "QM device is not ready to write\n");
214 return ret;
215 }
216
217 ret = qm_write_regs(qm, QM_VF_AEQ_INT_MASK, &vf_data->aeq_int_mask, 1);
218 if (ret) {
219 dev_err(dev, "failed to write QM_VF_AEQ_INT_MASK\n");
220 return ret;
221 }
222
223 ret = qm_write_regs(qm, QM_VF_EQ_INT_MASK, &vf_data->eq_int_mask, 1);
224 if (ret) {
225 dev_err(dev, "failed to write QM_VF_EQ_INT_MASK\n");
226 return ret;
227 }
228
229 ret = qm_write_regs(qm, QM_IFC_INT_SOURCE_V,
230 &vf_data->ifc_int_source, 1);
231 if (ret) {
232 dev_err(dev, "failed to write QM_IFC_INT_SOURCE_V\n");
233 return ret;
234 }
235
236 ret = qm_write_regs(qm, QM_IFC_INT_MASK, &vf_data->ifc_int_mask, 1);
237 if (ret) {
238 dev_err(dev, "failed to write QM_IFC_INT_MASK\n");
239 return ret;
240 }
241
242 ret = qm_write_regs(qm, QM_IFC_INT_SET_V, &vf_data->ifc_int_set, 1);
243 if (ret) {
244 dev_err(dev, "failed to write QM_IFC_INT_SET_V\n");
245 return ret;
246 }
247
248 ret = qm_write_regs(qm, QM_QUE_ISO_CFG_V, &vf_data->que_iso_cfg, 1);
249 if (ret) {
250 dev_err(dev, "failed to write QM_QUE_ISO_CFG_V\n");
251 return ret;
252 }
253
254 ret = qm_write_regs(qm, QM_PAGE_SIZE, &vf_data->page_size, 1);
255 if (ret) {
256 dev_err(dev, "failed to write QM_PAGE_SIZE\n");
257 return ret;
258 }
259
260 qm_xqc_reg_offsets(qm, &eqc_addr, &aeqc_addr);
261 /* QM_EQC_DW has 7 regs */
262 ret = qm_write_regs(qm, eqc_addr, vf_data->qm_eqc_dw, 7);
263 if (ret) {
264 dev_err(dev, "failed to write QM_EQC_DW\n");
265 return ret;
266 }
267
268 /* QM_AEQC_DW has 7 regs */
269 ret = qm_write_regs(qm, aeqc_addr, vf_data->qm_aeqc_dw, 7);
270 if (ret) {
271 dev_err(dev, "failed to write QM_AEQC_DW\n");
272 return ret;
273 }
274
275 return 0;
276 }
277
qm_db(struct hisi_qm * qm,u16 qn,u8 cmd,u16 index,u8 priority)278 static void qm_db(struct hisi_qm *qm, u16 qn, u8 cmd,
279 u16 index, u8 priority)
280 {
281 u64 doorbell;
282 u64 dbase;
283 u16 randata = 0;
284
285 if (cmd == QM_DOORBELL_CMD_SQ || cmd == QM_DOORBELL_CMD_CQ)
286 dbase = QM_DOORBELL_SQ_CQ_BASE_V2;
287 else
288 dbase = QM_DOORBELL_EQ_AEQ_BASE_V2;
289
290 doorbell = qn | ((u64)cmd << QM_DB_CMD_SHIFT_V2) |
291 ((u64)randata << QM_DB_RAND_SHIFT_V2) |
292 ((u64)index << QM_DB_INDEX_SHIFT_V2) |
293 ((u64)priority << QM_DB_PRIORITY_SHIFT_V2);
294
295 writeq(doorbell, qm->io_base + dbase);
296 }
297
pf_qm_get_qp_num(struct hisi_qm * qm,int vf_id,u32 * rbase)298 static int pf_qm_get_qp_num(struct hisi_qm *qm, int vf_id, u32 *rbase)
299 {
300 unsigned int val;
301 u64 sqc_vft;
302 u32 qp_num;
303 int ret;
304
305 ret = readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val,
306 val & BIT(0), MB_POLL_PERIOD_US,
307 MB_POLL_TIMEOUT_US);
308 if (ret)
309 return ret;
310
311 writel(0x1, qm->io_base + QM_VFT_CFG_OP_WR);
312 /* 0 mean SQC VFT */
313 writel(0x0, qm->io_base + QM_VFT_CFG_TYPE);
314 writel(vf_id, qm->io_base + QM_VFT_CFG);
315
316 writel(0x0, qm->io_base + QM_VFT_CFG_RDY);
317 writel(0x1, qm->io_base + QM_VFT_CFG_OP_ENABLE);
318
319 ret = readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val,
320 val & BIT(0), MB_POLL_PERIOD_US,
321 MB_POLL_TIMEOUT_US);
322 if (ret)
323 return ret;
324
325 sqc_vft = readl(qm->io_base + QM_VFT_CFG_DATA_L) |
326 ((u64)readl(qm->io_base + QM_VFT_CFG_DATA_H) <<
327 QM_XQC_ADDR_OFFSET);
328 *rbase = QM_SQC_VFT_BASE_MASK_V2 &
329 (sqc_vft >> QM_SQC_VFT_BASE_SHIFT_V2);
330 qp_num = (QM_SQC_VFT_NUM_MASK_V2 &
331 (sqc_vft >> QM_SQC_VFT_NUM_SHIFT_V2)) + 1;
332
333 return qp_num;
334 }
335
qm_dev_cmd_init(struct hisi_qm * qm)336 static void qm_dev_cmd_init(struct hisi_qm *qm)
337 {
338 /* Clear VF communication status registers. */
339 writel(0x1, qm->io_base + QM_IFC_INT_SOURCE_V);
340
341 /* Enable pf and vf communication. */
342 writel(0x0, qm->io_base + QM_IFC_INT_MASK);
343 }
344
vf_qm_cache_wb(struct hisi_qm * qm)345 static int vf_qm_cache_wb(struct hisi_qm *qm)
346 {
347 unsigned int val;
348 int ret;
349
350 writel(0x1, qm->io_base + QM_CACHE_WB_START);
351 ret = readl_relaxed_poll_timeout(qm->io_base + QM_CACHE_WB_DONE,
352 val, val & BIT(0), MB_POLL_PERIOD_US,
353 MB_POLL_TIMEOUT_US);
354 if (ret) {
355 dev_err(&qm->pdev->dev, "vf QM writeback sqc cache fail\n");
356 return ret;
357 }
358
359 return 0;
360 }
361
vf_qm_fun_reset(struct hisi_qm * qm)362 static void vf_qm_fun_reset(struct hisi_qm *qm)
363 {
364 int i;
365
366 for (i = 0; i < qm->qp_num; i++)
367 qm_db(qm, i, QM_DOORBELL_CMD_SQ, 0, 1);
368 }
369
vf_qm_func_stop(struct hisi_qm * qm)370 static int vf_qm_func_stop(struct hisi_qm *qm)
371 {
372 return hisi_qm_mb(qm, QM_MB_CMD_PAUSE_QM, 0, 0, 0);
373 }
374
vf_qm_version_check(struct acc_vf_data * vf_data,struct device * dev)375 static int vf_qm_version_check(struct acc_vf_data *vf_data, struct device *dev)
376 {
377 switch (vf_data->acc_magic) {
378 case ACC_DEV_MAGIC_V2:
379 if (vf_data->major_ver != ACC_DRV_MAJOR_VER) {
380 dev_info(dev, "migration driver version<%u.%u> not match!\n",
381 vf_data->major_ver, vf_data->minor_ver);
382 return -EINVAL;
383 }
384 break;
385 case ACC_DEV_MAGIC_V1:
386 /* Correct dma address */
387 vf_data->eqe_dma = vf_data->qm_eqc_dw[QM_XQC_ADDR_HIGH];
388 vf_data->eqe_dma <<= QM_XQC_ADDR_OFFSET;
389 vf_data->eqe_dma |= vf_data->qm_eqc_dw[QM_XQC_ADDR_LOW];
390 vf_data->aeqe_dma = vf_data->qm_aeqc_dw[QM_XQC_ADDR_HIGH];
391 vf_data->aeqe_dma <<= QM_XQC_ADDR_OFFSET;
392 vf_data->aeqe_dma |= vf_data->qm_aeqc_dw[QM_XQC_ADDR_LOW];
393 break;
394 default:
395 return -EINVAL;
396 }
397
398 return 0;
399 }
400
vf_qm_check_match(struct hisi_acc_vf_core_device * hisi_acc_vdev,struct hisi_acc_vf_migration_file * migf)401 static int vf_qm_check_match(struct hisi_acc_vf_core_device *hisi_acc_vdev,
402 struct hisi_acc_vf_migration_file *migf)
403 {
404 struct acc_vf_data *vf_data = &migf->vf_data;
405 struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
406 struct hisi_qm *pf_qm = hisi_acc_vdev->pf_qm;
407 struct device *dev = &vf_qm->pdev->dev;
408 u32 que_iso_state;
409 int ret;
410
411 if (migf->total_length < QM_MATCH_SIZE || hisi_acc_vdev->match_done)
412 return 0;
413
414 ret = vf_qm_version_check(vf_data, dev);
415 if (ret) {
416 dev_err(dev, "failed to match ACC_DEV_MAGIC\n");
417 return ret;
418 }
419
420 if (vf_data->dev_id != hisi_acc_vdev->vf_dev->device) {
421 dev_err(dev, "failed to match VF devices\n");
422 return -EINVAL;
423 }
424
425 /* VF qp num check */
426 ret = qm_get_vft(vf_qm, &vf_qm->qp_base);
427 if (ret <= 0) {
428 dev_err(dev, "failed to get vft qp nums\n");
429 return ret;
430 }
431
432 if (ret != vf_data->qp_num) {
433 dev_err(dev, "failed to match VF qp num\n");
434 return -EINVAL;
435 }
436
437 vf_qm->qp_num = ret;
438
439 /* VF isolation state check */
440 ret = qm_read_regs(pf_qm, QM_QUE_ISO_CFG_V, &que_iso_state, 1);
441 if (ret) {
442 dev_err(dev, "failed to read QM_QUE_ISO_CFG_V\n");
443 return ret;
444 }
445
446 if (vf_data->que_iso_cfg != que_iso_state) {
447 dev_err(dev, "failed to match isolation state\n");
448 return -EINVAL;
449 }
450
451 hisi_acc_vdev->match_done = true;
452 return 0;
453 }
454
vf_qm_get_match_data(struct hisi_acc_vf_core_device * hisi_acc_vdev,struct acc_vf_data * vf_data)455 static int vf_qm_get_match_data(struct hisi_acc_vf_core_device *hisi_acc_vdev,
456 struct acc_vf_data *vf_data)
457 {
458 struct hisi_qm *pf_qm = hisi_acc_vdev->pf_qm;
459 struct device *dev = &pf_qm->pdev->dev;
460 int vf_id = hisi_acc_vdev->vf_id;
461 int ret;
462
463 vf_data->acc_magic = ACC_DEV_MAGIC_V2;
464 vf_data->major_ver = ACC_DRV_MAJOR_VER;
465 vf_data->minor_ver = ACC_DRV_MINOR_VER;
466 /* Save device id */
467 vf_data->dev_id = hisi_acc_vdev->vf_dev->device;
468
469 /* VF qp num save from PF */
470 ret = pf_qm_get_qp_num(pf_qm, vf_id, &vf_data->qp_base);
471 if (ret <= 0) {
472 dev_err(dev, "failed to get vft qp nums!\n");
473 return -EINVAL;
474 }
475
476 vf_data->qp_num = ret;
477
478 /* VF isolation state save from PF */
479 ret = qm_read_regs(pf_qm, QM_QUE_ISO_CFG_V, &vf_data->que_iso_cfg, 1);
480 if (ret) {
481 dev_err(dev, "failed to read QM_QUE_ISO_CFG_V!\n");
482 return ret;
483 }
484
485 return 0;
486 }
487
vf_qm_xeqc_save(struct hisi_qm * qm,struct hisi_acc_vf_migration_file * migf)488 static void vf_qm_xeqc_save(struct hisi_qm *qm,
489 struct hisi_acc_vf_migration_file *migf)
490 {
491 struct acc_vf_data *vf_data = &migf->vf_data;
492 u16 eq_head, aeq_head;
493
494 eq_head = vf_data->qm_eqc_dw[0] & 0xFFFF;
495 qm_db(qm, 0, QM_DOORBELL_CMD_EQ, eq_head, 0);
496
497 aeq_head = vf_data->qm_aeqc_dw[0] & 0xFFFF;
498 qm_db(qm, 0, QM_DOORBELL_CMD_AEQ, aeq_head, 0);
499 }
500
vf_qm_load_data(struct hisi_acc_vf_core_device * hisi_acc_vdev,struct hisi_acc_vf_migration_file * migf)501 static int vf_qm_load_data(struct hisi_acc_vf_core_device *hisi_acc_vdev,
502 struct hisi_acc_vf_migration_file *migf)
503 {
504 struct hisi_qm *qm = &hisi_acc_vdev->vf_qm;
505 struct device *dev = &qm->pdev->dev;
506 struct acc_vf_data *vf_data = &migf->vf_data;
507 int ret;
508
509 /* Return if only match data was transferred */
510 if (migf->total_length == QM_MATCH_SIZE)
511 return 0;
512
513 if (migf->total_length < sizeof(struct acc_vf_data))
514 return -EINVAL;
515
516 if (!vf_data->eqe_dma || !vf_data->aeqe_dma ||
517 !vf_data->sqc_dma || !vf_data->cqc_dma) {
518 dev_info(dev, "resume dma addr is NULL!\n");
519 hisi_acc_vdev->vf_qm_state = QM_NOT_READY;
520 return 0;
521 }
522
523 ret = qm_write_regs(qm, QM_VF_STATE, &vf_data->vf_qm_state, 1);
524 if (ret) {
525 dev_err(dev, "failed to write QM_VF_STATE\n");
526 return ret;
527 }
528 hisi_acc_vdev->vf_qm_state = vf_data->vf_qm_state;
529
530 qm->eqe_dma = vf_data->eqe_dma;
531 qm->aeqe_dma = vf_data->aeqe_dma;
532 qm->sqc_dma = vf_data->sqc_dma;
533 qm->cqc_dma = vf_data->cqc_dma;
534
535 qm->qp_base = vf_data->qp_base;
536 qm->qp_num = vf_data->qp_num;
537
538 ret = qm_set_regs(qm, vf_data);
539 if (ret) {
540 dev_err(dev, "set VF regs failed\n");
541 return ret;
542 }
543
544 ret = hisi_qm_mb(qm, QM_MB_CMD_SQC_BT, qm->sqc_dma, 0, 0);
545 if (ret) {
546 dev_err(dev, "set sqc failed\n");
547 return ret;
548 }
549
550 ret = hisi_qm_mb(qm, QM_MB_CMD_CQC_BT, qm->cqc_dma, 0, 0);
551 if (ret) {
552 dev_err(dev, "set cqc failed\n");
553 return ret;
554 }
555
556 qm_dev_cmd_init(qm);
557 return 0;
558 }
559
vf_qm_read_data(struct hisi_qm * vf_qm,struct acc_vf_data * vf_data)560 static int vf_qm_read_data(struct hisi_qm *vf_qm, struct acc_vf_data *vf_data)
561 {
562 struct device *dev = &vf_qm->pdev->dev;
563 int ret;
564
565 ret = qm_get_regs(vf_qm, vf_data);
566 if (ret)
567 return ret;
568
569 /* Every reg is 32 bit, the dma address is 64 bit. */
570 vf_data->eqe_dma = vf_data->qm_eqc_dw[QM_XQC_ADDR_HIGH];
571 vf_data->eqe_dma <<= QM_XQC_ADDR_OFFSET;
572 vf_data->eqe_dma |= vf_data->qm_eqc_dw[QM_XQC_ADDR_LOW];
573 vf_data->aeqe_dma = vf_data->qm_aeqc_dw[QM_XQC_ADDR_HIGH];
574 vf_data->aeqe_dma <<= QM_XQC_ADDR_OFFSET;
575 vf_data->aeqe_dma |= vf_data->qm_aeqc_dw[QM_XQC_ADDR_LOW];
576
577 /* Through SQC_BT/CQC_BT to get sqc and cqc address */
578 ret = qm_get_sqc(vf_qm, &vf_data->sqc_dma);
579 if (ret) {
580 dev_err(dev, "failed to read SQC addr!\n");
581 return ret;
582 }
583
584 ret = qm_get_cqc(vf_qm, &vf_data->cqc_dma);
585 if (ret) {
586 dev_err(dev, "failed to read CQC addr!\n");
587 return ret;
588 }
589
590 return 0;
591 }
592
vf_qm_state_save(struct hisi_acc_vf_core_device * hisi_acc_vdev,struct hisi_acc_vf_migration_file * migf)593 static int vf_qm_state_save(struct hisi_acc_vf_core_device *hisi_acc_vdev,
594 struct hisi_acc_vf_migration_file *migf)
595 {
596 struct acc_vf_data *vf_data = &migf->vf_data;
597 struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
598 int ret;
599
600 if (unlikely(qm_wait_dev_not_ready(vf_qm))) {
601 /* Update state and return with match data */
602 vf_data->vf_qm_state = QM_NOT_READY;
603 hisi_acc_vdev->vf_qm_state = vf_data->vf_qm_state;
604 migf->total_length = QM_MATCH_SIZE;
605 return 0;
606 }
607
608 vf_data->vf_qm_state = QM_READY;
609 hisi_acc_vdev->vf_qm_state = vf_data->vf_qm_state;
610
611 ret = vf_qm_read_data(vf_qm, vf_data);
612 if (ret)
613 return ret;
614
615 migf->total_length = sizeof(struct acc_vf_data);
616 /* Save eqc and aeqc interrupt information */
617 vf_qm_xeqc_save(vf_qm, migf);
618
619 return 0;
620 }
621
hisi_acc_drvdata(struct pci_dev * pdev)622 static struct hisi_acc_vf_core_device *hisi_acc_drvdata(struct pci_dev *pdev)
623 {
624 struct vfio_pci_core_device *core_device = dev_get_drvdata(&pdev->dev);
625
626 return container_of(core_device, struct hisi_acc_vf_core_device,
627 core_device);
628 }
629
630 /* Check the PF's RAS state and Function INT state */
631 static int
hisi_acc_check_int_state(struct hisi_acc_vf_core_device * hisi_acc_vdev)632 hisi_acc_check_int_state(struct hisi_acc_vf_core_device *hisi_acc_vdev)
633 {
634 struct hisi_qm *vfqm = &hisi_acc_vdev->vf_qm;
635 struct hisi_qm *qm = hisi_acc_vdev->pf_qm;
636 struct pci_dev *vf_pdev = hisi_acc_vdev->vf_dev;
637 struct device *dev = &qm->pdev->dev;
638 u32 state;
639
640 /* Check RAS state */
641 state = qm_check_reg_state(qm, QM_ABNORMAL_INT_STATUS);
642 if (state) {
643 dev_err(dev, "failed to check QM RAS state!\n");
644 return -EBUSY;
645 }
646
647 /* Check Function Communication state between PF and VF */
648 state = qm_check_reg_state(vfqm, QM_IFC_INT_STATUS);
649 if (state) {
650 dev_err(dev, "failed to check QM IFC INT state!\n");
651 return -EBUSY;
652 }
653 state = qm_check_reg_state(vfqm, QM_IFC_INT_SET_V);
654 if (state) {
655 dev_err(dev, "failed to check QM IFC INT SET state!\n");
656 return -EBUSY;
657 }
658
659 /* Check submodule task state */
660 switch (vf_pdev->device) {
661 case PCI_DEVICE_ID_HUAWEI_SEC_VF:
662 state = qm_check_reg_state(qm, SEC_CORE_INT_STATUS);
663 if (state) {
664 dev_err(dev, "failed to check QM SEC Core INT state!\n");
665 return -EBUSY;
666 }
667 return 0;
668 case PCI_DEVICE_ID_HUAWEI_HPRE_VF:
669 state = qm_check_reg_state(qm, HPRE_HAC_INT_STATUS);
670 if (state) {
671 dev_err(dev, "failed to check QM HPRE HAC INT state!\n");
672 return -EBUSY;
673 }
674 return 0;
675 case PCI_DEVICE_ID_HUAWEI_ZIP_VF:
676 state = qm_check_reg_state(qm, HZIP_CORE_INT_STATUS);
677 if (state) {
678 dev_err(dev, "failed to check QM ZIP Core INT state!\n");
679 return -EBUSY;
680 }
681 return 0;
682 default:
683 dev_err(dev, "failed to detect acc module type!\n");
684 return -EINVAL;
685 }
686 }
687
hisi_acc_vf_disable_fd(struct hisi_acc_vf_migration_file * migf)688 static void hisi_acc_vf_disable_fd(struct hisi_acc_vf_migration_file *migf)
689 {
690 mutex_lock(&migf->lock);
691 migf->disabled = true;
692 migf->total_length = 0;
693 migf->filp->f_pos = 0;
694 mutex_unlock(&migf->lock);
695 }
696
697 static void
hisi_acc_debug_migf_copy(struct hisi_acc_vf_core_device * hisi_acc_vdev,struct hisi_acc_vf_migration_file * src_migf)698 hisi_acc_debug_migf_copy(struct hisi_acc_vf_core_device *hisi_acc_vdev,
699 struct hisi_acc_vf_migration_file *src_migf)
700 {
701 struct hisi_acc_vf_migration_file *dst_migf = hisi_acc_vdev->debug_migf;
702
703 if (!dst_migf)
704 return;
705
706 dst_migf->total_length = src_migf->total_length;
707 memcpy(&dst_migf->vf_data, &src_migf->vf_data,
708 sizeof(struct acc_vf_data));
709 }
710
hisi_acc_vf_disable_fds(struct hisi_acc_vf_core_device * hisi_acc_vdev)711 static void hisi_acc_vf_disable_fds(struct hisi_acc_vf_core_device *hisi_acc_vdev)
712 {
713 if (hisi_acc_vdev->resuming_migf) {
714 hisi_acc_debug_migf_copy(hisi_acc_vdev, hisi_acc_vdev->resuming_migf);
715 hisi_acc_vf_disable_fd(hisi_acc_vdev->resuming_migf);
716 fput(hisi_acc_vdev->resuming_migf->filp);
717 hisi_acc_vdev->resuming_migf = NULL;
718 }
719
720 if (hisi_acc_vdev->saving_migf) {
721 hisi_acc_debug_migf_copy(hisi_acc_vdev, hisi_acc_vdev->saving_migf);
722 hisi_acc_vf_disable_fd(hisi_acc_vdev->saving_migf);
723 fput(hisi_acc_vdev->saving_migf->filp);
724 hisi_acc_vdev->saving_migf = NULL;
725 }
726 }
727
hisi_acc_get_vf_dev(struct vfio_device * vdev)728 static struct hisi_acc_vf_core_device *hisi_acc_get_vf_dev(struct vfio_device *vdev)
729 {
730 return container_of(vdev, struct hisi_acc_vf_core_device,
731 core_device.vdev);
732 }
733
hisi_acc_vf_reset(struct hisi_acc_vf_core_device * hisi_acc_vdev)734 static void hisi_acc_vf_reset(struct hisi_acc_vf_core_device *hisi_acc_vdev)
735 {
736 hisi_acc_vdev->vf_qm_state = QM_NOT_READY;
737 hisi_acc_vdev->mig_state = VFIO_DEVICE_STATE_RUNNING;
738 hisi_acc_vf_disable_fds(hisi_acc_vdev);
739 }
740
hisi_acc_vf_start_device(struct hisi_acc_vf_core_device * hisi_acc_vdev)741 static void hisi_acc_vf_start_device(struct hisi_acc_vf_core_device *hisi_acc_vdev)
742 {
743 struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
744
745 if (hisi_acc_vdev->vf_qm_state != QM_READY)
746 return;
747
748 /* Make sure the device is enabled */
749 qm_dev_cmd_init(vf_qm);
750
751 vf_qm_fun_reset(vf_qm);
752 }
753
hisi_acc_vf_load_state(struct hisi_acc_vf_core_device * hisi_acc_vdev)754 static int hisi_acc_vf_load_state(struct hisi_acc_vf_core_device *hisi_acc_vdev)
755 {
756 struct device *dev = &hisi_acc_vdev->vf_dev->dev;
757 struct hisi_acc_vf_migration_file *migf = hisi_acc_vdev->resuming_migf;
758 int ret;
759
760 /* Recover data to VF */
761 ret = vf_qm_load_data(hisi_acc_vdev, migf);
762 if (ret) {
763 dev_err(dev, "failed to recover the VF!\n");
764 return ret;
765 }
766
767 return 0;
768 }
769
hisi_acc_vf_release_file(struct inode * inode,struct file * filp)770 static int hisi_acc_vf_release_file(struct inode *inode, struct file *filp)
771 {
772 struct hisi_acc_vf_migration_file *migf = filp->private_data;
773
774 hisi_acc_vf_disable_fd(migf);
775 mutex_destroy(&migf->lock);
776 kfree(migf);
777 return 0;
778 }
779
hisi_acc_vf_resume_write(struct file * filp,const char __user * buf,size_t len,loff_t * pos)780 static ssize_t hisi_acc_vf_resume_write(struct file *filp, const char __user *buf,
781 size_t len, loff_t *pos)
782 {
783 struct hisi_acc_vf_migration_file *migf = filp->private_data;
784 u8 *vf_data = (u8 *)&migf->vf_data;
785 loff_t requested_length;
786 ssize_t done = 0;
787 int ret;
788
789 if (pos)
790 return -ESPIPE;
791 pos = &filp->f_pos;
792
793 if (*pos < 0 ||
794 check_add_overflow((loff_t)len, *pos, &requested_length))
795 return -EINVAL;
796
797 if (requested_length > sizeof(struct acc_vf_data))
798 return -ENOMEM;
799
800 mutex_lock(&migf->lock);
801 if (migf->disabled) {
802 done = -ENODEV;
803 goto out_unlock;
804 }
805
806 ret = copy_from_user(vf_data + *pos, buf, len);
807 if (ret) {
808 done = -EFAULT;
809 goto out_unlock;
810 }
811 *pos += len;
812 done = len;
813 migf->total_length += len;
814
815 ret = vf_qm_check_match(migf->hisi_acc_vdev, migf);
816 if (ret)
817 done = -EFAULT;
818 out_unlock:
819 mutex_unlock(&migf->lock);
820 return done;
821 }
822
823 static const struct file_operations hisi_acc_vf_resume_fops = {
824 .owner = THIS_MODULE,
825 .write = hisi_acc_vf_resume_write,
826 .release = hisi_acc_vf_release_file,
827 };
828
829 static struct hisi_acc_vf_migration_file *
hisi_acc_vf_pci_resume(struct hisi_acc_vf_core_device * hisi_acc_vdev)830 hisi_acc_vf_pci_resume(struct hisi_acc_vf_core_device *hisi_acc_vdev)
831 {
832 struct hisi_acc_vf_migration_file *migf;
833
834 migf = kzalloc(sizeof(*migf), GFP_KERNEL_ACCOUNT);
835 if (!migf)
836 return ERR_PTR(-ENOMEM);
837
838 migf->filp = anon_inode_getfile("hisi_acc_vf_mig", &hisi_acc_vf_resume_fops, migf,
839 O_WRONLY);
840 if (IS_ERR(migf->filp)) {
841 int err = PTR_ERR(migf->filp);
842
843 kfree(migf);
844 return ERR_PTR(err);
845 }
846
847 stream_open(migf->filp->f_inode, migf->filp);
848 mutex_init(&migf->lock);
849 migf->hisi_acc_vdev = hisi_acc_vdev;
850 return migf;
851 }
852
hisi_acc_vf_precopy_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)853 static long hisi_acc_vf_precopy_ioctl(struct file *filp,
854 unsigned int cmd, unsigned long arg)
855 {
856 struct hisi_acc_vf_migration_file *migf = filp->private_data;
857 struct hisi_acc_vf_core_device *hisi_acc_vdev = migf->hisi_acc_vdev;
858 loff_t *pos = &filp->f_pos;
859 struct vfio_precopy_info info;
860 unsigned long minsz;
861 int ret;
862
863 if (cmd != VFIO_MIG_GET_PRECOPY_INFO)
864 return -ENOTTY;
865
866 minsz = offsetofend(struct vfio_precopy_info, dirty_bytes);
867
868 if (copy_from_user(&info, (void __user *)arg, minsz))
869 return -EFAULT;
870 if (info.argsz < minsz)
871 return -EINVAL;
872
873 mutex_lock(&hisi_acc_vdev->state_mutex);
874 if (hisi_acc_vdev->mig_state != VFIO_DEVICE_STATE_PRE_COPY) {
875 mutex_unlock(&hisi_acc_vdev->state_mutex);
876 return -EINVAL;
877 }
878
879 mutex_lock(&migf->lock);
880
881 if (migf->disabled) {
882 ret = -ENODEV;
883 goto out;
884 }
885
886 if (*pos > migf->total_length) {
887 ret = -EINVAL;
888 goto out;
889 }
890
891 info.dirty_bytes = 0;
892 info.initial_bytes = migf->total_length - *pos;
893 mutex_unlock(&migf->lock);
894 mutex_unlock(&hisi_acc_vdev->state_mutex);
895
896 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
897 out:
898 mutex_unlock(&migf->lock);
899 mutex_unlock(&hisi_acc_vdev->state_mutex);
900 return ret;
901 }
902
hisi_acc_vf_save_read(struct file * filp,char __user * buf,size_t len,loff_t * pos)903 static ssize_t hisi_acc_vf_save_read(struct file *filp, char __user *buf, size_t len,
904 loff_t *pos)
905 {
906 struct hisi_acc_vf_migration_file *migf = filp->private_data;
907 ssize_t done = 0;
908 int ret;
909
910 if (pos)
911 return -ESPIPE;
912 pos = &filp->f_pos;
913
914 mutex_lock(&migf->lock);
915 if (*pos > migf->total_length) {
916 done = -EINVAL;
917 goto out_unlock;
918 }
919
920 if (migf->disabled) {
921 done = -ENODEV;
922 goto out_unlock;
923 }
924
925 len = min_t(size_t, migf->total_length - *pos, len);
926 if (len) {
927 u8 *vf_data = (u8 *)&migf->vf_data;
928
929 ret = copy_to_user(buf, vf_data + *pos, len);
930 if (ret) {
931 done = -EFAULT;
932 goto out_unlock;
933 }
934 *pos += len;
935 done = len;
936 }
937 out_unlock:
938 mutex_unlock(&migf->lock);
939 return done;
940 }
941
942 static const struct file_operations hisi_acc_vf_save_fops = {
943 .owner = THIS_MODULE,
944 .read = hisi_acc_vf_save_read,
945 .unlocked_ioctl = hisi_acc_vf_precopy_ioctl,
946 .compat_ioctl = compat_ptr_ioctl,
947 .release = hisi_acc_vf_release_file,
948 };
949
950 static struct hisi_acc_vf_migration_file *
hisi_acc_open_saving_migf(struct hisi_acc_vf_core_device * hisi_acc_vdev)951 hisi_acc_open_saving_migf(struct hisi_acc_vf_core_device *hisi_acc_vdev)
952 {
953 struct hisi_acc_vf_migration_file *migf;
954 int ret;
955
956 migf = kzalloc(sizeof(*migf), GFP_KERNEL_ACCOUNT);
957 if (!migf)
958 return ERR_PTR(-ENOMEM);
959
960 migf->filp = anon_inode_getfile("hisi_acc_vf_mig", &hisi_acc_vf_save_fops, migf,
961 O_RDONLY);
962 if (IS_ERR(migf->filp)) {
963 int err = PTR_ERR(migf->filp);
964
965 kfree(migf);
966 return ERR_PTR(err);
967 }
968
969 stream_open(migf->filp->f_inode, migf->filp);
970 mutex_init(&migf->lock);
971 migf->hisi_acc_vdev = hisi_acc_vdev;
972
973 ret = vf_qm_get_match_data(hisi_acc_vdev, &migf->vf_data);
974 if (ret) {
975 fput(migf->filp);
976 return ERR_PTR(ret);
977 }
978
979 return migf;
980 }
981
982 static struct hisi_acc_vf_migration_file *
hisi_acc_vf_pre_copy(struct hisi_acc_vf_core_device * hisi_acc_vdev)983 hisi_acc_vf_pre_copy(struct hisi_acc_vf_core_device *hisi_acc_vdev)
984 {
985 struct hisi_acc_vf_migration_file *migf;
986
987 migf = hisi_acc_open_saving_migf(hisi_acc_vdev);
988 if (IS_ERR(migf))
989 return migf;
990
991 migf->total_length = QM_MATCH_SIZE;
992 return migf;
993 }
994
995 static struct hisi_acc_vf_migration_file *
hisi_acc_vf_stop_copy(struct hisi_acc_vf_core_device * hisi_acc_vdev,bool open)996 hisi_acc_vf_stop_copy(struct hisi_acc_vf_core_device *hisi_acc_vdev, bool open)
997 {
998 int ret;
999 struct hisi_acc_vf_migration_file *migf = NULL;
1000
1001 if (open) {
1002 /*
1003 * Userspace didn't use PRECOPY support. Hence saving_migf
1004 * is not opened yet.
1005 */
1006 migf = hisi_acc_open_saving_migf(hisi_acc_vdev);
1007 if (IS_ERR(migf))
1008 return migf;
1009 } else {
1010 migf = hisi_acc_vdev->saving_migf;
1011 }
1012
1013 ret = vf_qm_state_save(hisi_acc_vdev, migf);
1014 if (ret)
1015 return ERR_PTR(ret);
1016
1017 return open ? migf : NULL;
1018 }
1019
hisi_acc_vf_stop_device(struct hisi_acc_vf_core_device * hisi_acc_vdev)1020 static int hisi_acc_vf_stop_device(struct hisi_acc_vf_core_device *hisi_acc_vdev)
1021 {
1022 struct device *dev = &hisi_acc_vdev->vf_dev->dev;
1023 struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
1024 int ret;
1025
1026 ret = vf_qm_func_stop(vf_qm);
1027 if (ret) {
1028 dev_err(dev, "failed to stop QM VF function!\n");
1029 return ret;
1030 }
1031
1032 ret = hisi_acc_check_int_state(hisi_acc_vdev);
1033 if (ret) {
1034 dev_err(dev, "failed to check QM INT state!\n");
1035 return ret;
1036 }
1037
1038 ret = vf_qm_cache_wb(vf_qm);
1039 if (ret) {
1040 dev_err(dev, "failed to writeback QM cache!\n");
1041 return ret;
1042 }
1043
1044 return 0;
1045 }
1046
1047 static struct file *
hisi_acc_vf_set_device_state(struct hisi_acc_vf_core_device * hisi_acc_vdev,u32 new)1048 hisi_acc_vf_set_device_state(struct hisi_acc_vf_core_device *hisi_acc_vdev,
1049 u32 new)
1050 {
1051 u32 cur = hisi_acc_vdev->mig_state;
1052 int ret;
1053
1054 if (cur == VFIO_DEVICE_STATE_RUNNING && new == VFIO_DEVICE_STATE_PRE_COPY) {
1055 struct hisi_acc_vf_migration_file *migf;
1056
1057 migf = hisi_acc_vf_pre_copy(hisi_acc_vdev);
1058 if (IS_ERR(migf))
1059 return ERR_CAST(migf);
1060 get_file(migf->filp);
1061 hisi_acc_vdev->saving_migf = migf;
1062 return migf->filp;
1063 }
1064
1065 if (cur == VFIO_DEVICE_STATE_PRE_COPY && new == VFIO_DEVICE_STATE_STOP_COPY) {
1066 struct hisi_acc_vf_migration_file *migf;
1067
1068 ret = hisi_acc_vf_stop_device(hisi_acc_vdev);
1069 if (ret)
1070 return ERR_PTR(ret);
1071
1072 migf = hisi_acc_vf_stop_copy(hisi_acc_vdev, false);
1073 if (IS_ERR(migf))
1074 return ERR_CAST(migf);
1075
1076 return NULL;
1077 }
1078
1079 if (cur == VFIO_DEVICE_STATE_RUNNING && new == VFIO_DEVICE_STATE_STOP) {
1080 ret = hisi_acc_vf_stop_device(hisi_acc_vdev);
1081 if (ret)
1082 return ERR_PTR(ret);
1083 return NULL;
1084 }
1085
1086 if (cur == VFIO_DEVICE_STATE_STOP && new == VFIO_DEVICE_STATE_STOP_COPY) {
1087 struct hisi_acc_vf_migration_file *migf;
1088
1089 migf = hisi_acc_vf_stop_copy(hisi_acc_vdev, true);
1090 if (IS_ERR(migf))
1091 return ERR_CAST(migf);
1092 get_file(migf->filp);
1093 hisi_acc_vdev->saving_migf = migf;
1094 return migf->filp;
1095 }
1096
1097 if ((cur == VFIO_DEVICE_STATE_STOP_COPY && new == VFIO_DEVICE_STATE_STOP)) {
1098 hisi_acc_vf_disable_fds(hisi_acc_vdev);
1099 return NULL;
1100 }
1101
1102 if (cur == VFIO_DEVICE_STATE_STOP && new == VFIO_DEVICE_STATE_RESUMING) {
1103 struct hisi_acc_vf_migration_file *migf;
1104
1105 migf = hisi_acc_vf_pci_resume(hisi_acc_vdev);
1106 if (IS_ERR(migf))
1107 return ERR_CAST(migf);
1108 get_file(migf->filp);
1109 hisi_acc_vdev->resuming_migf = migf;
1110 return migf->filp;
1111 }
1112
1113 if (cur == VFIO_DEVICE_STATE_RESUMING && new == VFIO_DEVICE_STATE_STOP) {
1114 ret = hisi_acc_vf_load_state(hisi_acc_vdev);
1115 if (ret)
1116 return ERR_PTR(ret);
1117 hisi_acc_vf_disable_fds(hisi_acc_vdev);
1118 return NULL;
1119 }
1120
1121 if (cur == VFIO_DEVICE_STATE_PRE_COPY && new == VFIO_DEVICE_STATE_RUNNING) {
1122 hisi_acc_vf_disable_fds(hisi_acc_vdev);
1123 return NULL;
1124 }
1125
1126 if (cur == VFIO_DEVICE_STATE_STOP && new == VFIO_DEVICE_STATE_RUNNING) {
1127 hisi_acc_vf_start_device(hisi_acc_vdev);
1128 return NULL;
1129 }
1130
1131 /*
1132 * vfio_mig_get_next_state() does not use arcs other than the above
1133 */
1134 WARN_ON(true);
1135 return ERR_PTR(-EINVAL);
1136 }
1137
1138 static struct file *
hisi_acc_vfio_pci_set_device_state(struct vfio_device * vdev,enum vfio_device_mig_state new_state)1139 hisi_acc_vfio_pci_set_device_state(struct vfio_device *vdev,
1140 enum vfio_device_mig_state new_state)
1141 {
1142 struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
1143 enum vfio_device_mig_state next_state;
1144 struct file *res = NULL;
1145 int ret;
1146
1147 mutex_lock(&hisi_acc_vdev->state_mutex);
1148 while (new_state != hisi_acc_vdev->mig_state) {
1149 ret = vfio_mig_get_next_state(vdev,
1150 hisi_acc_vdev->mig_state,
1151 new_state, &next_state);
1152 if (ret) {
1153 res = ERR_PTR(-EINVAL);
1154 break;
1155 }
1156
1157 res = hisi_acc_vf_set_device_state(hisi_acc_vdev, next_state);
1158 if (IS_ERR(res))
1159 break;
1160 hisi_acc_vdev->mig_state = next_state;
1161 if (WARN_ON(res && new_state != hisi_acc_vdev->mig_state)) {
1162 fput(res);
1163 res = ERR_PTR(-EINVAL);
1164 break;
1165 }
1166 }
1167 mutex_unlock(&hisi_acc_vdev->state_mutex);
1168 return res;
1169 }
1170
1171 static int
hisi_acc_vfio_pci_get_data_size(struct vfio_device * vdev,unsigned long * stop_copy_length)1172 hisi_acc_vfio_pci_get_data_size(struct vfio_device *vdev,
1173 unsigned long *stop_copy_length)
1174 {
1175 *stop_copy_length = sizeof(struct acc_vf_data);
1176 return 0;
1177 }
1178
1179 static int
hisi_acc_vfio_pci_get_device_state(struct vfio_device * vdev,enum vfio_device_mig_state * curr_state)1180 hisi_acc_vfio_pci_get_device_state(struct vfio_device *vdev,
1181 enum vfio_device_mig_state *curr_state)
1182 {
1183 struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
1184
1185 mutex_lock(&hisi_acc_vdev->state_mutex);
1186 *curr_state = hisi_acc_vdev->mig_state;
1187 mutex_unlock(&hisi_acc_vdev->state_mutex);
1188 return 0;
1189 }
1190
hisi_acc_vf_pci_aer_reset_done(struct pci_dev * pdev)1191 static void hisi_acc_vf_pci_aer_reset_done(struct pci_dev *pdev)
1192 {
1193 struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_drvdata(pdev);
1194
1195 if (hisi_acc_vdev->core_device.vdev.migration_flags !=
1196 VFIO_MIGRATION_STOP_COPY)
1197 return;
1198
1199 mutex_lock(&hisi_acc_vdev->state_mutex);
1200 hisi_acc_vf_reset(hisi_acc_vdev);
1201 mutex_unlock(&hisi_acc_vdev->state_mutex);
1202 }
1203
hisi_acc_vf_qm_init(struct hisi_acc_vf_core_device * hisi_acc_vdev)1204 static int hisi_acc_vf_qm_init(struct hisi_acc_vf_core_device *hisi_acc_vdev)
1205 {
1206 struct vfio_pci_core_device *vdev = &hisi_acc_vdev->core_device;
1207 struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
1208 struct hisi_qm *pf_qm = hisi_acc_vdev->pf_qm;
1209 struct pci_dev *vf_dev = vdev->pdev;
1210 u32 val;
1211
1212 val = readl(pf_qm->io_base + QM_MIG_REGION_SEL);
1213 if (pf_qm->ver > QM_HW_V3 && (val & QM_MIG_REGION_EN))
1214 hisi_acc_vdev->drv_mode = HW_ACC_MIG_PF_CTRL;
1215 else
1216 hisi_acc_vdev->drv_mode = HW_ACC_MIG_VF_CTRL;
1217
1218 if (hisi_acc_vdev->drv_mode == HW_ACC_MIG_PF_CTRL) {
1219 /*
1220 * On hardware platforms greater than QM_HW_V3, the migration function
1221 * register is placed in the BAR2 configuration region of the PF,
1222 * and each VF device occupies 8KB of configuration space.
1223 */
1224 vf_qm->io_base = pf_qm->io_base + QM_MIG_REGION_OFFSET +
1225 hisi_acc_vdev->vf_id * QM_MIG_REGION_SIZE;
1226 } else {
1227 /*
1228 * ACC VF dev BAR2 region consists of both functional register space
1229 * and migration control register space. For migration to work, we
1230 * need access to both. Hence, we map the entire BAR2 region here.
1231 * But unnecessarily exposing the migration BAR region to the Guest
1232 * has the potential to prevent/corrupt the Guest migration. Hence,
1233 * we restrict access to the migration control space from
1234 * Guest(Please see mmap/ioctl/read/write override functions).
1235 *
1236 * Please note that it is OK to expose the entire VF BAR if migration
1237 * is not supported or required as this cannot affect the ACC PF
1238 * configurations.
1239 *
1240 * Also the HiSilicon ACC VF devices supported by this driver on
1241 * HiSilicon hardware platforms are integrated end point devices
1242 * and the platform lacks the capability to perform any PCIe P2P
1243 * between these devices.
1244 */
1245
1246 vf_qm->io_base =
1247 ioremap(pci_resource_start(vf_dev, VFIO_PCI_BAR2_REGION_INDEX),
1248 pci_resource_len(vf_dev, VFIO_PCI_BAR2_REGION_INDEX));
1249 if (!vf_qm->io_base)
1250 return -EIO;
1251 }
1252 vf_qm->fun_type = QM_HW_VF;
1253 vf_qm->ver = pf_qm->ver;
1254 vf_qm->pdev = vf_dev;
1255 mutex_init(&vf_qm->mailbox_lock);
1256
1257 return 0;
1258 }
1259
hisi_acc_get_pf_qm(struct pci_dev * pdev)1260 static struct hisi_qm *hisi_acc_get_pf_qm(struct pci_dev *pdev)
1261 {
1262 struct hisi_qm *pf_qm;
1263 struct pci_driver *pf_driver;
1264
1265 if (!pdev->is_virtfn)
1266 return NULL;
1267
1268 switch (pdev->device) {
1269 case PCI_DEVICE_ID_HUAWEI_SEC_VF:
1270 pf_driver = hisi_sec_get_pf_driver();
1271 break;
1272 case PCI_DEVICE_ID_HUAWEI_HPRE_VF:
1273 pf_driver = hisi_hpre_get_pf_driver();
1274 break;
1275 case PCI_DEVICE_ID_HUAWEI_ZIP_VF:
1276 pf_driver = hisi_zip_get_pf_driver();
1277 break;
1278 default:
1279 return NULL;
1280 }
1281
1282 if (!pf_driver)
1283 return NULL;
1284
1285 pf_qm = pci_iov_get_pf_drvdata(pdev, pf_driver);
1286
1287 return !IS_ERR(pf_qm) ? pf_qm : NULL;
1288 }
1289
hisi_acc_get_resource_len(struct vfio_pci_core_device * vdev,unsigned int index)1290 static size_t hisi_acc_get_resource_len(struct vfio_pci_core_device *vdev,
1291 unsigned int index)
1292 {
1293 struct hisi_acc_vf_core_device *hisi_acc_vdev =
1294 hisi_acc_drvdata(vdev->pdev);
1295
1296 /*
1297 * On the old HW_ACC_MIG_VF_CTRL mode device, the ACC VF device
1298 * BAR2 region encompasses both functional register space
1299 * and migration control register space.
1300 * only the functional region should be report to Guest.
1301 */
1302 if (hisi_acc_vdev->drv_mode == HW_ACC_MIG_VF_CTRL)
1303 return (pci_resource_len(vdev->pdev, index) >> 1);
1304 /*
1305 * On the new HW device, the migration control register
1306 * has been moved to the PF device BAR2 region.
1307 * The VF device BAR2 is entirely functional register space.
1308 */
1309 return pci_resource_len(vdev->pdev, index);
1310 }
1311
hisi_acc_pci_rw_access_check(struct vfio_device * core_vdev,size_t count,loff_t * ppos,size_t * new_count)1312 static int hisi_acc_pci_rw_access_check(struct vfio_device *core_vdev,
1313 size_t count, loff_t *ppos,
1314 size_t *new_count)
1315 {
1316 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
1317 struct vfio_pci_core_device *vdev =
1318 container_of(core_vdev, struct vfio_pci_core_device, vdev);
1319
1320 if (index == VFIO_PCI_BAR2_REGION_INDEX) {
1321 loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
1322 resource_size_t end;
1323
1324 end = hisi_acc_get_resource_len(vdev, index);
1325 /* Check if access is for migration control region */
1326 if (pos >= end)
1327 return -EINVAL;
1328
1329 *new_count = min(count, (size_t)(end - pos));
1330 }
1331
1332 return 0;
1333 }
1334
hisi_acc_vfio_pci_mmap(struct vfio_device * core_vdev,struct vm_area_struct * vma)1335 static int hisi_acc_vfio_pci_mmap(struct vfio_device *core_vdev,
1336 struct vm_area_struct *vma)
1337 {
1338 struct vfio_pci_core_device *vdev =
1339 container_of(core_vdev, struct vfio_pci_core_device, vdev);
1340 unsigned int index;
1341
1342 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
1343 if (index == VFIO_PCI_BAR2_REGION_INDEX) {
1344 u64 req_len, pgoff, req_start;
1345 resource_size_t end;
1346
1347 end = hisi_acc_get_resource_len(vdev, index);
1348 req_len = vma->vm_end - vma->vm_start;
1349 pgoff = vma->vm_pgoff &
1350 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
1351 req_start = pgoff << PAGE_SHIFT;
1352
1353 if (req_start + req_len > end)
1354 return -EINVAL;
1355 }
1356
1357 return vfio_pci_core_mmap(core_vdev, vma);
1358 }
1359
hisi_acc_vfio_pci_write(struct vfio_device * core_vdev,const char __user * buf,size_t count,loff_t * ppos)1360 static ssize_t hisi_acc_vfio_pci_write(struct vfio_device *core_vdev,
1361 const char __user *buf, size_t count,
1362 loff_t *ppos)
1363 {
1364 size_t new_count = count;
1365 int ret;
1366
1367 ret = hisi_acc_pci_rw_access_check(core_vdev, count, ppos, &new_count);
1368 if (ret)
1369 return ret;
1370
1371 return vfio_pci_core_write(core_vdev, buf, new_count, ppos);
1372 }
1373
hisi_acc_vfio_pci_read(struct vfio_device * core_vdev,char __user * buf,size_t count,loff_t * ppos)1374 static ssize_t hisi_acc_vfio_pci_read(struct vfio_device *core_vdev,
1375 char __user *buf, size_t count,
1376 loff_t *ppos)
1377 {
1378 size_t new_count = count;
1379 int ret;
1380
1381 ret = hisi_acc_pci_rw_access_check(core_vdev, count, ppos, &new_count);
1382 if (ret)
1383 return ret;
1384
1385 return vfio_pci_core_read(core_vdev, buf, new_count, ppos);
1386 }
1387
hisi_acc_vfio_ioctl_get_region(struct vfio_device * core_vdev,struct vfio_region_info * info,struct vfio_info_cap * caps)1388 static int hisi_acc_vfio_ioctl_get_region(struct vfio_device *core_vdev,
1389 struct vfio_region_info *info,
1390 struct vfio_info_cap *caps)
1391 {
1392 struct vfio_pci_core_device *vdev =
1393 container_of(core_vdev, struct vfio_pci_core_device, vdev);
1394
1395 if (info->index != VFIO_PCI_BAR2_REGION_INDEX)
1396 return vfio_pci_ioctl_get_region_info(core_vdev, info, caps);
1397
1398 info->offset = VFIO_PCI_INDEX_TO_OFFSET(info->index);
1399
1400 info->size = hisi_acc_get_resource_len(vdev, info->index);
1401
1402 info->flags = VFIO_REGION_INFO_FLAG_READ | VFIO_REGION_INFO_FLAG_WRITE |
1403 VFIO_REGION_INFO_FLAG_MMAP;
1404 return 0;
1405 }
1406
hisi_acc_vf_debug_check(struct seq_file * seq,struct vfio_device * vdev)1407 static int hisi_acc_vf_debug_check(struct seq_file *seq, struct vfio_device *vdev)
1408 {
1409 struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
1410 struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
1411 int ret;
1412
1413 lockdep_assert_held(&hisi_acc_vdev->open_mutex);
1414 /*
1415 * When the device is not opened, the io_base is not mapped.
1416 * The driver cannot perform device read and write operations.
1417 */
1418 if (!hisi_acc_vdev->dev_opened) {
1419 seq_puts(seq, "device not opened!\n");
1420 return -EINVAL;
1421 }
1422
1423 ret = qm_wait_dev_not_ready(vf_qm);
1424 if (ret) {
1425 seq_puts(seq, "VF device not ready!\n");
1426 return ret;
1427 }
1428
1429 return 0;
1430 }
1431
hisi_acc_vf_debug_cmd(struct seq_file * seq,void * data)1432 static int hisi_acc_vf_debug_cmd(struct seq_file *seq, void *data)
1433 {
1434 struct device *vf_dev = seq->private;
1435 struct vfio_pci_core_device *core_device = dev_get_drvdata(vf_dev);
1436 struct vfio_device *vdev = &core_device->vdev;
1437 struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
1438 struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
1439 u64 value;
1440 int ret;
1441
1442 mutex_lock(&hisi_acc_vdev->open_mutex);
1443 ret = hisi_acc_vf_debug_check(seq, vdev);
1444 if (ret) {
1445 mutex_unlock(&hisi_acc_vdev->open_mutex);
1446 return ret;
1447 }
1448
1449 value = readl(vf_qm->io_base + QM_MB_CMD_SEND_BASE);
1450 if (value == QM_MB_CMD_NOT_READY) {
1451 mutex_unlock(&hisi_acc_vdev->open_mutex);
1452 seq_puts(seq, "mailbox cmd channel not ready!\n");
1453 return -EINVAL;
1454 }
1455 mutex_unlock(&hisi_acc_vdev->open_mutex);
1456 seq_puts(seq, "mailbox cmd channel ready!\n");
1457
1458 return 0;
1459 }
1460
hisi_acc_vf_dev_read(struct seq_file * seq,void * data)1461 static int hisi_acc_vf_dev_read(struct seq_file *seq, void *data)
1462 {
1463 struct device *vf_dev = seq->private;
1464 struct vfio_pci_core_device *core_device = dev_get_drvdata(vf_dev);
1465 struct vfio_device *vdev = &core_device->vdev;
1466 struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
1467 size_t vf_data_sz = offsetofend(struct acc_vf_data, padding);
1468 struct acc_vf_data *vf_data;
1469 int ret;
1470
1471 mutex_lock(&hisi_acc_vdev->open_mutex);
1472 ret = hisi_acc_vf_debug_check(seq, vdev);
1473 if (ret) {
1474 mutex_unlock(&hisi_acc_vdev->open_mutex);
1475 return ret;
1476 }
1477
1478 mutex_lock(&hisi_acc_vdev->state_mutex);
1479 vf_data = kzalloc(sizeof(*vf_data), GFP_KERNEL);
1480 if (!vf_data) {
1481 ret = -ENOMEM;
1482 goto mutex_release;
1483 }
1484
1485 vf_data->vf_qm_state = hisi_acc_vdev->vf_qm_state;
1486 ret = vf_qm_read_data(&hisi_acc_vdev->vf_qm, vf_data);
1487 if (ret)
1488 goto migf_err;
1489
1490 seq_hex_dump(seq, "Dev Data:", DUMP_PREFIX_OFFSET, 16, 1,
1491 (const void *)vf_data, vf_data_sz, false);
1492
1493 seq_printf(seq,
1494 "guest driver load: %u\n"
1495 "data size: %lu\n",
1496 hisi_acc_vdev->vf_qm_state,
1497 sizeof(struct acc_vf_data));
1498
1499 migf_err:
1500 kfree(vf_data);
1501 mutex_release:
1502 mutex_unlock(&hisi_acc_vdev->state_mutex);
1503 mutex_unlock(&hisi_acc_vdev->open_mutex);
1504
1505 return ret;
1506 }
1507
hisi_acc_vf_migf_read(struct seq_file * seq,void * data)1508 static int hisi_acc_vf_migf_read(struct seq_file *seq, void *data)
1509 {
1510 struct device *vf_dev = seq->private;
1511 struct vfio_pci_core_device *core_device = dev_get_drvdata(vf_dev);
1512 struct vfio_device *vdev = &core_device->vdev;
1513 struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
1514 size_t vf_data_sz = offsetofend(struct acc_vf_data, padding);
1515 struct hisi_acc_vf_migration_file *debug_migf = hisi_acc_vdev->debug_migf;
1516
1517 /* Check whether the live migration operation has been performed */
1518 if (debug_migf->total_length < QM_MATCH_SIZE) {
1519 seq_puts(seq, "device not migrated!\n");
1520 return -EAGAIN;
1521 }
1522
1523 seq_hex_dump(seq, "Mig Data:", DUMP_PREFIX_OFFSET, 16, 1,
1524 (const void *)&debug_migf->vf_data, vf_data_sz, false);
1525 seq_printf(seq, "migrate data length: %lu\n", debug_migf->total_length);
1526
1527 return 0;
1528 }
1529
hisi_acc_vfio_pci_open_device(struct vfio_device * core_vdev)1530 static int hisi_acc_vfio_pci_open_device(struct vfio_device *core_vdev)
1531 {
1532 struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(core_vdev);
1533 struct vfio_pci_core_device *vdev = &hisi_acc_vdev->core_device;
1534 int ret;
1535
1536 ret = vfio_pci_core_enable(vdev);
1537 if (ret)
1538 return ret;
1539
1540 if (core_vdev->mig_ops) {
1541 mutex_lock(&hisi_acc_vdev->open_mutex);
1542 ret = hisi_acc_vf_qm_init(hisi_acc_vdev);
1543 if (ret) {
1544 mutex_unlock(&hisi_acc_vdev->open_mutex);
1545 vfio_pci_core_disable(vdev);
1546 return ret;
1547 }
1548 hisi_acc_vdev->mig_state = VFIO_DEVICE_STATE_RUNNING;
1549 hisi_acc_vdev->dev_opened = true;
1550 mutex_unlock(&hisi_acc_vdev->open_mutex);
1551 }
1552
1553 vfio_pci_core_finish_enable(vdev);
1554 return 0;
1555 }
1556
hisi_acc_vfio_pci_close_device(struct vfio_device * core_vdev)1557 static void hisi_acc_vfio_pci_close_device(struct vfio_device *core_vdev)
1558 {
1559 struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(core_vdev);
1560 struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
1561
1562 hisi_acc_vf_disable_fds(hisi_acc_vdev);
1563 mutex_lock(&hisi_acc_vdev->open_mutex);
1564 hisi_acc_vdev->dev_opened = false;
1565 if (hisi_acc_vdev->drv_mode == HW_ACC_MIG_VF_CTRL)
1566 iounmap(vf_qm->io_base);
1567 mutex_unlock(&hisi_acc_vdev->open_mutex);
1568 vfio_pci_core_close_device(core_vdev);
1569 }
1570
1571 static const struct vfio_migration_ops hisi_acc_vfio_pci_migrn_state_ops = {
1572 .migration_set_state = hisi_acc_vfio_pci_set_device_state,
1573 .migration_get_state = hisi_acc_vfio_pci_get_device_state,
1574 .migration_get_data_size = hisi_acc_vfio_pci_get_data_size,
1575 };
1576
hisi_acc_vfio_pci_migrn_init_dev(struct vfio_device * core_vdev)1577 static int hisi_acc_vfio_pci_migrn_init_dev(struct vfio_device *core_vdev)
1578 {
1579 struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(core_vdev);
1580 struct pci_dev *pdev = to_pci_dev(core_vdev->dev);
1581 struct hisi_qm *pf_qm = hisi_acc_get_pf_qm(pdev);
1582
1583 hisi_acc_vdev->vf_id = pci_iov_vf_id(pdev) + 1;
1584 hisi_acc_vdev->pf_qm = pf_qm;
1585 hisi_acc_vdev->vf_dev = pdev;
1586 hisi_acc_vdev->vf_qm_state = QM_NOT_READY;
1587 mutex_init(&hisi_acc_vdev->state_mutex);
1588 mutex_init(&hisi_acc_vdev->open_mutex);
1589
1590 core_vdev->migration_flags = VFIO_MIGRATION_STOP_COPY | VFIO_MIGRATION_PRE_COPY;
1591 core_vdev->mig_ops = &hisi_acc_vfio_pci_migrn_state_ops;
1592
1593 return vfio_pci_core_init_dev(core_vdev);
1594 }
1595
1596 static const struct vfio_device_ops hisi_acc_vfio_pci_migrn_ops = {
1597 .name = "hisi-acc-vfio-pci-migration",
1598 .init = hisi_acc_vfio_pci_migrn_init_dev,
1599 .release = vfio_pci_core_release_dev,
1600 .open_device = hisi_acc_vfio_pci_open_device,
1601 .close_device = hisi_acc_vfio_pci_close_device,
1602 .ioctl = vfio_pci_core_ioctl,
1603 .get_region_info_caps = hisi_acc_vfio_ioctl_get_region,
1604 .device_feature = vfio_pci_core_ioctl_feature,
1605 .read = hisi_acc_vfio_pci_read,
1606 .write = hisi_acc_vfio_pci_write,
1607 .mmap = hisi_acc_vfio_pci_mmap,
1608 .request = vfio_pci_core_request,
1609 .match = vfio_pci_core_match,
1610 .match_token_uuid = vfio_pci_core_match_token_uuid,
1611 .bind_iommufd = vfio_iommufd_physical_bind,
1612 .unbind_iommufd = vfio_iommufd_physical_unbind,
1613 .attach_ioas = vfio_iommufd_physical_attach_ioas,
1614 .detach_ioas = vfio_iommufd_physical_detach_ioas,
1615 };
1616
1617 static const struct vfio_device_ops hisi_acc_vfio_pci_ops = {
1618 .name = "hisi-acc-vfio-pci",
1619 .init = vfio_pci_core_init_dev,
1620 .release = vfio_pci_core_release_dev,
1621 .open_device = hisi_acc_vfio_pci_open_device,
1622 .close_device = vfio_pci_core_close_device,
1623 .ioctl = vfio_pci_core_ioctl,
1624 .get_region_info_caps = vfio_pci_ioctl_get_region_info,
1625 .device_feature = vfio_pci_core_ioctl_feature,
1626 .read = vfio_pci_core_read,
1627 .write = vfio_pci_core_write,
1628 .mmap = vfio_pci_core_mmap,
1629 .request = vfio_pci_core_request,
1630 .match = vfio_pci_core_match,
1631 .match_token_uuid = vfio_pci_core_match_token_uuid,
1632 .bind_iommufd = vfio_iommufd_physical_bind,
1633 .unbind_iommufd = vfio_iommufd_physical_unbind,
1634 .attach_ioas = vfio_iommufd_physical_attach_ioas,
1635 .detach_ioas = vfio_iommufd_physical_detach_ioas,
1636 };
1637
hisi_acc_vfio_debug_init(struct hisi_acc_vf_core_device * hisi_acc_vdev)1638 static void hisi_acc_vfio_debug_init(struct hisi_acc_vf_core_device *hisi_acc_vdev)
1639 {
1640 struct vfio_device *vdev = &hisi_acc_vdev->core_device.vdev;
1641 struct hisi_acc_vf_migration_file *migf;
1642 struct dentry *vfio_dev_migration;
1643 struct dentry *vfio_hisi_acc;
1644 struct device *dev = vdev->dev;
1645
1646 if (!debugfs_initialized() ||
1647 !IS_ENABLED(CONFIG_VFIO_DEBUGFS))
1648 return;
1649
1650 if (vdev->ops != &hisi_acc_vfio_pci_migrn_ops)
1651 return;
1652
1653 vfio_dev_migration = debugfs_lookup("migration", vdev->debug_root);
1654 if (!vfio_dev_migration) {
1655 dev_err(dev, "failed to lookup migration debugfs file!\n");
1656 return;
1657 }
1658
1659 migf = kzalloc(sizeof(*migf), GFP_KERNEL);
1660 if (!migf) {
1661 dput(vfio_dev_migration);
1662 return;
1663 }
1664 hisi_acc_vdev->debug_migf = migf;
1665
1666 vfio_hisi_acc = debugfs_create_dir("hisi_acc", vfio_dev_migration);
1667 debugfs_create_devm_seqfile(dev, "dev_data", vfio_hisi_acc,
1668 hisi_acc_vf_dev_read);
1669 debugfs_create_devm_seqfile(dev, "migf_data", vfio_hisi_acc,
1670 hisi_acc_vf_migf_read);
1671 debugfs_create_devm_seqfile(dev, "cmd_state", vfio_hisi_acc,
1672 hisi_acc_vf_debug_cmd);
1673
1674 dput(vfio_dev_migration);
1675 }
1676
hisi_acc_vf_debugfs_exit(struct hisi_acc_vf_core_device * hisi_acc_vdev)1677 static void hisi_acc_vf_debugfs_exit(struct hisi_acc_vf_core_device *hisi_acc_vdev)
1678 {
1679 kfree(hisi_acc_vdev->debug_migf);
1680 hisi_acc_vdev->debug_migf = NULL;
1681 }
1682
hisi_acc_vfio_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)1683 static int hisi_acc_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1684 {
1685 struct hisi_acc_vf_core_device *hisi_acc_vdev;
1686 const struct vfio_device_ops *ops = &hisi_acc_vfio_pci_ops;
1687 struct hisi_qm *pf_qm;
1688 int vf_id;
1689 int ret;
1690
1691 pf_qm = hisi_acc_get_pf_qm(pdev);
1692 if (pf_qm && pf_qm->ver >= QM_HW_V3) {
1693 vf_id = pci_iov_vf_id(pdev);
1694 if (vf_id >= 0)
1695 ops = &hisi_acc_vfio_pci_migrn_ops;
1696 else
1697 pci_warn(pdev, "migration support failed, continue with generic interface\n");
1698 }
1699
1700 hisi_acc_vdev = vfio_alloc_device(hisi_acc_vf_core_device,
1701 core_device.vdev, &pdev->dev, ops);
1702 if (IS_ERR(hisi_acc_vdev))
1703 return PTR_ERR(hisi_acc_vdev);
1704
1705 dev_set_drvdata(&pdev->dev, &hisi_acc_vdev->core_device);
1706 ret = vfio_pci_core_register_device(&hisi_acc_vdev->core_device);
1707 if (ret)
1708 goto out_put_vdev;
1709
1710 hisi_acc_vfio_debug_init(hisi_acc_vdev);
1711 return 0;
1712
1713 out_put_vdev:
1714 vfio_put_device(&hisi_acc_vdev->core_device.vdev);
1715 return ret;
1716 }
1717
hisi_acc_vfio_pci_remove(struct pci_dev * pdev)1718 static void hisi_acc_vfio_pci_remove(struct pci_dev *pdev)
1719 {
1720 struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_drvdata(pdev);
1721
1722 vfio_pci_core_unregister_device(&hisi_acc_vdev->core_device);
1723 hisi_acc_vf_debugfs_exit(hisi_acc_vdev);
1724 vfio_put_device(&hisi_acc_vdev->core_device.vdev);
1725 }
1726
1727 static const struct pci_device_id hisi_acc_vfio_pci_table[] = {
1728 { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_HUAWEI_SEC_VF) },
1729 { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_HUAWEI_HPRE_VF) },
1730 { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_HUAWEI_ZIP_VF) },
1731 { }
1732 };
1733
1734 MODULE_DEVICE_TABLE(pci, hisi_acc_vfio_pci_table);
1735
1736 static const struct pci_error_handlers hisi_acc_vf_err_handlers = {
1737 .reset_done = hisi_acc_vf_pci_aer_reset_done,
1738 .error_detected = vfio_pci_core_aer_err_detected,
1739 };
1740
1741 static struct pci_driver hisi_acc_vfio_pci_driver = {
1742 .name = KBUILD_MODNAME,
1743 .id_table = hisi_acc_vfio_pci_table,
1744 .probe = hisi_acc_vfio_pci_probe,
1745 .remove = hisi_acc_vfio_pci_remove,
1746 .err_handler = &hisi_acc_vf_err_handlers,
1747 .driver_managed_dma = true,
1748 };
1749
1750 module_pci_driver(hisi_acc_vfio_pci_driver);
1751
1752 MODULE_LICENSE("GPL v2");
1753 MODULE_AUTHOR("Liu Longfang <liulongfang@huawei.com>");
1754 MODULE_AUTHOR("Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>");
1755 MODULE_DESCRIPTION("HiSilicon VFIO PCI - VFIO PCI driver with live migration support for HiSilicon ACC device family");
1756