1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
7 * PCI Express I/O Virtualization (IOV) support.
9 * Address Translation Service 1.0
12 #include <linux/pci.h>
13 #include <linux/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/export.h>
16 #include <linux/string.h>
17 #include <linux/delay.h>
18 #include <linux/pci-ats.h>
21 #define VIRTFN_ID_LEN 16
23 int pci_iov_virtfn_bus(struct pci_dev
*dev
, int vf_id
)
27 return dev
->bus
->number
+ ((dev
->devfn
+ dev
->sriov
->offset
+
28 dev
->sriov
->stride
* vf_id
) >> 8);
31 int pci_iov_virtfn_devfn(struct pci_dev
*dev
, int vf_id
)
35 return (dev
->devfn
+ dev
->sriov
->offset
+
36 dev
->sriov
->stride
* vf_id
) & 0xff;
40 * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
41 * change when NumVFs changes.
43 * Update iov->offset and iov->stride when NumVFs is written.
45 static inline void pci_iov_set_numvfs(struct pci_dev
*dev
, int nr_virtfn
)
47 struct pci_sriov
*iov
= dev
->sriov
;
49 pci_write_config_word(dev
, iov
->pos
+ PCI_SRIOV_NUM_VF
, nr_virtfn
);
50 pci_read_config_word(dev
, iov
->pos
+ PCI_SRIOV_VF_OFFSET
, &iov
->offset
);
51 pci_read_config_word(dev
, iov
->pos
+ PCI_SRIOV_VF_STRIDE
, &iov
->stride
);
55 * The PF consumes one bus number. NumVFs, First VF Offset, and VF Stride
56 * determine how many additional bus numbers will be consumed by VFs.
58 * Iterate over all valid NumVFs, validate offset and stride, and calculate
59 * the maximum number of bus numbers that could ever be required.
61 static int compute_max_vf_buses(struct pci_dev
*dev
)
63 struct pci_sriov
*iov
= dev
->sriov
;
64 int nr_virtfn
, busnr
, rc
= 0;
66 for (nr_virtfn
= iov
->total_VFs
; nr_virtfn
; nr_virtfn
--) {
67 pci_iov_set_numvfs(dev
, nr_virtfn
);
68 if (!iov
->offset
|| (nr_virtfn
> 1 && !iov
->stride
)) {
73 busnr
= pci_iov_virtfn_bus(dev
, nr_virtfn
- 1);
74 if (busnr
> iov
->max_VF_buses
)
75 iov
->max_VF_buses
= busnr
;
79 pci_iov_set_numvfs(dev
, 0);
83 static struct pci_bus
*virtfn_add_bus(struct pci_bus
*bus
, int busnr
)
85 struct pci_bus
*child
;
87 if (bus
->number
== busnr
)
90 child
= pci_find_bus(pci_domain_nr(bus
), busnr
);
94 child
= pci_add_new_bus(bus
, NULL
, busnr
);
98 pci_bus_insert_busn_res(child
, busnr
, busnr
);
103 static void virtfn_remove_bus(struct pci_bus
*physbus
, struct pci_bus
*virtbus
)
105 if (physbus
!= virtbus
&& list_empty(&virtbus
->devices
))
106 pci_remove_bus(virtbus
);
109 resource_size_t
pci_iov_resource_size(struct pci_dev
*dev
, int resno
)
114 return dev
->sriov
->barsz
[resno
- PCI_IOV_RESOURCES
];
117 int pci_iov_add_virtfn(struct pci_dev
*dev
, int id
)
122 char buf
[VIRTFN_ID_LEN
];
123 struct pci_dev
*virtfn
;
124 struct resource
*res
;
125 struct pci_sriov
*iov
= dev
->sriov
;
128 bus
= virtfn_add_bus(dev
->bus
, pci_iov_virtfn_bus(dev
, id
));
132 virtfn
= pci_alloc_dev(bus
);
136 virtfn
->devfn
= pci_iov_virtfn_devfn(dev
, id
);
137 virtfn
->vendor
= dev
->vendor
;
138 virtfn
->device
= iov
->vf_device
;
139 rc
= pci_setup_device(virtfn
);
143 virtfn
->dev
.parent
= dev
->dev
.parent
;
144 virtfn
->physfn
= pci_dev_get(dev
);
145 virtfn
->is_virtfn
= 1;
146 virtfn
->multifunction
= 0;
148 for (i
= 0; i
< PCI_SRIOV_NUM_BARS
; i
++) {
149 res
= &dev
->resource
[i
+ PCI_IOV_RESOURCES
];
152 virtfn
->resource
[i
].name
= pci_name(virtfn
);
153 virtfn
->resource
[i
].flags
= res
->flags
;
154 size
= pci_iov_resource_size(dev
, i
+ PCI_IOV_RESOURCES
);
155 virtfn
->resource
[i
].start
= res
->start
+ size
* id
;
156 virtfn
->resource
[i
].end
= virtfn
->resource
[i
].start
+ size
- 1;
157 rc
= request_resource(res
, &virtfn
->resource
[i
]);
161 pci_device_add(virtfn
, virtfn
->bus
);
163 sprintf(buf
, "virtfn%u", id
);
164 rc
= sysfs_create_link(&dev
->dev
.kobj
, &virtfn
->dev
.kobj
, buf
);
167 rc
= sysfs_create_link(&virtfn
->dev
.kobj
, &dev
->dev
.kobj
, "physfn");
171 kobject_uevent(&virtfn
->dev
.kobj
, KOBJ_CHANGE
);
173 pci_bus_add_device(virtfn
);
178 sysfs_remove_link(&dev
->dev
.kobj
, buf
);
181 pci_stop_and_remove_bus_device(virtfn
);
183 virtfn_remove_bus(dev
->bus
, bus
);
189 void pci_iov_remove_virtfn(struct pci_dev
*dev
, int id
)
191 char buf
[VIRTFN_ID_LEN
];
192 struct pci_dev
*virtfn
;
194 virtfn
= pci_get_domain_bus_and_slot(pci_domain_nr(dev
->bus
),
195 pci_iov_virtfn_bus(dev
, id
),
196 pci_iov_virtfn_devfn(dev
, id
));
200 sprintf(buf
, "virtfn%u", id
);
201 sysfs_remove_link(&dev
->dev
.kobj
, buf
);
203 * pci_stop_dev() could have been called for this virtfn already,
204 * so the directory for the virtfn may have been removed before.
205 * Double check to avoid spurious sysfs warnings.
207 if (virtfn
->dev
.kobj
.sd
)
208 sysfs_remove_link(&virtfn
->dev
.kobj
, "physfn");
210 pci_stop_and_remove_bus_device(virtfn
);
211 virtfn_remove_bus(dev
->bus
, virtfn
->bus
);
213 /* balance pci_get_domain_bus_and_slot() */
218 int __weak
pcibios_sriov_enable(struct pci_dev
*pdev
, u16 num_vfs
)
223 int __weak
pcibios_sriov_disable(struct pci_dev
*pdev
)
228 static int sriov_enable(struct pci_dev
*dev
, int nr_virtfn
)
234 struct resource
*res
;
235 struct pci_dev
*pdev
;
236 struct pci_sriov
*iov
= dev
->sriov
;
246 pci_read_config_word(dev
, iov
->pos
+ PCI_SRIOV_INITIAL_VF
, &initial
);
247 if (initial
> iov
->total_VFs
||
248 (!(iov
->cap
& PCI_SRIOV_CAP_VFM
) && (initial
!= iov
->total_VFs
)))
251 if (nr_virtfn
< 0 || nr_virtfn
> iov
->total_VFs
||
252 (!(iov
->cap
& PCI_SRIOV_CAP_VFM
) && (nr_virtfn
> initial
)))
256 for (i
= 0; i
< PCI_SRIOV_NUM_BARS
; i
++) {
257 bars
|= (1 << (i
+ PCI_IOV_RESOURCES
));
258 res
= &dev
->resource
[i
+ PCI_IOV_RESOURCES
];
262 if (nres
!= iov
->nres
) {
263 pci_err(dev
, "not enough MMIO resources for SR-IOV\n");
267 bus
= pci_iov_virtfn_bus(dev
, nr_virtfn
- 1);
268 if (bus
> dev
->bus
->busn_res
.end
) {
269 pci_err(dev
, "can't enable %d VFs (bus %02x out of range of %pR)\n",
270 nr_virtfn
, bus
, &dev
->bus
->busn_res
);
274 if (pci_enable_resources(dev
, bars
)) {
275 pci_err(dev
, "SR-IOV: IOV BARS not allocated\n");
279 if (iov
->link
!= dev
->devfn
) {
280 pdev
= pci_get_slot(dev
->bus
, iov
->link
);
284 if (!pdev
->is_physfn
) {
289 rc
= sysfs_create_link(&dev
->dev
.kobj
,
290 &pdev
->dev
.kobj
, "dep_link");
296 iov
->initial_VFs
= initial
;
297 if (nr_virtfn
< initial
)
300 rc
= pcibios_sriov_enable(dev
, initial
);
302 pci_err(dev
, "failure %d from pcibios_sriov_enable()\n", rc
);
306 pci_iov_set_numvfs(dev
, nr_virtfn
);
307 iov
->ctrl
|= PCI_SRIOV_CTRL_VFE
| PCI_SRIOV_CTRL_MSE
;
308 pci_cfg_access_lock(dev
);
309 pci_write_config_word(dev
, iov
->pos
+ PCI_SRIOV_CTRL
, iov
->ctrl
);
311 pci_cfg_access_unlock(dev
);
313 for (i
= 0; i
< initial
; i
++) {
314 rc
= pci_iov_add_virtfn(dev
, i
);
319 kobject_uevent(&dev
->dev
.kobj
, KOBJ_CHANGE
);
320 iov
->num_VFs
= nr_virtfn
;
326 pci_iov_remove_virtfn(dev
, i
);
329 iov
->ctrl
&= ~(PCI_SRIOV_CTRL_VFE
| PCI_SRIOV_CTRL_MSE
);
330 pci_cfg_access_lock(dev
);
331 pci_write_config_word(dev
, iov
->pos
+ PCI_SRIOV_CTRL
, iov
->ctrl
);
333 pci_cfg_access_unlock(dev
);
335 pcibios_sriov_disable(dev
);
337 if (iov
->link
!= dev
->devfn
)
338 sysfs_remove_link(&dev
->dev
.kobj
, "dep_link");
340 pci_iov_set_numvfs(dev
, 0);
344 static void sriov_disable(struct pci_dev
*dev
)
347 struct pci_sriov
*iov
= dev
->sriov
;
352 for (i
= 0; i
< iov
->num_VFs
; i
++)
353 pci_iov_remove_virtfn(dev
, i
);
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 pcibios_sriov_disable(dev
);
363 if (iov
->link
!= dev
->devfn
)
364 sysfs_remove_link(&dev
->dev
.kobj
, "dep_link");
367 pci_iov_set_numvfs(dev
, 0);
370 static int sriov_init(struct pci_dev
*dev
, int pos
)
377 struct pci_sriov
*iov
;
378 struct resource
*res
;
379 struct pci_dev
*pdev
;
381 pci_read_config_word(dev
, pos
+ PCI_SRIOV_CTRL
, &ctrl
);
382 if (ctrl
& PCI_SRIOV_CTRL_VFE
) {
383 pci_write_config_word(dev
, pos
+ PCI_SRIOV_CTRL
, 0);
388 list_for_each_entry(pdev
, &dev
->bus
->devices
, bus_list
)
393 if (pci_ari_enabled(dev
->bus
))
394 ctrl
|= PCI_SRIOV_CTRL_ARI
;
397 pci_write_config_word(dev
, pos
+ PCI_SRIOV_CTRL
, ctrl
);
399 pci_read_config_word(dev
, pos
+ PCI_SRIOV_TOTAL_VF
, &total
);
403 pci_read_config_dword(dev
, pos
+ PCI_SRIOV_SUP_PGSIZE
, &pgsz
);
404 i
= PAGE_SHIFT
> 12 ? PAGE_SHIFT
- 12 : 0;
405 pgsz
&= ~((1 << i
) - 1);
410 pci_write_config_dword(dev
, pos
+ PCI_SRIOV_SYS_PGSIZE
, pgsz
);
412 iov
= kzalloc(sizeof(*iov
), GFP_KERNEL
);
417 for (i
= 0; i
< PCI_SRIOV_NUM_BARS
; i
++) {
418 res
= &dev
->resource
[i
+ PCI_IOV_RESOURCES
];
420 * If it is already FIXED, don't change it, something
421 * (perhaps EA or header fixups) wants it this way.
423 if (res
->flags
& IORESOURCE_PCI_FIXED
)
424 bar64
= (res
->flags
& IORESOURCE_MEM_64
) ? 1 : 0;
426 bar64
= __pci_read_base(dev
, pci_bar_unknown
, res
,
427 pos
+ PCI_SRIOV_BAR
+ i
* 4);
430 if (resource_size(res
) & (PAGE_SIZE
- 1)) {
434 iov
->barsz
[i
] = resource_size(res
);
435 res
->end
= res
->start
+ resource_size(res
) * total
- 1;
436 pci_info(dev
, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
445 iov
->total_VFs
= total
;
446 pci_read_config_word(dev
, pos
+ PCI_SRIOV_VF_DID
, &iov
->vf_device
);
449 iov
->drivers_autoprobe
= true;
450 pci_read_config_dword(dev
, pos
+ PCI_SRIOV_CAP
, &iov
->cap
);
451 pci_read_config_byte(dev
, pos
+ PCI_SRIOV_FUNC_LINK
, &iov
->link
);
452 if (pci_pcie_type(dev
) == PCI_EXP_TYPE_RC_END
)
453 iov
->link
= PCI_DEVFN(PCI_SLOT(dev
->devfn
), iov
->link
);
456 iov
->dev
= pci_dev_get(pdev
);
462 rc
= compute_max_vf_buses(dev
);
472 for (i
= 0; i
< PCI_SRIOV_NUM_BARS
; i
++) {
473 res
= &dev
->resource
[i
+ PCI_IOV_RESOURCES
];
481 static void sriov_release(struct pci_dev
*dev
)
483 BUG_ON(dev
->sriov
->num_VFs
);
485 if (dev
!= dev
->sriov
->dev
)
486 pci_dev_put(dev
->sriov
->dev
);
492 static void sriov_restore_state(struct pci_dev
*dev
)
496 struct pci_sriov
*iov
= dev
->sriov
;
498 pci_read_config_word(dev
, iov
->pos
+ PCI_SRIOV_CTRL
, &ctrl
);
499 if (ctrl
& PCI_SRIOV_CTRL_VFE
)
503 * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
504 * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
506 ctrl
&= ~PCI_SRIOV_CTRL_ARI
;
507 ctrl
|= iov
->ctrl
& PCI_SRIOV_CTRL_ARI
;
508 pci_write_config_word(dev
, iov
->pos
+ PCI_SRIOV_CTRL
, ctrl
);
510 for (i
= PCI_IOV_RESOURCES
; i
<= PCI_IOV_RESOURCE_END
; i
++)
511 pci_update_resource(dev
, i
);
513 pci_write_config_dword(dev
, iov
->pos
+ PCI_SRIOV_SYS_PGSIZE
, iov
->pgsz
);
514 pci_iov_set_numvfs(dev
, iov
->num_VFs
);
515 pci_write_config_word(dev
, iov
->pos
+ PCI_SRIOV_CTRL
, iov
->ctrl
);
516 if (iov
->ctrl
& PCI_SRIOV_CTRL_VFE
)
521 * pci_iov_init - initialize the IOV capability
522 * @dev: the PCI device
524 * Returns 0 on success, or negative on failure.
526 int pci_iov_init(struct pci_dev
*dev
)
530 if (!pci_is_pcie(dev
))
533 pos
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_SRIOV
);
535 return sriov_init(dev
, pos
);
541 * pci_iov_release - release resources used by the IOV capability
542 * @dev: the PCI device
544 void pci_iov_release(struct pci_dev
*dev
)
551 * pci_iov_update_resource - update a VF BAR
552 * @dev: the PCI device
553 * @resno: the resource number
555 * Update a VF BAR in the SR-IOV capability of a PF.
557 void pci_iov_update_resource(struct pci_dev
*dev
, int resno
)
559 struct pci_sriov
*iov
= dev
->is_physfn
? dev
->sriov
: NULL
;
560 struct resource
*res
= dev
->resource
+ resno
;
561 int vf_bar
= resno
- PCI_IOV_RESOURCES
;
562 struct pci_bus_region region
;
568 * The generic pci_restore_bars() path calls this for all devices,
569 * including VFs and non-SR-IOV devices. If this is not a PF, we
570 * have nothing to do.
575 pci_read_config_word(dev
, iov
->pos
+ PCI_SRIOV_CTRL
, &cmd
);
576 if ((cmd
& PCI_SRIOV_CTRL_VFE
) && (cmd
& PCI_SRIOV_CTRL_MSE
)) {
577 dev_WARN(&dev
->dev
, "can't update enabled VF BAR%d %pR\n",
583 * Ignore unimplemented BARs, unused resource slots for 64-bit
584 * BARs, and non-movable resources, e.g., those described via
585 * Enhanced Allocation.
590 if (res
->flags
& IORESOURCE_UNSET
)
593 if (res
->flags
& IORESOURCE_PCI_FIXED
)
596 pcibios_resource_to_bus(dev
->bus
, ®ion
, res
);
598 new |= res
->flags
& ~PCI_BASE_ADDRESS_MEM_MASK
;
600 reg
= iov
->pos
+ PCI_SRIOV_BAR
+ 4 * vf_bar
;
601 pci_write_config_dword(dev
, reg
, new);
602 if (res
->flags
& IORESOURCE_MEM_64
) {
603 new = region
.start
>> 16 >> 16;
604 pci_write_config_dword(dev
, reg
+ 4, new);
608 resource_size_t __weak
pcibios_iov_resource_alignment(struct pci_dev
*dev
,
611 return pci_iov_resource_size(dev
, resno
);
615 * pci_sriov_resource_alignment - get resource alignment for VF BAR
616 * @dev: the PCI device
617 * @resno: the resource number
619 * Returns the alignment of the VF BAR found in the SR-IOV capability.
620 * This is not the same as the resource size which is defined as
621 * the VF BAR size multiplied by the number of VFs. The alignment
622 * is just the VF BAR size.
624 resource_size_t
pci_sriov_resource_alignment(struct pci_dev
*dev
, int resno
)
626 return pcibios_iov_resource_alignment(dev
, resno
);
630 * pci_restore_iov_state - restore the state of the IOV capability
631 * @dev: the PCI device
633 void pci_restore_iov_state(struct pci_dev
*dev
)
636 sriov_restore_state(dev
);
640 * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs
641 * @dev: the PCI device
642 * @auto_probe: set VF drivers auto probe flag
644 void pci_vf_drivers_autoprobe(struct pci_dev
*dev
, bool auto_probe
)
647 dev
->sriov
->drivers_autoprobe
= auto_probe
;
651 * pci_iov_bus_range - find bus range used by Virtual Function
654 * Returns max number of buses (exclude current one) used by Virtual
657 int pci_iov_bus_range(struct pci_bus
*bus
)
662 list_for_each_entry(dev
, &bus
->devices
, bus_list
) {
665 if (dev
->sriov
->max_VF_buses
> max
)
666 max
= dev
->sriov
->max_VF_buses
;
669 return max
? max
- bus
->number
: 0;
673 * pci_enable_sriov - enable the SR-IOV capability
674 * @dev: the PCI device
675 * @nr_virtfn: number of virtual functions to enable
677 * Returns 0 on success, or negative on failure.
679 int pci_enable_sriov(struct pci_dev
*dev
, int nr_virtfn
)
686 return sriov_enable(dev
, nr_virtfn
);
688 EXPORT_SYMBOL_GPL(pci_enable_sriov
);
691 * pci_disable_sriov - disable the SR-IOV capability
692 * @dev: the PCI device
694 void pci_disable_sriov(struct pci_dev
*dev
)
703 EXPORT_SYMBOL_GPL(pci_disable_sriov
);
706 * pci_num_vf - return number of VFs associated with a PF device_release_driver
707 * @dev: the PCI device
709 * Returns number of VFs, or 0 if SR-IOV is not enabled.
711 int pci_num_vf(struct pci_dev
*dev
)
716 return dev
->sriov
->num_VFs
;
718 EXPORT_SYMBOL_GPL(pci_num_vf
);
721 * pci_vfs_assigned - returns number of VFs are assigned to a guest
722 * @dev: the PCI device
724 * Returns number of VFs belonging to this device that are assigned to a guest.
725 * If device is not a physical function returns 0.
727 int pci_vfs_assigned(struct pci_dev
*dev
)
729 struct pci_dev
*vfdev
;
730 unsigned int vfs_assigned
= 0;
731 unsigned short dev_id
;
733 /* only search if we are a PF */
738 * determine the device ID for the VFs, the vendor ID will be the
739 * same as the PF so there is no need to check for that one
741 dev_id
= dev
->sriov
->vf_device
;
743 /* loop through all the VFs to see if we own any that are assigned */
744 vfdev
= pci_get_device(dev
->vendor
, dev_id
, NULL
);
747 * It is considered assigned if it is a virtual function with
748 * our dev as the physical function and the assigned bit is set
750 if (vfdev
->is_virtfn
&& (vfdev
->physfn
== dev
) &&
751 pci_is_dev_assigned(vfdev
))
754 vfdev
= pci_get_device(dev
->vendor
, dev_id
, vfdev
);
759 EXPORT_SYMBOL_GPL(pci_vfs_assigned
);
762 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
763 * @dev: the PCI PF device
764 * @numvfs: number that should be used for TotalVFs supported
766 * Should be called from PF driver's probe routine with
767 * device's mutex held.
769 * Returns 0 if PF is an SRIOV-capable device and
770 * value of numvfs valid. If not a PF return -ENOSYS;
771 * if numvfs is invalid return -EINVAL;
772 * if VFs already enabled, return -EBUSY.
774 int pci_sriov_set_totalvfs(struct pci_dev
*dev
, u16 numvfs
)
778 if (numvfs
> dev
->sriov
->total_VFs
)
781 /* Shouldn't change if VFs already enabled */
782 if (dev
->sriov
->ctrl
& PCI_SRIOV_CTRL_VFE
)
785 dev
->sriov
->driver_max_VFs
= numvfs
;
789 EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs
);
792 * pci_sriov_get_totalvfs -- get total VFs supported on this device
793 * @dev: the PCI PF device
795 * For a PCIe device with SRIOV support, return the PCIe
796 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
797 * if the driver reduced it. Otherwise 0.
799 int pci_sriov_get_totalvfs(struct pci_dev
*dev
)
804 if (dev
->sriov
->driver_max_VFs
)
805 return dev
->sriov
->driver_max_VFs
;
807 return dev
->sriov
->total_VFs
;
809 EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs
);