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);
49 static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
51 struct pci_bus *child;
53 if (bus->number == busnr)
56 child = pci_find_bus(pci_domain_nr(bus), busnr);
60 child = pci_add_new_bus(bus, NULL, busnr);
64 pci_bus_insert_busn_res(child, busnr, busnr);
69 static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
71 if (physbus != virtbus && list_empty(&virtbus->devices))
72 pci_remove_bus(virtbus);
75 resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
80 return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
83 static int virtfn_add(struct pci_dev *dev, int id, int reset)
88 char buf[VIRTFN_ID_LEN];
89 struct pci_dev *virtfn;
91 struct pci_sriov *iov = dev->sriov;
94 mutex_lock(&iov->dev->sriov->lock);
95 bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
99 virtfn = pci_alloc_dev(bus);
103 virtfn->devfn = virtfn_devfn(dev, id);
104 virtfn->vendor = dev->vendor;
105 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device);
106 pci_setup_device(virtfn);
107 virtfn->dev.parent = dev->dev.parent;
108 virtfn->physfn = pci_dev_get(dev);
109 virtfn->is_virtfn = 1;
110 virtfn->multifunction = 0;
112 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
113 res = &dev->resource[i + PCI_IOV_RESOURCES];
116 virtfn->resource[i].name = pci_name(virtfn);
117 virtfn->resource[i].flags = res->flags;
118 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
119 virtfn->resource[i].start = res->start + size * id;
120 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
121 rc = request_resource(res, &virtfn->resource[i]);
126 __pci_reset_function(virtfn);
128 pci_device_add(virtfn, virtfn->bus);
129 mutex_unlock(&iov->dev->sriov->lock);
131 pci_bus_add_device(virtfn);
132 sprintf(buf, "virtfn%u", id);
133 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
136 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
140 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
145 sysfs_remove_link(&dev->dev.kobj, buf);
148 mutex_lock(&iov->dev->sriov->lock);
149 pci_stop_and_remove_bus_device(virtfn);
151 virtfn_remove_bus(dev->bus, bus);
153 mutex_unlock(&iov->dev->sriov->lock);
158 static void virtfn_remove(struct pci_dev *dev, int id, int reset)
160 char buf[VIRTFN_ID_LEN];
161 struct pci_dev *virtfn;
162 struct pci_sriov *iov = dev->sriov;
164 virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
166 virtfn_devfn(dev, id));
171 device_release_driver(&virtfn->dev);
172 __pci_reset_function(virtfn);
175 sprintf(buf, "virtfn%u", id);
176 sysfs_remove_link(&dev->dev.kobj, buf);
178 * pci_stop_dev() could have been called for this virtfn already,
179 * so the directory for the virtfn may have been removed before.
180 * Double check to avoid spurious sysfs warnings.
182 if (virtfn->dev.kobj.sd)
183 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
185 mutex_lock(&iov->dev->sriov->lock);
186 pci_stop_and_remove_bus_device(virtfn);
187 virtfn_remove_bus(dev->bus, virtfn->bus);
188 mutex_unlock(&iov->dev->sriov->lock);
190 /* balance pci_get_domain_bus_and_slot() */
195 static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
200 u16 offset, stride, initial;
201 struct resource *res;
202 struct pci_dev *pdev;
203 struct pci_sriov *iov = dev->sriov;
213 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
214 if (initial > iov->total_VFs ||
215 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
218 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
219 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
222 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset);
223 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride);
224 if (!offset || (nr_virtfn > 1 && !stride))
228 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
229 bars |= (1 << (i + PCI_IOV_RESOURCES));
230 res = &dev->resource[i + PCI_IOV_RESOURCES];
234 if (nres != iov->nres) {
235 dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
239 iov->offset = offset;
240 iov->stride = stride;
242 bus = virtfn_bus(dev, nr_virtfn - 1);
243 if (bus > dev->bus->busn_res.end) {
244 dev_err(&dev->dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
245 nr_virtfn, bus, &dev->bus->busn_res);
249 if (pci_enable_resources(dev, bars)) {
250 dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
254 if (iov->link != dev->devfn) {
255 pdev = pci_get_slot(dev->bus, iov->link);
259 if (!pdev->is_physfn) {
264 rc = sysfs_create_link(&dev->dev.kobj,
265 &pdev->dev.kobj, "dep_link");
271 pci_iov_set_numvfs(dev, nr_virtfn);
272 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
273 pci_cfg_access_lock(dev);
274 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
276 pci_cfg_access_unlock(dev);
278 iov->initial_VFs = initial;
279 if (nr_virtfn < initial)
282 for (i = 0; i < initial; i++) {
283 rc = virtfn_add(dev, i, 0);
288 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
289 iov->num_VFs = nr_virtfn;
294 for (j = 0; j < i; j++)
295 virtfn_remove(dev, j, 0);
297 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
298 pci_cfg_access_lock(dev);
299 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
300 pci_iov_set_numvfs(dev, 0);
302 pci_cfg_access_unlock(dev);
304 if (iov->link != dev->devfn)
305 sysfs_remove_link(&dev->dev.kobj, "dep_link");
310 static void sriov_disable(struct pci_dev *dev)
313 struct pci_sriov *iov = dev->sriov;
318 for (i = 0; i < iov->num_VFs; i++)
319 virtfn_remove(dev, i, 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);
325 pci_cfg_access_unlock(dev);
327 if (iov->link != dev->devfn)
328 sysfs_remove_link(&dev->dev.kobj, "dep_link");
331 pci_iov_set_numvfs(dev, 0);
334 static int sriov_init(struct pci_dev *dev, int pos)
340 u16 ctrl, total, offset, stride;
341 struct pci_sriov *iov;
342 struct resource *res;
343 struct pci_dev *pdev;
345 if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
346 pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
349 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
350 if (ctrl & PCI_SRIOV_CTRL_VFE) {
351 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
355 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
360 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
365 if (pci_ari_enabled(dev->bus))
366 ctrl |= PCI_SRIOV_CTRL_ARI;
369 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
370 pci_write_config_word(dev, pos + PCI_SRIOV_NUM_VF, 0);
371 pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset);
372 pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride);
373 if (!offset || (total > 1 && !stride))
376 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
377 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
378 pgsz &= ~((1 << i) - 1);
383 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
385 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
390 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
391 res = &dev->resource[i + PCI_IOV_RESOURCES];
392 bar64 = __pci_read_base(dev, pci_bar_unknown, res,
393 pos + PCI_SRIOV_BAR + i * 4);
396 if (resource_size(res) & (PAGE_SIZE - 1)) {
400 iov->barsz[i] = resource_size(res);
401 res->end = res->start + resource_size(res) * total - 1;
402 dev_info(&dev->dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
411 iov->total_VFs = total;
412 iov->offset = offset;
413 iov->stride = stride;
416 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
417 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
418 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
419 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
422 iov->dev = pci_dev_get(pdev);
426 mutex_init(&iov->lock);
434 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
435 res = &dev->resource[i + PCI_IOV_RESOURCES];
443 static void sriov_release(struct pci_dev *dev)
445 BUG_ON(dev->sriov->num_VFs);
447 if (dev != dev->sriov->dev)
448 pci_dev_put(dev->sriov->dev);
450 mutex_destroy(&dev->sriov->lock);
456 static void sriov_restore_state(struct pci_dev *dev)
460 struct pci_sriov *iov = dev->sriov;
462 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
463 if (ctrl & PCI_SRIOV_CTRL_VFE)
466 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
467 pci_update_resource(dev, i);
469 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
470 pci_iov_set_numvfs(dev, iov->num_VFs);
471 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
472 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
477 * pci_iov_init - initialize the IOV capability
478 * @dev: the PCI device
480 * Returns 0 on success, or negative on failure.
482 int pci_iov_init(struct pci_dev *dev)
486 if (!pci_is_pcie(dev))
489 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
491 return sriov_init(dev, pos);
497 * pci_iov_release - release resources used by the IOV capability
498 * @dev: the PCI device
500 void pci_iov_release(struct pci_dev *dev)
507 * pci_iov_resource_bar - get position of the SR-IOV BAR
508 * @dev: the PCI device
509 * @resno: the resource number
511 * Returns position of the BAR encapsulated in the SR-IOV capability.
513 int pci_iov_resource_bar(struct pci_dev *dev, int resno)
515 if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
518 BUG_ON(!dev->is_physfn);
520 return dev->sriov->pos + PCI_SRIOV_BAR +
521 4 * (resno - PCI_IOV_RESOURCES);
525 * pci_sriov_resource_alignment - get resource alignment for VF BAR
526 * @dev: the PCI device
527 * @resno: the resource number
529 * Returns the alignment of the VF BAR found in the SR-IOV capability.
530 * This is not the same as the resource size which is defined as
531 * the VF BAR size multiplied by the number of VFs. The alignment
532 * is just the VF BAR size.
534 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
536 return pci_iov_resource_size(dev, resno);
540 * pci_restore_iov_state - restore the state of the IOV capability
541 * @dev: the PCI device
543 void pci_restore_iov_state(struct pci_dev *dev)
546 sriov_restore_state(dev);
550 * pci_iov_bus_range - find bus range used by Virtual Function
553 * Returns max number of buses (exclude current one) used by Virtual
556 int pci_iov_bus_range(struct pci_bus *bus)
562 list_for_each_entry(dev, &bus->devices, bus_list) {
565 busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
570 return max ? max - bus->number : 0;
574 * pci_enable_sriov - enable the SR-IOV capability
575 * @dev: the PCI device
576 * @nr_virtfn: number of virtual functions to enable
578 * Returns 0 on success, or negative on failure.
580 int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
587 return sriov_enable(dev, nr_virtfn);
589 EXPORT_SYMBOL_GPL(pci_enable_sriov);
592 * pci_disable_sriov - disable the SR-IOV capability
593 * @dev: the PCI device
595 void pci_disable_sriov(struct pci_dev *dev)
604 EXPORT_SYMBOL_GPL(pci_disable_sriov);
607 * pci_num_vf - return number of VFs associated with a PF device_release_driver
608 * @dev: the PCI device
610 * Returns number of VFs, or 0 if SR-IOV is not enabled.
612 int pci_num_vf(struct pci_dev *dev)
617 return dev->sriov->num_VFs;
619 EXPORT_SYMBOL_GPL(pci_num_vf);
622 * pci_vfs_assigned - returns number of VFs are assigned to a guest
623 * @dev: the PCI device
625 * Returns number of VFs belonging to this device that are assigned to a guest.
626 * If device is not a physical function returns 0.
628 int pci_vfs_assigned(struct pci_dev *dev)
630 struct pci_dev *vfdev;
631 unsigned int vfs_assigned = 0;
632 unsigned short dev_id;
634 /* only search if we are a PF */
639 * determine the device ID for the VFs, the vendor ID will be the
640 * same as the PF so there is no need to check for that one
642 pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id);
644 /* loop through all the VFs to see if we own any that are assigned */
645 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
648 * It is considered assigned if it is a virtual function with
649 * our dev as the physical function and the assigned bit is set
651 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
652 pci_is_dev_assigned(vfdev))
655 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
660 EXPORT_SYMBOL_GPL(pci_vfs_assigned);
663 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
664 * @dev: the PCI PF device
665 * @numvfs: number that should be used for TotalVFs supported
667 * Should be called from PF driver's probe routine with
668 * device's mutex held.
670 * Returns 0 if PF is an SRIOV-capable device and
671 * value of numvfs valid. If not a PF return -ENOSYS;
672 * if numvfs is invalid return -EINVAL;
673 * if VFs already enabled, return -EBUSY.
675 int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
679 if (numvfs > dev->sriov->total_VFs)
682 /* Shouldn't change if VFs already enabled */
683 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
686 dev->sriov->driver_max_VFs = numvfs;
690 EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
693 * pci_sriov_get_totalvfs -- get total VFs supported on this device
694 * @dev: the PCI PF device
696 * For a PCIe device with SRIOV support, return the PCIe
697 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
698 * if the driver reduced it. Otherwise 0.
700 int pci_sriov_get_totalvfs(struct pci_dev *dev)
705 if (dev->sriov->driver_max_VFs)
706 return dev->sriov->driver_max_VFs;
708 return dev->sriov->total_VFs;
710 EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);