blob: 762fe87629ff6ac2f9db5654ddc9077e26e75b9d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Nishanth Aravamudan0287ebe2005-09-03 15:56:01 -070022#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
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>
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110030#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#if 0
33#define DEBUG(A...) printk(KERN_ERR A)
34#else
35#define DEBUG(A...)
36#endif
37
38static DEFINE_SPINLOCK(rtasd_log_lock);
39
40DECLARE_WAIT_QUEUE_HEAD(rtas_log_wait);
41
42static char *rtas_log_buf;
43static unsigned long rtas_log_start;
44static unsigned long rtas_log_size;
45
46static int surveillance_timeout = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static unsigned int rtas_error_log_max;
48static unsigned int rtas_error_log_buffer_max;
49
Linas Vepstasa4fc3a32007-08-10 07:01:50 +100050/* RTAS service tokens */
51static unsigned int event_scan;
52static unsigned int rtas_event_scan_rate;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static int full_rtas_msgs = 0;
55
56extern int no_logging;
57
58volatile 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 */
65static unsigned char logdata[RTAS_ERROR_LOG_MAX];
66
67static int get_eventscan_parms(void);
68
69static 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
75static 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 */
111static 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
158static 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 */
195void 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
269static int rtas_log_open(struct inode * inode, struct file * file)
270{
271 return 0;
272}
273
274static 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 */
283static 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;
324out:
325 kfree(tmp);
326 return error;
327}
328
329static 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
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800337const struct file_operations proc_rtas_log_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 .read = rtas_log_read,
339 .poll = rtas_log_poll,
340 .open = rtas_log_open,
341 .release = rtas_log_release,
342};
343
344static 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) {
Olof Johansson90ddfeb2006-04-12 15:28:13 -0500354 printk(KERN_DEBUG "rtasd: surveillance not supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return 0;
356 }
357
358 printk(KERN_ERR "rtasd: could not update surveillance\n");
359 return -1;
360}
361
362static int get_eventscan_parms(void)
363{
364 struct device_node *node;
Jeremy Kerr954a46e2006-07-12 15:39:43 +1000365 const int *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 node = of_find_node_by_path("/rtas");
368
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000369 ip = of_get_property(node, "rtas-event-scan-rate", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (ip == NULL) {
371 printk(KERN_ERR "rtasd: no rtas-event-scan-rate\n");
372 of_node_put(node);
373 return -1;
374 }
375 rtas_event_scan_rate = *ip;
376 DEBUG("rtas-event-scan-rate %d\n", rtas_event_scan_rate);
377
378 /* Make room for the sequence number */
379 rtas_error_log_max = rtas_get_error_log_max();
380 rtas_error_log_buffer_max = rtas_error_log_max + sizeof(int);
381
382 of_node_put(node);
383
384 return 0;
385}
386
Linas Vepstasa4fc3a32007-08-10 07:01:50 +1000387static void do_event_scan(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 int error;
390 do {
391 memset(logdata, 0, rtas_error_log_max);
392 error = rtas_call(event_scan, 4, 1, NULL,
393 RTAS_EVENT_SCAN_ALL_EVENTS, 0,
394 __pa(logdata), rtas_error_log_max);
395 if (error == -1) {
396 printk(KERN_ERR "event-scan failed\n");
397 break;
398 }
399
400 if (error == 0)
401 pSeries_log_error(logdata, ERR_TYPE_RTAS_LOG, 0);
402
403 } while(error == 0);
404}
405
406static void do_event_scan_all_cpus(long delay)
407{
408 int cpu;
409
410 lock_cpu_hotplug();
411 cpu = first_cpu(cpu_online_map);
412 for (;;) {
413 set_cpus_allowed(current, cpumask_of_cpu(cpu));
Linas Vepstasa4fc3a32007-08-10 07:01:50 +1000414 do_event_scan();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 set_cpus_allowed(current, CPU_MASK_ALL);
416
417 /* Drop hotplug lock, and sleep for the specified delay */
418 unlock_cpu_hotplug();
Nishanth Aravamudan0287ebe2005-09-03 15:56:01 -0700419 msleep_interruptible(delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 lock_cpu_hotplug();
421
422 cpu = next_cpu(cpu, cpu_online_map);
423 if (cpu == NR_CPUS)
424 break;
425 }
426 unlock_cpu_hotplug();
427}
428
429static int rtasd(void *unused)
430{
431 unsigned int err_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int rc;
433
434 daemonize("rtasd");
435
Linas Vepstasa4fc3a32007-08-10 07:01:50 +1000436 if (get_eventscan_parms() == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 goto error;
438
439 rtas_log_buf = vmalloc(rtas_error_log_buffer_max*LOG_NUMBER);
440 if (!rtas_log_buf) {
441 printk(KERN_ERR "rtasd: no memory\n");
442 goto error;
443 }
444
Olof Johansson90ddfeb2006-04-12 15:28:13 -0500445 printk(KERN_DEBUG "RTAS daemon started\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Nishanth Aravamudan0287ebe2005-09-03 15:56:01 -0700447 DEBUG("will sleep for %d milliseconds\n", (30000/rtas_event_scan_rate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /* See if we have any error stored in NVRAM */
450 memset(logdata, 0, rtas_error_log_max);
451
452 rc = nvram_read_error_log(logdata, rtas_error_log_max, &err_type);
453
454 /* We can use rtas_log_buf now */
455 no_logging = 0;
456
457 if (!rc) {
458 if (err_type != ERR_FLAG_ALREADY_LOGGED) {
459 pSeries_log_error(logdata, err_type | ERR_FLAG_BOOT, 0);
460 }
461 }
462
463 /* First pass. */
Nishanth Aravamudan0287ebe2005-09-03 15:56:01 -0700464 do_event_scan_all_cpus(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if (surveillance_timeout != -1) {
467 DEBUG("enabling surveillance\n");
468 enable_surveillance(surveillance_timeout);
469 DEBUG("surveillance enabled\n");
470 }
471
472 /* Delay should be at least one second since some
473 * machines have problems if we call event-scan too
474 * quickly. */
475 for (;;)
Nishanth Aravamudan0287ebe2005-09-03 15:56:01 -0700476 do_event_scan_all_cpus(30000/rtas_event_scan_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478error:
479 /* Should delete proc entries */
480 return -EINVAL;
481}
482
483static int __init rtas_init(void)
484{
485 struct proc_dir_entry *entry;
486
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100487 if (!machine_is(pseries))
Paul Mackerras799d6042005-11-10 13:37:51 +1100488 return 0;
489
490 /* No RTAS */
Linas Vepstasa4fc3a32007-08-10 07:01:50 +1000491 event_scan = rtas_token("event-scan");
492 if (event_scan == RTAS_UNKNOWN_SERVICE) {
Olof Johansson90ddfeb2006-04-12 15:28:13 -0500493 printk(KERN_DEBUG "rtasd: no event-scan on system\n");
Anton Blanchard49c28e42006-04-02 20:18:32 +1000494 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496
497 entry = create_proc_entry("ppc64/rtas/error_log", S_IRUSR, NULL);
498 if (entry)
499 entry->proc_fops = &proc_rtas_log_operations;
500 else
501 printk(KERN_ERR "Failed to create error_log proc entry\n");
502
503 if (kernel_thread(rtasd, NULL, CLONE_FS) < 0)
504 printk(KERN_ERR "Failed to start RTAS daemon\n");
505
506 return 0;
507}
508
509static int __init surveillance_setup(char *str)
510{
511 int i;
512
513 if (get_option(&str,&i)) {
514 if (i >= 0 && i <= 255)
515 surveillance_timeout = i;
516 }
517
518 return 1;
519}
520
521static int __init rtasmsgs_setup(char *str)
522{
523 if (strcmp(str, "on") == 0)
524 full_rtas_msgs = 1;
525 else if (strcmp(str, "off") == 0)
526 full_rtas_msgs = 0;
527
528 return 1;
529}
530__initcall(rtas_init);
531__setup("surveillance=", surveillance_setup);
532__setup("rtasmsgs=", rtasmsgs_setup);