drivers/edac: core Lindent cleanup
[pandora-kernel.git] / drivers / edac / edac_pci.c
1 /*
2  * EDAC PCI component
3  *
4  * Author: Dave Jiang <djiang@mvista.com>
5  *
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
9  * or implied.
10  *
11  */
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>
26 #include <asm/page.h>
27
28 #include "edac_core.h"
29 #include "edac_module.h"
30
31 static DEFINE_MUTEX(edac_pci_ctls_mutex);
32 static struct list_head edac_pci_list = LIST_HEAD_INIT(edac_pci_list);
33
34 static inline void edac_lock_pci_list(void)
35 {
36         mutex_lock(&edac_pci_ctls_mutex);
37 }
38
39 static inline void edac_unlock_pci_list(void)
40 {
41         mutex_unlock(&edac_pci_ctls_mutex);
42 }
43
44 /*
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.
48  */
49 struct edac_pci_ctl_info *edac_pci_alloc_ctl_info(unsigned int sz_pvt,
50                                                   const char *edac_pci_name)
51 {
52         struct edac_pci_ctl_info *pci;
53         void *pvt;
54         unsigned int size;
55
56         pci = (struct edac_pci_ctl_info *)0;
57         pvt = edac_align_ptr(&pci[1], sz_pvt);
58         size = ((unsigned long)pvt) + sz_pvt;
59
60         if ((pci = kzalloc(size, GFP_KERNEL)) == NULL)
61                 return NULL;
62
63         pvt = sz_pvt ? ((char *)pci) + ((unsigned long)pvt) : NULL;
64
65         pci->pvt_info = pvt;
66
67         pci->op_state = OP_ALLOC;
68
69         snprintf(pci->name, strlen(edac_pci_name) + 1, "%s", edac_pci_name);
70
71         return pci;
72 }
73
74 EXPORT_SYMBOL_GPL(edac_pci_alloc_ctl_info);
75
76 /*
77  * edac_pci_free_ctl_info()
78  *      frees the memory allocated by edac_pci_alloc_ctl_info() function
79  */
80 void edac_pci_free_ctl_info(struct edac_pci_ctl_info *pci)
81 {
82         kfree(pci);
83 }
84
85 EXPORT_SYMBOL_GPL(edac_pci_free_ctl_info);
86
87 /*
88  * find_edac_pci_by_dev()
89  *      scans the edac_pci list for a specific 'struct device *'
90  */
91 static struct edac_pci_ctl_info *find_edac_pci_by_dev(struct device *dev)
92 {
93         struct edac_pci_ctl_info *pci;
94         struct list_head *item;
95
96         debugf3("%s()\n", __func__);
97
98         list_for_each(item, &edac_pci_list) {
99                 pci = list_entry(item, struct edac_pci_ctl_info, link);
100
101                 if (pci->dev == dev)
102                         return pci;
103         }
104
105         return NULL;
106 }
107
108 /*
109  * add_edac_pci_to_global_list
110  *      Before calling this function, caller must assign a unique value to
111  *      edac_dev->pci_idx.
112  *      Return:
113  *              0 on success
114  *              1 on failure
115  */
116 static int add_edac_pci_to_global_list(struct edac_pci_ctl_info *pci)
117 {
118         struct list_head *item, *insert_before;
119         struct edac_pci_ctl_info *rover;
120
121         insert_before = &edac_pci_list;
122
123         /* Determine if already on the list */
124         if (unlikely((rover = find_edac_pci_by_dev(pci->dev)) != NULL))
125                 goto fail0;
126
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);
130
131                 if (rover->pci_idx >= pci->pci_idx) {
132                         if (unlikely(rover->pci_idx == pci->pci_idx))
133                                 goto fail1;
134
135                         insert_before = item;
136                         break;
137                 }
138         }
139
140         list_add_tail_rcu(&pci->link, insert_before);
141         return 0;
142
143       fail0:
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);
148         return 1;
149
150       fail1:
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,
154                     __func__);
155         return 1;
156 }
157
158 /*
159  * complete_edac_pci_list_del
160  */
161 static void complete_edac_pci_list_del(struct rcu_head *head)
162 {
163         struct edac_pci_ctl_info *pci;
164
165         pci = container_of(head, struct edac_pci_ctl_info, rcu);
166         INIT_LIST_HEAD(&pci->link);
167         complete(&pci->complete);
168 }
169
170 /*
171  * del_edac_pci_from_global_list
172  */
173 static void del_edac_pci_from_global_list(struct edac_pci_ctl_info *pci)
174 {
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);
179 }
180
181 /*
182  * edac_pci_find()
183  *      Search for an edac_pci_ctl_info structure whose index is 'idx'
184  *
185  * If found, return a pointer to the structure
186  * Else return NULL.
187  *
188  * Caller must hold pci_ctls_mutex.
189  */
190 struct edac_pci_ctl_info *edac_pci_find(int idx)
191 {
192         struct list_head *item;
193         struct edac_pci_ctl_info *pci;
194
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);
198
199                 if (pci->pci_idx >= idx) {
200                         if (pci->pci_idx == idx)
201                                 return pci;
202
203                         /* not on list, so terminate early */
204                         break;
205                 }
206         }
207
208         return NULL;
209 }
210
211 EXPORT_SYMBOL_GPL(edac_pci_find);
212
213 /*
214  * edac_pci_workq_function()
215  *      performs the operation scheduled by a workq request
216  */
217 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
218 static void edac_pci_workq_function(struct work_struct *work_req)
219 {
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);
222 #else
223 static void edac_pci_workq_function(void *ptr)
224 {
225         struct edac_pci_ctl_info *pci = ptr;
226 #endif
227
228         edac_lock_pci_list();
229
230         if ((pci->op_state == OP_RUNNING_POLL) &&
231             (pci->edac_check != NULL) && (edac_pci_get_check_errors()))
232                 pci->edac_check(pci);
233
234         edac_unlock_pci_list();
235
236         /* Reschedule */
237         queue_delayed_work(edac_workqueue, &pci->work,
238                            msecs_to_jiffies(edac_pci_get_poll_msec()));
239 }
240
241 /*
242  * edac_pci_workq_setup()
243  *      initialize a workq item for this edac_pci instance
244  *      passing in the new delay period in msec
245  */
246 static void edac_pci_workq_setup(struct edac_pci_ctl_info *pci,
247                                  unsigned int msec)
248 {
249         debugf0("%s()\n", __func__);
250
251 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
252         INIT_DELAYED_WORK(&pci->work, edac_pci_workq_function);
253 #else
254         INIT_WORK(&pci->work, edac_pci_workq_function, pci);
255 #endif
256         queue_delayed_work(edac_workqueue, &pci->work,
257                            msecs_to_jiffies(edac_pci_get_poll_msec()));
258 }
259
260 /*
261  * edac_pci_workq_teardown()
262  *      stop the workq processing on this edac_pci instance
263  */
264 static void edac_pci_workq_teardown(struct edac_pci_ctl_info *pci)
265 {
266         int status;
267
268         status = cancel_delayed_work(&pci->work);
269         if (status == 0)
270                 flush_workqueue(edac_workqueue);
271 }
272
273 /*
274  * edac_pci_reset_delay_period
275  */
276 void edac_pci_reset_delay_period(struct edac_pci_ctl_info *pci,
277                                  unsigned long value)
278 {
279         edac_lock_pci_list();
280
281         edac_pci_workq_teardown(pci);
282
283         edac_pci_workq_setup(pci, value);
284
285         edac_unlock_pci_list();
286 }
287
288 EXPORT_SYMBOL_GPL(edac_pci_reset_delay_period);
289
290 /*
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.
297  *
298  * Return:
299  *      0       Success
300  *      !0      Failure
301  */
302 int edac_pci_add_device(struct edac_pci_ctl_info *pci, int edac_idx)
303 {
304         debugf0("%s()\n", __func__);
305
306         pci->pci_idx = edac_idx;
307
308         edac_lock_pci_list();
309
310         if (add_edac_pci_to_global_list(pci))
311                 goto fail0;
312
313         pci->start_time = jiffies;
314
315         if (edac_pci_create_sysfs(pci)) {
316                 edac_pci_printk(pci, KERN_WARNING,
317                                 "failed to create sysfs pci\n");
318                 goto fail1;
319         }
320
321         if (pci->edac_check != NULL) {
322                 pci->op_state = OP_RUNNING_POLL;
323
324                 edac_pci_workq_setup(pci, 1000);
325         } else {
326                 pci->op_state = OP_RUNNING_INTERRUPT;
327         }
328
329         edac_pci_printk(pci, KERN_INFO,
330                         "Giving out device to module '%s' controller '%s':"
331                         " DEV '%s' (%s)\n",
332                         pci->mod_name,
333                         pci->ctl_name,
334                         dev_name(pci), edac_op_state_toString(pci->op_state));
335
336         edac_unlock_pci_list();
337         return 0;
338
339       fail1:
340         del_edac_pci_from_global_list(pci);
341       fail0:
342         edac_unlock_pci_list();
343         return 1;
344 }
345
346 EXPORT_SYMBOL_GPL(edac_pci_add_device);
347
348 /*
349  * edac_pci_del_device()
350  *      Remove sysfs entries for specified edac_pci structure and
351  *      then remove edac_pci structure from global list
352  *
353  * @dev:
354  *      Pointer to 'struct device' representing edac_pci structure
355  *      to remove
356  *
357  * Return:
358  *      Pointer to removed edac_pci structure,
359  *      or NULL if device not found
360  */
361 struct edac_pci_ctl_info *edac_pci_del_device(struct device *dev)
362 {
363         struct edac_pci_ctl_info *pci;
364
365         debugf0("%s()\n", __func__);
366
367         edac_lock_pci_list();
368
369         if ((pci = find_edac_pci_by_dev(dev)) == NULL) {
370                 edac_unlock_pci_list();
371                 return NULL;
372         }
373
374         pci->op_state = OP_OFFLINE;
375
376         edac_pci_workq_teardown(pci);
377
378         edac_pci_remove_sysfs(pci);
379
380         del_edac_pci_from_global_list(pci);
381
382         edac_unlock_pci_list();
383
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));
387
388         return pci;
389 }
390
391 EXPORT_SYMBOL_GPL(edac_pci_del_device);
392
393 void edac_pci_generic_check(struct edac_pci_ctl_info *pci)
394 {
395         edac_pci_do_parity_check();
396 }
397
398 static int edac_pci_idx = 0;
399 #define EDAC_PCI_GENCTL_NAME    "EDAC PCI controller"
400
401 struct edac_pci_gen_data {
402         int edac_idx;
403 };
404
405 struct edac_pci_ctl_info *edac_pci_create_generic_ctl(struct device *dev,
406                                                       const char *mod_name)
407 {
408         struct edac_pci_ctl_info *pci;
409         struct edac_pci_gen_data *pdata;
410
411         pci = edac_pci_alloc_ctl_info(sizeof(*pdata), EDAC_PCI_GENCTL_NAME);
412         if (!pci)
413                 return NULL;
414
415         pdata = pci->pvt_info;
416         pci->dev = dev;
417         dev_set_drvdata(pci->dev, pci);
418         pci->dev_name = pci_name(to_pci_dev(dev));
419
420         pci->mod_name = mod_name;
421         pci->ctl_name = EDAC_PCI_GENCTL_NAME;
422         pci->edac_check = edac_pci_generic_check;
423
424         pdata->edac_idx = edac_pci_idx++;
425
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);
429                 return NULL;
430         }
431
432         return pci;
433 }
434
435 EXPORT_SYMBOL_GPL(edac_pci_create_generic_ctl);
436
437 void edac_pci_release_generic_ctl(struct edac_pci_ctl_info *pci)
438 {
439         edac_pci_del_device(pci->dev);
440         edac_pci_free_ctl_info(pci);
441 }
442
443 EXPORT_SYMBOL_GPL(edac_pci_release_generic_ctl);