1/*
2 * EDAC PCI component
3 *
4 * Author: Dave Jiang <djiang@mvista.com>
5 *
6 * 2007 (c) MontaVista Software, Inc. This file is licensed under
7 * the terms of the GNU General Public License version 2. This program
8 * is licensed "as is" without any warranty of any kind, whether express
9 * or implied.
10 *
11 */
12#include <linux/module.h>
13#include <linux/types.h>
14#include <linux/smp.h>
15#include <linux/init.h>
16#include <linux/sysctl.h>
17#include <linux/highmem.h>
18#include <linux/timer.h>
19#include <linux/slab.h>
20#include <linux/spinlock.h>
21#include <linux/list.h>
22#include <linux/ctype.h>
23#include <linux/workqueue.h>
24#include <asm/uaccess.h>
25#include <asm/page.h>
26
27#include "edac_core.h"
28#include "edac_module.h"
29
30static DEFINE_MUTEX(edac_pci_ctls_mutex);
31static LIST_HEAD(edac_pci_list);
32static atomic_t pci_indexes = ATOMIC_INIT(0);
33
34/*
35 * edac_pci_alloc_ctl_info
36 *
37 *	The alloc() function for the 'edac_pci' control info
38 *	structure. The chip driver will allocate one of these for each
39 *	edac_pci it is going to control/register with the EDAC CORE.
40 */
41struct edac_pci_ctl_info *edac_pci_alloc_ctl_info(unsigned int sz_pvt,
42						const char *edac_pci_name)
43{
44	struct edac_pci_ctl_info *pci;
45	void *p = NULL, *pvt;
46	unsigned int size;
47
48	edac_dbg(1, "\n");
49
50	pci = edac_align_ptr(&p, sizeof(*pci), 1);
51	pvt = edac_align_ptr(&p, 1, sz_pvt);
52	size = ((unsigned long)pvt) + sz_pvt;
53
54	/* Alloc the needed control struct memory */
55	pci = kzalloc(size, GFP_KERNEL);
56	if (pci  == NULL)
57		return NULL;
58
59	/* Now much private space */
60	pvt = sz_pvt ? ((char *)pci) + ((unsigned long)pvt) : NULL;
61
62	pci->pvt_info = pvt;
63	pci->op_state = OP_ALLOC;
64
65	snprintf(pci->name, strlen(edac_pci_name) + 1, "%s", edac_pci_name);
66
67	return pci;
68}
69EXPORT_SYMBOL_GPL(edac_pci_alloc_ctl_info);
70
71/*
72 * edac_pci_free_ctl_info()
73 *
74 *	Last action on the pci control structure.
75 *
76 *	call the remove sysfs information, which will unregister
77 *	this control struct's kobj. When that kobj's ref count
78 *	goes to zero, its release function will be call and then
79 *	kfree() the memory.
80 */
81void edac_pci_free_ctl_info(struct edac_pci_ctl_info *pci)
82{
83	edac_dbg(1, "\n");
84
85	edac_pci_remove_sysfs(pci);
86}
87EXPORT_SYMBOL_GPL(edac_pci_free_ctl_info);
88
89/*
90 * find_edac_pci_by_dev()
91 * 	scans the edac_pci list for a specific 'struct device *'
92 *
93 *	return NULL if not found, or return control struct pointer
94 */
95static struct edac_pci_ctl_info *find_edac_pci_by_dev(struct device *dev)
96{
97	struct edac_pci_ctl_info *pci;
98	struct list_head *item;
99
100	edac_dbg(1, "\n");
101
102	list_for_each(item, &edac_pci_list) {
103		pci = list_entry(item, struct edac_pci_ctl_info, link);
104
105		if (pci->dev == dev)
106			return pci;
107	}
108
109	return NULL;
110}
111
112/*
113 * add_edac_pci_to_global_list
114 * 	Before calling this function, caller must assign a unique value to
115 * 	edac_dev->pci_idx.
116 * 	Return:
117 * 		0 on success
118 * 		1 on failure
119 */
120static int add_edac_pci_to_global_list(struct edac_pci_ctl_info *pci)
121{
122	struct list_head *item, *insert_before;
123	struct edac_pci_ctl_info *rover;
124
125	edac_dbg(1, "\n");
126
127	insert_before = &edac_pci_list;
128
129	/* Determine if already on the list */
130	rover = find_edac_pci_by_dev(pci->dev);
131	if (unlikely(rover != NULL))
132		goto fail0;
133
134	/* Insert in ascending order by 'pci_idx', so find position */
135	list_for_each(item, &edac_pci_list) {
136		rover = list_entry(item, struct edac_pci_ctl_info, link);
137
138		if (rover->pci_idx >= pci->pci_idx) {
139			if (unlikely(rover->pci_idx == pci->pci_idx))
140				goto fail1;
141
142			insert_before = item;
143			break;
144		}
145	}
146
147	list_add_tail_rcu(&pci->link, insert_before);
148	return 0;
149
150fail0:
151	edac_printk(KERN_WARNING, EDAC_PCI,
152		"%s (%s) %s %s already assigned %d\n",
153		dev_name(rover->dev), edac_dev_name(rover),
154		rover->mod_name, rover->ctl_name, rover->pci_idx);
155	return 1;
156
157fail1:
158	edac_printk(KERN_WARNING, EDAC_PCI,
159		"but in low-level driver: attempt to assign\n"
160		"\tduplicate pci_idx %d in %s()\n", rover->pci_idx,
161		__func__);
162	return 1;
163}
164
165/*
166 * del_edac_pci_from_global_list
167 *
168 *	remove the PCI control struct from the global list
169 */
170static void del_edac_pci_from_global_list(struct edac_pci_ctl_info *pci)
171{
172	list_del_rcu(&pci->link);
173
174	/* these are for safe removal of devices from global list while
175	 * NMI handlers may be traversing list
176	 */
177	synchronize_rcu();
178	INIT_LIST_HEAD(&pci->link);
179}
180
181#if 0
182/* Older code, but might use in the future */
183
184/*
185 * edac_pci_find()
186 * 	Search for an edac_pci_ctl_info structure whose index is 'idx'
187 *
188 * If found, return a pointer to the structure
189 * Else return NULL.
190 *
191 * Caller must hold pci_ctls_mutex.
192 */
193struct edac_pci_ctl_info *edac_pci_find(int idx)
194{
195	struct list_head *item;
196	struct edac_pci_ctl_info *pci;
197
198	/* Iterage over list, looking for exact match of ID */
199	list_for_each(item, &edac_pci_list) {
200		pci = list_entry(item, struct edac_pci_ctl_info, link);
201
202		if (pci->pci_idx >= idx) {
203			if (pci->pci_idx == idx)
204				return pci;
205
206			/* not on list, so terminate early */
207			break;
208		}
209	}
210
211	return NULL;
212}
213EXPORT_SYMBOL_GPL(edac_pci_find);
214#endif
215
216/*
217 * edac_pci_workq_function()
218 *
219 * 	periodic function that performs the operation
220 *	scheduled by a workq request, for a given PCI control struct
221 */
222static void edac_pci_workq_function(struct work_struct *work_req)
223{
224	struct delayed_work *d_work = to_delayed_work(work_req);
225	struct edac_pci_ctl_info *pci = to_edac_pci_ctl_work(d_work);
226	int msec;
227	unsigned long delay;
228
229	edac_dbg(3, "checking\n");
230
231	mutex_lock(&edac_pci_ctls_mutex);
232
233	if (pci->op_state == OP_RUNNING_POLL) {
234		/* we might be in POLL mode, but there may NOT be a poll func
235		 */
236		if ((pci->edac_check != NULL) && edac_pci_get_check_errors())
237			pci->edac_check(pci);
238
239		/* if we are on a one second period, then use round */
240		msec = edac_pci_get_poll_msec();
241		if (msec == 1000)
242			delay = round_jiffies_relative(msecs_to_jiffies(msec));
243		else
244			delay = msecs_to_jiffies(msec);
245
246		/* Reschedule only if we are in POLL mode */
247		queue_delayed_work(edac_workqueue, &pci->work, delay);
248	}
249
250	mutex_unlock(&edac_pci_ctls_mutex);
251}
252
253/*
254 * edac_pci_workq_setup()
255 * 	initialize a workq item for this edac_pci instance
256 * 	passing in the new delay period in msec
257 *
258 *	locking model:
259 *		called when 'edac_pci_ctls_mutex' is locked
260 */
261static void edac_pci_workq_setup(struct edac_pci_ctl_info *pci,
262				 unsigned int msec)
263{
264	edac_dbg(0, "\n");
265
266	INIT_DELAYED_WORK(&pci->work, edac_pci_workq_function);
267	queue_delayed_work(edac_workqueue, &pci->work,
268			msecs_to_jiffies(edac_pci_get_poll_msec()));
269}
270
271/*
272 * edac_pci_workq_teardown()
273 * 	stop the workq processing on this edac_pci instance
274 */
275static void edac_pci_workq_teardown(struct edac_pci_ctl_info *pci)
276{
277	edac_dbg(0, "\n");
278
279	pci->op_state = OP_OFFLINE;
280
281	cancel_delayed_work_sync(&pci->work);
282	flush_workqueue(edac_workqueue);
283}
284
285/*
286 * edac_pci_reset_delay_period
287 *
288 *	called with a new period value for the workq period
289 *	a) stop current workq timer
290 *	b) restart workq timer with new value
291 */
292void edac_pci_reset_delay_period(struct edac_pci_ctl_info *pci,
293				 unsigned long value)
294{
295	edac_dbg(0, "\n");
296
297	edac_pci_workq_teardown(pci);
298
299	/* need to lock for the setup */
300	mutex_lock(&edac_pci_ctls_mutex);
301
302	edac_pci_workq_setup(pci, value);
303
304	mutex_unlock(&edac_pci_ctls_mutex);
305}
306EXPORT_SYMBOL_GPL(edac_pci_reset_delay_period);
307
308/*
309 * edac_pci_alloc_index: Allocate a unique PCI index number
310 *
311 * Return:
312 *      allocated index number
313 *
314 */
315int edac_pci_alloc_index(void)
316{
317	return atomic_inc_return(&pci_indexes) - 1;
318}
319EXPORT_SYMBOL_GPL(edac_pci_alloc_index);
320
321/*
322 * edac_pci_add_device: Insert the 'edac_dev' structure into the
323 * edac_pci global list and create sysfs entries associated with
324 * edac_pci structure.
325 * @pci: pointer to the edac_device structure to be added to the list
326 * @edac_idx: A unique numeric identifier to be assigned to the
327 * 'edac_pci' structure.
328 *
329 * Return:
330 *      0       Success
331 *      !0      Failure
332 */
333int edac_pci_add_device(struct edac_pci_ctl_info *pci, int edac_idx)
334{
335	edac_dbg(0, "\n");
336
337	pci->pci_idx = edac_idx;
338	pci->start_time = jiffies;
339
340	mutex_lock(&edac_pci_ctls_mutex);
341
342	if (add_edac_pci_to_global_list(pci))
343		goto fail0;
344
345	if (edac_pci_create_sysfs(pci)) {
346		edac_pci_printk(pci, KERN_WARNING,
347				"failed to create sysfs pci\n");
348		goto fail1;
349	}
350
351	if (pci->edac_check != NULL) {
352		pci->op_state = OP_RUNNING_POLL;
353
354		edac_pci_workq_setup(pci, 1000);
355	} else {
356		pci->op_state = OP_RUNNING_INTERRUPT;
357	}
358
359	edac_pci_printk(pci, KERN_INFO,
360		"Giving out device to module %s controller %s: DEV %s (%s)\n",
361		pci->mod_name, pci->ctl_name, pci->dev_name,
362		edac_op_state_to_string(pci->op_state));
363
364	mutex_unlock(&edac_pci_ctls_mutex);
365	return 0;
366
367	/* error unwind stack */
368fail1:
369	del_edac_pci_from_global_list(pci);
370fail0:
371	mutex_unlock(&edac_pci_ctls_mutex);
372	return 1;
373}
374EXPORT_SYMBOL_GPL(edac_pci_add_device);
375
376/*
377 * edac_pci_del_device()
378 * 	Remove sysfs entries for specified edac_pci structure and
379 * 	then remove edac_pci structure from global list
380 *
381 * @dev:
382 * 	Pointer to 'struct device' representing edac_pci structure
383 * 	to remove
384 *
385 * Return:
386 * 	Pointer to removed edac_pci structure,
387 * 	or NULL if device not found
388 */
389struct edac_pci_ctl_info *edac_pci_del_device(struct device *dev)
390{
391	struct edac_pci_ctl_info *pci;
392
393	edac_dbg(0, "\n");
394
395	mutex_lock(&edac_pci_ctls_mutex);
396
397	/* ensure the control struct is on the global list
398	 * if not, then leave
399	 */
400	pci = find_edac_pci_by_dev(dev);
401	if (pci  == NULL) {
402		mutex_unlock(&edac_pci_ctls_mutex);
403		return NULL;
404	}
405
406	pci->op_state = OP_OFFLINE;
407
408	del_edac_pci_from_global_list(pci);
409
410	mutex_unlock(&edac_pci_ctls_mutex);
411
412	/* stop the workq timer */
413	edac_pci_workq_teardown(pci);
414
415	edac_printk(KERN_INFO, EDAC_PCI,
416		"Removed device %d for %s %s: DEV %s\n",
417		pci->pci_idx, pci->mod_name, pci->ctl_name, edac_dev_name(pci));
418
419	return pci;
420}
421EXPORT_SYMBOL_GPL(edac_pci_del_device);
422
423/*
424 * edac_pci_generic_check
425 *
426 *	a Generic parity check API
427 */
428static void edac_pci_generic_check(struct edac_pci_ctl_info *pci)
429{
430	edac_dbg(4, "\n");
431	edac_pci_do_parity_check();
432}
433
434/* free running instance index counter */
435static int edac_pci_idx;
436#define EDAC_PCI_GENCTL_NAME	"EDAC PCI controller"
437
438struct edac_pci_gen_data {
439	int edac_idx;
440};
441
442/*
443 * edac_pci_create_generic_ctl
444 *
445 *	A generic constructor for a PCI parity polling device
446 *	Some systems have more than one domain of PCI busses.
447 *	For systems with one domain, then this API will
448 *	provide for a generic poller.
449 *
450 *	This routine calls the edac_pci_alloc_ctl_info() for
451 *	the generic device, with default values
452 */
453struct edac_pci_ctl_info *edac_pci_create_generic_ctl(struct device *dev,
454						const char *mod_name)
455{
456	struct edac_pci_ctl_info *pci;
457	struct edac_pci_gen_data *pdata;
458
459	pci = edac_pci_alloc_ctl_info(sizeof(*pdata), EDAC_PCI_GENCTL_NAME);
460	if (!pci)
461		return NULL;
462
463	pdata = pci->pvt_info;
464	pci->dev = dev;
465	dev_set_drvdata(pci->dev, pci);
466	pci->dev_name = pci_name(to_pci_dev(dev));
467
468	pci->mod_name = mod_name;
469	pci->ctl_name = EDAC_PCI_GENCTL_NAME;
470	if (edac_op_state == EDAC_OPSTATE_POLL)
471		pci->edac_check = edac_pci_generic_check;
472
473	pdata->edac_idx = edac_pci_idx++;
474
475	if (edac_pci_add_device(pci, pdata->edac_idx) > 0) {
476		edac_dbg(3, "failed edac_pci_add_device()\n");
477		edac_pci_free_ctl_info(pci);
478		return NULL;
479	}
480
481	return pci;
482}
483EXPORT_SYMBOL_GPL(edac_pci_create_generic_ctl);
484
485/*
486 * edac_pci_release_generic_ctl
487 *
488 *	The release function of a generic EDAC PCI polling device
489 */
490void edac_pci_release_generic_ctl(struct edac_pci_ctl_info *pci)
491{
492	edac_dbg(0, "pci mod=%s\n", pci->mod_name);
493
494	edac_pci_del_device(pci->dev);
495	edac_pci_free_ctl_info(pci);
496}
497EXPORT_SYMBOL_GPL(edac_pci_release_generic_ctl);
498