2 * Copyright (C) 2004,2005 IBM Corporation
3 * Interface implementation for communication with the v/VM control program
4 * Author(s): Christian Borntraeger <cborntra@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 <asm/cpcmd.h>
20 #include <asm/debug.h>
21 #include <asm/uaccess.h>
24 MODULE_LICENSE("GPL");
25 MODULE_AUTHOR("Christian Borntraeger <cborntra@de.ibm.com>");
26 MODULE_DESCRIPTION("z/VM CP interface");
28 static debug_info_t
*vmcp_debug
;
30 static int vmcp_open(struct inode
*inode
, struct file
*file
)
32 struct vmcp_session
*session
;
34 if (!capable(CAP_SYS_ADMIN
))
37 session
= kmalloc(sizeof(*session
), GFP_KERNEL
);
40 session
->bufsize
= PAGE_SIZE
;
41 session
->response
= NULL
;
42 session
->resp_size
= 0;
43 init_MUTEX(&session
->mutex
);
44 file
->private_data
= session
;
45 return nonseekable_open(inode
, file
);
48 static int vmcp_release(struct inode
*inode
, struct file
*file
)
50 struct vmcp_session
*session
;
52 session
= (struct vmcp_session
*)file
->private_data
;
53 file
->private_data
= NULL
;
54 free_pages((unsigned long)session
->response
, get_order(session
->bufsize
));
60 vmcp_read(struct file
*file
, char __user
* buff
, size_t count
, loff_t
* ppos
)
63 struct vmcp_session
*session
;
65 session
= (struct vmcp_session
*)file
->private_data
;
66 if (down_interruptible(&session
->mutex
))
68 if (!session
->response
) {
72 if (*ppos
> session
->resp_size
) {
76 tocopy
= min(session
->resp_size
- (size_t) (*ppos
), count
);
77 tocopy
= min(tocopy
,session
->bufsize
- (size_t) (*ppos
));
79 if (copy_to_user(buff
, session
->response
+ (*ppos
), tocopy
)) {
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 (down_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
) {
117 debug_text_event(vmcp_debug
, 1, cmd
);
118 session
->resp_size
= __cpcmd(cmd
, session
->response
,
120 &session
->resp_code
);
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
;
145 session
= (struct vmcp_session
*)file
->private_data
;
146 if (down_interruptible(&session
->mutex
))
150 temp
= session
->resp_code
;
152 return put_user(temp
, (int __user
*)arg
);
154 free_pages((unsigned long)session
->response
,
155 get_order(session
->bufsize
));
156 session
->response
=NULL
;
157 temp
= get_user(session
->bufsize
, (int __user
*)arg
);
158 if (get_order(session
->bufsize
) > 8) {
159 session
->bufsize
= PAGE_SIZE
;
165 temp
= session
->resp_size
;
167 return put_user(temp
, (int __user
*)arg
);
174 static struct file_operations vmcp_fops
= {
175 .owner
= THIS_MODULE
,
177 .release
= &vmcp_release
,
179 .llseek
= &no_llseek
,
180 .write
= &vmcp_write
,
181 .unlocked_ioctl
= &vmcp_ioctl
,
182 .compat_ioctl
= &vmcp_ioctl
185 static struct miscdevice vmcp_dev
= {
187 .minor
= MISC_DYNAMIC_MINOR
,
191 static int __init
vmcp_init(void)
195 if (!MACHINE_IS_VM
) {
197 "z/VM CP interface is only available under z/VM\n");
200 ret
= misc_register(&vmcp_dev
);
202 printk(KERN_INFO
"z/VM CP interface loaded\n");
205 "z/VM CP interface not loaded. Could not register misc device.\n");
206 vmcp_debug
= debug_register("vmcp", 1, 1, 240);
207 debug_register_view(vmcp_debug
, &debug_hex_ascii_view
);
211 static void __exit
vmcp_exit(void)
213 WARN_ON(misc_deregister(&vmcp_dev
) != 0);
214 debug_unregister(vmcp_debug
);
215 printk(KERN_INFO
"z/VM CP interface unloaded.\n");
218 module_init(vmcp_init
);
219 module_exit(vmcp_exit
);