blob: a6087cec55b463fc0237e1fe1774d010a69d038e [file] [log] [blame]
Christian Borntraeger6b979de2005-06-25 14:55:32 -07001/*
Christian Borntraegerd9d119f2007-07-17 13:36:05 +02002 * Copyright IBM Corp. 2004,2007
Christian Borntraeger45af3af2006-09-20 15:59:24 +02003 * Interface implementation for communication with the z/VM control program
Christian Borntraegerd9d119f2007-07-17 13:36:05 +02004 * Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
Christian Borntraeger6b979de2005-06-25 14:55:32 -07005 *
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
Christian Borntraeger8988e582008-12-25 13:39:32 +010014#define KMSG_COMPONENT "vmcp"
15#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16
Christian Borntraeger6b979de2005-06-25 14:55:32 -070017#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/cpcmd.h>
23#include <asm/debug.h>
24#include <asm/uaccess.h>
25#include "vmcp.h"
26
27MODULE_LICENSE("GPL");
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020028MODULE_AUTHOR("Christian Borntraeger <borntraeger@de.ibm.com>");
Christian Borntraeger6b979de2005-06-25 14:55:32 -070029MODULE_DESCRIPTION("z/VM CP interface");
30
31static debug_info_t *vmcp_debug;
32
33static int vmcp_open(struct inode *inode, struct file *file)
34{
35 struct vmcp_session *session;
36
37 if (!capable(CAP_SYS_ADMIN))
38 return -EPERM;
39
40 session = kmalloc(sizeof(*session), GFP_KERNEL);
41 if (!session)
42 return -ENOMEM;
Arnd Bergmann6c111d82008-05-20 19:16:59 +020043
Christian Borntraeger6b979de2005-06-25 14:55:32 -070044 session->bufsize = PAGE_SIZE;
45 session->response = NULL;
46 session->resp_size = 0;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020047 mutex_init(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070048 file->private_data = session;
49 return nonseekable_open(inode, file);
50}
51
52static int vmcp_release(struct inode *inode, struct file *file)
53{
54 struct vmcp_session *session;
55
56 session = (struct vmcp_session *)file->private_data;
57 file->private_data = NULL;
58 free_pages((unsigned long)session->response, get_order(session->bufsize));
59 kfree(session);
60 return 0;
61}
62
63static ssize_t
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020064vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
Christian Borntraeger6b979de2005-06-25 14:55:32 -070065{
Akinobu Mita77858572008-07-14 09:59:14 +020066 ssize_t ret;
67 size_t size;
Christian Borntraeger6b979de2005-06-25 14:55:32 -070068 struct vmcp_session *session;
69
Akinobu Mita77858572008-07-14 09:59:14 +020070 session = file->private_data;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020071 if (mutex_lock_interruptible(&session->mutex))
Christian Borntraeger6b979de2005-06-25 14:55:32 -070072 return -ERESTARTSYS;
73 if (!session->response) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020074 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070075 return 0;
76 }
Akinobu Mita77858572008-07-14 09:59:14 +020077 size = min_t(size_t, session->resp_size, session->bufsize);
78 ret = simple_read_from_buffer(buff, count, ppos,
79 session->response, size);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070080
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020081 mutex_unlock(&session->mutex);
Akinobu Mita77858572008-07-14 09:59:14 +020082
83 return ret;
Christian Borntraeger6b979de2005-06-25 14:55:32 -070084}
85
86static ssize_t
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020087vmcp_write(struct file *file, const char __user *buff, size_t count,
88 loff_t *ppos)
Christian Borntraeger6b979de2005-06-25 14:55:32 -070089{
90 char *cmd;
91 struct vmcp_session *session;
92
93 if (count > 240)
94 return -EINVAL;
95 cmd = kmalloc(count + 1, GFP_KERNEL);
96 if (!cmd)
97 return -ENOMEM;
98 if (copy_from_user(cmd, buff, count)) {
99 kfree(cmd);
100 return -EFAULT;
101 }
102 cmd[count] = '\0';
103 session = (struct vmcp_session *)file->private_data;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200104 if (mutex_lock_interruptible(&session->mutex)) {
Christian Borntraegera5da8662005-11-07 00:59:12 -0800105 kfree(cmd);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700106 return -ERESTARTSYS;
Christian Borntraegera5da8662005-11-07 00:59:12 -0800107 }
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700108 if (!session->response)
109 session->response = (char *)__get_free_pages(GFP_KERNEL
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200110 | __GFP_REPEAT | GFP_DMA,
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700111 get_order(session->bufsize));
112 if (!session->response) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200113 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700114 kfree(cmd);
115 return -ENOMEM;
116 }
117 debug_text_event(vmcp_debug, 1, cmd);
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200118 session->resp_size = cpcmd(cmd, session->response, session->bufsize,
119 &session->resp_code);
120 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700121 kfree(cmd);
122 *ppos = 0; /* reset the file pointer after a command */
123 return count;
124}
125
126
127/*
128 * These ioctls are available, as the semantics of the diagnose 8 call
129 * does not fit very well into a Linux call. Diagnose X'08' is described in
130 * CP Programming Services SC24-6084-00
131 *
132 * VMCP_GETCODE: gives the CP return code back to user space
133 * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
134 * expects adjacent pages in real storage and to make matters worse, we
135 * dont know the size of the response. Therefore we default to PAGESIZE and
136 * let userspace to change the response size, if userspace expects a bigger
137 * response
138 */
139static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
140{
141 struct vmcp_session *session;
142 int temp;
143
144 session = (struct vmcp_session *)file->private_data;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200145 if (mutex_lock_interruptible(&session->mutex))
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700146 return -ERESTARTSYS;
147 switch (cmd) {
148 case VMCP_GETCODE:
149 temp = session->resp_code;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200150 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700151 return put_user(temp, (int __user *)arg);
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, (int __user *)arg);
157 if (get_order(session->bufsize) > 8) {
158 session->bufsize = PAGE_SIZE;
159 temp = -EINVAL;
160 }
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200161 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700162 return temp;
163 case VMCP_GETSIZE:
164 temp = session->resp_size;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200165 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700166 return put_user(temp, (int __user *)arg);
167 default:
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200168 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700169 return -ENOIOCTLCMD;
170 }
171}
172
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800173static const struct file_operations vmcp_fops = {
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700174 .owner = THIS_MODULE,
Robert P. J. Dayd197e692007-07-10 11:24:07 +0200175 .open = vmcp_open,
176 .release = vmcp_release,
177 .read = vmcp_read,
178 .write = vmcp_write,
179 .unlocked_ioctl = vmcp_ioctl,
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200180 .compat_ioctl = vmcp_ioctl,
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700181};
182
183static struct miscdevice vmcp_dev = {
184 .name = "vmcp",
185 .minor = MISC_DYNAMIC_MINOR,
186 .fops = &vmcp_fops,
187};
188
189static int __init vmcp_init(void)
190{
191 int ret;
192
193 if (!MACHINE_IS_VM) {
Christian Borntraeger8988e582008-12-25 13:39:32 +0100194 pr_warning("The z/VM CP interface device driver cannot be "
195 "loaded without z/VM\n");
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700196 return -ENODEV;
197 }
Christian Borntraegera44008f2008-07-14 09:59:43 +0200198
Michael Holzheu66a464d2005-06-25 14:55:33 -0700199 vmcp_debug = debug_register("vmcp", 1, 1, 240);
Christian Borntraegera44008f2008-07-14 09:59:43 +0200200 if (!vmcp_debug)
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200201 return -ENOMEM;
Christian Borntraegera44008f2008-07-14 09:59:43 +0200202
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200203 ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
204 if (ret) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200205 debug_unregister(vmcp_debug);
206 return ret;
207 }
Christian Borntraegera44008f2008-07-14 09:59:43 +0200208
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200209 ret = misc_register(&vmcp_dev);
210 if (ret) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200211 debug_unregister(vmcp_debug);
212 return ret;
213 }
Christian Borntraegera44008f2008-07-14 09:59:43 +0200214
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200215 return 0;
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700216}
217
218static void __exit vmcp_exit(void)
219{
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200220 misc_deregister(&vmcp_dev);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700221 debug_unregister(vmcp_debug);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700222}
223
224module_init(vmcp_init);
225module_exit(vmcp_exit);