blob: 8f1e1174b71fd29fca2c412445f461c8a80796a2 [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);
Yu Zhaodd7cc442009-03-20 11:25:15 +080050
51 return child;
52}
53
54static void virtfn_remove_bus(struct pci_bus *bus, int busnr)
55{
56 struct pci_bus *child;
57
58 if (bus->number == busnr)
59 return;
60
61 child = pci_find_bus(pci_domain_nr(bus), busnr);
62 BUG_ON(!child);
63
64 if (list_empty(&child->devices))
65 pci_remove_bus(child);
66}
67
68static int virtfn_add(struct pci_dev *dev, int id, int reset)
69{
70 int i;
71 int rc;
72 u64 size;
73 char buf[VIRTFN_ID_LEN];
74 struct pci_dev *virtfn;
75 struct resource *res;
76 struct pci_sriov *iov = dev->sriov;
Gu Zheng8b1fce02013-05-25 21:48:31 +080077 struct pci_bus *bus;
Yu Zhaodd7cc442009-03-20 11:25:15 +080078
Gu Zheng8b1fce02013-05-25 21:48:31 +080079 virtfn = pci_alloc_dev(NULL);
Yu Zhaodd7cc442009-03-20 11:25:15 +080080 if (!virtfn)
81 return -ENOMEM;
82
83 mutex_lock(&iov->dev->sriov->lock);
Gu Zheng8b1fce02013-05-25 21:48:31 +080084 bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
85 if (!bus) {
Yu Zhaodd7cc442009-03-20 11:25:15 +080086 kfree(virtfn);
87 mutex_unlock(&iov->dev->sriov->lock);
88 return -ENOMEM;
89 }
Gu Zheng8b1fce02013-05-25 21:48:31 +080090 virtfn->bus = pci_bus_get(bus);
Yu Zhaodd7cc442009-03-20 11:25:15 +080091 virtfn->devfn = virtfn_devfn(dev, id);
92 virtfn->vendor = dev->vendor;
93 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device);
94 pci_setup_device(virtfn);
95 virtfn->dev.parent = dev->dev.parent;
96
97 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
98 res = dev->resource + PCI_IOV_RESOURCES + i;
99 if (!res->parent)
100 continue;
101 virtfn->resource[i].name = pci_name(virtfn);
102 virtfn->resource[i].flags = res->flags;
103 size = resource_size(res);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700104 do_div(size, iov->total_VFs);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800105 virtfn->resource[i].start = res->start + size * id;
106 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
107 rc = request_resource(res, &virtfn->resource[i]);
108 BUG_ON(rc);
109 }
110
111 if (reset)
Yu Zhao8c1c6992009-06-13 15:52:13 +0800112 __pci_reset_function(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800113
114 pci_device_add(virtfn, virtfn->bus);
115 mutex_unlock(&iov->dev->sriov->lock);
116
117 virtfn->physfn = pci_dev_get(dev);
118 virtfn->is_virtfn = 1;
119
120 rc = pci_bus_add_device(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800121 sprintf(buf, "virtfn%u", id);
122 rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
123 if (rc)
124 goto failed1;
125 rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
126 if (rc)
127 goto failed2;
128
129 kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
130
131 return 0;
132
133failed2:
134 sysfs_remove_link(&dev->dev.kobj, buf);
135failed1:
136 pci_dev_put(dev);
137 mutex_lock(&iov->dev->sriov->lock);
Yinghai Lu210647a2012-02-25 13:54:20 -0800138 pci_stop_and_remove_bus_device(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800139 virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
140 mutex_unlock(&iov->dev->sriov->lock);
141
142 return rc;
143}
144
145static void virtfn_remove(struct pci_dev *dev, int id, int reset)
146{
147 char buf[VIRTFN_ID_LEN];
Bjorn Helgaas94bb3462012-09-20 17:09:48 -0600148 struct pci_bus *bus;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800149 struct pci_dev *virtfn;
150 struct pci_sriov *iov = dev->sriov;
151
Bjorn Helgaas94bb3462012-09-20 17:09:48 -0600152 bus = pci_find_bus(pci_domain_nr(dev->bus), virtfn_bus(dev, id));
153 if (!bus)
154 return;
155
156 virtfn = pci_get_slot(bus, virtfn_devfn(dev, id));
Yu Zhaodd7cc442009-03-20 11:25:15 +0800157 if (!virtfn)
158 return;
159
160 pci_dev_put(virtfn);
161
162 if (reset) {
163 device_release_driver(&virtfn->dev);
Yu Zhao8c1c6992009-06-13 15:52:13 +0800164 __pci_reset_function(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800165 }
166
167 sprintf(buf, "virtfn%u", id);
168 sysfs_remove_link(&dev->dev.kobj, buf);
Yinghai Lu09cedbe2012-02-04 22:55:01 -0800169 /*
170 * pci_stop_dev() could have been called for this virtfn already,
171 * so the directory for the virtfn may have been removed before.
172 * Double check to avoid spurious sysfs warnings.
173 */
174 if (virtfn->dev.kobj.sd)
175 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
Yu Zhaodd7cc442009-03-20 11:25:15 +0800176
177 mutex_lock(&iov->dev->sriov->lock);
Yinghai Lu210647a2012-02-25 13:54:20 -0800178 pci_stop_and_remove_bus_device(virtfn);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800179 virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
180 mutex_unlock(&iov->dev->sriov->lock);
181
182 pci_dev_put(dev);
183}
184
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800185static int sriov_migration(struct pci_dev *dev)
186{
187 u16 status;
188 struct pci_sriov *iov = dev->sriov;
189
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700190 if (!iov->num_VFs)
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800191 return 0;
192
193 if (!(iov->cap & PCI_SRIOV_CAP_VFM))
194 return 0;
195
196 pci_read_config_word(dev, iov->pos + PCI_SRIOV_STATUS, &status);
197 if (!(status & PCI_SRIOV_STATUS_VFM))
198 return 0;
199
200 schedule_work(&iov->mtask);
201
202 return 1;
203}
204
205static void sriov_migration_task(struct work_struct *work)
206{
207 int i;
208 u8 state;
209 u16 status;
210 struct pci_sriov *iov = container_of(work, struct pci_sriov, mtask);
211
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700212 for (i = iov->initial_VFs; i < iov->num_VFs; i++) {
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800213 state = readb(iov->mstate + i);
214 if (state == PCI_SRIOV_VFM_MI) {
215 writeb(PCI_SRIOV_VFM_AV, iov->mstate + i);
216 state = readb(iov->mstate + i);
217 if (state == PCI_SRIOV_VFM_AV)
218 virtfn_add(iov->self, i, 1);
219 } else if (state == PCI_SRIOV_VFM_MO) {
220 virtfn_remove(iov->self, i, 1);
221 writeb(PCI_SRIOV_VFM_UA, iov->mstate + i);
222 state = readb(iov->mstate + i);
223 if (state == PCI_SRIOV_VFM_AV)
224 virtfn_add(iov->self, i, 0);
225 }
226 }
227
228 pci_read_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, &status);
229 status &= ~PCI_SRIOV_STATUS_VFM;
230 pci_write_config_word(iov->self, iov->pos + PCI_SRIOV_STATUS, status);
231}
232
233static int sriov_enable_migration(struct pci_dev *dev, int nr_virtfn)
234{
235 int bir;
236 u32 table;
237 resource_size_t pa;
238 struct pci_sriov *iov = dev->sriov;
239
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700240 if (nr_virtfn <= iov->initial_VFs)
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800241 return 0;
242
243 pci_read_config_dword(dev, iov->pos + PCI_SRIOV_VFM, &table);
244 bir = PCI_SRIOV_VFM_BIR(table);
245 if (bir > PCI_STD_RESOURCE_END)
246 return -EIO;
247
248 table = PCI_SRIOV_VFM_OFFSET(table);
249 if (table + nr_virtfn > pci_resource_len(dev, bir))
250 return -EIO;
251
252 pa = pci_resource_start(dev, bir) + table;
253 iov->mstate = ioremap(pa, nr_virtfn);
254 if (!iov->mstate)
255 return -ENOMEM;
256
257 INIT_WORK(&iov->mtask, sriov_migration_task);
258
259 iov->ctrl |= PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR;
260 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
261
262 return 0;
263}
264
265static void sriov_disable_migration(struct pci_dev *dev)
266{
267 struct pci_sriov *iov = dev->sriov;
268
269 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFM | PCI_SRIOV_CTRL_INTR);
270 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
271
272 cancel_work_sync(&iov->mtask);
273 iounmap(iov->mstate);
274}
275
Yu Zhaodd7cc442009-03-20 11:25:15 +0800276static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
277{
278 int rc;
279 int i, j;
280 int nres;
281 u16 offset, stride, initial;
282 struct resource *res;
283 struct pci_dev *pdev;
284 struct pci_sriov *iov = dev->sriov;
Ram Paibbef98a2011-11-06 10:33:10 +0800285 int bars = 0;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800286
287 if (!nr_virtfn)
288 return 0;
289
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700290 if (iov->num_VFs)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800291 return -EINVAL;
292
293 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700294 if (initial > iov->total_VFs ||
295 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
Yu Zhaodd7cc442009-03-20 11:25:15 +0800296 return -EIO;
297
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700298 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
Yu Zhaodd7cc442009-03-20 11:25:15 +0800299 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
300 return -EINVAL;
301
302 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
303 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset);
304 pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride);
305 if (!offset || (nr_virtfn > 1 && !stride))
306 return -EIO;
307
308 nres = 0;
309 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
Ram Paibbef98a2011-11-06 10:33:10 +0800310 bars |= (1 << (i + PCI_IOV_RESOURCES));
Yu Zhaodd7cc442009-03-20 11:25:15 +0800311 res = dev->resource + PCI_IOV_RESOURCES + i;
312 if (res->parent)
313 nres++;
314 }
315 if (nres != iov->nres) {
316 dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
317 return -ENOMEM;
318 }
319
320 iov->offset = offset;
321 iov->stride = stride;
322
Yinghai Lub918c622012-05-17 18:51:11 -0700323 if (virtfn_bus(dev, nr_virtfn - 1) > dev->bus->busn_res.end) {
Yu Zhaodd7cc442009-03-20 11:25:15 +0800324 dev_err(&dev->dev, "SR-IOV: bus number out of range\n");
325 return -ENOMEM;
326 }
327
Ram Paibbef98a2011-11-06 10:33:10 +0800328 if (pci_enable_resources(dev, bars)) {
329 dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
330 return -ENOMEM;
331 }
332
Yu Zhaodd7cc442009-03-20 11:25:15 +0800333 if (iov->link != dev->devfn) {
334 pdev = pci_get_slot(dev->bus, iov->link);
335 if (!pdev)
336 return -ENODEV;
337
338 pci_dev_put(pdev);
339
340 if (!pdev->is_physfn)
341 return -ENODEV;
342
343 rc = sysfs_create_link(&dev->dev.kobj,
344 &pdev->dev.kobj, "dep_link");
345 if (rc)
346 return rc;
347 }
348
349 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100350 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800351 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
352 msleep(100);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100353 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800354
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700355 iov->initial_VFs = initial;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800356 if (nr_virtfn < initial)
357 initial = nr_virtfn;
358
359 for (i = 0; i < initial; i++) {
360 rc = virtfn_add(dev, i, 0);
361 if (rc)
362 goto failed;
363 }
364
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800365 if (iov->cap & PCI_SRIOV_CAP_VFM) {
366 rc = sriov_enable_migration(dev, nr_virtfn);
367 if (rc)
368 goto failed;
369 }
370
Yu Zhaodd7cc442009-03-20 11:25:15 +0800371 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700372 iov->num_VFs = nr_virtfn;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800373
374 return 0;
375
376failed:
377 for (j = 0; j < i; j++)
378 virtfn_remove(dev, j, 0);
379
380 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100381 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800382 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
383 ssleep(1);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100384 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800385
386 if (iov->link != dev->devfn)
387 sysfs_remove_link(&dev->dev.kobj, "dep_link");
388
389 return rc;
390}
391
392static void sriov_disable(struct pci_dev *dev)
393{
394 int i;
395 struct pci_sriov *iov = dev->sriov;
396
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700397 if (!iov->num_VFs)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800398 return;
399
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800400 if (iov->cap & PCI_SRIOV_CAP_VFM)
401 sriov_disable_migration(dev);
402
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700403 for (i = 0; i < iov->num_VFs; i++)
Yu Zhaodd7cc442009-03-20 11:25:15 +0800404 virtfn_remove(dev, i, 0);
405
406 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100407 pci_cfg_access_lock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800408 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
409 ssleep(1);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100410 pci_cfg_access_unlock(dev);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800411
412 if (iov->link != dev->devfn)
413 sysfs_remove_link(&dev->dev.kobj, "dep_link");
414
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700415 iov->num_VFs = 0;
Yu Zhaodd7cc442009-03-20 11:25:15 +0800416}
417
Yu Zhaod1b054d2009-03-20 11:25:11 +0800418static int sriov_init(struct pci_dev *dev, int pos)
419{
420 int i;
421 int rc;
422 int nres;
423 u32 pgsz;
424 u16 ctrl, total, offset, stride;
425 struct pci_sriov *iov;
426 struct resource *res;
427 struct pci_dev *pdev;
428
Yijing Wang62f87c02012-07-24 17:20:03 +0800429 if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
430 pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
Yu Zhaod1b054d2009-03-20 11:25:11 +0800431 return -ENODEV;
432
433 pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
434 if (ctrl & PCI_SRIOV_CTRL_VFE) {
435 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
436 ssleep(1);
437 }
438
439 pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
440 if (!total)
441 return 0;
442
443 ctrl = 0;
444 list_for_each_entry(pdev, &dev->bus->devices, bus_list)
445 if (pdev->is_physfn)
446 goto found;
447
448 pdev = NULL;
449 if (pci_ari_enabled(dev->bus))
450 ctrl |= PCI_SRIOV_CTRL_ARI;
451
452found:
453 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800454 pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset);
455 pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride);
456 if (!offset || (total > 1 && !stride))
457 return -EIO;
458
459 pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
460 i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
461 pgsz &= ~((1 << i) - 1);
462 if (!pgsz)
463 return -EIO;
464
465 pgsz &= ~(pgsz - 1);
Vaidyanathan Srinivasan8161fe92012-02-02 23:11:20 +0530466 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800467
468 nres = 0;
469 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
470 res = dev->resource + PCI_IOV_RESOURCES + i;
471 i += __pci_read_base(dev, pci_bar_unknown, res,
472 pos + PCI_SRIOV_BAR + i * 4);
473 if (!res->flags)
474 continue;
475 if (resource_size(res) & (PAGE_SIZE - 1)) {
476 rc = -EIO;
477 goto failed;
478 }
479 res->end = res->start + resource_size(res) * total - 1;
480 nres++;
481 }
482
483 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
484 if (!iov) {
485 rc = -ENOMEM;
486 goto failed;
487 }
488
489 iov->pos = pos;
490 iov->nres = nres;
491 iov->ctrl = ctrl;
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700492 iov->total_VFs = total;
Yu Zhaod1b054d2009-03-20 11:25:11 +0800493 iov->offset = offset;
494 iov->stride = stride;
495 iov->pgsz = pgsz;
496 iov->self = dev;
497 pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
498 pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
Yijing Wang62f87c02012-07-24 17:20:03 +0800499 if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
Yu Zhao4d135db2009-05-20 17:11:57 +0800500 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800501
502 if (pdev)
503 iov->dev = pci_dev_get(pdev);
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800504 else
Yu Zhaod1b054d2009-03-20 11:25:11 +0800505 iov->dev = dev;
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800506
507 mutex_init(&iov->lock);
Yu Zhaod1b054d2009-03-20 11:25:11 +0800508
509 dev->sriov = iov;
510 dev->is_physfn = 1;
511
512 return 0;
513
514failed:
515 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
516 res = dev->resource + PCI_IOV_RESOURCES + i;
517 res->flags = 0;
518 }
519
520 return rc;
521}
522
523static void sriov_release(struct pci_dev *dev)
524{
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700525 BUG_ON(dev->sriov->num_VFs);
Yu Zhaodd7cc442009-03-20 11:25:15 +0800526
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800527 if (dev != dev->sriov->dev)
Yu Zhaod1b054d2009-03-20 11:25:11 +0800528 pci_dev_put(dev->sriov->dev);
529
Yu Zhaoe277d2f2009-05-18 13:51:33 +0800530 mutex_destroy(&dev->sriov->lock);
531
Yu Zhaod1b054d2009-03-20 11:25:11 +0800532 kfree(dev->sriov);
533 dev->sriov = NULL;
534}
535
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800536static void sriov_restore_state(struct pci_dev *dev)
537{
538 int i;
539 u16 ctrl;
540 struct pci_sriov *iov = dev->sriov;
541
542 pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
543 if (ctrl & PCI_SRIOV_CTRL_VFE)
544 return;
545
546 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
547 pci_update_resource(dev, i);
548
549 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700550 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->num_VFs);
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800551 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
552 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
553 msleep(100);
554}
555
Yu Zhaod1b054d2009-03-20 11:25:11 +0800556/**
557 * pci_iov_init - initialize the IOV capability
558 * @dev: the PCI device
559 *
560 * Returns 0 on success, or negative on failure.
561 */
562int pci_iov_init(struct pci_dev *dev)
563{
564 int pos;
565
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900566 if (!pci_is_pcie(dev))
Yu Zhaod1b054d2009-03-20 11:25:11 +0800567 return -ENODEV;
568
569 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
570 if (pos)
571 return sriov_init(dev, pos);
572
573 return -ENODEV;
574}
575
576/**
577 * pci_iov_release - release resources used by the IOV capability
578 * @dev: the PCI device
579 */
580void pci_iov_release(struct pci_dev *dev)
581{
582 if (dev->is_physfn)
583 sriov_release(dev);
584}
585
586/**
587 * pci_iov_resource_bar - get position of the SR-IOV BAR
588 * @dev: the PCI device
589 * @resno: the resource number
590 * @type: the BAR type to be filled in
591 *
592 * Returns position of the BAR encapsulated in the SR-IOV capability.
593 */
594int pci_iov_resource_bar(struct pci_dev *dev, int resno,
595 enum pci_bar_type *type)
596{
597 if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
598 return 0;
599
600 BUG_ON(!dev->is_physfn);
601
602 *type = pci_bar_unknown;
603
604 return dev->sriov->pos + PCI_SRIOV_BAR +
605 4 * (resno - PCI_IOV_RESOURCES);
606}
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800607
608/**
Chris Wright6faf17f2009-08-28 13:00:06 -0700609 * pci_sriov_resource_alignment - get resource alignment for VF BAR
610 * @dev: the PCI device
611 * @resno: the resource number
612 *
613 * Returns the alignment of the VF BAR found in the SR-IOV capability.
614 * This is not the same as the resource size which is defined as
615 * the VF BAR size multiplied by the number of VFs. The alignment
616 * is just the VF BAR size.
617 */
Cam Macdonell0e522472010-09-07 17:25:20 -0700618resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
Chris Wright6faf17f2009-08-28 13:00:06 -0700619{
620 struct resource tmp;
621 enum pci_bar_type type;
622 int reg = pci_iov_resource_bar(dev, resno, &type);
623
624 if (!reg)
625 return 0;
626
627 __pci_read_base(dev, type, &tmp, reg);
628 return resource_alignment(&tmp);
629}
630
631/**
Yu Zhao8c5cdb62009-03-20 11:25:12 +0800632 * pci_restore_iov_state - restore the state of the IOV capability
633 * @dev: the PCI device
634 */
635void pci_restore_iov_state(struct pci_dev *dev)
636{
637 if (dev->is_physfn)
638 sriov_restore_state(dev);
639}
Yu Zhaoa28724b2009-03-20 11:25:13 +0800640
641/**
642 * pci_iov_bus_range - find bus range used by Virtual Function
643 * @bus: the PCI bus
644 *
645 * Returns max number of buses (exclude current one) used by Virtual
646 * Functions.
647 */
648int pci_iov_bus_range(struct pci_bus *bus)
649{
650 int max = 0;
651 u8 busnr;
652 struct pci_dev *dev;
653
654 list_for_each_entry(dev, &bus->devices, bus_list) {
655 if (!dev->is_physfn)
656 continue;
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700657 busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
Yu Zhaoa28724b2009-03-20 11:25:13 +0800658 if (busnr > max)
659 max = busnr;
660 }
661
662 return max ? max - bus->number : 0;
663}
Yu Zhaodd7cc442009-03-20 11:25:15 +0800664
665/**
666 * pci_enable_sriov - enable the SR-IOV capability
667 * @dev: the PCI device
Randy Dunlap52a88732009-04-01 17:45:30 -0700668 * @nr_virtfn: number of virtual functions to enable
Yu Zhaodd7cc442009-03-20 11:25:15 +0800669 *
670 * Returns 0 on success, or negative on failure.
671 */
672int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
673{
674 might_sleep();
675
676 if (!dev->is_physfn)
677 return -ENODEV;
678
679 return sriov_enable(dev, nr_virtfn);
680}
681EXPORT_SYMBOL_GPL(pci_enable_sriov);
682
683/**
684 * pci_disable_sriov - disable the SR-IOV capability
685 * @dev: the PCI device
686 */
687void pci_disable_sriov(struct pci_dev *dev)
688{
689 might_sleep();
690
691 if (!dev->is_physfn)
692 return;
693
694 sriov_disable(dev);
695}
696EXPORT_SYMBOL_GPL(pci_disable_sriov);
Yu Zhao74bb1bc2009-03-20 11:25:16 +0800697
698/**
699 * pci_sriov_migration - notify SR-IOV core of Virtual Function Migration
700 * @dev: the PCI device
701 *
702 * Returns IRQ_HANDLED if the IRQ is handled, or IRQ_NONE if not.
703 *
704 * Physical Function driver is responsible to register IRQ handler using
705 * VF Migration Interrupt Message Number, and call this function when the
706 * interrupt is generated by the hardware.
707 */
708irqreturn_t pci_sriov_migration(struct pci_dev *dev)
709{
710 if (!dev->is_physfn)
711 return IRQ_NONE;
712
713 return sriov_migration(dev) ? IRQ_HANDLED : IRQ_NONE;
714}
715EXPORT_SYMBOL_GPL(pci_sriov_migration);
Yu Zhao302b4212009-05-18 13:51:32 +0800716
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000717/**
718 * pci_num_vf - return number of VFs associated with a PF device_release_driver
719 * @dev: the PCI device
720 *
721 * Returns number of VFs, or 0 if SR-IOV is not enabled.
722 */
723int pci_num_vf(struct pci_dev *dev)
724{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700725 if (!dev->is_physfn)
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000726 return 0;
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700727
728 return dev->sriov->num_VFs;
Williams, Mitch Afb8a0d92010-02-10 01:43:04 +0000729}
730EXPORT_SYMBOL_GPL(pci_num_vf);
Donald Dutilebff73152012-11-05 15:20:37 -0500731
732/**
Alexander Duyck5a8eb242013-04-25 04:42:29 +0000733 * pci_vfs_assigned - returns number of VFs are assigned to a guest
734 * @dev: the PCI device
735 *
736 * Returns number of VFs belonging to this device that are assigned to a guest.
737 * If device is not a physical function returns -ENODEV.
738 */
739int pci_vfs_assigned(struct pci_dev *dev)
740{
741 struct pci_dev *vfdev;
742 unsigned int vfs_assigned = 0;
743 unsigned short dev_id;
744
745 /* only search if we are a PF */
746 if (!dev->is_physfn)
747 return 0;
748
749 /*
750 * determine the device ID for the VFs, the vendor ID will be the
751 * same as the PF so there is no need to check for that one
752 */
753 pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id);
754
755 /* loop through all the VFs to see if we own any that are assigned */
756 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
757 while (vfdev) {
758 /*
759 * It is considered assigned if it is a virtual function with
760 * our dev as the physical function and the assigned bit is set
761 */
762 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
763 (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED))
764 vfs_assigned++;
765
766 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
767 }
768
769 return vfs_assigned;
770}
771EXPORT_SYMBOL_GPL(pci_vfs_assigned);
772
773/**
Donald Dutilebff73152012-11-05 15:20:37 -0500774 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
775 * @dev: the PCI PF device
Randy Dunlap2094f162013-01-09 17:12:52 -0800776 * @numvfs: number that should be used for TotalVFs supported
Donald Dutilebff73152012-11-05 15:20:37 -0500777 *
778 * Should be called from PF driver's probe routine with
779 * device's mutex held.
780 *
781 * Returns 0 if PF is an SRIOV-capable device and
782 * value of numvfs valid. If not a PF with VFS, return -EINVAL;
783 * if VFs already enabled, return -EBUSY.
784 */
785int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
786{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700787 if (!dev->is_physfn || (numvfs > dev->sriov->total_VFs))
Donald Dutilebff73152012-11-05 15:20:37 -0500788 return -EINVAL;
789
790 /* Shouldn't change if VFs already enabled */
791 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
792 return -EBUSY;
793 else
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700794 dev->sriov->driver_max_VFs = numvfs;
Donald Dutilebff73152012-11-05 15:20:37 -0500795
796 return 0;
797}
798EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
799
800/**
801 * pci_sriov_get_totalvfs -- get total VFs supported on this devic3
802 * @dev: the PCI PF device
803 *
804 * For a PCIe device with SRIOV support, return the PCIe
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700805 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
Donald Dutilebff73152012-11-05 15:20:37 -0500806 * if the driver reduced it. Otherwise, -EINVAL.
807 */
808int pci_sriov_get_totalvfs(struct pci_dev *dev)
809{
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700810 if (!dev->is_physfn)
Donald Dutilebff73152012-11-05 15:20:37 -0500811 return -EINVAL;
812
Bjorn Helgaas6b136722012-11-09 20:27:53 -0700813 if (dev->sriov->driver_max_VFs)
814 return dev->sriov->driver_max_VFs;
Bjorn Helgaas1452cd72012-11-09 20:35:01 -0700815
816 return dev->sriov->total_VFs;
Donald Dutilebff73152012-11-05 15:20:37 -0500817}
818EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);