xref: /linux/drivers/edac/edac_device.c (revision fd309a9d8e63e9176759d00630b65d772ae06e0c)
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 
3552490c8dSDouglas Thompson /* lock to memory controller's control array 'edac_device_list' */
360ca84761SDoug Thompson static DEFINE_MUTEX(device_ctls_mutex);
37e27e3dacSDouglas Thompson static struct list_head edac_device_list = LIST_HEAD_INIT(edac_device_list);
38e27e3dacSDouglas Thompson 
39e27e3dacSDouglas Thompson #ifdef CONFIG_EDAC_DEBUG
40e27e3dacSDouglas Thompson static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
41e27e3dacSDouglas Thompson {
42e27e3dacSDouglas Thompson 	debugf3("\tedac_dev = %p dev_idx=%d \n", edac_dev, edac_dev->dev_idx);
43e27e3dacSDouglas Thompson 	debugf4("\tedac_dev->edac_check = %p\n", edac_dev->edac_check);
44e27e3dacSDouglas Thompson 	debugf3("\tdev = %p\n", edac_dev->dev);
45e27e3dacSDouglas Thompson 	debugf3("\tmod_name:ctl_name = %s:%s\n",
46e27e3dacSDouglas Thompson 		edac_dev->mod_name, edac_dev->ctl_name);
47e27e3dacSDouglas Thompson 	debugf3("\tpvt_info = %p\n\n", edac_dev->pvt_info);
48e27e3dacSDouglas Thompson }
49e27e3dacSDouglas Thompson #endif				/* CONFIG_EDAC_DEBUG */
50e27e3dacSDouglas Thompson 
51e27e3dacSDouglas Thompson /*
5252490c8dSDouglas Thompson  * edac_device_alloc_ctl_info()
5352490c8dSDouglas Thompson  *	Allocate a new edac device control info structure
5452490c8dSDouglas Thompson  *
5552490c8dSDouglas Thompson  *	The control structure is allocated in complete chunk
5652490c8dSDouglas Thompson  *	from the OS. It is in turn sub allocated to the
5752490c8dSDouglas Thompson  *	various objects that compose the struture
5852490c8dSDouglas Thompson  *
5952490c8dSDouglas Thompson  *	The structure has a 'nr_instance' array within itself.
6052490c8dSDouglas Thompson  *	Each instance represents a major component
6152490c8dSDouglas Thompson  *		Example:  L1 cache and L2 cache are 2 instance components
6252490c8dSDouglas Thompson  *
6352490c8dSDouglas Thompson  *	Within each instance is an array of 'nr_blocks' blockoffsets
64e27e3dacSDouglas Thompson  */
65e27e3dacSDouglas Thompson struct edac_device_ctl_info *edac_device_alloc_ctl_info(
66e27e3dacSDouglas Thompson 	unsigned sz_private,
6752490c8dSDouglas Thompson 	char *edac_device_name, unsigned nr_instances,
6852490c8dSDouglas Thompson 	char *edac_block_name, unsigned nr_blocks,
6952490c8dSDouglas Thompson 	unsigned offset_value,		/* zero, 1, or other based offset */
70*fd309a9dSDouglas Thompson 	struct edac_dev_sysfs_block_attribute *attrib_spec, unsigned nr_attrib)
71e27e3dacSDouglas Thompson {
72e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *dev_ctl;
73e27e3dacSDouglas Thompson 	struct edac_device_instance *dev_inst, *inst;
74e27e3dacSDouglas Thompson 	struct edac_device_block *dev_blk, *blk_p, *blk;
75*fd309a9dSDouglas Thompson 	struct edac_dev_sysfs_block_attribute *dev_attrib, *attrib_p, *attrib;
76e27e3dacSDouglas Thompson 	unsigned total_size;
77e27e3dacSDouglas Thompson 	unsigned count;
78e27e3dacSDouglas Thompson 	unsigned instance, block, attr;
79e27e3dacSDouglas Thompson 	void *pvt;
80e27e3dacSDouglas Thompson 
81e27e3dacSDouglas Thompson 	debugf1("%s() instances=%d blocks=%d\n",
82e27e3dacSDouglas Thompson 		__func__, nr_instances, nr_blocks);
83e27e3dacSDouglas Thompson 
84*fd309a9dSDouglas Thompson 	/* Calculate the size of memory we need to allocate AND
85*fd309a9dSDouglas Thompson 	 * determine the offsets of the various item arrays
86*fd309a9dSDouglas Thompson 	 * (instance,block,attrib) from the start of an  allocated structure.
87*fd309a9dSDouglas Thompson 	 * We want the alignment of each item  (instance,block,attrib)
88e27e3dacSDouglas Thompson 	 * to be at least as stringent as what the compiler would
89e27e3dacSDouglas Thompson 	 * provide if we could simply hardcode everything into a single struct.
90e27e3dacSDouglas Thompson 	 */
9152490c8dSDouglas Thompson 	dev_ctl = (struct edac_device_ctl_info *)NULL;
92e27e3dacSDouglas Thompson 
93*fd309a9dSDouglas Thompson 	/* Calc the 'end' offset past end of ONE ctl_info structure
94*fd309a9dSDouglas Thompson 	 * which will become the start of the 'instance' array
95*fd309a9dSDouglas Thompson 	 */
967391c6dcSDouglas Thompson 	dev_inst = edac_align_ptr(&dev_ctl[1], sizeof(*dev_inst));
97e27e3dacSDouglas Thompson 
98*fd309a9dSDouglas Thompson 	/* Calc the 'end' offset past the instance array within the ctl_info
99*fd309a9dSDouglas Thompson 	 * which will become the start of the block array
100*fd309a9dSDouglas Thompson 	 */
1017391c6dcSDouglas Thompson 	dev_blk = edac_align_ptr(&dev_inst[nr_instances], sizeof(*dev_blk));
102e27e3dacSDouglas Thompson 
103*fd309a9dSDouglas Thompson 	/* Calc the 'end' offset past the dev_blk array
104*fd309a9dSDouglas Thompson 	 * which will become the start of the attrib array, if any.
105*fd309a9dSDouglas Thompson 	 */
106e27e3dacSDouglas Thompson 	count = nr_instances * nr_blocks;
1077391c6dcSDouglas Thompson 	dev_attrib = edac_align_ptr(&dev_blk[count], sizeof(*dev_attrib));
108e27e3dacSDouglas Thompson 
109*fd309a9dSDouglas Thompson 	/* Check for case of when an attribute array is specified */
110*fd309a9dSDouglas Thompson 	if (nr_attrib > 0) {
111*fd309a9dSDouglas Thompson 		/* calc how many nr_attrib we need */
112*fd309a9dSDouglas Thompson 		count *= nr_attrib;
113e27e3dacSDouglas Thompson 
114e27e3dacSDouglas Thompson 		/* Calc the 'end' offset past the attributes array */
115e27e3dacSDouglas Thompson 		pvt = edac_align_ptr(&dev_attrib[count], sz_private);
116*fd309a9dSDouglas Thompson 	} else {
117*fd309a9dSDouglas Thompson 		/* no attribute array specificed */
118*fd309a9dSDouglas Thompson 		pvt = edac_align_ptr(dev_attrib, sz_private);
119*fd309a9dSDouglas Thompson 	}
120*fd309a9dSDouglas Thompson 
121*fd309a9dSDouglas Thompson 	/* 'pvt' now points to where the private data area is.
122*fd309a9dSDouglas Thompson 	 * At this point 'pvt' (like dev_inst,dev_blk and dev_attrib)
123*fd309a9dSDouglas Thompson 	 * is baselined at ZERO
124*fd309a9dSDouglas Thompson 	 */
125e27e3dacSDouglas Thompson 	total_size = ((unsigned long)pvt) + sz_private;
126e27e3dacSDouglas Thompson 
127e27e3dacSDouglas Thompson 	/* Allocate the amount of memory for the set of control structures */
12852490c8dSDouglas Thompson 	dev_ctl = kzalloc(total_size, GFP_KERNEL);
12952490c8dSDouglas Thompson 	if (dev_ctl == NULL)
130e27e3dacSDouglas Thompson 		return NULL;
131e27e3dacSDouglas Thompson 
132*fd309a9dSDouglas Thompson 	/* Adjust pointers so they point within the actual memory we
133*fd309a9dSDouglas Thompson 	 * just allocated rather than an imaginary chunk of memory
134*fd309a9dSDouglas Thompson 	 * located at address 0.
135*fd309a9dSDouglas Thompson 	 * 'dev_ctl' points to REAL memory, while the others are
136*fd309a9dSDouglas Thompson 	 * ZERO based and thus need to be adjusted to point within
137*fd309a9dSDouglas Thompson 	 * the allocated memory.
138e27e3dacSDouglas Thompson 	 */
139e27e3dacSDouglas Thompson 	dev_inst = (struct edac_device_instance *)
140e27e3dacSDouglas Thompson 		(((char *)dev_ctl) + ((unsigned long)dev_inst));
141e27e3dacSDouglas Thompson 	dev_blk = (struct edac_device_block *)
142e27e3dacSDouglas Thompson 		(((char *)dev_ctl) + ((unsigned long)dev_blk));
143*fd309a9dSDouglas Thompson 	dev_attrib = (struct edac_dev_sysfs_block_attribute *)
144e27e3dacSDouglas Thompson 		(((char *)dev_ctl) + ((unsigned long)dev_attrib));
145079708b9SDouglas Thompson 	pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;
146e27e3dacSDouglas Thompson 
147*fd309a9dSDouglas Thompson 	/* Begin storing the information into the control info structure */
148e27e3dacSDouglas Thompson 	dev_ctl->nr_instances = nr_instances;
149e27e3dacSDouglas Thompson 	dev_ctl->instances = dev_inst;
150e27e3dacSDouglas Thompson 	dev_ctl->pvt_info = pvt;
151e27e3dacSDouglas Thompson 
15252490c8dSDouglas Thompson 	/* Name of this edac device */
153e27e3dacSDouglas Thompson 	snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s",edac_device_name);
154e27e3dacSDouglas Thompson 
155e27e3dacSDouglas Thompson 	/* Initialize every Instance */
156e27e3dacSDouglas Thompson 	for (instance = 0; instance < nr_instances; instance++) {
157e27e3dacSDouglas Thompson 		inst = &dev_inst[instance];
158e27e3dacSDouglas Thompson 		inst->ctl = dev_ctl;
159e27e3dacSDouglas Thompson 		inst->nr_blocks = nr_blocks;
160e27e3dacSDouglas Thompson 		blk_p = &dev_blk[instance * nr_blocks];
161e27e3dacSDouglas Thompson 		inst->blocks = blk_p;
162e27e3dacSDouglas Thompson 
163e27e3dacSDouglas Thompson 		/* name of this instance */
164e27e3dacSDouglas Thompson 		snprintf(inst->name, sizeof(inst->name),
165e27e3dacSDouglas Thompson 			 "%s%u", edac_device_name, instance);
166e27e3dacSDouglas Thompson 
167e27e3dacSDouglas Thompson 		/* Initialize every block in each instance */
168079708b9SDouglas Thompson 		for (block = 0; block < nr_blocks; block++) {
169e27e3dacSDouglas Thompson 			blk = &blk_p[block];
170e27e3dacSDouglas Thompson 			blk->instance = inst;
171e27e3dacSDouglas Thompson 			snprintf(blk->name, sizeof(blk->name),
172d391a7b8SDouglas Thompson 				 "%s%d", edac_block_name, block+offset_value);
173e27e3dacSDouglas Thompson 
174e27e3dacSDouglas Thompson 			debugf1("%s() instance=%d block=%d name=%s\n",
175e27e3dacSDouglas Thompson 				__func__, instance, block, blk->name);
176e27e3dacSDouglas Thompson 
177*fd309a9dSDouglas Thompson 			/* if there are NO attributes OR no attribute pointer
178*fd309a9dSDouglas Thompson 			 * then continue on to next block iteration
179e27e3dacSDouglas Thompson 			 */
180*fd309a9dSDouglas Thompson 			if ((nr_attrib == 0) || (attrib_spec == NULL))
181*fd309a9dSDouglas Thompson 				continue;
182*fd309a9dSDouglas Thompson 
183*fd309a9dSDouglas Thompson 			/* setup the attribute array for this block */
184*fd309a9dSDouglas Thompson 			blk->nr_attribs = nr_attrib;
185*fd309a9dSDouglas Thompson 			attrib_p = &dev_attrib[block*nr_instances*nr_attrib];
186*fd309a9dSDouglas Thompson 			blk->block_attributes = attrib_p;
187*fd309a9dSDouglas Thompson 
188*fd309a9dSDouglas Thompson 			/* Initialize every user specified attribute in this
189*fd309a9dSDouglas Thompson 			 * block with the data the caller passed in
190*fd309a9dSDouglas Thompson 			 */
191*fd309a9dSDouglas Thompson 			for (attr = 0; attr < nr_attrib; attr++) {
192e27e3dacSDouglas Thompson 				attrib = &attrib_p[attr];
193*fd309a9dSDouglas Thompson 				attrib->attr = attrib_spec->attr;
194*fd309a9dSDouglas Thompson 				attrib->show = attrib_spec->show;
195*fd309a9dSDouglas Thompson 				attrib->store = attrib_spec->store;
196*fd309a9dSDouglas Thompson 
197*fd309a9dSDouglas Thompson 				/* up reference this block */
198e27e3dacSDouglas Thompson 				attrib->block = blk;
199e27e3dacSDouglas Thompson 
200*fd309a9dSDouglas Thompson 				/* bump the attrib_spec */
201*fd309a9dSDouglas Thompson 				attrib_spec++;
202e27e3dacSDouglas Thompson 			}
203e27e3dacSDouglas Thompson 		}
204e27e3dacSDouglas Thompson 	}
205e27e3dacSDouglas Thompson 
206e27e3dacSDouglas Thompson 	/* Mark this instance as merely ALLOCATED */
207e27e3dacSDouglas Thompson 	dev_ctl->op_state = OP_ALLOC;
208e27e3dacSDouglas Thompson 
209e27e3dacSDouglas Thompson 	return dev_ctl;
210e27e3dacSDouglas Thompson }
211e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
212e27e3dacSDouglas Thompson 
213e27e3dacSDouglas Thompson /*
214e27e3dacSDouglas Thompson  * edac_device_free_ctl_info()
215e27e3dacSDouglas Thompson  *	frees the memory allocated by the edac_device_alloc_ctl_info()
216e27e3dacSDouglas Thompson  *	function
217e27e3dacSDouglas Thompson  */
218079708b9SDouglas Thompson void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
219079708b9SDouglas Thompson {
220e27e3dacSDouglas Thompson 	kfree(ctl_info);
221e27e3dacSDouglas Thompson }
222e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
223e27e3dacSDouglas Thompson 
224e27e3dacSDouglas Thompson /*
225e27e3dacSDouglas Thompson  * find_edac_device_by_dev
226e27e3dacSDouglas Thompson  *	scans the edac_device list for a specific 'struct device *'
22752490c8dSDouglas Thompson  *
22852490c8dSDouglas Thompson  *	lock to be held prior to call:	device_ctls_mutex
22952490c8dSDouglas Thompson  *
23052490c8dSDouglas Thompson  *	Return:
23152490c8dSDouglas Thompson  *		pointer to control structure managing 'dev'
23252490c8dSDouglas Thompson  *		NULL if not found on list
233e27e3dacSDouglas Thompson  */
234079708b9SDouglas Thompson static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
235e27e3dacSDouglas Thompson {
236e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *edac_dev;
237e27e3dacSDouglas Thompson 	struct list_head *item;
238e27e3dacSDouglas Thompson 
239e27e3dacSDouglas Thompson 	debugf3("%s()\n", __func__);
240e27e3dacSDouglas Thompson 
241e27e3dacSDouglas Thompson 	list_for_each(item, &edac_device_list) {
242e27e3dacSDouglas Thompson 		edac_dev = list_entry(item, struct edac_device_ctl_info, link);
243e27e3dacSDouglas Thompson 
244e27e3dacSDouglas Thompson 		if (edac_dev->dev == dev)
245e27e3dacSDouglas Thompson 			return edac_dev;
246e27e3dacSDouglas Thompson 	}
247e27e3dacSDouglas Thompson 
248e27e3dacSDouglas Thompson 	return NULL;
249e27e3dacSDouglas Thompson }
250e27e3dacSDouglas Thompson 
251e27e3dacSDouglas Thompson /*
252e27e3dacSDouglas Thompson  * add_edac_dev_to_global_list
253e27e3dacSDouglas Thompson  *	Before calling this function, caller must
254e27e3dacSDouglas Thompson  *	assign a unique value to edac_dev->dev_idx.
25552490c8dSDouglas Thompson  *
25652490c8dSDouglas Thompson  *	lock to be held prior to call:	device_ctls_mutex
25752490c8dSDouglas Thompson  *
258e27e3dacSDouglas Thompson  *	Return:
259e27e3dacSDouglas Thompson  *		0 on success
260e27e3dacSDouglas Thompson  *		1 on failure.
261e27e3dacSDouglas Thompson  */
262e27e3dacSDouglas Thompson static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
263e27e3dacSDouglas Thompson {
264e27e3dacSDouglas Thompson 	struct list_head *item, *insert_before;
265e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *rover;
266e27e3dacSDouglas Thompson 
267e27e3dacSDouglas Thompson 	insert_before = &edac_device_list;
268e27e3dacSDouglas Thompson 
269e27e3dacSDouglas Thompson 	/* Determine if already on the list */
27052490c8dSDouglas Thompson 	rover = find_edac_device_by_dev(edac_dev->dev);
27152490c8dSDouglas Thompson 	if (unlikely(rover != NULL))
272e27e3dacSDouglas Thompson 		goto fail0;
273e27e3dacSDouglas Thompson 
274e27e3dacSDouglas Thompson 	/* Insert in ascending order by 'dev_idx', so find position */
275e27e3dacSDouglas Thompson 	list_for_each(item, &edac_device_list) {
276e27e3dacSDouglas Thompson 		rover = list_entry(item, struct edac_device_ctl_info, link);
277e27e3dacSDouglas Thompson 
278e27e3dacSDouglas Thompson 		if (rover->dev_idx >= edac_dev->dev_idx) {
279e27e3dacSDouglas Thompson 			if (unlikely(rover->dev_idx == edac_dev->dev_idx))
280e27e3dacSDouglas Thompson 				goto fail1;
281e27e3dacSDouglas Thompson 
282e27e3dacSDouglas Thompson 			insert_before = item;
283e27e3dacSDouglas Thompson 			break;
284e27e3dacSDouglas Thompson 		}
285e27e3dacSDouglas Thompson 	}
286e27e3dacSDouglas Thompson 
287e27e3dacSDouglas Thompson 	list_add_tail_rcu(&edac_dev->link, insert_before);
288e27e3dacSDouglas Thompson 	return 0;
289e27e3dacSDouglas Thompson 
290e27e3dacSDouglas Thompson fail0:
291e27e3dacSDouglas Thompson 	edac_printk(KERN_WARNING, EDAC_MC,
292e27e3dacSDouglas Thompson 			"%s (%s) %s %s already assigned %d\n",
293c4192705SDave Jiang 			rover->dev->bus_id, dev_name(rover),
294e27e3dacSDouglas Thompson 			rover->mod_name, rover->ctl_name, rover->dev_idx);
295e27e3dacSDouglas Thompson 	return 1;
296e27e3dacSDouglas Thompson 
297e27e3dacSDouglas Thompson fail1:
298e27e3dacSDouglas Thompson 	edac_printk(KERN_WARNING, EDAC_MC,
299e27e3dacSDouglas Thompson 			"bug in low-level driver: attempt to assign\n"
300079708b9SDouglas Thompson 			"    duplicate dev_idx %d in %s()\n", rover->dev_idx,
301079708b9SDouglas Thompson 			__func__);
302e27e3dacSDouglas Thompson 	return 1;
303e27e3dacSDouglas Thompson }
304e27e3dacSDouglas Thompson 
305e27e3dacSDouglas Thompson /*
306e27e3dacSDouglas Thompson  * complete_edac_device_list_del
30752490c8dSDouglas Thompson  *
30852490c8dSDouglas Thompson  *	callback function when reference count is zero
309e27e3dacSDouglas Thompson  */
310e27e3dacSDouglas Thompson static void complete_edac_device_list_del(struct rcu_head *head)
311e27e3dacSDouglas Thompson {
312e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *edac_dev;
313e27e3dacSDouglas Thompson 
314e27e3dacSDouglas Thompson 	edac_dev = container_of(head, struct edac_device_ctl_info, rcu);
315e27e3dacSDouglas Thompson 	INIT_LIST_HEAD(&edac_dev->link);
316e27e3dacSDouglas Thompson 	complete(&edac_dev->complete);
317e27e3dacSDouglas Thompson }
318e27e3dacSDouglas Thompson 
319e27e3dacSDouglas Thompson /*
320e27e3dacSDouglas Thompson  * del_edac_device_from_global_list
32152490c8dSDouglas Thompson  *
32252490c8dSDouglas Thompson  *	remove the RCU, setup for a callback call, then wait for the
32352490c8dSDouglas Thompson  *	callback to occur
324e27e3dacSDouglas Thompson  */
325079708b9SDouglas Thompson static void del_edac_device_from_global_list(struct edac_device_ctl_info
326079708b9SDouglas Thompson 						*edac_device)
327e27e3dacSDouglas Thompson {
328e27e3dacSDouglas Thompson 	list_del_rcu(&edac_device->link);
329e27e3dacSDouglas Thompson 	init_completion(&edac_device->complete);
330e27e3dacSDouglas Thompson 	call_rcu(&edac_device->rcu, complete_edac_device_list_del);
331e27e3dacSDouglas Thompson 	wait_for_completion(&edac_device->complete);
332e27e3dacSDouglas Thompson }
333e27e3dacSDouglas Thompson 
334e27e3dacSDouglas Thompson /**
335e27e3dacSDouglas Thompson  * edac_device_find
336e27e3dacSDouglas Thompson  *	Search for a edac_device_ctl_info structure whose index is 'idx'.
337e27e3dacSDouglas Thompson  *
338e27e3dacSDouglas Thompson  * If found, return a pointer to the structure.
339e27e3dacSDouglas Thompson  * Else return NULL.
340e27e3dacSDouglas Thompson  *
341e27e3dacSDouglas Thompson  * Caller must hold device_ctls_mutex.
342e27e3dacSDouglas Thompson  */
343e27e3dacSDouglas Thompson struct edac_device_ctl_info *edac_device_find(int idx)
344e27e3dacSDouglas Thompson {
345e27e3dacSDouglas Thompson 	struct list_head *item;
346e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *edac_dev;
347e27e3dacSDouglas Thompson 
348e27e3dacSDouglas Thompson 	/* Iterate over list, looking for exact match of ID */
349e27e3dacSDouglas Thompson 	list_for_each(item, &edac_device_list) {
350e27e3dacSDouglas Thompson 		edac_dev = list_entry(item, struct edac_device_ctl_info, link);
351e27e3dacSDouglas Thompson 
352e27e3dacSDouglas Thompson 		if (edac_dev->dev_idx >= idx) {
353e27e3dacSDouglas Thompson 			if (edac_dev->dev_idx == idx)
354e27e3dacSDouglas Thompson 				return edac_dev;
355e27e3dacSDouglas Thompson 
356e27e3dacSDouglas Thompson 			/* not on list, so terminate early */
357e27e3dacSDouglas Thompson 			break;
358e27e3dacSDouglas Thompson 		}
359e27e3dacSDouglas Thompson 	}
360e27e3dacSDouglas Thompson 
361e27e3dacSDouglas Thompson 	return NULL;
362e27e3dacSDouglas Thompson }
36352490c8dSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_find);
364e27e3dacSDouglas Thompson 
365e27e3dacSDouglas Thompson /*
36681d87cb1SDave Jiang  * edac_device_workq_function
367e27e3dacSDouglas Thompson  *	performs the operation scheduled by a workq request
368e27e3dacSDouglas Thompson  */
36981d87cb1SDave Jiang static void edac_device_workq_function(struct work_struct *work_req)
370e27e3dacSDouglas Thompson {
371e27e3dacSDouglas Thompson 	struct delayed_work *d_work = (struct delayed_work *)work_req;
372079708b9SDouglas Thompson 	struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
373e27e3dacSDouglas Thompson 
374e27e3dacSDouglas Thompson 	//debugf0("%s() here and running\n", __func__);
3750ca84761SDoug Thompson 	mutex_lock(&device_ctls_mutex);
376e27e3dacSDouglas Thompson 
377e27e3dacSDouglas Thompson 	/* Only poll controllers that are running polled and have a check */
378e27e3dacSDouglas Thompson 	if ((edac_dev->op_state == OP_RUNNING_POLL) &&
379e27e3dacSDouglas Thompson 		(edac_dev->edac_check != NULL)) {
380e27e3dacSDouglas Thompson 			edac_dev->edac_check(edac_dev);
381e27e3dacSDouglas Thompson 	}
382e27e3dacSDouglas Thompson 
3830ca84761SDoug Thompson 	mutex_unlock(&device_ctls_mutex);
384e27e3dacSDouglas Thompson 
385e27e3dacSDouglas Thompson 	/* Reschedule */
386e27e3dacSDouglas Thompson 	queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
387e27e3dacSDouglas Thompson }
388e27e3dacSDouglas Thompson 
389e27e3dacSDouglas Thompson /*
39081d87cb1SDave Jiang  * edac_device_workq_setup
391e27e3dacSDouglas Thompson  *	initialize a workq item for this edac_device instance
392e27e3dacSDouglas Thompson  *	passing in the new delay period in msec
393e27e3dacSDouglas Thompson  */
39481d87cb1SDave Jiang void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
39581d87cb1SDave Jiang 				unsigned msec)
396e27e3dacSDouglas Thompson {
397e27e3dacSDouglas Thompson 	debugf0("%s()\n", __func__);
398e27e3dacSDouglas Thompson 
399e27e3dacSDouglas Thompson 	edac_dev->poll_msec = msec;
40052490c8dSDouglas Thompson 	edac_dev->delay = msecs_to_jiffies(msec);	/* Calc delay jiffies */
401e27e3dacSDouglas Thompson 
40281d87cb1SDave Jiang 	INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
403e27e3dacSDouglas Thompson 	queue_delayed_work(edac_workqueue, &edac_dev->work, edac_dev->delay);
404e27e3dacSDouglas Thompson }
405e27e3dacSDouglas Thompson 
406e27e3dacSDouglas Thompson /*
40781d87cb1SDave Jiang  * edac_device_workq_teardown
408e27e3dacSDouglas Thompson  *	stop the workq processing on this edac_dev
409e27e3dacSDouglas Thompson  */
41081d87cb1SDave Jiang void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
411e27e3dacSDouglas Thompson {
412e27e3dacSDouglas Thompson 	int status;
413e27e3dacSDouglas Thompson 
414e27e3dacSDouglas Thompson 	status = cancel_delayed_work(&edac_dev->work);
415e27e3dacSDouglas Thompson 	if (status == 0) {
416e27e3dacSDouglas Thompson 		/* workq instance might be running, wait for it */
417e27e3dacSDouglas Thompson 		flush_workqueue(edac_workqueue);
418e27e3dacSDouglas Thompson 	}
419e27e3dacSDouglas Thompson }
420e27e3dacSDouglas Thompson 
421e27e3dacSDouglas Thompson /*
422e27e3dacSDouglas Thompson  * edac_device_reset_delay_period
423e27e3dacSDouglas Thompson  */
424e27e3dacSDouglas Thompson 
425079708b9SDouglas Thompson void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
426e27e3dacSDouglas Thompson 					unsigned long value)
427e27e3dacSDouglas Thompson {
4280ca84761SDoug Thompson 	mutex_lock(&device_ctls_mutex);
429e27e3dacSDouglas Thompson 
430e27e3dacSDouglas Thompson 	/* cancel the current workq request */
43181d87cb1SDave Jiang 	edac_device_workq_teardown(edac_dev);
432e27e3dacSDouglas Thompson 
433e27e3dacSDouglas Thompson 	/* restart the workq request, with new delay value */
43481d87cb1SDave Jiang 	edac_device_workq_setup(edac_dev, value);
435e27e3dacSDouglas Thompson 
4360ca84761SDoug Thompson 	mutex_unlock(&device_ctls_mutex);
437e27e3dacSDouglas Thompson }
438e27e3dacSDouglas Thompson 
439e27e3dacSDouglas Thompson /**
440e27e3dacSDouglas Thompson  * edac_device_add_device: Insert the 'edac_dev' structure into the
441e27e3dacSDouglas Thompson  * edac_device global list and create sysfs entries associated with
442e27e3dacSDouglas Thompson  * edac_device structure.
443e27e3dacSDouglas Thompson  * @edac_device: pointer to the edac_device structure to be added to the list
444e27e3dacSDouglas Thompson  * @edac_idx: A unique numeric identifier to be assigned to the
445e27e3dacSDouglas Thompson  * 'edac_device' structure.
446e27e3dacSDouglas Thompson  *
447e27e3dacSDouglas Thompson  * Return:
448e27e3dacSDouglas Thompson  *	0	Success
449e27e3dacSDouglas Thompson  *	!0	Failure
450e27e3dacSDouglas Thompson  */
451e27e3dacSDouglas Thompson int edac_device_add_device(struct edac_device_ctl_info *edac_dev, int edac_idx)
452e27e3dacSDouglas Thompson {
453e27e3dacSDouglas Thompson 	debugf0("%s()\n", __func__);
454e27e3dacSDouglas Thompson 
455e27e3dacSDouglas Thompson 	edac_dev->dev_idx = edac_idx;
456e27e3dacSDouglas Thompson #ifdef CONFIG_EDAC_DEBUG
457e27e3dacSDouglas Thompson 	if (edac_debug_level >= 3)
458e27e3dacSDouglas Thompson 		edac_device_dump_device(edac_dev);
459e27e3dacSDouglas Thompson #endif
4600ca84761SDoug Thompson 	mutex_lock(&device_ctls_mutex);
461e27e3dacSDouglas Thompson 
462e27e3dacSDouglas Thompson 	if (add_edac_dev_to_global_list(edac_dev))
463e27e3dacSDouglas Thompson 		goto fail0;
464e27e3dacSDouglas Thompson 
465e27e3dacSDouglas Thompson 	/* set load time so that error rate can be tracked */
466e27e3dacSDouglas Thompson 	edac_dev->start_time = jiffies;
467e27e3dacSDouglas Thompson 
468e27e3dacSDouglas Thompson 	/* create this instance's sysfs entries */
469e27e3dacSDouglas Thompson 	if (edac_device_create_sysfs(edac_dev)) {
470e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_WARNING,
471e27e3dacSDouglas Thompson 					"failed to create sysfs device\n");
472e27e3dacSDouglas Thompson 		goto fail1;
473e27e3dacSDouglas Thompson 	}
474e27e3dacSDouglas Thompson 
475e27e3dacSDouglas Thompson 	/* If there IS a check routine, then we are running POLLED */
476e27e3dacSDouglas Thompson 	if (edac_dev->edac_check != NULL) {
477e27e3dacSDouglas Thompson 		/* This instance is NOW RUNNING */
478e27e3dacSDouglas Thompson 		edac_dev->op_state = OP_RUNNING_POLL;
479e27e3dacSDouglas Thompson 
48081d87cb1SDave Jiang 		/*
48181d87cb1SDave Jiang 		 * enable workq processing on this instance,
48281d87cb1SDave Jiang 		 * default = 1000 msec
48381d87cb1SDave Jiang 		 */
48481d87cb1SDave Jiang 		edac_device_workq_setup(edac_dev, 1000);
485e27e3dacSDouglas Thompson 	} else {
486e27e3dacSDouglas Thompson 		edac_dev->op_state = OP_RUNNING_INTERRUPT;
487e27e3dacSDouglas Thompson 	}
488e27e3dacSDouglas Thompson 
489e27e3dacSDouglas Thompson 	/* Report action taken */
490e27e3dacSDouglas Thompson 	edac_device_printk(edac_dev, KERN_INFO,
491052dfb45SDouglas Thompson 				"Giving out device to module '%s' controller "
492052dfb45SDouglas Thompson 				"'%s': DEV '%s' (%s)\n",
493e27e3dacSDouglas Thompson 				edac_dev->mod_name,
494e27e3dacSDouglas Thompson 				edac_dev->ctl_name,
495c4192705SDave Jiang 				dev_name(edac_dev),
496494d0d55SDouglas Thompson 				edac_op_state_to_string(edac_dev->op_state));
497e27e3dacSDouglas Thompson 
4980ca84761SDoug Thompson 	mutex_unlock(&device_ctls_mutex);
499e27e3dacSDouglas Thompson 	return 0;
500e27e3dacSDouglas Thompson 
501e27e3dacSDouglas Thompson fail1:
502e27e3dacSDouglas Thompson 	/* Some error, so remove the entry from the lsit */
503e27e3dacSDouglas Thompson 	del_edac_device_from_global_list(edac_dev);
504e27e3dacSDouglas Thompson 
505e27e3dacSDouglas Thompson fail0:
5060ca84761SDoug Thompson 	mutex_unlock(&device_ctls_mutex);
507e27e3dacSDouglas Thompson 	return 1;
508e27e3dacSDouglas Thompson }
509e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_add_device);
510e27e3dacSDouglas Thompson 
511e27e3dacSDouglas Thompson /**
512e27e3dacSDouglas Thompson  * edac_device_del_device:
513e27e3dacSDouglas Thompson  *	Remove sysfs entries for specified edac_device structure and
514e27e3dacSDouglas Thompson  *	then remove edac_device structure from global list
515e27e3dacSDouglas Thompson  *
516e27e3dacSDouglas Thompson  * @pdev:
517e27e3dacSDouglas Thompson  *	Pointer to 'struct device' representing edac_device
518e27e3dacSDouglas Thompson  *	structure to remove.
519e27e3dacSDouglas Thompson  *
520e27e3dacSDouglas Thompson  * Return:
521e27e3dacSDouglas Thompson  *	Pointer to removed edac_device structure,
522e27e3dacSDouglas Thompson  *	OR NULL if device not found.
523e27e3dacSDouglas Thompson  */
524e27e3dacSDouglas Thompson struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
525e27e3dacSDouglas Thompson {
526e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *edac_dev;
527e27e3dacSDouglas Thompson 
528e27e3dacSDouglas Thompson 	debugf0("MC: %s()\n", __func__);
529e27e3dacSDouglas Thompson 
5300ca84761SDoug Thompson 	mutex_lock(&device_ctls_mutex);
531e27e3dacSDouglas Thompson 
53252490c8dSDouglas Thompson 	/* Find the structure on the list, if not there, then leave */
53352490c8dSDouglas Thompson 	edac_dev = find_edac_device_by_dev(dev);
53452490c8dSDouglas Thompson 	if (edac_dev == NULL) {
5350ca84761SDoug Thompson 		mutex_unlock(&device_ctls_mutex);
536e27e3dacSDouglas Thompson 		return NULL;
537e27e3dacSDouglas Thompson 	}
538e27e3dacSDouglas Thompson 
539e27e3dacSDouglas Thompson 	/* mark this instance as OFFLINE */
540e27e3dacSDouglas Thompson 	edac_dev->op_state = OP_OFFLINE;
541e27e3dacSDouglas Thompson 
542e27e3dacSDouglas Thompson 	/* clear workq processing on this instance */
54381d87cb1SDave Jiang 	edac_device_workq_teardown(edac_dev);
544e27e3dacSDouglas Thompson 
545e27e3dacSDouglas Thompson 	/* Tear down the sysfs entries for this instance */
546e27e3dacSDouglas Thompson 	edac_device_remove_sysfs(edac_dev);
547e27e3dacSDouglas Thompson 
548e27e3dacSDouglas Thompson 	/* deregister from global list */
549e27e3dacSDouglas Thompson 	del_edac_device_from_global_list(edac_dev);
550e27e3dacSDouglas Thompson 
5510ca84761SDoug Thompson 	mutex_unlock(&device_ctls_mutex);
552e27e3dacSDouglas Thompson 
553e27e3dacSDouglas Thompson 	edac_printk(KERN_INFO, EDAC_MC,
554e27e3dacSDouglas Thompson 		"Removed device %d for %s %s: DEV %s\n",
555e27e3dacSDouglas Thompson 		edac_dev->dev_idx,
556079708b9SDouglas Thompson 		edac_dev->mod_name, edac_dev->ctl_name, dev_name(edac_dev));
557e27e3dacSDouglas Thompson 
558e27e3dacSDouglas Thompson 	return edac_dev;
559e27e3dacSDouglas Thompson }
560079708b9SDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_del_device);
561e27e3dacSDouglas Thompson 
562e27e3dacSDouglas Thompson static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
563e27e3dacSDouglas Thompson {
564e27e3dacSDouglas Thompson 	return edac_dev->log_ce;
565e27e3dacSDouglas Thompson }
566e27e3dacSDouglas Thompson 
567e27e3dacSDouglas Thompson static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
568e27e3dacSDouglas Thompson {
569e27e3dacSDouglas Thompson 	return edac_dev->log_ue;
570e27e3dacSDouglas Thompson }
571e27e3dacSDouglas Thompson 
572079708b9SDouglas Thompson static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
573079708b9SDouglas Thompson 					*edac_dev)
574e27e3dacSDouglas Thompson {
575e27e3dacSDouglas Thompson 	return edac_dev->panic_on_ue;
576e27e3dacSDouglas Thompson }
577e27e3dacSDouglas Thompson 
578e27e3dacSDouglas Thompson /*
579e27e3dacSDouglas Thompson  * edac_device_handle_ce
580e27e3dacSDouglas Thompson  *	perform a common output and handling of an 'edac_dev' CE event
581e27e3dacSDouglas Thompson  */
582e27e3dacSDouglas Thompson void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
583e27e3dacSDouglas Thompson 			int inst_nr, int block_nr, const char *msg)
584e27e3dacSDouglas Thompson {
585e27e3dacSDouglas Thompson 	struct edac_device_instance *instance;
586e27e3dacSDouglas Thompson 	struct edac_device_block *block = NULL;
587e27e3dacSDouglas Thompson 
588e27e3dacSDouglas Thompson 	if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
589e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_ERR,
590e27e3dacSDouglas Thompson 				"INTERNAL ERROR: 'instance' out of range "
591079708b9SDouglas Thompson 				"(%d >= %d)\n", inst_nr,
592079708b9SDouglas Thompson 				edac_dev->nr_instances);
593e27e3dacSDouglas Thompson 		return;
594e27e3dacSDouglas Thompson 	}
595e27e3dacSDouglas Thompson 
596e27e3dacSDouglas Thompson 	instance = edac_dev->instances + inst_nr;
597e27e3dacSDouglas Thompson 
598e27e3dacSDouglas Thompson 	if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
599e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_ERR,
600052dfb45SDouglas Thompson 				"INTERNAL ERROR: instance %d 'block' "
601052dfb45SDouglas Thompson 				"out of range (%d >= %d)\n",
602052dfb45SDouglas Thompson 				inst_nr, block_nr,
603079708b9SDouglas Thompson 				instance->nr_blocks);
604e27e3dacSDouglas Thompson 		return;
605e27e3dacSDouglas Thompson 	}
606e27e3dacSDouglas Thompson 
607e27e3dacSDouglas Thompson 	if (instance->nr_blocks > 0) {
608e27e3dacSDouglas Thompson 		block = instance->blocks + block_nr;
609e27e3dacSDouglas Thompson 		block->counters.ce_count++;
610e27e3dacSDouglas Thompson 	}
611e27e3dacSDouglas Thompson 
612e27e3dacSDouglas Thompson 	/* Propogate the count up the 'totals' tree */
613e27e3dacSDouglas Thompson 	instance->counters.ce_count++;
614e27e3dacSDouglas Thompson 	edac_dev->counters.ce_count++;
615e27e3dacSDouglas Thompson 
616e27e3dacSDouglas Thompson 	if (edac_device_get_log_ce(edac_dev))
617e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_WARNING,
618d391a7b8SDouglas Thompson 				"CE: %s instance: %s block: %s '%s'\n",
619e27e3dacSDouglas Thompson 				edac_dev->ctl_name, instance->name,
620e27e3dacSDouglas Thompson 				block ? block->name : "N/A", msg);
621e27e3dacSDouglas Thompson }
622e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_handle_ce);
623e27e3dacSDouglas Thompson 
624e27e3dacSDouglas Thompson /*
625e27e3dacSDouglas Thompson  * edac_device_handle_ue
626e27e3dacSDouglas Thompson  *	perform a common output and handling of an 'edac_dev' UE event
627e27e3dacSDouglas Thompson  */
628e27e3dacSDouglas Thompson void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
629e27e3dacSDouglas Thompson 			int inst_nr, int block_nr, const char *msg)
630e27e3dacSDouglas Thompson {
631e27e3dacSDouglas Thompson 	struct edac_device_instance *instance;
632e27e3dacSDouglas Thompson 	struct edac_device_block *block = NULL;
633e27e3dacSDouglas Thompson 
634e27e3dacSDouglas Thompson 	if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
635e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_ERR,
636e27e3dacSDouglas Thompson 				"INTERNAL ERROR: 'instance' out of range "
637079708b9SDouglas Thompson 				"(%d >= %d)\n", inst_nr,
638079708b9SDouglas Thompson 				edac_dev->nr_instances);
639e27e3dacSDouglas Thompson 		return;
640e27e3dacSDouglas Thompson 	}
641e27e3dacSDouglas Thompson 
642e27e3dacSDouglas Thompson 	instance = edac_dev->instances + inst_nr;
643e27e3dacSDouglas Thompson 
644e27e3dacSDouglas Thompson 	if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
645e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_ERR,
646052dfb45SDouglas Thompson 				"INTERNAL ERROR: instance %d 'block' "
647052dfb45SDouglas Thompson 				"out of range (%d >= %d)\n",
648052dfb45SDouglas Thompson 				inst_nr, block_nr,
649079708b9SDouglas Thompson 				instance->nr_blocks);
650e27e3dacSDouglas Thompson 		return;
651e27e3dacSDouglas Thompson 	}
652e27e3dacSDouglas Thompson 
653e27e3dacSDouglas Thompson 	if (instance->nr_blocks > 0) {
654e27e3dacSDouglas Thompson 		block = instance->blocks + block_nr;
655e27e3dacSDouglas Thompson 		block->counters.ue_count++;
656e27e3dacSDouglas Thompson 	}
657e27e3dacSDouglas Thompson 
658e27e3dacSDouglas Thompson 	/* Propogate the count up the 'totals' tree */
659e27e3dacSDouglas Thompson 	instance->counters.ue_count++;
660e27e3dacSDouglas Thompson 	edac_dev->counters.ue_count++;
661e27e3dacSDouglas Thompson 
662e27e3dacSDouglas Thompson 	if (edac_device_get_log_ue(edac_dev))
663e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_EMERG,
664d391a7b8SDouglas Thompson 				"UE: %s instance: %s block: %s '%s'\n",
665e27e3dacSDouglas Thompson 				edac_dev->ctl_name, instance->name,
666e27e3dacSDouglas Thompson 				block ? block->name : "N/A", msg);
667e27e3dacSDouglas Thompson 
668e27e3dacSDouglas Thompson 	if (edac_device_get_panic_on_ue(edac_dev))
669d391a7b8SDouglas Thompson 		panic("EDAC %s: UE instance: %s block %s '%s'\n",
670e27e3dacSDouglas Thompson 			edac_dev->ctl_name, instance->name,
671e27e3dacSDouglas Thompson 			block ? block->name : "N/A", msg);
672e27e3dacSDouglas Thompson }
673079708b9SDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_handle_ue);
674