Merge git://git.kernel.org/pub/scm/linux/kernel/git/hirofumi/fatfs-2.6
[pandora-kernel.git] / drivers / s390 / char / vmcp.c
1 /*
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>
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 #define KMSG_COMPONENT "vmcp"
15 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16
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>
22 #include <asm/compat.h>
23 #include <asm/cpcmd.h>
24 #include <asm/debug.h>
25 #include <asm/uaccess.h>
26 #include "vmcp.h"
27
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Christian Borntraeger <borntraeger@de.ibm.com>");
30 MODULE_DESCRIPTION("z/VM CP interface");
31
32 static debug_info_t *vmcp_debug;
33
34 static int vmcp_open(struct inode *inode, struct file *file)
35 {
36         struct vmcp_session *session;
37
38         if (!capable(CAP_SYS_ADMIN))
39                 return -EPERM;
40
41         session = kmalloc(sizeof(*session), GFP_KERNEL);
42         if (!session)
43                 return -ENOMEM;
44
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);
51 }
52
53 static int vmcp_release(struct inode *inode, struct file *file)
54 {
55         struct vmcp_session *session;
56
57         session = (struct vmcp_session *)file->private_data;
58         file->private_data = NULL;
59         free_pages((unsigned long)session->response, get_order(session->bufsize));
60         kfree(session);
61         return 0;
62 }
63
64 static ssize_t
65 vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
66 {
67         ssize_t ret;
68         size_t size;
69         struct vmcp_session *session;
70
71         session = file->private_data;
72         if (mutex_lock_interruptible(&session->mutex))
73                 return -ERESTARTSYS;
74         if (!session->response) {
75                 mutex_unlock(&session->mutex);
76                 return 0;
77         }
78         size = min_t(size_t, session->resp_size, session->bufsize);
79         ret = simple_read_from_buffer(buff, count, ppos,
80                                         session->response, size);
81
82         mutex_unlock(&session->mutex);
83
84         return ret;
85 }
86
87 static ssize_t
88 vmcp_write(struct file *file, const char __user *buff, size_t count,
89            loff_t *ppos)
90 {
91         char *cmd;
92         struct vmcp_session *session;
93
94         if (count > 240)
95                 return -EINVAL;
96         cmd = kmalloc(count + 1, GFP_KERNEL);
97         if (!cmd)
98                 return -ENOMEM;
99         if (copy_from_user(cmd, buff, count)) {
100                 kfree(cmd);
101                 return -EFAULT;
102         }
103         cmd[count] = '\0';
104         session = (struct vmcp_session *)file->private_data;
105         if (mutex_lock_interruptible(&session->mutex)) {
106                 kfree(cmd);
107                 return -ERESTARTSYS;
108         }
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);
115                 kfree(cmd);
116                 return -ENOMEM;
117         }
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);
122         kfree(cmd);
123         *ppos = 0;              /* reset the file pointer after a command */
124         return count;
125 }
126
127
128 /*
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
132  *
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
138  * response
139  */
140 static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
141 {
142         struct vmcp_session *session;
143         int __user *argp;
144         int temp;
145
146         session = (struct vmcp_session *)file->private_data;
147         if (is_compat_task())
148                 argp = compat_ptr(arg);
149         else
150                 argp = (int __user *)arg;
151         if (mutex_lock_interruptible(&session->mutex))
152                 return -ERESTARTSYS;
153         switch (cmd) {
154         case VMCP_GETCODE:
155                 temp = session->resp_code;
156                 mutex_unlock(&session->mutex);
157                 return put_user(temp, argp);
158         case VMCP_SETBUF:
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;
165                         temp = -EINVAL;
166                 }
167                 mutex_unlock(&session->mutex);
168                 return temp;
169         case VMCP_GETSIZE:
170                 temp = session->resp_size;
171                 mutex_unlock(&session->mutex);
172                 return put_user(temp, argp);
173         default:
174                 mutex_unlock(&session->mutex);
175                 return -ENOIOCTLCMD;
176         }
177 }
178
179 static const struct file_operations vmcp_fops = {
180         .owner          = THIS_MODULE,
181         .open           = vmcp_open,
182         .release        = vmcp_release,
183         .read           = vmcp_read,
184         .write          = vmcp_write,
185         .unlocked_ioctl = vmcp_ioctl,
186         .compat_ioctl   = vmcp_ioctl,
187 };
188
189 static struct miscdevice vmcp_dev = {
190         .name   = "vmcp",
191         .minor  = MISC_DYNAMIC_MINOR,
192         .fops   = &vmcp_fops,
193 };
194
195 static int __init vmcp_init(void)
196 {
197         int ret;
198
199         if (!MACHINE_IS_VM) {
200                 pr_warning("The z/VM CP interface device driver cannot be "
201                            "loaded without z/VM\n");
202                 return -ENODEV;
203         }
204
205         vmcp_debug = debug_register("vmcp", 1, 1, 240);
206         if (!vmcp_debug)
207                 return -ENOMEM;
208
209         ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
210         if (ret) {
211                 debug_unregister(vmcp_debug);
212                 return ret;
213         }
214
215         ret = misc_register(&vmcp_dev);
216         if (ret) {
217                 debug_unregister(vmcp_debug);
218                 return ret;
219         }
220
221         return 0;
222 }
223
224 static void __exit vmcp_exit(void)
225 {
226         misc_deregister(&vmcp_dev);
227         debug_unregister(vmcp_debug);
228 }
229
230 module_init(vmcp_init);
231 module_exit(vmcp_exit);