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/nvme_spec.h" 43 #include "spdk/nvmf_cmd.h" 44 #include "spdk/string.h" 45 #include "spdk/util.h" 46 #include "spdk/version.h" 47 #include "spdk/log.h" 48 #include "spdk_internal/usdt.h" 49 50 #define MIN_KEEP_ALIVE_TIMEOUT_IN_MS 10000 51 #define NVMF_DISC_KATO_IN_MS 120000 52 #define KAS_TIME_UNIT_IN_MS 100 53 #define KAS_DEFAULT_VALUE (MIN_KEEP_ALIVE_TIMEOUT_IN_MS / KAS_TIME_UNIT_IN_MS) 54 55 /* 56 * Report the SPDK version as the firmware revision. 57 * SPDK_VERSION_STRING won't fit into FR (only 8 bytes), so try to fit the most important parts. 58 */ 59 #define FW_VERSION SPDK_VERSION_MAJOR_STRING SPDK_VERSION_MINOR_STRING SPDK_VERSION_PATCH_STRING 60 61 #define ANA_TRANSITION_TIME_IN_SEC 10 62 63 /* 64 * Support for custom admin command handlers 65 */ 66 struct spdk_nvmf_custom_admin_cmd { 67 spdk_nvmf_custom_cmd_hdlr hdlr; 68 uint32_t nsid; /* nsid to forward */ 69 }; 70 71 static struct spdk_nvmf_custom_admin_cmd g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_MAX_OPC + 1]; 72 73 static void _nvmf_request_complete(void *ctx); 74 75 static inline void 76 nvmf_invalid_connect_response(struct spdk_nvmf_fabric_connect_rsp *rsp, 77 uint8_t iattr, uint16_t ipo) 78 { 79 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 80 rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 81 rsp->status_code_specific.invalid.iattr = iattr; 82 rsp->status_code_specific.invalid.ipo = ipo; 83 } 84 85 #define SPDK_NVMF_INVALID_CONNECT_CMD(rsp, field) \ 86 nvmf_invalid_connect_response(rsp, 0, offsetof(struct spdk_nvmf_fabric_connect_cmd, field)) 87 #define SPDK_NVMF_INVALID_CONNECT_DATA(rsp, field) \ 88 nvmf_invalid_connect_response(rsp, 1, offsetof(struct spdk_nvmf_fabric_connect_data, field)) 89 90 91 static void 92 nvmf_ctrlr_stop_keep_alive_timer(struct spdk_nvmf_ctrlr *ctrlr) 93 { 94 if (!ctrlr) { 95 SPDK_ERRLOG("Controller is NULL\n"); 96 return; 97 } 98 99 if (ctrlr->keep_alive_poller == NULL) { 100 return; 101 } 102 103 SPDK_DEBUGLOG(nvmf, "Stop keep alive poller\n"); 104 spdk_poller_unregister(&ctrlr->keep_alive_poller); 105 } 106 107 static void 108 nvmf_ctrlr_stop_association_timer(struct spdk_nvmf_ctrlr *ctrlr) 109 { 110 if (!ctrlr) { 111 SPDK_ERRLOG("Controller is NULL\n"); 112 assert(false); 113 return; 114 } 115 116 if (ctrlr->association_timer == NULL) { 117 return; 118 } 119 120 SPDK_DEBUGLOG(nvmf, "Stop association timer\n"); 121 spdk_poller_unregister(&ctrlr->association_timer); 122 } 123 124 static void 125 nvmf_ctrlr_disconnect_qpairs_done(struct spdk_io_channel_iter *i, int status) 126 { 127 if (status == 0) { 128 SPDK_DEBUGLOG(nvmf, "ctrlr disconnect qpairs complete successfully\n"); 129 } else { 130 SPDK_ERRLOG("Fail to disconnect ctrlr qpairs\n"); 131 } 132 } 133 134 static int 135 _nvmf_ctrlr_disconnect_qpairs_on_pg(struct spdk_io_channel_iter *i, bool include_admin) 136 { 137 int rc = 0; 138 struct spdk_nvmf_ctrlr *ctrlr; 139 struct spdk_nvmf_qpair *qpair, *temp_qpair; 140 struct spdk_io_channel *ch; 141 struct spdk_nvmf_poll_group *group; 142 143 ctrlr = spdk_io_channel_iter_get_ctx(i); 144 ch = spdk_io_channel_iter_get_channel(i); 145 group = spdk_io_channel_get_ctx(ch); 146 147 TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, temp_qpair) { 148 if (qpair->ctrlr == ctrlr && (include_admin || !nvmf_qpair_is_admin_queue(qpair))) { 149 rc = spdk_nvmf_qpair_disconnect(qpair, NULL, NULL); 150 if (rc) { 151 SPDK_ERRLOG("Qpair disconnect failed\n"); 152 return rc; 153 } 154 } 155 } 156 157 return rc; 158 } 159 160 static void 161 nvmf_ctrlr_disconnect_qpairs_on_pg(struct spdk_io_channel_iter *i) 162 { 163 spdk_for_each_channel_continue(i, _nvmf_ctrlr_disconnect_qpairs_on_pg(i, true)); 164 } 165 166 static void 167 nvmf_ctrlr_disconnect_io_qpairs_on_pg(struct spdk_io_channel_iter *i) 168 { 169 spdk_for_each_channel_continue(i, _nvmf_ctrlr_disconnect_qpairs_on_pg(i, false)); 170 } 171 172 static int 173 nvmf_ctrlr_keep_alive_poll(void *ctx) 174 { 175 uint64_t keep_alive_timeout_tick; 176 uint64_t now = spdk_get_ticks(); 177 struct spdk_nvmf_ctrlr *ctrlr = ctx; 178 179 SPDK_DEBUGLOG(nvmf, "Polling ctrlr keep alive timeout\n"); 180 181 /* If the Keep alive feature is in use and the timer expires */ 182 keep_alive_timeout_tick = ctrlr->last_keep_alive_tick + 183 ctrlr->feat.keep_alive_timer.bits.kato * spdk_get_ticks_hz() / UINT64_C(1000); 184 if (now > keep_alive_timeout_tick) { 185 SPDK_NOTICELOG("Disconnecting host %s from subsystem %s due to keep alive timeout.\n", 186 ctrlr->hostnqn, ctrlr->subsys->subnqn); 187 /* set the Controller Fatal Status bit to '1' */ 188 if (ctrlr->vcprop.csts.bits.cfs == 0) { 189 ctrlr->vcprop.csts.bits.cfs = 1; 190 191 /* 192 * disconnect qpairs, terminate Transport connection 193 * destroy ctrlr, break the host to controller association 194 * disconnect qpairs with qpair->ctrlr == ctrlr 195 */ 196 spdk_for_each_channel(ctrlr->subsys->tgt, 197 nvmf_ctrlr_disconnect_qpairs_on_pg, 198 ctrlr, 199 nvmf_ctrlr_disconnect_qpairs_done); 200 return SPDK_POLLER_BUSY; 201 } 202 } 203 204 return SPDK_POLLER_IDLE; 205 } 206 207 static void 208 nvmf_ctrlr_start_keep_alive_timer(struct spdk_nvmf_ctrlr *ctrlr) 209 { 210 if (!ctrlr) { 211 SPDK_ERRLOG("Controller is NULL\n"); 212 return; 213 } 214 215 /* if cleared to 0 then the Keep Alive Timer is disabled */ 216 if (ctrlr->feat.keep_alive_timer.bits.kato != 0) { 217 218 ctrlr->last_keep_alive_tick = spdk_get_ticks(); 219 220 SPDK_DEBUGLOG(nvmf, "Ctrlr add keep alive poller\n"); 221 ctrlr->keep_alive_poller = SPDK_POLLER_REGISTER(nvmf_ctrlr_keep_alive_poll, ctrlr, 222 ctrlr->feat.keep_alive_timer.bits.kato * 1000); 223 } 224 } 225 226 static void 227 ctrlr_add_qpair_and_update_rsp(struct spdk_nvmf_qpair *qpair, 228 struct spdk_nvmf_ctrlr *ctrlr, 229 struct spdk_nvmf_fabric_connect_rsp *rsp) 230 { 231 assert(ctrlr->admin_qpair->group->thread == spdk_get_thread()); 232 233 /* check if we would exceed ctrlr connection limit */ 234 if (qpair->qid >= spdk_bit_array_capacity(ctrlr->qpair_mask)) { 235 SPDK_ERRLOG("Requested QID %u but Max QID is %u\n", 236 qpair->qid, spdk_bit_array_capacity(ctrlr->qpair_mask) - 1); 237 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 238 rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER; 239 return; 240 } 241 242 if (spdk_bit_array_get(ctrlr->qpair_mask, qpair->qid)) { 243 SPDK_ERRLOG("Got I/O connect with duplicate QID %u\n", qpair->qid); 244 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 245 rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER; 246 return; 247 } 248 249 qpair->ctrlr = ctrlr; 250 spdk_bit_array_set(ctrlr->qpair_mask, qpair->qid); 251 252 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 253 rsp->status_code_specific.success.cntlid = ctrlr->cntlid; 254 SPDK_DEBUGLOG(nvmf, "connect capsule response: cntlid = 0x%04x\n", 255 rsp->status_code_specific.success.cntlid); 256 257 SPDK_DTRACE_PROBE4(nvmf_ctrlr_add_qpair, qpair, qpair->qid, ctrlr->subsys->subnqn, 258 ctrlr->hostnqn); 259 } 260 261 static void 262 _nvmf_ctrlr_add_admin_qpair(void *ctx) 263 { 264 struct spdk_nvmf_request *req = ctx; 265 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 266 struct spdk_nvmf_qpair *qpair = req->qpair; 267 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 268 269 ctrlr->admin_qpair = qpair; 270 ctrlr->association_timeout = qpair->transport->opts.association_timeout; 271 nvmf_ctrlr_start_keep_alive_timer(ctrlr); 272 ctrlr_add_qpair_and_update_rsp(qpair, ctrlr, rsp); 273 _nvmf_request_complete(req); 274 } 275 276 static void 277 _nvmf_subsystem_add_ctrlr(void *ctx) 278 { 279 struct spdk_nvmf_request *req = ctx; 280 struct spdk_nvmf_qpair *qpair = req->qpair; 281 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 282 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 283 284 if (nvmf_subsystem_add_ctrlr(ctrlr->subsys, ctrlr)) { 285 SPDK_ERRLOG("Unable to add controller to subsystem\n"); 286 spdk_bit_array_free(&ctrlr->qpair_mask); 287 free(ctrlr); 288 qpair->ctrlr = NULL; 289 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 290 spdk_nvmf_request_complete(req); 291 return; 292 } 293 294 spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_add_admin_qpair, req); 295 } 296 297 static void 298 nvmf_ctrlr_cdata_init(struct spdk_nvmf_transport *transport, struct spdk_nvmf_subsystem *subsystem, 299 struct spdk_nvmf_ctrlr_data *cdata) 300 { 301 cdata->kas = KAS_DEFAULT_VALUE; 302 cdata->oncs.reservations = 1; 303 cdata->sgls.supported = 1; 304 cdata->sgls.keyed_sgl = 1; 305 cdata->sgls.sgl_offset = 1; 306 cdata->nvmf_specific.ioccsz = sizeof(struct spdk_nvme_cmd) / 16; 307 cdata->nvmf_specific.ioccsz += transport->opts.in_capsule_data_size / 16; 308 cdata->nvmf_specific.iorcsz = sizeof(struct spdk_nvme_cpl) / 16; 309 cdata->nvmf_specific.icdoff = 0; /* offset starts directly after SQE */ 310 cdata->nvmf_specific.ctrattr.ctrlr_model = SPDK_NVMF_CTRLR_MODEL_DYNAMIC; 311 cdata->nvmf_specific.msdbd = 1; 312 313 if (transport->ops->cdata_init) { 314 transport->ops->cdata_init(transport, subsystem, cdata); 315 } 316 } 317 318 static struct spdk_nvmf_ctrlr * 319 nvmf_ctrlr_create(struct spdk_nvmf_subsystem *subsystem, 320 struct spdk_nvmf_request *req, 321 struct spdk_nvmf_fabric_connect_cmd *connect_cmd, 322 struct spdk_nvmf_fabric_connect_data *connect_data) 323 { 324 struct spdk_nvmf_ctrlr *ctrlr; 325 struct spdk_nvmf_transport *transport; 326 struct spdk_nvme_transport_id listen_trid = {}; 327 328 ctrlr = calloc(1, sizeof(*ctrlr)); 329 if (ctrlr == NULL) { 330 SPDK_ERRLOG("Memory allocation failed\n"); 331 return NULL; 332 } 333 334 STAILQ_INIT(&ctrlr->async_events); 335 TAILQ_INIT(&ctrlr->log_head); 336 ctrlr->subsys = subsystem; 337 ctrlr->thread = req->qpair->group->thread; 338 ctrlr->disconnect_in_progress = false; 339 340 transport = req->qpair->transport; 341 ctrlr->qpair_mask = spdk_bit_array_create(transport->opts.max_qpairs_per_ctrlr); 342 if (!ctrlr->qpair_mask) { 343 SPDK_ERRLOG("Failed to allocate controller qpair mask\n"); 344 goto err_qpair_mask; 345 } 346 347 nvmf_ctrlr_cdata_init(transport, subsystem, &ctrlr->cdata); 348 349 /* 350 * KAS: This field indicates the granularity of the Keep Alive Timer in 100ms units. 351 * If this field is cleared to 0h, then Keep Alive is not supported. 352 */ 353 if (ctrlr->cdata.kas) { 354 ctrlr->feat.keep_alive_timer.bits.kato = spdk_divide_round_up(connect_cmd->kato, 355 KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS) * 356 KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS; 357 } 358 359 ctrlr->feat.async_event_configuration.bits.ns_attr_notice = 1; 360 if (ctrlr->subsys->flags.ana_reporting) { 361 ctrlr->feat.async_event_configuration.bits.ana_change_notice = 1; 362 } 363 ctrlr->feat.volatile_write_cache.bits.wce = 1; 364 /* Coalescing Disable */ 365 ctrlr->feat.interrupt_vector_configuration.bits.cd = 1; 366 367 if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 368 /* 369 * If keep-alive timeout is not set, discovery controllers use some 370 * arbitrary high value in order to cleanup stale discovery sessions 371 * 372 * From the 1.0a nvme-of spec: 373 * "The Keep Alive command is reserved for 374 * Discovery controllers. A transport may specify a 375 * fixed Discovery controller activity timeout value 376 * (e.g., 2 minutes). If no commands are received 377 * by a Discovery controller within that time 378 * period, the controller may perform the 379 * actions for Keep Alive Timer expiration". 380 * 381 * From the 1.1 nvme-of spec: 382 * "A host requests an explicit persistent connection 383 * to a Discovery controller and Asynchronous Event Notifications from 384 * the Discovery controller on that persistent connection by specifying 385 * a non-zero Keep Alive Timer value in the Connect command." 386 * 387 * In case non-zero KATO is used, we enable discovery_log_change_notice 388 * otherwise we disable it and use default discovery controller KATO. 389 * KATO is in millisecond. 390 */ 391 if (ctrlr->feat.keep_alive_timer.bits.kato == 0) { 392 ctrlr->feat.keep_alive_timer.bits.kato = NVMF_DISC_KATO_IN_MS; 393 ctrlr->feat.async_event_configuration.bits.discovery_log_change_notice = 0; 394 } else { 395 ctrlr->feat.async_event_configuration.bits.discovery_log_change_notice = 1; 396 } 397 } 398 399 /* Subtract 1 for admin queue, 1 for 0's based */ 400 ctrlr->feat.number_of_queues.bits.ncqr = transport->opts.max_qpairs_per_ctrlr - 1 - 401 1; 402 ctrlr->feat.number_of_queues.bits.nsqr = transport->opts.max_qpairs_per_ctrlr - 1 - 403 1; 404 405 spdk_uuid_copy(&ctrlr->hostid, (struct spdk_uuid *)connect_data->hostid); 406 memcpy(ctrlr->hostnqn, connect_data->hostnqn, sizeof(ctrlr->hostnqn)); 407 408 ctrlr->vcprop.cap.raw = 0; 409 ctrlr->vcprop.cap.bits.cqr = 1; /* NVMe-oF specification required */ 410 ctrlr->vcprop.cap.bits.mqes = transport->opts.max_queue_depth - 411 1; /* max queue depth */ 412 ctrlr->vcprop.cap.bits.ams = 0; /* optional arb mechanisms */ 413 ctrlr->vcprop.cap.bits.to = 1; /* ready timeout - 500 msec units */ 414 ctrlr->vcprop.cap.bits.dstrd = 0; /* fixed to 0 for NVMe-oF */ 415 ctrlr->vcprop.cap.bits.css = SPDK_NVME_CAP_CSS_NVM; /* NVM command set */ 416 ctrlr->vcprop.cap.bits.mpsmin = 0; /* 2 ^ (12 + mpsmin) == 4k */ 417 ctrlr->vcprop.cap.bits.mpsmax = 0; /* 2 ^ (12 + mpsmax) == 4k */ 418 419 /* Version Supported: 1.3 */ 420 ctrlr->vcprop.vs.bits.mjr = 1; 421 ctrlr->vcprop.vs.bits.mnr = 3; 422 ctrlr->vcprop.vs.bits.ter = 0; 423 424 ctrlr->vcprop.cc.raw = 0; 425 ctrlr->vcprop.cc.bits.en = 0; /* Init controller disabled */ 426 427 ctrlr->vcprop.csts.raw = 0; 428 ctrlr->vcprop.csts.bits.rdy = 0; /* Init controller as not ready */ 429 430 SPDK_DEBUGLOG(nvmf, "cap 0x%" PRIx64 "\n", ctrlr->vcprop.cap.raw); 431 SPDK_DEBUGLOG(nvmf, "vs 0x%x\n", ctrlr->vcprop.vs.raw); 432 SPDK_DEBUGLOG(nvmf, "cc 0x%x\n", ctrlr->vcprop.cc.raw); 433 SPDK_DEBUGLOG(nvmf, "csts 0x%x\n", ctrlr->vcprop.csts.raw); 434 435 ctrlr->dif_insert_or_strip = transport->opts.dif_insert_or_strip; 436 437 if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_NVME) { 438 if (spdk_nvmf_qpair_get_listen_trid(req->qpair, &listen_trid) != 0) { 439 SPDK_ERRLOG("Could not get listener transport ID\n"); 440 goto err_listener; 441 } 442 443 ctrlr->listener = nvmf_subsystem_find_listener(ctrlr->subsys, &listen_trid); 444 if (!ctrlr->listener) { 445 SPDK_ERRLOG("Listener was not found\n"); 446 goto err_listener; 447 } 448 } 449 450 req->qpair->ctrlr = ctrlr; 451 spdk_thread_send_msg(subsystem->thread, _nvmf_subsystem_add_ctrlr, req); 452 453 return ctrlr; 454 err_listener: 455 spdk_bit_array_free(&ctrlr->qpair_mask); 456 err_qpair_mask: 457 free(ctrlr); 458 return NULL; 459 } 460 461 static void 462 _nvmf_ctrlr_destruct(void *ctx) 463 { 464 struct spdk_nvmf_ctrlr *ctrlr = ctx; 465 struct spdk_nvmf_reservation_log *log, *log_tmp; 466 struct spdk_nvmf_async_event_completion *event, *event_tmp; 467 468 if (ctrlr->disconnect_in_progress) { 469 SPDK_ERRLOG("freeing ctrlr with disconnect in progress\n"); 470 spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_destruct, ctrlr); 471 return; 472 } 473 474 nvmf_ctrlr_stop_keep_alive_timer(ctrlr); 475 nvmf_ctrlr_stop_association_timer(ctrlr); 476 spdk_bit_array_free(&ctrlr->qpair_mask); 477 478 TAILQ_FOREACH_SAFE(log, &ctrlr->log_head, link, log_tmp) { 479 TAILQ_REMOVE(&ctrlr->log_head, log, link); 480 free(log); 481 } 482 STAILQ_FOREACH_SAFE(event, &ctrlr->async_events, link, event_tmp) { 483 STAILQ_REMOVE(&ctrlr->async_events, event, spdk_nvmf_async_event_completion, link); 484 free(event); 485 } 486 free(ctrlr); 487 } 488 489 void 490 nvmf_ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr) 491 { 492 nvmf_subsystem_remove_ctrlr(ctrlr->subsys, ctrlr); 493 494 spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_destruct, ctrlr); 495 } 496 497 static void 498 nvmf_ctrlr_add_io_qpair(void *ctx) 499 { 500 struct spdk_nvmf_request *req = ctx; 501 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 502 struct spdk_nvmf_qpair *qpair = req->qpair; 503 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 504 struct spdk_nvmf_qpair *admin_qpair = ctrlr->admin_qpair; 505 506 /* Unit test will check qpair->ctrlr after calling spdk_nvmf_ctrlr_connect. 507 * For error case, the value should be NULL. So set it to NULL at first. 508 */ 509 qpair->ctrlr = NULL; 510 511 /* Make sure the controller is not being destroyed. */ 512 if (ctrlr->in_destruct) { 513 SPDK_ERRLOG("Got I/O connect while ctrlr was being destroyed.\n"); 514 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 515 goto end; 516 } 517 518 if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 519 SPDK_ERRLOG("I/O connect not allowed on discovery controller\n"); 520 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 521 goto end; 522 } 523 524 if (!ctrlr->vcprop.cc.bits.en) { 525 SPDK_ERRLOG("Got I/O connect before ctrlr was enabled\n"); 526 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 527 goto end; 528 } 529 530 if (1u << ctrlr->vcprop.cc.bits.iosqes != sizeof(struct spdk_nvme_cmd)) { 531 SPDK_ERRLOG("Got I/O connect with invalid IOSQES %u\n", 532 ctrlr->vcprop.cc.bits.iosqes); 533 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 534 goto end; 535 } 536 537 if (1u << ctrlr->vcprop.cc.bits.iocqes != sizeof(struct spdk_nvme_cpl)) { 538 SPDK_ERRLOG("Got I/O connect with invalid IOCQES %u\n", 539 ctrlr->vcprop.cc.bits.iocqes); 540 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 541 goto end; 542 } 543 544 if (admin_qpair->state != SPDK_NVMF_QPAIR_ACTIVE || admin_qpair->group == NULL) { 545 /* There is a chance that admin qpair is being destroyed at this moment due to e.g. 546 * expired keep alive timer. Part of the qpair destruction process is change of qpair's 547 * state to DEACTIVATING and removing it from poll group */ 548 SPDK_ERRLOG("Inactive admin qpair (state %d, group %p)\n", admin_qpair->state, admin_qpair->group); 549 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 550 goto end; 551 } 552 553 ctrlr_add_qpair_and_update_rsp(qpair, ctrlr, rsp); 554 end: 555 spdk_nvmf_request_complete(req); 556 } 557 558 static void 559 _nvmf_ctrlr_add_io_qpair(void *ctx) 560 { 561 struct spdk_nvmf_request *req = ctx; 562 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 563 struct spdk_nvmf_fabric_connect_data *data = req->data; 564 struct spdk_nvmf_ctrlr *ctrlr; 565 struct spdk_nvmf_qpair *qpair = req->qpair; 566 struct spdk_nvmf_qpair *admin_qpair; 567 struct spdk_nvmf_tgt *tgt = qpair->transport->tgt; 568 struct spdk_nvmf_subsystem *subsystem; 569 struct spdk_nvme_transport_id listen_trid = {}; 570 const struct spdk_nvmf_subsystem_listener *listener; 571 572 SPDK_DEBUGLOG(nvmf, "Connect I/O Queue for controller id 0x%x\n", data->cntlid); 573 574 subsystem = spdk_nvmf_tgt_find_subsystem(tgt, data->subnqn); 575 /* We already checked this in spdk_nvmf_ctrlr_connect */ 576 assert(subsystem != NULL); 577 578 ctrlr = nvmf_subsystem_get_ctrlr(subsystem, data->cntlid); 579 if (ctrlr == NULL) { 580 SPDK_ERRLOG("Unknown controller ID 0x%x\n", data->cntlid); 581 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid); 582 spdk_nvmf_request_complete(req); 583 return; 584 } 585 586 /* fail before passing a message to the controller thread. */ 587 if (ctrlr->in_destruct) { 588 SPDK_ERRLOG("Got I/O connect while ctrlr was being destroyed.\n"); 589 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 590 spdk_nvmf_request_complete(req); 591 return; 592 } 593 594 /* If ANA reporting is enabled, check if I/O connect is on the same listener. */ 595 if (subsystem->flags.ana_reporting) { 596 if (spdk_nvmf_qpair_get_listen_trid(req->qpair, &listen_trid) != 0) { 597 SPDK_ERRLOG("Could not get listener transport ID\n"); 598 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 599 spdk_nvmf_request_complete(req); 600 return; 601 } 602 603 listener = nvmf_subsystem_find_listener(subsystem, &listen_trid); 604 if (listener != ctrlr->listener) { 605 SPDK_ERRLOG("I/O connect is on a listener different from admin connect\n"); 606 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 607 spdk_nvmf_request_complete(req); 608 return; 609 } 610 } 611 612 admin_qpair = ctrlr->admin_qpair; 613 if (admin_qpair->state != SPDK_NVMF_QPAIR_ACTIVE || admin_qpair->group == NULL) { 614 /* There is a chance that admin qpair is being destroyed at this moment due to e.g. 615 * expired keep alive timer. Part of the qpair destruction process is change of qpair's 616 * state to DEACTIVATING and removing it from poll group */ 617 SPDK_ERRLOG("Inactive admin qpair (state %d, group %p)\n", admin_qpair->state, admin_qpair->group); 618 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 619 spdk_nvmf_request_complete(req); 620 return; 621 } 622 qpair->ctrlr = ctrlr; 623 spdk_thread_send_msg(admin_qpair->group->thread, nvmf_ctrlr_add_io_qpair, req); 624 } 625 626 static bool 627 nvmf_qpair_access_allowed(struct spdk_nvmf_qpair *qpair, struct spdk_nvmf_subsystem *subsystem, 628 const char *hostnqn) 629 { 630 struct spdk_nvme_transport_id listen_trid = {}; 631 632 if (!spdk_nvmf_subsystem_host_allowed(subsystem, hostnqn)) { 633 SPDK_ERRLOG("Subsystem '%s' does not allow host '%s'\n", subsystem->subnqn, hostnqn); 634 return false; 635 } 636 637 if (spdk_nvmf_qpair_get_listen_trid(qpair, &listen_trid)) { 638 SPDK_ERRLOG("Subsystem '%s' is unable to enforce access control due to an internal error.\n", 639 subsystem->subnqn); 640 return false; 641 } 642 643 if (!spdk_nvmf_subsystem_listener_allowed(subsystem, &listen_trid)) { 644 SPDK_ERRLOG("Subsystem '%s' does not allow host '%s' to connect at this address.\n", 645 subsystem->subnqn, hostnqn); 646 return false; 647 } 648 649 return true; 650 } 651 652 static int 653 _nvmf_ctrlr_connect(struct spdk_nvmf_request *req) 654 { 655 struct spdk_nvmf_fabric_connect_data *data = req->data; 656 struct spdk_nvmf_fabric_connect_cmd *cmd = &req->cmd->connect_cmd; 657 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 658 struct spdk_nvmf_qpair *qpair = req->qpair; 659 struct spdk_nvmf_transport *transport = qpair->transport; 660 struct spdk_nvmf_ctrlr *ctrlr; 661 struct spdk_nvmf_subsystem *subsystem; 662 663 SPDK_DEBUGLOG(nvmf, "recfmt 0x%x qid %u sqsize %u\n", 664 cmd->recfmt, cmd->qid, cmd->sqsize); 665 666 SPDK_DEBUGLOG(nvmf, "Connect data:\n"); 667 SPDK_DEBUGLOG(nvmf, " cntlid: 0x%04x\n", data->cntlid); 668 SPDK_DEBUGLOG(nvmf, " hostid: %08x-%04x-%04x-%02x%02x-%04x%08x ***\n", 669 ntohl(*(uint32_t *)&data->hostid[0]), 670 ntohs(*(uint16_t *)&data->hostid[4]), 671 ntohs(*(uint16_t *)&data->hostid[6]), 672 data->hostid[8], 673 data->hostid[9], 674 ntohs(*(uint16_t *)&data->hostid[10]), 675 ntohl(*(uint32_t *)&data->hostid[12])); 676 SPDK_DEBUGLOG(nvmf, " subnqn: \"%s\"\n", data->subnqn); 677 SPDK_DEBUGLOG(nvmf, " hostnqn: \"%s\"\n", data->hostnqn); 678 679 subsystem = spdk_nvmf_tgt_find_subsystem(transport->tgt, data->subnqn); 680 if (!subsystem) { 681 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn); 682 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 683 } 684 685 if (cmd->recfmt != 0) { 686 SPDK_ERRLOG("Connect command unsupported RECFMT %u\n", cmd->recfmt); 687 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 688 rsp->status.sc = SPDK_NVMF_FABRIC_SC_INCOMPATIBLE_FORMAT; 689 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 690 } 691 692 /* 693 * SQSIZE is a 0-based value, so it must be at least 1 (minimum queue depth is 2) and 694 * strictly less than max_aq_depth (admin queues) or max_queue_depth (io queues). 695 */ 696 if (cmd->sqsize == 0) { 697 SPDK_ERRLOG("Invalid SQSIZE = 0\n"); 698 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize); 699 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 700 } 701 702 if (cmd->qid == 0) { 703 if (cmd->sqsize >= transport->opts.max_aq_depth) { 704 SPDK_ERRLOG("Invalid SQSIZE for admin queue %u (min 1, max %u)\n", 705 cmd->sqsize, transport->opts.max_aq_depth - 1); 706 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize); 707 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 708 } 709 } else if (cmd->sqsize >= transport->opts.max_queue_depth) { 710 SPDK_ERRLOG("Invalid SQSIZE %u (min 1, max %u)\n", 711 cmd->sqsize, transport->opts.max_queue_depth - 1); 712 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize); 713 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 714 } 715 716 qpair->sq_head_max = cmd->sqsize; 717 qpair->qid = cmd->qid; 718 719 if (0 == qpair->qid) { 720 qpair->group->stat.admin_qpairs++; 721 qpair->group->stat.current_admin_qpairs++; 722 } else { 723 qpair->group->stat.io_qpairs++; 724 qpair->group->stat.current_io_qpairs++; 725 } 726 727 if (cmd->qid == 0) { 728 SPDK_DEBUGLOG(nvmf, "Connect Admin Queue for controller ID 0x%x\n", data->cntlid); 729 730 if (data->cntlid != 0xFFFF) { 731 /* This NVMf target only supports dynamic mode. */ 732 SPDK_ERRLOG("The NVMf target only supports dynamic mode (CNTLID = 0x%x).\n", data->cntlid); 733 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid); 734 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 735 } 736 737 /* Establish a new ctrlr */ 738 ctrlr = nvmf_ctrlr_create(subsystem, req, cmd, data); 739 if (!ctrlr) { 740 SPDK_ERRLOG("nvmf_ctrlr_create() failed\n"); 741 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 742 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 743 } else { 744 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 745 } 746 } else { 747 spdk_thread_send_msg(subsystem->thread, _nvmf_ctrlr_add_io_qpair, req); 748 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 749 } 750 } 751 752 static inline bool 753 nvmf_request_is_fabric_connect(struct spdk_nvmf_request *req) 754 { 755 return req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC && 756 req->cmd->nvmf_cmd.fctype == SPDK_NVMF_FABRIC_COMMAND_CONNECT; 757 } 758 759 static struct spdk_nvmf_subsystem_poll_group * 760 nvmf_subsystem_pg_from_connect_cmd(struct spdk_nvmf_request *req) 761 { 762 struct spdk_nvmf_fabric_connect_data *data; 763 struct spdk_nvmf_subsystem *subsystem; 764 struct spdk_nvmf_tgt *tgt; 765 766 assert(nvmf_request_is_fabric_connect(req)); 767 assert(req->qpair->ctrlr == NULL); 768 769 data = req->data; 770 tgt = req->qpair->transport->tgt; 771 772 subsystem = spdk_nvmf_tgt_find_subsystem(tgt, data->subnqn); 773 if (subsystem == NULL) { 774 return NULL; 775 } 776 777 return &req->qpair->group->sgroups[subsystem->id]; 778 } 779 780 static void 781 nvmf_add_to_outstanding_queue(struct spdk_nvmf_request *req) 782 { 783 if (!spdk_nvmf_using_zcopy(req->zcopy_phase)) { 784 /* if using zcopy then request has been added when the start zcopy was actioned */ 785 struct spdk_nvmf_qpair *qpair = req->qpair; 786 TAILQ_INSERT_TAIL(&qpair->outstanding, req, link); 787 } 788 } 789 790 int 791 spdk_nvmf_ctrlr_connect(struct spdk_nvmf_request *req) 792 { 793 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 794 struct spdk_nvmf_subsystem_poll_group *sgroup; 795 enum spdk_nvmf_request_exec_status status; 796 797 sgroup = nvmf_subsystem_pg_from_connect_cmd(req); 798 if (!sgroup) { 799 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn); 800 status = SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 801 goto out; 802 } 803 804 sgroup->mgmt_io_outstanding++; 805 nvmf_add_to_outstanding_queue(req); 806 807 status = _nvmf_ctrlr_connect(req); 808 809 out: 810 if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 811 _nvmf_request_complete(req); 812 } 813 814 return status; 815 } 816 817 static int nvmf_ctrlr_cmd_connect(struct spdk_nvmf_request *req); 818 819 static int 820 retry_connect(void *arg) 821 { 822 struct spdk_nvmf_request *req = arg; 823 struct spdk_nvmf_subsystem_poll_group *sgroup; 824 int rc; 825 826 sgroup = nvmf_subsystem_pg_from_connect_cmd(req); 827 assert(sgroup != NULL); 828 sgroup->mgmt_io_outstanding++; 829 spdk_poller_unregister(&req->poller); 830 rc = nvmf_ctrlr_cmd_connect(req); 831 if (rc == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 832 _nvmf_request_complete(req); 833 } 834 return SPDK_POLLER_BUSY; 835 } 836 837 static int 838 nvmf_ctrlr_cmd_connect(struct spdk_nvmf_request *req) 839 { 840 struct spdk_nvmf_fabric_connect_data *data = req->data; 841 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 842 struct spdk_nvmf_transport *transport = req->qpair->transport; 843 struct spdk_nvmf_subsystem *subsystem; 844 845 if (req->length < sizeof(struct spdk_nvmf_fabric_connect_data)) { 846 SPDK_ERRLOG("Connect command data length 0x%x too small\n", req->length); 847 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 848 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 849 } 850 851 subsystem = spdk_nvmf_tgt_find_subsystem(transport->tgt, data->subnqn); 852 if (!subsystem) { 853 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn); 854 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 855 } 856 857 if ((subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE) || 858 (subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSING) || 859 (subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED) || 860 (subsystem->state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING)) { 861 struct spdk_nvmf_subsystem_poll_group *sgroup; 862 863 if (req->timeout_tsc == 0) { 864 /* We will only retry the request up to 1 second. */ 865 req->timeout_tsc = spdk_get_ticks() + spdk_get_ticks_hz(); 866 } else if (spdk_get_ticks() > req->timeout_tsc) { 867 SPDK_ERRLOG("Subsystem '%s' was not ready for 1 second\n", subsystem->subnqn); 868 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 869 rsp->status.sc = SPDK_NVMF_FABRIC_SC_CONTROLLER_BUSY; 870 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 871 } 872 873 /* Subsystem is not ready to handle a connect. Use a poller to retry it 874 * again later. Decrement the mgmt_io_outstanding to avoid the 875 * subsystem waiting for this command to complete before unpausing. 876 */ 877 sgroup = nvmf_subsystem_pg_from_connect_cmd(req); 878 assert(sgroup != NULL); 879 sgroup->mgmt_io_outstanding--; 880 SPDK_DEBUGLOG(nvmf, "Subsystem '%s' is not ready for connect, retrying...\n", subsystem->subnqn); 881 req->poller = SPDK_POLLER_REGISTER(retry_connect, req, 100); 882 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 883 } 884 885 /* Ensure that hostnqn is null terminated */ 886 if (!memchr(data->hostnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1)) { 887 SPDK_ERRLOG("Connect HOSTNQN is not null terminated\n"); 888 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, hostnqn); 889 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 890 } 891 892 if (!nvmf_qpair_access_allowed(req->qpair, subsystem, data->hostnqn)) { 893 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 894 rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_HOST; 895 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 896 } 897 898 return _nvmf_ctrlr_connect(req); 899 } 900 901 static int 902 nvmf_ctrlr_association_remove(void *ctx) 903 { 904 struct spdk_nvmf_ctrlr *ctrlr = ctx; 905 int rc; 906 907 SPDK_DEBUGLOG(nvmf, "Disconnecting host from subsystem %s due to association timeout.\n", 908 ctrlr->subsys->subnqn); 909 910 rc = spdk_nvmf_qpair_disconnect(ctrlr->admin_qpair, NULL, NULL); 911 if (rc < 0) { 912 SPDK_ERRLOG("Fail to disconnect admin ctrlr qpair\n"); 913 assert(false); 914 } 915 916 nvmf_ctrlr_stop_association_timer(ctrlr); 917 return SPDK_POLLER_BUSY; 918 } 919 920 static void 921 nvmf_ctrlr_cc_shn_done(struct spdk_io_channel_iter *i, int status) 922 { 923 struct spdk_nvmf_ctrlr *ctrlr = spdk_io_channel_iter_get_ctx(i); 924 925 if (status < 0) { 926 SPDK_ERRLOG("Fail to disconnect io ctrlr qpairs\n"); 927 assert(false); 928 } 929 930 ctrlr->vcprop.csts.bits.shst = SPDK_NVME_SHST_COMPLETE; 931 932 /* After CC.EN transitions to 0 (due to shutdown or reset), the association 933 * between the host and controller shall be preserved for at least 2 minutes */ 934 if (ctrlr->association_timer) { 935 SPDK_DEBUGLOG(nvmf, "Association timer already set\n"); 936 nvmf_ctrlr_stop_association_timer(ctrlr); 937 } 938 if (ctrlr->association_timeout) { 939 ctrlr->association_timer = SPDK_POLLER_REGISTER(nvmf_ctrlr_association_remove, ctrlr, 940 ctrlr->association_timeout * 1000); 941 } 942 ctrlr->disconnect_in_progress = false; 943 } 944 945 static void 946 nvmf_ctrlr_cc_reset_done(struct spdk_io_channel_iter *i, int status) 947 { 948 struct spdk_nvmf_ctrlr *ctrlr = spdk_io_channel_iter_get_ctx(i); 949 950 if (status < 0) { 951 SPDK_ERRLOG("Fail to disconnect io ctrlr qpairs\n"); 952 assert(false); 953 } 954 955 /* Only a subset of the registers are cleared out on a reset */ 956 ctrlr->vcprop.cc.raw = 0; 957 ctrlr->vcprop.csts.raw = 0; 958 959 /* After CC.EN transitions to 0 (due to shutdown or reset), the association 960 * between the host and controller shall be preserved for at least 2 minutes */ 961 if (ctrlr->association_timer) { 962 SPDK_DEBUGLOG(nvmf, "Association timer already set\n"); 963 nvmf_ctrlr_stop_association_timer(ctrlr); 964 } 965 if (ctrlr->association_timeout) { 966 ctrlr->association_timer = SPDK_POLLER_REGISTER(nvmf_ctrlr_association_remove, ctrlr, 967 ctrlr->association_timeout * 1000); 968 } 969 ctrlr->disconnect_in_progress = false; 970 } 971 972 const struct spdk_nvmf_registers * 973 spdk_nvmf_ctrlr_get_regs(struct spdk_nvmf_ctrlr *ctrlr) 974 { 975 return &ctrlr->vcprop; 976 } 977 978 static uint64_t 979 nvmf_prop_get_cap(struct spdk_nvmf_ctrlr *ctrlr) 980 { 981 return ctrlr->vcprop.cap.raw; 982 } 983 984 static uint64_t 985 nvmf_prop_get_vs(struct spdk_nvmf_ctrlr *ctrlr) 986 { 987 return ctrlr->vcprop.vs.raw; 988 } 989 990 static uint64_t 991 nvmf_prop_get_cc(struct spdk_nvmf_ctrlr *ctrlr) 992 { 993 return ctrlr->vcprop.cc.raw; 994 } 995 996 static bool 997 nvmf_prop_set_cc(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 998 { 999 union spdk_nvme_cc_register cc, diff; 1000 1001 cc.raw = value; 1002 1003 SPDK_DEBUGLOG(nvmf, "cur CC: 0x%08x\n", ctrlr->vcprop.cc.raw); 1004 SPDK_DEBUGLOG(nvmf, "new CC: 0x%08x\n", cc.raw); 1005 1006 /* 1007 * Calculate which bits changed between the current and new CC. 1008 * Mark each bit as 0 once it is handled to determine if any unhandled bits were changed. 1009 */ 1010 diff.raw = cc.raw ^ ctrlr->vcprop.cc.raw; 1011 1012 if (diff.bits.en) { 1013 if (cc.bits.en) { 1014 SPDK_DEBUGLOG(nvmf, "Property Set CC Enable!\n"); 1015 nvmf_ctrlr_stop_association_timer(ctrlr); 1016 1017 ctrlr->vcprop.cc.bits.en = 1; 1018 ctrlr->vcprop.csts.bits.rdy = 1; 1019 } else { 1020 SPDK_DEBUGLOG(nvmf, "Property Set CC Disable!\n"); 1021 ctrlr->vcprop.cc.bits.en = 0; 1022 ctrlr->disconnect_in_progress = true; 1023 spdk_for_each_channel(ctrlr->subsys->tgt, 1024 nvmf_ctrlr_disconnect_io_qpairs_on_pg, 1025 ctrlr, 1026 nvmf_ctrlr_cc_reset_done); 1027 } 1028 diff.bits.en = 0; 1029 } 1030 1031 if (diff.bits.shn) { 1032 if (cc.bits.shn == SPDK_NVME_SHN_NORMAL || 1033 cc.bits.shn == SPDK_NVME_SHN_ABRUPT) { 1034 SPDK_DEBUGLOG(nvmf, "Property Set CC Shutdown %u%ub!\n", 1035 cc.bits.shn >> 1, cc.bits.shn & 1); 1036 ctrlr->vcprop.cc.bits.shn = cc.bits.shn; 1037 ctrlr->disconnect_in_progress = true; 1038 spdk_for_each_channel(ctrlr->subsys->tgt, 1039 nvmf_ctrlr_disconnect_io_qpairs_on_pg, 1040 ctrlr, 1041 nvmf_ctrlr_cc_shn_done); 1042 1043 /* From the time a shutdown is initiated the controller shall disable 1044 * Keep Alive timer */ 1045 nvmf_ctrlr_stop_keep_alive_timer(ctrlr); 1046 } else if (cc.bits.shn == 0) { 1047 ctrlr->vcprop.cc.bits.shn = 0; 1048 } else { 1049 SPDK_ERRLOG("Prop Set CC: Invalid SHN value %u%ub\n", 1050 cc.bits.shn >> 1, cc.bits.shn & 1); 1051 return false; 1052 } 1053 diff.bits.shn = 0; 1054 } 1055 1056 if (diff.bits.iosqes) { 1057 SPDK_DEBUGLOG(nvmf, "Prop Set IOSQES = %u (%u bytes)\n", 1058 cc.bits.iosqes, 1u << cc.bits.iosqes); 1059 ctrlr->vcprop.cc.bits.iosqes = cc.bits.iosqes; 1060 diff.bits.iosqes = 0; 1061 } 1062 1063 if (diff.bits.iocqes) { 1064 SPDK_DEBUGLOG(nvmf, "Prop Set IOCQES = %u (%u bytes)\n", 1065 cc.bits.iocqes, 1u << cc.bits.iocqes); 1066 ctrlr->vcprop.cc.bits.iocqes = cc.bits.iocqes; 1067 diff.bits.iocqes = 0; 1068 } 1069 1070 if (diff.bits.ams) { 1071 SPDK_ERRLOG("Arbitration Mechanism Selected (AMS) 0x%x not supported!\n", cc.bits.ams); 1072 return false; 1073 } 1074 1075 if (diff.bits.mps) { 1076 SPDK_ERRLOG("Memory Page Size (MPS) %u KiB not supported!\n", (1 << (2 + cc.bits.mps))); 1077 return false; 1078 } 1079 1080 if (diff.bits.css) { 1081 SPDK_ERRLOG("I/O Command Set Selected (CSS) 0x%x not supported!\n", cc.bits.css); 1082 return false; 1083 } 1084 1085 if (diff.raw != 0) { 1086 /* Print an error message, but don't fail the command in this case. 1087 * If we did want to fail in this case, we'd need to ensure we acted 1088 * on no other bits or the initiator gets confused. */ 1089 SPDK_ERRLOG("Prop Set CC toggled reserved bits 0x%x!\n", diff.raw); 1090 } 1091 1092 return true; 1093 } 1094 1095 static uint64_t 1096 nvmf_prop_get_csts(struct spdk_nvmf_ctrlr *ctrlr) 1097 { 1098 return ctrlr->vcprop.csts.raw; 1099 } 1100 1101 static uint64_t 1102 nvmf_prop_get_aqa(struct spdk_nvmf_ctrlr *ctrlr) 1103 { 1104 return ctrlr->vcprop.aqa.raw; 1105 } 1106 1107 static bool 1108 nvmf_prop_set_aqa(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 1109 { 1110 union spdk_nvme_aqa_register aqa; 1111 1112 aqa.raw = value; 1113 1114 if (aqa.bits.asqs < SPDK_NVME_ADMIN_QUEUE_MIN_ENTRIES - 1 || 1115 aqa.bits.acqs < SPDK_NVME_ADMIN_QUEUE_MIN_ENTRIES - 1 || 1116 aqa.bits.reserved1 != 0 || aqa.bits.reserved2 != 0) { 1117 return false; 1118 } 1119 1120 ctrlr->vcprop.aqa.raw = value; 1121 1122 return true; 1123 } 1124 1125 static uint64_t 1126 nvmf_prop_get_asq(struct spdk_nvmf_ctrlr *ctrlr) 1127 { 1128 return ctrlr->vcprop.asq; 1129 } 1130 1131 static bool 1132 nvmf_prop_set_asq_lower(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 1133 { 1134 ctrlr->vcprop.asq = (ctrlr->vcprop.asq & (0xFFFFFFFFULL << 32ULL)) | value; 1135 1136 return true; 1137 } 1138 1139 static bool 1140 nvmf_prop_set_asq_upper(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 1141 { 1142 ctrlr->vcprop.asq = (ctrlr->vcprop.asq & 0xFFFFFFFFULL) | ((uint64_t)value << 32ULL); 1143 1144 return true; 1145 } 1146 1147 static uint64_t 1148 nvmf_prop_get_acq(struct spdk_nvmf_ctrlr *ctrlr) 1149 { 1150 return ctrlr->vcprop.acq; 1151 } 1152 1153 static bool 1154 nvmf_prop_set_acq_lower(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 1155 { 1156 ctrlr->vcprop.acq = (ctrlr->vcprop.acq & (0xFFFFFFFFULL << 32ULL)) | value; 1157 1158 return true; 1159 } 1160 1161 static bool 1162 nvmf_prop_set_acq_upper(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 1163 { 1164 ctrlr->vcprop.acq = (ctrlr->vcprop.acq & 0xFFFFFFFFULL) | ((uint64_t)value << 32ULL); 1165 1166 return true; 1167 } 1168 1169 struct nvmf_prop { 1170 uint32_t ofst; 1171 uint8_t size; 1172 char name[11]; 1173 uint64_t (*get_cb)(struct spdk_nvmf_ctrlr *ctrlr); 1174 bool (*set_cb)(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value); 1175 bool (*set_upper_cb)(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value); 1176 }; 1177 1178 #define PROP(field, size, get_cb, set_cb, set_upper_cb) \ 1179 { \ 1180 offsetof(struct spdk_nvme_registers, field), \ 1181 size, \ 1182 #field, \ 1183 get_cb, set_cb, set_upper_cb \ 1184 } 1185 1186 static const struct nvmf_prop nvmf_props[] = { 1187 PROP(cap, 8, nvmf_prop_get_cap, NULL, NULL), 1188 PROP(vs, 4, nvmf_prop_get_vs, NULL, NULL), 1189 PROP(cc, 4, nvmf_prop_get_cc, nvmf_prop_set_cc, NULL), 1190 PROP(csts, 4, nvmf_prop_get_csts, NULL, NULL), 1191 PROP(aqa, 4, nvmf_prop_get_aqa, nvmf_prop_set_aqa, NULL), 1192 PROP(asq, 8, nvmf_prop_get_asq, nvmf_prop_set_asq_lower, nvmf_prop_set_asq_upper), 1193 PROP(acq, 8, nvmf_prop_get_acq, nvmf_prop_set_acq_lower, nvmf_prop_set_acq_upper), 1194 }; 1195 1196 static const struct nvmf_prop * 1197 find_prop(uint32_t ofst, uint8_t size) 1198 { 1199 size_t i; 1200 1201 for (i = 0; i < SPDK_COUNTOF(nvmf_props); i++) { 1202 const struct nvmf_prop *prop = &nvmf_props[i]; 1203 1204 if ((ofst >= prop->ofst) && (ofst + size <= prop->ofst + prop->size)) { 1205 return prop; 1206 } 1207 } 1208 1209 return NULL; 1210 } 1211 1212 static int 1213 nvmf_property_get(struct spdk_nvmf_request *req) 1214 { 1215 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1216 struct spdk_nvmf_fabric_prop_get_cmd *cmd = &req->cmd->prop_get_cmd; 1217 struct spdk_nvmf_fabric_prop_get_rsp *response = &req->rsp->prop_get_rsp; 1218 const struct nvmf_prop *prop; 1219 uint8_t size; 1220 1221 response->status.sc = 0; 1222 response->value.u64 = 0; 1223 1224 SPDK_DEBUGLOG(nvmf, "size %d, offset 0x%x\n", 1225 cmd->attrib.size, cmd->ofst); 1226 1227 switch (cmd->attrib.size) { 1228 case SPDK_NVMF_PROP_SIZE_4: 1229 size = 4; 1230 break; 1231 case SPDK_NVMF_PROP_SIZE_8: 1232 size = 8; 1233 break; 1234 default: 1235 SPDK_ERRLOG("Invalid size value %d\n", cmd->attrib.size); 1236 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1237 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 1238 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1239 } 1240 1241 prop = find_prop(cmd->ofst, size); 1242 if (prop == NULL || prop->get_cb == NULL) { 1243 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1244 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 1245 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1246 } 1247 1248 SPDK_DEBUGLOG(nvmf, "name: %s\n", prop->name); 1249 1250 response->value.u64 = prop->get_cb(ctrlr); 1251 1252 if (size != prop->size) { 1253 /* The size must be 4 and the prop->size is 8. Figure out which part of the property to read. */ 1254 assert(size == 4); 1255 assert(prop->size == 8); 1256 1257 if (cmd->ofst == prop->ofst) { 1258 /* Keep bottom 4 bytes only */ 1259 response->value.u64 &= 0xFFFFFFFF; 1260 } else { 1261 /* Keep top 4 bytes only */ 1262 response->value.u64 >>= 32; 1263 } 1264 } 1265 1266 SPDK_DEBUGLOG(nvmf, "response value: 0x%" PRIx64 "\n", response->value.u64); 1267 1268 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1269 } 1270 1271 static int 1272 nvmf_property_set(struct spdk_nvmf_request *req) 1273 { 1274 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1275 struct spdk_nvmf_fabric_prop_set_cmd *cmd = &req->cmd->prop_set_cmd; 1276 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1277 const struct nvmf_prop *prop; 1278 uint64_t value; 1279 uint8_t size; 1280 bool ret; 1281 1282 SPDK_DEBUGLOG(nvmf, "size %d, offset 0x%x, value 0x%" PRIx64 "\n", 1283 cmd->attrib.size, cmd->ofst, cmd->value.u64); 1284 1285 switch (cmd->attrib.size) { 1286 case SPDK_NVMF_PROP_SIZE_4: 1287 size = 4; 1288 break; 1289 case SPDK_NVMF_PROP_SIZE_8: 1290 size = 8; 1291 break; 1292 default: 1293 SPDK_ERRLOG("Invalid size value %d\n", cmd->attrib.size); 1294 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1295 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 1296 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1297 } 1298 1299 prop = find_prop(cmd->ofst, size); 1300 if (prop == NULL || prop->set_cb == NULL) { 1301 SPDK_INFOLOG(nvmf, "Invalid offset 0x%x\n", cmd->ofst); 1302 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1303 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 1304 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1305 } 1306 1307 SPDK_DEBUGLOG(nvmf, "name: %s\n", prop->name); 1308 1309 value = cmd->value.u64; 1310 1311 if (prop->size == 4) { 1312 ret = prop->set_cb(ctrlr, (uint32_t)value); 1313 } else if (size != prop->size) { 1314 /* The size must be 4 and the prop->size is 8. Figure out which part of the property to write. */ 1315 assert(size == 4); 1316 assert(prop->size == 8); 1317 1318 if (cmd->ofst == prop->ofst) { 1319 ret = prop->set_cb(ctrlr, (uint32_t)value); 1320 } else { 1321 ret = prop->set_upper_cb(ctrlr, (uint32_t)value); 1322 } 1323 } else { 1324 ret = prop->set_cb(ctrlr, (uint32_t)value); 1325 if (ret) { 1326 ret = prop->set_upper_cb(ctrlr, (uint32_t)(value >> 32)); 1327 } 1328 } 1329 1330 if (!ret) { 1331 SPDK_ERRLOG("prop set_cb failed\n"); 1332 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1333 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 1334 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1335 } 1336 1337 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1338 } 1339 1340 static int 1341 nvmf_ctrlr_set_features_arbitration(struct spdk_nvmf_request *req) 1342 { 1343 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1344 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1345 1346 SPDK_DEBUGLOG(nvmf, "Set Features - Arbitration (cdw11 = 0x%0x)\n", cmd->cdw11); 1347 1348 ctrlr->feat.arbitration.raw = cmd->cdw11; 1349 ctrlr->feat.arbitration.bits.reserved = 0; 1350 1351 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1352 } 1353 1354 static int 1355 nvmf_ctrlr_set_features_power_management(struct spdk_nvmf_request *req) 1356 { 1357 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1358 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1359 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1360 1361 SPDK_DEBUGLOG(nvmf, "Set Features - Power Management (cdw11 = 0x%0x)\n", cmd->cdw11); 1362 1363 /* Only PS = 0 is allowed, since we report NPSS = 0 */ 1364 if (cmd->cdw11_bits.feat_power_management.bits.ps != 0) { 1365 SPDK_ERRLOG("Invalid power state %u\n", cmd->cdw11_bits.feat_power_management.bits.ps); 1366 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1367 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1368 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1369 } 1370 1371 ctrlr->feat.power_management.raw = cmd->cdw11; 1372 ctrlr->feat.power_management.bits.reserved = 0; 1373 1374 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1375 } 1376 1377 static bool 1378 temp_threshold_opts_valid(const union spdk_nvme_feat_temperature_threshold *opts) 1379 { 1380 /* 1381 * Valid TMPSEL values: 1382 * 0000b - 1000b: temperature sensors 1383 * 1111b: set all implemented temperature sensors 1384 */ 1385 if (opts->bits.tmpsel >= 9 && opts->bits.tmpsel != 15) { 1386 /* 1001b - 1110b: reserved */ 1387 SPDK_ERRLOG("Invalid TMPSEL %u\n", opts->bits.tmpsel); 1388 return false; 1389 } 1390 1391 /* 1392 * Valid THSEL values: 1393 * 00b: over temperature threshold 1394 * 01b: under temperature threshold 1395 */ 1396 if (opts->bits.thsel > 1) { 1397 /* 10b - 11b: reserved */ 1398 SPDK_ERRLOG("Invalid THSEL %u\n", opts->bits.thsel); 1399 return false; 1400 } 1401 1402 return true; 1403 } 1404 1405 static int 1406 nvmf_ctrlr_set_features_temperature_threshold(struct spdk_nvmf_request *req) 1407 { 1408 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1409 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1410 1411 SPDK_DEBUGLOG(nvmf, "Set Features - Temperature Threshold (cdw11 = 0x%0x)\n", cmd->cdw11); 1412 1413 if (!temp_threshold_opts_valid(&cmd->cdw11_bits.feat_temp_threshold)) { 1414 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1415 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1416 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1417 } 1418 1419 /* TODO: no sensors implemented - ignore new values */ 1420 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1421 } 1422 1423 static int 1424 nvmf_ctrlr_get_features_temperature_threshold(struct spdk_nvmf_request *req) 1425 { 1426 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1427 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1428 1429 SPDK_DEBUGLOG(nvmf, "Get Features - Temperature Threshold (cdw11 = 0x%0x)\n", cmd->cdw11); 1430 1431 if (!temp_threshold_opts_valid(&cmd->cdw11_bits.feat_temp_threshold)) { 1432 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1433 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1434 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1435 } 1436 1437 /* TODO: no sensors implemented - return 0 for all thresholds */ 1438 rsp->cdw0 = 0; 1439 1440 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1441 } 1442 1443 static int 1444 nvmf_ctrlr_get_features_interrupt_vector_configuration(struct spdk_nvmf_request *req) 1445 { 1446 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1447 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1448 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1449 union spdk_nvme_feat_interrupt_vector_configuration iv_conf = {}; 1450 1451 SPDK_DEBUGLOG(nvmf, "Get Features - Interrupt Vector Configuration (cdw11 = 0x%0x)\n", cmd->cdw11); 1452 1453 iv_conf.bits.iv = cmd->cdw11_bits.feat_interrupt_vector_configuration.bits.iv; 1454 iv_conf.bits.cd = ctrlr->feat.interrupt_vector_configuration.bits.cd; 1455 rsp->cdw0 = iv_conf.raw; 1456 1457 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1458 } 1459 1460 static int 1461 nvmf_ctrlr_set_features_error_recovery(struct spdk_nvmf_request *req) 1462 { 1463 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1464 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1465 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1466 1467 SPDK_DEBUGLOG(nvmf, "Set Features - Error Recovery (cdw11 = 0x%0x)\n", cmd->cdw11); 1468 1469 if (cmd->cdw11_bits.feat_error_recovery.bits.dulbe) { 1470 /* 1471 * Host is not allowed to set this bit, since we don't advertise it in 1472 * Identify Namespace. 1473 */ 1474 SPDK_ERRLOG("Host set unsupported DULBE bit\n"); 1475 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1476 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1477 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1478 } 1479 1480 ctrlr->feat.error_recovery.raw = cmd->cdw11; 1481 ctrlr->feat.error_recovery.bits.reserved = 0; 1482 1483 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1484 } 1485 1486 static int 1487 nvmf_ctrlr_set_features_volatile_write_cache(struct spdk_nvmf_request *req) 1488 { 1489 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1490 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1491 1492 SPDK_DEBUGLOG(nvmf, "Set Features - Volatile Write Cache (cdw11 = 0x%0x)\n", cmd->cdw11); 1493 1494 ctrlr->feat.volatile_write_cache.raw = cmd->cdw11; 1495 ctrlr->feat.volatile_write_cache.bits.reserved = 0; 1496 1497 SPDK_DEBUGLOG(nvmf, "Set Features - Volatile Write Cache %s\n", 1498 ctrlr->feat.volatile_write_cache.bits.wce ? "Enabled" : "Disabled"); 1499 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1500 } 1501 1502 static int 1503 nvmf_ctrlr_set_features_write_atomicity(struct spdk_nvmf_request *req) 1504 { 1505 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1506 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1507 1508 SPDK_DEBUGLOG(nvmf, "Set Features - Write Atomicity (cdw11 = 0x%0x)\n", cmd->cdw11); 1509 1510 ctrlr->feat.write_atomicity.raw = cmd->cdw11; 1511 ctrlr->feat.write_atomicity.bits.reserved = 0; 1512 1513 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1514 } 1515 1516 static int 1517 nvmf_ctrlr_set_features_host_identifier(struct spdk_nvmf_request *req) 1518 { 1519 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1520 1521 SPDK_ERRLOG("Set Features - Host Identifier not allowed\n"); 1522 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 1523 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1524 } 1525 1526 static int 1527 nvmf_ctrlr_get_features_host_identifier(struct spdk_nvmf_request *req) 1528 { 1529 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1530 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1531 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1532 1533 SPDK_DEBUGLOG(nvmf, "Get Features - Host Identifier\n"); 1534 1535 if (!cmd->cdw11_bits.feat_host_identifier.bits.exhid) { 1536 /* NVMe over Fabrics requires EXHID=1 (128-bit/16-byte host ID) */ 1537 SPDK_ERRLOG("Get Features - Host Identifier with EXHID=0 not allowed\n"); 1538 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1539 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1540 } 1541 1542 if (req->data == NULL || req->length < sizeof(ctrlr->hostid)) { 1543 SPDK_ERRLOG("Invalid data buffer for Get Features - Host Identifier\n"); 1544 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1545 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1546 } 1547 1548 spdk_uuid_copy((struct spdk_uuid *)req->data, &ctrlr->hostid); 1549 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1550 } 1551 1552 static int 1553 nvmf_ctrlr_get_features_reservation_notification_mask(struct spdk_nvmf_request *req) 1554 { 1555 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1556 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1557 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1558 struct spdk_nvmf_ns *ns; 1559 1560 SPDK_DEBUGLOG(nvmf, "get Features - Reservation Notificaton Mask\n"); 1561 1562 if (cmd->nsid == SPDK_NVME_GLOBAL_NS_TAG) { 1563 SPDK_ERRLOG("get Features - Invalid Namespace ID\n"); 1564 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1565 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1566 } 1567 1568 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 1569 if (ns == NULL) { 1570 SPDK_ERRLOG("Set Features - Invalid Namespace ID\n"); 1571 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1572 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1573 } 1574 rsp->cdw0 = ns->mask; 1575 1576 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1577 } 1578 1579 static int 1580 nvmf_ctrlr_set_features_reservation_notification_mask(struct spdk_nvmf_request *req) 1581 { 1582 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1583 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 1584 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1585 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1586 struct spdk_nvmf_ns *ns; 1587 1588 SPDK_DEBUGLOG(nvmf, "Set Features - Reservation Notificaton Mask\n"); 1589 1590 if (cmd->nsid == SPDK_NVME_GLOBAL_NS_TAG) { 1591 for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL; 1592 ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) { 1593 ns->mask = cmd->cdw11; 1594 } 1595 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1596 } 1597 1598 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 1599 if (ns == NULL) { 1600 SPDK_ERRLOG("Set Features - Invalid Namespace ID\n"); 1601 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1602 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1603 } 1604 ns->mask = cmd->cdw11; 1605 1606 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1607 } 1608 1609 static int 1610 nvmf_ctrlr_get_features_reservation_persistence(struct spdk_nvmf_request *req) 1611 { 1612 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1613 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1614 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1615 struct spdk_nvmf_ns *ns; 1616 1617 SPDK_DEBUGLOG(nvmf, "Get Features - Reservation Persistence\n"); 1618 1619 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 1620 /* NSID with SPDK_NVME_GLOBAL_NS_TAG (=0xffffffff) also included */ 1621 if (ns == NULL) { 1622 SPDK_ERRLOG("Get Features - Invalid Namespace ID\n"); 1623 response->status.sct = SPDK_NVME_SCT_GENERIC; 1624 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1625 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1626 } 1627 1628 response->cdw0 = ns->ptpl_activated; 1629 1630 response->status.sct = SPDK_NVME_SCT_GENERIC; 1631 response->status.sc = SPDK_NVME_SC_SUCCESS; 1632 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1633 } 1634 1635 static int 1636 nvmf_ctrlr_set_features_reservation_persistence(struct spdk_nvmf_request *req) 1637 { 1638 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1639 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1640 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1641 struct spdk_nvmf_ns *ns; 1642 bool ptpl; 1643 1644 SPDK_DEBUGLOG(nvmf, "Set Features - Reservation Persistence\n"); 1645 1646 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 1647 ptpl = cmd->cdw11_bits.feat_rsv_persistence.bits.ptpl; 1648 1649 if (cmd->nsid != SPDK_NVME_GLOBAL_NS_TAG && ns && ns->ptpl_file) { 1650 ns->ptpl_activated = ptpl; 1651 } else if (cmd->nsid == SPDK_NVME_GLOBAL_NS_TAG) { 1652 for (ns = spdk_nvmf_subsystem_get_first_ns(ctrlr->subsys); ns && ns->ptpl_file; 1653 ns = spdk_nvmf_subsystem_get_next_ns(ctrlr->subsys, ns)) { 1654 ns->ptpl_activated = ptpl; 1655 } 1656 } else { 1657 SPDK_ERRLOG("Set Features - Invalid Namespace ID or Reservation Configuration\n"); 1658 response->status.sct = SPDK_NVME_SCT_GENERIC; 1659 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1660 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1661 } 1662 1663 /* TODO: Feature not changeable for now */ 1664 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1665 response->status.sc = SPDK_NVME_SC_FEATURE_ID_NOT_SAVEABLE; 1666 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1667 } 1668 1669 static int 1670 nvmf_ctrlr_set_features_host_behavior_support(struct spdk_nvmf_request *req) 1671 { 1672 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1673 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1674 struct spdk_nvme_host_behavior *host_behavior; 1675 1676 SPDK_DEBUGLOG(nvmf, "Set Features - Host Behavior Support\n"); 1677 if (req->iovcnt != 1) { 1678 SPDK_ERRLOG("Host Behavior Support invalid iovcnt: %d\n", req->iovcnt); 1679 response->status.sct = SPDK_NVME_SCT_GENERIC; 1680 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1681 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1682 } 1683 if (req->iov[0].iov_len != sizeof(struct spdk_nvme_host_behavior)) { 1684 SPDK_ERRLOG("Host Behavior Support invalid iov_len: %zd\n", req->iov[0].iov_len); 1685 response->status.sct = SPDK_NVME_SCT_GENERIC; 1686 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1687 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1688 } 1689 1690 host_behavior = (struct spdk_nvme_host_behavior *)req->iov[0].iov_base; 1691 if (host_behavior->acre == 0) { 1692 ctrlr->acre_enabled = false; 1693 } else if (host_behavior->acre == 1) { 1694 ctrlr->acre_enabled = true; 1695 } else { 1696 SPDK_ERRLOG("Host Behavior Support invalid acre: 0x%02x\n", host_behavior->acre); 1697 response->status.sct = SPDK_NVME_SCT_GENERIC; 1698 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1699 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1700 } 1701 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1702 } 1703 1704 static int 1705 nvmf_ctrlr_set_features_keep_alive_timer(struct spdk_nvmf_request *req) 1706 { 1707 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1708 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1709 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1710 1711 SPDK_DEBUGLOG(nvmf, "Set Features - Keep Alive Timer (%u ms)\n", cmd->cdw11); 1712 1713 /* 1714 * if attempts to disable keep alive by setting kato to 0h 1715 * a status value of keep alive invalid shall be returned 1716 */ 1717 if (cmd->cdw11_bits.feat_keep_alive_timer.bits.kato == 0) { 1718 rsp->status.sc = SPDK_NVME_SC_KEEP_ALIVE_INVALID; 1719 } else if (cmd->cdw11_bits.feat_keep_alive_timer.bits.kato < MIN_KEEP_ALIVE_TIMEOUT_IN_MS) { 1720 ctrlr->feat.keep_alive_timer.bits.kato = MIN_KEEP_ALIVE_TIMEOUT_IN_MS; 1721 } else { 1722 /* round up to milliseconds */ 1723 ctrlr->feat.keep_alive_timer.bits.kato = spdk_divide_round_up( 1724 cmd->cdw11_bits.feat_keep_alive_timer.bits.kato, 1725 KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS) * 1726 KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS; 1727 } 1728 1729 /* 1730 * if change the keep alive timeout value successfully 1731 * update the keep alive poller. 1732 */ 1733 if (cmd->cdw11_bits.feat_keep_alive_timer.bits.kato != 0) { 1734 if (ctrlr->keep_alive_poller != NULL) { 1735 spdk_poller_unregister(&ctrlr->keep_alive_poller); 1736 } 1737 ctrlr->keep_alive_poller = SPDK_POLLER_REGISTER(nvmf_ctrlr_keep_alive_poll, ctrlr, 1738 ctrlr->feat.keep_alive_timer.bits.kato * 1000); 1739 } 1740 1741 SPDK_DEBUGLOG(nvmf, "Set Features - Keep Alive Timer set to %u ms\n", 1742 ctrlr->feat.keep_alive_timer.bits.kato); 1743 1744 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1745 } 1746 1747 static int 1748 nvmf_ctrlr_set_features_number_of_queues(struct spdk_nvmf_request *req) 1749 { 1750 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1751 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1752 uint32_t count; 1753 1754 SPDK_DEBUGLOG(nvmf, "Set Features - Number of Queues, cdw11 0x%x\n", 1755 req->cmd->nvme_cmd.cdw11); 1756 1757 count = spdk_bit_array_count_set(ctrlr->qpair_mask); 1758 /* verify that the controller is ready to process commands */ 1759 if (count > 1) { 1760 SPDK_DEBUGLOG(nvmf, "Queue pairs already active!\n"); 1761 rsp->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 1762 } else { 1763 /* 1764 * Ignore the value requested by the host - 1765 * always return the pre-configured value based on max_qpairs_allowed. 1766 */ 1767 rsp->cdw0 = ctrlr->feat.number_of_queues.raw; 1768 } 1769 1770 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1771 } 1772 1773 static int 1774 nvmf_ctrlr_set_features_async_event_configuration(struct spdk_nvmf_request *req) 1775 { 1776 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1777 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1778 1779 SPDK_DEBUGLOG(nvmf, "Set Features - Async Event Configuration, cdw11 0x%08x\n", 1780 cmd->cdw11); 1781 ctrlr->feat.async_event_configuration.raw = cmd->cdw11; 1782 ctrlr->feat.async_event_configuration.bits.reserved1 = 0; 1783 ctrlr->feat.async_event_configuration.bits.reserved2 = 0; 1784 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1785 } 1786 1787 static int 1788 nvmf_ctrlr_async_event_request(struct spdk_nvmf_request *req) 1789 { 1790 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1791 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1792 struct spdk_nvmf_subsystem_poll_group *sgroup; 1793 struct spdk_nvmf_async_event_completion *pending_event; 1794 1795 SPDK_DEBUGLOG(nvmf, "Async Event Request\n"); 1796 1797 /* AER cmd is an exception */ 1798 sgroup = &req->qpair->group->sgroups[ctrlr->subsys->id]; 1799 assert(sgroup != NULL); 1800 sgroup->mgmt_io_outstanding--; 1801 1802 /* Four asynchronous events are supported for now */ 1803 if (ctrlr->nr_aer_reqs >= NVMF_MAX_ASYNC_EVENTS) { 1804 SPDK_DEBUGLOG(nvmf, "AERL exceeded\n"); 1805 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1806 rsp->status.sc = SPDK_NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED; 1807 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1808 } 1809 1810 if (!STAILQ_EMPTY(&ctrlr->async_events)) { 1811 pending_event = STAILQ_FIRST(&ctrlr->async_events); 1812 rsp->cdw0 = pending_event->event.raw; 1813 STAILQ_REMOVE(&ctrlr->async_events, pending_event, spdk_nvmf_async_event_completion, link); 1814 free(pending_event); 1815 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1816 } 1817 1818 ctrlr->aer_req[ctrlr->nr_aer_reqs++] = req; 1819 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 1820 } 1821 1822 struct copy_iovs_ctx { 1823 struct iovec *iovs; 1824 int iovcnt; 1825 int cur_iov_idx; 1826 size_t cur_iov_offset; 1827 }; 1828 1829 static void 1830 _init_copy_iovs_ctx(struct copy_iovs_ctx *copy_ctx, struct iovec *iovs, int iovcnt) 1831 { 1832 int iov_idx = 0; 1833 struct iovec *iov; 1834 1835 copy_ctx->iovs = iovs; 1836 copy_ctx->iovcnt = iovcnt; 1837 copy_ctx->cur_iov_idx = 0; 1838 copy_ctx->cur_iov_offset = 0; 1839 1840 while (iov_idx < copy_ctx->iovcnt) { 1841 iov = ©_ctx->iovs[iov_idx]; 1842 memset(iov->iov_base, 0, iov->iov_len); 1843 iov_idx++; 1844 } 1845 } 1846 1847 static size_t 1848 _copy_buf_to_iovs(struct copy_iovs_ctx *copy_ctx, const void *buf, size_t buf_len) 1849 { 1850 size_t len, iov_remain_len, copied_len = 0; 1851 struct iovec *iov; 1852 1853 if (buf_len == 0) { 1854 return 0; 1855 } 1856 1857 while (copy_ctx->cur_iov_idx < copy_ctx->iovcnt) { 1858 iov = ©_ctx->iovs[copy_ctx->cur_iov_idx]; 1859 iov_remain_len = iov->iov_len - copy_ctx->cur_iov_offset; 1860 if (iov_remain_len == 0) { 1861 copy_ctx->cur_iov_idx++; 1862 copy_ctx->cur_iov_offset = 0; 1863 continue; 1864 } 1865 1866 len = spdk_min(iov_remain_len, buf_len - copied_len); 1867 memcpy((char *)iov->iov_base + copy_ctx->cur_iov_offset, 1868 (const char *)buf + copied_len, 1869 len); 1870 copied_len += len; 1871 copy_ctx->cur_iov_offset += len; 1872 1873 if (buf_len == copied_len) { 1874 return copied_len; 1875 } 1876 } 1877 1878 return copied_len; 1879 } 1880 1881 static void 1882 nvmf_get_firmware_slot_log_page(struct iovec *iovs, int iovcnt, uint64_t offset, uint32_t length) 1883 { 1884 struct spdk_nvme_firmware_page fw_page; 1885 size_t copy_len; 1886 struct copy_iovs_ctx copy_ctx; 1887 1888 _init_copy_iovs_ctx(©_ctx, iovs, iovcnt); 1889 1890 memset(&fw_page, 0, sizeof(fw_page)); 1891 fw_page.afi.active_slot = 1; 1892 fw_page.afi.next_reset_slot = 0; 1893 spdk_strcpy_pad(fw_page.revision[0], FW_VERSION, sizeof(fw_page.revision[0]), ' '); 1894 1895 if (offset < sizeof(fw_page)) { 1896 copy_len = spdk_min(sizeof(fw_page) - offset, length); 1897 if (copy_len > 0) { 1898 _copy_buf_to_iovs(©_ctx, (const char *)&fw_page + offset, copy_len); 1899 } 1900 } 1901 } 1902 1903 /* 1904 * Asynchronous Event Mask Bit 1905 */ 1906 enum spdk_nvme_async_event_mask_bit { 1907 /* Mask Namespace Change Notificaton */ 1908 SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGE_MASK_BIT = 0, 1909 /* Mask Asymmetric Namespace Access Change Notification */ 1910 SPDK_NVME_ASYNC_EVENT_ANA_CHANGE_MASK_BIT = 1, 1911 /* Mask Discovery Log Change Notification */ 1912 SPDK_NVME_ASYNC_EVENT_DISCOVERY_LOG_CHANGE_MASK_BIT = 2, 1913 /* Mask Reservation Log Page Available Notification */ 1914 SPDK_NVME_ASYNC_EVENT_RESERVATION_LOG_AVAIL_MASK_BIT = 3, 1915 1916 /* 4 - 63 Reserved */ 1917 }; 1918 1919 static inline void 1920 nvmf_ctrlr_unmask_aen(struct spdk_nvmf_ctrlr *ctrlr, 1921 enum spdk_nvme_async_event_mask_bit mask) 1922 { 1923 ctrlr->notice_aen_mask &= ~(1 << mask); 1924 } 1925 1926 static inline bool 1927 nvmf_ctrlr_mask_aen(struct spdk_nvmf_ctrlr *ctrlr, 1928 enum spdk_nvme_async_event_mask_bit mask) 1929 { 1930 if (ctrlr->notice_aen_mask & (1 << mask)) { 1931 return false; 1932 } else { 1933 ctrlr->notice_aen_mask |= (1 << mask); 1934 return true; 1935 } 1936 } 1937 1938 #define SPDK_NVMF_ANA_DESC_SIZE (sizeof(struct spdk_nvme_ana_group_descriptor) + \ 1939 sizeof(uint32_t)) 1940 static void 1941 nvmf_get_ana_log_page(struct spdk_nvmf_ctrlr *ctrlr, struct iovec *iovs, int iovcnt, 1942 uint64_t offset, uint32_t length, uint32_t rae) 1943 { 1944 struct spdk_nvme_ana_page ana_hdr; 1945 char _ana_desc[SPDK_NVMF_ANA_DESC_SIZE]; 1946 struct spdk_nvme_ana_group_descriptor *ana_desc; 1947 size_t copy_len, copied_len; 1948 uint32_t num_ns = 0; 1949 struct spdk_nvmf_ns *ns; 1950 struct copy_iovs_ctx copy_ctx; 1951 1952 _init_copy_iovs_ctx(©_ctx, iovs, iovcnt); 1953 1954 if (length == 0) { 1955 return; 1956 } 1957 1958 if (offset >= sizeof(ana_hdr)) { 1959 offset -= sizeof(ana_hdr); 1960 } else { 1961 for (ns = spdk_nvmf_subsystem_get_first_ns(ctrlr->subsys); ns != NULL; 1962 ns = spdk_nvmf_subsystem_get_next_ns(ctrlr->subsys, ns)) { 1963 num_ns++; 1964 } 1965 1966 memset(&ana_hdr, 0, sizeof(ana_hdr)); 1967 1968 ana_hdr.num_ana_group_desc = num_ns; 1969 /* TODO: Support Change Count. */ 1970 ana_hdr.change_count = 0; 1971 1972 copy_len = spdk_min(sizeof(ana_hdr) - offset, length); 1973 copied_len = _copy_buf_to_iovs(©_ctx, (const char *)&ana_hdr + offset, copy_len); 1974 assert(copied_len == copy_len); 1975 length -= copied_len; 1976 offset = 0; 1977 } 1978 1979 if (length == 0) { 1980 return; 1981 } 1982 1983 ana_desc = (void *)_ana_desc; 1984 1985 for (ns = spdk_nvmf_subsystem_get_first_ns(ctrlr->subsys); ns != NULL; 1986 ns = spdk_nvmf_subsystem_get_next_ns(ctrlr->subsys, ns)) { 1987 if (offset >= SPDK_NVMF_ANA_DESC_SIZE) { 1988 offset -= SPDK_NVMF_ANA_DESC_SIZE; 1989 continue; 1990 } 1991 1992 memset(ana_desc, 0, SPDK_NVMF_ANA_DESC_SIZE); 1993 1994 ana_desc->ana_group_id = ns->nsid; 1995 ana_desc->num_of_nsid = 1; 1996 ana_desc->ana_state = ctrlr->listener->ana_state; 1997 ana_desc->nsid[0] = ns->nsid; 1998 /* TODO: Support Change Count. */ 1999 ana_desc->change_count = 0; 2000 2001 copy_len = spdk_min(SPDK_NVMF_ANA_DESC_SIZE - offset, length); 2002 copied_len = _copy_buf_to_iovs(©_ctx, (const char *)ana_desc + offset, copy_len); 2003 assert(copied_len == copy_len); 2004 length -= copied_len; 2005 offset = 0; 2006 2007 if (length == 0) { 2008 goto done; 2009 } 2010 } 2011 2012 done: 2013 if (!rae) { 2014 nvmf_ctrlr_unmask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_ANA_CHANGE_MASK_BIT); 2015 } 2016 } 2017 2018 void 2019 nvmf_ctrlr_ns_changed(struct spdk_nvmf_ctrlr *ctrlr, uint32_t nsid) 2020 { 2021 uint16_t max_changes = SPDK_COUNTOF(ctrlr->changed_ns_list.ns_list); 2022 uint16_t i; 2023 bool found = false; 2024 2025 for (i = 0; i < ctrlr->changed_ns_list_count; i++) { 2026 if (ctrlr->changed_ns_list.ns_list[i] == nsid) { 2027 /* nsid is already in the list */ 2028 found = true; 2029 break; 2030 } 2031 } 2032 2033 if (!found) { 2034 if (ctrlr->changed_ns_list_count == max_changes) { 2035 /* Out of space - set first entry to FFFFFFFFh and zero-fill the rest. */ 2036 ctrlr->changed_ns_list.ns_list[0] = 0xFFFFFFFFu; 2037 for (i = 1; i < max_changes; i++) { 2038 ctrlr->changed_ns_list.ns_list[i] = 0; 2039 } 2040 } else { 2041 ctrlr->changed_ns_list.ns_list[ctrlr->changed_ns_list_count++] = nsid; 2042 } 2043 } 2044 } 2045 2046 static void 2047 nvmf_get_changed_ns_list_log_page(struct spdk_nvmf_ctrlr *ctrlr, 2048 struct iovec *iovs, int iovcnt, uint64_t offset, uint32_t length, uint32_t rae) 2049 { 2050 size_t copy_length; 2051 struct copy_iovs_ctx copy_ctx; 2052 2053 _init_copy_iovs_ctx(©_ctx, iovs, iovcnt); 2054 2055 if (offset < sizeof(ctrlr->changed_ns_list)) { 2056 copy_length = spdk_min(length, sizeof(ctrlr->changed_ns_list) - offset); 2057 if (copy_length) { 2058 _copy_buf_to_iovs(©_ctx, (char *)&ctrlr->changed_ns_list + offset, copy_length); 2059 } 2060 } 2061 2062 /* Clear log page each time it is read */ 2063 ctrlr->changed_ns_list_count = 0; 2064 memset(&ctrlr->changed_ns_list, 0, sizeof(ctrlr->changed_ns_list)); 2065 2066 if (!rae) { 2067 nvmf_ctrlr_unmask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGE_MASK_BIT); 2068 } 2069 } 2070 2071 /* The structure can be modified if we provide support for other commands in future */ 2072 static const struct spdk_nvme_cmds_and_effect_log_page g_cmds_and_effect_log_page = { 2073 .admin_cmds_supported = { 2074 /* CSUPP, LBCC, NCC, NIC, CCC, CSE */ 2075 /* Get Log Page */ 2076 [SPDK_NVME_OPC_GET_LOG_PAGE] = {1, 0, 0, 0, 0, 0, 0, 0}, 2077 /* Identify */ 2078 [SPDK_NVME_OPC_IDENTIFY] = {1, 0, 0, 0, 0, 0, 0, 0}, 2079 /* Abort */ 2080 [SPDK_NVME_OPC_ABORT] = {1, 0, 0, 0, 0, 0, 0, 0}, 2081 /* Set Features */ 2082 [SPDK_NVME_OPC_SET_FEATURES] = {1, 0, 0, 0, 0, 0, 0, 0}, 2083 /* Get Features */ 2084 [SPDK_NVME_OPC_GET_FEATURES] = {1, 0, 0, 0, 0, 0, 0, 0}, 2085 /* Async Event Request */ 2086 [SPDK_NVME_OPC_ASYNC_EVENT_REQUEST] = {1, 0, 0, 0, 0, 0, 0, 0}, 2087 /* Keep Alive */ 2088 [SPDK_NVME_OPC_KEEP_ALIVE] = {1, 0, 0, 0, 0, 0, 0, 0}, 2089 }, 2090 .io_cmds_supported = { 2091 /* FLUSH */ 2092 [SPDK_NVME_OPC_FLUSH] = {1, 1, 0, 0, 0, 0, 0, 0}, 2093 /* WRITE */ 2094 [SPDK_NVME_OPC_WRITE] = {1, 1, 0, 0, 0, 0, 0, 0}, 2095 /* READ */ 2096 [SPDK_NVME_OPC_READ] = {1, 0, 0, 0, 0, 0, 0, 0}, 2097 /* WRITE ZEROES */ 2098 [SPDK_NVME_OPC_WRITE_ZEROES] = {1, 1, 0, 0, 0, 0, 0, 0}, 2099 /* DATASET MANAGEMENT */ 2100 [SPDK_NVME_OPC_DATASET_MANAGEMENT] = {1, 1, 0, 0, 0, 0, 0, 0}, 2101 /* COMPARE */ 2102 [SPDK_NVME_OPC_COMPARE] = {1, 0, 0, 0, 0, 0, 0, 0}, 2103 }, 2104 }; 2105 2106 static void 2107 nvmf_get_cmds_and_effects_log_page(struct iovec *iovs, int iovcnt, 2108 uint64_t offset, uint32_t length) 2109 { 2110 uint32_t page_size = sizeof(struct spdk_nvme_cmds_and_effect_log_page); 2111 size_t copy_len = 0; 2112 struct copy_iovs_ctx copy_ctx; 2113 2114 _init_copy_iovs_ctx(©_ctx, iovs, iovcnt); 2115 2116 if (offset < page_size) { 2117 copy_len = spdk_min(page_size - offset, length); 2118 _copy_buf_to_iovs(©_ctx, (char *)(&g_cmds_and_effect_log_page) + offset, copy_len); 2119 } 2120 } 2121 2122 static void 2123 nvmf_get_reservation_notification_log_page(struct spdk_nvmf_ctrlr *ctrlr, 2124 struct iovec *iovs, int iovcnt, uint64_t offset, uint32_t length, uint32_t rae) 2125 { 2126 uint32_t unit_log_len, avail_log_len, next_pos, copy_len; 2127 struct spdk_nvmf_reservation_log *log, *log_tmp; 2128 struct copy_iovs_ctx copy_ctx; 2129 2130 _init_copy_iovs_ctx(©_ctx, iovs, iovcnt); 2131 2132 unit_log_len = sizeof(struct spdk_nvme_reservation_notification_log); 2133 /* No available log, return zeroed log pages */ 2134 if (!ctrlr->num_avail_log_pages) { 2135 return; 2136 } 2137 2138 avail_log_len = ctrlr->num_avail_log_pages * unit_log_len; 2139 if (offset >= avail_log_len) { 2140 return; 2141 } 2142 2143 next_pos = 0; 2144 TAILQ_FOREACH_SAFE(log, &ctrlr->log_head, link, log_tmp) { 2145 TAILQ_REMOVE(&ctrlr->log_head, log, link); 2146 ctrlr->num_avail_log_pages--; 2147 2148 next_pos += unit_log_len; 2149 if (next_pos > offset) { 2150 copy_len = spdk_min(next_pos - offset, length); 2151 _copy_buf_to_iovs(©_ctx, &log->log, copy_len); 2152 length -= copy_len; 2153 offset += copy_len; 2154 } 2155 free(log); 2156 2157 if (length == 0) { 2158 break; 2159 } 2160 } 2161 2162 if (!rae) { 2163 nvmf_ctrlr_unmask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_RESERVATION_LOG_AVAIL_MASK_BIT); 2164 } 2165 return; 2166 } 2167 2168 static int 2169 nvmf_ctrlr_get_log_page(struct spdk_nvmf_request *req) 2170 { 2171 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2172 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 2173 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2174 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 2175 uint64_t offset, len; 2176 uint32_t rae, numdl, numdu; 2177 uint8_t lid; 2178 2179 if (req->data == NULL) { 2180 SPDK_ERRLOG("get log command with no buffer\n"); 2181 response->status.sct = SPDK_NVME_SCT_GENERIC; 2182 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2183 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2184 } 2185 2186 offset = (uint64_t)cmd->cdw12 | ((uint64_t)cmd->cdw13 << 32); 2187 if (offset & 3) { 2188 SPDK_ERRLOG("Invalid log page offset 0x%" PRIx64 "\n", offset); 2189 response->status.sct = SPDK_NVME_SCT_GENERIC; 2190 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2191 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2192 } 2193 2194 rae = cmd->cdw10_bits.get_log_page.rae; 2195 numdl = cmd->cdw10_bits.get_log_page.numdl; 2196 numdu = cmd->cdw11_bits.get_log_page.numdu; 2197 len = ((numdu << 16) + numdl + (uint64_t)1) * 4; 2198 if (len > req->length) { 2199 SPDK_ERRLOG("Get log page: len (%" PRIu64 ") > buf size (%u)\n", 2200 len, req->length); 2201 response->status.sct = SPDK_NVME_SCT_GENERIC; 2202 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2203 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2204 } 2205 2206 lid = cmd->cdw10_bits.get_log_page.lid; 2207 SPDK_DEBUGLOG(nvmf, "Get log page: LID=0x%02X offset=0x%" PRIx64 " len=0x%" PRIx64 " rae=%u\n", 2208 lid, offset, len, rae); 2209 2210 if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 2211 switch (lid) { 2212 case SPDK_NVME_LOG_DISCOVERY: 2213 nvmf_get_discovery_log_page(subsystem->tgt, ctrlr->hostnqn, req->iov, req->iovcnt, offset, 2214 len); 2215 if (!rae) { 2216 nvmf_ctrlr_unmask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_DISCOVERY_LOG_CHANGE_MASK_BIT); 2217 } 2218 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2219 default: 2220 goto invalid_log_page; 2221 } 2222 } else { 2223 switch (lid) { 2224 case SPDK_NVME_LOG_ERROR: 2225 case SPDK_NVME_LOG_HEALTH_INFORMATION: 2226 /* TODO: actually fill out log page data */ 2227 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2228 case SPDK_NVME_LOG_FIRMWARE_SLOT: 2229 nvmf_get_firmware_slot_log_page(req->iov, req->iovcnt, offset, len); 2230 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2231 case SPDK_NVME_LOG_ASYMMETRIC_NAMESPACE_ACCESS: 2232 if (subsystem->flags.ana_reporting) { 2233 nvmf_get_ana_log_page(ctrlr, req->iov, req->iovcnt, offset, len, rae); 2234 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2235 } else { 2236 goto invalid_log_page; 2237 } 2238 case SPDK_NVME_LOG_COMMAND_EFFECTS_LOG: 2239 nvmf_get_cmds_and_effects_log_page(req->iov, req->iovcnt, offset, len); 2240 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2241 case SPDK_NVME_LOG_CHANGED_NS_LIST: 2242 nvmf_get_changed_ns_list_log_page(ctrlr, req->iov, req->iovcnt, offset, len, rae); 2243 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2244 case SPDK_NVME_LOG_RESERVATION_NOTIFICATION: 2245 nvmf_get_reservation_notification_log_page(ctrlr, req->iov, req->iovcnt, offset, len, rae); 2246 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2247 default: 2248 goto invalid_log_page; 2249 } 2250 } 2251 2252 invalid_log_page: 2253 SPDK_INFOLOG(nvmf, "Unsupported Get Log Page 0x%02X\n", lid); 2254 response->status.sct = SPDK_NVME_SCT_GENERIC; 2255 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2256 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2257 } 2258 2259 int 2260 spdk_nvmf_ctrlr_identify_ns(struct spdk_nvmf_ctrlr *ctrlr, 2261 struct spdk_nvme_cmd *cmd, 2262 struct spdk_nvme_cpl *rsp, 2263 struct spdk_nvme_ns_data *nsdata) 2264 { 2265 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 2266 struct spdk_nvmf_ns *ns; 2267 uint32_t max_num_blocks; 2268 2269 if (cmd->nsid == 0 || cmd->nsid > subsystem->max_nsid) { 2270 SPDK_ERRLOG("Identify Namespace for invalid NSID %u\n", cmd->nsid); 2271 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2272 rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 2273 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2274 } 2275 2276 ns = _nvmf_subsystem_get_ns(subsystem, cmd->nsid); 2277 if (ns == NULL || ns->bdev == NULL) { 2278 /* 2279 * Inactive namespaces should return a zero filled data structure. 2280 * The data buffer is already zeroed by nvmf_ctrlr_process_admin_cmd(), 2281 * so we can just return early here. 2282 */ 2283 SPDK_DEBUGLOG(nvmf, "Identify Namespace for inactive NSID %u\n", cmd->nsid); 2284 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2285 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 2286 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2287 } 2288 2289 nvmf_bdev_ctrlr_identify_ns(ns, nsdata, ctrlr->dif_insert_or_strip); 2290 2291 /* Due to bug in the Linux kernel NVMe driver we have to set noiob no larger than mdts */ 2292 max_num_blocks = ctrlr->admin_qpair->transport->opts.max_io_size / 2293 (1U << nsdata->lbaf[nsdata->flbas.format].lbads); 2294 if (nsdata->noiob > max_num_blocks) { 2295 nsdata->noiob = max_num_blocks; 2296 } 2297 2298 if (subsystem->flags.ana_reporting) { 2299 /* ANA group ID matches NSID. */ 2300 nsdata->anagrpid = ns->nsid; 2301 2302 if (ctrlr->listener->ana_state == SPDK_NVME_ANA_INACCESSIBLE_STATE || 2303 ctrlr->listener->ana_state == SPDK_NVME_ANA_PERSISTENT_LOSS_STATE) { 2304 nsdata->nuse = 0; 2305 } 2306 } 2307 2308 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2309 } 2310 2311 static void 2312 nvmf_ctrlr_populate_oacs(struct spdk_nvmf_ctrlr *ctrlr, 2313 struct spdk_nvme_ctrlr_data *cdata) 2314 { 2315 cdata->oacs.virtualization_management = 2316 g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_VIRTUALIZATION_MANAGEMENT].hdlr != NULL; 2317 cdata->oacs.nvme_mi = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NVME_MI_SEND].hdlr != NULL 2318 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NVME_MI_RECEIVE].hdlr != NULL; 2319 cdata->oacs.directives = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DIRECTIVE_SEND].hdlr != NULL 2320 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DIRECTIVE_RECEIVE].hdlr != NULL; 2321 cdata->oacs.device_self_test = 2322 g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DEVICE_SELF_TEST].hdlr != NULL; 2323 cdata->oacs.ns_manage = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NS_MANAGEMENT].hdlr != NULL 2324 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NS_ATTACHMENT].hdlr != NULL; 2325 cdata->oacs.firmware = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD].hdlr != 2326 NULL 2327 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FIRMWARE_COMMIT].hdlr != NULL; 2328 cdata->oacs.format = 2329 g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FORMAT_NVM].hdlr != NULL; 2330 cdata->oacs.security = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_SECURITY_SEND].hdlr != NULL 2331 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_SECURITY_RECEIVE].hdlr != NULL; 2332 cdata->oacs.get_lba_status = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_GET_LBA_STATUS].hdlr != 2333 NULL; 2334 } 2335 2336 int 2337 spdk_nvmf_ctrlr_identify_ctrlr(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_ctrlr_data *cdata) 2338 { 2339 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 2340 struct spdk_nvmf_transport *transport = ctrlr->admin_qpair->transport; 2341 2342 /* 2343 * Common fields for discovery and NVM subsystems 2344 */ 2345 spdk_strcpy_pad(cdata->fr, FW_VERSION, sizeof(cdata->fr), ' '); 2346 assert((transport->opts.max_io_size % 4096) == 0); 2347 cdata->mdts = spdk_u32log2(transport->opts.max_io_size / 4096); 2348 cdata->cntlid = ctrlr->cntlid; 2349 cdata->ver = ctrlr->vcprop.vs; 2350 cdata->aerl = NVMF_MAX_ASYNC_EVENTS - 1; 2351 cdata->lpa.edlp = 1; 2352 cdata->elpe = 127; 2353 cdata->maxcmd = transport->opts.max_queue_depth; 2354 cdata->sgls = ctrlr->cdata.sgls; 2355 cdata->fuses.compare_and_write = 1; 2356 cdata->acwu = 1; 2357 if (subsystem->flags.ana_reporting) { 2358 cdata->mnan = subsystem->max_nsid; 2359 } 2360 spdk_strcpy_pad(cdata->subnqn, subsystem->subnqn, sizeof(cdata->subnqn), '\0'); 2361 2362 SPDK_DEBUGLOG(nvmf, "ctrlr data: maxcmd 0x%x\n", cdata->maxcmd); 2363 SPDK_DEBUGLOG(nvmf, "sgls data: 0x%x\n", from_le32(&cdata->sgls)); 2364 2365 2366 if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 2367 /* 2368 * NVM Discovery subsystem fields 2369 */ 2370 cdata->oaes.discovery_log_change_notices = 1; 2371 } else { 2372 /* 2373 * NVM subsystem fields (reserved for discovery subsystems) 2374 */ 2375 spdk_strcpy_pad(cdata->mn, spdk_nvmf_subsystem_get_mn(subsystem), sizeof(cdata->mn), ' '); 2376 spdk_strcpy_pad(cdata->sn, spdk_nvmf_subsystem_get_sn(subsystem), sizeof(cdata->sn), ' '); 2377 cdata->kas = ctrlr->cdata.kas; 2378 2379 cdata->rab = 6; 2380 cdata->cmic.multi_port = 1; 2381 cdata->cmic.multi_ctrlr = 1; 2382 if (subsystem->flags.ana_reporting) { 2383 /* Asymmetric Namespace Access Reporting is supported. */ 2384 cdata->cmic.ana_reporting = 1; 2385 } 2386 cdata->oaes.ns_attribute_notices = 1; 2387 if (subsystem->flags.ana_reporting) { 2388 cdata->oaes.ana_change_notices = 1; 2389 } 2390 cdata->ctratt.host_id_exhid_supported = 1; 2391 /* TODO: Concurrent execution of multiple abort commands. */ 2392 cdata->acl = 0; 2393 cdata->frmw.slot1_ro = 1; 2394 cdata->frmw.num_slots = 1; 2395 2396 cdata->lpa.celp = 1; /* Command Effects log page supported */ 2397 2398 cdata->sqes.min = 6; 2399 cdata->sqes.max = 6; 2400 cdata->cqes.min = 4; 2401 cdata->cqes.max = 4; 2402 cdata->nn = subsystem->max_nsid; 2403 cdata->vwc.present = 1; 2404 cdata->vwc.flush_broadcast = SPDK_NVME_FLUSH_BROADCAST_NOT_SUPPORTED; 2405 2406 cdata->nvmf_specific = ctrlr->cdata.nvmf_specific; 2407 2408 cdata->oncs.dsm = nvmf_ctrlr_dsm_supported(ctrlr); 2409 cdata->oncs.write_zeroes = nvmf_ctrlr_write_zeroes_supported(ctrlr); 2410 cdata->oncs.reservations = ctrlr->cdata.oncs.reservations; 2411 if (subsystem->flags.ana_reporting) { 2412 cdata->anatt = ANA_TRANSITION_TIME_IN_SEC; 2413 /* ANA Change state is not used, and ANA Persistent Loss state 2414 * is not supported for now. 2415 */ 2416 cdata->anacap.ana_optimized_state = 1; 2417 cdata->anacap.ana_non_optimized_state = 1; 2418 cdata->anacap.ana_inaccessible_state = 1; 2419 /* ANAGRPID does not change while namespace is attached to controller */ 2420 cdata->anacap.no_change_anagrpid = 1; 2421 cdata->anagrpmax = subsystem->max_nsid; 2422 cdata->nanagrpid = subsystem->max_nsid; 2423 } 2424 2425 nvmf_ctrlr_populate_oacs(ctrlr, cdata); 2426 2427 assert(subsystem->tgt != NULL); 2428 cdata->crdt[0] = subsystem->tgt->crdt[0]; 2429 cdata->crdt[1] = subsystem->tgt->crdt[1]; 2430 cdata->crdt[2] = subsystem->tgt->crdt[2]; 2431 2432 SPDK_DEBUGLOG(nvmf, "ext ctrlr data: ioccsz 0x%x\n", 2433 cdata->nvmf_specific.ioccsz); 2434 SPDK_DEBUGLOG(nvmf, "ext ctrlr data: iorcsz 0x%x\n", 2435 cdata->nvmf_specific.iorcsz); 2436 SPDK_DEBUGLOG(nvmf, "ext ctrlr data: icdoff 0x%x\n", 2437 cdata->nvmf_specific.icdoff); 2438 SPDK_DEBUGLOG(nvmf, "ext ctrlr data: ctrattr 0x%x\n", 2439 *(uint8_t *)&cdata->nvmf_specific.ctrattr); 2440 SPDK_DEBUGLOG(nvmf, "ext ctrlr data: msdbd 0x%x\n", 2441 cdata->nvmf_specific.msdbd); 2442 } 2443 2444 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2445 } 2446 2447 static int 2448 nvmf_ctrlr_identify_active_ns_list(struct spdk_nvmf_subsystem *subsystem, 2449 struct spdk_nvme_cmd *cmd, 2450 struct spdk_nvme_cpl *rsp, 2451 struct spdk_nvme_ns_list *ns_list) 2452 { 2453 struct spdk_nvmf_ns *ns; 2454 uint32_t count = 0; 2455 2456 if (cmd->nsid >= 0xfffffffeUL) { 2457 SPDK_ERRLOG("Identify Active Namespace List with invalid NSID %u\n", cmd->nsid); 2458 rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 2459 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2460 } 2461 2462 memset(ns_list, 0, sizeof(*ns_list)); 2463 2464 for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL; 2465 ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) { 2466 if (ns->opts.nsid <= cmd->nsid) { 2467 continue; 2468 } 2469 2470 ns_list->ns_list[count++] = ns->opts.nsid; 2471 if (count == SPDK_COUNTOF(ns_list->ns_list)) { 2472 break; 2473 } 2474 } 2475 2476 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2477 } 2478 2479 static void 2480 _add_ns_id_desc(void **buf_ptr, size_t *buf_remain, 2481 enum spdk_nvme_nidt type, 2482 const void *data, size_t data_size) 2483 { 2484 struct spdk_nvme_ns_id_desc *desc; 2485 size_t desc_size = sizeof(*desc) + data_size; 2486 2487 /* 2488 * These should never fail in practice, since all valid NS ID descriptors 2489 * should be defined so that they fit in the available 4096-byte buffer. 2490 */ 2491 assert(data_size > 0); 2492 assert(data_size <= UINT8_MAX); 2493 assert(desc_size < *buf_remain); 2494 if (data_size == 0 || data_size > UINT8_MAX || desc_size > *buf_remain) { 2495 return; 2496 } 2497 2498 desc = *buf_ptr; 2499 desc->nidt = type; 2500 desc->nidl = data_size; 2501 memcpy(desc->nid, data, data_size); 2502 2503 *buf_ptr += desc_size; 2504 *buf_remain -= desc_size; 2505 } 2506 2507 static int 2508 nvmf_ctrlr_identify_ns_id_descriptor_list( 2509 struct spdk_nvmf_subsystem *subsystem, 2510 struct spdk_nvme_cmd *cmd, 2511 struct spdk_nvme_cpl *rsp, 2512 void *id_desc_list, size_t id_desc_list_size) 2513 { 2514 struct spdk_nvmf_ns *ns; 2515 size_t buf_remain = id_desc_list_size; 2516 void *buf_ptr = id_desc_list; 2517 2518 ns = _nvmf_subsystem_get_ns(subsystem, cmd->nsid); 2519 if (ns == NULL || ns->bdev == NULL) { 2520 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2521 rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 2522 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2523 } 2524 2525 #define ADD_ID_DESC(type, data, size) \ 2526 do { \ 2527 if (!spdk_mem_all_zero(data, size)) { \ 2528 _add_ns_id_desc(&buf_ptr, &buf_remain, type, data, size); \ 2529 } \ 2530 } while (0) 2531 2532 ADD_ID_DESC(SPDK_NVME_NIDT_EUI64, ns->opts.eui64, sizeof(ns->opts.eui64)); 2533 ADD_ID_DESC(SPDK_NVME_NIDT_NGUID, ns->opts.nguid, sizeof(ns->opts.nguid)); 2534 ADD_ID_DESC(SPDK_NVME_NIDT_UUID, &ns->opts.uuid, sizeof(ns->opts.uuid)); 2535 2536 /* 2537 * The list is automatically 0-terminated because controller to host buffers in 2538 * admin commands always get zeroed in nvmf_ctrlr_process_admin_cmd(). 2539 */ 2540 2541 #undef ADD_ID_DESC 2542 2543 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2544 } 2545 2546 static int 2547 nvmf_ctrlr_identify(struct spdk_nvmf_request *req) 2548 { 2549 uint8_t cns; 2550 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2551 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2552 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 2553 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 2554 2555 if (req->data == NULL || req->length < 4096) { 2556 SPDK_ERRLOG("identify command with invalid buffer\n"); 2557 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2558 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2559 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2560 } 2561 2562 cns = cmd->cdw10_bits.identify.cns; 2563 2564 if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY && 2565 cns != SPDK_NVME_IDENTIFY_CTRLR) { 2566 /* Discovery controllers only support Identify Controller */ 2567 goto invalid_cns; 2568 } 2569 2570 switch (cns) { 2571 case SPDK_NVME_IDENTIFY_NS: 2572 return spdk_nvmf_ctrlr_identify_ns(ctrlr, cmd, rsp, req->data); 2573 case SPDK_NVME_IDENTIFY_CTRLR: 2574 return spdk_nvmf_ctrlr_identify_ctrlr(ctrlr, req->data); 2575 case SPDK_NVME_IDENTIFY_ACTIVE_NS_LIST: 2576 return nvmf_ctrlr_identify_active_ns_list(subsystem, cmd, rsp, req->data); 2577 case SPDK_NVME_IDENTIFY_NS_ID_DESCRIPTOR_LIST: 2578 return nvmf_ctrlr_identify_ns_id_descriptor_list(subsystem, cmd, rsp, req->data, req->length); 2579 default: 2580 goto invalid_cns; 2581 } 2582 2583 invalid_cns: 2584 SPDK_INFOLOG(nvmf, "Identify command with unsupported CNS 0x%02x\n", cns); 2585 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2586 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2587 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2588 } 2589 2590 static bool 2591 nvmf_qpair_abort_aer(struct spdk_nvmf_qpair *qpair, uint16_t cid) 2592 { 2593 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 2594 struct spdk_nvmf_request *req; 2595 int i; 2596 2597 if (!nvmf_qpair_is_admin_queue(qpair)) { 2598 return false; 2599 } 2600 2601 for (i = 0; i < ctrlr->nr_aer_reqs; i++) { 2602 if (ctrlr->aer_req[i]->cmd->nvme_cmd.cid == cid) { 2603 SPDK_DEBUGLOG(nvmf, "Aborting AER request\n"); 2604 req = ctrlr->aer_req[i]; 2605 ctrlr->aer_req[i] = NULL; 2606 ctrlr->nr_aer_reqs--; 2607 2608 /* Move the last req to the aborting position for making aer_reqs 2609 * in continuous 2610 */ 2611 if (i < ctrlr->nr_aer_reqs) { 2612 ctrlr->aer_req[i] = ctrlr->aer_req[ctrlr->nr_aer_reqs]; 2613 ctrlr->aer_req[ctrlr->nr_aer_reqs] = NULL; 2614 } 2615 2616 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2617 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_ABORTED_BY_REQUEST; 2618 _nvmf_request_complete(req); 2619 return true; 2620 } 2621 } 2622 2623 return false; 2624 } 2625 2626 static void 2627 nvmf_qpair_abort_request(struct spdk_nvmf_qpair *qpair, struct spdk_nvmf_request *req) 2628 { 2629 uint16_t cid = req->cmd->nvme_cmd.cdw10_bits.abort.cid; 2630 2631 if (nvmf_qpair_abort_aer(qpair, cid)) { 2632 SPDK_DEBUGLOG(nvmf, "abort ctrlr=%p sqid=%u cid=%u successful\n", 2633 qpair->ctrlr, qpair->qid, cid); 2634 req->rsp->nvme_cpl.cdw0 &= ~1U; /* Command successfully aborted */ 2635 2636 spdk_nvmf_request_complete(req); 2637 return; 2638 } 2639 2640 nvmf_transport_qpair_abort_request(qpair, req); 2641 } 2642 2643 static void 2644 nvmf_ctrlr_abort_done(struct spdk_io_channel_iter *i, int status) 2645 { 2646 struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i); 2647 2648 if (status == 0) { 2649 /* There was no qpair whose ID matches SQID of the abort command. 2650 * Hence call _nvmf_request_complete() here. 2651 */ 2652 _nvmf_request_complete(req); 2653 } 2654 } 2655 2656 static void 2657 nvmf_ctrlr_abort_on_pg(struct spdk_io_channel_iter *i) 2658 { 2659 struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i); 2660 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 2661 struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch); 2662 uint16_t sqid = req->cmd->nvme_cmd.cdw10_bits.abort.sqid; 2663 struct spdk_nvmf_qpair *qpair; 2664 2665 TAILQ_FOREACH(qpair, &group->qpairs, link) { 2666 if (qpair->ctrlr == req->qpair->ctrlr && qpair->qid == sqid) { 2667 /* Found the qpair */ 2668 2669 nvmf_qpair_abort_request(qpair, req); 2670 2671 /* Return -1 for the status so the iteration across threads stops. */ 2672 spdk_for_each_channel_continue(i, -1); 2673 return; 2674 } 2675 } 2676 2677 spdk_for_each_channel_continue(i, 0); 2678 } 2679 2680 static int 2681 nvmf_ctrlr_abort(struct spdk_nvmf_request *req) 2682 { 2683 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 2684 2685 rsp->cdw0 = 1U; /* Command not aborted */ 2686 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2687 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 2688 2689 /* Send a message to each poll group, searching for this ctrlr, sqid, and command. */ 2690 spdk_for_each_channel(req->qpair->ctrlr->subsys->tgt, 2691 nvmf_ctrlr_abort_on_pg, 2692 req, 2693 nvmf_ctrlr_abort_done 2694 ); 2695 2696 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 2697 } 2698 2699 int 2700 nvmf_ctrlr_abort_request(struct spdk_nvmf_request *req) 2701 { 2702 struct spdk_nvmf_request *req_to_abort = req->req_to_abort; 2703 struct spdk_bdev *bdev; 2704 struct spdk_bdev_desc *desc; 2705 struct spdk_io_channel *ch; 2706 int rc; 2707 2708 assert(req_to_abort != NULL); 2709 2710 if (g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_ABORT].hdlr && 2711 nvmf_qpair_is_admin_queue(req_to_abort->qpair)) { 2712 return g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_ABORT].hdlr(req); 2713 } 2714 2715 rc = spdk_nvmf_request_get_bdev(req_to_abort->cmd->nvme_cmd.nsid, req_to_abort, 2716 &bdev, &desc, &ch); 2717 if (rc != 0) { 2718 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2719 } 2720 2721 return spdk_nvmf_bdev_ctrlr_abort_cmd(bdev, desc, ch, req, req_to_abort); 2722 } 2723 2724 static int 2725 get_features_generic(struct spdk_nvmf_request *req, uint32_t cdw0) 2726 { 2727 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 2728 2729 rsp->cdw0 = cdw0; 2730 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2731 } 2732 2733 /* we have to use the typedef in the function declaration to appease astyle. */ 2734 typedef enum spdk_nvme_path_status_code spdk_nvme_path_status_code_t; 2735 2736 static spdk_nvme_path_status_code_t 2737 _nvme_ana_state_to_path_status(enum spdk_nvme_ana_state ana_state) 2738 { 2739 switch (ana_state) { 2740 case SPDK_NVME_ANA_INACCESSIBLE_STATE: 2741 return SPDK_NVME_SC_ASYMMETRIC_ACCESS_INACCESSIBLE; 2742 case SPDK_NVME_ANA_PERSISTENT_LOSS_STATE: 2743 return SPDK_NVME_SC_ASYMMETRIC_ACCESS_PERSISTENT_LOSS; 2744 case SPDK_NVME_ANA_CHANGE_STATE: 2745 return SPDK_NVME_SC_ASYMMETRIC_ACCESS_TRANSITION; 2746 default: 2747 return SPDK_NVME_SC_INTERNAL_PATH_ERROR; 2748 } 2749 } 2750 2751 static int 2752 nvmf_ctrlr_get_features(struct spdk_nvmf_request *req) 2753 { 2754 uint8_t feature; 2755 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2756 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2757 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 2758 enum spdk_nvme_ana_state ana_state; 2759 2760 feature = cmd->cdw10_bits.get_features.fid; 2761 2762 if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 2763 /* 2764 * Features supported by Discovery controller 2765 */ 2766 switch (feature) { 2767 case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER: 2768 return get_features_generic(req, ctrlr->feat.keep_alive_timer.raw); 2769 case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION: 2770 return get_features_generic(req, ctrlr->feat.async_event_configuration.raw); 2771 default: 2772 SPDK_INFOLOG(nvmf, "Get Features command with unsupported feature ID 0x%02x\n", feature); 2773 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2774 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2775 } 2776 } 2777 /* 2778 * Process Get Features command for non-discovery controller 2779 */ 2780 ana_state = ctrlr->listener->ana_state; 2781 switch (ana_state) { 2782 case SPDK_NVME_ANA_INACCESSIBLE_STATE: 2783 case SPDK_NVME_ANA_PERSISTENT_LOSS_STATE: 2784 case SPDK_NVME_ANA_CHANGE_STATE: 2785 switch (feature) { 2786 case SPDK_NVME_FEAT_ERROR_RECOVERY: 2787 case SPDK_NVME_FEAT_WRITE_ATOMICITY: 2788 case SPDK_NVME_FEAT_HOST_RESERVE_MASK: 2789 case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST: 2790 response->status.sct = SPDK_NVME_SCT_PATH; 2791 response->status.sc = _nvme_ana_state_to_path_status(ana_state); 2792 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2793 default: 2794 break; 2795 } 2796 break; 2797 default: 2798 break; 2799 } 2800 2801 switch (feature) { 2802 case SPDK_NVME_FEAT_ARBITRATION: 2803 return get_features_generic(req, ctrlr->feat.arbitration.raw); 2804 case SPDK_NVME_FEAT_POWER_MANAGEMENT: 2805 return get_features_generic(req, ctrlr->feat.power_management.raw); 2806 case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD: 2807 return nvmf_ctrlr_get_features_temperature_threshold(req); 2808 case SPDK_NVME_FEAT_ERROR_RECOVERY: 2809 return get_features_generic(req, ctrlr->feat.error_recovery.raw); 2810 case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE: 2811 return get_features_generic(req, ctrlr->feat.volatile_write_cache.raw); 2812 case SPDK_NVME_FEAT_NUMBER_OF_QUEUES: 2813 return get_features_generic(req, ctrlr->feat.number_of_queues.raw); 2814 case SPDK_NVME_FEAT_INTERRUPT_COALESCING: 2815 return get_features_generic(req, ctrlr->feat.interrupt_coalescing.raw); 2816 case SPDK_NVME_FEAT_INTERRUPT_VECTOR_CONFIGURATION: 2817 return nvmf_ctrlr_get_features_interrupt_vector_configuration(req); 2818 case SPDK_NVME_FEAT_WRITE_ATOMICITY: 2819 return get_features_generic(req, ctrlr->feat.write_atomicity.raw); 2820 case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION: 2821 return get_features_generic(req, ctrlr->feat.async_event_configuration.raw); 2822 case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER: 2823 return get_features_generic(req, ctrlr->feat.keep_alive_timer.raw); 2824 case SPDK_NVME_FEAT_HOST_IDENTIFIER: 2825 return nvmf_ctrlr_get_features_host_identifier(req); 2826 case SPDK_NVME_FEAT_HOST_RESERVE_MASK: 2827 return nvmf_ctrlr_get_features_reservation_notification_mask(req); 2828 case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST: 2829 return nvmf_ctrlr_get_features_reservation_persistence(req); 2830 default: 2831 SPDK_INFOLOG(nvmf, "Get Features command with unsupported feature ID 0x%02x\n", feature); 2832 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2833 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2834 } 2835 } 2836 2837 static int 2838 nvmf_ctrlr_set_features(struct spdk_nvmf_request *req) 2839 { 2840 uint8_t feature, save; 2841 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2842 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2843 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 2844 enum spdk_nvme_ana_state ana_state; 2845 /* 2846 * Features are not saveable by the controller as indicated by 2847 * ONCS field of the Identify Controller data. 2848 * */ 2849 save = cmd->cdw10_bits.set_features.sv; 2850 if (save) { 2851 response->status.sc = SPDK_NVME_SC_FEATURE_ID_NOT_SAVEABLE; 2852 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 2853 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2854 } 2855 2856 feature = cmd->cdw10_bits.set_features.fid; 2857 2858 if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 2859 /* 2860 * Features supported by Discovery controller 2861 */ 2862 switch (feature) { 2863 case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER: 2864 return nvmf_ctrlr_set_features_keep_alive_timer(req); 2865 case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION: 2866 return nvmf_ctrlr_set_features_async_event_configuration(req); 2867 default: 2868 SPDK_INFOLOG(nvmf, "Set Features command with unsupported feature ID 0x%02x\n", feature); 2869 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2870 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2871 } 2872 } 2873 /* 2874 * Process Set Features command for non-discovery controller 2875 */ 2876 ana_state = ctrlr->listener->ana_state; 2877 switch (ana_state) { 2878 case SPDK_NVME_ANA_INACCESSIBLE_STATE: 2879 case SPDK_NVME_ANA_CHANGE_STATE: 2880 if (cmd->nsid == SPDK_NVME_GLOBAL_NS_TAG) { 2881 response->status.sct = SPDK_NVME_SCT_PATH; 2882 response->status.sc = _nvme_ana_state_to_path_status(ana_state); 2883 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2884 } else { 2885 switch (feature) { 2886 case SPDK_NVME_FEAT_ERROR_RECOVERY: 2887 case SPDK_NVME_FEAT_WRITE_ATOMICITY: 2888 case SPDK_NVME_FEAT_HOST_RESERVE_MASK: 2889 case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST: 2890 response->status.sct = SPDK_NVME_SCT_PATH; 2891 response->status.sc = _nvme_ana_state_to_path_status(ana_state); 2892 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2893 default: 2894 break; 2895 } 2896 } 2897 break; 2898 case SPDK_NVME_ANA_PERSISTENT_LOSS_STATE: 2899 response->status.sct = SPDK_NVME_SCT_PATH; 2900 response->status.sc = SPDK_NVME_SC_ASYMMETRIC_ACCESS_PERSISTENT_LOSS; 2901 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2902 default: 2903 break; 2904 } 2905 2906 switch (feature) { 2907 case SPDK_NVME_FEAT_ARBITRATION: 2908 return nvmf_ctrlr_set_features_arbitration(req); 2909 case SPDK_NVME_FEAT_POWER_MANAGEMENT: 2910 return nvmf_ctrlr_set_features_power_management(req); 2911 case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD: 2912 return nvmf_ctrlr_set_features_temperature_threshold(req); 2913 case SPDK_NVME_FEAT_ERROR_RECOVERY: 2914 return nvmf_ctrlr_set_features_error_recovery(req); 2915 case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE: 2916 return nvmf_ctrlr_set_features_volatile_write_cache(req); 2917 case SPDK_NVME_FEAT_NUMBER_OF_QUEUES: 2918 return nvmf_ctrlr_set_features_number_of_queues(req); 2919 case SPDK_NVME_FEAT_INTERRUPT_COALESCING: 2920 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 2921 response->status.sc = SPDK_NVME_SC_FEATURE_NOT_CHANGEABLE; 2922 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2923 case SPDK_NVME_FEAT_WRITE_ATOMICITY: 2924 return nvmf_ctrlr_set_features_write_atomicity(req); 2925 case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION: 2926 return nvmf_ctrlr_set_features_async_event_configuration(req); 2927 case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER: 2928 return nvmf_ctrlr_set_features_keep_alive_timer(req); 2929 case SPDK_NVME_FEAT_HOST_IDENTIFIER: 2930 return nvmf_ctrlr_set_features_host_identifier(req); 2931 case SPDK_NVME_FEAT_HOST_RESERVE_MASK: 2932 return nvmf_ctrlr_set_features_reservation_notification_mask(req); 2933 case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST: 2934 return nvmf_ctrlr_set_features_reservation_persistence(req); 2935 case SPDK_NVME_FEAT_HOST_BEHAVIOR_SUPPORT: 2936 return nvmf_ctrlr_set_features_host_behavior_support(req); 2937 default: 2938 SPDK_INFOLOG(nvmf, "Set Features command with unsupported feature ID 0x%02x\n", feature); 2939 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2940 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2941 } 2942 } 2943 2944 static int 2945 nvmf_ctrlr_keep_alive(struct spdk_nvmf_request *req) 2946 { 2947 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2948 2949 SPDK_DEBUGLOG(nvmf, "Keep Alive\n"); 2950 /* 2951 * To handle keep alive just clear or reset the 2952 * ctrlr based keep alive duration counter. 2953 * When added, a separate timer based process 2954 * will monitor if the time since last recorded 2955 * keep alive has exceeded the max duration and 2956 * take appropriate action. 2957 */ 2958 ctrlr->last_keep_alive_tick = spdk_get_ticks(); 2959 2960 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2961 } 2962 2963 int 2964 nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req) 2965 { 2966 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2967 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2968 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 2969 int rc; 2970 2971 if (ctrlr == NULL) { 2972 SPDK_ERRLOG("Admin command sent before CONNECT\n"); 2973 response->status.sct = SPDK_NVME_SCT_GENERIC; 2974 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 2975 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2976 } 2977 2978 if (ctrlr->vcprop.cc.bits.en != 1) { 2979 SPDK_ERRLOG("Admin command sent to disabled controller\n"); 2980 response->status.sct = SPDK_NVME_SCT_GENERIC; 2981 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 2982 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2983 } 2984 2985 if (req->data && spdk_nvme_opc_get_data_transfer(cmd->opc) == SPDK_NVME_DATA_CONTROLLER_TO_HOST) { 2986 memset(req->data, 0, req->length); 2987 } 2988 2989 if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 2990 /* Discovery controllers only support these admin OPS. */ 2991 switch (cmd->opc) { 2992 case SPDK_NVME_OPC_IDENTIFY: 2993 case SPDK_NVME_OPC_GET_LOG_PAGE: 2994 case SPDK_NVME_OPC_KEEP_ALIVE: 2995 case SPDK_NVME_OPC_SET_FEATURES: 2996 case SPDK_NVME_OPC_GET_FEATURES: 2997 case SPDK_NVME_OPC_ASYNC_EVENT_REQUEST: 2998 break; 2999 default: 3000 goto invalid_opcode; 3001 } 3002 } 3003 3004 /* Call a custom adm cmd handler if set. Aborts are handled in a different path (see nvmf_passthru_admin_cmd) */ 3005 if (g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].hdlr && cmd->opc != SPDK_NVME_OPC_ABORT) { 3006 rc = g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].hdlr(req); 3007 if (rc >= SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 3008 /* The handler took care of this commmand */ 3009 return rc; 3010 } 3011 } 3012 3013 switch (cmd->opc) { 3014 case SPDK_NVME_OPC_GET_LOG_PAGE: 3015 return nvmf_ctrlr_get_log_page(req); 3016 case SPDK_NVME_OPC_IDENTIFY: 3017 return nvmf_ctrlr_identify(req); 3018 case SPDK_NVME_OPC_ABORT: 3019 return nvmf_ctrlr_abort(req); 3020 case SPDK_NVME_OPC_GET_FEATURES: 3021 return nvmf_ctrlr_get_features(req); 3022 case SPDK_NVME_OPC_SET_FEATURES: 3023 return nvmf_ctrlr_set_features(req); 3024 case SPDK_NVME_OPC_ASYNC_EVENT_REQUEST: 3025 return nvmf_ctrlr_async_event_request(req); 3026 case SPDK_NVME_OPC_KEEP_ALIVE: 3027 return nvmf_ctrlr_keep_alive(req); 3028 3029 case SPDK_NVME_OPC_CREATE_IO_SQ: 3030 case SPDK_NVME_OPC_CREATE_IO_CQ: 3031 case SPDK_NVME_OPC_DELETE_IO_SQ: 3032 case SPDK_NVME_OPC_DELETE_IO_CQ: 3033 /* Create and Delete I/O CQ/SQ not allowed in NVMe-oF */ 3034 goto invalid_opcode; 3035 3036 default: 3037 goto invalid_opcode; 3038 } 3039 3040 invalid_opcode: 3041 SPDK_INFOLOG(nvmf, "Unsupported admin opcode 0x%x\n", cmd->opc); 3042 response->status.sct = SPDK_NVME_SCT_GENERIC; 3043 response->status.sc = SPDK_NVME_SC_INVALID_OPCODE; 3044 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3045 } 3046 3047 static int 3048 nvmf_ctrlr_process_fabrics_cmd(struct spdk_nvmf_request *req) 3049 { 3050 struct spdk_nvmf_qpair *qpair = req->qpair; 3051 struct spdk_nvmf_capsule_cmd *cap_hdr; 3052 3053 cap_hdr = &req->cmd->nvmf_cmd; 3054 3055 if (qpair->ctrlr == NULL) { 3056 /* No ctrlr established yet; the only valid command is Connect */ 3057 if (cap_hdr->fctype == SPDK_NVMF_FABRIC_COMMAND_CONNECT) { 3058 return nvmf_ctrlr_cmd_connect(req); 3059 } else { 3060 SPDK_DEBUGLOG(nvmf, "Got fctype 0x%x, expected Connect\n", 3061 cap_hdr->fctype); 3062 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3063 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 3064 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3065 } 3066 } else if (nvmf_qpair_is_admin_queue(qpair)) { 3067 /* 3068 * Controller session is established, and this is an admin queue. 3069 * Disallow Connect and allow other fabrics commands. 3070 */ 3071 switch (cap_hdr->fctype) { 3072 case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_SET: 3073 return nvmf_property_set(req); 3074 case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_GET: 3075 return nvmf_property_get(req); 3076 default: 3077 SPDK_DEBUGLOG(nvmf, "unknown fctype 0x%02x\n", 3078 cap_hdr->fctype); 3079 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3080 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE; 3081 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3082 } 3083 } else { 3084 /* Controller session is established, and this is an I/O queue */ 3085 /* For now, no I/O-specific Fabrics commands are implemented (other than Connect) */ 3086 SPDK_DEBUGLOG(nvmf, "Unexpected I/O fctype 0x%x\n", cap_hdr->fctype); 3087 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3088 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE; 3089 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3090 } 3091 } 3092 3093 static inline int 3094 nvmf_ctrlr_async_event_notification(struct spdk_nvmf_ctrlr *ctrlr, 3095 union spdk_nvme_async_event_completion *event) 3096 { 3097 struct spdk_nvmf_request *req; 3098 struct spdk_nvme_cpl *rsp; 3099 3100 assert(ctrlr->nr_aer_reqs > 0); 3101 3102 req = ctrlr->aer_req[--ctrlr->nr_aer_reqs]; 3103 rsp = &req->rsp->nvme_cpl; 3104 3105 rsp->cdw0 = event->raw; 3106 3107 _nvmf_request_complete(req); 3108 ctrlr->aer_req[ctrlr->nr_aer_reqs] = NULL; 3109 3110 return 0; 3111 } 3112 3113 static inline void 3114 nvmf_ctrlr_queue_pending_async_event(struct spdk_nvmf_ctrlr *ctrlr, 3115 union spdk_nvme_async_event_completion *event) 3116 { 3117 struct spdk_nvmf_async_event_completion *nvmf_event; 3118 3119 nvmf_event = calloc(1, sizeof(*nvmf_event)); 3120 if (!nvmf_event) { 3121 SPDK_ERRLOG("Alloc nvmf event failed, ignore the event\n"); 3122 return; 3123 } 3124 nvmf_event->event.raw = event->raw; 3125 STAILQ_INSERT_TAIL(&ctrlr->async_events, nvmf_event, link); 3126 } 3127 3128 int 3129 nvmf_ctrlr_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr) 3130 { 3131 union spdk_nvme_async_event_completion event = {0}; 3132 3133 /* Users may disable the event notification */ 3134 if (!ctrlr->feat.async_event_configuration.bits.ns_attr_notice) { 3135 return 0; 3136 } 3137 3138 if (!nvmf_ctrlr_mask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGE_MASK_BIT)) { 3139 return 0; 3140 } 3141 3142 event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE; 3143 event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGED; 3144 event.bits.log_page_identifier = SPDK_NVME_LOG_CHANGED_NS_LIST; 3145 3146 /* If there is no outstanding AER request, queue the event. Then 3147 * if an AER is later submitted, this event can be sent as a 3148 * response. 3149 */ 3150 if (ctrlr->nr_aer_reqs == 0) { 3151 nvmf_ctrlr_queue_pending_async_event(ctrlr, &event); 3152 return 0; 3153 } 3154 3155 return nvmf_ctrlr_async_event_notification(ctrlr, &event); 3156 } 3157 3158 int 3159 nvmf_ctrlr_async_event_ana_change_notice(struct spdk_nvmf_ctrlr *ctrlr) 3160 { 3161 union spdk_nvme_async_event_completion event = {0}; 3162 3163 /* Users may disable the event notification */ 3164 if (!ctrlr->feat.async_event_configuration.bits.ana_change_notice) { 3165 return 0; 3166 } 3167 3168 if (!nvmf_ctrlr_mask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_ANA_CHANGE_MASK_BIT)) { 3169 return 0; 3170 } 3171 3172 event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE; 3173 event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_ANA_CHANGE; 3174 event.bits.log_page_identifier = SPDK_NVME_LOG_ASYMMETRIC_NAMESPACE_ACCESS; 3175 3176 /* If there is no outstanding AER request, queue the event. Then 3177 * if an AER is later submitted, this event can be sent as a 3178 * response. 3179 */ 3180 if (ctrlr->nr_aer_reqs == 0) { 3181 nvmf_ctrlr_queue_pending_async_event(ctrlr, &event); 3182 return 0; 3183 } 3184 3185 return nvmf_ctrlr_async_event_notification(ctrlr, &event); 3186 } 3187 3188 void 3189 nvmf_ctrlr_async_event_reservation_notification(struct spdk_nvmf_ctrlr *ctrlr) 3190 { 3191 union spdk_nvme_async_event_completion event = {0}; 3192 3193 if (!ctrlr->num_avail_log_pages) { 3194 return; 3195 } 3196 3197 if (!nvmf_ctrlr_mask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_RESERVATION_LOG_AVAIL_MASK_BIT)) { 3198 return; 3199 } 3200 3201 event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_IO; 3202 event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_RESERVATION_LOG_AVAIL; 3203 event.bits.log_page_identifier = SPDK_NVME_LOG_RESERVATION_NOTIFICATION; 3204 3205 /* If there is no outstanding AER request, queue the event. Then 3206 * if an AER is later submitted, this event can be sent as a 3207 * response. 3208 */ 3209 if (ctrlr->nr_aer_reqs == 0) { 3210 nvmf_ctrlr_queue_pending_async_event(ctrlr, &event); 3211 return; 3212 } 3213 3214 nvmf_ctrlr_async_event_notification(ctrlr, &event); 3215 } 3216 3217 int 3218 nvmf_ctrlr_async_event_discovery_log_change_notice(struct spdk_nvmf_ctrlr *ctrlr) 3219 { 3220 union spdk_nvme_async_event_completion event = {0}; 3221 3222 /* Users may disable the event notification manually or 3223 * it may not be enabled due to keep alive timeout 3224 * not being set in connect command to discovery controller. 3225 */ 3226 if (!ctrlr->feat.async_event_configuration.bits.discovery_log_change_notice) { 3227 return 0; 3228 } 3229 3230 if (!nvmf_ctrlr_mask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_DISCOVERY_LOG_CHANGE_MASK_BIT)) { 3231 return 0; 3232 } 3233 3234 event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE; 3235 event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_DISCOVERY_LOG_CHANGE; 3236 event.bits.log_page_identifier = SPDK_NVME_LOG_DISCOVERY; 3237 3238 /* If there is no outstanding AER request, queue the event. Then 3239 * if an AER is later submitted, this event can be sent as a 3240 * response. 3241 */ 3242 if (ctrlr->nr_aer_reqs == 0) { 3243 nvmf_ctrlr_queue_pending_async_event(ctrlr, &event); 3244 return 0; 3245 } 3246 3247 return nvmf_ctrlr_async_event_notification(ctrlr, &event); 3248 } 3249 3250 void 3251 nvmf_qpair_free_aer(struct spdk_nvmf_qpair *qpair) 3252 { 3253 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 3254 int i; 3255 3256 if (!nvmf_qpair_is_admin_queue(qpair)) { 3257 return; 3258 } 3259 3260 for (i = 0; i < ctrlr->nr_aer_reqs; i++) { 3261 spdk_nvmf_request_free(ctrlr->aer_req[i]); 3262 ctrlr->aer_req[i] = NULL; 3263 } 3264 3265 ctrlr->nr_aer_reqs = 0; 3266 } 3267 3268 void 3269 nvmf_ctrlr_abort_aer(struct spdk_nvmf_ctrlr *ctrlr) 3270 { 3271 struct spdk_nvmf_request *req; 3272 int i; 3273 3274 if (!ctrlr->nr_aer_reqs) { 3275 return; 3276 } 3277 3278 for (i = 0; i < ctrlr->nr_aer_reqs; i++) { 3279 req = ctrlr->aer_req[i]; 3280 3281 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3282 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_ABORTED_BY_REQUEST; 3283 _nvmf_request_complete(req); 3284 3285 ctrlr->aer_req[i] = NULL; 3286 } 3287 3288 ctrlr->nr_aer_reqs = 0; 3289 } 3290 3291 static void 3292 _nvmf_ctrlr_add_reservation_log(void *ctx) 3293 { 3294 struct spdk_nvmf_reservation_log *log = (struct spdk_nvmf_reservation_log *)ctx; 3295 struct spdk_nvmf_ctrlr *ctrlr = log->ctrlr; 3296 3297 ctrlr->log_page_count++; 3298 3299 /* Maximum number of queued log pages is 255 */ 3300 if (ctrlr->num_avail_log_pages == 0xff) { 3301 struct spdk_nvmf_reservation_log *entry; 3302 entry = TAILQ_LAST(&ctrlr->log_head, log_page_head); 3303 entry->log.log_page_count = ctrlr->log_page_count; 3304 free(log); 3305 return; 3306 } 3307 3308 log->log.log_page_count = ctrlr->log_page_count; 3309 log->log.num_avail_log_pages = ctrlr->num_avail_log_pages++; 3310 TAILQ_INSERT_TAIL(&ctrlr->log_head, log, link); 3311 3312 nvmf_ctrlr_async_event_reservation_notification(ctrlr); 3313 } 3314 3315 void 3316 nvmf_ctrlr_reservation_notice_log(struct spdk_nvmf_ctrlr *ctrlr, 3317 struct spdk_nvmf_ns *ns, 3318 enum spdk_nvme_reservation_notification_log_page_type type) 3319 { 3320 struct spdk_nvmf_reservation_log *log; 3321 3322 switch (type) { 3323 case SPDK_NVME_RESERVATION_LOG_PAGE_EMPTY: 3324 return; 3325 case SPDK_NVME_REGISTRATION_PREEMPTED: 3326 if (ns->mask & SPDK_NVME_REGISTRATION_PREEMPTED_MASK) { 3327 return; 3328 } 3329 break; 3330 case SPDK_NVME_RESERVATION_RELEASED: 3331 if (ns->mask & SPDK_NVME_RESERVATION_RELEASED_MASK) { 3332 return; 3333 } 3334 break; 3335 case SPDK_NVME_RESERVATION_PREEMPTED: 3336 if (ns->mask & SPDK_NVME_RESERVATION_PREEMPTED_MASK) { 3337 return; 3338 } 3339 break; 3340 default: 3341 return; 3342 } 3343 3344 log = calloc(1, sizeof(*log)); 3345 if (!log) { 3346 SPDK_ERRLOG("Alloc log page failed, ignore the log\n"); 3347 return; 3348 } 3349 log->ctrlr = ctrlr; 3350 log->log.type = type; 3351 log->log.nsid = ns->nsid; 3352 3353 spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_add_reservation_log, log); 3354 } 3355 3356 /* Check from subsystem poll group's namespace information data structure */ 3357 static bool 3358 nvmf_ns_info_ctrlr_is_registrant(struct spdk_nvmf_subsystem_pg_ns_info *ns_info, 3359 struct spdk_nvmf_ctrlr *ctrlr) 3360 { 3361 uint32_t i; 3362 3363 for (i = 0; i < SPDK_NVMF_MAX_NUM_REGISTRANTS; i++) { 3364 if (!spdk_uuid_compare(&ns_info->reg_hostid[i], &ctrlr->hostid)) { 3365 return true; 3366 } 3367 } 3368 3369 return false; 3370 } 3371 3372 /* 3373 * Check the NVMe command is permitted or not for current controller(Host). 3374 */ 3375 static int 3376 nvmf_ns_reservation_request_check(struct spdk_nvmf_subsystem_pg_ns_info *ns_info, 3377 struct spdk_nvmf_ctrlr *ctrlr, 3378 struct spdk_nvmf_request *req) 3379 { 3380 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 3381 enum spdk_nvme_reservation_type rtype = ns_info->rtype; 3382 uint8_t status = SPDK_NVME_SC_SUCCESS; 3383 uint8_t racqa; 3384 bool is_registrant; 3385 3386 /* No valid reservation */ 3387 if (!rtype) { 3388 return 0; 3389 } 3390 3391 is_registrant = nvmf_ns_info_ctrlr_is_registrant(ns_info, ctrlr); 3392 /* All registrants type and current ctrlr is a valid registrant */ 3393 if ((rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS || 3394 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && is_registrant) { 3395 return 0; 3396 } else if (!spdk_uuid_compare(&ns_info->holder_id, &ctrlr->hostid)) { 3397 return 0; 3398 } 3399 3400 /* Non-holder for current controller */ 3401 switch (cmd->opc) { 3402 case SPDK_NVME_OPC_READ: 3403 case SPDK_NVME_OPC_COMPARE: 3404 if (rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) { 3405 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 3406 goto exit; 3407 } 3408 if ((rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY || 3409 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && !is_registrant) { 3410 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 3411 } 3412 break; 3413 case SPDK_NVME_OPC_FLUSH: 3414 case SPDK_NVME_OPC_WRITE: 3415 case SPDK_NVME_OPC_WRITE_UNCORRECTABLE: 3416 case SPDK_NVME_OPC_WRITE_ZEROES: 3417 case SPDK_NVME_OPC_DATASET_MANAGEMENT: 3418 if (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE || 3419 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) { 3420 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 3421 goto exit; 3422 } 3423 if (!is_registrant) { 3424 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 3425 } 3426 break; 3427 case SPDK_NVME_OPC_RESERVATION_ACQUIRE: 3428 racqa = cmd->cdw10_bits.resv_acquire.racqa; 3429 if (racqa == SPDK_NVME_RESERVE_ACQUIRE) { 3430 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 3431 goto exit; 3432 } 3433 if (!is_registrant) { 3434 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 3435 } 3436 break; 3437 case SPDK_NVME_OPC_RESERVATION_RELEASE: 3438 if (!is_registrant) { 3439 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 3440 } 3441 break; 3442 default: 3443 break; 3444 } 3445 3446 exit: 3447 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3448 req->rsp->nvme_cpl.status.sc = status; 3449 if (status == SPDK_NVME_SC_RESERVATION_CONFLICT) { 3450 return -EPERM; 3451 } 3452 3453 return 0; 3454 } 3455 3456 static int 3457 nvmf_ctrlr_process_io_fused_cmd(struct spdk_nvmf_request *req, struct spdk_bdev *bdev, 3458 struct spdk_bdev_desc *desc, struct spdk_io_channel *ch) 3459 { 3460 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 3461 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 3462 struct spdk_nvmf_request *first_fused_req = req->qpair->first_fused_req; 3463 int rc; 3464 3465 if (cmd->fuse == SPDK_NVME_CMD_FUSE_FIRST) { 3466 /* first fused operation (should be compare) */ 3467 if (first_fused_req != NULL) { 3468 struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl; 3469 3470 SPDK_ERRLOG("Wrong sequence of fused operations\n"); 3471 3472 /* abort req->qpair->first_fused_request and continue with new fused command */ 3473 fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED; 3474 fused_response->status.sct = SPDK_NVME_SCT_GENERIC; 3475 _nvmf_request_complete(first_fused_req); 3476 } else if (cmd->opc != SPDK_NVME_OPC_COMPARE) { 3477 SPDK_ERRLOG("Wrong op code of fused operations\n"); 3478 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 3479 rsp->status.sc = SPDK_NVME_SC_INVALID_OPCODE; 3480 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3481 } 3482 3483 req->qpair->first_fused_req = req; 3484 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 3485 } else if (cmd->fuse == SPDK_NVME_CMD_FUSE_SECOND) { 3486 /* second fused operation (should be write) */ 3487 if (first_fused_req == NULL) { 3488 SPDK_ERRLOG("Wrong sequence of fused operations\n"); 3489 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 3490 rsp->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED; 3491 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3492 } else if (cmd->opc != SPDK_NVME_OPC_WRITE) { 3493 struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl; 3494 3495 SPDK_ERRLOG("Wrong op code of fused operations\n"); 3496 3497 /* abort req->qpair->first_fused_request and fail current command */ 3498 fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED; 3499 fused_response->status.sct = SPDK_NVME_SCT_GENERIC; 3500 _nvmf_request_complete(first_fused_req); 3501 3502 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 3503 rsp->status.sc = SPDK_NVME_SC_INVALID_OPCODE; 3504 req->qpair->first_fused_req = NULL; 3505 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3506 } 3507 3508 /* save request of first command to generate response later */ 3509 req->first_fused_req = first_fused_req; 3510 req->qpair->first_fused_req = NULL; 3511 } else { 3512 SPDK_ERRLOG("Invalid fused command fuse field.\n"); 3513 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 3514 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 3515 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3516 } 3517 3518 rc = nvmf_bdev_ctrlr_compare_and_write_cmd(bdev, desc, ch, req->first_fused_req, req); 3519 3520 if (rc == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 3521 if (spdk_nvme_cpl_is_error(rsp)) { 3522 struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl; 3523 3524 fused_response->status = rsp->status; 3525 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 3526 rsp->status.sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED; 3527 /* Complete first of fused commands. Second will be completed by upper layer */ 3528 _nvmf_request_complete(first_fused_req); 3529 req->first_fused_req = NULL; 3530 } 3531 } 3532 3533 return rc; 3534 } 3535 3536 bool 3537 nvmf_ctrlr_use_zcopy(struct spdk_nvmf_request *req) 3538 { 3539 struct spdk_nvmf_ns *ns; 3540 3541 req->zcopy_phase = NVMF_ZCOPY_PHASE_NONE; 3542 3543 if (nvmf_qpair_is_admin_queue(req->qpair)) { 3544 /* Admin queue */ 3545 return false; 3546 } 3547 3548 if ((req->cmd->nvme_cmd.opc != SPDK_NVME_OPC_WRITE) && 3549 (req->cmd->nvme_cmd.opc != SPDK_NVME_OPC_READ)) { 3550 /* Not a READ or WRITE command */ 3551 return false; 3552 } 3553 3554 if (req->cmd->nvme_cmd.fuse != SPDK_NVME_CMD_FUSE_NONE) { 3555 /* Fused commands dont use zcopy buffers */ 3556 return false; 3557 } 3558 3559 ns = _nvmf_subsystem_get_ns(req->qpair->ctrlr->subsys, req->cmd->nvme_cmd.nsid); 3560 if (ns == NULL || ns->bdev == NULL || !ns->zcopy) { 3561 return false; 3562 } 3563 3564 req->zcopy_phase = NVMF_ZCOPY_PHASE_INIT; 3565 return true; 3566 } 3567 3568 /* If this function returns a non-zero value the request 3569 * reverts to using SPDK buffers 3570 */ 3571 int 3572 spdk_nvmf_request_zcopy_start(struct spdk_nvmf_request *req) 3573 { 3574 struct spdk_nvmf_qpair *qpair = req->qpair; 3575 struct spdk_nvmf_subsystem_poll_group *sgroup = NULL; 3576 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 3577 uint32_t nsid; 3578 struct spdk_bdev *bdev; 3579 struct spdk_bdev_desc *desc; 3580 struct spdk_io_channel *ch; 3581 int rc; 3582 3583 if (!qpair->ctrlr) { 3584 goto end; 3585 } 3586 3587 if (qpair->group->sgroups == NULL) { 3588 goto end; 3589 } 3590 3591 rc = spdk_nvmf_request_get_bdev(req->cmd->nvme_cmd.nsid, req, 3592 &bdev, &desc, &ch); 3593 if (rc != 0) { 3594 goto end; 3595 } 3596 3597 if (ch == NULL) { 3598 goto end; 3599 } 3600 3601 nsid = req->cmd->nvme_cmd.nsid; 3602 sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id]; 3603 ns_info = &sgroup->ns_info[nsid - 1]; 3604 if (ns_info->state != SPDK_NVMF_SUBSYSTEM_ACTIVE) { 3605 goto end; 3606 } 3607 3608 if (qpair->state != SPDK_NVMF_QPAIR_ACTIVE) { 3609 goto end; 3610 } 3611 3612 /* Set iovcnt to be the maximum number of 3613 * iovs that the ZCOPY can use 3614 */ 3615 req->iovcnt = NVMF_REQ_MAX_BUFFERS; 3616 TAILQ_INSERT_TAIL(&qpair->outstanding, req, link); 3617 rc = nvmf_bdev_ctrlr_start_zcopy(bdev, desc, ch, req); 3618 if (rc == 0) { 3619 ns_info->io_outstanding++; 3620 return 0; 3621 } 3622 TAILQ_REMOVE(&qpair->outstanding, req, link); 3623 3624 end: 3625 /* An error occurred, the subsystem is paused, or the qpair is not active. 3626 * Revert to using SPDK buffers 3627 */ 3628 req->zcopy_phase = NVMF_ZCOPY_PHASE_NONE; 3629 return -1; 3630 } 3631 3632 int 3633 spdk_nvmf_request_zcopy_end(struct spdk_nvmf_request *req, bool commit) 3634 { 3635 req->zcopy_phase = NVMF_ZCOPY_PHASE_END_PENDING; 3636 return nvmf_bdev_ctrlr_end_zcopy(req, commit); 3637 } 3638 3639 int 3640 nvmf_ctrlr_process_io_cmd(struct spdk_nvmf_request *req) 3641 { 3642 uint32_t nsid; 3643 struct spdk_nvmf_ns *ns; 3644 struct spdk_bdev *bdev; 3645 struct spdk_bdev_desc *desc; 3646 struct spdk_io_channel *ch; 3647 struct spdk_nvmf_poll_group *group = req->qpair->group; 3648 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 3649 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 3650 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 3651 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 3652 enum spdk_nvme_ana_state ana_state; 3653 3654 /* pre-set response details for this command */ 3655 response->status.sc = SPDK_NVME_SC_SUCCESS; 3656 nsid = cmd->nsid; 3657 3658 if (spdk_unlikely(ctrlr == NULL)) { 3659 SPDK_ERRLOG("I/O command sent before CONNECT\n"); 3660 response->status.sct = SPDK_NVME_SCT_GENERIC; 3661 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 3662 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3663 } 3664 3665 if (spdk_unlikely(ctrlr->vcprop.cc.bits.en != 1)) { 3666 SPDK_ERRLOG("I/O command sent to disabled controller\n"); 3667 response->status.sct = SPDK_NVME_SCT_GENERIC; 3668 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 3669 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3670 } 3671 3672 /* It will be lower overhead to check if ANA state is optimized or 3673 * non-optimized. 3674 */ 3675 ana_state = ctrlr->listener->ana_state; 3676 if (spdk_unlikely(ana_state != SPDK_NVME_ANA_OPTIMIZED_STATE && 3677 ana_state != SPDK_NVME_ANA_NON_OPTIMIZED_STATE)) { 3678 SPDK_DEBUGLOG(nvmf, "Fail I/O command due to ANA state %d\n", 3679 ana_state); 3680 response->status.sct = SPDK_NVME_SCT_PATH; 3681 response->status.sc = _nvme_ana_state_to_path_status(ana_state); 3682 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3683 } 3684 3685 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid); 3686 if (ns == NULL || ns->bdev == NULL) { 3687 SPDK_DEBUGLOG(nvmf, "Unsuccessful query for nsid %u\n", cmd->nsid); 3688 response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 3689 response->status.dnr = 1; 3690 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3691 } 3692 3693 /* scan-build falsely reporting dereference of null pointer */ 3694 assert(group != NULL && group->sgroups != NULL); 3695 ns_info = &group->sgroups[ctrlr->subsys->id].ns_info[nsid - 1]; 3696 if (nvmf_ns_reservation_request_check(ns_info, ctrlr, req)) { 3697 SPDK_DEBUGLOG(nvmf, "Reservation Conflict for nsid %u, opcode %u\n", 3698 cmd->nsid, cmd->opc); 3699 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3700 } 3701 3702 bdev = ns->bdev; 3703 desc = ns->desc; 3704 ch = ns_info->channel; 3705 3706 if (spdk_unlikely(cmd->fuse & SPDK_NVME_CMD_FUSE_MASK)) { 3707 return nvmf_ctrlr_process_io_fused_cmd(req, bdev, desc, ch); 3708 } else if (spdk_unlikely(req->qpair->first_fused_req != NULL)) { 3709 struct spdk_nvme_cpl *fused_response = &req->qpair->first_fused_req->rsp->nvme_cpl; 3710 3711 SPDK_ERRLOG("Expected second of fused commands - failing first of fused commands\n"); 3712 3713 /* abort req->qpair->first_fused_request and continue with new command */ 3714 fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED; 3715 fused_response->status.sct = SPDK_NVME_SCT_GENERIC; 3716 _nvmf_request_complete(req->qpair->first_fused_req); 3717 req->qpair->first_fused_req = NULL; 3718 } 3719 3720 switch (cmd->opc) { 3721 case SPDK_NVME_OPC_READ: 3722 return nvmf_bdev_ctrlr_read_cmd(bdev, desc, ch, req); 3723 case SPDK_NVME_OPC_WRITE: 3724 return nvmf_bdev_ctrlr_write_cmd(bdev, desc, ch, req); 3725 case SPDK_NVME_OPC_COMPARE: 3726 return nvmf_bdev_ctrlr_compare_cmd(bdev, desc, ch, req); 3727 case SPDK_NVME_OPC_WRITE_ZEROES: 3728 return nvmf_bdev_ctrlr_write_zeroes_cmd(bdev, desc, ch, req); 3729 case SPDK_NVME_OPC_FLUSH: 3730 return nvmf_bdev_ctrlr_flush_cmd(bdev, desc, ch, req); 3731 case SPDK_NVME_OPC_DATASET_MANAGEMENT: 3732 return nvmf_bdev_ctrlr_dsm_cmd(bdev, desc, ch, req); 3733 case SPDK_NVME_OPC_RESERVATION_REGISTER: 3734 case SPDK_NVME_OPC_RESERVATION_ACQUIRE: 3735 case SPDK_NVME_OPC_RESERVATION_RELEASE: 3736 case SPDK_NVME_OPC_RESERVATION_REPORT: 3737 spdk_thread_send_msg(ctrlr->subsys->thread, nvmf_ns_reservation_request, req); 3738 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 3739 default: 3740 return nvmf_bdev_ctrlr_nvme_passthru_io(bdev, desc, ch, req); 3741 } 3742 } 3743 3744 static void 3745 nvmf_qpair_request_cleanup(struct spdk_nvmf_qpair *qpair) 3746 { 3747 if (qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING) { 3748 assert(qpair->state_cb != NULL); 3749 3750 if (TAILQ_EMPTY(&qpair->outstanding)) { 3751 qpair->state_cb(qpair->state_cb_arg, 0); 3752 } 3753 } 3754 } 3755 3756 int 3757 spdk_nvmf_request_free(struct spdk_nvmf_request *req) 3758 { 3759 struct spdk_nvmf_qpair *qpair = req->qpair; 3760 3761 TAILQ_REMOVE(&qpair->outstanding, req, link); 3762 if (nvmf_transport_req_free(req)) { 3763 SPDK_ERRLOG("Unable to free transport level request resources.\n"); 3764 } 3765 3766 nvmf_qpair_request_cleanup(qpair); 3767 3768 return 0; 3769 } 3770 3771 static void 3772 _nvmf_request_complete(void *ctx) 3773 { 3774 struct spdk_nvmf_request *req = ctx; 3775 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 3776 struct spdk_nvmf_qpair *qpair; 3777 struct spdk_nvmf_subsystem_poll_group *sgroup = NULL; 3778 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 3779 bool is_aer = false; 3780 uint32_t nsid; 3781 bool paused; 3782 uint8_t opcode; 3783 3784 rsp->sqid = 0; 3785 rsp->status.p = 0; 3786 rsp->cid = req->cmd->nvme_cmd.cid; 3787 nsid = req->cmd->nvme_cmd.nsid; 3788 opcode = req->cmd->nvmf_cmd.opcode; 3789 3790 qpair = req->qpair; 3791 if (qpair->ctrlr) { 3792 sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id]; 3793 assert(sgroup != NULL); 3794 is_aer = req->cmd->nvme_cmd.opc == SPDK_NVME_OPC_ASYNC_EVENT_REQUEST; 3795 3796 /* 3797 * Set the crd value. 3798 * If the the IO has any error, and dnr (DoNotRetry) is not 1, 3799 * and ACRE is enabled, we will set the crd to 1 to select the first CRDT. 3800 */ 3801 if (spdk_nvme_cpl_is_error(rsp) && 3802 rsp->status.dnr == 0 && 3803 qpair->ctrlr->acre_enabled) { 3804 rsp->status.crd = 1; 3805 } 3806 } else if (spdk_unlikely(nvmf_request_is_fabric_connect(req))) { 3807 sgroup = nvmf_subsystem_pg_from_connect_cmd(req); 3808 } 3809 3810 if (SPDK_DEBUGLOG_FLAG_ENABLED("nvmf")) { 3811 spdk_nvme_print_completion(qpair->qid, rsp); 3812 } 3813 3814 switch (req->zcopy_phase) { 3815 case NVMF_ZCOPY_PHASE_NONE: 3816 TAILQ_REMOVE(&qpair->outstanding, req, link); 3817 break; 3818 case NVMF_ZCOPY_PHASE_INIT: 3819 if (spdk_unlikely(spdk_nvme_cpl_is_error(rsp))) { 3820 /* The START failed or was aborted so revert to a normal IO */ 3821 req->zcopy_phase = NVMF_ZCOPY_PHASE_INIT_FAILED; 3822 TAILQ_REMOVE(&qpair->outstanding, req, link); 3823 } else { 3824 req->zcopy_phase = NVMF_ZCOPY_PHASE_EXECUTE; 3825 } 3826 break; 3827 case NVMF_ZCOPY_PHASE_EXECUTE: 3828 break; 3829 case NVMF_ZCOPY_PHASE_END_PENDING: 3830 TAILQ_REMOVE(&qpair->outstanding, req, link); 3831 req->zcopy_phase = NVMF_ZCOPY_PHASE_COMPLETE; 3832 break; 3833 default: 3834 SPDK_ERRLOG("Invalid ZCOPY phase %u\n", req->zcopy_phase); 3835 break; 3836 } 3837 3838 if (nvmf_transport_req_complete(req)) { 3839 SPDK_ERRLOG("Transport request completion error!\n"); 3840 } 3841 3842 /* AER cmd is an exception */ 3843 if (sgroup && !is_aer) { 3844 if (spdk_unlikely(opcode == SPDK_NVME_OPC_FABRIC || 3845 nvmf_qpair_is_admin_queue(qpair))) { 3846 assert(sgroup->mgmt_io_outstanding > 0); 3847 sgroup->mgmt_io_outstanding--; 3848 } else { 3849 if ((req->zcopy_phase == NVMF_ZCOPY_PHASE_NONE) || 3850 (req->zcopy_phase == NVMF_ZCOPY_PHASE_COMPLETE)) { 3851 /* End of request */ 3852 3853 /* NOTE: This implicitly also checks for 0, since 0 - 1 wraps around to UINT32_MAX. */ 3854 if (spdk_likely(nsid - 1 < sgroup->num_ns)) { 3855 sgroup->ns_info[nsid - 1].io_outstanding--; 3856 } 3857 } 3858 } 3859 3860 if (spdk_unlikely(sgroup->state == SPDK_NVMF_SUBSYSTEM_PAUSING && 3861 sgroup->mgmt_io_outstanding == 0)) { 3862 paused = true; 3863 for (nsid = 0; nsid < sgroup->num_ns; nsid++) { 3864 ns_info = &sgroup->ns_info[nsid]; 3865 3866 if (ns_info->state == SPDK_NVMF_SUBSYSTEM_PAUSING && 3867 ns_info->io_outstanding > 0) { 3868 paused = false; 3869 break; 3870 } 3871 } 3872 3873 if (paused) { 3874 sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSED; 3875 sgroup->cb_fn(sgroup->cb_arg, 0); 3876 sgroup->cb_fn = NULL; 3877 sgroup->cb_arg = NULL; 3878 } 3879 } 3880 3881 } 3882 3883 nvmf_qpair_request_cleanup(qpair); 3884 } 3885 3886 int 3887 spdk_nvmf_request_complete(struct spdk_nvmf_request *req) 3888 { 3889 struct spdk_nvmf_qpair *qpair = req->qpair; 3890 3891 if (spdk_likely(qpair->group->thread == spdk_get_thread())) { 3892 _nvmf_request_complete(req); 3893 } else { 3894 spdk_thread_send_msg(qpair->group->thread, 3895 _nvmf_request_complete, req); 3896 } 3897 3898 return 0; 3899 } 3900 3901 void 3902 spdk_nvmf_request_exec_fabrics(struct spdk_nvmf_request *req) 3903 { 3904 struct spdk_nvmf_qpair *qpair = req->qpair; 3905 struct spdk_nvmf_subsystem_poll_group *sgroup = NULL; 3906 enum spdk_nvmf_request_exec_status status; 3907 3908 if (qpair->ctrlr) { 3909 sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id]; 3910 } else if (spdk_unlikely(nvmf_request_is_fabric_connect(req))) { 3911 sgroup = nvmf_subsystem_pg_from_connect_cmd(req); 3912 } 3913 3914 assert(sgroup != NULL); 3915 sgroup->mgmt_io_outstanding++; 3916 3917 /* Place the request on the outstanding list so we can keep track of it */ 3918 nvmf_add_to_outstanding_queue(req); 3919 3920 assert(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC); 3921 status = nvmf_ctrlr_process_fabrics_cmd(req); 3922 3923 if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 3924 _nvmf_request_complete(req); 3925 } 3926 } 3927 3928 static bool nvmf_check_subsystem_active(struct spdk_nvmf_request *req) 3929 { 3930 struct spdk_nvmf_qpair *qpair = req->qpair; 3931 struct spdk_nvmf_subsystem_poll_group *sgroup = NULL; 3932 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 3933 uint32_t nsid; 3934 3935 if (qpair->ctrlr) { 3936 sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id]; 3937 assert(sgroup != NULL); 3938 } else if (spdk_unlikely(nvmf_request_is_fabric_connect(req))) { 3939 sgroup = nvmf_subsystem_pg_from_connect_cmd(req); 3940 } 3941 3942 /* Check if the subsystem is paused (if there is a subsystem) */ 3943 if (sgroup != NULL) { 3944 if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC || 3945 nvmf_qpair_is_admin_queue(qpair))) { 3946 if (sgroup->state != SPDK_NVMF_SUBSYSTEM_ACTIVE) { 3947 /* The subsystem is not currently active. Queue this request. */ 3948 TAILQ_INSERT_TAIL(&sgroup->queued, req, link); 3949 return false; 3950 } 3951 sgroup->mgmt_io_outstanding++; 3952 } else { 3953 nsid = req->cmd->nvme_cmd.nsid; 3954 3955 /* NOTE: This implicitly also checks for 0, since 0 - 1 wraps around to UINT32_MAX. */ 3956 if (spdk_unlikely(nsid - 1 >= sgroup->num_ns)) { 3957 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3958 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 3959 req->rsp->nvme_cpl.status.dnr = 1; 3960 nvmf_add_to_outstanding_queue(req); 3961 _nvmf_request_complete(req); 3962 return false; 3963 } 3964 3965 ns_info = &sgroup->ns_info[nsid - 1]; 3966 if (ns_info->channel == NULL) { 3967 /* This can can happen if host sends I/O to a namespace that is 3968 * in the process of being added, but before the full addition 3969 * process is complete. Report invalid namespace in that case. 3970 */ 3971 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3972 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 3973 req->rsp->nvme_cpl.status.dnr = 1; 3974 nvmf_add_to_outstanding_queue(req); 3975 ns_info->io_outstanding++; 3976 _nvmf_request_complete(req); 3977 return false; 3978 } 3979 3980 if (ns_info->state != SPDK_NVMF_SUBSYSTEM_ACTIVE) { 3981 /* The namespace is not currently active. Queue this request. */ 3982 TAILQ_INSERT_TAIL(&sgroup->queued, req, link); 3983 return false; 3984 } 3985 3986 ns_info->io_outstanding++; 3987 } 3988 3989 if (qpair->state != SPDK_NVMF_QPAIR_ACTIVE) { 3990 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3991 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 3992 nvmf_add_to_outstanding_queue(req); 3993 _nvmf_request_complete(req); 3994 return false; 3995 } 3996 } 3997 3998 return true; 3999 } 4000 4001 void 4002 spdk_nvmf_request_exec(struct spdk_nvmf_request *req) 4003 { 4004 struct spdk_nvmf_qpair *qpair = req->qpair; 4005 enum spdk_nvmf_request_exec_status status; 4006 4007 if (!spdk_nvmf_using_zcopy(req->zcopy_phase)) { 4008 if (!nvmf_check_subsystem_active(req)) { 4009 return; 4010 } 4011 } 4012 4013 if (SPDK_DEBUGLOG_FLAG_ENABLED("nvmf")) { 4014 spdk_nvme_print_command(qpair->qid, &req->cmd->nvme_cmd); 4015 } 4016 4017 /* Place the request on the outstanding list so we can keep track of it */ 4018 nvmf_add_to_outstanding_queue(req); 4019 4020 if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC)) { 4021 status = nvmf_ctrlr_process_fabrics_cmd(req); 4022 } else if (spdk_unlikely(nvmf_qpair_is_admin_queue(qpair))) { 4023 status = nvmf_ctrlr_process_admin_cmd(req); 4024 } else { 4025 status = nvmf_ctrlr_process_io_cmd(req); 4026 } 4027 4028 if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 4029 _nvmf_request_complete(req); 4030 } 4031 } 4032 4033 static bool 4034 nvmf_ctrlr_get_dif_ctx(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_cmd *cmd, 4035 struct spdk_dif_ctx *dif_ctx) 4036 { 4037 struct spdk_nvmf_ns *ns; 4038 struct spdk_bdev *bdev; 4039 4040 if (ctrlr == NULL || cmd == NULL) { 4041 return false; 4042 } 4043 4044 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 4045 if (ns == NULL || ns->bdev == NULL) { 4046 return false; 4047 } 4048 4049 bdev = ns->bdev; 4050 4051 switch (cmd->opc) { 4052 case SPDK_NVME_OPC_READ: 4053 case SPDK_NVME_OPC_WRITE: 4054 case SPDK_NVME_OPC_COMPARE: 4055 return nvmf_bdev_ctrlr_get_dif_ctx(bdev, cmd, dif_ctx); 4056 default: 4057 break; 4058 } 4059 4060 return false; 4061 } 4062 4063 bool 4064 spdk_nvmf_request_get_dif_ctx(struct spdk_nvmf_request *req, struct spdk_dif_ctx *dif_ctx) 4065 { 4066 struct spdk_nvmf_qpair *qpair = req->qpair; 4067 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 4068 4069 if (spdk_likely(ctrlr == NULL || !ctrlr->dif_insert_or_strip)) { 4070 return false; 4071 } 4072 4073 if (spdk_unlikely(qpair->state != SPDK_NVMF_QPAIR_ACTIVE)) { 4074 return false; 4075 } 4076 4077 if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC)) { 4078 return false; 4079 } 4080 4081 if (spdk_unlikely(nvmf_qpair_is_admin_queue(qpair))) { 4082 return false; 4083 } 4084 4085 return nvmf_ctrlr_get_dif_ctx(ctrlr, &req->cmd->nvme_cmd, dif_ctx); 4086 } 4087 4088 void 4089 spdk_nvmf_set_custom_admin_cmd_hdlr(uint8_t opc, spdk_nvmf_custom_cmd_hdlr hdlr) 4090 { 4091 g_nvmf_custom_admin_cmd_hdlrs[opc].hdlr = hdlr; 4092 } 4093 4094 static int 4095 nvmf_passthru_admin_cmd(struct spdk_nvmf_request *req) 4096 { 4097 struct spdk_bdev *bdev; 4098 struct spdk_bdev_desc *desc; 4099 struct spdk_io_channel *ch; 4100 struct spdk_nvme_cmd *cmd = spdk_nvmf_request_get_cmd(req); 4101 struct spdk_nvme_cpl *response = spdk_nvmf_request_get_response(req); 4102 uint32_t bdev_nsid; 4103 int rc; 4104 4105 if (g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].nsid == 0) { 4106 bdev_nsid = cmd->nsid; 4107 } else { 4108 bdev_nsid = g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].nsid; 4109 } 4110 4111 rc = spdk_nvmf_request_get_bdev(bdev_nsid, req, &bdev, &desc, &ch); 4112 if (rc) { 4113 response->status.sct = SPDK_NVME_SCT_GENERIC; 4114 response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 4115 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 4116 } 4117 return spdk_nvmf_bdev_ctrlr_nvme_passthru_admin(bdev, desc, ch, req, NULL); 4118 } 4119 4120 void 4121 spdk_nvmf_set_passthru_admin_cmd(uint8_t opc, uint32_t forward_nsid) 4122 { 4123 g_nvmf_custom_admin_cmd_hdlrs[opc].hdlr = nvmf_passthru_admin_cmd; 4124 g_nvmf_custom_admin_cmd_hdlrs[opc].nsid = forward_nsid; 4125 } 4126 4127 int 4128 spdk_nvmf_request_get_bdev(uint32_t nsid, struct spdk_nvmf_request *req, 4129 struct spdk_bdev **bdev, struct spdk_bdev_desc **desc, struct spdk_io_channel **ch) 4130 { 4131 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 4132 struct spdk_nvmf_ns *ns; 4133 struct spdk_nvmf_poll_group *group = req->qpair->group; 4134 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 4135 4136 *bdev = NULL; 4137 *desc = NULL; 4138 *ch = NULL; 4139 4140 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid); 4141 if (ns == NULL || ns->bdev == NULL) { 4142 return -EINVAL; 4143 } 4144 4145 assert(group != NULL && group->sgroups != NULL); 4146 ns_info = &group->sgroups[ctrlr->subsys->id].ns_info[nsid - 1]; 4147 *bdev = ns->bdev; 4148 *desc = ns->desc; 4149 *ch = ns_info->channel; 4150 4151 return 0; 4152 } 4153 4154 struct spdk_nvmf_ctrlr *spdk_nvmf_request_get_ctrlr(struct spdk_nvmf_request *req) 4155 { 4156 return req->qpair->ctrlr; 4157 } 4158 4159 struct spdk_nvme_cmd *spdk_nvmf_request_get_cmd(struct spdk_nvmf_request *req) 4160 { 4161 return &req->cmd->nvme_cmd; 4162 } 4163 4164 struct spdk_nvme_cpl *spdk_nvmf_request_get_response(struct spdk_nvmf_request *req) 4165 { 4166 return &req->rsp->nvme_cpl; 4167 } 4168 4169 struct spdk_nvmf_subsystem *spdk_nvmf_request_get_subsystem(struct spdk_nvmf_request *req) 4170 { 4171 return req->qpair->ctrlr->subsys; 4172 } 4173 4174 void spdk_nvmf_request_get_data(struct spdk_nvmf_request *req, void **data, uint32_t *length) 4175 { 4176 *data = req->data; 4177 *length = req->length; 4178 } 4179 4180 struct spdk_nvmf_subsystem *spdk_nvmf_ctrlr_get_subsystem(struct spdk_nvmf_ctrlr *ctrlr) 4181 { 4182 return ctrlr->subsys; 4183 } 4184 4185 uint16_t spdk_nvmf_ctrlr_get_id(struct spdk_nvmf_ctrlr *ctrlr) 4186 { 4187 return ctrlr->cntlid; 4188 } 4189 4190 struct spdk_nvmf_request *spdk_nvmf_request_get_req_to_abort(struct spdk_nvmf_request *req) 4191 { 4192 return req->req_to_abort; 4193 } 4194