]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/platforms/pseries/rtasd.c
[POWERPC] pseries: Use rtas_token instead of hand-rolled code
[linux-2.6-omap-h63xx.git] / arch / powerpc / platforms / pseries / 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 #include <linux/delay.h>
23
24 #include <asm/uaccess.h>
25 #include <asm/io.h>
26 #include <asm/rtas.h>
27 #include <asm/prom.h>
28 #include <asm/nvram.h>
29 #include <asm/atomic.h>
30 #include <asm/machdep.h>
31
32 #if 0
33 #define DEBUG(A...)     printk(KERN_ERR A)
34 #else
35 #define DEBUG(A...)
36 #endif
37
38 static DEFINE_SPINLOCK(rtasd_log_lock);
39
40 DECLARE_WAIT_QUEUE_HEAD(rtas_log_wait);
41
42 static char *rtas_log_buf;
43 static unsigned long rtas_log_start;
44 static unsigned long rtas_log_size;
45
46 static int surveillance_timeout = -1;
47 static unsigned int rtas_error_log_max;
48 static unsigned int rtas_error_log_buffer_max;
49
50 /* RTAS service tokens */
51 static unsigned int event_scan;
52 static unsigned int rtas_event_scan_rate;
53
54 static int full_rtas_msgs = 0;
55
56 extern int no_logging;
57
58 volatile int error_log_cnt = 0;
59
60 /*
61  * Since we use 32 bit RTAS, the physical address of this must be below
62  * 4G or else bad things happen. Allocate this in the kernel data and
63  * make it big enough.
64  */
65 static unsigned char logdata[RTAS_ERROR_LOG_MAX];
66
67 static int get_eventscan_parms(void);
68
69 static char *rtas_type[] = {
70         "Unknown", "Retry", "TCE Error", "Internal Device Failure",
71         "Timeout", "Data Parity", "Address Parity", "Cache Parity",
72         "Address Invalid", "ECC Uncorrected", "ECC Corrupted",
73 };
74
75 static char *rtas_event_type(int type)
76 {
77         if ((type > 0) && (type < 11))
78                 return rtas_type[type];
79
80         switch (type) {
81                 case RTAS_TYPE_EPOW:
82                         return "EPOW";
83                 case RTAS_TYPE_PLATFORM:
84                         return "Platform Error";
85                 case RTAS_TYPE_IO:
86                         return "I/O Event";
87                 case RTAS_TYPE_INFO:
88                         return "Platform Information Event";
89                 case RTAS_TYPE_DEALLOC:
90                         return "Resource Deallocation Event";
91                 case RTAS_TYPE_DUMP:
92                         return "Dump Notification Event";
93         }
94
95         return rtas_type[0];
96 }
97
98 /* To see this info, grep RTAS /var/log/messages and each entry
99  * will be collected together with obvious begin/end.
100  * There will be a unique identifier on the begin and end lines.
101  * This will persist across reboots.
102  *
103  * format of error logs returned from RTAS:
104  * bytes        (size)  : contents
105  * --------------------------------------------------------
106  * 0-7          (8)     : rtas_error_log
107  * 8-47         (40)    : extended info
108  * 48-51        (4)     : vendor id
109  * 52-1023 (vendor specific) : location code and debug data
110  */
111 static void printk_log_rtas(char *buf, int len)
112 {
113
114         int i,j,n = 0;
115         int perline = 16;
116         char buffer[64];
117         char * str = "RTAS event";
118
119         if (full_rtas_msgs) {
120                 printk(RTAS_DEBUG "%d -------- %s begin --------\n",
121                        error_log_cnt, str);
122
123                 /*
124                  * Print perline bytes on each line, each line will start
125                  * with RTAS and a changing number, so syslogd will
126                  * print lines that are otherwise the same.  Separate every
127                  * 4 bytes with a space.
128                  */
129                 for (i = 0; i < len; i++) {
130                         j = i % perline;
131                         if (j == 0) {
132                                 memset(buffer, 0, sizeof(buffer));
133                                 n = sprintf(buffer, "RTAS %d:", i/perline);
134                         }
135
136                         if ((i % 4) == 0)
137                                 n += sprintf(buffer+n, " ");
138
139                         n += sprintf(buffer+n, "%02x", (unsigned char)buf[i]);
140
141                         if (j == (perline-1))
142                                 printk(KERN_DEBUG "%s\n", buffer);
143                 }
144                 if ((i % perline) != 0)
145                         printk(KERN_DEBUG "%s\n", buffer);
146
147                 printk(RTAS_DEBUG "%d -------- %s end ----------\n",
148                        error_log_cnt, str);
149         } else {
150                 struct rtas_error_log *errlog = (struct rtas_error_log *)buf;
151
152                 printk(RTAS_DEBUG "event: %d, Type: %s, Severity: %d\n",
153                        error_log_cnt, rtas_event_type(errlog->type),
154                        errlog->severity);
155         }
156 }
157
158 static int log_rtas_len(char * buf)
159 {
160         int len;
161         struct rtas_error_log *err;
162
163         /* rtas fixed header */
164         len = 8;
165         err = (struct rtas_error_log *)buf;
166         if (err->extended_log_length) {
167
168                 /* extended header */
169                 len += err->extended_log_length;
170         }
171
172         if (rtas_error_log_max == 0) {
173                 get_eventscan_parms();
174         }
175         if (len > rtas_error_log_max)
176                 len = rtas_error_log_max;
177
178         return len;
179 }
180
181 /*
182  * First write to nvram, if fatal error, that is the only
183  * place we log the info.  The error will be picked up
184  * on the next reboot by rtasd.  If not fatal, run the
185  * method for the type of error.  Currently, only RTAS
186  * errors have methods implemented, but in the future
187  * there might be a need to store data in nvram before a
188  * call to panic().
189  *
190  * XXX We write to nvram periodically, to indicate error has
191  * been written and sync'd, but there is a possibility
192  * that if we don't shutdown correctly, a duplicate error
193  * record will be created on next reboot.
194  */
195 void pSeries_log_error(char *buf, unsigned int err_type, int fatal)
196 {
197         unsigned long offset;
198         unsigned long s;
199         int len = 0;
200
201         DEBUG("logging event\n");
202         if (buf == NULL)
203                 return;
204
205         spin_lock_irqsave(&rtasd_log_lock, s);
206
207         /* get length and increase count */
208         switch (err_type & ERR_TYPE_MASK) {
209         case ERR_TYPE_RTAS_LOG:
210                 len = log_rtas_len(buf);
211                 if (!(err_type & ERR_FLAG_BOOT))
212                         error_log_cnt++;
213                 break;
214         case ERR_TYPE_KERNEL_PANIC:
215         default:
216                 spin_unlock_irqrestore(&rtasd_log_lock, s);
217                 return;
218         }
219
220         /* Write error to NVRAM */
221         if (!no_logging && !(err_type & ERR_FLAG_BOOT))
222                 nvram_write_error_log(buf, len, err_type);
223
224         /*
225          * rtas errors can occur during boot, and we do want to capture
226          * those somewhere, even if nvram isn't ready (why not?), and even
227          * if rtasd isn't ready. Put them into the boot log, at least.
228          */
229         if ((err_type & ERR_TYPE_MASK) == ERR_TYPE_RTAS_LOG)
230                 printk_log_rtas(buf, len);
231
232         /* Check to see if we need to or have stopped logging */
233         if (fatal || no_logging) {
234                 no_logging = 1;
235                 spin_unlock_irqrestore(&rtasd_log_lock, s);
236                 return;
237         }
238
239         /* call type specific method for error */
240         switch (err_type & ERR_TYPE_MASK) {
241         case ERR_TYPE_RTAS_LOG:
242                 offset = rtas_error_log_buffer_max *
243                         ((rtas_log_start+rtas_log_size) & LOG_NUMBER_MASK);
244
245                 /* First copy over sequence number */
246                 memcpy(&rtas_log_buf[offset], (void *) &error_log_cnt, sizeof(int));
247
248                 /* Second copy over error log data */
249                 offset += sizeof(int);
250                 memcpy(&rtas_log_buf[offset], buf, len);
251
252                 if (rtas_log_size < LOG_NUMBER)
253                         rtas_log_size += 1;
254                 else
255                         rtas_log_start += 1;
256
257                 spin_unlock_irqrestore(&rtasd_log_lock, s);
258                 wake_up_interruptible(&rtas_log_wait);
259                 break;
260         case ERR_TYPE_KERNEL_PANIC:
261         default:
262                 spin_unlock_irqrestore(&rtasd_log_lock, s);
263                 return;
264         }
265
266 }
267
268
269 static int rtas_log_open(struct inode * inode, struct file * file)
270 {
271         return 0;
272 }
273
274 static int rtas_log_release(struct inode * inode, struct file * file)
275 {
276         return 0;
277 }
278
279 /* This will check if all events are logged, if they are then, we
280  * know that we can safely clear the events in NVRAM.
281  * Next we'll sit and wait for something else to log.
282  */
283 static ssize_t rtas_log_read(struct file * file, char __user * buf,
284                          size_t count, loff_t *ppos)
285 {
286         int error;
287         char *tmp;
288         unsigned long s;
289         unsigned long offset;
290
291         if (!buf || count < rtas_error_log_buffer_max)
292                 return -EINVAL;
293
294         count = rtas_error_log_buffer_max;
295
296         if (!access_ok(VERIFY_WRITE, buf, count))
297                 return -EFAULT;
298
299         tmp = kmalloc(count, GFP_KERNEL);
300         if (!tmp)
301                 return -ENOMEM;
302
303
304         spin_lock_irqsave(&rtasd_log_lock, s);
305         /* if it's 0, then we know we got the last one (the one in NVRAM) */
306         if (rtas_log_size == 0 && !no_logging)
307                 nvram_clear_error_log();
308         spin_unlock_irqrestore(&rtasd_log_lock, s);
309
310
311         error = wait_event_interruptible(rtas_log_wait, rtas_log_size);
312         if (error)
313                 goto out;
314
315         spin_lock_irqsave(&rtasd_log_lock, s);
316         offset = rtas_error_log_buffer_max * (rtas_log_start & LOG_NUMBER_MASK);
317         memcpy(tmp, &rtas_log_buf[offset], count);
318
319         rtas_log_start += 1;
320         rtas_log_size -= 1;
321         spin_unlock_irqrestore(&rtasd_log_lock, s);
322
323         error = copy_to_user(buf, tmp, count) ? -EFAULT : count;
324 out:
325         kfree(tmp);
326         return error;
327 }
328
329 static unsigned int rtas_log_poll(struct file *file, poll_table * wait)
330 {
331         poll_wait(file, &rtas_log_wait, wait);
332         if (rtas_log_size)
333                 return POLLIN | POLLRDNORM;
334         return 0;
335 }
336
337 const struct file_operations proc_rtas_log_operations = {
338         .read =         rtas_log_read,
339         .poll =         rtas_log_poll,
340         .open =         rtas_log_open,
341         .release =      rtas_log_release,
342 };
343
344 static int enable_surveillance(int timeout)
345 {
346         int error;
347
348         error = rtas_set_indicator(SURVEILLANCE_TOKEN, 0, timeout);
349
350         if (error == 0)
351                 return 0;
352
353         if (error == -EINVAL) {
354                 printk(KERN_DEBUG "rtasd: surveillance not supported\n");
355                 return 0;
356         }
357
358         printk(KERN_ERR "rtasd: could not update surveillance\n");
359         return -1;
360 }
361
362 static int get_eventscan_parms(void)
363 {
364         rtas_event_scan_rate = rtas_token("rtas-event-scan-rate");
365         if (rtas_event_scan_rate == RTAS_UNKNOWN_SERVICE) {
366                 printk(KERN_ERR "rtasd: no rtas-event-scan-rate\n");
367                 return -1;
368         }
369         DEBUG("rtas-event-scan-rate %d\n", rtas_event_scan_rate);
370
371         /* Make room for the sequence number */
372         rtas_error_log_max = rtas_get_error_log_max();
373         rtas_error_log_buffer_max = rtas_error_log_max + sizeof(int);
374
375         return 0;
376 }
377
378 static void do_event_scan(void)
379 {
380         int error;
381         do {
382                 memset(logdata, 0, rtas_error_log_max);
383                 error = rtas_call(event_scan, 4, 1, NULL,
384                                   RTAS_EVENT_SCAN_ALL_EVENTS, 0,
385                                   __pa(logdata), rtas_error_log_max);
386                 if (error == -1) {
387                         printk(KERN_ERR "event-scan failed\n");
388                         break;
389                 }
390
391                 if (error == 0)
392                         pSeries_log_error(logdata, ERR_TYPE_RTAS_LOG, 0);
393
394         } while(error == 0);
395 }
396
397 static void do_event_scan_all_cpus(long delay)
398 {
399         int cpu;
400
401         lock_cpu_hotplug();
402         cpu = first_cpu(cpu_online_map);
403         for (;;) {
404                 set_cpus_allowed(current, cpumask_of_cpu(cpu));
405                 do_event_scan();
406                 set_cpus_allowed(current, CPU_MASK_ALL);
407
408                 /* Drop hotplug lock, and sleep for the specified delay */
409                 unlock_cpu_hotplug();
410                 msleep_interruptible(delay);
411                 lock_cpu_hotplug();
412
413                 cpu = next_cpu(cpu, cpu_online_map);
414                 if (cpu == NR_CPUS)
415                         break;
416         }
417         unlock_cpu_hotplug();
418 }
419
420 static int rtasd(void *unused)
421 {
422         unsigned int err_type;
423         int rc;
424
425         daemonize("rtasd");
426
427         if (get_eventscan_parms() == -1)
428                 goto error;
429
430         rtas_log_buf = vmalloc(rtas_error_log_buffer_max*LOG_NUMBER);
431         if (!rtas_log_buf) {
432                 printk(KERN_ERR "rtasd: no memory\n");
433                 goto error;
434         }
435
436         printk(KERN_DEBUG "RTAS daemon started\n");
437
438         DEBUG("will sleep for %d milliseconds\n", (30000/rtas_event_scan_rate));
439
440         /* See if we have any error stored in NVRAM */
441         memset(logdata, 0, rtas_error_log_max);
442
443         rc = nvram_read_error_log(logdata, rtas_error_log_max, &err_type);
444
445         /* We can use rtas_log_buf now */
446         no_logging = 0;
447
448         if (!rc) {
449                 if (err_type != ERR_FLAG_ALREADY_LOGGED) {
450                         pSeries_log_error(logdata, err_type | ERR_FLAG_BOOT, 0);
451                 }
452         }
453
454         /* First pass. */
455         do_event_scan_all_cpus(1000);
456
457         if (surveillance_timeout != -1) {
458                 DEBUG("enabling surveillance\n");
459                 enable_surveillance(surveillance_timeout);
460                 DEBUG("surveillance enabled\n");
461         }
462
463         /* Delay should be at least one second since some
464          * machines have problems if we call event-scan too
465          * quickly. */
466         for (;;)
467                 do_event_scan_all_cpus(30000/rtas_event_scan_rate);
468
469 error:
470         /* Should delete proc entries */
471         return -EINVAL;
472 }
473
474 static int __init rtas_init(void)
475 {
476         struct proc_dir_entry *entry;
477
478         if (!machine_is(pseries))
479                 return 0;
480
481         /* No RTAS */
482         event_scan = rtas_token("event-scan");
483         if (event_scan == RTAS_UNKNOWN_SERVICE) {
484                 printk(KERN_DEBUG "rtasd: no event-scan on system\n");
485                 return -ENODEV;
486         }
487
488         entry = create_proc_entry("ppc64/rtas/error_log", S_IRUSR, NULL);
489         if (entry)
490                 entry->proc_fops = &proc_rtas_log_operations;
491         else
492                 printk(KERN_ERR "Failed to create error_log proc entry\n");
493
494         if (kernel_thread(rtasd, NULL, CLONE_FS) < 0)
495                 printk(KERN_ERR "Failed to start RTAS daemon\n");
496
497         return 0;
498 }
499
500 static int __init surveillance_setup(char *str)
501 {
502         int i;
503
504         if (get_option(&str,&i)) {
505                 if (i >= 0 && i <= 255)
506                         surveillance_timeout = i;
507         }
508
509         return 1;
510 }
511
512 static int __init rtasmsgs_setup(char *str)
513 {
514         if (strcmp(str, "on") == 0)
515                 full_rtas_msgs = 1;
516         else if (strcmp(str, "off") == 0)
517                 full_rtas_msgs = 0;
518
519         return 1;
520 }
521 __initcall(rtas_init);
522 __setup("surveillance=", surveillance_setup);
523 __setup("rtasmsgs=", rtasmsgs_setup);