2 * Copyright IBM Corp. 2004,2007
3 * Interface implementation for communication with the z/VM control program
4 * 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
14 #define KMSG_COMPONENT "vmcp"
15 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/miscdevice.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <asm/compat.h>
24 #include <asm/cpcmd.h>
25 #include <asm/debug.h>
26 #include <asm/uaccess.h>
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Christian Borntraeger <borntraeger@de.ibm.com>");
31 MODULE_DESCRIPTION("z/VM CP interface");
33 static debug_info_t *vmcp_debug;
35 static int vmcp_open(struct inode *inode, struct file *file)
37 struct vmcp_session *session;
39 if (!capable(CAP_SYS_ADMIN))
42 session = kmalloc(sizeof(*session), GFP_KERNEL);
46 session->bufsize = PAGE_SIZE;
47 session->response = NULL;
48 session->resp_size = 0;
49 mutex_init(&session->mutex);
50 file->private_data = session;
51 return nonseekable_open(inode, file);
54 static int vmcp_release(struct inode *inode, struct file *file)
56 struct vmcp_session *session;
58 session = (struct vmcp_session *)file->private_data;
59 file->private_data = NULL;
60 free_pages((unsigned long)session->response, get_order(session->bufsize));
66 vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
70 struct vmcp_session *session;
72 session = file->private_data;
73 if (mutex_lock_interruptible(&session->mutex))
75 if (!session->response) {
76 mutex_unlock(&session->mutex);
79 size = min_t(size_t, session->resp_size, session->bufsize);
80 ret = simple_read_from_buffer(buff, count, ppos,
81 session->response, size);
83 mutex_unlock(&session->mutex);
89 vmcp_write(struct file *file, const char __user *buff, size_t count,
93 struct vmcp_session *session;
97 cmd = kmalloc(count + 1, GFP_KERNEL);
100 if (copy_from_user(cmd, buff, count)) {
105 session = (struct vmcp_session *)file->private_data;
106 if (mutex_lock_interruptible(&session->mutex)) {
110 if (!session->response)
111 session->response = (char *)__get_free_pages(GFP_KERNEL
112 | __GFP_REPEAT | GFP_DMA,
113 get_order(session->bufsize));
114 if (!session->response) {
115 mutex_unlock(&session->mutex);
119 debug_text_event(vmcp_debug, 1, cmd);
120 session->resp_size = cpcmd(cmd, session->response, session->bufsize,
121 &session->resp_code);
122 mutex_unlock(&session->mutex);
124 *ppos = 0; /* reset the file pointer after a command */
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
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
141 static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
143 struct vmcp_session *session;
147 session = (struct vmcp_session *)file->private_data;
148 if (is_compat_task())
149 argp = compat_ptr(arg);
151 argp = (int __user *)arg;
152 if (mutex_lock_interruptible(&session->mutex))
156 temp = session->resp_code;
157 mutex_unlock(&session->mutex);
158 return put_user(temp, argp);
160 free_pages((unsigned long)session->response,
161 get_order(session->bufsize));
162 session->response=NULL;
163 temp = get_user(session->bufsize, argp);
164 if (get_order(session->bufsize) > 8) {
165 session->bufsize = PAGE_SIZE;
168 mutex_unlock(&session->mutex);
171 temp = session->resp_size;
172 mutex_unlock(&session->mutex);
173 return put_user(temp, argp);
175 mutex_unlock(&session->mutex);
180 static const struct file_operations vmcp_fops = {
181 .owner = THIS_MODULE,
183 .release = vmcp_release,
186 .unlocked_ioctl = vmcp_ioctl,
187 .compat_ioctl = vmcp_ioctl,
190 static struct miscdevice vmcp_dev = {
192 .minor = MISC_DYNAMIC_MINOR,
196 static int __init vmcp_init(void)
200 if (!MACHINE_IS_VM) {
201 pr_warning("The z/VM CP interface device driver cannot be "
202 "loaded without z/VM\n");
206 vmcp_debug = debug_register("vmcp", 1, 1, 240);
210 ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
212 debug_unregister(vmcp_debug);
216 ret = misc_register(&vmcp_dev);
218 debug_unregister(vmcp_debug);
225 static void __exit vmcp_exit(void)
227 misc_deregister(&vmcp_dev);
228 debug_unregister(vmcp_debug);
231 module_init(vmcp_init);
232 module_exit(vmcp_exit);