edac_pci.c 7.3 KB

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