xref: /linux/drivers/edac/edac_device.c (revision bf52fa4a26567bfbf5b1d30f84cf0226e61d26cd)
1e27e3dacSDouglas Thompson 
2e27e3dacSDouglas Thompson /*
3e27e3dacSDouglas Thompson  * edac_device.c
4e27e3dacSDouglas Thompson  * (C) 2007 www.douglaskthompson.com
5e27e3dacSDouglas Thompson  *
6e27e3dacSDouglas Thompson  * This file may be distributed under the terms of the
7e27e3dacSDouglas Thompson  * GNU General Public License.
8e27e3dacSDouglas Thompson  *
9e27e3dacSDouglas Thompson  * Written by Doug Thompson <norsk5@xmission.com>
10e27e3dacSDouglas Thompson  *
11e27e3dacSDouglas Thompson  * edac_device API implementation
12e27e3dacSDouglas Thompson  * 19 Jan 2007
13e27e3dacSDouglas Thompson  */
14e27e3dacSDouglas Thompson 
15e27e3dacSDouglas Thompson #include <linux/module.h>
16e27e3dacSDouglas Thompson #include <linux/types.h>
17e27e3dacSDouglas Thompson #include <linux/smp.h>
18e27e3dacSDouglas Thompson #include <linux/init.h>
19e27e3dacSDouglas Thompson #include <linux/sysctl.h>
20e27e3dacSDouglas Thompson #include <linux/highmem.h>
21e27e3dacSDouglas Thompson #include <linux/timer.h>
22e27e3dacSDouglas Thompson #include <linux/slab.h>
2352490c8dSDouglas Thompson #include <linux/jiffies.h>
24e27e3dacSDouglas Thompson #include <linux/spinlock.h>
25e27e3dacSDouglas Thompson #include <linux/list.h>
26e27e3dacSDouglas Thompson #include <linux/sysdev.h>
27e27e3dacSDouglas Thompson #include <linux/ctype.h>
28e27e3dacSDouglas Thompson #include <linux/workqueue.h>
29e27e3dacSDouglas Thompson #include <asm/uaccess.h>
30e27e3dacSDouglas Thompson #include <asm/page.h>
31e27e3dacSDouglas Thompson 
32e27e3dacSDouglas Thompson #include "edac_core.h"
33e27e3dacSDouglas Thompson #include "edac_module.h"
34e27e3dacSDouglas Thompson 
35*bf52fa4aSDoug Thompson /* lock for the list: 'edac_device_list', manipulation of this list
36*bf52fa4aSDoug Thompson  * is protected by the 'device_ctls_mutex' lock
37*bf52fa4aSDoug Thompson  */
380ca84761SDoug Thompson static DEFINE_MUTEX(device_ctls_mutex);
39e27e3dacSDouglas Thompson static struct list_head edac_device_list = LIST_HEAD_INIT(edac_device_list);
40e27e3dacSDouglas Thompson 
41e27e3dacSDouglas Thompson #ifdef CONFIG_EDAC_DEBUG
42e27e3dacSDouglas Thompson static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
43e27e3dacSDouglas Thompson {
44e27e3dacSDouglas Thompson 	debugf3("\tedac_dev = %p dev_idx=%d \n", edac_dev, edac_dev->dev_idx);
45e27e3dacSDouglas Thompson 	debugf4("\tedac_dev->edac_check = %p\n", edac_dev->edac_check);
46e27e3dacSDouglas Thompson 	debugf3("\tdev = %p\n", edac_dev->dev);
47e27e3dacSDouglas Thompson 	debugf3("\tmod_name:ctl_name = %s:%s\n",
48e27e3dacSDouglas Thompson 		edac_dev->mod_name, edac_dev->ctl_name);
49e27e3dacSDouglas Thompson 	debugf3("\tpvt_info = %p\n\n", edac_dev->pvt_info);
50e27e3dacSDouglas Thompson }
51e27e3dacSDouglas Thompson #endif				/* CONFIG_EDAC_DEBUG */
52e27e3dacSDouglas Thompson 
531c3631ffSDouglas Thompson 
54e27e3dacSDouglas Thompson /*
5552490c8dSDouglas Thompson  * edac_device_alloc_ctl_info()
5652490c8dSDouglas Thompson  *	Allocate a new edac device control info structure
5752490c8dSDouglas Thompson  *
5852490c8dSDouglas Thompson  *	The control structure is allocated in complete chunk
5952490c8dSDouglas Thompson  *	from the OS. It is in turn sub allocated to the
6052490c8dSDouglas Thompson  *	various objects that compose the struture
6152490c8dSDouglas Thompson  *
6252490c8dSDouglas Thompson  *	The structure has a 'nr_instance' array within itself.
6352490c8dSDouglas Thompson  *	Each instance represents a major component
6452490c8dSDouglas Thompson  *		Example:  L1 cache and L2 cache are 2 instance components
6552490c8dSDouglas Thompson  *
6652490c8dSDouglas Thompson  *	Within each instance is an array of 'nr_blocks' blockoffsets
67e27e3dacSDouglas Thompson  */
68e27e3dacSDouglas Thompson struct edac_device_ctl_info *edac_device_alloc_ctl_info(
69e27e3dacSDouglas Thompson 	unsigned sz_private,
7052490c8dSDouglas Thompson 	char *edac_device_name, unsigned nr_instances,
7152490c8dSDouglas Thompson 	char *edac_block_name, unsigned nr_blocks,
7252490c8dSDouglas Thompson 	unsigned offset_value,		/* zero, 1, or other based offset */
73d45e7823SDoug Thompson 	struct edac_dev_sysfs_block_attribute *attrib_spec, unsigned nr_attrib,
74d45e7823SDoug Thompson 	int device_index)
75e27e3dacSDouglas Thompson {
76e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *dev_ctl;
77e27e3dacSDouglas Thompson 	struct edac_device_instance *dev_inst, *inst;
78e27e3dacSDouglas Thompson 	struct edac_device_block *dev_blk, *blk_p, *blk;
79fd309a9dSDouglas Thompson 	struct edac_dev_sysfs_block_attribute *dev_attrib, *attrib_p, *attrib;
80e27e3dacSDouglas Thompson 	unsigned total_size;
81e27e3dacSDouglas Thompson 	unsigned count;
82e27e3dacSDouglas Thompson 	unsigned instance, block, attr;
83e27e3dacSDouglas Thompson 	void *pvt;
841c3631ffSDouglas Thompson 	int err;
85e27e3dacSDouglas Thompson 
86e27e3dacSDouglas Thompson 	debugf1("%s() instances=%d blocks=%d\n",
87e27e3dacSDouglas Thompson 		__func__, nr_instances, nr_blocks);
88e27e3dacSDouglas Thompson 
89fd309a9dSDouglas Thompson 	/* Calculate the size of memory we need to allocate AND
90fd309a9dSDouglas Thompson 	 * determine the offsets of the various item arrays
91fd309a9dSDouglas Thompson 	 * (instance,block,attrib) from the start of an  allocated structure.
92fd309a9dSDouglas Thompson 	 * We want the alignment of each item  (instance,block,attrib)
93e27e3dacSDouglas Thompson 	 * to be at least as stringent as what the compiler would
94e27e3dacSDouglas Thompson 	 * provide if we could simply hardcode everything into a single struct.
95e27e3dacSDouglas Thompson 	 */
9652490c8dSDouglas Thompson 	dev_ctl = (struct edac_device_ctl_info *)NULL;
97e27e3dacSDouglas Thompson 
98fd309a9dSDouglas Thompson 	/* Calc the 'end' offset past end of ONE ctl_info structure
99fd309a9dSDouglas Thompson 	 * which will become the start of the 'instance' array
100fd309a9dSDouglas Thompson 	 */
1017391c6dcSDouglas Thompson 	dev_inst = edac_align_ptr(&dev_ctl[1], sizeof(*dev_inst));
102e27e3dacSDouglas Thompson 
103fd309a9dSDouglas Thompson 	/* Calc the 'end' offset past the instance array within the ctl_info
104fd309a9dSDouglas Thompson 	 * which will become the start of the block array
105fd309a9dSDouglas Thompson 	 */
1067391c6dcSDouglas Thompson 	dev_blk = edac_align_ptr(&dev_inst[nr_instances], sizeof(*dev_blk));
107e27e3dacSDouglas Thompson 
108fd309a9dSDouglas Thompson 	/* Calc the 'end' offset past the dev_blk array
109fd309a9dSDouglas Thompson 	 * which will become the start of the attrib array, if any.
110fd309a9dSDouglas Thompson 	 */
111e27e3dacSDouglas Thompson 	count = nr_instances * nr_blocks;
1127391c6dcSDouglas Thompson 	dev_attrib = edac_align_ptr(&dev_blk[count], sizeof(*dev_attrib));
113e27e3dacSDouglas Thompson 
114fd309a9dSDouglas Thompson 	/* Check for case of when an attribute array is specified */
115fd309a9dSDouglas Thompson 	if (nr_attrib > 0) {
116fd309a9dSDouglas Thompson 		/* calc how many nr_attrib we need */
117fd309a9dSDouglas Thompson 		count *= nr_attrib;
118e27e3dacSDouglas Thompson 
119e27e3dacSDouglas Thompson 		/* Calc the 'end' offset past the attributes array */
120e27e3dacSDouglas Thompson 		pvt = edac_align_ptr(&dev_attrib[count], sz_private);
121fd309a9dSDouglas Thompson 	} else {
122fd309a9dSDouglas Thompson 		/* no attribute array specificed */
123fd309a9dSDouglas Thompson 		pvt = edac_align_ptr(dev_attrib, sz_private);
124fd309a9dSDouglas Thompson 	}
125fd309a9dSDouglas Thompson 
126fd309a9dSDouglas Thompson 	/* 'pvt' now points to where the private data area is.
127fd309a9dSDouglas Thompson 	 * At this point 'pvt' (like dev_inst,dev_blk and dev_attrib)
128fd309a9dSDouglas Thompson 	 * is baselined at ZERO
129fd309a9dSDouglas Thompson 	 */
130e27e3dacSDouglas Thompson 	total_size = ((unsigned long)pvt) + sz_private;
131e27e3dacSDouglas Thompson 
132e27e3dacSDouglas Thompson 	/* Allocate the amount of memory for the set of control structures */
13352490c8dSDouglas Thompson 	dev_ctl = kzalloc(total_size, GFP_KERNEL);
13452490c8dSDouglas Thompson 	if (dev_ctl == NULL)
135e27e3dacSDouglas Thompson 		return NULL;
136e27e3dacSDouglas Thompson 
137fd309a9dSDouglas Thompson 	/* Adjust pointers so they point within the actual memory we
138fd309a9dSDouglas Thompson 	 * just allocated rather than an imaginary chunk of memory
139fd309a9dSDouglas Thompson 	 * located at address 0.
140fd309a9dSDouglas Thompson 	 * 'dev_ctl' points to REAL memory, while the others are
141fd309a9dSDouglas Thompson 	 * ZERO based and thus need to be adjusted to point within
142fd309a9dSDouglas Thompson 	 * the allocated memory.
143e27e3dacSDouglas Thompson 	 */
144e27e3dacSDouglas Thompson 	dev_inst = (struct edac_device_instance *)
145e27e3dacSDouglas Thompson 		(((char *)dev_ctl) + ((unsigned long)dev_inst));
146e27e3dacSDouglas Thompson 	dev_blk = (struct edac_device_block *)
147e27e3dacSDouglas Thompson 		(((char *)dev_ctl) + ((unsigned long)dev_blk));
148fd309a9dSDouglas Thompson 	dev_attrib = (struct edac_dev_sysfs_block_attribute *)
149e27e3dacSDouglas Thompson 		(((char *)dev_ctl) + ((unsigned long)dev_attrib));
150079708b9SDouglas Thompson 	pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;
151e27e3dacSDouglas Thompson 
152fd309a9dSDouglas Thompson 	/* Begin storing the information into the control info structure */
153d45e7823SDoug Thompson 	dev_ctl->dev_idx = device_index;
154e27e3dacSDouglas Thompson 	dev_ctl->nr_instances = nr_instances;
155e27e3dacSDouglas Thompson 	dev_ctl->instances = dev_inst;
156e27e3dacSDouglas Thompson 	dev_ctl->pvt_info = pvt;
157e27e3dacSDouglas Thompson 
15852490c8dSDouglas Thompson 	/* Name of this edac device */
159e27e3dacSDouglas Thompson 	snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s",edac_device_name);
160e27e3dacSDouglas Thompson 
161e27e3dacSDouglas Thompson 	/* Initialize every Instance */
162e27e3dacSDouglas Thompson 	for (instance = 0; instance < nr_instances; instance++) {
163e27e3dacSDouglas Thompson 		inst = &dev_inst[instance];
164e27e3dacSDouglas Thompson 		inst->ctl = dev_ctl;
165e27e3dacSDouglas Thompson 		inst->nr_blocks = nr_blocks;
166e27e3dacSDouglas Thompson 		blk_p = &dev_blk[instance * nr_blocks];
167e27e3dacSDouglas Thompson 		inst->blocks = blk_p;
168e27e3dacSDouglas Thompson 
169e27e3dacSDouglas Thompson 		/* name of this instance */
170e27e3dacSDouglas Thompson 		snprintf(inst->name, sizeof(inst->name),
171e27e3dacSDouglas Thompson 			 "%s%u", edac_device_name, instance);
172e27e3dacSDouglas Thompson 
173e27e3dacSDouglas Thompson 		/* Initialize every block in each instance */
174079708b9SDouglas Thompson 		for (block = 0; block < nr_blocks; block++) {
175e27e3dacSDouglas Thompson 			blk = &blk_p[block];
176e27e3dacSDouglas Thompson 			blk->instance = inst;
177e27e3dacSDouglas Thompson 			snprintf(blk->name, sizeof(blk->name),
178d391a7b8SDouglas Thompson 				 "%s%d", edac_block_name, block+offset_value);
179e27e3dacSDouglas Thompson 
180e27e3dacSDouglas Thompson 			debugf1("%s() instance=%d block=%d name=%s\n",
181e27e3dacSDouglas Thompson 				__func__, instance, block, blk->name);
182e27e3dacSDouglas Thompson 
183fd309a9dSDouglas Thompson 			/* if there are NO attributes OR no attribute pointer
184fd309a9dSDouglas Thompson 			 * then continue on to next block iteration
185e27e3dacSDouglas Thompson 			 */
186fd309a9dSDouglas Thompson 			if ((nr_attrib == 0) || (attrib_spec == NULL))
187fd309a9dSDouglas Thompson 				continue;
188fd309a9dSDouglas Thompson 
189fd309a9dSDouglas Thompson 			/* setup the attribute array for this block */
190fd309a9dSDouglas Thompson 			blk->nr_attribs = nr_attrib;
191fd309a9dSDouglas Thompson 			attrib_p = &dev_attrib[block*nr_instances*nr_attrib];
192fd309a9dSDouglas Thompson 			blk->block_attributes = attrib_p;
193fd309a9dSDouglas Thompson 
194fd309a9dSDouglas Thompson 			/* Initialize every user specified attribute in this
195fd309a9dSDouglas Thompson 			 * block with the data the caller passed in
196fd309a9dSDouglas Thompson 			 */
197fd309a9dSDouglas Thompson 			for (attr = 0; attr < nr_attrib; attr++) {
198e27e3dacSDouglas Thompson 				attrib = &attrib_p[attr];
199fd309a9dSDouglas Thompson 				attrib->attr = attrib_spec->attr;
200fd309a9dSDouglas Thompson 				attrib->show = attrib_spec->show;
201fd309a9dSDouglas Thompson 				attrib->store = attrib_spec->store;
202fd309a9dSDouglas Thompson 
203fd309a9dSDouglas Thompson 				/* up reference this block */
204e27e3dacSDouglas Thompson 				attrib->block = blk;
205e27e3dacSDouglas Thompson 
206fd309a9dSDouglas Thompson 				/* bump the attrib_spec */
207fd309a9dSDouglas Thompson 				attrib_spec++;
208e27e3dacSDouglas Thompson 			}
209e27e3dacSDouglas Thompson 		}
210e27e3dacSDouglas Thompson 	}
211e27e3dacSDouglas Thompson 
212e27e3dacSDouglas Thompson 	/* Mark this instance as merely ALLOCATED */
213e27e3dacSDouglas Thompson 	dev_ctl->op_state = OP_ALLOC;
214e27e3dacSDouglas Thompson 
2151c3631ffSDouglas Thompson 	/*
2161c3631ffSDouglas Thompson 	 * Initialize the 'root' kobj for the edac_device controller
2171c3631ffSDouglas Thompson 	 */
2181c3631ffSDouglas Thompson 	err = edac_device_register_sysfs_main_kobj(dev_ctl);
2191c3631ffSDouglas Thompson 	if (err) {
2201c3631ffSDouglas Thompson 		kfree(dev_ctl);
2211c3631ffSDouglas Thompson 		return NULL;
2221c3631ffSDouglas Thompson 	}
2231c3631ffSDouglas Thompson 
2241c3631ffSDouglas Thompson 	/* at this point, the root kobj is valid, and in order to
2251c3631ffSDouglas Thompson 	 * 'free' the object, then the function:
2261c3631ffSDouglas Thompson 	 *	edac_device_unregister_sysfs_main_kobj() must be called
2271c3631ffSDouglas Thompson 	 * which will perform kobj unregistration and the actual free
2281c3631ffSDouglas Thompson 	 * will occur during the kobject callback operation
2291c3631ffSDouglas Thompson 	 */
2301c3631ffSDouglas Thompson 
231e27e3dacSDouglas Thompson 	return dev_ctl;
232e27e3dacSDouglas Thompson }
233e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
234e27e3dacSDouglas Thompson 
235e27e3dacSDouglas Thompson /*
236e27e3dacSDouglas Thompson  * edac_device_free_ctl_info()
237e27e3dacSDouglas Thompson  *	frees the memory allocated by the edac_device_alloc_ctl_info()
238e27e3dacSDouglas Thompson  *	function
239e27e3dacSDouglas Thompson  */
240079708b9SDouglas Thompson void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
241079708b9SDouglas Thompson {
2421c3631ffSDouglas Thompson 	edac_device_unregister_sysfs_main_kobj(ctl_info);
243e27e3dacSDouglas Thompson }
244e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
245e27e3dacSDouglas Thompson 
246e27e3dacSDouglas Thompson /*
247e27e3dacSDouglas Thompson  * find_edac_device_by_dev
248e27e3dacSDouglas Thompson  *	scans the edac_device list for a specific 'struct device *'
24952490c8dSDouglas Thompson  *
25052490c8dSDouglas Thompson  *	lock to be held prior to call:	device_ctls_mutex
25152490c8dSDouglas Thompson  *
25252490c8dSDouglas Thompson  *	Return:
25352490c8dSDouglas Thompson  *		pointer to control structure managing 'dev'
25452490c8dSDouglas Thompson  *		NULL if not found on list
255e27e3dacSDouglas Thompson  */
256079708b9SDouglas Thompson static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
257e27e3dacSDouglas Thompson {
258e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *edac_dev;
259e27e3dacSDouglas Thompson 	struct list_head *item;
260e27e3dacSDouglas Thompson 
261e27e3dacSDouglas Thompson 	debugf3("%s()\n", __func__);
262e27e3dacSDouglas Thompson 
263e27e3dacSDouglas Thompson 	list_for_each(item, &edac_device_list) {
264e27e3dacSDouglas Thompson 		edac_dev = list_entry(item, struct edac_device_ctl_info, link);
265e27e3dacSDouglas Thompson 
266e27e3dacSDouglas Thompson 		if (edac_dev->dev == dev)
267e27e3dacSDouglas Thompson 			return edac_dev;
268e27e3dacSDouglas Thompson 	}
269e27e3dacSDouglas Thompson 
270e27e3dacSDouglas Thompson 	return NULL;
271e27e3dacSDouglas Thompson }
272e27e3dacSDouglas Thompson 
273e27e3dacSDouglas Thompson /*
274e27e3dacSDouglas Thompson  * add_edac_dev_to_global_list
275e27e3dacSDouglas Thompson  *	Before calling this function, caller must
276e27e3dacSDouglas Thompson  *	assign a unique value to edac_dev->dev_idx.
27752490c8dSDouglas Thompson  *
27852490c8dSDouglas Thompson  *	lock to be held prior to call:	device_ctls_mutex
27952490c8dSDouglas Thompson  *
280e27e3dacSDouglas Thompson  *	Return:
281e27e3dacSDouglas Thompson  *		0 on success
282e27e3dacSDouglas Thompson  *		1 on failure.
283e27e3dacSDouglas Thompson  */
284e27e3dacSDouglas Thompson static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
285e27e3dacSDouglas Thompson {
286e27e3dacSDouglas Thompson 	struct list_head *item, *insert_before;
287e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *rover;
288e27e3dacSDouglas Thompson 
289e27e3dacSDouglas Thompson 	insert_before = &edac_device_list;
290e27e3dacSDouglas Thompson 
291e27e3dacSDouglas Thompson 	/* Determine if already on the list */
29252490c8dSDouglas Thompson 	rover = find_edac_device_by_dev(edac_dev->dev);
29352490c8dSDouglas Thompson 	if (unlikely(rover != NULL))
294e27e3dacSDouglas Thompson 		goto fail0;
295e27e3dacSDouglas Thompson 
296e27e3dacSDouglas Thompson 	/* Insert in ascending order by 'dev_idx', so find position */
297e27e3dacSDouglas Thompson 	list_for_each(item, &edac_device_list) {
298e27e3dacSDouglas Thompson 		rover = list_entry(item, struct edac_device_ctl_info, link);
299e27e3dacSDouglas Thompson 
300e27e3dacSDouglas Thompson 		if (rover->dev_idx >= edac_dev->dev_idx) {
301e27e3dacSDouglas Thompson 			if (unlikely(rover->dev_idx == edac_dev->dev_idx))
302e27e3dacSDouglas Thompson 				goto fail1;
303e27e3dacSDouglas Thompson 
304e27e3dacSDouglas Thompson 			insert_before = item;
305e27e3dacSDouglas Thompson 			break;
306e27e3dacSDouglas Thompson 		}
307e27e3dacSDouglas Thompson 	}
308e27e3dacSDouglas Thompson 
309e27e3dacSDouglas Thompson 	list_add_tail_rcu(&edac_dev->link, insert_before);
310e27e3dacSDouglas Thompson 	return 0;
311e27e3dacSDouglas Thompson 
312e27e3dacSDouglas Thompson fail0:
313e27e3dacSDouglas Thompson 	edac_printk(KERN_WARNING, EDAC_MC,
314e27e3dacSDouglas Thompson 			"%s (%s) %s %s already assigned %d\n",
315c4192705SDave Jiang 			rover->dev->bus_id, dev_name(rover),
316e27e3dacSDouglas Thompson 			rover->mod_name, rover->ctl_name, rover->dev_idx);
317e27e3dacSDouglas Thompson 	return 1;
318e27e3dacSDouglas Thompson 
319e27e3dacSDouglas Thompson fail1:
320e27e3dacSDouglas Thompson 	edac_printk(KERN_WARNING, EDAC_MC,
321e27e3dacSDouglas Thompson 			"bug in low-level driver: attempt to assign\n"
322079708b9SDouglas Thompson 			"    duplicate dev_idx %d in %s()\n", rover->dev_idx,
323079708b9SDouglas Thompson 			__func__);
324e27e3dacSDouglas Thompson 	return 1;
325e27e3dacSDouglas Thompson }
326e27e3dacSDouglas Thompson 
327e27e3dacSDouglas Thompson /*
328e27e3dacSDouglas Thompson  * complete_edac_device_list_del
32952490c8dSDouglas Thompson  *
33052490c8dSDouglas Thompson  *	callback function when reference count is zero
331e27e3dacSDouglas Thompson  */
332e27e3dacSDouglas Thompson static void complete_edac_device_list_del(struct rcu_head *head)
333e27e3dacSDouglas Thompson {
334e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *edac_dev;
335e27e3dacSDouglas Thompson 
336e27e3dacSDouglas Thompson 	edac_dev = container_of(head, struct edac_device_ctl_info, rcu);
337e27e3dacSDouglas Thompson 	INIT_LIST_HEAD(&edac_dev->link);
3381c3631ffSDouglas Thompson 	complete(&edac_dev->removal_complete);
339e27e3dacSDouglas Thompson }
340e27e3dacSDouglas Thompson 
341e27e3dacSDouglas Thompson /*
342e27e3dacSDouglas Thompson  * del_edac_device_from_global_list
34352490c8dSDouglas Thompson  *
3441c3631ffSDouglas Thompson  *	remove the RCU, setup for a callback call,
3451c3631ffSDouglas Thompson  *	then wait for the callback to occur
346e27e3dacSDouglas Thompson  */
347079708b9SDouglas Thompson static void del_edac_device_from_global_list(struct edac_device_ctl_info
348079708b9SDouglas Thompson 						*edac_device)
349e27e3dacSDouglas Thompson {
350e27e3dacSDouglas Thompson 	list_del_rcu(&edac_device->link);
3511c3631ffSDouglas Thompson 
3521c3631ffSDouglas Thompson 	init_completion(&edac_device->removal_complete);
353e27e3dacSDouglas Thompson 	call_rcu(&edac_device->rcu, complete_edac_device_list_del);
3541c3631ffSDouglas Thompson 	wait_for_completion(&edac_device->removal_complete);
355e27e3dacSDouglas Thompson }
356e27e3dacSDouglas Thompson 
357e27e3dacSDouglas Thompson /**
358e27e3dacSDouglas Thompson  * edac_device_find
359e27e3dacSDouglas Thompson  *	Search for a edac_device_ctl_info structure whose index is 'idx'.
360e27e3dacSDouglas Thompson  *
361e27e3dacSDouglas Thompson  * If found, return a pointer to the structure.
362e27e3dacSDouglas Thompson  * Else return NULL.
363e27e3dacSDouglas Thompson  *
364e27e3dacSDouglas Thompson  * Caller must hold device_ctls_mutex.
365e27e3dacSDouglas Thompson  */
366e27e3dacSDouglas Thompson struct edac_device_ctl_info *edac_device_find(int idx)
367e27e3dacSDouglas Thompson {
368e27e3dacSDouglas Thompson 	struct list_head *item;
369e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *edac_dev;
370e27e3dacSDouglas Thompson 
371e27e3dacSDouglas Thompson 	/* Iterate over list, looking for exact match of ID */
372e27e3dacSDouglas Thompson 	list_for_each(item, &edac_device_list) {
373e27e3dacSDouglas Thompson 		edac_dev = list_entry(item, struct edac_device_ctl_info, link);
374e27e3dacSDouglas Thompson 
375e27e3dacSDouglas Thompson 		if (edac_dev->dev_idx >= idx) {
376e27e3dacSDouglas Thompson 			if (edac_dev->dev_idx == idx)
377e27e3dacSDouglas Thompson 				return edac_dev;
378e27e3dacSDouglas Thompson 
379e27e3dacSDouglas Thompson 			/* not on list, so terminate early */
380e27e3dacSDouglas Thompson 			break;
381e27e3dacSDouglas Thompson 		}
382e27e3dacSDouglas Thompson 	}
383e27e3dacSDouglas Thompson 
384e27e3dacSDouglas Thompson 	return NULL;
385e27e3dacSDouglas Thompson }
38652490c8dSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_find);
387e27e3dacSDouglas Thompson 
388e27e3dacSDouglas Thompson /*
38981d87cb1SDave Jiang  * edac_device_workq_function
390e27e3dacSDouglas Thompson  *	performs the operation scheduled by a workq request
391*bf52fa4aSDoug Thompson  *
392*bf52fa4aSDoug Thompson  *	this workq is embedded within an edac_device_ctl_info
393*bf52fa4aSDoug Thompson  *	structure, that needs to be polled for possible error events.
394*bf52fa4aSDoug Thompson  *
395*bf52fa4aSDoug Thompson  *	This operation is to acquire the list mutex lock
396*bf52fa4aSDoug Thompson  *	(thus preventing insertation or deletion)
397*bf52fa4aSDoug Thompson  *	and then call the device's poll function IFF this device is
398*bf52fa4aSDoug Thompson  *	running polled and there is a poll function defined.
399e27e3dacSDouglas Thompson  */
40081d87cb1SDave Jiang static void edac_device_workq_function(struct work_struct *work_req)
401e27e3dacSDouglas Thompson {
402e27e3dacSDouglas Thompson 	struct delayed_work *d_work = (struct delayed_work *)work_req;
403079708b9SDouglas Thompson 	struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
404e27e3dacSDouglas Thompson 
405e27e3dacSDouglas Thompson 	//debugf0("%s() here and running\n", __func__);
4060ca84761SDoug Thompson 	mutex_lock(&device_ctls_mutex);
407e27e3dacSDouglas Thompson 
408e27e3dacSDouglas Thompson 	/* Only poll controllers that are running polled and have a check */
409e27e3dacSDouglas Thompson 	if ((edac_dev->op_state == OP_RUNNING_POLL) &&
410e27e3dacSDouglas Thompson 		(edac_dev->edac_check != NULL)) {
411e27e3dacSDouglas Thompson 			edac_dev->edac_check(edac_dev);
412e27e3dacSDouglas Thompson 	}
413e27e3dacSDouglas Thompson 
4140ca84761SDoug Thompson 	mutex_unlock(&device_ctls_mutex);
415e27e3dacSDouglas Thompson 
416*bf52fa4aSDoug Thompson 	/* Reschedule the workq for the next time period to start again
417*bf52fa4aSDoug Thompson 	 * if the number of msec is for 1 sec, then adjust to the next
418*bf52fa4aSDoug Thompson 	 * whole one second to save timers fireing all over the period
419*bf52fa4aSDoug Thompson 	 * between integral seconds
420*bf52fa4aSDoug Thompson 	 */
421*bf52fa4aSDoug Thompson 	if (edac_dev->poll_msec == 1000)
422*bf52fa4aSDoug Thompson 		queue_delayed_work(edac_workqueue, &edac_dev->work,
423*bf52fa4aSDoug Thompson 				round_jiffies(edac_dev->delay));
424*bf52fa4aSDoug Thompson 	else
425*bf52fa4aSDoug Thompson 		queue_delayed_work(edac_workqueue, &edac_dev->work,
426*bf52fa4aSDoug Thompson 				edac_dev->delay);
427e27e3dacSDouglas Thompson }
428e27e3dacSDouglas Thompson 
429e27e3dacSDouglas Thompson /*
43081d87cb1SDave Jiang  * edac_device_workq_setup
431e27e3dacSDouglas Thompson  *	initialize a workq item for this edac_device instance
432e27e3dacSDouglas Thompson  *	passing in the new delay period in msec
433e27e3dacSDouglas Thompson  */
43481d87cb1SDave Jiang void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
43581d87cb1SDave Jiang 				unsigned msec)
436e27e3dacSDouglas Thompson {
437e27e3dacSDouglas Thompson 	debugf0("%s()\n", __func__);
438e27e3dacSDouglas Thompson 
439*bf52fa4aSDoug Thompson 	/* take the arg 'msec' and set it into the control structure
440*bf52fa4aSDoug Thompson 	 * to used in the time period calculation
441*bf52fa4aSDoug Thompson 	 * then calc the number of jiffies that represents
442*bf52fa4aSDoug Thompson 	 */
443e27e3dacSDouglas Thompson 	edac_dev->poll_msec = msec;
444*bf52fa4aSDoug Thompson 	edac_dev->delay = msecs_to_jiffies(msec);
445e27e3dacSDouglas Thompson 
44681d87cb1SDave Jiang 	INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
447*bf52fa4aSDoug Thompson 
448*bf52fa4aSDoug Thompson 	/* optimize here for the 1 second case, which will be normal value, to
449*bf52fa4aSDoug Thompson 	 * fire ON the 1 second time event. This helps reduce all sorts of
450*bf52fa4aSDoug Thompson 	 * timers firing on sub-second basis, while they are happy
451*bf52fa4aSDoug Thompson 	 * to fire together on the 1 second exactly
452*bf52fa4aSDoug Thompson 	 */
453*bf52fa4aSDoug Thompson 	if (edac_dev->poll_msec == 1000)
454*bf52fa4aSDoug Thompson 		queue_delayed_work(edac_workqueue, &edac_dev->work,
455*bf52fa4aSDoug Thompson 				round_jiffies(edac_dev->delay));
456*bf52fa4aSDoug Thompson 	else
457*bf52fa4aSDoug Thompson 		queue_delayed_work(edac_workqueue, &edac_dev->work,
458*bf52fa4aSDoug Thompson 				edac_dev->delay);
459e27e3dacSDouglas Thompson }
460e27e3dacSDouglas Thompson 
461e27e3dacSDouglas Thompson /*
46281d87cb1SDave Jiang  * edac_device_workq_teardown
463e27e3dacSDouglas Thompson  *	stop the workq processing on this edac_dev
464e27e3dacSDouglas Thompson  */
46581d87cb1SDave Jiang void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
466e27e3dacSDouglas Thompson {
467e27e3dacSDouglas Thompson 	int status;
468e27e3dacSDouglas Thompson 
469e27e3dacSDouglas Thompson 	status = cancel_delayed_work(&edac_dev->work);
470e27e3dacSDouglas Thompson 	if (status == 0) {
471e27e3dacSDouglas Thompson 		/* workq instance might be running, wait for it */
472e27e3dacSDouglas Thompson 		flush_workqueue(edac_workqueue);
473e27e3dacSDouglas Thompson 	}
474e27e3dacSDouglas Thompson }
475e27e3dacSDouglas Thompson 
476e27e3dacSDouglas Thompson /*
477e27e3dacSDouglas Thompson  * edac_device_reset_delay_period
478*bf52fa4aSDoug Thompson  *
479*bf52fa4aSDoug Thompson  *	need to stop any outstanding workq queued up at this time
480*bf52fa4aSDoug Thompson  *	because we will be resetting the sleep time.
481*bf52fa4aSDoug Thompson  *	Then restart the workq on the new delay
482e27e3dacSDouglas Thompson  */
483079708b9SDouglas Thompson void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
484e27e3dacSDouglas Thompson 					unsigned long value)
485e27e3dacSDouglas Thompson {
486*bf52fa4aSDoug Thompson 	/* cancel the current workq request, without the mutex lock */
48781d87cb1SDave Jiang 	edac_device_workq_teardown(edac_dev);
488e27e3dacSDouglas Thompson 
489*bf52fa4aSDoug Thompson 	/* acquire the mutex before doing the workq setup */
490*bf52fa4aSDoug Thompson 	mutex_lock(&device_ctls_mutex);
491*bf52fa4aSDoug Thompson 
492e27e3dacSDouglas Thompson 	/* restart the workq request, with new delay value */
49381d87cb1SDave Jiang 	edac_device_workq_setup(edac_dev, value);
494e27e3dacSDouglas Thompson 
4950ca84761SDoug Thompson 	mutex_unlock(&device_ctls_mutex);
496e27e3dacSDouglas Thompson }
497e27e3dacSDouglas Thompson 
498e27e3dacSDouglas Thompson /**
499e27e3dacSDouglas Thompson  * edac_device_add_device: Insert the 'edac_dev' structure into the
500e27e3dacSDouglas Thompson  * edac_device global list and create sysfs entries associated with
501e27e3dacSDouglas Thompson  * edac_device structure.
502e27e3dacSDouglas Thompson  * @edac_device: pointer to the edac_device structure to be added to the list
503e27e3dacSDouglas Thompson  * 'edac_device' structure.
504e27e3dacSDouglas Thompson  *
505e27e3dacSDouglas Thompson  * Return:
506e27e3dacSDouglas Thompson  *	0	Success
507e27e3dacSDouglas Thompson  *	!0	Failure
508e27e3dacSDouglas Thompson  */
509d45e7823SDoug Thompson int edac_device_add_device(struct edac_device_ctl_info *edac_dev)
510e27e3dacSDouglas Thompson {
511e27e3dacSDouglas Thompson 	debugf0("%s()\n", __func__);
512e27e3dacSDouglas Thompson 
513e27e3dacSDouglas Thompson #ifdef CONFIG_EDAC_DEBUG
514e27e3dacSDouglas Thompson 	if (edac_debug_level >= 3)
515e27e3dacSDouglas Thompson 		edac_device_dump_device(edac_dev);
516e27e3dacSDouglas Thompson #endif
5170ca84761SDoug Thompson 	mutex_lock(&device_ctls_mutex);
518e27e3dacSDouglas Thompson 
519e27e3dacSDouglas Thompson 	if (add_edac_dev_to_global_list(edac_dev))
520e27e3dacSDouglas Thompson 		goto fail0;
521e27e3dacSDouglas Thompson 
522e27e3dacSDouglas Thompson 	/* set load time so that error rate can be tracked */
523e27e3dacSDouglas Thompson 	edac_dev->start_time = jiffies;
524e27e3dacSDouglas Thompson 
525e27e3dacSDouglas Thompson 	/* create this instance's sysfs entries */
526e27e3dacSDouglas Thompson 	if (edac_device_create_sysfs(edac_dev)) {
527e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_WARNING,
528e27e3dacSDouglas Thompson 					"failed to create sysfs device\n");
529e27e3dacSDouglas Thompson 		goto fail1;
530e27e3dacSDouglas Thompson 	}
531e27e3dacSDouglas Thompson 
532e27e3dacSDouglas Thompson 	/* If there IS a check routine, then we are running POLLED */
533e27e3dacSDouglas Thompson 	if (edac_dev->edac_check != NULL) {
534e27e3dacSDouglas Thompson 		/* This instance is NOW RUNNING */
535e27e3dacSDouglas Thompson 		edac_dev->op_state = OP_RUNNING_POLL;
536e27e3dacSDouglas Thompson 
53781d87cb1SDave Jiang 		/*
53881d87cb1SDave Jiang 		 * enable workq processing on this instance,
53981d87cb1SDave Jiang 		 * default = 1000 msec
54081d87cb1SDave Jiang 		 */
54181d87cb1SDave Jiang 		edac_device_workq_setup(edac_dev, 1000);
542e27e3dacSDouglas Thompson 	} else {
543e27e3dacSDouglas Thompson 		edac_dev->op_state = OP_RUNNING_INTERRUPT;
544e27e3dacSDouglas Thompson 	}
545e27e3dacSDouglas Thompson 
546e27e3dacSDouglas Thompson 	/* Report action taken */
547e27e3dacSDouglas Thompson 	edac_device_printk(edac_dev, KERN_INFO,
548052dfb45SDouglas Thompson 				"Giving out device to module '%s' controller "
549052dfb45SDouglas Thompson 				"'%s': DEV '%s' (%s)\n",
550e27e3dacSDouglas Thompson 				edac_dev->mod_name,
551e27e3dacSDouglas Thompson 				edac_dev->ctl_name,
552c4192705SDave Jiang 				dev_name(edac_dev),
553494d0d55SDouglas Thompson 				edac_op_state_to_string(edac_dev->op_state));
554e27e3dacSDouglas Thompson 
5550ca84761SDoug Thompson 	mutex_unlock(&device_ctls_mutex);
556e27e3dacSDouglas Thompson 	return 0;
557e27e3dacSDouglas Thompson 
558e27e3dacSDouglas Thompson fail1:
559e27e3dacSDouglas Thompson 	/* Some error, so remove the entry from the lsit */
560e27e3dacSDouglas Thompson 	del_edac_device_from_global_list(edac_dev);
561e27e3dacSDouglas Thompson 
562e27e3dacSDouglas Thompson fail0:
5630ca84761SDoug Thompson 	mutex_unlock(&device_ctls_mutex);
564e27e3dacSDouglas Thompson 	return 1;
565e27e3dacSDouglas Thompson }
566e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_add_device);
567e27e3dacSDouglas Thompson 
568e27e3dacSDouglas Thompson /**
569e27e3dacSDouglas Thompson  * edac_device_del_device:
570e27e3dacSDouglas Thompson  *	Remove sysfs entries for specified edac_device structure and
571e27e3dacSDouglas Thompson  *	then remove edac_device structure from global list
572e27e3dacSDouglas Thompson  *
573e27e3dacSDouglas Thompson  * @pdev:
574e27e3dacSDouglas Thompson  *	Pointer to 'struct device' representing edac_device
575e27e3dacSDouglas Thompson  *	structure to remove.
576e27e3dacSDouglas Thompson  *
577e27e3dacSDouglas Thompson  * Return:
578e27e3dacSDouglas Thompson  *	Pointer to removed edac_device structure,
579e27e3dacSDouglas Thompson  *	OR NULL if device not found.
580e27e3dacSDouglas Thompson  */
581e27e3dacSDouglas Thompson struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
582e27e3dacSDouglas Thompson {
583e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *edac_dev;
584e27e3dacSDouglas Thompson 
585e27e3dacSDouglas Thompson 	debugf0("MC: %s()\n", __func__);
586e27e3dacSDouglas Thompson 
5870ca84761SDoug Thompson 	mutex_lock(&device_ctls_mutex);
588e27e3dacSDouglas Thompson 
58952490c8dSDouglas Thompson 	/* Find the structure on the list, if not there, then leave */
59052490c8dSDouglas Thompson 	edac_dev = find_edac_device_by_dev(dev);
59152490c8dSDouglas Thompson 	if (edac_dev == NULL) {
5920ca84761SDoug Thompson 		mutex_unlock(&device_ctls_mutex);
593e27e3dacSDouglas Thompson 		return NULL;
594e27e3dacSDouglas Thompson 	}
595e27e3dacSDouglas Thompson 
596e27e3dacSDouglas Thompson 	/* mark this instance as OFFLINE */
597e27e3dacSDouglas Thompson 	edac_dev->op_state = OP_OFFLINE;
598e27e3dacSDouglas Thompson 
599e27e3dacSDouglas Thompson 	/* clear workq processing on this instance */
60081d87cb1SDave Jiang 	edac_device_workq_teardown(edac_dev);
601e27e3dacSDouglas Thompson 
602e27e3dacSDouglas Thompson 	/* deregister from global list */
603e27e3dacSDouglas Thompson 	del_edac_device_from_global_list(edac_dev);
604e27e3dacSDouglas Thompson 
6050ca84761SDoug Thompson 	mutex_unlock(&device_ctls_mutex);
606e27e3dacSDouglas Thompson 
6071c3631ffSDouglas Thompson 	/* Tear down the sysfs entries for this instance */
6081c3631ffSDouglas Thompson 	edac_device_remove_sysfs(edac_dev);
6091c3631ffSDouglas Thompson 
610e27e3dacSDouglas Thompson 	edac_printk(KERN_INFO, EDAC_MC,
611e27e3dacSDouglas Thompson 		"Removed device %d for %s %s: DEV %s\n",
612e27e3dacSDouglas Thompson 		edac_dev->dev_idx,
613079708b9SDouglas Thompson 		edac_dev->mod_name, edac_dev->ctl_name, dev_name(edac_dev));
614e27e3dacSDouglas Thompson 
615e27e3dacSDouglas Thompson 	return edac_dev;
616e27e3dacSDouglas Thompson }
617079708b9SDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_del_device);
618e27e3dacSDouglas Thompson 
619e27e3dacSDouglas Thompson static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
620e27e3dacSDouglas Thompson {
621e27e3dacSDouglas Thompson 	return edac_dev->log_ce;
622e27e3dacSDouglas Thompson }
623e27e3dacSDouglas Thompson 
624e27e3dacSDouglas Thompson static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
625e27e3dacSDouglas Thompson {
626e27e3dacSDouglas Thompson 	return edac_dev->log_ue;
627e27e3dacSDouglas Thompson }
628e27e3dacSDouglas Thompson 
629079708b9SDouglas Thompson static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
630079708b9SDouglas Thompson 					*edac_dev)
631e27e3dacSDouglas Thompson {
632e27e3dacSDouglas Thompson 	return edac_dev->panic_on_ue;
633e27e3dacSDouglas Thompson }
634e27e3dacSDouglas Thompson 
635e27e3dacSDouglas Thompson /*
636e27e3dacSDouglas Thompson  * edac_device_handle_ce
637e27e3dacSDouglas Thompson  *	perform a common output and handling of an 'edac_dev' CE event
638e27e3dacSDouglas Thompson  */
639e27e3dacSDouglas Thompson void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
640e27e3dacSDouglas Thompson 			int inst_nr, int block_nr, const char *msg)
641e27e3dacSDouglas Thompson {
642e27e3dacSDouglas Thompson 	struct edac_device_instance *instance;
643e27e3dacSDouglas Thompson 	struct edac_device_block *block = NULL;
644e27e3dacSDouglas Thompson 
645e27e3dacSDouglas Thompson 	if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
646e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_ERR,
647e27e3dacSDouglas Thompson 				"INTERNAL ERROR: 'instance' out of range "
648079708b9SDouglas Thompson 				"(%d >= %d)\n", inst_nr,
649079708b9SDouglas Thompson 				edac_dev->nr_instances);
650e27e3dacSDouglas Thompson 		return;
651e27e3dacSDouglas Thompson 	}
652e27e3dacSDouglas Thompson 
653e27e3dacSDouglas Thompson 	instance = edac_dev->instances + inst_nr;
654e27e3dacSDouglas Thompson 
655e27e3dacSDouglas Thompson 	if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
656e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_ERR,
657052dfb45SDouglas Thompson 				"INTERNAL ERROR: instance %d 'block' "
658052dfb45SDouglas Thompson 				"out of range (%d >= %d)\n",
659052dfb45SDouglas Thompson 				inst_nr, block_nr,
660079708b9SDouglas Thompson 				instance->nr_blocks);
661e27e3dacSDouglas Thompson 		return;
662e27e3dacSDouglas Thompson 	}
663e27e3dacSDouglas Thompson 
664e27e3dacSDouglas Thompson 	if (instance->nr_blocks > 0) {
665e27e3dacSDouglas Thompson 		block = instance->blocks + block_nr;
666e27e3dacSDouglas Thompson 		block->counters.ce_count++;
667e27e3dacSDouglas Thompson 	}
668e27e3dacSDouglas Thompson 
669e27e3dacSDouglas Thompson 	/* Propogate the count up the 'totals' tree */
670e27e3dacSDouglas Thompson 	instance->counters.ce_count++;
671e27e3dacSDouglas Thompson 	edac_dev->counters.ce_count++;
672e27e3dacSDouglas Thompson 
673e27e3dacSDouglas Thompson 	if (edac_device_get_log_ce(edac_dev))
674e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_WARNING,
675d391a7b8SDouglas Thompson 				"CE: %s instance: %s block: %s '%s'\n",
676e27e3dacSDouglas Thompson 				edac_dev->ctl_name, instance->name,
677e27e3dacSDouglas Thompson 				block ? block->name : "N/A", msg);
678e27e3dacSDouglas Thompson }
679e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_handle_ce);
680e27e3dacSDouglas Thompson 
681e27e3dacSDouglas Thompson /*
682e27e3dacSDouglas Thompson  * edac_device_handle_ue
683e27e3dacSDouglas Thompson  *	perform a common output and handling of an 'edac_dev' UE event
684e27e3dacSDouglas Thompson  */
685e27e3dacSDouglas Thompson void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
686e27e3dacSDouglas Thompson 			int inst_nr, int block_nr, const char *msg)
687e27e3dacSDouglas Thompson {
688e27e3dacSDouglas Thompson 	struct edac_device_instance *instance;
689e27e3dacSDouglas Thompson 	struct edac_device_block *block = NULL;
690e27e3dacSDouglas Thompson 
691e27e3dacSDouglas Thompson 	if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
692e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_ERR,
693e27e3dacSDouglas Thompson 				"INTERNAL ERROR: 'instance' out of range "
694079708b9SDouglas Thompson 				"(%d >= %d)\n", inst_nr,
695079708b9SDouglas Thompson 				edac_dev->nr_instances);
696e27e3dacSDouglas Thompson 		return;
697e27e3dacSDouglas Thompson 	}
698e27e3dacSDouglas Thompson 
699e27e3dacSDouglas Thompson 	instance = edac_dev->instances + inst_nr;
700e27e3dacSDouglas Thompson 
701e27e3dacSDouglas Thompson 	if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
702e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_ERR,
703052dfb45SDouglas Thompson 				"INTERNAL ERROR: instance %d 'block' "
704052dfb45SDouglas Thompson 				"out of range (%d >= %d)\n",
705052dfb45SDouglas Thompson 				inst_nr, block_nr,
706079708b9SDouglas Thompson 				instance->nr_blocks);
707e27e3dacSDouglas Thompson 		return;
708e27e3dacSDouglas Thompson 	}
709e27e3dacSDouglas Thompson 
710e27e3dacSDouglas Thompson 	if (instance->nr_blocks > 0) {
711e27e3dacSDouglas Thompson 		block = instance->blocks + block_nr;
712e27e3dacSDouglas Thompson 		block->counters.ue_count++;
713e27e3dacSDouglas Thompson 	}
714e27e3dacSDouglas Thompson 
715e27e3dacSDouglas Thompson 	/* Propogate the count up the 'totals' tree */
716e27e3dacSDouglas Thompson 	instance->counters.ue_count++;
717e27e3dacSDouglas Thompson 	edac_dev->counters.ue_count++;
718e27e3dacSDouglas Thompson 
719e27e3dacSDouglas Thompson 	if (edac_device_get_log_ue(edac_dev))
720e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_EMERG,
721d391a7b8SDouglas Thompson 				"UE: %s instance: %s block: %s '%s'\n",
722e27e3dacSDouglas Thompson 				edac_dev->ctl_name, instance->name,
723e27e3dacSDouglas Thompson 				block ? block->name : "N/A", msg);
724e27e3dacSDouglas Thompson 
725e27e3dacSDouglas Thompson 	if (edac_device_get_panic_on_ue(edac_dev))
726d391a7b8SDouglas Thompson 		panic("EDAC %s: UE instance: %s block %s '%s'\n",
727e27e3dacSDouglas Thompson 			edac_dev->ctl_name, instance->name,
728e27e3dacSDouglas Thompson 			block ? block->name : "N/A", msg);
729e27e3dacSDouglas Thompson }
730079708b9SDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_handle_ue);
731