blob: 2ae921f84bd31d008be3f9026af4e38b110ac284 [file] [log] [blame]
Yu Zhaod1b054d2009-03-20 11:25:11 +08001/*
2 * drivers/pci/iov.c
3 *
4 * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
5 *
6 * PCI Express I/O Virtualization (IOV) support.
7 * Single Root IOV 1.0
Yu Zhao302b4212009-05-18 13:51:32 +08008 * Address Translation Service 1.0
Yu Zhaod1b054d2009-03-20 11:25:11 +08009 */
10
11#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Yu Zhaod1b054d2009-03-20 11:25:11 +080013#include <linux/mutex.h>
Paul Gortmaker363c75d2011-05-27 09:37:25 -040014#include <linux/export.h>
Yu Zhaod1b054d2009-03-20 11:25:11 +080015#include <linux/string.h>
16#include <linux/delay.h>
Joerg Roedel5cdede22011-04-04 15:55:18 +020017#include <linux/pci-ats.h>
Yu Zhaod1b054d2009-03-20 11:25:11 +080018#include "pci.h"
19
Yu Zhaodd7cc442009-03-20 11:25:15 +080020#define VIRTFN_ID_LEN 16
Yu Zhaod1b054d2009-03-20 11:25:11 +080021
Yu Zhaoa28724b2009-03-20 11:25:13 +080022static inline u8 virtfn_bus(struct pci_dev *dev, int id)
23{
24 return dev->bus->number + ((dev->devfn + dev->sriov->offset +
25 dev->sriov->stride * id) >> 8);
26}
27
28static inline u8 virtfn_devfn(struct pci_dev *dev, int id)
29{
30 return (dev->devfn + dev->sriov->offset +
31 dev->sriov->stride * id) & 0xff;
32}
33
Wei Yangf59dca22015-03-25 16:23:46 +080034/*
35 * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
36 * change when NumVFs changes.
37 *
38 * Update iov->offset and iov->stride when NumVFs is written.
39 */
40static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
41{
42 struct pci_sriov *iov = dev->sriov;
43
44 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
45 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
46 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
47}
48
Wei Yang4449f072015-03-25 16:23:47 +080049/*
50 * The PF consumes one bus number. NumVFs, First VF Offset, and VF Stride
51 * determine how many additional bus numbers will be consumed by VFs.
52 *
53 * Iterate over all valid NumVFs and calculate the maximum number of bus
54 * numbers that could ever be required.
55 */
56static inline u8 virtfn_max_buses(struct pci_dev *dev)
57{
58 struct pci_sriov *iov = dev->sriov;
59 int nr_virtfn;
60 u8 max = 0;
61 u8 busnr;
62
63 for (nr_virtfn = 1; nr_virtfn <= iov->total_VFs; nr_virtfn++) {
64 pci_iov_set_numvfs(dev, nr_virtfn);
65 busnr = virtfn_bus(dev, nr_virtfn - 1);
66 if (busnr > max)
67 max = busnr;
68 }
69
70 return max;
71}
72
Yu Zhaodd7cc442009-03-20 11:25:15 +080073static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
74{
Yu Zhaodd7cc442009-03-20 11:25:15 +080075 struct pci_bus *child;
76
77 if (bus->number == busnr)
78 return bus;
79
80 child = pci_find_bus(pci_domain_nr(bus), busnr);
81 if (child)
82 return child;
83
84 child = pci_add_new_bus(bus, NULL, busnr);
85 if (!child)
86 return NULL;
87
Yinghai Lub7eac052012-05-17 18:51:13 -070088 pci_bus_insert_busn_res(child, busnr, busnr);
Yu Zhaodd7cc442009-03-20 11:25:15 +080089
90 return child;
91}
92
Jiang Liudc087f22013-05-25 21:48:37 +080093static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
Yu Zhaodd7cc442009-03-20 11:25:15 +080094{
Jiang Liudc087f22013-05-25 21:48:37 +080095 if (physbus != virtbus && list_empty(&virtbus->devices))
96 pci_remove_bus(virtbus);
Yu Zhaodd7cc442009-03-20 11:25:15 +080097}
98
Wei Yang0e6c9122015-03-25 16:23:44 +080099resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
100{
101 if (!dev->is_physfn)
102 return 0;
103
104 return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
105}
106
Yu Zhaodd7cc442009-03-20 11:25:15 +0800107static int virtfn_add(struct pci_dev *dev, int id, int reset)
108{
109 int i;
Jiang Liudc087f22013-05-25 21:48:37 +0800110 int rc = -ENOMEM;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800111 u64 size;
112 char buf[VIRTFN_ID_LEN];
113 struct pci_dev *virtfn;
114 struct resource *res;
115 struct pci_sriov *iov = dev->sriov;
Gu Zheng8b1fce02013-05-25 21:48:31 +0800116 struct pci_bus *bus;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800117
118 mutex_lock(&iov->dev->sriov->lock);
Gu Zheng8b1fce02013-05-25 21:48:31 +0800119 bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
Jiang Liudc087f22013-05-25 21:48:37 +0800120 if (!bus)
121 goto failed;
122
123 virtfn = pci_alloc_dev(bus);
124 if (!virtfn)
125 goto failed0;
126
Yu Zhaodd7cc442009-03-20 11:25:15 +0800127 virtfn->devfn = virtfn_devfn(dev, id);
128 virtfn->vendor = dev->vendor;
129 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device);
130 pci_setup_device(virtfn);
131 virtfn->dev.parent = dev->dev.parent;
Xudong Haofbf33f52013-05-31 12:21:29 +0800132 virtfn->physfn = pci_dev_get(dev);
133 virtfn->is_virtfn = 1;
Alex Williamsonaa9319772014-01-09 08:36:08 -0700134 virtfn->multifunction = 0;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800135
136 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Bjorn Helgaasc1fe1f92015-03-25 16:23:45 +0800137 res = &dev->resource[i + PCI_IOV_RESOURCES];
Yu Zhaodd7cc442009-03-20 11:25:15 +0800138 if (!res->parent)
139 continue;
140 virtfn->resource[i].name = pci_name(virtfn);
141 virtfn->resource[i].flags = res->flags;
Wei Yang0e6c9122015-03-25 16:23:44 +0800142 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800143 virtfn->resource[i].start = res->start + size * id;
144 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
145 rc = request_resource(res, &virtfn->resource[i]);
146 BUG_ON(rc);
147 }
148
149 if (reset)
Yu Zhao8c1c6992009-06-13 15:52:13 +0800150 __pci_reset_function(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800151
152 pci_device_add(virtfn, virtfn->bus);
153 mutex_unlock(&iov->dev->sriov->lock);
154
Yijing Wangc893d132014-05-30 11:01:03 +0800155 pci_bus_add_device(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800156 sprintf(buf, "virtfn%u", id);
157 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
158 if (rc)
159 goto failed1;
160 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
161 if (rc)
162 goto failed2;
163
164 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
165
166 return 0;
167
168failed2:
169 sysfs_remove_link(&dev->dev.kobj, buf);
170failed1:
171 pci_dev_put(dev);
172 mutex_lock(&iov->dev->sriov->lock);
Yinghai Lu210647a2012-02-25 13:54:20 -0800173 pci_stop_and_remove_bus_device(virtfn);
Jiang Liudc087f22013-05-25 21:48:37 +0800174failed0:
175 virtfn_remove_bus(dev->bus, bus);
176failed:
Yu Zhaodd7cc442009-03-20 11:25:15 +0800177 mutex_unlock(&iov->dev->sriov->lock);
178
179 return rc;
180}
181
182static void virtfn_remove(struct pci_dev *dev, int id, int reset)
183{
184 char buf[VIRTFN_ID_LEN];
Yu Zhaodd7cc442009-03-20 11:25:15 +0800185 struct pci_dev *virtfn;
186 struct pci_sriov *iov = dev->sriov;
187
Jiang Liudc087f22013-05-25 21:48:37 +0800188 virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
189 virtfn_bus(dev, id),
190 virtfn_devfn(dev, id));
Yu Zhaodd7cc442009-03-20 11:25:15 +0800191 if (!virtfn)
192 return;
193
Yu Zhaodd7cc442009-03-20 11:25:15 +0800194 if (reset) {
195 device_release_driver(&virtfn->dev);
Yu Zhao8c1c6992009-06-13 15:52:13 +0800196 __pci_reset_function(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800197 }
198
199 sprintf(buf, "virtfn%u", id);
200 sysfs_remove_link(&dev->dev.kobj, buf);
Yinghai Lu09cedbe2012-02-04 22:55:01 -0800201 /*
202 * pci_stop_dev() could have been called for this virtfn already,
203 * so the directory for the virtfn may have been removed before.
204 * Double check to avoid spurious sysfs warnings.
205 */
206 if (virtfn->dev.kobj.sd)
207 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
Yu Zhaodd7cc442009-03-20 11:25:15 +0800208
209 mutex_lock(&iov->dev->sriov->lock);
Yinghai Lu210647a2012-02-25 13:54:20 -0800210 pci_stop_and_remove_bus_device(virtfn);
Jiang Liudc087f22013-05-25 21:48:37 +0800211 virtfn_remove_bus(dev->bus, virtfn->bus);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800212 mutex_unlock(&iov->dev->sriov->lock);
213
Jiang Liudc087f22013-05-25 21:48:37 +0800214 /* balance pci_get_domain_bus_and_slot() */
215 pci_dev_put(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800216 pci_dev_put(dev);
217}
218
219static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
220{
221 int rc;
222 int i, j;
223 int nres;
224 u16 offset, stride, initial;
225 struct resource *res;
226 struct pci_dev *pdev;
227 struct pci_sriov *iov = dev->sriov;
Ram Paibbef98a2011-11-06 10:33:10 +0800228 int bars = 0;
Bjorn Helgaas68f8e9f2015-03-25 16:23:42 +0800229 u8 bus;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800230
231 if (!nr_virtfn)
232 return 0;
233
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700234 if (iov->num_VFs)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800235 return -EINVAL;
236
237 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700238 if (initial > iov->total_VFs ||
239 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
Yu Zhaodd7cc442009-03-20 11:25:15 +0800240 return -EIO;
241
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700242 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
Yu Zhaodd7cc442009-03-20 11:25:15 +0800243 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
244 return -EINVAL;
245
Yu Zhaodd7cc442009-03-20 11:25:15 +0800246 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset);
247 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride);
248 if (!offset || (nr_virtfn > 1 && !stride))
249 return -EIO;
250
251 nres = 0;
252 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Ram Paibbef98a2011-11-06 10:33:10 +0800253 bars |= (1 << (i + PCI_IOV_RESOURCES));
Bjorn Helgaasc1fe1f92015-03-25 16:23:45 +0800254 res = &dev->resource[i + PCI_IOV_RESOURCES];
Yu Zhaodd7cc442009-03-20 11:25:15 +0800255 if (res->parent)
256 nres++;
257 }
258 if (nres != iov->nres) {
259 dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
260 return -ENOMEM;
261 }
262
263 iov->offset = offset;
264 iov->stride = stride;
265
Bjorn Helgaas68f8e9f2015-03-25 16:23:42 +0800266 bus = virtfn_bus(dev, nr_virtfn - 1);
267 if (bus > dev->bus->busn_res.end) {
268 dev_err(&dev->dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
269 nr_virtfn, bus, &dev->bus->busn_res);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800270 return -ENOMEM;
271 }
272
Ram Paibbef98a2011-11-06 10:33:10 +0800273 if (pci_enable_resources(dev, bars)) {
274 dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
275 return -ENOMEM;
276 }
277
Yu Zhaodd7cc442009-03-20 11:25:15 +0800278 if (iov->link != dev->devfn) {
279 pdev = pci_get_slot(dev->bus, iov->link);
280 if (!pdev)
281 return -ENODEV;
282
Jiang Liudc087f22013-05-25 21:48:37 +0800283 if (!pdev->is_physfn) {
284 pci_dev_put(pdev);
Stefan Assmann652d1102013-07-31 16:47:56 -0600285 return -ENOSYS;
Jiang Liudc087f22013-05-25 21:48:37 +0800286 }
Yu Zhaodd7cc442009-03-20 11:25:15 +0800287
288 rc = sysfs_create_link(&dev->dev.kobj,
289 &pdev->dev.kobj, "dep_link");
Jiang Liudc087f22013-05-25 21:48:37 +0800290 pci_dev_put(pdev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800291 if (rc)
292 return rc;
293 }
294
Wei Yangf59dca22015-03-25 16:23:46 +0800295 pci_iov_set_numvfs(dev, nr_virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800296 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100297 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800298 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
299 msleep(100);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100300 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800301
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700302 iov->initial_VFs = initial;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800303 if (nr_virtfn < initial)
304 initial = nr_virtfn;
305
306 for (i = 0; i < initial; i++) {
307 rc = virtfn_add(dev, i, 0);
308 if (rc)
309 goto failed;
310 }
311
312 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700313 iov->num_VFs = nr_virtfn;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800314
315 return 0;
316
317failed:
318 for (j = 0; j < i; j++)
319 virtfn_remove(dev, j, 0);
320
321 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100322 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800323 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
Wei Yangf59dca22015-03-25 16:23:46 +0800324 pci_iov_set_numvfs(dev, 0);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800325 ssleep(1);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100326 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800327
328 if (iov->link != dev->devfn)
329 sysfs_remove_link(&dev->dev.kobj, "dep_link");
330
331 return rc;
332}
333
334static void sriov_disable(struct pci_dev *dev)
335{
336 int i;
337 struct pci_sriov *iov = dev->sriov;
338
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700339 if (!iov->num_VFs)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800340 return;
341
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700342 for (i = 0; i < iov->num_VFs; i++)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800343 virtfn_remove(dev, i, 0);
344
345 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100346 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800347 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
348 ssleep(1);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100349 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800350
351 if (iov->link != dev->devfn)
352 sysfs_remove_link(&dev->dev.kobj, "dep_link");
353
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700354 iov->num_VFs = 0;
Wei Yangf59dca22015-03-25 16:23:46 +0800355 pci_iov_set_numvfs(dev, 0);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800356}
357
Yu Zhaod1b054d2009-03-20 11:25:11 +0800358static int sriov_init(struct pci_dev *dev, int pos)
359{
Wei Yang0e6c9122015-03-25 16:23:44 +0800360 int i, bar64;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800361 int rc;
362 int nres;
363 u32 pgsz;
364 u16 ctrl, total, offset, stride;
365 struct pci_sriov *iov;
366 struct resource *res;
367 struct pci_dev *pdev;
368
Yijing Wang62f87c02012-07-24 17:20:03 +0800369 if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
370 pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
Yu Zhaod1b054d2009-03-20 11:25:11 +0800371 return -ENODEV;
372
373 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
374 if (ctrl & PCI_SRIOV_CTRL_VFE) {
375 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
376 ssleep(1);
377 }
378
379 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
380 if (!total)
381 return 0;
382
383 ctrl = 0;
384 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
385 if (pdev->is_physfn)
386 goto found;
387
388 pdev = NULL;
389 if (pci_ari_enabled(dev->bus))
390 ctrl |= PCI_SRIOV_CTRL_ARI;
391
392found:
393 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
ethan.zhao045cc222013-11-06 22:49:13 +0800394 pci_write_config_word(dev, pos + PCI_SRIOV_NUM_VF, 0);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800395 pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset);
396 pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride);
397 if (!offset || (total > 1 && !stride))
398 return -EIO;
399
400 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
401 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
402 pgsz &= ~((1 << i) - 1);
403 if (!pgsz)
404 return -EIO;
405
406 pgsz &= ~(pgsz - 1);
Vaidyanathan Srinivasan8161fe92012-02-02 23:11:20 +0530407 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800408
Wei Yang0e6c9122015-03-25 16:23:44 +0800409 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
410 if (!iov)
411 return -ENOMEM;
412
Yu Zhaod1b054d2009-03-20 11:25:11 +0800413 nres = 0;
414 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Bjorn Helgaasc1fe1f92015-03-25 16:23:45 +0800415 res = &dev->resource[i + PCI_IOV_RESOURCES];
Wei Yang0e6c9122015-03-25 16:23:44 +0800416 bar64 = __pci_read_base(dev, pci_bar_unknown, res,
417 pos + PCI_SRIOV_BAR + i * 4);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800418 if (!res->flags)
419 continue;
420 if (resource_size(res) & (PAGE_SIZE - 1)) {
421 rc = -EIO;
422 goto failed;
423 }
Wei Yang0e6c9122015-03-25 16:23:44 +0800424 iov->barsz[i] = resource_size(res);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800425 res->end = res->start + resource_size(res) * total - 1;
Wei Yange88ae012015-03-25 16:23:43 +0800426 dev_info(&dev->dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
427 i, res, i, total);
Wei Yang0e6c9122015-03-25 16:23:44 +0800428 i += bar64;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800429 nres++;
430 }
431
Yu Zhaod1b054d2009-03-20 11:25:11 +0800432 iov->pos = pos;
433 iov->nres = nres;
434 iov->ctrl = ctrl;
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700435 iov->total_VFs = total;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800436 iov->offset = offset;
437 iov->stride = stride;
438 iov->pgsz = pgsz;
439 iov->self = dev;
440 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
441 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
Yijing Wang62f87c02012-07-24 17:20:03 +0800442 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
Yu Zhao4d135db2009-05-20 17:11:57 +0800443 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800444
445 if (pdev)
446 iov->dev = pci_dev_get(pdev);
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800447 else
Yu Zhaod1b054d2009-03-20 11:25:11 +0800448 iov->dev = dev;
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800449
450 mutex_init(&iov->lock);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800451
452 dev->sriov = iov;
453 dev->is_physfn = 1;
Wei Yang4449f072015-03-25 16:23:47 +0800454 iov->max_VF_buses = virtfn_max_buses(dev);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800455
456 return 0;
457
458failed:
459 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Bjorn Helgaasc1fe1f92015-03-25 16:23:45 +0800460 res = &dev->resource[i + PCI_IOV_RESOURCES];
Yu Zhaod1b054d2009-03-20 11:25:11 +0800461 res->flags = 0;
462 }
463
Wei Yang0e6c9122015-03-25 16:23:44 +0800464 kfree(iov);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800465 return rc;
466}
467
468static void sriov_release(struct pci_dev *dev)
469{
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700470 BUG_ON(dev->sriov->num_VFs);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800471
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800472 if (dev != dev->sriov->dev)
Yu Zhaod1b054d2009-03-20 11:25:11 +0800473 pci_dev_put(dev->sriov->dev);
474
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800475 mutex_destroy(&dev->sriov->lock);
476
Yu Zhaod1b054d2009-03-20 11:25:11 +0800477 kfree(dev->sriov);
478 dev->sriov = NULL;
479}
480
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800481static void sriov_restore_state(struct pci_dev *dev)
482{
483 int i;
484 u16 ctrl;
485 struct pci_sriov *iov = dev->sriov;
486
487 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
488 if (ctrl & PCI_SRIOV_CTRL_VFE)
489 return;
490
491 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
492 pci_update_resource(dev, i);
493
494 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
Wei Yangf59dca22015-03-25 16:23:46 +0800495 pci_iov_set_numvfs(dev, iov->num_VFs);
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800496 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
497 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
498 msleep(100);
499}
500
Yu Zhaod1b054d2009-03-20 11:25:11 +0800501/**
502 * pci_iov_init - initialize the IOV capability
503 * @dev: the PCI device
504 *
505 * Returns 0 on success, or negative on failure.
506 */
507int pci_iov_init(struct pci_dev *dev)
508{
509 int pos;
510
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900511 if (!pci_is_pcie(dev))
Yu Zhaod1b054d2009-03-20 11:25:11 +0800512 return -ENODEV;
513
514 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
515 if (pos)
516 return sriov_init(dev, pos);
517
518 return -ENODEV;
519}
520
521/**
522 * pci_iov_release - release resources used by the IOV capability
523 * @dev: the PCI device
524 */
525void pci_iov_release(struct pci_dev *dev)
526{
527 if (dev->is_physfn)
528 sriov_release(dev);
529}
530
531/**
532 * pci_iov_resource_bar - get position of the SR-IOV BAR
533 * @dev: the PCI device
534 * @resno: the resource number
Yu Zhaod1b054d2009-03-20 11:25:11 +0800535 *
536 * Returns position of the BAR encapsulated in the SR-IOV capability.
537 */
Myron Stowe26ff46c2014-11-11 08:04:50 -0700538int pci_iov_resource_bar(struct pci_dev *dev, int resno)
Yu Zhaod1b054d2009-03-20 11:25:11 +0800539{
540 if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
541 return 0;
542
543 BUG_ON(!dev->is_physfn);
544
Yu Zhaod1b054d2009-03-20 11:25:11 +0800545 return dev->sriov->pos + PCI_SRIOV_BAR +
546 4 * (resno - PCI_IOV_RESOURCES);
547}
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800548
549/**
Chris Wright6faf17f2009-08-28 13:00:06 -0700550 * pci_sriov_resource_alignment - get resource alignment for VF BAR
551 * @dev: the PCI device
552 * @resno: the resource number
553 *
554 * Returns the alignment of the VF BAR found in the SR-IOV capability.
555 * This is not the same as the resource size which is defined as
556 * the VF BAR size multiplied by the number of VFs. The alignment
557 * is just the VF BAR size.
558 */
Cam Macdonell0e522472010-09-07 17:25:20 -0700559resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
Chris Wright6faf17f2009-08-28 13:00:06 -0700560{
Wei Yang0e6c9122015-03-25 16:23:44 +0800561 return pci_iov_resource_size(dev, resno);
Chris Wright6faf17f2009-08-28 13:00:06 -0700562}
563
564/**
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800565 * pci_restore_iov_state - restore the state of the IOV capability
566 * @dev: the PCI device
567 */
568void pci_restore_iov_state(struct pci_dev *dev)
569{
570 if (dev->is_physfn)
571 sriov_restore_state(dev);
572}
Yu Zhaoa28724b2009-03-20 11:25:13 +0800573
574/**
575 * pci_iov_bus_range - find bus range used by Virtual Function
576 * @bus: the PCI bus
577 *
578 * Returns max number of buses (exclude current one) used by Virtual
579 * Functions.
580 */
581int pci_iov_bus_range(struct pci_bus *bus)
582{
583 int max = 0;
Yu Zhaoa28724b2009-03-20 11:25:13 +0800584 struct pci_dev *dev;
585
586 list_for_each_entry(dev, &bus->devices, bus_list) {
587 if (!dev->is_physfn)
588 continue;
Wei Yang4449f072015-03-25 16:23:47 +0800589 if (dev->sriov->max_VF_buses > max)
590 max = dev->sriov->max_VF_buses;
Yu Zhaoa28724b2009-03-20 11:25:13 +0800591 }
592
593 return max ? max - bus->number : 0;
594}
Yu Zhaodd7cc442009-03-20 11:25:15 +0800595
596/**
597 * pci_enable_sriov - enable the SR-IOV capability
598 * @dev: the PCI device
Randy Dunlap52a88732009-04-01 17:45:30 -0700599 * @nr_virtfn: number of virtual functions to enable
Yu Zhaodd7cc442009-03-20 11:25:15 +0800600 *
601 * Returns 0 on success, or negative on failure.
602 */
603int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
604{
605 might_sleep();
606
607 if (!dev->is_physfn)
Stefan Assmann652d1102013-07-31 16:47:56 -0600608 return -ENOSYS;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800609
610 return sriov_enable(dev, nr_virtfn);
611}
612EXPORT_SYMBOL_GPL(pci_enable_sriov);
613
614/**
615 * pci_disable_sriov - disable the SR-IOV capability
616 * @dev: the PCI device
617 */
618void pci_disable_sriov(struct pci_dev *dev)
619{
620 might_sleep();
621
622 if (!dev->is_physfn)
623 return;
624
625 sriov_disable(dev);
626}
627EXPORT_SYMBOL_GPL(pci_disable_sriov);
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800628
629/**
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000630 * pci_num_vf - return number of VFs associated with a PF device_release_driver
631 * @dev: the PCI device
632 *
633 * Returns number of VFs, or 0 if SR-IOV is not enabled.
634 */
635int pci_num_vf(struct pci_dev *dev)
636{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700637 if (!dev->is_physfn)
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000638 return 0;
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700639
640 return dev->sriov->num_VFs;
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000641}
642EXPORT_SYMBOL_GPL(pci_num_vf);
Donald Dutilebff73152012-11-05 15:20:37 -0500643
644/**
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000645 * pci_vfs_assigned - returns number of VFs are assigned to a guest
646 * @dev: the PCI device
647 *
648 * Returns number of VFs belonging to this device that are assigned to a guest.
Stefan Assmann652d1102013-07-31 16:47:56 -0600649 * If device is not a physical function returns 0.
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000650 */
651int pci_vfs_assigned(struct pci_dev *dev)
652{
653 struct pci_dev *vfdev;
654 unsigned int vfs_assigned = 0;
655 unsigned short dev_id;
656
657 /* only search if we are a PF */
658 if (!dev->is_physfn)
659 return 0;
660
661 /*
662 * determine the device ID for the VFs, the vendor ID will be the
663 * same as the PF so there is no need to check for that one
664 */
665 pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id);
666
667 /* loop through all the VFs to see if we own any that are assigned */
668 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
669 while (vfdev) {
670 /*
671 * It is considered assigned if it is a virtual function with
672 * our dev as the physical function and the assigned bit is set
673 */
674 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
Ethan Zhaobe634972014-09-09 10:21:28 +0800675 pci_is_dev_assigned(vfdev))
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000676 vfs_assigned++;
677
678 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
679 }
680
681 return vfs_assigned;
682}
683EXPORT_SYMBOL_GPL(pci_vfs_assigned);
684
685/**
Donald Dutilebff73152012-11-05 15:20:37 -0500686 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
687 * @dev: the PCI PF device
Randy Dunlap2094f162013-01-09 17:12:52 -0800688 * @numvfs: number that should be used for TotalVFs supported
Donald Dutilebff73152012-11-05 15:20:37 -0500689 *
690 * Should be called from PF driver's probe routine with
691 * device's mutex held.
692 *
693 * Returns 0 if PF is an SRIOV-capable device and
Stefan Assmann652d1102013-07-31 16:47:56 -0600694 * value of numvfs valid. If not a PF return -ENOSYS;
695 * if numvfs is invalid return -EINVAL;
Donald Dutilebff73152012-11-05 15:20:37 -0500696 * if VFs already enabled, return -EBUSY.
697 */
698int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
699{
Stefan Assmann652d1102013-07-31 16:47:56 -0600700 if (!dev->is_physfn)
701 return -ENOSYS;
702 if (numvfs > dev->sriov->total_VFs)
Donald Dutilebff73152012-11-05 15:20:37 -0500703 return -EINVAL;
704
705 /* Shouldn't change if VFs already enabled */
706 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
707 return -EBUSY;
708 else
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700709 dev->sriov->driver_max_VFs = numvfs;
Donald Dutilebff73152012-11-05 15:20:37 -0500710
711 return 0;
712}
713EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
714
715/**
Jonghwan Choiddc191f2013-07-08 14:02:43 -0600716 * pci_sriov_get_totalvfs -- get total VFs supported on this device
Donald Dutilebff73152012-11-05 15:20:37 -0500717 * @dev: the PCI PF device
718 *
719 * For a PCIe device with SRIOV support, return the PCIe
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700720 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
Stefan Assmann652d1102013-07-31 16:47:56 -0600721 * if the driver reduced it. Otherwise 0.
Donald Dutilebff73152012-11-05 15:20:37 -0500722 */
723int pci_sriov_get_totalvfs(struct pci_dev *dev)
724{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700725 if (!dev->is_physfn)
Stefan Assmann652d1102013-07-31 16:47:56 -0600726 return 0;
Donald Dutilebff73152012-11-05 15:20:37 -0500727
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700728 if (dev->sriov->driver_max_VFs)
729 return dev->sriov->driver_max_VFs;
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700730
731 return dev->sriov->total_VFs;
Donald Dutilebff73152012-11-05 15:20:37 -0500732}
733EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);