1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2016 Intel Corporation. All rights reserved. 3 * Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved. 4 * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 5 */ 6 7 #ifndef __NVMF_INTERNAL_H__ 8 #define __NVMF_INTERNAL_H__ 9 10 #include "spdk/stdinc.h" 11 12 #include "spdk/likely.h" 13 #include "spdk/nvmf.h" 14 #include "spdk/nvmf_cmd.h" 15 #include "spdk/nvmf_transport.h" 16 #include "spdk/nvmf_spec.h" 17 #include "spdk/assert.h" 18 #include "spdk/bdev.h" 19 #include "spdk/queue.h" 20 #include "spdk/util.h" 21 #include "spdk/thread.h" 22 #include "spdk/tree.h" 23 24 /* The spec reserves cntlid values in the range FFF0h to FFFFh. */ 25 #define NVMF_MIN_CNTLID 1 26 #define NVMF_MAX_CNTLID 0xFFEF 27 28 enum spdk_nvmf_tgt_state { 29 NVMF_TGT_IDLE = 0, 30 NVMF_TGT_RUNNING, 31 NVMF_TGT_PAUSING, 32 NVMF_TGT_PAUSED, 33 NVMF_TGT_RESUMING, 34 }; 35 36 enum spdk_nvmf_subsystem_state { 37 SPDK_NVMF_SUBSYSTEM_INACTIVE = 0, 38 SPDK_NVMF_SUBSYSTEM_ACTIVATING, 39 SPDK_NVMF_SUBSYSTEM_ACTIVE, 40 SPDK_NVMF_SUBSYSTEM_PAUSING, 41 SPDK_NVMF_SUBSYSTEM_PAUSED, 42 SPDK_NVMF_SUBSYSTEM_RESUMING, 43 SPDK_NVMF_SUBSYSTEM_DEACTIVATING, 44 SPDK_NVMF_SUBSYSTEM_NUM_STATES, 45 }; 46 47 RB_HEAD(subsystem_tree, spdk_nvmf_subsystem); 48 49 struct spdk_nvmf_tgt { 50 char name[NVMF_TGT_NAME_MAX_LENGTH]; 51 52 pthread_mutex_t mutex; 53 54 uint64_t discovery_genctr; 55 56 uint32_t max_subsystems; 57 58 enum spdk_nvmf_tgt_discovery_filter discovery_filter; 59 60 enum spdk_nvmf_tgt_state state; 61 62 struct spdk_bit_array *subsystem_ids; 63 64 struct subsystem_tree subsystems; 65 66 TAILQ_HEAD(, spdk_nvmf_transport) transports; 67 TAILQ_HEAD(, spdk_nvmf_poll_group) poll_groups; 68 69 /* Used for round-robin assignment of connections to poll groups */ 70 struct spdk_nvmf_poll_group *next_poll_group; 71 72 spdk_nvmf_tgt_destroy_done_fn *destroy_cb_fn; 73 void *destroy_cb_arg; 74 75 uint16_t crdt[3]; 76 uint16_t num_poll_groups; 77 78 TAILQ_ENTRY(spdk_nvmf_tgt) link; 79 }; 80 81 struct spdk_nvmf_host { 82 char nqn[SPDK_NVMF_NQN_MAX_LEN + 1]; 83 TAILQ_ENTRY(spdk_nvmf_host) link; 84 }; 85 86 struct spdk_nvmf_subsystem_listener { 87 struct spdk_nvmf_subsystem *subsystem; 88 spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn; 89 void *cb_arg; 90 struct spdk_nvme_transport_id *trid; 91 struct spdk_nvmf_transport *transport; 92 enum spdk_nvme_ana_state *ana_state; 93 uint64_t ana_state_change_count; 94 uint16_t id; 95 TAILQ_ENTRY(spdk_nvmf_subsystem_listener) link; 96 }; 97 98 /* Maximum number of registrants supported per namespace */ 99 #define SPDK_NVMF_MAX_NUM_REGISTRANTS 16 100 101 struct spdk_nvmf_registrant_info { 102 uint64_t rkey; 103 char host_uuid[SPDK_UUID_STRING_LEN]; 104 }; 105 106 struct spdk_nvmf_reservation_info { 107 bool ptpl_activated; 108 enum spdk_nvme_reservation_type rtype; 109 uint64_t crkey; 110 char bdev_uuid[SPDK_UUID_STRING_LEN]; 111 char holder_uuid[SPDK_UUID_STRING_LEN]; 112 uint32_t num_regs; 113 struct spdk_nvmf_registrant_info registrants[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 114 }; 115 116 struct spdk_nvmf_subsystem_pg_ns_info { 117 struct spdk_io_channel *channel; 118 struct spdk_uuid uuid; 119 /* current reservation key, no reservation if the value is 0 */ 120 uint64_t crkey; 121 /* reservation type */ 122 enum spdk_nvme_reservation_type rtype; 123 /* Host ID which holds the reservation */ 124 struct spdk_uuid holder_id; 125 /* Host ID for the registrants with the namespace */ 126 struct spdk_uuid reg_hostid[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 127 uint64_t num_blocks; 128 129 /* I/O outstanding to this namespace */ 130 uint64_t io_outstanding; 131 enum spdk_nvmf_subsystem_state state; 132 }; 133 134 typedef void(*spdk_nvmf_poll_group_mod_done)(void *cb_arg, int status); 135 136 struct spdk_nvmf_subsystem_poll_group { 137 /* Array of namespace information for each namespace indexed by nsid - 1 */ 138 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 139 uint32_t num_ns; 140 enum spdk_nvmf_subsystem_state state; 141 142 /* Number of ADMIN and FABRICS requests outstanding */ 143 uint64_t mgmt_io_outstanding; 144 spdk_nvmf_poll_group_mod_done cb_fn; 145 void *cb_arg; 146 147 TAILQ_HEAD(, spdk_nvmf_request) queued; 148 }; 149 150 struct spdk_nvmf_registrant { 151 TAILQ_ENTRY(spdk_nvmf_registrant) link; 152 struct spdk_uuid hostid; 153 /* Registration key */ 154 uint64_t rkey; 155 }; 156 157 struct spdk_nvmf_ns { 158 uint32_t nsid; 159 uint32_t anagrpid; 160 struct spdk_nvmf_subsystem *subsystem; 161 struct spdk_bdev *bdev; 162 struct spdk_bdev_desc *desc; 163 struct spdk_nvmf_ns_opts opts; 164 /* reservation notification mask */ 165 uint32_t mask; 166 /* generation code */ 167 uint32_t gen; 168 /* registrants head */ 169 TAILQ_HEAD(, spdk_nvmf_registrant) registrants; 170 /* current reservation key */ 171 uint64_t crkey; 172 /* reservation type */ 173 enum spdk_nvme_reservation_type rtype; 174 /* current reservation holder, only valid if reservation type can only have one holder */ 175 struct spdk_nvmf_registrant *holder; 176 /* Persist Through Power Loss file which contains the persistent reservation */ 177 char *ptpl_file; 178 /* Persist Through Power Loss feature is enabled */ 179 bool ptpl_activated; 180 /* ZCOPY supported on bdev device */ 181 bool zcopy; 182 /* Command Set Identifier */ 183 enum spdk_nvme_csi csi; 184 }; 185 186 /* 187 * NVMf reservation notification log page. 188 */ 189 struct spdk_nvmf_reservation_log { 190 struct spdk_nvme_reservation_notification_log log; 191 TAILQ_ENTRY(spdk_nvmf_reservation_log) link; 192 struct spdk_nvmf_ctrlr *ctrlr; 193 }; 194 195 /* 196 * NVMf async event completion. 197 */ 198 struct spdk_nvmf_async_event_completion { 199 union spdk_nvme_async_event_completion event; 200 STAILQ_ENTRY(spdk_nvmf_async_event_completion) link; 201 }; 202 203 /* 204 * This structure represents an NVMe-oF controller, 205 * which is like a "session" in networking terms. 206 */ 207 struct spdk_nvmf_ctrlr { 208 uint16_t cntlid; 209 char hostnqn[SPDK_NVMF_NQN_MAX_LEN + 1]; 210 struct spdk_nvmf_subsystem *subsys; 211 212 struct spdk_nvmf_ctrlr_data cdata; 213 214 struct spdk_nvmf_registers vcprop; 215 216 struct spdk_nvmf_ctrlr_feat feat; 217 218 struct spdk_nvmf_qpair *admin_qpair; 219 struct spdk_thread *thread; 220 struct spdk_bit_array *qpair_mask; 221 222 const struct spdk_nvmf_subsystem_listener *listener; 223 224 struct spdk_nvmf_request *aer_req[SPDK_NVMF_MAX_ASYNC_EVENTS]; 225 STAILQ_HEAD(, spdk_nvmf_async_event_completion) async_events; 226 uint64_t notice_aen_mask; 227 uint8_t nr_aer_reqs; 228 struct spdk_uuid hostid; 229 230 uint32_t association_timeout; /* in milliseconds */ 231 uint16_t changed_ns_list_count; 232 struct spdk_nvme_ns_list changed_ns_list; 233 uint64_t log_page_count; 234 uint8_t num_avail_log_pages; 235 TAILQ_HEAD(log_page_head, spdk_nvmf_reservation_log) log_head; 236 237 /* Time to trigger keep-alive--poller_time = now_tick + period */ 238 uint64_t last_keep_alive_tick; 239 struct spdk_poller *keep_alive_poller; 240 241 struct spdk_poller *association_timer; 242 243 struct spdk_poller *cc_timer; 244 uint64_t cc_timeout_tsc; 245 struct spdk_poller *cc_timeout_timer; 246 247 bool dif_insert_or_strip; 248 bool in_destruct; 249 bool disconnect_in_progress; 250 /* valid only when disconnect_in_progress is true */ 251 bool disconnect_is_shn; 252 bool acre_enabled; 253 bool dynamic_ctrlr; 254 255 TAILQ_ENTRY(spdk_nvmf_ctrlr) link; 256 }; 257 258 #define NVMF_MAX_LISTENERS_PER_SUBSYSTEM 16 259 260 struct spdk_nvmf_subsystem { 261 struct spdk_thread *thread; 262 263 uint32_t id; 264 265 enum spdk_nvmf_subsystem_state state; 266 enum spdk_nvmf_subtype subtype; 267 268 uint16_t next_cntlid; 269 struct { 270 uint8_t allow_any_host : 1; 271 uint8_t allow_any_listener : 1; 272 uint8_t ana_reporting : 1; 273 uint8_t reserved : 5; 274 } flags; 275 276 /* boolean for state change synchronization */ 277 bool changing_state; 278 279 bool destroying; 280 bool async_destroy; 281 282 /* Zoned storage related fields */ 283 bool zone_append_supported; 284 uint64_t max_zone_append_size_kib; 285 286 struct spdk_nvmf_tgt *tgt; 287 RB_ENTRY(spdk_nvmf_subsystem) link; 288 289 /* Array of pointers to namespaces of size max_nsid indexed by nsid - 1 */ 290 struct spdk_nvmf_ns **ns; 291 uint32_t max_nsid; 292 293 uint16_t min_cntlid; 294 uint16_t max_cntlid; 295 296 TAILQ_HEAD(, spdk_nvmf_ctrlr) ctrlrs; 297 298 /* A mutex used to protect the hosts list and allow_any_host flag. Unlike the namespace 299 * array, this list is not used on the I/O path (it's needed for handling things like 300 * the CONNECT command), so use a mutex to protect it instead of requiring the subsystem 301 * state to be paused. This removes the requirement to pause the subsystem when hosts 302 * are added or removed dynamically. */ 303 pthread_mutex_t mutex; 304 TAILQ_HEAD(, spdk_nvmf_host) hosts; 305 TAILQ_HEAD(, spdk_nvmf_subsystem_listener) listeners; 306 struct spdk_bit_array *used_listener_ids; 307 308 TAILQ_ENTRY(spdk_nvmf_subsystem) entries; 309 310 nvmf_subsystem_destroy_cb async_destroy_cb; 311 void *async_destroy_cb_arg; 312 313 char sn[SPDK_NVME_CTRLR_SN_LEN + 1]; 314 char mn[SPDK_NVME_CTRLR_MN_LEN + 1]; 315 char subnqn[SPDK_NVMF_NQN_MAX_LEN + 1]; 316 317 /* Array of namespace count per ANA group of size max_nsid indexed anagrpid - 1 318 * It will be enough for ANA group to use the same size as namespaces. 319 */ 320 uint32_t *ana_group; 321 }; 322 323 static int 324 subsystem_cmp(struct spdk_nvmf_subsystem *subsystem1, struct spdk_nvmf_subsystem *subsystem2) 325 { 326 return strncmp(subsystem1->subnqn, subsystem2->subnqn, sizeof(subsystem1->subnqn)); 327 } 328 329 RB_GENERATE_STATIC(subsystem_tree, spdk_nvmf_subsystem, link, subsystem_cmp); 330 331 int nvmf_poll_group_update_subsystem(struct spdk_nvmf_poll_group *group, 332 struct spdk_nvmf_subsystem *subsystem); 333 int nvmf_poll_group_add_subsystem(struct spdk_nvmf_poll_group *group, 334 struct spdk_nvmf_subsystem *subsystem, 335 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg); 336 void nvmf_poll_group_remove_subsystem(struct spdk_nvmf_poll_group *group, 337 struct spdk_nvmf_subsystem *subsystem, spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg); 338 void nvmf_poll_group_pause_subsystem(struct spdk_nvmf_poll_group *group, 339 struct spdk_nvmf_subsystem *subsystem, 340 uint32_t nsid, 341 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg); 342 void nvmf_poll_group_resume_subsystem(struct spdk_nvmf_poll_group *group, 343 struct spdk_nvmf_subsystem *subsystem, spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg); 344 345 void nvmf_update_discovery_log(struct spdk_nvmf_tgt *tgt, const char *hostnqn); 346 void nvmf_get_discovery_log_page(struct spdk_nvmf_tgt *tgt, const char *hostnqn, struct iovec *iov, 347 uint32_t iovcnt, uint64_t offset, uint32_t length, 348 struct spdk_nvme_transport_id *cmd_source_trid); 349 350 void nvmf_ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr); 351 int nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req); 352 int nvmf_ctrlr_process_io_cmd(struct spdk_nvmf_request *req); 353 bool nvmf_ctrlr_dsm_supported(struct spdk_nvmf_ctrlr *ctrlr); 354 bool nvmf_ctrlr_write_zeroes_supported(struct spdk_nvmf_ctrlr *ctrlr); 355 bool nvmf_ctrlr_copy_supported(struct spdk_nvmf_ctrlr *ctrlr); 356 void nvmf_ctrlr_ns_changed(struct spdk_nvmf_ctrlr *ctrlr, uint32_t nsid); 357 bool nvmf_ctrlr_use_zcopy(struct spdk_nvmf_request *req); 358 359 void nvmf_bdev_ctrlr_identify_ns(struct spdk_nvmf_ns *ns, struct spdk_nvme_ns_data *nsdata, 360 bool dif_insert_or_strip); 361 int nvmf_bdev_ctrlr_read_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 362 struct spdk_io_channel *ch, struct spdk_nvmf_request *req); 363 int nvmf_bdev_ctrlr_write_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 364 struct spdk_io_channel *ch, struct spdk_nvmf_request *req); 365 int nvmf_bdev_ctrlr_compare_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 366 struct spdk_io_channel *ch, struct spdk_nvmf_request *req); 367 int nvmf_bdev_ctrlr_compare_and_write_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 368 struct spdk_io_channel *ch, struct spdk_nvmf_request *cmp_req, struct spdk_nvmf_request *write_req); 369 int nvmf_bdev_ctrlr_write_zeroes_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 370 struct spdk_io_channel *ch, struct spdk_nvmf_request *req); 371 int nvmf_bdev_ctrlr_flush_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 372 struct spdk_io_channel *ch, struct spdk_nvmf_request *req); 373 int nvmf_bdev_ctrlr_dsm_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 374 struct spdk_io_channel *ch, struct spdk_nvmf_request *req); 375 int nvmf_bdev_ctrlr_copy_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 376 struct spdk_io_channel *ch, struct spdk_nvmf_request *req); 377 int nvmf_bdev_ctrlr_nvme_passthru_io(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 378 struct spdk_io_channel *ch, struct spdk_nvmf_request *req); 379 bool nvmf_bdev_ctrlr_get_dif_ctx(struct spdk_bdev *bdev, struct spdk_nvme_cmd *cmd, 380 struct spdk_dif_ctx *dif_ctx); 381 bool nvmf_bdev_zcopy_enabled(struct spdk_bdev *bdev); 382 383 int nvmf_subsystem_add_ctrlr(struct spdk_nvmf_subsystem *subsystem, 384 struct spdk_nvmf_ctrlr *ctrlr); 385 void nvmf_subsystem_remove_ctrlr(struct spdk_nvmf_subsystem *subsystem, 386 struct spdk_nvmf_ctrlr *ctrlr); 387 void nvmf_subsystem_remove_all_listeners(struct spdk_nvmf_subsystem *subsystem, 388 bool stop); 389 struct spdk_nvmf_ctrlr *nvmf_subsystem_get_ctrlr(struct spdk_nvmf_subsystem *subsystem, 390 uint16_t cntlid); 391 struct spdk_nvmf_subsystem_listener *nvmf_subsystem_find_listener( 392 struct spdk_nvmf_subsystem *subsystem, 393 const struct spdk_nvme_transport_id *trid); 394 struct spdk_nvmf_listener *nvmf_transport_find_listener( 395 struct spdk_nvmf_transport *transport, 396 const struct spdk_nvme_transport_id *trid); 397 void nvmf_transport_dump_opts(struct spdk_nvmf_transport *transport, struct spdk_json_write_ctx *w, 398 bool named); 399 void nvmf_transport_listen_dump_opts(struct spdk_nvmf_transport *transport, 400 const struct spdk_nvme_transport_id *trid, struct spdk_json_write_ctx *w); 401 void nvmf_subsystem_set_ana_state(struct spdk_nvmf_subsystem *subsystem, 402 const struct spdk_nvme_transport_id *trid, 403 enum spdk_nvme_ana_state ana_state, uint32_t anagrpid, 404 spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn, void *cb_arg); 405 bool nvmf_subsystem_get_ana_reporting(struct spdk_nvmf_subsystem *subsystem); 406 407 /** 408 * Sets the controller ID range for a subsystem. 409 * Valid range is [1, 0xFFEF]. 410 * 411 * May only be performed on subsystems in the INACTIVE state. 412 * 413 * \param subsystem Subsystem to modify. 414 * \param min_cntlid Minimum controller ID. 415 * \param max_cntlid Maximum controller ID. 416 * 417 * \return 0 on success, or negated errno value on failure. 418 */ 419 int nvmf_subsystem_set_cntlid_range(struct spdk_nvmf_subsystem *subsystem, 420 uint16_t min_cntlid, uint16_t max_cntlid); 421 422 int nvmf_ctrlr_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr); 423 int nvmf_ctrlr_async_event_ana_change_notice(struct spdk_nvmf_ctrlr *ctrlr); 424 void nvmf_ctrlr_async_event_discovery_log_change_notice(void *ctx); 425 void nvmf_ctrlr_async_event_reservation_notification(struct spdk_nvmf_ctrlr *ctrlr); 426 427 void nvmf_ns_reservation_request(void *ctx); 428 void nvmf_ctrlr_reservation_notice_log(struct spdk_nvmf_ctrlr *ctrlr, 429 struct spdk_nvmf_ns *ns, 430 enum spdk_nvme_reservation_notification_log_page_type type); 431 432 433 /* 434 * Abort zero-copy requests that already got the buffer (received zcopy_start cb), but haven't 435 * started zcopy_end. These requests are kept on the outstanding queue, but are not waiting for a 436 * completion from the bdev layer, so, when a qpair is being disconnected, we need to kick them to 437 * force their completion. 438 */ 439 void nvmf_qpair_abort_pending_zcopy_reqs(struct spdk_nvmf_qpair *qpair); 440 441 /* 442 * Free aer simply frees the rdma resources for the aer without informing the host. 443 * This function should be called when deleting a qpair when one wants to make sure 444 * the qpair is completely empty before freeing the request. The reason we free the 445 * AER without sending a completion is to prevent the host from sending another AER. 446 */ 447 void nvmf_qpair_free_aer(struct spdk_nvmf_qpair *qpair); 448 449 int nvmf_ctrlr_abort_request(struct spdk_nvmf_request *req); 450 451 void nvmf_ctrlr_set_fatal_status(struct spdk_nvmf_ctrlr *ctrlr); 452 453 static inline struct spdk_nvmf_ns * 454 _nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid) 455 { 456 /* NOTE: This implicitly also checks for 0, since 0 - 1 wraps around to UINT32_MAX. */ 457 if (spdk_unlikely(nsid - 1 >= subsystem->max_nsid)) { 458 return NULL; 459 } 460 461 return subsystem->ns[nsid - 1]; 462 } 463 464 static inline bool 465 nvmf_qpair_is_admin_queue(struct spdk_nvmf_qpair *qpair) 466 { 467 return qpair->qid == 0; 468 } 469 470 /** 471 * Initiates a zcopy start operation 472 * 473 * \param bdev The \ref spdk_bdev 474 * \param desc The \ref spdk_bdev_desc 475 * \param ch The \ref spdk_io_channel 476 * \param req The \ref spdk_nvmf_request passed to the bdev for processing 477 * 478 * \return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE if the command was completed immediately or 479 * SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS if the command was submitted and will be 480 * completed asynchronously. Asynchronous completions are notified through 481 * spdk_nvmf_request_complete(). 482 */ 483 int nvmf_bdev_ctrlr_zcopy_start(struct spdk_bdev *bdev, 484 struct spdk_bdev_desc *desc, 485 struct spdk_io_channel *ch, 486 struct spdk_nvmf_request *req); 487 488 /** 489 * Ends a zcopy operation 490 * 491 * \param req The NVMe-oF request 492 * \param commit Flag indicating whether the buffers should be committed 493 */ 494 void nvmf_bdev_ctrlr_zcopy_end(struct spdk_nvmf_request *req, bool commit); 495 496 #endif /* __NVMF_INTERNAL_H__ */ 497