]>
Commit | Line | Data |
---|---|---|
6b979de3 | 1 | /* |
d9d119f1 | 2 | * Copyright IBM Corp. 2004,2007 |
45af3af8 | 3 | * Interface implementation for communication with the z/VM control program |
d9d119f1 | 4 | * Author(s): Christian Borntraeger <borntraeger@de.ibm.com> |
6b979de3 CB |
5 | * |
6 | * | |
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. | |
10 | ||
11 | * The idea of this driver is based on cpint from Neale Ferguson and #CP in CMS | |
12 | */ | |
13 | ||
8988e58f CB |
14 | #define KMSG_COMPONENT "vmcp" |
15 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
16 | ||
6b979de3 CB |
17 | #include <linux/fs.h> |
18 | #include <linux/init.h> | |
19 | #include <linux/kernel.h> | |
20 | #include <linux/miscdevice.h> | |
21 | #include <linux/module.h> | |
5a0e3ad6 | 22 | #include <linux/slab.h> |
8f3eabe3 | 23 | #include <asm/compat.h> |
6b979de3 CB |
24 | #include <asm/cpcmd.h> |
25 | #include <asm/debug.h> | |
26 | #include <asm/uaccess.h> | |
27 | #include "vmcp.h" | |
28 | ||
29 | MODULE_LICENSE("GPL"); | |
d9d119f1 | 30 | MODULE_AUTHOR("Christian Borntraeger <borntraeger@de.ibm.com>"); |
6b979de3 CB |
31 | MODULE_DESCRIPTION("z/VM CP interface"); |
32 | ||
33 | static debug_info_t *vmcp_debug; | |
34 | ||
35 | static int vmcp_open(struct inode *inode, struct file *file) | |
36 | { | |
37 | struct vmcp_session *session; | |
38 | ||
39 | if (!capable(CAP_SYS_ADMIN)) | |
40 | return -EPERM; | |
41 | ||
42 | session = kmalloc(sizeof(*session), GFP_KERNEL); | |
43 | if (!session) | |
44 | return -ENOMEM; | |
6c111d88 | 45 | |
6b979de3 CB |
46 | session->bufsize = PAGE_SIZE; |
47 | session->response = NULL; | |
48 | session->resp_size = 0; | |
d9d119f1 | 49 | mutex_init(&session->mutex); |
6b979de3 CB |
50 | file->private_data = session; |
51 | return nonseekable_open(inode, file); | |
52 | } | |
53 | ||
54 | static int vmcp_release(struct inode *inode, struct file *file) | |
55 | { | |
56 | struct vmcp_session *session; | |
57 | ||
58 | session = (struct vmcp_session *)file->private_data; | |
59 | file->private_data = NULL; | |
60 | free_pages((unsigned long)session->response, get_order(session->bufsize)); | |
61 | kfree(session); | |
62 | return 0; | |
63 | } | |
64 | ||
65 | static ssize_t | |
d9d119f1 | 66 | vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos) |
6b979de3 | 67 | { |
7785857a AM |
68 | ssize_t ret; |
69 | size_t size; | |
6b979de3 CB |
70 | struct vmcp_session *session; |
71 | ||
7785857a | 72 | session = file->private_data; |
d9d119f1 | 73 | if (mutex_lock_interruptible(&session->mutex)) |
6b979de3 CB |
74 | return -ERESTARTSYS; |
75 | if (!session->response) { | |
d9d119f1 | 76 | mutex_unlock(&session->mutex); |
6b979de3 CB |
77 | return 0; |
78 | } | |
7785857a AM |
79 | size = min_t(size_t, session->resp_size, session->bufsize); |
80 | ret = simple_read_from_buffer(buff, count, ppos, | |
81 | session->response, size); | |
6b979de3 | 82 | |
d9d119f1 | 83 | mutex_unlock(&session->mutex); |
7785857a AM |
84 | |
85 | return ret; | |
6b979de3 CB |
86 | } |
87 | ||
88 | static ssize_t | |
d9d119f1 CB |
89 | vmcp_write(struct file *file, const char __user *buff, size_t count, |
90 | loff_t *ppos) | |
6b979de3 CB |
91 | { |
92 | char *cmd; | |
93 | struct vmcp_session *session; | |
94 | ||
95 | if (count > 240) | |
96 | return -EINVAL; | |
97 | cmd = kmalloc(count + 1, GFP_KERNEL); | |
98 | if (!cmd) | |
99 | return -ENOMEM; | |
100 | if (copy_from_user(cmd, buff, count)) { | |
101 | kfree(cmd); | |
102 | return -EFAULT; | |
103 | } | |
104 | cmd[count] = '\0'; | |
105 | session = (struct vmcp_session *)file->private_data; | |
d9d119f1 | 106 | if (mutex_lock_interruptible(&session->mutex)) { |
a5da866f | 107 | kfree(cmd); |
6b979de3 | 108 | return -ERESTARTSYS; |
a5da866f | 109 | } |
6b979de3 CB |
110 | if (!session->response) |
111 | session->response = (char *)__get_free_pages(GFP_KERNEL | |
d9d119f1 | 112 | | __GFP_REPEAT | GFP_DMA, |
6b979de3 CB |
113 | get_order(session->bufsize)); |
114 | if (!session->response) { | |
d9d119f1 | 115 | mutex_unlock(&session->mutex); |
6b979de3 CB |
116 | kfree(cmd); |
117 | return -ENOMEM; | |
118 | } | |
119 | debug_text_event(vmcp_debug, 1, cmd); | |
d9d119f1 CB |
120 | session->resp_size = cpcmd(cmd, session->response, session->bufsize, |
121 | &session->resp_code); | |
122 | mutex_unlock(&session->mutex); | |
6b979de3 CB |
123 | kfree(cmd); |
124 | *ppos = 0; /* reset the file pointer after a command */ | |
125 | return count; | |
126 | } | |
127 | ||
128 | ||
129 | /* | |
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 | |
133 | * | |
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 | |
139 | * response | |
140 | */ | |
141 | static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
142 | { | |
143 | struct vmcp_session *session; | |
8f3eabe3 | 144 | int __user *argp; |
6b979de3 CB |
145 | int temp; |
146 | ||
147 | session = (struct vmcp_session *)file->private_data; | |
8f3eabe3 HC |
148 | if (is_compat_task()) |
149 | argp = compat_ptr(arg); | |
150 | else | |
151 | argp = (int __user *)arg; | |
d9d119f1 | 152 | if (mutex_lock_interruptible(&session->mutex)) |
6b979de3 CB |
153 | return -ERESTARTSYS; |
154 | switch (cmd) { | |
155 | case VMCP_GETCODE: | |
156 | temp = session->resp_code; | |
d9d119f1 | 157 | mutex_unlock(&session->mutex); |
8f3eabe3 | 158 | return put_user(temp, argp); |
6b979de3 CB |
159 | case VMCP_SETBUF: |
160 | free_pages((unsigned long)session->response, | |
161 | get_order(session->bufsize)); | |
162 | session->response=NULL; | |
8f3eabe3 | 163 | temp = get_user(session->bufsize, argp); |
6b979de3 CB |
164 | if (get_order(session->bufsize) > 8) { |
165 | session->bufsize = PAGE_SIZE; | |
166 | temp = -EINVAL; | |
167 | } | |
d9d119f1 | 168 | mutex_unlock(&session->mutex); |
6b979de3 CB |
169 | return temp; |
170 | case VMCP_GETSIZE: | |
171 | temp = session->resp_size; | |
d9d119f1 | 172 | mutex_unlock(&session->mutex); |
8f3eabe3 | 173 | return put_user(temp, argp); |
6b979de3 | 174 | default: |
d9d119f1 | 175 | mutex_unlock(&session->mutex); |
6b979de3 CB |
176 | return -ENOIOCTLCMD; |
177 | } | |
178 | } | |
179 | ||
d54b1fdb | 180 | static const struct file_operations vmcp_fops = { |
6b979de3 | 181 | .owner = THIS_MODULE, |
d197e692 RD |
182 | .open = vmcp_open, |
183 | .release = vmcp_release, | |
184 | .read = vmcp_read, | |
185 | .write = vmcp_write, | |
186 | .unlocked_ioctl = vmcp_ioctl, | |
d9d119f1 | 187 | .compat_ioctl = vmcp_ioctl, |
6b979de3 CB |
188 | }; |
189 | ||
190 | static struct miscdevice vmcp_dev = { | |
191 | .name = "vmcp", | |
192 | .minor = MISC_DYNAMIC_MINOR, | |
193 | .fops = &vmcp_fops, | |
194 | }; | |
195 | ||
196 | static int __init vmcp_init(void) | |
197 | { | |
198 | int ret; | |
199 | ||
200 | if (!MACHINE_IS_VM) { | |
8988e58f CB |
201 | pr_warning("The z/VM CP interface device driver cannot be " |
202 | "loaded without z/VM\n"); | |
6b979de3 CB |
203 | return -ENODEV; |
204 | } | |
a44008f2 | 205 | |
66a464db | 206 | vmcp_debug = debug_register("vmcp", 1, 1, 240); |
a44008f2 | 207 | if (!vmcp_debug) |
d9d119f1 | 208 | return -ENOMEM; |
a44008f2 | 209 | |
d9d119f1 CB |
210 | ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view); |
211 | if (ret) { | |
d9d119f1 CB |
212 | debug_unregister(vmcp_debug); |
213 | return ret; | |
214 | } | |
a44008f2 | 215 | |
d9d119f1 CB |
216 | ret = misc_register(&vmcp_dev); |
217 | if (ret) { | |
d9d119f1 CB |
218 | debug_unregister(vmcp_debug); |
219 | return ret; | |
220 | } | |
a44008f2 | 221 | |
d9d119f1 | 222 | return 0; |
6b979de3 CB |
223 | } |
224 | ||
225 | static void __exit vmcp_exit(void) | |
226 | { | |
d9d119f1 | 227 | misc_deregister(&vmcp_dev); |
6b979de3 | 228 | debug_unregister(vmcp_debug); |
6b979de3 CB |
229 | } |
230 | ||
231 | module_init(vmcp_init); | |
232 | module_exit(vmcp_exit); |