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;
36 int pci_iov_vf_id(struct pci_dev *dev)
40 if (!dev->is_virtfn)
43 pf = pci_physfn(dev);
44 return (pci_dev_id(dev) - (pci_dev_id(pf) + pf->sriov->offset)) /
51 * @dev: VF pci_dev
65 void *pci_iov_get_pf_drvdata(struct pci_dev *dev, struct pci_driver *pf_driver)
69 if (!dev->is_virtfn)
71 pf_dev = dev->physfn;
84 static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
86 struct pci_sriov *iov = dev->sriov;
88 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
89 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
90 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
100 static int compute_max_vf_buses(struct pci_dev *dev)
102 struct pci_sriov *iov = dev->sriov;
106 pci_iov_set_numvfs(dev, nr_virtfn);
112 busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
118 pci_iov_set_numvfs(dev, 0);
148 resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
150 if (!dev->is_physfn)
153 return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
179 int pci_iov_sysfs_link(struct pci_dev *dev,
186 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
189 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
193 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
198 sysfs_remove_link(&dev->dev.kobj, buf);
204 static ssize_t sriov_vf_total_msix_show(struct device *dev,
208 struct pci_dev *pdev = to_pci_dev(dev);
211 device_lock(dev);
217 device_unlock(dev);
222 static ssize_t sriov_vf_msix_count_store(struct device *dev,
226 struct pci_dev *vf_dev = to_pci_dev(dev);
236 device_lock(&pdev->dev);
242 device_lock(&vf_dev->dev);
256 device_unlock(&vf_dev->dev);
258 device_unlock(&pdev->dev);
274 struct device *dev = kobj_to_dev(kobj);
275 struct pci_dev *pdev = to_pci_dev(dev);
288 int pci_iov_add_virtfn(struct pci_dev *dev, int id)
295 struct pci_sriov *iov = dev->sriov;
298 bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
306 virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
307 virtfn->vendor = dev->vendor;
310 virtfn->physfn = pci_dev_get(dev);
320 virtfn->dev.parent = dev->dev.parent;
324 res = &dev->resource[i + PCI_IOV_RESOURCES];
329 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
337 rc = pci_iov_sysfs_link(dev, virtfn, id);
347 pci_dev_put(dev);
349 virtfn_remove_bus(dev->bus, bus);
355 void pci_iov_remove_virtfn(struct pci_dev *dev, int id)
360 virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
361 pci_iov_virtfn_bus(dev, id),
362 pci_iov_virtfn_devfn(dev, id));
367 sysfs_remove_link(&dev->dev.kobj, buf);
373 if (virtfn->dev.kobj.sd)
374 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
377 virtfn_remove_bus(dev->bus, virtfn->bus);
381 pci_dev_put(dev);
384 static ssize_t sriov_totalvfs_show(struct device *dev,
388 struct pci_dev *pdev = to_pci_dev(dev);
393 static ssize_t sriov_numvfs_show(struct device *dev,
397 struct pci_dev *pdev = to_pci_dev(dev);
401 device_lock(&pdev->dev);
403 device_unlock(&pdev->dev);
415 static ssize_t sriov_numvfs_store(struct device *dev,
419 struct pci_dev *pdev = to_pci_dev(dev);
429 device_lock(&pdev->dev);
471 device_unlock(&pdev->dev);
479 static ssize_t sriov_offset_show(struct device *dev,
483 struct pci_dev *pdev = to_pci_dev(dev);
488 static ssize_t sriov_stride_show(struct device *dev,
492 struct pci_dev *pdev = to_pci_dev(dev);
497 static ssize_t sriov_vf_device_show(struct device *dev,
501 struct pci_dev *pdev = to_pci_dev(dev);
506 static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
510 struct pci_dev *pdev = to_pci_dev(dev);
515 static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
519 struct pci_dev *pdev = to_pci_dev(dev);
553 struct device *dev = kobj_to_dev(kobj);
555 if (!dev_is_pf(dev))
576 static int sriov_add_vfs(struct pci_dev *dev, u16 num_vfs)
581 if (dev->no_vf_scan)
585 rc = pci_iov_add_virtfn(dev, i);
592 pci_iov_remove_virtfn(dev, i);
597 static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
605 struct pci_sriov *iov = dev->sriov;
615 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
627 res = &dev->resource[i + PCI_IOV_RESOURCES];
632 pci_err(dev, "not enough MMIO resources for SR-IOV\n");
636 bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
637 if (bus > dev->bus->busn_res.end) {
638 pci_err(dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
639 nr_virtfn, bus, &dev->bus->busn_res);
643 if (pci_enable_resources(dev, bars)) {
644 pci_err(dev, "SR-IOV: IOV BARS not allocated\n");
648 if (iov->link != dev->devfn) {
649 pdev = pci_get_slot(dev->bus, iov->link);
658 rc = sysfs_create_link(&dev->dev.kobj,
659 &pdev->dev.kobj, "dep_link");
669 rc = pcibios_sriov_enable(dev, initial);
671 pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc);
675 pci_iov_set_numvfs(dev, nr_virtfn);
677 pci_cfg_access_lock(dev);
678 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
680 pci_cfg_access_unlock(dev);
682 rc = sriov_add_vfs(dev, initial);
686 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
693 pci_cfg_access_lock(dev);
694 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
696 pci_cfg_access_unlock(dev);
698 pcibios_sriov_disable(dev);
700 if (iov->link != dev->devfn)
701 sysfs_remove_link(&dev->dev.kobj, "dep_link");
703 pci_iov_set_numvfs(dev, 0);
707 static void sriov_del_vfs(struct pci_dev *dev)
709 struct pci_sriov *iov = dev->sriov;
713 pci_iov_remove_virtfn(dev, i);
716 static void sriov_disable(struct pci_dev *dev)
718 struct pci_sriov *iov = dev->sriov;
723 sriov_del_vfs(dev);
725 pci_cfg_access_lock(dev);
726 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
728 pci_cfg_access_unlock(dev);
730 pcibios_sriov_disable(dev);
732 if (iov->link != dev->devfn)
733 sysfs_remove_link(&dev->dev.kobj, "dep_link");
736 pci_iov_set_numvfs(dev, 0);
739 static int sriov_init(struct pci_dev *dev, int pos)
750 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
752 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
757 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
762 if (pci_ari_enabled(dev->bus))
766 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
768 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
772 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
779 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
787 res = &dev->resource[i + PCI_IOV_RESOURCES];
795 bar64 = __pci_read_base(dev, pci_bar_unknown, res,
805 pci_info(dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
816 pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
818 iov->self = dev;
820 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
821 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
822 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
823 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
826 iov->dev = pci_dev_get(pdev);
828 iov->dev = dev;
830 dev->sriov = iov;
831 dev->is_physfn = 1;
832 rc = compute_max_vf_buses(dev);
839 dev->sriov = NULL;
840 dev->is_physfn = 0;
843 res = &dev->resource[i + PCI_IOV_RESOURCES];
851 static void sriov_release(struct pci_dev *dev)
853 BUG_ON(dev->sriov->num_VFs);
855 if (dev != dev->sriov->dev)
856 pci_dev_put(dev->sriov->dev);
858 kfree(dev->sriov);
859 dev->sriov = NULL;
862 static void sriov_restore_state(struct pci_dev *dev)
866 struct pci_sriov *iov = dev->sriov;
868 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
878 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
881 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
883 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
884 pci_iov_set_numvfs(dev, iov->num_VFs);
885 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
892 * @dev: the PCI device
896 int pci_iov_init(struct pci_dev *dev)
900 if (!pci_is_pcie(dev))
903 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
905 return sriov_init(dev, pos);
912 * @dev: the PCI device
914 void pci_iov_release(struct pci_dev *dev)
916 if (dev->is_physfn)
917 sriov_release(dev);
922 * @dev: the PCI device
924 void pci_iov_remove(struct pci_dev *dev)
926 struct pci_sriov *iov = dev->sriov;
928 if (!dev->is_physfn)
933 pci_warn(dev, "driver left SR-IOV enabled after remove\n");
938 * @dev: the PCI device
943 void pci_iov_update_resource(struct pci_dev *dev, int resno)
945 struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
946 struct resource *res = dev->resource + resno;
961 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
963 dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
982 pcibios_resource_to_bus(dev->bus, ®ion, res);
987 pci_write_config_dword(dev, reg, new);
990 pci_write_config_dword(dev, reg + 4, new);
994 resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
997 return pci_iov_resource_size(dev, resno);
1002 * @dev: the PCI device
1010 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
1012 return pcibios_iov_resource_alignment(dev, resno);
1017 * @dev: the PCI device
1019 void pci_restore_iov_state(struct pci_dev *dev)
1021 if (dev->is_physfn)
1022 sriov_restore_state(dev);
1027 * @dev: the PCI device
1030 void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe)
1032 if (dev->is_physfn)
1033 dev->sriov->drivers_autoprobe = auto_probe;
1046 struct pci_dev *dev;
1048 list_for_each_entry(dev, &bus->devices, bus_list) {
1049 if (!dev->is_physfn)
1051 if (dev->sriov->max_VF_buses > max)
1052 max = dev->sriov->max_VF_buses;
1060 * @dev: the PCI device
1065 int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
1069 if (!dev->is_physfn)
1072 return sriov_enable(dev, nr_virtfn);
1078 * @dev: the PCI device
1080 void pci_disable_sriov(struct pci_dev *dev)
1084 if (!dev->is_physfn)
1087 sriov_disable(dev);
1093 * @dev: the PCI device
1097 int pci_num_vf(struct pci_dev *dev)
1099 if (!dev->is_physfn)
1102 return dev->sriov->num_VFs;
1108 * @dev: the PCI device
1113 int pci_vfs_assigned(struct pci_dev *dev)
1120 if (!dev->is_physfn)
1127 dev_id = dev->sriov->vf_device;
1130 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
1134 * our dev as the physical function and the assigned bit is set
1136 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
1140 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
1149 * @dev: the PCI PF device
1160 int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
1162 if (!dev->is_physfn)
1165 if (numvfs > dev->sriov->total_VFs)
1169 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
1172 dev->sriov->driver_max_VFs = numvfs;
1179 * @dev: the PCI PF device
1185 int pci_sriov_get_totalvfs(struct pci_dev *dev)
1187 if (!dev->is_physfn)
1190 return dev->sriov->driver_max_VFs;
1196 * @dev: the PCI device
1203 int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn)
1209 if (!dev->is_physfn)
1212 if (pci_vfs_assigned(dev)) {
1213 pci_warn(dev, "Cannot modify SR-IOV while VFs are assigned\n");
1218 sriov_disable(dev);
1222 rc = sriov_enable(dev, nr_virtfn);