Linux 4.19.133
[linux/fpc-iii.git] / drivers / pci / iov.c
blobc3b0b10f95cb6bdf97f218af93d3162df51f52e2
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * PCI Express I/O Virtualization (IOV) support
4 * Single Root IOV 1.0
5 * Address Translation Service 1.0
7 * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
8 */
10 #include <linux/pci.h>
11 #include <linux/slab.h>
12 #include <linux/mutex.h>
13 #include <linux/export.h>
14 #include <linux/string.h>
15 #include <linux/delay.h>
16 #include <linux/pci-ats.h>
17 #include "pci.h"
19 #define VIRTFN_ID_LEN 16
21 int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id)
23 if (!dev->is_physfn)
24 return -EINVAL;
25 return dev->bus->number + ((dev->devfn + dev->sriov->offset +
26 dev->sriov->stride * vf_id) >> 8);
29 int pci_iov_virtfn_devfn(struct pci_dev *dev, int vf_id)
31 if (!dev->is_physfn)
32 return -EINVAL;
33 return (dev->devfn + dev->sriov->offset +
34 dev->sriov->stride * vf_id) & 0xff;
38 * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
39 * change when NumVFs changes.
41 * Update iov->offset and iov->stride when NumVFs is written.
43 static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
45 struct pci_sriov *iov = dev->sriov;
47 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
48 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
49 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
53 * The PF consumes one bus number. NumVFs, First VF Offset, and VF Stride
54 * determine how many additional bus numbers will be consumed by VFs.
56 * Iterate over all valid NumVFs, validate offset and stride, and calculate
57 * the maximum number of bus numbers that could ever be required.
59 static int compute_max_vf_buses(struct pci_dev *dev)
61 struct pci_sriov *iov = dev->sriov;
62 int nr_virtfn, busnr, rc = 0;
64 for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) {
65 pci_iov_set_numvfs(dev, nr_virtfn);
66 if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) {
67 rc = -EIO;
68 goto out;
71 busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
72 if (busnr > iov->max_VF_buses)
73 iov->max_VF_buses = busnr;
76 out:
77 pci_iov_set_numvfs(dev, 0);
78 return rc;
81 static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
83 struct pci_bus *child;
85 if (bus->number == busnr)
86 return bus;
88 child = pci_find_bus(pci_domain_nr(bus), busnr);
89 if (child)
90 return child;
92 child = pci_add_new_bus(bus, NULL, busnr);
93 if (!child)
94 return NULL;
96 pci_bus_insert_busn_res(child, busnr, busnr);
98 return child;
101 static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
103 if (physbus != virtbus && list_empty(&virtbus->devices))
104 pci_remove_bus(virtbus);
107 resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
109 if (!dev->is_physfn)
110 return 0;
112 return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
115 static void pci_read_vf_config_common(struct pci_dev *virtfn)
117 struct pci_dev *physfn = virtfn->physfn;
120 * Some config registers are the same across all associated VFs.
121 * Read them once from VF0 so we can skip reading them from the
122 * other VFs.
124 * PCIe r4.0, sec 9.3.4.1, technically doesn't require all VFs to
125 * have the same Revision ID and Subsystem ID, but we assume they
126 * do.
128 pci_read_config_dword(virtfn, PCI_CLASS_REVISION,
129 &physfn->sriov->class);
130 pci_read_config_byte(virtfn, PCI_HEADER_TYPE,
131 &physfn->sriov->hdr_type);
132 pci_read_config_word(virtfn, PCI_SUBSYSTEM_VENDOR_ID,
133 &physfn->sriov->subsystem_vendor);
134 pci_read_config_word(virtfn, PCI_SUBSYSTEM_ID,
135 &physfn->sriov->subsystem_device);
138 int pci_iov_add_virtfn(struct pci_dev *dev, int id)
140 int i;
141 int rc = -ENOMEM;
142 u64 size;
143 char buf[VIRTFN_ID_LEN];
144 struct pci_dev *virtfn;
145 struct resource *res;
146 struct pci_sriov *iov = dev->sriov;
147 struct pci_bus *bus;
149 bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
150 if (!bus)
151 goto failed;
153 virtfn = pci_alloc_dev(bus);
154 if (!virtfn)
155 goto failed0;
157 virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
158 virtfn->vendor = dev->vendor;
159 virtfn->device = iov->vf_device;
160 virtfn->is_virtfn = 1;
161 virtfn->physfn = pci_dev_get(dev);
163 if (id == 0)
164 pci_read_vf_config_common(virtfn);
166 rc = pci_setup_device(virtfn);
167 if (rc)
168 goto failed1;
170 virtfn->dev.parent = dev->dev.parent;
171 virtfn->multifunction = 0;
173 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
174 res = &dev->resource[i + PCI_IOV_RESOURCES];
175 if (!res->parent)
176 continue;
177 virtfn->resource[i].name = pci_name(virtfn);
178 virtfn->resource[i].flags = res->flags;
179 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
180 virtfn->resource[i].start = res->start + size * id;
181 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
182 rc = request_resource(res, &virtfn->resource[i]);
183 BUG_ON(rc);
186 pci_device_add(virtfn, virtfn->bus);
188 sprintf(buf, "virtfn%u", id);
189 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
190 if (rc)
191 goto failed1;
192 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
193 if (rc)
194 goto failed2;
196 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
198 pci_bus_add_device(virtfn);
200 return 0;
202 failed2:
203 sysfs_remove_link(&dev->dev.kobj, buf);
204 failed1:
205 pci_stop_and_remove_bus_device(virtfn);
206 pci_dev_put(dev);
207 failed0:
208 virtfn_remove_bus(dev->bus, bus);
209 failed:
211 return rc;
214 void pci_iov_remove_virtfn(struct pci_dev *dev, int id)
216 char buf[VIRTFN_ID_LEN];
217 struct pci_dev *virtfn;
219 virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
220 pci_iov_virtfn_bus(dev, id),
221 pci_iov_virtfn_devfn(dev, id));
222 if (!virtfn)
223 return;
225 sprintf(buf, "virtfn%u", id);
226 sysfs_remove_link(&dev->dev.kobj, buf);
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.
232 if (virtfn->dev.kobj.sd)
233 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
235 pci_stop_and_remove_bus_device(virtfn);
236 virtfn_remove_bus(dev->bus, virtfn->bus);
238 /* balance pci_get_domain_bus_and_slot() */
239 pci_dev_put(virtfn);
240 pci_dev_put(dev);
243 int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
245 return 0;
248 int __weak pcibios_sriov_disable(struct pci_dev *pdev)
250 return 0;
253 static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
255 int rc;
256 int i;
257 int nres;
258 u16 initial;
259 struct resource *res;
260 struct pci_dev *pdev;
261 struct pci_sriov *iov = dev->sriov;
262 int bars = 0;
263 int bus;
265 if (!nr_virtfn)
266 return 0;
268 if (iov->num_VFs)
269 return -EINVAL;
271 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
272 if (initial > iov->total_VFs ||
273 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
274 return -EIO;
276 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
277 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
278 return -EINVAL;
280 nres = 0;
281 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
282 bars |= (1 << (i + PCI_IOV_RESOURCES));
283 res = &dev->resource[i + PCI_IOV_RESOURCES];
284 if (res->parent)
285 nres++;
287 if (nres != iov->nres) {
288 pci_err(dev, "not enough MMIO resources for SR-IOV\n");
289 return -ENOMEM;
292 bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
293 if (bus > dev->bus->busn_res.end) {
294 pci_err(dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
295 nr_virtfn, bus, &dev->bus->busn_res);
296 return -ENOMEM;
299 if (pci_enable_resources(dev, bars)) {
300 pci_err(dev, "SR-IOV: IOV BARS not allocated\n");
301 return -ENOMEM;
304 if (iov->link != dev->devfn) {
305 pdev = pci_get_slot(dev->bus, iov->link);
306 if (!pdev)
307 return -ENODEV;
309 if (!pdev->is_physfn) {
310 pci_dev_put(pdev);
311 return -ENOSYS;
314 rc = sysfs_create_link(&dev->dev.kobj,
315 &pdev->dev.kobj, "dep_link");
316 pci_dev_put(pdev);
317 if (rc)
318 return rc;
321 iov->initial_VFs = initial;
322 if (nr_virtfn < initial)
323 initial = nr_virtfn;
325 rc = pcibios_sriov_enable(dev, initial);
326 if (rc) {
327 pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc);
328 goto err_pcibios;
331 pci_iov_set_numvfs(dev, nr_virtfn);
332 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
333 pci_cfg_access_lock(dev);
334 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
335 msleep(100);
336 pci_cfg_access_unlock(dev);
338 for (i = 0; i < initial; i++) {
339 rc = pci_iov_add_virtfn(dev, i);
340 if (rc)
341 goto failed;
344 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
345 iov->num_VFs = nr_virtfn;
347 return 0;
349 failed:
350 while (i--)
351 pci_iov_remove_virtfn(dev, i);
353 err_pcibios:
354 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
355 pci_cfg_access_lock(dev);
356 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
357 ssleep(1);
358 pci_cfg_access_unlock(dev);
360 pcibios_sriov_disable(dev);
362 if (iov->link != dev->devfn)
363 sysfs_remove_link(&dev->dev.kobj, "dep_link");
365 pci_iov_set_numvfs(dev, 0);
366 return rc;
369 static void sriov_disable(struct pci_dev *dev)
371 int i;
372 struct pci_sriov *iov = dev->sriov;
374 if (!iov->num_VFs)
375 return;
377 for (i = 0; i < iov->num_VFs; i++)
378 pci_iov_remove_virtfn(dev, i);
380 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
381 pci_cfg_access_lock(dev);
382 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
383 ssleep(1);
384 pci_cfg_access_unlock(dev);
386 pcibios_sriov_disable(dev);
388 if (iov->link != dev->devfn)
389 sysfs_remove_link(&dev->dev.kobj, "dep_link");
391 iov->num_VFs = 0;
392 pci_iov_set_numvfs(dev, 0);
395 static int sriov_init(struct pci_dev *dev, int pos)
397 int i, bar64;
398 int rc;
399 int nres;
400 u32 pgsz;
401 u16 ctrl, total;
402 struct pci_sriov *iov;
403 struct resource *res;
404 struct pci_dev *pdev;
406 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
407 if (ctrl & PCI_SRIOV_CTRL_VFE) {
408 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
409 ssleep(1);
412 ctrl = 0;
413 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
414 if (pdev->is_physfn)
415 goto found;
417 pdev = NULL;
418 if (pci_ari_enabled(dev->bus))
419 ctrl |= PCI_SRIOV_CTRL_ARI;
421 found:
422 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
424 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
425 if (!total)
426 return 0;
428 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
429 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
430 pgsz &= ~((1 << i) - 1);
431 if (!pgsz)
432 return -EIO;
434 pgsz &= ~(pgsz - 1);
435 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
437 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
438 if (!iov)
439 return -ENOMEM;
441 nres = 0;
442 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
443 res = &dev->resource[i + PCI_IOV_RESOURCES];
445 * If it is already FIXED, don't change it, something
446 * (perhaps EA or header fixups) wants it this way.
448 if (res->flags & IORESOURCE_PCI_FIXED)
449 bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
450 else
451 bar64 = __pci_read_base(dev, pci_bar_unknown, res,
452 pos + PCI_SRIOV_BAR + i * 4);
453 if (!res->flags)
454 continue;
455 if (resource_size(res) & (PAGE_SIZE - 1)) {
456 rc = -EIO;
457 goto failed;
459 iov->barsz[i] = resource_size(res);
460 res->end = res->start + resource_size(res) * total - 1;
461 pci_info(dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
462 i, res, i, total);
463 i += bar64;
464 nres++;
467 iov->pos = pos;
468 iov->nres = nres;
469 iov->ctrl = ctrl;
470 iov->total_VFs = total;
471 iov->driver_max_VFs = total;
472 pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
473 iov->pgsz = pgsz;
474 iov->self = dev;
475 iov->drivers_autoprobe = true;
476 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
477 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
478 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
479 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
481 if (pdev)
482 iov->dev = pci_dev_get(pdev);
483 else
484 iov->dev = dev;
486 dev->sriov = iov;
487 dev->is_physfn = 1;
488 rc = compute_max_vf_buses(dev);
489 if (rc)
490 goto fail_max_buses;
492 return 0;
494 fail_max_buses:
495 dev->sriov = NULL;
496 dev->is_physfn = 0;
497 failed:
498 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
499 res = &dev->resource[i + PCI_IOV_RESOURCES];
500 res->flags = 0;
503 kfree(iov);
504 return rc;
507 static void sriov_release(struct pci_dev *dev)
509 BUG_ON(dev->sriov->num_VFs);
511 if (dev != dev->sriov->dev)
512 pci_dev_put(dev->sriov->dev);
514 kfree(dev->sriov);
515 dev->sriov = NULL;
518 static void sriov_restore_state(struct pci_dev *dev)
520 int i;
521 u16 ctrl;
522 struct pci_sriov *iov = dev->sriov;
524 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
525 if (ctrl & PCI_SRIOV_CTRL_VFE)
526 return;
529 * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
530 * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
532 ctrl &= ~PCI_SRIOV_CTRL_ARI;
533 ctrl |= iov->ctrl & PCI_SRIOV_CTRL_ARI;
534 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
536 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
537 pci_update_resource(dev, i);
539 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
540 pci_iov_set_numvfs(dev, iov->num_VFs);
541 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
542 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
543 msleep(100);
547 * pci_iov_init - initialize the IOV capability
548 * @dev: the PCI device
550 * Returns 0 on success, or negative on failure.
552 int pci_iov_init(struct pci_dev *dev)
554 int pos;
556 if (!pci_is_pcie(dev))
557 return -ENODEV;
559 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
560 if (pos)
561 return sriov_init(dev, pos);
563 return -ENODEV;
567 * pci_iov_release - release resources used by the IOV capability
568 * @dev: the PCI device
570 void pci_iov_release(struct pci_dev *dev)
572 if (dev->is_physfn)
573 sriov_release(dev);
577 * pci_iov_remove - clean up SR-IOV state after PF driver is detached
578 * @dev: the PCI device
580 void pci_iov_remove(struct pci_dev *dev)
582 struct pci_sriov *iov = dev->sriov;
584 if (!dev->is_physfn)
585 return;
587 iov->driver_max_VFs = iov->total_VFs;
588 if (iov->num_VFs)
589 pci_warn(dev, "driver left SR-IOV enabled after remove\n");
593 * pci_iov_update_resource - update a VF BAR
594 * @dev: the PCI device
595 * @resno: the resource number
597 * Update a VF BAR in the SR-IOV capability of a PF.
599 void pci_iov_update_resource(struct pci_dev *dev, int resno)
601 struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
602 struct resource *res = dev->resource + resno;
603 int vf_bar = resno - PCI_IOV_RESOURCES;
604 struct pci_bus_region region;
605 u16 cmd;
606 u32 new;
607 int reg;
610 * The generic pci_restore_bars() path calls this for all devices,
611 * including VFs and non-SR-IOV devices. If this is not a PF, we
612 * have nothing to do.
614 if (!iov)
615 return;
617 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
618 if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) {
619 dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
620 vf_bar, res);
621 return;
625 * Ignore unimplemented BARs, unused resource slots for 64-bit
626 * BARs, and non-movable resources, e.g., those described via
627 * Enhanced Allocation.
629 if (!res->flags)
630 return;
632 if (res->flags & IORESOURCE_UNSET)
633 return;
635 if (res->flags & IORESOURCE_PCI_FIXED)
636 return;
638 pcibios_resource_to_bus(dev->bus, &region, res);
639 new = region.start;
640 new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK;
642 reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar;
643 pci_write_config_dword(dev, reg, new);
644 if (res->flags & IORESOURCE_MEM_64) {
645 new = region.start >> 16 >> 16;
646 pci_write_config_dword(dev, reg + 4, new);
650 resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
651 int resno)
653 return pci_iov_resource_size(dev, resno);
657 * pci_sriov_resource_alignment - get resource alignment for VF BAR
658 * @dev: the PCI device
659 * @resno: the resource number
661 * Returns the alignment of the VF BAR found in the SR-IOV capability.
662 * This is not the same as the resource size which is defined as
663 * the VF BAR size multiplied by the number of VFs. The alignment
664 * is just the VF BAR size.
666 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
668 return pcibios_iov_resource_alignment(dev, resno);
672 * pci_restore_iov_state - restore the state of the IOV capability
673 * @dev: the PCI device
675 void pci_restore_iov_state(struct pci_dev *dev)
677 if (dev->is_physfn)
678 sriov_restore_state(dev);
682 * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs
683 * @dev: the PCI device
684 * @auto_probe: set VF drivers auto probe flag
686 void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe)
688 if (dev->is_physfn)
689 dev->sriov->drivers_autoprobe = auto_probe;
693 * pci_iov_bus_range - find bus range used by Virtual Function
694 * @bus: the PCI bus
696 * Returns max number of buses (exclude current one) used by Virtual
697 * Functions.
699 int pci_iov_bus_range(struct pci_bus *bus)
701 int max = 0;
702 struct pci_dev *dev;
704 list_for_each_entry(dev, &bus->devices, bus_list) {
705 if (!dev->is_physfn)
706 continue;
707 if (dev->sriov->max_VF_buses > max)
708 max = dev->sriov->max_VF_buses;
711 return max ? max - bus->number : 0;
715 * pci_enable_sriov - enable the SR-IOV capability
716 * @dev: the PCI device
717 * @nr_virtfn: number of virtual functions to enable
719 * Returns 0 on success, or negative on failure.
721 int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
723 might_sleep();
725 if (!dev->is_physfn)
726 return -ENOSYS;
728 return sriov_enable(dev, nr_virtfn);
730 EXPORT_SYMBOL_GPL(pci_enable_sriov);
733 * pci_disable_sriov - disable the SR-IOV capability
734 * @dev: the PCI device
736 void pci_disable_sriov(struct pci_dev *dev)
738 might_sleep();
740 if (!dev->is_physfn)
741 return;
743 sriov_disable(dev);
745 EXPORT_SYMBOL_GPL(pci_disable_sriov);
748 * pci_num_vf - return number of VFs associated with a PF device_release_driver
749 * @dev: the PCI device
751 * Returns number of VFs, or 0 if SR-IOV is not enabled.
753 int pci_num_vf(struct pci_dev *dev)
755 if (!dev->is_physfn)
756 return 0;
758 return dev->sriov->num_VFs;
760 EXPORT_SYMBOL_GPL(pci_num_vf);
763 * pci_vfs_assigned - returns number of VFs are assigned to a guest
764 * @dev: the PCI device
766 * Returns number of VFs belonging to this device that are assigned to a guest.
767 * If device is not a physical function returns 0.
769 int pci_vfs_assigned(struct pci_dev *dev)
771 struct pci_dev *vfdev;
772 unsigned int vfs_assigned = 0;
773 unsigned short dev_id;
775 /* only search if we are a PF */
776 if (!dev->is_physfn)
777 return 0;
780 * determine the device ID for the VFs, the vendor ID will be the
781 * same as the PF so there is no need to check for that one
783 dev_id = dev->sriov->vf_device;
785 /* loop through all the VFs to see if we own any that are assigned */
786 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
787 while (vfdev) {
789 * It is considered assigned if it is a virtual function with
790 * our dev as the physical function and the assigned bit is set
792 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
793 pci_is_dev_assigned(vfdev))
794 vfs_assigned++;
796 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
799 return vfs_assigned;
801 EXPORT_SYMBOL_GPL(pci_vfs_assigned);
804 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
805 * @dev: the PCI PF device
806 * @numvfs: number that should be used for TotalVFs supported
808 * Should be called from PF driver's probe routine with
809 * device's mutex held.
811 * Returns 0 if PF is an SRIOV-capable device and
812 * value of numvfs valid. If not a PF return -ENOSYS;
813 * if numvfs is invalid return -EINVAL;
814 * if VFs already enabled, return -EBUSY.
816 int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
818 if (!dev->is_physfn)
819 return -ENOSYS;
821 if (numvfs > dev->sriov->total_VFs)
822 return -EINVAL;
824 /* Shouldn't change if VFs already enabled */
825 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
826 return -EBUSY;
828 dev->sriov->driver_max_VFs = numvfs;
829 return 0;
831 EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
834 * pci_sriov_get_totalvfs -- get total VFs supported on this device
835 * @dev: the PCI PF device
837 * For a PCIe device with SRIOV support, return the PCIe
838 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
839 * if the driver reduced it. Otherwise 0.
841 int pci_sriov_get_totalvfs(struct pci_dev *dev)
843 if (!dev->is_physfn)
844 return 0;
846 return dev->sriov->driver_max_VFs;
848 EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);
851 * pci_sriov_configure_simple - helper to configure SR-IOV
852 * @dev: the PCI device
853 * @nr_virtfn: number of virtual functions to enable, 0 to disable
855 * Enable or disable SR-IOV for devices that don't require any PF setup
856 * before enabling SR-IOV. Return value is negative on error, or number of
857 * VFs allocated on success.
859 int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn)
861 int rc;
863 might_sleep();
865 if (!dev->is_physfn)
866 return -ENODEV;
868 if (pci_vfs_assigned(dev)) {
869 pci_warn(dev, "Cannot modify SR-IOV while VFs are assigned\n");
870 return -EPERM;
873 if (nr_virtfn == 0) {
874 sriov_disable(dev);
875 return 0;
878 rc = sriov_enable(dev, nr_virtfn);
879 if (rc < 0)
880 return rc;
882 return nr_virtfn;
884 EXPORT_SYMBOL_GPL(pci_sriov_configure_simple);