blob: a971a6f6268d83f3ec67e1519caaa0455c83a205 [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;
78
79 virtfn = alloc_pci_dev();
80 if (!virtfn)
81 return -ENOMEM;
82
83 mutex_lock(&iov->dev->sriov->lock);
84 virtfn->bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
85 if (!virtfn->bus) {
86 kfree(virtfn);
87 mutex_unlock(&iov->dev->sriov->lock);
88 return -ENOMEM;
89 }
90 virtfn->devfn = virtfn_devfn(dev, id);
91 virtfn->vendor = dev->vendor;
92 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device);
93 pci_setup_device(virtfn);
94 virtfn->dev.parent = dev->dev.parent;
Xudong Haofbf33f52013-05-31 12:21:29 +080095 virtfn->physfn = pci_dev_get(dev);
96 virtfn->is_virtfn = 1;
Yu Zhaodd7cc442009-03-20 11:25:15 +080097
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
Yu Zhaodd7cc442009-03-20 11:25:15 +0800118 rc = pci_bus_add_device(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800119 sprintf(buf, "virtfn%u", id);
120 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
121 if (rc)
122 goto failed1;
123 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
124 if (rc)
125 goto failed2;
126
127 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
128
129 return 0;
130
131failed2:
132 sysfs_remove_link(&dev->dev.kobj, buf);
133failed1:
134 pci_dev_put(dev);
135 mutex_lock(&iov->dev->sriov->lock);
Yinghai Lu210647a2012-02-25 13:54:20 -0800136 pci_stop_and_remove_bus_device(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800137 virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
138 mutex_unlock(&iov->dev->sriov->lock);
139
140 return rc;
141}
142
143static void virtfn_remove(struct pci_dev *dev, int id, int reset)
144{
145 char buf[VIRTFN_ID_LEN];
Bjorn Helgaas94bb3462012-09-20 17:09:48 -0600146 struct pci_bus *bus;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800147 struct pci_dev *virtfn;
148 struct pci_sriov *iov = dev->sriov;
149
Bjorn Helgaas94bb3462012-09-20 17:09:48 -0600150 bus = pci_find_bus(pci_domain_nr(dev->bus), virtfn_bus(dev, id));
151 if (!bus)
152 return;
153
154 virtfn = pci_get_slot(bus, virtfn_devfn(dev, id));
Yu Zhaodd7cc442009-03-20 11:25:15 +0800155 if (!virtfn)
156 return;
157
158 pci_dev_put(virtfn);
159
160 if (reset) {
161 device_release_driver(&virtfn->dev);
Yu Zhao8c1c6992009-06-13 15:52:13 +0800162 __pci_reset_function(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800163 }
164
165 sprintf(buf, "virtfn%u", id);
166 sysfs_remove_link(&dev->dev.kobj, buf);
Yinghai Lu09cedbe2012-02-04 22:55:01 -0800167 /*
168 * pci_stop_dev() could have been called for this virtfn already,
169 * so the directory for the virtfn may have been removed before.
170 * Double check to avoid spurious sysfs warnings.
171 */
172 if (virtfn->dev.kobj.sd)
173 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
Yu Zhaodd7cc442009-03-20 11:25:15 +0800174
175 mutex_lock(&iov->dev->sriov->lock);
Yinghai Lu210647a2012-02-25 13:54:20 -0800176 pci_stop_and_remove_bus_device(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800177 virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
178 mutex_unlock(&iov->dev->sriov->lock);
179
180 pci_dev_put(dev);
181}
182
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800183static int sriov_migration(struct pci_dev *dev)
184{
185 u16 status;
186 struct pci_sriov *iov = dev->sriov;
187
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700188 if (!iov->num_VFs)
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800189 return 0;
190
191 if (!(iov->cap & PCI_SRIOV_CAP_VFM))
192 return 0;
193
194 pci_read_config_word(dev, iov->pos + PCI_SRIOV_STATUS, &status);
195 if (!(status & PCI_SRIOV_STATUS_VFM))
196 return 0;
197
198 schedule_work(&iov->mtask);
199
200 return 1;
201}
202
203static void sriov_migration_task(struct work_struct *work)
204{
205 int i;
206 u8 state;
207 u16 status;
208 struct pci_sriov *iov = container_of(work, struct pci_sriov, mtask);
209
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700210 for (i = iov->initial_VFs; i < iov->num_VFs; i++) {
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800211 state = readb(iov->mstate + i);
212 if (state == PCI_SRIOV_VFM_MI) {
213 writeb(PCI_SRIOV_VFM_AV, iov->mstate + i);
214 state = readb(iov->mstate + i);
215 if (state == PCI_SRIOV_VFM_AV)
216 virtfn_add(iov->self, i, 1);
217 } else if (state == PCI_SRIOV_VFM_MO) {
218 virtfn_remove(iov->self, i, 1);
219 writeb(PCI_SRIOV_VFM_UA, iov->mstate + i);
220 state = readb(iov->mstate + i);
221 if (state == PCI_SRIOV_VFM_AV)
222 virtfn_add(iov->self, i, 0);
223 }
224 }
225
226 pci_read_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, &status);
227 status &= ~PCI_SRIOV_STATUS_VFM;
228 pci_write_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, status);
229}
230
231static int sriov_enable_migration(struct pci_dev *dev, int nr_virtfn)
232{
233 int bir;
234 u32 table;
235 resource_size_t pa;
236 struct pci_sriov *iov = dev->sriov;
237
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700238 if (nr_virtfn <= iov->initial_VFs)
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800239 return 0;
240
241 pci_read_config_dword(dev, iov->pos + PCI_SRIOV_VFM, &table);
242 bir = PCI_SRIOV_VFM_BIR(table);
243 if (bir > PCI_STD_RESOURCE_END)
244 return -EIO;
245
246 table = PCI_SRIOV_VFM_OFFSET(table);
247 if (table + nr_virtfn > pci_resource_len(dev, bir))
248 return -EIO;
249
250 pa = pci_resource_start(dev, bir) + table;
251 iov->mstate = ioremap(pa, nr_virtfn);
252 if (!iov->mstate)
253 return -ENOMEM;
254
255 INIT_WORK(&iov->mtask, sriov_migration_task);
256
257 iov->ctrl |= PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR;
258 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
259
260 return 0;
261}
262
263static void sriov_disable_migration(struct pci_dev *dev)
264{
265 struct pci_sriov *iov = dev->sriov;
266
267 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR);
268 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
269
270 cancel_work_sync(&iov->mtask);
271 iounmap(iov->mstate);
272}
273
Yu Zhaodd7cc442009-03-20 11:25:15 +0800274static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
275{
276 int rc;
277 int i, j;
278 int nres;
279 u16 offset, stride, initial;
280 struct resource *res;
281 struct pci_dev *pdev;
282 struct pci_sriov *iov = dev->sriov;
Ram Paibbef98a2011-11-06 10:33:10 +0800283 int bars = 0;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800284
285 if (!nr_virtfn)
286 return 0;
287
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700288 if (iov->num_VFs)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800289 return -EINVAL;
290
291 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700292 if (initial > iov->total_VFs ||
293 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
Yu Zhaodd7cc442009-03-20 11:25:15 +0800294 return -EIO;
295
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700296 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
Yu Zhaodd7cc442009-03-20 11:25:15 +0800297 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
298 return -EINVAL;
299
300 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
301 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset);
302 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride);
303 if (!offset || (nr_virtfn > 1 && !stride))
304 return -EIO;
305
306 nres = 0;
307 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Ram Paibbef98a2011-11-06 10:33:10 +0800308 bars |= (1 << (i + PCI_IOV_RESOURCES));
Yu Zhaodd7cc442009-03-20 11:25:15 +0800309 res = dev->resource + PCI_IOV_RESOURCES + i;
310 if (res->parent)
311 nres++;
312 }
313 if (nres != iov->nres) {
314 dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
315 return -ENOMEM;
316 }
317
318 iov->offset = offset;
319 iov->stride = stride;
320
Yinghai Lub918c622012-05-17 18:51:11 -0700321 if (virtfn_bus(dev, nr_virtfn - 1) > dev->bus->busn_res.end) {
Yu Zhaodd7cc442009-03-20 11:25:15 +0800322 dev_err(&dev->dev, "SR-IOV: bus number out of range\n");
323 return -ENOMEM;
324 }
325
Ram Paibbef98a2011-11-06 10:33:10 +0800326 if (pci_enable_resources(dev, bars)) {
327 dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
328 return -ENOMEM;
329 }
330
Yu Zhaodd7cc442009-03-20 11:25:15 +0800331 if (iov->link != dev->devfn) {
332 pdev = pci_get_slot(dev->bus, iov->link);
333 if (!pdev)
334 return -ENODEV;
335
336 pci_dev_put(pdev);
337
338 if (!pdev->is_physfn)
339 return -ENODEV;
340
341 rc = sysfs_create_link(&dev->dev.kobj,
342 &pdev->dev.kobj, "dep_link");
343 if (rc)
344 return rc;
345 }
346
347 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100348 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800349 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
350 msleep(100);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100351 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800352
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700353 iov->initial_VFs = initial;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800354 if (nr_virtfn < initial)
355 initial = nr_virtfn;
356
357 for (i = 0; i < initial; i++) {
358 rc = virtfn_add(dev, i, 0);
359 if (rc)
360 goto failed;
361 }
362
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800363 if (iov->cap & PCI_SRIOV_CAP_VFM) {
364 rc = sriov_enable_migration(dev, nr_virtfn);
365 if (rc)
366 goto failed;
367 }
368
Yu Zhaodd7cc442009-03-20 11:25:15 +0800369 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700370 iov->num_VFs = nr_virtfn;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800371
372 return 0;
373
374failed:
375 for (j = 0; j < i; j++)
376 virtfn_remove(dev, j, 0);
377
378 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100379 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800380 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
381 ssleep(1);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100382 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800383
384 if (iov->link != dev->devfn)
385 sysfs_remove_link(&dev->dev.kobj, "dep_link");
386
387 return rc;
388}
389
390static void sriov_disable(struct pci_dev *dev)
391{
392 int i;
393 struct pci_sriov *iov = dev->sriov;
394
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700395 if (!iov->num_VFs)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800396 return;
397
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800398 if (iov->cap & PCI_SRIOV_CAP_VFM)
399 sriov_disable_migration(dev);
400
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700401 for (i = 0; i < iov->num_VFs; i++)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800402 virtfn_remove(dev, i, 0);
403
404 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100405 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800406 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
407 ssleep(1);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100408 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800409
410 if (iov->link != dev->devfn)
411 sysfs_remove_link(&dev->dev.kobj, "dep_link");
412
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700413 iov->num_VFs = 0;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800414}
415
Yu Zhaod1b054d2009-03-20 11:25:11 +0800416static int sriov_init(struct pci_dev *dev, int pos)
417{
418 int i;
419 int rc;
420 int nres;
421 u32 pgsz;
422 u16 ctrl, total, offset, stride;
423 struct pci_sriov *iov;
424 struct resource *res;
425 struct pci_dev *pdev;
426
Yijing Wang62f87c02012-07-24 17:20:03 +0800427 if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
428 pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
Yu Zhaod1b054d2009-03-20 11:25:11 +0800429 return -ENODEV;
430
431 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
432 if (ctrl & PCI_SRIOV_CTRL_VFE) {
433 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
434 ssleep(1);
435 }
436
437 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
438 if (!total)
439 return 0;
440
441 ctrl = 0;
442 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
443 if (pdev->is_physfn)
444 goto found;
445
446 pdev = NULL;
447 if (pci_ari_enabled(dev->bus))
448 ctrl |= PCI_SRIOV_CTRL_ARI;
449
450found:
451 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800452 pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset);
453 pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride);
454 if (!offset || (total > 1 && !stride))
455 return -EIO;
456
457 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
458 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
459 pgsz &= ~((1 << i) - 1);
460 if (!pgsz)
461 return -EIO;
462
463 pgsz &= ~(pgsz - 1);
Vaidyanathan Srinivasan8161fe92012-02-02 23:11:20 +0530464 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800465
466 nres = 0;
467 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
468 res = dev->resource + PCI_IOV_RESOURCES + i;
469 i += __pci_read_base(dev, pci_bar_unknown, res,
470 pos + PCI_SRIOV_BAR + i * 4);
471 if (!res->flags)
472 continue;
473 if (resource_size(res) & (PAGE_SIZE - 1)) {
474 rc = -EIO;
475 goto failed;
476 }
477 res->end = res->start + resource_size(res) * total - 1;
478 nres++;
479 }
480
481 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
482 if (!iov) {
483 rc = -ENOMEM;
484 goto failed;
485 }
486
487 iov->pos = pos;
488 iov->nres = nres;
489 iov->ctrl = ctrl;
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700490 iov->total_VFs = total;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800491 iov->offset = offset;
492 iov->stride = stride;
493 iov->pgsz = pgsz;
494 iov->self = dev;
495 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
496 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
Yijing Wang62f87c02012-07-24 17:20:03 +0800497 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
Yu Zhao4d135db2009-05-20 17:11:57 +0800498 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800499
500 if (pdev)
501 iov->dev = pci_dev_get(pdev);
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800502 else
Yu Zhaod1b054d2009-03-20 11:25:11 +0800503 iov->dev = dev;
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800504
505 mutex_init(&iov->lock);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800506
507 dev->sriov = iov;
508 dev->is_physfn = 1;
509
510 return 0;
511
512failed:
513 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
514 res = dev->resource + PCI_IOV_RESOURCES + i;
515 res->flags = 0;
516 }
517
518 return rc;
519}
520
521static void sriov_release(struct pci_dev *dev)
522{
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700523 BUG_ON(dev->sriov->num_VFs);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800524
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800525 if (dev != dev->sriov->dev)
Yu Zhaod1b054d2009-03-20 11:25:11 +0800526 pci_dev_put(dev->sriov->dev);
527
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800528 mutex_destroy(&dev->sriov->lock);
529
Yu Zhaod1b054d2009-03-20 11:25:11 +0800530 kfree(dev->sriov);
531 dev->sriov = NULL;
532}
533
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800534static void sriov_restore_state(struct pci_dev *dev)
535{
536 int i;
537 u16 ctrl;
538 struct pci_sriov *iov = dev->sriov;
539
540 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
541 if (ctrl & PCI_SRIOV_CTRL_VFE)
542 return;
543
544 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
545 pci_update_resource(dev, i);
546
547 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700548 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->num_VFs);
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800549 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
550 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
551 msleep(100);
552}
553
Yu Zhaod1b054d2009-03-20 11:25:11 +0800554/**
555 * pci_iov_init - initialize the IOV capability
556 * @dev: the PCI device
557 *
558 * Returns 0 on success, or negative on failure.
559 */
560int pci_iov_init(struct pci_dev *dev)
561{
562 int pos;
563
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900564 if (!pci_is_pcie(dev))
Yu Zhaod1b054d2009-03-20 11:25:11 +0800565 return -ENODEV;
566
567 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
568 if (pos)
569 return sriov_init(dev, pos);
570
571 return -ENODEV;
572}
573
574/**
575 * pci_iov_release - release resources used by the IOV capability
576 * @dev: the PCI device
577 */
578void pci_iov_release(struct pci_dev *dev)
579{
580 if (dev->is_physfn)
581 sriov_release(dev);
582}
583
584/**
585 * pci_iov_resource_bar - get position of the SR-IOV BAR
586 * @dev: the PCI device
587 * @resno: the resource number
588 * @type: the BAR type to be filled in
589 *
590 * Returns position of the BAR encapsulated in the SR-IOV capability.
591 */
592int pci_iov_resource_bar(struct pci_dev *dev, int resno,
593 enum pci_bar_type *type)
594{
595 if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
596 return 0;
597
598 BUG_ON(!dev->is_physfn);
599
600 *type = pci_bar_unknown;
601
602 return dev->sriov->pos + PCI_SRIOV_BAR +
603 4 * (resno - PCI_IOV_RESOURCES);
604}
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800605
606/**
Chris Wright6faf17f2009-08-28 13:00:06 -0700607 * pci_sriov_resource_alignment - get resource alignment for VF BAR
608 * @dev: the PCI device
609 * @resno: the resource number
610 *
611 * Returns the alignment of the VF BAR found in the SR-IOV capability.
612 * This is not the same as the resource size which is defined as
613 * the VF BAR size multiplied by the number of VFs. The alignment
614 * is just the VF BAR size.
615 */
Cam Macdonell0e522472010-09-07 17:25:20 -0700616resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
Chris Wright6faf17f2009-08-28 13:00:06 -0700617{
618 struct resource tmp;
619 enum pci_bar_type type;
620 int reg = pci_iov_resource_bar(dev, resno, &type);
621
622 if (!reg)
623 return 0;
624
625 __pci_read_base(dev, type, &tmp, reg);
626 return resource_alignment(&tmp);
627}
628
629/**
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800630 * pci_restore_iov_state - restore the state of the IOV capability
631 * @dev: the PCI device
632 */
633void pci_restore_iov_state(struct pci_dev *dev)
634{
635 if (dev->is_physfn)
636 sriov_restore_state(dev);
637}
Yu Zhaoa28724b2009-03-20 11:25:13 +0800638
639/**
640 * pci_iov_bus_range - find bus range used by Virtual Function
641 * @bus: the PCI bus
642 *
643 * Returns max number of buses (exclude current one) used by Virtual
644 * Functions.
645 */
646int pci_iov_bus_range(struct pci_bus *bus)
647{
648 int max = 0;
649 u8 busnr;
650 struct pci_dev *dev;
651
652 list_for_each_entry(dev, &bus->devices, bus_list) {
653 if (!dev->is_physfn)
654 continue;
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700655 busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
Yu Zhaoa28724b2009-03-20 11:25:13 +0800656 if (busnr > max)
657 max = busnr;
658 }
659
660 return max ? max - bus->number : 0;
661}
Yu Zhaodd7cc442009-03-20 11:25:15 +0800662
663/**
664 * pci_enable_sriov - enable the SR-IOV capability
665 * @dev: the PCI device
Randy Dunlap52a88732009-04-01 17:45:30 -0700666 * @nr_virtfn: number of virtual functions to enable
Yu Zhaodd7cc442009-03-20 11:25:15 +0800667 *
668 * Returns 0 on success, or negative on failure.
669 */
670int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
671{
672 might_sleep();
673
674 if (!dev->is_physfn)
675 return -ENODEV;
676
677 return sriov_enable(dev, nr_virtfn);
678}
679EXPORT_SYMBOL_GPL(pci_enable_sriov);
680
681/**
682 * pci_disable_sriov - disable the SR-IOV capability
683 * @dev: the PCI device
684 */
685void pci_disable_sriov(struct pci_dev *dev)
686{
687 might_sleep();
688
689 if (!dev->is_physfn)
690 return;
691
692 sriov_disable(dev);
693}
694EXPORT_SYMBOL_GPL(pci_disable_sriov);
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800695
696/**
697 * pci_sriov_migration - notify SR-IOV core of Virtual Function Migration
698 * @dev: the PCI device
699 *
700 * Returns IRQ_HANDLED if the IRQ is handled, or IRQ_NONE if not.
701 *
702 * Physical Function driver is responsible to register IRQ handler using
703 * VF Migration Interrupt Message Number, and call this function when the
704 * interrupt is generated by the hardware.
705 */
706irqreturn_t pci_sriov_migration(struct pci_dev *dev)
707{
708 if (!dev->is_physfn)
709 return IRQ_NONE;
710
711 return sriov_migration(dev) ? IRQ_HANDLED : IRQ_NONE;
712}
713EXPORT_SYMBOL_GPL(pci_sriov_migration);
Yu Zhao302b4212009-05-18 13:51:32 +0800714
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000715/**
716 * pci_num_vf - return number of VFs associated with a PF device_release_driver
717 * @dev: the PCI device
718 *
719 * Returns number of VFs, or 0 if SR-IOV is not enabled.
720 */
721int pci_num_vf(struct pci_dev *dev)
722{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700723 if (!dev->is_physfn)
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000724 return 0;
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700725
726 return dev->sriov->num_VFs;
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000727}
728EXPORT_SYMBOL_GPL(pci_num_vf);
Donald Dutilebff73152012-11-05 15:20:37 -0500729
730/**
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000731 * pci_vfs_assigned - returns number of VFs are assigned to a guest
732 * @dev: the PCI device
733 *
734 * Returns number of VFs belonging to this device that are assigned to a guest.
735 * If device is not a physical function returns -ENODEV.
736 */
737int pci_vfs_assigned(struct pci_dev *dev)
738{
739 struct pci_dev *vfdev;
740 unsigned int vfs_assigned = 0;
741 unsigned short dev_id;
742
743 /* only search if we are a PF */
744 if (!dev->is_physfn)
745 return 0;
746
747 /*
748 * determine the device ID for the VFs, the vendor ID will be the
749 * same as the PF so there is no need to check for that one
750 */
751 pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id);
752
753 /* loop through all the VFs to see if we own any that are assigned */
754 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
755 while (vfdev) {
756 /*
757 * It is considered assigned if it is a virtual function with
758 * our dev as the physical function and the assigned bit is set
759 */
760 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
761 (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED))
762 vfs_assigned++;
763
764 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
765 }
766
767 return vfs_assigned;
768}
769EXPORT_SYMBOL_GPL(pci_vfs_assigned);
770
771/**
Donald Dutilebff73152012-11-05 15:20:37 -0500772 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
773 * @dev: the PCI PF device
Randy Dunlap2094f162013-01-09 17:12:52 -0800774 * @numvfs: number that should be used for TotalVFs supported
Donald Dutilebff73152012-11-05 15:20:37 -0500775 *
776 * Should be called from PF driver's probe routine with
777 * device's mutex held.
778 *
779 * Returns 0 if PF is an SRIOV-capable device and
780 * value of numvfs valid. If not a PF with VFS, return -EINVAL;
781 * if VFs already enabled, return -EBUSY.
782 */
783int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
784{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700785 if (!dev->is_physfn || (numvfs > dev->sriov->total_VFs))
Donald Dutilebff73152012-11-05 15:20:37 -0500786 return -EINVAL;
787
788 /* Shouldn't change if VFs already enabled */
789 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
790 return -EBUSY;
791 else
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700792 dev->sriov->driver_max_VFs = numvfs;
Donald Dutilebff73152012-11-05 15:20:37 -0500793
794 return 0;
795}
796EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
797
798/**
799 * pci_sriov_get_totalvfs -- get total VFs supported on this devic3
800 * @dev: the PCI PF device
801 *
802 * For a PCIe device with SRIOV support, return the PCIe
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700803 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
Donald Dutilebff73152012-11-05 15:20:37 -0500804 * if the driver reduced it. Otherwise, -EINVAL.
805 */
806int pci_sriov_get_totalvfs(struct pci_dev *dev)
807{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700808 if (!dev->is_physfn)
Donald Dutilebff73152012-11-05 15:20:37 -0500809 return -EINVAL;
810
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700811 if (dev->sriov->driver_max_VFs)
812 return dev->sriov->driver_max_VFs;
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700813
814 return dev->sriov->total_VFs;
Donald Dutilebff73152012-11-05 15:20:37 -0500815}
816EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);