xref: /OK3568_Linux_fs/kernel/drivers/edac/edac_device.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun 
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * edac_device.c
4*4882a593Smuzhiyun  * (C) 2007 www.douglaskthompson.com
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This file may be distributed under the terms of the
7*4882a593Smuzhiyun  * GNU General Public License.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Written by Doug Thompson <norsk5@xmission.com>
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * edac_device API implementation
12*4882a593Smuzhiyun  * 19 Jan 2007
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <asm/page.h>
16*4882a593Smuzhiyun #include <linux/uaccess.h>
17*4882a593Smuzhiyun #include <linux/ctype.h>
18*4882a593Smuzhiyun #include <linux/highmem.h>
19*4882a593Smuzhiyun #include <linux/init.h>
20*4882a593Smuzhiyun #include <linux/jiffies.h>
21*4882a593Smuzhiyun #include <linux/module.h>
22*4882a593Smuzhiyun #include <linux/slab.h>
23*4882a593Smuzhiyun #include <linux/smp.h>
24*4882a593Smuzhiyun #include <linux/spinlock.h>
25*4882a593Smuzhiyun #include <linux/sysctl.h>
26*4882a593Smuzhiyun #include <linux/timer.h>
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #include "edac_device.h"
29*4882a593Smuzhiyun #include "edac_module.h"
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun /* lock for the list: 'edac_device_list', manipulation of this list
32*4882a593Smuzhiyun  * is protected by the 'device_ctls_mutex' lock
33*4882a593Smuzhiyun  */
34*4882a593Smuzhiyun static DEFINE_MUTEX(device_ctls_mutex);
35*4882a593Smuzhiyun static LIST_HEAD(edac_device_list);
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #ifdef CONFIG_EDAC_DEBUG
edac_device_dump_device(struct edac_device_ctl_info * edac_dev)38*4882a593Smuzhiyun static void edac_device_dump_device(struct edac_device_ctl_info *edac_dev)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	edac_dbg(3, "\tedac_dev = %p dev_idx=%d\n",
41*4882a593Smuzhiyun 		 edac_dev, edac_dev->dev_idx);
42*4882a593Smuzhiyun 	edac_dbg(4, "\tedac_dev->edac_check = %p\n", edac_dev->edac_check);
43*4882a593Smuzhiyun 	edac_dbg(3, "\tdev = %p\n", edac_dev->dev);
44*4882a593Smuzhiyun 	edac_dbg(3, "\tmod_name:ctl_name = %s:%s\n",
45*4882a593Smuzhiyun 		 edac_dev->mod_name, edac_dev->ctl_name);
46*4882a593Smuzhiyun 	edac_dbg(3, "\tpvt_info = %p\n\n", edac_dev->pvt_info);
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun #endif				/* CONFIG_EDAC_DEBUG */
49*4882a593Smuzhiyun 
edac_device_alloc_ctl_info(unsigned sz_private,char * edac_device_name,unsigned nr_instances,char * edac_block_name,unsigned nr_blocks,unsigned offset_value,struct edac_dev_sysfs_block_attribute * attrib_spec,unsigned nr_attrib,int device_index)50*4882a593Smuzhiyun struct edac_device_ctl_info *edac_device_alloc_ctl_info(
51*4882a593Smuzhiyun 	unsigned sz_private,
52*4882a593Smuzhiyun 	char *edac_device_name, unsigned nr_instances,
53*4882a593Smuzhiyun 	char *edac_block_name, unsigned nr_blocks,
54*4882a593Smuzhiyun 	unsigned offset_value,		/* zero, 1, or other based offset */
55*4882a593Smuzhiyun 	struct edac_dev_sysfs_block_attribute *attrib_spec, unsigned nr_attrib,
56*4882a593Smuzhiyun 	int device_index)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun 	struct edac_device_ctl_info *dev_ctl;
59*4882a593Smuzhiyun 	struct edac_device_instance *dev_inst, *inst;
60*4882a593Smuzhiyun 	struct edac_device_block *dev_blk, *blk_p, *blk;
61*4882a593Smuzhiyun 	struct edac_dev_sysfs_block_attribute *dev_attrib, *attrib_p, *attrib;
62*4882a593Smuzhiyun 	unsigned total_size;
63*4882a593Smuzhiyun 	unsigned count;
64*4882a593Smuzhiyun 	unsigned instance, block, attr;
65*4882a593Smuzhiyun 	void *pvt, *p;
66*4882a593Smuzhiyun 	int err;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	edac_dbg(4, "instances=%d blocks=%d\n", nr_instances, nr_blocks);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	/* Calculate the size of memory we need to allocate AND
71*4882a593Smuzhiyun 	 * determine the offsets of the various item arrays
72*4882a593Smuzhiyun 	 * (instance,block,attrib) from the start of an  allocated structure.
73*4882a593Smuzhiyun 	 * We want the alignment of each item  (instance,block,attrib)
74*4882a593Smuzhiyun 	 * to be at least as stringent as what the compiler would
75*4882a593Smuzhiyun 	 * provide if we could simply hardcode everything into a single struct.
76*4882a593Smuzhiyun 	 */
77*4882a593Smuzhiyun 	p = NULL;
78*4882a593Smuzhiyun 	dev_ctl = edac_align_ptr(&p, sizeof(*dev_ctl), 1);
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	/* Calc the 'end' offset past end of ONE ctl_info structure
81*4882a593Smuzhiyun 	 * which will become the start of the 'instance' array
82*4882a593Smuzhiyun 	 */
83*4882a593Smuzhiyun 	dev_inst = edac_align_ptr(&p, sizeof(*dev_inst), nr_instances);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	/* Calc the 'end' offset past the instance array within the ctl_info
86*4882a593Smuzhiyun 	 * which will become the start of the block array
87*4882a593Smuzhiyun 	 */
88*4882a593Smuzhiyun 	count = nr_instances * nr_blocks;
89*4882a593Smuzhiyun 	dev_blk = edac_align_ptr(&p, sizeof(*dev_blk), count);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	/* Calc the 'end' offset past the dev_blk array
92*4882a593Smuzhiyun 	 * which will become the start of the attrib array, if any.
93*4882a593Smuzhiyun 	 */
94*4882a593Smuzhiyun 	/* calc how many nr_attrib we need */
95*4882a593Smuzhiyun 	if (nr_attrib > 0)
96*4882a593Smuzhiyun 		count *= nr_attrib;
97*4882a593Smuzhiyun 	dev_attrib = edac_align_ptr(&p, sizeof(*dev_attrib), count);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	/* Calc the 'end' offset past the attributes array */
100*4882a593Smuzhiyun 	pvt = edac_align_ptr(&p, sz_private, 1);
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	/* 'pvt' now points to where the private data area is.
103*4882a593Smuzhiyun 	 * At this point 'pvt' (like dev_inst,dev_blk and dev_attrib)
104*4882a593Smuzhiyun 	 * is baselined at ZERO
105*4882a593Smuzhiyun 	 */
106*4882a593Smuzhiyun 	total_size = ((unsigned long)pvt) + sz_private;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	/* Allocate the amount of memory for the set of control structures */
109*4882a593Smuzhiyun 	dev_ctl = kzalloc(total_size, GFP_KERNEL);
110*4882a593Smuzhiyun 	if (dev_ctl == NULL)
111*4882a593Smuzhiyun 		return NULL;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/* Adjust pointers so they point within the actual memory we
114*4882a593Smuzhiyun 	 * just allocated rather than an imaginary chunk of memory
115*4882a593Smuzhiyun 	 * located at address 0.
116*4882a593Smuzhiyun 	 * 'dev_ctl' points to REAL memory, while the others are
117*4882a593Smuzhiyun 	 * ZERO based and thus need to be adjusted to point within
118*4882a593Smuzhiyun 	 * the allocated memory.
119*4882a593Smuzhiyun 	 */
120*4882a593Smuzhiyun 	dev_inst = (struct edac_device_instance *)
121*4882a593Smuzhiyun 		(((char *)dev_ctl) + ((unsigned long)dev_inst));
122*4882a593Smuzhiyun 	dev_blk = (struct edac_device_block *)
123*4882a593Smuzhiyun 		(((char *)dev_ctl) + ((unsigned long)dev_blk));
124*4882a593Smuzhiyun 	dev_attrib = (struct edac_dev_sysfs_block_attribute *)
125*4882a593Smuzhiyun 		(((char *)dev_ctl) + ((unsigned long)dev_attrib));
126*4882a593Smuzhiyun 	pvt = sz_private ? (((char *)dev_ctl) + ((unsigned long)pvt)) : NULL;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	/* Begin storing the information into the control info structure */
129*4882a593Smuzhiyun 	dev_ctl->dev_idx = device_index;
130*4882a593Smuzhiyun 	dev_ctl->nr_instances = nr_instances;
131*4882a593Smuzhiyun 	dev_ctl->instances = dev_inst;
132*4882a593Smuzhiyun 	dev_ctl->pvt_info = pvt;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	/* Default logging of CEs and UEs */
135*4882a593Smuzhiyun 	dev_ctl->log_ce = 1;
136*4882a593Smuzhiyun 	dev_ctl->log_ue = 1;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	/* Name of this edac device */
139*4882a593Smuzhiyun 	snprintf(dev_ctl->name,sizeof(dev_ctl->name),"%s",edac_device_name);
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	edac_dbg(4, "edac_dev=%p next after end=%p\n",
142*4882a593Smuzhiyun 		 dev_ctl, pvt + sz_private);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	/* Initialize every Instance */
145*4882a593Smuzhiyun 	for (instance = 0; instance < nr_instances; instance++) {
146*4882a593Smuzhiyun 		inst = &dev_inst[instance];
147*4882a593Smuzhiyun 		inst->ctl = dev_ctl;
148*4882a593Smuzhiyun 		inst->nr_blocks = nr_blocks;
149*4882a593Smuzhiyun 		blk_p = &dev_blk[instance * nr_blocks];
150*4882a593Smuzhiyun 		inst->blocks = blk_p;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 		/* name of this instance */
153*4882a593Smuzhiyun 		snprintf(inst->name, sizeof(inst->name),
154*4882a593Smuzhiyun 			 "%s%u", edac_device_name, instance);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 		/* Initialize every block in each instance */
157*4882a593Smuzhiyun 		for (block = 0; block < nr_blocks; block++) {
158*4882a593Smuzhiyun 			blk = &blk_p[block];
159*4882a593Smuzhiyun 			blk->instance = inst;
160*4882a593Smuzhiyun 			snprintf(blk->name, sizeof(blk->name),
161*4882a593Smuzhiyun 				 "%s%d", edac_block_name, block+offset_value);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 			edac_dbg(4, "instance=%d inst_p=%p block=#%d block_p=%p name='%s'\n",
164*4882a593Smuzhiyun 				 instance, inst, block, blk, blk->name);
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 			/* if there are NO attributes OR no attribute pointer
167*4882a593Smuzhiyun 			 * then continue on to next block iteration
168*4882a593Smuzhiyun 			 */
169*4882a593Smuzhiyun 			if ((nr_attrib == 0) || (attrib_spec == NULL))
170*4882a593Smuzhiyun 				continue;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 			/* setup the attribute array for this block */
173*4882a593Smuzhiyun 			blk->nr_attribs = nr_attrib;
174*4882a593Smuzhiyun 			attrib_p = &dev_attrib[block*nr_instances*nr_attrib];
175*4882a593Smuzhiyun 			blk->block_attributes = attrib_p;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 			edac_dbg(4, "THIS BLOCK_ATTRIB=%p\n",
178*4882a593Smuzhiyun 				 blk->block_attributes);
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 			/* Initialize every user specified attribute in this
181*4882a593Smuzhiyun 			 * block with the data the caller passed in
182*4882a593Smuzhiyun 			 * Each block gets its own copy of pointers,
183*4882a593Smuzhiyun 			 * and its unique 'value'
184*4882a593Smuzhiyun 			 */
185*4882a593Smuzhiyun 			for (attr = 0; attr < nr_attrib; attr++) {
186*4882a593Smuzhiyun 				attrib = &attrib_p[attr];
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 				/* populate the unique per attrib
189*4882a593Smuzhiyun 				 * with the code pointers and info
190*4882a593Smuzhiyun 				 */
191*4882a593Smuzhiyun 				attrib->attr = attrib_spec[attr].attr;
192*4882a593Smuzhiyun 				attrib->show = attrib_spec[attr].show;
193*4882a593Smuzhiyun 				attrib->store = attrib_spec[attr].store;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 				attrib->block = blk;	/* up link */
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 				edac_dbg(4, "alloc-attrib=%p attrib_name='%s' attrib-spec=%p spec-name=%s\n",
198*4882a593Smuzhiyun 					 attrib, attrib->attr.name,
199*4882a593Smuzhiyun 					 &attrib_spec[attr],
200*4882a593Smuzhiyun 					 attrib_spec[attr].attr.name
201*4882a593Smuzhiyun 					);
202*4882a593Smuzhiyun 			}
203*4882a593Smuzhiyun 		}
204*4882a593Smuzhiyun 	}
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	/* Mark this instance as merely ALLOCATED */
207*4882a593Smuzhiyun 	dev_ctl->op_state = OP_ALLOC;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	/*
210*4882a593Smuzhiyun 	 * Initialize the 'root' kobj for the edac_device controller
211*4882a593Smuzhiyun 	 */
212*4882a593Smuzhiyun 	err = edac_device_register_sysfs_main_kobj(dev_ctl);
213*4882a593Smuzhiyun 	if (err) {
214*4882a593Smuzhiyun 		kfree(dev_ctl);
215*4882a593Smuzhiyun 		return NULL;
216*4882a593Smuzhiyun 	}
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	/* at this point, the root kobj is valid, and in order to
219*4882a593Smuzhiyun 	 * 'free' the object, then the function:
220*4882a593Smuzhiyun 	 *	edac_device_unregister_sysfs_main_kobj() must be called
221*4882a593Smuzhiyun 	 * which will perform kobj unregistration and the actual free
222*4882a593Smuzhiyun 	 * will occur during the kobject callback operation
223*4882a593Smuzhiyun 	 */
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	return dev_ctl;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(edac_device_alloc_ctl_info);
228*4882a593Smuzhiyun 
edac_device_free_ctl_info(struct edac_device_ctl_info * ctl_info)229*4882a593Smuzhiyun void edac_device_free_ctl_info(struct edac_device_ctl_info *ctl_info)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun 	edac_device_unregister_sysfs_main_kobj(ctl_info);
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(edac_device_free_ctl_info);
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun /*
236*4882a593Smuzhiyun  * find_edac_device_by_dev
237*4882a593Smuzhiyun  *	scans the edac_device list for a specific 'struct device *'
238*4882a593Smuzhiyun  *
239*4882a593Smuzhiyun  *	lock to be held prior to call:	device_ctls_mutex
240*4882a593Smuzhiyun  *
241*4882a593Smuzhiyun  *	Return:
242*4882a593Smuzhiyun  *		pointer to control structure managing 'dev'
243*4882a593Smuzhiyun  *		NULL if not found on list
244*4882a593Smuzhiyun  */
find_edac_device_by_dev(struct device * dev)245*4882a593Smuzhiyun static struct edac_device_ctl_info *find_edac_device_by_dev(struct device *dev)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun 	struct edac_device_ctl_info *edac_dev;
248*4882a593Smuzhiyun 	struct list_head *item;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	edac_dbg(0, "\n");
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	list_for_each(item, &edac_device_list) {
253*4882a593Smuzhiyun 		edac_dev = list_entry(item, struct edac_device_ctl_info, link);
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 		if (edac_dev->dev == dev)
256*4882a593Smuzhiyun 			return edac_dev;
257*4882a593Smuzhiyun 	}
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	return NULL;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun /*
263*4882a593Smuzhiyun  * add_edac_dev_to_global_list
264*4882a593Smuzhiyun  *	Before calling this function, caller must
265*4882a593Smuzhiyun  *	assign a unique value to edac_dev->dev_idx.
266*4882a593Smuzhiyun  *
267*4882a593Smuzhiyun  *	lock to be held prior to call:	device_ctls_mutex
268*4882a593Smuzhiyun  *
269*4882a593Smuzhiyun  *	Return:
270*4882a593Smuzhiyun  *		0 on success
271*4882a593Smuzhiyun  *		1 on failure.
272*4882a593Smuzhiyun  */
add_edac_dev_to_global_list(struct edac_device_ctl_info * edac_dev)273*4882a593Smuzhiyun static int add_edac_dev_to_global_list(struct edac_device_ctl_info *edac_dev)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun 	struct list_head *item, *insert_before;
276*4882a593Smuzhiyun 	struct edac_device_ctl_info *rover;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	insert_before = &edac_device_list;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	/* Determine if already on the list */
281*4882a593Smuzhiyun 	rover = find_edac_device_by_dev(edac_dev->dev);
282*4882a593Smuzhiyun 	if (unlikely(rover != NULL))
283*4882a593Smuzhiyun 		goto fail0;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	/* Insert in ascending order by 'dev_idx', so find position */
286*4882a593Smuzhiyun 	list_for_each(item, &edac_device_list) {
287*4882a593Smuzhiyun 		rover = list_entry(item, struct edac_device_ctl_info, link);
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 		if (rover->dev_idx >= edac_dev->dev_idx) {
290*4882a593Smuzhiyun 			if (unlikely(rover->dev_idx == edac_dev->dev_idx))
291*4882a593Smuzhiyun 				goto fail1;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 			insert_before = item;
294*4882a593Smuzhiyun 			break;
295*4882a593Smuzhiyun 		}
296*4882a593Smuzhiyun 	}
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	list_add_tail_rcu(&edac_dev->link, insert_before);
299*4882a593Smuzhiyun 	return 0;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun fail0:
302*4882a593Smuzhiyun 	edac_printk(KERN_WARNING, EDAC_MC,
303*4882a593Smuzhiyun 			"%s (%s) %s %s already assigned %d\n",
304*4882a593Smuzhiyun 			dev_name(rover->dev), edac_dev_name(rover),
305*4882a593Smuzhiyun 			rover->mod_name, rover->ctl_name, rover->dev_idx);
306*4882a593Smuzhiyun 	return 1;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun fail1:
309*4882a593Smuzhiyun 	edac_printk(KERN_WARNING, EDAC_MC,
310*4882a593Smuzhiyun 			"bug in low-level driver: attempt to assign\n"
311*4882a593Smuzhiyun 			"    duplicate dev_idx %d in %s()\n", rover->dev_idx,
312*4882a593Smuzhiyun 			__func__);
313*4882a593Smuzhiyun 	return 1;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun /*
317*4882a593Smuzhiyun  * del_edac_device_from_global_list
318*4882a593Smuzhiyun  */
del_edac_device_from_global_list(struct edac_device_ctl_info * edac_device)319*4882a593Smuzhiyun static void del_edac_device_from_global_list(struct edac_device_ctl_info
320*4882a593Smuzhiyun 						*edac_device)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun 	list_del_rcu(&edac_device->link);
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	/* these are for safe removal of devices from global list while
325*4882a593Smuzhiyun 	 * NMI handlers may be traversing list
326*4882a593Smuzhiyun 	 */
327*4882a593Smuzhiyun 	synchronize_rcu();
328*4882a593Smuzhiyun 	INIT_LIST_HEAD(&edac_device->link);
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun /*
332*4882a593Smuzhiyun  * edac_device_workq_function
333*4882a593Smuzhiyun  *	performs the operation scheduled by a workq request
334*4882a593Smuzhiyun  *
335*4882a593Smuzhiyun  *	this workq is embedded within an edac_device_ctl_info
336*4882a593Smuzhiyun  *	structure, that needs to be polled for possible error events.
337*4882a593Smuzhiyun  *
338*4882a593Smuzhiyun  *	This operation is to acquire the list mutex lock
339*4882a593Smuzhiyun  *	(thus preventing insertation or deletion)
340*4882a593Smuzhiyun  *	and then call the device's poll function IFF this device is
341*4882a593Smuzhiyun  *	running polled and there is a poll function defined.
342*4882a593Smuzhiyun  */
edac_device_workq_function(struct work_struct * work_req)343*4882a593Smuzhiyun static void edac_device_workq_function(struct work_struct *work_req)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun 	struct delayed_work *d_work = to_delayed_work(work_req);
346*4882a593Smuzhiyun 	struct edac_device_ctl_info *edac_dev = to_edac_device_ctl_work(d_work);
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	mutex_lock(&device_ctls_mutex);
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	/* If we are being removed, bail out immediately */
351*4882a593Smuzhiyun 	if (edac_dev->op_state == OP_OFFLINE) {
352*4882a593Smuzhiyun 		mutex_unlock(&device_ctls_mutex);
353*4882a593Smuzhiyun 		return;
354*4882a593Smuzhiyun 	}
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	/* Only poll controllers that are running polled and have a check */
357*4882a593Smuzhiyun 	if ((edac_dev->op_state == OP_RUNNING_POLL) &&
358*4882a593Smuzhiyun 		(edac_dev->edac_check != NULL)) {
359*4882a593Smuzhiyun 			edac_dev->edac_check(edac_dev);
360*4882a593Smuzhiyun 	}
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	mutex_unlock(&device_ctls_mutex);
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	/* Reschedule the workq for the next time period to start again
365*4882a593Smuzhiyun 	 * if the number of msec is for 1 sec, then adjust to the next
366*4882a593Smuzhiyun 	 * whole one second to save timers firing all over the period
367*4882a593Smuzhiyun 	 * between integral seconds
368*4882a593Smuzhiyun 	 */
369*4882a593Smuzhiyun 	if (edac_dev->poll_msec == 1000)
370*4882a593Smuzhiyun 		edac_queue_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
371*4882a593Smuzhiyun 	else
372*4882a593Smuzhiyun 		edac_queue_work(&edac_dev->work, edac_dev->delay);
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun /*
376*4882a593Smuzhiyun  * edac_device_workq_setup
377*4882a593Smuzhiyun  *	initialize a workq item for this edac_device instance
378*4882a593Smuzhiyun  *	passing in the new delay period in msec
379*4882a593Smuzhiyun  */
edac_device_workq_setup(struct edac_device_ctl_info * edac_dev,unsigned msec)380*4882a593Smuzhiyun static void edac_device_workq_setup(struct edac_device_ctl_info *edac_dev,
381*4882a593Smuzhiyun 				    unsigned msec)
382*4882a593Smuzhiyun {
383*4882a593Smuzhiyun 	edac_dbg(0, "\n");
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	/* take the arg 'msec' and set it into the control structure
386*4882a593Smuzhiyun 	 * to used in the time period calculation
387*4882a593Smuzhiyun 	 * then calc the number of jiffies that represents
388*4882a593Smuzhiyun 	 */
389*4882a593Smuzhiyun 	edac_dev->poll_msec = msec;
390*4882a593Smuzhiyun 	edac_dev->delay = msecs_to_jiffies(msec);
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&edac_dev->work, edac_device_workq_function);
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	/* optimize here for the 1 second case, which will be normal value, to
395*4882a593Smuzhiyun 	 * fire ON the 1 second time event. This helps reduce all sorts of
396*4882a593Smuzhiyun 	 * timers firing on sub-second basis, while they are happy
397*4882a593Smuzhiyun 	 * to fire together on the 1 second exactly
398*4882a593Smuzhiyun 	 */
399*4882a593Smuzhiyun 	if (edac_dev->poll_msec == 1000)
400*4882a593Smuzhiyun 		edac_queue_work(&edac_dev->work, round_jiffies_relative(edac_dev->delay));
401*4882a593Smuzhiyun 	else
402*4882a593Smuzhiyun 		edac_queue_work(&edac_dev->work, edac_dev->delay);
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun /*
406*4882a593Smuzhiyun  * edac_device_workq_teardown
407*4882a593Smuzhiyun  *	stop the workq processing on this edac_dev
408*4882a593Smuzhiyun  */
edac_device_workq_teardown(struct edac_device_ctl_info * edac_dev)409*4882a593Smuzhiyun static void edac_device_workq_teardown(struct edac_device_ctl_info *edac_dev)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun 	if (!edac_dev->edac_check)
412*4882a593Smuzhiyun 		return;
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	edac_dev->op_state = OP_OFFLINE;
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	edac_stop_work(&edac_dev->work);
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun /*
420*4882a593Smuzhiyun  * edac_device_reset_delay_period
421*4882a593Smuzhiyun  *
422*4882a593Smuzhiyun  *	need to stop any outstanding workq queued up at this time
423*4882a593Smuzhiyun  *	because we will be resetting the sleep time.
424*4882a593Smuzhiyun  *	Then restart the workq on the new delay
425*4882a593Smuzhiyun  */
edac_device_reset_delay_period(struct edac_device_ctl_info * edac_dev,unsigned long value)426*4882a593Smuzhiyun void edac_device_reset_delay_period(struct edac_device_ctl_info *edac_dev,
427*4882a593Smuzhiyun 					unsigned long value)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun 	unsigned long jiffs = msecs_to_jiffies(value);
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	if (value == 1000)
432*4882a593Smuzhiyun 		jiffs = round_jiffies_relative(value);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	edac_dev->poll_msec = value;
435*4882a593Smuzhiyun 	edac_dev->delay	    = jiffs;
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	edac_mod_work(&edac_dev->work, jiffs);
438*4882a593Smuzhiyun }
439*4882a593Smuzhiyun 
edac_device_alloc_index(void)440*4882a593Smuzhiyun int edac_device_alloc_index(void)
441*4882a593Smuzhiyun {
442*4882a593Smuzhiyun 	static atomic_t device_indexes = ATOMIC_INIT(0);
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	return atomic_inc_return(&device_indexes) - 1;
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(edac_device_alloc_index);
447*4882a593Smuzhiyun 
edac_device_add_device(struct edac_device_ctl_info * edac_dev)448*4882a593Smuzhiyun int edac_device_add_device(struct edac_device_ctl_info *edac_dev)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun 	edac_dbg(0, "\n");
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun #ifdef CONFIG_EDAC_DEBUG
453*4882a593Smuzhiyun 	if (edac_debug_level >= 3)
454*4882a593Smuzhiyun 		edac_device_dump_device(edac_dev);
455*4882a593Smuzhiyun #endif
456*4882a593Smuzhiyun 	mutex_lock(&device_ctls_mutex);
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	if (add_edac_dev_to_global_list(edac_dev))
459*4882a593Smuzhiyun 		goto fail0;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	/* set load time so that error rate can be tracked */
462*4882a593Smuzhiyun 	edac_dev->start_time = jiffies;
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	/* create this instance's sysfs entries */
465*4882a593Smuzhiyun 	if (edac_device_create_sysfs(edac_dev)) {
466*4882a593Smuzhiyun 		edac_device_printk(edac_dev, KERN_WARNING,
467*4882a593Smuzhiyun 					"failed to create sysfs device\n");
468*4882a593Smuzhiyun 		goto fail1;
469*4882a593Smuzhiyun 	}
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	/* If there IS a check routine, then we are running POLLED */
472*4882a593Smuzhiyun 	if (edac_dev->edac_check != NULL) {
473*4882a593Smuzhiyun 		/* This instance is NOW RUNNING */
474*4882a593Smuzhiyun 		edac_dev->op_state = OP_RUNNING_POLL;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 		/*
477*4882a593Smuzhiyun 		 * enable workq processing on this instance,
478*4882a593Smuzhiyun 		 * default = 1000 msec
479*4882a593Smuzhiyun 		 */
480*4882a593Smuzhiyun 		edac_device_workq_setup(edac_dev, 1000);
481*4882a593Smuzhiyun 	} else {
482*4882a593Smuzhiyun 		edac_dev->op_state = OP_RUNNING_INTERRUPT;
483*4882a593Smuzhiyun 	}
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	/* Report action taken */
486*4882a593Smuzhiyun 	edac_device_printk(edac_dev, KERN_INFO,
487*4882a593Smuzhiyun 		"Giving out device to module %s controller %s: DEV %s (%s)\n",
488*4882a593Smuzhiyun 		edac_dev->mod_name, edac_dev->ctl_name, edac_dev->dev_name,
489*4882a593Smuzhiyun 		edac_op_state_to_string(edac_dev->op_state));
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	mutex_unlock(&device_ctls_mutex);
492*4882a593Smuzhiyun 	return 0;
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun fail1:
495*4882a593Smuzhiyun 	/* Some error, so remove the entry from the lsit */
496*4882a593Smuzhiyun 	del_edac_device_from_global_list(edac_dev);
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun fail0:
499*4882a593Smuzhiyun 	mutex_unlock(&device_ctls_mutex);
500*4882a593Smuzhiyun 	return 1;
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(edac_device_add_device);
503*4882a593Smuzhiyun 
edac_device_del_device(struct device * dev)504*4882a593Smuzhiyun struct edac_device_ctl_info *edac_device_del_device(struct device *dev)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun 	struct edac_device_ctl_info *edac_dev;
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	edac_dbg(0, "\n");
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	mutex_lock(&device_ctls_mutex);
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	/* Find the structure on the list, if not there, then leave */
513*4882a593Smuzhiyun 	edac_dev = find_edac_device_by_dev(dev);
514*4882a593Smuzhiyun 	if (edac_dev == NULL) {
515*4882a593Smuzhiyun 		mutex_unlock(&device_ctls_mutex);
516*4882a593Smuzhiyun 		return NULL;
517*4882a593Smuzhiyun 	}
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	/* mark this instance as OFFLINE */
520*4882a593Smuzhiyun 	edac_dev->op_state = OP_OFFLINE;
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	/* deregister from global list */
523*4882a593Smuzhiyun 	del_edac_device_from_global_list(edac_dev);
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	mutex_unlock(&device_ctls_mutex);
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	/* clear workq processing on this instance */
528*4882a593Smuzhiyun 	edac_device_workq_teardown(edac_dev);
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	/* Tear down the sysfs entries for this instance */
531*4882a593Smuzhiyun 	edac_device_remove_sysfs(edac_dev);
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	edac_printk(KERN_INFO, EDAC_MC,
534*4882a593Smuzhiyun 		"Removed device %d for %s %s: DEV %s\n",
535*4882a593Smuzhiyun 		edac_dev->dev_idx,
536*4882a593Smuzhiyun 		edac_dev->mod_name, edac_dev->ctl_name, edac_dev_name(edac_dev));
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	return edac_dev;
539*4882a593Smuzhiyun }
540*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(edac_device_del_device);
541*4882a593Smuzhiyun 
edac_device_get_log_ce(struct edac_device_ctl_info * edac_dev)542*4882a593Smuzhiyun static inline int edac_device_get_log_ce(struct edac_device_ctl_info *edac_dev)
543*4882a593Smuzhiyun {
544*4882a593Smuzhiyun 	return edac_dev->log_ce;
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun 
edac_device_get_log_ue(struct edac_device_ctl_info * edac_dev)547*4882a593Smuzhiyun static inline int edac_device_get_log_ue(struct edac_device_ctl_info *edac_dev)
548*4882a593Smuzhiyun {
549*4882a593Smuzhiyun 	return edac_dev->log_ue;
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun 
edac_device_get_panic_on_ue(struct edac_device_ctl_info * edac_dev)552*4882a593Smuzhiyun static inline int edac_device_get_panic_on_ue(struct edac_device_ctl_info
553*4882a593Smuzhiyun 					*edac_dev)
554*4882a593Smuzhiyun {
555*4882a593Smuzhiyun 	return edac_dev->panic_on_ue;
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun 
edac_device_handle_ce_count(struct edac_device_ctl_info * edac_dev,unsigned int count,int inst_nr,int block_nr,const char * msg)558*4882a593Smuzhiyun void edac_device_handle_ce_count(struct edac_device_ctl_info *edac_dev,
559*4882a593Smuzhiyun 				 unsigned int count, int inst_nr, int block_nr,
560*4882a593Smuzhiyun 				 const char *msg)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun 	struct edac_device_instance *instance;
563*4882a593Smuzhiyun 	struct edac_device_block *block = NULL;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	if (!count)
566*4882a593Smuzhiyun 		return;
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
569*4882a593Smuzhiyun 		edac_device_printk(edac_dev, KERN_ERR,
570*4882a593Smuzhiyun 				"INTERNAL ERROR: 'instance' out of range "
571*4882a593Smuzhiyun 				"(%d >= %d)\n", inst_nr,
572*4882a593Smuzhiyun 				edac_dev->nr_instances);
573*4882a593Smuzhiyun 		return;
574*4882a593Smuzhiyun 	}
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	instance = edac_dev->instances + inst_nr;
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 	if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
579*4882a593Smuzhiyun 		edac_device_printk(edac_dev, KERN_ERR,
580*4882a593Smuzhiyun 				"INTERNAL ERROR: instance %d 'block' "
581*4882a593Smuzhiyun 				"out of range (%d >= %d)\n",
582*4882a593Smuzhiyun 				inst_nr, block_nr,
583*4882a593Smuzhiyun 				instance->nr_blocks);
584*4882a593Smuzhiyun 		return;
585*4882a593Smuzhiyun 	}
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	if (instance->nr_blocks > 0) {
588*4882a593Smuzhiyun 		block = instance->blocks + block_nr;
589*4882a593Smuzhiyun 		block->counters.ce_count += count;
590*4882a593Smuzhiyun 	}
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	/* Propagate the count up the 'totals' tree */
593*4882a593Smuzhiyun 	instance->counters.ce_count += count;
594*4882a593Smuzhiyun 	edac_dev->counters.ce_count += count;
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	if (edac_device_get_log_ce(edac_dev))
597*4882a593Smuzhiyun 		edac_device_printk(edac_dev, KERN_WARNING,
598*4882a593Smuzhiyun 				   "CE: %s instance: %s block: %s count: %d '%s'\n",
599*4882a593Smuzhiyun 				   edac_dev->ctl_name, instance->name,
600*4882a593Smuzhiyun 				   block ? block->name : "N/A", count, msg);
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(edac_device_handle_ce_count);
603*4882a593Smuzhiyun 
edac_device_handle_ue_count(struct edac_device_ctl_info * edac_dev,unsigned int count,int inst_nr,int block_nr,const char * msg)604*4882a593Smuzhiyun void edac_device_handle_ue_count(struct edac_device_ctl_info *edac_dev,
605*4882a593Smuzhiyun 				 unsigned int count, int inst_nr, int block_nr,
606*4882a593Smuzhiyun 				 const char *msg)
607*4882a593Smuzhiyun {
608*4882a593Smuzhiyun 	struct edac_device_instance *instance;
609*4882a593Smuzhiyun 	struct edac_device_block *block = NULL;
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	if (!count)
612*4882a593Smuzhiyun 		return;
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	if ((inst_nr >= edac_dev->nr_instances) || (inst_nr < 0)) {
615*4882a593Smuzhiyun 		edac_device_printk(edac_dev, KERN_ERR,
616*4882a593Smuzhiyun 				"INTERNAL ERROR: 'instance' out of range "
617*4882a593Smuzhiyun 				"(%d >= %d)\n", inst_nr,
618*4882a593Smuzhiyun 				edac_dev->nr_instances);
619*4882a593Smuzhiyun 		return;
620*4882a593Smuzhiyun 	}
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	instance = edac_dev->instances + inst_nr;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	if ((block_nr >= instance->nr_blocks) || (block_nr < 0)) {
625*4882a593Smuzhiyun 		edac_device_printk(edac_dev, KERN_ERR,
626*4882a593Smuzhiyun 				"INTERNAL ERROR: instance %d 'block' "
627*4882a593Smuzhiyun 				"out of range (%d >= %d)\n",
628*4882a593Smuzhiyun 				inst_nr, block_nr,
629*4882a593Smuzhiyun 				instance->nr_blocks);
630*4882a593Smuzhiyun 		return;
631*4882a593Smuzhiyun 	}
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	if (instance->nr_blocks > 0) {
634*4882a593Smuzhiyun 		block = instance->blocks + block_nr;
635*4882a593Smuzhiyun 		block->counters.ue_count += count;
636*4882a593Smuzhiyun 	}
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun 	/* Propagate the count up the 'totals' tree */
639*4882a593Smuzhiyun 	instance->counters.ue_count += count;
640*4882a593Smuzhiyun 	edac_dev->counters.ue_count += count;
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	if (edac_device_get_log_ue(edac_dev))
643*4882a593Smuzhiyun 		edac_device_printk(edac_dev, KERN_EMERG,
644*4882a593Smuzhiyun 				   "UE: %s instance: %s block: %s count: %d '%s'\n",
645*4882a593Smuzhiyun 				   edac_dev->ctl_name, instance->name,
646*4882a593Smuzhiyun 				   block ? block->name : "N/A", count, msg);
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	if (edac_device_get_panic_on_ue(edac_dev))
649*4882a593Smuzhiyun 		panic("EDAC %s: UE instance: %s block %s count: %d '%s'\n",
650*4882a593Smuzhiyun 		      edac_dev->ctl_name, instance->name,
651*4882a593Smuzhiyun 		      block ? block->name : "N/A", count, msg);
652*4882a593Smuzhiyun }
653*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(edac_device_handle_ue_count);
654