Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / pci / iov.c
blob677924ae03503e57ae7258cba3b2063316757a74
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * drivers/pci/iov.c
5 * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
7 * PCI Express I/O Virtualization (IOV) support.
8 * Single Root IOV 1.0
9 * Address Translation Service 1.0
12 #include <linux/pci.h>
13 #include <linux/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/export.h>
16 #include <linux/string.h>
17 #include <linux/delay.h>
18 #include <linux/pci-ats.h>
19 #include "pci.h"
21 #define VIRTFN_ID_LEN 16
23 int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id)
25 if (!dev->is_physfn)
26 return -EINVAL;
27 return dev->bus->number + ((dev->devfn + dev->sriov->offset +
28 dev->sriov->stride * vf_id) >> 8);
31 int pci_iov_virtfn_devfn(struct pci_dev *dev, int vf_id)
33 if (!dev->is_physfn)
34 return -EINVAL;
35 return (dev->devfn + dev->sriov->offset +
36 dev->sriov->stride * vf_id) & 0xff;
40 * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
41 * change when NumVFs changes.
43 * Update iov->offset and iov->stride when NumVFs is written.
45 static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
47 struct pci_sriov *iov = dev->sriov;
49 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
50 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
51 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
55 * The PF consumes one bus number. NumVFs, First VF Offset, and VF Stride
56 * determine how many additional bus numbers will be consumed by VFs.
58 * Iterate over all valid NumVFs, validate offset and stride, and calculate
59 * the maximum number of bus numbers that could ever be required.
61 static int compute_max_vf_buses(struct pci_dev *dev)
63 struct pci_sriov *iov = dev->sriov;
64 int nr_virtfn, busnr, rc = 0;
66 for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) {
67 pci_iov_set_numvfs(dev, nr_virtfn);
68 if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) {
69 rc = -EIO;
70 goto out;
73 busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
74 if (busnr > iov->max_VF_buses)
75 iov->max_VF_buses = busnr;
78 out:
79 pci_iov_set_numvfs(dev, 0);
80 return rc;
83 static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
85 struct pci_bus *child;
87 if (bus->number == busnr)
88 return bus;
90 child = pci_find_bus(pci_domain_nr(bus), busnr);
91 if (child)
92 return child;
94 child = pci_add_new_bus(bus, NULL, busnr);
95 if (!child)
96 return NULL;
98 pci_bus_insert_busn_res(child, busnr, busnr);
100 return child;
103 static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
105 if (physbus != virtbus && list_empty(&virtbus->devices))
106 pci_remove_bus(virtbus);
109 resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
111 if (!dev->is_physfn)
112 return 0;
114 return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
117 int pci_iov_add_virtfn(struct pci_dev *dev, int id)
119 int i;
120 int rc = -ENOMEM;
121 u64 size;
122 char buf[VIRTFN_ID_LEN];
123 struct pci_dev *virtfn;
124 struct resource *res;
125 struct pci_sriov *iov = dev->sriov;
126 struct pci_bus *bus;
128 bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
129 if (!bus)
130 goto failed;
132 virtfn = pci_alloc_dev(bus);
133 if (!virtfn)
134 goto failed0;
136 virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
137 virtfn->vendor = dev->vendor;
138 virtfn->device = iov->vf_device;
139 rc = pci_setup_device(virtfn);
140 if (rc)
141 goto failed0;
143 virtfn->dev.parent = dev->dev.parent;
144 virtfn->physfn = pci_dev_get(dev);
145 virtfn->is_virtfn = 1;
146 virtfn->multifunction = 0;
148 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
149 res = &dev->resource[i + PCI_IOV_RESOURCES];
150 if (!res->parent)
151 continue;
152 virtfn->resource[i].name = pci_name(virtfn);
153 virtfn->resource[i].flags = res->flags;
154 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
155 virtfn->resource[i].start = res->start + size * id;
156 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
157 rc = request_resource(res, &virtfn->resource[i]);
158 BUG_ON(rc);
161 pci_device_add(virtfn, virtfn->bus);
163 sprintf(buf, "virtfn%u", id);
164 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
165 if (rc)
166 goto failed1;
167 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
168 if (rc)
169 goto failed2;
171 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
173 pci_bus_add_device(virtfn);
175 return 0;
177 failed2:
178 sysfs_remove_link(&dev->dev.kobj, buf);
179 failed1:
180 pci_dev_put(dev);
181 pci_stop_and_remove_bus_device(virtfn);
182 failed0:
183 virtfn_remove_bus(dev->bus, bus);
184 failed:
186 return rc;
189 void pci_iov_remove_virtfn(struct pci_dev *dev, int id)
191 char buf[VIRTFN_ID_LEN];
192 struct pci_dev *virtfn;
194 virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
195 pci_iov_virtfn_bus(dev, id),
196 pci_iov_virtfn_devfn(dev, id));
197 if (!virtfn)
198 return;
200 sprintf(buf, "virtfn%u", id);
201 sysfs_remove_link(&dev->dev.kobj, buf);
203 * pci_stop_dev() could have been called for this virtfn already,
204 * so the directory for the virtfn may have been removed before.
205 * Double check to avoid spurious sysfs warnings.
207 if (virtfn->dev.kobj.sd)
208 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
210 pci_stop_and_remove_bus_device(virtfn);
211 virtfn_remove_bus(dev->bus, virtfn->bus);
213 /* balance pci_get_domain_bus_and_slot() */
214 pci_dev_put(virtfn);
215 pci_dev_put(dev);
218 int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
220 return 0;
223 int __weak pcibios_sriov_disable(struct pci_dev *pdev)
225 return 0;
228 static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
230 int rc;
231 int i;
232 int nres;
233 u16 initial;
234 struct resource *res;
235 struct pci_dev *pdev;
236 struct pci_sriov *iov = dev->sriov;
237 int bars = 0;
238 int bus;
240 if (!nr_virtfn)
241 return 0;
243 if (iov->num_VFs)
244 return -EINVAL;
246 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
247 if (initial > iov->total_VFs ||
248 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
249 return -EIO;
251 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
252 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
253 return -EINVAL;
255 nres = 0;
256 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
257 bars |= (1 << (i + PCI_IOV_RESOURCES));
258 res = &dev->resource[i + PCI_IOV_RESOURCES];
259 if (res->parent)
260 nres++;
262 if (nres != iov->nres) {
263 pci_err(dev, "not enough MMIO resources for SR-IOV\n");
264 return -ENOMEM;
267 bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
268 if (bus > dev->bus->busn_res.end) {
269 pci_err(dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
270 nr_virtfn, bus, &dev->bus->busn_res);
271 return -ENOMEM;
274 if (pci_enable_resources(dev, bars)) {
275 pci_err(dev, "SR-IOV: IOV BARS not allocated\n");
276 return -ENOMEM;
279 if (iov->link != dev->devfn) {
280 pdev = pci_get_slot(dev->bus, iov->link);
281 if (!pdev)
282 return -ENODEV;
284 if (!pdev->is_physfn) {
285 pci_dev_put(pdev);
286 return -ENOSYS;
289 rc = sysfs_create_link(&dev->dev.kobj,
290 &pdev->dev.kobj, "dep_link");
291 pci_dev_put(pdev);
292 if (rc)
293 return rc;
296 iov->initial_VFs = initial;
297 if (nr_virtfn < initial)
298 initial = nr_virtfn;
300 rc = pcibios_sriov_enable(dev, initial);
301 if (rc) {
302 pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc);
303 goto err_pcibios;
306 pci_iov_set_numvfs(dev, nr_virtfn);
307 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
308 pci_cfg_access_lock(dev);
309 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
310 msleep(100);
311 pci_cfg_access_unlock(dev);
313 for (i = 0; i < initial; i++) {
314 rc = pci_iov_add_virtfn(dev, i);
315 if (rc)
316 goto failed;
319 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
320 iov->num_VFs = nr_virtfn;
322 return 0;
324 failed:
325 while (i--)
326 pci_iov_remove_virtfn(dev, i);
328 err_pcibios:
329 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
330 pci_cfg_access_lock(dev);
331 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
332 ssleep(1);
333 pci_cfg_access_unlock(dev);
335 pcibios_sriov_disable(dev);
337 if (iov->link != dev->devfn)
338 sysfs_remove_link(&dev->dev.kobj, "dep_link");
340 pci_iov_set_numvfs(dev, 0);
341 return rc;
344 static void sriov_disable(struct pci_dev *dev)
346 int i;
347 struct pci_sriov *iov = dev->sriov;
349 if (!iov->num_VFs)
350 return;
352 for (i = 0; i < iov->num_VFs; i++)
353 pci_iov_remove_virtfn(dev, i);
355 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
356 pci_cfg_access_lock(dev);
357 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
358 ssleep(1);
359 pci_cfg_access_unlock(dev);
361 pcibios_sriov_disable(dev);
363 if (iov->link != dev->devfn)
364 sysfs_remove_link(&dev->dev.kobj, "dep_link");
366 iov->num_VFs = 0;
367 pci_iov_set_numvfs(dev, 0);
370 static int sriov_init(struct pci_dev *dev, int pos)
372 int i, bar64;
373 int rc;
374 int nres;
375 u32 pgsz;
376 u16 ctrl, total;
377 struct pci_sriov *iov;
378 struct resource *res;
379 struct pci_dev *pdev;
381 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
382 if (ctrl & PCI_SRIOV_CTRL_VFE) {
383 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
384 ssleep(1);
387 ctrl = 0;
388 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
389 if (pdev->is_physfn)
390 goto found;
392 pdev = NULL;
393 if (pci_ari_enabled(dev->bus))
394 ctrl |= PCI_SRIOV_CTRL_ARI;
396 found:
397 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
399 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
400 if (!total)
401 return 0;
403 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
404 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
405 pgsz &= ~((1 << i) - 1);
406 if (!pgsz)
407 return -EIO;
409 pgsz &= ~(pgsz - 1);
410 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
412 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
413 if (!iov)
414 return -ENOMEM;
416 nres = 0;
417 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
418 res = &dev->resource[i + PCI_IOV_RESOURCES];
420 * If it is already FIXED, don't change it, something
421 * (perhaps EA or header fixups) wants it this way.
423 if (res->flags & IORESOURCE_PCI_FIXED)
424 bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
425 else
426 bar64 = __pci_read_base(dev, pci_bar_unknown, res,
427 pos + PCI_SRIOV_BAR + i * 4);
428 if (!res->flags)
429 continue;
430 if (resource_size(res) & (PAGE_SIZE - 1)) {
431 rc = -EIO;
432 goto failed;
434 iov->barsz[i] = resource_size(res);
435 res->end = res->start + resource_size(res) * total - 1;
436 pci_info(dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
437 i, res, i, total);
438 i += bar64;
439 nres++;
442 iov->pos = pos;
443 iov->nres = nres;
444 iov->ctrl = ctrl;
445 iov->total_VFs = total;
446 pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
447 iov->pgsz = pgsz;
448 iov->self = dev;
449 iov->drivers_autoprobe = true;
450 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
451 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
452 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
453 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
455 if (pdev)
456 iov->dev = pci_dev_get(pdev);
457 else
458 iov->dev = dev;
460 dev->sriov = iov;
461 dev->is_physfn = 1;
462 rc = compute_max_vf_buses(dev);
463 if (rc)
464 goto fail_max_buses;
466 return 0;
468 fail_max_buses:
469 dev->sriov = NULL;
470 dev->is_physfn = 0;
471 failed:
472 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
473 res = &dev->resource[i + PCI_IOV_RESOURCES];
474 res->flags = 0;
477 kfree(iov);
478 return rc;
481 static void sriov_release(struct pci_dev *dev)
483 BUG_ON(dev->sriov->num_VFs);
485 if (dev != dev->sriov->dev)
486 pci_dev_put(dev->sriov->dev);
488 kfree(dev->sriov);
489 dev->sriov = NULL;
492 static void sriov_restore_state(struct pci_dev *dev)
494 int i;
495 u16 ctrl;
496 struct pci_sriov *iov = dev->sriov;
498 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
499 if (ctrl & PCI_SRIOV_CTRL_VFE)
500 return;
503 * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
504 * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
506 ctrl &= ~PCI_SRIOV_CTRL_ARI;
507 ctrl |= iov->ctrl & PCI_SRIOV_CTRL_ARI;
508 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
510 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
511 pci_update_resource(dev, i);
513 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
514 pci_iov_set_numvfs(dev, iov->num_VFs);
515 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
516 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
517 msleep(100);
521 * pci_iov_init - initialize the IOV capability
522 * @dev: the PCI device
524 * Returns 0 on success, or negative on failure.
526 int pci_iov_init(struct pci_dev *dev)
528 int pos;
530 if (!pci_is_pcie(dev))
531 return -ENODEV;
533 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
534 if (pos)
535 return sriov_init(dev, pos);
537 return -ENODEV;
541 * pci_iov_release - release resources used by the IOV capability
542 * @dev: the PCI device
544 void pci_iov_release(struct pci_dev *dev)
546 if (dev->is_physfn)
547 sriov_release(dev);
551 * pci_iov_update_resource - update a VF BAR
552 * @dev: the PCI device
553 * @resno: the resource number
555 * Update a VF BAR in the SR-IOV capability of a PF.
557 void pci_iov_update_resource(struct pci_dev *dev, int resno)
559 struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
560 struct resource *res = dev->resource + resno;
561 int vf_bar = resno - PCI_IOV_RESOURCES;
562 struct pci_bus_region region;
563 u16 cmd;
564 u32 new;
565 int reg;
568 * The generic pci_restore_bars() path calls this for all devices,
569 * including VFs and non-SR-IOV devices. If this is not a PF, we
570 * have nothing to do.
572 if (!iov)
573 return;
575 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
576 if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) {
577 dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
578 vf_bar, res);
579 return;
583 * Ignore unimplemented BARs, unused resource slots for 64-bit
584 * BARs, and non-movable resources, e.g., those described via
585 * Enhanced Allocation.
587 if (!res->flags)
588 return;
590 if (res->flags & IORESOURCE_UNSET)
591 return;
593 if (res->flags & IORESOURCE_PCI_FIXED)
594 return;
596 pcibios_resource_to_bus(dev->bus, &region, res);
597 new = region.start;
598 new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK;
600 reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar;
601 pci_write_config_dword(dev, reg, new);
602 if (res->flags & IORESOURCE_MEM_64) {
603 new = region.start >> 16 >> 16;
604 pci_write_config_dword(dev, reg + 4, new);
608 resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
609 int resno)
611 return pci_iov_resource_size(dev, resno);
615 * pci_sriov_resource_alignment - get resource alignment for VF BAR
616 * @dev: the PCI device
617 * @resno: the resource number
619 * Returns the alignment of the VF BAR found in the SR-IOV capability.
620 * This is not the same as the resource size which is defined as
621 * the VF BAR size multiplied by the number of VFs. The alignment
622 * is just the VF BAR size.
624 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
626 return pcibios_iov_resource_alignment(dev, resno);
630 * pci_restore_iov_state - restore the state of the IOV capability
631 * @dev: the PCI device
633 void pci_restore_iov_state(struct pci_dev *dev)
635 if (dev->is_physfn)
636 sriov_restore_state(dev);
640 * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs
641 * @dev: the PCI device
642 * @auto_probe: set VF drivers auto probe flag
644 void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe)
646 if (dev->is_physfn)
647 dev->sriov->drivers_autoprobe = auto_probe;
651 * pci_iov_bus_range - find bus range used by Virtual Function
652 * @bus: the PCI bus
654 * Returns max number of buses (exclude current one) used by Virtual
655 * Functions.
657 int pci_iov_bus_range(struct pci_bus *bus)
659 int max = 0;
660 struct pci_dev *dev;
662 list_for_each_entry(dev, &bus->devices, bus_list) {
663 if (!dev->is_physfn)
664 continue;
665 if (dev->sriov->max_VF_buses > max)
666 max = dev->sriov->max_VF_buses;
669 return max ? max - bus->number : 0;
673 * pci_enable_sriov - enable the SR-IOV capability
674 * @dev: the PCI device
675 * @nr_virtfn: number of virtual functions to enable
677 * Returns 0 on success, or negative on failure.
679 int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
681 might_sleep();
683 if (!dev->is_physfn)
684 return -ENOSYS;
686 return sriov_enable(dev, nr_virtfn);
688 EXPORT_SYMBOL_GPL(pci_enable_sriov);
691 * pci_disable_sriov - disable the SR-IOV capability
692 * @dev: the PCI device
694 void pci_disable_sriov(struct pci_dev *dev)
696 might_sleep();
698 if (!dev->is_physfn)
699 return;
701 sriov_disable(dev);
703 EXPORT_SYMBOL_GPL(pci_disable_sriov);
706 * pci_num_vf - return number of VFs associated with a PF device_release_driver
707 * @dev: the PCI device
709 * Returns number of VFs, or 0 if SR-IOV is not enabled.
711 int pci_num_vf(struct pci_dev *dev)
713 if (!dev->is_physfn)
714 return 0;
716 return dev->sriov->num_VFs;
718 EXPORT_SYMBOL_GPL(pci_num_vf);
721 * pci_vfs_assigned - returns number of VFs are assigned to a guest
722 * @dev: the PCI device
724 * Returns number of VFs belonging to this device that are assigned to a guest.
725 * If device is not a physical function returns 0.
727 int pci_vfs_assigned(struct pci_dev *dev)
729 struct pci_dev *vfdev;
730 unsigned int vfs_assigned = 0;
731 unsigned short dev_id;
733 /* only search if we are a PF */
734 if (!dev->is_physfn)
735 return 0;
738 * determine the device ID for the VFs, the vendor ID will be the
739 * same as the PF so there is no need to check for that one
741 dev_id = dev->sriov->vf_device;
743 /* loop through all the VFs to see if we own any that are assigned */
744 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
745 while (vfdev) {
747 * It is considered assigned if it is a virtual function with
748 * our dev as the physical function and the assigned bit is set
750 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
751 pci_is_dev_assigned(vfdev))
752 vfs_assigned++;
754 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
757 return vfs_assigned;
759 EXPORT_SYMBOL_GPL(pci_vfs_assigned);
762 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
763 * @dev: the PCI PF device
764 * @numvfs: number that should be used for TotalVFs supported
766 * Should be called from PF driver's probe routine with
767 * device's mutex held.
769 * Returns 0 if PF is an SRIOV-capable device and
770 * value of numvfs valid. If not a PF return -ENOSYS;
771 * if numvfs is invalid return -EINVAL;
772 * if VFs already enabled, return -EBUSY.
774 int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
776 if (!dev->is_physfn)
777 return -ENOSYS;
778 if (numvfs > dev->sriov->total_VFs)
779 return -EINVAL;
781 /* Shouldn't change if VFs already enabled */
782 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
783 return -EBUSY;
784 else
785 dev->sriov->driver_max_VFs = numvfs;
787 return 0;
789 EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
792 * pci_sriov_get_totalvfs -- get total VFs supported on this device
793 * @dev: the PCI PF device
795 * For a PCIe device with SRIOV support, return the PCIe
796 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
797 * if the driver reduced it. Otherwise 0.
799 int pci_sriov_get_totalvfs(struct pci_dev *dev)
801 if (!dev->is_physfn)
802 return 0;
804 if (dev->sriov->driver_max_VFs)
805 return dev->sriov->driver_max_VFs;
807 return dev->sriov->total_VFs;
809 EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);