edac_mc.c 32 KB

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