blob: b9fd003206f1f2c3715b206d6ad334b0092589b8 [file] [log] [blame]
Changbin Du4d2c7292019-05-14 22:47:26 +08001.. SPDX-License-Identifier: GPL-2.0
2.. include:: <isonum.txt>
Yu Zhao15b49be2009-03-20 11:25:18 +08003
Changbin Du4d2c7292019-05-14 22:47:26 +08004====================================
5PCI Express I/O Virtualization Howto
6====================================
Yu Zhao15b49be2009-03-20 11:25:18 +08007
Changbin Du4d2c7292019-05-14 22:47:26 +08008:Copyright: |copy| 2009 Intel Corporation
9:Authors: - Yu Zhao <yu.zhao@intel.com>
10 - Donald Dutile <ddutile@redhat.com>
Yu Zhao15b49be2009-03-20 11:25:18 +080011
Changbin Du4d2c7292019-05-14 22:47:26 +080012Overview
13========
14
15What is SR-IOV
16--------------
Yu Zhao15b49be2009-03-20 11:25:18 +080017
18Single Root I/O Virtualization (SR-IOV) is a PCI Express Extended
19capability which makes one physical device appear as multiple virtual
20devices. The physical device is referred to as Physical Function (PF)
21while the virtual devices are referred to as Virtual Functions (VF).
22Allocation of the VF can be dynamically controlled by the PF via
23registers encapsulated in the capability. By default, this feature is
24not enabled and the PF behaves as traditional PCIe device. Once it's
25turned on, each VF's PCI configuration space can be accessed by its own
26Bus, Device and Function Number (Routing ID). And each VF also has PCI
27Memory Space, which is used to map its register set. VF device driver
28operates on the register set so it can be functional and appear as a
29real existing PCI device.
30
Changbin Du4d2c7292019-05-14 22:47:26 +080031User Guide
32==========
Yu Zhao15b49be2009-03-20 11:25:18 +080033
Changbin Du4d2c7292019-05-14 22:47:26 +080034How can I enable SR-IOV capability
35----------------------------------
Yu Zhao15b49be2009-03-20 11:25:18 +080036
Donald Dutile2597ba72012-11-27 22:31:37 -050037Multiple methods are available for SR-IOV enablement.
38In the first method, the device driver (PF driver) will control the
39enabling and disabling of the capability via API provided by SR-IOV core.
40If the hardware has SR-IOV capability, loading its PF driver would
41enable it and all VFs associated with the PF. Some PF drivers require
42a module parameter to be set to determine the number of VFs to enable.
43In the second method, a write to the sysfs file sriov_numvfs will
44enable and disable the VFs associated with a PCIe PF. This method
45enables per-PF, VF enable/disable values versus the first method,
46which applies to all PFs of the same device. Additionally, the
47PCI SRIOV core support ensures that enable/disable operations are
48valid to reduce duplication in multiple drivers for the same
49checks, e.g., check numvfs == 0 if enabling VFs, ensure
50numvfs <= totalvfs.
51The second method is the recommended method for new/future VF devices.
Yu Zhao15b49be2009-03-20 11:25:18 +080052
Changbin Du4d2c7292019-05-14 22:47:26 +080053How can I use the Virtual Functions
54-----------------------------------
Yu Zhao15b49be2009-03-20 11:25:18 +080055
56The VF is treated as hot-plugged PCI devices in the kernel, so they
57should be able to work in the same way as real PCI devices. The VF
58requires device driver that is same as a normal PCI device's.
59
Changbin Du4d2c7292019-05-14 22:47:26 +080060Developer Guide
61===============
Yu Zhao15b49be2009-03-20 11:25:18 +080062
Changbin Du4d2c7292019-05-14 22:47:26 +080063SR-IOV API
64----------
Yu Zhao15b49be2009-03-20 11:25:18 +080065
66To enable SR-IOV capability:
Changbin Du4d2c7292019-05-14 22:47:26 +080067
68(a) For the first method, in the driver::
69
Yu Zhao15b49be2009-03-20 11:25:18 +080070 int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn);
Changbin Du4d2c7292019-05-14 22:47:26 +080071
72'nr_virtfn' is number of VFs to be enabled.
73
74(b) For the second method, from sysfs::
75
Donald Dutile2597ba72012-11-27 22:31:37 -050076 echo 'nr_virtfn' > \
77 /sys/bus/pci/devices/<DOMAIN:BUS:DEVICE.FUNCTION>/sriov_numvfs
Yu Zhao15b49be2009-03-20 11:25:18 +080078
79To disable SR-IOV capability:
Changbin Du4d2c7292019-05-14 22:47:26 +080080
81(a) For the first method, in the driver::
82
Yu Zhao15b49be2009-03-20 11:25:18 +080083 void pci_disable_sriov(struct pci_dev *dev);
Changbin Du4d2c7292019-05-14 22:47:26 +080084
85(b) For the second method, from sysfs::
86
Donald Dutile2597ba72012-11-27 22:31:37 -050087 echo 0 > \
88 /sys/bus/pci/devices/<DOMAIN:BUS:DEVICE.FUNCTION>/sriov_numvfs
Yu Zhao15b49be2009-03-20 11:25:18 +080089
Bodong Wang0e7df222017-04-13 01:51:40 +030090To enable auto probing VFs by a compatible driver on the host, run
91command below before enabling SR-IOV capabilities. This is the
92default behavior.
Changbin Du4d2c7292019-05-14 22:47:26 +080093::
94
Bodong Wang0e7df222017-04-13 01:51:40 +030095 echo 1 > \
96 /sys/bus/pci/devices/<DOMAIN:BUS:DEVICE.FUNCTION>/sriov_drivers_autoprobe
97
98To disable auto probing VFs by a compatible driver on the host, run
99command below before enabling SR-IOV capabilities. Updating this
100entry will not affect VFs which are already probed.
Changbin Du4d2c7292019-05-14 22:47:26 +0800101::
102
Bodong Wang0e7df222017-04-13 01:51:40 +0300103 echo 0 > \
104 /sys/bus/pci/devices/<DOMAIN:BUS:DEVICE.FUNCTION>/sriov_drivers_autoprobe
105
Changbin Du4d2c7292019-05-14 22:47:26 +0800106Usage example
107-------------
Yu Zhao15b49be2009-03-20 11:25:18 +0800108
109Following piece of code illustrates the usage of the SR-IOV API.
Changbin Du4d2c7292019-05-14 22:47:26 +0800110::
Yu Zhao15b49be2009-03-20 11:25:18 +0800111
Changbin Du4d2c7292019-05-14 22:47:26 +0800112 static int dev_probe(struct pci_dev *dev, const struct pci_device_id *id)
113 {
114 pci_enable_sriov(dev, NR_VIRTFN);
Yu Zhao15b49be2009-03-20 11:25:18 +0800115
Donald Dutile2597ba72012-11-27 22:31:37 -0500116 ...
Changbin Du4d2c7292019-05-14 22:47:26 +0800117
Donald Dutile2597ba72012-11-27 22:31:37 -0500118 return 0;
119 }
Donald Dutile2597ba72012-11-27 22:31:37 -0500120
Changbin Du4d2c7292019-05-14 22:47:26 +0800121 static void dev_remove(struct pci_dev *dev)
122 {
123 pci_disable_sriov(dev);
124
125 ...
126 }
127
128 static int dev_suspend(struct pci_dev *dev, pm_message_t state)
129 {
130 ...
131
132 return 0;
133 }
134
135 static int dev_resume(struct pci_dev *dev)
136 {
137 ...
138
139 return 0;
140 }
141
142 static void dev_shutdown(struct pci_dev *dev)
143 {
144 ...
145 }
146
147 static int dev_sriov_configure(struct pci_dev *dev, int numvfs)
148 {
149 if (numvfs > 0) {
150 ...
151 pci_enable_sriov(dev, numvfs);
152 ...
153 return numvfs;
154 }
155 if (numvfs == 0) {
156 ....
157 pci_disable_sriov(dev);
158 ...
159 return 0;
160 }
161 }
162
163 static struct pci_driver dev_driver = {
164 .name = "SR-IOV Physical Function driver",
165 .id_table = dev_id_table,
166 .probe = dev_probe,
167 .remove = dev_remove,
168 .suspend = dev_suspend,
169 .resume = dev_resume,
170 .shutdown = dev_shutdown,
171 .sriov_configure = dev_sriov_configure,
172 };