rtasd.c 12.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 * Communication to userspace based on kernel/printk.c
 */

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <linux/vmalloc.h>
20
#include <linux/spinlock.h>
21
#include <linux/cpu.h>
22 23 24 25 26

#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/rtas.h>
#include <asm/prom.h>
27 28
#include <asm/nvram.h>
#include <asm/atomic.h>
29 30 31 32 33 34 35

#if 0
#define DEBUG(A...)	printk(KERN_ERR A)
#else
#define DEBUG(A...)
#endif

36
static spinlock_t rtasd_log_lock = SPIN_LOCK_UNLOCKED;
37 38 39 40 41 42 43

DECLARE_WAIT_QUEUE_HEAD(rtas_log_wait);

static char *rtas_log_buf;
static unsigned long rtas_log_start;
static unsigned long rtas_log_size;

44
static int surveillance_timeout = -1;
45 46
static unsigned int rtas_event_scan_rate;
static unsigned int rtas_error_log_max;
47
static unsigned int rtas_error_log_buffer_max;
48

49 50
static int full_rtas_msgs = 0;

51
extern int no_logging;
52

53
volatile int error_log_cnt = 0;
54

55 56 57 58 59 60 61
/*
 * Since we use 32 bit RTAS, the physical address of this must be below
 * 4G or else bad things happen. Allocate this in the kernel data and
 * make it big enough.
 */
static unsigned char logdata[RTAS_ERROR_LOG_MAX];

62 63
static int get_eventscan_parms(void);

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
static char *rtas_type[] = {
	"Unknown", "Retry", "TCE Error", "Internal Device Failure",
	"Timeout", "Data Parity", "Address Parity", "Cache Parity",
	"Address Invalid", "ECC Uncorrected", "ECC Corrupted",
};

static char *rtas_event_type(int type)
{
	if ((type > 0) && (type < 11))
		return rtas_type[type];

	switch (type) {
		case RTAS_TYPE_EPOW:
			return "EPOW";
		case RTAS_TYPE_PLATFORM:
			return "Platform Error";
		case RTAS_TYPE_IO:
			return "I/O Event";
		case RTAS_TYPE_INFO:
			return "Platform Information Event";
		case RTAS_TYPE_DEALLOC:
			return "Resource Deallocation Event";
		case RTAS_TYPE_DUMP:
			return "Dump Notification Event";
	}

	return rtas_type[0];
}

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
/* To see this info, grep RTAS /var/log/messages and each entry
 * will be collected together with obvious begin/end.
 * There will be a unique identifier on the begin and end lines.
 * This will persist across reboots.
 *
 * format of error logs returned from RTAS:
 * bytes	(size)	: contents
 * --------------------------------------------------------
 * 0-7		(8)	: rtas_error_log
 * 8-47		(40)	: extended info
 * 48-51	(4)	: vendor id
 * 52-1023 (vendor specific) : location code and debug data
 */
static void printk_log_rtas(char *buf, int len)
{

109
	int i,j,n = 0;
110 111 112 113
	int perline = 16;
	char buffer[64];
	char * str = "RTAS event";

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	if (full_rtas_msgs) {
		printk(RTAS_DEBUG "%d -------- %s begin --------\n",
		       error_log_cnt, str);

		/*
		 * Print perline bytes on each line, each line will start
		 * with RTAS and a changing number, so syslogd will
		 * print lines that are otherwise the same.  Separate every
		 * 4 bytes with a space.
		 */
		for (i = 0; i < len; i++) {
			j = i % perline;
			if (j == 0) {
				memset(buffer, 0, sizeof(buffer));
				n = sprintf(buffer, "RTAS %d:", i/perline);
			}

			if ((i % 4) == 0)
				n += sprintf(buffer+n, " ");

			n += sprintf(buffer+n, "%02x", (unsigned char)buf[i]);

			if (j == (perline-1))
				printk(KERN_DEBUG "%s\n", buffer);
138
		}
139 140
		if ((i % perline) != 0)
			printk(KERN_DEBUG "%s\n", buffer);
141

142 143 144 145
		printk(RTAS_DEBUG "%d -------- %s end ----------\n",
		       error_log_cnt, str);
	} else {
		struct rtas_error_log *errlog = (struct rtas_error_log *)buf;
146

147 148 149
		printk(RTAS_DEBUG "event: %d, Type: %s, Severity: %d\n",
		       error_log_cnt, rtas_event_type(errlog->type),
		       errlog->severity);
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	}
}

static int log_rtas_len(char * buf)
{
	int len;
	struct rtas_error_log *err;

	/* rtas fixed header */
	len = 8;
	err = (struct rtas_error_log *)buf;
	if (err->extended_log_length) {

		/* extended header */
		len += err->extended_log_length;
	}
166

167 168 169
	if (rtas_error_log_max == 0) {
		get_eventscan_parms();
	}
170 171 172
	if (len > rtas_error_log_max)
		len = rtas_error_log_max;

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	return len;
}

/*
 * First write to nvram, if fatal error, that is the only
 * place we log the info.  The error will be picked up
 * on the next reboot by rtasd.  If not fatal, run the
 * method for the type of error.  Currently, only RTAS
 * errors have methods implemented, but in the future
 * there might be a need to store data in nvram before a
 * call to panic().
 *
 * XXX We write to nvram periodically, to indicate error has
 * been written and sync'd, but there is a possibility
 * that if we don't shutdown correctly, a duplicate error
 * record will be created on next reboot.
 */
void pSeries_log_error(char *buf, unsigned int err_type, int fatal)
{
	unsigned long offset;
	unsigned long s;
	int len = 0;

	DEBUG("logging event\n");
	if (buf == NULL)
		return;

200
	spin_lock_irqsave(&rtasd_log_lock, s);
201 202 203 204 205 206 207 208 209 210

	/* get length and increase count */
	switch (err_type & ERR_TYPE_MASK) {
	case ERR_TYPE_RTAS_LOG:
		len = log_rtas_len(buf);
		if (!(err_type & ERR_FLAG_BOOT))
			error_log_cnt++;
		break;
	case ERR_TYPE_KERNEL_PANIC:
	default:
211
		spin_unlock_irqrestore(&rtasd_log_lock, s);
212 213 214 215
		return;
	}

	/* Write error to NVRAM */
216
	if (!no_logging && !(err_type & ERR_FLAG_BOOT))
217 218
		nvram_write_error_log(buf, len, err_type);

219 220
	/*
	 * rtas errors can occur during boot, and we do want to capture
221
	 * those somewhere, even if nvram isn't ready (why not?), and even
222 223 224
	 * if rtasd isn't ready. Put them into the boot log, at least.
	 */
	if ((err_type & ERR_TYPE_MASK) == ERR_TYPE_RTAS_LOG)
225 226
		printk_log_rtas(buf, len);

227
	/* Check to see if we need to or have stopped logging */
228 229
	if (fatal || no_logging) {
		no_logging = 1;
230
		spin_unlock_irqrestore(&rtasd_log_lock, s);
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
		return;
	}

	/* call type specific method for error */
	switch (err_type & ERR_TYPE_MASK) {
	case ERR_TYPE_RTAS_LOG:
		offset = rtas_error_log_buffer_max *
			((rtas_log_start+rtas_log_size) & LOG_NUMBER_MASK);

		/* First copy over sequence number */
		memcpy(&rtas_log_buf[offset], (void *) &error_log_cnt, sizeof(int));

		/* Second copy over error log data */
		offset += sizeof(int);
		memcpy(&rtas_log_buf[offset], buf, len);

		if (rtas_log_size < LOG_NUMBER)
			rtas_log_size += 1;
		else
			rtas_log_start += 1;

252
		spin_unlock_irqrestore(&rtasd_log_lock, s);
253 254 255 256
		wake_up_interruptible(&rtas_log_wait);
		break;
	case ERR_TYPE_KERNEL_PANIC:
	default:
257
		spin_unlock_irqrestore(&rtasd_log_lock, s);
258 259 260 261 262 263
		return;
	}

}


264 265 266 267 268 269 270 271 272 273
static int rtas_log_open(struct inode * inode, struct file * file)
{
	return 0;
}

static int rtas_log_release(struct inode * inode, struct file * file)
{
	return 0;
}

274 275 276 277
/* This will check if all events are logged, if they are then, we
 * know that we can safely clear the events in NVRAM.
 * Next we'll sit and wait for something else to log.
 */
278
static ssize_t rtas_log_read(struct file * file, char __user * buf,
279 280 281 282
			 size_t count, loff_t *ppos)
{
	int error;
	char *tmp;
283
	unsigned long s;
284 285
	unsigned long offset;

286
	if (!buf || count < rtas_error_log_buffer_max)
287 288
		return -EINVAL;

289
	count = rtas_error_log_buffer_max;
290 291 292

	error = verify_area(VERIFY_WRITE, buf, count);
	if (error)
293
		return -EFAULT;
294

295
	tmp = kmalloc(count, GFP_KERNEL);
296 297 298
	if (!tmp)
		return -ENOMEM;

299

300
	spin_lock_irqsave(&rtasd_log_lock, s);
301
	/* if it's 0, then we know we got the last one (the one in NVRAM) */
302
	if (rtas_log_size == 0 && !no_logging)
303
		nvram_clear_error_log();
304
	spin_unlock_irqrestore(&rtasd_log_lock, s);
305 306


307 308 309 310
	error = wait_event_interruptible(rtas_log_wait, rtas_log_size);
	if (error)
		goto out;

311
	spin_lock_irqsave(&rtasd_log_lock, s);
312
	offset = rtas_error_log_buffer_max * (rtas_log_start & LOG_NUMBER_MASK);
313
	memcpy(tmp, &rtas_log_buf[offset], count);
314

315 316
	rtas_log_start += 1;
	rtas_log_size -= 1;
317
	spin_unlock_irqrestore(&rtasd_log_lock, s);
318

Arnaldo Carvalho de Melo's avatar
Arnaldo Carvalho de Melo committed
319
	error = copy_to_user(buf, tmp, count) ? -EFAULT : count;
320 321 322 323 324 325 326 327 328 329 330 331 332 333
out:
	kfree(tmp);
	return error;
}

static unsigned int rtas_log_poll(struct file *file, poll_table * wait)
{
	poll_wait(file, &rtas_log_wait, wait);
	if (rtas_log_size)
		return POLLIN | POLLRDNORM;
	return 0;
}

struct file_operations proc_rtas_log_operations = {
334 335 336 337
	.read =		rtas_log_read,
	.poll =		rtas_log_poll,
	.open =		rtas_log_open,
	.release =	rtas_log_release,
338 339
};

340
static int enable_surveillance(int timeout)
341 342 343
{
	int error;

344
	error = rtas_set_indicator(SURVEILLANCE_TOKEN, 0, timeout);
345

346 347 348 349 350 351
	if (error == 0)
		return 0;

	if (error == RTAS_NO_SUCH_INDICATOR) {
		printk(KERN_INFO "rtasd: surveillance not supported\n");
		return 0;
352 353
	}

354 355
	printk(KERN_ERR "rtasd: could not update surveillance\n");
	return -1;
356 357 358 359 360 361 362
}

static int get_eventscan_parms(void)
{
	struct device_node *node;
	int *ip;

363
	node = of_find_node_by_path("/rtas");
364 365 366 367

	ip = (int *)get_property(node, "rtas-event-scan-rate", NULL);
	if (ip == NULL) {
		printk(KERN_ERR "rtasd: no rtas-event-scan-rate\n");
368
		of_node_put(node);
369 370 371 372 373
		return -1;
	}
	rtas_event_scan_rate = *ip;
	DEBUG("rtas-event-scan-rate %d\n", rtas_event_scan_rate);

374
	/* Make room for the sequence number */
375
	rtas_error_log_max = rtas_get_error_log_max();
376 377
	rtas_error_log_buffer_max = rtas_error_log_max + sizeof(int);

378
	of_node_put(node);
379 380 381 382

	return 0;
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
static void do_event_scan(int event_scan)
{
	int error;
	do {
		memset(logdata, 0, rtas_error_log_max);
		error = rtas_call(event_scan, 4, 1, NULL,
				  RTAS_EVENT_SCAN_ALL_EVENTS, 0,
				  __pa(logdata), rtas_error_log_max);
		if (error == -1) {
			printk(KERN_ERR "event-scan failed\n");
			break;
		}

		if (error == 0)
			pSeries_log_error(logdata, ERR_TYPE_RTAS_LOG, 0);

	} while(error == 0);
}
401 402 403

static int rtasd(void *unused)
{
404
	unsigned int err_type;
405 406
	int cpu = 0;
	int event_scan = rtas_token("event-scan");
407
	int rc;
408

409 410
	daemonize("rtasd");

411 412 413
	if (event_scan == RTAS_UNKNOWN_SERVICE || get_eventscan_parms() == -1)
		goto error;

414
	rtas_log_buf = vmalloc(rtas_error_log_buffer_max*LOG_NUMBER);
415 416 417 418 419
	if (!rtas_log_buf) {
		printk(KERN_ERR "rtasd: no memory\n");
		goto error;
	}

420
	printk(KERN_ERR "RTAS daemon started\n");
421

422
	DEBUG("will sleep for %d jiffies\n", (HZ*60/rtas_event_scan_rate) / 2);
423

424 425 426 427
	/* See if we have any error stored in NVRAM */
	memset(logdata, 0, rtas_error_log_max);

	rc = nvram_read_error_log(logdata, rtas_error_log_max, &err_type);
428 429 430 431

	/* We can use rtas_log_buf now */
	no_logging = 0;

432 433 434 435 436 437
	if (!rc) {
		if (err_type != ERR_FLAG_ALREADY_LOGGED) {
			pSeries_log_error(logdata, err_type | ERR_FLAG_BOOT, 0);
		}
	}

438 439 440
	/* First pass. */
	lock_cpu_hotplug();
	for_each_online_cpu(cpu) {
441
		DEBUG("scheduling on %d\n", cpu);
442
		set_cpus_allowed(current, cpumask_of_cpu(cpu));
443
		DEBUG("watchdog scheduled on cpu %d\n", smp_processor_id());
444

445
		do_event_scan(event_scan);
446
		set_current_state(TASK_INTERRUPTIBLE);
447
		schedule_timeout(HZ);
448
	}
449
	unlock_cpu_hotplug();
450

451
	if (surveillance_timeout != -1) {
452
		DEBUG("enabling surveillance\n");
453
		enable_surveillance(surveillance_timeout);
454 455 456
		DEBUG("surveillance enabled\n");
	}

457
	lock_cpu_hotplug();
458
	cpu = first_cpu(cpu_online_map);
459 460 461
	for (;;) {
		set_cpus_allowed(current, cpumask_of_cpu(cpu));
		do_event_scan(event_scan);
462
		set_cpus_allowed(current, CPU_MASK_ALL);
463 464 465 466 467 468 469 470 471

		/* Drop hotplug lock, and sleep for a bit (at least
		 * one second since some machines have problems if we
		 * call event-scan too quickly). */
		unlock_cpu_hotplug();
		set_current_state(TASK_INTERRUPTIBLE);
		schedule_timeout((HZ*60/rtas_event_scan_rate) / 2);
		lock_cpu_hotplug();

472
		cpu = next_cpu(cpu, cpu_online_map);
473
		if (cpu == NR_CPUS)
474
			cpu = first_cpu(cpu_online_map);
475
	}
476

477 478 479 480 481
error:
	/* Should delete proc entries */
	return -EINVAL;
}

482
static int __init rtas_init(void)
483
{
484 485
	struct proc_dir_entry *entry;

486 487 488 489 490
	/* No RTAS, only warn if we are on a pSeries box  */
	if (rtas_token("event-scan") == RTAS_UNKNOWN_SERVICE) {
		if (systemcfg->platform & PLATFORM_PSERIES);
			printk(KERN_ERR "rtasd: no RTAS on system\n");
		return 1;
491
	}
492

493
	entry = create_proc_entry("ppc64/rtas/error_log", S_IRUSR, NULL);
494 495 496
	if (entry)
		entry->proc_fops = &proc_rtas_log_operations;
	else
497
		printk(KERN_ERR "Failed to create error_log proc entry\n");
498

499
	if (kernel_thread(rtasd, NULL, CLONE_FS) < 0)
500 501
		printk(KERN_ERR "Failed to start RTAS daemon\n");

502
	return 0;
503 504 505 506 507 508 509
}

static int __init surveillance_setup(char *str)
{
	int i;

	if (get_option(&str,&i)) {
510 511
		if (i >= 0 && i <= 255)
			surveillance_timeout = i;
512 513 514 515 516
	}

	return 1;
}

517 518 519 520 521 522 523 524 525
static int __init rtasmsgs_setup(char *str)
{
	if (strcmp(str, "on") == 0)
		full_rtas_msgs = 1;
	else if (strcmp(str, "off") == 0)
		full_rtas_msgs = 0;

	return 1;
}
526 527
__initcall(rtas_init);
__setup("surveillance=", surveillance_setup);
528
__setup("rtasmsgs=", rtasmsgs_setup);