pci_sysfs.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2012
  4. *
  5. * Author(s):
  6. * Jan Glauber <[email protected]>
  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(port, "%d\n", port);
  31. zpci_attr(uid, "0x%x\n", uid);
  32. zpci_attr(segment0, "0x%02x\n", pfip[0]);
  33. zpci_attr(segment1, "0x%02x\n", pfip[1]);
  34. zpci_attr(segment2, "0x%02x\n", pfip[2]);
  35. zpci_attr(segment3, "0x%02x\n", pfip[3]);
  36. static ssize_t mio_enabled_show(struct device *dev,
  37. struct device_attribute *attr, char *buf)
  38. {
  39. struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
  40. return sprintf(buf, zpci_use_mio(zdev) ? "1\n" : "0\n");
  41. }
  42. static DEVICE_ATTR_RO(mio_enabled);
  43. static ssize_t recover_store(struct device *dev, struct device_attribute *attr,
  44. const char *buf, size_t count)
  45. {
  46. struct kernfs_node *kn;
  47. struct pci_dev *pdev = to_pci_dev(dev);
  48. struct zpci_dev *zdev = to_zpci(pdev);
  49. int ret = 0;
  50. /* Can't use device_remove_self() here as that would lead us to lock
  51. * the pci_rescan_remove_lock while holding the device' kernfs lock.
  52. * This would create a possible deadlock with disable_slot() which is
  53. * not directly protected by the device' kernfs lock but takes it
  54. * during the device removal which happens under
  55. * pci_rescan_remove_lock.
  56. *
  57. * This is analogous to sdev_store_delete() in
  58. * drivers/scsi/scsi_sysfs.c
  59. */
  60. kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
  61. WARN_ON_ONCE(!kn);
  62. /* device_remove_file() serializes concurrent calls ignoring all but
  63. * the first
  64. */
  65. device_remove_file(dev, attr);
  66. /* A concurrent call to recover_store() may slip between
  67. * sysfs_break_active_protection() and the sysfs file removal.
  68. * Once it unblocks from pci_lock_rescan_remove() the original pdev
  69. * will already be removed.
  70. */
  71. pci_lock_rescan_remove();
  72. if (pci_dev_is_added(pdev)) {
  73. pci_stop_and_remove_bus_device(pdev);
  74. if (zdev->dma_table) {
  75. ret = zpci_dma_exit_device(zdev);
  76. if (ret)
  77. goto out;
  78. }
  79. if (zdev_enabled(zdev)) {
  80. ret = zpci_disable_device(zdev);
  81. /*
  82. * Due to a z/VM vs LPAR inconsistency in the error
  83. * state the FH may indicate an enabled device but
  84. * disable says the device is already disabled don't
  85. * treat it as an error here.
  86. */
  87. if (ret == -EINVAL)
  88. ret = 0;
  89. if (ret)
  90. goto out;
  91. }
  92. ret = zpci_enable_device(zdev);
  93. if (ret)
  94. goto out;
  95. ret = zpci_dma_init_device(zdev);
  96. if (ret) {
  97. zpci_disable_device(zdev);
  98. goto out;
  99. }
  100. pci_rescan_bus(zdev->zbus->bus);
  101. }
  102. out:
  103. pci_unlock_rescan_remove();
  104. if (kn)
  105. sysfs_unbreak_active_protection(kn);
  106. return ret ? ret : count;
  107. }
  108. static DEVICE_ATTR_WO(recover);
  109. static ssize_t util_string_read(struct file *filp, struct kobject *kobj,
  110. struct bin_attribute *attr, char *buf,
  111. loff_t off, size_t count)
  112. {
  113. struct device *dev = kobj_to_dev(kobj);
  114. struct pci_dev *pdev = to_pci_dev(dev);
  115. struct zpci_dev *zdev = to_zpci(pdev);
  116. return memory_read_from_buffer(buf, count, &off, zdev->util_str,
  117. sizeof(zdev->util_str));
  118. }
  119. static BIN_ATTR_RO(util_string, CLP_UTIL_STR_LEN);
  120. static ssize_t report_error_write(struct file *filp, struct kobject *kobj,
  121. struct bin_attribute *attr, char *buf,
  122. loff_t off, size_t count)
  123. {
  124. struct zpci_report_error_header *report = (void *) buf;
  125. struct device *dev = kobj_to_dev(kobj);
  126. struct pci_dev *pdev = to_pci_dev(dev);
  127. struct zpci_dev *zdev = to_zpci(pdev);
  128. int ret;
  129. if (off || (count < sizeof(*report)))
  130. return -EINVAL;
  131. ret = sclp_pci_report(report, zdev->fh, zdev->fid);
  132. return ret ? ret : count;
  133. }
  134. static BIN_ATTR(report_error, S_IWUSR, NULL, report_error_write, PAGE_SIZE);
  135. static ssize_t uid_is_unique_show(struct device *dev,
  136. struct device_attribute *attr, char *buf)
  137. {
  138. return sysfs_emit(buf, "%d\n", zpci_unique_uid ? 1 : 0);
  139. }
  140. static DEVICE_ATTR_RO(uid_is_unique);
  141. #ifndef CONFIG_DMI
  142. /* analogous to smbios index */
  143. static ssize_t index_show(struct device *dev,
  144. struct device_attribute *attr, char *buf)
  145. {
  146. struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
  147. u32 index = ~0;
  148. if (zpci_unique_uid)
  149. index = zdev->uid;
  150. return sysfs_emit(buf, "%u\n", index);
  151. }
  152. static DEVICE_ATTR_RO(index);
  153. static umode_t zpci_index_is_visible(struct kobject *kobj,
  154. struct attribute *attr, int n)
  155. {
  156. return zpci_unique_uid ? attr->mode : 0;
  157. }
  158. static struct attribute *zpci_ident_attrs[] = {
  159. &dev_attr_index.attr,
  160. NULL,
  161. };
  162. static struct attribute_group zpci_ident_attr_group = {
  163. .attrs = zpci_ident_attrs,
  164. .is_visible = zpci_index_is_visible,
  165. };
  166. #endif
  167. static struct bin_attribute *zpci_bin_attrs[] = {
  168. &bin_attr_util_string,
  169. &bin_attr_report_error,
  170. NULL,
  171. };
  172. static struct attribute *zpci_dev_attrs[] = {
  173. &dev_attr_function_id.attr,
  174. &dev_attr_function_handle.attr,
  175. &dev_attr_pchid.attr,
  176. &dev_attr_pfgid.attr,
  177. &dev_attr_pft.attr,
  178. &dev_attr_port.attr,
  179. &dev_attr_vfn.attr,
  180. &dev_attr_uid.attr,
  181. &dev_attr_recover.attr,
  182. &dev_attr_mio_enabled.attr,
  183. &dev_attr_uid_is_unique.attr,
  184. NULL,
  185. };
  186. static struct attribute_group zpci_attr_group = {
  187. .attrs = zpci_dev_attrs,
  188. .bin_attrs = zpci_bin_attrs,
  189. };
  190. static struct attribute *pfip_attrs[] = {
  191. &dev_attr_segment0.attr,
  192. &dev_attr_segment1.attr,
  193. &dev_attr_segment2.attr,
  194. &dev_attr_segment3.attr,
  195. NULL,
  196. };
  197. static struct attribute_group pfip_attr_group = {
  198. .name = "pfip",
  199. .attrs = pfip_attrs,
  200. };
  201. const struct attribute_group *zpci_attr_groups[] = {
  202. &zpci_attr_group,
  203. &pfip_attr_group,
  204. #ifndef CONFIG_DMI
  205. &zpci_ident_attr_group,
  206. #endif
  207. NULL,
  208. };