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;
34 EXPORT_SYMBOL_GPL(pci_iov_virtfn_devfn);
36 int pci_iov_vf_id(struct pci_dev *dev)
44 return (((dev->bus->number << 8) + dev->devfn) -
45 ((pf->bus->number << 8) + pf->devfn + pf->sriov->offset)) /
48 EXPORT_SYMBOL_GPL(pci_iov_vf_id);
51 * pci_iov_get_pf_drvdata - Return the drvdata of a PF
53 * @pf_driver: Device driver required to own the PF
55 * This must be called from a context that ensures that a VF driver is attached.
56 * The value returned is invalid once the VF driver completes its remove()
59 * Locking is achieved by the driver core. A VF driver cannot be probed until
60 * pci_enable_sriov() is called and pci_disable_sriov() does not return until
61 * all VF drivers have completed their remove().
63 * The PF driver must call pci_disable_sriov() before it begins to destroy the
66 void *pci_iov_get_pf_drvdata(struct pci_dev *dev, struct pci_driver *pf_driver)
68 struct pci_dev *pf_dev;
71 return ERR_PTR(-EINVAL);
73 if (pf_dev->driver != pf_driver)
74 return ERR_PTR(-EINVAL);
75 return pci_get_drvdata(pf_dev);
77 EXPORT_SYMBOL_GPL(pci_iov_get_pf_drvdata);
80 * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
81 * change when NumVFs changes.
83 * Update iov->offset and iov->stride when NumVFs is written.
85 static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
87 struct pci_sriov *iov = dev->sriov;
89 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
90 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
91 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
95 * The PF consumes one bus number. NumVFs, First VF Offset, and VF Stride
96 * determine how many additional bus numbers will be consumed by VFs.
98 * Iterate over all valid NumVFs, validate offset and stride, and calculate
99 * the maximum number of bus numbers that could ever be required.
101 static int compute_max_vf_buses(struct pci_dev *dev)
103 struct pci_sriov *iov = dev->sriov;
104 int nr_virtfn, busnr, rc = 0;
106 for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) {
107 pci_iov_set_numvfs(dev, nr_virtfn);
108 if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) {
113 busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
114 if (busnr > iov->max_VF_buses)
115 iov->max_VF_buses = busnr;
119 pci_iov_set_numvfs(dev, 0);
123 static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
125 struct pci_bus *child;
127 if (bus->number == busnr)
130 child = pci_find_bus(pci_domain_nr(bus), busnr);
134 child = pci_add_new_bus(bus, NULL, busnr);
138 pci_bus_insert_busn_res(child, busnr, busnr);
143 static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
145 if (physbus != virtbus && list_empty(&virtbus->devices))
146 pci_remove_bus(virtbus);
149 resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
154 return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
157 static void pci_read_vf_config_common(struct pci_dev *virtfn)
159 struct pci_dev *physfn = virtfn->physfn;
162 * Some config registers are the same across all associated VFs.
163 * Read them once from VF0 so we can skip reading them from the
166 * PCIe r4.0, sec 9.3.4.1, technically doesn't require all VFs to
167 * have the same Revision ID and Subsystem ID, but we assume they
170 pci_read_config_dword(virtfn, PCI_CLASS_REVISION,
171 &physfn->sriov->class);
172 pci_read_config_byte(virtfn, PCI_HEADER_TYPE,
173 &physfn->sriov->hdr_type);
174 pci_read_config_word(virtfn, PCI_SUBSYSTEM_VENDOR_ID,
175 &physfn->sriov->subsystem_vendor);
176 pci_read_config_word(virtfn, PCI_SUBSYSTEM_ID,
177 &physfn->sriov->subsystem_device);
180 int pci_iov_sysfs_link(struct pci_dev *dev,
181 struct pci_dev *virtfn, int id)
183 char buf[VIRTFN_ID_LEN];
186 sprintf(buf, "virtfn%u", id);
187 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
190 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
194 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
199 sysfs_remove_link(&dev->dev.kobj, buf);
204 #ifdef CONFIG_PCI_MSI
205 static ssize_t sriov_vf_total_msix_show(struct device *dev,
206 struct device_attribute *attr,
209 struct pci_dev *pdev = to_pci_dev(dev);
210 u32 vf_total_msix = 0;
213 if (!pdev->driver || !pdev->driver->sriov_get_vf_total_msix)
216 vf_total_msix = pdev->driver->sriov_get_vf_total_msix(pdev);
219 return sysfs_emit(buf, "%u\n", vf_total_msix);
221 static DEVICE_ATTR_RO(sriov_vf_total_msix);
223 static ssize_t sriov_vf_msix_count_store(struct device *dev,
224 struct device_attribute *attr,
225 const char *buf, size_t count)
227 struct pci_dev *vf_dev = to_pci_dev(dev);
228 struct pci_dev *pdev = pci_physfn(vf_dev);
231 if (kstrtoint(buf, 0, &val) < 0)
237 device_lock(&pdev->dev);
238 if (!pdev->driver || !pdev->driver->sriov_set_msix_vec_count) {
243 device_lock(&vf_dev->dev);
244 if (vf_dev->driver) {
246 * A driver is already attached to this VF and has configured
247 * itself based on the current MSI-X vector count. Changing
248 * the vector size could mess up the driver, so block it.
254 ret = pdev->driver->sriov_set_msix_vec_count(vf_dev, val);
257 device_unlock(&vf_dev->dev);
259 device_unlock(&pdev->dev);
260 return ret ? : count;
262 static DEVICE_ATTR_WO(sriov_vf_msix_count);
265 static struct attribute *sriov_vf_dev_attrs[] = {
266 #ifdef CONFIG_PCI_MSI
267 &dev_attr_sriov_vf_msix_count.attr,
272 static umode_t sriov_vf_attrs_are_visible(struct kobject *kobj,
273 struct attribute *a, int n)
275 struct device *dev = kobj_to_dev(kobj);
276 struct pci_dev *pdev = to_pci_dev(dev);
278 if (!pdev->is_virtfn)
284 const struct attribute_group sriov_vf_dev_attr_group = {
285 .attrs = sriov_vf_dev_attrs,
286 .is_visible = sriov_vf_attrs_are_visible,
289 int pci_iov_add_virtfn(struct pci_dev *dev, int id)
294 struct pci_dev *virtfn;
295 struct resource *res;
296 struct pci_sriov *iov = dev->sriov;
299 bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
303 virtfn = pci_alloc_dev(bus);
307 virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
308 virtfn->vendor = dev->vendor;
309 virtfn->device = iov->vf_device;
310 virtfn->is_virtfn = 1;
311 virtfn->physfn = pci_dev_get(dev);
312 virtfn->no_command_memory = 1;
315 pci_read_vf_config_common(virtfn);
317 rc = pci_setup_device(virtfn);
321 virtfn->dev.parent = dev->dev.parent;
322 virtfn->multifunction = 0;
324 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
325 res = &dev->resource[i + PCI_IOV_RESOURCES];
328 virtfn->resource[i].name = pci_name(virtfn);
329 virtfn->resource[i].flags = res->flags;
330 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
331 virtfn->resource[i].start = res->start + size * id;
332 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
333 rc = request_resource(res, &virtfn->resource[i]);
337 pci_device_add(virtfn, virtfn->bus);
338 rc = pci_iov_sysfs_link(dev, virtfn, id);
342 pci_bus_add_device(virtfn);
347 pci_stop_and_remove_bus_device(virtfn);
350 virtfn_remove_bus(dev->bus, bus);
356 void pci_iov_remove_virtfn(struct pci_dev *dev, int id)
358 char buf[VIRTFN_ID_LEN];
359 struct pci_dev *virtfn;
361 virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
362 pci_iov_virtfn_bus(dev, id),
363 pci_iov_virtfn_devfn(dev, id));
367 sprintf(buf, "virtfn%u", id);
368 sysfs_remove_link(&dev->dev.kobj, buf);
370 * pci_stop_dev() could have been called for this virtfn already,
371 * so the directory for the virtfn may have been removed before.
372 * Double check to avoid spurious sysfs warnings.
374 if (virtfn->dev.kobj.sd)
375 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
377 pci_stop_and_remove_bus_device(virtfn);
378 virtfn_remove_bus(dev->bus, virtfn->bus);
380 /* balance pci_get_domain_bus_and_slot() */
385 static ssize_t sriov_totalvfs_show(struct device *dev,
386 struct device_attribute *attr,
389 struct pci_dev *pdev = to_pci_dev(dev);
391 return sysfs_emit(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
394 static ssize_t sriov_numvfs_show(struct device *dev,
395 struct device_attribute *attr,
398 struct pci_dev *pdev = to_pci_dev(dev);
401 /* Serialize vs sriov_numvfs_store() so readers see valid num_VFs */
402 device_lock(&pdev->dev);
403 num_vfs = pdev->sriov->num_VFs;
404 device_unlock(&pdev->dev);
406 return sysfs_emit(buf, "%u\n", num_vfs);
410 * num_vfs > 0; number of VFs to enable
411 * num_vfs = 0; disable all VFs
413 * Note: SRIOV spec does not allow partial VF
414 * disable, so it's all or none.
416 static ssize_t sriov_numvfs_store(struct device *dev,
417 struct device_attribute *attr,
418 const char *buf, size_t count)
420 struct pci_dev *pdev = to_pci_dev(dev);
424 if (kstrtou16(buf, 0, &num_vfs) < 0)
427 if (num_vfs > pci_sriov_get_totalvfs(pdev))
430 device_lock(&pdev->dev);
432 if (num_vfs == pdev->sriov->num_VFs)
435 /* is PF driver loaded */
437 pci_info(pdev, "no driver bound to device; cannot configure SR-IOV\n");
442 /* is PF driver loaded w/callback */
443 if (!pdev->driver->sriov_configure) {
444 pci_info(pdev, "driver does not support SR-IOV configuration via sysfs\n");
451 ret = pdev->driver->sriov_configure(pdev, 0);
456 if (pdev->sriov->num_VFs) {
457 pci_warn(pdev, "%d VFs already enabled. Disable before enabling %d VFs\n",
458 pdev->sriov->num_VFs, num_vfs);
463 ret = pdev->driver->sriov_configure(pdev, num_vfs);
468 pci_warn(pdev, "%d VFs requested; only %d enabled\n",
472 device_unlock(&pdev->dev);
480 static ssize_t sriov_offset_show(struct device *dev,
481 struct device_attribute *attr,
484 struct pci_dev *pdev = to_pci_dev(dev);
486 return sysfs_emit(buf, "%u\n", pdev->sriov->offset);
489 static ssize_t sriov_stride_show(struct device *dev,
490 struct device_attribute *attr,
493 struct pci_dev *pdev = to_pci_dev(dev);
495 return sysfs_emit(buf, "%u\n", pdev->sriov->stride);
498 static ssize_t sriov_vf_device_show(struct device *dev,
499 struct device_attribute *attr,
502 struct pci_dev *pdev = to_pci_dev(dev);
504 return sysfs_emit(buf, "%x\n", pdev->sriov->vf_device);
507 static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
508 struct device_attribute *attr,
511 struct pci_dev *pdev = to_pci_dev(dev);
513 return sysfs_emit(buf, "%u\n", pdev->sriov->drivers_autoprobe);
516 static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
517 struct device_attribute *attr,
518 const char *buf, size_t count)
520 struct pci_dev *pdev = to_pci_dev(dev);
521 bool drivers_autoprobe;
523 if (kstrtobool(buf, &drivers_autoprobe) < 0)
526 pdev->sriov->drivers_autoprobe = drivers_autoprobe;
531 static DEVICE_ATTR_RO(sriov_totalvfs);
532 static DEVICE_ATTR_RW(sriov_numvfs);
533 static DEVICE_ATTR_RO(sriov_offset);
534 static DEVICE_ATTR_RO(sriov_stride);
535 static DEVICE_ATTR_RO(sriov_vf_device);
536 static DEVICE_ATTR_RW(sriov_drivers_autoprobe);
538 static struct attribute *sriov_pf_dev_attrs[] = {
539 &dev_attr_sriov_totalvfs.attr,
540 &dev_attr_sriov_numvfs.attr,
541 &dev_attr_sriov_offset.attr,
542 &dev_attr_sriov_stride.attr,
543 &dev_attr_sriov_vf_device.attr,
544 &dev_attr_sriov_drivers_autoprobe.attr,
545 #ifdef CONFIG_PCI_MSI
546 &dev_attr_sriov_vf_total_msix.attr,
551 static umode_t sriov_pf_attrs_are_visible(struct kobject *kobj,
552 struct attribute *a, int n)
554 struct device *dev = kobj_to_dev(kobj);
562 const struct attribute_group sriov_pf_dev_attr_group = {
563 .attrs = sriov_pf_dev_attrs,
564 .is_visible = sriov_pf_attrs_are_visible,
567 int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
572 int __weak pcibios_sriov_disable(struct pci_dev *pdev)
577 static int sriov_add_vfs(struct pci_dev *dev, u16 num_vfs)
585 for (i = 0; i < num_vfs; i++) {
586 rc = pci_iov_add_virtfn(dev, i);
593 pci_iov_remove_virtfn(dev, i);
598 static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
604 struct resource *res;
605 struct pci_dev *pdev;
606 struct pci_sriov *iov = dev->sriov;
616 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
617 if (initial > iov->total_VFs ||
618 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
621 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
622 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
626 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
627 bars |= (1 << (i + PCI_IOV_RESOURCES));
628 res = &dev->resource[i + PCI_IOV_RESOURCES];
632 if (nres != iov->nres) {
633 pci_err(dev, "not enough MMIO resources for SR-IOV\n");
637 bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
638 if (bus > dev->bus->busn_res.end) {
639 pci_err(dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
640 nr_virtfn, bus, &dev->bus->busn_res);
644 if (pci_enable_resources(dev, bars)) {
645 pci_err(dev, "SR-IOV: IOV BARS not allocated\n");
649 if (iov->link != dev->devfn) {
650 pdev = pci_get_slot(dev->bus, iov->link);
654 if (!pdev->is_physfn) {
659 rc = sysfs_create_link(&dev->dev.kobj,
660 &pdev->dev.kobj, "dep_link");
666 iov->initial_VFs = initial;
667 if (nr_virtfn < initial)
670 rc = pcibios_sriov_enable(dev, initial);
672 pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc);
676 pci_iov_set_numvfs(dev, nr_virtfn);
677 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
678 pci_cfg_access_lock(dev);
679 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
681 pci_cfg_access_unlock(dev);
683 rc = sriov_add_vfs(dev, initial);
687 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
688 iov->num_VFs = nr_virtfn;
693 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
694 pci_cfg_access_lock(dev);
695 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
697 pci_cfg_access_unlock(dev);
699 pcibios_sriov_disable(dev);
701 if (iov->link != dev->devfn)
702 sysfs_remove_link(&dev->dev.kobj, "dep_link");
704 pci_iov_set_numvfs(dev, 0);
708 static void sriov_del_vfs(struct pci_dev *dev)
710 struct pci_sriov *iov = dev->sriov;
713 for (i = 0; i < iov->num_VFs; i++)
714 pci_iov_remove_virtfn(dev, i);
717 static void sriov_disable(struct pci_dev *dev)
719 struct pci_sriov *iov = dev->sriov;
725 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
726 pci_cfg_access_lock(dev);
727 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
729 pci_cfg_access_unlock(dev);
731 pcibios_sriov_disable(dev);
733 if (iov->link != dev->devfn)
734 sysfs_remove_link(&dev->dev.kobj, "dep_link");
737 pci_iov_set_numvfs(dev, 0);
740 static int sriov_init(struct pci_dev *dev, int pos)
747 struct pci_sriov *iov;
748 struct resource *res;
749 struct pci_dev *pdev;
751 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
752 if (ctrl & PCI_SRIOV_CTRL_VFE) {
753 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
758 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
763 if (pci_ari_enabled(dev->bus))
764 ctrl |= PCI_SRIOV_CTRL_ARI;
767 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
769 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
773 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
774 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
775 pgsz &= ~((1 << i) - 1);
780 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
782 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
787 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
788 res = &dev->resource[i + PCI_IOV_RESOURCES];
790 * If it is already FIXED, don't change it, something
791 * (perhaps EA or header fixups) wants it this way.
793 if (res->flags & IORESOURCE_PCI_FIXED)
794 bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
796 bar64 = __pci_read_base(dev, pci_bar_unknown, res,
797 pos + PCI_SRIOV_BAR + i * 4);
800 if (resource_size(res) & (PAGE_SIZE - 1)) {
804 iov->barsz[i] = resource_size(res);
805 res->end = res->start + resource_size(res) * total - 1;
806 pci_info(dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
815 iov->total_VFs = total;
816 iov->driver_max_VFs = total;
817 pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
820 iov->drivers_autoprobe = true;
821 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
822 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
823 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
824 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
827 iov->dev = pci_dev_get(pdev);
833 rc = compute_max_vf_buses(dev);
843 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
844 res = &dev->resource[i + PCI_IOV_RESOURCES];
852 static void sriov_release(struct pci_dev *dev)
854 BUG_ON(dev->sriov->num_VFs);
856 if (dev != dev->sriov->dev)
857 pci_dev_put(dev->sriov->dev);
863 static void sriov_restore_state(struct pci_dev *dev)
867 struct pci_sriov *iov = dev->sriov;
869 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
870 if (ctrl & PCI_SRIOV_CTRL_VFE)
874 * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
875 * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
877 ctrl &= ~PCI_SRIOV_CTRL_ARI;
878 ctrl |= iov->ctrl & PCI_SRIOV_CTRL_ARI;
879 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
881 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
882 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
884 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
885 pci_iov_set_numvfs(dev, iov->num_VFs);
886 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
887 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
892 * pci_iov_init - initialize the IOV capability
893 * @dev: the PCI device
895 * Returns 0 on success, or negative on failure.
897 int pci_iov_init(struct pci_dev *dev)
901 if (!pci_is_pcie(dev))
904 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
906 return sriov_init(dev, pos);
912 * pci_iov_release - release resources used by the IOV capability
913 * @dev: the PCI device
915 void pci_iov_release(struct pci_dev *dev)
922 * pci_iov_remove - clean up SR-IOV state after PF driver is detached
923 * @dev: the PCI device
925 void pci_iov_remove(struct pci_dev *dev)
927 struct pci_sriov *iov = dev->sriov;
932 iov->driver_max_VFs = iov->total_VFs;
934 pci_warn(dev, "driver left SR-IOV enabled after remove\n");
938 * pci_iov_update_resource - update a VF BAR
939 * @dev: the PCI device
940 * @resno: the resource number
942 * Update a VF BAR in the SR-IOV capability of a PF.
944 void pci_iov_update_resource(struct pci_dev *dev, int resno)
946 struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
947 struct resource *res = dev->resource + resno;
948 int vf_bar = resno - PCI_IOV_RESOURCES;
949 struct pci_bus_region region;
955 * The generic pci_restore_bars() path calls this for all devices,
956 * including VFs and non-SR-IOV devices. If this is not a PF, we
957 * have nothing to do.
962 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
963 if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) {
964 dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
970 * Ignore unimplemented BARs, unused resource slots for 64-bit
971 * BARs, and non-movable resources, e.g., those described via
972 * Enhanced Allocation.
977 if (res->flags & IORESOURCE_UNSET)
980 if (res->flags & IORESOURCE_PCI_FIXED)
983 pcibios_resource_to_bus(dev->bus, ®ion, res);
985 new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK;
987 reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar;
988 pci_write_config_dword(dev, reg, new);
989 if (res->flags & IORESOURCE_MEM_64) {
990 new = region.start >> 16 >> 16;
991 pci_write_config_dword(dev, reg + 4, new);
995 resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
998 return pci_iov_resource_size(dev, resno);
1002 * pci_sriov_resource_alignment - get resource alignment for VF BAR
1003 * @dev: the PCI device
1004 * @resno: the resource number
1006 * Returns the alignment of the VF BAR found in the SR-IOV capability.
1007 * This is not the same as the resource size which is defined as
1008 * the VF BAR size multiplied by the number of VFs. The alignment
1009 * is just the VF BAR size.
1011 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
1013 return pcibios_iov_resource_alignment(dev, resno);
1017 * pci_restore_iov_state - restore the state of the IOV capability
1018 * @dev: the PCI device
1020 void pci_restore_iov_state(struct pci_dev *dev)
1023 sriov_restore_state(dev);
1027 * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs
1028 * @dev: the PCI device
1029 * @auto_probe: set VF drivers auto probe flag
1031 void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe)
1034 dev->sriov->drivers_autoprobe = auto_probe;
1038 * pci_iov_bus_range - find bus range used by Virtual Function
1041 * Returns max number of buses (exclude current one) used by Virtual
1044 int pci_iov_bus_range(struct pci_bus *bus)
1047 struct pci_dev *dev;
1049 list_for_each_entry(dev, &bus->devices, bus_list) {
1050 if (!dev->is_physfn)
1052 if (dev->sriov->max_VF_buses > max)
1053 max = dev->sriov->max_VF_buses;
1056 return max ? max - bus->number : 0;
1060 * pci_enable_sriov - enable the SR-IOV capability
1061 * @dev: the PCI device
1062 * @nr_virtfn: number of virtual functions to enable
1064 * Returns 0 on success, or negative on failure.
1066 int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
1070 if (!dev->is_physfn)
1073 return sriov_enable(dev, nr_virtfn);
1075 EXPORT_SYMBOL_GPL(pci_enable_sriov);
1078 * pci_disable_sriov - disable the SR-IOV capability
1079 * @dev: the PCI device
1081 void pci_disable_sriov(struct pci_dev *dev)
1085 if (!dev->is_physfn)
1090 EXPORT_SYMBOL_GPL(pci_disable_sriov);
1093 * pci_num_vf - return number of VFs associated with a PF device_release_driver
1094 * @dev: the PCI device
1096 * Returns number of VFs, or 0 if SR-IOV is not enabled.
1098 int pci_num_vf(struct pci_dev *dev)
1100 if (!dev->is_physfn)
1103 return dev->sriov->num_VFs;
1105 EXPORT_SYMBOL_GPL(pci_num_vf);
1108 * pci_vfs_assigned - returns number of VFs are assigned to a guest
1109 * @dev: the PCI device
1111 * Returns number of VFs belonging to this device that are assigned to a guest.
1112 * If device is not a physical function returns 0.
1114 int pci_vfs_assigned(struct pci_dev *dev)
1116 struct pci_dev *vfdev;
1117 unsigned int vfs_assigned = 0;
1118 unsigned short dev_id;
1120 /* only search if we are a PF */
1121 if (!dev->is_physfn)
1125 * determine the device ID for the VFs, the vendor ID will be the
1126 * same as the PF so there is no need to check for that one
1128 dev_id = dev->sriov->vf_device;
1130 /* loop through all the VFs to see if we own any that are assigned */
1131 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
1134 * It is considered assigned if it is a virtual function with
1135 * our dev as the physical function and the assigned bit is set
1137 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
1138 pci_is_dev_assigned(vfdev))
1141 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
1144 return vfs_assigned;
1146 EXPORT_SYMBOL_GPL(pci_vfs_assigned);
1149 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
1150 * @dev: the PCI PF device
1151 * @numvfs: number that should be used for TotalVFs supported
1153 * Should be called from PF driver's probe routine with
1154 * device's mutex held.
1156 * Returns 0 if PF is an SRIOV-capable device and
1157 * value of numvfs valid. If not a PF return -ENOSYS;
1158 * if numvfs is invalid return -EINVAL;
1159 * if VFs already enabled, return -EBUSY.
1161 int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
1163 if (!dev->is_physfn)
1166 if (numvfs > dev->sriov->total_VFs)
1169 /* Shouldn't change if VFs already enabled */
1170 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
1173 dev->sriov->driver_max_VFs = numvfs;
1176 EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
1179 * pci_sriov_get_totalvfs -- get total VFs supported on this device
1180 * @dev: the PCI PF device
1182 * For a PCIe device with SRIOV support, return the PCIe
1183 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
1184 * if the driver reduced it. Otherwise 0.
1186 int pci_sriov_get_totalvfs(struct pci_dev *dev)
1188 if (!dev->is_physfn)
1191 return dev->sriov->driver_max_VFs;
1193 EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);
1196 * pci_sriov_configure_simple - helper to configure SR-IOV
1197 * @dev: the PCI device
1198 * @nr_virtfn: number of virtual functions to enable, 0 to disable
1200 * Enable or disable SR-IOV for devices that don't require any PF setup
1201 * before enabling SR-IOV. Return value is negative on error, or number of
1202 * VFs allocated on success.
1204 int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn)
1210 if (!dev->is_physfn)
1213 if (pci_vfs_assigned(dev)) {
1214 pci_warn(dev, "Cannot modify SR-IOV while VFs are assigned\n");
1218 if (nr_virtfn == 0) {
1223 rc = sriov_enable(dev, nr_virtfn);
1229 EXPORT_SYMBOL_GPL(pci_sriov_configure_simple);