1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. All rights reserved. 5 * Copyright (c) 2019, 2020 Mellanox Technologies LTD. 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 #include "spdk/stdinc.h" 35 36 #include "nvmf_internal.h" 37 #include "transport.h" 38 39 #include "spdk/bit_array.h" 40 #include "spdk/endian.h" 41 #include "spdk/thread.h" 42 #include "spdk/trace.h" 43 #include "spdk/nvme_spec.h" 44 #include "spdk/nvmf_cmd.h" 45 #include "spdk/string.h" 46 #include "spdk/util.h" 47 #include "spdk/version.h" 48 49 #include "spdk_internal/log.h" 50 51 #define MIN_KEEP_ALIVE_TIMEOUT_IN_MS 10000 52 #define NVMF_DISC_KATO_IN_MS 120000 53 #define KAS_TIME_UNIT_IN_MS 100 54 #define KAS_DEFAULT_VALUE (MIN_KEEP_ALIVE_TIMEOUT_IN_MS / KAS_TIME_UNIT_IN_MS) 55 56 /* 57 * Report the SPDK version as the firmware revision. 58 * SPDK_VERSION_STRING won't fit into FR (only 8 bytes), so try to fit the most important parts. 59 */ 60 #define FW_VERSION SPDK_VERSION_MAJOR_STRING SPDK_VERSION_MINOR_STRING SPDK_VERSION_PATCH_STRING 61 62 /* 63 * Support for custom admin command handlers 64 */ 65 struct spdk_nvmf_custom_admin_cmd { 66 spdk_nvmf_custom_cmd_hdlr hdlr; 67 uint32_t nsid; /* nsid to forward */ 68 }; 69 70 static struct spdk_nvmf_custom_admin_cmd g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_MAX_OPC + 1]; 71 72 static inline void 73 spdk_nvmf_invalid_connect_response(struct spdk_nvmf_fabric_connect_rsp *rsp, 74 uint8_t iattr, uint16_t ipo) 75 { 76 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 77 rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 78 rsp->status_code_specific.invalid.iattr = iattr; 79 rsp->status_code_specific.invalid.ipo = ipo; 80 } 81 82 #define SPDK_NVMF_INVALID_CONNECT_CMD(rsp, field) \ 83 spdk_nvmf_invalid_connect_response(rsp, 0, offsetof(struct spdk_nvmf_fabric_connect_cmd, field)) 84 #define SPDK_NVMF_INVALID_CONNECT_DATA(rsp, field) \ 85 spdk_nvmf_invalid_connect_response(rsp, 1, offsetof(struct spdk_nvmf_fabric_connect_data, field)) 86 87 static void 88 spdk_nvmf_ctrlr_stop_keep_alive_timer(struct spdk_nvmf_ctrlr *ctrlr) 89 { 90 if (!ctrlr) { 91 SPDK_ERRLOG("Controller is NULL\n"); 92 return; 93 } 94 95 if (ctrlr->keep_alive_poller == NULL) { 96 return; 97 } 98 99 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Stop keep alive poller\n"); 100 spdk_poller_unregister(&ctrlr->keep_alive_poller); 101 } 102 103 static void 104 spdk_nvmf_ctrlr_disconnect_qpairs_done(struct spdk_io_channel_iter *i, int status) 105 { 106 if (status == 0) { 107 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ctrlr disconnect qpairs complete successfully\n"); 108 } else { 109 SPDK_ERRLOG("Fail to disconnect ctrlr qpairs\n"); 110 } 111 } 112 113 static int 114 _spdk_nvmf_ctrlr_disconnect_qpairs_on_pg(struct spdk_io_channel_iter *i, bool include_admin) 115 { 116 int rc = 0; 117 struct spdk_nvmf_ctrlr *ctrlr; 118 struct spdk_nvmf_qpair *qpair, *temp_qpair; 119 struct spdk_io_channel *ch; 120 struct spdk_nvmf_poll_group *group; 121 122 ctrlr = spdk_io_channel_iter_get_ctx(i); 123 ch = spdk_io_channel_iter_get_channel(i); 124 group = spdk_io_channel_get_ctx(ch); 125 126 TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, temp_qpair) { 127 if (qpair->ctrlr == ctrlr && (include_admin || !spdk_nvmf_qpair_is_admin_queue(qpair))) { 128 rc = spdk_nvmf_qpair_disconnect(qpair, NULL, NULL); 129 if (rc) { 130 SPDK_ERRLOG("Qpair disconnect failed\n"); 131 return rc; 132 } 133 } 134 } 135 136 return rc; 137 } 138 139 static void 140 spdk_nvmf_ctrlr_disconnect_qpairs_on_pg(struct spdk_io_channel_iter *i) 141 { 142 spdk_for_each_channel_continue(i, _spdk_nvmf_ctrlr_disconnect_qpairs_on_pg(i, true)); 143 } 144 145 static void 146 spdk_nvmf_ctrlr_disconnect_io_qpairs_on_pg(struct spdk_io_channel_iter *i) 147 { 148 spdk_for_each_channel_continue(i, _spdk_nvmf_ctrlr_disconnect_qpairs_on_pg(i, false)); 149 } 150 151 static int 152 spdk_nvmf_ctrlr_keep_alive_poll(void *ctx) 153 { 154 uint64_t keep_alive_timeout_tick; 155 uint64_t now = spdk_get_ticks(); 156 struct spdk_nvmf_ctrlr *ctrlr = ctx; 157 158 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Polling ctrlr keep alive timeout\n"); 159 160 /* If the Keep alive feature is in use and the timer expires */ 161 keep_alive_timeout_tick = ctrlr->last_keep_alive_tick + 162 ctrlr->feat.keep_alive_timer.bits.kato * spdk_get_ticks_hz() / UINT64_C(1000); 163 if (now > keep_alive_timeout_tick) { 164 SPDK_NOTICELOG("Disconnecting host from subsystem %s due to keep alive timeout.\n", 165 ctrlr->subsys->subnqn); 166 /* set the Controller Fatal Status bit to '1' */ 167 if (ctrlr->vcprop.csts.bits.cfs == 0) { 168 ctrlr->vcprop.csts.bits.cfs = 1; 169 170 /* 171 * disconnect qpairs, terminate Transport connection 172 * destroy ctrlr, break the host to controller association 173 * disconnect qpairs with qpair->ctrlr == ctrlr 174 */ 175 spdk_for_each_channel(ctrlr->subsys->tgt, 176 spdk_nvmf_ctrlr_disconnect_qpairs_on_pg, 177 ctrlr, 178 spdk_nvmf_ctrlr_disconnect_qpairs_done); 179 } 180 } 181 182 return 1; 183 } 184 185 static void 186 spdk_nvmf_ctrlr_start_keep_alive_timer(struct spdk_nvmf_ctrlr *ctrlr) 187 { 188 if (!ctrlr) { 189 SPDK_ERRLOG("Controller is NULL\n"); 190 return; 191 } 192 193 /* if cleared to 0 then the Keep Alive Timer is disabled */ 194 if (ctrlr->feat.keep_alive_timer.bits.kato != 0) { 195 196 ctrlr->last_keep_alive_tick = spdk_get_ticks(); 197 198 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Ctrlr add keep alive poller\n"); 199 ctrlr->keep_alive_poller = SPDK_POLLER_REGISTER(spdk_nvmf_ctrlr_keep_alive_poll, ctrlr, 200 ctrlr->feat.keep_alive_timer.bits.kato * 1000); 201 } 202 } 203 204 static void 205 ctrlr_add_qpair_and_update_rsp(struct spdk_nvmf_qpair *qpair, 206 struct spdk_nvmf_ctrlr *ctrlr, 207 struct spdk_nvmf_fabric_connect_rsp *rsp) 208 { 209 assert(ctrlr->admin_qpair->group->thread == spdk_get_thread()); 210 211 /* check if we would exceed ctrlr connection limit */ 212 if (qpair->qid >= spdk_bit_array_capacity(ctrlr->qpair_mask)) { 213 SPDK_ERRLOG("Requested QID %u but Max QID is %u\n", 214 qpair->qid, spdk_bit_array_capacity(ctrlr->qpair_mask) - 1); 215 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 216 rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER; 217 return; 218 } 219 220 if (spdk_bit_array_get(ctrlr->qpair_mask, qpair->qid)) { 221 SPDK_ERRLOG("Got I/O connect with duplicate QID %u\n", qpair->qid); 222 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 223 rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER; 224 return; 225 } 226 227 qpair->ctrlr = ctrlr; 228 spdk_bit_array_set(ctrlr->qpair_mask, qpair->qid); 229 230 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 231 rsp->status_code_specific.success.cntlid = ctrlr->cntlid; 232 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "connect capsule response: cntlid = 0x%04x\n", 233 rsp->status_code_specific.success.cntlid); 234 } 235 236 static void 237 _spdk_nvmf_request_complete(void *ctx) 238 { 239 struct spdk_nvmf_request *req = ctx; 240 241 spdk_nvmf_request_complete(req); 242 } 243 244 static void 245 _spdk_nvmf_ctrlr_add_admin_qpair(void *ctx) 246 { 247 struct spdk_nvmf_request *req = ctx; 248 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 249 struct spdk_nvmf_qpair *qpair = req->qpair; 250 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 251 252 ctrlr->admin_qpair = qpair; 253 spdk_nvmf_ctrlr_start_keep_alive_timer(ctrlr); 254 ctrlr_add_qpair_and_update_rsp(qpair, ctrlr, rsp); 255 spdk_nvmf_request_complete(req); 256 } 257 258 static void 259 _spdk_nvmf_subsystem_add_ctrlr(void *ctx) 260 { 261 struct spdk_nvmf_request *req = ctx; 262 struct spdk_nvmf_qpair *qpair = req->qpair; 263 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 264 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 265 266 if (spdk_nvmf_subsystem_add_ctrlr(ctrlr->subsys, ctrlr)) { 267 SPDK_ERRLOG("Unable to add controller to subsystem\n"); 268 spdk_bit_array_free(&ctrlr->qpair_mask); 269 free(ctrlr); 270 qpair->ctrlr = NULL; 271 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 272 spdk_thread_send_msg(qpair->group->thread, _spdk_nvmf_request_complete, req); 273 return; 274 } 275 276 spdk_thread_send_msg(ctrlr->thread, _spdk_nvmf_ctrlr_add_admin_qpair, req); 277 } 278 279 static struct spdk_nvmf_ctrlr * 280 spdk_nvmf_ctrlr_create(struct spdk_nvmf_subsystem *subsystem, 281 struct spdk_nvmf_request *req, 282 struct spdk_nvmf_fabric_connect_cmd *connect_cmd, 283 struct spdk_nvmf_fabric_connect_data *connect_data) 284 { 285 struct spdk_nvmf_ctrlr *ctrlr; 286 struct spdk_nvmf_transport *transport; 287 288 ctrlr = calloc(1, sizeof(*ctrlr)); 289 if (ctrlr == NULL) { 290 SPDK_ERRLOG("Memory allocation failed\n"); 291 return NULL; 292 } 293 294 TAILQ_INIT(&ctrlr->log_head); 295 ctrlr->subsys = subsystem; 296 ctrlr->thread = req->qpair->group->thread; 297 298 transport = req->qpair->transport; 299 ctrlr->qpair_mask = spdk_bit_array_create(transport->opts.max_qpairs_per_ctrlr); 300 if (!ctrlr->qpair_mask) { 301 SPDK_ERRLOG("Failed to allocate controller qpair mask\n"); 302 free(ctrlr); 303 return NULL; 304 } 305 306 /* 307 * KAS: this field indicates the granularity of the Keep Alive Timer in 100ms units 308 * keep-alive timeout in milliseconds 309 */ 310 ctrlr->feat.keep_alive_timer.bits.kato = spdk_divide_round_up(connect_cmd->kato, 311 KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS) * 312 KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS; 313 ctrlr->feat.async_event_configuration.bits.ns_attr_notice = 1; 314 ctrlr->feat.volatile_write_cache.bits.wce = 1; 315 316 if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 317 /* 318 * If keep-alive timeout is not set, discovery controllers use some 319 * arbitrary high value in order to cleanup stale discovery sessions 320 * 321 * From the 1.0a nvme-of spec: 322 * "The Keep Alive command is reserved for 323 * Discovery controllers. A transport may specify a 324 * fixed Discovery controller activity timeout value 325 * (e.g., 2 minutes). If no commands are received 326 * by a Discovery controller within that time 327 * period, the controller may perform the 328 * actions for Keep Alive Timer expiration". 329 * kato is in millisecond. 330 */ 331 if (ctrlr->feat.keep_alive_timer.bits.kato == 0) { 332 ctrlr->feat.keep_alive_timer.bits.kato = NVMF_DISC_KATO_IN_MS; 333 } 334 } 335 336 /* Subtract 1 for admin queue, 1 for 0's based */ 337 ctrlr->feat.number_of_queues.bits.ncqr = transport->opts.max_qpairs_per_ctrlr - 1 - 338 1; 339 ctrlr->feat.number_of_queues.bits.nsqr = transport->opts.max_qpairs_per_ctrlr - 1 - 340 1; 341 342 spdk_uuid_copy(&ctrlr->hostid, (struct spdk_uuid *)connect_data->hostid); 343 memcpy(ctrlr->hostnqn, connect_data->hostnqn, sizeof(ctrlr->hostnqn)); 344 345 ctrlr->vcprop.cap.raw = 0; 346 ctrlr->vcprop.cap.bits.cqr = 1; /* NVMe-oF specification required */ 347 ctrlr->vcprop.cap.bits.mqes = transport->opts.max_queue_depth - 348 1; /* max queue depth */ 349 ctrlr->vcprop.cap.bits.ams = 0; /* optional arb mechanisms */ 350 ctrlr->vcprop.cap.bits.to = 1; /* ready timeout - 500 msec units */ 351 ctrlr->vcprop.cap.bits.dstrd = 0; /* fixed to 0 for NVMe-oF */ 352 ctrlr->vcprop.cap.bits.css = SPDK_NVME_CAP_CSS_NVM; /* NVM command set */ 353 ctrlr->vcprop.cap.bits.mpsmin = 0; /* 2 ^ (12 + mpsmin) == 4k */ 354 ctrlr->vcprop.cap.bits.mpsmax = 0; /* 2 ^ (12 + mpsmax) == 4k */ 355 356 /* Version Supported: 1.3 */ 357 ctrlr->vcprop.vs.bits.mjr = 1; 358 ctrlr->vcprop.vs.bits.mnr = 3; 359 ctrlr->vcprop.vs.bits.ter = 0; 360 361 ctrlr->vcprop.cc.raw = 0; 362 ctrlr->vcprop.cc.bits.en = 0; /* Init controller disabled */ 363 364 ctrlr->vcprop.csts.raw = 0; 365 ctrlr->vcprop.csts.bits.rdy = 0; /* Init controller as not ready */ 366 367 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cap 0x%" PRIx64 "\n", ctrlr->vcprop.cap.raw); 368 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "vs 0x%x\n", ctrlr->vcprop.vs.raw); 369 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cc 0x%x\n", ctrlr->vcprop.cc.raw); 370 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "csts 0x%x\n", ctrlr->vcprop.csts.raw); 371 372 ctrlr->dif_insert_or_strip = transport->opts.dif_insert_or_strip; 373 374 req->qpair->ctrlr = ctrlr; 375 spdk_thread_send_msg(subsystem->thread, _spdk_nvmf_subsystem_add_ctrlr, req); 376 377 return ctrlr; 378 } 379 380 static void 381 _spdk_nvmf_ctrlr_destruct(void *ctx) 382 { 383 struct spdk_nvmf_ctrlr *ctrlr = ctx; 384 struct spdk_nvmf_reservation_log *log, *log_tmp; 385 386 spdk_nvmf_ctrlr_stop_keep_alive_timer(ctrlr); 387 388 TAILQ_FOREACH_SAFE(log, &ctrlr->log_head, link, log_tmp) { 389 TAILQ_REMOVE(&ctrlr->log_head, log, link); 390 free(log); 391 } 392 free(ctrlr); 393 } 394 395 void 396 spdk_nvmf_ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr) 397 { 398 spdk_nvmf_subsystem_remove_ctrlr(ctrlr->subsys, ctrlr); 399 400 spdk_thread_send_msg(ctrlr->thread, _spdk_nvmf_ctrlr_destruct, ctrlr); 401 } 402 403 static void 404 spdk_nvmf_ctrlr_add_io_qpair(void *ctx) 405 { 406 struct spdk_nvmf_request *req = ctx; 407 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 408 struct spdk_nvmf_qpair *qpair = req->qpair; 409 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 410 411 /* Unit test will check qpair->ctrlr after calling spdk_nvmf_ctrlr_connect. 412 * For error case, the value should be NULL. So set it to NULL at first. 413 */ 414 qpair->ctrlr = NULL; 415 416 if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 417 SPDK_ERRLOG("I/O connect not allowed on discovery controller\n"); 418 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 419 goto end; 420 } 421 422 if (!ctrlr->vcprop.cc.bits.en) { 423 SPDK_ERRLOG("Got I/O connect before ctrlr was enabled\n"); 424 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 425 goto end; 426 } 427 428 if (1u << ctrlr->vcprop.cc.bits.iosqes != sizeof(struct spdk_nvme_cmd)) { 429 SPDK_ERRLOG("Got I/O connect with invalid IOSQES %u\n", 430 ctrlr->vcprop.cc.bits.iosqes); 431 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 432 goto end; 433 } 434 435 if (1u << ctrlr->vcprop.cc.bits.iocqes != sizeof(struct spdk_nvme_cpl)) { 436 SPDK_ERRLOG("Got I/O connect with invalid IOCQES %u\n", 437 ctrlr->vcprop.cc.bits.iocqes); 438 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 439 goto end; 440 } 441 442 ctrlr_add_qpair_and_update_rsp(qpair, ctrlr, rsp); 443 end: 444 spdk_thread_send_msg(qpair->group->thread, _spdk_nvmf_request_complete, req); 445 } 446 447 static void 448 _spdk_nvmf_ctrlr_add_io_qpair(void *ctx) 449 { 450 struct spdk_nvmf_request *req = ctx; 451 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 452 struct spdk_nvmf_fabric_connect_data *data = req->data; 453 struct spdk_nvmf_ctrlr *ctrlr; 454 struct spdk_nvmf_qpair *qpair = req->qpair; 455 struct spdk_nvmf_qpair *admin_qpair; 456 struct spdk_nvmf_tgt *tgt = qpair->transport->tgt; 457 struct spdk_nvmf_subsystem *subsystem; 458 459 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect I/O Queue for controller id 0x%x\n", data->cntlid); 460 461 subsystem = spdk_nvmf_tgt_find_subsystem(tgt, data->subnqn); 462 /* We already checked this in spdk_nvmf_ctrlr_connect */ 463 assert(subsystem != NULL); 464 465 ctrlr = spdk_nvmf_subsystem_get_ctrlr(subsystem, data->cntlid); 466 if (ctrlr == NULL) { 467 SPDK_ERRLOG("Unknown controller ID 0x%x\n", data->cntlid); 468 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid); 469 spdk_thread_send_msg(qpair->group->thread, _spdk_nvmf_request_complete, req); 470 return; 471 } 472 473 admin_qpair = ctrlr->admin_qpair; 474 qpair->ctrlr = ctrlr; 475 spdk_thread_send_msg(admin_qpair->group->thread, spdk_nvmf_ctrlr_add_io_qpair, req); 476 } 477 478 static bool 479 spdk_nvmf_qpair_access_allowed(struct spdk_nvmf_qpair *qpair, struct spdk_nvmf_subsystem *subsystem, 480 const char *hostnqn) 481 { 482 struct spdk_nvme_transport_id listen_trid = {}; 483 484 if (!spdk_nvmf_subsystem_host_allowed(subsystem, hostnqn)) { 485 SPDK_ERRLOG("Subsystem '%s' does not allow host '%s'\n", subsystem->subnqn, hostnqn); 486 return false; 487 } 488 489 if (spdk_nvmf_qpair_get_listen_trid(qpair, &listen_trid)) { 490 SPDK_ERRLOG("Subsystem '%s' is unable to enforce access control due to an internal error.\n", 491 subsystem->subnqn); 492 return false; 493 } 494 495 if (!spdk_nvmf_subsystem_listener_allowed(subsystem, &listen_trid)) { 496 SPDK_ERRLOG("Subsystem '%s' does not allow host '%s' to connect at this address.\n", 497 subsystem->subnqn, hostnqn); 498 return false; 499 } 500 501 return true; 502 } 503 504 static int 505 _spdk_nvmf_ctrlr_connect(struct spdk_nvmf_request *req) 506 { 507 struct spdk_nvmf_fabric_connect_data *data = req->data; 508 struct spdk_nvmf_fabric_connect_cmd *cmd = &req->cmd->connect_cmd; 509 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 510 struct spdk_nvmf_qpair *qpair = req->qpair; 511 struct spdk_nvmf_transport *transport = qpair->transport; 512 struct spdk_nvmf_ctrlr *ctrlr; 513 struct spdk_nvmf_subsystem *subsystem; 514 515 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "recfmt 0x%x qid %u sqsize %u\n", 516 cmd->recfmt, cmd->qid, cmd->sqsize); 517 518 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect data:\n"); 519 SPDK_DEBUGLOG(SPDK_LOG_NVMF, " cntlid: 0x%04x\n", data->cntlid); 520 SPDK_DEBUGLOG(SPDK_LOG_NVMF, " hostid: %08x-%04x-%04x-%02x%02x-%04x%08x ***\n", 521 ntohl(*(uint32_t *)&data->hostid[0]), 522 ntohs(*(uint16_t *)&data->hostid[4]), 523 ntohs(*(uint16_t *)&data->hostid[6]), 524 data->hostid[8], 525 data->hostid[9], 526 ntohs(*(uint16_t *)&data->hostid[10]), 527 ntohl(*(uint32_t *)&data->hostid[12])); 528 SPDK_DEBUGLOG(SPDK_LOG_NVMF, " subnqn: \"%s\"\n", data->subnqn); 529 SPDK_DEBUGLOG(SPDK_LOG_NVMF, " hostnqn: \"%s\"\n", data->hostnqn); 530 531 subsystem = spdk_nvmf_tgt_find_subsystem(transport->tgt, data->subnqn); 532 if (!subsystem) { 533 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn); 534 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 535 } 536 537 if (cmd->recfmt != 0) { 538 SPDK_ERRLOG("Connect command unsupported RECFMT %u\n", cmd->recfmt); 539 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 540 rsp->status.sc = SPDK_NVMF_FABRIC_SC_INCOMPATIBLE_FORMAT; 541 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 542 } 543 544 /* 545 * SQSIZE is a 0-based value, so it must be at least 1 (minimum queue depth is 2) and 546 * strictly less than max_aq_depth (admin queues) or max_queue_depth (io queues). 547 */ 548 if (cmd->sqsize == 0) { 549 SPDK_ERRLOG("Invalid SQSIZE = 0\n"); 550 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize); 551 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 552 } 553 554 if (cmd->qid == 0) { 555 if (cmd->sqsize >= transport->opts.max_aq_depth) { 556 SPDK_ERRLOG("Invalid SQSIZE for admin queue %u (min 1, max %u)\n", 557 cmd->sqsize, transport->opts.max_aq_depth - 1); 558 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize); 559 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 560 } 561 } else if (cmd->sqsize >= transport->opts.max_queue_depth) { 562 SPDK_ERRLOG("Invalid SQSIZE %u (min 1, max %u)\n", 563 cmd->sqsize, transport->opts.max_queue_depth - 1); 564 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize); 565 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 566 } 567 568 qpair->sq_head_max = cmd->sqsize; 569 qpair->qid = cmd->qid; 570 571 if (0 == qpair->qid) { 572 qpair->group->stat.admin_qpairs++; 573 } else { 574 qpair->group->stat.io_qpairs++; 575 } 576 577 if (cmd->qid == 0) { 578 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Connect Admin Queue for controller ID 0x%x\n", data->cntlid); 579 580 if (data->cntlid != 0xFFFF) { 581 /* This NVMf target only supports dynamic mode. */ 582 SPDK_ERRLOG("The NVMf target only supports dynamic mode (CNTLID = 0x%x).\n", data->cntlid); 583 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid); 584 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 585 } 586 587 /* Establish a new ctrlr */ 588 ctrlr = spdk_nvmf_ctrlr_create(subsystem, req, cmd, data); 589 if (!ctrlr) { 590 SPDK_ERRLOG("spdk_nvmf_ctrlr_create() failed\n"); 591 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 592 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 593 } else { 594 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 595 } 596 } else { 597 spdk_thread_send_msg(subsystem->thread, _spdk_nvmf_ctrlr_add_io_qpair, req); 598 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 599 } 600 } 601 602 static inline bool 603 nvmf_request_is_fabric_connect(struct spdk_nvmf_request *req) 604 { 605 return req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC && 606 req->cmd->nvmf_cmd.fctype == SPDK_NVMF_FABRIC_COMMAND_CONNECT; 607 } 608 609 static struct spdk_nvmf_subsystem_poll_group * 610 nvmf_subsystem_pg_from_connect_cmd(struct spdk_nvmf_request *req) 611 { 612 struct spdk_nvmf_fabric_connect_data *data; 613 struct spdk_nvmf_subsystem *subsystem; 614 struct spdk_nvmf_tgt *tgt; 615 616 assert(nvmf_request_is_fabric_connect(req)); 617 assert(req->qpair->ctrlr == NULL); 618 619 data = req->data; 620 tgt = req->qpair->transport->tgt; 621 622 subsystem = spdk_nvmf_tgt_find_subsystem(tgt, data->subnqn); 623 if (subsystem == NULL) { 624 return NULL; 625 } 626 627 return &req->qpair->group->sgroups[subsystem->id]; 628 } 629 630 int 631 spdk_nvmf_ctrlr_connect(struct spdk_nvmf_request *req) 632 { 633 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 634 struct spdk_nvmf_qpair *qpair = req->qpair; 635 struct spdk_nvmf_subsystem_poll_group *sgroup; 636 enum spdk_nvmf_request_exec_status status; 637 638 sgroup = nvmf_subsystem_pg_from_connect_cmd(req); 639 if (!sgroup) { 640 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn); 641 status = SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 642 goto out; 643 } 644 645 sgroup->io_outstanding++; 646 TAILQ_INSERT_TAIL(&qpair->outstanding, req, link); 647 648 status = _spdk_nvmf_ctrlr_connect(req); 649 650 out: 651 if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 652 spdk_nvmf_request_complete(req); 653 } 654 655 return status; 656 } 657 658 static int 659 spdk_nvmf_ctrlr_cmd_connect(struct spdk_nvmf_request *req) 660 { 661 struct spdk_nvmf_fabric_connect_data *data = req->data; 662 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 663 struct spdk_nvmf_transport *transport = req->qpair->transport; 664 struct spdk_nvmf_subsystem *subsystem; 665 666 if (req->length < sizeof(struct spdk_nvmf_fabric_connect_data)) { 667 SPDK_ERRLOG("Connect command data length 0x%x too small\n", req->length); 668 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 669 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 670 } 671 672 subsystem = spdk_nvmf_tgt_find_subsystem(transport->tgt, data->subnqn); 673 if (!subsystem) { 674 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn); 675 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 676 } 677 678 if ((subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE) || 679 (subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSING) || 680 (subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED) || 681 (subsystem->state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING)) { 682 SPDK_ERRLOG("Subsystem '%s' is not ready\n", subsystem->subnqn); 683 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 684 rsp->status.sc = SPDK_NVMF_FABRIC_SC_CONTROLLER_BUSY; 685 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 686 } 687 688 /* Ensure that hostnqn is null terminated */ 689 if (!memchr(data->hostnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1)) { 690 SPDK_ERRLOG("Connect HOSTNQN is not null terminated\n"); 691 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, hostnqn); 692 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 693 } 694 695 if (!spdk_nvmf_qpair_access_allowed(req->qpair, subsystem, data->hostnqn)) { 696 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 697 rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_HOST; 698 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 699 } 700 701 return _spdk_nvmf_ctrlr_connect(req); 702 } 703 704 static void 705 spdk_nvmf_ctrlr_cc_reset_done(struct spdk_io_channel_iter *i, int status) 706 { 707 struct spdk_nvmf_ctrlr *ctrlr = spdk_io_channel_iter_get_ctx(i); 708 709 if (status < 0) { 710 SPDK_ERRLOG("Fail to disconnect io ctrlr qpairs\n"); 711 assert(false); 712 } 713 714 /* Only a subset of the registers are cleared out on a reset */ 715 ctrlr->vcprop.cc.raw = 0; 716 ctrlr->vcprop.csts.raw = 0; 717 718 } 719 720 const struct spdk_nvmf_registers * 721 spdk_nvmf_ctrlr_get_regs(struct spdk_nvmf_ctrlr *ctrlr) 722 { 723 return &ctrlr->vcprop; 724 } 725 726 static uint64_t 727 nvmf_prop_get_cap(struct spdk_nvmf_ctrlr *ctrlr) 728 { 729 return ctrlr->vcprop.cap.raw; 730 } 731 732 static uint64_t 733 nvmf_prop_get_vs(struct spdk_nvmf_ctrlr *ctrlr) 734 { 735 return ctrlr->vcprop.vs.raw; 736 } 737 738 static uint64_t 739 nvmf_prop_get_cc(struct spdk_nvmf_ctrlr *ctrlr) 740 { 741 return ctrlr->vcprop.cc.raw; 742 } 743 744 static bool 745 nvmf_prop_set_cc(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 746 { 747 union spdk_nvme_cc_register cc, diff; 748 749 cc.raw = value; 750 751 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cur CC: 0x%08x\n", ctrlr->vcprop.cc.raw); 752 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "new CC: 0x%08x\n", cc.raw); 753 754 /* 755 * Calculate which bits changed between the current and new CC. 756 * Mark each bit as 0 once it is handled to determine if any unhandled bits were changed. 757 */ 758 diff.raw = cc.raw ^ ctrlr->vcprop.cc.raw; 759 760 if (diff.bits.en) { 761 if (cc.bits.en) { 762 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Property Set CC Enable!\n"); 763 ctrlr->vcprop.cc.bits.en = 1; 764 ctrlr->vcprop.csts.bits.rdy = 1; 765 } else { 766 ctrlr->vcprop.cc.bits.en = 0; 767 spdk_for_each_channel(ctrlr->subsys->tgt, 768 spdk_nvmf_ctrlr_disconnect_io_qpairs_on_pg, 769 ctrlr, 770 spdk_nvmf_ctrlr_cc_reset_done); 771 } 772 diff.bits.en = 0; 773 } 774 775 if (diff.bits.shn) { 776 if (cc.bits.shn == SPDK_NVME_SHN_NORMAL || 777 cc.bits.shn == SPDK_NVME_SHN_ABRUPT) { 778 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Property Set CC Shutdown %u%ub!\n", 779 cc.bits.shn >> 1, cc.bits.shn & 1); 780 ctrlr->vcprop.cc.bits.shn = cc.bits.shn; 781 ctrlr->vcprop.cc.bits.en = 0; 782 ctrlr->vcprop.csts.bits.rdy = 0; 783 ctrlr->vcprop.csts.bits.shst = SPDK_NVME_SHST_COMPLETE; 784 } else if (cc.bits.shn == 0) { 785 ctrlr->vcprop.cc.bits.shn = 0; 786 } else { 787 SPDK_ERRLOG("Prop Set CC: Invalid SHN value %u%ub\n", 788 cc.bits.shn >> 1, cc.bits.shn & 1); 789 return false; 790 } 791 diff.bits.shn = 0; 792 } 793 794 if (diff.bits.iosqes) { 795 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Prop Set IOSQES = %u (%u bytes)\n", 796 cc.bits.iosqes, 1u << cc.bits.iosqes); 797 ctrlr->vcprop.cc.bits.iosqes = cc.bits.iosqes; 798 diff.bits.iosqes = 0; 799 } 800 801 if (diff.bits.iocqes) { 802 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Prop Set IOCQES = %u (%u bytes)\n", 803 cc.bits.iocqes, 1u << cc.bits.iocqes); 804 ctrlr->vcprop.cc.bits.iocqes = cc.bits.iocqes; 805 diff.bits.iocqes = 0; 806 } 807 808 if (diff.bits.ams) { 809 SPDK_ERRLOG("Arbitration Mechanism Selected (AMS) 0x%x not supported!\n", cc.bits.ams); 810 return false; 811 } 812 813 if (diff.bits.mps) { 814 SPDK_ERRLOG("Memory Page Size (MPS) %u KiB not supported!\n", (1 << (2 + cc.bits.mps))); 815 return false; 816 } 817 818 if (diff.bits.css) { 819 SPDK_ERRLOG("I/O Command Set Selected (CSS) 0x%x not supported!\n", cc.bits.css); 820 return false; 821 } 822 823 if (diff.raw != 0) { 824 SPDK_ERRLOG("Prop Set CC toggled reserved bits 0x%x!\n", diff.raw); 825 return false; 826 } 827 828 return true; 829 } 830 831 static uint64_t 832 nvmf_prop_get_csts(struct spdk_nvmf_ctrlr *ctrlr) 833 { 834 return ctrlr->vcprop.csts.raw; 835 } 836 837 static uint64_t 838 nvmf_prop_get_aqa(struct spdk_nvmf_ctrlr *ctrlr) 839 { 840 return ctrlr->vcprop.aqa.raw; 841 } 842 843 static bool 844 nvmf_prop_set_aqa(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 845 { 846 union spdk_nvme_aqa_register aqa; 847 848 aqa.raw = value; 849 850 if (aqa.bits.asqs > ctrlr->vcprop.cap.bits.mqes || 851 aqa.bits.acqs > ctrlr->vcprop.cap.bits.mqes) { 852 return false; 853 } 854 855 ctrlr->vcprop.aqa.raw = value; 856 857 return true; 858 } 859 860 static uint64_t 861 nvmf_prop_get_asq(struct spdk_nvmf_ctrlr *ctrlr) 862 { 863 return ctrlr->vcprop.asq; 864 } 865 866 static bool 867 nvmf_prop_set_asq_lower(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 868 { 869 ctrlr->vcprop.asq = (ctrlr->vcprop.asq & (0xFFFFFFFFULL << 32ULL)) | value; 870 871 return true; 872 } 873 874 static bool 875 nvmf_prop_set_asq_upper(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 876 { 877 ctrlr->vcprop.asq = (ctrlr->vcprop.asq & 0xFFFFFFFFULL) | ((uint64_t)value << 32ULL); 878 879 return true; 880 } 881 882 static uint64_t 883 nvmf_prop_get_acq(struct spdk_nvmf_ctrlr *ctrlr) 884 { 885 return ctrlr->vcprop.acq; 886 } 887 888 static bool 889 nvmf_prop_set_acq_lower(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 890 { 891 ctrlr->vcprop.acq = (ctrlr->vcprop.acq & (0xFFFFFFFFULL << 32ULL)) | value; 892 893 return true; 894 } 895 896 static bool 897 nvmf_prop_set_acq_upper(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 898 { 899 ctrlr->vcprop.acq = (ctrlr->vcprop.acq & 0xFFFFFFFFULL) | ((uint64_t)value << 32ULL); 900 901 return true; 902 } 903 904 struct nvmf_prop { 905 uint32_t ofst; 906 uint8_t size; 907 char name[11]; 908 uint64_t (*get_cb)(struct spdk_nvmf_ctrlr *ctrlr); 909 bool (*set_cb)(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value); 910 bool (*set_upper_cb)(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value); 911 }; 912 913 #define PROP(field, size, get_cb, set_cb, set_upper_cb) \ 914 { \ 915 offsetof(struct spdk_nvme_registers, field), \ 916 size, \ 917 #field, \ 918 get_cb, set_cb, set_upper_cb \ 919 } 920 921 static const struct nvmf_prop nvmf_props[] = { 922 PROP(cap, 8, nvmf_prop_get_cap, NULL, NULL), 923 PROP(vs, 4, nvmf_prop_get_vs, NULL, NULL), 924 PROP(cc, 4, nvmf_prop_get_cc, nvmf_prop_set_cc, NULL), 925 PROP(csts, 4, nvmf_prop_get_csts, NULL, NULL), 926 PROP(aqa, 4, nvmf_prop_get_aqa, nvmf_prop_set_aqa, NULL), 927 PROP(asq, 8, nvmf_prop_get_asq, nvmf_prop_set_asq_lower, nvmf_prop_set_asq_upper), 928 PROP(acq, 8, nvmf_prop_get_acq, nvmf_prop_set_acq_lower, nvmf_prop_set_acq_upper), 929 }; 930 931 static const struct nvmf_prop * 932 find_prop(uint32_t ofst, uint8_t size) 933 { 934 size_t i; 935 936 for (i = 0; i < SPDK_COUNTOF(nvmf_props); i++) { 937 const struct nvmf_prop *prop = &nvmf_props[i]; 938 939 if ((ofst >= prop->ofst) && (ofst + size <= prop->ofst + prop->size)) { 940 return prop; 941 } 942 } 943 944 return NULL; 945 } 946 947 static int 948 spdk_nvmf_property_get(struct spdk_nvmf_request *req) 949 { 950 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 951 struct spdk_nvmf_fabric_prop_get_cmd *cmd = &req->cmd->prop_get_cmd; 952 struct spdk_nvmf_fabric_prop_get_rsp *response = &req->rsp->prop_get_rsp; 953 const struct nvmf_prop *prop; 954 uint8_t size; 955 956 response->status.sc = 0; 957 response->value.u64 = 0; 958 959 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "size %d, offset 0x%x\n", 960 cmd->attrib.size, cmd->ofst); 961 962 switch (cmd->attrib.size) { 963 case SPDK_NVMF_PROP_SIZE_4: 964 size = 4; 965 break; 966 case SPDK_NVMF_PROP_SIZE_8: 967 size = 8; 968 break; 969 default: 970 SPDK_ERRLOG("Invalid size value %d\n", cmd->attrib.size); 971 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 972 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 973 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 974 } 975 976 prop = find_prop(cmd->ofst, size); 977 if (prop == NULL || prop->get_cb == NULL) { 978 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 979 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 980 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 981 } 982 983 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "name: %s\n", prop->name); 984 985 response->value.u64 = prop->get_cb(ctrlr); 986 987 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "response value: 0x%" PRIx64 "\n", response->value.u64); 988 989 if (size != prop->size) { 990 /* The size must be 4 and the prop->size is 8. Figure out which part of the property to read. */ 991 assert(size == 4); 992 assert(prop->size == 8); 993 994 if (cmd->ofst == prop->ofst) { 995 /* Keep bottom 4 bytes only */ 996 response->value.u64 &= 0xFFFFFFFF; 997 } else { 998 /* Keep top 4 bytes only */ 999 response->value.u64 >>= 32; 1000 } 1001 } 1002 1003 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1004 } 1005 1006 static int 1007 spdk_nvmf_property_set(struct spdk_nvmf_request *req) 1008 { 1009 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1010 struct spdk_nvmf_fabric_prop_set_cmd *cmd = &req->cmd->prop_set_cmd; 1011 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1012 const struct nvmf_prop *prop; 1013 uint64_t value; 1014 uint8_t size; 1015 bool ret; 1016 1017 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "size %d, offset 0x%x, value 0x%" PRIx64 "\n", 1018 cmd->attrib.size, cmd->ofst, cmd->value.u64); 1019 1020 switch (cmd->attrib.size) { 1021 case SPDK_NVMF_PROP_SIZE_4: 1022 size = 4; 1023 break; 1024 case SPDK_NVMF_PROP_SIZE_8: 1025 size = 8; 1026 break; 1027 default: 1028 SPDK_ERRLOG("Invalid size value %d\n", cmd->attrib.size); 1029 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1030 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 1031 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1032 } 1033 1034 prop = find_prop(cmd->ofst, size); 1035 if (prop == NULL || prop->set_cb == NULL) { 1036 SPDK_ERRLOG("Invalid offset 0x%x\n", cmd->ofst); 1037 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1038 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 1039 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1040 } 1041 1042 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "name: %s\n", prop->name); 1043 1044 value = cmd->value.u64; 1045 1046 if (prop->size == 4) { 1047 ret = prop->set_cb(ctrlr, (uint32_t)value); 1048 } else if (size != prop->size) { 1049 /* The size must be 4 and the prop->size is 8. Figure out which part of the property to write. */ 1050 assert(size == 4); 1051 assert(prop->size == 8); 1052 1053 if (cmd->ofst == prop->ofst) { 1054 ret = prop->set_cb(ctrlr, (uint32_t)value); 1055 } else { 1056 ret = prop->set_upper_cb(ctrlr, (uint32_t)value); 1057 } 1058 } else { 1059 ret = prop->set_cb(ctrlr, (uint32_t)value); 1060 if (ret) { 1061 ret = prop->set_upper_cb(ctrlr, (uint32_t)(value >> 32)); 1062 } 1063 } 1064 1065 if (!ret) { 1066 SPDK_ERRLOG("prop set_cb failed\n"); 1067 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1068 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 1069 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1070 } 1071 1072 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1073 } 1074 1075 static int 1076 spdk_nvmf_ctrlr_set_features_arbitration(struct spdk_nvmf_request *req) 1077 { 1078 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1079 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1080 1081 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Arbitration (cdw11 = 0x%0x)\n", cmd->cdw11); 1082 1083 ctrlr->feat.arbitration.raw = cmd->cdw11; 1084 ctrlr->feat.arbitration.bits.reserved = 0; 1085 1086 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1087 } 1088 1089 static int 1090 spdk_nvmf_ctrlr_set_features_power_management(struct spdk_nvmf_request *req) 1091 { 1092 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1093 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1094 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1095 1096 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Power Management (cdw11 = 0x%0x)\n", cmd->cdw11); 1097 1098 /* Only PS = 0 is allowed, since we report NPSS = 0 */ 1099 if (cmd->cdw11_bits.feat_power_management.bits.ps != 0) { 1100 SPDK_ERRLOG("Invalid power state %u\n", cmd->cdw11_bits.feat_power_management.bits.ps); 1101 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1102 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1103 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1104 } 1105 1106 ctrlr->feat.power_management.raw = cmd->cdw11; 1107 ctrlr->feat.power_management.bits.reserved = 0; 1108 1109 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1110 } 1111 1112 static bool 1113 temp_threshold_opts_valid(const union spdk_nvme_feat_temperature_threshold *opts) 1114 { 1115 /* 1116 * Valid TMPSEL values: 1117 * 0000b - 1000b: temperature sensors 1118 * 1111b: set all implemented temperature sensors 1119 */ 1120 if (opts->bits.tmpsel >= 9 && opts->bits.tmpsel != 15) { 1121 /* 1001b - 1110b: reserved */ 1122 SPDK_ERRLOG("Invalid TMPSEL %u\n", opts->bits.tmpsel); 1123 return false; 1124 } 1125 1126 /* 1127 * Valid THSEL values: 1128 * 00b: over temperature threshold 1129 * 01b: under temperature threshold 1130 */ 1131 if (opts->bits.thsel > 1) { 1132 /* 10b - 11b: reserved */ 1133 SPDK_ERRLOG("Invalid THSEL %u\n", opts->bits.thsel); 1134 return false; 1135 } 1136 1137 return true; 1138 } 1139 1140 static int 1141 spdk_nvmf_ctrlr_set_features_temperature_threshold(struct spdk_nvmf_request *req) 1142 { 1143 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1144 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1145 1146 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Temperature Threshold (cdw11 = 0x%0x)\n", cmd->cdw11); 1147 1148 if (!temp_threshold_opts_valid(&cmd->cdw11_bits.feat_temp_threshold)) { 1149 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1150 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1151 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1152 } 1153 1154 /* TODO: no sensors implemented - ignore new values */ 1155 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1156 } 1157 1158 static int 1159 spdk_nvmf_ctrlr_get_features_temperature_threshold(struct spdk_nvmf_request *req) 1160 { 1161 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1162 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1163 1164 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Temperature Threshold (cdw11 = 0x%0x)\n", cmd->cdw11); 1165 1166 if (!temp_threshold_opts_valid(&cmd->cdw11_bits.feat_temp_threshold)) { 1167 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1168 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1169 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1170 } 1171 1172 /* TODO: no sensors implemented - return 0 for all thresholds */ 1173 rsp->cdw0 = 0; 1174 1175 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1176 } 1177 1178 static int 1179 spdk_nvmf_ctrlr_set_features_error_recovery(struct spdk_nvmf_request *req) 1180 { 1181 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1182 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1183 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1184 1185 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Error Recovery (cdw11 = 0x%0x)\n", cmd->cdw11); 1186 1187 if (cmd->cdw11_bits.feat_error_recovery.bits.dulbe) { 1188 /* 1189 * Host is not allowed to set this bit, since we don't advertise it in 1190 * Identify Namespace. 1191 */ 1192 SPDK_ERRLOG("Host set unsupported DULBE bit\n"); 1193 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1194 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1195 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1196 } 1197 1198 ctrlr->feat.error_recovery.raw = cmd->cdw11; 1199 ctrlr->feat.error_recovery.bits.reserved = 0; 1200 1201 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1202 } 1203 1204 static int 1205 spdk_nvmf_ctrlr_set_features_volatile_write_cache(struct spdk_nvmf_request *req) 1206 { 1207 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1208 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1209 1210 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Volatile Write Cache (cdw11 = 0x%0x)\n", cmd->cdw11); 1211 1212 ctrlr->feat.volatile_write_cache.raw = cmd->cdw11; 1213 ctrlr->feat.volatile_write_cache.bits.reserved = 0; 1214 1215 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Volatile Write Cache %s\n", 1216 ctrlr->feat.volatile_write_cache.bits.wce ? "Enabled" : "Disabled"); 1217 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1218 } 1219 1220 static int 1221 spdk_nvmf_ctrlr_set_features_write_atomicity(struct spdk_nvmf_request *req) 1222 { 1223 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1224 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1225 1226 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Write Atomicity (cdw11 = 0x%0x)\n", cmd->cdw11); 1227 1228 ctrlr->feat.write_atomicity.raw = cmd->cdw11; 1229 ctrlr->feat.write_atomicity.bits.reserved = 0; 1230 1231 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1232 } 1233 1234 static int 1235 spdk_nvmf_ctrlr_set_features_host_identifier(struct spdk_nvmf_request *req) 1236 { 1237 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1238 1239 SPDK_ERRLOG("Set Features - Host Identifier not allowed\n"); 1240 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 1241 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1242 } 1243 1244 static int 1245 spdk_nvmf_ctrlr_get_features_host_identifier(struct spdk_nvmf_request *req) 1246 { 1247 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1248 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1249 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1250 1251 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Host Identifier\n"); 1252 1253 if (!cmd->cdw11_bits.feat_host_identifier.bits.exhid) { 1254 /* NVMe over Fabrics requires EXHID=1 (128-bit/16-byte host ID) */ 1255 SPDK_ERRLOG("Get Features - Host Identifier with EXHID=0 not allowed\n"); 1256 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1257 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1258 } 1259 1260 if (req->data == NULL || req->length < sizeof(ctrlr->hostid)) { 1261 SPDK_ERRLOG("Invalid data buffer for Get Features - Host Identifier\n"); 1262 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1263 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1264 } 1265 1266 spdk_uuid_copy((struct spdk_uuid *)req->data, &ctrlr->hostid); 1267 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1268 } 1269 1270 static int 1271 spdk_nvmf_ctrlr_get_features_reservation_notification_mask(struct spdk_nvmf_request *req) 1272 { 1273 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1274 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1275 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1276 struct spdk_nvmf_ns *ns; 1277 1278 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "get Features - Reservation Notificaton Mask\n"); 1279 1280 if (cmd->nsid == 0xffffffffu) { 1281 SPDK_ERRLOG("get Features - Invalid Namespace ID\n"); 1282 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1283 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1284 } 1285 1286 ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 1287 if (ns == NULL) { 1288 SPDK_ERRLOG("Set Features - Invalid Namespace ID\n"); 1289 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1290 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1291 } 1292 rsp->cdw0 = ns->mask; 1293 1294 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1295 } 1296 1297 static int 1298 spdk_nvmf_ctrlr_set_features_reservation_notification_mask(struct spdk_nvmf_request *req) 1299 { 1300 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1301 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 1302 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1303 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1304 struct spdk_nvmf_ns *ns; 1305 1306 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Reservation Notificaton Mask\n"); 1307 1308 if (cmd->nsid == 0xffffffffu) { 1309 for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL; 1310 ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) { 1311 ns->mask = cmd->cdw11; 1312 } 1313 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1314 } 1315 1316 ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 1317 if (ns == NULL) { 1318 SPDK_ERRLOG("Set Features - Invalid Namespace ID\n"); 1319 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1320 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1321 } 1322 ns->mask = cmd->cdw11; 1323 1324 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1325 } 1326 1327 static int 1328 spdk_nvmf_ctrlr_get_features_reservation_persistence(struct spdk_nvmf_request *req) 1329 { 1330 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1331 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1332 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1333 struct spdk_nvmf_ns *ns; 1334 1335 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get Features - Reservation Persistence\n"); 1336 1337 ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 1338 /* NSID with 0xffffffffu also included */ 1339 if (ns == NULL) { 1340 SPDK_ERRLOG("Get Features - Invalid Namespace ID\n"); 1341 response->status.sct = SPDK_NVME_SCT_GENERIC; 1342 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1343 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1344 } 1345 1346 response->cdw0 = ns->ptpl_activated; 1347 1348 response->status.sct = SPDK_NVME_SCT_GENERIC; 1349 response->status.sc = SPDK_NVME_SC_SUCCESS; 1350 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1351 } 1352 1353 static int 1354 spdk_nvmf_ctrlr_set_features_reservation_persistence(struct spdk_nvmf_request *req) 1355 { 1356 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1357 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1358 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1359 struct spdk_nvmf_ns *ns; 1360 bool ptpl; 1361 1362 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Reservation Persistence\n"); 1363 1364 ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 1365 ptpl = cmd->cdw11_bits.feat_rsv_persistence.bits.ptpl; 1366 1367 if (cmd->nsid != 0xffffffffu && ns && ns->ptpl_file) { 1368 ns->ptpl_activated = ptpl; 1369 } else if (cmd->nsid == 0xffffffffu) { 1370 for (ns = spdk_nvmf_subsystem_get_first_ns(ctrlr->subsys); ns && ns->ptpl_file; 1371 ns = spdk_nvmf_subsystem_get_next_ns(ctrlr->subsys, ns)) { 1372 ns->ptpl_activated = ptpl; 1373 } 1374 } else { 1375 SPDK_ERRLOG("Set Features - Invalid Namespace ID or Reservation Configuration\n"); 1376 response->status.sct = SPDK_NVME_SCT_GENERIC; 1377 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1378 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1379 } 1380 1381 /* TODO: Feature not changeable for now */ 1382 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1383 response->status.sc = SPDK_NVME_SC_FEATURE_ID_NOT_SAVEABLE; 1384 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1385 } 1386 1387 static int 1388 spdk_nvmf_ctrlr_set_features_keep_alive_timer(struct spdk_nvmf_request *req) 1389 { 1390 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1391 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1392 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1393 1394 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Keep Alive Timer (%u ms)\n", cmd->cdw11); 1395 1396 /* 1397 * if attempts to disable keep alive by setting kato to 0h 1398 * a status value of keep alive invalid shall be returned 1399 */ 1400 if (cmd->cdw11_bits.feat_keep_alive_timer.bits.kato == 0) { 1401 rsp->status.sc = SPDK_NVME_SC_KEEP_ALIVE_INVALID; 1402 } else if (cmd->cdw11_bits.feat_keep_alive_timer.bits.kato < MIN_KEEP_ALIVE_TIMEOUT_IN_MS) { 1403 ctrlr->feat.keep_alive_timer.bits.kato = MIN_KEEP_ALIVE_TIMEOUT_IN_MS; 1404 } else { 1405 /* round up to milliseconds */ 1406 ctrlr->feat.keep_alive_timer.bits.kato = spdk_divide_round_up( 1407 cmd->cdw11_bits.feat_keep_alive_timer.bits.kato, 1408 KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS) * 1409 KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS; 1410 } 1411 1412 /* 1413 * if change the keep alive timeout value successfully 1414 * update the keep alive poller. 1415 */ 1416 if (cmd->cdw11_bits.feat_keep_alive_timer.bits.kato != 0) { 1417 if (ctrlr->keep_alive_poller != NULL) { 1418 spdk_poller_unregister(&ctrlr->keep_alive_poller); 1419 } 1420 ctrlr->keep_alive_poller = SPDK_POLLER_REGISTER(spdk_nvmf_ctrlr_keep_alive_poll, ctrlr, 1421 ctrlr->feat.keep_alive_timer.bits.kato * 1000); 1422 } 1423 1424 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Keep Alive Timer set to %u ms\n", 1425 ctrlr->feat.keep_alive_timer.bits.kato); 1426 1427 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1428 } 1429 1430 static int 1431 spdk_nvmf_ctrlr_set_features_number_of_queues(struct spdk_nvmf_request *req) 1432 { 1433 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1434 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1435 uint32_t count; 1436 1437 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Number of Queues, cdw11 0x%x\n", 1438 req->cmd->nvme_cmd.cdw11); 1439 1440 count = spdk_bit_array_count_set(ctrlr->qpair_mask); 1441 /* verify that the controller is ready to process commands */ 1442 if (count > 1) { 1443 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Queue pairs already active!\n"); 1444 rsp->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 1445 } else { 1446 /* 1447 * Ignore the value requested by the host - 1448 * always return the pre-configured value based on max_qpairs_allowed. 1449 */ 1450 rsp->cdw0 = ctrlr->feat.number_of_queues.raw; 1451 } 1452 1453 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1454 } 1455 1456 static int 1457 spdk_nvmf_ctrlr_set_features_async_event_configuration(struct spdk_nvmf_request *req) 1458 { 1459 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1460 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1461 1462 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Set Features - Async Event Configuration, cdw11 0x%08x\n", 1463 cmd->cdw11); 1464 ctrlr->feat.async_event_configuration.raw = cmd->cdw11; 1465 ctrlr->feat.async_event_configuration.bits.reserved = 0; 1466 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1467 } 1468 1469 static int 1470 spdk_nvmf_ctrlr_async_event_request(struct spdk_nvmf_request *req) 1471 { 1472 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1473 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1474 struct spdk_nvmf_subsystem_poll_group *sgroup; 1475 1476 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Async Event Request\n"); 1477 1478 /* Only one asynchronous event is supported for now */ 1479 if (ctrlr->aer_req != NULL) { 1480 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "AERL exceeded\n"); 1481 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1482 rsp->status.sc = SPDK_NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED; 1483 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1484 } 1485 1486 if (ctrlr->notice_event.bits.async_event_type == 1487 SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) { 1488 rsp->cdw0 = ctrlr->notice_event.raw; 1489 ctrlr->notice_event.raw = 0; 1490 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1491 } 1492 1493 if (ctrlr->reservation_event.bits.async_event_type == 1494 SPDK_NVME_ASYNC_EVENT_TYPE_IO) { 1495 rsp->cdw0 = ctrlr->reservation_event.raw; 1496 ctrlr->reservation_event.raw = 0; 1497 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1498 } 1499 1500 /* AER cmd is an exception */ 1501 sgroup = &req->qpair->group->sgroups[ctrlr->subsys->id]; 1502 sgroup->io_outstanding--; 1503 1504 ctrlr->aer_req = req; 1505 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 1506 } 1507 1508 static void 1509 spdk_nvmf_get_firmware_slot_log_page(void *buffer, uint64_t offset, uint32_t length) 1510 { 1511 struct spdk_nvme_firmware_page fw_page; 1512 size_t copy_len; 1513 1514 memset(&fw_page, 0, sizeof(fw_page)); 1515 fw_page.afi.active_slot = 1; 1516 fw_page.afi.next_reset_slot = 0; 1517 spdk_strcpy_pad(fw_page.revision[0], FW_VERSION, sizeof(fw_page.revision[0]), ' '); 1518 1519 if (offset < sizeof(fw_page)) { 1520 copy_len = spdk_min(sizeof(fw_page) - offset, length); 1521 if (copy_len > 0) { 1522 memcpy(buffer, (const char *)&fw_page + offset, copy_len); 1523 } 1524 } 1525 } 1526 1527 void 1528 spdk_nvmf_ctrlr_ns_changed(struct spdk_nvmf_ctrlr *ctrlr, uint32_t nsid) 1529 { 1530 uint16_t max_changes = SPDK_COUNTOF(ctrlr->changed_ns_list.ns_list); 1531 uint16_t i; 1532 bool found = false; 1533 1534 for (i = 0; i < ctrlr->changed_ns_list_count; i++) { 1535 if (ctrlr->changed_ns_list.ns_list[i] == nsid) { 1536 /* nsid is already in the list */ 1537 found = true; 1538 break; 1539 } 1540 } 1541 1542 if (!found) { 1543 if (ctrlr->changed_ns_list_count == max_changes) { 1544 /* Out of space - set first entry to FFFFFFFFh and zero-fill the rest. */ 1545 ctrlr->changed_ns_list.ns_list[0] = 0xFFFFFFFFu; 1546 for (i = 1; i < max_changes; i++) { 1547 ctrlr->changed_ns_list.ns_list[i] = 0; 1548 } 1549 } else { 1550 ctrlr->changed_ns_list.ns_list[ctrlr->changed_ns_list_count++] = nsid; 1551 } 1552 } 1553 } 1554 1555 static void 1556 spdk_nvmf_get_changed_ns_list_log_page(struct spdk_nvmf_ctrlr *ctrlr, 1557 void *buffer, uint64_t offset, uint32_t length) 1558 { 1559 size_t copy_length; 1560 1561 if (offset < sizeof(ctrlr->changed_ns_list)) { 1562 copy_length = spdk_min(length, sizeof(ctrlr->changed_ns_list) - offset); 1563 if (copy_length) { 1564 memcpy(buffer, (char *)&ctrlr->changed_ns_list + offset, copy_length); 1565 } 1566 } 1567 1568 /* Clear log page each time it is read */ 1569 ctrlr->changed_ns_list_count = 0; 1570 memset(&ctrlr->changed_ns_list, 0, sizeof(ctrlr->changed_ns_list)); 1571 } 1572 1573 /* The structure can be modified if we provide support for other commands in future */ 1574 static const struct spdk_nvme_cmds_and_effect_log_page g_cmds_and_effect_log_page = { 1575 .admin_cmds_supported = { 1576 /* CSUPP, LBCC, NCC, NIC, CCC, CSE */ 1577 /* Get Log Page */ 1578 [SPDK_NVME_OPC_GET_LOG_PAGE] = {1, 0, 0, 0, 0, 0, 0, 0}, 1579 /* Identify */ 1580 [SPDK_NVME_OPC_IDENTIFY] = {1, 0, 0, 0, 0, 0, 0, 0}, 1581 /* Abort */ 1582 [SPDK_NVME_OPC_ABORT] = {1, 0, 0, 0, 0, 0, 0, 0}, 1583 /* Set Features */ 1584 [SPDK_NVME_OPC_SET_FEATURES] = {1, 0, 0, 0, 0, 0, 0, 0}, 1585 /* Get Features */ 1586 [SPDK_NVME_OPC_GET_FEATURES] = {1, 0, 0, 0, 0, 0, 0, 0}, 1587 /* Async Event Request */ 1588 [SPDK_NVME_OPC_ASYNC_EVENT_REQUEST] = {1, 0, 0, 0, 0, 0, 0, 0}, 1589 /* Keep Alive */ 1590 [SPDK_NVME_OPC_KEEP_ALIVE] = {1, 0, 0, 0, 0, 0, 0, 0}, 1591 }, 1592 .io_cmds_supported = { 1593 /* FLUSH */ 1594 [SPDK_NVME_OPC_FLUSH] = {1, 1, 0, 0, 0, 0, 0, 0}, 1595 /* WRITE */ 1596 [SPDK_NVME_OPC_WRITE] = {1, 1, 0, 0, 0, 0, 0, 0}, 1597 /* READ */ 1598 [SPDK_NVME_OPC_READ] = {1, 0, 0, 0, 0, 0, 0, 0}, 1599 /* WRITE ZEROES */ 1600 [SPDK_NVME_OPC_WRITE_ZEROES] = {1, 1, 0, 0, 0, 0, 0, 0}, 1601 /* DATASET MANAGEMENT */ 1602 [SPDK_NVME_OPC_DATASET_MANAGEMENT] = {1, 1, 0, 0, 0, 0, 0, 0}, 1603 /* COMPARE */ 1604 [SPDK_NVME_OPC_COMPARE] = {1, 0, 0, 0, 0, 0, 0, 0}, 1605 }, 1606 }; 1607 1608 static void 1609 spdk_nvmf_get_cmds_and_effects_log_page(void *buffer, 1610 uint64_t offset, uint32_t length) 1611 { 1612 uint32_t page_size = sizeof(struct spdk_nvme_cmds_and_effect_log_page); 1613 size_t copy_len = 0; 1614 size_t zero_len = length; 1615 1616 if (offset < page_size) { 1617 copy_len = spdk_min(page_size - offset, length); 1618 zero_len -= copy_len; 1619 memcpy(buffer, (char *)(&g_cmds_and_effect_log_page) + offset, copy_len); 1620 } 1621 1622 if (zero_len) { 1623 memset((char *)buffer + copy_len, 0, zero_len); 1624 } 1625 } 1626 1627 static void 1628 spdk_nvmf_get_reservation_notification_log_page(struct spdk_nvmf_ctrlr *ctrlr, 1629 void *data, uint64_t offset, uint32_t length) 1630 { 1631 uint32_t unit_log_len, avail_log_len, next_pos, copy_len; 1632 struct spdk_nvmf_reservation_log *log, *log_tmp; 1633 uint8_t *buf = data; 1634 1635 unit_log_len = sizeof(struct spdk_nvme_reservation_notification_log); 1636 /* No available log, return 1 zeroed log page */ 1637 if (!ctrlr->num_avail_log_pages) { 1638 memset(buf, 0, spdk_min(length, unit_log_len)); 1639 return; 1640 } 1641 1642 avail_log_len = ctrlr->num_avail_log_pages * unit_log_len; 1643 if (offset >= avail_log_len) { 1644 return; 1645 } 1646 1647 next_pos = copy_len = 0; 1648 TAILQ_FOREACH_SAFE(log, &ctrlr->log_head, link, log_tmp) { 1649 TAILQ_REMOVE(&ctrlr->log_head, log, link); 1650 ctrlr->num_avail_log_pages--; 1651 1652 next_pos += unit_log_len; 1653 if (next_pos > offset) { 1654 copy_len = spdk_min(next_pos - offset, length); 1655 memcpy(buf, &log->log, copy_len); 1656 length -= copy_len; 1657 offset += copy_len; 1658 buf += copy_len; 1659 } 1660 free(log); 1661 1662 if (length == 0) { 1663 break; 1664 } 1665 } 1666 return; 1667 } 1668 1669 static int 1670 spdk_nvmf_ctrlr_get_log_page(struct spdk_nvmf_request *req) 1671 { 1672 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1673 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 1674 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1675 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1676 uint64_t offset, len; 1677 uint32_t numdl, numdu; 1678 uint8_t lid; 1679 1680 if (req->data == NULL) { 1681 SPDK_ERRLOG("get log command with no buffer\n"); 1682 response->status.sct = SPDK_NVME_SCT_GENERIC; 1683 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1684 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1685 } 1686 1687 offset = (uint64_t)cmd->cdw12 | ((uint64_t)cmd->cdw13 << 32); 1688 if (offset & 3) { 1689 SPDK_ERRLOG("Invalid log page offset 0x%" PRIx64 "\n", offset); 1690 response->status.sct = SPDK_NVME_SCT_GENERIC; 1691 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1692 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1693 } 1694 1695 numdl = cmd->cdw10_bits.get_log_page.numdl; 1696 numdu = cmd->cdw11_bits.get_log_page.numdu; 1697 len = ((numdu << 16) + numdl + (uint64_t)1) * 4; 1698 if (len > req->length) { 1699 SPDK_ERRLOG("Get log page: len (%" PRIu64 ") > buf size (%u)\n", 1700 len, req->length); 1701 response->status.sct = SPDK_NVME_SCT_GENERIC; 1702 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1703 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1704 } 1705 1706 lid = cmd->cdw10_bits.get_log_page.lid; 1707 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Get log page: LID=0x%02X offset=0x%" PRIx64 " len=0x%" PRIx64 "\n", 1708 lid, offset, len); 1709 1710 if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 1711 switch (lid) { 1712 case SPDK_NVME_LOG_DISCOVERY: 1713 spdk_nvmf_get_discovery_log_page(subsystem->tgt, ctrlr->hostnqn, req->iov, req->iovcnt, offset, 1714 len); 1715 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1716 default: 1717 goto invalid_log_page; 1718 } 1719 } else { 1720 switch (lid) { 1721 case SPDK_NVME_LOG_ERROR: 1722 case SPDK_NVME_LOG_HEALTH_INFORMATION: 1723 /* TODO: actually fill out log page data */ 1724 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1725 case SPDK_NVME_LOG_FIRMWARE_SLOT: 1726 spdk_nvmf_get_firmware_slot_log_page(req->data, offset, len); 1727 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1728 case SPDK_NVME_LOG_COMMAND_EFFECTS_LOG: 1729 spdk_nvmf_get_cmds_and_effects_log_page(req->data, offset, len); 1730 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1731 case SPDK_NVME_LOG_CHANGED_NS_LIST: 1732 spdk_nvmf_get_changed_ns_list_log_page(ctrlr, req->data, offset, len); 1733 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1734 case SPDK_NVME_LOG_RESERVATION_NOTIFICATION: 1735 spdk_nvmf_get_reservation_notification_log_page(ctrlr, req->data, offset, len); 1736 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1737 default: 1738 goto invalid_log_page; 1739 } 1740 } 1741 1742 invalid_log_page: 1743 SPDK_ERRLOG("Unsupported Get Log Page 0x%02X\n", lid); 1744 response->status.sct = SPDK_NVME_SCT_GENERIC; 1745 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1746 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1747 } 1748 1749 int 1750 spdk_nvmf_ctrlr_identify_ns(struct spdk_nvmf_ctrlr *ctrlr, 1751 struct spdk_nvme_cmd *cmd, 1752 struct spdk_nvme_cpl *rsp, 1753 struct spdk_nvme_ns_data *nsdata) 1754 { 1755 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 1756 struct spdk_nvmf_ns *ns; 1757 uint32_t max_num_blocks; 1758 1759 if (cmd->nsid == 0 || cmd->nsid > subsystem->max_nsid) { 1760 SPDK_ERRLOG("Identify Namespace for invalid NSID %u\n", cmd->nsid); 1761 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1762 rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 1763 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1764 } 1765 1766 ns = _spdk_nvmf_subsystem_get_ns(subsystem, cmd->nsid); 1767 if (ns == NULL || ns->bdev == NULL) { 1768 /* 1769 * Inactive namespaces should return a zero filled data structure. 1770 * The data buffer is already zeroed by spdk_nvmf_ctrlr_process_admin_cmd(), 1771 * so we can just return early here. 1772 */ 1773 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Identify Namespace for inactive NSID %u\n", cmd->nsid); 1774 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1775 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 1776 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1777 } 1778 1779 spdk_nvmf_bdev_ctrlr_identify_ns(ns, nsdata, ctrlr->dif_insert_or_strip); 1780 1781 /* Due to bug in the Linux kernel NVMe driver we have to set noiob no larger than mdts */ 1782 max_num_blocks = ctrlr->admin_qpair->transport->opts.max_io_size / 1783 (1U << nsdata->lbaf[nsdata->flbas.format].lbads); 1784 if (nsdata->noiob > max_num_blocks) { 1785 nsdata->noiob = max_num_blocks; 1786 } 1787 1788 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1789 } 1790 1791 static void 1792 nvmf_ctrlr_populate_oacs(struct spdk_nvmf_ctrlr *ctrlr, 1793 struct spdk_nvme_ctrlr_data *cdata) 1794 { 1795 cdata->oacs.virtualization_management = 1796 g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_VIRTUALIZATION_MANAGEMENT].hdlr != NULL; 1797 cdata->oacs.nvme_mi = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NVME_MI_SEND].hdlr != NULL 1798 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NVME_MI_RECEIVE].hdlr != NULL; 1799 cdata->oacs.directives = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DIRECTIVE_SEND].hdlr != NULL 1800 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DIRECTIVE_RECEIVE].hdlr != NULL; 1801 cdata->oacs.device_self_test = 1802 g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DEVICE_SELF_TEST].hdlr != NULL; 1803 cdata->oacs.ns_manage = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NS_MANAGEMENT].hdlr != NULL 1804 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NS_ATTACHMENT].hdlr != NULL; 1805 cdata->oacs.firmware = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD].hdlr != 1806 NULL 1807 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FIRMWARE_COMMIT].hdlr != NULL; 1808 cdata->oacs.format = 1809 g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FORMAT_NVM].hdlr != NULL; 1810 cdata->oacs.security = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_SECURITY_SEND].hdlr != NULL 1811 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_SECURITY_RECEIVE].hdlr != NULL; 1812 cdata->oacs.get_lba_status = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_GET_LBA_STATUS].hdlr != 1813 NULL; 1814 } 1815 1816 int 1817 spdk_nvmf_ctrlr_identify_ctrlr(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_ctrlr_data *cdata) 1818 { 1819 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 1820 struct spdk_nvmf_transport *transport = ctrlr->admin_qpair->transport; 1821 1822 /* 1823 * Common fields for discovery and NVM subsystems 1824 */ 1825 spdk_strcpy_pad(cdata->fr, FW_VERSION, sizeof(cdata->fr), ' '); 1826 assert((transport->opts.max_io_size % 4096) == 0); 1827 cdata->mdts = spdk_u32log2(transport->opts.max_io_size / 4096); 1828 cdata->cntlid = ctrlr->cntlid; 1829 cdata->ver = ctrlr->vcprop.vs; 1830 cdata->lpa.edlp = 1; 1831 cdata->elpe = 127; 1832 cdata->maxcmd = transport->opts.max_queue_depth; 1833 cdata->sgls.supported = 1; 1834 cdata->sgls.keyed_sgl = 1; 1835 cdata->sgls.sgl_offset = 1; 1836 cdata->fuses.compare_and_write = 1; 1837 cdata->acwu = 1; 1838 spdk_strcpy_pad(cdata->subnqn, subsystem->subnqn, sizeof(cdata->subnqn), '\0'); 1839 1840 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ctrlr data: maxcmd 0x%x\n", cdata->maxcmd); 1841 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "sgls data: 0x%x\n", from_le32(&cdata->sgls)); 1842 1843 /* 1844 * NVM subsystem fields (reserved for discovery subsystems) 1845 */ 1846 if (subsystem->subtype == SPDK_NVMF_SUBTYPE_NVME) { 1847 spdk_strcpy_pad(cdata->mn, spdk_nvmf_subsystem_get_mn(subsystem), sizeof(cdata->mn), ' '); 1848 spdk_strcpy_pad(cdata->sn, spdk_nvmf_subsystem_get_sn(subsystem), sizeof(cdata->sn), ' '); 1849 cdata->kas = KAS_DEFAULT_VALUE; 1850 1851 cdata->rab = 6; 1852 cdata->cmic.multi_port = 1; 1853 cdata->cmic.multi_host = 1; 1854 cdata->oaes.ns_attribute_notices = 1; 1855 cdata->ctratt.host_id_exhid_supported = 1; 1856 cdata->aerl = 0; 1857 cdata->frmw.slot1_ro = 1; 1858 cdata->frmw.num_slots = 1; 1859 1860 cdata->lpa.celp = 1; /* Command Effects log page supported */ 1861 1862 cdata->sqes.min = 6; 1863 cdata->sqes.max = 6; 1864 cdata->cqes.min = 4; 1865 cdata->cqes.max = 4; 1866 cdata->nn = subsystem->max_nsid; 1867 cdata->vwc.present = 1; 1868 cdata->vwc.flush_broadcast = SPDK_NVME_FLUSH_BROADCAST_NOT_SUPPORTED; 1869 1870 cdata->nvmf_specific.ioccsz = sizeof(struct spdk_nvme_cmd) / 16; 1871 cdata->nvmf_specific.iorcsz = sizeof(struct spdk_nvme_cpl) / 16; 1872 cdata->nvmf_specific.icdoff = 0; /* offset starts directly after SQE */ 1873 cdata->nvmf_specific.ctrattr.ctrlr_model = SPDK_NVMF_CTRLR_MODEL_DYNAMIC; 1874 /* The RDMA transport supports up to SPDK_NVMF_MAX_SGL_ENTRIES descriptors. */ 1875 if (transport->ops->type == SPDK_NVME_TRANSPORT_RDMA) { 1876 cdata->nvmf_specific.msdbd = SPDK_NVMF_MAX_SGL_ENTRIES; 1877 } else { 1878 cdata->nvmf_specific.msdbd = 1; 1879 } 1880 1881 /* TODO: this should be set by the transport */ 1882 /* Disable in-capsule data transfer for RDMA controller when dif_insert_or_strip is enabled 1883 since in-capsule data only works with NVME drives that support SGL memory layout */ 1884 if (!(transport->ops->type == SPDK_NVME_TRANSPORT_RDMA && ctrlr->dif_insert_or_strip)) { 1885 cdata->nvmf_specific.ioccsz += transport->opts.in_capsule_data_size / 16; 1886 } 1887 1888 cdata->oncs.dsm = spdk_nvmf_ctrlr_dsm_supported(ctrlr); 1889 cdata->oncs.write_zeroes = spdk_nvmf_ctrlr_write_zeroes_supported(ctrlr); 1890 cdata->oncs.reservations = 1; 1891 1892 nvmf_ctrlr_populate_oacs(ctrlr, cdata); 1893 1894 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: ioccsz 0x%x\n", 1895 cdata->nvmf_specific.ioccsz); 1896 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: iorcsz 0x%x\n", 1897 cdata->nvmf_specific.iorcsz); 1898 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: icdoff 0x%x\n", 1899 cdata->nvmf_specific.icdoff); 1900 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: ctrattr 0x%x\n", 1901 *(uint8_t *)&cdata->nvmf_specific.ctrattr); 1902 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ext ctrlr data: msdbd 0x%x\n", 1903 cdata->nvmf_specific.msdbd); 1904 } 1905 1906 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1907 } 1908 1909 static int 1910 spdk_nvmf_ctrlr_identify_active_ns_list(struct spdk_nvmf_subsystem *subsystem, 1911 struct spdk_nvme_cmd *cmd, 1912 struct spdk_nvme_cpl *rsp, 1913 struct spdk_nvme_ns_list *ns_list) 1914 { 1915 struct spdk_nvmf_ns *ns; 1916 uint32_t count = 0; 1917 1918 if (cmd->nsid >= 0xfffffffeUL) { 1919 SPDK_ERRLOG("Identify Active Namespace List with invalid NSID %u\n", cmd->nsid); 1920 rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 1921 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1922 } 1923 1924 for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL; 1925 ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) { 1926 if (ns->opts.nsid <= cmd->nsid) { 1927 continue; 1928 } 1929 1930 ns_list->ns_list[count++] = ns->opts.nsid; 1931 if (count == SPDK_COUNTOF(ns_list->ns_list)) { 1932 break; 1933 } 1934 } 1935 1936 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1937 } 1938 1939 static void 1940 _add_ns_id_desc(void **buf_ptr, size_t *buf_remain, 1941 enum spdk_nvme_nidt type, 1942 const void *data, size_t data_size) 1943 { 1944 struct spdk_nvme_ns_id_desc *desc; 1945 size_t desc_size = sizeof(*desc) + data_size; 1946 1947 /* 1948 * These should never fail in practice, since all valid NS ID descriptors 1949 * should be defined so that they fit in the available 4096-byte buffer. 1950 */ 1951 assert(data_size > 0); 1952 assert(data_size <= UINT8_MAX); 1953 assert(desc_size < *buf_remain); 1954 if (data_size == 0 || data_size > UINT8_MAX || desc_size > *buf_remain) { 1955 return; 1956 } 1957 1958 desc = *buf_ptr; 1959 desc->nidt = type; 1960 desc->nidl = data_size; 1961 memcpy(desc->nid, data, data_size); 1962 1963 *buf_ptr += desc_size; 1964 *buf_remain -= desc_size; 1965 } 1966 1967 static int 1968 spdk_nvmf_ctrlr_identify_ns_id_descriptor_list( 1969 struct spdk_nvmf_subsystem *subsystem, 1970 struct spdk_nvme_cmd *cmd, 1971 struct spdk_nvme_cpl *rsp, 1972 void *id_desc_list, size_t id_desc_list_size) 1973 { 1974 struct spdk_nvmf_ns *ns; 1975 size_t buf_remain = id_desc_list_size; 1976 void *buf_ptr = id_desc_list; 1977 1978 ns = _spdk_nvmf_subsystem_get_ns(subsystem, cmd->nsid); 1979 if (ns == NULL || ns->bdev == NULL) { 1980 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1981 rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 1982 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1983 } 1984 1985 #define ADD_ID_DESC(type, data, size) \ 1986 do { \ 1987 if (!spdk_mem_all_zero(data, size)) { \ 1988 _add_ns_id_desc(&buf_ptr, &buf_remain, type, data, size); \ 1989 } \ 1990 } while (0) 1991 1992 ADD_ID_DESC(SPDK_NVME_NIDT_EUI64, ns->opts.eui64, sizeof(ns->opts.eui64)); 1993 ADD_ID_DESC(SPDK_NVME_NIDT_NGUID, ns->opts.nguid, sizeof(ns->opts.nguid)); 1994 ADD_ID_DESC(SPDK_NVME_NIDT_UUID, &ns->opts.uuid, sizeof(ns->opts.uuid)); 1995 1996 /* 1997 * The list is automatically 0-terminated because controller to host buffers in 1998 * admin commands always get zeroed in spdk_nvmf_ctrlr_process_admin_cmd(). 1999 */ 2000 2001 #undef ADD_ID_DESC 2002 2003 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2004 } 2005 2006 static int 2007 spdk_nvmf_ctrlr_identify(struct spdk_nvmf_request *req) 2008 { 2009 uint8_t cns; 2010 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2011 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2012 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 2013 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 2014 2015 if (req->data == NULL || req->length < 4096) { 2016 SPDK_ERRLOG("identify command with invalid buffer\n"); 2017 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2018 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2019 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2020 } 2021 2022 cns = cmd->cdw10_bits.identify.cns; 2023 2024 if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY && 2025 cns != SPDK_NVME_IDENTIFY_CTRLR) { 2026 /* Discovery controllers only support Identify Controller */ 2027 goto invalid_cns; 2028 } 2029 2030 switch (cns) { 2031 case SPDK_NVME_IDENTIFY_NS: 2032 return spdk_nvmf_ctrlr_identify_ns(ctrlr, cmd, rsp, req->data); 2033 case SPDK_NVME_IDENTIFY_CTRLR: 2034 return spdk_nvmf_ctrlr_identify_ctrlr(ctrlr, req->data); 2035 case SPDK_NVME_IDENTIFY_ACTIVE_NS_LIST: 2036 return spdk_nvmf_ctrlr_identify_active_ns_list(subsystem, cmd, rsp, req->data); 2037 case SPDK_NVME_IDENTIFY_NS_ID_DESCRIPTOR_LIST: 2038 return spdk_nvmf_ctrlr_identify_ns_id_descriptor_list(subsystem, cmd, rsp, req->data, req->length); 2039 default: 2040 goto invalid_cns; 2041 } 2042 2043 invalid_cns: 2044 SPDK_ERRLOG("Identify command with unsupported CNS 0x%02x\n", cns); 2045 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2046 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2047 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2048 } 2049 2050 2051 static struct spdk_nvmf_request * 2052 spdk_nvmf_qpair_abort(struct spdk_nvmf_qpair *qpair, uint16_t cid) 2053 { 2054 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 2055 struct spdk_nvmf_request *req; 2056 2057 if (spdk_nvmf_qpair_is_admin_queue(qpair)) { 2058 if (ctrlr->aer_req && ctrlr->aer_req->cmd->nvme_cmd.cid == cid) { 2059 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Aborting AER request\n"); 2060 req = ctrlr->aer_req; 2061 ctrlr->aer_req = NULL; 2062 return req; 2063 } 2064 } 2065 2066 /* TODO: track list of outstanding requests in qpair? */ 2067 return NULL; 2068 } 2069 2070 static void 2071 spdk_nvmf_ctrlr_abort_done(struct spdk_io_channel_iter *i, int status) 2072 { 2073 struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i); 2074 2075 spdk_nvmf_request_complete(req); 2076 } 2077 2078 static void 2079 spdk_nvmf_ctrlr_abort_on_pg(struct spdk_io_channel_iter *i) 2080 { 2081 struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i); 2082 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 2083 struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch); 2084 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 2085 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2086 uint16_t sqid = cmd->cdw10_bits.abort.sqid; 2087 struct spdk_nvmf_qpair *qpair; 2088 2089 TAILQ_FOREACH(qpair, &group->qpairs, link) { 2090 if (qpair->ctrlr == req->qpair->ctrlr && qpair->qid == sqid) { 2091 struct spdk_nvmf_request *req_to_abort; 2092 uint16_t cid = cmd->cdw10_bits.abort.cid; 2093 2094 /* Found the qpair */ 2095 2096 req_to_abort = spdk_nvmf_qpair_abort(qpair, cid); 2097 if (req_to_abort == NULL) { 2098 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "cid %u not found\n", cid); 2099 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2100 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2101 spdk_for_each_channel_continue(i, -EINVAL); 2102 return; 2103 } 2104 2105 /* Complete the request with aborted status */ 2106 req_to_abort->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2107 req_to_abort->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_ABORTED_BY_REQUEST; 2108 spdk_nvmf_request_complete(req_to_abort); 2109 2110 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "abort ctrlr=%p req=%p sqid=%u cid=%u successful\n", 2111 qpair->ctrlr, req_to_abort, sqid, cid); 2112 rsp->cdw0 = 0; /* Command successfully aborted */ 2113 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2114 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 2115 /* Return -1 for the status so the iteration across threads stops. */ 2116 spdk_for_each_channel_continue(i, -1); 2117 2118 } 2119 } 2120 2121 spdk_for_each_channel_continue(i, 0); 2122 } 2123 2124 static int 2125 spdk_nvmf_ctrlr_abort(struct spdk_nvmf_request *req) 2126 { 2127 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 2128 2129 rsp->cdw0 = 1; /* Command not aborted */ 2130 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 2131 rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER; 2132 2133 /* Send a message to each poll group, searching for this ctrlr, sqid, and command. */ 2134 spdk_for_each_channel(req->qpair->ctrlr->subsys->tgt, 2135 spdk_nvmf_ctrlr_abort_on_pg, 2136 req, 2137 spdk_nvmf_ctrlr_abort_done 2138 ); 2139 2140 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 2141 } 2142 2143 static int 2144 get_features_generic(struct spdk_nvmf_request *req, uint32_t cdw0) 2145 { 2146 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 2147 2148 rsp->cdw0 = cdw0; 2149 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2150 } 2151 2152 static int 2153 spdk_nvmf_ctrlr_get_features(struct spdk_nvmf_request *req) 2154 { 2155 uint8_t feature; 2156 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2157 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2158 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 2159 2160 feature = cmd->cdw10_bits.get_features.fid; 2161 switch (feature) { 2162 case SPDK_NVME_FEAT_ARBITRATION: 2163 return get_features_generic(req, ctrlr->feat.arbitration.raw); 2164 case SPDK_NVME_FEAT_POWER_MANAGEMENT: 2165 return get_features_generic(req, ctrlr->feat.power_management.raw); 2166 case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD: 2167 return spdk_nvmf_ctrlr_get_features_temperature_threshold(req); 2168 case SPDK_NVME_FEAT_ERROR_RECOVERY: 2169 return get_features_generic(req, ctrlr->feat.error_recovery.raw); 2170 case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE: 2171 return get_features_generic(req, ctrlr->feat.volatile_write_cache.raw); 2172 case SPDK_NVME_FEAT_NUMBER_OF_QUEUES: 2173 return get_features_generic(req, ctrlr->feat.number_of_queues.raw); 2174 case SPDK_NVME_FEAT_WRITE_ATOMICITY: 2175 return get_features_generic(req, ctrlr->feat.write_atomicity.raw); 2176 case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION: 2177 return get_features_generic(req, ctrlr->feat.async_event_configuration.raw); 2178 case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER: 2179 return get_features_generic(req, ctrlr->feat.keep_alive_timer.raw); 2180 case SPDK_NVME_FEAT_HOST_IDENTIFIER: 2181 return spdk_nvmf_ctrlr_get_features_host_identifier(req); 2182 case SPDK_NVME_FEAT_HOST_RESERVE_MASK: 2183 return spdk_nvmf_ctrlr_get_features_reservation_notification_mask(req); 2184 case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST: 2185 return spdk_nvmf_ctrlr_get_features_reservation_persistence(req); 2186 default: 2187 SPDK_ERRLOG("Get Features command with unsupported feature ID 0x%02x\n", feature); 2188 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2189 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2190 } 2191 } 2192 2193 static int 2194 spdk_nvmf_ctrlr_set_features(struct spdk_nvmf_request *req) 2195 { 2196 uint8_t feature, save; 2197 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2198 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 2199 2200 /* 2201 * Features are not saveable by the controller as indicated by 2202 * ONCS field of the Identify Controller data. 2203 * */ 2204 save = cmd->cdw10_bits.set_features.sv; 2205 if (save) { 2206 response->status.sc = SPDK_NVME_SC_FEATURE_ID_NOT_SAVEABLE; 2207 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 2208 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2209 } 2210 2211 feature = cmd->cdw10_bits.set_features.fid; 2212 switch (feature) { 2213 case SPDK_NVME_FEAT_ARBITRATION: 2214 return spdk_nvmf_ctrlr_set_features_arbitration(req); 2215 case SPDK_NVME_FEAT_POWER_MANAGEMENT: 2216 return spdk_nvmf_ctrlr_set_features_power_management(req); 2217 case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD: 2218 return spdk_nvmf_ctrlr_set_features_temperature_threshold(req); 2219 case SPDK_NVME_FEAT_ERROR_RECOVERY: 2220 return spdk_nvmf_ctrlr_set_features_error_recovery(req); 2221 case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE: 2222 return spdk_nvmf_ctrlr_set_features_volatile_write_cache(req); 2223 case SPDK_NVME_FEAT_NUMBER_OF_QUEUES: 2224 return spdk_nvmf_ctrlr_set_features_number_of_queues(req); 2225 case SPDK_NVME_FEAT_WRITE_ATOMICITY: 2226 return spdk_nvmf_ctrlr_set_features_write_atomicity(req); 2227 case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION: 2228 return spdk_nvmf_ctrlr_set_features_async_event_configuration(req); 2229 case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER: 2230 return spdk_nvmf_ctrlr_set_features_keep_alive_timer(req); 2231 case SPDK_NVME_FEAT_HOST_IDENTIFIER: 2232 return spdk_nvmf_ctrlr_set_features_host_identifier(req); 2233 case SPDK_NVME_FEAT_HOST_RESERVE_MASK: 2234 return spdk_nvmf_ctrlr_set_features_reservation_notification_mask(req); 2235 case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST: 2236 return spdk_nvmf_ctrlr_set_features_reservation_persistence(req); 2237 default: 2238 SPDK_ERRLOG("Set Features command with unsupported feature ID 0x%02x\n", feature); 2239 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2240 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2241 } 2242 } 2243 2244 static int 2245 spdk_nvmf_ctrlr_keep_alive(struct spdk_nvmf_request *req) 2246 { 2247 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2248 2249 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Keep Alive\n"); 2250 /* 2251 * To handle keep alive just clear or reset the 2252 * ctrlr based keep alive duration counter. 2253 * When added, a separate timer based process 2254 * will monitor if the time since last recorded 2255 * keep alive has exceeded the max duration and 2256 * take appropriate action. 2257 */ 2258 ctrlr->last_keep_alive_tick = spdk_get_ticks(); 2259 2260 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2261 } 2262 2263 int 2264 spdk_nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req) 2265 { 2266 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2267 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2268 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 2269 int rc; 2270 2271 if (ctrlr == NULL) { 2272 SPDK_ERRLOG("Admin command sent before CONNECT\n"); 2273 response->status.sct = SPDK_NVME_SCT_GENERIC; 2274 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 2275 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2276 } 2277 2278 if (ctrlr->vcprop.cc.bits.en != 1) { 2279 SPDK_ERRLOG("Admin command sent to disabled controller\n"); 2280 response->status.sct = SPDK_NVME_SCT_GENERIC; 2281 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 2282 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2283 } 2284 2285 if (req->data && spdk_nvme_opc_get_data_transfer(cmd->opc) == SPDK_NVME_DATA_CONTROLLER_TO_HOST) { 2286 memset(req->data, 0, req->length); 2287 } 2288 2289 if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 2290 /* Discovery controllers only support Get Log Page, Identify and Keep Alive. */ 2291 switch (cmd->opc) { 2292 case SPDK_NVME_OPC_IDENTIFY: 2293 case SPDK_NVME_OPC_GET_LOG_PAGE: 2294 case SPDK_NVME_OPC_KEEP_ALIVE: 2295 break; 2296 default: 2297 goto invalid_opcode; 2298 } 2299 } 2300 2301 if (g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].hdlr) { 2302 rc = g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].hdlr(req); 2303 if (rc >= SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 2304 /* The handler took care of this commmand */ 2305 return rc; 2306 } 2307 } 2308 2309 switch (cmd->opc) { 2310 case SPDK_NVME_OPC_GET_LOG_PAGE: 2311 return spdk_nvmf_ctrlr_get_log_page(req); 2312 case SPDK_NVME_OPC_IDENTIFY: 2313 return spdk_nvmf_ctrlr_identify(req); 2314 case SPDK_NVME_OPC_ABORT: 2315 return spdk_nvmf_ctrlr_abort(req); 2316 case SPDK_NVME_OPC_GET_FEATURES: 2317 return spdk_nvmf_ctrlr_get_features(req); 2318 case SPDK_NVME_OPC_SET_FEATURES: 2319 return spdk_nvmf_ctrlr_set_features(req); 2320 case SPDK_NVME_OPC_ASYNC_EVENT_REQUEST: 2321 return spdk_nvmf_ctrlr_async_event_request(req); 2322 case SPDK_NVME_OPC_KEEP_ALIVE: 2323 return spdk_nvmf_ctrlr_keep_alive(req); 2324 2325 case SPDK_NVME_OPC_CREATE_IO_SQ: 2326 case SPDK_NVME_OPC_CREATE_IO_CQ: 2327 case SPDK_NVME_OPC_DELETE_IO_SQ: 2328 case SPDK_NVME_OPC_DELETE_IO_CQ: 2329 /* Create and Delete I/O CQ/SQ not allowed in NVMe-oF */ 2330 goto invalid_opcode; 2331 2332 default: 2333 goto invalid_opcode; 2334 } 2335 2336 invalid_opcode: 2337 SPDK_ERRLOG("Unsupported admin opcode 0x%x\n", cmd->opc); 2338 response->status.sct = SPDK_NVME_SCT_GENERIC; 2339 response->status.sc = SPDK_NVME_SC_INVALID_OPCODE; 2340 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2341 } 2342 2343 int 2344 spdk_nvmf_ctrlr_process_fabrics_cmd(struct spdk_nvmf_request *req) 2345 { 2346 struct spdk_nvmf_qpair *qpair = req->qpair; 2347 struct spdk_nvmf_capsule_cmd *cap_hdr; 2348 2349 cap_hdr = &req->cmd->nvmf_cmd; 2350 2351 if (qpair->ctrlr == NULL) { 2352 /* No ctrlr established yet; the only valid command is Connect */ 2353 if (cap_hdr->fctype == SPDK_NVMF_FABRIC_COMMAND_CONNECT) { 2354 return spdk_nvmf_ctrlr_cmd_connect(req); 2355 } else { 2356 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Got fctype 0x%x, expected Connect\n", 2357 cap_hdr->fctype); 2358 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2359 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 2360 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2361 } 2362 } else if (spdk_nvmf_qpair_is_admin_queue(qpair)) { 2363 /* 2364 * Controller session is established, and this is an admin queue. 2365 * Disallow Connect and allow other fabrics commands. 2366 */ 2367 switch (cap_hdr->fctype) { 2368 case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_SET: 2369 return spdk_nvmf_property_set(req); 2370 case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_GET: 2371 return spdk_nvmf_property_get(req); 2372 default: 2373 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "unknown fctype 0x%02x\n", 2374 cap_hdr->fctype); 2375 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2376 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE; 2377 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2378 } 2379 } else { 2380 /* Controller session is established, and this is an I/O queue */ 2381 /* For now, no I/O-specific Fabrics commands are implemented (other than Connect) */ 2382 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Unexpected I/O fctype 0x%x\n", cap_hdr->fctype); 2383 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2384 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE; 2385 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2386 } 2387 } 2388 2389 int 2390 spdk_nvmf_ctrlr_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr) 2391 { 2392 struct spdk_nvmf_request *req; 2393 struct spdk_nvme_cpl *rsp; 2394 union spdk_nvme_async_event_completion event = {0}; 2395 2396 /* Users may disable the event notification */ 2397 if (!ctrlr->feat.async_event_configuration.bits.ns_attr_notice) { 2398 return 0; 2399 } 2400 2401 event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE; 2402 event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGED; 2403 event.bits.log_page_identifier = SPDK_NVME_LOG_CHANGED_NS_LIST; 2404 2405 /* If there is no outstanding AER request, queue the event. Then 2406 * if an AER is later submitted, this event can be sent as a 2407 * response. 2408 */ 2409 if (!ctrlr->aer_req) { 2410 if (ctrlr->notice_event.bits.async_event_type == 2411 SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) { 2412 return 0; 2413 } 2414 2415 ctrlr->notice_event.raw = event.raw; 2416 return 0; 2417 } 2418 2419 req = ctrlr->aer_req; 2420 rsp = &req->rsp->nvme_cpl; 2421 2422 rsp->cdw0 = event.raw; 2423 2424 spdk_nvmf_request_complete(req); 2425 ctrlr->aer_req = NULL; 2426 2427 return 0; 2428 } 2429 2430 void 2431 spdk_nvmf_ctrlr_async_event_reservation_notification(struct spdk_nvmf_ctrlr *ctrlr) 2432 { 2433 struct spdk_nvmf_request *req; 2434 struct spdk_nvme_cpl *rsp; 2435 union spdk_nvme_async_event_completion event = {0}; 2436 2437 if (!ctrlr->num_avail_log_pages) { 2438 return; 2439 } 2440 event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_IO; 2441 event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_RESERVATION_LOG_AVAIL; 2442 event.bits.log_page_identifier = SPDK_NVME_LOG_RESERVATION_NOTIFICATION; 2443 2444 /* If there is no outstanding AER request, queue the event. Then 2445 * if an AER is later submitted, this event can be sent as a 2446 * response. 2447 */ 2448 if (!ctrlr->aer_req) { 2449 if (ctrlr->reservation_event.bits.async_event_type == 2450 SPDK_NVME_ASYNC_EVENT_TYPE_IO) { 2451 return; 2452 } 2453 2454 ctrlr->reservation_event.raw = event.raw; 2455 return; 2456 } 2457 2458 req = ctrlr->aer_req; 2459 rsp = &req->rsp->nvme_cpl; 2460 2461 rsp->cdw0 = event.raw; 2462 2463 spdk_nvmf_request_complete(req); 2464 ctrlr->aer_req = NULL; 2465 } 2466 2467 void 2468 spdk_nvmf_qpair_free_aer(struct spdk_nvmf_qpair *qpair) 2469 { 2470 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 2471 2472 if (!spdk_nvmf_qpair_is_admin_queue(qpair)) { 2473 return; 2474 } 2475 2476 if (ctrlr->aer_req != NULL) { 2477 spdk_nvmf_request_free(ctrlr->aer_req); 2478 ctrlr->aer_req = NULL; 2479 } 2480 } 2481 2482 void 2483 spdk_nvmf_ctrlr_abort_aer(struct spdk_nvmf_ctrlr *ctrlr) 2484 { 2485 if (!ctrlr->aer_req) { 2486 return; 2487 } 2488 2489 spdk_nvmf_request_complete(ctrlr->aer_req); 2490 ctrlr->aer_req = NULL; 2491 } 2492 2493 static void 2494 _nvmf_ctrlr_add_reservation_log(void *ctx) 2495 { 2496 struct spdk_nvmf_reservation_log *log = (struct spdk_nvmf_reservation_log *)ctx; 2497 struct spdk_nvmf_ctrlr *ctrlr = log->ctrlr; 2498 2499 ctrlr->log_page_count++; 2500 2501 /* Maximum number of queued log pages is 255 */ 2502 if (ctrlr->num_avail_log_pages == 0xff) { 2503 struct spdk_nvmf_reservation_log *entry; 2504 entry = TAILQ_LAST(&ctrlr->log_head, log_page_head); 2505 entry->log.log_page_count = ctrlr->log_page_count; 2506 free(log); 2507 return; 2508 } 2509 2510 log->log.log_page_count = ctrlr->log_page_count; 2511 log->log.num_avail_log_pages = ctrlr->num_avail_log_pages++; 2512 TAILQ_INSERT_TAIL(&ctrlr->log_head, log, link); 2513 2514 spdk_nvmf_ctrlr_async_event_reservation_notification(ctrlr); 2515 } 2516 2517 void 2518 spdk_nvmf_ctrlr_reservation_notice_log(struct spdk_nvmf_ctrlr *ctrlr, 2519 struct spdk_nvmf_ns *ns, 2520 enum spdk_nvme_reservation_notification_log_page_type type) 2521 { 2522 struct spdk_nvmf_reservation_log *log; 2523 2524 switch (type) { 2525 case SPDK_NVME_RESERVATION_LOG_PAGE_EMPTY: 2526 return; 2527 case SPDK_NVME_REGISTRATION_PREEMPTED: 2528 if (ns->mask & SPDK_NVME_REGISTRATION_PREEMPTED_MASK) { 2529 return; 2530 } 2531 break; 2532 case SPDK_NVME_RESERVATION_RELEASED: 2533 if (ns->mask & SPDK_NVME_RESERVATION_RELEASED_MASK) { 2534 return; 2535 } 2536 break; 2537 case SPDK_NVME_RESERVATION_PREEMPTED: 2538 if (ns->mask & SPDK_NVME_RESERVATION_PREEMPTED_MASK) { 2539 return; 2540 } 2541 break; 2542 default: 2543 return; 2544 } 2545 2546 log = calloc(1, sizeof(*log)); 2547 if (!log) { 2548 SPDK_ERRLOG("Alloc log page failed, ignore the log\n"); 2549 return; 2550 } 2551 log->ctrlr = ctrlr; 2552 log->log.type = type; 2553 log->log.nsid = ns->nsid; 2554 2555 spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_add_reservation_log, log); 2556 } 2557 2558 /* Check from subsystem poll group's namespace information data structure */ 2559 static bool 2560 nvmf_ns_info_ctrlr_is_registrant(struct spdk_nvmf_subsystem_pg_ns_info *ns_info, 2561 struct spdk_nvmf_ctrlr *ctrlr) 2562 { 2563 uint32_t i; 2564 2565 for (i = 0; i < SPDK_NVMF_MAX_NUM_REGISTRANTS; i++) { 2566 if (!spdk_uuid_compare(&ns_info->reg_hostid[i], &ctrlr->hostid)) { 2567 return true; 2568 } 2569 } 2570 2571 return false; 2572 } 2573 2574 /* 2575 * Check the NVMe command is permitted or not for current controller(Host). 2576 */ 2577 static int 2578 nvmf_ns_reservation_request_check(struct spdk_nvmf_subsystem_pg_ns_info *ns_info, 2579 struct spdk_nvmf_ctrlr *ctrlr, 2580 struct spdk_nvmf_request *req) 2581 { 2582 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2583 enum spdk_nvme_reservation_type rtype = ns_info->rtype; 2584 uint8_t status = SPDK_NVME_SC_SUCCESS; 2585 uint8_t racqa; 2586 bool is_registrant; 2587 2588 /* No valid reservation */ 2589 if (!rtype) { 2590 return 0; 2591 } 2592 2593 is_registrant = nvmf_ns_info_ctrlr_is_registrant(ns_info, ctrlr); 2594 /* All registrants type and current ctrlr is a valid registrant */ 2595 if ((rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS || 2596 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && is_registrant) { 2597 return 0; 2598 } else if (!spdk_uuid_compare(&ns_info->holder_id, &ctrlr->hostid)) { 2599 return 0; 2600 } 2601 2602 /* Non-holder for current controller */ 2603 switch (cmd->opc) { 2604 case SPDK_NVME_OPC_READ: 2605 case SPDK_NVME_OPC_COMPARE: 2606 if (rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) { 2607 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2608 goto exit; 2609 } 2610 if ((rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY || 2611 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && !is_registrant) { 2612 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2613 } 2614 break; 2615 case SPDK_NVME_OPC_FLUSH: 2616 case SPDK_NVME_OPC_WRITE: 2617 case SPDK_NVME_OPC_WRITE_UNCORRECTABLE: 2618 case SPDK_NVME_OPC_WRITE_ZEROES: 2619 case SPDK_NVME_OPC_DATASET_MANAGEMENT: 2620 if (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE || 2621 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) { 2622 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2623 goto exit; 2624 } 2625 if (!is_registrant) { 2626 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2627 } 2628 break; 2629 case SPDK_NVME_OPC_RESERVATION_ACQUIRE: 2630 racqa = cmd->cdw10_bits.resv_acquire.racqa; 2631 if (racqa == SPDK_NVME_RESERVE_ACQUIRE) { 2632 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2633 goto exit; 2634 } 2635 if (!is_registrant) { 2636 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2637 } 2638 break; 2639 case SPDK_NVME_OPC_RESERVATION_RELEASE: 2640 if (!is_registrant) { 2641 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2642 } 2643 break; 2644 default: 2645 break; 2646 } 2647 2648 exit: 2649 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2650 req->rsp->nvme_cpl.status.sc = status; 2651 if (status == SPDK_NVME_SC_RESERVATION_CONFLICT) { 2652 return -EPERM; 2653 } 2654 2655 return 0; 2656 } 2657 2658 static int 2659 spdk_nvmf_ctrlr_process_io_fused_cmd(struct spdk_nvmf_request *req, struct spdk_bdev *bdev, 2660 struct spdk_bdev_desc *desc, struct spdk_io_channel *ch) 2661 { 2662 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2663 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 2664 struct spdk_nvmf_request *first_fused_req = req->qpair->first_fused_req; 2665 int rc; 2666 2667 if (cmd->fuse == SPDK_NVME_CMD_FUSE_FIRST) { 2668 /* first fused operation (should be compare) */ 2669 if (first_fused_req != NULL) { 2670 struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl; 2671 2672 SPDK_ERRLOG("Wrong sequence of fused operations\n"); 2673 2674 /* abort req->qpair->first_fused_request and continue with new fused command */ 2675 fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED; 2676 fused_response->status.sct = SPDK_NVME_SCT_GENERIC; 2677 spdk_nvmf_request_complete(first_fused_req); 2678 } else if (cmd->opc != SPDK_NVME_OPC_COMPARE) { 2679 SPDK_ERRLOG("Wrong op code of fused operations\n"); 2680 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2681 rsp->status.sc = SPDK_NVME_SC_INVALID_OPCODE; 2682 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2683 } 2684 2685 req->qpair->first_fused_req = req; 2686 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 2687 } else if (cmd->fuse == SPDK_NVME_CMD_FUSE_SECOND) { 2688 /* second fused operation (should be write) */ 2689 if (first_fused_req == NULL) { 2690 SPDK_ERRLOG("Wrong sequence of fused operations\n"); 2691 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2692 rsp->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED; 2693 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2694 } else if (cmd->opc != SPDK_NVME_OPC_WRITE) { 2695 struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl; 2696 2697 SPDK_ERRLOG("Wrong op code of fused operations\n"); 2698 2699 /* abort req->qpair->first_fused_request and fail current command */ 2700 fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED; 2701 fused_response->status.sct = SPDK_NVME_SCT_GENERIC; 2702 spdk_nvmf_request_complete(first_fused_req); 2703 2704 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2705 rsp->status.sc = SPDK_NVME_SC_INVALID_OPCODE; 2706 req->qpair->first_fused_req = NULL; 2707 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2708 } 2709 2710 /* save request of first command to generate response later */ 2711 req->first_fused_req = first_fused_req; 2712 req->qpair->first_fused_req = NULL; 2713 } else { 2714 SPDK_ERRLOG("Invalid fused command fuse field.\n"); 2715 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2716 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2717 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2718 } 2719 2720 rc = spdk_nvmf_bdev_ctrlr_compare_and_write_cmd(bdev, desc, ch, req->first_fused_req, req); 2721 2722 if (rc == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 2723 if (spdk_nvme_cpl_is_error(rsp)) { 2724 struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl; 2725 2726 fused_response->status = rsp->status; 2727 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2728 rsp->status.sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED; 2729 /* Complete first of fused commands. Second will be completed by upper layer */ 2730 spdk_nvmf_request_complete(first_fused_req); 2731 req->first_fused_req = NULL; 2732 } 2733 } 2734 2735 return rc; 2736 } 2737 2738 int 2739 spdk_nvmf_ctrlr_process_io_cmd(struct spdk_nvmf_request *req) 2740 { 2741 uint32_t nsid; 2742 struct spdk_nvmf_ns *ns; 2743 struct spdk_bdev *bdev; 2744 struct spdk_bdev_desc *desc; 2745 struct spdk_io_channel *ch; 2746 struct spdk_nvmf_poll_group *group = req->qpair->group; 2747 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2748 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2749 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 2750 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 2751 2752 /* pre-set response details for this command */ 2753 response->status.sc = SPDK_NVME_SC_SUCCESS; 2754 nsid = cmd->nsid; 2755 2756 if (spdk_unlikely(ctrlr == NULL)) { 2757 SPDK_ERRLOG("I/O command sent before CONNECT\n"); 2758 response->status.sct = SPDK_NVME_SCT_GENERIC; 2759 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 2760 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2761 } 2762 2763 if (spdk_unlikely(ctrlr->vcprop.cc.bits.en != 1)) { 2764 SPDK_ERRLOG("I/O command sent to disabled controller\n"); 2765 response->status.sct = SPDK_NVME_SCT_GENERIC; 2766 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 2767 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2768 } 2769 2770 ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, nsid); 2771 if (ns == NULL || ns->bdev == NULL) { 2772 SPDK_ERRLOG("Unsuccessful query for nsid %u\n", cmd->nsid); 2773 response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 2774 response->status.dnr = 1; 2775 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2776 } 2777 2778 /* scan-build falsely reporting dereference of null pointer */ 2779 assert(group != NULL && group->sgroups != NULL); 2780 ns_info = &group->sgroups[ctrlr->subsys->id].ns_info[nsid - 1]; 2781 if (nvmf_ns_reservation_request_check(ns_info, ctrlr, req)) { 2782 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Reservation Conflict for nsid %u, opcode %u\n", 2783 cmd->nsid, cmd->opc); 2784 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2785 } 2786 2787 bdev = ns->bdev; 2788 desc = ns->desc; 2789 ch = ns_info->channel; 2790 2791 if (spdk_unlikely(cmd->fuse & SPDK_NVME_CMD_FUSE_MASK)) { 2792 return spdk_nvmf_ctrlr_process_io_fused_cmd(req, bdev, desc, ch); 2793 } else if (spdk_unlikely(req->qpair->first_fused_req != NULL)) { 2794 struct spdk_nvme_cpl *fused_response = &req->qpair->first_fused_req->rsp->nvme_cpl; 2795 2796 SPDK_ERRLOG("Expected second of fused commands - failing first of fused commands\n"); 2797 2798 /* abort req->qpair->first_fused_request and continue with new command */ 2799 fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED; 2800 fused_response->status.sct = SPDK_NVME_SCT_GENERIC; 2801 spdk_nvmf_request_complete(req->qpair->first_fused_req); 2802 req->qpair->first_fused_req = NULL; 2803 } 2804 2805 switch (cmd->opc) { 2806 case SPDK_NVME_OPC_READ: 2807 return spdk_nvmf_bdev_ctrlr_read_cmd(bdev, desc, ch, req); 2808 case SPDK_NVME_OPC_WRITE: 2809 return spdk_nvmf_bdev_ctrlr_write_cmd(bdev, desc, ch, req); 2810 case SPDK_NVME_OPC_COMPARE: 2811 return spdk_nvmf_bdev_ctrlr_compare_cmd(bdev, desc, ch, req); 2812 case SPDK_NVME_OPC_WRITE_ZEROES: 2813 return spdk_nvmf_bdev_ctrlr_write_zeroes_cmd(bdev, desc, ch, req); 2814 case SPDK_NVME_OPC_FLUSH: 2815 return spdk_nvmf_bdev_ctrlr_flush_cmd(bdev, desc, ch, req); 2816 case SPDK_NVME_OPC_DATASET_MANAGEMENT: 2817 return spdk_nvmf_bdev_ctrlr_dsm_cmd(bdev, desc, ch, req); 2818 case SPDK_NVME_OPC_RESERVATION_REGISTER: 2819 case SPDK_NVME_OPC_RESERVATION_ACQUIRE: 2820 case SPDK_NVME_OPC_RESERVATION_RELEASE: 2821 case SPDK_NVME_OPC_RESERVATION_REPORT: 2822 spdk_thread_send_msg(ctrlr->subsys->thread, spdk_nvmf_ns_reservation_request, req); 2823 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 2824 default: 2825 return spdk_nvmf_bdev_ctrlr_nvme_passthru_io(bdev, desc, ch, req); 2826 } 2827 } 2828 2829 static void 2830 spdk_nvmf_qpair_request_cleanup(struct spdk_nvmf_qpair *qpair) 2831 { 2832 if (qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING) { 2833 assert(qpair->state_cb != NULL); 2834 2835 if (TAILQ_EMPTY(&qpair->outstanding)) { 2836 qpair->state_cb(qpair->state_cb_arg, 0); 2837 } 2838 } 2839 } 2840 2841 int 2842 spdk_nvmf_request_free(struct spdk_nvmf_request *req) 2843 { 2844 struct spdk_nvmf_qpair *qpair = req->qpair; 2845 2846 TAILQ_REMOVE(&qpair->outstanding, req, link); 2847 if (spdk_nvmf_transport_req_free(req)) { 2848 SPDK_ERRLOG("Unable to free transport level request resources.\n"); 2849 } 2850 2851 spdk_nvmf_qpair_request_cleanup(qpair); 2852 2853 return 0; 2854 } 2855 2856 int 2857 spdk_nvmf_request_complete(struct spdk_nvmf_request *req) 2858 { 2859 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 2860 struct spdk_nvmf_qpair *qpair; 2861 struct spdk_nvmf_subsystem_poll_group *sgroup = NULL; 2862 bool is_aer = false; 2863 2864 rsp->sqid = 0; 2865 rsp->status.p = 0; 2866 rsp->cid = req->cmd->nvme_cmd.cid; 2867 2868 qpair = req->qpair; 2869 if (qpair->ctrlr) { 2870 sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id]; 2871 is_aer = qpair->ctrlr->aer_req == req; 2872 } else if (spdk_unlikely(nvmf_request_is_fabric_connect(req))) { 2873 sgroup = nvmf_subsystem_pg_from_connect_cmd(req); 2874 } 2875 2876 SPDK_DEBUGLOG(SPDK_LOG_NVMF, 2877 "cpl: cdw0=0x%08x sct=0x%01x sc=0x%02x cid=0x%04x\n", 2878 rsp->cdw0, rsp->status.sct, rsp->status.sc, rsp->cid); 2879 2880 TAILQ_REMOVE(&qpair->outstanding, req, link); 2881 if (spdk_nvmf_transport_req_complete(req)) { 2882 SPDK_ERRLOG("Transport request completion error!\n"); 2883 } 2884 2885 /* AER cmd is an exception */ 2886 if (sgroup && !is_aer) { 2887 assert(sgroup->io_outstanding > 0); 2888 sgroup->io_outstanding--; 2889 if (sgroup->state == SPDK_NVMF_SUBSYSTEM_PAUSING && 2890 sgroup->io_outstanding == 0) { 2891 sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSED; 2892 sgroup->cb_fn(sgroup->cb_arg, 0); 2893 } 2894 } 2895 2896 spdk_nvmf_qpair_request_cleanup(qpair); 2897 2898 return 0; 2899 } 2900 2901 static void 2902 nvmf_trace_command(union nvmf_h2c_msg *h2c_msg, bool is_admin_queue) 2903 { 2904 struct spdk_nvmf_capsule_cmd *cap_hdr = &h2c_msg->nvmf_cmd; 2905 struct spdk_nvme_cmd *cmd = &h2c_msg->nvme_cmd; 2906 struct spdk_nvme_sgl_descriptor *sgl = &cmd->dptr.sgl1; 2907 uint8_t opc; 2908 2909 if (cmd->opc == SPDK_NVME_OPC_FABRIC) { 2910 opc = cap_hdr->fctype; 2911 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "%s Fabrics cmd: fctype 0x%02x cid %u\n", 2912 is_admin_queue ? "Admin" : "I/O", 2913 cap_hdr->fctype, cap_hdr->cid); 2914 } else { 2915 opc = cmd->opc; 2916 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "%s cmd: opc 0x%02x fuse %u cid %u nsid %u cdw10 0x%08x\n", 2917 is_admin_queue ? "Admin" : "I/O", 2918 cmd->opc, cmd->fuse, cmd->cid, cmd->nsid, cmd->cdw10); 2919 if (cmd->mptr) { 2920 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "mptr 0x%" PRIx64 "\n", cmd->mptr); 2921 } 2922 if (cmd->psdt != SPDK_NVME_PSDT_SGL_MPTR_CONTIG && 2923 cmd->psdt != SPDK_NVME_PSDT_SGL_MPTR_SGL) { 2924 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "psdt %u\n", cmd->psdt); 2925 } 2926 } 2927 2928 if (spdk_nvme_opc_get_data_transfer(opc) != SPDK_NVME_DATA_NONE) { 2929 if (sgl->generic.type == SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK) { 2930 SPDK_DEBUGLOG(SPDK_LOG_NVMF, 2931 "SGL: Keyed%s: addr 0x%" PRIx64 " key 0x%x len 0x%x\n", 2932 sgl->generic.subtype == SPDK_NVME_SGL_SUBTYPE_INVALIDATE_KEY ? " (Inv)" : "", 2933 sgl->address, sgl->keyed.key, sgl->keyed.length); 2934 } else if (sgl->generic.type == SPDK_NVME_SGL_TYPE_DATA_BLOCK) { 2935 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "SGL: Data block: %s 0x%" PRIx64 " len 0x%x\n", 2936 sgl->unkeyed.subtype == SPDK_NVME_SGL_SUBTYPE_OFFSET ? "offs" : "addr", 2937 sgl->address, sgl->unkeyed.length); 2938 } else { 2939 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "SGL type 0x%x subtype 0x%x\n", 2940 sgl->generic.type, sgl->generic.subtype); 2941 } 2942 } 2943 } 2944 2945 static void 2946 _nvmf_request_exec(struct spdk_nvmf_request *req, 2947 struct spdk_nvmf_subsystem_poll_group *sgroup) 2948 { 2949 struct spdk_nvmf_qpair *qpair = req->qpair; 2950 enum spdk_nvmf_request_exec_status status; 2951 2952 nvmf_trace_command(req->cmd, spdk_nvmf_qpair_is_admin_queue(qpair)); 2953 2954 if (sgroup) { 2955 sgroup->io_outstanding++; 2956 } 2957 2958 /* Place the request on the outstanding list so we can keep track of it */ 2959 TAILQ_INSERT_TAIL(&qpair->outstanding, req, link); 2960 2961 if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC)) { 2962 status = spdk_nvmf_ctrlr_process_fabrics_cmd(req); 2963 } else if (spdk_unlikely(spdk_nvmf_qpair_is_admin_queue(qpair))) { 2964 status = spdk_nvmf_ctrlr_process_admin_cmd(req); 2965 } else { 2966 status = spdk_nvmf_ctrlr_process_io_cmd(req); 2967 } 2968 2969 if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 2970 spdk_nvmf_request_complete(req); 2971 } 2972 } 2973 2974 void 2975 spdk_nvmf_request_exec_fabrics(struct spdk_nvmf_request *req) 2976 { 2977 struct spdk_nvmf_qpair *qpair = req->qpair; 2978 struct spdk_nvmf_subsystem_poll_group *sgroup = NULL; 2979 2980 assert(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC); 2981 2982 if (qpair->ctrlr) { 2983 sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id]; 2984 } 2985 2986 _nvmf_request_exec(req, sgroup); 2987 } 2988 2989 void 2990 spdk_nvmf_request_exec(struct spdk_nvmf_request *req) 2991 { 2992 struct spdk_nvmf_qpair *qpair = req->qpair; 2993 struct spdk_nvmf_subsystem_poll_group *sgroup = NULL; 2994 2995 if (qpair->ctrlr) { 2996 sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id]; 2997 } else if (spdk_unlikely(nvmf_request_is_fabric_connect(req))) { 2998 sgroup = nvmf_subsystem_pg_from_connect_cmd(req); 2999 } 3000 3001 if (qpair->state != SPDK_NVMF_QPAIR_ACTIVE) { 3002 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3003 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 3004 /* Place the request on the outstanding list so we can keep track of it */ 3005 TAILQ_INSERT_TAIL(&qpair->outstanding, req, link); 3006 /* Still increment io_outstanding because request_complete decrements it */ 3007 if (sgroup != NULL) { 3008 sgroup->io_outstanding++; 3009 } 3010 spdk_nvmf_request_complete(req); 3011 return; 3012 } 3013 3014 /* Check if the subsystem is paused (if there is a subsystem) */ 3015 if (sgroup != NULL) { 3016 if (sgroup->state != SPDK_NVMF_SUBSYSTEM_ACTIVE) { 3017 /* The subsystem is not currently active. Queue this request. */ 3018 TAILQ_INSERT_TAIL(&sgroup->queued, req, link); 3019 return; 3020 } 3021 } 3022 3023 _nvmf_request_exec(req, sgroup); 3024 } 3025 3026 static bool 3027 spdk_nvmf_ctrlr_get_dif_ctx(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_cmd *cmd, 3028 struct spdk_dif_ctx *dif_ctx) 3029 { 3030 struct spdk_nvmf_ns *ns; 3031 struct spdk_bdev *bdev; 3032 3033 if (ctrlr == NULL || cmd == NULL) { 3034 return false; 3035 } 3036 3037 ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 3038 if (ns == NULL || ns->bdev == NULL) { 3039 return false; 3040 } 3041 3042 bdev = ns->bdev; 3043 3044 switch (cmd->opc) { 3045 case SPDK_NVME_OPC_READ: 3046 case SPDK_NVME_OPC_WRITE: 3047 case SPDK_NVME_OPC_COMPARE: 3048 return spdk_nvmf_bdev_ctrlr_get_dif_ctx(bdev, cmd, dif_ctx); 3049 default: 3050 break; 3051 } 3052 3053 return false; 3054 } 3055 3056 bool 3057 spdk_nvmf_request_get_dif_ctx(struct spdk_nvmf_request *req, struct spdk_dif_ctx *dif_ctx) 3058 { 3059 struct spdk_nvmf_qpair *qpair = req->qpair; 3060 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 3061 3062 if (spdk_likely(ctrlr == NULL || !ctrlr->dif_insert_or_strip)) { 3063 return false; 3064 } 3065 3066 if (spdk_unlikely(qpair->state != SPDK_NVMF_QPAIR_ACTIVE)) { 3067 return false; 3068 } 3069 3070 if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC)) { 3071 return false; 3072 } 3073 3074 if (spdk_unlikely(spdk_nvmf_qpair_is_admin_queue(qpair))) { 3075 return false; 3076 } 3077 3078 return spdk_nvmf_ctrlr_get_dif_ctx(ctrlr, &req->cmd->nvme_cmd, dif_ctx); 3079 } 3080 3081 void 3082 spdk_nvmf_set_custom_admin_cmd_hdlr(uint8_t opc, spdk_nvmf_custom_cmd_hdlr hdlr) 3083 { 3084 g_nvmf_custom_admin_cmd_hdlrs[opc].hdlr = hdlr; 3085 } 3086 3087 static int 3088 nvmf_passthru_admin_cmd(struct spdk_nvmf_request *req) 3089 { 3090 struct spdk_bdev *bdev; 3091 struct spdk_bdev_desc *desc; 3092 struct spdk_io_channel *ch; 3093 struct spdk_nvme_cmd *cmd = spdk_nvmf_request_get_cmd(req); 3094 struct spdk_nvme_cpl *response = spdk_nvmf_request_get_response(req); 3095 uint32_t bdev_nsid; 3096 int rc; 3097 3098 if (g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].nsid == 0) { 3099 bdev_nsid = cmd->nsid; 3100 } else { 3101 bdev_nsid = g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].nsid; 3102 } 3103 3104 rc = spdk_nvmf_request_get_bdev(bdev_nsid, req, &bdev, &desc, &ch); 3105 if (rc) { 3106 response->status.sct = SPDK_NVME_SCT_GENERIC; 3107 response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 3108 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3109 } 3110 return spdk_nvmf_bdev_ctrlr_nvme_passthru_admin(bdev, desc, ch, req, NULL); 3111 } 3112 3113 void 3114 spdk_nvmf_set_passthru_admin_cmd(uint8_t opc, uint32_t forward_nsid) 3115 { 3116 g_nvmf_custom_admin_cmd_hdlrs[opc].hdlr = nvmf_passthru_admin_cmd; 3117 g_nvmf_custom_admin_cmd_hdlrs[opc].nsid = forward_nsid; 3118 } 3119 3120 int 3121 spdk_nvmf_request_get_bdev(uint32_t nsid, struct spdk_nvmf_request *req, 3122 struct spdk_bdev **bdev, struct spdk_bdev_desc **desc, struct spdk_io_channel **ch) 3123 { 3124 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 3125 struct spdk_nvmf_ns *ns; 3126 struct spdk_nvmf_poll_group *group = req->qpair->group; 3127 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 3128 3129 *bdev = NULL; 3130 *desc = NULL; 3131 *ch = NULL; 3132 3133 ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, nsid); 3134 if (ns == NULL || ns->bdev == NULL) { 3135 return -EINVAL; 3136 } 3137 3138 assert(group != NULL && group->sgroups != NULL); 3139 ns_info = &group->sgroups[ctrlr->subsys->id].ns_info[nsid - 1]; 3140 *bdev = ns->bdev; 3141 *desc = ns->desc; 3142 *ch = ns_info->channel; 3143 3144 return 0; 3145 } 3146 3147 struct spdk_nvmf_ctrlr *spdk_nvmf_request_get_ctrlr(struct spdk_nvmf_request *req) 3148 { 3149 return req->qpair->ctrlr; 3150 } 3151 3152 struct spdk_nvme_cmd *spdk_nvmf_request_get_cmd(struct spdk_nvmf_request *req) 3153 { 3154 return &req->cmd->nvme_cmd; 3155 } 3156 3157 struct spdk_nvme_cpl *spdk_nvmf_request_get_response(struct spdk_nvmf_request *req) 3158 { 3159 return &req->rsp->nvme_cpl; 3160 } 3161 3162 struct spdk_nvmf_subsystem *spdk_nvmf_request_get_subsystem(struct spdk_nvmf_request *req) 3163 { 3164 return req->qpair->ctrlr->subsys; 3165 } 3166 3167 void spdk_nvmf_request_get_data(struct spdk_nvmf_request *req, void **data, uint32_t *length) 3168 { 3169 *data = req->data; 3170 *length = req->length; 3171 } 3172 3173 struct spdk_nvmf_subsystem *spdk_nvmf_ctrlr_get_subsystem(struct spdk_nvmf_ctrlr *ctrlr) 3174 { 3175 return ctrlr->subsys; 3176 } 3177