edac_pci_sysfs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  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/edac.h>
  11. #include <linux/slab.h>
  12. #include <linux/ctype.h>
  13. #include "edac_core.h"
  14. #include "edac_module.h"
  15. /* Turn off this whole feature if PCI is not configured */
  16. #ifdef CONFIG_PCI
  17. #define EDAC_PCI_SYMLINK "device"
  18. /* data variables exported via sysfs */
  19. static int check_pci_errors; /* default NO check PCI parity */
  20. static int edac_pci_panic_on_pe; /* default NO panic on PCI Parity */
  21. static int edac_pci_log_pe = 1; /* log PCI parity errors */
  22. static int edac_pci_log_npe = 1; /* log PCI non-parity error errors */
  23. static int edac_pci_poll_msec = 1000; /* one second workq period */
  24. static atomic_t pci_parity_count = ATOMIC_INIT(0);
  25. static atomic_t pci_nonparity_count = ATOMIC_INIT(0);
  26. static struct kobject *edac_pci_top_main_kobj;
  27. static atomic_t edac_pci_sysfs_refcount = ATOMIC_INIT(0);
  28. /* getter functions for the data variables */
  29. int edac_pci_get_check_errors(void)
  30. {
  31. return check_pci_errors;
  32. }
  33. static int edac_pci_get_log_pe(void)
  34. {
  35. return edac_pci_log_pe;
  36. }
  37. static int edac_pci_get_log_npe(void)
  38. {
  39. return edac_pci_log_npe;
  40. }
  41. static int edac_pci_get_panic_on_pe(void)
  42. {
  43. return edac_pci_panic_on_pe;
  44. }
  45. int edac_pci_get_poll_msec(void)
  46. {
  47. return edac_pci_poll_msec;
  48. }
  49. /**************************** EDAC PCI sysfs instance *******************/
  50. static ssize_t instance_pe_count_show(struct edac_pci_ctl_info *pci, char *data)
  51. {
  52. return sprintf(data, "%u\n", atomic_read(&pci->counters.pe_count));
  53. }
  54. static ssize_t instance_npe_count_show(struct edac_pci_ctl_info *pci,
  55. char *data)
  56. {
  57. return sprintf(data, "%u\n", atomic_read(&pci->counters.npe_count));
  58. }
  59. #define to_instance(k) container_of(k, struct edac_pci_ctl_info, kobj)
  60. #define to_instance_attr(a) container_of(a, struct instance_attribute, attr)
  61. /* DEVICE instance kobject release() function */
  62. static void edac_pci_instance_release(struct kobject *kobj)
  63. {
  64. struct edac_pci_ctl_info *pci;
  65. debugf0("%s()\n", __func__);
  66. /* Form pointer to containing struct, the pci control struct */
  67. pci = to_instance(kobj);
  68. /* decrement reference count on top main kobj */
  69. kobject_put(edac_pci_top_main_kobj);
  70. kfree(pci); /* Free the control struct */
  71. }
  72. /* instance specific attribute structure */
  73. struct instance_attribute {
  74. struct attribute attr;
  75. ssize_t(*show) (struct edac_pci_ctl_info *, char *);
  76. ssize_t(*store) (struct edac_pci_ctl_info *, const char *, size_t);
  77. };
  78. /* Function to 'show' fields from the edac_pci 'instance' structure */
  79. static ssize_t edac_pci_instance_show(struct kobject *kobj,
  80. struct attribute *attr, char *buffer)
  81. {
  82. struct edac_pci_ctl_info *pci = to_instance(kobj);
  83. struct instance_attribute *instance_attr = to_instance_attr(attr);
  84. if (instance_attr->show)
  85. return instance_attr->show(pci, buffer);
  86. return -EIO;
  87. }
  88. /* Function to 'store' fields into the edac_pci 'instance' structure */
  89. static ssize_t edac_pci_instance_store(struct kobject *kobj,
  90. struct attribute *attr,
  91. const char *buffer, size_t count)
  92. {
  93. struct edac_pci_ctl_info *pci = to_instance(kobj);
  94. struct instance_attribute *instance_attr = to_instance_attr(attr);
  95. if (instance_attr->store)
  96. return instance_attr->store(pci, buffer, count);
  97. return -EIO;
  98. }
  99. /* fs_ops table */
  100. static const struct sysfs_ops pci_instance_ops = {
  101. .show = edac_pci_instance_show,
  102. .store = edac_pci_instance_store
  103. };
  104. #define INSTANCE_ATTR(_name, _mode, _show, _store) \
  105. static struct instance_attribute attr_instance_##_name = { \
  106. .attr = {.name = __stringify(_name), .mode = _mode }, \
  107. .show = _show, \
  108. .store = _store, \
  109. };
  110. INSTANCE_ATTR(pe_count, S_IRUGO, instance_pe_count_show, NULL);
  111. INSTANCE_ATTR(npe_count, S_IRUGO, instance_npe_count_show, NULL);
  112. /* pci instance attributes */
  113. static struct instance_attribute *pci_instance_attr[] = {
  114. &attr_instance_pe_count,
  115. &attr_instance_npe_count,
  116. NULL
  117. };
  118. /* the ktype for a pci instance */
  119. static struct kobj_type ktype_pci_instance = {
  120. .release = edac_pci_instance_release,
  121. .sysfs_ops = &pci_instance_ops,
  122. .default_attrs = (struct attribute **)pci_instance_attr,
  123. };
  124. /*
  125. * edac_pci_create_instance_kobj
  126. *
  127. * construct one EDAC PCI instance's kobject for use
  128. */
  129. static int edac_pci_create_instance_kobj(struct edac_pci_ctl_info *pci, int idx)
  130. {
  131. struct kobject *main_kobj;
  132. int err;
  133. debugf0("%s()\n", __func__);
  134. /* First bump the ref count on the top main kobj, which will
  135. * track the number of PCI instances we have, and thus nest
  136. * properly on keeping the module loaded
  137. */
  138. main_kobj = kobject_get(edac_pci_top_main_kobj);
  139. if (!main_kobj) {
  140. err = -ENODEV;
  141. goto error_out;
  142. }
  143. /* And now register this new kobject under the main kobj */
  144. err = kobject_init_and_add(&pci->kobj, &ktype_pci_instance,
  145. edac_pci_top_main_kobj, "pci%d", idx);
  146. if (err != 0) {
  147. debugf2("%s() failed to register instance pci%d\n",
  148. __func__, idx);
  149. kobject_put(edac_pci_top_main_kobj);
  150. goto error_out;
  151. }
  152. kobject_uevent(&pci->kobj, KOBJ_ADD);
  153. debugf1("%s() Register instance 'pci%d' kobject\n", __func__, idx);
  154. return 0;
  155. /* Error unwind statck */
  156. error_out:
  157. return err;
  158. }
  159. /*
  160. * edac_pci_unregister_sysfs_instance_kobj
  161. *
  162. * unregister the kobj for the EDAC PCI instance
  163. */
  164. static void edac_pci_unregister_sysfs_instance_kobj(
  165. struct edac_pci_ctl_info *pci)
  166. {
  167. debugf0("%s()\n", __func__);
  168. /* Unregister the instance kobject and allow its release
  169. * function release the main reference count and then
  170. * kfree the memory
  171. */
  172. kobject_put(&pci->kobj);
  173. }
  174. /***************************** EDAC PCI sysfs root **********************/
  175. #define to_edacpci(k) container_of(k, struct edac_pci_ctl_info, kobj)
  176. #define to_edacpci_attr(a) container_of(a, struct edac_pci_attr, attr)
  177. /* simple show/store functions for attributes */
  178. static ssize_t edac_pci_int_show(void *ptr, char *buffer)
  179. {
  180. int *value = ptr;
  181. return sprintf(buffer, "%d\n", *value);
  182. }
  183. static ssize_t edac_pci_int_store(void *ptr, const char *buffer, size_t count)
  184. {
  185. int *value = ptr;
  186. if (isdigit(*buffer))
  187. *value = simple_strtoul(buffer, NULL, 0);
  188. return count;
  189. }
  190. struct edac_pci_dev_attribute {
  191. struct attribute attr;
  192. void *value;
  193. ssize_t(*show) (void *, char *);
  194. ssize_t(*store) (void *, const char *, size_t);
  195. };
  196. /* Set of show/store abstract level functions for PCI Parity object */
  197. static ssize_t edac_pci_dev_show(struct kobject *kobj, struct attribute *attr,
  198. char *buffer)
  199. {
  200. struct edac_pci_dev_attribute *edac_pci_dev;
  201. edac_pci_dev = (struct edac_pci_dev_attribute *)attr;
  202. if (edac_pci_dev->show)
  203. return edac_pci_dev->show(edac_pci_dev->value, buffer);
  204. return -EIO;
  205. }
  206. static ssize_t edac_pci_dev_store(struct kobject *kobj,
  207. struct attribute *attr, const char *buffer,
  208. size_t count)
  209. {
  210. struct edac_pci_dev_attribute *edac_pci_dev;
  211. edac_pci_dev = (struct edac_pci_dev_attribute *)attr;
  212. if (edac_pci_dev->store)
  213. return edac_pci_dev->store(edac_pci_dev->value, buffer, count);
  214. return -EIO;
  215. }
  216. static const struct sysfs_ops edac_pci_sysfs_ops = {
  217. .show = edac_pci_dev_show,
  218. .store = edac_pci_dev_store
  219. };
  220. #define EDAC_PCI_ATTR(_name,_mode,_show,_store) \
  221. static struct edac_pci_dev_attribute edac_pci_attr_##_name = { \
  222. .attr = {.name = __stringify(_name), .mode = _mode }, \
  223. .value = &_name, \
  224. .show = _show, \
  225. .store = _store, \
  226. };
  227. #define EDAC_PCI_STRING_ATTR(_name,_data,_mode,_show,_store) \
  228. static struct edac_pci_dev_attribute edac_pci_attr_##_name = { \
  229. .attr = {.name = __stringify(_name), .mode = _mode }, \
  230. .value = _data, \
  231. .show = _show, \
  232. .store = _store, \
  233. };
  234. /* PCI Parity control files */
  235. EDAC_PCI_ATTR(check_pci_errors, S_IRUGO | S_IWUSR, edac_pci_int_show,
  236. edac_pci_int_store);
  237. EDAC_PCI_ATTR(edac_pci_log_pe, S_IRUGO | S_IWUSR, edac_pci_int_show,
  238. edac_pci_int_store);
  239. EDAC_PCI_ATTR(edac_pci_log_npe, S_IRUGO | S_IWUSR, edac_pci_int_show,
  240. edac_pci_int_store);
  241. EDAC_PCI_ATTR(edac_pci_panic_on_pe, S_IRUGO | S_IWUSR, edac_pci_int_show,
  242. edac_pci_int_store);
  243. EDAC_PCI_ATTR(pci_parity_count, S_IRUGO, edac_pci_int_show, NULL);
  244. EDAC_PCI_ATTR(pci_nonparity_count, S_IRUGO, edac_pci_int_show, NULL);
  245. /* Base Attributes of the memory ECC object */
  246. static struct edac_pci_dev_attribute *edac_pci_attr[] = {
  247. &edac_pci_attr_check_pci_errors,
  248. &edac_pci_attr_edac_pci_log_pe,
  249. &edac_pci_attr_edac_pci_log_npe,
  250. &edac_pci_attr_edac_pci_panic_on_pe,
  251. &edac_pci_attr_pci_parity_count,
  252. &edac_pci_attr_pci_nonparity_count,
  253. NULL,
  254. };
  255. /*
  256. * edac_pci_release_main_kobj
  257. *
  258. * This release function is called when the reference count to the
  259. * passed kobj goes to zero.
  260. *
  261. * This kobj is the 'main' kobject that EDAC PCI instances
  262. * link to, and thus provide for proper nesting counts
  263. */
  264. static void edac_pci_release_main_kobj(struct kobject *kobj)
  265. {
  266. debugf0("%s() here to module_put(THIS_MODULE)\n", __func__);
  267. kfree(kobj);
  268. /* last reference to top EDAC PCI kobject has been removed,
  269. * NOW release our ref count on the core module
  270. */
  271. module_put(THIS_MODULE);
  272. }
  273. /* ktype struct for the EDAC PCI main kobj */
  274. static struct kobj_type ktype_edac_pci_main_kobj = {
  275. .release = edac_pci_release_main_kobj,
  276. .sysfs_ops = &edac_pci_sysfs_ops,
  277. .default_attrs = (struct attribute **)edac_pci_attr,
  278. };
  279. /**
  280. * edac_pci_main_kobj_setup()
  281. *
  282. * setup the sysfs for EDAC PCI attributes
  283. * assumes edac_subsys has already been initialized
  284. */
  285. static int edac_pci_main_kobj_setup(void)
  286. {
  287. int err;
  288. struct bus_type *edac_subsys;
  289. debugf0("%s()\n", __func__);
  290. /* check and count if we have already created the main kobject */
  291. if (atomic_inc_return(&edac_pci_sysfs_refcount) != 1)
  292. return 0;
  293. /* First time, so create the main kobject and its
  294. * controls and attributes
  295. */
  296. edac_subsys = edac_get_sysfs_subsys();
  297. if (edac_subsys == NULL) {
  298. debugf1("%s() no edac_subsys\n", __func__);
  299. err = -ENODEV;
  300. goto decrement_count_fail;
  301. }
  302. /* Bump the reference count on this module to ensure the
  303. * modules isn't unloaded until we deconstruct the top
  304. * level main kobj for EDAC PCI
  305. */
  306. if (!try_module_get(THIS_MODULE)) {
  307. debugf1("%s() try_module_get() failed\n", __func__);
  308. err = -ENODEV;
  309. goto mod_get_fail;
  310. }
  311. edac_pci_top_main_kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
  312. if (!edac_pci_top_main_kobj) {
  313. debugf1("Failed to allocate\n");
  314. err = -ENOMEM;
  315. goto kzalloc_fail;
  316. }
  317. /* Instanstiate the pci object */
  318. err = kobject_init_and_add(edac_pci_top_main_kobj,
  319. &ktype_edac_pci_main_kobj,
  320. &edac_subsys->dev_root->kobj, "pci");
  321. if (err) {
  322. debugf1("Failed to register '.../edac/pci'\n");
  323. goto kobject_init_and_add_fail;
  324. }
  325. /* At this point, to 'release' the top level kobject
  326. * for EDAC PCI, then edac_pci_main_kobj_teardown()
  327. * must be used, for resources to be cleaned up properly
  328. */
  329. kobject_uevent(edac_pci_top_main_kobj, KOBJ_ADD);
  330. debugf1("Registered '.../edac/pci' kobject\n");
  331. return 0;
  332. /* Error unwind statck */
  333. kobject_init_and_add_fail:
  334. kfree(edac_pci_top_main_kobj);
  335. kzalloc_fail:
  336. module_put(THIS_MODULE);
  337. mod_get_fail:
  338. edac_put_sysfs_subsys();
  339. decrement_count_fail:
  340. /* if are on this error exit, nothing to tear down */
  341. atomic_dec(&edac_pci_sysfs_refcount);
  342. return err;
  343. }
  344. /*
  345. * edac_pci_main_kobj_teardown()
  346. *
  347. * if no longer linked (needed) remove the top level EDAC PCI
  348. * kobject with its controls and attributes
  349. */
  350. static void edac_pci_main_kobj_teardown(void)
  351. {
  352. debugf0("%s()\n", __func__);
  353. /* Decrement the count and only if no more controller instances
  354. * are connected perform the unregisteration of the top level
  355. * main kobj
  356. */
  357. if (atomic_dec_return(&edac_pci_sysfs_refcount) == 0) {
  358. debugf0("%s() called kobject_put on main kobj\n",
  359. __func__);
  360. kobject_put(edac_pci_top_main_kobj);
  361. }
  362. edac_put_sysfs_subsys();
  363. }
  364. /*
  365. *
  366. * edac_pci_create_sysfs
  367. *
  368. * Create the controls/attributes for the specified EDAC PCI device
  369. */
  370. int edac_pci_create_sysfs(struct edac_pci_ctl_info *pci)
  371. {
  372. int err;
  373. struct kobject *edac_kobj = &pci->kobj;
  374. debugf0("%s() idx=%d\n", __func__, pci->pci_idx);
  375. /* create the top main EDAC PCI kobject, IF needed */
  376. err = edac_pci_main_kobj_setup();
  377. if (err)
  378. return err;
  379. /* Create this instance's kobject under the MAIN kobject */
  380. err = edac_pci_create_instance_kobj(pci, pci->pci_idx);
  381. if (err)
  382. goto unregister_cleanup;
  383. err = sysfs_create_link(edac_kobj, &pci->dev->kobj, EDAC_PCI_SYMLINK);
  384. if (err) {
  385. debugf0("%s() sysfs_create_link() returned err= %d\n",
  386. __func__, err);
  387. goto symlink_fail;
  388. }
  389. return 0;
  390. /* Error unwind stack */
  391. symlink_fail:
  392. edac_pci_unregister_sysfs_instance_kobj(pci);
  393. unregister_cleanup:
  394. edac_pci_main_kobj_teardown();
  395. return err;
  396. }
  397. /*
  398. * edac_pci_remove_sysfs
  399. *
  400. * remove the controls and attributes for this EDAC PCI device
  401. */
  402. void edac_pci_remove_sysfs(struct edac_pci_ctl_info *pci)
  403. {
  404. debugf0("%s() index=%d\n", __func__, pci->pci_idx);
  405. /* Remove the symlink */
  406. sysfs_remove_link(&pci->kobj, EDAC_PCI_SYMLINK);
  407. /* remove this PCI instance's sysfs entries */
  408. edac_pci_unregister_sysfs_instance_kobj(pci);
  409. /* Call the main unregister function, which will determine
  410. * if this 'pci' is the last instance.
  411. * If it is, the main kobject will be unregistered as a result
  412. */
  413. debugf0("%s() calling edac_pci_main_kobj_teardown()\n", __func__);
  414. edac_pci_main_kobj_teardown();
  415. }
  416. /************************ PCI error handling *************************/
  417. static u16 get_pci_parity_status(struct pci_dev *dev, int secondary)
  418. {
  419. int where;
  420. u16 status;
  421. where = secondary ? PCI_SEC_STATUS : PCI_STATUS;
  422. pci_read_config_word(dev, where, &status);
  423. /* If we get back 0xFFFF then we must suspect that the card has been
  424. * pulled but the Linux PCI layer has not yet finished cleaning up.
  425. * We don't want to report on such devices
  426. */
  427. if (status == 0xFFFF) {
  428. u32 sanity;
  429. pci_read_config_dword(dev, 0, &sanity);
  430. if (sanity == 0xFFFFFFFF)
  431. return 0;
  432. }
  433. status &= PCI_STATUS_DETECTED_PARITY | PCI_STATUS_SIG_SYSTEM_ERROR |
  434. PCI_STATUS_PARITY;
  435. if (status)
  436. /* reset only the bits we are interested in */
  437. pci_write_config_word(dev, where, status);
  438. return status;
  439. }
  440. /* Clear any PCI parity errors logged by this device. */
  441. static void edac_pci_dev_parity_clear(struct pci_dev *dev)
  442. {
  443. u8 header_type;
  444. get_pci_parity_status(dev, 0);
  445. /* read the device TYPE, looking for bridges */
  446. pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
  447. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE)
  448. get_pci_parity_status(dev, 1);
  449. }
  450. /*
  451. * PCI Parity polling
  452. *
  453. * Function to retrieve the current parity status
  454. * and decode it
  455. *
  456. */
  457. static void edac_pci_dev_parity_test(struct pci_dev *dev)
  458. {
  459. unsigned long flags;
  460. u16 status;
  461. u8 header_type;
  462. /* stop any interrupts until we can acquire the status */
  463. local_irq_save(flags);
  464. /* read the STATUS register on this device */
  465. status = get_pci_parity_status(dev, 0);
  466. /* read the device TYPE, looking for bridges */
  467. pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
  468. local_irq_restore(flags);
  469. debugf4("PCI STATUS= 0x%04x %s\n", status, dev_name(&dev->dev));
  470. /* check the status reg for errors on boards NOT marked as broken
  471. * if broken, we cannot trust any of the status bits
  472. */
  473. if (status && !dev->broken_parity_status) {
  474. if (status & (PCI_STATUS_SIG_SYSTEM_ERROR)) {
  475. edac_printk(KERN_CRIT, EDAC_PCI,
  476. "Signaled System Error on %s\n",
  477. pci_name(dev));
  478. atomic_inc(&pci_nonparity_count);
  479. }
  480. if (status & (PCI_STATUS_PARITY)) {
  481. edac_printk(KERN_CRIT, EDAC_PCI,
  482. "Master Data Parity Error on %s\n",
  483. pci_name(dev));
  484. atomic_inc(&pci_parity_count);
  485. }
  486. if (status & (PCI_STATUS_DETECTED_PARITY)) {
  487. edac_printk(KERN_CRIT, EDAC_PCI,
  488. "Detected Parity Error on %s\n",
  489. pci_name(dev));
  490. atomic_inc(&pci_parity_count);
  491. }
  492. }
  493. debugf4("PCI HEADER TYPE= 0x%02x %s\n", header_type, dev_name(&dev->dev));
  494. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
  495. /* On bridges, need to examine secondary status register */
  496. status = get_pci_parity_status(dev, 1);
  497. debugf4("PCI SEC_STATUS= 0x%04x %s\n", status, dev_name(&dev->dev));
  498. /* check the secondary status reg for errors,
  499. * on NOT broken boards
  500. */
  501. if (status && !dev->broken_parity_status) {
  502. if (status & (PCI_STATUS_SIG_SYSTEM_ERROR)) {
  503. edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
  504. "Signaled System Error on %s\n",
  505. pci_name(dev));
  506. atomic_inc(&pci_nonparity_count);
  507. }
  508. if (status & (PCI_STATUS_PARITY)) {
  509. edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
  510. "Master Data Parity Error on "
  511. "%s\n", pci_name(dev));
  512. atomic_inc(&pci_parity_count);
  513. }
  514. if (status & (PCI_STATUS_DETECTED_PARITY)) {
  515. edac_printk(KERN_CRIT, EDAC_PCI, "Bridge "
  516. "Detected Parity Error on %s\n",
  517. pci_name(dev));
  518. atomic_inc(&pci_parity_count);
  519. }
  520. }
  521. }
  522. }
  523. /* reduce some complexity in definition of the iterator */
  524. typedef void (*pci_parity_check_fn_t) (struct pci_dev *dev);
  525. /*
  526. * pci_dev parity list iterator
  527. * Scan the PCI device list for one pass, looking for SERRORs
  528. * Master Parity ERRORS or Parity ERRORs on primary or secondary devices
  529. */
  530. static inline void edac_pci_dev_parity_iterator(pci_parity_check_fn_t fn)
  531. {
  532. struct pci_dev *dev = NULL;
  533. /* request for kernel access to the next PCI device, if any,
  534. * and while we are looking at it have its reference count
  535. * bumped until we are done with it
  536. */
  537. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  538. fn(dev);
  539. }
  540. }
  541. /*
  542. * edac_pci_do_parity_check
  543. *
  544. * performs the actual PCI parity check operation
  545. */
  546. void edac_pci_do_parity_check(void)
  547. {
  548. int before_count;
  549. debugf3("%s()\n", __func__);
  550. /* if policy has PCI check off, leave now */
  551. if (!check_pci_errors)
  552. return;
  553. before_count = atomic_read(&pci_parity_count);
  554. /* scan all PCI devices looking for a Parity Error on devices and
  555. * bridges.
  556. * The iterator calls pci_get_device() which might sleep, thus
  557. * we cannot disable interrupts in this scan.
  558. */
  559. edac_pci_dev_parity_iterator(edac_pci_dev_parity_test);
  560. /* Only if operator has selected panic on PCI Error */
  561. if (edac_pci_get_panic_on_pe()) {
  562. /* If the count is different 'after' from 'before' */
  563. if (before_count != atomic_read(&pci_parity_count))
  564. panic("EDAC: PCI Parity Error");
  565. }
  566. }
  567. /*
  568. * edac_pci_clear_parity_errors
  569. *
  570. * function to perform an iteration over the PCI devices
  571. * and clearn their current status
  572. */
  573. void edac_pci_clear_parity_errors(void)
  574. {
  575. /* Clear any PCI bus parity errors that devices initially have logged
  576. * in their registers.
  577. */
  578. edac_pci_dev_parity_iterator(edac_pci_dev_parity_clear);
  579. }
  580. /*
  581. * edac_pci_handle_pe
  582. *
  583. * Called to handle a PARITY ERROR event
  584. */
  585. void edac_pci_handle_pe(struct edac_pci_ctl_info *pci, const char *msg)
  586. {
  587. /* global PE counter incremented by edac_pci_do_parity_check() */
  588. atomic_inc(&pci->counters.pe_count);
  589. if (edac_pci_get_log_pe())
  590. edac_pci_printk(pci, KERN_WARNING,
  591. "Parity Error ctl: %s %d: %s\n",
  592. pci->ctl_name, pci->pci_idx, msg);
  593. /*
  594. * poke all PCI devices and see which one is the troublemaker
  595. * panic() is called if set
  596. */
  597. edac_pci_do_parity_check();
  598. }
  599. EXPORT_SYMBOL_GPL(edac_pci_handle_pe);
  600. /*
  601. * edac_pci_handle_npe
  602. *
  603. * Called to handle a NON-PARITY ERROR event
  604. */
  605. void edac_pci_handle_npe(struct edac_pci_ctl_info *pci, const char *msg)
  606. {
  607. /* global NPE counter incremented by edac_pci_do_parity_check() */
  608. atomic_inc(&pci->counters.npe_count);
  609. if (edac_pci_get_log_npe())
  610. edac_pci_printk(pci, KERN_WARNING,
  611. "Non-Parity Error ctl: %s %d: %s\n",
  612. pci->ctl_name, pci->pci_idx, msg);
  613. /*
  614. * poke all PCI devices and see which one is the troublemaker
  615. * panic() is called if set
  616. */
  617. edac_pci_do_parity_check();
  618. }
  619. EXPORT_SYMBOL_GPL(edac_pci_handle_npe);
  620. /*
  621. * Define the PCI parameter to the module
  622. */
  623. module_param(check_pci_errors, int, 0644);
  624. MODULE_PARM_DESC(check_pci_errors,
  625. "Check for PCI bus parity errors: 0=off 1=on");
  626. module_param(edac_pci_panic_on_pe, int, 0644);
  627. MODULE_PARM_DESC(edac_pci_panic_on_pe,
  628. "Panic on PCI Bus Parity error: 0=off 1=on");
  629. #endif /* CONFIG_PCI */