summaryrefslogtreecommitdiff
path: root/usr/src/uts/intel/io/amdzen/usmn.c
blob: c9e1608b19ab5658a27c8e88396f29414e689f71 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2022 Oxide Computer Company
 */

/*
 * A device driver that provides user access to the AMD System Management
 * Network for debugging purposes.
 */

#include <sys/types.h>
#include <sys/file.h>
#include <sys/errno.h>
#include <sys/open.h>
#include <sys/cred.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/stat.h>
#include <sys/conf.h>
#include <sys/devops.h>
#include <sys/cmn_err.h>
#include <sys/policy.h>
#include <amdzen_client.h>

#include "usmn.h"

typedef struct usmn {
	dev_info_t *usmn_dip;
	uint_t usmn_ndfs;
} usmn_t;

static usmn_t usmn_data;

static int
usmn_open(dev_t *devp, int flags, int otype, cred_t *credp)
{
	minor_t m;
	usmn_t *usmn = &usmn_data;

	if (crgetzoneid(credp) != GLOBAL_ZONEID ||
	    secpolicy_hwmanip(credp) != 0) {
		return (EPERM);
	}

	if ((flags & (FEXCL | FNDELAY | FNONBLOCK)) != 0) {
		return (EINVAL);
	}

	if (otype != OTYP_CHR) {
		return (EINVAL);
	}

	m = getminor(*devp);
	if (m >= usmn->usmn_ndfs) {
		return (ENXIO);
	}

	return (0);
}

static int
usmn_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
    int *rvalp)
{
	uint_t dfno;
	usmn_t *usmn = &usmn_data;
	usmn_reg_t usr;

	if (cmd != USMN_READ && cmd != USMN_WRITE) {
		return (ENOTTY);
	}

	dfno = getminor(dev);
	if (dfno >= usmn->usmn_ndfs) {
		return (ENXIO);
	}

	if (crgetzoneid(credp) != GLOBAL_ZONEID ||
	    secpolicy_hwmanip(credp) != 0) {
		return (EPERM);
	}

	if (ddi_copyin((void *)arg, &usr, sizeof (usr), mode & FKIOCTL) != 0) {
		return (EFAULT);
	}

	/*
	 * We don't need to check size and alignment here; the client access
	 * routines do so for us and return EINVAL if violated.  The same goes
	 * for the value to be written in the USMN_WRITE case below.
	 */
	const smn_reg_t reg = SMN_MAKE_REG_SIZED(usr.usr_addr, usr.usr_size);

	if (cmd == USMN_READ) {
		int ret;

		if ((mode & FREAD) == 0) {
			return (EINVAL);
		}

		ret = amdzen_c_smn_read(dfno, reg, &usr.usr_data);
		if (ret != 0) {
			return (ret);
		}
	} else if (cmd == USMN_WRITE) {
		int ret;

		if ((mode & FWRITE) == 0) {
			return (EINVAL);
		}

		ret = amdzen_c_smn_write(dfno, reg, usr.usr_data);
		if (ret != 0) {
			return (ret);
		}
	} else {
		return (ENOTSUP);
	}

	if (cmd == USMN_READ &&
	    ddi_copyout(&usr, (void *)arg, sizeof (usr), mode & FKIOCTL) != 0) {
		return (EFAULT);
	}

	return (0);
}

static int
usmn_close(dev_t dev, int flag, int otyp, cred_t *credp)
{
	return (0);
}

static void
usmn_cleanup(usmn_t *usmn)
{
	ddi_remove_minor_node(usmn->usmn_dip, NULL);
	usmn->usmn_ndfs = 0;
	usmn->usmn_dip = NULL;
}

static int
usmn_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
	usmn_t *usmn = &usmn_data;

	if (cmd == DDI_RESUME) {
		return (DDI_SUCCESS);
	} else if (cmd != DDI_ATTACH) {
		return (DDI_FAILURE);
	}

	if (usmn->usmn_dip != NULL) {
		dev_err(dip, CE_WARN, "!usmn is already attached to a "
		    "dev_info_t: %p", usmn->usmn_dip);
		return (DDI_FAILURE);
	}

	usmn->usmn_dip = dip;
	usmn->usmn_ndfs = amdzen_c_df_count();
	for (uint_t i = 0; i < usmn->usmn_ndfs; i++) {
		char buf[32];

		(void) snprintf(buf, sizeof (buf), "usmn.%u", i);
		if (ddi_create_minor_node(dip, buf, S_IFCHR, i, DDI_PSEUDO,
		    0) != DDI_SUCCESS) {
			dev_err(dip, CE_WARN, "!failed to create minor %s",
			    buf);
			goto err;
		}
	}

	return (DDI_SUCCESS);

err:
	usmn_cleanup(usmn);
	return (DDI_FAILURE);
}

static int
usmn_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
{
	usmn_t *usmn = &usmn_data;
	minor_t m;

	switch (cmd) {
	case DDI_INFO_DEVT2DEVINFO:
		m = getminor((dev_t)arg);
		if (m >= usmn->usmn_ndfs) {
			return (DDI_FAILURE);
		}
		*resultp = (void *)usmn->usmn_dip;
		break;
	case DDI_INFO_DEVT2INSTANCE:
		m = getminor((dev_t)arg);
		if (m >= usmn->usmn_ndfs) {
			return (DDI_FAILURE);
		}
		*resultp = (void *)(uintptr_t)ddi_get_instance(usmn->usmn_dip);
		break;
	default:
		return (DDI_FAILURE);
	}
	return (DDI_SUCCESS);
}

static int
usmn_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
	usmn_t *usmn = &usmn_data;

	if (cmd == DDI_SUSPEND) {
		return (DDI_SUCCESS);
	} else if (cmd != DDI_DETACH) {
		return (DDI_FAILURE);
	}

	if (usmn->usmn_dip != dip) {
		dev_err(dip, CE_WARN, "!asked to detach usmn, but dip doesn't "
		    "match");
		return (DDI_FAILURE);
	}

	usmn_cleanup(usmn);
	return (DDI_SUCCESS);
}

static struct cb_ops usmn_cb_ops = {
	.cb_open = usmn_open,
	.cb_close = usmn_close,
	.cb_strategy = nodev,
	.cb_print = nodev,
	.cb_dump = nodev,
	.cb_read = nodev,
	.cb_write = nodev,
	.cb_ioctl = usmn_ioctl,
	.cb_devmap = nodev,
	.cb_mmap = nodev,
	.cb_segmap = nodev,
	.cb_chpoll = nochpoll,
	.cb_prop_op = ddi_prop_op,
	.cb_flag = D_MP,
	.cb_rev = CB_REV,
	.cb_aread = nodev,
	.cb_awrite = nodev
};

static struct dev_ops usmn_dev_ops = {
	.devo_rev = DEVO_REV,
	.devo_refcnt = 0,
	.devo_getinfo = usmn_getinfo,
	.devo_identify = nulldev,
	.devo_probe = nulldev,
	.devo_attach = usmn_attach,
	.devo_detach = usmn_detach,
	.devo_reset = nodev,
	.devo_quiesce = ddi_quiesce_not_needed,
	.devo_cb_ops = &usmn_cb_ops
};

static struct modldrv usmn_modldrv = {
	.drv_modops = &mod_driverops,
	.drv_linkinfo = "AMD User SMN Access",
	.drv_dev_ops = &usmn_dev_ops
};

static struct modlinkage usmn_modlinkage = {
	.ml_rev = MODREV_1,
	.ml_linkage = { &usmn_modldrv, NULL }
};

int
_init(void)
{
	return (mod_install(&usmn_modlinkage));
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&usmn_modlinkage, modinfop));
}

int
_fini(void)
{
	return (mod_remove(&usmn_modlinkage));
}