pci_sysfs.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2012
  4. *
  5. * Author(s):
  6. * Jan Glauber <jang@linux.vnet.ibm.com>
  7. */
  8. #define KMSG_COMPONENT "zpci"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/stat.h>
  12. #include <linux/pci.h>
  13. #include "../../../drivers/pci/pci.h"
  14. #include <asm/sclp.h>
  15. #define zpci_attr(name, fmt, member) \
  16. static ssize_t name##_show(struct device *dev, \
  17. struct device_attribute *attr, char *buf) \
  18. { \
  19. struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); \
  20. \
  21. return sprintf(buf, fmt, zdev->member); \
  22. } \
  23. static DEVICE_ATTR_RO(name)
  24. zpci_attr(function_id, "0x%08x\n", fid);
  25. zpci_attr(function_handle, "0x%08x\n", fh);
  26. zpci_attr(pchid, "0x%04x\n", pchid);
  27. zpci_attr(pfgid, "0x%02x\n", pfgid);
  28. zpci_attr(vfn, "0x%04x\n", vfn);
  29. zpci_attr(pft, "0x%02x\n", pft);
  30. zpci_attr(uid, "0x%x\n", uid);
  31. zpci_attr(segment0, "0x%02x\n", pfip[0]);
  32. zpci_attr(segment1, "0x%02x\n", pfip[1]);
  33. zpci_attr(segment2, "0x%02x\n", pfip[2]);
  34. zpci_attr(segment3, "0x%02x\n", pfip[3]);
  35. static ssize_t recover_store(struct device *dev, struct device_attribute *attr,
  36. const char *buf, size_t count)
  37. {
  38. struct kernfs_node *kn;
  39. struct pci_dev *pdev = to_pci_dev(dev);
  40. struct zpci_dev *zdev = to_zpci(pdev);
  41. int ret = 0;
  42. /* Can't use device_remove_self() here as that would lead us to lock
  43. * the pci_rescan_remove_lock while holding the device' kernfs lock.
  44. * This would create a possible deadlock with disable_slot() which is
  45. * not directly protected by the device' kernfs lock but takes it
  46. * during the device removal which happens under
  47. * pci_rescan_remove_lock.
  48. *
  49. * This is analogous to sdev_store_delete() in
  50. * drivers/scsi/scsi_sysfs.c
  51. */
  52. kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
  53. WARN_ON_ONCE(!kn);
  54. /* device_remove_file() serializes concurrent calls ignoring all but
  55. * the first
  56. */
  57. device_remove_file(dev, attr);
  58. /* A concurrent call to recover_store() may slip between
  59. * sysfs_break_active_protection() and the sysfs file removal.
  60. * Once it unblocks from pci_lock_rescan_remove() the original pdev
  61. * will already be removed.
  62. */
  63. pci_lock_rescan_remove();
  64. if (pci_dev_is_added(pdev)) {
  65. pci_stop_and_remove_bus_device(pdev);
  66. ret = zpci_disable_device(zdev);
  67. if (ret)
  68. goto out;
  69. ret = zpci_enable_device(zdev);
  70. if (ret)
  71. goto out;
  72. pci_rescan_bus(zdev->bus);
  73. }
  74. out:
  75. pci_unlock_rescan_remove();
  76. if (kn)
  77. sysfs_unbreak_active_protection(kn);
  78. return ret ? ret : count;
  79. }
  80. static DEVICE_ATTR_WO(recover);
  81. static ssize_t util_string_read(struct file *filp, struct kobject *kobj,
  82. struct bin_attribute *attr, char *buf,
  83. loff_t off, size_t count)
  84. {
  85. struct device *dev = kobj_to_dev(kobj);
  86. struct pci_dev *pdev = to_pci_dev(dev);
  87. struct zpci_dev *zdev = to_zpci(pdev);
  88. return memory_read_from_buffer(buf, count, &off, zdev->util_str,
  89. sizeof(zdev->util_str));
  90. }
  91. static BIN_ATTR_RO(util_string, CLP_UTIL_STR_LEN);
  92. static ssize_t report_error_write(struct file *filp, struct kobject *kobj,
  93. struct bin_attribute *attr, char *buf,
  94. loff_t off, size_t count)
  95. {
  96. struct zpci_report_error_header *report = (void *) buf;
  97. struct device *dev = kobj_to_dev(kobj);
  98. struct pci_dev *pdev = to_pci_dev(dev);
  99. struct zpci_dev *zdev = to_zpci(pdev);
  100. int ret;
  101. if (off || (count < sizeof(*report)))
  102. return -EINVAL;
  103. ret = sclp_pci_report(report, zdev->fh, zdev->fid);
  104. return ret ? ret : count;
  105. }
  106. static BIN_ATTR(report_error, S_IWUSR, NULL, report_error_write, PAGE_SIZE);
  107. static struct bin_attribute *zpci_bin_attrs[] = {
  108. &bin_attr_util_string,
  109. &bin_attr_report_error,
  110. NULL,
  111. };
  112. static struct attribute *zpci_dev_attrs[] = {
  113. &dev_attr_function_id.attr,
  114. &dev_attr_function_handle.attr,
  115. &dev_attr_pchid.attr,
  116. &dev_attr_pfgid.attr,
  117. &dev_attr_pft.attr,
  118. &dev_attr_vfn.attr,
  119. &dev_attr_uid.attr,
  120. &dev_attr_recover.attr,
  121. NULL,
  122. };
  123. static struct attribute_group zpci_attr_group = {
  124. .attrs = zpci_dev_attrs,
  125. .bin_attrs = zpci_bin_attrs,
  126. };
  127. static struct attribute *pfip_attrs[] = {
  128. &dev_attr_segment0.attr,
  129. &dev_attr_segment1.attr,
  130. &dev_attr_segment2.attr,
  131. &dev_attr_segment3.attr,
  132. NULL,
  133. };
  134. static struct attribute_group pfip_attr_group = {
  135. .name = "pfip",
  136. .attrs = pfip_attrs,
  137. };
  138. const struct attribute_group *zpci_attr_groups[] = {
  139. &zpci_attr_group,
  140. &pfip_attr_group,
  141. NULL,
  142. };