pci_sysfs.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright IBM Corp. 2012
  3. *
  4. * Author(s):
  5. * Jan Glauber <jang@linux.vnet.ibm.com>
  6. */
  7. #define COMPONENT "zPCI"
  8. #define pr_fmt(fmt) COMPONENT ": " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/stat.h>
  11. #include <linux/pci.h>
  12. static ssize_t show_fid(struct device *dev, struct device_attribute *attr,
  13. char *buf)
  14. {
  15. struct zpci_dev *zdev = get_zdev(to_pci_dev(dev));
  16. return sprintf(buf, "0x%08x\n", zdev->fid);
  17. }
  18. static DEVICE_ATTR(function_id, S_IRUGO, show_fid, NULL);
  19. static ssize_t show_fh(struct device *dev, struct device_attribute *attr,
  20. char *buf)
  21. {
  22. struct zpci_dev *zdev = get_zdev(to_pci_dev(dev));
  23. return sprintf(buf, "0x%08x\n", zdev->fh);
  24. }
  25. static DEVICE_ATTR(function_handle, S_IRUGO, show_fh, NULL);
  26. static ssize_t show_pchid(struct device *dev, struct device_attribute *attr,
  27. char *buf)
  28. {
  29. struct zpci_dev *zdev = get_zdev(to_pci_dev(dev));
  30. return sprintf(buf, "0x%04x\n", zdev->pchid);
  31. }
  32. static DEVICE_ATTR(pchid, S_IRUGO, show_pchid, NULL);
  33. static ssize_t show_pfgid(struct device *dev, struct device_attribute *attr,
  34. char *buf)
  35. {
  36. struct zpci_dev *zdev = get_zdev(to_pci_dev(dev));
  37. return sprintf(buf, "0x%02x\n", zdev->pfgid);
  38. }
  39. static DEVICE_ATTR(pfgid, S_IRUGO, show_pfgid, NULL);
  40. static void recover_callback(struct device *dev)
  41. {
  42. struct pci_dev *pdev = to_pci_dev(dev);
  43. struct zpci_dev *zdev = get_zdev(pdev);
  44. int ret;
  45. pci_stop_and_remove_bus_device(pdev);
  46. ret = zpci_disable_device(zdev);
  47. if (ret)
  48. return;
  49. ret = zpci_enable_device(zdev);
  50. if (ret)
  51. return;
  52. pci_rescan_bus(zdev->bus);
  53. }
  54. static ssize_t store_recover(struct device *dev, struct device_attribute *attr,
  55. const char *buf, size_t count)
  56. {
  57. int rc = device_schedule_callback(dev, recover_callback);
  58. return rc ? rc : count;
  59. }
  60. static DEVICE_ATTR(recover, S_IWUSR, NULL, store_recover);
  61. static struct device_attribute *zpci_dev_attrs[] = {
  62. &dev_attr_function_id,
  63. &dev_attr_function_handle,
  64. &dev_attr_pchid,
  65. &dev_attr_pfgid,
  66. &dev_attr_recover,
  67. NULL,
  68. };
  69. int zpci_sysfs_add_device(struct device *dev)
  70. {
  71. int i, rc = 0;
  72. for (i = 0; zpci_dev_attrs[i]; i++) {
  73. rc = device_create_file(dev, zpci_dev_attrs[i]);
  74. if (rc)
  75. goto error;
  76. }
  77. return 0;
  78. error:
  79. while (--i >= 0)
  80. device_remove_file(dev, zpci_dev_attrs[i]);
  81. return rc;
  82. }
  83. void zpci_sysfs_remove_device(struct device *dev)
  84. {
  85. int i;
  86. for (i = 0; zpci_dev_attrs[i]; i++)
  87. device_remove_file(dev, zpci_dev_attrs[i]);
  88. }