Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / pci / iov.c
blob4afd4ee4f7f04f8d4aed5c7d0f9fc79dd547be0d
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/export.h>
13 #include <linux/string.h>
14 #include <linux/delay.h>
15 #include "pci.h"
17 #define VIRTFN_ID_LEN 16
19 int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id)
21 if (!dev->is_physfn)
22 return -EINVAL;
23 return dev->bus->number + ((dev->devfn + dev->sriov->offset +
24 dev->sriov->stride * vf_id) >> 8);
27 int pci_iov_virtfn_devfn(struct pci_dev *dev, int vf_id)
29 if (!dev->is_physfn)
30 return -EINVAL;
31 return (dev->devfn + dev->sriov->offset +
32 dev->sriov->stride * vf_id) & 0xff;
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.
39 * Update iov->offset and iov->stride when NumVFs is written.
41 static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
43 struct pci_sriov *iov = dev->sriov;
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);
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.
54 * Iterate over all valid NumVFs, validate offset and stride, and calculate
55 * the maximum number of bus numbers that could ever be required.
57 static int compute_max_vf_buses(struct pci_dev *dev)
59 struct pci_sriov *iov = dev->sriov;
60 int nr_virtfn, busnr, rc = 0;
62 for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) {
63 pci_iov_set_numvfs(dev, nr_virtfn);
64 if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) {
65 rc = -EIO;
66 goto out;
69 busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
70 if (busnr > iov->max_VF_buses)
71 iov->max_VF_buses = busnr;
74 out:
75 pci_iov_set_numvfs(dev, 0);
76 return rc;
79 static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
81 struct pci_bus *child;
83 if (bus->number == busnr)
84 return bus;
86 child = pci_find_bus(pci_domain_nr(bus), busnr);
87 if (child)
88 return child;
90 child = pci_add_new_bus(bus, NULL, busnr);
91 if (!child)
92 return NULL;
94 pci_bus_insert_busn_res(child, busnr, busnr);
96 return child;
99 static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
101 if (physbus != virtbus && list_empty(&virtbus->devices))
102 pci_remove_bus(virtbus);
105 resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
107 if (!dev->is_physfn)
108 return 0;
110 return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
113 static void pci_read_vf_config_common(struct pci_dev *virtfn)
115 struct pci_dev *physfn = virtfn->physfn;
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.
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.
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);
136 int pci_iov_sysfs_link(struct pci_dev *dev,
137 struct pci_dev *virtfn, int id)
139 char buf[VIRTFN_ID_LEN];
140 int rc;
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;
150 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
152 return 0;
154 failed1:
155 sysfs_remove_link(&dev->dev.kobj, buf);
156 failed:
157 return rc;
160 int pci_iov_add_virtfn(struct pci_dev *dev, int id)
162 int i;
163 int rc = -ENOMEM;
164 u64 size;
165 struct pci_dev *virtfn;
166 struct resource *res;
167 struct pci_sriov *iov = dev->sriov;
168 struct pci_bus *bus;
170 bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
171 if (!bus)
172 goto failed;
174 virtfn = pci_alloc_dev(bus);
175 if (!virtfn)
176 goto failed0;
178 virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
179 virtfn->vendor = dev->vendor;
180 virtfn->device = iov->vf_device;
181 virtfn->is_virtfn = 1;
182 virtfn->physfn = pci_dev_get(dev);
183 virtfn->no_command_memory = 1;
185 if (id == 0)
186 pci_read_vf_config_common(virtfn);
188 rc = pci_setup_device(virtfn);
189 if (rc)
190 goto failed1;
192 virtfn->dev.parent = dev->dev.parent;
193 virtfn->multifunction = 0;
195 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
196 res = &dev->resource[i + PCI_IOV_RESOURCES];
197 if (!res->parent)
198 continue;
199 virtfn->resource[i].name = pci_name(virtfn);
200 virtfn->resource[i].flags = res->flags;
201 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
202 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);
208 pci_device_add(virtfn, virtfn->bus);
209 rc = pci_iov_sysfs_link(dev, virtfn, id);
210 if (rc)
211 goto failed1;
213 pci_bus_add_device(virtfn);
215 return 0;
217 failed1:
218 pci_stop_and_remove_bus_device(virtfn);
219 pci_dev_put(dev);
220 failed0:
221 virtfn_remove_bus(dev->bus, bus);
222 failed:
224 return rc;
227 void pci_iov_remove_virtfn(struct pci_dev *dev, int id)
229 char buf[VIRTFN_ID_LEN];
230 struct pci_dev *virtfn;
232 virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
233 pci_iov_virtfn_bus(dev, id),
234 pci_iov_virtfn_devfn(dev, id));
235 if (!virtfn)
236 return;
238 sprintf(buf, "virtfn%u", id);
239 sysfs_remove_link(&dev->dev.kobj, buf);
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.
245 if (virtfn->dev.kobj.sd)
246 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
248 pci_stop_and_remove_bus_device(virtfn);
249 virtfn_remove_bus(dev->bus, virtfn->bus);
251 /* balance pci_get_domain_bus_and_slot() */
252 pci_dev_put(virtfn);
253 pci_dev_put(dev);
256 static ssize_t sriov_totalvfs_show(struct device *dev,
257 struct device_attribute *attr,
258 char *buf)
260 struct pci_dev *pdev = to_pci_dev(dev);
262 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
265 static ssize_t sriov_numvfs_show(struct device *dev,
266 struct device_attribute *attr,
267 char *buf)
269 struct pci_dev *pdev = to_pci_dev(dev);
270 u16 num_vfs;
272 /* 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);
277 return sprintf(buf, "%u\n", num_vfs);
281 * num_vfs > 0; number of VFs to enable
282 * num_vfs = 0; disable all VFs
284 * Note: SRIOV spec does not allow partial VF
285 * disable, so it's all or none.
287 static ssize_t sriov_numvfs_store(struct device *dev,
288 struct device_attribute *attr,
289 const char *buf, size_t count)
291 struct pci_dev *pdev = to_pci_dev(dev);
292 int ret;
293 u16 num_vfs;
295 ret = kstrtou16(buf, 0, &num_vfs);
296 if (ret < 0)
297 return ret;
299 if (num_vfs > pci_sriov_get_totalvfs(pdev))
300 return -ERANGE;
302 device_lock(&pdev->dev);
304 if (num_vfs == pdev->sriov->num_VFs)
305 goto exit;
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;
314 if (num_vfs == 0) {
315 /* disable VFs */
316 ret = pdev->driver->sriov_configure(pdev, 0);
317 goto exit;
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;
328 ret = pdev->driver->sriov_configure(pdev, num_vfs);
329 if (ret < 0)
330 goto exit;
332 if (ret != num_vfs)
333 pci_warn(pdev, "%d VFs requested; only %d enabled\n",
334 num_vfs, ret);
336 exit:
337 device_unlock(&pdev->dev);
339 if (ret < 0)
340 return ret;
342 return count;
345 static ssize_t sriov_offset_show(struct device *dev,
346 struct device_attribute *attr,
347 char *buf)
349 struct pci_dev *pdev = to_pci_dev(dev);
351 return sprintf(buf, "%u\n", pdev->sriov->offset);
354 static ssize_t sriov_stride_show(struct device *dev,
355 struct device_attribute *attr,
356 char *buf)
358 struct pci_dev *pdev = to_pci_dev(dev);
360 return sprintf(buf, "%u\n", pdev->sriov->stride);
363 static ssize_t sriov_vf_device_show(struct device *dev,
364 struct device_attribute *attr,
365 char *buf)
367 struct pci_dev *pdev = to_pci_dev(dev);
369 return sprintf(buf, "%x\n", pdev->sriov->vf_device);
372 static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
373 struct device_attribute *attr,
374 char *buf)
376 struct pci_dev *pdev = to_pci_dev(dev);
378 return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe);
381 static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
382 struct device_attribute *attr,
383 const char *buf, size_t count)
385 struct pci_dev *pdev = to_pci_dev(dev);
386 bool drivers_autoprobe;
388 if (kstrtobool(buf, &drivers_autoprobe) < 0)
389 return -EINVAL;
391 pdev->sriov->drivers_autoprobe = drivers_autoprobe;
393 return count;
396 static DEVICE_ATTR_RO(sriov_totalvfs);
397 static DEVICE_ATTR_RW(sriov_numvfs);
398 static DEVICE_ATTR_RO(sriov_offset);
399 static DEVICE_ATTR_RO(sriov_stride);
400 static DEVICE_ATTR_RO(sriov_vf_device);
401 static DEVICE_ATTR_RW(sriov_drivers_autoprobe);
403 static 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,
413 static umode_t sriov_attrs_are_visible(struct kobject *kobj,
414 struct attribute *a, int n)
416 struct device *dev = kobj_to_dev(kobj);
418 if (!dev_is_pf(dev))
419 return 0;
421 return a->mode;
424 const struct attribute_group sriov_dev_attr_group = {
425 .attrs = sriov_dev_attrs,
426 .is_visible = sriov_attrs_are_visible,
429 int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
431 return 0;
434 int __weak pcibios_sriov_disable(struct pci_dev *pdev)
436 return 0;
439 static int sriov_add_vfs(struct pci_dev *dev, u16 num_vfs)
441 unsigned int i;
442 int rc;
444 if (dev->no_vf_scan)
445 return 0;
447 for (i = 0; i < num_vfs; i++) {
448 rc = pci_iov_add_virtfn(dev, i);
449 if (rc)
450 goto failed;
452 return 0;
453 failed:
454 while (i--)
455 pci_iov_remove_virtfn(dev, i);
457 return rc;
460 static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
462 int rc;
463 int i;
464 int nres;
465 u16 initial;
466 struct resource *res;
467 struct pci_dev *pdev;
468 struct pci_sriov *iov = dev->sriov;
469 int bars = 0;
470 int bus;
472 if (!nr_virtfn)
473 return 0;
475 if (iov->num_VFs)
476 return -EINVAL;
478 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
479 if (initial > iov->total_VFs ||
480 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
481 return -EIO;
483 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
484 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
485 return -EINVAL;
487 nres = 0;
488 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
489 bars |= (1 << (i + PCI_IOV_RESOURCES));
490 res = &dev->resource[i + PCI_IOV_RESOURCES];
491 if (res->parent)
492 nres++;
494 if (nres != iov->nres) {
495 pci_err(dev, "not enough MMIO resources for SR-IOV\n");
496 return -ENOMEM;
499 bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
500 if (bus > dev->bus->busn_res.end) {
501 pci_err(dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
502 nr_virtfn, bus, &dev->bus->busn_res);
503 return -ENOMEM;
506 if (pci_enable_resources(dev, bars)) {
507 pci_err(dev, "SR-IOV: IOV BARS not allocated\n");
508 return -ENOMEM;
511 if (iov->link != dev->devfn) {
512 pdev = pci_get_slot(dev->bus, iov->link);
513 if (!pdev)
514 return -ENODEV;
516 if (!pdev->is_physfn) {
517 pci_dev_put(pdev);
518 return -ENOSYS;
521 rc = sysfs_create_link(&dev->dev.kobj,
522 &pdev->dev.kobj, "dep_link");
523 pci_dev_put(pdev);
524 if (rc)
525 return rc;
528 iov->initial_VFs = initial;
529 if (nr_virtfn < initial)
530 initial = nr_virtfn;
532 rc = pcibios_sriov_enable(dev, initial);
533 if (rc) {
534 pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc);
535 goto err_pcibios;
538 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);
545 rc = sriov_add_vfs(dev, initial);
546 if (rc)
547 goto err_pcibios;
549 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
550 iov->num_VFs = nr_virtfn;
552 return 0;
554 err_pcibios:
555 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
556 pci_cfg_access_lock(dev);
557 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
558 ssleep(1);
559 pci_cfg_access_unlock(dev);
561 pcibios_sriov_disable(dev);
563 if (iov->link != dev->devfn)
564 sysfs_remove_link(&dev->dev.kobj, "dep_link");
566 pci_iov_set_numvfs(dev, 0);
567 return rc;
570 static void sriov_del_vfs(struct pci_dev *dev)
572 struct pci_sriov *iov = dev->sriov;
573 int i;
575 for (i = 0; i < iov->num_VFs; i++)
576 pci_iov_remove_virtfn(dev, i);
579 static void sriov_disable(struct pci_dev *dev)
581 struct pci_sriov *iov = dev->sriov;
583 if (!iov->num_VFs)
584 return;
586 sriov_del_vfs(dev);
587 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
588 pci_cfg_access_lock(dev);
589 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
590 ssleep(1);
591 pci_cfg_access_unlock(dev);
593 pcibios_sriov_disable(dev);
595 if (iov->link != dev->devfn)
596 sysfs_remove_link(&dev->dev.kobj, "dep_link");
598 iov->num_VFs = 0;
599 pci_iov_set_numvfs(dev, 0);
602 static int sriov_init(struct pci_dev *dev, int pos)
604 int i, bar64;
605 int rc;
606 int nres;
607 u32 pgsz;
608 u16 ctrl, total;
609 struct pci_sriov *iov;
610 struct resource *res;
611 struct pci_dev *pdev;
613 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);
619 ctrl = 0;
620 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
621 if (pdev->is_physfn)
622 goto found;
624 pdev = NULL;
625 if (pci_ari_enabled(dev->bus))
626 ctrl |= PCI_SRIOV_CTRL_ARI;
628 found:
629 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
631 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
632 if (!total)
633 return 0;
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;
641 pgsz &= ~(pgsz - 1);
642 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
644 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
645 if (!iov)
646 return -ENOMEM;
648 nres = 0;
649 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
650 res = &dev->resource[i + PCI_IOV_RESOURCES];
652 * If it is already FIXED, don't change it, something
653 * (perhaps EA or header fixups) wants it this way.
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);
660 if (!res->flags)
661 continue;
662 if (resource_size(res) & (PAGE_SIZE - 1)) {
663 rc = -EIO;
664 goto failed;
666 iov->barsz[i] = resource_size(res);
667 res->end = res->start + resource_size(res) * total - 1;
668 pci_info(dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
669 i, res, i, total);
670 i += bar64;
671 nres++;
674 iov->pos = pos;
675 iov->nres = nres;
676 iov->ctrl = ctrl;
677 iov->total_VFs = total;
678 iov->driver_max_VFs = total;
679 pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
680 iov->pgsz = pgsz;
681 iov->self = dev;
682 iov->drivers_autoprobe = true;
683 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
684 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
685 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
686 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
688 if (pdev)
689 iov->dev = pci_dev_get(pdev);
690 else
691 iov->dev = dev;
693 dev->sriov = iov;
694 dev->is_physfn = 1;
695 rc = compute_max_vf_buses(dev);
696 if (rc)
697 goto fail_max_buses;
699 return 0;
701 fail_max_buses:
702 dev->sriov = NULL;
703 dev->is_physfn = 0;
704 failed:
705 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
706 res = &dev->resource[i + PCI_IOV_RESOURCES];
707 res->flags = 0;
710 kfree(iov);
711 return rc;
714 static void sriov_release(struct pci_dev *dev)
716 BUG_ON(dev->sriov->num_VFs);
718 if (dev != dev->sriov->dev)
719 pci_dev_put(dev->sriov->dev);
721 kfree(dev->sriov);
722 dev->sriov = NULL;
725 static void sriov_restore_state(struct pci_dev *dev)
727 int i;
728 u16 ctrl;
729 struct pci_sriov *iov = dev->sriov;
731 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
732 if (ctrl & PCI_SRIOV_CTRL_VFE)
733 return;
736 * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
737 * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
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);
743 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
744 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
746 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
747 pci_iov_set_numvfs(dev, iov->num_VFs);
748 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
749 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
750 msleep(100);
754 * pci_iov_init - initialize the IOV capability
755 * @dev: the PCI device
757 * Returns 0 on success, or negative on failure.
759 int pci_iov_init(struct pci_dev *dev)
761 int pos;
763 if (!pci_is_pcie(dev))
764 return -ENODEV;
766 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
767 if (pos)
768 return sriov_init(dev, pos);
770 return -ENODEV;
774 * pci_iov_release - release resources used by the IOV capability
775 * @dev: the PCI device
777 void pci_iov_release(struct pci_dev *dev)
779 if (dev->is_physfn)
780 sriov_release(dev);
784 * pci_iov_remove - clean up SR-IOV state after PF driver is detached
785 * @dev: the PCI device
787 void pci_iov_remove(struct pci_dev *dev)
789 struct pci_sriov *iov = dev->sriov;
791 if (!dev->is_physfn)
792 return;
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");
800 * pci_iov_update_resource - update a VF BAR
801 * @dev: the PCI device
802 * @resno: the resource number
804 * Update a VF BAR in the SR-IOV capability of a PF.
806 void pci_iov_update_resource(struct pci_dev *dev, int resno)
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;
812 u16 cmd;
813 u32 new;
814 int reg;
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.
821 if (!iov)
822 return;
824 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;
832 * Ignore unimplemented BARs, unused resource slots for 64-bit
833 * BARs, and non-movable resources, e.g., those described via
834 * Enhanced Allocation.
836 if (!res->flags)
837 return;
839 if (res->flags & IORESOURCE_UNSET)
840 return;
842 if (res->flags & IORESOURCE_PCI_FIXED)
843 return;
845 pcibios_resource_to_bus(dev->bus, &region, res);
846 new = region.start;
847 new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK;
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);
857 resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
858 int resno)
860 return pci_iov_resource_size(dev, resno);
864 * pci_sriov_resource_alignment - get resource alignment for VF BAR
865 * @dev: the PCI device
866 * @resno: the resource number
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.
873 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
875 return pcibios_iov_resource_alignment(dev, resno);
879 * pci_restore_iov_state - restore the state of the IOV capability
880 * @dev: the PCI device
882 void pci_restore_iov_state(struct pci_dev *dev)
884 if (dev->is_physfn)
885 sriov_restore_state(dev);
889 * 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
893 void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe)
895 if (dev->is_physfn)
896 dev->sriov->drivers_autoprobe = auto_probe;
900 * pci_iov_bus_range - find bus range used by Virtual Function
901 * @bus: the PCI bus
903 * Returns max number of buses (exclude current one) used by Virtual
904 * Functions.
906 int pci_iov_bus_range(struct pci_bus *bus)
908 int max = 0;
909 struct pci_dev *dev;
911 list_for_each_entry(dev, &bus->devices, bus_list) {
912 if (!dev->is_physfn)
913 continue;
914 if (dev->sriov->max_VF_buses > max)
915 max = dev->sriov->max_VF_buses;
918 return max ? max - bus->number : 0;
922 * pci_enable_sriov - enable the SR-IOV capability
923 * @dev: the PCI device
924 * @nr_virtfn: number of virtual functions to enable
926 * Returns 0 on success, or negative on failure.
928 int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
930 might_sleep();
932 if (!dev->is_physfn)
933 return -ENOSYS;
935 return sriov_enable(dev, nr_virtfn);
937 EXPORT_SYMBOL_GPL(pci_enable_sriov);
940 * pci_disable_sriov - disable the SR-IOV capability
941 * @dev: the PCI device
943 void pci_disable_sriov(struct pci_dev *dev)
945 might_sleep();
947 if (!dev->is_physfn)
948 return;
950 sriov_disable(dev);
952 EXPORT_SYMBOL_GPL(pci_disable_sriov);
955 * pci_num_vf - return number of VFs associated with a PF device_release_driver
956 * @dev: the PCI device
958 * Returns number of VFs, or 0 if SR-IOV is not enabled.
960 int pci_num_vf(struct pci_dev *dev)
962 if (!dev->is_physfn)
963 return 0;
965 return dev->sriov->num_VFs;
967 EXPORT_SYMBOL_GPL(pci_num_vf);
970 * pci_vfs_assigned - returns number of VFs are assigned to a guest
971 * @dev: the PCI device
973 * Returns number of VFs belonging to this device that are assigned to a guest.
974 * If device is not a physical function returns 0.
976 int pci_vfs_assigned(struct pci_dev *dev)
978 struct pci_dev *vfdev;
979 unsigned int vfs_assigned = 0;
980 unsigned short dev_id;
982 /* only search if we are a PF */
983 if (!dev->is_physfn)
984 return 0;
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
990 dev_id = dev->sriov->vf_device;
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) {
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
999 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
1000 pci_is_dev_assigned(vfdev))
1001 vfs_assigned++;
1003 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
1006 return vfs_assigned;
1008 EXPORT_SYMBOL_GPL(pci_vfs_assigned);
1011 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
1012 * @dev: the PCI PF device
1013 * @numvfs: number that should be used for TotalVFs supported
1015 * Should be called from PF driver's probe routine with
1016 * device's mutex held.
1018 * Returns 0 if PF is an SRIOV-capable device and
1019 * value of numvfs valid. If not a PF return -ENOSYS;
1020 * if numvfs is invalid return -EINVAL;
1021 * if VFs already enabled, return -EBUSY.
1023 int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
1025 if (!dev->is_physfn)
1026 return -ENOSYS;
1028 if (numvfs > dev->sriov->total_VFs)
1029 return -EINVAL;
1031 /* Shouldn't change if VFs already enabled */
1032 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
1033 return -EBUSY;
1035 dev->sriov->driver_max_VFs = numvfs;
1036 return 0;
1038 EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
1041 * pci_sriov_get_totalvfs -- get total VFs supported on this device
1042 * @dev: the PCI PF device
1044 * For a PCIe device with SRIOV support, return the PCIe
1045 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
1046 * if the driver reduced it. Otherwise 0.
1048 int pci_sriov_get_totalvfs(struct pci_dev *dev)
1050 if (!dev->is_physfn)
1051 return 0;
1053 return dev->sriov->driver_max_VFs;
1055 EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);
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
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.
1066 int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn)
1068 int rc;
1070 might_sleep();
1072 if (!dev->is_physfn)
1073 return -ENODEV;
1075 if (pci_vfs_assigned(dev)) {
1076 pci_warn(dev, "Cannot modify SR-IOV while VFs are assigned\n");
1077 return -EPERM;
1080 if (nr_virtfn == 0) {
1081 sriov_disable(dev);
1082 return 0;
1085 rc = sriov_enable(dev, nr_virtfn);
1086 if (rc < 0)
1087 return rc;
1089 return nr_virtfn;
1091 EXPORT_SYMBOL_GPL(pci_sriov_configure_simple);