Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * c 2001 PPC 64 Team, IBM Corp |
| 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Benjamin Herrenschmidt | 188917e | 2009-09-24 19:29:13 +0000 | [diff] [blame] | 12 | * This driver exports /proc/powerpc/scan-log-dump which can be read. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Nishanth Aravamudan | 0287ebe | 2005-09-03 15:56:01 -0700 | [diff] [blame] | 24 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 26 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | static unsigned int ibm_scan_log_dump; /* RTAS token */ |
David Howells | 4c23782 | 2013-04-12 18:54:43 +0100 | [diff] [blame] | 40 | static unsigned int *scanlog_buffer; /* The data buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Al Viro | efa5457 | 2005-04-26 11:26:53 -0700 | [diff] [blame] | 42 | static ssize_t scanlog_read(struct file *file, char __user *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | size_t count, loff_t *ppos) |
| 44 | { |
David Howells | 4c23782 | 2013-04-12 18:54:43 +0100 | [diff] [blame] | 45 | unsigned int *data = scanlog_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | int status; |
| 47 | unsigned long len, off; |
| 48 | unsigned int wait_time; |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 62 | if (!access_ok(buf, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | return -EFAULT; |
| 64 | |
| 65 | for (;;) { |
Nishanth Aravamudan | 0287ebe | 2005-09-03 15:56:01 -0700 | [diff] [blame] | 66 | wait_time = 500; /* default wait if no data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Michael Ellerman | f7ebf35 | 2008-04-24 15:13:19 +1000 | [diff] [blame] | 74 | pr_debug("scanlog: status=%d, data[0]=%x, data[1]=%x, " \ |
| 75 | "data[2]=%x\n", status, data[0], data[1], data[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | switch (status) { |
| 77 | case SCANLOG_COMPLETE: |
Michael Ellerman | f7ebf35 | 2008-04-24 15:13:19 +1000 | [diff] [blame] | 78 | pr_debug("scanlog: hit eof\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | return 0; |
| 80 | case SCANLOG_HWERROR: |
Michael Ellerman | f7ebf35 | 2008-04-24 15:13:19 +1000 | [diff] [blame] | 81 | pr_debug("scanlog: hardware error reading data\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
John Rose | 7932f0b | 2006-06-15 17:32:15 -0500 | [diff] [blame] | 95 | /* Assume extended busy */ |
| 96 | wait_time = rtas_busy_delay_time(status); |
| 97 | if (!wait_time) { |
Michael Ellerman | f7ebf35 | 2008-04-24 15:13:19 +1000 | [diff] [blame] | 98 | printk(KERN_ERR "scanlog: unknown error " \ |
| 99 | "from rtas: %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | return -EIO; |
| 101 | } |
| 102 | } |
| 103 | /* Apparently no data yet. Wait and try again. */ |
Nishanth Aravamudan | 0287ebe | 2005-09-03 15:56:01 -0700 | [diff] [blame] | 104 | msleep_interruptible(wait_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | /*NOTREACHED*/ |
| 107 | } |
| 108 | |
Al Viro | efa5457 | 2005-04-26 11:26:53 -0700 | [diff] [blame] | 109 | static ssize_t scanlog_write(struct file * file, const char __user * buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Michael Ellerman | f7ebf35 | 2008-04-24 15:13:19 +1000 | [diff] [blame] | 123 | pr_debug("scanlog: reset scanlog\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | status = rtas_call(ibm_scan_log_dump, 2, 1, NULL, 0, 0); |
Michael Ellerman | f7ebf35 | 2008-04-24 15:13:19 +1000 | [diff] [blame] | 125 | pr_debug("scanlog: rtas returns %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | return count; |
| 129 | } |
| 130 | |
| 131 | static int scanlog_open(struct inode * inode, struct file * file) |
| 132 | { |
David Howells | 4c23782 | 2013-04-12 18:54:43 +0100 | [diff] [blame] | 133 | unsigned int *data = scanlog_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
David Howells | 4c23782 | 2013-04-12 18:54:43 +0100 | [diff] [blame] | 149 | unsigned int *data = scanlog_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | data[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
Alexey Dobriyan | 97a3253 | 2020-02-03 17:37:17 -0800 | [diff] [blame] | 155 | static const struct proc_ops scanlog_proc_ops = { |
| 156 | .proc_read = scanlog_read, |
| 157 | .proc_write = scanlog_write, |
| 158 | .proc_open = scanlog_open, |
| 159 | .proc_release = scanlog_release, |
| 160 | .proc_lseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
Arnd Bergmann | 8446196 | 2006-01-11 00:00:02 +0000 | [diff] [blame] | 163 | static int __init scanlog_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
| 165 | struct proc_dir_entry *ent; |
Nathan Lynch | f61fb8a | 2008-03-24 09:51:45 +1100 | [diff] [blame] | 166 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | ibm_scan_log_dump = rtas_token("ibm,scan-log-dump"); |
Nathan Lynch | f61fb8a | 2008-03-24 09:51:45 +1100 | [diff] [blame] | 169 | if (ibm_scan_log_dump == RTAS_UNKNOWN_SERVICE) |
| 170 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Nathan Lynch | f61fb8a | 2008-03-24 09:51:45 +1100 | [diff] [blame] | 172 | /* Ideally we could allocate a buffer < 4G */ |
David Howells | 4c23782 | 2013-04-12 18:54:43 +0100 | [diff] [blame] | 173 | scanlog_buffer = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL); |
| 174 | if (!scanlog_buffer) |
Nathan Lynch | f61fb8a | 2008-03-24 09:51:45 +1100 | [diff] [blame] | 175 | goto err; |
| 176 | |
Russell Currey | 57ad583f | 2017-01-12 14:54:13 +1100 | [diff] [blame] | 177 | ent = proc_create("powerpc/rtas/scan-log-dump", 0400, NULL, |
Alexey Dobriyan | 97a3253 | 2020-02-03 17:37:17 -0800 | [diff] [blame] | 178 | &scanlog_proc_ops); |
Nathan Lynch | f61fb8a | 2008-03-24 09:51:45 +1100 | [diff] [blame] | 179 | if (!ent) |
| 180 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | return 0; |
Nathan Lynch | f61fb8a | 2008-03-24 09:51:45 +1100 | [diff] [blame] | 182 | err: |
David Howells | 4c23782 | 2013-04-12 18:54:43 +0100 | [diff] [blame] | 183 | kfree(scanlog_buffer); |
Nathan Lynch | f61fb8a | 2008-03-24 09:51:45 +1100 | [diff] [blame] | 184 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Arnd Bergmann | 8446196 | 2006-01-11 00:00:02 +0000 | [diff] [blame] | 187 | static void __exit scanlog_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
David Howells | 4c23782 | 2013-04-12 18:54:43 +0100 | [diff] [blame] | 189 | remove_proc_entry("powerpc/rtas/scan-log-dump", NULL); |
| 190 | kfree(scanlog_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | module_init(scanlog_init); |
| 194 | module_exit(scanlog_cleanup); |
| 195 | MODULE_LICENSE("GPL"); |