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)
37 struct pci_bus *child;
39 if (bus->number == busnr)
42 child = pci_find_bus(pci_domain_nr(bus), busnr);
46 child = pci_add_new_bus(bus, NULL, busnr);
50 pci_bus_insert_busn_res(child, busnr, busnr);
56 static void virtfn_remove_bus(struct pci_bus *bus, int busnr)
58 struct pci_bus *child;
60 if (bus->number == busnr)
63 child = pci_find_bus(pci_domain_nr(bus), busnr);
66 if (list_empty(&child->devices))
67 pci_remove_bus(child);
70 static int virtfn_add(struct pci_dev *dev, int id, int reset)
75 char buf[VIRTFN_ID_LEN];
76 struct pci_dev *virtfn;
78 struct pci_sriov *iov = dev->sriov;
80 virtfn = alloc_pci_dev();
84 mutex_lock(&iov->dev->sriov->lock);
85 virtfn->bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
88 mutex_unlock(&iov->dev->sriov->lock);
91 virtfn->devfn = virtfn_devfn(dev, id);
92 virtfn->vendor = dev->vendor;
93 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device);
94 pci_setup_device(virtfn);
95 virtfn->dev.parent = dev->dev.parent;
97 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
98 res = dev->resource + PCI_IOV_RESOURCES + i;
101 virtfn->resource[i].name = pci_name(virtfn);
102 virtfn->resource[i].flags = res->flags;
103 size = resource_size(res);
104 do_div(size, iov->total_VFs);
105 virtfn->resource[i].start = res->start + size * id;
106 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
107 rc = request_resource(res, &virtfn->resource[i]);
112 __pci_reset_function(virtfn);
114 pci_device_add(virtfn, virtfn->bus);
115 mutex_unlock(&iov->dev->sriov->lock);
117 virtfn->physfn = pci_dev_get(dev);
118 virtfn->is_virtfn = 1;
120 rc = pci_bus_add_device(virtfn);
121 sprintf(buf, "virtfn%u", id);
122 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
125 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
129 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
134 sysfs_remove_link(&dev->dev.kobj, buf);
137 mutex_lock(&iov->dev->sriov->lock);
138 pci_stop_and_remove_bus_device(virtfn);
139 virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
140 mutex_unlock(&iov->dev->sriov->lock);
145 static void virtfn_remove(struct pci_dev *dev, int id, int reset)
147 char buf[VIRTFN_ID_LEN];
149 struct pci_dev *virtfn;
150 struct pci_sriov *iov = dev->sriov;
152 bus = pci_find_bus(pci_domain_nr(dev->bus), virtfn_bus(dev, id));
156 virtfn = pci_get_slot(bus, virtfn_devfn(dev, id));
163 device_release_driver(&virtfn->dev);
164 __pci_reset_function(virtfn);
167 sprintf(buf, "virtfn%u", id);
168 sysfs_remove_link(&dev->dev.kobj, buf);
170 * pci_stop_dev() could have been called for this virtfn already,
171 * so the directory for the virtfn may have been removed before.
172 * Double check to avoid spurious sysfs warnings.
174 if (virtfn->dev.kobj.sd)
175 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
177 mutex_lock(&iov->dev->sriov->lock);
178 pci_stop_and_remove_bus_device(virtfn);
179 virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
180 mutex_unlock(&iov->dev->sriov->lock);
185 static int sriov_migration(struct pci_dev *dev)
188 struct pci_sriov *iov = dev->sriov;
193 if (!(iov->cap & PCI_SRIOV_CAP_VFM))
196 pci_read_config_word(dev, iov->pos + PCI_SRIOV_STATUS, &status);
197 if (!(status & PCI_SRIOV_STATUS_VFM))
200 schedule_work(&iov->mtask);
205 static void sriov_migration_task(struct work_struct *work)
210 struct pci_sriov *iov = container_of(work, struct pci_sriov, mtask);
212 for (i = iov->initial_VFs; i < iov->num_VFs; i++) {
213 state = readb(iov->mstate + i);
214 if (state == PCI_SRIOV_VFM_MI) {
215 writeb(PCI_SRIOV_VFM_AV, iov->mstate + i);
216 state = readb(iov->mstate + i);
217 if (state == PCI_SRIOV_VFM_AV)
218 virtfn_add(iov->self, i, 1);
219 } else if (state == PCI_SRIOV_VFM_MO) {
220 virtfn_remove(iov->self, i, 1);
221 writeb(PCI_SRIOV_VFM_UA, iov->mstate + i);
222 state = readb(iov->mstate + i);
223 if (state == PCI_SRIOV_VFM_AV)
224 virtfn_add(iov->self, i, 0);
228 pci_read_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, &status);
229 status &= ~PCI_SRIOV_STATUS_VFM;
230 pci_write_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, status);
233 static int sriov_enable_migration(struct pci_dev *dev, int nr_virtfn)
238 struct pci_sriov *iov = dev->sriov;
240 if (nr_virtfn <= iov->initial_VFs)
243 pci_read_config_dword(dev, iov->pos + PCI_SRIOV_VFM, &table);
244 bir = PCI_SRIOV_VFM_BIR(table);
245 if (bir > PCI_STD_RESOURCE_END)
248 table = PCI_SRIOV_VFM_OFFSET(table);
249 if (table + nr_virtfn > pci_resource_len(dev, bir))
252 pa = pci_resource_start(dev, bir) + table;
253 iov->mstate = ioremap(pa, nr_virtfn);
257 INIT_WORK(&iov->mtask, sriov_migration_task);
259 iov->ctrl |= PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR;
260 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
265 static void sriov_disable_migration(struct pci_dev *dev)
267 struct pci_sriov *iov = dev->sriov;
269 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR);
270 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
272 cancel_work_sync(&iov->mtask);
273 iounmap(iov->mstate);
276 static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
281 u16 offset, stride, initial;
282 struct resource *res;
283 struct pci_dev *pdev;
284 struct pci_sriov *iov = dev->sriov;
293 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
294 if (initial > iov->total_VFs ||
295 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
298 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
299 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
302 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
303 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset);
304 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride);
305 if (!offset || (nr_virtfn > 1 && !stride))
309 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
310 bars |= (1 << (i + PCI_IOV_RESOURCES));
311 res = dev->resource + PCI_IOV_RESOURCES + i;
315 if (nres != iov->nres) {
316 dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
320 iov->offset = offset;
321 iov->stride = stride;
323 if (virtfn_bus(dev, nr_virtfn - 1) > dev->bus->busn_res.end) {
324 dev_err(&dev->dev, "SR-IOV: bus number out of range\n");
328 if (pci_enable_resources(dev, bars)) {
329 dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
333 if (iov->link != dev->devfn) {
334 pdev = pci_get_slot(dev->bus, iov->link);
340 if (!pdev->is_physfn)
343 rc = sysfs_create_link(&dev->dev.kobj,
344 &pdev->dev.kobj, "dep_link");
349 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
350 pci_cfg_access_lock(dev);
351 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
353 pci_cfg_access_unlock(dev);
355 iov->initial_VFs = initial;
356 if (nr_virtfn < initial)
359 for (i = 0; i < initial; i++) {
360 rc = virtfn_add(dev, i, 0);
365 if (iov->cap & PCI_SRIOV_CAP_VFM) {
366 rc = sriov_enable_migration(dev, nr_virtfn);
371 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
372 iov->num_VFs = nr_virtfn;
377 for (j = 0; j < i; j++)
378 virtfn_remove(dev, j, 0);
380 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
381 pci_cfg_access_lock(dev);
382 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
384 pci_cfg_access_unlock(dev);
386 if (iov->link != dev->devfn)
387 sysfs_remove_link(&dev->dev.kobj, "dep_link");
392 static void sriov_disable(struct pci_dev *dev)
395 struct pci_sriov *iov = dev->sriov;
400 if (iov->cap & PCI_SRIOV_CAP_VFM)
401 sriov_disable_migration(dev);
403 for (i = 0; i < iov->num_VFs; i++)
404 virtfn_remove(dev, i, 0);
406 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
407 pci_cfg_access_lock(dev);
408 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
410 pci_cfg_access_unlock(dev);
412 if (iov->link != dev->devfn)
413 sysfs_remove_link(&dev->dev.kobj, "dep_link");
418 static int sriov_init(struct pci_dev *dev, int pos)
424 u16 ctrl, total, offset, stride;
425 struct pci_sriov *iov;
426 struct resource *res;
427 struct pci_dev *pdev;
429 if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
430 pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
433 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
434 if (ctrl & PCI_SRIOV_CTRL_VFE) {
435 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
439 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
444 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
449 if (pci_ari_enabled(dev->bus))
450 ctrl |= PCI_SRIOV_CTRL_ARI;
453 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
454 pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset);
455 pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride);
456 if (!offset || (total > 1 && !stride))
459 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
460 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
461 pgsz &= ~((1 << i) - 1);
466 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
469 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
470 res = dev->resource + PCI_IOV_RESOURCES + i;
471 i += __pci_read_base(dev, pci_bar_unknown, res,
472 pos + PCI_SRIOV_BAR + i * 4);
475 if (resource_size(res) & (PAGE_SIZE - 1)) {
479 res->end = res->start + resource_size(res) * total - 1;
483 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
492 iov->total_VFs = total;
493 iov->offset = offset;
494 iov->stride = stride;
497 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
498 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
499 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
500 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
503 iov->dev = pci_dev_get(pdev);
507 mutex_init(&iov->lock);
515 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
516 res = dev->resource + PCI_IOV_RESOURCES + i;
523 static void sriov_release(struct pci_dev *dev)
525 BUG_ON(dev->sriov->num_VFs);
527 if (dev != dev->sriov->dev)
528 pci_dev_put(dev->sriov->dev);
530 mutex_destroy(&dev->sriov->lock);
536 static void sriov_restore_state(struct pci_dev *dev)
540 struct pci_sriov *iov = dev->sriov;
542 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
543 if (ctrl & PCI_SRIOV_CTRL_VFE)
546 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
547 pci_update_resource(dev, i);
549 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
550 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->num_VFs);
551 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
552 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
557 * pci_iov_init - initialize the IOV capability
558 * @dev: the PCI device
560 * Returns 0 on success, or negative on failure.
562 int pci_iov_init(struct pci_dev *dev)
566 if (!pci_is_pcie(dev))
569 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
571 return sriov_init(dev, pos);
577 * pci_iov_release - release resources used by the IOV capability
578 * @dev: the PCI device
580 void pci_iov_release(struct pci_dev *dev)
587 * pci_iov_resource_bar - get position of the SR-IOV BAR
588 * @dev: the PCI device
589 * @resno: the resource number
590 * @type: the BAR type to be filled in
592 * Returns position of the BAR encapsulated in the SR-IOV capability.
594 int pci_iov_resource_bar(struct pci_dev *dev, int resno,
595 enum pci_bar_type *type)
597 if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
600 BUG_ON(!dev->is_physfn);
602 *type = pci_bar_unknown;
604 return dev->sriov->pos + PCI_SRIOV_BAR +
605 4 * (resno - PCI_IOV_RESOURCES);
609 * pci_sriov_resource_alignment - get resource alignment for VF BAR
610 * @dev: the PCI device
611 * @resno: the resource number
613 * Returns the alignment of the VF BAR found in the SR-IOV capability.
614 * This is not the same as the resource size which is defined as
615 * the VF BAR size multiplied by the number of VFs. The alignment
616 * is just the VF BAR size.
618 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
621 enum pci_bar_type type;
622 int reg = pci_iov_resource_bar(dev, resno, &type);
627 __pci_read_base(dev, type, &tmp, reg);
628 return resource_alignment(&tmp);
632 * pci_restore_iov_state - restore the state of the IOV capability
633 * @dev: the PCI device
635 void pci_restore_iov_state(struct pci_dev *dev)
638 sriov_restore_state(dev);
642 * pci_iov_bus_range - find bus range used by Virtual Function
645 * Returns max number of buses (exclude current one) used by Virtual
648 int pci_iov_bus_range(struct pci_bus *bus)
654 list_for_each_entry(dev, &bus->devices, bus_list) {
657 busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
662 return max ? max - bus->number : 0;
666 * pci_enable_sriov - enable the SR-IOV capability
667 * @dev: the PCI device
668 * @nr_virtfn: number of virtual functions to enable
670 * Returns 0 on success, or negative on failure.
672 int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
679 return sriov_enable(dev, nr_virtfn);
681 EXPORT_SYMBOL_GPL(pci_enable_sriov);
684 * pci_disable_sriov - disable the SR-IOV capability
685 * @dev: the PCI device
687 void pci_disable_sriov(struct pci_dev *dev)
696 EXPORT_SYMBOL_GPL(pci_disable_sriov);
699 * pci_sriov_migration - notify SR-IOV core of Virtual Function Migration
700 * @dev: the PCI device
702 * Returns IRQ_HANDLED if the IRQ is handled, or IRQ_NONE if not.
704 * Physical Function driver is responsible to register IRQ handler using
705 * VF Migration Interrupt Message Number, and call this function when the
706 * interrupt is generated by the hardware.
708 irqreturn_t pci_sriov_migration(struct pci_dev *dev)
713 return sriov_migration(dev) ? IRQ_HANDLED : IRQ_NONE;
715 EXPORT_SYMBOL_GPL(pci_sriov_migration);
718 * pci_num_vf - return number of VFs associated with a PF device_release_driver
719 * @dev: the PCI device
721 * Returns number of VFs, or 0 if SR-IOV is not enabled.
723 int pci_num_vf(struct pci_dev *dev)
728 return dev->sriov->num_VFs;
730 EXPORT_SYMBOL_GPL(pci_num_vf);
733 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
734 * @dev: the PCI PF device
735 * numvfs: number that should be used for TotalVFs supported
737 * Should be called from PF driver's probe routine with
738 * device's mutex held.
740 * Returns 0 if PF is an SRIOV-capable device and
741 * value of numvfs valid. If not a PF with VFS, return -EINVAL;
742 * if VFs already enabled, return -EBUSY.
744 int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
746 if (!dev->is_physfn || (numvfs > dev->sriov->total_VFs))
749 /* Shouldn't change if VFs already enabled */
750 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
753 dev->sriov->driver_max_VFs = numvfs;
757 EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
760 * pci_sriov_get_totalvfs -- get total VFs supported on this devic3
761 * @dev: the PCI PF device
763 * For a PCIe device with SRIOV support, return the PCIe
764 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
765 * if the driver reduced it. Otherwise, -EINVAL.
767 int pci_sriov_get_totalvfs(struct pci_dev *dev)
772 if (dev->sriov->driver_max_VFs)
773 return dev->sriov->driver_max_VFs;
775 return dev->sriov->total_VFs;
777 EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);