LCOV - code coverage report
Current view: top level - module/bdev/nvme - bdev_nvme.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 2265 4210 53.8 %
Date: 2024-07-12 13:55:03 Functions: 214 307 69.7 %

          Line data    Source code
       1             : /*   SPDX-License-Identifier: BSD-3-Clause
       2             :  *   Copyright (C) 2016 Intel Corporation. All rights reserved.
       3             :  *   Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
       4             :  *   Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
       5             :  *   Copyright (c) 2022 Dell Inc, or its subsidiaries. All rights reserved.
       6             :  */
       7             : 
       8             : #include "spdk/stdinc.h"
       9             : 
      10             : #include "bdev_nvme.h"
      11             : 
      12             : #include "spdk/accel.h"
      13             : #include "spdk/config.h"
      14             : #include "spdk/endian.h"
      15             : #include "spdk/bdev.h"
      16             : #include "spdk/json.h"
      17             : #include "spdk/keyring.h"
      18             : #include "spdk/likely.h"
      19             : #include "spdk/nvme.h"
      20             : #include "spdk/nvme_ocssd.h"
      21             : #include "spdk/nvme_zns.h"
      22             : #include "spdk/opal.h"
      23             : #include "spdk/thread.h"
      24             : #include "spdk/trace.h"
      25             : #include "spdk/string.h"
      26             : #include "spdk/util.h"
      27             : #include "spdk/uuid.h"
      28             : 
      29             : #include "spdk/bdev_module.h"
      30             : #include "spdk/log.h"
      31             : 
      32             : #include "spdk_internal/usdt.h"
      33             : #include "spdk_internal/trace_defs.h"
      34             : 
      35             : #define SPDK_BDEV_NVME_DEFAULT_DELAY_CMD_SUBMIT true
      36             : #define SPDK_BDEV_NVME_DEFAULT_KEEP_ALIVE_TIMEOUT_IN_MS (10000)
      37             : 
      38             : #define NSID_STR_LEN 10
      39             : 
      40             : #define SPDK_CONTROLLER_NAME_MAX 512
      41             : 
      42             : static int bdev_nvme_config_json(struct spdk_json_write_ctx *w);
      43             : 
      44             : struct nvme_bdev_io {
      45             :         /** array of iovecs to transfer. */
      46             :         struct iovec *iovs;
      47             : 
      48             :         /** Number of iovecs in iovs array. */
      49             :         int iovcnt;
      50             : 
      51             :         /** Current iovec position. */
      52             :         int iovpos;
      53             : 
      54             :         /** Offset in current iovec. */
      55             :         uint32_t iov_offset;
      56             : 
      57             :         /** I/O path the current I/O or admin passthrough is submitted on, or the I/O path
      58             :          *  being reset in a reset I/O.
      59             :          */
      60             :         struct nvme_io_path *io_path;
      61             : 
      62             :         /** array of iovecs to transfer. */
      63             :         struct iovec *fused_iovs;
      64             : 
      65             :         /** Number of iovecs in iovs array. */
      66             :         int fused_iovcnt;
      67             : 
      68             :         /** Current iovec position. */
      69             :         int fused_iovpos;
      70             : 
      71             :         /** Offset in current iovec. */
      72             :         uint32_t fused_iov_offset;
      73             : 
      74             :         /** Saved status for admin passthru completion event, PI error verification, or intermediate compare-and-write status */
      75             :         struct spdk_nvme_cpl cpl;
      76             : 
      77             :         /** Extended IO opts passed by the user to bdev layer and mapped to NVME format */
      78             :         struct spdk_nvme_ns_cmd_ext_io_opts ext_opts;
      79             : 
      80             :         /** Keeps track if first of fused commands was submitted */
      81             :         bool first_fused_submitted;
      82             : 
      83             :         /** Keeps track if first of fused commands was completed */
      84             :         bool first_fused_completed;
      85             : 
      86             :         /** Temporary pointer to zone report buffer */
      87             :         struct spdk_nvme_zns_zone_report *zone_report_buf;
      88             : 
      89             :         /** Keep track of how many zones that have been copied to the spdk_bdev_zone_info struct */
      90             :         uint64_t handled_zones;
      91             : 
      92             :         /** Expiration value in ticks to retry the current I/O. */
      93             :         uint64_t retry_ticks;
      94             : 
      95             :         /* How many times the current I/O was retried. */
      96             :         int32_t retry_count;
      97             : 
      98             :         /* Current tsc at submit time. */
      99             :         uint64_t submit_tsc;
     100             : };
     101             : 
     102             : struct nvme_probe_skip_entry {
     103             :         struct spdk_nvme_transport_id           trid;
     104             :         TAILQ_ENTRY(nvme_probe_skip_entry)      tailq;
     105             : };
     106             : /* All the controllers deleted by users via RPC are skipped by hotplug monitor */
     107             : static TAILQ_HEAD(, nvme_probe_skip_entry) g_skipped_nvme_ctrlrs = TAILQ_HEAD_INITIALIZER(
     108             :                         g_skipped_nvme_ctrlrs);
     109             : 
     110             : #define BDEV_NVME_DEFAULT_DIGESTS (SPDK_BIT(SPDK_NVMF_DHCHAP_HASH_SHA256) | \
     111             :                                    SPDK_BIT(SPDK_NVMF_DHCHAP_HASH_SHA384) | \
     112             :                                    SPDK_BIT(SPDK_NVMF_DHCHAP_HASH_SHA512))
     113             : 
     114             : #define BDEV_NVME_DEFAULT_DHGROUPS (SPDK_BIT(SPDK_NVMF_DHCHAP_DHGROUP_NULL) | \
     115             :                                     SPDK_BIT(SPDK_NVMF_DHCHAP_DHGROUP_2048) | \
     116             :                                     SPDK_BIT(SPDK_NVMF_DHCHAP_DHGROUP_3072) | \
     117             :                                     SPDK_BIT(SPDK_NVMF_DHCHAP_DHGROUP_4096) | \
     118             :                                     SPDK_BIT(SPDK_NVMF_DHCHAP_DHGROUP_6144) | \
     119             :                                     SPDK_BIT(SPDK_NVMF_DHCHAP_DHGROUP_8192))
     120             : 
     121             : static struct spdk_bdev_nvme_opts g_opts = {
     122             :         .action_on_timeout = SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE,
     123             :         .timeout_us = 0,
     124             :         .timeout_admin_us = 0,
     125             :         .keep_alive_timeout_ms = SPDK_BDEV_NVME_DEFAULT_KEEP_ALIVE_TIMEOUT_IN_MS,
     126             :         .transport_retry_count = 4,
     127             :         .arbitration_burst = 0,
     128             :         .low_priority_weight = 0,
     129             :         .medium_priority_weight = 0,
     130             :         .high_priority_weight = 0,
     131             :         .nvme_adminq_poll_period_us = 10000ULL,
     132             :         .nvme_ioq_poll_period_us = 0,
     133             :         .io_queue_requests = 0,
     134             :         .delay_cmd_submit = SPDK_BDEV_NVME_DEFAULT_DELAY_CMD_SUBMIT,
     135             :         .bdev_retry_count = 3,
     136             :         .transport_ack_timeout = 0,
     137             :         .ctrlr_loss_timeout_sec = 0,
     138             :         .reconnect_delay_sec = 0,
     139             :         .fast_io_fail_timeout_sec = 0,
     140             :         .disable_auto_failback = false,
     141             :         .generate_uuids = false,
     142             :         .transport_tos = 0,
     143             :         .nvme_error_stat = false,
     144             :         .io_path_stat = false,
     145             :         .allow_accel_sequence = false,
     146             :         .dhchap_digests = BDEV_NVME_DEFAULT_DIGESTS,
     147             :         .dhchap_dhgroups = BDEV_NVME_DEFAULT_DHGROUPS,
     148             : };
     149             : 
     150             : #define NVME_HOTPLUG_POLL_PERIOD_MAX                    10000000ULL
     151             : #define NVME_HOTPLUG_POLL_PERIOD_DEFAULT                100000ULL
     152             : 
     153             : static int g_hot_insert_nvme_controller_index = 0;
     154             : static uint64_t g_nvme_hotplug_poll_period_us = NVME_HOTPLUG_POLL_PERIOD_DEFAULT;
     155             : static bool g_nvme_hotplug_enabled = false;
     156             : struct spdk_thread *g_bdev_nvme_init_thread;
     157             : static struct spdk_poller *g_hotplug_poller;
     158             : static struct spdk_poller *g_hotplug_probe_poller;
     159             : static struct spdk_nvme_probe_ctx *g_hotplug_probe_ctx;
     160             : 
     161             : static void nvme_ctrlr_populate_namespaces(struct nvme_ctrlr *nvme_ctrlr,
     162             :                 struct nvme_async_probe_ctx *ctx);
     163             : static void nvme_ctrlr_populate_namespaces_done(struct nvme_ctrlr *nvme_ctrlr,
     164             :                 struct nvme_async_probe_ctx *ctx);
     165             : static int bdev_nvme_library_init(void);
     166             : static void bdev_nvme_library_fini(void);
     167             : static void _bdev_nvme_submit_request(struct nvme_bdev_channel *nbdev_ch,
     168             :                                       struct spdk_bdev_io *bdev_io);
     169             : static void bdev_nvme_submit_request(struct spdk_io_channel *ch,
     170             :                                      struct spdk_bdev_io *bdev_io);
     171             : static int bdev_nvme_readv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
     172             :                            void *md, uint64_t lba_count, uint64_t lba,
     173             :                            uint32_t flags, struct spdk_memory_domain *domain, void *domain_ctx,
     174             :                            struct spdk_accel_sequence *seq);
     175             : static int bdev_nvme_no_pi_readv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
     176             :                                  void *md, uint64_t lba_count, uint64_t lba);
     177             : static int bdev_nvme_writev(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
     178             :                             void *md, uint64_t lba_count, uint64_t lba,
     179             :                             uint32_t flags, struct spdk_memory_domain *domain, void *domain_ctx,
     180             :                             struct spdk_accel_sequence *seq,
     181             :                             union spdk_bdev_nvme_cdw12 cdw12, union spdk_bdev_nvme_cdw13 cdw13);
     182             : static int bdev_nvme_zone_appendv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
     183             :                                   void *md, uint64_t lba_count,
     184             :                                   uint64_t zslba, uint32_t flags);
     185             : static int bdev_nvme_comparev(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
     186             :                               void *md, uint64_t lba_count, uint64_t lba,
     187             :                               uint32_t flags);
     188             : static int bdev_nvme_comparev_and_writev(struct nvme_bdev_io *bio,
     189             :                 struct iovec *cmp_iov, int cmp_iovcnt, struct iovec *write_iov,
     190             :                 int write_iovcnt, void *md, uint64_t lba_count, uint64_t lba,
     191             :                 uint32_t flags);
     192             : static int bdev_nvme_get_zone_info(struct nvme_bdev_io *bio, uint64_t zone_id,
     193             :                                    uint32_t num_zones, struct spdk_bdev_zone_info *info);
     194             : static int bdev_nvme_zone_management(struct nvme_bdev_io *bio, uint64_t zone_id,
     195             :                                      enum spdk_bdev_zone_action action);
     196             : static void bdev_nvme_admin_passthru(struct nvme_bdev_channel *nbdev_ch,
     197             :                                      struct nvme_bdev_io *bio,
     198             :                                      struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes);
     199             : static int bdev_nvme_io_passthru(struct nvme_bdev_io *bio, struct spdk_nvme_cmd *cmd,
     200             :                                  void *buf, size_t nbytes);
     201             : static int bdev_nvme_io_passthru_md(struct nvme_bdev_io *bio, struct spdk_nvme_cmd *cmd,
     202             :                                     void *buf, size_t nbytes, void *md_buf, size_t md_len);
     203             : static int bdev_nvme_iov_passthru_md(struct nvme_bdev_io *bio, struct spdk_nvme_cmd *cmd,
     204             :                                      struct iovec *iov, int iovcnt, size_t nbytes,
     205             :                                      void *md_buf, size_t md_len);
     206             : static void bdev_nvme_abort(struct nvme_bdev_channel *nbdev_ch,
     207             :                             struct nvme_bdev_io *bio, struct nvme_bdev_io *bio_to_abort);
     208             : static void bdev_nvme_reset_io(struct nvme_bdev_channel *nbdev_ch, struct nvme_bdev_io *bio);
     209             : static int bdev_nvme_reset_ctrlr(struct nvme_ctrlr *nvme_ctrlr);
     210             : static int bdev_nvme_failover_ctrlr(struct nvme_ctrlr *nvme_ctrlr);
     211             : static void remove_cb(void *cb_ctx, struct spdk_nvme_ctrlr *ctrlr);
     212             : static int nvme_ctrlr_read_ana_log_page(struct nvme_ctrlr *nvme_ctrlr);
     213             : 
     214             : static struct nvme_ns *nvme_ns_alloc(void);
     215             : static void nvme_ns_free(struct nvme_ns *ns);
     216             : 
     217             : static int
     218         173 : nvme_ns_cmp(struct nvme_ns *ns1, struct nvme_ns *ns2)
     219             : {
     220         173 :         return ns1->id < ns2->id ? -1 : ns1->id > ns2->id;
     221             : }
     222             : 
     223         902 : RB_GENERATE_STATIC(nvme_ns_tree, nvme_ns, node, nvme_ns_cmp);
     224             : 
     225             : struct spdk_nvme_qpair *
     226           1 : bdev_nvme_get_io_qpair(struct spdk_io_channel *ctrlr_io_ch)
     227             : {
     228             :         struct nvme_ctrlr_channel *ctrlr_ch;
     229             : 
     230           1 :         assert(ctrlr_io_ch != NULL);
     231             : 
     232           1 :         ctrlr_ch = spdk_io_channel_get_ctx(ctrlr_io_ch);
     233             : 
     234           1 :         return ctrlr_ch->qpair->qpair;
     235             : }
     236             : 
     237             : static int
     238           0 : bdev_nvme_get_ctx_size(void)
     239             : {
     240           0 :         return sizeof(struct nvme_bdev_io);
     241             : }
     242             : 
     243             : static struct spdk_bdev_module nvme_if = {
     244             :         .name = "nvme",
     245             :         .async_fini = true,
     246             :         .module_init = bdev_nvme_library_init,
     247             :         .module_fini = bdev_nvme_library_fini,
     248             :         .config_json = bdev_nvme_config_json,
     249             :         .get_ctx_size = bdev_nvme_get_ctx_size,
     250             : 
     251             : };
     252           1 : SPDK_BDEV_MODULE_REGISTER(nvme, &nvme_if)
     253             : 
     254             : struct nvme_bdev_ctrlrs g_nvme_bdev_ctrlrs = TAILQ_HEAD_INITIALIZER(g_nvme_bdev_ctrlrs);
     255             : pthread_mutex_t g_bdev_nvme_mutex = PTHREAD_MUTEX_INITIALIZER;
     256             : bool g_bdev_nvme_module_finish;
     257             : 
     258             : struct nvme_bdev_ctrlr *
     259         270 : nvme_bdev_ctrlr_get_by_name(const char *name)
     260             : {
     261             :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
     262             : 
     263         270 :         TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
     264         148 :                 if (strcmp(name, nbdev_ctrlr->name) == 0) {
     265         148 :                         break;
     266             :                 }
     267             :         }
     268             : 
     269         270 :         return nbdev_ctrlr;
     270             : }
     271             : 
     272             : static struct nvme_ctrlr *
     273          58 : nvme_bdev_ctrlr_get_ctrlr(struct nvme_bdev_ctrlr *nbdev_ctrlr,
     274             :                           const struct spdk_nvme_transport_id *trid, const char *hostnqn)
     275             : {
     276             :         const struct spdk_nvme_ctrlr_opts *opts;
     277             :         struct nvme_ctrlr *nvme_ctrlr;
     278             : 
     279          99 :         TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
     280          74 :                 opts = spdk_nvme_ctrlr_get_opts(nvme_ctrlr->ctrlr);
     281          74 :                 if (spdk_nvme_transport_id_compare(trid, &nvme_ctrlr->active_path_id->trid) == 0 &&
     282          33 :                     strcmp(hostnqn, opts->hostnqn) == 0) {
     283          33 :                         break;
     284             :                 }
     285             :         }
     286             : 
     287          58 :         return nvme_ctrlr;
     288             : }
     289             : 
     290             : struct nvme_ctrlr *
     291           0 : nvme_bdev_ctrlr_get_ctrlr_by_id(struct nvme_bdev_ctrlr *nbdev_ctrlr,
     292             :                                 uint16_t cntlid)
     293             : {
     294             :         struct nvme_ctrlr *nvme_ctrlr;
     295             :         const struct spdk_nvme_ctrlr_data *cdata;
     296             : 
     297           0 :         TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
     298           0 :                 cdata = spdk_nvme_ctrlr_get_data(nvme_ctrlr->ctrlr);
     299           0 :                 if (cdata->cntlid == cntlid) {
     300           0 :                         break;
     301             :                 }
     302             :         }
     303             : 
     304           0 :         return nvme_ctrlr;
     305             : }
     306             : 
     307             : static struct nvme_bdev *
     308          72 : nvme_bdev_ctrlr_get_bdev(struct nvme_bdev_ctrlr *nbdev_ctrlr, uint32_t nsid)
     309             : {
     310             :         struct nvme_bdev *bdev;
     311             : 
     312          72 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     313         106 :         TAILQ_FOREACH(bdev, &nbdev_ctrlr->bdevs, tailq) {
     314          68 :                 if (bdev->nsid == nsid) {
     315          34 :                         break;
     316             :                 }
     317             :         }
     318          72 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     319             : 
     320          72 :         return bdev;
     321             : }
     322             : 
     323             : struct nvme_ns *
     324         139 : nvme_ctrlr_get_ns(struct nvme_ctrlr *nvme_ctrlr, uint32_t nsid)
     325             : {
     326         139 :         struct nvme_ns ns;
     327             : 
     328         139 :         assert(nsid > 0);
     329             : 
     330         139 :         ns.id = nsid;
     331         139 :         return RB_FIND(nvme_ns_tree, &nvme_ctrlr->namespaces, &ns);
     332             : }
     333             : 
     334             : struct nvme_ns *
     335         152 : nvme_ctrlr_get_first_active_ns(struct nvme_ctrlr *nvme_ctrlr)
     336             : {
     337         152 :         return RB_MIN(nvme_ns_tree, &nvme_ctrlr->namespaces);
     338             : }
     339             : 
     340             : struct nvme_ns *
     341          63 : nvme_ctrlr_get_next_active_ns(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *ns)
     342             : {
     343          63 :         if (ns == NULL) {
     344           0 :                 return NULL;
     345             :         }
     346             : 
     347          63 :         return RB_NEXT(nvme_ns_tree, &nvme_ctrlr->namespaces, ns);
     348             : }
     349             : 
     350             : static struct nvme_ctrlr *
     351          51 : nvme_ctrlr_get(const struct spdk_nvme_transport_id *trid, const char *hostnqn)
     352             : {
     353             :         struct nvme_bdev_ctrlr  *nbdev_ctrlr;
     354          51 :         struct nvme_ctrlr       *nvme_ctrlr = NULL;
     355             : 
     356          51 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     357          70 :         TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
     358          19 :                 nvme_ctrlr = nvme_bdev_ctrlr_get_ctrlr(nbdev_ctrlr, trid, hostnqn);
     359          19 :                 if (nvme_ctrlr != NULL) {
     360           0 :                         break;
     361             :                 }
     362             :         }
     363          51 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     364             : 
     365          51 :         return nvme_ctrlr;
     366             : }
     367             : 
     368             : struct nvme_ctrlr *
     369          71 : nvme_ctrlr_get_by_name(const char *name)
     370             : {
     371             :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
     372          71 :         struct nvme_ctrlr *nvme_ctrlr = NULL;
     373             : 
     374          71 :         if (name == NULL) {
     375           0 :                 return NULL;
     376             :         }
     377             : 
     378          71 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     379          71 :         nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
     380          71 :         if (nbdev_ctrlr != NULL) {
     381          40 :                 nvme_ctrlr = TAILQ_FIRST(&nbdev_ctrlr->ctrlrs);
     382             :         }
     383          71 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     384             : 
     385          71 :         return nvme_ctrlr;
     386             : }
     387             : 
     388             : void
     389           0 : nvme_bdev_ctrlr_for_each(nvme_bdev_ctrlr_for_each_fn fn, void *ctx)
     390             : {
     391             :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
     392             : 
     393           0 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     394           0 :         TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
     395           0 :                 fn(nbdev_ctrlr, ctx);
     396             :         }
     397           0 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     398           0 : }
     399             : 
     400             : void
     401           0 : nvme_bdev_dump_trid_json(const struct spdk_nvme_transport_id *trid, struct spdk_json_write_ctx *w)
     402             : {
     403             :         const char *trtype_str;
     404             :         const char *adrfam_str;
     405             : 
     406           0 :         trtype_str = spdk_nvme_transport_id_trtype_str(trid->trtype);
     407           0 :         if (trtype_str) {
     408           0 :                 spdk_json_write_named_string(w, "trtype", trtype_str);
     409             :         }
     410             : 
     411           0 :         adrfam_str = spdk_nvme_transport_id_adrfam_str(trid->adrfam);
     412           0 :         if (adrfam_str) {
     413           0 :                 spdk_json_write_named_string(w, "adrfam", adrfam_str);
     414             :         }
     415             : 
     416           0 :         if (trid->traddr[0] != '\0') {
     417           0 :                 spdk_json_write_named_string(w, "traddr", trid->traddr);
     418             :         }
     419             : 
     420           0 :         if (trid->trsvcid[0] != '\0') {
     421           0 :                 spdk_json_write_named_string(w, "trsvcid", trid->trsvcid);
     422             :         }
     423             : 
     424           0 :         if (trid->subnqn[0] != '\0') {
     425           0 :                 spdk_json_write_named_string(w, "subnqn", trid->subnqn);
     426             :         }
     427           0 : }
     428             : 
     429             : static void
     430          59 : nvme_bdev_ctrlr_delete(struct nvme_bdev_ctrlr *nbdev_ctrlr,
     431             :                        struct nvme_ctrlr *nvme_ctrlr)
     432             : {
     433             :         SPDK_DTRACE_PROBE1(bdev_nvme_ctrlr_delete, nvme_ctrlr->nbdev_ctrlr->name);
     434          59 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     435             : 
     436          59 :         TAILQ_REMOVE(&nbdev_ctrlr->ctrlrs, nvme_ctrlr, tailq);
     437          59 :         if (!TAILQ_EMPTY(&nbdev_ctrlr->ctrlrs)) {
     438          15 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
     439             : 
     440          15 :                 return;
     441             :         }
     442          44 :         TAILQ_REMOVE(&g_nvme_bdev_ctrlrs, nbdev_ctrlr, tailq);
     443             : 
     444          44 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     445             : 
     446          44 :         assert(TAILQ_EMPTY(&nbdev_ctrlr->bdevs));
     447             : 
     448          44 :         free(nbdev_ctrlr->name);
     449          44 :         free(nbdev_ctrlr);
     450             : }
     451             : 
     452             : static void
     453          60 : _nvme_ctrlr_delete(struct nvme_ctrlr *nvme_ctrlr)
     454             : {
     455             :         struct nvme_path_id *path_id, *tmp_path;
     456             :         struct nvme_ns *ns, *tmp_ns;
     457             : 
     458          60 :         free(nvme_ctrlr->copied_ana_desc);
     459          60 :         spdk_free(nvme_ctrlr->ana_log_page);
     460             : 
     461          60 :         if (nvme_ctrlr->opal_dev) {
     462           0 :                 spdk_opal_dev_destruct(nvme_ctrlr->opal_dev);
     463           0 :                 nvme_ctrlr->opal_dev = NULL;
     464             :         }
     465             : 
     466          60 :         if (nvme_ctrlr->nbdev_ctrlr) {
     467          59 :                 nvme_bdev_ctrlr_delete(nvme_ctrlr->nbdev_ctrlr, nvme_ctrlr);
     468             :         }
     469             : 
     470          60 :         RB_FOREACH_SAFE(ns, nvme_ns_tree, &nvme_ctrlr->namespaces, tmp_ns) {
     471           0 :                 RB_REMOVE(nvme_ns_tree, &nvme_ctrlr->namespaces, ns);
     472           0 :                 nvme_ns_free(ns);
     473             :         }
     474             : 
     475         120 :         TAILQ_FOREACH_SAFE(path_id, &nvme_ctrlr->trids, link, tmp_path) {
     476          60 :                 TAILQ_REMOVE(&nvme_ctrlr->trids, path_id, link);
     477          60 :                 free(path_id);
     478             :         }
     479             : 
     480          60 :         pthread_mutex_destroy(&nvme_ctrlr->mutex);
     481          60 :         spdk_keyring_put_key(nvme_ctrlr->psk);
     482          60 :         spdk_keyring_put_key(nvme_ctrlr->dhchap_key);
     483          60 :         spdk_keyring_put_key(nvme_ctrlr->dhchap_ctrlr_key);
     484          60 :         free(nvme_ctrlr);
     485             : 
     486          60 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     487          60 :         if (g_bdev_nvme_module_finish && TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) {
     488           0 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
     489           0 :                 spdk_io_device_unregister(&g_nvme_bdev_ctrlrs, NULL);
     490           0 :                 spdk_bdev_module_fini_done();
     491           0 :                 return;
     492             :         }
     493          60 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     494             : }
     495             : 
     496             : static int
     497          60 : nvme_detach_poller(void *arg)
     498             : {
     499          60 :         struct nvme_ctrlr *nvme_ctrlr = arg;
     500             :         int rc;
     501             : 
     502          60 :         rc = spdk_nvme_detach_poll_async(nvme_ctrlr->detach_ctx);
     503          60 :         if (rc != -EAGAIN) {
     504          60 :                 spdk_poller_unregister(&nvme_ctrlr->reset_detach_poller);
     505          60 :                 _nvme_ctrlr_delete(nvme_ctrlr);
     506             :         }
     507             : 
     508          60 :         return SPDK_POLLER_BUSY;
     509             : }
     510             : 
     511             : static void
     512          60 : nvme_ctrlr_delete(struct nvme_ctrlr *nvme_ctrlr)
     513             : {
     514             :         int rc;
     515             : 
     516          60 :         spdk_poller_unregister(&nvme_ctrlr->reconnect_delay_timer);
     517             : 
     518             :         /* First, unregister the adminq poller, as the driver will poll adminq if necessary */
     519          60 :         spdk_poller_unregister(&nvme_ctrlr->adminq_timer_poller);
     520             : 
     521             :         /* If we got here, the reset/detach poller cannot be active */
     522          60 :         assert(nvme_ctrlr->reset_detach_poller == NULL);
     523          60 :         nvme_ctrlr->reset_detach_poller = SPDK_POLLER_REGISTER(nvme_detach_poller,
     524             :                                           nvme_ctrlr, 1000);
     525          60 :         if (nvme_ctrlr->reset_detach_poller == NULL) {
     526           0 :                 SPDK_ERRLOG("Failed to register detach poller\n");
     527           0 :                 goto error;
     528             :         }
     529             : 
     530          60 :         rc = spdk_nvme_detach_async(nvme_ctrlr->ctrlr, &nvme_ctrlr->detach_ctx);
     531          60 :         if (rc != 0) {
     532           0 :                 SPDK_ERRLOG("Failed to detach the NVMe controller\n");
     533           0 :                 goto error;
     534             :         }
     535             : 
     536          60 :         return;
     537           0 : error:
     538             :         /* We don't have a good way to handle errors here, so just do what we can and delete the
     539             :          * controller without detaching the underlying NVMe device.
     540             :          */
     541           0 :         spdk_poller_unregister(&nvme_ctrlr->reset_detach_poller);
     542           0 :         _nvme_ctrlr_delete(nvme_ctrlr);
     543             : }
     544             : 
     545             : static void
     546          59 : nvme_ctrlr_unregister_cb(void *io_device)
     547             : {
     548          59 :         struct nvme_ctrlr *nvme_ctrlr = io_device;
     549             : 
     550          59 :         nvme_ctrlr_delete(nvme_ctrlr);
     551          59 : }
     552             : 
     553             : static void
     554          59 : nvme_ctrlr_unregister(void *ctx)
     555             : {
     556          59 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
     557             : 
     558          59 :         spdk_io_device_unregister(nvme_ctrlr, nvme_ctrlr_unregister_cb);
     559          59 : }
     560             : 
     561             : static bool
     562         220 : nvme_ctrlr_can_be_unregistered(struct nvme_ctrlr *nvme_ctrlr)
     563             : {
     564         220 :         if (!nvme_ctrlr->destruct) {
     565         105 :                 return false;
     566             :         }
     567             : 
     568         115 :         if (nvme_ctrlr->ref > 0) {
     569          56 :                 return false;
     570             :         }
     571             : 
     572          59 :         if (nvme_ctrlr->resetting) {
     573           0 :                 return false;
     574             :         }
     575             : 
     576          59 :         if (nvme_ctrlr->ana_log_page_updating) {
     577           0 :                 return false;
     578             :         }
     579             : 
     580          59 :         if (nvme_ctrlr->io_path_cache_clearing) {
     581           0 :                 return false;
     582             :         }
     583             : 
     584          59 :         return true;
     585             : }
     586             : 
     587             : static void
     588         164 : nvme_ctrlr_release(struct nvme_ctrlr *nvme_ctrlr)
     589             : {
     590         164 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
     591             :         SPDK_DTRACE_PROBE2(bdev_nvme_ctrlr_release, nvme_ctrlr->nbdev_ctrlr->name, nvme_ctrlr->ref);
     592             : 
     593         164 :         assert(nvme_ctrlr->ref > 0);
     594         164 :         nvme_ctrlr->ref--;
     595             : 
     596         164 :         if (!nvme_ctrlr_can_be_unregistered(nvme_ctrlr)) {
     597         105 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
     598         105 :                 return;
     599             :         }
     600             : 
     601          59 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
     602             : 
     603          59 :         spdk_thread_exec_msg(nvme_ctrlr->thread, nvme_ctrlr_unregister, nvme_ctrlr);
     604             : }
     605             : 
     606             : static void
     607         161 : bdev_nvme_clear_current_io_path(struct nvme_bdev_channel *nbdev_ch)
     608             : {
     609         161 :         nbdev_ch->current_io_path = NULL;
     610         161 :         nbdev_ch->rr_counter = 0;
     611         161 : }
     612             : 
     613             : static struct nvme_io_path *
     614           8 : _bdev_nvme_get_io_path(struct nvme_bdev_channel *nbdev_ch, struct nvme_ns *nvme_ns)
     615             : {
     616             :         struct nvme_io_path *io_path;
     617             : 
     618          16 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
     619          15 :                 if (io_path->nvme_ns == nvme_ns) {
     620           7 :                         break;
     621             :                 }
     622             :         }
     623             : 
     624           8 :         return io_path;
     625             : }
     626             : 
     627             : static struct nvme_io_path *
     628          35 : nvme_io_path_alloc(void)
     629             : {
     630             :         struct nvme_io_path *io_path;
     631             : 
     632          35 :         io_path = calloc(1, sizeof(*io_path));
     633          35 :         if (io_path == NULL) {
     634           0 :                 SPDK_ERRLOG("Failed to alloc io_path.\n");
     635           0 :                 return NULL;
     636             :         }
     637             : 
     638          35 :         if (g_opts.io_path_stat) {
     639           0 :                 io_path->stat = calloc(1, sizeof(struct spdk_bdev_io_stat));
     640           0 :                 if (io_path->stat == NULL) {
     641           0 :                         free(io_path);
     642           0 :                         SPDK_ERRLOG("Failed to alloc io_path stat.\n");
     643           0 :                         return NULL;
     644             :                 }
     645           0 :                 spdk_bdev_reset_io_stat(io_path->stat, SPDK_BDEV_RESET_STAT_MAXMIN);
     646             :         }
     647             : 
     648          35 :         return io_path;
     649             : }
     650             : 
     651             : static void
     652          35 : nvme_io_path_free(struct nvme_io_path *io_path)
     653             : {
     654          35 :         free(io_path->stat);
     655          35 :         free(io_path);
     656          35 : }
     657             : 
     658             : static int
     659          35 : _bdev_nvme_add_io_path(struct nvme_bdev_channel *nbdev_ch, struct nvme_ns *nvme_ns)
     660             : {
     661             :         struct nvme_io_path *io_path;
     662             :         struct spdk_io_channel *ch;
     663             :         struct nvme_ctrlr_channel *ctrlr_ch;
     664             :         struct nvme_qpair *nvme_qpair;
     665             : 
     666          35 :         io_path = nvme_io_path_alloc();
     667          35 :         if (io_path == NULL) {
     668           0 :                 return -ENOMEM;
     669             :         }
     670             : 
     671          35 :         io_path->nvme_ns = nvme_ns;
     672             : 
     673          35 :         ch = spdk_get_io_channel(nvme_ns->ctrlr);
     674          35 :         if (ch == NULL) {
     675           0 :                 nvme_io_path_free(io_path);
     676           0 :                 SPDK_ERRLOG("Failed to alloc io_channel.\n");
     677           0 :                 return -ENOMEM;
     678             :         }
     679             : 
     680          35 :         ctrlr_ch = spdk_io_channel_get_ctx(ch);
     681             : 
     682          35 :         nvme_qpair = ctrlr_ch->qpair;
     683          35 :         assert(nvme_qpair != NULL);
     684             : 
     685          35 :         io_path->qpair = nvme_qpair;
     686          35 :         TAILQ_INSERT_TAIL(&nvme_qpair->io_path_list, io_path, tailq);
     687             : 
     688          35 :         io_path->nbdev_ch = nbdev_ch;
     689          35 :         STAILQ_INSERT_TAIL(&nbdev_ch->io_path_list, io_path, stailq);
     690             : 
     691          35 :         bdev_nvme_clear_current_io_path(nbdev_ch);
     692             : 
     693          35 :         return 0;
     694             : }
     695             : 
     696             : static void
     697          35 : bdev_nvme_clear_retry_io_path(struct nvme_bdev_channel *nbdev_ch,
     698             :                               struct nvme_io_path *io_path)
     699             : {
     700             :         struct spdk_bdev_io *bdev_io;
     701             :         struct nvme_bdev_io *bio;
     702             : 
     703          36 :         TAILQ_FOREACH(bdev_io, &nbdev_ch->retry_io_list, module_link) {
     704           1 :                 bio = (struct nvme_bdev_io *)bdev_io->driver_ctx;
     705           1 :                 if (bio->io_path == io_path) {
     706           1 :                         bio->io_path = NULL;
     707             :                 }
     708             :         }
     709          35 : }
     710             : 
     711             : static void
     712          35 : _bdev_nvme_delete_io_path(struct nvme_bdev_channel *nbdev_ch, struct nvme_io_path *io_path)
     713             : {
     714             :         struct spdk_io_channel *ch;
     715             :         struct nvme_qpair *nvme_qpair;
     716             :         struct nvme_ctrlr_channel *ctrlr_ch;
     717             :         struct nvme_bdev *nbdev;
     718             : 
     719          35 :         nbdev = spdk_io_channel_get_io_device(spdk_io_channel_from_ctx(nbdev_ch));
     720             : 
     721             :         /* Add the statistics to nvme_ns before this path is destroyed. */
     722          35 :         pthread_mutex_lock(&nbdev->mutex);
     723          35 :         if (nbdev->ref != 0 && io_path->nvme_ns->stat != NULL && io_path->stat != NULL) {
     724           0 :                 spdk_bdev_add_io_stat(io_path->nvme_ns->stat, io_path->stat);
     725             :         }
     726          35 :         pthread_mutex_unlock(&nbdev->mutex);
     727             : 
     728          35 :         bdev_nvme_clear_current_io_path(nbdev_ch);
     729          35 :         bdev_nvme_clear_retry_io_path(nbdev_ch, io_path);
     730             : 
     731          35 :         STAILQ_REMOVE(&nbdev_ch->io_path_list, io_path, nvme_io_path, stailq);
     732          35 :         io_path->nbdev_ch = NULL;
     733             : 
     734          35 :         nvme_qpair = io_path->qpair;
     735          35 :         assert(nvme_qpair != NULL);
     736             : 
     737          35 :         ctrlr_ch = nvme_qpair->ctrlr_ch;
     738          35 :         assert(ctrlr_ch != NULL);
     739             : 
     740          35 :         ch = spdk_io_channel_from_ctx(ctrlr_ch);
     741          35 :         spdk_put_io_channel(ch);
     742             : 
     743             :         /* After an io_path is removed, I/Os submitted to it may complete and update statistics
     744             :          * of the io_path. To avoid heap-use-after-free error from this case, do not free the
     745             :          * io_path here but free the io_path when the associated qpair is freed. It is ensured
     746             :          * that all I/Os submitted to the io_path are completed when the associated qpair is freed.
     747             :          */
     748          35 : }
     749             : 
     750             : static void
     751          22 : _bdev_nvme_delete_io_paths(struct nvme_bdev_channel *nbdev_ch)
     752             : {
     753             :         struct nvme_io_path *io_path, *tmp_io_path;
     754             : 
     755          55 :         STAILQ_FOREACH_SAFE(io_path, &nbdev_ch->io_path_list, stailq, tmp_io_path) {
     756          33 :                 _bdev_nvme_delete_io_path(nbdev_ch, io_path);
     757             :         }
     758          22 : }
     759             : 
     760             : static int
     761          22 : bdev_nvme_create_bdev_channel_cb(void *io_device, void *ctx_buf)
     762             : {
     763          22 :         struct nvme_bdev_channel *nbdev_ch = ctx_buf;
     764          22 :         struct nvme_bdev *nbdev = io_device;
     765             :         struct nvme_ns *nvme_ns;
     766             :         int rc;
     767             : 
     768          22 :         STAILQ_INIT(&nbdev_ch->io_path_list);
     769          22 :         TAILQ_INIT(&nbdev_ch->retry_io_list);
     770             : 
     771          22 :         pthread_mutex_lock(&nbdev->mutex);
     772             : 
     773          22 :         nbdev_ch->mp_policy = nbdev->mp_policy;
     774          22 :         nbdev_ch->mp_selector = nbdev->mp_selector;
     775          22 :         nbdev_ch->rr_min_io = nbdev->rr_min_io;
     776             : 
     777          55 :         TAILQ_FOREACH(nvme_ns, &nbdev->nvme_ns_list, tailq) {
     778          33 :                 rc = _bdev_nvme_add_io_path(nbdev_ch, nvme_ns);
     779          33 :                 if (rc != 0) {
     780           0 :                         pthread_mutex_unlock(&nbdev->mutex);
     781             : 
     782           0 :                         _bdev_nvme_delete_io_paths(nbdev_ch);
     783           0 :                         return rc;
     784             :                 }
     785             :         }
     786          22 :         pthread_mutex_unlock(&nbdev->mutex);
     787             : 
     788          22 :         return 0;
     789             : }
     790             : 
     791             : /* If cpl != NULL, complete the bdev_io with nvme status based on 'cpl'.
     792             :  * If cpl == NULL, complete the bdev_io with bdev status based on 'status'.
     793             :  */
     794             : static inline void
     795          47 : __bdev_nvme_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status,
     796             :                         const struct spdk_nvme_cpl *cpl)
     797             : {
     798          47 :         spdk_trace_record(TRACE_BDEV_NVME_IO_DONE, 0, 0, (uintptr_t)bdev_io->driver_ctx,
     799             :                           (uintptr_t)bdev_io);
     800          47 :         if (cpl) {
     801          29 :                 spdk_bdev_io_complete_nvme_status(bdev_io, cpl->cdw0, cpl->status.sct, cpl->status.sc);
     802             :         } else {
     803          18 :                 spdk_bdev_io_complete(bdev_io, status);
     804             :         }
     805          47 : }
     806             : 
     807             : static void bdev_nvme_abort_retry_ios(struct nvme_bdev_channel *nbdev_ch);
     808             : 
     809             : static void
     810          22 : bdev_nvme_destroy_bdev_channel_cb(void *io_device, void *ctx_buf)
     811             : {
     812          22 :         struct nvme_bdev_channel *nbdev_ch = ctx_buf;
     813             : 
     814          22 :         bdev_nvme_abort_retry_ios(nbdev_ch);
     815          22 :         _bdev_nvme_delete_io_paths(nbdev_ch);
     816          22 : }
     817             : 
     818             : static inline bool
     819          58 : bdev_nvme_io_type_is_admin(enum spdk_bdev_io_type io_type)
     820             : {
     821          58 :         switch (io_type) {
     822           5 :         case SPDK_BDEV_IO_TYPE_RESET:
     823             :         case SPDK_BDEV_IO_TYPE_NVME_ADMIN:
     824             :         case SPDK_BDEV_IO_TYPE_ABORT:
     825           5 :                 return true;
     826          53 :         default:
     827          53 :                 break;
     828             :         }
     829             : 
     830          53 :         return false;
     831             : }
     832             : 
     833             : static inline bool
     834          90 : nvme_ns_is_active(struct nvme_ns *nvme_ns)
     835             : {
     836          90 :         if (spdk_unlikely(nvme_ns->ana_state_updating)) {
     837           1 :                 return false;
     838             :         }
     839             : 
     840          89 :         if (spdk_unlikely(nvme_ns->ns == NULL)) {
     841           0 :                 return false;
     842             :         }
     843             : 
     844          89 :         return true;
     845             : }
     846             : 
     847             : static inline bool
     848          78 : nvme_ns_is_accessible(struct nvme_ns *nvme_ns)
     849             : {
     850          78 :         if (spdk_unlikely(!nvme_ns_is_active(nvme_ns))) {
     851           1 :                 return false;
     852             :         }
     853             : 
     854          77 :         switch (nvme_ns->ana_state) {
     855          68 :         case SPDK_NVME_ANA_OPTIMIZED_STATE:
     856             :         case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
     857          68 :                 return true;
     858           9 :         default:
     859           9 :                 break;
     860             :         }
     861             : 
     862           9 :         return false;
     863             : }
     864             : 
     865             : static inline bool
     866         117 : nvme_qpair_is_connected(struct nvme_qpair *nvme_qpair)
     867             : {
     868         117 :         if (spdk_unlikely(nvme_qpair->qpair == NULL)) {
     869          20 :                 return false;
     870             :         }
     871             : 
     872          97 :         if (spdk_unlikely(spdk_nvme_qpair_get_failure_reason(nvme_qpair->qpair) !=
     873             :                           SPDK_NVME_QPAIR_FAILURE_NONE)) {
     874           2 :                 return false;
     875             :         }
     876             : 
     877          95 :         if (spdk_unlikely(nvme_qpair->ctrlr_ch->reset_iter != NULL)) {
     878           0 :                 return false;
     879             :         }
     880             : 
     881          95 :         return true;
     882             : }
     883             : 
     884             : static inline bool
     885          92 : nvme_io_path_is_available(struct nvme_io_path *io_path)
     886             : {
     887          92 :         if (spdk_unlikely(!nvme_qpair_is_connected(io_path->qpair))) {
     888          14 :                 return false;
     889             :         }
     890             : 
     891          78 :         if (spdk_unlikely(!nvme_ns_is_accessible(io_path->nvme_ns))) {
     892          10 :                 return false;
     893             :         }
     894             : 
     895          68 :         return true;
     896             : }
     897             : 
     898             : static inline bool
     899           8 : nvme_ctrlr_is_failed(struct nvme_ctrlr *nvme_ctrlr)
     900             : {
     901           8 :         if (nvme_ctrlr->destruct) {
     902           0 :                 return true;
     903             :         }
     904             : 
     905           8 :         if (nvme_ctrlr->fast_io_fail_timedout) {
     906           2 :                 return true;
     907             :         }
     908             : 
     909           6 :         if (nvme_ctrlr->resetting) {
     910           4 :                 if (nvme_ctrlr->opts.reconnect_delay_sec != 0) {
     911           4 :                         return false;
     912             :                 } else {
     913           0 :                         return true;
     914             :                 }
     915             :         }
     916             : 
     917           2 :         if (nvme_ctrlr->reconnect_is_delayed) {
     918           2 :                 return false;
     919             :         }
     920             : 
     921           0 :         if (nvme_ctrlr->disabled) {
     922           0 :                 return true;
     923             :         }
     924             : 
     925           0 :         if (spdk_nvme_ctrlr_is_failed(nvme_ctrlr->ctrlr)) {
     926           0 :                 return true;
     927             :         } else {
     928           0 :                 return false;
     929             :         }
     930             : }
     931             : 
     932             : static bool
     933          20 : nvme_ctrlr_is_available(struct nvme_ctrlr *nvme_ctrlr)
     934             : {
     935          20 :         if (nvme_ctrlr->destruct) {
     936           0 :                 return false;
     937             :         }
     938             : 
     939          20 :         if (spdk_nvme_ctrlr_is_failed(nvme_ctrlr->ctrlr)) {
     940           3 :                 return false;
     941             :         }
     942             : 
     943          17 :         if (nvme_ctrlr->resetting || nvme_ctrlr->reconnect_is_delayed) {
     944           1 :                 return false;
     945             :         }
     946             : 
     947          16 :         if (nvme_ctrlr->disabled) {
     948           0 :                 return false;
     949             :         }
     950             : 
     951          16 :         return true;
     952             : }
     953             : 
     954             : /* Simulate circular linked list. */
     955             : static inline struct nvme_io_path *
     956          87 : nvme_io_path_get_next(struct nvme_bdev_channel *nbdev_ch, struct nvme_io_path *prev_path)
     957             : {
     958             :         struct nvme_io_path *next_path;
     959             : 
     960          87 :         if (prev_path != NULL) {
     961          37 :                 next_path = STAILQ_NEXT(prev_path, stailq);
     962          37 :                 if (next_path != NULL) {
     963          14 :                         return next_path;
     964             :                 }
     965             :         }
     966             : 
     967          73 :         return STAILQ_FIRST(&nbdev_ch->io_path_list);
     968             : }
     969             : 
     970             : static struct nvme_io_path *
     971          57 : _bdev_nvme_find_io_path(struct nvme_bdev_channel *nbdev_ch)
     972             : {
     973          57 :         struct nvme_io_path *io_path, *start, *non_optimized = NULL;
     974             : 
     975          57 :         start = nvme_io_path_get_next(nbdev_ch, nbdev_ch->current_io_path);
     976             : 
     977          57 :         io_path = start;
     978             :         do {
     979          69 :                 if (spdk_likely(nvme_io_path_is_available(io_path))) {
     980          49 :                         switch (io_path->nvme_ns->ana_state) {
     981          39 :                         case SPDK_NVME_ANA_OPTIMIZED_STATE:
     982          39 :                                 nbdev_ch->current_io_path = io_path;
     983          39 :                                 return io_path;
     984          10 :                         case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
     985          10 :                                 if (non_optimized == NULL) {
     986           7 :                                         non_optimized = io_path;
     987             :                                 }
     988          10 :                                 break;
     989           0 :                         default:
     990           0 :                                 assert(false);
     991             :                                 break;
     992             :                         }
     993          20 :                 }
     994          30 :                 io_path = nvme_io_path_get_next(nbdev_ch, io_path);
     995          30 :         } while (io_path != start);
     996             : 
     997          18 :         if (nbdev_ch->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE) {
     998             :                 /* We come here only if there is no optimized path. Cache even non_optimized
     999             :                  * path for load balance across multiple non_optimized paths.
    1000             :                  */
    1001           1 :                 nbdev_ch->current_io_path = non_optimized;
    1002             :         }
    1003             : 
    1004          18 :         return non_optimized;
    1005             : }
    1006             : 
    1007             : static struct nvme_io_path *
    1008           4 : _bdev_nvme_find_io_path_min_qd(struct nvme_bdev_channel *nbdev_ch)
    1009             : {
    1010             :         struct nvme_io_path *io_path;
    1011           4 :         struct nvme_io_path *optimized = NULL, *non_optimized = NULL;
    1012           4 :         uint32_t opt_min_qd = UINT32_MAX, non_opt_min_qd = UINT32_MAX;
    1013             :         uint32_t num_outstanding_reqs;
    1014             : 
    1015          16 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
    1016          12 :                 if (spdk_unlikely(!nvme_qpair_is_connected(io_path->qpair))) {
    1017             :                         /* The device is currently resetting. */
    1018           0 :                         continue;
    1019             :                 }
    1020             : 
    1021          12 :                 if (spdk_unlikely(!nvme_ns_is_active(io_path->nvme_ns))) {
    1022           0 :                         continue;
    1023             :                 }
    1024             : 
    1025          12 :                 num_outstanding_reqs = spdk_nvme_qpair_get_num_outstanding_reqs(io_path->qpair->qpair);
    1026          12 :                 switch (io_path->nvme_ns->ana_state) {
    1027           6 :                 case SPDK_NVME_ANA_OPTIMIZED_STATE:
    1028           6 :                         if (num_outstanding_reqs < opt_min_qd) {
    1029           5 :                                 opt_min_qd = num_outstanding_reqs;
    1030           5 :                                 optimized = io_path;
    1031             :                         }
    1032           6 :                         break;
    1033           3 :                 case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
    1034           3 :                         if (num_outstanding_reqs < non_opt_min_qd) {
    1035           3 :                                 non_opt_min_qd = num_outstanding_reqs;
    1036           3 :                                 non_optimized = io_path;
    1037             :                         }
    1038           3 :                         break;
    1039           3 :                 default:
    1040           3 :                         break;
    1041             :                 }
    1042             :         }
    1043             : 
    1044             :         /* don't cache io path for BDEV_NVME_MP_SELECTOR_QUEUE_DEPTH selector */
    1045           4 :         if (optimized != NULL) {
    1046           3 :                 return optimized;
    1047             :         }
    1048             : 
    1049           1 :         return non_optimized;
    1050             : }
    1051             : 
    1052             : static inline struct nvme_io_path *
    1053          95 : bdev_nvme_find_io_path(struct nvme_bdev_channel *nbdev_ch)
    1054             : {
    1055          95 :         if (spdk_likely(nbdev_ch->current_io_path != NULL)) {
    1056          41 :                 if (nbdev_ch->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE) {
    1057          31 :                         return nbdev_ch->current_io_path;
    1058          10 :                 } else if (nbdev_ch->mp_selector == BDEV_NVME_MP_SELECTOR_ROUND_ROBIN) {
    1059          10 :                         if (++nbdev_ch->rr_counter < nbdev_ch->rr_min_io) {
    1060           3 :                                 return nbdev_ch->current_io_path;
    1061             :                         }
    1062           7 :                         nbdev_ch->rr_counter = 0;
    1063             :                 }
    1064             :         }
    1065             : 
    1066          61 :         if (nbdev_ch->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE ||
    1067          14 :             nbdev_ch->mp_selector == BDEV_NVME_MP_SELECTOR_ROUND_ROBIN) {
    1068          57 :                 return _bdev_nvme_find_io_path(nbdev_ch);
    1069             :         } else {
    1070           4 :                 return _bdev_nvme_find_io_path_min_qd(nbdev_ch);
    1071             :         }
    1072             : }
    1073             : 
    1074             : /* Return true if there is any io_path whose qpair is active or ctrlr is not failed,
    1075             :  * or false otherwise.
    1076             :  *
    1077             :  * If any io_path has an active qpair but find_io_path() returned NULL, its namespace
    1078             :  * is likely to be non-accessible now but may become accessible.
    1079             :  *
    1080             :  * If any io_path has an unfailed ctrlr but find_io_path() returned NULL, the ctrlr
    1081             :  * is likely to be resetting now but the reset may succeed. A ctrlr is set to unfailed
    1082             :  * when starting to reset it but it is set to failed when the reset failed. Hence, if
    1083             :  * a ctrlr is unfailed, it is likely that it works fine or is resetting.
    1084             :  */
    1085             : static bool
    1086          13 : any_io_path_may_become_available(struct nvme_bdev_channel *nbdev_ch)
    1087             : {
    1088             :         struct nvme_io_path *io_path;
    1089             : 
    1090          15 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
    1091          13 :                 if (io_path->nvme_ns->ana_transition_timedout) {
    1092           0 :                         continue;
    1093             :                 }
    1094             : 
    1095          13 :                 if (nvme_qpair_is_connected(io_path->qpair) ||
    1096           8 :                     !nvme_ctrlr_is_failed(io_path->qpair->ctrlr)) {
    1097          11 :                         return true;
    1098             :                 }
    1099             :         }
    1100             : 
    1101           2 :         return false;
    1102             : }
    1103             : 
    1104             : static void
    1105          14 : bdev_nvme_retry_io(struct nvme_bdev_channel *nbdev_ch, struct spdk_bdev_io *bdev_io)
    1106             : {
    1107          14 :         struct nvme_bdev_io *nbdev_io = (struct nvme_bdev_io *)bdev_io->driver_ctx;
    1108             :         struct spdk_io_channel *ch;
    1109             : 
    1110          14 :         if (nbdev_io->io_path != NULL && nvme_io_path_is_available(nbdev_io->io_path)) {
    1111           3 :                 _bdev_nvme_submit_request(nbdev_ch, bdev_io);
    1112             :         } else {
    1113          11 :                 ch = spdk_io_channel_from_ctx(nbdev_ch);
    1114          11 :                 bdev_nvme_submit_request(ch, bdev_io);
    1115             :         }
    1116          14 : }
    1117             : 
    1118             : static int
    1119          14 : bdev_nvme_retry_ios(void *arg)
    1120             : {
    1121          14 :         struct nvme_bdev_channel *nbdev_ch = arg;
    1122             :         struct spdk_bdev_io *bdev_io, *tmp_bdev_io;
    1123             :         struct nvme_bdev_io *bio;
    1124             :         uint64_t now, delay_us;
    1125             : 
    1126          14 :         now = spdk_get_ticks();
    1127             : 
    1128          28 :         TAILQ_FOREACH_SAFE(bdev_io, &nbdev_ch->retry_io_list, module_link, tmp_bdev_io) {
    1129          15 :                 bio = (struct nvme_bdev_io *)bdev_io->driver_ctx;
    1130          15 :                 if (bio->retry_ticks > now) {
    1131           1 :                         break;
    1132             :                 }
    1133             : 
    1134          14 :                 TAILQ_REMOVE(&nbdev_ch->retry_io_list, bdev_io, module_link);
    1135             : 
    1136          14 :                 bdev_nvme_retry_io(nbdev_ch, bdev_io);
    1137             :         }
    1138             : 
    1139          14 :         spdk_poller_unregister(&nbdev_ch->retry_io_poller);
    1140             : 
    1141          14 :         bdev_io = TAILQ_FIRST(&nbdev_ch->retry_io_list);
    1142          14 :         if (bdev_io != NULL) {
    1143           4 :                 bio = (struct nvme_bdev_io *)bdev_io->driver_ctx;
    1144             : 
    1145           4 :                 delay_us = (bio->retry_ticks - now) * SPDK_SEC_TO_USEC / spdk_get_ticks_hz();
    1146             : 
    1147           4 :                 nbdev_ch->retry_io_poller = SPDK_POLLER_REGISTER(bdev_nvme_retry_ios, nbdev_ch,
    1148             :                                             delay_us);
    1149             :         }
    1150             : 
    1151          14 :         return SPDK_POLLER_BUSY;
    1152             : }
    1153             : 
    1154             : static void
    1155          15 : bdev_nvme_queue_retry_io(struct nvme_bdev_channel *nbdev_ch,
    1156             :                          struct nvme_bdev_io *bio, uint64_t delay_ms)
    1157             : {
    1158          15 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    1159             :         struct spdk_bdev_io *tmp_bdev_io;
    1160             :         struct nvme_bdev_io *tmp_bio;
    1161             : 
    1162          15 :         bio->retry_ticks = spdk_get_ticks() + delay_ms * spdk_get_ticks_hz() / 1000ULL;
    1163             : 
    1164          15 :         TAILQ_FOREACH_REVERSE(tmp_bdev_io, &nbdev_ch->retry_io_list, retry_io_head, module_link) {
    1165           1 :                 tmp_bio = (struct nvme_bdev_io *)tmp_bdev_io->driver_ctx;
    1166             : 
    1167           1 :                 if (tmp_bio->retry_ticks <= bio->retry_ticks) {
    1168           1 :                         TAILQ_INSERT_AFTER(&nbdev_ch->retry_io_list, tmp_bdev_io, bdev_io,
    1169             :                                            module_link);
    1170           1 :                         return;
    1171             :                 }
    1172             :         }
    1173             : 
    1174             :         /* No earlier I/Os were found. This I/O must be the new head. */
    1175          14 :         TAILQ_INSERT_HEAD(&nbdev_ch->retry_io_list, bdev_io, module_link);
    1176             : 
    1177          14 :         spdk_poller_unregister(&nbdev_ch->retry_io_poller);
    1178             : 
    1179          14 :         nbdev_ch->retry_io_poller = SPDK_POLLER_REGISTER(bdev_nvme_retry_ios, nbdev_ch,
    1180             :                                     delay_ms * 1000ULL);
    1181             : }
    1182             : 
    1183             : static void
    1184          36 : bdev_nvme_abort_retry_ios(struct nvme_bdev_channel *nbdev_ch)
    1185             : {
    1186             :         struct spdk_bdev_io *bdev_io, *tmp_io;
    1187             : 
    1188          36 :         TAILQ_FOREACH_SAFE(bdev_io, &nbdev_ch->retry_io_list, module_link, tmp_io) {
    1189           0 :                 TAILQ_REMOVE(&nbdev_ch->retry_io_list, bdev_io, module_link);
    1190           0 :                 __bdev_nvme_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_ABORTED, NULL);
    1191             :         }
    1192             : 
    1193          36 :         spdk_poller_unregister(&nbdev_ch->retry_io_poller);
    1194          36 : }
    1195             : 
    1196             : static int
    1197           6 : bdev_nvme_abort_retry_io(struct nvme_bdev_channel *nbdev_ch,
    1198             :                          struct nvme_bdev_io *bio_to_abort)
    1199             : {
    1200             :         struct spdk_bdev_io *bdev_io_to_abort;
    1201             : 
    1202           6 :         TAILQ_FOREACH(bdev_io_to_abort, &nbdev_ch->retry_io_list, module_link) {
    1203           1 :                 if ((struct nvme_bdev_io *)bdev_io_to_abort->driver_ctx == bio_to_abort) {
    1204           1 :                         TAILQ_REMOVE(&nbdev_ch->retry_io_list, bdev_io_to_abort, module_link);
    1205           1 :                         __bdev_nvme_io_complete(bdev_io_to_abort, SPDK_BDEV_IO_STATUS_ABORTED, NULL);
    1206           1 :                         return 0;
    1207             :                 }
    1208             :         }
    1209             : 
    1210           5 :         return -ENOENT;
    1211             : }
    1212             : 
    1213             : static void
    1214          12 : bdev_nvme_update_nvme_error_stat(struct spdk_bdev_io *bdev_io, const struct spdk_nvme_cpl *cpl)
    1215             : {
    1216             :         struct nvme_bdev *nbdev;
    1217             :         uint16_t sct, sc;
    1218             : 
    1219          12 :         assert(spdk_nvme_cpl_is_error(cpl));
    1220             : 
    1221          12 :         nbdev = bdev_io->bdev->ctxt;
    1222             : 
    1223          12 :         if (nbdev->err_stat == NULL) {
    1224          12 :                 return;
    1225             :         }
    1226             : 
    1227           0 :         sct = cpl->status.sct;
    1228           0 :         sc = cpl->status.sc;
    1229             : 
    1230           0 :         pthread_mutex_lock(&nbdev->mutex);
    1231             : 
    1232           0 :         nbdev->err_stat->status_type[sct]++;
    1233           0 :         switch (sct) {
    1234           0 :         case SPDK_NVME_SCT_GENERIC:
    1235             :         case SPDK_NVME_SCT_COMMAND_SPECIFIC:
    1236             :         case SPDK_NVME_SCT_MEDIA_ERROR:
    1237             :         case SPDK_NVME_SCT_PATH:
    1238           0 :                 nbdev->err_stat->status[sct][sc]++;
    1239           0 :                 break;
    1240           0 :         default:
    1241           0 :                 break;
    1242             :         }
    1243             : 
    1244           0 :         pthread_mutex_unlock(&nbdev->mutex);
    1245             : }
    1246             : 
    1247             : static inline void
    1248          20 : bdev_nvme_update_io_path_stat(struct nvme_bdev_io *bio)
    1249             : {
    1250          20 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    1251          20 :         uint64_t num_blocks = bdev_io->u.bdev.num_blocks;
    1252          20 :         uint32_t blocklen = bdev_io->bdev->blocklen;
    1253             :         struct spdk_bdev_io_stat *stat;
    1254             :         uint64_t tsc_diff;
    1255             : 
    1256          20 :         if (bio->io_path->stat == NULL) {
    1257          20 :                 return;
    1258             :         }
    1259             : 
    1260           0 :         tsc_diff = spdk_get_ticks() - bio->submit_tsc;
    1261           0 :         stat = bio->io_path->stat;
    1262             : 
    1263           0 :         switch (bdev_io->type) {
    1264           0 :         case SPDK_BDEV_IO_TYPE_READ:
    1265           0 :                 stat->bytes_read += num_blocks * blocklen;
    1266           0 :                 stat->num_read_ops++;
    1267           0 :                 stat->read_latency_ticks += tsc_diff;
    1268           0 :                 if (stat->max_read_latency_ticks < tsc_diff) {
    1269           0 :                         stat->max_read_latency_ticks = tsc_diff;
    1270             :                 }
    1271           0 :                 if (stat->min_read_latency_ticks > tsc_diff) {
    1272           0 :                         stat->min_read_latency_ticks = tsc_diff;
    1273             :                 }
    1274           0 :                 break;
    1275           0 :         case SPDK_BDEV_IO_TYPE_WRITE:
    1276           0 :                 stat->bytes_written += num_blocks * blocklen;
    1277           0 :                 stat->num_write_ops++;
    1278           0 :                 stat->write_latency_ticks += tsc_diff;
    1279           0 :                 if (stat->max_write_latency_ticks < tsc_diff) {
    1280           0 :                         stat->max_write_latency_ticks = tsc_diff;
    1281             :                 }
    1282           0 :                 if (stat->min_write_latency_ticks > tsc_diff) {
    1283           0 :                         stat->min_write_latency_ticks = tsc_diff;
    1284             :                 }
    1285           0 :                 break;
    1286           0 :         case SPDK_BDEV_IO_TYPE_UNMAP:
    1287           0 :                 stat->bytes_unmapped += num_blocks * blocklen;
    1288           0 :                 stat->num_unmap_ops++;
    1289           0 :                 stat->unmap_latency_ticks += tsc_diff;
    1290           0 :                 if (stat->max_unmap_latency_ticks < tsc_diff) {
    1291           0 :                         stat->max_unmap_latency_ticks = tsc_diff;
    1292             :                 }
    1293           0 :                 if (stat->min_unmap_latency_ticks > tsc_diff) {
    1294           0 :                         stat->min_unmap_latency_ticks = tsc_diff;
    1295             :                 }
    1296           0 :                 break;
    1297           0 :         case SPDK_BDEV_IO_TYPE_ZCOPY:
    1298             :                 /* Track the data in the start phase only */
    1299           0 :                 if (!bdev_io->u.bdev.zcopy.start) {
    1300           0 :                         break;
    1301             :                 }
    1302           0 :                 if (bdev_io->u.bdev.zcopy.populate) {
    1303           0 :                         stat->bytes_read += num_blocks * blocklen;
    1304           0 :                         stat->num_read_ops++;
    1305           0 :                         stat->read_latency_ticks += tsc_diff;
    1306           0 :                         if (stat->max_read_latency_ticks < tsc_diff) {
    1307           0 :                                 stat->max_read_latency_ticks = tsc_diff;
    1308             :                         }
    1309           0 :                         if (stat->min_read_latency_ticks > tsc_diff) {
    1310           0 :                                 stat->min_read_latency_ticks = tsc_diff;
    1311             :                         }
    1312             :                 } else {
    1313           0 :                         stat->bytes_written += num_blocks * blocklen;
    1314           0 :                         stat->num_write_ops++;
    1315           0 :                         stat->write_latency_ticks += tsc_diff;
    1316           0 :                         if (stat->max_write_latency_ticks < tsc_diff) {
    1317           0 :                                 stat->max_write_latency_ticks = tsc_diff;
    1318             :                         }
    1319           0 :                         if (stat->min_write_latency_ticks > tsc_diff) {
    1320           0 :                                 stat->min_write_latency_ticks = tsc_diff;
    1321             :                         }
    1322             :                 }
    1323           0 :                 break;
    1324           0 :         case SPDK_BDEV_IO_TYPE_COPY:
    1325           0 :                 stat->bytes_copied += num_blocks * blocklen;
    1326           0 :                 stat->num_copy_ops++;
    1327           0 :                 stat->copy_latency_ticks += tsc_diff;
    1328           0 :                 if (stat->max_copy_latency_ticks < tsc_diff) {
    1329           0 :                         stat->max_copy_latency_ticks = tsc_diff;
    1330             :                 }
    1331           0 :                 if (stat->min_copy_latency_ticks > tsc_diff) {
    1332           0 :                         stat->min_copy_latency_ticks = tsc_diff;
    1333             :                 }
    1334           0 :                 break;
    1335           0 :         default:
    1336           0 :                 break;
    1337             :         }
    1338             : }
    1339             : 
    1340             : static bool
    1341           7 : bdev_nvme_check_retry_io(struct nvme_bdev_io *bio,
    1342             :                          const struct spdk_nvme_cpl *cpl,
    1343             :                          struct nvme_bdev_channel *nbdev_ch,
    1344             :                          uint64_t *_delay_ms)
    1345             : {
    1346           7 :         struct nvme_io_path *io_path = bio->io_path;
    1347           7 :         struct nvme_ctrlr *nvme_ctrlr = io_path->qpair->ctrlr;
    1348             :         const struct spdk_nvme_ctrlr_data *cdata;
    1349             : 
    1350           7 :         if (spdk_nvme_cpl_is_path_error(cpl) ||
    1351           5 :             spdk_nvme_cpl_is_aborted_sq_deletion(cpl) ||
    1352           4 :             !nvme_io_path_is_available(io_path) ||
    1353           4 :             !nvme_ctrlr_is_available(nvme_ctrlr)) {
    1354           3 :                 bdev_nvme_clear_current_io_path(nbdev_ch);
    1355           3 :                 bio->io_path = NULL;
    1356           3 :                 if (spdk_nvme_cpl_is_ana_error(cpl)) {
    1357           1 :                         if (nvme_ctrlr_read_ana_log_page(nvme_ctrlr) == 0) {
    1358           1 :                                 io_path->nvme_ns->ana_state_updating = true;
    1359             :                         }
    1360             :                 }
    1361           3 :                 if (!any_io_path_may_become_available(nbdev_ch)) {
    1362           0 :                         return false;
    1363             :                 }
    1364           3 :                 *_delay_ms = 0;
    1365             :         } else {
    1366           4 :                 bio->retry_count++;
    1367             : 
    1368           4 :                 cdata = spdk_nvme_ctrlr_get_data(nvme_ctrlr->ctrlr);
    1369             : 
    1370           4 :                 if (cpl->status.crd != 0) {
    1371           1 :                         *_delay_ms = cdata->crdt[cpl->status.crd] * 100;
    1372             :                 } else {
    1373           3 :                         *_delay_ms = 0;
    1374             :                 }
    1375             :         }
    1376             : 
    1377           7 :         return true;
    1378             : }
    1379             : 
    1380             : static inline void
    1381          32 : bdev_nvme_io_complete_nvme_status(struct nvme_bdev_io *bio,
    1382             :                                   const struct spdk_nvme_cpl *cpl)
    1383             : {
    1384          32 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    1385             :         struct nvme_bdev_channel *nbdev_ch;
    1386          32 :         uint64_t delay_ms;
    1387             : 
    1388          32 :         assert(!bdev_nvme_io_type_is_admin(bdev_io->type));
    1389             : 
    1390          32 :         if (spdk_likely(spdk_nvme_cpl_is_success(cpl))) {
    1391          20 :                 bdev_nvme_update_io_path_stat(bio);
    1392          20 :                 goto complete;
    1393             :         }
    1394             : 
    1395             :         /* Update error counts before deciding if retry is needed.
    1396             :          * Hence, error counts may be more than the number of I/O errors.
    1397             :          */
    1398          12 :         bdev_nvme_update_nvme_error_stat(bdev_io, cpl);
    1399             : 
    1400          12 :         if (cpl->status.dnr != 0 || spdk_nvme_cpl_is_aborted_by_request(cpl) ||
    1401           8 :             (g_opts.bdev_retry_count != -1 && bio->retry_count >= g_opts.bdev_retry_count)) {
    1402           5 :                 goto complete;
    1403             :         }
    1404             : 
    1405             :         /* At this point we don't know whether the sequence was successfully executed or not, so we
    1406             :          * cannot retry the IO */
    1407           7 :         if (bdev_io->u.bdev.accel_sequence != NULL) {
    1408           0 :                 goto complete;
    1409             :         }
    1410             : 
    1411           7 :         nbdev_ch = spdk_io_channel_get_ctx(spdk_bdev_io_get_io_channel(bdev_io));
    1412             : 
    1413           7 :         if (bdev_nvme_check_retry_io(bio, cpl, nbdev_ch, &delay_ms)) {
    1414           7 :                 bdev_nvme_queue_retry_io(nbdev_ch, bio, delay_ms);
    1415           7 :                 return;
    1416             :         }
    1417             : 
    1418          25 : complete:
    1419          25 :         bio->retry_count = 0;
    1420          25 :         bio->submit_tsc = 0;
    1421          25 :         bdev_io->u.bdev.accel_sequence = NULL;
    1422          25 :         __bdev_nvme_io_complete(bdev_io, 0, cpl);
    1423             : }
    1424             : 
    1425             : static inline void
    1426          11 : bdev_nvme_io_complete(struct nvme_bdev_io *bio, int rc)
    1427             : {
    1428          11 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    1429             :         struct nvme_bdev_channel *nbdev_ch;
    1430             :         enum spdk_bdev_io_status io_status;
    1431             : 
    1432          11 :         assert(!bdev_nvme_io_type_is_admin(bdev_io->type));
    1433             : 
    1434          11 :         switch (rc) {
    1435           1 :         case 0:
    1436           1 :                 io_status = SPDK_BDEV_IO_STATUS_SUCCESS;
    1437           1 :                 break;
    1438           0 :         case -ENOMEM:
    1439           0 :                 io_status = SPDK_BDEV_IO_STATUS_NOMEM;
    1440           0 :                 break;
    1441          10 :         case -ENXIO:
    1442          10 :                 if (g_opts.bdev_retry_count == -1 || bio->retry_count < g_opts.bdev_retry_count) {
    1443          10 :                         nbdev_ch = spdk_io_channel_get_ctx(spdk_bdev_io_get_io_channel(bdev_io));
    1444             : 
    1445          10 :                         bdev_nvme_clear_current_io_path(nbdev_ch);
    1446          10 :                         bio->io_path = NULL;
    1447             : 
    1448          10 :                         if (any_io_path_may_become_available(nbdev_ch)) {
    1449           8 :                                 bdev_nvme_queue_retry_io(nbdev_ch, bio, 1000ULL);
    1450           8 :                                 return;
    1451             :                         }
    1452             :                 }
    1453             : 
    1454             :         /* fallthrough */
    1455             :         default:
    1456           2 :                 spdk_accel_sequence_abort(bdev_io->u.bdev.accel_sequence);
    1457           2 :                 bdev_io->u.bdev.accel_sequence = NULL;
    1458           2 :                 io_status = SPDK_BDEV_IO_STATUS_FAILED;
    1459           2 :                 break;
    1460             :         }
    1461             : 
    1462           3 :         bio->retry_count = 0;
    1463           3 :         bio->submit_tsc = 0;
    1464           3 :         __bdev_nvme_io_complete(bdev_io, io_status, NULL);
    1465             : }
    1466             : 
    1467             : static inline void
    1468           4 : bdev_nvme_admin_complete(struct nvme_bdev_io *bio, int rc)
    1469             : {
    1470           4 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    1471             :         enum spdk_bdev_io_status io_status;
    1472             : 
    1473           4 :         switch (rc) {
    1474           1 :         case 0:
    1475           1 :                 io_status = SPDK_BDEV_IO_STATUS_SUCCESS;
    1476           1 :                 break;
    1477           0 :         case -ENOMEM:
    1478           0 :                 io_status = SPDK_BDEV_IO_STATUS_NOMEM;
    1479           0 :                 break;
    1480           3 :         case -ENXIO:
    1481             :         /* fallthrough */
    1482             :         default:
    1483           3 :                 io_status = SPDK_BDEV_IO_STATUS_FAILED;
    1484           3 :                 break;
    1485             :         }
    1486             : 
    1487           4 :         __bdev_nvme_io_complete(bdev_io, io_status, NULL);
    1488           4 : }
    1489             : 
    1490             : static void
    1491           3 : bdev_nvme_clear_io_path_caches_done(struct spdk_io_channel_iter *i, int status)
    1492             : {
    1493           3 :         struct nvme_ctrlr *nvme_ctrlr = spdk_io_channel_iter_get_io_device(i);
    1494             : 
    1495           3 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    1496             : 
    1497           3 :         assert(nvme_ctrlr->io_path_cache_clearing == true);
    1498           3 :         nvme_ctrlr->io_path_cache_clearing = false;
    1499             : 
    1500           3 :         if (!nvme_ctrlr_can_be_unregistered(nvme_ctrlr)) {
    1501           3 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    1502           3 :                 return;
    1503             :         }
    1504             : 
    1505           0 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    1506             : 
    1507           0 :         nvme_ctrlr_unregister(nvme_ctrlr);
    1508             : }
    1509             : 
    1510             : static void
    1511         320 : _bdev_nvme_clear_io_path_cache(struct nvme_qpair *nvme_qpair)
    1512             : {
    1513             :         struct nvme_io_path *io_path;
    1514             : 
    1515         459 :         TAILQ_FOREACH(io_path, &nvme_qpair->io_path_list, tailq) {
    1516         139 :                 if (io_path->nbdev_ch == NULL) {
    1517          64 :                         continue;
    1518             :                 }
    1519          75 :                 bdev_nvme_clear_current_io_path(io_path->nbdev_ch);
    1520             :         }
    1521         320 : }
    1522             : 
    1523             : static void
    1524           1 : bdev_nvme_clear_io_path_cache(struct spdk_io_channel_iter *i)
    1525             : {
    1526           1 :         struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
    1527           1 :         struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(_ch);
    1528             : 
    1529           1 :         assert(ctrlr_ch->qpair != NULL);
    1530             : 
    1531           1 :         _bdev_nvme_clear_io_path_cache(ctrlr_ch->qpair);
    1532             : 
    1533           1 :         spdk_for_each_channel_continue(i, 0);
    1534           1 : }
    1535             : 
    1536             : static void
    1537           3 : bdev_nvme_clear_io_path_caches(struct nvme_ctrlr *nvme_ctrlr)
    1538             : {
    1539           3 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    1540           3 :         if (!nvme_ctrlr_is_available(nvme_ctrlr) ||
    1541             :             nvme_ctrlr->io_path_cache_clearing) {
    1542           0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    1543           0 :                 return;
    1544             :         }
    1545             : 
    1546           3 :         nvme_ctrlr->io_path_cache_clearing = true;
    1547           3 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    1548             : 
    1549           3 :         spdk_for_each_channel(nvme_ctrlr,
    1550             :                               bdev_nvme_clear_io_path_cache,
    1551             :                               NULL,
    1552             :                               bdev_nvme_clear_io_path_caches_done);
    1553             : }
    1554             : 
    1555             : static struct nvme_qpair *
    1556          99 : nvme_poll_group_get_qpair(struct nvme_poll_group *group, struct spdk_nvme_qpair *qpair)
    1557             : {
    1558             :         struct nvme_qpair *nvme_qpair;
    1559             : 
    1560         108 :         TAILQ_FOREACH(nvme_qpair, &group->qpair_list, tailq) {
    1561         108 :                 if (nvme_qpair->qpair == qpair) {
    1562          99 :                         break;
    1563             :                 }
    1564             :         }
    1565             : 
    1566          99 :         return nvme_qpair;
    1567             : }
    1568             : 
    1569             : static void nvme_qpair_delete(struct nvme_qpair *nvme_qpair);
    1570             : 
    1571             : static void
    1572          99 : bdev_nvme_disconnected_qpair_cb(struct spdk_nvme_qpair *qpair, void *poll_group_ctx)
    1573             : {
    1574          99 :         struct nvme_poll_group *group = poll_group_ctx;
    1575             :         struct nvme_qpair *nvme_qpair;
    1576             :         struct nvme_ctrlr_channel *ctrlr_ch;
    1577             :         int status;
    1578             : 
    1579          99 :         nvme_qpair = nvme_poll_group_get_qpair(group, qpair);
    1580          99 :         if (nvme_qpair == NULL) {
    1581           0 :                 return;
    1582             :         }
    1583             : 
    1584          99 :         if (nvme_qpair->qpair != NULL) {
    1585          99 :                 spdk_nvme_ctrlr_free_io_qpair(nvme_qpair->qpair);
    1586          99 :                 nvme_qpair->qpair = NULL;
    1587             :         }
    1588             : 
    1589          99 :         _bdev_nvme_clear_io_path_cache(nvme_qpair);
    1590             : 
    1591          99 :         ctrlr_ch = nvme_qpair->ctrlr_ch;
    1592             : 
    1593          99 :         if (ctrlr_ch != NULL) {
    1594          56 :                 if (ctrlr_ch->reset_iter != NULL) {
    1595             :                         /* We are in a full reset sequence. */
    1596          52 :                         if (ctrlr_ch->connect_poller != NULL) {
    1597             :                                 /* qpair was failed to connect. Abort the reset sequence. */
    1598           0 :                                 SPDK_DEBUGLOG(bdev_nvme, "qpair %p was failed to connect. abort the reset ctrlr sequence.\n",
    1599             :                                               qpair);
    1600           0 :                                 spdk_poller_unregister(&ctrlr_ch->connect_poller);
    1601           0 :                                 status = -1;
    1602             :                         } else {
    1603             :                                 /* qpair was completed to disconnect. Just move to the next ctrlr_channel. */
    1604          52 :                                 SPDK_DEBUGLOG(bdev_nvme, "qpair %p was disconnected and freed in a reset ctrlr sequence.\n",
    1605             :                                               qpair);
    1606          52 :                                 status = 0;
    1607             :                         }
    1608          52 :                         spdk_for_each_channel_continue(ctrlr_ch->reset_iter, status);
    1609          52 :                         ctrlr_ch->reset_iter = NULL;
    1610             :                 } else {
    1611             :                         /* qpair was disconnected unexpectedly. Reset controller for recovery. */
    1612           4 :                         SPDK_NOTICELOG("qpair %p was disconnected and freed. reset controller.\n", qpair);
    1613           4 :                         bdev_nvme_failover_ctrlr(nvme_qpair->ctrlr);
    1614             :                 }
    1615             :         } else {
    1616             :                 /* In this case, ctrlr_channel is already deleted. */
    1617          43 :                 SPDK_DEBUGLOG(bdev_nvme, "qpair %p was disconnected and freed. delete nvme_qpair.\n", qpair);
    1618          43 :                 nvme_qpair_delete(nvme_qpair);
    1619             :         }
    1620             : }
    1621             : 
    1622             : static void
    1623           0 : bdev_nvme_check_io_qpairs(struct nvme_poll_group *group)
    1624             : {
    1625             :         struct nvme_qpair *nvme_qpair;
    1626             : 
    1627           0 :         TAILQ_FOREACH(nvme_qpair, &group->qpair_list, tailq) {
    1628           0 :                 if (nvme_qpair->qpair == NULL || nvme_qpair->ctrlr_ch == NULL) {
    1629           0 :                         continue;
    1630             :                 }
    1631             : 
    1632           0 :                 if (spdk_nvme_qpair_get_failure_reason(nvme_qpair->qpair) !=
    1633             :                     SPDK_NVME_QPAIR_FAILURE_NONE) {
    1634           0 :                         _bdev_nvme_clear_io_path_cache(nvme_qpair);
    1635             :                 }
    1636             :         }
    1637           0 : }
    1638             : 
    1639             : static int
    1640        1025 : bdev_nvme_poll(void *arg)
    1641             : {
    1642        1025 :         struct nvme_poll_group *group = arg;
    1643             :         int64_t num_completions;
    1644             : 
    1645        1025 :         if (group->collect_spin_stat && group->start_ticks == 0) {
    1646           0 :                 group->start_ticks = spdk_get_ticks();
    1647             :         }
    1648             : 
    1649        1025 :         num_completions = spdk_nvme_poll_group_process_completions(group->group, 0,
    1650             :                           bdev_nvme_disconnected_qpair_cb);
    1651        1025 :         if (group->collect_spin_stat) {
    1652           0 :                 if (num_completions > 0) {
    1653           0 :                         if (group->end_ticks != 0) {
    1654           0 :                                 group->spin_ticks += (group->end_ticks - group->start_ticks);
    1655           0 :                                 group->end_ticks = 0;
    1656             :                         }
    1657           0 :                         group->start_ticks = 0;
    1658             :                 } else {
    1659           0 :                         group->end_ticks = spdk_get_ticks();
    1660             :                 }
    1661             :         }
    1662             : 
    1663        1025 :         if (spdk_unlikely(num_completions < 0)) {
    1664           0 :                 bdev_nvme_check_io_qpairs(group);
    1665             :         }
    1666             : 
    1667        1025 :         return num_completions > 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE;
    1668             : }
    1669             : 
    1670             : static int bdev_nvme_poll_adminq(void *arg);
    1671             : 
    1672             : static void
    1673         100 : bdev_nvme_change_adminq_poll_period(struct nvme_ctrlr *nvme_ctrlr, uint64_t new_period_us)
    1674             : {
    1675         100 :         spdk_poller_unregister(&nvme_ctrlr->adminq_timer_poller);
    1676             : 
    1677         100 :         nvme_ctrlr->adminq_timer_poller = SPDK_POLLER_REGISTER(bdev_nvme_poll_adminq,
    1678             :                                           nvme_ctrlr, new_period_us);
    1679         100 : }
    1680             : 
    1681             : static int
    1682         146 : bdev_nvme_poll_adminq(void *arg)
    1683             : {
    1684             :         int32_t rc;
    1685         146 :         struct nvme_ctrlr *nvme_ctrlr = arg;
    1686             :         nvme_ctrlr_disconnected_cb disconnected_cb;
    1687             : 
    1688         146 :         assert(nvme_ctrlr != NULL);
    1689             : 
    1690         146 :         rc = spdk_nvme_ctrlr_process_admin_completions(nvme_ctrlr->ctrlr);
    1691         146 :         if (rc < 0) {
    1692          53 :                 disconnected_cb = nvme_ctrlr->disconnected_cb;
    1693          53 :                 nvme_ctrlr->disconnected_cb = NULL;
    1694             : 
    1695          53 :                 if (disconnected_cb != NULL) {
    1696          50 :                         bdev_nvme_change_adminq_poll_period(nvme_ctrlr,
    1697             :                                                             g_opts.nvme_adminq_poll_period_us);
    1698          50 :                         disconnected_cb(nvme_ctrlr);
    1699             :                 } else {
    1700           3 :                         bdev_nvme_failover_ctrlr(nvme_ctrlr);
    1701             :                 }
    1702          93 :         } else if (spdk_nvme_ctrlr_get_admin_qp_failure_reason(nvme_ctrlr->ctrlr) !=
    1703             :                    SPDK_NVME_QPAIR_FAILURE_NONE) {
    1704           0 :                 bdev_nvme_clear_io_path_caches(nvme_ctrlr);
    1705             :         }
    1706             : 
    1707         146 :         return rc == 0 ? SPDK_POLLER_IDLE : SPDK_POLLER_BUSY;
    1708             : }
    1709             : 
    1710             : static void
    1711          37 : nvme_bdev_free(void *io_device)
    1712             : {
    1713          37 :         struct nvme_bdev *nvme_disk = io_device;
    1714             : 
    1715          37 :         pthread_mutex_destroy(&nvme_disk->mutex);
    1716          37 :         free(nvme_disk->disk.name);
    1717          37 :         free(nvme_disk->err_stat);
    1718          37 :         free(nvme_disk);
    1719          37 : }
    1720             : 
    1721             : static int
    1722          36 : bdev_nvme_destruct(void *ctx)
    1723             : {
    1724          36 :         struct nvme_bdev *nvme_disk = ctx;
    1725             :         struct nvme_ns *nvme_ns, *tmp_nvme_ns;
    1726             : 
    1727             :         SPDK_DTRACE_PROBE2(bdev_nvme_destruct, nvme_disk->nbdev_ctrlr->name, nvme_disk->nsid);
    1728             : 
    1729          73 :         TAILQ_FOREACH_SAFE(nvme_ns, &nvme_disk->nvme_ns_list, tailq, tmp_nvme_ns) {
    1730          37 :                 pthread_mutex_lock(&nvme_ns->ctrlr->mutex);
    1731             : 
    1732          37 :                 nvme_ns->bdev = NULL;
    1733             : 
    1734          37 :                 assert(nvme_ns->id > 0);
    1735             : 
    1736          37 :                 if (nvme_ctrlr_get_ns(nvme_ns->ctrlr, nvme_ns->id) == NULL) {
    1737           0 :                         pthread_mutex_unlock(&nvme_ns->ctrlr->mutex);
    1738             : 
    1739           0 :                         nvme_ctrlr_release(nvme_ns->ctrlr);
    1740           0 :                         nvme_ns_free(nvme_ns);
    1741             :                 } else {
    1742          37 :                         pthread_mutex_unlock(&nvme_ns->ctrlr->mutex);
    1743             :                 }
    1744             :         }
    1745             : 
    1746          36 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    1747          36 :         TAILQ_REMOVE(&nvme_disk->nbdev_ctrlr->bdevs, nvme_disk, tailq);
    1748          36 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    1749             : 
    1750          36 :         spdk_io_device_unregister(nvme_disk, nvme_bdev_free);
    1751             : 
    1752          36 :         return 0;
    1753             : }
    1754             : 
    1755             : static int
    1756         100 : bdev_nvme_create_qpair(struct nvme_qpair *nvme_qpair)
    1757             : {
    1758             :         struct nvme_ctrlr *nvme_ctrlr;
    1759         100 :         struct spdk_nvme_io_qpair_opts opts;
    1760             :         struct spdk_nvme_qpair *qpair;
    1761             :         int rc;
    1762             : 
    1763         100 :         nvme_ctrlr = nvme_qpair->ctrlr;
    1764             : 
    1765         100 :         spdk_nvme_ctrlr_get_default_io_qpair_opts(nvme_ctrlr->ctrlr, &opts, sizeof(opts));
    1766         100 :         opts.delay_cmd_submit = g_opts.delay_cmd_submit;
    1767         100 :         opts.create_only = true;
    1768         100 :         opts.async_mode = true;
    1769         100 :         opts.io_queue_requests = spdk_max(g_opts.io_queue_requests, opts.io_queue_requests);
    1770         100 :         g_opts.io_queue_requests = opts.io_queue_requests;
    1771             : 
    1772         100 :         qpair = spdk_nvme_ctrlr_alloc_io_qpair(nvme_ctrlr->ctrlr, &opts, sizeof(opts));
    1773         100 :         if (qpair == NULL) {
    1774           0 :                 return -1;
    1775             :         }
    1776             : 
    1777             :         SPDK_DTRACE_PROBE3(bdev_nvme_create_qpair, nvme_ctrlr->nbdev_ctrlr->name,
    1778             :                            spdk_nvme_qpair_get_id(qpair), spdk_thread_get_id(nvme_ctrlr->thread));
    1779             : 
    1780         100 :         assert(nvme_qpair->group != NULL);
    1781             : 
    1782         100 :         rc = spdk_nvme_poll_group_add(nvme_qpair->group->group, qpair);
    1783         100 :         if (rc != 0) {
    1784           0 :                 SPDK_ERRLOG("Unable to begin polling on NVMe Channel.\n");
    1785           0 :                 goto err;
    1786             :         }
    1787             : 
    1788         100 :         rc = spdk_nvme_ctrlr_connect_io_qpair(nvme_ctrlr->ctrlr, qpair);
    1789         100 :         if (rc != 0) {
    1790           0 :                 SPDK_ERRLOG("Unable to connect I/O qpair.\n");
    1791           0 :                 goto err;
    1792             :         }
    1793             : 
    1794         100 :         nvme_qpair->qpair = qpair;
    1795             : 
    1796         100 :         if (!g_opts.disable_auto_failback) {
    1797          71 :                 _bdev_nvme_clear_io_path_cache(nvme_qpair);
    1798             :         }
    1799             : 
    1800         100 :         return 0;
    1801             : 
    1802           0 : err:
    1803           0 :         spdk_nvme_ctrlr_free_io_qpair(qpair);
    1804             : 
    1805           0 :         return rc;
    1806             : }
    1807             : 
    1808             : static void bdev_nvme_reset_io_continue(void *cb_arg, int rc);
    1809             : 
    1810             : static void
    1811          82 : bdev_nvme_complete_pending_resets(struct spdk_io_channel_iter *i)
    1812             : {
    1813          82 :         struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
    1814          82 :         struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(_ch);
    1815          82 :         int rc = 0;
    1816             :         struct spdk_bdev_io *bdev_io;
    1817             :         struct nvme_bdev_io *bio;
    1818             : 
    1819          82 :         if (spdk_io_channel_iter_get_ctx(i) != NULL) {
    1820          35 :                 rc = -1;
    1821             :         }
    1822             : 
    1823          86 :         while (!TAILQ_EMPTY(&ctrlr_ch->pending_resets)) {
    1824           4 :                 bdev_io = TAILQ_FIRST(&ctrlr_ch->pending_resets);
    1825           4 :                 TAILQ_REMOVE(&ctrlr_ch->pending_resets, bdev_io, module_link);
    1826             : 
    1827           4 :                 bio = (struct nvme_bdev_io *)bdev_io->driver_ctx;
    1828           4 :                 bdev_nvme_reset_io_continue(bio, rc);
    1829             :         }
    1830             : 
    1831          82 :         spdk_for_each_channel_continue(i, 0);
    1832          82 : }
    1833             : 
    1834             : /* This function marks the current trid as failed by storing the current ticks
    1835             :  * and then sets the next trid to the active trid within a controller if exists.
    1836             :  *
    1837             :  * The purpose of the boolean return value is to request the caller to disconnect
    1838             :  * the current trid now to try connecting the next trid.
    1839             :  */
    1840             : static bool
    1841          36 : bdev_nvme_failover_trid(struct nvme_ctrlr *nvme_ctrlr, bool remove, bool start)
    1842             : {
    1843             :         struct nvme_path_id *path_id, *next_path;
    1844             :         int rc __attribute__((unused));
    1845             : 
    1846          36 :         path_id = TAILQ_FIRST(&nvme_ctrlr->trids);
    1847          36 :         assert(path_id);
    1848          36 :         assert(path_id == nvme_ctrlr->active_path_id);
    1849          36 :         next_path = TAILQ_NEXT(path_id, link);
    1850             : 
    1851             :         /* Update the last failed time. It means the trid is failed if its last
    1852             :          * failed time is non-zero.
    1853             :          */
    1854          36 :         path_id->last_failed_tsc = spdk_get_ticks();
    1855             : 
    1856          36 :         if (next_path == NULL) {
    1857             :                 /* There is no alternate trid within a controller. */
    1858          25 :                 return false;
    1859             :         }
    1860             : 
    1861          11 :         if (!start && nvme_ctrlr->opts.reconnect_delay_sec == 0) {
    1862             :                 /* Connect is not retried in a controller reset sequence. Connecting
    1863             :                  * the next trid will be done by the next bdev_nvme_failover_ctrlr() call.
    1864             :                  */
    1865           3 :                 return false;
    1866             :         }
    1867             : 
    1868           8 :         assert(path_id->trid.trtype != SPDK_NVME_TRANSPORT_PCIE);
    1869             : 
    1870           8 :         SPDK_NOTICELOG("Start failover from %s:%s to %s:%s\n", path_id->trid.traddr,
    1871             :                        path_id->trid.trsvcid,        next_path->trid.traddr, next_path->trid.trsvcid);
    1872             : 
    1873           8 :         spdk_nvme_ctrlr_fail(nvme_ctrlr->ctrlr);
    1874           8 :         nvme_ctrlr->active_path_id = next_path;
    1875           8 :         rc = spdk_nvme_ctrlr_set_trid(nvme_ctrlr->ctrlr, &next_path->trid);
    1876           8 :         assert(rc == 0);
    1877           8 :         TAILQ_REMOVE(&nvme_ctrlr->trids, path_id, link);
    1878           8 :         if (!remove) {
    1879             :                 /** Shuffle the old trid to the end of the list and use the new one.
    1880             :                  * Allows for round robin through multiple connections.
    1881             :                  */
    1882           6 :                 TAILQ_INSERT_TAIL(&nvme_ctrlr->trids, path_id, link);
    1883             :         } else {
    1884           2 :                 free(path_id);
    1885             :         }
    1886             : 
    1887           8 :         if (start || next_path->last_failed_tsc == 0) {
    1888             :                 /* bdev_nvme_failover_ctrlr() is just called or the next trid is not failed
    1889             :                  * or used yet. Try the next trid now.
    1890             :                  */
    1891           7 :                 return true;
    1892             :         }
    1893             : 
    1894           1 :         if (spdk_get_ticks() > next_path->last_failed_tsc + spdk_get_ticks_hz() *
    1895           1 :             nvme_ctrlr->opts.reconnect_delay_sec) {
    1896             :                 /* Enough backoff passed since the next trid failed. Try the next trid now. */
    1897           0 :                 return true;
    1898             :         }
    1899             : 
    1900             :         /* The next trid will be tried after reconnect_delay_sec seconds. */
    1901           1 :         return false;
    1902             : }
    1903             : 
    1904             : static bool
    1905          68 : bdev_nvme_check_ctrlr_loss_timeout(struct nvme_ctrlr *nvme_ctrlr)
    1906             : {
    1907             :         int32_t elapsed;
    1908             : 
    1909          68 :         if (nvme_ctrlr->opts.ctrlr_loss_timeout_sec == 0 ||
    1910          36 :             nvme_ctrlr->opts.ctrlr_loss_timeout_sec == -1) {
    1911          42 :                 return false;
    1912             :         }
    1913             : 
    1914          26 :         elapsed = (spdk_get_ticks() - nvme_ctrlr->reset_start_tsc) / spdk_get_ticks_hz();
    1915          26 :         if (elapsed >= nvme_ctrlr->opts.ctrlr_loss_timeout_sec) {
    1916           6 :                 return true;
    1917             :         } else {
    1918          20 :                 return false;
    1919             :         }
    1920             : }
    1921             : 
    1922             : static bool
    1923          12 : bdev_nvme_check_fast_io_fail_timeout(struct nvme_ctrlr *nvme_ctrlr)
    1924             : {
    1925             :         uint32_t elapsed;
    1926             : 
    1927          12 :         if (nvme_ctrlr->opts.fast_io_fail_timeout_sec == 0) {
    1928           8 :                 return false;
    1929             :         }
    1930             : 
    1931           4 :         elapsed = (spdk_get_ticks() - nvme_ctrlr->reset_start_tsc) / spdk_get_ticks_hz();
    1932           4 :         if (elapsed >= nvme_ctrlr->opts.fast_io_fail_timeout_sec) {
    1933           2 :                 return true;
    1934             :         } else {
    1935           2 :                 return false;
    1936             :         }
    1937             : }
    1938             : 
    1939             : static void bdev_nvme_reset_ctrlr_complete(struct nvme_ctrlr *nvme_ctrlr, bool success);
    1940             : 
    1941             : static void
    1942          51 : nvme_ctrlr_disconnect(struct nvme_ctrlr *nvme_ctrlr, nvme_ctrlr_disconnected_cb cb_fn)
    1943             : {
    1944             :         int rc;
    1945             : 
    1946          51 :         rc = spdk_nvme_ctrlr_disconnect(nvme_ctrlr->ctrlr);
    1947          51 :         if (rc != 0) {
    1948             :                 /* Disconnect fails if ctrlr is already resetting or removed. In this case,
    1949             :                  * fail the reset sequence immediately.
    1950             :                  */
    1951           1 :                 bdev_nvme_reset_ctrlr_complete(nvme_ctrlr, false);
    1952           1 :                 return;
    1953             :         }
    1954             : 
    1955             :         /* spdk_nvme_ctrlr_disconnect() may complete asynchronously later by polling adminq.
    1956             :          * Set callback here to execute the specified operation after ctrlr is really disconnected.
    1957             :          */
    1958          50 :         assert(nvme_ctrlr->disconnected_cb == NULL);
    1959          50 :         nvme_ctrlr->disconnected_cb = cb_fn;
    1960             : 
    1961             :         /* During disconnection, reduce the period to poll adminq more often. */
    1962          50 :         bdev_nvme_change_adminq_poll_period(nvme_ctrlr, 0);
    1963             : }
    1964             : 
    1965             : enum bdev_nvme_op_after_reset {
    1966             :         OP_NONE,
    1967             :         OP_COMPLETE_PENDING_DESTRUCT,
    1968             :         OP_DESTRUCT,
    1969             :         OP_DELAYED_RECONNECT,
    1970             :         OP_FAILOVER,
    1971             : };
    1972             : 
    1973             : typedef enum bdev_nvme_op_after_reset _bdev_nvme_op_after_reset;
    1974             : 
    1975             : static _bdev_nvme_op_after_reset
    1976          50 : bdev_nvme_check_op_after_reset(struct nvme_ctrlr *nvme_ctrlr, bool success)
    1977             : {
    1978          50 :         if (nvme_ctrlr_can_be_unregistered(nvme_ctrlr)) {
    1979             :                 /* Complete pending destruct after reset completes. */
    1980           0 :                 return OP_COMPLETE_PENDING_DESTRUCT;
    1981          50 :         } else if (nvme_ctrlr->pending_failover) {
    1982           3 :                 nvme_ctrlr->pending_failover = false;
    1983           3 :                 nvme_ctrlr->reset_start_tsc = 0;
    1984           3 :                 return OP_FAILOVER;
    1985          47 :         } else if (success || nvme_ctrlr->opts.reconnect_delay_sec == 0) {
    1986          33 :                 nvme_ctrlr->reset_start_tsc = 0;
    1987          33 :                 return OP_NONE;
    1988          14 :         } else if (bdev_nvme_check_ctrlr_loss_timeout(nvme_ctrlr)) {
    1989           2 :                 return OP_DESTRUCT;
    1990             :         } else {
    1991          12 :                 if (bdev_nvme_check_fast_io_fail_timeout(nvme_ctrlr)) {
    1992           2 :                         nvme_ctrlr->fast_io_fail_timedout = true;
    1993             :                 }
    1994          12 :                 return OP_DELAYED_RECONNECT;
    1995             :         }
    1996             : }
    1997             : 
    1998             : static int bdev_nvme_delete_ctrlr(struct nvme_ctrlr *nvme_ctrlr, bool hotplug);
    1999             : static void bdev_nvme_reconnect_ctrlr(struct nvme_ctrlr *nvme_ctrlr);
    2000             : 
    2001             : static int
    2002           9 : bdev_nvme_reconnect_delay_timer_expired(void *ctx)
    2003             : {
    2004           9 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    2005             : 
    2006             :         SPDK_DTRACE_PROBE1(bdev_nvme_ctrlr_reconnect_delay, nvme_ctrlr->nbdev_ctrlr->name);
    2007           9 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2008             : 
    2009           9 :         spdk_poller_unregister(&nvme_ctrlr->reconnect_delay_timer);
    2010             : 
    2011           9 :         if (!nvme_ctrlr->reconnect_is_delayed) {
    2012           0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2013           0 :                 return SPDK_POLLER_BUSY;
    2014             :         }
    2015             : 
    2016           9 :         nvme_ctrlr->reconnect_is_delayed = false;
    2017             : 
    2018           9 :         if (nvme_ctrlr->destruct) {
    2019           0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2020           0 :                 return SPDK_POLLER_BUSY;
    2021             :         }
    2022             : 
    2023           9 :         assert(nvme_ctrlr->resetting == false);
    2024           9 :         nvme_ctrlr->resetting = true;
    2025             : 
    2026           9 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2027             : 
    2028           9 :         spdk_poller_resume(nvme_ctrlr->adminq_timer_poller);
    2029             : 
    2030           9 :         bdev_nvme_reconnect_ctrlr(nvme_ctrlr);
    2031           9 :         return SPDK_POLLER_BUSY;
    2032             : }
    2033             : 
    2034             : static void
    2035          12 : bdev_nvme_start_reconnect_delay_timer(struct nvme_ctrlr *nvme_ctrlr)
    2036             : {
    2037          12 :         spdk_poller_pause(nvme_ctrlr->adminq_timer_poller);
    2038             : 
    2039          12 :         assert(nvme_ctrlr->reconnect_is_delayed == false);
    2040          12 :         nvme_ctrlr->reconnect_is_delayed = true;
    2041             : 
    2042          12 :         assert(nvme_ctrlr->reconnect_delay_timer == NULL);
    2043          12 :         nvme_ctrlr->reconnect_delay_timer = SPDK_POLLER_REGISTER(bdev_nvme_reconnect_delay_timer_expired,
    2044             :                                             nvme_ctrlr,
    2045             :                                             nvme_ctrlr->opts.reconnect_delay_sec * SPDK_SEC_TO_USEC);
    2046          12 : }
    2047             : 
    2048             : static void remove_discovery_entry(struct nvme_ctrlr *nvme_ctrlr);
    2049             : 
    2050             : static void
    2051          48 : _bdev_nvme_reset_ctrlr_complete(struct spdk_io_channel_iter *i, int status)
    2052             : {
    2053          48 :         struct nvme_ctrlr *nvme_ctrlr = spdk_io_channel_iter_get_io_device(i);
    2054          48 :         bool success = spdk_io_channel_iter_get_ctx(i) == NULL;
    2055          48 :         bdev_nvme_ctrlr_op_cb ctrlr_op_cb_fn = nvme_ctrlr->ctrlr_op_cb_fn;
    2056          48 :         void *ctrlr_op_cb_arg = nvme_ctrlr->ctrlr_op_cb_arg;
    2057             :         enum bdev_nvme_op_after_reset op_after_reset;
    2058             : 
    2059          48 :         assert(nvme_ctrlr->thread == spdk_get_thread());
    2060             : 
    2061          48 :         nvme_ctrlr->ctrlr_op_cb_fn = NULL;
    2062          48 :         nvme_ctrlr->ctrlr_op_cb_arg = NULL;
    2063             : 
    2064          48 :         if (!success) {
    2065          21 :                 SPDK_ERRLOG("Resetting controller failed.\n");
    2066             :         } else {
    2067          27 :                 SPDK_NOTICELOG("Resetting controller successful.\n");
    2068             :         }
    2069             : 
    2070          48 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2071          48 :         nvme_ctrlr->resetting = false;
    2072          48 :         nvme_ctrlr->dont_retry = false;
    2073          48 :         nvme_ctrlr->in_failover = false;
    2074             : 
    2075          48 :         op_after_reset = bdev_nvme_check_op_after_reset(nvme_ctrlr, success);
    2076          48 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2077             : 
    2078             :         /* Delay callbacks when the next operation is a failover. */
    2079          48 :         if (ctrlr_op_cb_fn && op_after_reset != OP_FAILOVER) {
    2080          10 :                 ctrlr_op_cb_fn(ctrlr_op_cb_arg, success ? 0 : -1);
    2081             :         }
    2082             : 
    2083          48 :         switch (op_after_reset) {
    2084           0 :         case OP_COMPLETE_PENDING_DESTRUCT:
    2085           0 :                 nvme_ctrlr_unregister(nvme_ctrlr);
    2086           0 :                 break;
    2087           2 :         case OP_DESTRUCT:
    2088           2 :                 bdev_nvme_delete_ctrlr(nvme_ctrlr, false);
    2089           2 :                 remove_discovery_entry(nvme_ctrlr);
    2090           2 :                 break;
    2091          12 :         case OP_DELAYED_RECONNECT:
    2092          12 :                 nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_start_reconnect_delay_timer);
    2093          12 :                 break;
    2094           3 :         case OP_FAILOVER:
    2095           3 :                 nvme_ctrlr->ctrlr_op_cb_fn = ctrlr_op_cb_fn;
    2096           3 :                 nvme_ctrlr->ctrlr_op_cb_arg = ctrlr_op_cb_arg;
    2097           3 :                 bdev_nvme_failover_ctrlr(nvme_ctrlr);
    2098           3 :                 break;
    2099          31 :         default:
    2100          31 :                 break;
    2101             :         }
    2102          48 : }
    2103             : 
    2104             : static void
    2105          50 : bdev_nvme_reset_ctrlr_complete(struct nvme_ctrlr *nvme_ctrlr, bool success)
    2106             : {
    2107          50 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2108          50 :         if (!success) {
    2109             :                 /* Connecting the active trid failed. Set the next alternate trid to the
    2110             :                  * active trid if it exists.
    2111             :                  */
    2112          23 :                 if (bdev_nvme_failover_trid(nvme_ctrlr, false, false)) {
    2113             :                         /* The next alternate trid exists and is ready to try. Try it now. */
    2114           2 :                         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2115             : 
    2116           2 :                         nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_reconnect_ctrlr);
    2117           2 :                         return;
    2118             :                 }
    2119             : 
    2120             :                 /* We came here if there is no alternate trid or if the next trid exists but
    2121             :                  * is not ready to try. We will try the active trid after reconnect_delay_sec
    2122             :                  * seconds if it is non-zero or at the next reset call otherwise.
    2123             :                  */
    2124             :         } else {
    2125             :                 /* Connecting the active trid succeeded. Clear the last failed time because it
    2126             :                  * means the trid is failed if its last failed time is non-zero.
    2127             :                  */
    2128          27 :                 nvme_ctrlr->active_path_id->last_failed_tsc = 0;
    2129             :         }
    2130          48 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2131             : 
    2132             :         /* Make sure we clear any pending resets before returning. */
    2133          48 :         spdk_for_each_channel(nvme_ctrlr,
    2134             :                               bdev_nvme_complete_pending_resets,
    2135             :                               success ? NULL : (void *)0x1,
    2136             :                               _bdev_nvme_reset_ctrlr_complete);
    2137             : }
    2138             : 
    2139             : static void
    2140           0 : bdev_nvme_reset_create_qpairs_failed(struct spdk_io_channel_iter *i, int status)
    2141             : {
    2142           0 :         struct nvme_ctrlr *nvme_ctrlr = spdk_io_channel_iter_get_io_device(i);
    2143             : 
    2144           0 :         bdev_nvme_reset_ctrlr_complete(nvme_ctrlr, false);
    2145           0 : }
    2146             : 
    2147             : static void
    2148          62 : bdev_nvme_reset_destroy_qpair(struct spdk_io_channel_iter *i)
    2149             : {
    2150          62 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
    2151          62 :         struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch);
    2152             :         struct nvme_qpair *nvme_qpair;
    2153             : 
    2154          62 :         nvme_qpair = ctrlr_ch->qpair;
    2155          62 :         assert(nvme_qpair != NULL);
    2156             : 
    2157          62 :         _bdev_nvme_clear_io_path_cache(nvme_qpair);
    2158             : 
    2159          62 :         if (nvme_qpair->qpair != NULL) {
    2160          52 :                 if (nvme_qpair->ctrlr->dont_retry) {
    2161          39 :                         spdk_nvme_qpair_set_abort_dnr(nvme_qpair->qpair, true);
    2162             :                 }
    2163          52 :                 spdk_nvme_ctrlr_disconnect_io_qpair(nvme_qpair->qpair);
    2164             : 
    2165             :                 /* The current full reset sequence will move to the next
    2166             :                  * ctrlr_channel after the qpair is actually disconnected.
    2167             :                  */
    2168          52 :                 assert(ctrlr_ch->reset_iter == NULL);
    2169          52 :                 ctrlr_ch->reset_iter = i;
    2170             :         } else {
    2171          10 :                 spdk_for_each_channel_continue(i, 0);
    2172             :         }
    2173          62 : }
    2174             : 
    2175             : static void
    2176          27 : bdev_nvme_reset_create_qpairs_done(struct spdk_io_channel_iter *i, int status)
    2177             : {
    2178          27 :         struct nvme_ctrlr *nvme_ctrlr = spdk_io_channel_iter_get_io_device(i);
    2179             : 
    2180          27 :         if (status == 0) {
    2181          27 :                 bdev_nvme_reset_ctrlr_complete(nvme_ctrlr, true);
    2182             :         } else {
    2183             :                 /* Delete the added qpairs and quiesce ctrlr to make the states clean. */
    2184           0 :                 spdk_for_each_channel(nvme_ctrlr,
    2185             :                                       bdev_nvme_reset_destroy_qpair,
    2186             :                                       NULL,
    2187             :                                       bdev_nvme_reset_create_qpairs_failed);
    2188             :         }
    2189          27 : }
    2190             : 
    2191             : static int
    2192          43 : bdev_nvme_reset_check_qpair_connected(void *ctx)
    2193             : {
    2194          43 :         struct nvme_ctrlr_channel *ctrlr_ch = ctx;
    2195             : 
    2196          43 :         if (ctrlr_ch->reset_iter == NULL) {
    2197             :                 /* qpair was already failed to connect and the reset sequence is being aborted. */
    2198           0 :                 assert(ctrlr_ch->connect_poller == NULL);
    2199           0 :                 assert(ctrlr_ch->qpair->qpair == NULL);
    2200           0 :                 return SPDK_POLLER_BUSY;
    2201             :         }
    2202             : 
    2203          43 :         assert(ctrlr_ch->qpair->qpair != NULL);
    2204             : 
    2205          43 :         if (!spdk_nvme_qpair_is_connected(ctrlr_ch->qpair->qpair)) {
    2206           0 :                 return SPDK_POLLER_BUSY;
    2207             :         }
    2208             : 
    2209          43 :         spdk_poller_unregister(&ctrlr_ch->connect_poller);
    2210             : 
    2211             :         /* qpair was completed to connect. Move to the next ctrlr_channel */
    2212          43 :         spdk_for_each_channel_continue(ctrlr_ch->reset_iter, 0);
    2213          43 :         ctrlr_ch->reset_iter = NULL;
    2214             : 
    2215          43 :         if (!g_opts.disable_auto_failback) {
    2216          30 :                 _bdev_nvme_clear_io_path_cache(ctrlr_ch->qpair);
    2217             :         }
    2218             : 
    2219          43 :         return SPDK_POLLER_BUSY;
    2220             : }
    2221             : 
    2222             : static void
    2223          43 : bdev_nvme_reset_create_qpair(struct spdk_io_channel_iter *i)
    2224             : {
    2225          43 :         struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
    2226          43 :         struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(_ch);
    2227             :         int rc;
    2228             : 
    2229          43 :         rc = bdev_nvme_create_qpair(ctrlr_ch->qpair);
    2230          43 :         if (rc == 0) {
    2231          43 :                 ctrlr_ch->connect_poller = SPDK_POLLER_REGISTER(bdev_nvme_reset_check_qpair_connected,
    2232             :                                            ctrlr_ch, 0);
    2233             : 
    2234             :                 /* The current full reset sequence will move to the next
    2235             :                  * ctrlr_channel after the qpair is actually connected.
    2236             :                  */
    2237          43 :                 assert(ctrlr_ch->reset_iter == NULL);
    2238          43 :                 ctrlr_ch->reset_iter = i;
    2239             :         } else {
    2240           0 :                 spdk_for_each_channel_continue(i, rc);
    2241             :         }
    2242          43 : }
    2243             : 
    2244             : static void
    2245          27 : nvme_ctrlr_check_namespaces(struct nvme_ctrlr *nvme_ctrlr)
    2246             : {
    2247          27 :         struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
    2248             :         struct nvme_ns *nvme_ns;
    2249             : 
    2250          39 :         for (nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
    2251             :              nvme_ns != NULL;
    2252          12 :              nvme_ns = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns)) {
    2253          12 :                 if (!spdk_nvme_ctrlr_is_active_ns(ctrlr, nvme_ns->id)) {
    2254           1 :                         SPDK_DEBUGLOG(bdev_nvme, "NSID %u was removed during reset.\n", nvme_ns->id);
    2255             :                         /* NS can be added again. Just nullify nvme_ns->ns. */
    2256           1 :                         nvme_ns->ns = NULL;
    2257             :                 }
    2258             :         }
    2259          27 : }
    2260             : 
    2261             : 
    2262             : static int
    2263          49 : bdev_nvme_reconnect_ctrlr_poll(void *arg)
    2264             : {
    2265          49 :         struct nvme_ctrlr *nvme_ctrlr = arg;
    2266          49 :         int rc = -ETIMEDOUT;
    2267             : 
    2268          49 :         if (bdev_nvme_check_ctrlr_loss_timeout(nvme_ctrlr)) {
    2269             :                 /* Mark the ctrlr as failed. The next call to
    2270             :                  * spdk_nvme_ctrlr_reconnect_poll_async() will then
    2271             :                  * do the necessary cleanup and return failure.
    2272             :                  */
    2273           2 :                 spdk_nvme_ctrlr_fail(nvme_ctrlr->ctrlr);
    2274             :         }
    2275             : 
    2276          49 :         rc = spdk_nvme_ctrlr_reconnect_poll_async(nvme_ctrlr->ctrlr);
    2277          49 :         if (rc == -EAGAIN) {
    2278           0 :                 return SPDK_POLLER_BUSY;
    2279             :         }
    2280             : 
    2281          49 :         spdk_poller_unregister(&nvme_ctrlr->reset_detach_poller);
    2282          49 :         if (rc == 0) {
    2283          27 :                 nvme_ctrlr_check_namespaces(nvme_ctrlr);
    2284             : 
    2285             :                 /* Recreate all of the I/O queue pairs */
    2286          27 :                 spdk_for_each_channel(nvme_ctrlr,
    2287             :                                       bdev_nvme_reset_create_qpair,
    2288             :                                       NULL,
    2289             :                                       bdev_nvme_reset_create_qpairs_done);
    2290             :         } else {
    2291          22 :                 bdev_nvme_reset_ctrlr_complete(nvme_ctrlr, false);
    2292             :         }
    2293          49 :         return SPDK_POLLER_BUSY;
    2294             : }
    2295             : 
    2296             : static void
    2297          49 : bdev_nvme_reconnect_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
    2298             : {
    2299          49 :         spdk_nvme_ctrlr_reconnect_async(nvme_ctrlr->ctrlr);
    2300             : 
    2301             :         SPDK_DTRACE_PROBE1(bdev_nvme_ctrlr_reconnect, nvme_ctrlr->nbdev_ctrlr->name);
    2302          49 :         assert(nvme_ctrlr->reset_detach_poller == NULL);
    2303          49 :         nvme_ctrlr->reset_detach_poller = SPDK_POLLER_REGISTER(bdev_nvme_reconnect_ctrlr_poll,
    2304             :                                           nvme_ctrlr, 0);
    2305          49 : }
    2306             : 
    2307             : static void
    2308          36 : bdev_nvme_reset_destroy_qpair_done(struct spdk_io_channel_iter *i, int status)
    2309             : {
    2310          36 :         struct nvme_ctrlr *nvme_ctrlr = spdk_io_channel_iter_get_io_device(i);
    2311             : 
    2312             :         SPDK_DTRACE_PROBE1(bdev_nvme_ctrlr_reset, nvme_ctrlr->nbdev_ctrlr->name);
    2313          36 :         assert(status == 0);
    2314             : 
    2315          36 :         if (!spdk_nvme_ctrlr_is_fabrics(nvme_ctrlr->ctrlr)) {
    2316           0 :                 bdev_nvme_reconnect_ctrlr(nvme_ctrlr);
    2317             :         } else {
    2318          36 :                 nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_reconnect_ctrlr);
    2319             :         }
    2320          36 : }
    2321             : 
    2322             : static void
    2323          36 : bdev_nvme_reset_destroy_qpairs(struct nvme_ctrlr *nvme_ctrlr)
    2324             : {
    2325          36 :         spdk_for_each_channel(nvme_ctrlr,
    2326             :                               bdev_nvme_reset_destroy_qpair,
    2327             :                               NULL,
    2328             :                               bdev_nvme_reset_destroy_qpair_done);
    2329          36 : }
    2330             : 
    2331             : static void
    2332           3 : bdev_nvme_reconnect_ctrlr_now(void *ctx)
    2333             : {
    2334           3 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    2335             : 
    2336           3 :         assert(nvme_ctrlr->resetting == true);
    2337           3 :         assert(nvme_ctrlr->thread == spdk_get_thread());
    2338             : 
    2339           3 :         spdk_poller_unregister(&nvme_ctrlr->reconnect_delay_timer);
    2340             : 
    2341           3 :         spdk_poller_resume(nvme_ctrlr->adminq_timer_poller);
    2342             : 
    2343           3 :         bdev_nvme_reconnect_ctrlr(nvme_ctrlr);
    2344           3 : }
    2345             : 
    2346             : static void
    2347          36 : _bdev_nvme_reset_ctrlr(void *ctx)
    2348             : {
    2349          36 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    2350             : 
    2351          36 :         assert(nvme_ctrlr->resetting == true);
    2352          36 :         assert(nvme_ctrlr->thread == spdk_get_thread());
    2353             : 
    2354          36 :         if (!spdk_nvme_ctrlr_is_fabrics(nvme_ctrlr->ctrlr)) {
    2355           0 :                 nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_reset_destroy_qpairs);
    2356             :         } else {
    2357          36 :                 bdev_nvme_reset_destroy_qpairs(nvme_ctrlr);
    2358             :         }
    2359          36 : }
    2360             : 
    2361             : static int
    2362          34 : bdev_nvme_reset_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
    2363             : {
    2364             :         spdk_msg_fn msg_fn;
    2365             : 
    2366          34 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2367          34 :         if (nvme_ctrlr->destruct) {
    2368           3 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2369           3 :                 return -ENXIO;
    2370             :         }
    2371             : 
    2372          31 :         if (nvme_ctrlr->resetting) {
    2373           6 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2374           6 :                 SPDK_NOTICELOG("Unable to perform reset, already in progress.\n");
    2375           6 :                 return -EBUSY;
    2376             :         }
    2377             : 
    2378          25 :         if (nvme_ctrlr->disabled) {
    2379           0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2380           0 :                 SPDK_NOTICELOG("Unable to perform reset. Controller is disabled.\n");
    2381           0 :                 return -EALREADY;
    2382             :         }
    2383             : 
    2384          25 :         nvme_ctrlr->resetting = true;
    2385          25 :         nvme_ctrlr->dont_retry = true;
    2386             : 
    2387          25 :         if (nvme_ctrlr->reconnect_is_delayed) {
    2388           1 :                 SPDK_DEBUGLOG(bdev_nvme, "Reconnect is already scheduled.\n");
    2389           1 :                 msg_fn = bdev_nvme_reconnect_ctrlr_now;
    2390           1 :                 nvme_ctrlr->reconnect_is_delayed = false;
    2391             :         } else {
    2392          24 :                 msg_fn = _bdev_nvme_reset_ctrlr;
    2393          24 :                 assert(nvme_ctrlr->reset_start_tsc == 0);
    2394             :         }
    2395             : 
    2396          25 :         nvme_ctrlr->reset_start_tsc = spdk_get_ticks();
    2397             : 
    2398          25 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2399             : 
    2400          25 :         spdk_thread_send_msg(nvme_ctrlr->thread, msg_fn, nvme_ctrlr);
    2401          25 :         return 0;
    2402             : }
    2403             : 
    2404             : static int
    2405           3 : bdev_nvme_enable_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
    2406             : {
    2407           3 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2408           3 :         if (nvme_ctrlr->destruct) {
    2409           0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2410           0 :                 return -ENXIO;
    2411             :         }
    2412             : 
    2413           3 :         if (nvme_ctrlr->resetting) {
    2414           0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2415           0 :                 return -EBUSY;
    2416             :         }
    2417             : 
    2418           3 :         if (!nvme_ctrlr->disabled) {
    2419           1 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2420           1 :                 return -EALREADY;
    2421             :         }
    2422             : 
    2423           2 :         nvme_ctrlr->disabled = false;
    2424           2 :         nvme_ctrlr->resetting = true;
    2425             : 
    2426           2 :         nvme_ctrlr->reset_start_tsc = spdk_get_ticks();
    2427             : 
    2428           2 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2429             : 
    2430           2 :         spdk_thread_send_msg(nvme_ctrlr->thread, bdev_nvme_reconnect_ctrlr_now, nvme_ctrlr);
    2431           2 :         return 0;
    2432             : }
    2433             : 
    2434             : static void
    2435           2 : _bdev_nvme_disable_ctrlr_complete(struct spdk_io_channel_iter *i, int status)
    2436             : {
    2437           2 :         struct nvme_ctrlr *nvme_ctrlr = spdk_io_channel_iter_get_io_device(i);
    2438           2 :         bdev_nvme_ctrlr_op_cb ctrlr_op_cb_fn = nvme_ctrlr->ctrlr_op_cb_fn;
    2439           2 :         void *ctrlr_op_cb_arg = nvme_ctrlr->ctrlr_op_cb_arg;
    2440             :         enum bdev_nvme_op_after_reset op_after_disable;
    2441             : 
    2442           2 :         assert(nvme_ctrlr->thread == spdk_get_thread());
    2443             : 
    2444           2 :         nvme_ctrlr->ctrlr_op_cb_fn = NULL;
    2445           2 :         nvme_ctrlr->ctrlr_op_cb_arg = NULL;
    2446             : 
    2447           2 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2448             : 
    2449           2 :         nvme_ctrlr->resetting = false;
    2450           2 :         nvme_ctrlr->dont_retry = false;
    2451             : 
    2452           2 :         op_after_disable = bdev_nvme_check_op_after_reset(nvme_ctrlr, true);
    2453             : 
    2454           2 :         nvme_ctrlr->disabled = true;
    2455           2 :         spdk_poller_pause(nvme_ctrlr->adminq_timer_poller);
    2456             : 
    2457           2 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2458             : 
    2459           2 :         if (ctrlr_op_cb_fn) {
    2460           0 :                 ctrlr_op_cb_fn(ctrlr_op_cb_arg, 0);
    2461             :         }
    2462             : 
    2463           2 :         switch (op_after_disable) {
    2464           0 :         case OP_COMPLETE_PENDING_DESTRUCT:
    2465           0 :                 nvme_ctrlr_unregister(nvme_ctrlr);
    2466           0 :                 break;
    2467           2 :         default:
    2468           2 :                 break;
    2469             :         }
    2470             : 
    2471           2 : }
    2472             : 
    2473             : static void
    2474           2 : bdev_nvme_disable_ctrlr_complete(struct nvme_ctrlr *nvme_ctrlr)
    2475             : {
    2476             :         /* Make sure we clear any pending resets before returning. */
    2477           2 :         spdk_for_each_channel(nvme_ctrlr,
    2478             :                               bdev_nvme_complete_pending_resets,
    2479             :                               NULL,
    2480             :                               _bdev_nvme_disable_ctrlr_complete);
    2481           2 : }
    2482             : 
    2483             : static void
    2484           1 : bdev_nvme_disable_destroy_qpairs_done(struct spdk_io_channel_iter *i, int status)
    2485             : {
    2486           1 :         struct nvme_ctrlr *nvme_ctrlr = spdk_io_channel_iter_get_io_device(i);
    2487             : 
    2488           1 :         assert(status == 0);
    2489             : 
    2490           1 :         if (!spdk_nvme_ctrlr_is_fabrics(nvme_ctrlr->ctrlr)) {
    2491           0 :                 bdev_nvme_disable_ctrlr_complete(nvme_ctrlr);
    2492             :         } else {
    2493           1 :                 nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_disable_ctrlr_complete);
    2494             :         }
    2495           1 : }
    2496             : 
    2497             : static void
    2498           1 : bdev_nvme_disable_destroy_qpairs(struct nvme_ctrlr *nvme_ctrlr)
    2499             : {
    2500           1 :         spdk_for_each_channel(nvme_ctrlr,
    2501             :                               bdev_nvme_reset_destroy_qpair,
    2502             :                               NULL,
    2503             :                               bdev_nvme_disable_destroy_qpairs_done);
    2504           1 : }
    2505             : 
    2506             : static void
    2507           1 : _bdev_nvme_cancel_reconnect_and_disable_ctrlr(void *ctx)
    2508             : {
    2509           1 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    2510             : 
    2511           1 :         assert(nvme_ctrlr->resetting == true);
    2512           1 :         assert(nvme_ctrlr->thread == spdk_get_thread());
    2513             : 
    2514           1 :         spdk_poller_unregister(&nvme_ctrlr->reconnect_delay_timer);
    2515             : 
    2516           1 :         bdev_nvme_disable_ctrlr_complete(nvme_ctrlr);
    2517           1 : }
    2518             : 
    2519             : static void
    2520           1 : _bdev_nvme_disconnect_and_disable_ctrlr(void *ctx)
    2521             : {
    2522           1 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    2523             : 
    2524           1 :         assert(nvme_ctrlr->resetting == true);
    2525           1 :         assert(nvme_ctrlr->thread == spdk_get_thread());
    2526             : 
    2527           1 :         if (!spdk_nvme_ctrlr_is_fabrics(nvme_ctrlr->ctrlr)) {
    2528           0 :                 nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_disable_destroy_qpairs);
    2529             :         } else {
    2530           1 :                 bdev_nvme_disable_destroy_qpairs(nvme_ctrlr);
    2531             :         }
    2532           1 : }
    2533             : 
    2534             : static int
    2535           5 : bdev_nvme_disable_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
    2536             : {
    2537             :         spdk_msg_fn msg_fn;
    2538             : 
    2539           5 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2540           5 :         if (nvme_ctrlr->destruct) {
    2541           1 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2542           1 :                 return -ENXIO;
    2543             :         }
    2544             : 
    2545           4 :         if (nvme_ctrlr->resetting) {
    2546           1 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2547           1 :                 return -EBUSY;
    2548             :         }
    2549             : 
    2550           3 :         if (nvme_ctrlr->disabled) {
    2551           1 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2552           1 :                 return -EALREADY;
    2553             :         }
    2554             : 
    2555           2 :         nvme_ctrlr->resetting = true;
    2556           2 :         nvme_ctrlr->dont_retry = true;
    2557             : 
    2558           2 :         if (nvme_ctrlr->reconnect_is_delayed) {
    2559           1 :                 msg_fn = _bdev_nvme_cancel_reconnect_and_disable_ctrlr;
    2560           1 :                 nvme_ctrlr->reconnect_is_delayed = false;
    2561             :         } else {
    2562           1 :                 msg_fn = _bdev_nvme_disconnect_and_disable_ctrlr;
    2563             :         }
    2564             : 
    2565           2 :         nvme_ctrlr->reset_start_tsc = spdk_get_ticks();
    2566             : 
    2567           2 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2568             : 
    2569           2 :         spdk_thread_send_msg(nvme_ctrlr->thread, msg_fn, nvme_ctrlr);
    2570           2 :         return 0;
    2571             : }
    2572             : 
    2573             : static int
    2574          16 : nvme_ctrlr_op(struct nvme_ctrlr *nvme_ctrlr, enum nvme_ctrlr_op op,
    2575             :               bdev_nvme_ctrlr_op_cb cb_fn, void *cb_arg)
    2576             : {
    2577             :         int rc;
    2578             : 
    2579          16 :         switch (op) {
    2580          15 :         case NVME_CTRLR_OP_RESET:
    2581          15 :                 rc = bdev_nvme_reset_ctrlr(nvme_ctrlr);
    2582          15 :                 break;
    2583           0 :         case NVME_CTRLR_OP_ENABLE:
    2584           0 :                 rc = bdev_nvme_enable_ctrlr(nvme_ctrlr);
    2585           0 :                 break;
    2586           0 :         case NVME_CTRLR_OP_DISABLE:
    2587           0 :                 rc = bdev_nvme_disable_ctrlr(nvme_ctrlr);
    2588           0 :                 break;
    2589           1 :         default:
    2590           1 :                 rc = -EINVAL;
    2591           1 :                 break;
    2592             :         }
    2593             : 
    2594          16 :         if (rc == 0) {
    2595           9 :                 assert(nvme_ctrlr->ctrlr_op_cb_fn == NULL);
    2596           9 :                 assert(nvme_ctrlr->ctrlr_op_cb_arg == NULL);
    2597           9 :                 nvme_ctrlr->ctrlr_op_cb_fn = cb_fn;
    2598           9 :                 nvme_ctrlr->ctrlr_op_cb_arg = cb_arg;
    2599             :         }
    2600          16 :         return rc;
    2601             : }
    2602             : 
    2603             : struct nvme_ctrlr_op_rpc_ctx {
    2604             :         struct nvme_ctrlr *nvme_ctrlr;
    2605             :         struct spdk_thread *orig_thread;
    2606             :         enum nvme_ctrlr_op op;
    2607             :         int rc;
    2608             :         bdev_nvme_ctrlr_op_cb cb_fn;
    2609             :         void *cb_arg;
    2610             : };
    2611             : 
    2612             : static void
    2613           4 : _nvme_ctrlr_op_rpc_complete(void *_ctx)
    2614             : {
    2615           4 :         struct nvme_ctrlr_op_rpc_ctx *ctx = _ctx;
    2616             : 
    2617           4 :         assert(ctx != NULL);
    2618           4 :         assert(ctx->cb_fn != NULL);
    2619             : 
    2620           4 :         ctx->cb_fn(ctx->cb_arg, ctx->rc);
    2621             : 
    2622           4 :         free(ctx);
    2623           4 : }
    2624             : 
    2625             : static void
    2626           4 : nvme_ctrlr_op_rpc_complete(void *cb_arg, int rc)
    2627             : {
    2628           4 :         struct nvme_ctrlr_op_rpc_ctx *ctx = cb_arg;
    2629             : 
    2630           4 :         ctx->rc = rc;
    2631             : 
    2632           4 :         spdk_thread_send_msg(ctx->orig_thread, _nvme_ctrlr_op_rpc_complete, ctx);
    2633           4 : }
    2634             : 
    2635             : void
    2636           4 : nvme_ctrlr_op_rpc(struct nvme_ctrlr *nvme_ctrlr, enum nvme_ctrlr_op op,
    2637             :                   bdev_nvme_ctrlr_op_cb cb_fn, void *cb_arg)
    2638             : {
    2639             :         struct nvme_ctrlr_op_rpc_ctx *ctx;
    2640             :         int rc;
    2641             : 
    2642           4 :         assert(cb_fn != NULL);
    2643             : 
    2644           4 :         ctx = calloc(1, sizeof(*ctx));
    2645           4 :         if (ctx == NULL) {
    2646           0 :                 SPDK_ERRLOG("Failed to allocate nvme_ctrlr_op_rpc_ctx.\n");
    2647           0 :                 cb_fn(cb_arg, -ENOMEM);
    2648           0 :                 return;
    2649             :         }
    2650             : 
    2651           4 :         ctx->orig_thread = spdk_get_thread();
    2652           4 :         ctx->cb_fn = cb_fn;
    2653           4 :         ctx->cb_arg = cb_arg;
    2654             : 
    2655           4 :         rc = nvme_ctrlr_op(nvme_ctrlr, op, nvme_ctrlr_op_rpc_complete, ctx);
    2656           4 :         if (rc == 0) {
    2657           1 :                 return;
    2658           3 :         } else if (rc == -EALREADY) {
    2659           0 :                 rc = 0;
    2660             :         }
    2661             : 
    2662           3 :         nvme_ctrlr_op_rpc_complete(ctx, rc);
    2663             : }
    2664             : 
    2665             : static void nvme_bdev_ctrlr_op_rpc_continue(void *cb_arg, int rc);
    2666             : 
    2667             : static void
    2668           2 : _nvme_bdev_ctrlr_op_rpc_continue(void *_ctx)
    2669             : {
    2670           2 :         struct nvme_ctrlr_op_rpc_ctx *ctx = _ctx;
    2671             :         struct nvme_ctrlr *prev_nvme_ctrlr, *next_nvme_ctrlr;
    2672             :         int rc;
    2673             : 
    2674           2 :         prev_nvme_ctrlr = ctx->nvme_ctrlr;
    2675           2 :         ctx->nvme_ctrlr = NULL;
    2676             : 
    2677           2 :         if (ctx->rc != 0) {
    2678           0 :                 goto complete;
    2679             :         }
    2680             : 
    2681           2 :         next_nvme_ctrlr = TAILQ_NEXT(prev_nvme_ctrlr, tailq);
    2682           2 :         if (next_nvme_ctrlr == NULL) {
    2683           1 :                 goto complete;
    2684             :         }
    2685             : 
    2686           1 :         rc = nvme_ctrlr_op(next_nvme_ctrlr, ctx->op, nvme_bdev_ctrlr_op_rpc_continue, ctx);
    2687           1 :         if (rc == 0) {
    2688           1 :                 ctx->nvme_ctrlr = next_nvme_ctrlr;
    2689           1 :                 return;
    2690           0 :         } else if (rc == -EALREADY) {
    2691           0 :                 ctx->nvme_ctrlr = next_nvme_ctrlr;
    2692           0 :                 rc = 0;
    2693             :         }
    2694             : 
    2695           0 :         ctx->rc = rc;
    2696             : 
    2697           1 : complete:
    2698           1 :         ctx->cb_fn(ctx->cb_arg, ctx->rc);
    2699           1 :         free(ctx);
    2700             : }
    2701             : 
    2702             : static void
    2703           2 : nvme_bdev_ctrlr_op_rpc_continue(void *cb_arg, int rc)
    2704             : {
    2705           2 :         struct nvme_ctrlr_op_rpc_ctx *ctx = cb_arg;
    2706             : 
    2707           2 :         ctx->rc = rc;
    2708             : 
    2709           2 :         spdk_thread_send_msg(ctx->orig_thread, _nvme_bdev_ctrlr_op_rpc_continue, ctx);
    2710           2 : }
    2711             : 
    2712             : void
    2713           1 : nvme_bdev_ctrlr_op_rpc(struct nvme_bdev_ctrlr *nbdev_ctrlr, enum nvme_ctrlr_op op,
    2714             :                        bdev_nvme_ctrlr_op_cb cb_fn, void *cb_arg)
    2715             : {
    2716             :         struct nvme_ctrlr_op_rpc_ctx *ctx;
    2717             :         struct nvme_ctrlr *nvme_ctrlr;
    2718             :         int rc;
    2719             : 
    2720           1 :         assert(cb_fn != NULL);
    2721             : 
    2722           1 :         ctx = calloc(1, sizeof(*ctx));
    2723           1 :         if (ctx == NULL) {
    2724           0 :                 SPDK_ERRLOG("Failed to allocate nvme_ctrlr_op_rpc_ctx.\n");
    2725           0 :                 cb_fn(cb_arg, -ENOMEM);
    2726           0 :                 return;
    2727             :         }
    2728             : 
    2729           1 :         ctx->orig_thread = spdk_get_thread();
    2730           1 :         ctx->op = op;
    2731           1 :         ctx->cb_fn = cb_fn;
    2732           1 :         ctx->cb_arg = cb_arg;
    2733             : 
    2734           1 :         nvme_ctrlr = TAILQ_FIRST(&nbdev_ctrlr->ctrlrs);
    2735           1 :         assert(nvme_ctrlr != NULL);
    2736             : 
    2737           1 :         rc = nvme_ctrlr_op(nvme_ctrlr, op, nvme_bdev_ctrlr_op_rpc_continue, ctx);
    2738           1 :         if (rc == 0) {
    2739           1 :                 ctx->nvme_ctrlr = nvme_ctrlr;
    2740           1 :                 return;
    2741           0 :         } else if (rc == -EALREADY) {
    2742           0 :                 ctx->nvme_ctrlr = nvme_ctrlr;
    2743           0 :                 rc = 0;
    2744             :         }
    2745             : 
    2746           0 :         nvme_bdev_ctrlr_op_rpc_continue(ctx, rc);
    2747             : }
    2748             : 
    2749             : static int _bdev_nvme_reset_io(struct nvme_io_path *io_path, struct nvme_bdev_io *bio);
    2750             : 
    2751             : static void
    2752           7 : _bdev_nvme_reset_io_complete(struct spdk_io_channel_iter *i, int status)
    2753             : {
    2754           7 :         struct nvme_bdev_io *bio = spdk_io_channel_iter_get_ctx(i);
    2755             :         enum spdk_bdev_io_status io_status;
    2756             : 
    2757           7 :         if (bio->cpl.cdw0 == 0) {
    2758           5 :                 io_status = SPDK_BDEV_IO_STATUS_SUCCESS;
    2759             :         } else {
    2760           2 :                 io_status = SPDK_BDEV_IO_STATUS_FAILED;
    2761             :         }
    2762             : 
    2763           7 :         __bdev_nvme_io_complete(spdk_bdev_io_from_ctx(bio), io_status, NULL);
    2764           7 : }
    2765             : 
    2766             : static void
    2767          14 : bdev_nvme_abort_bdev_channel(struct spdk_io_channel_iter *i)
    2768             : {
    2769          14 :         struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
    2770          14 :         struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(_ch);
    2771             : 
    2772          14 :         bdev_nvme_abort_retry_ios(nbdev_ch);
    2773             : 
    2774          14 :         spdk_for_each_channel_continue(i, 0);
    2775          14 : }
    2776             : 
    2777             : static void
    2778           7 : bdev_nvme_reset_io_complete(struct nvme_bdev_io *bio)
    2779             : {
    2780           7 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    2781           7 :         struct nvme_bdev *nbdev = (struct nvme_bdev *)bdev_io->bdev->ctxt;
    2782             : 
    2783             :         /* Abort all queued I/Os for retry. */
    2784           7 :         spdk_for_each_channel(nbdev,
    2785             :                               bdev_nvme_abort_bdev_channel,
    2786             :                               bio,
    2787             :                               _bdev_nvme_reset_io_complete);
    2788           7 : }
    2789             : 
    2790             : static void
    2791          10 : _bdev_nvme_reset_io_continue(void *ctx)
    2792             : {
    2793          10 :         struct nvme_bdev_io *bio = ctx;
    2794             :         struct nvme_io_path *prev_io_path, *next_io_path;
    2795             :         int rc;
    2796             : 
    2797          10 :         prev_io_path = bio->io_path;
    2798          10 :         bio->io_path = NULL;
    2799             : 
    2800          10 :         if (bio->cpl.cdw0 != 0) {
    2801           2 :                 goto complete;
    2802             :         }
    2803             : 
    2804           8 :         next_io_path = STAILQ_NEXT(prev_io_path, stailq);
    2805           8 :         if (next_io_path == NULL) {
    2806           5 :                 goto complete;
    2807             :         }
    2808             : 
    2809           3 :         rc = _bdev_nvme_reset_io(next_io_path, bio);
    2810           3 :         if (rc == 0) {
    2811           3 :                 return;
    2812             :         }
    2813             : 
    2814           0 :         bio->cpl.cdw0 = 1;
    2815             : 
    2816           7 : complete:
    2817           7 :         bdev_nvme_reset_io_complete(bio);
    2818             : }
    2819             : 
    2820             : static void
    2821          10 : bdev_nvme_reset_io_continue(void *cb_arg, int rc)
    2822             : {
    2823          10 :         struct nvme_bdev_io *bio = cb_arg;
    2824          10 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    2825             : 
    2826          10 :         bio->cpl.cdw0 = (rc == 0) ? 0 : 1;
    2827             : 
    2828          10 :         spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io), _bdev_nvme_reset_io_continue, bio);
    2829          10 : }
    2830             : 
    2831             : static int
    2832          10 : _bdev_nvme_reset_io(struct nvme_io_path *io_path, struct nvme_bdev_io *bio)
    2833             : {
    2834             :         struct nvme_ctrlr_channel *ctrlr_ch;
    2835             :         struct spdk_bdev_io *bdev_io;
    2836             :         int rc;
    2837             : 
    2838          10 :         rc = nvme_ctrlr_op(io_path->qpair->ctrlr, NVME_CTRLR_OP_RESET,
    2839             :                            bdev_nvme_reset_io_continue, bio);
    2840          10 :         if (rc != 0 && rc != -EBUSY) {
    2841           0 :                 return rc;
    2842             :         }
    2843             : 
    2844          10 :         assert(bio->io_path == NULL);
    2845          10 :         bio->io_path = io_path;
    2846             : 
    2847          10 :         if (rc == -EBUSY) {
    2848           4 :                 ctrlr_ch = io_path->qpair->ctrlr_ch;
    2849           4 :                 assert(ctrlr_ch != NULL);
    2850             :                 /*
    2851             :                  * Reset call is queued only if it is from the app framework. This is on purpose so that
    2852             :                  * we don't interfere with the app framework reset strategy. i.e. we are deferring to the
    2853             :                  * upper level. If they are in the middle of a reset, we won't try to schedule another one.
    2854             :                  */
    2855           4 :                 bdev_io = spdk_bdev_io_from_ctx(bio);
    2856           4 :                 TAILQ_INSERT_TAIL(&ctrlr_ch->pending_resets, bdev_io, module_link);
    2857             :         }
    2858             : 
    2859          10 :         return 0;
    2860             : }
    2861             : 
    2862             : static void
    2863           7 : bdev_nvme_reset_io(struct nvme_bdev_channel *nbdev_ch, struct nvme_bdev_io *bio)
    2864             : {
    2865             :         struct nvme_io_path *io_path;
    2866             :         int rc;
    2867             : 
    2868           7 :         bio->cpl.cdw0 = 0;
    2869             : 
    2870             :         /* Reset all nvme_ctrlrs of a bdev controller sequentially. */
    2871           7 :         io_path = STAILQ_FIRST(&nbdev_ch->io_path_list);
    2872           7 :         assert(io_path != NULL);
    2873             : 
    2874           7 :         rc = _bdev_nvme_reset_io(io_path, bio);
    2875           7 :         if (rc != 0) {
    2876             :                 /* If the current nvme_ctrlr is disabled, skip it and move to the next nvme_ctrlr. */
    2877           0 :                 rc = (rc == -EALREADY) ? 0 : rc;
    2878             : 
    2879           0 :                 bdev_nvme_reset_io_continue(bio, rc);
    2880             :         }
    2881           7 : }
    2882             : 
    2883             : static int
    2884          18 : bdev_nvme_failover_ctrlr_unsafe(struct nvme_ctrlr *nvme_ctrlr, bool remove)
    2885             : {
    2886          18 :         if (nvme_ctrlr->destruct) {
    2887             :                 /* Don't bother resetting if the controller is in the process of being destructed. */
    2888           2 :                 return -ENXIO;
    2889             :         }
    2890             : 
    2891          16 :         if (nvme_ctrlr->resetting) {
    2892           3 :                 if (!nvme_ctrlr->in_failover) {
    2893           3 :                         SPDK_NOTICELOG("Reset is already in progress. Defer failover until reset completes.\n");
    2894             : 
    2895             :                         /* Defer failover until reset completes. */
    2896           3 :                         nvme_ctrlr->pending_failover = true;
    2897           3 :                         return -EINPROGRESS;
    2898             :                 } else {
    2899           0 :                         SPDK_NOTICELOG("Unable to perform failover, already in progress.\n");
    2900           0 :                         return -EBUSY;
    2901             :                 }
    2902             :         }
    2903             : 
    2904          13 :         bdev_nvme_failover_trid(nvme_ctrlr, remove, true);
    2905             : 
    2906          13 :         if (nvme_ctrlr->reconnect_is_delayed) {
    2907           1 :                 SPDK_NOTICELOG("Reconnect is already scheduled.\n");
    2908             : 
    2909             :                 /* We rely on the next reconnect for the failover. */
    2910           1 :                 return -EALREADY;
    2911             :         }
    2912             : 
    2913          12 :         if (nvme_ctrlr->disabled) {
    2914           0 :                 SPDK_NOTICELOG("Controller is disabled.\n");
    2915             : 
    2916             :                 /* We rely on the enablement for the failover. */
    2917           0 :                 return -EALREADY;
    2918             :         }
    2919             : 
    2920          12 :         nvme_ctrlr->resetting = true;
    2921          12 :         nvme_ctrlr->in_failover = true;
    2922             : 
    2923          12 :         assert(nvme_ctrlr->reset_start_tsc == 0);
    2924          12 :         nvme_ctrlr->reset_start_tsc = spdk_get_ticks();
    2925             : 
    2926          12 :         return 0;
    2927             : }
    2928             : 
    2929             : static int
    2930          16 : bdev_nvme_failover_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
    2931             : {
    2932             :         int rc;
    2933             : 
    2934          16 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2935          16 :         rc = bdev_nvme_failover_ctrlr_unsafe(nvme_ctrlr, false);
    2936          16 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2937             : 
    2938          16 :         if (rc == 0) {
    2939          11 :                 spdk_thread_send_msg(nvme_ctrlr->thread, _bdev_nvme_reset_ctrlr, nvme_ctrlr);
    2940           5 :         } else if (rc == -EALREADY) {
    2941           0 :                 rc = 0;
    2942             :         }
    2943             : 
    2944          16 :         return rc;
    2945             : }
    2946             : 
    2947             : static int bdev_nvme_unmap(struct nvme_bdev_io *bio, uint64_t offset_blocks,
    2948             :                            uint64_t num_blocks);
    2949             : 
    2950             : static int bdev_nvme_write_zeroes(struct nvme_bdev_io *bio, uint64_t offset_blocks,
    2951             :                                   uint64_t num_blocks);
    2952             : 
    2953             : static int bdev_nvme_copy(struct nvme_bdev_io *bio, uint64_t dst_offset_blocks,
    2954             :                           uint64_t src_offset_blocks,
    2955             :                           uint64_t num_blocks);
    2956             : 
    2957             : static void
    2958           1 : bdev_nvme_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
    2959             :                      bool success)
    2960             : {
    2961           1 :         struct nvme_bdev_io *bio = (struct nvme_bdev_io *)bdev_io->driver_ctx;
    2962             :         int ret;
    2963             : 
    2964           1 :         if (!success) {
    2965           0 :                 ret = -EINVAL;
    2966           0 :                 goto exit;
    2967             :         }
    2968             : 
    2969           1 :         if (spdk_unlikely(!nvme_io_path_is_available(bio->io_path))) {
    2970           0 :                 ret = -ENXIO;
    2971           0 :                 goto exit;
    2972             :         }
    2973             : 
    2974           1 :         ret = bdev_nvme_readv(bio,
    2975             :                               bdev_io->u.bdev.iovs,
    2976             :                               bdev_io->u.bdev.iovcnt,
    2977             :                               bdev_io->u.bdev.md_buf,
    2978             :                               bdev_io->u.bdev.num_blocks,
    2979             :                               bdev_io->u.bdev.offset_blocks,
    2980             :                               bdev_io->u.bdev.dif_check_flags,
    2981             :                               bdev_io->u.bdev.memory_domain,
    2982             :                               bdev_io->u.bdev.memory_domain_ctx,
    2983             :                               bdev_io->u.bdev.accel_sequence);
    2984             : 
    2985           1 : exit:
    2986           1 :         if (spdk_unlikely(ret != 0)) {
    2987           0 :                 bdev_nvme_io_complete(bio, ret);
    2988             :         }
    2989           1 : }
    2990             : 
    2991             : static inline void
    2992          51 : _bdev_nvme_submit_request(struct nvme_bdev_channel *nbdev_ch, struct spdk_bdev_io *bdev_io)
    2993             : {
    2994          51 :         struct nvme_bdev_io *nbdev_io = (struct nvme_bdev_io *)bdev_io->driver_ctx;
    2995          51 :         struct spdk_bdev *bdev = bdev_io->bdev;
    2996             :         struct nvme_bdev_io *nbdev_io_to_abort;
    2997          51 :         int rc = 0;
    2998             : 
    2999          51 :         switch (bdev_io->type) {
    3000           3 :         case SPDK_BDEV_IO_TYPE_READ:
    3001           3 :                 if (bdev_io->u.bdev.iovs && bdev_io->u.bdev.iovs[0].iov_base) {
    3002             : 
    3003           2 :                         rc = bdev_nvme_readv(nbdev_io,
    3004             :                                              bdev_io->u.bdev.iovs,
    3005             :                                              bdev_io->u.bdev.iovcnt,
    3006             :                                              bdev_io->u.bdev.md_buf,
    3007             :                                              bdev_io->u.bdev.num_blocks,
    3008             :                                              bdev_io->u.bdev.offset_blocks,
    3009             :                                              bdev_io->u.bdev.dif_check_flags,
    3010             :                                              bdev_io->u.bdev.memory_domain,
    3011             :                                              bdev_io->u.bdev.memory_domain_ctx,
    3012             :                                              bdev_io->u.bdev.accel_sequence);
    3013             :                 } else {
    3014           1 :                         spdk_bdev_io_get_buf(bdev_io, bdev_nvme_get_buf_cb,
    3015           1 :                                              bdev_io->u.bdev.num_blocks * bdev->blocklen);
    3016           1 :                         rc = 0;
    3017             :                 }
    3018           3 :                 break;
    3019          25 :         case SPDK_BDEV_IO_TYPE_WRITE:
    3020          25 :                 rc = bdev_nvme_writev(nbdev_io,
    3021             :                                       bdev_io->u.bdev.iovs,
    3022             :                                       bdev_io->u.bdev.iovcnt,
    3023             :                                       bdev_io->u.bdev.md_buf,
    3024             :                                       bdev_io->u.bdev.num_blocks,
    3025             :                                       bdev_io->u.bdev.offset_blocks,
    3026             :                                       bdev_io->u.bdev.dif_check_flags,
    3027             :                                       bdev_io->u.bdev.memory_domain,
    3028             :                                       bdev_io->u.bdev.memory_domain_ctx,
    3029             :                                       bdev_io->u.bdev.accel_sequence,
    3030             :                                       bdev_io->u.bdev.nvme_cdw12,
    3031             :                                       bdev_io->u.bdev.nvme_cdw13);
    3032          25 :                 break;
    3033           1 :         case SPDK_BDEV_IO_TYPE_COMPARE:
    3034           1 :                 rc = bdev_nvme_comparev(nbdev_io,
    3035             :                                         bdev_io->u.bdev.iovs,
    3036             :                                         bdev_io->u.bdev.iovcnt,
    3037             :                                         bdev_io->u.bdev.md_buf,
    3038             :                                         bdev_io->u.bdev.num_blocks,
    3039             :                                         bdev_io->u.bdev.offset_blocks,
    3040             :                                         bdev_io->u.bdev.dif_check_flags);
    3041           1 :                 break;
    3042           2 :         case SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE:
    3043           2 :                 rc = bdev_nvme_comparev_and_writev(nbdev_io,
    3044             :                                                    bdev_io->u.bdev.iovs,
    3045             :                                                    bdev_io->u.bdev.iovcnt,
    3046             :                                                    bdev_io->u.bdev.fused_iovs,
    3047             :                                                    bdev_io->u.bdev.fused_iovcnt,
    3048             :                                                    bdev_io->u.bdev.md_buf,
    3049             :                                                    bdev_io->u.bdev.num_blocks,
    3050             :                                                    bdev_io->u.bdev.offset_blocks,
    3051             :                                                    bdev_io->u.bdev.dif_check_flags);
    3052           2 :                 break;
    3053           1 :         case SPDK_BDEV_IO_TYPE_UNMAP:
    3054           1 :                 rc = bdev_nvme_unmap(nbdev_io,
    3055             :                                      bdev_io->u.bdev.offset_blocks,
    3056             :                                      bdev_io->u.bdev.num_blocks);
    3057           1 :                 break;
    3058           0 :         case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
    3059           0 :                 rc =  bdev_nvme_write_zeroes(nbdev_io,
    3060             :                                              bdev_io->u.bdev.offset_blocks,
    3061             :                                              bdev_io->u.bdev.num_blocks);
    3062           0 :                 break;
    3063           7 :         case SPDK_BDEV_IO_TYPE_RESET:
    3064           7 :                 nbdev_io->io_path = NULL;
    3065           7 :                 bdev_nvme_reset_io(nbdev_ch, nbdev_io);
    3066           7 :                 return;
    3067             : 
    3068           1 :         case SPDK_BDEV_IO_TYPE_FLUSH:
    3069           1 :                 bdev_nvme_io_complete(nbdev_io, 0);
    3070           1 :                 return;
    3071             : 
    3072           0 :         case SPDK_BDEV_IO_TYPE_ZONE_APPEND:
    3073           0 :                 rc = bdev_nvme_zone_appendv(nbdev_io,
    3074             :                                             bdev_io->u.bdev.iovs,
    3075             :                                             bdev_io->u.bdev.iovcnt,
    3076             :                                             bdev_io->u.bdev.md_buf,
    3077             :                                             bdev_io->u.bdev.num_blocks,
    3078             :                                             bdev_io->u.bdev.offset_blocks,
    3079             :                                             bdev_io->u.bdev.dif_check_flags);
    3080           0 :                 break;
    3081           0 :         case SPDK_BDEV_IO_TYPE_GET_ZONE_INFO:
    3082           0 :                 rc = bdev_nvme_get_zone_info(nbdev_io,
    3083             :                                              bdev_io->u.zone_mgmt.zone_id,
    3084             :                                              bdev_io->u.zone_mgmt.num_zones,
    3085           0 :                                              bdev_io->u.zone_mgmt.buf);
    3086           0 :                 break;
    3087           0 :         case SPDK_BDEV_IO_TYPE_ZONE_MANAGEMENT:
    3088           0 :                 rc = bdev_nvme_zone_management(nbdev_io,
    3089             :                                                bdev_io->u.zone_mgmt.zone_id,
    3090             :                                                bdev_io->u.zone_mgmt.zone_action);
    3091           0 :                 break;
    3092           5 :         case SPDK_BDEV_IO_TYPE_NVME_ADMIN:
    3093           5 :                 nbdev_io->io_path = NULL;
    3094           5 :                 bdev_nvme_admin_passthru(nbdev_ch,
    3095             :                                          nbdev_io,
    3096             :                                          &bdev_io->u.nvme_passthru.cmd,
    3097             :                                          bdev_io->u.nvme_passthru.buf,
    3098             :                                          bdev_io->u.nvme_passthru.nbytes);
    3099           5 :                 return;
    3100             : 
    3101           0 :         case SPDK_BDEV_IO_TYPE_NVME_IO:
    3102           0 :                 rc = bdev_nvme_io_passthru(nbdev_io,
    3103             :                                            &bdev_io->u.nvme_passthru.cmd,
    3104             :                                            bdev_io->u.nvme_passthru.buf,
    3105             :                                            bdev_io->u.nvme_passthru.nbytes);
    3106           0 :                 break;
    3107           0 :         case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
    3108           0 :                 rc = bdev_nvme_io_passthru_md(nbdev_io,
    3109             :                                               &bdev_io->u.nvme_passthru.cmd,
    3110             :                                               bdev_io->u.nvme_passthru.buf,
    3111             :                                               bdev_io->u.nvme_passthru.nbytes,
    3112             :                                               bdev_io->u.nvme_passthru.md_buf,
    3113             :                                               bdev_io->u.nvme_passthru.md_len);
    3114           0 :                 break;
    3115           0 :         case SPDK_BDEV_IO_TYPE_NVME_IOV_MD:
    3116           0 :                 rc = bdev_nvme_iov_passthru_md(nbdev_io,
    3117             :                                                &bdev_io->u.nvme_passthru.cmd,
    3118             :                                                bdev_io->u.nvme_passthru.iovs,
    3119             :                                                bdev_io->u.nvme_passthru.iovcnt,
    3120             :                                                bdev_io->u.nvme_passthru.nbytes,
    3121             :                                                bdev_io->u.nvme_passthru.md_buf,
    3122             :                                                bdev_io->u.nvme_passthru.md_len);
    3123           0 :                 break;
    3124           6 :         case SPDK_BDEV_IO_TYPE_ABORT:
    3125           6 :                 nbdev_io->io_path = NULL;
    3126           6 :                 nbdev_io_to_abort = (struct nvme_bdev_io *)bdev_io->u.abort.bio_to_abort->driver_ctx;
    3127           6 :                 bdev_nvme_abort(nbdev_ch,
    3128             :                                 nbdev_io,
    3129             :                                 nbdev_io_to_abort);
    3130           6 :                 return;
    3131             : 
    3132           0 :         case SPDK_BDEV_IO_TYPE_COPY:
    3133           0 :                 rc = bdev_nvme_copy(nbdev_io,
    3134             :                                     bdev_io->u.bdev.offset_blocks,
    3135             :                                     bdev_io->u.bdev.copy.src_offset_blocks,
    3136             :                                     bdev_io->u.bdev.num_blocks);
    3137           0 :                 break;
    3138           0 :         default:
    3139           0 :                 rc = -EINVAL;
    3140           0 :                 break;
    3141             :         }
    3142             : 
    3143          32 :         if (spdk_unlikely(rc != 0)) {
    3144           0 :                 bdev_nvme_io_complete(nbdev_io, rc);
    3145             :         }
    3146             : }
    3147             : 
    3148             : static void
    3149          58 : bdev_nvme_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
    3150             : {
    3151          58 :         struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(ch);
    3152          58 :         struct nvme_bdev_io *nbdev_io = (struct nvme_bdev_io *)bdev_io->driver_ctx;
    3153             : 
    3154          58 :         if (spdk_likely(nbdev_io->submit_tsc == 0)) {
    3155          58 :                 nbdev_io->submit_tsc = spdk_bdev_io_get_submit_tsc(bdev_io);
    3156             :         } else {
    3157             :                 /* There are cases where submit_tsc != 0, i.e. retry I/O.
    3158             :                  * We need to update submit_tsc here.
    3159             :                  */
    3160           0 :                 nbdev_io->submit_tsc = spdk_get_ticks();
    3161             :         }
    3162             : 
    3163          58 :         spdk_trace_record(TRACE_BDEV_NVME_IO_START, 0, 0, (uintptr_t)nbdev_io, (uintptr_t)bdev_io);
    3164          58 :         nbdev_io->io_path = bdev_nvme_find_io_path(nbdev_ch);
    3165          58 :         if (spdk_unlikely(!nbdev_io->io_path)) {
    3166          11 :                 if (!bdev_nvme_io_type_is_admin(bdev_io->type)) {
    3167          10 :                         bdev_nvme_io_complete(nbdev_io, -ENXIO);
    3168          10 :                         return;
    3169             :                 }
    3170             : 
    3171             :                 /* Admin commands do not use the optimal I/O path.
    3172             :                  * Simply fall through even if it is not found.
    3173             :                  */
    3174             :         }
    3175             : 
    3176          48 :         _bdev_nvme_submit_request(nbdev_ch, bdev_io);
    3177             : }
    3178             : 
    3179             : static bool
    3180           0 : bdev_nvme_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
    3181             : {
    3182           0 :         struct nvme_bdev *nbdev = ctx;
    3183             :         struct nvme_ns *nvme_ns;
    3184             :         struct spdk_nvme_ns *ns;
    3185             :         struct spdk_nvme_ctrlr *ctrlr;
    3186             :         const struct spdk_nvme_ctrlr_data *cdata;
    3187             : 
    3188           0 :         nvme_ns = TAILQ_FIRST(&nbdev->nvme_ns_list);
    3189           0 :         assert(nvme_ns != NULL);
    3190           0 :         ns = nvme_ns->ns;
    3191           0 :         if (ns == NULL) {
    3192           0 :                 return false;
    3193             :         }
    3194             : 
    3195           0 :         ctrlr = spdk_nvme_ns_get_ctrlr(ns);
    3196             : 
    3197           0 :         switch (io_type) {
    3198           0 :         case SPDK_BDEV_IO_TYPE_READ:
    3199             :         case SPDK_BDEV_IO_TYPE_WRITE:
    3200             :         case SPDK_BDEV_IO_TYPE_RESET:
    3201             :         case SPDK_BDEV_IO_TYPE_FLUSH:
    3202             :         case SPDK_BDEV_IO_TYPE_NVME_ADMIN:
    3203             :         case SPDK_BDEV_IO_TYPE_NVME_IO:
    3204             :         case SPDK_BDEV_IO_TYPE_ABORT:
    3205           0 :                 return true;
    3206             : 
    3207           0 :         case SPDK_BDEV_IO_TYPE_COMPARE:
    3208           0 :                 return spdk_nvme_ns_supports_compare(ns);
    3209             : 
    3210           0 :         case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
    3211           0 :                 return spdk_nvme_ns_get_md_size(ns) ? true : false;
    3212             : 
    3213           0 :         case SPDK_BDEV_IO_TYPE_UNMAP:
    3214           0 :                 cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    3215           0 :                 return cdata->oncs.dsm;
    3216             : 
    3217           0 :         case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
    3218           0 :                 cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    3219           0 :                 return cdata->oncs.write_zeroes;
    3220             : 
    3221           0 :         case SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE:
    3222           0 :                 if (spdk_nvme_ctrlr_get_flags(ctrlr) &
    3223             :                     SPDK_NVME_CTRLR_COMPARE_AND_WRITE_SUPPORTED) {
    3224           0 :                         return true;
    3225             :                 }
    3226           0 :                 return false;
    3227             : 
    3228           0 :         case SPDK_BDEV_IO_TYPE_GET_ZONE_INFO:
    3229             :         case SPDK_BDEV_IO_TYPE_ZONE_MANAGEMENT:
    3230           0 :                 return spdk_nvme_ns_get_csi(ns) == SPDK_NVME_CSI_ZNS;
    3231             : 
    3232           0 :         case SPDK_BDEV_IO_TYPE_ZONE_APPEND:
    3233           0 :                 return spdk_nvme_ns_get_csi(ns) == SPDK_NVME_CSI_ZNS &&
    3234           0 :                        spdk_nvme_ctrlr_get_flags(ctrlr) & SPDK_NVME_CTRLR_ZONE_APPEND_SUPPORTED;
    3235             : 
    3236           0 :         case SPDK_BDEV_IO_TYPE_COPY:
    3237           0 :                 cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    3238           0 :                 return cdata->oncs.copy;
    3239             : 
    3240           0 :         default:
    3241           0 :                 return false;
    3242             :         }
    3243             : }
    3244             : 
    3245             : static int
    3246          57 : nvme_qpair_create(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ctrlr_channel *ctrlr_ch)
    3247             : {
    3248             :         struct nvme_qpair *nvme_qpair;
    3249             :         struct spdk_io_channel *pg_ch;
    3250             :         int rc;
    3251             : 
    3252          57 :         nvme_qpair = calloc(1, sizeof(*nvme_qpair));
    3253          57 :         if (!nvme_qpair) {
    3254           0 :                 SPDK_ERRLOG("Failed to alloc nvme_qpair.\n");
    3255           0 :                 return -1;
    3256             :         }
    3257             : 
    3258          57 :         TAILQ_INIT(&nvme_qpair->io_path_list);
    3259             : 
    3260          57 :         nvme_qpair->ctrlr = nvme_ctrlr;
    3261          57 :         nvme_qpair->ctrlr_ch = ctrlr_ch;
    3262             : 
    3263          57 :         pg_ch = spdk_get_io_channel(&g_nvme_bdev_ctrlrs);
    3264          57 :         if (!pg_ch) {
    3265           0 :                 free(nvme_qpair);
    3266           0 :                 return -1;
    3267             :         }
    3268             : 
    3269          57 :         nvme_qpair->group = spdk_io_channel_get_ctx(pg_ch);
    3270             : 
    3271             : #ifdef SPDK_CONFIG_VTUNE
    3272             :         nvme_qpair->group->collect_spin_stat = true;
    3273             : #else
    3274          57 :         nvme_qpair->group->collect_spin_stat = false;
    3275             : #endif
    3276             : 
    3277          57 :         if (!nvme_ctrlr->disabled) {
    3278             :                 /* If a nvme_ctrlr is disabled, don't try to create qpair for it. Qpair will
    3279             :                  * be created when it's enabled.
    3280             :                  */
    3281          57 :                 rc = bdev_nvme_create_qpair(nvme_qpair);
    3282          57 :                 if (rc != 0) {
    3283             :                         /* nvme_ctrlr can't create IO qpair if connection is down.
    3284             :                          * If reconnect_delay_sec is non-zero, creating IO qpair is retried
    3285             :                          * after reconnect_delay_sec seconds. If bdev_retry_count is non-zero,
    3286             :                          * submitted IO will be queued until IO qpair is successfully created.
    3287             :                          *
    3288             :                          * Hence, if both are satisfied, ignore the failure.
    3289             :                          */
    3290           0 :                         if (nvme_ctrlr->opts.reconnect_delay_sec == 0 || g_opts.bdev_retry_count == 0) {
    3291           0 :                                 spdk_put_io_channel(pg_ch);
    3292           0 :                                 free(nvme_qpair);
    3293           0 :                                 return rc;
    3294             :                         }
    3295             :                 }
    3296             :         }
    3297             : 
    3298          57 :         TAILQ_INSERT_TAIL(&nvme_qpair->group->qpair_list, nvme_qpair, tailq);
    3299             : 
    3300          57 :         ctrlr_ch->qpair = nvme_qpair;
    3301             : 
    3302          57 :         pthread_mutex_lock(&nvme_qpair->ctrlr->mutex);
    3303          57 :         nvme_qpair->ctrlr->ref++;
    3304          57 :         pthread_mutex_unlock(&nvme_qpair->ctrlr->mutex);
    3305             : 
    3306          57 :         return 0;
    3307             : }
    3308             : 
    3309             : static int
    3310          57 : bdev_nvme_create_ctrlr_channel_cb(void *io_device, void *ctx_buf)
    3311             : {
    3312          57 :         struct nvme_ctrlr *nvme_ctrlr = io_device;
    3313          57 :         struct nvme_ctrlr_channel *ctrlr_ch = ctx_buf;
    3314             : 
    3315          57 :         TAILQ_INIT(&ctrlr_ch->pending_resets);
    3316             : 
    3317          57 :         return nvme_qpair_create(nvme_ctrlr, ctrlr_ch);
    3318             : }
    3319             : 
    3320             : static void
    3321          57 : nvme_qpair_delete(struct nvme_qpair *nvme_qpair)
    3322             : {
    3323             :         struct nvme_io_path *io_path, *next;
    3324             : 
    3325          57 :         assert(nvme_qpair->group != NULL);
    3326             : 
    3327          92 :         TAILQ_FOREACH_SAFE(io_path, &nvme_qpair->io_path_list, tailq, next) {
    3328          35 :                 TAILQ_REMOVE(&nvme_qpair->io_path_list, io_path, tailq);
    3329          35 :                 nvme_io_path_free(io_path);
    3330             :         }
    3331             : 
    3332          57 :         TAILQ_REMOVE(&nvme_qpair->group->qpair_list, nvme_qpair, tailq);
    3333             : 
    3334          57 :         spdk_put_io_channel(spdk_io_channel_from_ctx(nvme_qpair->group));
    3335             : 
    3336          57 :         nvme_ctrlr_release(nvme_qpair->ctrlr);
    3337             : 
    3338          57 :         free(nvme_qpair);
    3339          57 : }
    3340             : 
    3341             : static void
    3342          57 : bdev_nvme_destroy_ctrlr_channel_cb(void *io_device, void *ctx_buf)
    3343             : {
    3344          57 :         struct nvme_ctrlr_channel *ctrlr_ch = ctx_buf;
    3345             :         struct nvme_qpair *nvme_qpair;
    3346             : 
    3347          57 :         nvme_qpair = ctrlr_ch->qpair;
    3348          57 :         assert(nvme_qpair != NULL);
    3349             : 
    3350          57 :         _bdev_nvme_clear_io_path_cache(nvme_qpair);
    3351             : 
    3352          57 :         if (nvme_qpair->qpair != NULL) {
    3353          43 :                 if (ctrlr_ch->reset_iter == NULL) {
    3354          43 :                         spdk_nvme_ctrlr_disconnect_io_qpair(nvme_qpair->qpair);
    3355             :                 } else {
    3356             :                         /* Skip current ctrlr_channel in a full reset sequence because
    3357             :                          * it is being deleted now. The qpair is already being disconnected.
    3358             :                          * We do not have to restart disconnecting it.
    3359             :                          */
    3360           0 :                         spdk_for_each_channel_continue(ctrlr_ch->reset_iter, 0);
    3361             :                 }
    3362             : 
    3363             :                 /* We cannot release a reference to the poll group now.
    3364             :                  * The qpair may be disconnected asynchronously later.
    3365             :                  * We need to poll it until it is actually disconnected.
    3366             :                  * Just detach the qpair from the deleting ctrlr_channel.
    3367             :                  */
    3368          43 :                 nvme_qpair->ctrlr_ch = NULL;
    3369             :         } else {
    3370          14 :                 assert(ctrlr_ch->reset_iter == NULL);
    3371             : 
    3372          14 :                 nvme_qpair_delete(nvme_qpair);
    3373             :         }
    3374          57 : }
    3375             : 
    3376             : static inline struct spdk_io_channel *
    3377           0 : bdev_nvme_get_accel_channel(struct nvme_poll_group *group)
    3378             : {
    3379           0 :         if (spdk_unlikely(!group->accel_channel)) {
    3380           0 :                 group->accel_channel = spdk_accel_get_io_channel();
    3381           0 :                 if (!group->accel_channel) {
    3382           0 :                         SPDK_ERRLOG("Cannot get the accel_channel for bdev nvme polling group=%p\n",
    3383             :                                     group);
    3384           0 :                         return NULL;
    3385             :                 }
    3386             :         }
    3387             : 
    3388           0 :         return group->accel_channel;
    3389             : }
    3390             : 
    3391             : static void
    3392           0 : bdev_nvme_submit_accel_crc32c(void *ctx, uint32_t *dst, struct iovec *iov,
    3393             :                               uint32_t iov_cnt, uint32_t seed,
    3394             :                               spdk_nvme_accel_completion_cb cb_fn, void *cb_arg)
    3395             : {
    3396             :         struct spdk_io_channel *accel_ch;
    3397           0 :         struct nvme_poll_group *group = ctx;
    3398             :         int rc;
    3399             : 
    3400           0 :         assert(cb_fn != NULL);
    3401             : 
    3402           0 :         accel_ch = bdev_nvme_get_accel_channel(group);
    3403           0 :         if (spdk_unlikely(accel_ch == NULL)) {
    3404           0 :                 cb_fn(cb_arg, -ENOMEM);
    3405           0 :                 return;
    3406             :         }
    3407             : 
    3408           0 :         rc = spdk_accel_submit_crc32cv(accel_ch, dst, iov, iov_cnt, seed, cb_fn, cb_arg);
    3409           0 :         if (rc) {
    3410             :                 /* For the two cases, spdk_accel_submit_crc32cv does not call the user's cb_fn */
    3411           0 :                 if (rc == -ENOMEM || rc == -EINVAL) {
    3412           0 :                         cb_fn(cb_arg, rc);
    3413             :                 }
    3414           0 :                 SPDK_ERRLOG("Cannot complete the accelerated crc32c operation with iov=%p\n", iov);
    3415             :         }
    3416             : }
    3417             : 
    3418             : static void
    3419           0 : bdev_nvme_finish_sequence(void *seq, spdk_nvme_accel_completion_cb cb_fn, void *cb_arg)
    3420             : {
    3421           0 :         spdk_accel_sequence_finish(seq, cb_fn, cb_arg);
    3422           0 : }
    3423             : 
    3424             : static void
    3425           0 : bdev_nvme_abort_sequence(void *seq)
    3426             : {
    3427           0 :         spdk_accel_sequence_abort(seq);
    3428           0 : }
    3429             : 
    3430             : static void
    3431           0 : bdev_nvme_reverse_sequence(void *seq)
    3432             : {
    3433           0 :         spdk_accel_sequence_reverse(seq);
    3434           0 : }
    3435             : 
    3436             : static int
    3437           0 : bdev_nvme_append_crc32c(void *ctx, void **seq, uint32_t *dst, struct iovec *iovs, uint32_t iovcnt,
    3438             :                         struct spdk_memory_domain *domain, void *domain_ctx, uint32_t seed,
    3439             :                         spdk_nvme_accel_step_cb cb_fn, void *cb_arg)
    3440             : {
    3441             :         struct spdk_io_channel *ch;
    3442           0 :         struct nvme_poll_group *group = ctx;
    3443             : 
    3444           0 :         ch = bdev_nvme_get_accel_channel(group);
    3445           0 :         if (spdk_unlikely(ch == NULL)) {
    3446           0 :                 return -ENOMEM;
    3447             :         }
    3448             : 
    3449           0 :         return spdk_accel_append_crc32c((struct spdk_accel_sequence **)seq, ch, dst, iovs, iovcnt,
    3450             :                                         domain, domain_ctx, seed, cb_fn, cb_arg);
    3451             : }
    3452             : 
    3453             : static struct spdk_nvme_accel_fn_table g_bdev_nvme_accel_fn_table = {
    3454             :         .table_size             = sizeof(struct spdk_nvme_accel_fn_table),
    3455             :         .submit_accel_crc32c    = bdev_nvme_submit_accel_crc32c,
    3456             :         .append_crc32c          = bdev_nvme_append_crc32c,
    3457             :         .finish_sequence        = bdev_nvme_finish_sequence,
    3458             :         .reverse_sequence       = bdev_nvme_reverse_sequence,
    3459             :         .abort_sequence         = bdev_nvme_abort_sequence,
    3460             : };
    3461             : 
    3462             : static int
    3463          42 : bdev_nvme_create_poll_group_cb(void *io_device, void *ctx_buf)
    3464             : {
    3465          42 :         struct nvme_poll_group *group = ctx_buf;
    3466             : 
    3467          42 :         TAILQ_INIT(&group->qpair_list);
    3468             : 
    3469          42 :         group->group = spdk_nvme_poll_group_create(group, &g_bdev_nvme_accel_fn_table);
    3470          42 :         if (group->group == NULL) {
    3471           0 :                 return -1;
    3472             :         }
    3473             : 
    3474          42 :         group->poller = SPDK_POLLER_REGISTER(bdev_nvme_poll, group, g_opts.nvme_ioq_poll_period_us);
    3475             : 
    3476          42 :         if (group->poller == NULL) {
    3477           0 :                 spdk_nvme_poll_group_destroy(group->group);
    3478           0 :                 return -1;
    3479             :         }
    3480             : 
    3481          42 :         return 0;
    3482             : }
    3483             : 
    3484             : static void
    3485          42 : bdev_nvme_destroy_poll_group_cb(void *io_device, void *ctx_buf)
    3486             : {
    3487          42 :         struct nvme_poll_group *group = ctx_buf;
    3488             : 
    3489          42 :         assert(TAILQ_EMPTY(&group->qpair_list));
    3490             : 
    3491          42 :         if (group->accel_channel) {
    3492           0 :                 spdk_put_io_channel(group->accel_channel);
    3493             :         }
    3494             : 
    3495          42 :         spdk_poller_unregister(&group->poller);
    3496          42 :         if (spdk_nvme_poll_group_destroy(group->group)) {
    3497           0 :                 SPDK_ERRLOG("Unable to destroy a poll group for the NVMe bdev module.\n");
    3498           0 :                 assert(false);
    3499             :         }
    3500          42 : }
    3501             : 
    3502             : static struct spdk_io_channel *
    3503           0 : bdev_nvme_get_io_channel(void *ctx)
    3504             : {
    3505           0 :         struct nvme_bdev *nvme_bdev = ctx;
    3506             : 
    3507           0 :         return spdk_get_io_channel(nvme_bdev);
    3508             : }
    3509             : 
    3510             : static void *
    3511           0 : bdev_nvme_get_module_ctx(void *ctx)
    3512             : {
    3513           0 :         struct nvme_bdev *nvme_bdev = ctx;
    3514             :         struct nvme_ns *nvme_ns;
    3515             : 
    3516           0 :         if (!nvme_bdev || nvme_bdev->disk.module != &nvme_if) {
    3517           0 :                 return NULL;
    3518             :         }
    3519             : 
    3520           0 :         nvme_ns = TAILQ_FIRST(&nvme_bdev->nvme_ns_list);
    3521           0 :         if (!nvme_ns) {
    3522           0 :                 return NULL;
    3523             :         }
    3524             : 
    3525           0 :         return nvme_ns->ns;
    3526             : }
    3527             : 
    3528             : static const char *
    3529           0 : _nvme_ana_state_str(enum spdk_nvme_ana_state ana_state)
    3530             : {
    3531           0 :         switch (ana_state) {
    3532           0 :         case SPDK_NVME_ANA_OPTIMIZED_STATE:
    3533           0 :                 return "optimized";
    3534           0 :         case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
    3535           0 :                 return "non_optimized";
    3536           0 :         case SPDK_NVME_ANA_INACCESSIBLE_STATE:
    3537           0 :                 return "inaccessible";
    3538           0 :         case SPDK_NVME_ANA_PERSISTENT_LOSS_STATE:
    3539           0 :                 return "persistent_loss";
    3540           0 :         case SPDK_NVME_ANA_CHANGE_STATE:
    3541           0 :                 return "change";
    3542           0 :         default:
    3543           0 :                 return NULL;
    3544             :         }
    3545             : }
    3546             : 
    3547             : static int
    3548           8 : bdev_nvme_get_memory_domains(void *ctx, struct spdk_memory_domain **domains, int array_size)
    3549             : {
    3550           8 :         struct spdk_memory_domain **_domains = NULL;
    3551           8 :         struct nvme_bdev *nbdev = ctx;
    3552             :         struct nvme_ns *nvme_ns;
    3553           8 :         int i = 0, _array_size = array_size;
    3554           8 :         int rc = 0;
    3555             : 
    3556          22 :         TAILQ_FOREACH(nvme_ns, &nbdev->nvme_ns_list, tailq) {
    3557          14 :                 if (domains && array_size >= i) {
    3558          11 :                         _domains = &domains[i];
    3559             :                 } else {
    3560           3 :                         _domains = NULL;
    3561             :                 }
    3562          14 :                 rc = spdk_nvme_ctrlr_get_memory_domains(nvme_ns->ctrlr->ctrlr, _domains, _array_size);
    3563          14 :                 if (rc > 0) {
    3564          13 :                         i += rc;
    3565          13 :                         if (_array_size >= rc) {
    3566           9 :                                 _array_size -= rc;
    3567             :                         } else {
    3568           4 :                                 _array_size = 0;
    3569             :                         }
    3570           1 :                 } else if (rc < 0) {
    3571           0 :                         return rc;
    3572             :                 }
    3573             :         }
    3574             : 
    3575           8 :         return i;
    3576             : }
    3577             : 
    3578             : static const char *
    3579           0 : nvme_ctrlr_get_state_str(struct nvme_ctrlr *nvme_ctrlr)
    3580             : {
    3581           0 :         if (nvme_ctrlr->destruct) {
    3582           0 :                 return "deleting";
    3583           0 :         } else if (spdk_nvme_ctrlr_is_failed(nvme_ctrlr->ctrlr)) {
    3584           0 :                 return "failed";
    3585           0 :         } else if (nvme_ctrlr->resetting) {
    3586           0 :                 return "resetting";
    3587           0 :         } else if (nvme_ctrlr->reconnect_is_delayed > 0) {
    3588           0 :                 return "reconnect_is_delayed";
    3589           0 :         } else if (nvme_ctrlr->disabled) {
    3590           0 :                 return "disabled";
    3591             :         } else {
    3592           0 :                 return "enabled";
    3593             :         }
    3594             : }
    3595             : 
    3596             : void
    3597           0 : nvme_ctrlr_info_json(struct spdk_json_write_ctx *w, struct nvme_ctrlr *nvme_ctrlr)
    3598           0 : {
    3599             :         struct spdk_nvme_transport_id *trid;
    3600             :         const struct spdk_nvme_ctrlr_opts *opts;
    3601             :         const struct spdk_nvme_ctrlr_data *cdata;
    3602             :         struct nvme_path_id *path_id;
    3603             :         uint32_t numa_socket_id;
    3604             : 
    3605           0 :         spdk_json_write_object_begin(w);
    3606             : 
    3607           0 :         spdk_json_write_named_string(w, "state", nvme_ctrlr_get_state_str(nvme_ctrlr));
    3608             : 
    3609             : #ifdef SPDK_CONFIG_NVME_CUSE
    3610           0 :         size_t cuse_name_size = 128;
    3611           0 :         char cuse_name[cuse_name_size];
    3612             : 
    3613           0 :         int rc = spdk_nvme_cuse_get_ctrlr_name(nvme_ctrlr->ctrlr, cuse_name, &cuse_name_size);
    3614           0 :         if (rc == 0) {
    3615           0 :                 spdk_json_write_named_string(w, "cuse_device", cuse_name);
    3616             :         }
    3617             : #endif
    3618           0 :         trid = &nvme_ctrlr->active_path_id->trid;
    3619           0 :         spdk_json_write_named_object_begin(w, "trid");
    3620           0 :         nvme_bdev_dump_trid_json(trid, w);
    3621           0 :         spdk_json_write_object_end(w);
    3622             : 
    3623           0 :         path_id = TAILQ_NEXT(nvme_ctrlr->active_path_id, link);
    3624           0 :         if (path_id != NULL) {
    3625           0 :                 spdk_json_write_named_array_begin(w, "alternate_trids");
    3626             :                 do {
    3627           0 :                         trid = &path_id->trid;
    3628           0 :                         spdk_json_write_object_begin(w);
    3629           0 :                         nvme_bdev_dump_trid_json(trid, w);
    3630           0 :                         spdk_json_write_object_end(w);
    3631             : 
    3632           0 :                         path_id = TAILQ_NEXT(path_id, link);
    3633           0 :                 } while (path_id != NULL);
    3634           0 :                 spdk_json_write_array_end(w);
    3635             :         }
    3636             : 
    3637           0 :         cdata = spdk_nvme_ctrlr_get_data(nvme_ctrlr->ctrlr);
    3638           0 :         spdk_json_write_named_uint16(w, "cntlid", cdata->cntlid);
    3639             : 
    3640           0 :         opts = spdk_nvme_ctrlr_get_opts(nvme_ctrlr->ctrlr);
    3641           0 :         spdk_json_write_named_object_begin(w, "host");
    3642           0 :         spdk_json_write_named_string(w, "nqn", opts->hostnqn);
    3643           0 :         spdk_json_write_named_string(w, "addr", opts->src_addr);
    3644           0 :         spdk_json_write_named_string(w, "svcid", opts->src_svcid);
    3645           0 :         spdk_json_write_object_end(w);
    3646             : 
    3647           0 :         numa_socket_id = spdk_nvme_ctrlr_get_socket_id(nvme_ctrlr->ctrlr);
    3648           0 :         if (numa_socket_id != (uint32_t)SPDK_ENV_SOCKET_ID_ANY) {
    3649           0 :                 spdk_json_write_named_uint32(w, "numa_socket_id", numa_socket_id);
    3650             :         }
    3651           0 :         spdk_json_write_object_end(w);
    3652           0 : }
    3653             : 
    3654             : static void
    3655           0 : nvme_namespace_info_json(struct spdk_json_write_ctx *w,
    3656             :                          struct nvme_ns *nvme_ns)
    3657           0 : {
    3658             :         struct spdk_nvme_ns *ns;
    3659             :         struct spdk_nvme_ctrlr *ctrlr;
    3660             :         const struct spdk_nvme_ctrlr_data *cdata;
    3661             :         const struct spdk_nvme_transport_id *trid;
    3662             :         union spdk_nvme_vs_register vs;
    3663             :         const struct spdk_nvme_ns_data *nsdata;
    3664           0 :         char buf[128];
    3665             : 
    3666           0 :         ns = nvme_ns->ns;
    3667           0 :         if (ns == NULL) {
    3668           0 :                 return;
    3669             :         }
    3670             : 
    3671           0 :         ctrlr = spdk_nvme_ns_get_ctrlr(ns);
    3672             : 
    3673           0 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    3674           0 :         trid = spdk_nvme_ctrlr_get_transport_id(ctrlr);
    3675           0 :         vs = spdk_nvme_ctrlr_get_regs_vs(ctrlr);
    3676             : 
    3677           0 :         spdk_json_write_object_begin(w);
    3678             : 
    3679           0 :         if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
    3680           0 :                 spdk_json_write_named_string(w, "pci_address", trid->traddr);
    3681             :         }
    3682             : 
    3683           0 :         spdk_json_write_named_object_begin(w, "trid");
    3684             : 
    3685           0 :         nvme_bdev_dump_trid_json(trid, w);
    3686             : 
    3687           0 :         spdk_json_write_object_end(w);
    3688             : 
    3689             : #ifdef SPDK_CONFIG_NVME_CUSE
    3690           0 :         size_t cuse_name_size = 128;
    3691           0 :         char cuse_name[cuse_name_size];
    3692             : 
    3693           0 :         int rc = spdk_nvme_cuse_get_ns_name(ctrlr, spdk_nvme_ns_get_id(ns),
    3694             :                                             cuse_name, &cuse_name_size);
    3695           0 :         if (rc == 0) {
    3696           0 :                 spdk_json_write_named_string(w, "cuse_device", cuse_name);
    3697             :         }
    3698             : #endif
    3699             : 
    3700           0 :         spdk_json_write_named_object_begin(w, "ctrlr_data");
    3701             : 
    3702           0 :         spdk_json_write_named_uint16(w, "cntlid", cdata->cntlid);
    3703             : 
    3704           0 :         spdk_json_write_named_string_fmt(w, "vendor_id", "0x%04x", cdata->vid);
    3705             : 
    3706           0 :         snprintf(buf, sizeof(cdata->mn) + 1, "%s", cdata->mn);
    3707           0 :         spdk_str_trim(buf);
    3708           0 :         spdk_json_write_named_string(w, "model_number", buf);
    3709             : 
    3710           0 :         snprintf(buf, sizeof(cdata->sn) + 1, "%s", cdata->sn);
    3711           0 :         spdk_str_trim(buf);
    3712           0 :         spdk_json_write_named_string(w, "serial_number", buf);
    3713             : 
    3714           0 :         snprintf(buf, sizeof(cdata->fr) + 1, "%s", cdata->fr);
    3715           0 :         spdk_str_trim(buf);
    3716           0 :         spdk_json_write_named_string(w, "firmware_revision", buf);
    3717             : 
    3718           0 :         if (cdata->subnqn[0] != '\0') {
    3719           0 :                 spdk_json_write_named_string(w, "subnqn", cdata->subnqn);
    3720             :         }
    3721             : 
    3722           0 :         spdk_json_write_named_object_begin(w, "oacs");
    3723             : 
    3724           0 :         spdk_json_write_named_uint32(w, "security", cdata->oacs.security);
    3725           0 :         spdk_json_write_named_uint32(w, "format", cdata->oacs.format);
    3726           0 :         spdk_json_write_named_uint32(w, "firmware", cdata->oacs.firmware);
    3727           0 :         spdk_json_write_named_uint32(w, "ns_manage", cdata->oacs.ns_manage);
    3728             : 
    3729           0 :         spdk_json_write_object_end(w);
    3730             : 
    3731           0 :         spdk_json_write_named_bool(w, "multi_ctrlr", cdata->cmic.multi_ctrlr);
    3732           0 :         spdk_json_write_named_bool(w, "ana_reporting", cdata->cmic.ana_reporting);
    3733             : 
    3734           0 :         spdk_json_write_object_end(w);
    3735             : 
    3736           0 :         spdk_json_write_named_object_begin(w, "vs");
    3737             : 
    3738           0 :         spdk_json_write_name(w, "nvme_version");
    3739           0 :         if (vs.bits.ter) {
    3740           0 :                 spdk_json_write_string_fmt(w, "%u.%u.%u", vs.bits.mjr, vs.bits.mnr, vs.bits.ter);
    3741             :         } else {
    3742           0 :                 spdk_json_write_string_fmt(w, "%u.%u", vs.bits.mjr, vs.bits.mnr);
    3743             :         }
    3744             : 
    3745           0 :         spdk_json_write_object_end(w);
    3746             : 
    3747           0 :         nsdata = spdk_nvme_ns_get_data(ns);
    3748             : 
    3749           0 :         spdk_json_write_named_object_begin(w, "ns_data");
    3750             : 
    3751           0 :         spdk_json_write_named_uint32(w, "id", spdk_nvme_ns_get_id(ns));
    3752             : 
    3753           0 :         if (cdata->cmic.ana_reporting) {
    3754           0 :                 spdk_json_write_named_string(w, "ana_state",
    3755             :                                              _nvme_ana_state_str(nvme_ns->ana_state));
    3756             :         }
    3757             : 
    3758           0 :         spdk_json_write_named_bool(w, "can_share", nsdata->nmic.can_share);
    3759             : 
    3760           0 :         spdk_json_write_object_end(w);
    3761             : 
    3762           0 :         if (cdata->oacs.security) {
    3763           0 :                 spdk_json_write_named_object_begin(w, "security");
    3764             : 
    3765           0 :                 spdk_json_write_named_bool(w, "opal", nvme_ns->bdev->opal);
    3766             : 
    3767           0 :                 spdk_json_write_object_end(w);
    3768             :         }
    3769             : 
    3770           0 :         spdk_json_write_object_end(w);
    3771             : }
    3772             : 
    3773             : static const char *
    3774           0 : nvme_bdev_get_mp_policy_str(struct nvme_bdev *nbdev)
    3775             : {
    3776           0 :         switch (nbdev->mp_policy) {
    3777           0 :         case BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE:
    3778           0 :                 return "active_passive";
    3779           0 :         case BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE:
    3780           0 :                 return "active_active";
    3781           0 :         default:
    3782           0 :                 assert(false);
    3783             :                 return "invalid";
    3784             :         }
    3785             : }
    3786             : 
    3787             : static const char *
    3788           0 : nvme_bdev_get_mp_selector_str(struct nvme_bdev *nbdev)
    3789             : {
    3790           0 :         switch (nbdev->mp_selector) {
    3791           0 :         case BDEV_NVME_MP_SELECTOR_ROUND_ROBIN:
    3792           0 :                 return "round_robin";
    3793           0 :         case BDEV_NVME_MP_SELECTOR_QUEUE_DEPTH:
    3794           0 :                 return "queue_depth";
    3795           0 :         default:
    3796           0 :                 assert(false);
    3797             :                 return "invalid";
    3798             :         }
    3799             : }
    3800             : 
    3801             : static int
    3802           0 : bdev_nvme_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
    3803             : {
    3804           0 :         struct nvme_bdev *nvme_bdev = ctx;
    3805             :         struct nvme_ns *nvme_ns;
    3806             : 
    3807           0 :         pthread_mutex_lock(&nvme_bdev->mutex);
    3808           0 :         spdk_json_write_named_array_begin(w, "nvme");
    3809           0 :         TAILQ_FOREACH(nvme_ns, &nvme_bdev->nvme_ns_list, tailq) {
    3810           0 :                 nvme_namespace_info_json(w, nvme_ns);
    3811             :         }
    3812           0 :         spdk_json_write_array_end(w);
    3813           0 :         spdk_json_write_named_string(w, "mp_policy", nvme_bdev_get_mp_policy_str(nvme_bdev));
    3814           0 :         if (nvme_bdev->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE) {
    3815           0 :                 spdk_json_write_named_string(w, "selector", nvme_bdev_get_mp_selector_str(nvme_bdev));
    3816           0 :                 if (nvme_bdev->mp_selector == BDEV_NVME_MP_SELECTOR_ROUND_ROBIN) {
    3817           0 :                         spdk_json_write_named_uint32(w, "rr_min_io", nvme_bdev->rr_min_io);
    3818             :                 }
    3819             :         }
    3820           0 :         pthread_mutex_unlock(&nvme_bdev->mutex);
    3821             : 
    3822           0 :         return 0;
    3823             : }
    3824             : 
    3825             : static void
    3826           0 : bdev_nvme_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
    3827             : {
    3828             :         /* No config per bdev needed */
    3829           0 : }
    3830             : 
    3831             : static uint64_t
    3832           0 : bdev_nvme_get_spin_time(struct spdk_io_channel *ch)
    3833             : {
    3834           0 :         struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(ch);
    3835             :         struct nvme_io_path *io_path;
    3836             :         struct nvme_poll_group *group;
    3837           0 :         uint64_t spin_time = 0;
    3838             : 
    3839           0 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
    3840           0 :                 group = io_path->qpair->group;
    3841             : 
    3842           0 :                 if (!group || !group->collect_spin_stat) {
    3843           0 :                         continue;
    3844             :                 }
    3845             : 
    3846           0 :                 if (group->end_ticks != 0) {
    3847           0 :                         group->spin_ticks += (group->end_ticks - group->start_ticks);
    3848           0 :                         group->end_ticks = 0;
    3849             :                 }
    3850             : 
    3851           0 :                 spin_time += group->spin_ticks;
    3852           0 :                 group->start_ticks = 0;
    3853           0 :                 group->spin_ticks = 0;
    3854             :         }
    3855             : 
    3856           0 :         return (spin_time * 1000000ULL) / spdk_get_ticks_hz();
    3857             : }
    3858             : 
    3859             : static void
    3860           0 : bdev_nvme_reset_device_stat(void *ctx)
    3861             : {
    3862           0 :         struct nvme_bdev *nbdev = ctx;
    3863             : 
    3864           0 :         if (nbdev->err_stat != NULL) {
    3865           0 :                 memset(nbdev->err_stat, 0, sizeof(struct nvme_error_stat));
    3866             :         }
    3867           0 : }
    3868             : 
    3869             : /* JSON string should be lowercases and underscore delimited string. */
    3870             : static void
    3871           0 : bdev_nvme_format_nvme_status(char *dst, const char *src)
    3872             : {
    3873           0 :         char tmp[256];
    3874             : 
    3875           0 :         spdk_strcpy_replace(dst, 256, src, " - ", "_");
    3876           0 :         spdk_strcpy_replace(tmp, 256, dst, "-", "_");
    3877           0 :         spdk_strcpy_replace(dst, 256, tmp, " ", "_");
    3878           0 :         spdk_strlwr(dst);
    3879           0 : }
    3880             : 
    3881             : static void
    3882           0 : bdev_nvme_dump_device_stat_json(void *ctx, struct spdk_json_write_ctx *w)
    3883             : {
    3884           0 :         struct nvme_bdev *nbdev = ctx;
    3885           0 :         struct spdk_nvme_status status = {};
    3886             :         uint16_t sct, sc;
    3887           0 :         char status_json[256];
    3888             :         const char *status_str;
    3889             : 
    3890           0 :         if (nbdev->err_stat == NULL) {
    3891           0 :                 return;
    3892             :         }
    3893             : 
    3894           0 :         spdk_json_write_named_object_begin(w, "nvme_error");
    3895             : 
    3896           0 :         spdk_json_write_named_object_begin(w, "status_type");
    3897           0 :         for (sct = 0; sct < 8; sct++) {
    3898           0 :                 if (nbdev->err_stat->status_type[sct] == 0) {
    3899           0 :                         continue;
    3900             :                 }
    3901           0 :                 status.sct = sct;
    3902             : 
    3903           0 :                 status_str = spdk_nvme_cpl_get_status_type_string(&status);
    3904           0 :                 assert(status_str != NULL);
    3905           0 :                 bdev_nvme_format_nvme_status(status_json, status_str);
    3906             : 
    3907           0 :                 spdk_json_write_named_uint32(w, status_json, nbdev->err_stat->status_type[sct]);
    3908             :         }
    3909           0 :         spdk_json_write_object_end(w);
    3910             : 
    3911           0 :         spdk_json_write_named_object_begin(w, "status_code");
    3912           0 :         for (sct = 0; sct < 4; sct++) {
    3913           0 :                 status.sct = sct;
    3914           0 :                 for (sc = 0; sc < 256; sc++) {
    3915           0 :                         if (nbdev->err_stat->status[sct][sc] == 0) {
    3916           0 :                                 continue;
    3917             :                         }
    3918           0 :                         status.sc = sc;
    3919             : 
    3920           0 :                         status_str = spdk_nvme_cpl_get_status_string(&status);
    3921           0 :                         assert(status_str != NULL);
    3922           0 :                         bdev_nvme_format_nvme_status(status_json, status_str);
    3923             : 
    3924           0 :                         spdk_json_write_named_uint32(w, status_json, nbdev->err_stat->status[sct][sc]);
    3925             :                 }
    3926             :         }
    3927           0 :         spdk_json_write_object_end(w);
    3928             : 
    3929           0 :         spdk_json_write_object_end(w);
    3930             : }
    3931             : 
    3932             : static bool
    3933           0 : bdev_nvme_accel_sequence_supported(void *ctx, enum spdk_bdev_io_type type)
    3934             : {
    3935           0 :         struct nvme_bdev *nbdev = ctx;
    3936             :         struct spdk_nvme_ctrlr *ctrlr;
    3937             : 
    3938           0 :         if (!g_opts.allow_accel_sequence) {
    3939           0 :                 return false;
    3940             :         }
    3941             : 
    3942           0 :         switch (type) {
    3943           0 :         case SPDK_BDEV_IO_TYPE_WRITE:
    3944             :         case SPDK_BDEV_IO_TYPE_READ:
    3945           0 :                 break;
    3946           0 :         default:
    3947           0 :                 return false;
    3948             :         }
    3949             : 
    3950           0 :         ctrlr = bdev_nvme_get_ctrlr(&nbdev->disk);
    3951           0 :         assert(ctrlr != NULL);
    3952             : 
    3953           0 :         return spdk_nvme_ctrlr_get_flags(ctrlr) & SPDK_NVME_CTRLR_ACCEL_SEQUENCE_SUPPORTED;
    3954             : }
    3955             : 
    3956             : static const struct spdk_bdev_fn_table nvmelib_fn_table = {
    3957             :         .destruct                       = bdev_nvme_destruct,
    3958             :         .submit_request                 = bdev_nvme_submit_request,
    3959             :         .io_type_supported              = bdev_nvme_io_type_supported,
    3960             :         .get_io_channel                 = bdev_nvme_get_io_channel,
    3961             :         .dump_info_json                 = bdev_nvme_dump_info_json,
    3962             :         .write_config_json              = bdev_nvme_write_config_json,
    3963             :         .get_spin_time                  = bdev_nvme_get_spin_time,
    3964             :         .get_module_ctx                 = bdev_nvme_get_module_ctx,
    3965             :         .get_memory_domains             = bdev_nvme_get_memory_domains,
    3966             :         .accel_sequence_supported       = bdev_nvme_accel_sequence_supported,
    3967             :         .reset_device_stat              = bdev_nvme_reset_device_stat,
    3968             :         .dump_device_stat_json          = bdev_nvme_dump_device_stat_json,
    3969             : };
    3970             : 
    3971             : typedef int (*bdev_nvme_parse_ana_log_page_cb)(
    3972             :         const struct spdk_nvme_ana_group_descriptor *desc, void *cb_arg);
    3973             : 
    3974             : static int
    3975          40 : bdev_nvme_parse_ana_log_page(struct nvme_ctrlr *nvme_ctrlr,
    3976             :                              bdev_nvme_parse_ana_log_page_cb cb_fn, void *cb_arg)
    3977             : {
    3978             :         struct spdk_nvme_ana_group_descriptor *copied_desc;
    3979             :         uint8_t *orig_desc;
    3980             :         uint32_t i, desc_size, copy_len;
    3981          40 :         int rc = 0;
    3982             : 
    3983          40 :         if (nvme_ctrlr->ana_log_page == NULL) {
    3984           0 :                 return -EINVAL;
    3985             :         }
    3986             : 
    3987          40 :         copied_desc = nvme_ctrlr->copied_ana_desc;
    3988             : 
    3989          40 :         orig_desc = (uint8_t *)nvme_ctrlr->ana_log_page + sizeof(struct spdk_nvme_ana_page);
    3990          40 :         copy_len = nvme_ctrlr->max_ana_log_page_size - sizeof(struct spdk_nvme_ana_page);
    3991             : 
    3992          69 :         for (i = 0; i < nvme_ctrlr->ana_log_page->num_ana_group_desc; i++) {
    3993          65 :                 memcpy(copied_desc, orig_desc, copy_len);
    3994             : 
    3995          65 :                 rc = cb_fn(copied_desc, cb_arg);
    3996          65 :                 if (rc != 0) {
    3997          36 :                         break;
    3998             :                 }
    3999             : 
    4000          29 :                 desc_size = sizeof(struct spdk_nvme_ana_group_descriptor) +
    4001          29 :                             copied_desc->num_of_nsid * sizeof(uint32_t);
    4002          29 :                 orig_desc += desc_size;
    4003          29 :                 copy_len -= desc_size;
    4004             :         }
    4005             : 
    4006          40 :         return rc;
    4007             : }
    4008             : 
    4009             : static int
    4010           5 : nvme_ns_ana_transition_timedout(void *ctx)
    4011             : {
    4012           5 :         struct nvme_ns *nvme_ns = ctx;
    4013             : 
    4014           5 :         spdk_poller_unregister(&nvme_ns->anatt_timer);
    4015           5 :         nvme_ns->ana_transition_timedout = true;
    4016             : 
    4017           5 :         return SPDK_POLLER_BUSY;
    4018             : }
    4019             : 
    4020             : static void
    4021          45 : _nvme_ns_set_ana_state(struct nvme_ns *nvme_ns,
    4022             :                        const struct spdk_nvme_ana_group_descriptor *desc)
    4023             : {
    4024             :         const struct spdk_nvme_ctrlr_data *cdata;
    4025             : 
    4026          45 :         nvme_ns->ana_group_id = desc->ana_group_id;
    4027          45 :         nvme_ns->ana_state = desc->ana_state;
    4028          45 :         nvme_ns->ana_state_updating = false;
    4029             : 
    4030          45 :         switch (nvme_ns->ana_state) {
    4031          38 :         case SPDK_NVME_ANA_OPTIMIZED_STATE:
    4032             :         case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
    4033          38 :                 nvme_ns->ana_transition_timedout = false;
    4034          38 :                 spdk_poller_unregister(&nvme_ns->anatt_timer);
    4035          38 :                 break;
    4036             : 
    4037           6 :         case SPDK_NVME_ANA_INACCESSIBLE_STATE:
    4038             :         case SPDK_NVME_ANA_CHANGE_STATE:
    4039           6 :                 if (nvme_ns->anatt_timer != NULL) {
    4040           1 :                         break;
    4041             :                 }
    4042             : 
    4043           5 :                 cdata = spdk_nvme_ctrlr_get_data(nvme_ns->ctrlr->ctrlr);
    4044           5 :                 nvme_ns->anatt_timer = SPDK_POLLER_REGISTER(nvme_ns_ana_transition_timedout,
    4045             :                                        nvme_ns,
    4046             :                                        cdata->anatt * SPDK_SEC_TO_USEC);
    4047           5 :                 break;
    4048           1 :         default:
    4049           1 :                 break;
    4050             :         }
    4051          45 : }
    4052             : 
    4053             : static int
    4054          59 : nvme_ns_set_ana_state(const struct spdk_nvme_ana_group_descriptor *desc, void *cb_arg)
    4055             : {
    4056          59 :         struct nvme_ns *nvme_ns = cb_arg;
    4057             :         uint32_t i;
    4058             : 
    4059          59 :         assert(nvme_ns->ns != NULL);
    4060             : 
    4061          81 :         for (i = 0; i < desc->num_of_nsid; i++) {
    4062          58 :                 if (desc->nsid[i] != spdk_nvme_ns_get_id(nvme_ns->ns)) {
    4063          22 :                         continue;
    4064             :                 }
    4065             : 
    4066          36 :                 _nvme_ns_set_ana_state(nvme_ns, desc);
    4067          36 :                 return 1;
    4068             :         }
    4069             : 
    4070          23 :         return 0;
    4071             : }
    4072             : 
    4073             : static int
    4074           5 : nvme_generate_uuid(const char *sn, uint32_t nsid, struct spdk_uuid *uuid)
    4075             : {
    4076           5 :         int rc = 0;
    4077           5 :         struct spdk_uuid new_uuid, namespace_uuid;
    4078           5 :         char merged_str[SPDK_NVME_CTRLR_SN_LEN + NSID_STR_LEN + 1] = {'\0'};
    4079             :         /* This namespace UUID was generated using uuid_generate() method. */
    4080           5 :         const char *namespace_str = {"edaed2de-24bc-4b07-b559-f47ecbe730fd"};
    4081             :         int size;
    4082             : 
    4083           5 :         assert(strlen(sn) <= SPDK_NVME_CTRLR_SN_LEN);
    4084             : 
    4085           5 :         spdk_uuid_set_null(&new_uuid);
    4086           5 :         spdk_uuid_set_null(&namespace_uuid);
    4087             : 
    4088           5 :         size = snprintf(merged_str, sizeof(merged_str), "%s%"PRIu32, sn, nsid);
    4089           5 :         if (size <= 0 || (unsigned long)size >= sizeof(merged_str)) {
    4090           0 :                 return -EINVAL;
    4091             :         }
    4092             : 
    4093           5 :         spdk_uuid_parse(&namespace_uuid, namespace_str);
    4094             : 
    4095           5 :         rc = spdk_uuid_generate_sha1(&new_uuid, &namespace_uuid, merged_str, size);
    4096           5 :         if (rc == 0) {
    4097           5 :                 memcpy(uuid, &new_uuid, sizeof(struct spdk_uuid));
    4098             :         }
    4099             : 
    4100           5 :         return rc;
    4101             : }
    4102             : 
    4103             : static int
    4104          37 : nvme_disk_create(struct spdk_bdev *disk, const char *base_name,
    4105             :                  struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_ns *ns,
    4106             :                  uint32_t prchk_flags, void *ctx)
    4107             : {
    4108             :         const struct spdk_uuid          *uuid;
    4109             :         const uint8_t *nguid;
    4110             :         const struct spdk_nvme_ctrlr_data *cdata;
    4111             :         const struct spdk_nvme_ns_data  *nsdata;
    4112             :         const struct spdk_nvme_ctrlr_opts *opts;
    4113             :         enum spdk_nvme_csi              csi;
    4114             :         uint32_t atomic_bs, phys_bs, bs;
    4115          37 :         char sn_tmp[SPDK_NVME_CTRLR_SN_LEN + 1] = {'\0'};
    4116             :         int rc;
    4117             : 
    4118          37 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    4119          37 :         csi = spdk_nvme_ns_get_csi(ns);
    4120          37 :         opts = spdk_nvme_ctrlr_get_opts(ctrlr);
    4121             : 
    4122          37 :         switch (csi) {
    4123          37 :         case SPDK_NVME_CSI_NVM:
    4124          37 :                 disk->product_name = "NVMe disk";
    4125          37 :                 break;
    4126           0 :         case SPDK_NVME_CSI_ZNS:
    4127           0 :                 disk->product_name = "NVMe ZNS disk";
    4128           0 :                 disk->zoned = true;
    4129           0 :                 disk->zone_size = spdk_nvme_zns_ns_get_zone_size_sectors(ns);
    4130           0 :                 disk->max_zone_append_size = spdk_nvme_zns_ctrlr_get_max_zone_append_size(ctrlr) /
    4131           0 :                                              spdk_nvme_ns_get_extended_sector_size(ns);
    4132           0 :                 disk->max_open_zones = spdk_nvme_zns_ns_get_max_open_zones(ns);
    4133           0 :                 disk->max_active_zones = spdk_nvme_zns_ns_get_max_active_zones(ns);
    4134           0 :                 break;
    4135           0 :         default:
    4136           0 :                 SPDK_ERRLOG("unsupported CSI: %u\n", csi);
    4137           0 :                 return -ENOTSUP;
    4138             :         }
    4139             : 
    4140          37 :         nguid = spdk_nvme_ns_get_nguid(ns);
    4141          37 :         if (!nguid) {
    4142          37 :                 uuid = spdk_nvme_ns_get_uuid(ns);
    4143          37 :                 if (uuid) {
    4144          12 :                         disk->uuid = *uuid;
    4145          25 :                 } else if (g_opts.generate_uuids) {
    4146           0 :                         spdk_strcpy_pad(sn_tmp, cdata->sn, SPDK_NVME_CTRLR_SN_LEN, '\0');
    4147           0 :                         rc = nvme_generate_uuid(sn_tmp, spdk_nvme_ns_get_id(ns), &disk->uuid);
    4148           0 :                         if (rc < 0) {
    4149           0 :                                 SPDK_ERRLOG("UUID generation failed (%s)\n", spdk_strerror(-rc));
    4150           0 :                                 return rc;
    4151             :                         }
    4152             :                 }
    4153             :         } else {
    4154           0 :                 memcpy(&disk->uuid, nguid, sizeof(disk->uuid));
    4155             :         }
    4156             : 
    4157          37 :         disk->name = spdk_sprintf_alloc("%sn%d", base_name, spdk_nvme_ns_get_id(ns));
    4158          37 :         if (!disk->name) {
    4159           0 :                 return -ENOMEM;
    4160             :         }
    4161             : 
    4162          37 :         disk->write_cache = 0;
    4163          37 :         if (cdata->vwc.present) {
    4164             :                 /* Enable if the Volatile Write Cache exists */
    4165           0 :                 disk->write_cache = 1;
    4166             :         }
    4167          37 :         if (cdata->oncs.write_zeroes) {
    4168           0 :                 disk->max_write_zeroes = UINT16_MAX + 1;
    4169             :         }
    4170          37 :         disk->blocklen = spdk_nvme_ns_get_extended_sector_size(ns);
    4171          37 :         disk->blockcnt = spdk_nvme_ns_get_num_sectors(ns);
    4172          37 :         disk->max_segment_size = spdk_nvme_ctrlr_get_max_xfer_size(ctrlr);
    4173          37 :         disk->ctratt.raw = cdata->ctratt.raw;
    4174             :         /* NVMe driver will split one request into multiple requests
    4175             :          * based on MDTS and stripe boundary, the bdev layer will use
    4176             :          * max_segment_size and max_num_segments to split one big IO
    4177             :          * into multiple requests, then small request can't run out
    4178             :          * of NVMe internal requests data structure.
    4179             :          */
    4180          37 :         if (opts && opts->io_queue_requests) {
    4181           0 :                 disk->max_num_segments = opts->io_queue_requests / 2;
    4182             :         }
    4183          37 :         if (spdk_nvme_ctrlr_get_flags(ctrlr) & SPDK_NVME_CTRLR_SGL_SUPPORTED) {
    4184             :                 /* The nvme driver will try to split I/O that have too many
    4185             :                  * SGEs, but it doesn't work if that last SGE doesn't end on
    4186             :                  * an aggregate total that is block aligned. The bdev layer has
    4187             :                  * a more robust splitting framework, so use that instead for
    4188             :                  * this case. (See issue #3269.)
    4189             :                  */
    4190           0 :                 uint16_t max_sges = spdk_nvme_ctrlr_get_max_sges(ctrlr);
    4191             : 
    4192           0 :                 if (disk->max_num_segments == 0) {
    4193           0 :                         disk->max_num_segments = max_sges;
    4194             :                 } else {
    4195           0 :                         disk->max_num_segments = spdk_min(disk->max_num_segments, max_sges);
    4196             :                 }
    4197             :         }
    4198          37 :         disk->optimal_io_boundary = spdk_nvme_ns_get_optimal_io_boundary(ns);
    4199             : 
    4200          37 :         nsdata = spdk_nvme_ns_get_data(ns);
    4201          37 :         bs = spdk_nvme_ns_get_sector_size(ns);
    4202          37 :         atomic_bs = bs;
    4203          37 :         phys_bs = bs;
    4204          37 :         if (nsdata->nabo == 0) {
    4205          37 :                 if (nsdata->nsfeat.ns_atomic_write_unit && nsdata->nawupf) {
    4206           0 :                         atomic_bs = bs * (1 + nsdata->nawupf);
    4207             :                 } else {
    4208          37 :                         atomic_bs = bs * (1 + cdata->awupf);
    4209             :                 }
    4210             :         }
    4211          37 :         if (nsdata->nsfeat.optperf) {
    4212           0 :                 phys_bs = bs * (1 + nsdata->npwg);
    4213             :         }
    4214          37 :         disk->phys_blocklen = spdk_min(phys_bs, atomic_bs);
    4215             : 
    4216          37 :         disk->md_len = spdk_nvme_ns_get_md_size(ns);
    4217          37 :         if (disk->md_len != 0) {
    4218           0 :                 disk->md_interleave = nsdata->flbas.extended;
    4219           0 :                 disk->dif_type = (enum spdk_dif_type)spdk_nvme_ns_get_pi_type(ns);
    4220           0 :                 if (disk->dif_type != SPDK_DIF_DISABLE) {
    4221           0 :                         disk->dif_is_head_of_md = nsdata->dps.md_start;
    4222           0 :                         disk->dif_check_flags = prchk_flags;
    4223             :                 }
    4224             :         }
    4225             : 
    4226          37 :         if (!(spdk_nvme_ctrlr_get_flags(ctrlr) &
    4227             :               SPDK_NVME_CTRLR_COMPARE_AND_WRITE_SUPPORTED)) {
    4228          37 :                 disk->acwu = 0;
    4229           0 :         } else if (nsdata->nsfeat.ns_atomic_write_unit) {
    4230           0 :                 disk->acwu = nsdata->nacwu + 1; /* 0-based */
    4231             :         } else {
    4232           0 :                 disk->acwu = cdata->acwu + 1; /* 0-based */
    4233             :         }
    4234             : 
    4235          37 :         if (cdata->oncs.copy) {
    4236             :                 /* For now bdev interface allows only single segment copy */
    4237           0 :                 disk->max_copy = nsdata->mssrl;
    4238             :         }
    4239             : 
    4240          37 :         disk->ctxt = ctx;
    4241          37 :         disk->fn_table = &nvmelib_fn_table;
    4242          37 :         disk->module = &nvme_if;
    4243             : 
    4244          37 :         return 0;
    4245             : }
    4246             : 
    4247             : static struct nvme_bdev *
    4248          37 : nvme_bdev_alloc(void)
    4249             : {
    4250             :         struct nvme_bdev *bdev;
    4251             :         int rc;
    4252             : 
    4253          37 :         bdev = calloc(1, sizeof(*bdev));
    4254          37 :         if (!bdev) {
    4255           0 :                 SPDK_ERRLOG("bdev calloc() failed\n");
    4256           0 :                 return NULL;
    4257             :         }
    4258             : 
    4259          37 :         if (g_opts.nvme_error_stat) {
    4260           0 :                 bdev->err_stat = calloc(1, sizeof(struct nvme_error_stat));
    4261           0 :                 if (!bdev->err_stat) {
    4262           0 :                         SPDK_ERRLOG("err_stat calloc() failed\n");
    4263           0 :                         free(bdev);
    4264           0 :                         return NULL;
    4265             :                 }
    4266             :         }
    4267             : 
    4268          37 :         rc = pthread_mutex_init(&bdev->mutex, NULL);
    4269          37 :         if (rc != 0) {
    4270           0 :                 free(bdev->err_stat);
    4271           0 :                 free(bdev);
    4272           0 :                 return NULL;
    4273             :         }
    4274             : 
    4275          37 :         bdev->ref = 1;
    4276          37 :         bdev->mp_policy = BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE;
    4277          37 :         bdev->mp_selector = BDEV_NVME_MP_SELECTOR_ROUND_ROBIN;
    4278          37 :         bdev->rr_min_io = UINT32_MAX;
    4279          37 :         TAILQ_INIT(&bdev->nvme_ns_list);
    4280             : 
    4281          37 :         return bdev;
    4282             : }
    4283             : 
    4284             : static int
    4285          37 : nvme_bdev_create(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *nvme_ns)
    4286             : {
    4287             :         struct nvme_bdev *bdev;
    4288          37 :         struct nvme_bdev_ctrlr *nbdev_ctrlr = nvme_ctrlr->nbdev_ctrlr;
    4289             :         int rc;
    4290             : 
    4291          37 :         bdev = nvme_bdev_alloc();
    4292          37 :         if (bdev == NULL) {
    4293           0 :                 SPDK_ERRLOG("Failed to allocate NVMe bdev\n");
    4294           0 :                 return -ENOMEM;
    4295             :         }
    4296             : 
    4297          37 :         bdev->opal = nvme_ctrlr->opal_dev != NULL;
    4298             : 
    4299          37 :         rc = nvme_disk_create(&bdev->disk, nbdev_ctrlr->name, nvme_ctrlr->ctrlr,
    4300             :                               nvme_ns->ns, nvme_ctrlr->opts.prchk_flags, bdev);
    4301          37 :         if (rc != 0) {
    4302           0 :                 SPDK_ERRLOG("Failed to create NVMe disk\n");
    4303           0 :                 nvme_bdev_free(bdev);
    4304           0 :                 return rc;
    4305             :         }
    4306             : 
    4307          37 :         spdk_io_device_register(bdev,
    4308             :                                 bdev_nvme_create_bdev_channel_cb,
    4309             :                                 bdev_nvme_destroy_bdev_channel_cb,
    4310             :                                 sizeof(struct nvme_bdev_channel),
    4311          37 :                                 bdev->disk.name);
    4312             : 
    4313          37 :         nvme_ns->bdev = bdev;
    4314          37 :         bdev->nsid = nvme_ns->id;
    4315          37 :         TAILQ_INSERT_TAIL(&bdev->nvme_ns_list, nvme_ns, tailq);
    4316             : 
    4317          37 :         bdev->nbdev_ctrlr = nbdev_ctrlr;
    4318          37 :         TAILQ_INSERT_TAIL(&nbdev_ctrlr->bdevs, bdev, tailq);
    4319             : 
    4320          37 :         rc = spdk_bdev_register(&bdev->disk);
    4321          37 :         if (rc != 0) {
    4322           1 :                 SPDK_ERRLOG("spdk_bdev_register() failed\n");
    4323           1 :                 spdk_io_device_unregister(bdev, NULL);
    4324           1 :                 nvme_ns->bdev = NULL;
    4325           1 :                 TAILQ_REMOVE(&nbdev_ctrlr->bdevs, bdev, tailq);
    4326           1 :                 nvme_bdev_free(bdev);
    4327           1 :                 return rc;
    4328             :         }
    4329             : 
    4330          36 :         return 0;
    4331             : }
    4332             : 
    4333             : static bool
    4334          23 : bdev_nvme_compare_ns(struct spdk_nvme_ns *ns1, struct spdk_nvme_ns *ns2)
    4335             : {
    4336             :         const struct spdk_nvme_ns_data *nsdata1, *nsdata2;
    4337             :         const struct spdk_uuid *uuid1, *uuid2;
    4338             : 
    4339          23 :         nsdata1 = spdk_nvme_ns_get_data(ns1);
    4340          23 :         nsdata2 = spdk_nvme_ns_get_data(ns2);
    4341          23 :         uuid1 = spdk_nvme_ns_get_uuid(ns1);
    4342          23 :         uuid2 = spdk_nvme_ns_get_uuid(ns2);
    4343             : 
    4344          45 :         return memcmp(nsdata1->nguid, nsdata2->nguid, sizeof(nsdata1->nguid)) == 0 &&
    4345          22 :                nsdata1->eui64 == nsdata2->eui64 &&
    4346          21 :                ((uuid1 == NULL && uuid2 == NULL) ||
    4347          59 :                 (uuid1 != NULL && uuid2 != NULL && spdk_uuid_compare(uuid1, uuid2) == 0)) &&
    4348          18 :                spdk_nvme_ns_get_csi(ns1) == spdk_nvme_ns_get_csi(ns2);
    4349             : }
    4350             : 
    4351             : static bool
    4352           0 : hotplug_probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
    4353             :                  struct spdk_nvme_ctrlr_opts *opts)
    4354             : {
    4355             :         struct nvme_probe_skip_entry *entry;
    4356             : 
    4357           0 :         TAILQ_FOREACH(entry, &g_skipped_nvme_ctrlrs, tailq) {
    4358           0 :                 if (spdk_nvme_transport_id_compare(trid, &entry->trid) == 0) {
    4359           0 :                         return false;
    4360             :                 }
    4361             :         }
    4362             : 
    4363           0 :         opts->arbitration_burst = (uint8_t)g_opts.arbitration_burst;
    4364           0 :         opts->low_priority_weight = (uint8_t)g_opts.low_priority_weight;
    4365           0 :         opts->medium_priority_weight = (uint8_t)g_opts.medium_priority_weight;
    4366           0 :         opts->high_priority_weight = (uint8_t)g_opts.high_priority_weight;
    4367           0 :         opts->disable_read_ana_log_page = true;
    4368             : 
    4369           0 :         SPDK_DEBUGLOG(bdev_nvme, "Attaching to %s\n", trid->traddr);
    4370             : 
    4371           0 :         return true;
    4372             : }
    4373             : 
    4374             : static void
    4375           0 : nvme_abort_cpl(void *ctx, const struct spdk_nvme_cpl *cpl)
    4376             : {
    4377           0 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    4378             : 
    4379           0 :         if (spdk_nvme_cpl_is_error(cpl)) {
    4380           0 :                 SPDK_WARNLOG("Abort failed. Resetting controller. sc is %u, sct is %u.\n", cpl->status.sc,
    4381             :                              cpl->status.sct);
    4382           0 :                 bdev_nvme_reset_ctrlr(nvme_ctrlr);
    4383           0 :         } else if (cpl->cdw0 & 0x1) {
    4384           0 :                 SPDK_WARNLOG("Specified command could not be aborted.\n");
    4385           0 :                 bdev_nvme_reset_ctrlr(nvme_ctrlr);
    4386             :         }
    4387           0 : }
    4388             : 
    4389             : static void
    4390           0 : timeout_cb(void *cb_arg, struct spdk_nvme_ctrlr *ctrlr,
    4391             :            struct spdk_nvme_qpair *qpair, uint16_t cid)
    4392             : {
    4393           0 :         struct nvme_ctrlr *nvme_ctrlr = cb_arg;
    4394             :         union spdk_nvme_csts_register csts;
    4395             :         int rc;
    4396             : 
    4397           0 :         assert(nvme_ctrlr->ctrlr == ctrlr);
    4398             : 
    4399           0 :         SPDK_WARNLOG("Warning: Detected a timeout. ctrlr=%p qpair=%p cid=%u\n", ctrlr, qpair, cid);
    4400             : 
    4401             :         /* Only try to read CSTS if it's a PCIe controller or we have a timeout on an I/O
    4402             :          * queue.  (Note: qpair == NULL when there's an admin cmd timeout.)  Otherwise we
    4403             :          * would submit another fabrics cmd on the admin queue to read CSTS and check for its
    4404             :          * completion recursively.
    4405             :          */
    4406           0 :         if (nvme_ctrlr->active_path_id->trid.trtype == SPDK_NVME_TRANSPORT_PCIE || qpair != NULL) {
    4407           0 :                 csts = spdk_nvme_ctrlr_get_regs_csts(ctrlr);
    4408           0 :                 if (csts.bits.cfs) {
    4409           0 :                         SPDK_ERRLOG("Controller Fatal Status, reset required\n");
    4410           0 :                         bdev_nvme_reset_ctrlr(nvme_ctrlr);
    4411           0 :                         return;
    4412             :                 }
    4413             :         }
    4414             : 
    4415           0 :         switch (g_opts.action_on_timeout) {
    4416           0 :         case SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT:
    4417           0 :                 if (qpair) {
    4418             :                         /* Don't send abort to ctrlr when ctrlr is not available. */
    4419           0 :                         pthread_mutex_lock(&nvme_ctrlr->mutex);
    4420           0 :                         if (!nvme_ctrlr_is_available(nvme_ctrlr)) {
    4421           0 :                                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4422           0 :                                 SPDK_NOTICELOG("Quit abort. Ctrlr is not available.\n");
    4423           0 :                                 return;
    4424             :                         }
    4425           0 :                         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4426             : 
    4427           0 :                         rc = spdk_nvme_ctrlr_cmd_abort(ctrlr, qpair, cid,
    4428             :                                                        nvme_abort_cpl, nvme_ctrlr);
    4429           0 :                         if (rc == 0) {
    4430           0 :                                 return;
    4431             :                         }
    4432             : 
    4433           0 :                         SPDK_ERRLOG("Unable to send abort. Resetting, rc is %d.\n", rc);
    4434             :                 }
    4435             : 
    4436             :         /* FALLTHROUGH */
    4437             :         case SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET:
    4438           0 :                 bdev_nvme_reset_ctrlr(nvme_ctrlr);
    4439           0 :                 break;
    4440           0 :         case SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE:
    4441           0 :                 SPDK_DEBUGLOG(bdev_nvme, "No action for nvme controller timeout.\n");
    4442           0 :                 break;
    4443           0 :         default:
    4444           0 :                 SPDK_ERRLOG("An invalid timeout action value is found.\n");
    4445           0 :                 break;
    4446             :         }
    4447             : }
    4448             : 
    4449             : static struct nvme_ns *
    4450          50 : nvme_ns_alloc(void)
    4451             : {
    4452             :         struct nvme_ns *nvme_ns;
    4453             : 
    4454          50 :         nvme_ns = calloc(1, sizeof(struct nvme_ns));
    4455          50 :         if (nvme_ns == NULL) {
    4456           0 :                 return NULL;
    4457             :         }
    4458             : 
    4459          50 :         if (g_opts.io_path_stat) {
    4460           0 :                 nvme_ns->stat = calloc(1, sizeof(struct spdk_bdev_io_stat));
    4461           0 :                 if (nvme_ns->stat == NULL) {
    4462           0 :                         free(nvme_ns);
    4463           0 :                         return NULL;
    4464             :                 }
    4465           0 :                 spdk_bdev_reset_io_stat(nvme_ns->stat, SPDK_BDEV_RESET_STAT_MAXMIN);
    4466             :         }
    4467             : 
    4468          50 :         return nvme_ns;
    4469             : }
    4470             : 
    4471             : static void
    4472          50 : nvme_ns_free(struct nvme_ns *nvme_ns)
    4473             : {
    4474          50 :         free(nvme_ns->stat);
    4475          50 :         free(nvme_ns);
    4476          50 : }
    4477             : 
    4478             : static void
    4479          50 : nvme_ctrlr_populate_namespace_done(struct nvme_ns *nvme_ns, int rc)
    4480             : {
    4481          50 :         struct nvme_ctrlr *nvme_ctrlr = nvme_ns->ctrlr;
    4482          50 :         struct nvme_async_probe_ctx *ctx = nvme_ns->probe_ctx;
    4483             : 
    4484          50 :         if (rc == 0) {
    4485          48 :                 nvme_ns->probe_ctx = NULL;
    4486          48 :                 pthread_mutex_lock(&nvme_ctrlr->mutex);
    4487          48 :                 nvme_ctrlr->ref++;
    4488          48 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4489             :         } else {
    4490           2 :                 RB_REMOVE(nvme_ns_tree, &nvme_ctrlr->namespaces, nvme_ns);
    4491           2 :                 nvme_ns_free(nvme_ns);
    4492             :         }
    4493             : 
    4494          50 :         if (ctx) {
    4495          49 :                 ctx->populates_in_progress--;
    4496          49 :                 if (ctx->populates_in_progress == 0) {
    4497          12 :                         nvme_ctrlr_populate_namespaces_done(nvme_ctrlr, ctx);
    4498             :                 }
    4499             :         }
    4500          50 : }
    4501             : 
    4502             : static void
    4503           2 : bdev_nvme_add_io_path(struct spdk_io_channel_iter *i)
    4504             : {
    4505           2 :         struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
    4506           2 :         struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(_ch);
    4507           2 :         struct nvme_ns *nvme_ns = spdk_io_channel_iter_get_ctx(i);
    4508             :         int rc;
    4509             : 
    4510           2 :         rc = _bdev_nvme_add_io_path(nbdev_ch, nvme_ns);
    4511           2 :         if (rc != 0) {
    4512           0 :                 SPDK_ERRLOG("Failed to add I/O path to bdev_channel dynamically.\n");
    4513             :         }
    4514             : 
    4515           2 :         spdk_for_each_channel_continue(i, rc);
    4516           2 : }
    4517             : 
    4518             : static void
    4519           2 : bdev_nvme_delete_io_path(struct spdk_io_channel_iter *i)
    4520             : {
    4521           2 :         struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
    4522           2 :         struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(_ch);
    4523           2 :         struct nvme_ns *nvme_ns = spdk_io_channel_iter_get_ctx(i);
    4524             :         struct nvme_io_path *io_path;
    4525             : 
    4526           2 :         io_path = _bdev_nvme_get_io_path(nbdev_ch, nvme_ns);
    4527           2 :         if (io_path != NULL) {
    4528           2 :                 _bdev_nvme_delete_io_path(nbdev_ch, io_path);
    4529             :         }
    4530             : 
    4531           2 :         spdk_for_each_channel_continue(i, 0);
    4532           2 : }
    4533             : 
    4534             : static void
    4535           0 : bdev_nvme_add_io_path_failed(struct spdk_io_channel_iter *i, int status)
    4536             : {
    4537           0 :         struct nvme_ns *nvme_ns = spdk_io_channel_iter_get_ctx(i);
    4538             : 
    4539           0 :         nvme_ctrlr_populate_namespace_done(nvme_ns, -1);
    4540           0 : }
    4541             : 
    4542             : static void
    4543          12 : bdev_nvme_add_io_path_done(struct spdk_io_channel_iter *i, int status)
    4544             : {
    4545          12 :         struct nvme_ns *nvme_ns = spdk_io_channel_iter_get_ctx(i);
    4546          12 :         struct nvme_bdev *bdev = spdk_io_channel_iter_get_io_device(i);
    4547             : 
    4548          12 :         if (status == 0) {
    4549          12 :                 nvme_ctrlr_populate_namespace_done(nvme_ns, 0);
    4550             :         } else {
    4551             :                 /* Delete the added io_paths and fail populating the namespace. */
    4552           0 :                 spdk_for_each_channel(bdev,
    4553             :                                       bdev_nvme_delete_io_path,
    4554             :                                       nvme_ns,
    4555             :                                       bdev_nvme_add_io_path_failed);
    4556             :         }
    4557          12 : }
    4558             : 
    4559             : static int
    4560          13 : nvme_bdev_add_ns(struct nvme_bdev *bdev, struct nvme_ns *nvme_ns)
    4561             : {
    4562             :         struct nvme_ns *tmp_ns;
    4563             :         const struct spdk_nvme_ns_data *nsdata;
    4564             : 
    4565          13 :         nsdata = spdk_nvme_ns_get_data(nvme_ns->ns);
    4566          13 :         if (!nsdata->nmic.can_share) {
    4567           0 :                 SPDK_ERRLOG("Namespace cannot be shared.\n");
    4568           0 :                 return -EINVAL;
    4569             :         }
    4570             : 
    4571          13 :         pthread_mutex_lock(&bdev->mutex);
    4572             : 
    4573          13 :         tmp_ns = TAILQ_FIRST(&bdev->nvme_ns_list);
    4574          13 :         assert(tmp_ns != NULL);
    4575             : 
    4576          13 :         if (tmp_ns->ns != NULL && !bdev_nvme_compare_ns(nvme_ns->ns, tmp_ns->ns)) {
    4577           1 :                 pthread_mutex_unlock(&bdev->mutex);
    4578           1 :                 SPDK_ERRLOG("Namespaces are not identical.\n");
    4579           1 :                 return -EINVAL;
    4580             :         }
    4581             : 
    4582          12 :         bdev->ref++;
    4583          12 :         TAILQ_INSERT_TAIL(&bdev->nvme_ns_list, nvme_ns, tailq);
    4584          12 :         nvme_ns->bdev = bdev;
    4585             : 
    4586          12 :         pthread_mutex_unlock(&bdev->mutex);
    4587             : 
    4588             :         /* Add nvme_io_path to nvme_bdev_channels dynamically. */
    4589          12 :         spdk_for_each_channel(bdev,
    4590             :                               bdev_nvme_add_io_path,
    4591             :                               nvme_ns,
    4592             :                               bdev_nvme_add_io_path_done);
    4593             : 
    4594          12 :         return 0;
    4595             : }
    4596             : 
    4597             : static void
    4598          50 : nvme_ctrlr_populate_namespace(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *nvme_ns)
    4599             : {
    4600             :         struct spdk_nvme_ns     *ns;
    4601             :         struct nvme_bdev        *bdev;
    4602          50 :         int                     rc = 0;
    4603             : 
    4604          50 :         ns = spdk_nvme_ctrlr_get_ns(nvme_ctrlr->ctrlr, nvme_ns->id);
    4605          50 :         if (!ns) {
    4606           0 :                 SPDK_DEBUGLOG(bdev_nvme, "Invalid NS %d\n", nvme_ns->id);
    4607           0 :                 rc = -EINVAL;
    4608           0 :                 goto done;
    4609             :         }
    4610             : 
    4611          50 :         nvme_ns->ns = ns;
    4612          50 :         nvme_ns->ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
    4613             : 
    4614          50 :         if (nvme_ctrlr->ana_log_page != NULL) {
    4615          37 :                 bdev_nvme_parse_ana_log_page(nvme_ctrlr, nvme_ns_set_ana_state, nvme_ns);
    4616             :         }
    4617             : 
    4618          50 :         bdev = nvme_bdev_ctrlr_get_bdev(nvme_ctrlr->nbdev_ctrlr, nvme_ns->id);
    4619          50 :         if (bdev == NULL) {
    4620          37 :                 rc = nvme_bdev_create(nvme_ctrlr, nvme_ns);
    4621             :         } else {
    4622          13 :                 rc = nvme_bdev_add_ns(bdev, nvme_ns);
    4623          13 :                 if (rc == 0) {
    4624          12 :                         return;
    4625             :                 }
    4626             :         }
    4627           1 : done:
    4628          38 :         nvme_ctrlr_populate_namespace_done(nvme_ns, rc);
    4629             : }
    4630             : 
    4631             : static void
    4632          48 : nvme_ctrlr_depopulate_namespace_done(struct nvme_ns *nvme_ns)
    4633             : {
    4634          48 :         struct nvme_ctrlr *nvme_ctrlr = nvme_ns->ctrlr;
    4635             : 
    4636          48 :         assert(nvme_ctrlr != NULL);
    4637             : 
    4638          48 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    4639             : 
    4640          48 :         RB_REMOVE(nvme_ns_tree, &nvme_ctrlr->namespaces, nvme_ns);
    4641             : 
    4642          48 :         if (nvme_ns->bdev != NULL) {
    4643           0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4644           0 :                 return;
    4645             :         }
    4646             : 
    4647          48 :         nvme_ns_free(nvme_ns);
    4648          48 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4649             : 
    4650          48 :         nvme_ctrlr_release(nvme_ctrlr);
    4651             : }
    4652             : 
    4653             : static void
    4654          11 : bdev_nvme_delete_io_path_done(struct spdk_io_channel_iter *i, int status)
    4655             : {
    4656          11 :         struct nvme_ns *nvme_ns = spdk_io_channel_iter_get_ctx(i);
    4657             : 
    4658          11 :         nvme_ctrlr_depopulate_namespace_done(nvme_ns);
    4659          11 : }
    4660             : 
    4661             : static void
    4662          48 : nvme_ctrlr_depopulate_namespace(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *nvme_ns)
    4663             : {
    4664             :         struct nvme_bdev *bdev;
    4665             : 
    4666          48 :         spdk_poller_unregister(&nvme_ns->anatt_timer);
    4667             : 
    4668          48 :         bdev = nvme_ns->bdev;
    4669          48 :         if (bdev != NULL) {
    4670          44 :                 pthread_mutex_lock(&bdev->mutex);
    4671             : 
    4672          44 :                 assert(bdev->ref > 0);
    4673          44 :                 bdev->ref--;
    4674          44 :                 if (bdev->ref == 0) {
    4675          33 :                         pthread_mutex_unlock(&bdev->mutex);
    4676             : 
    4677          33 :                         spdk_bdev_unregister(&bdev->disk, NULL, NULL);
    4678             :                 } else {
    4679             :                         /* spdk_bdev_unregister() is not called until the last nvme_ns is
    4680             :                          * depopulated. Hence we need to remove nvme_ns from bdev->nvme_ns_list
    4681             :                          * and clear nvme_ns->bdev here.
    4682             :                          */
    4683          11 :                         TAILQ_REMOVE(&bdev->nvme_ns_list, nvme_ns, tailq);
    4684          11 :                         nvme_ns->bdev = NULL;
    4685             : 
    4686          11 :                         pthread_mutex_unlock(&bdev->mutex);
    4687             : 
    4688             :                         /* Delete nvme_io_paths from nvme_bdev_channels dynamically. After that,
    4689             :                          * we call depopulate_namespace_done() to avoid use-after-free.
    4690             :                          */
    4691          11 :                         spdk_for_each_channel(bdev,
    4692             :                                               bdev_nvme_delete_io_path,
    4693             :                                               nvme_ns,
    4694             :                                               bdev_nvme_delete_io_path_done);
    4695          11 :                         return;
    4696             :                 }
    4697             :         }
    4698             : 
    4699          37 :         nvme_ctrlr_depopulate_namespace_done(nvme_ns);
    4700             : }
    4701             : 
    4702             : static void
    4703          61 : nvme_ctrlr_populate_namespaces(struct nvme_ctrlr *nvme_ctrlr,
    4704             :                                struct nvme_async_probe_ctx *ctx)
    4705             : {
    4706          61 :         struct spdk_nvme_ctrlr  *ctrlr = nvme_ctrlr->ctrlr;
    4707             :         struct nvme_ns  *nvme_ns, *next;
    4708             :         struct spdk_nvme_ns     *ns;
    4709             :         struct nvme_bdev        *bdev;
    4710             :         uint32_t                nsid;
    4711             :         int                     rc;
    4712             :         uint64_t                num_sectors;
    4713             : 
    4714          61 :         if (ctx) {
    4715             :                 /* Initialize this count to 1 to handle the populate functions
    4716             :                  * calling nvme_ctrlr_populate_namespace_done() immediately.
    4717             :                  */
    4718          45 :                 ctx->populates_in_progress = 1;
    4719             :         }
    4720             : 
    4721             :         /* First loop over our existing namespaces and see if they have been
    4722             :          * removed. */
    4723          61 :         nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
    4724          65 :         while (nvme_ns != NULL) {
    4725           4 :                 next = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns);
    4726             : 
    4727           4 :                 if (spdk_nvme_ctrlr_is_active_ns(ctrlr, nvme_ns->id)) {
    4728             :                         /* NS is still there or added again. Its attributes may have changed. */
    4729           3 :                         ns = spdk_nvme_ctrlr_get_ns(ctrlr, nvme_ns->id);
    4730           3 :                         if (nvme_ns->ns != ns) {
    4731           1 :                                 assert(nvme_ns->ns == NULL);
    4732           1 :                                 nvme_ns->ns = ns;
    4733           1 :                                 SPDK_DEBUGLOG(bdev_nvme, "NSID %u was added\n", nvme_ns->id);
    4734             :                         }
    4735             : 
    4736           3 :                         num_sectors = spdk_nvme_ns_get_num_sectors(ns);
    4737           3 :                         bdev = nvme_ns->bdev;
    4738           3 :                         assert(bdev != NULL);
    4739           3 :                         if (bdev->disk.blockcnt != num_sectors) {
    4740           1 :                                 SPDK_NOTICELOG("NSID %u is resized: bdev name %s, old size %" PRIu64 ", new size %" PRIu64 "\n",
    4741             :                                                nvme_ns->id,
    4742             :                                                bdev->disk.name,
    4743             :                                                bdev->disk.blockcnt,
    4744             :                                                num_sectors);
    4745           1 :                                 rc = spdk_bdev_notify_blockcnt_change(&bdev->disk, num_sectors);
    4746           1 :                                 if (rc != 0) {
    4747           0 :                                         SPDK_ERRLOG("Could not change num blocks for nvme bdev: name %s, errno: %d.\n",
    4748             :                                                     bdev->disk.name, rc);
    4749             :                                 }
    4750             :                         }
    4751             :                 } else {
    4752             :                         /* Namespace was removed */
    4753           1 :                         nvme_ctrlr_depopulate_namespace(nvme_ctrlr, nvme_ns);
    4754             :                 }
    4755             : 
    4756           4 :                 nvme_ns = next;
    4757             :         }
    4758             : 
    4759             :         /* Loop through all of the namespaces at the nvme level and see if any of them are new */
    4760          61 :         nsid = spdk_nvme_ctrlr_get_first_active_ns(ctrlr);
    4761         114 :         while (nsid != 0) {
    4762          53 :                 nvme_ns = nvme_ctrlr_get_ns(nvme_ctrlr, nsid);
    4763             : 
    4764          53 :                 if (nvme_ns == NULL) {
    4765             :                         /* Found a new one */
    4766          50 :                         nvme_ns = nvme_ns_alloc();
    4767          50 :                         if (nvme_ns == NULL) {
    4768           0 :                                 SPDK_ERRLOG("Failed to allocate namespace\n");
    4769             :                                 /* This just fails to attach the namespace. It may work on a future attempt. */
    4770           0 :                                 continue;
    4771             :                         }
    4772             : 
    4773          50 :                         nvme_ns->id = nsid;
    4774          50 :                         nvme_ns->ctrlr = nvme_ctrlr;
    4775             : 
    4776          50 :                         nvme_ns->bdev = NULL;
    4777             : 
    4778          50 :                         if (ctx) {
    4779          49 :                                 ctx->populates_in_progress++;
    4780             :                         }
    4781          50 :                         nvme_ns->probe_ctx = ctx;
    4782             : 
    4783          50 :                         RB_INSERT(nvme_ns_tree, &nvme_ctrlr->namespaces, nvme_ns);
    4784             : 
    4785          50 :                         nvme_ctrlr_populate_namespace(nvme_ctrlr, nvme_ns);
    4786             :                 }
    4787             : 
    4788          53 :                 nsid = spdk_nvme_ctrlr_get_next_active_ns(ctrlr, nsid);
    4789             :         }
    4790             : 
    4791          61 :         if (ctx) {
    4792             :                 /* Decrement this count now that the loop is over to account
    4793             :                  * for the one we started with.  If the count is then 0, we
    4794             :                  * know any populate_namespace functions completed immediately,
    4795             :                  * so we'll kick the callback here.
    4796             :                  */
    4797          45 :                 ctx->populates_in_progress--;
    4798          45 :                 if (ctx->populates_in_progress == 0) {
    4799          33 :                         nvme_ctrlr_populate_namespaces_done(nvme_ctrlr, ctx);
    4800             :                 }
    4801             :         }
    4802             : 
    4803          61 : }
    4804             : 
    4805             : static void
    4806          59 : nvme_ctrlr_depopulate_namespaces(struct nvme_ctrlr *nvme_ctrlr)
    4807             : {
    4808             :         struct nvme_ns *nvme_ns, *tmp;
    4809             : 
    4810         106 :         RB_FOREACH_SAFE(nvme_ns, nvme_ns_tree, &nvme_ctrlr->namespaces, tmp) {
    4811          47 :                 nvme_ctrlr_depopulate_namespace(nvme_ctrlr, nvme_ns);
    4812             :         }
    4813          59 : }
    4814             : 
    4815             : static uint32_t
    4816          36 : nvme_ctrlr_get_ana_log_page_size(struct nvme_ctrlr *nvme_ctrlr)
    4817             : {
    4818          36 :         struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
    4819             :         const struct spdk_nvme_ctrlr_data *cdata;
    4820          36 :         uint32_t nsid, ns_count = 0;
    4821             : 
    4822          36 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    4823             : 
    4824          80 :         for (nsid = spdk_nvme_ctrlr_get_first_active_ns(ctrlr);
    4825          44 :              nsid != 0; nsid = spdk_nvme_ctrlr_get_next_active_ns(ctrlr, nsid)) {
    4826          44 :                 ns_count++;
    4827             :         }
    4828             : 
    4829          36 :         return sizeof(struct spdk_nvme_ana_page) + cdata->nanagrpid *
    4830          36 :                sizeof(struct spdk_nvme_ana_group_descriptor) + ns_count *
    4831             :                sizeof(uint32_t);
    4832             : }
    4833             : 
    4834             : static int
    4835           6 : nvme_ctrlr_set_ana_states(const struct spdk_nvme_ana_group_descriptor *desc,
    4836             :                           void *cb_arg)
    4837             : {
    4838           6 :         struct nvme_ctrlr *nvme_ctrlr = cb_arg;
    4839             :         struct nvme_ns *nvme_ns;
    4840             :         uint32_t i, nsid;
    4841             : 
    4842          11 :         for (i = 0; i < desc->num_of_nsid; i++) {
    4843           5 :                 nsid = desc->nsid[i];
    4844           5 :                 if (nsid == 0) {
    4845           0 :                         continue;
    4846             :                 }
    4847             : 
    4848           5 :                 nvme_ns = nvme_ctrlr_get_ns(nvme_ctrlr, nsid);
    4849             : 
    4850           5 :                 assert(nvme_ns != NULL);
    4851           5 :                 if (nvme_ns == NULL) {
    4852             :                         /* Target told us that an inactive namespace had an ANA change */
    4853           0 :                         continue;
    4854             :                 }
    4855             : 
    4856           5 :                 _nvme_ns_set_ana_state(nvme_ns, desc);
    4857             :         }
    4858             : 
    4859           6 :         return 0;
    4860             : }
    4861             : 
    4862             : static void
    4863           0 : bdev_nvme_disable_read_ana_log_page(struct nvme_ctrlr *nvme_ctrlr)
    4864             : {
    4865             :         struct nvme_ns *nvme_ns;
    4866             : 
    4867           0 :         spdk_free(nvme_ctrlr->ana_log_page);
    4868           0 :         nvme_ctrlr->ana_log_page = NULL;
    4869             : 
    4870           0 :         for (nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
    4871             :              nvme_ns != NULL;
    4872           0 :              nvme_ns = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns)) {
    4873           0 :                 nvme_ns->ana_state_updating = false;
    4874           0 :                 nvme_ns->ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
    4875             :         }
    4876           0 : }
    4877             : 
    4878             : static void
    4879           3 : nvme_ctrlr_read_ana_log_page_done(void *ctx, const struct spdk_nvme_cpl *cpl)
    4880             : {
    4881           3 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    4882             : 
    4883           3 :         if (cpl != NULL && spdk_nvme_cpl_is_success(cpl)) {
    4884           3 :                 bdev_nvme_parse_ana_log_page(nvme_ctrlr, nvme_ctrlr_set_ana_states,
    4885             :                                              nvme_ctrlr);
    4886             :         } else {
    4887           0 :                 bdev_nvme_disable_read_ana_log_page(nvme_ctrlr);
    4888             :         }
    4889             : 
    4890           3 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    4891             : 
    4892           3 :         assert(nvme_ctrlr->ana_log_page_updating == true);
    4893           3 :         nvme_ctrlr->ana_log_page_updating = false;
    4894             : 
    4895           3 :         if (nvme_ctrlr_can_be_unregistered(nvme_ctrlr)) {
    4896           0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4897             : 
    4898           0 :                 nvme_ctrlr_unregister(nvme_ctrlr);
    4899             :         } else {
    4900           3 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4901             : 
    4902           3 :                 bdev_nvme_clear_io_path_caches(nvme_ctrlr);
    4903             :         }
    4904           3 : }
    4905             : 
    4906             : static int
    4907           6 : nvme_ctrlr_read_ana_log_page(struct nvme_ctrlr *nvme_ctrlr)
    4908             : {
    4909             :         uint32_t ana_log_page_size;
    4910             :         int rc;
    4911             : 
    4912           6 :         if (nvme_ctrlr->ana_log_page == NULL) {
    4913           0 :                 return -EINVAL;
    4914             :         }
    4915             : 
    4916           6 :         ana_log_page_size = nvme_ctrlr_get_ana_log_page_size(nvme_ctrlr);
    4917             : 
    4918           6 :         if (ana_log_page_size > nvme_ctrlr->max_ana_log_page_size) {
    4919           0 :                 SPDK_ERRLOG("ANA log page size %" PRIu32 " is larger than allowed %" PRIu32 "\n",
    4920             :                             ana_log_page_size, nvme_ctrlr->max_ana_log_page_size);
    4921           0 :                 return -EINVAL;
    4922             :         }
    4923             : 
    4924           6 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    4925           6 :         if (!nvme_ctrlr_is_available(nvme_ctrlr) ||
    4926             :             nvme_ctrlr->ana_log_page_updating) {
    4927           3 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4928           3 :                 return -EBUSY;
    4929             :         }
    4930             : 
    4931           3 :         nvme_ctrlr->ana_log_page_updating = true;
    4932           3 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4933             : 
    4934           3 :         rc = spdk_nvme_ctrlr_cmd_get_log_page(nvme_ctrlr->ctrlr,
    4935             :                                               SPDK_NVME_LOG_ASYMMETRIC_NAMESPACE_ACCESS,
    4936             :                                               SPDK_NVME_GLOBAL_NS_TAG,
    4937           3 :                                               nvme_ctrlr->ana_log_page,
    4938             :                                               ana_log_page_size, 0,
    4939             :                                               nvme_ctrlr_read_ana_log_page_done,
    4940             :                                               nvme_ctrlr);
    4941           3 :         if (rc != 0) {
    4942           0 :                 nvme_ctrlr_read_ana_log_page_done(nvme_ctrlr, NULL);
    4943             :         }
    4944             : 
    4945           3 :         return rc;
    4946             : }
    4947             : 
    4948             : static void
    4949           0 : dummy_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *ctx)
    4950             : {
    4951           0 : }
    4952             : 
    4953             : struct bdev_nvme_set_preferred_path_ctx {
    4954             :         struct spdk_bdev_desc *desc;
    4955             :         struct nvme_ns *nvme_ns;
    4956             :         bdev_nvme_set_preferred_path_cb cb_fn;
    4957             :         void *cb_arg;
    4958             : };
    4959             : 
    4960             : static void
    4961           3 : bdev_nvme_set_preferred_path_done(struct spdk_io_channel_iter *i, int status)
    4962             : {
    4963           3 :         struct bdev_nvme_set_preferred_path_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    4964             : 
    4965           3 :         assert(ctx != NULL);
    4966           3 :         assert(ctx->desc != NULL);
    4967           3 :         assert(ctx->cb_fn != NULL);
    4968             : 
    4969           3 :         spdk_bdev_close(ctx->desc);
    4970             : 
    4971           3 :         ctx->cb_fn(ctx->cb_arg, status);
    4972             : 
    4973           3 :         free(ctx);
    4974           3 : }
    4975             : 
    4976             : static void
    4977           2 : _bdev_nvme_set_preferred_path(struct spdk_io_channel_iter *i)
    4978             : {
    4979           2 :         struct bdev_nvme_set_preferred_path_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    4980           2 :         struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
    4981           2 :         struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(_ch);
    4982             :         struct nvme_io_path *io_path, *prev;
    4983             : 
    4984           2 :         prev = NULL;
    4985           3 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
    4986           3 :                 if (io_path->nvme_ns == ctx->nvme_ns) {
    4987           2 :                         break;
    4988             :                 }
    4989           1 :                 prev = io_path;
    4990             :         }
    4991             : 
    4992           2 :         if (io_path != NULL) {
    4993           2 :                 if (prev != NULL) {
    4994           1 :                         STAILQ_REMOVE_AFTER(&nbdev_ch->io_path_list, prev, stailq);
    4995           1 :                         STAILQ_INSERT_HEAD(&nbdev_ch->io_path_list, io_path, stailq);
    4996             :                 }
    4997             : 
    4998             :                 /* We can set io_path to nbdev_ch->current_io_path directly here.
    4999             :                  * However, it needs to be conditional. To simplify the code,
    5000             :                  * just clear nbdev_ch->current_io_path and let find_io_path()
    5001             :                  * fill it.
    5002             :                  *
    5003             :                  * Automatic failback may be disabled. Hence even if the io_path is
    5004             :                  * already at the head, clear nbdev_ch->current_io_path.
    5005             :                  */
    5006           2 :                 bdev_nvme_clear_current_io_path(nbdev_ch);
    5007             :         }
    5008             : 
    5009           2 :         spdk_for_each_channel_continue(i, 0);
    5010           2 : }
    5011             : 
    5012             : static struct nvme_ns *
    5013           3 : bdev_nvme_set_preferred_ns(struct nvme_bdev *nbdev, uint16_t cntlid)
    5014             : {
    5015             :         struct nvme_ns *nvme_ns, *prev;
    5016             :         const struct spdk_nvme_ctrlr_data *cdata;
    5017             : 
    5018           3 :         prev = NULL;
    5019           6 :         TAILQ_FOREACH(nvme_ns, &nbdev->nvme_ns_list, tailq) {
    5020           6 :                 cdata = spdk_nvme_ctrlr_get_data(nvme_ns->ctrlr->ctrlr);
    5021             : 
    5022           6 :                 if (cdata->cntlid == cntlid) {
    5023           3 :                         break;
    5024             :                 }
    5025           3 :                 prev = nvme_ns;
    5026             :         }
    5027             : 
    5028           3 :         if (nvme_ns != NULL && prev != NULL) {
    5029           2 :                 TAILQ_REMOVE(&nbdev->nvme_ns_list, nvme_ns, tailq);
    5030           2 :                 TAILQ_INSERT_HEAD(&nbdev->nvme_ns_list, nvme_ns, tailq);
    5031             :         }
    5032             : 
    5033           3 :         return nvme_ns;
    5034             : }
    5035             : 
    5036             : /* This function supports only multipath mode. There is only a single I/O path
    5037             :  * for each NVMe-oF controller. Hence, just move the matched I/O path to the
    5038             :  * head of the I/O path list for each NVMe bdev channel.
    5039             :  *
    5040             :  * NVMe bdev channel may be acquired after completing this function. move the
    5041             :  * matched namespace to the head of the namespace list for the NVMe bdev too.
    5042             :  */
    5043             : void
    5044           3 : bdev_nvme_set_preferred_path(const char *name, uint16_t cntlid,
    5045             :                              bdev_nvme_set_preferred_path_cb cb_fn, void *cb_arg)
    5046             : {
    5047             :         struct bdev_nvme_set_preferred_path_ctx *ctx;
    5048             :         struct spdk_bdev *bdev;
    5049             :         struct nvme_bdev *nbdev;
    5050           3 :         int rc = 0;
    5051             : 
    5052           3 :         assert(cb_fn != NULL);
    5053             : 
    5054           3 :         ctx = calloc(1, sizeof(*ctx));
    5055           3 :         if (ctx == NULL) {
    5056           0 :                 SPDK_ERRLOG("Failed to alloc context.\n");
    5057           0 :                 rc = -ENOMEM;
    5058           0 :                 goto err_alloc;
    5059             :         }
    5060             : 
    5061           3 :         ctx->cb_fn = cb_fn;
    5062           3 :         ctx->cb_arg = cb_arg;
    5063             : 
    5064           3 :         rc = spdk_bdev_open_ext(name, false, dummy_bdev_event_cb, NULL, &ctx->desc);
    5065           3 :         if (rc != 0) {
    5066           0 :                 SPDK_ERRLOG("Failed to open bdev %s.\n", name);
    5067           0 :                 goto err_open;
    5068             :         }
    5069             : 
    5070           3 :         bdev = spdk_bdev_desc_get_bdev(ctx->desc);
    5071             : 
    5072           3 :         if (bdev->module != &nvme_if) {
    5073           0 :                 SPDK_ERRLOG("bdev %s is not registered in this module.\n", name);
    5074           0 :                 rc = -ENODEV;
    5075           0 :                 goto err_bdev;
    5076             :         }
    5077             : 
    5078           3 :         nbdev = SPDK_CONTAINEROF(bdev, struct nvme_bdev, disk);
    5079             : 
    5080           3 :         pthread_mutex_lock(&nbdev->mutex);
    5081             : 
    5082           3 :         ctx->nvme_ns = bdev_nvme_set_preferred_ns(nbdev, cntlid);
    5083           3 :         if (ctx->nvme_ns == NULL) {
    5084           0 :                 pthread_mutex_unlock(&nbdev->mutex);
    5085             : 
    5086           0 :                 SPDK_ERRLOG("bdev %s does not have namespace to controller %u.\n", name, cntlid);
    5087           0 :                 rc = -ENODEV;
    5088           0 :                 goto err_bdev;
    5089             :         }
    5090             : 
    5091           3 :         pthread_mutex_unlock(&nbdev->mutex);
    5092             : 
    5093           3 :         spdk_for_each_channel(nbdev,
    5094             :                               _bdev_nvme_set_preferred_path,
    5095             :                               ctx,
    5096             :                               bdev_nvme_set_preferred_path_done);
    5097           3 :         return;
    5098             : 
    5099           0 : err_bdev:
    5100           0 :         spdk_bdev_close(ctx->desc);
    5101           0 : err_open:
    5102           0 :         free(ctx);
    5103           0 : err_alloc:
    5104           0 :         cb_fn(cb_arg, rc);
    5105             : }
    5106             : 
    5107             : struct bdev_nvme_set_multipath_policy_ctx {
    5108             :         struct spdk_bdev_desc *desc;
    5109             :         bdev_nvme_set_multipath_policy_cb cb_fn;
    5110             :         void *cb_arg;
    5111             : };
    5112             : 
    5113             : static void
    5114           3 : bdev_nvme_set_multipath_policy_done(struct spdk_io_channel_iter *i, int status)
    5115             : {
    5116           3 :         struct bdev_nvme_set_multipath_policy_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    5117             : 
    5118           3 :         assert(ctx != NULL);
    5119           3 :         assert(ctx->desc != NULL);
    5120           3 :         assert(ctx->cb_fn != NULL);
    5121             : 
    5122           3 :         spdk_bdev_close(ctx->desc);
    5123             : 
    5124           3 :         ctx->cb_fn(ctx->cb_arg, status);
    5125             : 
    5126           3 :         free(ctx);
    5127           3 : }
    5128             : 
    5129             : static void
    5130           1 : _bdev_nvme_set_multipath_policy(struct spdk_io_channel_iter *i)
    5131             : {
    5132           1 :         struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
    5133           1 :         struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(_ch);
    5134           1 :         struct nvme_bdev *nbdev = spdk_io_channel_get_io_device(_ch);
    5135             : 
    5136           1 :         nbdev_ch->mp_policy = nbdev->mp_policy;
    5137           1 :         nbdev_ch->mp_selector = nbdev->mp_selector;
    5138           1 :         nbdev_ch->rr_min_io = nbdev->rr_min_io;
    5139           1 :         bdev_nvme_clear_current_io_path(nbdev_ch);
    5140             : 
    5141           1 :         spdk_for_each_channel_continue(i, 0);
    5142           1 : }
    5143             : 
    5144             : void
    5145           3 : bdev_nvme_set_multipath_policy(const char *name, enum bdev_nvme_multipath_policy policy,
    5146             :                                enum bdev_nvme_multipath_selector selector, uint32_t rr_min_io,
    5147             :                                bdev_nvme_set_multipath_policy_cb cb_fn, void *cb_arg)
    5148             : {
    5149             :         struct bdev_nvme_set_multipath_policy_ctx *ctx;
    5150             :         struct spdk_bdev *bdev;
    5151             :         struct nvme_bdev *nbdev;
    5152             :         int rc;
    5153             : 
    5154           3 :         assert(cb_fn != NULL);
    5155             : 
    5156           3 :         switch (policy) {
    5157           1 :         case BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE:
    5158           1 :                 break;
    5159           2 :         case BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE:
    5160             :                 switch (selector) {
    5161           1 :                 case BDEV_NVME_MP_SELECTOR_ROUND_ROBIN:
    5162           1 :                         if (rr_min_io == UINT32_MAX) {
    5163           0 :                                 rr_min_io = 1;
    5164           1 :                         } else if (rr_min_io == 0) {
    5165           0 :                                 rc = -EINVAL;
    5166           0 :                                 goto exit;
    5167             :                         }
    5168           1 :                         break;
    5169           1 :                 case BDEV_NVME_MP_SELECTOR_QUEUE_DEPTH:
    5170           1 :                         break;
    5171           0 :                 default:
    5172           0 :                         rc = -EINVAL;
    5173           0 :                         goto exit;
    5174             :                 }
    5175           2 :                 break;
    5176           0 :         default:
    5177           0 :                 rc = -EINVAL;
    5178           0 :                 goto exit;
    5179             :         }
    5180             : 
    5181           3 :         ctx = calloc(1, sizeof(*ctx));
    5182           3 :         if (ctx == NULL) {
    5183           0 :                 SPDK_ERRLOG("Failed to alloc context.\n");
    5184           0 :                 rc = -ENOMEM;
    5185           0 :                 goto exit;
    5186             :         }
    5187             : 
    5188           3 :         ctx->cb_fn = cb_fn;
    5189           3 :         ctx->cb_arg = cb_arg;
    5190             : 
    5191           3 :         rc = spdk_bdev_open_ext(name, false, dummy_bdev_event_cb, NULL, &ctx->desc);
    5192           3 :         if (rc != 0) {
    5193           0 :                 SPDK_ERRLOG("Failed to open bdev %s.\n", name);
    5194           0 :                 rc = -ENODEV;
    5195           0 :                 goto err_open;
    5196             :         }
    5197             : 
    5198           3 :         bdev = spdk_bdev_desc_get_bdev(ctx->desc);
    5199           3 :         if (bdev->module != &nvme_if) {
    5200           0 :                 SPDK_ERRLOG("bdev %s is not registered in this module.\n", name);
    5201           0 :                 rc = -ENODEV;
    5202           0 :                 goto err_module;
    5203             :         }
    5204           3 :         nbdev = SPDK_CONTAINEROF(bdev, struct nvme_bdev, disk);
    5205             : 
    5206           3 :         pthread_mutex_lock(&nbdev->mutex);
    5207           3 :         nbdev->mp_policy = policy;
    5208           3 :         nbdev->mp_selector = selector;
    5209           3 :         nbdev->rr_min_io = rr_min_io;
    5210           3 :         pthread_mutex_unlock(&nbdev->mutex);
    5211             : 
    5212           3 :         spdk_for_each_channel(nbdev,
    5213             :                               _bdev_nvme_set_multipath_policy,
    5214             :                               ctx,
    5215             :                               bdev_nvme_set_multipath_policy_done);
    5216           3 :         return;
    5217             : 
    5218           0 : err_module:
    5219           0 :         spdk_bdev_close(ctx->desc);
    5220           0 : err_open:
    5221           0 :         free(ctx);
    5222           0 : exit:
    5223           0 :         cb_fn(cb_arg, rc);
    5224             : }
    5225             : 
    5226             : static void
    5227           3 : aer_cb(void *arg, const struct spdk_nvme_cpl *cpl)
    5228             : {
    5229           3 :         struct nvme_ctrlr *nvme_ctrlr           = arg;
    5230             :         union spdk_nvme_async_event_completion  event;
    5231             : 
    5232           3 :         if (spdk_nvme_cpl_is_error(cpl)) {
    5233           0 :                 SPDK_WARNLOG("AER request execute failed\n");
    5234           0 :                 return;
    5235             :         }
    5236             : 
    5237           3 :         event.raw = cpl->cdw0;
    5238           3 :         if ((event.bits.async_event_type == SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) &&
    5239           3 :             (event.bits.async_event_info == SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGED)) {
    5240           2 :                 nvme_ctrlr_populate_namespaces(nvme_ctrlr, NULL);
    5241           1 :         } else if ((event.bits.async_event_type == SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) &&
    5242           1 :                    (event.bits.async_event_info == SPDK_NVME_ASYNC_EVENT_ANA_CHANGE)) {
    5243           1 :                 nvme_ctrlr_read_ana_log_page(nvme_ctrlr);
    5244             :         }
    5245             : }
    5246             : 
    5247             : static void
    5248          51 : free_nvme_async_probe_ctx(struct nvme_async_probe_ctx *ctx)
    5249             : {
    5250          51 :         spdk_keyring_put_key(ctx->drv_opts.tls_psk);
    5251          51 :         spdk_keyring_put_key(ctx->drv_opts.dhchap_key);
    5252          51 :         spdk_keyring_put_key(ctx->drv_opts.dhchap_ctrlr_key);
    5253          51 :         free(ctx);
    5254          51 : }
    5255             : 
    5256             : static void
    5257          51 : populate_namespaces_cb(struct nvme_async_probe_ctx *ctx, int rc)
    5258             : {
    5259          51 :         if (ctx->cb_fn) {
    5260          51 :                 ctx->cb_fn(ctx->cb_ctx, ctx->reported_bdevs, rc);
    5261             :         }
    5262             : 
    5263          51 :         ctx->namespaces_populated = true;
    5264          51 :         if (ctx->probe_done) {
    5265             :                 /* The probe was already completed, so we need to free the context
    5266             :                  * here.  This can happen for cases like OCSSD, where we need to
    5267             :                  * send additional commands to the SSD after attach.
    5268             :                  */
    5269          31 :                 free_nvme_async_probe_ctx(ctx);
    5270             :         }
    5271          51 : }
    5272             : 
    5273             : static void
    5274          59 : nvme_ctrlr_create_done(struct nvme_ctrlr *nvme_ctrlr,
    5275             :                        struct nvme_async_probe_ctx *ctx)
    5276             : {
    5277          59 :         spdk_io_device_register(nvme_ctrlr,
    5278             :                                 bdev_nvme_create_ctrlr_channel_cb,
    5279             :                                 bdev_nvme_destroy_ctrlr_channel_cb,
    5280             :                                 sizeof(struct nvme_ctrlr_channel),
    5281          59 :                                 nvme_ctrlr->nbdev_ctrlr->name);
    5282             : 
    5283          59 :         nvme_ctrlr_populate_namespaces(nvme_ctrlr, ctx);
    5284          59 : }
    5285             : 
    5286             : static void
    5287          30 : nvme_ctrlr_init_ana_log_page_done(void *_ctx, const struct spdk_nvme_cpl *cpl)
    5288             : {
    5289          30 :         struct nvme_ctrlr *nvme_ctrlr = _ctx;
    5290          30 :         struct nvme_async_probe_ctx *ctx = nvme_ctrlr->probe_ctx;
    5291             : 
    5292          30 :         nvme_ctrlr->probe_ctx = NULL;
    5293             : 
    5294          30 :         if (spdk_nvme_cpl_is_error(cpl)) {
    5295           0 :                 nvme_ctrlr_delete(nvme_ctrlr);
    5296             : 
    5297           0 :                 if (ctx != NULL) {
    5298           0 :                         ctx->reported_bdevs = 0;
    5299           0 :                         populate_namespaces_cb(ctx, -1);
    5300             :                 }
    5301           0 :                 return;
    5302             :         }
    5303             : 
    5304          30 :         nvme_ctrlr_create_done(nvme_ctrlr, ctx);
    5305             : }
    5306             : 
    5307             : static int
    5308          30 : nvme_ctrlr_init_ana_log_page(struct nvme_ctrlr *nvme_ctrlr,
    5309             :                              struct nvme_async_probe_ctx *ctx)
    5310             : {
    5311          30 :         struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
    5312             :         const struct spdk_nvme_ctrlr_data *cdata;
    5313             :         uint32_t ana_log_page_size;
    5314             : 
    5315          30 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    5316             : 
    5317             :         /* Set buffer size enough to include maximum number of allowed namespaces. */
    5318          30 :         ana_log_page_size = sizeof(struct spdk_nvme_ana_page) + cdata->nanagrpid *
    5319          30 :                             sizeof(struct spdk_nvme_ana_group_descriptor) + cdata->mnan *
    5320             :                             sizeof(uint32_t);
    5321             : 
    5322          30 :         nvme_ctrlr->ana_log_page = spdk_zmalloc(ana_log_page_size, 64, NULL,
    5323             :                                                 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    5324          30 :         if (nvme_ctrlr->ana_log_page == NULL) {
    5325           0 :                 SPDK_ERRLOG("could not allocate ANA log page buffer\n");
    5326           0 :                 return -ENXIO;
    5327             :         }
    5328             : 
    5329             :         /* Each descriptor in a ANA log page is not ensured to be 8-bytes aligned.
    5330             :          * Hence copy each descriptor to a temporary area when parsing it.
    5331             :          *
    5332             :          * Allocate a buffer whose size is as large as ANA log page buffer because
    5333             :          * we do not know the size of a descriptor until actually reading it.
    5334             :          */
    5335          30 :         nvme_ctrlr->copied_ana_desc = calloc(1, ana_log_page_size);
    5336          30 :         if (nvme_ctrlr->copied_ana_desc == NULL) {
    5337           0 :                 SPDK_ERRLOG("could not allocate a buffer to parse ANA descriptor\n");
    5338           0 :                 return -ENOMEM;
    5339             :         }
    5340             : 
    5341          30 :         nvme_ctrlr->max_ana_log_page_size = ana_log_page_size;
    5342             : 
    5343          30 :         nvme_ctrlr->probe_ctx = ctx;
    5344             : 
    5345             :         /* Then, set the read size only to include the current active namespaces. */
    5346          30 :         ana_log_page_size = nvme_ctrlr_get_ana_log_page_size(nvme_ctrlr);
    5347             : 
    5348          30 :         if (ana_log_page_size > nvme_ctrlr->max_ana_log_page_size) {
    5349           0 :                 SPDK_ERRLOG("ANA log page size %" PRIu32 " is larger than allowed %" PRIu32 "\n",
    5350             :                             ana_log_page_size, nvme_ctrlr->max_ana_log_page_size);
    5351           0 :                 return -EINVAL;
    5352             :         }
    5353             : 
    5354          30 :         return spdk_nvme_ctrlr_cmd_get_log_page(ctrlr,
    5355             :                                                 SPDK_NVME_LOG_ASYMMETRIC_NAMESPACE_ACCESS,
    5356             :                                                 SPDK_NVME_GLOBAL_NS_TAG,
    5357          30 :                                                 nvme_ctrlr->ana_log_page,
    5358             :                                                 ana_log_page_size, 0,
    5359             :                                                 nvme_ctrlr_init_ana_log_page_done,
    5360             :                                                 nvme_ctrlr);
    5361             : }
    5362             : 
    5363             : /* hostnqn and subnqn were already verified before attaching a controller.
    5364             :  * Hence check only the multipath capability and cntlid here.
    5365             :  */
    5366             : static bool
    5367          16 : bdev_nvme_check_multipath(struct nvme_bdev_ctrlr *nbdev_ctrlr, struct spdk_nvme_ctrlr *ctrlr)
    5368             : {
    5369             :         struct nvme_ctrlr *tmp;
    5370             :         const struct spdk_nvme_ctrlr_data *cdata, *tmp_cdata;
    5371             : 
    5372          16 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    5373             : 
    5374          16 :         if (!cdata->cmic.multi_ctrlr) {
    5375           0 :                 SPDK_ERRLOG("Ctrlr%u does not support multipath.\n", cdata->cntlid);
    5376           0 :                 return false;
    5377             :         }
    5378             : 
    5379          33 :         TAILQ_FOREACH(tmp, &nbdev_ctrlr->ctrlrs, tailq) {
    5380          18 :                 tmp_cdata = spdk_nvme_ctrlr_get_data(tmp->ctrlr);
    5381             : 
    5382          18 :                 if (!tmp_cdata->cmic.multi_ctrlr) {
    5383           0 :                         SPDK_ERRLOG("Ctrlr%u does not support multipath.\n", cdata->cntlid);
    5384           0 :                         return false;
    5385             :                 }
    5386          18 :                 if (cdata->cntlid == tmp_cdata->cntlid) {
    5387           1 :                         SPDK_ERRLOG("cntlid %u are duplicated.\n", tmp_cdata->cntlid);
    5388           1 :                         return false;
    5389             :                 }
    5390             :         }
    5391             : 
    5392          15 :         return true;
    5393             : }
    5394             : 
    5395             : static int
    5396          60 : nvme_bdev_ctrlr_create(const char *name, struct nvme_ctrlr *nvme_ctrlr)
    5397             : {
    5398             :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
    5399          60 :         struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
    5400          60 :         int rc = 0;
    5401             : 
    5402          60 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    5403             : 
    5404          60 :         nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
    5405          60 :         if (nbdev_ctrlr != NULL) {
    5406          16 :                 if (!bdev_nvme_check_multipath(nbdev_ctrlr, ctrlr)) {
    5407           1 :                         rc = -EINVAL;
    5408           1 :                         goto exit;
    5409             :                 }
    5410             :         } else {
    5411          44 :                 nbdev_ctrlr = calloc(1, sizeof(*nbdev_ctrlr));
    5412          44 :                 if (nbdev_ctrlr == NULL) {
    5413           0 :                         SPDK_ERRLOG("Failed to allocate nvme_bdev_ctrlr.\n");
    5414           0 :                         rc = -ENOMEM;
    5415           0 :                         goto exit;
    5416             :                 }
    5417          44 :                 nbdev_ctrlr->name = strdup(name);
    5418          44 :                 if (nbdev_ctrlr->name == NULL) {
    5419           0 :                         SPDK_ERRLOG("Failed to allocate name of nvme_bdev_ctrlr.\n");
    5420           0 :                         free(nbdev_ctrlr);
    5421           0 :                         goto exit;
    5422             :                 }
    5423          44 :                 TAILQ_INIT(&nbdev_ctrlr->ctrlrs);
    5424          44 :                 TAILQ_INIT(&nbdev_ctrlr->bdevs);
    5425          44 :                 TAILQ_INSERT_TAIL(&g_nvme_bdev_ctrlrs, nbdev_ctrlr, tailq);
    5426             :         }
    5427          59 :         nvme_ctrlr->nbdev_ctrlr = nbdev_ctrlr;
    5428          59 :         TAILQ_INSERT_TAIL(&nbdev_ctrlr->ctrlrs, nvme_ctrlr, tailq);
    5429          60 : exit:
    5430          60 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    5431          60 :         return rc;
    5432             : }
    5433             : 
    5434             : static int
    5435          60 : nvme_ctrlr_create(struct spdk_nvme_ctrlr *ctrlr,
    5436             :                   const char *name,
    5437             :                   const struct spdk_nvme_transport_id *trid,
    5438             :                   struct nvme_async_probe_ctx *ctx)
    5439             : {
    5440             :         struct nvme_ctrlr *nvme_ctrlr;
    5441             :         struct nvme_path_id *path_id;
    5442             :         const struct spdk_nvme_ctrlr_data *cdata;
    5443             :         int rc;
    5444             : 
    5445          60 :         nvme_ctrlr = calloc(1, sizeof(*nvme_ctrlr));
    5446          60 :         if (nvme_ctrlr == NULL) {
    5447           0 :                 SPDK_ERRLOG("Failed to allocate device struct\n");
    5448           0 :                 return -ENOMEM;
    5449             :         }
    5450             : 
    5451          60 :         rc = pthread_mutex_init(&nvme_ctrlr->mutex, NULL);
    5452          60 :         if (rc != 0) {
    5453           0 :                 free(nvme_ctrlr);
    5454           0 :                 return rc;
    5455             :         }
    5456             : 
    5457          60 :         TAILQ_INIT(&nvme_ctrlr->trids);
    5458          60 :         RB_INIT(&nvme_ctrlr->namespaces);
    5459             : 
    5460             :         /* Get another reference to the key, so the first one can be released from probe_ctx */
    5461          60 :         if (ctx != NULL) {
    5462          46 :                 if (ctx->drv_opts.tls_psk != NULL) {
    5463           0 :                         nvme_ctrlr->psk = spdk_keyring_get_key(
    5464             :                                                   spdk_key_get_name(ctx->drv_opts.tls_psk));
    5465           0 :                         if (nvme_ctrlr->psk == NULL) {
    5466             :                                 /* Could only happen if the key was removed in the meantime */
    5467           0 :                                 SPDK_ERRLOG("Couldn't get a reference to the key '%s'\n",
    5468             :                                             spdk_key_get_name(ctx->drv_opts.tls_psk));
    5469           0 :                                 rc = -ENOKEY;
    5470           0 :                                 goto err;
    5471             :                         }
    5472             :                 }
    5473             : 
    5474          46 :                 if (ctx->drv_opts.dhchap_key != NULL) {
    5475           0 :                         nvme_ctrlr->dhchap_key = spdk_keyring_get_key(
    5476             :                                                          spdk_key_get_name(ctx->drv_opts.dhchap_key));
    5477           0 :                         if (nvme_ctrlr->dhchap_key == NULL) {
    5478           0 :                                 SPDK_ERRLOG("Couldn't get a reference to the key '%s'\n",
    5479             :                                             spdk_key_get_name(ctx->drv_opts.dhchap_key));
    5480           0 :                                 rc = -ENOKEY;
    5481           0 :                                 goto err;
    5482             :                         }
    5483             :                 }
    5484             : 
    5485          46 :                 if (ctx->drv_opts.dhchap_ctrlr_key != NULL) {
    5486           0 :                         nvme_ctrlr->dhchap_ctrlr_key =
    5487           0 :                                 spdk_keyring_get_key(
    5488             :                                         spdk_key_get_name(ctx->drv_opts.dhchap_ctrlr_key));
    5489           0 :                         if (nvme_ctrlr->dhchap_ctrlr_key == NULL) {
    5490           0 :                                 SPDK_ERRLOG("Couldn't get a reference to the key '%s'\n",
    5491             :                                             spdk_key_get_name(ctx->drv_opts.dhchap_ctrlr_key));
    5492           0 :                                 rc = -ENOKEY;
    5493           0 :                                 goto err;
    5494             :                         }
    5495             :                 }
    5496             :         }
    5497             : 
    5498          60 :         path_id = calloc(1, sizeof(*path_id));
    5499          60 :         if (path_id == NULL) {
    5500           0 :                 SPDK_ERRLOG("Failed to allocate trid entry pointer\n");
    5501           0 :                 rc = -ENOMEM;
    5502           0 :                 goto err;
    5503             :         }
    5504             : 
    5505          60 :         path_id->trid = *trid;
    5506          60 :         if (ctx != NULL) {
    5507          46 :                 memcpy(path_id->hostid.hostaddr, ctx->drv_opts.src_addr, sizeof(path_id->hostid.hostaddr));
    5508          46 :                 memcpy(path_id->hostid.hostsvcid, ctx->drv_opts.src_svcid, sizeof(path_id->hostid.hostsvcid));
    5509             :         }
    5510          60 :         nvme_ctrlr->active_path_id = path_id;
    5511          60 :         TAILQ_INSERT_HEAD(&nvme_ctrlr->trids, path_id, link);
    5512             : 
    5513          60 :         nvme_ctrlr->thread = spdk_get_thread();
    5514          60 :         nvme_ctrlr->ctrlr = ctrlr;
    5515          60 :         nvme_ctrlr->ref = 1;
    5516             : 
    5517          60 :         if (spdk_nvme_ctrlr_is_ocssd_supported(ctrlr)) {
    5518           0 :                 SPDK_ERRLOG("OCSSDs are not supported");
    5519           0 :                 rc = -ENOTSUP;
    5520           0 :                 goto err;
    5521             :         }
    5522             : 
    5523          60 :         if (ctx != NULL) {
    5524          46 :                 memcpy(&nvme_ctrlr->opts, &ctx->bdev_opts, sizeof(ctx->bdev_opts));
    5525             :         } else {
    5526          14 :                 bdev_nvme_get_default_ctrlr_opts(&nvme_ctrlr->opts);
    5527             :         }
    5528             : 
    5529          60 :         nvme_ctrlr->adminq_timer_poller = SPDK_POLLER_REGISTER(bdev_nvme_poll_adminq, nvme_ctrlr,
    5530             :                                           g_opts.nvme_adminq_poll_period_us);
    5531             : 
    5532          60 :         if (g_opts.timeout_us > 0) {
    5533             :                 /* Register timeout callback. Timeout values for IO vs. admin reqs can be different. */
    5534             :                 /* If timeout_admin_us is 0 (not specified), admin uses same timeout as IO. */
    5535           0 :                 uint64_t adm_timeout_us = (g_opts.timeout_admin_us == 0) ?
    5536           0 :                                           g_opts.timeout_us : g_opts.timeout_admin_us;
    5537           0 :                 spdk_nvme_ctrlr_register_timeout_callback(ctrlr, g_opts.timeout_us,
    5538             :                                 adm_timeout_us, timeout_cb, nvme_ctrlr);
    5539             :         }
    5540             : 
    5541          60 :         spdk_nvme_ctrlr_register_aer_callback(ctrlr, aer_cb, nvme_ctrlr);
    5542          60 :         spdk_nvme_ctrlr_set_remove_cb(ctrlr, remove_cb, nvme_ctrlr);
    5543             : 
    5544          60 :         if (spdk_nvme_ctrlr_get_flags(ctrlr) &
    5545             :             SPDK_NVME_CTRLR_SECURITY_SEND_RECV_SUPPORTED) {
    5546           0 :                 nvme_ctrlr->opal_dev = spdk_opal_dev_construct(ctrlr);
    5547             :         }
    5548             : 
    5549          60 :         rc = nvme_bdev_ctrlr_create(name, nvme_ctrlr);
    5550          60 :         if (rc != 0) {
    5551           1 :                 goto err;
    5552             :         }
    5553             : 
    5554          59 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    5555             : 
    5556          59 :         if (cdata->cmic.ana_reporting) {
    5557          30 :                 rc = nvme_ctrlr_init_ana_log_page(nvme_ctrlr, ctx);
    5558          30 :                 if (rc == 0) {
    5559          30 :                         return 0;
    5560             :                 }
    5561             :         } else {
    5562          29 :                 nvme_ctrlr_create_done(nvme_ctrlr, ctx);
    5563          29 :                 return 0;
    5564             :         }
    5565             : 
    5566           1 : err:
    5567           1 :         nvme_ctrlr_delete(nvme_ctrlr);
    5568           1 :         return rc;
    5569             : }
    5570             : 
    5571             : void
    5572          56 : bdev_nvme_get_default_ctrlr_opts(struct nvme_ctrlr_opts *opts)
    5573             : {
    5574          56 :         opts->prchk_flags = 0;
    5575          56 :         opts->ctrlr_loss_timeout_sec = g_opts.ctrlr_loss_timeout_sec;
    5576          56 :         opts->reconnect_delay_sec = g_opts.reconnect_delay_sec;
    5577          56 :         opts->fast_io_fail_timeout_sec = g_opts.fast_io_fail_timeout_sec;
    5578          56 : }
    5579             : 
    5580             : static void
    5581           0 : attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
    5582             :           struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *drv_opts)
    5583             : {
    5584             :         char *name;
    5585             : 
    5586           0 :         name = spdk_sprintf_alloc("HotInNvme%d", g_hot_insert_nvme_controller_index++);
    5587           0 :         if (!name) {
    5588           0 :                 SPDK_ERRLOG("Failed to assign name to NVMe device\n");
    5589           0 :                 return;
    5590             :         }
    5591             : 
    5592           0 :         if (nvme_ctrlr_create(ctrlr, name, trid, NULL) == 0) {
    5593           0 :                 SPDK_DEBUGLOG(bdev_nvme, "Attached to %s (%s)\n", trid->traddr, name);
    5594             :         } else {
    5595           0 :                 SPDK_ERRLOG("Failed to attach to %s (%s)\n", trid->traddr, name);
    5596             :         }
    5597             : 
    5598           0 :         free(name);
    5599             : }
    5600             : 
    5601             : static void
    5602          59 : _nvme_ctrlr_destruct(void *ctx)
    5603             : {
    5604          59 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    5605             : 
    5606          59 :         nvme_ctrlr_depopulate_namespaces(nvme_ctrlr);
    5607          59 :         nvme_ctrlr_release(nvme_ctrlr);
    5608          59 : }
    5609             : 
    5610             : static int
    5611          56 : bdev_nvme_delete_ctrlr_unsafe(struct nvme_ctrlr *nvme_ctrlr, bool hotplug)
    5612             : {
    5613             :         struct nvme_probe_skip_entry *entry;
    5614             : 
    5615             :         /* The controller's destruction was already started */
    5616          56 :         if (nvme_ctrlr->destruct) {
    5617           0 :                 return -EALREADY;
    5618             :         }
    5619             : 
    5620          56 :         if (!hotplug &&
    5621          56 :             nvme_ctrlr->active_path_id->trid.trtype == SPDK_NVME_TRANSPORT_PCIE) {
    5622           0 :                 entry = calloc(1, sizeof(*entry));
    5623           0 :                 if (!entry) {
    5624           0 :                         return -ENOMEM;
    5625             :                 }
    5626           0 :                 entry->trid = nvme_ctrlr->active_path_id->trid;
    5627           0 :                 TAILQ_INSERT_TAIL(&g_skipped_nvme_ctrlrs, entry, tailq);
    5628             :         }
    5629             : 
    5630          56 :         nvme_ctrlr->destruct = true;
    5631          56 :         return 0;
    5632             : }
    5633             : 
    5634             : static int
    5635           2 : bdev_nvme_delete_ctrlr(struct nvme_ctrlr *nvme_ctrlr, bool hotplug)
    5636             : {
    5637             :         int rc;
    5638             : 
    5639           2 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    5640           2 :         rc = bdev_nvme_delete_ctrlr_unsafe(nvme_ctrlr, hotplug);
    5641           2 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    5642             : 
    5643           2 :         if (rc == 0) {
    5644           2 :                 _nvme_ctrlr_destruct(nvme_ctrlr);
    5645           0 :         } else if (rc == -EALREADY) {
    5646           0 :                 rc = 0;
    5647             :         }
    5648             : 
    5649           2 :         return rc;
    5650             : }
    5651             : 
    5652             : static void
    5653           0 : remove_cb(void *cb_ctx, struct spdk_nvme_ctrlr *ctrlr)
    5654             : {
    5655           0 :         struct nvme_ctrlr *nvme_ctrlr = cb_ctx;
    5656             : 
    5657           0 :         bdev_nvme_delete_ctrlr(nvme_ctrlr, true);
    5658           0 : }
    5659             : 
    5660             : static int
    5661           0 : bdev_nvme_hotplug_probe(void *arg)
    5662             : {
    5663           0 :         if (g_hotplug_probe_ctx == NULL) {
    5664           0 :                 spdk_poller_unregister(&g_hotplug_probe_poller);
    5665           0 :                 return SPDK_POLLER_IDLE;
    5666             :         }
    5667             : 
    5668           0 :         if (spdk_nvme_probe_poll_async(g_hotplug_probe_ctx) != -EAGAIN) {
    5669           0 :                 g_hotplug_probe_ctx = NULL;
    5670           0 :                 spdk_poller_unregister(&g_hotplug_probe_poller);
    5671             :         }
    5672             : 
    5673           0 :         return SPDK_POLLER_BUSY;
    5674             : }
    5675             : 
    5676             : static int
    5677           0 : bdev_nvme_hotplug(void *arg)
    5678             : {
    5679           0 :         struct spdk_nvme_transport_id trid_pcie;
    5680             : 
    5681           0 :         if (g_hotplug_probe_ctx) {
    5682           0 :                 return SPDK_POLLER_BUSY;
    5683             :         }
    5684             : 
    5685           0 :         memset(&trid_pcie, 0, sizeof(trid_pcie));
    5686           0 :         spdk_nvme_trid_populate_transport(&trid_pcie, SPDK_NVME_TRANSPORT_PCIE);
    5687             : 
    5688           0 :         g_hotplug_probe_ctx = spdk_nvme_probe_async(&trid_pcie, NULL,
    5689             :                               hotplug_probe_cb, attach_cb, NULL);
    5690             : 
    5691           0 :         if (g_hotplug_probe_ctx) {
    5692           0 :                 assert(g_hotplug_probe_poller == NULL);
    5693           0 :                 g_hotplug_probe_poller = SPDK_POLLER_REGISTER(bdev_nvme_hotplug_probe, NULL, 1000);
    5694             :         }
    5695             : 
    5696           0 :         return SPDK_POLLER_BUSY;
    5697             : }
    5698             : 
    5699             : void
    5700           0 : bdev_nvme_get_opts(struct spdk_bdev_nvme_opts *opts)
    5701             : {
    5702           0 :         *opts = g_opts;
    5703           0 : }
    5704             : 
    5705             : static bool bdev_nvme_check_io_error_resiliency_params(int32_t ctrlr_loss_timeout_sec,
    5706             :                 uint32_t reconnect_delay_sec,
    5707             :                 uint32_t fast_io_fail_timeout_sec);
    5708             : 
    5709             : static int
    5710           0 : bdev_nvme_validate_opts(const struct spdk_bdev_nvme_opts *opts)
    5711             : {
    5712           0 :         if ((opts->timeout_us == 0) && (opts->timeout_admin_us != 0)) {
    5713             :                 /* Can't set timeout_admin_us without also setting timeout_us */
    5714           0 :                 SPDK_WARNLOG("Invalid options: Can't have (timeout_us == 0) with (timeout_admin_us > 0)\n");
    5715           0 :                 return -EINVAL;
    5716             :         }
    5717             : 
    5718           0 :         if (opts->bdev_retry_count < -1) {
    5719           0 :                 SPDK_WARNLOG("Invalid option: bdev_retry_count can't be less than -1.\n");
    5720           0 :                 return -EINVAL;
    5721             :         }
    5722             : 
    5723           0 :         if (!bdev_nvme_check_io_error_resiliency_params(opts->ctrlr_loss_timeout_sec,
    5724             :                         opts->reconnect_delay_sec,
    5725             :                         opts->fast_io_fail_timeout_sec)) {
    5726           0 :                 return -EINVAL;
    5727             :         }
    5728             : 
    5729           0 :         return 0;
    5730             : }
    5731             : 
    5732             : int
    5733           0 : bdev_nvme_set_opts(const struct spdk_bdev_nvme_opts *opts)
    5734             : {
    5735             :         int ret;
    5736             : 
    5737           0 :         ret = bdev_nvme_validate_opts(opts);
    5738           0 :         if (ret) {
    5739           0 :                 SPDK_WARNLOG("Failed to set nvme opts.\n");
    5740           0 :                 return ret;
    5741             :         }
    5742             : 
    5743           0 :         if (g_bdev_nvme_init_thread != NULL) {
    5744           0 :                 if (!TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) {
    5745           0 :                         return -EPERM;
    5746             :                 }
    5747             :         }
    5748             : 
    5749           0 :         if (opts->rdma_srq_size != 0 ||
    5750           0 :             opts->rdma_max_cq_size != 0 ||
    5751           0 :             opts->rdma_cm_event_timeout_ms != 0) {
    5752           0 :                 struct spdk_nvme_transport_opts drv_opts;
    5753             : 
    5754           0 :                 spdk_nvme_transport_get_opts(&drv_opts, sizeof(drv_opts));
    5755           0 :                 if (opts->rdma_srq_size != 0) {
    5756           0 :                         drv_opts.rdma_srq_size = opts->rdma_srq_size;
    5757             :                 }
    5758           0 :                 if (opts->rdma_max_cq_size != 0) {
    5759           0 :                         drv_opts.rdma_max_cq_size = opts->rdma_max_cq_size;
    5760             :                 }
    5761           0 :                 if (opts->rdma_cm_event_timeout_ms != 0) {
    5762           0 :                         drv_opts.rdma_cm_event_timeout_ms = opts->rdma_cm_event_timeout_ms;
    5763             :                 }
    5764             : 
    5765           0 :                 ret = spdk_nvme_transport_set_opts(&drv_opts, sizeof(drv_opts));
    5766           0 :                 if (ret) {
    5767           0 :                         SPDK_ERRLOG("Failed to set NVMe transport opts.\n");
    5768           0 :                         return ret;
    5769             :                 }
    5770             :         }
    5771             : 
    5772           0 :         g_opts = *opts;
    5773             : 
    5774           0 :         return 0;
    5775             : }
    5776             : 
    5777             : struct set_nvme_hotplug_ctx {
    5778             :         uint64_t period_us;
    5779             :         bool enabled;
    5780             :         spdk_msg_fn fn;
    5781             :         void *fn_ctx;
    5782             : };
    5783             : 
    5784             : static void
    5785           0 : set_nvme_hotplug_period_cb(void *_ctx)
    5786             : {
    5787           0 :         struct set_nvme_hotplug_ctx *ctx = _ctx;
    5788             : 
    5789           0 :         spdk_poller_unregister(&g_hotplug_poller);
    5790           0 :         if (ctx->enabled) {
    5791           0 :                 g_hotplug_poller = SPDK_POLLER_REGISTER(bdev_nvme_hotplug, NULL, ctx->period_us);
    5792             :         }
    5793             : 
    5794           0 :         g_nvme_hotplug_poll_period_us = ctx->period_us;
    5795           0 :         g_nvme_hotplug_enabled = ctx->enabled;
    5796           0 :         if (ctx->fn) {
    5797           0 :                 ctx->fn(ctx->fn_ctx);
    5798             :         }
    5799             : 
    5800           0 :         free(ctx);
    5801           0 : }
    5802             : 
    5803             : int
    5804           0 : bdev_nvme_set_hotplug(bool enabled, uint64_t period_us, spdk_msg_fn cb, void *cb_ctx)
    5805             : {
    5806             :         struct set_nvme_hotplug_ctx *ctx;
    5807             : 
    5808           0 :         if (enabled == true && !spdk_process_is_primary()) {
    5809           0 :                 return -EPERM;
    5810             :         }
    5811             : 
    5812           0 :         ctx = calloc(1, sizeof(*ctx));
    5813           0 :         if (ctx == NULL) {
    5814           0 :                 return -ENOMEM;
    5815             :         }
    5816             : 
    5817           0 :         period_us = period_us == 0 ? NVME_HOTPLUG_POLL_PERIOD_DEFAULT : period_us;
    5818           0 :         ctx->period_us = spdk_min(period_us, NVME_HOTPLUG_POLL_PERIOD_MAX);
    5819           0 :         ctx->enabled = enabled;
    5820           0 :         ctx->fn = cb;
    5821           0 :         ctx->fn_ctx = cb_ctx;
    5822             : 
    5823           0 :         spdk_thread_send_msg(g_bdev_nvme_init_thread, set_nvme_hotplug_period_cb, ctx);
    5824           0 :         return 0;
    5825             : }
    5826             : 
    5827             : static void
    5828          45 : nvme_ctrlr_populate_namespaces_done(struct nvme_ctrlr *nvme_ctrlr,
    5829             :                                     struct nvme_async_probe_ctx *ctx)
    5830             : {
    5831             :         struct nvme_ns  *nvme_ns;
    5832             :         struct nvme_bdev        *nvme_bdev;
    5833             :         size_t                  j;
    5834             : 
    5835          45 :         assert(nvme_ctrlr != NULL);
    5836             : 
    5837          45 :         if (ctx->names == NULL) {
    5838           0 :                 ctx->reported_bdevs = 0;
    5839           0 :                 populate_namespaces_cb(ctx, 0);
    5840           0 :                 return;
    5841             :         }
    5842             : 
    5843             :         /*
    5844             :          * Report the new bdevs that were created in this call.
    5845             :          * There can be more than one bdev per NVMe controller.
    5846             :          */
    5847          45 :         j = 0;
    5848          45 :         nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
    5849          92 :         while (nvme_ns != NULL) {
    5850          47 :                 nvme_bdev = nvme_ns->bdev;
    5851          47 :                 if (j < ctx->max_bdevs) {
    5852          47 :                         ctx->names[j] = nvme_bdev->disk.name;
    5853          47 :                         j++;
    5854             :                 } else {
    5855           0 :                         SPDK_ERRLOG("Maximum number of namespaces supported per NVMe controller is %du. Unable to return all names of created bdevs\n",
    5856             :                                     ctx->max_bdevs);
    5857           0 :                         ctx->reported_bdevs = 0;
    5858           0 :                         populate_namespaces_cb(ctx, -ERANGE);
    5859           0 :                         return;
    5860             :                 }
    5861             : 
    5862          47 :                 nvme_ns = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns);
    5863             :         }
    5864             : 
    5865          45 :         ctx->reported_bdevs = j;
    5866          45 :         populate_namespaces_cb(ctx, 0);
    5867             : }
    5868             : 
    5869             : static int
    5870           9 : bdev_nvme_check_secondary_trid(struct nvme_ctrlr *nvme_ctrlr,
    5871             :                                struct spdk_nvme_ctrlr *new_ctrlr,
    5872             :                                struct spdk_nvme_transport_id *trid)
    5873             : {
    5874             :         struct nvme_path_id *tmp_trid;
    5875             : 
    5876           9 :         if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
    5877           0 :                 SPDK_ERRLOG("PCIe failover is not supported.\n");
    5878           0 :                 return -ENOTSUP;
    5879             :         }
    5880             : 
    5881             :         /* Currently we only support failover to the same transport type. */
    5882           9 :         if (nvme_ctrlr->active_path_id->trid.trtype != trid->trtype) {
    5883           0 :                 SPDK_WARNLOG("Failover from trtype: %s to a different trtype: %s is not supported currently\n",
    5884             :                              spdk_nvme_transport_id_trtype_str(nvme_ctrlr->active_path_id->trid.trtype),
    5885             :                              spdk_nvme_transport_id_trtype_str(trid->trtype));
    5886           0 :                 return -EINVAL;
    5887             :         }
    5888             : 
    5889             : 
    5890             :         /* Currently we only support failover to the same NQN. */
    5891           9 :         if (strncmp(trid->subnqn, nvme_ctrlr->active_path_id->trid.subnqn, SPDK_NVMF_NQN_MAX_LEN)) {
    5892           0 :                 SPDK_WARNLOG("Failover from subnqn: %s to a different subnqn: %s is not supported currently\n",
    5893             :                              nvme_ctrlr->active_path_id->trid.subnqn, trid->subnqn);
    5894           0 :                 return -EINVAL;
    5895             :         }
    5896             : 
    5897             :         /* Skip all the other checks if we've already registered this path. */
    5898          21 :         TAILQ_FOREACH(tmp_trid, &nvme_ctrlr->trids, link) {
    5899          12 :                 if (!spdk_nvme_transport_id_compare(&tmp_trid->trid, trid)) {
    5900           0 :                         SPDK_WARNLOG("This path (traddr: %s subnqn: %s) is already registered\n", trid->traddr,
    5901             :                                      trid->subnqn);
    5902           0 :                         return -EALREADY;
    5903             :                 }
    5904             :         }
    5905             : 
    5906           9 :         return 0;
    5907             : }
    5908             : 
    5909             : static int
    5910           9 : bdev_nvme_check_secondary_namespace(struct nvme_ctrlr *nvme_ctrlr,
    5911             :                                     struct spdk_nvme_ctrlr *new_ctrlr)
    5912             : {
    5913             :         struct nvme_ns *nvme_ns;
    5914             :         struct spdk_nvme_ns *new_ns;
    5915             : 
    5916           9 :         nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
    5917           9 :         while (nvme_ns != NULL) {
    5918           0 :                 new_ns = spdk_nvme_ctrlr_get_ns(new_ctrlr, nvme_ns->id);
    5919           0 :                 assert(new_ns != NULL);
    5920             : 
    5921           0 :                 if (!bdev_nvme_compare_ns(nvme_ns->ns, new_ns)) {
    5922           0 :                         return -EINVAL;
    5923             :                 }
    5924             : 
    5925           0 :                 nvme_ns = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns);
    5926             :         }
    5927             : 
    5928           9 :         return 0;
    5929             : }
    5930             : 
    5931             : static int
    5932           9 : _bdev_nvme_add_secondary_trid(struct nvme_ctrlr *nvme_ctrlr,
    5933             :                               struct spdk_nvme_transport_id *trid)
    5934             : {
    5935             :         struct nvme_path_id *active_id, *new_trid, *tmp_trid;
    5936             : 
    5937           9 :         new_trid = calloc(1, sizeof(*new_trid));
    5938           9 :         if (new_trid == NULL) {
    5939           0 :                 return -ENOMEM;
    5940             :         }
    5941           9 :         new_trid->trid = *trid;
    5942             : 
    5943           9 :         active_id = nvme_ctrlr->active_path_id;
    5944           9 :         assert(active_id != NULL);
    5945           9 :         assert(active_id == TAILQ_FIRST(&nvme_ctrlr->trids));
    5946             : 
    5947             :         /* Skip the active trid not to replace it until it is failed. */
    5948           9 :         tmp_trid = TAILQ_NEXT(active_id, link);
    5949           9 :         if (tmp_trid == NULL) {
    5950           6 :                 goto add_tail;
    5951             :         }
    5952             : 
    5953             :         /* It means the trid is faled if its last failed time is non-zero.
    5954             :          * Insert the new alternate trid before any failed trid.
    5955             :          */
    5956           5 :         TAILQ_FOREACH_FROM(tmp_trid, &nvme_ctrlr->trids, link) {
    5957           3 :                 if (tmp_trid->last_failed_tsc != 0) {
    5958           1 :                         TAILQ_INSERT_BEFORE(tmp_trid, new_trid, link);
    5959           1 :                         return 0;
    5960             :                 }
    5961             :         }
    5962             : 
    5963           2 : add_tail:
    5964           8 :         TAILQ_INSERT_TAIL(&nvme_ctrlr->trids, new_trid, link);
    5965           8 :         return 0;
    5966             : }
    5967             : 
    5968             : /* This is the case that a secondary path is added to an existing
    5969             :  * nvme_ctrlr for failover. After checking if it can access the same
    5970             :  * namespaces as the primary path, it is disconnected until failover occurs.
    5971             :  */
    5972             : static int
    5973           9 : bdev_nvme_add_secondary_trid(struct nvme_ctrlr *nvme_ctrlr,
    5974             :                              struct spdk_nvme_ctrlr *new_ctrlr,
    5975             :                              struct spdk_nvme_transport_id *trid)
    5976             : {
    5977             :         int rc;
    5978             : 
    5979           9 :         assert(nvme_ctrlr != NULL);
    5980             : 
    5981           9 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    5982             : 
    5983           9 :         rc = bdev_nvme_check_secondary_trid(nvme_ctrlr, new_ctrlr, trid);
    5984           9 :         if (rc != 0) {
    5985           0 :                 goto exit;
    5986             :         }
    5987             : 
    5988           9 :         rc = bdev_nvme_check_secondary_namespace(nvme_ctrlr, new_ctrlr);
    5989           9 :         if (rc != 0) {
    5990           0 :                 goto exit;
    5991             :         }
    5992             : 
    5993           9 :         rc = _bdev_nvme_add_secondary_trid(nvme_ctrlr, trid);
    5994             : 
    5995           9 : exit:
    5996           9 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    5997             : 
    5998           9 :         spdk_nvme_detach(new_ctrlr);
    5999             : 
    6000           9 :         return rc;
    6001             : }
    6002             : 
    6003             : static void
    6004          46 : connect_attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
    6005             :                   struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
    6006             : {
    6007          46 :         struct spdk_nvme_ctrlr_opts *user_opts = cb_ctx;
    6008             :         struct nvme_async_probe_ctx *ctx;
    6009             :         int rc;
    6010             : 
    6011          46 :         ctx = SPDK_CONTAINEROF(user_opts, struct nvme_async_probe_ctx, drv_opts);
    6012          46 :         ctx->ctrlr_attached = true;
    6013             : 
    6014          46 :         rc = nvme_ctrlr_create(ctrlr, ctx->base_name, &ctx->trid, ctx);
    6015          46 :         if (rc != 0) {
    6016           1 :                 ctx->reported_bdevs = 0;
    6017           1 :                 populate_namespaces_cb(ctx, rc);
    6018             :         }
    6019          46 : }
    6020             : 
    6021             : static void
    6022           4 : connect_set_failover_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
    6023             :                         struct spdk_nvme_ctrlr *ctrlr,
    6024             :                         const struct spdk_nvme_ctrlr_opts *opts)
    6025             : {
    6026           4 :         struct spdk_nvme_ctrlr_opts *user_opts = cb_ctx;
    6027             :         struct nvme_ctrlr *nvme_ctrlr;
    6028             :         struct nvme_async_probe_ctx *ctx;
    6029             :         int rc;
    6030             : 
    6031           4 :         ctx = SPDK_CONTAINEROF(user_opts, struct nvme_async_probe_ctx, drv_opts);
    6032           4 :         ctx->ctrlr_attached = true;
    6033             : 
    6034           4 :         nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->base_name);
    6035           4 :         if (nvme_ctrlr) {
    6036           4 :                 rc = bdev_nvme_add_secondary_trid(nvme_ctrlr, ctrlr, &ctx->trid);
    6037             :         } else {
    6038           0 :                 rc = -ENODEV;
    6039             :         }
    6040             : 
    6041           4 :         ctx->reported_bdevs = 0;
    6042           4 :         populate_namespaces_cb(ctx, rc);
    6043           4 : }
    6044             : 
    6045             : static int
    6046          51 : bdev_nvme_async_poll(void *arg)
    6047             : {
    6048          51 :         struct nvme_async_probe_ctx     *ctx = arg;
    6049             :         int                             rc;
    6050             : 
    6051          51 :         rc = spdk_nvme_probe_poll_async(ctx->probe_ctx);
    6052          51 :         if (spdk_unlikely(rc != -EAGAIN)) {
    6053          51 :                 ctx->probe_done = true;
    6054          51 :                 spdk_poller_unregister(&ctx->poller);
    6055          51 :                 if (!ctx->ctrlr_attached) {
    6056             :                         /* The probe is done, but no controller was attached.
    6057             :                          * That means we had a failure, so report -EIO back to
    6058             :                          * the caller (usually the RPC). populate_namespaces_cb()
    6059             :                          * will take care of freeing the nvme_async_probe_ctx.
    6060             :                          */
    6061           1 :                         ctx->reported_bdevs = 0;
    6062           1 :                         populate_namespaces_cb(ctx, -EIO);
    6063          50 :                 } else if (ctx->namespaces_populated) {
    6064             :                         /* The namespaces for the attached controller were all
    6065             :                          * populated and the response was already sent to the
    6066             :                          * caller (usually the RPC).  So free the context here.
    6067             :                          */
    6068          20 :                         free_nvme_async_probe_ctx(ctx);
    6069             :                 }
    6070             :         }
    6071             : 
    6072          51 :         return SPDK_POLLER_BUSY;
    6073             : }
    6074             : 
    6075             : static bool
    6076          28 : bdev_nvme_check_io_error_resiliency_params(int32_t ctrlr_loss_timeout_sec,
    6077             :                 uint32_t reconnect_delay_sec,
    6078             :                 uint32_t fast_io_fail_timeout_sec)
    6079             : {
    6080          28 :         if (ctrlr_loss_timeout_sec < -1) {
    6081           1 :                 SPDK_ERRLOG("ctrlr_loss_timeout_sec can't be less than -1.\n");
    6082           1 :                 return false;
    6083          27 :         } else if (ctrlr_loss_timeout_sec == -1) {
    6084          13 :                 if (reconnect_delay_sec == 0) {
    6085           1 :                         SPDK_ERRLOG("reconnect_delay_sec can't be 0 if ctrlr_loss_timeout_sec is not 0.\n");
    6086           1 :                         return false;
    6087          12 :                 } else if (fast_io_fail_timeout_sec != 0 &&
    6088             :                            fast_io_fail_timeout_sec < reconnect_delay_sec) {
    6089           1 :                         SPDK_ERRLOG("reconnect_delay_sec can't be more than fast_io-fail_timeout_sec.\n");
    6090           1 :                         return false;
    6091             :                 }
    6092          14 :         } else if (ctrlr_loss_timeout_sec != 0) {
    6093          11 :                 if (reconnect_delay_sec == 0) {
    6094           1 :                         SPDK_ERRLOG("reconnect_delay_sec can't be 0 if ctrlr_loss_timeout_sec is not 0.\n");
    6095           1 :                         return false;
    6096          10 :                 } else if (reconnect_delay_sec > (uint32_t)ctrlr_loss_timeout_sec) {
    6097           1 :                         SPDK_ERRLOG("reconnect_delay_sec can't be more than ctrlr_loss_timeout_sec.\n");
    6098           1 :                         return false;
    6099           9 :                 } else if (fast_io_fail_timeout_sec != 0) {
    6100           6 :                         if (fast_io_fail_timeout_sec < reconnect_delay_sec) {
    6101           1 :                                 SPDK_ERRLOG("reconnect_delay_sec can't be more than fast_io_fail_timeout_sec.\n");
    6102           1 :                                 return false;
    6103           5 :                         } else if (fast_io_fail_timeout_sec > (uint32_t)ctrlr_loss_timeout_sec) {
    6104           1 :                                 SPDK_ERRLOG("fast_io_fail_timeout_sec can't be more than ctrlr_loss_timeout_sec.\n");
    6105           1 :                                 return false;
    6106             :                         }
    6107             :                 }
    6108           3 :         } else if (reconnect_delay_sec != 0 || fast_io_fail_timeout_sec != 0) {
    6109           2 :                 SPDK_ERRLOG("Both reconnect_delay_sec and fast_io_fail_timeout_sec must be 0 if ctrlr_loss_timeout_sec is 0.\n");
    6110           2 :                 return false;
    6111             :         }
    6112             : 
    6113          19 :         return true;
    6114             : }
    6115             : 
    6116             : static int
    6117           0 : bdev_nvme_load_psk(const char *fname, char *buf, size_t bufsz)
    6118             : {
    6119             :         FILE *psk_file;
    6120           0 :         struct stat statbuf;
    6121             :         int rc;
    6122             : #define TCP_PSK_INVALID_PERMISSIONS 0177
    6123             : 
    6124           0 :         if (stat(fname, &statbuf) != 0) {
    6125           0 :                 SPDK_ERRLOG("Could not read permissions for PSK file\n");
    6126           0 :                 return -EACCES;
    6127             :         }
    6128             : 
    6129           0 :         if ((statbuf.st_mode & TCP_PSK_INVALID_PERMISSIONS) != 0) {
    6130           0 :                 SPDK_ERRLOG("Incorrect permissions for PSK file\n");
    6131           0 :                 return -EPERM;
    6132             :         }
    6133           0 :         if ((size_t)statbuf.st_size >= bufsz) {
    6134           0 :                 SPDK_ERRLOG("Invalid PSK: too long\n");
    6135           0 :                 return -EINVAL;
    6136             :         }
    6137           0 :         psk_file = fopen(fname, "r");
    6138           0 :         if (psk_file == NULL) {
    6139           0 :                 SPDK_ERRLOG("Could not open PSK file\n");
    6140           0 :                 return -EINVAL;
    6141             :         }
    6142             : 
    6143           0 :         memset(buf, 0, bufsz);
    6144           0 :         rc = fread(buf, 1, statbuf.st_size, psk_file);
    6145           0 :         if (rc != statbuf.st_size) {
    6146           0 :                 SPDK_ERRLOG("Failed to read PSK\n");
    6147           0 :                 fclose(psk_file);
    6148           0 :                 return -EINVAL;
    6149             :         }
    6150             : 
    6151           0 :         fclose(psk_file);
    6152           0 :         return 0;
    6153             : }
    6154             : 
    6155             : int
    6156          51 : bdev_nvme_create(struct spdk_nvme_transport_id *trid,
    6157             :                  const char *base_name,
    6158             :                  const char **names,
    6159             :                  uint32_t count,
    6160             :                  spdk_bdev_create_nvme_fn cb_fn,
    6161             :                  void *cb_ctx,
    6162             :                  struct spdk_nvme_ctrlr_opts *drv_opts,
    6163             :                  struct nvme_ctrlr_opts *bdev_opts,
    6164             :                  bool multipath)
    6165             : {
    6166             :         struct nvme_probe_skip_entry *entry, *tmp;
    6167             :         struct nvme_async_probe_ctx *ctx;
    6168             :         spdk_nvme_attach_cb attach_cb;
    6169             :         int rc, len;
    6170             : 
    6171             :         /* TODO expand this check to include both the host and target TRIDs.
    6172             :          * Only if both are the same should we fail.
    6173             :          */
    6174          51 :         if (nvme_ctrlr_get(trid, drv_opts->hostnqn) != NULL) {
    6175           0 :                 SPDK_ERRLOG("A controller with the provided trid (traddr: %s, hostnqn: %s) "
    6176             :                             "already exists.\n", trid->traddr, drv_opts->hostnqn);
    6177           0 :                 return -EEXIST;
    6178             :         }
    6179             : 
    6180          51 :         len = strnlen(base_name, SPDK_CONTROLLER_NAME_MAX);
    6181             : 
    6182          51 :         if (len == 0 || len == SPDK_CONTROLLER_NAME_MAX) {
    6183           0 :                 SPDK_ERRLOG("controller name must be between 1 and %d characters\n", SPDK_CONTROLLER_NAME_MAX - 1);
    6184           0 :                 return -EINVAL;
    6185             :         }
    6186             : 
    6187          51 :         if (bdev_opts != NULL &&
    6188           9 :             !bdev_nvme_check_io_error_resiliency_params(bdev_opts->ctrlr_loss_timeout_sec,
    6189             :                             bdev_opts->reconnect_delay_sec,
    6190             :                             bdev_opts->fast_io_fail_timeout_sec)) {
    6191           0 :                 return -EINVAL;
    6192             :         }
    6193             : 
    6194          51 :         ctx = calloc(1, sizeof(*ctx));
    6195          51 :         if (!ctx) {
    6196           0 :                 return -ENOMEM;
    6197             :         }
    6198          51 :         ctx->base_name = base_name;
    6199          51 :         ctx->names = names;
    6200          51 :         ctx->max_bdevs = count;
    6201          51 :         ctx->cb_fn = cb_fn;
    6202          51 :         ctx->cb_ctx = cb_ctx;
    6203          51 :         ctx->trid = *trid;
    6204             : 
    6205          51 :         if (bdev_opts) {
    6206           9 :                 memcpy(&ctx->bdev_opts, bdev_opts, sizeof(*bdev_opts));
    6207             :         } else {
    6208          42 :                 bdev_nvme_get_default_ctrlr_opts(&ctx->bdev_opts);
    6209             :         }
    6210             : 
    6211          51 :         if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
    6212           0 :                 TAILQ_FOREACH_SAFE(entry, &g_skipped_nvme_ctrlrs, tailq, tmp) {
    6213           0 :                         if (spdk_nvme_transport_id_compare(trid, &entry->trid) == 0) {
    6214           0 :                                 TAILQ_REMOVE(&g_skipped_nvme_ctrlrs, entry, tailq);
    6215           0 :                                 free(entry);
    6216           0 :                                 break;
    6217             :                         }
    6218             :                 }
    6219             :         }
    6220             : 
    6221          51 :         memcpy(&ctx->drv_opts, drv_opts, sizeof(*drv_opts));
    6222          51 :         ctx->drv_opts.transport_retry_count = g_opts.transport_retry_count;
    6223          51 :         ctx->drv_opts.transport_ack_timeout = g_opts.transport_ack_timeout;
    6224          51 :         ctx->drv_opts.keep_alive_timeout_ms = g_opts.keep_alive_timeout_ms;
    6225          51 :         ctx->drv_opts.disable_read_ana_log_page = true;
    6226          51 :         ctx->drv_opts.transport_tos = g_opts.transport_tos;
    6227             : 
    6228          51 :         if (ctx->bdev_opts.psk[0] != '\0') {
    6229             :                 /* Try to use the keyring first */
    6230           0 :                 ctx->drv_opts.tls_psk = spdk_keyring_get_key(ctx->bdev_opts.psk);
    6231           0 :                 if (ctx->drv_opts.tls_psk == NULL) {
    6232           0 :                         rc = bdev_nvme_load_psk(ctx->bdev_opts.psk,
    6233           0 :                                                 ctx->drv_opts.psk, sizeof(ctx->drv_opts.psk));
    6234           0 :                         if (rc != 0) {
    6235           0 :                                 SPDK_ERRLOG("Could not load PSK from %s\n", ctx->bdev_opts.psk);
    6236           0 :                                 free_nvme_async_probe_ctx(ctx);
    6237           0 :                                 return rc;
    6238             :                         }
    6239             :                 }
    6240             :         }
    6241             : 
    6242          51 :         if (ctx->bdev_opts.dhchap_key != NULL) {
    6243           0 :                 ctx->drv_opts.dhchap_key = spdk_keyring_get_key(ctx->bdev_opts.dhchap_key);
    6244           0 :                 if (ctx->drv_opts.dhchap_key == NULL) {
    6245           0 :                         SPDK_ERRLOG("Could not load DH-HMAC-CHAP key: %s\n",
    6246             :                                     ctx->bdev_opts.dhchap_key);
    6247           0 :                         free_nvme_async_probe_ctx(ctx);
    6248           0 :                         return -ENOKEY;
    6249             :                 }
    6250             : 
    6251           0 :                 ctx->drv_opts.dhchap_digests = g_opts.dhchap_digests;
    6252           0 :                 ctx->drv_opts.dhchap_dhgroups = g_opts.dhchap_dhgroups;
    6253             :         }
    6254          51 :         if (ctx->bdev_opts.dhchap_ctrlr_key != NULL) {
    6255           0 :                 ctx->drv_opts.dhchap_ctrlr_key =
    6256           0 :                         spdk_keyring_get_key(ctx->bdev_opts.dhchap_ctrlr_key);
    6257           0 :                 if (ctx->drv_opts.dhchap_ctrlr_key == NULL) {
    6258           0 :                         SPDK_ERRLOG("Could not load DH-HMAC-CHAP controller key: %s\n",
    6259             :                                     ctx->bdev_opts.dhchap_ctrlr_key);
    6260           0 :                         free_nvme_async_probe_ctx(ctx);
    6261           0 :                         return -ENOKEY;
    6262             :                 }
    6263             :         }
    6264             : 
    6265          51 :         if (nvme_bdev_ctrlr_get_by_name(base_name) == NULL || multipath) {
    6266          47 :                 attach_cb = connect_attach_cb;
    6267             :         } else {
    6268           4 :                 attach_cb = connect_set_failover_cb;
    6269             :         }
    6270             : 
    6271          51 :         ctx->probe_ctx = spdk_nvme_connect_async(trid, &ctx->drv_opts, attach_cb);
    6272          51 :         if (ctx->probe_ctx == NULL) {
    6273           0 :                 SPDK_ERRLOG("No controller was found with provided trid (traddr: %s)\n", trid->traddr);
    6274           0 :                 free_nvme_async_probe_ctx(ctx);
    6275           0 :                 return -ENODEV;
    6276             :         }
    6277          51 :         ctx->poller = SPDK_POLLER_REGISTER(bdev_nvme_async_poll, ctx, 1000);
    6278             : 
    6279          51 :         return 0;
    6280             : }
    6281             : 
    6282             : struct bdev_nvme_delete_ctx {
    6283             :         char                        *name;
    6284             :         struct nvme_path_id         path_id;
    6285             :         bdev_nvme_delete_done_fn    delete_done;
    6286             :         void                        *delete_done_ctx;
    6287             :         uint64_t                    timeout_ticks;
    6288             :         struct spdk_poller          *poller;
    6289             : };
    6290             : 
    6291             : static void
    6292           2 : free_bdev_nvme_delete_ctx(struct bdev_nvme_delete_ctx *ctx)
    6293             : {
    6294           2 :         if (ctx != NULL) {
    6295           1 :                 free(ctx->name);
    6296           1 :                 free(ctx);
    6297             :         }
    6298           2 : }
    6299             : 
    6300             : static bool
    6301          74 : nvme_path_id_compare(struct nvme_path_id *p, const struct nvme_path_id *path_id)
    6302             : {
    6303          74 :         if (path_id->trid.trtype != 0) {
    6304          21 :                 if (path_id->trid.trtype == SPDK_NVME_TRANSPORT_CUSTOM) {
    6305           0 :                         if (strcasecmp(path_id->trid.trstring, p->trid.trstring) != 0) {
    6306           0 :                                 return false;
    6307             :                         }
    6308             :                 } else {
    6309          21 :                         if (path_id->trid.trtype != p->trid.trtype) {
    6310           0 :                                 return false;
    6311             :                         }
    6312             :                 }
    6313             :         }
    6314             : 
    6315          74 :         if (!spdk_mem_all_zero(path_id->trid.traddr, sizeof(path_id->trid.traddr))) {
    6316          21 :                 if (strcasecmp(path_id->trid.traddr, p->trid.traddr) != 0) {
    6317          11 :                         return false;
    6318             :                 }
    6319             :         }
    6320             : 
    6321          63 :         if (path_id->trid.adrfam != 0) {
    6322           0 :                 if (path_id->trid.adrfam != p->trid.adrfam) {
    6323           0 :                         return false;
    6324             :                 }
    6325             :         }
    6326             : 
    6327          63 :         if (!spdk_mem_all_zero(path_id->trid.trsvcid, sizeof(path_id->trid.trsvcid))) {
    6328          10 :                 if (strcasecmp(path_id->trid.trsvcid, p->trid.trsvcid) != 0) {
    6329           0 :                         return false;
    6330             :                 }
    6331             :         }
    6332             : 
    6333          63 :         if (!spdk_mem_all_zero(path_id->trid.subnqn, sizeof(path_id->trid.subnqn))) {
    6334          10 :                 if (strcmp(path_id->trid.subnqn, p->trid.subnqn) != 0) {
    6335           0 :                         return false;
    6336             :                 }
    6337             :         }
    6338             : 
    6339          63 :         if (!spdk_mem_all_zero(path_id->hostid.hostaddr, sizeof(path_id->hostid.hostaddr))) {
    6340           0 :                 if (strcmp(path_id->hostid.hostaddr, p->hostid.hostaddr) != 0) {
    6341           0 :                         return false;
    6342             :                 }
    6343             :         }
    6344             : 
    6345          63 :         if (!spdk_mem_all_zero(path_id->hostid.hostsvcid, sizeof(path_id->hostid.hostsvcid))) {
    6346           0 :                 if (strcmp(path_id->hostid.hostsvcid, p->hostid.hostsvcid) != 0) {
    6347           0 :                         return false;
    6348             :                 }
    6349             :         }
    6350             : 
    6351          63 :         return true;
    6352             : }
    6353             : 
    6354             : static bool
    6355           2 : nvme_path_id_exists(const char *name, const struct nvme_path_id *path_id)
    6356             : {
    6357             :         struct nvme_bdev_ctrlr  *nbdev_ctrlr;
    6358             :         struct nvme_ctrlr       *ctrlr;
    6359             :         struct nvme_path_id     *p;
    6360             : 
    6361           2 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    6362           2 :         nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
    6363           2 :         if (!nbdev_ctrlr) {
    6364           1 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6365           1 :                 return false;
    6366             :         }
    6367             : 
    6368           1 :         TAILQ_FOREACH(ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
    6369           1 :                 pthread_mutex_lock(&ctrlr->mutex);
    6370           1 :                 TAILQ_FOREACH(p, &ctrlr->trids, link) {
    6371           1 :                         if (nvme_path_id_compare(p, path_id)) {
    6372           1 :                                 pthread_mutex_unlock(&ctrlr->mutex);
    6373           1 :                                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6374           1 :                                 return true;
    6375             :                         }
    6376             :                 }
    6377           0 :                 pthread_mutex_unlock(&ctrlr->mutex);
    6378             :         }
    6379           0 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6380             : 
    6381           0 :         return false;
    6382             : }
    6383             : 
    6384             : static int
    6385           2 : bdev_nvme_delete_complete_poll(void *arg)
    6386             : {
    6387           2 :         struct bdev_nvme_delete_ctx     *ctx = arg;
    6388           2 :         int                             rc = 0;
    6389             : 
    6390           2 :         if (nvme_path_id_exists(ctx->name, &ctx->path_id)) {
    6391           1 :                 if (ctx->timeout_ticks > spdk_get_ticks()) {
    6392           1 :                         return SPDK_POLLER_BUSY;
    6393             :                 }
    6394             : 
    6395           0 :                 SPDK_ERRLOG("NVMe path '%s' still exists after delete\n", ctx->name);
    6396           0 :                 rc = -ETIMEDOUT;
    6397             :         }
    6398             : 
    6399           1 :         spdk_poller_unregister(&ctx->poller);
    6400             : 
    6401           1 :         ctx->delete_done(ctx->delete_done_ctx, rc);
    6402           1 :         free_bdev_nvme_delete_ctx(ctx);
    6403             : 
    6404           1 :         return SPDK_POLLER_BUSY;
    6405             : }
    6406             : 
    6407             : static int
    6408          63 : _bdev_nvme_delete(struct nvme_ctrlr *nvme_ctrlr, const struct nvme_path_id *path_id)
    6409             : {
    6410             :         struct nvme_path_id     *p, *t;
    6411             :         spdk_msg_fn             msg_fn;
    6412          63 :         int                     rc = -ENXIO;
    6413             : 
    6414          63 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    6415             : 
    6416          73 :         TAILQ_FOREACH_REVERSE_SAFE(p, &nvme_ctrlr->trids, nvme_paths, link, t) {
    6417          73 :                 if (p == TAILQ_FIRST(&nvme_ctrlr->trids)) {
    6418          63 :                         break;
    6419             :                 }
    6420             : 
    6421          10 :                 if (!nvme_path_id_compare(p, path_id)) {
    6422           3 :                         continue;
    6423             :                 }
    6424             : 
    6425             :                 /* We are not using the specified path. */
    6426           7 :                 TAILQ_REMOVE(&nvme_ctrlr->trids, p, link);
    6427           7 :                 free(p);
    6428           7 :                 rc = 0;
    6429             :         }
    6430             : 
    6431          63 :         if (p == NULL || !nvme_path_id_compare(p, path_id)) {
    6432           8 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    6433           8 :                 return rc;
    6434             :         }
    6435             : 
    6436             :         /* If we made it here, then this path is a match! Now we need to remove it. */
    6437             : 
    6438             :         /* This is the active path in use right now. The active path is always the first in the list. */
    6439          55 :         assert(p == nvme_ctrlr->active_path_id);
    6440             : 
    6441          55 :         if (!TAILQ_NEXT(p, link)) {
    6442             :                 /* The current path is the only path. */
    6443          54 :                 msg_fn = _nvme_ctrlr_destruct;
    6444          54 :                 rc = bdev_nvme_delete_ctrlr_unsafe(nvme_ctrlr, false);
    6445             :         } else {
    6446             :                 /* There is an alternative path. */
    6447           1 :                 msg_fn = _bdev_nvme_reset_ctrlr;
    6448           1 :                 rc = bdev_nvme_failover_ctrlr_unsafe(nvme_ctrlr, true);
    6449             :         }
    6450             : 
    6451          55 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    6452             : 
    6453          55 :         if (rc == 0) {
    6454          55 :                 spdk_thread_send_msg(nvme_ctrlr->thread, msg_fn, nvme_ctrlr);
    6455           0 :         } else if (rc == -EALREADY) {
    6456           0 :                 rc = 0;
    6457             :         }
    6458             : 
    6459          55 :         return rc;
    6460             : }
    6461             : 
    6462             : int
    6463          48 : bdev_nvme_delete(const char *name, const struct nvme_path_id *path_id,
    6464             :                  bdev_nvme_delete_done_fn delete_done, void *delete_done_ctx)
    6465             : {
    6466             :         struct nvme_bdev_ctrlr          *nbdev_ctrlr;
    6467             :         struct nvme_ctrlr               *nvme_ctrlr, *tmp_nvme_ctrlr;
    6468          48 :         struct bdev_nvme_delete_ctx     *ctx = NULL;
    6469          48 :         int                             rc = -ENXIO, _rc;
    6470             : 
    6471          48 :         if (name == NULL || path_id == NULL) {
    6472           0 :                 rc = -EINVAL;
    6473           0 :                 goto exit;
    6474             :         }
    6475             : 
    6476          48 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    6477             : 
    6478          48 :         nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
    6479          48 :         if (nbdev_ctrlr == NULL) {
    6480           0 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6481             : 
    6482           0 :                 SPDK_ERRLOG("Failed to find NVMe bdev controller\n");
    6483           0 :                 rc = -ENODEV;
    6484           0 :                 goto exit;
    6485             :         }
    6486             : 
    6487         111 :         TAILQ_FOREACH_SAFE(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq, tmp_nvme_ctrlr) {
    6488          63 :                 _rc = _bdev_nvme_delete(nvme_ctrlr, path_id);
    6489          63 :                 if (_rc < 0 && _rc != -ENXIO) {
    6490           0 :                         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6491           0 :                         rc = _rc;
    6492           0 :                         goto exit;
    6493          63 :                 } else if (_rc == 0) {
    6494             :                         /* We traverse all remaining nvme_ctrlrs even if one nvme_ctrlr
    6495             :                          * was deleted successfully. To remember the successful deletion,
    6496             :                          * overwrite rc only if _rc is zero.
    6497             :                          */
    6498          57 :                         rc = 0;
    6499             :                 }
    6500             :         }
    6501             : 
    6502          48 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6503             : 
    6504          48 :         if (rc != 0 || delete_done == NULL) {
    6505          47 :                 goto exit;
    6506             :         }
    6507             : 
    6508           1 :         ctx = calloc(1, sizeof(*ctx));
    6509           1 :         if (ctx == NULL) {
    6510           0 :                 SPDK_ERRLOG("Failed to allocate context for bdev_nvme_delete\n");
    6511           0 :                 rc = -ENOMEM;
    6512           0 :                 goto exit;
    6513             :         }
    6514             : 
    6515           1 :         ctx->name = strdup(name);
    6516           1 :         if (ctx->name == NULL) {
    6517           0 :                 SPDK_ERRLOG("Failed to copy controller name for deletion\n");
    6518           0 :                 rc = -ENOMEM;
    6519           0 :                 goto exit;
    6520             :         }
    6521             : 
    6522           1 :         ctx->delete_done = delete_done;
    6523           1 :         ctx->delete_done_ctx = delete_done_ctx;
    6524           1 :         ctx->path_id = *path_id;
    6525           1 :         ctx->timeout_ticks = spdk_get_ticks() + 10 * spdk_get_ticks_hz();
    6526           1 :         ctx->poller = SPDK_POLLER_REGISTER(bdev_nvme_delete_complete_poll, ctx, 1000);
    6527           1 :         if (ctx->poller == NULL) {
    6528           0 :                 SPDK_ERRLOG("Failed to register bdev_nvme_delete poller\n");
    6529           0 :                 rc = -ENOMEM;
    6530           0 :                 goto exit;
    6531             :         }
    6532             : 
    6533           1 : exit:
    6534          48 :         if (rc != 0) {
    6535           1 :                 free_bdev_nvme_delete_ctx(ctx);
    6536             :         }
    6537             : 
    6538          48 :         return rc;
    6539             : }
    6540             : 
    6541             : #define DISCOVERY_INFOLOG(ctx, format, ...) \
    6542             :         SPDK_INFOLOG(bdev_nvme, "Discovery[%s:%s] " format, ctx->trid.traddr, ctx->trid.trsvcid, ##__VA_ARGS__);
    6543             : 
    6544             : #define DISCOVERY_ERRLOG(ctx, format, ...) \
    6545             :         SPDK_ERRLOG("Discovery[%s:%s] " format, ctx->trid.traddr, ctx->trid.trsvcid, ##__VA_ARGS__);
    6546             : 
    6547             : struct discovery_entry_ctx {
    6548             :         char                                            name[128];
    6549             :         struct spdk_nvme_transport_id                   trid;
    6550             :         struct spdk_nvme_ctrlr_opts                     drv_opts;
    6551             :         struct spdk_nvmf_discovery_log_page_entry       entry;
    6552             :         TAILQ_ENTRY(discovery_entry_ctx)                tailq;
    6553             :         struct discovery_ctx                            *ctx;
    6554             : };
    6555             : 
    6556             : struct discovery_ctx {
    6557             :         char                                    *name;
    6558             :         spdk_bdev_nvme_start_discovery_fn       start_cb_fn;
    6559             :         spdk_bdev_nvme_stop_discovery_fn        stop_cb_fn;
    6560             :         void                                    *cb_ctx;
    6561             :         struct spdk_nvme_probe_ctx              *probe_ctx;
    6562             :         struct spdk_nvme_detach_ctx             *detach_ctx;
    6563             :         struct spdk_nvme_ctrlr                  *ctrlr;
    6564             :         struct spdk_nvme_transport_id           trid;
    6565             :         struct discovery_entry_ctx              *entry_ctx_in_use;
    6566             :         struct spdk_poller                      *poller;
    6567             :         struct spdk_nvme_ctrlr_opts             drv_opts;
    6568             :         struct nvme_ctrlr_opts                  bdev_opts;
    6569             :         struct spdk_nvmf_discovery_log_page     *log_page;
    6570             :         TAILQ_ENTRY(discovery_ctx)              tailq;
    6571             :         TAILQ_HEAD(, discovery_entry_ctx)       nvm_entry_ctxs;
    6572             :         TAILQ_HEAD(, discovery_entry_ctx)       discovery_entry_ctxs;
    6573             :         int                                     rc;
    6574             :         bool                                    wait_for_attach;
    6575             :         uint64_t                                timeout_ticks;
    6576             :         /* Denotes that the discovery service is being started. We're waiting
    6577             :          * for the initial connection to the discovery controller to be
    6578             :          * established and attach discovered NVM ctrlrs.
    6579             :          */
    6580             :         bool                                    initializing;
    6581             :         /* Denotes if a discovery is currently in progress for this context.
    6582             :          * That includes connecting to newly discovered subsystems.  Used to
    6583             :          * ensure we do not start a new discovery until an existing one is
    6584             :          * complete.
    6585             :          */
    6586             :         bool                                    in_progress;
    6587             : 
    6588             :         /* Denotes if another discovery is needed after the one in progress
    6589             :          * completes.  Set when we receive an AER completion while a discovery
    6590             :          * is already in progress.
    6591             :          */
    6592             :         bool                                    pending;
    6593             : 
    6594             :         /* Signal to the discovery context poller that it should stop the
    6595             :          * discovery service, including detaching from the current discovery
    6596             :          * controller.
    6597             :          */
    6598             :         bool                                    stop;
    6599             : 
    6600             :         struct spdk_thread                      *calling_thread;
    6601             :         uint32_t                                index;
    6602             :         uint32_t                                attach_in_progress;
    6603             :         char                                    *hostnqn;
    6604             : 
    6605             :         /* Denotes if the discovery service was started by the mdns discovery.
    6606             :          */
    6607             :         bool                                    from_mdns_discovery_service;
    6608             : };
    6609             : 
    6610             : TAILQ_HEAD(discovery_ctxs, discovery_ctx);
    6611             : static struct discovery_ctxs g_discovery_ctxs = TAILQ_HEAD_INITIALIZER(g_discovery_ctxs);
    6612             : 
    6613             : static void get_discovery_log_page(struct discovery_ctx *ctx);
    6614             : 
    6615             : static void
    6616           0 : free_discovery_ctx(struct discovery_ctx *ctx)
    6617             : {
    6618           0 :         free(ctx->log_page);
    6619           0 :         free(ctx->hostnqn);
    6620           0 :         free(ctx->name);
    6621           0 :         free(ctx);
    6622           0 : }
    6623             : 
    6624             : static void
    6625           0 : discovery_complete(struct discovery_ctx *ctx)
    6626             : {
    6627           0 :         ctx->initializing = false;
    6628           0 :         ctx->in_progress = false;
    6629           0 :         if (ctx->pending) {
    6630           0 :                 ctx->pending = false;
    6631           0 :                 get_discovery_log_page(ctx);
    6632             :         }
    6633           0 : }
    6634             : 
    6635             : static void
    6636           0 : build_trid_from_log_page_entry(struct spdk_nvme_transport_id *trid,
    6637             :                                struct spdk_nvmf_discovery_log_page_entry *entry)
    6638             : {
    6639             :         char *space;
    6640             : 
    6641           0 :         trid->trtype = entry->trtype;
    6642           0 :         trid->adrfam = entry->adrfam;
    6643           0 :         memcpy(trid->traddr, entry->traddr, sizeof(entry->traddr));
    6644           0 :         memcpy(trid->trsvcid, entry->trsvcid, sizeof(entry->trsvcid));
    6645             :         /* Because the source buffer (entry->subnqn) is longer than trid->subnqn, and
    6646             :          * before call to this function trid->subnqn is zeroed out, we need
    6647             :          * to copy sizeof(trid->subnqn) minus one byte to make sure the last character
    6648             :          * remains 0. Then we can shorten the string (replace ' ' with 0) if required
    6649             :          */
    6650           0 :         memcpy(trid->subnqn, entry->subnqn, sizeof(trid->subnqn) - 1);
    6651             : 
    6652             :         /* We want the traddr, trsvcid and subnqn fields to be NULL-terminated.
    6653             :          * But the log page entries typically pad them with spaces, not zeroes.
    6654             :          * So add a NULL terminator to each of these fields at the appropriate
    6655             :          * location.
    6656             :          */
    6657           0 :         space = strchr(trid->traddr, ' ');
    6658           0 :         if (space) {
    6659           0 :                 *space = 0;
    6660             :         }
    6661           0 :         space = strchr(trid->trsvcid, ' ');
    6662           0 :         if (space) {
    6663           0 :                 *space = 0;
    6664             :         }
    6665           0 :         space = strchr(trid->subnqn, ' ');
    6666           0 :         if (space) {
    6667           0 :                 *space = 0;
    6668             :         }
    6669           0 : }
    6670             : 
    6671             : static void
    6672           0 : _stop_discovery(void *_ctx)
    6673             : {
    6674           0 :         struct discovery_ctx *ctx = _ctx;
    6675             : 
    6676           0 :         if (ctx->attach_in_progress > 0) {
    6677           0 :                 spdk_thread_send_msg(spdk_get_thread(), _stop_discovery, ctx);
    6678           0 :                 return;
    6679             :         }
    6680             : 
    6681           0 :         ctx->stop = true;
    6682             : 
    6683           0 :         while (!TAILQ_EMPTY(&ctx->nvm_entry_ctxs)) {
    6684             :                 struct discovery_entry_ctx *entry_ctx;
    6685           0 :                 struct nvme_path_id path = {};
    6686             : 
    6687           0 :                 entry_ctx = TAILQ_FIRST(&ctx->nvm_entry_ctxs);
    6688           0 :                 path.trid = entry_ctx->trid;
    6689           0 :                 bdev_nvme_delete(entry_ctx->name, &path, NULL, NULL);
    6690           0 :                 TAILQ_REMOVE(&ctx->nvm_entry_ctxs, entry_ctx, tailq);
    6691           0 :                 free(entry_ctx);
    6692             :         }
    6693             : 
    6694           0 :         while (!TAILQ_EMPTY(&ctx->discovery_entry_ctxs)) {
    6695             :                 struct discovery_entry_ctx *entry_ctx;
    6696             : 
    6697           0 :                 entry_ctx = TAILQ_FIRST(&ctx->discovery_entry_ctxs);
    6698           0 :                 TAILQ_REMOVE(&ctx->discovery_entry_ctxs, entry_ctx, tailq);
    6699           0 :                 free(entry_ctx);
    6700             :         }
    6701             : 
    6702           0 :         free(ctx->entry_ctx_in_use);
    6703           0 :         ctx->entry_ctx_in_use = NULL;
    6704             : }
    6705             : 
    6706             : static void
    6707           0 : stop_discovery(struct discovery_ctx *ctx, spdk_bdev_nvme_stop_discovery_fn cb_fn, void *cb_ctx)
    6708             : {
    6709           0 :         ctx->stop_cb_fn = cb_fn;
    6710           0 :         ctx->cb_ctx = cb_ctx;
    6711             : 
    6712           0 :         if (ctx->attach_in_progress > 0) {
    6713           0 :                 DISCOVERY_INFOLOG(ctx, "stopping discovery with attach_in_progress: %"PRIu32"\n",
    6714             :                                   ctx->attach_in_progress);
    6715             :         }
    6716             : 
    6717           0 :         _stop_discovery(ctx);
    6718           0 : }
    6719             : 
    6720             : static void
    6721           2 : remove_discovery_entry(struct nvme_ctrlr *nvme_ctrlr)
    6722             : {
    6723             :         struct discovery_ctx *d_ctx;
    6724             :         struct nvme_path_id *path_id;
    6725           2 :         struct spdk_nvme_transport_id trid = {};
    6726             :         struct discovery_entry_ctx *entry_ctx, *tmp;
    6727             : 
    6728           2 :         path_id = TAILQ_FIRST(&nvme_ctrlr->trids);
    6729             : 
    6730           2 :         TAILQ_FOREACH(d_ctx, &g_discovery_ctxs, tailq) {
    6731           0 :                 TAILQ_FOREACH_SAFE(entry_ctx, &d_ctx->nvm_entry_ctxs, tailq, tmp) {
    6732           0 :                         build_trid_from_log_page_entry(&trid, &entry_ctx->entry);
    6733           0 :                         if (spdk_nvme_transport_id_compare(&trid, &path_id->trid) != 0) {
    6734           0 :                                 continue;
    6735             :                         }
    6736             : 
    6737           0 :                         TAILQ_REMOVE(&d_ctx->nvm_entry_ctxs, entry_ctx, tailq);
    6738           0 :                         free(entry_ctx);
    6739           0 :                         DISCOVERY_INFOLOG(d_ctx, "Remove discovery entry: %s:%s:%s\n",
    6740             :                                           trid.subnqn, trid.traddr, trid.trsvcid);
    6741             : 
    6742             :                         /* Fail discovery ctrlr to force reattach attempt */
    6743           0 :                         spdk_nvme_ctrlr_fail(d_ctx->ctrlr);
    6744             :                 }
    6745             :         }
    6746           2 : }
    6747             : 
    6748             : static void
    6749           0 : discovery_remove_controllers(struct discovery_ctx *ctx)
    6750             : {
    6751           0 :         struct spdk_nvmf_discovery_log_page *log_page = ctx->log_page;
    6752             :         struct discovery_entry_ctx *entry_ctx, *tmp;
    6753             :         struct spdk_nvmf_discovery_log_page_entry *new_entry, *old_entry;
    6754           0 :         struct spdk_nvme_transport_id old_trid = {};
    6755             :         uint64_t numrec, i;
    6756             :         bool found;
    6757             : 
    6758           0 :         numrec = from_le64(&log_page->numrec);
    6759           0 :         TAILQ_FOREACH_SAFE(entry_ctx, &ctx->nvm_entry_ctxs, tailq, tmp) {
    6760           0 :                 found = false;
    6761           0 :                 old_entry = &entry_ctx->entry;
    6762           0 :                 build_trid_from_log_page_entry(&old_trid, old_entry);
    6763           0 :                 for (i = 0; i < numrec; i++) {
    6764           0 :                         new_entry = &log_page->entries[i];
    6765           0 :                         if (!memcmp(old_entry, new_entry, sizeof(*old_entry))) {
    6766           0 :                                 DISCOVERY_INFOLOG(ctx, "NVM %s:%s:%s found again\n",
    6767             :                                                   old_trid.subnqn, old_trid.traddr, old_trid.trsvcid);
    6768           0 :                                 found = true;
    6769           0 :                                 break;
    6770             :                         }
    6771             :                 }
    6772           0 :                 if (!found) {
    6773           0 :                         struct nvme_path_id path = {};
    6774             : 
    6775           0 :                         DISCOVERY_INFOLOG(ctx, "NVM %s:%s:%s not found\n",
    6776             :                                           old_trid.subnqn, old_trid.traddr, old_trid.trsvcid);
    6777             : 
    6778           0 :                         path.trid = entry_ctx->trid;
    6779           0 :                         bdev_nvme_delete(entry_ctx->name, &path, NULL, NULL);
    6780           0 :                         TAILQ_REMOVE(&ctx->nvm_entry_ctxs, entry_ctx, tailq);
    6781           0 :                         free(entry_ctx);
    6782             :                 }
    6783             :         }
    6784           0 :         free(log_page);
    6785           0 :         ctx->log_page = NULL;
    6786           0 :         discovery_complete(ctx);
    6787           0 : }
    6788             : 
    6789             : static void
    6790           0 : complete_discovery_start(struct discovery_ctx *ctx, int status)
    6791             : {
    6792           0 :         ctx->timeout_ticks = 0;
    6793           0 :         ctx->rc = status;
    6794           0 :         if (ctx->start_cb_fn) {
    6795           0 :                 ctx->start_cb_fn(ctx->cb_ctx, status);
    6796           0 :                 ctx->start_cb_fn = NULL;
    6797           0 :                 ctx->cb_ctx = NULL;
    6798             :         }
    6799           0 : }
    6800             : 
    6801             : static void
    6802           0 : discovery_attach_controller_done(void *cb_ctx, size_t bdev_count, int rc)
    6803             : {
    6804           0 :         struct discovery_entry_ctx *entry_ctx = cb_ctx;
    6805           0 :         struct discovery_ctx *ctx = entry_ctx->ctx;
    6806             : 
    6807           0 :         DISCOVERY_INFOLOG(ctx, "attach %s done\n", entry_ctx->name);
    6808           0 :         ctx->attach_in_progress--;
    6809           0 :         if (ctx->attach_in_progress == 0) {
    6810           0 :                 complete_discovery_start(ctx, ctx->rc);
    6811           0 :                 if (ctx->initializing && ctx->rc != 0) {
    6812           0 :                         DISCOVERY_ERRLOG(ctx, "stopping discovery due to errors: %d\n", ctx->rc);
    6813           0 :                         stop_discovery(ctx, NULL, ctx->cb_ctx);
    6814             :                 } else {
    6815           0 :                         discovery_remove_controllers(ctx);
    6816             :                 }
    6817             :         }
    6818           0 : }
    6819             : 
    6820             : static struct discovery_entry_ctx *
    6821           0 : create_discovery_entry_ctx(struct discovery_ctx *ctx, struct spdk_nvme_transport_id *trid)
    6822             : {
    6823             :         struct discovery_entry_ctx *new_ctx;
    6824             : 
    6825           0 :         new_ctx = calloc(1, sizeof(*new_ctx));
    6826           0 :         if (new_ctx == NULL) {
    6827           0 :                 DISCOVERY_ERRLOG(ctx, "could not allocate new entry_ctx\n");
    6828           0 :                 return NULL;
    6829             :         }
    6830             : 
    6831           0 :         new_ctx->ctx = ctx;
    6832           0 :         memcpy(&new_ctx->trid, trid, sizeof(*trid));
    6833           0 :         spdk_nvme_ctrlr_get_default_ctrlr_opts(&new_ctx->drv_opts, sizeof(new_ctx->drv_opts));
    6834           0 :         snprintf(new_ctx->drv_opts.hostnqn, sizeof(new_ctx->drv_opts.hostnqn), "%s", ctx->hostnqn);
    6835           0 :         return new_ctx;
    6836             : }
    6837             : 
    6838             : static void
    6839           0 : discovery_log_page_cb(void *cb_arg, int rc, const struct spdk_nvme_cpl *cpl,
    6840             :                       struct spdk_nvmf_discovery_log_page *log_page)
    6841             : {
    6842           0 :         struct discovery_ctx *ctx = cb_arg;
    6843             :         struct discovery_entry_ctx *entry_ctx, *tmp;
    6844             :         struct spdk_nvmf_discovery_log_page_entry *new_entry, *old_entry;
    6845             :         uint64_t numrec, i;
    6846             :         bool found;
    6847             : 
    6848           0 :         if (rc || spdk_nvme_cpl_is_error(cpl)) {
    6849           0 :                 DISCOVERY_ERRLOG(ctx, "could not get discovery log page\n");
    6850           0 :                 return;
    6851             :         }
    6852             : 
    6853           0 :         ctx->log_page = log_page;
    6854           0 :         assert(ctx->attach_in_progress == 0);
    6855           0 :         numrec = from_le64(&log_page->numrec);
    6856           0 :         TAILQ_FOREACH_SAFE(entry_ctx, &ctx->discovery_entry_ctxs, tailq, tmp) {
    6857           0 :                 TAILQ_REMOVE(&ctx->discovery_entry_ctxs, entry_ctx, tailq);
    6858           0 :                 free(entry_ctx);
    6859             :         }
    6860           0 :         for (i = 0; i < numrec; i++) {
    6861           0 :                 found = false;
    6862           0 :                 new_entry = &log_page->entries[i];
    6863           0 :                 if (new_entry->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY_CURRENT ||
    6864           0 :                     new_entry->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
    6865             :                         struct discovery_entry_ctx *new_ctx;
    6866           0 :                         struct spdk_nvme_transport_id trid = {};
    6867             : 
    6868           0 :                         build_trid_from_log_page_entry(&trid, new_entry);
    6869           0 :                         new_ctx = create_discovery_entry_ctx(ctx, &trid);
    6870           0 :                         if (new_ctx == NULL) {
    6871           0 :                                 DISCOVERY_ERRLOG(ctx, "could not allocate new entry_ctx\n");
    6872           0 :                                 break;
    6873             :                         }
    6874             : 
    6875           0 :                         TAILQ_INSERT_TAIL(&ctx->discovery_entry_ctxs, new_ctx, tailq);
    6876           0 :                         continue;
    6877             :                 }
    6878           0 :                 TAILQ_FOREACH(entry_ctx, &ctx->nvm_entry_ctxs, tailq) {
    6879           0 :                         old_entry = &entry_ctx->entry;
    6880           0 :                         if (!memcmp(new_entry, old_entry, sizeof(*new_entry))) {
    6881           0 :                                 found = true;
    6882           0 :                                 break;
    6883             :                         }
    6884             :                 }
    6885           0 :                 if (!found) {
    6886           0 :                         struct discovery_entry_ctx *subnqn_ctx = NULL, *new_ctx;
    6887             :                         struct discovery_ctx *d_ctx;
    6888             : 
    6889           0 :                         TAILQ_FOREACH(d_ctx, &g_discovery_ctxs, tailq) {
    6890           0 :                                 TAILQ_FOREACH(subnqn_ctx, &d_ctx->nvm_entry_ctxs, tailq) {
    6891           0 :                                         if (!memcmp(subnqn_ctx->entry.subnqn, new_entry->subnqn,
    6892             :                                                     sizeof(new_entry->subnqn))) {
    6893           0 :                                                 break;
    6894             :                                         }
    6895             :                                 }
    6896           0 :                                 if (subnqn_ctx) {
    6897           0 :                                         break;
    6898             :                                 }
    6899             :                         }
    6900             : 
    6901           0 :                         new_ctx = calloc(1, sizeof(*new_ctx));
    6902           0 :                         if (new_ctx == NULL) {
    6903           0 :                                 DISCOVERY_ERRLOG(ctx, "could not allocate new entry_ctx\n");
    6904           0 :                                 break;
    6905             :                         }
    6906             : 
    6907           0 :                         new_ctx->ctx = ctx;
    6908           0 :                         memcpy(&new_ctx->entry, new_entry, sizeof(*new_entry));
    6909           0 :                         build_trid_from_log_page_entry(&new_ctx->trid, new_entry);
    6910           0 :                         if (subnqn_ctx) {
    6911           0 :                                 snprintf(new_ctx->name, sizeof(new_ctx->name), "%s", subnqn_ctx->name);
    6912           0 :                                 DISCOVERY_INFOLOG(ctx, "NVM %s:%s:%s new path for %s\n",
    6913             :                                                   new_ctx->trid.subnqn, new_ctx->trid.traddr, new_ctx->trid.trsvcid,
    6914             :                                                   new_ctx->name);
    6915             :                         } else {
    6916           0 :                                 snprintf(new_ctx->name, sizeof(new_ctx->name), "%s%d", ctx->name, ctx->index++);
    6917           0 :                                 DISCOVERY_INFOLOG(ctx, "NVM %s:%s:%s new subsystem %s\n",
    6918             :                                                   new_ctx->trid.subnqn, new_ctx->trid.traddr, new_ctx->trid.trsvcid,
    6919             :                                                   new_ctx->name);
    6920             :                         }
    6921           0 :                         spdk_nvme_ctrlr_get_default_ctrlr_opts(&new_ctx->drv_opts, sizeof(new_ctx->drv_opts));
    6922           0 :                         snprintf(new_ctx->drv_opts.hostnqn, sizeof(new_ctx->drv_opts.hostnqn), "%s", ctx->hostnqn);
    6923           0 :                         rc = bdev_nvme_create(&new_ctx->trid, new_ctx->name, NULL, 0,
    6924             :                                               discovery_attach_controller_done, new_ctx,
    6925             :                                               &new_ctx->drv_opts, &ctx->bdev_opts, true);
    6926           0 :                         if (rc == 0) {
    6927           0 :                                 TAILQ_INSERT_TAIL(&ctx->nvm_entry_ctxs, new_ctx, tailq);
    6928           0 :                                 ctx->attach_in_progress++;
    6929             :                         } else {
    6930           0 :                                 DISCOVERY_ERRLOG(ctx, "bdev_nvme_create failed (%s)\n", spdk_strerror(-rc));
    6931             :                         }
    6932             :                 }
    6933             :         }
    6934             : 
    6935           0 :         if (ctx->attach_in_progress == 0) {
    6936           0 :                 discovery_remove_controllers(ctx);
    6937             :         }
    6938             : }
    6939             : 
    6940             : static void
    6941           0 : get_discovery_log_page(struct discovery_ctx *ctx)
    6942             : {
    6943             :         int rc;
    6944             : 
    6945           0 :         assert(ctx->in_progress == false);
    6946           0 :         ctx->in_progress = true;
    6947           0 :         rc = spdk_nvme_ctrlr_get_discovery_log_page(ctx->ctrlr, discovery_log_page_cb, ctx);
    6948           0 :         if (rc != 0) {
    6949           0 :                 DISCOVERY_ERRLOG(ctx, "could not get discovery log page\n");
    6950             :         }
    6951           0 :         DISCOVERY_INFOLOG(ctx, "sent discovery log page command\n");
    6952           0 : }
    6953             : 
    6954             : static void
    6955           0 : discovery_aer_cb(void *arg, const struct spdk_nvme_cpl *cpl)
    6956             : {
    6957           0 :         struct discovery_ctx *ctx = arg;
    6958           0 :         uint32_t log_page_id = (cpl->cdw0 & 0xFF0000) >> 16;
    6959             : 
    6960           0 :         if (spdk_nvme_cpl_is_error(cpl)) {
    6961           0 :                 DISCOVERY_ERRLOG(ctx, "aer failed\n");
    6962           0 :                 return;
    6963             :         }
    6964             : 
    6965           0 :         if (log_page_id != SPDK_NVME_LOG_DISCOVERY) {
    6966           0 :                 DISCOVERY_ERRLOG(ctx, "unexpected log page 0x%x\n", log_page_id);
    6967           0 :                 return;
    6968             :         }
    6969             : 
    6970           0 :         DISCOVERY_INFOLOG(ctx, "got aer\n");
    6971           0 :         if (ctx->in_progress) {
    6972           0 :                 ctx->pending = true;
    6973           0 :                 return;
    6974             :         }
    6975             : 
    6976           0 :         get_discovery_log_page(ctx);
    6977             : }
    6978             : 
    6979             : static void
    6980           0 : discovery_attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
    6981             :                     struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
    6982             : {
    6983           0 :         struct spdk_nvme_ctrlr_opts *user_opts = cb_ctx;
    6984             :         struct discovery_ctx *ctx;
    6985             : 
    6986           0 :         ctx = SPDK_CONTAINEROF(user_opts, struct discovery_ctx, drv_opts);
    6987             : 
    6988           0 :         DISCOVERY_INFOLOG(ctx, "discovery ctrlr attached\n");
    6989           0 :         ctx->probe_ctx = NULL;
    6990           0 :         ctx->ctrlr = ctrlr;
    6991             : 
    6992           0 :         if (ctx->rc != 0) {
    6993           0 :                 DISCOVERY_ERRLOG(ctx, "encountered error while attaching discovery ctrlr: %d\n",
    6994             :                                  ctx->rc);
    6995           0 :                 return;
    6996             :         }
    6997             : 
    6998           0 :         spdk_nvme_ctrlr_register_aer_callback(ctx->ctrlr, discovery_aer_cb, ctx);
    6999             : }
    7000             : 
    7001             : static int
    7002           0 : discovery_poller(void *arg)
    7003             : {
    7004           0 :         struct discovery_ctx *ctx = arg;
    7005             :         struct spdk_nvme_transport_id *trid;
    7006             :         int rc;
    7007             : 
    7008           0 :         if (ctx->detach_ctx) {
    7009           0 :                 rc = spdk_nvme_detach_poll_async(ctx->detach_ctx);
    7010           0 :                 if (rc != -EAGAIN) {
    7011           0 :                         ctx->detach_ctx = NULL;
    7012           0 :                         ctx->ctrlr = NULL;
    7013             :                 }
    7014           0 :         } else if (ctx->stop) {
    7015           0 :                 if (ctx->ctrlr != NULL) {
    7016           0 :                         rc = spdk_nvme_detach_async(ctx->ctrlr, &ctx->detach_ctx);
    7017           0 :                         if (rc == 0) {
    7018           0 :                                 return SPDK_POLLER_BUSY;
    7019             :                         }
    7020           0 :                         DISCOVERY_ERRLOG(ctx, "could not detach discovery ctrlr\n");
    7021             :                 }
    7022           0 :                 spdk_poller_unregister(&ctx->poller);
    7023           0 :                 TAILQ_REMOVE(&g_discovery_ctxs, ctx, tailq);
    7024           0 :                 assert(ctx->start_cb_fn == NULL);
    7025           0 :                 if (ctx->stop_cb_fn != NULL) {
    7026           0 :                         ctx->stop_cb_fn(ctx->cb_ctx);
    7027             :                 }
    7028           0 :                 free_discovery_ctx(ctx);
    7029           0 :         } else if (ctx->probe_ctx == NULL && ctx->ctrlr == NULL) {
    7030           0 :                 if (ctx->timeout_ticks != 0 && ctx->timeout_ticks < spdk_get_ticks()) {
    7031           0 :                         DISCOVERY_ERRLOG(ctx, "timed out while attaching discovery ctrlr\n");
    7032           0 :                         assert(ctx->initializing);
    7033           0 :                         spdk_poller_unregister(&ctx->poller);
    7034           0 :                         TAILQ_REMOVE(&g_discovery_ctxs, ctx, tailq);
    7035           0 :                         complete_discovery_start(ctx, -ETIMEDOUT);
    7036           0 :                         stop_discovery(ctx, NULL, NULL);
    7037           0 :                         free_discovery_ctx(ctx);
    7038           0 :                         return SPDK_POLLER_BUSY;
    7039             :                 }
    7040             : 
    7041           0 :                 assert(ctx->entry_ctx_in_use == NULL);
    7042           0 :                 ctx->entry_ctx_in_use = TAILQ_FIRST(&ctx->discovery_entry_ctxs);
    7043           0 :                 TAILQ_REMOVE(&ctx->discovery_entry_ctxs, ctx->entry_ctx_in_use, tailq);
    7044           0 :                 trid = &ctx->entry_ctx_in_use->trid;
    7045           0 :                 ctx->probe_ctx = spdk_nvme_connect_async(trid, &ctx->drv_opts, discovery_attach_cb);
    7046           0 :                 if (ctx->probe_ctx) {
    7047           0 :                         spdk_poller_unregister(&ctx->poller);
    7048           0 :                         ctx->poller = SPDK_POLLER_REGISTER(discovery_poller, ctx, 1000);
    7049             :                 } else {
    7050           0 :                         DISCOVERY_ERRLOG(ctx, "could not start discovery connect\n");
    7051           0 :                         TAILQ_INSERT_TAIL(&ctx->discovery_entry_ctxs, ctx->entry_ctx_in_use, tailq);
    7052           0 :                         ctx->entry_ctx_in_use = NULL;
    7053             :                 }
    7054           0 :         } else if (ctx->probe_ctx) {
    7055           0 :                 if (ctx->timeout_ticks != 0 && ctx->timeout_ticks < spdk_get_ticks()) {
    7056           0 :                         DISCOVERY_ERRLOG(ctx, "timed out while attaching discovery ctrlr\n");
    7057           0 :                         complete_discovery_start(ctx, -ETIMEDOUT);
    7058           0 :                         return SPDK_POLLER_BUSY;
    7059             :                 }
    7060             : 
    7061           0 :                 rc = spdk_nvme_probe_poll_async(ctx->probe_ctx);
    7062           0 :                 if (rc != -EAGAIN) {
    7063           0 :                         if (ctx->rc != 0) {
    7064           0 :                                 assert(ctx->initializing);
    7065           0 :                                 stop_discovery(ctx, NULL, ctx->cb_ctx);
    7066             :                         } else {
    7067           0 :                                 assert(rc == 0);
    7068           0 :                                 DISCOVERY_INFOLOG(ctx, "discovery ctrlr connected\n");
    7069           0 :                                 ctx->rc = rc;
    7070           0 :                                 get_discovery_log_page(ctx);
    7071             :                         }
    7072             :                 }
    7073             :         } else {
    7074           0 :                 if (ctx->timeout_ticks != 0 && ctx->timeout_ticks < spdk_get_ticks()) {
    7075           0 :                         DISCOVERY_ERRLOG(ctx, "timed out while attaching NVM ctrlrs\n");
    7076           0 :                         complete_discovery_start(ctx, -ETIMEDOUT);
    7077             :                         /* We need to wait until all NVM ctrlrs are attached before we stop the
    7078             :                          * discovery service to make sure we don't detach a ctrlr that is still
    7079             :                          * being attached.
    7080             :                          */
    7081           0 :                         if (ctx->attach_in_progress == 0) {
    7082           0 :                                 stop_discovery(ctx, NULL, ctx->cb_ctx);
    7083           0 :                                 return SPDK_POLLER_BUSY;
    7084             :                         }
    7085             :                 }
    7086             : 
    7087           0 :                 rc = spdk_nvme_ctrlr_process_admin_completions(ctx->ctrlr);
    7088           0 :                 if (rc < 0) {
    7089           0 :                         spdk_poller_unregister(&ctx->poller);
    7090           0 :                         ctx->poller = SPDK_POLLER_REGISTER(discovery_poller, ctx, 1000 * 1000);
    7091           0 :                         TAILQ_INSERT_TAIL(&ctx->discovery_entry_ctxs, ctx->entry_ctx_in_use, tailq);
    7092           0 :                         ctx->entry_ctx_in_use = NULL;
    7093             : 
    7094           0 :                         rc = spdk_nvme_detach_async(ctx->ctrlr, &ctx->detach_ctx);
    7095           0 :                         if (rc != 0) {
    7096           0 :                                 DISCOVERY_ERRLOG(ctx, "could not detach discovery ctrlr\n");
    7097           0 :                                 ctx->ctrlr = NULL;
    7098             :                         }
    7099             :                 }
    7100             :         }
    7101             : 
    7102           0 :         return SPDK_POLLER_BUSY;
    7103             : }
    7104             : 
    7105             : static void
    7106           0 : start_discovery_poller(void *arg)
    7107             : {
    7108           0 :         struct discovery_ctx *ctx = arg;
    7109             : 
    7110           0 :         TAILQ_INSERT_TAIL(&g_discovery_ctxs, ctx, tailq);
    7111           0 :         ctx->poller = SPDK_POLLER_REGISTER(discovery_poller, ctx, 1000 * 1000);
    7112           0 : }
    7113             : 
    7114             : int
    7115           0 : bdev_nvme_start_discovery(struct spdk_nvme_transport_id *trid,
    7116             :                           const char *base_name,
    7117             :                           struct spdk_nvme_ctrlr_opts *drv_opts,
    7118             :                           struct nvme_ctrlr_opts *bdev_opts,
    7119             :                           uint64_t attach_timeout,
    7120             :                           bool from_mdns,
    7121             :                           spdk_bdev_nvme_start_discovery_fn cb_fn, void *cb_ctx)
    7122             : {
    7123             :         struct discovery_ctx *ctx;
    7124             :         struct discovery_entry_ctx *discovery_entry_ctx;
    7125             : 
    7126           0 :         snprintf(trid->subnqn, sizeof(trid->subnqn), "%s", SPDK_NVMF_DISCOVERY_NQN);
    7127           0 :         TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
    7128           0 :                 if (strcmp(ctx->name, base_name) == 0) {
    7129           0 :                         return -EEXIST;
    7130             :                 }
    7131             : 
    7132           0 :                 if (ctx->entry_ctx_in_use != NULL) {
    7133           0 :                         if (!spdk_nvme_transport_id_compare(trid, &ctx->entry_ctx_in_use->trid)) {
    7134           0 :                                 return -EEXIST;
    7135             :                         }
    7136             :                 }
    7137             : 
    7138           0 :                 TAILQ_FOREACH(discovery_entry_ctx, &ctx->discovery_entry_ctxs, tailq) {
    7139           0 :                         if (!spdk_nvme_transport_id_compare(trid, &discovery_entry_ctx->trid)) {
    7140           0 :                                 return -EEXIST;
    7141             :                         }
    7142             :                 }
    7143             :         }
    7144             : 
    7145           0 :         ctx = calloc(1, sizeof(*ctx));
    7146           0 :         if (ctx == NULL) {
    7147           0 :                 return -ENOMEM;
    7148             :         }
    7149             : 
    7150           0 :         ctx->name = strdup(base_name);
    7151           0 :         if (ctx->name == NULL) {
    7152           0 :                 free_discovery_ctx(ctx);
    7153           0 :                 return -ENOMEM;
    7154             :         }
    7155           0 :         memcpy(&ctx->drv_opts, drv_opts, sizeof(*drv_opts));
    7156           0 :         memcpy(&ctx->bdev_opts, bdev_opts, sizeof(*bdev_opts));
    7157           0 :         ctx->from_mdns_discovery_service = from_mdns;
    7158           0 :         ctx->bdev_opts.from_discovery_service = true;
    7159           0 :         ctx->calling_thread = spdk_get_thread();
    7160           0 :         ctx->start_cb_fn = cb_fn;
    7161           0 :         ctx->cb_ctx = cb_ctx;
    7162           0 :         ctx->initializing = true;
    7163           0 :         if (ctx->start_cb_fn) {
    7164             :                 /* We can use this when dumping json to denote if this RPC parameter
    7165             :                  * was specified or not.
    7166             :                  */
    7167           0 :                 ctx->wait_for_attach = true;
    7168             :         }
    7169           0 :         if (attach_timeout != 0) {
    7170           0 :                 ctx->timeout_ticks = spdk_get_ticks() + attach_timeout *
    7171           0 :                                      spdk_get_ticks_hz() / 1000ull;
    7172             :         }
    7173           0 :         TAILQ_INIT(&ctx->nvm_entry_ctxs);
    7174           0 :         TAILQ_INIT(&ctx->discovery_entry_ctxs);
    7175           0 :         memcpy(&ctx->trid, trid, sizeof(*trid));
    7176             :         /* Even if user did not specify hostnqn, we can still strdup("\0"); */
    7177           0 :         ctx->hostnqn = strdup(ctx->drv_opts.hostnqn);
    7178           0 :         if (ctx->hostnqn == NULL) {
    7179           0 :                 free_discovery_ctx(ctx);
    7180           0 :                 return -ENOMEM;
    7181             :         }
    7182           0 :         discovery_entry_ctx = create_discovery_entry_ctx(ctx, trid);
    7183           0 :         if (discovery_entry_ctx == NULL) {
    7184           0 :                 DISCOVERY_ERRLOG(ctx, "could not allocate new entry_ctx\n");
    7185           0 :                 free_discovery_ctx(ctx);
    7186           0 :                 return -ENOMEM;
    7187             :         }
    7188             : 
    7189           0 :         TAILQ_INSERT_TAIL(&ctx->discovery_entry_ctxs, discovery_entry_ctx, tailq);
    7190           0 :         spdk_thread_send_msg(g_bdev_nvme_init_thread, start_discovery_poller, ctx);
    7191           0 :         return 0;
    7192             : }
    7193             : 
    7194             : int
    7195           0 : bdev_nvme_stop_discovery(const char *name, spdk_bdev_nvme_stop_discovery_fn cb_fn, void *cb_ctx)
    7196             : {
    7197             :         struct discovery_ctx *ctx;
    7198             : 
    7199           0 :         TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
    7200           0 :                 if (strcmp(name, ctx->name) == 0) {
    7201           0 :                         if (ctx->stop) {
    7202           0 :                                 return -EALREADY;
    7203             :                         }
    7204             :                         /* If we're still starting the discovery service and ->rc is non-zero, we're
    7205             :                          * going to stop it as soon as we can
    7206             :                          */
    7207           0 :                         if (ctx->initializing && ctx->rc != 0) {
    7208           0 :                                 return -EALREADY;
    7209             :                         }
    7210           0 :                         stop_discovery(ctx, cb_fn, cb_ctx);
    7211           0 :                         return 0;
    7212             :                 }
    7213             :         }
    7214             : 
    7215           0 :         return -ENOENT;
    7216             : }
    7217             : 
    7218             : static int
    7219           1 : bdev_nvme_library_init(void)
    7220             : {
    7221           1 :         g_bdev_nvme_init_thread = spdk_get_thread();
    7222             : 
    7223           1 :         spdk_io_device_register(&g_nvme_bdev_ctrlrs, bdev_nvme_create_poll_group_cb,
    7224             :                                 bdev_nvme_destroy_poll_group_cb,
    7225             :                                 sizeof(struct nvme_poll_group),  "nvme_poll_groups");
    7226             : 
    7227           1 :         return 0;
    7228             : }
    7229             : 
    7230             : static void
    7231           1 : bdev_nvme_fini_destruct_ctrlrs(void)
    7232             : {
    7233             :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
    7234             :         struct nvme_ctrlr *nvme_ctrlr;
    7235             : 
    7236           1 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    7237           1 :         TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
    7238           0 :                 TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
    7239           0 :                         pthread_mutex_lock(&nvme_ctrlr->mutex);
    7240           0 :                         if (nvme_ctrlr->destruct) {
    7241             :                                 /* This controller's destruction was already started
    7242             :                                  * before the application started shutting down
    7243             :                                  */
    7244           0 :                                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    7245           0 :                                 continue;
    7246             :                         }
    7247           0 :                         nvme_ctrlr->destruct = true;
    7248           0 :                         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    7249             : 
    7250           0 :                         spdk_thread_send_msg(nvme_ctrlr->thread, _nvme_ctrlr_destruct,
    7251             :                                              nvme_ctrlr);
    7252             :                 }
    7253             :         }
    7254             : 
    7255           1 :         g_bdev_nvme_module_finish = true;
    7256           1 :         if (TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) {
    7257           1 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
    7258           1 :                 spdk_io_device_unregister(&g_nvme_bdev_ctrlrs, NULL);
    7259           1 :                 spdk_bdev_module_fini_done();
    7260           1 :                 return;
    7261             :         }
    7262             : 
    7263           0 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    7264             : }
    7265             : 
    7266             : static void
    7267           0 : check_discovery_fini(void *arg)
    7268             : {
    7269           0 :         if (TAILQ_EMPTY(&g_discovery_ctxs)) {
    7270           0 :                 bdev_nvme_fini_destruct_ctrlrs();
    7271             :         }
    7272           0 : }
    7273             : 
    7274             : static void
    7275           1 : bdev_nvme_library_fini(void)
    7276             : {
    7277             :         struct nvme_probe_skip_entry *entry, *entry_tmp;
    7278             :         struct discovery_ctx *ctx;
    7279             : 
    7280           1 :         spdk_poller_unregister(&g_hotplug_poller);
    7281           1 :         free(g_hotplug_probe_ctx);
    7282           1 :         g_hotplug_probe_ctx = NULL;
    7283             : 
    7284           1 :         TAILQ_FOREACH_SAFE(entry, &g_skipped_nvme_ctrlrs, tailq, entry_tmp) {
    7285           0 :                 TAILQ_REMOVE(&g_skipped_nvme_ctrlrs, entry, tailq);
    7286           0 :                 free(entry);
    7287             :         }
    7288             : 
    7289           1 :         assert(spdk_get_thread() == g_bdev_nvme_init_thread);
    7290           1 :         if (TAILQ_EMPTY(&g_discovery_ctxs)) {
    7291           1 :                 bdev_nvme_fini_destruct_ctrlrs();
    7292             :         } else {
    7293           0 :                 TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
    7294           0 :                         stop_discovery(ctx, check_discovery_fini, NULL);
    7295             :                 }
    7296             :         }
    7297           1 : }
    7298             : 
    7299             : static void
    7300           0 : bdev_nvme_verify_pi_error(struct nvme_bdev_io *bio)
    7301             : {
    7302           0 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    7303           0 :         struct spdk_bdev *bdev = bdev_io->bdev;
    7304           0 :         struct spdk_dif_ctx dif_ctx;
    7305           0 :         struct spdk_dif_error err_blk = {};
    7306             :         int rc;
    7307           0 :         struct spdk_dif_ctx_init_ext_opts dif_opts;
    7308             : 
    7309           0 :         dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
    7310           0 :         dif_opts.dif_pi_format = SPDK_DIF_PI_FORMAT_16;
    7311           0 :         rc = spdk_dif_ctx_init(&dif_ctx,
    7312           0 :                                bdev->blocklen, bdev->md_len, bdev->md_interleave,
    7313           0 :                                bdev->dif_is_head_of_md, bdev->dif_type,
    7314             :                                bdev_io->u.bdev.dif_check_flags,
    7315           0 :                                bdev_io->u.bdev.offset_blocks, 0, 0, 0, 0, &dif_opts);
    7316           0 :         if (rc != 0) {
    7317           0 :                 SPDK_ERRLOG("Initialization of DIF context failed\n");
    7318           0 :                 return;
    7319             :         }
    7320             : 
    7321           0 :         if (bdev->md_interleave) {
    7322           0 :                 rc = spdk_dif_verify(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
    7323           0 :                                      bdev_io->u.bdev.num_blocks, &dif_ctx, &err_blk);
    7324             :         } else {
    7325           0 :                 struct iovec md_iov = {
    7326           0 :                         .iov_base       = bdev_io->u.bdev.md_buf,
    7327           0 :                         .iov_len        = bdev_io->u.bdev.num_blocks * bdev->md_len,
    7328             :                 };
    7329             : 
    7330           0 :                 rc = spdk_dix_verify(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
    7331           0 :                                      &md_iov, bdev_io->u.bdev.num_blocks, &dif_ctx, &err_blk);
    7332             :         }
    7333             : 
    7334           0 :         if (rc != 0) {
    7335           0 :                 SPDK_ERRLOG("DIF error detected. type=%d, offset=%" PRIu32 "\n",
    7336             :                             err_blk.err_type, err_blk.err_offset);
    7337             :         } else {
    7338           0 :                 SPDK_ERRLOG("Hardware reported PI error but SPDK could not find any.\n");
    7339             :         }
    7340             : }
    7341             : 
    7342             : static void
    7343           0 : bdev_nvme_no_pi_readv_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7344             : {
    7345           0 :         struct nvme_bdev_io *bio = ref;
    7346             : 
    7347           0 :         if (spdk_nvme_cpl_is_success(cpl)) {
    7348             :                 /* Run PI verification for read data buffer. */
    7349           0 :                 bdev_nvme_verify_pi_error(bio);
    7350             :         }
    7351             : 
    7352             :         /* Return original completion status */
    7353           0 :         bdev_nvme_io_complete_nvme_status(bio, &bio->cpl);
    7354           0 : }
    7355             : 
    7356             : static void
    7357           3 : bdev_nvme_readv_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7358             : {
    7359           3 :         struct nvme_bdev_io *bio = ref;
    7360           3 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    7361             :         int ret;
    7362             : 
    7363           3 :         if (spdk_unlikely(spdk_nvme_cpl_is_pi_error(cpl))) {
    7364           0 :                 SPDK_ERRLOG("readv completed with PI error (sct=%d, sc=%d)\n",
    7365             :                             cpl->status.sct, cpl->status.sc);
    7366             : 
    7367             :                 /* Save completion status to use after verifying PI error. */
    7368           0 :                 bio->cpl = *cpl;
    7369             : 
    7370           0 :                 if (spdk_likely(nvme_io_path_is_available(bio->io_path))) {
    7371             :                         /* Read without PI checking to verify PI error. */
    7372           0 :                         ret = bdev_nvme_no_pi_readv(bio,
    7373             :                                                     bdev_io->u.bdev.iovs,
    7374             :                                                     bdev_io->u.bdev.iovcnt,
    7375             :                                                     bdev_io->u.bdev.md_buf,
    7376             :                                                     bdev_io->u.bdev.num_blocks,
    7377             :                                                     bdev_io->u.bdev.offset_blocks);
    7378           0 :                         if (ret == 0) {
    7379           0 :                                 return;
    7380             :                         }
    7381             :                 }
    7382             :         }
    7383             : 
    7384           3 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7385             : }
    7386             : 
    7387             : static void
    7388          25 : bdev_nvme_writev_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7389             : {
    7390          25 :         struct nvme_bdev_io *bio = ref;
    7391             : 
    7392          25 :         if (spdk_unlikely(spdk_nvme_cpl_is_pi_error(cpl))) {
    7393           0 :                 SPDK_ERRLOG("writev completed with PI error (sct=%d, sc=%d)\n",
    7394             :                             cpl->status.sct, cpl->status.sc);
    7395             :                 /* Run PI verification for write data buffer if PI error is detected. */
    7396           0 :                 bdev_nvme_verify_pi_error(bio);
    7397             :         }
    7398             : 
    7399          25 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7400          25 : }
    7401             : 
    7402             : static void
    7403           0 : bdev_nvme_zone_appendv_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7404             : {
    7405           0 :         struct nvme_bdev_io *bio = ref;
    7406           0 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    7407             : 
    7408             :         /* spdk_bdev_io_get_append_location() requires that the ALBA is stored in offset_blocks.
    7409             :          * Additionally, offset_blocks has to be set before calling bdev_nvme_verify_pi_error().
    7410             :          */
    7411           0 :         bdev_io->u.bdev.offset_blocks = *(uint64_t *)&cpl->cdw0;
    7412             : 
    7413           0 :         if (spdk_nvme_cpl_is_pi_error(cpl)) {
    7414           0 :                 SPDK_ERRLOG("zone append completed with PI error (sct=%d, sc=%d)\n",
    7415             :                             cpl->status.sct, cpl->status.sc);
    7416             :                 /* Run PI verification for zone append data buffer if PI error is detected. */
    7417           0 :                 bdev_nvme_verify_pi_error(bio);
    7418             :         }
    7419             : 
    7420           0 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7421           0 : }
    7422             : 
    7423             : static void
    7424           1 : bdev_nvme_comparev_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7425             : {
    7426           1 :         struct nvme_bdev_io *bio = ref;
    7427             : 
    7428           1 :         if (spdk_nvme_cpl_is_pi_error(cpl)) {
    7429           0 :                 SPDK_ERRLOG("comparev completed with PI error (sct=%d, sc=%d)\n",
    7430             :                             cpl->status.sct, cpl->status.sc);
    7431             :                 /* Run PI verification for compare data buffer if PI error is detected. */
    7432           0 :                 bdev_nvme_verify_pi_error(bio);
    7433             :         }
    7434             : 
    7435           1 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7436           1 : }
    7437             : 
    7438             : static void
    7439           4 : bdev_nvme_comparev_and_writev_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7440             : {
    7441           4 :         struct nvme_bdev_io *bio = ref;
    7442             : 
    7443             :         /* Compare operation completion */
    7444           4 :         if (!bio->first_fused_completed) {
    7445             :                 /* Save compare result for write callback */
    7446           2 :                 bio->cpl = *cpl;
    7447           2 :                 bio->first_fused_completed = true;
    7448           2 :                 return;
    7449             :         }
    7450             : 
    7451             :         /* Write operation completion */
    7452           2 :         if (spdk_nvme_cpl_is_error(&bio->cpl)) {
    7453             :                 /* If bio->cpl is already an error, it means the compare operation failed.  In that case,
    7454             :                  * complete the IO with the compare operation's status.
    7455             :                  */
    7456           1 :                 if (!spdk_nvme_cpl_is_error(cpl)) {
    7457           1 :                         SPDK_ERRLOG("Unexpected write success after compare failure.\n");
    7458             :                 }
    7459             : 
    7460           1 :                 bdev_nvme_io_complete_nvme_status(bio, &bio->cpl);
    7461             :         } else {
    7462           1 :                 bdev_nvme_io_complete_nvme_status(bio, cpl);
    7463             :         }
    7464             : }
    7465             : 
    7466             : static void
    7467           1 : bdev_nvme_queued_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7468             : {
    7469           1 :         struct nvme_bdev_io *bio = ref;
    7470             : 
    7471           1 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7472           1 : }
    7473             : 
    7474             : static int
    7475           0 : fill_zone_from_report(struct spdk_bdev_zone_info *info, struct spdk_nvme_zns_zone_desc *desc)
    7476             : {
    7477           0 :         switch (desc->zt) {
    7478           0 :         case SPDK_NVME_ZONE_TYPE_SEQWR:
    7479           0 :                 info->type = SPDK_BDEV_ZONE_TYPE_SEQWR;
    7480           0 :                 break;
    7481           0 :         default:
    7482           0 :                 SPDK_ERRLOG("Invalid zone type: %#x in zone report\n", desc->zt);
    7483           0 :                 return -EIO;
    7484             :         }
    7485             : 
    7486           0 :         switch (desc->zs) {
    7487           0 :         case SPDK_NVME_ZONE_STATE_EMPTY:
    7488           0 :                 info->state = SPDK_BDEV_ZONE_STATE_EMPTY;
    7489           0 :                 break;
    7490           0 :         case SPDK_NVME_ZONE_STATE_IOPEN:
    7491           0 :                 info->state = SPDK_BDEV_ZONE_STATE_IMP_OPEN;
    7492           0 :                 break;
    7493           0 :         case SPDK_NVME_ZONE_STATE_EOPEN:
    7494           0 :                 info->state = SPDK_BDEV_ZONE_STATE_EXP_OPEN;
    7495           0 :                 break;
    7496           0 :         case SPDK_NVME_ZONE_STATE_CLOSED:
    7497           0 :                 info->state = SPDK_BDEV_ZONE_STATE_CLOSED;
    7498           0 :                 break;
    7499           0 :         case SPDK_NVME_ZONE_STATE_RONLY:
    7500           0 :                 info->state = SPDK_BDEV_ZONE_STATE_READ_ONLY;
    7501           0 :                 break;
    7502           0 :         case SPDK_NVME_ZONE_STATE_FULL:
    7503           0 :                 info->state = SPDK_BDEV_ZONE_STATE_FULL;
    7504           0 :                 break;
    7505           0 :         case SPDK_NVME_ZONE_STATE_OFFLINE:
    7506           0 :                 info->state = SPDK_BDEV_ZONE_STATE_OFFLINE;
    7507           0 :                 break;
    7508           0 :         default:
    7509           0 :                 SPDK_ERRLOG("Invalid zone state: %#x in zone report\n", desc->zs);
    7510           0 :                 return -EIO;
    7511             :         }
    7512             : 
    7513           0 :         info->zone_id = desc->zslba;
    7514           0 :         info->write_pointer = desc->wp;
    7515           0 :         info->capacity = desc->zcap;
    7516             : 
    7517           0 :         return 0;
    7518             : }
    7519             : 
    7520             : static void
    7521           0 : bdev_nvme_get_zone_info_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7522             : {
    7523           0 :         struct nvme_bdev_io *bio = ref;
    7524           0 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    7525           0 :         uint64_t zone_id = bdev_io->u.zone_mgmt.zone_id;
    7526           0 :         uint32_t zones_to_copy = bdev_io->u.zone_mgmt.num_zones;
    7527           0 :         struct spdk_bdev_zone_info *info = bdev_io->u.zone_mgmt.buf;
    7528             :         uint64_t max_zones_per_buf, i;
    7529             :         uint32_t zone_report_bufsize;
    7530             :         struct spdk_nvme_ns *ns;
    7531             :         struct spdk_nvme_qpair *qpair;
    7532             :         int ret;
    7533             : 
    7534           0 :         if (spdk_nvme_cpl_is_error(cpl)) {
    7535           0 :                 goto out_complete_io_nvme_cpl;
    7536             :         }
    7537             : 
    7538           0 :         if (spdk_unlikely(!nvme_io_path_is_available(bio->io_path))) {
    7539           0 :                 ret = -ENXIO;
    7540           0 :                 goto out_complete_io_ret;
    7541             :         }
    7542             : 
    7543           0 :         ns = bio->io_path->nvme_ns->ns;
    7544           0 :         qpair = bio->io_path->qpair->qpair;
    7545             : 
    7546           0 :         zone_report_bufsize = spdk_nvme_ns_get_max_io_xfer_size(ns);
    7547           0 :         max_zones_per_buf = (zone_report_bufsize - sizeof(*bio->zone_report_buf)) /
    7548             :                             sizeof(bio->zone_report_buf->descs[0]);
    7549             : 
    7550           0 :         if (bio->zone_report_buf->nr_zones > max_zones_per_buf) {
    7551           0 :                 ret = -EINVAL;
    7552           0 :                 goto out_complete_io_ret;
    7553             :         }
    7554             : 
    7555           0 :         if (!bio->zone_report_buf->nr_zones) {
    7556           0 :                 ret = -EINVAL;
    7557           0 :                 goto out_complete_io_ret;
    7558             :         }
    7559             : 
    7560           0 :         for (i = 0; i < bio->zone_report_buf->nr_zones && bio->handled_zones < zones_to_copy; i++) {
    7561           0 :                 ret = fill_zone_from_report(&info[bio->handled_zones],
    7562           0 :                                             &bio->zone_report_buf->descs[i]);
    7563           0 :                 if (ret) {
    7564           0 :                         goto out_complete_io_ret;
    7565             :                 }
    7566           0 :                 bio->handled_zones++;
    7567             :         }
    7568             : 
    7569           0 :         if (bio->handled_zones < zones_to_copy) {
    7570           0 :                 uint64_t zone_size_lba = spdk_nvme_zns_ns_get_zone_size_sectors(ns);
    7571           0 :                 uint64_t slba = zone_id + (zone_size_lba * bio->handled_zones);
    7572             : 
    7573           0 :                 memset(bio->zone_report_buf, 0, zone_report_bufsize);
    7574           0 :                 ret = spdk_nvme_zns_report_zones(ns, qpair,
    7575           0 :                                                  bio->zone_report_buf, zone_report_bufsize,
    7576             :                                                  slba, SPDK_NVME_ZRA_LIST_ALL, true,
    7577             :                                                  bdev_nvme_get_zone_info_done, bio);
    7578           0 :                 if (!ret) {
    7579           0 :                         return;
    7580             :                 } else {
    7581           0 :                         goto out_complete_io_ret;
    7582             :                 }
    7583             :         }
    7584             : 
    7585           0 : out_complete_io_nvme_cpl:
    7586           0 :         free(bio->zone_report_buf);
    7587           0 :         bio->zone_report_buf = NULL;
    7588           0 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7589           0 :         return;
    7590             : 
    7591           0 : out_complete_io_ret:
    7592           0 :         free(bio->zone_report_buf);
    7593           0 :         bio->zone_report_buf = NULL;
    7594           0 :         bdev_nvme_io_complete(bio, ret);
    7595             : }
    7596             : 
    7597             : static void
    7598           0 : bdev_nvme_zone_management_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7599             : {
    7600           0 :         struct nvme_bdev_io *bio = ref;
    7601             : 
    7602           0 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7603           0 : }
    7604             : 
    7605             : static void
    7606           4 : bdev_nvme_admin_passthru_complete_nvme_status(void *ctx)
    7607             : {
    7608           4 :         struct nvme_bdev_io *bio = ctx;
    7609           4 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    7610           4 :         const struct spdk_nvme_cpl *cpl = &bio->cpl;
    7611             : 
    7612           4 :         assert(bdev_nvme_io_type_is_admin(bdev_io->type));
    7613             : 
    7614           4 :         __bdev_nvme_io_complete(bdev_io, 0, cpl);
    7615           4 : }
    7616             : 
    7617             : static void
    7618           3 : bdev_nvme_abort_complete(void *ctx)
    7619             : {
    7620           3 :         struct nvme_bdev_io *bio = ctx;
    7621           3 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    7622             : 
    7623           3 :         if (spdk_nvme_cpl_is_abort_success(&bio->cpl)) {
    7624           3 :                 __bdev_nvme_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS, NULL);
    7625             :         } else {
    7626           0 :                 __bdev_nvme_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED, NULL);
    7627             :         }
    7628           3 : }
    7629             : 
    7630             : static void
    7631           3 : bdev_nvme_abort_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7632             : {
    7633           3 :         struct nvme_bdev_io *bio = ref;
    7634           3 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    7635             : 
    7636           3 :         bio->cpl = *cpl;
    7637           3 :         spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io), bdev_nvme_abort_complete, bio);
    7638           3 : }
    7639             : 
    7640             : static void
    7641           4 : bdev_nvme_admin_passthru_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7642             : {
    7643           4 :         struct nvme_bdev_io *bio = ref;
    7644           4 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    7645             : 
    7646           4 :         bio->cpl = *cpl;
    7647           4 :         spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io),
    7648             :                              bdev_nvme_admin_passthru_complete_nvme_status, bio);
    7649           4 : }
    7650             : 
    7651             : static void
    7652           0 : bdev_nvme_queued_reset_sgl(void *ref, uint32_t sgl_offset)
    7653             : {
    7654           0 :         struct nvme_bdev_io *bio = ref;
    7655             :         struct iovec *iov;
    7656             : 
    7657           0 :         bio->iov_offset = sgl_offset;
    7658           0 :         for (bio->iovpos = 0; bio->iovpos < bio->iovcnt; bio->iovpos++) {
    7659           0 :                 iov = &bio->iovs[bio->iovpos];
    7660           0 :                 if (bio->iov_offset < iov->iov_len) {
    7661           0 :                         break;
    7662             :                 }
    7663             : 
    7664           0 :                 bio->iov_offset -= iov->iov_len;
    7665             :         }
    7666           0 : }
    7667             : 
    7668             : static int
    7669           0 : bdev_nvme_queued_next_sge(void *ref, void **address, uint32_t *length)
    7670             : {
    7671           0 :         struct nvme_bdev_io *bio = ref;
    7672             :         struct iovec *iov;
    7673             : 
    7674           0 :         assert(bio->iovpos < bio->iovcnt);
    7675             : 
    7676           0 :         iov = &bio->iovs[bio->iovpos];
    7677             : 
    7678           0 :         *address = iov->iov_base;
    7679           0 :         *length = iov->iov_len;
    7680             : 
    7681           0 :         if (bio->iov_offset) {
    7682           0 :                 assert(bio->iov_offset <= iov->iov_len);
    7683           0 :                 *address += bio->iov_offset;
    7684           0 :                 *length -= bio->iov_offset;
    7685             :         }
    7686             : 
    7687           0 :         bio->iov_offset += *length;
    7688           0 :         if (bio->iov_offset == iov->iov_len) {
    7689           0 :                 bio->iovpos++;
    7690           0 :                 bio->iov_offset = 0;
    7691             :         }
    7692             : 
    7693           0 :         return 0;
    7694             : }
    7695             : 
    7696             : static void
    7697           0 : bdev_nvme_queued_reset_fused_sgl(void *ref, uint32_t sgl_offset)
    7698             : {
    7699           0 :         struct nvme_bdev_io *bio = ref;
    7700             :         struct iovec *iov;
    7701             : 
    7702           0 :         bio->fused_iov_offset = sgl_offset;
    7703           0 :         for (bio->fused_iovpos = 0; bio->fused_iovpos < bio->fused_iovcnt; bio->fused_iovpos++) {
    7704           0 :                 iov = &bio->fused_iovs[bio->fused_iovpos];
    7705           0 :                 if (bio->fused_iov_offset < iov->iov_len) {
    7706           0 :                         break;
    7707             :                 }
    7708             : 
    7709           0 :                 bio->fused_iov_offset -= iov->iov_len;
    7710             :         }
    7711           0 : }
    7712             : 
    7713             : static int
    7714           0 : bdev_nvme_queued_next_fused_sge(void *ref, void **address, uint32_t *length)
    7715             : {
    7716           0 :         struct nvme_bdev_io *bio = ref;
    7717             :         struct iovec *iov;
    7718             : 
    7719           0 :         assert(bio->fused_iovpos < bio->fused_iovcnt);
    7720             : 
    7721           0 :         iov = &bio->fused_iovs[bio->fused_iovpos];
    7722             : 
    7723           0 :         *address = iov->iov_base;
    7724           0 :         *length = iov->iov_len;
    7725             : 
    7726           0 :         if (bio->fused_iov_offset) {
    7727           0 :                 assert(bio->fused_iov_offset <= iov->iov_len);
    7728           0 :                 *address += bio->fused_iov_offset;
    7729           0 :                 *length -= bio->fused_iov_offset;
    7730             :         }
    7731             : 
    7732           0 :         bio->fused_iov_offset += *length;
    7733           0 :         if (bio->fused_iov_offset == iov->iov_len) {
    7734           0 :                 bio->fused_iovpos++;
    7735           0 :                 bio->fused_iov_offset = 0;
    7736             :         }
    7737             : 
    7738           0 :         return 0;
    7739             : }
    7740             : 
    7741             : static int
    7742           0 : bdev_nvme_no_pi_readv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
    7743             :                       void *md, uint64_t lba_count, uint64_t lba)
    7744             : {
    7745             :         int rc;
    7746             : 
    7747           0 :         SPDK_DEBUGLOG(bdev_nvme, "read %" PRIu64 " blocks with offset %#" PRIx64 " without PI check\n",
    7748             :                       lba_count, lba);
    7749             : 
    7750           0 :         bio->iovs = iov;
    7751           0 :         bio->iovcnt = iovcnt;
    7752           0 :         bio->iovpos = 0;
    7753           0 :         bio->iov_offset = 0;
    7754             : 
    7755           0 :         rc = spdk_nvme_ns_cmd_readv_with_md(bio->io_path->nvme_ns->ns,
    7756           0 :                                             bio->io_path->qpair->qpair,
    7757             :                                             lba, lba_count,
    7758             :                                             bdev_nvme_no_pi_readv_done, bio, 0,
    7759             :                                             bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge,
    7760             :                                             md, 0, 0);
    7761             : 
    7762           0 :         if (rc != 0 && rc != -ENOMEM) {
    7763           0 :                 SPDK_ERRLOG("no_pi_readv failed: rc = %d\n", rc);
    7764             :         }
    7765           0 :         return rc;
    7766             : }
    7767             : 
    7768             : static int
    7769           3 : bdev_nvme_readv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
    7770             :                 void *md, uint64_t lba_count, uint64_t lba, uint32_t flags,
    7771             :                 struct spdk_memory_domain *domain, void *domain_ctx,
    7772             :                 struct spdk_accel_sequence *seq)
    7773             : {
    7774           3 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
    7775           3 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
    7776             :         int rc;
    7777             : 
    7778           3 :         SPDK_DEBUGLOG(bdev_nvme, "read %" PRIu64 " blocks with offset %#" PRIx64 "\n",
    7779             :                       lba_count, lba);
    7780             : 
    7781           3 :         bio->iovs = iov;
    7782           3 :         bio->iovcnt = iovcnt;
    7783           3 :         bio->iovpos = 0;
    7784           3 :         bio->iov_offset = 0;
    7785             : 
    7786           3 :         if (domain != NULL || seq != NULL) {
    7787           1 :                 bio->ext_opts.size = SPDK_SIZEOF(&bio->ext_opts, accel_sequence);
    7788           1 :                 bio->ext_opts.memory_domain = domain;
    7789           1 :                 bio->ext_opts.memory_domain_ctx = domain_ctx;
    7790           1 :                 bio->ext_opts.io_flags = flags;
    7791           1 :                 bio->ext_opts.metadata = md;
    7792           1 :                 bio->ext_opts.accel_sequence = seq;
    7793             : 
    7794           1 :                 if (iovcnt == 1) {
    7795           1 :                         rc = spdk_nvme_ns_cmd_read_ext(ns, qpair, iov[0].iov_base, lba, lba_count, bdev_nvme_readv_done,
    7796             :                                                        bio, &bio->ext_opts);
    7797             :                 } else {
    7798           0 :                         rc = spdk_nvme_ns_cmd_readv_ext(ns, qpair, lba, lba_count,
    7799             :                                                         bdev_nvme_readv_done, bio,
    7800             :                                                         bdev_nvme_queued_reset_sgl,
    7801             :                                                         bdev_nvme_queued_next_sge,
    7802             :                                                         &bio->ext_opts);
    7803             :                 }
    7804           2 :         } else if (iovcnt == 1) {
    7805           2 :                 rc = spdk_nvme_ns_cmd_read_with_md(ns, qpair, iov[0].iov_base,
    7806             :                                                    md, lba, lba_count, bdev_nvme_readv_done,
    7807             :                                                    bio, flags, 0, 0);
    7808             :         } else {
    7809           0 :                 rc = spdk_nvme_ns_cmd_readv_with_md(ns, qpair, lba, lba_count,
    7810             :                                                     bdev_nvme_readv_done, bio, flags,
    7811             :                                                     bdev_nvme_queued_reset_sgl,
    7812             :                                                     bdev_nvme_queued_next_sge, md, 0, 0);
    7813             :         }
    7814             : 
    7815           3 :         if (spdk_unlikely(rc != 0 && rc != -ENOMEM)) {
    7816           0 :                 SPDK_ERRLOG("readv failed: rc = %d\n", rc);
    7817             :         }
    7818           3 :         return rc;
    7819             : }
    7820             : 
    7821             : static int
    7822          25 : bdev_nvme_writev(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
    7823             :                  void *md, uint64_t lba_count, uint64_t lba, uint32_t flags,
    7824             :                  struct spdk_memory_domain *domain, void *domain_ctx,
    7825             :                  struct spdk_accel_sequence *seq,
    7826             :                  union spdk_bdev_nvme_cdw12 cdw12, union spdk_bdev_nvme_cdw13 cdw13)
    7827             : {
    7828          25 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
    7829          25 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
    7830             :         int rc;
    7831             : 
    7832          25 :         SPDK_DEBUGLOG(bdev_nvme, "write %" PRIu64 " blocks with offset %#" PRIx64 "\n",
    7833             :                       lba_count, lba);
    7834             : 
    7835          25 :         bio->iovs = iov;
    7836          25 :         bio->iovcnt = iovcnt;
    7837          25 :         bio->iovpos = 0;
    7838          25 :         bio->iov_offset = 0;
    7839             : 
    7840          25 :         if (domain != NULL || seq != NULL) {
    7841           0 :                 bio->ext_opts.size = SPDK_SIZEOF(&bio->ext_opts, accel_sequence);
    7842           0 :                 bio->ext_opts.memory_domain = domain;
    7843           0 :                 bio->ext_opts.memory_domain_ctx = domain_ctx;
    7844           0 :                 bio->ext_opts.io_flags = flags | SPDK_NVME_IO_FLAGS_DIRECTIVE(cdw12.write.dtype);
    7845           0 :                 bio->ext_opts.cdw13 = cdw13.raw;
    7846           0 :                 bio->ext_opts.metadata = md;
    7847           0 :                 bio->ext_opts.accel_sequence = seq;
    7848             : 
    7849           0 :                 if (iovcnt == 1) {
    7850           0 :                         rc = spdk_nvme_ns_cmd_write_ext(ns, qpair, iov[0].iov_base, lba, lba_count, bdev_nvme_writev_done,
    7851             :                                                         bio, &bio->ext_opts);
    7852             :                 } else {
    7853           0 :                         rc = spdk_nvme_ns_cmd_writev_ext(ns, qpair, lba, lba_count,
    7854             :                                                          bdev_nvme_writev_done, bio,
    7855             :                                                          bdev_nvme_queued_reset_sgl,
    7856             :                                                          bdev_nvme_queued_next_sge,
    7857             :                                                          &bio->ext_opts);
    7858             :                 }
    7859          25 :         } else if (iovcnt == 1) {
    7860          25 :                 rc = spdk_nvme_ns_cmd_write_with_md(ns, qpair, iov[0].iov_base,
    7861             :                                                     md, lba, lba_count, bdev_nvme_writev_done,
    7862             :                                                     bio, flags, 0, 0);
    7863             :         } else {
    7864           0 :                 rc = spdk_nvme_ns_cmd_writev_with_md(ns, qpair, lba, lba_count,
    7865             :                                                      bdev_nvme_writev_done, bio, flags,
    7866             :                                                      bdev_nvme_queued_reset_sgl,
    7867             :                                                      bdev_nvme_queued_next_sge, md, 0, 0);
    7868             :         }
    7869             : 
    7870          25 :         if (spdk_unlikely(rc != 0 && rc != -ENOMEM)) {
    7871           0 :                 SPDK_ERRLOG("writev failed: rc = %d\n", rc);
    7872             :         }
    7873          25 :         return rc;
    7874             : }
    7875             : 
    7876             : static int
    7877           0 : bdev_nvme_zone_appendv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
    7878             :                        void *md, uint64_t lba_count, uint64_t zslba,
    7879             :                        uint32_t flags)
    7880             : {
    7881           0 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
    7882           0 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
    7883             :         int rc;
    7884             : 
    7885           0 :         SPDK_DEBUGLOG(bdev_nvme, "zone append %" PRIu64 " blocks to zone start lba %#" PRIx64 "\n",
    7886             :                       lba_count, zslba);
    7887             : 
    7888           0 :         bio->iovs = iov;
    7889           0 :         bio->iovcnt = iovcnt;
    7890           0 :         bio->iovpos = 0;
    7891           0 :         bio->iov_offset = 0;
    7892             : 
    7893           0 :         if (iovcnt == 1) {
    7894           0 :                 rc = spdk_nvme_zns_zone_append_with_md(ns, qpair, iov[0].iov_base, md, zslba,
    7895             :                                                        lba_count,
    7896             :                                                        bdev_nvme_zone_appendv_done, bio,
    7897             :                                                        flags,
    7898             :                                                        0, 0);
    7899             :         } else {
    7900           0 :                 rc = spdk_nvme_zns_zone_appendv_with_md(ns, qpair, zslba, lba_count,
    7901             :                                                         bdev_nvme_zone_appendv_done, bio, flags,
    7902             :                                                         bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge,
    7903             :                                                         md, 0, 0);
    7904             :         }
    7905             : 
    7906           0 :         if (rc != 0 && rc != -ENOMEM) {
    7907           0 :                 SPDK_ERRLOG("zone append failed: rc = %d\n", rc);
    7908             :         }
    7909           0 :         return rc;
    7910             : }
    7911             : 
    7912             : static int
    7913           1 : bdev_nvme_comparev(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
    7914             :                    void *md, uint64_t lba_count, uint64_t lba,
    7915             :                    uint32_t flags)
    7916             : {
    7917             :         int rc;
    7918             : 
    7919           1 :         SPDK_DEBUGLOG(bdev_nvme, "compare %" PRIu64 " blocks with offset %#" PRIx64 "\n",
    7920             :                       lba_count, lba);
    7921             : 
    7922           1 :         bio->iovs = iov;
    7923           1 :         bio->iovcnt = iovcnt;
    7924           1 :         bio->iovpos = 0;
    7925           1 :         bio->iov_offset = 0;
    7926             : 
    7927           1 :         rc = spdk_nvme_ns_cmd_comparev_with_md(bio->io_path->nvme_ns->ns,
    7928           1 :                                                bio->io_path->qpair->qpair,
    7929             :                                                lba, lba_count,
    7930             :                                                bdev_nvme_comparev_done, bio, flags,
    7931             :                                                bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge,
    7932             :                                                md, 0, 0);
    7933             : 
    7934           1 :         if (rc != 0 && rc != -ENOMEM) {
    7935           0 :                 SPDK_ERRLOG("comparev failed: rc = %d\n", rc);
    7936             :         }
    7937           1 :         return rc;
    7938             : }
    7939             : 
    7940             : static int
    7941           2 : bdev_nvme_comparev_and_writev(struct nvme_bdev_io *bio, struct iovec *cmp_iov, int cmp_iovcnt,
    7942             :                               struct iovec *write_iov, int write_iovcnt,
    7943             :                               void *md, uint64_t lba_count, uint64_t lba, uint32_t flags)
    7944             : {
    7945           2 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
    7946           2 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
    7947           2 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    7948             :         int rc;
    7949             : 
    7950           2 :         SPDK_DEBUGLOG(bdev_nvme, "compare and write %" PRIu64 " blocks with offset %#" PRIx64 "\n",
    7951             :                       lba_count, lba);
    7952             : 
    7953           2 :         bio->iovs = cmp_iov;
    7954           2 :         bio->iovcnt = cmp_iovcnt;
    7955           2 :         bio->iovpos = 0;
    7956           2 :         bio->iov_offset = 0;
    7957           2 :         bio->fused_iovs = write_iov;
    7958           2 :         bio->fused_iovcnt = write_iovcnt;
    7959           2 :         bio->fused_iovpos = 0;
    7960           2 :         bio->fused_iov_offset = 0;
    7961             : 
    7962           2 :         if (bdev_io->num_retries == 0) {
    7963           2 :                 bio->first_fused_submitted = false;
    7964           2 :                 bio->first_fused_completed = false;
    7965             :         }
    7966             : 
    7967           2 :         if (!bio->first_fused_submitted) {
    7968           2 :                 flags |= SPDK_NVME_IO_FLAGS_FUSE_FIRST;
    7969           2 :                 memset(&bio->cpl, 0, sizeof(bio->cpl));
    7970             : 
    7971           2 :                 rc = spdk_nvme_ns_cmd_comparev_with_md(ns, qpair, lba, lba_count,
    7972             :                                                        bdev_nvme_comparev_and_writev_done, bio, flags,
    7973             :                                                        bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge, md, 0, 0);
    7974           2 :                 if (rc == 0) {
    7975           2 :                         bio->first_fused_submitted = true;
    7976           2 :                         flags &= ~SPDK_NVME_IO_FLAGS_FUSE_FIRST;
    7977             :                 } else {
    7978           0 :                         if (rc != -ENOMEM) {
    7979           0 :                                 SPDK_ERRLOG("compare failed: rc = %d\n", rc);
    7980             :                         }
    7981           0 :                         return rc;
    7982             :                 }
    7983             :         }
    7984             : 
    7985           2 :         flags |= SPDK_NVME_IO_FLAGS_FUSE_SECOND;
    7986             : 
    7987           2 :         rc = spdk_nvme_ns_cmd_writev_with_md(ns, qpair, lba, lba_count,
    7988             :                                              bdev_nvme_comparev_and_writev_done, bio, flags,
    7989             :                                              bdev_nvme_queued_reset_fused_sgl, bdev_nvme_queued_next_fused_sge, md, 0, 0);
    7990           2 :         if (rc != 0 && rc != -ENOMEM) {
    7991           0 :                 SPDK_ERRLOG("write failed: rc = %d\n", rc);
    7992           0 :                 rc = 0;
    7993             :         }
    7994             : 
    7995           2 :         return rc;
    7996             : }
    7997             : 
    7998             : static int
    7999           1 : bdev_nvme_unmap(struct nvme_bdev_io *bio, uint64_t offset_blocks, uint64_t num_blocks)
    8000             : {
    8001           1 :         struct spdk_nvme_dsm_range dsm_ranges[SPDK_NVME_DATASET_MANAGEMENT_MAX_RANGES];
    8002             :         struct spdk_nvme_dsm_range *range;
    8003             :         uint64_t offset, remaining;
    8004             :         uint64_t num_ranges_u64;
    8005             :         uint16_t num_ranges;
    8006             :         int rc;
    8007             : 
    8008           1 :         num_ranges_u64 = (num_blocks + SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS - 1) /
    8009             :                          SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS;
    8010           1 :         if (num_ranges_u64 > SPDK_COUNTOF(dsm_ranges)) {
    8011           0 :                 SPDK_ERRLOG("Unmap request for %" PRIu64 " blocks is too large\n", num_blocks);
    8012           0 :                 return -EINVAL;
    8013             :         }
    8014           1 :         num_ranges = (uint16_t)num_ranges_u64;
    8015             : 
    8016           1 :         offset = offset_blocks;
    8017           1 :         remaining = num_blocks;
    8018           1 :         range = &dsm_ranges[0];
    8019             : 
    8020             :         /* Fill max-size ranges until the remaining blocks fit into one range */
    8021           1 :         while (remaining > SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS) {
    8022           0 :                 range->attributes.raw = 0;
    8023           0 :                 range->length = SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS;
    8024           0 :                 range->starting_lba = offset;
    8025             : 
    8026           0 :                 offset += SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS;
    8027           0 :                 remaining -= SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS;
    8028           0 :                 range++;
    8029             :         }
    8030             : 
    8031             :         /* Final range describes the remaining blocks */
    8032           1 :         range->attributes.raw = 0;
    8033           1 :         range->length = remaining;
    8034           1 :         range->starting_lba = offset;
    8035             : 
    8036           1 :         rc = spdk_nvme_ns_cmd_dataset_management(bio->io_path->nvme_ns->ns,
    8037           1 :                         bio->io_path->qpair->qpair,
    8038             :                         SPDK_NVME_DSM_ATTR_DEALLOCATE,
    8039             :                         dsm_ranges, num_ranges,
    8040             :                         bdev_nvme_queued_done, bio);
    8041             : 
    8042           1 :         return rc;
    8043             : }
    8044             : 
    8045             : static int
    8046           0 : bdev_nvme_write_zeroes(struct nvme_bdev_io *bio, uint64_t offset_blocks, uint64_t num_blocks)
    8047             : {
    8048           0 :         if (num_blocks > UINT16_MAX + 1) {
    8049           0 :                 SPDK_ERRLOG("NVMe write zeroes is limited to 16-bit block count\n");
    8050           0 :                 return -EINVAL;
    8051             :         }
    8052             : 
    8053           0 :         return spdk_nvme_ns_cmd_write_zeroes(bio->io_path->nvme_ns->ns,
    8054           0 :                                              bio->io_path->qpair->qpair,
    8055             :                                              offset_blocks, num_blocks,
    8056             :                                              bdev_nvme_queued_done, bio,
    8057             :                                              0);
    8058             : }
    8059             : 
    8060             : static int
    8061           0 : bdev_nvme_get_zone_info(struct nvme_bdev_io *bio, uint64_t zone_id, uint32_t num_zones,
    8062             :                         struct spdk_bdev_zone_info *info)
    8063             : {
    8064           0 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
    8065           0 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
    8066           0 :         uint32_t zone_report_bufsize = spdk_nvme_ns_get_max_io_xfer_size(ns);
    8067           0 :         uint64_t zone_size = spdk_nvme_zns_ns_get_zone_size_sectors(ns);
    8068           0 :         uint64_t total_zones = spdk_nvme_zns_ns_get_num_zones(ns);
    8069             : 
    8070           0 :         if (zone_id % zone_size != 0) {
    8071           0 :                 return -EINVAL;
    8072             :         }
    8073             : 
    8074           0 :         if (num_zones > total_zones || !num_zones) {
    8075           0 :                 return -EINVAL;
    8076             :         }
    8077             : 
    8078           0 :         assert(!bio->zone_report_buf);
    8079           0 :         bio->zone_report_buf = calloc(1, zone_report_bufsize);
    8080           0 :         if (!bio->zone_report_buf) {
    8081           0 :                 return -ENOMEM;
    8082             :         }
    8083             : 
    8084           0 :         bio->handled_zones = 0;
    8085             : 
    8086           0 :         return spdk_nvme_zns_report_zones(ns, qpair, bio->zone_report_buf, zone_report_bufsize,
    8087             :                                           zone_id, SPDK_NVME_ZRA_LIST_ALL, true,
    8088             :                                           bdev_nvme_get_zone_info_done, bio);
    8089             : }
    8090             : 
    8091             : static int
    8092           0 : bdev_nvme_zone_management(struct nvme_bdev_io *bio, uint64_t zone_id,
    8093             :                           enum spdk_bdev_zone_action action)
    8094             : {
    8095           0 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
    8096           0 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
    8097             : 
    8098           0 :         switch (action) {
    8099           0 :         case SPDK_BDEV_ZONE_CLOSE:
    8100           0 :                 return spdk_nvme_zns_close_zone(ns, qpair, zone_id, false,
    8101             :                                                 bdev_nvme_zone_management_done, bio);
    8102           0 :         case SPDK_BDEV_ZONE_FINISH:
    8103           0 :                 return spdk_nvme_zns_finish_zone(ns, qpair, zone_id, false,
    8104             :                                                  bdev_nvme_zone_management_done, bio);
    8105           0 :         case SPDK_BDEV_ZONE_OPEN:
    8106           0 :                 return spdk_nvme_zns_open_zone(ns, qpair, zone_id, false,
    8107             :                                                bdev_nvme_zone_management_done, bio);
    8108           0 :         case SPDK_BDEV_ZONE_RESET:
    8109           0 :                 return spdk_nvme_zns_reset_zone(ns, qpair, zone_id, false,
    8110             :                                                 bdev_nvme_zone_management_done, bio);
    8111           0 :         case SPDK_BDEV_ZONE_OFFLINE:
    8112           0 :                 return spdk_nvme_zns_offline_zone(ns, qpair, zone_id, false,
    8113             :                                                   bdev_nvme_zone_management_done, bio);
    8114           0 :         default:
    8115           0 :                 return -EINVAL;
    8116             :         }
    8117             : }
    8118             : 
    8119             : static void
    8120           5 : bdev_nvme_admin_passthru(struct nvme_bdev_channel *nbdev_ch, struct nvme_bdev_io *bio,
    8121             :                          struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes)
    8122             : {
    8123             :         struct nvme_io_path *io_path;
    8124             :         struct nvme_ctrlr *nvme_ctrlr;
    8125             :         uint32_t max_xfer_size;
    8126           5 :         int rc = -ENXIO;
    8127             : 
    8128             :         /* Choose the first ctrlr which is not failed. */
    8129           8 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
    8130           7 :                 nvme_ctrlr = io_path->qpair->ctrlr;
    8131             : 
    8132             :                 /* We should skip any unavailable nvme_ctrlr rather than checking
    8133             :                  * if the return value of spdk_nvme_ctrlr_cmd_admin_raw() is -ENXIO.
    8134             :                  */
    8135           7 :                 if (!nvme_ctrlr_is_available(nvme_ctrlr)) {
    8136           3 :                         continue;
    8137             :                 }
    8138             : 
    8139           4 :                 max_xfer_size = spdk_nvme_ctrlr_get_max_xfer_size(nvme_ctrlr->ctrlr);
    8140             : 
    8141           4 :                 if (nbytes > max_xfer_size) {
    8142           0 :                         SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size);
    8143           0 :                         rc = -EINVAL;
    8144           0 :                         goto err;
    8145             :                 }
    8146             : 
    8147           4 :                 rc = spdk_nvme_ctrlr_cmd_admin_raw(nvme_ctrlr->ctrlr, cmd, buf, (uint32_t)nbytes,
    8148             :                                                    bdev_nvme_admin_passthru_done, bio);
    8149           4 :                 if (rc == 0) {
    8150           4 :                         return;
    8151             :                 }
    8152             :         }
    8153             : 
    8154           1 : err:
    8155           1 :         bdev_nvme_admin_complete(bio, rc);
    8156             : }
    8157             : 
    8158             : static int
    8159           0 : bdev_nvme_io_passthru(struct nvme_bdev_io *bio, struct spdk_nvme_cmd *cmd,
    8160             :                       void *buf, size_t nbytes)
    8161             : {
    8162           0 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
    8163           0 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
    8164           0 :         uint32_t max_xfer_size = spdk_nvme_ns_get_max_io_xfer_size(ns);
    8165           0 :         struct spdk_nvme_ctrlr *ctrlr = spdk_nvme_ns_get_ctrlr(ns);
    8166             : 
    8167           0 :         if (nbytes > max_xfer_size) {
    8168           0 :                 SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size);
    8169           0 :                 return -EINVAL;
    8170             :         }
    8171             : 
    8172             :         /*
    8173             :          * Each NVMe bdev is a specific namespace, and all NVMe I/O commands require a nsid,
    8174             :          * so fill it out automatically.
    8175             :          */
    8176           0 :         cmd->nsid = spdk_nvme_ns_get_id(ns);
    8177             : 
    8178           0 :         return spdk_nvme_ctrlr_cmd_io_raw(ctrlr, qpair, cmd, buf,
    8179             :                                           (uint32_t)nbytes, bdev_nvme_queued_done, bio);
    8180             : }
    8181             : 
    8182             : static int
    8183           0 : bdev_nvme_io_passthru_md(struct nvme_bdev_io *bio, struct spdk_nvme_cmd *cmd,
    8184             :                          void *buf, size_t nbytes, void *md_buf, size_t md_len)
    8185             : {
    8186           0 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
    8187           0 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
    8188           0 :         size_t nr_sectors = nbytes / spdk_nvme_ns_get_extended_sector_size(ns);
    8189           0 :         uint32_t max_xfer_size = spdk_nvme_ns_get_max_io_xfer_size(ns);
    8190           0 :         struct spdk_nvme_ctrlr *ctrlr = spdk_nvme_ns_get_ctrlr(ns);
    8191             : 
    8192           0 :         if (nbytes > max_xfer_size) {
    8193           0 :                 SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size);
    8194           0 :                 return -EINVAL;
    8195             :         }
    8196             : 
    8197           0 :         if (md_len != nr_sectors * spdk_nvme_ns_get_md_size(ns)) {
    8198           0 :                 SPDK_ERRLOG("invalid meta data buffer size\n");
    8199           0 :                 return -EINVAL;
    8200             :         }
    8201             : 
    8202             :         /*
    8203             :          * Each NVMe bdev is a specific namespace, and all NVMe I/O commands require a nsid,
    8204             :          * so fill it out automatically.
    8205             :          */
    8206           0 :         cmd->nsid = spdk_nvme_ns_get_id(ns);
    8207             : 
    8208           0 :         return spdk_nvme_ctrlr_cmd_io_raw_with_md(ctrlr, qpair, cmd, buf,
    8209             :                         (uint32_t)nbytes, md_buf, bdev_nvme_queued_done, bio);
    8210             : }
    8211             : 
    8212             : static int
    8213           0 : bdev_nvme_iov_passthru_md(struct nvme_bdev_io *bio,
    8214             :                           struct spdk_nvme_cmd *cmd, struct iovec *iov, int iovcnt,
    8215             :                           size_t nbytes, void *md_buf, size_t md_len)
    8216             : {
    8217           0 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
    8218           0 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
    8219           0 :         size_t nr_sectors = nbytes / spdk_nvme_ns_get_extended_sector_size(ns);
    8220           0 :         uint32_t max_xfer_size = spdk_nvme_ns_get_max_io_xfer_size(ns);
    8221           0 :         struct spdk_nvme_ctrlr *ctrlr = spdk_nvme_ns_get_ctrlr(ns);
    8222             : 
    8223           0 :         bio->iovs = iov;
    8224           0 :         bio->iovcnt = iovcnt;
    8225           0 :         bio->iovpos = 0;
    8226           0 :         bio->iov_offset = 0;
    8227             : 
    8228           0 :         if (nbytes > max_xfer_size) {
    8229           0 :                 SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size);
    8230           0 :                 return -EINVAL;
    8231             :         }
    8232             : 
    8233           0 :         if (md_len != nr_sectors * spdk_nvme_ns_get_md_size(ns)) {
    8234           0 :                 SPDK_ERRLOG("invalid meta data buffer size\n");
    8235           0 :                 return -EINVAL;
    8236             :         }
    8237             : 
    8238             :         /*
    8239             :          * Each NVMe bdev is a specific namespace, and all NVMe I/O commands
    8240             :          * require a nsid, so fill it out automatically.
    8241             :          */
    8242           0 :         cmd->nsid = spdk_nvme_ns_get_id(ns);
    8243             : 
    8244           0 :         return spdk_nvme_ctrlr_cmd_iov_raw_with_md(
    8245             :                        ctrlr, qpair, cmd, (uint32_t)nbytes, md_buf, bdev_nvme_queued_done, bio,
    8246             :                        bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge);
    8247             : }
    8248             : 
    8249             : static void
    8250           6 : bdev_nvme_abort(struct nvme_bdev_channel *nbdev_ch, struct nvme_bdev_io *bio,
    8251             :                 struct nvme_bdev_io *bio_to_abort)
    8252             : {
    8253             :         struct nvme_io_path *io_path;
    8254           6 :         int rc = 0;
    8255             : 
    8256           6 :         rc = bdev_nvme_abort_retry_io(nbdev_ch, bio_to_abort);
    8257           6 :         if (rc == 0) {
    8258           1 :                 bdev_nvme_admin_complete(bio, 0);
    8259           1 :                 return;
    8260             :         }
    8261             : 
    8262           5 :         io_path = bio_to_abort->io_path;
    8263           5 :         if (io_path != NULL) {
    8264           3 :                 rc = spdk_nvme_ctrlr_cmd_abort_ext(io_path->qpair->ctrlr->ctrlr,
    8265           3 :                                                    io_path->qpair->qpair,
    8266             :                                                    bio_to_abort,
    8267             :                                                    bdev_nvme_abort_done, bio);
    8268             :         } else {
    8269           3 :                 STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
    8270           2 :                         rc = spdk_nvme_ctrlr_cmd_abort_ext(io_path->qpair->ctrlr->ctrlr,
    8271             :                                                            NULL,
    8272             :                                                            bio_to_abort,
    8273             :                                                            bdev_nvme_abort_done, bio);
    8274             : 
    8275           2 :                         if (rc != -ENOENT) {
    8276           1 :                                 break;
    8277             :                         }
    8278             :                 }
    8279             :         }
    8280             : 
    8281           5 :         if (rc != 0) {
    8282             :                 /* If no command was found or there was any error, complete the abort
    8283             :                  * request with failure.
    8284             :                  */
    8285           2 :                 bdev_nvme_admin_complete(bio, rc);
    8286             :         }
    8287             : }
    8288             : 
    8289             : static int
    8290           0 : bdev_nvme_copy(struct nvme_bdev_io *bio, uint64_t dst_offset_blocks, uint64_t src_offset_blocks,
    8291             :                uint64_t num_blocks)
    8292             : {
    8293           0 :         struct spdk_nvme_scc_source_range range = {
    8294             :                 .slba = src_offset_blocks,
    8295           0 :                 .nlb = num_blocks - 1
    8296             :         };
    8297             : 
    8298           0 :         return spdk_nvme_ns_cmd_copy(bio->io_path->nvme_ns->ns,
    8299           0 :                                      bio->io_path->qpair->qpair,
    8300             :                                      &range, 1, dst_offset_blocks,
    8301             :                                      bdev_nvme_queued_done, bio);
    8302             : }
    8303             : 
    8304             : static void
    8305           0 : bdev_nvme_opts_config_json(struct spdk_json_write_ctx *w)
    8306             : {
    8307             :         const char *action;
    8308             :         uint32_t i;
    8309             : 
    8310           0 :         if (g_opts.action_on_timeout == SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET) {
    8311           0 :                 action = "reset";
    8312           0 :         } else if (g_opts.action_on_timeout == SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT) {
    8313           0 :                 action = "abort";
    8314             :         } else {
    8315           0 :                 action = "none";
    8316             :         }
    8317             : 
    8318           0 :         spdk_json_write_object_begin(w);
    8319             : 
    8320           0 :         spdk_json_write_named_string(w, "method", "bdev_nvme_set_options");
    8321             : 
    8322           0 :         spdk_json_write_named_object_begin(w, "params");
    8323           0 :         spdk_json_write_named_string(w, "action_on_timeout", action);
    8324           0 :         spdk_json_write_named_uint64(w, "timeout_us", g_opts.timeout_us);
    8325           0 :         spdk_json_write_named_uint64(w, "timeout_admin_us", g_opts.timeout_admin_us);
    8326           0 :         spdk_json_write_named_uint32(w, "keep_alive_timeout_ms", g_opts.keep_alive_timeout_ms);
    8327           0 :         spdk_json_write_named_uint32(w, "arbitration_burst", g_opts.arbitration_burst);
    8328           0 :         spdk_json_write_named_uint32(w, "low_priority_weight", g_opts.low_priority_weight);
    8329           0 :         spdk_json_write_named_uint32(w, "medium_priority_weight", g_opts.medium_priority_weight);
    8330           0 :         spdk_json_write_named_uint32(w, "high_priority_weight", g_opts.high_priority_weight);
    8331           0 :         spdk_json_write_named_uint64(w, "nvme_adminq_poll_period_us", g_opts.nvme_adminq_poll_period_us);
    8332           0 :         spdk_json_write_named_uint64(w, "nvme_ioq_poll_period_us", g_opts.nvme_ioq_poll_period_us);
    8333           0 :         spdk_json_write_named_uint32(w, "io_queue_requests", g_opts.io_queue_requests);
    8334           0 :         spdk_json_write_named_bool(w, "delay_cmd_submit", g_opts.delay_cmd_submit);
    8335           0 :         spdk_json_write_named_uint32(w, "transport_retry_count", g_opts.transport_retry_count);
    8336           0 :         spdk_json_write_named_int32(w, "bdev_retry_count", g_opts.bdev_retry_count);
    8337           0 :         spdk_json_write_named_uint8(w, "transport_ack_timeout", g_opts.transport_ack_timeout);
    8338           0 :         spdk_json_write_named_int32(w, "ctrlr_loss_timeout_sec", g_opts.ctrlr_loss_timeout_sec);
    8339           0 :         spdk_json_write_named_uint32(w, "reconnect_delay_sec", g_opts.reconnect_delay_sec);
    8340           0 :         spdk_json_write_named_uint32(w, "fast_io_fail_timeout_sec", g_opts.fast_io_fail_timeout_sec);
    8341           0 :         spdk_json_write_named_bool(w, "disable_auto_failback", g_opts.disable_auto_failback);
    8342           0 :         spdk_json_write_named_bool(w, "generate_uuids", g_opts.generate_uuids);
    8343           0 :         spdk_json_write_named_uint8(w, "transport_tos", g_opts.transport_tos);
    8344           0 :         spdk_json_write_named_bool(w, "nvme_error_stat", g_opts.nvme_error_stat);
    8345           0 :         spdk_json_write_named_uint32(w, "rdma_srq_size", g_opts.rdma_srq_size);
    8346           0 :         spdk_json_write_named_bool(w, "io_path_stat", g_opts.io_path_stat);
    8347           0 :         spdk_json_write_named_bool(w, "allow_accel_sequence", g_opts.allow_accel_sequence);
    8348           0 :         spdk_json_write_named_uint32(w, "rdma_max_cq_size", g_opts.rdma_max_cq_size);
    8349           0 :         spdk_json_write_named_uint16(w, "rdma_cm_event_timeout_ms", g_opts.rdma_cm_event_timeout_ms);
    8350           0 :         spdk_json_write_named_array_begin(w, "dhchap_digests");
    8351           0 :         for (i = 0; i < 32; ++i) {
    8352           0 :                 if (g_opts.dhchap_digests & SPDK_BIT(i)) {
    8353           0 :                         spdk_json_write_string(w, spdk_nvme_dhchap_get_digest_name(i));
    8354             :                 }
    8355             :         }
    8356           0 :         spdk_json_write_array_end(w);
    8357           0 :         spdk_json_write_named_array_begin(w, "dhchap_dhgroups");
    8358           0 :         for (i = 0; i < 32; ++i) {
    8359           0 :                 if (g_opts.dhchap_dhgroups & SPDK_BIT(i)) {
    8360           0 :                         spdk_json_write_string(w, spdk_nvme_dhchap_get_dhgroup_name(i));
    8361             :                 }
    8362             :         }
    8363             : 
    8364           0 :         spdk_json_write_array_end(w);
    8365           0 :         spdk_json_write_object_end(w);
    8366             : 
    8367           0 :         spdk_json_write_object_end(w);
    8368           0 : }
    8369             : 
    8370             : static void
    8371           0 : bdev_nvme_discovery_config_json(struct spdk_json_write_ctx *w, struct discovery_ctx *ctx)
    8372             : {
    8373           0 :         struct spdk_nvme_transport_id trid;
    8374             : 
    8375           0 :         spdk_json_write_object_begin(w);
    8376             : 
    8377           0 :         spdk_json_write_named_string(w, "method", "bdev_nvme_start_discovery");
    8378             : 
    8379           0 :         spdk_json_write_named_object_begin(w, "params");
    8380           0 :         spdk_json_write_named_string(w, "name", ctx->name);
    8381           0 :         spdk_json_write_named_string(w, "hostnqn", ctx->hostnqn);
    8382             : 
    8383           0 :         trid = ctx->trid;
    8384           0 :         memset(trid.subnqn, 0, sizeof(trid.subnqn));
    8385           0 :         nvme_bdev_dump_trid_json(&trid, w);
    8386             : 
    8387           0 :         spdk_json_write_named_bool(w, "wait_for_attach", ctx->wait_for_attach);
    8388           0 :         spdk_json_write_named_int32(w, "ctrlr_loss_timeout_sec", ctx->bdev_opts.ctrlr_loss_timeout_sec);
    8389           0 :         spdk_json_write_named_uint32(w, "reconnect_delay_sec", ctx->bdev_opts.reconnect_delay_sec);
    8390           0 :         spdk_json_write_named_uint32(w, "fast_io_fail_timeout_sec",
    8391             :                                      ctx->bdev_opts.fast_io_fail_timeout_sec);
    8392           0 :         spdk_json_write_object_end(w);
    8393             : 
    8394           0 :         spdk_json_write_object_end(w);
    8395           0 : }
    8396             : 
    8397             : #ifdef SPDK_CONFIG_NVME_CUSE
    8398             : static void
    8399           0 : nvme_ctrlr_cuse_config_json(struct spdk_json_write_ctx *w,
    8400             :                             struct nvme_ctrlr *nvme_ctrlr)
    8401           0 : {
    8402           0 :         size_t cuse_name_size = 128;
    8403           0 :         char cuse_name[cuse_name_size];
    8404             : 
    8405           0 :         if (spdk_nvme_cuse_get_ctrlr_name(nvme_ctrlr->ctrlr,
    8406             :                                           cuse_name, &cuse_name_size) != 0) {
    8407           0 :                 return;
    8408             :         }
    8409             : 
    8410           0 :         spdk_json_write_object_begin(w);
    8411             : 
    8412           0 :         spdk_json_write_named_string(w, "method", "bdev_nvme_cuse_register");
    8413             : 
    8414           0 :         spdk_json_write_named_object_begin(w, "params");
    8415           0 :         spdk_json_write_named_string(w, "name", nvme_ctrlr->nbdev_ctrlr->name);
    8416           0 :         spdk_json_write_object_end(w);
    8417             : 
    8418           0 :         spdk_json_write_object_end(w);
    8419             : }
    8420             : #endif
    8421             : 
    8422             : static void
    8423           0 : nvme_ctrlr_config_json(struct spdk_json_write_ctx *w,
    8424             :                        struct nvme_ctrlr *nvme_ctrlr)
    8425             : {
    8426             :         struct spdk_nvme_transport_id   *trid;
    8427             :         const struct spdk_nvme_ctrlr_opts *opts;
    8428             : 
    8429           0 :         if (nvme_ctrlr->opts.from_discovery_service) {
    8430             :                 /* Do not emit an RPC for this - it will be implicitly
    8431             :                  * covered by a separate bdev_nvme_start_discovery or
    8432             :                  * bdev_nvme_start_mdns_discovery RPC.
    8433             :                  */
    8434           0 :                 return;
    8435             :         }
    8436             : 
    8437           0 :         trid = &nvme_ctrlr->active_path_id->trid;
    8438             : 
    8439           0 :         spdk_json_write_object_begin(w);
    8440             : 
    8441           0 :         spdk_json_write_named_string(w, "method", "bdev_nvme_attach_controller");
    8442             : 
    8443           0 :         spdk_json_write_named_object_begin(w, "params");
    8444           0 :         spdk_json_write_named_string(w, "name", nvme_ctrlr->nbdev_ctrlr->name);
    8445           0 :         nvme_bdev_dump_trid_json(trid, w);
    8446           0 :         spdk_json_write_named_bool(w, "prchk_reftag",
    8447           0 :                                    (nvme_ctrlr->opts.prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_REFTAG) != 0);
    8448           0 :         spdk_json_write_named_bool(w, "prchk_guard",
    8449           0 :                                    (nvme_ctrlr->opts.prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_GUARD) != 0);
    8450           0 :         spdk_json_write_named_int32(w, "ctrlr_loss_timeout_sec", nvme_ctrlr->opts.ctrlr_loss_timeout_sec);
    8451           0 :         spdk_json_write_named_uint32(w, "reconnect_delay_sec", nvme_ctrlr->opts.reconnect_delay_sec);
    8452           0 :         spdk_json_write_named_uint32(w, "fast_io_fail_timeout_sec",
    8453             :                                      nvme_ctrlr->opts.fast_io_fail_timeout_sec);
    8454           0 :         if (nvme_ctrlr->psk != NULL) {
    8455           0 :                 spdk_json_write_named_string(w, "psk", spdk_key_get_name(nvme_ctrlr->psk));
    8456           0 :         } else if (nvme_ctrlr->opts.psk[0] != '\0') {
    8457           0 :                 spdk_json_write_named_string(w, "psk", nvme_ctrlr->opts.psk);
    8458             :         }
    8459             : 
    8460           0 :         opts = spdk_nvme_ctrlr_get_opts(nvme_ctrlr->ctrlr);
    8461           0 :         spdk_json_write_named_string(w, "hostnqn", opts->hostnqn);
    8462           0 :         spdk_json_write_named_bool(w, "hdgst", opts->header_digest);
    8463           0 :         spdk_json_write_named_bool(w, "ddgst", opts->data_digest);
    8464           0 :         if (opts->src_addr[0] != '\0') {
    8465           0 :                 spdk_json_write_named_string(w, "hostaddr", opts->src_addr);
    8466             :         }
    8467           0 :         if (opts->src_svcid[0] != '\0') {
    8468           0 :                 spdk_json_write_named_string(w, "hostsvcid", opts->src_svcid);
    8469             :         }
    8470             : 
    8471           0 :         spdk_json_write_object_end(w);
    8472             : 
    8473           0 :         spdk_json_write_object_end(w);
    8474             : }
    8475             : 
    8476             : static void
    8477           0 : bdev_nvme_hotplug_config_json(struct spdk_json_write_ctx *w)
    8478             : {
    8479           0 :         spdk_json_write_object_begin(w);
    8480           0 :         spdk_json_write_named_string(w, "method", "bdev_nvme_set_hotplug");
    8481             : 
    8482           0 :         spdk_json_write_named_object_begin(w, "params");
    8483           0 :         spdk_json_write_named_uint64(w, "period_us", g_nvme_hotplug_poll_period_us);
    8484           0 :         spdk_json_write_named_bool(w, "enable", g_nvme_hotplug_enabled);
    8485           0 :         spdk_json_write_object_end(w);
    8486             : 
    8487           0 :         spdk_json_write_object_end(w);
    8488           0 : }
    8489             : 
    8490             : static int
    8491           0 : bdev_nvme_config_json(struct spdk_json_write_ctx *w)
    8492             : {
    8493             :         struct nvme_bdev_ctrlr  *nbdev_ctrlr;
    8494             :         struct nvme_ctrlr       *nvme_ctrlr;
    8495             :         struct discovery_ctx    *ctx;
    8496             : 
    8497           0 :         bdev_nvme_opts_config_json(w);
    8498             : 
    8499           0 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    8500             : 
    8501           0 :         TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
    8502           0 :                 TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
    8503           0 :                         nvme_ctrlr_config_json(w, nvme_ctrlr);
    8504             : 
    8505             : #ifdef SPDK_CONFIG_NVME_CUSE
    8506           0 :                         nvme_ctrlr_cuse_config_json(w, nvme_ctrlr);
    8507             : #endif
    8508             :                 }
    8509             :         }
    8510             : 
    8511           0 :         TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
    8512           0 :                 if (!ctx->from_mdns_discovery_service) {
    8513           0 :                         bdev_nvme_discovery_config_json(w, ctx);
    8514             :                 }
    8515             :         }
    8516             : 
    8517           0 :         bdev_nvme_mdns_discovery_config_json(w);
    8518             : 
    8519             :         /* Dump as last parameter to give all NVMe bdevs chance to be constructed
    8520             :          * before enabling hotplug poller.
    8521             :          */
    8522           0 :         bdev_nvme_hotplug_config_json(w);
    8523             : 
    8524           0 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    8525           0 :         return 0;
    8526             : }
    8527             : 
    8528             : struct spdk_nvme_ctrlr *
    8529           1 : bdev_nvme_get_ctrlr(struct spdk_bdev *bdev)
    8530             : {
    8531             :         struct nvme_bdev *nbdev;
    8532             :         struct nvme_ns *nvme_ns;
    8533             : 
    8534           1 :         if (!bdev || bdev->module != &nvme_if) {
    8535           0 :                 return NULL;
    8536             :         }
    8537             : 
    8538           1 :         nbdev = SPDK_CONTAINEROF(bdev, struct nvme_bdev, disk);
    8539           1 :         nvme_ns = TAILQ_FIRST(&nbdev->nvme_ns_list);
    8540           1 :         assert(nvme_ns != NULL);
    8541             : 
    8542           1 :         return nvme_ns->ctrlr->ctrlr;
    8543             : }
    8544             : 
    8545             : static bool
    8546          12 : nvme_io_path_is_current(struct nvme_io_path *io_path)
    8547             : {
    8548             :         const struct nvme_bdev_channel *nbdev_ch;
    8549             :         bool current;
    8550             : 
    8551          12 :         if (!nvme_io_path_is_available(io_path)) {
    8552           4 :                 return false;
    8553             :         }
    8554             : 
    8555           8 :         nbdev_ch = io_path->nbdev_ch;
    8556           8 :         if (nbdev_ch == NULL) {
    8557           1 :                 current = false;
    8558           7 :         } else if (nbdev_ch->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE) {
    8559           3 :                 struct nvme_io_path *optimized_io_path = NULL;
    8560             : 
    8561           6 :                 STAILQ_FOREACH(optimized_io_path, &nbdev_ch->io_path_list, stailq) {
    8562           5 :                         if (optimized_io_path->nvme_ns->ana_state == SPDK_NVME_ANA_OPTIMIZED_STATE) {
    8563           2 :                                 break;
    8564             :                         }
    8565             :                 }
    8566             : 
    8567             :                 /* A non-optimized path is only current if there are no optimized paths. */
    8568           3 :                 current = (io_path->nvme_ns->ana_state == SPDK_NVME_ANA_OPTIMIZED_STATE) ||
    8569             :                           (optimized_io_path == NULL);
    8570             :         } else {
    8571           4 :                 if (nbdev_ch->current_io_path) {
    8572           1 :                         current = (io_path == nbdev_ch->current_io_path);
    8573             :                 } else {
    8574             :                         struct nvme_io_path *first_path;
    8575             : 
    8576             :                         /* We arrived here as there are no optimized paths for active-passive
    8577             :                          * mode. Check if this io_path is the first one available on the list.
    8578             :                          */
    8579           3 :                         current = false;
    8580           3 :                         STAILQ_FOREACH(first_path, &nbdev_ch->io_path_list, stailq) {
    8581           3 :                                 if (nvme_io_path_is_available(first_path)) {
    8582           3 :                                         current = (io_path == first_path);
    8583           3 :                                         break;
    8584             :                                 }
    8585             :                         }
    8586             :                 }
    8587             :         }
    8588             : 
    8589           8 :         return current;
    8590             : }
    8591             : 
    8592             : void
    8593           0 : nvme_io_path_info_json(struct spdk_json_write_ctx *w, struct nvme_io_path *io_path)
    8594             : {
    8595           0 :         struct nvme_ns *nvme_ns = io_path->nvme_ns;
    8596           0 :         struct nvme_ctrlr *nvme_ctrlr = io_path->qpair->ctrlr;
    8597             :         const struct spdk_nvme_ctrlr_data *cdata;
    8598             :         const struct spdk_nvme_transport_id *trid;
    8599             :         const char *adrfam_str;
    8600             : 
    8601           0 :         spdk_json_write_object_begin(w);
    8602             : 
    8603           0 :         spdk_json_write_named_string(w, "bdev_name", nvme_ns->bdev->disk.name);
    8604             : 
    8605           0 :         cdata = spdk_nvme_ctrlr_get_data(nvme_ctrlr->ctrlr);
    8606           0 :         trid = spdk_nvme_ctrlr_get_transport_id(nvme_ctrlr->ctrlr);
    8607             : 
    8608           0 :         spdk_json_write_named_uint32(w, "cntlid", cdata->cntlid);
    8609           0 :         spdk_json_write_named_bool(w, "current", nvme_io_path_is_current(io_path));
    8610           0 :         spdk_json_write_named_bool(w, "connected", nvme_qpair_is_connected(io_path->qpair));
    8611           0 :         spdk_json_write_named_bool(w, "accessible", nvme_ns_is_accessible(nvme_ns));
    8612             : 
    8613           0 :         spdk_json_write_named_object_begin(w, "transport");
    8614           0 :         spdk_json_write_named_string(w, "trtype", trid->trstring);
    8615           0 :         spdk_json_write_named_string(w, "traddr", trid->traddr);
    8616           0 :         if (trid->trsvcid[0] != '\0') {
    8617           0 :                 spdk_json_write_named_string(w, "trsvcid", trid->trsvcid);
    8618             :         }
    8619           0 :         adrfam_str = spdk_nvme_transport_id_adrfam_str(trid->adrfam);
    8620           0 :         if (adrfam_str) {
    8621           0 :                 spdk_json_write_named_string(w, "adrfam", adrfam_str);
    8622             :         }
    8623           0 :         spdk_json_write_object_end(w);
    8624             : 
    8625           0 :         spdk_json_write_object_end(w);
    8626           0 : }
    8627             : 
    8628             : void
    8629           0 : bdev_nvme_get_discovery_info(struct spdk_json_write_ctx *w)
    8630             : {
    8631             :         struct discovery_ctx *ctx;
    8632             :         struct discovery_entry_ctx *entry_ctx;
    8633             : 
    8634           0 :         spdk_json_write_array_begin(w);
    8635           0 :         TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
    8636           0 :                 spdk_json_write_object_begin(w);
    8637           0 :                 spdk_json_write_named_string(w, "name", ctx->name);
    8638             : 
    8639           0 :                 spdk_json_write_named_object_begin(w, "trid");
    8640           0 :                 nvme_bdev_dump_trid_json(&ctx->trid, w);
    8641           0 :                 spdk_json_write_object_end(w);
    8642             : 
    8643           0 :                 spdk_json_write_named_array_begin(w, "referrals");
    8644           0 :                 TAILQ_FOREACH(entry_ctx, &ctx->discovery_entry_ctxs, tailq) {
    8645           0 :                         spdk_json_write_object_begin(w);
    8646           0 :                         spdk_json_write_named_object_begin(w, "trid");
    8647           0 :                         nvme_bdev_dump_trid_json(&entry_ctx->trid, w);
    8648           0 :                         spdk_json_write_object_end(w);
    8649           0 :                         spdk_json_write_object_end(w);
    8650             :                 }
    8651           0 :                 spdk_json_write_array_end(w);
    8652             : 
    8653           0 :                 spdk_json_write_object_end(w);
    8654             :         }
    8655           0 :         spdk_json_write_array_end(w);
    8656           0 : }
    8657             : 
    8658           1 : SPDK_LOG_REGISTER_COMPONENT(bdev_nvme)
    8659             : 
    8660           1 : SPDK_TRACE_REGISTER_FN(bdev_nvme_trace, "bdev_nvme", TRACE_GROUP_BDEV_NVME)
    8661             : {
    8662           0 :         struct spdk_trace_tpoint_opts opts[] = {
    8663             :                 {
    8664             :                         "BDEV_NVME_IO_START", TRACE_BDEV_NVME_IO_START,
    8665             :                         OWNER_TYPE_NONE, OBJECT_BDEV_NVME_IO, 1,
    8666             :                         {{ "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 }}
    8667             :                 },
    8668             :                 {
    8669             :                         "BDEV_NVME_IO_DONE", TRACE_BDEV_NVME_IO_DONE,
    8670             :                         OWNER_TYPE_NONE, OBJECT_BDEV_NVME_IO, 0,
    8671             :                         {{ "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 }}
    8672             :                 }
    8673             :         };
    8674             : 
    8675             : 
    8676           0 :         spdk_trace_register_object(OBJECT_BDEV_NVME_IO, 'N');
    8677           0 :         spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts));
    8678           0 :         spdk_trace_tpoint_register_relation(TRACE_NVME_PCIE_SUBMIT, OBJECT_BDEV_NVME_IO, 0);
    8679           0 :         spdk_trace_tpoint_register_relation(TRACE_NVME_TCP_SUBMIT, OBJECT_BDEV_NVME_IO, 0);
    8680           0 :         spdk_trace_tpoint_register_relation(TRACE_NVME_PCIE_COMPLETE, OBJECT_BDEV_NVME_IO, 0);
    8681           0 :         spdk_trace_tpoint_register_relation(TRACE_NVME_TCP_COMPLETE, OBJECT_BDEV_NVME_IO, 0);
    8682           0 : }

Generated by: LCOV version 1.15