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