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 enum spdk_nvmf_subsystem_state { 51 SPDK_NVMF_SUBSYSTEM_INACTIVE = 0, 52 SPDK_NVMF_SUBSYSTEM_ACTIVATING, 53 SPDK_NVMF_SUBSYSTEM_ACTIVE, 54 SPDK_NVMF_SUBSYSTEM_PAUSING, 55 SPDK_NVMF_SUBSYSTEM_PAUSED, 56 SPDK_NVMF_SUBSYSTEM_RESUMING, 57 SPDK_NVMF_SUBSYSTEM_DEACTIVATING, 58 }; 59 60 enum spdk_nvmf_qpair_state { 61 SPDK_NVMF_QPAIR_UNINITIALIZED = 0, 62 SPDK_NVMF_QPAIR_ACTIVATING, 63 SPDK_NVMF_QPAIR_ACTIVE, 64 SPDK_NVMF_QPAIR_DEACTIVATING, 65 SPDK_NVMF_QPAIR_ERROR, 66 }; 67 68 typedef void (*spdk_nvmf_state_change_done)(void *cb_arg, int status); 69 70 struct spdk_nvmf_tgt { 71 uint64_t discovery_genctr; 72 73 uint32_t max_subsystems; 74 75 /* Array of subsystem pointers of size max_subsystems indexed by sid */ 76 struct spdk_nvmf_subsystem **subsystems; 77 78 struct spdk_nvmf_discovery_log_page *discovery_log_page; 79 size_t discovery_log_page_size; 80 TAILQ_HEAD(, spdk_nvmf_transport) transports; 81 82 spdk_nvmf_tgt_destroy_done_fn *destroy_cb_fn; 83 void *destroy_cb_arg; 84 }; 85 86 struct spdk_nvmf_host { 87 char *nqn; 88 TAILQ_ENTRY(spdk_nvmf_host) link; 89 }; 90 91 struct spdk_nvmf_listener { 92 struct spdk_nvme_transport_id trid; 93 struct spdk_nvmf_transport *transport; 94 TAILQ_ENTRY(spdk_nvmf_listener) link; 95 }; 96 97 struct spdk_nvmf_transport_poll_group { 98 struct spdk_nvmf_transport *transport; 99 TAILQ_ENTRY(spdk_nvmf_transport_poll_group) link; 100 }; 101 102 struct spdk_nvmf_subsystem_poll_group { 103 /* Array of channels for each namespace indexed by nsid - 1 */ 104 struct spdk_io_channel **channels; 105 uint32_t num_channels; 106 107 enum spdk_nvmf_subsystem_state state; 108 109 TAILQ_HEAD(, spdk_nvmf_request) queued; 110 }; 111 112 struct spdk_nvmf_poll_group { 113 struct spdk_thread *thread; 114 struct spdk_poller *poller; 115 116 TAILQ_HEAD(, spdk_nvmf_transport_poll_group) tgroups; 117 118 /* Array of poll groups indexed by subsystem id (sid) */ 119 struct spdk_nvmf_subsystem_poll_group *sgroups; 120 uint32_t num_sgroups; 121 122 /* All of the queue pairs that belong to this poll group */ 123 TAILQ_HEAD(, spdk_nvmf_qpair) qpairs; 124 }; 125 126 typedef enum _spdk_nvmf_request_exec_status { 127 SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE, 128 SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS, 129 } spdk_nvmf_request_exec_status; 130 131 union nvmf_h2c_msg { 132 struct spdk_nvmf_capsule_cmd nvmf_cmd; 133 struct spdk_nvme_cmd nvme_cmd; 134 struct spdk_nvmf_fabric_prop_set_cmd prop_set_cmd; 135 struct spdk_nvmf_fabric_prop_get_cmd prop_get_cmd; 136 struct spdk_nvmf_fabric_connect_cmd connect_cmd; 137 }; 138 SPDK_STATIC_ASSERT(sizeof(union nvmf_h2c_msg) == 64, "Incorrect size"); 139 140 union nvmf_c2h_msg { 141 struct spdk_nvme_cpl nvme_cpl; 142 struct spdk_nvmf_fabric_prop_get_rsp prop_get_rsp; 143 struct spdk_nvmf_fabric_connect_rsp connect_rsp; 144 }; 145 SPDK_STATIC_ASSERT(sizeof(union nvmf_c2h_msg) == 16, "Incorrect size"); 146 147 struct spdk_nvmf_request { 148 struct spdk_nvmf_qpair *qpair; 149 uint32_t length; 150 enum spdk_nvme_data_transfer xfer; 151 void *data; 152 union nvmf_h2c_msg *cmd; 153 union nvmf_c2h_msg *rsp; 154 struct iovec iov[SPDK_NVMF_MAX_SGL_ENTRIES * 2]; 155 uint32_t iovcnt; 156 struct spdk_bdev_io_wait_entry bdev_io_wait; 157 158 TAILQ_ENTRY(spdk_nvmf_request) link; 159 }; 160 161 struct spdk_nvmf_ns { 162 struct spdk_nvmf_subsystem *subsystem; 163 struct spdk_bdev *bdev; 164 struct spdk_bdev_desc *desc; 165 struct spdk_nvmf_ns_opts opts; 166 }; 167 168 struct spdk_nvmf_qpair { 169 enum spdk_nvmf_qpair_state state; 170 spdk_nvmf_state_change_done state_cb; 171 void *state_cb_arg; 172 173 struct spdk_nvmf_transport *transport; 174 struct spdk_nvmf_ctrlr *ctrlr; 175 struct spdk_nvmf_poll_group *group; 176 177 uint16_t qid; 178 uint16_t sq_head; 179 uint16_t sq_head_max; 180 181 TAILQ_HEAD(, spdk_nvmf_request) outstanding; 182 TAILQ_ENTRY(spdk_nvmf_qpair) link; 183 }; 184 185 struct spdk_nvmf_ctrlr_feat { 186 union spdk_nvme_feat_arbitration arbitration; 187 union spdk_nvme_feat_power_management power_management; 188 union spdk_nvme_feat_error_recovery error_recovery; 189 union spdk_nvme_feat_volatile_write_cache volatile_write_cache; 190 union spdk_nvme_feat_number_of_queues number_of_queues; 191 union spdk_nvme_feat_write_atomicity write_atomicity; 192 union spdk_nvme_feat_async_event_configuration async_event_configuration; 193 union spdk_nvme_feat_keep_alive_timer keep_alive_timer; 194 }; 195 196 /* 197 * This structure represents an NVMe-oF controller, 198 * which is like a "session" in networking terms. 199 */ 200 struct spdk_nvmf_ctrlr { 201 uint16_t cntlid; 202 struct spdk_nvmf_subsystem *subsys; 203 204 struct { 205 union spdk_nvme_cap_register cap; 206 union spdk_nvme_vs_register vs; 207 union spdk_nvme_cc_register cc; 208 union spdk_nvme_csts_register csts; 209 } vcprop; /* virtual controller properties */ 210 211 struct spdk_nvmf_ctrlr_feat feat; 212 213 struct spdk_nvmf_qpair *admin_qpair; 214 struct spdk_thread *thread; 215 struct spdk_bit_array *qpair_mask; 216 217 struct spdk_nvmf_request *aer_req; 218 union spdk_nvme_async_event_completion notice_event; 219 uint8_t hostid[16]; 220 221 uint16_t changed_ns_list_count; 222 struct spdk_nvme_ns_list changed_ns_list; 223 224 TAILQ_ENTRY(spdk_nvmf_ctrlr) link; 225 }; 226 227 struct spdk_nvmf_subsystem { 228 struct spdk_thread *thread; 229 uint32_t id; 230 enum spdk_nvmf_subsystem_state state; 231 232 char subnqn[SPDK_NVMF_NQN_MAX_LEN + 1]; 233 enum spdk_nvmf_subtype subtype; 234 uint16_t next_cntlid; 235 bool allow_any_host; 236 237 struct spdk_nvmf_tgt *tgt; 238 239 char sn[SPDK_NVME_CTRLR_SN_LEN + 1]; 240 241 /* Array of pointers to namespaces of size max_nsid indexed by nsid - 1 */ 242 struct spdk_nvmf_ns **ns; 243 uint32_t max_nsid; 244 /* This is the maximum allowed nsid to a subsystem */ 245 uint32_t max_allowed_nsid; 246 247 TAILQ_HEAD(, spdk_nvmf_ctrlr) ctrlrs; 248 249 TAILQ_HEAD(, spdk_nvmf_host) hosts; 250 251 TAILQ_HEAD(, spdk_nvmf_listener) listeners; 252 253 TAILQ_ENTRY(spdk_nvmf_subsystem) entries; 254 }; 255 256 typedef void(*spdk_nvmf_poll_group_mod_done)(void *cb_arg, int status); 257 258 struct spdk_nvmf_transport *spdk_nvmf_tgt_get_transport(struct spdk_nvmf_tgt *tgt, 259 enum spdk_nvme_transport_type); 260 261 int spdk_nvmf_poll_group_add_transport(struct spdk_nvmf_poll_group *group, 262 struct spdk_nvmf_transport *transport); 263 int spdk_nvmf_poll_group_update_subsystem(struct spdk_nvmf_poll_group *group, 264 struct spdk_nvmf_subsystem *subsystem); 265 int spdk_nvmf_poll_group_add_subsystem(struct spdk_nvmf_poll_group *group, 266 struct spdk_nvmf_subsystem *subsystem, 267 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg); 268 void spdk_nvmf_poll_group_remove_subsystem(struct spdk_nvmf_poll_group *group, 269 struct spdk_nvmf_subsystem *subsystem, spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg); 270 void spdk_nvmf_poll_group_pause_subsystem(struct spdk_nvmf_poll_group *group, 271 struct spdk_nvmf_subsystem *subsystem, spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg); 272 void spdk_nvmf_poll_group_resume_subsystem(struct spdk_nvmf_poll_group *group, 273 struct spdk_nvmf_subsystem *subsystem, spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg); 274 void spdk_nvmf_request_exec(struct spdk_nvmf_request *req); 275 int spdk_nvmf_request_free(struct spdk_nvmf_request *req); 276 int spdk_nvmf_request_complete(struct spdk_nvmf_request *req); 277 278 void spdk_nvmf_get_discovery_log_page(struct spdk_nvmf_tgt *tgt, 279 void *buffer, uint64_t offset, 280 uint32_t length); 281 282 void spdk_nvmf_ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr); 283 int spdk_nvmf_ctrlr_process_fabrics_cmd(struct spdk_nvmf_request *req); 284 int spdk_nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req); 285 int spdk_nvmf_ctrlr_process_io_cmd(struct spdk_nvmf_request *req); 286 bool spdk_nvmf_ctrlr_dsm_supported(struct spdk_nvmf_ctrlr *ctrlr); 287 bool spdk_nvmf_ctrlr_write_zeroes_supported(struct spdk_nvmf_ctrlr *ctrlr); 288 void spdk_nvmf_ctrlr_ns_changed(struct spdk_nvmf_ctrlr *ctrlr, uint32_t nsid); 289 290 void spdk_nvmf_bdev_ctrlr_identify_ns(struct spdk_nvmf_ns *ns, struct spdk_nvme_ns_data *nsdata); 291 292 int spdk_nvmf_subsystem_add_ctrlr(struct spdk_nvmf_subsystem *subsystem, 293 struct spdk_nvmf_ctrlr *ctrlr); 294 void spdk_nvmf_subsystem_remove_ctrlr(struct spdk_nvmf_subsystem *subsystem, 295 struct spdk_nvmf_ctrlr *ctrlr); 296 struct spdk_nvmf_ctrlr *spdk_nvmf_subsystem_get_ctrlr(struct spdk_nvmf_subsystem *subsystem, 297 uint16_t cntlid); 298 int spdk_nvmf_ctrlr_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr); 299 300 /* 301 * Abort aer is sent on a per controller basis and sends a completion for the aer to the host. 302 * This function should be called when attempting to recover in error paths when it is OK for 303 * the host to send a subsequent AER. 304 */ 305 void spdk_nvmf_ctrlr_abort_aer(struct spdk_nvmf_ctrlr *ctrlr); 306 307 /* 308 * Free aer simply frees the rdma resources for the aer without informing the host. 309 * This function should be called when deleting a qpair when one wants to make sure 310 * the qpair is completely empty before freeing the request. The reason we free the 311 * AER without sending a completion is to prevent the host from sending another AER. 312 */ 313 void spdk_nvmf_qpair_free_aer(struct spdk_nvmf_qpair *qpair); 314 315 static inline struct spdk_nvmf_ns * 316 _spdk_nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid) 317 { 318 /* NOTE: This implicitly also checks for 0, since 0 - 1 wraps around to UINT32_MAX. */ 319 if (spdk_unlikely(nsid - 1 >= subsystem->max_nsid)) { 320 return NULL; 321 } 322 323 return subsystem->ns[nsid - 1]; 324 } 325 326 static inline bool 327 spdk_nvmf_qpair_is_admin_queue(struct spdk_nvmf_qpair *qpair) 328 { 329 return qpair->qid == 0; 330 } 331 332 #endif /* __NVMF_INTERNAL_H__ */ 333