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