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 | * |
Benjamin Herrenschmidt | 188917e | 2009-09-24 19:29:13 +0000 | [diff] [blame] | 16 | * This driver exports /proc/powerpc/scan-log-dump which can be read. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Aravamudan | 0287ebe | 2005-09-03 15:56:01 -0700 | [diff] [blame] | 28 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 30 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <asm/rtas.h> |
| 32 | #include <asm/prom.h> |
| 33 | |
| 34 | #define MODULE_VERS "1.0" |
| 35 | #define MODULE_NAME "scanlog" |
| 36 | |
| 37 | /* Status returns from ibm,scan-log-dump */ |
| 38 | #define SCANLOG_COMPLETE 0 |
| 39 | #define SCANLOG_HWERROR -1 |
| 40 | #define SCANLOG_CONTINUE 1 |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | static unsigned int ibm_scan_log_dump; /* RTAS token */ |
David Howells | 4c23782 | 2013-04-12 18:54:43 +0100 | [diff] [blame] | 44 | static unsigned int *scanlog_buffer; /* The data buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Al Viro | efa5457 | 2005-04-26 11:26:53 -0700 | [diff] [blame] | 46 | static ssize_t scanlog_read(struct file *file, char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | size_t count, loff_t *ppos) |
| 48 | { |
David Howells | 4c23782 | 2013-04-12 18:54:43 +0100 | [diff] [blame] | 49 | unsigned int *data = scanlog_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | int status; |
| 51 | unsigned long len, off; |
| 52 | unsigned int wait_time; |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | if (count > RTAS_DATA_BUF_SIZE) |
| 55 | count = RTAS_DATA_BUF_SIZE; |
| 56 | |
| 57 | if (count < 1024) { |
| 58 | /* This is the min supported by this RTAS call. Rather |
| 59 | * than do all the buffering we insist the user code handle |
| 60 | * larger reads. As long as cp works... :) |
| 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 (;;) { |
Nishanth Aravamudan | 0287ebe | 2005-09-03 15:56:01 -0700 | [diff] [blame] | 70 | wait_time = 500; /* default wait if no data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Michael Ellerman | f7ebf35 | 2008-04-24 15:13:19 +1000 | [diff] [blame] | 78 | pr_debug("scanlog: status=%d, data[0]=%x, data[1]=%x, " \ |
| 79 | "data[2]=%x\n", status, data[0], data[1], data[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | switch (status) { |
| 81 | case SCANLOG_COMPLETE: |
Michael Ellerman | f7ebf35 | 2008-04-24 15:13:19 +1000 | [diff] [blame] | 82 | pr_debug("scanlog: hit eof\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return 0; |
| 84 | case SCANLOG_HWERROR: |
Michael Ellerman | f7ebf35 | 2008-04-24 15:13:19 +1000 | [diff] [blame] | 85 | pr_debug("scanlog: hardware error reading data\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return -EIO; |
| 87 | case SCANLOG_CONTINUE: |
| 88 | /* We may or may not have data yet */ |
| 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 | /* Break to sleep default time */ |
| 97 | break; |
| 98 | default: |
John Rose | 7932f0b | 2006-06-15 17:32:15 -0500 | [diff] [blame] | 99 | /* Assume extended busy */ |
| 100 | wait_time = rtas_busy_delay_time(status); |
| 101 | if (!wait_time) { |
Michael Ellerman | f7ebf35 | 2008-04-24 15:13:19 +1000 | [diff] [blame] | 102 | printk(KERN_ERR "scanlog: unknown error " \ |
| 103 | "from rtas: %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | return -EIO; |
| 105 | } |
| 106 | } |
| 107 | /* Apparently no data yet. Wait and try again. */ |
Nishanth Aravamudan | 0287ebe | 2005-09-03 15:56:01 -0700 | [diff] [blame] | 108 | msleep_interruptible(wait_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
| 110 | /*NOTREACHED*/ |
| 111 | } |
| 112 | |
Al Viro | efa5457 | 2005-04-26 11:26:53 -0700 | [diff] [blame] | 113 | static ssize_t scanlog_write(struct file * file, const char __user * buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Michael Ellerman | f7ebf35 | 2008-04-24 15:13:19 +1000 | [diff] [blame] | 127 | pr_debug("scanlog: reset scanlog\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | status = rtas_call(ibm_scan_log_dump, 2, 1, NULL, 0, 0); |
Michael Ellerman | f7ebf35 | 2008-04-24 15:13:19 +1000 | [diff] [blame] | 129 | pr_debug("scanlog: rtas returns %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | return count; |
| 133 | } |
| 134 | |
| 135 | static int scanlog_open(struct inode * inode, struct file * file) |
| 136 | { |
David Howells | 4c23782 | 2013-04-12 18:54:43 +0100 | [diff] [blame] | 137 | unsigned int *data = scanlog_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | if (data[0] != 0) { |
| 140 | /* This imperfect test stops a second copy of the |
| 141 | * data (or a reset while data is being copied) |
| 142 | */ |
| 143 | return -EBUSY; |
| 144 | } |
| 145 | |
| 146 | data[0] = 0; /* re-init so we restart the scan */ |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static int scanlog_release(struct inode * inode, struct file * file) |
| 152 | { |
David Howells | 4c23782 | 2013-04-12 18:54:43 +0100 | [diff] [blame] | 153 | unsigned int *data = scanlog_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | data[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
Daniel Axtens | 7c98bd7 | 2016-09-06 15:32:40 +1000 | [diff] [blame] | 159 | static const struct file_operations scanlog_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | .owner = THIS_MODULE, |
| 161 | .read = scanlog_read, |
| 162 | .write = scanlog_write, |
| 163 | .open = scanlog_open, |
| 164 | .release = scanlog_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 165 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | }; |
| 167 | |
Arnd Bergmann | 8446196 | 2006-01-11 00:00:02 +0000 | [diff] [blame] | 168 | static int __init scanlog_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
| 170 | struct proc_dir_entry *ent; |
Nathan Lynch | f61fb8a | 2008-03-24 09:51:45 +1100 | [diff] [blame] | 171 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | ibm_scan_log_dump = rtas_token("ibm,scan-log-dump"); |
Nathan Lynch | f61fb8a | 2008-03-24 09:51:45 +1100 | [diff] [blame] | 174 | if (ibm_scan_log_dump == RTAS_UNKNOWN_SERVICE) |
| 175 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Nathan Lynch | f61fb8a | 2008-03-24 09:51:45 +1100 | [diff] [blame] | 177 | /* Ideally we could allocate a buffer < 4G */ |
David Howells | 4c23782 | 2013-04-12 18:54:43 +0100 | [diff] [blame] | 178 | scanlog_buffer = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL); |
| 179 | if (!scanlog_buffer) |
Nathan Lynch | f61fb8a | 2008-03-24 09:51:45 +1100 | [diff] [blame] | 180 | goto err; |
| 181 | |
David Howells | 4c23782 | 2013-04-12 18:54:43 +0100 | [diff] [blame] | 182 | ent = proc_create("powerpc/rtas/scan-log-dump", S_IRUSR, NULL, |
| 183 | &scanlog_fops); |
Nathan Lynch | f61fb8a | 2008-03-24 09:51:45 +1100 | [diff] [blame] | 184 | if (!ent) |
| 185 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | return 0; |
Nathan Lynch | f61fb8a | 2008-03-24 09:51:45 +1100 | [diff] [blame] | 187 | err: |
David Howells | 4c23782 | 2013-04-12 18:54:43 +0100 | [diff] [blame] | 188 | kfree(scanlog_buffer); |
Nathan Lynch | f61fb8a | 2008-03-24 09:51:45 +1100 | [diff] [blame] | 189 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Arnd Bergmann | 8446196 | 2006-01-11 00:00:02 +0000 | [diff] [blame] | 192 | static void __exit scanlog_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
David Howells | 4c23782 | 2013-04-12 18:54:43 +0100 | [diff] [blame] | 194 | remove_proc_entry("powerpc/rtas/scan-log-dump", NULL); |
| 195 | kfree(scanlog_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | module_init(scanlog_init); |
| 199 | module_exit(scanlog_cleanup); |
| 200 | MODULE_LICENSE("GPL"); |