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>
23 #include <asm/uaccess.h>
27 #include <asm/nvram.h>
28 #include <asm/atomic.h>
29 #include <asm/proc_fs.h>
32 #define DEBUG(A...) printk(KERN_ERR A)
37 static spinlock_t log_lock = SPIN_LOCK_UNLOCKED;
39 DECLARE_WAIT_QUEUE_HEAD(rtas_log_wait);
41 static char *rtas_log_buf;
42 static unsigned long rtas_log_start;
43 static unsigned long rtas_log_size;
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;
50 extern spinlock_t proc_ppc64_lock;
51 extern volatile int no_more_logging;
53 volatile int error_log_cnt = 0;
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
60 static unsigned char logdata[RTAS_ERROR_LOG_MAX];
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.
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
75 static void printk_log_rtas(char *buf, int len)
81 char * str = "RTAS event";
83 printk(RTAS_ERR "%d -------- %s begin --------\n", error_log_cnt, str);
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.
91 for (i=0; i < len; i++) {
94 memset(buffer, 0, sizeof(buffer));
95 n = sprintf(buffer, "RTAS %d:", i/perline);
99 n += sprintf(buffer+n, " ");
101 n += sprintf(buffer+n, "%02x", (unsigned char)buf[i]);
103 if (j == (perline-1))
104 printk(KERN_ERR "%s\n", buffer);
106 if ((i % perline) != 0)
107 printk(KERN_ERR "%s\n", buffer);
109 printk(RTAS_ERR "%d -------- %s end ----------\n", error_log_cnt, str);
112 static int log_rtas_len(char * buf)
115 struct rtas_error_log *err;
117 /* rtas fixed header */
119 err = (struct rtas_error_log *)buf;
120 if (err->extended_log_length) {
122 /* extended header */
123 len += err->extended_log_length;
125 if (len > rtas_error_log_max)
126 len = rtas_error_log_max;
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
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.
145 void pSeries_log_error(char *buf, unsigned int err_type, int fatal)
147 unsigned long offset;
151 DEBUG("logging event\n");
156 spin_lock_irqsave(&log_lock, s);
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))
165 case ERR_TYPE_KERNEL_PANIC:
167 spin_unlock_irqrestore(&log_lock, s);
171 /* Write error to NVRAM */
172 if (!no_more_logging && !(err_type & ERR_FLAG_BOOT))
173 nvram_write_error_log(buf, len, err_type);
175 /* Check to see if we need to or have stopped logging */
176 if (fatal || no_more_logging) {
178 spin_unlock_irqrestore(&log_lock, s);
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);
188 offset = rtas_error_log_buffer_max *
189 ((rtas_log_start+rtas_log_size) & LOG_NUMBER_MASK);
191 /* First copy over sequence number */
192 memcpy(&rtas_log_buf[offset], (void *) &error_log_cnt, sizeof(int));
194 /* Second copy over error log data */
195 offset += sizeof(int);
196 memcpy(&rtas_log_buf[offset], buf, len);
198 if (rtas_log_size < LOG_NUMBER)
203 spin_unlock_irqrestore(&log_lock, s);
204 wake_up_interruptible(&rtas_log_wait);
206 case ERR_TYPE_KERNEL_PANIC:
208 spin_unlock_irqrestore(&log_lock, s);
215 static int rtas_log_open(struct inode * inode, struct file * file)
220 static int rtas_log_release(struct inode * inode, struct file * file)
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.
229 static ssize_t rtas_log_read(struct file * file, char * buf,
230 size_t count, loff_t *ppos)
235 unsigned long offset;
237 if (!buf || count < rtas_error_log_buffer_max)
240 count = rtas_error_log_buffer_max;
242 error = verify_area(VERIFY_WRITE, buf, count);
246 tmp = kmalloc(count, GFP_KERNEL);
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);
258 error = wait_event_interruptible(rtas_log_wait, rtas_log_size);
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);
268 spin_unlock_irqrestore(&log_lock, s);
270 error = copy_to_user(buf, tmp, count) ? -EFAULT : count;
276 static unsigned int rtas_log_poll(struct file *file, poll_table * wait)
278 poll_wait(file, &rtas_log_wait, wait);
280 return POLLIN | POLLRDNORM;
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,
291 static int enable_surveillance(int timeout)
295 error = rtas_call(rtas_token("set-indicator"), 3, 1, NULL,
296 SURVEILLANCE_TOKEN, 0, timeout);
299 printk(KERN_ERR "rtasd: could not enable surveillance\n");
306 static int get_eventscan_parms(void)
308 struct device_node *node;
311 node = of_find_node_by_path("/rtas");
313 ip = (int *)get_property(node, "rtas-event-scan-rate", NULL);
315 printk(KERN_ERR "rtasd: no rtas-event-scan-rate\n");
319 rtas_event_scan_rate = *ip;
320 DEBUG("rtas-event-scan-rate %d\n", rtas_event_scan_rate);
322 ip = (int *)get_property(node, "rtas-error-log-max", NULL);
324 printk(KERN_ERR "rtasd: no rtas-error-log-max\n");
328 rtas_error_log_max = *ip;
329 DEBUG("rtas-error-log-max %d\n", rtas_error_log_max);
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;
340 static void do_event_scan(int event_scan)
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);
349 printk(KERN_ERR "event-scan failed\n");
354 pSeries_log_error(logdata, ERR_TYPE_RTAS_LOG, 0);
359 static int rtasd(void *unused)
361 unsigned int err_type;
363 int event_scan = rtas_token("event-scan");
364 cpumask_t all = CPU_MASK_ALL;
369 if (event_scan == RTAS_UNKNOWN_SERVICE || get_eventscan_parms() == -1)
372 rtas_log_buf = vmalloc(rtas_error_log_max*LOG_NUMBER);
374 printk(KERN_ERR "rtasd: no memory\n");
378 /* We can use rtas_log_buf now */
381 printk(KERN_ERR "RTAS daemon started\n");
383 DEBUG("will sleep for %d jiffies\n", (HZ*60/rtas_event_scan_rate) / 2);
385 /* See if we have any error stored in NVRAM */
386 memset(logdata, 0, rtas_error_log_max);
388 rc = nvram_read_error_log(logdata, rtas_error_log_max, &err_type);
390 if (err_type != ERR_FLAG_ALREADY_LOGGED) {
391 pSeries_log_error(logdata, err_type | ERR_FLAG_BOOT, 0);
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());
402 do_event_scan(event_scan);
403 set_current_state(TASK_INTERRUPTIBLE);
404 schedule_timeout(HZ);
406 unlock_cpu_hotplug();
408 if (surveillance_timeout != -1) {
409 DEBUG("enabling surveillance\n");
410 enable_surveillance(surveillance_timeout);
411 DEBUG("surveillance enabled\n");
415 cpu = first_cpu_const(mk_cpumask_const(cpu_online_map));
417 set_cpus_allowed(current, cpumask_of_cpu(cpu));
418 do_event_scan(event_scan);
419 set_cpus_allowed(current, all);
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);
429 cpu = next_cpu_const(cpu, mk_cpumask_const(cpu_online_map));
431 cpu = first_cpu_const(mk_cpumask_const(cpu_online_map));
435 /* Should delete proc entries */
439 static int __init rtas_init(void)
441 struct proc_dir_entry *entry;
443 if (proc_ppc64.rtas == NULL) {
447 if (proc_ppc64.rtas == NULL) {
448 printk(KERN_ERR "rtas_init: /proc/ppc64/rtas does not exist.");
452 entry = create_proc_entry("error_log", S_IRUSR, proc_ppc64.rtas);
454 entry->proc_fops = &proc_rtas_log_operations;
456 printk(KERN_ERR "Failed to create rtas/error_log proc entry\n");
458 if (kernel_thread(rtasd, 0, CLONE_FS) < 0)
459 printk(KERN_ERR "Failed to start RTAS daemon\n");
461 /* Make room for the sequence number */
462 rtas_error_log_buffer_max = rtas_error_log_max + sizeof(int);
467 static int __init surveillance_setup(char *str)
471 if (get_option(&str,&i)) {
472 if (i >= 0 && i <= 255)
473 surveillance_timeout = i;
479 __initcall(rtas_init);
480 __setup("surveillance=", surveillance_setup);