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
);
136 physfn
->sriov
->cfg_size
= pci_cfg_space_size(virtfn
);
139 int pci_iov_add_virtfn(struct pci_dev
*dev
, int id
)
144 char buf
[VIRTFN_ID_LEN
];
145 struct pci_dev
*virtfn
;
146 struct resource
*res
;
147 struct pci_sriov
*iov
= dev
->sriov
;
150 bus
= virtfn_add_bus(dev
->bus
, pci_iov_virtfn_bus(dev
, id
));
154 virtfn
= pci_alloc_dev(bus
);
158 virtfn
->devfn
= pci_iov_virtfn_devfn(dev
, id
);
159 virtfn
->vendor
= dev
->vendor
;
160 virtfn
->device
= iov
->vf_device
;
161 virtfn
->is_virtfn
= 1;
162 virtfn
->physfn
= pci_dev_get(dev
);
165 pci_read_vf_config_common(virtfn
);
167 rc
= pci_setup_device(virtfn
);
171 virtfn
->dev
.parent
= dev
->dev
.parent
;
172 virtfn
->multifunction
= 0;
174 for (i
= 0; i
< PCI_SRIOV_NUM_BARS
; i
++) {
175 res
= &dev
->resource
[i
+ PCI_IOV_RESOURCES
];
178 virtfn
->resource
[i
].name
= pci_name(virtfn
);
179 virtfn
->resource
[i
].flags
= res
->flags
;
180 size
= pci_iov_resource_size(dev
, i
+ PCI_IOV_RESOURCES
);
181 virtfn
->resource
[i
].start
= res
->start
+ size
* id
;
182 virtfn
->resource
[i
].end
= virtfn
->resource
[i
].start
+ size
- 1;
183 rc
= request_resource(res
, &virtfn
->resource
[i
]);
187 pci_device_add(virtfn
, virtfn
->bus
);
189 sprintf(buf
, "virtfn%u", id
);
190 rc
= sysfs_create_link(&dev
->dev
.kobj
, &virtfn
->dev
.kobj
, buf
);
193 rc
= sysfs_create_link(&virtfn
->dev
.kobj
, &dev
->dev
.kobj
, "physfn");
197 kobject_uevent(&virtfn
->dev
.kobj
, KOBJ_CHANGE
);
199 pci_bus_add_device(virtfn
);
204 sysfs_remove_link(&dev
->dev
.kobj
, buf
);
206 pci_stop_and_remove_bus_device(virtfn
);
210 virtfn_remove_bus(dev
->bus
, bus
);
216 void pci_iov_remove_virtfn(struct pci_dev
*dev
, int id
)
218 char buf
[VIRTFN_ID_LEN
];
219 struct pci_dev
*virtfn
;
221 virtfn
= pci_get_domain_bus_and_slot(pci_domain_nr(dev
->bus
),
222 pci_iov_virtfn_bus(dev
, id
),
223 pci_iov_virtfn_devfn(dev
, id
));
227 sprintf(buf
, "virtfn%u", id
);
228 sysfs_remove_link(&dev
->dev
.kobj
, buf
);
230 * pci_stop_dev() could have been called for this virtfn already,
231 * so the directory for the virtfn may have been removed before.
232 * Double check to avoid spurious sysfs warnings.
234 if (virtfn
->dev
.kobj
.sd
)
235 sysfs_remove_link(&virtfn
->dev
.kobj
, "physfn");
237 pci_stop_and_remove_bus_device(virtfn
);
238 virtfn_remove_bus(dev
->bus
, virtfn
->bus
);
240 /* balance pci_get_domain_bus_and_slot() */
245 int __weak
pcibios_sriov_enable(struct pci_dev
*pdev
, u16 num_vfs
)
250 int __weak
pcibios_sriov_disable(struct pci_dev
*pdev
)
255 static int sriov_add_vfs(struct pci_dev
*dev
, u16 num_vfs
)
263 for (i
= 0; i
< num_vfs
; i
++) {
264 rc
= pci_iov_add_virtfn(dev
, i
);
271 pci_iov_remove_virtfn(dev
, i
);
276 static int sriov_enable(struct pci_dev
*dev
, int nr_virtfn
)
282 struct resource
*res
;
283 struct pci_dev
*pdev
;
284 struct pci_sriov
*iov
= dev
->sriov
;
294 pci_read_config_word(dev
, iov
->pos
+ PCI_SRIOV_INITIAL_VF
, &initial
);
295 if (initial
> iov
->total_VFs
||
296 (!(iov
->cap
& PCI_SRIOV_CAP_VFM
) && (initial
!= iov
->total_VFs
)))
299 if (nr_virtfn
< 0 || nr_virtfn
> iov
->total_VFs
||
300 (!(iov
->cap
& PCI_SRIOV_CAP_VFM
) && (nr_virtfn
> initial
)))
304 for (i
= 0; i
< PCI_SRIOV_NUM_BARS
; i
++) {
305 bars
|= (1 << (i
+ PCI_IOV_RESOURCES
));
306 res
= &dev
->resource
[i
+ PCI_IOV_RESOURCES
];
310 if (nres
!= iov
->nres
) {
311 pci_err(dev
, "not enough MMIO resources for SR-IOV\n");
315 bus
= pci_iov_virtfn_bus(dev
, nr_virtfn
- 1);
316 if (bus
> dev
->bus
->busn_res
.end
) {
317 pci_err(dev
, "can't enable %d VFs (bus %02x out of range of %pR)\n",
318 nr_virtfn
, bus
, &dev
->bus
->busn_res
);
322 if (pci_enable_resources(dev
, bars
)) {
323 pci_err(dev
, "SR-IOV: IOV BARS not allocated\n");
327 if (iov
->link
!= dev
->devfn
) {
328 pdev
= pci_get_slot(dev
->bus
, iov
->link
);
332 if (!pdev
->is_physfn
) {
337 rc
= sysfs_create_link(&dev
->dev
.kobj
,
338 &pdev
->dev
.kobj
, "dep_link");
344 iov
->initial_VFs
= initial
;
345 if (nr_virtfn
< initial
)
348 rc
= pcibios_sriov_enable(dev
, initial
);
350 pci_err(dev
, "failure %d from pcibios_sriov_enable()\n", rc
);
354 pci_iov_set_numvfs(dev
, nr_virtfn
);
355 iov
->ctrl
|= PCI_SRIOV_CTRL_VFE
| PCI_SRIOV_CTRL_MSE
;
356 pci_cfg_access_lock(dev
);
357 pci_write_config_word(dev
, iov
->pos
+ PCI_SRIOV_CTRL
, iov
->ctrl
);
359 pci_cfg_access_unlock(dev
);
361 rc
= sriov_add_vfs(dev
, initial
);
365 kobject_uevent(&dev
->dev
.kobj
, KOBJ_CHANGE
);
366 iov
->num_VFs
= nr_virtfn
;
371 iov
->ctrl
&= ~(PCI_SRIOV_CTRL_VFE
| PCI_SRIOV_CTRL_MSE
);
372 pci_cfg_access_lock(dev
);
373 pci_write_config_word(dev
, iov
->pos
+ PCI_SRIOV_CTRL
, iov
->ctrl
);
375 pci_cfg_access_unlock(dev
);
377 pcibios_sriov_disable(dev
);
379 if (iov
->link
!= dev
->devfn
)
380 sysfs_remove_link(&dev
->dev
.kobj
, "dep_link");
382 pci_iov_set_numvfs(dev
, 0);
386 static void sriov_del_vfs(struct pci_dev
*dev
)
388 struct pci_sriov
*iov
= dev
->sriov
;
394 for (i
= 0; i
< iov
->num_VFs
; i
++)
395 pci_iov_remove_virtfn(dev
, i
);
398 static void sriov_disable(struct pci_dev
*dev
)
400 struct pci_sriov
*iov
= dev
->sriov
;
406 iov
->ctrl
&= ~(PCI_SRIOV_CTRL_VFE
| PCI_SRIOV_CTRL_MSE
);
407 pci_cfg_access_lock(dev
);
408 pci_write_config_word(dev
, iov
->pos
+ PCI_SRIOV_CTRL
, iov
->ctrl
);
410 pci_cfg_access_unlock(dev
);
412 pcibios_sriov_disable(dev
);
414 if (iov
->link
!= dev
->devfn
)
415 sysfs_remove_link(&dev
->dev
.kobj
, "dep_link");
418 pci_iov_set_numvfs(dev
, 0);
421 static int sriov_init(struct pci_dev
*dev
, int pos
)
428 struct pci_sriov
*iov
;
429 struct resource
*res
;
430 struct pci_dev
*pdev
;
432 pci_read_config_word(dev
, pos
+ PCI_SRIOV_CTRL
, &ctrl
);
433 if (ctrl
& PCI_SRIOV_CTRL_VFE
) {
434 pci_write_config_word(dev
, pos
+ PCI_SRIOV_CTRL
, 0);
439 list_for_each_entry(pdev
, &dev
->bus
->devices
, bus_list
)
444 if (pci_ari_enabled(dev
->bus
))
445 ctrl
|= PCI_SRIOV_CTRL_ARI
;
448 pci_write_config_word(dev
, pos
+ PCI_SRIOV_CTRL
, ctrl
);
450 pci_read_config_word(dev
, pos
+ PCI_SRIOV_TOTAL_VF
, &total
);
454 pci_read_config_dword(dev
, pos
+ PCI_SRIOV_SUP_PGSIZE
, &pgsz
);
455 i
= PAGE_SHIFT
> 12 ? PAGE_SHIFT
- 12 : 0;
456 pgsz
&= ~((1 << i
) - 1);
461 pci_write_config_dword(dev
, pos
+ PCI_SRIOV_SYS_PGSIZE
, pgsz
);
463 iov
= kzalloc(sizeof(*iov
), GFP_KERNEL
);
468 for (i
= 0; i
< PCI_SRIOV_NUM_BARS
; i
++) {
469 res
= &dev
->resource
[i
+ PCI_IOV_RESOURCES
];
471 * If it is already FIXED, don't change it, something
472 * (perhaps EA or header fixups) wants it this way.
474 if (res
->flags
& IORESOURCE_PCI_FIXED
)
475 bar64
= (res
->flags
& IORESOURCE_MEM_64
) ? 1 : 0;
477 bar64
= __pci_read_base(dev
, pci_bar_unknown
, res
,
478 pos
+ PCI_SRIOV_BAR
+ i
* 4);
481 if (resource_size(res
) & (PAGE_SIZE
- 1)) {
485 iov
->barsz
[i
] = resource_size(res
);
486 res
->end
= res
->start
+ resource_size(res
) * total
- 1;
487 pci_info(dev
, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
496 iov
->total_VFs
= total
;
497 iov
->driver_max_VFs
= total
;
498 pci_read_config_word(dev
, pos
+ PCI_SRIOV_VF_DID
, &iov
->vf_device
);
501 iov
->drivers_autoprobe
= true;
502 pci_read_config_dword(dev
, pos
+ PCI_SRIOV_CAP
, &iov
->cap
);
503 pci_read_config_byte(dev
, pos
+ PCI_SRIOV_FUNC_LINK
, &iov
->link
);
504 if (pci_pcie_type(dev
) == PCI_EXP_TYPE_RC_END
)
505 iov
->link
= PCI_DEVFN(PCI_SLOT(dev
->devfn
), iov
->link
);
508 iov
->dev
= pci_dev_get(pdev
);
514 rc
= compute_max_vf_buses(dev
);
524 for (i
= 0; i
< PCI_SRIOV_NUM_BARS
; i
++) {
525 res
= &dev
->resource
[i
+ PCI_IOV_RESOURCES
];
533 static void sriov_release(struct pci_dev
*dev
)
535 BUG_ON(dev
->sriov
->num_VFs
);
537 if (dev
!= dev
->sriov
->dev
)
538 pci_dev_put(dev
->sriov
->dev
);
544 static void sriov_restore_state(struct pci_dev
*dev
)
548 struct pci_sriov
*iov
= dev
->sriov
;
550 pci_read_config_word(dev
, iov
->pos
+ PCI_SRIOV_CTRL
, &ctrl
);
551 if (ctrl
& PCI_SRIOV_CTRL_VFE
)
555 * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
556 * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
558 ctrl
&= ~PCI_SRIOV_CTRL_ARI
;
559 ctrl
|= iov
->ctrl
& PCI_SRIOV_CTRL_ARI
;
560 pci_write_config_word(dev
, iov
->pos
+ PCI_SRIOV_CTRL
, ctrl
);
562 for (i
= PCI_IOV_RESOURCES
; i
<= PCI_IOV_RESOURCE_END
; i
++)
563 pci_update_resource(dev
, i
);
565 pci_write_config_dword(dev
, iov
->pos
+ PCI_SRIOV_SYS_PGSIZE
, iov
->pgsz
);
566 pci_iov_set_numvfs(dev
, iov
->num_VFs
);
567 pci_write_config_word(dev
, iov
->pos
+ PCI_SRIOV_CTRL
, iov
->ctrl
);
568 if (iov
->ctrl
& PCI_SRIOV_CTRL_VFE
)
573 * pci_iov_init - initialize the IOV capability
574 * @dev: the PCI device
576 * Returns 0 on success, or negative on failure.
578 int pci_iov_init(struct pci_dev
*dev
)
582 if (!pci_is_pcie(dev
))
585 pos
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_SRIOV
);
587 return sriov_init(dev
, pos
);
593 * pci_iov_release - release resources used by the IOV capability
594 * @dev: the PCI device
596 void pci_iov_release(struct pci_dev
*dev
)
603 * pci_iov_remove - clean up SR-IOV state after PF driver is detached
604 * @dev: the PCI device
606 void pci_iov_remove(struct pci_dev
*dev
)
608 struct pci_sriov
*iov
= dev
->sriov
;
613 iov
->driver_max_VFs
= iov
->total_VFs
;
615 pci_warn(dev
, "driver left SR-IOV enabled after remove\n");
619 * pci_iov_update_resource - update a VF BAR
620 * @dev: the PCI device
621 * @resno: the resource number
623 * Update a VF BAR in the SR-IOV capability of a PF.
625 void pci_iov_update_resource(struct pci_dev
*dev
, int resno
)
627 struct pci_sriov
*iov
= dev
->is_physfn
? dev
->sriov
: NULL
;
628 struct resource
*res
= dev
->resource
+ resno
;
629 int vf_bar
= resno
- PCI_IOV_RESOURCES
;
630 struct pci_bus_region region
;
636 * The generic pci_restore_bars() path calls this for all devices,
637 * including VFs and non-SR-IOV devices. If this is not a PF, we
638 * have nothing to do.
643 pci_read_config_word(dev
, iov
->pos
+ PCI_SRIOV_CTRL
, &cmd
);
644 if ((cmd
& PCI_SRIOV_CTRL_VFE
) && (cmd
& PCI_SRIOV_CTRL_MSE
)) {
645 dev_WARN(&dev
->dev
, "can't update enabled VF BAR%d %pR\n",
651 * Ignore unimplemented BARs, unused resource slots for 64-bit
652 * BARs, and non-movable resources, e.g., those described via
653 * Enhanced Allocation.
658 if (res
->flags
& IORESOURCE_UNSET
)
661 if (res
->flags
& IORESOURCE_PCI_FIXED
)
664 pcibios_resource_to_bus(dev
->bus
, ®ion
, res
);
666 new |= res
->flags
& ~PCI_BASE_ADDRESS_MEM_MASK
;
668 reg
= iov
->pos
+ PCI_SRIOV_BAR
+ 4 * vf_bar
;
669 pci_write_config_dword(dev
, reg
, new);
670 if (res
->flags
& IORESOURCE_MEM_64
) {
671 new = region
.start
>> 16 >> 16;
672 pci_write_config_dword(dev
, reg
+ 4, new);
676 resource_size_t __weak
pcibios_iov_resource_alignment(struct pci_dev
*dev
,
679 return pci_iov_resource_size(dev
, resno
);
683 * pci_sriov_resource_alignment - get resource alignment for VF BAR
684 * @dev: the PCI device
685 * @resno: the resource number
687 * Returns the alignment of the VF BAR found in the SR-IOV capability.
688 * This is not the same as the resource size which is defined as
689 * the VF BAR size multiplied by the number of VFs. The alignment
690 * is just the VF BAR size.
692 resource_size_t
pci_sriov_resource_alignment(struct pci_dev
*dev
, int resno
)
694 return pcibios_iov_resource_alignment(dev
, resno
);
698 * pci_restore_iov_state - restore the state of the IOV capability
699 * @dev: the PCI device
701 void pci_restore_iov_state(struct pci_dev
*dev
)
704 sriov_restore_state(dev
);
708 * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs
709 * @dev: the PCI device
710 * @auto_probe: set VF drivers auto probe flag
712 void pci_vf_drivers_autoprobe(struct pci_dev
*dev
, bool auto_probe
)
715 dev
->sriov
->drivers_autoprobe
= auto_probe
;
719 * pci_iov_bus_range - find bus range used by Virtual Function
722 * Returns max number of buses (exclude current one) used by Virtual
725 int pci_iov_bus_range(struct pci_bus
*bus
)
730 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
733 if (dev
->sriov
->max_VF_buses
> max
)
734 max
= dev
->sriov
->max_VF_buses
;
737 return max
? max
- bus
->number
: 0;
741 * pci_enable_sriov - enable the SR-IOV capability
742 * @dev: the PCI device
743 * @nr_virtfn: number of virtual functions to enable
745 * Returns 0 on success, or negative on failure.
747 int pci_enable_sriov(struct pci_dev
*dev
, int nr_virtfn
)
754 return sriov_enable(dev
, nr_virtfn
);
756 EXPORT_SYMBOL_GPL(pci_enable_sriov
);
759 * pci_disable_sriov - disable the SR-IOV capability
760 * @dev: the PCI device
762 void pci_disable_sriov(struct pci_dev
*dev
)
771 EXPORT_SYMBOL_GPL(pci_disable_sriov
);
774 * pci_num_vf - return number of VFs associated with a PF device_release_driver
775 * @dev: the PCI device
777 * Returns number of VFs, or 0 if SR-IOV is not enabled.
779 int pci_num_vf(struct pci_dev
*dev
)
784 return dev
->sriov
->num_VFs
;
786 EXPORT_SYMBOL_GPL(pci_num_vf
);
789 * pci_vfs_assigned - returns number of VFs are assigned to a guest
790 * @dev: the PCI device
792 * Returns number of VFs belonging to this device that are assigned to a guest.
793 * If device is not a physical function returns 0.
795 int pci_vfs_assigned(struct pci_dev
*dev
)
797 struct pci_dev
*vfdev
;
798 unsigned int vfs_assigned
= 0;
799 unsigned short dev_id
;
801 /* only search if we are a PF */
806 * determine the device ID for the VFs, the vendor ID will be the
807 * same as the PF so there is no need to check for that one
809 dev_id
= dev
->sriov
->vf_device
;
811 /* loop through all the VFs to see if we own any that are assigned */
812 vfdev
= pci_get_device(dev
->vendor
, dev_id
, NULL
);
815 * It is considered assigned if it is a virtual function with
816 * our dev as the physical function and the assigned bit is set
818 if (vfdev
->is_virtfn
&& (vfdev
->physfn
== dev
) &&
819 pci_is_dev_assigned(vfdev
))
822 vfdev
= pci_get_device(dev
->vendor
, dev_id
, vfdev
);
827 EXPORT_SYMBOL_GPL(pci_vfs_assigned
);
830 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
831 * @dev: the PCI PF device
832 * @numvfs: number that should be used for TotalVFs supported
834 * Should be called from PF driver's probe routine with
835 * device's mutex held.
837 * Returns 0 if PF is an SRIOV-capable device and
838 * value of numvfs valid. If not a PF return -ENOSYS;
839 * if numvfs is invalid return -EINVAL;
840 * if VFs already enabled, return -EBUSY.
842 int pci_sriov_set_totalvfs(struct pci_dev
*dev
, u16 numvfs
)
847 if (numvfs
> dev
->sriov
->total_VFs
)
850 /* Shouldn't change if VFs already enabled */
851 if (dev
->sriov
->ctrl
& PCI_SRIOV_CTRL_VFE
)
854 dev
->sriov
->driver_max_VFs
= numvfs
;
857 EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs
);
860 * pci_sriov_get_totalvfs -- get total VFs supported on this device
861 * @dev: the PCI PF device
863 * For a PCIe device with SRIOV support, return the PCIe
864 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
865 * if the driver reduced it. Otherwise 0.
867 int pci_sriov_get_totalvfs(struct pci_dev
*dev
)
872 return dev
->sriov
->driver_max_VFs
;
874 EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs
);
877 * pci_sriov_configure_simple - helper to configure SR-IOV
878 * @dev: the PCI device
879 * @nr_virtfn: number of virtual functions to enable, 0 to disable
881 * Enable or disable SR-IOV for devices that don't require any PF setup
882 * before enabling SR-IOV. Return value is negative on error, or number of
883 * VFs allocated on success.
885 int pci_sriov_configure_simple(struct pci_dev
*dev
, int nr_virtfn
)
894 if (pci_vfs_assigned(dev
)) {
895 pci_warn(dev
, "Cannot modify SR-IOV while VFs are assigned\n");
899 if (nr_virtfn
== 0) {
904 rc
= sriov_enable(dev
, nr_virtfn
);
910 EXPORT_SYMBOL_GPL(pci_sriov_configure_simple
);