blob: 31a3ccbb6495c2e8efcdd34bbd8b3cacfbbf9154 [file] [log] [blame]
Christian Borntraeger6b979de2005-06-25 14:55:32 -07001/*
Heiko Carstensf73a2b02010-05-17 10:00:06 +02002 * Copyright IBM Corp. 2004,2010
Christian Borntraeger45af3af2006-09-20 15:59:24 +02003 * Interface implementation for communication with the z/VM control program
Christian Borntraeger6b979de2005-06-25 14:55:32 -07004 *
Heiko Carstensf73a2b02010-05-17 10:00:06 +02005 * Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
Christian Borntraeger6b979de2005-06-25 14:55:32 -07006 *
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 Carstensf73a2b02010-05-17 10:00:06 +020010 *
Christian Borntraeger6b979de2005-06-25 14:55:32 -070011 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Heiko Carstens8f3eabe2010-01-13 20:44:31 +010019#include <asm/compat.h>
Christian Borntraeger6b979de2005-06-25 14:55:32 -070020#include <asm/cpcmd.h>
21#include <asm/debug.h>
22#include <asm/uaccess.h>
23#include "vmcp.h"
24
Christian Borntraeger6b979de2005-06-25 14:55:32 -070025static debug_info_t *vmcp_debug;
26
27static int vmcp_open(struct inode *inode, struct file *file)
28{
29 struct vmcp_session *session;
30
31 if (!capable(CAP_SYS_ADMIN))
32 return -EPERM;
33
34 session = kmalloc(sizeof(*session), GFP_KERNEL);
35 if (!session)
36 return -ENOMEM;
Arnd Bergmann6c111d82008-05-20 19:16:59 +020037
Christian Borntraeger6b979de2005-06-25 14:55:32 -070038 session->bufsize = PAGE_SIZE;
39 session->response = NULL;
40 session->resp_size = 0;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020041 mutex_init(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070042 file->private_data = session;
43 return nonseekable_open(inode, file);
44}
45
46static int vmcp_release(struct inode *inode, struct file *file)
47{
48 struct vmcp_session *session;
49
Joe Perches27e49942010-09-04 18:52:45 -070050 session = file->private_data;
Christian Borntraeger6b979de2005-06-25 14:55:32 -070051 file->private_data = NULL;
52 free_pages((unsigned long)session->response, get_order(session->bufsize));
53 kfree(session);
54 return 0;
55}
56
57static ssize_t
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020058vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
Christian Borntraeger6b979de2005-06-25 14:55:32 -070059{
Akinobu Mita77858572008-07-14 09:59:14 +020060 ssize_t ret;
61 size_t size;
Christian Borntraeger6b979de2005-06-25 14:55:32 -070062 struct vmcp_session *session;
63
Akinobu Mita77858572008-07-14 09:59:14 +020064 session = file->private_data;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020065 if (mutex_lock_interruptible(&session->mutex))
Christian Borntraeger6b979de2005-06-25 14:55:32 -070066 return -ERESTARTSYS;
67 if (!session->response) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020068 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070069 return 0;
70 }
Akinobu Mita77858572008-07-14 09:59:14 +020071 size = min_t(size_t, session->resp_size, session->bufsize);
72 ret = simple_read_from_buffer(buff, count, ppos,
73 session->response, size);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070074
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020075 mutex_unlock(&session->mutex);
Akinobu Mita77858572008-07-14 09:59:14 +020076
77 return ret;
Christian Borntraeger6b979de2005-06-25 14:55:32 -070078}
79
80static ssize_t
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020081vmcp_write(struct file *file, const char __user *buff, size_t count,
82 loff_t *ppos)
Christian Borntraeger6b979de2005-06-25 14:55:32 -070083{
84 char *cmd;
85 struct vmcp_session *session;
86
87 if (count > 240)
88 return -EINVAL;
89 cmd = kmalloc(count + 1, GFP_KERNEL);
90 if (!cmd)
91 return -ENOMEM;
92 if (copy_from_user(cmd, buff, count)) {
93 kfree(cmd);
94 return -EFAULT;
95 }
96 cmd[count] = '\0';
Joe Perches27e49942010-09-04 18:52:45 -070097 session = file->private_data;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020098 if (mutex_lock_interruptible(&session->mutex)) {
Christian Borntraegera5da8662005-11-07 00:59:12 -080099 kfree(cmd);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700100 return -ERESTARTSYS;
Christian Borntraegera5da8662005-11-07 00:59:12 -0800101 }
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700102 if (!session->response)
103 session->response = (char *)__get_free_pages(GFP_KERNEL
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200104 | __GFP_REPEAT | GFP_DMA,
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700105 get_order(session->bufsize));
106 if (!session->response) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200107 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700108 kfree(cmd);
109 return -ENOMEM;
110 }
111 debug_text_event(vmcp_debug, 1, cmd);
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200112 session->resp_size = cpcmd(cmd, session->response, session->bufsize,
113 &session->resp_code);
114 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700115 kfree(cmd);
116 *ppos = 0; /* reset the file pointer after a command */
117 return count;
118}
119
120
121/*
122 * These ioctls are available, as the semantics of the diagnose 8 call
123 * does not fit very well into a Linux call. Diagnose X'08' is described in
124 * CP Programming Services SC24-6084-00
125 *
126 * VMCP_GETCODE: gives the CP return code back to user space
127 * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
128 * expects adjacent pages in real storage and to make matters worse, we
129 * dont know the size of the response. Therefore we default to PAGESIZE and
130 * let userspace to change the response size, if userspace expects a bigger
131 * response
132 */
133static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
134{
135 struct vmcp_session *session;
Heiko Carstens8f3eabe2010-01-13 20:44:31 +0100136 int __user *argp;
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700137 int temp;
138
Joe Perches27e49942010-09-04 18:52:45 -0700139 session = file->private_data;
Heiko Carstens8f3eabe2010-01-13 20:44:31 +0100140 if (is_compat_task())
141 argp = compat_ptr(arg);
142 else
143 argp = (int __user *)arg;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200144 if (mutex_lock_interruptible(&session->mutex))
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700145 return -ERESTARTSYS;
146 switch (cmd) {
147 case VMCP_GETCODE:
148 temp = session->resp_code;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200149 mutex_unlock(&session->mutex);
Heiko Carstens8f3eabe2010-01-13 20:44:31 +0100150 return put_user(temp, argp);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700151 case VMCP_SETBUF:
152 free_pages((unsigned long)session->response,
153 get_order(session->bufsize));
154 session->response=NULL;
Heiko Carstens8f3eabe2010-01-13 20:44:31 +0100155 temp = get_user(session->bufsize, argp);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700156 if (get_order(session->bufsize) > 8) {
157 session->bufsize = PAGE_SIZE;
158 temp = -EINVAL;
159 }
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200160 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700161 return temp;
162 case VMCP_GETSIZE:
163 temp = session->resp_size;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200164 mutex_unlock(&session->mutex);
Heiko Carstens8f3eabe2010-01-13 20:44:31 +0100165 return put_user(temp, argp);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700166 default:
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200167 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700168 return -ENOIOCTLCMD;
169 }
170}
171
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800172static const struct file_operations vmcp_fops = {
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700173 .owner = THIS_MODULE,
Robert P. J. Dayd197e692007-07-10 11:24:07 +0200174 .open = vmcp_open,
175 .release = vmcp_release,
176 .read = vmcp_read,
177 .write = vmcp_write,
178 .unlocked_ioctl = vmcp_ioctl,
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200179 .compat_ioctl = vmcp_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200180 .llseek = no_llseek,
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700181};
182
183static struct miscdevice vmcp_dev = {
184 .name = "vmcp",
185 .minor = MISC_DYNAMIC_MINOR,
186 .fops = &vmcp_fops,
187};
188
189static int __init vmcp_init(void)
190{
191 int ret;
192
Heiko Carstensf73a2b02010-05-17 10:00:06 +0200193 if (!MACHINE_IS_VM)
194 return 0;
Christian Borntraegera44008f2008-07-14 09:59:43 +0200195
Michael Holzheu66a464d2005-06-25 14:55:33 -0700196 vmcp_debug = debug_register("vmcp", 1, 1, 240);
Christian Borntraegera44008f2008-07-14 09:59:43 +0200197 if (!vmcp_debug)
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200198 return -ENOMEM;
Christian Borntraegera44008f2008-07-14 09:59:43 +0200199
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200200 ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
201 if (ret) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200202 debug_unregister(vmcp_debug);
203 return ret;
204 }
Christian Borntraegera44008f2008-07-14 09:59:43 +0200205
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200206 ret = misc_register(&vmcp_dev);
Heiko Carstensf73a2b02010-05-17 10:00:06 +0200207 if (ret)
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200208 debug_unregister(vmcp_debug);
Heiko Carstensf73a2b02010-05-17 10:00:06 +0200209 return ret;
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700210}
Heiko Carstensf73a2b02010-05-17 10:00:06 +0200211device_initcall(vmcp_init);