eeh_sysfs.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Sysfs entries for PCI Error Recovery for PAPR-compliant platform.
  4. * Copyright IBM Corporation 2007
  5. * Copyright Linas Vepstas <[email protected]> 2007
  6. *
  7. * Send comments and feedback to Linas Vepstas <[email protected]>
  8. */
  9. #include <linux/of.h>
  10. #include <linux/pci.h>
  11. #include <linux/stat.h>
  12. #include <asm/ppc-pci.h>
  13. #include <asm/pci-bridge.h>
  14. /**
  15. * EEH_SHOW_ATTR -- Create sysfs entry for eeh statistic
  16. * @_name: name of file in sysfs directory
  17. * @_memb: name of member in struct eeh_dev to access
  18. * @_format: printf format for display
  19. *
  20. * All of the attributes look very similar, so just
  21. * auto-gen a cut-n-paste routine to display them.
  22. */
  23. #define EEH_SHOW_ATTR(_name,_memb,_format) \
  24. static ssize_t eeh_show_##_name(struct device *dev, \
  25. struct device_attribute *attr, char *buf) \
  26. { \
  27. struct pci_dev *pdev = to_pci_dev(dev); \
  28. struct eeh_dev *edev = pci_dev_to_eeh_dev(pdev); \
  29. \
  30. if (!edev) \
  31. return 0; \
  32. \
  33. return sprintf(buf, _format "\n", edev->_memb); \
  34. } \
  35. static DEVICE_ATTR(_name, 0444, eeh_show_##_name, NULL);
  36. EEH_SHOW_ATTR(eeh_mode, mode, "0x%x");
  37. EEH_SHOW_ATTR(eeh_pe_config_addr, pe_config_addr, "0x%x");
  38. static ssize_t eeh_pe_state_show(struct device *dev,
  39. struct device_attribute *attr, char *buf)
  40. {
  41. struct pci_dev *pdev = to_pci_dev(dev);
  42. struct eeh_dev *edev = pci_dev_to_eeh_dev(pdev);
  43. int state;
  44. if (!edev || !edev->pe)
  45. return -ENODEV;
  46. state = eeh_ops->get_state(edev->pe, NULL);
  47. return sprintf(buf, "0x%08x 0x%08x\n",
  48. state, edev->pe->state);
  49. }
  50. static ssize_t eeh_pe_state_store(struct device *dev,
  51. struct device_attribute *attr,
  52. const char *buf, size_t count)
  53. {
  54. struct pci_dev *pdev = to_pci_dev(dev);
  55. struct eeh_dev *edev = pci_dev_to_eeh_dev(pdev);
  56. if (!edev || !edev->pe)
  57. return -ENODEV;
  58. /* Nothing to do if it's not frozen */
  59. if (!(edev->pe->state & EEH_PE_ISOLATED))
  60. return count;
  61. if (eeh_unfreeze_pe(edev->pe))
  62. return -EIO;
  63. eeh_pe_state_clear(edev->pe, EEH_PE_ISOLATED, true);
  64. return count;
  65. }
  66. static DEVICE_ATTR_RW(eeh_pe_state);
  67. #if defined(CONFIG_PCI_IOV) && defined(CONFIG_PPC_PSERIES)
  68. static ssize_t eeh_notify_resume_show(struct device *dev,
  69. struct device_attribute *attr, char *buf)
  70. {
  71. struct pci_dev *pdev = to_pci_dev(dev);
  72. struct eeh_dev *edev = pci_dev_to_eeh_dev(pdev);
  73. struct pci_dn *pdn = pci_get_pdn(pdev);
  74. if (!edev || !edev->pe)
  75. return -ENODEV;
  76. return sprintf(buf, "%d\n", pdn->last_allow_rc);
  77. }
  78. static ssize_t eeh_notify_resume_store(struct device *dev,
  79. struct device_attribute *attr,
  80. const char *buf, size_t count)
  81. {
  82. struct pci_dev *pdev = to_pci_dev(dev);
  83. struct eeh_dev *edev = pci_dev_to_eeh_dev(pdev);
  84. if (!edev || !edev->pe || !eeh_ops->notify_resume)
  85. return -ENODEV;
  86. if (eeh_ops->notify_resume(edev))
  87. return -EIO;
  88. return count;
  89. }
  90. static DEVICE_ATTR_RW(eeh_notify_resume);
  91. static int eeh_notify_resume_add(struct pci_dev *pdev)
  92. {
  93. struct device_node *np;
  94. int rc = 0;
  95. np = pci_device_to_OF_node(pdev->is_physfn ? pdev : pdev->physfn);
  96. if (of_property_read_bool(np, "ibm,is-open-sriov-pf"))
  97. rc = device_create_file(&pdev->dev, &dev_attr_eeh_notify_resume);
  98. return rc;
  99. }
  100. static void eeh_notify_resume_remove(struct pci_dev *pdev)
  101. {
  102. struct device_node *np;
  103. np = pci_device_to_OF_node(pdev->is_physfn ? pdev : pdev->physfn);
  104. if (of_property_read_bool(np, "ibm,is-open-sriov-pf"))
  105. device_remove_file(&pdev->dev, &dev_attr_eeh_notify_resume);
  106. }
  107. #else
  108. static inline int eeh_notify_resume_add(struct pci_dev *pdev) { return 0; }
  109. static inline void eeh_notify_resume_remove(struct pci_dev *pdev) { }
  110. #endif /* CONFIG_PCI_IOV && CONFIG PPC_PSERIES*/
  111. void eeh_sysfs_add_device(struct pci_dev *pdev)
  112. {
  113. struct eeh_dev *edev = pci_dev_to_eeh_dev(pdev);
  114. int rc=0;
  115. if (!eeh_enabled())
  116. return;
  117. if (edev && (edev->mode & EEH_DEV_SYSFS))
  118. return;
  119. rc += device_create_file(&pdev->dev, &dev_attr_eeh_mode);
  120. rc += device_create_file(&pdev->dev, &dev_attr_eeh_pe_config_addr);
  121. rc += device_create_file(&pdev->dev, &dev_attr_eeh_pe_state);
  122. rc += eeh_notify_resume_add(pdev);
  123. if (rc)
  124. pr_warn("EEH: Unable to create sysfs entries\n");
  125. else if (edev)
  126. edev->mode |= EEH_DEV_SYSFS;
  127. }
  128. void eeh_sysfs_remove_device(struct pci_dev *pdev)
  129. {
  130. struct eeh_dev *edev = pci_dev_to_eeh_dev(pdev);
  131. if (!edev) {
  132. WARN_ON(eeh_enabled());
  133. return;
  134. }
  135. edev->mode &= ~EEH_DEV_SYSFS;
  136. /*
  137. * The parent directory might have been removed. We needn't
  138. * continue for that case.
  139. */
  140. if (!pdev->dev.kobj.sd)
  141. return;
  142. device_remove_file(&pdev->dev, &dev_attr_eeh_mode);
  143. device_remove_file(&pdev->dev, &dev_attr_eeh_pe_config_addr);
  144. device_remove_file(&pdev->dev, &dev_attr_eeh_pe_state);
  145. eeh_notify_resume_remove(pdev);
  146. }