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
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/miscdevice.h>
18 #include <linux/module.h>
19 #include <linux/smp_lock.h>
20 #include <asm/cpcmd.h>
21 #include <asm/debug.h>
22 #include <asm/uaccess.h>
25 MODULE_LICENSE("GPL");
26 MODULE_AUTHOR("Christian Borntraeger <borntraeger@de.ibm.com>");
27 MODULE_DESCRIPTION("z/VM CP interface");
29 #define PRINTK_HEADER "vmcp: "
31 static debug_info_t *vmcp_debug;
33 static int vmcp_open(struct inode *inode, struct file *file)
35 struct vmcp_session *session;
37 if (!capable(CAP_SYS_ADMIN))
40 session = kmalloc(sizeof(*session), GFP_KERNEL);
45 session->bufsize = PAGE_SIZE;
46 session->response = NULL;
47 session->resp_size = 0;
48 mutex_init(&session->mutex);
49 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)
69 struct vmcp_session *session;
71 session = (struct vmcp_session *)file->private_data;
72 if (mutex_lock_interruptible(&session->mutex))
74 if (!session->response) {
75 mutex_unlock(&session->mutex);
78 if (*ppos > session->resp_size) {
79 mutex_unlock(&session->mutex);
82 tocopy = min(session->resp_size - (size_t) (*ppos), count);
83 tocopy = min(tocopy, session->bufsize - (size_t) (*ppos));
85 if (copy_to_user(buff, session->response + (*ppos), tocopy)) {
86 mutex_unlock(&session->mutex);
89 mutex_unlock(&session->mutex);
95 vmcp_write(struct file *file, const char __user *buff, size_t count,
99 struct vmcp_session *session;
103 cmd = kmalloc(count + 1, GFP_KERNEL);
106 if (copy_from_user(cmd, buff, count)) {
111 session = (struct vmcp_session *)file->private_data;
112 if (mutex_lock_interruptible(&session->mutex)) {
116 if (!session->response)
117 session->response = (char *)__get_free_pages(GFP_KERNEL
118 | __GFP_REPEAT | GFP_DMA,
119 get_order(session->bufsize));
120 if (!session->response) {
121 mutex_unlock(&session->mutex);
125 debug_text_event(vmcp_debug, 1, cmd);
126 session->resp_size = cpcmd(cmd, session->response, session->bufsize,
127 &session->resp_code);
128 mutex_unlock(&session->mutex);
130 *ppos = 0; /* reset the file pointer after a command */
136 * These ioctls are available, as the semantics of the diagnose 8 call
137 * does not fit very well into a Linux call. Diagnose X'08' is described in
138 * CP Programming Services SC24-6084-00
140 * VMCP_GETCODE: gives the CP return code back to user space
141 * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
142 * expects adjacent pages in real storage and to make matters worse, we
143 * dont know the size of the response. Therefore we default to PAGESIZE and
144 * let userspace to change the response size, if userspace expects a bigger
147 static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
149 struct vmcp_session *session;
152 session = (struct vmcp_session *)file->private_data;
153 if (mutex_lock_interruptible(&session->mutex))
157 temp = session->resp_code;
158 mutex_unlock(&session->mutex);
159 return put_user(temp, (int __user *)arg);
161 free_pages((unsigned long)session->response,
162 get_order(session->bufsize));
163 session->response=NULL;
164 temp = get_user(session->bufsize, (int __user *)arg);
165 if (get_order(session->bufsize) > 8) {
166 session->bufsize = PAGE_SIZE;
169 mutex_unlock(&session->mutex);
172 temp = session->resp_size;
173 mutex_unlock(&session->mutex);
174 return put_user(temp, (int __user *)arg);
176 mutex_unlock(&session->mutex);
181 static const struct file_operations vmcp_fops = {
182 .owner = THIS_MODULE,
184 .release = vmcp_release,
187 .unlocked_ioctl = vmcp_ioctl,
188 .compat_ioctl = vmcp_ioctl,
191 static struct miscdevice vmcp_dev = {
193 .minor = MISC_DYNAMIC_MINOR,
197 static int __init vmcp_init(void)
201 if (!MACHINE_IS_VM) {
202 PRINT_WARN("z/VM CP interface is only available under z/VM\n");
205 vmcp_debug = debug_register("vmcp", 1, 1, 240);
207 PRINT_ERR("z/VM CP interface not loaded. Could not register "
211 ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
213 PRINT_ERR("z/VM CP interface not loaded. Could not register "
214 "debug feature view. Error code: %d\n", ret);
215 debug_unregister(vmcp_debug);
218 ret = misc_register(&vmcp_dev);
220 PRINT_ERR("z/VM CP interface not loaded. Could not register "
221 "misc device. Error code: %d\n", ret);
222 debug_unregister(vmcp_debug);
225 PRINT_INFO("z/VM CP interface loaded\n");
229 static void __exit vmcp_exit(void)
231 misc_deregister(&vmcp_dev);
232 debug_unregister(vmcp_debug);
233 PRINT_INFO("z/VM CP interface unloaded.\n");
236 module_init(vmcp_init);
237 module_exit(vmcp_exit);