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