]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/pci/iov.c
PCI: Put pci_dev in device tree as early as possible
[karo-tx-linux.git] / drivers / pci / iov.c
1 /*
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
8  *   Address Translation Service 1.0
9  */
10
11 #include <linux/pci.h>
12 #include <linux/slab.h>
13 #include <linux/mutex.h>
14 #include <linux/export.h>
15 #include <linux/string.h>
16 #include <linux/delay.h>
17 #include <linux/pci-ats.h>
18 #include "pci.h"
19
20 #define VIRTFN_ID_LEN   16
21
22 static 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
28 static 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
34 static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
35 {
36         int rc;
37         struct pci_bus *child;
38
39         if (bus->number == busnr)
40                 return bus;
41
42         child = pci_find_bus(pci_domain_nr(bus), busnr);
43         if (child)
44                 return child;
45
46         child = pci_add_new_bus(bus, NULL, busnr);
47         if (!child)
48                 return NULL;
49
50         pci_bus_insert_busn_res(child, busnr, busnr);
51         bus->is_added = 1;
52
53         return child;
54 }
55
56 static void virtfn_remove_bus(struct pci_bus *bus, int busnr)
57 {
58         struct pci_bus *child;
59
60         if (bus->number == busnr)
61                 return;
62
63         child = pci_find_bus(pci_domain_nr(bus), busnr);
64         BUG_ON(!child);
65
66         if (list_empty(&child->devices))
67                 pci_remove_bus(child);
68 }
69
70 static int virtfn_add(struct pci_dev *dev, int id, int reset)
71 {
72         int i;
73         int rc;
74         u64 size;
75         char buf[VIRTFN_ID_LEN];
76         struct pci_dev *virtfn;
77         struct resource *res;
78         struct pci_sriov *iov = dev->sriov;
79
80         virtfn = alloc_pci_dev();
81         if (!virtfn)
82                 return -ENOMEM;
83
84         mutex_lock(&iov->dev->sriov->lock);
85         virtfn->bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
86         if (!virtfn->bus) {
87                 kfree(virtfn);
88                 mutex_unlock(&iov->dev->sriov->lock);
89                 return -ENOMEM;
90         }
91         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);
104                 do_div(size, iov->total_VFs);
105                 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)
112                 __pci_reset_function(virtfn);
113
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);
121         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
133 failed2:
134         sysfs_remove_link(&dev->dev.kobj, buf);
135 failed1:
136         pci_dev_put(dev);
137         mutex_lock(&iov->dev->sriov->lock);
138         pci_stop_and_remove_bus_device(virtfn);
139         virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
140         mutex_unlock(&iov->dev->sriov->lock);
141
142         return rc;
143 }
144
145 static void virtfn_remove(struct pci_dev *dev, int id, int reset)
146 {
147         char buf[VIRTFN_ID_LEN];
148         struct pci_bus *bus;
149         struct pci_dev *virtfn;
150         struct pci_sriov *iov = dev->sriov;
151
152         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));
157         if (!virtfn)
158                 return;
159
160         pci_dev_put(virtfn);
161
162         if (reset) {
163                 device_release_driver(&virtfn->dev);
164                 __pci_reset_function(virtfn);
165         }
166
167         sprintf(buf, "virtfn%u", id);
168         sysfs_remove_link(&dev->dev.kobj, buf);
169         /*
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");
176
177         mutex_lock(&iov->dev->sriov->lock);
178         pci_stop_and_remove_bus_device(virtfn);
179         virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
180         mutex_unlock(&iov->dev->sriov->lock);
181
182         pci_dev_put(dev);
183 }
184
185 static int sriov_migration(struct pci_dev *dev)
186 {
187         u16 status;
188         struct pci_sriov *iov = dev->sriov;
189
190         if (!iov->num_VFs)
191                 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
205 static 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
212         for (i = iov->initial_VFs; i < iov->num_VFs; i++) {
213                 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
233 static 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
240         if (nr_virtfn <= iov->initial_VFs)
241                 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
265 static 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
276 static 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;
285         int bars = 0;
286
287         if (!nr_virtfn)
288                 return 0;
289
290         if (iov->num_VFs)
291                 return -EINVAL;
292
293         pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
294         if (initial > iov->total_VFs ||
295             (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
296                 return -EIO;
297
298         if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
299             (!(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++) {
310                 bars |= (1 << (i + PCI_IOV_RESOURCES));
311                 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
323         if (virtfn_bus(dev, nr_virtfn - 1) > dev->bus->busn_res.end) {
324                 dev_err(&dev->dev, "SR-IOV: bus number out of range\n");
325                 return -ENOMEM;
326         }
327
328         if (pci_enable_resources(dev, bars)) {
329                 dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
330                 return -ENOMEM;
331         }
332
333         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;
350         pci_cfg_access_lock(dev);
351         pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
352         msleep(100);
353         pci_cfg_access_unlock(dev);
354
355         iov->initial_VFs = initial;
356         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
365         if (iov->cap & PCI_SRIOV_CAP_VFM) {
366                 rc = sriov_enable_migration(dev, nr_virtfn);
367                 if (rc)
368                         goto failed;
369         }
370
371         kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
372         iov->num_VFs = nr_virtfn;
373
374         return 0;
375
376 failed:
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);
381         pci_cfg_access_lock(dev);
382         pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
383         ssleep(1);
384         pci_cfg_access_unlock(dev);
385
386         if (iov->link != dev->devfn)
387                 sysfs_remove_link(&dev->dev.kobj, "dep_link");
388
389         return rc;
390 }
391
392 static void sriov_disable(struct pci_dev *dev)
393 {
394         int i;
395         struct pci_sriov *iov = dev->sriov;
396
397         if (!iov->num_VFs)
398                 return;
399
400         if (iov->cap & PCI_SRIOV_CAP_VFM)
401                 sriov_disable_migration(dev);
402
403         for (i = 0; i < iov->num_VFs; i++)
404                 virtfn_remove(dev, i, 0);
405
406         iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
407         pci_cfg_access_lock(dev);
408         pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
409         ssleep(1);
410         pci_cfg_access_unlock(dev);
411
412         if (iov->link != dev->devfn)
413                 sysfs_remove_link(&dev->dev.kobj, "dep_link");
414
415         iov->num_VFs = 0;
416 }
417
418 static 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
429         if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
430             pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
431                 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
452 found:
453         pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
454         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);
466         pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
467
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;
492         iov->total_VFs = total;
493         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);
499         if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
500                 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
501
502         if (pdev)
503                 iov->dev = pci_dev_get(pdev);
504         else
505                 iov->dev = dev;
506
507         mutex_init(&iov->lock);
508
509         dev->sriov = iov;
510         dev->is_physfn = 1;
511
512         return 0;
513
514 failed:
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
523 static void sriov_release(struct pci_dev *dev)
524 {
525         BUG_ON(dev->sriov->num_VFs);
526
527         if (dev != dev->sriov->dev)
528                 pci_dev_put(dev->sriov->dev);
529
530         mutex_destroy(&dev->sriov->lock);
531
532         kfree(dev->sriov);
533         dev->sriov = NULL;
534 }
535
536 static 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);
550         pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->num_VFs);
551         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
556 /**
557  * pci_iov_init - initialize the IOV capability
558  * @dev: the PCI device
559  *
560  * Returns 0 on success, or negative on failure.
561  */
562 int pci_iov_init(struct pci_dev *dev)
563 {
564         int pos;
565
566         if (!pci_is_pcie(dev))
567                 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  */
580 void 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  */
594 int 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 }
607
608 /**
609  * 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  */
618 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
619 {
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 /**
632  * pci_restore_iov_state - restore the state of the IOV capability
633  * @dev: the PCI device
634  */
635 void pci_restore_iov_state(struct pci_dev *dev)
636 {
637         if (dev->is_physfn)
638                 sriov_restore_state(dev);
639 }
640
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  */
648 int 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;
657                 busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
658                 if (busnr > max)
659                         max = busnr;
660         }
661
662         return max ? max - bus->number : 0;
663 }
664
665 /**
666  * pci_enable_sriov - enable the SR-IOV capability
667  * @dev: the PCI device
668  * @nr_virtfn: number of virtual functions to enable
669  *
670  * Returns 0 on success, or negative on failure.
671  */
672 int 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 }
681 EXPORT_SYMBOL_GPL(pci_enable_sriov);
682
683 /**
684  * pci_disable_sriov - disable the SR-IOV capability
685  * @dev: the PCI device
686  */
687 void 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 }
696 EXPORT_SYMBOL_GPL(pci_disable_sriov);
697
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  */
708 irqreturn_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 }
715 EXPORT_SYMBOL_GPL(pci_sriov_migration);
716
717 /**
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  */
723 int pci_num_vf(struct pci_dev *dev)
724 {
725         if (!dev->is_physfn)
726                 return 0;
727
728         return dev->sriov->num_VFs;
729 }
730 EXPORT_SYMBOL_GPL(pci_num_vf);
731
732 /**
733  * pci_sriov_set_totalvfs -- reduce the TotalVFs available
734  * @dev: the PCI PF device
735  * numvfs: number that should be used for TotalVFs supported
736  *
737  * Should be called from PF driver's probe routine with
738  * device's mutex held.
739  *
740  * Returns 0 if PF is an SRIOV-capable device and
741  * value of numvfs valid. If not a PF with VFS, return -EINVAL;
742  * if VFs already enabled, return -EBUSY.
743  */
744 int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
745 {
746         if (!dev->is_physfn || (numvfs > dev->sriov->total_VFs))
747                 return -EINVAL;
748
749         /* Shouldn't change if VFs already enabled */
750         if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
751                 return -EBUSY;
752         else
753                 dev->sriov->driver_max_VFs = numvfs;
754
755         return 0;
756 }
757 EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
758
759 /**
760  * pci_sriov_get_totalvfs -- get total VFs supported on this devic3
761  * @dev: the PCI PF device
762  *
763  * For a PCIe device with SRIOV support, return the PCIe
764  * SRIOV capability value of TotalVFs or the value of driver_max_VFs
765  * if the driver reduced it.  Otherwise, -EINVAL.
766  */
767 int pci_sriov_get_totalvfs(struct pci_dev *dev)
768 {
769         if (!dev->is_physfn)
770                 return -EINVAL;
771
772         if (dev->sriov->driver_max_VFs)
773                 return dev->sriov->driver_max_VFs;
774
775         return dev->sriov->total_VFs;
776 }
777 EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);