xref: /linux/drivers/pci/iov.c (revision 8d85a7a4f2c935013a01964b7e81d5a105bd7a4c)
17328c8f4SBjorn Helgaas // SPDX-License-Identifier: GPL-2.0
2d1b054daSYu Zhao /*
3df62ab5eSBjorn Helgaas  * PCI Express I/O Virtualization (IOV) support
4d1b054daSYu Zhao  *   Single Root IOV 1.0
5302b4215SYu Zhao  *   Address Translation Service 1.0
6df62ab5eSBjorn Helgaas  *
7df62ab5eSBjorn Helgaas  * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
8d1b054daSYu Zhao  */
9d1b054daSYu Zhao 
10d1b054daSYu Zhao #include <linux/pci.h>
115a0e3ad6STejun Heo #include <linux/slab.h>
12d1b054daSYu Zhao #include <linux/mutex.h>
13363c75dbSPaul Gortmaker #include <linux/export.h>
14d1b054daSYu Zhao #include <linux/string.h>
15d1b054daSYu Zhao #include <linux/delay.h>
165cdede24SJoerg Roedel #include <linux/pci-ats.h>
17d1b054daSYu Zhao #include "pci.h"
18d1b054daSYu Zhao 
19dd7cc44dSYu Zhao #define VIRTFN_ID_LEN	16
20d1b054daSYu Zhao 
21b07579c0SWei Yang int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id)
22a28724b0SYu Zhao {
23b07579c0SWei Yang 	if (!dev->is_physfn)
24b07579c0SWei Yang 		return -EINVAL;
25a28724b0SYu Zhao 	return dev->bus->number + ((dev->devfn + dev->sriov->offset +
26b07579c0SWei Yang 				    dev->sriov->stride * vf_id) >> 8);
27a28724b0SYu Zhao }
28a28724b0SYu Zhao 
29b07579c0SWei Yang int pci_iov_virtfn_devfn(struct pci_dev *dev, int vf_id)
30a28724b0SYu Zhao {
31b07579c0SWei Yang 	if (!dev->is_physfn)
32b07579c0SWei Yang 		return -EINVAL;
33a28724b0SYu Zhao 	return (dev->devfn + dev->sriov->offset +
34b07579c0SWei Yang 		dev->sriov->stride * vf_id) & 0xff;
35a28724b0SYu Zhao }
36a28724b0SYu Zhao 
37f59dca27SWei Yang /*
38f59dca27SWei Yang  * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
39f59dca27SWei Yang  * change when NumVFs changes.
40f59dca27SWei Yang  *
41f59dca27SWei Yang  * Update iov->offset and iov->stride when NumVFs is written.
42f59dca27SWei Yang  */
43f59dca27SWei Yang static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
44f59dca27SWei Yang {
45f59dca27SWei Yang 	struct pci_sriov *iov = dev->sriov;
46f59dca27SWei Yang 
47f59dca27SWei Yang 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
48f59dca27SWei Yang 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
49f59dca27SWei Yang 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
50f59dca27SWei Yang }
51f59dca27SWei Yang 
524449f079SWei Yang /*
534449f079SWei Yang  * The PF consumes one bus number.  NumVFs, First VF Offset, and VF Stride
544449f079SWei Yang  * determine how many additional bus numbers will be consumed by VFs.
554449f079SWei Yang  *
56ea9a8854SAlexander Duyck  * Iterate over all valid NumVFs, validate offset and stride, and calculate
57ea9a8854SAlexander Duyck  * the maximum number of bus numbers that could ever be required.
584449f079SWei Yang  */
59ea9a8854SAlexander Duyck static int compute_max_vf_buses(struct pci_dev *dev)
604449f079SWei Yang {
614449f079SWei Yang 	struct pci_sriov *iov = dev->sriov;
62ea9a8854SAlexander Duyck 	int nr_virtfn, busnr, rc = 0;
634449f079SWei Yang 
64ea9a8854SAlexander Duyck 	for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) {
654449f079SWei Yang 		pci_iov_set_numvfs(dev, nr_virtfn);
66ea9a8854SAlexander Duyck 		if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) {
67ea9a8854SAlexander Duyck 			rc = -EIO;
68ea9a8854SAlexander Duyck 			goto out;
694449f079SWei Yang 		}
704449f079SWei Yang 
71ea9a8854SAlexander Duyck 		busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
72ea9a8854SAlexander Duyck 		if (busnr > iov->max_VF_buses)
73ea9a8854SAlexander Duyck 			iov->max_VF_buses = busnr;
74ea9a8854SAlexander Duyck 	}
75ea9a8854SAlexander Duyck 
76ea9a8854SAlexander Duyck out:
77ea9a8854SAlexander Duyck 	pci_iov_set_numvfs(dev, 0);
78ea9a8854SAlexander Duyck 	return rc;
794449f079SWei Yang }
804449f079SWei Yang 
81dd7cc44dSYu Zhao static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
82dd7cc44dSYu Zhao {
83dd7cc44dSYu Zhao 	struct pci_bus *child;
84dd7cc44dSYu Zhao 
85dd7cc44dSYu Zhao 	if (bus->number == busnr)
86dd7cc44dSYu Zhao 		return bus;
87dd7cc44dSYu Zhao 
88dd7cc44dSYu Zhao 	child = pci_find_bus(pci_domain_nr(bus), busnr);
89dd7cc44dSYu Zhao 	if (child)
90dd7cc44dSYu Zhao 		return child;
91dd7cc44dSYu Zhao 
92dd7cc44dSYu Zhao 	child = pci_add_new_bus(bus, NULL, busnr);
93dd7cc44dSYu Zhao 	if (!child)
94dd7cc44dSYu Zhao 		return NULL;
95dd7cc44dSYu Zhao 
96b7eac055SYinghai Lu 	pci_bus_insert_busn_res(child, busnr, busnr);
97dd7cc44dSYu Zhao 
98dd7cc44dSYu Zhao 	return child;
99dd7cc44dSYu Zhao }
100dd7cc44dSYu Zhao 
101dc087f2fSJiang Liu static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
102dd7cc44dSYu Zhao {
103dc087f2fSJiang Liu 	if (physbus != virtbus && list_empty(&virtbus->devices))
104dc087f2fSJiang Liu 		pci_remove_bus(virtbus);
105dd7cc44dSYu Zhao }
106dd7cc44dSYu Zhao 
1070e6c9122SWei Yang resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
1080e6c9122SWei Yang {
1090e6c9122SWei Yang 	if (!dev->is_physfn)
1100e6c9122SWei Yang 		return 0;
1110e6c9122SWei Yang 
1120e6c9122SWei Yang 	return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
1130e6c9122SWei Yang }
1140e6c9122SWei Yang 
115cf0921beSKarimAllah Ahmed static void pci_read_vf_config_common(struct pci_dev *virtfn)
116cf0921beSKarimAllah Ahmed {
117cf0921beSKarimAllah Ahmed 	struct pci_dev *physfn = virtfn->physfn;
118cf0921beSKarimAllah Ahmed 
119cf0921beSKarimAllah Ahmed 	/*
120cf0921beSKarimAllah Ahmed 	 * Some config registers are the same across all associated VFs.
121cf0921beSKarimAllah Ahmed 	 * Read them once from VF0 so we can skip reading them from the
122cf0921beSKarimAllah Ahmed 	 * other VFs.
123cf0921beSKarimAllah Ahmed 	 *
124cf0921beSKarimAllah Ahmed 	 * PCIe r4.0, sec 9.3.4.1, technically doesn't require all VFs to
125cf0921beSKarimAllah Ahmed 	 * have the same Revision ID and Subsystem ID, but we assume they
126cf0921beSKarimAllah Ahmed 	 * do.
127cf0921beSKarimAllah Ahmed 	 */
128cf0921beSKarimAllah Ahmed 	pci_read_config_dword(virtfn, PCI_CLASS_REVISION,
129cf0921beSKarimAllah Ahmed 			      &physfn->sriov->class);
130cf0921beSKarimAllah Ahmed 	pci_read_config_byte(virtfn, PCI_HEADER_TYPE,
131cf0921beSKarimAllah Ahmed 			     &physfn->sriov->hdr_type);
132cf0921beSKarimAllah Ahmed 	pci_read_config_word(virtfn, PCI_SUBSYSTEM_VENDOR_ID,
133cf0921beSKarimAllah Ahmed 			     &physfn->sriov->subsystem_vendor);
134cf0921beSKarimAllah Ahmed 	pci_read_config_word(virtfn, PCI_SUBSYSTEM_ID,
135cf0921beSKarimAllah Ahmed 			     &physfn->sriov->subsystem_device);
136cf0921beSKarimAllah Ahmed }
137cf0921beSKarimAllah Ahmed 
138753f6124SJan H. Schönherr int pci_iov_add_virtfn(struct pci_dev *dev, int id)
139dd7cc44dSYu Zhao {
140dd7cc44dSYu Zhao 	int i;
141dc087f2fSJiang Liu 	int rc = -ENOMEM;
142dd7cc44dSYu Zhao 	u64 size;
143dd7cc44dSYu Zhao 	char buf[VIRTFN_ID_LEN];
144dd7cc44dSYu Zhao 	struct pci_dev *virtfn;
145dd7cc44dSYu Zhao 	struct resource *res;
146dd7cc44dSYu Zhao 	struct pci_sriov *iov = dev->sriov;
1478b1fce04SGu Zheng 	struct pci_bus *bus;
148dd7cc44dSYu Zhao 
149b07579c0SWei Yang 	bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
150dc087f2fSJiang Liu 	if (!bus)
151dc087f2fSJiang Liu 		goto failed;
152dc087f2fSJiang Liu 
153dc087f2fSJiang Liu 	virtfn = pci_alloc_dev(bus);
154dc087f2fSJiang Liu 	if (!virtfn)
155dc087f2fSJiang Liu 		goto failed0;
156dc087f2fSJiang Liu 
157b07579c0SWei Yang 	virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
158dd7cc44dSYu Zhao 	virtfn->vendor = dev->vendor;
1593142d832SFilippo Sironi 	virtfn->device = iov->vf_device;
160cf0921beSKarimAllah Ahmed 	virtfn->is_virtfn = 1;
161cf0921beSKarimAllah Ahmed 	virtfn->physfn = pci_dev_get(dev);
162cf0921beSKarimAllah Ahmed 
163cf0921beSKarimAllah Ahmed 	if (id == 0)
164cf0921beSKarimAllah Ahmed 		pci_read_vf_config_common(virtfn);
165cf0921beSKarimAllah Ahmed 
166156c5532SPo Liu 	rc = pci_setup_device(virtfn);
167156c5532SPo Liu 	if (rc)
168cf0921beSKarimAllah Ahmed 		goto failed1;
169156c5532SPo Liu 
170dd7cc44dSYu Zhao 	virtfn->dev.parent = dev->dev.parent;
171aa931977SAlex Williamson 	virtfn->multifunction = 0;
172dd7cc44dSYu Zhao 
173dd7cc44dSYu Zhao 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
174c1fe1f96SBjorn Helgaas 		res = &dev->resource[i + PCI_IOV_RESOURCES];
175dd7cc44dSYu Zhao 		if (!res->parent)
176dd7cc44dSYu Zhao 			continue;
177dd7cc44dSYu Zhao 		virtfn->resource[i].name = pci_name(virtfn);
178dd7cc44dSYu Zhao 		virtfn->resource[i].flags = res->flags;
1790e6c9122SWei Yang 		size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
180dd7cc44dSYu Zhao 		virtfn->resource[i].start = res->start + size * id;
181dd7cc44dSYu Zhao 		virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
182dd7cc44dSYu Zhao 		rc = request_resource(res, &virtfn->resource[i]);
183dd7cc44dSYu Zhao 		BUG_ON(rc);
184dd7cc44dSYu Zhao 	}
185dd7cc44dSYu Zhao 
186dd7cc44dSYu Zhao 	pci_device_add(virtfn, virtfn->bus);
187dd7cc44dSYu Zhao 
188dd7cc44dSYu Zhao 	sprintf(buf, "virtfn%u", id);
189dd7cc44dSYu Zhao 	rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
190dd7cc44dSYu Zhao 	if (rc)
191cf0921beSKarimAllah Ahmed 		goto failed2;
192dd7cc44dSYu Zhao 	rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
193dd7cc44dSYu Zhao 	if (rc)
194cf0921beSKarimAllah Ahmed 		goto failed3;
195dd7cc44dSYu Zhao 
196dd7cc44dSYu Zhao 	kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
197dd7cc44dSYu Zhao 
19827d61629SStuart Hayes 	pci_bus_add_device(virtfn);
19927d61629SStuart Hayes 
200dd7cc44dSYu Zhao 	return 0;
201dd7cc44dSYu Zhao 
202cf0921beSKarimAllah Ahmed failed3:
203dd7cc44dSYu Zhao 	sysfs_remove_link(&dev->dev.kobj, buf);
204cf0921beSKarimAllah Ahmed failed2:
205cf0921beSKarimAllah Ahmed 	pci_stop_and_remove_bus_device(virtfn);
206dd7cc44dSYu Zhao failed1:
207dd7cc44dSYu Zhao 	pci_dev_put(dev);
208dc087f2fSJiang Liu failed0:
209dc087f2fSJiang Liu 	virtfn_remove_bus(dev->bus, bus);
210dc087f2fSJiang Liu failed:
211dd7cc44dSYu Zhao 
212dd7cc44dSYu Zhao 	return rc;
213dd7cc44dSYu Zhao }
214dd7cc44dSYu Zhao 
215753f6124SJan H. Schönherr void pci_iov_remove_virtfn(struct pci_dev *dev, int id)
216dd7cc44dSYu Zhao {
217dd7cc44dSYu Zhao 	char buf[VIRTFN_ID_LEN];
218dd7cc44dSYu Zhao 	struct pci_dev *virtfn;
219dd7cc44dSYu Zhao 
220dc087f2fSJiang Liu 	virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
221b07579c0SWei Yang 					     pci_iov_virtfn_bus(dev, id),
222b07579c0SWei Yang 					     pci_iov_virtfn_devfn(dev, id));
223dd7cc44dSYu Zhao 	if (!virtfn)
224dd7cc44dSYu Zhao 		return;
225dd7cc44dSYu Zhao 
226dd7cc44dSYu Zhao 	sprintf(buf, "virtfn%u", id);
227dd7cc44dSYu Zhao 	sysfs_remove_link(&dev->dev.kobj, buf);
22809cedbefSYinghai Lu 	/*
22909cedbefSYinghai Lu 	 * pci_stop_dev() could have been called for this virtfn already,
23009cedbefSYinghai Lu 	 * so the directory for the virtfn may have been removed before.
23109cedbefSYinghai Lu 	 * Double check to avoid spurious sysfs warnings.
23209cedbefSYinghai Lu 	 */
23309cedbefSYinghai Lu 	if (virtfn->dev.kobj.sd)
234dd7cc44dSYu Zhao 		sysfs_remove_link(&virtfn->dev.kobj, "physfn");
235dd7cc44dSYu Zhao 
236210647afSYinghai Lu 	pci_stop_and_remove_bus_device(virtfn);
237dc087f2fSJiang Liu 	virtfn_remove_bus(dev->bus, virtfn->bus);
238dd7cc44dSYu Zhao 
239dc087f2fSJiang Liu 	/* balance pci_get_domain_bus_and_slot() */
240dc087f2fSJiang Liu 	pci_dev_put(virtfn);
241dd7cc44dSYu Zhao 	pci_dev_put(dev);
242dd7cc44dSYu Zhao }
243dd7cc44dSYu Zhao 
244995df527SWei Yang int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
245995df527SWei Yang {
246995df527SWei Yang 	return 0;
247995df527SWei Yang }
248995df527SWei Yang 
249a39e3fcdSAlexander Duyck int __weak pcibios_sriov_disable(struct pci_dev *pdev)
250a39e3fcdSAlexander Duyck {
251a39e3fcdSAlexander Duyck 	return 0;
252a39e3fcdSAlexander Duyck }
253a39e3fcdSAlexander Duyck 
254dd7cc44dSYu Zhao static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
255dd7cc44dSYu Zhao {
256dd7cc44dSYu Zhao 	int rc;
2573443c382SAlexander Duyck 	int i;
258dd7cc44dSYu Zhao 	int nres;
259ce288ec3SAlexander Duyck 	u16 initial;
260dd7cc44dSYu Zhao 	struct resource *res;
261dd7cc44dSYu Zhao 	struct pci_dev *pdev;
262dd7cc44dSYu Zhao 	struct pci_sriov *iov = dev->sriov;
263bbef98abSRam Pai 	int bars = 0;
264b07579c0SWei Yang 	int bus;
265dd7cc44dSYu Zhao 
266dd7cc44dSYu Zhao 	if (!nr_virtfn)
267dd7cc44dSYu Zhao 		return 0;
268dd7cc44dSYu Zhao 
2696b136724SBjorn Helgaas 	if (iov->num_VFs)
270dd7cc44dSYu Zhao 		return -EINVAL;
271dd7cc44dSYu Zhao 
272dd7cc44dSYu Zhao 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
2736b136724SBjorn Helgaas 	if (initial > iov->total_VFs ||
2746b136724SBjorn Helgaas 	    (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
275dd7cc44dSYu Zhao 		return -EIO;
276dd7cc44dSYu Zhao 
2776b136724SBjorn Helgaas 	if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
278dd7cc44dSYu Zhao 	    (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
279dd7cc44dSYu Zhao 		return -EINVAL;
280dd7cc44dSYu Zhao 
281dd7cc44dSYu Zhao 	nres = 0;
282dd7cc44dSYu Zhao 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
283bbef98abSRam Pai 		bars |= (1 << (i + PCI_IOV_RESOURCES));
284c1fe1f96SBjorn Helgaas 		res = &dev->resource[i + PCI_IOV_RESOURCES];
285dd7cc44dSYu Zhao 		if (res->parent)
286dd7cc44dSYu Zhao 			nres++;
287dd7cc44dSYu Zhao 	}
288dd7cc44dSYu Zhao 	if (nres != iov->nres) {
2897506dc79SFrederick Lawler 		pci_err(dev, "not enough MMIO resources for SR-IOV\n");
290dd7cc44dSYu Zhao 		return -ENOMEM;
291dd7cc44dSYu Zhao 	}
292dd7cc44dSYu Zhao 
293b07579c0SWei Yang 	bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
29468f8e9faSBjorn Helgaas 	if (bus > dev->bus->busn_res.end) {
2957506dc79SFrederick Lawler 		pci_err(dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
29668f8e9faSBjorn Helgaas 			nr_virtfn, bus, &dev->bus->busn_res);
297dd7cc44dSYu Zhao 		return -ENOMEM;
298dd7cc44dSYu Zhao 	}
299dd7cc44dSYu Zhao 
300bbef98abSRam Pai 	if (pci_enable_resources(dev, bars)) {
3017506dc79SFrederick Lawler 		pci_err(dev, "SR-IOV: IOV BARS not allocated\n");
302bbef98abSRam Pai 		return -ENOMEM;
303bbef98abSRam Pai 	}
304bbef98abSRam Pai 
305dd7cc44dSYu Zhao 	if (iov->link != dev->devfn) {
306dd7cc44dSYu Zhao 		pdev = pci_get_slot(dev->bus, iov->link);
307dd7cc44dSYu Zhao 		if (!pdev)
308dd7cc44dSYu Zhao 			return -ENODEV;
309dd7cc44dSYu Zhao 
310dc087f2fSJiang Liu 		if (!pdev->is_physfn) {
311dd7cc44dSYu Zhao 			pci_dev_put(pdev);
312652d1100SStefan Assmann 			return -ENOSYS;
313dc087f2fSJiang Liu 		}
314dd7cc44dSYu Zhao 
315dd7cc44dSYu Zhao 		rc = sysfs_create_link(&dev->dev.kobj,
316dd7cc44dSYu Zhao 					&pdev->dev.kobj, "dep_link");
317dc087f2fSJiang Liu 		pci_dev_put(pdev);
318dd7cc44dSYu Zhao 		if (rc)
319dd7cc44dSYu Zhao 			return rc;
320dd7cc44dSYu Zhao 	}
321dd7cc44dSYu Zhao 
3226b136724SBjorn Helgaas 	iov->initial_VFs = initial;
323dd7cc44dSYu Zhao 	if (nr_virtfn < initial)
324dd7cc44dSYu Zhao 		initial = nr_virtfn;
325dd7cc44dSYu Zhao 
326c23b6135SAlexander Duyck 	rc = pcibios_sriov_enable(dev, initial);
327c23b6135SAlexander Duyck 	if (rc) {
3287506dc79SFrederick Lawler 		pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc);
329c23b6135SAlexander Duyck 		goto err_pcibios;
330995df527SWei Yang 	}
331995df527SWei Yang 
332f40ec3c7SGavin Shan 	pci_iov_set_numvfs(dev, nr_virtfn);
333f40ec3c7SGavin Shan 	iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
334f40ec3c7SGavin Shan 	pci_cfg_access_lock(dev);
335f40ec3c7SGavin Shan 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
336f40ec3c7SGavin Shan 	msleep(100);
337f40ec3c7SGavin Shan 	pci_cfg_access_unlock(dev);
338f40ec3c7SGavin Shan 
339dd7cc44dSYu Zhao 	for (i = 0; i < initial; i++) {
340753f6124SJan H. Schönherr 		rc = pci_iov_add_virtfn(dev, i);
341dd7cc44dSYu Zhao 		if (rc)
342dd7cc44dSYu Zhao 			goto failed;
343dd7cc44dSYu Zhao 	}
344dd7cc44dSYu Zhao 
345dd7cc44dSYu Zhao 	kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
3466b136724SBjorn Helgaas 	iov->num_VFs = nr_virtfn;
347dd7cc44dSYu Zhao 
348dd7cc44dSYu Zhao 	return 0;
349dd7cc44dSYu Zhao 
350dd7cc44dSYu Zhao failed:
3513443c382SAlexander Duyck 	while (i--)
352753f6124SJan H. Schönherr 		pci_iov_remove_virtfn(dev, i);
353dd7cc44dSYu Zhao 
354c23b6135SAlexander Duyck err_pcibios:
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 
366b3908644SAlexander Duyck 	pci_iov_set_numvfs(dev, 0);
367dd7cc44dSYu Zhao 	return rc;
368dd7cc44dSYu Zhao }
369dd7cc44dSYu Zhao 
370dd7cc44dSYu Zhao static void sriov_disable(struct pci_dev *dev)
371dd7cc44dSYu Zhao {
372dd7cc44dSYu Zhao 	int i;
373dd7cc44dSYu Zhao 	struct pci_sriov *iov = dev->sriov;
374dd7cc44dSYu Zhao 
3756b136724SBjorn Helgaas 	if (!iov->num_VFs)
376dd7cc44dSYu Zhao 		return;
377dd7cc44dSYu Zhao 
3786b136724SBjorn Helgaas 	for (i = 0; i < iov->num_VFs; i++)
379753f6124SJan H. Schönherr 		pci_iov_remove_virtfn(dev, i);
380dd7cc44dSYu Zhao 
381dd7cc44dSYu Zhao 	iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
382fb51ccbfSJan Kiszka 	pci_cfg_access_lock(dev);
383dd7cc44dSYu Zhao 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
384dd7cc44dSYu Zhao 	ssleep(1);
385fb51ccbfSJan Kiszka 	pci_cfg_access_unlock(dev);
386dd7cc44dSYu Zhao 
3870fc690a7SGavin Shan 	pcibios_sriov_disable(dev);
3880fc690a7SGavin Shan 
389dd7cc44dSYu Zhao 	if (iov->link != dev->devfn)
390dd7cc44dSYu Zhao 		sysfs_remove_link(&dev->dev.kobj, "dep_link");
391dd7cc44dSYu Zhao 
3926b136724SBjorn Helgaas 	iov->num_VFs = 0;
393f59dca27SWei Yang 	pci_iov_set_numvfs(dev, 0);
394dd7cc44dSYu Zhao }
395dd7cc44dSYu Zhao 
396d1b054daSYu Zhao static int sriov_init(struct pci_dev *dev, int pos)
397d1b054daSYu Zhao {
3980e6c9122SWei Yang 	int i, bar64;
399d1b054daSYu Zhao 	int rc;
400d1b054daSYu Zhao 	int nres;
401d1b054daSYu Zhao 	u32 pgsz;
402ea9a8854SAlexander Duyck 	u16 ctrl, total;
403d1b054daSYu Zhao 	struct pci_sriov *iov;
404d1b054daSYu Zhao 	struct resource *res;
405d1b054daSYu Zhao 	struct pci_dev *pdev;
406d1b054daSYu Zhao 
407d1b054daSYu Zhao 	pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
408d1b054daSYu Zhao 	if (ctrl & PCI_SRIOV_CTRL_VFE) {
409d1b054daSYu Zhao 		pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
410d1b054daSYu Zhao 		ssleep(1);
411d1b054daSYu Zhao 	}
412d1b054daSYu Zhao 
413d1b054daSYu Zhao 	ctrl = 0;
414d1b054daSYu Zhao 	list_for_each_entry(pdev, &dev->bus->devices, bus_list)
415d1b054daSYu Zhao 		if (pdev->is_physfn)
416d1b054daSYu Zhao 			goto found;
417d1b054daSYu Zhao 
418d1b054daSYu Zhao 	pdev = NULL;
419d1b054daSYu Zhao 	if (pci_ari_enabled(dev->bus))
420d1b054daSYu Zhao 		ctrl |= PCI_SRIOV_CTRL_ARI;
421d1b054daSYu Zhao 
422d1b054daSYu Zhao found:
423d1b054daSYu Zhao 	pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
424d1b054daSYu Zhao 
425ff45f9ddSBen Shelton 	pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
426ff45f9ddSBen Shelton 	if (!total)
427ff45f9ddSBen Shelton 		return 0;
428d1b054daSYu Zhao 
429d1b054daSYu Zhao 	pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
430d1b054daSYu Zhao 	i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
431d1b054daSYu Zhao 	pgsz &= ~((1 << i) - 1);
432d1b054daSYu Zhao 	if (!pgsz)
433d1b054daSYu Zhao 		return -EIO;
434d1b054daSYu Zhao 
435d1b054daSYu Zhao 	pgsz &= ~(pgsz - 1);
4368161fe91SVaidyanathan Srinivasan 	pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
437d1b054daSYu Zhao 
4380e6c9122SWei Yang 	iov = kzalloc(sizeof(*iov), GFP_KERNEL);
4390e6c9122SWei Yang 	if (!iov)
4400e6c9122SWei Yang 		return -ENOMEM;
4410e6c9122SWei Yang 
442d1b054daSYu Zhao 	nres = 0;
443d1b054daSYu Zhao 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
444c1fe1f96SBjorn Helgaas 		res = &dev->resource[i + PCI_IOV_RESOURCES];
44511183991SDavid Daney 		/*
44611183991SDavid Daney 		 * If it is already FIXED, don't change it, something
44711183991SDavid Daney 		 * (perhaps EA or header fixups) wants it this way.
44811183991SDavid Daney 		 */
44911183991SDavid Daney 		if (res->flags & IORESOURCE_PCI_FIXED)
45011183991SDavid Daney 			bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
45111183991SDavid Daney 		else
4520e6c9122SWei Yang 			bar64 = __pci_read_base(dev, pci_bar_unknown, res,
453d1b054daSYu Zhao 						pos + PCI_SRIOV_BAR + i * 4);
454d1b054daSYu Zhao 		if (!res->flags)
455d1b054daSYu Zhao 			continue;
456d1b054daSYu Zhao 		if (resource_size(res) & (PAGE_SIZE - 1)) {
457d1b054daSYu Zhao 			rc = -EIO;
458d1b054daSYu Zhao 			goto failed;
459d1b054daSYu Zhao 		}
4600e6c9122SWei Yang 		iov->barsz[i] = resource_size(res);
461d1b054daSYu Zhao 		res->end = res->start + resource_size(res) * total - 1;
4627506dc79SFrederick Lawler 		pci_info(dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
463e88ae01dSWei Yang 			 i, res, i, total);
4640e6c9122SWei Yang 		i += bar64;
465d1b054daSYu Zhao 		nres++;
466d1b054daSYu Zhao 	}
467d1b054daSYu Zhao 
468d1b054daSYu Zhao 	iov->pos = pos;
469d1b054daSYu Zhao 	iov->nres = nres;
470d1b054daSYu Zhao 	iov->ctrl = ctrl;
4716b136724SBjorn Helgaas 	iov->total_VFs = total;
472*8d85a7a4SJakub Kicinski 	iov->driver_max_VFs = total;
4733142d832SFilippo Sironi 	pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
474d1b054daSYu Zhao 	iov->pgsz = pgsz;
475d1b054daSYu Zhao 	iov->self = dev;
4760e7df224SBodong Wang 	iov->drivers_autoprobe = true;
477d1b054daSYu Zhao 	pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
478d1b054daSYu Zhao 	pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
47962f87c0eSYijing Wang 	if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
4804d135dbeSYu Zhao 		iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
481d1b054daSYu Zhao 
482d1b054daSYu Zhao 	if (pdev)
483d1b054daSYu Zhao 		iov->dev = pci_dev_get(pdev);
484e277d2fcSYu Zhao 	else
485d1b054daSYu Zhao 		iov->dev = dev;
486e277d2fcSYu Zhao 
487d1b054daSYu Zhao 	dev->sriov = iov;
488d1b054daSYu Zhao 	dev->is_physfn = 1;
489ea9a8854SAlexander Duyck 	rc = compute_max_vf_buses(dev);
490ea9a8854SAlexander Duyck 	if (rc)
491ea9a8854SAlexander Duyck 		goto fail_max_buses;
492d1b054daSYu Zhao 
493d1b054daSYu Zhao 	return 0;
494d1b054daSYu Zhao 
495ea9a8854SAlexander Duyck fail_max_buses:
496ea9a8854SAlexander Duyck 	dev->sriov = NULL;
497ea9a8854SAlexander Duyck 	dev->is_physfn = 0;
498d1b054daSYu Zhao failed:
499d1b054daSYu Zhao 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
500c1fe1f96SBjorn Helgaas 		res = &dev->resource[i + PCI_IOV_RESOURCES];
501d1b054daSYu Zhao 		res->flags = 0;
502d1b054daSYu Zhao 	}
503d1b054daSYu Zhao 
5040e6c9122SWei Yang 	kfree(iov);
505d1b054daSYu Zhao 	return rc;
506d1b054daSYu Zhao }
507d1b054daSYu Zhao 
508d1b054daSYu Zhao static void sriov_release(struct pci_dev *dev)
509d1b054daSYu Zhao {
5106b136724SBjorn Helgaas 	BUG_ON(dev->sriov->num_VFs);
511dd7cc44dSYu Zhao 
512e277d2fcSYu Zhao 	if (dev != dev->sriov->dev)
513d1b054daSYu Zhao 		pci_dev_put(dev->sriov->dev);
514d1b054daSYu Zhao 
515d1b054daSYu Zhao 	kfree(dev->sriov);
516d1b054daSYu Zhao 	dev->sriov = NULL;
517d1b054daSYu Zhao }
518d1b054daSYu Zhao 
5198c5cdb6aSYu Zhao static void sriov_restore_state(struct pci_dev *dev)
5208c5cdb6aSYu Zhao {
5218c5cdb6aSYu Zhao 	int i;
5228c5cdb6aSYu Zhao 	u16 ctrl;
5238c5cdb6aSYu Zhao 	struct pci_sriov *iov = dev->sriov;
5248c5cdb6aSYu Zhao 
5258c5cdb6aSYu Zhao 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
5268c5cdb6aSYu Zhao 	if (ctrl & PCI_SRIOV_CTRL_VFE)
5278c5cdb6aSYu Zhao 		return;
5288c5cdb6aSYu Zhao 
529ff26449eSTony Nguyen 	/*
530ff26449eSTony Nguyen 	 * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
531ff26449eSTony Nguyen 	 * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
532ff26449eSTony Nguyen 	 */
533ff26449eSTony Nguyen 	ctrl &= ~PCI_SRIOV_CTRL_ARI;
534ff26449eSTony Nguyen 	ctrl |= iov->ctrl & PCI_SRIOV_CTRL_ARI;
535ff26449eSTony Nguyen 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
536ff26449eSTony Nguyen 
5378c5cdb6aSYu Zhao 	for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
5388c5cdb6aSYu Zhao 		pci_update_resource(dev, i);
5398c5cdb6aSYu Zhao 
5408c5cdb6aSYu Zhao 	pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
541f59dca27SWei Yang 	pci_iov_set_numvfs(dev, iov->num_VFs);
5428c5cdb6aSYu Zhao 	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
5438c5cdb6aSYu Zhao 	if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
5448c5cdb6aSYu Zhao 		msleep(100);
5458c5cdb6aSYu Zhao }
5468c5cdb6aSYu Zhao 
547d1b054daSYu Zhao /**
548d1b054daSYu Zhao  * pci_iov_init - initialize the IOV capability
549d1b054daSYu Zhao  * @dev: the PCI device
550d1b054daSYu Zhao  *
551d1b054daSYu Zhao  * Returns 0 on success, or negative on failure.
552d1b054daSYu Zhao  */
553d1b054daSYu Zhao int pci_iov_init(struct pci_dev *dev)
554d1b054daSYu Zhao {
555d1b054daSYu Zhao 	int pos;
556d1b054daSYu Zhao 
5575f4d91a1SKenji Kaneshige 	if (!pci_is_pcie(dev))
558d1b054daSYu Zhao 		return -ENODEV;
559d1b054daSYu Zhao 
560d1b054daSYu Zhao 	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
561d1b054daSYu Zhao 	if (pos)
562d1b054daSYu Zhao 		return sriov_init(dev, pos);
563d1b054daSYu Zhao 
564d1b054daSYu Zhao 	return -ENODEV;
565d1b054daSYu Zhao }
566d1b054daSYu Zhao 
567d1b054daSYu Zhao /**
568d1b054daSYu Zhao  * pci_iov_release - release resources used by the IOV capability
569d1b054daSYu Zhao  * @dev: the PCI device
570d1b054daSYu Zhao  */
571d1b054daSYu Zhao void pci_iov_release(struct pci_dev *dev)
572d1b054daSYu Zhao {
573d1b054daSYu Zhao 	if (dev->is_physfn)
574d1b054daSYu Zhao 		sriov_release(dev);
575d1b054daSYu Zhao }
576d1b054daSYu Zhao 
577d1b054daSYu Zhao /**
5786ffa2489SBjorn Helgaas  * pci_iov_update_resource - update a VF BAR
5796ffa2489SBjorn Helgaas  * @dev: the PCI device
5806ffa2489SBjorn Helgaas  * @resno: the resource number
5816ffa2489SBjorn Helgaas  *
5826ffa2489SBjorn Helgaas  * Update a VF BAR in the SR-IOV capability of a PF.
5836ffa2489SBjorn Helgaas  */
5846ffa2489SBjorn Helgaas void pci_iov_update_resource(struct pci_dev *dev, int resno)
5856ffa2489SBjorn Helgaas {
5866ffa2489SBjorn Helgaas 	struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
5876ffa2489SBjorn Helgaas 	struct resource *res = dev->resource + resno;
5886ffa2489SBjorn Helgaas 	int vf_bar = resno - PCI_IOV_RESOURCES;
5896ffa2489SBjorn Helgaas 	struct pci_bus_region region;
590546ba9f8SBjorn Helgaas 	u16 cmd;
5916ffa2489SBjorn Helgaas 	u32 new;
5926ffa2489SBjorn Helgaas 	int reg;
5936ffa2489SBjorn Helgaas 
5946ffa2489SBjorn Helgaas 	/*
5956ffa2489SBjorn Helgaas 	 * The generic pci_restore_bars() path calls this for all devices,
5966ffa2489SBjorn Helgaas 	 * including VFs and non-SR-IOV devices.  If this is not a PF, we
5976ffa2489SBjorn Helgaas 	 * have nothing to do.
5986ffa2489SBjorn Helgaas 	 */
5996ffa2489SBjorn Helgaas 	if (!iov)
6006ffa2489SBjorn Helgaas 		return;
6016ffa2489SBjorn Helgaas 
602546ba9f8SBjorn Helgaas 	pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
603546ba9f8SBjorn Helgaas 	if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) {
604546ba9f8SBjorn Helgaas 		dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
605546ba9f8SBjorn Helgaas 			 vf_bar, res);
606546ba9f8SBjorn Helgaas 		return;
607546ba9f8SBjorn Helgaas 	}
608546ba9f8SBjorn Helgaas 
6096ffa2489SBjorn Helgaas 	/*
6106ffa2489SBjorn Helgaas 	 * Ignore unimplemented BARs, unused resource slots for 64-bit
6116ffa2489SBjorn Helgaas 	 * BARs, and non-movable resources, e.g., those described via
6126ffa2489SBjorn Helgaas 	 * Enhanced Allocation.
6136ffa2489SBjorn Helgaas 	 */
6146ffa2489SBjorn Helgaas 	if (!res->flags)
6156ffa2489SBjorn Helgaas 		return;
6166ffa2489SBjorn Helgaas 
6176ffa2489SBjorn Helgaas 	if (res->flags & IORESOURCE_UNSET)
6186ffa2489SBjorn Helgaas 		return;
6196ffa2489SBjorn Helgaas 
6206ffa2489SBjorn Helgaas 	if (res->flags & IORESOURCE_PCI_FIXED)
6216ffa2489SBjorn Helgaas 		return;
6226ffa2489SBjorn Helgaas 
6236ffa2489SBjorn Helgaas 	pcibios_resource_to_bus(dev->bus, &region, res);
6246ffa2489SBjorn Helgaas 	new = region.start;
6256ffa2489SBjorn Helgaas 	new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK;
6266ffa2489SBjorn Helgaas 
6276ffa2489SBjorn Helgaas 	reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar;
6286ffa2489SBjorn Helgaas 	pci_write_config_dword(dev, reg, new);
6296ffa2489SBjorn Helgaas 	if (res->flags & IORESOURCE_MEM_64) {
6306ffa2489SBjorn Helgaas 		new = region.start >> 16 >> 16;
6316ffa2489SBjorn Helgaas 		pci_write_config_dword(dev, reg + 4, new);
6326ffa2489SBjorn Helgaas 	}
6336ffa2489SBjorn Helgaas }
6346ffa2489SBjorn Helgaas 
635978d2d68SWei Yang resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
636978d2d68SWei Yang 						      int resno)
637978d2d68SWei Yang {
638978d2d68SWei Yang 	return pci_iov_resource_size(dev, resno);
639978d2d68SWei Yang }
640978d2d68SWei Yang 
6418c5cdb6aSYu Zhao /**
6426faf17f6SChris Wright  * pci_sriov_resource_alignment - get resource alignment for VF BAR
6436faf17f6SChris Wright  * @dev: the PCI device
6446faf17f6SChris Wright  * @resno: the resource number
6456faf17f6SChris Wright  *
6466faf17f6SChris Wright  * Returns the alignment of the VF BAR found in the SR-IOV capability.
6476faf17f6SChris Wright  * This is not the same as the resource size which is defined as
6486faf17f6SChris Wright  * the VF BAR size multiplied by the number of VFs.  The alignment
6496faf17f6SChris Wright  * is just the VF BAR size.
6506faf17f6SChris Wright  */
6510e52247aSCam Macdonell resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
6526faf17f6SChris Wright {
653978d2d68SWei Yang 	return pcibios_iov_resource_alignment(dev, resno);
6546faf17f6SChris Wright }
6556faf17f6SChris Wright 
6566faf17f6SChris Wright /**
6578c5cdb6aSYu Zhao  * pci_restore_iov_state - restore the state of the IOV capability
6588c5cdb6aSYu Zhao  * @dev: the PCI device
6598c5cdb6aSYu Zhao  */
6608c5cdb6aSYu Zhao void pci_restore_iov_state(struct pci_dev *dev)
6618c5cdb6aSYu Zhao {
6628c5cdb6aSYu Zhao 	if (dev->is_physfn)
6638c5cdb6aSYu Zhao 		sriov_restore_state(dev);
6648c5cdb6aSYu Zhao }
665a28724b0SYu Zhao 
666a28724b0SYu Zhao /**
667608c0d88SBryant G. Ly  * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs
668608c0d88SBryant G. Ly  * @dev: the PCI device
669608c0d88SBryant G. Ly  * @auto_probe: set VF drivers auto probe flag
670608c0d88SBryant G. Ly  */
671608c0d88SBryant G. Ly void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe)
672608c0d88SBryant G. Ly {
673608c0d88SBryant G. Ly 	if (dev->is_physfn)
674608c0d88SBryant G. Ly 		dev->sriov->drivers_autoprobe = auto_probe;
675608c0d88SBryant G. Ly }
676608c0d88SBryant G. Ly 
677608c0d88SBryant G. Ly /**
678a28724b0SYu Zhao  * pci_iov_bus_range - find bus range used by Virtual Function
679a28724b0SYu Zhao  * @bus: the PCI bus
680a28724b0SYu Zhao  *
681a28724b0SYu Zhao  * Returns max number of buses (exclude current one) used by Virtual
682a28724b0SYu Zhao  * Functions.
683a28724b0SYu Zhao  */
684a28724b0SYu Zhao int pci_iov_bus_range(struct pci_bus *bus)
685a28724b0SYu Zhao {
686a28724b0SYu Zhao 	int max = 0;
687a28724b0SYu Zhao 	struct pci_dev *dev;
688a28724b0SYu Zhao 
689a28724b0SYu Zhao 	list_for_each_entry(dev, &bus->devices, bus_list) {
690a28724b0SYu Zhao 		if (!dev->is_physfn)
691a28724b0SYu Zhao 			continue;
6924449f079SWei Yang 		if (dev->sriov->max_VF_buses > max)
6934449f079SWei Yang 			max = dev->sriov->max_VF_buses;
694a28724b0SYu Zhao 	}
695a28724b0SYu Zhao 
696a28724b0SYu Zhao 	return max ? max - bus->number : 0;
697a28724b0SYu Zhao }
698dd7cc44dSYu Zhao 
699dd7cc44dSYu Zhao /**
700dd7cc44dSYu Zhao  * pci_enable_sriov - enable the SR-IOV capability
701dd7cc44dSYu Zhao  * @dev: the PCI device
70252a8873bSRandy Dunlap  * @nr_virtfn: number of virtual functions to enable
703dd7cc44dSYu Zhao  *
704dd7cc44dSYu Zhao  * Returns 0 on success, or negative on failure.
705dd7cc44dSYu Zhao  */
706dd7cc44dSYu Zhao int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
707dd7cc44dSYu Zhao {
708dd7cc44dSYu Zhao 	might_sleep();
709dd7cc44dSYu Zhao 
710dd7cc44dSYu Zhao 	if (!dev->is_physfn)
711652d1100SStefan Assmann 		return -ENOSYS;
712dd7cc44dSYu Zhao 
713dd7cc44dSYu Zhao 	return sriov_enable(dev, nr_virtfn);
714dd7cc44dSYu Zhao }
715dd7cc44dSYu Zhao EXPORT_SYMBOL_GPL(pci_enable_sriov);
716dd7cc44dSYu Zhao 
717dd7cc44dSYu Zhao /**
718dd7cc44dSYu Zhao  * pci_disable_sriov - disable the SR-IOV capability
719dd7cc44dSYu Zhao  * @dev: the PCI device
720dd7cc44dSYu Zhao  */
721dd7cc44dSYu Zhao void pci_disable_sriov(struct pci_dev *dev)
722dd7cc44dSYu Zhao {
723dd7cc44dSYu Zhao 	might_sleep();
724dd7cc44dSYu Zhao 
725dd7cc44dSYu Zhao 	if (!dev->is_physfn)
726dd7cc44dSYu Zhao 		return;
727dd7cc44dSYu Zhao 
728dd7cc44dSYu Zhao 	sriov_disable(dev);
729dd7cc44dSYu Zhao }
730dd7cc44dSYu Zhao EXPORT_SYMBOL_GPL(pci_disable_sriov);
73174bb1bccSYu Zhao 
73274bb1bccSYu Zhao /**
733fb8a0d9dSWilliams, Mitch A  * pci_num_vf - return number of VFs associated with a PF device_release_driver
734fb8a0d9dSWilliams, Mitch A  * @dev: the PCI device
735fb8a0d9dSWilliams, Mitch A  *
736fb8a0d9dSWilliams, Mitch A  * Returns number of VFs, or 0 if SR-IOV is not enabled.
737fb8a0d9dSWilliams, Mitch A  */
738fb8a0d9dSWilliams, Mitch A int pci_num_vf(struct pci_dev *dev)
739fb8a0d9dSWilliams, Mitch A {
7401452cd76SBjorn Helgaas 	if (!dev->is_physfn)
741fb8a0d9dSWilliams, Mitch A 		return 0;
7421452cd76SBjorn Helgaas 
7436b136724SBjorn Helgaas 	return dev->sriov->num_VFs;
744fb8a0d9dSWilliams, Mitch A }
745fb8a0d9dSWilliams, Mitch A EXPORT_SYMBOL_GPL(pci_num_vf);
746bff73156SDonald Dutile 
747bff73156SDonald Dutile /**
7485a8eb242SAlexander Duyck  * pci_vfs_assigned - returns number of VFs are assigned to a guest
7495a8eb242SAlexander Duyck  * @dev: the PCI device
7505a8eb242SAlexander Duyck  *
7515a8eb242SAlexander Duyck  * Returns number of VFs belonging to this device that are assigned to a guest.
752652d1100SStefan Assmann  * If device is not a physical function returns 0.
7535a8eb242SAlexander Duyck  */
7545a8eb242SAlexander Duyck int pci_vfs_assigned(struct pci_dev *dev)
7555a8eb242SAlexander Duyck {
7565a8eb242SAlexander Duyck 	struct pci_dev *vfdev;
7575a8eb242SAlexander Duyck 	unsigned int vfs_assigned = 0;
7585a8eb242SAlexander Duyck 	unsigned short dev_id;
7595a8eb242SAlexander Duyck 
7605a8eb242SAlexander Duyck 	/* only search if we are a PF */
7615a8eb242SAlexander Duyck 	if (!dev->is_physfn)
7625a8eb242SAlexander Duyck 		return 0;
7635a8eb242SAlexander Duyck 
7645a8eb242SAlexander Duyck 	/*
7655a8eb242SAlexander Duyck 	 * determine the device ID for the VFs, the vendor ID will be the
7665a8eb242SAlexander Duyck 	 * same as the PF so there is no need to check for that one
7675a8eb242SAlexander Duyck 	 */
7683142d832SFilippo Sironi 	dev_id = dev->sriov->vf_device;
7695a8eb242SAlexander Duyck 
7705a8eb242SAlexander Duyck 	/* loop through all the VFs to see if we own any that are assigned */
7715a8eb242SAlexander Duyck 	vfdev = pci_get_device(dev->vendor, dev_id, NULL);
7725a8eb242SAlexander Duyck 	while (vfdev) {
7735a8eb242SAlexander Duyck 		/*
7745a8eb242SAlexander Duyck 		 * It is considered assigned if it is a virtual function with
7755a8eb242SAlexander Duyck 		 * our dev as the physical function and the assigned bit is set
7765a8eb242SAlexander Duyck 		 */
7775a8eb242SAlexander Duyck 		if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
778be63497cSEthan Zhao 			pci_is_dev_assigned(vfdev))
7795a8eb242SAlexander Duyck 			vfs_assigned++;
7805a8eb242SAlexander Duyck 
7815a8eb242SAlexander Duyck 		vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
7825a8eb242SAlexander Duyck 	}
7835a8eb242SAlexander Duyck 
7845a8eb242SAlexander Duyck 	return vfs_assigned;
7855a8eb242SAlexander Duyck }
7865a8eb242SAlexander Duyck EXPORT_SYMBOL_GPL(pci_vfs_assigned);
7875a8eb242SAlexander Duyck 
7885a8eb242SAlexander Duyck /**
789bff73156SDonald Dutile  * pci_sriov_set_totalvfs -- reduce the TotalVFs available
790bff73156SDonald Dutile  * @dev: the PCI PF device
7912094f167SRandy Dunlap  * @numvfs: number that should be used for TotalVFs supported
792bff73156SDonald Dutile  *
793bff73156SDonald Dutile  * Should be called from PF driver's probe routine with
794bff73156SDonald Dutile  * device's mutex held.
795bff73156SDonald Dutile  *
796bff73156SDonald Dutile  * Returns 0 if PF is an SRIOV-capable device and
797652d1100SStefan Assmann  * value of numvfs valid. If not a PF return -ENOSYS;
798652d1100SStefan Assmann  * if numvfs is invalid return -EINVAL;
799bff73156SDonald Dutile  * if VFs already enabled, return -EBUSY.
800bff73156SDonald Dutile  */
801bff73156SDonald Dutile int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
802bff73156SDonald Dutile {
803652d1100SStefan Assmann 	if (!dev->is_physfn)
804652d1100SStefan Assmann 		return -ENOSYS;
805652d1100SStefan Assmann 	if (numvfs > dev->sriov->total_VFs)
806bff73156SDonald Dutile 		return -EINVAL;
807bff73156SDonald Dutile 
808bff73156SDonald Dutile 	/* Shouldn't change if VFs already enabled */
809bff73156SDonald Dutile 	if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
810bff73156SDonald Dutile 		return -EBUSY;
811bff73156SDonald Dutile 	else
8126b136724SBjorn Helgaas 		dev->sriov->driver_max_VFs = numvfs;
813bff73156SDonald Dutile 
814bff73156SDonald Dutile 	return 0;
815bff73156SDonald Dutile }
816bff73156SDonald Dutile EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
817bff73156SDonald Dutile 
818bff73156SDonald Dutile /**
819ddc191f5SJonghwan Choi  * pci_sriov_get_totalvfs -- get total VFs supported on this device
820bff73156SDonald Dutile  * @dev: the PCI PF device
821bff73156SDonald Dutile  *
822bff73156SDonald Dutile  * For a PCIe device with SRIOV support, return the PCIe
8236b136724SBjorn Helgaas  * SRIOV capability value of TotalVFs or the value of driver_max_VFs
824652d1100SStefan Assmann  * if the driver reduced it.  Otherwise 0.
825bff73156SDonald Dutile  */
826bff73156SDonald Dutile int pci_sriov_get_totalvfs(struct pci_dev *dev)
827bff73156SDonald Dutile {
8281452cd76SBjorn Helgaas 	if (!dev->is_physfn)
829652d1100SStefan Assmann 		return 0;
830bff73156SDonald Dutile 
8316b136724SBjorn Helgaas 	return dev->sriov->driver_max_VFs;
832bff73156SDonald Dutile }
833bff73156SDonald Dutile EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);
8348effc395SAlexander Duyck 
8358effc395SAlexander Duyck /**
8368effc395SAlexander Duyck  * pci_sriov_configure_simple - helper to configure SR-IOV
8378effc395SAlexander Duyck  * @dev: the PCI device
8388effc395SAlexander Duyck  * @nr_virtfn: number of virtual functions to enable, 0 to disable
8398effc395SAlexander Duyck  *
8408effc395SAlexander Duyck  * Enable or disable SR-IOV for devices that don't require any PF setup
8418effc395SAlexander Duyck  * before enabling SR-IOV.  Return value is negative on error, or number of
8428effc395SAlexander Duyck  * VFs allocated on success.
8438effc395SAlexander Duyck  */
8448effc395SAlexander Duyck int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn)
8458effc395SAlexander Duyck {
8468effc395SAlexander Duyck 	int rc;
8478effc395SAlexander Duyck 
8488effc395SAlexander Duyck 	might_sleep();
8498effc395SAlexander Duyck 
8508effc395SAlexander Duyck 	if (!dev->is_physfn)
8518effc395SAlexander Duyck 		return -ENODEV;
8528effc395SAlexander Duyck 
8538effc395SAlexander Duyck 	if (pci_vfs_assigned(dev)) {
8548effc395SAlexander Duyck 		pci_warn(dev, "Cannot modify SR-IOV while VFs are assigned\n");
8558effc395SAlexander Duyck 		return -EPERM;
8568effc395SAlexander Duyck 	}
8578effc395SAlexander Duyck 
8588effc395SAlexander Duyck 	if (nr_virtfn == 0) {
8598effc395SAlexander Duyck 		sriov_disable(dev);
8608effc395SAlexander Duyck 		return 0;
8618effc395SAlexander Duyck 	}
8628effc395SAlexander Duyck 
8638effc395SAlexander Duyck 	rc = sriov_enable(dev, nr_virtfn);
8648effc395SAlexander Duyck 	if (rc < 0)
8658effc395SAlexander Duyck 		return rc;
8668effc395SAlexander Duyck 
8678effc395SAlexander Duyck 	return nr_virtfn;
8688effc395SAlexander Duyck }
8698effc395SAlexander Duyck EXPORT_SYMBOL_GPL(pci_sriov_configure_simple);
870