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
13 #include <linux/uaccess.h>
14 #include <linux/ctype.h>
15 #include <linux/highmem.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/smp.h>
20 #include <linux/spinlock.h>
21 #include <linux/sysctl.h>
22 #include <linux/timer.h>
25 #include "edac_module.h"
27 static DEFINE_MUTEX(edac_pci_ctls_mutex);
28 static LIST_HEAD(edac_pci_list);
29 static atomic_t pci_indexes = ATOMIC_INIT(0);
31 struct edac_pci_ctl_info *edac_pci_alloc_ctl_info(unsigned int sz_pvt,
32 const char *edac_pci_name)
34 struct edac_pci_ctl_info *pci;
38 pci = kzalloc(sizeof(struct edac_pci_ctl_info), GFP_KERNEL);
43 pci->pvt_info = kzalloc(sz_pvt, GFP_KERNEL);
48 pci->op_state = OP_ALLOC;
50 snprintf(pci->name, strlen(edac_pci_name) + 1, "%s", edac_pci_name);
58 EXPORT_SYMBOL_GPL(edac_pci_alloc_ctl_info);
60 void edac_pci_free_ctl_info(struct edac_pci_ctl_info *pci)
64 edac_pci_remove_sysfs(pci);
66 EXPORT_SYMBOL_GPL(edac_pci_free_ctl_info);
69 * find_edac_pci_by_dev()
70 * scans the edac_pci list for a specific 'struct device *'
72 * return NULL if not found, or return control struct pointer
74 static struct edac_pci_ctl_info *find_edac_pci_by_dev(struct device *dev)
76 struct edac_pci_ctl_info *pci;
77 struct list_head *item;
81 list_for_each(item, &edac_pci_list) {
82 pci = list_entry(item, struct edac_pci_ctl_info, link);
92 * add_edac_pci_to_global_list
93 * Before calling this function, caller must assign a unique value to
99 static int add_edac_pci_to_global_list(struct edac_pci_ctl_info *pci)
101 struct list_head *item, *insert_before;
102 struct edac_pci_ctl_info *rover;
106 insert_before = &edac_pci_list;
108 /* Determine if already on the list */
109 rover = find_edac_pci_by_dev(pci->dev);
110 if (unlikely(rover != NULL))
113 /* Insert in ascending order by 'pci_idx', so find position */
114 list_for_each(item, &edac_pci_list) {
115 rover = list_entry(item, struct edac_pci_ctl_info, link);
117 if (rover->pci_idx >= pci->pci_idx) {
118 if (unlikely(rover->pci_idx == pci->pci_idx))
121 insert_before = item;
126 list_add_tail_rcu(&pci->link, insert_before);
130 edac_printk(KERN_WARNING, EDAC_PCI,
131 "%s (%s) %s %s already assigned %d\n",
132 dev_name(rover->dev), edac_dev_name(rover),
133 rover->mod_name, rover->ctl_name, rover->pci_idx);
137 edac_printk(KERN_WARNING, EDAC_PCI,
138 "but in low-level driver: attempt to assign\n"
139 "\tduplicate pci_idx %d in %s()\n", rover->pci_idx,
145 * del_edac_pci_from_global_list
147 * remove the PCI control struct from the global list
149 static void del_edac_pci_from_global_list(struct edac_pci_ctl_info *pci)
151 list_del_rcu(&pci->link);
153 /* these are for safe removal of devices from global list while
154 * NMI handlers may be traversing list
157 INIT_LIST_HEAD(&pci->link);
161 * edac_pci_workq_function()
163 * periodic function that performs the operation
164 * scheduled by a workq request, for a given PCI control struct
166 static void edac_pci_workq_function(struct work_struct *work_req)
168 struct delayed_work *d_work = to_delayed_work(work_req);
169 struct edac_pci_ctl_info *pci = to_edac_pci_ctl_work(d_work);
173 edac_dbg(3, "checking\n");
175 mutex_lock(&edac_pci_ctls_mutex);
177 if (pci->op_state != OP_RUNNING_POLL) {
178 mutex_unlock(&edac_pci_ctls_mutex);
182 if (edac_pci_get_check_errors())
183 pci->edac_check(pci);
185 /* if we are on a one second period, then use round */
186 msec = edac_pci_get_poll_msec();
188 delay = round_jiffies_relative(msecs_to_jiffies(msec));
190 delay = msecs_to_jiffies(msec);
192 edac_queue_work(&pci->work, delay);
194 mutex_unlock(&edac_pci_ctls_mutex);
197 int edac_pci_alloc_index(void)
199 return atomic_inc_return(&pci_indexes) - 1;
201 EXPORT_SYMBOL_GPL(edac_pci_alloc_index);
203 int edac_pci_add_device(struct edac_pci_ctl_info *pci, int edac_idx)
207 pci->pci_idx = edac_idx;
208 pci->start_time = jiffies;
210 mutex_lock(&edac_pci_ctls_mutex);
212 if (add_edac_pci_to_global_list(pci))
215 if (edac_pci_create_sysfs(pci)) {
216 edac_pci_printk(pci, KERN_WARNING,
217 "failed to create sysfs pci\n");
221 if (pci->edac_check) {
222 pci->op_state = OP_RUNNING_POLL;
224 INIT_DELAYED_WORK(&pci->work, edac_pci_workq_function);
225 edac_queue_work(&pci->work, msecs_to_jiffies(edac_pci_get_poll_msec()));
228 pci->op_state = OP_RUNNING_INTERRUPT;
231 edac_pci_printk(pci, KERN_INFO,
232 "Giving out device to module %s controller %s: DEV %s (%s)\n",
233 pci->mod_name, pci->ctl_name, pci->dev_name,
234 edac_op_state_to_string(pci->op_state));
236 mutex_unlock(&edac_pci_ctls_mutex);
239 /* error unwind stack */
241 del_edac_pci_from_global_list(pci);
243 mutex_unlock(&edac_pci_ctls_mutex);
246 EXPORT_SYMBOL_GPL(edac_pci_add_device);
248 struct edac_pci_ctl_info *edac_pci_del_device(struct device *dev)
250 struct edac_pci_ctl_info *pci;
254 mutex_lock(&edac_pci_ctls_mutex);
256 /* ensure the control struct is on the global list
259 pci = find_edac_pci_by_dev(dev);
261 mutex_unlock(&edac_pci_ctls_mutex);
265 pci->op_state = OP_OFFLINE;
267 del_edac_pci_from_global_list(pci);
269 mutex_unlock(&edac_pci_ctls_mutex);
272 edac_stop_work(&pci->work);
274 edac_printk(KERN_INFO, EDAC_PCI,
275 "Removed device %d for %s %s: DEV %s\n",
276 pci->pci_idx, pci->mod_name, pci->ctl_name, edac_dev_name(pci));
280 EXPORT_SYMBOL_GPL(edac_pci_del_device);
283 * edac_pci_generic_check
285 * a Generic parity check API
287 static void edac_pci_generic_check(struct edac_pci_ctl_info *pci)
290 edac_pci_do_parity_check();
293 /* free running instance index counter */
294 static int edac_pci_idx;
295 #define EDAC_PCI_GENCTL_NAME "EDAC PCI controller"
297 struct edac_pci_gen_data {
301 struct edac_pci_ctl_info *edac_pci_create_generic_ctl(struct device *dev,
302 const char *mod_name)
304 struct edac_pci_ctl_info *pci;
305 struct edac_pci_gen_data *pdata;
307 pci = edac_pci_alloc_ctl_info(sizeof(*pdata), EDAC_PCI_GENCTL_NAME);
311 pdata = pci->pvt_info;
313 dev_set_drvdata(pci->dev, pci);
314 pci->dev_name = pci_name(to_pci_dev(dev));
316 pci->mod_name = mod_name;
317 pci->ctl_name = EDAC_PCI_GENCTL_NAME;
318 if (edac_op_state == EDAC_OPSTATE_POLL)
319 pci->edac_check = edac_pci_generic_check;
321 pdata->edac_idx = edac_pci_idx++;
323 if (edac_pci_add_device(pci, pdata->edac_idx) > 0) {
324 edac_dbg(3, "failed edac_pci_add_device()\n");
325 edac_pci_free_ctl_info(pci);
331 EXPORT_SYMBOL_GPL(edac_pci_create_generic_ctl);
333 void edac_pci_release_generic_ctl(struct edac_pci_ctl_info *pci)
335 edac_dbg(0, "pci mod=%s\n", pci->mod_name);
337 edac_pci_del_device(pci->dev);
338 edac_pci_free_ctl_info(pci);
340 EXPORT_SYMBOL_GPL(edac_pci_release_generic_ctl);