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