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