blob: 2a67b496a9e28869dd9443aa5b823be6003b900f [file] [log] [blame]
Christian Borntraeger6b979de2005-06-25 14:55:32 -07001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * Copyright IBM Corp. 2004, 2010
Christian Borntraeger45af3af2006-09-20 15:59:24 +02003 * Interface implementation for communication with the z/VM control program
Christian Borntraeger6b979de2005-06-25 14:55:32 -07004 *
Heiko Carstensf73a2b02010-05-17 10:00:06 +02005 * Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
Christian Borntraeger6b979de2005-06-25 14:55:32 -07006 *
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.
Heiko Carstensf73a2b02010-05-17 10:00:06 +020010 *
Christian Borntraeger6b979de2005-06-25 14:55:32 -070011 * 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>
Heiko Carstens048cd4e2012-02-27 10:01:52 +010016#include <linux/compat.h>
Christian Borntraeger6b979de2005-06-25 14:55:32 -070017#include <linux/kernel.h>
18#include <linux/miscdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Heiko Carstens3a4c5d52011-07-30 09:25:15 +020020#include <linux/export.h>
Heiko Carstens8f3eabe2010-01-13 20:44:31 +010021#include <asm/compat.h>
Christian Borntraeger6b979de2005-06-25 14:55:32 -070022#include <asm/cpcmd.h>
23#include <asm/debug.h>
24#include <asm/uaccess.h>
25#include "vmcp.h"
26
Christian Borntraeger6b979de2005-06-25 14:55:32 -070027static debug_info_t *vmcp_debug;
28
29static int vmcp_open(struct inode *inode, struct file *file)
30{
31 struct vmcp_session *session;
32
33 if (!capable(CAP_SYS_ADMIN))
34 return -EPERM;
35
36 session = kmalloc(sizeof(*session), GFP_KERNEL);
37 if (!session)
38 return -ENOMEM;
Arnd Bergmann6c111d82008-05-20 19:16:59 +020039
Christian Borntraeger6b979de2005-06-25 14:55:32 -070040 session->bufsize = PAGE_SIZE;
41 session->response = NULL;
42 session->resp_size = 0;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020043 mutex_init(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070044 file->private_data = session;
45 return nonseekable_open(inode, file);
46}
47
48static int vmcp_release(struct inode *inode, struct file *file)
49{
50 struct vmcp_session *session;
51
Joe Perches27e49942010-09-04 18:52:45 -070052 session = file->private_data;
Christian Borntraeger6b979de2005-06-25 14:55:32 -070053 file->private_data = NULL;
54 free_pages((unsigned long)session->response, get_order(session->bufsize));
55 kfree(session);
56 return 0;
57}
58
59static ssize_t
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020060vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
Christian Borntraeger6b979de2005-06-25 14:55:32 -070061{
Akinobu Mita77858572008-07-14 09:59:14 +020062 ssize_t ret;
63 size_t size;
Christian Borntraeger6b979de2005-06-25 14:55:32 -070064 struct vmcp_session *session;
65
Akinobu Mita77858572008-07-14 09:59:14 +020066 session = file->private_data;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020067 if (mutex_lock_interruptible(&session->mutex))
Christian Borntraeger6b979de2005-06-25 14:55:32 -070068 return -ERESTARTSYS;
69 if (!session->response) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020070 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070071 return 0;
72 }
Akinobu Mita77858572008-07-14 09:59:14 +020073 size = min_t(size_t, session->resp_size, session->bufsize);
74 ret = simple_read_from_buffer(buff, count, ppos,
75 session->response, size);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070076
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020077 mutex_unlock(&session->mutex);
Akinobu Mita77858572008-07-14 09:59:14 +020078
79 return ret;
Christian Borntraeger6b979de2005-06-25 14:55:32 -070080}
81
82static ssize_t
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020083vmcp_write(struct file *file, const char __user *buff, size_t count,
84 loff_t *ppos)
Christian Borntraeger6b979de2005-06-25 14:55:32 -070085{
86 char *cmd;
87 struct vmcp_session *session;
88
89 if (count > 240)
90 return -EINVAL;
Al Viro16e5c1f2015-12-24 00:06:05 -050091 cmd = memdup_user_nul(buff, count);
92 if (IS_ERR(cmd))
93 return PTR_ERR(cmd);
Joe Perches27e49942010-09-04 18:52:45 -070094 session = file->private_data;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +020095 if (mutex_lock_interruptible(&session->mutex)) {
Christian Borntraegera5da8662005-11-07 00:59:12 -080096 kfree(cmd);
Christian Borntraeger6b979de2005-06-25 14:55:32 -070097 return -ERESTARTSYS;
Christian Borntraegera5da8662005-11-07 00:59:12 -080098 }
Christian Borntraeger6b979de2005-06-25 14:55:32 -070099 if (!session->response)
100 session->response = (char *)__get_free_pages(GFP_KERNEL
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200101 | __GFP_REPEAT | GFP_DMA,
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700102 get_order(session->bufsize));
103 if (!session->response) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200104 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700105 kfree(cmd);
106 return -ENOMEM;
107 }
108 debug_text_event(vmcp_debug, 1, cmd);
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200109 session->resp_size = cpcmd(cmd, session->response, session->bufsize,
110 &session->resp_code);
111 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700112 kfree(cmd);
113 *ppos = 0; /* reset the file pointer after a command */
114 return count;
115}
116
117
118/*
119 * These ioctls are available, as the semantics of the diagnose 8 call
120 * does not fit very well into a Linux call. Diagnose X'08' is described in
121 * CP Programming Services SC24-6084-00
122 *
123 * VMCP_GETCODE: gives the CP return code back to user space
124 * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
125 * expects adjacent pages in real storage and to make matters worse, we
126 * dont know the size of the response. Therefore we default to PAGESIZE and
127 * let userspace to change the response size, if userspace expects a bigger
128 * response
129 */
130static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
131{
132 struct vmcp_session *session;
Heiko Carstens8f3eabe2010-01-13 20:44:31 +0100133 int __user *argp;
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700134 int temp;
135
Joe Perches27e49942010-09-04 18:52:45 -0700136 session = file->private_data;
Heiko Carstens8f3eabe2010-01-13 20:44:31 +0100137 if (is_compat_task())
138 argp = compat_ptr(arg);
139 else
140 argp = (int __user *)arg;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200141 if (mutex_lock_interruptible(&session->mutex))
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700142 return -ERESTARTSYS;
143 switch (cmd) {
144 case VMCP_GETCODE:
145 temp = session->resp_code;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200146 mutex_unlock(&session->mutex);
Heiko Carstens8f3eabe2010-01-13 20:44:31 +0100147 return put_user(temp, argp);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700148 case VMCP_SETBUF:
149 free_pages((unsigned long)session->response,
150 get_order(session->bufsize));
151 session->response=NULL;
Heiko Carstens8f3eabe2010-01-13 20:44:31 +0100152 temp = get_user(session->bufsize, argp);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700153 if (get_order(session->bufsize) > 8) {
154 session->bufsize = PAGE_SIZE;
155 temp = -EINVAL;
156 }
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200157 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700158 return temp;
159 case VMCP_GETSIZE:
160 temp = session->resp_size;
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200161 mutex_unlock(&session->mutex);
Heiko Carstens8f3eabe2010-01-13 20:44:31 +0100162 return put_user(temp, argp);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700163 default:
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200164 mutex_unlock(&session->mutex);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700165 return -ENOIOCTLCMD;
166 }
167}
168
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800169static const struct file_operations vmcp_fops = {
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700170 .owner = THIS_MODULE,
Robert P. J. Dayd197e692007-07-10 11:24:07 +0200171 .open = vmcp_open,
172 .release = vmcp_release,
173 .read = vmcp_read,
174 .write = vmcp_write,
175 .unlocked_ioctl = vmcp_ioctl,
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200176 .compat_ioctl = vmcp_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200177 .llseek = no_llseek,
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700178};
179
180static struct miscdevice vmcp_dev = {
181 .name = "vmcp",
182 .minor = MISC_DYNAMIC_MINOR,
183 .fops = &vmcp_fops,
184};
185
186static int __init vmcp_init(void)
187{
188 int ret;
189
Heiko Carstensf73a2b02010-05-17 10:00:06 +0200190 if (!MACHINE_IS_VM)
191 return 0;
Christian Borntraegera44008f2008-07-14 09:59:43 +0200192
Michael Holzheu66a464d2005-06-25 14:55:33 -0700193 vmcp_debug = debug_register("vmcp", 1, 1, 240);
Christian Borntraegera44008f2008-07-14 09:59:43 +0200194 if (!vmcp_debug)
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200195 return -ENOMEM;
Christian Borntraegera44008f2008-07-14 09:59:43 +0200196
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200197 ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
198 if (ret) {
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200199 debug_unregister(vmcp_debug);
200 return ret;
201 }
Christian Borntraegera44008f2008-07-14 09:59:43 +0200202
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200203 ret = misc_register(&vmcp_dev);
Heiko Carstensf73a2b02010-05-17 10:00:06 +0200204 if (ret)
Christian Borntraegerd9d119f2007-07-17 13:36:05 +0200205 debug_unregister(vmcp_debug);
Heiko Carstensf73a2b02010-05-17 10:00:06 +0200206 return ret;
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700207}
Heiko Carstensf73a2b02010-05-17 10:00:06 +0200208device_initcall(vmcp_init);