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/compat.h>
23 #include <asm/cpcmd.h>
24 #include <asm/debug.h>
25 #include <asm/uaccess.h>
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Christian Borntraeger <borntraeger@de.ibm.com>");
30 MODULE_DESCRIPTION("z/VM CP interface");
32 static debug_info_t
*vmcp_debug
;
34 static int vmcp_open(struct inode
*inode
, struct file
*file
)
36 struct vmcp_session
*session
;
38 if (!capable(CAP_SYS_ADMIN
))
41 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
;
50 return nonseekable_open(inode
, file
);
53 static int vmcp_release(struct inode
*inode
, struct file
*file
)
55 struct vmcp_session
*session
;
57 session
= (struct vmcp_session
*)file
->private_data
;
58 file
->private_data
= NULL
;
59 free_pages((unsigned long)session
->response
, get_order(session
->bufsize
));
65 vmcp_read(struct file
*file
, char __user
*buff
, size_t count
, loff_t
*ppos
)
69 struct vmcp_session
*session
;
71 session
= file
->private_data
;
72 if (mutex_lock_interruptible(&session
->mutex
))
74 if (!session
->response
) {
75 mutex_unlock(&session
->mutex
);
78 size
= min_t(size_t, session
->resp_size
, session
->bufsize
);
79 ret
= simple_read_from_buffer(buff
, count
, ppos
,
80 session
->response
, size
);
82 mutex_unlock(&session
->mutex
);
88 vmcp_write(struct file
*file
, const char __user
*buff
, size_t count
,
92 struct vmcp_session
*session
;
96 cmd
= kmalloc(count
+ 1, GFP_KERNEL
);
99 if (copy_from_user(cmd
, buff
, count
)) {
104 session
= (struct vmcp_session
*)file
->private_data
;
105 if (mutex_lock_interruptible(&session
->mutex
)) {
109 if (!session
->response
)
110 session
->response
= (char *)__get_free_pages(GFP_KERNEL
111 | __GFP_REPEAT
| GFP_DMA
,
112 get_order(session
->bufsize
));
113 if (!session
->response
) {
114 mutex_unlock(&session
->mutex
);
118 debug_text_event(vmcp_debug
, 1, cmd
);
119 session
->resp_size
= cpcmd(cmd
, session
->response
, session
->bufsize
,
120 &session
->resp_code
);
121 mutex_unlock(&session
->mutex
);
123 *ppos
= 0; /* reset the file pointer after a command */
129 * These ioctls are available, as the semantics of the diagnose 8 call
130 * does not fit very well into a Linux call. Diagnose X'08' is described in
131 * CP Programming Services SC24-6084-00
133 * VMCP_GETCODE: gives the CP return code back to user space
134 * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
135 * expects adjacent pages in real storage and to make matters worse, we
136 * dont know the size of the response. Therefore we default to PAGESIZE and
137 * let userspace to change the response size, if userspace expects a bigger
140 static long vmcp_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
142 struct vmcp_session
*session
;
146 session
= (struct vmcp_session
*)file
->private_data
;
147 if (is_compat_task())
148 argp
= compat_ptr(arg
);
150 argp
= (int __user
*)arg
;
151 if (mutex_lock_interruptible(&session
->mutex
))
155 temp
= session
->resp_code
;
156 mutex_unlock(&session
->mutex
);
157 return put_user(temp
, argp
);
159 free_pages((unsigned long)session
->response
,
160 get_order(session
->bufsize
));
161 session
->response
=NULL
;
162 temp
= get_user(session
->bufsize
, argp
);
163 if (get_order(session
->bufsize
) > 8) {
164 session
->bufsize
= PAGE_SIZE
;
167 mutex_unlock(&session
->mutex
);
170 temp
= session
->resp_size
;
171 mutex_unlock(&session
->mutex
);
172 return put_user(temp
, argp
);
174 mutex_unlock(&session
->mutex
);
179 static const struct file_operations vmcp_fops
= {
180 .owner
= THIS_MODULE
,
182 .release
= vmcp_release
,
185 .unlocked_ioctl
= vmcp_ioctl
,
186 .compat_ioctl
= vmcp_ioctl
,
189 static struct miscdevice vmcp_dev
= {
191 .minor
= MISC_DYNAMIC_MINOR
,
195 static int __init
vmcp_init(void)
199 if (!MACHINE_IS_VM
) {
200 pr_warning("The z/VM CP interface device driver cannot be "
201 "loaded without z/VM\n");
205 vmcp_debug
= debug_register("vmcp", 1, 1, 240);
209 ret
= debug_register_view(vmcp_debug
, &debug_hex_ascii_view
);
211 debug_unregister(vmcp_debug
);
215 ret
= misc_register(&vmcp_dev
);
217 debug_unregister(vmcp_debug
);
224 static void __exit
vmcp_exit(void)
226 misc_deregister(&vmcp_dev
);
227 debug_unregister(vmcp_debug
);
230 module_init(vmcp_init
);
231 module_exit(vmcp_exit
);