pci_sysfs.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 pr_fmt(fmt) "zpci: " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/stat.h>
  11. #include <linux/pci.h>
  12. #include "../../../drivers/pci/pci.h"
  13. #include <asm/sclp.h>
  14. #define zpci_attr(name, fmt, member) \
  15. static ssize_t name##_show(struct device *dev, \
  16. struct device_attribute *attr, char *buf) \
  17. { \
  18. struct zpci_dev *zdev = to_zpci(to_pci_dev(dev)); \
  19. \
  20. return sysfs_emit(buf, fmt, zdev->member); \
  21. } \
  22. static DEVICE_ATTR_RO(name)
  23. zpci_attr(function_id, "0x%08x\n", fid);
  24. zpci_attr(function_handle, "0x%08x\n", fh);
  25. zpci_attr(pchid, "0x%04x\n", pchid);
  26. zpci_attr(pfgid, "0x%02x\n", pfgid);
  27. zpci_attr(vfn, "0x%04x\n", vfn);
  28. zpci_attr(pft, "0x%02x\n", pft);
  29. zpci_attr(port, "%d\n", port);
  30. zpci_attr(fidparm, "0x%02x\n", fidparm);
  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. #define ZPCI_FW_ATTR_RO(_name) \
  37. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  38. static ssize_t mio_enabled_show(struct device *dev,
  39. struct device_attribute *attr, char *buf)
  40. {
  41. struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
  42. return sysfs_emit(buf, zpci_use_mio(zdev) ? "1\n" : "0\n");
  43. }
  44. static DEVICE_ATTR_RO(mio_enabled);
  45. static int _do_recover(struct pci_dev *pdev, struct zpci_dev *zdev)
  46. {
  47. int ret;
  48. pci_stop_and_remove_bus_device(pdev);
  49. if (zdev_enabled(zdev)) {
  50. ret = zpci_disable_device(zdev);
  51. /*
  52. * Due to a z/VM vs LPAR inconsistency in the error
  53. * state the FH may indicate an enabled device but
  54. * disable says the device is already disabled don't
  55. * treat it as an error here.
  56. */
  57. if (ret == -EINVAL)
  58. ret = 0;
  59. if (ret)
  60. return ret;
  61. }
  62. ret = zpci_reenable_device(zdev);
  63. return ret;
  64. }
  65. static ssize_t recover_store(struct device *dev, struct device_attribute *attr,
  66. const char *buf, size_t count)
  67. {
  68. struct kernfs_node *kn;
  69. struct pci_dev *pdev = to_pci_dev(dev);
  70. struct zpci_dev *zdev = to_zpci(pdev);
  71. int ret = 0;
  72. /* Can't use device_remove_self() here as that would lead us to lock
  73. * the pci_rescan_remove_lock while holding the device' kernfs lock.
  74. * This would create a possible deadlock with disable_slot() which is
  75. * not directly protected by the device' kernfs lock but takes it
  76. * during the device removal which happens under
  77. * pci_rescan_remove_lock.
  78. *
  79. * This is analogous to sdev_store_delete() in
  80. * drivers/scsi/scsi_sysfs.c
  81. */
  82. kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
  83. WARN_ON_ONCE(!kn);
  84. /* Device needs to be configured and state must not change */
  85. mutex_lock(&zdev->state_lock);
  86. if (zdev->state != ZPCI_FN_STATE_CONFIGURED)
  87. goto out;
  88. /* device_remove_file() serializes concurrent calls ignoring all but
  89. * the first
  90. */
  91. device_remove_file(dev, attr);
  92. /* A concurrent call to recover_store() may slip between
  93. * sysfs_break_active_protection() and the sysfs file removal.
  94. * Once it unblocks from pci_lock_rescan_remove() the original pdev
  95. * will already be removed.
  96. */
  97. pci_lock_rescan_remove();
  98. if (pci_dev_is_added(pdev)) {
  99. ret = _do_recover(pdev, zdev);
  100. }
  101. pci_rescan_bus(zdev->zbus->bus);
  102. pci_unlock_rescan_remove();
  103. out:
  104. mutex_unlock(&zdev->state_lock);
  105. if (kn)
  106. sysfs_unbreak_active_protection(kn);
  107. return ret ? ret : count;
  108. }
  109. static DEVICE_ATTR_WO(recover);
  110. static ssize_t util_string_read(struct file *filp, struct kobject *kobj,
  111. const struct bin_attribute *attr, char *buf,
  112. loff_t off, size_t count)
  113. {
  114. struct device *dev = kobj_to_dev(kobj);
  115. struct pci_dev *pdev = to_pci_dev(dev);
  116. struct zpci_dev *zdev = to_zpci(pdev);
  117. return memory_read_from_buffer(buf, count, &off, zdev->util_str,
  118. sizeof(zdev->util_str));
  119. }
  120. static const BIN_ATTR_RO(util_string, CLP_UTIL_STR_LEN);
  121. static ssize_t report_error_write(struct file *filp, struct kobject *kobj,
  122. const struct bin_attribute *attr, char *buf,
  123. loff_t off, size_t count)
  124. {
  125. struct zpci_report_error_header *report = (void *) buf;
  126. struct device *dev = kobj_to_dev(kobj);
  127. struct pci_dev *pdev = to_pci_dev(dev);
  128. struct zpci_dev *zdev = to_zpci(pdev);
  129. int ret;
  130. if (off || (count < sizeof(*report)))
  131. return -EINVAL;
  132. ret = sclp_pci_report(report, zdev->fh, zdev->fid);
  133. return ret ? ret : count;
  134. }
  135. static const BIN_ATTR(report_error, S_IWUSR, NULL, report_error_write, PAGE_SIZE);
  136. static ssize_t uid_is_unique_show(struct device *dev,
  137. struct device_attribute *attr, char *buf)
  138. {
  139. return sysfs_emit(buf, "%d\n", zpci_unique_uid ? 1 : 0);
  140. }
  141. static DEVICE_ATTR_RO(uid_is_unique);
  142. static ssize_t uid_checking_show(struct kobject *kobj,
  143. struct kobj_attribute *attr, char *buf)
  144. {
  145. return sysfs_emit(buf, "%d\n", zpci_unique_uid ? 1 : 0);
  146. }
  147. ZPCI_FW_ATTR_RO(uid_checking);
  148. /* analogous to smbios index */
  149. static ssize_t index_show(struct device *dev,
  150. struct device_attribute *attr, char *buf)
  151. {
  152. struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
  153. u32 index = ~0;
  154. if (zpci_unique_uid)
  155. index = zdev->uid;
  156. return sysfs_emit(buf, "%u\n", index);
  157. }
  158. static DEVICE_ATTR_RO(index);
  159. static umode_t zpci_index_is_visible(struct kobject *kobj,
  160. struct attribute *attr, int n)
  161. {
  162. return zpci_unique_uid ? attr->mode : 0;
  163. }
  164. static struct attribute *zpci_ident_attrs[] = {
  165. &dev_attr_index.attr,
  166. NULL,
  167. };
  168. const struct attribute_group zpci_ident_attr_group = {
  169. .attrs = zpci_ident_attrs,
  170. .is_visible = zpci_index_is_visible,
  171. };
  172. static const struct bin_attribute *const zpci_bin_attrs[] = {
  173. &bin_attr_util_string,
  174. &bin_attr_report_error,
  175. NULL,
  176. };
  177. static struct attribute *zpci_dev_attrs[] = {
  178. &dev_attr_function_id.attr,
  179. &dev_attr_function_handle.attr,
  180. &dev_attr_pchid.attr,
  181. &dev_attr_pfgid.attr,
  182. &dev_attr_pft.attr,
  183. &dev_attr_port.attr,
  184. &dev_attr_fidparm.attr,
  185. &dev_attr_vfn.attr,
  186. &dev_attr_uid.attr,
  187. &dev_attr_recover.attr,
  188. &dev_attr_mio_enabled.attr,
  189. &dev_attr_uid_is_unique.attr,
  190. NULL,
  191. };
  192. const struct attribute_group zpci_attr_group = {
  193. .attrs = zpci_dev_attrs,
  194. .bin_attrs = zpci_bin_attrs,
  195. };
  196. static struct attribute *pfip_attrs[] = {
  197. &dev_attr_segment0.attr,
  198. &dev_attr_segment1.attr,
  199. &dev_attr_segment2.attr,
  200. &dev_attr_segment3.attr,
  201. NULL,
  202. };
  203. const struct attribute_group pfip_attr_group = {
  204. .name = "pfip",
  205. .attrs = pfip_attrs,
  206. };
  207. static struct attribute *clp_fw_attrs[] = {
  208. &uid_checking_attr.attr,
  209. NULL,
  210. };
  211. static struct attribute_group clp_fw_attr_group = {
  212. .name = "clp",
  213. .attrs = clp_fw_attrs,
  214. };
  215. int __init __zpci_fw_sysfs_init(void)
  216. {
  217. return sysfs_create_group(firmware_kobj, &clp_fw_attr_group);
  218. }