Merge remote-tracking branch 'origin/develop-3.0' into develop-3.0-jb
[firefly-linux-kernel-4.4.55.git] / drivers / s390 / char / vmcp.c
1 /*
2  * Copyright IBM Corp. 2004,2010
3  * Interface implementation for communication with the z/VM control program
4  *
5  * Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
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/compat.h>
17 #include <linux/kernel.h>
18 #include <linux/miscdevice.h>
19 #include <linux/slab.h>
20 #include <asm/compat.h>
21 #include <asm/cpcmd.h>
22 #include <asm/debug.h>
23 #include <asm/uaccess.h>
24 #include "vmcp.h"
25
26 static debug_info_t *vmcp_debug;
27
28 static int vmcp_open(struct inode *inode, struct file *file)
29 {
30         struct vmcp_session *session;
31
32         if (!capable(CAP_SYS_ADMIN))
33                 return -EPERM;
34
35         session = kmalloc(sizeof(*session), GFP_KERNEL);
36         if (!session)
37                 return -ENOMEM;
38
39         session->bufsize = PAGE_SIZE;
40         session->response = NULL;
41         session->resp_size = 0;
42         mutex_init(&session->mutex);
43         file->private_data = session;
44         return nonseekable_open(inode, file);
45 }
46
47 static int vmcp_release(struct inode *inode, struct file *file)
48 {
49         struct vmcp_session *session;
50
51         session = file->private_data;
52         file->private_data = NULL;
53         free_pages((unsigned long)session->response, get_order(session->bufsize));
54         kfree(session);
55         return 0;
56 }
57
58 static ssize_t
59 vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
60 {
61         ssize_t ret;
62         size_t size;
63         struct vmcp_session *session;
64
65         session = file->private_data;
66         if (mutex_lock_interruptible(&session->mutex))
67                 return -ERESTARTSYS;
68         if (!session->response) {
69                 mutex_unlock(&session->mutex);
70                 return 0;
71         }
72         size = min_t(size_t, session->resp_size, session->bufsize);
73         ret = simple_read_from_buffer(buff, count, ppos,
74                                         session->response, size);
75
76         mutex_unlock(&session->mutex);
77
78         return ret;
79 }
80
81 static ssize_t
82 vmcp_write(struct file *file, const char __user *buff, size_t count,
83            loff_t *ppos)
84 {
85         char *cmd;
86         struct vmcp_session *session;
87
88         if (count > 240)
89                 return -EINVAL;
90         cmd = kmalloc(count + 1, GFP_KERNEL);
91         if (!cmd)
92                 return -ENOMEM;
93         if (copy_from_user(cmd, buff, count)) {
94                 kfree(cmd);
95                 return -EFAULT;
96         }
97         cmd[count] = '\0';
98         session = file->private_data;
99         if (mutex_lock_interruptible(&session->mutex)) {
100                 kfree(cmd);
101                 return -ERESTARTSYS;
102         }
103         if (!session->response)
104                 session->response = (char *)__get_free_pages(GFP_KERNEL
105                                                 | __GFP_REPEAT | GFP_DMA,
106                                                 get_order(session->bufsize));
107         if (!session->response) {
108                 mutex_unlock(&session->mutex);
109                 kfree(cmd);
110                 return -ENOMEM;
111         }
112         debug_text_event(vmcp_debug, 1, cmd);
113         session->resp_size = cpcmd(cmd, session->response, session->bufsize,
114                                    &session->resp_code);
115         mutex_unlock(&session->mutex);
116         kfree(cmd);
117         *ppos = 0;              /* reset the file pointer after a command */
118         return count;
119 }
120
121
122 /*
123  * These ioctls are available, as the semantics of the diagnose 8 call
124  * does not fit very well into a Linux call. Diagnose X'08' is described in
125  * CP Programming Services SC24-6084-00
126  *
127  * VMCP_GETCODE: gives the CP return code back to user space
128  * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
129  * expects adjacent pages in real storage and to make matters worse, we
130  * dont know the size of the response. Therefore we default to PAGESIZE and
131  * let userspace to change the response size, if userspace expects a bigger
132  * response
133  */
134 static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
135 {
136         struct vmcp_session *session;
137         int __user *argp;
138         int temp;
139
140         session = file->private_data;
141         if (is_compat_task())
142                 argp = compat_ptr(arg);
143         else
144                 argp = (int __user *)arg;
145         if (mutex_lock_interruptible(&session->mutex))
146                 return -ERESTARTSYS;
147         switch (cmd) {
148         case VMCP_GETCODE:
149                 temp = session->resp_code;
150                 mutex_unlock(&session->mutex);
151                 return put_user(temp, argp);
152         case VMCP_SETBUF:
153                 free_pages((unsigned long)session->response,
154                                 get_order(session->bufsize));
155                 session->response=NULL;
156                 temp = get_user(session->bufsize, argp);
157                 if (get_order(session->bufsize) > 8) {
158                         session->bufsize = PAGE_SIZE;
159                         temp = -EINVAL;
160                 }
161                 mutex_unlock(&session->mutex);
162                 return temp;
163         case VMCP_GETSIZE:
164                 temp = session->resp_size;
165                 mutex_unlock(&session->mutex);
166                 return put_user(temp, argp);
167         default:
168                 mutex_unlock(&session->mutex);
169                 return -ENOIOCTLCMD;
170         }
171 }
172
173 static const struct file_operations vmcp_fops = {
174         .owner          = THIS_MODULE,
175         .open           = vmcp_open,
176         .release        = vmcp_release,
177         .read           = vmcp_read,
178         .write          = vmcp_write,
179         .unlocked_ioctl = vmcp_ioctl,
180         .compat_ioctl   = vmcp_ioctl,
181         .llseek         = no_llseek,
182 };
183
184 static struct miscdevice vmcp_dev = {
185         .name   = "vmcp",
186         .minor  = MISC_DYNAMIC_MINOR,
187         .fops   = &vmcp_fops,
188 };
189
190 static int __init vmcp_init(void)
191 {
192         int ret;
193
194         if (!MACHINE_IS_VM)
195                 return 0;
196
197         vmcp_debug = debug_register("vmcp", 1, 1, 240);
198         if (!vmcp_debug)
199                 return -ENOMEM;
200
201         ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
202         if (ret) {
203                 debug_unregister(vmcp_debug);
204                 return ret;
205         }
206
207         ret = misc_register(&vmcp_dev);
208         if (ret)
209                 debug_unregister(vmcp_debug);
210         return ret;
211 }
212 device_initcall(vmcp_init);