Linux-libre 5.4.47-gnu
[librecmc/linux-libre.git] / drivers / pci / iov.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * PCI Express I/O Virtualization (IOV) support
4  *   Single Root IOV 1.0
5  *   Address Translation Service 1.0
6  *
7  * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com>
8  */
9
10 #include <linux/pci.h>
11 #include <linux/slab.h>
12 #include <linux/mutex.h>
13 #include <linux/export.h>
14 #include <linux/string.h>
15 #include <linux/delay.h>
16 #include "pci.h"
17
18 #define VIRTFN_ID_LEN   16
19
20 int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id)
21 {
22         if (!dev->is_physfn)
23                 return -EINVAL;
24         return dev->bus->number + ((dev->devfn + dev->sriov->offset +
25                                     dev->sriov->stride * vf_id) >> 8);
26 }
27
28 int pci_iov_virtfn_devfn(struct pci_dev *dev, int vf_id)
29 {
30         if (!dev->is_physfn)
31                 return -EINVAL;
32         return (dev->devfn + dev->sriov->offset +
33                 dev->sriov->stride * vf_id) & 0xff;
34 }
35
36 /*
37  * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may
38  * change when NumVFs changes.
39  *
40  * Update iov->offset and iov->stride when NumVFs is written.
41  */
42 static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn)
43 {
44         struct pci_sriov *iov = dev->sriov;
45
46         pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn);
47         pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
48         pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
49 }
50
51 /*
52  * The PF consumes one bus number.  NumVFs, First VF Offset, and VF Stride
53  * determine how many additional bus numbers will be consumed by VFs.
54  *
55  * Iterate over all valid NumVFs, validate offset and stride, and calculate
56  * the maximum number of bus numbers that could ever be required.
57  */
58 static int compute_max_vf_buses(struct pci_dev *dev)
59 {
60         struct pci_sriov *iov = dev->sriov;
61         int nr_virtfn, busnr, rc = 0;
62
63         for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) {
64                 pci_iov_set_numvfs(dev, nr_virtfn);
65                 if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) {
66                         rc = -EIO;
67                         goto out;
68                 }
69
70                 busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
71                 if (busnr > iov->max_VF_buses)
72                         iov->max_VF_buses = busnr;
73         }
74
75 out:
76         pci_iov_set_numvfs(dev, 0);
77         return rc;
78 }
79
80 static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr)
81 {
82         struct pci_bus *child;
83
84         if (bus->number == busnr)
85                 return bus;
86
87         child = pci_find_bus(pci_domain_nr(bus), busnr);
88         if (child)
89                 return child;
90
91         child = pci_add_new_bus(bus, NULL, busnr);
92         if (!child)
93                 return NULL;
94
95         pci_bus_insert_busn_res(child, busnr, busnr);
96
97         return child;
98 }
99
100 static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus)
101 {
102         if (physbus != virtbus && list_empty(&virtbus->devices))
103                 pci_remove_bus(virtbus);
104 }
105
106 resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
107 {
108         if (!dev->is_physfn)
109                 return 0;
110
111         return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
112 }
113
114 static void pci_read_vf_config_common(struct pci_dev *virtfn)
115 {
116         struct pci_dev *physfn = virtfn->physfn;
117
118         /*
119          * Some config registers are the same across all associated VFs.
120          * Read them once from VF0 so we can skip reading them from the
121          * other VFs.
122          *
123          * PCIe r4.0, sec 9.3.4.1, technically doesn't require all VFs to
124          * have the same Revision ID and Subsystem ID, but we assume they
125          * do.
126          */
127         pci_read_config_dword(virtfn, PCI_CLASS_REVISION,
128                               &physfn->sriov->class);
129         pci_read_config_byte(virtfn, PCI_HEADER_TYPE,
130                              &physfn->sriov->hdr_type);
131         pci_read_config_word(virtfn, PCI_SUBSYSTEM_VENDOR_ID,
132                              &physfn->sriov->subsystem_vendor);
133         pci_read_config_word(virtfn, PCI_SUBSYSTEM_ID,
134                              &physfn->sriov->subsystem_device);
135 }
136
137 int pci_iov_add_virtfn(struct pci_dev *dev, int id)
138 {
139         int i;
140         int rc = -ENOMEM;
141         u64 size;
142         char buf[VIRTFN_ID_LEN];
143         struct pci_dev *virtfn;
144         struct resource *res;
145         struct pci_sriov *iov = dev->sriov;
146         struct pci_bus *bus;
147
148         bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id));
149         if (!bus)
150                 goto failed;
151
152         virtfn = pci_alloc_dev(bus);
153         if (!virtfn)
154                 goto failed0;
155
156         virtfn->devfn = pci_iov_virtfn_devfn(dev, id);
157         virtfn->vendor = dev->vendor;
158         virtfn->device = iov->vf_device;
159         virtfn->is_virtfn = 1;
160         virtfn->physfn = pci_dev_get(dev);
161
162         if (id == 0)
163                 pci_read_vf_config_common(virtfn);
164
165         rc = pci_setup_device(virtfn);
166         if (rc)
167                 goto failed1;
168
169         virtfn->dev.parent = dev->dev.parent;
170         virtfn->multifunction = 0;
171
172         for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
173                 res = &dev->resource[i + PCI_IOV_RESOURCES];
174                 if (!res->parent)
175                         continue;
176                 virtfn->resource[i].name = pci_name(virtfn);
177                 virtfn->resource[i].flags = res->flags;
178                 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
179                 virtfn->resource[i].start = res->start + size * id;
180                 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
181                 rc = request_resource(res, &virtfn->resource[i]);
182                 BUG_ON(rc);
183         }
184
185         pci_device_add(virtfn, virtfn->bus);
186
187         sprintf(buf, "virtfn%u", id);
188         rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf);
189         if (rc)
190                 goto failed1;
191         rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn");
192         if (rc)
193                 goto failed2;
194
195         kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE);
196
197         pci_bus_add_device(virtfn);
198
199         return 0;
200
201 failed2:
202         sysfs_remove_link(&dev->dev.kobj, buf);
203 failed1:
204         pci_stop_and_remove_bus_device(virtfn);
205         pci_dev_put(dev);
206 failed0:
207         virtfn_remove_bus(dev->bus, bus);
208 failed:
209
210         return rc;
211 }
212
213 void pci_iov_remove_virtfn(struct pci_dev *dev, int id)
214 {
215         char buf[VIRTFN_ID_LEN];
216         struct pci_dev *virtfn;
217
218         virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus),
219                                              pci_iov_virtfn_bus(dev, id),
220                                              pci_iov_virtfn_devfn(dev, id));
221         if (!virtfn)
222                 return;
223
224         sprintf(buf, "virtfn%u", id);
225         sysfs_remove_link(&dev->dev.kobj, buf);
226         /*
227          * pci_stop_dev() could have been called for this virtfn already,
228          * so the directory for the virtfn may have been removed before.
229          * Double check to avoid spurious sysfs warnings.
230          */
231         if (virtfn->dev.kobj.sd)
232                 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
233
234         pci_stop_and_remove_bus_device(virtfn);
235         virtfn_remove_bus(dev->bus, virtfn->bus);
236
237         /* balance pci_get_domain_bus_and_slot() */
238         pci_dev_put(virtfn);
239         pci_dev_put(dev);
240 }
241
242 static ssize_t sriov_totalvfs_show(struct device *dev,
243                                    struct device_attribute *attr,
244                                    char *buf)
245 {
246         struct pci_dev *pdev = to_pci_dev(dev);
247
248         return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
249 }
250
251 static ssize_t sriov_numvfs_show(struct device *dev,
252                                  struct device_attribute *attr,
253                                  char *buf)
254 {
255         struct pci_dev *pdev = to_pci_dev(dev);
256
257         return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
258 }
259
260 /*
261  * num_vfs > 0; number of VFs to enable
262  * num_vfs = 0; disable all VFs
263  *
264  * Note: SRIOV spec does not allow partial VF
265  *       disable, so it's all or none.
266  */
267 static ssize_t sriov_numvfs_store(struct device *dev,
268                                   struct device_attribute *attr,
269                                   const char *buf, size_t count)
270 {
271         struct pci_dev *pdev = to_pci_dev(dev);
272         int ret;
273         u16 num_vfs;
274
275         ret = kstrtou16(buf, 0, &num_vfs);
276         if (ret < 0)
277                 return ret;
278
279         if (num_vfs > pci_sriov_get_totalvfs(pdev))
280                 return -ERANGE;
281
282         device_lock(&pdev->dev);
283
284         if (num_vfs == pdev->sriov->num_VFs)
285                 goto exit;
286
287         /* is PF driver loaded w/callback */
288         if (!pdev->driver || !pdev->driver->sriov_configure) {
289                 pci_info(pdev, "Driver does not support SRIOV configuration via sysfs\n");
290                 ret = -ENOENT;
291                 goto exit;
292         }
293
294         if (num_vfs == 0) {
295                 /* disable VFs */
296                 ret = pdev->driver->sriov_configure(pdev, 0);
297                 goto exit;
298         }
299
300         /* enable VFs */
301         if (pdev->sriov->num_VFs) {
302                 pci_warn(pdev, "%d VFs already enabled. Disable before enabling %d VFs\n",
303                          pdev->sriov->num_VFs, num_vfs);
304                 ret = -EBUSY;
305                 goto exit;
306         }
307
308         ret = pdev->driver->sriov_configure(pdev, num_vfs);
309         if (ret < 0)
310                 goto exit;
311
312         if (ret != num_vfs)
313                 pci_warn(pdev, "%d VFs requested; only %d enabled\n",
314                          num_vfs, ret);
315
316 exit:
317         device_unlock(&pdev->dev);
318
319         if (ret < 0)
320                 return ret;
321
322         return count;
323 }
324
325 static ssize_t sriov_offset_show(struct device *dev,
326                                  struct device_attribute *attr,
327                                  char *buf)
328 {
329         struct pci_dev *pdev = to_pci_dev(dev);
330
331         return sprintf(buf, "%u\n", pdev->sriov->offset);
332 }
333
334 static ssize_t sriov_stride_show(struct device *dev,
335                                  struct device_attribute *attr,
336                                  char *buf)
337 {
338         struct pci_dev *pdev = to_pci_dev(dev);
339
340         return sprintf(buf, "%u\n", pdev->sriov->stride);
341 }
342
343 static ssize_t sriov_vf_device_show(struct device *dev,
344                                     struct device_attribute *attr,
345                                     char *buf)
346 {
347         struct pci_dev *pdev = to_pci_dev(dev);
348
349         return sprintf(buf, "%x\n", pdev->sriov->vf_device);
350 }
351
352 static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
353                                             struct device_attribute *attr,
354                                             char *buf)
355 {
356         struct pci_dev *pdev = to_pci_dev(dev);
357
358         return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe);
359 }
360
361 static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
362                                              struct device_attribute *attr,
363                                              const char *buf, size_t count)
364 {
365         struct pci_dev *pdev = to_pci_dev(dev);
366         bool drivers_autoprobe;
367
368         if (kstrtobool(buf, &drivers_autoprobe) < 0)
369                 return -EINVAL;
370
371         pdev->sriov->drivers_autoprobe = drivers_autoprobe;
372
373         return count;
374 }
375
376 static DEVICE_ATTR_RO(sriov_totalvfs);
377 static DEVICE_ATTR_RW(sriov_numvfs);
378 static DEVICE_ATTR_RO(sriov_offset);
379 static DEVICE_ATTR_RO(sriov_stride);
380 static DEVICE_ATTR_RO(sriov_vf_device);
381 static DEVICE_ATTR_RW(sriov_drivers_autoprobe);
382
383 static struct attribute *sriov_dev_attrs[] = {
384         &dev_attr_sriov_totalvfs.attr,
385         &dev_attr_sriov_numvfs.attr,
386         &dev_attr_sriov_offset.attr,
387         &dev_attr_sriov_stride.attr,
388         &dev_attr_sriov_vf_device.attr,
389         &dev_attr_sriov_drivers_autoprobe.attr,
390         NULL,
391 };
392
393 static umode_t sriov_attrs_are_visible(struct kobject *kobj,
394                                        struct attribute *a, int n)
395 {
396         struct device *dev = kobj_to_dev(kobj);
397
398         if (!dev_is_pf(dev))
399                 return 0;
400
401         return a->mode;
402 }
403
404 const struct attribute_group sriov_dev_attr_group = {
405         .attrs = sriov_dev_attrs,
406         .is_visible = sriov_attrs_are_visible,
407 };
408
409 int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
410 {
411         return 0;
412 }
413
414 int __weak pcibios_sriov_disable(struct pci_dev *pdev)
415 {
416         return 0;
417 }
418
419 static int sriov_add_vfs(struct pci_dev *dev, u16 num_vfs)
420 {
421         unsigned int i;
422         int rc;
423
424         if (dev->no_vf_scan)
425                 return 0;
426
427         for (i = 0; i < num_vfs; i++) {
428                 rc = pci_iov_add_virtfn(dev, i);
429                 if (rc)
430                         goto failed;
431         }
432         return 0;
433 failed:
434         while (i--)
435                 pci_iov_remove_virtfn(dev, i);
436
437         return rc;
438 }
439
440 static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
441 {
442         int rc;
443         int i;
444         int nres;
445         u16 initial;
446         struct resource *res;
447         struct pci_dev *pdev;
448         struct pci_sriov *iov = dev->sriov;
449         int bars = 0;
450         int bus;
451
452         if (!nr_virtfn)
453                 return 0;
454
455         if (iov->num_VFs)
456                 return -EINVAL;
457
458         pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
459         if (initial > iov->total_VFs ||
460             (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
461                 return -EIO;
462
463         if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
464             (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
465                 return -EINVAL;
466
467         nres = 0;
468         for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
469                 bars |= (1 << (i + PCI_IOV_RESOURCES));
470                 res = &dev->resource[i + PCI_IOV_RESOURCES];
471                 if (res->parent)
472                         nres++;
473         }
474         if (nres != iov->nres) {
475                 pci_err(dev, "not enough MMIO resources for SR-IOV\n");
476                 return -ENOMEM;
477         }
478
479         bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1);
480         if (bus > dev->bus->busn_res.end) {
481                 pci_err(dev, "can't enable %d VFs (bus %02x out of range of %pR)\n",
482                         nr_virtfn, bus, &dev->bus->busn_res);
483                 return -ENOMEM;
484         }
485
486         if (pci_enable_resources(dev, bars)) {
487                 pci_err(dev, "SR-IOV: IOV BARS not allocated\n");
488                 return -ENOMEM;
489         }
490
491         if (iov->link != dev->devfn) {
492                 pdev = pci_get_slot(dev->bus, iov->link);
493                 if (!pdev)
494                         return -ENODEV;
495
496                 if (!pdev->is_physfn) {
497                         pci_dev_put(pdev);
498                         return -ENOSYS;
499                 }
500
501                 rc = sysfs_create_link(&dev->dev.kobj,
502                                         &pdev->dev.kobj, "dep_link");
503                 pci_dev_put(pdev);
504                 if (rc)
505                         return rc;
506         }
507
508         iov->initial_VFs = initial;
509         if (nr_virtfn < initial)
510                 initial = nr_virtfn;
511
512         rc = pcibios_sriov_enable(dev, initial);
513         if (rc) {
514                 pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc);
515                 goto err_pcibios;
516         }
517
518         pci_iov_set_numvfs(dev, nr_virtfn);
519         iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
520         pci_cfg_access_lock(dev);
521         pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
522         msleep(100);
523         pci_cfg_access_unlock(dev);
524
525         rc = sriov_add_vfs(dev, initial);
526         if (rc)
527                 goto err_pcibios;
528
529         kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
530         iov->num_VFs = nr_virtfn;
531
532         return 0;
533
534 err_pcibios:
535         iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
536         pci_cfg_access_lock(dev);
537         pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
538         ssleep(1);
539         pci_cfg_access_unlock(dev);
540
541         pcibios_sriov_disable(dev);
542
543         if (iov->link != dev->devfn)
544                 sysfs_remove_link(&dev->dev.kobj, "dep_link");
545
546         pci_iov_set_numvfs(dev, 0);
547         return rc;
548 }
549
550 static void sriov_del_vfs(struct pci_dev *dev)
551 {
552         struct pci_sriov *iov = dev->sriov;
553         int i;
554
555         if (dev->no_vf_scan)
556                 return;
557
558         for (i = 0; i < iov->num_VFs; i++)
559                 pci_iov_remove_virtfn(dev, i);
560 }
561
562 static void sriov_disable(struct pci_dev *dev)
563 {
564         struct pci_sriov *iov = dev->sriov;
565
566         if (!iov->num_VFs)
567                 return;
568
569         sriov_del_vfs(dev);
570         iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
571         pci_cfg_access_lock(dev);
572         pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
573         ssleep(1);
574         pci_cfg_access_unlock(dev);
575
576         pcibios_sriov_disable(dev);
577
578         if (iov->link != dev->devfn)
579                 sysfs_remove_link(&dev->dev.kobj, "dep_link");
580
581         iov->num_VFs = 0;
582         pci_iov_set_numvfs(dev, 0);
583 }
584
585 static int sriov_init(struct pci_dev *dev, int pos)
586 {
587         int i, bar64;
588         int rc;
589         int nres;
590         u32 pgsz;
591         u16 ctrl, total;
592         struct pci_sriov *iov;
593         struct resource *res;
594         struct pci_dev *pdev;
595
596         pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl);
597         if (ctrl & PCI_SRIOV_CTRL_VFE) {
598                 pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0);
599                 ssleep(1);
600         }
601
602         ctrl = 0;
603         list_for_each_entry(pdev, &dev->bus->devices, bus_list)
604                 if (pdev->is_physfn)
605                         goto found;
606
607         pdev = NULL;
608         if (pci_ari_enabled(dev->bus))
609                 ctrl |= PCI_SRIOV_CTRL_ARI;
610
611 found:
612         pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl);
613
614         pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total);
615         if (!total)
616                 return 0;
617
618         pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz);
619         i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0;
620         pgsz &= ~((1 << i) - 1);
621         if (!pgsz)
622                 return -EIO;
623
624         pgsz &= ~(pgsz - 1);
625         pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
626
627         iov = kzalloc(sizeof(*iov), GFP_KERNEL);
628         if (!iov)
629                 return -ENOMEM;
630
631         nres = 0;
632         for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
633                 res = &dev->resource[i + PCI_IOV_RESOURCES];
634                 /*
635                  * If it is already FIXED, don't change it, something
636                  * (perhaps EA or header fixups) wants it this way.
637                  */
638                 if (res->flags & IORESOURCE_PCI_FIXED)
639                         bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0;
640                 else
641                         bar64 = __pci_read_base(dev, pci_bar_unknown, res,
642                                                 pos + PCI_SRIOV_BAR + i * 4);
643                 if (!res->flags)
644                         continue;
645                 if (resource_size(res) & (PAGE_SIZE - 1)) {
646                         rc = -EIO;
647                         goto failed;
648                 }
649                 iov->barsz[i] = resource_size(res);
650                 res->end = res->start + resource_size(res) * total - 1;
651                 pci_info(dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n",
652                          i, res, i, total);
653                 i += bar64;
654                 nres++;
655         }
656
657         iov->pos = pos;
658         iov->nres = nres;
659         iov->ctrl = ctrl;
660         iov->total_VFs = total;
661         iov->driver_max_VFs = total;
662         pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device);
663         iov->pgsz = pgsz;
664         iov->self = dev;
665         iov->drivers_autoprobe = true;
666         pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
667         pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
668         if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END)
669                 iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link);
670
671         if (pdev)
672                 iov->dev = pci_dev_get(pdev);
673         else
674                 iov->dev = dev;
675
676         dev->sriov = iov;
677         dev->is_physfn = 1;
678         rc = compute_max_vf_buses(dev);
679         if (rc)
680                 goto fail_max_buses;
681
682         return 0;
683
684 fail_max_buses:
685         dev->sriov = NULL;
686         dev->is_physfn = 0;
687 failed:
688         for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
689                 res = &dev->resource[i + PCI_IOV_RESOURCES];
690                 res->flags = 0;
691         }
692
693         kfree(iov);
694         return rc;
695 }
696
697 static void sriov_release(struct pci_dev *dev)
698 {
699         BUG_ON(dev->sriov->num_VFs);
700
701         if (dev != dev->sriov->dev)
702                 pci_dev_put(dev->sriov->dev);
703
704         kfree(dev->sriov);
705         dev->sriov = NULL;
706 }
707
708 static void sriov_restore_state(struct pci_dev *dev)
709 {
710         int i;
711         u16 ctrl;
712         struct pci_sriov *iov = dev->sriov;
713
714         pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl);
715         if (ctrl & PCI_SRIOV_CTRL_VFE)
716                 return;
717
718         /*
719          * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because
720          * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI.
721          */
722         ctrl &= ~PCI_SRIOV_CTRL_ARI;
723         ctrl |= iov->ctrl & PCI_SRIOV_CTRL_ARI;
724         pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
725
726         for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
727                 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
728
729         pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
730         pci_iov_set_numvfs(dev, iov->num_VFs);
731         pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
732         if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
733                 msleep(100);
734 }
735
736 /**
737  * pci_iov_init - initialize the IOV capability
738  * @dev: the PCI device
739  *
740  * Returns 0 on success, or negative on failure.
741  */
742 int pci_iov_init(struct pci_dev *dev)
743 {
744         int pos;
745
746         if (!pci_is_pcie(dev))
747                 return -ENODEV;
748
749         pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
750         if (pos)
751                 return sriov_init(dev, pos);
752
753         return -ENODEV;
754 }
755
756 /**
757  * pci_iov_release - release resources used by the IOV capability
758  * @dev: the PCI device
759  */
760 void pci_iov_release(struct pci_dev *dev)
761 {
762         if (dev->is_physfn)
763                 sriov_release(dev);
764 }
765
766 /**
767  * pci_iov_remove - clean up SR-IOV state after PF driver is detached
768  * @dev: the PCI device
769  */
770 void pci_iov_remove(struct pci_dev *dev)
771 {
772         struct pci_sriov *iov = dev->sriov;
773
774         if (!dev->is_physfn)
775                 return;
776
777         iov->driver_max_VFs = iov->total_VFs;
778         if (iov->num_VFs)
779                 pci_warn(dev, "driver left SR-IOV enabled after remove\n");
780 }
781
782 /**
783  * pci_iov_update_resource - update a VF BAR
784  * @dev: the PCI device
785  * @resno: the resource number
786  *
787  * Update a VF BAR in the SR-IOV capability of a PF.
788  */
789 void pci_iov_update_resource(struct pci_dev *dev, int resno)
790 {
791         struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
792         struct resource *res = dev->resource + resno;
793         int vf_bar = resno - PCI_IOV_RESOURCES;
794         struct pci_bus_region region;
795         u16 cmd;
796         u32 new;
797         int reg;
798
799         /*
800          * The generic pci_restore_bars() path calls this for all devices,
801          * including VFs and non-SR-IOV devices.  If this is not a PF, we
802          * have nothing to do.
803          */
804         if (!iov)
805                 return;
806
807         pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd);
808         if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) {
809                 dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n",
810                          vf_bar, res);
811                 return;
812         }
813
814         /*
815          * Ignore unimplemented BARs, unused resource slots for 64-bit
816          * BARs, and non-movable resources, e.g., those described via
817          * Enhanced Allocation.
818          */
819         if (!res->flags)
820                 return;
821
822         if (res->flags & IORESOURCE_UNSET)
823                 return;
824
825         if (res->flags & IORESOURCE_PCI_FIXED)
826                 return;
827
828         pcibios_resource_to_bus(dev->bus, &region, res);
829         new = region.start;
830         new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK;
831
832         reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar;
833         pci_write_config_dword(dev, reg, new);
834         if (res->flags & IORESOURCE_MEM_64) {
835                 new = region.start >> 16 >> 16;
836                 pci_write_config_dword(dev, reg + 4, new);
837         }
838 }
839
840 resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev,
841                                                       int resno)
842 {
843         return pci_iov_resource_size(dev, resno);
844 }
845
846 /**
847  * pci_sriov_resource_alignment - get resource alignment for VF BAR
848  * @dev: the PCI device
849  * @resno: the resource number
850  *
851  * Returns the alignment of the VF BAR found in the SR-IOV capability.
852  * This is not the same as the resource size which is defined as
853  * the VF BAR size multiplied by the number of VFs.  The alignment
854  * is just the VF BAR size.
855  */
856 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno)
857 {
858         return pcibios_iov_resource_alignment(dev, resno);
859 }
860
861 /**
862  * pci_restore_iov_state - restore the state of the IOV capability
863  * @dev: the PCI device
864  */
865 void pci_restore_iov_state(struct pci_dev *dev)
866 {
867         if (dev->is_physfn)
868                 sriov_restore_state(dev);
869 }
870
871 /**
872  * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs
873  * @dev: the PCI device
874  * @auto_probe: set VF drivers auto probe flag
875  */
876 void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe)
877 {
878         if (dev->is_physfn)
879                 dev->sriov->drivers_autoprobe = auto_probe;
880 }
881
882 /**
883  * pci_iov_bus_range - find bus range used by Virtual Function
884  * @bus: the PCI bus
885  *
886  * Returns max number of buses (exclude current one) used by Virtual
887  * Functions.
888  */
889 int pci_iov_bus_range(struct pci_bus *bus)
890 {
891         int max = 0;
892         struct pci_dev *dev;
893
894         list_for_each_entry(dev, &bus->devices, bus_list) {
895                 if (!dev->is_physfn)
896                         continue;
897                 if (dev->sriov->max_VF_buses > max)
898                         max = dev->sriov->max_VF_buses;
899         }
900
901         return max ? max - bus->number : 0;
902 }
903
904 /**
905  * pci_enable_sriov - enable the SR-IOV capability
906  * @dev: the PCI device
907  * @nr_virtfn: number of virtual functions to enable
908  *
909  * Returns 0 on success, or negative on failure.
910  */
911 int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
912 {
913         might_sleep();
914
915         if (!dev->is_physfn)
916                 return -ENOSYS;
917
918         return sriov_enable(dev, nr_virtfn);
919 }
920 EXPORT_SYMBOL_GPL(pci_enable_sriov);
921
922 /**
923  * pci_disable_sriov - disable the SR-IOV capability
924  * @dev: the PCI device
925  */
926 void pci_disable_sriov(struct pci_dev *dev)
927 {
928         might_sleep();
929
930         if (!dev->is_physfn)
931                 return;
932
933         sriov_disable(dev);
934 }
935 EXPORT_SYMBOL_GPL(pci_disable_sriov);
936
937 /**
938  * pci_num_vf - return number of VFs associated with a PF device_release_driver
939  * @dev: the PCI device
940  *
941  * Returns number of VFs, or 0 if SR-IOV is not enabled.
942  */
943 int pci_num_vf(struct pci_dev *dev)
944 {
945         if (!dev->is_physfn)
946                 return 0;
947
948         return dev->sriov->num_VFs;
949 }
950 EXPORT_SYMBOL_GPL(pci_num_vf);
951
952 /**
953  * pci_vfs_assigned - returns number of VFs are assigned to a guest
954  * @dev: the PCI device
955  *
956  * Returns number of VFs belonging to this device that are assigned to a guest.
957  * If device is not a physical function returns 0.
958  */
959 int pci_vfs_assigned(struct pci_dev *dev)
960 {
961         struct pci_dev *vfdev;
962         unsigned int vfs_assigned = 0;
963         unsigned short dev_id;
964
965         /* only search if we are a PF */
966         if (!dev->is_physfn)
967                 return 0;
968
969         /*
970          * determine the device ID for the VFs, the vendor ID will be the
971          * same as the PF so there is no need to check for that one
972          */
973         dev_id = dev->sriov->vf_device;
974
975         /* loop through all the VFs to see if we own any that are assigned */
976         vfdev = pci_get_device(dev->vendor, dev_id, NULL);
977         while (vfdev) {
978                 /*
979                  * It is considered assigned if it is a virtual function with
980                  * our dev as the physical function and the assigned bit is set
981                  */
982                 if (vfdev->is_virtfn && (vfdev->physfn == dev) &&
983                         pci_is_dev_assigned(vfdev))
984                         vfs_assigned++;
985
986                 vfdev = pci_get_device(dev->vendor, dev_id, vfdev);
987         }
988
989         return vfs_assigned;
990 }
991 EXPORT_SYMBOL_GPL(pci_vfs_assigned);
992
993 /**
994  * pci_sriov_set_totalvfs -- reduce the TotalVFs available
995  * @dev: the PCI PF device
996  * @numvfs: number that should be used for TotalVFs supported
997  *
998  * Should be called from PF driver's probe routine with
999  * device's mutex held.
1000  *
1001  * Returns 0 if PF is an SRIOV-capable device and
1002  * value of numvfs valid. If not a PF return -ENOSYS;
1003  * if numvfs is invalid return -EINVAL;
1004  * if VFs already enabled, return -EBUSY.
1005  */
1006 int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
1007 {
1008         if (!dev->is_physfn)
1009                 return -ENOSYS;
1010
1011         if (numvfs > dev->sriov->total_VFs)
1012                 return -EINVAL;
1013
1014         /* Shouldn't change if VFs already enabled */
1015         if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
1016                 return -EBUSY;
1017
1018         dev->sriov->driver_max_VFs = numvfs;
1019         return 0;
1020 }
1021 EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
1022
1023 /**
1024  * pci_sriov_get_totalvfs -- get total VFs supported on this device
1025  * @dev: the PCI PF device
1026  *
1027  * For a PCIe device with SRIOV support, return the PCIe
1028  * SRIOV capability value of TotalVFs or the value of driver_max_VFs
1029  * if the driver reduced it.  Otherwise 0.
1030  */
1031 int pci_sriov_get_totalvfs(struct pci_dev *dev)
1032 {
1033         if (!dev->is_physfn)
1034                 return 0;
1035
1036         return dev->sriov->driver_max_VFs;
1037 }
1038 EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);
1039
1040 /**
1041  * pci_sriov_configure_simple - helper to configure SR-IOV
1042  * @dev: the PCI device
1043  * @nr_virtfn: number of virtual functions to enable, 0 to disable
1044  *
1045  * Enable or disable SR-IOV for devices that don't require any PF setup
1046  * before enabling SR-IOV.  Return value is negative on error, or number of
1047  * VFs allocated on success.
1048  */
1049 int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn)
1050 {
1051         int rc;
1052
1053         might_sleep();
1054
1055         if (!dev->is_physfn)
1056                 return -ENODEV;
1057
1058         if (pci_vfs_assigned(dev)) {
1059                 pci_warn(dev, "Cannot modify SR-IOV while VFs are assigned\n");
1060                 return -EPERM;
1061         }
1062
1063         if (nr_virtfn == 0) {
1064                 sriov_disable(dev);
1065                 return 0;
1066         }
1067
1068         rc = sriov_enable(dev, nr_virtfn);
1069         if (rc < 0)
1070                 return rc;
1071
1072         return nr_virtfn;
1073 }
1074 EXPORT_SYMBOL_GPL(pci_sriov_configure_simple);