blob: 1b45c458f952fc6da9cb0b9e3ebb63416023ae06 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * c 2001 PPC 64 Team, IBM Corp
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * scan-log-data driver for PPC64 Todd Inglett <tinglett@vnet.ibm.com>
10 *
11 * When ppc64 hardware fails the service processor dumps internal state
12 * of the system. After a reboot the operating system can access a dump
13 * of this data using this driver. A dump exists if the device-tree
14 * /chosen/ibm,scan-log-data property exists.
15 *
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +000016 * This driver exports /proc/powerpc/scan-log-dump which can be read.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * The driver supports only sequential reads.
18 *
19 * The driver looks at a write to the driver for the single word "reset".
20 * If given, the driver will reset the scanlog so the platform can free it.
21 */
22
23#include <linux/module.h>
24#include <linux/types.h>
25#include <linux/errno.h>
26#include <linux/proc_fs.h>
27#include <linux/init.h>
Nishanth Aravamudan0287ebe2005-09-03 15:56:01 -070028#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
30#include <asm/rtas.h>
31#include <asm/prom.h>
32
33#define MODULE_VERS "1.0"
34#define MODULE_NAME "scanlog"
35
36/* Status returns from ibm,scan-log-dump */
37#define SCANLOG_COMPLETE 0
38#define SCANLOG_HWERROR -1
39#define SCANLOG_CONTINUE 1
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static unsigned int ibm_scan_log_dump; /* RTAS token */
43static struct proc_dir_entry *proc_ppc64_scan_log_dump; /* The proc file */
44
Al Viroefa54572005-04-26 11:26:53 -070045static ssize_t scanlog_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 size_t count, loff_t *ppos)
47{
Josef Sipekb4d1ab52006-12-08 02:37:30 -080048 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct proc_dir_entry *dp;
50 unsigned int *data;
51 int status;
52 unsigned long len, off;
53 unsigned int wait_time;
54
55 dp = PDE(inode);
56 data = (unsigned int *)dp->data;
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 if (count > RTAS_DATA_BUF_SIZE)
59 count = RTAS_DATA_BUF_SIZE;
60
61 if (count < 1024) {
62 /* This is the min supported by this RTAS call. Rather
63 * than do all the buffering we insist the user code handle
64 * larger reads. As long as cp works... :)
65 */
66 printk(KERN_ERR "scanlog: cannot perform a small read (%ld)\n", count);
67 return -EINVAL;
68 }
69
70 if (!access_ok(VERIFY_WRITE, buf, count))
71 return -EFAULT;
72
73 for (;;) {
Nishanth Aravamudan0287ebe2005-09-03 15:56:01 -070074 wait_time = 500; /* default wait if no data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 spin_lock(&rtas_data_buf_lock);
76 memcpy(rtas_data_buf, data, RTAS_DATA_BUF_SIZE);
77 status = rtas_call(ibm_scan_log_dump, 2, 1, NULL,
78 (u32) __pa(rtas_data_buf), (u32) count);
79 memcpy(data, rtas_data_buf, RTAS_DATA_BUF_SIZE);
80 spin_unlock(&rtas_data_buf_lock);
81
Michael Ellermanf7ebf352008-04-24 15:13:19 +100082 pr_debug("scanlog: status=%d, data[0]=%x, data[1]=%x, " \
83 "data[2]=%x\n", status, data[0], data[1], data[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 switch (status) {
85 case SCANLOG_COMPLETE:
Michael Ellermanf7ebf352008-04-24 15:13:19 +100086 pr_debug("scanlog: hit eof\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return 0;
88 case SCANLOG_HWERROR:
Michael Ellermanf7ebf352008-04-24 15:13:19 +100089 pr_debug("scanlog: hardware error reading data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return -EIO;
91 case SCANLOG_CONTINUE:
92 /* We may or may not have data yet */
93 len = data[1];
94 off = data[2];
95 if (len > 0) {
96 if (copy_to_user(buf, ((char *)data)+off, len))
97 return -EFAULT;
98 return len;
99 }
100 /* Break to sleep default time */
101 break;
102 default:
John Rose7932f0b2006-06-15 17:32:15 -0500103 /* Assume extended busy */
104 wait_time = rtas_busy_delay_time(status);
105 if (!wait_time) {
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000106 printk(KERN_ERR "scanlog: unknown error " \
107 "from rtas: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return -EIO;
109 }
110 }
111 /* Apparently no data yet. Wait and try again. */
Nishanth Aravamudan0287ebe2005-09-03 15:56:01 -0700112 msleep_interruptible(wait_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114 /*NOTREACHED*/
115}
116
Al Viroefa54572005-04-26 11:26:53 -0700117static ssize_t scanlog_write(struct file * file, const char __user * buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 size_t count, loff_t *ppos)
119{
120 char stkbuf[20];
121 int status;
122
123 if (count > 19) count = 19;
124 if (copy_from_user (stkbuf, buf, count)) {
125 return -EFAULT;
126 }
127 stkbuf[count] = 0;
128
129 if (buf) {
130 if (strncmp(stkbuf, "reset", 5) == 0) {
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000131 pr_debug("scanlog: reset scanlog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 status = rtas_call(ibm_scan_log_dump, 2, 1, NULL, 0, 0);
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000133 pr_debug("scanlog: rtas returns %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135 }
136 return count;
137}
138
139static int scanlog_open(struct inode * inode, struct file * file)
140{
141 struct proc_dir_entry *dp = PDE(inode);
142 unsigned int *data = (unsigned int *)dp->data;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (data[0] != 0) {
145 /* This imperfect test stops a second copy of the
146 * data (or a reset while data is being copied)
147 */
148 return -EBUSY;
149 }
150
151 data[0] = 0; /* re-init so we restart the scan */
152
153 return 0;
154}
155
156static int scanlog_release(struct inode * inode, struct file * file)
157{
158 struct proc_dir_entry *dp = PDE(inode);
159 unsigned int *data = (unsigned int *)dp->data;
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 data[0] = 0;
162
163 return 0;
164}
165
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800166const struct file_operations scanlog_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 .owner = THIS_MODULE,
168 .read = scanlog_read,
169 .write = scanlog_write,
170 .open = scanlog_open,
171 .release = scanlog_release,
172};
173
Arnd Bergmann84461962006-01-11 00:00:02 +0000174static int __init scanlog_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct proc_dir_entry *ent;
Nathan Lynchf61fb8a2008-03-24 09:51:45 +1100177 void *data;
178 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 ibm_scan_log_dump = rtas_token("ibm,scan-log-dump");
Nathan Lynchf61fb8a2008-03-24 09:51:45 +1100181 if (ibm_scan_log_dump == RTAS_UNKNOWN_SERVICE)
182 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Nathan Lynchf61fb8a2008-03-24 09:51:45 +1100184 /* Ideally we could allocate a buffer < 4G */
185 data = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
186 if (!data)
187 goto err;
188
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000189 ent = proc_create_data("powerpc/rtas/scan-log-dump", S_IRUSR, NULL,
Denis V. Lunev9185ef62008-05-03 06:34:05 +1000190 &scanlog_fops, data);
Nathan Lynchf61fb8a2008-03-24 09:51:45 +1100191 if (!ent)
192 goto err;
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 proc_ppc64_scan_log_dump = ent;
195
196 return 0;
Nathan Lynchf61fb8a2008-03-24 09:51:45 +1100197err:
198 kfree(data);
199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Arnd Bergmann84461962006-01-11 00:00:02 +0000202static void __exit scanlog_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 if (proc_ppc64_scan_log_dump) {
Jesper Juhlb2325fe2005-11-07 01:01:35 -0800205 kfree(proc_ppc64_scan_log_dump->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 remove_proc_entry("scan-log-dump", proc_ppc64_scan_log_dump->parent);
207 }
208}
209
210module_init(scanlog_init);
211module_exit(scanlog_cleanup);
212MODULE_LICENSE("GPL");