Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 1 | /* |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 2 | * Copyright IBM Corp. 2004, 2010 |
Christian Borntraeger | 45af3af | 2006-09-20 15:59:24 +0200 | [diff] [blame] | 3 | * Interface implementation for communication with the z/VM control program |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 4 | * |
Heiko Carstens | f73a2b0 | 2010-05-17 10:00:06 +0200 | [diff] [blame] | 5 | * Author(s): Christian Borntraeger <borntraeger@de.ibm.com> |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 6 | * |
| 7 | * z/VMs CP offers the possibility to issue commands via the diagnose code 8 |
| 8 | * this driver implements a character device that issues these commands and |
| 9 | * returns the answer of CP. |
Heiko Carstens | f73a2b0 | 2010-05-17 10:00:06 +0200 | [diff] [blame] | 10 | * |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 11 | * The idea of this driver is based on cpint from Neale Ferguson and #CP in CMS |
| 12 | */ |
| 13 | |
| 14 | #include <linux/fs.h> |
| 15 | #include <linux/init.h> |
Heiko Carstens | 048cd4e | 2012-02-27 10:01:52 +0100 | [diff] [blame] | 16 | #include <linux/compat.h> |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 17 | #include <linux/kernel.h> |
| 18 | #include <linux/miscdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Heiko Carstens | 3a4c5d5 | 2011-07-30 09:25:15 +0200 | [diff] [blame] | 20 | #include <linux/export.h> |
Heiko Carstens | 8f3eabe | 2010-01-13 20:44:31 +0100 | [diff] [blame] | 21 | #include <asm/compat.h> |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 22 | #include <asm/cpcmd.h> |
| 23 | #include <asm/debug.h> |
| 24 | #include <asm/uaccess.h> |
| 25 | #include "vmcp.h" |
| 26 | |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 27 | static debug_info_t *vmcp_debug; |
| 28 | |
| 29 | static int vmcp_open(struct inode *inode, struct file *file) |
| 30 | { |
| 31 | struct vmcp_session *session; |
| 32 | |
| 33 | if (!capable(CAP_SYS_ADMIN)) |
| 34 | return -EPERM; |
| 35 | |
| 36 | session = kmalloc(sizeof(*session), GFP_KERNEL); |
| 37 | if (!session) |
| 38 | return -ENOMEM; |
Arnd Bergmann | 6c111d8 | 2008-05-20 19:16:59 +0200 | [diff] [blame] | 39 | |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 40 | session->bufsize = PAGE_SIZE; |
| 41 | session->response = NULL; |
| 42 | session->resp_size = 0; |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 43 | mutex_init(&session->mutex); |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 44 | file->private_data = session; |
| 45 | return nonseekable_open(inode, file); |
| 46 | } |
| 47 | |
| 48 | static int vmcp_release(struct inode *inode, struct file *file) |
| 49 | { |
| 50 | struct vmcp_session *session; |
| 51 | |
Joe Perches | 27e4994 | 2010-09-04 18:52:45 -0700 | [diff] [blame] | 52 | session = file->private_data; |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 53 | file->private_data = NULL; |
| 54 | free_pages((unsigned long)session->response, get_order(session->bufsize)); |
| 55 | kfree(session); |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static ssize_t |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 60 | vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos) |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 61 | { |
Akinobu Mita | 7785857 | 2008-07-14 09:59:14 +0200 | [diff] [blame] | 62 | ssize_t ret; |
| 63 | size_t size; |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 64 | struct vmcp_session *session; |
| 65 | |
Akinobu Mita | 7785857 | 2008-07-14 09:59:14 +0200 | [diff] [blame] | 66 | session = file->private_data; |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 67 | if (mutex_lock_interruptible(&session->mutex)) |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 68 | return -ERESTARTSYS; |
| 69 | if (!session->response) { |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 70 | mutex_unlock(&session->mutex); |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 71 | return 0; |
| 72 | } |
Akinobu Mita | 7785857 | 2008-07-14 09:59:14 +0200 | [diff] [blame] | 73 | size = min_t(size_t, session->resp_size, session->bufsize); |
| 74 | ret = simple_read_from_buffer(buff, count, ppos, |
| 75 | session->response, size); |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 76 | |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 77 | mutex_unlock(&session->mutex); |
Akinobu Mita | 7785857 | 2008-07-14 09:59:14 +0200 | [diff] [blame] | 78 | |
| 79 | return ret; |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static ssize_t |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 83 | vmcp_write(struct file *file, const char __user *buff, size_t count, |
| 84 | loff_t *ppos) |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 85 | { |
| 86 | char *cmd; |
| 87 | struct vmcp_session *session; |
| 88 | |
| 89 | if (count > 240) |
| 90 | return -EINVAL; |
| 91 | cmd = kmalloc(count + 1, GFP_KERNEL); |
| 92 | if (!cmd) |
| 93 | return -ENOMEM; |
| 94 | if (copy_from_user(cmd, buff, count)) { |
| 95 | kfree(cmd); |
| 96 | return -EFAULT; |
| 97 | } |
| 98 | cmd[count] = '\0'; |
Joe Perches | 27e4994 | 2010-09-04 18:52:45 -0700 | [diff] [blame] | 99 | session = file->private_data; |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 100 | if (mutex_lock_interruptible(&session->mutex)) { |
Christian Borntraeger | a5da866 | 2005-11-07 00:59:12 -0800 | [diff] [blame] | 101 | kfree(cmd); |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 102 | return -ERESTARTSYS; |
Christian Borntraeger | a5da866 | 2005-11-07 00:59:12 -0800 | [diff] [blame] | 103 | } |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 104 | if (!session->response) |
| 105 | session->response = (char *)__get_free_pages(GFP_KERNEL |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 106 | | __GFP_REPEAT | GFP_DMA, |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 107 | get_order(session->bufsize)); |
| 108 | if (!session->response) { |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 109 | mutex_unlock(&session->mutex); |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 110 | kfree(cmd); |
| 111 | return -ENOMEM; |
| 112 | } |
| 113 | debug_text_event(vmcp_debug, 1, cmd); |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 114 | session->resp_size = cpcmd(cmd, session->response, session->bufsize, |
| 115 | &session->resp_code); |
| 116 | mutex_unlock(&session->mutex); |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 117 | kfree(cmd); |
| 118 | *ppos = 0; /* reset the file pointer after a command */ |
| 119 | return count; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | /* |
| 124 | * These ioctls are available, as the semantics of the diagnose 8 call |
| 125 | * does not fit very well into a Linux call. Diagnose X'08' is described in |
| 126 | * CP Programming Services SC24-6084-00 |
| 127 | * |
| 128 | * VMCP_GETCODE: gives the CP return code back to user space |
| 129 | * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8 |
| 130 | * expects adjacent pages in real storage and to make matters worse, we |
| 131 | * dont know the size of the response. Therefore we default to PAGESIZE and |
| 132 | * let userspace to change the response size, if userspace expects a bigger |
| 133 | * response |
| 134 | */ |
| 135 | static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 136 | { |
| 137 | struct vmcp_session *session; |
Heiko Carstens | 8f3eabe | 2010-01-13 20:44:31 +0100 | [diff] [blame] | 138 | int __user *argp; |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 139 | int temp; |
| 140 | |
Joe Perches | 27e4994 | 2010-09-04 18:52:45 -0700 | [diff] [blame] | 141 | session = file->private_data; |
Heiko Carstens | 8f3eabe | 2010-01-13 20:44:31 +0100 | [diff] [blame] | 142 | if (is_compat_task()) |
| 143 | argp = compat_ptr(arg); |
| 144 | else |
| 145 | argp = (int __user *)arg; |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 146 | if (mutex_lock_interruptible(&session->mutex)) |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 147 | return -ERESTARTSYS; |
| 148 | switch (cmd) { |
| 149 | case VMCP_GETCODE: |
| 150 | temp = session->resp_code; |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 151 | mutex_unlock(&session->mutex); |
Heiko Carstens | 8f3eabe | 2010-01-13 20:44:31 +0100 | [diff] [blame] | 152 | return put_user(temp, argp); |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 153 | case VMCP_SETBUF: |
| 154 | free_pages((unsigned long)session->response, |
| 155 | get_order(session->bufsize)); |
| 156 | session->response=NULL; |
Heiko Carstens | 8f3eabe | 2010-01-13 20:44:31 +0100 | [diff] [blame] | 157 | temp = get_user(session->bufsize, argp); |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 158 | if (get_order(session->bufsize) > 8) { |
| 159 | session->bufsize = PAGE_SIZE; |
| 160 | temp = -EINVAL; |
| 161 | } |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 162 | mutex_unlock(&session->mutex); |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 163 | return temp; |
| 164 | case VMCP_GETSIZE: |
| 165 | temp = session->resp_size; |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 166 | mutex_unlock(&session->mutex); |
Heiko Carstens | 8f3eabe | 2010-01-13 20:44:31 +0100 | [diff] [blame] | 167 | return put_user(temp, argp); |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 168 | default: |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 169 | mutex_unlock(&session->mutex); |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 170 | return -ENOIOCTLCMD; |
| 171 | } |
| 172 | } |
| 173 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 174 | static const struct file_operations vmcp_fops = { |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 175 | .owner = THIS_MODULE, |
Robert P. J. Day | d197e69 | 2007-07-10 11:24:07 +0200 | [diff] [blame] | 176 | .open = vmcp_open, |
| 177 | .release = vmcp_release, |
| 178 | .read = vmcp_read, |
| 179 | .write = vmcp_write, |
| 180 | .unlocked_ioctl = vmcp_ioctl, |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 181 | .compat_ioctl = vmcp_ioctl, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 182 | .llseek = no_llseek, |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | static struct miscdevice vmcp_dev = { |
| 186 | .name = "vmcp", |
| 187 | .minor = MISC_DYNAMIC_MINOR, |
| 188 | .fops = &vmcp_fops, |
| 189 | }; |
| 190 | |
| 191 | static int __init vmcp_init(void) |
| 192 | { |
| 193 | int ret; |
| 194 | |
Heiko Carstens | f73a2b0 | 2010-05-17 10:00:06 +0200 | [diff] [blame] | 195 | if (!MACHINE_IS_VM) |
| 196 | return 0; |
Christian Borntraeger | a44008f | 2008-07-14 09:59:43 +0200 | [diff] [blame] | 197 | |
Michael Holzheu | 66a464d | 2005-06-25 14:55:33 -0700 | [diff] [blame] | 198 | vmcp_debug = debug_register("vmcp", 1, 1, 240); |
Christian Borntraeger | a44008f | 2008-07-14 09:59:43 +0200 | [diff] [blame] | 199 | if (!vmcp_debug) |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 200 | return -ENOMEM; |
Christian Borntraeger | a44008f | 2008-07-14 09:59:43 +0200 | [diff] [blame] | 201 | |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 202 | ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view); |
| 203 | if (ret) { |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 204 | debug_unregister(vmcp_debug); |
| 205 | return ret; |
| 206 | } |
Christian Borntraeger | a44008f | 2008-07-14 09:59:43 +0200 | [diff] [blame] | 207 | |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 208 | ret = misc_register(&vmcp_dev); |
Heiko Carstens | f73a2b0 | 2010-05-17 10:00:06 +0200 | [diff] [blame] | 209 | if (ret) |
Christian Borntraeger | d9d119f | 2007-07-17 13:36:05 +0200 | [diff] [blame] | 210 | debug_unregister(vmcp_debug); |
Heiko Carstens | f73a2b0 | 2010-05-17 10:00:06 +0200 | [diff] [blame] | 211 | return ret; |
Christian Borntraeger | 6b979de | 2005-06-25 14:55:32 -0700 | [diff] [blame] | 212 | } |
Heiko Carstens | f73a2b0 | 2010-05-17 10:00:06 +0200 | [diff] [blame] | 213 | device_initcall(vmcp_init); |