kbuild: Fix instrumentation removal breakage on avr32
[wrt350n-kernel.git] / arch / powerpc / platforms / pseries / rtasd.c
blobe3078ce41518aec0abb0579a5c2f9cb21315dd44
1 /*
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>
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>
32 #if 0
33 #define DEBUG(A...) printk(KERN_ERR A)
34 #else
35 #define DEBUG(A...)
36 #endif
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
59 * anything */
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
65 * make it big enough.
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];
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";
95 return rtas_type[0];
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)
114 int i,j,n = 0;
115 int perline = 16;
116 char buffer[64];
117 char * str = "RTAS event";
119 if (full_rtas_msgs) {
120 printk(RTAS_DEBUG "%d -------- %s begin --------\n",
121 error_log_cnt, str);
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++) {
130 j = i % perline;
131 if (j == 0) {
132 memset(buffer, 0, sizeof(buffer));
133 n = sprintf(buffer, "RTAS %d:", i/perline);
136 if ((i % 4) == 0)
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",
148 error_log_cnt, str);
149 } else {
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),
154 errlog->severity);
158 static int log_rtas_len(char * buf)
160 int len;
161 struct rtas_error_log *err;
163 /* rtas fixed header */
164 len = 8;
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;
178 return len;
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().
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;
198 unsigned long s;
199 int len = 0;
201 DEBUG("logging event\n");
202 if (buf == NULL)
203 return;
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))
212 error_log_cnt++;
213 break;
214 case ERR_TYPE_KERNEL_PANIC:
215 default:
216 spin_unlock_irqrestore(&rtasd_log_lock, s);
217 return;
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) {
234 logging_enabled = 0;
235 spin_unlock_irqrestore(&rtasd_log_lock, s);
236 return;
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)
253 rtas_log_size += 1;
254 else
255 rtas_log_start += 1;
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;
269 static int rtas_log_open(struct inode * inode, struct file * file)
271 return 0;
274 static int rtas_log_release(struct inode * inode, struct file * file)
276 return 0;
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)
286 int error;
287 char *tmp;
288 unsigned long s;
289 unsigned long offset;
291 if (!buf || count < rtas_error_log_buffer_max)
292 return -EINVAL;
294 count = rtas_error_log_buffer_max;
296 if (!access_ok(VERIFY_WRITE, buf, count))
297 return -EFAULT;
299 tmp = kmalloc(count, GFP_KERNEL);
300 if (!tmp)
301 return -ENOMEM;
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);
312 if (error)
313 goto out;
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);
319 rtas_log_start += 1;
320 rtas_log_size -= 1;
321 spin_unlock_irqrestore(&rtasd_log_lock, s);
323 error = copy_to_user(buf, tmp, count) ? -EFAULT : count;
324 out:
325 kfree(tmp);
326 return error;
329 static unsigned int rtas_log_poll(struct file *file, poll_table * wait)
331 poll_wait(file, &rtas_log_wait, wait);
332 if (rtas_log_size)
333 return POLLIN | POLLRDNORM;
334 return 0;
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)
346 int error;
348 error = rtas_set_indicator(SURVEILLANCE_TOKEN, 0, timeout);
350 if (error == 0)
351 return 0;
353 if (error == -EINVAL) {
354 printk(KERN_DEBUG "rtasd: surveillance not supported\n");
355 return 0;
358 printk(KERN_ERR "rtasd: could not update surveillance\n");
359 return -1;
362 static void do_event_scan(void)
364 int error;
365 do {
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);
370 if (error == -1) {
371 printk(KERN_ERR "event-scan failed\n");
372 break;
375 if (error == 0)
376 pSeries_log_error(logdata, ERR_TYPE_RTAS_LOG, 0);
378 } while(error == 0);
381 static void do_event_scan_all_cpus(long delay)
383 int cpu;
385 get_online_cpus();
386 cpu = first_cpu(cpu_online_map);
387 for (;;) {
388 set_cpus_allowed(current, cpumask_of_cpu(cpu));
389 do_event_scan();
390 set_cpus_allowed(current, CPU_MASK_ALL);
392 /* Drop hotplug lock, and sleep for the specified delay */
393 put_online_cpus();
394 msleep_interruptible(delay);
395 get_online_cpus();
397 cpu = next_cpu(cpu, cpu_online_map);
398 if (cpu == NR_CPUS)
399 break;
401 put_online_cpus();
404 static int rtasd(void *unused)
406 unsigned int err_type;
407 int rc;
409 daemonize("rtasd");
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 */
419 logging_enabled = 1;
421 if (!rc) {
422 if (err_type != ERR_FLAG_ALREADY_LOGGED) {
423 pSeries_log_error(logdata, err_type | ERR_FLAG_BOOT, 0);
427 /* First pass. */
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
438 * quickly. */
439 for (;;)
440 do_event_scan_all_cpus(30000/rtas_event_scan_rate);
442 return -EINVAL;
445 static int __init rtas_init(void)
447 struct proc_dir_entry *entry;
449 if (!machine_is(pseries))
450 return 0;
452 /* No RTAS */
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");
456 return -ENODEV;
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");
462 return -ENODEV;
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);
470 if (!rtas_log_buf) {
471 printk(KERN_ERR "rtasd: no memory\n");
472 return -ENOMEM;
475 entry = create_proc_entry("ppc64/rtas/error_log", S_IRUSR, NULL);
476 if (entry)
477 entry->proc_fops = &proc_rtas_log_operations;
478 else
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");
484 return 0;
487 static int __init surveillance_setup(char *str)
489 int i;
491 if (get_option(&str,&i)) {
492 if (i >= 0 && i <= 255)
493 surveillance_timeout = i;
496 return 1;
499 static int __init rtasmsgs_setup(char *str)
501 if (strcmp(str, "on") == 0)
502 full_rtas_msgs = 1;
503 else if (strcmp(str, "off") == 0)
504 full_rtas_msgs = 0;
506 return 1;
508 __initcall(rtas_init);
509 __setup("surveillance=", surveillance_setup);
510 __setup("rtasmsgs=", rtasmsgs_setup);