4 * Author: Dave Jiang <djiang@mvista.com>
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
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/sysdev.h>
23 #include <linux/ctype.h>
24 #include <linux/workqueue.h>
25 #include <asm/uaccess.h>
28 #include "edac_core.h"
29 #include "edac_module.h"
31 static DEFINE_MUTEX(edac_pci_ctls_mutex);
32 static struct list_head edac_pci_list = LIST_HEAD_INIT(edac_pci_list);
34 static inline void edac_lock_pci_list(void)
36 mutex_lock(&edac_pci_ctls_mutex);
39 static inline void edac_unlock_pci_list(void)
41 mutex_unlock(&edac_pci_ctls_mutex);
45 * The alloc() and free() functions for the 'edac_pci' control info
46 * structure. The chip driver will allocate one of these for each
47 * edac_pci it is going to control/register with the EDAC CORE.
49 struct edac_pci_ctl_info * edac_pci_alloc_ctl_info(
51 const char *edac_pci_name)
53 struct edac_pci_ctl_info *pci;
57 pci = (struct edac_pci_ctl_info *)0;
58 pvt = edac_align_ptr(&pci[1], sz_pvt);
59 size = ((unsigned long)pvt) + sz_pvt;
61 if ((pci = kzalloc(size, GFP_KERNEL)) == NULL)
64 pvt = sz_pvt ? ((char *)pci) + ((unsigned long)pvt) : NULL;
68 pci->op_state = OP_ALLOC;
70 snprintf(pci->name, strlen(edac_pci_name)+1, "%s", edac_pci_name);
74 EXPORT_SYMBOL_GPL(edac_pci_alloc_ctl_info);
77 * edac_pci_free_ctl_info()
78 * frees the memory allocated by edac_pci_alloc_ctl_info() function
80 void edac_pci_free_ctl_info(struct edac_pci_ctl_info *pci)
84 EXPORT_SYMBOL_GPL(edac_pci_free_ctl_info);
87 * find_edac_pci_by_dev()
88 * scans the edac_pci list for a specific 'struct device *'
90 static struct edac_pci_ctl_info * find_edac_pci_by_dev(struct device *dev)
92 struct edac_pci_ctl_info *pci;
93 struct list_head *item;
95 debugf3("%s()\n", __func__);
97 list_for_each(item, &edac_pci_list) {
98 pci = list_entry(item, struct edac_pci_ctl_info, link);
108 * add_edac_pci_to_global_list
109 * Before calling this function, caller must assign a unique value to
115 static int add_edac_pci_to_global_list(struct edac_pci_ctl_info *pci)
117 struct list_head *item, *insert_before;
118 struct edac_pci_ctl_info *rover;
120 insert_before = &edac_pci_list;
122 /* Determine if already on the list */
123 if (unlikely((rover = find_edac_pci_by_dev(pci->dev)) != NULL))
126 /* Insert in ascending order by 'pci_idx', so find position */
127 list_for_each(item, &edac_pci_list) {
128 rover = list_entry(item, struct edac_pci_ctl_info, link);
130 if (rover->pci_idx >= pci->pci_idx) {
131 if (unlikely(rover->pci_idx == pci->pci_idx))
134 insert_before = item;
139 list_add_tail_rcu(&pci->link, insert_before);
143 edac_printk(KERN_WARNING, EDAC_PCI,
144 "%s (%s) %s %s already assigned %d\n",
145 rover->dev->bus_id, dev_name(rover),
146 rover->mod_name, rover->ctl_name, rover->pci_idx);
150 edac_printk(KERN_WARNING, EDAC_PCI,
151 "but in low-level driver: attempt to assign\n"
152 "\tduplicate pci_idx %d in %s()\n", rover->pci_idx, __func__);
157 * complete_edac_pci_list_del
159 static void complete_edac_pci_list_del(struct rcu_head *head)
161 struct edac_pci_ctl_info *pci;
163 pci = container_of(head, struct edac_pci_ctl_info, rcu);
164 INIT_LIST_HEAD(&pci->link);
165 complete(&pci->complete);
169 * del_edac_pci_from_global_list
171 static void del_edac_pci_from_global_list(struct edac_pci_ctl_info *pci)
173 list_del_rcu(&pci->link);
174 init_completion(&pci->complete);
175 call_rcu(&pci->rcu, complete_edac_pci_list_del);
176 wait_for_completion(&pci->complete);
181 * Search for an edac_pci_ctl_info structure whose index is 'idx'
183 * If found, return a pointer to the structure
186 * Caller must hold pci_ctls_mutex.
188 struct edac_pci_ctl_info * edac_pci_find(int idx)
190 struct list_head *item;
191 struct edac_pci_ctl_info *pci;
193 /* Iterage over list, looking for exact match of ID */
194 list_for_each(item, &edac_pci_list) {
195 pci = list_entry(item, struct edac_pci_ctl_info, link);
197 if (pci->pci_idx >= idx) {
198 if (pci->pci_idx == idx)
201 /* not on list, so terminate early */
208 EXPORT_SYMBOL_GPL(edac_pci_find);
211 * edac_pci_workq_function()
212 * performs the operation scheduled by a workq request
214 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
215 static void edac_pci_workq_function(struct work_struct *work_req)
217 struct delayed_work *d_work = (struct delayed_work *)work_req;
218 struct edac_pci_ctl_info *pci = to_edac_pci_ctl_work(d_work);
220 static void edac_pci_workq_function(void *ptr)
222 struct edac_pci_ctl_info *pci = ptr;
225 edac_lock_pci_list();
227 if ((pci->op_state == OP_RUNNING_POLL) &&
228 (pci->edac_check != NULL) &&
229 (pci->check_parity_error))
230 pci->edac_check(pci);
232 edac_unlock_pci_list();
235 queue_delayed_work(edac_workqueue, &pci->work, pci->delay);
239 * edac_pci_workq_setup()
240 * initialize a workq item for this edac_pci instance
241 * passing in the new delay period in msec
243 static void edac_pci_workq_setup(struct edac_pci_ctl_info *pci,
246 debugf0("%s()\n", __func__);
248 pci->poll_msec = msec;
249 edac_calc_delay(pci);
251 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
252 INIT_DELAYED_WORK(&pci->work, edac_pci_workq_function);
254 INIT_WORK(&pci->work, edac_pci_workq_function, pci);
256 queue_delayed_work(edac_workqueue, &pci->work, pci->delay);
260 * edac_pci_workq_teardown()
261 * stop the workq processing on this edac_pci instance
263 static void edac_pci_workq_teardown(struct edac_pci_ctl_info *pci)
267 status = cancel_delayed_work(&pci->work);
269 flush_workqueue(edac_workqueue);
273 * edac_pci_reset_delay_period
275 void edac_pci_reset_delay_period(struct edac_pci_ctl_info *pci,
278 edac_lock_pci_list();
280 edac_pci_workq_teardown(pci);
282 edac_pci_workq_setup(pci, value);
284 edac_unlock_pci_list();
286 EXPORT_SYMBOL_GPL(edac_pci_reset_delay_period);
289 * edac_pci_add_device: Insert the 'edac_dev' structure into the
290 * edac_pci global list and create sysfs entries associated with
291 * edac_pci structure.
292 * @pci: pointer to the edac_device structure to be added to the list
293 * @edac_idx: A unique numeric identifier to be assigned to the
294 * 'edac_pci' structure.
300 int edac_pci_add_device(struct edac_pci_ctl_info *pci, int edac_idx)
302 debugf0("%s()\n", __func__);
304 pci->pci_idx = edac_idx;
306 edac_lock_pci_list();
308 if (add_edac_pci_to_global_list(pci))
311 pci->start_time = jiffies;
313 if (edac_pci_create_sysfs(pci)) {
314 edac_pci_printk(pci, KERN_WARNING,
315 "failed to create sysfs pci\n");
319 if (pci->edac_check != NULL) {
320 pci->op_state = OP_RUNNING_POLL;
322 edac_pci_workq_setup(pci, 1000);
324 pci->op_state = OP_RUNNING_INTERRUPT;
327 edac_pci_printk(pci, KERN_INFO,
328 "Giving out device to module '%s' controller '%s':"
333 edac_op_state_toString(pci->op_state));
335 edac_unlock_pci_list();
339 del_edac_pci_from_global_list(pci);
341 edac_unlock_pci_list();
344 EXPORT_SYMBOL_GPL(edac_pci_add_device);
347 * edac_pci_del_device()
348 * Remove sysfs entries for specified edac_pci structure and
349 * then remove edac_pci structure from global list
352 * Pointer to 'struct device' representing edac_pci structure
356 * Pointer to removed edac_pci structure,
357 * or NULL if device not found
359 struct edac_pci_ctl_info * edac_pci_del_device(struct device *dev)
361 struct edac_pci_ctl_info *pci;
363 debugf0("%s()\n", __func__);
365 edac_lock_pci_list();
367 if ((pci = find_edac_pci_by_dev(dev)) == NULL) {
368 edac_unlock_pci_list();
372 pci->op_state = OP_OFFLINE;
374 edac_pci_workq_teardown(pci);
376 edac_pci_remove_sysfs(pci);
378 del_edac_pci_from_global_list(pci);
380 edac_unlock_pci_list();
382 edac_printk(KERN_INFO, EDAC_PCI,
383 "Removed device %d for %s %s: DEV %s\n",
391 EXPORT_SYMBOL_GPL(edac_pci_del_device);
393 static inline int edac_pci_get_log_pe(struct edac_pci_ctl_info *pci)
395 return pci->log_parity_error;
398 static inline int edac_pci_get_panic_on_pe(struct edac_pci_ctl_info *pci)
400 return pci->panic_on_pe;
403 void edac_pci_generic_check(struct edac_pci_ctl_info *pci)
405 edac_pci_do_parity_check();
408 static int edac_pci_idx = 0;
409 #define EDAC_PCI_GENCTL_NAME "EDAC PCI controller"
411 struct edac_pci_gen_data {
415 struct edac_pci_ctl_info *
416 edac_pci_create_generic_ctl(struct device *dev, const char *mod_name)
418 struct edac_pci_ctl_info *pci;
419 struct edac_pci_gen_data *pdata;
421 pci = edac_pci_alloc_ctl_info(sizeof(*pdata), EDAC_PCI_GENCTL_NAME);
425 pdata = pci->pvt_info;
427 dev_set_drvdata(pci->dev, pci);
428 pci->dev_name = pci_name(to_pci_dev(dev));
430 pci->mod_name = mod_name;
431 pci->ctl_name = EDAC_PCI_GENCTL_NAME;
432 pci->edac_check = edac_pci_generic_check;
434 pdata->edac_idx = edac_pci_idx++;
436 if (edac_pci_add_device(pci, pdata->edac_idx) > 0) {
437 debugf3("%s(): failed edac_pci_add_device()\n", __func__);
438 edac_pci_free_ctl_info(pci);
444 EXPORT_SYMBOL_GPL(edac_pci_create_generic_ctl);
446 void edac_pci_release_generic_ctl(struct edac_pci_ctl_info *pci)
448 edac_pci_del_device(pci->dev);
449 edac_pci_free_ctl_info(pci);
451 EXPORT_SYMBOL_GPL(edac_pci_release_generic_ctl);