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_supported_nsid(const nvme_field_info_t *, 113 const nvme_valid_ctrl_data_t *, char *, size_t); 114 extern bool nvme_field_valid_nsid(const nvme_field_info_t *, 115 const nvme_valid_ctrl_data_t *, uint64_t, char *, size_t); 116 extern bool nvme_field_range_check(const nvme_field_info_t *, uint64_t, 117 uint64_t, char *, size_t, uint64_t); 118 119 /* 120 * Log page request information. The goal with these structures and fields is to 121 * be able to validate whether something is valid, both in user/kernel context. 122 * This phrasing also makes this much easier to unit test. Because information 123 * is shared between libnvme and the kernel, some things are not needed for the 124 * kernel. We do not ifdef it out for the moment, to simplify things. 125 */ 126 127 /* 128 * This is the set of fields that the driver knows about how to validate that 129 * can end up in an NVMe log request. Items should be added here once the kernel 130 * knows how to put them in a log request command. 131 */ 132 typedef enum { 133 NVME_LOG_REQ_FIELD_LID = 0, 134 NVME_LOG_REQ_FIELD_LSP, 135 NVME_LOG_REQ_FIELD_LSI, 136 NVME_LOG_REQ_FIELD_SIZE, 137 NVME_LOG_REQ_FIELD_CSI, 138 NVME_LOG_REQ_FIELD_RAE, 139 NVME_LOG_REQ_FIELD_OFFSET, 140 NVME_LOG_REQ_FIELD_NSID 141 } nvme_log_req_field_t; 142 143 extern const nvme_field_info_t nvme_log_fields[]; 144 extern size_t nvme_log_nfields; 145 146 /* 147 * We now use the field based information to have a common structure to define 148 * information about standard log pages. 149 */ 150 typedef struct nvme_log_page_info nvme_log_page_info_t; 151 typedef bool (*nvme_log_page_sup_f)(const nvme_valid_ctrl_data_t *, 152 const nvme_log_page_info_t *); 153 typedef uint64_t (*nvme_log_page_len_f)(const nvme_valid_ctrl_data_t *, 154 const nvme_log_page_info_t *); 155 typedef nvme_log_disc_scope_t (*nvme_log_page_scope_f)( 156 const nvme_valid_ctrl_data_t *, const nvme_log_page_info_t *); 157 typedef bool (*nvme_log_page_var_len_f)(uint64_t *, const void *, size_t); 158 159 struct nvme_log_page_info { 160 const char *nlpi_short; 161 const char *nlpi_human; 162 uint32_t nlpi_lid; 163 nvme_csi_t nlpi_csi; 164 /* 165 * These two entries can be used to determine whether a log page is 166 * supported based upon its version or with a supplemental function. A 167 * NULL item means it doesn't need to be checked. This would be the case 168 * for vendor-specific logs. 169 */ 170 const nvme_version_t *nlpi_vers; 171 const nvme_log_page_sup_f nlpi_sup_func; 172 nvme_log_disc_kind_t nlpi_kind; 173 nvme_log_disc_source_t nlpi_source; 174 nvme_log_disc_fields_t nlpi_disc; 175 /* 176 * Log pages are valid in certain contexts. This is generally static 177 * information, but if the scope function is implemented, we will use 178 * that and ignore the contents of nlpi_scope. 179 */ 180 nvme_log_disc_scope_t nlpi_scope; 181 nvme_log_page_scope_f nlpi_scope_func; 182 /* 183 * The lengths for a log page come in three forms. The first form is 184 * ones where we can determine based on information in the controller 185 * (or at build time) the length of the log page. Many log pages have a 186 * fixed length or they include information in the identify controller 187 * data structure as to their length (e.g. the error log page). To 188 * communicate the log page's length, we will first check if 189 * nlpi_len_func is non-NULL and call that to determine the log page 190 * length. Otherwise we will use the value in nlpi_len. If these return 191 * a non-zero value, the NVME_LOG_DISC_F_SIZE_FIXED will be set 192 * automatically. 193 * 194 * The second form of log pages are those whose length is variable, but 195 * we cannot determine it based on information present in the 196 * controller. Rather we must read some amount of data from the log page 197 * to figure this out at all. For example, many vendor specific logs 198 * have a first uint32_t that indicates the number of valid samples and 199 * therefore you must read that to determine the overall length of the 200 * log page. This case follows the same path as the first case; however, 201 * one must also set the nlpi_var_func function pointer. This results 202 * in the NVME_LOG_DISC_F_SIZE_VAR flag being set. 203 * 204 * The third set of these are ones we just don't know about. In this 205 * case, leave nlpi_len set to zero and nlpi_len_func to NULL. If this 206 * happens or neither path returns a valid size (i.e. 0) then we will 207 * set this to a general size that should be large enough (i.e. the 208 * non-extended NVMe log page size) and not set either size flag. 209 */ 210 uint64_t nlpi_len; 211 nvme_log_page_len_f nlpi_len_func; 212 nvme_log_page_var_len_f nlpi_var_func; 213 }; 214 215 extern const nvme_log_page_info_t nvme_std_log_pages[]; 216 extern size_t nvme_std_log_npages; 217 218 /* 219 * These are functions that can be used to compute information about what's 220 * supported and similar information that sometimes requires dynamic support. 221 */ 222 extern nvme_log_disc_scope_t nvme_log_page_info_scope( 223 const nvme_log_page_info_t *, const nvme_valid_ctrl_data_t *); 224 extern uint64_t nvme_log_page_info_size(const nvme_log_page_info_t *, 225 const nvme_valid_ctrl_data_t *, bool *); 226 extern bool nvme_log_page_info_supported(const nvme_log_page_info_t *, 227 const nvme_valid_ctrl_data_t *); 228 229 /* 230 * This next section identifies the various fields that make up the NVMe 231 * IDENTIFY command and the corresponding pieces that are in use throughout. 232 */ 233 typedef enum { 234 NVME_ID_REQ_F_CNS = 0, 235 NVME_ID_REQ_F_NSID, 236 NVME_ID_REQ_F_CTRLID, 237 NVME_ID_REQ_F_BUF, 238 } nvme_identify_req_field_t; 239 240 typedef enum { 241 /* 242 * Indicates that we allow this identify command to operate on a 243 * namespace minor. 244 */ 245 NVME_IDENTIFY_INFO_F_NS_OK = 1 << 0, 246 /* 247 * Indicates that if we support namespace management we should attempt 248 * to use the broadcast nsid when asking about the controller. 249 */ 250 NVME_IDENTIFY_INFO_F_BCAST = 1 << 1, 251 /* 252 * This indicates that we are performing an operation which lists 253 * namespace IDs. As such, we don't need to validate the namespace 254 * against the controller's list. In addition, a zero namespace ID is 255 * allowed. 256 */ 257 NVME_IDENTIFY_INFO_F_NSID_LIST = 1 << 2 258 } nvme_identify_info_flags_t; 259 260 typedef struct nvme_identify_info nvme_identify_info_t; 261 typedef bool (*nvme_identify_sup_f)(const nvme_valid_ctrl_data_t *); 262 struct nvme_identify_info { 263 const char *nii_name; 264 nvme_csi_t nii_csi; 265 uint32_t nii_cns; 266 const nvme_version_t *nii_vers; 267 nvme_identify_sup_f nii_sup_func; 268 nvme_identify_req_field_t nii_fields; 269 nvme_identify_info_flags_t nii_flags; 270 }; 271 272 extern const nvme_field_info_t nvme_identify_fields[]; 273 extern size_t nvme_identify_nfields; 274 extern const nvme_identify_info_t nvme_identify_cmds[]; 275 extern size_t nvme_identify_ncmds; 276 277 extern bool nvme_identify_info_supported(const nvme_identify_info_t *, 278 const nvme_valid_ctrl_data_t *); 279 280 /* 281 * NVMe Vendor Unique Commands. Note, unlike others this hasn't really changed 282 * since it was introduced in NVMe 1.0. While libnvme wraps these up a bit to 283 * construct commands, there is no common vendor unique command discovery 284 * information as the kernel more or less stays out of it. 285 */ 286 typedef enum { 287 NVME_VUC_REQ_FIELD_OPC = 0, 288 NVME_VUC_REQ_FIELD_NSID, 289 NVME_VUC_REQ_FIELD_CDW12, 290 NVME_VUC_REQ_FIELD_CDW13, 291 NVME_VUC_REQ_FIELD_CDW14, 292 NVME_VUC_REQ_FIELD_CDW15, 293 NVME_VUC_REQ_FIELD_NDT, 294 /* 295 * While the timeout field here is not actually part of the standard, we 296 * require it as part of the command execution and therefore include it 297 * in here. 298 */ 299 NVME_VUC_REQ_FIELD_TO 300 } nvme_vuc_req_field_t; 301 302 extern const nvme_field_info_t nvme_vuc_fields[]; 303 extern size_t nvme_vuc_nfields; 304 305 /* 306 * Firmware download and commit related fields and routines. 307 */ 308 typedef enum { 309 NVME_FW_LOAD_REQ_FIELD_NUMD = 0, 310 NVME_FW_LOAD_REQ_FIELD_OFFSET 311 } nvme_fw_load_req_field_t; 312 313 extern const nvme_field_info_t nvme_fw_load_fields[]; 314 extern size_t nvme_fw_load_nfields; 315 316 extern bool nvme_fw_cmds_supported(const nvme_valid_ctrl_data_t *); 317 extern uint32_t nvme_fw_load_granularity(const nvme_valid_ctrl_data_t *); 318 319 typedef enum { 320 NVME_FW_COMMIT_REQ_FIELD_SLOT = 0, 321 NVME_FW_COMMIT_REQ_FIELD_ACT 322 } nvme_fw_commit_req_field_t; 323 324 extern const nvme_field_info_t nvme_fw_commit_fields[]; 325 extern size_t nvme_fw_commit_nfields; 326 327 /* 328 * Format NVM operations 329 */ 330 typedef enum { 331 NVME_FORMAT_REQ_FIELD_LBAF = 0, 332 NVME_FORMAT_REQ_FIELD_SES, 333 NVME_FORMAT_REQ_FIELD_NSID 334 } nvme_format_req_field_t; 335 336 extern const nvme_field_info_t nvme_format_fields[]; 337 extern size_t nvme_format_nfields; 338 339 extern bool nvme_format_cmds_supported(const nvme_valid_ctrl_data_t *); 340 341 /* 342 * Feature related requests 343 */ 344 typedef enum { 345 NVME_GET_FEAT_REQ_FIELD_FID = 0, 346 NVME_GET_FEAT_REQ_FIELD_SEL, 347 NVME_GET_FEAT_REQ_FIELD_DPTR, 348 NVME_GET_FEAT_REQ_FIELD_CDW11, 349 NVME_GET_FEAT_REQ_FIELD_NSID 350 } nvme_get_feat_req_field_t; 351 352 extern const nvme_field_info_t nvme_get_feat_fields[]; 353 extern size_t nvme_get_feat_nfields; 354 355 /* 356 * Common feature information. 357 */ 358 typedef struct nvme_feat_info nvme_feat_info_t; 359 typedef bool (*nvme_feat_sup_f)(const nvme_valid_ctrl_data_t *, 360 const nvme_feat_info_t *); 361 362 struct nvme_feat_info { 363 const char *nfeat_short; 364 const char *nfeat_spec; 365 uint32_t nfeat_fid; 366 /* 367 * These three entries can be used to determine whether a feature is 368 * supported or not based upon its version or supplemental information. 369 */ 370 const nvme_version_t *nfeat_vers; 371 const nvme_feat_sup_f nfeat_sup_func; 372 nvme_feat_kind_t nfeat_kind; 373 /* 374 * These describe whether the feature operates on namespaces or the 375 * controller and misc. flags and information about them. 376 */ 377 nvme_feat_scope_t nfeat_scope; 378 nvme_feat_csi_t nfeat_csi; 379 nvme_feat_flags_t nfeat_flags; 380 /* 381 * These four entries describe what an NVMe device uses as input and 382 * output fields. 383 */ 384 nvme_get_feat_fields_t nfeat_in_get; 385 nvme_set_feat_fields_t nfeat_in_set; 386 nvme_feat_output_t nfeat_out_get; 387 nvme_feat_output_t nfeat_out_set; 388 /* 389 * Feature data size. This should be zero if the feature does not use a 390 * data payload. Right now we assume the get and set sizes are identical 391 * as that's how this normally works. 392 */ 393 uint64_t nfeat_len; 394 }; 395 396 extern const nvme_feat_info_t nvme_std_feats[]; 397 extern size_t nvme_std_nfeats; 398 399 extern nvme_feat_impl_t nvme_feat_supported(const nvme_feat_info_t *, 400 const nvme_valid_ctrl_data_t *); 401 402 #ifdef __cplusplus 403 } 404 #endif 405 406 #endif /* _NVME_COMMON_H */ 407