xref: /OK3568_Linux_fs/kernel/arch/powerpc/platforms/pseries/scanlog.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  c 2001 PPC 64 Team, IBM Corp
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * scan-log-data driver for PPC64  Todd Inglett <tinglett@vnet.ibm.com>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * When ppc64 hardware fails the service processor dumps internal state
8*4882a593Smuzhiyun  * of the system.  After a reboot the operating system can access a dump
9*4882a593Smuzhiyun  * of this data using this driver.  A dump exists if the device-tree
10*4882a593Smuzhiyun  * /chosen/ibm,scan-log-data property exists.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * This driver exports /proc/powerpc/scan-log-dump which can be read.
13*4882a593Smuzhiyun  * The driver supports only sequential reads.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * The driver looks at a write to the driver for the single word "reset".
16*4882a593Smuzhiyun  * If given, the driver will reset the scanlog so the platform can free it.
17*4882a593Smuzhiyun  */
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include <linux/module.h>
20*4882a593Smuzhiyun #include <linux/types.h>
21*4882a593Smuzhiyun #include <linux/errno.h>
22*4882a593Smuzhiyun #include <linux/proc_fs.h>
23*4882a593Smuzhiyun #include <linux/init.h>
24*4882a593Smuzhiyun #include <linux/delay.h>
25*4882a593Smuzhiyun #include <linux/slab.h>
26*4882a593Smuzhiyun #include <linux/uaccess.h>
27*4882a593Smuzhiyun #include <asm/rtas.h>
28*4882a593Smuzhiyun #include <asm/prom.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #define MODULE_VERS "1.0"
31*4882a593Smuzhiyun #define MODULE_NAME "scanlog"
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /* Status returns from ibm,scan-log-dump */
34*4882a593Smuzhiyun #define SCANLOG_COMPLETE 0
35*4882a593Smuzhiyun #define SCANLOG_HWERROR -1
36*4882a593Smuzhiyun #define SCANLOG_CONTINUE 1
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun static unsigned int ibm_scan_log_dump;			/* RTAS token */
40*4882a593Smuzhiyun static unsigned int *scanlog_buffer;			/* The data buffer */
41*4882a593Smuzhiyun 
scanlog_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)42*4882a593Smuzhiyun static ssize_t scanlog_read(struct file *file, char __user *buf,
43*4882a593Smuzhiyun 			    size_t count, loff_t *ppos)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	unsigned int *data = scanlog_buffer;
46*4882a593Smuzhiyun 	int status;
47*4882a593Smuzhiyun 	unsigned long len, off;
48*4882a593Smuzhiyun 	unsigned int wait_time;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	if (count > RTAS_DATA_BUF_SIZE)
51*4882a593Smuzhiyun 		count = RTAS_DATA_BUF_SIZE;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	if (count < 1024) {
54*4882a593Smuzhiyun 		/* This is the min supported by this RTAS call.  Rather
55*4882a593Smuzhiyun 		 * than do all the buffering we insist the user code handle
56*4882a593Smuzhiyun 		 * larger reads.  As long as cp works... :)
57*4882a593Smuzhiyun 		 */
58*4882a593Smuzhiyun 		printk(KERN_ERR "scanlog: cannot perform a small read (%ld)\n", count);
59*4882a593Smuzhiyun 		return -EINVAL;
60*4882a593Smuzhiyun 	}
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	if (!access_ok(buf, count))
63*4882a593Smuzhiyun 		return -EFAULT;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	for (;;) {
66*4882a593Smuzhiyun 		wait_time = 500;	/* default wait if no data */
67*4882a593Smuzhiyun 		spin_lock(&rtas_data_buf_lock);
68*4882a593Smuzhiyun 		memcpy(rtas_data_buf, data, RTAS_DATA_BUF_SIZE);
69*4882a593Smuzhiyun 		status = rtas_call(ibm_scan_log_dump, 2, 1, NULL,
70*4882a593Smuzhiyun 				   (u32) __pa(rtas_data_buf), (u32) count);
71*4882a593Smuzhiyun 		memcpy(data, rtas_data_buf, RTAS_DATA_BUF_SIZE);
72*4882a593Smuzhiyun 		spin_unlock(&rtas_data_buf_lock);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 		pr_debug("scanlog: status=%d, data[0]=%x, data[1]=%x, " \
75*4882a593Smuzhiyun 			 "data[2]=%x\n", status, data[0], data[1], data[2]);
76*4882a593Smuzhiyun 		switch (status) {
77*4882a593Smuzhiyun 		    case SCANLOG_COMPLETE:
78*4882a593Smuzhiyun 			pr_debug("scanlog: hit eof\n");
79*4882a593Smuzhiyun 			return 0;
80*4882a593Smuzhiyun 		    case SCANLOG_HWERROR:
81*4882a593Smuzhiyun 			pr_debug("scanlog: hardware error reading data\n");
82*4882a593Smuzhiyun 			return -EIO;
83*4882a593Smuzhiyun 		    case SCANLOG_CONTINUE:
84*4882a593Smuzhiyun 			/* We may or may not have data yet */
85*4882a593Smuzhiyun 			len = data[1];
86*4882a593Smuzhiyun 			off = data[2];
87*4882a593Smuzhiyun 			if (len > 0) {
88*4882a593Smuzhiyun 				if (copy_to_user(buf, ((char *)data)+off, len))
89*4882a593Smuzhiyun 					return -EFAULT;
90*4882a593Smuzhiyun 				return len;
91*4882a593Smuzhiyun 			}
92*4882a593Smuzhiyun 			/* Break to sleep default time */
93*4882a593Smuzhiyun 			break;
94*4882a593Smuzhiyun 		    default:
95*4882a593Smuzhiyun 			/* Assume extended busy */
96*4882a593Smuzhiyun 			wait_time = rtas_busy_delay_time(status);
97*4882a593Smuzhiyun 			if (!wait_time) {
98*4882a593Smuzhiyun 				printk(KERN_ERR "scanlog: unknown error " \
99*4882a593Smuzhiyun 				       "from rtas: %d\n", status);
100*4882a593Smuzhiyun 				return -EIO;
101*4882a593Smuzhiyun 			}
102*4882a593Smuzhiyun 		}
103*4882a593Smuzhiyun 		/* Apparently no data yet.  Wait and try again. */
104*4882a593Smuzhiyun 		msleep_interruptible(wait_time);
105*4882a593Smuzhiyun 	}
106*4882a593Smuzhiyun 	/*NOTREACHED*/
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
scanlog_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)109*4882a593Smuzhiyun static ssize_t scanlog_write(struct file * file, const char __user * buf,
110*4882a593Smuzhiyun 			     size_t count, loff_t *ppos)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	char stkbuf[20];
113*4882a593Smuzhiyun 	int status;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	if (count > 19) count = 19;
116*4882a593Smuzhiyun 	if (copy_from_user (stkbuf, buf, count)) {
117*4882a593Smuzhiyun 		return -EFAULT;
118*4882a593Smuzhiyun 	}
119*4882a593Smuzhiyun 	stkbuf[count] = 0;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	if (buf) {
122*4882a593Smuzhiyun 		if (strncmp(stkbuf, "reset", 5) == 0) {
123*4882a593Smuzhiyun 			pr_debug("scanlog: reset scanlog\n");
124*4882a593Smuzhiyun 			status = rtas_call(ibm_scan_log_dump, 2, 1, NULL, 0, 0);
125*4882a593Smuzhiyun 			pr_debug("scanlog: rtas returns %d\n", status);
126*4882a593Smuzhiyun 		}
127*4882a593Smuzhiyun 	}
128*4882a593Smuzhiyun 	return count;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
scanlog_open(struct inode * inode,struct file * file)131*4882a593Smuzhiyun static int scanlog_open(struct inode * inode, struct file * file)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	unsigned int *data = scanlog_buffer;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	if (data[0] != 0) {
136*4882a593Smuzhiyun 		/* This imperfect test stops a second copy of the
137*4882a593Smuzhiyun 		 * data (or a reset while data is being copied)
138*4882a593Smuzhiyun 		 */
139*4882a593Smuzhiyun 		return -EBUSY;
140*4882a593Smuzhiyun 	}
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	data[0] = 0;	/* re-init so we restart the scan */
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	return 0;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
scanlog_release(struct inode * inode,struct file * file)147*4882a593Smuzhiyun static int scanlog_release(struct inode * inode, struct file * file)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	unsigned int *data = scanlog_buffer;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	data[0] = 0;
152*4882a593Smuzhiyun 	return 0;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun static const struct proc_ops scanlog_proc_ops = {
156*4882a593Smuzhiyun 	.proc_read	= scanlog_read,
157*4882a593Smuzhiyun 	.proc_write	= scanlog_write,
158*4882a593Smuzhiyun 	.proc_open	= scanlog_open,
159*4882a593Smuzhiyun 	.proc_release	= scanlog_release,
160*4882a593Smuzhiyun 	.proc_lseek	= noop_llseek,
161*4882a593Smuzhiyun };
162*4882a593Smuzhiyun 
scanlog_init(void)163*4882a593Smuzhiyun static int __init scanlog_init(void)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun 	struct proc_dir_entry *ent;
166*4882a593Smuzhiyun 	int err = -ENOMEM;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	ibm_scan_log_dump = rtas_token("ibm,scan-log-dump");
169*4882a593Smuzhiyun 	if (ibm_scan_log_dump == RTAS_UNKNOWN_SERVICE)
170*4882a593Smuzhiyun 		return -ENODEV;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	/* Ideally we could allocate a buffer < 4G */
173*4882a593Smuzhiyun 	scanlog_buffer = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
174*4882a593Smuzhiyun 	if (!scanlog_buffer)
175*4882a593Smuzhiyun 		goto err;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	ent = proc_create("powerpc/rtas/scan-log-dump", 0400, NULL,
178*4882a593Smuzhiyun 			  &scanlog_proc_ops);
179*4882a593Smuzhiyun 	if (!ent)
180*4882a593Smuzhiyun 		goto err;
181*4882a593Smuzhiyun 	return 0;
182*4882a593Smuzhiyun err:
183*4882a593Smuzhiyun 	kfree(scanlog_buffer);
184*4882a593Smuzhiyun 	return err;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun 
scanlog_cleanup(void)187*4882a593Smuzhiyun static void __exit scanlog_cleanup(void)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun 	remove_proc_entry("powerpc/rtas/scan-log-dump", NULL);
190*4882a593Smuzhiyun 	kfree(scanlog_buffer);
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun module_init(scanlog_init);
194*4882a593Smuzhiyun module_exit(scanlog_cleanup);
195*4882a593Smuzhiyun MODULE_LICENSE("GPL");
196