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