2 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
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.
9 * Communication to userspace based on kernel/printk.c
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>
24 #include <asm/uaccess.h>
28 #include <asm/nvram.h>
29 #include <asm/atomic.h>
30 #include <asm/machdep.h>
33 #define DEBUG(A...) printk(KERN_ERR A)
38 static DEFINE_SPINLOCK(rtasd_log_lock
);
40 DECLARE_WAIT_QUEUE_HEAD(rtas_log_wait
);
42 static char *rtas_log_buf
;
43 static unsigned long rtas_log_start
;
44 static unsigned long rtas_log_size
;
46 static int surveillance_timeout
= -1;
47 static unsigned int rtas_error_log_max
;
48 static unsigned int rtas_error_log_buffer_max
;
50 /* RTAS service tokens */
51 static unsigned int event_scan
;
52 static unsigned int rtas_event_scan_rate
;
54 static int full_rtas_msgs
= 0;
56 /* Stop logging to nvram after first fatal error */
57 static int logging_enabled
; /* Until we initialize everything,
58 * make sure we don't try logging
60 static int error_log_cnt
;
63 * Since we use 32 bit RTAS, the physical address of this must be below
64 * 4G or else bad things happen. Allocate this in the kernel data and
67 static unsigned char logdata
[RTAS_ERROR_LOG_MAX
];
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",
75 static char *rtas_event_type(int type
)
77 if ((type
> 0) && (type
< 11))
78 return rtas_type
[type
];
83 case RTAS_TYPE_PLATFORM
:
84 return "Platform Error";
88 return "Platform Information Event";
89 case RTAS_TYPE_DEALLOC
:
90 return "Resource Deallocation Event";
92 return "Dump Notification Event";
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.
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
111 static void printk_log_rtas(char *buf
, int len
)
117 char * str
= "RTAS event";
119 if (full_rtas_msgs
) {
120 printk(RTAS_DEBUG
"%d -------- %s begin --------\n",
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.
129 for (i
= 0; i
< len
; i
++) {
132 memset(buffer
, 0, sizeof(buffer
));
133 n
= sprintf(buffer
, "RTAS %d:", i
/perline
);
137 n
+= sprintf(buffer
+n
, " ");
139 n
+= sprintf(buffer
+n
, "%02x", (unsigned char)buf
[i
]);
141 if (j
== (perline
-1))
142 printk(KERN_DEBUG
"%s\n", buffer
);
144 if ((i
% perline
) != 0)
145 printk(KERN_DEBUG
"%s\n", buffer
);
147 printk(RTAS_DEBUG
"%d -------- %s end ----------\n",
150 struct rtas_error_log
*errlog
= (struct rtas_error_log
*)buf
;
152 printk(RTAS_DEBUG
"event: %d, Type: %s, Severity: %d\n",
153 error_log_cnt
, rtas_event_type(errlog
->type
),
158 static int log_rtas_len(char * buf
)
161 struct rtas_error_log
*err
;
163 /* rtas fixed header */
165 err
= (struct rtas_error_log
*)buf
;
166 if (err
->extended_log_length
) {
168 /* extended header */
169 len
+= err
->extended_log_length
;
172 if (rtas_error_log_max
== 0)
173 rtas_error_log_max
= rtas_get_error_log_max();
175 if (len
> rtas_error_log_max
)
176 len
= rtas_error_log_max
;
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
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.
195 void pSeries_log_error(char *buf
, unsigned int err_type
, int fatal
)
197 unsigned long offset
;
201 DEBUG("logging event\n");
205 spin_lock_irqsave(&rtasd_log_lock
, s
);
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
))
214 case ERR_TYPE_KERNEL_PANIC
:
216 spin_unlock_irqrestore(&rtasd_log_lock
, s
);
220 /* Write error to NVRAM */
221 if (logging_enabled
&& !(err_type
& ERR_FLAG_BOOT
))
222 nvram_write_error_log(buf
, len
, err_type
, error_log_cnt
);
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.
229 if ((err_type
& ERR_TYPE_MASK
) == ERR_TYPE_RTAS_LOG
)
230 printk_log_rtas(buf
, len
);
232 /* Check to see if we need to or have stopped logging */
233 if (fatal
|| !logging_enabled
) {
235 spin_unlock_irqrestore(&rtasd_log_lock
, s
);
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
);
245 /* First copy over sequence number */
246 memcpy(&rtas_log_buf
[offset
], (void *) &error_log_cnt
, sizeof(int));
248 /* Second copy over error log data */
249 offset
+= sizeof(int);
250 memcpy(&rtas_log_buf
[offset
], buf
, len
);
252 if (rtas_log_size
< LOG_NUMBER
)
257 spin_unlock_irqrestore(&rtasd_log_lock
, s
);
258 wake_up_interruptible(&rtas_log_wait
);
260 case ERR_TYPE_KERNEL_PANIC
:
262 spin_unlock_irqrestore(&rtasd_log_lock
, s
);
269 static int rtas_log_open(struct inode
* inode
, struct file
* file
)
274 static int rtas_log_release(struct inode
* inode
, struct file
* file
)
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.
283 static ssize_t
rtas_log_read(struct file
* file
, char __user
* buf
,
284 size_t count
, loff_t
*ppos
)
289 unsigned long offset
;
291 if (!buf
|| count
< rtas_error_log_buffer_max
)
294 count
= rtas_error_log_buffer_max
;
296 if (!access_ok(VERIFY_WRITE
, buf
, count
))
299 tmp
= kmalloc(count
, GFP_KERNEL
);
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 && logging_enabled
)
307 nvram_clear_error_log();
308 spin_unlock_irqrestore(&rtasd_log_lock
, s
);
311 error
= wait_event_interruptible(rtas_log_wait
, rtas_log_size
);
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
);
321 spin_unlock_irqrestore(&rtasd_log_lock
, s
);
323 error
= copy_to_user(buf
, tmp
, count
) ? -EFAULT
: count
;
329 static unsigned int rtas_log_poll(struct file
*file
, poll_table
* wait
)
331 poll_wait(file
, &rtas_log_wait
, wait
);
333 return POLLIN
| POLLRDNORM
;
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
,
344 static int enable_surveillance(int timeout
)
348 error
= rtas_set_indicator(SURVEILLANCE_TOKEN
, 0, timeout
);
353 if (error
== -EINVAL
) {
354 printk(KERN_DEBUG
"rtasd: surveillance not supported\n");
358 printk(KERN_ERR
"rtasd: could not update surveillance\n");
362 static void do_event_scan(void)
366 memset(logdata
, 0, rtas_error_log_max
);
367 error
= rtas_call(event_scan
, 4, 1, NULL
,
368 RTAS_EVENT_SCAN_ALL_EVENTS
, 0,
369 __pa(logdata
), rtas_error_log_max
);
371 printk(KERN_ERR
"event-scan failed\n");
376 pSeries_log_error(logdata
, ERR_TYPE_RTAS_LOG
, 0);
381 static void do_event_scan_all_cpus(long delay
)
386 cpu
= first_cpu(cpu_online_map
);
388 set_cpus_allowed(current
, cpumask_of_cpu(cpu
));
390 set_cpus_allowed(current
, CPU_MASK_ALL
);
392 /* Drop hotplug lock, and sleep for the specified delay */
394 msleep_interruptible(delay
);
397 cpu
= next_cpu(cpu
, cpu_online_map
);
404 static int rtasd(void *unused
)
406 unsigned int err_type
;
411 printk(KERN_DEBUG
"RTAS daemon started\n");
412 DEBUG("will sleep for %d milliseconds\n", (30000/rtas_event_scan_rate
));
414 /* See if we have any error stored in NVRAM */
415 memset(logdata
, 0, rtas_error_log_max
);
416 rc
= nvram_read_error_log(logdata
, rtas_error_log_max
,
417 &err_type
, &error_log_cnt
);
418 /* We can use rtas_log_buf now */
422 if (err_type
!= ERR_FLAG_ALREADY_LOGGED
) {
423 pSeries_log_error(logdata
, err_type
| ERR_FLAG_BOOT
, 0);
428 do_event_scan_all_cpus(1000);
430 if (surveillance_timeout
!= -1) {
431 DEBUG("enabling surveillance\n");
432 enable_surveillance(surveillance_timeout
);
433 DEBUG("surveillance enabled\n");
436 /* Delay should be at least one second since some
437 * machines have problems if we call event-scan too
440 do_event_scan_all_cpus(30000/rtas_event_scan_rate
);
445 static int __init
rtas_init(void)
447 struct proc_dir_entry
*entry
;
449 if (!machine_is(pseries
))
453 event_scan
= rtas_token("event-scan");
454 if (event_scan
== RTAS_UNKNOWN_SERVICE
) {
455 printk(KERN_DEBUG
"rtasd: no event-scan on system\n");
459 rtas_event_scan_rate
= rtas_token("rtas-event-scan-rate");
460 if (rtas_event_scan_rate
== RTAS_UNKNOWN_SERVICE
) {
461 printk(KERN_ERR
"rtasd: no rtas-event-scan-rate on system\n");
465 /* Make room for the sequence number */
466 rtas_error_log_max
= rtas_get_error_log_max();
467 rtas_error_log_buffer_max
= rtas_error_log_max
+ sizeof(int);
469 rtas_log_buf
= vmalloc(rtas_error_log_buffer_max
*LOG_NUMBER
);
471 printk(KERN_ERR
"rtasd: no memory\n");
475 entry
= create_proc_entry("ppc64/rtas/error_log", S_IRUSR
, NULL
);
477 entry
->proc_fops
= &proc_rtas_log_operations
;
479 printk(KERN_ERR
"Failed to create error_log proc entry\n");
481 if (kernel_thread(rtasd
, NULL
, CLONE_FS
) < 0)
482 printk(KERN_ERR
"Failed to start RTAS daemon\n");
487 static int __init
surveillance_setup(char *str
)
491 if (get_option(&str
,&i
)) {
492 if (i
>= 0 && i
<= 255)
493 surveillance_timeout
= i
;
499 static int __init
rtasmsgs_setup(char *str
)
501 if (strcmp(str
, "on") == 0)
503 else if (strcmp(str
, "off") == 0)
508 __initcall(rtas_init
);
509 __setup("surveillance=", surveillance_setup
);
510 __setup("rtasmsgs=", rtasmsgs_setup
);