edac_pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 LIST_HEAD(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 information, 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, edac_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. #if 0
  162. /* Older code, but might use in the future */
  163. /*
  164. * edac_pci_find()
  165. * Search for an edac_pci_ctl_info structure whose index is 'idx'
  166. *
  167. * If found, return a pointer to the structure
  168. * Else return NULL.
  169. *
  170. * Caller must hold pci_ctls_mutex.
  171. */
  172. struct edac_pci_ctl_info *edac_pci_find(int idx)
  173. {
  174. struct list_head *item;
  175. struct edac_pci_ctl_info *pci;
  176. /* Iterage over list, looking for exact match of ID */
  177. list_for_each(item, &edac_pci_list) {
  178. pci = list_entry(item, struct edac_pci_ctl_info, link);
  179. if (pci->pci_idx >= idx) {
  180. if (pci->pci_idx == idx)
  181. return pci;
  182. /* not on list, so terminate early */
  183. break;
  184. }
  185. }
  186. return NULL;
  187. }
  188. EXPORT_SYMBOL_GPL(edac_pci_find);
  189. #endif
  190. /*
  191. * edac_pci_workq_function()
  192. *
  193. * periodic function that performs the operation
  194. * scheduled by a workq request, for a given PCI control struct
  195. */
  196. static void edac_pci_workq_function(struct work_struct *work_req)
  197. {
  198. struct delayed_work *d_work = (struct delayed_work *)work_req;
  199. struct edac_pci_ctl_info *pci = to_edac_pci_ctl_work(d_work);
  200. int msec;
  201. unsigned long delay;
  202. debugf3("%s() checking\n", __func__);
  203. mutex_lock(&edac_pci_ctls_mutex);
  204. if (pci->op_state == OP_RUNNING_POLL) {
  205. /* we might be in POLL mode, but there may NOT be a poll func
  206. */
  207. if ((pci->edac_check != NULL) && edac_pci_get_check_errors())
  208. pci->edac_check(pci);
  209. /* if we are on a one second period, then use round */
  210. msec = edac_pci_get_poll_msec();
  211. if (msec == 1000)
  212. delay = round_jiffies_relative(msecs_to_jiffies(msec));
  213. else
  214. delay = msecs_to_jiffies(msec);
  215. /* Reschedule only if we are in POLL mode */
  216. queue_delayed_work(edac_workqueue, &pci->work, delay);
  217. }
  218. mutex_unlock(&edac_pci_ctls_mutex);
  219. }
  220. /*
  221. * edac_pci_workq_setup()
  222. * initialize a workq item for this edac_pci instance
  223. * passing in the new delay period in msec
  224. *
  225. * locking model:
  226. * called when 'edac_pci_ctls_mutex' is locked
  227. */
  228. static void edac_pci_workq_setup(struct edac_pci_ctl_info *pci,
  229. unsigned int msec)
  230. {
  231. debugf0("%s()\n", __func__);
  232. INIT_DELAYED_WORK(&pci->work, edac_pci_workq_function);
  233. queue_delayed_work(edac_workqueue, &pci->work,
  234. msecs_to_jiffies(edac_pci_get_poll_msec()));
  235. }
  236. /*
  237. * edac_pci_workq_teardown()
  238. * stop the workq processing on this edac_pci instance
  239. */
  240. static void edac_pci_workq_teardown(struct edac_pci_ctl_info *pci)
  241. {
  242. int status;
  243. debugf0("%s()\n", __func__);
  244. status = cancel_delayed_work(&pci->work);
  245. if (status == 0)
  246. flush_workqueue(edac_workqueue);
  247. }
  248. /*
  249. * edac_pci_reset_delay_period
  250. *
  251. * called with a new period value for the workq period
  252. * a) stop current workq timer
  253. * b) restart workq timer with new value
  254. */
  255. void edac_pci_reset_delay_period(struct edac_pci_ctl_info *pci,
  256. unsigned long value)
  257. {
  258. debugf0("%s()\n", __func__);
  259. edac_pci_workq_teardown(pci);
  260. /* need to lock for the setup */
  261. mutex_lock(&edac_pci_ctls_mutex);
  262. edac_pci_workq_setup(pci, value);
  263. mutex_unlock(&edac_pci_ctls_mutex);
  264. }
  265. EXPORT_SYMBOL_GPL(edac_pci_reset_delay_period);
  266. /*
  267. * edac_pci_add_device: Insert the 'edac_dev' structure into the
  268. * edac_pci global list and create sysfs entries associated with
  269. * edac_pci structure.
  270. * @pci: pointer to the edac_device structure to be added to the list
  271. * @edac_idx: A unique numeric identifier to be assigned to the
  272. * 'edac_pci' structure.
  273. *
  274. * Return:
  275. * 0 Success
  276. * !0 Failure
  277. */
  278. int edac_pci_add_device(struct edac_pci_ctl_info *pci, int edac_idx)
  279. {
  280. debugf0("%s()\n", __func__);
  281. pci->pci_idx = edac_idx;
  282. pci->start_time = jiffies;
  283. mutex_lock(&edac_pci_ctls_mutex);
  284. if (add_edac_pci_to_global_list(pci))
  285. goto fail0;
  286. if (edac_pci_create_sysfs(pci)) {
  287. edac_pci_printk(pci, KERN_WARNING,
  288. "failed to create sysfs pci\n");
  289. goto fail1;
  290. }
  291. if (pci->edac_check != NULL) {
  292. pci->op_state = OP_RUNNING_POLL;
  293. edac_pci_workq_setup(pci, 1000);
  294. } else {
  295. pci->op_state = OP_RUNNING_INTERRUPT;
  296. }
  297. edac_pci_printk(pci, KERN_INFO,
  298. "Giving out device to module '%s' controller '%s':"
  299. " DEV '%s' (%s)\n",
  300. pci->mod_name,
  301. pci->ctl_name,
  302. edac_dev_name(pci), edac_op_state_to_string(pci->op_state));
  303. mutex_unlock(&edac_pci_ctls_mutex);
  304. return 0;
  305. /* error unwind stack */
  306. fail1:
  307. del_edac_pci_from_global_list(pci);
  308. fail0:
  309. mutex_unlock(&edac_pci_ctls_mutex);
  310. return 1;
  311. }
  312. EXPORT_SYMBOL_GPL(edac_pci_add_device);
  313. /*
  314. * edac_pci_del_device()
  315. * Remove sysfs entries for specified edac_pci structure and
  316. * then remove edac_pci structure from global list
  317. *
  318. * @dev:
  319. * Pointer to 'struct device' representing edac_pci structure
  320. * to remove
  321. *
  322. * Return:
  323. * Pointer to removed edac_pci structure,
  324. * or NULL if device not found
  325. */
  326. struct edac_pci_ctl_info *edac_pci_del_device(struct device *dev)
  327. {
  328. struct edac_pci_ctl_info *pci;
  329. debugf0("%s()\n", __func__);
  330. mutex_lock(&edac_pci_ctls_mutex);
  331. /* ensure the control struct is on the global list
  332. * if not, then leave
  333. */
  334. pci = find_edac_pci_by_dev(dev);
  335. if (pci == NULL) {
  336. mutex_unlock(&edac_pci_ctls_mutex);
  337. return NULL;
  338. }
  339. pci->op_state = OP_OFFLINE;
  340. del_edac_pci_from_global_list(pci);
  341. mutex_unlock(&edac_pci_ctls_mutex);
  342. /* stop the workq timer */
  343. edac_pci_workq_teardown(pci);
  344. edac_printk(KERN_INFO, EDAC_PCI,
  345. "Removed device %d for %s %s: DEV %s\n",
  346. pci->pci_idx, pci->mod_name, pci->ctl_name, edac_dev_name(pci));
  347. return pci;
  348. }
  349. EXPORT_SYMBOL_GPL(edac_pci_del_device);
  350. /*
  351. * edac_pci_generic_check
  352. *
  353. * a Generic parity check API
  354. */
  355. static void edac_pci_generic_check(struct edac_pci_ctl_info *pci)
  356. {
  357. debugf4("%s()\n", __func__);
  358. edac_pci_do_parity_check();
  359. }
  360. /* free running instance index counter */
  361. static int edac_pci_idx;
  362. #define EDAC_PCI_GENCTL_NAME "EDAC PCI controller"
  363. struct edac_pci_gen_data {
  364. int edac_idx;
  365. };
  366. /*
  367. * edac_pci_create_generic_ctl
  368. *
  369. * A generic constructor for a PCI parity polling device
  370. * Some systems have more than one domain of PCI busses.
  371. * For systems with one domain, then this API will
  372. * provide for a generic poller.
  373. *
  374. * This routine calls the edac_pci_alloc_ctl_info() for
  375. * the generic device, with default values
  376. */
  377. struct edac_pci_ctl_info *edac_pci_create_generic_ctl(struct device *dev,
  378. const char *mod_name)
  379. {
  380. struct edac_pci_ctl_info *pci;
  381. struct edac_pci_gen_data *pdata;
  382. pci = edac_pci_alloc_ctl_info(sizeof(*pdata), EDAC_PCI_GENCTL_NAME);
  383. if (!pci)
  384. return NULL;
  385. pdata = pci->pvt_info;
  386. pci->dev = dev;
  387. dev_set_drvdata(pci->dev, pci);
  388. pci->dev_name = pci_name(to_pci_dev(dev));
  389. pci->mod_name = mod_name;
  390. pci->ctl_name = EDAC_PCI_GENCTL_NAME;
  391. pci->edac_check = edac_pci_generic_check;
  392. pdata->edac_idx = edac_pci_idx++;
  393. if (edac_pci_add_device(pci, pdata->edac_idx) > 0) {
  394. debugf3("%s(): failed edac_pci_add_device()\n", __func__);
  395. edac_pci_free_ctl_info(pci);
  396. return NULL;
  397. }
  398. return pci;
  399. }
  400. EXPORT_SYMBOL_GPL(edac_pci_create_generic_ctl);
  401. /*
  402. * edac_pci_release_generic_ctl
  403. *
  404. * The release function of a generic EDAC PCI polling device
  405. */
  406. void edac_pci_release_generic_ctl(struct edac_pci_ctl_info *pci)
  407. {
  408. debugf0("%s() pci mod=%s\n", __func__, pci->mod_name);
  409. edac_pci_del_device(pci->dev);
  410. edac_pci_free_ctl_info(pci);
  411. }
  412. EXPORT_SYMBOL_GPL(edac_pci_release_generic_ctl);