xref: /illumos-gate/usr/src/common/nvme/nvme_common.h (revision cdd3e9a818787b4def17c9f707f435885ce0ed31)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2024 Oxide Computer Company
14  */
15 
16 #ifndef _NVME_COMMON_H
17 #define	_NVME_COMMON_H
18 
19 /*
20  * Collection of common files and utilities that can be used for NVMe related
21  * functionality. Broadly, these are meant so that the kernel and userland have
22  * consistent validation routines.
23  *
24  * When we perform error checking and validation we use the kernel's set of
25  * ioctl errors for more semantic errors. These semantic errors are translated
26  * into ones that the library wishes to expose. Our goal is to try to use a
27  * mostly uniform error checking framework between the two entities.
28  *
29  * A consumer must build nvme_version.o and nvme_field.o. Other pieces can be
30  * added based on their needs.
31  */
32 
33 #include <sys/stdbool.h>
34 #include <sys/nvme.h>
35 #include <sys/nvme/discovery.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /*
42  * Version related pieces from nvme_version.c. The main idea is that consumers
43  * such as the kernel and libnvme will wrap up the nvme_vers_atleast() function
44  * with an object that contains an NVMe version, thus reducing the likelihood
45  * that we'll confuse versions.
46  */
47 extern const nvme_version_t nvme_vers_1v0;
48 extern const nvme_version_t nvme_vers_1v1;
49 extern const nvme_version_t nvme_vers_1v2;
50 extern const nvme_version_t nvme_vers_1v3;
51 extern const nvme_version_t nvme_vers_1v4;
52 extern const nvme_version_t nvme_vers_2v0;
53 
54 extern bool nvme_vers_atleast(const nvme_version_t *, const nvme_version_t *);
55 
56 /*
57  * This structure contains information about the controller that must be
58  * supplied to the various validation functions.
59  */
60 typedef struct nvme_valid_ctrl_data {
61 	const nvme_version_t *vcd_vers;
62 	const nvme_identify_ctrl_t *vcd_id;
63 } nvme_valid_ctrl_data_t;
64 
65 /*
66  * This structure is used to represent a field that is in use in a given
67  * command. This allows us to use common validation logic for different classes
68  * of commands such as IDENTIFY, GET LOG PAGE, etc. If everything is fine about
69  * a field, then it should return true. Otherwise, it should return false and
70  * fill out the error message. It is optional to override the specifics of the
71  * nvme_ioctl_err_t with a more specific error where appropriate and known. If
72  * it is not filled in, the validation default will be used.
73  */
74 struct nvme_field_info;
75 typedef bool (*nvme_field_sup_f)(const struct nvme_field_info *,
76     const nvme_valid_ctrl_data_t *, char *, size_t);
77 typedef bool (*nvme_field_valid_f)(const struct nvme_field_info *,
78     const nvme_valid_ctrl_data_t *, uint64_t, char *, size_t);
79 
80 typedef struct nvme_field_info {
81 	const nvme_version_t *nlfi_vers;
82 	nvme_field_sup_f nlfi_sup;
83 	uint64_t nlfi_max_size;
84 	nvme_field_valid_f nlfi_valid;
85 	/*
86 	 * Fields below this point are mostly meant to be used by libnvme and by
87 	 * our printing logic, which we assume is not executed in the kernel.
88 	 */
89 	const char *nlfi_spec;
90 	const char *nlfi_human;
91 	bool nlfi_def_req;
92 	bool nlfi_def_allow;
93 } nvme_field_info_t;
94 
95 typedef enum {
96 	NVME_FIELD_ERR_OK = 0,
97 	NVME_FIELD_ERR_UNSUP_VERSION,
98 	NVME_FIELD_ERR_UNSUP_FIELD,
99 	NVME_FIELD_ERR_BAD_VALUE
100 } nvme_field_error_t;
101 
102 extern nvme_field_error_t nvme_field_validate(const nvme_field_info_t *,
103     const nvme_valid_ctrl_data_t *, uint64_t, char *, size_t);
104 
105 /*
106  * Various common utility routines for field validation and implementation. This
107  * version of NSID checking treats the NSID as valid. Currently checking for the
108  * validity of the broadcast namespace ID is left to consumers.
109  */
110 extern bool nvme_field_atleast(const nvme_valid_ctrl_data_t *,
111     const nvme_version_t *);
112 extern bool nvme_field_valid_nsid(const nvme_field_info_t *,
113     const nvme_valid_ctrl_data_t *, uint64_t, char *, size_t);
114 extern bool nvme_field_range_check(const nvme_field_info_t *, uint64_t,
115     uint64_t, char *, size_t, uint64_t);
116 
117 /*
118  * Log page request information. The goal with these structures and fields is to
119  * be able to validate whether something is valid, both in user/kernel context.
120  * This phrasing also makes this much easier to unit test. Because information
121  * is shared between libnvme and the kernel, some things are not needed for the
122  * kernel. We do not ifdef it out for the moment, to simplify things.
123  */
124 
125 /*
126  * This is the set of fields that the driver knows about how to validate that
127  * can end up in an NVMe log request. Items should be added here once the kernel
128  * knows how to put them in a log request command.
129  */
130 typedef enum {
131 	NVME_LOG_REQ_FIELD_LID	= 0,
132 	NVME_LOG_REQ_FIELD_LSP,
133 	NVME_LOG_REQ_FIELD_LSI,
134 	NVME_LOG_REQ_FIELD_SIZE,
135 	NVME_LOG_REQ_FIELD_CSI,
136 	NVME_LOG_REQ_FIELD_RAE,
137 	NVME_LOG_REQ_FIELD_OFFSET,
138 	NVME_LOG_REQ_FIELD_NSID
139 } nvme_log_req_field_t;
140 
141 extern const nvme_field_info_t nvme_log_fields[];
142 extern size_t nvme_log_nfields;
143 
144 /*
145  * We now use the field based information to have a common structure to define
146  * information about standard log pages.
147  */
148 typedef struct nvme_log_page_info nvme_log_page_info_t;
149 typedef bool (*nvme_log_page_sup_f)(const nvme_valid_ctrl_data_t *,
150     const nvme_log_page_info_t *);
151 typedef uint64_t (*nvme_log_page_len_f)(const nvme_valid_ctrl_data_t *,
152     const nvme_log_page_info_t *);
153 typedef nvme_log_disc_scope_t (*nvme_log_page_scope_f)(
154     const nvme_valid_ctrl_data_t *, const nvme_log_page_info_t *);
155 typedef bool (*nvme_log_page_var_len_f)(uint64_t *, const void *, size_t);
156 
157 struct nvme_log_page_info {
158 	const char *nlpi_short;
159 	const char *nlpi_human;
160 	uint32_t nlpi_lid;
161 	nvme_csi_t nlpi_csi;
162 	/*
163 	 * These two entries can be used to determine whether a log page is
164 	 * supported based upon its version or with a supplemental function. A
165 	 * NULL item means it doesn't need to be checked. This would be the case
166 	 * for vendor-specific logs.
167 	 */
168 	const nvme_version_t *nlpi_vers;
169 	const nvme_log_page_sup_f nlpi_sup_func;
170 	nvme_log_disc_kind_t nlpi_kind;
171 	nvme_log_disc_source_t nlpi_source;
172 	nvme_log_disc_fields_t nlpi_disc;
173 	/*
174 	 * Log pages are valid in certain contexts. This is generally static
175 	 * information, but if the scope function is implemented, we will use
176 	 * that and ignore the contents of nlpi_scope.
177 	 */
178 	nvme_log_disc_scope_t nlpi_scope;
179 	nvme_log_page_scope_f nlpi_scope_func;
180 	/*
181 	 * The lengths for a log page come in three forms. The first form is
182 	 * ones where we can determine based on information in the controller
183 	 * (or at build time) the length of the log page. Many log pages have a
184 	 * fixed length or they include information in the identify controller
185 	 * data structure as to their length (e.g. the error log page). To
186 	 * communicate the log page's length, we will first check if
187 	 * nlpi_len_func is non-NULL and call that to determine the log page
188 	 * length. Otherwise we will use the value in nlpi_len. If these return
189 	 * a non-zero value, the NVME_LOG_DISC_F_SIZE_FIXED will be set
190 	 * automatically.
191 	 *
192 	 * The second form of log pages are those whose length is variable, but
193 	 * we cannot determine it based on information present in the
194 	 * controller. Rather we must read some amount of data from the log page
195 	 * to figure this out at all. For example, many vendor specific logs
196 	 * have a first uint32_t that indicates the number of valid samples and
197 	 * therefore you must read that to determine the overall length of the
198 	 * log page. This case follows the same path as the first case; however,
199 	 * one must also set the nlpi_var_func function pointer. This results
200 	 * in the NVME_LOG_DISC_F_SIZE_VAR flag being set.
201 	 *
202 	 * The third set of these are ones we just don't know about. In this
203 	 * case, leave nlpi_len set to zero and nlpi_len_func to NULL. If this
204 	 * happens or neither path returns a valid size (i.e. 0) then we will
205 	 * set this to a general size that should be large enough (i.e. the
206 	 * non-extended NVMe log page size) and not set either size flag.
207 	 */
208 	uint64_t nlpi_len;
209 	nvme_log_page_len_f nlpi_len_func;
210 	nvme_log_page_var_len_f nlpi_var_func;
211 };
212 
213 extern const nvme_log_page_info_t nvme_std_log_pages[];
214 extern size_t nvme_std_log_npages;
215 
216 /*
217  * These are functions that can be used to compute information about what's
218  * supported and similar information that sometimes requires dynamic support.
219  */
220 extern nvme_log_disc_scope_t nvme_log_page_info_scope(
221     const nvme_log_page_info_t *, const nvme_valid_ctrl_data_t *);
222 extern uint64_t nvme_log_page_info_size(const nvme_log_page_info_t *,
223     const nvme_valid_ctrl_data_t *, bool *);
224 extern bool nvme_log_page_info_supported(const nvme_log_page_info_t *,
225     const nvme_valid_ctrl_data_t *);
226 
227 /*
228  * This next section identifies the various fields that make up the NVMe
229  * IDENTIFY command and the corresponding pieces that are in use throughout.
230  */
231 typedef enum {
232 	NVME_ID_REQ_F_CNS = 0,
233 	NVME_ID_REQ_F_NSID,
234 	NVME_ID_REQ_F_CTRLID,
235 	NVME_ID_REQ_F_BUF,
236 } nvme_identify_req_field_t;
237 
238 typedef enum {
239 	/*
240 	 * Indicates that we allow this identify command to operate on a
241 	 * namespace minor.
242 	 */
243 	NVME_IDENTIFY_INFO_F_NS_OK		= 1 << 0,
244 	/*
245 	 * Indicates that if we support namespace management we should attempt
246 	 * to use the broadcast nsid when asking about the controller.
247 	 */
248 	NVME_IDENTIFY_INFO_F_BCAST		= 1 << 1,
249 	/*
250 	 * This indicates that we are performing an operation which lists
251 	 * namespace IDs. As such, we don't need to validate the namespace
252 	 * against the controller's list. In addition, a zero namespace ID is
253 	 * allowed.
254 	 */
255 	NVME_IDENTIFY_INFO_F_NSID_LIST		= 1 << 2
256 } nvme_identify_info_flags_t;
257 
258 typedef struct nvme_identify_info nvme_identify_info_t;
259 typedef bool (*nvme_identify_sup_f)(const nvme_valid_ctrl_data_t *);
260 struct nvme_identify_info {
261 	const char			*nii_name;
262 	nvme_csi_t			nii_csi;
263 	uint32_t			nii_cns;
264 	const nvme_version_t		*nii_vers;
265 	nvme_identify_sup_f		nii_sup_func;
266 	nvme_identify_req_field_t	nii_fields;
267 	nvme_identify_info_flags_t	nii_flags;
268 };
269 
270 extern const nvme_field_info_t nvme_identify_fields[];
271 extern size_t nvme_identify_nfields;
272 extern const nvme_identify_info_t nvme_identify_cmds[];
273 extern size_t nvme_identify_ncmds;
274 
275 extern bool nvme_identify_info_supported(const nvme_identify_info_t *,
276     const nvme_valid_ctrl_data_t *);
277 
278 /*
279  * NVMe Vendor Unique Commands. Note, unlike others this hasn't really changed
280  * since it was introduced in NVMe 1.0. While libnvme wraps these up a bit to
281  * construct commands, there is no common vendor unique command discovery
282  * information as the kernel more or less stays out of it.
283  */
284 typedef enum {
285 	NVME_VUC_REQ_FIELD_OPC = 0,
286 	NVME_VUC_REQ_FIELD_NSID,
287 	NVME_VUC_REQ_FIELD_CDW12,
288 	NVME_VUC_REQ_FIELD_CDW13,
289 	NVME_VUC_REQ_FIELD_CDW14,
290 	NVME_VUC_REQ_FIELD_CDW15,
291 	NVME_VUC_REQ_FIELD_NDT,
292 	/*
293 	 * While the timeout field here is not actually part of the standard, we
294 	 * require it as part of the command execution and therefore include it
295 	 * in here.
296 	 */
297 	NVME_VUC_REQ_FIELD_TO
298 } nvme_vuc_req_field_t;
299 
300 extern const nvme_field_info_t nvme_vuc_fields[];
301 extern size_t nvme_vuc_nfields;
302 
303 /*
304  * Firmware download and commit related fields and routines.
305  */
306 typedef enum {
307 	NVME_FW_LOAD_REQ_FIELD_NUMD = 0,
308 	NVME_FW_LOAD_REQ_FIELD_OFFSET
309 } nvme_fw_load_req_field_t;
310 
311 extern const nvme_field_info_t nvme_fw_load_fields[];
312 extern size_t nvme_fw_load_nfields;
313 
314 extern bool nvme_fw_cmds_supported(const nvme_valid_ctrl_data_t *);
315 extern uint32_t nvme_fw_load_granularity(const nvme_valid_ctrl_data_t *);
316 
317 typedef enum {
318 	NVME_FW_COMMIT_REQ_FIELD_SLOT = 0,
319 	NVME_FW_COMMIT_REQ_FIELD_ACT
320 } nvme_fw_commit_req_field_t;
321 
322 extern const nvme_field_info_t nvme_fw_commit_fields[];
323 extern size_t nvme_fw_commit_nfields;
324 
325 /*
326  * Format NVM operations
327  */
328 typedef enum {
329 	NVME_FORMAT_REQ_FIELD_LBAF	= 0,
330 	NVME_FORMAT_REQ_FIELD_SES,
331 	NVME_FORMAT_REQ_FIELD_NSID
332 } nvme_format_req_field_t;
333 
334 extern const nvme_field_info_t nvme_format_fields[];
335 extern size_t nvme_format_nfields;
336 
337 extern bool nvme_format_cmds_supported(const nvme_valid_ctrl_data_t *);
338 
339 /*
340  * Feature related requests
341  */
342 typedef enum {
343 	NVME_GET_FEAT_REQ_FIELD_FID		= 0,
344 	NVME_GET_FEAT_REQ_FIELD_SEL,
345 	NVME_GET_FEAT_REQ_FIELD_DPTR,
346 	NVME_GET_FEAT_REQ_FIELD_CDW11,
347 	NVME_GET_FEAT_REQ_FIELD_NSID
348 } nvme_get_feat_req_field_t;
349 
350 extern const nvme_field_info_t nvme_get_feat_fields[];
351 extern size_t nvme_get_feat_nfields;
352 
353 /*
354  * Common feature information.
355  */
356 typedef struct nvme_feat_info nvme_feat_info_t;
357 typedef bool (*nvme_feat_sup_f)(const nvme_valid_ctrl_data_t *,
358     const nvme_feat_info_t *);
359 
360 struct nvme_feat_info {
361 	const char *nfeat_short;
362 	const char *nfeat_spec;
363 	uint32_t nfeat_fid;
364 	/*
365 	 * These three entries can be used to determine whether a feature is
366 	 * supported or not based upon its version or supplemental information.
367 	 */
368 	const nvme_version_t *nfeat_vers;
369 	const nvme_feat_sup_f nfeat_sup_func;
370 	nvme_feat_kind_t nfeat_kind;
371 	/*
372 	 * These describe whether the feature operates on namespaces or the
373 	 * controller and misc. flags and information about them.
374 	 */
375 	nvme_feat_scope_t nfeat_scope;
376 	nvme_feat_csi_t nfeat_csi;
377 	nvme_feat_flags_t nfeat_flags;
378 	/*
379 	 * These four entries describe what an NVMe device uses as input and
380 	 * output fields.
381 	 */
382 	nvme_get_feat_fields_t nfeat_in_get;
383 	nvme_set_feat_fields_t nfeat_in_set;
384 	nvme_feat_output_t nfeat_out_get;
385 	nvme_feat_output_t nfeat_out_set;
386 	/*
387 	 * Feature data size. This should be zero if the feature does not use a
388 	 * data payload. Right now we assume the get and set sizes are identical
389 	 * as that's how this normally works.
390 	 */
391 	uint64_t nfeat_len;
392 };
393 
394 extern const nvme_feat_info_t nvme_std_feats[];
395 extern size_t nvme_std_nfeats;
396 
397 extern nvme_feat_impl_t nvme_feat_supported(const nvme_feat_info_t *,
398     const nvme_valid_ctrl_data_t *);
399 
400 #ifdef __cplusplus
401 }
402 #endif
403 
404 #endif /* _NVME_COMMON_H */
405