xref: /linux/drivers/edac/edac_device.c (revision c4192705fec85219086231a1c0fa61e8776e2c3b)
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>
23e27e3dacSDouglas Thompson #include <linux/spinlock.h>
24e27e3dacSDouglas Thompson #include <linux/list.h>
25e27e3dacSDouglas Thompson #include <linux/sysdev.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 
34e27e3dacSDouglas Thompson /* lock to memory controller's control array */
35e27e3dacSDouglas Thompson static DECLARE_MUTEX(device_ctls_mutex);
36e27e3dacSDouglas Thompson static struct list_head edac_device_list = LIST_HEAD_INIT(edac_device_list);
37e27e3dacSDouglas Thompson 
38e27e3dacSDouglas Thompson 
39e27e3dacSDouglas Thompson static inline void lock_device_list(void)
40e27e3dacSDouglas Thompson {
41e27e3dacSDouglas Thompson 	down(&device_ctls_mutex);
42e27e3dacSDouglas Thompson }
43e27e3dacSDouglas Thompson 
44e27e3dacSDouglas Thompson static inline void unlock_device_list(void)
45e27e3dacSDouglas Thompson {
46e27e3dacSDouglas Thompson 	up(&device_ctls_mutex);
47e27e3dacSDouglas Thompson }
48e27e3dacSDouglas Thompson 
49e27e3dacSDouglas Thompson 
50e27e3dacSDouglas Thompson #ifdef CONFIG_EDAC_DEBUG
51e27e3dacSDouglas Thompson static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
52e27e3dacSDouglas Thompson {
53e27e3dacSDouglas Thompson 	debugf3("\tedac_dev = %p dev_idx=%d \n", edac_dev,edac_dev->dev_idx);
54e27e3dacSDouglas Thompson 	debugf4("\tedac_dev->edac_check = %p\n", edac_dev->edac_check);
55e27e3dacSDouglas Thompson 	debugf3("\tdev = %p\n", edac_dev->dev);
56e27e3dacSDouglas Thompson 	debugf3("\tmod_name:ctl_name = %s:%s\n",
57e27e3dacSDouglas Thompson 		edac_dev->mod_name, edac_dev->ctl_name);
58e27e3dacSDouglas Thompson 	debugf3("\tpvt_info = %p\n\n", edac_dev->pvt_info);
59e27e3dacSDouglas Thompson }
60e27e3dacSDouglas Thompson #endif  /* CONFIG_EDAC_DEBUG */
61e27e3dacSDouglas Thompson 
62e27e3dacSDouglas Thompson /*
63e27e3dacSDouglas Thompson  * The alloc() and free() functions for the 'edac_device' control info
64e27e3dacSDouglas Thompson  * structure. A MC driver will allocate one of these for each edac_device
65e27e3dacSDouglas Thompson  * it is going to control/register with the EDAC CORE.
66e27e3dacSDouglas Thompson  */
67e27e3dacSDouglas Thompson struct edac_device_ctl_info *edac_device_alloc_ctl_info(
68e27e3dacSDouglas Thompson 	unsigned sz_private,
69e27e3dacSDouglas Thompson 	char *edac_device_name,
70e27e3dacSDouglas Thompson 	unsigned nr_instances,
71e27e3dacSDouglas Thompson 	char *edac_block_name,
72e27e3dacSDouglas Thompson 	unsigned nr_blocks,
73e27e3dacSDouglas Thompson 	unsigned offset_value,
74e27e3dacSDouglas Thompson 	struct edac_attrib_spec *attrib_spec,
75e27e3dacSDouglas Thompson 	unsigned nr_attribs)
76e27e3dacSDouglas Thompson {
77e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *dev_ctl;
78e27e3dacSDouglas Thompson 	struct edac_device_instance *dev_inst, *inst;
79e27e3dacSDouglas Thompson 	struct edac_device_block *dev_blk, *blk_p, *blk;
80e27e3dacSDouglas Thompson 	struct edac_attrib *dev_attrib, *attrib_p, *attrib;
81e27e3dacSDouglas Thompson 	unsigned total_size;
82e27e3dacSDouglas Thompson 	unsigned count;
83e27e3dacSDouglas Thompson 	unsigned instance, block, attr;
84e27e3dacSDouglas Thompson 	void *pvt;
85e27e3dacSDouglas Thompson 
86e27e3dacSDouglas Thompson 	debugf1("%s() instances=%d blocks=%d\n",
87e27e3dacSDouglas Thompson 		__func__,nr_instances,nr_blocks);
88e27e3dacSDouglas Thompson 
89e27e3dacSDouglas Thompson 	/* Figure out the offsets of the various items from the start of an
90e27e3dacSDouglas Thompson 	 * ctl_info structure.  We want the alignment of each item
91e27e3dacSDouglas Thompson 	 * to be at least as stringent as what the compiler would
92e27e3dacSDouglas Thompson 	 * provide if we could simply hardcode everything into a single struct.
93e27e3dacSDouglas Thompson 	 */
94e27e3dacSDouglas Thompson 	dev_ctl = (struct edac_device_ctl_info *) 0;
95e27e3dacSDouglas Thompson 
96e27e3dacSDouglas Thompson 	/* Calc the 'end' offset past the ctl_info structure */
97e27e3dacSDouglas Thompson 	dev_inst = (struct edac_device_instance *)
98e27e3dacSDouglas Thompson 			edac_align_ptr(&dev_ctl[1],sizeof(*dev_inst));
99e27e3dacSDouglas Thompson 
100e27e3dacSDouglas Thompson 	/* Calc the 'end' offset past the instance array */
101e27e3dacSDouglas Thompson 	dev_blk = (struct edac_device_block *)
102e27e3dacSDouglas Thompson 			edac_align_ptr(&dev_inst[nr_instances],sizeof(*dev_blk));
103e27e3dacSDouglas Thompson 
104e27e3dacSDouglas Thompson 	/* Calc the 'end' offset past the dev_blk array */
105e27e3dacSDouglas Thompson 	count = nr_instances * nr_blocks;
106e27e3dacSDouglas Thompson 	dev_attrib = (struct edac_attrib *)
107e27e3dacSDouglas Thompson 			edac_align_ptr(&dev_blk[count],sizeof(*dev_attrib));
108e27e3dacSDouglas Thompson 
109e27e3dacSDouglas Thompson 	/* Check for case of NO attributes specified */
110e27e3dacSDouglas Thompson 	if (nr_attribs > 0)
111e27e3dacSDouglas Thompson 		count *= nr_attribs;
112e27e3dacSDouglas Thompson 
113e27e3dacSDouglas Thompson 	/* Calc the 'end' offset past the attributes array */
114e27e3dacSDouglas Thompson 	pvt = edac_align_ptr(&dev_attrib[count],sz_private);
115e27e3dacSDouglas Thompson 	total_size = ((unsigned long) pvt) + sz_private;
116e27e3dacSDouglas Thompson 
117e27e3dacSDouglas Thompson 	/* Allocate the amount of memory for the set of control structures */
118e27e3dacSDouglas Thompson 	if ((dev_ctl = kmalloc(total_size, GFP_KERNEL)) == NULL)
119e27e3dacSDouglas Thompson 		return NULL;
120e27e3dacSDouglas Thompson 
121e27e3dacSDouglas Thompson 	/* Adjust pointers so they point within the memory we just allocated
122e27e3dacSDouglas Thompson 	 * rather than an imaginary chunk of memory located at address 0.
123e27e3dacSDouglas Thompson 	 */
124e27e3dacSDouglas Thompson 	dev_inst = (struct edac_device_instance *)
125e27e3dacSDouglas Thompson 			(((char *) dev_ctl) + ((unsigned long) dev_inst));
126e27e3dacSDouglas Thompson 	dev_blk = (struct edac_device_block *)
127e27e3dacSDouglas Thompson 			(((char *) dev_ctl) + ((unsigned long) dev_blk));
128e27e3dacSDouglas Thompson 	dev_attrib = (struct edac_attrib *)
129e27e3dacSDouglas Thompson 			(((char *) dev_ctl) + ((unsigned long) dev_attrib));
130e27e3dacSDouglas Thompson 	pvt = sz_private ?
131e27e3dacSDouglas Thompson 			(((char *) dev_ctl) + ((unsigned long) pvt)) : NULL;
132e27e3dacSDouglas Thompson 
133e27e3dacSDouglas Thompson 	memset(dev_ctl, 0, total_size);		/* clear all fields */
134e27e3dacSDouglas Thompson 	dev_ctl->nr_instances = nr_instances;
135e27e3dacSDouglas Thompson 	dev_ctl->instances = dev_inst;
136e27e3dacSDouglas Thompson 	dev_ctl->pvt_info = pvt;
137e27e3dacSDouglas Thompson 
138e27e3dacSDouglas Thompson 	/* Name of this edac device, ensure null terminated */
139e27e3dacSDouglas Thompson 	snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s", edac_device_name);
140e27e3dacSDouglas Thompson 	dev_ctl->name[sizeof(dev_ctl->name)-1] = '\0';
141e27e3dacSDouglas Thompson 
142e27e3dacSDouglas Thompson 	/* Initialize every Instance */
143e27e3dacSDouglas Thompson 	for (instance = 0; instance < nr_instances; instance++) {
144e27e3dacSDouglas Thompson 		inst = &dev_inst[instance];
145e27e3dacSDouglas Thompson 		inst->ctl = dev_ctl;
146e27e3dacSDouglas Thompson 		inst->nr_blocks = nr_blocks;
147e27e3dacSDouglas Thompson 		blk_p = &dev_blk[instance * nr_blocks];
148e27e3dacSDouglas Thompson 		inst->blocks = blk_p;
149e27e3dacSDouglas Thompson 
150e27e3dacSDouglas Thompson 		/* name of this instance */
151e27e3dacSDouglas Thompson 		snprintf(inst->name, sizeof(inst->name),
152e27e3dacSDouglas Thompson 			"%s%u", edac_device_name, instance);
153e27e3dacSDouglas Thompson 		inst->name[sizeof(inst->name)-1] = '\0';
154e27e3dacSDouglas Thompson 
155e27e3dacSDouglas Thompson 		/* Initialize every block in each instance */
156e27e3dacSDouglas Thompson 		for (		block = 0;
157e27e3dacSDouglas Thompson 				block < nr_blocks;
158e27e3dacSDouglas Thompson 				block++) {
159e27e3dacSDouglas Thompson 			blk = &blk_p[block];
160e27e3dacSDouglas Thompson 			blk->instance = inst;
161e27e3dacSDouglas Thompson 			blk->nr_attribs = nr_attribs;
162e27e3dacSDouglas Thompson 			attrib_p = &dev_attrib[block * nr_attribs];
163e27e3dacSDouglas Thompson 			blk->attribs = attrib_p;
164e27e3dacSDouglas Thompson 			snprintf(blk->name, sizeof(blk->name),
165e27e3dacSDouglas Thompson 				"%s%d", edac_block_name,block+1);
166e27e3dacSDouglas Thompson 			blk->name[sizeof(blk->name)-1] = '\0';
167e27e3dacSDouglas Thompson 
168e27e3dacSDouglas Thompson 			debugf1("%s() instance=%d block=%d name=%s\n",
169e27e3dacSDouglas Thompson 				__func__, instance,block,blk->name);
170e27e3dacSDouglas Thompson 
171e27e3dacSDouglas Thompson 			if (attrib_spec != NULL) {
172e27e3dacSDouglas Thompson 				/* when there is an attrib_spec passed int then
173e27e3dacSDouglas Thompson 				 * Initialize every attrib of each block
174e27e3dacSDouglas Thompson 				 */
175e27e3dacSDouglas Thompson 				for (attr = 0; attr < nr_attribs; attr++) {
176e27e3dacSDouglas Thompson 					attrib = &attrib_p[attr];
177e27e3dacSDouglas Thompson 					attrib->block = blk;
178e27e3dacSDouglas Thompson 
179e27e3dacSDouglas Thompson 					/* Link each attribute to the caller's
180e27e3dacSDouglas Thompson 					 * spec entry, for name and type
181e27e3dacSDouglas Thompson 				 	 */
182e27e3dacSDouglas Thompson 					attrib->spec = &attrib_spec[attr];
183e27e3dacSDouglas Thompson 				}
184e27e3dacSDouglas Thompson 			}
185e27e3dacSDouglas Thompson 		}
186e27e3dacSDouglas Thompson 	}
187e27e3dacSDouglas Thompson 
188e27e3dacSDouglas Thompson 	/* Mark this instance as merely ALLOCATED */
189e27e3dacSDouglas Thompson 	dev_ctl->op_state = OP_ALLOC;
190e27e3dacSDouglas Thompson 
191e27e3dacSDouglas Thompson 	return dev_ctl;
192e27e3dacSDouglas Thompson }
193e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
194e27e3dacSDouglas Thompson 
195e27e3dacSDouglas Thompson /*
196e27e3dacSDouglas Thompson  * edac_device_free_ctl_info()
197e27e3dacSDouglas Thompson  *	frees the memory allocated by the edac_device_alloc_ctl_info()
198e27e3dacSDouglas Thompson  *	function
199e27e3dacSDouglas Thompson  */
200e27e3dacSDouglas Thompson void edac_device_free_ctl_info( struct edac_device_ctl_info *ctl_info) {
201e27e3dacSDouglas Thompson 	kfree(ctl_info);
202e27e3dacSDouglas Thompson }
203e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
204e27e3dacSDouglas Thompson 
205e27e3dacSDouglas Thompson 
206e27e3dacSDouglas Thompson 
207e27e3dacSDouglas Thompson /*
208e27e3dacSDouglas Thompson  * find_edac_device_by_dev
209e27e3dacSDouglas Thompson  *	scans the edac_device list for a specific 'struct device *'
210e27e3dacSDouglas Thompson  */
211e27e3dacSDouglas Thompson static struct edac_device_ctl_info *
212e27e3dacSDouglas Thompson find_edac_device_by_dev(struct device *dev)
213e27e3dacSDouglas Thompson {
214e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *edac_dev;
215e27e3dacSDouglas Thompson 	struct list_head *item;
216e27e3dacSDouglas Thompson 
217e27e3dacSDouglas Thompson 	debugf3("%s()\n", __func__);
218e27e3dacSDouglas Thompson 
219e27e3dacSDouglas Thompson 	list_for_each(item, &edac_device_list) {
220e27e3dacSDouglas Thompson 		edac_dev = list_entry(item, struct edac_device_ctl_info, link);
221e27e3dacSDouglas Thompson 
222e27e3dacSDouglas Thompson 		if (edac_dev->dev == dev)
223e27e3dacSDouglas Thompson 			return edac_dev;
224e27e3dacSDouglas Thompson 	}
225e27e3dacSDouglas Thompson 
226e27e3dacSDouglas Thompson 	return NULL;
227e27e3dacSDouglas Thompson }
228e27e3dacSDouglas Thompson 
229e27e3dacSDouglas Thompson /*
230e27e3dacSDouglas Thompson  * add_edac_dev_to_global_list
231e27e3dacSDouglas Thompson  *	Before calling this function, caller must
232e27e3dacSDouglas Thompson  *	assign a unique value to edac_dev->dev_idx.
233e27e3dacSDouglas Thompson  *	Return:
234e27e3dacSDouglas Thompson  *		0 on success
235e27e3dacSDouglas Thompson  *		1 on failure.
236e27e3dacSDouglas Thompson  */
237e27e3dacSDouglas Thompson static int add_edac_dev_to_global_list (struct edac_device_ctl_info *edac_dev)
238e27e3dacSDouglas Thompson {
239e27e3dacSDouglas Thompson 	struct list_head *item, *insert_before;
240e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *rover;
241e27e3dacSDouglas Thompson 
242e27e3dacSDouglas Thompson 	insert_before = &edac_device_list;
243e27e3dacSDouglas Thompson 
244e27e3dacSDouglas Thompson 	/* Determine if already on the list */
245e27e3dacSDouglas Thompson 	if (unlikely((rover = find_edac_device_by_dev(edac_dev->dev)) != NULL))
246e27e3dacSDouglas Thompson 		goto fail0;
247e27e3dacSDouglas Thompson 
248e27e3dacSDouglas Thompson 	/* Insert in ascending order by 'dev_idx', so find position */
249e27e3dacSDouglas Thompson 	list_for_each(item, &edac_device_list) {
250e27e3dacSDouglas Thompson 		rover = list_entry(item, struct edac_device_ctl_info, link);
251e27e3dacSDouglas Thompson 
252e27e3dacSDouglas Thompson 		if (rover->dev_idx >= edac_dev->dev_idx) {
253e27e3dacSDouglas Thompson 			if (unlikely(rover->dev_idx == edac_dev->dev_idx))
254e27e3dacSDouglas Thompson 				goto fail1;
255e27e3dacSDouglas Thompson 
256e27e3dacSDouglas Thompson 			insert_before = item;
257e27e3dacSDouglas Thompson 			break;
258e27e3dacSDouglas Thompson 		}
259e27e3dacSDouglas Thompson 	}
260e27e3dacSDouglas Thompson 
261e27e3dacSDouglas Thompson 	list_add_tail_rcu(&edac_dev->link, insert_before);
262e27e3dacSDouglas Thompson 	return 0;
263e27e3dacSDouglas Thompson 
264e27e3dacSDouglas Thompson fail0:
265e27e3dacSDouglas Thompson 	edac_printk(KERN_WARNING, EDAC_MC,
266e27e3dacSDouglas Thompson 		"%s (%s) %s %s already assigned %d\n",
267*c4192705SDave Jiang 		rover->dev->bus_id, dev_name(rover),
268e27e3dacSDouglas Thompson 		rover->mod_name, rover->ctl_name, rover->dev_idx);
269e27e3dacSDouglas Thompson 	return 1;
270e27e3dacSDouglas Thompson 
271e27e3dacSDouglas Thompson fail1:
272e27e3dacSDouglas Thompson 	edac_printk(KERN_WARNING, EDAC_MC,
273e27e3dacSDouglas Thompson 		"bug in low-level driver: attempt to assign\n"
274e27e3dacSDouglas Thompson 		"    duplicate dev_idx %d in %s()\n", rover->dev_idx, __func__);
275e27e3dacSDouglas Thompson 	return 1;
276e27e3dacSDouglas Thompson }
277e27e3dacSDouglas Thompson 
278e27e3dacSDouglas Thompson /*
279e27e3dacSDouglas Thompson  * complete_edac_device_list_del
280e27e3dacSDouglas Thompson  */
281e27e3dacSDouglas Thompson static void complete_edac_device_list_del(struct rcu_head *head)
282e27e3dacSDouglas Thompson {
283e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *edac_dev;
284e27e3dacSDouglas Thompson 
285e27e3dacSDouglas Thompson 	edac_dev = container_of(head, struct edac_device_ctl_info, rcu);
286e27e3dacSDouglas Thompson 	INIT_LIST_HEAD(&edac_dev->link);
287e27e3dacSDouglas Thompson 	complete(&edac_dev->complete);
288e27e3dacSDouglas Thompson }
289e27e3dacSDouglas Thompson 
290e27e3dacSDouglas Thompson /*
291e27e3dacSDouglas Thompson  * del_edac_device_from_global_list
292e27e3dacSDouglas Thompson  */
293e27e3dacSDouglas Thompson static void del_edac_device_from_global_list(
294e27e3dacSDouglas Thompson 			struct edac_device_ctl_info *edac_device)
295e27e3dacSDouglas Thompson {
296e27e3dacSDouglas Thompson 	list_del_rcu(&edac_device->link);
297e27e3dacSDouglas Thompson 	init_completion(&edac_device->complete);
298e27e3dacSDouglas Thompson 	call_rcu(&edac_device->rcu, complete_edac_device_list_del);
299e27e3dacSDouglas Thompson 	wait_for_completion(&edac_device->complete);
300e27e3dacSDouglas Thompson }
301e27e3dacSDouglas Thompson 
302e27e3dacSDouglas Thompson /**
303e27e3dacSDouglas Thompson  * edac_device_find
304e27e3dacSDouglas Thompson  *	Search for a edac_device_ctl_info structure whose index is 'idx'.
305e27e3dacSDouglas Thompson  *
306e27e3dacSDouglas Thompson  * If found, return a pointer to the structure.
307e27e3dacSDouglas Thompson  * Else return NULL.
308e27e3dacSDouglas Thompson  *
309e27e3dacSDouglas Thompson  * Caller must hold device_ctls_mutex.
310e27e3dacSDouglas Thompson  */
311e27e3dacSDouglas Thompson struct edac_device_ctl_info * edac_device_find(int idx)
312e27e3dacSDouglas Thompson {
313e27e3dacSDouglas Thompson 	struct list_head *item;
314e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *edac_dev;
315e27e3dacSDouglas Thompson 
316e27e3dacSDouglas Thompson 	/* Iterate over list, looking for exact match of ID */
317e27e3dacSDouglas Thompson 	list_for_each(item, &edac_device_list) {
318e27e3dacSDouglas Thompson 		edac_dev = list_entry(item, struct edac_device_ctl_info, link);
319e27e3dacSDouglas Thompson 
320e27e3dacSDouglas Thompson 		if (edac_dev->dev_idx >= idx) {
321e27e3dacSDouglas Thompson 			if (edac_dev->dev_idx == idx)
322e27e3dacSDouglas Thompson 				return edac_dev;
323e27e3dacSDouglas Thompson 
324e27e3dacSDouglas Thompson 			/* not on list, so terminate early */
325e27e3dacSDouglas Thompson 			break;
326e27e3dacSDouglas Thompson 		}
327e27e3dacSDouglas Thompson 	}
328e27e3dacSDouglas Thompson 
329e27e3dacSDouglas Thompson 	return NULL;
330e27e3dacSDouglas Thompson }
331e27e3dacSDouglas Thompson EXPORT_SYMBOL(edac_device_find);
332e27e3dacSDouglas Thompson 
333e27e3dacSDouglas Thompson 
334e27e3dacSDouglas Thompson /*
335e27e3dacSDouglas Thompson  * edac_workq_function
336e27e3dacSDouglas Thompson  *	performs the operation scheduled by a workq request
337e27e3dacSDouglas Thompson  */
338e27e3dacSDouglas Thompson #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
339e27e3dacSDouglas Thompson static void edac_workq_function(struct work_struct *work_req)
340e27e3dacSDouglas Thompson {
341e27e3dacSDouglas Thompson 	struct delayed_work *d_work = (struct delayed_work*) work_req;
342e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *edac_dev =
343e27e3dacSDouglas Thompson 		to_edac_device_ctl_work(d_work);
344e27e3dacSDouglas Thompson #else
345e27e3dacSDouglas Thompson static void edac_workq_function(void *ptr)
346e27e3dacSDouglas Thompson {
347e27e3dacSDouglas Thompson 	struct edac_device_ctl_info *edac_dev =
348e27e3dacSDouglas Thompson 		(struct edac_device_ctl_info *) ptr;
349e27e3dacSDouglas Thompson #endif
350e27e3dacSDouglas Thompson 
351e27e3dacSDouglas Thompson 	//debugf0("%s() here and running\n", __func__);
352e27e3dacSDouglas Thompson 	lock_device_list();
353e27e3dacSDouglas Thompson 
354e27e3dacSDouglas Thompson 	/* Only poll controllers that are running polled and have a check */
355e27e3dacSDouglas Thompson 	if ((edac_dev->op_state == OP_RUNNING_POLL) &&
356e27e3dacSDouglas Thompson 					(edac_dev->edac_check != NULL)) {
357e27e3dacSDouglas Thompson 		edac_dev->edac_check(edac_dev);
358e27e3dacSDouglas Thompson 	}
359e27e3dacSDouglas Thompson 
360e27e3dacSDouglas Thompson 	unlock_device_list();
361e27e3dacSDouglas Thompson 
362e27e3dacSDouglas Thompson 	/* Reschedule */
363e27e3dacSDouglas Thompson 	queue_delayed_work(edac_workqueue,&edac_dev->work, edac_dev->delay);
364e27e3dacSDouglas Thompson }
365e27e3dacSDouglas Thompson 
366e27e3dacSDouglas Thompson /*
367e27e3dacSDouglas Thompson  * edac_workq_setup
368e27e3dacSDouglas Thompson  *	initialize a workq item for this edac_device instance
369e27e3dacSDouglas Thompson  *	passing in the new delay period in msec
370e27e3dacSDouglas Thompson  */
371e27e3dacSDouglas Thompson void edac_workq_setup(struct edac_device_ctl_info *edac_dev, unsigned msec)
372e27e3dacSDouglas Thompson {
373e27e3dacSDouglas Thompson 	debugf0("%s()\n", __func__);
374e27e3dacSDouglas Thompson 
375e27e3dacSDouglas Thompson 	edac_dev->poll_msec = msec;
376e27e3dacSDouglas Thompson 	edac_device_calc_delay(edac_dev);	/* Calc delay jiffies */
377e27e3dacSDouglas Thompson 
378e27e3dacSDouglas Thompson #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
379e27e3dacSDouglas Thompson 	INIT_DELAYED_WORK(&edac_dev->work,edac_workq_function);
380e27e3dacSDouglas Thompson #else
381e27e3dacSDouglas Thompson 	INIT_WORK(&edac_dev->work,edac_workq_function,edac_dev);
382e27e3dacSDouglas Thompson #endif
383e27e3dacSDouglas Thompson 	queue_delayed_work(edac_workqueue,&edac_dev->work, edac_dev->delay);
384e27e3dacSDouglas Thompson }
385e27e3dacSDouglas Thompson 
386e27e3dacSDouglas Thompson /*
387e27e3dacSDouglas Thompson  * edac_workq_teardown
388e27e3dacSDouglas Thompson  *	stop the workq processing on this edac_dev
389e27e3dacSDouglas Thompson  */
390e27e3dacSDouglas Thompson void edac_workq_teardown(struct edac_device_ctl_info *edac_dev)
391e27e3dacSDouglas Thompson {
392e27e3dacSDouglas Thompson 	int status;
393e27e3dacSDouglas Thompson 
394e27e3dacSDouglas Thompson 	status = cancel_delayed_work(&edac_dev->work);
395e27e3dacSDouglas Thompson 	if (status == 0) {
396e27e3dacSDouglas Thompson 		/* workq instance might be running, wait for it */
397e27e3dacSDouglas Thompson 		flush_workqueue(edac_workqueue);
398e27e3dacSDouglas Thompson 	}
399e27e3dacSDouglas Thompson }
400e27e3dacSDouglas Thompson 
401e27e3dacSDouglas Thompson /*
402e27e3dacSDouglas Thompson  * edac_device_reset_delay_period
403e27e3dacSDouglas Thompson  */
404e27e3dacSDouglas Thompson 
405e27e3dacSDouglas Thompson void edac_device_reset_delay_period(
406e27e3dacSDouglas Thompson 		struct edac_device_ctl_info *edac_dev,
407e27e3dacSDouglas Thompson 		unsigned long value)
408e27e3dacSDouglas Thompson {
409e27e3dacSDouglas Thompson 	lock_device_list();
410e27e3dacSDouglas Thompson 
411e27e3dacSDouglas Thompson 	/* cancel the current workq request */
412e27e3dacSDouglas Thompson 	edac_workq_teardown(edac_dev);
413e27e3dacSDouglas Thompson 
414e27e3dacSDouglas Thompson 	/* restart the workq request, with new delay value */
415e27e3dacSDouglas Thompson 	edac_workq_setup(edac_dev, value);
416e27e3dacSDouglas Thompson 
417e27e3dacSDouglas Thompson 	unlock_device_list();
418e27e3dacSDouglas Thompson }
419e27e3dacSDouglas Thompson 
420e27e3dacSDouglas Thompson /*
421e27e3dacSDouglas Thompson  * edac_op_state_toString(edac_dev)
422e27e3dacSDouglas Thompson  */
423e27e3dacSDouglas Thompson static char *edac_op_state_toString(struct edac_device_ctl_info *edac_dev)
424e27e3dacSDouglas Thompson {
425e27e3dacSDouglas Thompson 	int opstate = edac_dev->op_state;
426e27e3dacSDouglas Thompson 
427e27e3dacSDouglas Thompson 	if (opstate == OP_RUNNING_POLL)
428e27e3dacSDouglas Thompson 		return "POLLED";
429e27e3dacSDouglas Thompson 	else if (opstate == OP_RUNNING_INTERRUPT)
430e27e3dacSDouglas Thompson 		return "INTERRUPT";
431e27e3dacSDouglas Thompson 	else if (opstate == OP_RUNNING_POLL_INTR)
432e27e3dacSDouglas Thompson 		return "POLL-INTR";
433e27e3dacSDouglas Thompson 	else if (opstate == OP_ALLOC)
434e27e3dacSDouglas Thompson 		return "ALLOC";
435e27e3dacSDouglas Thompson 	else if (opstate == OP_OFFLINE)
436e27e3dacSDouglas Thompson 		return "OFFLINE";
437e27e3dacSDouglas Thompson 
438e27e3dacSDouglas Thompson 	return "UNKNOWN";
439e27e3dacSDouglas Thompson }
440e27e3dacSDouglas Thompson 
441e27e3dacSDouglas Thompson /**
442e27e3dacSDouglas Thompson  * edac_device_add_device: Insert the 'edac_dev' structure into the
443e27e3dacSDouglas Thompson  * edac_device global list and create sysfs entries associated with
444e27e3dacSDouglas Thompson  * edac_device structure.
445e27e3dacSDouglas Thompson  * @edac_device: pointer to the edac_device structure to be added to the list
446e27e3dacSDouglas Thompson  * @edac_idx: A unique numeric identifier to be assigned to the
447e27e3dacSDouglas Thompson  * 'edac_device' structure.
448e27e3dacSDouglas Thompson  *
449e27e3dacSDouglas Thompson  * Return:
450e27e3dacSDouglas Thompson  *	0	Success
451e27e3dacSDouglas Thompson  *	!0	Failure
452e27e3dacSDouglas Thompson  */
453e27e3dacSDouglas Thompson int edac_device_add_device(struct edac_device_ctl_info *edac_dev, int edac_idx)
454e27e3dacSDouglas Thompson {
455e27e3dacSDouglas Thompson 	debugf0("%s()\n", __func__);
456e27e3dacSDouglas Thompson 
457e27e3dacSDouglas Thompson 	edac_dev->dev_idx = edac_idx;
458e27e3dacSDouglas Thompson #ifdef CONFIG_EDAC_DEBUG
459e27e3dacSDouglas Thompson 	if (edac_debug_level >= 3)
460e27e3dacSDouglas Thompson 		edac_device_dump_device(edac_dev);
461e27e3dacSDouglas Thompson #endif
462e27e3dacSDouglas Thompson 	lock_device_list();
463e27e3dacSDouglas Thompson 
464e27e3dacSDouglas Thompson 	if (add_edac_dev_to_global_list(edac_dev))
465e27e3dacSDouglas Thompson 		goto fail0;
466e27e3dacSDouglas Thompson 
467e27e3dacSDouglas Thompson 	/* set load time so that error rate can be tracked */
468e27e3dacSDouglas Thompson 	edac_dev->start_time = jiffies;
469e27e3dacSDouglas Thompson 
470e27e3dacSDouglas Thompson 	/* create this instance's sysfs entries */
471e27e3dacSDouglas Thompson 	if (edac_device_create_sysfs(edac_dev)) {
472e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_WARNING,
473e27e3dacSDouglas Thompson 			"failed to create sysfs device\n");
474e27e3dacSDouglas Thompson 		goto fail1;
475e27e3dacSDouglas Thompson 	}
476e27e3dacSDouglas Thompson 
477e27e3dacSDouglas Thompson 	/* If there IS a check routine, then we are running POLLED */
478e27e3dacSDouglas Thompson 	if (edac_dev->edac_check != NULL) {
479e27e3dacSDouglas Thompson 		/* This instance is NOW RUNNING */
480e27e3dacSDouglas Thompson 		edac_dev->op_state = OP_RUNNING_POLL;
481e27e3dacSDouglas Thompson 
482e27e3dacSDouglas Thompson 		/* enable workq processing on this instance, default = 1000 msec */
483e27e3dacSDouglas Thompson 		edac_workq_setup(edac_dev, 1000);
484e27e3dacSDouglas Thompson 	} else {
485e27e3dacSDouglas Thompson 		edac_dev->op_state = OP_RUNNING_INTERRUPT;
486e27e3dacSDouglas Thompson 	}
487e27e3dacSDouglas Thompson 
488e27e3dacSDouglas Thompson 
489e27e3dacSDouglas Thompson 	/* Report action taken */
490e27e3dacSDouglas Thompson 	edac_device_printk(edac_dev, KERN_INFO,
491e27e3dacSDouglas Thompson 		"Giving out device to module '%s' controller '%s': DEV '%s' (%s)\n",
492e27e3dacSDouglas Thompson 		edac_dev->mod_name,
493e27e3dacSDouglas Thompson 		edac_dev->ctl_name,
494*c4192705SDave Jiang 		dev_name(edac_dev),
495e27e3dacSDouglas Thompson 		edac_op_state_toString(edac_dev)
496e27e3dacSDouglas Thompson 		);
497e27e3dacSDouglas Thompson 
498e27e3dacSDouglas Thompson 	unlock_device_list();
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:
506e27e3dacSDouglas Thompson 	unlock_device_list();
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 
530e27e3dacSDouglas Thompson 	lock_device_list();
531e27e3dacSDouglas Thompson 
532e27e3dacSDouglas Thompson 	if ((edac_dev = find_edac_device_by_dev(dev)) == NULL) {
533e27e3dacSDouglas Thompson 		unlock_device_list();
534e27e3dacSDouglas Thompson 		return NULL;
535e27e3dacSDouglas Thompson 	}
536e27e3dacSDouglas Thompson 
537e27e3dacSDouglas Thompson 	/* mark this instance as OFFLINE */
538e27e3dacSDouglas Thompson 	edac_dev->op_state = OP_OFFLINE;
539e27e3dacSDouglas Thompson 
540e27e3dacSDouglas Thompson 	/* clear workq processing on this instance */
541e27e3dacSDouglas Thompson 	edac_workq_teardown(edac_dev);
542e27e3dacSDouglas Thompson 
543e27e3dacSDouglas Thompson 	/* Tear down the sysfs entries for this instance */
544e27e3dacSDouglas Thompson 	edac_device_remove_sysfs(edac_dev);
545e27e3dacSDouglas Thompson 
546e27e3dacSDouglas Thompson 	/* deregister from global list */
547e27e3dacSDouglas Thompson 	del_edac_device_from_global_list(edac_dev);
548e27e3dacSDouglas Thompson 
549e27e3dacSDouglas Thompson 	unlock_device_list();
550e27e3dacSDouglas Thompson 
551e27e3dacSDouglas Thompson 	edac_printk(KERN_INFO, EDAC_MC,
552e27e3dacSDouglas Thompson 		"Removed device %d for %s %s: DEV %s\n",
553e27e3dacSDouglas Thompson 		edac_dev->dev_idx,
554e27e3dacSDouglas Thompson 		edac_dev->mod_name,
555e27e3dacSDouglas Thompson 		edac_dev->ctl_name,
556*c4192705SDave Jiang 		dev_name(edac_dev));
557e27e3dacSDouglas Thompson 
558e27e3dacSDouglas Thompson 	return edac_dev;
559e27e3dacSDouglas Thompson }
560e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_del_device);
561e27e3dacSDouglas Thompson 
562e27e3dacSDouglas Thompson 
563e27e3dacSDouglas Thompson static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
564e27e3dacSDouglas Thompson {
565e27e3dacSDouglas Thompson 	return edac_dev->log_ce;
566e27e3dacSDouglas Thompson }
567e27e3dacSDouglas Thompson 
568e27e3dacSDouglas Thompson static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
569e27e3dacSDouglas Thompson {
570e27e3dacSDouglas Thompson 	return edac_dev->log_ue;
571e27e3dacSDouglas Thompson }
572e27e3dacSDouglas Thompson 
573e27e3dacSDouglas Thompson static inline int edac_device_get_panic_on_ue(
574e27e3dacSDouglas Thompson 		struct edac_device_ctl_info *edac_dev)
575e27e3dacSDouglas Thompson {
576e27e3dacSDouglas Thompson 	return edac_dev->panic_on_ue;
577e27e3dacSDouglas Thompson }
578e27e3dacSDouglas Thompson 
579e27e3dacSDouglas Thompson /*
580e27e3dacSDouglas Thompson  * edac_device_handle_ce
581e27e3dacSDouglas Thompson  *	perform a common output and handling of an 'edac_dev' CE event
582e27e3dacSDouglas Thompson  */
583e27e3dacSDouglas Thompson void edac_device_handle_ce(struct edac_device_ctl_info *edac_dev,
584e27e3dacSDouglas Thompson 		int inst_nr, int block_nr, const char *msg)
585e27e3dacSDouglas Thompson {
586e27e3dacSDouglas Thompson 	struct edac_device_instance *instance;
587e27e3dacSDouglas Thompson 	struct edac_device_block *block = NULL;
588e27e3dacSDouglas Thompson 
589e27e3dacSDouglas Thompson 	if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
590e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_ERR,
591e27e3dacSDouglas Thompson 			"INTERNAL ERROR: 'instance' out of range "
592e27e3dacSDouglas Thompson 			"(%d >= %d)\n", inst_nr, 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,
600e27e3dacSDouglas Thompson 			"INTERNAL ERROR: instance %d 'block' out of range "
601e27e3dacSDouglas Thompson 			"(%d >= %d)\n", inst_nr, block_nr, instance->nr_blocks);
602e27e3dacSDouglas Thompson 		return;
603e27e3dacSDouglas Thompson 	}
604e27e3dacSDouglas Thompson 
605e27e3dacSDouglas Thompson 	if (instance->nr_blocks > 0) {
606e27e3dacSDouglas Thompson 		block = instance->blocks + block_nr;
607e27e3dacSDouglas Thompson 		block->counters.ce_count++;
608e27e3dacSDouglas Thompson 	}
609e27e3dacSDouglas Thompson 
610e27e3dacSDouglas Thompson 	/* Propogate the count up the 'totals' tree */
611e27e3dacSDouglas Thompson 	instance->counters.ce_count++;
612e27e3dacSDouglas Thompson 	edac_dev->counters.ce_count++;
613e27e3dacSDouglas Thompson 
614e27e3dacSDouglas Thompson 	if (edac_device_get_log_ce(edac_dev))
615e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_WARNING,
616e27e3dacSDouglas Thompson 		"CE ctl: %s, instance: %s, block: %s: %s\n",
617e27e3dacSDouglas Thompson 		edac_dev->ctl_name, instance->name,
618e27e3dacSDouglas Thompson 		block ? block->name : "N/A", msg);
619e27e3dacSDouglas Thompson }
620e27e3dacSDouglas Thompson EXPORT_SYMBOL_GPL(edac_device_handle_ce);
621e27e3dacSDouglas Thompson 
622e27e3dacSDouglas Thompson /*
623e27e3dacSDouglas Thompson  * edac_device_handle_ue
624e27e3dacSDouglas Thompson  *	perform a common output and handling of an 'edac_dev' UE event
625e27e3dacSDouglas Thompson  */
626e27e3dacSDouglas Thompson void edac_device_handle_ue(struct edac_device_ctl_info *edac_dev,
627e27e3dacSDouglas Thompson 		int inst_nr, int block_nr, const char *msg)
628e27e3dacSDouglas Thompson {
629e27e3dacSDouglas Thompson 	struct edac_device_instance *instance;
630e27e3dacSDouglas Thompson 	struct edac_device_block *block = NULL;
631e27e3dacSDouglas Thompson 
632e27e3dacSDouglas Thompson 	if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
633e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_ERR,
634e27e3dacSDouglas Thompson 			"INTERNAL ERROR: 'instance' out of range "
635e27e3dacSDouglas Thompson 			"(%d >= %d)\n", inst_nr, edac_dev->nr_instances);
636e27e3dacSDouglas Thompson 		return;
637e27e3dacSDouglas Thompson 	}
638e27e3dacSDouglas Thompson 
639e27e3dacSDouglas Thompson 	instance = edac_dev->instances + inst_nr;
640e27e3dacSDouglas Thompson 
641e27e3dacSDouglas Thompson 	if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
642e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_ERR,
643e27e3dacSDouglas Thompson 			"INTERNAL ERROR: instance %d 'block' out of range "
644e27e3dacSDouglas Thompson 			"(%d >= %d)\n", inst_nr, block_nr, instance->nr_blocks);
645e27e3dacSDouglas Thompson 		return;
646e27e3dacSDouglas Thompson 	}
647e27e3dacSDouglas Thompson 
648e27e3dacSDouglas Thompson 	if (instance->nr_blocks > 0) {
649e27e3dacSDouglas Thompson 		block = instance->blocks + block_nr;
650e27e3dacSDouglas Thompson 		block->counters.ue_count++;
651e27e3dacSDouglas Thompson 	}
652e27e3dacSDouglas Thompson 
653e27e3dacSDouglas Thompson 	/* Propogate the count up the 'totals' tree */
654e27e3dacSDouglas Thompson 	instance->counters.ue_count++;
655e27e3dacSDouglas Thompson 	edac_dev->counters.ue_count++;
656e27e3dacSDouglas Thompson 
657e27e3dacSDouglas Thompson 	if (edac_device_get_log_ue(edac_dev))
658e27e3dacSDouglas Thompson 		edac_device_printk(edac_dev, KERN_EMERG,
659e27e3dacSDouglas Thompson 		"UE ctl: %s, instance: %s, block: %s: %s\n",
660e27e3dacSDouglas Thompson 		edac_dev->ctl_name, instance->name,
661e27e3dacSDouglas Thompson 		block ? block->name : "N/A", msg);
662e27e3dacSDouglas Thompson 
663e27e3dacSDouglas Thompson 	if (edac_device_get_panic_on_ue(edac_dev))
664e27e3dacSDouglas Thompson 		panic("EDAC %s: UE 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 EXPORT_SYMBOL_GPL(edac_device_handle_ue);
669e27e3dacSDouglas Thompson 
670