edac_mc.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. /*
  2. * edac_mc kernel module
  3. * (C) 2005, 2006 Linux Networx (http://lnxi.com)
  4. * This file may be distributed under the terms of the
  5. * GNU General Public License.
  6. *
  7. * Written by Thayne Harbaugh
  8. * Based on work by Dan Hollis <goemon at anime dot net> and others.
  9. * http://www.anime.net/~goemon/linux-ecc/
  10. *
  11. * Modified by Dave Peterson and Doug Thompson
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/smp.h>
  19. #include <linux/init.h>
  20. #include <linux/sysctl.h>
  21. #include <linux/highmem.h>
  22. #include <linux/timer.h>
  23. #include <linux/slab.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/list.h>
  27. #include <linux/ctype.h>
  28. #include <linux/edac.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/page.h>
  31. #include <asm/edac.h>
  32. #include "edac_core.h"
  33. #include "edac_module.h"
  34. /* lock to memory controller's control array */
  35. static DEFINE_MUTEX(mem_ctls_mutex);
  36. static LIST_HEAD(mc_devices);
  37. #ifdef CONFIG_EDAC_DEBUG
  38. static void edac_mc_dump_channel(struct rank_info *chan)
  39. {
  40. debugf4("\tchannel = %p\n", chan);
  41. debugf4("\tchannel->chan_idx = %d\n", chan->chan_idx);
  42. debugf4("\tchannel->csrow = %p\n\n", chan->csrow);
  43. debugf4("\tchannel->dimm = %p\n", chan->dimm);
  44. }
  45. static void edac_mc_dump_dimm(struct dimm_info *dimm)
  46. {
  47. int i;
  48. debugf4("\tdimm = %p\n", dimm);
  49. debugf4("\tdimm->label = '%s'\n", dimm->label);
  50. debugf4("\tdimm->nr_pages = 0x%x\n", dimm->nr_pages);
  51. debugf4("\tdimm location ");
  52. for (i = 0; i < dimm->mci->n_layers; i++) {
  53. printk(KERN_CONT "%d", dimm->location[i]);
  54. if (i < dimm->mci->n_layers - 1)
  55. printk(KERN_CONT ".");
  56. }
  57. printk(KERN_CONT "\n");
  58. debugf4("\tdimm->grain = %d\n", dimm->grain);
  59. debugf4("\tdimm->nr_pages = 0x%x\n", dimm->nr_pages);
  60. }
  61. static void edac_mc_dump_csrow(struct csrow_info *csrow)
  62. {
  63. debugf4("\tcsrow = %p\n", csrow);
  64. debugf4("\tcsrow->csrow_idx = %d\n", csrow->csrow_idx);
  65. debugf4("\tcsrow->first_page = 0x%lx\n", csrow->first_page);
  66. debugf4("\tcsrow->last_page = 0x%lx\n", csrow->last_page);
  67. debugf4("\tcsrow->page_mask = 0x%lx\n", csrow->page_mask);
  68. debugf4("\tcsrow->nr_channels = %d\n", csrow->nr_channels);
  69. debugf4("\tcsrow->channels = %p\n", csrow->channels);
  70. debugf4("\tcsrow->mci = %p\n\n", csrow->mci);
  71. }
  72. static void edac_mc_dump_mci(struct mem_ctl_info *mci)
  73. {
  74. debugf3("\tmci = %p\n", mci);
  75. debugf3("\tmci->mtype_cap = %lx\n", mci->mtype_cap);
  76. debugf3("\tmci->edac_ctl_cap = %lx\n", mci->edac_ctl_cap);
  77. debugf3("\tmci->edac_cap = %lx\n", mci->edac_cap);
  78. debugf4("\tmci->edac_check = %p\n", mci->edac_check);
  79. debugf3("\tmci->nr_csrows = %d, csrows = %p\n",
  80. mci->nr_csrows, mci->csrows);
  81. debugf3("\tmci->nr_dimms = %d, dimms = %p\n",
  82. mci->tot_dimms, mci->dimms);
  83. debugf3("\tdev = %p\n", mci->dev);
  84. debugf3("\tmod_name:ctl_name = %s:%s\n", mci->mod_name, mci->ctl_name);
  85. debugf3("\tpvt_info = %p\n\n", mci->pvt_info);
  86. }
  87. #endif /* CONFIG_EDAC_DEBUG */
  88. /*
  89. * keep those in sync with the enum mem_type
  90. */
  91. const char *edac_mem_types[] = {
  92. "Empty csrow",
  93. "Reserved csrow type",
  94. "Unknown csrow type",
  95. "Fast page mode RAM",
  96. "Extended data out RAM",
  97. "Burst Extended data out RAM",
  98. "Single data rate SDRAM",
  99. "Registered single data rate SDRAM",
  100. "Double data rate SDRAM",
  101. "Registered Double data rate SDRAM",
  102. "Rambus DRAM",
  103. "Unbuffered DDR2 RAM",
  104. "Fully buffered DDR2",
  105. "Registered DDR2 RAM",
  106. "Rambus XDR",
  107. "Unbuffered DDR3 RAM",
  108. "Registered DDR3 RAM",
  109. };
  110. EXPORT_SYMBOL_GPL(edac_mem_types);
  111. /**
  112. * edac_align_ptr - Prepares the pointer offsets for a single-shot allocation
  113. * @p: pointer to a pointer with the memory offset to be used. At
  114. * return, this will be incremented to point to the next offset
  115. * @size: Size of the data structure to be reserved
  116. * @n_elems: Number of elements that should be reserved
  117. *
  118. * If 'size' is a constant, the compiler will optimize this whole function
  119. * down to either a no-op or the addition of a constant to the value of '*p'.
  120. *
  121. * The 'p' pointer is absolutely needed to keep the proper advancing
  122. * further in memory to the proper offsets when allocating the struct along
  123. * with its embedded structs, as edac_device_alloc_ctl_info() does it
  124. * above, for example.
  125. *
  126. * At return, the pointer 'p' will be incremented to be used on a next call
  127. * to this function.
  128. */
  129. void *edac_align_ptr(void **p, unsigned size, int n_elems)
  130. {
  131. unsigned align, r;
  132. void *ptr = *p;
  133. *p += size * n_elems;
  134. /*
  135. * 'p' can possibly be an unaligned item X such that sizeof(X) is
  136. * 'size'. Adjust 'p' so that its alignment is at least as
  137. * stringent as what the compiler would provide for X and return
  138. * the aligned result.
  139. * Here we assume that the alignment of a "long long" is the most
  140. * stringent alignment that the compiler will ever provide by default.
  141. * As far as I know, this is a reasonable assumption.
  142. */
  143. if (size > sizeof(long))
  144. align = sizeof(long long);
  145. else if (size > sizeof(int))
  146. align = sizeof(long);
  147. else if (size > sizeof(short))
  148. align = sizeof(int);
  149. else if (size > sizeof(char))
  150. align = sizeof(short);
  151. else
  152. return (char *)ptr;
  153. r = size % align;
  154. if (r == 0)
  155. return (char *)ptr;
  156. *p += align - r;
  157. return (void *)(((unsigned long)ptr) + align - r);
  158. }
  159. /**
  160. * edac_mc_alloc: Allocate and partially fill a struct mem_ctl_info structure
  161. * @mc_num: Memory controller number
  162. * @n_layers: Number of MC hierarchy layers
  163. * layers: Describes each layer as seen by the Memory Controller
  164. * @size_pvt: size of private storage needed
  165. *
  166. *
  167. * Everything is kmalloc'ed as one big chunk - more efficient.
  168. * Only can be used if all structures have the same lifetime - otherwise
  169. * you have to allocate and initialize your own structures.
  170. *
  171. * Use edac_mc_free() to free mc structures allocated by this function.
  172. *
  173. * NOTE: drivers handle multi-rank memories in different ways: in some
  174. * drivers, one multi-rank memory stick is mapped as one entry, while, in
  175. * others, a single multi-rank memory stick would be mapped into several
  176. * entries. Currently, this function will allocate multiple struct dimm_info
  177. * on such scenarios, as grouping the multiple ranks require drivers change.
  178. *
  179. * Returns:
  180. * On failure: NULL
  181. * On success: struct mem_ctl_info pointer
  182. */
  183. struct mem_ctl_info *edac_mc_alloc(unsigned mc_num,
  184. unsigned n_layers,
  185. struct edac_mc_layer *layers,
  186. unsigned sz_pvt)
  187. {
  188. struct mem_ctl_info *mci;
  189. struct edac_mc_layer *layer;
  190. struct csrow_info *csi, *csr;
  191. struct rank_info *chi, *chp, *chan;
  192. struct dimm_info *dimm;
  193. u32 *ce_per_layer[EDAC_MAX_LAYERS], *ue_per_layer[EDAC_MAX_LAYERS];
  194. unsigned pos[EDAC_MAX_LAYERS];
  195. unsigned size, tot_dimms = 1, count = 1;
  196. unsigned tot_csrows = 1, tot_channels = 1, tot_errcount = 0;
  197. void *pvt, *p, *ptr = NULL;
  198. int i, j, err, row, chn, n, len;
  199. bool per_rank = false;
  200. BUG_ON(n_layers > EDAC_MAX_LAYERS || n_layers == 0);
  201. /*
  202. * Calculate the total amount of dimms and csrows/cschannels while
  203. * in the old API emulation mode
  204. */
  205. for (i = 0; i < n_layers; i++) {
  206. tot_dimms *= layers[i].size;
  207. if (layers[i].is_virt_csrow)
  208. tot_csrows *= layers[i].size;
  209. else
  210. tot_channels *= layers[i].size;
  211. if (layers[i].type == EDAC_MC_LAYER_CHIP_SELECT)
  212. per_rank = true;
  213. }
  214. /* Figure out the offsets of the various items from the start of an mc
  215. * structure. We want the alignment of each item to be at least as
  216. * stringent as what the compiler would provide if we could simply
  217. * hardcode everything into a single struct.
  218. */
  219. mci = edac_align_ptr(&ptr, sizeof(*mci), 1);
  220. layer = edac_align_ptr(&ptr, sizeof(*layer), n_layers);
  221. csi = edac_align_ptr(&ptr, sizeof(*csi), tot_csrows);
  222. chi = edac_align_ptr(&ptr, sizeof(*chi), tot_csrows * tot_channels);
  223. dimm = edac_align_ptr(&ptr, sizeof(*dimm), tot_dimms);
  224. for (i = 0; i < n_layers; i++) {
  225. count *= layers[i].size;
  226. debugf4("%s: errcount layer %d size %d\n", __func__, i, count);
  227. ce_per_layer[i] = edac_align_ptr(&ptr, sizeof(u32), count);
  228. ue_per_layer[i] = edac_align_ptr(&ptr, sizeof(u32), count);
  229. tot_errcount += 2 * count;
  230. }
  231. debugf4("%s: allocating %d error counters\n", __func__, tot_errcount);
  232. pvt = edac_align_ptr(&ptr, sz_pvt, 1);
  233. size = ((unsigned long)pvt) + sz_pvt;
  234. debugf1("%s(): allocating %u bytes for mci data (%d %s, %d csrows/channels)\n",
  235. __func__, size,
  236. tot_dimms,
  237. per_rank ? "ranks" : "dimms",
  238. tot_csrows * tot_channels);
  239. mci = kzalloc(size, GFP_KERNEL);
  240. if (mci == NULL)
  241. return NULL;
  242. /* Adjust pointers so they point within the memory we just allocated
  243. * rather than an imaginary chunk of memory located at address 0.
  244. */
  245. layer = (struct edac_mc_layer *)(((char *)mci) + ((unsigned long)layer));
  246. csi = (struct csrow_info *)(((char *)mci) + ((unsigned long)csi));
  247. chi = (struct rank_info *)(((char *)mci) + ((unsigned long)chi));
  248. dimm = (struct dimm_info *)(((char *)mci) + ((unsigned long)dimm));
  249. for (i = 0; i < n_layers; i++) {
  250. mci->ce_per_layer[i] = (u32 *)((char *)mci + ((unsigned long)ce_per_layer[i]));
  251. mci->ue_per_layer[i] = (u32 *)((char *)mci + ((unsigned long)ue_per_layer[i]));
  252. }
  253. pvt = sz_pvt ? (((char *)mci) + ((unsigned long)pvt)) : NULL;
  254. /* setup index and various internal pointers */
  255. mci->mc_idx = mc_num;
  256. mci->csrows = csi;
  257. mci->dimms = dimm;
  258. mci->tot_dimms = tot_dimms;
  259. mci->pvt_info = pvt;
  260. mci->n_layers = n_layers;
  261. mci->layers = layer;
  262. memcpy(mci->layers, layers, sizeof(*layer) * n_layers);
  263. mci->nr_csrows = tot_csrows;
  264. mci->num_cschannel = tot_channels;
  265. mci->mem_is_per_rank = per_rank;
  266. /*
  267. * Fill the csrow struct
  268. */
  269. for (row = 0; row < tot_csrows; row++) {
  270. csr = &csi[row];
  271. csr->csrow_idx = row;
  272. csr->mci = mci;
  273. csr->nr_channels = tot_channels;
  274. chp = &chi[row * tot_channels];
  275. csr->channels = chp;
  276. for (chn = 0; chn < tot_channels; chn++) {
  277. chan = &chp[chn];
  278. chan->chan_idx = chn;
  279. chan->csrow = csr;
  280. }
  281. }
  282. /*
  283. * Fill the dimm struct
  284. */
  285. memset(&pos, 0, sizeof(pos));
  286. row = 0;
  287. chn = 0;
  288. debugf4("%s: initializing %d %s\n", __func__, tot_dimms,
  289. per_rank ? "ranks" : "dimms");
  290. for (i = 0; i < tot_dimms; i++) {
  291. chan = &csi[row].channels[chn];
  292. dimm = EDAC_DIMM_PTR(layer, mci->dimms, n_layers,
  293. pos[0], pos[1], pos[2]);
  294. dimm->mci = mci;
  295. debugf2("%s: %d: %s%zd (%d:%d:%d): row %d, chan %d\n", __func__,
  296. i, per_rank ? "rank" : "dimm", (dimm - mci->dimms),
  297. pos[0], pos[1], pos[2], row, chn);
  298. /*
  299. * Copy DIMM location and initialize it.
  300. */
  301. len = sizeof(dimm->label);
  302. p = dimm->label;
  303. n = snprintf(p, len, "mc#%u", mc_num);
  304. p += n;
  305. len -= n;
  306. for (j = 0; j < n_layers; j++) {
  307. n = snprintf(p, len, "%s#%u",
  308. edac_layer_name[layers[j].type],
  309. pos[j]);
  310. p += n;
  311. len -= n;
  312. dimm->location[j] = pos[j];
  313. if (len <= 0)
  314. break;
  315. }
  316. /* Link it to the csrows old API data */
  317. chan->dimm = dimm;
  318. dimm->csrow = row;
  319. dimm->cschannel = chn;
  320. /* Increment csrow location */
  321. row++;
  322. if (row == tot_csrows) {
  323. row = 0;
  324. chn++;
  325. }
  326. /* Increment dimm location */
  327. for (j = n_layers - 1; j >= 0; j--) {
  328. pos[j]++;
  329. if (pos[j] < layers[j].size)
  330. break;
  331. pos[j] = 0;
  332. }
  333. }
  334. mci->op_state = OP_ALLOC;
  335. INIT_LIST_HEAD(&mci->grp_kobj_list);
  336. /*
  337. * Initialize the 'root' kobj for the edac_mc controller
  338. */
  339. err = edac_mc_register_sysfs_main_kobj(mci);
  340. if (err) {
  341. kfree(mci);
  342. return NULL;
  343. }
  344. /* at this point, the root kobj is valid, and in order to
  345. * 'free' the object, then the function:
  346. * edac_mc_unregister_sysfs_main_kobj() must be called
  347. * which will perform kobj unregistration and the actual free
  348. * will occur during the kobject callback operation
  349. */
  350. return mci;
  351. }
  352. EXPORT_SYMBOL_GPL(edac_mc_alloc);
  353. /**
  354. * edac_mc_free
  355. * 'Free' a previously allocated 'mci' structure
  356. * @mci: pointer to a struct mem_ctl_info structure
  357. */
  358. void edac_mc_free(struct mem_ctl_info *mci)
  359. {
  360. debugf1("%s()\n", __func__);
  361. edac_mc_unregister_sysfs_main_kobj(mci);
  362. /* free the mci instance memory here */
  363. kfree(mci);
  364. }
  365. EXPORT_SYMBOL_GPL(edac_mc_free);
  366. /**
  367. * find_mci_by_dev
  368. *
  369. * scan list of controllers looking for the one that manages
  370. * the 'dev' device
  371. * @dev: pointer to a struct device related with the MCI
  372. */
  373. struct mem_ctl_info *find_mci_by_dev(struct device *dev)
  374. {
  375. struct mem_ctl_info *mci;
  376. struct list_head *item;
  377. debugf3("%s()\n", __func__);
  378. list_for_each(item, &mc_devices) {
  379. mci = list_entry(item, struct mem_ctl_info, link);
  380. if (mci->dev == dev)
  381. return mci;
  382. }
  383. return NULL;
  384. }
  385. EXPORT_SYMBOL_GPL(find_mci_by_dev);
  386. /*
  387. * handler for EDAC to check if NMI type handler has asserted interrupt
  388. */
  389. static int edac_mc_assert_error_check_and_clear(void)
  390. {
  391. int old_state;
  392. if (edac_op_state == EDAC_OPSTATE_POLL)
  393. return 1;
  394. old_state = edac_err_assert;
  395. edac_err_assert = 0;
  396. return old_state;
  397. }
  398. /*
  399. * edac_mc_workq_function
  400. * performs the operation scheduled by a workq request
  401. */
  402. static void edac_mc_workq_function(struct work_struct *work_req)
  403. {
  404. struct delayed_work *d_work = to_delayed_work(work_req);
  405. struct mem_ctl_info *mci = to_edac_mem_ctl_work(d_work);
  406. mutex_lock(&mem_ctls_mutex);
  407. /* if this control struct has movd to offline state, we are done */
  408. if (mci->op_state == OP_OFFLINE) {
  409. mutex_unlock(&mem_ctls_mutex);
  410. return;
  411. }
  412. /* Only poll controllers that are running polled and have a check */
  413. if (edac_mc_assert_error_check_and_clear() && (mci->edac_check != NULL))
  414. mci->edac_check(mci);
  415. mutex_unlock(&mem_ctls_mutex);
  416. /* Reschedule */
  417. queue_delayed_work(edac_workqueue, &mci->work,
  418. msecs_to_jiffies(edac_mc_get_poll_msec()));
  419. }
  420. /*
  421. * edac_mc_workq_setup
  422. * initialize a workq item for this mci
  423. * passing in the new delay period in msec
  424. *
  425. * locking model:
  426. *
  427. * called with the mem_ctls_mutex held
  428. */
  429. static void edac_mc_workq_setup(struct mem_ctl_info *mci, unsigned msec)
  430. {
  431. debugf0("%s()\n", __func__);
  432. /* if this instance is not in the POLL state, then simply return */
  433. if (mci->op_state != OP_RUNNING_POLL)
  434. return;
  435. INIT_DELAYED_WORK(&mci->work, edac_mc_workq_function);
  436. queue_delayed_work(edac_workqueue, &mci->work, msecs_to_jiffies(msec));
  437. }
  438. /*
  439. * edac_mc_workq_teardown
  440. * stop the workq processing on this mci
  441. *
  442. * locking model:
  443. *
  444. * called WITHOUT lock held
  445. */
  446. static void edac_mc_workq_teardown(struct mem_ctl_info *mci)
  447. {
  448. int status;
  449. if (mci->op_state != OP_RUNNING_POLL)
  450. return;
  451. status = cancel_delayed_work(&mci->work);
  452. if (status == 0) {
  453. debugf0("%s() not canceled, flush the queue\n",
  454. __func__);
  455. /* workq instance might be running, wait for it */
  456. flush_workqueue(edac_workqueue);
  457. }
  458. }
  459. /*
  460. * edac_mc_reset_delay_period(unsigned long value)
  461. *
  462. * user space has updated our poll period value, need to
  463. * reset our workq delays
  464. */
  465. void edac_mc_reset_delay_period(int value)
  466. {
  467. struct mem_ctl_info *mci;
  468. struct list_head *item;
  469. mutex_lock(&mem_ctls_mutex);
  470. /* scan the list and turn off all workq timers, doing so under lock
  471. */
  472. list_for_each(item, &mc_devices) {
  473. mci = list_entry(item, struct mem_ctl_info, link);
  474. if (mci->op_state == OP_RUNNING_POLL)
  475. cancel_delayed_work(&mci->work);
  476. }
  477. mutex_unlock(&mem_ctls_mutex);
  478. /* re-walk the list, and reset the poll delay */
  479. mutex_lock(&mem_ctls_mutex);
  480. list_for_each(item, &mc_devices) {
  481. mci = list_entry(item, struct mem_ctl_info, link);
  482. edac_mc_workq_setup(mci, (unsigned long) value);
  483. }
  484. mutex_unlock(&mem_ctls_mutex);
  485. }
  486. /* Return 0 on success, 1 on failure.
  487. * Before calling this function, caller must
  488. * assign a unique value to mci->mc_idx.
  489. *
  490. * locking model:
  491. *
  492. * called with the mem_ctls_mutex lock held
  493. */
  494. static int add_mc_to_global_list(struct mem_ctl_info *mci)
  495. {
  496. struct list_head *item, *insert_before;
  497. struct mem_ctl_info *p;
  498. insert_before = &mc_devices;
  499. p = find_mci_by_dev(mci->dev);
  500. if (unlikely(p != NULL))
  501. goto fail0;
  502. list_for_each(item, &mc_devices) {
  503. p = list_entry(item, struct mem_ctl_info, link);
  504. if (p->mc_idx >= mci->mc_idx) {
  505. if (unlikely(p->mc_idx == mci->mc_idx))
  506. goto fail1;
  507. insert_before = item;
  508. break;
  509. }
  510. }
  511. list_add_tail_rcu(&mci->link, insert_before);
  512. atomic_inc(&edac_handlers);
  513. return 0;
  514. fail0:
  515. edac_printk(KERN_WARNING, EDAC_MC,
  516. "%s (%s) %s %s already assigned %d\n", dev_name(p->dev),
  517. edac_dev_name(mci), p->mod_name, p->ctl_name, p->mc_idx);
  518. return 1;
  519. fail1:
  520. edac_printk(KERN_WARNING, EDAC_MC,
  521. "bug in low-level driver: attempt to assign\n"
  522. " duplicate mc_idx %d in %s()\n", p->mc_idx, __func__);
  523. return 1;
  524. }
  525. static void del_mc_from_global_list(struct mem_ctl_info *mci)
  526. {
  527. atomic_dec(&edac_handlers);
  528. list_del_rcu(&mci->link);
  529. /* these are for safe removal of devices from global list while
  530. * NMI handlers may be traversing list
  531. */
  532. synchronize_rcu();
  533. INIT_LIST_HEAD(&mci->link);
  534. }
  535. /**
  536. * edac_mc_find: Search for a mem_ctl_info structure whose index is 'idx'.
  537. *
  538. * If found, return a pointer to the structure.
  539. * Else return NULL.
  540. *
  541. * Caller must hold mem_ctls_mutex.
  542. */
  543. struct mem_ctl_info *edac_mc_find(int idx)
  544. {
  545. struct list_head *item;
  546. struct mem_ctl_info *mci;
  547. list_for_each(item, &mc_devices) {
  548. mci = list_entry(item, struct mem_ctl_info, link);
  549. if (mci->mc_idx >= idx) {
  550. if (mci->mc_idx == idx)
  551. return mci;
  552. break;
  553. }
  554. }
  555. return NULL;
  556. }
  557. EXPORT_SYMBOL(edac_mc_find);
  558. /**
  559. * edac_mc_add_mc: Insert the 'mci' structure into the mci global list and
  560. * create sysfs entries associated with mci structure
  561. * @mci: pointer to the mci structure to be added to the list
  562. *
  563. * Return:
  564. * 0 Success
  565. * !0 Failure
  566. */
  567. /* FIXME - should a warning be printed if no error detection? correction? */
  568. int edac_mc_add_mc(struct mem_ctl_info *mci)
  569. {
  570. debugf0("%s()\n", __func__);
  571. #ifdef CONFIG_EDAC_DEBUG
  572. if (edac_debug_level >= 3)
  573. edac_mc_dump_mci(mci);
  574. if (edac_debug_level >= 4) {
  575. int i;
  576. for (i = 0; i < mci->nr_csrows; i++) {
  577. int j;
  578. edac_mc_dump_csrow(&mci->csrows[i]);
  579. for (j = 0; j < mci->csrows[i].nr_channels; j++)
  580. edac_mc_dump_channel(&mci->csrows[i].
  581. channels[j]);
  582. }
  583. for (i = 0; i < mci->tot_dimms; i++)
  584. edac_mc_dump_dimm(&mci->dimms[i]);
  585. }
  586. #endif
  587. mutex_lock(&mem_ctls_mutex);
  588. if (add_mc_to_global_list(mci))
  589. goto fail0;
  590. /* set load time so that error rate can be tracked */
  591. mci->start_time = jiffies;
  592. if (edac_create_sysfs_mci_device(mci)) {
  593. edac_mc_printk(mci, KERN_WARNING,
  594. "failed to create sysfs device\n");
  595. goto fail1;
  596. }
  597. /* If there IS a check routine, then we are running POLLED */
  598. if (mci->edac_check != NULL) {
  599. /* This instance is NOW RUNNING */
  600. mci->op_state = OP_RUNNING_POLL;
  601. edac_mc_workq_setup(mci, edac_mc_get_poll_msec());
  602. } else {
  603. mci->op_state = OP_RUNNING_INTERRUPT;
  604. }
  605. /* Report action taken */
  606. edac_mc_printk(mci, KERN_INFO, "Giving out device to '%s' '%s':"
  607. " DEV %s\n", mci->mod_name, mci->ctl_name, edac_dev_name(mci));
  608. mutex_unlock(&mem_ctls_mutex);
  609. return 0;
  610. fail1:
  611. del_mc_from_global_list(mci);
  612. fail0:
  613. mutex_unlock(&mem_ctls_mutex);
  614. return 1;
  615. }
  616. EXPORT_SYMBOL_GPL(edac_mc_add_mc);
  617. /**
  618. * edac_mc_del_mc: Remove sysfs entries for specified mci structure and
  619. * remove mci structure from global list
  620. * @pdev: Pointer to 'struct device' representing mci structure to remove.
  621. *
  622. * Return pointer to removed mci structure, or NULL if device not found.
  623. */
  624. struct mem_ctl_info *edac_mc_del_mc(struct device *dev)
  625. {
  626. struct mem_ctl_info *mci;
  627. debugf0("%s()\n", __func__);
  628. mutex_lock(&mem_ctls_mutex);
  629. /* find the requested mci struct in the global list */
  630. mci = find_mci_by_dev(dev);
  631. if (mci == NULL) {
  632. mutex_unlock(&mem_ctls_mutex);
  633. return NULL;
  634. }
  635. del_mc_from_global_list(mci);
  636. mutex_unlock(&mem_ctls_mutex);
  637. /* flush workq processes */
  638. edac_mc_workq_teardown(mci);
  639. /* marking MCI offline */
  640. mci->op_state = OP_OFFLINE;
  641. /* remove from sysfs */
  642. edac_remove_sysfs_mci_device(mci);
  643. edac_printk(KERN_INFO, EDAC_MC,
  644. "Removed device %d for %s %s: DEV %s\n", mci->mc_idx,
  645. mci->mod_name, mci->ctl_name, edac_dev_name(mci));
  646. return mci;
  647. }
  648. EXPORT_SYMBOL_GPL(edac_mc_del_mc);
  649. static void edac_mc_scrub_block(unsigned long page, unsigned long offset,
  650. u32 size)
  651. {
  652. struct page *pg;
  653. void *virt_addr;
  654. unsigned long flags = 0;
  655. debugf3("%s()\n", __func__);
  656. /* ECC error page was not in our memory. Ignore it. */
  657. if (!pfn_valid(page))
  658. return;
  659. /* Find the actual page structure then map it and fix */
  660. pg = pfn_to_page(page);
  661. if (PageHighMem(pg))
  662. local_irq_save(flags);
  663. virt_addr = kmap_atomic(pg);
  664. /* Perform architecture specific atomic scrub operation */
  665. atomic_scrub(virt_addr + offset, size);
  666. /* Unmap and complete */
  667. kunmap_atomic(virt_addr);
  668. if (PageHighMem(pg))
  669. local_irq_restore(flags);
  670. }
  671. /* FIXME - should return -1 */
  672. int edac_mc_find_csrow_by_page(struct mem_ctl_info *mci, unsigned long page)
  673. {
  674. struct csrow_info *csrows = mci->csrows;
  675. int row, i, j, n;
  676. debugf1("MC%d: %s(): 0x%lx\n", mci->mc_idx, __func__, page);
  677. row = -1;
  678. for (i = 0; i < mci->nr_csrows; i++) {
  679. struct csrow_info *csrow = &csrows[i];
  680. n = 0;
  681. for (j = 0; j < csrow->nr_channels; j++) {
  682. struct dimm_info *dimm = csrow->channels[j].dimm;
  683. n += dimm->nr_pages;
  684. }
  685. if (n == 0)
  686. continue;
  687. debugf3("MC%d: %s(): first(0x%lx) page(0x%lx) last(0x%lx) "
  688. "mask(0x%lx)\n", mci->mc_idx, __func__,
  689. csrow->first_page, page, csrow->last_page,
  690. csrow->page_mask);
  691. if ((page >= csrow->first_page) &&
  692. (page <= csrow->last_page) &&
  693. ((page & csrow->page_mask) ==
  694. (csrow->first_page & csrow->page_mask))) {
  695. row = i;
  696. break;
  697. }
  698. }
  699. if (row == -1)
  700. edac_mc_printk(mci, KERN_ERR,
  701. "could not look up page error address %lx\n",
  702. (unsigned long)page);
  703. return row;
  704. }
  705. EXPORT_SYMBOL_GPL(edac_mc_find_csrow_by_page);
  706. const char *edac_layer_name[] = {
  707. [EDAC_MC_LAYER_BRANCH] = "branch",
  708. [EDAC_MC_LAYER_CHANNEL] = "channel",
  709. [EDAC_MC_LAYER_SLOT] = "slot",
  710. [EDAC_MC_LAYER_CHIP_SELECT] = "csrow",
  711. };
  712. EXPORT_SYMBOL_GPL(edac_layer_name);
  713. static void edac_inc_ce_error(struct mem_ctl_info *mci,
  714. bool enable_per_layer_report,
  715. const int pos[EDAC_MAX_LAYERS])
  716. {
  717. int i, index = 0;
  718. mci->ce_mc++;
  719. if (!enable_per_layer_report) {
  720. mci->ce_noinfo_count++;
  721. return;
  722. }
  723. for (i = 0; i < mci->n_layers; i++) {
  724. if (pos[i] < 0)
  725. break;
  726. index += pos[i];
  727. mci->ce_per_layer[i][index]++;
  728. if (i < mci->n_layers - 1)
  729. index *= mci->layers[i + 1].size;
  730. }
  731. }
  732. static void edac_inc_ue_error(struct mem_ctl_info *mci,
  733. bool enable_per_layer_report,
  734. const int pos[EDAC_MAX_LAYERS])
  735. {
  736. int i, index = 0;
  737. mci->ue_mc++;
  738. if (!enable_per_layer_report) {
  739. mci->ce_noinfo_count++;
  740. return;
  741. }
  742. for (i = 0; i < mci->n_layers; i++) {
  743. if (pos[i] < 0)
  744. break;
  745. index += pos[i];
  746. mci->ue_per_layer[i][index]++;
  747. if (i < mci->n_layers - 1)
  748. index *= mci->layers[i + 1].size;
  749. }
  750. }
  751. static void edac_ce_error(struct mem_ctl_info *mci,
  752. const int pos[EDAC_MAX_LAYERS],
  753. const char *msg,
  754. const char *location,
  755. const char *label,
  756. const char *detail,
  757. const char *other_detail,
  758. const bool enable_per_layer_report,
  759. const unsigned long page_frame_number,
  760. const unsigned long offset_in_page,
  761. u32 grain)
  762. {
  763. unsigned long remapped_page;
  764. if (edac_mc_get_log_ce()) {
  765. if (other_detail && *other_detail)
  766. edac_mc_printk(mci, KERN_WARNING,
  767. "CE %s on %s (%s%s - %s)\n",
  768. msg, label, location,
  769. detail, other_detail);
  770. else
  771. edac_mc_printk(mci, KERN_WARNING,
  772. "CE %s on %s (%s%s)\n",
  773. msg, label, location,
  774. detail);
  775. }
  776. edac_inc_ce_error(mci, enable_per_layer_report, pos);
  777. if (mci->scrub_mode & SCRUB_SW_SRC) {
  778. /*
  779. * Some memory controllers (called MCs below) can remap
  780. * memory so that it is still available at a different
  781. * address when PCI devices map into memory.
  782. * MC's that can't do this, lose the memory where PCI
  783. * devices are mapped. This mapping is MC-dependent
  784. * and so we call back into the MC driver for it to
  785. * map the MC page to a physical (CPU) page which can
  786. * then be mapped to a virtual page - which can then
  787. * be scrubbed.
  788. */
  789. remapped_page = mci->ctl_page_to_phys ?
  790. mci->ctl_page_to_phys(mci, page_frame_number) :
  791. page_frame_number;
  792. edac_mc_scrub_block(remapped_page,
  793. offset_in_page, grain);
  794. }
  795. }
  796. static void edac_ue_error(struct mem_ctl_info *mci,
  797. const int pos[EDAC_MAX_LAYERS],
  798. const char *msg,
  799. const char *location,
  800. const char *label,
  801. const char *detail,
  802. const char *other_detail,
  803. const bool enable_per_layer_report)
  804. {
  805. if (edac_mc_get_log_ue()) {
  806. if (other_detail && *other_detail)
  807. edac_mc_printk(mci, KERN_WARNING,
  808. "UE %s on %s (%s%s - %s)\n",
  809. msg, label, location, detail,
  810. other_detail);
  811. else
  812. edac_mc_printk(mci, KERN_WARNING,
  813. "UE %s on %s (%s%s)\n",
  814. msg, label, location, detail);
  815. }
  816. if (edac_mc_get_panic_on_ue()) {
  817. if (other_detail && *other_detail)
  818. panic("UE %s on %s (%s%s - %s)\n",
  819. msg, label, location, detail, other_detail);
  820. else
  821. panic("UE %s on %s (%s%s)\n",
  822. msg, label, location, detail);
  823. }
  824. edac_inc_ue_error(mci, enable_per_layer_report, pos);
  825. }
  826. #define OTHER_LABEL " or "
  827. void edac_mc_handle_error(const enum hw_event_mc_err_type type,
  828. struct mem_ctl_info *mci,
  829. const unsigned long page_frame_number,
  830. const unsigned long offset_in_page,
  831. const unsigned long syndrome,
  832. const int layer0,
  833. const int layer1,
  834. const int layer2,
  835. const char *msg,
  836. const char *other_detail,
  837. const void *mcelog)
  838. {
  839. /* FIXME: too much for stack: move it to some pre-alocated area */
  840. char detail[80], location[80];
  841. char label[(EDAC_MC_LABEL_LEN + 1 + sizeof(OTHER_LABEL)) * mci->tot_dimms];
  842. char *p;
  843. int row = -1, chan = -1;
  844. int pos[EDAC_MAX_LAYERS] = { layer0, layer1, layer2 };
  845. int i;
  846. u32 grain;
  847. bool enable_per_layer_report = false;
  848. debugf3("MC%d: %s()\n", mci->mc_idx, __func__);
  849. /*
  850. * Check if the event report is consistent and if the memory
  851. * location is known. If it is known, enable_per_layer_report will be
  852. * true, the DIMM(s) label info will be filled and the per-layer
  853. * error counters will be incremented.
  854. */
  855. for (i = 0; i < mci->n_layers; i++) {
  856. if (pos[i] >= (int)mci->layers[i].size) {
  857. if (type == HW_EVENT_ERR_CORRECTED)
  858. p = "CE";
  859. else
  860. p = "UE";
  861. edac_mc_printk(mci, KERN_ERR,
  862. "INTERNAL ERROR: %s value is out of range (%d >= %d)\n",
  863. edac_layer_name[mci->layers[i].type],
  864. pos[i], mci->layers[i].size);
  865. /*
  866. * Instead of just returning it, let's use what's
  867. * known about the error. The increment routines and
  868. * the DIMM filter logic will do the right thing by
  869. * pointing the likely damaged DIMMs.
  870. */
  871. pos[i] = -1;
  872. }
  873. if (pos[i] >= 0)
  874. enable_per_layer_report = true;
  875. }
  876. /*
  877. * Get the dimm label/grain that applies to the match criteria.
  878. * As the error algorithm may not be able to point to just one memory
  879. * stick, the logic here will get all possible labels that could
  880. * pottentially be affected by the error.
  881. * On FB-DIMM memory controllers, for uncorrected errors, it is common
  882. * to have only the MC channel and the MC dimm (also called "branch")
  883. * but the channel is not known, as the memory is arranged in pairs,
  884. * where each memory belongs to a separate channel within the same
  885. * branch.
  886. */
  887. grain = 0;
  888. p = label;
  889. *p = '\0';
  890. for (i = 0; i < mci->tot_dimms; i++) {
  891. struct dimm_info *dimm = &mci->dimms[i];
  892. if (layer0 >= 0 && layer0 != dimm->location[0])
  893. continue;
  894. if (layer1 >= 0 && layer1 != dimm->location[1])
  895. continue;
  896. if (layer2 >= 0 && layer2 != dimm->location[2])
  897. continue;
  898. /* get the max grain, over the error match range */
  899. if (dimm->grain > grain)
  900. grain = dimm->grain;
  901. /*
  902. * If the error is memory-controller wide, there's no need to
  903. * seek for the affected DIMMs because the whole
  904. * channel/memory controller/... may be affected.
  905. * Also, don't show errors for empty DIMM slots.
  906. */
  907. if (enable_per_layer_report && dimm->nr_pages) {
  908. if (p != label) {
  909. strcpy(p, OTHER_LABEL);
  910. p += strlen(OTHER_LABEL);
  911. }
  912. strcpy(p, dimm->label);
  913. p += strlen(p);
  914. *p = '\0';
  915. /*
  916. * get csrow/channel of the DIMM, in order to allow
  917. * incrementing the compat API counters
  918. */
  919. debugf4("%s: %s csrows map: (%d,%d)\n",
  920. __func__,
  921. mci->mem_is_per_rank ? "rank" : "dimm",
  922. dimm->csrow, dimm->cschannel);
  923. if (row == -1)
  924. row = dimm->csrow;
  925. else if (row >= 0 && row != dimm->csrow)
  926. row = -2;
  927. if (chan == -1)
  928. chan = dimm->cschannel;
  929. else if (chan >= 0 && chan != dimm->cschannel)
  930. chan = -2;
  931. }
  932. }
  933. if (!enable_per_layer_report) {
  934. strcpy(label, "any memory");
  935. } else {
  936. debugf4("%s: csrow/channel to increment: (%d,%d)\n",
  937. __func__, row, chan);
  938. if (p == label)
  939. strcpy(label, "unknown memory");
  940. if (type == HW_EVENT_ERR_CORRECTED) {
  941. if (row >= 0) {
  942. mci->csrows[row].ce_count++;
  943. if (chan >= 0)
  944. mci->csrows[row].channels[chan].ce_count++;
  945. }
  946. } else
  947. if (row >= 0)
  948. mci->csrows[row].ue_count++;
  949. }
  950. /* Fill the RAM location data */
  951. p = location;
  952. for (i = 0; i < mci->n_layers; i++) {
  953. if (pos[i] < 0)
  954. continue;
  955. p += sprintf(p, "%s:%d ",
  956. edac_layer_name[mci->layers[i].type],
  957. pos[i]);
  958. }
  959. /* Memory type dependent details about the error */
  960. if (type == HW_EVENT_ERR_CORRECTED) {
  961. snprintf(detail, sizeof(detail),
  962. "page:0x%lx offset:0x%lx grain:%d syndrome:0x%lx",
  963. page_frame_number, offset_in_page,
  964. grain, syndrome);
  965. edac_ce_error(mci, pos, msg, location, label, detail,
  966. other_detail, enable_per_layer_report,
  967. page_frame_number, offset_in_page, grain);
  968. } else {
  969. snprintf(detail, sizeof(detail),
  970. "page:0x%lx offset:0x%lx grain:%d",
  971. page_frame_number, offset_in_page, grain);
  972. edac_ue_error(mci, pos, msg, location, label, detail,
  973. other_detail, enable_per_layer_report);
  974. }
  975. }
  976. EXPORT_SYMBOL_GPL(edac_mc_handle_error);