1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Express I/O Virtualization (IOV) support
5 * Address Translation Service 1.0
7 * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
10 #include <linux/pci.h>
11 #include <linux/slab.h>
12 #include <linux/mutex.h>
13 #include <linux/export.h>
14 #include <linux/string.h>
15 #include <linux/delay.h>
18 #define VIRTFN_ID_LEN 16
20 int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id)
24 return dev->bus->number + ((dev->devfn + dev->sriov->offset +
25 dev->sriov->stride * vf_id) >> 8);
28 int pci_iov_virtfn_devfn(struct pci_dev *dev, int vf_id)
32 return (dev->devfn + dev->sriov->offset +
33 dev->sriov->stride * vf_id) & 0xff;
37 * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
38 * change when NumVFs changes.
40 * Update iov->offset and iov->stride when NumVFs is written.
42 static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
44 struct pci_sriov *iov = dev->sriov;
46 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
47 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
48 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
52 * The PF consumes one bus number. NumVFs, First VF Offset, and VF Stride
53 * determine how many additional bus numbers will be consumed by VFs.
55 * Iterate over all valid NumVFs, validate offset and stride, and calculate
56 * the maximum number of bus numbers that could ever be required.
58 static int compute_max_vf_buses(struct pci_dev *dev)
60 struct pci_sriov *iov = dev->sriov;
61 int nr_virtfn, busnr, rc = 0;
63 for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) {
64 pci_iov_set_numvfs(dev, nr_virtfn);
65 if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) {
70 busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
71 if (busnr > iov->max_VF_buses)
72 iov->max_VF_buses = busnr;
76 pci_iov_set_numvfs(dev, 0);
80 static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
82 struct pci_bus *child;
84 if (bus->number == busnr)
87 child = pci_find_bus(pci_domain_nr(bus), busnr);
91 child = pci_add_new_bus(bus, NULL, busnr);
95 pci_bus_insert_busn_res(child, busnr, busnr);
100 static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
102 if (physbus != virtbus && list_empty(&virtbus->devices))
103 pci_remove_bus(virtbus);
106 resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
111 return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
114 static void pci_read_vf_config_common(struct pci_dev *virtfn)
116 struct pci_dev *physfn = virtfn->physfn;
119 * Some config registers are the same across all associated VFs.
120 * Read them once from VF0 so we can skip reading them from the
123 * PCIe r4.0, sec 9.3.4.1, technically doesn't require all VFs to
124 * have the same Revision ID and Subsystem ID, but we assume they
127 pci_read_config_dword(virtfn, PCI_CLASS_REVISION,
128 &physfn->sriov->class);
129 pci_read_config_byte(virtfn, PCI_HEADER_TYPE,
130 &physfn->sriov->hdr_type);
131 pci_read_config_word(virtfn, PCI_SUBSYSTEM_VENDOR_ID,
132 &physfn->sriov->subsystem_vendor);
133 pci_read_config_word(virtfn, PCI_SUBSYSTEM_ID,
134 &physfn->sriov->subsystem_device);
137 int pci_iov_add_virtfn(struct pci_dev *dev, int id)
142 char buf[VIRTFN_ID_LEN];
143 struct pci_dev *virtfn;
144 struct resource *res;
145 struct pci_sriov *iov = dev->sriov;
148 bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
152 virtfn = pci_alloc_dev(bus);
156 virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
157 virtfn->vendor = dev->vendor;
158 virtfn->device = iov->vf_device;
159 virtfn->is_virtfn = 1;
160 virtfn->physfn = pci_dev_get(dev);
163 pci_read_vf_config_common(virtfn);
165 rc = pci_setup_device(virtfn);
169 virtfn->dev.parent = dev->dev.parent;
170 virtfn->multifunction = 0;
172 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
173 res = &dev->resource[i + PCI_IOV_RESOURCES];
176 virtfn->resource[i].name = pci_name(virtfn);
177 virtfn->resource[i].flags = res->flags;
178 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
179 virtfn->resource[i].start = res->start + size * id;
180 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
181 rc = request_resource(res, &virtfn->resource[i]);
185 pci_device_add(virtfn, virtfn->bus);
187 sprintf(buf, "virtfn%u", id);
188 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
191 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
195 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
197 pci_bus_add_device(virtfn);
202 sysfs_remove_link(&dev->dev.kobj, buf);
204 pci_stop_and_remove_bus_device(virtfn);
207 virtfn_remove_bus(dev->bus, bus);
213 void pci_iov_remove_virtfn(struct pci_dev *dev, int id)
215 char buf[VIRTFN_ID_LEN];
216 struct pci_dev *virtfn;
218 virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
219 pci_iov_virtfn_bus(dev, id),
220 pci_iov_virtfn_devfn(dev, id));
224 sprintf(buf, "virtfn%u", id);
225 sysfs_remove_link(&dev->dev.kobj, buf);
227 * pci_stop_dev() could have been called for this virtfn already,
228 * so the directory for the virtfn may have been removed before.
229 * Double check to avoid spurious sysfs warnings.
231 if (virtfn->dev.kobj.sd)
232 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
234 pci_stop_and_remove_bus_device(virtfn);
235 virtfn_remove_bus(dev->bus, virtfn->bus);
237 /* balance pci_get_domain_bus_and_slot() */
242 static ssize_t sriov_totalvfs_show(struct device *dev,
243 struct device_attribute *attr,
246 struct pci_dev *pdev = to_pci_dev(dev);
248 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
251 static ssize_t sriov_numvfs_show(struct device *dev,
252 struct device_attribute *attr,
255 struct pci_dev *pdev = to_pci_dev(dev);
257 return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
261 * num_vfs > 0; number of VFs to enable
262 * num_vfs = 0; disable all VFs
264 * Note: SRIOV spec does not allow partial VF
265 * disable, so it's all or none.
267 static ssize_t sriov_numvfs_store(struct device *dev,
268 struct device_attribute *attr,
269 const char *buf, size_t count)
271 struct pci_dev *pdev = to_pci_dev(dev);
275 ret = kstrtou16(buf, 0, &num_vfs);
279 if (num_vfs > pci_sriov_get_totalvfs(pdev))
282 device_lock(&pdev->dev);
284 if (num_vfs == pdev->sriov->num_VFs)
287 /* is PF driver loaded w/callback */
288 if (!pdev->driver || !pdev->driver->sriov_configure) {
289 pci_info(pdev, "Driver does not support SRIOV configuration via sysfs\n");
296 ret = pdev->driver->sriov_configure(pdev, 0);
301 if (pdev->sriov->num_VFs) {
302 pci_warn(pdev, "%d VFs already enabled. Disable before enabling %d VFs\n",
303 pdev->sriov->num_VFs, num_vfs);
308 ret = pdev->driver->sriov_configure(pdev, num_vfs);
313 pci_warn(pdev, "%d VFs requested; only %d enabled\n",
317 device_unlock(&pdev->dev);
325 static ssize_t sriov_offset_show(struct device *dev,
326 struct device_attribute *attr,
329 struct pci_dev *pdev = to_pci_dev(dev);
331 return sprintf(buf, "%u\n", pdev->sriov->offset);
334 static ssize_t sriov_stride_show(struct device *dev,
335 struct device_attribute *attr,
338 struct pci_dev *pdev = to_pci_dev(dev);
340 return sprintf(buf, "%u\n", pdev->sriov->stride);
343 static ssize_t sriov_vf_device_show(struct device *dev,
344 struct device_attribute *attr,
347 struct pci_dev *pdev = to_pci_dev(dev);
349 return sprintf(buf, "%x\n", pdev->sriov->vf_device);
352 static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
353 struct device_attribute *attr,
356 struct pci_dev *pdev = to_pci_dev(dev);
358 return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe);
361 static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
362 struct device_attribute *attr,
363 const char *buf, size_t count)
365 struct pci_dev *pdev = to_pci_dev(dev);
366 bool drivers_autoprobe;
368 if (kstrtobool(buf, &drivers_autoprobe) < 0)
371 pdev->sriov->drivers_autoprobe = drivers_autoprobe;
376 static DEVICE_ATTR_RO(sriov_totalvfs);
377 static DEVICE_ATTR_RW(sriov_numvfs);
378 static DEVICE_ATTR_RO(sriov_offset);
379 static DEVICE_ATTR_RO(sriov_stride);
380 static DEVICE_ATTR_RO(sriov_vf_device);
381 static DEVICE_ATTR_RW(sriov_drivers_autoprobe);
383 static struct attribute *sriov_dev_attrs[] = {
384 &dev_attr_sriov_totalvfs.attr,
385 &dev_attr_sriov_numvfs.attr,
386 &dev_attr_sriov_offset.attr,
387 &dev_attr_sriov_stride.attr,
388 &dev_attr_sriov_vf_device.attr,
389 &dev_attr_sriov_drivers_autoprobe.attr,
393 static umode_t sriov_attrs_are_visible(struct kobject *kobj,
394 struct attribute *a, int n)
396 struct device *dev = kobj_to_dev(kobj);
404 const struct attribute_group sriov_dev_attr_group = {
405 .attrs = sriov_dev_attrs,
406 .is_visible = sriov_attrs_are_visible,
409 int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
414 int __weak pcibios_sriov_disable(struct pci_dev *pdev)
419 static int sriov_add_vfs(struct pci_dev *dev, u16 num_vfs)
427 for (i = 0; i < num_vfs; i++) {
428 rc = pci_iov_add_virtfn(dev, i);
435 pci_iov_remove_virtfn(dev, i);
440 static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
446 struct resource *res;
447 struct pci_dev *pdev;
448 struct pci_sriov *iov = dev->sriov;
458 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
459 if (initial > iov->total_VFs ||
460 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
463 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
464 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
468 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
469 bars |= (1 << (i + PCI_IOV_RESOURCES));
470 res = &dev->resource[i + PCI_IOV_RESOURCES];
474 if (nres != iov->nres) {
475 pci_err(dev, "not enough MMIO resources for SR-IOV\n");
479 bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
480 if (bus > dev->bus->busn_res.end) {
481 pci_err(dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
482 nr_virtfn, bus, &dev->bus->busn_res);
486 if (pci_enable_resources(dev, bars)) {
487 pci_err(dev, "SR-IOV: IOV BARS not allocated\n");
491 if (iov->link != dev->devfn) {
492 pdev = pci_get_slot(dev->bus, iov->link);
496 if (!pdev->is_physfn) {
501 rc = sysfs_create_link(&dev->dev.kobj,
502 &pdev->dev.kobj, "dep_link");
508 iov->initial_VFs = initial;
509 if (nr_virtfn < initial)
512 rc = pcibios_sriov_enable(dev, initial);
514 pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc);
518 pci_iov_set_numvfs(dev, nr_virtfn);
519 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
520 pci_cfg_access_lock(dev);
521 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
523 pci_cfg_access_unlock(dev);
525 rc = sriov_add_vfs(dev, initial);
529 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
530 iov->num_VFs = nr_virtfn;
535 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
536 pci_cfg_access_lock(dev);
537 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
539 pci_cfg_access_unlock(dev);
541 pcibios_sriov_disable(dev);
543 if (iov->link != dev->devfn)
544 sysfs_remove_link(&dev->dev.kobj, "dep_link");
546 pci_iov_set_numvfs(dev, 0);
550 static void sriov_del_vfs(struct pci_dev *dev)
552 struct pci_sriov *iov = dev->sriov;
558 for (i = 0; i < iov->num_VFs; i++)
559 pci_iov_remove_virtfn(dev, i);
562 static void sriov_disable(struct pci_dev *dev)
564 struct pci_sriov *iov = dev->sriov;
570 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
571 pci_cfg_access_lock(dev);
572 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
574 pci_cfg_access_unlock(dev);
576 pcibios_sriov_disable(dev);
578 if (iov->link != dev->devfn)
579 sysfs_remove_link(&dev->dev.kobj, "dep_link");
582 pci_iov_set_numvfs(dev, 0);
585 static int sriov_init(struct pci_dev *dev, int pos)
592 struct pci_sriov *iov;
593 struct resource *res;
594 struct pci_dev *pdev;
596 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
597 if (ctrl & PCI_SRIOV_CTRL_VFE) {
598 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
603 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
608 if (pci_ari_enabled(dev->bus))
609 ctrl |= PCI_SRIOV_CTRL_ARI;
612 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
614 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
618 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
619 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
620 pgsz &= ~((1 << i) - 1);
625 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
627 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
632 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
633 res = &dev->resource[i + PCI_IOV_RESOURCES];
635 * If it is already FIXED, don't change it, something
636 * (perhaps EA or header fixups) wants it this way.
638 if (res->flags & IORESOURCE_PCI_FIXED)
639 bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
641 bar64 = __pci_read_base(dev, pci_bar_unknown, res,
642 pos + PCI_SRIOV_BAR + i * 4);
645 if (resource_size(res) & (PAGE_SIZE - 1)) {
649 iov->barsz[i] = resource_size(res);
650 res->end = res->start + resource_size(res) * total - 1;
651 pci_info(dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
660 iov->total_VFs = total;
661 iov->driver_max_VFs = total;
662 pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
665 iov->drivers_autoprobe = true;
666 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
667 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
668 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
669 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
672 iov->dev = pci_dev_get(pdev);
678 rc = compute_max_vf_buses(dev);
688 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
689 res = &dev->resource[i + PCI_IOV_RESOURCES];
697 static void sriov_release(struct pci_dev *dev)
699 BUG_ON(dev->sriov->num_VFs);
701 if (dev != dev->sriov->dev)
702 pci_dev_put(dev->sriov->dev);
708 static void sriov_restore_state(struct pci_dev *dev)
712 struct pci_sriov *iov = dev->sriov;
714 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
715 if (ctrl & PCI_SRIOV_CTRL_VFE)
719 * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
720 * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
722 ctrl &= ~PCI_SRIOV_CTRL_ARI;
723 ctrl |= iov->ctrl & PCI_SRIOV_CTRL_ARI;
724 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
726 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
727 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
729 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
730 pci_iov_set_numvfs(dev, iov->num_VFs);
731 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
732 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
737 * pci_iov_init - initialize the IOV capability
738 * @dev: the PCI device
740 * Returns 0 on success, or negative on failure.
742 int pci_iov_init(struct pci_dev *dev)
746 if (!pci_is_pcie(dev))
749 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
751 return sriov_init(dev, pos);
757 * pci_iov_release - release resources used by the IOV capability
758 * @dev: the PCI device
760 void pci_iov_release(struct pci_dev *dev)
767 * pci_iov_remove - clean up SR-IOV state after PF driver is detached
768 * @dev: the PCI device
770 void pci_iov_remove(struct pci_dev *dev)
772 struct pci_sriov *iov = dev->sriov;
777 iov->driver_max_VFs = iov->total_VFs;
779 pci_warn(dev, "driver left SR-IOV enabled after remove\n");
783 * pci_iov_update_resource - update a VF BAR
784 * @dev: the PCI device
785 * @resno: the resource number
787 * Update a VF BAR in the SR-IOV capability of a PF.
789 void pci_iov_update_resource(struct pci_dev *dev, int resno)
791 struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
792 struct resource *res = dev->resource + resno;
793 int vf_bar = resno - PCI_IOV_RESOURCES;
794 struct pci_bus_region region;
800 * The generic pci_restore_bars() path calls this for all devices,
801 * including VFs and non-SR-IOV devices. If this is not a PF, we
802 * have nothing to do.
807 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
808 if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) {
809 dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
815 * Ignore unimplemented BARs, unused resource slots for 64-bit
816 * BARs, and non-movable resources, e.g., those described via
817 * Enhanced Allocation.
822 if (res->flags & IORESOURCE_UNSET)
825 if (res->flags & IORESOURCE_PCI_FIXED)
828 pcibios_resource_to_bus(dev->bus, ®ion, res);
830 new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK;
832 reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar;
833 pci_write_config_dword(dev, reg, new);
834 if (res->flags & IORESOURCE_MEM_64) {
835 new = region.start >> 16 >> 16;
836 pci_write_config_dword(dev, reg + 4, new);
840 resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
843 return pci_iov_resource_size(dev, resno);
847 * pci_sriov_resource_alignment - get resource alignment for VF BAR
848 * @dev: the PCI device
849 * @resno: the resource number
851 * Returns the alignment of the VF BAR found in the SR-IOV capability.
852 * This is not the same as the resource size which is defined as
853 * the VF BAR size multiplied by the number of VFs. The alignment
854 * is just the VF BAR size.
856 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
858 return pcibios_iov_resource_alignment(dev, resno);
862 * pci_restore_iov_state - restore the state of the IOV capability
863 * @dev: the PCI device
865 void pci_restore_iov_state(struct pci_dev *dev)
868 sriov_restore_state(dev);
872 * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs
873 * @dev: the PCI device
874 * @auto_probe: set VF drivers auto probe flag
876 void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe)
879 dev->sriov->drivers_autoprobe = auto_probe;
883 * pci_iov_bus_range - find bus range used by Virtual Function
886 * Returns max number of buses (exclude current one) used by Virtual
889 int pci_iov_bus_range(struct pci_bus *bus)
894 list_for_each_entry(dev, &bus->devices, bus_list) {
897 if (dev->sriov->max_VF_buses > max)
898 max = dev->sriov->max_VF_buses;
901 return max ? max - bus->number : 0;
905 * pci_enable_sriov - enable the SR-IOV capability
906 * @dev: the PCI device
907 * @nr_virtfn: number of virtual functions to enable
909 * Returns 0 on success, or negative on failure.
911 int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
918 return sriov_enable(dev, nr_virtfn);
920 EXPORT_SYMBOL_GPL(pci_enable_sriov);
923 * pci_disable_sriov - disable the SR-IOV capability
924 * @dev: the PCI device
926 void pci_disable_sriov(struct pci_dev *dev)
935 EXPORT_SYMBOL_GPL(pci_disable_sriov);
938 * pci_num_vf - return number of VFs associated with a PF device_release_driver
939 * @dev: the PCI device
941 * Returns number of VFs, or 0 if SR-IOV is not enabled.
943 int pci_num_vf(struct pci_dev *dev)
948 return dev->sriov->num_VFs;
950 EXPORT_SYMBOL_GPL(pci_num_vf);
953 * pci_vfs_assigned - returns number of VFs are assigned to a guest
954 * @dev: the PCI device
956 * Returns number of VFs belonging to this device that are assigned to a guest.
957 * If device is not a physical function returns 0.
959 int pci_vfs_assigned(struct pci_dev *dev)
961 struct pci_dev *vfdev;
962 unsigned int vfs_assigned = 0;
963 unsigned short dev_id;
965 /* only search if we are a PF */
970 * determine the device ID for the VFs, the vendor ID will be the
971 * same as the PF so there is no need to check for that one
973 dev_id = dev->sriov->vf_device;
975 /* loop through all the VFs to see if we own any that are assigned */
976 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
979 * It is considered assigned if it is a virtual function with
980 * our dev as the physical function and the assigned bit is set
982 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
983 pci_is_dev_assigned(vfdev))
986 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
991 EXPORT_SYMBOL_GPL(pci_vfs_assigned);
994 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
995 * @dev: the PCI PF device
996 * @numvfs: number that should be used for TotalVFs supported
998 * Should be called from PF driver's probe routine with
999 * device's mutex held.
1001 * Returns 0 if PF is an SRIOV-capable device and
1002 * value of numvfs valid. If not a PF return -ENOSYS;
1003 * if numvfs is invalid return -EINVAL;
1004 * if VFs already enabled, return -EBUSY.
1006 int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
1008 if (!dev->is_physfn)
1011 if (numvfs > dev->sriov->total_VFs)
1014 /* Shouldn't change if VFs already enabled */
1015 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
1018 dev->sriov->driver_max_VFs = numvfs;
1021 EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
1024 * pci_sriov_get_totalvfs -- get total VFs supported on this device
1025 * @dev: the PCI PF device
1027 * For a PCIe device with SRIOV support, return the PCIe
1028 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
1029 * if the driver reduced it. Otherwise 0.
1031 int pci_sriov_get_totalvfs(struct pci_dev *dev)
1033 if (!dev->is_physfn)
1036 return dev->sriov->driver_max_VFs;
1038 EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);
1041 * pci_sriov_configure_simple - helper to configure SR-IOV
1042 * @dev: the PCI device
1043 * @nr_virtfn: number of virtual functions to enable, 0 to disable
1045 * Enable or disable SR-IOV for devices that don't require any PF setup
1046 * before enabling SR-IOV. Return value is negative on error, or number of
1047 * VFs allocated on success.
1049 int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn)
1055 if (!dev->is_physfn)
1058 if (pci_vfs_assigned(dev)) {
1059 pci_warn(dev, "Cannot modify SR-IOV while VFs are assigned\n");
1063 if (nr_virtfn == 0) {
1068 rc = sriov_enable(dev, nr_virtfn);
1074 EXPORT_SYMBOL_GPL(pci_sriov_configure_simple);