2 * Copyright IBM Corp. 2004,2010
3 * Interface implementation for communication with the z/VM control program
5 * Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
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.
11 * The idea of this driver is based on cpint from Neale Ferguson and #CP in CMS
15 #include <linux/init.h>
16 #include <linux/compat.h>
17 #include <linux/kernel.h>
18 #include <linux/miscdevice.h>
19 #include <linux/slab.h>
20 #include <asm/compat.h>
21 #include <asm/cpcmd.h>
22 #include <asm/debug.h>
23 #include <asm/uaccess.h>
26 static debug_info_t *vmcp_debug;
28 static int vmcp_open(struct inode *inode, struct file *file)
30 struct vmcp_session *session;
32 if (!capable(CAP_SYS_ADMIN))
35 session = kmalloc(sizeof(*session), GFP_KERNEL);
39 session->bufsize = PAGE_SIZE;
40 session->response = NULL;
41 session->resp_size = 0;
42 mutex_init(&session->mutex);
43 file->private_data = session;
44 return nonseekable_open(inode, file);
47 static int vmcp_release(struct inode *inode, struct file *file)
49 struct vmcp_session *session;
51 session = file->private_data;
52 file->private_data = NULL;
53 free_pages((unsigned long)session->response, get_order(session->bufsize));
59 vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
63 struct vmcp_session *session;
65 session = file->private_data;
66 if (mutex_lock_interruptible(&session->mutex))
68 if (!session->response) {
69 mutex_unlock(&session->mutex);
72 size = min_t(size_t, session->resp_size, session->bufsize);
73 ret = simple_read_from_buffer(buff, count, ppos,
74 session->response, size);
76 mutex_unlock(&session->mutex);
82 vmcp_write(struct file *file, const char __user *buff, size_t count,
86 struct vmcp_session *session;
90 cmd = kmalloc(count + 1, GFP_KERNEL);
93 if (copy_from_user(cmd, buff, count)) {
98 session = file->private_data;
99 if (mutex_lock_interruptible(&session->mutex)) {
103 if (!session->response)
104 session->response = (char *)__get_free_pages(GFP_KERNEL
105 | __GFP_REPEAT | GFP_DMA,
106 get_order(session->bufsize));
107 if (!session->response) {
108 mutex_unlock(&session->mutex);
112 debug_text_event(vmcp_debug, 1, cmd);
113 session->resp_size = cpcmd(cmd, session->response, session->bufsize,
114 &session->resp_code);
115 mutex_unlock(&session->mutex);
117 *ppos = 0; /* reset the file pointer after a command */
123 * These ioctls are available, as the semantics of the diagnose 8 call
124 * does not fit very well into a Linux call. Diagnose X'08' is described in
125 * CP Programming Services SC24-6084-00
127 * VMCP_GETCODE: gives the CP return code back to user space
128 * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
129 * expects adjacent pages in real storage and to make matters worse, we
130 * dont know the size of the response. Therefore we default to PAGESIZE and
131 * let userspace to change the response size, if userspace expects a bigger
134 static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
136 struct vmcp_session *session;
140 session = file->private_data;
141 if (is_compat_task())
142 argp = compat_ptr(arg);
144 argp = (int __user *)arg;
145 if (mutex_lock_interruptible(&session->mutex))
149 temp = session->resp_code;
150 mutex_unlock(&session->mutex);
151 return put_user(temp, argp);
153 free_pages((unsigned long)session->response,
154 get_order(session->bufsize));
155 session->response=NULL;
156 temp = get_user(session->bufsize, argp);
157 if (get_order(session->bufsize) > 8) {
158 session->bufsize = PAGE_SIZE;
161 mutex_unlock(&session->mutex);
164 temp = session->resp_size;
165 mutex_unlock(&session->mutex);
166 return put_user(temp, argp);
168 mutex_unlock(&session->mutex);
173 static const struct file_operations vmcp_fops = {
174 .owner = THIS_MODULE,
176 .release = vmcp_release,
179 .unlocked_ioctl = vmcp_ioctl,
180 .compat_ioctl = vmcp_ioctl,
184 static struct miscdevice vmcp_dev = {
186 .minor = MISC_DYNAMIC_MINOR,
190 static int __init vmcp_init(void)
197 vmcp_debug = debug_register("vmcp", 1, 1, 240);
201 ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
203 debug_unregister(vmcp_debug);
207 ret = misc_register(&vmcp_dev);
209 debug_unregister(vmcp_debug);
212 device_initcall(vmcp_init);