edac_pci.c 11 KB

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