edac_pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * EDAC PCI component
  3. *
  4. * Author: Dave Jiang <djiang@mvista.com>
  5. *
  6. * 2007 (c) MontaVista Software, Inc. This file is licensed under
  7. * the terms of the GNU General Public License version 2. This program
  8. * is licensed "as is" without any warranty of any kind, whether express
  9. * or implied.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/smp.h>
  15. #include <linux/init.h>
  16. #include <linux/sysctl.h>
  17. #include <linux/highmem.h>
  18. #include <linux/timer.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/list.h>
  22. #include <linux/sysdev.h>
  23. #include <linux/ctype.h>
  24. #include <linux/workqueue.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/page.h>
  27. #include "edac_core.h"
  28. #include "edac_module.h"
  29. static DEFINE_MUTEX(edac_pci_ctls_mutex);
  30. static LIST_HEAD(edac_pci_list);
  31. static atomic_t pci_indexes = ATOMIC_INIT(0);
  32. /*
  33. * edac_pci_alloc_ctl_info
  34. *
  35. * The alloc() function for the 'edac_pci' control info
  36. * structure. The chip driver will allocate one of these for each
  37. * edac_pci it is going to control/register with the EDAC CORE.
  38. */
  39. struct edac_pci_ctl_info *edac_pci_alloc_ctl_info(unsigned int sz_pvt,
  40. const char *edac_pci_name)
  41. {
  42. struct edac_pci_ctl_info *pci;
  43. void *pvt;
  44. unsigned int size;
  45. debugf1("%s()\n", __func__);
  46. pci = (struct edac_pci_ctl_info *)0;
  47. pvt = edac_align_ptr(&pci[1], sz_pvt);
  48. size = ((unsigned long)pvt) + sz_pvt;
  49. /* Alloc the needed control struct memory */
  50. pci = kzalloc(size, GFP_KERNEL);
  51. if (pci == NULL)
  52. return NULL;
  53. /* Now much private space */
  54. pvt = sz_pvt ? ((char *)pci) + ((unsigned long)pvt) : NULL;
  55. pci->pvt_info = pvt;
  56. pci->op_state = OP_ALLOC;
  57. snprintf(pci->name, strlen(edac_pci_name) + 1, "%s", edac_pci_name);
  58. return pci;
  59. }
  60. EXPORT_SYMBOL_GPL(edac_pci_alloc_ctl_info);
  61. /*
  62. * edac_pci_free_ctl_info()
  63. *
  64. * Last action on the pci control structure.
  65. *
  66. * call the remove sysfs information, which will unregister
  67. * this control struct's kobj. When that kobj's ref count
  68. * goes to zero, its release function will be call and then
  69. * kfree() the memory.
  70. */
  71. void edac_pci_free_ctl_info(struct edac_pci_ctl_info *pci)
  72. {
  73. debugf1("%s()\n", __func__);
  74. edac_pci_remove_sysfs(pci);
  75. }
  76. EXPORT_SYMBOL_GPL(edac_pci_free_ctl_info);
  77. /*
  78. * find_edac_pci_by_dev()
  79. * scans the edac_pci list for a specific 'struct device *'
  80. *
  81. * return NULL if not found, or return control struct pointer
  82. */
  83. static struct edac_pci_ctl_info *find_edac_pci_by_dev(struct device *dev)
  84. {
  85. struct edac_pci_ctl_info *pci;
  86. struct list_head *item;
  87. debugf1("%s()\n", __func__);
  88. list_for_each(item, &edac_pci_list) {
  89. pci = list_entry(item, struct edac_pci_ctl_info, link);
  90. if (pci->dev == dev)
  91. return pci;
  92. }
  93. return NULL;
  94. }
  95. /*
  96. * add_edac_pci_to_global_list
  97. * Before calling this function, caller must assign a unique value to
  98. * edac_dev->pci_idx.
  99. * Return:
  100. * 0 on success
  101. * 1 on failure
  102. */
  103. static int add_edac_pci_to_global_list(struct edac_pci_ctl_info *pci)
  104. {
  105. struct list_head *item, *insert_before;
  106. struct edac_pci_ctl_info *rover;
  107. debugf1("%s()\n", __func__);
  108. insert_before = &edac_pci_list;
  109. /* Determine if already on the list */
  110. rover = find_edac_pci_by_dev(pci->dev);
  111. if (unlikely(rover != NULL))
  112. goto fail0;
  113. /* Insert in ascending order by 'pci_idx', so find position */
  114. list_for_each(item, &edac_pci_list) {
  115. rover = list_entry(item, struct edac_pci_ctl_info, link);
  116. if (rover->pci_idx >= pci->pci_idx) {
  117. if (unlikely(rover->pci_idx == pci->pci_idx))
  118. goto fail1;
  119. insert_before = item;
  120. break;
  121. }
  122. }
  123. list_add_tail_rcu(&pci->link, insert_before);
  124. return 0;
  125. fail0:
  126. edac_printk(KERN_WARNING, EDAC_PCI,
  127. "%s (%s) %s %s already assigned %d\n",
  128. dev_name(rover->dev), edac_dev_name(rover),
  129. rover->mod_name, rover->ctl_name, rover->pci_idx);
  130. return 1;
  131. fail1:
  132. edac_printk(KERN_WARNING, EDAC_PCI,
  133. "but in low-level driver: attempt to assign\n"
  134. "\tduplicate pci_idx %d in %s()\n", rover->pci_idx,
  135. __func__);
  136. return 1;
  137. }
  138. /*
  139. * complete_edac_pci_list_del
  140. *
  141. * RCU completion callback to indicate item is deleted
  142. */
  143. static void complete_edac_pci_list_del(struct rcu_head *head)
  144. {
  145. struct edac_pci_ctl_info *pci;
  146. pci = container_of(head, struct edac_pci_ctl_info, rcu);
  147. INIT_LIST_HEAD(&pci->link);
  148. }
  149. /*
  150. * del_edac_pci_from_global_list
  151. *
  152. * remove the PCI control struct from the global list
  153. */
  154. static void del_edac_pci_from_global_list(struct edac_pci_ctl_info *pci)
  155. {
  156. list_del_rcu(&pci->link);
  157. call_rcu(&pci->rcu, complete_edac_pci_list_del);
  158. rcu_barrier();
  159. }
  160. #if 0
  161. /* Older code, but might use in the future */
  162. /*
  163. * edac_pci_find()
  164. * Search for an edac_pci_ctl_info structure whose index is 'idx'
  165. *
  166. * If found, return a pointer to the structure
  167. * Else return NULL.
  168. *
  169. * Caller must hold pci_ctls_mutex.
  170. */
  171. struct edac_pci_ctl_info *edac_pci_find(int idx)
  172. {
  173. struct list_head *item;
  174. struct edac_pci_ctl_info *pci;
  175. /* Iterage over list, looking for exact match of ID */
  176. list_for_each(item, &edac_pci_list) {
  177. pci = list_entry(item, struct edac_pci_ctl_info, link);
  178. if (pci->pci_idx >= idx) {
  179. if (pci->pci_idx == idx)
  180. return pci;
  181. /* not on list, so terminate early */
  182. break;
  183. }
  184. }
  185. return NULL;
  186. }
  187. EXPORT_SYMBOL_GPL(edac_pci_find);
  188. #endif
  189. /*
  190. * edac_pci_workq_function()
  191. *
  192. * periodic function that performs the operation
  193. * scheduled by a workq request, for a given PCI control struct
  194. */
  195. static void edac_pci_workq_function(struct work_struct *work_req)
  196. {
  197. struct delayed_work *d_work = to_delayed_work(work_req);
  198. struct edac_pci_ctl_info *pci = to_edac_pci_ctl_work(d_work);
  199. int msec;
  200. unsigned long delay;
  201. debugf3("%s() checking\n", __func__);
  202. mutex_lock(&edac_pci_ctls_mutex);
  203. if (pci->op_state == OP_RUNNING_POLL) {
  204. /* we might be in POLL mode, but there may NOT be a poll func
  205. */
  206. if ((pci->edac_check != NULL) && edac_pci_get_check_errors())
  207. pci->edac_check(pci);
  208. /* if we are on a one second period, then use round */
  209. msec = edac_pci_get_poll_msec();
  210. if (msec == 1000)
  211. delay = round_jiffies_relative(msecs_to_jiffies(msec));
  212. else
  213. delay = msecs_to_jiffies(msec);
  214. /* Reschedule only if we are in POLL mode */
  215. queue_delayed_work(edac_workqueue, &pci->work, delay);
  216. }
  217. mutex_unlock(&edac_pci_ctls_mutex);
  218. }
  219. /*
  220. * edac_pci_workq_setup()
  221. * initialize a workq item for this edac_pci instance
  222. * passing in the new delay period in msec
  223. *
  224. * locking model:
  225. * called when 'edac_pci_ctls_mutex' is locked
  226. */
  227. static void edac_pci_workq_setup(struct edac_pci_ctl_info *pci,
  228. unsigned int msec)
  229. {
  230. debugf0("%s()\n", __func__);
  231. INIT_DELAYED_WORK(&pci->work, edac_pci_workq_function);
  232. queue_delayed_work(edac_workqueue, &pci->work,
  233. msecs_to_jiffies(edac_pci_get_poll_msec()));
  234. }
  235. /*
  236. * edac_pci_workq_teardown()
  237. * stop the workq processing on this edac_pci instance
  238. */
  239. static void edac_pci_workq_teardown(struct edac_pci_ctl_info *pci)
  240. {
  241. int status;
  242. debugf0("%s()\n", __func__);
  243. status = cancel_delayed_work(&pci->work);
  244. if (status == 0)
  245. flush_workqueue(edac_workqueue);
  246. }
  247. /*
  248. * edac_pci_reset_delay_period
  249. *
  250. * called with a new period value for the workq period
  251. * a) stop current workq timer
  252. * b) restart workq timer with new value
  253. */
  254. void edac_pci_reset_delay_period(struct edac_pci_ctl_info *pci,
  255. unsigned long value)
  256. {
  257. debugf0("%s()\n", __func__);
  258. edac_pci_workq_teardown(pci);
  259. /* need to lock for the setup */
  260. mutex_lock(&edac_pci_ctls_mutex);
  261. edac_pci_workq_setup(pci, value);
  262. mutex_unlock(&edac_pci_ctls_mutex);
  263. }
  264. EXPORT_SYMBOL_GPL(edac_pci_reset_delay_period);
  265. /*
  266. * edac_pci_alloc_index: Allocate a unique PCI index number
  267. *
  268. * Return:
  269. * allocated index number
  270. *
  271. */
  272. int edac_pci_alloc_index(void)
  273. {
  274. return atomic_inc_return(&pci_indexes) - 1;
  275. }
  276. EXPORT_SYMBOL_GPL(edac_pci_alloc_index);
  277. /*
  278. * edac_pci_add_device: Insert the 'edac_dev' structure into the
  279. * edac_pci global list and create sysfs entries associated with
  280. * edac_pci structure.
  281. * @pci: pointer to the edac_device structure to be added to the list
  282. * @edac_idx: A unique numeric identifier to be assigned to the
  283. * 'edac_pci' structure.
  284. *
  285. * Return:
  286. * 0 Success
  287. * !0 Failure
  288. */
  289. int edac_pci_add_device(struct edac_pci_ctl_info *pci, int edac_idx)
  290. {
  291. debugf0("%s()\n", __func__);
  292. pci->pci_idx = edac_idx;
  293. pci->start_time = jiffies;
  294. mutex_lock(&edac_pci_ctls_mutex);
  295. if (add_edac_pci_to_global_list(pci))
  296. goto fail0;
  297. if (edac_pci_create_sysfs(pci)) {
  298. edac_pci_printk(pci, KERN_WARNING,
  299. "failed to create sysfs pci\n");
  300. goto fail1;
  301. }
  302. if (pci->edac_check != NULL) {
  303. pci->op_state = OP_RUNNING_POLL;
  304. edac_pci_workq_setup(pci, 1000);
  305. } else {
  306. pci->op_state = OP_RUNNING_INTERRUPT;
  307. }
  308. edac_pci_printk(pci, KERN_INFO,
  309. "Giving out device to module '%s' controller '%s':"
  310. " DEV '%s' (%s)\n",
  311. pci->mod_name,
  312. pci->ctl_name,
  313. edac_dev_name(pci), edac_op_state_to_string(pci->op_state));
  314. mutex_unlock(&edac_pci_ctls_mutex);
  315. return 0;
  316. /* error unwind stack */
  317. fail1:
  318. del_edac_pci_from_global_list(pci);
  319. fail0:
  320. mutex_unlock(&edac_pci_ctls_mutex);
  321. return 1;
  322. }
  323. EXPORT_SYMBOL_GPL(edac_pci_add_device);
  324. /*
  325. * edac_pci_del_device()
  326. * Remove sysfs entries for specified edac_pci structure and
  327. * then remove edac_pci structure from global list
  328. *
  329. * @dev:
  330. * Pointer to 'struct device' representing edac_pci structure
  331. * to remove
  332. *
  333. * Return:
  334. * Pointer to removed edac_pci structure,
  335. * or NULL if device not found
  336. */
  337. struct edac_pci_ctl_info *edac_pci_del_device(struct device *dev)
  338. {
  339. struct edac_pci_ctl_info *pci;
  340. debugf0("%s()\n", __func__);
  341. mutex_lock(&edac_pci_ctls_mutex);
  342. /* ensure the control struct is on the global list
  343. * if not, then leave
  344. */
  345. pci = find_edac_pci_by_dev(dev);
  346. if (pci == NULL) {
  347. mutex_unlock(&edac_pci_ctls_mutex);
  348. return NULL;
  349. }
  350. pci->op_state = OP_OFFLINE;
  351. del_edac_pci_from_global_list(pci);
  352. mutex_unlock(&edac_pci_ctls_mutex);
  353. /* stop the workq timer */
  354. edac_pci_workq_teardown(pci);
  355. edac_printk(KERN_INFO, EDAC_PCI,
  356. "Removed device %d for %s %s: DEV %s\n",
  357. pci->pci_idx, pci->mod_name, pci->ctl_name, edac_dev_name(pci));
  358. return pci;
  359. }
  360. EXPORT_SYMBOL_GPL(edac_pci_del_device);
  361. /*
  362. * edac_pci_generic_check
  363. *
  364. * a Generic parity check API
  365. */
  366. static void edac_pci_generic_check(struct edac_pci_ctl_info *pci)
  367. {
  368. debugf4("%s()\n", __func__);
  369. edac_pci_do_parity_check();
  370. }
  371. /* free running instance index counter */
  372. static int edac_pci_idx;
  373. #define EDAC_PCI_GENCTL_NAME "EDAC PCI controller"
  374. struct edac_pci_gen_data {
  375. int edac_idx;
  376. };
  377. /*
  378. * edac_pci_create_generic_ctl
  379. *
  380. * A generic constructor for a PCI parity polling device
  381. * Some systems have more than one domain of PCI busses.
  382. * For systems with one domain, then this API will
  383. * provide for a generic poller.
  384. *
  385. * This routine calls the edac_pci_alloc_ctl_info() for
  386. * the generic device, with default values
  387. */
  388. struct edac_pci_ctl_info *edac_pci_create_generic_ctl(struct device *dev,
  389. const char *mod_name)
  390. {
  391. struct edac_pci_ctl_info *pci;
  392. struct edac_pci_gen_data *pdata;
  393. pci = edac_pci_alloc_ctl_info(sizeof(*pdata), EDAC_PCI_GENCTL_NAME);
  394. if (!pci)
  395. return NULL;
  396. pdata = pci->pvt_info;
  397. pci->dev = dev;
  398. dev_set_drvdata(pci->dev, pci);
  399. pci->dev_name = pci_name(to_pci_dev(dev));
  400. pci->mod_name = mod_name;
  401. pci->ctl_name = EDAC_PCI_GENCTL_NAME;
  402. pci->edac_check = edac_pci_generic_check;
  403. pdata->edac_idx = edac_pci_idx++;
  404. if (edac_pci_add_device(pci, pdata->edac_idx) > 0) {
  405. debugf3("%s(): failed edac_pci_add_device()\n", __func__);
  406. edac_pci_free_ctl_info(pci);
  407. return NULL;
  408. }
  409. return pci;
  410. }
  411. EXPORT_SYMBOL_GPL(edac_pci_create_generic_ctl);
  412. /*
  413. * edac_pci_release_generic_ctl
  414. *
  415. * The release function of a generic EDAC PCI polling device
  416. */
  417. void edac_pci_release_generic_ctl(struct edac_pci_ctl_info *pci)
  418. {
  419. debugf0("%s() pci mod=%s\n", __func__, pci->mod_name);
  420. edac_pci_del_device(pci->dev);
  421. edac_pci_free_ctl_info(pci);
  422. }
  423. EXPORT_SYMBOL_GPL(edac_pci_release_generic_ctl);