aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pci
diff options
context:
space:
mode:
authorBjorn Helgaas <bhelgaas@google.com>2012-11-13 16:33:32 -0500
committerBjorn Helgaas <bhelgaas@google.com>2012-11-13 16:33:32 -0500
commitf9c15b429a5c82e613d59a32d4f49cea6e9d64eb (patch)
treeb0862edccf51627ed70b6beaf06baaf2134844ea /drivers/pci
parent0dcccc5c53c55565a6b1061e1b15894495c7c9b9 (diff)
parent1452cd76a97bf7b93a015586dcabc73fd935e692 (diff)
Merge branch 'pci/don-sriov' into next
* pci/don-sriov: PCI: Remove useless "!dev" tests PCI: Use spec names for SR-IOV capability fields PCI: Provide method to reduce the number of total VFs supported PCI: SRIOV control and status via sysfs PCI: Use is_visible() with boot_vga attribute for pci_dev PCI: Add pci_device_type to pdev's device struct
Diffstat (limited to 'drivers/pci')
-rw-r--r--drivers/pci/iov.c87
-rw-r--r--drivers/pci/pci-sysfs.c172
-rw-r--r--drivers/pci/pci.h8
-rw-r--r--drivers/pci/probe.c1
4 files changed, 234 insertions, 34 deletions
diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
index aeccc911abb8..bafd2bbcaf65 100644
--- a/drivers/pci/iov.c
+++ b/drivers/pci/iov.c
@@ -106,7 +106,7 @@ static int virtfn_add(struct pci_dev *dev, int id, int reset)
106 virtfn->resource[i].name = pci_name(virtfn); 106 virtfn->resource[i].name = pci_name(virtfn);
107 virtfn->resource[i].flags = res->flags; 107 virtfn->resource[i].flags = res->flags;
108 size = resource_size(res); 108 size = resource_size(res);
109 do_div(size, iov->total); 109 do_div(size, iov->total_VFs);
110 virtfn->resource[i].start = res->start + size * id; 110 virtfn->resource[i].start = res->start + size * id;
111 virtfn->resource[i].end = virtfn->resource[i].start + size - 1; 111 virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
112 rc = request_resource(res, &virtfn->resource[i]); 112 rc = request_resource(res, &virtfn->resource[i]);
@@ -194,7 +194,7 @@ static int sriov_migration(struct pci_dev *dev)
194 u16 status; 194 u16 status;
195 struct pci_sriov *iov = dev->sriov; 195 struct pci_sriov *iov = dev->sriov;
196 196
197 if (!iov->nr_virtfn) 197 if (!iov->num_VFs)
198 return 0; 198 return 0;
199 199
200 if (!(iov->cap & PCI_SRIOV_CAP_VFM)) 200 if (!(iov->cap & PCI_SRIOV_CAP_VFM))
@@ -216,7 +216,7 @@ static void sriov_migration_task(struct work_struct *work)
216 u16 status; 216 u16 status;
217 struct pci_sriov *iov = container_of(work, struct pci_sriov, mtask); 217 struct pci_sriov *iov = container_of(work, struct pci_sriov, mtask);
218 218
219 for (i = iov->initial; i < iov->nr_virtfn; i++) { 219 for (i = iov->initial_VFs; i < iov->num_VFs; i++) {
220 state = readb(iov->mstate + i); 220 state = readb(iov->mstate + i);
221 if (state == PCI_SRIOV_VFM_MI) { 221 if (state == PCI_SRIOV_VFM_MI) {
222 writeb(PCI_SRIOV_VFM_AV, iov->mstate + i); 222 writeb(PCI_SRIOV_VFM_AV, iov->mstate + i);
@@ -244,7 +244,7 @@ static int sriov_enable_migration(struct pci_dev *dev, int nr_virtfn)
244 resource_size_t pa; 244 resource_size_t pa;
245 struct pci_sriov *iov = dev->sriov; 245 struct pci_sriov *iov = dev->sriov;
246 246
247 if (nr_virtfn <= iov->initial) 247 if (nr_virtfn <= iov->initial_VFs)
248 return 0; 248 return 0;
249 249
250 pci_read_config_dword(dev, iov->pos + PCI_SRIOV_VFM, &table); 250 pci_read_config_dword(dev, iov->pos + PCI_SRIOV_VFM, &table);
@@ -294,15 +294,15 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
294 if (!nr_virtfn) 294 if (!nr_virtfn)
295 return 0; 295 return 0;
296 296
297 if (iov->nr_virtfn) 297 if (iov->num_VFs)
298 return -EINVAL; 298 return -EINVAL;
299 299
300 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial); 300 pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial);
301 if (initial > iov->total || 301 if (initial > iov->total_VFs ||
302 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total))) 302 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs)))
303 return -EIO; 303 return -EIO;
304 304
305 if (nr_virtfn < 0 || nr_virtfn > iov->total || 305 if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs ||
306 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial))) 306 (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial)))
307 return -EINVAL; 307 return -EINVAL;
308 308
@@ -359,7 +359,7 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
359 msleep(100); 359 msleep(100);
360 pci_cfg_access_unlock(dev); 360 pci_cfg_access_unlock(dev);
361 361
362 iov->initial = initial; 362 iov->initial_VFs = initial;
363 if (nr_virtfn < initial) 363 if (nr_virtfn < initial)
364 initial = nr_virtfn; 364 initial = nr_virtfn;
365 365
@@ -376,7 +376,7 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
376 } 376 }
377 377
378 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE); 378 kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
379 iov->nr_virtfn = nr_virtfn; 379 iov->num_VFs = nr_virtfn;
380 380
381 return 0; 381 return 0;
382 382
@@ -401,13 +401,13 @@ static void sriov_disable(struct pci_dev *dev)
401 int i; 401 int i;
402 struct pci_sriov *iov = dev->sriov; 402 struct pci_sriov *iov = dev->sriov;
403 403
404 if (!iov->nr_virtfn) 404 if (!iov->num_VFs)
405 return; 405 return;
406 406
407 if (iov->cap & PCI_SRIOV_CAP_VFM) 407 if (iov->cap & PCI_SRIOV_CAP_VFM)
408 sriov_disable_migration(dev); 408 sriov_disable_migration(dev);
409 409
410 for (i = 0; i < iov->nr_virtfn; i++) 410 for (i = 0; i < iov->num_VFs; i++)
411 virtfn_remove(dev, i, 0); 411 virtfn_remove(dev, i, 0);
412 412
413 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE); 413 iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE);
@@ -419,7 +419,7 @@ static void sriov_disable(struct pci_dev *dev)
419 if (iov->link != dev->devfn) 419 if (iov->link != dev->devfn)
420 sysfs_remove_link(&dev->dev.kobj, "dep_link"); 420 sysfs_remove_link(&dev->dev.kobj, "dep_link");
421 421
422 iov->nr_virtfn = 0; 422 iov->num_VFs = 0;
423} 423}
424 424
425static int sriov_init(struct pci_dev *dev, int pos) 425static int sriov_init(struct pci_dev *dev, int pos)
@@ -496,7 +496,7 @@ found:
496 iov->pos = pos; 496 iov->pos = pos;
497 iov->nres = nres; 497 iov->nres = nres;
498 iov->ctrl = ctrl; 498 iov->ctrl = ctrl;
499 iov->total = total; 499 iov->total_VFs = total;
500 iov->offset = offset; 500 iov->offset = offset;
501 iov->stride = stride; 501 iov->stride = stride;
502 iov->pgsz = pgsz; 502 iov->pgsz = pgsz;
@@ -529,7 +529,7 @@ failed:
529 529
530static void sriov_release(struct pci_dev *dev) 530static void sriov_release(struct pci_dev *dev)
531{ 531{
532 BUG_ON(dev->sriov->nr_virtfn); 532 BUG_ON(dev->sriov->num_VFs);
533 533
534 if (dev != dev->sriov->dev) 534 if (dev != dev->sriov->dev)
535 pci_dev_put(dev->sriov->dev); 535 pci_dev_put(dev->sriov->dev);
@@ -554,7 +554,7 @@ static void sriov_restore_state(struct pci_dev *dev)
554 pci_update_resource(dev, i); 554 pci_update_resource(dev, i);
555 555
556 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz); 556 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
557 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->nr_virtfn); 557 pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, iov->num_VFs);
558 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); 558 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
559 if (iov->ctrl & PCI_SRIOV_CTRL_VFE) 559 if (iov->ctrl & PCI_SRIOV_CTRL_VFE)
560 msleep(100); 560 msleep(100);
@@ -661,7 +661,7 @@ int pci_iov_bus_range(struct pci_bus *bus)
661 list_for_each_entry(dev, &bus->devices, bus_list) { 661 list_for_each_entry(dev, &bus->devices, bus_list) {
662 if (!dev->is_physfn) 662 if (!dev->is_physfn)
663 continue; 663 continue;
664 busnr = virtfn_bus(dev, dev->sriov->total - 1); 664 busnr = virtfn_bus(dev, dev->sriov->total_VFs - 1);
665 if (busnr > max) 665 if (busnr > max)
666 max = busnr; 666 max = busnr;
667 } 667 }
@@ -729,9 +729,56 @@ EXPORT_SYMBOL_GPL(pci_sriov_migration);
729 */ 729 */
730int pci_num_vf(struct pci_dev *dev) 730int pci_num_vf(struct pci_dev *dev)
731{ 731{
732 if (!dev || !dev->is_physfn) 732 if (!dev->is_physfn)
733 return 0; 733 return 0;
734 else 734
735 return dev->sriov->nr_virtfn; 735 return dev->sriov->num_VFs;
736} 736}
737EXPORT_SYMBOL_GPL(pci_num_vf); 737EXPORT_SYMBOL_GPL(pci_num_vf);
738
739/**
740 * pci_sriov_set_totalvfs -- reduce the TotalVFs available
741 * @dev: the PCI PF device
742 * numvfs: number that should be used for TotalVFs supported
743 *
744 * Should be called from PF driver's probe routine with
745 * device's mutex held.
746 *
747 * Returns 0 if PF is an SRIOV-capable device and
748 * value of numvfs valid. If not a PF with VFS, return -EINVAL;
749 * if VFs already enabled, return -EBUSY.
750 */
751int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
752{
753 if (!dev->is_physfn || (numvfs > dev->sriov->total_VFs))
754 return -EINVAL;
755
756 /* Shouldn't change if VFs already enabled */
757 if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE)
758 return -EBUSY;
759 else
760 dev->sriov->driver_max_VFs = numvfs;
761
762 return 0;
763}
764EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs);
765
766/**
767 * pci_sriov_get_totalvfs -- get total VFs supported on this devic3
768 * @dev: the PCI PF device
769 *
770 * For a PCIe device with SRIOV support, return the PCIe
771 * SRIOV capability value of TotalVFs or the value of driver_max_VFs
772 * if the driver reduced it. Otherwise, -EINVAL.
773 */
774int pci_sriov_get_totalvfs(struct pci_dev *dev)
775{
776 if (!dev->is_physfn)
777 return -EINVAL;
778
779 if (dev->sriov->driver_max_VFs)
780 return dev->sriov->driver_max_VFs;
781
782 return dev->sriov->total_VFs;
783}
784EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs);
diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 02d107b15281..2f21991b8bcd 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -404,6 +404,106 @@ static ssize_t d3cold_allowed_show(struct device *dev,
404} 404}
405#endif 405#endif
406 406
407#ifdef CONFIG_PCI_IOV
408static ssize_t sriov_totalvfs_show(struct device *dev,
409 struct device_attribute *attr,
410 char *buf)
411{
412 struct pci_dev *pdev = to_pci_dev(dev);
413
414 return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
415}
416
417
418static ssize_t sriov_numvfs_show(struct device *dev,
419 struct device_attribute *attr,
420 char *buf)
421{
422 struct pci_dev *pdev = to_pci_dev(dev);
423
424 return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
425}
426
427/*
428 * num_vfs > 0; number of vfs to enable
429 * num_vfs = 0; disable all vfs
430 *
431 * Note: SRIOV spec doesn't allow partial VF
432 * disable, so its all or none.
433 */
434static ssize_t sriov_numvfs_store(struct device *dev,
435 struct device_attribute *attr,
436 const char *buf, size_t count)
437{
438 struct pci_dev *pdev = to_pci_dev(dev);
439 int num_vfs_enabled = 0;
440 int num_vfs;
441 int ret = 0;
442 u16 total;
443
444 if (kstrtoint(buf, 0, &num_vfs) < 0)
445 return -EINVAL;
446
447 /* is PF driver loaded w/callback */
448 if (!pdev->driver || !pdev->driver->sriov_configure) {
449 dev_info(&pdev->dev,
450 "Driver doesn't support SRIOV configuration via sysfs\n");
451 return -ENOSYS;
452 }
453
454 /* if enabling vf's ... */
455 total = pci_sriov_get_totalvfs(pdev);
456 /* Requested VFs to enable < totalvfs and none enabled already */
457 if ((num_vfs > 0) && (num_vfs <= total)) {
458 if (pdev->sriov->num_VFs == 0) {
459 num_vfs_enabled =
460 pdev->driver->sriov_configure(pdev, num_vfs);
461 if ((num_vfs_enabled >= 0) &&
462 (num_vfs_enabled != num_vfs)) {
463 dev_warn(&pdev->dev,
464 "Only %d VFs enabled\n",
465 num_vfs_enabled);
466 return count;
467 } else if (num_vfs_enabled < 0)
468 /* error code from driver callback */
469 return num_vfs_enabled;
470 } else if (num_vfs == pdev->sriov->num_VFs) {
471 dev_warn(&pdev->dev,
472 "%d VFs already enabled; no enable action taken\n",
473 num_vfs);
474 return count;
475 } else {
476 dev_warn(&pdev->dev,
477 "%d VFs already enabled. Disable before enabling %d VFs\n",
478 pdev->sriov->num_VFs, num_vfs);
479 return -EINVAL;
480 }
481 }
482
483 /* disable vfs */
484 if (num_vfs == 0) {
485 if (pdev->sriov->num_VFs != 0) {
486 ret = pdev->driver->sriov_configure(pdev, 0);
487 return ret ? ret : count;
488 } else {
489 dev_warn(&pdev->dev,
490 "All VFs disabled; no disable action taken\n");
491 return count;
492 }
493 }
494
495 dev_err(&pdev->dev,
496 "Invalid value for number of VFs to enable: %d\n", num_vfs);
497
498 return -EINVAL;
499}
500
501static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs);
502static struct device_attribute sriov_numvfs_attr =
503 __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP),
504 sriov_numvfs_show, sriov_numvfs_store);
505#endif /* CONFIG_PCI_IOV */
506
407struct device_attribute pci_dev_attrs[] = { 507struct device_attribute pci_dev_attrs[] = {
408 __ATTR_RO(resource), 508 __ATTR_RO(resource),
409 __ATTR_RO(vendor), 509 __ATTR_RO(vendor),
@@ -1303,29 +1403,20 @@ int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)
1303 pdev->rom_attr = attr; 1403 pdev->rom_attr = attr;
1304 } 1404 }
1305 1405
1306 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1307 retval = device_create_file(&pdev->dev, &vga_attr);
1308 if (retval)
1309 goto err_rom_file;
1310 }
1311
1312 /* add platform-specific attributes */ 1406 /* add platform-specific attributes */
1313 retval = pcibios_add_platform_entries(pdev); 1407 retval = pcibios_add_platform_entries(pdev);
1314 if (retval) 1408 if (retval)
1315 goto err_vga_file; 1409 goto err_rom_file;
1316 1410
1317 /* add sysfs entries for various capabilities */ 1411 /* add sysfs entries for various capabilities */
1318 retval = pci_create_capabilities_sysfs(pdev); 1412 retval = pci_create_capabilities_sysfs(pdev);
1319 if (retval) 1413 if (retval)
1320 goto err_vga_file; 1414 goto err_rom_file;
1321 1415
1322 pci_create_firmware_label_files(pdev); 1416 pci_create_firmware_label_files(pdev);
1323 1417
1324 return 0; 1418 return 0;
1325 1419
1326err_vga_file:
1327 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
1328 device_remove_file(&pdev->dev, &vga_attr);
1329err_rom_file: 1420err_rom_file:
1330 if (rom_size) { 1421 if (rom_size) {
1331 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr); 1422 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
@@ -1411,3 +1502,62 @@ static int __init pci_sysfs_init(void)
1411} 1502}
1412 1503
1413late_initcall(pci_sysfs_init); 1504late_initcall(pci_sysfs_init);
1505
1506static struct attribute *pci_dev_dev_attrs[] = {
1507 &vga_attr.attr,
1508 NULL,
1509};
1510
1511static umode_t pci_dev_attrs_are_visible(struct kobject *kobj,
1512 struct attribute *a, int n)
1513{
1514 struct device *dev = container_of(kobj, struct device, kobj);
1515 struct pci_dev *pdev = to_pci_dev(dev);
1516
1517 if (a == &vga_attr.attr)
1518 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1519 return 0;
1520
1521 return a->mode;
1522}
1523
1524#ifdef CONFIG_PCI_IOV
1525static struct attribute *sriov_dev_attrs[] = {
1526 &sriov_totalvfs_attr.attr,
1527 &sriov_numvfs_attr.attr,
1528 NULL,
1529};
1530
1531static umode_t sriov_attrs_are_visible(struct kobject *kobj,
1532 struct attribute *a, int n)
1533{
1534 struct device *dev = container_of(kobj, struct device, kobj);
1535
1536 if (!dev_is_pf(dev))
1537 return 0;
1538
1539 return a->mode;
1540}
1541
1542static struct attribute_group sriov_dev_attr_group = {
1543 .attrs = sriov_dev_attrs,
1544 .is_visible = sriov_attrs_are_visible,
1545};
1546#endif /* CONFIG_PCI_IOV */
1547
1548static struct attribute_group pci_dev_attr_group = {
1549 .attrs = pci_dev_dev_attrs,
1550 .is_visible = pci_dev_attrs_are_visible,
1551};
1552
1553static const struct attribute_group *pci_dev_attr_groups[] = {
1554 &pci_dev_attr_group,
1555#ifdef CONFIG_PCI_IOV
1556 &sriov_dev_attr_group,
1557#endif
1558 NULL,
1559};
1560
1561struct device_type pci_dev_type = {
1562 .groups = pci_dev_attr_groups,
1563};
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index bacbcba69cf3..3c9443ee00e6 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -157,6 +157,7 @@ static inline int pci_no_d1d2(struct pci_dev *dev)
157} 157}
158extern struct device_attribute pci_dev_attrs[]; 158extern struct device_attribute pci_dev_attrs[];
159extern struct device_attribute pcibus_dev_attrs[]; 159extern struct device_attribute pcibus_dev_attrs[];
160extern struct device_type pci_dev_type;
160#ifdef CONFIG_HOTPLUG 161#ifdef CONFIG_HOTPLUG
161extern struct bus_attribute pci_bus_attrs[]; 162extern struct bus_attribute pci_bus_attrs[];
162#else 163#else
@@ -232,13 +233,14 @@ struct pci_sriov {
232 int nres; /* number of resources */ 233 int nres; /* number of resources */
233 u32 cap; /* SR-IOV Capabilities */ 234 u32 cap; /* SR-IOV Capabilities */
234 u16 ctrl; /* SR-IOV Control */ 235 u16 ctrl; /* SR-IOV Control */
235 u16 total; /* total VFs associated with the PF */ 236 u16 total_VFs; /* total VFs associated with the PF */
236 u16 initial; /* initial VFs associated with the PF */ 237 u16 initial_VFs; /* initial VFs associated with the PF */
237 u16 nr_virtfn; /* number of VFs available */ 238 u16 num_VFs; /* number of VFs available */
238 u16 offset; /* first VF Routing ID offset */ 239 u16 offset; /* first VF Routing ID offset */
239 u16 stride; /* following VF stride */ 240 u16 stride; /* following VF stride */
240 u32 pgsz; /* page size for BAR alignment */ 241 u32 pgsz; /* page size for BAR alignment */
241 u8 link; /* Function Dependency Link */ 242 u8 link; /* Function Dependency Link */
243 u16 driver_max_VFs; /* max num VFs driver supports */
242 struct pci_dev *dev; /* lowest numbered PF */ 244 struct pci_dev *dev; /* lowest numbered PF */
243 struct pci_dev *self; /* this PF */ 245 struct pci_dev *self; /* this PF */
244 struct mutex lock; /* lock for VF bus */ 246 struct mutex lock; /* lock for VF bus */
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 59cf1ba34936..293af5a6f912 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -975,6 +975,7 @@ int pci_setup_device(struct pci_dev *dev)
975 dev->sysdata = dev->bus->sysdata; 975 dev->sysdata = dev->bus->sysdata;
976 dev->dev.parent = dev->bus->bridge; 976 dev->dev.parent = dev->bus->bridge;
977 dev->dev.bus = &pci_bus_type; 977 dev->dev.bus = &pci_bus_type;
978 dev->dev.type = &pci_dev_type;
978 dev->hdr_type = hdr_type & 0x7f; 979 dev->hdr_type = hdr_type & 0x7f;
979 dev->multifunction = !!(hdr_type & 0x80); 980 dev->multifunction = !!(hdr_type & 0x80);
980 dev->error_state = pci_channel_io_normal; 981 dev->error_state = pci_channel_io_normal;