xref: /linux/drivers/pci/iov.c (revision ea9a8854161d9580cfabe011c0ae296ecc0e1d4f)
1d1b054daSYu Zhao /*
2d1b054daSYu Zhao  * drivers/pci/iov.c
3d1b054daSYu Zhao  *
4d1b054daSYu Zhao  * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
5d1b054daSYu Zhao  *
6d1b054daSYu Zhao  * PCI Express I/O Virtualization (IOV) support.
7d1b054daSYu Zhao  *   Single Root IOV 1.0
8302b4215SYu Zhao  *   Address Translation Service 1.0
9d1b054daSYu Zhao  */
10d1b054daSYu Zhao 
11d1b054daSYu Zhao #include <linux/pci.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
13d1b054daSYu Zhao #include <linux/mutex.h>
14363c75dbSPaul Gortmaker #include <linux/export.h>
15d1b054daSYu Zhao #include <linux/string.h>
16d1b054daSYu Zhao #include <linux/delay.h>
175cdede24SJoerg Roedel #include <linux/pci-ats.h>
18d1b054daSYu Zhao #include "pci.h"
19d1b054daSYu Zhao 
20dd7cc44dSYu Zhao #define VIRTFN_ID_LEN	16
21d1b054daSYu Zhao 
22b07579c0SWei Yang int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id)
23a28724b0SYu Zhao {
24b07579c0SWei Yang 	if (!dev->is_physfn)
25b07579c0SWei Yang 		return -EINVAL;
26a28724b0SYu Zhao 	return dev->bus->number + ((dev->devfn + dev->sriov->offset +
27b07579c0SWei Yang 				    dev->sriov->stride * vf_id) >> 8);
28a28724b0SYu Zhao }
29a28724b0SYu Zhao 
30b07579c0SWei Yang int pci_iov_virtfn_devfn(struct pci_dev *dev, int vf_id)
31a28724b0SYu Zhao {
32b07579c0SWei Yang 	if (!dev->is_physfn)
33b07579c0SWei Yang 		return -EINVAL;
34a28724b0SYu Zhao 	return (dev->devfn + dev->sriov->offset +
35b07579c0SWei Yang 		dev->sriov->stride * vf_id) & 0xff;
36a28724b0SYu Zhao }
37a28724b0SYu Zhao 
38f59dca27SWei Yang /*
39f59dca27SWei Yang  * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
40f59dca27SWei Yang  * change when NumVFs changes.
41f59dca27SWei Yang  *
42f59dca27SWei Yang  * Update iov->offset and iov->stride when NumVFs is written.
43f59dca27SWei Yang  */
44f59dca27SWei Yang static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
45f59dca27SWei Yang {
46f59dca27SWei Yang 	struct pci_sriov *iov = dev->sriov;
47f59dca27SWei Yang 
48f59dca27SWei Yang 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
49f59dca27SWei Yang 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
50f59dca27SWei Yang 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
51f59dca27SWei Yang }
52f59dca27SWei Yang 
534449f079SWei Yang /*
544449f079SWei Yang  * The PF consumes one bus number.  NumVFs, First VF Offset, and VF Stride
554449f079SWei Yang  * determine how many additional bus numbers will be consumed by VFs.
564449f079SWei Yang  *
57*ea9a8854SAlexander Duyck  * Iterate over all valid NumVFs, validate offset and stride, and calculate
58*ea9a8854SAlexander Duyck  * the maximum number of bus numbers that could ever be required.
594449f079SWei Yang  */
60*ea9a8854SAlexander Duyck static int compute_max_vf_buses(struct pci_dev *dev)
614449f079SWei Yang {
624449f079SWei Yang 	struct pci_sriov *iov = dev->sriov;
63*ea9a8854SAlexander Duyck 	int nr_virtfn, busnr, rc = 0;
644449f079SWei Yang 
65*ea9a8854SAlexander Duyck 	for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) {
664449f079SWei Yang 		pci_iov_set_numvfs(dev, nr_virtfn);
67*ea9a8854SAlexander Duyck 		if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) {
68*ea9a8854SAlexander Duyck 			rc = -EIO;
69*ea9a8854SAlexander Duyck 			goto out;
704449f079SWei Yang 		}
714449f079SWei Yang 
72*ea9a8854SAlexander Duyck 		busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
73*ea9a8854SAlexander Duyck 		if (busnr > iov->max_VF_buses)
74*ea9a8854SAlexander Duyck 			iov->max_VF_buses = busnr;
75*ea9a8854SAlexander Duyck 	}
76*ea9a8854SAlexander Duyck 
77*ea9a8854SAlexander Duyck out:
78*ea9a8854SAlexander Duyck 	pci_iov_set_numvfs(dev, 0);
79*ea9a8854SAlexander Duyck 	return rc;
804449f079SWei Yang }
814449f079SWei Yang 
82dd7cc44dSYu Zhao static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
83dd7cc44dSYu Zhao {
84dd7cc44dSYu Zhao 	struct pci_bus *child;
85dd7cc44dSYu Zhao 
86dd7cc44dSYu Zhao 	if (bus->number == busnr)
87dd7cc44dSYu Zhao 		return bus;
88dd7cc44dSYu Zhao 
89dd7cc44dSYu Zhao 	child = pci_find_bus(pci_domain_nr(bus), busnr);
90dd7cc44dSYu Zhao 	if (child)
91dd7cc44dSYu Zhao 		return child;
92dd7cc44dSYu Zhao 
93dd7cc44dSYu Zhao 	child = pci_add_new_bus(bus, NULL, busnr);
94dd7cc44dSYu Zhao 	if (!child)
95dd7cc44dSYu Zhao 		return NULL;
96dd7cc44dSYu Zhao 
97b7eac055SYinghai Lu 	pci_bus_insert_busn_res(child, busnr, busnr);
98dd7cc44dSYu Zhao 
99dd7cc44dSYu Zhao 	return child;
100dd7cc44dSYu Zhao }
101dd7cc44dSYu Zhao 
102dc087f2fSJiang Liu static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
103dd7cc44dSYu Zhao {
104dc087f2fSJiang Liu 	if (physbus != virtbus && list_empty(&virtbus->devices))
105dc087f2fSJiang Liu 		pci_remove_bus(virtbus);
106dd7cc44dSYu Zhao }
107dd7cc44dSYu Zhao 
1080e6c9122SWei Yang resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
1090e6c9122SWei Yang {
1100e6c9122SWei Yang 	if (!dev->is_physfn)
1110e6c9122SWei Yang 		return 0;
1120e6c9122SWei Yang 
1130e6c9122SWei Yang 	return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
1140e6c9122SWei Yang }
1150e6c9122SWei Yang 
116dd7cc44dSYu Zhao static int virtfn_add(struct pci_dev *dev, int id, int reset)
117dd7cc44dSYu Zhao {
118dd7cc44dSYu Zhao 	int i;
119dc087f2fSJiang Liu 	int rc = -ENOMEM;
120dd7cc44dSYu Zhao 	u64 size;
121dd7cc44dSYu Zhao 	char buf[VIRTFN_ID_LEN];
122dd7cc44dSYu Zhao 	struct pci_dev *virtfn;
123dd7cc44dSYu Zhao 	struct resource *res;
124dd7cc44dSYu Zhao 	struct pci_sriov *iov = dev->sriov;
1258b1fce04SGu Zheng 	struct pci_bus *bus;
126dd7cc44dSYu Zhao 
127dd7cc44dSYu Zhao 	mutex_lock(&iov->dev->sriov->lock);
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;
138dd7cc44dSYu Zhao 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device);
139dd7cc44dSYu Zhao 	pci_setup_device(virtfn);
140dd7cc44dSYu Zhao 	virtfn->dev.parent = dev->dev.parent;
141fbf33f51SXudong Hao 	virtfn->physfn = pci_dev_get(dev);
142fbf33f51SXudong Hao 	virtfn->is_virtfn = 1;
143aa931977SAlex Williamson 	virtfn->multifunction = 0;
144dd7cc44dSYu Zhao 
145dd7cc44dSYu Zhao 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
146c1fe1f96SBjorn Helgaas 		res = &dev->resource[i + PCI_IOV_RESOURCES];
147dd7cc44dSYu Zhao 		if (!res->parent)
148dd7cc44dSYu Zhao 			continue;
149dd7cc44dSYu Zhao 		virtfn->resource[i].name = pci_name(virtfn);
150dd7cc44dSYu Zhao 		virtfn->resource[i].flags = res->flags;
1510e6c9122SWei Yang 		size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
152dd7cc44dSYu Zhao 		virtfn->resource[i].start = res->start + size * id;
153dd7cc44dSYu Zhao 		virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
154dd7cc44dSYu Zhao 		rc = request_resource(res, &virtfn->resource[i]);
155dd7cc44dSYu Zhao 		BUG_ON(rc);
156dd7cc44dSYu Zhao 	}
157dd7cc44dSYu Zhao 
158dd7cc44dSYu Zhao 	if (reset)
1598c1c699fSYu Zhao 		__pci_reset_function(virtfn);
160dd7cc44dSYu Zhao 
161dd7cc44dSYu Zhao 	pci_device_add(virtfn, virtfn->bus);
162dd7cc44dSYu Zhao 	mutex_unlock(&iov->dev->sriov->lock);
163dd7cc44dSYu Zhao 
164c893d133SYijing Wang 	pci_bus_add_device(virtfn);
165dd7cc44dSYu Zhao 	sprintf(buf, "virtfn%u", id);
166dd7cc44dSYu Zhao 	rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
167dd7cc44dSYu Zhao 	if (rc)
168dd7cc44dSYu Zhao 		goto failed1;
169dd7cc44dSYu Zhao 	rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
170dd7cc44dSYu Zhao 	if (rc)
171dd7cc44dSYu Zhao 		goto failed2;
172dd7cc44dSYu Zhao 
173dd7cc44dSYu Zhao 	kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
174dd7cc44dSYu Zhao 
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);
181dd7cc44dSYu Zhao 	mutex_lock(&iov->dev->sriov->lock);
182210647afSYinghai Lu 	pci_stop_and_remove_bus_device(virtfn);
183dc087f2fSJiang Liu failed0:
184dc087f2fSJiang Liu 	virtfn_remove_bus(dev->bus, bus);
185dc087f2fSJiang Liu failed:
186dd7cc44dSYu Zhao 	mutex_unlock(&iov->dev->sriov->lock);
187dd7cc44dSYu Zhao 
188dd7cc44dSYu Zhao 	return rc;
189dd7cc44dSYu Zhao }
190dd7cc44dSYu Zhao 
191dd7cc44dSYu Zhao static void virtfn_remove(struct pci_dev *dev, int id, int reset)
192dd7cc44dSYu Zhao {
193dd7cc44dSYu Zhao 	char buf[VIRTFN_ID_LEN];
194dd7cc44dSYu Zhao 	struct pci_dev *virtfn;
195dd7cc44dSYu Zhao 	struct pci_sriov *iov = dev->sriov;
196dd7cc44dSYu Zhao 
197dc087f2fSJiang Liu 	virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
198b07579c0SWei Yang 					     pci_iov_virtfn_bus(dev, id),
199b07579c0SWei Yang 					     pci_iov_virtfn_devfn(dev, id));
200dd7cc44dSYu Zhao 	if (!virtfn)
201dd7cc44dSYu Zhao 		return;
202dd7cc44dSYu Zhao 
203dd7cc44dSYu Zhao 	if (reset) {
204dd7cc44dSYu Zhao 		device_release_driver(&virtfn->dev);
2058c1c699fSYu Zhao 		__pci_reset_function(virtfn);
206dd7cc44dSYu Zhao 	}
207dd7cc44dSYu Zhao 
208dd7cc44dSYu Zhao 	sprintf(buf, "virtfn%u", id);
209dd7cc44dSYu Zhao 	sysfs_remove_link(&dev->dev.kobj, buf);
21009cedbefSYinghai Lu 	/*
21109cedbefSYinghai Lu 	 * pci_stop_dev() could have been called for this virtfn already,
21209cedbefSYinghai Lu 	 * so the directory for the virtfn may have been removed before.
21309cedbefSYinghai Lu 	 * Double check to avoid spurious sysfs warnings.
21409cedbefSYinghai Lu 	 */
21509cedbefSYinghai Lu 	if (virtfn->dev.kobj.sd)
216dd7cc44dSYu Zhao 		sysfs_remove_link(&virtfn->dev.kobj, "physfn");
217dd7cc44dSYu Zhao 
218dd7cc44dSYu Zhao 	mutex_lock(&iov->dev->sriov->lock);
219210647afSYinghai Lu 	pci_stop_and_remove_bus_device(virtfn);
220dc087f2fSJiang Liu 	virtfn_remove_bus(dev->bus, virtfn->bus);
221dd7cc44dSYu Zhao 	mutex_unlock(&iov->dev->sriov->lock);
222dd7cc44dSYu Zhao 
223dc087f2fSJiang Liu 	/* balance pci_get_domain_bus_and_slot() */
224dc087f2fSJiang Liu 	pci_dev_put(virtfn);
225dd7cc44dSYu Zhao 	pci_dev_put(dev);
226dd7cc44dSYu Zhao }
227dd7cc44dSYu Zhao 
228995df527SWei Yang int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
229995df527SWei Yang {
230995df527SWei Yang        return 0;
231995df527SWei Yang }
232995df527SWei Yang 
233dd7cc44dSYu Zhao static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
234dd7cc44dSYu Zhao {
235dd7cc44dSYu Zhao 	int rc;
236dd7cc44dSYu Zhao 	int i, j;
237dd7cc44dSYu Zhao 	int nres;
238dd7cc44dSYu Zhao 	u16 offset, stride, initial;
239dd7cc44dSYu Zhao 	struct resource *res;
240dd7cc44dSYu Zhao 	struct pci_dev *pdev;
241dd7cc44dSYu Zhao 	struct pci_sriov *iov = dev->sriov;
242bbef98abSRam Pai 	int bars = 0;
243b07579c0SWei Yang 	int bus;
244995df527SWei Yang 	int retval;
245dd7cc44dSYu Zhao 
246dd7cc44dSYu Zhao 	if (!nr_virtfn)
247dd7cc44dSYu Zhao 		return 0;
248dd7cc44dSYu Zhao 
2496b136724SBjorn Helgaas 	if (iov->num_VFs)
250dd7cc44dSYu Zhao 		return -EINVAL;
251dd7cc44dSYu Zhao 
252dd7cc44dSYu Zhao 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
2536b136724SBjorn Helgaas 	if (initial > iov->total_VFs ||
2546b136724SBjorn Helgaas 	    (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
255dd7cc44dSYu Zhao 		return -EIO;
256dd7cc44dSYu Zhao 
2576b136724SBjorn Helgaas 	if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
258dd7cc44dSYu Zhao 	    (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
259dd7cc44dSYu Zhao 		return -EINVAL;
260dd7cc44dSYu Zhao 
261dd7cc44dSYu Zhao 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset);
262dd7cc44dSYu Zhao 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride);
263dd7cc44dSYu Zhao 	if (!offset || (nr_virtfn > 1 && !stride))
264dd7cc44dSYu Zhao 		return -EIO;
265dd7cc44dSYu Zhao 
266dd7cc44dSYu Zhao 	nres = 0;
267dd7cc44dSYu Zhao 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
268bbef98abSRam Pai 		bars |= (1 << (i + PCI_IOV_RESOURCES));
269c1fe1f96SBjorn Helgaas 		res = &dev->resource[i + PCI_IOV_RESOURCES];
270dd7cc44dSYu Zhao 		if (res->parent)
271dd7cc44dSYu Zhao 			nres++;
272dd7cc44dSYu Zhao 	}
273dd7cc44dSYu Zhao 	if (nres != iov->nres) {
274dd7cc44dSYu Zhao 		dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
275dd7cc44dSYu Zhao 		return -ENOMEM;
276dd7cc44dSYu Zhao 	}
277dd7cc44dSYu Zhao 
278dd7cc44dSYu Zhao 	iov->offset = offset;
279dd7cc44dSYu Zhao 	iov->stride = stride;
280dd7cc44dSYu Zhao 
281b07579c0SWei Yang 	bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
28268f8e9faSBjorn Helgaas 	if (bus > dev->bus->busn_res.end) {
28368f8e9faSBjorn Helgaas 		dev_err(&dev->dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
28468f8e9faSBjorn Helgaas 			nr_virtfn, bus, &dev->bus->busn_res);
285dd7cc44dSYu Zhao 		return -ENOMEM;
286dd7cc44dSYu Zhao 	}
287dd7cc44dSYu Zhao 
288bbef98abSRam Pai 	if (pci_enable_resources(dev, bars)) {
289bbef98abSRam Pai 		dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
290bbef98abSRam Pai 		return -ENOMEM;
291bbef98abSRam Pai 	}
292bbef98abSRam Pai 
293dd7cc44dSYu Zhao 	if (iov->link != dev->devfn) {
294dd7cc44dSYu Zhao 		pdev = pci_get_slot(dev->bus, iov->link);
295dd7cc44dSYu Zhao 		if (!pdev)
296dd7cc44dSYu Zhao 			return -ENODEV;
297dd7cc44dSYu Zhao 
298dc087f2fSJiang Liu 		if (!pdev->is_physfn) {
299dd7cc44dSYu Zhao 			pci_dev_put(pdev);
300652d1100SStefan Assmann 			return -ENOSYS;
301dc087f2fSJiang Liu 		}
302dd7cc44dSYu Zhao 
303dd7cc44dSYu Zhao 		rc = sysfs_create_link(&dev->dev.kobj,
304dd7cc44dSYu Zhao 					&pdev->dev.kobj, "dep_link");
305dc087f2fSJiang Liu 		pci_dev_put(pdev);
306dd7cc44dSYu Zhao 		if (rc)
307dd7cc44dSYu Zhao 			return rc;
308dd7cc44dSYu Zhao 	}
309dd7cc44dSYu Zhao 
310f59dca27SWei Yang 	pci_iov_set_numvfs(dev, nr_virtfn);
311dd7cc44dSYu Zhao 	iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
312fb51ccbfSJan Kiszka 	pci_cfg_access_lock(dev);
313dd7cc44dSYu Zhao 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
314dd7cc44dSYu Zhao 	msleep(100);
315fb51ccbfSJan Kiszka 	pci_cfg_access_unlock(dev);
316dd7cc44dSYu Zhao 
3176b136724SBjorn Helgaas 	iov->initial_VFs = initial;
318dd7cc44dSYu Zhao 	if (nr_virtfn < initial)
319dd7cc44dSYu Zhao 		initial = nr_virtfn;
320dd7cc44dSYu Zhao 
321995df527SWei Yang 	if ((retval = pcibios_sriov_enable(dev, initial))) {
322995df527SWei Yang 		dev_err(&dev->dev, "failure %d from pcibios_sriov_enable()\n",
323995df527SWei Yang 			retval);
324995df527SWei Yang 		return retval;
325995df527SWei Yang 	}
326995df527SWei Yang 
327dd7cc44dSYu Zhao 	for (i = 0; i < initial; i++) {
328dd7cc44dSYu Zhao 		rc = virtfn_add(dev, i, 0);
329dd7cc44dSYu Zhao 		if (rc)
330dd7cc44dSYu Zhao 			goto failed;
331dd7cc44dSYu Zhao 	}
332dd7cc44dSYu Zhao 
333dd7cc44dSYu Zhao 	kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
3346b136724SBjorn Helgaas 	iov->num_VFs = nr_virtfn;
335dd7cc44dSYu Zhao 
336dd7cc44dSYu Zhao 	return 0;
337dd7cc44dSYu Zhao 
338dd7cc44dSYu Zhao failed:
339dd7cc44dSYu Zhao 	for (j = 0; j < i; j++)
340dd7cc44dSYu Zhao 		virtfn_remove(dev, j, 0);
341dd7cc44dSYu Zhao 
342dd7cc44dSYu Zhao 	iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
343fb51ccbfSJan Kiszka 	pci_cfg_access_lock(dev);
344dd7cc44dSYu Zhao 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
345f59dca27SWei Yang 	pci_iov_set_numvfs(dev, 0);
346dd7cc44dSYu Zhao 	ssleep(1);
347fb51ccbfSJan Kiszka 	pci_cfg_access_unlock(dev);
348dd7cc44dSYu Zhao 
349dd7cc44dSYu Zhao 	if (iov->link != dev->devfn)
350dd7cc44dSYu Zhao 		sysfs_remove_link(&dev->dev.kobj, "dep_link");
351dd7cc44dSYu Zhao 
352dd7cc44dSYu Zhao 	return rc;
353dd7cc44dSYu Zhao }
354dd7cc44dSYu Zhao 
355995df527SWei Yang int __weak pcibios_sriov_disable(struct pci_dev *pdev)
356995df527SWei Yang {
357995df527SWei Yang        return 0;
358995df527SWei Yang }
359995df527SWei Yang 
360dd7cc44dSYu Zhao static void sriov_disable(struct pci_dev *dev)
361dd7cc44dSYu Zhao {
362dd7cc44dSYu Zhao 	int i;
363dd7cc44dSYu Zhao 	struct pci_sriov *iov = dev->sriov;
364dd7cc44dSYu Zhao 
3656b136724SBjorn Helgaas 	if (!iov->num_VFs)
366dd7cc44dSYu Zhao 		return;
367dd7cc44dSYu Zhao 
3686b136724SBjorn Helgaas 	for (i = 0; i < iov->num_VFs; i++)
369dd7cc44dSYu Zhao 		virtfn_remove(dev, i, 0);
370dd7cc44dSYu Zhao 
371995df527SWei Yang 	pcibios_sriov_disable(dev);
372995df527SWei Yang 
373dd7cc44dSYu Zhao 	iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
374fb51ccbfSJan Kiszka 	pci_cfg_access_lock(dev);
375dd7cc44dSYu Zhao 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
376dd7cc44dSYu Zhao 	ssleep(1);
377fb51ccbfSJan Kiszka 	pci_cfg_access_unlock(dev);
378dd7cc44dSYu Zhao 
379dd7cc44dSYu Zhao 	if (iov->link != dev->devfn)
380dd7cc44dSYu Zhao 		sysfs_remove_link(&dev->dev.kobj, "dep_link");
381dd7cc44dSYu Zhao 
3826b136724SBjorn Helgaas 	iov->num_VFs = 0;
383f59dca27SWei Yang 	pci_iov_set_numvfs(dev, 0);
384dd7cc44dSYu Zhao }
385dd7cc44dSYu Zhao 
386d1b054daSYu Zhao static int sriov_init(struct pci_dev *dev, int pos)
387d1b054daSYu Zhao {
3880e6c9122SWei Yang 	int i, bar64;
389d1b054daSYu Zhao 	int rc;
390d1b054daSYu Zhao 	int nres;
391d1b054daSYu Zhao 	u32 pgsz;
392*ea9a8854SAlexander Duyck 	u16 ctrl, total;
393d1b054daSYu Zhao 	struct pci_sriov *iov;
394d1b054daSYu Zhao 	struct resource *res;
395d1b054daSYu Zhao 	struct pci_dev *pdev;
396d1b054daSYu Zhao 
39762f87c0eSYijing Wang 	if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
39862f87c0eSYijing Wang 	    pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
399d1b054daSYu Zhao 		return -ENODEV;
400d1b054daSYu Zhao 
401d1b054daSYu Zhao 	pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
402d1b054daSYu Zhao 	if (ctrl & PCI_SRIOV_CTRL_VFE) {
403d1b054daSYu Zhao 		pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
404d1b054daSYu Zhao 		ssleep(1);
405d1b054daSYu Zhao 	}
406d1b054daSYu Zhao 
407d1b054daSYu Zhao 	ctrl = 0;
408d1b054daSYu Zhao 	list_for_each_entry(pdev, &dev->bus->devices, bus_list)
409d1b054daSYu Zhao 		if (pdev->is_physfn)
410d1b054daSYu Zhao 			goto found;
411d1b054daSYu Zhao 
412d1b054daSYu Zhao 	pdev = NULL;
413d1b054daSYu Zhao 	if (pci_ari_enabled(dev->bus))
414d1b054daSYu Zhao 		ctrl |= PCI_SRIOV_CTRL_ARI;
415d1b054daSYu Zhao 
416d1b054daSYu Zhao found:
417d1b054daSYu Zhao 	pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
418d1b054daSYu Zhao 
419ff45f9ddSBen Shelton 	pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
420ff45f9ddSBen Shelton 	if (!total)
421ff45f9ddSBen Shelton 		return 0;
422ff45f9ddSBen Shelton 
423d1b054daSYu Zhao 	pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
424d1b054daSYu Zhao 	i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
425d1b054daSYu Zhao 	pgsz &= ~((1 << i) - 1);
426d1b054daSYu Zhao 	if (!pgsz)
427d1b054daSYu Zhao 		return -EIO;
428d1b054daSYu Zhao 
429d1b054daSYu Zhao 	pgsz &= ~(pgsz - 1);
4308161fe91SVaidyanathan Srinivasan 	pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
431d1b054daSYu Zhao 
4320e6c9122SWei Yang 	iov = kzalloc(sizeof(*iov), GFP_KERNEL);
4330e6c9122SWei Yang 	if (!iov)
4340e6c9122SWei Yang 		return -ENOMEM;
4350e6c9122SWei Yang 
436d1b054daSYu Zhao 	nres = 0;
437d1b054daSYu Zhao 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
438c1fe1f96SBjorn Helgaas 		res = &dev->resource[i + PCI_IOV_RESOURCES];
4390e6c9122SWei Yang 		bar64 = __pci_read_base(dev, pci_bar_unknown, res,
440d1b054daSYu Zhao 					pos + PCI_SRIOV_BAR + i * 4);
441d1b054daSYu Zhao 		if (!res->flags)
442d1b054daSYu Zhao 			continue;
443d1b054daSYu Zhao 		if (resource_size(res) & (PAGE_SIZE - 1)) {
444d1b054daSYu Zhao 			rc = -EIO;
445d1b054daSYu Zhao 			goto failed;
446d1b054daSYu Zhao 		}
4470e6c9122SWei Yang 		iov->barsz[i] = resource_size(res);
448d1b054daSYu Zhao 		res->end = res->start + resource_size(res) * total - 1;
449e88ae01dSWei Yang 		dev_info(&dev->dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
450e88ae01dSWei Yang 			 i, res, i, total);
4510e6c9122SWei Yang 		i += bar64;
452d1b054daSYu Zhao 		nres++;
453d1b054daSYu Zhao 	}
454d1b054daSYu Zhao 
455d1b054daSYu Zhao 	iov->pos = pos;
456d1b054daSYu Zhao 	iov->nres = nres;
457d1b054daSYu Zhao 	iov->ctrl = ctrl;
4586b136724SBjorn Helgaas 	iov->total_VFs = total;
459d1b054daSYu Zhao 	iov->pgsz = pgsz;
460d1b054daSYu Zhao 	iov->self = dev;
461d1b054daSYu Zhao 	pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
462d1b054daSYu Zhao 	pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
46362f87c0eSYijing Wang 	if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
4644d135dbeSYu Zhao 		iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
465d1b054daSYu Zhao 
466d1b054daSYu Zhao 	if (pdev)
467d1b054daSYu Zhao 		iov->dev = pci_dev_get(pdev);
468e277d2fcSYu Zhao 	else
469d1b054daSYu Zhao 		iov->dev = dev;
470e277d2fcSYu Zhao 
471d1b054daSYu Zhao 	mutex_init(&iov->lock);
472d1b054daSYu Zhao 
473d1b054daSYu Zhao 	dev->sriov = iov;
474d1b054daSYu Zhao 	dev->is_physfn = 1;
475*ea9a8854SAlexander Duyck 	rc = compute_max_vf_buses(dev);
476*ea9a8854SAlexander Duyck 	if (rc)
477*ea9a8854SAlexander Duyck 		goto fail_max_buses;
478d1b054daSYu Zhao 
479d1b054daSYu Zhao 	return 0;
480d1b054daSYu Zhao 
481*ea9a8854SAlexander Duyck fail_max_buses:
482*ea9a8854SAlexander Duyck 	dev->sriov = NULL;
483*ea9a8854SAlexander Duyck 	dev->is_physfn = 0;
484d1b054daSYu Zhao failed:
485d1b054daSYu Zhao 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
486c1fe1f96SBjorn Helgaas 		res = &dev->resource[i + PCI_IOV_RESOURCES];
487d1b054daSYu Zhao 		res->flags = 0;
488d1b054daSYu Zhao 	}
489d1b054daSYu Zhao 
4900e6c9122SWei Yang 	kfree(iov);
491d1b054daSYu Zhao 	return rc;
492d1b054daSYu Zhao }
493d1b054daSYu Zhao 
494d1b054daSYu Zhao static void sriov_release(struct pci_dev *dev)
495d1b054daSYu Zhao {
4966b136724SBjorn Helgaas 	BUG_ON(dev->sriov->num_VFs);
497dd7cc44dSYu Zhao 
498e277d2fcSYu Zhao 	if (dev != dev->sriov->dev)
499d1b054daSYu Zhao 		pci_dev_put(dev->sriov->dev);
500d1b054daSYu Zhao 
501e277d2fcSYu Zhao 	mutex_destroy(&dev->sriov->lock);
502e277d2fcSYu Zhao 
503d1b054daSYu Zhao 	kfree(dev->sriov);
504d1b054daSYu Zhao 	dev->sriov = NULL;
505d1b054daSYu Zhao }
506d1b054daSYu Zhao 
5078c5cdb6aSYu Zhao static void sriov_restore_state(struct pci_dev *dev)
5088c5cdb6aSYu Zhao {
5098c5cdb6aSYu Zhao 	int i;
5108c5cdb6aSYu Zhao 	u16 ctrl;
5118c5cdb6aSYu Zhao 	struct pci_sriov *iov = dev->sriov;
5128c5cdb6aSYu Zhao 
5138c5cdb6aSYu Zhao 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
5148c5cdb6aSYu Zhao 	if (ctrl & PCI_SRIOV_CTRL_VFE)
5158c5cdb6aSYu Zhao 		return;
5168c5cdb6aSYu Zhao 
5178c5cdb6aSYu Zhao 	for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
5188c5cdb6aSYu Zhao 		pci_update_resource(dev, i);
5198c5cdb6aSYu Zhao 
5208c5cdb6aSYu Zhao 	pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
521f59dca27SWei Yang 	pci_iov_set_numvfs(dev, iov->num_VFs);
5228c5cdb6aSYu Zhao 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
5238c5cdb6aSYu Zhao 	if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
5248c5cdb6aSYu Zhao 		msleep(100);
5258c5cdb6aSYu Zhao }
5268c5cdb6aSYu Zhao 
527d1b054daSYu Zhao /**
528d1b054daSYu Zhao  * pci_iov_init - initialize the IOV capability
529d1b054daSYu Zhao  * @dev: the PCI device
530d1b054daSYu Zhao  *
531d1b054daSYu Zhao  * Returns 0 on success, or negative on failure.
532d1b054daSYu Zhao  */
533d1b054daSYu Zhao int pci_iov_init(struct pci_dev *dev)
534d1b054daSYu Zhao {
535d1b054daSYu Zhao 	int pos;
536d1b054daSYu Zhao 
5375f4d91a1SKenji Kaneshige 	if (!pci_is_pcie(dev))
538d1b054daSYu Zhao 		return -ENODEV;
539d1b054daSYu Zhao 
540d1b054daSYu Zhao 	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
541d1b054daSYu Zhao 	if (pos)
542d1b054daSYu Zhao 		return sriov_init(dev, pos);
543d1b054daSYu Zhao 
544d1b054daSYu Zhao 	return -ENODEV;
545d1b054daSYu Zhao }
546d1b054daSYu Zhao 
547d1b054daSYu Zhao /**
548d1b054daSYu Zhao  * pci_iov_release - release resources used by the IOV capability
549d1b054daSYu Zhao  * @dev: the PCI device
550d1b054daSYu Zhao  */
551d1b054daSYu Zhao void pci_iov_release(struct pci_dev *dev)
552d1b054daSYu Zhao {
553d1b054daSYu Zhao 	if (dev->is_physfn)
554d1b054daSYu Zhao 		sriov_release(dev);
555d1b054daSYu Zhao }
556d1b054daSYu Zhao 
557d1b054daSYu Zhao /**
558d1b054daSYu Zhao  * pci_iov_resource_bar - get position of the SR-IOV BAR
559d1b054daSYu Zhao  * @dev: the PCI device
560d1b054daSYu Zhao  * @resno: the resource number
561d1b054daSYu Zhao  *
562d1b054daSYu Zhao  * Returns position of the BAR encapsulated in the SR-IOV capability.
563d1b054daSYu Zhao  */
56426ff46c6SMyron Stowe int pci_iov_resource_bar(struct pci_dev *dev, int resno)
565d1b054daSYu Zhao {
566d1b054daSYu Zhao 	if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
567d1b054daSYu Zhao 		return 0;
568d1b054daSYu Zhao 
569d1b054daSYu Zhao 	BUG_ON(!dev->is_physfn);
570d1b054daSYu Zhao 
571d1b054daSYu Zhao 	return dev->sriov->pos + PCI_SRIOV_BAR +
572d1b054daSYu Zhao 		4 * (resno - PCI_IOV_RESOURCES);
573d1b054daSYu Zhao }
5748c5cdb6aSYu Zhao 
575978d2d68SWei Yang resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
576978d2d68SWei Yang 						      int resno)
577978d2d68SWei Yang {
578978d2d68SWei Yang 	return pci_iov_resource_size(dev, resno);
579978d2d68SWei Yang }
580978d2d68SWei Yang 
5818c5cdb6aSYu Zhao /**
5826faf17f6SChris Wright  * pci_sriov_resource_alignment - get resource alignment for VF BAR
5836faf17f6SChris Wright  * @dev: the PCI device
5846faf17f6SChris Wright  * @resno: the resource number
5856faf17f6SChris Wright  *
5866faf17f6SChris Wright  * Returns the alignment of the VF BAR found in the SR-IOV capability.
5876faf17f6SChris Wright  * This is not the same as the resource size which is defined as
5886faf17f6SChris Wright  * the VF BAR size multiplied by the number of VFs.  The alignment
5896faf17f6SChris Wright  * is just the VF BAR size.
5906faf17f6SChris Wright  */
5910e52247aSCam Macdonell resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
5926faf17f6SChris Wright {
593978d2d68SWei Yang 	return pcibios_iov_resource_alignment(dev, resno);
5946faf17f6SChris Wright }
5956faf17f6SChris Wright 
5966faf17f6SChris Wright /**
5978c5cdb6aSYu Zhao  * pci_restore_iov_state - restore the state of the IOV capability
5988c5cdb6aSYu Zhao  * @dev: the PCI device
5998c5cdb6aSYu Zhao  */
6008c5cdb6aSYu Zhao void pci_restore_iov_state(struct pci_dev *dev)
6018c5cdb6aSYu Zhao {
6028c5cdb6aSYu Zhao 	if (dev->is_physfn)
6038c5cdb6aSYu Zhao 		sriov_restore_state(dev);
6048c5cdb6aSYu Zhao }
605a28724b0SYu Zhao 
606a28724b0SYu Zhao /**
607a28724b0SYu Zhao  * pci_iov_bus_range - find bus range used by Virtual Function
608a28724b0SYu Zhao  * @bus: the PCI bus
609a28724b0SYu Zhao  *
610a28724b0SYu Zhao  * Returns max number of buses (exclude current one) used by Virtual
611a28724b0SYu Zhao  * Functions.
612a28724b0SYu Zhao  */
613a28724b0SYu Zhao int pci_iov_bus_range(struct pci_bus *bus)
614a28724b0SYu Zhao {
615a28724b0SYu Zhao 	int max = 0;
616a28724b0SYu Zhao 	struct pci_dev *dev;
617a28724b0SYu Zhao 
618a28724b0SYu Zhao 	list_for_each_entry(dev, &bus->devices, bus_list) {
619a28724b0SYu Zhao 		if (!dev->is_physfn)
620a28724b0SYu Zhao 			continue;
6214449f079SWei Yang 		if (dev->sriov->max_VF_buses > max)
6224449f079SWei Yang 			max = dev->sriov->max_VF_buses;
623a28724b0SYu Zhao 	}
624a28724b0SYu Zhao 
625a28724b0SYu Zhao 	return max ? max - bus->number : 0;
626a28724b0SYu Zhao }
627dd7cc44dSYu Zhao 
628dd7cc44dSYu Zhao /**
629dd7cc44dSYu Zhao  * pci_enable_sriov - enable the SR-IOV capability
630dd7cc44dSYu Zhao  * @dev: the PCI device
63152a8873bSRandy Dunlap  * @nr_virtfn: number of virtual functions to enable
632dd7cc44dSYu Zhao  *
633dd7cc44dSYu Zhao  * Returns 0 on success, or negative on failure.
634dd7cc44dSYu Zhao  */
635dd7cc44dSYu Zhao int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
636dd7cc44dSYu Zhao {
637dd7cc44dSYu Zhao 	might_sleep();
638dd7cc44dSYu Zhao 
639dd7cc44dSYu Zhao 	if (!dev->is_physfn)
640652d1100SStefan Assmann 		return -ENOSYS;
641dd7cc44dSYu Zhao 
642dd7cc44dSYu Zhao 	return sriov_enable(dev, nr_virtfn);
643dd7cc44dSYu Zhao }
644dd7cc44dSYu Zhao EXPORT_SYMBOL_GPL(pci_enable_sriov);
645dd7cc44dSYu Zhao 
646dd7cc44dSYu Zhao /**
647dd7cc44dSYu Zhao  * pci_disable_sriov - disable the SR-IOV capability
648dd7cc44dSYu Zhao  * @dev: the PCI device
649dd7cc44dSYu Zhao  */
650dd7cc44dSYu Zhao void pci_disable_sriov(struct pci_dev *dev)
651dd7cc44dSYu Zhao {
652dd7cc44dSYu Zhao 	might_sleep();
653dd7cc44dSYu Zhao 
654dd7cc44dSYu Zhao 	if (!dev->is_physfn)
655dd7cc44dSYu Zhao 		return;
656dd7cc44dSYu Zhao 
657dd7cc44dSYu Zhao 	sriov_disable(dev);
658dd7cc44dSYu Zhao }
659dd7cc44dSYu Zhao EXPORT_SYMBOL_GPL(pci_disable_sriov);
66074bb1bccSYu Zhao 
66174bb1bccSYu Zhao /**
662fb8a0d9dSWilliams, Mitch A  * pci_num_vf - return number of VFs associated with a PF device_release_driver
663fb8a0d9dSWilliams, Mitch A  * @dev: the PCI device
664fb8a0d9dSWilliams, Mitch A  *
665fb8a0d9dSWilliams, Mitch A  * Returns number of VFs, or 0 if SR-IOV is not enabled.
666fb8a0d9dSWilliams, Mitch A  */
667fb8a0d9dSWilliams, Mitch A int pci_num_vf(struct pci_dev *dev)
668fb8a0d9dSWilliams, Mitch A {
6691452cd76SBjorn Helgaas 	if (!dev->is_physfn)
670fb8a0d9dSWilliams, Mitch A 		return 0;
6711452cd76SBjorn Helgaas 
6726b136724SBjorn Helgaas 	return dev->sriov->num_VFs;
673fb8a0d9dSWilliams, Mitch A }
674fb8a0d9dSWilliams, Mitch A EXPORT_SYMBOL_GPL(pci_num_vf);
675bff73156SDonald Dutile 
676bff73156SDonald Dutile /**
6775a8eb242SAlexander Duyck  * pci_vfs_assigned - returns number of VFs are assigned to a guest
6785a8eb242SAlexander Duyck  * @dev: the PCI device
6795a8eb242SAlexander Duyck  *
6805a8eb242SAlexander Duyck  * Returns number of VFs belonging to this device that are assigned to a guest.
681652d1100SStefan Assmann  * If device is not a physical function returns 0.
6825a8eb242SAlexander Duyck  */
6835a8eb242SAlexander Duyck int pci_vfs_assigned(struct pci_dev *dev)
6845a8eb242SAlexander Duyck {
6855a8eb242SAlexander Duyck 	struct pci_dev *vfdev;
6865a8eb242SAlexander Duyck 	unsigned int vfs_assigned = 0;
6875a8eb242SAlexander Duyck 	unsigned short dev_id;
6885a8eb242SAlexander Duyck 
6895a8eb242SAlexander Duyck 	/* only search if we are a PF */
6905a8eb242SAlexander Duyck 	if (!dev->is_physfn)
6915a8eb242SAlexander Duyck 		return 0;
6925a8eb242SAlexander Duyck 
6935a8eb242SAlexander Duyck 	/*
6945a8eb242SAlexander Duyck 	 * determine the device ID for the VFs, the vendor ID will be the
6955a8eb242SAlexander Duyck 	 * same as the PF so there is no need to check for that one
6965a8eb242SAlexander Duyck 	 */
6975a8eb242SAlexander Duyck 	pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id);
6985a8eb242SAlexander Duyck 
6995a8eb242SAlexander Duyck 	/* loop through all the VFs to see if we own any that are assigned */
7005a8eb242SAlexander Duyck 	vfdev = pci_get_device(dev->vendor, dev_id, NULL);
7015a8eb242SAlexander Duyck 	while (vfdev) {
7025a8eb242SAlexander Duyck 		/*
7035a8eb242SAlexander Duyck 		 * It is considered assigned if it is a virtual function with
7045a8eb242SAlexander Duyck 		 * our dev as the physical function and the assigned bit is set
7055a8eb242SAlexander Duyck 		 */
7065a8eb242SAlexander Duyck 		if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
707be63497cSEthan Zhao 			pci_is_dev_assigned(vfdev))
7085a8eb242SAlexander Duyck 			vfs_assigned++;
7095a8eb242SAlexander Duyck 
7105a8eb242SAlexander Duyck 		vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
7115a8eb242SAlexander Duyck 	}
7125a8eb242SAlexander Duyck 
7135a8eb242SAlexander Duyck 	return vfs_assigned;
7145a8eb242SAlexander Duyck }
7155a8eb242SAlexander Duyck EXPORT_SYMBOL_GPL(pci_vfs_assigned);
7165a8eb242SAlexander Duyck 
7175a8eb242SAlexander Duyck /**
718bff73156SDonald Dutile  * pci_sriov_set_totalvfs -- reduce the TotalVFs available
719bff73156SDonald Dutile  * @dev: the PCI PF device
7202094f167SRandy Dunlap  * @numvfs: number that should be used for TotalVFs supported
721bff73156SDonald Dutile  *
722bff73156SDonald Dutile  * Should be called from PF driver's probe routine with
723bff73156SDonald Dutile  * device's mutex held.
724bff73156SDonald Dutile  *
725bff73156SDonald Dutile  * Returns 0 if PF is an SRIOV-capable device and
726652d1100SStefan Assmann  * value of numvfs valid. If not a PF return -ENOSYS;
727652d1100SStefan Assmann  * if numvfs is invalid return -EINVAL;
728bff73156SDonald Dutile  * if VFs already enabled, return -EBUSY.
729bff73156SDonald Dutile  */
730bff73156SDonald Dutile int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
731bff73156SDonald Dutile {
732652d1100SStefan Assmann 	if (!dev->is_physfn)
733652d1100SStefan Assmann 		return -ENOSYS;
734652d1100SStefan Assmann 	if (numvfs > dev->sriov->total_VFs)
735bff73156SDonald Dutile 		return -EINVAL;
736bff73156SDonald Dutile 
737bff73156SDonald Dutile 	/* Shouldn't change if VFs already enabled */
738bff73156SDonald Dutile 	if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
739bff73156SDonald Dutile 		return -EBUSY;
740bff73156SDonald Dutile 	else
7416b136724SBjorn Helgaas 		dev->sriov->driver_max_VFs = numvfs;
742bff73156SDonald Dutile 
743bff73156SDonald Dutile 	return 0;
744bff73156SDonald Dutile }
745bff73156SDonald Dutile EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
746bff73156SDonald Dutile 
747bff73156SDonald Dutile /**
748ddc191f5SJonghwan Choi  * pci_sriov_get_totalvfs -- get total VFs supported on this device
749bff73156SDonald Dutile  * @dev: the PCI PF device
750bff73156SDonald Dutile  *
751bff73156SDonald Dutile  * For a PCIe device with SRIOV support, return the PCIe
7526b136724SBjorn Helgaas  * SRIOV capability value of TotalVFs or the value of driver_max_VFs
753652d1100SStefan Assmann  * if the driver reduced it.  Otherwise 0.
754bff73156SDonald Dutile  */
755bff73156SDonald Dutile int pci_sriov_get_totalvfs(struct pci_dev *dev)
756bff73156SDonald Dutile {
7571452cd76SBjorn Helgaas 	if (!dev->is_physfn)
758652d1100SStefan Assmann 		return 0;
759bff73156SDonald Dutile 
7606b136724SBjorn Helgaas 	if (dev->sriov->driver_max_VFs)
7616b136724SBjorn Helgaas 		return dev->sriov->driver_max_VFs;
7621452cd76SBjorn Helgaas 
7636b136724SBjorn Helgaas 	return dev->sriov->total_VFs;
764bff73156SDonald Dutile }
765bff73156SDonald Dutile EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);
766