1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Intel IFC VF NIC driver for virtio dataplane offloading 4 * 5 * Copyright (C) 2020 Intel Corporation. 6 * 7 * Author: Zhu Lingshan <lingshan.zhu@intel.com> 8 * 9 */ 10 11 #include <linux/interrupt.h> 12 #include <linux/module.h> 13 #include <linux/pci.h> 14 #include <linux/sysfs.h> 15 #include "ifcvf_base.h" 16 17 #define DRIVER_AUTHOR "Intel Corporation" 18 #define IFCVF_DRIVER_NAME "ifcvf" 19 20 static irqreturn_t ifcvf_config_changed(int irq, void *arg) 21 { 22 struct ifcvf_hw *vf = arg; 23 24 if (vf->config_cb.callback) 25 return vf->config_cb.callback(vf->config_cb.private); 26 27 return IRQ_HANDLED; 28 } 29 30 static irqreturn_t ifcvf_vq_intr_handler(int irq, void *arg) 31 { 32 struct vring_info *vring = arg; 33 34 if (vring->cb.callback) 35 return vring->cb.callback(vring->cb.private); 36 37 return IRQ_HANDLED; 38 } 39 40 static irqreturn_t ifcvf_vqs_reused_intr_handler(int irq, void *arg) 41 { 42 struct ifcvf_hw *vf = arg; 43 struct vring_info *vring; 44 int i; 45 46 for (i = 0; i < vf->nr_vring; i++) { 47 vring = &vf->vring[i]; 48 if (vring->cb.callback) 49 vring->cb.callback(vring->cb.private); 50 } 51 52 return IRQ_HANDLED; 53 } 54 55 static irqreturn_t ifcvf_dev_intr_handler(int irq, void *arg) 56 { 57 struct ifcvf_hw *vf = arg; 58 u8 isr; 59 60 isr = vp_ioread8(vf->isr); 61 if (isr & VIRTIO_PCI_ISR_CONFIG) 62 ifcvf_config_changed(irq, arg); 63 64 return ifcvf_vqs_reused_intr_handler(irq, arg); 65 } 66 67 static void ifcvf_free_irq_vectors(void *data) 68 { 69 pci_free_irq_vectors(data); 70 } 71 72 static void ifcvf_free_per_vq_irq(struct ifcvf_hw *vf) 73 { 74 struct pci_dev *pdev = vf->pdev; 75 int i; 76 77 for (i = 0; i < vf->nr_vring; i++) { 78 if (vf->vring[i].irq != -EINVAL) { 79 devm_free_irq(&pdev->dev, vf->vring[i].irq, &vf->vring[i]); 80 vf->vring[i].irq = -EINVAL; 81 } 82 } 83 } 84 85 static void ifcvf_free_vqs_reused_irq(struct ifcvf_hw *vf) 86 { 87 struct pci_dev *pdev = vf->pdev; 88 89 if (vf->vqs_reused_irq != -EINVAL) { 90 devm_free_irq(&pdev->dev, vf->vqs_reused_irq, vf); 91 vf->vqs_reused_irq = -EINVAL; 92 } 93 94 } 95 96 static void ifcvf_free_vq_irq(struct ifcvf_hw *vf) 97 { 98 if (vf->msix_vector_status == MSIX_VECTOR_PER_VQ_AND_CONFIG) 99 ifcvf_free_per_vq_irq(vf); 100 else 101 ifcvf_free_vqs_reused_irq(vf); 102 } 103 104 static void ifcvf_free_config_irq(struct ifcvf_hw *vf) 105 { 106 struct pci_dev *pdev = vf->pdev; 107 108 if (vf->config_irq == -EINVAL) 109 return; 110 111 /* If the irq is shared by all vqs and the config interrupt, 112 * it is already freed in ifcvf_free_vq_irq, so here only 113 * need to free config irq when msix_vector_status != MSIX_VECTOR_DEV_SHARED 114 */ 115 if (vf->msix_vector_status != MSIX_VECTOR_DEV_SHARED) { 116 devm_free_irq(&pdev->dev, vf->config_irq, vf); 117 vf->config_irq = -EINVAL; 118 } 119 } 120 121 static void ifcvf_free_irq(struct ifcvf_hw *vf) 122 { 123 struct pci_dev *pdev = vf->pdev; 124 125 ifcvf_free_vq_irq(vf); 126 ifcvf_free_config_irq(vf); 127 ifcvf_free_irq_vectors(pdev); 128 vf->num_msix_vectors = 0; 129 } 130 131 /* ifcvf MSIX vectors allocator, this helper tries to allocate 132 * vectors for all virtqueues and the config interrupt. 133 * It returns the number of allocated vectors, negative 134 * return value when fails. 135 */ 136 static int ifcvf_alloc_vectors(struct ifcvf_hw *vf) 137 { 138 struct pci_dev *pdev = vf->pdev; 139 int max_intr, ret; 140 141 /* all queues and config interrupt */ 142 max_intr = vf->nr_vring + 1; 143 ret = pci_alloc_irq_vectors(pdev, 1, max_intr, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY); 144 145 if (ret < 0) { 146 IFCVF_ERR(pdev, "Failed to alloc IRQ vectors\n"); 147 return ret; 148 } 149 150 if (ret < max_intr) 151 IFCVF_INFO(pdev, 152 "Requested %u vectors, however only %u allocated, lower performance\n", 153 max_intr, ret); 154 155 return ret; 156 } 157 158 static int ifcvf_request_per_vq_irq(struct ifcvf_hw *vf) 159 { 160 struct pci_dev *pdev = vf->pdev; 161 int i, vector, ret, irq; 162 163 vf->vqs_reused_irq = -EINVAL; 164 for (i = 0; i < vf->nr_vring; i++) { 165 snprintf(vf->vring[i].msix_name, 256, "ifcvf[%s]-%d\n", pci_name(pdev), i); 166 vector = i; 167 irq = pci_irq_vector(pdev, vector); 168 ret = devm_request_irq(&pdev->dev, irq, 169 ifcvf_vq_intr_handler, 0, 170 vf->vring[i].msix_name, 171 &vf->vring[i]); 172 if (ret) { 173 IFCVF_ERR(pdev, "Failed to request irq for vq %d\n", i); 174 goto err; 175 } 176 177 vf->vring[i].irq = irq; 178 ret = ifcvf_set_vq_vector(vf, i, vector); 179 if (ret == VIRTIO_MSI_NO_VECTOR) { 180 IFCVF_ERR(pdev, "No msix vector for vq %u\n", i); 181 goto err; 182 } 183 } 184 185 return 0; 186 err: 187 ifcvf_free_irq(vf); 188 189 return -EFAULT; 190 } 191 192 static int ifcvf_request_vqs_reused_irq(struct ifcvf_hw *vf) 193 { 194 struct pci_dev *pdev = vf->pdev; 195 int i, vector, ret, irq; 196 197 vector = 0; 198 snprintf(vf->vring[0].msix_name, 256, "ifcvf[%s]-vqs-reused-irq\n", pci_name(pdev)); 199 irq = pci_irq_vector(pdev, vector); 200 ret = devm_request_irq(&pdev->dev, irq, 201 ifcvf_vqs_reused_intr_handler, 0, 202 vf->vring[0].msix_name, vf); 203 if (ret) { 204 IFCVF_ERR(pdev, "Failed to request reused irq for the device\n"); 205 goto err; 206 } 207 208 vf->vqs_reused_irq = irq; 209 for (i = 0; i < vf->nr_vring; i++) { 210 vf->vring[i].irq = -EINVAL; 211 ret = ifcvf_set_vq_vector(vf, i, vector); 212 if (ret == VIRTIO_MSI_NO_VECTOR) { 213 IFCVF_ERR(pdev, "No msix vector for vq %u\n", i); 214 goto err; 215 } 216 } 217 218 return 0; 219 err: 220 ifcvf_free_irq(vf); 221 222 return -EFAULT; 223 } 224 225 static int ifcvf_request_dev_irq(struct ifcvf_hw *vf) 226 { 227 struct pci_dev *pdev = vf->pdev; 228 int i, vector, ret, irq; 229 230 vector = 0; 231 snprintf(vf->vring[0].msix_name, 256, "ifcvf[%s]-dev-irq\n", pci_name(pdev)); 232 irq = pci_irq_vector(pdev, vector); 233 ret = devm_request_irq(&pdev->dev, irq, 234 ifcvf_dev_intr_handler, 0, 235 vf->vring[0].msix_name, vf); 236 if (ret) { 237 IFCVF_ERR(pdev, "Failed to request irq for the device\n"); 238 goto err; 239 } 240 241 vf->vqs_reused_irq = irq; 242 for (i = 0; i < vf->nr_vring; i++) { 243 vf->vring[i].irq = -EINVAL; 244 ret = ifcvf_set_vq_vector(vf, i, vector); 245 if (ret == VIRTIO_MSI_NO_VECTOR) { 246 IFCVF_ERR(pdev, "No msix vector for vq %u\n", i); 247 goto err; 248 } 249 } 250 251 vf->config_irq = irq; 252 ret = ifcvf_set_config_vector(vf, vector); 253 if (ret == VIRTIO_MSI_NO_VECTOR) { 254 IFCVF_ERR(pdev, "No msix vector for device config\n"); 255 goto err; 256 } 257 258 return 0; 259 err: 260 ifcvf_free_irq(vf); 261 262 return -EFAULT; 263 264 } 265 266 static int ifcvf_request_vq_irq(struct ifcvf_hw *vf) 267 { 268 int ret; 269 270 if (vf->msix_vector_status == MSIX_VECTOR_PER_VQ_AND_CONFIG) 271 ret = ifcvf_request_per_vq_irq(vf); 272 else 273 ret = ifcvf_request_vqs_reused_irq(vf); 274 275 return ret; 276 } 277 278 static int ifcvf_request_config_irq(struct ifcvf_hw *vf) 279 { 280 struct pci_dev *pdev = vf->pdev; 281 int config_vector, ret; 282 283 if (vf->msix_vector_status == MSIX_VECTOR_PER_VQ_AND_CONFIG) 284 config_vector = vf->nr_vring; 285 else if (vf->msix_vector_status == MSIX_VECTOR_SHARED_VQ_AND_CONFIG) 286 /* vector 0 for vqs and 1 for config interrupt */ 287 config_vector = 1; 288 else if (vf->msix_vector_status == MSIX_VECTOR_DEV_SHARED) 289 /* re-use the vqs vector */ 290 return 0; 291 else 292 return -EINVAL; 293 294 snprintf(vf->config_msix_name, 256, "ifcvf[%s]-config\n", 295 pci_name(pdev)); 296 vf->config_irq = pci_irq_vector(pdev, config_vector); 297 ret = devm_request_irq(&pdev->dev, vf->config_irq, 298 ifcvf_config_changed, 0, 299 vf->config_msix_name, vf); 300 if (ret) { 301 IFCVF_ERR(pdev, "Failed to request config irq\n"); 302 goto err; 303 } 304 305 ret = ifcvf_set_config_vector(vf, config_vector); 306 if (ret == VIRTIO_MSI_NO_VECTOR) { 307 IFCVF_ERR(pdev, "No msix vector for device config\n"); 308 goto err; 309 } 310 311 return 0; 312 err: 313 ifcvf_free_irq(vf); 314 315 return -EFAULT; 316 } 317 318 static int ifcvf_request_irq(struct ifcvf_hw *vf) 319 { 320 int nvectors, ret, max_intr; 321 322 nvectors = ifcvf_alloc_vectors(vf); 323 if (nvectors <= 0) 324 return -EFAULT; 325 326 vf->msix_vector_status = MSIX_VECTOR_PER_VQ_AND_CONFIG; 327 max_intr = vf->nr_vring + 1; 328 if (nvectors < max_intr) 329 vf->msix_vector_status = MSIX_VECTOR_SHARED_VQ_AND_CONFIG; 330 331 if (nvectors == 1) { 332 vf->msix_vector_status = MSIX_VECTOR_DEV_SHARED; 333 ret = ifcvf_request_dev_irq(vf); 334 335 return ret; 336 } 337 338 ret = ifcvf_request_vq_irq(vf); 339 if (ret) 340 return ret; 341 342 ret = ifcvf_request_config_irq(vf); 343 344 if (ret) 345 return ret; 346 347 vf->num_msix_vectors = nvectors; 348 349 return 0; 350 } 351 352 static struct ifcvf_adapter *vdpa_to_adapter(struct vdpa_device *vdpa_dev) 353 { 354 return container_of(vdpa_dev, struct ifcvf_adapter, vdpa); 355 } 356 357 static struct ifcvf_hw *vdpa_to_vf(struct vdpa_device *vdpa_dev) 358 { 359 struct ifcvf_adapter *adapter = vdpa_to_adapter(vdpa_dev); 360 361 return adapter->vf; 362 } 363 364 static u64 ifcvf_vdpa_get_device_features(struct vdpa_device *vdpa_dev) 365 { 366 struct ifcvf_adapter *adapter = vdpa_to_adapter(vdpa_dev); 367 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 368 struct pci_dev *pdev = adapter->pdev; 369 u32 type = vf->dev_type; 370 u64 features; 371 372 if (type == VIRTIO_ID_NET || type == VIRTIO_ID_BLOCK) 373 features = ifcvf_get_dev_features(vf); 374 else { 375 features = 0; 376 IFCVF_ERR(pdev, "VIRTIO ID %u not supported\n", vf->dev_type); 377 } 378 379 return features; 380 } 381 382 static int ifcvf_vdpa_set_driver_features(struct vdpa_device *vdpa_dev, u64 features) 383 { 384 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 385 int ret; 386 387 ret = ifcvf_verify_min_features(vf, features); 388 if (ret) 389 return ret; 390 391 ifcvf_set_driver_features(vf, features); 392 393 return 0; 394 } 395 396 static u64 ifcvf_vdpa_get_driver_features(struct vdpa_device *vdpa_dev) 397 { 398 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 399 u64 features; 400 401 features = ifcvf_get_driver_features(vf); 402 403 return features; 404 } 405 406 static u8 ifcvf_vdpa_get_status(struct vdpa_device *vdpa_dev) 407 { 408 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 409 410 return ifcvf_get_status(vf); 411 } 412 413 static void ifcvf_vdpa_set_status(struct vdpa_device *vdpa_dev, u8 status) 414 { 415 struct ifcvf_hw *vf; 416 u8 status_old; 417 int ret; 418 419 vf = vdpa_to_vf(vdpa_dev); 420 status_old = ifcvf_get_status(vf); 421 422 if (status_old == status) 423 return; 424 425 if ((status & VIRTIO_CONFIG_S_DRIVER_OK) && 426 !(status_old & VIRTIO_CONFIG_S_DRIVER_OK)) { 427 ret = ifcvf_request_irq(vf); 428 if (ret) { 429 IFCVF_ERR(vf->pdev, "failed to request irq with error %d\n", ret); 430 return; 431 } 432 } 433 434 ifcvf_set_status(vf, status); 435 } 436 437 static int ifcvf_vdpa_reset(struct vdpa_device *vdpa_dev) 438 { 439 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 440 u8 status = ifcvf_get_status(vf); 441 442 ifcvf_stop(vf); 443 444 if (status & VIRTIO_CONFIG_S_DRIVER_OK) 445 ifcvf_free_irq(vf); 446 447 ifcvf_reset(vf); 448 449 return 0; 450 } 451 452 static u16 ifcvf_vdpa_get_vq_num_max(struct vdpa_device *vdpa_dev) 453 { 454 return IFCVF_QUEUE_MAX; 455 } 456 457 static int ifcvf_vdpa_get_vq_state(struct vdpa_device *vdpa_dev, u16 qid, 458 struct vdpa_vq_state *state) 459 { 460 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 461 462 state->split.avail_index = ifcvf_get_vq_state(vf, qid); 463 return 0; 464 } 465 466 static int ifcvf_vdpa_set_vq_state(struct vdpa_device *vdpa_dev, u16 qid, 467 const struct vdpa_vq_state *state) 468 { 469 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 470 471 return ifcvf_set_vq_state(vf, qid, state->split.avail_index); 472 } 473 474 static void ifcvf_vdpa_set_vq_cb(struct vdpa_device *vdpa_dev, u16 qid, 475 struct vdpa_callback *cb) 476 { 477 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 478 479 vf->vring[qid].cb = *cb; 480 } 481 482 static void ifcvf_vdpa_set_vq_ready(struct vdpa_device *vdpa_dev, 483 u16 qid, bool ready) 484 { 485 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 486 487 ifcvf_set_vq_ready(vf, qid, ready); 488 } 489 490 static bool ifcvf_vdpa_get_vq_ready(struct vdpa_device *vdpa_dev, u16 qid) 491 { 492 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 493 494 return ifcvf_get_vq_ready(vf, qid); 495 } 496 497 static void ifcvf_vdpa_set_vq_num(struct vdpa_device *vdpa_dev, u16 qid, 498 u32 num) 499 { 500 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 501 502 ifcvf_set_vq_num(vf, qid, num); 503 } 504 505 static int ifcvf_vdpa_set_vq_address(struct vdpa_device *vdpa_dev, u16 qid, 506 u64 desc_area, u64 driver_area, 507 u64 device_area) 508 { 509 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 510 511 return ifcvf_set_vq_address(vf, qid, desc_area, driver_area, device_area); 512 } 513 514 static void ifcvf_vdpa_kick_vq(struct vdpa_device *vdpa_dev, u16 qid) 515 { 516 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 517 518 ifcvf_notify_queue(vf, qid); 519 } 520 521 static u32 ifcvf_vdpa_get_generation(struct vdpa_device *vdpa_dev) 522 { 523 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 524 525 return vp_ioread8(&vf->common_cfg->config_generation); 526 } 527 528 static u32 ifcvf_vdpa_get_device_id(struct vdpa_device *vdpa_dev) 529 { 530 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 531 532 return vf->dev_type; 533 } 534 535 static u32 ifcvf_vdpa_get_vendor_id(struct vdpa_device *vdpa_dev) 536 { 537 struct ifcvf_adapter *adapter = vdpa_to_adapter(vdpa_dev); 538 struct pci_dev *pdev = adapter->pdev; 539 540 return pdev->subsystem_vendor; 541 } 542 543 static u32 ifcvf_vdpa_get_vq_align(struct vdpa_device *vdpa_dev) 544 { 545 return IFCVF_QUEUE_ALIGNMENT; 546 } 547 548 static size_t ifcvf_vdpa_get_config_size(struct vdpa_device *vdpa_dev) 549 { 550 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 551 552 return vf->config_size; 553 } 554 555 static u32 ifcvf_vdpa_get_vq_group(struct vdpa_device *vdpa, u16 idx) 556 { 557 return 0; 558 } 559 560 static void ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev, 561 unsigned int offset, 562 void *buf, unsigned int len) 563 { 564 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 565 566 ifcvf_read_dev_config(vf, offset, buf, len); 567 } 568 569 static void ifcvf_vdpa_set_config(struct vdpa_device *vdpa_dev, 570 unsigned int offset, const void *buf, 571 unsigned int len) 572 { 573 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 574 575 ifcvf_write_dev_config(vf, offset, buf, len); 576 } 577 578 static void ifcvf_vdpa_set_config_cb(struct vdpa_device *vdpa_dev, 579 struct vdpa_callback *cb) 580 { 581 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 582 583 vf->config_cb.callback = cb->callback; 584 vf->config_cb.private = cb->private; 585 } 586 587 static int ifcvf_vdpa_get_vq_irq(struct vdpa_device *vdpa_dev, 588 u16 qid) 589 { 590 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 591 592 if (vf->vqs_reused_irq < 0) 593 return vf->vring[qid].irq; 594 else 595 return -EINVAL; 596 } 597 598 static struct vdpa_notification_area ifcvf_get_vq_notification(struct vdpa_device *vdpa_dev, 599 u16 idx) 600 { 601 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 602 struct vdpa_notification_area area; 603 604 area.addr = vf->vring[idx].notify_pa; 605 if (!vf->notify_off_multiplier) 606 area.size = PAGE_SIZE; 607 else 608 area.size = vf->notify_off_multiplier; 609 610 return area; 611 } 612 613 /* 614 * IFCVF currently doesn't have on-chip IOMMU, so not 615 * implemented set_map()/dma_map()/dma_unmap() 616 */ 617 static const struct vdpa_config_ops ifc_vdpa_ops = { 618 .get_device_features = ifcvf_vdpa_get_device_features, 619 .set_driver_features = ifcvf_vdpa_set_driver_features, 620 .get_driver_features = ifcvf_vdpa_get_driver_features, 621 .get_status = ifcvf_vdpa_get_status, 622 .set_status = ifcvf_vdpa_set_status, 623 .reset = ifcvf_vdpa_reset, 624 .get_vq_num_max = ifcvf_vdpa_get_vq_num_max, 625 .get_vq_state = ifcvf_vdpa_get_vq_state, 626 .set_vq_state = ifcvf_vdpa_set_vq_state, 627 .set_vq_cb = ifcvf_vdpa_set_vq_cb, 628 .set_vq_ready = ifcvf_vdpa_set_vq_ready, 629 .get_vq_ready = ifcvf_vdpa_get_vq_ready, 630 .set_vq_num = ifcvf_vdpa_set_vq_num, 631 .set_vq_address = ifcvf_vdpa_set_vq_address, 632 .get_vq_irq = ifcvf_vdpa_get_vq_irq, 633 .kick_vq = ifcvf_vdpa_kick_vq, 634 .get_generation = ifcvf_vdpa_get_generation, 635 .get_device_id = ifcvf_vdpa_get_device_id, 636 .get_vendor_id = ifcvf_vdpa_get_vendor_id, 637 .get_vq_align = ifcvf_vdpa_get_vq_align, 638 .get_vq_group = ifcvf_vdpa_get_vq_group, 639 .get_config_size = ifcvf_vdpa_get_config_size, 640 .get_config = ifcvf_vdpa_get_config, 641 .set_config = ifcvf_vdpa_set_config, 642 .set_config_cb = ifcvf_vdpa_set_config_cb, 643 .get_vq_notification = ifcvf_get_vq_notification, 644 }; 645 646 static struct virtio_device_id id_table_net[] = { 647 {VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID}, 648 {0}, 649 }; 650 651 static struct virtio_device_id id_table_blk[] = { 652 {VIRTIO_ID_BLOCK, VIRTIO_DEV_ANY_ID}, 653 {0}, 654 }; 655 656 static u32 get_dev_type(struct pci_dev *pdev) 657 { 658 u32 dev_type; 659 660 /* This drirver drives both modern virtio devices and transitional 661 * devices in modern mode. 662 * vDPA requires feature bit VIRTIO_F_ACCESS_PLATFORM, 663 * so legacy devices and transitional devices in legacy 664 * mode will not work for vDPA, this driver will not 665 * drive devices with legacy interface. 666 */ 667 668 if (pdev->device < 0x1040) 669 dev_type = pdev->subsystem_device; 670 else 671 dev_type = pdev->device - 0x1040; 672 673 return dev_type; 674 } 675 676 static int ifcvf_vdpa_dev_add(struct vdpa_mgmt_dev *mdev, const char *name, 677 const struct vdpa_dev_set_config *config) 678 { 679 struct ifcvf_vdpa_mgmt_dev *ifcvf_mgmt_dev; 680 struct ifcvf_adapter *adapter; 681 struct vdpa_device *vdpa_dev; 682 struct pci_dev *pdev; 683 struct ifcvf_hw *vf; 684 u64 device_features; 685 int ret; 686 687 ifcvf_mgmt_dev = container_of(mdev, struct ifcvf_vdpa_mgmt_dev, mdev); 688 vf = &ifcvf_mgmt_dev->vf; 689 pdev = vf->pdev; 690 adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa, 691 &pdev->dev, &ifc_vdpa_ops, 1, 1, NULL, false); 692 if (IS_ERR(adapter)) { 693 IFCVF_ERR(pdev, "Failed to allocate vDPA structure"); 694 return PTR_ERR(adapter); 695 } 696 697 ifcvf_mgmt_dev->adapter = adapter; 698 adapter->pdev = pdev; 699 adapter->vdpa.dma_dev = &pdev->dev; 700 adapter->vdpa.mdev = mdev; 701 adapter->vf = vf; 702 vdpa_dev = &adapter->vdpa; 703 704 device_features = vf->hw_features; 705 if (config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) { 706 if (config->device_features & ~device_features) { 707 IFCVF_ERR(pdev, "The provisioned features 0x%llx are not supported by this device with features 0x%llx\n", 708 config->device_features, device_features); 709 return -EINVAL; 710 } 711 device_features &= config->device_features; 712 } 713 vf->dev_features = device_features; 714 715 if (name) 716 ret = dev_set_name(&vdpa_dev->dev, "%s", name); 717 else 718 ret = dev_set_name(&vdpa_dev->dev, "vdpa%u", vdpa_dev->index); 719 720 ret = _vdpa_register_device(&adapter->vdpa, vf->nr_vring); 721 if (ret) { 722 put_device(&adapter->vdpa.dev); 723 IFCVF_ERR(pdev, "Failed to register to vDPA bus"); 724 return ret; 725 } 726 727 return 0; 728 } 729 730 static void ifcvf_vdpa_dev_del(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev) 731 { 732 struct ifcvf_vdpa_mgmt_dev *ifcvf_mgmt_dev; 733 734 ifcvf_mgmt_dev = container_of(mdev, struct ifcvf_vdpa_mgmt_dev, mdev); 735 _vdpa_unregister_device(dev); 736 ifcvf_mgmt_dev->adapter = NULL; 737 } 738 739 static const struct vdpa_mgmtdev_ops ifcvf_vdpa_mgmt_dev_ops = { 740 .dev_add = ifcvf_vdpa_dev_add, 741 .dev_del = ifcvf_vdpa_dev_del 742 }; 743 744 static int ifcvf_probe(struct pci_dev *pdev, const struct pci_device_id *id) 745 { 746 struct ifcvf_vdpa_mgmt_dev *ifcvf_mgmt_dev; 747 struct device *dev = &pdev->dev; 748 struct ifcvf_hw *vf; 749 u32 dev_type; 750 int ret, i; 751 752 ret = pcim_enable_device(pdev); 753 if (ret) { 754 IFCVF_ERR(pdev, "Failed to enable device\n"); 755 return ret; 756 } 757 ret = pcim_iomap_regions(pdev, BIT(0) | BIT(2) | BIT(4), 758 IFCVF_DRIVER_NAME); 759 if (ret) { 760 IFCVF_ERR(pdev, "Failed to request MMIO region\n"); 761 return ret; 762 } 763 764 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); 765 if (ret) { 766 IFCVF_ERR(pdev, "No usable DMA configuration\n"); 767 return ret; 768 } 769 770 ret = devm_add_action_or_reset(dev, ifcvf_free_irq_vectors, pdev); 771 if (ret) { 772 IFCVF_ERR(pdev, 773 "Failed for adding devres for freeing irq vectors\n"); 774 return ret; 775 } 776 777 pci_set_master(pdev); 778 ifcvf_mgmt_dev = kzalloc(sizeof(struct ifcvf_vdpa_mgmt_dev), GFP_KERNEL); 779 if (!ifcvf_mgmt_dev) { 780 IFCVF_ERR(pdev, "Failed to alloc memory for the vDPA management device\n"); 781 return -ENOMEM; 782 } 783 784 vf = &ifcvf_mgmt_dev->vf; 785 vf->dev_type = get_dev_type(pdev); 786 vf->base = pcim_iomap_table(pdev); 787 vf->pdev = pdev; 788 789 ret = ifcvf_init_hw(vf, pdev); 790 if (ret) { 791 IFCVF_ERR(pdev, "Failed to init IFCVF hw\n"); 792 goto err; 793 } 794 795 for (i = 0; i < vf->nr_vring; i++) 796 vf->vring[i].irq = -EINVAL; 797 798 vf->hw_features = ifcvf_get_hw_features(vf); 799 vf->config_size = ifcvf_get_config_size(vf); 800 801 dev_type = get_dev_type(pdev); 802 switch (dev_type) { 803 case VIRTIO_ID_NET: 804 ifcvf_mgmt_dev->mdev.id_table = id_table_net; 805 break; 806 case VIRTIO_ID_BLOCK: 807 ifcvf_mgmt_dev->mdev.id_table = id_table_blk; 808 break; 809 default: 810 IFCVF_ERR(pdev, "VIRTIO ID %u not supported\n", dev_type); 811 ret = -EOPNOTSUPP; 812 goto err; 813 } 814 815 ifcvf_mgmt_dev->mdev.ops = &ifcvf_vdpa_mgmt_dev_ops; 816 ifcvf_mgmt_dev->mdev.device = dev; 817 ifcvf_mgmt_dev->mdev.max_supported_vqs = vf->nr_vring; 818 ifcvf_mgmt_dev->mdev.supported_features = vf->hw_features; 819 ifcvf_mgmt_dev->mdev.config_attr_mask = (1 << VDPA_ATTR_DEV_FEATURES); 820 821 ret = vdpa_mgmtdev_register(&ifcvf_mgmt_dev->mdev); 822 if (ret) { 823 IFCVF_ERR(pdev, 824 "Failed to initialize the management interfaces\n"); 825 goto err; 826 } 827 828 pci_set_drvdata(pdev, ifcvf_mgmt_dev); 829 830 return 0; 831 832 err: 833 kfree(ifcvf_mgmt_dev); 834 return ret; 835 } 836 837 static void ifcvf_remove(struct pci_dev *pdev) 838 { 839 struct ifcvf_vdpa_mgmt_dev *ifcvf_mgmt_dev; 840 841 ifcvf_mgmt_dev = pci_get_drvdata(pdev); 842 vdpa_mgmtdev_unregister(&ifcvf_mgmt_dev->mdev); 843 kfree(ifcvf_mgmt_dev); 844 } 845 846 static struct pci_device_id ifcvf_pci_ids[] = { 847 /* N3000 network device */ 848 { PCI_DEVICE_SUB(PCI_VENDOR_ID_REDHAT_QUMRANET, 849 N3000_DEVICE_ID, 850 PCI_VENDOR_ID_INTEL, 851 N3000_SUBSYS_DEVICE_ID) }, 852 /* C5000X-PL network device */ 853 { PCI_DEVICE_SUB(PCI_VENDOR_ID_REDHAT_QUMRANET, 854 VIRTIO_TRANS_ID_NET, 855 PCI_VENDOR_ID_INTEL, 856 VIRTIO_ID_NET) }, 857 /* C5000X-PL block device */ 858 { PCI_DEVICE_SUB(PCI_VENDOR_ID_REDHAT_QUMRANET, 859 VIRTIO_TRANS_ID_BLOCK, 860 PCI_VENDOR_ID_INTEL, 861 VIRTIO_ID_BLOCK) }, 862 863 { 0 }, 864 }; 865 MODULE_DEVICE_TABLE(pci, ifcvf_pci_ids); 866 867 static struct pci_driver ifcvf_driver = { 868 .name = IFCVF_DRIVER_NAME, 869 .id_table = ifcvf_pci_ids, 870 .probe = ifcvf_probe, 871 .remove = ifcvf_remove, 872 }; 873 874 module_pci_driver(ifcvf_driver); 875 876 MODULE_LICENSE("GPL v2"); 877