1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2023 Advanced Micro Devices, Inc */
5 #include <linux/vdpa.h>
6 #include <uapi/linux/vdpa.h>
7 #include <linux/virtio_pci_modern.h>
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>
19 static u64 pds_vdpa_get_driver_features(struct vdpa_device *vdpa_dev);
21 static struct pds_vdpa_device *vdpa_to_pdsv(struct vdpa_device *vdpa_dev)
23 return container_of(vdpa_dev, struct pds_vdpa_device, vdpa_dev);
26 static int pds_vdpa_notify_handler(struct notifier_block *nb,
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;
33 dev_dbg(dev, "%s: event code %lu\n", __func__, ecode);
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);
43 static int pds_vdpa_register_event_handler(struct pds_vdpa_device *pdsv)
45 struct device *dev = &pdsv->vdpa_aux->padev->aux_dev.dev;
46 struct notifier_block *nb = &pdsv->nb;
49 if (!nb->notifier_call) {
50 nb->notifier_call = pds_vdpa_notify_handler;
51 err = pdsc_register_notify(nb);
53 nb->notifier_call = NULL;
54 dev_err(dev, "failed to register pds event handler: %ps\n",
58 dev_dbg(dev, "pds event handler registered\n");
64 static void pds_vdpa_unregister_event_handler(struct pds_vdpa_device *pdsv)
66 if (pdsv->nb.notifier_call) {
67 pdsc_unregister_notify(&pdsv->nb);
68 pdsv->nb.notifier_call = NULL;
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)
75 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
77 pdsv->vqs[qid].desc_addr = desc_addr;
78 pdsv->vqs[qid].avail_addr = driver_addr;
79 pdsv->vqs[qid].used_addr = device_addr;
84 static void pds_vdpa_set_vq_num(struct vdpa_device *vdpa_dev, u16 qid, u32 num)
86 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
88 pdsv->vqs[qid].q_len = num;
91 static void pds_vdpa_kick_vq(struct vdpa_device *vdpa_dev, u16 qid)
93 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
95 iowrite16(qid, pdsv->vqs[qid].notify);
98 static void pds_vdpa_set_vq_cb(struct vdpa_device *vdpa_dev, u16 qid,
99 struct vdpa_callback *cb)
101 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
103 pdsv->vqs[qid].event_cb = *cb;
106 static irqreturn_t pds_vdpa_isr(int irq, void *data)
108 struct pds_vdpa_vq_info *vq;
111 if (vq->event_cb.callback)
112 vq->event_cb.callback(vq->event_cb.private);
117 static void pds_vdpa_release_irq(struct pds_vdpa_device *pdsv, int qid)
119 if (pdsv->vqs[qid].irq == VIRTIO_MSI_NO_VECTOR)
122 free_irq(pdsv->vqs[qid].irq, &pdsv->vqs[qid]);
123 pdsv->vqs[qid].irq = VIRTIO_MSI_NO_VECTOR;
126 static void pds_vdpa_set_vq_ready(struct vdpa_device *vdpa_dev, u16 qid, bool ready)
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;
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)
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;
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);
150 err = request_irq(irq, pds_vdpa_isr, 0,
151 pdsv->vqs[qid].irq_name, &pdsv->vqs[qid]);
153 dev_err(dev, "%s: no irq for qid %d: %pe\n",
154 __func__, qid, ERR_PTR(err));
157 pdsv->vqs[qid].irq = irq;
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
163 err = pds_vdpa_cmd_init_vq(pdsv, qid, invert_idx, &pdsv->vqs[qid]);
165 dev_err(dev, "Failed to init vq %d: %pe\n",
167 pds_vdpa_release_irq(pdsv, qid);
171 err = pds_vdpa_cmd_reset_vq(pdsv, qid, invert_idx, &pdsv->vqs[qid]);
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);
178 pdsv->vqs[qid].ready = ready;
181 static bool pds_vdpa_get_vq_ready(struct vdpa_device *vdpa_dev, u16 qid)
183 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
185 return pdsv->vqs[qid].ready;
188 static int pds_vdpa_set_vq_state(struct vdpa_device *vdpa_dev, u16 qid,
189 const struct vdpa_vq_state *state)
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;
198 if (pdsv->vqs[qid].ready) {
199 dev_err(dev, "Setting device position is denied while vq is enabled\n");
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);
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
216 avail ^= PDS_VDPA_PACKED_INVERT_IDX;
217 used ^= PDS_VDPA_PACKED_INVERT_IDX;
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.
228 dev_dbg(dev, "Setting used equal to avail, for interoperability\n");
232 pdsv->vqs[qid].avail_idx = avail;
233 pdsv->vqs[qid].used_idx = used;
238 static int pds_vdpa_get_vq_state(struct vdpa_device *vdpa_dev, u16 qid,
239 struct vdpa_vq_state *state)
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;
248 if (pdsv->vqs[qid].ready) {
249 dev_err(dev, "Getting device position is denied while vq is enabled\n");
253 avail = pdsv->vqs[qid].avail_idx;
254 used = pdsv->vqs[qid].used_idx;
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;
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;
266 state->split.avail_index = avail;
267 /* state->split does not provide a used_index. */
273 static struct vdpa_notification_area
274 pds_vdpa_get_vq_notification(struct vdpa_device *vdpa_dev, u16 qid)
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;
280 area.addr = pdsv->vqs[qid].notify_pa;
282 vd_mdev = &pdsv->vdpa_aux->vd_mdev;
283 if (!vd_mdev->notify_offset_multiplier)
284 area.size = PDS_PAGE_SIZE;
286 area.size = vd_mdev->notify_offset_multiplier;
291 static int pds_vdpa_get_vq_irq(struct vdpa_device *vdpa_dev, u16 qid)
293 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
295 return pdsv->vqs[qid].irq;
298 static u32 pds_vdpa_get_vq_align(struct vdpa_device *vdpa_dev)
300 return PDS_PAGE_SIZE;
303 static u32 pds_vdpa_get_vq_group(struct vdpa_device *vdpa_dev, u16 idx)
308 static u64 pds_vdpa_get_device_features(struct vdpa_device *vdpa_dev)
310 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
312 return pdsv->supported_features;
315 static int pds_vdpa_set_driver_features(struct vdpa_device *vdpa_dev, u64 features)
317 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
318 struct device *dev = &pdsv->vdpa_dev.dev;
323 if (!(features & BIT_ULL(VIRTIO_F_ACCESS_PLATFORM)) && features) {
324 dev_err(dev, "VIRTIO_F_ACCESS_PLATFORM is not negotiated\n");
328 pdsv->req_features = features;
330 /* Check for valid feature bits */
331 nego_features = features & le64_to_cpu(pdsv->vdpa_aux->ident.hw_features);
332 missing = pdsv->req_features & ~nego_features;
334 dev_err(dev, "Can't support all requested features in %#llx, missing %#llx features\n",
335 pdsv->req_features, missing);
339 driver_features = pds_vdpa_get_driver_features(vdpa_dev);
340 dev_dbg(dev, "%s: %#llx => %#llx\n",
341 __func__, driver_features, nego_features);
343 if (driver_features == nego_features)
346 vp_modern_set_features(&pdsv->vdpa_aux->vd_mdev, nego_features);
351 static u64 pds_vdpa_get_driver_features(struct vdpa_device *vdpa_dev)
353 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
355 return vp_modern_get_driver_features(&pdsv->vdpa_aux->vd_mdev);
358 static void pds_vdpa_set_config_cb(struct vdpa_device *vdpa_dev,
359 struct vdpa_callback *cb)
361 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
363 pdsv->config_cb.callback = cb->callback;
364 pdsv->config_cb.private = cb->private;
367 static u16 pds_vdpa_get_vq_num_max(struct vdpa_device *vdpa_dev)
369 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
371 /* qemu has assert() that vq_num_max <= VIRTQUEUE_MAX_SIZE (1024) */
372 return min_t(u16, 1024, BIT(le16_to_cpu(pdsv->vdpa_aux->ident.max_qlen)));
375 static u32 pds_vdpa_get_device_id(struct vdpa_device *vdpa_dev)
377 return VIRTIO_ID_NET;
380 static u32 pds_vdpa_get_vendor_id(struct vdpa_device *vdpa_dev)
382 return PCI_VENDOR_ID_PENSANDO;
385 static u8 pds_vdpa_get_status(struct vdpa_device *vdpa_dev)
387 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
389 return vp_modern_get_status(&pdsv->vdpa_aux->vd_mdev);
392 static void pds_vdpa_set_status(struct vdpa_device *vdpa_dev, u8 status)
394 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
395 struct device *dev = &pdsv->vdpa_dev.dev;
399 old_status = pds_vdpa_get_status(vdpa_dev);
400 dev_dbg(dev, "%s: old %#x new %#x\n", __func__, old_status, status);
402 pds_vdpa_cmd_set_status(pdsv, status);
404 /* Note: still working with FW on the need for this reset cmd */
406 pds_vdpa_cmd_reset(pdsv);
408 for (i = 0; i < pdsv->num_vqs; i++) {
409 pdsv->vqs[i].avail_idx = 0;
410 pdsv->vqs[i].used_idx = 0;
414 if (status & ~old_status & VIRTIO_CONFIG_S_FEATURES_OK) {
415 for (i = 0; i < pdsv->num_vqs; i++) {
416 pdsv->vqs[i].notify =
417 vp_modern_map_vq_notify(&pdsv->vdpa_aux->vd_mdev,
418 i, &pdsv->vqs[i].notify_pa);
423 static int pds_vdpa_reset(struct vdpa_device *vdpa_dev)
425 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
431 dev = &pdsv->vdpa_aux->padev->aux_dev.dev;
432 status = pds_vdpa_get_status(vdpa_dev);
437 if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
439 for (i = 0; i < pdsv->num_vqs && !err; i++) {
440 err = pds_vdpa_cmd_reset_vq(pdsv, i, 0, &pdsv->vqs[i]);
442 dev_err(dev, "%s: reset_vq failed qid %d: %pe\n",
443 __func__, i, ERR_PTR(err));
444 pds_vdpa_release_irq(pdsv, i);
445 memset(&pdsv->vqs[i], 0, sizeof(pdsv->vqs[0]));
446 pdsv->vqs[i].ready = false;
450 pds_vdpa_set_status(vdpa_dev, 0);
455 static size_t pds_vdpa_get_config_size(struct vdpa_device *vdpa_dev)
457 return sizeof(struct virtio_net_config);
460 static void pds_vdpa_get_config(struct vdpa_device *vdpa_dev,
462 void *buf, unsigned int len)
464 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
465 void __iomem *device;
467 if (offset + len > sizeof(struct virtio_net_config)) {
468 WARN(true, "%s: bad read, offset %d len %d\n", __func__, offset, len);
472 device = pdsv->vdpa_aux->vd_mdev.device;
473 memcpy_fromio(buf, device + offset, len);
476 static void pds_vdpa_set_config(struct vdpa_device *vdpa_dev,
477 unsigned int offset, const void *buf,
480 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
481 void __iomem *device;
483 if (offset + len > sizeof(struct virtio_net_config)) {
484 WARN(true, "%s: bad read, offset %d len %d\n", __func__, offset, len);
488 device = pdsv->vdpa_aux->vd_mdev.device;
489 memcpy_toio(device + offset, buf, len);
492 static const struct vdpa_config_ops pds_vdpa_ops = {
493 .set_vq_address = pds_vdpa_set_vq_address,
494 .set_vq_num = pds_vdpa_set_vq_num,
495 .kick_vq = pds_vdpa_kick_vq,
496 .set_vq_cb = pds_vdpa_set_vq_cb,
497 .set_vq_ready = pds_vdpa_set_vq_ready,
498 .get_vq_ready = pds_vdpa_get_vq_ready,
499 .set_vq_state = pds_vdpa_set_vq_state,
500 .get_vq_state = pds_vdpa_get_vq_state,
501 .get_vq_notification = pds_vdpa_get_vq_notification,
502 .get_vq_irq = pds_vdpa_get_vq_irq,
503 .get_vq_align = pds_vdpa_get_vq_align,
504 .get_vq_group = pds_vdpa_get_vq_group,
506 .get_device_features = pds_vdpa_get_device_features,
507 .set_driver_features = pds_vdpa_set_driver_features,
508 .get_driver_features = pds_vdpa_get_driver_features,
509 .set_config_cb = pds_vdpa_set_config_cb,
510 .get_vq_num_max = pds_vdpa_get_vq_num_max,
511 .get_device_id = pds_vdpa_get_device_id,
512 .get_vendor_id = pds_vdpa_get_vendor_id,
513 .get_status = pds_vdpa_get_status,
514 .set_status = pds_vdpa_set_status,
515 .reset = pds_vdpa_reset,
516 .get_config_size = pds_vdpa_get_config_size,
517 .get_config = pds_vdpa_get_config,
518 .set_config = pds_vdpa_set_config,
520 static struct virtio_device_id pds_vdpa_id_table[] = {
521 {VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID},
525 static int pds_vdpa_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
526 const struct vdpa_dev_set_config *add_config)
528 struct pds_vdpa_aux *vdpa_aux;
529 struct pds_vdpa_device *pdsv;
530 struct vdpa_mgmt_dev *mgmt;
531 u16 fw_max_vqs, vq_pairs;
532 struct device *dma_dev;
533 struct pci_dev *pdev;
539 vdpa_aux = container_of(mdev, struct pds_vdpa_aux, vdpa_mdev);
540 dev = &vdpa_aux->padev->aux_dev.dev;
541 mgmt = &vdpa_aux->vdpa_mdev;
543 if (vdpa_aux->pdsv) {
544 dev_warn(dev, "Multiple vDPA devices on a VF is not supported.\n");
548 pdsv = vdpa_alloc_device(struct pds_vdpa_device, vdpa_dev,
549 dev, &pds_vdpa_ops, 1, 1, name, false);
551 dev_err(dev, "Failed to allocate vDPA structure: %pe\n", pdsv);
552 return PTR_ERR(pdsv);
555 vdpa_aux->pdsv = pdsv;
556 pdsv->vdpa_aux = vdpa_aux;
558 pdev = vdpa_aux->padev->vf_pdev;
559 dma_dev = &pdev->dev;
560 pdsv->vdpa_dev.dma_dev = dma_dev;
562 pdsv->supported_features = mgmt->supported_features;
564 if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) {
565 u64 unsupp_features =
566 add_config->device_features & ~mgmt->supported_features;
568 if (unsupp_features) {
569 dev_err(dev, "Unsupported features: %#llx\n", unsupp_features);
574 pdsv->supported_features = add_config->device_features;
577 err = pds_vdpa_cmd_reset(pdsv);
579 dev_err(dev, "Failed to reset hw: %pe\n", ERR_PTR(err));
583 err = pds_vdpa_init_hw(pdsv);
585 dev_err(dev, "Failed to init hw: %pe\n", ERR_PTR(err));
589 fw_max_vqs = le16_to_cpu(pdsv->vdpa_aux->ident.max_vqs);
590 vq_pairs = fw_max_vqs / 2;
592 /* Make sure we have the queues being requested */
593 if (add_config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MAX_VQP))
594 vq_pairs = add_config->net.max_vq_pairs;
596 pdsv->num_vqs = 2 * vq_pairs;
597 if (pdsv->supported_features & BIT_ULL(VIRTIO_NET_F_CTRL_VQ))
600 if (pdsv->num_vqs > fw_max_vqs) {
601 dev_err(dev, "%s: queue count requested %u greater than max %u\n",
602 __func__, pdsv->num_vqs, fw_max_vqs);
607 if (pdsv->num_vqs != fw_max_vqs) {
608 err = pds_vdpa_cmd_set_max_vq_pairs(pdsv, vq_pairs);
610 dev_err(dev, "Failed to set max_vq_pairs: %pe\n",
616 /* Set a mac, either from the user config if provided
617 * or set a random mac if default is 00:..:00
619 if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR)) {
620 ether_addr_copy(mac, add_config->net.mac);
621 pds_vdpa_cmd_set_mac(pdsv, mac);
623 struct virtio_net_config __iomem *vc;
625 vc = pdsv->vdpa_aux->vd_mdev.device;
626 memcpy_fromio(mac, vc->mac, sizeof(mac));
627 if (is_zero_ether_addr(mac)) {
628 eth_random_addr(mac);
629 dev_info(dev, "setting random mac %pM\n", mac);
630 pds_vdpa_cmd_set_mac(pdsv, mac);
634 for (i = 0; i < pdsv->num_vqs; i++) {
635 pdsv->vqs[i].qid = i;
636 pdsv->vqs[i].pdsv = pdsv;
637 pdsv->vqs[i].irq = VIRTIO_MSI_NO_VECTOR;
638 pdsv->vqs[i].notify = vp_modern_map_vq_notify(&pdsv->vdpa_aux->vd_mdev,
639 i, &pdsv->vqs[i].notify_pa);
642 pdsv->vdpa_dev.mdev = &vdpa_aux->vdpa_mdev;
644 err = pds_vdpa_register_event_handler(pdsv);
646 dev_err(dev, "Failed to register for PDS events: %pe\n", ERR_PTR(err));
650 /* We use the _vdpa_register_device() call rather than the
651 * vdpa_register_device() to avoid a deadlock because our
652 * dev_add() is called with the vdpa_dev_lock already set
653 * by vdpa_nl_cmd_dev_add_set_doit()
655 err = _vdpa_register_device(&pdsv->vdpa_dev, pdsv->num_vqs);
657 dev_err(dev, "Failed to register to vDPA bus: %pe\n", ERR_PTR(err));
661 pds_vdpa_debugfs_add_vdpadev(vdpa_aux);
666 pds_vdpa_unregister_event_handler(pdsv);
668 put_device(&pdsv->vdpa_dev.dev);
669 vdpa_aux->pdsv = NULL;
673 static void pds_vdpa_dev_del(struct vdpa_mgmt_dev *mdev,
674 struct vdpa_device *vdpa_dev)
676 struct pds_vdpa_device *pdsv = vdpa_to_pdsv(vdpa_dev);
677 struct pds_vdpa_aux *vdpa_aux;
679 pds_vdpa_unregister_event_handler(pdsv);
681 vdpa_aux = container_of(mdev, struct pds_vdpa_aux, vdpa_mdev);
682 _vdpa_unregister_device(vdpa_dev);
684 pds_vdpa_cmd_reset(vdpa_aux->pdsv);
685 pds_vdpa_debugfs_reset_vdpadev(vdpa_aux);
687 vdpa_aux->pdsv = NULL;
689 dev_info(&vdpa_aux->padev->aux_dev.dev, "Removed vdpa device\n");
692 static const struct vdpa_mgmtdev_ops pds_vdpa_mgmt_dev_ops = {
693 .dev_add = pds_vdpa_dev_add,
694 .dev_del = pds_vdpa_dev_del
697 int pds_vdpa_get_mgmt_info(struct pds_vdpa_aux *vdpa_aux)
699 union pds_core_adminq_cmd cmd = {
700 .vdpa_ident.opcode = PDS_VDPA_CMD_IDENT,
701 .vdpa_ident.vf_id = cpu_to_le16(vdpa_aux->vf_id),
703 union pds_core_adminq_comp comp = {};
704 struct vdpa_mgmt_dev *mgmt;
705 struct pci_dev *pf_pdev;
706 struct device *pf_dev;
707 struct pci_dev *pdev;
714 dev = &vdpa_aux->padev->aux_dev.dev;
715 pdev = vdpa_aux->padev->vf_pdev;
716 mgmt = &vdpa_aux->vdpa_mdev;
718 /* Get resource info through the PF's adminq. It is a block of info,
719 * so we need to map some memory for PF to make available to the
720 * firmware for writing the data.
722 pf_pdev = pci_physfn(vdpa_aux->padev->vf_pdev);
723 pf_dev = &pf_pdev->dev;
724 ident_pa = dma_map_single(pf_dev, &vdpa_aux->ident,
725 sizeof(vdpa_aux->ident), DMA_FROM_DEVICE);
726 if (dma_mapping_error(pf_dev, ident_pa)) {
727 dev_err(dev, "Failed to map ident space\n");
731 cmd.vdpa_ident.ident_pa = cpu_to_le64(ident_pa);
732 cmd.vdpa_ident.len = cpu_to_le32(sizeof(vdpa_aux->ident));
733 err = pds_client_adminq_cmd(vdpa_aux->padev, &cmd,
734 sizeof(cmd.vdpa_ident), &comp, 0);
735 dma_unmap_single(pf_dev, ident_pa,
736 sizeof(vdpa_aux->ident), DMA_FROM_DEVICE);
738 dev_err(dev, "Failed to ident hw, status %d: %pe\n",
739 comp.status, ERR_PTR(err));
743 max_vqs = le16_to_cpu(vdpa_aux->ident.max_vqs);
744 dev_intrs = pci_msix_vec_count(pdev);
745 dev_dbg(dev, "ident.max_vqs %d dev_intrs %d\n", max_vqs, dev_intrs);
747 max_vqs = min_t(u16, dev_intrs, max_vqs);
748 mgmt->max_supported_vqs = min_t(u16, PDS_VDPA_MAX_QUEUES, max_vqs);
749 vdpa_aux->nintrs = mgmt->max_supported_vqs;
751 mgmt->ops = &pds_vdpa_mgmt_dev_ops;
752 mgmt->id_table = pds_vdpa_id_table;
754 mgmt->supported_features = le64_to_cpu(vdpa_aux->ident.hw_features);
755 mgmt->config_attr_mask = BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR);
756 mgmt->config_attr_mask |= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP);
757 mgmt->config_attr_mask |= BIT_ULL(VDPA_ATTR_DEV_FEATURES);
759 err = pci_alloc_irq_vectors(pdev, vdpa_aux->nintrs, vdpa_aux->nintrs,
762 dev_err(dev, "Couldn't get %d msix vectors: %pe\n",
763 vdpa_aux->nintrs, ERR_PTR(err));
766 vdpa_aux->nintrs = err;