eeh_sysfs.c 5.0 KB

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