drivers/edac: updated PCI monitoring
[powerpc.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(
50         unsigned int sz_pvt,
51         const char *edac_pci_name)
52 {
53         struct edac_pci_ctl_info *pci;
54         void *pvt;
55         unsigned int size;
56
57         pci = (struct edac_pci_ctl_info *)0;
58         pvt = edac_align_ptr(&pci[1], sz_pvt);
59         size = ((unsigned long)pvt) + sz_pvt;
60
61         if ((pci = kzalloc(size, GFP_KERNEL)) == NULL)
62                 return NULL;
63
64         pvt = sz_pvt ? ((char *)pci) + ((unsigned long)pvt) : NULL;
65
66         pci->pvt_info = pvt;
67
68         pci->op_state = OP_ALLOC;
69
70         snprintf(pci->name, strlen(edac_pci_name)+1, "%s", edac_pci_name);
71
72         return pci;
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 EXPORT_SYMBOL_GPL(edac_pci_free_ctl_info);
85
86 /*
87  * find_edac_pci_by_dev()
88  *      scans the edac_pci list for a specific 'struct device *'
89  */
90 static struct edac_pci_ctl_info * find_edac_pci_by_dev(struct device *dev)
91 {
92         struct edac_pci_ctl_info *pci;
93         struct list_head *item;
94
95         debugf3("%s()\n", __func__);
96
97         list_for_each(item, &edac_pci_list) {
98                 pci = list_entry(item, struct edac_pci_ctl_info, link);
99
100                 if (pci->dev == dev)
101                         return pci;
102         }
103
104         return NULL;
105 }
106
107 /*
108  * add_edac_pci_to_global_list
109  *      Before calling this function, caller must assign a unique value to
110  *      edac_dev->pci_idx.
111  *      Return:
112  *              0 on success
113  *              1 on failure
114  */
115 static int add_edac_pci_to_global_list(struct edac_pci_ctl_info *pci)
116 {
117         struct list_head *item, *insert_before;
118         struct edac_pci_ctl_info *rover;
119
120         insert_before = &edac_pci_list;
121
122         /* Determine if already on the list */
123         if (unlikely((rover = find_edac_pci_by_dev(pci->dev)) != NULL))
124                 goto fail0;
125
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);
129
130                 if (rover->pci_idx >= pci->pci_idx) {
131                         if (unlikely(rover->pci_idx == pci->pci_idx))
132                                 goto fail1;
133
134                         insert_before = item;
135                         break;
136                 }
137         }
138
139         list_add_tail_rcu(&pci->link, insert_before);
140         return 0;
141
142 fail0:
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);
147         return 1;
148
149 fail1:
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__);
153         return 1;
154 }
155
156 /*
157  * complete_edac_pci_list_del
158  */
159 static void complete_edac_pci_list_del(struct rcu_head *head)
160 {
161         struct edac_pci_ctl_info *pci;
162
163         pci = container_of(head, struct edac_pci_ctl_info, rcu);
164         INIT_LIST_HEAD(&pci->link);
165         complete(&pci->complete);
166 }
167
168 /*
169  * del_edac_pci_from_global_list
170  */
171 static void del_edac_pci_from_global_list(struct edac_pci_ctl_info *pci)
172 {
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);
177 }
178
179 /*
180  * edac_pci_find()
181  *      Search for an edac_pci_ctl_info structure whose index is 'idx'
182  *
183  * If found, return a pointer to the structure
184  * Else return NULL.
185  *
186  * Caller must hold pci_ctls_mutex.
187  */
188 struct edac_pci_ctl_info * edac_pci_find(int idx)
189 {
190         struct list_head *item;
191         struct edac_pci_ctl_info *pci;
192
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);
196
197                 if (pci->pci_idx >= idx) {
198                         if (pci->pci_idx == idx)
199                                 return pci;
200
201                 /* not on list, so terminate early */
202                         break;
203                 }
204         }
205
206         return NULL;
207 }
208 EXPORT_SYMBOL_GPL(edac_pci_find);
209
210 /*
211  * edac_pci_workq_function()
212  *      performs the operation scheduled by a workq request
213  */
214 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
215 static void edac_pci_workq_function(struct work_struct *work_req)
216 {
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);
219 #else
220 static void edac_pci_workq_function(void *ptr)
221 {
222         struct edac_pci_ctl_info *pci = ptr;
223 #endif
224
225         edac_lock_pci_list();
226
227         if ((pci->op_state == OP_RUNNING_POLL) &&
228                 (pci->edac_check != NULL) &&
229                 (pci->check_parity_error))
230                 pci->edac_check(pci);
231
232         edac_unlock_pci_list();
233
234         /* Reschedule */
235         queue_delayed_work(edac_workqueue, &pci->work, pci->delay);
236 }
237
238 /*
239  * edac_pci_workq_setup()
240  *      initialize a workq item for this edac_pci instance
241  *      passing in the new delay period in msec
242  */
243 static void edac_pci_workq_setup(struct edac_pci_ctl_info *pci,
244                 unsigned int msec)
245 {
246         debugf0("%s()\n", __func__);
247
248         pci->poll_msec = msec;
249         edac_calc_delay(pci);
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, pci->delay);
257 }
258
259 /*
260  * edac_pci_workq_teardown()
261  *      stop the workq processing on this edac_pci instance
262  */
263 static void edac_pci_workq_teardown(struct edac_pci_ctl_info *pci)
264 {
265         int status;
266
267         status = cancel_delayed_work(&pci->work);
268         if (status == 0)
269                 flush_workqueue(edac_workqueue);
270 }
271
272 /*
273  * edac_pci_reset_delay_period
274  */
275 void edac_pci_reset_delay_period(struct edac_pci_ctl_info *pci,
276                 unsigned long value)
277 {
278         edac_lock_pci_list();
279
280         edac_pci_workq_teardown(pci);
281
282         edac_pci_workq_setup(pci, value);
283
284         edac_unlock_pci_list();
285 }
286 EXPORT_SYMBOL_GPL(edac_pci_reset_delay_period);
287
288 /*
289  * edac_pci_add_device: Insert the 'edac_dev' structure into the
290  * edac_pci global list and create sysfs entries associated with
291  * edac_pci structure.
292  * @pci: pointer to the edac_device structure to be added to the list
293  * @edac_idx: A unique numeric identifier to be assigned to the
294  * 'edac_pci' structure.
295  *
296  * Return:
297  *      0       Success
298  *      !0      Failure
299  */
300 int edac_pci_add_device(struct edac_pci_ctl_info *pci, int edac_idx)
301 {
302         debugf0("%s()\n", __func__);
303
304         pci->pci_idx = edac_idx;
305
306         edac_lock_pci_list();
307
308         if (add_edac_pci_to_global_list(pci))
309                 goto fail0;
310
311         pci->start_time = jiffies;
312
313         if (edac_pci_create_sysfs(pci)) {
314                 edac_pci_printk(pci, KERN_WARNING,
315                                 "failed to create sysfs pci\n");
316                 goto fail1;
317         }
318
319         if (pci->edac_check != NULL) {
320                 pci->op_state = OP_RUNNING_POLL;
321
322                 edac_pci_workq_setup(pci, 1000);
323         } else {
324                 pci->op_state = OP_RUNNING_INTERRUPT;
325         }
326
327         edac_pci_printk(pci, KERN_INFO,
328                 "Giving out device to module '%s' controller '%s':"
329                 " DEV '%s' (%s)\n",
330                 pci->mod_name,
331                 pci->ctl_name,
332                 dev_name(pci),
333                 edac_op_state_toString(pci->op_state));
334
335         edac_unlock_pci_list();
336         return 0;
337
338 fail1:
339         del_edac_pci_from_global_list(pci);
340 fail0:
341         edac_unlock_pci_list();
342         return 1;
343 }
344 EXPORT_SYMBOL_GPL(edac_pci_add_device);
345
346 /*
347  * edac_pci_del_device()
348  *      Remove sysfs entries for specified edac_pci structure and
349  *      then remove edac_pci structure from global list
350  *
351  * @dev:
352  *      Pointer to 'struct device' representing edac_pci structure
353  *      to remove
354  *
355  * Return:
356  *      Pointer to removed edac_pci structure,
357  *      or NULL if device not found
358  */
359 struct edac_pci_ctl_info * edac_pci_del_device(struct device *dev)
360 {
361         struct edac_pci_ctl_info *pci;
362
363         debugf0("%s()\n", __func__);
364
365         edac_lock_pci_list();
366
367         if ((pci = find_edac_pci_by_dev(dev)) == NULL) {
368                 edac_unlock_pci_list();
369                 return NULL;
370         }
371
372         pci->op_state = OP_OFFLINE;
373
374         edac_pci_workq_teardown(pci);
375
376         edac_pci_remove_sysfs(pci);
377
378         del_edac_pci_from_global_list(pci);
379
380         edac_unlock_pci_list();
381
382         edac_printk(KERN_INFO, EDAC_PCI,
383                 "Removed device %d for %s %s: DEV %s\n",
384                 pci->pci_idx,
385                 pci->mod_name,
386                 pci->ctl_name,
387                 dev_name(pci));
388
389         return pci;
390 }
391 EXPORT_SYMBOL_GPL(edac_pci_del_device);
392
393 static inline int edac_pci_get_log_pe(struct edac_pci_ctl_info *pci)
394 {
395         return pci->log_parity_error;
396 }
397
398 static inline int edac_pci_get_panic_on_pe(struct edac_pci_ctl_info *pci)
399 {
400         return pci->panic_on_pe;
401 }
402
403 void edac_pci_generic_check(struct edac_pci_ctl_info *pci)
404 {
405         edac_pci_do_parity_check();
406 }
407
408 static int edac_pci_idx = 0;
409 #define EDAC_PCI_GENCTL_NAME    "EDAC PCI controller"
410
411 struct edac_pci_gen_data {
412         int edac_idx;
413 };
414
415 struct edac_pci_ctl_info *
416 edac_pci_create_generic_ctl(struct device *dev, const char *mod_name)
417 {
418         struct edac_pci_ctl_info *pci;
419         struct edac_pci_gen_data *pdata;
420
421         pci = edac_pci_alloc_ctl_info(sizeof(*pdata), EDAC_PCI_GENCTL_NAME);
422         if (!pci)
423                 return NULL;
424
425         pdata = pci->pvt_info;
426         pci->dev = dev;
427         dev_set_drvdata(pci->dev, pci);
428         pci->dev_name = pci_name(to_pci_dev(dev));
429
430         pci->mod_name = mod_name;
431         pci->ctl_name = EDAC_PCI_GENCTL_NAME;
432         pci->edac_check = edac_pci_generic_check;
433
434         pdata->edac_idx = edac_pci_idx++;
435
436         if (edac_pci_add_device(pci, pdata->edac_idx) > 0) {
437                 debugf3("%s(): failed edac_pci_add_device()\n", __func__);
438                 edac_pci_free_ctl_info(pci);
439                 return NULL;
440         }
441
442         return pci;
443 }
444 EXPORT_SYMBOL_GPL(edac_pci_create_generic_ctl);
445
446 void edac_pci_release_generic_ctl(struct edac_pci_ctl_info *pci)
447 {
448         edac_pci_del_device(pci->dev);
449         edac_pci_free_ctl_info(pci);
450 }
451 EXPORT_SYMBOL_GPL(edac_pci_release_generic_ctl);