blob: 2652ca00fae740d510b0edcbf6256d3bb78d3525 [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
Yu Zhaodd7cc442009-03-20 11:25:15 +080034static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
35{
Yu Zhaodd7cc442009-03-20 11:25:15 +080036 struct pci_bus *child;
37
38 if (bus->number == busnr)
39 return bus;
40
41 child = pci_find_bus(pci_domain_nr(bus), busnr);
42 if (child)
43 return child;
44
45 child = pci_add_new_bus(bus, NULL, busnr);
46 if (!child)
47 return NULL;
48
Yinghai Lub7eac052012-05-17 18:51:13 -070049 pci_bus_insert_busn_res(child, busnr, busnr);
Yinghai Lu4f535092013-01-21 13:20:52 -080050 bus->is_added = 1;
Yu Zhaodd7cc442009-03-20 11:25:15 +080051
52 return child;
53}
54
55static void virtfn_remove_bus(struct pci_bus *bus, int busnr)
56{
57 struct pci_bus *child;
58
59 if (bus->number == busnr)
60 return;
61
62 child = pci_find_bus(pci_domain_nr(bus), busnr);
63 BUG_ON(!child);
64
65 if (list_empty(&child->devices))
66 pci_remove_bus(child);
67}
68
69static int virtfn_add(struct pci_dev *dev, int id, int reset)
70{
71 int i;
72 int rc;
73 u64 size;
74 char buf[VIRTFN_ID_LEN];
75 struct pci_dev *virtfn;
76 struct resource *res;
77 struct pci_sriov *iov = dev->sriov;
Gu Zheng8b1fce02013-05-25 21:48:31 +080078 struct pci_bus *bus;
Yu Zhaodd7cc442009-03-20 11:25:15 +080079
Gu Zheng8b1fce02013-05-25 21:48:31 +080080 virtfn = pci_alloc_dev(NULL);
Yu Zhaodd7cc442009-03-20 11:25:15 +080081 if (!virtfn)
82 return -ENOMEM;
83
84 mutex_lock(&iov->dev->sriov->lock);
Gu Zheng8b1fce02013-05-25 21:48:31 +080085 bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
86 if (!bus) {
Yu Zhaodd7cc442009-03-20 11:25:15 +080087 kfree(virtfn);
88 mutex_unlock(&iov->dev->sriov->lock);
89 return -ENOMEM;
90 }
Gu Zheng8b1fce02013-05-25 21:48:31 +080091 virtfn->bus = pci_bus_get(bus);
Yu Zhaodd7cc442009-03-20 11:25:15 +080092 virtfn->devfn = virtfn_devfn(dev, id);
93 virtfn->vendor = dev->vendor;
94 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device);
95 pci_setup_device(virtfn);
96 virtfn->dev.parent = dev->dev.parent;
97
98 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
99 res = dev->resource + PCI_IOV_RESOURCES + i;
100 if (!res->parent)
101 continue;
102 virtfn->resource[i].name = pci_name(virtfn);
103 virtfn->resource[i].flags = res->flags;
104 size = resource_size(res);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700105 do_div(size, iov->total_VFs);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800106 virtfn->resource[i].start = res->start + size * id;
107 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
108 rc = request_resource(res, &virtfn->resource[i]);
109 BUG_ON(rc);
110 }
111
112 if (reset)
Yu Zhao8c1c6992009-06-13 15:52:13 +0800113 __pci_reset_function(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800114
115 pci_device_add(virtfn, virtfn->bus);
116 mutex_unlock(&iov->dev->sriov->lock);
117
118 virtfn->physfn = pci_dev_get(dev);
119 virtfn->is_virtfn = 1;
120
121 rc = pci_bus_add_device(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800122 sprintf(buf, "virtfn%u", id);
123 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
124 if (rc)
125 goto failed1;
126 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
127 if (rc)
128 goto failed2;
129
130 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
131
132 return 0;
133
134failed2:
135 sysfs_remove_link(&dev->dev.kobj, buf);
136failed1:
137 pci_dev_put(dev);
138 mutex_lock(&iov->dev->sriov->lock);
Yinghai Lu210647a2012-02-25 13:54:20 -0800139 pci_stop_and_remove_bus_device(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800140 virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
141 mutex_unlock(&iov->dev->sriov->lock);
142
143 return rc;
144}
145
146static void virtfn_remove(struct pci_dev *dev, int id, int reset)
147{
148 char buf[VIRTFN_ID_LEN];
Bjorn Helgaas94bb3462012-09-20 17:09:48 -0600149 struct pci_bus *bus;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800150 struct pci_dev *virtfn;
151 struct pci_sriov *iov = dev->sriov;
152
Bjorn Helgaas94bb3462012-09-20 17:09:48 -0600153 bus = pci_find_bus(pci_domain_nr(dev->bus), virtfn_bus(dev, id));
154 if (!bus)
155 return;
156
157 virtfn = pci_get_slot(bus, virtfn_devfn(dev, id));
Yu Zhaodd7cc442009-03-20 11:25:15 +0800158 if (!virtfn)
159 return;
160
161 pci_dev_put(virtfn);
162
163 if (reset) {
164 device_release_driver(&virtfn->dev);
Yu Zhao8c1c6992009-06-13 15:52:13 +0800165 __pci_reset_function(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800166 }
167
168 sprintf(buf, "virtfn%u", id);
169 sysfs_remove_link(&dev->dev.kobj, buf);
Yinghai Lu09cedbe2012-02-04 22:55:01 -0800170 /*
171 * pci_stop_dev() could have been called for this virtfn already,
172 * so the directory for the virtfn may have been removed before.
173 * Double check to avoid spurious sysfs warnings.
174 */
175 if (virtfn->dev.kobj.sd)
176 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
Yu Zhaodd7cc442009-03-20 11:25:15 +0800177
178 mutex_lock(&iov->dev->sriov->lock);
Yinghai Lu210647a2012-02-25 13:54:20 -0800179 pci_stop_and_remove_bus_device(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800180 virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
181 mutex_unlock(&iov->dev->sriov->lock);
182
183 pci_dev_put(dev);
184}
185
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800186static int sriov_migration(struct pci_dev *dev)
187{
188 u16 status;
189 struct pci_sriov *iov = dev->sriov;
190
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700191 if (!iov->num_VFs)
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800192 return 0;
193
194 if (!(iov->cap & PCI_SRIOV_CAP_VFM))
195 return 0;
196
197 pci_read_config_word(dev, iov->pos + PCI_SRIOV_STATUS, &status);
198 if (!(status & PCI_SRIOV_STATUS_VFM))
199 return 0;
200
201 schedule_work(&iov->mtask);
202
203 return 1;
204}
205
206static void sriov_migration_task(struct work_struct *work)
207{
208 int i;
209 u8 state;
210 u16 status;
211 struct pci_sriov *iov = container_of(work, struct pci_sriov, mtask);
212
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700213 for (i = iov->initial_VFs; i < iov->num_VFs; i++) {
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800214 state = readb(iov->mstate + i);
215 if (state == PCI_SRIOV_VFM_MI) {
216 writeb(PCI_SRIOV_VFM_AV, iov->mstate + i);
217 state = readb(iov->mstate + i);
218 if (state == PCI_SRIOV_VFM_AV)
219 virtfn_add(iov->self, i, 1);
220 } else if (state == PCI_SRIOV_VFM_MO) {
221 virtfn_remove(iov->self, i, 1);
222 writeb(PCI_SRIOV_VFM_UA, iov->mstate + i);
223 state = readb(iov->mstate + i);
224 if (state == PCI_SRIOV_VFM_AV)
225 virtfn_add(iov->self, i, 0);
226 }
227 }
228
229 pci_read_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, &status);
230 status &= ~PCI_SRIOV_STATUS_VFM;
231 pci_write_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, status);
232}
233
234static int sriov_enable_migration(struct pci_dev *dev, int nr_virtfn)
235{
236 int bir;
237 u32 table;
238 resource_size_t pa;
239 struct pci_sriov *iov = dev->sriov;
240
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700241 if (nr_virtfn <= iov->initial_VFs)
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800242 return 0;
243
244 pci_read_config_dword(dev, iov->pos + PCI_SRIOV_VFM, &table);
245 bir = PCI_SRIOV_VFM_BIR(table);
246 if (bir > PCI_STD_RESOURCE_END)
247 return -EIO;
248
249 table = PCI_SRIOV_VFM_OFFSET(table);
250 if (table + nr_virtfn > pci_resource_len(dev, bir))
251 return -EIO;
252
253 pa = pci_resource_start(dev, bir) + table;
254 iov->mstate = ioremap(pa, nr_virtfn);
255 if (!iov->mstate)
256 return -ENOMEM;
257
258 INIT_WORK(&iov->mtask, sriov_migration_task);
259
260 iov->ctrl |= PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR;
261 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
262
263 return 0;
264}
265
266static void sriov_disable_migration(struct pci_dev *dev)
267{
268 struct pci_sriov *iov = dev->sriov;
269
270 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR);
271 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
272
273 cancel_work_sync(&iov->mtask);
274 iounmap(iov->mstate);
275}
276
Yu Zhaodd7cc442009-03-20 11:25:15 +0800277static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
278{
279 int rc;
280 int i, j;
281 int nres;
282 u16 offset, stride, initial;
283 struct resource *res;
284 struct pci_dev *pdev;
285 struct pci_sriov *iov = dev->sriov;
Ram Paibbef98a2011-11-06 10:33:10 +0800286 int bars = 0;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800287
288 if (!nr_virtfn)
289 return 0;
290
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700291 if (iov->num_VFs)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800292 return -EINVAL;
293
294 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700295 if (initial > iov->total_VFs ||
296 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
Yu Zhaodd7cc442009-03-20 11:25:15 +0800297 return -EIO;
298
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700299 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
Yu Zhaodd7cc442009-03-20 11:25:15 +0800300 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
301 return -EINVAL;
302
303 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
304 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset);
305 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride);
306 if (!offset || (nr_virtfn > 1 && !stride))
307 return -EIO;
308
309 nres = 0;
310 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Ram Paibbef98a2011-11-06 10:33:10 +0800311 bars |= (1 << (i + PCI_IOV_RESOURCES));
Yu Zhaodd7cc442009-03-20 11:25:15 +0800312 res = dev->resource + PCI_IOV_RESOURCES + i;
313 if (res->parent)
314 nres++;
315 }
316 if (nres != iov->nres) {
317 dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
318 return -ENOMEM;
319 }
320
321 iov->offset = offset;
322 iov->stride = stride;
323
Yinghai Lub918c622012-05-17 18:51:11 -0700324 if (virtfn_bus(dev, nr_virtfn - 1) > dev->bus->busn_res.end) {
Yu Zhaodd7cc442009-03-20 11:25:15 +0800325 dev_err(&dev->dev, "SR-IOV: bus number out of range\n");
326 return -ENOMEM;
327 }
328
Ram Paibbef98a2011-11-06 10:33:10 +0800329 if (pci_enable_resources(dev, bars)) {
330 dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
331 return -ENOMEM;
332 }
333
Yu Zhaodd7cc442009-03-20 11:25:15 +0800334 if (iov->link != dev->devfn) {
335 pdev = pci_get_slot(dev->bus, iov->link);
336 if (!pdev)
337 return -ENODEV;
338
339 pci_dev_put(pdev);
340
341 if (!pdev->is_physfn)
342 return -ENODEV;
343
344 rc = sysfs_create_link(&dev->dev.kobj,
345 &pdev->dev.kobj, "dep_link");
346 if (rc)
347 return rc;
348 }
349
350 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100351 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800352 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
353 msleep(100);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100354 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800355
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700356 iov->initial_VFs = initial;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800357 if (nr_virtfn < initial)
358 initial = nr_virtfn;
359
360 for (i = 0; i < initial; i++) {
361 rc = virtfn_add(dev, i, 0);
362 if (rc)
363 goto failed;
364 }
365
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800366 if (iov->cap & PCI_SRIOV_CAP_VFM) {
367 rc = sriov_enable_migration(dev, nr_virtfn);
368 if (rc)
369 goto failed;
370 }
371
Yu Zhaodd7cc442009-03-20 11:25:15 +0800372 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700373 iov->num_VFs = nr_virtfn;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800374
375 return 0;
376
377failed:
378 for (j = 0; j < i; j++)
379 virtfn_remove(dev, j, 0);
380
381 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100382 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800383 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
384 ssleep(1);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100385 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800386
387 if (iov->link != dev->devfn)
388 sysfs_remove_link(&dev->dev.kobj, "dep_link");
389
390 return rc;
391}
392
393static void sriov_disable(struct pci_dev *dev)
394{
395 int i;
396 struct pci_sriov *iov = dev->sriov;
397
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700398 if (!iov->num_VFs)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800399 return;
400
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800401 if (iov->cap & PCI_SRIOV_CAP_VFM)
402 sriov_disable_migration(dev);
403
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700404 for (i = 0; i < iov->num_VFs; i++)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800405 virtfn_remove(dev, i, 0);
406
407 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100408 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800409 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
410 ssleep(1);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100411 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800412
413 if (iov->link != dev->devfn)
414 sysfs_remove_link(&dev->dev.kobj, "dep_link");
415
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700416 iov->num_VFs = 0;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800417}
418
Yu Zhaod1b054d2009-03-20 11:25:11 +0800419static int sriov_init(struct pci_dev *dev, int pos)
420{
421 int i;
422 int rc;
423 int nres;
424 u32 pgsz;
425 u16 ctrl, total, offset, stride;
426 struct pci_sriov *iov;
427 struct resource *res;
428 struct pci_dev *pdev;
429
Yijing Wang62f87c02012-07-24 17:20:03 +0800430 if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
431 pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
Yu Zhaod1b054d2009-03-20 11:25:11 +0800432 return -ENODEV;
433
434 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
435 if (ctrl & PCI_SRIOV_CTRL_VFE) {
436 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
437 ssleep(1);
438 }
439
440 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
441 if (!total)
442 return 0;
443
444 ctrl = 0;
445 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
446 if (pdev->is_physfn)
447 goto found;
448
449 pdev = NULL;
450 if (pci_ari_enabled(dev->bus))
451 ctrl |= PCI_SRIOV_CTRL_ARI;
452
453found:
454 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800455 pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset);
456 pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride);
457 if (!offset || (total > 1 && !stride))
458 return -EIO;
459
460 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
461 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
462 pgsz &= ~((1 << i) - 1);
463 if (!pgsz)
464 return -EIO;
465
466 pgsz &= ~(pgsz - 1);
Vaidyanathan Srinivasan8161fe92012-02-02 23:11:20 +0530467 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800468
469 nres = 0;
470 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
471 res = dev->resource + PCI_IOV_RESOURCES + i;
472 i += __pci_read_base(dev, pci_bar_unknown, res,
473 pos + PCI_SRIOV_BAR + i * 4);
474 if (!res->flags)
475 continue;
476 if (resource_size(res) & (PAGE_SIZE - 1)) {
477 rc = -EIO;
478 goto failed;
479 }
480 res->end = res->start + resource_size(res) * total - 1;
481 nres++;
482 }
483
484 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
485 if (!iov) {
486 rc = -ENOMEM;
487 goto failed;
488 }
489
490 iov->pos = pos;
491 iov->nres = nres;
492 iov->ctrl = ctrl;
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700493 iov->total_VFs = total;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800494 iov->offset = offset;
495 iov->stride = stride;
496 iov->pgsz = pgsz;
497 iov->self = dev;
498 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
499 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
Yijing Wang62f87c02012-07-24 17:20:03 +0800500 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
Yu Zhao4d135db2009-05-20 17:11:57 +0800501 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800502
503 if (pdev)
504 iov->dev = pci_dev_get(pdev);
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800505 else
Yu Zhaod1b054d2009-03-20 11:25:11 +0800506 iov->dev = dev;
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800507
508 mutex_init(&iov->lock);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800509
510 dev->sriov = iov;
511 dev->is_physfn = 1;
512
513 return 0;
514
515failed:
516 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
517 res = dev->resource + PCI_IOV_RESOURCES + i;
518 res->flags = 0;
519 }
520
521 return rc;
522}
523
524static void sriov_release(struct pci_dev *dev)
525{
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700526 BUG_ON(dev->sriov->num_VFs);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800527
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800528 if (dev != dev->sriov->dev)
Yu Zhaod1b054d2009-03-20 11:25:11 +0800529 pci_dev_put(dev->sriov->dev);
530
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800531 mutex_destroy(&dev->sriov->lock);
532
Yu Zhaod1b054d2009-03-20 11:25:11 +0800533 kfree(dev->sriov);
534 dev->sriov = NULL;
535}
536
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800537static void sriov_restore_state(struct pci_dev *dev)
538{
539 int i;
540 u16 ctrl;
541 struct pci_sriov *iov = dev->sriov;
542
543 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
544 if (ctrl & PCI_SRIOV_CTRL_VFE)
545 return;
546
547 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
548 pci_update_resource(dev, i);
549
550 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700551 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->num_VFs);
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800552 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
553 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
554 msleep(100);
555}
556
Yu Zhaod1b054d2009-03-20 11:25:11 +0800557/**
558 * pci_iov_init - initialize the IOV capability
559 * @dev: the PCI device
560 *
561 * Returns 0 on success, or negative on failure.
562 */
563int pci_iov_init(struct pci_dev *dev)
564{
565 int pos;
566
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900567 if (!pci_is_pcie(dev))
Yu Zhaod1b054d2009-03-20 11:25:11 +0800568 return -ENODEV;
569
570 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
571 if (pos)
572 return sriov_init(dev, pos);
573
574 return -ENODEV;
575}
576
577/**
578 * pci_iov_release - release resources used by the IOV capability
579 * @dev: the PCI device
580 */
581void pci_iov_release(struct pci_dev *dev)
582{
583 if (dev->is_physfn)
584 sriov_release(dev);
585}
586
587/**
588 * pci_iov_resource_bar - get position of the SR-IOV BAR
589 * @dev: the PCI device
590 * @resno: the resource number
591 * @type: the BAR type to be filled in
592 *
593 * Returns position of the BAR encapsulated in the SR-IOV capability.
594 */
595int pci_iov_resource_bar(struct pci_dev *dev, int resno,
596 enum pci_bar_type *type)
597{
598 if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
599 return 0;
600
601 BUG_ON(!dev->is_physfn);
602
603 *type = pci_bar_unknown;
604
605 return dev->sriov->pos + PCI_SRIOV_BAR +
606 4 * (resno - PCI_IOV_RESOURCES);
607}
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800608
609/**
Chris Wright6faf17f2009-08-28 13:00:06 -0700610 * pci_sriov_resource_alignment - get resource alignment for VF BAR
611 * @dev: the PCI device
612 * @resno: the resource number
613 *
614 * Returns the alignment of the VF BAR found in the SR-IOV capability.
615 * This is not the same as the resource size which is defined as
616 * the VF BAR size multiplied by the number of VFs. The alignment
617 * is just the VF BAR size.
618 */
Cam Macdonell0e522472010-09-07 17:25:20 -0700619resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
Chris Wright6faf17f2009-08-28 13:00:06 -0700620{
621 struct resource tmp;
622 enum pci_bar_type type;
623 int reg = pci_iov_resource_bar(dev, resno, &type);
624
625 if (!reg)
626 return 0;
627
628 __pci_read_base(dev, type, &tmp, reg);
629 return resource_alignment(&tmp);
630}
631
632/**
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800633 * pci_restore_iov_state - restore the state of the IOV capability
634 * @dev: the PCI device
635 */
636void pci_restore_iov_state(struct pci_dev *dev)
637{
638 if (dev->is_physfn)
639 sriov_restore_state(dev);
640}
Yu Zhaoa28724b2009-03-20 11:25:13 +0800641
642/**
643 * pci_iov_bus_range - find bus range used by Virtual Function
644 * @bus: the PCI bus
645 *
646 * Returns max number of buses (exclude current one) used by Virtual
647 * Functions.
648 */
649int pci_iov_bus_range(struct pci_bus *bus)
650{
651 int max = 0;
652 u8 busnr;
653 struct pci_dev *dev;
654
655 list_for_each_entry(dev, &bus->devices, bus_list) {
656 if (!dev->is_physfn)
657 continue;
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700658 busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
Yu Zhaoa28724b2009-03-20 11:25:13 +0800659 if (busnr > max)
660 max = busnr;
661 }
662
663 return max ? max - bus->number : 0;
664}
Yu Zhaodd7cc442009-03-20 11:25:15 +0800665
666/**
667 * pci_enable_sriov - enable the SR-IOV capability
668 * @dev: the PCI device
Randy Dunlap52a88732009-04-01 17:45:30 -0700669 * @nr_virtfn: number of virtual functions to enable
Yu Zhaodd7cc442009-03-20 11:25:15 +0800670 *
671 * Returns 0 on success, or negative on failure.
672 */
673int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
674{
675 might_sleep();
676
677 if (!dev->is_physfn)
678 return -ENODEV;
679
680 return sriov_enable(dev, nr_virtfn);
681}
682EXPORT_SYMBOL_GPL(pci_enable_sriov);
683
684/**
685 * pci_disable_sriov - disable the SR-IOV capability
686 * @dev: the PCI device
687 */
688void pci_disable_sriov(struct pci_dev *dev)
689{
690 might_sleep();
691
692 if (!dev->is_physfn)
693 return;
694
695 sriov_disable(dev);
696}
697EXPORT_SYMBOL_GPL(pci_disable_sriov);
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800698
699/**
700 * pci_sriov_migration - notify SR-IOV core of Virtual Function Migration
701 * @dev: the PCI device
702 *
703 * Returns IRQ_HANDLED if the IRQ is handled, or IRQ_NONE if not.
704 *
705 * Physical Function driver is responsible to register IRQ handler using
706 * VF Migration Interrupt Message Number, and call this function when the
707 * interrupt is generated by the hardware.
708 */
709irqreturn_t pci_sriov_migration(struct pci_dev *dev)
710{
711 if (!dev->is_physfn)
712 return IRQ_NONE;
713
714 return sriov_migration(dev) ? IRQ_HANDLED : IRQ_NONE;
715}
716EXPORT_SYMBOL_GPL(pci_sriov_migration);
Yu Zhao302b4212009-05-18 13:51:32 +0800717
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000718/**
719 * pci_num_vf - return number of VFs associated with a PF device_release_driver
720 * @dev: the PCI device
721 *
722 * Returns number of VFs, or 0 if SR-IOV is not enabled.
723 */
724int pci_num_vf(struct pci_dev *dev)
725{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700726 if (!dev->is_physfn)
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000727 return 0;
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700728
729 return dev->sriov->num_VFs;
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000730}
731EXPORT_SYMBOL_GPL(pci_num_vf);
Donald Dutilebff73152012-11-05 15:20:37 -0500732
733/**
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000734 * pci_vfs_assigned - returns number of VFs are assigned to a guest
735 * @dev: the PCI device
736 *
737 * Returns number of VFs belonging to this device that are assigned to a guest.
738 * If device is not a physical function returns -ENODEV.
739 */
740int pci_vfs_assigned(struct pci_dev *dev)
741{
742 struct pci_dev *vfdev;
743 unsigned int vfs_assigned = 0;
744 unsigned short dev_id;
745
746 /* only search if we are a PF */
747 if (!dev->is_physfn)
748 return 0;
749
750 /*
751 * determine the device ID for the VFs, the vendor ID will be the
752 * same as the PF so there is no need to check for that one
753 */
754 pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id);
755
756 /* loop through all the VFs to see if we own any that are assigned */
757 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
758 while (vfdev) {
759 /*
760 * It is considered assigned if it is a virtual function with
761 * our dev as the physical function and the assigned bit is set
762 */
763 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
764 (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED))
765 vfs_assigned++;
766
767 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
768 }
769
770 return vfs_assigned;
771}
772EXPORT_SYMBOL_GPL(pci_vfs_assigned);
773
774/**
Donald Dutilebff73152012-11-05 15:20:37 -0500775 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
776 * @dev: the PCI PF device
Randy Dunlap2094f162013-01-09 17:12:52 -0800777 * @numvfs: number that should be used for TotalVFs supported
Donald Dutilebff73152012-11-05 15:20:37 -0500778 *
779 * Should be called from PF driver's probe routine with
780 * device's mutex held.
781 *
782 * Returns 0 if PF is an SRIOV-capable device and
783 * value of numvfs valid. If not a PF with VFS, return -EINVAL;
784 * if VFs already enabled, return -EBUSY.
785 */
786int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
787{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700788 if (!dev->is_physfn || (numvfs > dev->sriov->total_VFs))
Donald Dutilebff73152012-11-05 15:20:37 -0500789 return -EINVAL;
790
791 /* Shouldn't change if VFs already enabled */
792 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
793 return -EBUSY;
794 else
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700795 dev->sriov->driver_max_VFs = numvfs;
Donald Dutilebff73152012-11-05 15:20:37 -0500796
797 return 0;
798}
799EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
800
801/**
802 * pci_sriov_get_totalvfs -- get total VFs supported on this devic3
803 * @dev: the PCI PF device
804 *
805 * For a PCIe device with SRIOV support, return the PCIe
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700806 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
Donald Dutilebff73152012-11-05 15:20:37 -0500807 * if the driver reduced it. Otherwise, -EINVAL.
808 */
809int pci_sriov_get_totalvfs(struct pci_dev *dev)
810{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700811 if (!dev->is_physfn)
Donald Dutilebff73152012-11-05 15:20:37 -0500812 return -EINVAL;
813
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700814 if (dev->sriov->driver_max_VFs)
815 return dev->sriov->driver_max_VFs;
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700816
817 return dev->sriov->total_VFs;
Donald Dutilebff73152012-11-05 15:20:37 -0500818}
819EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);