edac_device.c 17 KB

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