blob: 2f419b0ea628bf1dd47ca0385f54f95bbf808235 [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
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
24MODULE_LICENSE("GPL");
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020025MODULE_AUTHOR("Christian Borntraeger <borntraeger@de.ibm.com>");
Christian Borntraeger6b979de2005-06-25 14:55:32 -070026MODULE_DESCRIPTION("z/VM CP interface");
27
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020028#define PRINTK_HEADER "vmcp: "
29
Christian Borntraeger6b979de2005-06-25 14:55:32 -070030static debug_info_t *vmcp_debug;
31
32static int vmcp_open(struct inode *inode, struct file *file)
33{
34 struct vmcp_session *session;
35
36 if (!capable(CAP_SYS_ADMIN))
37 return -EPERM;
38
39 session = kmalloc(sizeof(*session), GFP_KERNEL);
40 if (!session)
41 return -ENOMEM;
42 session->bufsize = PAGE_SIZE;
43 session->response = NULL;
44 session->resp_size = 0;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020045 mutex_init(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070046 file->private_data = session;
47 return nonseekable_open(inode, file);
48}
49
50static int vmcp_release(struct inode *inode, struct file *file)
51{
52 struct vmcp_session *session;
53
54 session = (struct vmcp_session *)file->private_data;
55 file->private_data = NULL;
56 free_pages((unsigned long)session->response, get_order(session->bufsize));
57 kfree(session);
58 return 0;
59}
60
61static ssize_t
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020062vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
Christian Borntraeger6b979de2005-06-25 14:55:32 -070063{
64 size_t tocopy;
65 struct vmcp_session *session;
66
67 session = (struct vmcp_session *)file->private_data;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020068 if (mutex_lock_interruptible(&session->mutex))
Christian Borntraeger6b979de2005-06-25 14:55:32 -070069 return -ERESTARTSYS;
70 if (!session->response) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020071 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070072 return 0;
73 }
74 if (*ppos > session->resp_size) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020075 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070076 return 0;
77 }
78 tocopy = min(session->resp_size - (size_t) (*ppos), count);
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020079 tocopy = min(tocopy, session->bufsize - (size_t) (*ppos));
Christian Borntraeger6b979de2005-06-25 14:55:32 -070080
81 if (copy_to_user(buff, session->response + (*ppos), tocopy)) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020082 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070083 return -EFAULT;
84 }
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020085 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070086 *ppos += tocopy;
87 return tocopy;
88}
89
90static ssize_t
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020091vmcp_write(struct file *file, const char __user *buff, size_t count,
92 loff_t *ppos)
Christian Borntraeger6b979de2005-06-25 14:55:32 -070093{
94 char *cmd;
95 struct vmcp_session *session;
96
97 if (count > 240)
98 return -EINVAL;
99 cmd = kmalloc(count + 1, GFP_KERNEL);
100 if (!cmd)
101 return -ENOMEM;
102 if (copy_from_user(cmd, buff, count)) {
103 kfree(cmd);
104 return -EFAULT;
105 }
106 cmd[count] = '\0';
107 session = (struct vmcp_session *)file->private_data;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200108 if (mutex_lock_interruptible(&session->mutex)) {
Christian Borntraegera5da8662005-11-07 00:59:12 -0800109 kfree(cmd);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700110 return -ERESTARTSYS;
Christian Borntraegera5da8662005-11-07 00:59:12 -0800111 }
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700112 if (!session->response)
113 session->response = (char *)__get_free_pages(GFP_KERNEL
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200114 | __GFP_REPEAT | GFP_DMA,
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700115 get_order(session->bufsize));
116 if (!session->response) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200117 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700118 kfree(cmd);
119 return -ENOMEM;
120 }
121 debug_text_event(vmcp_debug, 1, cmd);
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200122 session->resp_size = cpcmd(cmd, session->response, session->bufsize,
123 &session->resp_code);
124 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700125 kfree(cmd);
126 *ppos = 0; /* reset the file pointer after a command */
127 return count;
128}
129
130
131/*
132 * These ioctls are available, as the semantics of the diagnose 8 call
133 * does not fit very well into a Linux call. Diagnose X'08' is described in
134 * CP Programming Services SC24-6084-00
135 *
136 * VMCP_GETCODE: gives the CP return code back to user space
137 * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
138 * expects adjacent pages in real storage and to make matters worse, we
139 * dont know the size of the response. Therefore we default to PAGESIZE and
140 * let userspace to change the response size, if userspace expects a bigger
141 * response
142 */
143static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
144{
145 struct vmcp_session *session;
146 int temp;
147
148 session = (struct vmcp_session *)file->private_data;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200149 if (mutex_lock_interruptible(&session->mutex))
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700150 return -ERESTARTSYS;
151 switch (cmd) {
152 case VMCP_GETCODE:
153 temp = session->resp_code;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200154 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700155 return put_user(temp, (int __user *)arg);
156 case VMCP_SETBUF:
157 free_pages((unsigned long)session->response,
158 get_order(session->bufsize));
159 session->response=NULL;
160 temp = get_user(session->bufsize, (int __user *)arg);
161 if (get_order(session->bufsize) > 8) {
162 session->bufsize = PAGE_SIZE;
163 temp = -EINVAL;
164 }
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200165 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700166 return temp;
167 case VMCP_GETSIZE:
168 temp = session->resp_size;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200169 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700170 return put_user(temp, (int __user *)arg);
171 default:
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200172 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700173 return -ENOIOCTLCMD;
174 }
175}
176
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800177static const struct file_operations vmcp_fops = {
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700178 .owner = THIS_MODULE,
Robert P. J. Dayd197e692007-07-10 11:24:07 +0200179 .open = vmcp_open,
180 .release = vmcp_release,
181 .read = vmcp_read,
182 .write = vmcp_write,
183 .unlocked_ioctl = vmcp_ioctl,
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200184 .compat_ioctl = vmcp_ioctl,
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700185};
186
187static struct miscdevice vmcp_dev = {
188 .name = "vmcp",
189 .minor = MISC_DYNAMIC_MINOR,
190 .fops = &vmcp_fops,
191};
192
193static int __init vmcp_init(void)
194{
195 int ret;
196
197 if (!MACHINE_IS_VM) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200198 PRINT_WARN("z/VM CP interface is only available under z/VM\n");
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700199 return -ENODEV;
200 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700201 vmcp_debug = debug_register("vmcp", 1, 1, 240);
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200202 if (!vmcp_debug) {
203 PRINT_ERR("z/VM CP interface not loaded. Could not register "
204 "debug feature\n");
205 return -ENOMEM;
206 }
207 ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
208 if (ret) {
209 PRINT_ERR("z/VM CP interface not loaded. Could not register "
210 "debug feature view. Error code: %d\n", ret);
211 debug_unregister(vmcp_debug);
212 return ret;
213 }
214 ret = misc_register(&vmcp_dev);
215 if (ret) {
216 PRINT_ERR("z/VM CP interface not loaded. Could not register "
217 "misc device. Error code: %d\n", ret);
218 debug_unregister(vmcp_debug);
219 return ret;
220 }
221 PRINT_INFO("z/VM CP interface loaded\n");
222 return 0;
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700223}
224
225static void __exit vmcp_exit(void)
226{
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200227 misc_deregister(&vmcp_dev);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700228 debug_unregister(vmcp_debug);
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200229 PRINT_INFO("z/VM CP interface unloaded.\n");
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700230}
231
232module_init(vmcp_init);
233module_exit(vmcp_exit);