cleanup
[linux-2.4.21-pre4.git] / arch / ppc64 / kernel / 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 <asm/uaccess.h>
29 #include <asm/rtas.h>
30 #include <asm/prom.h>
31
32 #define MODULE_VERSION "1.0"
33 #define MODULE_NAME "scanlog"
34
35 /* Status returns from ibm,scan-log-dump */
36 #define SCANLOG_COMPLETE 0
37 #define SCANLOG_HWERROR -1
38 #define SCANLOG_CONTINUE 1
39
40 #define DEBUG(A...) do { if (scanlog_debug) printk(KERN_ERR "scanlog: " A); } while (0)
41
42 static int scanlog_debug;
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
47 static ssize_t scanlog_read(struct file *file, char *buf,
48                             size_t count, loff_t *ppos)
49 {
50         struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
51         unsigned int *data = (unsigned int *)dp->data;
52         unsigned long status;
53         unsigned long len, off;
54         unsigned int wait_time;
55
56         if (!data) {
57                 printk(KERN_ERR "scanlog: read failed no data\n");
58                 return -EIO;
59         }
60
61         if (count > RTAS_DATA_BUF_SIZE)
62                 count = RTAS_DATA_BUF_SIZE;
63
64         if (count < 1024) {
65                 /* This is the min supported by this RTAS call.  Rather
66                  * than do all the buffering we insist the user code handle
67                  * larger reads.  As long as cp works... :)
68                  */
69                 printk(KERN_ERR "scanlog: cannot perform a small read (%ld)\n", count);
70                 return -EINVAL;
71         }
72
73         if (verify_area(VERIFY_WRITE, buf, count))
74                 return -EFAULT;
75
76         for (;;) {
77                 wait_time = HZ/2;       /* default wait if no data */
78                 spin_lock(&rtas_data_buf_lock);
79                 memcpy(rtas_data_buf, data, RTAS_DATA_BUF_SIZE);
80                 status = rtas_call(ibm_scan_log_dump, 2, 1, NULL,
81                                    __pa(rtas_data_buf), count);
82                 memcpy(data, rtas_data_buf, RTAS_DATA_BUF_SIZE);
83                 spin_unlock(&rtas_data_buf_lock);
84
85                 DEBUG("status=%ld, data[0]=%x, data[1]=%x, data[2]=%x\n",
86                       status, data[0], data[1], data[2]);
87                 switch (status) {
88                     case SCANLOG_COMPLETE:
89                         DEBUG("hit eof\n");
90                         return 0;
91                     case SCANLOG_HWERROR:
92                         DEBUG("hardware error reading scan log data\n");
93                         return -EIO;
94                     case SCANLOG_CONTINUE:
95                         /* We may or may not have data yet */
96                         len = data[1];
97                         off = data[2];
98                         if (len > 0) {
99                                 if (copy_to_user(buf, ((char *)data)+off, len))
100                                         return -EFAULT;
101                                 return len;
102                         }
103                         /* Break to sleep default time */
104                         break;
105                     default:
106                         if (status > 9900 && status <= 9905) {
107                                 /* No data.  RTAS is hinting at a delay required
108                                  * between 1-100000 milliseconds
109                                  */
110                                 int ms = 1;
111                                 for (; status > 9900; status--)
112                                         ms = ms * 10;
113                                 /* Use microseconds for reasonable accuracy */
114                                 ms *= 1000;
115                                 wait_time = ms / (1000000/HZ); /* round down is fine */
116                                 /* Fall through to sleep */
117                         } else {
118                                 printk(KERN_ERR "scanlog: unknown error from rtas: %ld\n", status);
119                                 return -EIO;
120                         }
121                 }
122                 /* Apparently no data yet.  Wait and try again. */
123                 set_current_state(TASK_INTERRUPTIBLE);
124                 schedule_timeout(wait_time);
125         }
126         /*NOTREACHED*/
127 }
128
129 static ssize_t scanlog_write(struct file * file, const char * buf,
130                              size_t count, loff_t *ppos)
131 {
132         unsigned long status;
133
134         if (buf) {
135                 if (strncmp(buf, "reset", 5) == 0) {
136                         DEBUG("reset scanlog\n");
137                         status = rtas_call(ibm_scan_log_dump, 2, 1, NULL, NULL, 0);
138                         DEBUG("rtas returns %ld\n", status);
139                 } else if (strncmp(buf, "debugon", 7) == 0) {
140                         printk(KERN_ERR "scanlog: debug on\n");
141                         scanlog_debug = 1;
142                 } else if (strncmp(buf, "debugoff", 8) == 0) {
143                         printk(KERN_ERR "scanlog: debug off\n");
144                         scanlog_debug = 0;
145                 }
146         }
147         return count;
148 }
149
150 static int scanlog_open(struct inode * inode, struct file * file)
151 {
152         struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
153         unsigned int *data = (unsigned int *)dp->data;
154
155         if (!data) {
156                 printk(KERN_ERR "scanlog: open failed no data\n");
157                 return -EIO;
158         }
159         if (data[0] != 0) {
160                 /* This imperfect test stops a second copy of the
161                  * data (or a reset while data is being copied)
162                  */
163                 return -EBUSY;
164         }
165
166         data[0] = 0;    /* re-init so we restart the scan */
167
168         return 0;
169 }
170
171 static int scanlog_release(struct inode * inode, struct file * file)
172 {
173         struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
174         unsigned int *data = (unsigned int *)dp->data;
175
176         if (!data) {
177                 printk(KERN_ERR "scanlog: release failed no data\n");
178                 return -EIO;
179         }
180         data[0] = 0;
181
182         return 0;
183 }
184
185 struct file_operations scanlog_fops = {
186         owner:          THIS_MODULE,
187         read:           scanlog_read,
188         write:          scanlog_write,
189         open:           scanlog_open,
190         release:        scanlog_release,
191 };
192
193 int __init scanlog_init(void)
194 {
195         struct proc_dir_entry *ent;
196
197         ibm_scan_log_dump = rtas_token("ibm,scan-log-dump");
198         if (ibm_scan_log_dump == RTAS_UNKNOWN_SERVICE) {
199                 printk(KERN_ERR "scan-log-dump not implemented on this system\n");
200                 return -EIO;
201         }
202
203         ent = create_proc_entry("ppc64/scan-log-dump", S_IRUSR, NULL);
204         if (ent) {
205                 ent->proc_fops = &scanlog_fops;
206                 /* Ideally we could allocate a buffer < 4G */
207                 ent->data = kmalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
208                 if (!ent->data) {
209                         printk(KERN_ERR "Failed to allocate a buffer\n");
210                         remove_proc_entry("scan-log-dump", ent->parent);
211                         return -ENOMEM;
212                 }
213                 ((unsigned int *)ent->data)[0] = 0;
214         } else {
215                 printk(KERN_ERR "Failed to create ppc64/scan-log-dump proc entry\n");
216                 return -EIO;
217         }
218         proc_ppc64_scan_log_dump = ent;
219
220         return 0;
221 }
222
223 void __exit scanlog_cleanup(void)
224 {
225         if (proc_ppc64_scan_log_dump) {
226                 if (proc_ppc64_scan_log_dump->data)
227                     kfree(proc_ppc64_scan_log_dump->data);
228                 remove_proc_entry("scan-log-dump", proc_ppc64_scan_log_dump->parent);
229         }
230 }
231
232 module_init(scanlog_init);
233 module_exit(scanlog_cleanup);
234 MODULE_LICENSE("GPL");