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