edac_device.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*
  2. * edac_device.c
  3. * (C) 2007 www.douglaskthompson.com
  4. *
  5. * This file may be distributed under the terms of the
  6. * GNU General Public License.
  7. *
  8. * Written by Doug Thompson <norsk5@xmission.com>
  9. *
  10. * edac_device API implementation
  11. * 19 Jan 2007
  12. */
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/smp.h>
  16. #include <linux/init.h>
  17. #include <linux/sysctl.h>
  18. #include <linux/highmem.h>
  19. #include <linux/timer.h>
  20. #include <linux/slab.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/list.h>
  23. #include <linux/sysdev.h>
  24. #include <linux/ctype.h>
  25. #include <linux/workqueue.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/page.h>
  28. #include "edac_core.h"
  29. #include "edac_module.h"
  30. /* lock to memory controller's control array */
  31. static DECLARE_MUTEX(device_ctls_mutex);
  32. static struct list_head edac_device_list = LIST_HEAD_INIT(edac_device_list);
  33. static inline void lock_device_list(void)
  34. {
  35. down(&device_ctls_mutex);
  36. }
  37. static inline void unlock_device_list(void)
  38. {
  39. up(&device_ctls_mutex);
  40. }
  41. #ifdef CONFIG_EDAC_DEBUG
  42. static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
  43. {
  44. debugf3("\tedac_dev = %p dev_idx=%d \n", edac_dev,edac_dev->dev_idx);
  45. debugf4("\tedac_dev->edac_check = %p\n", edac_dev->edac_check);
  46. debugf3("\tdev = %p\n", edac_dev->dev);
  47. debugf3("\tmod_name:ctl_name = %s:%s\n",
  48. edac_dev->mod_name, edac_dev->ctl_name);
  49. debugf3("\tpvt_info = %p\n\n", edac_dev->pvt_info);
  50. }
  51. #endif /* CONFIG_EDAC_DEBUG */
  52. /*
  53. * The alloc() and free() functions for the 'edac_device' control info
  54. * structure. A MC driver will allocate one of these for each edac_device
  55. * it is going to control/register with the EDAC CORE.
  56. */
  57. struct edac_device_ctl_info *edac_device_alloc_ctl_info(
  58. unsigned sz_private,
  59. char *edac_device_name,
  60. unsigned nr_instances,
  61. char *edac_block_name,
  62. unsigned nr_blocks,
  63. unsigned offset_value,
  64. struct edac_attrib_spec *attrib_spec,
  65. unsigned nr_attribs)
  66. {
  67. struct edac_device_ctl_info *dev_ctl;
  68. struct edac_device_instance *dev_inst, *inst;
  69. struct edac_device_block *dev_blk, *blk_p, *blk;
  70. struct edac_attrib *dev_attrib, *attrib_p, *attrib;
  71. unsigned total_size;
  72. unsigned count;
  73. unsigned instance, block, attr;
  74. void *pvt;
  75. debugf1("%s() instances=%d blocks=%d\n",
  76. __func__,nr_instances,nr_blocks);
  77. /* Figure out the offsets of the various items from the start of an
  78. * ctl_info structure. We want the alignment of each item
  79. * to be at least as stringent as what the compiler would
  80. * provide if we could simply hardcode everything into a single struct.
  81. */
  82. dev_ctl = (struct edac_device_ctl_info *) 0;
  83. /* Calc the 'end' offset past the ctl_info structure */
  84. dev_inst = (struct edac_device_instance *)
  85. edac_align_ptr(&dev_ctl[1],sizeof(*dev_inst));
  86. /* Calc the 'end' offset past the instance array */
  87. dev_blk = (struct edac_device_block *)
  88. edac_align_ptr(&dev_inst[nr_instances],sizeof(*dev_blk));
  89. /* Calc the 'end' offset past the dev_blk array */
  90. count = nr_instances * nr_blocks;
  91. dev_attrib = (struct edac_attrib *)
  92. edac_align_ptr(&dev_blk[count],sizeof(*dev_attrib));
  93. /* Check for case of NO attributes specified */
  94. if (nr_attribs > 0)
  95. count *= nr_attribs;
  96. /* Calc the 'end' offset past the attributes array */
  97. pvt = edac_align_ptr(&dev_attrib[count],sz_private);
  98. total_size = ((unsigned long) pvt) + sz_private;
  99. /* Allocate the amount of memory for the set of control structures */
  100. if ((dev_ctl = kmalloc(total_size, GFP_KERNEL)) == NULL)
  101. return NULL;
  102. /* Adjust pointers so they point within the memory we just allocated
  103. * rather than an imaginary chunk of memory located at address 0.
  104. */
  105. dev_inst = (struct edac_device_instance *)
  106. (((char *) dev_ctl) + ((unsigned long) dev_inst));
  107. dev_blk = (struct edac_device_block *)
  108. (((char *) dev_ctl) + ((unsigned long) dev_blk));
  109. dev_attrib = (struct edac_attrib *)
  110. (((char *) dev_ctl) + ((unsigned long) dev_attrib));
  111. pvt = sz_private ?
  112. (((char *) dev_ctl) + ((unsigned long) pvt)) : NULL;
  113. memset(dev_ctl, 0, total_size); /* clear all fields */
  114. dev_ctl->nr_instances = nr_instances;
  115. dev_ctl->instances = dev_inst;
  116. dev_ctl->pvt_info = pvt;
  117. /* Name of this edac device, ensure null terminated */
  118. snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s", edac_device_name);
  119. dev_ctl->name[sizeof(dev_ctl->name)-1] = '\0';
  120. /* Initialize every Instance */
  121. for (instance = 0; instance < nr_instances; instance++) {
  122. inst = &dev_inst[instance];
  123. inst->ctl = dev_ctl;
  124. inst->nr_blocks = nr_blocks;
  125. blk_p = &dev_blk[instance * nr_blocks];
  126. inst->blocks = blk_p;
  127. /* name of this instance */
  128. snprintf(inst->name, sizeof(inst->name),
  129. "%s%u", edac_device_name, instance);
  130. inst->name[sizeof(inst->name)-1] = '\0';
  131. /* Initialize every block in each instance */
  132. for ( block = 0;
  133. block < nr_blocks;
  134. block++) {
  135. blk = &blk_p[block];
  136. blk->instance = inst;
  137. blk->nr_attribs = nr_attribs;
  138. attrib_p = &dev_attrib[block * nr_attribs];
  139. blk->attribs = attrib_p;
  140. snprintf(blk->name, sizeof(blk->name),
  141. "%s%d", edac_block_name,block+1);
  142. blk->name[sizeof(blk->name)-1] = '\0';
  143. debugf1("%s() instance=%d block=%d name=%s\n",
  144. __func__, instance,block,blk->name);
  145. if (attrib_spec != NULL) {
  146. /* when there is an attrib_spec passed int then
  147. * Initialize every attrib of each block
  148. */
  149. for (attr = 0; attr < nr_attribs; attr++) {
  150. attrib = &attrib_p[attr];
  151. attrib->block = blk;
  152. /* Link each attribute to the caller's
  153. * spec entry, for name and type
  154. */
  155. attrib->spec = &attrib_spec[attr];
  156. }
  157. }
  158. }
  159. }
  160. /* Mark this instance as merely ALLOCATED */
  161. dev_ctl->op_state = OP_ALLOC;
  162. return dev_ctl;
  163. }
  164. EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
  165. /*
  166. * edac_device_free_ctl_info()
  167. * frees the memory allocated by the edac_device_alloc_ctl_info()
  168. * function
  169. */
  170. void edac_device_free_ctl_info( struct edac_device_ctl_info *ctl_info) {
  171. kfree(ctl_info);
  172. }
  173. EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
  174. /*
  175. * find_edac_device_by_dev
  176. * scans the edac_device list for a specific 'struct device *'
  177. */
  178. static struct edac_device_ctl_info *
  179. find_edac_device_by_dev(struct device *dev)
  180. {
  181. struct edac_device_ctl_info *edac_dev;
  182. struct list_head *item;
  183. debugf3("%s()\n", __func__);
  184. list_for_each(item, &edac_device_list) {
  185. edac_dev = list_entry(item, struct edac_device_ctl_info, link);
  186. if (edac_dev->dev == dev)
  187. return edac_dev;
  188. }
  189. return NULL;
  190. }
  191. /*
  192. * add_edac_dev_to_global_list
  193. * Before calling this function, caller must
  194. * assign a unique value to edac_dev->dev_idx.
  195. * Return:
  196. * 0 on success
  197. * 1 on failure.
  198. */
  199. static int add_edac_dev_to_global_list (struct edac_device_ctl_info *edac_dev)
  200. {
  201. struct list_head *item, *insert_before;
  202. struct edac_device_ctl_info *rover;
  203. insert_before = &edac_device_list;
  204. /* Determine if already on the list */
  205. if (unlikely((rover = find_edac_device_by_dev(edac_dev->dev)) != NULL))
  206. goto fail0;
  207. /* Insert in ascending order by 'dev_idx', so find position */
  208. list_for_each(item, &edac_device_list) {
  209. rover = list_entry(item, struct edac_device_ctl_info, link);
  210. if (rover->dev_idx >= edac_dev->dev_idx) {
  211. if (unlikely(rover->dev_idx == edac_dev->dev_idx))
  212. goto fail1;
  213. insert_before = item;
  214. break;
  215. }
  216. }
  217. list_add_tail_rcu(&edac_dev->link, insert_before);
  218. return 0;
  219. fail0:
  220. edac_printk(KERN_WARNING, EDAC_MC,
  221. "%s (%s) %s %s already assigned %d\n",
  222. rover->dev->bus_id, dev_name(rover),
  223. rover->mod_name, rover->ctl_name, rover->dev_idx);
  224. return 1;
  225. fail1:
  226. edac_printk(KERN_WARNING, EDAC_MC,
  227. "bug in low-level driver: attempt to assign\n"
  228. " duplicate dev_idx %d in %s()\n", rover->dev_idx, __func__);
  229. return 1;
  230. }
  231. /*
  232. * complete_edac_device_list_del
  233. */
  234. static void complete_edac_device_list_del(struct rcu_head *head)
  235. {
  236. struct edac_device_ctl_info *edac_dev;
  237. edac_dev = container_of(head, struct edac_device_ctl_info, rcu);
  238. INIT_LIST_HEAD(&edac_dev->link);
  239. complete(&edac_dev->complete);
  240. }
  241. /*
  242. * del_edac_device_from_global_list
  243. */
  244. static void del_edac_device_from_global_list(
  245. struct edac_device_ctl_info *edac_device)
  246. {
  247. list_del_rcu(&edac_device->link);
  248. init_completion(&edac_device->complete);
  249. call_rcu(&edac_device->rcu, complete_edac_device_list_del);
  250. wait_for_completion(&edac_device->complete);
  251. }
  252. /**
  253. * edac_device_find
  254. * Search for a edac_device_ctl_info structure whose index is 'idx'.
  255. *
  256. * If found, return a pointer to the structure.
  257. * Else return NULL.
  258. *
  259. * Caller must hold device_ctls_mutex.
  260. */
  261. struct edac_device_ctl_info * edac_device_find(int idx)
  262. {
  263. struct list_head *item;
  264. struct edac_device_ctl_info *edac_dev;
  265. /* Iterate over list, looking for exact match of ID */
  266. list_for_each(item, &edac_device_list) {
  267. edac_dev = list_entry(item, struct edac_device_ctl_info, link);
  268. if (edac_dev->dev_idx >= idx) {
  269. if (edac_dev->dev_idx == idx)
  270. return edac_dev;
  271. /* not on list, so terminate early */
  272. break;
  273. }
  274. }
  275. return NULL;
  276. }
  277. EXPORT_SYMBOL(edac_device_find);
  278. /*
  279. * edac_device_workq_function
  280. * performs the operation scheduled by a workq request
  281. */
  282. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
  283. static void edac_device_workq_function(struct work_struct *work_req)
  284. {
  285. struct delayed_work *d_work = (struct delayed_work*) work_req;
  286. struct edac_device_ctl_info *edac_dev =
  287. to_edac_device_ctl_work(d_work);
  288. #else
  289. static void edac_device_workq_function(void *ptr)
  290. {
  291. struct edac_device_ctl_info *edac_dev =
  292. (struct edac_device_ctl_info *) ptr;
  293. #endif
  294. //debugf0("%s() here and running\n", __func__);
  295. lock_device_list();
  296. /* Only poll controllers that are running polled and have a check */
  297. if ((edac_dev->op_state == OP_RUNNING_POLL) &&
  298. (edac_dev->edac_check != NULL)) {
  299. edac_dev->edac_check(edac_dev);
  300. }
  301. unlock_device_list();
  302. /* Reschedule */
  303. queue_delayed_work(edac_workqueue,&edac_dev->work, edac_dev->delay);
  304. }
  305. /*
  306. * edac_device_workq_setup
  307. * initialize a workq item for this edac_device instance
  308. * passing in the new delay period in msec
  309. */
  310. void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
  311. unsigned msec)
  312. {
  313. debugf0("%s()\n", __func__);
  314. edac_dev->poll_msec = msec;
  315. edac_calc_delay(edac_dev); /* Calc delay jiffies */
  316. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
  317. INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
  318. #else
  319. INIT_WORK(&edac_dev->work, edac_device_workq_function, edac_dev);
  320. #endif
  321. queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
  322. }
  323. /*
  324. * edac_device_workq_teardown
  325. * stop the workq processing on this edac_dev
  326. */
  327. void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
  328. {
  329. int status;
  330. status = cancel_delayed_work(&edac_dev->work);
  331. if (status == 0) {
  332. /* workq instance might be running, wait for it */
  333. flush_workqueue(edac_workqueue);
  334. }
  335. }
  336. /*
  337. * edac_device_reset_delay_period
  338. */
  339. void edac_device_reset_delay_period(
  340. struct edac_device_ctl_info *edac_dev,
  341. unsigned long value)
  342. {
  343. lock_device_list();
  344. /* cancel the current workq request */
  345. edac_device_workq_teardown(edac_dev);
  346. /* restart the workq request, with new delay value */
  347. edac_device_workq_setup(edac_dev, value);
  348. unlock_device_list();
  349. }
  350. /**
  351. * edac_device_add_device: Insert the 'edac_dev' structure into the
  352. * edac_device global list and create sysfs entries associated with
  353. * edac_device structure.
  354. * @edac_device: pointer to the edac_device structure to be added to the list
  355. * @edac_idx: A unique numeric identifier to be assigned to the
  356. * 'edac_device' structure.
  357. *
  358. * Return:
  359. * 0 Success
  360. * !0 Failure
  361. */
  362. int edac_device_add_device(struct edac_device_ctl_info *edac_dev, int edac_idx)
  363. {
  364. debugf0("%s()\n", __func__);
  365. edac_dev->dev_idx = edac_idx;
  366. #ifdef CONFIG_EDAC_DEBUG
  367. if (edac_debug_level >= 3)
  368. edac_device_dump_device(edac_dev);
  369. #endif
  370. lock_device_list();
  371. if (add_edac_dev_to_global_list(edac_dev))
  372. goto fail0;
  373. /* set load time so that error rate can be tracked */
  374. edac_dev->start_time = jiffies;
  375. /* create this instance's sysfs entries */
  376. if (edac_device_create_sysfs(edac_dev)) {
  377. edac_device_printk(edac_dev, KERN_WARNING,
  378. "failed to create sysfs device\n");
  379. goto fail1;
  380. }
  381. /* If there IS a check routine, then we are running POLLED */
  382. if (edac_dev->edac_check != NULL) {
  383. /* This instance is NOW RUNNING */
  384. edac_dev->op_state = OP_RUNNING_POLL;
  385. /*
  386. * enable workq processing on this instance,
  387. * default = 1000 msec
  388. */
  389. edac_device_workq_setup(edac_dev, 1000);
  390. } else {
  391. edac_dev->op_state = OP_RUNNING_INTERRUPT;
  392. }
  393. /* Report action taken */
  394. edac_device_printk(edac_dev, KERN_INFO,
  395. "Giving out device to module '%s' controller '%s': DEV '%s' (%s)\n",
  396. edac_dev->mod_name,
  397. edac_dev->ctl_name,
  398. dev_name(edac_dev),
  399. edac_op_state_toString(edac_dev->op_state)
  400. );
  401. unlock_device_list();
  402. return 0;
  403. fail1:
  404. /* Some error, so remove the entry from the lsit */
  405. del_edac_device_from_global_list(edac_dev);
  406. fail0:
  407. unlock_device_list();
  408. return 1;
  409. }
  410. EXPORT_SYMBOL_GPL(edac_device_add_device);
  411. /**
  412. * edac_device_del_device:
  413. * Remove sysfs entries for specified edac_device structure and
  414. * then remove edac_device structure from global list
  415. *
  416. * @pdev:
  417. * Pointer to 'struct device' representing edac_device
  418. * structure to remove.
  419. *
  420. * Return:
  421. * Pointer to removed edac_device structure,
  422. * OR NULL if device not found.
  423. */
  424. struct edac_device_ctl_info * edac_device_del_device(struct device *dev)
  425. {
  426. struct edac_device_ctl_info *edac_dev;
  427. debugf0("MC: %s()\n", __func__);
  428. lock_device_list();
  429. if ((edac_dev = find_edac_device_by_dev(dev)) == NULL) {
  430. unlock_device_list();
  431. return NULL;
  432. }
  433. /* mark this instance as OFFLINE */
  434. edac_dev->op_state = OP_OFFLINE;
  435. /* clear workq processing on this instance */
  436. edac_device_workq_teardown(edac_dev);
  437. /* Tear down the sysfs entries for this instance */
  438. edac_device_remove_sysfs(edac_dev);
  439. /* deregister from global list */
  440. del_edac_device_from_global_list(edac_dev);
  441. unlock_device_list();
  442. edac_printk(KERN_INFO, EDAC_MC,
  443. "Removed device %d for %s %s: DEV %s\n",
  444. edac_dev->dev_idx,
  445. edac_dev->mod_name,
  446. edac_dev->ctl_name,
  447. dev_name(edac_dev));
  448. return edac_dev;
  449. }
  450. EXPORT_SYMBOL_GPL(edac_device_del_device);
  451. static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
  452. {
  453. return edac_dev->log_ce;
  454. }
  455. static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
  456. {
  457. return edac_dev->log_ue;
  458. }
  459. static inline int edac_device_get_panic_on_ue(
  460. struct edac_device_ctl_info *edac_dev)
  461. {
  462. return edac_dev->panic_on_ue;
  463. }
  464. /*
  465. * edac_device_handle_ce
  466. * perform a common output and handling of an 'edac_dev' CE event
  467. */
  468. void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
  469. int inst_nr, int block_nr, const char *msg)
  470. {
  471. struct edac_device_instance *instance;
  472. struct edac_device_block *block = NULL;
  473. if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
  474. edac_device_printk(edac_dev, KERN_ERR,
  475. "INTERNAL ERROR: 'instance' out of range "
  476. "(%d >= %d)\n", inst_nr, edac_dev->nr_instances);
  477. return;
  478. }
  479. instance = edac_dev->instances + inst_nr;
  480. if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
  481. edac_device_printk(edac_dev, KERN_ERR,
  482. "INTERNAL ERROR: instance %d 'block' out of range "
  483. "(%d >= %d)\n", inst_nr, block_nr, instance->nr_blocks);
  484. return;
  485. }
  486. if (instance->nr_blocks > 0) {
  487. block = instance->blocks + block_nr;
  488. block->counters.ce_count++;
  489. }
  490. /* Propogate the count up the 'totals' tree */
  491. instance->counters.ce_count++;
  492. edac_dev->counters.ce_count++;
  493. if (edac_device_get_log_ce(edac_dev))
  494. edac_device_printk(edac_dev, KERN_WARNING,
  495. "CE ctl: %s, instance: %s, block: %s: %s\n",
  496. edac_dev->ctl_name, instance->name,
  497. block ? block->name : "N/A", msg);
  498. }
  499. EXPORT_SYMBOL_GPL(edac_device_handle_ce);
  500. /*
  501. * edac_device_handle_ue
  502. * perform a common output and handling of an 'edac_dev' UE event
  503. */
  504. void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
  505. int inst_nr, int block_nr, const char *msg)
  506. {
  507. struct edac_device_instance *instance;
  508. struct edac_device_block *block = NULL;
  509. if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
  510. edac_device_printk(edac_dev, KERN_ERR,
  511. "INTERNAL ERROR: 'instance' out of range "
  512. "(%d >= %d)\n", inst_nr, edac_dev->nr_instances);
  513. return;
  514. }
  515. instance = edac_dev->instances + inst_nr;
  516. if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
  517. edac_device_printk(edac_dev, KERN_ERR,
  518. "INTERNAL ERROR: instance %d 'block' out of range "
  519. "(%d >= %d)\n", inst_nr, block_nr, instance->nr_blocks);
  520. return;
  521. }
  522. if (instance->nr_blocks > 0) {
  523. block = instance->blocks + block_nr;
  524. block->counters.ue_count++;
  525. }
  526. /* Propogate the count up the 'totals' tree */
  527. instance->counters.ue_count++;
  528. edac_dev->counters.ue_count++;
  529. if (edac_device_get_log_ue(edac_dev))
  530. edac_device_printk(edac_dev, KERN_EMERG,
  531. "UE ctl: %s, instance: %s, block: %s: %s\n",
  532. edac_dev->ctl_name, instance->name,
  533. block ? block->name : "N/A", msg);
  534. if (edac_device_get_panic_on_ue(edac_dev))
  535. panic("EDAC %s: UE instance: %s, block %s: %s\n",
  536. edac_dev->ctl_name, instance->name,
  537. block ? block->name : "N/A", msg);
  538. }
  539. EXPORT_SYMBOL_GPL(edac_device_handle_ue);