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