xref: /openbmc/linux/drivers/edac/edac_device.c (revision 0030d7d6)
1 
2 /*
3  * edac_device.c
4  * (C) 2007 www.douglaskthompson.com
5  *
6  * This file may be distributed under the terms of the
7  * GNU General Public License.
8  *
9  * Written by Doug Thompson <norsk5@xmission.com>
10  *
11  * edac_device API implementation
12  * 19 Jan 2007
13  */
14 
15 #include <asm/page.h>
16 #include <linux/uaccess.h>
17 #include <linux/ctype.h>
18 #include <linux/highmem.h>
19 #include <linux/init.h>
20 #include <linux/jiffies.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/smp.h>
24 #include <linux/spinlock.h>
25 #include <linux/sysctl.h>
26 #include <linux/timer.h>
27 
28 #include "edac_device.h"
29 #include "edac_module.h"
30 
31 /* lock for the list: 'edac_device_list', manipulation of this list
32  * is protected by the 'device_ctls_mutex' lock
33  */
34 static DEFINE_MUTEX(device_ctls_mutex);
35 static LIST_HEAD(edac_device_list);
36 
37 #ifdef CONFIG_EDAC_DEBUG
38 static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
39 {
40 	edac_dbg(3, "\tedac_dev = %p dev_idx=%d\n",
41 		 edac_dev, edac_dev->dev_idx);
42 	edac_dbg(4, "\tedac_dev->edac_check = %p\n", edac_dev->edac_check);
43 	edac_dbg(3, "\tdev = %p\n", edac_dev->dev);
44 	edac_dbg(3, "\tmod_name:ctl_name = %s:%s\n",
45 		 edac_dev->mod_name, edac_dev->ctl_name);
46 	edac_dbg(3, "\tpvt_info = %p\n\n", edac_dev->pvt_info);
47 }
48 #endif				/* CONFIG_EDAC_DEBUG */
49 
50 /*
51  * @off_val: zero, 1, or other based offset
52  */
53 struct edac_device_ctl_info *
54 edac_device_alloc_ctl_info(unsigned pvt_sz, char *dev_name, unsigned nr_instances,
55 			   char *blk_name, unsigned nr_blocks, unsigned off_val,
56 			   struct edac_dev_sysfs_block_attribute *attrib_spec,
57 			   unsigned nr_attrib, int device_index)
58 {
59 	struct edac_dev_sysfs_block_attribute *dev_attrib, *attrib_p, *attrib;
60 	struct edac_device_block *dev_blk, *blk_p, *blk;
61 	struct edac_device_instance *dev_inst, *inst;
62 	struct edac_device_ctl_info *dev_ctl;
63 	unsigned instance, block, attr;
64 	void *pvt;
65 	int err;
66 
67 	edac_dbg(4, "instances=%d blocks=%d\n", nr_instances, nr_blocks);
68 
69 	dev_ctl = kzalloc(sizeof(struct edac_device_ctl_info), GFP_KERNEL);
70 	if (!dev_ctl)
71 		return NULL;
72 
73 	dev_inst = kcalloc(nr_instances, sizeof(struct edac_device_instance), GFP_KERNEL);
74 	if (!dev_inst)
75 		goto free;
76 
77 	dev_ctl->instances = dev_inst;
78 
79 	dev_blk = kcalloc(nr_instances * nr_blocks, sizeof(struct edac_device_block), GFP_KERNEL);
80 	if (!dev_blk)
81 		goto free;
82 
83 	dev_ctl->blocks = dev_blk;
84 
85 	if (nr_attrib) {
86 		dev_attrib = kcalloc(nr_attrib, sizeof(struct edac_dev_sysfs_block_attribute),
87 				     GFP_KERNEL);
88 		if (!dev_attrib)
89 			goto free;
90 
91 		dev_ctl->attribs = dev_attrib;
92 	}
93 
94 	if (pvt_sz) {
95 		pvt = kzalloc(pvt_sz, GFP_KERNEL);
96 		if (!pvt)
97 			goto free;
98 
99 		dev_ctl->pvt_info = pvt;
100 	}
101 
102 	dev_ctl->dev_idx	= device_index;
103 	dev_ctl->nr_instances	= nr_instances;
104 
105 	/* Default logging of CEs and UEs */
106 	dev_ctl->log_ce = 1;
107 	dev_ctl->log_ue = 1;
108 
109 	/* Name of this edac device */
110 	snprintf(dev_ctl->name, sizeof(dev_ctl->name),"%s", dev_name);
111 
112 	/* Initialize every Instance */
113 	for (instance = 0; instance < nr_instances; instance++) {
114 		inst = &dev_inst[instance];
115 		inst->ctl = dev_ctl;
116 		inst->nr_blocks = nr_blocks;
117 		blk_p = &dev_blk[instance * nr_blocks];
118 		inst->blocks = blk_p;
119 
120 		/* name of this instance */
121 		snprintf(inst->name, sizeof(inst->name), "%s%u", dev_name, instance);
122 
123 		/* Initialize every block in each instance */
124 		for (block = 0; block < nr_blocks; block++) {
125 			blk = &blk_p[block];
126 			blk->instance = inst;
127 			snprintf(blk->name, sizeof(blk->name),
128 				 "%s%d", blk_name, block + off_val);
129 
130 			edac_dbg(4, "instance=%d inst_p=%p block=#%d block_p=%p name='%s'\n",
131 				 instance, inst, block, blk, blk->name);
132 
133 			/* if there are NO attributes OR no attribute pointer
134 			 * then continue on to next block iteration
135 			 */
136 			if ((nr_attrib == 0) || (attrib_spec == NULL))
137 				continue;
138 
139 			/* setup the attribute array for this block */
140 			blk->nr_attribs = nr_attrib;
141 			attrib_p = &dev_attrib[block*nr_instances*nr_attrib];
142 			blk->block_attributes = attrib_p;
143 
144 			edac_dbg(4, "THIS BLOCK_ATTRIB=%p\n",
145 				 blk->block_attributes);
146 
147 			/* Initialize every user specified attribute in this
148 			 * block with the data the caller passed in
149 			 * Each block gets its own copy of pointers,
150 			 * and its unique 'value'
151 			 */
152 			for (attr = 0; attr < nr_attrib; attr++) {
153 				attrib = &attrib_p[attr];
154 
155 				/* populate the unique per attrib
156 				 * with the code pointers and info
157 				 */
158 				attrib->attr = attrib_spec[attr].attr;
159 				attrib->show = attrib_spec[attr].show;
160 				attrib->store = attrib_spec[attr].store;
161 
162 				attrib->block = blk;	/* up link */
163 
164 				edac_dbg(4, "alloc-attrib=%p attrib_name='%s' attrib-spec=%p spec-name=%s\n",
165 					 attrib, attrib->attr.name,
166 					 &attrib_spec[attr],
167 					 attrib_spec[attr].attr.name
168 					);
169 			}
170 		}
171 	}
172 
173 	/* Mark this instance as merely ALLOCATED */
174 	dev_ctl->op_state = OP_ALLOC;
175 
176 	/*
177 	 * Initialize the 'root' kobj for the edac_device controller
178 	 */
179 	err = edac_device_register_sysfs_main_kobj(dev_ctl);
180 	if (err)
181 		goto free;
182 
183 	/* at this point, the root kobj is valid, and in order to
184 	 * 'free' the object, then the function:
185 	 *	edac_device_unregister_sysfs_main_kobj() must be called
186 	 * which will perform kobj unregistration and the actual free
187 	 * will occur during the kobject callback operation
188 	 */
189 
190 	return dev_ctl;
191 
192 free:
193 	__edac_device_free_ctl_info(dev_ctl);
194 
195 	return NULL;
196 }
197 EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
198 
199 void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
200 {
201 	edac_device_unregister_sysfs_main_kobj(ctl_info);
202 }
203 EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
204 
205 /*
206  * find_edac_device_by_dev
207  *	scans the edac_device list for a specific 'struct device *'
208  *
209  *	lock to be held prior to call:	device_ctls_mutex
210  *
211  *	Return:
212  *		pointer to control structure managing 'dev'
213  *		NULL if not found on list
214  */
215 static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
216 {
217 	struct edac_device_ctl_info *edac_dev;
218 	struct list_head *item;
219 
220 	edac_dbg(0, "\n");
221 
222 	list_for_each(item, &edac_device_list) {
223 		edac_dev = list_entry(item, struct edac_device_ctl_info, link);
224 
225 		if (edac_dev->dev == dev)
226 			return edac_dev;
227 	}
228 
229 	return NULL;
230 }
231 
232 /*
233  * add_edac_dev_to_global_list
234  *	Before calling this function, caller must
235  *	assign a unique value to edac_dev->dev_idx.
236  *
237  *	lock to be held prior to call:	device_ctls_mutex
238  *
239  *	Return:
240  *		0 on success
241  *		1 on failure.
242  */
243 static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
244 {
245 	struct list_head *item, *insert_before;
246 	struct edac_device_ctl_info *rover;
247 
248 	insert_before = &edac_device_list;
249 
250 	/* Determine if already on the list */
251 	rover = find_edac_device_by_dev(edac_dev->dev);
252 	if (unlikely(rover != NULL))
253 		goto fail0;
254 
255 	/* Insert in ascending order by 'dev_idx', so find position */
256 	list_for_each(item, &edac_device_list) {
257 		rover = list_entry(item, struct edac_device_ctl_info, link);
258 
259 		if (rover->dev_idx >= edac_dev->dev_idx) {
260 			if (unlikely(rover->dev_idx == edac_dev->dev_idx))
261 				goto fail1;
262 
263 			insert_before = item;
264 			break;
265 		}
266 	}
267 
268 	list_add_tail_rcu(&edac_dev->link, insert_before);
269 	return 0;
270 
271 fail0:
272 	edac_printk(KERN_WARNING, EDAC_MC,
273 			"%s (%s) %s %s already assigned %d\n",
274 			dev_name(rover->dev), edac_dev_name(rover),
275 			rover->mod_name, rover->ctl_name, rover->dev_idx);
276 	return 1;
277 
278 fail1:
279 	edac_printk(KERN_WARNING, EDAC_MC,
280 			"bug in low-level driver: attempt to assign\n"
281 			"    duplicate dev_idx %d in %s()\n", rover->dev_idx,
282 			__func__);
283 	return 1;
284 }
285 
286 /*
287  * del_edac_device_from_global_list
288  */
289 static void del_edac_device_from_global_list(struct edac_device_ctl_info
290 						*edac_device)
291 {
292 	list_del_rcu(&edac_device->link);
293 
294 	/* these are for safe removal of devices from global list while
295 	 * NMI handlers may be traversing list
296 	 */
297 	synchronize_rcu();
298 	INIT_LIST_HEAD(&edac_device->link);
299 }
300 
301 /*
302  * edac_device_workq_function
303  *	performs the operation scheduled by a workq request
304  *
305  *	this workq is embedded within an edac_device_ctl_info
306  *	structure, that needs to be polled for possible error events.
307  *
308  *	This operation is to acquire the list mutex lock
309  *	(thus preventing insertation or deletion)
310  *	and then call the device's poll function IFF this device is
311  *	running polled and there is a poll function defined.
312  */
313 static void edac_device_workq_function(struct work_struct *work_req)
314 {
315 	struct delayed_work *d_work = to_delayed_work(work_req);
316 	struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
317 
318 	mutex_lock(&device_ctls_mutex);
319 
320 	/* If we are being removed, bail out immediately */
321 	if (edac_dev->op_state == OP_OFFLINE) {
322 		mutex_unlock(&device_ctls_mutex);
323 		return;
324 	}
325 
326 	/* Only poll controllers that are running polled and have a check */
327 	if ((edac_dev->op_state == OP_RUNNING_POLL) &&
328 		(edac_dev->edac_check != NULL)) {
329 			edac_dev->edac_check(edac_dev);
330 	}
331 
332 	mutex_unlock(&device_ctls_mutex);
333 
334 	/* Reschedule the workq for the next time period to start again
335 	 * if the number of msec is for 1 sec, then adjust to the next
336 	 * whole one second to save timers firing all over the period
337 	 * between integral seconds
338 	 */
339 	if (edac_dev->poll_msec == 1000)
340 		edac_queue_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
341 	else
342 		edac_queue_work(&edac_dev->work, edac_dev->delay);
343 }
344 
345 /*
346  * edac_device_workq_setup
347  *	initialize a workq item for this edac_device instance
348  *	passing in the new delay period in msec
349  */
350 static void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
351 				    unsigned msec)
352 {
353 	edac_dbg(0, "\n");
354 
355 	/* take the arg 'msec' and set it into the control structure
356 	 * to used in the time period calculation
357 	 * then calc the number of jiffies that represents
358 	 */
359 	edac_dev->poll_msec = msec;
360 	edac_dev->delay = msecs_to_jiffies(msec);
361 
362 	INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
363 
364 	/* optimize here for the 1 second case, which will be normal value, to
365 	 * fire ON the 1 second time event. This helps reduce all sorts of
366 	 * timers firing on sub-second basis, while they are happy
367 	 * to fire together on the 1 second exactly
368 	 */
369 	if (edac_dev->poll_msec == 1000)
370 		edac_queue_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
371 	else
372 		edac_queue_work(&edac_dev->work, edac_dev->delay);
373 }
374 
375 /*
376  * edac_device_workq_teardown
377  *	stop the workq processing on this edac_dev
378  */
379 static void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
380 {
381 	if (!edac_dev->edac_check)
382 		return;
383 
384 	edac_dev->op_state = OP_OFFLINE;
385 
386 	edac_stop_work(&edac_dev->work);
387 }
388 
389 /*
390  * edac_device_reset_delay_period
391  *
392  *	need to stop any outstanding workq queued up at this time
393  *	because we will be resetting the sleep time.
394  *	Then restart the workq on the new delay
395  */
396 void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
397 				    unsigned long msec)
398 {
399 	edac_dev->poll_msec = msec;
400 	edac_dev->delay	    = msecs_to_jiffies(msec);
401 
402 	/* See comment in edac_device_workq_setup() above */
403 	if (edac_dev->poll_msec == 1000)
404 		edac_mod_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
405 	else
406 		edac_mod_work(&edac_dev->work, edac_dev->delay);
407 }
408 
409 int edac_device_alloc_index(void)
410 {
411 	static atomic_t device_indexes = ATOMIC_INIT(0);
412 
413 	return atomic_inc_return(&device_indexes) - 1;
414 }
415 EXPORT_SYMBOL_GPL(edac_device_alloc_index);
416 
417 int edac_device_add_device(struct edac_device_ctl_info *edac_dev)
418 {
419 	edac_dbg(0, "\n");
420 
421 #ifdef CONFIG_EDAC_DEBUG
422 	if (edac_debug_level >= 3)
423 		edac_device_dump_device(edac_dev);
424 #endif
425 	mutex_lock(&device_ctls_mutex);
426 
427 	if (add_edac_dev_to_global_list(edac_dev))
428 		goto fail0;
429 
430 	/* set load time so that error rate can be tracked */
431 	edac_dev->start_time = jiffies;
432 
433 	/* create this instance's sysfs entries */
434 	if (edac_device_create_sysfs(edac_dev)) {
435 		edac_device_printk(edac_dev, KERN_WARNING,
436 					"failed to create sysfs device\n");
437 		goto fail1;
438 	}
439 
440 	/* If there IS a check routine, then we are running POLLED */
441 	if (edac_dev->edac_check != NULL) {
442 		/* This instance is NOW RUNNING */
443 		edac_dev->op_state = OP_RUNNING_POLL;
444 
445 		/*
446 		 * enable workq processing on this instance,
447 		 * default = 1000 msec
448 		 */
449 		edac_device_workq_setup(edac_dev, 1000);
450 	} else {
451 		edac_dev->op_state = OP_RUNNING_INTERRUPT;
452 	}
453 
454 	/* Report action taken */
455 	edac_device_printk(edac_dev, KERN_INFO,
456 		"Giving out device to module %s controller %s: DEV %s (%s)\n",
457 		edac_dev->mod_name, edac_dev->ctl_name, edac_dev->dev_name,
458 		edac_op_state_to_string(edac_dev->op_state));
459 
460 	mutex_unlock(&device_ctls_mutex);
461 	return 0;
462 
463 fail1:
464 	/* Some error, so remove the entry from the lsit */
465 	del_edac_device_from_global_list(edac_dev);
466 
467 fail0:
468 	mutex_unlock(&device_ctls_mutex);
469 	return 1;
470 }
471 EXPORT_SYMBOL_GPL(edac_device_add_device);
472 
473 struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
474 {
475 	struct edac_device_ctl_info *edac_dev;
476 
477 	edac_dbg(0, "\n");
478 
479 	mutex_lock(&device_ctls_mutex);
480 
481 	/* Find the structure on the list, if not there, then leave */
482 	edac_dev = find_edac_device_by_dev(dev);
483 	if (edac_dev == NULL) {
484 		mutex_unlock(&device_ctls_mutex);
485 		return NULL;
486 	}
487 
488 	/* mark this instance as OFFLINE */
489 	edac_dev->op_state = OP_OFFLINE;
490 
491 	/* deregister from global list */
492 	del_edac_device_from_global_list(edac_dev);
493 
494 	mutex_unlock(&device_ctls_mutex);
495 
496 	/* clear workq processing on this instance */
497 	edac_device_workq_teardown(edac_dev);
498 
499 	/* Tear down the sysfs entries for this instance */
500 	edac_device_remove_sysfs(edac_dev);
501 
502 	edac_printk(KERN_INFO, EDAC_MC,
503 		"Removed device %d for %s %s: DEV %s\n",
504 		edac_dev->dev_idx,
505 		edac_dev->mod_name, edac_dev->ctl_name, edac_dev_name(edac_dev));
506 
507 	return edac_dev;
508 }
509 EXPORT_SYMBOL_GPL(edac_device_del_device);
510 
511 static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
512 {
513 	return edac_dev->log_ce;
514 }
515 
516 static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
517 {
518 	return edac_dev->log_ue;
519 }
520 
521 static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
522 					*edac_dev)
523 {
524 	return edac_dev->panic_on_ue;
525 }
526 
527 void edac_device_handle_ce_count(struct edac_device_ctl_info *edac_dev,
528 				 unsigned int count, int inst_nr, int block_nr,
529 				 const char *msg)
530 {
531 	struct edac_device_instance *instance;
532 	struct edac_device_block *block = NULL;
533 
534 	if (!count)
535 		return;
536 
537 	if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
538 		edac_device_printk(edac_dev, KERN_ERR,
539 				"INTERNAL ERROR: 'instance' out of range "
540 				"(%d >= %d)\n", inst_nr,
541 				edac_dev->nr_instances);
542 		return;
543 	}
544 
545 	instance = edac_dev->instances + inst_nr;
546 
547 	if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
548 		edac_device_printk(edac_dev, KERN_ERR,
549 				"INTERNAL ERROR: instance %d 'block' "
550 				"out of range (%d >= %d)\n",
551 				inst_nr, block_nr,
552 				instance->nr_blocks);
553 		return;
554 	}
555 
556 	if (instance->nr_blocks > 0) {
557 		block = instance->blocks + block_nr;
558 		block->counters.ce_count += count;
559 	}
560 
561 	/* Propagate the count up the 'totals' tree */
562 	instance->counters.ce_count += count;
563 	edac_dev->counters.ce_count += count;
564 
565 	if (edac_device_get_log_ce(edac_dev))
566 		edac_device_printk(edac_dev, KERN_WARNING,
567 				   "CE: %s instance: %s block: %s count: %d '%s'\n",
568 				   edac_dev->ctl_name, instance->name,
569 				   block ? block->name : "N/A", count, msg);
570 }
571 EXPORT_SYMBOL_GPL(edac_device_handle_ce_count);
572 
573 void edac_device_handle_ue_count(struct edac_device_ctl_info *edac_dev,
574 				 unsigned int count, int inst_nr, int block_nr,
575 				 const char *msg)
576 {
577 	struct edac_device_instance *instance;
578 	struct edac_device_block *block = NULL;
579 
580 	if (!count)
581 		return;
582 
583 	if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
584 		edac_device_printk(edac_dev, KERN_ERR,
585 				"INTERNAL ERROR: 'instance' out of range "
586 				"(%d >= %d)\n", inst_nr,
587 				edac_dev->nr_instances);
588 		return;
589 	}
590 
591 	instance = edac_dev->instances + inst_nr;
592 
593 	if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
594 		edac_device_printk(edac_dev, KERN_ERR,
595 				"INTERNAL ERROR: instance %d 'block' "
596 				"out of range (%d >= %d)\n",
597 				inst_nr, block_nr,
598 				instance->nr_blocks);
599 		return;
600 	}
601 
602 	if (instance->nr_blocks > 0) {
603 		block = instance->blocks + block_nr;
604 		block->counters.ue_count += count;
605 	}
606 
607 	/* Propagate the count up the 'totals' tree */
608 	instance->counters.ue_count += count;
609 	edac_dev->counters.ue_count += count;
610 
611 	if (edac_device_get_log_ue(edac_dev))
612 		edac_device_printk(edac_dev, KERN_EMERG,
613 				   "UE: %s instance: %s block: %s count: %d '%s'\n",
614 				   edac_dev->ctl_name, instance->name,
615 				   block ? block->name : "N/A", count, msg);
616 
617 	if (edac_device_get_panic_on_ue(edac_dev))
618 		panic("EDAC %s: UE instance: %s block %s count: %d '%s'\n",
619 		      edac_dev->ctl_name, instance->name,
620 		      block ? block->name : "N/A", count, msg);
621 }
622 EXPORT_SYMBOL_GPL(edac_device_handle_ue_count);
623