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 static void edac_pci_workq_function(struct work_struct *work_req)
219 struct delayed_work *d_work = (struct delayed_work *)work_req;
220 struct edac_pci_ctl_info *pci = to_edac_pci_ctl_work(d_work);
222 edac_lock_pci_list();
224 if ((pci->op_state == OP_RUNNING_POLL) &&
225 (pci->edac_check != NULL) && (edac_pci_get_check_errors()))
226 pci->edac_check(pci);
228 edac_unlock_pci_list();
231 queue_delayed_work(edac_workqueue, &pci->work,
232 msecs_to_jiffies(edac_pci_get_poll_msec()));
236 * edac_pci_workq_setup()
237 * initialize a workq item for this edac_pci instance
238 * passing in the new delay period in msec
240 static void edac_pci_workq_setup(struct edac_pci_ctl_info *pci,
243 debugf0("%s()\n", __func__);
245 INIT_DELAYED_WORK(&pci->work, edac_pci_workq_function);
246 queue_delayed_work(edac_workqueue, &pci->work,
247 msecs_to_jiffies(edac_pci_get_poll_msec()));
251 * edac_pci_workq_teardown()
252 * stop the workq processing on this edac_pci instance
254 static void edac_pci_workq_teardown(struct edac_pci_ctl_info *pci)
258 status = cancel_delayed_work(&pci->work);
260 flush_workqueue(edac_workqueue);
264 * edac_pci_reset_delay_period
266 void edac_pci_reset_delay_period(struct edac_pci_ctl_info *pci,
269 edac_lock_pci_list();
271 edac_pci_workq_teardown(pci);
273 edac_pci_workq_setup(pci, value);
275 edac_unlock_pci_list();
278 EXPORT_SYMBOL_GPL(edac_pci_reset_delay_period);
281 * edac_pci_add_device: Insert the 'edac_dev' structure into the
282 * edac_pci global list and create sysfs entries associated with
283 * edac_pci structure.
284 * @pci: pointer to the edac_device structure to be added to the list
285 * @edac_idx: A unique numeric identifier to be assigned to the
286 * 'edac_pci' structure.
292 int edac_pci_add_device(struct edac_pci_ctl_info *pci, int edac_idx)
294 debugf0("%s()\n", __func__);
296 pci->pci_idx = edac_idx;
298 edac_lock_pci_list();
300 if (add_edac_pci_to_global_list(pci))
303 pci->start_time = jiffies;
305 if (edac_pci_create_sysfs(pci)) {
306 edac_pci_printk(pci, KERN_WARNING,
307 "failed to create sysfs pci\n");
311 if (pci->edac_check != NULL) {
312 pci->op_state = OP_RUNNING_POLL;
314 edac_pci_workq_setup(pci, 1000);
316 pci->op_state = OP_RUNNING_INTERRUPT;
319 edac_pci_printk(pci, KERN_INFO,
320 "Giving out device to module '%s' controller '%s':"
324 dev_name(pci), edac_op_state_toString(pci->op_state));
326 edac_unlock_pci_list();
330 del_edac_pci_from_global_list(pci);
332 edac_unlock_pci_list();
336 EXPORT_SYMBOL_GPL(edac_pci_add_device);
339 * edac_pci_del_device()
340 * Remove sysfs entries for specified edac_pci structure and
341 * then remove edac_pci structure from global list
344 * Pointer to 'struct device' representing edac_pci structure
348 * Pointer to removed edac_pci structure,
349 * or NULL if device not found
351 struct edac_pci_ctl_info *edac_pci_del_device(struct device *dev)
353 struct edac_pci_ctl_info *pci;
355 debugf0("%s()\n", __func__);
357 edac_lock_pci_list();
359 if ((pci = find_edac_pci_by_dev(dev)) == NULL) {
360 edac_unlock_pci_list();
364 pci->op_state = OP_OFFLINE;
366 edac_pci_workq_teardown(pci);
368 edac_pci_remove_sysfs(pci);
370 del_edac_pci_from_global_list(pci);
372 edac_unlock_pci_list();
374 edac_printk(KERN_INFO, EDAC_PCI,
375 "Removed device %d for %s %s: DEV %s\n",
376 pci->pci_idx, pci->mod_name, pci->ctl_name, dev_name(pci));
381 EXPORT_SYMBOL_GPL(edac_pci_del_device);
383 void edac_pci_generic_check(struct edac_pci_ctl_info *pci)
385 edac_pci_do_parity_check();
388 static int edac_pci_idx;
389 #define EDAC_PCI_GENCTL_NAME "EDAC PCI controller"
391 struct edac_pci_gen_data {
395 struct edac_pci_ctl_info *edac_pci_create_generic_ctl(struct device *dev,
396 const char *mod_name)
398 struct edac_pci_ctl_info *pci;
399 struct edac_pci_gen_data *pdata;
401 pci = edac_pci_alloc_ctl_info(sizeof(*pdata), EDAC_PCI_GENCTL_NAME);
405 pdata = pci->pvt_info;
407 dev_set_drvdata(pci->dev, pci);
408 pci->dev_name = pci_name(to_pci_dev(dev));
410 pci->mod_name = mod_name;
411 pci->ctl_name = EDAC_PCI_GENCTL_NAME;
412 pci->edac_check = edac_pci_generic_check;
414 pdata->edac_idx = edac_pci_idx++;
416 if (edac_pci_add_device(pci, pdata->edac_idx) > 0) {
417 debugf3("%s(): failed edac_pci_add_device()\n", __func__);
418 edac_pci_free_ctl_info(pci);
425 EXPORT_SYMBOL_GPL(edac_pci_create_generic_ctl);
427 void edac_pci_release_generic_ctl(struct edac_pci_ctl_info *pci)
429 edac_pci_del_device(pci->dev);
430 edac_pci_free_ctl_info(pci);
433 EXPORT_SYMBOL_GPL(edac_pci_release_generic_ctl);