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_get_regs(struct hisi_qm * qm,struct acc_vf_data * vf_data)128 static int qm_get_regs(struct hisi_qm *qm, struct acc_vf_data *vf_data)
129 {
130 struct device *dev = &qm->pdev->dev;
131 int ret;
132
133 ret = qm_read_regs(qm, QM_VF_AEQ_INT_MASK, &vf_data->aeq_int_mask, 1);
134 if (ret) {
135 dev_err(dev, "failed to read QM_VF_AEQ_INT_MASK\n");
136 return ret;
137 }
138
139 ret = qm_read_regs(qm, QM_VF_EQ_INT_MASK, &vf_data->eq_int_mask, 1);
140 if (ret) {
141 dev_err(dev, "failed to read QM_VF_EQ_INT_MASK\n");
142 return ret;
143 }
144
145 ret = qm_read_regs(qm, QM_IFC_INT_SOURCE_V,
146 &vf_data->ifc_int_source, 1);
147 if (ret) {
148 dev_err(dev, "failed to read QM_IFC_INT_SOURCE_V\n");
149 return ret;
150 }
151
152 ret = qm_read_regs(qm, QM_IFC_INT_MASK, &vf_data->ifc_int_mask, 1);
153 if (ret) {
154 dev_err(dev, "failed to read QM_IFC_INT_MASK\n");
155 return ret;
156 }
157
158 ret = qm_read_regs(qm, QM_IFC_INT_SET_V, &vf_data->ifc_int_set, 1);
159 if (ret) {
160 dev_err(dev, "failed to read QM_IFC_INT_SET_V\n");
161 return ret;
162 }
163
164 ret = qm_read_regs(qm, QM_PAGE_SIZE, &vf_data->page_size, 1);
165 if (ret) {
166 dev_err(dev, "failed to read QM_PAGE_SIZE\n");
167 return ret;
168 }
169
170 /* QM_EQC_DW has 7 regs */
171 ret = qm_read_regs(qm, QM_EQC_DW0, vf_data->qm_eqc_dw, 7);
172 if (ret) {
173 dev_err(dev, "failed to read QM_EQC_DW\n");
174 return ret;
175 }
176
177 /* QM_AEQC_DW has 7 regs */
178 ret = qm_read_regs(qm, QM_AEQC_DW0, vf_data->qm_aeqc_dw, 7);
179 if (ret) {
180 dev_err(dev, "failed to read QM_AEQC_DW\n");
181 return ret;
182 }
183
184 return 0;
185 }
186
qm_set_regs(struct hisi_qm * qm,struct acc_vf_data * vf_data)187 static int qm_set_regs(struct hisi_qm *qm, struct acc_vf_data *vf_data)
188 {
189 struct device *dev = &qm->pdev->dev;
190 int ret;
191
192 /* Check VF state */
193 if (unlikely(hisi_qm_wait_mb_ready(qm))) {
194 dev_err(&qm->pdev->dev, "QM device is not ready to write\n");
195 return -EBUSY;
196 }
197
198 ret = qm_write_regs(qm, QM_VF_AEQ_INT_MASK, &vf_data->aeq_int_mask, 1);
199 if (ret) {
200 dev_err(dev, "failed to write QM_VF_AEQ_INT_MASK\n");
201 return ret;
202 }
203
204 ret = qm_write_regs(qm, QM_VF_EQ_INT_MASK, &vf_data->eq_int_mask, 1);
205 if (ret) {
206 dev_err(dev, "failed to write QM_VF_EQ_INT_MASK\n");
207 return ret;
208 }
209
210 ret = qm_write_regs(qm, QM_IFC_INT_SOURCE_V,
211 &vf_data->ifc_int_source, 1);
212 if (ret) {
213 dev_err(dev, "failed to write QM_IFC_INT_SOURCE_V\n");
214 return ret;
215 }
216
217 ret = qm_write_regs(qm, QM_IFC_INT_MASK, &vf_data->ifc_int_mask, 1);
218 if (ret) {
219 dev_err(dev, "failed to write QM_IFC_INT_MASK\n");
220 return ret;
221 }
222
223 ret = qm_write_regs(qm, QM_IFC_INT_SET_V, &vf_data->ifc_int_set, 1);
224 if (ret) {
225 dev_err(dev, "failed to write QM_IFC_INT_SET_V\n");
226 return ret;
227 }
228
229 ret = qm_write_regs(qm, QM_QUE_ISO_CFG_V, &vf_data->que_iso_cfg, 1);
230 if (ret) {
231 dev_err(dev, "failed to write QM_QUE_ISO_CFG_V\n");
232 return ret;
233 }
234
235 ret = qm_write_regs(qm, QM_PAGE_SIZE, &vf_data->page_size, 1);
236 if (ret) {
237 dev_err(dev, "failed to write QM_PAGE_SIZE\n");
238 return ret;
239 }
240
241 /* QM_EQC_DW has 7 regs */
242 ret = qm_write_regs(qm, QM_EQC_DW0, vf_data->qm_eqc_dw, 7);
243 if (ret) {
244 dev_err(dev, "failed to write QM_EQC_DW\n");
245 return ret;
246 }
247
248 /* QM_AEQC_DW has 7 regs */
249 ret = qm_write_regs(qm, QM_AEQC_DW0, vf_data->qm_aeqc_dw, 7);
250 if (ret) {
251 dev_err(dev, "failed to write QM_AEQC_DW\n");
252 return ret;
253 }
254
255 return 0;
256 }
257
qm_db(struct hisi_qm * qm,u16 qn,u8 cmd,u16 index,u8 priority)258 static void qm_db(struct hisi_qm *qm, u16 qn, u8 cmd,
259 u16 index, u8 priority)
260 {
261 u64 doorbell;
262 u64 dbase;
263 u16 randata = 0;
264
265 if (cmd == QM_DOORBELL_CMD_SQ || cmd == QM_DOORBELL_CMD_CQ)
266 dbase = QM_DOORBELL_SQ_CQ_BASE_V2;
267 else
268 dbase = QM_DOORBELL_EQ_AEQ_BASE_V2;
269
270 doorbell = qn | ((u64)cmd << QM_DB_CMD_SHIFT_V2) |
271 ((u64)randata << QM_DB_RAND_SHIFT_V2) |
272 ((u64)index << QM_DB_INDEX_SHIFT_V2) |
273 ((u64)priority << QM_DB_PRIORITY_SHIFT_V2);
274
275 writeq(doorbell, qm->io_base + dbase);
276 }
277
pf_qm_get_qp_num(struct hisi_qm * qm,int vf_id,u32 * rbase)278 static int pf_qm_get_qp_num(struct hisi_qm *qm, int vf_id, u32 *rbase)
279 {
280 unsigned int val;
281 u64 sqc_vft;
282 u32 qp_num;
283 int ret;
284
285 ret = readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val,
286 val & BIT(0), MB_POLL_PERIOD_US,
287 MB_POLL_TIMEOUT_US);
288 if (ret)
289 return ret;
290
291 writel(0x1, qm->io_base + QM_VFT_CFG_OP_WR);
292 /* 0 mean SQC VFT */
293 writel(0x0, qm->io_base + QM_VFT_CFG_TYPE);
294 writel(vf_id, qm->io_base + QM_VFT_CFG);
295
296 writel(0x0, qm->io_base + QM_VFT_CFG_RDY);
297 writel(0x1, qm->io_base + QM_VFT_CFG_OP_ENABLE);
298
299 ret = readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val,
300 val & BIT(0), MB_POLL_PERIOD_US,
301 MB_POLL_TIMEOUT_US);
302 if (ret)
303 return ret;
304
305 sqc_vft = readl(qm->io_base + QM_VFT_CFG_DATA_L) |
306 ((u64)readl(qm->io_base + QM_VFT_CFG_DATA_H) <<
307 QM_XQC_ADDR_OFFSET);
308 *rbase = QM_SQC_VFT_BASE_MASK_V2 &
309 (sqc_vft >> QM_SQC_VFT_BASE_SHIFT_V2);
310 qp_num = (QM_SQC_VFT_NUM_MASK_V2 &
311 (sqc_vft >> QM_SQC_VFT_NUM_SHIFT_V2)) + 1;
312
313 return qp_num;
314 }
315
qm_dev_cmd_init(struct hisi_qm * qm)316 static void qm_dev_cmd_init(struct hisi_qm *qm)
317 {
318 /* Clear VF communication status registers. */
319 writel(0x1, qm->io_base + QM_IFC_INT_SOURCE_V);
320
321 /* Enable pf and vf communication. */
322 writel(0x0, qm->io_base + QM_IFC_INT_MASK);
323 }
324
vf_qm_cache_wb(struct hisi_qm * qm)325 static int vf_qm_cache_wb(struct hisi_qm *qm)
326 {
327 unsigned int val;
328
329 writel(0x1, qm->io_base + QM_CACHE_WB_START);
330 if (readl_relaxed_poll_timeout(qm->io_base + QM_CACHE_WB_DONE,
331 val, val & BIT(0), MB_POLL_PERIOD_US,
332 MB_POLL_TIMEOUT_US)) {
333 dev_err(&qm->pdev->dev, "vf QM writeback sqc cache fail\n");
334 return -EINVAL;
335 }
336
337 return 0;
338 }
339
vf_qm_fun_reset(struct hisi_qm * qm)340 static void vf_qm_fun_reset(struct hisi_qm *qm)
341 {
342 int i;
343
344 for (i = 0; i < qm->qp_num; i++)
345 qm_db(qm, i, QM_DOORBELL_CMD_SQ, 0, 1);
346 }
347
vf_qm_func_stop(struct hisi_qm * qm)348 static int vf_qm_func_stop(struct hisi_qm *qm)
349 {
350 return hisi_qm_mb(qm, QM_MB_CMD_PAUSE_QM, 0, 0, 0);
351 }
352
vf_qm_version_check(struct acc_vf_data * vf_data,struct device * dev)353 static int vf_qm_version_check(struct acc_vf_data *vf_data, struct device *dev)
354 {
355 switch (vf_data->acc_magic) {
356 case ACC_DEV_MAGIC_V2:
357 if (vf_data->major_ver != ACC_DRV_MAJOR_VER) {
358 dev_info(dev, "migration driver version<%u.%u> not match!\n",
359 vf_data->major_ver, vf_data->minor_ver);
360 return -EINVAL;
361 }
362 break;
363 case ACC_DEV_MAGIC_V1:
364 /* Correct dma address */
365 vf_data->eqe_dma = vf_data->qm_eqc_dw[QM_XQC_ADDR_HIGH];
366 vf_data->eqe_dma <<= QM_XQC_ADDR_OFFSET;
367 vf_data->eqe_dma |= vf_data->qm_eqc_dw[QM_XQC_ADDR_LOW];
368 vf_data->aeqe_dma = vf_data->qm_aeqc_dw[QM_XQC_ADDR_HIGH];
369 vf_data->aeqe_dma <<= QM_XQC_ADDR_OFFSET;
370 vf_data->aeqe_dma |= vf_data->qm_aeqc_dw[QM_XQC_ADDR_LOW];
371 break;
372 default:
373 return -EINVAL;
374 }
375
376 return 0;
377 }
378
vf_qm_check_match(struct hisi_acc_vf_core_device * hisi_acc_vdev,struct hisi_acc_vf_migration_file * migf)379 static int vf_qm_check_match(struct hisi_acc_vf_core_device *hisi_acc_vdev,
380 struct hisi_acc_vf_migration_file *migf)
381 {
382 struct acc_vf_data *vf_data = &migf->vf_data;
383 struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
384 struct hisi_qm *pf_qm = hisi_acc_vdev->pf_qm;
385 struct device *dev = &vf_qm->pdev->dev;
386 u32 que_iso_state;
387 int ret;
388
389 if (migf->total_length < QM_MATCH_SIZE || hisi_acc_vdev->match_done)
390 return 0;
391
392 ret = vf_qm_version_check(vf_data, dev);
393 if (ret) {
394 dev_err(dev, "failed to match ACC_DEV_MAGIC\n");
395 return -EINVAL;
396 }
397
398 if (vf_data->dev_id != hisi_acc_vdev->vf_dev->device) {
399 dev_err(dev, "failed to match VF devices\n");
400 return -EINVAL;
401 }
402
403 /* VF qp num check */
404 ret = qm_get_vft(vf_qm, &vf_qm->qp_base);
405 if (ret <= 0) {
406 dev_err(dev, "failed to get vft qp nums\n");
407 return -EINVAL;
408 }
409
410 if (ret != vf_data->qp_num) {
411 dev_err(dev, "failed to match VF qp num\n");
412 return -EINVAL;
413 }
414
415 vf_qm->qp_num = ret;
416
417 /* VF isolation state check */
418 ret = qm_read_regs(pf_qm, QM_QUE_ISO_CFG_V, &que_iso_state, 1);
419 if (ret) {
420 dev_err(dev, "failed to read QM_QUE_ISO_CFG_V\n");
421 return ret;
422 }
423
424 if (vf_data->que_iso_cfg != que_iso_state) {
425 dev_err(dev, "failed to match isolation state\n");
426 return -EINVAL;
427 }
428
429 hisi_acc_vdev->match_done = true;
430 return 0;
431 }
432
vf_qm_get_match_data(struct hisi_acc_vf_core_device * hisi_acc_vdev,struct acc_vf_data * vf_data)433 static int vf_qm_get_match_data(struct hisi_acc_vf_core_device *hisi_acc_vdev,
434 struct acc_vf_data *vf_data)
435 {
436 struct hisi_qm *pf_qm = hisi_acc_vdev->pf_qm;
437 struct device *dev = &pf_qm->pdev->dev;
438 int vf_id = hisi_acc_vdev->vf_id;
439 int ret;
440
441 vf_data->acc_magic = ACC_DEV_MAGIC_V2;
442 vf_data->major_ver = ACC_DRV_MAJOR_VER;
443 vf_data->minor_ver = ACC_DRV_MINOR_VER;
444 /* Save device id */
445 vf_data->dev_id = hisi_acc_vdev->vf_dev->device;
446
447 /* VF qp num save from PF */
448 ret = pf_qm_get_qp_num(pf_qm, vf_id, &vf_data->qp_base);
449 if (ret <= 0) {
450 dev_err(dev, "failed to get vft qp nums!\n");
451 return -EINVAL;
452 }
453
454 vf_data->qp_num = ret;
455
456 /* VF isolation state save from PF */
457 ret = qm_read_regs(pf_qm, QM_QUE_ISO_CFG_V, &vf_data->que_iso_cfg, 1);
458 if (ret) {
459 dev_err(dev, "failed to read QM_QUE_ISO_CFG_V!\n");
460 return ret;
461 }
462
463 return 0;
464 }
465
vf_qm_xeqc_save(struct hisi_qm * qm,struct hisi_acc_vf_migration_file * migf)466 static void vf_qm_xeqc_save(struct hisi_qm *qm,
467 struct hisi_acc_vf_migration_file *migf)
468 {
469 struct acc_vf_data *vf_data = &migf->vf_data;
470 u16 eq_head, aeq_head;
471
472 eq_head = vf_data->qm_eqc_dw[0] & 0xFFFF;
473 qm_db(qm, 0, QM_DOORBELL_CMD_EQ, eq_head, 0);
474
475 aeq_head = vf_data->qm_aeqc_dw[0] & 0xFFFF;
476 qm_db(qm, 0, QM_DOORBELL_CMD_AEQ, aeq_head, 0);
477 }
478
vf_qm_load_data(struct hisi_acc_vf_core_device * hisi_acc_vdev,struct hisi_acc_vf_migration_file * migf)479 static int vf_qm_load_data(struct hisi_acc_vf_core_device *hisi_acc_vdev,
480 struct hisi_acc_vf_migration_file *migf)
481 {
482 struct hisi_qm *qm = &hisi_acc_vdev->vf_qm;
483 struct device *dev = &qm->pdev->dev;
484 struct acc_vf_data *vf_data = &migf->vf_data;
485 int ret;
486
487 /* Return if only match data was transferred */
488 if (migf->total_length == QM_MATCH_SIZE)
489 return 0;
490
491 if (migf->total_length < sizeof(struct acc_vf_data))
492 return -EINVAL;
493
494 if (!vf_data->eqe_dma || !vf_data->aeqe_dma ||
495 !vf_data->sqc_dma || !vf_data->cqc_dma) {
496 dev_info(dev, "resume dma addr is NULL!\n");
497 hisi_acc_vdev->vf_qm_state = QM_NOT_READY;
498 return 0;
499 }
500
501 ret = qm_write_regs(qm, QM_VF_STATE, &vf_data->vf_qm_state, 1);
502 if (ret) {
503 dev_err(dev, "failed to write QM_VF_STATE\n");
504 return -EINVAL;
505 }
506 hisi_acc_vdev->vf_qm_state = vf_data->vf_qm_state;
507
508 qm->eqe_dma = vf_data->eqe_dma;
509 qm->aeqe_dma = vf_data->aeqe_dma;
510 qm->sqc_dma = vf_data->sqc_dma;
511 qm->cqc_dma = vf_data->cqc_dma;
512
513 qm->qp_base = vf_data->qp_base;
514 qm->qp_num = vf_data->qp_num;
515
516 ret = qm_set_regs(qm, vf_data);
517 if (ret) {
518 dev_err(dev, "set VF regs failed\n");
519 return ret;
520 }
521
522 ret = hisi_qm_mb(qm, QM_MB_CMD_SQC_BT, qm->sqc_dma, 0, 0);
523 if (ret) {
524 dev_err(dev, "set sqc failed\n");
525 return ret;
526 }
527
528 ret = hisi_qm_mb(qm, QM_MB_CMD_CQC_BT, qm->cqc_dma, 0, 0);
529 if (ret) {
530 dev_err(dev, "set cqc failed\n");
531 return ret;
532 }
533
534 qm_dev_cmd_init(qm);
535 return 0;
536 }
537
vf_qm_state_save(struct hisi_acc_vf_core_device * hisi_acc_vdev,struct hisi_acc_vf_migration_file * migf)538 static int vf_qm_state_save(struct hisi_acc_vf_core_device *hisi_acc_vdev,
539 struct hisi_acc_vf_migration_file *migf)
540 {
541 struct acc_vf_data *vf_data = &migf->vf_data;
542 struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
543 struct device *dev = &vf_qm->pdev->dev;
544 int ret;
545
546 if (unlikely(qm_wait_dev_not_ready(vf_qm))) {
547 /* Update state and return with match data */
548 vf_data->vf_qm_state = QM_NOT_READY;
549 hisi_acc_vdev->vf_qm_state = vf_data->vf_qm_state;
550 migf->total_length = QM_MATCH_SIZE;
551 return 0;
552 }
553
554 vf_data->vf_qm_state = QM_READY;
555 hisi_acc_vdev->vf_qm_state = vf_data->vf_qm_state;
556
557 ret = vf_qm_cache_wb(vf_qm);
558 if (ret) {
559 dev_err(dev, "failed to writeback QM Cache!\n");
560 return ret;
561 }
562
563 ret = qm_get_regs(vf_qm, vf_data);
564 if (ret)
565 return -EINVAL;
566
567 /* Every reg is 32 bit, the dma address is 64 bit. */
568 vf_data->eqe_dma = vf_data->qm_eqc_dw[QM_XQC_ADDR_HIGH];
569 vf_data->eqe_dma <<= QM_XQC_ADDR_OFFSET;
570 vf_data->eqe_dma |= vf_data->qm_eqc_dw[QM_XQC_ADDR_LOW];
571 vf_data->aeqe_dma = vf_data->qm_aeqc_dw[QM_XQC_ADDR_HIGH];
572 vf_data->aeqe_dma <<= QM_XQC_ADDR_OFFSET;
573 vf_data->aeqe_dma |= vf_data->qm_aeqc_dw[QM_XQC_ADDR_LOW];
574
575 /* Through SQC_BT/CQC_BT to get sqc and cqc address */
576 ret = qm_get_sqc(vf_qm, &vf_data->sqc_dma);
577 if (ret) {
578 dev_err(dev, "failed to read SQC addr!\n");
579 return -EINVAL;
580 }
581
582 ret = qm_get_cqc(vf_qm, &vf_data->cqc_dma);
583 if (ret) {
584 dev_err(dev, "failed to read CQC addr!\n");
585 return -EINVAL;
586 }
587
588 migf->total_length = sizeof(struct acc_vf_data);
589 /* Save eqc and aeqc interrupt information */
590 vf_qm_xeqc_save(vf_qm, migf);
591
592 return 0;
593 }
594
hisi_acc_drvdata(struct pci_dev * pdev)595 static struct hisi_acc_vf_core_device *hisi_acc_drvdata(struct pci_dev *pdev)
596 {
597 struct vfio_pci_core_device *core_device = dev_get_drvdata(&pdev->dev);
598
599 return container_of(core_device, struct hisi_acc_vf_core_device,
600 core_device);
601 }
602
603 /* Check the PF's RAS state and Function INT state */
604 static int
hisi_acc_check_int_state(struct hisi_acc_vf_core_device * hisi_acc_vdev)605 hisi_acc_check_int_state(struct hisi_acc_vf_core_device *hisi_acc_vdev)
606 {
607 struct hisi_qm *vfqm = &hisi_acc_vdev->vf_qm;
608 struct hisi_qm *qm = hisi_acc_vdev->pf_qm;
609 struct pci_dev *vf_pdev = hisi_acc_vdev->vf_dev;
610 struct device *dev = &qm->pdev->dev;
611 u32 state;
612
613 /* Check RAS state */
614 state = qm_check_reg_state(qm, QM_ABNORMAL_INT_STATUS);
615 if (state) {
616 dev_err(dev, "failed to check QM RAS state!\n");
617 return -EBUSY;
618 }
619
620 /* Check Function Communication state between PF and VF */
621 state = qm_check_reg_state(vfqm, QM_IFC_INT_STATUS);
622 if (state) {
623 dev_err(dev, "failed to check QM IFC INT state!\n");
624 return -EBUSY;
625 }
626 state = qm_check_reg_state(vfqm, QM_IFC_INT_SET_V);
627 if (state) {
628 dev_err(dev, "failed to check QM IFC INT SET state!\n");
629 return -EBUSY;
630 }
631
632 /* Check submodule task state */
633 switch (vf_pdev->device) {
634 case PCI_DEVICE_ID_HUAWEI_SEC_VF:
635 state = qm_check_reg_state(qm, SEC_CORE_INT_STATUS);
636 if (state) {
637 dev_err(dev, "failed to check QM SEC Core INT state!\n");
638 return -EBUSY;
639 }
640 return 0;
641 case PCI_DEVICE_ID_HUAWEI_HPRE_VF:
642 state = qm_check_reg_state(qm, HPRE_HAC_INT_STATUS);
643 if (state) {
644 dev_err(dev, "failed to check QM HPRE HAC INT state!\n");
645 return -EBUSY;
646 }
647 return 0;
648 case PCI_DEVICE_ID_HUAWEI_ZIP_VF:
649 state = qm_check_reg_state(qm, HZIP_CORE_INT_STATUS);
650 if (state) {
651 dev_err(dev, "failed to check QM ZIP Core INT state!\n");
652 return -EBUSY;
653 }
654 return 0;
655 default:
656 dev_err(dev, "failed to detect acc module type!\n");
657 return -EINVAL;
658 }
659 }
660
hisi_acc_vf_disable_fd(struct hisi_acc_vf_migration_file * migf)661 static void hisi_acc_vf_disable_fd(struct hisi_acc_vf_migration_file *migf)
662 {
663 mutex_lock(&migf->lock);
664 migf->disabled = true;
665 migf->total_length = 0;
666 migf->filp->f_pos = 0;
667 mutex_unlock(&migf->lock);
668 }
669
hisi_acc_vf_disable_fds(struct hisi_acc_vf_core_device * hisi_acc_vdev)670 static void hisi_acc_vf_disable_fds(struct hisi_acc_vf_core_device *hisi_acc_vdev)
671 {
672 if (hisi_acc_vdev->resuming_migf) {
673 hisi_acc_vf_disable_fd(hisi_acc_vdev->resuming_migf);
674 fput(hisi_acc_vdev->resuming_migf->filp);
675 hisi_acc_vdev->resuming_migf = NULL;
676 }
677
678 if (hisi_acc_vdev->saving_migf) {
679 hisi_acc_vf_disable_fd(hisi_acc_vdev->saving_migf);
680 fput(hisi_acc_vdev->saving_migf->filp);
681 hisi_acc_vdev->saving_migf = NULL;
682 }
683 }
684
685 /*
686 * This function is called in all state_mutex unlock cases to
687 * handle a 'deferred_reset' if exists.
688 */
689 static void
hisi_acc_vf_state_mutex_unlock(struct hisi_acc_vf_core_device * hisi_acc_vdev)690 hisi_acc_vf_state_mutex_unlock(struct hisi_acc_vf_core_device *hisi_acc_vdev)
691 {
692 again:
693 spin_lock(&hisi_acc_vdev->reset_lock);
694 if (hisi_acc_vdev->deferred_reset) {
695 hisi_acc_vdev->deferred_reset = false;
696 spin_unlock(&hisi_acc_vdev->reset_lock);
697 hisi_acc_vdev->vf_qm_state = QM_NOT_READY;
698 hisi_acc_vdev->mig_state = VFIO_DEVICE_STATE_RUNNING;
699 hisi_acc_vf_disable_fds(hisi_acc_vdev);
700 goto again;
701 }
702 mutex_unlock(&hisi_acc_vdev->state_mutex);
703 spin_unlock(&hisi_acc_vdev->reset_lock);
704 }
705
hisi_acc_vf_start_device(struct hisi_acc_vf_core_device * hisi_acc_vdev)706 static void hisi_acc_vf_start_device(struct hisi_acc_vf_core_device *hisi_acc_vdev)
707 {
708 struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
709
710 if (hisi_acc_vdev->vf_qm_state != QM_READY)
711 return;
712
713 /* Make sure the device is enabled */
714 qm_dev_cmd_init(vf_qm);
715
716 vf_qm_fun_reset(vf_qm);
717 }
718
hisi_acc_vf_load_state(struct hisi_acc_vf_core_device * hisi_acc_vdev)719 static int hisi_acc_vf_load_state(struct hisi_acc_vf_core_device *hisi_acc_vdev)
720 {
721 struct device *dev = &hisi_acc_vdev->vf_dev->dev;
722 struct hisi_acc_vf_migration_file *migf = hisi_acc_vdev->resuming_migf;
723 int ret;
724
725 /* Recover data to VF */
726 ret = vf_qm_load_data(hisi_acc_vdev, migf);
727 if (ret) {
728 dev_err(dev, "failed to recover the VF!\n");
729 return ret;
730 }
731
732 return 0;
733 }
734
hisi_acc_vf_release_file(struct inode * inode,struct file * filp)735 static int hisi_acc_vf_release_file(struct inode *inode, struct file *filp)
736 {
737 struct hisi_acc_vf_migration_file *migf = filp->private_data;
738
739 hisi_acc_vf_disable_fd(migf);
740 mutex_destroy(&migf->lock);
741 kfree(migf);
742 return 0;
743 }
744
hisi_acc_vf_resume_write(struct file * filp,const char __user * buf,size_t len,loff_t * pos)745 static ssize_t hisi_acc_vf_resume_write(struct file *filp, const char __user *buf,
746 size_t len, loff_t *pos)
747 {
748 struct hisi_acc_vf_migration_file *migf = filp->private_data;
749 u8 *vf_data = (u8 *)&migf->vf_data;
750 loff_t requested_length;
751 ssize_t done = 0;
752 int ret;
753
754 if (pos)
755 return -ESPIPE;
756 pos = &filp->f_pos;
757
758 if (*pos < 0 ||
759 check_add_overflow((loff_t)len, *pos, &requested_length))
760 return -EINVAL;
761
762 if (requested_length > sizeof(struct acc_vf_data))
763 return -ENOMEM;
764
765 mutex_lock(&migf->lock);
766 if (migf->disabled) {
767 done = -ENODEV;
768 goto out_unlock;
769 }
770
771 ret = copy_from_user(vf_data + *pos, buf, len);
772 if (ret) {
773 done = -EFAULT;
774 goto out_unlock;
775 }
776 *pos += len;
777 done = len;
778 migf->total_length += len;
779
780 ret = vf_qm_check_match(migf->hisi_acc_vdev, migf);
781 if (ret)
782 done = -EFAULT;
783 out_unlock:
784 mutex_unlock(&migf->lock);
785 return done;
786 }
787
788 static const struct file_operations hisi_acc_vf_resume_fops = {
789 .owner = THIS_MODULE,
790 .write = hisi_acc_vf_resume_write,
791 .release = hisi_acc_vf_release_file,
792 .llseek = no_llseek,
793 };
794
795 static struct hisi_acc_vf_migration_file *
hisi_acc_vf_pci_resume(struct hisi_acc_vf_core_device * hisi_acc_vdev)796 hisi_acc_vf_pci_resume(struct hisi_acc_vf_core_device *hisi_acc_vdev)
797 {
798 struct hisi_acc_vf_migration_file *migf;
799
800 migf = kzalloc(sizeof(*migf), GFP_KERNEL_ACCOUNT);
801 if (!migf)
802 return ERR_PTR(-ENOMEM);
803
804 migf->filp = anon_inode_getfile("hisi_acc_vf_mig", &hisi_acc_vf_resume_fops, migf,
805 O_WRONLY);
806 if (IS_ERR(migf->filp)) {
807 int err = PTR_ERR(migf->filp);
808
809 kfree(migf);
810 return ERR_PTR(err);
811 }
812
813 stream_open(migf->filp->f_inode, migf->filp);
814 mutex_init(&migf->lock);
815 migf->hisi_acc_vdev = hisi_acc_vdev;
816 return migf;
817 }
818
hisi_acc_vf_precopy_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)819 static long hisi_acc_vf_precopy_ioctl(struct file *filp,
820 unsigned int cmd, unsigned long arg)
821 {
822 struct hisi_acc_vf_migration_file *migf = filp->private_data;
823 struct hisi_acc_vf_core_device *hisi_acc_vdev = migf->hisi_acc_vdev;
824 loff_t *pos = &filp->f_pos;
825 struct vfio_precopy_info info;
826 unsigned long minsz;
827 int ret;
828
829 if (cmd != VFIO_MIG_GET_PRECOPY_INFO)
830 return -ENOTTY;
831
832 minsz = offsetofend(struct vfio_precopy_info, dirty_bytes);
833
834 if (copy_from_user(&info, (void __user *)arg, minsz))
835 return -EFAULT;
836 if (info.argsz < minsz)
837 return -EINVAL;
838
839 mutex_lock(&hisi_acc_vdev->state_mutex);
840 if (hisi_acc_vdev->mig_state != VFIO_DEVICE_STATE_PRE_COPY) {
841 mutex_unlock(&hisi_acc_vdev->state_mutex);
842 return -EINVAL;
843 }
844
845 mutex_lock(&migf->lock);
846
847 if (migf->disabled) {
848 ret = -ENODEV;
849 goto out;
850 }
851
852 if (*pos > migf->total_length) {
853 ret = -EINVAL;
854 goto out;
855 }
856
857 info.dirty_bytes = 0;
858 info.initial_bytes = migf->total_length - *pos;
859
860 ret = copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
861 out:
862 mutex_unlock(&migf->lock);
863 mutex_unlock(&hisi_acc_vdev->state_mutex);
864 return ret;
865 }
866
hisi_acc_vf_save_read(struct file * filp,char __user * buf,size_t len,loff_t * pos)867 static ssize_t hisi_acc_vf_save_read(struct file *filp, char __user *buf, size_t len,
868 loff_t *pos)
869 {
870 struct hisi_acc_vf_migration_file *migf = filp->private_data;
871 ssize_t done = 0;
872 int ret;
873
874 if (pos)
875 return -ESPIPE;
876 pos = &filp->f_pos;
877
878 mutex_lock(&migf->lock);
879 if (*pos > migf->total_length) {
880 done = -EINVAL;
881 goto out_unlock;
882 }
883
884 if (migf->disabled) {
885 done = -ENODEV;
886 goto out_unlock;
887 }
888
889 len = min_t(size_t, migf->total_length - *pos, len);
890 if (len) {
891 u8 *vf_data = (u8 *)&migf->vf_data;
892
893 ret = copy_to_user(buf, vf_data + *pos, len);
894 if (ret) {
895 done = -EFAULT;
896 goto out_unlock;
897 }
898 *pos += len;
899 done = len;
900 }
901 out_unlock:
902 mutex_unlock(&migf->lock);
903 return done;
904 }
905
906 static const struct file_operations hisi_acc_vf_save_fops = {
907 .owner = THIS_MODULE,
908 .read = hisi_acc_vf_save_read,
909 .unlocked_ioctl = hisi_acc_vf_precopy_ioctl,
910 .compat_ioctl = compat_ptr_ioctl,
911 .release = hisi_acc_vf_release_file,
912 .llseek = no_llseek,
913 };
914
915 static struct hisi_acc_vf_migration_file *
hisi_acc_open_saving_migf(struct hisi_acc_vf_core_device * hisi_acc_vdev)916 hisi_acc_open_saving_migf(struct hisi_acc_vf_core_device *hisi_acc_vdev)
917 {
918 struct hisi_acc_vf_migration_file *migf;
919 int ret;
920
921 migf = kzalloc(sizeof(*migf), GFP_KERNEL_ACCOUNT);
922 if (!migf)
923 return ERR_PTR(-ENOMEM);
924
925 migf->filp = anon_inode_getfile("hisi_acc_vf_mig", &hisi_acc_vf_save_fops, migf,
926 O_RDONLY);
927 if (IS_ERR(migf->filp)) {
928 int err = PTR_ERR(migf->filp);
929
930 kfree(migf);
931 return ERR_PTR(err);
932 }
933
934 stream_open(migf->filp->f_inode, migf->filp);
935 mutex_init(&migf->lock);
936 migf->hisi_acc_vdev = hisi_acc_vdev;
937
938 ret = vf_qm_get_match_data(hisi_acc_vdev, &migf->vf_data);
939 if (ret) {
940 fput(migf->filp);
941 return ERR_PTR(ret);
942 }
943
944 return migf;
945 }
946
947 static struct hisi_acc_vf_migration_file *
hisi_acc_vf_pre_copy(struct hisi_acc_vf_core_device * hisi_acc_vdev)948 hisi_acc_vf_pre_copy(struct hisi_acc_vf_core_device *hisi_acc_vdev)
949 {
950 struct hisi_acc_vf_migration_file *migf;
951
952 migf = hisi_acc_open_saving_migf(hisi_acc_vdev);
953 if (IS_ERR(migf))
954 return migf;
955
956 migf->total_length = QM_MATCH_SIZE;
957 return migf;
958 }
959
960 static struct hisi_acc_vf_migration_file *
hisi_acc_vf_stop_copy(struct hisi_acc_vf_core_device * hisi_acc_vdev,bool open)961 hisi_acc_vf_stop_copy(struct hisi_acc_vf_core_device *hisi_acc_vdev, bool open)
962 {
963 int ret;
964 struct hisi_acc_vf_migration_file *migf = NULL;
965
966 if (open) {
967 /*
968 * Userspace didn't use PRECOPY support. Hence saving_migf
969 * is not opened yet.
970 */
971 migf = hisi_acc_open_saving_migf(hisi_acc_vdev);
972 if (IS_ERR(migf))
973 return migf;
974 } else {
975 migf = hisi_acc_vdev->saving_migf;
976 }
977
978 ret = vf_qm_state_save(hisi_acc_vdev, migf);
979 if (ret)
980 return ERR_PTR(ret);
981
982 return open ? migf : NULL;
983 }
984
hisi_acc_vf_stop_device(struct hisi_acc_vf_core_device * hisi_acc_vdev)985 static int hisi_acc_vf_stop_device(struct hisi_acc_vf_core_device *hisi_acc_vdev)
986 {
987 struct device *dev = &hisi_acc_vdev->vf_dev->dev;
988 struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
989 int ret;
990
991 ret = vf_qm_func_stop(vf_qm);
992 if (ret) {
993 dev_err(dev, "failed to stop QM VF function!\n");
994 return ret;
995 }
996
997 ret = hisi_acc_check_int_state(hisi_acc_vdev);
998 if (ret) {
999 dev_err(dev, "failed to check QM INT state!\n");
1000 return ret;
1001 }
1002 return 0;
1003 }
1004
1005 static struct file *
hisi_acc_vf_set_device_state(struct hisi_acc_vf_core_device * hisi_acc_vdev,u32 new)1006 hisi_acc_vf_set_device_state(struct hisi_acc_vf_core_device *hisi_acc_vdev,
1007 u32 new)
1008 {
1009 u32 cur = hisi_acc_vdev->mig_state;
1010 int ret;
1011
1012 if (cur == VFIO_DEVICE_STATE_RUNNING && new == VFIO_DEVICE_STATE_PRE_COPY) {
1013 struct hisi_acc_vf_migration_file *migf;
1014
1015 migf = hisi_acc_vf_pre_copy(hisi_acc_vdev);
1016 if (IS_ERR(migf))
1017 return ERR_CAST(migf);
1018 get_file(migf->filp);
1019 hisi_acc_vdev->saving_migf = migf;
1020 return migf->filp;
1021 }
1022
1023 if (cur == VFIO_DEVICE_STATE_PRE_COPY && new == VFIO_DEVICE_STATE_STOP_COPY) {
1024 struct hisi_acc_vf_migration_file *migf;
1025
1026 ret = hisi_acc_vf_stop_device(hisi_acc_vdev);
1027 if (ret)
1028 return ERR_PTR(ret);
1029
1030 migf = hisi_acc_vf_stop_copy(hisi_acc_vdev, false);
1031 if (IS_ERR(migf))
1032 return ERR_CAST(migf);
1033
1034 return NULL;
1035 }
1036
1037 if (cur == VFIO_DEVICE_STATE_RUNNING && new == VFIO_DEVICE_STATE_STOP) {
1038 ret = hisi_acc_vf_stop_device(hisi_acc_vdev);
1039 if (ret)
1040 return ERR_PTR(ret);
1041 return NULL;
1042 }
1043
1044 if (cur == VFIO_DEVICE_STATE_STOP && new == VFIO_DEVICE_STATE_STOP_COPY) {
1045 struct hisi_acc_vf_migration_file *migf;
1046
1047 migf = hisi_acc_vf_stop_copy(hisi_acc_vdev, true);
1048 if (IS_ERR(migf))
1049 return ERR_CAST(migf);
1050 get_file(migf->filp);
1051 hisi_acc_vdev->saving_migf = migf;
1052 return migf->filp;
1053 }
1054
1055 if ((cur == VFIO_DEVICE_STATE_STOP_COPY && new == VFIO_DEVICE_STATE_STOP)) {
1056 hisi_acc_vf_disable_fds(hisi_acc_vdev);
1057 return NULL;
1058 }
1059
1060 if (cur == VFIO_DEVICE_STATE_STOP && new == VFIO_DEVICE_STATE_RESUMING) {
1061 struct hisi_acc_vf_migration_file *migf;
1062
1063 migf = hisi_acc_vf_pci_resume(hisi_acc_vdev);
1064 if (IS_ERR(migf))
1065 return ERR_CAST(migf);
1066 get_file(migf->filp);
1067 hisi_acc_vdev->resuming_migf = migf;
1068 return migf->filp;
1069 }
1070
1071 if (cur == VFIO_DEVICE_STATE_RESUMING && new == VFIO_DEVICE_STATE_STOP) {
1072 ret = hisi_acc_vf_load_state(hisi_acc_vdev);
1073 if (ret)
1074 return ERR_PTR(ret);
1075 hisi_acc_vf_disable_fds(hisi_acc_vdev);
1076 return NULL;
1077 }
1078
1079 if (cur == VFIO_DEVICE_STATE_PRE_COPY && new == VFIO_DEVICE_STATE_RUNNING) {
1080 hisi_acc_vf_disable_fds(hisi_acc_vdev);
1081 return NULL;
1082 }
1083
1084 if (cur == VFIO_DEVICE_STATE_STOP && new == VFIO_DEVICE_STATE_RUNNING) {
1085 hisi_acc_vf_start_device(hisi_acc_vdev);
1086 return NULL;
1087 }
1088
1089 /*
1090 * vfio_mig_get_next_state() does not use arcs other than the above
1091 */
1092 WARN_ON(true);
1093 return ERR_PTR(-EINVAL);
1094 }
1095
1096 static struct file *
hisi_acc_vfio_pci_set_device_state(struct vfio_device * vdev,enum vfio_device_mig_state new_state)1097 hisi_acc_vfio_pci_set_device_state(struct vfio_device *vdev,
1098 enum vfio_device_mig_state new_state)
1099 {
1100 struct hisi_acc_vf_core_device *hisi_acc_vdev = container_of(vdev,
1101 struct hisi_acc_vf_core_device, core_device.vdev);
1102 enum vfio_device_mig_state next_state;
1103 struct file *res = NULL;
1104 int ret;
1105
1106 mutex_lock(&hisi_acc_vdev->state_mutex);
1107 while (new_state != hisi_acc_vdev->mig_state) {
1108 ret = vfio_mig_get_next_state(vdev,
1109 hisi_acc_vdev->mig_state,
1110 new_state, &next_state);
1111 if (ret) {
1112 res = ERR_PTR(-EINVAL);
1113 break;
1114 }
1115
1116 res = hisi_acc_vf_set_device_state(hisi_acc_vdev, next_state);
1117 if (IS_ERR(res))
1118 break;
1119 hisi_acc_vdev->mig_state = next_state;
1120 if (WARN_ON(res && new_state != hisi_acc_vdev->mig_state)) {
1121 fput(res);
1122 res = ERR_PTR(-EINVAL);
1123 break;
1124 }
1125 }
1126 hisi_acc_vf_state_mutex_unlock(hisi_acc_vdev);
1127 return res;
1128 }
1129
1130 static int
hisi_acc_vfio_pci_get_data_size(struct vfio_device * vdev,unsigned long * stop_copy_length)1131 hisi_acc_vfio_pci_get_data_size(struct vfio_device *vdev,
1132 unsigned long *stop_copy_length)
1133 {
1134 *stop_copy_length = sizeof(struct acc_vf_data);
1135 return 0;
1136 }
1137
1138 static int
hisi_acc_vfio_pci_get_device_state(struct vfio_device * vdev,enum vfio_device_mig_state * curr_state)1139 hisi_acc_vfio_pci_get_device_state(struct vfio_device *vdev,
1140 enum vfio_device_mig_state *curr_state)
1141 {
1142 struct hisi_acc_vf_core_device *hisi_acc_vdev = container_of(vdev,
1143 struct hisi_acc_vf_core_device, core_device.vdev);
1144
1145 mutex_lock(&hisi_acc_vdev->state_mutex);
1146 *curr_state = hisi_acc_vdev->mig_state;
1147 hisi_acc_vf_state_mutex_unlock(hisi_acc_vdev);
1148 return 0;
1149 }
1150
hisi_acc_vf_pci_aer_reset_done(struct pci_dev * pdev)1151 static void hisi_acc_vf_pci_aer_reset_done(struct pci_dev *pdev)
1152 {
1153 struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_drvdata(pdev);
1154
1155 if (hisi_acc_vdev->core_device.vdev.migration_flags !=
1156 VFIO_MIGRATION_STOP_COPY)
1157 return;
1158
1159 /*
1160 * As the higher VFIO layers are holding locks across reset and using
1161 * those same locks with the mm_lock we need to prevent ABBA deadlock
1162 * with the state_mutex and mm_lock.
1163 * In case the state_mutex was taken already we defer the cleanup work
1164 * to the unlock flow of the other running context.
1165 */
1166 spin_lock(&hisi_acc_vdev->reset_lock);
1167 hisi_acc_vdev->deferred_reset = true;
1168 if (!mutex_trylock(&hisi_acc_vdev->state_mutex)) {
1169 spin_unlock(&hisi_acc_vdev->reset_lock);
1170 return;
1171 }
1172 spin_unlock(&hisi_acc_vdev->reset_lock);
1173 hisi_acc_vf_state_mutex_unlock(hisi_acc_vdev);
1174 }
1175
hisi_acc_vf_qm_init(struct hisi_acc_vf_core_device * hisi_acc_vdev)1176 static int hisi_acc_vf_qm_init(struct hisi_acc_vf_core_device *hisi_acc_vdev)
1177 {
1178 struct vfio_pci_core_device *vdev = &hisi_acc_vdev->core_device;
1179 struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
1180 struct pci_dev *vf_dev = vdev->pdev;
1181
1182 /*
1183 * ACC VF dev BAR2 region consists of both functional register space
1184 * and migration control register space. For migration to work, we
1185 * need access to both. Hence, we map the entire BAR2 region here.
1186 * But unnecessarily exposing the migration BAR region to the Guest
1187 * has the potential to prevent/corrupt the Guest migration. Hence,
1188 * we restrict access to the migration control space from
1189 * Guest(Please see mmap/ioctl/read/write override functions).
1190 *
1191 * Please note that it is OK to expose the entire VF BAR if migration
1192 * is not supported or required as this cannot affect the ACC PF
1193 * configurations.
1194 *
1195 * Also the HiSilicon ACC VF devices supported by this driver on
1196 * HiSilicon hardware platforms are integrated end point devices
1197 * and the platform lacks the capability to perform any PCIe P2P
1198 * between these devices.
1199 */
1200
1201 vf_qm->io_base =
1202 ioremap(pci_resource_start(vf_dev, VFIO_PCI_BAR2_REGION_INDEX),
1203 pci_resource_len(vf_dev, VFIO_PCI_BAR2_REGION_INDEX));
1204 if (!vf_qm->io_base)
1205 return -EIO;
1206
1207 vf_qm->fun_type = QM_HW_VF;
1208 vf_qm->pdev = vf_dev;
1209 mutex_init(&vf_qm->mailbox_lock);
1210
1211 return 0;
1212 }
1213
hisi_acc_get_pf_qm(struct pci_dev * pdev)1214 static struct hisi_qm *hisi_acc_get_pf_qm(struct pci_dev *pdev)
1215 {
1216 struct hisi_qm *pf_qm;
1217 struct pci_driver *pf_driver;
1218
1219 if (!pdev->is_virtfn)
1220 return NULL;
1221
1222 switch (pdev->device) {
1223 case PCI_DEVICE_ID_HUAWEI_SEC_VF:
1224 pf_driver = hisi_sec_get_pf_driver();
1225 break;
1226 case PCI_DEVICE_ID_HUAWEI_HPRE_VF:
1227 pf_driver = hisi_hpre_get_pf_driver();
1228 break;
1229 case PCI_DEVICE_ID_HUAWEI_ZIP_VF:
1230 pf_driver = hisi_zip_get_pf_driver();
1231 break;
1232 default:
1233 return NULL;
1234 }
1235
1236 if (!pf_driver)
1237 return NULL;
1238
1239 pf_qm = pci_iov_get_pf_drvdata(pdev, pf_driver);
1240
1241 return !IS_ERR(pf_qm) ? pf_qm : NULL;
1242 }
1243
hisi_acc_pci_rw_access_check(struct vfio_device * core_vdev,size_t count,loff_t * ppos,size_t * new_count)1244 static int hisi_acc_pci_rw_access_check(struct vfio_device *core_vdev,
1245 size_t count, loff_t *ppos,
1246 size_t *new_count)
1247 {
1248 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
1249 struct vfio_pci_core_device *vdev =
1250 container_of(core_vdev, struct vfio_pci_core_device, vdev);
1251
1252 if (index == VFIO_PCI_BAR2_REGION_INDEX) {
1253 loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
1254 resource_size_t end = pci_resource_len(vdev->pdev, index) / 2;
1255
1256 /* Check if access is for migration control region */
1257 if (pos >= end)
1258 return -EINVAL;
1259
1260 *new_count = min(count, (size_t)(end - pos));
1261 }
1262
1263 return 0;
1264 }
1265
hisi_acc_vfio_pci_mmap(struct vfio_device * core_vdev,struct vm_area_struct * vma)1266 static int hisi_acc_vfio_pci_mmap(struct vfio_device *core_vdev,
1267 struct vm_area_struct *vma)
1268 {
1269 struct vfio_pci_core_device *vdev =
1270 container_of(core_vdev, struct vfio_pci_core_device, vdev);
1271 unsigned int index;
1272
1273 index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
1274 if (index == VFIO_PCI_BAR2_REGION_INDEX) {
1275 u64 req_len, pgoff, req_start;
1276 resource_size_t end = pci_resource_len(vdev->pdev, index) / 2;
1277
1278 req_len = vma->vm_end - vma->vm_start;
1279 pgoff = vma->vm_pgoff &
1280 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
1281 req_start = pgoff << PAGE_SHIFT;
1282
1283 if (req_start + req_len > end)
1284 return -EINVAL;
1285 }
1286
1287 return vfio_pci_core_mmap(core_vdev, vma);
1288 }
1289
hisi_acc_vfio_pci_write(struct vfio_device * core_vdev,const char __user * buf,size_t count,loff_t * ppos)1290 static ssize_t hisi_acc_vfio_pci_write(struct vfio_device *core_vdev,
1291 const char __user *buf, size_t count,
1292 loff_t *ppos)
1293 {
1294 size_t new_count = count;
1295 int ret;
1296
1297 ret = hisi_acc_pci_rw_access_check(core_vdev, count, ppos, &new_count);
1298 if (ret)
1299 return ret;
1300
1301 return vfio_pci_core_write(core_vdev, buf, new_count, ppos);
1302 }
1303
hisi_acc_vfio_pci_read(struct vfio_device * core_vdev,char __user * buf,size_t count,loff_t * ppos)1304 static ssize_t hisi_acc_vfio_pci_read(struct vfio_device *core_vdev,
1305 char __user *buf, size_t count,
1306 loff_t *ppos)
1307 {
1308 size_t new_count = count;
1309 int ret;
1310
1311 ret = hisi_acc_pci_rw_access_check(core_vdev, count, ppos, &new_count);
1312 if (ret)
1313 return ret;
1314
1315 return vfio_pci_core_read(core_vdev, buf, new_count, ppos);
1316 }
1317
hisi_acc_vfio_pci_ioctl(struct vfio_device * core_vdev,unsigned int cmd,unsigned long arg)1318 static long hisi_acc_vfio_pci_ioctl(struct vfio_device *core_vdev, unsigned int cmd,
1319 unsigned long arg)
1320 {
1321 if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
1322 struct vfio_pci_core_device *vdev =
1323 container_of(core_vdev, struct vfio_pci_core_device, vdev);
1324 struct pci_dev *pdev = vdev->pdev;
1325 struct vfio_region_info info;
1326 unsigned long minsz;
1327
1328 minsz = offsetofend(struct vfio_region_info, offset);
1329
1330 if (copy_from_user(&info, (void __user *)arg, minsz))
1331 return -EFAULT;
1332
1333 if (info.argsz < minsz)
1334 return -EINVAL;
1335
1336 if (info.index == VFIO_PCI_BAR2_REGION_INDEX) {
1337 info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
1338
1339 /*
1340 * ACC VF dev BAR2 region consists of both functional
1341 * register space and migration control register space.
1342 * Report only the functional region to Guest.
1343 */
1344 info.size = pci_resource_len(pdev, info.index) / 2;
1345
1346 info.flags = VFIO_REGION_INFO_FLAG_READ |
1347 VFIO_REGION_INFO_FLAG_WRITE |
1348 VFIO_REGION_INFO_FLAG_MMAP;
1349
1350 return copy_to_user((void __user *)arg, &info, minsz) ?
1351 -EFAULT : 0;
1352 }
1353 }
1354 return vfio_pci_core_ioctl(core_vdev, cmd, arg);
1355 }
1356
hisi_acc_vfio_pci_open_device(struct vfio_device * core_vdev)1357 static int hisi_acc_vfio_pci_open_device(struct vfio_device *core_vdev)
1358 {
1359 struct hisi_acc_vf_core_device *hisi_acc_vdev = container_of(core_vdev,
1360 struct hisi_acc_vf_core_device, core_device.vdev);
1361 struct vfio_pci_core_device *vdev = &hisi_acc_vdev->core_device;
1362 int ret;
1363
1364 ret = vfio_pci_core_enable(vdev);
1365 if (ret)
1366 return ret;
1367
1368 if (core_vdev->mig_ops) {
1369 ret = hisi_acc_vf_qm_init(hisi_acc_vdev);
1370 if (ret) {
1371 vfio_pci_core_disable(vdev);
1372 return ret;
1373 }
1374 hisi_acc_vdev->mig_state = VFIO_DEVICE_STATE_RUNNING;
1375 }
1376
1377 vfio_pci_core_finish_enable(vdev);
1378 return 0;
1379 }
1380
hisi_acc_vfio_pci_close_device(struct vfio_device * core_vdev)1381 static void hisi_acc_vfio_pci_close_device(struct vfio_device *core_vdev)
1382 {
1383 struct hisi_acc_vf_core_device *hisi_acc_vdev = container_of(core_vdev,
1384 struct hisi_acc_vf_core_device, core_device.vdev);
1385 struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
1386
1387 iounmap(vf_qm->io_base);
1388 vfio_pci_core_close_device(core_vdev);
1389 }
1390
1391 static const struct vfio_migration_ops hisi_acc_vfio_pci_migrn_state_ops = {
1392 .migration_set_state = hisi_acc_vfio_pci_set_device_state,
1393 .migration_get_state = hisi_acc_vfio_pci_get_device_state,
1394 .migration_get_data_size = hisi_acc_vfio_pci_get_data_size,
1395 };
1396
hisi_acc_vfio_pci_migrn_init_dev(struct vfio_device * core_vdev)1397 static int hisi_acc_vfio_pci_migrn_init_dev(struct vfio_device *core_vdev)
1398 {
1399 struct hisi_acc_vf_core_device *hisi_acc_vdev = container_of(core_vdev,
1400 struct hisi_acc_vf_core_device, core_device.vdev);
1401 struct pci_dev *pdev = to_pci_dev(core_vdev->dev);
1402 struct hisi_qm *pf_qm = hisi_acc_get_pf_qm(pdev);
1403
1404 hisi_acc_vdev->vf_id = pci_iov_vf_id(pdev) + 1;
1405 hisi_acc_vdev->pf_qm = pf_qm;
1406 hisi_acc_vdev->vf_dev = pdev;
1407 hisi_acc_vdev->vf_qm_state = QM_NOT_READY;
1408 mutex_init(&hisi_acc_vdev->state_mutex);
1409
1410 core_vdev->migration_flags = VFIO_MIGRATION_STOP_COPY | VFIO_MIGRATION_PRE_COPY;
1411 core_vdev->mig_ops = &hisi_acc_vfio_pci_migrn_state_ops;
1412
1413 return vfio_pci_core_init_dev(core_vdev);
1414 }
1415
1416 static const struct vfio_device_ops hisi_acc_vfio_pci_migrn_ops = {
1417 .name = "hisi-acc-vfio-pci-migration",
1418 .init = hisi_acc_vfio_pci_migrn_init_dev,
1419 .release = vfio_pci_core_release_dev,
1420 .open_device = hisi_acc_vfio_pci_open_device,
1421 .close_device = hisi_acc_vfio_pci_close_device,
1422 .ioctl = hisi_acc_vfio_pci_ioctl,
1423 .device_feature = vfio_pci_core_ioctl_feature,
1424 .read = hisi_acc_vfio_pci_read,
1425 .write = hisi_acc_vfio_pci_write,
1426 .mmap = hisi_acc_vfio_pci_mmap,
1427 .request = vfio_pci_core_request,
1428 .match = vfio_pci_core_match,
1429 .bind_iommufd = vfio_iommufd_physical_bind,
1430 .unbind_iommufd = vfio_iommufd_physical_unbind,
1431 .attach_ioas = vfio_iommufd_physical_attach_ioas,
1432 .detach_ioas = vfio_iommufd_physical_detach_ioas,
1433 };
1434
1435 static const struct vfio_device_ops hisi_acc_vfio_pci_ops = {
1436 .name = "hisi-acc-vfio-pci",
1437 .init = vfio_pci_core_init_dev,
1438 .release = vfio_pci_core_release_dev,
1439 .open_device = hisi_acc_vfio_pci_open_device,
1440 .close_device = vfio_pci_core_close_device,
1441 .ioctl = vfio_pci_core_ioctl,
1442 .device_feature = vfio_pci_core_ioctl_feature,
1443 .read = vfio_pci_core_read,
1444 .write = vfio_pci_core_write,
1445 .mmap = vfio_pci_core_mmap,
1446 .request = vfio_pci_core_request,
1447 .match = vfio_pci_core_match,
1448 .bind_iommufd = vfio_iommufd_physical_bind,
1449 .unbind_iommufd = vfio_iommufd_physical_unbind,
1450 .attach_ioas = vfio_iommufd_physical_attach_ioas,
1451 .detach_ioas = vfio_iommufd_physical_detach_ioas,
1452 };
1453
hisi_acc_vfio_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)1454 static int hisi_acc_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1455 {
1456 struct hisi_acc_vf_core_device *hisi_acc_vdev;
1457 const struct vfio_device_ops *ops = &hisi_acc_vfio_pci_ops;
1458 struct hisi_qm *pf_qm;
1459 int vf_id;
1460 int ret;
1461
1462 pf_qm = hisi_acc_get_pf_qm(pdev);
1463 if (pf_qm && pf_qm->ver >= QM_HW_V3) {
1464 vf_id = pci_iov_vf_id(pdev);
1465 if (vf_id >= 0)
1466 ops = &hisi_acc_vfio_pci_migrn_ops;
1467 else
1468 pci_warn(pdev, "migration support failed, continue with generic interface\n");
1469 }
1470
1471 hisi_acc_vdev = vfio_alloc_device(hisi_acc_vf_core_device,
1472 core_device.vdev, &pdev->dev, ops);
1473 if (IS_ERR(hisi_acc_vdev))
1474 return PTR_ERR(hisi_acc_vdev);
1475
1476 dev_set_drvdata(&pdev->dev, &hisi_acc_vdev->core_device);
1477 ret = vfio_pci_core_register_device(&hisi_acc_vdev->core_device);
1478 if (ret)
1479 goto out_put_vdev;
1480 return 0;
1481
1482 out_put_vdev:
1483 vfio_put_device(&hisi_acc_vdev->core_device.vdev);
1484 return ret;
1485 }
1486
hisi_acc_vfio_pci_remove(struct pci_dev * pdev)1487 static void hisi_acc_vfio_pci_remove(struct pci_dev *pdev)
1488 {
1489 struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_drvdata(pdev);
1490
1491 vfio_pci_core_unregister_device(&hisi_acc_vdev->core_device);
1492 vfio_put_device(&hisi_acc_vdev->core_device.vdev);
1493 }
1494
1495 static const struct pci_device_id hisi_acc_vfio_pci_table[] = {
1496 { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_HUAWEI_SEC_VF) },
1497 { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_HUAWEI_HPRE_VF) },
1498 { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_HUAWEI_ZIP_VF) },
1499 { }
1500 };
1501
1502 MODULE_DEVICE_TABLE(pci, hisi_acc_vfio_pci_table);
1503
1504 static const struct pci_error_handlers hisi_acc_vf_err_handlers = {
1505 .reset_done = hisi_acc_vf_pci_aer_reset_done,
1506 .error_detected = vfio_pci_core_aer_err_detected,
1507 };
1508
1509 static struct pci_driver hisi_acc_vfio_pci_driver = {
1510 .name = KBUILD_MODNAME,
1511 .id_table = hisi_acc_vfio_pci_table,
1512 .probe = hisi_acc_vfio_pci_probe,
1513 .remove = hisi_acc_vfio_pci_remove,
1514 .err_handler = &hisi_acc_vf_err_handlers,
1515 .driver_managed_dma = true,
1516 };
1517
1518 module_pci_driver(hisi_acc_vfio_pci_driver);
1519
1520 MODULE_LICENSE("GPL v2");
1521 MODULE_AUTHOR("Liu Longfang <liulongfang@huawei.com>");
1522 MODULE_AUTHOR("Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>");
1523 MODULE_DESCRIPTION("HiSilicon VFIO PCI - VFIO PCI driver with live migration support for HiSilicon ACC device family");
1524