This source file includes following definitions.
- pci_iov_virtfn_bus
- pci_iov_virtfn_devfn
- pci_iov_set_numvfs
- compute_max_vf_buses
- virtfn_add_bus
- virtfn_remove_bus
- pci_iov_resource_size
- pci_read_vf_config_common
- pci_iov_add_virtfn
- pci_iov_remove_virtfn
- sriov_totalvfs_show
- sriov_numvfs_show
- sriov_numvfs_store
- sriov_offset_show
- sriov_stride_show
- sriov_vf_device_show
- sriov_drivers_autoprobe_show
- sriov_drivers_autoprobe_store
- sriov_attrs_are_visible
- pcibios_sriov_enable
- pcibios_sriov_disable
- sriov_add_vfs
- sriov_enable
- sriov_del_vfs
- sriov_disable
- sriov_init
- sriov_release
- sriov_restore_state
- pci_iov_init
- pci_iov_release
- pci_iov_remove
- pci_iov_update_resource
- pcibios_iov_resource_alignment
- pci_sriov_resource_alignment
- pci_restore_iov_state
- pci_vf_drivers_autoprobe
- pci_iov_bus_range
- pci_enable_sriov
- pci_disable_sriov
- pci_num_vf
- pci_vfs_assigned
- pci_sriov_set_totalvfs
- pci_sriov_get_totalvfs
- pci_sriov_configure_simple
1
2
3
4
5
6
7
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
38
39
40
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
53
54
55
56
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
120
121
122
123
124
125
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
228
229
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
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
262
263
264
265
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
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
296 ret = pdev->driver->sriov_configure(pdev, 0);
297 goto exit;
298 }
299
300
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
636
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
720
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
738
739
740
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
758
759
760 void pci_iov_release(struct pci_dev *dev)
761 {
762 if (dev->is_physfn)
763 sriov_release(dev);
764 }
765
766
767
768
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
784
785
786
787
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
801
802
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
816
817
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, ®ion, 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
848
849
850
851
852
853
854
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
863
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
873
874
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
884
885
886
887
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
906
907
908
909
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
924
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
939
940
941
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
954
955
956
957
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
966 if (!dev->is_physfn)
967 return 0;
968
969
970
971
972
973 dev_id = dev->sriov->vf_device;
974
975
976 vfdev = pci_get_device(dev->vendor, dev_id, NULL);
977 while (vfdev) {
978
979
980
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
995
996
997
998
999
1000
1001
1002
1003
1004
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
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
1025
1026
1027
1028
1029
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
1042
1043
1044
1045
1046
1047
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);