1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright(c) 2023 Advanced Micro Devices, Inc */ 3 4 #include <linux/pci.h> 5 #include <linux/vdpa.h> 6 #include <uapi/linux/vdpa.h> 7 #include <linux/virtio_pci_modern.h> 8 9 #include <linux/pds/pds_common.h> 10 #include <linux/pds/pds_core_if.h> 11 #include <linux/pds/pds_adminq.h> 12 #include <linux/pds/pds_auxbus.h> 13 14 #include "vdpa_dev.h" 15 #include "aux_drv.h" 16 #include "cmds.h" 17 #include "debugfs.h" 18 19 static u64 pds_vdpa_get_driver_features(struct vdpa_device *vdpa_dev); 20 21 static struct pds_vdpa_device *vdpa_to_pdsv(struct vdpa_device *vdpa_dev) 22 { 23 return container_of(vdpa_dev, struct pds_vdpa_device, vdpa_dev); 24 } 25 26 static int pds_vdpa_notify_handler(struct notifier_block *nb, 27 unsigned long ecode, 28 void *data) 29 { 30 struct pds_vdpa_device *pdsv = container_of(nb, struct pds_vdpa_device, nb); 31 struct device *dev = &pdsv->vdpa_aux->padev->aux_dev.dev; 32 33 dev_dbg(dev, "%s: event code %lu\n", __func__, ecode); 34 35 if (ecode == PDS_EVENT_RESET || ecode == PDS_EVENT_LINK_CHANGE) { 36 if (pdsv->config_cb.callback) 37 pdsv->config_cb.callback(pdsv->config_cb.private); 38 } 39 40 return 0; 41 } 42 43 static int pds_vdpa_register_event_handler(struct pds_vdpa_device *pdsv) 44 { 45 struct device *dev = &pdsv->vdpa_aux->padev->aux_dev.dev; 46 struct notifier_block *nb = &pdsv->nb; 47 int err; 48 49 if (!nb->notifier_call) { 50 nb->notifier_call = pds_vdpa_notify_handler; 51 err = pdsc_register_notify(nb); 52 if (err) { 53 nb->notifier_call = NULL; 54 dev_err(dev, "failed to register pds event handler: %ps\n", 55 ERR_PTR(err)); 56 return -EINVAL; 57 } 58 dev_dbg(dev, "pds event handler registered\n"); 59 } 60 61 return 0; 62 } 63 64 static void pds_vdpa_unregister_event_handler(struct pds_vdpa_device *pdsv) 65 { 66 if (pdsv->nb.notifier_call) { 67 pdsc_unregister_notify(&pdsv->nb); 68 pdsv->nb.notifier_call = NULL; 69 } 70 } 71 72 static int pds_vdpa_set_vq_address(struct vdpa_device *vdpa_dev, u16 qid, 73 u64 desc_addr, u64 driver_addr, u64 device_addr) 74 { 75 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 76 77 pdsv->vqs[qid].desc_addr = desc_addr; 78 pdsv->vqs[qid].avail_addr = driver_addr; 79 pdsv->vqs[qid].used_addr = device_addr; 80 81 return 0; 82 } 83 84 static void pds_vdpa_set_vq_num(struct vdpa_device *vdpa_dev, u16 qid, u32 num) 85 { 86 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 87 88 pdsv->vqs[qid].q_len = num; 89 } 90 91 static void pds_vdpa_kick_vq(struct vdpa_device *vdpa_dev, u16 qid) 92 { 93 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 94 95 iowrite16(qid, pdsv->vqs[qid].notify); 96 } 97 98 static void pds_vdpa_set_vq_cb(struct vdpa_device *vdpa_dev, u16 qid, 99 struct vdpa_callback *cb) 100 { 101 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 102 103 pdsv->vqs[qid].event_cb = *cb; 104 } 105 106 static irqreturn_t pds_vdpa_isr(int irq, void *data) 107 { 108 struct pds_vdpa_vq_info *vq; 109 110 vq = data; 111 if (vq->event_cb.callback) 112 vq->event_cb.callback(vq->event_cb.private); 113 114 return IRQ_HANDLED; 115 } 116 117 static void pds_vdpa_release_irq(struct pds_vdpa_device *pdsv, int qid) 118 { 119 if (pdsv->vqs[qid].irq == VIRTIO_MSI_NO_VECTOR) 120 return; 121 122 free_irq(pdsv->vqs[qid].irq, &pdsv->vqs[qid]); 123 pdsv->vqs[qid].irq = VIRTIO_MSI_NO_VECTOR; 124 } 125 126 static void pds_vdpa_set_vq_ready(struct vdpa_device *vdpa_dev, u16 qid, bool ready) 127 { 128 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 129 struct pci_dev *pdev = pdsv->vdpa_aux->padev->vf_pdev; 130 struct device *dev = &pdsv->vdpa_dev.dev; 131 u64 driver_features; 132 u16 invert_idx = 0; 133 int irq; 134 int err; 135 136 dev_dbg(dev, "%s: qid %d ready %d => %d\n", 137 __func__, qid, pdsv->vqs[qid].ready, ready); 138 if (ready == pdsv->vqs[qid].ready) 139 return; 140 141 driver_features = pds_vdpa_get_driver_features(vdpa_dev); 142 if (driver_features & BIT_ULL(VIRTIO_F_RING_PACKED)) 143 invert_idx = PDS_VDPA_PACKED_INVERT_IDX; 144 145 if (ready) { 146 irq = pci_irq_vector(pdev, qid); 147 snprintf(pdsv->vqs[qid].irq_name, sizeof(pdsv->vqs[qid].irq_name), 148 "vdpa-%s-%d", dev_name(dev), qid); 149 150 err = request_irq(irq, pds_vdpa_isr, 0, 151 pdsv->vqs[qid].irq_name, &pdsv->vqs[qid]); 152 if (err) { 153 dev_err(dev, "%s: no irq for qid %d: %pe\n", 154 __func__, qid, ERR_PTR(err)); 155 return; 156 } 157 pdsv->vqs[qid].irq = irq; 158 159 /* Pass vq setup info to DSC using adminq to gather up and 160 * send all info at once so FW can do its full set up in 161 * one easy operation 162 */ 163 err = pds_vdpa_cmd_init_vq(pdsv, qid, invert_idx, &pdsv->vqs[qid]); 164 if (err) { 165 dev_err(dev, "Failed to init vq %d: %pe\n", 166 qid, ERR_PTR(err)); 167 pds_vdpa_release_irq(pdsv, qid); 168 ready = false; 169 } 170 } else { 171 err = pds_vdpa_cmd_reset_vq(pdsv, qid, invert_idx, &pdsv->vqs[qid]); 172 if (err) 173 dev_err(dev, "%s: reset_vq failed qid %d: %pe\n", 174 __func__, qid, ERR_PTR(err)); 175 pds_vdpa_release_irq(pdsv, qid); 176 } 177 178 pdsv->vqs[qid].ready = ready; 179 } 180 181 static bool pds_vdpa_get_vq_ready(struct vdpa_device *vdpa_dev, u16 qid) 182 { 183 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 184 185 return pdsv->vqs[qid].ready; 186 } 187 188 static int pds_vdpa_set_vq_state(struct vdpa_device *vdpa_dev, u16 qid, 189 const struct vdpa_vq_state *state) 190 { 191 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 192 struct pds_auxiliary_dev *padev = pdsv->vdpa_aux->padev; 193 struct device *dev = &padev->aux_dev.dev; 194 u64 driver_features; 195 u16 avail; 196 u16 used; 197 198 if (pdsv->vqs[qid].ready) { 199 dev_err(dev, "Setting device position is denied while vq is enabled\n"); 200 return -EINVAL; 201 } 202 203 driver_features = pds_vdpa_get_driver_features(vdpa_dev); 204 if (driver_features & BIT_ULL(VIRTIO_F_RING_PACKED)) { 205 avail = state->packed.last_avail_idx | 206 (state->packed.last_avail_counter << 15); 207 used = state->packed.last_used_idx | 208 (state->packed.last_used_counter << 15); 209 210 /* The avail and used index are stored with the packed wrap 211 * counter bit inverted. This way, in case set_vq_state is 212 * not called, the initial value can be set to zero prior to 213 * feature negotiation, and it is good for both packed and 214 * split vq. 215 */ 216 avail ^= PDS_VDPA_PACKED_INVERT_IDX; 217 used ^= PDS_VDPA_PACKED_INVERT_IDX; 218 } else { 219 avail = state->split.avail_index; 220 /* state->split does not provide a used_index: 221 * the vq will be set to "empty" here, and the vq will read 222 * the current used index the next time the vq is kicked. 223 */ 224 used = avail; 225 } 226 227 if (used != avail) { 228 dev_dbg(dev, "Setting used equal to avail, for interoperability\n"); 229 used = avail; 230 } 231 232 pdsv->vqs[qid].avail_idx = avail; 233 pdsv->vqs[qid].used_idx = used; 234 235 return 0; 236 } 237 238 static int pds_vdpa_get_vq_state(struct vdpa_device *vdpa_dev, u16 qid, 239 struct vdpa_vq_state *state) 240 { 241 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 242 struct pds_auxiliary_dev *padev = pdsv->vdpa_aux->padev; 243 struct device *dev = &padev->aux_dev.dev; 244 u64 driver_features; 245 u16 avail; 246 u16 used; 247 248 if (pdsv->vqs[qid].ready) { 249 dev_err(dev, "Getting device position is denied while vq is enabled\n"); 250 return -EINVAL; 251 } 252 253 avail = pdsv->vqs[qid].avail_idx; 254 used = pdsv->vqs[qid].used_idx; 255 256 driver_features = pds_vdpa_get_driver_features(vdpa_dev); 257 if (driver_features & BIT_ULL(VIRTIO_F_RING_PACKED)) { 258 avail ^= PDS_VDPA_PACKED_INVERT_IDX; 259 used ^= PDS_VDPA_PACKED_INVERT_IDX; 260 261 state->packed.last_avail_idx = avail & 0x7fff; 262 state->packed.last_avail_counter = avail >> 15; 263 state->packed.last_used_idx = used & 0x7fff; 264 state->packed.last_used_counter = used >> 15; 265 } else { 266 state->split.avail_index = avail; 267 /* state->split does not provide a used_index. */ 268 } 269 270 return 0; 271 } 272 273 static struct vdpa_notification_area 274 pds_vdpa_get_vq_notification(struct vdpa_device *vdpa_dev, u16 qid) 275 { 276 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 277 struct virtio_pci_modern_device *vd_mdev; 278 struct vdpa_notification_area area; 279 280 area.addr = pdsv->vqs[qid].notify_pa; 281 282 vd_mdev = &pdsv->vdpa_aux->vd_mdev; 283 if (!vd_mdev->notify_offset_multiplier) 284 area.size = PDS_PAGE_SIZE; 285 else 286 area.size = vd_mdev->notify_offset_multiplier; 287 288 return area; 289 } 290 291 static int pds_vdpa_get_vq_irq(struct vdpa_device *vdpa_dev, u16 qid) 292 { 293 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 294 295 return pdsv->vqs[qid].irq; 296 } 297 298 static u32 pds_vdpa_get_vq_align(struct vdpa_device *vdpa_dev) 299 { 300 return PDS_PAGE_SIZE; 301 } 302 303 static u32 pds_vdpa_get_vq_group(struct vdpa_device *vdpa_dev, u16 idx) 304 { 305 return 0; 306 } 307 308 static u64 pds_vdpa_get_device_features(struct vdpa_device *vdpa_dev) 309 { 310 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 311 312 return pdsv->supported_features; 313 } 314 315 static int pds_vdpa_set_driver_features(struct vdpa_device *vdpa_dev, u64 features) 316 { 317 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 318 struct device *dev = &pdsv->vdpa_dev.dev; 319 u64 driver_features; 320 u64 nego_features; 321 u64 hw_features; 322 u64 missing; 323 324 if (!(features & BIT_ULL(VIRTIO_F_ACCESS_PLATFORM)) && features) { 325 dev_err(dev, "VIRTIO_F_ACCESS_PLATFORM is not negotiated\n"); 326 return -EOPNOTSUPP; 327 } 328 329 /* Check for valid feature bits */ 330 nego_features = features & pdsv->supported_features; 331 missing = features & ~nego_features; 332 if (missing) { 333 dev_err(dev, "Can't support all requested features in %#llx, missing %#llx features\n", 334 features, missing); 335 return -EOPNOTSUPP; 336 } 337 338 pdsv->negotiated_features = nego_features; 339 340 driver_features = pds_vdpa_get_driver_features(vdpa_dev); 341 dev_dbg(dev, "%s: %#llx => %#llx\n", 342 __func__, driver_features, nego_features); 343 344 /* if we're faking the F_MAC, strip it before writing to device */ 345 hw_features = le64_to_cpu(pdsv->vdpa_aux->ident.hw_features); 346 if (!(hw_features & BIT_ULL(VIRTIO_NET_F_MAC))) 347 nego_features &= ~BIT_ULL(VIRTIO_NET_F_MAC); 348 349 if (driver_features == nego_features) 350 return 0; 351 352 vp_modern_set_features(&pdsv->vdpa_aux->vd_mdev, nego_features); 353 354 return 0; 355 } 356 357 static u64 pds_vdpa_get_driver_features(struct vdpa_device *vdpa_dev) 358 { 359 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 360 361 return pdsv->negotiated_features; 362 } 363 364 static void pds_vdpa_set_config_cb(struct vdpa_device *vdpa_dev, 365 struct vdpa_callback *cb) 366 { 367 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 368 369 pdsv->config_cb.callback = cb->callback; 370 pdsv->config_cb.private = cb->private; 371 } 372 373 static u16 pds_vdpa_get_vq_num_max(struct vdpa_device *vdpa_dev) 374 { 375 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 376 377 /* qemu has assert() that vq_num_max <= VIRTQUEUE_MAX_SIZE (1024) */ 378 return min_t(u16, 1024, BIT(le16_to_cpu(pdsv->vdpa_aux->ident.max_qlen))); 379 } 380 381 static u32 pds_vdpa_get_device_id(struct vdpa_device *vdpa_dev) 382 { 383 return VIRTIO_ID_NET; 384 } 385 386 static u32 pds_vdpa_get_vendor_id(struct vdpa_device *vdpa_dev) 387 { 388 return PCI_VENDOR_ID_PENSANDO; 389 } 390 391 static u8 pds_vdpa_get_status(struct vdpa_device *vdpa_dev) 392 { 393 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 394 395 return vp_modern_get_status(&pdsv->vdpa_aux->vd_mdev); 396 } 397 398 static void pds_vdpa_set_status(struct vdpa_device *vdpa_dev, u8 status) 399 { 400 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 401 struct device *dev = &pdsv->vdpa_dev.dev; 402 u8 old_status; 403 int i; 404 405 old_status = pds_vdpa_get_status(vdpa_dev); 406 dev_dbg(dev, "%s: old %#x new %#x\n", __func__, old_status, status); 407 408 pds_vdpa_cmd_set_status(pdsv, status); 409 410 /* Note: still working with FW on the need for this reset cmd */ 411 if (status == 0) { 412 pds_vdpa_cmd_reset(pdsv); 413 414 for (i = 0; i < pdsv->num_vqs; i++) { 415 pdsv->vqs[i].avail_idx = 0; 416 pdsv->vqs[i].used_idx = 0; 417 } 418 419 pds_vdpa_cmd_set_mac(pdsv, pdsv->mac); 420 } 421 422 if (status & ~old_status & VIRTIO_CONFIG_S_FEATURES_OK) { 423 for (i = 0; i < pdsv->num_vqs; i++) { 424 pdsv->vqs[i].notify = 425 vp_modern_map_vq_notify(&pdsv->vdpa_aux->vd_mdev, 426 i, &pdsv->vqs[i].notify_pa); 427 } 428 } 429 } 430 431 static int pds_vdpa_reset(struct vdpa_device *vdpa_dev) 432 { 433 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 434 struct device *dev; 435 int err = 0; 436 u8 status; 437 int i; 438 439 dev = &pdsv->vdpa_aux->padev->aux_dev.dev; 440 status = pds_vdpa_get_status(vdpa_dev); 441 442 if (status == 0) 443 return 0; 444 445 if (status & VIRTIO_CONFIG_S_DRIVER_OK) { 446 /* Reset the vqs */ 447 for (i = 0; i < pdsv->num_vqs && !err; i++) { 448 err = pds_vdpa_cmd_reset_vq(pdsv, i, 0, &pdsv->vqs[i]); 449 if (err) 450 dev_err(dev, "%s: reset_vq failed qid %d: %pe\n", 451 __func__, i, ERR_PTR(err)); 452 pds_vdpa_release_irq(pdsv, i); 453 memset(&pdsv->vqs[i], 0, sizeof(pdsv->vqs[0])); 454 pdsv->vqs[i].ready = false; 455 } 456 } 457 458 pds_vdpa_set_status(vdpa_dev, 0); 459 460 return 0; 461 } 462 463 static size_t pds_vdpa_get_config_size(struct vdpa_device *vdpa_dev) 464 { 465 return sizeof(struct virtio_net_config); 466 } 467 468 static void pds_vdpa_get_config(struct vdpa_device *vdpa_dev, 469 unsigned int offset, 470 void *buf, unsigned int len) 471 { 472 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 473 void __iomem *device; 474 475 if (offset + len > sizeof(struct virtio_net_config)) { 476 WARN(true, "%s: bad read, offset %d len %d\n", __func__, offset, len); 477 return; 478 } 479 480 device = pdsv->vdpa_aux->vd_mdev.device; 481 memcpy_fromio(buf, device + offset, len); 482 } 483 484 static void pds_vdpa_set_config(struct vdpa_device *vdpa_dev, 485 unsigned int offset, const void *buf, 486 unsigned int len) 487 { 488 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 489 void __iomem *device; 490 491 if (offset + len > sizeof(struct virtio_net_config)) { 492 WARN(true, "%s: bad read, offset %d len %d\n", __func__, offset, len); 493 return; 494 } 495 496 device = pdsv->vdpa_aux->vd_mdev.device; 497 memcpy_toio(device + offset, buf, len); 498 } 499 500 static const struct vdpa_config_ops pds_vdpa_ops = { 501 .set_vq_address = pds_vdpa_set_vq_address, 502 .set_vq_num = pds_vdpa_set_vq_num, 503 .kick_vq = pds_vdpa_kick_vq, 504 .set_vq_cb = pds_vdpa_set_vq_cb, 505 .set_vq_ready = pds_vdpa_set_vq_ready, 506 .get_vq_ready = pds_vdpa_get_vq_ready, 507 .set_vq_state = pds_vdpa_set_vq_state, 508 .get_vq_state = pds_vdpa_get_vq_state, 509 .get_vq_notification = pds_vdpa_get_vq_notification, 510 .get_vq_irq = pds_vdpa_get_vq_irq, 511 .get_vq_align = pds_vdpa_get_vq_align, 512 .get_vq_group = pds_vdpa_get_vq_group, 513 514 .get_device_features = pds_vdpa_get_device_features, 515 .set_driver_features = pds_vdpa_set_driver_features, 516 .get_driver_features = pds_vdpa_get_driver_features, 517 .set_config_cb = pds_vdpa_set_config_cb, 518 .get_vq_num_max = pds_vdpa_get_vq_num_max, 519 .get_device_id = pds_vdpa_get_device_id, 520 .get_vendor_id = pds_vdpa_get_vendor_id, 521 .get_status = pds_vdpa_get_status, 522 .set_status = pds_vdpa_set_status, 523 .reset = pds_vdpa_reset, 524 .get_config_size = pds_vdpa_get_config_size, 525 .get_config = pds_vdpa_get_config, 526 .set_config = pds_vdpa_set_config, 527 }; 528 static struct virtio_device_id pds_vdpa_id_table[] = { 529 {VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID}, 530 {0}, 531 }; 532 533 static int pds_vdpa_dev_add(struct vdpa_mgmt_dev *mdev, const char *name, 534 const struct vdpa_dev_set_config *add_config) 535 { 536 struct pds_vdpa_aux *vdpa_aux; 537 struct pds_vdpa_device *pdsv; 538 struct vdpa_mgmt_dev *mgmt; 539 u16 fw_max_vqs, vq_pairs; 540 struct device *dma_dev; 541 struct pci_dev *pdev; 542 struct device *dev; 543 int err; 544 int i; 545 546 vdpa_aux = container_of(mdev, struct pds_vdpa_aux, vdpa_mdev); 547 dev = &vdpa_aux->padev->aux_dev.dev; 548 mgmt = &vdpa_aux->vdpa_mdev; 549 550 if (vdpa_aux->pdsv) { 551 dev_warn(dev, "Multiple vDPA devices on a VF is not supported.\n"); 552 return -EOPNOTSUPP; 553 } 554 555 pdsv = vdpa_alloc_device(struct pds_vdpa_device, vdpa_dev, 556 dev, &pds_vdpa_ops, 1, 1, name, false); 557 if (IS_ERR(pdsv)) { 558 dev_err(dev, "Failed to allocate vDPA structure: %pe\n", pdsv); 559 return PTR_ERR(pdsv); 560 } 561 562 vdpa_aux->pdsv = pdsv; 563 pdsv->vdpa_aux = vdpa_aux; 564 565 pdev = vdpa_aux->padev->vf_pdev; 566 dma_dev = &pdev->dev; 567 pdsv->vdpa_dev.dma_dev = dma_dev; 568 569 pdsv->supported_features = mgmt->supported_features; 570 571 if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) { 572 u64 unsupp_features = 573 add_config->device_features & ~pdsv->supported_features; 574 575 if (unsupp_features) { 576 dev_err(dev, "Unsupported features: %#llx\n", unsupp_features); 577 err = -EOPNOTSUPP; 578 goto err_unmap; 579 } 580 581 pdsv->supported_features = add_config->device_features; 582 } 583 584 err = pds_vdpa_cmd_reset(pdsv); 585 if (err) { 586 dev_err(dev, "Failed to reset hw: %pe\n", ERR_PTR(err)); 587 goto err_unmap; 588 } 589 590 err = pds_vdpa_init_hw(pdsv); 591 if (err) { 592 dev_err(dev, "Failed to init hw: %pe\n", ERR_PTR(err)); 593 goto err_unmap; 594 } 595 596 fw_max_vqs = le16_to_cpu(pdsv->vdpa_aux->ident.max_vqs); 597 vq_pairs = fw_max_vqs / 2; 598 599 /* Make sure we have the queues being requested */ 600 if (add_config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MAX_VQP)) 601 vq_pairs = add_config->net.max_vq_pairs; 602 603 pdsv->num_vqs = 2 * vq_pairs; 604 if (pdsv->supported_features & BIT_ULL(VIRTIO_NET_F_CTRL_VQ)) 605 pdsv->num_vqs++; 606 607 if (pdsv->num_vqs > fw_max_vqs) { 608 dev_err(dev, "%s: queue count requested %u greater than max %u\n", 609 __func__, pdsv->num_vqs, fw_max_vqs); 610 err = -ENOSPC; 611 goto err_unmap; 612 } 613 614 if (pdsv->num_vqs != fw_max_vqs) { 615 err = pds_vdpa_cmd_set_max_vq_pairs(pdsv, vq_pairs); 616 if (err) { 617 dev_err(dev, "Failed to set max_vq_pairs: %pe\n", 618 ERR_PTR(err)); 619 goto err_unmap; 620 } 621 } 622 623 /* Set a mac, either from the user config if provided 624 * or use the device's mac if not 00:..:00 625 * or set a random mac 626 */ 627 if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR)) { 628 ether_addr_copy(pdsv->mac, add_config->net.mac); 629 } else { 630 struct virtio_net_config __iomem *vc; 631 632 vc = pdsv->vdpa_aux->vd_mdev.device; 633 memcpy_fromio(pdsv->mac, vc->mac, sizeof(pdsv->mac)); 634 if (is_zero_ether_addr(pdsv->mac) && 635 (pdsv->supported_features & BIT_ULL(VIRTIO_NET_F_MAC))) { 636 eth_random_addr(pdsv->mac); 637 dev_info(dev, "setting random mac %pM\n", pdsv->mac); 638 } 639 } 640 pds_vdpa_cmd_set_mac(pdsv, pdsv->mac); 641 642 for (i = 0; i < pdsv->num_vqs; i++) { 643 pdsv->vqs[i].qid = i; 644 pdsv->vqs[i].pdsv = pdsv; 645 pdsv->vqs[i].irq = VIRTIO_MSI_NO_VECTOR; 646 pdsv->vqs[i].notify = vp_modern_map_vq_notify(&pdsv->vdpa_aux->vd_mdev, 647 i, &pdsv->vqs[i].notify_pa); 648 } 649 650 pdsv->vdpa_dev.mdev = &vdpa_aux->vdpa_mdev; 651 652 err = pds_vdpa_register_event_handler(pdsv); 653 if (err) { 654 dev_err(dev, "Failed to register for PDS events: %pe\n", ERR_PTR(err)); 655 goto err_unmap; 656 } 657 658 /* We use the _vdpa_register_device() call rather than the 659 * vdpa_register_device() to avoid a deadlock because our 660 * dev_add() is called with the vdpa_dev_lock already set 661 * by vdpa_nl_cmd_dev_add_set_doit() 662 */ 663 err = _vdpa_register_device(&pdsv->vdpa_dev, pdsv->num_vqs); 664 if (err) { 665 dev_err(dev, "Failed to register to vDPA bus: %pe\n", ERR_PTR(err)); 666 goto err_unevent; 667 } 668 669 pds_vdpa_debugfs_add_vdpadev(vdpa_aux); 670 671 return 0; 672 673 err_unevent: 674 pds_vdpa_unregister_event_handler(pdsv); 675 err_unmap: 676 put_device(&pdsv->vdpa_dev.dev); 677 vdpa_aux->pdsv = NULL; 678 return err; 679 } 680 681 static void pds_vdpa_dev_del(struct vdpa_mgmt_dev *mdev, 682 struct vdpa_device *vdpa_dev) 683 { 684 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev); 685 struct pds_vdpa_aux *vdpa_aux; 686 687 pds_vdpa_unregister_event_handler(pdsv); 688 689 vdpa_aux = container_of(mdev, struct pds_vdpa_aux, vdpa_mdev); 690 _vdpa_unregister_device(vdpa_dev); 691 692 pds_vdpa_cmd_reset(vdpa_aux->pdsv); 693 pds_vdpa_debugfs_reset_vdpadev(vdpa_aux); 694 695 vdpa_aux->pdsv = NULL; 696 697 dev_info(&vdpa_aux->padev->aux_dev.dev, "Removed vdpa device\n"); 698 } 699 700 static const struct vdpa_mgmtdev_ops pds_vdpa_mgmt_dev_ops = { 701 .dev_add = pds_vdpa_dev_add, 702 .dev_del = pds_vdpa_dev_del 703 }; 704 705 int pds_vdpa_get_mgmt_info(struct pds_vdpa_aux *vdpa_aux) 706 { 707 union pds_core_adminq_cmd cmd = { 708 .vdpa_ident.opcode = PDS_VDPA_CMD_IDENT, 709 .vdpa_ident.vf_id = cpu_to_le16(vdpa_aux->vf_id), 710 }; 711 union pds_core_adminq_comp comp = {}; 712 struct vdpa_mgmt_dev *mgmt; 713 struct pci_dev *pf_pdev; 714 struct device *pf_dev; 715 struct pci_dev *pdev; 716 dma_addr_t ident_pa; 717 struct device *dev; 718 u16 dev_intrs; 719 u16 max_vqs; 720 int err; 721 722 dev = &vdpa_aux->padev->aux_dev.dev; 723 pdev = vdpa_aux->padev->vf_pdev; 724 mgmt = &vdpa_aux->vdpa_mdev; 725 726 /* Get resource info through the PF's adminq. It is a block of info, 727 * so we need to map some memory for PF to make available to the 728 * firmware for writing the data. 729 */ 730 pf_pdev = pci_physfn(vdpa_aux->padev->vf_pdev); 731 pf_dev = &pf_pdev->dev; 732 ident_pa = dma_map_single(pf_dev, &vdpa_aux->ident, 733 sizeof(vdpa_aux->ident), DMA_FROM_DEVICE); 734 if (dma_mapping_error(pf_dev, ident_pa)) { 735 dev_err(dev, "Failed to map ident space\n"); 736 return -ENOMEM; 737 } 738 739 cmd.vdpa_ident.ident_pa = cpu_to_le64(ident_pa); 740 cmd.vdpa_ident.len = cpu_to_le32(sizeof(vdpa_aux->ident)); 741 err = pds_client_adminq_cmd(vdpa_aux->padev, &cmd, 742 sizeof(cmd.vdpa_ident), &comp, 0); 743 dma_unmap_single(pf_dev, ident_pa, 744 sizeof(vdpa_aux->ident), DMA_FROM_DEVICE); 745 if (err) { 746 dev_err(dev, "Failed to ident hw, status %d: %pe\n", 747 comp.status, ERR_PTR(err)); 748 return err; 749 } 750 751 max_vqs = le16_to_cpu(vdpa_aux->ident.max_vqs); 752 dev_intrs = pci_msix_vec_count(pdev); 753 dev_dbg(dev, "ident.max_vqs %d dev_intrs %d\n", max_vqs, dev_intrs); 754 755 max_vqs = min_t(u16, dev_intrs, max_vqs); 756 mgmt->max_supported_vqs = min_t(u16, PDS_VDPA_MAX_QUEUES, max_vqs); 757 vdpa_aux->nintrs = mgmt->max_supported_vqs; 758 759 mgmt->ops = &pds_vdpa_mgmt_dev_ops; 760 mgmt->id_table = pds_vdpa_id_table; 761 mgmt->device = dev; 762 mgmt->supported_features = le64_to_cpu(vdpa_aux->ident.hw_features); 763 764 /* advertise F_MAC even if the device doesn't */ 765 mgmt->supported_features |= BIT_ULL(VIRTIO_NET_F_MAC); 766 767 mgmt->config_attr_mask = BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR); 768 mgmt->config_attr_mask |= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP); 769 mgmt->config_attr_mask |= BIT_ULL(VDPA_ATTR_DEV_FEATURES); 770 771 err = pci_alloc_irq_vectors(pdev, vdpa_aux->nintrs, vdpa_aux->nintrs, 772 PCI_IRQ_MSIX); 773 if (err < 0) { 774 dev_err(dev, "Couldn't get %d msix vectors: %pe\n", 775 vdpa_aux->nintrs, ERR_PTR(err)); 776 return err; 777 } 778 vdpa_aux->nintrs = err; 779 780 return 0; 781 } 782