xref: /linux/drivers/pci/iov.c (revision 7328c8f48d1895b3fec98b0b319cfb856b4c4fa1)
1*7328c8f4SBjorn Helgaas // SPDX-License-Identifier: GPL-2.0
2d1b054daSYu Zhao /*
3d1b054daSYu Zhao  * drivers/pci/iov.c
4d1b054daSYu Zhao  *
5d1b054daSYu Zhao  * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
6d1b054daSYu Zhao  *
7d1b054daSYu Zhao  * PCI Express I/O Virtualization (IOV) support.
8d1b054daSYu Zhao  *   Single Root IOV 1.0
9302b4215SYu Zhao  *   Address Translation Service 1.0
10d1b054daSYu Zhao  */
11d1b054daSYu Zhao 
12d1b054daSYu Zhao #include <linux/pci.h>
135a0e3ad6STejun Heo #include <linux/slab.h>
14d1b054daSYu Zhao #include <linux/mutex.h>
15363c75dbSPaul Gortmaker #include <linux/export.h>
16d1b054daSYu Zhao #include <linux/string.h>
17d1b054daSYu Zhao #include <linux/delay.h>
185cdede24SJoerg Roedel #include <linux/pci-ats.h>
19d1b054daSYu Zhao #include "pci.h"
20d1b054daSYu Zhao 
21dd7cc44dSYu Zhao #define VIRTFN_ID_LEN	16
22d1b054daSYu Zhao 
23b07579c0SWei Yang int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id)
24a28724b0SYu Zhao {
25b07579c0SWei Yang 	if (!dev->is_physfn)
26b07579c0SWei Yang 		return -EINVAL;
27a28724b0SYu Zhao 	return dev->bus->number + ((dev->devfn + dev->sriov->offset +
28b07579c0SWei Yang 				    dev->sriov->stride * vf_id) >> 8);
29a28724b0SYu Zhao }
30a28724b0SYu Zhao 
31b07579c0SWei Yang int pci_iov_virtfn_devfn(struct pci_dev *dev, int vf_id)
32a28724b0SYu Zhao {
33b07579c0SWei Yang 	if (!dev->is_physfn)
34b07579c0SWei Yang 		return -EINVAL;
35a28724b0SYu Zhao 	return (dev->devfn + dev->sriov->offset +
36b07579c0SWei Yang 		dev->sriov->stride * vf_id) & 0xff;
37a28724b0SYu Zhao }
38a28724b0SYu Zhao 
39f59dca27SWei Yang /*
40f59dca27SWei Yang  * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
41f59dca27SWei Yang  * change when NumVFs changes.
42f59dca27SWei Yang  *
43f59dca27SWei Yang  * Update iov->offset and iov->stride when NumVFs is written.
44f59dca27SWei Yang  */
45f59dca27SWei Yang static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
46f59dca27SWei Yang {
47f59dca27SWei Yang 	struct pci_sriov *iov = dev->sriov;
48f59dca27SWei Yang 
49f59dca27SWei Yang 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
50f59dca27SWei Yang 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
51f59dca27SWei Yang 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
52f59dca27SWei Yang }
53f59dca27SWei Yang 
544449f079SWei Yang /*
554449f079SWei Yang  * The PF consumes one bus number.  NumVFs, First VF Offset, and VF Stride
564449f079SWei Yang  * determine how many additional bus numbers will be consumed by VFs.
574449f079SWei Yang  *
58ea9a8854SAlexander Duyck  * Iterate over all valid NumVFs, validate offset and stride, and calculate
59ea9a8854SAlexander Duyck  * the maximum number of bus numbers that could ever be required.
604449f079SWei Yang  */
61ea9a8854SAlexander Duyck static int compute_max_vf_buses(struct pci_dev *dev)
624449f079SWei Yang {
634449f079SWei Yang 	struct pci_sriov *iov = dev->sriov;
64ea9a8854SAlexander Duyck 	int nr_virtfn, busnr, rc = 0;
654449f079SWei Yang 
66ea9a8854SAlexander Duyck 	for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) {
674449f079SWei Yang 		pci_iov_set_numvfs(dev, nr_virtfn);
68ea9a8854SAlexander Duyck 		if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) {
69ea9a8854SAlexander Duyck 			rc = -EIO;
70ea9a8854SAlexander Duyck 			goto out;
714449f079SWei Yang 		}
724449f079SWei Yang 
73ea9a8854SAlexander Duyck 		busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
74ea9a8854SAlexander Duyck 		if (busnr > iov->max_VF_buses)
75ea9a8854SAlexander Duyck 			iov->max_VF_buses = busnr;
76ea9a8854SAlexander Duyck 	}
77ea9a8854SAlexander Duyck 
78ea9a8854SAlexander Duyck out:
79ea9a8854SAlexander Duyck 	pci_iov_set_numvfs(dev, 0);
80ea9a8854SAlexander Duyck 	return rc;
814449f079SWei Yang }
824449f079SWei Yang 
83dd7cc44dSYu Zhao static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
84dd7cc44dSYu Zhao {
85dd7cc44dSYu Zhao 	struct pci_bus *child;
86dd7cc44dSYu Zhao 
87dd7cc44dSYu Zhao 	if (bus->number == busnr)
88dd7cc44dSYu Zhao 		return bus;
89dd7cc44dSYu Zhao 
90dd7cc44dSYu Zhao 	child = pci_find_bus(pci_domain_nr(bus), busnr);
91dd7cc44dSYu Zhao 	if (child)
92dd7cc44dSYu Zhao 		return child;
93dd7cc44dSYu Zhao 
94dd7cc44dSYu Zhao 	child = pci_add_new_bus(bus, NULL, busnr);
95dd7cc44dSYu Zhao 	if (!child)
96dd7cc44dSYu Zhao 		return NULL;
97dd7cc44dSYu Zhao 
98b7eac055SYinghai Lu 	pci_bus_insert_busn_res(child, busnr, busnr);
99dd7cc44dSYu Zhao 
100dd7cc44dSYu Zhao 	return child;
101dd7cc44dSYu Zhao }
102dd7cc44dSYu Zhao 
103dc087f2fSJiang Liu static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
104dd7cc44dSYu Zhao {
105dc087f2fSJiang Liu 	if (physbus != virtbus && list_empty(&virtbus->devices))
106dc087f2fSJiang Liu 		pci_remove_bus(virtbus);
107dd7cc44dSYu Zhao }
108dd7cc44dSYu Zhao 
1090e6c9122SWei Yang resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
1100e6c9122SWei Yang {
1110e6c9122SWei Yang 	if (!dev->is_physfn)
1120e6c9122SWei Yang 		return 0;
1130e6c9122SWei Yang 
1140e6c9122SWei Yang 	return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
1150e6c9122SWei Yang }
1160e6c9122SWei Yang 
117753f6124SJan H. Schönherr int pci_iov_add_virtfn(struct pci_dev *dev, int id)
118dd7cc44dSYu Zhao {
119dd7cc44dSYu Zhao 	int i;
120dc087f2fSJiang Liu 	int rc = -ENOMEM;
121dd7cc44dSYu Zhao 	u64 size;
122dd7cc44dSYu Zhao 	char buf[VIRTFN_ID_LEN];
123dd7cc44dSYu Zhao 	struct pci_dev *virtfn;
124dd7cc44dSYu Zhao 	struct resource *res;
125dd7cc44dSYu Zhao 	struct pci_sriov *iov = dev->sriov;
1268b1fce04SGu Zheng 	struct pci_bus *bus;
127dd7cc44dSYu Zhao 
128b07579c0SWei Yang 	bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
129dc087f2fSJiang Liu 	if (!bus)
130dc087f2fSJiang Liu 		goto failed;
131dc087f2fSJiang Liu 
132dc087f2fSJiang Liu 	virtfn = pci_alloc_dev(bus);
133dc087f2fSJiang Liu 	if (!virtfn)
134dc087f2fSJiang Liu 		goto failed0;
135dc087f2fSJiang Liu 
136b07579c0SWei Yang 	virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
137dd7cc44dSYu Zhao 	virtfn->vendor = dev->vendor;
1383142d832SFilippo Sironi 	virtfn->device = iov->vf_device;
139156c5532SPo Liu 	rc = pci_setup_device(virtfn);
140156c5532SPo Liu 	if (rc)
141156c5532SPo Liu 		goto failed0;
142156c5532SPo Liu 
143dd7cc44dSYu Zhao 	virtfn->dev.parent = dev->dev.parent;
144fbf33f51SXudong Hao 	virtfn->physfn = pci_dev_get(dev);
145fbf33f51SXudong Hao 	virtfn->is_virtfn = 1;
146aa931977SAlex Williamson 	virtfn->multifunction = 0;
147dd7cc44dSYu Zhao 
148dd7cc44dSYu Zhao 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
149c1fe1f96SBjorn Helgaas 		res = &dev->resource[i + PCI_IOV_RESOURCES];
150dd7cc44dSYu Zhao 		if (!res->parent)
151dd7cc44dSYu Zhao 			continue;
152dd7cc44dSYu Zhao 		virtfn->resource[i].name = pci_name(virtfn);
153dd7cc44dSYu Zhao 		virtfn->resource[i].flags = res->flags;
1540e6c9122SWei Yang 		size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
155dd7cc44dSYu Zhao 		virtfn->resource[i].start = res->start + size * id;
156dd7cc44dSYu Zhao 		virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
157dd7cc44dSYu Zhao 		rc = request_resource(res, &virtfn->resource[i]);
158dd7cc44dSYu Zhao 		BUG_ON(rc);
159dd7cc44dSYu Zhao 	}
160dd7cc44dSYu Zhao 
161dd7cc44dSYu Zhao 	pci_device_add(virtfn, virtfn->bus);
162dd7cc44dSYu Zhao 
163dd7cc44dSYu Zhao 	sprintf(buf, "virtfn%u", id);
164dd7cc44dSYu Zhao 	rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
165dd7cc44dSYu Zhao 	if (rc)
166dd7cc44dSYu Zhao 		goto failed1;
167dd7cc44dSYu Zhao 	rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
168dd7cc44dSYu Zhao 	if (rc)
169dd7cc44dSYu Zhao 		goto failed2;
170dd7cc44dSYu Zhao 
171dd7cc44dSYu Zhao 	kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
172dd7cc44dSYu Zhao 
17327d61629SStuart Hayes 	pci_bus_add_device(virtfn);
17427d61629SStuart Hayes 
175dd7cc44dSYu Zhao 	return 0;
176dd7cc44dSYu Zhao 
177dd7cc44dSYu Zhao failed2:
178dd7cc44dSYu Zhao 	sysfs_remove_link(&dev->dev.kobj, buf);
179dd7cc44dSYu Zhao failed1:
180dd7cc44dSYu Zhao 	pci_dev_put(dev);
181210647afSYinghai Lu 	pci_stop_and_remove_bus_device(virtfn);
182dc087f2fSJiang Liu failed0:
183dc087f2fSJiang Liu 	virtfn_remove_bus(dev->bus, bus);
184dc087f2fSJiang Liu failed:
185dd7cc44dSYu Zhao 
186dd7cc44dSYu Zhao 	return rc;
187dd7cc44dSYu Zhao }
188dd7cc44dSYu Zhao 
189753f6124SJan H. Schönherr void pci_iov_remove_virtfn(struct pci_dev *dev, int id)
190dd7cc44dSYu Zhao {
191dd7cc44dSYu Zhao 	char buf[VIRTFN_ID_LEN];
192dd7cc44dSYu Zhao 	struct pci_dev *virtfn;
193dd7cc44dSYu Zhao 
194dc087f2fSJiang Liu 	virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
195b07579c0SWei Yang 					     pci_iov_virtfn_bus(dev, id),
196b07579c0SWei Yang 					     pci_iov_virtfn_devfn(dev, id));
197dd7cc44dSYu Zhao 	if (!virtfn)
198dd7cc44dSYu Zhao 		return;
199dd7cc44dSYu Zhao 
200dd7cc44dSYu Zhao 	sprintf(buf, "virtfn%u", id);
201dd7cc44dSYu Zhao 	sysfs_remove_link(&dev->dev.kobj, buf);
20209cedbefSYinghai Lu 	/*
20309cedbefSYinghai Lu 	 * pci_stop_dev() could have been called for this virtfn already,
20409cedbefSYinghai Lu 	 * so the directory for the virtfn may have been removed before.
20509cedbefSYinghai Lu 	 * Double check to avoid spurious sysfs warnings.
20609cedbefSYinghai Lu 	 */
20709cedbefSYinghai Lu 	if (virtfn->dev.kobj.sd)
208dd7cc44dSYu Zhao 		sysfs_remove_link(&virtfn->dev.kobj, "physfn");
209dd7cc44dSYu Zhao 
210210647afSYinghai Lu 	pci_stop_and_remove_bus_device(virtfn);
211dc087f2fSJiang Liu 	virtfn_remove_bus(dev->bus, virtfn->bus);
212dd7cc44dSYu Zhao 
213dc087f2fSJiang Liu 	/* balance pci_get_domain_bus_and_slot() */
214dc087f2fSJiang Liu 	pci_dev_put(virtfn);
215dd7cc44dSYu Zhao 	pci_dev_put(dev);
216dd7cc44dSYu Zhao }
217dd7cc44dSYu Zhao 
218995df527SWei Yang int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
219995df527SWei Yang {
220995df527SWei Yang 	return 0;
221995df527SWei Yang }
222995df527SWei Yang 
223a39e3fcdSAlexander Duyck int __weak pcibios_sriov_disable(struct pci_dev *pdev)
224a39e3fcdSAlexander Duyck {
225a39e3fcdSAlexander Duyck 	return 0;
226a39e3fcdSAlexander Duyck }
227a39e3fcdSAlexander Duyck 
228dd7cc44dSYu Zhao static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
229dd7cc44dSYu Zhao {
230dd7cc44dSYu Zhao 	int rc;
2313443c382SAlexander Duyck 	int i;
232dd7cc44dSYu Zhao 	int nres;
233ce288ec3SAlexander Duyck 	u16 initial;
234dd7cc44dSYu Zhao 	struct resource *res;
235dd7cc44dSYu Zhao 	struct pci_dev *pdev;
236dd7cc44dSYu Zhao 	struct pci_sriov *iov = dev->sriov;
237bbef98abSRam Pai 	int bars = 0;
238b07579c0SWei Yang 	int bus;
239dd7cc44dSYu Zhao 
240dd7cc44dSYu Zhao 	if (!nr_virtfn)
241dd7cc44dSYu Zhao 		return 0;
242dd7cc44dSYu Zhao 
2436b136724SBjorn Helgaas 	if (iov->num_VFs)
244dd7cc44dSYu Zhao 		return -EINVAL;
245dd7cc44dSYu Zhao 
246dd7cc44dSYu Zhao 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
2476b136724SBjorn Helgaas 	if (initial > iov->total_VFs ||
2486b136724SBjorn Helgaas 	    (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
249dd7cc44dSYu Zhao 		return -EIO;
250dd7cc44dSYu Zhao 
2516b136724SBjorn Helgaas 	if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
252dd7cc44dSYu Zhao 	    (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
253dd7cc44dSYu Zhao 		return -EINVAL;
254dd7cc44dSYu Zhao 
255dd7cc44dSYu Zhao 	nres = 0;
256dd7cc44dSYu Zhao 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
257bbef98abSRam Pai 		bars |= (1 << (i + PCI_IOV_RESOURCES));
258c1fe1f96SBjorn Helgaas 		res = &dev->resource[i + PCI_IOV_RESOURCES];
259dd7cc44dSYu Zhao 		if (res->parent)
260dd7cc44dSYu Zhao 			nres++;
261dd7cc44dSYu Zhao 	}
262dd7cc44dSYu Zhao 	if (nres != iov->nres) {
263dd7cc44dSYu Zhao 		dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
264dd7cc44dSYu Zhao 		return -ENOMEM;
265dd7cc44dSYu Zhao 	}
266dd7cc44dSYu Zhao 
267b07579c0SWei Yang 	bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
26868f8e9faSBjorn Helgaas 	if (bus > dev->bus->busn_res.end) {
26968f8e9faSBjorn Helgaas 		dev_err(&dev->dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
27068f8e9faSBjorn Helgaas 			nr_virtfn, bus, &dev->bus->busn_res);
271dd7cc44dSYu Zhao 		return -ENOMEM;
272dd7cc44dSYu Zhao 	}
273dd7cc44dSYu Zhao 
274bbef98abSRam Pai 	if (pci_enable_resources(dev, bars)) {
275bbef98abSRam Pai 		dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
276bbef98abSRam Pai 		return -ENOMEM;
277bbef98abSRam Pai 	}
278bbef98abSRam Pai 
279dd7cc44dSYu Zhao 	if (iov->link != dev->devfn) {
280dd7cc44dSYu Zhao 		pdev = pci_get_slot(dev->bus, iov->link);
281dd7cc44dSYu Zhao 		if (!pdev)
282dd7cc44dSYu Zhao 			return -ENODEV;
283dd7cc44dSYu Zhao 
284dc087f2fSJiang Liu 		if (!pdev->is_physfn) {
285dd7cc44dSYu Zhao 			pci_dev_put(pdev);
286652d1100SStefan Assmann 			return -ENOSYS;
287dc087f2fSJiang Liu 		}
288dd7cc44dSYu Zhao 
289dd7cc44dSYu Zhao 		rc = sysfs_create_link(&dev->dev.kobj,
290dd7cc44dSYu Zhao 					&pdev->dev.kobj, "dep_link");
291dc087f2fSJiang Liu 		pci_dev_put(pdev);
292dd7cc44dSYu Zhao 		if (rc)
293dd7cc44dSYu Zhao 			return rc;
294dd7cc44dSYu Zhao 	}
295dd7cc44dSYu Zhao 
2966b136724SBjorn Helgaas 	iov->initial_VFs = initial;
297dd7cc44dSYu Zhao 	if (nr_virtfn < initial)
298dd7cc44dSYu Zhao 		initial = nr_virtfn;
299dd7cc44dSYu Zhao 
300c23b6135SAlexander Duyck 	rc = pcibios_sriov_enable(dev, initial);
301c23b6135SAlexander Duyck 	if (rc) {
302c23b6135SAlexander Duyck 		dev_err(&dev->dev, "failure %d from pcibios_sriov_enable()\n", rc);
303c23b6135SAlexander Duyck 		goto err_pcibios;
304995df527SWei Yang 	}
305995df527SWei Yang 
306f40ec3c7SGavin Shan 	pci_iov_set_numvfs(dev, nr_virtfn);
307f40ec3c7SGavin Shan 	iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
308f40ec3c7SGavin Shan 	pci_cfg_access_lock(dev);
309f40ec3c7SGavin Shan 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
310f40ec3c7SGavin Shan 	msleep(100);
311f40ec3c7SGavin Shan 	pci_cfg_access_unlock(dev);
312f40ec3c7SGavin Shan 
313dd7cc44dSYu Zhao 	for (i = 0; i < initial; i++) {
314753f6124SJan H. Schönherr 		rc = pci_iov_add_virtfn(dev, i);
315dd7cc44dSYu Zhao 		if (rc)
316dd7cc44dSYu Zhao 			goto failed;
317dd7cc44dSYu Zhao 	}
318dd7cc44dSYu Zhao 
319dd7cc44dSYu Zhao 	kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
3206b136724SBjorn Helgaas 	iov->num_VFs = nr_virtfn;
321dd7cc44dSYu Zhao 
322dd7cc44dSYu Zhao 	return 0;
323dd7cc44dSYu Zhao 
324dd7cc44dSYu Zhao failed:
3253443c382SAlexander Duyck 	while (i--)
326753f6124SJan H. Schönherr 		pci_iov_remove_virtfn(dev, i);
327dd7cc44dSYu Zhao 
328c23b6135SAlexander Duyck err_pcibios:
329dd7cc44dSYu Zhao 	iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
330fb51ccbfSJan Kiszka 	pci_cfg_access_lock(dev);
331dd7cc44dSYu Zhao 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
332dd7cc44dSYu Zhao 	ssleep(1);
333fb51ccbfSJan Kiszka 	pci_cfg_access_unlock(dev);
334dd7cc44dSYu Zhao 
3350fc690a7SGavin Shan 	pcibios_sriov_disable(dev);
3360fc690a7SGavin Shan 
337dd7cc44dSYu Zhao 	if (iov->link != dev->devfn)
338dd7cc44dSYu Zhao 		sysfs_remove_link(&dev->dev.kobj, "dep_link");
339dd7cc44dSYu Zhao 
340b3908644SAlexander Duyck 	pci_iov_set_numvfs(dev, 0);
341dd7cc44dSYu Zhao 	return rc;
342dd7cc44dSYu Zhao }
343dd7cc44dSYu Zhao 
344dd7cc44dSYu Zhao static void sriov_disable(struct pci_dev *dev)
345dd7cc44dSYu Zhao {
346dd7cc44dSYu Zhao 	int i;
347dd7cc44dSYu Zhao 	struct pci_sriov *iov = dev->sriov;
348dd7cc44dSYu Zhao 
3496b136724SBjorn Helgaas 	if (!iov->num_VFs)
350dd7cc44dSYu Zhao 		return;
351dd7cc44dSYu Zhao 
3526b136724SBjorn Helgaas 	for (i = 0; i < iov->num_VFs; i++)
353753f6124SJan H. Schönherr 		pci_iov_remove_virtfn(dev, i);
354dd7cc44dSYu Zhao 
355dd7cc44dSYu Zhao 	iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
356fb51ccbfSJan Kiszka 	pci_cfg_access_lock(dev);
357dd7cc44dSYu Zhao 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
358dd7cc44dSYu Zhao 	ssleep(1);
359fb51ccbfSJan Kiszka 	pci_cfg_access_unlock(dev);
360dd7cc44dSYu Zhao 
3610fc690a7SGavin Shan 	pcibios_sriov_disable(dev);
3620fc690a7SGavin Shan 
363dd7cc44dSYu Zhao 	if (iov->link != dev->devfn)
364dd7cc44dSYu Zhao 		sysfs_remove_link(&dev->dev.kobj, "dep_link");
365dd7cc44dSYu Zhao 
3666b136724SBjorn Helgaas 	iov->num_VFs = 0;
367f59dca27SWei Yang 	pci_iov_set_numvfs(dev, 0);
368dd7cc44dSYu Zhao }
369dd7cc44dSYu Zhao 
370d1b054daSYu Zhao static int sriov_init(struct pci_dev *dev, int pos)
371d1b054daSYu Zhao {
3720e6c9122SWei Yang 	int i, bar64;
373d1b054daSYu Zhao 	int rc;
374d1b054daSYu Zhao 	int nres;
375d1b054daSYu Zhao 	u32 pgsz;
376ea9a8854SAlexander Duyck 	u16 ctrl, total;
377d1b054daSYu Zhao 	struct pci_sriov *iov;
378d1b054daSYu Zhao 	struct resource *res;
379d1b054daSYu Zhao 	struct pci_dev *pdev;
380d1b054daSYu Zhao 
381d1b054daSYu Zhao 	pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
382d1b054daSYu Zhao 	if (ctrl & PCI_SRIOV_CTRL_VFE) {
383d1b054daSYu Zhao 		pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
384d1b054daSYu Zhao 		ssleep(1);
385d1b054daSYu Zhao 	}
386d1b054daSYu Zhao 
387d1b054daSYu Zhao 	ctrl = 0;
388d1b054daSYu Zhao 	list_for_each_entry(pdev, &dev->bus->devices, bus_list)
389d1b054daSYu Zhao 		if (pdev->is_physfn)
390d1b054daSYu Zhao 			goto found;
391d1b054daSYu Zhao 
392d1b054daSYu Zhao 	pdev = NULL;
393d1b054daSYu Zhao 	if (pci_ari_enabled(dev->bus))
394d1b054daSYu Zhao 		ctrl |= PCI_SRIOV_CTRL_ARI;
395d1b054daSYu Zhao 
396d1b054daSYu Zhao found:
397d1b054daSYu Zhao 	pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
398d1b054daSYu Zhao 
399ff45f9ddSBen Shelton 	pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
400ff45f9ddSBen Shelton 	if (!total)
401ff45f9ddSBen Shelton 		return 0;
402d1b054daSYu Zhao 
403d1b054daSYu Zhao 	pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
404d1b054daSYu Zhao 	i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
405d1b054daSYu Zhao 	pgsz &= ~((1 << i) - 1);
406d1b054daSYu Zhao 	if (!pgsz)
407d1b054daSYu Zhao 		return -EIO;
408d1b054daSYu Zhao 
409d1b054daSYu Zhao 	pgsz &= ~(pgsz - 1);
4108161fe91SVaidyanathan Srinivasan 	pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
411d1b054daSYu Zhao 
4120e6c9122SWei Yang 	iov = kzalloc(sizeof(*iov), GFP_KERNEL);
4130e6c9122SWei Yang 	if (!iov)
4140e6c9122SWei Yang 		return -ENOMEM;
4150e6c9122SWei Yang 
416d1b054daSYu Zhao 	nres = 0;
417d1b054daSYu Zhao 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
418c1fe1f96SBjorn Helgaas 		res = &dev->resource[i + PCI_IOV_RESOURCES];
41911183991SDavid Daney 		/*
42011183991SDavid Daney 		 * If it is already FIXED, don't change it, something
42111183991SDavid Daney 		 * (perhaps EA or header fixups) wants it this way.
42211183991SDavid Daney 		 */
42311183991SDavid Daney 		if (res->flags & IORESOURCE_PCI_FIXED)
42411183991SDavid Daney 			bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
42511183991SDavid Daney 		else
4260e6c9122SWei Yang 			bar64 = __pci_read_base(dev, pci_bar_unknown, res,
427d1b054daSYu Zhao 						pos + PCI_SRIOV_BAR + i * 4);
428d1b054daSYu Zhao 		if (!res->flags)
429d1b054daSYu Zhao 			continue;
430d1b054daSYu Zhao 		if (resource_size(res) & (PAGE_SIZE - 1)) {
431d1b054daSYu Zhao 			rc = -EIO;
432d1b054daSYu Zhao 			goto failed;
433d1b054daSYu Zhao 		}
4340e6c9122SWei Yang 		iov->barsz[i] = resource_size(res);
435d1b054daSYu Zhao 		res->end = res->start + resource_size(res) * total - 1;
436e88ae01dSWei Yang 		dev_info(&dev->dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
437e88ae01dSWei Yang 			 i, res, i, total);
4380e6c9122SWei Yang 		i += bar64;
439d1b054daSYu Zhao 		nres++;
440d1b054daSYu Zhao 	}
441d1b054daSYu Zhao 
442d1b054daSYu Zhao 	iov->pos = pos;
443d1b054daSYu Zhao 	iov->nres = nres;
444d1b054daSYu Zhao 	iov->ctrl = ctrl;
4456b136724SBjorn Helgaas 	iov->total_VFs = total;
4463142d832SFilippo Sironi 	pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
447d1b054daSYu Zhao 	iov->pgsz = pgsz;
448d1b054daSYu Zhao 	iov->self = dev;
4490e7df224SBodong Wang 	iov->drivers_autoprobe = true;
450d1b054daSYu Zhao 	pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
451d1b054daSYu Zhao 	pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
45262f87c0eSYijing Wang 	if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
4534d135dbeSYu Zhao 		iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
454d1b054daSYu Zhao 
455d1b054daSYu Zhao 	if (pdev)
456d1b054daSYu Zhao 		iov->dev = pci_dev_get(pdev);
457e277d2fcSYu Zhao 	else
458d1b054daSYu Zhao 		iov->dev = dev;
459e277d2fcSYu Zhao 
460d1b054daSYu Zhao 	dev->sriov = iov;
461d1b054daSYu Zhao 	dev->is_physfn = 1;
462ea9a8854SAlexander Duyck 	rc = compute_max_vf_buses(dev);
463ea9a8854SAlexander Duyck 	if (rc)
464ea9a8854SAlexander Duyck 		goto fail_max_buses;
465d1b054daSYu Zhao 
466d1b054daSYu Zhao 	return 0;
467d1b054daSYu Zhao 
468ea9a8854SAlexander Duyck fail_max_buses:
469ea9a8854SAlexander Duyck 	dev->sriov = NULL;
470ea9a8854SAlexander Duyck 	dev->is_physfn = 0;
471d1b054daSYu Zhao failed:
472d1b054daSYu Zhao 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
473c1fe1f96SBjorn Helgaas 		res = &dev->resource[i + PCI_IOV_RESOURCES];
474d1b054daSYu Zhao 		res->flags = 0;
475d1b054daSYu Zhao 	}
476d1b054daSYu Zhao 
4770e6c9122SWei Yang 	kfree(iov);
478d1b054daSYu Zhao 	return rc;
479d1b054daSYu Zhao }
480d1b054daSYu Zhao 
481d1b054daSYu Zhao static void sriov_release(struct pci_dev *dev)
482d1b054daSYu Zhao {
4836b136724SBjorn Helgaas 	BUG_ON(dev->sriov->num_VFs);
484dd7cc44dSYu Zhao 
485e277d2fcSYu Zhao 	if (dev != dev->sriov->dev)
486d1b054daSYu Zhao 		pci_dev_put(dev->sriov->dev);
487d1b054daSYu Zhao 
488d1b054daSYu Zhao 	kfree(dev->sriov);
489d1b054daSYu Zhao 	dev->sriov = NULL;
490d1b054daSYu Zhao }
491d1b054daSYu Zhao 
4928c5cdb6aSYu Zhao static void sriov_restore_state(struct pci_dev *dev)
4938c5cdb6aSYu Zhao {
4948c5cdb6aSYu Zhao 	int i;
4958c5cdb6aSYu Zhao 	u16 ctrl;
4968c5cdb6aSYu Zhao 	struct pci_sriov *iov = dev->sriov;
4978c5cdb6aSYu Zhao 
4988c5cdb6aSYu Zhao 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
4998c5cdb6aSYu Zhao 	if (ctrl & PCI_SRIOV_CTRL_VFE)
5008c5cdb6aSYu Zhao 		return;
5018c5cdb6aSYu Zhao 
502ff26449eSTony Nguyen 	/*
503ff26449eSTony Nguyen 	 * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
504ff26449eSTony Nguyen 	 * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
505ff26449eSTony Nguyen 	 */
506ff26449eSTony Nguyen 	ctrl &= ~PCI_SRIOV_CTRL_ARI;
507ff26449eSTony Nguyen 	ctrl |= iov->ctrl & PCI_SRIOV_CTRL_ARI;
508ff26449eSTony Nguyen 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
509ff26449eSTony Nguyen 
5108c5cdb6aSYu Zhao 	for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
5118c5cdb6aSYu Zhao 		pci_update_resource(dev, i);
5128c5cdb6aSYu Zhao 
5138c5cdb6aSYu Zhao 	pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
514f59dca27SWei Yang 	pci_iov_set_numvfs(dev, iov->num_VFs);
5158c5cdb6aSYu Zhao 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
5168c5cdb6aSYu Zhao 	if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
5178c5cdb6aSYu Zhao 		msleep(100);
5188c5cdb6aSYu Zhao }
5198c5cdb6aSYu Zhao 
520d1b054daSYu Zhao /**
521d1b054daSYu Zhao  * pci_iov_init - initialize the IOV capability
522d1b054daSYu Zhao  * @dev: the PCI device
523d1b054daSYu Zhao  *
524d1b054daSYu Zhao  * Returns 0 on success, or negative on failure.
525d1b054daSYu Zhao  */
526d1b054daSYu Zhao int pci_iov_init(struct pci_dev *dev)
527d1b054daSYu Zhao {
528d1b054daSYu Zhao 	int pos;
529d1b054daSYu Zhao 
5305f4d91a1SKenji Kaneshige 	if (!pci_is_pcie(dev))
531d1b054daSYu Zhao 		return -ENODEV;
532d1b054daSYu Zhao 
533d1b054daSYu Zhao 	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
534d1b054daSYu Zhao 	if (pos)
535d1b054daSYu Zhao 		return sriov_init(dev, pos);
536d1b054daSYu Zhao 
537d1b054daSYu Zhao 	return -ENODEV;
538d1b054daSYu Zhao }
539d1b054daSYu Zhao 
540d1b054daSYu Zhao /**
541d1b054daSYu Zhao  * pci_iov_release - release resources used by the IOV capability
542d1b054daSYu Zhao  * @dev: the PCI device
543d1b054daSYu Zhao  */
544d1b054daSYu Zhao void pci_iov_release(struct pci_dev *dev)
545d1b054daSYu Zhao {
546d1b054daSYu Zhao 	if (dev->is_physfn)
547d1b054daSYu Zhao 		sriov_release(dev);
548d1b054daSYu Zhao }
549d1b054daSYu Zhao 
550d1b054daSYu Zhao /**
5516ffa2489SBjorn Helgaas  * pci_iov_update_resource - update a VF BAR
5526ffa2489SBjorn Helgaas  * @dev: the PCI device
5536ffa2489SBjorn Helgaas  * @resno: the resource number
5546ffa2489SBjorn Helgaas  *
5556ffa2489SBjorn Helgaas  * Update a VF BAR in the SR-IOV capability of a PF.
5566ffa2489SBjorn Helgaas  */
5576ffa2489SBjorn Helgaas void pci_iov_update_resource(struct pci_dev *dev, int resno)
5586ffa2489SBjorn Helgaas {
5596ffa2489SBjorn Helgaas 	struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
5606ffa2489SBjorn Helgaas 	struct resource *res = dev->resource + resno;
5616ffa2489SBjorn Helgaas 	int vf_bar = resno - PCI_IOV_RESOURCES;
5626ffa2489SBjorn Helgaas 	struct pci_bus_region region;
563546ba9f8SBjorn Helgaas 	u16 cmd;
5646ffa2489SBjorn Helgaas 	u32 new;
5656ffa2489SBjorn Helgaas 	int reg;
5666ffa2489SBjorn Helgaas 
5676ffa2489SBjorn Helgaas 	/*
5686ffa2489SBjorn Helgaas 	 * The generic pci_restore_bars() path calls this for all devices,
5696ffa2489SBjorn Helgaas 	 * including VFs and non-SR-IOV devices.  If this is not a PF, we
5706ffa2489SBjorn Helgaas 	 * have nothing to do.
5716ffa2489SBjorn Helgaas 	 */
5726ffa2489SBjorn Helgaas 	if (!iov)
5736ffa2489SBjorn Helgaas 		return;
5746ffa2489SBjorn Helgaas 
575546ba9f8SBjorn Helgaas 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
576546ba9f8SBjorn Helgaas 	if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) {
577546ba9f8SBjorn Helgaas 		dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
578546ba9f8SBjorn Helgaas 			 vf_bar, res);
579546ba9f8SBjorn Helgaas 		return;
580546ba9f8SBjorn Helgaas 	}
581546ba9f8SBjorn Helgaas 
5826ffa2489SBjorn Helgaas 	/*
5836ffa2489SBjorn Helgaas 	 * Ignore unimplemented BARs, unused resource slots for 64-bit
5846ffa2489SBjorn Helgaas 	 * BARs, and non-movable resources, e.g., those described via
5856ffa2489SBjorn Helgaas 	 * Enhanced Allocation.
5866ffa2489SBjorn Helgaas 	 */
5876ffa2489SBjorn Helgaas 	if (!res->flags)
5886ffa2489SBjorn Helgaas 		return;
5896ffa2489SBjorn Helgaas 
5906ffa2489SBjorn Helgaas 	if (res->flags & IORESOURCE_UNSET)
5916ffa2489SBjorn Helgaas 		return;
5926ffa2489SBjorn Helgaas 
5936ffa2489SBjorn Helgaas 	if (res->flags & IORESOURCE_PCI_FIXED)
5946ffa2489SBjorn Helgaas 		return;
5956ffa2489SBjorn Helgaas 
5966ffa2489SBjorn Helgaas 	pcibios_resource_to_bus(dev->bus, &region, res);
5976ffa2489SBjorn Helgaas 	new = region.start;
5986ffa2489SBjorn Helgaas 	new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK;
5996ffa2489SBjorn Helgaas 
6006ffa2489SBjorn Helgaas 	reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar;
6016ffa2489SBjorn Helgaas 	pci_write_config_dword(dev, reg, new);
6026ffa2489SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM_64) {
6036ffa2489SBjorn Helgaas 		new = region.start >> 16 >> 16;
6046ffa2489SBjorn Helgaas 		pci_write_config_dword(dev, reg + 4, new);
6056ffa2489SBjorn Helgaas 	}
6066ffa2489SBjorn Helgaas }
6076ffa2489SBjorn Helgaas 
608978d2d68SWei Yang resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
609978d2d68SWei Yang 						      int resno)
610978d2d68SWei Yang {
611978d2d68SWei Yang 	return pci_iov_resource_size(dev, resno);
612978d2d68SWei Yang }
613978d2d68SWei Yang 
6148c5cdb6aSYu Zhao /**
6156faf17f6SChris Wright  * pci_sriov_resource_alignment - get resource alignment for VF BAR
6166faf17f6SChris Wright  * @dev: the PCI device
6176faf17f6SChris Wright  * @resno: the resource number
6186faf17f6SChris Wright  *
6196faf17f6SChris Wright  * Returns the alignment of the VF BAR found in the SR-IOV capability.
6206faf17f6SChris Wright  * This is not the same as the resource size which is defined as
6216faf17f6SChris Wright  * the VF BAR size multiplied by the number of VFs.  The alignment
6226faf17f6SChris Wright  * is just the VF BAR size.
6236faf17f6SChris Wright  */
6240e52247aSCam Macdonell resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
6256faf17f6SChris Wright {
626978d2d68SWei Yang 	return pcibios_iov_resource_alignment(dev, resno);
6276faf17f6SChris Wright }
6286faf17f6SChris Wright 
6296faf17f6SChris Wright /**
6308c5cdb6aSYu Zhao  * pci_restore_iov_state - restore the state of the IOV capability
6318c5cdb6aSYu Zhao  * @dev: the PCI device
6328c5cdb6aSYu Zhao  */
6338c5cdb6aSYu Zhao void pci_restore_iov_state(struct pci_dev *dev)
6348c5cdb6aSYu Zhao {
6358c5cdb6aSYu Zhao 	if (dev->is_physfn)
6368c5cdb6aSYu Zhao 		sriov_restore_state(dev);
6378c5cdb6aSYu Zhao }
638a28724b0SYu Zhao 
639a28724b0SYu Zhao /**
640a28724b0SYu Zhao  * pci_iov_bus_range - find bus range used by Virtual Function
641a28724b0SYu Zhao  * @bus: the PCI bus
642a28724b0SYu Zhao  *
643a28724b0SYu Zhao  * Returns max number of buses (exclude current one) used by Virtual
644a28724b0SYu Zhao  * Functions.
645a28724b0SYu Zhao  */
646a28724b0SYu Zhao int pci_iov_bus_range(struct pci_bus *bus)
647a28724b0SYu Zhao {
648a28724b0SYu Zhao 	int max = 0;
649a28724b0SYu Zhao 	struct pci_dev *dev;
650a28724b0SYu Zhao 
651a28724b0SYu Zhao 	list_for_each_entry(dev, &bus->devices, bus_list) {
652a28724b0SYu Zhao 		if (!dev->is_physfn)
653a28724b0SYu Zhao 			continue;
6544449f079SWei Yang 		if (dev->sriov->max_VF_buses > max)
6554449f079SWei Yang 			max = dev->sriov->max_VF_buses;
656a28724b0SYu Zhao 	}
657a28724b0SYu Zhao 
658a28724b0SYu Zhao 	return max ? max - bus->number : 0;
659a28724b0SYu Zhao }
660dd7cc44dSYu Zhao 
661dd7cc44dSYu Zhao /**
662dd7cc44dSYu Zhao  * pci_enable_sriov - enable the SR-IOV capability
663dd7cc44dSYu Zhao  * @dev: the PCI device
66452a8873bSRandy Dunlap  * @nr_virtfn: number of virtual functions to enable
665dd7cc44dSYu Zhao  *
666dd7cc44dSYu Zhao  * Returns 0 on success, or negative on failure.
667dd7cc44dSYu Zhao  */
668dd7cc44dSYu Zhao int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
669dd7cc44dSYu Zhao {
670dd7cc44dSYu Zhao 	might_sleep();
671dd7cc44dSYu Zhao 
672dd7cc44dSYu Zhao 	if (!dev->is_physfn)
673652d1100SStefan Assmann 		return -ENOSYS;
674dd7cc44dSYu Zhao 
675dd7cc44dSYu Zhao 	return sriov_enable(dev, nr_virtfn);
676dd7cc44dSYu Zhao }
677dd7cc44dSYu Zhao EXPORT_SYMBOL_GPL(pci_enable_sriov);
678dd7cc44dSYu Zhao 
679dd7cc44dSYu Zhao /**
680dd7cc44dSYu Zhao  * pci_disable_sriov - disable the SR-IOV capability
681dd7cc44dSYu Zhao  * @dev: the PCI device
682dd7cc44dSYu Zhao  */
683dd7cc44dSYu Zhao void pci_disable_sriov(struct pci_dev *dev)
684dd7cc44dSYu Zhao {
685dd7cc44dSYu Zhao 	might_sleep();
686dd7cc44dSYu Zhao 
687dd7cc44dSYu Zhao 	if (!dev->is_physfn)
688dd7cc44dSYu Zhao 		return;
689dd7cc44dSYu Zhao 
690dd7cc44dSYu Zhao 	sriov_disable(dev);
691dd7cc44dSYu Zhao }
692dd7cc44dSYu Zhao EXPORT_SYMBOL_GPL(pci_disable_sriov);
69374bb1bccSYu Zhao 
69474bb1bccSYu Zhao /**
695fb8a0d9dSWilliams, Mitch A  * pci_num_vf - return number of VFs associated with a PF device_release_driver
696fb8a0d9dSWilliams, Mitch A  * @dev: the PCI device
697fb8a0d9dSWilliams, Mitch A  *
698fb8a0d9dSWilliams, Mitch A  * Returns number of VFs, or 0 if SR-IOV is not enabled.
699fb8a0d9dSWilliams, Mitch A  */
700fb8a0d9dSWilliams, Mitch A int pci_num_vf(struct pci_dev *dev)
701fb8a0d9dSWilliams, Mitch A {
7021452cd76SBjorn Helgaas 	if (!dev->is_physfn)
703fb8a0d9dSWilliams, Mitch A 		return 0;
7041452cd76SBjorn Helgaas 
7056b136724SBjorn Helgaas 	return dev->sriov->num_VFs;
706fb8a0d9dSWilliams, Mitch A }
707fb8a0d9dSWilliams, Mitch A EXPORT_SYMBOL_GPL(pci_num_vf);
708bff73156SDonald Dutile 
709bff73156SDonald Dutile /**
7105a8eb242SAlexander Duyck  * pci_vfs_assigned - returns number of VFs are assigned to a guest
7115a8eb242SAlexander Duyck  * @dev: the PCI device
7125a8eb242SAlexander Duyck  *
7135a8eb242SAlexander Duyck  * Returns number of VFs belonging to this device that are assigned to a guest.
714652d1100SStefan Assmann  * If device is not a physical function returns 0.
7155a8eb242SAlexander Duyck  */
7165a8eb242SAlexander Duyck int pci_vfs_assigned(struct pci_dev *dev)
7175a8eb242SAlexander Duyck {
7185a8eb242SAlexander Duyck 	struct pci_dev *vfdev;
7195a8eb242SAlexander Duyck 	unsigned int vfs_assigned = 0;
7205a8eb242SAlexander Duyck 	unsigned short dev_id;
7215a8eb242SAlexander Duyck 
7225a8eb242SAlexander Duyck 	/* only search if we are a PF */
7235a8eb242SAlexander Duyck 	if (!dev->is_physfn)
7245a8eb242SAlexander Duyck 		return 0;
7255a8eb242SAlexander Duyck 
7265a8eb242SAlexander Duyck 	/*
7275a8eb242SAlexander Duyck 	 * determine the device ID for the VFs, the vendor ID will be the
7285a8eb242SAlexander Duyck 	 * same as the PF so there is no need to check for that one
7295a8eb242SAlexander Duyck 	 */
7303142d832SFilippo Sironi 	dev_id = dev->sriov->vf_device;
7315a8eb242SAlexander Duyck 
7325a8eb242SAlexander Duyck 	/* loop through all the VFs to see if we own any that are assigned */
7335a8eb242SAlexander Duyck 	vfdev = pci_get_device(dev->vendor, dev_id, NULL);
7345a8eb242SAlexander Duyck 	while (vfdev) {
7355a8eb242SAlexander Duyck 		/*
7365a8eb242SAlexander Duyck 		 * It is considered assigned if it is a virtual function with
7375a8eb242SAlexander Duyck 		 * our dev as the physical function and the assigned bit is set
7385a8eb242SAlexander Duyck 		 */
7395a8eb242SAlexander Duyck 		if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
740be63497cSEthan Zhao 			pci_is_dev_assigned(vfdev))
7415a8eb242SAlexander Duyck 			vfs_assigned++;
7425a8eb242SAlexander Duyck 
7435a8eb242SAlexander Duyck 		vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
7445a8eb242SAlexander Duyck 	}
7455a8eb242SAlexander Duyck 
7465a8eb242SAlexander Duyck 	return vfs_assigned;
7475a8eb242SAlexander Duyck }
7485a8eb242SAlexander Duyck EXPORT_SYMBOL_GPL(pci_vfs_assigned);
7495a8eb242SAlexander Duyck 
7505a8eb242SAlexander Duyck /**
751bff73156SDonald Dutile  * pci_sriov_set_totalvfs -- reduce the TotalVFs available
752bff73156SDonald Dutile  * @dev: the PCI PF device
7532094f167SRandy Dunlap  * @numvfs: number that should be used for TotalVFs supported
754bff73156SDonald Dutile  *
755bff73156SDonald Dutile  * Should be called from PF driver's probe routine with
756bff73156SDonald Dutile  * device's mutex held.
757bff73156SDonald Dutile  *
758bff73156SDonald Dutile  * Returns 0 if PF is an SRIOV-capable device and
759652d1100SStefan Assmann  * value of numvfs valid. If not a PF return -ENOSYS;
760652d1100SStefan Assmann  * if numvfs is invalid return -EINVAL;
761bff73156SDonald Dutile  * if VFs already enabled, return -EBUSY.
762bff73156SDonald Dutile  */
763bff73156SDonald Dutile int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
764bff73156SDonald Dutile {
765652d1100SStefan Assmann 	if (!dev->is_physfn)
766652d1100SStefan Assmann 		return -ENOSYS;
767652d1100SStefan Assmann 	if (numvfs > dev->sriov->total_VFs)
768bff73156SDonald Dutile 		return -EINVAL;
769bff73156SDonald Dutile 
770bff73156SDonald Dutile 	/* Shouldn't change if VFs already enabled */
771bff73156SDonald Dutile 	if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
772bff73156SDonald Dutile 		return -EBUSY;
773bff73156SDonald Dutile 	else
7746b136724SBjorn Helgaas 		dev->sriov->driver_max_VFs = numvfs;
775bff73156SDonald Dutile 
776bff73156SDonald Dutile 	return 0;
777bff73156SDonald Dutile }
778bff73156SDonald Dutile EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
779bff73156SDonald Dutile 
780bff73156SDonald Dutile /**
781ddc191f5SJonghwan Choi  * pci_sriov_get_totalvfs -- get total VFs supported on this device
782bff73156SDonald Dutile  * @dev: the PCI PF device
783bff73156SDonald Dutile  *
784bff73156SDonald Dutile  * For a PCIe device with SRIOV support, return the PCIe
7856b136724SBjorn Helgaas  * SRIOV capability value of TotalVFs or the value of driver_max_VFs
786652d1100SStefan Assmann  * if the driver reduced it.  Otherwise 0.
787bff73156SDonald Dutile  */
788bff73156SDonald Dutile int pci_sriov_get_totalvfs(struct pci_dev *dev)
789bff73156SDonald Dutile {
7901452cd76SBjorn Helgaas 	if (!dev->is_physfn)
791652d1100SStefan Assmann 		return 0;
792bff73156SDonald Dutile 
7936b136724SBjorn Helgaas 	if (dev->sriov->driver_max_VFs)
7946b136724SBjorn Helgaas 		return dev->sriov->driver_max_VFs;
7951452cd76SBjorn Helgaas 
7966b136724SBjorn Helgaas 	return dev->sriov->total_VFs;
797bff73156SDonald Dutile }
798bff73156SDonald Dutile EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);
799