edac_pci.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 <asm/page.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/ctype.h>
  15. #include <linux/highmem.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/smp.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/sysctl.h>
  22. #include <linux/timer.h>
  23. #include "edac_pci.h"
  24. #include "edac_module.h"
  25. static DEFINE_MUTEX(edac_pci_ctls_mutex);
  26. static LIST_HEAD(edac_pci_list);
  27. static atomic_t pci_indexes = ATOMIC_INIT(0);
  28. struct edac_pci_ctl_info *edac_pci_alloc_ctl_info(unsigned int sz_pvt,
  29. const char *edac_pci_name)
  30. {
  31. struct edac_pci_ctl_info *pci;
  32. void *p = NULL, *pvt;
  33. unsigned int size;
  34. edac_dbg(1, "\n");
  35. pci = edac_align_ptr(&p, sizeof(*pci), 1);
  36. pvt = edac_align_ptr(&p, 1, sz_pvt);
  37. size = ((unsigned long)pvt) + sz_pvt;
  38. /* Alloc the needed control struct memory */
  39. pci = kzalloc(size, GFP_KERNEL);
  40. if (pci == NULL)
  41. return NULL;
  42. /* Now much private space */
  43. pvt = sz_pvt ? ((char *)pci) + ((unsigned long)pvt) : NULL;
  44. pci->pvt_info = pvt;
  45. pci->op_state = OP_ALLOC;
  46. snprintf(pci->name, strlen(edac_pci_name) + 1, "%s", edac_pci_name);
  47. return pci;
  48. }
  49. EXPORT_SYMBOL_GPL(edac_pci_alloc_ctl_info);
  50. void edac_pci_free_ctl_info(struct edac_pci_ctl_info *pci)
  51. {
  52. edac_dbg(1, "\n");
  53. edac_pci_remove_sysfs(pci);
  54. }
  55. EXPORT_SYMBOL_GPL(edac_pci_free_ctl_info);
  56. /*
  57. * find_edac_pci_by_dev()
  58. * scans the edac_pci list for a specific 'struct device *'
  59. *
  60. * return NULL if not found, or return control struct pointer
  61. */
  62. static struct edac_pci_ctl_info *find_edac_pci_by_dev(struct device *dev)
  63. {
  64. struct edac_pci_ctl_info *pci;
  65. struct list_head *item;
  66. edac_dbg(1, "\n");
  67. list_for_each(item, &edac_pci_list) {
  68. pci = list_entry(item, struct edac_pci_ctl_info, link);
  69. if (pci->dev == dev)
  70. return pci;
  71. }
  72. return NULL;
  73. }
  74. /*
  75. * add_edac_pci_to_global_list
  76. * Before calling this function, caller must assign a unique value to
  77. * edac_dev->pci_idx.
  78. * Return:
  79. * 0 on success
  80. * 1 on failure
  81. */
  82. static int add_edac_pci_to_global_list(struct edac_pci_ctl_info *pci)
  83. {
  84. struct list_head *item, *insert_before;
  85. struct edac_pci_ctl_info *rover;
  86. edac_dbg(1, "\n");
  87. insert_before = &edac_pci_list;
  88. /* Determine if already on the list */
  89. rover = find_edac_pci_by_dev(pci->dev);
  90. if (unlikely(rover != NULL))
  91. goto fail0;
  92. /* Insert in ascending order by 'pci_idx', so find position */
  93. list_for_each(item, &edac_pci_list) {
  94. rover = list_entry(item, struct edac_pci_ctl_info, link);
  95. if (rover->pci_idx >= pci->pci_idx) {
  96. if (unlikely(rover->pci_idx == pci->pci_idx))
  97. goto fail1;
  98. insert_before = item;
  99. break;
  100. }
  101. }
  102. list_add_tail_rcu(&pci->link, insert_before);
  103. return 0;
  104. fail0:
  105. edac_printk(KERN_WARNING, EDAC_PCI,
  106. "%s (%s) %s %s already assigned %d\n",
  107. dev_name(rover->dev), edac_dev_name(rover),
  108. rover->mod_name, rover->ctl_name, rover->pci_idx);
  109. return 1;
  110. fail1:
  111. edac_printk(KERN_WARNING, EDAC_PCI,
  112. "but in low-level driver: attempt to assign\n"
  113. "\tduplicate pci_idx %d in %s()\n", rover->pci_idx,
  114. __func__);
  115. return 1;
  116. }
  117. /*
  118. * del_edac_pci_from_global_list
  119. *
  120. * remove the PCI control struct from the global list
  121. */
  122. static void del_edac_pci_from_global_list(struct edac_pci_ctl_info *pci)
  123. {
  124. list_del_rcu(&pci->link);
  125. /* these are for safe removal of devices from global list while
  126. * NMI handlers may be traversing list
  127. */
  128. synchronize_rcu();
  129. INIT_LIST_HEAD(&pci->link);
  130. }
  131. /*
  132. * edac_pci_workq_function()
  133. *
  134. * periodic function that performs the operation
  135. * scheduled by a workq request, for a given PCI control struct
  136. */
  137. static void edac_pci_workq_function(struct work_struct *work_req)
  138. {
  139. struct delayed_work *d_work = to_delayed_work(work_req);
  140. struct edac_pci_ctl_info *pci = to_edac_pci_ctl_work(d_work);
  141. int msec;
  142. unsigned long delay;
  143. edac_dbg(3, "checking\n");
  144. mutex_lock(&edac_pci_ctls_mutex);
  145. if (pci->op_state != OP_RUNNING_POLL) {
  146. mutex_unlock(&edac_pci_ctls_mutex);
  147. return;
  148. }
  149. if (edac_pci_get_check_errors())
  150. pci->edac_check(pci);
  151. /* if we are on a one second period, then use round */
  152. msec = edac_pci_get_poll_msec();
  153. if (msec == 1000)
  154. delay = round_jiffies_relative(msecs_to_jiffies(msec));
  155. else
  156. delay = msecs_to_jiffies(msec);
  157. edac_queue_work(&pci->work, delay);
  158. mutex_unlock(&edac_pci_ctls_mutex);
  159. }
  160. int edac_pci_alloc_index(void)
  161. {
  162. return atomic_inc_return(&pci_indexes) - 1;
  163. }
  164. EXPORT_SYMBOL_GPL(edac_pci_alloc_index);
  165. int edac_pci_add_device(struct edac_pci_ctl_info *pci, int edac_idx)
  166. {
  167. edac_dbg(0, "\n");
  168. pci->pci_idx = edac_idx;
  169. pci->start_time = jiffies;
  170. mutex_lock(&edac_pci_ctls_mutex);
  171. if (add_edac_pci_to_global_list(pci))
  172. goto fail0;
  173. if (edac_pci_create_sysfs(pci)) {
  174. edac_pci_printk(pci, KERN_WARNING,
  175. "failed to create sysfs pci\n");
  176. goto fail1;
  177. }
  178. if (pci->edac_check) {
  179. pci->op_state = OP_RUNNING_POLL;
  180. INIT_DELAYED_WORK(&pci->work, edac_pci_workq_function);
  181. edac_queue_work(&pci->work, msecs_to_jiffies(edac_pci_get_poll_msec()));
  182. } else {
  183. pci->op_state = OP_RUNNING_INTERRUPT;
  184. }
  185. edac_pci_printk(pci, KERN_INFO,
  186. "Giving out device to module %s controller %s: DEV %s (%s)\n",
  187. pci->mod_name, pci->ctl_name, pci->dev_name,
  188. edac_op_state_to_string(pci->op_state));
  189. mutex_unlock(&edac_pci_ctls_mutex);
  190. return 0;
  191. /* error unwind stack */
  192. fail1:
  193. del_edac_pci_from_global_list(pci);
  194. fail0:
  195. mutex_unlock(&edac_pci_ctls_mutex);
  196. return 1;
  197. }
  198. EXPORT_SYMBOL_GPL(edac_pci_add_device);
  199. struct edac_pci_ctl_info *edac_pci_del_device(struct device *dev)
  200. {
  201. struct edac_pci_ctl_info *pci;
  202. edac_dbg(0, "\n");
  203. mutex_lock(&edac_pci_ctls_mutex);
  204. /* ensure the control struct is on the global list
  205. * if not, then leave
  206. */
  207. pci = find_edac_pci_by_dev(dev);
  208. if (pci == NULL) {
  209. mutex_unlock(&edac_pci_ctls_mutex);
  210. return NULL;
  211. }
  212. pci->op_state = OP_OFFLINE;
  213. del_edac_pci_from_global_list(pci);
  214. mutex_unlock(&edac_pci_ctls_mutex);
  215. if (pci->edac_check)
  216. edac_stop_work(&pci->work);
  217. edac_printk(KERN_INFO, EDAC_PCI,
  218. "Removed device %d for %s %s: DEV %s\n",
  219. pci->pci_idx, pci->mod_name, pci->ctl_name, edac_dev_name(pci));
  220. return pci;
  221. }
  222. EXPORT_SYMBOL_GPL(edac_pci_del_device);
  223. /*
  224. * edac_pci_generic_check
  225. *
  226. * a Generic parity check API
  227. */
  228. static void edac_pci_generic_check(struct edac_pci_ctl_info *pci)
  229. {
  230. edac_dbg(4, "\n");
  231. edac_pci_do_parity_check();
  232. }
  233. /* free running instance index counter */
  234. static int edac_pci_idx;
  235. #define EDAC_PCI_GENCTL_NAME "EDAC PCI controller"
  236. struct edac_pci_gen_data {
  237. int edac_idx;
  238. };
  239. struct edac_pci_ctl_info *edac_pci_create_generic_ctl(struct device *dev,
  240. const char *mod_name)
  241. {
  242. struct edac_pci_ctl_info *pci;
  243. struct edac_pci_gen_data *pdata;
  244. pci = edac_pci_alloc_ctl_info(sizeof(*pdata), EDAC_PCI_GENCTL_NAME);
  245. if (!pci)
  246. return NULL;
  247. pdata = pci->pvt_info;
  248. pci->dev = dev;
  249. dev_set_drvdata(pci->dev, pci);
  250. pci->dev_name = pci_name(to_pci_dev(dev));
  251. pci->mod_name = mod_name;
  252. pci->ctl_name = EDAC_PCI_GENCTL_NAME;
  253. if (edac_op_state == EDAC_OPSTATE_POLL)
  254. pci->edac_check = edac_pci_generic_check;
  255. pdata->edac_idx = edac_pci_idx++;
  256. if (edac_pci_add_device(pci, pdata->edac_idx) > 0) {
  257. edac_dbg(3, "failed edac_pci_add_device()\n");
  258. edac_pci_free_ctl_info(pci);
  259. return NULL;
  260. }
  261. return pci;
  262. }
  263. EXPORT_SYMBOL_GPL(edac_pci_create_generic_ctl);
  264. void edac_pci_release_generic_ctl(struct edac_pci_ctl_info *pci)
  265. {
  266. edac_dbg(0, "pci mod=%s\n", pci->mod_name);
  267. edac_pci_del_device(pci->dev);
  268. edac_pci_free_ctl_info(pci);
  269. }
  270. EXPORT_SYMBOL_GPL(edac_pci_release_generic_ctl);