1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * IOCTL interface for SCLP
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright IBM Corp. 2012
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Author: Michael Holzheu <holzheu@linux.vnet.ibm.com>
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/compat.h>
11*4882a593Smuzhiyun #include <linux/uaccess.h>
12*4882a593Smuzhiyun #include <linux/miscdevice.h>
13*4882a593Smuzhiyun #include <linux/gfp.h>
14*4882a593Smuzhiyun #include <linux/init.h>
15*4882a593Smuzhiyun #include <linux/ioctl.h>
16*4882a593Smuzhiyun #include <linux/fs.h>
17*4882a593Smuzhiyun #include <asm/sclp_ctl.h>
18*4882a593Smuzhiyun #include <asm/sclp.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include "sclp.h"
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /*
23*4882a593Smuzhiyun * Supported command words
24*4882a593Smuzhiyun */
25*4882a593Smuzhiyun static unsigned int sclp_ctl_sccb_wlist[] = {
26*4882a593Smuzhiyun 0x00400002,
27*4882a593Smuzhiyun 0x00410002,
28*4882a593Smuzhiyun };
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun * Check if command word is supported
32*4882a593Smuzhiyun */
sclp_ctl_cmdw_supported(unsigned int cmdw)33*4882a593Smuzhiyun static int sclp_ctl_cmdw_supported(unsigned int cmdw)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun int i;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(sclp_ctl_sccb_wlist); i++) {
38*4882a593Smuzhiyun if (cmdw == sclp_ctl_sccb_wlist[i])
39*4882a593Smuzhiyun return 1;
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun return 0;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun
u64_to_uptr(u64 value)44*4882a593Smuzhiyun static void __user *u64_to_uptr(u64 value)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun if (is_compat_task())
47*4882a593Smuzhiyun return compat_ptr(value);
48*4882a593Smuzhiyun else
49*4882a593Smuzhiyun return (void __user *)(unsigned long)value;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /*
53*4882a593Smuzhiyun * Start SCLP request
54*4882a593Smuzhiyun */
sclp_ctl_ioctl_sccb(void __user * user_area)55*4882a593Smuzhiyun static int sclp_ctl_ioctl_sccb(void __user *user_area)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun struct sclp_ctl_sccb ctl_sccb;
58*4882a593Smuzhiyun struct sccb_header *sccb;
59*4882a593Smuzhiyun unsigned long copied;
60*4882a593Smuzhiyun int rc;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun if (copy_from_user(&ctl_sccb, user_area, sizeof(ctl_sccb)))
63*4882a593Smuzhiyun return -EFAULT;
64*4882a593Smuzhiyun if (!sclp_ctl_cmdw_supported(ctl_sccb.cmdw))
65*4882a593Smuzhiyun return -EOPNOTSUPP;
66*4882a593Smuzhiyun sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
67*4882a593Smuzhiyun if (!sccb)
68*4882a593Smuzhiyun return -ENOMEM;
69*4882a593Smuzhiyun copied = PAGE_SIZE -
70*4882a593Smuzhiyun copy_from_user(sccb, u64_to_uptr(ctl_sccb.sccb), PAGE_SIZE);
71*4882a593Smuzhiyun if (offsetof(struct sccb_header, length) +
72*4882a593Smuzhiyun sizeof(sccb->length) > copied || sccb->length > copied) {
73*4882a593Smuzhiyun rc = -EFAULT;
74*4882a593Smuzhiyun goto out_free;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun if (sccb->length < 8) {
77*4882a593Smuzhiyun rc = -EINVAL;
78*4882a593Smuzhiyun goto out_free;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun rc = sclp_sync_request(ctl_sccb.cmdw, sccb);
81*4882a593Smuzhiyun if (rc)
82*4882a593Smuzhiyun goto out_free;
83*4882a593Smuzhiyun if (copy_to_user(u64_to_uptr(ctl_sccb.sccb), sccb, sccb->length))
84*4882a593Smuzhiyun rc = -EFAULT;
85*4882a593Smuzhiyun out_free:
86*4882a593Smuzhiyun free_page((unsigned long) sccb);
87*4882a593Smuzhiyun return rc;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /*
91*4882a593Smuzhiyun * SCLP SCCB ioctl function
92*4882a593Smuzhiyun */
sclp_ctl_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)93*4882a593Smuzhiyun static long sclp_ctl_ioctl(struct file *filp, unsigned int cmd,
94*4882a593Smuzhiyun unsigned long arg)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun void __user *argp;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun if (is_compat_task())
99*4882a593Smuzhiyun argp = compat_ptr(arg);
100*4882a593Smuzhiyun else
101*4882a593Smuzhiyun argp = (void __user *) arg;
102*4882a593Smuzhiyun switch (cmd) {
103*4882a593Smuzhiyun case SCLP_CTL_SCCB:
104*4882a593Smuzhiyun return sclp_ctl_ioctl_sccb(argp);
105*4882a593Smuzhiyun default: /* unknown ioctl number */
106*4882a593Smuzhiyun return -ENOTTY;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /*
111*4882a593Smuzhiyun * File operations
112*4882a593Smuzhiyun */
113*4882a593Smuzhiyun static const struct file_operations sclp_ctl_fops = {
114*4882a593Smuzhiyun .owner = THIS_MODULE,
115*4882a593Smuzhiyun .open = nonseekable_open,
116*4882a593Smuzhiyun .unlocked_ioctl = sclp_ctl_ioctl,
117*4882a593Smuzhiyun .compat_ioctl = sclp_ctl_ioctl,
118*4882a593Smuzhiyun .llseek = no_llseek,
119*4882a593Smuzhiyun };
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /*
122*4882a593Smuzhiyun * Misc device definition
123*4882a593Smuzhiyun */
124*4882a593Smuzhiyun static struct miscdevice sclp_ctl_device = {
125*4882a593Smuzhiyun .minor = MISC_DYNAMIC_MINOR,
126*4882a593Smuzhiyun .name = "sclp",
127*4882a593Smuzhiyun .fops = &sclp_ctl_fops,
128*4882a593Smuzhiyun };
129*4882a593Smuzhiyun builtin_misc_device(sclp_ctl_device);
130