4 * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
6 * PCI Express I/O Virtualization (IOV) support.
8 * Address Translation Service 1.0
11 #include <linux/pci.h>
12 #include <linux/slab.h>
13 #include <linux/mutex.h>
14 #include <linux/export.h>
15 #include <linux/string.h>
16 #include <linux/delay.h>
17 #include <linux/pci-ats.h>
20 #define VIRTFN_ID_LEN 16
22 static inline u8 virtfn_bus(struct pci_dev *dev, int id)
24 return dev->bus->number + ((dev->devfn + dev->sriov->offset +
25 dev->sriov->stride * id) >> 8);
28 static inline u8 virtfn_devfn(struct pci_dev *dev, int id)
30 return (dev->devfn + dev->sriov->offset +
31 dev->sriov->stride * id) & 0xff;
35 * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
36 * change when NumVFs changes.
38 * Update iov->offset and iov->stride when NumVFs is written.
40 static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
42 struct pci_sriov *iov = dev->sriov;
44 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
45 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
46 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
50 * The PF consumes one bus number. NumVFs, First VF Offset, and VF Stride
51 * determine how many additional bus numbers will be consumed by VFs.
53 * Iterate over all valid NumVFs and calculate the maximum number of bus
54 * numbers that could ever be required.
56 static inline u8 virtfn_max_buses(struct pci_dev *dev)
58 struct pci_sriov *iov = dev->sriov;
63 for (nr_virtfn = 1; nr_virtfn <= iov->total_VFs; nr_virtfn++) {
64 pci_iov_set_numvfs(dev, nr_virtfn);
65 busnr = virtfn_bus(dev, nr_virtfn - 1);
73 static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
75 struct pci_bus *child;
77 if (bus->number == busnr)
80 child = pci_find_bus(pci_domain_nr(bus), busnr);
84 child = pci_add_new_bus(bus, NULL, busnr);
88 pci_bus_insert_busn_res(child, busnr, busnr);
93 static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
95 if (physbus != virtbus && list_empty(&virtbus->devices))
96 pci_remove_bus(virtbus);
99 resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
104 return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
107 static int virtfn_add(struct pci_dev *dev, int id, int reset)
112 char buf[VIRTFN_ID_LEN];
113 struct pci_dev *virtfn;
114 struct resource *res;
115 struct pci_sriov *iov = dev->sriov;
118 mutex_lock(&iov->dev->sriov->lock);
119 bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
123 virtfn = pci_alloc_dev(bus);
127 virtfn->devfn = virtfn_devfn(dev, id);
128 virtfn->vendor = dev->vendor;
129 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device);
130 pci_setup_device(virtfn);
131 virtfn->dev.parent = dev->dev.parent;
132 virtfn->physfn = pci_dev_get(dev);
133 virtfn->is_virtfn = 1;
134 virtfn->multifunction = 0;
136 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
137 res = &dev->resource[i + PCI_IOV_RESOURCES];
140 virtfn->resource[i].name = pci_name(virtfn);
141 virtfn->resource[i].flags = res->flags;
142 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
143 virtfn->resource[i].start = res->start + size * id;
144 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
145 rc = request_resource(res, &virtfn->resource[i]);
150 __pci_reset_function(virtfn);
152 pci_device_add(virtfn, virtfn->bus);
153 mutex_unlock(&iov->dev->sriov->lock);
155 pci_bus_add_device(virtfn);
156 sprintf(buf, "virtfn%u", id);
157 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
160 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
164 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
169 sysfs_remove_link(&dev->dev.kobj, buf);
172 mutex_lock(&iov->dev->sriov->lock);
173 pci_stop_and_remove_bus_device(virtfn);
175 virtfn_remove_bus(dev->bus, bus);
177 mutex_unlock(&iov->dev->sriov->lock);
182 static void virtfn_remove(struct pci_dev *dev, int id, int reset)
184 char buf[VIRTFN_ID_LEN];
185 struct pci_dev *virtfn;
186 struct pci_sriov *iov = dev->sriov;
188 virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
190 virtfn_devfn(dev, id));
195 device_release_driver(&virtfn->dev);
196 __pci_reset_function(virtfn);
199 sprintf(buf, "virtfn%u", id);
200 sysfs_remove_link(&dev->dev.kobj, buf);
202 * pci_stop_dev() could have been called for this virtfn already,
203 * so the directory for the virtfn may have been removed before.
204 * Double check to avoid spurious sysfs warnings.
206 if (virtfn->dev.kobj.sd)
207 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
209 mutex_lock(&iov->dev->sriov->lock);
210 pci_stop_and_remove_bus_device(virtfn);
211 virtfn_remove_bus(dev->bus, virtfn->bus);
212 mutex_unlock(&iov->dev->sriov->lock);
214 /* balance pci_get_domain_bus_and_slot() */
219 static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
224 u16 offset, stride, initial;
225 struct resource *res;
226 struct pci_dev *pdev;
227 struct pci_sriov *iov = dev->sriov;
237 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
238 if (initial > iov->total_VFs ||
239 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
242 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
243 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
246 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset);
247 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride);
248 if (!offset || (nr_virtfn > 1 && !stride))
252 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
253 bars |= (1 << (i + PCI_IOV_RESOURCES));
254 res = &dev->resource[i + PCI_IOV_RESOURCES];
258 if (nres != iov->nres) {
259 dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
263 iov->offset = offset;
264 iov->stride = stride;
266 bus = virtfn_bus(dev, nr_virtfn - 1);
267 if (bus > dev->bus->busn_res.end) {
268 dev_err(&dev->dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
269 nr_virtfn, bus, &dev->bus->busn_res);
273 if (pci_enable_resources(dev, bars)) {
274 dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
278 if (iov->link != dev->devfn) {
279 pdev = pci_get_slot(dev->bus, iov->link);
283 if (!pdev->is_physfn) {
288 rc = sysfs_create_link(&dev->dev.kobj,
289 &pdev->dev.kobj, "dep_link");
295 pci_iov_set_numvfs(dev, nr_virtfn);
296 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
297 pci_cfg_access_lock(dev);
298 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
300 pci_cfg_access_unlock(dev);
302 iov->initial_VFs = initial;
303 if (nr_virtfn < initial)
306 for (i = 0; i < initial; i++) {
307 rc = virtfn_add(dev, i, 0);
312 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
313 iov->num_VFs = nr_virtfn;
318 for (j = 0; j < i; j++)
319 virtfn_remove(dev, j, 0);
321 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
322 pci_cfg_access_lock(dev);
323 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
324 pci_iov_set_numvfs(dev, 0);
326 pci_cfg_access_unlock(dev);
328 if (iov->link != dev->devfn)
329 sysfs_remove_link(&dev->dev.kobj, "dep_link");
334 static void sriov_disable(struct pci_dev *dev)
337 struct pci_sriov *iov = dev->sriov;
342 for (i = 0; i < iov->num_VFs; i++)
343 virtfn_remove(dev, i, 0);
345 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
346 pci_cfg_access_lock(dev);
347 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
349 pci_cfg_access_unlock(dev);
351 if (iov->link != dev->devfn)
352 sysfs_remove_link(&dev->dev.kobj, "dep_link");
355 pci_iov_set_numvfs(dev, 0);
358 static int sriov_init(struct pci_dev *dev, int pos)
364 u16 ctrl, total, offset, stride;
365 struct pci_sriov *iov;
366 struct resource *res;
367 struct pci_dev *pdev;
369 if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
370 pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
373 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
374 if (ctrl & PCI_SRIOV_CTRL_VFE) {
375 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
379 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
384 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
389 if (pci_ari_enabled(dev->bus))
390 ctrl |= PCI_SRIOV_CTRL_ARI;
393 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
394 pci_write_config_word(dev, pos + PCI_SRIOV_NUM_VF, 0);
395 pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset);
396 pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride);
397 if (!offset || (total > 1 && !stride))
400 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
401 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
402 pgsz &= ~((1 << i) - 1);
407 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
409 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
414 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
415 res = &dev->resource[i + PCI_IOV_RESOURCES];
416 bar64 = __pci_read_base(dev, pci_bar_unknown, res,
417 pos + PCI_SRIOV_BAR + i * 4);
420 if (resource_size(res) & (PAGE_SIZE - 1)) {
424 iov->barsz[i] = resource_size(res);
425 res->end = res->start + resource_size(res) * total - 1;
426 dev_info(&dev->dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
435 iov->total_VFs = total;
436 iov->offset = offset;
437 iov->stride = stride;
440 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
441 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
442 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
443 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
446 iov->dev = pci_dev_get(pdev);
450 mutex_init(&iov->lock);
454 iov->max_VF_buses = virtfn_max_buses(dev);
459 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
460 res = &dev->resource[i + PCI_IOV_RESOURCES];
468 static void sriov_release(struct pci_dev *dev)
470 BUG_ON(dev->sriov->num_VFs);
472 if (dev != dev->sriov->dev)
473 pci_dev_put(dev->sriov->dev);
475 mutex_destroy(&dev->sriov->lock);
481 static void sriov_restore_state(struct pci_dev *dev)
485 struct pci_sriov *iov = dev->sriov;
487 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
488 if (ctrl & PCI_SRIOV_CTRL_VFE)
491 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
492 pci_update_resource(dev, i);
494 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
495 pci_iov_set_numvfs(dev, iov->num_VFs);
496 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
497 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
502 * pci_iov_init - initialize the IOV capability
503 * @dev: the PCI device
505 * Returns 0 on success, or negative on failure.
507 int pci_iov_init(struct pci_dev *dev)
511 if (!pci_is_pcie(dev))
514 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
516 return sriov_init(dev, pos);
522 * pci_iov_release - release resources used by the IOV capability
523 * @dev: the PCI device
525 void pci_iov_release(struct pci_dev *dev)
532 * pci_iov_resource_bar - get position of the SR-IOV BAR
533 * @dev: the PCI device
534 * @resno: the resource number
536 * Returns position of the BAR encapsulated in the SR-IOV capability.
538 int pci_iov_resource_bar(struct pci_dev *dev, int resno)
540 if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
543 BUG_ON(!dev->is_physfn);
545 return dev->sriov->pos + PCI_SRIOV_BAR +
546 4 * (resno - PCI_IOV_RESOURCES);
550 * pci_sriov_resource_alignment - get resource alignment for VF BAR
551 * @dev: the PCI device
552 * @resno: the resource number
554 * Returns the alignment of the VF BAR found in the SR-IOV capability.
555 * This is not the same as the resource size which is defined as
556 * the VF BAR size multiplied by the number of VFs. The alignment
557 * is just the VF BAR size.
559 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
561 return pci_iov_resource_size(dev, resno);
565 * pci_restore_iov_state - restore the state of the IOV capability
566 * @dev: the PCI device
568 void pci_restore_iov_state(struct pci_dev *dev)
571 sriov_restore_state(dev);
575 * pci_iov_bus_range - find bus range used by Virtual Function
578 * Returns max number of buses (exclude current one) used by Virtual
581 int pci_iov_bus_range(struct pci_bus *bus)
586 list_for_each_entry(dev, &bus->devices, bus_list) {
589 if (dev->sriov->max_VF_buses > max)
590 max = dev->sriov->max_VF_buses;
593 return max ? max - bus->number : 0;
597 * pci_enable_sriov - enable the SR-IOV capability
598 * @dev: the PCI device
599 * @nr_virtfn: number of virtual functions to enable
601 * Returns 0 on success, or negative on failure.
603 int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
610 return sriov_enable(dev, nr_virtfn);
612 EXPORT_SYMBOL_GPL(pci_enable_sriov);
615 * pci_disable_sriov - disable the SR-IOV capability
616 * @dev: the PCI device
618 void pci_disable_sriov(struct pci_dev *dev)
627 EXPORT_SYMBOL_GPL(pci_disable_sriov);
630 * pci_num_vf - return number of VFs associated with a PF device_release_driver
631 * @dev: the PCI device
633 * Returns number of VFs, or 0 if SR-IOV is not enabled.
635 int pci_num_vf(struct pci_dev *dev)
640 return dev->sriov->num_VFs;
642 EXPORT_SYMBOL_GPL(pci_num_vf);
645 * pci_vfs_assigned - returns number of VFs are assigned to a guest
646 * @dev: the PCI device
648 * Returns number of VFs belonging to this device that are assigned to a guest.
649 * If device is not a physical function returns 0.
651 int pci_vfs_assigned(struct pci_dev *dev)
653 struct pci_dev *vfdev;
654 unsigned int vfs_assigned = 0;
655 unsigned short dev_id;
657 /* only search if we are a PF */
662 * determine the device ID for the VFs, the vendor ID will be the
663 * same as the PF so there is no need to check for that one
665 pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id);
667 /* loop through all the VFs to see if we own any that are assigned */
668 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
671 * It is considered assigned if it is a virtual function with
672 * our dev as the physical function and the assigned bit is set
674 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
675 pci_is_dev_assigned(vfdev))
678 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
683 EXPORT_SYMBOL_GPL(pci_vfs_assigned);
686 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
687 * @dev: the PCI PF device
688 * @numvfs: number that should be used for TotalVFs supported
690 * Should be called from PF driver's probe routine with
691 * device's mutex held.
693 * Returns 0 if PF is an SRIOV-capable device and
694 * value of numvfs valid. If not a PF return -ENOSYS;
695 * if numvfs is invalid return -EINVAL;
696 * if VFs already enabled, return -EBUSY.
698 int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
702 if (numvfs > dev->sriov->total_VFs)
705 /* Shouldn't change if VFs already enabled */
706 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
709 dev->sriov->driver_max_VFs = numvfs;
713 EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
716 * pci_sriov_get_totalvfs -- get total VFs supported on this device
717 * @dev: the PCI PF device
719 * For a PCIe device with SRIOV support, return the PCIe
720 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
721 * if the driver reduced it. Otherwise 0.
723 int pci_sriov_get_totalvfs(struct pci_dev *dev)
728 if (dev->sriov->driver_max_VFs)
729 return dev->sriov->driver_max_VFs;
731 return dev->sriov->total_VFs;
733 EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);