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 2025 Oxide Computer Company 14 * Copyright 2022 Tintri by DDN, Inc. All rights reserved. 15 */ 16 17 #ifndef _NVMEADM_H 18 #define _NVMEADM_H 19 20 #include <stdio.h> 21 #include <libdevinfo.h> 22 #include <libnvme.h> 23 #include <nvme_common.h> 24 #include <nvme_reg.h> 25 #include <ofmt.h> 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 extern int verbose; 32 extern int debug; 33 34 /* Common structures */ 35 typedef struct nvme_process_arg nvme_process_arg_t; 36 typedef struct nvmeadm_feature nvmeadm_feature_t; 37 typedef struct nvmeadm_cmd nvmeadm_cmd_t; 38 39 #define NVMEADM_C_MULTI 1 40 #define NVMEADM_C_EXCL 2 41 42 /* 43 * General command structure 44 */ 45 struct nvmeadm_cmd { 46 const char *c_name; 47 const char *c_desc; 48 const char *c_flagdesc; 49 const char *c_fielddesc; 50 int (*c_func)(const nvme_process_arg_t *); 51 void (*c_usage)(const char *); 52 void (*c_optparse)(nvme_process_arg_t *); 53 int c_flags; 54 }; 55 56 /* 57 * This is used to represent information for getting and printing specific 58 * features. 59 */ 60 struct nvmeadm_feature { 61 uint8_t f_feature; 62 boolean_t (*f_get)(const nvme_process_arg_t *, const nvme_feat_disc_t *, 63 const nvmeadm_feature_t *); 64 void (*f_print)(uint32_t, void *, size_t, const nvme_identify_ctrl_t *, 65 const nvme_version_t *); 66 }; 67 68 struct nvme_process_arg { 69 nvme_t *npa_nvme; 70 nvme_ctrl_t *npa_ctrl; 71 nvme_ns_t *npa_ns; 72 nvme_ctrl_info_t *npa_ctrl_info; 73 nvme_ns_info_t *npa_ns_info; 74 int npa_argc; 75 char **npa_argv; 76 char *npa_name; 77 const char *npa_ctrl_name; 78 boolean_t npa_excl; 79 uint32_t npa_cmdflags; 80 const nvmeadm_cmd_t *npa_cmd; 81 const nvme_identify_ctrl_t *npa_idctl; 82 const nvme_version_t *npa_version; 83 ofmt_handle_t npa_ofmt; 84 void *npa_cmd_arg; 85 }; 86 87 /* 88 * Command-specific arguments 89 */ 90 typedef struct { 91 boolean_t nll_unimpl; 92 nvme_log_disc_scope_t nll_scope; 93 uint32_t nll_nprint; 94 int nll_nfilts; 95 char *const *nll_filts; 96 boolean_t *nll_used; 97 } nvmeadm_list_logs_t; 98 99 typedef struct { 100 boolean_t nf_unimpl; 101 uint32_t nf_nprint; 102 uint32_t nf_nfilts; 103 char *const *nf_filts; 104 boolean_t *nf_used; 105 } nvmeadm_features_t; 106 107 typedef struct { 108 boolean_t ncn_use_flbas; 109 nvme_csi_t ncn_csi; 110 uint64_t ncn_size; 111 uint64_t ncn_cap; 112 uint32_t ncn_lba; 113 uint32_t ncn_nmic; 114 } nvmeadm_create_ns_t; 115 116 typedef struct { 117 const char *ngl_output; 118 } nvmeadm_get_logpage_t; 119 120 /* Version checking */ 121 extern boolean_t nvme_version_check(const nvme_process_arg_t *, 122 const nvme_version_t *); 123 124 /* printing functions */ 125 extern int nvme_strlen(const char *, int); 126 extern void nvme_print(int, const char *, int, const char *, ...); 127 extern int nvme_snprint_uint128(char *, size_t, nvme_uint128_t, int, int); 128 extern void nvme_print_ctrl_summary(nvme_ctrl_info_t *); 129 extern void nvme_print_nsid_summary(nvme_ns_info_t *); 130 extern void nvme_print_identify_ctrl(const nvme_identify_ctrl_t *, uint32_t, 131 const nvme_version_t *); 132 extern void nvme_print_identify_nsid(const nvme_identify_nsid_t *, 133 const nvme_version_t *); 134 extern void nvme_print_identify_nsid_list(const char *, 135 const nvme_identify_nsid_list_t *); 136 extern void nvme_print_identify_nsid_desc(void *); 137 extern void nvme_print_identify_ctrl_list(const char *, 138 const nvme_identify_ctrl_list_t *); 139 extern void nvme_print_error_log(int, const nvme_error_log_entry_t *, 140 const nvme_version_t *); 141 extern void nvme_print_health_log(const nvme_health_log_t *, 142 const nvme_identify_ctrl_t *, 143 const nvme_version_t *); 144 extern void nvme_print_fwslot_log(const nvme_fwslot_log_t *, 145 const nvme_identify_ctrl_t *); 146 147 extern void nvme_print_feat_unknown(nvme_feat_output_t, uint32_t, void *, 148 size_t); 149 extern void nvme_print_feat_arbitration(uint32_t, void *, size_t, 150 const nvme_identify_ctrl_t *, const nvme_version_t *); 151 extern void nvme_print_feat_power_mgmt(uint32_t, void *, size_t, 152 const nvme_identify_ctrl_t *, const nvme_version_t *); 153 extern void nvme_print_feat_lba_range(uint32_t, void *, size_t, 154 const nvme_identify_ctrl_t *, const nvme_version_t *); 155 extern void nvme_print_feat_temperature(uint32_t, void *, size_t, 156 const nvme_identify_ctrl_t *, const nvme_version_t *); 157 extern void nvme_print_feat_error(uint32_t, void *, size_t, 158 const nvme_identify_ctrl_t *, const nvme_version_t *); 159 extern void nvme_print_feat_write_cache(uint32_t, void *, size_t, 160 const nvme_identify_ctrl_t *, const nvme_version_t *); 161 extern void nvme_print_feat_nqueues(uint32_t, void *, size_t, 162 const nvme_identify_ctrl_t *, const nvme_version_t *); 163 extern void nvme_print_feat_intr_coal(uint32_t, void *, size_t, 164 const nvme_identify_ctrl_t *, const nvme_version_t *); 165 extern void nvme_print_feat_intr_vect(uint32_t, void *, size_t, 166 const nvme_identify_ctrl_t *, const nvme_version_t *); 167 extern void nvme_print_feat_write_atom(uint32_t, void *, size_t, 168 const nvme_identify_ctrl_t *, const nvme_version_t *); 169 extern void nvme_print_feat_async_event(uint32_t, void *, size_t, 170 const nvme_identify_ctrl_t *, const nvme_version_t *); 171 extern void nvme_print_feat_auto_pst(uint32_t, void *, size_t, 172 const nvme_identify_ctrl_t *, const nvme_version_t *); 173 extern void nvme_print_feat_progress(uint32_t, void *, size_t, 174 const nvme_identify_ctrl_t *, const nvme_version_t *); 175 176 extern void nvmeadm_dump_hex(const uint8_t *, size_t); 177 178 /* 179 * ofmt related 180 */ 181 typedef struct { 182 const char *nloa_name; 183 nvme_ctrl_info_t *nloa_ctrl; 184 nvme_ns_info_t *nloa_ns; 185 const char *nloa_disk; 186 const char *nloa_state; 187 } nvmeadm_list_ofmt_arg_t; 188 189 extern const ofmt_field_t nvmeadm_list_ctrl_ofmt[]; 190 extern const ofmt_field_t nvmeadm_list_nsid_ofmt[]; 191 192 typedef struct { 193 const char *nlloa_name; 194 const nvme_log_disc_t *nlloa_disc; 195 } nvmeadm_list_logs_ofmt_arg_t; 196 197 extern const char *nvmeadm_list_logs_fields; 198 extern const char *nvmeadm_list_logs_fields_impl; 199 extern const ofmt_field_t nvmeadm_list_logs_ofmt[]; 200 201 typedef struct { 202 const char *nlfoa_name; 203 const nvme_feat_disc_t *nlfoa_feat; 204 } nvmeadm_list_features_ofmt_arg_t; 205 206 extern const char *nvmeadm_list_features_fields; 207 extern const ofmt_field_t nvmeadm_list_features_ofmt[]; 208 209 210 /* 211 * Warning and error cases. The default nvmeadm ones assume a libnvme related 212 * issue. Most errors are on the nvme_ctrl_t, which are the versions without any 213 * args. The ones that operate on the nvme_t handle have hdl in the name. 214 */ 215 extern void nvmeadm_warn(const nvme_process_arg_t *, const char *, 216 ...) __PRINTFLIKE(2); 217 extern void nvmeadm_fatal(const nvme_process_arg_t *, const char *, 218 ...) __PRINTFLIKE(2) __NORETURN; 219 extern void nvmeadm_hdl_warn(const nvme_process_arg_t *, const char *, 220 ...) __PRINTFLIKE(2); 221 extern void nvmeadm_hdl_fatal(const nvme_process_arg_t *, const char *, 222 ...) __PRINTFLIKE(2) __NORETURN; 223 224 /* 225 * Namespace Management Commands 226 */ 227 extern int do_create_ns(const nvme_process_arg_t *); 228 extern void optparse_create_ns(nvme_process_arg_t *); 229 extern void usage_create_ns(const char *); 230 231 extern int do_delete_ns(const nvme_process_arg_t *); 232 extern void usage_delete_ns(const char *); 233 234 extern int do_attach_ns(const nvme_process_arg_t *); 235 extern void usage_attach_ns(const char *); 236 237 extern int do_detach_ns(const nvme_process_arg_t *); 238 extern void usage_detach_ns(const char *); 239 240 /* 241 * Vendor specific commands. 242 * 243 * All vendor commands must first call nvmeadm_vuc_validate() which will 244 * validate that a given vendor unique command is useable by the device and then 245 * proceed to take any necessary locks that the command suggests. 246 */ 247 extern nvme_vuc_disc_t *nvmeadm_vuc_init(const nvme_process_arg_t *, 248 const char *); 249 extern void nvmeadm_vuc_fini(const nvme_process_arg_t *, nvme_vuc_disc_t *); 250 251 extern int do_wdc_e6dump(const nvme_process_arg_t *); 252 extern void optparse_wdc_e6dump(nvme_process_arg_t *); 253 extern void usage_wdc_e6dump(const char *); 254 255 extern int do_wdc_resize(const nvme_process_arg_t *); 256 extern void optparse_wdc_resize(nvme_process_arg_t *); 257 extern void usage_wdc_resize(const char *); 258 259 extern int do_wdc_clear_assert(const nvme_process_arg_t *); 260 extern void usage_wdc_clear_assert(const char *); 261 262 extern int do_wdc_inject_assert(const nvme_process_arg_t *); 263 extern void usage_wdc_inject_assert(const char *); 264 265 #ifdef __cplusplus 266 } 267 #endif 268 269 #endif /* _NVMEADM_H */ 270