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