summaryrefslogtreecommitdiff
path: root/usr/src/cmd/nvmeadm/nvmeadm_dev.c
blob: ce3bc96e22c2b2617395eaf47c1809cbadabe35f (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
/*
 * 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 Tintri by DDN, Inc. All rights reserved.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stropts.h>
#include <err.h>
#include <libdevinfo.h>
#include <sys/nvme.h>
#include <assert.h>

#include "nvmeadm.h"


static boolean_t
nvme_ioctl(int fd, int ioc, size_t *bufsize, void **buf, uint64_t arg,
    uint64_t *res)
{
	nvme_ioctl_t nioc = { 0 };
	void *ptr = NULL;
	int ret;

	if (bufsize != NULL && *bufsize != 0) {
		assert(buf != NULL);

		if (*buf != NULL) {
			nioc.n_buf = (uintptr_t)*buf;
		} else {
			ptr = calloc(1, *bufsize);
			if (ptr == NULL)
				err(-1, "nvme_ioctl()");

			nioc.n_buf = (uintptr_t)ptr;
		}

		nioc.n_len = *bufsize;
	}

	nioc.n_arg = arg;

	ret = ioctl(fd, ioc, &nioc);

	if (res != NULL)
		*res = nioc.n_arg;

	if (ret != 0) {
		/*
		 * We're not clearing *res here as there may be cases where
		 * we get an error _and_ we have interesting information in
		 * returned in *res that callers of this functions might be
		 * interested in.
		 */

		if (debug)
			warn("nvme_ioctl()");
		if (ptr != NULL)
			free(ptr);

		return (B_FALSE);
	}

	if (bufsize != NULL)
		*bufsize = nioc.n_len;

	if (buf != NULL)
		*buf = (void *)nioc.n_buf;

	return (B_TRUE);
}

nvme_capabilities_t *
nvme_capabilities(int fd)
{
	void *cap = NULL;
	size_t bufsize = sizeof (nvme_capabilities_t);

	(void) nvme_ioctl(fd, NVME_IOC_CAPABILITIES, &bufsize, &cap, 0, NULL);

	return (cap);
}

nvme_version_t *
nvme_version(int fd)
{
	void *vs = NULL;
	size_t bufsize = sizeof (nvme_version_t);

	(void) nvme_ioctl(fd, NVME_IOC_VERSION, &bufsize, &vs, 0, NULL);

	return (vs);
}

void *
nvme_identify(int fd, uint8_t cns)
{
	void *idctl = NULL;
	size_t bufsize = NVME_IDENTIFY_BUFSIZE;

	(void) nvme_ioctl(fd, NVME_IOC_IDENTIFY, &bufsize, &idctl, cns, NULL);

	return (idctl);
}

void *
nvme_get_logpage(int fd, uint8_t logpage, size_t *bufsize)
{
	void *buf = NULL;

	(void) nvme_ioctl(fd, NVME_IOC_GET_LOGPAGE, bufsize, &buf, logpage,
	    NULL);

	return (buf);
}

boolean_t
nvme_get_feature(int fd, uint8_t feature, uint32_t arg, uint64_t *res,
    size_t *bufsize, void **buf)
{
	return (nvme_ioctl(fd, NVME_IOC_GET_FEATURES, bufsize, buf,
	    (uint64_t)feature << 32 | arg, res));
}

int
nvme_intr_cnt(int fd)
{
	uint64_t res = 0;

	if (!nvme_ioctl(fd, NVME_IOC_INTR_CNT, NULL, NULL, 0, &res))
		return (-1);

	return ((int)res);
}

boolean_t
nvme_format_nvm(int fd, uint8_t lbaf, uint8_t ses)
{
	nvme_format_nvm_t frmt = { 0 };

	frmt.b.fm_lbaf = lbaf & 0xf;
	frmt.b.fm_ses = ses & 0x7;

	return (nvme_ioctl(fd, NVME_IOC_FORMAT, NULL, NULL, frmt.r, NULL));
}

boolean_t
nvme_detach(int fd)
{
	return (nvme_ioctl(fd, NVME_IOC_DETACH, NULL, NULL, 0, NULL));
}

boolean_t
nvme_attach(int fd)
{
	return (nvme_ioctl(fd, NVME_IOC_ATTACH, NULL, NULL, 0, NULL));
}

boolean_t
nvme_firmware_load(int fd, void *buf, size_t len, offset_t offset, uint16_t *sc)
{
	boolean_t rv;
	uint64_t res;

	rv = nvme_ioctl(fd, NVME_IOC_FIRMWARE_DOWNLOAD, &len, &buf, offset,
	    &res);

	/*
	 * If the hardware returned a command-specific status code, we'll get
	 * it as a negative value from the driver.
	 */
	if ((int64_t)res < 0)
		*sc = (uint16_t)-(int64_t)res;
	else
		*sc = 0;

	return (rv);
}

boolean_t
nvme_firmware_commit(int fd, int slot, int action, uint16_t *sc)
{
	boolean_t rv;
	uint64_t res;

	rv = nvme_ioctl(fd, NVME_IOC_FIRMWARE_COMMIT, NULL, NULL,
	    ((uint64_t)action << 32) | slot, &res);

	/*
	 * If the hardware returned a command-specific status code, we'll get
	 * it as a negative value from the driver.
	 */
	if ((int64_t)res < 0)
		*sc = (uint16_t)-(int64_t)res;
	else
		*sc = 0;

	return (rv);
}

uint32_t
nvme_namespace_state(int fd)
{
	uint64_t res = 0;

	/*
	 * Ask the driver for the namespace state.
	 */
	if (nvme_ioctl(fd, NVME_IOC_NS_STATE, NULL, NULL, 0, &res)) {
		return (res);
	}

	/*
	 * We're only here if the ioctl failed, which it really shouldnt. If so,
	 * we treat this the same as if the namespace was ignored.
	 */
	return (NVME_NS_STATE_IGNORED);
}

int
nvme_open(di_minor_t minor, boolean_t excl)
{
	char *devpath, *path;
	int fd;

	if ((devpath = di_devfs_minor_path(minor)) == NULL)
		err(-1, "nvme_open()");

	if (asprintf(&path, "/devices%s", devpath) < 0) {
		di_devfs_path_free(devpath);
		err(-1, "nvme_open()");
	}

	di_devfs_path_free(devpath);

	fd = open(path, O_RDWR | (excl ? O_EXCL: 0));

	if (fd < 0) {
		if (debug)
			warn("nvme_open(%s)", path);
		free(path);
		return (-1);
	}
	free(path);

	return (fd);
}

void
nvme_close(int fd)
{
	(void) close(fd);
}