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