summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/powerpc/papr_sysparm/papr_sysparm.c
blob: f56c15a11e2f46e9046f30f94cb47f208b8be10a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// SPDX-License-Identifier: GPL-2.0-only
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <asm/papr-sysparm.h>

#include "utils.h"

#define DEVPATH "/dev/papr-sysparm"

static int open_close(void)
{
	const int devfd = open(DEVPATH, O_RDONLY);

	SKIP_IF_MSG(devfd < 0 && errno == ENOENT,
		    DEVPATH " not present");

	FAIL_IF(devfd < 0);
	FAIL_IF(close(devfd) != 0);

	return 0;
}

static int get_splpar(void)
{
	struct papr_sysparm_io_block sp = {
		.parameter = 20, // SPLPAR characteristics
	};
	const int devfd = open(DEVPATH, O_RDONLY);

	SKIP_IF_MSG(devfd < 0 && errno == ENOENT,
		    DEVPATH " not present");

	FAIL_IF(devfd < 0);
	FAIL_IF(ioctl(devfd, PAPR_SYSPARM_IOC_GET, &sp) != 0);
	FAIL_IF(sp.length == 0);
	FAIL_IF(sp.length > sizeof(sp.data));
	FAIL_IF(close(devfd) != 0);

	return 0;
}

static int get_bad_parameter(void)
{
	struct papr_sysparm_io_block sp = {
		.parameter = UINT32_MAX, // there are only ~60 specified parameters
	};
	const int devfd = open(DEVPATH, O_RDONLY);

	SKIP_IF_MSG(devfd < 0 && errno == ENOENT,
		    DEVPATH " not present");

	FAIL_IF(devfd < 0);

	// Ensure expected error
	FAIL_IF(ioctl(devfd, PAPR_SYSPARM_IOC_GET, &sp) != -1);
	FAIL_IF(errno != EOPNOTSUPP);

	// Ensure the buffer is unchanged
	FAIL_IF(sp.length != 0);
	for (size_t i = 0; i < ARRAY_SIZE(sp.data); ++i)
		FAIL_IF(sp.data[i] != 0);

	FAIL_IF(close(devfd) != 0);

	return 0;
}

static int check_efault_common(unsigned long cmd)
{
	const int devfd = open(DEVPATH, O_RDWR);

	SKIP_IF_MSG(devfd < 0 && errno == ENOENT,
		    DEVPATH " not present");

	FAIL_IF(devfd < 0);

	// Ensure expected error
	FAIL_IF(ioctl(devfd, cmd, NULL) != -1);
	FAIL_IF(errno != EFAULT);

	FAIL_IF(close(devfd) != 0);

	return 0;
}

static int check_efault_get(void)
{
	return check_efault_common(PAPR_SYSPARM_IOC_GET);
}

static int check_efault_set(void)
{
	return check_efault_common(PAPR_SYSPARM_IOC_SET);
}

static int set_hmc0(void)
{
	struct papr_sysparm_io_block sp = {
		.parameter = 0, // HMC0, not a settable parameter
	};
	const int devfd = open(DEVPATH, O_RDWR);

	SKIP_IF_MSG(devfd < 0 && errno == ENOENT,
		    DEVPATH " not present");

	FAIL_IF(devfd < 0);

	// Ensure expected error
	FAIL_IF(ioctl(devfd, PAPR_SYSPARM_IOC_SET, &sp) != -1);
	SKIP_IF_MSG(errno == EOPNOTSUPP, "operation not supported");
	FAIL_IF(errno != EPERM);

	FAIL_IF(close(devfd) != 0);

	return 0;
}

static int set_with_ro_fd(void)
{
	struct papr_sysparm_io_block sp = {
		.parameter = 0, // HMC0, not a settable parameter.
	};
	const int devfd = open(DEVPATH, O_RDONLY);

	SKIP_IF_MSG(devfd < 0 && errno == ENOENT,
		    DEVPATH " not present");

	FAIL_IF(devfd < 0);

	// Ensure expected error
	FAIL_IF(ioctl(devfd, PAPR_SYSPARM_IOC_SET, &sp) != -1);
	SKIP_IF_MSG(errno == EOPNOTSUPP, "operation not supported");

	// HMC0 isn't a settable parameter and we would normally
	// expect to get EPERM on attempts to modify it. However, when
	// the file is open read-only, we expect the driver to prevent
	// the attempt with a distinct error.
	FAIL_IF(errno != EBADF);

	FAIL_IF(close(devfd) != 0);

	return 0;
}

struct sysparm_test {
	int (*function)(void);
	const char *description;
};

static const struct sysparm_test sysparm_tests[] = {
	{
		.function = open_close,
		.description = "open and close " DEVPATH " without issuing commands",
	},
	{
		.function = get_splpar,
		.description = "retrieve SPLPAR characteristics",
	},
	{
		.function = get_bad_parameter,
		.description = "verify EOPNOTSUPP for known-bad parameter",
	},
	{
		.function = check_efault_get,
		.description = "PAPR_SYSPARM_IOC_GET returns EFAULT on bad address",
	},
	{
		.function = check_efault_set,
		.description = "PAPR_SYSPARM_IOC_SET returns EFAULT on bad address",
	},
	{
		.function = set_hmc0,
		.description = "ensure EPERM on attempt to update HMC0",
	},
	{
		.function = set_with_ro_fd,
		.description = "PAPR_IOC_SYSPARM_SET returns EACCES on read-only fd",
	},
};

int main(void)
{
	size_t fails = 0;

	for (size_t i = 0; i < ARRAY_SIZE(sysparm_tests); ++i) {
		const struct sysparm_test *t = &sysparm_tests[i];

		if (test_harness(t->function, t->description))
			++fails;
	}

	return fails == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}