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