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 <asm/cpcmd.h>
23 #include <asm/debug.h>
24 #include <asm/uaccess.h>
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Christian Borntraeger <borntraeger@de.ibm.com>");
29 MODULE_DESCRIPTION("z/VM CP interface");
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
);
44 session
->bufsize
= PAGE_SIZE
;
45 session
->response
= NULL
;
46 session
->resp_size
= 0;
47 mutex_init(&session
->mutex
);
48 file
->private_data
= session
;
49 return nonseekable_open(inode
, file
);
52 static int vmcp_release(struct inode
*inode
, struct file
*file
)
54 struct vmcp_session
*session
;
56 session
= (struct vmcp_session
*)file
->private_data
;
57 file
->private_data
= NULL
;
58 free_pages((unsigned long)session
->response
, get_order(session
->bufsize
));
64 vmcp_read(struct file
*file
, char __user
*buff
, size_t count
, loff_t
*ppos
)
68 struct vmcp_session
*session
;
70 session
= file
->private_data
;
71 if (mutex_lock_interruptible(&session
->mutex
))
73 if (!session
->response
) {
74 mutex_unlock(&session
->mutex
);
77 size
= min_t(size_t, session
->resp_size
, session
->bufsize
);
78 ret
= simple_read_from_buffer(buff
, count
, ppos
,
79 session
->response
, size
);
81 mutex_unlock(&session
->mutex
);
87 vmcp_write(struct file
*file
, const char __user
*buff
, size_t count
,
91 struct vmcp_session
*session
;
95 cmd
= kmalloc(count
+ 1, GFP_KERNEL
);
98 if (copy_from_user(cmd
, buff
, count
)) {
103 session
= (struct vmcp_session
*)file
->private_data
;
104 if (mutex_lock_interruptible(&session
->mutex
)) {
108 if (!session
->response
)
109 session
->response
= (char *)__get_free_pages(GFP_KERNEL
110 | __GFP_REPEAT
| GFP_DMA
,
111 get_order(session
->bufsize
));
112 if (!session
->response
) {
113 mutex_unlock(&session
->mutex
);
117 debug_text_event(vmcp_debug
, 1, cmd
);
118 session
->resp_size
= cpcmd(cmd
, session
->response
, session
->bufsize
,
119 &session
->resp_code
);
120 mutex_unlock(&session
->mutex
);
122 *ppos
= 0; /* reset the file pointer after a command */
128 * These ioctls are available, as the semantics of the diagnose 8 call
129 * does not fit very well into a Linux call. Diagnose X'08' is described in
130 * CP Programming Services SC24-6084-00
132 * VMCP_GETCODE: gives the CP return code back to user space
133 * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
134 * expects adjacent pages in real storage and to make matters worse, we
135 * dont know the size of the response. Therefore we default to PAGESIZE and
136 * let userspace to change the response size, if userspace expects a bigger
139 static long vmcp_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
141 struct vmcp_session
*session
;
144 session
= (struct vmcp_session
*)file
->private_data
;
145 if (mutex_lock_interruptible(&session
->mutex
))
149 temp
= session
->resp_code
;
150 mutex_unlock(&session
->mutex
);
151 return put_user(temp
, (int __user
*)arg
);
153 free_pages((unsigned long)session
->response
,
154 get_order(session
->bufsize
));
155 session
->response
=NULL
;
156 temp
= get_user(session
->bufsize
, (int __user
*)arg
);
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
, (int __user
*)arg
);
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
,
183 static struct miscdevice vmcp_dev
= {
185 .minor
= MISC_DYNAMIC_MINOR
,
189 static int __init
vmcp_init(void)
193 if (!MACHINE_IS_VM
) {
194 pr_warning("The z/VM CP interface device driver cannot be "
195 "loaded without z/VM\n");
199 vmcp_debug
= debug_register("vmcp", 1, 1, 240);
203 ret
= debug_register_view(vmcp_debug
, &debug_hex_ascii_view
);
205 debug_unregister(vmcp_debug
);
209 ret
= misc_register(&vmcp_dev
);
211 debug_unregister(vmcp_debug
);
218 static void __exit
vmcp_exit(void)
220 misc_deregister(&vmcp_dev
);
221 debug_unregister(vmcp_debug
);
224 module_init(vmcp_init
);
225 module_exit(vmcp_exit
);