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