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