Commit | Line | Data |
---|---|---|
6b979de3 CB |
1 | /* |
2 | * Copyright (C) 2004,2005 IBM Corporation | |
45af3af8 | 3 | * Interface implementation for communication with the z/VM control program |
6b979de3 CB |
4 | * Author(s): Christian Borntraeger <cborntra@de.ibm.com> |
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 | ||
14 | #include <linux/fs.h> | |
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> | |
22 | #include "vmcp.h" | |
23 | ||
24 | MODULE_LICENSE("GPL"); | |
25 | MODULE_AUTHOR("Christian Borntraeger <cborntra@de.ibm.com>"); | |
26 | MODULE_DESCRIPTION("z/VM CP interface"); | |
27 | ||
28 | static debug_info_t *vmcp_debug; | |
29 | ||
30 | static int vmcp_open(struct inode *inode, struct file *file) | |
31 | { | |
32 | struct vmcp_session *session; | |
33 | ||
34 | if (!capable(CAP_SYS_ADMIN)) | |
35 | return -EPERM; | |
36 | ||
37 | session = kmalloc(sizeof(*session), GFP_KERNEL); | |
38 | if (!session) | |
39 | return -ENOMEM; | |
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); | |
46 | } | |
47 | ||
48 | static int vmcp_release(struct inode *inode, struct file *file) | |
49 | { | |
50 | struct vmcp_session *session; | |
51 | ||
52 | session = (struct vmcp_session *)file->private_data; | |
53 | file->private_data = NULL; | |
54 | free_pages((unsigned long)session->response, get_order(session->bufsize)); | |
55 | kfree(session); | |
56 | return 0; | |
57 | } | |
58 | ||
59 | static ssize_t | |
60 | vmcp_read(struct file *file, char __user * buff, size_t count, loff_t * ppos) | |
61 | { | |
62 | size_t tocopy; | |
63 | struct vmcp_session *session; | |
64 | ||
65 | session = (struct vmcp_session *)file->private_data; | |
66 | if (down_interruptible(&session->mutex)) | |
67 | return -ERESTARTSYS; | |
68 | if (!session->response) { | |
69 | up(&session->mutex); | |
70 | return 0; | |
71 | } | |
72 | if (*ppos > session->resp_size) { | |
73 | up(&session->mutex); | |
74 | return 0; | |
75 | } | |
76 | tocopy = min(session->resp_size - (size_t) (*ppos), count); | |
77 | tocopy = min(tocopy,session->bufsize - (size_t) (*ppos)); | |
78 | ||
79 | if (copy_to_user(buff, session->response + (*ppos), tocopy)) { | |
80 | up(&session->mutex); | |
81 | return -EFAULT; | |
82 | } | |
83 | up(&session->mutex); | |
84 | *ppos += tocopy; | |
85 | return tocopy; | |
86 | } | |
87 | ||
88 | static ssize_t | |
89 | vmcp_write(struct file *file, const char __user * buff, size_t count, | |
90 | loff_t * ppos) | |
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; | |
a5da866f CB |
106 | if (down_interruptible(&session->mutex)) { |
107 | kfree(cmd); | |
6b979de3 | 108 | return -ERESTARTSYS; |
a5da866f | 109 | } |
6b979de3 CB |
110 | if (!session->response) |
111 | session->response = (char *)__get_free_pages(GFP_KERNEL | |
112 | | __GFP_REPEAT | GFP_DMA, | |
113 | get_order(session->bufsize)); | |
114 | if (!session->response) { | |
115 | up(&session->mutex); | |
116 | kfree(cmd); | |
117 | return -ENOMEM; | |
118 | } | |
119 | debug_text_event(vmcp_debug, 1, cmd); | |
3e5ea098 CB |
120 | session->resp_size = __cpcmd(cmd, session->response, |
121 | session->bufsize, | |
122 | &session->resp_code); | |
6b979de3 CB |
123 | up(&session->mutex); |
124 | kfree(cmd); | |
125 | *ppos = 0; /* reset the file pointer after a command */ | |
126 | return count; | |
127 | } | |
128 | ||
129 | ||
130 | /* | |
131 | * These ioctls are available, as the semantics of the diagnose 8 call | |
132 | * does not fit very well into a Linux call. Diagnose X'08' is described in | |
133 | * CP Programming Services SC24-6084-00 | |
134 | * | |
135 | * VMCP_GETCODE: gives the CP return code back to user space | |
136 | * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8 | |
137 | * expects adjacent pages in real storage and to make matters worse, we | |
138 | * dont know the size of the response. Therefore we default to PAGESIZE and | |
139 | * let userspace to change the response size, if userspace expects a bigger | |
140 | * response | |
141 | */ | |
142 | static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
143 | { | |
144 | struct vmcp_session *session; | |
145 | int temp; | |
146 | ||
147 | session = (struct vmcp_session *)file->private_data; | |
148 | if (down_interruptible(&session->mutex)) | |
149 | return -ERESTARTSYS; | |
150 | switch (cmd) { | |
151 | case VMCP_GETCODE: | |
152 | temp = session->resp_code; | |
153 | up(&session->mutex); | |
154 | return put_user(temp, (int __user *)arg); | |
155 | case VMCP_SETBUF: | |
156 | free_pages((unsigned long)session->response, | |
157 | get_order(session->bufsize)); | |
158 | session->response=NULL; | |
159 | temp = get_user(session->bufsize, (int __user *)arg); | |
160 | if (get_order(session->bufsize) > 8) { | |
161 | session->bufsize = PAGE_SIZE; | |
162 | temp = -EINVAL; | |
163 | } | |
164 | up(&session->mutex); | |
165 | return temp; | |
166 | case VMCP_GETSIZE: | |
167 | temp = session->resp_size; | |
168 | up(&session->mutex); | |
169 | return put_user(temp, (int __user *)arg); | |
170 | default: | |
171 | up(&session->mutex); | |
172 | return -ENOIOCTLCMD; | |
173 | } | |
174 | } | |
175 | ||
176 | static struct file_operations vmcp_fops = { | |
177 | .owner = THIS_MODULE, | |
178 | .open = &vmcp_open, | |
179 | .release = &vmcp_release, | |
180 | .read = &vmcp_read, | |
181 | .llseek = &no_llseek, | |
182 | .write = &vmcp_write, | |
183 | .unlocked_ioctl = &vmcp_ioctl, | |
184 | .compat_ioctl = &vmcp_ioctl | |
185 | }; | |
186 | ||
187 | static struct miscdevice vmcp_dev = { | |
188 | .name = "vmcp", | |
189 | .minor = MISC_DYNAMIC_MINOR, | |
190 | .fops = &vmcp_fops, | |
191 | }; | |
192 | ||
193 | static int __init vmcp_init(void) | |
194 | { | |
195 | int ret; | |
196 | ||
197 | if (!MACHINE_IS_VM) { | |
198 | printk(KERN_WARNING | |
199 | "z/VM CP interface is only available under z/VM\n"); | |
200 | return -ENODEV; | |
201 | } | |
202 | ret = misc_register(&vmcp_dev); | |
203 | if (!ret) | |
204 | printk(KERN_INFO "z/VM CP interface loaded\n"); | |
205 | else | |
206 | printk(KERN_WARNING | |
207 | "z/VM CP interface not loaded. Could not register misc device.\n"); | |
66a464db | 208 | vmcp_debug = debug_register("vmcp", 1, 1, 240); |
6b979de3 CB |
209 | debug_register_view(vmcp_debug, &debug_hex_ascii_view); |
210 | return ret; | |
211 | } | |
212 | ||
213 | static void __exit vmcp_exit(void) | |
214 | { | |
215 | WARN_ON(misc_deregister(&vmcp_dev) != 0); | |
216 | debug_unregister(vmcp_debug); | |
217 | printk(KERN_INFO "z/VM CP interface unloaded.\n"); | |
218 | } | |
219 | ||
220 | module_init(vmcp_init); | |
221 | module_exit(vmcp_exit); |