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