blob: 05305436224795570cae0a05194b19b2d1df57d8 [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
Kelsey Skunbergaaee0c12019-08-13 14:45:13 -0600243static ssize_t sriov_totalvfs_show(struct device *dev,
244 struct device_attribute *attr,
245 char *buf)
246{
247 struct pci_dev *pdev = to_pci_dev(dev);
248
249 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
250}
251
252static ssize_t sriov_numvfs_show(struct device *dev,
253 struct device_attribute *attr,
254 char *buf)
255{
256 struct pci_dev *pdev = to_pci_dev(dev);
257
258 return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
259}
260
261/*
262 * num_vfs > 0; number of VFs to enable
263 * num_vfs = 0; disable all VFs
264 *
265 * Note: SRIOV spec does not allow partial VF
266 * disable, so it's all or none.
267 */
268static ssize_t sriov_numvfs_store(struct device *dev,
269 struct device_attribute *attr,
270 const char *buf, size_t count)
271{
272 struct pci_dev *pdev = to_pci_dev(dev);
273 int ret;
274 u16 num_vfs;
275
276 ret = kstrtou16(buf, 0, &num_vfs);
277 if (ret < 0)
278 return ret;
279
280 if (num_vfs > pci_sriov_get_totalvfs(pdev))
281 return -ERANGE;
282
283 device_lock(&pdev->dev);
284
285 if (num_vfs == pdev->sriov->num_VFs)
286 goto exit;
287
288 /* is PF driver loaded w/callback */
289 if (!pdev->driver || !pdev->driver->sriov_configure) {
290 pci_info(pdev, "Driver does not support SRIOV configuration via sysfs\n");
291 ret = -ENOENT;
292 goto exit;
293 }
294
295 if (num_vfs == 0) {
296 /* disable VFs */
297 ret = pdev->driver->sriov_configure(pdev, 0);
298 goto exit;
299 }
300
301 /* enable VFs */
302 if (pdev->sriov->num_VFs) {
303 pci_warn(pdev, "%d VFs already enabled. Disable before enabling %d VFs\n",
304 pdev->sriov->num_VFs, num_vfs);
305 ret = -EBUSY;
306 goto exit;
307 }
308
309 ret = pdev->driver->sriov_configure(pdev, num_vfs);
310 if (ret < 0)
311 goto exit;
312
313 if (ret != num_vfs)
314 pci_warn(pdev, "%d VFs requested; only %d enabled\n",
315 num_vfs, ret);
316
317exit:
318 device_unlock(&pdev->dev);
319
320 if (ret < 0)
321 return ret;
322
323 return count;
324}
325
326static ssize_t sriov_offset_show(struct device *dev,
327 struct device_attribute *attr,
328 char *buf)
329{
330 struct pci_dev *pdev = to_pci_dev(dev);
331
332 return sprintf(buf, "%u\n", pdev->sriov->offset);
333}
334
335static ssize_t sriov_stride_show(struct device *dev,
336 struct device_attribute *attr,
337 char *buf)
338{
339 struct pci_dev *pdev = to_pci_dev(dev);
340
341 return sprintf(buf, "%u\n", pdev->sriov->stride);
342}
343
344static ssize_t sriov_vf_device_show(struct device *dev,
345 struct device_attribute *attr,
346 char *buf)
347{
348 struct pci_dev *pdev = to_pci_dev(dev);
349
350 return sprintf(buf, "%x\n", pdev->sriov->vf_device);
351}
352
353static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
354 struct device_attribute *attr,
355 char *buf)
356{
357 struct pci_dev *pdev = to_pci_dev(dev);
358
359 return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe);
360}
361
362static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
363 struct device_attribute *attr,
364 const char *buf, size_t count)
365{
366 struct pci_dev *pdev = to_pci_dev(dev);
367 bool drivers_autoprobe;
368
369 if (kstrtobool(buf, &drivers_autoprobe) < 0)
370 return -EINVAL;
371
372 pdev->sriov->drivers_autoprobe = drivers_autoprobe;
373
374 return count;
375}
376
377static DEVICE_ATTR_RO(sriov_totalvfs);
378static DEVICE_ATTR(sriov_numvfs, 0664, sriov_numvfs_show, sriov_numvfs_store);
379static DEVICE_ATTR_RO(sriov_offset);
380static DEVICE_ATTR_RO(sriov_stride);
381static DEVICE_ATTR_RO(sriov_vf_device);
382static DEVICE_ATTR(sriov_drivers_autoprobe, 0664, sriov_drivers_autoprobe_show,
383 sriov_drivers_autoprobe_store);
384
385static struct attribute *sriov_dev_attrs[] = {
386 &dev_attr_sriov_totalvfs.attr,
387 &dev_attr_sriov_numvfs.attr,
388 &dev_attr_sriov_offset.attr,
389 &dev_attr_sriov_stride.attr,
390 &dev_attr_sriov_vf_device.attr,
391 &dev_attr_sriov_drivers_autoprobe.attr,
392 NULL,
393};
394
395static umode_t sriov_attrs_are_visible(struct kobject *kobj,
396 struct attribute *a, int n)
397{
398 struct device *dev = kobj_to_dev(kobj);
399
400 if (!dev_is_pf(dev))
401 return 0;
402
403 return a->mode;
404}
405
406const struct attribute_group sriov_dev_attr_group = {
407 .attrs = sriov_dev_attrs,
408 .is_visible = sriov_attrs_are_visible,
409};
410
Wei Yang995df522015-03-25 16:23:49 +0800411int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
412{
Alexander Duycka39e3fc2015-10-29 16:21:11 -0500413 return 0;
414}
415
416int __weak pcibios_sriov_disable(struct pci_dev *pdev)
417{
418 return 0;
Wei Yang995df522015-03-25 16:23:49 +0800419}
420
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100421static int sriov_add_vfs(struct pci_dev *dev, u16 num_vfs)
422{
423 unsigned int i;
424 int rc;
425
Sebastian Ottaff68a5a2018-12-21 15:14:19 +0100426 if (dev->no_vf_scan)
427 return 0;
428
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100429 for (i = 0; i < num_vfs; i++) {
430 rc = pci_iov_add_virtfn(dev, i);
431 if (rc)
432 goto failed;
433 }
434 return 0;
435failed:
436 while (i--)
437 pci_iov_remove_virtfn(dev, i);
438
439 return rc;
440}
441
Yu Zhaodd7cc442009-03-20 11:25:15 +0800442static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
443{
444 int rc;
Alexander Duyck3443c382015-10-29 16:21:05 -0500445 int i;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800446 int nres;
Alexander Duyckce288ec2015-10-29 16:20:57 -0500447 u16 initial;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800448 struct resource *res;
449 struct pci_dev *pdev;
450 struct pci_sriov *iov = dev->sriov;
Ram Paibbef98a2011-11-06 10:33:10 +0800451 int bars = 0;
Wei Yangb07579c2015-03-25 16:23:48 +0800452 int bus;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800453
454 if (!nr_virtfn)
455 return 0;
456
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700457 if (iov->num_VFs)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800458 return -EINVAL;
459
460 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700461 if (initial > iov->total_VFs ||
462 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
Yu Zhaodd7cc442009-03-20 11:25:15 +0800463 return -EIO;
464
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700465 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
Yu Zhaodd7cc442009-03-20 11:25:15 +0800466 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
467 return -EINVAL;
468
Yu Zhaodd7cc442009-03-20 11:25:15 +0800469 nres = 0;
470 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Ram Paibbef98a2011-11-06 10:33:10 +0800471 bars |= (1 << (i + PCI_IOV_RESOURCES));
Bjorn Helgaasc1fe1f92015-03-25 16:23:45 +0800472 res = &dev->resource[i + PCI_IOV_RESOURCES];
Yu Zhaodd7cc442009-03-20 11:25:15 +0800473 if (res->parent)
474 nres++;
475 }
476 if (nres != iov->nres) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600477 pci_err(dev, "not enough MMIO resources for SR-IOV\n");
Yu Zhaodd7cc442009-03-20 11:25:15 +0800478 return -ENOMEM;
479 }
480
Wei Yangb07579c2015-03-25 16:23:48 +0800481 bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
Bjorn Helgaas68f8e9f2015-03-25 16:23:42 +0800482 if (bus > dev->bus->busn_res.end) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600483 pci_err(dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
Bjorn Helgaas68f8e9f2015-03-25 16:23:42 +0800484 nr_virtfn, bus, &dev->bus->busn_res);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800485 return -ENOMEM;
486 }
487
Ram Paibbef98a2011-11-06 10:33:10 +0800488 if (pci_enable_resources(dev, bars)) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600489 pci_err(dev, "SR-IOV: IOV BARS not allocated\n");
Ram Paibbef98a2011-11-06 10:33:10 +0800490 return -ENOMEM;
491 }
492
Yu Zhaodd7cc442009-03-20 11:25:15 +0800493 if (iov->link != dev->devfn) {
494 pdev = pci_get_slot(dev->bus, iov->link);
495 if (!pdev)
496 return -ENODEV;
497
Jiang Liudc087f22013-05-25 21:48:37 +0800498 if (!pdev->is_physfn) {
499 pci_dev_put(pdev);
Stefan Assmann652d1102013-07-31 16:47:56 -0600500 return -ENOSYS;
Jiang Liudc087f22013-05-25 21:48:37 +0800501 }
Yu Zhaodd7cc442009-03-20 11:25:15 +0800502
503 rc = sysfs_create_link(&dev->dev.kobj,
504 &pdev->dev.kobj, "dep_link");
Jiang Liudc087f22013-05-25 21:48:37 +0800505 pci_dev_put(pdev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800506 if (rc)
507 return rc;
508 }
509
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700510 iov->initial_VFs = initial;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800511 if (nr_virtfn < initial)
512 initial = nr_virtfn;
513
Alexander Duyckc23b6132015-10-29 16:21:20 -0500514 rc = pcibios_sriov_enable(dev, initial);
515 if (rc) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600516 pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc);
Alexander Duyckc23b6132015-10-29 16:21:20 -0500517 goto err_pcibios;
Wei Yang995df522015-03-25 16:23:49 +0800518 }
519
Gavin Shanf40ec3c2016-10-26 12:15:35 +1100520 pci_iov_set_numvfs(dev, nr_virtfn);
521 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
522 pci_cfg_access_lock(dev);
523 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
524 msleep(100);
525 pci_cfg_access_unlock(dev);
526
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100527 rc = sriov_add_vfs(dev, initial);
528 if (rc)
529 goto err_pcibios;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800530
531 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700532 iov->num_VFs = nr_virtfn;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800533
534 return 0;
535
Alexander Duyckc23b6132015-10-29 16:21:20 -0500536err_pcibios:
Yu Zhaodd7cc442009-03-20 11:25:15 +0800537 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100538 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800539 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
540 ssleep(1);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100541 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800542
Gavin Shan0fc690a2017-08-11 18:19:33 +1000543 pcibios_sriov_disable(dev);
544
Yu Zhaodd7cc442009-03-20 11:25:15 +0800545 if (iov->link != dev->devfn)
546 sysfs_remove_link(&dev->dev.kobj, "dep_link");
547
Alexander Duyckb3908642015-10-29 16:21:16 -0500548 pci_iov_set_numvfs(dev, 0);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800549 return rc;
550}
551
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100552static void sriov_del_vfs(struct pci_dev *dev)
553{
554 struct pci_sriov *iov = dev->sriov;
555 int i;
556
Sebastian Ottaff68a5a2018-12-21 15:14:19 +0100557 if (dev->no_vf_scan)
558 return;
559
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100560 for (i = 0; i < iov->num_VFs; i++)
561 pci_iov_remove_virtfn(dev, i);
562}
563
Yu Zhaodd7cc442009-03-20 11:25:15 +0800564static void sriov_disable(struct pci_dev *dev)
565{
Yu Zhaodd7cc442009-03-20 11:25:15 +0800566 struct pci_sriov *iov = dev->sriov;
567
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700568 if (!iov->num_VFs)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800569 return;
570
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100571 sriov_del_vfs(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800572 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100573 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800574 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
575 ssleep(1);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100576 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800577
Gavin Shan0fc690a2017-08-11 18:19:33 +1000578 pcibios_sriov_disable(dev);
579
Yu Zhaodd7cc442009-03-20 11:25:15 +0800580 if (iov->link != dev->devfn)
581 sysfs_remove_link(&dev->dev.kobj, "dep_link");
582
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700583 iov->num_VFs = 0;
Wei Yangf59dca22015-03-25 16:23:46 +0800584 pci_iov_set_numvfs(dev, 0);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800585}
586
Yu Zhaod1b054d2009-03-20 11:25:11 +0800587static int sriov_init(struct pci_dev *dev, int pos)
588{
Wei Yang0e6c9122015-03-25 16:23:44 +0800589 int i, bar64;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800590 int rc;
591 int nres;
592 u32 pgsz;
Alexander Duyckea9a8852015-10-29 16:20:50 -0500593 u16 ctrl, total;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800594 struct pci_sriov *iov;
595 struct resource *res;
596 struct pci_dev *pdev;
597
Yu Zhaod1b054d2009-03-20 11:25:11 +0800598 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
599 if (ctrl & PCI_SRIOV_CTRL_VFE) {
600 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
601 ssleep(1);
602 }
603
Yu Zhaod1b054d2009-03-20 11:25:11 +0800604 ctrl = 0;
605 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
606 if (pdev->is_physfn)
607 goto found;
608
609 pdev = NULL;
610 if (pci_ari_enabled(dev->bus))
611 ctrl |= PCI_SRIOV_CTRL_ARI;
612
613found:
614 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800615
Ben Sheltonff45f9d2015-10-29 16:20:31 -0500616 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
617 if (!total)
618 return 0;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800619
620 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
621 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
622 pgsz &= ~((1 << i) - 1);
623 if (!pgsz)
624 return -EIO;
625
626 pgsz &= ~(pgsz - 1);
Vaidyanathan Srinivasan8161fe92012-02-02 23:11:20 +0530627 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800628
Wei Yang0e6c9122015-03-25 16:23:44 +0800629 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
630 if (!iov)
631 return -ENOMEM;
632
Yu Zhaod1b054d2009-03-20 11:25:11 +0800633 nres = 0;
634 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Bjorn Helgaasc1fe1f92015-03-25 16:23:45 +0800635 res = &dev->resource[i + PCI_IOV_RESOURCES];
David Daney11183992015-10-29 17:35:40 -0500636 /*
637 * If it is already FIXED, don't change it, something
638 * (perhaps EA or header fixups) wants it this way.
639 */
640 if (res->flags & IORESOURCE_PCI_FIXED)
641 bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
642 else
643 bar64 = __pci_read_base(dev, pci_bar_unknown, res,
644 pos + PCI_SRIOV_BAR + i * 4);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800645 if (!res->flags)
646 continue;
647 if (resource_size(res) & (PAGE_SIZE - 1)) {
648 rc = -EIO;
649 goto failed;
650 }
Wei Yang0e6c9122015-03-25 16:23:44 +0800651 iov->barsz[i] = resource_size(res);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800652 res->end = res->start + resource_size(res) * total - 1;
Frederick Lawler7506dc72018-01-18 12:55:24 -0600653 pci_info(dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
Wei Yange88ae012015-03-25 16:23:43 +0800654 i, res, i, total);
Wei Yang0e6c9122015-03-25 16:23:44 +0800655 i += bar64;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800656 nres++;
657 }
658
Yu Zhaod1b054d2009-03-20 11:25:11 +0800659 iov->pos = pos;
660 iov->nres = nres;
661 iov->ctrl = ctrl;
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700662 iov->total_VFs = total;
Jakub Kicinski8d85a7a2018-05-25 08:18:34 -0500663 iov->driver_max_VFs = total;
Filippo Sironi3142d832017-08-28 15:38:49 +0200664 pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800665 iov->pgsz = pgsz;
666 iov->self = dev;
Bodong Wang0e7df222017-04-13 01:51:40 +0300667 iov->drivers_autoprobe = true;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800668 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
669 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
Yijing Wang62f87c02012-07-24 17:20:03 +0800670 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
Yu Zhao4d135db2009-05-20 17:11:57 +0800671 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800672
673 if (pdev)
674 iov->dev = pci_dev_get(pdev);
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800675 else
Yu Zhaod1b054d2009-03-20 11:25:11 +0800676 iov->dev = dev;
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800677
Yu Zhaod1b054d2009-03-20 11:25:11 +0800678 dev->sriov = iov;
679 dev->is_physfn = 1;
Alexander Duyckea9a8852015-10-29 16:20:50 -0500680 rc = compute_max_vf_buses(dev);
681 if (rc)
682 goto fail_max_buses;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800683
684 return 0;
685
Alexander Duyckea9a8852015-10-29 16:20:50 -0500686fail_max_buses:
687 dev->sriov = NULL;
688 dev->is_physfn = 0;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800689failed:
690 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Bjorn Helgaasc1fe1f92015-03-25 16:23:45 +0800691 res = &dev->resource[i + PCI_IOV_RESOURCES];
Yu Zhaod1b054d2009-03-20 11:25:11 +0800692 res->flags = 0;
693 }
694
Wei Yang0e6c9122015-03-25 16:23:44 +0800695 kfree(iov);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800696 return rc;
697}
698
699static void sriov_release(struct pci_dev *dev)
700{
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700701 BUG_ON(dev->sriov->num_VFs);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800702
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800703 if (dev != dev->sriov->dev)
Yu Zhaod1b054d2009-03-20 11:25:11 +0800704 pci_dev_put(dev->sriov->dev);
705
706 kfree(dev->sriov);
707 dev->sriov = NULL;
708}
709
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800710static void sriov_restore_state(struct pci_dev *dev)
711{
712 int i;
713 u16 ctrl;
714 struct pci_sriov *iov = dev->sriov;
715
716 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
717 if (ctrl & PCI_SRIOV_CTRL_VFE)
718 return;
719
Tony Nguyenff264492017-10-04 08:52:58 -0700720 /*
721 * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
722 * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
723 */
724 ctrl &= ~PCI_SRIOV_CTRL_ARI;
725 ctrl |= iov->ctrl & PCI_SRIOV_CTRL_ARI;
726 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
727
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800728 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
729 pci_update_resource(dev, i);
730
731 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
Wei Yangf59dca22015-03-25 16:23:46 +0800732 pci_iov_set_numvfs(dev, iov->num_VFs);
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800733 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
734 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
735 msleep(100);
736}
737
Yu Zhaod1b054d2009-03-20 11:25:11 +0800738/**
739 * pci_iov_init - initialize the IOV capability
740 * @dev: the PCI device
741 *
742 * Returns 0 on success, or negative on failure.
743 */
744int pci_iov_init(struct pci_dev *dev)
745{
746 int pos;
747
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900748 if (!pci_is_pcie(dev))
Yu Zhaod1b054d2009-03-20 11:25:11 +0800749 return -ENODEV;
750
751 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
752 if (pos)
753 return sriov_init(dev, pos);
754
755 return -ENODEV;
756}
757
758/**
759 * pci_iov_release - release resources used by the IOV capability
760 * @dev: the PCI device
761 */
762void pci_iov_release(struct pci_dev *dev)
763{
764 if (dev->is_physfn)
765 sriov_release(dev);
766}
767
768/**
Jakub Kicinski38972372018-06-29 15:08:52 -0500769 * pci_iov_remove - clean up SR-IOV state after PF driver is detached
770 * @dev: the PCI device
771 */
772void pci_iov_remove(struct pci_dev *dev)
773{
774 struct pci_sriov *iov = dev->sriov;
775
776 if (!dev->is_physfn)
777 return;
778
779 iov->driver_max_VFs = iov->total_VFs;
780 if (iov->num_VFs)
781 pci_warn(dev, "driver left SR-IOV enabled after remove\n");
782}
783
784/**
Bjorn Helgaas6ffa2482016-11-28 09:15:52 -0600785 * pci_iov_update_resource - update a VF BAR
786 * @dev: the PCI device
787 * @resno: the resource number
788 *
789 * Update a VF BAR in the SR-IOV capability of a PF.
790 */
791void pci_iov_update_resource(struct pci_dev *dev, int resno)
792{
793 struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
794 struct resource *res = dev->resource + resno;
795 int vf_bar = resno - PCI_IOV_RESOURCES;
796 struct pci_bus_region region;
Bjorn Helgaas546ba9f2016-11-28 16:43:06 -0600797 u16 cmd;
Bjorn Helgaas6ffa2482016-11-28 09:15:52 -0600798 u32 new;
799 int reg;
800
801 /*
802 * The generic pci_restore_bars() path calls this for all devices,
803 * including VFs and non-SR-IOV devices. If this is not a PF, we
804 * have nothing to do.
805 */
806 if (!iov)
807 return;
808
Bjorn Helgaas546ba9f2016-11-28 16:43:06 -0600809 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
810 if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) {
811 dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
812 vf_bar, res);
813 return;
814 }
815
Bjorn Helgaas6ffa2482016-11-28 09:15:52 -0600816 /*
817 * Ignore unimplemented BARs, unused resource slots for 64-bit
818 * BARs, and non-movable resources, e.g., those described via
819 * Enhanced Allocation.
820 */
821 if (!res->flags)
822 return;
823
824 if (res->flags & IORESOURCE_UNSET)
825 return;
826
827 if (res->flags & IORESOURCE_PCI_FIXED)
828 return;
829
830 pcibios_resource_to_bus(dev->bus, &region, res);
831 new = region.start;
832 new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK;
833
834 reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar;
835 pci_write_config_dword(dev, reg, new);
836 if (res->flags & IORESOURCE_MEM_64) {
837 new = region.start >> 16 >> 16;
838 pci_write_config_dword(dev, reg + 4, new);
839 }
840}
841
Wei Yang978d2d62015-03-25 16:23:50 +0800842resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
843 int resno)
844{
845 return pci_iov_resource_size(dev, resno);
846}
847
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800848/**
Chris Wright6faf17f2009-08-28 13:00:06 -0700849 * pci_sriov_resource_alignment - get resource alignment for VF BAR
850 * @dev: the PCI device
851 * @resno: the resource number
852 *
853 * Returns the alignment of the VF BAR found in the SR-IOV capability.
854 * This is not the same as the resource size which is defined as
855 * the VF BAR size multiplied by the number of VFs. The alignment
856 * is just the VF BAR size.
857 */
Cam Macdonell0e522472010-09-07 17:25:20 -0700858resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
Chris Wright6faf17f2009-08-28 13:00:06 -0700859{
Wei Yang978d2d62015-03-25 16:23:50 +0800860 return pcibios_iov_resource_alignment(dev, resno);
Chris Wright6faf17f2009-08-28 13:00:06 -0700861}
862
863/**
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800864 * pci_restore_iov_state - restore the state of the IOV capability
865 * @dev: the PCI device
866 */
867void pci_restore_iov_state(struct pci_dev *dev)
868{
869 if (dev->is_physfn)
870 sriov_restore_state(dev);
871}
Yu Zhaoa28724b2009-03-20 11:25:13 +0800872
873/**
Bryant G. Ly608c0d82017-11-09 08:00:35 -0600874 * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs
875 * @dev: the PCI device
876 * @auto_probe: set VF drivers auto probe flag
877 */
878void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe)
879{
880 if (dev->is_physfn)
881 dev->sriov->drivers_autoprobe = auto_probe;
882}
883
884/**
Yu Zhaoa28724b2009-03-20 11:25:13 +0800885 * pci_iov_bus_range - find bus range used by Virtual Function
886 * @bus: the PCI bus
887 *
888 * Returns max number of buses (exclude current one) used by Virtual
889 * Functions.
890 */
891int pci_iov_bus_range(struct pci_bus *bus)
892{
893 int max = 0;
Yu Zhaoa28724b2009-03-20 11:25:13 +0800894 struct pci_dev *dev;
895
896 list_for_each_entry(dev, &bus->devices, bus_list) {
897 if (!dev->is_physfn)
898 continue;
Wei Yang4449f072015-03-25 16:23:47 +0800899 if (dev->sriov->max_VF_buses > max)
900 max = dev->sriov->max_VF_buses;
Yu Zhaoa28724b2009-03-20 11:25:13 +0800901 }
902
903 return max ? max - bus->number : 0;
904}
Yu Zhaodd7cc442009-03-20 11:25:15 +0800905
906/**
907 * pci_enable_sriov - enable the SR-IOV capability
908 * @dev: the PCI device
Randy Dunlap52a88732009-04-01 17:45:30 -0700909 * @nr_virtfn: number of virtual functions to enable
Yu Zhaodd7cc442009-03-20 11:25:15 +0800910 *
911 * Returns 0 on success, or negative on failure.
912 */
913int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
914{
915 might_sleep();
916
917 if (!dev->is_physfn)
Stefan Assmann652d1102013-07-31 16:47:56 -0600918 return -ENOSYS;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800919
920 return sriov_enable(dev, nr_virtfn);
921}
922EXPORT_SYMBOL_GPL(pci_enable_sriov);
923
924/**
925 * pci_disable_sriov - disable the SR-IOV capability
926 * @dev: the PCI device
927 */
928void pci_disable_sriov(struct pci_dev *dev)
929{
930 might_sleep();
931
932 if (!dev->is_physfn)
933 return;
934
935 sriov_disable(dev);
936}
937EXPORT_SYMBOL_GPL(pci_disable_sriov);
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800938
939/**
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000940 * pci_num_vf - return number of VFs associated with a PF device_release_driver
941 * @dev: the PCI device
942 *
943 * Returns number of VFs, or 0 if SR-IOV is not enabled.
944 */
945int pci_num_vf(struct pci_dev *dev)
946{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700947 if (!dev->is_physfn)
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000948 return 0;
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700949
950 return dev->sriov->num_VFs;
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000951}
952EXPORT_SYMBOL_GPL(pci_num_vf);
Donald Dutilebff73152012-11-05 15:20:37 -0500953
954/**
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000955 * pci_vfs_assigned - returns number of VFs are assigned to a guest
956 * @dev: the PCI device
957 *
958 * Returns number of VFs belonging to this device that are assigned to a guest.
Stefan Assmann652d1102013-07-31 16:47:56 -0600959 * If device is not a physical function returns 0.
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000960 */
961int pci_vfs_assigned(struct pci_dev *dev)
962{
963 struct pci_dev *vfdev;
964 unsigned int vfs_assigned = 0;
965 unsigned short dev_id;
966
967 /* only search if we are a PF */
968 if (!dev->is_physfn)
969 return 0;
970
971 /*
972 * determine the device ID for the VFs, the vendor ID will be the
973 * same as the PF so there is no need to check for that one
974 */
Filippo Sironi3142d832017-08-28 15:38:49 +0200975 dev_id = dev->sriov->vf_device;
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000976
977 /* loop through all the VFs to see if we own any that are assigned */
978 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
979 while (vfdev) {
980 /*
981 * It is considered assigned if it is a virtual function with
982 * our dev as the physical function and the assigned bit is set
983 */
984 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
Ethan Zhaobe634972014-09-09 10:21:28 +0800985 pci_is_dev_assigned(vfdev))
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000986 vfs_assigned++;
987
988 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
989 }
990
991 return vfs_assigned;
992}
993EXPORT_SYMBOL_GPL(pci_vfs_assigned);
994
995/**
Donald Dutilebff73152012-11-05 15:20:37 -0500996 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
997 * @dev: the PCI PF device
Randy Dunlap2094f162013-01-09 17:12:52 -0800998 * @numvfs: number that should be used for TotalVFs supported
Donald Dutilebff73152012-11-05 15:20:37 -0500999 *
1000 * Should be called from PF driver's probe routine with
1001 * device's mutex held.
1002 *
1003 * Returns 0 if PF is an SRIOV-capable device and
Stefan Assmann652d1102013-07-31 16:47:56 -06001004 * value of numvfs valid. If not a PF return -ENOSYS;
1005 * if numvfs is invalid return -EINVAL;
Donald Dutilebff73152012-11-05 15:20:37 -05001006 * if VFs already enabled, return -EBUSY.
1007 */
1008int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
1009{
Stefan Assmann652d1102013-07-31 16:47:56 -06001010 if (!dev->is_physfn)
1011 return -ENOSYS;
Bjorn Helgaas51259d02018-05-25 08:51:50 -05001012
Stefan Assmann652d1102013-07-31 16:47:56 -06001013 if (numvfs > dev->sriov->total_VFs)
Donald Dutilebff73152012-11-05 15:20:37 -05001014 return -EINVAL;
1015
1016 /* Shouldn't change if VFs already enabled */
1017 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
1018 return -EBUSY;
Donald Dutilebff73152012-11-05 15:20:37 -05001019
Bjorn Helgaas51259d02018-05-25 08:51:50 -05001020 dev->sriov->driver_max_VFs = numvfs;
Donald Dutilebff73152012-11-05 15:20:37 -05001021 return 0;
1022}
1023EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
1024
1025/**
Jonghwan Choiddc191f2013-07-08 14:02:43 -06001026 * pci_sriov_get_totalvfs -- get total VFs supported on this device
Donald Dutilebff73152012-11-05 15:20:37 -05001027 * @dev: the PCI PF device
1028 *
1029 * For a PCIe device with SRIOV support, return the PCIe
Bjorn Helgaas6b136722012-11-09 20:27:53 -07001030 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
Stefan Assmann652d1102013-07-31 16:47:56 -06001031 * if the driver reduced it. Otherwise 0.
Donald Dutilebff73152012-11-05 15:20:37 -05001032 */
1033int pci_sriov_get_totalvfs(struct pci_dev *dev)
1034{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -07001035 if (!dev->is_physfn)
Stefan Assmann652d1102013-07-31 16:47:56 -06001036 return 0;
Donald Dutilebff73152012-11-05 15:20:37 -05001037
Jakub Kicinski8d85a7a2018-05-25 08:18:34 -05001038 return dev->sriov->driver_max_VFs;
Donald Dutilebff73152012-11-05 15:20:37 -05001039}
1040EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);
Alexander Duyck8effc392018-04-21 15:23:09 -05001041
1042/**
1043 * pci_sriov_configure_simple - helper to configure SR-IOV
1044 * @dev: the PCI device
1045 * @nr_virtfn: number of virtual functions to enable, 0 to disable
1046 *
1047 * Enable or disable SR-IOV for devices that don't require any PF setup
1048 * before enabling SR-IOV. Return value is negative on error, or number of
1049 * VFs allocated on success.
1050 */
1051int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn)
1052{
1053 int rc;
1054
1055 might_sleep();
1056
1057 if (!dev->is_physfn)
1058 return -ENODEV;
1059
1060 if (pci_vfs_assigned(dev)) {
1061 pci_warn(dev, "Cannot modify SR-IOV while VFs are assigned\n");
1062 return -EPERM;
1063 }
1064
1065 if (nr_virtfn == 0) {
1066 sriov_disable(dev);
1067 return 0;
1068 }
1069
1070 rc = sriov_enable(dev, nr_virtfn);
1071 if (rc < 0)
1072 return rc;
1073
1074 return nr_virtfn;
1075}
1076EXPORT_SYMBOL_GPL(pci_sriov_configure_simple);