2 * Copyright IBM Corp. 2004,2010
3 * Interface implementation for communication with the z/VM control program
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/kernel.h>
17 #include <linux/miscdevice.h>
18 #include <linux/slab.h>
19 #include <asm/compat.h>
20 #include <asm/cpcmd.h>
21 #include <asm/debug.h>
22 #include <asm/uaccess.h>
25 static debug_info_t *vmcp_debug;
27 static int vmcp_open(struct inode *inode, struct file *file)
29 struct vmcp_session *session;
31 if (!capable(CAP_SYS_ADMIN))
34 session = kmalloc(sizeof(*session), GFP_KERNEL);
38 session->bufsize = PAGE_SIZE;
39 session->response = NULL;
40 session->resp_size = 0;
41 mutex_init(&session->mutex);
42 file->private_data = session;
43 return nonseekable_open(inode, file);
46 static int vmcp_release(struct inode *inode, struct file *file)
48 struct vmcp_session *session;
50 session = (struct vmcp_session *)file->private_data;
51 file->private_data = NULL;
52 free_pages((unsigned long)session->response, get_order(session->bufsize));
58 vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
62 struct vmcp_session *session;
64 session = file->private_data;
65 if (mutex_lock_interruptible(&session->mutex))
67 if (!session->response) {
68 mutex_unlock(&session->mutex);
71 size = min_t(size_t, session->resp_size, session->bufsize);
72 ret = simple_read_from_buffer(buff, count, ppos,
73 session->response, size);
75 mutex_unlock(&session->mutex);
81 vmcp_write(struct file *file, const char __user *buff, size_t count,
85 struct vmcp_session *session;
89 cmd = kmalloc(count + 1, GFP_KERNEL);
92 if (copy_from_user(cmd, buff, count)) {
97 session = (struct vmcp_session *)file->private_data;
98 if (mutex_lock_interruptible(&session->mutex)) {
102 if (!session->response)
103 session->response = (char *)__get_free_pages(GFP_KERNEL
104 | __GFP_REPEAT | GFP_DMA,
105 get_order(session->bufsize));
106 if (!session->response) {
107 mutex_unlock(&session->mutex);
111 debug_text_event(vmcp_debug, 1, cmd);
112 session->resp_size = cpcmd(cmd, session->response, session->bufsize,
113 &session->resp_code);
114 mutex_unlock(&session->mutex);
116 *ppos = 0; /* reset the file pointer after a command */
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
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
133 static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
135 struct vmcp_session *session;
139 session = (struct vmcp_session *)file->private_data;
140 if (is_compat_task())
141 argp = compat_ptr(arg);
143 argp = (int __user *)arg;
144 if (mutex_lock_interruptible(&session->mutex))
148 temp = session->resp_code;
149 mutex_unlock(&session->mutex);
150 return put_user(temp, argp);
152 free_pages((unsigned long)session->response,
153 get_order(session->bufsize));
154 session->response=NULL;
155 temp = get_user(session->bufsize, argp);
156 if (get_order(session->bufsize) > 8) {
157 session->bufsize = PAGE_SIZE;
160 mutex_unlock(&session->mutex);
163 temp = session->resp_size;
164 mutex_unlock(&session->mutex);
165 return put_user(temp, argp);
167 mutex_unlock(&session->mutex);
172 static const struct file_operations vmcp_fops = {
173 .owner = THIS_MODULE,
175 .release = vmcp_release,
178 .unlocked_ioctl = vmcp_ioctl,
179 .compat_ioctl = vmcp_ioctl,
182 static struct miscdevice vmcp_dev = {
184 .minor = MISC_DYNAMIC_MINOR,
188 static int __init vmcp_init(void)
195 vmcp_debug = debug_register("vmcp", 1, 1, 240);
199 ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
201 debug_unregister(vmcp_debug);
205 ret = misc_register(&vmcp_dev);
207 debug_unregister(vmcp_debug);
210 device_initcall(vmcp_init);