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

Generated by: LCOV version 1.15