xref: /openbmc/linux/arch/powerpc/kernel/rtasd.c (revision 4c5d87db497832c493ed296157bd1749dddc69f1)
13d541c4bSBenjamin Herrenschmidt /*
23d541c4bSBenjamin Herrenschmidt  * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
33d541c4bSBenjamin Herrenschmidt  *
43d541c4bSBenjamin Herrenschmidt  * This program is free software; you can redistribute it and/or
53d541c4bSBenjamin Herrenschmidt  * modify it under the terms of the GNU General Public License
63d541c4bSBenjamin Herrenschmidt  * as published by the Free Software Foundation; either version
73d541c4bSBenjamin Herrenschmidt  * 2 of the License, or (at your option) any later version.
83d541c4bSBenjamin Herrenschmidt  *
93d541c4bSBenjamin Herrenschmidt  * Communication to userspace based on kernel/printk.c
103d541c4bSBenjamin Herrenschmidt  */
113d541c4bSBenjamin Herrenschmidt 
123d541c4bSBenjamin Herrenschmidt #include <linux/types.h>
133d541c4bSBenjamin Herrenschmidt #include <linux/errno.h>
143d541c4bSBenjamin Herrenschmidt #include <linux/sched.h>
153d541c4bSBenjamin Herrenschmidt #include <linux/kernel.h>
163d541c4bSBenjamin Herrenschmidt #include <linux/poll.h>
173d541c4bSBenjamin Herrenschmidt #include <linux/proc_fs.h>
183d541c4bSBenjamin Herrenschmidt #include <linux/init.h>
193d541c4bSBenjamin Herrenschmidt #include <linux/vmalloc.h>
203d541c4bSBenjamin Herrenschmidt #include <linux/spinlock.h>
213d541c4bSBenjamin Herrenschmidt #include <linux/cpu.h>
223d541c4bSBenjamin Herrenschmidt #include <linux/workqueue.h>
235a0e3ad6STejun Heo #include <linux/slab.h>
24675d8ee6SJohn Allen #include <linux/topology.h>
253d541c4bSBenjamin Herrenschmidt 
267c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
273d541c4bSBenjamin Herrenschmidt #include <asm/io.h>
283d541c4bSBenjamin Herrenschmidt #include <asm/rtas.h>
293d541c4bSBenjamin Herrenschmidt #include <asm/prom.h>
303d541c4bSBenjamin Herrenschmidt #include <asm/nvram.h>
3160063497SArun Sharma #include <linux/atomic.h>
323d541c4bSBenjamin Herrenschmidt #include <asm/machdep.h>
33e04fa612SNathan Fontenot #include <asm/topology.h>
343d541c4bSBenjamin Herrenschmidt 
353d541c4bSBenjamin Herrenschmidt 
363d541c4bSBenjamin Herrenschmidt static DEFINE_SPINLOCK(rtasd_log_lock);
373d541c4bSBenjamin Herrenschmidt 
383d541c4bSBenjamin Herrenschmidt static DECLARE_WAIT_QUEUE_HEAD(rtas_log_wait);
393d541c4bSBenjamin Herrenschmidt 
403d541c4bSBenjamin Herrenschmidt static char *rtas_log_buf;
413d541c4bSBenjamin Herrenschmidt static unsigned long rtas_log_start;
423d541c4bSBenjamin Herrenschmidt static unsigned long rtas_log_size;
433d541c4bSBenjamin Herrenschmidt 
443d541c4bSBenjamin Herrenschmidt static int surveillance_timeout = -1;
453d541c4bSBenjamin Herrenschmidt 
463d541c4bSBenjamin Herrenschmidt static unsigned int rtas_error_log_max;
473d541c4bSBenjamin Herrenschmidt static unsigned int rtas_error_log_buffer_max;
483d541c4bSBenjamin Herrenschmidt 
493d541c4bSBenjamin Herrenschmidt /* RTAS service tokens */
503d541c4bSBenjamin Herrenschmidt static unsigned int event_scan;
513d541c4bSBenjamin Herrenschmidt static unsigned int rtas_event_scan_rate;
523d541c4bSBenjamin Herrenschmidt 
534cc7ecb7SKees Cook static bool full_rtas_msgs;
543d541c4bSBenjamin Herrenschmidt 
553d541c4bSBenjamin Herrenschmidt /* Stop logging to nvram after first fatal error */
563d541c4bSBenjamin Herrenschmidt static int logging_enabled; /* Until we initialize everything,
573d541c4bSBenjamin Herrenschmidt                              * make sure we don't try logging
583d541c4bSBenjamin Herrenschmidt                              * anything */
593d541c4bSBenjamin Herrenschmidt static int error_log_cnt;
603d541c4bSBenjamin Herrenschmidt 
613d541c4bSBenjamin Herrenschmidt /*
623d541c4bSBenjamin Herrenschmidt  * Since we use 32 bit RTAS, the physical address of this must be below
633d541c4bSBenjamin Herrenschmidt  * 4G or else bad things happen. Allocate this in the kernel data and
643d541c4bSBenjamin Herrenschmidt  * make it big enough.
653d541c4bSBenjamin Herrenschmidt  */
663d541c4bSBenjamin Herrenschmidt static unsigned char logdata[RTAS_ERROR_LOG_MAX];
673d541c4bSBenjamin Herrenschmidt 
683d541c4bSBenjamin Herrenschmidt static char *rtas_type[] = {
693d541c4bSBenjamin Herrenschmidt 	"Unknown", "Retry", "TCE Error", "Internal Device Failure",
703d541c4bSBenjamin Herrenschmidt 	"Timeout", "Data Parity", "Address Parity", "Cache Parity",
713d541c4bSBenjamin Herrenschmidt 	"Address Invalid", "ECC Uncorrected", "ECC Corrupted",
723d541c4bSBenjamin Herrenschmidt };
733d541c4bSBenjamin Herrenschmidt 
743d541c4bSBenjamin Herrenschmidt static char *rtas_event_type(int type)
753d541c4bSBenjamin Herrenschmidt {
763d541c4bSBenjamin Herrenschmidt 	if ((type > 0) && (type < 11))
773d541c4bSBenjamin Herrenschmidt 		return rtas_type[type];
783d541c4bSBenjamin Herrenschmidt 
793d541c4bSBenjamin Herrenschmidt 	switch (type) {
803d541c4bSBenjamin Herrenschmidt 		case RTAS_TYPE_EPOW:
813d541c4bSBenjamin Herrenschmidt 			return "EPOW";
823d541c4bSBenjamin Herrenschmidt 		case RTAS_TYPE_PLATFORM:
833d541c4bSBenjamin Herrenschmidt 			return "Platform Error";
843d541c4bSBenjamin Herrenschmidt 		case RTAS_TYPE_IO:
853d541c4bSBenjamin Herrenschmidt 			return "I/O Event";
863d541c4bSBenjamin Herrenschmidt 		case RTAS_TYPE_INFO:
873d541c4bSBenjamin Herrenschmidt 			return "Platform Information Event";
883d541c4bSBenjamin Herrenschmidt 		case RTAS_TYPE_DEALLOC:
893d541c4bSBenjamin Herrenschmidt 			return "Resource Deallocation Event";
903d541c4bSBenjamin Herrenschmidt 		case RTAS_TYPE_DUMP:
913d541c4bSBenjamin Herrenschmidt 			return "Dump Notification Event";
9249c68a85SJesse Larrew 		case RTAS_TYPE_PRRN:
9349c68a85SJesse Larrew 			return "Platform Resource Reassignment Event";
94*4c5d87dbSOliver O'Halloran 		case RTAS_TYPE_HOTPLUG:
95*4c5d87dbSOliver O'Halloran 			return "Hotplug Event";
963d541c4bSBenjamin Herrenschmidt 	}
973d541c4bSBenjamin Herrenschmidt 
983d541c4bSBenjamin Herrenschmidt 	return rtas_type[0];
993d541c4bSBenjamin Herrenschmidt }
1003d541c4bSBenjamin Herrenschmidt 
1013d541c4bSBenjamin Herrenschmidt /* To see this info, grep RTAS /var/log/messages and each entry
1023d541c4bSBenjamin Herrenschmidt  * will be collected together with obvious begin/end.
1033d541c4bSBenjamin Herrenschmidt  * There will be a unique identifier on the begin and end lines.
1043d541c4bSBenjamin Herrenschmidt  * This will persist across reboots.
1053d541c4bSBenjamin Herrenschmidt  *
1063d541c4bSBenjamin Herrenschmidt  * format of error logs returned from RTAS:
1073d541c4bSBenjamin Herrenschmidt  * bytes	(size)	: contents
1083d541c4bSBenjamin Herrenschmidt  * --------------------------------------------------------
1093d541c4bSBenjamin Herrenschmidt  * 0-7		(8)	: rtas_error_log
1103d541c4bSBenjamin Herrenschmidt  * 8-47		(40)	: extended info
1113d541c4bSBenjamin Herrenschmidt  * 48-51	(4)	: vendor id
1123d541c4bSBenjamin Herrenschmidt  * 52-1023 (vendor specific) : location code and debug data
1133d541c4bSBenjamin Herrenschmidt  */
1143d541c4bSBenjamin Herrenschmidt static void printk_log_rtas(char *buf, int len)
1153d541c4bSBenjamin Herrenschmidt {
1163d541c4bSBenjamin Herrenschmidt 
1173d541c4bSBenjamin Herrenschmidt 	int i,j,n = 0;
1183d541c4bSBenjamin Herrenschmidt 	int perline = 16;
1193d541c4bSBenjamin Herrenschmidt 	char buffer[64];
1203d541c4bSBenjamin Herrenschmidt 	char * str = "RTAS event";
1213d541c4bSBenjamin Herrenschmidt 
1223d541c4bSBenjamin Herrenschmidt 	if (full_rtas_msgs) {
1233d541c4bSBenjamin Herrenschmidt 		printk(RTAS_DEBUG "%d -------- %s begin --------\n",
1243d541c4bSBenjamin Herrenschmidt 		       error_log_cnt, str);
1253d541c4bSBenjamin Herrenschmidt 
1263d541c4bSBenjamin Herrenschmidt 		/*
1273d541c4bSBenjamin Herrenschmidt 		 * Print perline bytes on each line, each line will start
1283d541c4bSBenjamin Herrenschmidt 		 * with RTAS and a changing number, so syslogd will
1293d541c4bSBenjamin Herrenschmidt 		 * print lines that are otherwise the same.  Separate every
1303d541c4bSBenjamin Herrenschmidt 		 * 4 bytes with a space.
1313d541c4bSBenjamin Herrenschmidt 		 */
1323d541c4bSBenjamin Herrenschmidt 		for (i = 0; i < len; i++) {
1333d541c4bSBenjamin Herrenschmidt 			j = i % perline;
1343d541c4bSBenjamin Herrenschmidt 			if (j == 0) {
1353d541c4bSBenjamin Herrenschmidt 				memset(buffer, 0, sizeof(buffer));
1363d541c4bSBenjamin Herrenschmidt 				n = sprintf(buffer, "RTAS %d:", i/perline);
1373d541c4bSBenjamin Herrenschmidt 			}
1383d541c4bSBenjamin Herrenschmidt 
1393d541c4bSBenjamin Herrenschmidt 			if ((i % 4) == 0)
1403d541c4bSBenjamin Herrenschmidt 				n += sprintf(buffer+n, " ");
1413d541c4bSBenjamin Herrenschmidt 
1423d541c4bSBenjamin Herrenschmidt 			n += sprintf(buffer+n, "%02x", (unsigned char)buf[i]);
1433d541c4bSBenjamin Herrenschmidt 
1443d541c4bSBenjamin Herrenschmidt 			if (j == (perline-1))
1453d541c4bSBenjamin Herrenschmidt 				printk(KERN_DEBUG "%s\n", buffer);
1463d541c4bSBenjamin Herrenschmidt 		}
1473d541c4bSBenjamin Herrenschmidt 		if ((i % perline) != 0)
1483d541c4bSBenjamin Herrenschmidt 			printk(KERN_DEBUG "%s\n", buffer);
1493d541c4bSBenjamin Herrenschmidt 
1503d541c4bSBenjamin Herrenschmidt 		printk(RTAS_DEBUG "%d -------- %s end ----------\n",
1513d541c4bSBenjamin Herrenschmidt 		       error_log_cnt, str);
1523d541c4bSBenjamin Herrenschmidt 	} else {
1533d541c4bSBenjamin Herrenschmidt 		struct rtas_error_log *errlog = (struct rtas_error_log *)buf;
1543d541c4bSBenjamin Herrenschmidt 
155b27e5f93SOliver O'Halloran 		printk(RTAS_DEBUG "event: %d, Type: %s (%d), Severity: %d\n",
156b27e5f93SOliver O'Halloran 		       error_log_cnt,
157b27e5f93SOliver O'Halloran 		       rtas_event_type(rtas_error_type(errlog)),
158b27e5f93SOliver O'Halloran 		       rtas_error_type(errlog),
159a08a53eaSGreg Kurz 		       rtas_error_severity(errlog));
1603d541c4bSBenjamin Herrenschmidt 	}
1613d541c4bSBenjamin Herrenschmidt }
1623d541c4bSBenjamin Herrenschmidt 
1633d541c4bSBenjamin Herrenschmidt static int log_rtas_len(char * buf)
1643d541c4bSBenjamin Herrenschmidt {
1653d541c4bSBenjamin Herrenschmidt 	int len;
1663d541c4bSBenjamin Herrenschmidt 	struct rtas_error_log *err;
167a08a53eaSGreg Kurz 	uint32_t extended_log_length;
1683d541c4bSBenjamin Herrenschmidt 
1693d541c4bSBenjamin Herrenschmidt 	/* rtas fixed header */
1703d541c4bSBenjamin Herrenschmidt 	len = 8;
1713d541c4bSBenjamin Herrenschmidt 	err = (struct rtas_error_log *)buf;
172a08a53eaSGreg Kurz 	extended_log_length = rtas_error_extended_log_length(err);
173a08a53eaSGreg Kurz 	if (rtas_error_extended(err) && extended_log_length) {
1743d541c4bSBenjamin Herrenschmidt 
1753d541c4bSBenjamin Herrenschmidt 		/* extended header */
176a08a53eaSGreg Kurz 		len += extended_log_length;
1773d541c4bSBenjamin Herrenschmidt 	}
1783d541c4bSBenjamin Herrenschmidt 
1793d541c4bSBenjamin Herrenschmidt 	if (rtas_error_log_max == 0)
1803d541c4bSBenjamin Herrenschmidt 		rtas_error_log_max = rtas_get_error_log_max();
1813d541c4bSBenjamin Herrenschmidt 
1823d541c4bSBenjamin Herrenschmidt 	if (len > rtas_error_log_max)
1833d541c4bSBenjamin Herrenschmidt 		len = rtas_error_log_max;
1843d541c4bSBenjamin Herrenschmidt 
1853d541c4bSBenjamin Herrenschmidt 	return len;
1863d541c4bSBenjamin Herrenschmidt }
1873d541c4bSBenjamin Herrenschmidt 
1883d541c4bSBenjamin Herrenschmidt /*
1893d541c4bSBenjamin Herrenschmidt  * First write to nvram, if fatal error, that is the only
1903d541c4bSBenjamin Herrenschmidt  * place we log the info.  The error will be picked up
1913d541c4bSBenjamin Herrenschmidt  * on the next reboot by rtasd.  If not fatal, run the
1923d541c4bSBenjamin Herrenschmidt  * method for the type of error.  Currently, only RTAS
1933d541c4bSBenjamin Herrenschmidt  * errors have methods implemented, but in the future
1943d541c4bSBenjamin Herrenschmidt  * there might be a need to store data in nvram before a
1953d541c4bSBenjamin Herrenschmidt  * call to panic().
1963d541c4bSBenjamin Herrenschmidt  *
1973d541c4bSBenjamin Herrenschmidt  * XXX We write to nvram periodically, to indicate error has
1983d541c4bSBenjamin Herrenschmidt  * been written and sync'd, but there is a possibility
1993d541c4bSBenjamin Herrenschmidt  * that if we don't shutdown correctly, a duplicate error
2003d541c4bSBenjamin Herrenschmidt  * record will be created on next reboot.
2013d541c4bSBenjamin Herrenschmidt  */
2023d541c4bSBenjamin Herrenschmidt void pSeries_log_error(char *buf, unsigned int err_type, int fatal)
2033d541c4bSBenjamin Herrenschmidt {
2043d541c4bSBenjamin Herrenschmidt 	unsigned long offset;
2053d541c4bSBenjamin Herrenschmidt 	unsigned long s;
2063d541c4bSBenjamin Herrenschmidt 	int len = 0;
2073d541c4bSBenjamin Herrenschmidt 
2083d541c4bSBenjamin Herrenschmidt 	pr_debug("rtasd: logging event\n");
2093d541c4bSBenjamin Herrenschmidt 	if (buf == NULL)
2103d541c4bSBenjamin Herrenschmidt 		return;
2113d541c4bSBenjamin Herrenschmidt 
2123d541c4bSBenjamin Herrenschmidt 	spin_lock_irqsave(&rtasd_log_lock, s);
2133d541c4bSBenjamin Herrenschmidt 
2143d541c4bSBenjamin Herrenschmidt 	/* get length and increase count */
2153d541c4bSBenjamin Herrenschmidt 	switch (err_type & ERR_TYPE_MASK) {
2163d541c4bSBenjamin Herrenschmidt 	case ERR_TYPE_RTAS_LOG:
2173d541c4bSBenjamin Herrenschmidt 		len = log_rtas_len(buf);
2183d541c4bSBenjamin Herrenschmidt 		if (!(err_type & ERR_FLAG_BOOT))
2193d541c4bSBenjamin Herrenschmidt 			error_log_cnt++;
2203d541c4bSBenjamin Herrenschmidt 		break;
2213d541c4bSBenjamin Herrenschmidt 	case ERR_TYPE_KERNEL_PANIC:
2223d541c4bSBenjamin Herrenschmidt 	default:
2233d541c4bSBenjamin Herrenschmidt 		WARN_ON_ONCE(!irqs_disabled()); /* @@@ DEBUG @@@ */
2243d541c4bSBenjamin Herrenschmidt 		spin_unlock_irqrestore(&rtasd_log_lock, s);
2253d541c4bSBenjamin Herrenschmidt 		return;
2263d541c4bSBenjamin Herrenschmidt 	}
2273d541c4bSBenjamin Herrenschmidt 
2283d541c4bSBenjamin Herrenschmidt #ifdef CONFIG_PPC64
2293d541c4bSBenjamin Herrenschmidt 	/* Write error to NVRAM */
2303d541c4bSBenjamin Herrenschmidt 	if (logging_enabled && !(err_type & ERR_FLAG_BOOT))
2313d541c4bSBenjamin Herrenschmidt 		nvram_write_error_log(buf, len, err_type, error_log_cnt);
2323d541c4bSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
2333d541c4bSBenjamin Herrenschmidt 
2343d541c4bSBenjamin Herrenschmidt 	/*
2353d541c4bSBenjamin Herrenschmidt 	 * rtas errors can occur during boot, and we do want to capture
2363d541c4bSBenjamin Herrenschmidt 	 * those somewhere, even if nvram isn't ready (why not?), and even
2373d541c4bSBenjamin Herrenschmidt 	 * if rtasd isn't ready. Put them into the boot log, at least.
2383d541c4bSBenjamin Herrenschmidt 	 */
2393d541c4bSBenjamin Herrenschmidt 	if ((err_type & ERR_TYPE_MASK) == ERR_TYPE_RTAS_LOG)
2403d541c4bSBenjamin Herrenschmidt 		printk_log_rtas(buf, len);
2413d541c4bSBenjamin Herrenschmidt 
2423d541c4bSBenjamin Herrenschmidt 	/* Check to see if we need to or have stopped logging */
2433d541c4bSBenjamin Herrenschmidt 	if (fatal || !logging_enabled) {
2443d541c4bSBenjamin Herrenschmidt 		logging_enabled = 0;
2453d541c4bSBenjamin Herrenschmidt 		WARN_ON_ONCE(!irqs_disabled()); /* @@@ DEBUG @@@ */
2463d541c4bSBenjamin Herrenschmidt 		spin_unlock_irqrestore(&rtasd_log_lock, s);
2473d541c4bSBenjamin Herrenschmidt 		return;
2483d541c4bSBenjamin Herrenschmidt 	}
2493d541c4bSBenjamin Herrenschmidt 
2503d541c4bSBenjamin Herrenschmidt 	/* call type specific method for error */
2513d541c4bSBenjamin Herrenschmidt 	switch (err_type & ERR_TYPE_MASK) {
2523d541c4bSBenjamin Herrenschmidt 	case ERR_TYPE_RTAS_LOG:
2533d541c4bSBenjamin Herrenschmidt 		offset = rtas_error_log_buffer_max *
2543d541c4bSBenjamin Herrenschmidt 			((rtas_log_start+rtas_log_size) & LOG_NUMBER_MASK);
2553d541c4bSBenjamin Herrenschmidt 
2563d541c4bSBenjamin Herrenschmidt 		/* First copy over sequence number */
2573d541c4bSBenjamin Herrenschmidt 		memcpy(&rtas_log_buf[offset], (void *) &error_log_cnt, sizeof(int));
2583d541c4bSBenjamin Herrenschmidt 
2593d541c4bSBenjamin Herrenschmidt 		/* Second copy over error log data */
2603d541c4bSBenjamin Herrenschmidt 		offset += sizeof(int);
2613d541c4bSBenjamin Herrenschmidt 		memcpy(&rtas_log_buf[offset], buf, len);
2623d541c4bSBenjamin Herrenschmidt 
2633d541c4bSBenjamin Herrenschmidt 		if (rtas_log_size < LOG_NUMBER)
2643d541c4bSBenjamin Herrenschmidt 			rtas_log_size += 1;
2653d541c4bSBenjamin Herrenschmidt 		else
2663d541c4bSBenjamin Herrenschmidt 			rtas_log_start += 1;
2673d541c4bSBenjamin Herrenschmidt 
2683d541c4bSBenjamin Herrenschmidt 		WARN_ON_ONCE(!irqs_disabled()); /* @@@ DEBUG @@@ */
2693d541c4bSBenjamin Herrenschmidt 		spin_unlock_irqrestore(&rtasd_log_lock, s);
2703d541c4bSBenjamin Herrenschmidt 		wake_up_interruptible(&rtas_log_wait);
2713d541c4bSBenjamin Herrenschmidt 		break;
2723d541c4bSBenjamin Herrenschmidt 	case ERR_TYPE_KERNEL_PANIC:
2733d541c4bSBenjamin Herrenschmidt 	default:
2743d541c4bSBenjamin Herrenschmidt 		WARN_ON_ONCE(!irqs_disabled()); /* @@@ DEBUG @@@ */
2753d541c4bSBenjamin Herrenschmidt 		spin_unlock_irqrestore(&rtasd_log_lock, s);
2763d541c4bSBenjamin Herrenschmidt 		return;
2773d541c4bSBenjamin Herrenschmidt 	}
2783d541c4bSBenjamin Herrenschmidt }
2793d541c4bSBenjamin Herrenschmidt 
28049c68a85SJesse Larrew #ifdef CONFIG_PPC_PSERIES
281cd24e457SNathan Fontenot static void handle_prrn_event(s32 scope)
28249c68a85SJesse Larrew {
28349c68a85SJesse Larrew 	/*
28449c68a85SJesse Larrew 	 * For PRRN, we must pass the negative of the scope value in
28549c68a85SJesse Larrew 	 * the RTAS event.
28649c68a85SJesse Larrew 	 */
287cd24e457SNathan Fontenot 	pseries_devicetree_update(-scope);
2883e401f7aSThiago Jung Bauermann 	numa_update_cpu_topology(false);
28949c68a85SJesse Larrew }
29049c68a85SJesse Larrew 
29149c68a85SJesse Larrew static void handle_rtas_event(const struct rtas_error_log *log)
29249c68a85SJesse Larrew {
293a08a53eaSGreg Kurz 	if (rtas_error_type(log) != RTAS_TYPE_PRRN || !prrn_is_enabled())
294a08a53eaSGreg Kurz 		return;
295a08a53eaSGreg Kurz 
29649c68a85SJesse Larrew 	/* For PRRN Events the extended log length is used to denote
29749c68a85SJesse Larrew 	 * the scope for calling rtas update-nodes.
29849c68a85SJesse Larrew 	 */
299cd24e457SNathan Fontenot 	handle_prrn_event(rtas_error_extended_log_length(log));
30049c68a85SJesse Larrew }
30149c68a85SJesse Larrew 
30249c68a85SJesse Larrew #else
30349c68a85SJesse Larrew 
30449c68a85SJesse Larrew static void handle_rtas_event(const struct rtas_error_log *log)
30549c68a85SJesse Larrew {
30649c68a85SJesse Larrew 	return;
30749c68a85SJesse Larrew }
30849c68a85SJesse Larrew 
30949c68a85SJesse Larrew #endif
31049c68a85SJesse Larrew 
3113d541c4bSBenjamin Herrenschmidt static int rtas_log_open(struct inode * inode, struct file * file)
3123d541c4bSBenjamin Herrenschmidt {
3133d541c4bSBenjamin Herrenschmidt 	return 0;
3143d541c4bSBenjamin Herrenschmidt }
3153d541c4bSBenjamin Herrenschmidt 
3163d541c4bSBenjamin Herrenschmidt static int rtas_log_release(struct inode * inode, struct file * file)
3173d541c4bSBenjamin Herrenschmidt {
3183d541c4bSBenjamin Herrenschmidt 	return 0;
3193d541c4bSBenjamin Herrenschmidt }
3203d541c4bSBenjamin Herrenschmidt 
3213d541c4bSBenjamin Herrenschmidt /* This will check if all events are logged, if they are then, we
3223d541c4bSBenjamin Herrenschmidt  * know that we can safely clear the events in NVRAM.
3233d541c4bSBenjamin Herrenschmidt  * Next we'll sit and wait for something else to log.
3243d541c4bSBenjamin Herrenschmidt  */
3253d541c4bSBenjamin Herrenschmidt static ssize_t rtas_log_read(struct file * file, char __user * buf,
3263d541c4bSBenjamin Herrenschmidt 			 size_t count, loff_t *ppos)
3273d541c4bSBenjamin Herrenschmidt {
3283d541c4bSBenjamin Herrenschmidt 	int error;
3293d541c4bSBenjamin Herrenschmidt 	char *tmp;
3303d541c4bSBenjamin Herrenschmidt 	unsigned long s;
3313d541c4bSBenjamin Herrenschmidt 	unsigned long offset;
3323d541c4bSBenjamin Herrenschmidt 
3333d541c4bSBenjamin Herrenschmidt 	if (!buf || count < rtas_error_log_buffer_max)
3343d541c4bSBenjamin Herrenschmidt 		return -EINVAL;
3353d541c4bSBenjamin Herrenschmidt 
3363d541c4bSBenjamin Herrenschmidt 	count = rtas_error_log_buffer_max;
3373d541c4bSBenjamin Herrenschmidt 
3383d541c4bSBenjamin Herrenschmidt 	if (!access_ok(VERIFY_WRITE, buf, count))
3393d541c4bSBenjamin Herrenschmidt 		return -EFAULT;
3403d541c4bSBenjamin Herrenschmidt 
3413d541c4bSBenjamin Herrenschmidt 	tmp = kmalloc(count, GFP_KERNEL);
3423d541c4bSBenjamin Herrenschmidt 	if (!tmp)
3433d541c4bSBenjamin Herrenschmidt 		return -ENOMEM;
3443d541c4bSBenjamin Herrenschmidt 
3453d541c4bSBenjamin Herrenschmidt 	spin_lock_irqsave(&rtasd_log_lock, s);
3463d541c4bSBenjamin Herrenschmidt 
3473d541c4bSBenjamin Herrenschmidt 	/* if it's 0, then we know we got the last one (the one in NVRAM) */
3483d541c4bSBenjamin Herrenschmidt 	while (rtas_log_size == 0) {
3493d541c4bSBenjamin Herrenschmidt 		if (file->f_flags & O_NONBLOCK) {
3503d541c4bSBenjamin Herrenschmidt 			spin_unlock_irqrestore(&rtasd_log_lock, s);
3513d541c4bSBenjamin Herrenschmidt 			error = -EAGAIN;
3523d541c4bSBenjamin Herrenschmidt 			goto out;
3533d541c4bSBenjamin Herrenschmidt 		}
3543d541c4bSBenjamin Herrenschmidt 
3553d541c4bSBenjamin Herrenschmidt 		if (!logging_enabled) {
3563d541c4bSBenjamin Herrenschmidt 			spin_unlock_irqrestore(&rtasd_log_lock, s);
3573d541c4bSBenjamin Herrenschmidt 			error = -ENODATA;
3583d541c4bSBenjamin Herrenschmidt 			goto out;
3593d541c4bSBenjamin Herrenschmidt 		}
3603d541c4bSBenjamin Herrenschmidt #ifdef CONFIG_PPC64
3613d541c4bSBenjamin Herrenschmidt 		nvram_clear_error_log();
3623d541c4bSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
3633d541c4bSBenjamin Herrenschmidt 
3643d541c4bSBenjamin Herrenschmidt 		spin_unlock_irqrestore(&rtasd_log_lock, s);
3653d541c4bSBenjamin Herrenschmidt 		error = wait_event_interruptible(rtas_log_wait, rtas_log_size);
3663d541c4bSBenjamin Herrenschmidt 		if (error)
3673d541c4bSBenjamin Herrenschmidt 			goto out;
3683d541c4bSBenjamin Herrenschmidt 		spin_lock_irqsave(&rtasd_log_lock, s);
3693d541c4bSBenjamin Herrenschmidt 	}
3703d541c4bSBenjamin Herrenschmidt 
3713d541c4bSBenjamin Herrenschmidt 	offset = rtas_error_log_buffer_max * (rtas_log_start & LOG_NUMBER_MASK);
3723d541c4bSBenjamin Herrenschmidt 	memcpy(tmp, &rtas_log_buf[offset], count);
3733d541c4bSBenjamin Herrenschmidt 
3743d541c4bSBenjamin Herrenschmidt 	rtas_log_start += 1;
3753d541c4bSBenjamin Herrenschmidt 	rtas_log_size -= 1;
3763d541c4bSBenjamin Herrenschmidt 	spin_unlock_irqrestore(&rtasd_log_lock, s);
3773d541c4bSBenjamin Herrenschmidt 
3783d541c4bSBenjamin Herrenschmidt 	error = copy_to_user(buf, tmp, count) ? -EFAULT : count;
3793d541c4bSBenjamin Herrenschmidt out:
3803d541c4bSBenjamin Herrenschmidt 	kfree(tmp);
3813d541c4bSBenjamin Herrenschmidt 	return error;
3823d541c4bSBenjamin Herrenschmidt }
3833d541c4bSBenjamin Herrenschmidt 
3848153a5eaSAl Viro static __poll_t rtas_log_poll(struct file *file, poll_table * wait)
3853d541c4bSBenjamin Herrenschmidt {
3863d541c4bSBenjamin Herrenschmidt 	poll_wait(file, &rtas_log_wait, wait);
3873d541c4bSBenjamin Herrenschmidt 	if (rtas_log_size)
388a9a08845SLinus Torvalds 		return EPOLLIN | EPOLLRDNORM;
3893d541c4bSBenjamin Herrenschmidt 	return 0;
3903d541c4bSBenjamin Herrenschmidt }
3913d541c4bSBenjamin Herrenschmidt 
3923d541c4bSBenjamin Herrenschmidt static const struct file_operations proc_rtas_log_operations = {
3933d541c4bSBenjamin Herrenschmidt 	.read =		rtas_log_read,
3943d541c4bSBenjamin Herrenschmidt 	.poll =		rtas_log_poll,
3953d541c4bSBenjamin Herrenschmidt 	.open =		rtas_log_open,
3963d541c4bSBenjamin Herrenschmidt 	.release =	rtas_log_release,
3976038f373SArnd Bergmann 	.llseek =	noop_llseek,
3983d541c4bSBenjamin Herrenschmidt };
3993d541c4bSBenjamin Herrenschmidt 
4003d541c4bSBenjamin Herrenschmidt static int enable_surveillance(int timeout)
4013d541c4bSBenjamin Herrenschmidt {
4023d541c4bSBenjamin Herrenschmidt 	int error;
4033d541c4bSBenjamin Herrenschmidt 
4043d541c4bSBenjamin Herrenschmidt 	error = rtas_set_indicator(SURVEILLANCE_TOKEN, 0, timeout);
4053d541c4bSBenjamin Herrenschmidt 
4063d541c4bSBenjamin Herrenschmidt 	if (error == 0)
4073d541c4bSBenjamin Herrenschmidt 		return 0;
4083d541c4bSBenjamin Herrenschmidt 
4093d541c4bSBenjamin Herrenschmidt 	if (error == -EINVAL) {
4103d541c4bSBenjamin Herrenschmidt 		printk(KERN_DEBUG "rtasd: surveillance not supported\n");
4113d541c4bSBenjamin Herrenschmidt 		return 0;
4123d541c4bSBenjamin Herrenschmidt 	}
4133d541c4bSBenjamin Herrenschmidt 
4143d541c4bSBenjamin Herrenschmidt 	printk(KERN_ERR "rtasd: could not update surveillance\n");
4153d541c4bSBenjamin Herrenschmidt 	return -1;
4163d541c4bSBenjamin Herrenschmidt }
4173d541c4bSBenjamin Herrenschmidt 
4183d541c4bSBenjamin Herrenschmidt static void do_event_scan(void)
4193d541c4bSBenjamin Herrenschmidt {
4203d541c4bSBenjamin Herrenschmidt 	int error;
4213d541c4bSBenjamin Herrenschmidt 	do {
4223d541c4bSBenjamin Herrenschmidt 		memset(logdata, 0, rtas_error_log_max);
4233d541c4bSBenjamin Herrenschmidt 		error = rtas_call(event_scan, 4, 1, NULL,
4243d541c4bSBenjamin Herrenschmidt 				  RTAS_EVENT_SCAN_ALL_EVENTS, 0,
4253d541c4bSBenjamin Herrenschmidt 				  __pa(logdata), rtas_error_log_max);
4263d541c4bSBenjamin Herrenschmidt 		if (error == -1) {
4273d541c4bSBenjamin Herrenschmidt 			printk(KERN_ERR "event-scan failed\n");
4283d541c4bSBenjamin Herrenschmidt 			break;
4293d541c4bSBenjamin Herrenschmidt 		}
4303d541c4bSBenjamin Herrenschmidt 
43149c68a85SJesse Larrew 		if (error == 0) {
432675d8ee6SJohn Allen 			if (rtas_error_type((struct rtas_error_log *)logdata) !=
433675d8ee6SJohn Allen 			    RTAS_TYPE_PRRN)
434675d8ee6SJohn Allen 				pSeries_log_error(logdata, ERR_TYPE_RTAS_LOG,
435675d8ee6SJohn Allen 						  0);
43649c68a85SJesse Larrew 			handle_rtas_event((struct rtas_error_log *)logdata);
43749c68a85SJesse Larrew 		}
4383d541c4bSBenjamin Herrenschmidt 
4393d541c4bSBenjamin Herrenschmidt 	} while(error == 0);
4403d541c4bSBenjamin Herrenschmidt }
4413d541c4bSBenjamin Herrenschmidt 
4423d541c4bSBenjamin Herrenschmidt static void rtas_event_scan(struct work_struct *w);
443635218c7SDaniel Axtens static DECLARE_DELAYED_WORK(event_scan_work, rtas_event_scan);
4443d541c4bSBenjamin Herrenschmidt 
4453d541c4bSBenjamin Herrenschmidt /*
4463d541c4bSBenjamin Herrenschmidt  * Delay should be at least one second since some machines have problems if
4473d541c4bSBenjamin Herrenschmidt  * we call event-scan too quickly.
4483d541c4bSBenjamin Herrenschmidt  */
4493d541c4bSBenjamin Herrenschmidt static unsigned long event_scan_delay = 1*HZ;
4503d541c4bSBenjamin Herrenschmidt static int first_pass = 1;
4513d541c4bSBenjamin Herrenschmidt 
4523d541c4bSBenjamin Herrenschmidt static void rtas_event_scan(struct work_struct *w)
4533d541c4bSBenjamin Herrenschmidt {
4543d541c4bSBenjamin Herrenschmidt 	unsigned int cpu;
4553d541c4bSBenjamin Herrenschmidt 
4563d541c4bSBenjamin Herrenschmidt 	do_event_scan();
4573d541c4bSBenjamin Herrenschmidt 
4583d541c4bSBenjamin Herrenschmidt 	get_online_cpus();
4593d541c4bSBenjamin Herrenschmidt 
4609ff0c61dSPaul E. McKenney 	/* raw_ OK because just using CPU as starting point. */
4619ff0c61dSPaul E. McKenney 	cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
462d5f86fe3SAnton Blanchard         if (cpu >= nr_cpu_ids) {
463d5f86fe3SAnton Blanchard 		cpu = cpumask_first(cpu_online_mask);
4643d541c4bSBenjamin Herrenschmidt 
4653d541c4bSBenjamin Herrenschmidt 		if (first_pass) {
4663d541c4bSBenjamin Herrenschmidt 			first_pass = 0;
4673d541c4bSBenjamin Herrenschmidt 			event_scan_delay = 30*HZ/rtas_event_scan_rate;
4683d541c4bSBenjamin Herrenschmidt 
4693d541c4bSBenjamin Herrenschmidt 			if (surveillance_timeout != -1) {
4703d541c4bSBenjamin Herrenschmidt 				pr_debug("rtasd: enabling surveillance\n");
4713d541c4bSBenjamin Herrenschmidt 				enable_surveillance(surveillance_timeout);
4723d541c4bSBenjamin Herrenschmidt 				pr_debug("rtasd: surveillance enabled\n");
4733d541c4bSBenjamin Herrenschmidt 			}
4743d541c4bSBenjamin Herrenschmidt 		}
4753d541c4bSBenjamin Herrenschmidt 	}
4763d541c4bSBenjamin Herrenschmidt 
4773d541c4bSBenjamin Herrenschmidt 	schedule_delayed_work_on(cpu, &event_scan_work,
4783d541c4bSBenjamin Herrenschmidt 		__round_jiffies_relative(event_scan_delay, cpu));
4793d541c4bSBenjamin Herrenschmidt 
4803d541c4bSBenjamin Herrenschmidt 	put_online_cpus();
4813d541c4bSBenjamin Herrenschmidt }
4823d541c4bSBenjamin Herrenschmidt 
4833d541c4bSBenjamin Herrenschmidt #ifdef CONFIG_PPC64
4846e45273eSGreg Kurz static void retrieve_nvram_error_log(void)
4853d541c4bSBenjamin Herrenschmidt {
4863d541c4bSBenjamin Herrenschmidt 	unsigned int err_type ;
4873d541c4bSBenjamin Herrenschmidt 	int rc ;
4883d541c4bSBenjamin Herrenschmidt 
4893d541c4bSBenjamin Herrenschmidt 	/* See if we have any error stored in NVRAM */
4903d541c4bSBenjamin Herrenschmidt 	memset(logdata, 0, rtas_error_log_max);
4913d541c4bSBenjamin Herrenschmidt 	rc = nvram_read_error_log(logdata, rtas_error_log_max,
4923d541c4bSBenjamin Herrenschmidt 	                          &err_type, &error_log_cnt);
4933d541c4bSBenjamin Herrenschmidt 	/* We can use rtas_log_buf now */
4943d541c4bSBenjamin Herrenschmidt 	logging_enabled = 1;
4953d541c4bSBenjamin Herrenschmidt 	if (!rc) {
4963d541c4bSBenjamin Herrenschmidt 		if (err_type != ERR_FLAG_ALREADY_LOGGED) {
4973d541c4bSBenjamin Herrenschmidt 			pSeries_log_error(logdata, err_type | ERR_FLAG_BOOT, 0);
4983d541c4bSBenjamin Herrenschmidt 		}
4993d541c4bSBenjamin Herrenschmidt 	}
5003d541c4bSBenjamin Herrenschmidt }
5013d541c4bSBenjamin Herrenschmidt #else /* CONFIG_PPC64 */
5026e45273eSGreg Kurz static void retrieve_nvram_error_log(void)
5033d541c4bSBenjamin Herrenschmidt {
5043d541c4bSBenjamin Herrenschmidt }
5053d541c4bSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
5063d541c4bSBenjamin Herrenschmidt 
5073d541c4bSBenjamin Herrenschmidt static void start_event_scan(void)
5083d541c4bSBenjamin Herrenschmidt {
5093d541c4bSBenjamin Herrenschmidt 	printk(KERN_DEBUG "RTAS daemon started\n");
5103d541c4bSBenjamin Herrenschmidt 	pr_debug("rtasd: will sleep for %d milliseconds\n",
5113d541c4bSBenjamin Herrenschmidt 		 (30000 / rtas_event_scan_rate));
5123d541c4bSBenjamin Herrenschmidt 
51325985edcSLucas De Marchi 	/* Retrieve errors from nvram if any */
5146e45273eSGreg Kurz 	retrieve_nvram_error_log();
5153d541c4bSBenjamin Herrenschmidt 
516d5f86fe3SAnton Blanchard 	schedule_delayed_work_on(cpumask_first(cpu_online_mask),
517d5f86fe3SAnton Blanchard 				 &event_scan_work, event_scan_delay);
5183d541c4bSBenjamin Herrenschmidt }
5193d541c4bSBenjamin Herrenschmidt 
520df17f56dSRavi K. Nittala /* Cancel the rtas event scan work */
521df17f56dSRavi K. Nittala void rtas_cancel_event_scan(void)
522df17f56dSRavi K. Nittala {
523df17f56dSRavi K. Nittala 	cancel_delayed_work_sync(&event_scan_work);
524df17f56dSRavi K. Nittala }
525df17f56dSRavi K. Nittala EXPORT_SYMBOL_GPL(rtas_cancel_event_scan);
526df17f56dSRavi K. Nittala 
5278c6a0a1fSGreg Kurz static int __init rtas_event_scan_init(void)
5283d541c4bSBenjamin Herrenschmidt {
5293d541c4bSBenjamin Herrenschmidt 	if (!machine_is(pseries) && !machine_is(chrp))
5303d541c4bSBenjamin Herrenschmidt 		return 0;
5313d541c4bSBenjamin Herrenschmidt 
5323d541c4bSBenjamin Herrenschmidt 	/* No RTAS */
5333d541c4bSBenjamin Herrenschmidt 	event_scan = rtas_token("event-scan");
5343d541c4bSBenjamin Herrenschmidt 	if (event_scan == RTAS_UNKNOWN_SERVICE) {
5353d541c4bSBenjamin Herrenschmidt 		printk(KERN_INFO "rtasd: No event-scan on system\n");
5363d541c4bSBenjamin Herrenschmidt 		return -ENODEV;
5373d541c4bSBenjamin Herrenschmidt 	}
5383d541c4bSBenjamin Herrenschmidt 
5393d541c4bSBenjamin Herrenschmidt 	rtas_event_scan_rate = rtas_token("rtas-event-scan-rate");
5403d541c4bSBenjamin Herrenschmidt 	if (rtas_event_scan_rate == RTAS_UNKNOWN_SERVICE) {
5413d541c4bSBenjamin Herrenschmidt 		printk(KERN_ERR "rtasd: no rtas-event-scan-rate on system\n");
5423d541c4bSBenjamin Herrenschmidt 		return -ENODEV;
5433d541c4bSBenjamin Herrenschmidt 	}
5443d541c4bSBenjamin Herrenschmidt 
5457358650eSMichael Ellerman 	if (!rtas_event_scan_rate) {
5467358650eSMichael Ellerman 		/* Broken firmware: take a rate of zero to mean don't scan */
5477358650eSMichael Ellerman 		printk(KERN_DEBUG "rtasd: scan rate is 0, not scanning\n");
5487358650eSMichael Ellerman 		return 0;
5497358650eSMichael Ellerman 	}
5507358650eSMichael Ellerman 
5513d541c4bSBenjamin Herrenschmidt 	/* Make room for the sequence number */
5523d541c4bSBenjamin Herrenschmidt 	rtas_error_log_max = rtas_get_error_log_max();
5533d541c4bSBenjamin Herrenschmidt 	rtas_error_log_buffer_max = rtas_error_log_max + sizeof(int);
5543d541c4bSBenjamin Herrenschmidt 
55542bc47b3SKees Cook 	rtas_log_buf = vmalloc(array_size(LOG_NUMBER,
55642bc47b3SKees Cook 					  rtas_error_log_buffer_max));
5573d541c4bSBenjamin Herrenschmidt 	if (!rtas_log_buf) {
5583d541c4bSBenjamin Herrenschmidt 		printk(KERN_ERR "rtasd: no memory\n");
5593d541c4bSBenjamin Herrenschmidt 		return -ENOMEM;
5603d541c4bSBenjamin Herrenschmidt 	}
5613d541c4bSBenjamin Herrenschmidt 
5628c6a0a1fSGreg Kurz 	start_event_scan();
5638c6a0a1fSGreg Kurz 
5648c6a0a1fSGreg Kurz 	return 0;
5658c6a0a1fSGreg Kurz }
5668c6a0a1fSGreg Kurz arch_initcall(rtas_event_scan_init);
5678c6a0a1fSGreg Kurz 
5688c6a0a1fSGreg Kurz static int __init rtas_init(void)
5698c6a0a1fSGreg Kurz {
5708c6a0a1fSGreg Kurz 	struct proc_dir_entry *entry;
5718c6a0a1fSGreg Kurz 
5728c6a0a1fSGreg Kurz 	if (!machine_is(pseries) && !machine_is(chrp))
5738c6a0a1fSGreg Kurz 		return 0;
5748c6a0a1fSGreg Kurz 
5758c6a0a1fSGreg Kurz 	if (!rtas_log_buf)
5768c6a0a1fSGreg Kurz 		return -ENODEV;
5778c6a0a1fSGreg Kurz 
57857ad583fSRussell Currey 	entry = proc_create("powerpc/rtas/error_log", 0400, NULL,
5793d541c4bSBenjamin Herrenschmidt 			    &proc_rtas_log_operations);
5803d541c4bSBenjamin Herrenschmidt 	if (!entry)
5813d541c4bSBenjamin Herrenschmidt 		printk(KERN_ERR "Failed to create error_log proc entry\n");
5823d541c4bSBenjamin Herrenschmidt 
5833d541c4bSBenjamin Herrenschmidt 	return 0;
5843d541c4bSBenjamin Herrenschmidt }
5853d541c4bSBenjamin Herrenschmidt __initcall(rtas_init);
5863d541c4bSBenjamin Herrenschmidt 
5873d541c4bSBenjamin Herrenschmidt static int __init surveillance_setup(char *str)
5883d541c4bSBenjamin Herrenschmidt {
5893d541c4bSBenjamin Herrenschmidt 	int i;
5903d541c4bSBenjamin Herrenschmidt 
5913d541c4bSBenjamin Herrenschmidt 	/* We only do surveillance on pseries */
5923d541c4bSBenjamin Herrenschmidt 	if (!machine_is(pseries))
5933d541c4bSBenjamin Herrenschmidt 		return 0;
5943d541c4bSBenjamin Herrenschmidt 
5953d541c4bSBenjamin Herrenschmidt 	if (get_option(&str,&i)) {
5963d541c4bSBenjamin Herrenschmidt 		if (i >= 0 && i <= 255)
5973d541c4bSBenjamin Herrenschmidt 			surveillance_timeout = i;
5983d541c4bSBenjamin Herrenschmidt 	}
5993d541c4bSBenjamin Herrenschmidt 
6003d541c4bSBenjamin Herrenschmidt 	return 1;
6013d541c4bSBenjamin Herrenschmidt }
6023d541c4bSBenjamin Herrenschmidt __setup("surveillance=", surveillance_setup);
6033d541c4bSBenjamin Herrenschmidt 
6043d541c4bSBenjamin Herrenschmidt static int __init rtasmsgs_setup(char *str)
6053d541c4bSBenjamin Herrenschmidt {
6064cc7ecb7SKees Cook 	return (kstrtobool(str, &full_rtas_msgs) == 0);
6073d541c4bSBenjamin Herrenschmidt }
6083d541c4bSBenjamin Herrenschmidt __setup("rtasmsgs=", rtasmsgs_setup);
609