PCI: Convert alloc_pci_dev(void) to pci_alloc_dev(bus)
[firefly-linux-kernel-4.4.55.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         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
49         pci_bus_insert_busn_res(child, busnr, busnr);
50         bus->is_added = 1;
51
52         return child;
53 }
54
55 static 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
69 static 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         struct pci_bus *bus;
79
80         virtfn = pci_alloc_dev(NULL);
81         if (!virtfn)
82                 return -ENOMEM;
83
84         mutex_lock(&iov->dev->sriov->lock);
85         bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
86         if (!bus) {
87                 kfree(virtfn);
88                 mutex_unlock(&iov->dev->sriov->lock);
89                 return -ENOMEM;
90         }
91         virtfn->bus = pci_bus_get(bus);
92         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);
105                 do_div(size, iov->total_VFs);
106                 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)
113                 __pci_reset_function(virtfn);
114
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);
122         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
134 failed2:
135         sysfs_remove_link(&dev->dev.kobj, buf);
136 failed1:
137         pci_dev_put(dev);
138         mutex_lock(&iov->dev->sriov->lock);
139         pci_stop_and_remove_bus_device(virtfn);
140         virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
141         mutex_unlock(&iov->dev->sriov->lock);
142
143         return rc;
144 }
145
146 static void virtfn_remove(struct pci_dev *dev, int id, int reset)
147 {
148         char buf[VIRTFN_ID_LEN];
149         struct pci_bus *bus;
150         struct pci_dev *virtfn;
151         struct pci_sriov *iov = dev->sriov;
152
153         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));
158         if (!virtfn)
159                 return;
160
161         pci_dev_put(virtfn);
162
163         if (reset) {
164                 device_release_driver(&virtfn->dev);
165                 __pci_reset_function(virtfn);
166         }
167
168         sprintf(buf, "virtfn%u", id);
169         sysfs_remove_link(&dev->dev.kobj, buf);
170         /*
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");
177
178         mutex_lock(&iov->dev->sriov->lock);
179         pci_stop_and_remove_bus_device(virtfn);
180         virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
181         mutex_unlock(&iov->dev->sriov->lock);
182
183         pci_dev_put(dev);
184 }
185
186 static int sriov_migration(struct pci_dev *dev)
187 {
188         u16 status;
189         struct pci_sriov *iov = dev->sriov;
190
191         if (!iov->num_VFs)
192                 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
206 static 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
213         for (i = iov->initial_VFs; i < iov->num_VFs; i++) {
214                 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
234 static 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
241         if (nr_virtfn <= iov->initial_VFs)
242                 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
266 static 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
277 static 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;
286         int bars = 0;
287
288         if (!nr_virtfn)
289                 return 0;
290
291         if (iov->num_VFs)
292                 return -EINVAL;
293
294         pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
295         if (initial > iov->total_VFs ||
296             (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
297                 return -EIO;
298
299         if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
300             (!(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++) {
311                 bars |= (1 << (i + PCI_IOV_RESOURCES));
312                 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
324         if (virtfn_bus(dev, nr_virtfn - 1) > dev->bus->busn_res.end) {
325                 dev_err(&dev->dev, "SR-IOV: bus number out of range\n");
326                 return -ENOMEM;
327         }
328
329         if (pci_enable_resources(dev, bars)) {
330                 dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
331                 return -ENOMEM;
332         }
333
334         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;
351         pci_cfg_access_lock(dev);
352         pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
353         msleep(100);
354         pci_cfg_access_unlock(dev);
355
356         iov->initial_VFs = initial;
357         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
366         if (iov->cap & PCI_SRIOV_CAP_VFM) {
367                 rc = sriov_enable_migration(dev, nr_virtfn);
368                 if (rc)
369                         goto failed;
370         }
371
372         kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
373         iov->num_VFs = nr_virtfn;
374
375         return 0;
376
377 failed:
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);
382         pci_cfg_access_lock(dev);
383         pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
384         ssleep(1);
385         pci_cfg_access_unlock(dev);
386
387         if (iov->link != dev->devfn)
388                 sysfs_remove_link(&dev->dev.kobj, "dep_link");
389
390         return rc;
391 }
392
393 static void sriov_disable(struct pci_dev *dev)
394 {
395         int i;
396         struct pci_sriov *iov = dev->sriov;
397
398         if (!iov->num_VFs)
399                 return;
400
401         if (iov->cap & PCI_SRIOV_CAP_VFM)
402                 sriov_disable_migration(dev);
403
404         for (i = 0; i < iov->num_VFs; i++)
405                 virtfn_remove(dev, i, 0);
406
407         iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
408         pci_cfg_access_lock(dev);
409         pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
410         ssleep(1);
411         pci_cfg_access_unlock(dev);
412
413         if (iov->link != dev->devfn)
414                 sysfs_remove_link(&dev->dev.kobj, "dep_link");
415
416         iov->num_VFs = 0;
417 }
418
419 static 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
430         if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
431             pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
432                 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
453 found:
454         pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
455         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);
467         pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
468
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;
493         iov->total_VFs = total;
494         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);
500         if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
501                 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
502
503         if (pdev)
504                 iov->dev = pci_dev_get(pdev);
505         else
506                 iov->dev = dev;
507
508         mutex_init(&iov->lock);
509
510         dev->sriov = iov;
511         dev->is_physfn = 1;
512
513         return 0;
514
515 failed:
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
524 static void sriov_release(struct pci_dev *dev)
525 {
526         BUG_ON(dev->sriov->num_VFs);
527
528         if (dev != dev->sriov->dev)
529                 pci_dev_put(dev->sriov->dev);
530
531         mutex_destroy(&dev->sriov->lock);
532
533         kfree(dev->sriov);
534         dev->sriov = NULL;
535 }
536
537 static 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);
551         pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->num_VFs);
552         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
557 /**
558  * pci_iov_init - initialize the IOV capability
559  * @dev: the PCI device
560  *
561  * Returns 0 on success, or negative on failure.
562  */
563 int pci_iov_init(struct pci_dev *dev)
564 {
565         int pos;
566
567         if (!pci_is_pcie(dev))
568                 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  */
581 void 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  */
595 int 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 }
608
609 /**
610  * 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  */
619 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
620 {
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 /**
633  * pci_restore_iov_state - restore the state of the IOV capability
634  * @dev: the PCI device
635  */
636 void pci_restore_iov_state(struct pci_dev *dev)
637 {
638         if (dev->is_physfn)
639                 sriov_restore_state(dev);
640 }
641
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  */
649 int 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;
658                 busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
659                 if (busnr > max)
660                         max = busnr;
661         }
662
663         return max ? max - bus->number : 0;
664 }
665
666 /**
667  * pci_enable_sriov - enable the SR-IOV capability
668  * @dev: the PCI device
669  * @nr_virtfn: number of virtual functions to enable
670  *
671  * Returns 0 on success, or negative on failure.
672  */
673 int 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 }
682 EXPORT_SYMBOL_GPL(pci_enable_sriov);
683
684 /**
685  * pci_disable_sriov - disable the SR-IOV capability
686  * @dev: the PCI device
687  */
688 void 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 }
697 EXPORT_SYMBOL_GPL(pci_disable_sriov);
698
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  */
709 irqreturn_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 }
716 EXPORT_SYMBOL_GPL(pci_sriov_migration);
717
718 /**
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  */
724 int pci_num_vf(struct pci_dev *dev)
725 {
726         if (!dev->is_physfn)
727                 return 0;
728
729         return dev->sriov->num_VFs;
730 }
731 EXPORT_SYMBOL_GPL(pci_num_vf);
732
733 /**
734  * 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  */
740 int 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 }
772 EXPORT_SYMBOL_GPL(pci_vfs_assigned);
773
774 /**
775  * pci_sriov_set_totalvfs -- reduce the TotalVFs available
776  * @dev: the PCI PF device
777  * @numvfs: number that should be used for TotalVFs supported
778  *
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  */
786 int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
787 {
788         if (!dev->is_physfn || (numvfs > dev->sriov->total_VFs))
789                 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
795                 dev->sriov->driver_max_VFs = numvfs;
796
797         return 0;
798 }
799 EXPORT_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
806  * SRIOV capability value of TotalVFs or the value of driver_max_VFs
807  * if the driver reduced it.  Otherwise, -EINVAL.
808  */
809 int pci_sriov_get_totalvfs(struct pci_dev *dev)
810 {
811         if (!dev->is_physfn)
812                 return -EINVAL;
813
814         if (dev->sriov->driver_max_VFs)
815                 return dev->sriov->driver_max_VFs;
816
817         return dev->sriov->total_VFs;
818 }
819 EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);