blob: 525fd3f272b3c1f35dbd1f0c2d380a734cca2e37 [file] [log] [blame]
Bjorn Helgaas7328c8f2018-01-26 11:45:16 -06001// SPDX-License-Identifier: GPL-2.0
Yu Zhaod1b054d2009-03-20 11:25:11 +08002/*
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06003 * PCI Express I/O Virtualization (IOV) support
Yu Zhaod1b054d2009-03-20 11:25:11 +08004 * Single Root IOV 1.0
Yu Zhao302b4212009-05-18 13:51:32 +08005 * Address Translation Service 1.0
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06006 *
7 * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
Yu Zhaod1b054d2009-03-20 11:25:11 +08008 */
9
10#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Yu Zhaod1b054d2009-03-20 11:25:11 +080012#include <linux/mutex.h>
Paul Gortmaker363c75d2011-05-27 09:37:25 -040013#include <linux/export.h>
Yu Zhaod1b054d2009-03-20 11:25:11 +080014#include <linux/string.h>
15#include <linux/delay.h>
16#include "pci.h"
17
Yu Zhaodd7cc442009-03-20 11:25:15 +080018#define VIRTFN_ID_LEN 16
Yu Zhaod1b054d2009-03-20 11:25:11 +080019
Wei Yangb07579c2015-03-25 16:23:48 +080020int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id)
Yu Zhaoa28724b2009-03-20 11:25:13 +080021{
Wei Yangb07579c2015-03-25 16:23:48 +080022 if (!dev->is_physfn)
23 return -EINVAL;
Yu Zhaoa28724b2009-03-20 11:25:13 +080024 return dev->bus->number + ((dev->devfn + dev->sriov->offset +
Wei Yangb07579c2015-03-25 16:23:48 +080025 dev->sriov->stride * vf_id) >> 8);
Yu Zhaoa28724b2009-03-20 11:25:13 +080026}
27
Wei Yangb07579c2015-03-25 16:23:48 +080028int pci_iov_virtfn_devfn(struct pci_dev *dev, int vf_id)
Yu Zhaoa28724b2009-03-20 11:25:13 +080029{
Wei Yangb07579c2015-03-25 16:23:48 +080030 if (!dev->is_physfn)
31 return -EINVAL;
Yu Zhaoa28724b2009-03-20 11:25:13 +080032 return (dev->devfn + dev->sriov->offset +
Wei Yangb07579c2015-03-25 16:23:48 +080033 dev->sriov->stride * vf_id) & 0xff;
Yu Zhaoa28724b2009-03-20 11:25:13 +080034}
35
Wei Yangf59dca22015-03-25 16:23:46 +080036/*
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.
39 *
40 * Update iov->offset and iov->stride when NumVFs is written.
41 */
42static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
43{
44 struct pci_sriov *iov = dev->sriov;
45
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);
49}
50
Wei Yang4449f072015-03-25 16:23:47 +080051/*
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.
54 *
Alexander Duyckea9a8852015-10-29 16:20:50 -050055 * Iterate over all valid NumVFs, validate offset and stride, and calculate
56 * the maximum number of bus numbers that could ever be required.
Wei Yang4449f072015-03-25 16:23:47 +080057 */
Alexander Duyckea9a8852015-10-29 16:20:50 -050058static int compute_max_vf_buses(struct pci_dev *dev)
Wei Yang4449f072015-03-25 16:23:47 +080059{
60 struct pci_sriov *iov = dev->sriov;
Alexander Duyckea9a8852015-10-29 16:20:50 -050061 int nr_virtfn, busnr, rc = 0;
Wei Yang4449f072015-03-25 16:23:47 +080062
Alexander Duyckea9a8852015-10-29 16:20:50 -050063 for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) {
Wei Yang4449f072015-03-25 16:23:47 +080064 pci_iov_set_numvfs(dev, nr_virtfn);
Alexander Duyckea9a8852015-10-29 16:20:50 -050065 if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) {
66 rc = -EIO;
67 goto out;
68 }
69
Wei Yangb07579c2015-03-25 16:23:48 +080070 busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
Alexander Duyckea9a8852015-10-29 16:20:50 -050071 if (busnr > iov->max_VF_buses)
72 iov->max_VF_buses = busnr;
Wei Yang4449f072015-03-25 16:23:47 +080073 }
74
Alexander Duyckea9a8852015-10-29 16:20:50 -050075out:
76 pci_iov_set_numvfs(dev, 0);
77 return rc;
Wei Yang4449f072015-03-25 16:23:47 +080078}
79
Yu Zhaodd7cc442009-03-20 11:25:15 +080080static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
81{
Yu Zhaodd7cc442009-03-20 11:25:15 +080082 struct pci_bus *child;
83
84 if (bus->number == busnr)
85 return bus;
86
87 child = pci_find_bus(pci_domain_nr(bus), busnr);
88 if (child)
89 return child;
90
91 child = pci_add_new_bus(bus, NULL, busnr);
92 if (!child)
93 return NULL;
94
Yinghai Lub7eac052012-05-17 18:51:13 -070095 pci_bus_insert_busn_res(child, busnr, busnr);
Yu Zhaodd7cc442009-03-20 11:25:15 +080096
97 return child;
98}
99
Jiang Liudc087f22013-05-25 21:48:37 +0800100static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800101{
Jiang Liudc087f22013-05-25 21:48:37 +0800102 if (physbus != virtbus && list_empty(&virtbus->devices))
103 pci_remove_bus(virtbus);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800104}
105
Wei Yang0e6c9122015-03-25 16:23:44 +0800106resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
107{
108 if (!dev->is_physfn)
109 return 0;
110
111 return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
112}
113
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +0100114static void pci_read_vf_config_common(struct pci_dev *virtfn)
115{
116 struct pci_dev *physfn = virtfn->physfn;
117
118 /*
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
121 * other VFs.
122 *
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
125 * do.
126 */
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);
135}
136
Jan H. Schönherr753f6122017-09-26 12:53:23 -0500137int pci_iov_add_virtfn(struct pci_dev *dev, int id)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800138{
139 int i;
Jiang Liudc087f22013-05-25 21:48:37 +0800140 int rc = -ENOMEM;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800141 u64 size;
142 char buf[VIRTFN_ID_LEN];
143 struct pci_dev *virtfn;
144 struct resource *res;
145 struct pci_sriov *iov = dev->sriov;
Gu Zheng8b1fce02013-05-25 21:48:31 +0800146 struct pci_bus *bus;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800147
Wei Yangb07579c2015-03-25 16:23:48 +0800148 bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
Jiang Liudc087f22013-05-25 21:48:37 +0800149 if (!bus)
150 goto failed;
151
152 virtfn = pci_alloc_dev(bus);
153 if (!virtfn)
154 goto failed0;
155
Wei Yangb07579c2015-03-25 16:23:48 +0800156 virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800157 virtfn->vendor = dev->vendor;
Filippo Sironi3142d832017-08-28 15:38:49 +0200158 virtfn->device = iov->vf_device;
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +0100159 virtfn->is_virtfn = 1;
160 virtfn->physfn = pci_dev_get(dev);
161
162 if (id == 0)
163 pci_read_vf_config_common(virtfn);
164
Po Liu156c5532016-08-29 15:28:01 +0800165 rc = pci_setup_device(virtfn);
166 if (rc)
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +0100167 goto failed1;
Po Liu156c5532016-08-29 15:28:01 +0800168
Yu Zhaodd7cc442009-03-20 11:25:15 +0800169 virtfn->dev.parent = dev->dev.parent;
Alex Williamsonaa9319772014-01-09 08:36:08 -0700170 virtfn->multifunction = 0;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800171
172 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Bjorn Helgaasc1fe1f92015-03-25 16:23:45 +0800173 res = &dev->resource[i + PCI_IOV_RESOURCES];
Yu Zhaodd7cc442009-03-20 11:25:15 +0800174 if (!res->parent)
175 continue;
176 virtfn->resource[i].name = pci_name(virtfn);
177 virtfn->resource[i].flags = res->flags;
Wei Yang0e6c9122015-03-25 16:23:44 +0800178 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800179 virtfn->resource[i].start = res->start + size * id;
180 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
181 rc = request_resource(res, &virtfn->resource[i]);
182 BUG_ON(rc);
183 }
184
Yu Zhaodd7cc442009-03-20 11:25:15 +0800185 pci_device_add(virtfn, virtfn->bus);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800186
Yu Zhaodd7cc442009-03-20 11:25:15 +0800187 sprintf(buf, "virtfn%u", id);
188 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
189 if (rc)
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +0100190 goto failed2;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800191 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
192 if (rc)
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +0100193 goto failed3;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800194
195 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
196
Stuart Hayes27d61622017-10-04 10:57:52 -0500197 pci_bus_add_device(virtfn);
198
Yu Zhaodd7cc442009-03-20 11:25:15 +0800199 return 0;
200
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +0100201failed3:
Yu Zhaodd7cc442009-03-20 11:25:15 +0800202 sysfs_remove_link(&dev->dev.kobj, buf);
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +0100203failed2:
204 pci_stop_and_remove_bus_device(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800205failed1:
206 pci_dev_put(dev);
Jiang Liudc087f22013-05-25 21:48:37 +0800207failed0:
208 virtfn_remove_bus(dev->bus, bus);
209failed:
Yu Zhaodd7cc442009-03-20 11:25:15 +0800210
211 return rc;
212}
213
Jan H. Schönherr753f6122017-09-26 12:53:23 -0500214void pci_iov_remove_virtfn(struct pci_dev *dev, int id)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800215{
216 char buf[VIRTFN_ID_LEN];
Yu Zhaodd7cc442009-03-20 11:25:15 +0800217 struct pci_dev *virtfn;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800218
Jiang Liudc087f22013-05-25 21:48:37 +0800219 virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
Wei Yangb07579c2015-03-25 16:23:48 +0800220 pci_iov_virtfn_bus(dev, id),
221 pci_iov_virtfn_devfn(dev, id));
Yu Zhaodd7cc442009-03-20 11:25:15 +0800222 if (!virtfn)
223 return;
224
Yu Zhaodd7cc442009-03-20 11:25:15 +0800225 sprintf(buf, "virtfn%u", id);
226 sysfs_remove_link(&dev->dev.kobj, buf);
Yinghai Lu09cedbe2012-02-04 22:55:01 -0800227 /*
228 * pci_stop_dev() could have been called for this virtfn already,
229 * so the directory for the virtfn may have been removed before.
230 * Double check to avoid spurious sysfs warnings.
231 */
232 if (virtfn->dev.kobj.sd)
233 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
Yu Zhaodd7cc442009-03-20 11:25:15 +0800234
Yinghai Lu210647a2012-02-25 13:54:20 -0800235 pci_stop_and_remove_bus_device(virtfn);
Jiang Liudc087f22013-05-25 21:48:37 +0800236 virtfn_remove_bus(dev->bus, virtfn->bus);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800237
Jiang Liudc087f22013-05-25 21:48:37 +0800238 /* balance pci_get_domain_bus_and_slot() */
239 pci_dev_put(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800240 pci_dev_put(dev);
241}
242
Wei Yang995df522015-03-25 16:23:49 +0800243int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
244{
Alexander Duycka39e3fc2015-10-29 16:21:11 -0500245 return 0;
246}
247
248int __weak pcibios_sriov_disable(struct pci_dev *pdev)
249{
250 return 0;
Wei Yang995df522015-03-25 16:23:49 +0800251}
252
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100253static int sriov_add_vfs(struct pci_dev *dev, u16 num_vfs)
254{
255 unsigned int i;
256 int rc;
257
Sebastian Ottaff68a5a2018-12-21 15:14:19 +0100258 if (dev->no_vf_scan)
259 return 0;
260
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100261 for (i = 0; i < num_vfs; i++) {
262 rc = pci_iov_add_virtfn(dev, i);
263 if (rc)
264 goto failed;
265 }
266 return 0;
267failed:
268 while (i--)
269 pci_iov_remove_virtfn(dev, i);
270
271 return rc;
272}
273
Yu Zhaodd7cc442009-03-20 11:25:15 +0800274static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
275{
276 int rc;
Alexander Duyck3443c382015-10-29 16:21:05 -0500277 int i;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800278 int nres;
Alexander Duyckce288ec2015-10-29 16:20:57 -0500279 u16 initial;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800280 struct resource *res;
281 struct pci_dev *pdev;
282 struct pci_sriov *iov = dev->sriov;
Ram Paibbef98a2011-11-06 10:33:10 +0800283 int bars = 0;
Wei Yangb07579c2015-03-25 16:23:48 +0800284 int bus;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800285
286 if (!nr_virtfn)
287 return 0;
288
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700289 if (iov->num_VFs)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800290 return -EINVAL;
291
292 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700293 if (initial > iov->total_VFs ||
294 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
Yu Zhaodd7cc442009-03-20 11:25:15 +0800295 return -EIO;
296
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700297 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
Yu Zhaodd7cc442009-03-20 11:25:15 +0800298 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
299 return -EINVAL;
300
Yu Zhaodd7cc442009-03-20 11:25:15 +0800301 nres = 0;
302 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Ram Paibbef98a2011-11-06 10:33:10 +0800303 bars |= (1 << (i + PCI_IOV_RESOURCES));
Bjorn Helgaasc1fe1f92015-03-25 16:23:45 +0800304 res = &dev->resource[i + PCI_IOV_RESOURCES];
Yu Zhaodd7cc442009-03-20 11:25:15 +0800305 if (res->parent)
306 nres++;
307 }
308 if (nres != iov->nres) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600309 pci_err(dev, "not enough MMIO resources for SR-IOV\n");
Yu Zhaodd7cc442009-03-20 11:25:15 +0800310 return -ENOMEM;
311 }
312
Wei Yangb07579c2015-03-25 16:23:48 +0800313 bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
Bjorn Helgaas68f8e9f2015-03-25 16:23:42 +0800314 if (bus > dev->bus->busn_res.end) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600315 pci_err(dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
Bjorn Helgaas68f8e9f2015-03-25 16:23:42 +0800316 nr_virtfn, bus, &dev->bus->busn_res);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800317 return -ENOMEM;
318 }
319
Ram Paibbef98a2011-11-06 10:33:10 +0800320 if (pci_enable_resources(dev, bars)) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600321 pci_err(dev, "SR-IOV: IOV BARS not allocated\n");
Ram Paibbef98a2011-11-06 10:33:10 +0800322 return -ENOMEM;
323 }
324
Yu Zhaodd7cc442009-03-20 11:25:15 +0800325 if (iov->link != dev->devfn) {
326 pdev = pci_get_slot(dev->bus, iov->link);
327 if (!pdev)
328 return -ENODEV;
329
Jiang Liudc087f22013-05-25 21:48:37 +0800330 if (!pdev->is_physfn) {
331 pci_dev_put(pdev);
Stefan Assmann652d1102013-07-31 16:47:56 -0600332 return -ENOSYS;
Jiang Liudc087f22013-05-25 21:48:37 +0800333 }
Yu Zhaodd7cc442009-03-20 11:25:15 +0800334
335 rc = sysfs_create_link(&dev->dev.kobj,
336 &pdev->dev.kobj, "dep_link");
Jiang Liudc087f22013-05-25 21:48:37 +0800337 pci_dev_put(pdev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800338 if (rc)
339 return rc;
340 }
341
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700342 iov->initial_VFs = initial;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800343 if (nr_virtfn < initial)
344 initial = nr_virtfn;
345
Alexander Duyckc23b6132015-10-29 16:21:20 -0500346 rc = pcibios_sriov_enable(dev, initial);
347 if (rc) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600348 pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc);
Alexander Duyckc23b6132015-10-29 16:21:20 -0500349 goto err_pcibios;
Wei Yang995df522015-03-25 16:23:49 +0800350 }
351
Gavin Shanf40ec3c2016-10-26 12:15:35 +1100352 pci_iov_set_numvfs(dev, nr_virtfn);
353 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
354 pci_cfg_access_lock(dev);
355 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
356 msleep(100);
357 pci_cfg_access_unlock(dev);
358
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100359 rc = sriov_add_vfs(dev, initial);
360 if (rc)
361 goto err_pcibios;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800362
363 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700364 iov->num_VFs = nr_virtfn;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800365
366 return 0;
367
Alexander Duyckc23b6132015-10-29 16:21:20 -0500368err_pcibios:
Yu Zhaodd7cc442009-03-20 11:25:15 +0800369 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100370 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800371 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
372 ssleep(1);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100373 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800374
Gavin Shan0fc690a2017-08-11 18:19:33 +1000375 pcibios_sriov_disable(dev);
376
Yu Zhaodd7cc442009-03-20 11:25:15 +0800377 if (iov->link != dev->devfn)
378 sysfs_remove_link(&dev->dev.kobj, "dep_link");
379
Alexander Duyckb3908642015-10-29 16:21:16 -0500380 pci_iov_set_numvfs(dev, 0);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800381 return rc;
382}
383
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100384static void sriov_del_vfs(struct pci_dev *dev)
385{
386 struct pci_sriov *iov = dev->sriov;
387 int i;
388
Sebastian Ottaff68a5a2018-12-21 15:14:19 +0100389 if (dev->no_vf_scan)
390 return;
391
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100392 for (i = 0; i < iov->num_VFs; i++)
393 pci_iov_remove_virtfn(dev, i);
394}
395
Yu Zhaodd7cc442009-03-20 11:25:15 +0800396static void sriov_disable(struct pci_dev *dev)
397{
Yu Zhaodd7cc442009-03-20 11:25:15 +0800398 struct pci_sriov *iov = dev->sriov;
399
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700400 if (!iov->num_VFs)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800401 return;
402
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100403 sriov_del_vfs(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800404 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100405 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800406 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
407 ssleep(1);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100408 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800409
Gavin Shan0fc690a2017-08-11 18:19:33 +1000410 pcibios_sriov_disable(dev);
411
Yu Zhaodd7cc442009-03-20 11:25:15 +0800412 if (iov->link != dev->devfn)
413 sysfs_remove_link(&dev->dev.kobj, "dep_link");
414
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700415 iov->num_VFs = 0;
Wei Yangf59dca22015-03-25 16:23:46 +0800416 pci_iov_set_numvfs(dev, 0);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800417}
418
Yu Zhaod1b054d2009-03-20 11:25:11 +0800419static int sriov_init(struct pci_dev *dev, int pos)
420{
Wei Yang0e6c9122015-03-25 16:23:44 +0800421 int i, bar64;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800422 int rc;
423 int nres;
424 u32 pgsz;
Alexander Duyckea9a8852015-10-29 16:20:50 -0500425 u16 ctrl, total;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800426 struct pci_sriov *iov;
427 struct resource *res;
428 struct pci_dev *pdev;
429
Yu Zhaod1b054d2009-03-20 11:25:11 +0800430 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
431 if (ctrl & PCI_SRIOV_CTRL_VFE) {
432 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
433 ssleep(1);
434 }
435
Yu Zhaod1b054d2009-03-20 11:25:11 +0800436 ctrl = 0;
437 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
438 if (pdev->is_physfn)
439 goto found;
440
441 pdev = NULL;
442 if (pci_ari_enabled(dev->bus))
443 ctrl |= PCI_SRIOV_CTRL_ARI;
444
445found:
446 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800447
Ben Sheltonff45f9d2015-10-29 16:20:31 -0500448 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
449 if (!total)
450 return 0;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800451
452 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
453 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
454 pgsz &= ~((1 << i) - 1);
455 if (!pgsz)
456 return -EIO;
457
458 pgsz &= ~(pgsz - 1);
Vaidyanathan Srinivasan8161fe92012-02-02 23:11:20 +0530459 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800460
Wei Yang0e6c9122015-03-25 16:23:44 +0800461 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
462 if (!iov)
463 return -ENOMEM;
464
Yu Zhaod1b054d2009-03-20 11:25:11 +0800465 nres = 0;
466 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Bjorn Helgaasc1fe1f92015-03-25 16:23:45 +0800467 res = &dev->resource[i + PCI_IOV_RESOURCES];
David Daney11183992015-10-29 17:35:40 -0500468 /*
469 * If it is already FIXED, don't change it, something
470 * (perhaps EA or header fixups) wants it this way.
471 */
472 if (res->flags & IORESOURCE_PCI_FIXED)
473 bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
474 else
475 bar64 = __pci_read_base(dev, pci_bar_unknown, res,
476 pos + PCI_SRIOV_BAR + i * 4);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800477 if (!res->flags)
478 continue;
479 if (resource_size(res) & (PAGE_SIZE - 1)) {
480 rc = -EIO;
481 goto failed;
482 }
Wei Yang0e6c9122015-03-25 16:23:44 +0800483 iov->barsz[i] = resource_size(res);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800484 res->end = res->start + resource_size(res) * total - 1;
Frederick Lawler7506dc72018-01-18 12:55:24 -0600485 pci_info(dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
Wei Yange88ae012015-03-25 16:23:43 +0800486 i, res, i, total);
Wei Yang0e6c9122015-03-25 16:23:44 +0800487 i += bar64;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800488 nres++;
489 }
490
Yu Zhaod1b054d2009-03-20 11:25:11 +0800491 iov->pos = pos;
492 iov->nres = nres;
493 iov->ctrl = ctrl;
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700494 iov->total_VFs = total;
Jakub Kicinski8d85a7a2018-05-25 08:18:34 -0500495 iov->driver_max_VFs = total;
Filippo Sironi3142d832017-08-28 15:38:49 +0200496 pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800497 iov->pgsz = pgsz;
498 iov->self = dev;
Bodong Wang0e7df222017-04-13 01:51:40 +0300499 iov->drivers_autoprobe = true;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800500 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
501 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
Yijing Wang62f87c02012-07-24 17:20:03 +0800502 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
Yu Zhao4d135db2009-05-20 17:11:57 +0800503 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800504
505 if (pdev)
506 iov->dev = pci_dev_get(pdev);
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800507 else
Yu Zhaod1b054d2009-03-20 11:25:11 +0800508 iov->dev = dev;
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800509
Yu Zhaod1b054d2009-03-20 11:25:11 +0800510 dev->sriov = iov;
511 dev->is_physfn = 1;
Alexander Duyckea9a8852015-10-29 16:20:50 -0500512 rc = compute_max_vf_buses(dev);
513 if (rc)
514 goto fail_max_buses;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800515
516 return 0;
517
Alexander Duyckea9a8852015-10-29 16:20:50 -0500518fail_max_buses:
519 dev->sriov = NULL;
520 dev->is_physfn = 0;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800521failed:
522 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Bjorn Helgaasc1fe1f92015-03-25 16:23:45 +0800523 res = &dev->resource[i + PCI_IOV_RESOURCES];
Yu Zhaod1b054d2009-03-20 11:25:11 +0800524 res->flags = 0;
525 }
526
Wei Yang0e6c9122015-03-25 16:23:44 +0800527 kfree(iov);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800528 return rc;
529}
530
531static void sriov_release(struct pci_dev *dev)
532{
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700533 BUG_ON(dev->sriov->num_VFs);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800534
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800535 if (dev != dev->sriov->dev)
Yu Zhaod1b054d2009-03-20 11:25:11 +0800536 pci_dev_put(dev->sriov->dev);
537
538 kfree(dev->sriov);
539 dev->sriov = NULL;
540}
541
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800542static void sriov_restore_state(struct pci_dev *dev)
543{
544 int i;
545 u16 ctrl;
546 struct pci_sriov *iov = dev->sriov;
547
548 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
549 if (ctrl & PCI_SRIOV_CTRL_VFE)
550 return;
551
Tony Nguyenff264492017-10-04 08:52:58 -0700552 /*
553 * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
554 * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
555 */
556 ctrl &= ~PCI_SRIOV_CTRL_ARI;
557 ctrl |= iov->ctrl & PCI_SRIOV_CTRL_ARI;
558 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
559
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800560 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
561 pci_update_resource(dev, i);
562
563 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
Wei Yangf59dca22015-03-25 16:23:46 +0800564 pci_iov_set_numvfs(dev, iov->num_VFs);
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800565 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
566 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
567 msleep(100);
568}
569
Yu Zhaod1b054d2009-03-20 11:25:11 +0800570/**
571 * pci_iov_init - initialize the IOV capability
572 * @dev: the PCI device
573 *
574 * Returns 0 on success, or negative on failure.
575 */
576int pci_iov_init(struct pci_dev *dev)
577{
578 int pos;
579
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900580 if (!pci_is_pcie(dev))
Yu Zhaod1b054d2009-03-20 11:25:11 +0800581 return -ENODEV;
582
583 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
584 if (pos)
585 return sriov_init(dev, pos);
586
587 return -ENODEV;
588}
589
590/**
591 * pci_iov_release - release resources used by the IOV capability
592 * @dev: the PCI device
593 */
594void pci_iov_release(struct pci_dev *dev)
595{
596 if (dev->is_physfn)
597 sriov_release(dev);
598}
599
600/**
Jakub Kicinski38972372018-06-29 15:08:52 -0500601 * pci_iov_remove - clean up SR-IOV state after PF driver is detached
602 * @dev: the PCI device
603 */
604void pci_iov_remove(struct pci_dev *dev)
605{
606 struct pci_sriov *iov = dev->sriov;
607
608 if (!dev->is_physfn)
609 return;
610
611 iov->driver_max_VFs = iov->total_VFs;
612 if (iov->num_VFs)
613 pci_warn(dev, "driver left SR-IOV enabled after remove\n");
614}
615
616/**
Bjorn Helgaas6ffa2482016-11-28 09:15:52 -0600617 * pci_iov_update_resource - update a VF BAR
618 * @dev: the PCI device
619 * @resno: the resource number
620 *
621 * Update a VF BAR in the SR-IOV capability of a PF.
622 */
623void pci_iov_update_resource(struct pci_dev *dev, int resno)
624{
625 struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
626 struct resource *res = dev->resource + resno;
627 int vf_bar = resno - PCI_IOV_RESOURCES;
628 struct pci_bus_region region;
Bjorn Helgaas546ba9f2016-11-28 16:43:06 -0600629 u16 cmd;
Bjorn Helgaas6ffa2482016-11-28 09:15:52 -0600630 u32 new;
631 int reg;
632
633 /*
634 * The generic pci_restore_bars() path calls this for all devices,
635 * including VFs and non-SR-IOV devices. If this is not a PF, we
636 * have nothing to do.
637 */
638 if (!iov)
639 return;
640
Bjorn Helgaas546ba9f2016-11-28 16:43:06 -0600641 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
642 if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) {
643 dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
644 vf_bar, res);
645 return;
646 }
647
Bjorn Helgaas6ffa2482016-11-28 09:15:52 -0600648 /*
649 * Ignore unimplemented BARs, unused resource slots for 64-bit
650 * BARs, and non-movable resources, e.g., those described via
651 * Enhanced Allocation.
652 */
653 if (!res->flags)
654 return;
655
656 if (res->flags & IORESOURCE_UNSET)
657 return;
658
659 if (res->flags & IORESOURCE_PCI_FIXED)
660 return;
661
662 pcibios_resource_to_bus(dev->bus, &region, res);
663 new = region.start;
664 new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK;
665
666 reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar;
667 pci_write_config_dword(dev, reg, new);
668 if (res->flags & IORESOURCE_MEM_64) {
669 new = region.start >> 16 >> 16;
670 pci_write_config_dword(dev, reg + 4, new);
671 }
672}
673
Wei Yang978d2d62015-03-25 16:23:50 +0800674resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
675 int resno)
676{
677 return pci_iov_resource_size(dev, resno);
678}
679
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800680/**
Chris Wright6faf17f2009-08-28 13:00:06 -0700681 * pci_sriov_resource_alignment - get resource alignment for VF BAR
682 * @dev: the PCI device
683 * @resno: the resource number
684 *
685 * Returns the alignment of the VF BAR found in the SR-IOV capability.
686 * This is not the same as the resource size which is defined as
687 * the VF BAR size multiplied by the number of VFs. The alignment
688 * is just the VF BAR size.
689 */
Cam Macdonell0e522472010-09-07 17:25:20 -0700690resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
Chris Wright6faf17f2009-08-28 13:00:06 -0700691{
Wei Yang978d2d62015-03-25 16:23:50 +0800692 return pcibios_iov_resource_alignment(dev, resno);
Chris Wright6faf17f2009-08-28 13:00:06 -0700693}
694
695/**
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800696 * pci_restore_iov_state - restore the state of the IOV capability
697 * @dev: the PCI device
698 */
699void pci_restore_iov_state(struct pci_dev *dev)
700{
701 if (dev->is_physfn)
702 sriov_restore_state(dev);
703}
Yu Zhaoa28724b2009-03-20 11:25:13 +0800704
705/**
Bryant G. Ly608c0d82017-11-09 08:00:35 -0600706 * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs
707 * @dev: the PCI device
708 * @auto_probe: set VF drivers auto probe flag
709 */
710void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe)
711{
712 if (dev->is_physfn)
713 dev->sriov->drivers_autoprobe = auto_probe;
714}
715
716/**
Yu Zhaoa28724b2009-03-20 11:25:13 +0800717 * pci_iov_bus_range - find bus range used by Virtual Function
718 * @bus: the PCI bus
719 *
720 * Returns max number of buses (exclude current one) used by Virtual
721 * Functions.
722 */
723int pci_iov_bus_range(struct pci_bus *bus)
724{
725 int max = 0;
Yu Zhaoa28724b2009-03-20 11:25:13 +0800726 struct pci_dev *dev;
727
728 list_for_each_entry(dev, &bus->devices, bus_list) {
729 if (!dev->is_physfn)
730 continue;
Wei Yang4449f072015-03-25 16:23:47 +0800731 if (dev->sriov->max_VF_buses > max)
732 max = dev->sriov->max_VF_buses;
Yu Zhaoa28724b2009-03-20 11:25:13 +0800733 }
734
735 return max ? max - bus->number : 0;
736}
Yu Zhaodd7cc442009-03-20 11:25:15 +0800737
738/**
739 * pci_enable_sriov - enable the SR-IOV capability
740 * @dev: the PCI device
Randy Dunlap52a88732009-04-01 17:45:30 -0700741 * @nr_virtfn: number of virtual functions to enable
Yu Zhaodd7cc442009-03-20 11:25:15 +0800742 *
743 * Returns 0 on success, or negative on failure.
744 */
745int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
746{
747 might_sleep();
748
749 if (!dev->is_physfn)
Stefan Assmann652d1102013-07-31 16:47:56 -0600750 return -ENOSYS;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800751
752 return sriov_enable(dev, nr_virtfn);
753}
754EXPORT_SYMBOL_GPL(pci_enable_sriov);
755
756/**
757 * pci_disable_sriov - disable the SR-IOV capability
758 * @dev: the PCI device
759 */
760void pci_disable_sriov(struct pci_dev *dev)
761{
762 might_sleep();
763
764 if (!dev->is_physfn)
765 return;
766
767 sriov_disable(dev);
768}
769EXPORT_SYMBOL_GPL(pci_disable_sriov);
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800770
771/**
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000772 * pci_num_vf - return number of VFs associated with a PF device_release_driver
773 * @dev: the PCI device
774 *
775 * Returns number of VFs, or 0 if SR-IOV is not enabled.
776 */
777int pci_num_vf(struct pci_dev *dev)
778{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700779 if (!dev->is_physfn)
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000780 return 0;
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700781
782 return dev->sriov->num_VFs;
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000783}
784EXPORT_SYMBOL_GPL(pci_num_vf);
Donald Dutilebff73152012-11-05 15:20:37 -0500785
786/**
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000787 * pci_vfs_assigned - returns number of VFs are assigned to a guest
788 * @dev: the PCI device
789 *
790 * Returns number of VFs belonging to this device that are assigned to a guest.
Stefan Assmann652d1102013-07-31 16:47:56 -0600791 * If device is not a physical function returns 0.
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000792 */
793int pci_vfs_assigned(struct pci_dev *dev)
794{
795 struct pci_dev *vfdev;
796 unsigned int vfs_assigned = 0;
797 unsigned short dev_id;
798
799 /* only search if we are a PF */
800 if (!dev->is_physfn)
801 return 0;
802
803 /*
804 * determine the device ID for the VFs, the vendor ID will be the
805 * same as the PF so there is no need to check for that one
806 */
Filippo Sironi3142d832017-08-28 15:38:49 +0200807 dev_id = dev->sriov->vf_device;
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000808
809 /* loop through all the VFs to see if we own any that are assigned */
810 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
811 while (vfdev) {
812 /*
813 * It is considered assigned if it is a virtual function with
814 * our dev as the physical function and the assigned bit is set
815 */
816 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
Ethan Zhaobe634972014-09-09 10:21:28 +0800817 pci_is_dev_assigned(vfdev))
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000818 vfs_assigned++;
819
820 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
821 }
822
823 return vfs_assigned;
824}
825EXPORT_SYMBOL_GPL(pci_vfs_assigned);
826
827/**
Donald Dutilebff73152012-11-05 15:20:37 -0500828 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
829 * @dev: the PCI PF device
Randy Dunlap2094f162013-01-09 17:12:52 -0800830 * @numvfs: number that should be used for TotalVFs supported
Donald Dutilebff73152012-11-05 15:20:37 -0500831 *
832 * Should be called from PF driver's probe routine with
833 * device's mutex held.
834 *
835 * Returns 0 if PF is an SRIOV-capable device and
Stefan Assmann652d1102013-07-31 16:47:56 -0600836 * value of numvfs valid. If not a PF return -ENOSYS;
837 * if numvfs is invalid return -EINVAL;
Donald Dutilebff73152012-11-05 15:20:37 -0500838 * if VFs already enabled, return -EBUSY.
839 */
840int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
841{
Stefan Assmann652d1102013-07-31 16:47:56 -0600842 if (!dev->is_physfn)
843 return -ENOSYS;
Bjorn Helgaas51259d02018-05-25 08:51:50 -0500844
Stefan Assmann652d1102013-07-31 16:47:56 -0600845 if (numvfs > dev->sriov->total_VFs)
Donald Dutilebff73152012-11-05 15:20:37 -0500846 return -EINVAL;
847
848 /* Shouldn't change if VFs already enabled */
849 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
850 return -EBUSY;
Donald Dutilebff73152012-11-05 15:20:37 -0500851
Bjorn Helgaas51259d02018-05-25 08:51:50 -0500852 dev->sriov->driver_max_VFs = numvfs;
Donald Dutilebff73152012-11-05 15:20:37 -0500853 return 0;
854}
855EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
856
857/**
Jonghwan Choiddc191f2013-07-08 14:02:43 -0600858 * pci_sriov_get_totalvfs -- get total VFs supported on this device
Donald Dutilebff73152012-11-05 15:20:37 -0500859 * @dev: the PCI PF device
860 *
861 * For a PCIe device with SRIOV support, return the PCIe
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700862 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
Stefan Assmann652d1102013-07-31 16:47:56 -0600863 * if the driver reduced it. Otherwise 0.
Donald Dutilebff73152012-11-05 15:20:37 -0500864 */
865int pci_sriov_get_totalvfs(struct pci_dev *dev)
866{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700867 if (!dev->is_physfn)
Stefan Assmann652d1102013-07-31 16:47:56 -0600868 return 0;
Donald Dutilebff73152012-11-05 15:20:37 -0500869
Jakub Kicinski8d85a7a2018-05-25 08:18:34 -0500870 return dev->sriov->driver_max_VFs;
Donald Dutilebff73152012-11-05 15:20:37 -0500871}
872EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);
Alexander Duyck8effc392018-04-21 15:23:09 -0500873
874/**
875 * pci_sriov_configure_simple - helper to configure SR-IOV
876 * @dev: the PCI device
877 * @nr_virtfn: number of virtual functions to enable, 0 to disable
878 *
879 * Enable or disable SR-IOV for devices that don't require any PF setup
880 * before enabling SR-IOV. Return value is negative on error, or number of
881 * VFs allocated on success.
882 */
883int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn)
884{
885 int rc;
886
887 might_sleep();
888
889 if (!dev->is_physfn)
890 return -ENODEV;
891
892 if (pci_vfs_assigned(dev)) {
893 pci_warn(dev, "Cannot modify SR-IOV while VFs are assigned\n");
894 return -EPERM;
895 }
896
897 if (nr_virtfn == 0) {
898 sriov_disable(dev);
899 return 0;
900 }
901
902 rc = sriov_enable(dev, nr_virtfn);
903 if (rc < 0)
904 return rc;
905
906 return nr_virtfn;
907}
908EXPORT_SYMBOL_GPL(pci_sriov_configure_simple);