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