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(unsigned int sz_pvt,
50 const char *edac_pci_name)
52 struct edac_pci_ctl_info *pci;
56 pci = (struct edac_pci_ctl_info *)0;
57 pvt = edac_align_ptr(&pci[1], sz_pvt);
58 size = ((unsigned long)pvt) + sz_pvt;
60 if ((pci = kzalloc(size, GFP_KERNEL)) == NULL)
63 pvt = sz_pvt ? ((char *)pci) + ((unsigned long)pvt) : NULL;
67 pci->op_state = OP_ALLOC;
69 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)
85 EXPORT_SYMBOL_GPL(edac_pci_free_ctl_info);
88 * find_edac_pci_by_dev()
89 * scans the edac_pci list for a specific 'struct device *'
91 static struct edac_pci_ctl_info *find_edac_pci_by_dev(struct device *dev)
93 struct edac_pci_ctl_info *pci;
94 struct list_head *item;
96 debugf3("%s()\n", __func__);
98 list_for_each(item, &edac_pci_list) {
99 pci = list_entry(item, struct edac_pci_ctl_info, link);
109 * add_edac_pci_to_global_list
110 * Before calling this function, caller must assign a unique value to
116 static int add_edac_pci_to_global_list(struct edac_pci_ctl_info *pci)
118 struct list_head *item, *insert_before;
119 struct edac_pci_ctl_info *rover;
121 insert_before = &edac_pci_list;
123 /* Determine if already on the list */
124 if (unlikely((rover = find_edac_pci_by_dev(pci->dev)) != NULL))
127 /* Insert in ascending order by 'pci_idx', so find position */
128 list_for_each(item, &edac_pci_list) {
129 rover = list_entry(item, struct edac_pci_ctl_info, link);
131 if (rover->pci_idx >= pci->pci_idx) {
132 if (unlikely(rover->pci_idx == pci->pci_idx))
135 insert_before = item;
140 list_add_tail_rcu(&pci->link, insert_before);
144 edac_printk(KERN_WARNING, EDAC_PCI,
145 "%s (%s) %s %s already assigned %d\n",
146 rover->dev->bus_id, dev_name(rover),
147 rover->mod_name, rover->ctl_name, rover->pci_idx);
151 edac_printk(KERN_WARNING, EDAC_PCI,
152 "but in low-level driver: attempt to assign\n"
153 "\tduplicate pci_idx %d in %s()\n", rover->pci_idx,
159 * complete_edac_pci_list_del
161 static void complete_edac_pci_list_del(struct rcu_head *head)
163 struct edac_pci_ctl_info *pci;
165 pci = container_of(head, struct edac_pci_ctl_info, rcu);
166 INIT_LIST_HEAD(&pci->link);
167 complete(&pci->complete);
171 * del_edac_pci_from_global_list
173 static void del_edac_pci_from_global_list(struct edac_pci_ctl_info *pci)
175 list_del_rcu(&pci->link);
176 init_completion(&pci->complete);
177 call_rcu(&pci->rcu, complete_edac_pci_list_del);
178 wait_for_completion(&pci->complete);
183 * Search for an edac_pci_ctl_info structure whose index is 'idx'
185 * If found, return a pointer to the structure
188 * Caller must hold pci_ctls_mutex.
190 struct edac_pci_ctl_info *edac_pci_find(int idx)
192 struct list_head *item;
193 struct edac_pci_ctl_info *pci;
195 /* Iterage over list, looking for exact match of ID */
196 list_for_each(item, &edac_pci_list) {
197 pci = list_entry(item, struct edac_pci_ctl_info, link);
199 if (pci->pci_idx >= idx) {
200 if (pci->pci_idx == idx)
203 /* not on list, so terminate early */
211 EXPORT_SYMBOL_GPL(edac_pci_find);
214 * edac_pci_workq_function()
215 * performs the operation scheduled by a workq request
217 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
218 static void edac_pci_workq_function(struct work_struct *work_req)
220 struct delayed_work *d_work = (struct delayed_work *)work_req;
221 struct edac_pci_ctl_info *pci = to_edac_pci_ctl_work(d_work);
223 static void edac_pci_workq_function(void *ptr)
225 struct edac_pci_ctl_info *pci = ptr;
228 edac_lock_pci_list();
230 if ((pci->op_state == OP_RUNNING_POLL) &&
231 (pci->edac_check != NULL) && (edac_pci_get_check_errors()))
232 pci->edac_check(pci);
234 edac_unlock_pci_list();
237 queue_delayed_work(edac_workqueue, &pci->work,
238 msecs_to_jiffies(edac_pci_get_poll_msec()));
242 * edac_pci_workq_setup()
243 * initialize a workq item for this edac_pci instance
244 * passing in the new delay period in msec
246 static void edac_pci_workq_setup(struct edac_pci_ctl_info *pci,
249 debugf0("%s()\n", __func__);
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,
257 msecs_to_jiffies(edac_pci_get_poll_msec()));
261 * edac_pci_workq_teardown()
262 * stop the workq processing on this edac_pci instance
264 static void edac_pci_workq_teardown(struct edac_pci_ctl_info *pci)
268 status = cancel_delayed_work(&pci->work);
270 flush_workqueue(edac_workqueue);
274 * edac_pci_reset_delay_period
276 void edac_pci_reset_delay_period(struct edac_pci_ctl_info *pci,
279 edac_lock_pci_list();
281 edac_pci_workq_teardown(pci);
283 edac_pci_workq_setup(pci, value);
285 edac_unlock_pci_list();
288 EXPORT_SYMBOL_GPL(edac_pci_reset_delay_period);
291 * edac_pci_add_device: Insert the 'edac_dev' structure into the
292 * edac_pci global list and create sysfs entries associated with
293 * edac_pci structure.
294 * @pci: pointer to the edac_device structure to be added to the list
295 * @edac_idx: A unique numeric identifier to be assigned to the
296 * 'edac_pci' structure.
302 int edac_pci_add_device(struct edac_pci_ctl_info *pci, int edac_idx)
304 debugf0("%s()\n", __func__);
306 pci->pci_idx = edac_idx;
308 edac_lock_pci_list();
310 if (add_edac_pci_to_global_list(pci))
313 pci->start_time = jiffies;
315 if (edac_pci_create_sysfs(pci)) {
316 edac_pci_printk(pci, KERN_WARNING,
317 "failed to create sysfs pci\n");
321 if (pci->edac_check != NULL) {
322 pci->op_state = OP_RUNNING_POLL;
324 edac_pci_workq_setup(pci, 1000);
326 pci->op_state = OP_RUNNING_INTERRUPT;
329 edac_pci_printk(pci, KERN_INFO,
330 "Giving out device to module '%s' controller '%s':"
334 dev_name(pci), edac_op_state_toString(pci->op_state));
336 edac_unlock_pci_list();
340 del_edac_pci_from_global_list(pci);
342 edac_unlock_pci_list();
346 EXPORT_SYMBOL_GPL(edac_pci_add_device);
349 * edac_pci_del_device()
350 * Remove sysfs entries for specified edac_pci structure and
351 * then remove edac_pci structure from global list
354 * Pointer to 'struct device' representing edac_pci structure
358 * Pointer to removed edac_pci structure,
359 * or NULL if device not found
361 struct edac_pci_ctl_info *edac_pci_del_device(struct device *dev)
363 struct edac_pci_ctl_info *pci;
365 debugf0("%s()\n", __func__);
367 edac_lock_pci_list();
369 if ((pci = find_edac_pci_by_dev(dev)) == NULL) {
370 edac_unlock_pci_list();
374 pci->op_state = OP_OFFLINE;
376 edac_pci_workq_teardown(pci);
378 edac_pci_remove_sysfs(pci);
380 del_edac_pci_from_global_list(pci);
382 edac_unlock_pci_list();
384 edac_printk(KERN_INFO, EDAC_PCI,
385 "Removed device %d for %s %s: DEV %s\n",
386 pci->pci_idx, pci->mod_name, pci->ctl_name, dev_name(pci));
391 EXPORT_SYMBOL_GPL(edac_pci_del_device);
393 void edac_pci_generic_check(struct edac_pci_ctl_info *pci)
395 edac_pci_do_parity_check();
398 static int edac_pci_idx = 0;
399 #define EDAC_PCI_GENCTL_NAME "EDAC PCI controller"
401 struct edac_pci_gen_data {
405 struct edac_pci_ctl_info *edac_pci_create_generic_ctl(struct device *dev,
406 const char *mod_name)
408 struct edac_pci_ctl_info *pci;
409 struct edac_pci_gen_data *pdata;
411 pci = edac_pci_alloc_ctl_info(sizeof(*pdata), EDAC_PCI_GENCTL_NAME);
415 pdata = pci->pvt_info;
417 dev_set_drvdata(pci->dev, pci);
418 pci->dev_name = pci_name(to_pci_dev(dev));
420 pci->mod_name = mod_name;
421 pci->ctl_name = EDAC_PCI_GENCTL_NAME;
422 pci->edac_check = edac_pci_generic_check;
424 pdata->edac_idx = edac_pci_idx++;
426 if (edac_pci_add_device(pci, pdata->edac_idx) > 0) {
427 debugf3("%s(): failed edac_pci_add_device()\n", __func__);
428 edac_pci_free_ctl_info(pci);
435 EXPORT_SYMBOL_GPL(edac_pci_create_generic_ctl);
437 void edac_pci_release_generic_ctl(struct edac_pci_ctl_info *pci)
439 edac_pci_del_device(pci->dev);
440 edac_pci_free_ctl_info(pci);
443 EXPORT_SYMBOL_GPL(edac_pci_release_generic_ctl);