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;
34 static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
36 struct pci_bus *child;
38 if (bus->number == busnr)
41 child = pci_find_bus(pci_domain_nr(bus), busnr);
45 child = pci_add_new_bus(bus, NULL, busnr);
49 pci_bus_insert_busn_res(child, busnr, busnr);
55 static void virtfn_remove_bus(struct pci_bus *bus, int busnr)
57 struct pci_bus *child;
59 if (bus->number == busnr)
62 child = pci_find_bus(pci_domain_nr(bus), busnr);
65 if (list_empty(&child->devices))
66 pci_remove_bus(child);
69 static int virtfn_add(struct pci_dev *dev, int id, int reset)
74 char buf[VIRTFN_ID_LEN];
75 struct pci_dev *virtfn;
77 struct pci_sriov *iov = dev->sriov;
79 virtfn = alloc_pci_dev();
83 mutex_lock(&iov->dev->sriov->lock);
84 virtfn->bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
87 mutex_unlock(&iov->dev->sriov->lock);
90 virtfn->devfn = virtfn_devfn(dev, id);
91 virtfn->vendor = dev->vendor;
92 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device);
93 pci_setup_device(virtfn);
94 virtfn->dev.parent = dev->dev.parent;
95 virtfn->physfn = pci_dev_get(dev);
96 virtfn->is_virtfn = 1;
98 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
99 res = dev->resource + PCI_IOV_RESOURCES + i;
102 virtfn->resource[i].name = pci_name(virtfn);
103 virtfn->resource[i].flags = res->flags;
104 size = resource_size(res);
105 do_div(size, iov->total_VFs);
106 virtfn->resource[i].start = res->start + size * id;
107 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
108 rc = request_resource(res, &virtfn->resource[i]);
113 __pci_reset_function(virtfn);
115 pci_device_add(virtfn, virtfn->bus);
116 mutex_unlock(&iov->dev->sriov->lock);
118 rc = pci_bus_add_device(virtfn);
119 sprintf(buf, "virtfn%u", id);
120 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
123 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
127 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
132 sysfs_remove_link(&dev->dev.kobj, buf);
135 mutex_lock(&iov->dev->sriov->lock);
136 pci_stop_and_remove_bus_device(virtfn);
137 virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
138 mutex_unlock(&iov->dev->sriov->lock);
143 static void virtfn_remove(struct pci_dev *dev, int id, int reset)
145 char buf[VIRTFN_ID_LEN];
147 struct pci_dev *virtfn;
148 struct pci_sriov *iov = dev->sriov;
150 bus = pci_find_bus(pci_domain_nr(dev->bus), virtfn_bus(dev, id));
154 virtfn = pci_get_slot(bus, virtfn_devfn(dev, id));
161 device_release_driver(&virtfn->dev);
162 __pci_reset_function(virtfn);
165 sprintf(buf, "virtfn%u", id);
166 sysfs_remove_link(&dev->dev.kobj, buf);
168 * pci_stop_dev() could have been called for this virtfn already,
169 * so the directory for the virtfn may have been removed before.
170 * Double check to avoid spurious sysfs warnings.
172 if (virtfn->dev.kobj.sd)
173 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
175 mutex_lock(&iov->dev->sriov->lock);
176 pci_stop_and_remove_bus_device(virtfn);
177 virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
178 mutex_unlock(&iov->dev->sriov->lock);
183 static int sriov_migration(struct pci_dev *dev)
186 struct pci_sriov *iov = dev->sriov;
191 if (!(iov->cap & PCI_SRIOV_CAP_VFM))
194 pci_read_config_word(dev, iov->pos + PCI_SRIOV_STATUS, &status);
195 if (!(status & PCI_SRIOV_STATUS_VFM))
198 schedule_work(&iov->mtask);
203 static void sriov_migration_task(struct work_struct *work)
208 struct pci_sriov *iov = container_of(work, struct pci_sriov, mtask);
210 for (i = iov->initial_VFs; i < iov->num_VFs; i++) {
211 state = readb(iov->mstate + i);
212 if (state == PCI_SRIOV_VFM_MI) {
213 writeb(PCI_SRIOV_VFM_AV, iov->mstate + i);
214 state = readb(iov->mstate + i);
215 if (state == PCI_SRIOV_VFM_AV)
216 virtfn_add(iov->self, i, 1);
217 } else if (state == PCI_SRIOV_VFM_MO) {
218 virtfn_remove(iov->self, i, 1);
219 writeb(PCI_SRIOV_VFM_UA, iov->mstate + i);
220 state = readb(iov->mstate + i);
221 if (state == PCI_SRIOV_VFM_AV)
222 virtfn_add(iov->self, i, 0);
226 pci_read_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, &status);
227 status &= ~PCI_SRIOV_STATUS_VFM;
228 pci_write_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, status);
231 static int sriov_enable_migration(struct pci_dev *dev, int nr_virtfn)
236 struct pci_sriov *iov = dev->sriov;
238 if (nr_virtfn <= iov->initial_VFs)
241 pci_read_config_dword(dev, iov->pos + PCI_SRIOV_VFM, &table);
242 bir = PCI_SRIOV_VFM_BIR(table);
243 if (bir > PCI_STD_RESOURCE_END)
246 table = PCI_SRIOV_VFM_OFFSET(table);
247 if (table + nr_virtfn > pci_resource_len(dev, bir))
250 pa = pci_resource_start(dev, bir) + table;
251 iov->mstate = ioremap(pa, nr_virtfn);
255 INIT_WORK(&iov->mtask, sriov_migration_task);
257 iov->ctrl |= PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR;
258 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
263 static void sriov_disable_migration(struct pci_dev *dev)
265 struct pci_sriov *iov = dev->sriov;
267 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR);
268 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
270 cancel_work_sync(&iov->mtask);
271 iounmap(iov->mstate);
274 static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
279 u16 offset, stride, initial;
280 struct resource *res;
281 struct pci_dev *pdev;
282 struct pci_sriov *iov = dev->sriov;
291 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
292 if (initial > iov->total_VFs ||
293 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
296 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
297 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
300 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
301 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset);
302 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride);
303 if (!offset || (nr_virtfn > 1 && !stride))
307 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
308 bars |= (1 << (i + PCI_IOV_RESOURCES));
309 res = dev->resource + PCI_IOV_RESOURCES + i;
313 if (nres != iov->nres) {
314 dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
318 iov->offset = offset;
319 iov->stride = stride;
321 if (virtfn_bus(dev, nr_virtfn - 1) > dev->bus->busn_res.end) {
322 dev_err(&dev->dev, "SR-IOV: bus number out of range\n");
326 if (pci_enable_resources(dev, bars)) {
327 dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
331 if (iov->link != dev->devfn) {
332 pdev = pci_get_slot(dev->bus, iov->link);
338 if (!pdev->is_physfn)
341 rc = sysfs_create_link(&dev->dev.kobj,
342 &pdev->dev.kobj, "dep_link");
347 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
348 pci_cfg_access_lock(dev);
349 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
351 pci_cfg_access_unlock(dev);
353 iov->initial_VFs = initial;
354 if (nr_virtfn < initial)
357 for (i = 0; i < initial; i++) {
358 rc = virtfn_add(dev, i, 0);
363 if (iov->cap & PCI_SRIOV_CAP_VFM) {
364 rc = sriov_enable_migration(dev, nr_virtfn);
369 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
370 iov->num_VFs = nr_virtfn;
375 for (j = 0; j < i; j++)
376 virtfn_remove(dev, j, 0);
378 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
379 pci_cfg_access_lock(dev);
380 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
382 pci_cfg_access_unlock(dev);
384 if (iov->link != dev->devfn)
385 sysfs_remove_link(&dev->dev.kobj, "dep_link");
390 static void sriov_disable(struct pci_dev *dev)
393 struct pci_sriov *iov = dev->sriov;
398 if (iov->cap & PCI_SRIOV_CAP_VFM)
399 sriov_disable_migration(dev);
401 for (i = 0; i < iov->num_VFs; i++)
402 virtfn_remove(dev, i, 0);
404 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
405 pci_cfg_access_lock(dev);
406 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
408 pci_cfg_access_unlock(dev);
410 if (iov->link != dev->devfn)
411 sysfs_remove_link(&dev->dev.kobj, "dep_link");
416 static int sriov_init(struct pci_dev *dev, int pos)
422 u16 ctrl, total, offset, stride;
423 struct pci_sriov *iov;
424 struct resource *res;
425 struct pci_dev *pdev;
427 if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
428 pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
431 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
432 if (ctrl & PCI_SRIOV_CTRL_VFE) {
433 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
437 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
442 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
447 if (pci_ari_enabled(dev->bus))
448 ctrl |= PCI_SRIOV_CTRL_ARI;
451 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
452 pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset);
453 pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride);
454 if (!offset || (total > 1 && !stride))
457 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
458 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
459 pgsz &= ~((1 << i) - 1);
464 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
467 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
468 res = dev->resource + PCI_IOV_RESOURCES + i;
469 i += __pci_read_base(dev, pci_bar_unknown, res,
470 pos + PCI_SRIOV_BAR + i * 4);
473 if (resource_size(res) & (PAGE_SIZE - 1)) {
477 res->end = res->start + resource_size(res) * total - 1;
481 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
490 iov->total_VFs = total;
491 iov->offset = offset;
492 iov->stride = stride;
495 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
496 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
497 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
498 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
501 iov->dev = pci_dev_get(pdev);
505 mutex_init(&iov->lock);
513 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
514 res = dev->resource + PCI_IOV_RESOURCES + i;
521 static void sriov_release(struct pci_dev *dev)
523 BUG_ON(dev->sriov->num_VFs);
525 if (dev != dev->sriov->dev)
526 pci_dev_put(dev->sriov->dev);
528 mutex_destroy(&dev->sriov->lock);
534 static void sriov_restore_state(struct pci_dev *dev)
538 struct pci_sriov *iov = dev->sriov;
540 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
541 if (ctrl & PCI_SRIOV_CTRL_VFE)
544 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
545 pci_update_resource(dev, i);
547 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
548 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->num_VFs);
549 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
550 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
555 * pci_iov_init - initialize the IOV capability
556 * @dev: the PCI device
558 * Returns 0 on success, or negative on failure.
560 int pci_iov_init(struct pci_dev *dev)
564 if (!pci_is_pcie(dev))
567 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
569 return sriov_init(dev, pos);
575 * pci_iov_release - release resources used by the IOV capability
576 * @dev: the PCI device
578 void pci_iov_release(struct pci_dev *dev)
585 * pci_iov_resource_bar - get position of the SR-IOV BAR
586 * @dev: the PCI device
587 * @resno: the resource number
588 * @type: the BAR type to be filled in
590 * Returns position of the BAR encapsulated in the SR-IOV capability.
592 int pci_iov_resource_bar(struct pci_dev *dev, int resno,
593 enum pci_bar_type *type)
595 if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
598 BUG_ON(!dev->is_physfn);
600 *type = pci_bar_unknown;
602 return dev->sriov->pos + PCI_SRIOV_BAR +
603 4 * (resno - PCI_IOV_RESOURCES);
607 * pci_sriov_resource_alignment - get resource alignment for VF BAR
608 * @dev: the PCI device
609 * @resno: the resource number
611 * Returns the alignment of the VF BAR found in the SR-IOV capability.
612 * This is not the same as the resource size which is defined as
613 * the VF BAR size multiplied by the number of VFs. The alignment
614 * is just the VF BAR size.
616 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
619 enum pci_bar_type type;
620 int reg = pci_iov_resource_bar(dev, resno, &type);
625 __pci_read_base(dev, type, &tmp, reg);
626 return resource_alignment(&tmp);
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)
636 sriov_restore_state(dev);
640 * pci_iov_bus_range - find bus range used by Virtual Function
643 * Returns max number of buses (exclude current one) used by Virtual
646 int pci_iov_bus_range(struct pci_bus *bus)
652 list_for_each_entry(dev, &bus->devices, bus_list) {
655 busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
660 return max ? max - bus->number : 0;
664 * pci_enable_sriov - enable the SR-IOV capability
665 * @dev: the PCI device
666 * @nr_virtfn: number of virtual functions to enable
668 * Returns 0 on success, or negative on failure.
670 int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
677 return sriov_enable(dev, nr_virtfn);
679 EXPORT_SYMBOL_GPL(pci_enable_sriov);
682 * pci_disable_sriov - disable the SR-IOV capability
683 * @dev: the PCI device
685 void pci_disable_sriov(struct pci_dev *dev)
694 EXPORT_SYMBOL_GPL(pci_disable_sriov);
697 * pci_sriov_migration - notify SR-IOV core of Virtual Function Migration
698 * @dev: the PCI device
700 * Returns IRQ_HANDLED if the IRQ is handled, or IRQ_NONE if not.
702 * Physical Function driver is responsible to register IRQ handler using
703 * VF Migration Interrupt Message Number, and call this function when the
704 * interrupt is generated by the hardware.
706 irqreturn_t pci_sriov_migration(struct pci_dev *dev)
711 return sriov_migration(dev) ? IRQ_HANDLED : IRQ_NONE;
713 EXPORT_SYMBOL_GPL(pci_sriov_migration);
716 * pci_num_vf - return number of VFs associated with a PF device_release_driver
717 * @dev: the PCI device
719 * Returns number of VFs, or 0 if SR-IOV is not enabled.
721 int pci_num_vf(struct pci_dev *dev)
726 return dev->sriov->num_VFs;
728 EXPORT_SYMBOL_GPL(pci_num_vf);
731 * pci_vfs_assigned - returns number of VFs are assigned to a guest
732 * @dev: the PCI device
734 * Returns number of VFs belonging to this device that are assigned to a guest.
735 * If device is not a physical function returns -ENODEV.
737 int pci_vfs_assigned(struct pci_dev *dev)
739 struct pci_dev *vfdev;
740 unsigned int vfs_assigned = 0;
741 unsigned short dev_id;
743 /* only search if we are a PF */
748 * determine the device ID for the VFs, the vendor ID will be the
749 * same as the PF so there is no need to check for that one
751 pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id);
753 /* loop through all the VFs to see if we own any that are assigned */
754 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
757 * It is considered assigned if it is a virtual function with
758 * our dev as the physical function and the assigned bit is set
760 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
761 (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED))
764 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
769 EXPORT_SYMBOL_GPL(pci_vfs_assigned);
772 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
773 * @dev: the PCI PF device
774 * @numvfs: number that should be used for TotalVFs supported
776 * Should be called from PF driver's probe routine with
777 * device's mutex held.
779 * Returns 0 if PF is an SRIOV-capable device and
780 * value of numvfs valid. If not a PF with VFS, return -EINVAL;
781 * if VFs already enabled, return -EBUSY.
783 int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
785 if (!dev->is_physfn || (numvfs > dev->sriov->total_VFs))
788 /* Shouldn't change if VFs already enabled */
789 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
792 dev->sriov->driver_max_VFs = numvfs;
796 EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
799 * pci_sriov_get_totalvfs -- get total VFs supported on this devic3
800 * @dev: the PCI PF device
802 * For a PCIe device with SRIOV support, return the PCIe
803 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
804 * if the driver reduced it. Otherwise, -EINVAL.
806 int pci_sriov_get_totalvfs(struct pci_dev *dev)
811 if (dev->sriov->driver_max_VFs)
812 return dev->sriov->driver_max_VFs;
814 return dev->sriov->total_VFs;
816 EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);