blob: ae0ed8dd5f1b362eb3c93ee58ed10d947a587c83 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Martin Schwidefsky07be0382014-01-24 09:18:52 +01002/*
3 * Hypervisor filesystem for Linux on s390.
4 * Set Partition-Resource Parameter interface.
5 *
6 * Copyright IBM Corp. 2013
7 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
8 */
9
10#include <linux/compat.h>
11#include <linux/errno.h>
12#include <linux/gfp.h>
13#include <linux/string.h>
14#include <linux/types.h>
15#include <linux/uaccess.h>
16#include <asm/compat.h>
Martin Schwidefsky1ec27722015-08-20 17:28:44 +020017#include <asm/diag.h>
Martin Schwidefsky07be0382014-01-24 09:18:52 +010018#include <asm/sclp.h>
19#include "hypfs.h"
20
21#define DIAG304_SET_WEIGHTS 0
22#define DIAG304_QUERY_PRP 1
23#define DIAG304_SET_CAPPING 2
24
25#define DIAG304_CMD_MAX 2
26
Martin Schwidefsky1ec27722015-08-20 17:28:44 +020027static inline unsigned long __hypfs_sprp_diag304(void *data, unsigned long cmd)
Martin Schwidefsky07be0382014-01-24 09:18:52 +010028{
29 register unsigned long _data asm("2") = (unsigned long) data;
30 register unsigned long _rc asm("3");
31 register unsigned long _cmd asm("4") = cmd;
32
33 asm volatile("diag %1,%2,0x304\n"
34 : "=d" (_rc) : "d" (_data), "d" (_cmd) : "memory");
35
36 return _rc;
37}
38
Martin Schwidefsky1ec27722015-08-20 17:28:44 +020039static unsigned long hypfs_sprp_diag304(void *data, unsigned long cmd)
40{
41 diag_stat_inc(DIAG_STAT_X304);
42 return __hypfs_sprp_diag304(data, cmd);
43}
44
Martin Schwidefsky07be0382014-01-24 09:18:52 +010045static void hypfs_sprp_free(const void *data)
46{
47 free_page((unsigned long) data);
48}
49
50static int hypfs_sprp_create(void **data_ptr, void **free_ptr, size_t *size)
51{
52 unsigned long rc;
53 void *data;
54
55 data = (void *) get_zeroed_page(GFP_KERNEL);
56 if (!data)
57 return -ENOMEM;
58 rc = hypfs_sprp_diag304(data, DIAG304_QUERY_PRP);
59 if (rc != 1) {
60 *data_ptr = *free_ptr = NULL;
61 *size = 0;
62 free_page((unsigned long) data);
63 return -EIO;
64 }
65 *data_ptr = *free_ptr = data;
66 *size = PAGE_SIZE;
67 return 0;
68}
69
70static int __hypfs_sprp_ioctl(void __user *user_area)
71{
72 struct hypfs_diag304 diag304;
73 unsigned long cmd;
74 void __user *udata;
75 void *data;
76 int rc;
77
78 if (copy_from_user(&diag304, user_area, sizeof(diag304)))
79 return -EFAULT;
80 if ((diag304.args[0] >> 8) != 0 || diag304.args[1] > DIAG304_CMD_MAX)
81 return -EINVAL;
82
83 data = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
84 if (!data)
85 return -ENOMEM;
86
87 udata = (void __user *)(unsigned long) diag304.data;
88 if (diag304.args[1] == DIAG304_SET_WEIGHTS ||
89 diag304.args[1] == DIAG304_SET_CAPPING)
90 if (copy_from_user(data, udata, PAGE_SIZE)) {
91 rc = -EFAULT;
92 goto out;
93 }
94
95 cmd = *(unsigned long *) &diag304.args[0];
96 diag304.rc = hypfs_sprp_diag304(data, cmd);
97
98 if (diag304.args[1] == DIAG304_QUERY_PRP)
99 if (copy_to_user(udata, data, PAGE_SIZE)) {
100 rc = -EFAULT;
101 goto out;
102 }
103
104 rc = copy_to_user(user_area, &diag304, sizeof(diag304)) ? -EFAULT : 0;
105out:
106 free_page((unsigned long) data);
107 return rc;
108}
109
110static long hypfs_sprp_ioctl(struct file *file, unsigned int cmd,
111 unsigned long arg)
112{
113 void __user *argp;
114
115 if (!capable(CAP_SYS_ADMIN))
116 return -EACCES;
117 if (is_compat_task())
118 argp = compat_ptr(arg);
119 else
120 argp = (void __user *) arg;
121 switch (cmd) {
122 case HYPFS_DIAG304:
123 return __hypfs_sprp_ioctl(argp);
124 default: /* unknown ioctl number */
125 return -ENOTTY;
126 }
127 return 0;
128}
129
130static struct hypfs_dbfs_file hypfs_sprp_file = {
131 .name = "diag_304",
132 .data_create = hypfs_sprp_create,
133 .data_free = hypfs_sprp_free,
134 .unlocked_ioctl = hypfs_sprp_ioctl,
135};
136
137int hypfs_sprp_init(void)
138{
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200139 if (!sclp.has_sprp)
Martin Schwidefsky07be0382014-01-24 09:18:52 +0100140 return 0;
141 return hypfs_dbfs_create_file(&hypfs_sprp_file);
142}
143
144void hypfs_sprp_exit(void)
145{
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200146 if (!sclp.has_sprp)
Martin Schwidefsky07be0382014-01-24 09:18:52 +0100147 return;
148 hypfs_dbfs_remove_file(&hypfs_sprp_file);
149}