Lines Matching refs:dev

19 int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id)
21 if (!dev->is_physfn)
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)
31 return (dev->devfn + dev->sriov->offset +
32 dev->sriov->stride * vf_id) & 0xff;
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);
57 static int compute_max_vf_buses(struct pci_dev *dev)
59 struct pci_sriov *iov = dev->sriov;
63 pci_iov_set_numvfs(dev, nr_virtfn);
69 busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
75 pci_iov_set_numvfs(dev, 0);
105 resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
107 if (!dev->is_physfn)
110 return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
136 int pci_iov_sysfs_link(struct pci_dev *dev,
143 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
146 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
150 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
155 sysfs_remove_link(&dev->dev.kobj, buf);
160 int pci_iov_add_virtfn(struct pci_dev *dev, int id)
167 struct pci_sriov *iov = dev->sriov;
170 bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
178 virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
179 virtfn->vendor = dev->vendor;
182 virtfn->physfn = pci_dev_get(dev);
192 virtfn->dev.parent = dev->dev.parent;
196 res = &dev->resource[i + PCI_IOV_RESOURCES];
201 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
209 rc = pci_iov_sysfs_link(dev, virtfn, id);
219 pci_dev_put(dev);
221 virtfn_remove_bus(dev->bus, bus);
227 void pci_iov_remove_virtfn(struct pci_dev *dev, int id)
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));
239 sysfs_remove_link(&dev->dev.kobj, buf);
245 if (virtfn->dev.kobj.sd)
246 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
249 virtfn_remove_bus(dev->bus, virtfn->bus);
253 pci_dev_put(dev);
256 static ssize_t sriov_totalvfs_show(struct device *dev,
260 struct pci_dev *pdev = to_pci_dev(dev);
265 static ssize_t sriov_numvfs_show(struct device *dev,
269 struct pci_dev *pdev = to_pci_dev(dev);
273 device_lock(&pdev->dev);
275 device_unlock(&pdev->dev);
287 static ssize_t sriov_numvfs_store(struct device *dev,
291 struct pci_dev *pdev = to_pci_dev(dev);
302 device_lock(&pdev->dev);
337 device_unlock(&pdev->dev);
345 static ssize_t sriov_offset_show(struct device *dev,
349 struct pci_dev *pdev = to_pci_dev(dev);
354 static ssize_t sriov_stride_show(struct device *dev,
358 struct pci_dev *pdev = to_pci_dev(dev);
363 static ssize_t sriov_vf_device_show(struct device *dev,
367 struct pci_dev *pdev = to_pci_dev(dev);
372 static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
376 struct pci_dev *pdev = to_pci_dev(dev);
381 static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
385 struct pci_dev *pdev = to_pci_dev(dev);
416 struct device *dev = kobj_to_dev(kobj);
418 if (!dev_is_pf(dev))
439 static int sriov_add_vfs(struct pci_dev *dev, u16 num_vfs)
444 if (dev->no_vf_scan)
448 rc = pci_iov_add_virtfn(dev, i);
455 pci_iov_remove_virtfn(dev, i);
460 static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
468 struct pci_sriov *iov = dev->sriov;
478 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
490 res = &dev->resource[i + PCI_IOV_RESOURCES];
495 pci_err(dev, "not enough MMIO resources for SR-IOV\n");
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);
506 if (pci_enable_resources(dev, bars)) {
507 pci_err(dev, "SR-IOV: IOV BARS not allocated\n");
511 if (iov->link != dev->devfn) {
512 pdev = pci_get_slot(dev->bus, iov->link);
521 rc = sysfs_create_link(&dev->dev.kobj,
522 &pdev->dev.kobj, "dep_link");
532 rc = pcibios_sriov_enable(dev, initial);
534 pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc);
538 pci_iov_set_numvfs(dev, nr_virtfn);
540 pci_cfg_access_lock(dev);
541 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
543 pci_cfg_access_unlock(dev);
545 rc = sriov_add_vfs(dev, initial);
549 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
556 pci_cfg_access_lock(dev);
557 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
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);
570 static void sriov_del_vfs(struct pci_dev *dev)
572 struct pci_sriov *iov = dev->sriov;
576 pci_iov_remove_virtfn(dev, i);
579 static void sriov_disable(struct pci_dev *dev)
581 struct pci_sriov *iov = dev->sriov;
586 sriov_del_vfs(dev);
588 pci_cfg_access_lock(dev);
589 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
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");
599 pci_iov_set_numvfs(dev, 0);
602 static int sriov_init(struct pci_dev *dev, int pos)
613 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
615 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
620 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
625 if (pci_ari_enabled(dev->bus))
629 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
631 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
635 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
642 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
650 res = &dev->resource[i + PCI_IOV_RESOURCES];
658 bar64 = __pci_read_base(dev, pci_bar_unknown, res,
668 pci_info(dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
679 pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
681 iov->self = dev;
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);
689 iov->dev = pci_dev_get(pdev);
691 iov->dev = dev;
693 dev->sriov = iov;
694 dev->is_physfn = 1;
695 rc = compute_max_vf_buses(dev);
702 dev->sriov = NULL;
703 dev->is_physfn = 0;
706 res = &dev->resource[i + PCI_IOV_RESOURCES];
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)
729 struct pci_sriov *iov = dev->sriov;
731 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
741 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
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);
755 * @dev: the PCI device
759 int pci_iov_init(struct pci_dev *dev)
763 if (!pci_is_pcie(dev))
766 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
768 return sriov_init(dev, pos);
775 * @dev: the PCI device
777 void pci_iov_release(struct pci_dev *dev)
779 if (dev->is_physfn)
780 sriov_release(dev);
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)
796 pci_warn(dev, "driver left SR-IOV enabled after remove\n");
801 * @dev: the PCI device
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;
824 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
826 dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
845 pcibios_resource_to_bus(dev->bus, &region, res);
850 pci_write_config_dword(dev, reg, new);
853 pci_write_config_dword(dev, reg + 4, new);
857 resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
860 return pci_iov_resource_size(dev, resno);
865 * @dev: the PCI device
873 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
875 return pcibios_iov_resource_alignment(dev, resno);
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);
890 * @dev: the PCI device
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;
909 struct pci_dev *dev;
911 list_for_each_entry(dev, &bus->devices, bus_list) {
912 if (!dev->is_physfn)
914 if (dev->sriov->max_VF_buses > max)
915 max = dev->sriov->max_VF_buses;
923 * @dev: the PCI device
928 int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
932 if (!dev->is_physfn)
935 return sriov_enable(dev, nr_virtfn);
941 * @dev: the PCI device
943 void pci_disable_sriov(struct pci_dev *dev)
947 if (!dev->is_physfn)
950 sriov_disable(dev);
956 * @dev: the PCI device
960 int pci_num_vf(struct pci_dev *dev)
962 if (!dev->is_physfn)
965 return dev->sriov->num_VFs;
971 * @dev: the PCI device
976 int pci_vfs_assigned(struct pci_dev *dev)
983 if (!dev->is_physfn)
990 dev_id = dev->sriov->vf_device;
993 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
997 * our dev as the physical function and the assigned bit is set
999 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
1003 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
1012 * @dev: the PCI PF device
1023 int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
1025 if (!dev->is_physfn)
1028 if (numvfs > dev->sriov->total_VFs)
1032 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
1035 dev->sriov->driver_max_VFs = numvfs;
1042 * @dev: the PCI PF device
1048 int pci_sriov_get_totalvfs(struct pci_dev *dev)
1050 if (!dev->is_physfn)
1053 return dev->sriov->driver_max_VFs;
1059 * @dev: the PCI device
1066 int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn)
1072 if (!dev->is_physfn)
1075 if (pci_vfs_assigned(dev)) {
1076 pci_warn(dev, "Cannot modify SR-IOV while VFs are assigned\n");
1081 sriov_disable(dev);
1085 rc = sriov_enable(dev, nr_virtfn);