blob: 4afd4ee4f7f04f8d4aed5c7d0f9fc79dd547be0d [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>
Paul Gortmaker363c75d2011-05-27 09:37:25 -040012#include <linux/export.h>
Yu Zhaod1b054d2009-03-20 11:25:11 +080013#include <linux/string.h>
14#include <linux/delay.h>
15#include "pci.h"
16
Yu Zhaodd7cc442009-03-20 11:25:15 +080017#define VIRTFN_ID_LEN 16
Yu Zhaod1b054d2009-03-20 11:25:11 +080018
Wei Yangb07579c2015-03-25 16:23:48 +080019int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id)
Yu Zhaoa28724b2009-03-20 11:25:13 +080020{
Wei Yangb07579c2015-03-25 16:23:48 +080021 if (!dev->is_physfn)
22 return -EINVAL;
Yu Zhaoa28724b2009-03-20 11:25:13 +080023 return dev->bus->number + ((dev->devfn + dev->sriov->offset +
Wei Yangb07579c2015-03-25 16:23:48 +080024 dev->sriov->stride * vf_id) >> 8);
Yu Zhaoa28724b2009-03-20 11:25:13 +080025}
26
Wei Yangb07579c2015-03-25 16:23:48 +080027int pci_iov_virtfn_devfn(struct pci_dev *dev, int vf_id)
Yu Zhaoa28724b2009-03-20 11:25:13 +080028{
Wei Yangb07579c2015-03-25 16:23:48 +080029 if (!dev->is_physfn)
30 return -EINVAL;
Yu Zhaoa28724b2009-03-20 11:25:13 +080031 return (dev->devfn + dev->sriov->offset +
Wei Yangb07579c2015-03-25 16:23:48 +080032 dev->sriov->stride * vf_id) & 0xff;
Yu Zhaoa28724b2009-03-20 11:25:13 +080033}
34
Wei Yangf59dca22015-03-25 16:23:46 +080035/*
36 * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
37 * change when NumVFs changes.
38 *
39 * Update iov->offset and iov->stride when NumVFs is written.
40 */
41static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
42{
43 struct pci_sriov *iov = dev->sriov;
44
45 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
46 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
47 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
48}
49
Wei Yang4449f0792015-03-25 16:23:47 +080050/*
51 * The PF consumes one bus number. NumVFs, First VF Offset, and VF Stride
52 * determine how many additional bus numbers will be consumed by VFs.
53 *
Alexander Duyckea9a8852015-10-29 16:20:50 -050054 * Iterate over all valid NumVFs, validate offset and stride, and calculate
55 * the maximum number of bus numbers that could ever be required.
Wei Yang4449f0792015-03-25 16:23:47 +080056 */
Alexander Duyckea9a8852015-10-29 16:20:50 -050057static int compute_max_vf_buses(struct pci_dev *dev)
Wei Yang4449f0792015-03-25 16:23:47 +080058{
59 struct pci_sriov *iov = dev->sriov;
Alexander Duyckea9a8852015-10-29 16:20:50 -050060 int nr_virtfn, busnr, rc = 0;
Wei Yang4449f0792015-03-25 16:23:47 +080061
Alexander Duyckea9a8852015-10-29 16:20:50 -050062 for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) {
Wei Yang4449f0792015-03-25 16:23:47 +080063 pci_iov_set_numvfs(dev, nr_virtfn);
Alexander Duyckea9a8852015-10-29 16:20:50 -050064 if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) {
65 rc = -EIO;
66 goto out;
67 }
68
Wei Yangb07579c2015-03-25 16:23:48 +080069 busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
Alexander Duyckea9a8852015-10-29 16:20:50 -050070 if (busnr > iov->max_VF_buses)
71 iov->max_VF_buses = busnr;
Wei Yang4449f0792015-03-25 16:23:47 +080072 }
73
Alexander Duyckea9a8852015-10-29 16:20:50 -050074out:
75 pci_iov_set_numvfs(dev, 0);
76 return rc;
Wei Yang4449f0792015-03-25 16:23:47 +080077}
78
Yu Zhaodd7cc442009-03-20 11:25:15 +080079static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
80{
Yu Zhaodd7cc442009-03-20 11:25:15 +080081 struct pci_bus *child;
82
83 if (bus->number == busnr)
84 return bus;
85
86 child = pci_find_bus(pci_domain_nr(bus), busnr);
87 if (child)
88 return child;
89
90 child = pci_add_new_bus(bus, NULL, busnr);
91 if (!child)
92 return NULL;
93
Yinghai Lub7eac052012-05-17 18:51:13 -070094 pci_bus_insert_busn_res(child, busnr, busnr);
Yu Zhaodd7cc442009-03-20 11:25:15 +080095
96 return child;
97}
98
Jiang Liudc087f22013-05-25 21:48:37 +080099static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800100{
Jiang Liudc087f22013-05-25 21:48:37 +0800101 if (physbus != virtbus && list_empty(&virtbus->devices))
102 pci_remove_bus(virtbus);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800103}
104
Wei Yang0e6c9122015-03-25 16:23:44 +0800105resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
106{
107 if (!dev->is_physfn)
108 return 0;
109
110 return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
111}
112
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +0100113static void pci_read_vf_config_common(struct pci_dev *virtfn)
114{
115 struct pci_dev *physfn = virtfn->physfn;
116
117 /*
118 * Some config registers are the same across all associated VFs.
119 * Read them once from VF0 so we can skip reading them from the
120 * other VFs.
121 *
122 * PCIe r4.0, sec 9.3.4.1, technically doesn't require all VFs to
123 * have the same Revision ID and Subsystem ID, but we assume they
124 * do.
125 */
126 pci_read_config_dword(virtfn, PCI_CLASS_REVISION,
127 &physfn->sriov->class);
128 pci_read_config_byte(virtfn, PCI_HEADER_TYPE,
129 &physfn->sriov->hdr_type);
130 pci_read_config_word(virtfn, PCI_SUBSYSTEM_VENDOR_ID,
131 &physfn->sriov->subsystem_vendor);
132 pci_read_config_word(virtfn, PCI_SUBSYSTEM_ID,
133 &physfn->sriov->subsystem_device);
134}
135
Niklas Schnellea1ceea62020-04-27 16:25:27 +0200136int pci_iov_sysfs_link(struct pci_dev *dev,
137 struct pci_dev *virtfn, int id)
138{
139 char buf[VIRTFN_ID_LEN];
140 int rc;
141
142 sprintf(buf, "virtfn%u", id);
143 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
144 if (rc)
145 goto failed;
146 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
147 if (rc)
148 goto failed1;
149
150 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
151
152 return 0;
153
154failed1:
155 sysfs_remove_link(&dev->dev.kobj, buf);
156failed:
157 return rc;
158}
159
Jan H. Schönherr753f6122017-09-26 12:53:23 -0500160int pci_iov_add_virtfn(struct pci_dev *dev, int id)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800161{
162 int i;
Jiang Liudc087f22013-05-25 21:48:37 +0800163 int rc = -ENOMEM;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800164 u64 size;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800165 struct pci_dev *virtfn;
166 struct resource *res;
167 struct pci_sriov *iov = dev->sriov;
Gu Zheng8b1fce02013-05-25 21:48:31 +0800168 struct pci_bus *bus;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800169
Wei Yangb07579c2015-03-25 16:23:48 +0800170 bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
Jiang Liudc087f22013-05-25 21:48:37 +0800171 if (!bus)
172 goto failed;
173
174 virtfn = pci_alloc_dev(bus);
175 if (!virtfn)
176 goto failed0;
177
Wei Yangb07579c2015-03-25 16:23:48 +0800178 virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800179 virtfn->vendor = dev->vendor;
Filippo Sironi3142d832017-08-28 15:38:49 +0200180 virtfn->device = iov->vf_device;
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +0100181 virtfn->is_virtfn = 1;
182 virtfn->physfn = pci_dev_get(dev);
Matthew Rosato12856e72020-09-10 10:59:55 -0400183 virtfn->no_command_memory = 1;
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +0100184
185 if (id == 0)
186 pci_read_vf_config_common(virtfn);
187
Po Liu156c5532016-08-29 15:28:01 +0800188 rc = pci_setup_device(virtfn);
189 if (rc)
KarimAllah Ahmedcf0921b2018-03-19 21:06:00 +0100190 goto failed1;
Po Liu156c5532016-08-29 15:28:01 +0800191
Yu Zhaodd7cc442009-03-20 11:25:15 +0800192 virtfn->dev.parent = dev->dev.parent;
Alex Williamsonaa9319772014-01-09 08:36:08 -0700193 virtfn->multifunction = 0;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800194
195 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Bjorn Helgaasc1fe1f92015-03-25 16:23:45 +0800196 res = &dev->resource[i + PCI_IOV_RESOURCES];
Yu Zhaodd7cc442009-03-20 11:25:15 +0800197 if (!res->parent)
198 continue;
199 virtfn->resource[i].name = pci_name(virtfn);
200 virtfn->resource[i].flags = res->flags;
Wei Yang0e6c9122015-03-25 16:23:44 +0800201 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800202 virtfn->resource[i].start = res->start + size * id;
203 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
204 rc = request_resource(res, &virtfn->resource[i]);
205 BUG_ON(rc);
206 }
207
Yu Zhaodd7cc442009-03-20 11:25:15 +0800208 pci_device_add(virtfn, virtfn->bus);
Niklas Schnellea1ceea62020-04-27 16:25:27 +0200209 rc = pci_iov_sysfs_link(dev, virtfn, id);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800210 if (rc)
Navid Emamdoost8c386cc2019-11-25 13:52:52 -0600211 goto failed1;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800212
Stuart Hayes27d61622017-10-04 10:57:52 -0500213 pci_bus_add_device(virtfn);
214
Yu Zhaodd7cc442009-03-20 11:25:15 +0800215 return 0;
216
Yu Zhaodd7cc442009-03-20 11:25:15 +0800217failed1:
Navid Emamdoost8c386cc2019-11-25 13:52:52 -0600218 pci_stop_and_remove_bus_device(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800219 pci_dev_put(dev);
Jiang Liudc087f22013-05-25 21:48:37 +0800220failed0:
221 virtfn_remove_bus(dev->bus, bus);
222failed:
Yu Zhaodd7cc442009-03-20 11:25:15 +0800223
224 return rc;
225}
226
Jan H. Schönherr753f6122017-09-26 12:53:23 -0500227void pci_iov_remove_virtfn(struct pci_dev *dev, int id)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800228{
229 char buf[VIRTFN_ID_LEN];
Yu Zhaodd7cc442009-03-20 11:25:15 +0800230 struct pci_dev *virtfn;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800231
Jiang Liudc087f22013-05-25 21:48:37 +0800232 virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
Wei Yangb07579c2015-03-25 16:23:48 +0800233 pci_iov_virtfn_bus(dev, id),
234 pci_iov_virtfn_devfn(dev, id));
Yu Zhaodd7cc442009-03-20 11:25:15 +0800235 if (!virtfn)
236 return;
237
Yu Zhaodd7cc442009-03-20 11:25:15 +0800238 sprintf(buf, "virtfn%u", id);
239 sysfs_remove_link(&dev->dev.kobj, buf);
Yinghai Lu09cedbe2012-02-04 22:55:01 -0800240 /*
241 * pci_stop_dev() could have been called for this virtfn already,
242 * so the directory for the virtfn may have been removed before.
243 * Double check to avoid spurious sysfs warnings.
244 */
245 if (virtfn->dev.kobj.sd)
246 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
Yu Zhaodd7cc442009-03-20 11:25:15 +0800247
Yinghai Lu210647a2012-02-25 13:54:20 -0800248 pci_stop_and_remove_bus_device(virtfn);
Jiang Liudc087f22013-05-25 21:48:37 +0800249 virtfn_remove_bus(dev->bus, virtfn->bus);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800250
Jiang Liudc087f22013-05-25 21:48:37 +0800251 /* balance pci_get_domain_bus_and_slot() */
252 pci_dev_put(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800253 pci_dev_put(dev);
254}
255
Kelsey Skunbergaaee0c12019-08-13 14:45:13 -0600256static ssize_t sriov_totalvfs_show(struct device *dev,
257 struct device_attribute *attr,
258 char *buf)
259{
260 struct pci_dev *pdev = to_pci_dev(dev);
261
262 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
263}
264
265static ssize_t sriov_numvfs_show(struct device *dev,
266 struct device_attribute *attr,
267 char *buf)
268{
269 struct pci_dev *pdev = to_pci_dev(dev);
Pierre Crégut35ff8672019-09-11 09:27:36 +0200270 u16 num_vfs;
Kelsey Skunbergaaee0c12019-08-13 14:45:13 -0600271
Pierre Crégut35ff8672019-09-11 09:27:36 +0200272 /* Serialize vs sriov_numvfs_store() so readers see valid num_VFs */
273 device_lock(&pdev->dev);
274 num_vfs = pdev->sriov->num_VFs;
275 device_unlock(&pdev->dev);
276
277 return sprintf(buf, "%u\n", num_vfs);
Kelsey Skunbergaaee0c12019-08-13 14:45:13 -0600278}
279
280/*
281 * num_vfs > 0; number of VFs to enable
282 * num_vfs = 0; disable all VFs
283 *
284 * Note: SRIOV spec does not allow partial VF
285 * disable, so it's all or none.
286 */
287static ssize_t sriov_numvfs_store(struct device *dev,
288 struct device_attribute *attr,
289 const char *buf, size_t count)
290{
291 struct pci_dev *pdev = to_pci_dev(dev);
292 int ret;
293 u16 num_vfs;
294
295 ret = kstrtou16(buf, 0, &num_vfs);
296 if (ret < 0)
297 return ret;
298
299 if (num_vfs > pci_sriov_get_totalvfs(pdev))
300 return -ERANGE;
301
302 device_lock(&pdev->dev);
303
304 if (num_vfs == pdev->sriov->num_VFs)
305 goto exit;
306
307 /* is PF driver loaded w/callback */
308 if (!pdev->driver || !pdev->driver->sriov_configure) {
309 pci_info(pdev, "Driver does not support SRIOV configuration via sysfs\n");
310 ret = -ENOENT;
311 goto exit;
312 }
313
314 if (num_vfs == 0) {
315 /* disable VFs */
316 ret = pdev->driver->sriov_configure(pdev, 0);
317 goto exit;
318 }
319
320 /* enable VFs */
321 if (pdev->sriov->num_VFs) {
322 pci_warn(pdev, "%d VFs already enabled. Disable before enabling %d VFs\n",
323 pdev->sriov->num_VFs, num_vfs);
324 ret = -EBUSY;
325 goto exit;
326 }
327
328 ret = pdev->driver->sriov_configure(pdev, num_vfs);
329 if (ret < 0)
330 goto exit;
331
332 if (ret != num_vfs)
333 pci_warn(pdev, "%d VFs requested; only %d enabled\n",
334 num_vfs, ret);
335
336exit:
337 device_unlock(&pdev->dev);
338
339 if (ret < 0)
340 return ret;
341
342 return count;
343}
344
345static ssize_t sriov_offset_show(struct device *dev,
346 struct device_attribute *attr,
347 char *buf)
348{
349 struct pci_dev *pdev = to_pci_dev(dev);
350
351 return sprintf(buf, "%u\n", pdev->sriov->offset);
352}
353
354static ssize_t sriov_stride_show(struct device *dev,
355 struct device_attribute *attr,
356 char *buf)
357{
358 struct pci_dev *pdev = to_pci_dev(dev);
359
360 return sprintf(buf, "%u\n", pdev->sriov->stride);
361}
362
363static ssize_t sriov_vf_device_show(struct device *dev,
364 struct device_attribute *attr,
365 char *buf)
366{
367 struct pci_dev *pdev = to_pci_dev(dev);
368
369 return sprintf(buf, "%x\n", pdev->sriov->vf_device);
370}
371
372static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
373 struct device_attribute *attr,
374 char *buf)
375{
376 struct pci_dev *pdev = to_pci_dev(dev);
377
378 return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe);
379}
380
381static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
382 struct device_attribute *attr,
383 const char *buf, size_t count)
384{
385 struct pci_dev *pdev = to_pci_dev(dev);
386 bool drivers_autoprobe;
387
388 if (kstrtobool(buf, &drivers_autoprobe) < 0)
389 return -EINVAL;
390
391 pdev->sriov->drivers_autoprobe = drivers_autoprobe;
392
393 return count;
394}
395
396static DEVICE_ATTR_RO(sriov_totalvfs);
Kelsey Skunberg244c06c2019-09-05 00:32:26 -0600397static DEVICE_ATTR_RW(sriov_numvfs);
Kelsey Skunbergaaee0c12019-08-13 14:45:13 -0600398static DEVICE_ATTR_RO(sriov_offset);
399static DEVICE_ATTR_RO(sriov_stride);
400static DEVICE_ATTR_RO(sriov_vf_device);
Kelsey Skunberg244c06c2019-09-05 00:32:26 -0600401static DEVICE_ATTR_RW(sriov_drivers_autoprobe);
Kelsey Skunbergaaee0c12019-08-13 14:45:13 -0600402
403static struct attribute *sriov_dev_attrs[] = {
404 &dev_attr_sriov_totalvfs.attr,
405 &dev_attr_sriov_numvfs.attr,
406 &dev_attr_sriov_offset.attr,
407 &dev_attr_sriov_stride.attr,
408 &dev_attr_sriov_vf_device.attr,
409 &dev_attr_sriov_drivers_autoprobe.attr,
410 NULL,
411};
412
413static umode_t sriov_attrs_are_visible(struct kobject *kobj,
414 struct attribute *a, int n)
415{
416 struct device *dev = kobj_to_dev(kobj);
417
418 if (!dev_is_pf(dev))
419 return 0;
420
421 return a->mode;
422}
423
424const struct attribute_group sriov_dev_attr_group = {
425 .attrs = sriov_dev_attrs,
426 .is_visible = sriov_attrs_are_visible,
427};
428
Wei Yang995df522015-03-25 16:23:49 +0800429int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
430{
Alexander Duycka39e3fc2015-10-29 16:21:11 -0500431 return 0;
432}
433
434int __weak pcibios_sriov_disable(struct pci_dev *pdev)
435{
436 return 0;
Wei Yang995df522015-03-25 16:23:49 +0800437}
438
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100439static int sriov_add_vfs(struct pci_dev *dev, u16 num_vfs)
440{
441 unsigned int i;
442 int rc;
443
Sebastian Ottaff68a5a2018-12-21 15:14:19 +0100444 if (dev->no_vf_scan)
445 return 0;
446
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100447 for (i = 0; i < num_vfs; i++) {
448 rc = pci_iov_add_virtfn(dev, i);
449 if (rc)
450 goto failed;
451 }
452 return 0;
453failed:
454 while (i--)
455 pci_iov_remove_virtfn(dev, i);
456
457 return rc;
458}
459
Yu Zhaodd7cc442009-03-20 11:25:15 +0800460static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
461{
462 int rc;
Alexander Duyck3443c382015-10-29 16:21:05 -0500463 int i;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800464 int nres;
Alexander Duyckce288ec2015-10-29 16:20:57 -0500465 u16 initial;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800466 struct resource *res;
467 struct pci_dev *pdev;
468 struct pci_sriov *iov = dev->sriov;
Ram Paibbef98a2011-11-06 10:33:10 +0800469 int bars = 0;
Wei Yangb07579c2015-03-25 16:23:48 +0800470 int bus;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800471
472 if (!nr_virtfn)
473 return 0;
474
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700475 if (iov->num_VFs)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800476 return -EINVAL;
477
478 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700479 if (initial > iov->total_VFs ||
480 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
Yu Zhaodd7cc442009-03-20 11:25:15 +0800481 return -EIO;
482
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700483 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
Yu Zhaodd7cc442009-03-20 11:25:15 +0800484 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
485 return -EINVAL;
486
Yu Zhaodd7cc442009-03-20 11:25:15 +0800487 nres = 0;
488 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Ram Paibbef98a2011-11-06 10:33:10 +0800489 bars |= (1 << (i + PCI_IOV_RESOURCES));
Bjorn Helgaasc1fe1f92015-03-25 16:23:45 +0800490 res = &dev->resource[i + PCI_IOV_RESOURCES];
Yu Zhaodd7cc442009-03-20 11:25:15 +0800491 if (res->parent)
492 nres++;
493 }
494 if (nres != iov->nres) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600495 pci_err(dev, "not enough MMIO resources for SR-IOV\n");
Yu Zhaodd7cc442009-03-20 11:25:15 +0800496 return -ENOMEM;
497 }
498
Wei Yangb07579c2015-03-25 16:23:48 +0800499 bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
Bjorn Helgaas68f8e9f2015-03-25 16:23:42 +0800500 if (bus > dev->bus->busn_res.end) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600501 pci_err(dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
Bjorn Helgaas68f8e9f2015-03-25 16:23:42 +0800502 nr_virtfn, bus, &dev->bus->busn_res);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800503 return -ENOMEM;
504 }
505
Ram Paibbef98a2011-11-06 10:33:10 +0800506 if (pci_enable_resources(dev, bars)) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600507 pci_err(dev, "SR-IOV: IOV BARS not allocated\n");
Ram Paibbef98a2011-11-06 10:33:10 +0800508 return -ENOMEM;
509 }
510
Yu Zhaodd7cc442009-03-20 11:25:15 +0800511 if (iov->link != dev->devfn) {
512 pdev = pci_get_slot(dev->bus, iov->link);
513 if (!pdev)
514 return -ENODEV;
515
Jiang Liudc087f22013-05-25 21:48:37 +0800516 if (!pdev->is_physfn) {
517 pci_dev_put(pdev);
Stefan Assmann652d1102013-07-31 16:47:56 -0600518 return -ENOSYS;
Jiang Liudc087f22013-05-25 21:48:37 +0800519 }
Yu Zhaodd7cc442009-03-20 11:25:15 +0800520
521 rc = sysfs_create_link(&dev->dev.kobj,
522 &pdev->dev.kobj, "dep_link");
Jiang Liudc087f22013-05-25 21:48:37 +0800523 pci_dev_put(pdev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800524 if (rc)
525 return rc;
526 }
527
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700528 iov->initial_VFs = initial;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800529 if (nr_virtfn < initial)
530 initial = nr_virtfn;
531
Alexander Duyckc23b6132015-10-29 16:21:20 -0500532 rc = pcibios_sriov_enable(dev, initial);
533 if (rc) {
Frederick Lawler7506dc72018-01-18 12:55:24 -0600534 pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc);
Alexander Duyckc23b6132015-10-29 16:21:20 -0500535 goto err_pcibios;
Wei Yang995df522015-03-25 16:23:49 +0800536 }
537
Gavin Shanf40ec3c2016-10-26 12:15:35 +1100538 pci_iov_set_numvfs(dev, nr_virtfn);
539 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
540 pci_cfg_access_lock(dev);
541 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
542 msleep(100);
543 pci_cfg_access_unlock(dev);
544
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100545 rc = sriov_add_vfs(dev, initial);
546 if (rc)
547 goto err_pcibios;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800548
549 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700550 iov->num_VFs = nr_virtfn;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800551
552 return 0;
553
Alexander Duyckc23b6132015-10-29 16:21:20 -0500554err_pcibios:
Yu Zhaodd7cc442009-03-20 11:25:15 +0800555 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100556 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800557 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
558 ssleep(1);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100559 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800560
Gavin Shan0fc690a2017-08-11 18:19:33 +1000561 pcibios_sriov_disable(dev);
562
Yu Zhaodd7cc442009-03-20 11:25:15 +0800563 if (iov->link != dev->devfn)
564 sysfs_remove_link(&dev->dev.kobj, "dep_link");
565
Alexander Duyckb3908642015-10-29 16:21:16 -0500566 pci_iov_set_numvfs(dev, 0);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800567 return rc;
568}
569
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100570static void sriov_del_vfs(struct pci_dev *dev)
571{
572 struct pci_sriov *iov = dev->sriov;
573 int i;
574
575 for (i = 0; i < iov->num_VFs; i++)
576 pci_iov_remove_virtfn(dev, i);
577}
578
Yu Zhaodd7cc442009-03-20 11:25:15 +0800579static void sriov_disable(struct pci_dev *dev)
580{
Yu Zhaodd7cc442009-03-20 11:25:15 +0800581 struct pci_sriov *iov = dev->sriov;
582
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700583 if (!iov->num_VFs)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800584 return;
585
Sebastian Ott18f9e9d2018-12-21 15:14:18 +0100586 sriov_del_vfs(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800587 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100588 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800589 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
590 ssleep(1);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100591 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800592
Gavin Shan0fc690a2017-08-11 18:19:33 +1000593 pcibios_sriov_disable(dev);
594
Yu Zhaodd7cc442009-03-20 11:25:15 +0800595 if (iov->link != dev->devfn)
596 sysfs_remove_link(&dev->dev.kobj, "dep_link");
597
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700598 iov->num_VFs = 0;
Wei Yangf59dca22015-03-25 16:23:46 +0800599 pci_iov_set_numvfs(dev, 0);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800600}
601
Yu Zhaod1b054d2009-03-20 11:25:11 +0800602static int sriov_init(struct pci_dev *dev, int pos)
603{
Wei Yang0e6c9122015-03-25 16:23:44 +0800604 int i, bar64;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800605 int rc;
606 int nres;
607 u32 pgsz;
Alexander Duyckea9a8852015-10-29 16:20:50 -0500608 u16 ctrl, total;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800609 struct pci_sriov *iov;
610 struct resource *res;
611 struct pci_dev *pdev;
612
Yu Zhaod1b054d2009-03-20 11:25:11 +0800613 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
614 if (ctrl & PCI_SRIOV_CTRL_VFE) {
615 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
616 ssleep(1);
617 }
618
Yu Zhaod1b054d2009-03-20 11:25:11 +0800619 ctrl = 0;
620 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
621 if (pdev->is_physfn)
622 goto found;
623
624 pdev = NULL;
625 if (pci_ari_enabled(dev->bus))
626 ctrl |= PCI_SRIOV_CTRL_ARI;
627
628found:
629 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800630
Ben Sheltonff45f9d2015-10-29 16:20:31 -0500631 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
632 if (!total)
633 return 0;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800634
635 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
636 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
637 pgsz &= ~((1 << i) - 1);
638 if (!pgsz)
639 return -EIO;
640
641 pgsz &= ~(pgsz - 1);
Vaidyanathan Srinivasan8161fe92012-02-02 23:11:20 +0530642 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800643
Wei Yang0e6c9122015-03-25 16:23:44 +0800644 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
645 if (!iov)
646 return -ENOMEM;
647
Yu Zhaod1b054d2009-03-20 11:25:11 +0800648 nres = 0;
649 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Bjorn Helgaasc1fe1f92015-03-25 16:23:45 +0800650 res = &dev->resource[i + PCI_IOV_RESOURCES];
David Daney11183992015-10-29 17:35:40 -0500651 /*
652 * If it is already FIXED, don't change it, something
653 * (perhaps EA or header fixups) wants it this way.
654 */
655 if (res->flags & IORESOURCE_PCI_FIXED)
656 bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
657 else
658 bar64 = __pci_read_base(dev, pci_bar_unknown, res,
659 pos + PCI_SRIOV_BAR + i * 4);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800660 if (!res->flags)
661 continue;
662 if (resource_size(res) & (PAGE_SIZE - 1)) {
663 rc = -EIO;
664 goto failed;
665 }
Wei Yang0e6c9122015-03-25 16:23:44 +0800666 iov->barsz[i] = resource_size(res);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800667 res->end = res->start + resource_size(res) * total - 1;
Frederick Lawler7506dc72018-01-18 12:55:24 -0600668 pci_info(dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
Wei Yange88ae012015-03-25 16:23:43 +0800669 i, res, i, total);
Wei Yang0e6c9122015-03-25 16:23:44 +0800670 i += bar64;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800671 nres++;
672 }
673
Yu Zhaod1b054d2009-03-20 11:25:11 +0800674 iov->pos = pos;
675 iov->nres = nres;
676 iov->ctrl = ctrl;
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700677 iov->total_VFs = total;
Jakub Kicinski8d85a7a2018-05-25 08:18:34 -0500678 iov->driver_max_VFs = total;
Filippo Sironi3142d832017-08-28 15:38:49 +0200679 pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800680 iov->pgsz = pgsz;
681 iov->self = dev;
Bodong Wang0e7df222017-04-13 01:51:40 +0300682 iov->drivers_autoprobe = true;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800683 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
684 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
Yijing Wang62f87c02012-07-24 17:20:03 +0800685 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
Yu Zhao4d135db2009-05-20 17:11:57 +0800686 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800687
688 if (pdev)
689 iov->dev = pci_dev_get(pdev);
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800690 else
Yu Zhaod1b054d2009-03-20 11:25:11 +0800691 iov->dev = dev;
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800692
Yu Zhaod1b054d2009-03-20 11:25:11 +0800693 dev->sriov = iov;
694 dev->is_physfn = 1;
Alexander Duyckea9a8852015-10-29 16:20:50 -0500695 rc = compute_max_vf_buses(dev);
696 if (rc)
697 goto fail_max_buses;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800698
699 return 0;
700
Alexander Duyckea9a8852015-10-29 16:20:50 -0500701fail_max_buses:
702 dev->sriov = NULL;
703 dev->is_physfn = 0;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800704failed:
705 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Bjorn Helgaasc1fe1f92015-03-25 16:23:45 +0800706 res = &dev->resource[i + PCI_IOV_RESOURCES];
Yu Zhaod1b054d2009-03-20 11:25:11 +0800707 res->flags = 0;
708 }
709
Wei Yang0e6c9122015-03-25 16:23:44 +0800710 kfree(iov);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800711 return rc;
712}
713
714static void sriov_release(struct pci_dev *dev)
715{
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700716 BUG_ON(dev->sriov->num_VFs);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800717
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800718 if (dev != dev->sriov->dev)
Yu Zhaod1b054d2009-03-20 11:25:11 +0800719 pci_dev_put(dev->sriov->dev);
720
721 kfree(dev->sriov);
722 dev->sriov = NULL;
723}
724
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800725static void sriov_restore_state(struct pci_dev *dev)
726{
727 int i;
728 u16 ctrl;
729 struct pci_sriov *iov = dev->sriov;
730
731 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
732 if (ctrl & PCI_SRIOV_CTRL_VFE)
733 return;
734
Tony Nguyenff264492017-10-04 08:52:58 -0700735 /*
736 * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
737 * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
738 */
739 ctrl &= ~PCI_SRIOV_CTRL_ARI;
740 ctrl |= iov->ctrl & PCI_SRIOV_CTRL_ARI;
741 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
742
Denis Efremov39098ed2019-08-06 17:07:15 +0300743 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
744 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800745
746 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
Wei Yangf59dca22015-03-25 16:23:46 +0800747 pci_iov_set_numvfs(dev, iov->num_VFs);
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800748 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
749 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
750 msleep(100);
751}
752
Yu Zhaod1b054d2009-03-20 11:25:11 +0800753/**
754 * pci_iov_init - initialize the IOV capability
755 * @dev: the PCI device
756 *
757 * Returns 0 on success, or negative on failure.
758 */
759int pci_iov_init(struct pci_dev *dev)
760{
761 int pos;
762
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900763 if (!pci_is_pcie(dev))
Yu Zhaod1b054d2009-03-20 11:25:11 +0800764 return -ENODEV;
765
766 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
767 if (pos)
768 return sriov_init(dev, pos);
769
770 return -ENODEV;
771}
772
773/**
774 * pci_iov_release - release resources used by the IOV capability
775 * @dev: the PCI device
776 */
777void pci_iov_release(struct pci_dev *dev)
778{
779 if (dev->is_physfn)
780 sriov_release(dev);
781}
782
783/**
Jakub Kicinski38972372018-06-29 15:08:52 -0500784 * pci_iov_remove - clean up SR-IOV state after PF driver is detached
785 * @dev: the PCI device
786 */
787void pci_iov_remove(struct pci_dev *dev)
788{
789 struct pci_sriov *iov = dev->sriov;
790
791 if (!dev->is_physfn)
792 return;
793
794 iov->driver_max_VFs = iov->total_VFs;
795 if (iov->num_VFs)
796 pci_warn(dev, "driver left SR-IOV enabled after remove\n");
797}
798
799/**
Bjorn Helgaas6ffa2482016-11-28 09:15:52 -0600800 * pci_iov_update_resource - update a VF BAR
801 * @dev: the PCI device
802 * @resno: the resource number
803 *
804 * Update a VF BAR in the SR-IOV capability of a PF.
805 */
806void pci_iov_update_resource(struct pci_dev *dev, int resno)
807{
808 struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
809 struct resource *res = dev->resource + resno;
810 int vf_bar = resno - PCI_IOV_RESOURCES;
811 struct pci_bus_region region;
Bjorn Helgaas546ba9f2016-11-28 16:43:06 -0600812 u16 cmd;
Bjorn Helgaas6ffa2482016-11-28 09:15:52 -0600813 u32 new;
814 int reg;
815
816 /*
817 * The generic pci_restore_bars() path calls this for all devices,
818 * including VFs and non-SR-IOV devices. If this is not a PF, we
819 * have nothing to do.
820 */
821 if (!iov)
822 return;
823
Bjorn Helgaas546ba9f2016-11-28 16:43:06 -0600824 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
825 if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) {
826 dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
827 vf_bar, res);
828 return;
829 }
830
Bjorn Helgaas6ffa2482016-11-28 09:15:52 -0600831 /*
832 * Ignore unimplemented BARs, unused resource slots for 64-bit
833 * BARs, and non-movable resources, e.g., those described via
834 * Enhanced Allocation.
835 */
836 if (!res->flags)
837 return;
838
839 if (res->flags & IORESOURCE_UNSET)
840 return;
841
842 if (res->flags & IORESOURCE_PCI_FIXED)
843 return;
844
845 pcibios_resource_to_bus(dev->bus, &region, res);
846 new = region.start;
847 new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK;
848
849 reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar;
850 pci_write_config_dword(dev, reg, new);
851 if (res->flags & IORESOURCE_MEM_64) {
852 new = region.start >> 16 >> 16;
853 pci_write_config_dword(dev, reg + 4, new);
854 }
855}
856
Wei Yang978d2d62015-03-25 16:23:50 +0800857resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
858 int resno)
859{
860 return pci_iov_resource_size(dev, resno);
861}
862
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800863/**
Chris Wright6faf17f2009-08-28 13:00:06 -0700864 * pci_sriov_resource_alignment - get resource alignment for VF BAR
865 * @dev: the PCI device
866 * @resno: the resource number
867 *
868 * Returns the alignment of the VF BAR found in the SR-IOV capability.
869 * This is not the same as the resource size which is defined as
870 * the VF BAR size multiplied by the number of VFs. The alignment
871 * is just the VF BAR size.
872 */
Cam Macdonell0e522472010-09-07 17:25:20 -0700873resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
Chris Wright6faf17f2009-08-28 13:00:06 -0700874{
Wei Yang978d2d62015-03-25 16:23:50 +0800875 return pcibios_iov_resource_alignment(dev, resno);
Chris Wright6faf17f2009-08-28 13:00:06 -0700876}
877
878/**
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800879 * pci_restore_iov_state - restore the state of the IOV capability
880 * @dev: the PCI device
881 */
882void pci_restore_iov_state(struct pci_dev *dev)
883{
884 if (dev->is_physfn)
885 sriov_restore_state(dev);
886}
Yu Zhaoa28724b2009-03-20 11:25:13 +0800887
888/**
Bryant G. Ly608c0d82017-11-09 08:00:35 -0600889 * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs
890 * @dev: the PCI device
891 * @auto_probe: set VF drivers auto probe flag
892 */
893void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe)
894{
895 if (dev->is_physfn)
896 dev->sriov->drivers_autoprobe = auto_probe;
897}
898
899/**
Yu Zhaoa28724b2009-03-20 11:25:13 +0800900 * pci_iov_bus_range - find bus range used by Virtual Function
901 * @bus: the PCI bus
902 *
903 * Returns max number of buses (exclude current one) used by Virtual
904 * Functions.
905 */
906int pci_iov_bus_range(struct pci_bus *bus)
907{
908 int max = 0;
Yu Zhaoa28724b2009-03-20 11:25:13 +0800909 struct pci_dev *dev;
910
911 list_for_each_entry(dev, &bus->devices, bus_list) {
912 if (!dev->is_physfn)
913 continue;
Wei Yang4449f0792015-03-25 16:23:47 +0800914 if (dev->sriov->max_VF_buses > max)
915 max = dev->sriov->max_VF_buses;
Yu Zhaoa28724b2009-03-20 11:25:13 +0800916 }
917
918 return max ? max - bus->number : 0;
919}
Yu Zhaodd7cc442009-03-20 11:25:15 +0800920
921/**
922 * pci_enable_sriov - enable the SR-IOV capability
923 * @dev: the PCI device
Randy Dunlap52a88732009-04-01 17:45:30 -0700924 * @nr_virtfn: number of virtual functions to enable
Yu Zhaodd7cc442009-03-20 11:25:15 +0800925 *
926 * Returns 0 on success, or negative on failure.
927 */
928int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
929{
930 might_sleep();
931
932 if (!dev->is_physfn)
Stefan Assmann652d1102013-07-31 16:47:56 -0600933 return -ENOSYS;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800934
935 return sriov_enable(dev, nr_virtfn);
936}
937EXPORT_SYMBOL_GPL(pci_enable_sriov);
938
939/**
940 * pci_disable_sriov - disable the SR-IOV capability
941 * @dev: the PCI device
942 */
943void pci_disable_sriov(struct pci_dev *dev)
944{
945 might_sleep();
946
947 if (!dev->is_physfn)
948 return;
949
950 sriov_disable(dev);
951}
952EXPORT_SYMBOL_GPL(pci_disable_sriov);
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800953
954/**
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000955 * pci_num_vf - return number of VFs associated with a PF device_release_driver
956 * @dev: the PCI device
957 *
958 * Returns number of VFs, or 0 if SR-IOV is not enabled.
959 */
960int pci_num_vf(struct pci_dev *dev)
961{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700962 if (!dev->is_physfn)
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000963 return 0;
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700964
965 return dev->sriov->num_VFs;
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000966}
967EXPORT_SYMBOL_GPL(pci_num_vf);
Donald Dutilebff73152012-11-05 15:20:37 -0500968
969/**
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000970 * pci_vfs_assigned - returns number of VFs are assigned to a guest
971 * @dev: the PCI device
972 *
973 * Returns number of VFs belonging to this device that are assigned to a guest.
Stefan Assmann652d1102013-07-31 16:47:56 -0600974 * If device is not a physical function returns 0.
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000975 */
976int pci_vfs_assigned(struct pci_dev *dev)
977{
978 struct pci_dev *vfdev;
979 unsigned int vfs_assigned = 0;
980 unsigned short dev_id;
981
982 /* only search if we are a PF */
983 if (!dev->is_physfn)
984 return 0;
985
986 /*
987 * determine the device ID for the VFs, the vendor ID will be the
988 * same as the PF so there is no need to check for that one
989 */
Filippo Sironi3142d832017-08-28 15:38:49 +0200990 dev_id = dev->sriov->vf_device;
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000991
992 /* loop through all the VFs to see if we own any that are assigned */
993 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
994 while (vfdev) {
995 /*
996 * It is considered assigned if it is a virtual function with
997 * our dev as the physical function and the assigned bit is set
998 */
999 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
Ethan Zhaobe634972014-09-09 10:21:28 +08001000 pci_is_dev_assigned(vfdev))
Alexander Duyck5a8eb242013-04-25 04:42:29 +00001001 vfs_assigned++;
1002
1003 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
1004 }
1005
1006 return vfs_assigned;
1007}
1008EXPORT_SYMBOL_GPL(pci_vfs_assigned);
1009
1010/**
Donald Dutilebff73152012-11-05 15:20:37 -05001011 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
1012 * @dev: the PCI PF device
Randy Dunlap2094f162013-01-09 17:12:52 -08001013 * @numvfs: number that should be used for TotalVFs supported
Donald Dutilebff73152012-11-05 15:20:37 -05001014 *
1015 * Should be called from PF driver's probe routine with
1016 * device's mutex held.
1017 *
1018 * Returns 0 if PF is an SRIOV-capable device and
Stefan Assmann652d1102013-07-31 16:47:56 -06001019 * value of numvfs valid. If not a PF return -ENOSYS;
1020 * if numvfs is invalid return -EINVAL;
Donald Dutilebff73152012-11-05 15:20:37 -05001021 * if VFs already enabled, return -EBUSY.
1022 */
1023int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
1024{
Stefan Assmann652d1102013-07-31 16:47:56 -06001025 if (!dev->is_physfn)
1026 return -ENOSYS;
Bjorn Helgaas51259d02018-05-25 08:51:50 -05001027
Stefan Assmann652d1102013-07-31 16:47:56 -06001028 if (numvfs > dev->sriov->total_VFs)
Donald Dutilebff73152012-11-05 15:20:37 -05001029 return -EINVAL;
1030
1031 /* Shouldn't change if VFs already enabled */
1032 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
1033 return -EBUSY;
Donald Dutilebff73152012-11-05 15:20:37 -05001034
Bjorn Helgaas51259d02018-05-25 08:51:50 -05001035 dev->sriov->driver_max_VFs = numvfs;
Donald Dutilebff73152012-11-05 15:20:37 -05001036 return 0;
1037}
1038EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
1039
1040/**
Jonghwan Choiddc191f2013-07-08 14:02:43 -06001041 * pci_sriov_get_totalvfs -- get total VFs supported on this device
Donald Dutilebff73152012-11-05 15:20:37 -05001042 * @dev: the PCI PF device
1043 *
1044 * For a PCIe device with SRIOV support, return the PCIe
Bjorn Helgaas6b136722012-11-09 20:27:53 -07001045 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
Stefan Assmann652d1102013-07-31 16:47:56 -06001046 * if the driver reduced it. Otherwise 0.
Donald Dutilebff73152012-11-05 15:20:37 -05001047 */
1048int pci_sriov_get_totalvfs(struct pci_dev *dev)
1049{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -07001050 if (!dev->is_physfn)
Stefan Assmann652d1102013-07-31 16:47:56 -06001051 return 0;
Donald Dutilebff73152012-11-05 15:20:37 -05001052
Jakub Kicinski8d85a7a2018-05-25 08:18:34 -05001053 return dev->sriov->driver_max_VFs;
Donald Dutilebff73152012-11-05 15:20:37 -05001054}
1055EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);
Alexander Duyck8effc392018-04-21 15:23:09 -05001056
1057/**
1058 * pci_sriov_configure_simple - helper to configure SR-IOV
1059 * @dev: the PCI device
1060 * @nr_virtfn: number of virtual functions to enable, 0 to disable
1061 *
1062 * Enable or disable SR-IOV for devices that don't require any PF setup
1063 * before enabling SR-IOV. Return value is negative on error, or number of
1064 * VFs allocated on success.
1065 */
1066int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn)
1067{
1068 int rc;
1069
1070 might_sleep();
1071
1072 if (!dev->is_physfn)
1073 return -ENODEV;
1074
1075 if (pci_vfs_assigned(dev)) {
1076 pci_warn(dev, "Cannot modify SR-IOV while VFs are assigned\n");
1077 return -EPERM;
1078 }
1079
1080 if (nr_virtfn == 0) {
1081 sriov_disable(dev);
1082 return 0;
1083 }
1084
1085 rc = sriov_enable(dev, nr_virtfn);
1086 if (rc < 0)
1087 return rc;
1088
1089 return nr_virtfn;
1090}
1091EXPORT_SYMBOL_GPL(pci_sriov_configure_simple);