]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * c 2001 PPC 64 Team, IBM Corp | |
4 | * | |
1da177e4 LT |
5 | * scan-log-data driver for PPC64 Todd Inglett <tinglett@vnet.ibm.com> |
6 | * | |
7 | * When ppc64 hardware fails the service processor dumps internal state | |
8 | * of the system. After a reboot the operating system can access a dump | |
9 | * of this data using this driver. A dump exists if the device-tree | |
10 | * /chosen/ibm,scan-log-data property exists. | |
11 | * | |
188917e1 | 12 | * This driver exports /proc/powerpc/scan-log-dump which can be read. |
1da177e4 LT |
13 | * The driver supports only sequential reads. |
14 | * | |
15 | * The driver looks at a write to the driver for the single word "reset". | |
16 | * If given, the driver will reset the scanlog so the platform can free it. | |
17 | */ | |
18 | ||
19 | #include <linux/module.h> | |
20 | #include <linux/types.h> | |
21 | #include <linux/errno.h> | |
22 | #include <linux/proc_fs.h> | |
23 | #include <linux/init.h> | |
0287ebed | 24 | #include <linux/delay.h> |
5a0e3ad6 | 25 | #include <linux/slab.h> |
7c0f6ba6 | 26 | #include <linux/uaccess.h> |
1da177e4 LT |
27 | #include <asm/rtas.h> |
28 | #include <asm/prom.h> | |
29 | ||
30 | #define MODULE_VERS "1.0" | |
31 | #define MODULE_NAME "scanlog" | |
32 | ||
33 | /* Status returns from ibm,scan-log-dump */ | |
34 | #define SCANLOG_COMPLETE 0 | |
35 | #define SCANLOG_HWERROR -1 | |
36 | #define SCANLOG_CONTINUE 1 | |
37 | ||
1da177e4 | 38 | |
1da177e4 | 39 | static unsigned int ibm_scan_log_dump; /* RTAS token */ |
4c237820 | 40 | static unsigned int *scanlog_buffer; /* The data buffer */ |
1da177e4 | 41 | |
efa54579 | 42 | static ssize_t scanlog_read(struct file *file, char __user *buf, |
1da177e4 LT |
43 | size_t count, loff_t *ppos) |
44 | { | |
4c237820 | 45 | unsigned int *data = scanlog_buffer; |
1da177e4 LT |
46 | int status; |
47 | unsigned long len, off; | |
48 | unsigned int wait_time; | |
49 | ||
1da177e4 LT |
50 | if (count > RTAS_DATA_BUF_SIZE) |
51 | count = RTAS_DATA_BUF_SIZE; | |
52 | ||
53 | if (count < 1024) { | |
54 | /* This is the min supported by this RTAS call. Rather | |
55 | * than do all the buffering we insist the user code handle | |
56 | * larger reads. As long as cp works... :) | |
57 | */ | |
58 | printk(KERN_ERR "scanlog: cannot perform a small read (%ld)\n", count); | |
59 | return -EINVAL; | |
60 | } | |
61 | ||
96d4f267 | 62 | if (!access_ok(buf, count)) |
1da177e4 LT |
63 | return -EFAULT; |
64 | ||
65 | for (;;) { | |
0287ebed | 66 | wait_time = 500; /* default wait if no data */ |
1da177e4 LT |
67 | spin_lock(&rtas_data_buf_lock); |
68 | memcpy(rtas_data_buf, data, RTAS_DATA_BUF_SIZE); | |
69 | status = rtas_call(ibm_scan_log_dump, 2, 1, NULL, | |
70 | (u32) __pa(rtas_data_buf), (u32) count); | |
71 | memcpy(data, rtas_data_buf, RTAS_DATA_BUF_SIZE); | |
72 | spin_unlock(&rtas_data_buf_lock); | |
73 | ||
f7ebf352 ME |
74 | pr_debug("scanlog: status=%d, data[0]=%x, data[1]=%x, " \ |
75 | "data[2]=%x\n", status, data[0], data[1], data[2]); | |
1da177e4 LT |
76 | switch (status) { |
77 | case SCANLOG_COMPLETE: | |
f7ebf352 | 78 | pr_debug("scanlog: hit eof\n"); |
1da177e4 LT |
79 | return 0; |
80 | case SCANLOG_HWERROR: | |
f7ebf352 | 81 | pr_debug("scanlog: hardware error reading data\n"); |
1da177e4 LT |
82 | return -EIO; |
83 | case SCANLOG_CONTINUE: | |
84 | /* We may or may not have data yet */ | |
85 | len = data[1]; | |
86 | off = data[2]; | |
87 | if (len > 0) { | |
88 | if (copy_to_user(buf, ((char *)data)+off, len)) | |
89 | return -EFAULT; | |
90 | return len; | |
91 | } | |
92 | /* Break to sleep default time */ | |
93 | break; | |
94 | default: | |
7932f0b8 JR |
95 | /* Assume extended busy */ |
96 | wait_time = rtas_busy_delay_time(status); | |
97 | if (!wait_time) { | |
f7ebf352 ME |
98 | printk(KERN_ERR "scanlog: unknown error " \ |
99 | "from rtas: %d\n", status); | |
1da177e4 LT |
100 | return -EIO; |
101 | } | |
102 | } | |
103 | /* Apparently no data yet. Wait and try again. */ | |
0287ebed | 104 | msleep_interruptible(wait_time); |
1da177e4 LT |
105 | } |
106 | /*NOTREACHED*/ | |
107 | } | |
108 | ||
efa54579 | 109 | static ssize_t scanlog_write(struct file * file, const char __user * buf, |
1da177e4 LT |
110 | size_t count, loff_t *ppos) |
111 | { | |
112 | char stkbuf[20]; | |
113 | int status; | |
114 | ||
115 | if (count > 19) count = 19; | |
116 | if (copy_from_user (stkbuf, buf, count)) { | |
117 | return -EFAULT; | |
118 | } | |
119 | stkbuf[count] = 0; | |
120 | ||
121 | if (buf) { | |
122 | if (strncmp(stkbuf, "reset", 5) == 0) { | |
f7ebf352 | 123 | pr_debug("scanlog: reset scanlog\n"); |
1da177e4 | 124 | status = rtas_call(ibm_scan_log_dump, 2, 1, NULL, 0, 0); |
f7ebf352 | 125 | pr_debug("scanlog: rtas returns %d\n", status); |
1da177e4 LT |
126 | } |
127 | } | |
128 | return count; | |
129 | } | |
130 | ||
131 | static int scanlog_open(struct inode * inode, struct file * file) | |
132 | { | |
4c237820 | 133 | unsigned int *data = scanlog_buffer; |
1da177e4 | 134 | |
1da177e4 LT |
135 | if (data[0] != 0) { |
136 | /* This imperfect test stops a second copy of the | |
137 | * data (or a reset while data is being copied) | |
138 | */ | |
139 | return -EBUSY; | |
140 | } | |
141 | ||
142 | data[0] = 0; /* re-init so we restart the scan */ | |
143 | ||
144 | return 0; | |
145 | } | |
146 | ||
147 | static int scanlog_release(struct inode * inode, struct file * file) | |
148 | { | |
4c237820 | 149 | unsigned int *data = scanlog_buffer; |
1da177e4 | 150 | |
1da177e4 | 151 | data[0] = 0; |
1da177e4 LT |
152 | return 0; |
153 | } | |
154 | ||
7c98bd72 | 155 | static const struct file_operations scanlog_fops = { |
1da177e4 LT |
156 | .owner = THIS_MODULE, |
157 | .read = scanlog_read, | |
158 | .write = scanlog_write, | |
159 | .open = scanlog_open, | |
160 | .release = scanlog_release, | |
6038f373 | 161 | .llseek = noop_llseek, |
1da177e4 LT |
162 | }; |
163 | ||
8446196a | 164 | static int __init scanlog_init(void) |
1da177e4 LT |
165 | { |
166 | struct proc_dir_entry *ent; | |
f61fb8a5 | 167 | int err = -ENOMEM; |
1da177e4 LT |
168 | |
169 | ibm_scan_log_dump = rtas_token("ibm,scan-log-dump"); | |
f61fb8a5 NL |
170 | if (ibm_scan_log_dump == RTAS_UNKNOWN_SERVICE) |
171 | return -ENODEV; | |
1da177e4 | 172 | |
f61fb8a5 | 173 | /* Ideally we could allocate a buffer < 4G */ |
4c237820 DH |
174 | scanlog_buffer = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL); |
175 | if (!scanlog_buffer) | |
f61fb8a5 NL |
176 | goto err; |
177 | ||
57ad583f | 178 | ent = proc_create("powerpc/rtas/scan-log-dump", 0400, NULL, |
4c237820 | 179 | &scanlog_fops); |
f61fb8a5 NL |
180 | if (!ent) |
181 | goto err; | |
1da177e4 | 182 | return 0; |
f61fb8a5 | 183 | err: |
4c237820 | 184 | kfree(scanlog_buffer); |
f61fb8a5 | 185 | return err; |
1da177e4 LT |
186 | } |
187 | ||
8446196a | 188 | static void __exit scanlog_cleanup(void) |
1da177e4 | 189 | { |
4c237820 DH |
190 | remove_proc_entry("powerpc/rtas/scan-log-dump", NULL); |
191 | kfree(scanlog_buffer); | |
1da177e4 LT |
192 | } |
193 | ||
194 | module_init(scanlog_init); | |
195 | module_exit(scanlog_cleanup); | |
196 | MODULE_LICENSE("GPL"); |