blob: bf3dc6e0e33af13f1468dfe3fe26e89b67d13dff [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>
Arnd Bergmann6c111d82008-05-20 19:16:59 +020019#include <linux/smp_lock.h>
Christian Borntraeger6b979de2005-06-25 14:55:32 -070020#include <asm/cpcmd.h>
21#include <asm/debug.h>
22#include <asm/uaccess.h>
23#include "vmcp.h"
24
25MODULE_LICENSE("GPL");
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020026MODULE_AUTHOR("Christian Borntraeger <borntraeger@de.ibm.com>");
Christian Borntraeger6b979de2005-06-25 14:55:32 -070027MODULE_DESCRIPTION("z/VM CP interface");
28
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020029#define PRINTK_HEADER "vmcp: "
30
Christian Borntraeger6b979de2005-06-25 14:55:32 -070031static 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
44 lock_kernel();
Christian Borntraeger6b979de2005-06-25 14:55:32 -070045 session->bufsize = PAGE_SIZE;
46 session->response = NULL;
47 session->resp_size = 0;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020048 mutex_init(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070049 file->private_data = session;
Arnd Bergmann6c111d82008-05-20 19:16:59 +020050 unlock_kernel();
Christian Borntraeger6b979de2005-06-25 14:55:32 -070051 return nonseekable_open(inode, file);
52}
53
54static int vmcp_release(struct inode *inode, struct file *file)
55{
56 struct vmcp_session *session;
57
58 session = (struct vmcp_session *)file->private_data;
59 file->private_data = NULL;
60 free_pages((unsigned long)session->response, get_order(session->bufsize));
61 kfree(session);
62 return 0;
63}
64
65static ssize_t
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020066vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
Christian Borntraeger6b979de2005-06-25 14:55:32 -070067{
68 size_t tocopy;
69 struct vmcp_session *session;
70
71 session = (struct vmcp_session *)file->private_data;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020072 if (mutex_lock_interruptible(&session->mutex))
Christian Borntraeger6b979de2005-06-25 14:55:32 -070073 return -ERESTARTSYS;
74 if (!session->response) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020075 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070076 return 0;
77 }
78 if (*ppos > session->resp_size) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020079 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070080 return 0;
81 }
82 tocopy = min(session->resp_size - (size_t) (*ppos), count);
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020083 tocopy = min(tocopy, session->bufsize - (size_t) (*ppos));
Christian Borntraeger6b979de2005-06-25 14:55:32 -070084
85 if (copy_to_user(buff, session->response + (*ppos), tocopy)) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020086 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070087 return -EFAULT;
88 }
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020089 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070090 *ppos += tocopy;
91 return tocopy;
92}
93
94static ssize_t
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020095vmcp_write(struct file *file, const char __user *buff, size_t count,
96 loff_t *ppos)
Christian Borntraeger6b979de2005-06-25 14:55:32 -070097{
98 char *cmd;
99 struct vmcp_session *session;
100
101 if (count > 240)
102 return -EINVAL;
103 cmd = kmalloc(count + 1, GFP_KERNEL);
104 if (!cmd)
105 return -ENOMEM;
106 if (copy_from_user(cmd, buff, count)) {
107 kfree(cmd);
108 return -EFAULT;
109 }
110 cmd[count] = '\0';
111 session = (struct vmcp_session *)file->private_data;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200112 if (mutex_lock_interruptible(&session->mutex)) {
Christian Borntraegera5da8662005-11-07 00:59:12 -0800113 kfree(cmd);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700114 return -ERESTARTSYS;
Christian Borntraegera5da8662005-11-07 00:59:12 -0800115 }
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700116 if (!session->response)
117 session->response = (char *)__get_free_pages(GFP_KERNEL
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200118 | __GFP_REPEAT | GFP_DMA,
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700119 get_order(session->bufsize));
120 if (!session->response) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200121 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700122 kfree(cmd);
123 return -ENOMEM;
124 }
125 debug_text_event(vmcp_debug, 1, cmd);
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200126 session->resp_size = cpcmd(cmd, session->response, session->bufsize,
127 &session->resp_code);
128 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700129 kfree(cmd);
130 *ppos = 0; /* reset the file pointer after a command */
131 return count;
132}
133
134
135/*
136 * These ioctls are available, as the semantics of the diagnose 8 call
137 * does not fit very well into a Linux call. Diagnose X'08' is described in
138 * CP Programming Services SC24-6084-00
139 *
140 * VMCP_GETCODE: gives the CP return code back to user space
141 * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
142 * expects adjacent pages in real storage and to make matters worse, we
143 * dont know the size of the response. Therefore we default to PAGESIZE and
144 * let userspace to change the response size, if userspace expects a bigger
145 * response
146 */
147static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
148{
149 struct vmcp_session *session;
150 int temp;
151
152 session = (struct vmcp_session *)file->private_data;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200153 if (mutex_lock_interruptible(&session->mutex))
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700154 return -ERESTARTSYS;
155 switch (cmd) {
156 case VMCP_GETCODE:
157 temp = session->resp_code;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200158 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700159 return put_user(temp, (int __user *)arg);
160 case VMCP_SETBUF:
161 free_pages((unsigned long)session->response,
162 get_order(session->bufsize));
163 session->response=NULL;
164 temp = get_user(session->bufsize, (int __user *)arg);
165 if (get_order(session->bufsize) > 8) {
166 session->bufsize = PAGE_SIZE;
167 temp = -EINVAL;
168 }
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200169 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700170 return temp;
171 case VMCP_GETSIZE:
172 temp = session->resp_size;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200173 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700174 return put_user(temp, (int __user *)arg);
175 default:
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200176 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700177 return -ENOIOCTLCMD;
178 }
179}
180
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800181static const struct file_operations vmcp_fops = {
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700182 .owner = THIS_MODULE,
Robert P. J. Dayd197e692007-07-10 11:24:07 +0200183 .open = vmcp_open,
184 .release = vmcp_release,
185 .read = vmcp_read,
186 .write = vmcp_write,
187 .unlocked_ioctl = vmcp_ioctl,
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200188 .compat_ioctl = vmcp_ioctl,
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700189};
190
191static struct miscdevice vmcp_dev = {
192 .name = "vmcp",
193 .minor = MISC_DYNAMIC_MINOR,
194 .fops = &vmcp_fops,
195};
196
197static int __init vmcp_init(void)
198{
199 int ret;
200
201 if (!MACHINE_IS_VM) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200202 PRINT_WARN("z/VM CP interface is only available under z/VM\n");
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700203 return -ENODEV;
204 }
Michael Holzheu66a464d2005-06-25 14:55:33 -0700205 vmcp_debug = debug_register("vmcp", 1, 1, 240);
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200206 if (!vmcp_debug) {
207 PRINT_ERR("z/VM CP interface not loaded. Could not register "
208 "debug feature\n");
209 return -ENOMEM;
210 }
211 ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
212 if (ret) {
213 PRINT_ERR("z/VM CP interface not loaded. Could not register "
214 "debug feature view. Error code: %d\n", ret);
215 debug_unregister(vmcp_debug);
216 return ret;
217 }
218 ret = misc_register(&vmcp_dev);
219 if (ret) {
220 PRINT_ERR("z/VM CP interface not loaded. Could not register "
221 "misc device. Error code: %d\n", ret);
222 debug_unregister(vmcp_debug);
223 return ret;
224 }
225 PRINT_INFO("z/VM CP interface loaded\n");
226 return 0;
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700227}
228
229static void __exit vmcp_exit(void)
230{
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200231 misc_deregister(&vmcp_dev);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700232 debug_unregister(vmcp_debug);
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200233 PRINT_INFO("z/VM CP interface unloaded.\n");
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700234}
235
236module_init(vmcp_init);
237module_exit(vmcp_exit);