1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2024 Marvell. */
4 #include <linux/interrupt.h>
5 #include <linux/io-64-nonatomic-lo-hi.h>
6 #include <linux/module.h>
7 #include <linux/iommu.h>
8 #include "octep_vdpa.h"
10 #define OCTEP_VDPA_DRIVER_NAME "octep_vdpa"
13 u8 __iomem *base[PCI_STD_NUM_BARS];
23 struct vdpa_device vdpa;
24 struct octep_hw *oct_hw;
28 struct octep_vdpa_mgmt_dev {
29 struct vdpa_mgmt_dev mdev;
30 struct octep_hw oct_hw;
32 /* Work entry to handle device setup */
33 struct work_struct setup_task;
38 static struct octep_hw *vdpa_to_octep_hw(struct vdpa_device *vdpa_dev)
40 struct octep_vdpa *oct_vdpa;
42 oct_vdpa = container_of(vdpa_dev, struct octep_vdpa, vdpa);
44 return oct_vdpa->oct_hw;
47 static irqreturn_t octep_vdpa_intr_handler(int irq, void *data)
49 struct octep_hw *oct_hw = data;
52 /* Each device has multiple interrupts (nb_irqs) shared among rings
53 * (nr_vring). Device interrupts are mapped to the rings in a
54 * round-robin fashion.
56 * For example, if nb_irqs = 8 and nr_vring = 64:
57 * 0 -> 0, 8, 16, 24, 32, 40, 48, 56;
58 * 1 -> 1, 9, 17, 25, 33, 41, 49, 57;
60 * 7 -> 7, 15, 23, 31, 39, 47, 55, 63;
63 for (i = irq - oct_hw->irqs[0]; i < oct_hw->nr_vring; i += oct_hw->nb_irqs) {
64 if (ioread8(oct_hw->vqs[i].cb_notify_addr)) {
65 /* Acknowledge the per ring notification to the device */
66 iowrite8(0, oct_hw->vqs[i].cb_notify_addr);
68 if (likely(oct_hw->vqs[i].cb.callback))
69 oct_hw->vqs[i].cb.callback(oct_hw->vqs[i].cb.private);
74 /* Check for config interrupt. Config uses the first interrupt */
75 if (unlikely(irq == oct_hw->irqs[0] && ioread8(oct_hw->isr))) {
76 iowrite8(0, oct_hw->isr);
78 if (oct_hw->config_cb.callback)
79 oct_hw->config_cb.callback(oct_hw->config_cb.private);
85 static void octep_free_irqs(struct octep_hw *oct_hw)
87 struct pci_dev *pdev = oct_hw->pdev;
93 for (irq = 0; irq < oct_hw->nb_irqs; irq++) {
94 if (!oct_hw->irqs[irq])
97 devm_free_irq(&pdev->dev, oct_hw->irqs[irq], oct_hw);
100 pci_free_irq_vectors(pdev);
101 devm_kfree(&pdev->dev, oct_hw->irqs);
105 static int octep_request_irqs(struct octep_hw *oct_hw)
107 struct pci_dev *pdev = oct_hw->pdev;
110 oct_hw->irqs = devm_kcalloc(&pdev->dev, oct_hw->nb_irqs, sizeof(int), GFP_KERNEL);
114 ret = pci_alloc_irq_vectors(pdev, 1, oct_hw->nb_irqs, PCI_IRQ_MSIX);
116 dev_err(&pdev->dev, "Failed to alloc msix vector");
120 for (idx = 0; idx < oct_hw->nb_irqs; idx++) {
121 irq = pci_irq_vector(pdev, idx);
122 ret = devm_request_irq(&pdev->dev, irq, octep_vdpa_intr_handler, 0,
123 dev_name(&pdev->dev), oct_hw);
125 dev_err(&pdev->dev, "Failed to register interrupt handler\n");
128 oct_hw->irqs[idx] = irq;
134 octep_free_irqs(oct_hw);
138 static u64 octep_vdpa_get_device_features(struct vdpa_device *vdpa_dev)
140 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
142 return oct_hw->features;
145 static int octep_vdpa_set_driver_features(struct vdpa_device *vdpa_dev, u64 features)
147 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
150 pr_debug("Driver Features: %llx\n", features);
152 ret = octep_verify_features(features);
154 dev_warn(&oct_hw->pdev->dev,
155 "Must negotiate minimum features 0x%llx for this device",
156 BIT_ULL(VIRTIO_F_VERSION_1) | BIT_ULL(VIRTIO_F_NOTIFICATION_DATA) |
157 BIT_ULL(VIRTIO_F_RING_PACKED));
160 octep_hw_set_drv_features(oct_hw, features);
165 static u64 octep_vdpa_get_driver_features(struct vdpa_device *vdpa_dev)
167 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
169 return octep_hw_get_drv_features(oct_hw);
172 static u8 octep_vdpa_get_status(struct vdpa_device *vdpa_dev)
174 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
176 return octep_hw_get_status(oct_hw);
179 static void octep_vdpa_set_status(struct vdpa_device *vdpa_dev, u8 status)
181 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
184 status_old = octep_hw_get_status(oct_hw);
186 if (status_old == status)
189 if ((status & VIRTIO_CONFIG_S_DRIVER_OK) &&
190 !(status_old & VIRTIO_CONFIG_S_DRIVER_OK)) {
191 if (octep_request_irqs(oct_hw))
192 status = status_old | VIRTIO_CONFIG_S_FAILED;
194 octep_hw_set_status(oct_hw, status);
197 static int octep_vdpa_reset(struct vdpa_device *vdpa_dev)
199 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
200 u8 status = octep_hw_get_status(oct_hw);
206 for (qid = 0; qid < oct_hw->nr_vring; qid++) {
207 oct_hw->vqs[qid].cb.callback = NULL;
208 oct_hw->vqs[qid].cb.private = NULL;
209 oct_hw->config_cb.callback = NULL;
210 oct_hw->config_cb.private = NULL;
212 octep_hw_reset(oct_hw);
214 if (status & VIRTIO_CONFIG_S_DRIVER_OK)
215 octep_free_irqs(oct_hw);
220 static u16 octep_vdpa_get_vq_num_max(struct vdpa_device *vdpa_dev)
222 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
224 return octep_get_vq_size(oct_hw);
227 static int octep_vdpa_get_vq_state(struct vdpa_device *vdpa_dev, u16 qid,
228 struct vdpa_vq_state *state)
230 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
232 return octep_get_vq_state(oct_hw, qid, state);
235 static int octep_vdpa_set_vq_state(struct vdpa_device *vdpa_dev, u16 qid,
236 const struct vdpa_vq_state *state)
238 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
240 return octep_set_vq_state(oct_hw, qid, state);
243 static void octep_vdpa_set_vq_cb(struct vdpa_device *vdpa_dev, u16 qid, struct vdpa_callback *cb)
245 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
247 oct_hw->vqs[qid].cb = *cb;
250 static void octep_vdpa_set_vq_ready(struct vdpa_device *vdpa_dev, u16 qid, bool ready)
252 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
254 octep_set_vq_ready(oct_hw, qid, ready);
257 static bool octep_vdpa_get_vq_ready(struct vdpa_device *vdpa_dev, u16 qid)
259 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
261 return octep_get_vq_ready(oct_hw, qid);
264 static void octep_vdpa_set_vq_num(struct vdpa_device *vdpa_dev, u16 qid, u32 num)
266 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
268 octep_set_vq_num(oct_hw, qid, num);
271 static int octep_vdpa_set_vq_address(struct vdpa_device *vdpa_dev, u16 qid, u64 desc_area,
272 u64 driver_area, u64 device_area)
274 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
276 pr_debug("qid[%d]: desc_area: %llx\n", qid, desc_area);
277 pr_debug("qid[%d]: driver_area: %llx\n", qid, driver_area);
278 pr_debug("qid[%d]: device_area: %llx\n\n", qid, device_area);
280 return octep_set_vq_address(oct_hw, qid, desc_area, driver_area, device_area);
283 static void octep_vdpa_kick_vq(struct vdpa_device *vdpa_dev, u16 qid)
288 static void octep_vdpa_kick_vq_with_data(struct vdpa_device *vdpa_dev, u32 data)
290 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
291 u16 idx = data & 0xFFFF;
293 vp_iowrite32(data, oct_hw->vqs[idx].notify_addr);
296 static u32 octep_vdpa_get_generation(struct vdpa_device *vdpa_dev)
298 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
300 return vp_ioread8(&oct_hw->common_cfg->config_generation);
303 static u32 octep_vdpa_get_device_id(struct vdpa_device *vdpa_dev)
305 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
307 return oct_hw->dev_id;
310 static u32 octep_vdpa_get_vendor_id(struct vdpa_device *vdpa_dev)
312 return PCI_VENDOR_ID_CAVIUM;
315 static u32 octep_vdpa_get_vq_align(struct vdpa_device *vdpa_dev)
320 static size_t octep_vdpa_get_config_size(struct vdpa_device *vdpa_dev)
322 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
324 return oct_hw->config_size;
327 static void octep_vdpa_get_config(struct vdpa_device *vdpa_dev, unsigned int offset, void *buf,
330 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
332 octep_read_dev_config(oct_hw, offset, buf, len);
335 static void octep_vdpa_set_config(struct vdpa_device *vdpa_dev, unsigned int offset,
336 const void *buf, unsigned int len)
341 static void octep_vdpa_set_config_cb(struct vdpa_device *vdpa_dev, struct vdpa_callback *cb)
343 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
345 oct_hw->config_cb.callback = cb->callback;
346 oct_hw->config_cb.private = cb->private;
349 static struct vdpa_notification_area octep_get_vq_notification(struct vdpa_device *vdpa_dev,
352 struct octep_hw *oct_hw = vdpa_to_octep_hw(vdpa_dev);
353 struct vdpa_notification_area area;
355 area.addr = oct_hw->vqs[idx].notify_pa;
356 area.size = PAGE_SIZE;
361 static struct vdpa_config_ops octep_vdpa_ops = {
362 .get_device_features = octep_vdpa_get_device_features,
363 .set_driver_features = octep_vdpa_set_driver_features,
364 .get_driver_features = octep_vdpa_get_driver_features,
365 .get_status = octep_vdpa_get_status,
366 .set_status = octep_vdpa_set_status,
367 .reset = octep_vdpa_reset,
368 .get_vq_num_max = octep_vdpa_get_vq_num_max,
369 .get_vq_state = octep_vdpa_get_vq_state,
370 .set_vq_state = octep_vdpa_set_vq_state,
371 .set_vq_cb = octep_vdpa_set_vq_cb,
372 .set_vq_ready = octep_vdpa_set_vq_ready,
373 .get_vq_ready = octep_vdpa_get_vq_ready,
374 .set_vq_num = octep_vdpa_set_vq_num,
375 .set_vq_address = octep_vdpa_set_vq_address,
377 .kick_vq = octep_vdpa_kick_vq,
378 .kick_vq_with_data = octep_vdpa_kick_vq_with_data,
379 .get_generation = octep_vdpa_get_generation,
380 .get_device_id = octep_vdpa_get_device_id,
381 .get_vendor_id = octep_vdpa_get_vendor_id,
382 .get_vq_align = octep_vdpa_get_vq_align,
383 .get_config_size = octep_vdpa_get_config_size,
384 .get_config = octep_vdpa_get_config,
385 .set_config = octep_vdpa_set_config,
386 .set_config_cb = octep_vdpa_set_config_cb,
387 .get_vq_notification = octep_get_vq_notification,
390 static int octep_iomap_region(struct pci_dev *pdev, u8 __iomem **tbl, u8 bar)
394 ret = pci_request_region(pdev, bar, OCTEP_VDPA_DRIVER_NAME);
396 dev_err(&pdev->dev, "Failed to request BAR:%u region\n", bar);
400 tbl[bar] = pci_iomap(pdev, bar, pci_resource_len(pdev, bar));
402 dev_err(&pdev->dev, "Failed to iomap BAR:%u\n", bar);
403 pci_release_region(pdev, bar);
410 static void octep_iounmap_region(struct pci_dev *pdev, u8 __iomem **tbl, u8 bar)
412 pci_iounmap(pdev, tbl[bar]);
413 pci_release_region(pdev, bar);
416 static void octep_vdpa_pf_bar_shrink(struct octep_pf *octpf)
418 struct pci_dev *pf_dev = octpf->pdev;
419 struct resource *res = pf_dev->resource + PCI_STD_RESOURCES + 4;
420 struct pci_bus_region bus_region;
422 octpf->res.start = res->start;
423 octpf->res.end = res->end;
424 octpf->vf_base = res->start;
426 bus_region.start = res->start;
427 bus_region.end = res->start - 1;
429 pcibios_bus_to_resource(pf_dev->bus, res, &bus_region);
432 static void octep_vdpa_pf_bar_expand(struct octep_pf *octpf)
434 struct pci_dev *pf_dev = octpf->pdev;
435 struct resource *res = pf_dev->resource + PCI_STD_RESOURCES + 4;
436 struct pci_bus_region bus_region;
438 bus_region.start = octpf->res.start;
439 bus_region.end = octpf->res.end;
441 pcibios_bus_to_resource(pf_dev->bus, res, &bus_region);
444 static void octep_vdpa_remove_pf(struct pci_dev *pdev)
446 struct octep_pf *octpf = pci_get_drvdata(pdev);
448 pci_disable_sriov(pdev);
450 if (octpf->base[OCTEP_HW_CAPS_BAR])
451 octep_iounmap_region(pdev, octpf->base, OCTEP_HW_CAPS_BAR);
453 if (octpf->base[OCTEP_HW_MBOX_BAR])
454 octep_iounmap_region(pdev, octpf->base, OCTEP_HW_MBOX_BAR);
456 octep_vdpa_pf_bar_expand(octpf);
459 static void octep_vdpa_vf_bar_shrink(struct pci_dev *pdev)
461 struct resource *vf_res = pdev->resource + PCI_STD_RESOURCES + 4;
463 memset(vf_res, 0, sizeof(*vf_res));
466 static void octep_vdpa_remove_vf(struct pci_dev *pdev)
468 struct octep_vdpa_mgmt_dev *mgmt_dev = pci_get_drvdata(pdev);
469 struct octep_hw *oct_hw;
472 oct_hw = &mgmt_dev->oct_hw;
473 status = atomic_read(&mgmt_dev->status);
474 atomic_set(&mgmt_dev->status, OCTEP_VDPA_DEV_STATUS_UNINIT);
476 cancel_work_sync(&mgmt_dev->setup_task);
477 if (status == OCTEP_VDPA_DEV_STATUS_READY)
478 vdpa_mgmtdev_unregister(&mgmt_dev->mdev);
480 if (oct_hw->base[OCTEP_HW_CAPS_BAR])
481 octep_iounmap_region(pdev, oct_hw->base, OCTEP_HW_CAPS_BAR);
483 if (oct_hw->base[OCTEP_HW_MBOX_BAR])
484 octep_iounmap_region(pdev, oct_hw->base, OCTEP_HW_MBOX_BAR);
486 octep_vdpa_vf_bar_shrink(pdev);
489 static void octep_vdpa_remove(struct pci_dev *pdev)
492 octep_vdpa_remove_vf(pdev);
494 octep_vdpa_remove_pf(pdev);
497 static int octep_vdpa_dev_add(struct vdpa_mgmt_dev *mdev, const char *name,
498 const struct vdpa_dev_set_config *config)
500 struct octep_vdpa_mgmt_dev *mgmt_dev = container_of(mdev, struct octep_vdpa_mgmt_dev, mdev);
501 struct octep_hw *oct_hw = &mgmt_dev->oct_hw;
502 struct pci_dev *pdev = oct_hw->pdev;
503 struct vdpa_device *vdpa_dev;
504 struct octep_vdpa *oct_vdpa;
508 oct_vdpa = vdpa_alloc_device(struct octep_vdpa, vdpa, &pdev->dev, &octep_vdpa_ops, 1, 1,
510 if (IS_ERR(oct_vdpa)) {
511 dev_err(&pdev->dev, "Failed to allocate vDPA structure for octep vdpa device");
512 return PTR_ERR(oct_vdpa);
515 oct_vdpa->pdev = pdev;
516 oct_vdpa->vdpa.dma_dev = &pdev->dev;
517 oct_vdpa->vdpa.mdev = mdev;
518 oct_vdpa->oct_hw = oct_hw;
519 vdpa_dev = &oct_vdpa->vdpa;
521 device_features = oct_hw->features;
522 if (config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) {
523 if (config->device_features & ~device_features) {
524 dev_err(&pdev->dev, "The provisioned features 0x%llx are not supported by this device with features 0x%llx\n",
525 config->device_features, device_features);
529 device_features &= config->device_features;
532 oct_hw->features = device_features;
533 dev_info(&pdev->dev, "Vdpa management device features : %llx\n", device_features);
535 ret = octep_verify_features(device_features);
537 dev_warn(mdev->device,
538 "Must provision minimum features 0x%llx for this device",
539 BIT_ULL(VIRTIO_F_VERSION_1) | BIT_ULL(VIRTIO_F_ACCESS_PLATFORM) |
540 BIT_ULL(VIRTIO_F_NOTIFICATION_DATA) | BIT_ULL(VIRTIO_F_RING_PACKED));
544 ret = dev_set_name(&vdpa_dev->dev, "%s", name);
546 ret = dev_set_name(&vdpa_dev->dev, "vdpa%u", vdpa_dev->index);
548 ret = _vdpa_register_device(&oct_vdpa->vdpa, oct_hw->nr_vring);
550 dev_err(&pdev->dev, "Failed to register to vDPA bus");
556 put_device(&oct_vdpa->vdpa.dev);
560 static void octep_vdpa_dev_del(struct vdpa_mgmt_dev *mdev, struct vdpa_device *vdpa_dev)
562 _vdpa_unregister_device(vdpa_dev);
565 static const struct vdpa_mgmtdev_ops octep_vdpa_mgmt_dev_ops = {
566 .dev_add = octep_vdpa_dev_add,
567 .dev_del = octep_vdpa_dev_del
570 static bool get_device_ready_status(u8 __iomem *addr)
572 u64 signature = readq(addr + OCTEP_VF_MBOX_DATA(0));
574 if (signature == OCTEP_DEV_READY_SIGNATURE) {
575 writeq(0, addr + OCTEP_VF_MBOX_DATA(0));
582 static struct virtio_device_id id_table[] = {
583 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
587 static void octep_vdpa_setup_task(struct work_struct *work)
589 struct octep_vdpa_mgmt_dev *mgmt_dev = container_of(work, struct octep_vdpa_mgmt_dev,
591 struct pci_dev *pdev = mgmt_dev->pdev;
592 struct device *dev = &pdev->dev;
593 struct octep_hw *oct_hw;
594 unsigned long timeout;
598 oct_hw = &mgmt_dev->oct_hw;
600 atomic_set(&mgmt_dev->status, OCTEP_VDPA_DEV_STATUS_WAIT_FOR_BAR_INIT);
602 /* Wait for a maximum of 5 sec */
603 timeout = jiffies + msecs_to_jiffies(5000);
604 while (!time_after(jiffies, timeout)) {
605 if (get_device_ready_status(oct_hw->base[OCTEP_HW_MBOX_BAR])) {
606 atomic_set(&mgmt_dev->status, OCTEP_VDPA_DEV_STATUS_INIT);
610 if (atomic_read(&mgmt_dev->status) >= OCTEP_VDPA_DEV_STATUS_READY) {
611 dev_info(dev, "Stopping vDPA setup task.\n");
615 usleep_range(1000, 1500);
618 if (atomic_read(&mgmt_dev->status) != OCTEP_VDPA_DEV_STATUS_INIT) {
619 dev_err(dev, "BAR initialization is timed out\n");
623 ret = octep_iomap_region(pdev, oct_hw->base, OCTEP_HW_CAPS_BAR);
627 val = readq(oct_hw->base[OCTEP_HW_MBOX_BAR] + OCTEP_VF_IN_CTRL(0));
628 oct_hw->nb_irqs = OCTEP_VF_IN_CTRL_RPVF(val);
629 if (!oct_hw->nb_irqs || oct_hw->nb_irqs > OCTEP_MAX_CB_INTR) {
630 dev_err(dev, "Invalid number of interrupts %d\n", oct_hw->nb_irqs);
634 ret = octep_hw_caps_read(oct_hw, pdev);
638 mgmt_dev->mdev.ops = &octep_vdpa_mgmt_dev_ops;
639 mgmt_dev->mdev.id_table = id_table;
640 mgmt_dev->mdev.max_supported_vqs = oct_hw->nr_vring;
641 mgmt_dev->mdev.supported_features = oct_hw->features;
642 mgmt_dev->mdev.config_attr_mask = (1 << VDPA_ATTR_DEV_FEATURES);
643 mgmt_dev->mdev.device = dev;
645 ret = vdpa_mgmtdev_register(&mgmt_dev->mdev);
647 dev_err(dev, "Failed to register vdpa management interface\n");
651 atomic_set(&mgmt_dev->status, OCTEP_VDPA_DEV_STATUS_READY);
656 octep_iounmap_region(pdev, oct_hw->base, OCTEP_HW_CAPS_BAR);
657 oct_hw->base[OCTEP_HW_CAPS_BAR] = NULL;
660 static int octep_vdpa_probe_vf(struct pci_dev *pdev)
662 struct octep_vdpa_mgmt_dev *mgmt_dev;
663 struct device *dev = &pdev->dev;
666 ret = pcim_enable_device(pdev);
668 dev_err(dev, "Failed to enable device\n");
672 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
674 dev_err(dev, "No usable DMA configuration\n");
677 pci_set_master(pdev);
679 mgmt_dev = devm_kzalloc(dev, sizeof(struct octep_vdpa_mgmt_dev), GFP_KERNEL);
683 ret = octep_iomap_region(pdev, mgmt_dev->oct_hw.base, OCTEP_HW_MBOX_BAR);
687 mgmt_dev->pdev = pdev;
688 pci_set_drvdata(pdev, mgmt_dev);
690 atomic_set(&mgmt_dev->status, OCTEP_VDPA_DEV_STATUS_ALLOC);
691 INIT_WORK(&mgmt_dev->setup_task, octep_vdpa_setup_task);
692 schedule_work(&mgmt_dev->setup_task);
693 dev_info(&pdev->dev, "octep vdpa mgmt device setup task is queued\n");
698 static void octep_vdpa_assign_barspace(struct pci_dev *vf_dev, struct pci_dev *pf_dev, u8 idx)
700 struct resource *vf_res = vf_dev->resource + PCI_STD_RESOURCES + 4;
701 struct resource *pf_res = pf_dev->resource + PCI_STD_RESOURCES + 4;
702 struct octep_pf *pf = pci_get_drvdata(pf_dev);
703 struct pci_bus_region bus_region;
705 vf_res->name = pci_name(vf_dev);
706 vf_res->flags = pf_res->flags;
707 vf_res->parent = (pf_dev->resource + PCI_STD_RESOURCES)->parent;
709 bus_region.start = pf->vf_base + idx * pf->vf_stride;
710 bus_region.end = bus_region.start + pf->vf_stride - 1;
711 pcibios_bus_to_resource(vf_dev->bus, vf_res, &bus_region);
714 static int octep_sriov_enable(struct pci_dev *pdev, int num_vfs)
716 struct octep_pf *pf = pci_get_drvdata(pdev);
717 u8 __iomem *addr = pf->base[OCTEP_HW_MBOX_BAR];
718 struct pci_dev *vf_pdev = NULL;
723 ret = pci_enable_sriov(pdev, num_vfs);
727 pf->enabled_vfs = num_vfs;
729 while ((vf_pdev = pci_get_device(PCI_VENDOR_ID_CAVIUM, PCI_ANY_ID, vf_pdev))) {
730 if (vf_pdev->device != pf->vf_devid)
733 octep_vdpa_assign_barspace(vf_pdev, pdev, index);
734 if (++index == num_vfs) {
741 for (i = 0; i < pf->enabled_vfs; i++)
742 writeq(OCTEP_DEV_READY_SIGNATURE, addr + OCTEP_PF_MBOX_DATA(i));
748 static int octep_sriov_disable(struct pci_dev *pdev)
750 struct octep_pf *pf = pci_get_drvdata(pdev);
752 if (!pci_num_vf(pdev))
755 pci_disable_sriov(pdev);
761 static int octep_vdpa_sriov_configure(struct pci_dev *pdev, int num_vfs)
764 return octep_sriov_enable(pdev, num_vfs);
766 return octep_sriov_disable(pdev);
769 static u16 octep_get_vf_devid(struct pci_dev *pdev)
773 switch (pdev->device) {
774 case OCTEP_VDPA_DEVID_CN106K_PF:
775 did = OCTEP_VDPA_DEVID_CN106K_VF;
777 case OCTEP_VDPA_DEVID_CN105K_PF:
778 did = OCTEP_VDPA_DEVID_CN105K_VF;
780 case OCTEP_VDPA_DEVID_CN103K_PF:
781 did = OCTEP_VDPA_DEVID_CN103K_VF;
791 static int octep_vdpa_pf_setup(struct octep_pf *octpf)
793 u8 __iomem *addr = octpf->base[OCTEP_HW_MBOX_BAR];
794 struct pci_dev *pdev = octpf->pdev;
799 totalvfs = pci_sriov_get_totalvfs(pdev);
800 if (unlikely(!totalvfs)) {
801 dev_info(&pdev->dev, "Total VFs are %d in PF sriov configuration\n", totalvfs);
805 addr = octpf->base[OCTEP_HW_MBOX_BAR];
806 val = readq(addr + OCTEP_EPF_RINFO(0));
808 dev_err(&pdev->dev, "Invalid device configuration\n");
812 len = pci_resource_len(pdev, OCTEP_HW_CAPS_BAR);
814 octpf->vf_stride = len / totalvfs;
815 octpf->vf_devid = octep_get_vf_devid(pdev);
817 octep_vdpa_pf_bar_shrink(octpf);
822 static int octep_vdpa_probe_pf(struct pci_dev *pdev)
824 struct device *dev = &pdev->dev;
825 struct octep_pf *octpf;
828 ret = pcim_enable_device(pdev);
830 dev_err(dev, "Failed to enable device\n");
834 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
836 dev_err(dev, "No usable DMA configuration\n");
839 octpf = devm_kzalloc(dev, sizeof(*octpf), GFP_KERNEL);
843 ret = octep_iomap_region(pdev, octpf->base, OCTEP_HW_MBOX_BAR);
847 pci_set_master(pdev);
848 pci_set_drvdata(pdev, octpf);
851 ret = octep_vdpa_pf_setup(octpf);
858 octep_iounmap_region(pdev, octpf->base, OCTEP_HW_MBOX_BAR);
862 static int octep_vdpa_probe(struct pci_dev *pdev, const struct pci_device_id *id)
865 return octep_vdpa_probe_vf(pdev);
867 return octep_vdpa_probe_pf(pdev);
870 static struct pci_device_id octep_pci_vdpa_map[] = {
871 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, OCTEP_VDPA_DEVID_CN106K_PF) },
872 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, OCTEP_VDPA_DEVID_CN106K_VF) },
873 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, OCTEP_VDPA_DEVID_CN105K_PF) },
874 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, OCTEP_VDPA_DEVID_CN105K_VF) },
875 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, OCTEP_VDPA_DEVID_CN103K_PF) },
876 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, OCTEP_VDPA_DEVID_CN103K_VF) },
880 static struct pci_driver octep_pci_vdpa = {
881 .name = OCTEP_VDPA_DRIVER_NAME,
882 .id_table = octep_pci_vdpa_map,
883 .probe = octep_vdpa_probe,
884 .remove = octep_vdpa_remove,
885 .sriov_configure = octep_vdpa_sriov_configure
888 module_pci_driver(octep_pci_vdpa);
890 MODULE_AUTHOR("Marvell");
891 MODULE_DESCRIPTION("Marvell Octeon PCIe endpoint vDPA driver");
892 MODULE_LICENSE("GPL");