db8ce9f89fcbc257b861e08cea30a2c4aa815fe9
[linux-flexiantxendom0-3.2.10.git] / arch / ppc64 / kernel / rtasd.c
1 /*
2  * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version
7  * 2 of the License, or (at your option) any later version.
8  *
9  * Communication to userspace based on kernel/printk.c
10  */
11
12 #include <linux/types.h>
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/poll.h>
17 #include <linux/proc_fs.h>
18 #include <linux/init.h>
19 #include <linux/vmalloc.h>
20 #include <linux/spinlock.h>
21 #include <linux/cpu.h>
22
23 #include <asm/uaccess.h>
24 #include <asm/io.h>
25 #include <asm/rtas.h>
26 #include <asm/prom.h>
27 #include <asm/nvram.h>
28 #include <asm/atomic.h>
29 #include <asm/proc_fs.h>
30
31 #if 0
32 #define DEBUG(A...)     printk(KERN_ERR A)
33 #else
34 #define DEBUG(A...)
35 #endif
36
37 static spinlock_t log_lock = SPIN_LOCK_UNLOCKED;
38
39 DECLARE_WAIT_QUEUE_HEAD(rtas_log_wait);
40
41 static char *rtas_log_buf;
42 static unsigned long rtas_log_start;
43 static unsigned long rtas_log_size;
44
45 static int surveillance_timeout = -1;
46 static unsigned int rtas_event_scan_rate;
47 static unsigned int rtas_error_log_max;
48 static unsigned int rtas_error_log_buffer_max;
49
50 extern spinlock_t proc_ppc64_lock;
51 extern volatile int no_more_logging;
52
53 volatile int error_log_cnt = 0;
54
55 /*
56  * Since we use 32 bit RTAS, the physical address of this must be below
57  * 4G or else bad things happen. Allocate this in the kernel data and
58  * make it big enough.
59  */
60 static unsigned char logdata[RTAS_ERROR_LOG_MAX];
61
62 /* To see this info, grep RTAS /var/log/messages and each entry
63  * will be collected together with obvious begin/end.
64  * There will be a unique identifier on the begin and end lines.
65  * This will persist across reboots.
66  *
67  * format of error logs returned from RTAS:
68  * bytes        (size)  : contents
69  * --------------------------------------------------------
70  * 0-7          (8)     : rtas_error_log
71  * 8-47         (40)    : extended info
72  * 48-51        (4)     : vendor id
73  * 52-1023 (vendor specific) : location code and debug data
74  */
75 static void printk_log_rtas(char *buf, int len)
76 {
77
78         int i,j,n;
79         int perline = 16;
80         char buffer[64];
81         char * str = "RTAS event";
82
83         printk(RTAS_ERR "%d -------- %s begin --------\n", error_log_cnt, str);
84
85         /*
86          * Print perline bytes on each line, each line will start
87          * with RTAS and a changing number, so syslogd will
88          * print lines that are otherwise the same.  Separate every
89          * 4 bytes with a space.
90          */
91         for (i=0; i < len; i++) {
92                 j = i % perline;
93                 if (j == 0) {
94                         memset(buffer, 0, sizeof(buffer));
95                         n = sprintf(buffer, "RTAS %d:", i/perline);
96                 }
97
98                 if ((i % 4) == 0)
99                         n += sprintf(buffer+n, " ");
100
101                 n += sprintf(buffer+n, "%02x", (unsigned char)buf[i]);
102
103                 if (j == (perline-1))
104                         printk(KERN_ERR "%s\n", buffer);
105         }
106         if ((i % perline) != 0)
107                 printk(KERN_ERR "%s\n", buffer);
108
109         printk(RTAS_ERR "%d -------- %s end ----------\n", error_log_cnt, str);
110 }
111
112 static int log_rtas_len(char * buf)
113 {
114         int len;
115         struct rtas_error_log *err;
116
117         /* rtas fixed header */
118         len = 8;
119         err = (struct rtas_error_log *)buf;
120         if (err->extended_log_length) {
121
122                 /* extended header */
123                 len += err->extended_log_length;
124
125                 if (len > rtas_error_log_max)
126                         len = rtas_error_log_max;
127         }
128         return len;
129 }
130
131 /*
132  * First write to nvram, if fatal error, that is the only
133  * place we log the info.  The error will be picked up
134  * on the next reboot by rtasd.  If not fatal, run the
135  * method for the type of error.  Currently, only RTAS
136  * errors have methods implemented, but in the future
137  * there might be a need to store data in nvram before a
138  * call to panic().
139  *
140  * XXX We write to nvram periodically, to indicate error has
141  * been written and sync'd, but there is a possibility
142  * that if we don't shutdown correctly, a duplicate error
143  * record will be created on next reboot.
144  */
145 void pSeries_log_error(char *buf, unsigned int err_type, int fatal)
146 {
147         unsigned long offset;
148         unsigned long s;
149         int len = 0;
150
151         DEBUG("logging event\n");
152
153         if (buf == NULL)
154                 return;
155
156         spin_lock_irqsave(&log_lock, s);
157
158         /* get length and increase count */
159         switch (err_type & ERR_TYPE_MASK) {
160         case ERR_TYPE_RTAS_LOG:
161                 len = log_rtas_len(buf);
162                 if (!(err_type & ERR_FLAG_BOOT))
163                         error_log_cnt++;
164                 break;
165         case ERR_TYPE_KERNEL_PANIC:
166         default:
167                 spin_unlock_irqrestore(&log_lock, s);
168                 return;
169         }
170
171         /* Write error to NVRAM */
172         if (!no_more_logging && !(err_type & ERR_FLAG_BOOT))
173                 nvram_write_error_log(buf, len, err_type);
174
175         /* Check to see if we need to or have stopped logging */
176         if (fatal || no_more_logging) {
177                 no_more_logging = 1;
178                 spin_unlock_irqrestore(&log_lock, s);
179                 return;
180         }
181
182         /* call type specific method for error */
183         switch (err_type & ERR_TYPE_MASK) {
184         case ERR_TYPE_RTAS_LOG:
185                 /* put into syslog and error_log file */
186                 printk_log_rtas(buf, len);
187
188                 offset = rtas_error_log_buffer_max *
189                         ((rtas_log_start+rtas_log_size) & LOG_NUMBER_MASK);
190
191                 /* First copy over sequence number */
192                 memcpy(&rtas_log_buf[offset], (void *) &error_log_cnt, sizeof(int));
193
194                 /* Second copy over error log data */
195                 offset += sizeof(int);
196                 memcpy(&rtas_log_buf[offset], buf, len);
197
198                 if (rtas_log_size < LOG_NUMBER)
199                         rtas_log_size += 1;
200                 else
201                         rtas_log_start += 1;
202
203                 spin_unlock_irqrestore(&log_lock, s);
204                 wake_up_interruptible(&rtas_log_wait);
205                 break;
206         case ERR_TYPE_KERNEL_PANIC:
207         default:
208                 spin_unlock_irqrestore(&log_lock, s);
209                 return;
210         }
211
212 }
213
214
215 static int rtas_log_open(struct inode * inode, struct file * file)
216 {
217         return 0;
218 }
219
220 static int rtas_log_release(struct inode * inode, struct file * file)
221 {
222         return 0;
223 }
224
225 /* This will check if all events are logged, if they are then, we
226  * know that we can safely clear the events in NVRAM.
227  * Next we'll sit and wait for something else to log.
228  */
229 static ssize_t rtas_log_read(struct file * file, char * buf,
230                          size_t count, loff_t *ppos)
231 {
232         int error;
233         char *tmp;
234         unsigned long s;
235         unsigned long offset;
236
237         if (!buf || count < rtas_error_log_buffer_max)
238                 return -EINVAL;
239
240         count = rtas_error_log_buffer_max;
241
242         error = verify_area(VERIFY_WRITE, buf, count);
243         if (error)
244                 return -EFAULT;
245
246         tmp = kmalloc(count, GFP_KERNEL);
247         if (!tmp)
248                 return -ENOMEM;
249
250
251         spin_lock_irqsave(&log_lock, s);
252         /* if it's 0, then we know we got the last one (the one in NVRAM) */
253         if (rtas_log_size == 0 && !no_more_logging)
254                 nvram_clear_error_log();
255         spin_unlock_irqrestore(&log_lock, s);
256
257
258         error = wait_event_interruptible(rtas_log_wait, rtas_log_size);
259         if (error)
260                 goto out;
261
262         spin_lock_irqsave(&log_lock, s);
263         offset = rtas_error_log_buffer_max * (rtas_log_start & LOG_NUMBER_MASK);
264         memcpy(tmp, &rtas_log_buf[offset], count);
265
266         rtas_log_start += 1;
267         rtas_log_size -= 1;
268         spin_unlock_irqrestore(&log_lock, s);
269
270         error = copy_to_user(buf, tmp, count) ? -EFAULT : count;
271 out:
272         kfree(tmp);
273         return error;
274 }
275
276 static unsigned int rtas_log_poll(struct file *file, poll_table * wait)
277 {
278         poll_wait(file, &rtas_log_wait, wait);
279         if (rtas_log_size)
280                 return POLLIN | POLLRDNORM;
281         return 0;
282 }
283
284 struct file_operations proc_rtas_log_operations = {
285         .read =         rtas_log_read,
286         .poll =         rtas_log_poll,
287         .open =         rtas_log_open,
288         .release =      rtas_log_release,
289 };
290
291 static int enable_surveillance(int timeout)
292 {
293         int error;
294
295         error = rtas_call(rtas_token("set-indicator"), 3, 1, NULL,
296                           SURVEILLANCE_TOKEN, 0, timeout);
297
298         if (error) {
299                 printk(KERN_ERR "rtasd: could not enable surveillance\n");
300                 return -1;
301         }
302
303         return 0;
304 }
305
306 static int get_eventscan_parms(void)
307 {
308         struct device_node *node;
309         int *ip;
310
311         node = of_find_node_by_path("/rtas");
312
313         ip = (int *)get_property(node, "rtas-event-scan-rate", NULL);
314         if (ip == NULL) {
315                 printk(KERN_ERR "rtasd: no rtas-event-scan-rate\n");
316                 of_node_put(node);
317                 return -1;
318         }
319         rtas_event_scan_rate = *ip;
320         DEBUG("rtas-event-scan-rate %d\n", rtas_event_scan_rate);
321
322         ip = (int *)get_property(node, "rtas-error-log-max", NULL);
323         if (ip == NULL) {
324                 printk(KERN_ERR "rtasd: no rtas-error-log-max\n");
325                 of_node_put(node);
326                 return -1;
327         }
328         rtas_error_log_max = *ip;
329         DEBUG("rtas-error-log-max %d\n", rtas_error_log_max);
330
331         if (rtas_error_log_max > RTAS_ERROR_LOG_MAX) {
332                 printk(KERN_ERR "rtasd: truncated error log from %d to %d bytes\n", rtas_error_log_max, RTAS_ERROR_LOG_MAX);
333                 rtas_error_log_max = RTAS_ERROR_LOG_MAX;
334         }
335         of_node_put(node);
336
337         return 0;
338 }
339
340 static void do_event_scan(int event_scan)
341 {
342         int error;
343         do {
344                 memset(logdata, 0, rtas_error_log_max);
345                 error = rtas_call(event_scan, 4, 1, NULL,
346                                   RTAS_EVENT_SCAN_ALL_EVENTS, 0,
347                                   __pa(logdata), rtas_error_log_max);
348                 if (error == -1) {
349                         printk(KERN_ERR "event-scan failed\n");
350                         break;
351                 }
352
353                 if (error == 0)
354                         pSeries_log_error(logdata, ERR_TYPE_RTAS_LOG, 0);
355
356         } while(error == 0);
357 }
358
359 static int rtasd(void *unused)
360 {
361         unsigned int err_type;
362         int cpu = 0;
363         int event_scan = rtas_token("event-scan");
364         cpumask_t all = CPU_MASK_ALL;
365         int rc;
366
367         daemonize("rtasd");
368
369         if (event_scan == RTAS_UNKNOWN_SERVICE || get_eventscan_parms() == -1)
370                 goto error;
371
372         rtas_log_buf = vmalloc(rtas_error_log_max*LOG_NUMBER);
373         if (!rtas_log_buf) {
374                 printk(KERN_ERR "rtasd: no memory\n");
375                 goto error;
376         }
377
378         /* We can use rtas_log_buf now */
379         no_more_logging = 0;
380
381         printk(KERN_ERR "RTAS daemon started\n");
382
383         DEBUG("will sleep for %d jiffies\n", (HZ*60/rtas_event_scan_rate) / 2);
384
385         /* See if we have any error stored in NVRAM */
386         memset(logdata, 0, rtas_error_log_max);
387
388         rc = nvram_read_error_log(logdata, rtas_error_log_max, &err_type);
389         if (!rc) {
390                 if (err_type != ERR_FLAG_ALREADY_LOGGED) {
391                         pSeries_log_error(logdata, err_type | ERR_FLAG_BOOT, 0);
392                 }
393         }
394
395         /* First pass. */
396         lock_cpu_hotplug();
397         for_each_online_cpu(cpu) {
398                 DEBUG("scheduling on %d\n", cpu);
399                 set_cpus_allowed(current, cpumask_of_cpu(cpu));
400                 DEBUG("watchdog scheduled on cpu %d\n", smp_processor_id());
401
402                 do_event_scan(event_scan);
403                 set_current_state(TASK_INTERRUPTIBLE);
404                 schedule_timeout(HZ);
405         }
406         unlock_cpu_hotplug();
407
408         if (surveillance_timeout != -1) {
409                 DEBUG("enabling surveillance\n");
410                 enable_surveillance(surveillance_timeout);
411                 DEBUG("surveillance enabled\n");
412         }
413
414         lock_cpu_hotplug();
415         cpu = first_cpu_const(mk_cpumask_const(cpu_online_map));
416         for (;;) {
417                 set_cpus_allowed(current, cpumask_of_cpu(cpu));
418                 do_event_scan(event_scan);
419                 set_cpus_allowed(current, all);
420
421                 /* Drop hotplug lock, and sleep for a bit (at least
422                  * one second since some machines have problems if we
423                  * call event-scan too quickly). */
424                 unlock_cpu_hotplug();
425                 set_current_state(TASK_INTERRUPTIBLE);
426                 schedule_timeout((HZ*60/rtas_event_scan_rate) / 2);
427                 lock_cpu_hotplug();
428
429                 cpu = next_cpu_const(cpu, mk_cpumask_const(cpu_online_map));
430                 if (cpu == NR_CPUS)
431                         cpu = first_cpu_const(mk_cpumask_const(cpu_online_map));
432         }
433
434 error:
435         /* Should delete proc entries */
436         return -EINVAL;
437 }
438
439 static int __init rtas_init(void)
440 {
441         struct proc_dir_entry *entry;
442
443         if (proc_ppc64.rtas == NULL) {
444                 proc_ppc64_init();
445         }
446
447         if (proc_ppc64.rtas == NULL) {
448                 printk(KERN_ERR "rtas_init: /proc/ppc64/rtas does not exist.");
449                 return -EIO;
450         }
451
452         entry = create_proc_entry("error_log", S_IRUSR, proc_ppc64.rtas);
453         if (entry)
454                 entry->proc_fops = &proc_rtas_log_operations;
455         else
456                 printk(KERN_ERR "Failed to create rtas/error_log proc entry\n");
457
458         if (kernel_thread(rtasd, 0, CLONE_FS) < 0)
459                 printk(KERN_ERR "Failed to start RTAS daemon\n");
460
461         /* Make room for the sequence number */
462         rtas_error_log_buffer_max = rtas_error_log_max + sizeof(int);
463
464         return 0;
465 }
466
467 static int __init surveillance_setup(char *str)
468 {
469         int i;
470
471         if (get_option(&str,&i)) {
472                 if (i >= 0 && i <= 255)
473                         surveillance_timeout = i;
474         }
475
476         return 1;
477 }
478
479 __initcall(rtas_init);
480 __setup("surveillance=", surveillance_setup);