xref: /openbmc/linux/drivers/edac/edac_device.c (revision 5352ebf7)
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 value)
398 {
399 	unsigned long jiffs = msecs_to_jiffies(value);
400 
401 	if (value == 1000)
402 		jiffs = round_jiffies_relative(value);
403 
404 	edac_dev->poll_msec = value;
405 	edac_dev->delay	    = jiffs;
406 
407 	edac_mod_work(&edac_dev->work, jiffs);
408 }
409 
410 int edac_device_alloc_index(void)
411 {
412 	static atomic_t device_indexes = ATOMIC_INIT(0);
413 
414 	return atomic_inc_return(&device_indexes) - 1;
415 }
416 EXPORT_SYMBOL_GPL(edac_device_alloc_index);
417 
418 int edac_device_add_device(struct edac_device_ctl_info *edac_dev)
419 {
420 	edac_dbg(0, "\n");
421 
422 #ifdef CONFIG_EDAC_DEBUG
423 	if (edac_debug_level >= 3)
424 		edac_device_dump_device(edac_dev);
425 #endif
426 	mutex_lock(&device_ctls_mutex);
427 
428 	if (add_edac_dev_to_global_list(edac_dev))
429 		goto fail0;
430 
431 	/* set load time so that error rate can be tracked */
432 	edac_dev->start_time = jiffies;
433 
434 	/* create this instance's sysfs entries */
435 	if (edac_device_create_sysfs(edac_dev)) {
436 		edac_device_printk(edac_dev, KERN_WARNING,
437 					"failed to create sysfs device\n");
438 		goto fail1;
439 	}
440 
441 	/* If there IS a check routine, then we are running POLLED */
442 	if (edac_dev->edac_check != NULL) {
443 		/* This instance is NOW RUNNING */
444 		edac_dev->op_state = OP_RUNNING_POLL;
445 
446 		/*
447 		 * enable workq processing on this instance,
448 		 * default = 1000 msec
449 		 */
450 		edac_device_workq_setup(edac_dev, 1000);
451 	} else {
452 		edac_dev->op_state = OP_RUNNING_INTERRUPT;
453 	}
454 
455 	/* Report action taken */
456 	edac_device_printk(edac_dev, KERN_INFO,
457 		"Giving out device to module %s controller %s: DEV %s (%s)\n",
458 		edac_dev->mod_name, edac_dev->ctl_name, edac_dev->dev_name,
459 		edac_op_state_to_string(edac_dev->op_state));
460 
461 	mutex_unlock(&device_ctls_mutex);
462 	return 0;
463 
464 fail1:
465 	/* Some error, so remove the entry from the lsit */
466 	del_edac_device_from_global_list(edac_dev);
467 
468 fail0:
469 	mutex_unlock(&device_ctls_mutex);
470 	return 1;
471 }
472 EXPORT_SYMBOL_GPL(edac_device_add_device);
473 
474 struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
475 {
476 	struct edac_device_ctl_info *edac_dev;
477 
478 	edac_dbg(0, "\n");
479 
480 	mutex_lock(&device_ctls_mutex);
481 
482 	/* Find the structure on the list, if not there, then leave */
483 	edac_dev = find_edac_device_by_dev(dev);
484 	if (edac_dev == NULL) {
485 		mutex_unlock(&device_ctls_mutex);
486 		return NULL;
487 	}
488 
489 	/* mark this instance as OFFLINE */
490 	edac_dev->op_state = OP_OFFLINE;
491 
492 	/* deregister from global list */
493 	del_edac_device_from_global_list(edac_dev);
494 
495 	mutex_unlock(&device_ctls_mutex);
496 
497 	/* clear workq processing on this instance */
498 	edac_device_workq_teardown(edac_dev);
499 
500 	/* Tear down the sysfs entries for this instance */
501 	edac_device_remove_sysfs(edac_dev);
502 
503 	edac_printk(KERN_INFO, EDAC_MC,
504 		"Removed device %d for %s %s: DEV %s\n",
505 		edac_dev->dev_idx,
506 		edac_dev->mod_name, edac_dev->ctl_name, edac_dev_name(edac_dev));
507 
508 	return edac_dev;
509 }
510 EXPORT_SYMBOL_GPL(edac_device_del_device);
511 
512 static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
513 {
514 	return edac_dev->log_ce;
515 }
516 
517 static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
518 {
519 	return edac_dev->log_ue;
520 }
521 
522 static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
523 					*edac_dev)
524 {
525 	return edac_dev->panic_on_ue;
526 }
527 
528 void edac_device_handle_ce_count(struct edac_device_ctl_info *edac_dev,
529 				 unsigned int count, int inst_nr, int block_nr,
530 				 const char *msg)
531 {
532 	struct edac_device_instance *instance;
533 	struct edac_device_block *block = NULL;
534 
535 	if (!count)
536 		return;
537 
538 	if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
539 		edac_device_printk(edac_dev, KERN_ERR,
540 				"INTERNAL ERROR: 'instance' out of range "
541 				"(%d >= %d)\n", inst_nr,
542 				edac_dev->nr_instances);
543 		return;
544 	}
545 
546 	instance = edac_dev->instances + inst_nr;
547 
548 	if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
549 		edac_device_printk(edac_dev, KERN_ERR,
550 				"INTERNAL ERROR: instance %d 'block' "
551 				"out of range (%d >= %d)\n",
552 				inst_nr, block_nr,
553 				instance->nr_blocks);
554 		return;
555 	}
556 
557 	if (instance->nr_blocks > 0) {
558 		block = instance->blocks + block_nr;
559 		block->counters.ce_count += count;
560 	}
561 
562 	/* Propagate the count up the 'totals' tree */
563 	instance->counters.ce_count += count;
564 	edac_dev->counters.ce_count += count;
565 
566 	if (edac_device_get_log_ce(edac_dev))
567 		edac_device_printk(edac_dev, KERN_WARNING,
568 				   "CE: %s instance: %s block: %s count: %d '%s'\n",
569 				   edac_dev->ctl_name, instance->name,
570 				   block ? block->name : "N/A", count, msg);
571 }
572 EXPORT_SYMBOL_GPL(edac_device_handle_ce_count);
573 
574 void edac_device_handle_ue_count(struct edac_device_ctl_info *edac_dev,
575 				 unsigned int count, int inst_nr, int block_nr,
576 				 const char *msg)
577 {
578 	struct edac_device_instance *instance;
579 	struct edac_device_block *block = NULL;
580 
581 	if (!count)
582 		return;
583 
584 	if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
585 		edac_device_printk(edac_dev, KERN_ERR,
586 				"INTERNAL ERROR: 'instance' out of range "
587 				"(%d >= %d)\n", inst_nr,
588 				edac_dev->nr_instances);
589 		return;
590 	}
591 
592 	instance = edac_dev->instances + inst_nr;
593 
594 	if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
595 		edac_device_printk(edac_dev, KERN_ERR,
596 				"INTERNAL ERROR: instance %d 'block' "
597 				"out of range (%d >= %d)\n",
598 				inst_nr, block_nr,
599 				instance->nr_blocks);
600 		return;
601 	}
602 
603 	if (instance->nr_blocks > 0) {
604 		block = instance->blocks + block_nr;
605 		block->counters.ue_count += count;
606 	}
607 
608 	/* Propagate the count up the 'totals' tree */
609 	instance->counters.ue_count += count;
610 	edac_dev->counters.ue_count += count;
611 
612 	if (edac_device_get_log_ue(edac_dev))
613 		edac_device_printk(edac_dev, KERN_EMERG,
614 				   "UE: %s instance: %s block: %s count: %d '%s'\n",
615 				   edac_dev->ctl_name, instance->name,
616 				   block ? block->name : "N/A", count, msg);
617 
618 	if (edac_device_get_panic_on_ue(edac_dev))
619 		panic("EDAC %s: UE instance: %s block %s count: %d '%s'\n",
620 		      edac_dev->ctl_name, instance->name,
621 		      block ? block->name : "N/A", count, msg);
622 }
623 EXPORT_SYMBOL_GPL(edac_device_handle_ue_count);
624