blob: e5b0ea87016495743f6dac0856da16d277a8fc03 [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 *
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>
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
41#define DEBUG(A...) do { if (scanlog_debug) printk(KERN_ERR "scanlog: " A); } while (0)
42
43static int scanlog_debug;
44static unsigned int ibm_scan_log_dump; /* RTAS token */
45static struct proc_dir_entry *proc_ppc64_scan_log_dump; /* The proc file */
46
Al Viroefa54572005-04-26 11:26:53 -070047static ssize_t scanlog_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 size_t count, loff_t *ppos)
49{
Josef Sipekb4d1ab52006-12-08 02:37:30 -080050 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 struct proc_dir_entry *dp;
52 unsigned int *data;
53 int status;
54 unsigned long len, off;
55 unsigned int wait_time;
56
57 dp = PDE(inode);
58 data = (unsigned int *)dp->data;
59
60 if (!data) {
61 printk(KERN_ERR "scanlog: read failed no data\n");
62 return -EIO;
63 }
64
65 if (count > RTAS_DATA_BUF_SIZE)
66 count = RTAS_DATA_BUF_SIZE;
67
68 if (count < 1024) {
69 /* This is the min supported by this RTAS call. Rather
70 * than do all the buffering we insist the user code handle
71 * larger reads. As long as cp works... :)
72 */
73 printk(KERN_ERR "scanlog: cannot perform a small read (%ld)\n", count);
74 return -EINVAL;
75 }
76
77 if (!access_ok(VERIFY_WRITE, buf, count))
78 return -EFAULT;
79
80 for (;;) {
Nishanth Aravamudan0287ebe2005-09-03 15:56:01 -070081 wait_time = 500; /* default wait if no data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 spin_lock(&rtas_data_buf_lock);
83 memcpy(rtas_data_buf, data, RTAS_DATA_BUF_SIZE);
84 status = rtas_call(ibm_scan_log_dump, 2, 1, NULL,
85 (u32) __pa(rtas_data_buf), (u32) count);
86 memcpy(data, rtas_data_buf, RTAS_DATA_BUF_SIZE);
87 spin_unlock(&rtas_data_buf_lock);
88
89 DEBUG("status=%d, data[0]=%x, data[1]=%x, data[2]=%x\n",
90 status, data[0], data[1], data[2]);
91 switch (status) {
92 case SCANLOG_COMPLETE:
93 DEBUG("hit eof\n");
94 return 0;
95 case SCANLOG_HWERROR:
96 DEBUG("hardware error reading scan log data\n");
97 return -EIO;
98 case SCANLOG_CONTINUE:
99 /* We may or may not have data yet */
100 len = data[1];
101 off = data[2];
102 if (len > 0) {
103 if (copy_to_user(buf, ((char *)data)+off, len))
104 return -EFAULT;
105 return len;
106 }
107 /* Break to sleep default time */
108 break;
109 default:
John Rose7932f0b2006-06-15 17:32:15 -0500110 /* Assume extended busy */
111 wait_time = rtas_busy_delay_time(status);
112 if (!wait_time) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 printk(KERN_ERR "scanlog: unknown error from rtas: %d\n", status);
114 return -EIO;
115 }
116 }
117 /* Apparently no data yet. Wait and try again. */
Nishanth Aravamudan0287ebe2005-09-03 15:56:01 -0700118 msleep_interruptible(wait_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120 /*NOTREACHED*/
121}
122
Al Viroefa54572005-04-26 11:26:53 -0700123static ssize_t scanlog_write(struct file * file, const char __user * buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 size_t count, loff_t *ppos)
125{
126 char stkbuf[20];
127 int status;
128
129 if (count > 19) count = 19;
130 if (copy_from_user (stkbuf, buf, count)) {
131 return -EFAULT;
132 }
133 stkbuf[count] = 0;
134
135 if (buf) {
136 if (strncmp(stkbuf, "reset", 5) == 0) {
137 DEBUG("reset scanlog\n");
138 status = rtas_call(ibm_scan_log_dump, 2, 1, NULL, 0, 0);
139 DEBUG("rtas returns %d\n", status);
140 } else if (strncmp(stkbuf, "debugon", 7) == 0) {
141 printk(KERN_ERR "scanlog: debug on\n");
142 scanlog_debug = 1;
143 } else if (strncmp(stkbuf, "debugoff", 8) == 0) {
144 printk(KERN_ERR "scanlog: debug off\n");
145 scanlog_debug = 0;
146 }
147 }
148 return count;
149}
150
151static int scanlog_open(struct inode * inode, struct file * file)
152{
153 struct proc_dir_entry *dp = PDE(inode);
154 unsigned int *data = (unsigned int *)dp->data;
155
156 if (!data) {
157 printk(KERN_ERR "scanlog: open failed no data\n");
158 return -EIO;
159 }
160
161 if (data[0] != 0) {
162 /* This imperfect test stops a second copy of the
163 * data (or a reset while data is being copied)
164 */
165 return -EBUSY;
166 }
167
168 data[0] = 0; /* re-init so we restart the scan */
169
170 return 0;
171}
172
173static int scanlog_release(struct inode * inode, struct file * file)
174{
175 struct proc_dir_entry *dp = PDE(inode);
176 unsigned int *data = (unsigned int *)dp->data;
177
178 if (!data) {
179 printk(KERN_ERR "scanlog: release failed no data\n");
180 return -EIO;
181 }
182 data[0] = 0;
183
184 return 0;
185}
186
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800187const struct file_operations scanlog_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 .owner = THIS_MODULE,
189 .read = scanlog_read,
190 .write = scanlog_write,
191 .open = scanlog_open,
192 .release = scanlog_release,
193};
194
Arnd Bergmann84461962006-01-11 00:00:02 +0000195static int __init scanlog_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 struct proc_dir_entry *ent;
Nathan Lynchf61fb8a2008-03-24 09:51:45 +1100198 void *data;
199 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 ibm_scan_log_dump = rtas_token("ibm,scan-log-dump");
Nathan Lynchf61fb8a2008-03-24 09:51:45 +1100202 if (ibm_scan_log_dump == RTAS_UNKNOWN_SERVICE)
203 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Nathan Lynchf61fb8a2008-03-24 09:51:45 +1100205 /* Ideally we could allocate a buffer < 4G */
206 data = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
207 if (!data)
208 goto err;
209
210 ent = proc_create("ppc64/rtas/scan-log-dump", S_IRUSR, NULL,
211 &scanlog_fops);
212 if (!ent)
213 goto err;
214
215 ent->data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 proc_ppc64_scan_log_dump = ent;
217
218 return 0;
Nathan Lynchf61fb8a2008-03-24 09:51:45 +1100219err:
220 kfree(data);
221 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Arnd Bergmann84461962006-01-11 00:00:02 +0000224static void __exit scanlog_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 if (proc_ppc64_scan_log_dump) {
Jesper Juhlb2325fe2005-11-07 01:01:35 -0800227 kfree(proc_ppc64_scan_log_dump->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 remove_proc_entry("scan-log-dump", proc_ppc64_scan_log_dump->parent);
229 }
230}
231
232module_init(scanlog_init);
233module_exit(scanlog_cleanup);
234MODULE_LICENSE("GPL");