1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2017 Intel Corporation. All rights reserved. 3 * Copyright (c) 2019, 2020 Mellanox Technologies LTD. All rights reserved. 4 * Copyright (c) 2021, 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 5 */ 6 7 #include "spdk/stdinc.h" 8 9 #include "nvmf_internal.h" 10 #include "transport.h" 11 12 #include "spdk/bdev.h" 13 #include "spdk/bdev_zone.h" 14 #include "spdk/bit_array.h" 15 #include "spdk/endian.h" 16 #include "spdk/thread.h" 17 #include "spdk/nvme_spec.h" 18 #include "spdk/nvmf_cmd.h" 19 #include "spdk/string.h" 20 #include "spdk/util.h" 21 #include "spdk/version.h" 22 #include "spdk/log.h" 23 #include "spdk_internal/usdt.h" 24 25 #define MIN_KEEP_ALIVE_TIMEOUT_IN_MS 10000 26 #define NVMF_DISC_KATO_IN_MS 120000 27 #define KAS_TIME_UNIT_IN_MS 100 28 #define KAS_DEFAULT_VALUE (MIN_KEEP_ALIVE_TIMEOUT_IN_MS / KAS_TIME_UNIT_IN_MS) 29 30 #define NVMF_CC_RESET_SHN_TIMEOUT_IN_MS 10000 31 32 #define NVMF_CTRLR_RESET_SHN_TIMEOUT_IN_MS (NVMF_CC_RESET_SHN_TIMEOUT_IN_MS + 5000) 33 34 #define DUPLICATE_QID_RETRY_US 100 35 36 /* 37 * Report the SPDK version as the firmware revision. 38 * SPDK_VERSION_STRING won't fit into FR (only 8 bytes), so try to fit the most important parts. 39 */ 40 #define FW_VERSION SPDK_VERSION_MAJOR_STRING SPDK_VERSION_MINOR_STRING SPDK_VERSION_PATCH_STRING 41 42 #define ANA_TRANSITION_TIME_IN_SEC 10 43 44 #define NVMF_ABORT_COMMAND_LIMIT 3 45 46 /* 47 * Support for custom admin command handlers 48 */ 49 struct spdk_nvmf_custom_admin_cmd { 50 spdk_nvmf_custom_cmd_hdlr hdlr; 51 uint32_t nsid; /* nsid to forward */ 52 }; 53 54 static struct spdk_nvmf_custom_admin_cmd g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_MAX_OPC + 1]; 55 56 static void _nvmf_request_complete(void *ctx); 57 int nvmf_passthru_admin_cmd_for_ctrlr(struct spdk_nvmf_request *req, struct spdk_nvmf_ctrlr *ctrlr); 58 59 static inline void 60 nvmf_invalid_connect_response(struct spdk_nvmf_fabric_connect_rsp *rsp, 61 uint8_t iattr, uint16_t ipo) 62 { 63 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 64 rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 65 rsp->status_code_specific.invalid.iattr = iattr; 66 rsp->status_code_specific.invalid.ipo = ipo; 67 } 68 69 #define SPDK_NVMF_INVALID_CONNECT_CMD(rsp, field) \ 70 nvmf_invalid_connect_response(rsp, 0, offsetof(struct spdk_nvmf_fabric_connect_cmd, field)) 71 #define SPDK_NVMF_INVALID_CONNECT_DATA(rsp, field) \ 72 nvmf_invalid_connect_response(rsp, 1, offsetof(struct spdk_nvmf_fabric_connect_data, field)) 73 74 75 static void 76 nvmf_ctrlr_stop_keep_alive_timer(struct spdk_nvmf_ctrlr *ctrlr) 77 { 78 if (!ctrlr) { 79 SPDK_ERRLOG("Controller is NULL\n"); 80 return; 81 } 82 83 if (ctrlr->keep_alive_poller == NULL) { 84 return; 85 } 86 87 SPDK_DEBUGLOG(nvmf, "Stop keep alive poller\n"); 88 spdk_poller_unregister(&ctrlr->keep_alive_poller); 89 } 90 91 static void 92 nvmf_ctrlr_stop_association_timer(struct spdk_nvmf_ctrlr *ctrlr) 93 { 94 if (!ctrlr) { 95 SPDK_ERRLOG("Controller is NULL\n"); 96 assert(false); 97 return; 98 } 99 100 if (ctrlr->association_timer == NULL) { 101 return; 102 } 103 104 SPDK_DEBUGLOG(nvmf, "Stop association timer\n"); 105 spdk_poller_unregister(&ctrlr->association_timer); 106 } 107 108 static void 109 nvmf_ctrlr_disconnect_qpairs_done(struct spdk_io_channel_iter *i, int status) 110 { 111 if (status == 0) { 112 SPDK_DEBUGLOG(nvmf, "ctrlr disconnect qpairs complete successfully\n"); 113 } else { 114 SPDK_ERRLOG("Fail to disconnect ctrlr qpairs\n"); 115 } 116 } 117 118 static int 119 _nvmf_ctrlr_disconnect_qpairs_on_pg(struct spdk_io_channel_iter *i, bool include_admin) 120 { 121 int rc = 0; 122 struct spdk_nvmf_ctrlr *ctrlr; 123 struct spdk_nvmf_qpair *qpair, *temp_qpair; 124 struct spdk_io_channel *ch; 125 struct spdk_nvmf_poll_group *group; 126 127 ctrlr = spdk_io_channel_iter_get_ctx(i); 128 ch = spdk_io_channel_iter_get_channel(i); 129 group = spdk_io_channel_get_ctx(ch); 130 131 TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, temp_qpair) { 132 if (qpair->ctrlr == ctrlr && (include_admin || !nvmf_qpair_is_admin_queue(qpair))) { 133 rc = spdk_nvmf_qpair_disconnect(qpair, NULL, NULL); 134 if (rc) { 135 if (rc == -EINPROGRESS) { 136 rc = 0; 137 } else { 138 SPDK_ERRLOG("Qpair disconnect failed\n"); 139 return rc; 140 } 141 } 142 } 143 } 144 145 return rc; 146 } 147 148 static void 149 nvmf_ctrlr_disconnect_qpairs_on_pg(struct spdk_io_channel_iter *i) 150 { 151 spdk_for_each_channel_continue(i, _nvmf_ctrlr_disconnect_qpairs_on_pg(i, true)); 152 } 153 154 static void 155 nvmf_ctrlr_disconnect_io_qpairs_on_pg(struct spdk_io_channel_iter *i) 156 { 157 spdk_for_each_channel_continue(i, _nvmf_ctrlr_disconnect_qpairs_on_pg(i, false)); 158 } 159 160 static int 161 nvmf_ctrlr_keep_alive_poll(void *ctx) 162 { 163 uint64_t keep_alive_timeout_tick; 164 uint64_t now = spdk_get_ticks(); 165 struct spdk_nvmf_ctrlr *ctrlr = ctx; 166 167 if (ctrlr->in_destruct) { 168 nvmf_ctrlr_stop_keep_alive_timer(ctrlr); 169 return SPDK_POLLER_IDLE; 170 } 171 172 SPDK_DEBUGLOG(nvmf, "Polling ctrlr keep alive timeout\n"); 173 174 /* If the Keep alive feature is in use and the timer expires */ 175 keep_alive_timeout_tick = ctrlr->last_keep_alive_tick + 176 ctrlr->feat.keep_alive_timer.bits.kato * spdk_get_ticks_hz() / UINT64_C(1000); 177 if (now > keep_alive_timeout_tick) { 178 SPDK_NOTICELOG("Disconnecting host %s from subsystem %s due to keep alive timeout.\n", 179 ctrlr->hostnqn, ctrlr->subsys->subnqn); 180 /* set the Controller Fatal Status bit to '1' */ 181 if (ctrlr->vcprop.csts.bits.cfs == 0) { 182 nvmf_ctrlr_set_fatal_status(ctrlr); 183 184 /* 185 * disconnect qpairs, terminate Transport connection 186 * destroy ctrlr, break the host to controller association 187 * disconnect qpairs with qpair->ctrlr == ctrlr 188 */ 189 spdk_for_each_channel(ctrlr->subsys->tgt, 190 nvmf_ctrlr_disconnect_qpairs_on_pg, 191 ctrlr, 192 nvmf_ctrlr_disconnect_qpairs_done); 193 return SPDK_POLLER_BUSY; 194 } 195 } 196 197 return SPDK_POLLER_IDLE; 198 } 199 200 static void 201 nvmf_ctrlr_start_keep_alive_timer(struct spdk_nvmf_ctrlr *ctrlr) 202 { 203 if (!ctrlr) { 204 SPDK_ERRLOG("Controller is NULL\n"); 205 return; 206 } 207 208 /* if cleared to 0 then the Keep Alive Timer is disabled */ 209 if (ctrlr->feat.keep_alive_timer.bits.kato != 0) { 210 211 ctrlr->last_keep_alive_tick = spdk_get_ticks(); 212 213 SPDK_DEBUGLOG(nvmf, "Ctrlr add keep alive poller\n"); 214 ctrlr->keep_alive_poller = SPDK_POLLER_REGISTER(nvmf_ctrlr_keep_alive_poll, ctrlr, 215 ctrlr->feat.keep_alive_timer.bits.kato * 1000); 216 } 217 } 218 219 static int _retry_qid_check(void *ctx); 220 221 static void 222 ctrlr_add_qpair_and_send_rsp(struct spdk_nvmf_qpair *qpair, 223 struct spdk_nvmf_ctrlr *ctrlr, 224 struct spdk_nvmf_request *req) 225 { 226 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 227 228 assert(ctrlr->admin_qpair->group->thread == spdk_get_thread()); 229 230 if (spdk_bit_array_get(ctrlr->qpair_mask, qpair->qid)) { 231 if (qpair->connect_req != NULL) { 232 SPDK_ERRLOG("Got I/O connect with duplicate QID %u\n", qpair->qid); 233 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 234 rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER; 235 qpair->connect_req = NULL; 236 qpair->ctrlr = NULL; 237 spdk_nvmf_request_complete(req); 238 } else { 239 SPDK_WARNLOG("Duplicate QID detected, re-check in %dus\n", 240 DUPLICATE_QID_RETRY_US); 241 qpair->connect_req = req; 242 /* Set qpair->ctrlr here so that we'll have it when the poller expires. */ 243 qpair->ctrlr = ctrlr; 244 req->poller = SPDK_POLLER_REGISTER(_retry_qid_check, qpair, 245 DUPLICATE_QID_RETRY_US); 246 } 247 return; 248 } 249 250 qpair->ctrlr = ctrlr; 251 spdk_bit_array_set(ctrlr->qpair_mask, qpair->qid); 252 253 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 254 rsp->status_code_specific.success.cntlid = ctrlr->cntlid; 255 SPDK_DEBUGLOG(nvmf, "connect capsule response: cntlid = 0x%04x\n", 256 rsp->status_code_specific.success.cntlid); 257 spdk_nvmf_request_complete(req); 258 259 SPDK_DTRACE_PROBE4_TICKS(nvmf_ctrlr_add_qpair, qpair, qpair->qid, ctrlr->subsys->subnqn, 260 ctrlr->hostnqn); 261 } 262 263 static int 264 _retry_qid_check(void *ctx) 265 { 266 struct spdk_nvmf_qpair *qpair = ctx; 267 struct spdk_nvmf_request *req = qpair->connect_req; 268 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 269 270 spdk_poller_unregister(&req->poller); 271 ctrlr_add_qpair_and_send_rsp(qpair, ctrlr, req); 272 return SPDK_POLLER_BUSY; 273 } 274 275 static void 276 _nvmf_ctrlr_add_admin_qpair(void *ctx) 277 { 278 struct spdk_nvmf_request *req = ctx; 279 struct spdk_nvmf_qpair *qpair = req->qpair; 280 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 281 282 ctrlr->admin_qpair = qpair; 283 ctrlr->association_timeout = qpair->transport->opts.association_timeout; 284 nvmf_ctrlr_start_keep_alive_timer(ctrlr); 285 ctrlr_add_qpair_and_send_rsp(qpair, ctrlr, req); 286 } 287 288 static void 289 _nvmf_subsystem_add_ctrlr(void *ctx) 290 { 291 struct spdk_nvmf_request *req = ctx; 292 struct spdk_nvmf_qpair *qpair = req->qpair; 293 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 294 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 295 296 if (nvmf_subsystem_add_ctrlr(ctrlr->subsys, ctrlr)) { 297 SPDK_ERRLOG("Unable to add controller to subsystem\n"); 298 spdk_bit_array_free(&ctrlr->qpair_mask); 299 free(ctrlr); 300 qpair->ctrlr = NULL; 301 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 302 spdk_nvmf_request_complete(req); 303 return; 304 } 305 306 spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_add_admin_qpair, req); 307 } 308 309 static void 310 nvmf_ctrlr_cdata_init(struct spdk_nvmf_transport *transport, struct spdk_nvmf_subsystem *subsystem, 311 struct spdk_nvmf_ctrlr_data *cdata) 312 { 313 cdata->aerl = SPDK_NVMF_MAX_ASYNC_EVENTS - 1; 314 cdata->kas = KAS_DEFAULT_VALUE; 315 cdata->vid = SPDK_PCI_VID_INTEL; 316 cdata->ssvid = SPDK_PCI_VID_INTEL; 317 /* INTEL OUI */ 318 cdata->ieee[0] = 0xe4; 319 cdata->ieee[1] = 0xd2; 320 cdata->ieee[2] = 0x5c; 321 cdata->oncs.compare = 1; 322 cdata->oncs.reservations = 1; 323 cdata->fuses.compare_and_write = 1; 324 cdata->oncs.copy = 1; 325 cdata->sgls.supported = 1; 326 cdata->sgls.keyed_sgl = 1; 327 cdata->sgls.sgl_offset = 1; 328 cdata->nvmf_specific.ioccsz = sizeof(struct spdk_nvme_cmd) / 16; 329 cdata->nvmf_specific.ioccsz += transport->opts.in_capsule_data_size / 16; 330 cdata->nvmf_specific.iorcsz = sizeof(struct spdk_nvme_cpl) / 16; 331 cdata->nvmf_specific.icdoff = 0; /* offset starts directly after SQE */ 332 cdata->nvmf_specific.ctrattr.ctrlr_model = SPDK_NVMF_CTRLR_MODEL_DYNAMIC; 333 cdata->nvmf_specific.msdbd = 1; 334 335 if (transport->ops->cdata_init) { 336 transport->ops->cdata_init(transport, subsystem, cdata); 337 } 338 } 339 340 static bool 341 nvmf_subsys_has_multi_iocs(struct spdk_nvmf_subsystem *subsystem) 342 { 343 struct spdk_nvmf_ns *ns; 344 uint32_t i; 345 346 for (i = 0; i < subsystem->max_nsid; i++) { 347 ns = subsystem->ns[i]; 348 if (ns && ns->bdev && spdk_bdev_is_zoned(ns->bdev)) { 349 return true; 350 } 351 } 352 return false; 353 } 354 355 static struct spdk_nvmf_ctrlr * 356 nvmf_ctrlr_create(struct spdk_nvmf_subsystem *subsystem, 357 struct spdk_nvmf_request *req, 358 struct spdk_nvmf_fabric_connect_cmd *connect_cmd, 359 struct spdk_nvmf_fabric_connect_data *connect_data) 360 { 361 struct spdk_nvmf_ctrlr *ctrlr; 362 struct spdk_nvmf_transport *transport = req->qpair->transport; 363 struct spdk_nvme_transport_id listen_trid = {}; 364 bool subsys_has_multi_iocs = false; 365 366 ctrlr = calloc(1, sizeof(*ctrlr)); 367 if (ctrlr == NULL) { 368 SPDK_ERRLOG("Memory allocation failed\n"); 369 return NULL; 370 } 371 372 if (spdk_nvme_trtype_is_fabrics(transport->ops->type)) { 373 ctrlr->dynamic_ctrlr = true; 374 } else { 375 ctrlr->cntlid = connect_data->cntlid; 376 } 377 378 SPDK_DTRACE_PROBE3_TICKS(nvmf_ctrlr_create, ctrlr, subsystem->subnqn, 379 spdk_thread_get_id(req->qpair->group->thread)); 380 381 STAILQ_INIT(&ctrlr->async_events); 382 TAILQ_INIT(&ctrlr->log_head); 383 ctrlr->subsys = subsystem; 384 ctrlr->thread = req->qpair->group->thread; 385 ctrlr->disconnect_in_progress = false; 386 387 ctrlr->qpair_mask = spdk_bit_array_create(transport->opts.max_qpairs_per_ctrlr); 388 if (!ctrlr->qpair_mask) { 389 SPDK_ERRLOG("Failed to allocate controller qpair mask\n"); 390 goto err_qpair_mask; 391 } 392 393 nvmf_ctrlr_cdata_init(transport, subsystem, &ctrlr->cdata); 394 395 /* 396 * KAS: This field indicates the granularity of the Keep Alive Timer in 100ms units. 397 * If this field is cleared to 0h, then Keep Alive is not supported. 398 */ 399 if (ctrlr->cdata.kas) { 400 ctrlr->feat.keep_alive_timer.bits.kato = spdk_divide_round_up(connect_cmd->kato, 401 KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS) * 402 KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS; 403 } 404 405 ctrlr->feat.async_event_configuration.bits.ns_attr_notice = 1; 406 if (ctrlr->subsys->flags.ana_reporting) { 407 ctrlr->feat.async_event_configuration.bits.ana_change_notice = 1; 408 } 409 ctrlr->feat.volatile_write_cache.bits.wce = 1; 410 /* Coalescing Disable */ 411 ctrlr->feat.interrupt_vector_configuration.bits.cd = 1; 412 413 if (spdk_nvmf_subsystem_is_discovery(ctrlr->subsys)) { 414 /* 415 * If keep-alive timeout is not set, discovery controllers use some 416 * arbitrary high value in order to cleanup stale discovery sessions 417 * 418 * From the 1.0a nvme-of spec: 419 * "The Keep Alive command is reserved for 420 * Discovery controllers. A transport may specify a 421 * fixed Discovery controller activity timeout value 422 * (e.g., 2 minutes). If no commands are received 423 * by a Discovery controller within that time 424 * period, the controller may perform the 425 * actions for Keep Alive Timer expiration". 426 * 427 * From the 1.1 nvme-of spec: 428 * "A host requests an explicit persistent connection 429 * to a Discovery controller and Asynchronous Event Notifications from 430 * the Discovery controller on that persistent connection by specifying 431 * a non-zero Keep Alive Timer value in the Connect command." 432 * 433 * In case non-zero KATO is used, we enable discovery_log_change_notice 434 * otherwise we disable it and use default discovery controller KATO. 435 * KATO is in millisecond. 436 */ 437 if (ctrlr->feat.keep_alive_timer.bits.kato == 0) { 438 ctrlr->feat.keep_alive_timer.bits.kato = NVMF_DISC_KATO_IN_MS; 439 ctrlr->feat.async_event_configuration.bits.discovery_log_change_notice = 0; 440 } else { 441 ctrlr->feat.async_event_configuration.bits.discovery_log_change_notice = 1; 442 } 443 } 444 445 /* Subtract 1 for admin queue, 1 for 0's based */ 446 ctrlr->feat.number_of_queues.bits.ncqr = transport->opts.max_qpairs_per_ctrlr - 1 - 447 1; 448 ctrlr->feat.number_of_queues.bits.nsqr = transport->opts.max_qpairs_per_ctrlr - 1 - 449 1; 450 451 spdk_uuid_copy(&ctrlr->hostid, (struct spdk_uuid *)connect_data->hostid); 452 memcpy(ctrlr->hostnqn, connect_data->hostnqn, sizeof(ctrlr->hostnqn)); 453 454 ctrlr->vcprop.cap.raw = 0; 455 ctrlr->vcprop.cap.bits.cqr = 1; /* NVMe-oF specification required */ 456 ctrlr->vcprop.cap.bits.mqes = transport->opts.max_queue_depth - 457 1; /* max queue depth */ 458 ctrlr->vcprop.cap.bits.ams = 0; /* optional arb mechanisms */ 459 /* ready timeout - 500 msec units */ 460 ctrlr->vcprop.cap.bits.to = NVMF_CTRLR_RESET_SHN_TIMEOUT_IN_MS / 500; 461 ctrlr->vcprop.cap.bits.dstrd = 0; /* fixed to 0 for NVMe-oF */ 462 subsys_has_multi_iocs = nvmf_subsys_has_multi_iocs(subsystem); 463 if (subsys_has_multi_iocs) { 464 ctrlr->vcprop.cap.bits.css = 465 SPDK_NVME_CAP_CSS_IOCS; /* One or more I/O command sets supported */ 466 } else { 467 ctrlr->vcprop.cap.bits.css = SPDK_NVME_CAP_CSS_NVM; /* NVM command set */ 468 } 469 470 ctrlr->vcprop.cap.bits.mpsmin = 0; /* 2 ^ (12 + mpsmin) == 4k */ 471 ctrlr->vcprop.cap.bits.mpsmax = 0; /* 2 ^ (12 + mpsmax) == 4k */ 472 473 /* Version Supported: 1.3 */ 474 ctrlr->vcprop.vs.bits.mjr = 1; 475 ctrlr->vcprop.vs.bits.mnr = 3; 476 ctrlr->vcprop.vs.bits.ter = 0; 477 478 ctrlr->vcprop.cc.raw = 0; 479 ctrlr->vcprop.cc.bits.en = 0; /* Init controller disabled */ 480 if (subsys_has_multi_iocs) { 481 ctrlr->vcprop.cc.bits.css = 482 SPDK_NVME_CC_CSS_IOCS; /* All supported I/O Command Sets */ 483 } 484 485 ctrlr->vcprop.csts.raw = 0; 486 ctrlr->vcprop.csts.bits.rdy = 0; /* Init controller as not ready */ 487 488 SPDK_DEBUGLOG(nvmf, "cap 0x%" PRIx64 "\n", ctrlr->vcprop.cap.raw); 489 SPDK_DEBUGLOG(nvmf, "vs 0x%x\n", ctrlr->vcprop.vs.raw); 490 SPDK_DEBUGLOG(nvmf, "cc 0x%x\n", ctrlr->vcprop.cc.raw); 491 SPDK_DEBUGLOG(nvmf, "csts 0x%x\n", ctrlr->vcprop.csts.raw); 492 493 ctrlr->dif_insert_or_strip = transport->opts.dif_insert_or_strip; 494 495 if (ctrlr->subsys->subtype == SPDK_NVMF_SUBTYPE_NVME) { 496 if (spdk_nvmf_qpair_get_listen_trid(req->qpair, &listen_trid) != 0) { 497 SPDK_ERRLOG("Could not get listener transport ID\n"); 498 goto err_listener; 499 } 500 501 ctrlr->listener = nvmf_subsystem_find_listener(ctrlr->subsys, &listen_trid); 502 if (!ctrlr->listener) { 503 SPDK_ERRLOG("Listener was not found\n"); 504 goto err_listener; 505 } 506 } 507 508 req->qpair->ctrlr = ctrlr; 509 spdk_thread_send_msg(subsystem->thread, _nvmf_subsystem_add_ctrlr, req); 510 511 return ctrlr; 512 err_listener: 513 spdk_bit_array_free(&ctrlr->qpair_mask); 514 err_qpair_mask: 515 free(ctrlr); 516 return NULL; 517 } 518 519 static void 520 _nvmf_ctrlr_destruct(void *ctx) 521 { 522 struct spdk_nvmf_ctrlr *ctrlr = ctx; 523 struct spdk_nvmf_reservation_log *log, *log_tmp; 524 struct spdk_nvmf_async_event_completion *event, *event_tmp; 525 526 SPDK_DTRACE_PROBE3_TICKS(nvmf_ctrlr_destruct, ctrlr, ctrlr->subsys->subnqn, 527 spdk_thread_get_id(ctrlr->thread)); 528 529 assert(spdk_get_thread() == ctrlr->thread); 530 assert(ctrlr->in_destruct); 531 532 SPDK_DEBUGLOG(nvmf, "Destroy ctrlr 0x%hx\n", ctrlr->cntlid); 533 if (ctrlr->disconnect_in_progress) { 534 SPDK_ERRLOG("freeing ctrlr with disconnect in progress\n"); 535 spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_destruct, ctrlr); 536 return; 537 } 538 539 nvmf_ctrlr_stop_keep_alive_timer(ctrlr); 540 nvmf_ctrlr_stop_association_timer(ctrlr); 541 spdk_bit_array_free(&ctrlr->qpair_mask); 542 543 TAILQ_FOREACH_SAFE(log, &ctrlr->log_head, link, log_tmp) { 544 TAILQ_REMOVE(&ctrlr->log_head, log, link); 545 free(log); 546 } 547 STAILQ_FOREACH_SAFE(event, &ctrlr->async_events, link, event_tmp) { 548 STAILQ_REMOVE(&ctrlr->async_events, event, spdk_nvmf_async_event_completion, link); 549 free(event); 550 } 551 free(ctrlr); 552 } 553 554 void 555 nvmf_ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr) 556 { 557 nvmf_subsystem_remove_ctrlr(ctrlr->subsys, ctrlr); 558 559 spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_destruct, ctrlr); 560 } 561 562 static void 563 nvmf_ctrlr_add_io_qpair(void *ctx) 564 { 565 struct spdk_nvmf_request *req = ctx; 566 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 567 struct spdk_nvmf_qpair *qpair = req->qpair; 568 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 569 struct spdk_nvmf_qpair *admin_qpair = ctrlr->admin_qpair; 570 571 SPDK_DTRACE_PROBE4_TICKS(nvmf_ctrlr_add_io_qpair, ctrlr, req->qpair, req->qpair->qid, 572 spdk_thread_get_id(ctrlr->thread)); 573 574 /* Unit test will check qpair->ctrlr after calling spdk_nvmf_ctrlr_connect. 575 * For error case, the value should be NULL. So set it to NULL at first. 576 */ 577 qpair->ctrlr = NULL; 578 579 /* Make sure the controller is not being destroyed. */ 580 if (ctrlr->in_destruct) { 581 SPDK_ERRLOG("Got I/O connect while ctrlr was being destroyed.\n"); 582 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 583 goto end; 584 } 585 586 if (spdk_nvmf_subsystem_is_discovery(ctrlr->subsys)) { 587 SPDK_ERRLOG("I/O connect not allowed on discovery controller\n"); 588 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 589 goto end; 590 } 591 592 if (!ctrlr->vcprop.cc.bits.en) { 593 SPDK_ERRLOG("Got I/O connect before ctrlr was enabled\n"); 594 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 595 goto end; 596 } 597 598 if (1u << ctrlr->vcprop.cc.bits.iosqes != sizeof(struct spdk_nvme_cmd)) { 599 SPDK_ERRLOG("Got I/O connect with invalid IOSQES %u\n", 600 ctrlr->vcprop.cc.bits.iosqes); 601 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 602 goto end; 603 } 604 605 if (1u << ctrlr->vcprop.cc.bits.iocqes != sizeof(struct spdk_nvme_cpl)) { 606 SPDK_ERRLOG("Got I/O connect with invalid IOCQES %u\n", 607 ctrlr->vcprop.cc.bits.iocqes); 608 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 609 goto end; 610 } 611 612 if (admin_qpair->state != SPDK_NVMF_QPAIR_ACTIVE || admin_qpair->group == NULL) { 613 /* There is a chance that admin qpair is being destroyed at this moment due to e.g. 614 * expired keep alive timer. Part of the qpair destruction process is change of qpair's 615 * state to DEACTIVATING and removing it from poll group */ 616 SPDK_ERRLOG("Inactive admin qpair (state %d, group %p)\n", admin_qpair->state, admin_qpair->group); 617 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 618 goto end; 619 } 620 621 /* check if we would exceed ctrlr connection limit */ 622 if (qpair->qid >= spdk_bit_array_capacity(ctrlr->qpair_mask)) { 623 SPDK_ERRLOG("Requested QID %u but Max QID is %u\n", 624 qpair->qid, spdk_bit_array_capacity(ctrlr->qpair_mask) - 1); 625 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 626 rsp->status.sc = SPDK_NVME_SC_INVALID_QUEUE_IDENTIFIER; 627 goto end; 628 } 629 630 ctrlr_add_qpair_and_send_rsp(qpair, ctrlr, req); 631 return; 632 end: 633 spdk_nvmf_request_complete(req); 634 } 635 636 static void 637 _nvmf_ctrlr_add_io_qpair(void *ctx) 638 { 639 struct spdk_nvmf_request *req = ctx; 640 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 641 struct spdk_nvmf_fabric_connect_data *data; 642 struct spdk_nvmf_ctrlr *ctrlr; 643 struct spdk_nvmf_qpair *qpair = req->qpair; 644 struct spdk_nvmf_qpair *admin_qpair; 645 struct spdk_nvmf_tgt *tgt = qpair->transport->tgt; 646 struct spdk_nvmf_subsystem *subsystem; 647 struct spdk_nvme_transport_id listen_trid = {}; 648 const struct spdk_nvmf_subsystem_listener *listener; 649 650 assert(req->iovcnt == 1); 651 652 data = req->iov[0].iov_base; 653 654 SPDK_DEBUGLOG(nvmf, "Connect I/O Queue for controller id 0x%x\n", data->cntlid); 655 656 subsystem = spdk_nvmf_tgt_find_subsystem(tgt, data->subnqn); 657 /* We already checked this in spdk_nvmf_ctrlr_connect */ 658 assert(subsystem != NULL); 659 660 ctrlr = nvmf_subsystem_get_ctrlr(subsystem, data->cntlid); 661 if (ctrlr == NULL) { 662 SPDK_ERRLOG("Unknown controller ID 0x%x\n", data->cntlid); 663 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid); 664 spdk_nvmf_request_complete(req); 665 return; 666 } 667 668 /* fail before passing a message to the controller thread. */ 669 if (ctrlr->in_destruct) { 670 SPDK_ERRLOG("Got I/O connect while ctrlr was being destroyed.\n"); 671 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 672 spdk_nvmf_request_complete(req); 673 return; 674 } 675 676 /* If ANA reporting is enabled, check if I/O connect is on the same listener. */ 677 if (subsystem->flags.ana_reporting) { 678 if (spdk_nvmf_qpair_get_listen_trid(req->qpair, &listen_trid) != 0) { 679 SPDK_ERRLOG("Could not get listener transport ID\n"); 680 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 681 spdk_nvmf_request_complete(req); 682 return; 683 } 684 685 listener = nvmf_subsystem_find_listener(subsystem, &listen_trid); 686 if (listener != ctrlr->listener) { 687 SPDK_ERRLOG("I/O connect is on a listener different from admin connect\n"); 688 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 689 spdk_nvmf_request_complete(req); 690 return; 691 } 692 } 693 694 admin_qpair = ctrlr->admin_qpair; 695 if (admin_qpair->state != SPDK_NVMF_QPAIR_ACTIVE || admin_qpair->group == NULL) { 696 /* There is a chance that admin qpair is being destroyed at this moment due to e.g. 697 * expired keep alive timer. Part of the qpair destruction process is change of qpair's 698 * state to DEACTIVATING and removing it from poll group */ 699 SPDK_ERRLOG("Inactive admin qpair (state %d, group %p)\n", admin_qpair->state, admin_qpair->group); 700 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, qid); 701 spdk_nvmf_request_complete(req); 702 return; 703 } 704 qpair->ctrlr = ctrlr; 705 spdk_thread_send_msg(admin_qpair->group->thread, nvmf_ctrlr_add_io_qpair, req); 706 } 707 708 static bool 709 nvmf_qpair_access_allowed(struct spdk_nvmf_qpair *qpair, struct spdk_nvmf_subsystem *subsystem, 710 const char *hostnqn) 711 { 712 struct spdk_nvme_transport_id listen_trid = {}; 713 714 if (!spdk_nvmf_subsystem_host_allowed(subsystem, hostnqn)) { 715 SPDK_ERRLOG("Subsystem '%s' does not allow host '%s'\n", subsystem->subnqn, hostnqn); 716 return false; 717 } 718 719 if (spdk_nvmf_qpair_get_listen_trid(qpair, &listen_trid)) { 720 SPDK_ERRLOG("Subsystem '%s' is unable to enforce access control due to an internal error.\n", 721 subsystem->subnqn); 722 return false; 723 } 724 725 if (!spdk_nvmf_subsystem_listener_allowed(subsystem, &listen_trid)) { 726 SPDK_ERRLOG("Subsystem '%s' does not allow host '%s' to connect at this address.\n", 727 subsystem->subnqn, hostnqn); 728 return false; 729 } 730 731 return true; 732 } 733 734 static int 735 _nvmf_ctrlr_connect(struct spdk_nvmf_request *req) 736 { 737 struct spdk_nvmf_fabric_connect_data *data = req->iov[0].iov_base; 738 struct spdk_nvmf_fabric_connect_cmd *cmd = &req->cmd->connect_cmd; 739 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 740 struct spdk_nvmf_qpair *qpair = req->qpair; 741 struct spdk_nvmf_transport *transport = qpair->transport; 742 struct spdk_nvmf_ctrlr *ctrlr; 743 struct spdk_nvmf_subsystem *subsystem; 744 745 SPDK_DEBUGLOG(nvmf, "recfmt 0x%x qid %u sqsize %u\n", 746 cmd->recfmt, cmd->qid, cmd->sqsize); 747 748 SPDK_DEBUGLOG(nvmf, "Connect data:\n"); 749 SPDK_DEBUGLOG(nvmf, " cntlid: 0x%04x\n", data->cntlid); 750 SPDK_DEBUGLOG(nvmf, " hostid: %08x-%04x-%04x-%02x%02x-%04x%08x ***\n", 751 ntohl(*(uint32_t *)&data->hostid[0]), 752 ntohs(*(uint16_t *)&data->hostid[4]), 753 ntohs(*(uint16_t *)&data->hostid[6]), 754 data->hostid[8], 755 data->hostid[9], 756 ntohs(*(uint16_t *)&data->hostid[10]), 757 ntohl(*(uint32_t *)&data->hostid[12])); 758 SPDK_DEBUGLOG(nvmf, " subnqn: \"%s\"\n", data->subnqn); 759 SPDK_DEBUGLOG(nvmf, " hostnqn: \"%s\"\n", data->hostnqn); 760 761 subsystem = spdk_nvmf_tgt_find_subsystem(transport->tgt, data->subnqn); 762 if (!subsystem) { 763 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn); 764 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 765 } 766 767 if (cmd->recfmt != 0) { 768 SPDK_ERRLOG("Connect command unsupported RECFMT %u\n", cmd->recfmt); 769 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 770 rsp->status.sc = SPDK_NVMF_FABRIC_SC_INCOMPATIBLE_FORMAT; 771 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 772 } 773 774 /* 775 * SQSIZE is a 0-based value, so it must be at least 1 (minimum queue depth is 2) and 776 * strictly less than max_aq_depth (admin queues) or max_queue_depth (io queues). 777 */ 778 if (cmd->sqsize == 0) { 779 SPDK_ERRLOG("Invalid SQSIZE = 0\n"); 780 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize); 781 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 782 } 783 784 if (cmd->qid == 0) { 785 if (cmd->sqsize >= transport->opts.max_aq_depth) { 786 SPDK_ERRLOG("Invalid SQSIZE for admin queue %u (min 1, max %u)\n", 787 cmd->sqsize, transport->opts.max_aq_depth - 1); 788 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize); 789 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 790 } 791 } else if (cmd->sqsize >= transport->opts.max_queue_depth) { 792 SPDK_ERRLOG("Invalid SQSIZE %u (min 1, max %u)\n", 793 cmd->sqsize, transport->opts.max_queue_depth - 1); 794 SPDK_NVMF_INVALID_CONNECT_CMD(rsp, sqsize); 795 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 796 } 797 798 qpair->sq_head_max = cmd->sqsize; 799 qpair->qid = cmd->qid; 800 qpair->connect_received = true; 801 802 pthread_mutex_lock(&qpair->group->mutex); 803 qpair->group->current_unassociated_qpairs--; 804 pthread_mutex_unlock(&qpair->group->mutex); 805 806 if (0 == qpair->qid) { 807 qpair->group->stat.admin_qpairs++; 808 qpair->group->stat.current_admin_qpairs++; 809 } else { 810 qpair->group->stat.io_qpairs++; 811 qpair->group->stat.current_io_qpairs++; 812 } 813 814 if (cmd->qid == 0) { 815 SPDK_DEBUGLOG(nvmf, "Connect Admin Queue for controller ID 0x%x\n", data->cntlid); 816 817 if (spdk_nvme_trtype_is_fabrics(transport->ops->type) && data->cntlid != 0xFFFF) { 818 /* This NVMf target only supports dynamic mode. */ 819 SPDK_ERRLOG("The NVMf target only supports dynamic mode (CNTLID = 0x%x).\n", data->cntlid); 820 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, cntlid); 821 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 822 } 823 824 /* Establish a new ctrlr */ 825 ctrlr = nvmf_ctrlr_create(subsystem, req, cmd, data); 826 if (!ctrlr) { 827 SPDK_ERRLOG("nvmf_ctrlr_create() failed\n"); 828 rsp->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 829 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 830 } else { 831 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 832 } 833 } else { 834 spdk_thread_send_msg(subsystem->thread, _nvmf_ctrlr_add_io_qpair, req); 835 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 836 } 837 } 838 839 static inline bool 840 nvmf_request_is_fabric_connect(struct spdk_nvmf_request *req) 841 { 842 return req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC && 843 req->cmd->nvmf_cmd.fctype == SPDK_NVMF_FABRIC_COMMAND_CONNECT; 844 } 845 846 static struct spdk_nvmf_subsystem_poll_group * 847 nvmf_subsystem_pg_from_connect_cmd(struct spdk_nvmf_request *req) 848 { 849 struct spdk_nvmf_fabric_connect_data *data; 850 struct spdk_nvmf_subsystem *subsystem; 851 struct spdk_nvmf_tgt *tgt; 852 853 assert(nvmf_request_is_fabric_connect(req)); 854 assert(req->qpair->ctrlr == NULL); 855 assert(req->iovcnt == 1); 856 857 data = req->iov[0].iov_base; 858 tgt = req->qpair->transport->tgt; 859 860 subsystem = spdk_nvmf_tgt_find_subsystem(tgt, data->subnqn); 861 if (subsystem == NULL) { 862 return NULL; 863 } 864 865 return &req->qpair->group->sgroups[subsystem->id]; 866 } 867 868 int 869 spdk_nvmf_ctrlr_connect(struct spdk_nvmf_request *req) 870 { 871 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 872 struct spdk_nvmf_subsystem_poll_group *sgroup; 873 struct spdk_nvmf_qpair *qpair = req->qpair; 874 enum spdk_nvmf_request_exec_status status; 875 876 if (req->iovcnt > 1) { 877 SPDK_ERRLOG("Connect command invalid iovcnt: %d\n", req->iovcnt); 878 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 879 status = SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 880 goto out; 881 } 882 883 sgroup = nvmf_subsystem_pg_from_connect_cmd(req); 884 if (!sgroup) { 885 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn); 886 status = SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 887 goto out; 888 } 889 890 sgroup->mgmt_io_outstanding++; 891 TAILQ_INSERT_TAIL(&qpair->outstanding, req, link); 892 893 status = _nvmf_ctrlr_connect(req); 894 895 out: 896 if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 897 _nvmf_request_complete(req); 898 } 899 900 return status; 901 } 902 903 static int nvmf_ctrlr_cmd_connect(struct spdk_nvmf_request *req); 904 905 static int 906 retry_connect(void *arg) 907 { 908 struct spdk_nvmf_request *req = arg; 909 struct spdk_nvmf_subsystem_poll_group *sgroup; 910 int rc; 911 912 sgroup = nvmf_subsystem_pg_from_connect_cmd(req); 913 /* subsystem may be deleted during the retry interval, so we need to check sgroup */ 914 if (sgroup != NULL) { 915 sgroup->mgmt_io_outstanding++; 916 } 917 spdk_poller_unregister(&req->poller); 918 rc = nvmf_ctrlr_cmd_connect(req); 919 if (rc == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 920 _nvmf_request_complete(req); 921 } 922 return SPDK_POLLER_BUSY; 923 } 924 925 static int 926 nvmf_ctrlr_cmd_connect(struct spdk_nvmf_request *req) 927 { 928 struct spdk_nvmf_fabric_connect_data *data = req->iov[0].iov_base; 929 struct spdk_nvmf_fabric_connect_rsp *rsp = &req->rsp->connect_rsp; 930 struct spdk_nvmf_transport *transport = req->qpair->transport; 931 struct spdk_nvmf_subsystem *subsystem; 932 933 if (req->length < sizeof(struct spdk_nvmf_fabric_connect_data)) { 934 SPDK_ERRLOG("Connect command data length 0x%x too small\n", req->length); 935 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 936 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 937 } 938 939 if (req->iovcnt > 1) { 940 SPDK_ERRLOG("Connect command invalid iovcnt: %d\n", req->iovcnt); 941 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 942 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 943 } 944 945 subsystem = spdk_nvmf_tgt_find_subsystem(transport->tgt, data->subnqn); 946 if (!subsystem) { 947 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, subnqn); 948 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 949 } 950 951 if ((subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE) || 952 (subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSING) || 953 (subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED) || 954 (subsystem->state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING)) { 955 struct spdk_nvmf_subsystem_poll_group *sgroup; 956 957 if (req->timeout_tsc == 0) { 958 /* We will only retry the request up to 1 second. */ 959 req->timeout_tsc = spdk_get_ticks() + spdk_get_ticks_hz(); 960 } else if (spdk_get_ticks() > req->timeout_tsc) { 961 SPDK_ERRLOG("Subsystem '%s' was not ready for 1 second\n", subsystem->subnqn); 962 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 963 rsp->status.sc = SPDK_NVMF_FABRIC_SC_CONTROLLER_BUSY; 964 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 965 } 966 967 /* Subsystem is not ready to handle a connect. Use a poller to retry it 968 * again later. Decrement the mgmt_io_outstanding to avoid the 969 * subsystem waiting for this command to complete before unpausing. 970 */ 971 sgroup = nvmf_subsystem_pg_from_connect_cmd(req); 972 assert(sgroup != NULL); 973 sgroup->mgmt_io_outstanding--; 974 SPDK_DEBUGLOG(nvmf, "Subsystem '%s' is not ready for connect, retrying...\n", subsystem->subnqn); 975 req->poller = SPDK_POLLER_REGISTER(retry_connect, req, 100); 976 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 977 } 978 979 /* Ensure that hostnqn is null terminated */ 980 if (!memchr(data->hostnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1)) { 981 SPDK_ERRLOG("Connect HOSTNQN is not null terminated\n"); 982 SPDK_NVMF_INVALID_CONNECT_DATA(rsp, hostnqn); 983 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 984 } 985 986 if (!nvmf_qpair_access_allowed(req->qpair, subsystem, data->hostnqn)) { 987 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 988 rsp->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_HOST; 989 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 990 } 991 992 return _nvmf_ctrlr_connect(req); 993 } 994 995 static int 996 nvmf_ctrlr_association_remove(void *ctx) 997 { 998 struct spdk_nvmf_ctrlr *ctrlr = ctx; 999 int rc; 1000 1001 nvmf_ctrlr_stop_association_timer(ctrlr); 1002 1003 if (ctrlr->in_destruct) { 1004 return SPDK_POLLER_IDLE; 1005 } 1006 SPDK_DEBUGLOG(nvmf, "Disconnecting host from subsystem %s due to association timeout.\n", 1007 ctrlr->subsys->subnqn); 1008 1009 if (ctrlr->admin_qpair) { 1010 rc = spdk_nvmf_qpair_disconnect(ctrlr->admin_qpair, NULL, NULL); 1011 if (rc < 0 && rc != -EINPROGRESS) { 1012 SPDK_ERRLOG("Fail to disconnect admin ctrlr qpair\n"); 1013 assert(false); 1014 } 1015 } 1016 1017 return SPDK_POLLER_BUSY; 1018 } 1019 1020 static int 1021 _nvmf_ctrlr_cc_reset_shn_done(void *ctx) 1022 { 1023 struct spdk_nvmf_ctrlr *ctrlr = ctx; 1024 uint64_t now = spdk_get_ticks(); 1025 uint32_t count; 1026 1027 if (ctrlr->cc_timer) { 1028 spdk_poller_unregister(&ctrlr->cc_timer); 1029 } 1030 1031 count = spdk_bit_array_count_set(ctrlr->qpair_mask); 1032 SPDK_DEBUGLOG(nvmf, "ctrlr %p active queue count %u\n", ctrlr, count); 1033 1034 if (count > 1) { 1035 if (now < ctrlr->cc_timeout_tsc) { 1036 /* restart cc timer */ 1037 ctrlr->cc_timer = SPDK_POLLER_REGISTER(_nvmf_ctrlr_cc_reset_shn_done, ctrlr, 100 * 1000); 1038 return SPDK_POLLER_IDLE; 1039 } else { 1040 /* controller fatal status */ 1041 SPDK_WARNLOG("IO timeout, ctrlr %p is in fatal status\n", ctrlr); 1042 nvmf_ctrlr_set_fatal_status(ctrlr); 1043 } 1044 } 1045 1046 spdk_poller_unregister(&ctrlr->cc_timeout_timer); 1047 1048 if (ctrlr->disconnect_is_shn) { 1049 ctrlr->vcprop.csts.bits.shst = SPDK_NVME_SHST_COMPLETE; 1050 ctrlr->disconnect_is_shn = false; 1051 } else { 1052 /* Only a subset of the registers are cleared out on a reset */ 1053 ctrlr->vcprop.cc.raw = 0; 1054 ctrlr->vcprop.csts.raw = 0; 1055 } 1056 1057 /* After CC.EN transitions to 0 (due to shutdown or reset), the association 1058 * between the host and controller shall be preserved for at least 2 minutes */ 1059 if (ctrlr->association_timer) { 1060 SPDK_DEBUGLOG(nvmf, "Association timer already set\n"); 1061 nvmf_ctrlr_stop_association_timer(ctrlr); 1062 } 1063 if (ctrlr->association_timeout) { 1064 ctrlr->association_timer = SPDK_POLLER_REGISTER(nvmf_ctrlr_association_remove, ctrlr, 1065 ctrlr->association_timeout * 1000); 1066 } 1067 ctrlr->disconnect_in_progress = false; 1068 return SPDK_POLLER_BUSY; 1069 } 1070 1071 static void 1072 nvmf_ctrlr_cc_reset_shn_done(struct spdk_io_channel_iter *i, int status) 1073 { 1074 struct spdk_nvmf_ctrlr *ctrlr = spdk_io_channel_iter_get_ctx(i); 1075 1076 if (status < 0) { 1077 SPDK_ERRLOG("Fail to disconnect io ctrlr qpairs\n"); 1078 assert(false); 1079 } 1080 1081 _nvmf_ctrlr_cc_reset_shn_done((void *)ctrlr); 1082 } 1083 1084 static void 1085 nvmf_bdev_complete_reset(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 1086 { 1087 SPDK_NOTICELOG("Resetting bdev done with %s\n", success ? "success" : "failure"); 1088 1089 spdk_bdev_free_io(bdev_io); 1090 } 1091 1092 1093 static int 1094 nvmf_ctrlr_cc_timeout(void *ctx) 1095 { 1096 struct spdk_nvmf_ctrlr *ctrlr = ctx; 1097 struct spdk_nvmf_poll_group *group = ctrlr->admin_qpair->group; 1098 struct spdk_nvmf_ns *ns; 1099 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 1100 1101 assert(group != NULL && group->sgroups != NULL); 1102 spdk_poller_unregister(&ctrlr->cc_timeout_timer); 1103 SPDK_DEBUGLOG(nvmf, "Ctrlr %p reset or shutdown timeout\n", ctrlr); 1104 1105 for (ns = spdk_nvmf_subsystem_get_first_ns(ctrlr->subsys); ns != NULL; 1106 ns = spdk_nvmf_subsystem_get_next_ns(ctrlr->subsys, ns)) { 1107 if (ns->bdev == NULL) { 1108 continue; 1109 } 1110 ns_info = &group->sgroups[ctrlr->subsys->id].ns_info[ns->opts.nsid - 1]; 1111 SPDK_NOTICELOG("Ctrlr %p resetting NSID %u\n", ctrlr, ns->opts.nsid); 1112 spdk_bdev_reset(ns->desc, ns_info->channel, nvmf_bdev_complete_reset, NULL); 1113 } 1114 1115 return SPDK_POLLER_BUSY; 1116 } 1117 1118 const struct spdk_nvmf_registers * 1119 spdk_nvmf_ctrlr_get_regs(struct spdk_nvmf_ctrlr *ctrlr) 1120 { 1121 return &ctrlr->vcprop; 1122 } 1123 1124 void 1125 nvmf_ctrlr_set_fatal_status(struct spdk_nvmf_ctrlr *ctrlr) 1126 { 1127 ctrlr->vcprop.csts.bits.cfs = 1; 1128 } 1129 1130 static uint64_t 1131 nvmf_prop_get_cap(struct spdk_nvmf_ctrlr *ctrlr) 1132 { 1133 return ctrlr->vcprop.cap.raw; 1134 } 1135 1136 static uint64_t 1137 nvmf_prop_get_vs(struct spdk_nvmf_ctrlr *ctrlr) 1138 { 1139 return ctrlr->vcprop.vs.raw; 1140 } 1141 1142 static uint64_t 1143 nvmf_prop_get_cc(struct spdk_nvmf_ctrlr *ctrlr) 1144 { 1145 return ctrlr->vcprop.cc.raw; 1146 } 1147 1148 static bool 1149 nvmf_prop_set_cc(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 1150 { 1151 union spdk_nvme_cc_register cc, diff; 1152 uint32_t cc_timeout_ms; 1153 1154 cc.raw = value; 1155 1156 SPDK_DEBUGLOG(nvmf, "cur CC: 0x%08x\n", ctrlr->vcprop.cc.raw); 1157 SPDK_DEBUGLOG(nvmf, "new CC: 0x%08x\n", cc.raw); 1158 1159 /* 1160 * Calculate which bits changed between the current and new CC. 1161 * Mark each bit as 0 once it is handled to determine if any unhandled bits were changed. 1162 */ 1163 diff.raw = cc.raw ^ ctrlr->vcprop.cc.raw; 1164 1165 if (diff.bits.en) { 1166 if (cc.bits.en) { 1167 SPDK_DEBUGLOG(nvmf, "Property Set CC Enable!\n"); 1168 nvmf_ctrlr_stop_association_timer(ctrlr); 1169 1170 ctrlr->vcprop.cc.bits.en = 1; 1171 ctrlr->vcprop.csts.bits.rdy = 1; 1172 } else { 1173 SPDK_DEBUGLOG(nvmf, "Property Set CC Disable!\n"); 1174 if (ctrlr->disconnect_in_progress) { 1175 SPDK_DEBUGLOG(nvmf, "Disconnect in progress\n"); 1176 return true; 1177 } 1178 1179 ctrlr->cc_timeout_timer = SPDK_POLLER_REGISTER(nvmf_ctrlr_cc_timeout, ctrlr, 1180 NVMF_CC_RESET_SHN_TIMEOUT_IN_MS * 1000); 1181 /* Make sure cc_timeout_ms is between cc_timeout_timer and Host reset/shutdown timeout */ 1182 cc_timeout_ms = (NVMF_CC_RESET_SHN_TIMEOUT_IN_MS + NVMF_CTRLR_RESET_SHN_TIMEOUT_IN_MS) / 2; 1183 ctrlr->cc_timeout_tsc = spdk_get_ticks() + cc_timeout_ms * spdk_get_ticks_hz() / (uint64_t)1000; 1184 1185 ctrlr->vcprop.cc.bits.en = 0; 1186 ctrlr->disconnect_in_progress = true; 1187 ctrlr->disconnect_is_shn = false; 1188 spdk_for_each_channel(ctrlr->subsys->tgt, 1189 nvmf_ctrlr_disconnect_io_qpairs_on_pg, 1190 ctrlr, 1191 nvmf_ctrlr_cc_reset_shn_done); 1192 } 1193 diff.bits.en = 0; 1194 } 1195 1196 if (diff.bits.shn) { 1197 if (cc.bits.shn == SPDK_NVME_SHN_NORMAL || 1198 cc.bits.shn == SPDK_NVME_SHN_ABRUPT) { 1199 SPDK_DEBUGLOG(nvmf, "Property Set CC Shutdown %u%ub!\n", 1200 cc.bits.shn >> 1, cc.bits.shn & 1); 1201 if (ctrlr->disconnect_in_progress) { 1202 SPDK_DEBUGLOG(nvmf, "Disconnect in progress\n"); 1203 return true; 1204 } 1205 1206 ctrlr->cc_timeout_timer = SPDK_POLLER_REGISTER(nvmf_ctrlr_cc_timeout, ctrlr, 1207 NVMF_CC_RESET_SHN_TIMEOUT_IN_MS * 1000); 1208 /* Make sure cc_timeout_ms is between cc_timeout_timer and Host reset/shutdown timeout */ 1209 cc_timeout_ms = (NVMF_CC_RESET_SHN_TIMEOUT_IN_MS + NVMF_CTRLR_RESET_SHN_TIMEOUT_IN_MS) / 2; 1210 ctrlr->cc_timeout_tsc = spdk_get_ticks() + cc_timeout_ms * spdk_get_ticks_hz() / (uint64_t)1000; 1211 1212 ctrlr->vcprop.cc.bits.shn = cc.bits.shn; 1213 ctrlr->disconnect_in_progress = true; 1214 ctrlr->disconnect_is_shn = true; 1215 spdk_for_each_channel(ctrlr->subsys->tgt, 1216 nvmf_ctrlr_disconnect_io_qpairs_on_pg, 1217 ctrlr, 1218 nvmf_ctrlr_cc_reset_shn_done); 1219 1220 /* From the time a shutdown is initiated the controller shall disable 1221 * Keep Alive timer */ 1222 nvmf_ctrlr_stop_keep_alive_timer(ctrlr); 1223 } else if (cc.bits.shn == 0) { 1224 ctrlr->vcprop.cc.bits.shn = 0; 1225 } else { 1226 SPDK_ERRLOG("Prop Set CC: Invalid SHN value %u%ub\n", 1227 cc.bits.shn >> 1, cc.bits.shn & 1); 1228 return false; 1229 } 1230 diff.bits.shn = 0; 1231 } 1232 1233 if (diff.bits.iosqes) { 1234 SPDK_DEBUGLOG(nvmf, "Prop Set IOSQES = %u (%u bytes)\n", 1235 cc.bits.iosqes, 1u << cc.bits.iosqes); 1236 ctrlr->vcprop.cc.bits.iosqes = cc.bits.iosqes; 1237 diff.bits.iosqes = 0; 1238 } 1239 1240 if (diff.bits.iocqes) { 1241 SPDK_DEBUGLOG(nvmf, "Prop Set IOCQES = %u (%u bytes)\n", 1242 cc.bits.iocqes, 1u << cc.bits.iocqes); 1243 ctrlr->vcprop.cc.bits.iocqes = cc.bits.iocqes; 1244 diff.bits.iocqes = 0; 1245 } 1246 1247 if (diff.bits.ams) { 1248 SPDK_ERRLOG("Arbitration Mechanism Selected (AMS) 0x%x not supported!\n", cc.bits.ams); 1249 return false; 1250 } 1251 1252 if (diff.bits.mps) { 1253 SPDK_ERRLOG("Memory Page Size (MPS) %u KiB not supported!\n", (1 << (2 + cc.bits.mps))); 1254 return false; 1255 } 1256 1257 if (diff.bits.css) { 1258 if (cc.bits.css > SPDK_NVME_CC_CSS_IOCS) { 1259 SPDK_ERRLOG("I/O Command Set Selected (CSS) 0x%x not supported!\n", cc.bits.css); 1260 return false; 1261 } 1262 diff.bits.css = 0; 1263 } 1264 1265 if (diff.raw != 0) { 1266 /* Print an error message, but don't fail the command in this case. 1267 * If we did want to fail in this case, we'd need to ensure we acted 1268 * on no other bits or the initiator gets confused. */ 1269 SPDK_ERRLOG("Prop Set CC toggled reserved bits 0x%x!\n", diff.raw); 1270 } 1271 1272 return true; 1273 } 1274 1275 static uint64_t 1276 nvmf_prop_get_csts(struct spdk_nvmf_ctrlr *ctrlr) 1277 { 1278 return ctrlr->vcprop.csts.raw; 1279 } 1280 1281 static uint64_t 1282 nvmf_prop_get_aqa(struct spdk_nvmf_ctrlr *ctrlr) 1283 { 1284 return ctrlr->vcprop.aqa.raw; 1285 } 1286 1287 static bool 1288 nvmf_prop_set_aqa(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 1289 { 1290 union spdk_nvme_aqa_register aqa; 1291 1292 aqa.raw = value; 1293 1294 /* 1295 * We don't need to explicitly check for maximum size, as the fields are 1296 * limited to 12 bits (4096). 1297 */ 1298 if (aqa.bits.asqs < SPDK_NVME_ADMIN_QUEUE_MIN_ENTRIES - 1 || 1299 aqa.bits.acqs < SPDK_NVME_ADMIN_QUEUE_MIN_ENTRIES - 1 || 1300 aqa.bits.reserved1 != 0 || aqa.bits.reserved2 != 0) { 1301 return false; 1302 } 1303 1304 ctrlr->vcprop.aqa.raw = value; 1305 1306 return true; 1307 } 1308 1309 static uint64_t 1310 nvmf_prop_get_asq(struct spdk_nvmf_ctrlr *ctrlr) 1311 { 1312 return ctrlr->vcprop.asq; 1313 } 1314 1315 static bool 1316 nvmf_prop_set_asq_lower(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 1317 { 1318 ctrlr->vcprop.asq = (ctrlr->vcprop.asq & (0xFFFFFFFFULL << 32ULL)) | value; 1319 1320 return true; 1321 } 1322 1323 static bool 1324 nvmf_prop_set_asq_upper(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 1325 { 1326 ctrlr->vcprop.asq = (ctrlr->vcprop.asq & 0xFFFFFFFFULL) | ((uint64_t)value << 32ULL); 1327 1328 return true; 1329 } 1330 1331 static uint64_t 1332 nvmf_prop_get_acq(struct spdk_nvmf_ctrlr *ctrlr) 1333 { 1334 return ctrlr->vcprop.acq; 1335 } 1336 1337 static bool 1338 nvmf_prop_set_acq_lower(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 1339 { 1340 ctrlr->vcprop.acq = (ctrlr->vcprop.acq & (0xFFFFFFFFULL << 32ULL)) | value; 1341 1342 return true; 1343 } 1344 1345 static bool 1346 nvmf_prop_set_acq_upper(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value) 1347 { 1348 ctrlr->vcprop.acq = (ctrlr->vcprop.acq & 0xFFFFFFFFULL) | ((uint64_t)value << 32ULL); 1349 1350 return true; 1351 } 1352 1353 struct nvmf_prop { 1354 uint32_t ofst; 1355 uint8_t size; 1356 char name[11]; 1357 uint64_t (*get_cb)(struct spdk_nvmf_ctrlr *ctrlr); 1358 bool (*set_cb)(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value); 1359 bool (*set_upper_cb)(struct spdk_nvmf_ctrlr *ctrlr, uint32_t value); 1360 }; 1361 1362 #define PROP(field, size, get_cb, set_cb, set_upper_cb) \ 1363 { \ 1364 offsetof(struct spdk_nvme_registers, field), \ 1365 size, \ 1366 #field, \ 1367 get_cb, set_cb, set_upper_cb \ 1368 } 1369 1370 static const struct nvmf_prop nvmf_props[] = { 1371 PROP(cap, 8, nvmf_prop_get_cap, NULL, NULL), 1372 PROP(vs, 4, nvmf_prop_get_vs, NULL, NULL), 1373 PROP(cc, 4, nvmf_prop_get_cc, nvmf_prop_set_cc, NULL), 1374 PROP(csts, 4, nvmf_prop_get_csts, NULL, NULL), 1375 PROP(aqa, 4, nvmf_prop_get_aqa, nvmf_prop_set_aqa, NULL), 1376 PROP(asq, 8, nvmf_prop_get_asq, nvmf_prop_set_asq_lower, nvmf_prop_set_asq_upper), 1377 PROP(acq, 8, nvmf_prop_get_acq, nvmf_prop_set_acq_lower, nvmf_prop_set_acq_upper), 1378 }; 1379 1380 static const struct nvmf_prop * 1381 find_prop(uint32_t ofst, uint8_t size) 1382 { 1383 size_t i; 1384 1385 for (i = 0; i < SPDK_COUNTOF(nvmf_props); i++) { 1386 const struct nvmf_prop *prop = &nvmf_props[i]; 1387 1388 if ((ofst >= prop->ofst) && (ofst + size <= prop->ofst + prop->size)) { 1389 return prop; 1390 } 1391 } 1392 1393 return NULL; 1394 } 1395 1396 static int 1397 nvmf_property_get(struct spdk_nvmf_request *req) 1398 { 1399 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1400 struct spdk_nvmf_fabric_prop_get_cmd *cmd = &req->cmd->prop_get_cmd; 1401 struct spdk_nvmf_fabric_prop_get_rsp *response = &req->rsp->prop_get_rsp; 1402 const struct nvmf_prop *prop; 1403 uint8_t size; 1404 1405 response->status.sc = 0; 1406 response->value.u64 = 0; 1407 1408 SPDK_DEBUGLOG(nvmf, "size %d, offset 0x%x\n", 1409 cmd->attrib.size, cmd->ofst); 1410 1411 switch (cmd->attrib.size) { 1412 case SPDK_NVMF_PROP_SIZE_4: 1413 size = 4; 1414 break; 1415 case SPDK_NVMF_PROP_SIZE_8: 1416 size = 8; 1417 break; 1418 default: 1419 SPDK_DEBUGLOG(nvmf, "Invalid size value %d\n", cmd->attrib.size); 1420 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1421 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 1422 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1423 } 1424 1425 prop = find_prop(cmd->ofst, size); 1426 if (prop == NULL || prop->get_cb == NULL) { 1427 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1428 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 1429 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1430 } 1431 1432 SPDK_DEBUGLOG(nvmf, "name: %s\n", prop->name); 1433 1434 response->value.u64 = prop->get_cb(ctrlr); 1435 1436 if (size != prop->size) { 1437 /* The size must be 4 and the prop->size is 8. Figure out which part of the property to read. */ 1438 assert(size == 4); 1439 assert(prop->size == 8); 1440 1441 if (cmd->ofst == prop->ofst) { 1442 /* Keep bottom 4 bytes only */ 1443 response->value.u64 &= 0xFFFFFFFF; 1444 } else { 1445 /* Keep top 4 bytes only */ 1446 response->value.u64 >>= 32; 1447 } 1448 } 1449 1450 SPDK_DEBUGLOG(nvmf, "response value: 0x%" PRIx64 "\n", response->value.u64); 1451 1452 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1453 } 1454 1455 static int 1456 nvmf_property_set(struct spdk_nvmf_request *req) 1457 { 1458 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1459 struct spdk_nvmf_fabric_prop_set_cmd *cmd = &req->cmd->prop_set_cmd; 1460 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1461 const struct nvmf_prop *prop; 1462 uint64_t value; 1463 uint8_t size; 1464 bool ret; 1465 1466 SPDK_DEBUGLOG(nvmf, "size %d, offset 0x%x, value 0x%" PRIx64 "\n", 1467 cmd->attrib.size, cmd->ofst, cmd->value.u64); 1468 1469 switch (cmd->attrib.size) { 1470 case SPDK_NVMF_PROP_SIZE_4: 1471 size = 4; 1472 break; 1473 case SPDK_NVMF_PROP_SIZE_8: 1474 size = 8; 1475 break; 1476 default: 1477 SPDK_DEBUGLOG(nvmf, "Invalid size value %d\n", cmd->attrib.size); 1478 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1479 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 1480 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1481 } 1482 1483 prop = find_prop(cmd->ofst, size); 1484 if (prop == NULL || prop->set_cb == NULL) { 1485 SPDK_INFOLOG(nvmf, "Invalid offset 0x%x\n", cmd->ofst); 1486 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1487 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 1488 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1489 } 1490 1491 SPDK_DEBUGLOG(nvmf, "name: %s\n", prop->name); 1492 1493 value = cmd->value.u64; 1494 1495 if (prop->size == 4) { 1496 ret = prop->set_cb(ctrlr, (uint32_t)value); 1497 } else if (size != prop->size) { 1498 /* The size must be 4 and the prop->size is 8. Figure out which part of the property to write. */ 1499 assert(size == 4); 1500 assert(prop->size == 8); 1501 1502 if (cmd->ofst == prop->ofst) { 1503 ret = prop->set_cb(ctrlr, (uint32_t)value); 1504 } else { 1505 ret = prop->set_upper_cb(ctrlr, (uint32_t)value); 1506 } 1507 } else { 1508 ret = prop->set_cb(ctrlr, (uint32_t)value); 1509 if (ret) { 1510 ret = prop->set_upper_cb(ctrlr, (uint32_t)(value >> 32)); 1511 } 1512 } 1513 1514 if (!ret) { 1515 SPDK_ERRLOG("prop set_cb failed\n"); 1516 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1517 response->status.sc = SPDK_NVMF_FABRIC_SC_INVALID_PARAM; 1518 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1519 } 1520 1521 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1522 } 1523 1524 static int 1525 nvmf_ctrlr_set_features_arbitration(struct spdk_nvmf_request *req) 1526 { 1527 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1528 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1529 1530 SPDK_DEBUGLOG(nvmf, "Set Features - Arbitration (cdw11 = 0x%0x)\n", cmd->cdw11); 1531 1532 ctrlr->feat.arbitration.raw = cmd->cdw11; 1533 ctrlr->feat.arbitration.bits.reserved = 0; 1534 1535 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1536 } 1537 1538 static int 1539 nvmf_ctrlr_set_features_power_management(struct spdk_nvmf_request *req) 1540 { 1541 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1542 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1543 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1544 1545 SPDK_DEBUGLOG(nvmf, "Set Features - Power Management (cdw11 = 0x%0x)\n", cmd->cdw11); 1546 1547 /* Only PS = 0 is allowed, since we report NPSS = 0 */ 1548 if (cmd->cdw11_bits.feat_power_management.bits.ps != 0) { 1549 SPDK_ERRLOG("Invalid power state %u\n", cmd->cdw11_bits.feat_power_management.bits.ps); 1550 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1551 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1552 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1553 } 1554 1555 ctrlr->feat.power_management.raw = cmd->cdw11; 1556 ctrlr->feat.power_management.bits.reserved = 0; 1557 1558 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1559 } 1560 1561 static bool 1562 temp_threshold_opts_valid(const union spdk_nvme_feat_temperature_threshold *opts) 1563 { 1564 /* 1565 * Valid TMPSEL values: 1566 * 0000b - 1000b: temperature sensors 1567 * 1111b: set all implemented temperature sensors 1568 */ 1569 if (opts->bits.tmpsel >= 9 && opts->bits.tmpsel != 15) { 1570 /* 1001b - 1110b: reserved */ 1571 SPDK_ERRLOG("Invalid TMPSEL %u\n", opts->bits.tmpsel); 1572 return false; 1573 } 1574 1575 /* 1576 * Valid THSEL values: 1577 * 00b: over temperature threshold 1578 * 01b: under temperature threshold 1579 */ 1580 if (opts->bits.thsel > 1) { 1581 /* 10b - 11b: reserved */ 1582 SPDK_ERRLOG("Invalid THSEL %u\n", opts->bits.thsel); 1583 return false; 1584 } 1585 1586 return true; 1587 } 1588 1589 static int 1590 nvmf_ctrlr_set_features_temperature_threshold(struct spdk_nvmf_request *req) 1591 { 1592 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1593 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1594 1595 SPDK_DEBUGLOG(nvmf, "Set Features - Temperature Threshold (cdw11 = 0x%0x)\n", cmd->cdw11); 1596 1597 if (!temp_threshold_opts_valid(&cmd->cdw11_bits.feat_temp_threshold)) { 1598 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1599 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1600 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1601 } 1602 1603 /* TODO: no sensors implemented - ignore new values */ 1604 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1605 } 1606 1607 static int 1608 nvmf_ctrlr_get_features_temperature_threshold(struct spdk_nvmf_request *req) 1609 { 1610 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1611 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1612 1613 SPDK_DEBUGLOG(nvmf, "Get Features - Temperature Threshold (cdw11 = 0x%0x)\n", cmd->cdw11); 1614 1615 if (!temp_threshold_opts_valid(&cmd->cdw11_bits.feat_temp_threshold)) { 1616 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1617 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1618 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1619 } 1620 1621 /* TODO: no sensors implemented - return 0 for all thresholds */ 1622 rsp->cdw0 = 0; 1623 1624 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1625 } 1626 1627 static int 1628 nvmf_ctrlr_get_features_interrupt_vector_configuration(struct spdk_nvmf_request *req) 1629 { 1630 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1631 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1632 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1633 union spdk_nvme_feat_interrupt_vector_configuration iv_conf = {}; 1634 1635 SPDK_DEBUGLOG(nvmf, "Get Features - Interrupt Vector Configuration (cdw11 = 0x%0x)\n", cmd->cdw11); 1636 1637 iv_conf.bits.iv = cmd->cdw11_bits.feat_interrupt_vector_configuration.bits.iv; 1638 iv_conf.bits.cd = ctrlr->feat.interrupt_vector_configuration.bits.cd; 1639 rsp->cdw0 = iv_conf.raw; 1640 1641 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1642 } 1643 1644 static int 1645 nvmf_ctrlr_set_features_error_recovery(struct spdk_nvmf_request *req) 1646 { 1647 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1648 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1649 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1650 1651 SPDK_DEBUGLOG(nvmf, "Set Features - Error Recovery (cdw11 = 0x%0x)\n", cmd->cdw11); 1652 1653 if (cmd->cdw11_bits.feat_error_recovery.bits.dulbe) { 1654 /* 1655 * Host is not allowed to set this bit, since we don't advertise it in 1656 * Identify Namespace. 1657 */ 1658 SPDK_ERRLOG("Host set unsupported DULBE bit\n"); 1659 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 1660 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1661 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1662 } 1663 1664 ctrlr->feat.error_recovery.raw = cmd->cdw11; 1665 ctrlr->feat.error_recovery.bits.reserved = 0; 1666 1667 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1668 } 1669 1670 static int 1671 nvmf_ctrlr_set_features_volatile_write_cache(struct spdk_nvmf_request *req) 1672 { 1673 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1674 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1675 1676 SPDK_DEBUGLOG(nvmf, "Set Features - Volatile Write Cache (cdw11 = 0x%0x)\n", cmd->cdw11); 1677 1678 ctrlr->feat.volatile_write_cache.raw = cmd->cdw11; 1679 ctrlr->feat.volatile_write_cache.bits.reserved = 0; 1680 1681 SPDK_DEBUGLOG(nvmf, "Set Features - Volatile Write Cache %s\n", 1682 ctrlr->feat.volatile_write_cache.bits.wce ? "Enabled" : "Disabled"); 1683 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1684 } 1685 1686 static int 1687 nvmf_ctrlr_set_features_write_atomicity(struct spdk_nvmf_request *req) 1688 { 1689 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1690 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1691 1692 SPDK_DEBUGLOG(nvmf, "Set Features - Write Atomicity (cdw11 = 0x%0x)\n", cmd->cdw11); 1693 1694 ctrlr->feat.write_atomicity.raw = cmd->cdw11; 1695 ctrlr->feat.write_atomicity.bits.reserved = 0; 1696 1697 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1698 } 1699 1700 static int 1701 nvmf_ctrlr_set_features_host_identifier(struct spdk_nvmf_request *req) 1702 { 1703 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1704 1705 SPDK_ERRLOG("Set Features - Host Identifier not allowed\n"); 1706 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 1707 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1708 } 1709 1710 static int 1711 nvmf_ctrlr_get_features_host_identifier(struct spdk_nvmf_request *req) 1712 { 1713 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1714 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1715 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1716 struct spdk_iov_xfer ix; 1717 1718 SPDK_DEBUGLOG(nvmf, "Get Features - Host Identifier\n"); 1719 1720 if (!cmd->cdw11_bits.feat_host_identifier.bits.exhid) { 1721 /* NVMe over Fabrics requires EXHID=1 (128-bit/16-byte host ID) */ 1722 SPDK_ERRLOG("Get Features - Host Identifier with EXHID=0 not allowed\n"); 1723 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1724 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1725 } 1726 1727 if (req->iovcnt < 1 || req->length < sizeof(ctrlr->hostid)) { 1728 SPDK_ERRLOG("Invalid data buffer for Get Features - Host Identifier\n"); 1729 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1730 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1731 } 1732 1733 spdk_iov_xfer_init(&ix, req->iov, req->iovcnt); 1734 spdk_iov_xfer_from_buf(&ix, &ctrlr->hostid, sizeof(ctrlr->hostid)); 1735 1736 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1737 } 1738 1739 static int 1740 nvmf_ctrlr_get_features_reservation_notification_mask(struct spdk_nvmf_request *req) 1741 { 1742 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1743 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1744 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1745 struct spdk_nvmf_ns *ns; 1746 1747 SPDK_DEBUGLOG(nvmf, "get Features - Reservation Notification Mask\n"); 1748 1749 if (cmd->nsid == SPDK_NVME_GLOBAL_NS_TAG) { 1750 SPDK_ERRLOG("get Features - Invalid Namespace ID\n"); 1751 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1752 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1753 } 1754 1755 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 1756 if (ns == NULL) { 1757 SPDK_ERRLOG("Set Features - Invalid Namespace ID\n"); 1758 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1759 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1760 } 1761 rsp->cdw0 = ns->mask; 1762 1763 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1764 } 1765 1766 static int 1767 nvmf_ctrlr_set_features_reservation_notification_mask(struct spdk_nvmf_request *req) 1768 { 1769 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1770 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 1771 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1772 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1773 struct spdk_nvmf_ns *ns; 1774 1775 SPDK_DEBUGLOG(nvmf, "Set Features - Reservation Notification Mask\n"); 1776 1777 if (cmd->nsid == SPDK_NVME_GLOBAL_NS_TAG) { 1778 for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL; 1779 ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) { 1780 ns->mask = cmd->cdw11; 1781 } 1782 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1783 } 1784 1785 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 1786 if (ns == NULL) { 1787 SPDK_ERRLOG("Set Features - Invalid Namespace ID\n"); 1788 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1789 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1790 } 1791 ns->mask = cmd->cdw11; 1792 1793 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1794 } 1795 1796 static int 1797 nvmf_ctrlr_get_features_reservation_persistence(struct spdk_nvmf_request *req) 1798 { 1799 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1800 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1801 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1802 struct spdk_nvmf_ns *ns; 1803 1804 SPDK_DEBUGLOG(nvmf, "Get Features - Reservation Persistence\n"); 1805 1806 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 1807 /* NSID with SPDK_NVME_GLOBAL_NS_TAG (=0xffffffff) also included */ 1808 if (ns == NULL) { 1809 SPDK_ERRLOG("Get Features - Invalid Namespace ID\n"); 1810 response->status.sct = SPDK_NVME_SCT_GENERIC; 1811 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1812 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1813 } 1814 1815 response->cdw0 = ns->ptpl_activated; 1816 1817 response->status.sct = SPDK_NVME_SCT_GENERIC; 1818 response->status.sc = SPDK_NVME_SC_SUCCESS; 1819 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1820 } 1821 1822 static int 1823 nvmf_ctrlr_set_features_reservation_persistence(struct spdk_nvmf_request *req) 1824 { 1825 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1826 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1827 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1828 struct spdk_nvmf_ns *ns; 1829 bool ptpl; 1830 1831 SPDK_DEBUGLOG(nvmf, "Set Features - Reservation Persistence\n"); 1832 1833 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 1834 ptpl = cmd->cdw11_bits.feat_rsv_persistence.bits.ptpl; 1835 1836 if (cmd->nsid != SPDK_NVME_GLOBAL_NS_TAG && ns && ns->ptpl_file) { 1837 ns->ptpl_activated = ptpl; 1838 } else if (cmd->nsid == SPDK_NVME_GLOBAL_NS_TAG) { 1839 for (ns = spdk_nvmf_subsystem_get_first_ns(ctrlr->subsys); ns && ns->ptpl_file; 1840 ns = spdk_nvmf_subsystem_get_next_ns(ctrlr->subsys, ns)) { 1841 ns->ptpl_activated = ptpl; 1842 } 1843 } else { 1844 SPDK_ERRLOG("Set Features - Invalid Namespace ID or Reservation Configuration\n"); 1845 response->status.sct = SPDK_NVME_SCT_GENERIC; 1846 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1847 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1848 } 1849 1850 /* TODO: Feature not changeable for now */ 1851 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 1852 response->status.sc = SPDK_NVME_SC_FEATURE_ID_NOT_SAVEABLE; 1853 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1854 } 1855 1856 static int 1857 nvmf_ctrlr_get_features_host_behavior_support(struct spdk_nvmf_request *req) 1858 { 1859 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1860 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1861 struct spdk_nvme_host_behavior host_behavior = {}; 1862 struct spdk_iov_xfer ix; 1863 1864 SPDK_DEBUGLOG(nvmf, "Get Features - Host Behavior Support\n"); 1865 1866 if (req->iovcnt < 1 || req->length < sizeof(struct spdk_nvme_host_behavior)) { 1867 SPDK_ERRLOG("invalid data buffer for Host Behavior Support\n"); 1868 response->status.sct = SPDK_NVME_SCT_GENERIC; 1869 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1870 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1871 } 1872 1873 host_behavior.acre = ctrlr->acre_enabled; 1874 1875 spdk_iov_xfer_init(&ix, req->iov, req->iovcnt); 1876 spdk_iov_xfer_from_buf(&ix, &host_behavior, sizeof(host_behavior)); 1877 1878 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1879 } 1880 1881 static int 1882 nvmf_ctrlr_set_features_host_behavior_support(struct spdk_nvmf_request *req) 1883 { 1884 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1885 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 1886 struct spdk_nvme_host_behavior *host_behavior; 1887 1888 SPDK_DEBUGLOG(nvmf, "Set Features - Host Behavior Support\n"); 1889 if (req->iovcnt != 1) { 1890 SPDK_ERRLOG("Host Behavior Support invalid iovcnt: %d\n", req->iovcnt); 1891 response->status.sct = SPDK_NVME_SCT_GENERIC; 1892 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1893 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1894 } 1895 if (req->iov[0].iov_len != sizeof(struct spdk_nvme_host_behavior)) { 1896 SPDK_ERRLOG("Host Behavior Support invalid iov_len: %zd\n", req->iov[0].iov_len); 1897 response->status.sct = SPDK_NVME_SCT_GENERIC; 1898 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1899 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1900 } 1901 1902 host_behavior = (struct spdk_nvme_host_behavior *)req->iov[0].iov_base; 1903 if (host_behavior->acre == 0) { 1904 ctrlr->acre_enabled = false; 1905 } else if (host_behavior->acre == 1) { 1906 ctrlr->acre_enabled = true; 1907 } else { 1908 SPDK_ERRLOG("Host Behavior Support invalid acre: 0x%02x\n", host_behavior->acre); 1909 response->status.sct = SPDK_NVME_SCT_GENERIC; 1910 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1911 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1912 } 1913 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1914 } 1915 1916 static int 1917 nvmf_ctrlr_set_features_keep_alive_timer(struct spdk_nvmf_request *req) 1918 { 1919 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1920 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1921 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1922 1923 SPDK_DEBUGLOG(nvmf, "Set Features - Keep Alive Timer (%u ms)\n", cmd->cdw11); 1924 1925 /* 1926 * if attempts to disable keep alive by setting kato to 0h 1927 * a status value of keep alive invalid shall be returned 1928 */ 1929 if (cmd->cdw11_bits.feat_keep_alive_timer.bits.kato == 0) { 1930 rsp->status.sc = SPDK_NVME_SC_KEEP_ALIVE_INVALID; 1931 } else if (cmd->cdw11_bits.feat_keep_alive_timer.bits.kato < MIN_KEEP_ALIVE_TIMEOUT_IN_MS) { 1932 ctrlr->feat.keep_alive_timer.bits.kato = MIN_KEEP_ALIVE_TIMEOUT_IN_MS; 1933 } else { 1934 /* round up to milliseconds */ 1935 ctrlr->feat.keep_alive_timer.bits.kato = spdk_divide_round_up( 1936 cmd->cdw11_bits.feat_keep_alive_timer.bits.kato, 1937 KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS) * 1938 KAS_DEFAULT_VALUE * KAS_TIME_UNIT_IN_MS; 1939 } 1940 1941 /* 1942 * if change the keep alive timeout value successfully 1943 * update the keep alive poller. 1944 */ 1945 if (cmd->cdw11_bits.feat_keep_alive_timer.bits.kato != 0) { 1946 if (ctrlr->keep_alive_poller != NULL) { 1947 spdk_poller_unregister(&ctrlr->keep_alive_poller); 1948 } 1949 ctrlr->keep_alive_poller = SPDK_POLLER_REGISTER(nvmf_ctrlr_keep_alive_poll, ctrlr, 1950 ctrlr->feat.keep_alive_timer.bits.kato * 1000); 1951 } 1952 1953 SPDK_DEBUGLOG(nvmf, "Set Features - Keep Alive Timer set to %u ms\n", 1954 ctrlr->feat.keep_alive_timer.bits.kato); 1955 1956 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1957 } 1958 1959 static int 1960 nvmf_ctrlr_set_features_number_of_queues(struct spdk_nvmf_request *req) 1961 { 1962 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 1963 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1964 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 1965 uint32_t count; 1966 1967 SPDK_DEBUGLOG(nvmf, "Set Features - Number of Queues, cdw11 0x%x\n", 1968 req->cmd->nvme_cmd.cdw11); 1969 1970 if (cmd->cdw11_bits.feat_num_of_queues.bits.ncqr == UINT16_MAX || 1971 cmd->cdw11_bits.feat_num_of_queues.bits.nsqr == UINT16_MAX) { 1972 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 1973 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1974 } 1975 1976 count = spdk_bit_array_count_set(ctrlr->qpair_mask); 1977 /* verify that the controller is ready to process commands */ 1978 if (count > 1) { 1979 SPDK_DEBUGLOG(nvmf, "Queue pairs already active!\n"); 1980 rsp->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 1981 } else { 1982 /* 1983 * Ignore the value requested by the host - 1984 * always return the pre-configured value based on max_qpairs_allowed. 1985 */ 1986 rsp->cdw0 = ctrlr->feat.number_of_queues.raw; 1987 } 1988 1989 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 1990 } 1991 1992 SPDK_STATIC_ASSERT(sizeof(struct spdk_nvmf_ctrlr) == 4920, 1993 "Please check migration fields that need to be added or not"); 1994 1995 static void 1996 nvmf_ctrlr_migr_data_copy(struct spdk_nvmf_ctrlr_migr_data *data, 1997 const struct spdk_nvmf_ctrlr_migr_data *data_src, size_t data_size) 1998 { 1999 assert(data); 2000 assert(data_src); 2001 assert(data_size); 2002 2003 memcpy(&data->regs, &data_src->regs, spdk_min(data->regs_size, data_src->regs_size)); 2004 memcpy(&data->feat, &data_src->feat, spdk_min(data->feat_size, data_src->feat_size)); 2005 2006 #define SET_FIELD(field) \ 2007 if (offsetof(struct spdk_nvmf_ctrlr_migr_data, field) + sizeof(data->field) <= data_size) { \ 2008 data->field = data_src->field; \ 2009 } \ 2010 2011 SET_FIELD(cntlid); 2012 SET_FIELD(acre); 2013 SET_FIELD(num_aer_cids); 2014 SET_FIELD(num_async_events); 2015 SET_FIELD(notice_aen_mask); 2016 #undef SET_FIELD 2017 2018 #define SET_ARRAY(arr) \ 2019 if (offsetof(struct spdk_nvmf_ctrlr_migr_data, arr) + sizeof(data->arr) <= data_size) { \ 2020 memcpy(&data->arr, &data_src->arr, sizeof(data->arr)); \ 2021 } \ 2022 2023 SET_ARRAY(async_events); 2024 SET_ARRAY(aer_cids); 2025 #undef SET_ARRAY 2026 } 2027 2028 int 2029 spdk_nvmf_ctrlr_save_migr_data(struct spdk_nvmf_ctrlr *ctrlr, 2030 struct spdk_nvmf_ctrlr_migr_data *data) 2031 { 2032 struct spdk_nvmf_async_event_completion *event, *event_tmp; 2033 uint32_t i; 2034 struct spdk_nvmf_ctrlr_migr_data data_local = { 2035 .data_size = offsetof(struct spdk_nvmf_ctrlr_migr_data, unused), 2036 .regs_size = sizeof(struct spdk_nvmf_registers), 2037 .feat_size = sizeof(struct spdk_nvmf_ctrlr_feat) 2038 }; 2039 2040 assert(data->data_size <= sizeof(data_local)); 2041 assert(spdk_get_thread() == ctrlr->thread); 2042 2043 memcpy(&data_local.regs, &ctrlr->vcprop, sizeof(struct spdk_nvmf_registers)); 2044 memcpy(&data_local.feat, &ctrlr->feat, sizeof(struct spdk_nvmf_ctrlr_feat)); 2045 2046 data_local.cntlid = ctrlr->cntlid; 2047 data_local.acre = ctrlr->acre_enabled; 2048 data_local.num_aer_cids = ctrlr->nr_aer_reqs; 2049 2050 STAILQ_FOREACH_SAFE(event, &ctrlr->async_events, link, event_tmp) { 2051 if (data_local.num_async_events + 1 > SPDK_NVMF_MIGR_MAX_PENDING_AERS) { 2052 SPDK_ERRLOG("ctrlr %p has too many pending AERs\n", ctrlr); 2053 break; 2054 } 2055 2056 data_local.async_events[data_local.num_async_events++].raw = event->event.raw; 2057 } 2058 2059 for (i = 0; i < ctrlr->nr_aer_reqs; i++) { 2060 struct spdk_nvmf_request *req = ctrlr->aer_req[i]; 2061 data_local.aer_cids[i] = req->cmd->nvme_cmd.cid; 2062 } 2063 data_local.notice_aen_mask = ctrlr->notice_aen_mask; 2064 2065 nvmf_ctrlr_migr_data_copy(data, &data_local, spdk_min(data->data_size, data_local.data_size)); 2066 return 0; 2067 } 2068 2069 int 2070 spdk_nvmf_ctrlr_restore_migr_data(struct spdk_nvmf_ctrlr *ctrlr, 2071 const struct spdk_nvmf_ctrlr_migr_data *data) 2072 { 2073 uint32_t i; 2074 struct spdk_nvmf_ctrlr_migr_data data_local = { 2075 .data_size = offsetof(struct spdk_nvmf_ctrlr_migr_data, unused), 2076 .regs_size = sizeof(struct spdk_nvmf_registers), 2077 .feat_size = sizeof(struct spdk_nvmf_ctrlr_feat) 2078 }; 2079 2080 assert(data->data_size <= sizeof(data_local)); 2081 assert(spdk_get_thread() == ctrlr->thread); 2082 2083 /* local version of data should have defaults set before copy */ 2084 nvmf_ctrlr_migr_data_copy(&data_local, data, spdk_min(data->data_size, data_local.data_size)); 2085 memcpy(&ctrlr->vcprop, &data_local.regs, sizeof(struct spdk_nvmf_registers)); 2086 memcpy(&ctrlr->feat, &data_local.feat, sizeof(struct spdk_nvmf_ctrlr_feat)); 2087 2088 ctrlr->cntlid = data_local.cntlid; 2089 ctrlr->acre_enabled = data_local.acre; 2090 2091 for (i = 0; i < data_local.num_async_events; i++) { 2092 struct spdk_nvmf_async_event_completion *event; 2093 2094 event = calloc(1, sizeof(*event)); 2095 if (!event) { 2096 return -ENOMEM; 2097 } 2098 2099 event->event.raw = data_local.async_events[i].raw; 2100 STAILQ_INSERT_TAIL(&ctrlr->async_events, event, link); 2101 } 2102 ctrlr->notice_aen_mask = data_local.notice_aen_mask; 2103 2104 return 0; 2105 } 2106 2107 static int 2108 nvmf_ctrlr_set_features_async_event_configuration(struct spdk_nvmf_request *req) 2109 { 2110 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2111 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2112 2113 SPDK_DEBUGLOG(nvmf, "Set Features - Async Event Configuration, cdw11 0x%08x\n", 2114 cmd->cdw11); 2115 ctrlr->feat.async_event_configuration.raw = cmd->cdw11; 2116 ctrlr->feat.async_event_configuration.bits.reserved1 = 0; 2117 ctrlr->feat.async_event_configuration.bits.reserved2 = 0; 2118 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2119 } 2120 2121 static int 2122 nvmf_ctrlr_async_event_request(struct spdk_nvmf_request *req) 2123 { 2124 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2125 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 2126 struct spdk_nvmf_async_event_completion *pending_event; 2127 2128 SPDK_DEBUGLOG(nvmf, "Async Event Request\n"); 2129 2130 /* Four asynchronous events are supported for now */ 2131 if (ctrlr->nr_aer_reqs >= SPDK_NVMF_MAX_ASYNC_EVENTS) { 2132 SPDK_DEBUGLOG(nvmf, "AERL exceeded\n"); 2133 rsp->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 2134 rsp->status.sc = SPDK_NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED; 2135 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2136 } 2137 2138 if (!STAILQ_EMPTY(&ctrlr->async_events)) { 2139 pending_event = STAILQ_FIRST(&ctrlr->async_events); 2140 rsp->cdw0 = pending_event->event.raw; 2141 STAILQ_REMOVE(&ctrlr->async_events, pending_event, spdk_nvmf_async_event_completion, link); 2142 free(pending_event); 2143 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2144 } 2145 2146 ctrlr->aer_req[ctrlr->nr_aer_reqs++] = req; 2147 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 2148 } 2149 2150 static void 2151 nvmf_get_firmware_slot_log_page(struct iovec *iovs, int iovcnt, uint64_t offset, uint32_t length) 2152 { 2153 struct spdk_nvme_firmware_page fw_page; 2154 size_t copy_len; 2155 struct spdk_iov_xfer ix; 2156 2157 spdk_iov_xfer_init(&ix, iovs, iovcnt); 2158 2159 memset(&fw_page, 0, sizeof(fw_page)); 2160 fw_page.afi.active_slot = 1; 2161 fw_page.afi.next_reset_slot = 0; 2162 spdk_strcpy_pad(fw_page.revision[0], FW_VERSION, sizeof(fw_page.revision[0]), ' '); 2163 2164 if (offset < sizeof(fw_page)) { 2165 copy_len = spdk_min(sizeof(fw_page) - offset, length); 2166 if (copy_len > 0) { 2167 spdk_iov_xfer_from_buf(&ix, (const char *)&fw_page + offset, copy_len); 2168 } 2169 } 2170 } 2171 2172 /* 2173 * Asynchronous Event Mask Bit 2174 */ 2175 enum spdk_nvme_async_event_mask_bit { 2176 /* Mask Namespace Change Notification */ 2177 SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGE_MASK_BIT = 0, 2178 /* Mask Asymmetric Namespace Access Change Notification */ 2179 SPDK_NVME_ASYNC_EVENT_ANA_CHANGE_MASK_BIT = 1, 2180 /* Mask Discovery Log Change Notification */ 2181 SPDK_NVME_ASYNC_EVENT_DISCOVERY_LOG_CHANGE_MASK_BIT = 2, 2182 /* Mask Reservation Log Page Available Notification */ 2183 SPDK_NVME_ASYNC_EVENT_RESERVATION_LOG_AVAIL_MASK_BIT = 3, 2184 /* Mask Error Event */ 2185 SPDK_NVME_ASYNC_EVENT_ERROR_MASK_BIT = 4, 2186 /* 4 - 63 Reserved */ 2187 }; 2188 2189 static inline void 2190 nvmf_ctrlr_unmask_aen(struct spdk_nvmf_ctrlr *ctrlr, 2191 enum spdk_nvme_async_event_mask_bit mask) 2192 { 2193 ctrlr->notice_aen_mask &= ~(1 << mask); 2194 } 2195 2196 static inline bool 2197 nvmf_ctrlr_mask_aen(struct spdk_nvmf_ctrlr *ctrlr, 2198 enum spdk_nvme_async_event_mask_bit mask) 2199 { 2200 if (ctrlr->notice_aen_mask & (1 << mask)) { 2201 return false; 2202 } else { 2203 ctrlr->notice_aen_mask |= (1 << mask); 2204 return true; 2205 } 2206 } 2207 2208 /* we have to use the typedef in the function declaration to appease astyle. */ 2209 typedef enum spdk_nvme_ana_state spdk_nvme_ana_state_t; 2210 2211 static inline spdk_nvme_ana_state_t 2212 nvmf_ctrlr_get_ana_state(struct spdk_nvmf_ctrlr *ctrlr, uint32_t anagrpid) 2213 { 2214 if (!ctrlr->subsys->flags.ana_reporting) { 2215 return SPDK_NVME_ANA_OPTIMIZED_STATE; 2216 } 2217 2218 if (spdk_unlikely(ctrlr->listener == NULL)) { 2219 return SPDK_NVME_ANA_INACCESSIBLE_STATE; 2220 } 2221 2222 assert(anagrpid - 1 < ctrlr->subsys->max_nsid); 2223 return ctrlr->listener->ana_state[anagrpid - 1]; 2224 } 2225 2226 static spdk_nvme_ana_state_t 2227 nvmf_ctrlr_get_ana_state_from_nsid(struct spdk_nvmf_ctrlr *ctrlr, uint32_t nsid) 2228 { 2229 struct spdk_nvmf_ns *ns; 2230 2231 /* We do not have NVM subsystem specific ANA state. Hence if NSID is either 2232 * SPDK_NVMF_GLOBAL_NS_TAG, invalid, or for inactive namespace, return 2233 * the optimized state. 2234 */ 2235 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid); 2236 if (ns == NULL) { 2237 return SPDK_NVME_ANA_OPTIMIZED_STATE; 2238 } 2239 2240 return nvmf_ctrlr_get_ana_state(ctrlr, ns->anagrpid); 2241 } 2242 2243 static void 2244 nvmf_get_error_log_page(struct spdk_nvmf_ctrlr *ctrlr, struct iovec *iovs, int iovcnt, 2245 uint64_t offset, uint32_t length, uint32_t rae) 2246 { 2247 if (!rae) { 2248 nvmf_ctrlr_unmask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_ERROR_MASK_BIT); 2249 } 2250 2251 /* TODO: actually fill out log page data */ 2252 } 2253 2254 static void 2255 nvmf_get_ana_log_page(struct spdk_nvmf_ctrlr *ctrlr, struct iovec *iovs, int iovcnt, 2256 uint64_t offset, uint32_t length, uint32_t rae) 2257 { 2258 struct spdk_nvme_ana_page ana_hdr; 2259 struct spdk_nvme_ana_group_descriptor ana_desc; 2260 size_t copy_len, copied_len; 2261 uint32_t num_anagrp = 0, anagrpid; 2262 struct spdk_nvmf_ns *ns; 2263 struct spdk_iov_xfer ix; 2264 2265 spdk_iov_xfer_init(&ix, iovs, iovcnt); 2266 2267 if (length == 0) { 2268 goto done; 2269 } 2270 2271 if (offset >= sizeof(ana_hdr)) { 2272 offset -= sizeof(ana_hdr); 2273 } else { 2274 for (anagrpid = 1; anagrpid <= ctrlr->subsys->max_nsid; anagrpid++) { 2275 if (ctrlr->subsys->ana_group[anagrpid - 1] > 0) { 2276 num_anagrp++; 2277 } 2278 } 2279 2280 memset(&ana_hdr, 0, sizeof(ana_hdr)); 2281 2282 ana_hdr.num_ana_group_desc = num_anagrp; 2283 /* TODO: Support Change Count. */ 2284 ana_hdr.change_count = 0; 2285 2286 copy_len = spdk_min(sizeof(ana_hdr) - offset, length); 2287 copied_len = spdk_iov_xfer_from_buf(&ix, (const char *)&ana_hdr + offset, copy_len); 2288 assert(copied_len == copy_len); 2289 length -= copied_len; 2290 offset = 0; 2291 } 2292 2293 if (length == 0) { 2294 goto done; 2295 } 2296 2297 for (anagrpid = 1; anagrpid <= ctrlr->subsys->max_nsid; anagrpid++) { 2298 if (ctrlr->subsys->ana_group[anagrpid - 1] == 0) { 2299 continue; 2300 } 2301 2302 if (offset >= sizeof(ana_desc)) { 2303 offset -= sizeof(ana_desc); 2304 } else { 2305 memset(&ana_desc, 0, sizeof(ana_desc)); 2306 2307 ana_desc.ana_group_id = anagrpid; 2308 ana_desc.num_of_nsid = ctrlr->subsys->ana_group[anagrpid - 1]; 2309 ana_desc.ana_state = nvmf_ctrlr_get_ana_state(ctrlr, anagrpid); 2310 2311 copy_len = spdk_min(sizeof(ana_desc) - offset, length); 2312 copied_len = spdk_iov_xfer_from_buf(&ix, (const char *)&ana_desc + offset, 2313 copy_len); 2314 assert(copied_len == copy_len); 2315 length -= copied_len; 2316 offset = 0; 2317 2318 if (length == 0) { 2319 goto done; 2320 } 2321 } 2322 2323 /* TODO: Revisit here about O(n^2) cost if we have subsystem with 2324 * many namespaces in the future. 2325 */ 2326 for (ns = spdk_nvmf_subsystem_get_first_ns(ctrlr->subsys); ns != NULL; 2327 ns = spdk_nvmf_subsystem_get_next_ns(ctrlr->subsys, ns)) { 2328 if (ns->anagrpid != anagrpid) { 2329 continue; 2330 } 2331 2332 if (offset >= sizeof(uint32_t)) { 2333 offset -= sizeof(uint32_t); 2334 continue; 2335 } 2336 2337 copy_len = spdk_min(sizeof(uint32_t) - offset, length); 2338 copied_len = spdk_iov_xfer_from_buf(&ix, (const char *)&ns->nsid + offset, 2339 copy_len); 2340 assert(copied_len == copy_len); 2341 length -= copied_len; 2342 offset = 0; 2343 2344 if (length == 0) { 2345 goto done; 2346 } 2347 } 2348 } 2349 2350 done: 2351 if (!rae) { 2352 nvmf_ctrlr_unmask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_ANA_CHANGE_MASK_BIT); 2353 } 2354 } 2355 2356 void 2357 nvmf_ctrlr_ns_changed(struct spdk_nvmf_ctrlr *ctrlr, uint32_t nsid) 2358 { 2359 uint16_t max_changes = SPDK_COUNTOF(ctrlr->changed_ns_list.ns_list); 2360 uint16_t i; 2361 bool found = false; 2362 2363 for (i = 0; i < ctrlr->changed_ns_list_count; i++) { 2364 if (ctrlr->changed_ns_list.ns_list[i] == nsid) { 2365 /* nsid is already in the list */ 2366 found = true; 2367 break; 2368 } 2369 } 2370 2371 if (!found) { 2372 if (ctrlr->changed_ns_list_count == max_changes) { 2373 /* Out of space - set first entry to FFFFFFFFh and zero-fill the rest. */ 2374 ctrlr->changed_ns_list.ns_list[0] = 0xFFFFFFFFu; 2375 for (i = 1; i < max_changes; i++) { 2376 ctrlr->changed_ns_list.ns_list[i] = 0; 2377 } 2378 } else { 2379 ctrlr->changed_ns_list.ns_list[ctrlr->changed_ns_list_count++] = nsid; 2380 } 2381 } 2382 } 2383 2384 static void 2385 nvmf_get_changed_ns_list_log_page(struct spdk_nvmf_ctrlr *ctrlr, 2386 struct iovec *iovs, int iovcnt, uint64_t offset, uint32_t length, uint32_t rae) 2387 { 2388 size_t copy_length; 2389 struct spdk_iov_xfer ix; 2390 2391 spdk_iov_xfer_init(&ix, iovs, iovcnt); 2392 2393 if (offset < sizeof(ctrlr->changed_ns_list)) { 2394 copy_length = spdk_min(length, sizeof(ctrlr->changed_ns_list) - offset); 2395 if (copy_length) { 2396 spdk_iov_xfer_from_buf(&ix, (char *)&ctrlr->changed_ns_list + offset, copy_length); 2397 } 2398 } 2399 2400 /* Clear log page each time it is read */ 2401 ctrlr->changed_ns_list_count = 0; 2402 memset(&ctrlr->changed_ns_list, 0, sizeof(ctrlr->changed_ns_list)); 2403 2404 if (!rae) { 2405 nvmf_ctrlr_unmask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGE_MASK_BIT); 2406 } 2407 } 2408 2409 /* The structure can be modified if we provide support for other commands in future */ 2410 static const struct spdk_nvme_cmds_and_effect_log_page g_cmds_and_effect_log_page = { 2411 .admin_cmds_supported = { 2412 /* CSUPP, LBCC, NCC, NIC, CCC, CSE */ 2413 /* Get Log Page */ 2414 [SPDK_NVME_OPC_GET_LOG_PAGE] = {1, 0, 0, 0, 0, 0, 0, 0}, 2415 /* Identify */ 2416 [SPDK_NVME_OPC_IDENTIFY] = {1, 0, 0, 0, 0, 0, 0, 0}, 2417 /* Abort */ 2418 [SPDK_NVME_OPC_ABORT] = {1, 0, 0, 0, 0, 0, 0, 0}, 2419 /* Set Features */ 2420 [SPDK_NVME_OPC_SET_FEATURES] = {1, 0, 0, 0, 0, 0, 0, 0}, 2421 /* Get Features */ 2422 [SPDK_NVME_OPC_GET_FEATURES] = {1, 0, 0, 0, 0, 0, 0, 0}, 2423 /* Async Event Request */ 2424 [SPDK_NVME_OPC_ASYNC_EVENT_REQUEST] = {1, 0, 0, 0, 0, 0, 0, 0}, 2425 /* Keep Alive */ 2426 [SPDK_NVME_OPC_KEEP_ALIVE] = {1, 0, 0, 0, 0, 0, 0, 0}, 2427 }, 2428 .io_cmds_supported = { 2429 /* FLUSH */ 2430 [SPDK_NVME_OPC_FLUSH] = {1, 1, 0, 0, 0, 0, 0, 0}, 2431 /* WRITE */ 2432 [SPDK_NVME_OPC_WRITE] = {1, 1, 0, 0, 0, 0, 0, 0}, 2433 /* READ */ 2434 [SPDK_NVME_OPC_READ] = {1, 0, 0, 0, 0, 0, 0, 0}, 2435 /* WRITE ZEROES */ 2436 [SPDK_NVME_OPC_WRITE_ZEROES] = {1, 1, 0, 0, 0, 0, 0, 0}, 2437 /* DATASET MANAGEMENT */ 2438 [SPDK_NVME_OPC_DATASET_MANAGEMENT] = {1, 1, 0, 0, 0, 0, 0, 0}, 2439 /* COMPARE */ 2440 [SPDK_NVME_OPC_COMPARE] = {1, 0, 0, 0, 0, 0, 0, 0}, 2441 /* ZONE MANAGEMENT SEND */ 2442 [SPDK_NVME_OPC_ZONE_MGMT_SEND] = {1, 1, 0, 0, 0, 0, 0, 0}, 2443 /* ZONE MANAGEMENT RECEIVE */ 2444 [SPDK_NVME_OPC_ZONE_MGMT_RECV] = {1, 0, 0, 0, 0, 0, 0, 0}, 2445 /* COPY */ 2446 [SPDK_NVME_OPC_COPY] = {1, 1, 0, 0, 0, 0, 0, 0}, 2447 }, 2448 }; 2449 2450 static void 2451 nvmf_get_cmds_and_effects_log_page(struct spdk_nvmf_ctrlr *ctrlr, struct iovec *iovs, int iovcnt, 2452 uint64_t offset, uint32_t length) 2453 { 2454 uint32_t page_size = sizeof(struct spdk_nvme_cmds_and_effect_log_page); 2455 size_t copy_len = 0; 2456 struct spdk_nvme_cmds_and_effect_log_page cmds_and_effect_log_page = g_cmds_and_effect_log_page; 2457 struct spdk_nvme_cmds_and_effect_entry csupp_and_lbcc_effect_entry = {1, 1, 0, 0, 0, 0, 0, 0}; 2458 struct spdk_iov_xfer ix; 2459 2460 spdk_iov_xfer_init(&ix, iovs, iovcnt); 2461 2462 if (offset < page_size) { 2463 if (ctrlr->subsys->zone_append_supported) { 2464 cmds_and_effect_log_page.io_cmds_supported[SPDK_NVME_OPC_ZONE_APPEND] = 2465 csupp_and_lbcc_effect_entry; 2466 } 2467 copy_len = spdk_min(page_size - offset, length); 2468 spdk_iov_xfer_from_buf(&ix, (char *)(&cmds_and_effect_log_page) + offset, copy_len); 2469 } 2470 } 2471 2472 static void 2473 nvmf_get_reservation_notification_log_page(struct spdk_nvmf_ctrlr *ctrlr, 2474 struct iovec *iovs, int iovcnt, uint64_t offset, uint32_t length, uint32_t rae) 2475 { 2476 uint32_t unit_log_len, avail_log_len, next_pos, copy_len; 2477 struct spdk_nvmf_reservation_log *log, *log_tmp; 2478 struct spdk_iov_xfer ix; 2479 2480 spdk_iov_xfer_init(&ix, iovs, iovcnt); 2481 2482 unit_log_len = sizeof(struct spdk_nvme_reservation_notification_log); 2483 /* No available log, return zeroed log pages */ 2484 if (!ctrlr->num_avail_log_pages) { 2485 return; 2486 } 2487 2488 avail_log_len = ctrlr->num_avail_log_pages * unit_log_len; 2489 if (offset >= avail_log_len) { 2490 return; 2491 } 2492 2493 next_pos = 0; 2494 TAILQ_FOREACH_SAFE(log, &ctrlr->log_head, link, log_tmp) { 2495 TAILQ_REMOVE(&ctrlr->log_head, log, link); 2496 ctrlr->num_avail_log_pages--; 2497 2498 next_pos += unit_log_len; 2499 if (next_pos > offset) { 2500 copy_len = spdk_min(next_pos - offset, length); 2501 spdk_iov_xfer_from_buf(&ix, &log->log, copy_len); 2502 length -= copy_len; 2503 offset += copy_len; 2504 } 2505 free(log); 2506 2507 if (length == 0) { 2508 break; 2509 } 2510 } 2511 2512 if (!rae) { 2513 nvmf_ctrlr_unmask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_RESERVATION_LOG_AVAIL_MASK_BIT); 2514 } 2515 return; 2516 } 2517 2518 static int 2519 nvmf_ctrlr_get_log_page(struct spdk_nvmf_request *req) 2520 { 2521 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2522 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 2523 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2524 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 2525 struct spdk_nvme_transport_id cmd_source_trid; 2526 uint64_t offset, len; 2527 uint32_t rae, numdl, numdu; 2528 uint8_t lid; 2529 2530 if (req->iovcnt < 1) { 2531 SPDK_DEBUGLOG(nvmf, "get log command with no buffer\n"); 2532 response->status.sct = SPDK_NVME_SCT_GENERIC; 2533 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2534 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2535 } 2536 2537 offset = (uint64_t)cmd->cdw12 | ((uint64_t)cmd->cdw13 << 32); 2538 if (offset & 3) { 2539 SPDK_ERRLOG("Invalid log page offset 0x%" PRIx64 "\n", offset); 2540 response->status.sct = SPDK_NVME_SCT_GENERIC; 2541 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2542 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2543 } 2544 2545 rae = cmd->cdw10_bits.get_log_page.rae; 2546 numdl = cmd->cdw10_bits.get_log_page.numdl; 2547 numdu = cmd->cdw11_bits.get_log_page.numdu; 2548 len = ((numdu << 16) + numdl + (uint64_t)1) * 4; 2549 if (len > req->length) { 2550 SPDK_ERRLOG("Get log page: len (%" PRIu64 ") > buf size (%u)\n", 2551 len, req->length); 2552 response->status.sct = SPDK_NVME_SCT_GENERIC; 2553 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2554 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2555 } 2556 2557 lid = cmd->cdw10_bits.get_log_page.lid; 2558 SPDK_DEBUGLOG(nvmf, "Get log page: LID=0x%02X offset=0x%" PRIx64 " len=0x%" PRIx64 " rae=%u\n", 2559 lid, offset, len, rae); 2560 2561 if (spdk_nvmf_subsystem_is_discovery(subsystem)) { 2562 switch (lid) { 2563 case SPDK_NVME_LOG_DISCOVERY: 2564 if (spdk_nvmf_qpair_get_listen_trid(req->qpair, &cmd_source_trid)) { 2565 SPDK_ERRLOG("Failed to get LOG_DISCOVERY source trid\n"); 2566 response->status.sct = SPDK_NVME_SCT_GENERIC; 2567 response->status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 2568 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2569 } 2570 nvmf_get_discovery_log_page(subsystem->tgt, ctrlr->hostnqn, req->iov, req->iovcnt, 2571 offset, len, &cmd_source_trid); 2572 if (!rae) { 2573 nvmf_ctrlr_unmask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_DISCOVERY_LOG_CHANGE_MASK_BIT); 2574 } 2575 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2576 default: 2577 goto invalid_log_page; 2578 } 2579 } else { 2580 if (offset > len) { 2581 SPDK_ERRLOG("Get log page: offset (%" PRIu64 ") > len (%" PRIu64 ")\n", 2582 offset, len); 2583 response->status.sct = SPDK_NVME_SCT_GENERIC; 2584 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2585 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2586 } 2587 2588 switch (lid) { 2589 case SPDK_NVME_LOG_ERROR: 2590 nvmf_get_error_log_page(ctrlr, req->iov, req->iovcnt, offset, len, rae); 2591 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2592 case SPDK_NVME_LOG_HEALTH_INFORMATION: 2593 /* TODO: actually fill out log page data */ 2594 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2595 case SPDK_NVME_LOG_FIRMWARE_SLOT: 2596 nvmf_get_firmware_slot_log_page(req->iov, req->iovcnt, offset, len); 2597 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2598 case SPDK_NVME_LOG_ASYMMETRIC_NAMESPACE_ACCESS: 2599 if (subsystem->flags.ana_reporting) { 2600 nvmf_get_ana_log_page(ctrlr, req->iov, req->iovcnt, offset, len, rae); 2601 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2602 } else { 2603 goto invalid_log_page; 2604 } 2605 case SPDK_NVME_LOG_COMMAND_EFFECTS_LOG: 2606 nvmf_get_cmds_and_effects_log_page(ctrlr, req->iov, req->iovcnt, offset, len); 2607 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2608 case SPDK_NVME_LOG_CHANGED_NS_LIST: 2609 nvmf_get_changed_ns_list_log_page(ctrlr, req->iov, req->iovcnt, offset, len, rae); 2610 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2611 case SPDK_NVME_LOG_RESERVATION_NOTIFICATION: 2612 nvmf_get_reservation_notification_log_page(ctrlr, req->iov, req->iovcnt, offset, len, rae); 2613 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2614 default: 2615 goto invalid_log_page; 2616 } 2617 } 2618 2619 invalid_log_page: 2620 SPDK_INFOLOG(nvmf, "Unsupported Get Log Page 0x%02X\n", lid); 2621 response->status.sct = SPDK_NVME_SCT_GENERIC; 2622 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 2623 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2624 } 2625 2626 static struct spdk_nvmf_ns * 2627 _nvmf_subsystem_get_ns_safe(struct spdk_nvmf_subsystem *subsystem, 2628 uint32_t nsid, 2629 struct spdk_nvme_cpl *rsp) 2630 { 2631 struct spdk_nvmf_ns *ns; 2632 if (nsid == 0 || nsid > subsystem->max_nsid) { 2633 SPDK_ERRLOG("Identify Namespace for invalid NSID %u\n", nsid); 2634 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2635 rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 2636 return NULL; 2637 } 2638 2639 ns = _nvmf_subsystem_get_ns(subsystem, nsid); 2640 if (ns == NULL || ns->bdev == NULL) { 2641 /* 2642 * Inactive namespaces should return a zero filled data structure. 2643 * The data buffer is already zeroed by nvmf_ctrlr_process_admin_cmd(), 2644 * so we can just return early here. 2645 */ 2646 SPDK_DEBUGLOG(nvmf, "Identify Namespace for inactive NSID %u\n", nsid); 2647 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2648 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 2649 return NULL; 2650 } 2651 return ns; 2652 } 2653 2654 int 2655 spdk_nvmf_ctrlr_identify_ns(struct spdk_nvmf_ctrlr *ctrlr, 2656 struct spdk_nvme_cmd *cmd, 2657 struct spdk_nvme_cpl *rsp, 2658 struct spdk_nvme_ns_data *nsdata) 2659 { 2660 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 2661 struct spdk_nvmf_ns *ns; 2662 uint32_t max_num_blocks, format_index; 2663 enum spdk_nvme_ana_state ana_state; 2664 2665 ns = _nvmf_subsystem_get_ns_safe(subsystem, cmd->nsid, rsp); 2666 if (ns == NULL) { 2667 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2668 } 2669 2670 nvmf_bdev_ctrlr_identify_ns(ns, nsdata, ctrlr->dif_insert_or_strip); 2671 2672 assert(ctrlr->admin_qpair); 2673 2674 format_index = spdk_nvme_ns_get_format_index(nsdata); 2675 2676 /* Due to bug in the Linux kernel NVMe driver we have to set noiob no larger than mdts */ 2677 max_num_blocks = ctrlr->admin_qpair->transport->opts.max_io_size / 2678 (1U << nsdata->lbaf[format_index].lbads); 2679 if (nsdata->noiob > max_num_blocks) { 2680 nsdata->noiob = max_num_blocks; 2681 } 2682 2683 /* Set NOWS equal to Controller MDTS */ 2684 if (nsdata->nsfeat.optperf) { 2685 nsdata->nows = max_num_blocks - 1; 2686 } 2687 2688 if (subsystem->flags.ana_reporting) { 2689 assert(ns->anagrpid - 1 < subsystem->max_nsid); 2690 nsdata->anagrpid = ns->anagrpid; 2691 2692 ana_state = nvmf_ctrlr_get_ana_state(ctrlr, ns->anagrpid); 2693 if (ana_state == SPDK_NVME_ANA_INACCESSIBLE_STATE || 2694 ana_state == SPDK_NVME_ANA_PERSISTENT_LOSS_STATE) { 2695 nsdata->nuse = 0; 2696 } 2697 } 2698 2699 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2700 } 2701 2702 static void 2703 nvmf_ctrlr_populate_oacs(struct spdk_nvmf_ctrlr *ctrlr, 2704 struct spdk_nvme_ctrlr_data *cdata) 2705 { 2706 cdata->oacs = ctrlr->cdata.oacs; 2707 2708 cdata->oacs.virtualization_management = 2709 g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_VIRTUALIZATION_MANAGEMENT].hdlr != NULL; 2710 cdata->oacs.nvme_mi = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NVME_MI_SEND].hdlr != NULL 2711 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NVME_MI_RECEIVE].hdlr != NULL; 2712 cdata->oacs.directives = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DIRECTIVE_SEND].hdlr != NULL 2713 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DIRECTIVE_RECEIVE].hdlr != NULL; 2714 cdata->oacs.device_self_test = 2715 g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_DEVICE_SELF_TEST].hdlr != NULL; 2716 cdata->oacs.ns_manage = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NS_MANAGEMENT].hdlr != NULL 2717 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_NS_ATTACHMENT].hdlr != NULL; 2718 cdata->oacs.firmware = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD].hdlr != 2719 NULL 2720 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FIRMWARE_COMMIT].hdlr != NULL; 2721 cdata->oacs.format = 2722 g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_FORMAT_NVM].hdlr != NULL; 2723 cdata->oacs.security = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_SECURITY_SEND].hdlr != NULL 2724 && g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_SECURITY_RECEIVE].hdlr != NULL; 2725 cdata->oacs.get_lba_status = g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_GET_LBA_STATUS].hdlr != 2726 NULL; 2727 } 2728 2729 int 2730 spdk_nvmf_ctrlr_identify_ctrlr(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_ctrlr_data *cdata) 2731 { 2732 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 2733 struct spdk_nvmf_transport *transport; 2734 2735 /* 2736 * Common fields for discovery and NVM subsystems 2737 */ 2738 assert(ctrlr->admin_qpair); 2739 transport = ctrlr->admin_qpair->transport; 2740 spdk_strcpy_pad(cdata->fr, FW_VERSION, sizeof(cdata->fr), ' '); 2741 assert((transport->opts.max_io_size % 4096) == 0); 2742 cdata->mdts = spdk_u32log2(transport->opts.max_io_size / 4096); 2743 cdata->cntlid = ctrlr->cntlid; 2744 cdata->ver = ctrlr->vcprop.vs; 2745 cdata->aerl = ctrlr->cdata.aerl; 2746 cdata->lpa.edlp = 1; 2747 cdata->elpe = 127; 2748 cdata->maxcmd = transport->opts.max_queue_depth; 2749 cdata->sgls = ctrlr->cdata.sgls; 2750 cdata->fuses = ctrlr->cdata.fuses; 2751 cdata->acwu = 0; /* ACWU is 0-based. */ 2752 if (subsystem->flags.ana_reporting) { 2753 cdata->mnan = subsystem->max_nsid; 2754 } 2755 spdk_strcpy_pad(cdata->subnqn, subsystem->subnqn, sizeof(cdata->subnqn), '\0'); 2756 2757 SPDK_DEBUGLOG(nvmf, "ctrlr data: maxcmd 0x%x\n", cdata->maxcmd); 2758 SPDK_DEBUGLOG(nvmf, "sgls data: 0x%x\n", from_le32(&cdata->sgls)); 2759 2760 2761 if (spdk_nvmf_subsystem_is_discovery(subsystem)) { 2762 /* 2763 * NVM Discovery subsystem fields 2764 */ 2765 cdata->oaes.discovery_log_change_notices = 1; 2766 } else { 2767 cdata->vid = ctrlr->cdata.vid; 2768 cdata->ssvid = ctrlr->cdata.ssvid; 2769 cdata->ieee[0] = ctrlr->cdata.ieee[0]; 2770 cdata->ieee[1] = ctrlr->cdata.ieee[1]; 2771 cdata->ieee[2] = ctrlr->cdata.ieee[2]; 2772 2773 /* 2774 * NVM subsystem fields (reserved for discovery subsystems) 2775 */ 2776 spdk_strcpy_pad(cdata->mn, spdk_nvmf_subsystem_get_mn(subsystem), sizeof(cdata->mn), ' '); 2777 spdk_strcpy_pad(cdata->sn, spdk_nvmf_subsystem_get_sn(subsystem), sizeof(cdata->sn), ' '); 2778 cdata->kas = ctrlr->cdata.kas; 2779 2780 cdata->rab = 6; 2781 cdata->cmic.multi_port = 1; 2782 cdata->cmic.multi_ctrlr = 1; 2783 cdata->oaes.ns_attribute_notices = 1; 2784 cdata->ctratt.host_id_exhid_supported = 1; 2785 /* We do not have any actual limitation to the number of abort commands. 2786 * We follow the recommendation by the NVMe specification. 2787 */ 2788 cdata->acl = NVMF_ABORT_COMMAND_LIMIT; 2789 cdata->frmw.slot1_ro = 1; 2790 cdata->frmw.num_slots = 1; 2791 2792 cdata->lpa.celp = 1; /* Command Effects log page supported */ 2793 2794 cdata->sqes.min = 6; 2795 cdata->sqes.max = 6; 2796 cdata->cqes.min = 4; 2797 cdata->cqes.max = 4; 2798 cdata->nn = subsystem->max_nsid; 2799 cdata->vwc.present = 1; 2800 cdata->vwc.flush_broadcast = SPDK_NVME_FLUSH_BROADCAST_NOT_SUPPORTED; 2801 2802 cdata->nvmf_specific = ctrlr->cdata.nvmf_specific; 2803 2804 cdata->oncs.compare = ctrlr->cdata.oncs.compare; 2805 cdata->oncs.dsm = nvmf_ctrlr_dsm_supported(ctrlr); 2806 cdata->oncs.write_zeroes = nvmf_ctrlr_write_zeroes_supported(ctrlr); 2807 cdata->oncs.reservations = ctrlr->cdata.oncs.reservations; 2808 cdata->oncs.copy = ctrlr->cdata.oncs.copy; 2809 cdata->ocfs.copy_format0 = cdata->oncs.copy; 2810 if (subsystem->flags.ana_reporting) { 2811 /* Asymmetric Namespace Access Reporting is supported. */ 2812 cdata->cmic.ana_reporting = 1; 2813 cdata->oaes.ana_change_notices = 1; 2814 2815 cdata->anatt = ANA_TRANSITION_TIME_IN_SEC; 2816 /* ANA Change state is not used, and ANA Persistent Loss state 2817 * is not supported for now. 2818 */ 2819 cdata->anacap.ana_optimized_state = 1; 2820 cdata->anacap.ana_non_optimized_state = 1; 2821 cdata->anacap.ana_inaccessible_state = 1; 2822 /* ANAGRPID does not change while namespace is attached to controller */ 2823 cdata->anacap.no_change_anagrpid = 1; 2824 cdata->anagrpmax = subsystem->max_nsid; 2825 cdata->nanagrpid = subsystem->max_nsid; 2826 } 2827 2828 nvmf_ctrlr_populate_oacs(ctrlr, cdata); 2829 2830 assert(subsystem->tgt != NULL); 2831 cdata->crdt[0] = subsystem->tgt->crdt[0]; 2832 cdata->crdt[1] = subsystem->tgt->crdt[1]; 2833 cdata->crdt[2] = subsystem->tgt->crdt[2]; 2834 2835 SPDK_DEBUGLOG(nvmf, "ext ctrlr data: ioccsz 0x%x\n", 2836 cdata->nvmf_specific.ioccsz); 2837 SPDK_DEBUGLOG(nvmf, "ext ctrlr data: iorcsz 0x%x\n", 2838 cdata->nvmf_specific.iorcsz); 2839 SPDK_DEBUGLOG(nvmf, "ext ctrlr data: icdoff 0x%x\n", 2840 cdata->nvmf_specific.icdoff); 2841 SPDK_DEBUGLOG(nvmf, "ext ctrlr data: ctrattr 0x%x\n", 2842 *(uint8_t *)&cdata->nvmf_specific.ctrattr); 2843 SPDK_DEBUGLOG(nvmf, "ext ctrlr data: msdbd 0x%x\n", 2844 cdata->nvmf_specific.msdbd); 2845 } 2846 2847 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2848 } 2849 2850 static int 2851 nvmf_ns_identify_iocs_zns(struct spdk_nvmf_ns *ns, 2852 struct spdk_nvme_cmd *cmd, 2853 struct spdk_nvme_cpl *rsp, 2854 struct spdk_nvme_zns_ns_data *nsdata_zns) 2855 { 2856 nsdata_zns->zoc.variable_zone_capacity = 0; 2857 nsdata_zns->zoc.zone_active_excursions = 0; 2858 nsdata_zns->ozcs.read_across_zone_boundaries = 1; 2859 /* Underflowing the zero based mar and mor bdev helper results in the correct 2860 value of FFFFFFFFh. */ 2861 nsdata_zns->mar = spdk_bdev_get_max_active_zones(ns->bdev) - 1; 2862 nsdata_zns->mor = spdk_bdev_get_max_open_zones(ns->bdev) - 1; 2863 nsdata_zns->rrl = 0; 2864 nsdata_zns->frl = 0; 2865 nsdata_zns->lbafe[0].zsze = spdk_bdev_get_zone_size(ns->bdev); 2866 2867 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2868 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 2869 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2870 } 2871 2872 int 2873 spdk_nvmf_ns_identify_iocs_specific(struct spdk_nvmf_ctrlr *ctrlr, 2874 struct spdk_nvme_cmd *cmd, 2875 struct spdk_nvme_cpl *rsp, 2876 void *nsdata, 2877 size_t nsdata_size) 2878 { 2879 uint8_t csi = cmd->cdw11_bits.identify.csi; 2880 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 2881 struct spdk_nvmf_ns *ns = _nvmf_subsystem_get_ns_safe(subsystem, cmd->nsid, rsp); 2882 2883 memset(nsdata, 0, nsdata_size); 2884 2885 if (ns == NULL) { 2886 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2887 rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 2888 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2889 } 2890 2891 switch (csi) { 2892 case SPDK_NVME_CSI_ZNS: 2893 return nvmf_ns_identify_iocs_zns(ns, cmd, rsp, nsdata); 2894 default: 2895 break; 2896 } 2897 2898 SPDK_DEBUGLOG(nvmf, 2899 "Returning zero filled struct for the iocs specific ns " 2900 "identify command and CSI 0x%02x\n", 2901 csi); 2902 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2903 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 2904 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2905 } 2906 2907 static int 2908 nvmf_ctrlr_identify_iocs_nvm(struct spdk_nvmf_ctrlr *ctrlr, 2909 struct spdk_nvme_cmd *cmd, 2910 struct spdk_nvme_cpl *rsp, 2911 struct spdk_nvme_nvm_ctrlr_data *cdata_nvm) 2912 { 2913 /* The unit of max_write_zeroes_size_kib is KiB. 2914 * The unit of wzsl is the minimum memory page size(2 ^ (12 + CAP.MPSMIN) bytes) 2915 * and is reported as a power of two (2^n). 2916 */ 2917 cdata_nvm->wzsl = spdk_u64log2(ctrlr->subsys->max_write_zeroes_size_kib >> 2918 (2 + ctrlr->vcprop.cap.bits.mpsmin)); 2919 2920 /* The unit of max_discard_size_kib is KiB. 2921 * The dmrsl indicates the maximum number of logical blocks for 2922 * dataset management command. 2923 */ 2924 cdata_nvm->dmrsl = ctrlr->subsys->max_discard_size_kib << 1; 2925 cdata_nvm->dmrl = 1; 2926 2927 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2928 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 2929 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2930 } 2931 2932 static int 2933 nvmf_ctrlr_identify_iocs_zns(struct spdk_nvmf_ctrlr *ctrlr, 2934 struct spdk_nvme_cmd *cmd, 2935 struct spdk_nvme_cpl *rsp, 2936 struct spdk_nvme_zns_ctrlr_data *cdata_zns) 2937 { 2938 /* The unit of max_zone_append_size_kib is KiB. 2939 The unit of zasl is the minimum memory page size 2940 (2 ^ (12 + CAP.MPSMIN) KiB) 2941 and is reported as a power of two (2^n). */ 2942 cdata_zns->zasl = spdk_u64log2(ctrlr->subsys->max_zone_append_size_kib >> 2943 (12 + ctrlr->vcprop.cap.bits.mpsmin)); 2944 2945 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2946 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 2947 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2948 } 2949 2950 int 2951 spdk_nvmf_ctrlr_identify_iocs_specific(struct spdk_nvmf_ctrlr *ctrlr, 2952 struct spdk_nvme_cmd *cmd, 2953 struct spdk_nvme_cpl *rsp, 2954 void *cdata, 2955 size_t cdata_size) 2956 { 2957 uint8_t csi = cmd->cdw11_bits.identify.csi; 2958 2959 memset(cdata, 0, cdata_size); 2960 2961 switch (csi) { 2962 case SPDK_NVME_CSI_NVM: 2963 return nvmf_ctrlr_identify_iocs_nvm(ctrlr, cmd, rsp, cdata); 2964 case SPDK_NVME_CSI_ZNS: 2965 return nvmf_ctrlr_identify_iocs_zns(ctrlr, cmd, rsp, cdata); 2966 default: 2967 break; 2968 } 2969 2970 SPDK_DEBUGLOG(nvmf, 2971 "Returning zero filled struct for the iocs specific ctrlr " 2972 "identify command and CSI 0x%02x\n", 2973 csi); 2974 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 2975 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 2976 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2977 } 2978 2979 static int 2980 nvmf_ctrlr_identify_active_ns_list(struct spdk_nvmf_subsystem *subsystem, 2981 struct spdk_nvme_cmd *cmd, 2982 struct spdk_nvme_cpl *rsp, 2983 struct spdk_nvme_ns_list *ns_list) 2984 { 2985 struct spdk_nvmf_ns *ns; 2986 uint32_t count = 0; 2987 2988 if (cmd->nsid >= 0xfffffffeUL) { 2989 SPDK_ERRLOG("Identify Active Namespace List with invalid NSID %u\n", cmd->nsid); 2990 rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 2991 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 2992 } 2993 2994 memset(ns_list, 0, sizeof(*ns_list)); 2995 2996 for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL; 2997 ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) { 2998 if (ns->opts.nsid <= cmd->nsid) { 2999 continue; 3000 } 3001 3002 ns_list->ns_list[count++] = ns->opts.nsid; 3003 if (count == SPDK_COUNTOF(ns_list->ns_list)) { 3004 break; 3005 } 3006 } 3007 3008 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3009 } 3010 3011 static void 3012 _add_ns_id_desc(void **buf_ptr, size_t *buf_remain, 3013 enum spdk_nvme_nidt type, 3014 const void *data, size_t data_size) 3015 { 3016 struct spdk_nvme_ns_id_desc *desc; 3017 size_t desc_size = sizeof(*desc) + data_size; 3018 3019 /* 3020 * These should never fail in practice, since all valid NS ID descriptors 3021 * should be defined so that they fit in the available 4096-byte buffer. 3022 */ 3023 assert(data_size > 0); 3024 assert(data_size <= UINT8_MAX); 3025 assert(desc_size < *buf_remain); 3026 if (data_size == 0 || data_size > UINT8_MAX || desc_size > *buf_remain) { 3027 return; 3028 } 3029 3030 desc = *buf_ptr; 3031 desc->nidt = type; 3032 desc->nidl = data_size; 3033 memcpy(desc->nid, data, data_size); 3034 3035 *buf_ptr += desc_size; 3036 *buf_remain -= desc_size; 3037 } 3038 3039 static int 3040 nvmf_ctrlr_identify_ns_id_descriptor_list( 3041 struct spdk_nvmf_subsystem *subsystem, 3042 struct spdk_nvme_cmd *cmd, 3043 struct spdk_nvme_cpl *rsp, 3044 void *id_desc_list, size_t id_desc_list_size) 3045 { 3046 struct spdk_nvmf_ns *ns; 3047 size_t buf_remain = id_desc_list_size; 3048 void *buf_ptr = id_desc_list; 3049 3050 ns = _nvmf_subsystem_get_ns(subsystem, cmd->nsid); 3051 if (ns == NULL || ns->bdev == NULL) { 3052 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 3053 rsp->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 3054 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3055 } 3056 3057 #define ADD_ID_DESC(type, data, size) \ 3058 do { \ 3059 if (!spdk_mem_all_zero(data, size)) { \ 3060 _add_ns_id_desc(&buf_ptr, &buf_remain, type, data, size); \ 3061 } \ 3062 } while (0) 3063 3064 ADD_ID_DESC(SPDK_NVME_NIDT_EUI64, ns->opts.eui64, sizeof(ns->opts.eui64)); 3065 ADD_ID_DESC(SPDK_NVME_NIDT_NGUID, ns->opts.nguid, sizeof(ns->opts.nguid)); 3066 ADD_ID_DESC(SPDK_NVME_NIDT_UUID, &ns->opts.uuid, sizeof(ns->opts.uuid)); 3067 ADD_ID_DESC(SPDK_NVME_NIDT_CSI, &ns->csi, sizeof(uint8_t)); 3068 3069 /* 3070 * The list is automatically 0-terminated, both in the temporary buffer 3071 * used by nvmf_ctrlr_identify(), and the eventual iov destination - 3072 * controller to host buffers in admin commands always get zeroed in 3073 * nvmf_ctrlr_process_admin_cmd(). 3074 */ 3075 3076 #undef ADD_ID_DESC 3077 3078 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3079 } 3080 3081 static int 3082 nvmf_ctrlr_identify_iocs(struct spdk_nvmf_ctrlr *ctrlr, 3083 struct spdk_nvme_cmd *cmd, 3084 struct spdk_nvme_cpl *rsp, 3085 void *cdata, size_t cdata_size) 3086 { 3087 struct spdk_nvme_iocs_vector *vector; 3088 struct spdk_nvmf_ns *ns; 3089 3090 if (cdata_size < sizeof(struct spdk_nvme_iocs_vector)) { 3091 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 3092 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 3093 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3094 } 3095 3096 /* For now we only support this command sent to the current 3097 * controller. 3098 */ 3099 if (cmd->cdw10_bits.identify.cntid != 0xFFFF && 3100 cmd->cdw10_bits.identify.cntid != ctrlr->cntlid) { 3101 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 3102 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 3103 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3104 } 3105 memset(cdata, 0, cdata_size); 3106 3107 vector = cdata; 3108 vector->nvm = 1; 3109 for (ns = spdk_nvmf_subsystem_get_first_ns(ctrlr->subsys); ns != NULL; 3110 ns = spdk_nvmf_subsystem_get_next_ns(ctrlr->subsys, ns)) { 3111 if (ns->bdev == NULL) { 3112 continue; 3113 } 3114 if (spdk_bdev_is_zoned(ns->bdev)) { 3115 vector->zns = 1; 3116 } 3117 } 3118 3119 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 3120 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 3121 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3122 } 3123 3124 static int 3125 nvmf_ctrlr_identify(struct spdk_nvmf_request *req) 3126 { 3127 uint8_t cns; 3128 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 3129 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 3130 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 3131 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 3132 int ret = SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3133 char tmpbuf[SPDK_NVME_IDENTIFY_BUFLEN] = ""; 3134 struct spdk_iov_xfer ix; 3135 3136 if (req->iovcnt < 1 || req->length < SPDK_NVME_IDENTIFY_BUFLEN) { 3137 SPDK_DEBUGLOG(nvmf, "identify command with invalid buffer\n"); 3138 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 3139 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 3140 return ret; 3141 } 3142 3143 cns = cmd->cdw10_bits.identify.cns; 3144 3145 if (spdk_nvmf_subsystem_is_discovery(subsystem) && 3146 cns != SPDK_NVME_IDENTIFY_CTRLR) { 3147 /* Discovery controllers only support Identify Controller */ 3148 goto invalid_cns; 3149 } 3150 3151 /* 3152 * We must use a temporary buffer: it's entirely possible the out buffer 3153 * is split across more than one IOV. 3154 */ 3155 spdk_iov_xfer_init(&ix, req->iov, req->iovcnt); 3156 3157 SPDK_DEBUGLOG(nvmf, "Received identify command with CNS 0x%02x\n", cns); 3158 3159 switch (cns) { 3160 case SPDK_NVME_IDENTIFY_NS: 3161 ret = spdk_nvmf_ctrlr_identify_ns(ctrlr, cmd, rsp, (void *)&tmpbuf); 3162 break; 3163 case SPDK_NVME_IDENTIFY_CTRLR: 3164 ret = spdk_nvmf_ctrlr_identify_ctrlr(ctrlr, (void *)&tmpbuf); 3165 break; 3166 case SPDK_NVME_IDENTIFY_ACTIVE_NS_LIST: 3167 ret = nvmf_ctrlr_identify_active_ns_list(subsystem, cmd, rsp, (void *)&tmpbuf); 3168 break; 3169 case SPDK_NVME_IDENTIFY_NS_ID_DESCRIPTOR_LIST: 3170 ret = nvmf_ctrlr_identify_ns_id_descriptor_list(subsystem, cmd, rsp, 3171 tmpbuf, req->length); 3172 break; 3173 case SPDK_NVME_IDENTIFY_NS_IOCS: 3174 ret = spdk_nvmf_ns_identify_iocs_specific(ctrlr, cmd, rsp, (void *)&tmpbuf, req->length); 3175 break; 3176 case SPDK_NVME_IDENTIFY_CTRLR_IOCS: 3177 ret = spdk_nvmf_ctrlr_identify_iocs_specific(ctrlr, cmd, rsp, (void *)&tmpbuf, req->length); 3178 break; 3179 case SPDK_NVME_IDENTIFY_IOCS: 3180 ret = nvmf_ctrlr_identify_iocs(ctrlr, cmd, rsp, (void *)&tmpbuf, req->length); 3181 break; 3182 default: 3183 goto invalid_cns; 3184 } 3185 3186 if (ret == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 3187 spdk_iov_xfer_from_buf(&ix, tmpbuf, sizeof(tmpbuf)); 3188 } 3189 3190 return ret; 3191 3192 invalid_cns: 3193 SPDK_DEBUGLOG(nvmf, "Identify command with unsupported CNS 0x%02x\n", cns); 3194 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 3195 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 3196 return ret; 3197 } 3198 3199 static bool 3200 nvmf_qpair_abort_aer(struct spdk_nvmf_qpair *qpair, uint16_t cid) 3201 { 3202 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 3203 struct spdk_nvmf_request *req; 3204 int i; 3205 3206 if (!nvmf_qpair_is_admin_queue(qpair)) { 3207 return false; 3208 } 3209 3210 assert(spdk_get_thread() == ctrlr->thread); 3211 3212 for (i = 0; i < ctrlr->nr_aer_reqs; i++) { 3213 if (ctrlr->aer_req[i]->cmd->nvme_cmd.cid == cid) { 3214 SPDK_DEBUGLOG(nvmf, "Aborting AER request\n"); 3215 req = ctrlr->aer_req[i]; 3216 ctrlr->aer_req[i] = NULL; 3217 ctrlr->nr_aer_reqs--; 3218 3219 /* Move the last req to the aborting position for making aer_reqs 3220 * in continuous 3221 */ 3222 if (i < ctrlr->nr_aer_reqs) { 3223 ctrlr->aer_req[i] = ctrlr->aer_req[ctrlr->nr_aer_reqs]; 3224 ctrlr->aer_req[ctrlr->nr_aer_reqs] = NULL; 3225 } 3226 3227 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3228 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_ABORTED_BY_REQUEST; 3229 _nvmf_request_complete(req); 3230 return true; 3231 } 3232 } 3233 3234 return false; 3235 } 3236 3237 void 3238 nvmf_qpair_abort_pending_zcopy_reqs(struct spdk_nvmf_qpair *qpair) 3239 { 3240 struct spdk_nvmf_request *req, *tmp; 3241 3242 TAILQ_FOREACH_SAFE(req, &qpair->outstanding, link, tmp) { 3243 if (req->zcopy_phase == NVMF_ZCOPY_PHASE_EXECUTE) { 3244 /* Zero-copy requests are kept on the outstanding queue from the moment 3245 * zcopy_start is sent until a zcopy_end callback is received. Therefore, 3246 * we can't remove them from the outstanding queue here, but need to rely on 3247 * the transport to do a zcopy_end to release their buffers and, in turn, 3248 * remove them from the queue. 3249 */ 3250 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3251 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_ABORTED_BY_REQUEST; 3252 nvmf_transport_req_free(req); 3253 } 3254 } 3255 } 3256 3257 static void 3258 nvmf_qpair_abort_request(struct spdk_nvmf_qpair *qpair, struct spdk_nvmf_request *req) 3259 { 3260 uint16_t cid = req->cmd->nvme_cmd.cdw10_bits.abort.cid; 3261 3262 if (nvmf_qpair_abort_aer(qpair, cid)) { 3263 SPDK_DEBUGLOG(nvmf, "abort ctrlr=%p sqid=%u cid=%u successful\n", 3264 qpair->ctrlr, qpair->qid, cid); 3265 req->rsp->nvme_cpl.cdw0 &= ~1U; /* Command successfully aborted */ 3266 3267 spdk_nvmf_request_complete(req); 3268 return; 3269 } 3270 3271 nvmf_transport_qpair_abort_request(qpair, req); 3272 } 3273 3274 static void 3275 nvmf_ctrlr_abort_done(struct spdk_io_channel_iter *i, int status) 3276 { 3277 struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i); 3278 3279 if (status == 0) { 3280 /* There was no qpair whose ID matches SQID of the abort command. 3281 * Hence call _nvmf_request_complete() here. 3282 */ 3283 _nvmf_request_complete(req); 3284 } 3285 } 3286 3287 static void 3288 nvmf_ctrlr_abort_on_pg(struct spdk_io_channel_iter *i) 3289 { 3290 struct spdk_nvmf_request *req = spdk_io_channel_iter_get_ctx(i); 3291 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 3292 struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch); 3293 uint16_t sqid = req->cmd->nvme_cmd.cdw10_bits.abort.sqid; 3294 struct spdk_nvmf_qpair *qpair; 3295 3296 TAILQ_FOREACH(qpair, &group->qpairs, link) { 3297 if (qpair->ctrlr == req->qpair->ctrlr && qpair->qid == sqid) { 3298 /* Found the qpair */ 3299 3300 nvmf_qpair_abort_request(qpair, req); 3301 3302 /* Return -1 for the status so the iteration across threads stops. */ 3303 spdk_for_each_channel_continue(i, -1); 3304 return; 3305 } 3306 } 3307 3308 spdk_for_each_channel_continue(i, 0); 3309 } 3310 3311 static int 3312 nvmf_ctrlr_abort(struct spdk_nvmf_request *req) 3313 { 3314 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 3315 3316 rsp->cdw0 = 1U; /* Command not aborted */ 3317 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 3318 rsp->status.sc = SPDK_NVME_SC_SUCCESS; 3319 3320 /* Send a message to each poll group, searching for this ctrlr, sqid, and command. */ 3321 spdk_for_each_channel(req->qpair->ctrlr->subsys->tgt, 3322 nvmf_ctrlr_abort_on_pg, 3323 req, 3324 nvmf_ctrlr_abort_done 3325 ); 3326 3327 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 3328 } 3329 3330 int 3331 nvmf_ctrlr_abort_request(struct spdk_nvmf_request *req) 3332 { 3333 struct spdk_nvmf_request *req_to_abort = req->req_to_abort; 3334 struct spdk_bdev *bdev; 3335 struct spdk_bdev_desc *desc; 3336 struct spdk_io_channel *ch; 3337 int rc; 3338 3339 assert(req_to_abort != NULL); 3340 3341 if (g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_ABORT].hdlr && 3342 nvmf_qpair_is_admin_queue(req_to_abort->qpair)) { 3343 return g_nvmf_custom_admin_cmd_hdlrs[SPDK_NVME_OPC_ABORT].hdlr(req); 3344 } 3345 3346 rc = spdk_nvmf_request_get_bdev(req_to_abort->cmd->nvme_cmd.nsid, req_to_abort, 3347 &bdev, &desc, &ch); 3348 if (rc != 0) { 3349 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3350 } 3351 3352 return spdk_nvmf_bdev_ctrlr_abort_cmd(bdev, desc, ch, req, req_to_abort); 3353 } 3354 3355 static int 3356 get_features_generic(struct spdk_nvmf_request *req, uint32_t cdw0) 3357 { 3358 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 3359 3360 rsp->cdw0 = cdw0; 3361 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3362 } 3363 3364 /* we have to use the typedef in the function declaration to appease astyle. */ 3365 typedef enum spdk_nvme_path_status_code spdk_nvme_path_status_code_t; 3366 3367 static spdk_nvme_path_status_code_t 3368 _nvme_ana_state_to_path_status(enum spdk_nvme_ana_state ana_state) 3369 { 3370 switch (ana_state) { 3371 case SPDK_NVME_ANA_INACCESSIBLE_STATE: 3372 return SPDK_NVME_SC_ASYMMETRIC_ACCESS_INACCESSIBLE; 3373 case SPDK_NVME_ANA_PERSISTENT_LOSS_STATE: 3374 return SPDK_NVME_SC_ASYMMETRIC_ACCESS_PERSISTENT_LOSS; 3375 case SPDK_NVME_ANA_CHANGE_STATE: 3376 return SPDK_NVME_SC_ASYMMETRIC_ACCESS_TRANSITION; 3377 default: 3378 return SPDK_NVME_SC_INTERNAL_PATH_ERROR; 3379 } 3380 } 3381 3382 static int 3383 nvmf_ctrlr_get_features(struct spdk_nvmf_request *req) 3384 { 3385 uint8_t feature; 3386 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 3387 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 3388 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 3389 enum spdk_nvme_ana_state ana_state; 3390 3391 feature = cmd->cdw10_bits.get_features.fid; 3392 3393 if (spdk_nvmf_subsystem_is_discovery(ctrlr->subsys)) { 3394 /* 3395 * Features supported by Discovery controller 3396 */ 3397 switch (feature) { 3398 case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER: 3399 return get_features_generic(req, ctrlr->feat.keep_alive_timer.raw); 3400 case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION: 3401 return get_features_generic(req, ctrlr->feat.async_event_configuration.raw); 3402 default: 3403 SPDK_INFOLOG(nvmf, "Get Features command with unsupported feature ID 0x%02x\n", feature); 3404 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 3405 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3406 } 3407 } 3408 /* 3409 * Process Get Features command for non-discovery controller 3410 */ 3411 ana_state = nvmf_ctrlr_get_ana_state_from_nsid(ctrlr, cmd->nsid); 3412 switch (ana_state) { 3413 case SPDK_NVME_ANA_INACCESSIBLE_STATE: 3414 case SPDK_NVME_ANA_PERSISTENT_LOSS_STATE: 3415 case SPDK_NVME_ANA_CHANGE_STATE: 3416 switch (feature) { 3417 case SPDK_NVME_FEAT_ERROR_RECOVERY: 3418 case SPDK_NVME_FEAT_WRITE_ATOMICITY: 3419 case SPDK_NVME_FEAT_HOST_RESERVE_MASK: 3420 case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST: 3421 response->status.sct = SPDK_NVME_SCT_PATH; 3422 response->status.sc = _nvme_ana_state_to_path_status(ana_state); 3423 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3424 default: 3425 break; 3426 } 3427 break; 3428 default: 3429 break; 3430 } 3431 3432 switch (feature) { 3433 case SPDK_NVME_FEAT_ARBITRATION: 3434 return get_features_generic(req, ctrlr->feat.arbitration.raw); 3435 case SPDK_NVME_FEAT_POWER_MANAGEMENT: 3436 return get_features_generic(req, ctrlr->feat.power_management.raw); 3437 case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD: 3438 return nvmf_ctrlr_get_features_temperature_threshold(req); 3439 case SPDK_NVME_FEAT_ERROR_RECOVERY: 3440 return get_features_generic(req, ctrlr->feat.error_recovery.raw); 3441 case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE: 3442 return get_features_generic(req, ctrlr->feat.volatile_write_cache.raw); 3443 case SPDK_NVME_FEAT_NUMBER_OF_QUEUES: 3444 return get_features_generic(req, ctrlr->feat.number_of_queues.raw); 3445 case SPDK_NVME_FEAT_INTERRUPT_COALESCING: 3446 return get_features_generic(req, ctrlr->feat.interrupt_coalescing.raw); 3447 case SPDK_NVME_FEAT_INTERRUPT_VECTOR_CONFIGURATION: 3448 return nvmf_ctrlr_get_features_interrupt_vector_configuration(req); 3449 case SPDK_NVME_FEAT_WRITE_ATOMICITY: 3450 return get_features_generic(req, ctrlr->feat.write_atomicity.raw); 3451 case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION: 3452 return get_features_generic(req, ctrlr->feat.async_event_configuration.raw); 3453 case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER: 3454 return get_features_generic(req, ctrlr->feat.keep_alive_timer.raw); 3455 case SPDK_NVME_FEAT_HOST_IDENTIFIER: 3456 return nvmf_ctrlr_get_features_host_identifier(req); 3457 case SPDK_NVME_FEAT_HOST_RESERVE_MASK: 3458 return nvmf_ctrlr_get_features_reservation_notification_mask(req); 3459 case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST: 3460 return nvmf_ctrlr_get_features_reservation_persistence(req); 3461 case SPDK_NVME_FEAT_HOST_BEHAVIOR_SUPPORT: 3462 return nvmf_ctrlr_get_features_host_behavior_support(req); 3463 default: 3464 SPDK_INFOLOG(nvmf, "Get Features command with unsupported feature ID 0x%02x\n", feature); 3465 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 3466 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3467 } 3468 } 3469 3470 static int 3471 nvmf_ctrlr_set_features(struct spdk_nvmf_request *req) 3472 { 3473 uint8_t feature, save; 3474 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 3475 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 3476 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 3477 enum spdk_nvme_ana_state ana_state; 3478 /* 3479 * Features are not saveable by the controller as indicated by 3480 * ONCS field of the Identify Controller data. 3481 * */ 3482 save = cmd->cdw10_bits.set_features.sv; 3483 if (save) { 3484 response->status.sc = SPDK_NVME_SC_FEATURE_ID_NOT_SAVEABLE; 3485 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 3486 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3487 } 3488 3489 feature = cmd->cdw10_bits.set_features.fid; 3490 3491 if (spdk_nvmf_subsystem_is_discovery(ctrlr->subsys)) { 3492 /* 3493 * Features supported by Discovery controller 3494 */ 3495 switch (feature) { 3496 case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER: 3497 return nvmf_ctrlr_set_features_keep_alive_timer(req); 3498 case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION: 3499 return nvmf_ctrlr_set_features_async_event_configuration(req); 3500 default: 3501 SPDK_INFOLOG(nvmf, "Set Features command with unsupported feature ID 0x%02x\n", feature); 3502 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 3503 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3504 } 3505 } 3506 /* 3507 * Process Set Features command for non-discovery controller 3508 */ 3509 ana_state = nvmf_ctrlr_get_ana_state_from_nsid(ctrlr, cmd->nsid); 3510 switch (ana_state) { 3511 case SPDK_NVME_ANA_INACCESSIBLE_STATE: 3512 case SPDK_NVME_ANA_CHANGE_STATE: 3513 if (cmd->nsid == SPDK_NVME_GLOBAL_NS_TAG) { 3514 response->status.sct = SPDK_NVME_SCT_PATH; 3515 response->status.sc = _nvme_ana_state_to_path_status(ana_state); 3516 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3517 } else { 3518 switch (feature) { 3519 case SPDK_NVME_FEAT_ERROR_RECOVERY: 3520 case SPDK_NVME_FEAT_WRITE_ATOMICITY: 3521 case SPDK_NVME_FEAT_HOST_RESERVE_MASK: 3522 case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST: 3523 response->status.sct = SPDK_NVME_SCT_PATH; 3524 response->status.sc = _nvme_ana_state_to_path_status(ana_state); 3525 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3526 default: 3527 break; 3528 } 3529 } 3530 break; 3531 case SPDK_NVME_ANA_PERSISTENT_LOSS_STATE: 3532 response->status.sct = SPDK_NVME_SCT_PATH; 3533 response->status.sc = SPDK_NVME_SC_ASYMMETRIC_ACCESS_PERSISTENT_LOSS; 3534 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3535 default: 3536 break; 3537 } 3538 3539 switch (feature) { 3540 case SPDK_NVME_FEAT_ARBITRATION: 3541 return nvmf_ctrlr_set_features_arbitration(req); 3542 case SPDK_NVME_FEAT_POWER_MANAGEMENT: 3543 return nvmf_ctrlr_set_features_power_management(req); 3544 case SPDK_NVME_FEAT_TEMPERATURE_THRESHOLD: 3545 return nvmf_ctrlr_set_features_temperature_threshold(req); 3546 case SPDK_NVME_FEAT_ERROR_RECOVERY: 3547 return nvmf_ctrlr_set_features_error_recovery(req); 3548 case SPDK_NVME_FEAT_VOLATILE_WRITE_CACHE: 3549 return nvmf_ctrlr_set_features_volatile_write_cache(req); 3550 case SPDK_NVME_FEAT_NUMBER_OF_QUEUES: 3551 return nvmf_ctrlr_set_features_number_of_queues(req); 3552 case SPDK_NVME_FEAT_INTERRUPT_COALESCING: 3553 response->status.sct = SPDK_NVME_SCT_COMMAND_SPECIFIC; 3554 response->status.sc = SPDK_NVME_SC_FEATURE_NOT_CHANGEABLE; 3555 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3556 case SPDK_NVME_FEAT_WRITE_ATOMICITY: 3557 return nvmf_ctrlr_set_features_write_atomicity(req); 3558 case SPDK_NVME_FEAT_ASYNC_EVENT_CONFIGURATION: 3559 return nvmf_ctrlr_set_features_async_event_configuration(req); 3560 case SPDK_NVME_FEAT_KEEP_ALIVE_TIMER: 3561 return nvmf_ctrlr_set_features_keep_alive_timer(req); 3562 case SPDK_NVME_FEAT_HOST_IDENTIFIER: 3563 return nvmf_ctrlr_set_features_host_identifier(req); 3564 case SPDK_NVME_FEAT_HOST_RESERVE_MASK: 3565 return nvmf_ctrlr_set_features_reservation_notification_mask(req); 3566 case SPDK_NVME_FEAT_HOST_RESERVE_PERSIST: 3567 return nvmf_ctrlr_set_features_reservation_persistence(req); 3568 case SPDK_NVME_FEAT_HOST_BEHAVIOR_SUPPORT: 3569 return nvmf_ctrlr_set_features_host_behavior_support(req); 3570 default: 3571 SPDK_INFOLOG(nvmf, "Set Features command with unsupported feature ID 0x%02x\n", feature); 3572 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 3573 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3574 } 3575 } 3576 3577 static int 3578 nvmf_ctrlr_keep_alive(struct spdk_nvmf_request *req) 3579 { 3580 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 3581 3582 SPDK_DEBUGLOG(nvmf, "Keep Alive\n"); 3583 /* 3584 * To handle keep alive just clear or reset the 3585 * ctrlr based keep alive duration counter. 3586 * When added, a separate timer based process 3587 * will monitor if the time since last recorded 3588 * keep alive has exceeded the max duration and 3589 * take appropriate action. 3590 */ 3591 ctrlr->last_keep_alive_tick = spdk_get_ticks(); 3592 3593 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3594 } 3595 3596 int 3597 nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req) 3598 { 3599 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 3600 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 3601 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 3602 struct spdk_nvmf_subsystem_poll_group *sgroup; 3603 int rc; 3604 3605 if (ctrlr == NULL) { 3606 SPDK_ERRLOG("Admin command sent before CONNECT\n"); 3607 response->status.sct = SPDK_NVME_SCT_GENERIC; 3608 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 3609 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3610 } 3611 3612 if (cmd->opc == SPDK_NVME_OPC_ASYNC_EVENT_REQUEST) { 3613 /* We do not want to treat AERs as outstanding commands, 3614 * so decrement mgmt_io_outstanding here to offset 3615 * the increment that happened prior to this call. 3616 */ 3617 sgroup = &req->qpair->group->sgroups[ctrlr->subsys->id]; 3618 assert(sgroup != NULL); 3619 sgroup->mgmt_io_outstanding--; 3620 } 3621 3622 assert(spdk_get_thread() == ctrlr->thread); 3623 3624 if (cmd->fuse != 0) { 3625 /* Fused admin commands are not supported. */ 3626 response->status.sct = SPDK_NVME_SCT_GENERIC; 3627 response->status.sc = SPDK_NVME_SC_INVALID_FIELD; 3628 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3629 } 3630 3631 if (ctrlr->vcprop.cc.bits.en != 1) { 3632 SPDK_ERRLOG("Admin command sent to disabled controller\n"); 3633 response->status.sct = SPDK_NVME_SCT_GENERIC; 3634 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 3635 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3636 } 3637 3638 if (req->iovcnt && spdk_nvme_opc_get_data_transfer(cmd->opc) == SPDK_NVME_DATA_CONTROLLER_TO_HOST) { 3639 spdk_iov_memset(req->iov, req->iovcnt, 0); 3640 } 3641 3642 if (spdk_nvmf_subsystem_is_discovery(ctrlr->subsys)) { 3643 /* Discovery controllers only support these admin OPS. */ 3644 switch (cmd->opc) { 3645 case SPDK_NVME_OPC_IDENTIFY: 3646 case SPDK_NVME_OPC_GET_LOG_PAGE: 3647 case SPDK_NVME_OPC_KEEP_ALIVE: 3648 case SPDK_NVME_OPC_SET_FEATURES: 3649 case SPDK_NVME_OPC_GET_FEATURES: 3650 case SPDK_NVME_OPC_ASYNC_EVENT_REQUEST: 3651 break; 3652 default: 3653 goto invalid_opcode; 3654 } 3655 } 3656 3657 /* Call a custom adm cmd handler if set. Aborts are handled in a different path (see nvmf_passthru_admin_cmd) */ 3658 if (g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].hdlr && cmd->opc != SPDK_NVME_OPC_ABORT) { 3659 rc = g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].hdlr(req); 3660 if (rc >= SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 3661 /* The handler took care of this command */ 3662 return rc; 3663 } 3664 } 3665 3666 switch (cmd->opc) { 3667 case SPDK_NVME_OPC_GET_LOG_PAGE: 3668 return nvmf_ctrlr_get_log_page(req); 3669 case SPDK_NVME_OPC_IDENTIFY: 3670 return nvmf_ctrlr_identify(req); 3671 case SPDK_NVME_OPC_ABORT: 3672 return nvmf_ctrlr_abort(req); 3673 case SPDK_NVME_OPC_GET_FEATURES: 3674 return nvmf_ctrlr_get_features(req); 3675 case SPDK_NVME_OPC_SET_FEATURES: 3676 return nvmf_ctrlr_set_features(req); 3677 case SPDK_NVME_OPC_ASYNC_EVENT_REQUEST: 3678 return nvmf_ctrlr_async_event_request(req); 3679 case SPDK_NVME_OPC_KEEP_ALIVE: 3680 return nvmf_ctrlr_keep_alive(req); 3681 3682 case SPDK_NVME_OPC_CREATE_IO_SQ: 3683 case SPDK_NVME_OPC_CREATE_IO_CQ: 3684 case SPDK_NVME_OPC_DELETE_IO_SQ: 3685 case SPDK_NVME_OPC_DELETE_IO_CQ: 3686 /* Create and Delete I/O CQ/SQ not allowed in NVMe-oF */ 3687 goto invalid_opcode; 3688 3689 default: 3690 goto invalid_opcode; 3691 } 3692 3693 invalid_opcode: 3694 SPDK_INFOLOG(nvmf, "Unsupported admin opcode 0x%x\n", cmd->opc); 3695 response->status.sct = SPDK_NVME_SCT_GENERIC; 3696 response->status.sc = SPDK_NVME_SC_INVALID_OPCODE; 3697 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3698 } 3699 3700 static int 3701 nvmf_ctrlr_process_fabrics_cmd(struct spdk_nvmf_request *req) 3702 { 3703 struct spdk_nvmf_qpair *qpair = req->qpair; 3704 struct spdk_nvmf_capsule_cmd *cap_hdr; 3705 3706 cap_hdr = &req->cmd->nvmf_cmd; 3707 3708 if (qpair->ctrlr == NULL) { 3709 /* No ctrlr established yet; the only valid command is Connect */ 3710 if (cap_hdr->fctype == SPDK_NVMF_FABRIC_COMMAND_CONNECT) { 3711 return nvmf_ctrlr_cmd_connect(req); 3712 } else { 3713 SPDK_DEBUGLOG(nvmf, "Got fctype 0x%x, expected Connect\n", 3714 cap_hdr->fctype); 3715 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3716 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 3717 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3718 } 3719 } else if (nvmf_qpair_is_admin_queue(qpair)) { 3720 /* 3721 * Controller session is established, and this is an admin queue. 3722 * Disallow Connect and allow other fabrics commands. 3723 */ 3724 switch (cap_hdr->fctype) { 3725 case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_SET: 3726 return nvmf_property_set(req); 3727 case SPDK_NVMF_FABRIC_COMMAND_PROPERTY_GET: 3728 return nvmf_property_get(req); 3729 default: 3730 SPDK_DEBUGLOG(nvmf, "unknown fctype 0x%02x\n", 3731 cap_hdr->fctype); 3732 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3733 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE; 3734 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3735 } 3736 } else { 3737 /* Controller session is established, and this is an I/O queue */ 3738 /* For now, no I/O-specific Fabrics commands are implemented (other than Connect) */ 3739 SPDK_DEBUGLOG(nvmf, "Unexpected I/O fctype 0x%x\n", cap_hdr->fctype); 3740 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3741 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_OPCODE; 3742 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 3743 } 3744 } 3745 3746 static inline void 3747 nvmf_ctrlr_queue_pending_async_event(struct spdk_nvmf_ctrlr *ctrlr, 3748 union spdk_nvme_async_event_completion *event) 3749 { 3750 struct spdk_nvmf_async_event_completion *nvmf_event; 3751 3752 nvmf_event = calloc(1, sizeof(*nvmf_event)); 3753 if (!nvmf_event) { 3754 SPDK_ERRLOG("Alloc nvmf event failed, ignore the event\n"); 3755 return; 3756 } 3757 nvmf_event->event.raw = event->raw; 3758 STAILQ_INSERT_TAIL(&ctrlr->async_events, nvmf_event, link); 3759 } 3760 3761 static inline int 3762 nvmf_ctrlr_async_event_notification(struct spdk_nvmf_ctrlr *ctrlr, 3763 union spdk_nvme_async_event_completion *event) 3764 { 3765 struct spdk_nvmf_request *req; 3766 struct spdk_nvme_cpl *rsp; 3767 3768 assert(spdk_get_thread() == ctrlr->thread); 3769 3770 /* If there is no outstanding AER request, queue the event. Then 3771 * if an AER is later submitted, this event can be sent as a 3772 * response. 3773 */ 3774 if (ctrlr->nr_aer_reqs == 0) { 3775 nvmf_ctrlr_queue_pending_async_event(ctrlr, event); 3776 return 0; 3777 } 3778 3779 req = ctrlr->aer_req[--ctrlr->nr_aer_reqs]; 3780 rsp = &req->rsp->nvme_cpl; 3781 3782 rsp->cdw0 = event->raw; 3783 3784 _nvmf_request_complete(req); 3785 ctrlr->aer_req[ctrlr->nr_aer_reqs] = NULL; 3786 3787 return 0; 3788 } 3789 3790 int 3791 nvmf_ctrlr_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr) 3792 { 3793 union spdk_nvme_async_event_completion event = {0}; 3794 3795 /* Users may disable the event notification */ 3796 if (!ctrlr->feat.async_event_configuration.bits.ns_attr_notice) { 3797 return 0; 3798 } 3799 3800 if (!nvmf_ctrlr_mask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGE_MASK_BIT)) { 3801 return 0; 3802 } 3803 3804 event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE; 3805 event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGED; 3806 event.bits.log_page_identifier = SPDK_NVME_LOG_CHANGED_NS_LIST; 3807 3808 return nvmf_ctrlr_async_event_notification(ctrlr, &event); 3809 } 3810 3811 int 3812 nvmf_ctrlr_async_event_ana_change_notice(struct spdk_nvmf_ctrlr *ctrlr) 3813 { 3814 union spdk_nvme_async_event_completion event = {0}; 3815 3816 /* Users may disable the event notification */ 3817 if (!ctrlr->feat.async_event_configuration.bits.ana_change_notice) { 3818 return 0; 3819 } 3820 3821 if (!nvmf_ctrlr_mask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_ANA_CHANGE_MASK_BIT)) { 3822 return 0; 3823 } 3824 3825 event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE; 3826 event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_ANA_CHANGE; 3827 event.bits.log_page_identifier = SPDK_NVME_LOG_ASYMMETRIC_NAMESPACE_ACCESS; 3828 3829 return nvmf_ctrlr_async_event_notification(ctrlr, &event); 3830 } 3831 3832 void 3833 nvmf_ctrlr_async_event_reservation_notification(struct spdk_nvmf_ctrlr *ctrlr) 3834 { 3835 union spdk_nvme_async_event_completion event = {0}; 3836 3837 if (!ctrlr->num_avail_log_pages) { 3838 return; 3839 } 3840 3841 if (!nvmf_ctrlr_mask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_RESERVATION_LOG_AVAIL_MASK_BIT)) { 3842 return; 3843 } 3844 3845 event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_IO; 3846 event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_RESERVATION_LOG_AVAIL; 3847 event.bits.log_page_identifier = SPDK_NVME_LOG_RESERVATION_NOTIFICATION; 3848 3849 nvmf_ctrlr_async_event_notification(ctrlr, &event); 3850 } 3851 3852 void 3853 nvmf_ctrlr_async_event_discovery_log_change_notice(void *ctx) 3854 { 3855 union spdk_nvme_async_event_completion event = {0}; 3856 struct spdk_nvmf_ctrlr *ctrlr = ctx; 3857 3858 /* Users may disable the event notification manually or 3859 * it may not be enabled due to keep alive timeout 3860 * not being set in connect command to discovery controller. 3861 */ 3862 if (!ctrlr->feat.async_event_configuration.bits.discovery_log_change_notice) { 3863 return; 3864 } 3865 3866 if (!nvmf_ctrlr_mask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_DISCOVERY_LOG_CHANGE_MASK_BIT)) { 3867 return; 3868 } 3869 3870 event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE; 3871 event.bits.async_event_info = SPDK_NVME_ASYNC_EVENT_DISCOVERY_LOG_CHANGE; 3872 event.bits.log_page_identifier = SPDK_NVME_LOG_DISCOVERY; 3873 3874 nvmf_ctrlr_async_event_notification(ctrlr, &event); 3875 } 3876 3877 int 3878 spdk_nvmf_ctrlr_async_event_error_event(struct spdk_nvmf_ctrlr *ctrlr, 3879 enum spdk_nvme_async_event_info_error info) 3880 { 3881 union spdk_nvme_async_event_completion event; 3882 3883 if (!nvmf_ctrlr_mask_aen(ctrlr, SPDK_NVME_ASYNC_EVENT_ERROR_MASK_BIT)) { 3884 return 0; 3885 } 3886 3887 if (info > SPDK_NVME_ASYNC_EVENT_FW_IMAGE_LOAD) { 3888 return 0; 3889 } 3890 3891 event.bits.async_event_type = SPDK_NVME_ASYNC_EVENT_TYPE_ERROR; 3892 event.bits.log_page_identifier = SPDK_NVME_LOG_ERROR; 3893 event.bits.async_event_info = info; 3894 3895 return nvmf_ctrlr_async_event_notification(ctrlr, &event); 3896 } 3897 3898 void 3899 nvmf_qpair_free_aer(struct spdk_nvmf_qpair *qpair) 3900 { 3901 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 3902 int i; 3903 3904 if (!nvmf_qpair_is_admin_queue(qpair)) { 3905 return; 3906 } 3907 3908 assert(spdk_get_thread() == ctrlr->thread); 3909 3910 for (i = 0; i < ctrlr->nr_aer_reqs; i++) { 3911 spdk_nvmf_request_free(ctrlr->aer_req[i]); 3912 ctrlr->aer_req[i] = NULL; 3913 } 3914 3915 ctrlr->nr_aer_reqs = 0; 3916 } 3917 3918 void 3919 spdk_nvmf_ctrlr_abort_aer(struct spdk_nvmf_ctrlr *ctrlr) 3920 { 3921 struct spdk_nvmf_request *req; 3922 int i; 3923 3924 assert(spdk_get_thread() == ctrlr->thread); 3925 3926 if (!ctrlr->nr_aer_reqs) { 3927 return; 3928 } 3929 3930 for (i = 0; i < ctrlr->nr_aer_reqs; i++) { 3931 req = ctrlr->aer_req[i]; 3932 3933 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3934 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_ABORTED_BY_REQUEST; 3935 _nvmf_request_complete(req); 3936 3937 ctrlr->aer_req[i] = NULL; 3938 } 3939 3940 ctrlr->nr_aer_reqs = 0; 3941 } 3942 3943 static void 3944 _nvmf_ctrlr_add_reservation_log(void *ctx) 3945 { 3946 struct spdk_nvmf_reservation_log *log = (struct spdk_nvmf_reservation_log *)ctx; 3947 struct spdk_nvmf_ctrlr *ctrlr = log->ctrlr; 3948 3949 ctrlr->log_page_count++; 3950 3951 /* Maximum number of queued log pages is 255 */ 3952 if (ctrlr->num_avail_log_pages == 0xff) { 3953 struct spdk_nvmf_reservation_log *entry; 3954 entry = TAILQ_LAST(&ctrlr->log_head, log_page_head); 3955 entry->log.log_page_count = ctrlr->log_page_count; 3956 free(log); 3957 return; 3958 } 3959 3960 log->log.log_page_count = ctrlr->log_page_count; 3961 log->log.num_avail_log_pages = ctrlr->num_avail_log_pages++; 3962 TAILQ_INSERT_TAIL(&ctrlr->log_head, log, link); 3963 3964 nvmf_ctrlr_async_event_reservation_notification(ctrlr); 3965 } 3966 3967 void 3968 nvmf_ctrlr_reservation_notice_log(struct spdk_nvmf_ctrlr *ctrlr, 3969 struct spdk_nvmf_ns *ns, 3970 enum spdk_nvme_reservation_notification_log_page_type type) 3971 { 3972 struct spdk_nvmf_reservation_log *log; 3973 3974 switch (type) { 3975 case SPDK_NVME_RESERVATION_LOG_PAGE_EMPTY: 3976 return; 3977 case SPDK_NVME_REGISTRATION_PREEMPTED: 3978 if (ns->mask & SPDK_NVME_REGISTRATION_PREEMPTED_MASK) { 3979 return; 3980 } 3981 break; 3982 case SPDK_NVME_RESERVATION_RELEASED: 3983 if (ns->mask & SPDK_NVME_RESERVATION_RELEASED_MASK) { 3984 return; 3985 } 3986 break; 3987 case SPDK_NVME_RESERVATION_PREEMPTED: 3988 if (ns->mask & SPDK_NVME_RESERVATION_PREEMPTED_MASK) { 3989 return; 3990 } 3991 break; 3992 default: 3993 return; 3994 } 3995 3996 log = calloc(1, sizeof(*log)); 3997 if (!log) { 3998 SPDK_ERRLOG("Alloc log page failed, ignore the log\n"); 3999 return; 4000 } 4001 log->ctrlr = ctrlr; 4002 log->log.type = type; 4003 log->log.nsid = ns->nsid; 4004 4005 spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_add_reservation_log, log); 4006 } 4007 4008 /* Check from subsystem poll group's namespace information data structure */ 4009 static bool 4010 nvmf_ns_info_ctrlr_is_registrant(struct spdk_nvmf_subsystem_pg_ns_info *ns_info, 4011 struct spdk_nvmf_ctrlr *ctrlr) 4012 { 4013 uint32_t i; 4014 4015 for (i = 0; i < SPDK_NVMF_MAX_NUM_REGISTRANTS; i++) { 4016 if (!spdk_uuid_compare(&ns_info->reg_hostid[i], &ctrlr->hostid)) { 4017 return true; 4018 } 4019 } 4020 4021 return false; 4022 } 4023 4024 /* 4025 * Check the NVMe command is permitted or not for current controller(Host). 4026 */ 4027 static int 4028 nvmf_ns_reservation_request_check(struct spdk_nvmf_subsystem_pg_ns_info *ns_info, 4029 struct spdk_nvmf_ctrlr *ctrlr, 4030 struct spdk_nvmf_request *req) 4031 { 4032 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 4033 enum spdk_nvme_reservation_type rtype = ns_info->rtype; 4034 uint8_t status = SPDK_NVME_SC_SUCCESS; 4035 uint8_t racqa; 4036 bool is_registrant; 4037 4038 /* No valid reservation */ 4039 if (!rtype) { 4040 return 0; 4041 } 4042 4043 is_registrant = nvmf_ns_info_ctrlr_is_registrant(ns_info, ctrlr); 4044 /* All registrants type and current ctrlr is a valid registrant */ 4045 if ((rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS || 4046 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && is_registrant) { 4047 return 0; 4048 } else if (!spdk_uuid_compare(&ns_info->holder_id, &ctrlr->hostid)) { 4049 return 0; 4050 } 4051 4052 /* Non-holder for current controller */ 4053 switch (cmd->opc) { 4054 case SPDK_NVME_OPC_READ: 4055 case SPDK_NVME_OPC_COMPARE: 4056 if (rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) { 4057 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 4058 goto exit; 4059 } 4060 if ((rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY || 4061 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) && !is_registrant) { 4062 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 4063 } 4064 break; 4065 case SPDK_NVME_OPC_FLUSH: 4066 case SPDK_NVME_OPC_WRITE: 4067 case SPDK_NVME_OPC_WRITE_UNCORRECTABLE: 4068 case SPDK_NVME_OPC_WRITE_ZEROES: 4069 case SPDK_NVME_OPC_DATASET_MANAGEMENT: 4070 if (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE || 4071 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) { 4072 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 4073 goto exit; 4074 } 4075 if (!is_registrant) { 4076 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 4077 } 4078 break; 4079 case SPDK_NVME_OPC_RESERVATION_ACQUIRE: 4080 racqa = cmd->cdw10_bits.resv_acquire.racqa; 4081 if (racqa == SPDK_NVME_RESERVE_ACQUIRE) { 4082 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 4083 goto exit; 4084 } 4085 if (!is_registrant) { 4086 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 4087 } 4088 break; 4089 case SPDK_NVME_OPC_RESERVATION_RELEASE: 4090 if (!is_registrant) { 4091 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 4092 } 4093 break; 4094 default: 4095 break; 4096 } 4097 4098 exit: 4099 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 4100 req->rsp->nvme_cpl.status.sc = status; 4101 if (status == SPDK_NVME_SC_RESERVATION_CONFLICT) { 4102 return -EPERM; 4103 } 4104 4105 return 0; 4106 } 4107 4108 static int 4109 nvmf_ctrlr_process_io_fused_cmd(struct spdk_nvmf_request *req, struct spdk_bdev *bdev, 4110 struct spdk_bdev_desc *desc, struct spdk_io_channel *ch) 4111 { 4112 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 4113 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 4114 struct spdk_nvmf_request *first_fused_req = req->qpair->first_fused_req; 4115 int rc; 4116 4117 if (cmd->fuse == SPDK_NVME_CMD_FUSE_FIRST) { 4118 /* first fused operation (should be compare) */ 4119 if (first_fused_req != NULL) { 4120 struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl; 4121 4122 SPDK_ERRLOG("Wrong sequence of fused operations\n"); 4123 4124 /* abort req->qpair->first_fused_request and continue with new fused command */ 4125 fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED; 4126 fused_response->status.sct = SPDK_NVME_SCT_GENERIC; 4127 _nvmf_request_complete(first_fused_req); 4128 } else if (cmd->opc != SPDK_NVME_OPC_COMPARE) { 4129 SPDK_ERRLOG("Wrong op code of fused operations\n"); 4130 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 4131 rsp->status.sc = SPDK_NVME_SC_INVALID_OPCODE; 4132 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 4133 } 4134 4135 req->qpair->first_fused_req = req; 4136 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 4137 } else if (cmd->fuse == SPDK_NVME_CMD_FUSE_SECOND) { 4138 /* second fused operation (should be write) */ 4139 if (first_fused_req == NULL) { 4140 SPDK_ERRLOG("Wrong sequence of fused operations\n"); 4141 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 4142 rsp->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED; 4143 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 4144 } else if (cmd->opc != SPDK_NVME_OPC_WRITE) { 4145 struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl; 4146 4147 SPDK_ERRLOG("Wrong op code of fused operations\n"); 4148 4149 /* abort req->qpair->first_fused_request and fail current command */ 4150 fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED; 4151 fused_response->status.sct = SPDK_NVME_SCT_GENERIC; 4152 _nvmf_request_complete(first_fused_req); 4153 4154 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 4155 rsp->status.sc = SPDK_NVME_SC_INVALID_OPCODE; 4156 req->qpair->first_fused_req = NULL; 4157 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 4158 } 4159 4160 /* save request of first command to generate response later */ 4161 req->first_fused_req = first_fused_req; 4162 req->qpair->first_fused_req = NULL; 4163 } else { 4164 SPDK_ERRLOG("Invalid fused command fuse field.\n"); 4165 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 4166 rsp->status.sc = SPDK_NVME_SC_INVALID_FIELD; 4167 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 4168 } 4169 4170 rc = nvmf_bdev_ctrlr_compare_and_write_cmd(bdev, desc, ch, req->first_fused_req, req); 4171 4172 if (rc == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 4173 if (spdk_nvme_cpl_is_error(rsp)) { 4174 struct spdk_nvme_cpl *fused_response = &first_fused_req->rsp->nvme_cpl; 4175 4176 fused_response->status = rsp->status; 4177 rsp->status.sct = SPDK_NVME_SCT_GENERIC; 4178 rsp->status.sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED; 4179 /* Complete first of fused commands. Second will be completed by upper layer */ 4180 _nvmf_request_complete(first_fused_req); 4181 req->first_fused_req = NULL; 4182 } 4183 } 4184 4185 return rc; 4186 } 4187 4188 bool 4189 nvmf_ctrlr_use_zcopy(struct spdk_nvmf_request *req) 4190 { 4191 struct spdk_nvmf_transport *transport = req->qpair->transport; 4192 struct spdk_nvmf_ns *ns; 4193 4194 assert(req->zcopy_phase == NVMF_ZCOPY_PHASE_NONE); 4195 4196 if (!transport->opts.zcopy) { 4197 return false; 4198 } 4199 4200 if (nvmf_qpair_is_admin_queue(req->qpair)) { 4201 /* Admin queue */ 4202 return false; 4203 } 4204 4205 if ((req->cmd->nvme_cmd.opc != SPDK_NVME_OPC_WRITE) && 4206 (req->cmd->nvme_cmd.opc != SPDK_NVME_OPC_READ)) { 4207 /* Not a READ or WRITE command */ 4208 return false; 4209 } 4210 4211 if (req->cmd->nvme_cmd.fuse != SPDK_NVME_CMD_FUSE_NONE) { 4212 /* Fused commands dont use zcopy buffers */ 4213 return false; 4214 } 4215 4216 ns = _nvmf_subsystem_get_ns(req->qpair->ctrlr->subsys, req->cmd->nvme_cmd.nsid); 4217 if (ns == NULL || ns->bdev == NULL || !ns->zcopy) { 4218 return false; 4219 } 4220 4221 req->zcopy_phase = NVMF_ZCOPY_PHASE_INIT; 4222 return true; 4223 } 4224 4225 void 4226 spdk_nvmf_request_zcopy_start(struct spdk_nvmf_request *req) 4227 { 4228 assert(req->zcopy_phase == NVMF_ZCOPY_PHASE_INIT); 4229 4230 /* Set iovcnt to be the maximum number of iovs that the ZCOPY can use */ 4231 req->iovcnt = NVMF_REQ_MAX_BUFFERS; 4232 4233 spdk_nvmf_request_exec(req); 4234 } 4235 4236 void 4237 spdk_nvmf_request_zcopy_end(struct spdk_nvmf_request *req, bool commit) 4238 { 4239 assert(req->zcopy_phase == NVMF_ZCOPY_PHASE_EXECUTE); 4240 req->zcopy_phase = NVMF_ZCOPY_PHASE_END_PENDING; 4241 4242 nvmf_bdev_ctrlr_zcopy_end(req, commit); 4243 } 4244 4245 int 4246 nvmf_ctrlr_process_io_cmd(struct spdk_nvmf_request *req) 4247 { 4248 uint32_t nsid; 4249 struct spdk_nvmf_ns *ns; 4250 struct spdk_bdev *bdev; 4251 struct spdk_bdev_desc *desc; 4252 struct spdk_io_channel *ch; 4253 struct spdk_nvmf_poll_group *group = req->qpair->group; 4254 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 4255 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 4256 struct spdk_nvme_cpl *response = &req->rsp->nvme_cpl; 4257 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 4258 enum spdk_nvme_ana_state ana_state; 4259 4260 /* pre-set response details for this command */ 4261 response->status.sc = SPDK_NVME_SC_SUCCESS; 4262 nsid = cmd->nsid; 4263 4264 if (spdk_unlikely(ctrlr == NULL)) { 4265 SPDK_ERRLOG("I/O command sent before CONNECT\n"); 4266 response->status.sct = SPDK_NVME_SCT_GENERIC; 4267 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 4268 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 4269 } 4270 4271 if (spdk_unlikely(ctrlr->vcprop.cc.bits.en != 1)) { 4272 SPDK_ERRLOG("I/O command sent to disabled controller\n"); 4273 response->status.sct = SPDK_NVME_SCT_GENERIC; 4274 response->status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 4275 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 4276 } 4277 4278 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid); 4279 if (ns == NULL || ns->bdev == NULL) { 4280 SPDK_DEBUGLOG(nvmf, "Unsuccessful query for nsid %u\n", cmd->nsid); 4281 response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 4282 response->status.dnr = 1; 4283 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 4284 } 4285 4286 ana_state = nvmf_ctrlr_get_ana_state(ctrlr, ns->anagrpid); 4287 if (spdk_unlikely(ana_state != SPDK_NVME_ANA_OPTIMIZED_STATE && 4288 ana_state != SPDK_NVME_ANA_NON_OPTIMIZED_STATE)) { 4289 SPDK_DEBUGLOG(nvmf, "Fail I/O command due to ANA state %d\n", 4290 ana_state); 4291 response->status.sct = SPDK_NVME_SCT_PATH; 4292 response->status.sc = _nvme_ana_state_to_path_status(ana_state); 4293 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 4294 } 4295 4296 if (spdk_likely(ctrlr->listener != NULL)) { 4297 SPDK_DTRACE_PROBE3_TICKS(nvmf_request_io_exec_path, req, 4298 ctrlr->listener->trid->traddr, 4299 ctrlr->listener->trid->trsvcid); 4300 } 4301 4302 /* scan-build falsely reporting dereference of null pointer */ 4303 assert(group != NULL && group->sgroups != NULL); 4304 ns_info = &group->sgroups[ctrlr->subsys->id].ns_info[nsid - 1]; 4305 if (nvmf_ns_reservation_request_check(ns_info, ctrlr, req)) { 4306 SPDK_DEBUGLOG(nvmf, "Reservation Conflict for nsid %u, opcode %u\n", 4307 cmd->nsid, cmd->opc); 4308 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 4309 } 4310 4311 bdev = ns->bdev; 4312 desc = ns->desc; 4313 ch = ns_info->channel; 4314 4315 if (spdk_unlikely(cmd->fuse & SPDK_NVME_CMD_FUSE_MASK)) { 4316 return nvmf_ctrlr_process_io_fused_cmd(req, bdev, desc, ch); 4317 } else if (spdk_unlikely(req->qpair->first_fused_req != NULL)) { 4318 struct spdk_nvme_cpl *fused_response = &req->qpair->first_fused_req->rsp->nvme_cpl; 4319 4320 SPDK_ERRLOG("Expected second of fused commands - failing first of fused commands\n"); 4321 4322 /* abort req->qpair->first_fused_request and continue with new command */ 4323 fused_response->status.sc = SPDK_NVME_SC_ABORTED_MISSING_FUSED; 4324 fused_response->status.sct = SPDK_NVME_SCT_GENERIC; 4325 _nvmf_request_complete(req->qpair->first_fused_req); 4326 req->qpair->first_fused_req = NULL; 4327 } 4328 4329 if (spdk_nvmf_request_using_zcopy(req)) { 4330 assert(req->zcopy_phase == NVMF_ZCOPY_PHASE_INIT); 4331 return nvmf_bdev_ctrlr_zcopy_start(bdev, desc, ch, req); 4332 } else { 4333 switch (cmd->opc) { 4334 case SPDK_NVME_OPC_READ: 4335 return nvmf_bdev_ctrlr_read_cmd(bdev, desc, ch, req); 4336 case SPDK_NVME_OPC_WRITE: 4337 return nvmf_bdev_ctrlr_write_cmd(bdev, desc, ch, req); 4338 case SPDK_NVME_OPC_COMPARE: 4339 return nvmf_bdev_ctrlr_compare_cmd(bdev, desc, ch, req); 4340 case SPDK_NVME_OPC_WRITE_ZEROES: 4341 return nvmf_bdev_ctrlr_write_zeroes_cmd(bdev, desc, ch, req); 4342 case SPDK_NVME_OPC_FLUSH: 4343 return nvmf_bdev_ctrlr_flush_cmd(bdev, desc, ch, req); 4344 case SPDK_NVME_OPC_DATASET_MANAGEMENT: 4345 return nvmf_bdev_ctrlr_dsm_cmd(bdev, desc, ch, req); 4346 case SPDK_NVME_OPC_RESERVATION_REGISTER: 4347 case SPDK_NVME_OPC_RESERVATION_ACQUIRE: 4348 case SPDK_NVME_OPC_RESERVATION_RELEASE: 4349 case SPDK_NVME_OPC_RESERVATION_REPORT: 4350 spdk_thread_send_msg(ctrlr->subsys->thread, nvmf_ns_reservation_request, req); 4351 return SPDK_NVMF_REQUEST_EXEC_STATUS_ASYNCHRONOUS; 4352 case SPDK_NVME_OPC_COPY: 4353 return nvmf_bdev_ctrlr_copy_cmd(bdev, desc, ch, req); 4354 default: 4355 return nvmf_bdev_ctrlr_nvme_passthru_io(bdev, desc, ch, req); 4356 } 4357 } 4358 } 4359 4360 static void 4361 nvmf_qpair_request_cleanup(struct spdk_nvmf_qpair *qpair) 4362 { 4363 if (qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING) { 4364 assert(qpair->state_cb != NULL); 4365 4366 if (TAILQ_EMPTY(&qpair->outstanding)) { 4367 qpair->state_cb(qpair->state_cb_arg, 0); 4368 } 4369 } 4370 } 4371 4372 int 4373 spdk_nvmf_request_free(struct spdk_nvmf_request *req) 4374 { 4375 struct spdk_nvmf_qpair *qpair = req->qpair; 4376 4377 TAILQ_REMOVE(&qpair->outstanding, req, link); 4378 if (nvmf_transport_req_free(req)) { 4379 SPDK_ERRLOG("Unable to free transport level request resources.\n"); 4380 } 4381 4382 nvmf_qpair_request_cleanup(qpair); 4383 4384 return 0; 4385 } 4386 4387 static void 4388 _nvmf_request_complete(void *ctx) 4389 { 4390 struct spdk_nvmf_request *req = ctx; 4391 struct spdk_nvme_cpl *rsp = &req->rsp->nvme_cpl; 4392 struct spdk_nvmf_qpair *qpair; 4393 struct spdk_nvmf_subsystem_poll_group *sgroup = NULL; 4394 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 4395 bool is_aer = false; 4396 uint32_t nsid; 4397 bool paused; 4398 uint8_t opcode; 4399 4400 rsp->sqid = 0; 4401 rsp->status.p = 0; 4402 rsp->cid = req->cmd->nvme_cmd.cid; 4403 nsid = req->cmd->nvme_cmd.nsid; 4404 opcode = req->cmd->nvmf_cmd.opcode; 4405 4406 qpair = req->qpair; 4407 if (qpair->ctrlr) { 4408 sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id]; 4409 assert(sgroup != NULL); 4410 is_aer = req->cmd->nvme_cmd.opc == SPDK_NVME_OPC_ASYNC_EVENT_REQUEST; 4411 if (spdk_likely(qpair->qid != 0)) { 4412 qpair->group->stat.completed_nvme_io++; 4413 } 4414 4415 /* 4416 * Set the crd value. 4417 * If the the IO has any error, and dnr (DoNotRetry) is not 1, 4418 * and ACRE is enabled, we will set the crd to 1 to select the first CRDT. 4419 */ 4420 if (spdk_nvme_cpl_is_error(rsp) && 4421 rsp->status.dnr == 0 && 4422 qpair->ctrlr->acre_enabled) { 4423 rsp->status.crd = 1; 4424 } 4425 } else if (spdk_unlikely(nvmf_request_is_fabric_connect(req))) { 4426 sgroup = nvmf_subsystem_pg_from_connect_cmd(req); 4427 } 4428 4429 if (SPDK_DEBUGLOG_FLAG_ENABLED("nvmf")) { 4430 spdk_nvme_print_completion(qpair->qid, rsp); 4431 } 4432 4433 switch (req->zcopy_phase) { 4434 case NVMF_ZCOPY_PHASE_NONE: 4435 TAILQ_REMOVE(&qpair->outstanding, req, link); 4436 break; 4437 case NVMF_ZCOPY_PHASE_INIT: 4438 if (spdk_unlikely(spdk_nvme_cpl_is_error(rsp))) { 4439 req->zcopy_phase = NVMF_ZCOPY_PHASE_INIT_FAILED; 4440 TAILQ_REMOVE(&qpair->outstanding, req, link); 4441 } else { 4442 req->zcopy_phase = NVMF_ZCOPY_PHASE_EXECUTE; 4443 } 4444 break; 4445 case NVMF_ZCOPY_PHASE_EXECUTE: 4446 break; 4447 case NVMF_ZCOPY_PHASE_END_PENDING: 4448 TAILQ_REMOVE(&qpair->outstanding, req, link); 4449 req->zcopy_phase = NVMF_ZCOPY_PHASE_COMPLETE; 4450 break; 4451 default: 4452 SPDK_ERRLOG("Invalid ZCOPY phase %u\n", req->zcopy_phase); 4453 break; 4454 } 4455 4456 if (nvmf_transport_req_complete(req)) { 4457 SPDK_ERRLOG("Transport request completion error!\n"); 4458 } 4459 4460 /* AER cmd is an exception */ 4461 if (sgroup && !is_aer) { 4462 if (spdk_unlikely(opcode == SPDK_NVME_OPC_FABRIC || 4463 nvmf_qpair_is_admin_queue(qpair))) { 4464 assert(sgroup->mgmt_io_outstanding > 0); 4465 sgroup->mgmt_io_outstanding--; 4466 } else { 4467 if (req->zcopy_phase == NVMF_ZCOPY_PHASE_NONE || 4468 req->zcopy_phase == NVMF_ZCOPY_PHASE_COMPLETE || 4469 req->zcopy_phase == NVMF_ZCOPY_PHASE_INIT_FAILED) { 4470 /* End of request */ 4471 4472 /* NOTE: This implicitly also checks for 0, since 0 - 1 wraps around to UINT32_MAX. */ 4473 if (spdk_likely(nsid - 1 < sgroup->num_ns)) { 4474 sgroup->ns_info[nsid - 1].io_outstanding--; 4475 } 4476 } 4477 } 4478 4479 if (spdk_unlikely(sgroup->state == SPDK_NVMF_SUBSYSTEM_PAUSING && 4480 sgroup->mgmt_io_outstanding == 0)) { 4481 paused = true; 4482 for (nsid = 0; nsid < sgroup->num_ns; nsid++) { 4483 ns_info = &sgroup->ns_info[nsid]; 4484 4485 if (ns_info->state == SPDK_NVMF_SUBSYSTEM_PAUSING && 4486 ns_info->io_outstanding > 0) { 4487 paused = false; 4488 break; 4489 } 4490 } 4491 4492 if (paused) { 4493 sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSED; 4494 sgroup->cb_fn(sgroup->cb_arg, 0); 4495 sgroup->cb_fn = NULL; 4496 sgroup->cb_arg = NULL; 4497 } 4498 } 4499 4500 } 4501 4502 nvmf_qpair_request_cleanup(qpair); 4503 } 4504 4505 int 4506 spdk_nvmf_request_complete(struct spdk_nvmf_request *req) 4507 { 4508 struct spdk_nvmf_qpair *qpair = req->qpair; 4509 4510 spdk_thread_exec_msg(qpair->group->thread, _nvmf_request_complete, req); 4511 4512 return 0; 4513 } 4514 4515 void 4516 spdk_nvmf_request_exec_fabrics(struct spdk_nvmf_request *req) 4517 { 4518 struct spdk_nvmf_qpair *qpair = req->qpair; 4519 struct spdk_nvmf_subsystem_poll_group *sgroup = NULL; 4520 enum spdk_nvmf_request_exec_status status; 4521 4522 if (qpair->ctrlr) { 4523 sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id]; 4524 } else if (spdk_unlikely(nvmf_request_is_fabric_connect(req))) { 4525 sgroup = nvmf_subsystem_pg_from_connect_cmd(req); 4526 } 4527 4528 assert(sgroup != NULL); 4529 sgroup->mgmt_io_outstanding++; 4530 4531 /* Place the request on the outstanding list so we can keep track of it */ 4532 TAILQ_INSERT_TAIL(&qpair->outstanding, req, link); 4533 4534 assert(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC); 4535 status = nvmf_ctrlr_process_fabrics_cmd(req); 4536 4537 if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 4538 _nvmf_request_complete(req); 4539 } 4540 } 4541 4542 static bool 4543 nvmf_check_subsystem_active(struct spdk_nvmf_request *req) 4544 { 4545 struct spdk_nvmf_qpair *qpair = req->qpair; 4546 struct spdk_nvmf_subsystem_poll_group *sgroup = NULL; 4547 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 4548 uint32_t nsid; 4549 4550 if (qpair->ctrlr) { 4551 sgroup = &qpair->group->sgroups[qpair->ctrlr->subsys->id]; 4552 assert(sgroup != NULL); 4553 } else if (spdk_unlikely(nvmf_request_is_fabric_connect(req))) { 4554 sgroup = nvmf_subsystem_pg_from_connect_cmd(req); 4555 } 4556 4557 /* Check if the subsystem is paused (if there is a subsystem) */ 4558 if (sgroup != NULL) { 4559 if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC || 4560 nvmf_qpair_is_admin_queue(qpair))) { 4561 if (sgroup->state != SPDK_NVMF_SUBSYSTEM_ACTIVE) { 4562 /* The subsystem is not currently active. Queue this request. */ 4563 TAILQ_INSERT_TAIL(&sgroup->queued, req, link); 4564 return false; 4565 } 4566 sgroup->mgmt_io_outstanding++; 4567 } else { 4568 nsid = req->cmd->nvme_cmd.nsid; 4569 4570 /* NOTE: This implicitly also checks for 0, since 0 - 1 wraps around to UINT32_MAX. */ 4571 if (spdk_unlikely(nsid - 1 >= sgroup->num_ns)) { 4572 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 4573 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 4574 req->rsp->nvme_cpl.status.dnr = 1; 4575 TAILQ_INSERT_TAIL(&qpair->outstanding, req, link); 4576 _nvmf_request_complete(req); 4577 return false; 4578 } 4579 4580 ns_info = &sgroup->ns_info[nsid - 1]; 4581 if (ns_info->channel == NULL) { 4582 /* This can can happen if host sends I/O to a namespace that is 4583 * in the process of being added, but before the full addition 4584 * process is complete. Report invalid namespace in that case. 4585 */ 4586 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 4587 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 4588 req->rsp->nvme_cpl.status.dnr = 1; 4589 TAILQ_INSERT_TAIL(&qpair->outstanding, req, link); 4590 ns_info->io_outstanding++; 4591 _nvmf_request_complete(req); 4592 return false; 4593 } 4594 4595 if (ns_info->state != SPDK_NVMF_SUBSYSTEM_ACTIVE) { 4596 /* The namespace is not currently active. Queue this request. */ 4597 TAILQ_INSERT_TAIL(&sgroup->queued, req, link); 4598 return false; 4599 } 4600 4601 ns_info->io_outstanding++; 4602 } 4603 4604 if (qpair->state != SPDK_NVMF_QPAIR_ACTIVE) { 4605 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 4606 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_COMMAND_SEQUENCE_ERROR; 4607 TAILQ_INSERT_TAIL(&qpair->outstanding, req, link); 4608 _nvmf_request_complete(req); 4609 return false; 4610 } 4611 } 4612 4613 return true; 4614 } 4615 4616 void 4617 spdk_nvmf_request_exec(struct spdk_nvmf_request *req) 4618 { 4619 struct spdk_nvmf_qpair *qpair = req->qpair; 4620 struct spdk_nvmf_transport *transport = qpair->transport; 4621 enum spdk_nvmf_request_exec_status status; 4622 4623 if (!nvmf_check_subsystem_active(req)) { 4624 return; 4625 } 4626 4627 if (SPDK_DEBUGLOG_FLAG_ENABLED("nvmf")) { 4628 spdk_nvme_print_command(qpair->qid, &req->cmd->nvme_cmd); 4629 } 4630 4631 /* Place the request on the outstanding list so we can keep track of it */ 4632 TAILQ_INSERT_TAIL(&qpair->outstanding, req, link); 4633 4634 if (spdk_unlikely((req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC) && 4635 spdk_nvme_trtype_is_fabrics(transport->ops->type))) { 4636 status = nvmf_ctrlr_process_fabrics_cmd(req); 4637 } else if (spdk_unlikely(nvmf_qpair_is_admin_queue(qpair))) { 4638 status = nvmf_ctrlr_process_admin_cmd(req); 4639 } else { 4640 status = nvmf_ctrlr_process_io_cmd(req); 4641 } 4642 4643 if (status == SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE) { 4644 _nvmf_request_complete(req); 4645 } 4646 } 4647 4648 static bool 4649 nvmf_ctrlr_get_dif_ctx(struct spdk_nvmf_ctrlr *ctrlr, struct spdk_nvme_cmd *cmd, 4650 struct spdk_dif_ctx *dif_ctx) 4651 { 4652 struct spdk_nvmf_ns *ns; 4653 struct spdk_bdev *bdev; 4654 4655 if (ctrlr == NULL || cmd == NULL) { 4656 return false; 4657 } 4658 4659 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, cmd->nsid); 4660 if (ns == NULL || ns->bdev == NULL) { 4661 return false; 4662 } 4663 4664 bdev = ns->bdev; 4665 4666 switch (cmd->opc) { 4667 case SPDK_NVME_OPC_READ: 4668 case SPDK_NVME_OPC_WRITE: 4669 case SPDK_NVME_OPC_COMPARE: 4670 return nvmf_bdev_ctrlr_get_dif_ctx(bdev, cmd, dif_ctx); 4671 default: 4672 break; 4673 } 4674 4675 return false; 4676 } 4677 4678 bool 4679 spdk_nvmf_request_get_dif_ctx(struct spdk_nvmf_request *req, struct spdk_dif_ctx *dif_ctx) 4680 { 4681 struct spdk_nvmf_qpair *qpair = req->qpair; 4682 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 4683 4684 if (spdk_likely(ctrlr == NULL || !ctrlr->dif_insert_or_strip)) { 4685 return false; 4686 } 4687 4688 if (spdk_unlikely(qpair->state != SPDK_NVMF_QPAIR_ACTIVE)) { 4689 return false; 4690 } 4691 4692 if (spdk_unlikely(req->cmd->nvmf_cmd.opcode == SPDK_NVME_OPC_FABRIC)) { 4693 return false; 4694 } 4695 4696 if (spdk_unlikely(nvmf_qpair_is_admin_queue(qpair))) { 4697 return false; 4698 } 4699 4700 return nvmf_ctrlr_get_dif_ctx(ctrlr, &req->cmd->nvme_cmd, dif_ctx); 4701 } 4702 4703 void 4704 spdk_nvmf_set_custom_admin_cmd_hdlr(uint8_t opc, spdk_nvmf_custom_cmd_hdlr hdlr) 4705 { 4706 g_nvmf_custom_admin_cmd_hdlrs[opc].hdlr = hdlr; 4707 } 4708 4709 static int 4710 nvmf_passthru_admin_cmd_for_bdev_nsid(struct spdk_nvmf_request *req, uint32_t bdev_nsid) 4711 { 4712 struct spdk_bdev *bdev; 4713 struct spdk_bdev_desc *desc; 4714 struct spdk_io_channel *ch; 4715 struct spdk_nvme_cpl *response = spdk_nvmf_request_get_response(req); 4716 int rc; 4717 4718 rc = spdk_nvmf_request_get_bdev(bdev_nsid, req, &bdev, &desc, &ch); 4719 if (rc) { 4720 response->status.sct = SPDK_NVME_SCT_GENERIC; 4721 response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 4722 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 4723 } 4724 return spdk_nvmf_bdev_ctrlr_nvme_passthru_admin(bdev, desc, ch, req, NULL); 4725 } 4726 4727 static int 4728 nvmf_passthru_admin_cmd(struct spdk_nvmf_request *req) 4729 { 4730 struct spdk_nvme_cmd *cmd = spdk_nvmf_request_get_cmd(req); 4731 uint32_t bdev_nsid; 4732 4733 if (g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].nsid != 0) { 4734 bdev_nsid = g_nvmf_custom_admin_cmd_hdlrs[cmd->opc].nsid; 4735 } else { 4736 bdev_nsid = cmd->nsid; 4737 } 4738 4739 return nvmf_passthru_admin_cmd_for_bdev_nsid(req, bdev_nsid); 4740 } 4741 4742 int 4743 nvmf_passthru_admin_cmd_for_ctrlr(struct spdk_nvmf_request *req, struct spdk_nvmf_ctrlr *ctrlr) 4744 { 4745 struct spdk_nvme_cpl *response = spdk_nvmf_request_get_response(req); 4746 struct spdk_nvmf_ns *ns; 4747 4748 ns = spdk_nvmf_subsystem_get_first_ns(ctrlr->subsys); 4749 if (ns == NULL) { 4750 /* Is there a better sc to use here? */ 4751 response->status.sct = SPDK_NVME_SCT_GENERIC; 4752 response->status.sc = SPDK_NVME_SC_INVALID_NAMESPACE_OR_FORMAT; 4753 return SPDK_NVMF_REQUEST_EXEC_STATUS_COMPLETE; 4754 } 4755 4756 return nvmf_passthru_admin_cmd_for_bdev_nsid(req, ns->nsid); 4757 } 4758 4759 void 4760 spdk_nvmf_set_passthru_admin_cmd(uint8_t opc, uint32_t forward_nsid) 4761 { 4762 g_nvmf_custom_admin_cmd_hdlrs[opc].hdlr = nvmf_passthru_admin_cmd; 4763 g_nvmf_custom_admin_cmd_hdlrs[opc].nsid = forward_nsid; 4764 } 4765 4766 int 4767 spdk_nvmf_request_get_bdev(uint32_t nsid, struct spdk_nvmf_request *req, 4768 struct spdk_bdev **bdev, struct spdk_bdev_desc **desc, struct spdk_io_channel **ch) 4769 { 4770 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 4771 struct spdk_nvmf_ns *ns; 4772 struct spdk_nvmf_poll_group *group = req->qpair->group; 4773 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 4774 4775 *bdev = NULL; 4776 *desc = NULL; 4777 *ch = NULL; 4778 4779 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid); 4780 if (ns == NULL || ns->bdev == NULL) { 4781 return -EINVAL; 4782 } 4783 4784 assert(group != NULL && group->sgroups != NULL); 4785 ns_info = &group->sgroups[ctrlr->subsys->id].ns_info[nsid - 1]; 4786 *bdev = ns->bdev; 4787 *desc = ns->desc; 4788 *ch = ns_info->channel; 4789 4790 return 0; 4791 } 4792 4793 struct spdk_nvmf_ctrlr *spdk_nvmf_request_get_ctrlr(struct spdk_nvmf_request *req) 4794 { 4795 return req->qpair->ctrlr; 4796 } 4797 4798 struct spdk_nvme_cmd *spdk_nvmf_request_get_cmd(struct spdk_nvmf_request *req) 4799 { 4800 return &req->cmd->nvme_cmd; 4801 } 4802 4803 struct spdk_nvme_cpl *spdk_nvmf_request_get_response(struct spdk_nvmf_request *req) 4804 { 4805 return &req->rsp->nvme_cpl; 4806 } 4807 4808 struct spdk_nvmf_subsystem *spdk_nvmf_request_get_subsystem(struct spdk_nvmf_request *req) 4809 { 4810 return req->qpair->ctrlr->subsys; 4811 } 4812 4813 size_t 4814 spdk_nvmf_request_copy_from_buf(struct spdk_nvmf_request *req, 4815 void *buf, size_t buflen) 4816 { 4817 struct spdk_iov_xfer ix; 4818 4819 spdk_iov_xfer_init(&ix, req->iov, req->iovcnt); 4820 return spdk_iov_xfer_from_buf(&ix, buf, buflen); 4821 } 4822 4823 size_t 4824 spdk_nvmf_request_copy_to_buf(struct spdk_nvmf_request *req, 4825 void *buf, size_t buflen) 4826 { 4827 struct spdk_iov_xfer ix; 4828 4829 spdk_iov_xfer_init(&ix, req->iov, req->iovcnt); 4830 return spdk_iov_xfer_to_buf(&ix, buf, buflen); 4831 } 4832 4833 struct spdk_nvmf_subsystem *spdk_nvmf_ctrlr_get_subsystem(struct spdk_nvmf_ctrlr *ctrlr) 4834 { 4835 return ctrlr->subsys; 4836 } 4837 4838 uint16_t 4839 spdk_nvmf_ctrlr_get_id(struct spdk_nvmf_ctrlr *ctrlr) 4840 { 4841 return ctrlr->cntlid; 4842 } 4843 4844 struct spdk_nvmf_request *spdk_nvmf_request_get_req_to_abort(struct spdk_nvmf_request *req) 4845 { 4846 return req->req_to_abort; 4847 } 4848