1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
38#define SCANLOG_COMPLETE 0
39#define SCANLOG_HWERROR -1
40#define SCANLOG_CONTINUE 1
41
42
43static unsigned int ibm_scan_log_dump;
44static unsigned int *scanlog_buffer;
45
46static ssize_t scanlog_read(struct file *file, char __user *buf,
47 size_t count, loff_t *ppos)
48{
49 unsigned int *data = scanlog_buffer;
50 int status;
51 unsigned long len, off;
52 unsigned int wait_time;
53
54 if (count > RTAS_DATA_BUF_SIZE)
55 count = RTAS_DATA_BUF_SIZE;
56
57 if (count < 1024) {
58
59
60
61
62 printk(KERN_ERR "scanlog: cannot perform a small read (%ld)\n", count);
63 return -EINVAL;
64 }
65
66 if (!access_ok(VERIFY_WRITE, buf, count))
67 return -EFAULT;
68
69 for (;;) {
70 wait_time = 500;
71 spin_lock(&rtas_data_buf_lock);
72 memcpy(rtas_data_buf, data, RTAS_DATA_BUF_SIZE);
73 status = rtas_call(ibm_scan_log_dump, 2, 1, NULL,
74 (u32) __pa(rtas_data_buf), (u32) count);
75 memcpy(data, rtas_data_buf, RTAS_DATA_BUF_SIZE);
76 spin_unlock(&rtas_data_buf_lock);
77
78 pr_debug("scanlog: status=%d, data[0]=%x, data[1]=%x, " \
79 "data[2]=%x\n", status, data[0], data[1], data[2]);
80 switch (status) {
81 case SCANLOG_COMPLETE:
82 pr_debug("scanlog: hit eof\n");
83 return 0;
84 case SCANLOG_HWERROR:
85 pr_debug("scanlog: hardware error reading data\n");
86 return -EIO;
87 case SCANLOG_CONTINUE:
88
89 len = data[1];
90 off = data[2];
91 if (len > 0) {
92 if (copy_to_user(buf, ((char *)data)+off, len))
93 return -EFAULT;
94 return len;
95 }
96
97 break;
98 default:
99
100 wait_time = rtas_busy_delay_time(status);
101 if (!wait_time) {
102 printk(KERN_ERR "scanlog: unknown error " \
103 "from rtas: %d\n", status);
104 return -EIO;
105 }
106 }
107
108 msleep_interruptible(wait_time);
109 }
110
111}
112
113static ssize_t scanlog_write(struct file * file, const char __user * buf,
114 size_t count, loff_t *ppos)
115{
116 char stkbuf[20];
117 int status;
118
119 if (count > 19) count = 19;
120 if (copy_from_user (stkbuf, buf, count)) {
121 return -EFAULT;
122 }
123 stkbuf[count] = 0;
124
125 if (buf) {
126 if (strncmp(stkbuf, "reset", 5) == 0) {
127 pr_debug("scanlog: reset scanlog\n");
128 status = rtas_call(ibm_scan_log_dump, 2, 1, NULL, 0, 0);
129 pr_debug("scanlog: rtas returns %d\n", status);
130 }
131 }
132 return count;
133}
134
135static int scanlog_open(struct inode * inode, struct file * file)
136{
137 unsigned int *data = scanlog_buffer;
138
139 if (data[0] != 0) {
140
141
142
143 return -EBUSY;
144 }
145
146 data[0] = 0;
147
148 return 0;
149}
150
151static int scanlog_release(struct inode * inode, struct file * file)
152{
153 unsigned int *data = scanlog_buffer;
154
155 data[0] = 0;
156 return 0;
157}
158
159const struct file_operations scanlog_fops = {
160 .owner = THIS_MODULE,
161 .read = scanlog_read,
162 .write = scanlog_write,
163 .open = scanlog_open,
164 .release = scanlog_release,
165 .llseek = noop_llseek,
166};
167
168static int __init scanlog_init(void)
169{
170 struct proc_dir_entry *ent;
171 int err = -ENOMEM;
172
173 ibm_scan_log_dump = rtas_token("ibm,scan-log-dump");
174 if (ibm_scan_log_dump == RTAS_UNKNOWN_SERVICE)
175 return -ENODEV;
176
177
178 scanlog_buffer = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
179 if (!scanlog_buffer)
180 goto err;
181
182 ent = proc_create("powerpc/rtas/scan-log-dump", S_IRUSR, NULL,
183 &scanlog_fops);
184 if (!ent)
185 goto err;
186 return 0;
187err:
188 kfree(scanlog_buffer);
189 return err;
190}
191
192static void __exit scanlog_cleanup(void)
193{
194 remove_proc_entry("powerpc/rtas/scan-log-dump", NULL);
195 kfree(scanlog_buffer);
196}
197
198module_init(scanlog_init);
199module_exit(scanlog_cleanup);
200MODULE_LICENSE("GPL");
201