PCI: Refresh First VF Offset and VF Stride when updating NumVFs
[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 /*
35  * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
36  * change when NumVFs changes.
37  *
38  * Update iov->offset and iov->stride when NumVFs is written.
39  */
40 static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
41 {
42         struct pci_sriov *iov = dev->sriov;
43
44         pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
45         pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
46         pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
47 }
48
49 static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
50 {
51         struct pci_bus *child;
52
53         if (bus->number == busnr)
54                 return bus;
55
56         child = pci_find_bus(pci_domain_nr(bus), busnr);
57         if (child)
58                 return child;
59
60         child = pci_add_new_bus(bus, NULL, busnr);
61         if (!child)
62                 return NULL;
63
64         pci_bus_insert_busn_res(child, busnr, busnr);
65
66         return child;
67 }
68
69 static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
70 {
71         if (physbus != virtbus && list_empty(&virtbus->devices))
72                 pci_remove_bus(virtbus);
73 }
74
75 resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
76 {
77         if (!dev->is_physfn)
78                 return 0;
79
80         return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
81 }
82
83 static int virtfn_add(struct pci_dev *dev, int id, int reset)
84 {
85         int i;
86         int rc = -ENOMEM;
87         u64 size;
88         char buf[VIRTFN_ID_LEN];
89         struct pci_dev *virtfn;
90         struct resource *res;
91         struct pci_sriov *iov = dev->sriov;
92         struct pci_bus *bus;
93
94         mutex_lock(&iov->dev->sriov->lock);
95         bus = virtfn_add_bus(dev->bus, virtfn_bus(dev, id));
96         if (!bus)
97                 goto failed;
98
99         virtfn = pci_alloc_dev(bus);
100         if (!virtfn)
101                 goto failed0;
102
103         virtfn->devfn = virtfn_devfn(dev, id);
104         virtfn->vendor = dev->vendor;
105         pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_DID, &virtfn->device);
106         pci_setup_device(virtfn);
107         virtfn->dev.parent = dev->dev.parent;
108         virtfn->physfn = pci_dev_get(dev);
109         virtfn->is_virtfn = 1;
110         virtfn->multifunction = 0;
111
112         for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
113                 res = &dev->resource[i + PCI_IOV_RESOURCES];
114                 if (!res->parent)
115                         continue;
116                 virtfn->resource[i].name = pci_name(virtfn);
117                 virtfn->resource[i].flags = res->flags;
118                 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
119                 virtfn->resource[i].start = res->start + size * id;
120                 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
121                 rc = request_resource(res, &virtfn->resource[i]);
122                 BUG_ON(rc);
123         }
124
125         if (reset)
126                 __pci_reset_function(virtfn);
127
128         pci_device_add(virtfn, virtfn->bus);
129         mutex_unlock(&iov->dev->sriov->lock);
130
131         pci_bus_add_device(virtfn);
132         sprintf(buf, "virtfn%u", id);
133         rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
134         if (rc)
135                 goto failed1;
136         rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
137         if (rc)
138                 goto failed2;
139
140         kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
141
142         return 0;
143
144 failed2:
145         sysfs_remove_link(&dev->dev.kobj, buf);
146 failed1:
147         pci_dev_put(dev);
148         mutex_lock(&iov->dev->sriov->lock);
149         pci_stop_and_remove_bus_device(virtfn);
150 failed0:
151         virtfn_remove_bus(dev->bus, bus);
152 failed:
153         mutex_unlock(&iov->dev->sriov->lock);
154
155         return rc;
156 }
157
158 static void virtfn_remove(struct pci_dev *dev, int id, int reset)
159 {
160         char buf[VIRTFN_ID_LEN];
161         struct pci_dev *virtfn;
162         struct pci_sriov *iov = dev->sriov;
163
164         virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
165                                              virtfn_bus(dev, id),
166                                              virtfn_devfn(dev, id));
167         if (!virtfn)
168                 return;
169
170         if (reset) {
171                 device_release_driver(&virtfn->dev);
172                 __pci_reset_function(virtfn);
173         }
174
175         sprintf(buf, "virtfn%u", id);
176         sysfs_remove_link(&dev->dev.kobj, buf);
177         /*
178          * pci_stop_dev() could have been called for this virtfn already,
179          * so the directory for the virtfn may have been removed before.
180          * Double check to avoid spurious sysfs warnings.
181          */
182         if (virtfn->dev.kobj.sd)
183                 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
184
185         mutex_lock(&iov->dev->sriov->lock);
186         pci_stop_and_remove_bus_device(virtfn);
187         virtfn_remove_bus(dev->bus, virtfn->bus);
188         mutex_unlock(&iov->dev->sriov->lock);
189
190         /* balance pci_get_domain_bus_and_slot() */
191         pci_dev_put(virtfn);
192         pci_dev_put(dev);
193 }
194
195 static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
196 {
197         int rc;
198         int i, j;
199         int nres;
200         u16 offset, stride, initial;
201         struct resource *res;
202         struct pci_dev *pdev;
203         struct pci_sriov *iov = dev->sriov;
204         int bars = 0;
205         u8 bus;
206
207         if (!nr_virtfn)
208                 return 0;
209
210         if (iov->num_VFs)
211                 return -EINVAL;
212
213         pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
214         if (initial > iov->total_VFs ||
215             (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
216                 return -EIO;
217
218         if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
219             (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
220                 return -EINVAL;
221
222         pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &offset);
223         pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &stride);
224         if (!offset || (nr_virtfn > 1 && !stride))
225                 return -EIO;
226
227         nres = 0;
228         for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
229                 bars |= (1 << (i + PCI_IOV_RESOURCES));
230                 res = &dev->resource[i + PCI_IOV_RESOURCES];
231                 if (res->parent)
232                         nres++;
233         }
234         if (nres != iov->nres) {
235                 dev_err(&dev->dev, "not enough MMIO resources for SR-IOV\n");
236                 return -ENOMEM;
237         }
238
239         iov->offset = offset;
240         iov->stride = stride;
241
242         bus = virtfn_bus(dev, nr_virtfn - 1);
243         if (bus > dev->bus->busn_res.end) {
244                 dev_err(&dev->dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
245                         nr_virtfn, bus, &dev->bus->busn_res);
246                 return -ENOMEM;
247         }
248
249         if (pci_enable_resources(dev, bars)) {
250                 dev_err(&dev->dev, "SR-IOV: IOV BARS not allocated\n");
251                 return -ENOMEM;
252         }
253
254         if (iov->link != dev->devfn) {
255                 pdev = pci_get_slot(dev->bus, iov->link);
256                 if (!pdev)
257                         return -ENODEV;
258
259                 if (!pdev->is_physfn) {
260                         pci_dev_put(pdev);
261                         return -ENOSYS;
262                 }
263
264                 rc = sysfs_create_link(&dev->dev.kobj,
265                                         &pdev->dev.kobj, "dep_link");
266                 pci_dev_put(pdev);
267                 if (rc)
268                         return rc;
269         }
270
271         pci_iov_set_numvfs(dev, nr_virtfn);
272         iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
273         pci_cfg_access_lock(dev);
274         pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
275         msleep(100);
276         pci_cfg_access_unlock(dev);
277
278         iov->initial_VFs = initial;
279         if (nr_virtfn < initial)
280                 initial = nr_virtfn;
281
282         for (i = 0; i < initial; i++) {
283                 rc = virtfn_add(dev, i, 0);
284                 if (rc)
285                         goto failed;
286         }
287
288         kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
289         iov->num_VFs = nr_virtfn;
290
291         return 0;
292
293 failed:
294         for (j = 0; j < i; j++)
295                 virtfn_remove(dev, j, 0);
296
297         iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
298         pci_cfg_access_lock(dev);
299         pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
300         pci_iov_set_numvfs(dev, 0);
301         ssleep(1);
302         pci_cfg_access_unlock(dev);
303
304         if (iov->link != dev->devfn)
305                 sysfs_remove_link(&dev->dev.kobj, "dep_link");
306
307         return rc;
308 }
309
310 static void sriov_disable(struct pci_dev *dev)
311 {
312         int i;
313         struct pci_sriov *iov = dev->sriov;
314
315         if (!iov->num_VFs)
316                 return;
317
318         for (i = 0; i < iov->num_VFs; i++)
319                 virtfn_remove(dev, i, 0);
320
321         iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
322         pci_cfg_access_lock(dev);
323         pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
324         ssleep(1);
325         pci_cfg_access_unlock(dev);
326
327         if (iov->link != dev->devfn)
328                 sysfs_remove_link(&dev->dev.kobj, "dep_link");
329
330         iov->num_VFs = 0;
331         pci_iov_set_numvfs(dev, 0);
332 }
333
334 static int sriov_init(struct pci_dev *dev, int pos)
335 {
336         int i, bar64;
337         int rc;
338         int nres;
339         u32 pgsz;
340         u16 ctrl, total, offset, stride;
341         struct pci_sriov *iov;
342         struct resource *res;
343         struct pci_dev *pdev;
344
345         if (pci_pcie_type(dev) != PCI_EXP_TYPE_RC_END &&
346             pci_pcie_type(dev) != PCI_EXP_TYPE_ENDPOINT)
347                 return -ENODEV;
348
349         pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
350         if (ctrl & PCI_SRIOV_CTRL_VFE) {
351                 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
352                 ssleep(1);
353         }
354
355         pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
356         if (!total)
357                 return 0;
358
359         ctrl = 0;
360         list_for_each_entry(pdev, &dev->bus->devices, bus_list)
361                 if (pdev->is_physfn)
362                         goto found;
363
364         pdev = NULL;
365         if (pci_ari_enabled(dev->bus))
366                 ctrl |= PCI_SRIOV_CTRL_ARI;
367
368 found:
369         pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
370         pci_write_config_word(dev, pos + PCI_SRIOV_NUM_VF, 0);
371         pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset);
372         pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride);
373         if (!offset || (total > 1 && !stride))
374                 return -EIO;
375
376         pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
377         i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
378         pgsz &= ~((1 << i) - 1);
379         if (!pgsz)
380                 return -EIO;
381
382         pgsz &= ~(pgsz - 1);
383         pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
384
385         iov = kzalloc(sizeof(*iov), GFP_KERNEL);
386         if (!iov)
387                 return -ENOMEM;
388
389         nres = 0;
390         for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
391                 res = &dev->resource[i + PCI_IOV_RESOURCES];
392                 bar64 = __pci_read_base(dev, pci_bar_unknown, res,
393                                         pos + PCI_SRIOV_BAR + i * 4);
394                 if (!res->flags)
395                         continue;
396                 if (resource_size(res) & (PAGE_SIZE - 1)) {
397                         rc = -EIO;
398                         goto failed;
399                 }
400                 iov->barsz[i] = resource_size(res);
401                 res->end = res->start + resource_size(res) * total - 1;
402                 dev_info(&dev->dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
403                          i, res, i, total);
404                 i += bar64;
405                 nres++;
406         }
407
408         iov->pos = pos;
409         iov->nres = nres;
410         iov->ctrl = ctrl;
411         iov->total_VFs = total;
412         iov->offset = offset;
413         iov->stride = stride;
414         iov->pgsz = pgsz;
415         iov->self = dev;
416         pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
417         pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
418         if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
419                 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
420
421         if (pdev)
422                 iov->dev = pci_dev_get(pdev);
423         else
424                 iov->dev = dev;
425
426         mutex_init(&iov->lock);
427
428         dev->sriov = iov;
429         dev->is_physfn = 1;
430
431         return 0;
432
433 failed:
434         for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
435                 res = &dev->resource[i + PCI_IOV_RESOURCES];
436                 res->flags = 0;
437         }
438
439         kfree(iov);
440         return rc;
441 }
442
443 static void sriov_release(struct pci_dev *dev)
444 {
445         BUG_ON(dev->sriov->num_VFs);
446
447         if (dev != dev->sriov->dev)
448                 pci_dev_put(dev->sriov->dev);
449
450         mutex_destroy(&dev->sriov->lock);
451
452         kfree(dev->sriov);
453         dev->sriov = NULL;
454 }
455
456 static void sriov_restore_state(struct pci_dev *dev)
457 {
458         int i;
459         u16 ctrl;
460         struct pci_sriov *iov = dev->sriov;
461
462         pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
463         if (ctrl & PCI_SRIOV_CTRL_VFE)
464                 return;
465
466         for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++)
467                 pci_update_resource(dev, i);
468
469         pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
470         pci_iov_set_numvfs(dev, iov->num_VFs);
471         pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
472         if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
473                 msleep(100);
474 }
475
476 /**
477  * pci_iov_init - initialize the IOV capability
478  * @dev: the PCI device
479  *
480  * Returns 0 on success, or negative on failure.
481  */
482 int pci_iov_init(struct pci_dev *dev)
483 {
484         int pos;
485
486         if (!pci_is_pcie(dev))
487                 return -ENODEV;
488
489         pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
490         if (pos)
491                 return sriov_init(dev, pos);
492
493         return -ENODEV;
494 }
495
496 /**
497  * pci_iov_release - release resources used by the IOV capability
498  * @dev: the PCI device
499  */
500 void pci_iov_release(struct pci_dev *dev)
501 {
502         if (dev->is_physfn)
503                 sriov_release(dev);
504 }
505
506 /**
507  * pci_iov_resource_bar - get position of the SR-IOV BAR
508  * @dev: the PCI device
509  * @resno: the resource number
510  *
511  * Returns position of the BAR encapsulated in the SR-IOV capability.
512  */
513 int pci_iov_resource_bar(struct pci_dev *dev, int resno)
514 {
515         if (resno < PCI_IOV_RESOURCES || resno > PCI_IOV_RESOURCE_END)
516                 return 0;
517
518         BUG_ON(!dev->is_physfn);
519
520         return dev->sriov->pos + PCI_SRIOV_BAR +
521                 4 * (resno - PCI_IOV_RESOURCES);
522 }
523
524 /**
525  * pci_sriov_resource_alignment - get resource alignment for VF BAR
526  * @dev: the PCI device
527  * @resno: the resource number
528  *
529  * Returns the alignment of the VF BAR found in the SR-IOV capability.
530  * This is not the same as the resource size which is defined as
531  * the VF BAR size multiplied by the number of VFs.  The alignment
532  * is just the VF BAR size.
533  */
534 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
535 {
536         return pci_iov_resource_size(dev, resno);
537 }
538
539 /**
540  * pci_restore_iov_state - restore the state of the IOV capability
541  * @dev: the PCI device
542  */
543 void pci_restore_iov_state(struct pci_dev *dev)
544 {
545         if (dev->is_physfn)
546                 sriov_restore_state(dev);
547 }
548
549 /**
550  * pci_iov_bus_range - find bus range used by Virtual Function
551  * @bus: the PCI bus
552  *
553  * Returns max number of buses (exclude current one) used by Virtual
554  * Functions.
555  */
556 int pci_iov_bus_range(struct pci_bus *bus)
557 {
558         int max = 0;
559         u8 busnr;
560         struct pci_dev *dev;
561
562         list_for_each_entry(dev, &bus->devices, bus_list) {
563                 if (!dev->is_physfn)
564                         continue;
565                 busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
566                 if (busnr > max)
567                         max = busnr;
568         }
569
570         return max ? max - bus->number : 0;
571 }
572
573 /**
574  * pci_enable_sriov - enable the SR-IOV capability
575  * @dev: the PCI device
576  * @nr_virtfn: number of virtual functions to enable
577  *
578  * Returns 0 on success, or negative on failure.
579  */
580 int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
581 {
582         might_sleep();
583
584         if (!dev->is_physfn)
585                 return -ENOSYS;
586
587         return sriov_enable(dev, nr_virtfn);
588 }
589 EXPORT_SYMBOL_GPL(pci_enable_sriov);
590
591 /**
592  * pci_disable_sriov - disable the SR-IOV capability
593  * @dev: the PCI device
594  */
595 void pci_disable_sriov(struct pci_dev *dev)
596 {
597         might_sleep();
598
599         if (!dev->is_physfn)
600                 return;
601
602         sriov_disable(dev);
603 }
604 EXPORT_SYMBOL_GPL(pci_disable_sriov);
605
606 /**
607  * pci_num_vf - return number of VFs associated with a PF device_release_driver
608  * @dev: the PCI device
609  *
610  * Returns number of VFs, or 0 if SR-IOV is not enabled.
611  */
612 int pci_num_vf(struct pci_dev *dev)
613 {
614         if (!dev->is_physfn)
615                 return 0;
616
617         return dev->sriov->num_VFs;
618 }
619 EXPORT_SYMBOL_GPL(pci_num_vf);
620
621 /**
622  * pci_vfs_assigned - returns number of VFs are assigned to a guest
623  * @dev: the PCI device
624  *
625  * Returns number of VFs belonging to this device that are assigned to a guest.
626  * If device is not a physical function returns 0.
627  */
628 int pci_vfs_assigned(struct pci_dev *dev)
629 {
630         struct pci_dev *vfdev;
631         unsigned int vfs_assigned = 0;
632         unsigned short dev_id;
633
634         /* only search if we are a PF */
635         if (!dev->is_physfn)
636                 return 0;
637
638         /*
639          * determine the device ID for the VFs, the vendor ID will be the
640          * same as the PF so there is no need to check for that one
641          */
642         pci_read_config_word(dev, dev->sriov->pos + PCI_SRIOV_VF_DID, &dev_id);
643
644         /* loop through all the VFs to see if we own any that are assigned */
645         vfdev = pci_get_device(dev->vendor, dev_id, NULL);
646         while (vfdev) {
647                 /*
648                  * It is considered assigned if it is a virtual function with
649                  * our dev as the physical function and the assigned bit is set
650                  */
651                 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
652                         pci_is_dev_assigned(vfdev))
653                         vfs_assigned++;
654
655                 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
656         }
657
658         return vfs_assigned;
659 }
660 EXPORT_SYMBOL_GPL(pci_vfs_assigned);
661
662 /**
663  * pci_sriov_set_totalvfs -- reduce the TotalVFs available
664  * @dev: the PCI PF device
665  * @numvfs: number that should be used for TotalVFs supported
666  *
667  * Should be called from PF driver's probe routine with
668  * device's mutex held.
669  *
670  * Returns 0 if PF is an SRIOV-capable device and
671  * value of numvfs valid. If not a PF return -ENOSYS;
672  * if numvfs is invalid return -EINVAL;
673  * if VFs already enabled, return -EBUSY.
674  */
675 int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
676 {
677         if (!dev->is_physfn)
678                 return -ENOSYS;
679         if (numvfs > dev->sriov->total_VFs)
680                 return -EINVAL;
681
682         /* Shouldn't change if VFs already enabled */
683         if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
684                 return -EBUSY;
685         else
686                 dev->sriov->driver_max_VFs = numvfs;
687
688         return 0;
689 }
690 EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
691
692 /**
693  * pci_sriov_get_totalvfs -- get total VFs supported on this device
694  * @dev: the PCI PF device
695  *
696  * For a PCIe device with SRIOV support, return the PCIe
697  * SRIOV capability value of TotalVFs or the value of driver_max_VFs
698  * if the driver reduced it.  Otherwise 0.
699  */
700 int pci_sriov_get_totalvfs(struct pci_dev *dev)
701 {
702         if (!dev->is_physfn)
703                 return 0;
704
705         if (dev->sriov->driver_max_VFs)
706                 return dev->sriov->driver_max_VFs;
707
708         return dev->sriov->total_VFs;
709 }
710 EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);