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