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