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 2016 The MathWorks, Inc. All rights reserved. 14 * Copyright 2019 Joyent, Inc. 15 * Copyright 2019 Unix Software Ltd. 16 * Copyright 2024 Oxide Computer Company. 17 * Copyright 2022 OmniOS Community Edition (OmniOSce) Association. 18 * Copyright 2022 Tintri by DDN, Inc. All rights reserved. 19 */ 20 21 #ifndef _NVME_VAR_H 22 #define _NVME_VAR_H 23 24 #include <sys/ddi.h> 25 #include <sys/sunddi.h> 26 #include <sys/blkdev.h> 27 #include <sys/taskq_impl.h> 28 #include <sys/list.h> 29 #include <sys/ddi_ufm.h> 30 #include <nvme_common.h> 31 32 /* 33 * NVMe driver state 34 */ 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif 39 40 #define NVME_MODULE_NAME "nvme" 41 42 typedef enum { 43 NVME_PCI_CONFIG = 1 << 0, 44 NVME_FMA_INIT = 1 << 1, 45 NVME_REGS_MAPPED = 1 << 2, 46 NVME_ADMIN_QUEUE = 1 << 3, 47 NVME_CTRL_LIMITS = 1 << 4, 48 NVME_INTERRUPTS = 1 << 5, 49 NVME_UFM_INIT = 1 << 6, 50 NVME_MUTEX_INIT = 1 << 7, 51 NVME_MGMT_INIT = 1 << 8, 52 NVME_STAT_INIT = 1 << 9 53 } nvme_progress_t; 54 55 typedef enum { 56 NVME_NS_LOCK = 1 << 0 57 } nvme_ns_progress_t; 58 59 typedef enum { 60 /* 61 * The controller fails to properly process commands on the admin queue 62 * if the first one has CID 0. Subsequent use of CID 0 doesn't present 63 * a problem. 64 */ 65 NVME_QUIRK_START_CID = 1 << 0, 66 } nvme_quirk_t; 67 68 #define NVME_MIN_ADMIN_QUEUE_LEN 16 69 #define NVME_MIN_IO_QUEUE_LEN 16 70 #define NVME_DEFAULT_ADMIN_QUEUE_LEN 256 71 #define NVME_DEFAULT_IO_QUEUE_LEN 1024 72 #define NVME_DEFAULT_ASYNC_EVENT_LIMIT 10 73 #define NVME_MIN_ASYNC_EVENT_LIMIT 1 74 #define NVME_DEFAULT_MIN_BLOCK_SIZE 512 75 76 typedef struct nvme nvme_t; 77 typedef struct nvme_namespace nvme_namespace_t; 78 typedef struct nvme_minor nvme_minor_t; 79 typedef struct nvme_lock nvme_lock_t; 80 typedef struct nvme_minor_lock_info nvme_minor_lock_info_t; 81 typedef struct nvme_dma nvme_dma_t; 82 typedef struct nvme_cmd nvme_cmd_t; 83 typedef struct nvme_cq nvme_cq_t; 84 typedef struct nvme_qpair nvme_qpair_t; 85 typedef struct nvme_task_arg nvme_task_arg_t; 86 typedef struct nvme_device_stat nvme_device_stat_t; 87 typedef struct nvme_admin_stat nvme_admin_stat_t; 88 89 /* 90 * These states represent the minor's perspective. That is, of a minor's 91 * namespace and controller lock, where is it? 92 */ 93 typedef enum { 94 NVME_LOCK_STATE_UNLOCKED = 0, 95 NVME_LOCK_STATE_BLOCKED, 96 NVME_LOCK_STATE_ACQUIRED 97 } nvme_minor_lock_state_t; 98 99 struct nvme_minor_lock_info { 100 list_node_t nli_node; 101 nvme_lock_t *nli_lock; 102 nvme_minor_lock_state_t nli_state; 103 nvme_lock_level_t nli_curlevel; 104 /* 105 * While the minor points back to itself and the nvme_t should always 106 * point to the current controller, the namespace should only point to 107 * one if this is a particular namespace lock. The former two are 108 * initialized at minor initialization time. 109 */ 110 nvme_minor_t *nli_minor; 111 nvme_t *nli_nvme; 112 nvme_namespace_t *nli_ns; 113 /* 114 * This is the common ioctl information that should be filled in when 115 * we're being woken up for any reason other than an interrupted signal. 116 * This should only be set while blocking. 117 */ 118 nvme_ioctl_common_t *nli_ioc; 119 /* 120 * The following are provided for debugging purposes. In particular, 121 * information like the kthread_t and related that performed this should 122 * be considered suspect as it represents who took the operation, not 123 * who performed the operation (unless we're actively blocking). 124 */ 125 hrtime_t nli_last_change; 126 uintptr_t nli_acq_kthread; 127 pid_t nli_acq_pid; 128 }; 129 130 struct nvme_minor { 131 /* 132 * The following three fields are set when this is created. 133 */ 134 id_t nm_minor; 135 nvme_t *nm_ctrl; 136 nvme_namespace_t *nm_ns; 137 /* 138 * This link is used to index this minor on the global list of active 139 * open-related minors. This is only manipulated under the 140 * nvme_open_minors_mutex. 141 */ 142 avl_node_t nm_avl; 143 /* 144 * Information related to locking. Note, there is no pointer to a locked 145 * controller as the only one can be the one specified here. This data 146 * is protected by the controller's n_minor_mutex. 147 */ 148 kcondvar_t nm_cv; 149 nvme_minor_lock_info_t nm_ctrl_lock; 150 nvme_minor_lock_info_t nm_ns_lock; 151 }; 152 153 struct nvme_lock { 154 nvme_minor_lock_info_t *nl_writer; 155 list_t nl_readers; 156 list_t nl_pend_readers; 157 list_t nl_pend_writers; 158 /* 159 * The following are stats to indicate how often certain locking 160 * activities have occurred for debugging purposes. 161 */ 162 uint32_t nl_nwrite_locks; 163 uint32_t nl_nread_locks; 164 uint32_t nl_npend_writes; 165 uint32_t nl_npend_reads; 166 uint32_t nl_nnonblock; 167 uint32_t nl_nsignals; 168 uint32_t nl_nsig_unlock; 169 uint32_t nl_nsig_blocks; 170 uint32_t nl_nsig_acq; 171 }; 172 173 struct nvme_dma { 174 ddi_dma_handle_t nd_dmah; 175 ddi_acc_handle_t nd_acch; 176 ddi_dma_cookie_t nd_cookie; 177 uint_t nd_ncookie; 178 caddr_t nd_memp; 179 size_t nd_len; 180 boolean_t nd_cached; 181 }; 182 183 typedef enum { 184 NVME_CMD_ALLOCATED = 0, 185 NVME_CMD_SUBMITTED, 186 NVME_CMD_QUEUED, 187 NVME_CMD_COMPLETED, 188 NVME_CMD_LOST 189 } nvme_cmd_state_t; 190 191 typedef enum { 192 NVME_CMD_F_DONTPANIC = 1 << 0, 193 NVME_CMD_F_USELOCK = 1 << 1, 194 } nvme_cmd_flag_t; 195 196 /* 197 * This command structure is shared between admin and I/O commands. When used 198 * for an admin command, nc_mutex and nc_cv are used to synchronise access to 199 * various fields, and to signal command completion. NVME_CMD_F_USELOCK in 200 * nc_flags indicates whether the lock and CV are in use. For I/O commands, 201 * these are neither initialised nor used. 202 */ 203 struct nvme_cmd { 204 struct list_node nc_list; 205 206 nvme_sqe_t nc_sqe; 207 nvme_cqe_t nc_cqe; 208 209 void (*nc_callback)(void *); 210 bd_xfer_t *nc_xfer; 211 212 uint32_t nc_timeout; 213 nvme_cmd_flag_t nc_flags; 214 nvme_cmd_state_t nc_state; /* Protected by nc_mutex iff F_USELOCK */ 215 uint16_t nc_sqid; 216 217 hrtime_t nc_submit_ts; 218 hrtime_t nc_queue_ts; 219 220 nvme_dma_t *nc_dma; 221 nvme_dma_t *nc_prp; /* DMA for PRP lists */ 222 223 kmutex_t nc_mutex; 224 kcondvar_t nc_cv; 225 226 taskq_ent_t nc_tqent; 227 nvme_t *nc_nvme; 228 }; 229 230 struct nvme_cq { 231 size_t ncq_nentry; 232 uint16_t ncq_id; 233 234 nvme_dma_t *ncq_dma; 235 nvme_cqe_t *ncq_cq; 236 uint_t ncq_head; 237 uintptr_t ncq_hdbl; 238 int ncq_phase; 239 240 taskq_t *ncq_cmd_taskq; 241 242 kmutex_t ncq_mutex; 243 }; 244 245 struct nvme_qpair { 246 size_t nq_nentry; 247 248 /* submission fields */ 249 nvme_dma_t *nq_sqdma; 250 nvme_sqe_t *nq_sq; 251 uint_t nq_sqhead; 252 uint_t nq_sqtail; 253 uintptr_t nq_sqtdbl; 254 255 /* completion */ 256 nvme_cq_t *nq_cq; 257 258 /* shared structures for completion and submission */ 259 nvme_cmd_t **nq_cmd; /* active command array */ 260 uint16_t nq_next_cmd; /* next potential empty queue slot */ 261 uint_t nq_active_cmds; /* number of active cmds */ 262 uint32_t nq_active_timeout; /* sum of the timeouts of active cmds */ 263 264 kmutex_t nq_mutex; /* protects shared state */ 265 ksema_t nq_sema; /* semaphore to ensure q always has >= 1 empty slot */ 266 }; 267 268 typedef struct nvme_mgmt_lock { 269 kmutex_t nml_lock; 270 kcondvar_t nml_cv; 271 uintptr_t nml_bd_own; 272 } nvme_mgmt_lock_t; 273 274 struct nvme_device_stat { 275 /* Errors detected by driver */ 276 kstat_named_t nds_dma_bind_err; 277 kstat_named_t nds_abort_timeout; 278 kstat_named_t nds_abort_failed; 279 kstat_named_t nds_abort_successful; 280 kstat_named_t nds_abort_unsuccessful; 281 kstat_named_t nds_cmd_timeout; 282 kstat_named_t nds_wrong_logpage; 283 kstat_named_t nds_unknown_logpage; 284 kstat_named_t nds_too_many_cookies; 285 kstat_named_t nds_unknown_cid; 286 287 /* Errors detected by hardware */ 288 kstat_named_t nds_inv_cmd_err; 289 kstat_named_t nds_inv_field_err; 290 kstat_named_t nds_inv_nsfmt_err; 291 kstat_named_t nds_data_xfr_err; 292 kstat_named_t nds_internal_err; 293 kstat_named_t nds_abort_rq_err; 294 kstat_named_t nds_abort_pwrloss_err; 295 kstat_named_t nds_abort_sq_del; 296 kstat_named_t nds_nvm_cap_exc; 297 kstat_named_t nds_nvm_ns_notrdy; 298 kstat_named_t nds_nvm_ns_formatting; 299 kstat_named_t nds_inv_cq_err; 300 kstat_named_t nds_inv_qid_err; 301 kstat_named_t nds_max_qsz_exc; 302 kstat_named_t nds_inv_int_vect; 303 kstat_named_t nds_inv_log_page; 304 kstat_named_t nds_inv_format; 305 kstat_named_t nds_inv_q_del; 306 kstat_named_t nds_cnfl_attr; 307 kstat_named_t nds_inv_prot; 308 kstat_named_t nds_readonly; 309 kstat_named_t nds_inv_fwslot; 310 kstat_named_t nds_inv_fwimg; 311 kstat_named_t nds_fwact_creset; 312 kstat_named_t nds_fwact_nssr; 313 kstat_named_t nds_fwact_reset; 314 kstat_named_t nds_fwact_mtfa; 315 kstat_named_t nds_fwact_prohibited; 316 kstat_named_t nds_fw_overlap; 317 318 /* Errors reported by asynchronous events */ 319 kstat_named_t nds_diagfail_event; 320 kstat_named_t nds_persistent_event; 321 kstat_named_t nds_transient_event; 322 kstat_named_t nds_fw_load_event; 323 kstat_named_t nds_reliability_event; 324 kstat_named_t nds_temperature_event; 325 kstat_named_t nds_spare_event; 326 kstat_named_t nds_vendor_event; 327 kstat_named_t nds_notice_event; 328 kstat_named_t nds_unknown_event; 329 }; 330 331 #define NAS_CNT 0 332 #define NAS_AVG 1 333 #define NAS_MAX 2 334 struct nvme_admin_stat { 335 kstat_named_t nas_getlogpage[3]; 336 kstat_named_t nas_identify[3]; 337 kstat_named_t nas_abort[3]; 338 kstat_named_t nas_fwactivate[3]; 339 kstat_named_t nas_fwimgload[3]; 340 kstat_named_t nas_nsformat[3]; 341 kstat_named_t nas_vendor[3]; 342 kstat_named_t nas_other[3]; 343 }; 344 345 struct nvme { 346 dev_info_t *n_dip; 347 nvme_progress_t n_progress; 348 nvme_quirk_t n_quirks; 349 350 caddr_t n_regs; 351 ddi_acc_handle_t n_regh; 352 353 kmem_cache_t *n_cmd_cache; 354 kmem_cache_t *n_prp_cache; 355 356 size_t n_inth_sz; 357 ddi_intr_handle_t *n_inth; 358 int n_intr_cnt; 359 uint_t n_intr_pri; 360 int n_intr_cap; 361 int n_intr_type; 362 int n_intr_types; 363 364 ddi_acc_handle_t n_pcicfg_handle; 365 uint16_t n_vendor_id; 366 uint16_t n_device_id; 367 uint16_t n_subsystem_vendor_id; 368 uint16_t n_subsystem_device_id; 369 uint8_t n_revision_id; 370 371 char *n_product; 372 char *n_vendor; 373 374 nvme_version_t n_version; 375 boolean_t n_dead; 376 nvme_ioctl_errno_t n_dead_status; 377 taskq_ent_t n_dead_tqent; 378 boolean_t n_strict_version; 379 boolean_t n_ignore_unknown_vendor_status; 380 uint32_t n_admin_queue_len; 381 uint32_t n_io_squeue_len; 382 uint32_t n_io_cqueue_len; 383 uint16_t n_async_event_limit; 384 uint_t n_min_block_size; 385 uint16_t n_abort_command_limit; 386 uint64_t n_max_data_transfer_size; 387 boolean_t n_write_cache_present; 388 boolean_t n_write_cache_enabled; 389 int n_error_log_len; 390 boolean_t n_async_event_supported; 391 int n_submission_queues; 392 int n_completion_queues; 393 394 int n_nssr_supported; 395 int n_doorbell_stride; 396 int n_timeout; 397 int n_arbitration_mechanisms; 398 int n_cont_queues_reqd; 399 int n_max_queue_entries; 400 int n_pageshift; 401 int n_pagesize; 402 403 uint32_t n_namespace_count; 404 uint_t n_namespaces_attachable; 405 uint_t n_ioq_count; 406 uint_t n_cq_count; 407 408 /* 409 * This is cached identify controller and common namespace data that 410 * exists in the system. This generally can be used in the kernel; 411 * however, we have to be careful about what we use here because these 412 * values are not refreshed after attach. Therefore these are good for 413 * answering the question what does the controller support or what is in 414 * the common namespace information, but not otherwise. That means you 415 * shouldn't use this to try to answer how much capacity is still in the 416 * controller because this information is just cached. 417 */ 418 nvme_identify_ctrl_t *n_idctl; 419 nvme_identify_nsid_t *n_idcomns; 420 421 /* Pointer to the admin queue, which is always queue 0 in n_ioq. */ 422 nvme_qpair_t *n_adminq; 423 /* 424 * All command queues, including the admin queue. 425 * Its length is: n_ioq_count + 1. 426 */ 427 nvme_qpair_t **n_ioq; 428 nvme_cq_t **n_cq; 429 430 nvme_namespace_t *n_ns; 431 432 ddi_dma_attr_t n_queue_dma_attr; 433 ddi_dma_attr_t n_prp_dma_attr; 434 ddi_dma_attr_t n_sgl_dma_attr; 435 ddi_device_acc_attr_t n_reg_acc_attr; 436 ddi_iblock_cookie_t n_fm_ibc; 437 int n_fm_cap; 438 439 ksema_t n_abort_sema; 440 441 /* protects namespace management operations */ 442 nvme_mgmt_lock_t n_mgmt; 443 444 /* 445 * This lock protects the minor node locking state across the controller 446 * and all related namespaces. 447 */ 448 kmutex_t n_minor_mutex; 449 nvme_lock_t n_lock; 450 451 kstat_t *n_device_kstat; 452 nvme_device_stat_t n_device_stat; 453 454 kstat_t *n_admin_kstat; 455 kmutex_t n_admin_stat_mutex; 456 nvme_admin_stat_t n_admin_stat; 457 458 /* hot removal NDI event handling */ 459 ddi_eventcookie_t n_rm_cookie; 460 ddi_callback_id_t n_ev_rm_cb_id; 461 462 /* DDI UFM handle */ 463 ddi_ufm_handle_t *n_ufmh; 464 /* Cached Firmware Slot Information log page */ 465 nvme_fwslot_log_t *n_fwslot; 466 /* Lock protecting the cached firmware slot info */ 467 kmutex_t n_fwslot_mutex; 468 }; 469 470 struct nvme_namespace { 471 nvme_t *ns_nvme; 472 nvme_ns_progress_t ns_progress; 473 uint8_t ns_eui64[8]; 474 uint8_t ns_nguid[16]; 475 char ns_name[11]; 476 477 bd_handle_t ns_bd_hdl; 478 479 uint32_t ns_id; 480 size_t ns_block_count; 481 size_t ns_block_size; 482 size_t ns_best_block_size; 483 484 boolean_t ns_allocated; 485 boolean_t ns_active; 486 boolean_t ns_ignore; 487 boolean_t ns_attached; 488 489 nvme_identify_nsid_t *ns_idns; 490 491 /* 492 * Namespace lock, see the theory statement for more information. 493 */ 494 nvme_lock_t ns_lock; 495 496 /* 497 * If a namespace has neither NGUID nor EUI64, we create a devid in 498 * nvme_prepare_devid(). 499 */ 500 char *ns_devid; 501 }; 502 503 struct nvme_task_arg { 504 nvme_t *nt_nvme; 505 nvme_cmd_t *nt_cmd; 506 }; 507 508 typedef enum { 509 /* 510 * This indicates that there is no exclusive access required for this 511 * operation. However, this operation will fail if someone attempts to 512 * perform this operation and someone else holds a write lock. 513 */ 514 NVME_IOCTL_EXCL_NONE = 0, 515 /* 516 * This indicates that a write lock is required to perform the 517 * operation. 518 */ 519 NVME_IOCTL_EXCL_WRITE, 520 /* 521 * This indicates that the exclusive check should be skipped. The only 522 * case this should be used in is the lock and unlock ioctls as they 523 * should be able to proceed even when the controller is being used 524 * exclusively. 525 */ 526 NVME_IOCTL_EXCL_SKIP 527 } nvme_ioctl_excl_t; 528 529 /* 530 * This structure represents the set of checks that we apply to ioctl's using 531 * the nvme_ioctl_common_t structure as part of validation. 532 */ 533 typedef struct nvme_ioctl_check { 534 /* 535 * This indicates whether or not the command in question allows a 536 * namespace to be specified at all. If this is false, a namespace minor 537 * cannot be used and a controller minor must leave the nsid set to 538 * zero. 539 */ 540 boolean_t nck_ns_ok; 541 /* 542 * This indicates that a minor node corresponding to a namespace is 543 * allowed to issue this. 544 */ 545 boolean_t nck_ns_minor_ok; 546 /* 547 * This indicates that the controller should be skipped from all of the 548 * following processing behavior. That is, it's allowed to specify 549 * whatever it wants in the nsid field, regardless if it is valid or 550 * not. This is required for some of the Identify Command options that 551 * list endpoints. This should generally not be used and the driver 552 * should still validate the nuance here. 553 */ 554 boolean_t nck_skip_ctrl; 555 /* 556 * This indicates that if we're on the controller's minor and we don't 557 * have an explicit namespace ID (i.e. 0), should the namespace be 558 * rewritten to be the broadcast namespace. 559 */ 560 boolean_t nck_ctrl_rewrite; 561 /* 562 * This indicates whether or not the broadcast NSID is acceptable for 563 * the controller node. 564 */ 565 boolean_t nck_bcast_ok; 566 567 /* 568 * This indicates to the lock checking code what kind of exclusive 569 * access is required. This check occurs after any namespace rewriting 570 * has occurred. When looking at exclusivity, a broadcast namespace or 571 * namespace 0 indicate that the controller is the target, otherwise the 572 * target namespace will be checked for a write lock. 573 */ 574 nvme_ioctl_excl_t nck_excl; 575 } nvme_ioctl_check_t; 576 577 /* 578 * Constants 579 */ 580 extern uint_t nvme_vendor_specific_admin_cmd_max_timeout; 581 extern uint32_t nvme_vendor_specific_admin_cmd_size; 582 583 /* 584 * Common functions. 585 */ 586 extern nvme_namespace_t *nvme_nsid2ns(nvme_t *, uint32_t); 587 extern boolean_t nvme_ioctl_error(nvme_ioctl_common_t *, nvme_ioctl_errno_t, 588 uint32_t, uint32_t); 589 extern boolean_t nvme_ctrl_atleast(nvme_t *, const nvme_version_t *); 590 extern void nvme_ioctl_success(nvme_ioctl_common_t *); 591 592 /* 593 * Validation related functions and kernel tunable limits. 594 */ 595 extern boolean_t nvme_validate_logpage(nvme_t *, nvme_ioctl_get_logpage_t *); 596 extern boolean_t nvme_validate_identify(nvme_t *, nvme_ioctl_identify_t *, 597 boolean_t); 598 extern boolean_t nvme_validate_get_feature(nvme_t *, 599 nvme_ioctl_get_feature_t *); 600 extern boolean_t nvme_validate_vuc(nvme_t *, nvme_ioctl_passthru_t *); 601 extern boolean_t nvme_validate_format(nvme_t *, nvme_ioctl_format_t *); 602 extern boolean_t nvme_validate_fw_load(nvme_t *, nvme_ioctl_fw_load_t *); 603 extern boolean_t nvme_validate_fw_commit(nvme_t *, nvme_ioctl_fw_commit_t *); 604 605 /* 606 * Locking functions 607 */ 608 extern void nvme_rwlock(nvme_minor_t *, nvme_ioctl_lock_t *); 609 extern void nvme_rwunlock(nvme_minor_lock_info_t *, nvme_lock_t *); 610 extern void nvme_rwlock_ctrl_dead(void *); 611 extern void nvme_lock_init(nvme_lock_t *); 612 extern void nvme_lock_fini(nvme_lock_t *); 613 614 /* 615 * Statistics functions 616 */ 617 extern boolean_t nvme_stat_init(nvme_t *); 618 extern void nvme_stat_cleanup(nvme_t *); 619 extern void nvme_admin_stat_cmd(nvme_t *, nvme_cmd_t *); 620 621 #ifdef __cplusplus 622 } 623 #endif 624 625 #endif /* _NVME_VAR_H */ 626