LCOV - code coverage report
Current view: top level - lib/nvmf - ctrlr.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 1411 2681 52.6 %
Date: 2024-12-06 15:15:04 Functions: 91 162 56.2 %

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

Generated by: LCOV version 1.15