new helper: file_inode(file)
[linux-2.6-block.git] / arch / powerpc / platforms / pseries / scanlog.c
CommitLineData
1da177e4
LT
1/*
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 *
188917e1 16 * This driver exports /proc/powerpc/scan-log-dump which can be read.
1da177e4
LT
17 * 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>
0287ebed 28#include <linux/delay.h>
5a0e3ad6 29#include <linux/slab.h>
1da177e4
LT
30#include <asm/uaccess.h>
31#include <asm/rtas.h>
32#include <asm/prom.h>
33
34#define MODULE_VERS "1.0"
35#define MODULE_NAME "scanlog"
36
37/* Status returns from ibm,scan-log-dump */
38#define SCANLOG_COMPLETE 0
39#define SCANLOG_HWERROR -1
40#define SCANLOG_CONTINUE 1
41
1da177e4 42
1da177e4
LT
43static unsigned int ibm_scan_log_dump; /* RTAS token */
44static struct proc_dir_entry *proc_ppc64_scan_log_dump; /* The proc file */
45
efa54579 46static ssize_t scanlog_read(struct file *file, char __user *buf,
1da177e4
LT
47 size_t count, loff_t *ppos)
48{
496ad9aa
AV
49 struct proc_dir_entry *dp = PDE(file_inode(file));
50 unsigned int *data = (unsigned int *)dp->data;
1da177e4
LT
51 int status;
52 unsigned long len, off;
53 unsigned int wait_time;
54
1da177e4
LT
55 if (count > RTAS_DATA_BUF_SIZE)
56 count = RTAS_DATA_BUF_SIZE;
57
58 if (count < 1024) {
59 /* This is the min supported by this RTAS call. Rather
60 * than do all the buffering we insist the user code handle
61 * larger reads. As long as cp works... :)
62 */
63 printk(KERN_ERR "scanlog: cannot perform a small read (%ld)\n", count);
64 return -EINVAL;
65 }
66
67 if (!access_ok(VERIFY_WRITE, buf, count))
68 return -EFAULT;
69
70 for (;;) {
0287ebed 71 wait_time = 500; /* default wait if no data */
1da177e4
LT
72 spin_lock(&rtas_data_buf_lock);
73 memcpy(rtas_data_buf, data, RTAS_DATA_BUF_SIZE);
74 status = rtas_call(ibm_scan_log_dump, 2, 1, NULL,
75 (u32) __pa(rtas_data_buf), (u32) count);
76 memcpy(data, rtas_data_buf, RTAS_DATA_BUF_SIZE);
77 spin_unlock(&rtas_data_buf_lock);
78
f7ebf352
ME
79 pr_debug("scanlog: status=%d, data[0]=%x, data[1]=%x, " \
80 "data[2]=%x\n", status, data[0], data[1], data[2]);
1da177e4
LT
81 switch (status) {
82 case SCANLOG_COMPLETE:
f7ebf352 83 pr_debug("scanlog: hit eof\n");
1da177e4
LT
84 return 0;
85 case SCANLOG_HWERROR:
f7ebf352 86 pr_debug("scanlog: hardware error reading data\n");
1da177e4
LT
87 return -EIO;
88 case SCANLOG_CONTINUE:
89 /* We may or may not have data yet */
90 len = data[1];
91 off = data[2];
92 if (len > 0) {
93 if (copy_to_user(buf, ((char *)data)+off, len))
94 return -EFAULT;
95 return len;
96 }
97 /* Break to sleep default time */
98 break;
99 default:
7932f0b8
JR
100 /* Assume extended busy */
101 wait_time = rtas_busy_delay_time(status);
102 if (!wait_time) {
f7ebf352
ME
103 printk(KERN_ERR "scanlog: unknown error " \
104 "from rtas: %d\n", status);
1da177e4
LT
105 return -EIO;
106 }
107 }
108 /* Apparently no data yet. Wait and try again. */
0287ebed 109 msleep_interruptible(wait_time);
1da177e4
LT
110 }
111 /*NOTREACHED*/
112}
113
efa54579 114static ssize_t scanlog_write(struct file * file, const char __user * buf,
1da177e4
LT
115 size_t count, loff_t *ppos)
116{
117 char stkbuf[20];
118 int status;
119
120 if (count > 19) count = 19;
121 if (copy_from_user (stkbuf, buf, count)) {
122 return -EFAULT;
123 }
124 stkbuf[count] = 0;
125
126 if (buf) {
127 if (strncmp(stkbuf, "reset", 5) == 0) {
f7ebf352 128 pr_debug("scanlog: reset scanlog\n");
1da177e4 129 status = rtas_call(ibm_scan_log_dump, 2, 1, NULL, 0, 0);
f7ebf352 130 pr_debug("scanlog: rtas returns %d\n", status);
1da177e4
LT
131 }
132 }
133 return count;
134}
135
136static int scanlog_open(struct inode * inode, struct file * file)
137{
138 struct proc_dir_entry *dp = PDE(inode);
139 unsigned int *data = (unsigned int *)dp->data;
140
1da177e4
LT
141 if (data[0] != 0) {
142 /* This imperfect test stops a second copy of the
143 * data (or a reset while data is being copied)
144 */
145 return -EBUSY;
146 }
147
148 data[0] = 0; /* re-init so we restart the scan */
149
150 return 0;
151}
152
153static int scanlog_release(struct inode * inode, struct file * file)
154{
155 struct proc_dir_entry *dp = PDE(inode);
156 unsigned int *data = (unsigned int *)dp->data;
157
1da177e4
LT
158 data[0] = 0;
159
160 return 0;
161}
162
5dfe4c96 163const struct file_operations scanlog_fops = {
1da177e4
LT
164 .owner = THIS_MODULE,
165 .read = scanlog_read,
166 .write = scanlog_write,
167 .open = scanlog_open,
168 .release = scanlog_release,
6038f373 169 .llseek = noop_llseek,
1da177e4
LT
170};
171
8446196a 172static int __init scanlog_init(void)
1da177e4
LT
173{
174 struct proc_dir_entry *ent;
f61fb8a5
NL
175 void *data;
176 int err = -ENOMEM;
1da177e4
LT
177
178 ibm_scan_log_dump = rtas_token("ibm,scan-log-dump");
f61fb8a5
NL
179 if (ibm_scan_log_dump == RTAS_UNKNOWN_SERVICE)
180 return -ENODEV;
1da177e4 181
f61fb8a5
NL
182 /* Ideally we could allocate a buffer < 4G */
183 data = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
184 if (!data)
185 goto err;
186
188917e1 187 ent = proc_create_data("powerpc/rtas/scan-log-dump", S_IRUSR, NULL,
9185ef67 188 &scanlog_fops, data);
f61fb8a5
NL
189 if (!ent)
190 goto err;
191
1da177e4
LT
192 proc_ppc64_scan_log_dump = ent;
193
194 return 0;
f61fb8a5
NL
195err:
196 kfree(data);
197 return err;
1da177e4
LT
198}
199
8446196a 200static void __exit scanlog_cleanup(void)
1da177e4
LT
201{
202 if (proc_ppc64_scan_log_dump) {
b2325fe1 203 kfree(proc_ppc64_scan_log_dump->data);
1da177e4
LT
204 remove_proc_entry("scan-log-dump", proc_ppc64_scan_log_dump->parent);
205 }
206}
207
208module_init(scanlog_init);
209module_exit(scanlog_cleanup);
210MODULE_LICENSE("GPL");