1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #ifndef __NVMF_INTERNAL_H__ 35 #define __NVMF_INTERNAL_H__ 36 37 #include "spdk/stdinc.h" 38 39 #include "spdk/likely.h" 40 #include "spdk/nvmf.h" 41 #include "spdk/nvmf_spec.h" 42 #include "spdk/assert.h" 43 #include "spdk/bdev.h" 44 #include "spdk/queue.h" 45 #include "spdk/util.h" 46 #include "spdk/thread.h" 47 48 #define SPDK_NVMF_MAX_SGL_ENTRIES 16 49 50 /* AIO backend requires block size aligned data buffers, 51 * extra 4KiB aligned data buffer should work for most devices. 52 */ 53 #define SHIFT_4KB 12u 54 #define NVMF_DATA_BUFFER_ALIGNMENT (1u << SHIFT_4KB) 55 #define NVMF_DATA_BUFFER_MASK (NVMF_DATA_BUFFER_ALIGNMENT - 1LL) 56 57 enum spdk_nvmf_subsystem_state { 58 SPDK_NVMF_SUBSYSTEM_INACTIVE = 0, 59 SPDK_NVMF_SUBSYSTEM_ACTIVATING, 60 SPDK_NVMF_SUBSYSTEM_ACTIVE, 61 SPDK_NVMF_SUBSYSTEM_PAUSING, 62 SPDK_NVMF_SUBSYSTEM_PAUSED, 63 SPDK_NVMF_SUBSYSTEM_RESUMING, 64 SPDK_NVMF_SUBSYSTEM_DEACTIVATING, 65 }; 66 67 enum spdk_nvmf_qpair_state { 68 SPDK_NVMF_QPAIR_UNINITIALIZED = 0, 69 SPDK_NVMF_QPAIR_ACTIVE, 70 SPDK_NVMF_QPAIR_DEACTIVATING, 71 SPDK_NVMF_QPAIR_ERROR, 72 }; 73 74 typedef void (*spdk_nvmf_state_change_done)(void *cb_arg, int status); 75 76 struct spdk_nvmf_tgt { 77 uint64_t discovery_genctr; 78 79 uint32_t max_subsystems; 80 81 /* Array of subsystem pointers of size max_subsystems indexed by sid */ 82 struct spdk_nvmf_subsystem **subsystems; 83 84 struct spdk_nvmf_discovery_log_page *discovery_log_page; 85 size_t discovery_log_page_size; 86 TAILQ_HEAD(, spdk_nvmf_transport) transports; 87 88 spdk_nvmf_tgt_destroy_done_fn *destroy_cb_fn; 89 void *destroy_cb_arg; 90 }; 91 92 struct spdk_nvmf_host { 93 char *nqn; 94 TAILQ_ENTRY(spdk_nvmf_host) link; 95 }; 96 97 struct spdk_nvmf_listener { 98 struct spdk_nvme_transport_id trid; 99 struct spdk_nvmf_transport *transport; 100 TAILQ_ENTRY(spdk_nvmf_listener) link; 101 }; 102 103 struct spdk_nvmf_transport_pg_cache_buf { 104 STAILQ_ENTRY(spdk_nvmf_transport_pg_cache_buf) link; 105 }; 106 107 struct spdk_nvmf_transport_poll_group { 108 struct spdk_nvmf_transport *transport; 109 STAILQ_HEAD(, spdk_nvmf_transport_pg_cache_buf) buf_cache; 110 uint32_t buf_cache_count; 111 uint32_t buf_cache_size; 112 TAILQ_ENTRY(spdk_nvmf_transport_poll_group) link; 113 }; 114 115 struct spdk_nvmf_subsystem_poll_group { 116 /* Array of channels for each namespace indexed by nsid - 1 */ 117 struct spdk_io_channel **channels; 118 uint32_t num_channels; 119 120 enum spdk_nvmf_subsystem_state state; 121 122 TAILQ_HEAD(, spdk_nvmf_request) queued; 123 }; 124 125 struct spdk_nvmf_poll_group { 126 struct spdk_thread *thread; 127 struct spdk_poller *poller; 128 129 TAILQ_HEAD(, spdk_nvmf_transport_poll_group) tgroups; 130 131 /* Array of poll groups indexed by subsystem id (sid) */ 132 struct spdk_nvmf_subsystem_poll_group *sgroups; 133 uint32_t num_sgroups; 134 135 /* All of the queue pairs that belong to this poll group */ 136 TAILQ_HEAD(, spdk_nvmf_qpair) qpairs; 137 }; 138 139 typedef enum _spdk_nvmf_request_exec_status { 140 SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE, 141 SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS, 142 } spdk_nvmf_request_exec_status; 143 144 union nvmf_h2c_msg { 145 struct spdk_nvmf_capsule_cmd nvmf_cmd; 146 struct spdk_nvme_cmd nvme_cmd; 147 struct spdk_nvmf_fabric_prop_set_cmd prop_set_cmd; 148 struct spdk_nvmf_fabric_prop_get_cmd prop_get_cmd; 149 struct spdk_nvmf_fabric_connect_cmd connect_cmd; 150 }; 151 SPDK_STATIC_ASSERT(sizeof(union nvmf_h2c_msg) == 64, "Incorrect size"); 152 153 union nvmf_c2h_msg { 154 struct spdk_nvme_cpl nvme_cpl; 155 struct spdk_nvmf_fabric_prop_get_rsp prop_get_rsp; 156 struct spdk_nvmf_fabric_connect_rsp connect_rsp; 157 }; 158 SPDK_STATIC_ASSERT(sizeof(union nvmf_c2h_msg) == 16, "Incorrect size"); 159 160 struct spdk_nvmf_request { 161 struct spdk_nvmf_qpair *qpair; 162 uint32_t length; 163 enum spdk_nvme_data_transfer xfer; 164 void *data; 165 union nvmf_h2c_msg *cmd; 166 union nvmf_c2h_msg *rsp; 167 struct iovec iov[SPDK_NVMF_MAX_SGL_ENTRIES * 2]; 168 uint32_t iovcnt; 169 struct spdk_bdev_io_wait_entry bdev_io_wait; 170 171 TAILQ_ENTRY(spdk_nvmf_request) link; 172 }; 173 174 struct spdk_nvmf_ns { 175 uint32_t nsid; 176 struct spdk_nvmf_subsystem *subsystem; 177 struct spdk_bdev *bdev; 178 struct spdk_bdev_desc *desc; 179 struct spdk_nvmf_ns_opts opts; 180 /* reservation notificaton mask */ 181 uint32_t mask; 182 }; 183 184 struct spdk_nvmf_qpair { 185 enum spdk_nvmf_qpair_state state; 186 spdk_nvmf_state_change_done state_cb; 187 void *state_cb_arg; 188 189 struct spdk_nvmf_transport *transport; 190 struct spdk_nvmf_ctrlr *ctrlr; 191 struct spdk_nvmf_poll_group *group; 192 193 uint16_t qid; 194 uint16_t sq_head; 195 uint16_t sq_head_max; 196 197 TAILQ_HEAD(, spdk_nvmf_request) outstanding; 198 TAILQ_ENTRY(spdk_nvmf_qpair) link; 199 }; 200 201 struct spdk_nvmf_ctrlr_feat { 202 union spdk_nvme_feat_arbitration arbitration; 203 union spdk_nvme_feat_power_management power_management; 204 union spdk_nvme_feat_error_recovery error_recovery; 205 union spdk_nvme_feat_volatile_write_cache volatile_write_cache; 206 union spdk_nvme_feat_number_of_queues number_of_queues; 207 union spdk_nvme_feat_write_atomicity write_atomicity; 208 union spdk_nvme_feat_async_event_configuration async_event_configuration; 209 union spdk_nvme_feat_keep_alive_timer keep_alive_timer; 210 }; 211 212 /* 213 * This structure represents an NVMe-oF controller, 214 * which is like a "session" in networking terms. 215 */ 216 struct spdk_nvmf_ctrlr { 217 uint16_t cntlid; 218 struct spdk_nvmf_subsystem *subsys; 219 220 struct { 221 union spdk_nvme_cap_register cap; 222 union spdk_nvme_vs_register vs; 223 union spdk_nvme_cc_register cc; 224 union spdk_nvme_csts_register csts; 225 } vcprop; /* virtual controller properties */ 226 227 struct spdk_nvmf_ctrlr_feat feat; 228 229 struct spdk_nvmf_qpair *admin_qpair; 230 struct spdk_thread *thread; 231 struct spdk_bit_array *qpair_mask; 232 233 struct spdk_nvmf_request *aer_req; 234 union spdk_nvme_async_event_completion notice_event; 235 struct spdk_uuid hostid; 236 237 uint16_t changed_ns_list_count; 238 struct spdk_nvme_ns_list changed_ns_list; 239 240 /* Time to trigger keep-alive--poller_time = now_tick + period */ 241 uint64_t last_keep_alive_tick; 242 struct spdk_poller *keep_alive_poller; 243 244 TAILQ_ENTRY(spdk_nvmf_ctrlr) link; 245 }; 246 247 struct spdk_nvmf_subsystem { 248 struct spdk_thread *thread; 249 uint32_t id; 250 enum spdk_nvmf_subsystem_state state; 251 252 char subnqn[SPDK_NVMF_NQN_MAX_LEN + 1]; 253 enum spdk_nvmf_subtype subtype; 254 uint16_t next_cntlid; 255 bool allow_any_host; 256 257 struct spdk_nvmf_tgt *tgt; 258 259 char sn[SPDK_NVME_CTRLR_SN_LEN + 1]; 260 261 /* Array of pointers to namespaces of size max_nsid indexed by nsid - 1 */ 262 struct spdk_nvmf_ns **ns; 263 uint32_t max_nsid; 264 /* This is the maximum allowed nsid to a subsystem */ 265 uint32_t max_allowed_nsid; 266 267 TAILQ_HEAD(, spdk_nvmf_ctrlr) ctrlrs; 268 269 TAILQ_HEAD(, spdk_nvmf_host) hosts; 270 271 TAILQ_HEAD(, spdk_nvmf_listener) listeners; 272 273 TAILQ_ENTRY(spdk_nvmf_subsystem) entries; 274 }; 275 276 typedef void(*spdk_nvmf_poll_group_mod_done)(void *cb_arg, int status); 277 278 struct spdk_nvmf_transport *spdk_nvmf_tgt_get_transport(struct spdk_nvmf_tgt *tgt, 279 enum spdk_nvme_transport_type); 280 281 int spdk_nvmf_poll_group_add_transport(struct spdk_nvmf_poll_group *group, 282 struct spdk_nvmf_transport *transport); 283 int spdk_nvmf_poll_group_update_subsystem(struct spdk_nvmf_poll_group *group, 284 struct spdk_nvmf_subsystem *subsystem); 285 int spdk_nvmf_poll_group_add_subsystem(struct spdk_nvmf_poll_group *group, 286 struct spdk_nvmf_subsystem *subsystem, 287 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg); 288 void spdk_nvmf_poll_group_remove_subsystem(struct spdk_nvmf_poll_group *group, 289 struct spdk_nvmf_subsystem *subsystem, spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg); 290 void spdk_nvmf_poll_group_pause_subsystem(struct spdk_nvmf_poll_group *group, 291 struct spdk_nvmf_subsystem *subsystem, spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg); 292 void spdk_nvmf_poll_group_resume_subsystem(struct spdk_nvmf_poll_group *group, 293 struct spdk_nvmf_subsystem *subsystem, spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg); 294 void spdk_nvmf_request_exec(struct spdk_nvmf_request *req); 295 int spdk_nvmf_request_free(struct spdk_nvmf_request *req); 296 int spdk_nvmf_request_complete(struct spdk_nvmf_request *req); 297 298 void spdk_nvmf_get_discovery_log_page(struct spdk_nvmf_tgt *tgt, struct iovec *iov, 299 uint32_t iovcnt, uint64_t offset, uint32_t length); 300 301 void spdk_nvmf_ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr); 302 int spdk_nvmf_ctrlr_process_fabrics_cmd(struct spdk_nvmf_request *req); 303 int spdk_nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req); 304 int spdk_nvmf_ctrlr_process_io_cmd(struct spdk_nvmf_request *req); 305 bool spdk_nvmf_ctrlr_dsm_supported(struct spdk_nvmf_ctrlr *ctrlr); 306 bool spdk_nvmf_ctrlr_write_zeroes_supported(struct spdk_nvmf_ctrlr *ctrlr); 307 void spdk_nvmf_ctrlr_ns_changed(struct spdk_nvmf_ctrlr *ctrlr, uint32_t nsid); 308 309 void spdk_nvmf_bdev_ctrlr_identify_ns(struct spdk_nvmf_ns *ns, struct spdk_nvme_ns_data *nsdata); 310 int spdk_nvmf_bdev_ctrlr_read_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 311 struct spdk_io_channel *ch, struct spdk_nvmf_request *req); 312 int spdk_nvmf_bdev_ctrlr_write_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 313 struct spdk_io_channel *ch, struct spdk_nvmf_request *req); 314 int spdk_nvmf_bdev_ctrlr_write_zeroes_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 315 struct spdk_io_channel *ch, struct spdk_nvmf_request *req); 316 int spdk_nvmf_bdev_ctrlr_flush_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 317 struct spdk_io_channel *ch, struct spdk_nvmf_request *req); 318 int spdk_nvmf_bdev_ctrlr_dsm_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 319 struct spdk_io_channel *ch, struct spdk_nvmf_request *req); 320 int spdk_nvmf_bdev_ctrlr_nvme_passthru_io(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 321 struct spdk_io_channel *ch, struct spdk_nvmf_request *req); 322 323 int spdk_nvmf_subsystem_add_ctrlr(struct spdk_nvmf_subsystem *subsystem, 324 struct spdk_nvmf_ctrlr *ctrlr); 325 void spdk_nvmf_subsystem_remove_ctrlr(struct spdk_nvmf_subsystem *subsystem, 326 struct spdk_nvmf_ctrlr *ctrlr); 327 struct spdk_nvmf_ctrlr *spdk_nvmf_subsystem_get_ctrlr(struct spdk_nvmf_subsystem *subsystem, 328 uint16_t cntlid); 329 int spdk_nvmf_ctrlr_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr); 330 331 /* 332 * Abort aer is sent on a per controller basis and sends a completion for the aer to the host. 333 * This function should be called when attempting to recover in error paths when it is OK for 334 * the host to send a subsequent AER. 335 */ 336 void spdk_nvmf_ctrlr_abort_aer(struct spdk_nvmf_ctrlr *ctrlr); 337 338 /* 339 * Free aer simply frees the rdma resources for the aer without informing the host. 340 * This function should be called when deleting a qpair when one wants to make sure 341 * the qpair is completely empty before freeing the request. The reason we free the 342 * AER without sending a completion is to prevent the host from sending another AER. 343 */ 344 void spdk_nvmf_qpair_free_aer(struct spdk_nvmf_qpair *qpair); 345 346 static inline struct spdk_nvmf_ns * 347 _spdk_nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid) 348 { 349 /* NOTE: This implicitly also checks for 0, since 0 - 1 wraps around to UINT32_MAX. */ 350 if (spdk_unlikely(nsid - 1 >= subsystem->max_nsid)) { 351 return NULL; 352 } 353 354 return subsystem->ns[nsid - 1]; 355 } 356 357 static inline bool 358 spdk_nvmf_qpair_is_admin_queue(struct spdk_nvmf_qpair *qpair) 359 { 360 return qpair->qid == 0; 361 } 362 363 #endif /* __NVMF_INTERNAL_H__ */ 364