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