edac_device.c 17 KB

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