xref: /linux/drivers/edac/edac_device.c (revision 93e4fe64ece4eccf0ff4ac69bceb389290b8ab7c)
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/ctype.h>
27e27e3dacSDouglas Thompson #include <linux/workqueue.h>
28e27e3dacSDouglas Thompson #include <asm/uaccess.h>
29e27e3dacSDouglas Thompson #include <asm/page.h>
30e27e3dacSDouglas Thompson 
31e27e3dacSDouglas Thompson #include "edac_core.h"
32e27e3dacSDouglas Thompson #include "edac_module.h"
33e27e3dacSDouglas Thompson 
34bf52fa4aSDoug Thompson /* lock for the list: 'edac_device_list', manipulation of this list
35bf52fa4aSDoug Thompson  * is protected by the 'device_ctls_mutex' lock
36bf52fa4aSDoug Thompson  */
370ca84761SDoug Thompson static DEFINE_MUTEX(device_ctls_mutex);
38ff6ac2a6SRobert P. J. Day static LIST_HEAD(edac_device_list);
39e27e3dacSDouglas Thompson 
40e27e3dacSDouglas Thompson #ifdef CONFIG_EDAC_DEBUG
41e27e3dacSDouglas Thompson static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
42e27e3dacSDouglas Thompson {
43e27e3dacSDouglas Thompson 	debugf3("\tedac_dev = %p dev_idx=%d \n", edac_dev, edac_dev->dev_idx);
44e27e3dacSDouglas Thompson 	debugf4("\tedac_dev->edac_check = %p\n", edac_dev->edac_check);
45e27e3dacSDouglas Thompson 	debugf3("\tdev = %p\n", edac_dev->dev);
46e27e3dacSDouglas Thompson 	debugf3("\tmod_name:ctl_name = %s:%s\n",
47e27e3dacSDouglas Thompson 		edac_dev->mod_name, edac_dev->ctl_name);
48e27e3dacSDouglas Thompson 	debugf3("\tpvt_info = %p\n\n", edac_dev->pvt_info);
49e27e3dacSDouglas Thompson }
50e27e3dacSDouglas Thompson #endif				/* CONFIG_EDAC_DEBUG */
51e27e3dacSDouglas Thompson 
521c3631ffSDouglas Thompson 
53e27e3dacSDouglas Thompson /*
5452490c8dSDouglas Thompson  * edac_device_alloc_ctl_info()
5552490c8dSDouglas Thompson  *	Allocate a new edac device control info structure
5652490c8dSDouglas Thompson  *
5752490c8dSDouglas Thompson  *	The control structure is allocated in complete chunk
5852490c8dSDouglas Thompson  *	from the OS. It is in turn sub allocated to the
5952490c8dSDouglas Thompson  *	various objects that compose the struture
6052490c8dSDouglas Thompson  *
6152490c8dSDouglas Thompson  *	The structure has a 'nr_instance' array within itself.
6252490c8dSDouglas Thompson  *	Each instance represents a major component
6352490c8dSDouglas Thompson  *		Example:  L1 cache and L2 cache are 2 instance components
6452490c8dSDouglas Thompson  *
6552490c8dSDouglas Thompson  *	Within each instance is an array of 'nr_blocks' blockoffsets
66e27e3dacSDouglas Thompson  */
67e27e3dacSDouglas Thompson struct edac_device_ctl_info *edac_device_alloc_ctl_info(
68e27e3dacSDouglas Thompson 	unsigned sz_private,
6952490c8dSDouglas Thompson 	char *edac_device_name, unsigned nr_instances,
7052490c8dSDouglas Thompson 	char *edac_block_name, unsigned nr_blocks,
7152490c8dSDouglas Thompson 	unsigned offset_value,		/* zero, 1, or other based offset */
72d45e7823SDoug Thompson 	struct edac_dev_sysfs_block_attribute *attrib_spec, unsigned nr_attrib,
73d45e7823SDoug Thompson 	int device_index)
74e27e3dacSDouglas Thompson {
75e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *dev_ctl;
76e27e3dacSDouglas Thompson 	struct edac_device_instance *dev_inst, *inst;
77e27e3dacSDouglas Thompson 	struct edac_device_block *dev_blk, *blk_p, *blk;
78fd309a9dSDouglas Thompson 	struct edac_dev_sysfs_block_attribute *dev_attrib, *attrib_p, *attrib;
79e27e3dacSDouglas Thompson 	unsigned total_size;
80e27e3dacSDouglas Thompson 	unsigned count;
81e27e3dacSDouglas Thompson 	unsigned instance, block, attr;
82*93e4fe64SMauro Carvalho Chehab 	void *pvt, *p;
831c3631ffSDouglas Thompson 	int err;
84e27e3dacSDouglas Thompson 
85b2a4ac0cSDoug Thompson 	debugf4("%s() instances=%d blocks=%d\n",
86e27e3dacSDouglas Thompson 		__func__, nr_instances, nr_blocks);
87e27e3dacSDouglas Thompson 
88fd309a9dSDouglas Thompson 	/* Calculate the size of memory we need to allocate AND
89fd309a9dSDouglas Thompson 	 * determine the offsets of the various item arrays
90fd309a9dSDouglas Thompson 	 * (instance,block,attrib) from the start of an  allocated structure.
91fd309a9dSDouglas Thompson 	 * We want the alignment of each item  (instance,block,attrib)
92e27e3dacSDouglas Thompson 	 * to be at least as stringent as what the compiler would
93e27e3dacSDouglas Thompson 	 * provide if we could simply hardcode everything into a single struct.
94e27e3dacSDouglas Thompson 	 */
95*93e4fe64SMauro Carvalho Chehab 	p = NULL;
96*93e4fe64SMauro Carvalho Chehab 	dev_ctl = edac_align_ptr(&p, sizeof(*dev_ctl), 1);
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 	 */
101*93e4fe64SMauro Carvalho Chehab 	dev_inst = edac_align_ptr(&p, sizeof(*dev_inst), nr_instances);
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 	 */
106*93e4fe64SMauro Carvalho Chehab 	count = nr_instances * nr_blocks;
107*93e4fe64SMauro Carvalho Chehab 	dev_blk = edac_align_ptr(&p, sizeof(*dev_blk), count);
108e27e3dacSDouglas Thompson 
109fd309a9dSDouglas Thompson 	/* Calc the 'end' offset past the dev_blk array
110fd309a9dSDouglas Thompson 	 * which will become the start of the attrib array, if any.
111fd309a9dSDouglas Thompson 	 */
112fd309a9dSDouglas Thompson 	/* calc how many nr_attrib we need */
113*93e4fe64SMauro Carvalho Chehab 	if (nr_attrib > 0)
114fd309a9dSDouglas Thompson 		count *= nr_attrib;
115*93e4fe64SMauro Carvalho Chehab 	dev_attrib = edac_align_ptr(&p, sizeof(*dev_attrib), count);
116e27e3dacSDouglas Thompson 
117e27e3dacSDouglas Thompson 	/* Calc the 'end' offset past the attributes array */
118*93e4fe64SMauro Carvalho Chehab 	pvt = edac_align_ptr(&p, sz_private, 1);
119fd309a9dSDouglas Thompson 
120fd309a9dSDouglas Thompson 	/* 'pvt' now points to where the private data area is.
121fd309a9dSDouglas Thompson 	 * At this point 'pvt' (like dev_inst,dev_blk and dev_attrib)
122fd309a9dSDouglas Thompson 	 * is baselined at ZERO
123fd309a9dSDouglas Thompson 	 */
124e27e3dacSDouglas Thompson 	total_size = ((unsigned long)pvt) + sz_private;
125e27e3dacSDouglas Thompson 
126e27e3dacSDouglas Thompson 	/* Allocate the amount of memory for the set of control structures */
12752490c8dSDouglas Thompson 	dev_ctl = kzalloc(total_size, GFP_KERNEL);
12852490c8dSDouglas Thompson 	if (dev_ctl == NULL)
129e27e3dacSDouglas Thompson 		return NULL;
130e27e3dacSDouglas Thompson 
131fd309a9dSDouglas Thompson 	/* Adjust pointers so they point within the actual memory we
132fd309a9dSDouglas Thompson 	 * just allocated rather than an imaginary chunk of memory
133fd309a9dSDouglas Thompson 	 * located at address 0.
134fd309a9dSDouglas Thompson 	 * 'dev_ctl' points to REAL memory, while the others are
135fd309a9dSDouglas Thompson 	 * ZERO based and thus need to be adjusted to point within
136fd309a9dSDouglas Thompson 	 * the allocated memory.
137e27e3dacSDouglas Thompson 	 */
138e27e3dacSDouglas Thompson 	dev_inst = (struct edac_device_instance *)
139e27e3dacSDouglas Thompson 		(((char *)dev_ctl) + ((unsigned long)dev_inst));
140e27e3dacSDouglas Thompson 	dev_blk = (struct edac_device_block *)
141e27e3dacSDouglas Thompson 		(((char *)dev_ctl) + ((unsigned long)dev_blk));
142fd309a9dSDouglas Thompson 	dev_attrib = (struct edac_dev_sysfs_block_attribute *)
143e27e3dacSDouglas Thompson 		(((char *)dev_ctl) + ((unsigned long)dev_attrib));
144079708b9SDouglas Thompson 	pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;
145e27e3dacSDouglas Thompson 
146fd309a9dSDouglas Thompson 	/* Begin storing the information into the control info structure */
147d45e7823SDoug Thompson 	dev_ctl->dev_idx = device_index;
148e27e3dacSDouglas Thompson 	dev_ctl->nr_instances = nr_instances;
149e27e3dacSDouglas Thompson 	dev_ctl->instances = dev_inst;
150e27e3dacSDouglas Thompson 	dev_ctl->pvt_info = pvt;
151e27e3dacSDouglas Thompson 
15256e61a9cSDoug Thompson 	/* Default logging of CEs and UEs */
15356e61a9cSDoug Thompson 	dev_ctl->log_ce = 1;
15456e61a9cSDoug Thompson 	dev_ctl->log_ue = 1;
15556e61a9cSDoug Thompson 
15652490c8dSDouglas Thompson 	/* Name of this edac device */
157e27e3dacSDouglas Thompson 	snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s",edac_device_name);
158e27e3dacSDouglas Thompson 
159b2a4ac0cSDoug Thompson 	debugf4("%s() edac_dev=%p next after end=%p\n",
160b2a4ac0cSDoug Thompson 		__func__, dev_ctl, pvt + sz_private );
161b2a4ac0cSDoug Thompson 
162e27e3dacSDouglas Thompson 	/* Initialize every Instance */
163e27e3dacSDouglas Thompson 	for (instance = 0; instance < nr_instances; instance++) {
164e27e3dacSDouglas Thompson 		inst = &dev_inst[instance];
165e27e3dacSDouglas Thompson 		inst->ctl = dev_ctl;
166e27e3dacSDouglas Thompson 		inst->nr_blocks = nr_blocks;
167e27e3dacSDouglas Thompson 		blk_p = &dev_blk[instance * nr_blocks];
168e27e3dacSDouglas Thompson 		inst->blocks = blk_p;
169e27e3dacSDouglas Thompson 
170e27e3dacSDouglas Thompson 		/* name of this instance */
171e27e3dacSDouglas Thompson 		snprintf(inst->name, sizeof(inst->name),
172e27e3dacSDouglas Thompson 			 "%s%u", edac_device_name, instance);
173e27e3dacSDouglas Thompson 
174e27e3dacSDouglas Thompson 		/* Initialize every block in each instance */
175079708b9SDouglas Thompson 		for (block = 0; block < nr_blocks; block++) {
176e27e3dacSDouglas Thompson 			blk = &blk_p[block];
177e27e3dacSDouglas Thompson 			blk->instance = inst;
178e27e3dacSDouglas Thompson 			snprintf(blk->name, sizeof(blk->name),
179d391a7b8SDouglas Thompson 				 "%s%d", edac_block_name, block+offset_value);
180e27e3dacSDouglas Thompson 
181b2a4ac0cSDoug Thompson 			debugf4("%s() instance=%d inst_p=%p block=#%d "
182b2a4ac0cSDoug Thompson 				"block_p=%p name='%s'\n",
183b2a4ac0cSDoug Thompson 				__func__, instance, inst, block,
184b2a4ac0cSDoug Thompson 				blk, blk->name);
185e27e3dacSDouglas Thompson 
186fd309a9dSDouglas Thompson 			/* if there are NO attributes OR no attribute pointer
187fd309a9dSDouglas Thompson 			 * then continue on to next block iteration
188e27e3dacSDouglas Thompson 			 */
189fd309a9dSDouglas Thompson 			if ((nr_attrib == 0) || (attrib_spec == NULL))
190fd309a9dSDouglas Thompson 				continue;
191fd309a9dSDouglas Thompson 
192fd309a9dSDouglas Thompson 			/* setup the attribute array for this block */
193fd309a9dSDouglas Thompson 			blk->nr_attribs = nr_attrib;
194fd309a9dSDouglas Thompson 			attrib_p = &dev_attrib[block*nr_instances*nr_attrib];
195fd309a9dSDouglas Thompson 			blk->block_attributes = attrib_p;
196fd309a9dSDouglas Thompson 
197b2a4ac0cSDoug Thompson 			debugf4("%s() THIS BLOCK_ATTRIB=%p\n",
198b2a4ac0cSDoug Thompson 				__func__, blk->block_attributes);
199b2a4ac0cSDoug Thompson 
200fd309a9dSDouglas Thompson 			/* Initialize every user specified attribute in this
201fd309a9dSDouglas Thompson 			 * block with the data the caller passed in
202b2a4ac0cSDoug Thompson 			 * Each block gets its own copy of pointers,
203b2a4ac0cSDoug Thompson 			 * and its unique 'value'
204fd309a9dSDouglas Thompson 			 */
205fd309a9dSDouglas Thompson 			for (attr = 0; attr < nr_attrib; attr++) {
206e27e3dacSDouglas Thompson 				attrib = &attrib_p[attr];
207fd309a9dSDouglas Thompson 
208b2a4ac0cSDoug Thompson 				/* populate the unique per attrib
209b2a4ac0cSDoug Thompson 				 * with the code pointers and info
210b2a4ac0cSDoug Thompson 				 */
211b2a4ac0cSDoug Thompson 				attrib->attr = attrib_spec[attr].attr;
212b2a4ac0cSDoug Thompson 				attrib->show = attrib_spec[attr].show;
213b2a4ac0cSDoug Thompson 				attrib->store = attrib_spec[attr].store;
214e27e3dacSDouglas Thompson 
215b2a4ac0cSDoug Thompson 				attrib->block = blk;	/* up link */
216b2a4ac0cSDoug Thompson 
217b2a4ac0cSDoug Thompson 				debugf4("%s() alloc-attrib=%p attrib_name='%s' "
218b2a4ac0cSDoug Thompson 					"attrib-spec=%p spec-name=%s\n",
219b2a4ac0cSDoug Thompson 					__func__, attrib, attrib->attr.name,
220b2a4ac0cSDoug Thompson 					&attrib_spec[attr],
221b2a4ac0cSDoug Thompson 					attrib_spec[attr].attr.name
222b2a4ac0cSDoug Thompson 					);
223e27e3dacSDouglas Thompson 			}
224e27e3dacSDouglas Thompson 		}
225e27e3dacSDouglas Thompson 	}
226e27e3dacSDouglas Thompson 
227e27e3dacSDouglas Thompson 	/* Mark this instance as merely ALLOCATED */
228e27e3dacSDouglas Thompson 	dev_ctl->op_state = OP_ALLOC;
229e27e3dacSDouglas Thompson 
2301c3631ffSDouglas Thompson 	/*
2311c3631ffSDouglas Thompson 	 * Initialize the 'root' kobj for the edac_device controller
2321c3631ffSDouglas Thompson 	 */
2331c3631ffSDouglas Thompson 	err = edac_device_register_sysfs_main_kobj(dev_ctl);
2341c3631ffSDouglas Thompson 	if (err) {
2351c3631ffSDouglas Thompson 		kfree(dev_ctl);
2361c3631ffSDouglas Thompson 		return NULL;
2371c3631ffSDouglas Thompson 	}
2381c3631ffSDouglas Thompson 
2391c3631ffSDouglas Thompson 	/* at this point, the root kobj is valid, and in order to
2401c3631ffSDouglas Thompson 	 * 'free' the object, then the function:
2411c3631ffSDouglas Thompson 	 *	edac_device_unregister_sysfs_main_kobj() must be called
2421c3631ffSDouglas Thompson 	 * which will perform kobj unregistration and the actual free
2431c3631ffSDouglas Thompson 	 * will occur during the kobject callback operation
2441c3631ffSDouglas Thompson 	 */
2451c3631ffSDouglas Thompson 
246e27e3dacSDouglas Thompson 	return dev_ctl;
247e27e3dacSDouglas Thompson }
248e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
249e27e3dacSDouglas Thompson 
250e27e3dacSDouglas Thompson /*
251e27e3dacSDouglas Thompson  * edac_device_free_ctl_info()
252e27e3dacSDouglas Thompson  *	frees the memory allocated by the edac_device_alloc_ctl_info()
253e27e3dacSDouglas Thompson  *	function
254e27e3dacSDouglas Thompson  */
255079708b9SDouglas Thompson void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
256079708b9SDouglas Thompson {
2571c3631ffSDouglas Thompson 	edac_device_unregister_sysfs_main_kobj(ctl_info);
258e27e3dacSDouglas Thompson }
259e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
260e27e3dacSDouglas Thompson 
261e27e3dacSDouglas Thompson /*
262e27e3dacSDouglas Thompson  * find_edac_device_by_dev
263e27e3dacSDouglas Thompson  *	scans the edac_device list for a specific 'struct device *'
26452490c8dSDouglas Thompson  *
26552490c8dSDouglas Thompson  *	lock to be held prior to call:	device_ctls_mutex
26652490c8dSDouglas Thompson  *
26752490c8dSDouglas Thompson  *	Return:
26852490c8dSDouglas Thompson  *		pointer to control structure managing 'dev'
26952490c8dSDouglas Thompson  *		NULL if not found on list
270e27e3dacSDouglas Thompson  */
271079708b9SDouglas Thompson static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
272e27e3dacSDouglas Thompson {
273e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *edac_dev;
274e27e3dacSDouglas Thompson 	struct list_head *item;
275e27e3dacSDouglas Thompson 
276b2a4ac0cSDoug Thompson 	debugf0("%s()\n", __func__);
277e27e3dacSDouglas Thompson 
278e27e3dacSDouglas Thompson 	list_for_each(item, &edac_device_list) {
279e27e3dacSDouglas Thompson 		edac_dev = list_entry(item, struct edac_device_ctl_info, link);
280e27e3dacSDouglas Thompson 
281e27e3dacSDouglas Thompson 		if (edac_dev->dev == dev)
282e27e3dacSDouglas Thompson 			return edac_dev;
283e27e3dacSDouglas Thompson 	}
284e27e3dacSDouglas Thompson 
285e27e3dacSDouglas Thompson 	return NULL;
286e27e3dacSDouglas Thompson }
287e27e3dacSDouglas Thompson 
288e27e3dacSDouglas Thompson /*
289e27e3dacSDouglas Thompson  * add_edac_dev_to_global_list
290e27e3dacSDouglas Thompson  *	Before calling this function, caller must
291e27e3dacSDouglas Thompson  *	assign a unique value to edac_dev->dev_idx.
29252490c8dSDouglas Thompson  *
29352490c8dSDouglas Thompson  *	lock to be held prior to call:	device_ctls_mutex
29452490c8dSDouglas Thompson  *
295e27e3dacSDouglas Thompson  *	Return:
296e27e3dacSDouglas Thompson  *		0 on success
297e27e3dacSDouglas Thompson  *		1 on failure.
298e27e3dacSDouglas Thompson  */
299e27e3dacSDouglas Thompson static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
300e27e3dacSDouglas Thompson {
301e27e3dacSDouglas Thompson 	struct list_head *item, *insert_before;
302e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *rover;
303e27e3dacSDouglas Thompson 
304e27e3dacSDouglas Thompson 	insert_before = &edac_device_list;
305e27e3dacSDouglas Thompson 
306e27e3dacSDouglas Thompson 	/* Determine if already on the list */
30752490c8dSDouglas Thompson 	rover = find_edac_device_by_dev(edac_dev->dev);
30852490c8dSDouglas Thompson 	if (unlikely(rover != NULL))
309e27e3dacSDouglas Thompson 		goto fail0;
310e27e3dacSDouglas Thompson 
311e27e3dacSDouglas Thompson 	/* Insert in ascending order by 'dev_idx', so find position */
312e27e3dacSDouglas Thompson 	list_for_each(item, &edac_device_list) {
313e27e3dacSDouglas Thompson 		rover = list_entry(item, struct edac_device_ctl_info, link);
314e27e3dacSDouglas Thompson 
315e27e3dacSDouglas Thompson 		if (rover->dev_idx >= edac_dev->dev_idx) {
316e27e3dacSDouglas Thompson 			if (unlikely(rover->dev_idx == edac_dev->dev_idx))
317e27e3dacSDouglas Thompson 				goto fail1;
318e27e3dacSDouglas Thompson 
319e27e3dacSDouglas Thompson 			insert_before = item;
320e27e3dacSDouglas Thompson 			break;
321e27e3dacSDouglas Thompson 		}
322e27e3dacSDouglas Thompson 	}
323e27e3dacSDouglas Thompson 
324e27e3dacSDouglas Thompson 	list_add_tail_rcu(&edac_dev->link, insert_before);
325e27e3dacSDouglas Thompson 	return 0;
326e27e3dacSDouglas Thompson 
327e27e3dacSDouglas Thompson fail0:
328e27e3dacSDouglas Thompson 	edac_printk(KERN_WARNING, EDAC_MC,
329e27e3dacSDouglas Thompson 			"%s (%s) %s %s already assigned %d\n",
330281efb17SKay Sievers 			dev_name(rover->dev), edac_dev_name(rover),
331e27e3dacSDouglas Thompson 			rover->mod_name, rover->ctl_name, rover->dev_idx);
332e27e3dacSDouglas Thompson 	return 1;
333e27e3dacSDouglas Thompson 
334e27e3dacSDouglas Thompson fail1:
335e27e3dacSDouglas Thompson 	edac_printk(KERN_WARNING, EDAC_MC,
336e27e3dacSDouglas Thompson 			"bug in low-level driver: attempt to assign\n"
337079708b9SDouglas Thompson 			"    duplicate dev_idx %d in %s()\n", rover->dev_idx,
338079708b9SDouglas Thompson 			__func__);
339e27e3dacSDouglas Thompson 	return 1;
340e27e3dacSDouglas Thompson }
341e27e3dacSDouglas Thompson 
342e27e3dacSDouglas Thompson /*
343e27e3dacSDouglas Thompson  * del_edac_device_from_global_list
344e27e3dacSDouglas Thompson  */
345079708b9SDouglas Thompson static void del_edac_device_from_global_list(struct edac_device_ctl_info
346079708b9SDouglas Thompson 						*edac_device)
347e27e3dacSDouglas Thompson {
348e27e3dacSDouglas Thompson 	list_del_rcu(&edac_device->link);
349e2e77098SLai Jiangshan 
350e2e77098SLai Jiangshan 	/* these are for safe removal of devices from global list while
351e2e77098SLai Jiangshan 	 * NMI handlers may be traversing list
352e2e77098SLai Jiangshan 	 */
353e2e77098SLai Jiangshan 	synchronize_rcu();
354e2e77098SLai Jiangshan 	INIT_LIST_HEAD(&edac_device->link);
355e27e3dacSDouglas Thompson }
356e27e3dacSDouglas Thompson 
357e27e3dacSDouglas Thompson /*
35881d87cb1SDave Jiang  * edac_device_workq_function
359e27e3dacSDouglas Thompson  *	performs the operation scheduled by a workq request
360bf52fa4aSDoug Thompson  *
361bf52fa4aSDoug Thompson  *	this workq is embedded within an edac_device_ctl_info
362bf52fa4aSDoug Thompson  *	structure, that needs to be polled for possible error events.
363bf52fa4aSDoug Thompson  *
364bf52fa4aSDoug Thompson  *	This operation is to acquire the list mutex lock
365bf52fa4aSDoug Thompson  *	(thus preventing insertation or deletion)
366bf52fa4aSDoug Thompson  *	and then call the device's poll function IFF this device is
367bf52fa4aSDoug Thompson  *	running polled and there is a poll function defined.
368e27e3dacSDouglas Thompson  */
36981d87cb1SDave Jiang static void edac_device_workq_function(struct work_struct *work_req)
370e27e3dacSDouglas Thompson {
371fbeb4384SJean Delvare 	struct delayed_work *d_work = to_delayed_work(work_req);
372079708b9SDouglas Thompson 	struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
373e27e3dacSDouglas Thompson 
3740ca84761SDoug Thompson 	mutex_lock(&device_ctls_mutex);
375e27e3dacSDouglas Thompson 
376d519c8d9SHarry Ciao 	/* If we are being removed, bail out immediately */
377d519c8d9SHarry Ciao 	if (edac_dev->op_state == OP_OFFLINE) {
378d519c8d9SHarry Ciao 		mutex_unlock(&device_ctls_mutex);
379d519c8d9SHarry Ciao 		return;
380d519c8d9SHarry Ciao 	}
381d519c8d9SHarry Ciao 
382e27e3dacSDouglas Thompson 	/* Only poll controllers that are running polled and have a check */
383e27e3dacSDouglas Thompson 	if ((edac_dev->op_state == OP_RUNNING_POLL) &&
384e27e3dacSDouglas Thompson 		(edac_dev->edac_check != NULL)) {
385e27e3dacSDouglas Thompson 			edac_dev->edac_check(edac_dev);
386e27e3dacSDouglas Thompson 	}
387e27e3dacSDouglas Thompson 
3880ca84761SDoug Thompson 	mutex_unlock(&device_ctls_mutex);
389e27e3dacSDouglas Thompson 
390bf52fa4aSDoug Thompson 	/* Reschedule the workq for the next time period to start again
391bf52fa4aSDoug Thompson 	 * if the number of msec is for 1 sec, then adjust to the next
392bf52fa4aSDoug Thompson 	 * whole one second to save timers fireing all over the period
393bf52fa4aSDoug Thompson 	 * between integral seconds
394bf52fa4aSDoug Thompson 	 */
395bf52fa4aSDoug Thompson 	if (edac_dev->poll_msec == 1000)
396bf52fa4aSDoug Thompson 		queue_delayed_work(edac_workqueue, &edac_dev->work,
397c2ae24cfSAnton Blanchard 				round_jiffies_relative(edac_dev->delay));
398bf52fa4aSDoug Thompson 	else
399bf52fa4aSDoug Thompson 		queue_delayed_work(edac_workqueue, &edac_dev->work,
400bf52fa4aSDoug Thompson 				edac_dev->delay);
401e27e3dacSDouglas Thompson }
402e27e3dacSDouglas Thompson 
403e27e3dacSDouglas Thompson /*
40481d87cb1SDave Jiang  * edac_device_workq_setup
405e27e3dacSDouglas Thompson  *	initialize a workq item for this edac_device instance
406e27e3dacSDouglas Thompson  *	passing in the new delay period in msec
407e27e3dacSDouglas Thompson  */
40881d87cb1SDave Jiang void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
40981d87cb1SDave Jiang 				unsigned msec)
410e27e3dacSDouglas Thompson {
411e27e3dacSDouglas Thompson 	debugf0("%s()\n", __func__);
412e27e3dacSDouglas Thompson 
413bf52fa4aSDoug Thompson 	/* take the arg 'msec' and set it into the control structure
414bf52fa4aSDoug Thompson 	 * to used in the time period calculation
415bf52fa4aSDoug Thompson 	 * then calc the number of jiffies that represents
416bf52fa4aSDoug Thompson 	 */
417e27e3dacSDouglas Thompson 	edac_dev->poll_msec = msec;
418bf52fa4aSDoug Thompson 	edac_dev->delay = msecs_to_jiffies(msec);
419e27e3dacSDouglas Thompson 
42081d87cb1SDave Jiang 	INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
421bf52fa4aSDoug Thompson 
422bf52fa4aSDoug Thompson 	/* optimize here for the 1 second case, which will be normal value, to
423bf52fa4aSDoug Thompson 	 * fire ON the 1 second time event. This helps reduce all sorts of
424bf52fa4aSDoug Thompson 	 * timers firing on sub-second basis, while they are happy
425bf52fa4aSDoug Thompson 	 * to fire together on the 1 second exactly
426bf52fa4aSDoug Thompson 	 */
427bf52fa4aSDoug Thompson 	if (edac_dev->poll_msec == 1000)
428bf52fa4aSDoug Thompson 		queue_delayed_work(edac_workqueue, &edac_dev->work,
429c2ae24cfSAnton Blanchard 				round_jiffies_relative(edac_dev->delay));
430bf52fa4aSDoug Thompson 	else
431bf52fa4aSDoug Thompson 		queue_delayed_work(edac_workqueue, &edac_dev->work,
432bf52fa4aSDoug Thompson 				edac_dev->delay);
433e27e3dacSDouglas Thompson }
434e27e3dacSDouglas Thompson 
435e27e3dacSDouglas Thompson /*
43681d87cb1SDave Jiang  * edac_device_workq_teardown
437e27e3dacSDouglas Thompson  *	stop the workq processing on this edac_dev
438e27e3dacSDouglas Thompson  */
43981d87cb1SDave Jiang void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
440e27e3dacSDouglas Thompson {
441e27e3dacSDouglas Thompson 	int status;
442e27e3dacSDouglas Thompson 
443e27e3dacSDouglas Thompson 	status = cancel_delayed_work(&edac_dev->work);
444e27e3dacSDouglas Thompson 	if (status == 0) {
445e27e3dacSDouglas Thompson 		/* workq instance might be running, wait for it */
446e27e3dacSDouglas Thompson 		flush_workqueue(edac_workqueue);
447e27e3dacSDouglas Thompson 	}
448e27e3dacSDouglas Thompson }
449e27e3dacSDouglas Thompson 
450e27e3dacSDouglas Thompson /*
451e27e3dacSDouglas Thompson  * edac_device_reset_delay_period
452bf52fa4aSDoug Thompson  *
453bf52fa4aSDoug Thompson  *	need to stop any outstanding workq queued up at this time
454bf52fa4aSDoug Thompson  *	because we will be resetting the sleep time.
455bf52fa4aSDoug Thompson  *	Then restart the workq on the new delay
456e27e3dacSDouglas Thompson  */
457079708b9SDouglas Thompson void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
458e27e3dacSDouglas Thompson 					unsigned long value)
459e27e3dacSDouglas Thompson {
460bf52fa4aSDoug Thompson 	/* cancel the current workq request, without the mutex lock */
46181d87cb1SDave Jiang 	edac_device_workq_teardown(edac_dev);
462e27e3dacSDouglas Thompson 
463bf52fa4aSDoug Thompson 	/* acquire the mutex before doing the workq setup */
464bf52fa4aSDoug Thompson 	mutex_lock(&device_ctls_mutex);
465bf52fa4aSDoug Thompson 
466e27e3dacSDouglas Thompson 	/* restart the workq request, with new delay value */
46781d87cb1SDave Jiang 	edac_device_workq_setup(edac_dev, value);
468e27e3dacSDouglas Thompson 
4690ca84761SDoug Thompson 	mutex_unlock(&device_ctls_mutex);
470e27e3dacSDouglas Thompson }
471e27e3dacSDouglas Thompson 
4721dc9b70dSHarry Ciao /*
4731dc9b70dSHarry Ciao  * edac_device_alloc_index: Allocate a unique device index number
4741dc9b70dSHarry Ciao  *
4751dc9b70dSHarry Ciao  * Return:
4761dc9b70dSHarry Ciao  *	allocated index number
4771dc9b70dSHarry Ciao  */
4781dc9b70dSHarry Ciao int edac_device_alloc_index(void)
4791dc9b70dSHarry Ciao {
4801dc9b70dSHarry Ciao 	static atomic_t device_indexes = ATOMIC_INIT(0);
4811dc9b70dSHarry Ciao 
4821dc9b70dSHarry Ciao 	return atomic_inc_return(&device_indexes) - 1;
4831dc9b70dSHarry Ciao }
4841dc9b70dSHarry Ciao EXPORT_SYMBOL_GPL(edac_device_alloc_index);
4851dc9b70dSHarry Ciao 
486e27e3dacSDouglas Thompson /**
487e27e3dacSDouglas Thompson  * edac_device_add_device: Insert the 'edac_dev' structure into the
488e27e3dacSDouglas Thompson  * edac_device global list and create sysfs entries associated with
489e27e3dacSDouglas Thompson  * edac_device structure.
490e27e3dacSDouglas Thompson  * @edac_device: pointer to the edac_device structure to be added to the list
491e27e3dacSDouglas Thompson  * 'edac_device' structure.
492e27e3dacSDouglas Thompson  *
493e27e3dacSDouglas Thompson  * Return:
494e27e3dacSDouglas Thompson  *	0	Success
495e27e3dacSDouglas Thompson  *	!0	Failure
496e27e3dacSDouglas Thompson  */
497d45e7823SDoug Thompson int edac_device_add_device(struct edac_device_ctl_info *edac_dev)
498e27e3dacSDouglas Thompson {
499e27e3dacSDouglas Thompson 	debugf0("%s()\n", __func__);
500e27e3dacSDouglas Thompson 
501e27e3dacSDouglas Thompson #ifdef CONFIG_EDAC_DEBUG
502e27e3dacSDouglas Thompson 	if (edac_debug_level >= 3)
503e27e3dacSDouglas Thompson 		edac_device_dump_device(edac_dev);
504e27e3dacSDouglas Thompson #endif
5050ca84761SDoug Thompson 	mutex_lock(&device_ctls_mutex);
506e27e3dacSDouglas Thompson 
507e27e3dacSDouglas Thompson 	if (add_edac_dev_to_global_list(edac_dev))
508e27e3dacSDouglas Thompson 		goto fail0;
509e27e3dacSDouglas Thompson 
510e27e3dacSDouglas Thompson 	/* set load time so that error rate can be tracked */
511e27e3dacSDouglas Thompson 	edac_dev->start_time = jiffies;
512e27e3dacSDouglas Thompson 
513e27e3dacSDouglas Thompson 	/* create this instance's sysfs entries */
514e27e3dacSDouglas Thompson 	if (edac_device_create_sysfs(edac_dev)) {
515e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_WARNING,
516e27e3dacSDouglas Thompson 					"failed to create sysfs device\n");
517e27e3dacSDouglas Thompson 		goto fail1;
518e27e3dacSDouglas Thompson 	}
519e27e3dacSDouglas Thompson 
520e27e3dacSDouglas Thompson 	/* If there IS a check routine, then we are running POLLED */
521e27e3dacSDouglas Thompson 	if (edac_dev->edac_check != NULL) {
522e27e3dacSDouglas Thompson 		/* This instance is NOW RUNNING */
523e27e3dacSDouglas Thompson 		edac_dev->op_state = OP_RUNNING_POLL;
524e27e3dacSDouglas Thompson 
52581d87cb1SDave Jiang 		/*
52681d87cb1SDave Jiang 		 * enable workq processing on this instance,
52781d87cb1SDave Jiang 		 * default = 1000 msec
52881d87cb1SDave Jiang 		 */
52981d87cb1SDave Jiang 		edac_device_workq_setup(edac_dev, 1000);
530e27e3dacSDouglas Thompson 	} else {
531e27e3dacSDouglas Thompson 		edac_dev->op_state = OP_RUNNING_INTERRUPT;
532e27e3dacSDouglas Thompson 	}
533e27e3dacSDouglas Thompson 
534e27e3dacSDouglas Thompson 	/* Report action taken */
535e27e3dacSDouglas Thompson 	edac_device_printk(edac_dev, KERN_INFO,
536052dfb45SDouglas Thompson 				"Giving out device to module '%s' controller "
537052dfb45SDouglas Thompson 				"'%s': DEV '%s' (%s)\n",
538e27e3dacSDouglas Thompson 				edac_dev->mod_name,
539e27e3dacSDouglas Thompson 				edac_dev->ctl_name,
54017aa7e03SStephen Rothwell 				edac_dev_name(edac_dev),
541494d0d55SDouglas Thompson 				edac_op_state_to_string(edac_dev->op_state));
542e27e3dacSDouglas Thompson 
5430ca84761SDoug Thompson 	mutex_unlock(&device_ctls_mutex);
544e27e3dacSDouglas Thompson 	return 0;
545e27e3dacSDouglas Thompson 
546e27e3dacSDouglas Thompson fail1:
547e27e3dacSDouglas Thompson 	/* Some error, so remove the entry from the lsit */
548e27e3dacSDouglas Thompson 	del_edac_device_from_global_list(edac_dev);
549e27e3dacSDouglas Thompson 
550e27e3dacSDouglas Thompson fail0:
5510ca84761SDoug Thompson 	mutex_unlock(&device_ctls_mutex);
552e27e3dacSDouglas Thompson 	return 1;
553e27e3dacSDouglas Thompson }
554e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_add_device);
555e27e3dacSDouglas Thompson 
556e27e3dacSDouglas Thompson /**
557e27e3dacSDouglas Thompson  * edac_device_del_device:
558e27e3dacSDouglas Thompson  *	Remove sysfs entries for specified edac_device structure and
559e27e3dacSDouglas Thompson  *	then remove edac_device structure from global list
560e27e3dacSDouglas Thompson  *
561e27e3dacSDouglas Thompson  * @pdev:
562e27e3dacSDouglas Thompson  *	Pointer to 'struct device' representing edac_device
563e27e3dacSDouglas Thompson  *	structure to remove.
564e27e3dacSDouglas Thompson  *
565e27e3dacSDouglas Thompson  * Return:
566e27e3dacSDouglas Thompson  *	Pointer to removed edac_device structure,
567e27e3dacSDouglas Thompson  *	OR NULL if device not found.
568e27e3dacSDouglas Thompson  */
569e27e3dacSDouglas Thompson struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
570e27e3dacSDouglas Thompson {
571e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *edac_dev;
572e27e3dacSDouglas Thompson 
573b2a4ac0cSDoug Thompson 	debugf0("%s()\n", __func__);
574e27e3dacSDouglas Thompson 
5750ca84761SDoug Thompson 	mutex_lock(&device_ctls_mutex);
576e27e3dacSDouglas Thompson 
57752490c8dSDouglas Thompson 	/* Find the structure on the list, if not there, then leave */
57852490c8dSDouglas Thompson 	edac_dev = find_edac_device_by_dev(dev);
57952490c8dSDouglas Thompson 	if (edac_dev == NULL) {
5800ca84761SDoug Thompson 		mutex_unlock(&device_ctls_mutex);
581e27e3dacSDouglas Thompson 		return NULL;
582e27e3dacSDouglas Thompson 	}
583e27e3dacSDouglas Thompson 
584e27e3dacSDouglas Thompson 	/* mark this instance as OFFLINE */
585e27e3dacSDouglas Thompson 	edac_dev->op_state = OP_OFFLINE;
586e27e3dacSDouglas Thompson 
587e27e3dacSDouglas Thompson 	/* deregister from global list */
588e27e3dacSDouglas Thompson 	del_edac_device_from_global_list(edac_dev);
589e27e3dacSDouglas Thompson 
5900ca84761SDoug Thompson 	mutex_unlock(&device_ctls_mutex);
591e27e3dacSDouglas Thompson 
592d519c8d9SHarry Ciao 	/* clear workq processing on this instance */
593d519c8d9SHarry Ciao 	edac_device_workq_teardown(edac_dev);
594d519c8d9SHarry Ciao 
5951c3631ffSDouglas Thompson 	/* Tear down the sysfs entries for this instance */
5961c3631ffSDouglas Thompson 	edac_device_remove_sysfs(edac_dev);
5971c3631ffSDouglas Thompson 
598e27e3dacSDouglas Thompson 	edac_printk(KERN_INFO, EDAC_MC,
599e27e3dacSDouglas Thompson 		"Removed device %d for %s %s: DEV %s\n",
600e27e3dacSDouglas Thompson 		edac_dev->dev_idx,
60117aa7e03SStephen Rothwell 		edac_dev->mod_name, edac_dev->ctl_name, edac_dev_name(edac_dev));
602e27e3dacSDouglas Thompson 
603e27e3dacSDouglas Thompson 	return edac_dev;
604e27e3dacSDouglas Thompson }
605079708b9SDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_del_device);
606e27e3dacSDouglas Thompson 
607e27e3dacSDouglas Thompson static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
608e27e3dacSDouglas Thompson {
609e27e3dacSDouglas Thompson 	return edac_dev->log_ce;
610e27e3dacSDouglas Thompson }
611e27e3dacSDouglas Thompson 
612e27e3dacSDouglas Thompson static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
613e27e3dacSDouglas Thompson {
614e27e3dacSDouglas Thompson 	return edac_dev->log_ue;
615e27e3dacSDouglas Thompson }
616e27e3dacSDouglas Thompson 
617079708b9SDouglas Thompson static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
618079708b9SDouglas Thompson 					*edac_dev)
619e27e3dacSDouglas Thompson {
620e27e3dacSDouglas Thompson 	return edac_dev->panic_on_ue;
621e27e3dacSDouglas Thompson }
622e27e3dacSDouglas Thompson 
623e27e3dacSDouglas Thompson /*
624e27e3dacSDouglas Thompson  * edac_device_handle_ce
625e27e3dacSDouglas Thompson  *	perform a common output and handling of an 'edac_dev' CE event
626e27e3dacSDouglas Thompson  */
627e27e3dacSDouglas Thompson void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
628e27e3dacSDouglas Thompson 			int inst_nr, int block_nr, const char *msg)
629e27e3dacSDouglas Thompson {
630e27e3dacSDouglas Thompson 	struct edac_device_instance *instance;
631e27e3dacSDouglas Thompson 	struct edac_device_block *block = NULL;
632e27e3dacSDouglas Thompson 
633e27e3dacSDouglas Thompson 	if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
634e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_ERR,
635e27e3dacSDouglas Thompson 				"INTERNAL ERROR: 'instance' out of range "
636079708b9SDouglas Thompson 				"(%d >= %d)\n", inst_nr,
637079708b9SDouglas Thompson 				edac_dev->nr_instances);
638e27e3dacSDouglas Thompson 		return;
639e27e3dacSDouglas Thompson 	}
640e27e3dacSDouglas Thompson 
641e27e3dacSDouglas Thompson 	instance = edac_dev->instances + inst_nr;
642e27e3dacSDouglas Thompson 
643e27e3dacSDouglas Thompson 	if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
644e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_ERR,
645052dfb45SDouglas Thompson 				"INTERNAL ERROR: instance %d 'block' "
646052dfb45SDouglas Thompson 				"out of range (%d >= %d)\n",
647052dfb45SDouglas Thompson 				inst_nr, block_nr,
648079708b9SDouglas Thompson 				instance->nr_blocks);
649e27e3dacSDouglas Thompson 		return;
650e27e3dacSDouglas Thompson 	}
651e27e3dacSDouglas Thompson 
652e27e3dacSDouglas Thompson 	if (instance->nr_blocks > 0) {
653e27e3dacSDouglas Thompson 		block = instance->blocks + block_nr;
654e27e3dacSDouglas Thompson 		block->counters.ce_count++;
655e27e3dacSDouglas Thompson 	}
656e27e3dacSDouglas Thompson 
65725985edcSLucas De Marchi 	/* Propagate the count up the 'totals' tree */
658e27e3dacSDouglas Thompson 	instance->counters.ce_count++;
659e27e3dacSDouglas Thompson 	edac_dev->counters.ce_count++;
660e27e3dacSDouglas Thompson 
661e27e3dacSDouglas Thompson 	if (edac_device_get_log_ce(edac_dev))
662e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_WARNING,
663d391a7b8SDouglas Thompson 				"CE: %s instance: %s block: %s '%s'\n",
664e27e3dacSDouglas Thompson 				edac_dev->ctl_name, instance->name,
665e27e3dacSDouglas Thompson 				block ? block->name : "N/A", msg);
666e27e3dacSDouglas Thompson }
667e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_handle_ce);
668e27e3dacSDouglas Thompson 
669e27e3dacSDouglas Thompson /*
670e27e3dacSDouglas Thompson  * edac_device_handle_ue
671e27e3dacSDouglas Thompson  *	perform a common output and handling of an 'edac_dev' UE event
672e27e3dacSDouglas Thompson  */
673e27e3dacSDouglas Thompson void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
674e27e3dacSDouglas Thompson 			int inst_nr, int block_nr, const char *msg)
675e27e3dacSDouglas Thompson {
676e27e3dacSDouglas Thompson 	struct edac_device_instance *instance;
677e27e3dacSDouglas Thompson 	struct edac_device_block *block = NULL;
678e27e3dacSDouglas Thompson 
679e27e3dacSDouglas Thompson 	if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
680e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_ERR,
681e27e3dacSDouglas Thompson 				"INTERNAL ERROR: 'instance' out of range "
682079708b9SDouglas Thompson 				"(%d >= %d)\n", inst_nr,
683079708b9SDouglas Thompson 				edac_dev->nr_instances);
684e27e3dacSDouglas Thompson 		return;
685e27e3dacSDouglas Thompson 	}
686e27e3dacSDouglas Thompson 
687e27e3dacSDouglas Thompson 	instance = edac_dev->instances + inst_nr;
688e27e3dacSDouglas Thompson 
689e27e3dacSDouglas Thompson 	if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
690e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_ERR,
691052dfb45SDouglas Thompson 				"INTERNAL ERROR: instance %d 'block' "
692052dfb45SDouglas Thompson 				"out of range (%d >= %d)\n",
693052dfb45SDouglas Thompson 				inst_nr, block_nr,
694079708b9SDouglas Thompson 				instance->nr_blocks);
695e27e3dacSDouglas Thompson 		return;
696e27e3dacSDouglas Thompson 	}
697e27e3dacSDouglas Thompson 
698e27e3dacSDouglas Thompson 	if (instance->nr_blocks > 0) {
699e27e3dacSDouglas Thompson 		block = instance->blocks + block_nr;
700e27e3dacSDouglas Thompson 		block->counters.ue_count++;
701e27e3dacSDouglas Thompson 	}
702e27e3dacSDouglas Thompson 
70325985edcSLucas De Marchi 	/* Propagate the count up the 'totals' tree */
704e27e3dacSDouglas Thompson 	instance->counters.ue_count++;
705e27e3dacSDouglas Thompson 	edac_dev->counters.ue_count++;
706e27e3dacSDouglas Thompson 
707e27e3dacSDouglas Thompson 	if (edac_device_get_log_ue(edac_dev))
708e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_EMERG,
709d391a7b8SDouglas Thompson 				"UE: %s instance: %s block: %s '%s'\n",
710e27e3dacSDouglas Thompson 				edac_dev->ctl_name, instance->name,
711e27e3dacSDouglas Thompson 				block ? block->name : "N/A", msg);
712e27e3dacSDouglas Thompson 
713e27e3dacSDouglas Thompson 	if (edac_device_get_panic_on_ue(edac_dev))
714d391a7b8SDouglas Thompson 		panic("EDAC %s: UE instance: %s block %s '%s'\n",
715e27e3dacSDouglas Thompson 			edac_dev->ctl_name, instance->name,
716e27e3dacSDouglas Thompson 			block ? block->name : "N/A", msg);
717e27e3dacSDouglas Thompson }
718079708b9SDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_handle_ue);
719