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 (edac_pci_get_check_errors()))
230 pci
->edac_check(pci
);
232 edac_unlock_pci_list();
235 queue_delayed_work(edac_workqueue
, &pci
->work
,
236 msecs_to_jiffies(edac_pci_get_poll_msec()));
240 * edac_pci_workq_setup()
241 * initialize a workq item for this edac_pci instance
242 * passing in the new delay period in msec
244 static void edac_pci_workq_setup(struct edac_pci_ctl_info
*pci
,
247 debugf0("%s()\n", __func__
);
249 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
250 INIT_DELAYED_WORK(&pci
->work
, edac_pci_workq_function
);
252 INIT_WORK(&pci
->work
, edac_pci_workq_function
, pci
);
254 queue_delayed_work(edac_workqueue
, &pci
->work
,
255 msecs_to_jiffies(edac_pci_get_poll_msec()));
259 * edac_pci_workq_teardown()
260 * stop the workq processing on this edac_pci instance
262 static void edac_pci_workq_teardown(struct edac_pci_ctl_info
*pci
)
266 status
= cancel_delayed_work(&pci
->work
);
268 flush_workqueue(edac_workqueue
);
272 * edac_pci_reset_delay_period
274 void edac_pci_reset_delay_period(struct edac_pci_ctl_info
*pci
,
277 edac_lock_pci_list();
279 edac_pci_workq_teardown(pci
);
281 edac_pci_workq_setup(pci
, value
);
283 edac_unlock_pci_list();
285 EXPORT_SYMBOL_GPL(edac_pci_reset_delay_period
);
288 * edac_pci_add_device: Insert the 'edac_dev' structure into the
289 * edac_pci global list and create sysfs entries associated with
290 * edac_pci structure.
291 * @pci: pointer to the edac_device structure to be added to the list
292 * @edac_idx: A unique numeric identifier to be assigned to the
293 * 'edac_pci' structure.
299 int edac_pci_add_device(struct edac_pci_ctl_info
*pci
, int edac_idx
)
301 debugf0("%s()\n", __func__
);
303 pci
->pci_idx
= edac_idx
;
305 edac_lock_pci_list();
307 if (add_edac_pci_to_global_list(pci
))
310 pci
->start_time
= jiffies
;
312 if (edac_pci_create_sysfs(pci
)) {
313 edac_pci_printk(pci
, KERN_WARNING
,
314 "failed to create sysfs pci\n");
318 if (pci
->edac_check
!= NULL
) {
319 pci
->op_state
= OP_RUNNING_POLL
;
321 edac_pci_workq_setup(pci
, 1000);
323 pci
->op_state
= OP_RUNNING_INTERRUPT
;
326 edac_pci_printk(pci
, KERN_INFO
,
327 "Giving out device to module '%s' controller '%s':"
332 edac_op_state_toString(pci
->op_state
));
334 edac_unlock_pci_list();
338 del_edac_pci_from_global_list(pci
);
340 edac_unlock_pci_list();
343 EXPORT_SYMBOL_GPL(edac_pci_add_device
);
346 * edac_pci_del_device()
347 * Remove sysfs entries for specified edac_pci structure and
348 * then remove edac_pci structure from global list
351 * Pointer to 'struct device' representing edac_pci structure
355 * Pointer to removed edac_pci structure,
356 * or NULL if device not found
358 struct edac_pci_ctl_info
* edac_pci_del_device(struct device
*dev
)
360 struct edac_pci_ctl_info
*pci
;
362 debugf0("%s()\n", __func__
);
364 edac_lock_pci_list();
366 if ((pci
= find_edac_pci_by_dev(dev
)) == NULL
) {
367 edac_unlock_pci_list();
371 pci
->op_state
= OP_OFFLINE
;
373 edac_pci_workq_teardown(pci
);
375 edac_pci_remove_sysfs(pci
);
377 del_edac_pci_from_global_list(pci
);
379 edac_unlock_pci_list();
381 edac_printk(KERN_INFO
, EDAC_PCI
,
382 "Removed device %d for %s %s: DEV %s\n",
390 EXPORT_SYMBOL_GPL(edac_pci_del_device
);
392 void edac_pci_generic_check(struct edac_pci_ctl_info
*pci
)
394 edac_pci_do_parity_check();
397 static int edac_pci_idx
= 0;
398 #define EDAC_PCI_GENCTL_NAME "EDAC PCI controller"
400 struct edac_pci_gen_data
{
404 struct edac_pci_ctl_info
*
405 edac_pci_create_generic_ctl(struct device
*dev
, const char *mod_name
)
407 struct edac_pci_ctl_info
*pci
;
408 struct edac_pci_gen_data
*pdata
;
410 pci
= edac_pci_alloc_ctl_info(sizeof(*pdata
), EDAC_PCI_GENCTL_NAME
);
414 pdata
= pci
->pvt_info
;
416 dev_set_drvdata(pci
->dev
, pci
);
417 pci
->dev_name
= pci_name(to_pci_dev(dev
));
419 pci
->mod_name
= mod_name
;
420 pci
->ctl_name
= EDAC_PCI_GENCTL_NAME
;
421 pci
->edac_check
= edac_pci_generic_check
;
423 pdata
->edac_idx
= edac_pci_idx
++;
425 if (edac_pci_add_device(pci
, pdata
->edac_idx
) > 0) {
426 debugf3("%s(): failed edac_pci_add_device()\n", __func__
);
427 edac_pci_free_ctl_info(pci
);
433 EXPORT_SYMBOL_GPL(edac_pci_create_generic_ctl
);
435 void edac_pci_release_generic_ctl(struct edac_pci_ctl_info
*pci
)
437 edac_pci_del_device(pci
->dev
);
438 edac_pci_free_ctl_info(pci
);
440 EXPORT_SYMBOL_GPL(edac_pci_release_generic_ctl
);