12874c5fdSThomas 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>
12b10af504SNathan Lynch #include <linux/of.h>
133d541c4bSBenjamin Herrenschmidt #include <linux/poll.h>
143d541c4bSBenjamin Herrenschmidt #include <linux/proc_fs.h>
153d541c4bSBenjamin Herrenschmidt #include <linux/init.h>
163d541c4bSBenjamin Herrenschmidt #include <linux/vmalloc.h>
173d541c4bSBenjamin Herrenschmidt #include <linux/spinlock.h>
183d541c4bSBenjamin Herrenschmidt #include <linux/cpu.h>
193d541c4bSBenjamin Herrenschmidt #include <linux/workqueue.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
21675d8ee6SJohn Allen #include <linux/topology.h>
223d541c4bSBenjamin Herrenschmidt
237c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
243d541c4bSBenjamin Herrenschmidt #include <asm/io.h>
253d541c4bSBenjamin Herrenschmidt #include <asm/rtas.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
rtas_event_type(int type)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 */
printk_log_rtas(char * buf,int len)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
log_rtas_len(char * buf)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 */
pSeries_log_error(char * buf,unsigned int err_type,int fatal)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
handle_rtas_event(const struct rtas_error_log * log)27649c68a85SJesse Larrew static void handle_rtas_event(const struct rtas_error_log *log)
27749c68a85SJesse Larrew {
27891713ac3SNathan Lynch if (!machine_is(pseries))
279a08a53eaSGreg Kurz return;
280a08a53eaSGreg Kurz
28191713ac3SNathan Lynch if (rtas_error_type(log) == RTAS_TYPE_PRRN)
28291713ac3SNathan Lynch pr_info_ratelimited("Platform resource reassignment ignored.\n");
28349c68a85SJesse Larrew }
28449c68a85SJesse Larrew
rtas_log_open(struct inode * inode,struct file * file)2853d541c4bSBenjamin Herrenschmidt static int rtas_log_open(struct inode * inode, struct file * file)
2863d541c4bSBenjamin Herrenschmidt {
2873d541c4bSBenjamin Herrenschmidt return 0;
2883d541c4bSBenjamin Herrenschmidt }
2893d541c4bSBenjamin Herrenschmidt
rtas_log_release(struct inode * inode,struct file * file)2903d541c4bSBenjamin Herrenschmidt static int rtas_log_release(struct inode * inode, struct file * file)
2913d541c4bSBenjamin Herrenschmidt {
2923d541c4bSBenjamin Herrenschmidt return 0;
2933d541c4bSBenjamin Herrenschmidt }
2943d541c4bSBenjamin Herrenschmidt
2953d541c4bSBenjamin Herrenschmidt /* This will check if all events are logged, if they are then, we
2963d541c4bSBenjamin Herrenschmidt * know that we can safely clear the events in NVRAM.
2973d541c4bSBenjamin Herrenschmidt * Next we'll sit and wait for something else to log.
2983d541c4bSBenjamin Herrenschmidt */
rtas_log_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)2993d541c4bSBenjamin Herrenschmidt static ssize_t rtas_log_read(struct file * file, char __user * buf,
3003d541c4bSBenjamin Herrenschmidt size_t count, loff_t *ppos)
3013d541c4bSBenjamin Herrenschmidt {
3023d541c4bSBenjamin Herrenschmidt int error;
3033d541c4bSBenjamin Herrenschmidt char *tmp;
3043d541c4bSBenjamin Herrenschmidt unsigned long s;
3053d541c4bSBenjamin Herrenschmidt unsigned long offset;
3063d541c4bSBenjamin Herrenschmidt
3073d541c4bSBenjamin Herrenschmidt if (!buf || count < rtas_error_log_buffer_max)
3083d541c4bSBenjamin Herrenschmidt return -EINVAL;
3093d541c4bSBenjamin Herrenschmidt
3103d541c4bSBenjamin Herrenschmidt count = rtas_error_log_buffer_max;
3113d541c4bSBenjamin Herrenschmidt
31296d4f267SLinus Torvalds if (!access_ok(buf, count))
3133d541c4bSBenjamin Herrenschmidt return -EFAULT;
3143d541c4bSBenjamin Herrenschmidt
3153d541c4bSBenjamin Herrenschmidt tmp = kmalloc(count, GFP_KERNEL);
3163d541c4bSBenjamin Herrenschmidt if (!tmp)
3173d541c4bSBenjamin Herrenschmidt return -ENOMEM;
3183d541c4bSBenjamin Herrenschmidt
3193d541c4bSBenjamin Herrenschmidt spin_lock_irqsave(&rtasd_log_lock, s);
3203d541c4bSBenjamin Herrenschmidt
3213d541c4bSBenjamin Herrenschmidt /* if it's 0, then we know we got the last one (the one in NVRAM) */
3223d541c4bSBenjamin Herrenschmidt while (rtas_log_size == 0) {
3233d541c4bSBenjamin Herrenschmidt if (file->f_flags & O_NONBLOCK) {
3243d541c4bSBenjamin Herrenschmidt spin_unlock_irqrestore(&rtasd_log_lock, s);
3253d541c4bSBenjamin Herrenschmidt error = -EAGAIN;
3263d541c4bSBenjamin Herrenschmidt goto out;
3273d541c4bSBenjamin Herrenschmidt }
3283d541c4bSBenjamin Herrenschmidt
3293d541c4bSBenjamin Herrenschmidt if (!logging_enabled) {
3303d541c4bSBenjamin Herrenschmidt spin_unlock_irqrestore(&rtasd_log_lock, s);
3313d541c4bSBenjamin Herrenschmidt error = -ENODATA;
3323d541c4bSBenjamin Herrenschmidt goto out;
3333d541c4bSBenjamin Herrenschmidt }
3343d541c4bSBenjamin Herrenschmidt #ifdef CONFIG_PPC64
3353d541c4bSBenjamin Herrenschmidt nvram_clear_error_log();
3363d541c4bSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
3373d541c4bSBenjamin Herrenschmidt
3383d541c4bSBenjamin Herrenschmidt spin_unlock_irqrestore(&rtasd_log_lock, s);
3393d541c4bSBenjamin Herrenschmidt error = wait_event_interruptible(rtas_log_wait, rtas_log_size);
3403d541c4bSBenjamin Herrenschmidt if (error)
3413d541c4bSBenjamin Herrenschmidt goto out;
3423d541c4bSBenjamin Herrenschmidt spin_lock_irqsave(&rtasd_log_lock, s);
3433d541c4bSBenjamin Herrenschmidt }
3443d541c4bSBenjamin Herrenschmidt
3453d541c4bSBenjamin Herrenschmidt offset = rtas_error_log_buffer_max * (rtas_log_start & LOG_NUMBER_MASK);
3463d541c4bSBenjamin Herrenschmidt memcpy(tmp, &rtas_log_buf[offset], count);
3473d541c4bSBenjamin Herrenschmidt
3483d541c4bSBenjamin Herrenschmidt rtas_log_start += 1;
3493d541c4bSBenjamin Herrenschmidt rtas_log_size -= 1;
3503d541c4bSBenjamin Herrenschmidt spin_unlock_irqrestore(&rtasd_log_lock, s);
3513d541c4bSBenjamin Herrenschmidt
3523d541c4bSBenjamin Herrenschmidt error = copy_to_user(buf, tmp, count) ? -EFAULT : count;
3533d541c4bSBenjamin Herrenschmidt out:
3543d541c4bSBenjamin Herrenschmidt kfree(tmp);
3553d541c4bSBenjamin Herrenschmidt return error;
3563d541c4bSBenjamin Herrenschmidt }
3573d541c4bSBenjamin Herrenschmidt
rtas_log_poll(struct file * file,poll_table * wait)3588153a5eaSAl Viro static __poll_t rtas_log_poll(struct file *file, poll_table * wait)
3593d541c4bSBenjamin Herrenschmidt {
3603d541c4bSBenjamin Herrenschmidt poll_wait(file, &rtas_log_wait, wait);
3613d541c4bSBenjamin Herrenschmidt if (rtas_log_size)
362a9a08845SLinus Torvalds return EPOLLIN | EPOLLRDNORM;
3633d541c4bSBenjamin Herrenschmidt return 0;
3643d541c4bSBenjamin Herrenschmidt }
3653d541c4bSBenjamin Herrenschmidt
36697a32539SAlexey Dobriyan static const struct proc_ops rtas_log_proc_ops = {
36797a32539SAlexey Dobriyan .proc_read = rtas_log_read,
36897a32539SAlexey Dobriyan .proc_poll = rtas_log_poll,
36997a32539SAlexey Dobriyan .proc_open = rtas_log_open,
37097a32539SAlexey Dobriyan .proc_release = rtas_log_release,
37197a32539SAlexey Dobriyan .proc_lseek = noop_llseek,
3723d541c4bSBenjamin Herrenschmidt };
3733d541c4bSBenjamin Herrenschmidt
enable_surveillance(int timeout)3743d541c4bSBenjamin Herrenschmidt static int enable_surveillance(int timeout)
3753d541c4bSBenjamin Herrenschmidt {
3763d541c4bSBenjamin Herrenschmidt int error;
3773d541c4bSBenjamin Herrenschmidt
3783d541c4bSBenjamin Herrenschmidt error = rtas_set_indicator(SURVEILLANCE_TOKEN, 0, timeout);
3793d541c4bSBenjamin Herrenschmidt
3803d541c4bSBenjamin Herrenschmidt if (error == 0)
3813d541c4bSBenjamin Herrenschmidt return 0;
3823d541c4bSBenjamin Herrenschmidt
3833d541c4bSBenjamin Herrenschmidt if (error == -EINVAL) {
3843d541c4bSBenjamin Herrenschmidt printk(KERN_DEBUG "rtasd: surveillance not supported\n");
3853d541c4bSBenjamin Herrenschmidt return 0;
3863d541c4bSBenjamin Herrenschmidt }
3873d541c4bSBenjamin Herrenschmidt
3883d541c4bSBenjamin Herrenschmidt printk(KERN_ERR "rtasd: could not update surveillance\n");
3893d541c4bSBenjamin Herrenschmidt return -1;
3903d541c4bSBenjamin Herrenschmidt }
3913d541c4bSBenjamin Herrenschmidt
do_event_scan(void)3923d541c4bSBenjamin Herrenschmidt static void do_event_scan(void)
3933d541c4bSBenjamin Herrenschmidt {
3943d541c4bSBenjamin Herrenschmidt int error;
3953d541c4bSBenjamin Herrenschmidt do {
3963d541c4bSBenjamin Herrenschmidt memset(logdata, 0, rtas_error_log_max);
3973d541c4bSBenjamin Herrenschmidt error = rtas_call(event_scan, 4, 1, NULL,
3983d541c4bSBenjamin Herrenschmidt RTAS_EVENT_SCAN_ALL_EVENTS, 0,
3993d541c4bSBenjamin Herrenschmidt __pa(logdata), rtas_error_log_max);
4003d541c4bSBenjamin Herrenschmidt if (error == -1) {
4013d541c4bSBenjamin Herrenschmidt printk(KERN_ERR "event-scan failed\n");
4023d541c4bSBenjamin Herrenschmidt break;
4033d541c4bSBenjamin Herrenschmidt }
4043d541c4bSBenjamin Herrenschmidt
40549c68a85SJesse Larrew if (error == 0) {
406675d8ee6SJohn Allen if (rtas_error_type((struct rtas_error_log *)logdata) !=
407675d8ee6SJohn Allen RTAS_TYPE_PRRN)
408675d8ee6SJohn Allen pSeries_log_error(logdata, ERR_TYPE_RTAS_LOG,
409675d8ee6SJohn Allen 0);
41049c68a85SJesse Larrew handle_rtas_event((struct rtas_error_log *)logdata);
41149c68a85SJesse Larrew }
4123d541c4bSBenjamin Herrenschmidt
4133d541c4bSBenjamin Herrenschmidt } while(error == 0);
4143d541c4bSBenjamin Herrenschmidt }
4153d541c4bSBenjamin Herrenschmidt
4163d541c4bSBenjamin Herrenschmidt static void rtas_event_scan(struct work_struct *w);
417635218c7SDaniel Axtens static DECLARE_DELAYED_WORK(event_scan_work, rtas_event_scan);
4183d541c4bSBenjamin Herrenschmidt
4193d541c4bSBenjamin Herrenschmidt /*
4203d541c4bSBenjamin Herrenschmidt * Delay should be at least one second since some machines have problems if
4213d541c4bSBenjamin Herrenschmidt * we call event-scan too quickly.
4223d541c4bSBenjamin Herrenschmidt */
4233d541c4bSBenjamin Herrenschmidt static unsigned long event_scan_delay = 1*HZ;
4243d541c4bSBenjamin Herrenschmidt static int first_pass = 1;
4253d541c4bSBenjamin Herrenschmidt
rtas_event_scan(struct work_struct * w)4263d541c4bSBenjamin Herrenschmidt static void rtas_event_scan(struct work_struct *w)
4273d541c4bSBenjamin Herrenschmidt {
4283d541c4bSBenjamin Herrenschmidt unsigned int cpu;
4293d541c4bSBenjamin Herrenschmidt
4303d541c4bSBenjamin Herrenschmidt do_event_scan();
4313d541c4bSBenjamin Herrenschmidt
4325ae36401SSebastian Andrzej Siewior cpus_read_lock();
4333d541c4bSBenjamin Herrenschmidt
4349ff0c61dSPaul E. McKenney /* raw_ OK because just using CPU as starting point. */
4359ff0c61dSPaul E. McKenney cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
436d5f86fe3SAnton Blanchard if (cpu >= nr_cpu_ids) {
437d5f86fe3SAnton Blanchard cpu = cpumask_first(cpu_online_mask);
4383d541c4bSBenjamin Herrenschmidt
4393d541c4bSBenjamin Herrenschmidt if (first_pass) {
4403d541c4bSBenjamin Herrenschmidt first_pass = 0;
4413d541c4bSBenjamin Herrenschmidt event_scan_delay = 30*HZ/rtas_event_scan_rate;
4423d541c4bSBenjamin Herrenschmidt
4433d541c4bSBenjamin Herrenschmidt if (surveillance_timeout != -1) {
4443d541c4bSBenjamin Herrenschmidt pr_debug("rtasd: enabling surveillance\n");
4453d541c4bSBenjamin Herrenschmidt enable_surveillance(surveillance_timeout);
4463d541c4bSBenjamin Herrenschmidt pr_debug("rtasd: surveillance enabled\n");
4473d541c4bSBenjamin Herrenschmidt }
4483d541c4bSBenjamin Herrenschmidt }
4493d541c4bSBenjamin Herrenschmidt }
4503d541c4bSBenjamin Herrenschmidt
4513d541c4bSBenjamin Herrenschmidt schedule_delayed_work_on(cpu, &event_scan_work,
4523d541c4bSBenjamin Herrenschmidt __round_jiffies_relative(event_scan_delay, cpu));
4533d541c4bSBenjamin Herrenschmidt
4545ae36401SSebastian Andrzej Siewior cpus_read_unlock();
4553d541c4bSBenjamin Herrenschmidt }
4563d541c4bSBenjamin Herrenschmidt
4573d541c4bSBenjamin Herrenschmidt #ifdef CONFIG_PPC64
retrieve_nvram_error_log(void)458d276960dSNick Child static void __init retrieve_nvram_error_log(void)
4593d541c4bSBenjamin Herrenschmidt {
4603d541c4bSBenjamin Herrenschmidt unsigned int err_type ;
4613d541c4bSBenjamin Herrenschmidt int rc ;
4623d541c4bSBenjamin Herrenschmidt
4633d541c4bSBenjamin Herrenschmidt /* See if we have any error stored in NVRAM */
4643d541c4bSBenjamin Herrenschmidt memset(logdata, 0, rtas_error_log_max);
4653d541c4bSBenjamin Herrenschmidt rc = nvram_read_error_log(logdata, rtas_error_log_max,
4663d541c4bSBenjamin Herrenschmidt &err_type, &error_log_cnt);
4673d541c4bSBenjamin Herrenschmidt /* We can use rtas_log_buf now */
4683d541c4bSBenjamin Herrenschmidt logging_enabled = 1;
4693d541c4bSBenjamin Herrenschmidt if (!rc) {
4703d541c4bSBenjamin Herrenschmidt if (err_type != ERR_FLAG_ALREADY_LOGGED) {
4713d541c4bSBenjamin Herrenschmidt pSeries_log_error(logdata, err_type | ERR_FLAG_BOOT, 0);
4723d541c4bSBenjamin Herrenschmidt }
4733d541c4bSBenjamin Herrenschmidt }
4743d541c4bSBenjamin Herrenschmidt }
4753d541c4bSBenjamin Herrenschmidt #else /* CONFIG_PPC64 */
retrieve_nvram_error_log(void)476d276960dSNick Child static void __init retrieve_nvram_error_log(void)
4773d541c4bSBenjamin Herrenschmidt {
4783d541c4bSBenjamin Herrenschmidt }
4793d541c4bSBenjamin Herrenschmidt #endif /* CONFIG_PPC64 */
4803d541c4bSBenjamin Herrenschmidt
start_event_scan(void)481d276960dSNick Child static void __init start_event_scan(void)
4823d541c4bSBenjamin Herrenschmidt {
4833d541c4bSBenjamin Herrenschmidt printk(KERN_DEBUG "RTAS daemon started\n");
4843d541c4bSBenjamin Herrenschmidt pr_debug("rtasd: will sleep for %d milliseconds\n",
4853d541c4bSBenjamin Herrenschmidt (30000 / rtas_event_scan_rate));
4863d541c4bSBenjamin Herrenschmidt
48725985edcSLucas De Marchi /* Retrieve errors from nvram if any */
4886e45273eSGreg Kurz retrieve_nvram_error_log();
4893d541c4bSBenjamin Herrenschmidt
490d5f86fe3SAnton Blanchard schedule_delayed_work_on(cpumask_first(cpu_online_mask),
491d5f86fe3SAnton Blanchard &event_scan_work, event_scan_delay);
4923d541c4bSBenjamin Herrenschmidt }
4933d541c4bSBenjamin Herrenschmidt
494df17f56dSRavi K. Nittala /* Cancel the rtas event scan work */
rtas_cancel_event_scan(void)495df17f56dSRavi K. Nittala void rtas_cancel_event_scan(void)
496df17f56dSRavi K. Nittala {
497df17f56dSRavi K. Nittala cancel_delayed_work_sync(&event_scan_work);
498df17f56dSRavi K. Nittala }
499df17f56dSRavi K. Nittala EXPORT_SYMBOL_GPL(rtas_cancel_event_scan);
500df17f56dSRavi K. Nittala
rtas_event_scan_init(void)5018c6a0a1fSGreg Kurz static int __init rtas_event_scan_init(void)
5023d541c4bSBenjamin Herrenschmidt {
503b10af504SNathan Lynch int err;
504b10af504SNathan Lynch
5053d541c4bSBenjamin Herrenschmidt if (!machine_is(pseries) && !machine_is(chrp))
5063d541c4bSBenjamin Herrenschmidt return 0;
5073d541c4bSBenjamin Herrenschmidt
5083d541c4bSBenjamin Herrenschmidt /* No RTAS */
509*08273c9fSNathan Lynch event_scan = rtas_function_token(RTAS_FN_EVENT_SCAN);
5103d541c4bSBenjamin Herrenschmidt if (event_scan == RTAS_UNKNOWN_SERVICE) {
5113d541c4bSBenjamin Herrenschmidt printk(KERN_INFO "rtasd: No event-scan on system\n");
5123d541c4bSBenjamin Herrenschmidt return -ENODEV;
5133d541c4bSBenjamin Herrenschmidt }
5143d541c4bSBenjamin Herrenschmidt
515b10af504SNathan Lynch err = of_property_read_u32(rtas.dev, "rtas-event-scan-rate", &rtas_event_scan_rate);
516b10af504SNathan Lynch if (err) {
5173d541c4bSBenjamin Herrenschmidt printk(KERN_ERR "rtasd: no rtas-event-scan-rate on system\n");
5183d541c4bSBenjamin Herrenschmidt return -ENODEV;
5193d541c4bSBenjamin Herrenschmidt }
5203d541c4bSBenjamin Herrenschmidt
5217358650eSMichael Ellerman if (!rtas_event_scan_rate) {
5227358650eSMichael Ellerman /* Broken firmware: take a rate of zero to mean don't scan */
5237358650eSMichael Ellerman printk(KERN_DEBUG "rtasd: scan rate is 0, not scanning\n");
5247358650eSMichael Ellerman return 0;
5257358650eSMichael Ellerman }
5267358650eSMichael Ellerman
5273d541c4bSBenjamin Herrenschmidt /* Make room for the sequence number */
5283d541c4bSBenjamin Herrenschmidt rtas_error_log_max = rtas_get_error_log_max();
5293d541c4bSBenjamin Herrenschmidt rtas_error_log_buffer_max = rtas_error_log_max + sizeof(int);
5303d541c4bSBenjamin Herrenschmidt
53142bc47b3SKees Cook rtas_log_buf = vmalloc(array_size(LOG_NUMBER,
53242bc47b3SKees Cook rtas_error_log_buffer_max));
5333d541c4bSBenjamin Herrenschmidt if (!rtas_log_buf) {
5343d541c4bSBenjamin Herrenschmidt printk(KERN_ERR "rtasd: no memory\n");
5353d541c4bSBenjamin Herrenschmidt return -ENOMEM;
5363d541c4bSBenjamin Herrenschmidt }
5373d541c4bSBenjamin Herrenschmidt
5388c6a0a1fSGreg Kurz start_event_scan();
5398c6a0a1fSGreg Kurz
5408c6a0a1fSGreg Kurz return 0;
5418c6a0a1fSGreg Kurz }
5428c6a0a1fSGreg Kurz arch_initcall(rtas_event_scan_init);
5438c6a0a1fSGreg Kurz
rtas_init(void)5448c6a0a1fSGreg Kurz static int __init rtas_init(void)
5458c6a0a1fSGreg Kurz {
5468c6a0a1fSGreg Kurz struct proc_dir_entry *entry;
5478c6a0a1fSGreg Kurz
5488c6a0a1fSGreg Kurz if (!machine_is(pseries) && !machine_is(chrp))
5498c6a0a1fSGreg Kurz return 0;
5508c6a0a1fSGreg Kurz
5518c6a0a1fSGreg Kurz if (!rtas_log_buf)
5528c6a0a1fSGreg Kurz return -ENODEV;
5538c6a0a1fSGreg Kurz
55457ad583fSRussell Currey entry = proc_create("powerpc/rtas/error_log", 0400, NULL,
55597a32539SAlexey Dobriyan &rtas_log_proc_ops);
5563d541c4bSBenjamin Herrenschmidt if (!entry)
5573d541c4bSBenjamin Herrenschmidt printk(KERN_ERR "Failed to create error_log proc entry\n");
5583d541c4bSBenjamin Herrenschmidt
5593d541c4bSBenjamin Herrenschmidt return 0;
5603d541c4bSBenjamin Herrenschmidt }
5613d541c4bSBenjamin Herrenschmidt __initcall(rtas_init);
5623d541c4bSBenjamin Herrenschmidt
surveillance_setup(char * str)5633d541c4bSBenjamin Herrenschmidt static int __init surveillance_setup(char *str)
5643d541c4bSBenjamin Herrenschmidt {
5653d541c4bSBenjamin Herrenschmidt int i;
5663d541c4bSBenjamin Herrenschmidt
5673d541c4bSBenjamin Herrenschmidt /* We only do surveillance on pseries */
5683d541c4bSBenjamin Herrenschmidt if (!machine_is(pseries))
5693d541c4bSBenjamin Herrenschmidt return 0;
5703d541c4bSBenjamin Herrenschmidt
5713d541c4bSBenjamin Herrenschmidt if (get_option(&str,&i)) {
5723d541c4bSBenjamin Herrenschmidt if (i >= 0 && i <= 255)
5733d541c4bSBenjamin Herrenschmidt surveillance_timeout = i;
5743d541c4bSBenjamin Herrenschmidt }
5753d541c4bSBenjamin Herrenschmidt
5763d541c4bSBenjamin Herrenschmidt return 1;
5773d541c4bSBenjamin Herrenschmidt }
5783d541c4bSBenjamin Herrenschmidt __setup("surveillance=", surveillance_setup);
5793d541c4bSBenjamin Herrenschmidt
rtasmsgs_setup(char * str)5803d541c4bSBenjamin Herrenschmidt static int __init rtasmsgs_setup(char *str)
5813d541c4bSBenjamin Herrenschmidt {
5824cc7ecb7SKees Cook return (kstrtobool(str, &full_rtas_msgs) == 0);
5833d541c4bSBenjamin Herrenschmidt }
5843d541c4bSBenjamin Herrenschmidt __setup("rtasmsgs=", rtasmsgs_setup);
585