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 <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
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
42static int scanlog_debug;
43static unsigned int ibm_scan_log_dump;
44static struct proc_dir_entry *proc_ppc64_scan_log_dump;
45
46
47static 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
66
67
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;
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
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
104 break;
105 default:
106 if (status > 9900 && status <= 9905) {
107
108
109
110 int ms = 1;
111 for (; status > 9900; status--)
112 ms = ms * 10;
113
114 ms *= 1000;
115 wait_time = ms / (1000000/HZ);
116
117 } else {
118 printk(KERN_ERR "scanlog: unknown error from rtas: %ld\n", status);
119 return -EIO;
120 }
121 }
122
123 set_current_state(TASK_INTERRUPTIBLE);
124 schedule_timeout(wait_time);
125 }
126
127}
128
129static ssize_t scanlog_write(struct file * file, const char * buf,
130 size_t count, loff_t *ppos)
131{
132 char stkbuf[20];
133 unsigned long status;
134
135 if (count > 19)
136 count = 19;
137 if (copy_from_user (stkbuf, buf, count))
138 return -EFAULT;
139
140 stkbuf[count] = 0;
141
142 if (buf) {
143 if (strncmp(stkbuf, "reset", 5) == 0) {
144 DEBUG("reset scanlog\n");
145 status = rtas_call(ibm_scan_log_dump, 2, 1, NULL, NULL, 0);
146 DEBUG("rtas returns %ld\n", status);
147 } else if (strncmp(stkbuf, "debugon", 7) == 0) {
148 printk(KERN_ERR "scanlog: debug on\n");
149 scanlog_debug = 1;
150 } else if (strncmp(stkbuf, "debugoff", 8) == 0) {
151 printk(KERN_ERR "scanlog: debug off\n");
152 scanlog_debug = 0;
153 }
154 }
155 return count;
156}
157
158static int scanlog_open(struct inode * inode, struct file * file)
159{
160 struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
161 unsigned int *data = (unsigned int *)dp->data;
162
163 if (!data) {
164 printk(KERN_ERR "scanlog: open failed no data\n");
165 return -EIO;
166 }
167 if (data[0] != 0) {
168
169
170
171 return -EBUSY;
172 }
173
174 data[0] = 0;
175
176 return 0;
177}
178
179static int scanlog_release(struct inode * inode, struct file * file)
180{
181 struct proc_dir_entry *dp = file->f_dentry->d_inode->u.generic_ip;
182 unsigned int *data = (unsigned int *)dp->data;
183
184 if (!data) {
185 printk(KERN_ERR "scanlog: release failed no data\n");
186 return -EIO;
187 }
188 data[0] = 0;
189
190 return 0;
191}
192
193struct file_operations scanlog_fops = {
194 owner: THIS_MODULE,
195 read: scanlog_read,
196 write: scanlog_write,
197 open: scanlog_open,
198 release: scanlog_release,
199};
200
201int __init scanlog_init(void)
202{
203 struct proc_dir_entry *ent;
204
205 ibm_scan_log_dump = rtas_token("ibm,scan-log-dump");
206 if (ibm_scan_log_dump == RTAS_UNKNOWN_SERVICE) {
207 printk(KERN_ERR "scan-log-dump not implemented on this system\n");
208 return -EIO;
209 }
210
211 ent = create_proc_entry("ppc64/scan-log-dump", S_IRUSR, NULL);
212 if (ent) {
213 ent->proc_fops = &scanlog_fops;
214
215 ent->data = kmalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
216 if (!ent->data) {
217 printk(KERN_ERR "Failed to allocate a buffer\n");
218 remove_proc_entry("scan-log-dump", ent->parent);
219 return -ENOMEM;
220 }
221 ((unsigned int *)ent->data)[0] = 0;
222 } else {
223 printk(KERN_ERR "Failed to create ppc64/scan-log-dump proc entry\n");
224 return -EIO;
225 }
226 proc_ppc64_scan_log_dump = ent;
227
228 return 0;
229}
230
231void __exit scanlog_cleanup(void)
232{
233 if (proc_ppc64_scan_log_dump) {
234 if (proc_ppc64_scan_log_dump->data)
235 kfree(proc_ppc64_scan_log_dump->data);
236 remove_proc_entry("scan-log-dump", proc_ppc64_scan_log_dump->parent);
237 }
238}
239
240module_init(scanlog_init);
241module_exit(scanlog_cleanup);
242MODULE_LICENSE("GPL");
243