3cd3a236821cb61c983fb34bc56df7f6383fb2cd
[pandora-kernel.git] / drivers / edac / edac_module.c
1
2 #include <linux/freezer.h>
3 #include <linux/kthread.h>
4
5 #include "edac_mc.h"
6 #include "edac_module.h"
7
8 #define EDAC_MC_VERSION "Ver: 2.0.3" __DATE__
9
10 #ifdef CONFIG_EDAC_DEBUG
11 /* Values of 0 to 4 will generate output */
12 int edac_debug_level = 1;
13 EXPORT_SYMBOL_GPL(edac_debug_level);
14 #endif
15
16 /* scope is to module level only */
17 struct workqueue_struct *edac_workqueue;
18
19 /* private to this file */
20 static struct task_struct *edac_thread;
21
22
23 /*
24  * sysfs object: /sys/devices/system/edac
25  *      need to export to other files in this modules
26  */
27 static struct sysdev_class edac_class = {
28         set_kset_name("edac"),
29 };
30 static int edac_class_valid = 0;
31
32 /*
33  * edac_get_edac_class()
34  *
35  *      return pointer to the edac class of 'edac'
36  */
37 struct sysdev_class *edac_get_edac_class(void)
38 {
39         struct sysdev_class *classptr=NULL;
40
41         if (edac_class_valid)
42                 classptr = &edac_class;
43
44         return classptr;
45 }
46
47 /*
48  * edac_register_sysfs_edac_name()
49  *
50  *      register the 'edac' into /sys/devices/system
51  *
52  * return:
53  *      0  success
54  *      !0 error
55  */
56 static int edac_register_sysfs_edac_name(void)
57 {
58         int err;
59
60         /* create the /sys/devices/system/edac directory */
61         err = sysdev_class_register(&edac_class);
62
63         if (err) {
64                 debugf1("%s() error=%d\n", __func__, err);
65                 return err;
66         }
67
68         edac_class_valid = 1;
69         return 0;
70 }
71
72 /*
73  * sysdev_class_unregister()
74  *
75  *      unregister the 'edac' from /sys/devices/system
76  */
77 static void edac_unregister_sysfs_edac_name(void)
78 {
79         /* only if currently registered, then unregister it */
80         if (edac_class_valid)
81                 sysdev_class_unregister(&edac_class);
82
83         edac_class_valid = 0;
84 }
85
86
87 /*
88  * Check MC status every edac_get_poll_msec().
89  * Check PCI status every edac_get_poll_msec() as well.
90  *
91  * This where the work gets done for edac.
92  *
93  * SMP safe, doesn't use NMI, and auto-rate-limits.
94  */
95 static void do_edac_check(void)
96 {
97         debugf3("%s()\n", __func__);
98
99         /* perform the poll activities */
100         edac_check_mc_devices();
101         edac_pci_do_parity_check();
102 }
103
104 /*
105  * Action thread for EDAC to perform the POLL operations
106  */
107 static int edac_kernel_thread(void *arg)
108 {
109         int msec;
110
111         while (!kthread_should_stop()) {
112
113                 do_edac_check();
114
115                 /* goto sleep for the interval */
116                 msec = (HZ * edac_get_poll_msec()) / 1000;
117                 schedule_timeout_interruptible(msec);
118                 try_to_freeze();
119         }
120
121         return 0;
122 }
123
124 /*
125  * edac_workqueue_setup
126  *      initialize the edac work queue for polling operations
127  */
128 static int edac_workqueue_setup(void)
129 {
130         edac_workqueue = create_singlethread_workqueue("edac-poller");
131         if (edac_workqueue == NULL)
132                 return -ENODEV;
133         else
134                 return 0;
135 }
136
137 /*
138  * edac_workqueue_teardown
139  *      teardown the edac workqueue
140  */
141 static void edac_workqueue_teardown(void)
142 {
143         if (edac_workqueue) {
144                 flush_workqueue(edac_workqueue);
145                 destroy_workqueue(edac_workqueue);
146                 edac_workqueue = NULL;
147         }
148 }
149
150
151 /*
152  * edac_init
153  *      module initialization entry point
154  */
155 static int __init edac_init(void)
156 {
157         int err = 0;
158
159         edac_printk(KERN_INFO, EDAC_MC, EDAC_MC_VERSION "\n");
160
161         /*
162          * Harvest and clear any boot/initialization PCI parity errors
163          *
164          * FIXME: This only clears errors logged by devices present at time of
165          *      module initialization.  We should also do an initial clear
166          *      of each newly hotplugged device.
167          */
168         edac_pci_clear_parity_errors();
169
170         /*
171          * perform the registration of the /sys/devices/system/edac object
172          */
173         if (edac_register_sysfs_edac_name()) {
174                 edac_printk(KERN_ERR, EDAC_MC,
175                         "Error initializing 'edac' kobject\n");
176                 err = -ENODEV;
177                 goto error;
178         }
179
180         /* Create the MC sysfs entries, must be first
181          */
182         if (edac_sysfs_memctrl_setup()) {
183                 edac_printk(KERN_ERR, EDAC_MC,
184                         "Error initializing sysfs code\n");
185                 err = -ENODEV;
186                 goto error_sysfs;
187         }
188
189         /* Create the PCI parity sysfs entries */
190         if (edac_sysfs_pci_setup()) {
191                 edac_printk(KERN_ERR, EDAC_MC,
192                         "PCI: Error initializing sysfs code\n");
193                 err = -ENODEV;
194                 goto error_mem;
195         }
196
197         /* Setup/Initialize the edac_device system */
198         err = edac_workqueue_setup();
199         if (err) {
200                 edac_printk(KERN_ERR, EDAC_MC, "init WorkQueue failure\n");
201                 goto error_pci;
202         }
203
204         /* create our kernel thread */
205         edac_thread = kthread_run(edac_kernel_thread, NULL, "kedac");
206
207         if (IS_ERR(edac_thread)) {
208                 err = PTR_ERR(edac_thread);
209                 goto error_work;
210         }
211
212         return 0;
213
214         /* Error teardown stack */
215 error_work:
216         edac_workqueue_teardown();
217 error_pci:
218         edac_sysfs_pci_teardown();
219 error_mem:
220         edac_sysfs_memctrl_teardown();
221 error_sysfs:
222         edac_unregister_sysfs_edac_name();
223 error:
224         return err;
225 }
226
227 /*
228  * edac_exit()
229  *      module exit/termination function
230  */
231 static void __exit edac_exit(void)
232 {
233         debugf0("%s()\n", __func__);
234         kthread_stop(edac_thread);
235
236         /* tear down the various subsystems*/
237         edac_workqueue_teardown();
238         edac_sysfs_memctrl_teardown();
239         edac_sysfs_pci_teardown();
240         edac_unregister_sysfs_edac_name();
241 }
242
243 /*
244  * Inform the kernel of our entry and exit points
245  */
246 module_init(edac_init);
247 module_exit(edac_exit);
248
249 MODULE_LICENSE("GPL");
250 MODULE_AUTHOR("Doug Thompson www.softwarebitmaker.com, et al");
251 MODULE_DESCRIPTION("Core library routines for EDAC reporting");
252
253 /* refer to *_sysfs.c files for parameters that are exported via sysfs */
254
255 #ifdef CONFIG_EDAC_DEBUG
256 module_param(edac_debug_level, int, 0644);
257 MODULE_PARM_DESC(edac_debug_level, "Debug level");
258 #endif
259