edac_pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. complete(&pci->complete);
  149. }
  150. /*
  151. * del_edac_pci_from_global_list
  152. *
  153. * remove the PCI control struct from the global list
  154. */
  155. static void del_edac_pci_from_global_list(struct edac_pci_ctl_info *pci)
  156. {
  157. list_del_rcu(&pci->link);
  158. init_completion(&pci->complete);
  159. call_rcu(&pci->rcu, complete_edac_pci_list_del);
  160. wait_for_completion(&pci->complete);
  161. }
  162. #if 0
  163. /* Older code, but might use in the future */
  164. /*
  165. * edac_pci_find()
  166. * Search for an edac_pci_ctl_info structure whose index is 'idx'
  167. *
  168. * If found, return a pointer to the structure
  169. * Else return NULL.
  170. *
  171. * Caller must hold pci_ctls_mutex.
  172. */
  173. struct edac_pci_ctl_info *edac_pci_find(int idx)
  174. {
  175. struct list_head *item;
  176. struct edac_pci_ctl_info *pci;
  177. /* Iterage over list, looking for exact match of ID */
  178. list_for_each(item, &edac_pci_list) {
  179. pci = list_entry(item, struct edac_pci_ctl_info, link);
  180. if (pci->pci_idx >= idx) {
  181. if (pci->pci_idx == idx)
  182. return pci;
  183. /* not on list, so terminate early */
  184. break;
  185. }
  186. }
  187. return NULL;
  188. }
  189. EXPORT_SYMBOL_GPL(edac_pci_find);
  190. #endif
  191. /*
  192. * edac_pci_workq_function()
  193. *
  194. * periodic function that performs the operation
  195. * scheduled by a workq request, for a given PCI control struct
  196. */
  197. static void edac_pci_workq_function(struct work_struct *work_req)
  198. {
  199. struct delayed_work *d_work = to_delayed_work(work_req);
  200. struct edac_pci_ctl_info *pci = to_edac_pci_ctl_work(d_work);
  201. int msec;
  202. unsigned long delay;
  203. debugf3("%s() checking\n", __func__);
  204. mutex_lock(&edac_pci_ctls_mutex);
  205. if (pci->op_state == OP_RUNNING_POLL) {
  206. /* we might be in POLL mode, but there may NOT be a poll func
  207. */
  208. if ((pci->edac_check != NULL) && edac_pci_get_check_errors())
  209. pci->edac_check(pci);
  210. /* if we are on a one second period, then use round */
  211. msec = edac_pci_get_poll_msec();
  212. if (msec == 1000)
  213. delay = round_jiffies_relative(msecs_to_jiffies(msec));
  214. else
  215. delay = msecs_to_jiffies(msec);
  216. /* Reschedule only if we are in POLL mode */
  217. queue_delayed_work(edac_workqueue, &pci->work, delay);
  218. }
  219. mutex_unlock(&edac_pci_ctls_mutex);
  220. }
  221. /*
  222. * edac_pci_workq_setup()
  223. * initialize a workq item for this edac_pci instance
  224. * passing in the new delay period in msec
  225. *
  226. * locking model:
  227. * called when 'edac_pci_ctls_mutex' is locked
  228. */
  229. static void edac_pci_workq_setup(struct edac_pci_ctl_info *pci,
  230. unsigned int msec)
  231. {
  232. debugf0("%s()\n", __func__);
  233. INIT_DELAYED_WORK(&pci->work, edac_pci_workq_function);
  234. queue_delayed_work(edac_workqueue, &pci->work,
  235. msecs_to_jiffies(edac_pci_get_poll_msec()));
  236. }
  237. /*
  238. * edac_pci_workq_teardown()
  239. * stop the workq processing on this edac_pci instance
  240. */
  241. static void edac_pci_workq_teardown(struct edac_pci_ctl_info *pci)
  242. {
  243. int status;
  244. debugf0("%s()\n", __func__);
  245. status = cancel_delayed_work(&pci->work);
  246. if (status == 0)
  247. flush_workqueue(edac_workqueue);
  248. }
  249. /*
  250. * edac_pci_reset_delay_period
  251. *
  252. * called with a new period value for the workq period
  253. * a) stop current workq timer
  254. * b) restart workq timer with new value
  255. */
  256. void edac_pci_reset_delay_period(struct edac_pci_ctl_info *pci,
  257. unsigned long value)
  258. {
  259. debugf0("%s()\n", __func__);
  260. edac_pci_workq_teardown(pci);
  261. /* need to lock for the setup */
  262. mutex_lock(&edac_pci_ctls_mutex);
  263. edac_pci_workq_setup(pci, value);
  264. mutex_unlock(&edac_pci_ctls_mutex);
  265. }
  266. EXPORT_SYMBOL_GPL(edac_pci_reset_delay_period);
  267. /*
  268. * edac_pci_alloc_index: Allocate a unique PCI index number
  269. *
  270. * Return:
  271. * allocated index number
  272. *
  273. */
  274. int edac_pci_alloc_index(void)
  275. {
  276. return atomic_inc_return(&pci_indexes) - 1;
  277. }
  278. EXPORT_SYMBOL_GPL(edac_pci_alloc_index);
  279. /*
  280. * edac_pci_add_device: Insert the 'edac_dev' structure into the
  281. * edac_pci global list and create sysfs entries associated with
  282. * edac_pci structure.
  283. * @pci: pointer to the edac_device structure to be added to the list
  284. * @edac_idx: A unique numeric identifier to be assigned to the
  285. * 'edac_pci' structure.
  286. *
  287. * Return:
  288. * 0 Success
  289. * !0 Failure
  290. */
  291. int edac_pci_add_device(struct edac_pci_ctl_info *pci, int edac_idx)
  292. {
  293. debugf0("%s()\n", __func__);
  294. pci->pci_idx = edac_idx;
  295. pci->start_time = jiffies;
  296. mutex_lock(&edac_pci_ctls_mutex);
  297. if (add_edac_pci_to_global_list(pci))
  298. goto fail0;
  299. if (edac_pci_create_sysfs(pci)) {
  300. edac_pci_printk(pci, KERN_WARNING,
  301. "failed to create sysfs pci\n");
  302. goto fail1;
  303. }
  304. if (pci->edac_check != NULL) {
  305. pci->op_state = OP_RUNNING_POLL;
  306. edac_pci_workq_setup(pci, 1000);
  307. } else {
  308. pci->op_state = OP_RUNNING_INTERRUPT;
  309. }
  310. edac_pci_printk(pci, KERN_INFO,
  311. "Giving out device to module '%s' controller '%s':"
  312. " DEV '%s' (%s)\n",
  313. pci->mod_name,
  314. pci->ctl_name,
  315. edac_dev_name(pci), edac_op_state_to_string(pci->op_state));
  316. mutex_unlock(&edac_pci_ctls_mutex);
  317. return 0;
  318. /* error unwind stack */
  319. fail1:
  320. del_edac_pci_from_global_list(pci);
  321. fail0:
  322. mutex_unlock(&edac_pci_ctls_mutex);
  323. return 1;
  324. }
  325. EXPORT_SYMBOL_GPL(edac_pci_add_device);
  326. /*
  327. * edac_pci_del_device()
  328. * Remove sysfs entries for specified edac_pci structure and
  329. * then remove edac_pci structure from global list
  330. *
  331. * @dev:
  332. * Pointer to 'struct device' representing edac_pci structure
  333. * to remove
  334. *
  335. * Return:
  336. * Pointer to removed edac_pci structure,
  337. * or NULL if device not found
  338. */
  339. struct edac_pci_ctl_info *edac_pci_del_device(struct device *dev)
  340. {
  341. struct edac_pci_ctl_info *pci;
  342. debugf0("%s()\n", __func__);
  343. mutex_lock(&edac_pci_ctls_mutex);
  344. /* ensure the control struct is on the global list
  345. * if not, then leave
  346. */
  347. pci = find_edac_pci_by_dev(dev);
  348. if (pci == NULL) {
  349. mutex_unlock(&edac_pci_ctls_mutex);
  350. return NULL;
  351. }
  352. pci->op_state = OP_OFFLINE;
  353. del_edac_pci_from_global_list(pci);
  354. mutex_unlock(&edac_pci_ctls_mutex);
  355. /* stop the workq timer */
  356. edac_pci_workq_teardown(pci);
  357. edac_printk(KERN_INFO, EDAC_PCI,
  358. "Removed device %d for %s %s: DEV %s\n",
  359. pci->pci_idx, pci->mod_name, pci->ctl_name, edac_dev_name(pci));
  360. return pci;
  361. }
  362. EXPORT_SYMBOL_GPL(edac_pci_del_device);
  363. /*
  364. * edac_pci_generic_check
  365. *
  366. * a Generic parity check API
  367. */
  368. static void edac_pci_generic_check(struct edac_pci_ctl_info *pci)
  369. {
  370. debugf4("%s()\n", __func__);
  371. edac_pci_do_parity_check();
  372. }
  373. /* free running instance index counter */
  374. static int edac_pci_idx;
  375. #define EDAC_PCI_GENCTL_NAME "EDAC PCI controller"
  376. struct edac_pci_gen_data {
  377. int edac_idx;
  378. };
  379. /*
  380. * edac_pci_create_generic_ctl
  381. *
  382. * A generic constructor for a PCI parity polling device
  383. * Some systems have more than one domain of PCI busses.
  384. * For systems with one domain, then this API will
  385. * provide for a generic poller.
  386. *
  387. * This routine calls the edac_pci_alloc_ctl_info() for
  388. * the generic device, with default values
  389. */
  390. struct edac_pci_ctl_info *edac_pci_create_generic_ctl(struct device *dev,
  391. const char *mod_name)
  392. {
  393. struct edac_pci_ctl_info *pci;
  394. struct edac_pci_gen_data *pdata;
  395. pci = edac_pci_alloc_ctl_info(sizeof(*pdata), EDAC_PCI_GENCTL_NAME);
  396. if (!pci)
  397. return NULL;
  398. pdata = pci->pvt_info;
  399. pci->dev = dev;
  400. dev_set_drvdata(pci->dev, pci);
  401. pci->dev_name = pci_name(to_pci_dev(dev));
  402. pci->mod_name = mod_name;
  403. pci->ctl_name = EDAC_PCI_GENCTL_NAME;
  404. pci->edac_check = edac_pci_generic_check;
  405. pdata->edac_idx = edac_pci_idx++;
  406. if (edac_pci_add_device(pci, pdata->edac_idx) > 0) {
  407. debugf3("%s(): failed edac_pci_add_device()\n", __func__);
  408. edac_pci_free_ctl_info(pci);
  409. return NULL;
  410. }
  411. return pci;
  412. }
  413. EXPORT_SYMBOL_GPL(edac_pci_create_generic_ctl);
  414. /*
  415. * edac_pci_release_generic_ctl
  416. *
  417. * The release function of a generic EDAC PCI polling device
  418. */
  419. void edac_pci_release_generic_ctl(struct edac_pci_ctl_info *pci)
  420. {
  421. debugf0("%s() pci mod=%s\n", __func__, pci->mod_name);
  422. edac_pci_del_device(pci->dev);
  423. edac_pci_free_ctl_info(pci);
  424. }
  425. EXPORT_SYMBOL_GPL(edac_pci_release_generic_ctl);