edac_pci_sysfs.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /* edac_mc kernel module
  2. * (C) 2005, 2006 Linux Networx (http://lnxi.com)
  3. * This file may be distributed under the terms of the
  4. * GNU General Public License.
  5. *
  6. * Written Doug Thompson <norsk5@xmission.com>
  7. *
  8. */
  9. #include <linux/module.h>
  10. #include <linux/sysdev.h>
  11. #include <linux/ctype.h>
  12. #include "edac_mc.h"
  13. #include "edac_module.h"
  14. #ifdef CONFIG_PCI
  15. static int check_pci_parity = 0; /* default YES check PCI parity */
  16. static int panic_on_pci_parity; /* default no panic on PCI Parity */
  17. static atomic_t pci_parity_count = ATOMIC_INIT(0);
  18. static struct kobject edac_pci_kobj; /* /sys/devices/system/edac/pci */
  19. static struct completion edac_pci_kobj_complete;
  20. static ssize_t edac_pci_int_show(void *ptr, char *buffer)
  21. {
  22. int *value = ptr;
  23. return sprintf(buffer,"%d\n",*value);
  24. }
  25. static ssize_t edac_pci_int_store(void *ptr, const char *buffer, size_t count)
  26. {
  27. int *value = ptr;
  28. if (isdigit(*buffer))
  29. *value = simple_strtoul(buffer,NULL,0);
  30. return count;
  31. }
  32. struct edac_pci_dev_attribute {
  33. struct attribute attr;
  34. void *value;
  35. ssize_t (*show)(void *,char *);
  36. ssize_t (*store)(void *, const char *,size_t);
  37. };
  38. /* Set of show/store abstract level functions for PCI Parity object */
  39. static ssize_t edac_pci_dev_show(struct kobject *kobj, struct attribute *attr,
  40. char *buffer)
  41. {
  42. struct edac_pci_dev_attribute *edac_pci_dev;
  43. edac_pci_dev= (struct edac_pci_dev_attribute*)attr;
  44. if (edac_pci_dev->show)
  45. return edac_pci_dev->show(edac_pci_dev->value, buffer);
  46. return -EIO;
  47. }
  48. static ssize_t edac_pci_dev_store(struct kobject *kobj,
  49. struct attribute *attr, const char *buffer, size_t count)
  50. {
  51. struct edac_pci_dev_attribute *edac_pci_dev;
  52. edac_pci_dev= (struct edac_pci_dev_attribute*)attr;
  53. if (edac_pci_dev->show)
  54. return edac_pci_dev->store(edac_pci_dev->value, buffer, count);
  55. return -EIO;
  56. }
  57. static struct sysfs_ops edac_pci_sysfs_ops = {
  58. .show = edac_pci_dev_show,
  59. .store = edac_pci_dev_store
  60. };
  61. #define EDAC_PCI_ATTR(_name,_mode,_show,_store) \
  62. static struct edac_pci_dev_attribute edac_pci_attr_##_name = { \
  63. .attr = {.name = __stringify(_name), .mode = _mode }, \
  64. .value = &_name, \
  65. .show = _show, \
  66. .store = _store, \
  67. };
  68. #define EDAC_PCI_STRING_ATTR(_name,_data,_mode,_show,_store) \
  69. static struct edac_pci_dev_attribute edac_pci_attr_##_name = { \
  70. .attr = {.name = __stringify(_name), .mode = _mode }, \
  71. .value = _data, \
  72. .show = _show, \
  73. .store = _store, \
  74. };
  75. /* PCI Parity control files */
  76. EDAC_PCI_ATTR(check_pci_parity, S_IRUGO|S_IWUSR, edac_pci_int_show,
  77. edac_pci_int_store);
  78. EDAC_PCI_ATTR(panic_on_pci_parity, S_IRUGO|S_IWUSR, edac_pci_int_show,
  79. edac_pci_int_store);
  80. EDAC_PCI_ATTR(pci_parity_count, S_IRUGO, edac_pci_int_show, NULL);
  81. /* Base Attributes of the memory ECC object */
  82. static struct edac_pci_dev_attribute *edac_pci_attr[] = {
  83. &edac_pci_attr_check_pci_parity,
  84. &edac_pci_attr_panic_on_pci_parity,
  85. &edac_pci_attr_pci_parity_count,
  86. NULL,
  87. };
  88. /* No memory to release */
  89. static void edac_pci_release(struct kobject *kobj)
  90. {
  91. debugf1("%s()\n", __func__);
  92. complete(&edac_pci_kobj_complete);
  93. }
  94. static struct kobj_type ktype_edac_pci = {
  95. .release = edac_pci_release,
  96. .sysfs_ops = &edac_pci_sysfs_ops,
  97. .default_attrs = (struct attribute **) edac_pci_attr,
  98. };
  99. /**
  100. * edac_sysfs_pci_setup()
  101. *
  102. * setup the sysfs for EDAC PCI attributes
  103. * assumes edac_class has already been initialized
  104. */
  105. int edac_sysfs_pci_setup(void)
  106. {
  107. int err;
  108. struct sysdev_class *edac_class;
  109. debugf1("%s()\n", __func__);
  110. edac_class = edac_get_edac_class();
  111. memset(&edac_pci_kobj, 0, sizeof(edac_pci_kobj));
  112. edac_pci_kobj.parent = &edac_class->kset.kobj;
  113. edac_pci_kobj.ktype = &ktype_edac_pci;
  114. err = kobject_set_name(&edac_pci_kobj, "pci");
  115. if (!err) {
  116. /* Instanstiate the pci object */
  117. /* FIXME: maybe new sysdev_create_subdir() */
  118. err = kobject_register(&edac_pci_kobj);
  119. if (err)
  120. debugf1("Failed to register '.../edac/pci'\n");
  121. else
  122. debugf1("Registered '.../edac/pci' kobject\n");
  123. }
  124. return err;
  125. }
  126. /*
  127. * edac_sysfs_pci_teardown
  128. *
  129. * perform the sysfs teardown for the PCI attributes
  130. */
  131. void edac_sysfs_pci_teardown(void)
  132. {
  133. debugf0("%s()\n", __func__);
  134. init_completion(&edac_pci_kobj_complete);
  135. kobject_unregister(&edac_pci_kobj);
  136. wait_for_completion(&edac_pci_kobj_complete);
  137. }
  138. static u16 get_pci_parity_status(struct pci_dev *dev, int secondary)
  139. {
  140. int where;
  141. u16 status;
  142. where = secondary ? PCI_SEC_STATUS : PCI_STATUS;
  143. pci_read_config_word(dev, where, &status);
  144. /* If we get back 0xFFFF then we must suspect that the card has been
  145. * pulled but the Linux PCI layer has not yet finished cleaning up.
  146. * We don't want to report on such devices
  147. */
  148. if (status == 0xFFFF) {
  149. u32 sanity;
  150. pci_read_config_dword(dev, 0, &sanity);
  151. if (sanity == 0xFFFFFFFF)
  152. return 0;
  153. }
  154. status &= PCI_STATUS_DETECTED_PARITY | PCI_STATUS_SIG_SYSTEM_ERROR |
  155. PCI_STATUS_PARITY;
  156. if (status)
  157. /* reset only the bits we are interested in */
  158. pci_write_config_word(dev, where, status);
  159. return status;
  160. }
  161. typedef void (*pci_parity_check_fn_t) (struct pci_dev *dev);
  162. /* Clear any PCI parity errors logged by this device. */
  163. static void edac_pci_dev_parity_clear(struct pci_dev *dev)
  164. {
  165. u8 header_type;
  166. get_pci_parity_status(dev, 0);
  167. /* read the device TYPE, looking for bridges */
  168. pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
  169. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE)
  170. get_pci_parity_status(dev, 1);
  171. }
  172. /*
  173. * PCI Parity polling
  174. *
  175. */
  176. static void edac_pci_dev_parity_test(struct pci_dev *dev)
  177. {
  178. u16 status;
  179. u8 header_type;
  180. /* read the STATUS register on this device
  181. */
  182. status = get_pci_parity_status(dev, 0);
  183. debugf2("PCI STATUS= 0x%04x %s\n", status, dev->dev.bus_id );
  184. /* check the status reg for errors */
  185. if (status) {
  186. if (status & (PCI_STATUS_SIG_SYSTEM_ERROR))
  187. edac_printk(KERN_CRIT, EDAC_PCI,
  188. "Signaled System Error on %s\n",
  189. pci_name(dev));
  190. if (status & (PCI_STATUS_PARITY)) {
  191. edac_printk(KERN_CRIT, EDAC_PCI,
  192. "Master Data Parity Error on %s\n",
  193. pci_name(dev));
  194. atomic_inc(&pci_parity_count);
  195. }
  196. if (status & (PCI_STATUS_DETECTED_PARITY)) {
  197. edac_printk(KERN_CRIT, EDAC_PCI,
  198. "Detected Parity Error on %s\n",
  199. pci_name(dev));
  200. atomic_inc(&pci_parity_count);
  201. }
  202. }
  203. /* read the device TYPE, looking for bridges */
  204. pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
  205. debugf2("PCI HEADER TYPE= 0x%02x %s\n", header_type, dev->dev.bus_id );
  206. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
  207. /* On bridges, need to examine secondary status register */
  208. status = get_pci_parity_status(dev, 1);
  209. debugf2("PCI SEC_STATUS= 0x%04x %s\n",
  210. status, dev->dev.bus_id );
  211. /* check the secondary status reg for errors */
  212. if (status) {
  213. if (status & (PCI_STATUS_SIG_SYSTEM_ERROR))
  214. edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
  215. "Signaled System Error on %s\n",
  216. pci_name(dev));
  217. if (status & (PCI_STATUS_PARITY)) {
  218. edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
  219. "Master Data Parity Error on "
  220. "%s\n", pci_name(dev));
  221. atomic_inc(&pci_parity_count);
  222. }
  223. if (status & (PCI_STATUS_DETECTED_PARITY)) {
  224. edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
  225. "Detected Parity Error on %s\n",
  226. pci_name(dev));
  227. atomic_inc(&pci_parity_count);
  228. }
  229. }
  230. }
  231. }
  232. /*
  233. * pci_dev parity list iterator
  234. * Scan the PCI device list for one iteration, looking for SERRORs
  235. * Master Parity ERRORS or Parity ERRORs on primary or secondary devices
  236. */
  237. static inline void edac_pci_dev_parity_iterator(pci_parity_check_fn_t fn)
  238. {
  239. struct pci_dev *dev = NULL;
  240. /* request for kernel access to the next PCI device, if any,
  241. * and while we are looking at it have its reference count
  242. * bumped until we are done with it
  243. */
  244. while((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  245. fn(dev);
  246. }
  247. }
  248. /*
  249. * edac_pci_do_parity_check
  250. *
  251. * performs the actual PCI parity check operation
  252. */
  253. void edac_pci_do_parity_check(void)
  254. {
  255. unsigned long flags;
  256. int before_count;
  257. debugf3("%s()\n", __func__);
  258. if (!check_pci_parity)
  259. return;
  260. before_count = atomic_read(&pci_parity_count);
  261. /* scan all PCI devices looking for a Parity Error on devices and
  262. * bridges
  263. */
  264. local_irq_save(flags);
  265. edac_pci_dev_parity_iterator(edac_pci_dev_parity_test);
  266. local_irq_restore(flags);
  267. /* Only if operator has selected panic on PCI Error */
  268. if (panic_on_pci_parity) {
  269. /* If the count is different 'after' from 'before' */
  270. if (before_count != atomic_read(&pci_parity_count))
  271. panic("EDAC: PCI Parity Error");
  272. }
  273. }
  274. void edac_pci_clear_parity_errors(void)
  275. {
  276. /* Clear any PCI bus parity errors that devices initially have logged
  277. * in their registers.
  278. */
  279. edac_pci_dev_parity_iterator(edac_pci_dev_parity_clear);
  280. }
  281. /*
  282. * Define the PCI parameter to the module
  283. */
  284. module_param(check_pci_parity, int, 0644);
  285. MODULE_PARM_DESC(check_pci_parity, "Check for PCI bus parity errors: 0=off 1=on");
  286. module_param(panic_on_pci_parity, int, 0644);
  287. MODULE_PARM_DESC(panic_on_pci_parity, "Panic on PCI Bus Parity error: 0=off 1=on");
  288. #endif /* CONFIG_PCI */