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 <linux/export.h>
21 #include <asm/compat.h>
22 #include <asm/cpcmd.h>
23 #include <asm/debug.h>
24 #include <asm/uaccess.h>
27 static debug_info_t *vmcp_debug;
29 static int vmcp_open(struct inode *inode, struct file *file)
31 struct vmcp_session *session;
33 if (!capable(CAP_SYS_ADMIN))
36 session = kmalloc(sizeof(*session), GFP_KERNEL);
40 session->bufsize = PAGE_SIZE;
41 session->response = NULL;
42 session->resp_size = 0;
43 mutex_init(&session->mutex);
44 file->private_data = session;
45 return nonseekable_open(inode, file);
48 static int vmcp_release(struct inode *inode, struct file *file)
50 struct vmcp_session *session;
52 session = file->private_data;
53 file->private_data = NULL;
54 free_pages((unsigned long)session->response, get_order(session->bufsize));
60 vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
64 struct vmcp_session *session;
66 session = file->private_data;
67 if (mutex_lock_interruptible(&session->mutex))
69 if (!session->response) {
70 mutex_unlock(&session->mutex);
73 size = min_t(size_t, session->resp_size, session->bufsize);
74 ret = simple_read_from_buffer(buff, count, ppos,
75 session->response, size);
77 mutex_unlock(&session->mutex);
83 vmcp_write(struct file *file, const char __user *buff, size_t count,
87 struct vmcp_session *session;
91 cmd = kmalloc(count + 1, GFP_KERNEL);
94 if (copy_from_user(cmd, buff, count)) {
99 session = file->private_data;
100 if (mutex_lock_interruptible(&session->mutex)) {
104 if (!session->response)
105 session->response = (char *)__get_free_pages(GFP_KERNEL
106 | __GFP_REPEAT | GFP_DMA,
107 get_order(session->bufsize));
108 if (!session->response) {
109 mutex_unlock(&session->mutex);
113 debug_text_event(vmcp_debug, 1, cmd);
114 session->resp_size = cpcmd(cmd, session->response, session->bufsize,
115 &session->resp_code);
116 mutex_unlock(&session->mutex);
118 *ppos = 0; /* reset the file pointer after a command */
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
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
135 static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
137 struct vmcp_session *session;
141 session = file->private_data;
142 if (is_compat_task())
143 argp = compat_ptr(arg);
145 argp = (int __user *)arg;
146 if (mutex_lock_interruptible(&session->mutex))
150 temp = session->resp_code;
151 mutex_unlock(&session->mutex);
152 return put_user(temp, argp);
154 free_pages((unsigned long)session->response,
155 get_order(session->bufsize));
156 session->response=NULL;
157 temp = get_user(session->bufsize, argp);
158 if (get_order(session->bufsize) > 8) {
159 session->bufsize = PAGE_SIZE;
162 mutex_unlock(&session->mutex);
165 temp = session->resp_size;
166 mutex_unlock(&session->mutex);
167 return put_user(temp, argp);
169 mutex_unlock(&session->mutex);
174 static const struct file_operations vmcp_fops = {
175 .owner = THIS_MODULE,
177 .release = vmcp_release,
180 .unlocked_ioctl = vmcp_ioctl,
181 .compat_ioctl = vmcp_ioctl,
185 static struct miscdevice vmcp_dev = {
187 .minor = MISC_DYNAMIC_MINOR,
191 static int __init vmcp_init(void)
198 vmcp_debug = debug_register("vmcp", 1, 1, 240);
202 ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
204 debug_unregister(vmcp_debug);
208 ret = misc_register(&vmcp_dev);
210 debug_unregister(vmcp_debug);
213 device_initcall(vmcp_init);