edac_pci.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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(
  45. unsigned int sz_pvt,
  46. const char *edac_pci_name)
  47. {
  48. struct edac_pci_ctl_info *pci;
  49. void *pvt;
  50. unsigned int size;
  51. pci = (struct edac_pci_ctl_info *)0;
  52. pvt = edac_align_ptr(&pci[1], sz_pvt);
  53. size = ((unsigned long)pvt) + sz_pvt;
  54. if ((pci = kzalloc(size, GFP_KERNEL)) == NULL)
  55. return NULL;
  56. pvt = sz_pvt ? ((char *)pci) + ((unsigned long)pvt) : NULL;
  57. pci->pvt_info = pvt;
  58. pci->op_state = OP_ALLOC;
  59. snprintf(pci->name, strlen(edac_pci_name)+1, "%s", edac_pci_name);
  60. return pci;
  61. }
  62. EXPORT_SYMBOL_GPL(edac_pci_alloc_ctl_info);
  63. /*
  64. * edac_pci_free_ctl_info()
  65. * frees the memory allocated by edac_pci_alloc_ctl_info() function
  66. */
  67. void edac_pci_free_ctl_info(struct edac_pci_ctl_info *pci)
  68. {
  69. kfree(pci);
  70. }
  71. EXPORT_SYMBOL_GPL(edac_pci_free_ctl_info);
  72. /*
  73. * find_edac_pci_by_dev()
  74. * scans the edac_pci list for a specific 'struct device *'
  75. */
  76. static struct edac_pci_ctl_info * find_edac_pci_by_dev(struct device *dev)
  77. {
  78. struct edac_pci_ctl_info *pci;
  79. struct list_head *item;
  80. debugf3("%s()\n", __func__);
  81. list_for_each(item, &edac_pci_list) {
  82. pci = list_entry(item, struct edac_pci_ctl_info, link);
  83. if (pci->dev == dev)
  84. return pci;
  85. }
  86. return NULL;
  87. }
  88. /*
  89. * add_edac_pci_to_global_list
  90. * Before calling this function, caller must assign a unique value to
  91. * edac_dev->pci_idx.
  92. * Return:
  93. * 0 on success
  94. * 1 on failure
  95. */
  96. static int add_edac_pci_to_global_list(struct edac_pci_ctl_info *pci)
  97. {
  98. struct list_head *item, *insert_before;
  99. struct edac_pci_ctl_info *rover;
  100. insert_before = &edac_pci_list;
  101. /* Determine if already on the list */
  102. if (unlikely((rover = find_edac_pci_by_dev(pci->dev)) != NULL))
  103. goto fail0;
  104. /* Insert in ascending order by 'pci_idx', so find position */
  105. list_for_each(item, &edac_pci_list) {
  106. rover = list_entry(item, struct edac_pci_ctl_info, link);
  107. if (rover->pci_idx >= pci->pci_idx) {
  108. if (unlikely(rover->pci_idx == pci->pci_idx))
  109. goto fail1;
  110. insert_before = item;
  111. break;
  112. }
  113. }
  114. list_add_tail_rcu(&pci->link, insert_before);
  115. return 0;
  116. fail0:
  117. edac_printk(KERN_WARNING, EDAC_PCI,
  118. "%s (%s) %s %s already assigned %d\n",
  119. rover->dev->bus_id, dev_name(rover),
  120. rover->mod_name, rover->ctl_name, rover->pci_idx);
  121. return 1;
  122. fail1:
  123. edac_printk(KERN_WARNING, EDAC_PCI,
  124. "but in low-level driver: attempt to assign\n"
  125. "\tduplicate pci_idx %d in %s()\n", rover->pci_idx, __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. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
  179. static void edac_pci_workq_function(struct work_struct *work_req)
  180. {
  181. struct delayed_work *d_work = (struct delayed_work *)work_req;
  182. struct edac_pci_ctl_info *pci = to_edac_pci_ctl_work(d_work);
  183. #else
  184. static void edac_pci_workq_function(void *ptr)
  185. {
  186. struct edac_pci_ctl_info *pci = ptr;
  187. #endif
  188. edac_lock_pci_list();
  189. if ((pci->op_state == OP_RUNNING_POLL) &&
  190. (pci->edac_check != NULL) &&
  191. (edac_pci_get_check_errors()))
  192. pci->edac_check(pci);
  193. edac_unlock_pci_list();
  194. /* Reschedule */
  195. queue_delayed_work(edac_workqueue, &pci->work,
  196. msecs_to_jiffies(edac_pci_get_poll_msec()));
  197. }
  198. /*
  199. * edac_pci_workq_setup()
  200. * initialize a workq item for this edac_pci instance
  201. * passing in the new delay period in msec
  202. */
  203. static void edac_pci_workq_setup(struct edac_pci_ctl_info *pci,
  204. unsigned int msec)
  205. {
  206. debugf0("%s()\n", __func__);
  207. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
  208. INIT_DELAYED_WORK(&pci->work, edac_pci_workq_function);
  209. #else
  210. INIT_WORK(&pci->work, edac_pci_workq_function, pci);
  211. #endif
  212. queue_delayed_work(edac_workqueue, &pci->work,
  213. msecs_to_jiffies(edac_pci_get_poll_msec()));
  214. }
  215. /*
  216. * edac_pci_workq_teardown()
  217. * stop the workq processing on this edac_pci instance
  218. */
  219. static void edac_pci_workq_teardown(struct edac_pci_ctl_info *pci)
  220. {
  221. int status;
  222. status = cancel_delayed_work(&pci->work);
  223. if (status == 0)
  224. flush_workqueue(edac_workqueue);
  225. }
  226. /*
  227. * edac_pci_reset_delay_period
  228. */
  229. void edac_pci_reset_delay_period(struct edac_pci_ctl_info *pci,
  230. unsigned long value)
  231. {
  232. edac_lock_pci_list();
  233. edac_pci_workq_teardown(pci);
  234. edac_pci_workq_setup(pci, value);
  235. edac_unlock_pci_list();
  236. }
  237. EXPORT_SYMBOL_GPL(edac_pci_reset_delay_period);
  238. /*
  239. * edac_pci_add_device: Insert the 'edac_dev' structure into the
  240. * edac_pci global list and create sysfs entries associated with
  241. * edac_pci structure.
  242. * @pci: pointer to the edac_device structure to be added to the list
  243. * @edac_idx: A unique numeric identifier to be assigned to the
  244. * 'edac_pci' structure.
  245. *
  246. * Return:
  247. * 0 Success
  248. * !0 Failure
  249. */
  250. int edac_pci_add_device(struct edac_pci_ctl_info *pci, int edac_idx)
  251. {
  252. debugf0("%s()\n", __func__);
  253. pci->pci_idx = edac_idx;
  254. edac_lock_pci_list();
  255. if (add_edac_pci_to_global_list(pci))
  256. goto fail0;
  257. pci->start_time = jiffies;
  258. if (edac_pci_create_sysfs(pci)) {
  259. edac_pci_printk(pci, KERN_WARNING,
  260. "failed to create sysfs pci\n");
  261. goto fail1;
  262. }
  263. if (pci->edac_check != NULL) {
  264. pci->op_state = OP_RUNNING_POLL;
  265. edac_pci_workq_setup(pci, 1000);
  266. } else {
  267. pci->op_state = OP_RUNNING_INTERRUPT;
  268. }
  269. edac_pci_printk(pci, KERN_INFO,
  270. "Giving out device to module '%s' controller '%s':"
  271. " DEV '%s' (%s)\n",
  272. pci->mod_name,
  273. pci->ctl_name,
  274. dev_name(pci),
  275. edac_op_state_toString(pci->op_state));
  276. edac_unlock_pci_list();
  277. return 0;
  278. fail1:
  279. del_edac_pci_from_global_list(pci);
  280. fail0:
  281. edac_unlock_pci_list();
  282. return 1;
  283. }
  284. EXPORT_SYMBOL_GPL(edac_pci_add_device);
  285. /*
  286. * edac_pci_del_device()
  287. * Remove sysfs entries for specified edac_pci structure and
  288. * then remove edac_pci structure from global list
  289. *
  290. * @dev:
  291. * Pointer to 'struct device' representing edac_pci structure
  292. * to remove
  293. *
  294. * Return:
  295. * Pointer to removed edac_pci structure,
  296. * or NULL if device not found
  297. */
  298. struct edac_pci_ctl_info * edac_pci_del_device(struct device *dev)
  299. {
  300. struct edac_pci_ctl_info *pci;
  301. debugf0("%s()\n", __func__);
  302. edac_lock_pci_list();
  303. if ((pci = find_edac_pci_by_dev(dev)) == NULL) {
  304. edac_unlock_pci_list();
  305. return NULL;
  306. }
  307. pci->op_state = OP_OFFLINE;
  308. edac_pci_workq_teardown(pci);
  309. edac_pci_remove_sysfs(pci);
  310. del_edac_pci_from_global_list(pci);
  311. edac_unlock_pci_list();
  312. edac_printk(KERN_INFO, EDAC_PCI,
  313. "Removed device %d for %s %s: DEV %s\n",
  314. pci->pci_idx,
  315. pci->mod_name,
  316. pci->ctl_name,
  317. dev_name(pci));
  318. return pci;
  319. }
  320. EXPORT_SYMBOL_GPL(edac_pci_del_device);
  321. void edac_pci_generic_check(struct edac_pci_ctl_info *pci)
  322. {
  323. edac_pci_do_parity_check();
  324. }
  325. static int edac_pci_idx = 0;
  326. #define EDAC_PCI_GENCTL_NAME "EDAC PCI controller"
  327. struct edac_pci_gen_data {
  328. int edac_idx;
  329. };
  330. struct edac_pci_ctl_info *
  331. edac_pci_create_generic_ctl(struct device *dev, const char *mod_name)
  332. {
  333. struct edac_pci_ctl_info *pci;
  334. struct edac_pci_gen_data *pdata;
  335. pci = edac_pci_alloc_ctl_info(sizeof(*pdata), EDAC_PCI_GENCTL_NAME);
  336. if (!pci)
  337. return NULL;
  338. pdata = pci->pvt_info;
  339. pci->dev = dev;
  340. dev_set_drvdata(pci->dev, pci);
  341. pci->dev_name = pci_name(to_pci_dev(dev));
  342. pci->mod_name = mod_name;
  343. pci->ctl_name = EDAC_PCI_GENCTL_NAME;
  344. pci->edac_check = edac_pci_generic_check;
  345. pdata->edac_idx = edac_pci_idx++;
  346. if (edac_pci_add_device(pci, pdata->edac_idx) > 0) {
  347. debugf3("%s(): failed edac_pci_add_device()\n", __func__);
  348. edac_pci_free_ctl_info(pci);
  349. return NULL;
  350. }
  351. return pci;
  352. }
  353. EXPORT_SYMBOL_GPL(edac_pci_create_generic_ctl);
  354. void edac_pci_release_generic_ctl(struct edac_pci_ctl_info *pci)
  355. {
  356. edac_pci_del_device(pci->dev);
  357. edac_pci_free_ctl_info(pci);
  358. }
  359. EXPORT_SYMBOL_GPL(edac_pci_release_generic_ctl);