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/export.h>
13 #include <linux/string.h>
14 #include <linux/delay.h>
17 #define VIRTFN_ID_LEN 16
19 int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id)
23 return dev->bus->number + ((dev->devfn + dev->sriov->offset +
24 dev->sriov->stride * vf_id) >> 8);
27 int pci_iov_virtfn_devfn(struct pci_dev *dev, int vf_id)
31 return (dev->devfn + dev->sriov->offset +
32 dev->sriov->stride * vf_id) & 0xff;
36 * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
37 * change when NumVFs changes.
39 * Update iov->offset and iov->stride when NumVFs is written.
41 static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
43 struct pci_sriov *iov = dev->sriov;
45 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
46 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
47 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
51 * The PF consumes one bus number. NumVFs, First VF Offset, and VF Stride
52 * determine how many additional bus numbers will be consumed by VFs.
54 * Iterate over all valid NumVFs, validate offset and stride, and calculate
55 * the maximum number of bus numbers that could ever be required.
57 static int compute_max_vf_buses(struct pci_dev *dev)
59 struct pci_sriov *iov = dev->sriov;
60 int nr_virtfn, busnr, rc = 0;
62 for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) {
63 pci_iov_set_numvfs(dev, nr_virtfn);
64 if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) {
69 busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
70 if (busnr > iov->max_VF_buses)
71 iov->max_VF_buses = busnr;
75 pci_iov_set_numvfs(dev, 0);
79 static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
81 struct pci_bus *child;
83 if (bus->number == busnr)
86 child = pci_find_bus(pci_domain_nr(bus), busnr);
90 child = pci_add_new_bus(bus, NULL, busnr);
94 pci_bus_insert_busn_res(child, busnr, busnr);
99 static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
101 if (physbus != virtbus && list_empty(&virtbus->devices))
102 pci_remove_bus(virtbus);
105 resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
110 return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
113 static void pci_read_vf_config_common(struct pci_dev *virtfn)
115 struct pci_dev *physfn = virtfn->physfn;
118 * Some config registers are the same across all associated VFs.
119 * Read them once from VF0 so we can skip reading them from the
122 * PCIe r4.0, sec 9.3.4.1, technically doesn't require all VFs to
123 * have the same Revision ID and Subsystem ID, but we assume they
126 pci_read_config_dword(virtfn, PCI_CLASS_REVISION,
127 &physfn->sriov->class);
128 pci_read_config_byte(virtfn, PCI_HEADER_TYPE,
129 &physfn->sriov->hdr_type);
130 pci_read_config_word(virtfn, PCI_SUBSYSTEM_VENDOR_ID,
131 &physfn->sriov->subsystem_vendor);
132 pci_read_config_word(virtfn, PCI_SUBSYSTEM_ID,
133 &physfn->sriov->subsystem_device);
136 int pci_iov_sysfs_link(struct pci_dev *dev,
137 struct pci_dev *virtfn, int id)
139 char buf[VIRTFN_ID_LEN];
142 sprintf(buf, "virtfn%u", id);
143 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
146 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
150 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
155 sysfs_remove_link(&dev->dev.kobj, buf);
160 int pci_iov_add_virtfn(struct pci_dev *dev, int id)
165 struct pci_dev *virtfn;
166 struct resource *res;
167 struct pci_sriov *iov = dev->sriov;
170 bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
174 virtfn = pci_alloc_dev(bus);
178 virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
179 virtfn->vendor = dev->vendor;
180 virtfn->device = iov->vf_device;
181 virtfn->is_virtfn = 1;
182 virtfn->physfn = pci_dev_get(dev);
183 virtfn->no_command_memory = 1;
186 pci_read_vf_config_common(virtfn);
188 rc = pci_setup_device(virtfn);
192 virtfn->dev.parent = dev->dev.parent;
193 virtfn->multifunction = 0;
195 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
196 res = &dev->resource[i + PCI_IOV_RESOURCES];
199 virtfn->resource[i].name = pci_name(virtfn);
200 virtfn->resource[i].flags = res->flags;
201 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
202 virtfn->resource[i].start = res->start + size * id;
203 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
204 rc = request_resource(res, &virtfn->resource[i]);
208 pci_device_add(virtfn, virtfn->bus);
209 rc = pci_iov_sysfs_link(dev, virtfn, id);
213 pci_bus_add_device(virtfn);
218 pci_stop_and_remove_bus_device(virtfn);
221 virtfn_remove_bus(dev->bus, bus);
227 void pci_iov_remove_virtfn(struct pci_dev *dev, int id)
229 char buf[VIRTFN_ID_LEN];
230 struct pci_dev *virtfn;
232 virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
233 pci_iov_virtfn_bus(dev, id),
234 pci_iov_virtfn_devfn(dev, id));
238 sprintf(buf, "virtfn%u", id);
239 sysfs_remove_link(&dev->dev.kobj, buf);
241 * pci_stop_dev() could have been called for this virtfn already,
242 * so the directory for the virtfn may have been removed before.
243 * Double check to avoid spurious sysfs warnings.
245 if (virtfn->dev.kobj.sd)
246 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
248 pci_stop_and_remove_bus_device(virtfn);
249 virtfn_remove_bus(dev->bus, virtfn->bus);
251 /* balance pci_get_domain_bus_and_slot() */
256 static ssize_t sriov_totalvfs_show(struct device *dev,
257 struct device_attribute *attr,
260 struct pci_dev *pdev = to_pci_dev(dev);
262 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
265 static ssize_t sriov_numvfs_show(struct device *dev,
266 struct device_attribute *attr,
269 struct pci_dev *pdev = to_pci_dev(dev);
272 /* Serialize vs sriov_numvfs_store() so readers see valid num_VFs */
273 device_lock(&pdev->dev);
274 num_vfs = pdev->sriov->num_VFs;
275 device_unlock(&pdev->dev);
277 return sprintf(buf, "%u\n", num_vfs);
281 * num_vfs > 0; number of VFs to enable
282 * num_vfs = 0; disable all VFs
284 * Note: SRIOV spec does not allow partial VF
285 * disable, so it's all or none.
287 static ssize_t sriov_numvfs_store(struct device *dev,
288 struct device_attribute *attr,
289 const char *buf, size_t count)
291 struct pci_dev *pdev = to_pci_dev(dev);
295 ret = kstrtou16(buf, 0, &num_vfs);
299 if (num_vfs > pci_sriov_get_totalvfs(pdev))
302 device_lock(&pdev->dev);
304 if (num_vfs == pdev->sriov->num_VFs)
307 /* is PF driver loaded w/callback */
308 if (!pdev->driver || !pdev->driver->sriov_configure) {
309 pci_info(pdev, "Driver does not support SRIOV configuration via sysfs\n");
316 ret = pdev->driver->sriov_configure(pdev, 0);
321 if (pdev->sriov->num_VFs) {
322 pci_warn(pdev, "%d VFs already enabled. Disable before enabling %d VFs\n",
323 pdev->sriov->num_VFs, num_vfs);
328 ret = pdev->driver->sriov_configure(pdev, num_vfs);
333 pci_warn(pdev, "%d VFs requested; only %d enabled\n",
337 device_unlock(&pdev->dev);
345 static ssize_t sriov_offset_show(struct device *dev,
346 struct device_attribute *attr,
349 struct pci_dev *pdev = to_pci_dev(dev);
351 return sprintf(buf, "%u\n", pdev->sriov->offset);
354 static ssize_t sriov_stride_show(struct device *dev,
355 struct device_attribute *attr,
358 struct pci_dev *pdev = to_pci_dev(dev);
360 return sprintf(buf, "%u\n", pdev->sriov->stride);
363 static ssize_t sriov_vf_device_show(struct device *dev,
364 struct device_attribute *attr,
367 struct pci_dev *pdev = to_pci_dev(dev);
369 return sprintf(buf, "%x\n", pdev->sriov->vf_device);
372 static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
373 struct device_attribute *attr,
376 struct pci_dev *pdev = to_pci_dev(dev);
378 return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe);
381 static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
382 struct device_attribute *attr,
383 const char *buf, size_t count)
385 struct pci_dev *pdev = to_pci_dev(dev);
386 bool drivers_autoprobe;
388 if (kstrtobool(buf, &drivers_autoprobe) < 0)
391 pdev->sriov->drivers_autoprobe = drivers_autoprobe;
396 static DEVICE_ATTR_RO(sriov_totalvfs);
397 static DEVICE_ATTR_RW(sriov_numvfs);
398 static DEVICE_ATTR_RO(sriov_offset);
399 static DEVICE_ATTR_RO(sriov_stride);
400 static DEVICE_ATTR_RO(sriov_vf_device);
401 static DEVICE_ATTR_RW(sriov_drivers_autoprobe);
403 static struct attribute *sriov_dev_attrs[] = {
404 &dev_attr_sriov_totalvfs.attr,
405 &dev_attr_sriov_numvfs.attr,
406 &dev_attr_sriov_offset.attr,
407 &dev_attr_sriov_stride.attr,
408 &dev_attr_sriov_vf_device.attr,
409 &dev_attr_sriov_drivers_autoprobe.attr,
413 static umode_t sriov_attrs_are_visible(struct kobject *kobj,
414 struct attribute *a, int n)
416 struct device *dev = kobj_to_dev(kobj);
424 const struct attribute_group sriov_dev_attr_group = {
425 .attrs = sriov_dev_attrs,
426 .is_visible = sriov_attrs_are_visible,
429 int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
434 int __weak pcibios_sriov_disable(struct pci_dev *pdev)
439 static int sriov_add_vfs(struct pci_dev *dev, u16 num_vfs)
447 for (i = 0; i < num_vfs; i++) {
448 rc = pci_iov_add_virtfn(dev, i);
455 pci_iov_remove_virtfn(dev, i);
460 static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
466 struct resource *res;
467 struct pci_dev *pdev;
468 struct pci_sriov *iov = dev->sriov;
478 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
479 if (initial > iov->total_VFs ||
480 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
483 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
484 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
488 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
489 bars |= (1 << (i + PCI_IOV_RESOURCES));
490 res = &dev->resource[i + PCI_IOV_RESOURCES];
494 if (nres != iov->nres) {
495 pci_err(dev, "not enough MMIO resources for SR-IOV\n");
499 bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
500 if (bus > dev->bus->busn_res.end) {
501 pci_err(dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
502 nr_virtfn, bus, &dev->bus->busn_res);
506 if (pci_enable_resources(dev, bars)) {
507 pci_err(dev, "SR-IOV: IOV BARS not allocated\n");
511 if (iov->link != dev->devfn) {
512 pdev = pci_get_slot(dev->bus, iov->link);
516 if (!pdev->is_physfn) {
521 rc = sysfs_create_link(&dev->dev.kobj,
522 &pdev->dev.kobj, "dep_link");
528 iov->initial_VFs = initial;
529 if (nr_virtfn < initial)
532 rc = pcibios_sriov_enable(dev, initial);
534 pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc);
538 pci_iov_set_numvfs(dev, nr_virtfn);
539 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
540 pci_cfg_access_lock(dev);
541 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
543 pci_cfg_access_unlock(dev);
545 rc = sriov_add_vfs(dev, initial);
549 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
550 iov->num_VFs = nr_virtfn;
555 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
556 pci_cfg_access_lock(dev);
557 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
559 pci_cfg_access_unlock(dev);
561 pcibios_sriov_disable(dev);
563 if (iov->link != dev->devfn)
564 sysfs_remove_link(&dev->dev.kobj, "dep_link");
566 pci_iov_set_numvfs(dev, 0);
570 static void sriov_del_vfs(struct pci_dev *dev)
572 struct pci_sriov *iov = dev->sriov;
575 for (i = 0; i < iov->num_VFs; i++)
576 pci_iov_remove_virtfn(dev, i);
579 static void sriov_disable(struct pci_dev *dev)
581 struct pci_sriov *iov = dev->sriov;
587 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
588 pci_cfg_access_lock(dev);
589 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
591 pci_cfg_access_unlock(dev);
593 pcibios_sriov_disable(dev);
595 if (iov->link != dev->devfn)
596 sysfs_remove_link(&dev->dev.kobj, "dep_link");
599 pci_iov_set_numvfs(dev, 0);
602 static int sriov_init(struct pci_dev *dev, int pos)
609 struct pci_sriov *iov;
610 struct resource *res;
611 struct pci_dev *pdev;
613 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
614 if (ctrl & PCI_SRIOV_CTRL_VFE) {
615 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
620 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
625 if (pci_ari_enabled(dev->bus))
626 ctrl |= PCI_SRIOV_CTRL_ARI;
629 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
631 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
635 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
636 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
637 pgsz &= ~((1 << i) - 1);
642 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
644 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
649 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
650 res = &dev->resource[i + PCI_IOV_RESOURCES];
652 * If it is already FIXED, don't change it, something
653 * (perhaps EA or header fixups) wants it this way.
655 if (res->flags & IORESOURCE_PCI_FIXED)
656 bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
658 bar64 = __pci_read_base(dev, pci_bar_unknown, res,
659 pos + PCI_SRIOV_BAR + i * 4);
662 if (resource_size(res) & (PAGE_SIZE - 1)) {
666 iov->barsz[i] = resource_size(res);
667 res->end = res->start + resource_size(res) * total - 1;
668 pci_info(dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
677 iov->total_VFs = total;
678 iov->driver_max_VFs = total;
679 pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
682 iov->drivers_autoprobe = true;
683 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
684 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
685 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
686 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
689 iov->dev = pci_dev_get(pdev);
695 rc = compute_max_vf_buses(dev);
705 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
706 res = &dev->resource[i + PCI_IOV_RESOURCES];
714 static void sriov_release(struct pci_dev *dev)
716 BUG_ON(dev->sriov->num_VFs);
718 if (dev != dev->sriov->dev)
719 pci_dev_put(dev->sriov->dev);
725 static void sriov_restore_state(struct pci_dev *dev)
729 struct pci_sriov *iov = dev->sriov;
731 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
732 if (ctrl & PCI_SRIOV_CTRL_VFE)
736 * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
737 * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
739 ctrl &= ~PCI_SRIOV_CTRL_ARI;
740 ctrl |= iov->ctrl & PCI_SRIOV_CTRL_ARI;
741 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
743 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
744 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
746 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
747 pci_iov_set_numvfs(dev, iov->num_VFs);
748 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
749 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
754 * pci_iov_init - initialize the IOV capability
755 * @dev: the PCI device
757 * Returns 0 on success, or negative on failure.
759 int pci_iov_init(struct pci_dev *dev)
763 if (!pci_is_pcie(dev))
766 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
768 return sriov_init(dev, pos);
774 * pci_iov_release - release resources used by the IOV capability
775 * @dev: the PCI device
777 void pci_iov_release(struct pci_dev *dev)
784 * pci_iov_remove - clean up SR-IOV state after PF driver is detached
785 * @dev: the PCI device
787 void pci_iov_remove(struct pci_dev *dev)
789 struct pci_sriov *iov = dev->sriov;
794 iov->driver_max_VFs = iov->total_VFs;
796 pci_warn(dev, "driver left SR-IOV enabled after remove\n");
800 * pci_iov_update_resource - update a VF BAR
801 * @dev: the PCI device
802 * @resno: the resource number
804 * Update a VF BAR in the SR-IOV capability of a PF.
806 void pci_iov_update_resource(struct pci_dev *dev, int resno)
808 struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
809 struct resource *res = dev->resource + resno;
810 int vf_bar = resno - PCI_IOV_RESOURCES;
811 struct pci_bus_region region;
817 * The generic pci_restore_bars() path calls this for all devices,
818 * including VFs and non-SR-IOV devices. If this is not a PF, we
819 * have nothing to do.
824 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
825 if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) {
826 dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
832 * Ignore unimplemented BARs, unused resource slots for 64-bit
833 * BARs, and non-movable resources, e.g., those described via
834 * Enhanced Allocation.
839 if (res->flags & IORESOURCE_UNSET)
842 if (res->flags & IORESOURCE_PCI_FIXED)
845 pcibios_resource_to_bus(dev->bus, ®ion, res);
847 new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK;
849 reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar;
850 pci_write_config_dword(dev, reg, new);
851 if (res->flags & IORESOURCE_MEM_64) {
852 new = region.start >> 16 >> 16;
853 pci_write_config_dword(dev, reg + 4, new);
857 resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
860 return pci_iov_resource_size(dev, resno);
864 * pci_sriov_resource_alignment - get resource alignment for VF BAR
865 * @dev: the PCI device
866 * @resno: the resource number
868 * Returns the alignment of the VF BAR found in the SR-IOV capability.
869 * This is not the same as the resource size which is defined as
870 * the VF BAR size multiplied by the number of VFs. The alignment
871 * is just the VF BAR size.
873 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
875 return pcibios_iov_resource_alignment(dev, resno);
879 * pci_restore_iov_state - restore the state of the IOV capability
880 * @dev: the PCI device
882 void pci_restore_iov_state(struct pci_dev *dev)
885 sriov_restore_state(dev);
889 * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs
890 * @dev: the PCI device
891 * @auto_probe: set VF drivers auto probe flag
893 void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe)
896 dev->sriov->drivers_autoprobe = auto_probe;
900 * pci_iov_bus_range - find bus range used by Virtual Function
903 * Returns max number of buses (exclude current one) used by Virtual
906 int pci_iov_bus_range(struct pci_bus *bus)
911 list_for_each_entry(dev, &bus->devices, bus_list) {
914 if (dev->sriov->max_VF_buses > max)
915 max = dev->sriov->max_VF_buses;
918 return max ? max - bus->number : 0;
922 * pci_enable_sriov - enable the SR-IOV capability
923 * @dev: the PCI device
924 * @nr_virtfn: number of virtual functions to enable
926 * Returns 0 on success, or negative on failure.
928 int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
935 return sriov_enable(dev, nr_virtfn);
937 EXPORT_SYMBOL_GPL(pci_enable_sriov);
940 * pci_disable_sriov - disable the SR-IOV capability
941 * @dev: the PCI device
943 void pci_disable_sriov(struct pci_dev *dev)
952 EXPORT_SYMBOL_GPL(pci_disable_sriov);
955 * pci_num_vf - return number of VFs associated with a PF device_release_driver
956 * @dev: the PCI device
958 * Returns number of VFs, or 0 if SR-IOV is not enabled.
960 int pci_num_vf(struct pci_dev *dev)
965 return dev->sriov->num_VFs;
967 EXPORT_SYMBOL_GPL(pci_num_vf);
970 * pci_vfs_assigned - returns number of VFs are assigned to a guest
971 * @dev: the PCI device
973 * Returns number of VFs belonging to this device that are assigned to a guest.
974 * If device is not a physical function returns 0.
976 int pci_vfs_assigned(struct pci_dev *dev)
978 struct pci_dev *vfdev;
979 unsigned int vfs_assigned = 0;
980 unsigned short dev_id;
982 /* only search if we are a PF */
987 * determine the device ID for the VFs, the vendor ID will be the
988 * same as the PF so there is no need to check for that one
990 dev_id = dev->sriov->vf_device;
992 /* loop through all the VFs to see if we own any that are assigned */
993 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
996 * It is considered assigned if it is a virtual function with
997 * our dev as the physical function and the assigned bit is set
999 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
1000 pci_is_dev_assigned(vfdev))
1003 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
1006 return vfs_assigned;
1008 EXPORT_SYMBOL_GPL(pci_vfs_assigned);
1011 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
1012 * @dev: the PCI PF device
1013 * @numvfs: number that should be used for TotalVFs supported
1015 * Should be called from PF driver's probe routine with
1016 * device's mutex held.
1018 * Returns 0 if PF is an SRIOV-capable device and
1019 * value of numvfs valid. If not a PF return -ENOSYS;
1020 * if numvfs is invalid return -EINVAL;
1021 * if VFs already enabled, return -EBUSY.
1023 int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
1025 if (!dev->is_physfn)
1028 if (numvfs > dev->sriov->total_VFs)
1031 /* Shouldn't change if VFs already enabled */
1032 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
1035 dev->sriov->driver_max_VFs = numvfs;
1038 EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
1041 * pci_sriov_get_totalvfs -- get total VFs supported on this device
1042 * @dev: the PCI PF device
1044 * For a PCIe device with SRIOV support, return the PCIe
1045 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
1046 * if the driver reduced it. Otherwise 0.
1048 int pci_sriov_get_totalvfs(struct pci_dev *dev)
1050 if (!dev->is_physfn)
1053 return dev->sriov->driver_max_VFs;
1055 EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);
1058 * pci_sriov_configure_simple - helper to configure SR-IOV
1059 * @dev: the PCI device
1060 * @nr_virtfn: number of virtual functions to enable, 0 to disable
1062 * Enable or disable SR-IOV for devices that don't require any PF setup
1063 * before enabling SR-IOV. Return value is negative on error, or number of
1064 * VFs allocated on success.
1066 int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn)
1072 if (!dev->is_physfn)
1075 if (pci_vfs_assigned(dev)) {
1076 pci_warn(dev, "Cannot modify SR-IOV while VFs are assigned\n");
1080 if (nr_virtfn == 0) {
1085 rc = sriov_enable(dev, nr_virtfn);
1091 EXPORT_SYMBOL_GPL(pci_sriov_configure_simple);