edac_pci.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. static inline void edac_lock_pci_list(void)
  32. {
  33. mutex_lock(&edac_pci_ctls_mutex);
  34. }
  35. static inline void edac_unlock_pci_list(void)
  36. {
  37. mutex_unlock(&edac_pci_ctls_mutex);
  38. }
  39. /*
  40. * The alloc() and free() functions for the 'edac_pci' control info
  41. * structure. The chip driver will allocate one of these for each
  42. * edac_pci it is going to control/register with the EDAC CORE.
  43. */
  44. struct edac_pci_ctl_info *edac_pci_alloc_ctl_info(unsigned int sz_pvt,
  45. const char *edac_pci_name)
  46. {
  47. struct edac_pci_ctl_info *pci;
  48. void *pvt;
  49. unsigned int size;
  50. pci = (struct edac_pci_ctl_info *)0;
  51. pvt = edac_align_ptr(&pci[1], sz_pvt);
  52. size = ((unsigned long)pvt) + sz_pvt;
  53. if ((pci = kzalloc(size, GFP_KERNEL)) == NULL)
  54. return NULL;
  55. pvt = sz_pvt ? ((char *)pci) + ((unsigned long)pvt) : NULL;
  56. pci->pvt_info = pvt;
  57. pci->op_state = OP_ALLOC;
  58. snprintf(pci->name, strlen(edac_pci_name) + 1, "%s", edac_pci_name);
  59. return pci;
  60. }
  61. EXPORT_SYMBOL_GPL(edac_pci_alloc_ctl_info);
  62. /*
  63. * edac_pci_free_ctl_info()
  64. * frees the memory allocated by edac_pci_alloc_ctl_info() function
  65. */
  66. void edac_pci_free_ctl_info(struct edac_pci_ctl_info *pci)
  67. {
  68. kfree(pci);
  69. }
  70. EXPORT_SYMBOL_GPL(edac_pci_free_ctl_info);
  71. /*
  72. * find_edac_pci_by_dev()
  73. * scans the edac_pci list for a specific 'struct device *'
  74. */
  75. static struct edac_pci_ctl_info *find_edac_pci_by_dev(struct device *dev)
  76. {
  77. struct edac_pci_ctl_info *pci;
  78. struct list_head *item;
  79. debugf3("%s()\n", __func__);
  80. list_for_each(item, &edac_pci_list) {
  81. pci = list_entry(item, struct edac_pci_ctl_info, link);
  82. if (pci->dev == dev)
  83. return pci;
  84. }
  85. return NULL;
  86. }
  87. /*
  88. * add_edac_pci_to_global_list
  89. * Before calling this function, caller must assign a unique value to
  90. * edac_dev->pci_idx.
  91. * Return:
  92. * 0 on success
  93. * 1 on failure
  94. */
  95. static int add_edac_pci_to_global_list(struct edac_pci_ctl_info *pci)
  96. {
  97. struct list_head *item, *insert_before;
  98. struct edac_pci_ctl_info *rover;
  99. insert_before = &edac_pci_list;
  100. /* Determine if already on the list */
  101. if (unlikely((rover = find_edac_pci_by_dev(pci->dev)) != NULL))
  102. goto fail0;
  103. /* Insert in ascending order by 'pci_idx', so find position */
  104. list_for_each(item, &edac_pci_list) {
  105. rover = list_entry(item, struct edac_pci_ctl_info, link);
  106. if (rover->pci_idx >= pci->pci_idx) {
  107. if (unlikely(rover->pci_idx == pci->pci_idx))
  108. goto fail1;
  109. insert_before = item;
  110. break;
  111. }
  112. }
  113. list_add_tail_rcu(&pci->link, insert_before);
  114. return 0;
  115. fail0:
  116. edac_printk(KERN_WARNING, EDAC_PCI,
  117. "%s (%s) %s %s already assigned %d\n",
  118. rover->dev->bus_id, dev_name(rover),
  119. rover->mod_name, rover->ctl_name, rover->pci_idx);
  120. return 1;
  121. fail1:
  122. edac_printk(KERN_WARNING, EDAC_PCI,
  123. "but in low-level driver: attempt to assign\n"
  124. "\tduplicate pci_idx %d in %s()\n", rover->pci_idx,
  125. __func__);
  126. return 1;
  127. }
  128. /*
  129. * complete_edac_pci_list_del
  130. */
  131. static void complete_edac_pci_list_del(struct rcu_head *head)
  132. {
  133. struct edac_pci_ctl_info *pci;
  134. pci = container_of(head, struct edac_pci_ctl_info, rcu);
  135. INIT_LIST_HEAD(&pci->link);
  136. complete(&pci->complete);
  137. }
  138. /*
  139. * del_edac_pci_from_global_list
  140. */
  141. static void del_edac_pci_from_global_list(struct edac_pci_ctl_info *pci)
  142. {
  143. list_del_rcu(&pci->link);
  144. init_completion(&pci->complete);
  145. call_rcu(&pci->rcu, complete_edac_pci_list_del);
  146. wait_for_completion(&pci->complete);
  147. }
  148. /*
  149. * edac_pci_find()
  150. * Search for an edac_pci_ctl_info structure whose index is 'idx'
  151. *
  152. * If found, return a pointer to the structure
  153. * Else return NULL.
  154. *
  155. * Caller must hold pci_ctls_mutex.
  156. */
  157. struct edac_pci_ctl_info *edac_pci_find(int idx)
  158. {
  159. struct list_head *item;
  160. struct edac_pci_ctl_info *pci;
  161. /* Iterage over list, looking for exact match of ID */
  162. list_for_each(item, &edac_pci_list) {
  163. pci = list_entry(item, struct edac_pci_ctl_info, link);
  164. if (pci->pci_idx >= idx) {
  165. if (pci->pci_idx == idx)
  166. return pci;
  167. /* not on list, so terminate early */
  168. break;
  169. }
  170. }
  171. return NULL;
  172. }
  173. EXPORT_SYMBOL_GPL(edac_pci_find);
  174. /*
  175. * edac_pci_workq_function()
  176. * performs the operation scheduled by a workq request
  177. */
  178. static void edac_pci_workq_function(struct work_struct *work_req)
  179. {
  180. struct delayed_work *d_work = (struct delayed_work *)work_req;
  181. struct edac_pci_ctl_info *pci = to_edac_pci_ctl_work(d_work);
  182. edac_lock_pci_list();
  183. if ((pci->op_state == OP_RUNNING_POLL) &&
  184. (pci->edac_check != NULL) && (edac_pci_get_check_errors()))
  185. pci->edac_check(pci);
  186. edac_unlock_pci_list();
  187. /* Reschedule */
  188. queue_delayed_work(edac_workqueue, &pci->work,
  189. msecs_to_jiffies(edac_pci_get_poll_msec()));
  190. }
  191. /*
  192. * edac_pci_workq_setup()
  193. * initialize a workq item for this edac_pci instance
  194. * passing in the new delay period in msec
  195. */
  196. static void edac_pci_workq_setup(struct edac_pci_ctl_info *pci,
  197. unsigned int msec)
  198. {
  199. debugf0("%s()\n", __func__);
  200. INIT_DELAYED_WORK(&pci->work, edac_pci_workq_function);
  201. queue_delayed_work(edac_workqueue, &pci->work,
  202. msecs_to_jiffies(edac_pci_get_poll_msec()));
  203. }
  204. /*
  205. * edac_pci_workq_teardown()
  206. * stop the workq processing on this edac_pci instance
  207. */
  208. static void edac_pci_workq_teardown(struct edac_pci_ctl_info *pci)
  209. {
  210. int status;
  211. status = cancel_delayed_work(&pci->work);
  212. if (status == 0)
  213. flush_workqueue(edac_workqueue);
  214. }
  215. /*
  216. * edac_pci_reset_delay_period
  217. */
  218. void edac_pci_reset_delay_period(struct edac_pci_ctl_info *pci,
  219. unsigned long value)
  220. {
  221. edac_lock_pci_list();
  222. edac_pci_workq_teardown(pci);
  223. edac_pci_workq_setup(pci, value);
  224. edac_unlock_pci_list();
  225. }
  226. EXPORT_SYMBOL_GPL(edac_pci_reset_delay_period);
  227. /*
  228. * edac_pci_add_device: Insert the 'edac_dev' structure into the
  229. * edac_pci global list and create sysfs entries associated with
  230. * edac_pci structure.
  231. * @pci: pointer to the edac_device structure to be added to the list
  232. * @edac_idx: A unique numeric identifier to be assigned to the
  233. * 'edac_pci' structure.
  234. *
  235. * Return:
  236. * 0 Success
  237. * !0 Failure
  238. */
  239. int edac_pci_add_device(struct edac_pci_ctl_info *pci, int edac_idx)
  240. {
  241. debugf0("%s()\n", __func__);
  242. pci->pci_idx = edac_idx;
  243. edac_lock_pci_list();
  244. if (add_edac_pci_to_global_list(pci))
  245. goto fail0;
  246. pci->start_time = jiffies;
  247. if (edac_pci_create_sysfs(pci)) {
  248. edac_pci_printk(pci, KERN_WARNING,
  249. "failed to create sysfs pci\n");
  250. goto fail1;
  251. }
  252. if (pci->edac_check != NULL) {
  253. pci->op_state = OP_RUNNING_POLL;
  254. edac_pci_workq_setup(pci, 1000);
  255. } else {
  256. pci->op_state = OP_RUNNING_INTERRUPT;
  257. }
  258. edac_pci_printk(pci, KERN_INFO,
  259. "Giving out device to module '%s' controller '%s':"
  260. " DEV '%s' (%s)\n",
  261. pci->mod_name,
  262. pci->ctl_name,
  263. dev_name(pci), edac_op_state_toString(pci->op_state));
  264. edac_unlock_pci_list();
  265. return 0;
  266. fail1:
  267. del_edac_pci_from_global_list(pci);
  268. fail0:
  269. edac_unlock_pci_list();
  270. return 1;
  271. }
  272. EXPORT_SYMBOL_GPL(edac_pci_add_device);
  273. /*
  274. * edac_pci_del_device()
  275. * Remove sysfs entries for specified edac_pci structure and
  276. * then remove edac_pci structure from global list
  277. *
  278. * @dev:
  279. * Pointer to 'struct device' representing edac_pci structure
  280. * to remove
  281. *
  282. * Return:
  283. * Pointer to removed edac_pci structure,
  284. * or NULL if device not found
  285. */
  286. struct edac_pci_ctl_info *edac_pci_del_device(struct device *dev)
  287. {
  288. struct edac_pci_ctl_info *pci;
  289. debugf0("%s()\n", __func__);
  290. edac_lock_pci_list();
  291. if ((pci = find_edac_pci_by_dev(dev)) == NULL) {
  292. edac_unlock_pci_list();
  293. return NULL;
  294. }
  295. pci->op_state = OP_OFFLINE;
  296. edac_pci_workq_teardown(pci);
  297. edac_pci_remove_sysfs(pci);
  298. del_edac_pci_from_global_list(pci);
  299. edac_unlock_pci_list();
  300. edac_printk(KERN_INFO, EDAC_PCI,
  301. "Removed device %d for %s %s: DEV %s\n",
  302. pci->pci_idx, pci->mod_name, pci->ctl_name, dev_name(pci));
  303. return pci;
  304. }
  305. EXPORT_SYMBOL_GPL(edac_pci_del_device);
  306. void edac_pci_generic_check(struct edac_pci_ctl_info *pci)
  307. {
  308. edac_pci_do_parity_check();
  309. }
  310. static int edac_pci_idx = 0;
  311. #define EDAC_PCI_GENCTL_NAME "EDAC PCI controller"
  312. struct edac_pci_gen_data {
  313. int edac_idx;
  314. };
  315. struct edac_pci_ctl_info *edac_pci_create_generic_ctl(struct device *dev,
  316. const char *mod_name)
  317. {
  318. struct edac_pci_ctl_info *pci;
  319. struct edac_pci_gen_data *pdata;
  320. pci = edac_pci_alloc_ctl_info(sizeof(*pdata), EDAC_PCI_GENCTL_NAME);
  321. if (!pci)
  322. return NULL;
  323. pdata = pci->pvt_info;
  324. pci->dev = dev;
  325. dev_set_drvdata(pci->dev, pci);
  326. pci->dev_name = pci_name(to_pci_dev(dev));
  327. pci->mod_name = mod_name;
  328. pci->ctl_name = EDAC_PCI_GENCTL_NAME;
  329. pci->edac_check = edac_pci_generic_check;
  330. pdata->edac_idx = edac_pci_idx++;
  331. if (edac_pci_add_device(pci, pdata->edac_idx) > 0) {
  332. debugf3("%s(): failed edac_pci_add_device()\n", __func__);
  333. edac_pci_free_ctl_info(pci);
  334. return NULL;
  335. }
  336. return pci;
  337. }
  338. EXPORT_SYMBOL_GPL(edac_pci_create_generic_ctl);
  339. void edac_pci_release_generic_ctl(struct edac_pci_ctl_info *pci)
  340. {
  341. edac_pci_del_device(pci->dev);
  342. edac_pci_free_ctl_info(pci);
  343. }
  344. EXPORT_SYMBOL_GPL(edac_pci_release_generic_ctl);