LCOV - code coverage report
Current view: top level - spdk/module/bdev/nvme - bdev_nvme.c (source / functions) Hit Total Coverage
Test: Combined Lines: 3975 5188 76.6 %
Date: 2024-11-15 14:56:24 Functions: 304 330 92.1 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 3588 19526 18.4 %

           Branch data     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 CTRLR_STRING(nvme_ctrlr) \
      36                 :            :         (spdk_nvme_trtype_is_fabrics(nvme_ctrlr->active_path_id->trid.trtype) ? \
      37                 :            :         nvme_ctrlr->active_path_id->trid.subnqn : nvme_ctrlr->active_path_id->trid.traddr)
      38                 :            : 
      39                 :            : #define CTRLR_ID(nvme_ctrlr)    (spdk_nvme_ctrlr_get_id(nvme_ctrlr->ctrlr))
      40                 :            : 
      41                 :            : #define NVME_CTRLR_ERRLOG(ctrlr, format, ...) \
      42                 :            :         SPDK_ERRLOG("[%s, %u] " format, CTRLR_STRING(ctrlr), CTRLR_ID(ctrlr), ##__VA_ARGS__);
      43                 :            : 
      44                 :            : #define NVME_CTRLR_WARNLOG(ctrlr, format, ...) \
      45                 :            :         SPDK_WARNLOG("[%s, %u] " format, CTRLR_STRING(ctrlr), CTRLR_ID(ctrlr), ##__VA_ARGS__);
      46                 :            : 
      47                 :            : #define NVME_CTRLR_NOTICELOG(ctrlr, format, ...) \
      48                 :            :         SPDK_NOTICELOG("[%s, %u] " format, CTRLR_STRING(ctrlr), CTRLR_ID(ctrlr), ##__VA_ARGS__);
      49                 :            : 
      50                 :            : #define NVME_CTRLR_INFOLOG(ctrlr, format, ...) \
      51                 :            :         SPDK_INFOLOG(bdev_nvme, "[%s, %u] " format, CTRLR_STRING(ctrlr), CTRLR_ID(ctrlr), ##__VA_ARGS__);
      52                 :            : 
      53                 :            : #ifdef DEBUG
      54                 :            : #define NVME_CTRLR_DEBUGLOG(ctrlr, format, ...) \
      55                 :            :         SPDK_DEBUGLOG(bdev_nvme, "[%s, %u] " format, CTRLR_STRING(ctrlr), CTRLR_ID(ctrlr), ##__VA_ARGS__);
      56                 :            : #else
      57                 :            : #define NVME_CTRLR_DEBUGLOG(ctrlr, ...) do { } while (0)
      58                 :            : #endif
      59                 :            : 
      60                 :            : #define BDEV_STRING(nbdev) (nbdev->disk.name)
      61                 :            : 
      62                 :            : #define NVME_BDEV_ERRLOG(nbdev, format, ...) \
      63                 :            :         SPDK_ERRLOG("[%s] " format, BDEV_STRING(nbdev), ##__VA_ARGS__);
      64                 :            : 
      65                 :            : #define NVME_BDEV_WARNLOG(nbdev, format, ...) \
      66                 :            :         SPDK_WARNLOG("[%s] " format, BDEV_STRING(nbdev), ##__VA_ARGS__);
      67                 :            : 
      68                 :            : #define NVME_BDEV_NOTICELOG(nbdev, format, ...) \
      69                 :            :         SPDK_NOTICELOG("[%s] " format, BDEV_STRING(nbdev), ##__VA_ARGS__);
      70                 :            : 
      71                 :            : #define NVME_BDEV_INFOLOG(nbdev, format, ...) \
      72                 :            :         SPDK_INFOLOG(bdev_nvme, "[%s] " format, BDEV_STRING(nbdev), ##__VA_ARGS__);
      73                 :            : 
      74                 :            : #define SPDK_BDEV_NVME_DEFAULT_DELAY_CMD_SUBMIT true
      75                 :            : #define SPDK_BDEV_NVME_DEFAULT_KEEP_ALIVE_TIMEOUT_IN_MS (10000)
      76                 :            : 
      77                 :            : #define NSID_STR_LEN 10
      78                 :            : 
      79                 :            : #define SPDK_CONTROLLER_NAME_MAX 512
      80                 :            : 
      81                 :            : static int bdev_nvme_config_json(struct spdk_json_write_ctx *w);
      82                 :            : 
      83                 :            : struct nvme_bdev_io {
      84                 :            :         /** array of iovecs to transfer. */
      85                 :            :         struct iovec *iovs;
      86                 :            : 
      87                 :            :         /** Number of iovecs in iovs array. */
      88                 :            :         int iovcnt;
      89                 :            : 
      90                 :            :         /** Current iovec position. */
      91                 :            :         int iovpos;
      92                 :            : 
      93                 :            :         /** Offset in current iovec. */
      94                 :            :         uint32_t iov_offset;
      95                 :            : 
      96                 :            :         /** Offset in current iovec. */
      97                 :            :         uint32_t fused_iov_offset;
      98                 :            : 
      99                 :            :         /** array of iovecs to transfer. */
     100                 :            :         struct iovec *fused_iovs;
     101                 :            : 
     102                 :            :         /** Number of iovecs in iovs array. */
     103                 :            :         int fused_iovcnt;
     104                 :            : 
     105                 :            :         /** Current iovec position. */
     106                 :            :         int fused_iovpos;
     107                 :            : 
     108                 :            :         /** I/O path the current I/O or admin passthrough is submitted on, or the I/O path
     109                 :            :          *  being reset in a reset I/O.
     110                 :            :          */
     111                 :            :         struct nvme_io_path *io_path;
     112                 :            : 
     113                 :            :         /** Saved status for admin passthru completion event, PI error verification, or intermediate compare-and-write status */
     114                 :            :         struct spdk_nvme_cpl cpl;
     115                 :            : 
     116                 :            :         /** Extended IO opts passed by the user to bdev layer and mapped to NVME format */
     117                 :            :         struct spdk_nvme_ns_cmd_ext_io_opts ext_opts;
     118                 :            : 
     119                 :            :         /** Keeps track if first of fused commands was submitted */
     120                 :            :         bool first_fused_submitted;
     121                 :            : 
     122                 :            :         /** Keeps track if first of fused commands was completed */
     123                 :            :         bool first_fused_completed;
     124                 :            : 
     125                 :            :         /* How many times the current I/O was retried. */
     126                 :            :         int32_t retry_count;
     127                 :            : 
     128                 :            :         /** Expiration value in ticks to retry the current I/O. */
     129                 :            :         uint64_t retry_ticks;
     130                 :            : 
     131                 :            :         /** Temporary pointer to zone report buffer */
     132                 :            :         struct spdk_nvme_zns_zone_report *zone_report_buf;
     133                 :            : 
     134                 :            :         /** Keep track of how many zones that have been copied to the spdk_bdev_zone_info struct */
     135                 :            :         uint64_t handled_zones;
     136                 :            : 
     137                 :            :         /* Current tsc at submit time. */
     138                 :            :         uint64_t submit_tsc;
     139                 :            : 
     140                 :            :         /* Used to put nvme_bdev_io into the list */
     141                 :            :         TAILQ_ENTRY(nvme_bdev_io) retry_link;
     142                 :            : };
     143                 :            : 
     144                 :            : struct nvme_probe_skip_entry {
     145                 :            :         struct spdk_nvme_transport_id           trid;
     146                 :            :         TAILQ_ENTRY(nvme_probe_skip_entry)      tailq;
     147                 :            : };
     148                 :            : /* All the controllers deleted by users via RPC are skipped by hotplug monitor */
     149                 :            : static TAILQ_HEAD(, nvme_probe_skip_entry) g_skipped_nvme_ctrlrs = TAILQ_HEAD_INITIALIZER(
     150                 :            :                         g_skipped_nvme_ctrlrs);
     151                 :            : 
     152                 :            : #define BDEV_NVME_DEFAULT_DIGESTS (SPDK_BIT(SPDK_NVMF_DHCHAP_HASH_SHA256) | \
     153                 :            :                                    SPDK_BIT(SPDK_NVMF_DHCHAP_HASH_SHA384) | \
     154                 :            :                                    SPDK_BIT(SPDK_NVMF_DHCHAP_HASH_SHA512))
     155                 :            : 
     156                 :            : #define BDEV_NVME_DEFAULT_DHGROUPS (SPDK_BIT(SPDK_NVMF_DHCHAP_DHGROUP_NULL) | \
     157                 :            :                                     SPDK_BIT(SPDK_NVMF_DHCHAP_DHGROUP_2048) | \
     158                 :            :                                     SPDK_BIT(SPDK_NVMF_DHCHAP_DHGROUP_3072) | \
     159                 :            :                                     SPDK_BIT(SPDK_NVMF_DHCHAP_DHGROUP_4096) | \
     160                 :            :                                     SPDK_BIT(SPDK_NVMF_DHCHAP_DHGROUP_6144) | \
     161                 :            :                                     SPDK_BIT(SPDK_NVMF_DHCHAP_DHGROUP_8192))
     162                 :            : 
     163                 :            : static struct spdk_bdev_nvme_opts g_opts = {
     164                 :            :         .action_on_timeout = SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE,
     165                 :            :         .timeout_us = 0,
     166                 :            :         .timeout_admin_us = 0,
     167                 :            :         .keep_alive_timeout_ms = SPDK_BDEV_NVME_DEFAULT_KEEP_ALIVE_TIMEOUT_IN_MS,
     168                 :            :         .transport_retry_count = 4,
     169                 :            :         .arbitration_burst = 0,
     170                 :            :         .low_priority_weight = 0,
     171                 :            :         .medium_priority_weight = 0,
     172                 :            :         .high_priority_weight = 0,
     173                 :            :         .nvme_adminq_poll_period_us = 10000ULL,
     174                 :            :         .nvme_ioq_poll_period_us = 0,
     175                 :            :         .io_queue_requests = 0,
     176                 :            :         .delay_cmd_submit = SPDK_BDEV_NVME_DEFAULT_DELAY_CMD_SUBMIT,
     177                 :            :         .bdev_retry_count = 3,
     178                 :            :         .transport_ack_timeout = 0,
     179                 :            :         .ctrlr_loss_timeout_sec = 0,
     180                 :            :         .reconnect_delay_sec = 0,
     181                 :            :         .fast_io_fail_timeout_sec = 0,
     182                 :            :         .disable_auto_failback = false,
     183                 :            :         .generate_uuids = false,
     184                 :            :         .transport_tos = 0,
     185                 :            :         .nvme_error_stat = false,
     186                 :            :         .io_path_stat = false,
     187                 :            :         .allow_accel_sequence = false,
     188                 :            :         .dhchap_digests = BDEV_NVME_DEFAULT_DIGESTS,
     189                 :            :         .dhchap_dhgroups = BDEV_NVME_DEFAULT_DHGROUPS,
     190                 :            : };
     191                 :            : 
     192                 :            : #define NVME_HOTPLUG_POLL_PERIOD_MAX                    10000000ULL
     193                 :            : #define NVME_HOTPLUG_POLL_PERIOD_DEFAULT                100000ULL
     194                 :            : 
     195                 :            : static int g_hot_insert_nvme_controller_index = 0;
     196                 :            : static uint64_t g_nvme_hotplug_poll_period_us = NVME_HOTPLUG_POLL_PERIOD_DEFAULT;
     197                 :            : static bool g_nvme_hotplug_enabled = false;
     198                 :            : struct spdk_thread *g_bdev_nvme_init_thread;
     199                 :            : static struct spdk_poller *g_hotplug_poller;
     200                 :            : static struct spdk_poller *g_hotplug_probe_poller;
     201                 :            : static struct spdk_nvme_probe_ctx *g_hotplug_probe_ctx;
     202                 :            : 
     203                 :            : static void nvme_ctrlr_populate_namespaces(struct nvme_ctrlr *nvme_ctrlr,
     204                 :            :                 struct nvme_async_probe_ctx *ctx);
     205                 :            : static void nvme_ctrlr_populate_namespaces_done(struct nvme_ctrlr *nvme_ctrlr,
     206                 :            :                 struct nvme_async_probe_ctx *ctx);
     207                 :            : static int bdev_nvme_library_init(void);
     208                 :            : static void bdev_nvme_library_fini(void);
     209                 :            : static void _bdev_nvme_submit_request(struct nvme_bdev_channel *nbdev_ch,
     210                 :            :                                       struct spdk_bdev_io *bdev_io);
     211                 :            : static void bdev_nvme_submit_request(struct spdk_io_channel *ch,
     212                 :            :                                      struct spdk_bdev_io *bdev_io);
     213                 :            : static int bdev_nvme_readv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
     214                 :            :                            void *md, uint64_t lba_count, uint64_t lba,
     215                 :            :                            uint32_t flags, struct spdk_memory_domain *domain, void *domain_ctx,
     216                 :            :                            struct spdk_accel_sequence *seq);
     217                 :            : static int bdev_nvme_no_pi_readv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
     218                 :            :                                  void *md, uint64_t lba_count, uint64_t lba);
     219                 :            : static int bdev_nvme_writev(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
     220                 :            :                             void *md, uint64_t lba_count, uint64_t lba,
     221                 :            :                             uint32_t flags, struct spdk_memory_domain *domain, void *domain_ctx,
     222                 :            :                             struct spdk_accel_sequence *seq,
     223                 :            :                             union spdk_bdev_nvme_cdw12 cdw12, union spdk_bdev_nvme_cdw13 cdw13);
     224                 :            : static int bdev_nvme_zone_appendv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
     225                 :            :                                   void *md, uint64_t lba_count,
     226                 :            :                                   uint64_t zslba, uint32_t flags);
     227                 :            : static int bdev_nvme_comparev(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
     228                 :            :                               void *md, uint64_t lba_count, uint64_t lba,
     229                 :            :                               uint32_t flags);
     230                 :            : static int bdev_nvme_comparev_and_writev(struct nvme_bdev_io *bio,
     231                 :            :                 struct iovec *cmp_iov, int cmp_iovcnt, struct iovec *write_iov,
     232                 :            :                 int write_iovcnt, void *md, uint64_t lba_count, uint64_t lba,
     233                 :            :                 uint32_t flags);
     234                 :            : static int bdev_nvme_get_zone_info(struct nvme_bdev_io *bio, uint64_t zone_id,
     235                 :            :                                    uint32_t num_zones, struct spdk_bdev_zone_info *info);
     236                 :            : static int bdev_nvme_zone_management(struct nvme_bdev_io *bio, uint64_t zone_id,
     237                 :            :                                      enum spdk_bdev_zone_action action);
     238                 :            : static void bdev_nvme_admin_passthru(struct nvme_bdev_channel *nbdev_ch,
     239                 :            :                                      struct nvme_bdev_io *bio,
     240                 :            :                                      struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes);
     241                 :            : static int bdev_nvme_io_passthru(struct nvme_bdev_io *bio, struct spdk_nvme_cmd *cmd,
     242                 :            :                                  void *buf, size_t nbytes);
     243                 :            : static int bdev_nvme_io_passthru_md(struct nvme_bdev_io *bio, struct spdk_nvme_cmd *cmd,
     244                 :            :                                     void *buf, size_t nbytes, void *md_buf, size_t md_len);
     245                 :            : static int bdev_nvme_iov_passthru_md(struct nvme_bdev_io *bio, struct spdk_nvme_cmd *cmd,
     246                 :            :                                      struct iovec *iov, int iovcnt, size_t nbytes,
     247                 :            :                                      void *md_buf, size_t md_len);
     248                 :            : static void bdev_nvme_abort(struct nvme_bdev_channel *nbdev_ch,
     249                 :            :                             struct nvme_bdev_io *bio, struct nvme_bdev_io *bio_to_abort);
     250                 :            : static void bdev_nvme_reset_io(struct nvme_bdev *nbdev, struct nvme_bdev_io *bio);
     251                 :            : static int bdev_nvme_reset_ctrlr(struct nvme_ctrlr *nvme_ctrlr);
     252                 :            : static int bdev_nvme_failover_ctrlr(struct nvme_ctrlr *nvme_ctrlr);
     253                 :            : static void remove_cb(void *cb_ctx, struct spdk_nvme_ctrlr *ctrlr);
     254                 :            : static int nvme_ctrlr_read_ana_log_page(struct nvme_ctrlr *nvme_ctrlr);
     255                 :            : 
     256                 :            : static struct nvme_ns *nvme_ns_alloc(void);
     257                 :            : static void nvme_ns_free(struct nvme_ns *ns);
     258                 :            : 
     259                 :            : static int
     260                 :       1677 : nvme_ns_cmp(struct nvme_ns *ns1, struct nvme_ns *ns2)
     261                 :            : {
     262   [ +  +  +  -  :       1677 :         return ns1->id < ns2->id ? -1 : ns1->id > ns2->id;
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
     263                 :            : }
     264                 :            : 
     265   [ +  +  +  +  :      14750 : RB_GENERATE_STATIC(nvme_ns_tree, nvme_ns, node, nvme_ns_cmp);
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  -  +  
          -  +  +  -  -  
          -  -  +  #  #  
          #  #  #  #  -  
          +  +  +  +  -  
          +  +  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  +  
          -  +  +  +  +  
          +  +  +  -  +  
          +  +  +  -  +  
          +  +  +  +  +  
          +  +  +  +  -  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  +  +  
          -  +  -  +  +  
          +  -  +  -  +  
          -  -  +  #  #  
          #  #  -  +  +  
          +  +  -  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  -  
          +  #  #  #  #  
          #  #  #  #  #  
          #  #  #  -  +  
          -  +  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          +  -  +  -  -  
          +  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          -  +  +  -  +  
          -  +  -  +  -  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     266                 :            : 
     267                 :            : struct spdk_nvme_qpair *
     268                 :          4 : bdev_nvme_get_io_qpair(struct spdk_io_channel *ctrlr_io_ch)
     269                 :            : {
     270                 :            :         struct nvme_ctrlr_channel *ctrlr_ch;
     271                 :            : 
     272   [ +  +  #  # ]:          4 :         assert(ctrlr_io_ch != NULL);
     273                 :            : 
     274                 :          4 :         ctrlr_ch = spdk_io_channel_get_ctx(ctrlr_io_ch);
     275                 :            : 
     276   [ #  #  #  #  :          4 :         return ctrlr_ch->qpair->qpair;
             #  #  #  # ]
     277                 :            : }
     278                 :            : 
     279                 :            : static int
     280                 :       1488 : bdev_nvme_get_ctx_size(void)
     281                 :            : {
     282                 :       1488 :         return sizeof(struct nvme_bdev_io);
     283                 :            : }
     284                 :            : 
     285                 :            : static struct spdk_bdev_module nvme_if = {
     286                 :            :         .name = "nvme",
     287                 :            :         .async_fini = true,
     288                 :            :         .module_init = bdev_nvme_library_init,
     289                 :            :         .module_fini = bdev_nvme_library_fini,
     290                 :            :         .config_json = bdev_nvme_config_json,
     291                 :            :         .get_ctx_size = bdev_nvme_get_ctx_size,
     292                 :            : 
     293                 :            : };
     294                 :       1665 : SPDK_BDEV_MODULE_REGISTER(nvme, &nvme_if)
     295                 :            : 
     296                 :            : struct nvme_bdev_ctrlrs g_nvme_bdev_ctrlrs = TAILQ_HEAD_INITIALIZER(g_nvme_bdev_ctrlrs);
     297                 :            : pthread_mutex_t g_bdev_nvme_mutex = PTHREAD_MUTEX_INITIALIZER;
     298                 :            : bool g_bdev_nvme_module_finish;
     299                 :            : 
     300                 :            : struct nvme_bdev_ctrlr *
     301                 :      34683 : nvme_bdev_ctrlr_get_by_name(const char *name)
     302                 :            : {
     303                 :            :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
     304                 :            : 
     305   [ +  +  #  #  :      35378 :         TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
             #  #  #  # ]
     306   [ +  +  +  +  :      31562 :                 if (strcmp(name, nbdev_ctrlr->name) == 0) {
          +  +  +  -  -  
                      + ]
     307                 :      30867 :                         break;
     308                 :            :                 }
     309                 :          0 :         }
     310                 :            : 
     311                 :      34683 :         return nbdev_ctrlr;
     312                 :            : }
     313                 :            : 
     314                 :            : static struct nvme_ctrlr *
     315                 :        412 : nvme_bdev_ctrlr_get_ctrlr(struct nvme_bdev_ctrlr *nbdev_ctrlr,
     316                 :            :                           const struct spdk_nvme_transport_id *trid, const char *hostnqn)
     317                 :            : {
     318                 :            :         const struct spdk_nvme_ctrlr_opts *opts;
     319                 :            :         struct nvme_ctrlr *nvme_ctrlr;
     320                 :            : 
     321   [ +  +  #  #  :        752 :         TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     322   [ #  #  #  # ]:        476 :                 opts = spdk_nvme_ctrlr_get_opts(nvme_ctrlr->ctrlr);
     323   [ +  +  +  -  :        476 :                 if (spdk_nvme_transport_id_compare(trid, &nvme_ctrlr->active_path_id->trid) == 0 &&
          #  #  #  #  #  
                      # ]
     324   [ +  +  -  +  :        136 :                     strcmp(hostnqn, opts->hostnqn) == 0) {
                   +  - ]
     325                 :        136 :                         break;
     326                 :            :                 }
     327                 :         41 :         }
     328                 :            : 
     329                 :        412 :         return nvme_ctrlr;
     330                 :            : }
     331                 :            : 
     332                 :            : struct nvme_ctrlr *
     333                 :          0 : nvme_bdev_ctrlr_get_ctrlr_by_id(struct nvme_bdev_ctrlr *nbdev_ctrlr,
     334                 :            :                                 uint16_t cntlid)
     335                 :            : {
     336                 :            :         struct nvme_ctrlr *nvme_ctrlr;
     337                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
     338                 :            : 
     339   [ #  #  #  #  :          0 :         TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     340   [ #  #  #  # ]:          0 :                 cdata = spdk_nvme_ctrlr_get_data(nvme_ctrlr->ctrlr);
     341   [ #  #  #  #  :          0 :                 if (cdata->cntlid == cntlid) {
                   #  # ]
     342                 :          0 :                         break;
     343                 :            :                 }
     344                 :          0 :         }
     345                 :            : 
     346                 :          0 :         return nvme_ctrlr;
     347                 :            : }
     348                 :            : 
     349                 :            : static struct nvme_bdev *
     350                 :       1100 : nvme_bdev_ctrlr_get_bdev(struct nvme_bdev_ctrlr *nbdev_ctrlr, uint32_t nsid)
     351                 :            : {
     352                 :            :         struct nvme_bdev *bdev;
     353                 :            : 
     354         [ +  + ]:       1100 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     355   [ +  +  +  -  :       1374 :         TAILQ_FOREACH(bdev, &nbdev_ctrlr->bdevs, tailq) {
          +  -  +  -  #  
             #  #  #  #  
                      # ]
     356   [ +  +  #  #  :        418 :                 if (bdev->nsid == nsid) {
                   #  # ]
     357                 :        144 :                         break;
     358                 :            :                 }
     359                 :         34 :         }
     360         [ +  + ]:       1100 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     361                 :            : 
     362                 :       1100 :         return bdev;
     363                 :            : }
     364                 :            : 
     365                 :            : struct nvme_ns *
     366                 :       2210 : nvme_ctrlr_get_ns(struct nvme_ctrlr *nvme_ctrlr, uint32_t nsid)
     367                 :            : {
     368                 :       1144 :         struct nvme_ns ns;
     369                 :            : 
     370   [ +  +  #  # ]:       2210 :         assert(nsid > 0);
     371                 :            : 
     372                 :       2210 :         ns.id = nsid;
     373         [ +  - ]:       2210 :         return RB_FIND(nvme_ns_tree, &nvme_ctrlr->namespaces, &ns);
     374                 :            : }
     375                 :            : 
     376                 :            : struct nvme_ns *
     377                 :       2198 : nvme_ctrlr_get_first_active_ns(struct nvme_ctrlr *nvme_ctrlr)
     378                 :            : {
     379         [ +  - ]:       2198 :         return RB_MIN(nvme_ns_tree, &nvme_ctrlr->namespaces);
     380                 :            : }
     381                 :            : 
     382                 :            : struct nvme_ns *
     383                 :       1080 : nvme_ctrlr_get_next_active_ns(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *ns)
     384                 :            : {
     385         [ -  + ]:       1080 :         if (ns == NULL) {
     386                 :          0 :                 return NULL;
     387                 :            :         }
     388                 :            : 
     389                 :       1080 :         return RB_NEXT(nvme_ns_tree, &nvme_ctrlr->namespaces, ns);
     390                 :         87 : }
     391                 :            : 
     392                 :            : static struct nvme_ctrlr *
     393                 :        957 : nvme_ctrlr_get(const struct spdk_nvme_transport_id *trid, const char *hostnqn)
     394                 :            : {
     395                 :            :         struct nvme_bdev_ctrlr  *nbdev_ctrlr;
     396                 :        957 :         struct nvme_ctrlr       *nvme_ctrlr = NULL;
     397                 :            : 
     398         [ +  + ]:        957 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     399   [ +  +  #  #  :       1209 :         TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
             #  #  #  # ]
     400                 :        252 :                 nvme_ctrlr = nvme_bdev_ctrlr_get_ctrlr(nbdev_ctrlr, trid, hostnqn);
     401         [ -  + ]:        252 :                 if (nvme_ctrlr != NULL) {
     402                 :          0 :                         break;
     403                 :            :                 }
     404                 :         19 :         }
     405         [ +  + ]:        957 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     406                 :            : 
     407                 :        957 :         return nvme_ctrlr;
     408                 :            : }
     409                 :            : 
     410                 :            : struct nvme_ctrlr *
     411                 :       2003 : nvme_ctrlr_get_by_name(const char *name)
     412                 :            : {
     413                 :            :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
     414                 :       2003 :         struct nvme_ctrlr *nvme_ctrlr = NULL;
     415                 :            : 
     416         [ +  + ]:       2003 :         if (name == NULL) {
     417                 :          0 :                 return NULL;
     418                 :            :         }
     419                 :            : 
     420         [ +  + ]:       2003 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     421                 :       2003 :         nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
     422         [ +  + ]:       2003 :         if (nbdev_ctrlr != NULL) {
     423   [ +  -  +  -  :        285 :                 nvme_ctrlr = TAILQ_FIRST(&nbdev_ctrlr->ctrlrs);
                   +  - ]
     424                 :         63 :         }
     425         [ +  + ]:       2003 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     426                 :            : 
     427                 :       2003 :         return nvme_ctrlr;
     428                 :        151 : }
     429                 :            : 
     430                 :            : void
     431                 :        204 : nvme_bdev_ctrlr_for_each(nvme_bdev_ctrlr_for_each_fn fn, void *ctx)
     432                 :            : {
     433                 :            :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
     434                 :            : 
     435         [ -  + ]:        204 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     436   [ +  +  #  #  :        395 :         TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
             #  #  #  # ]
     437   [ #  #  #  # ]:        191 :                 fn(nbdev_ctrlr, ctx);
     438                 :          1 :         }
     439         [ -  + ]:        204 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     440                 :        204 : }
     441                 :            : 
     442                 :            : struct nvme_ctrlr_channel_iter {
     443                 :            :         nvme_ctrlr_for_each_channel_msg fn;
     444                 :            :         nvme_ctrlr_for_each_channel_done cpl;
     445                 :            :         struct spdk_io_channel_iter *i;
     446                 :            :         void *ctx;
     447                 :            : };
     448                 :            : 
     449                 :            : void
     450                 :        777 : nvme_ctrlr_for_each_channel_continue(struct nvme_ctrlr_channel_iter *iter, int status)
     451                 :            : {
     452   [ #  #  #  # ]:        777 :         spdk_for_each_channel_continue(iter->i, status);
     453                 :        777 : }
     454                 :            : 
     455                 :            : static void
     456                 :        777 : nvme_ctrlr_each_channel_msg(struct spdk_io_channel_iter *i)
     457                 :            : {
     458                 :        777 :         struct nvme_ctrlr_channel_iter *iter = spdk_io_channel_iter_get_ctx(i);
     459                 :        777 :         struct nvme_ctrlr *nvme_ctrlr = spdk_io_channel_iter_get_io_device(i);
     460                 :        777 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
     461                 :        777 :         struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch);
     462                 :            : 
     463   [ #  #  #  # ]:        777 :         iter->i = i;
     464   [ #  #  #  #  :        777 :         iter->fn(iter, nvme_ctrlr, ctrlr_ch, iter->ctx);
          #  #  #  #  #  
                #  #  # ]
     465                 :        777 : }
     466                 :            : 
     467                 :            : static void
     468                 :        532 : nvme_ctrlr_each_channel_cpl(struct spdk_io_channel_iter *i, int status)
     469                 :            : {
     470                 :        532 :         struct nvme_ctrlr_channel_iter *iter = spdk_io_channel_iter_get_ctx(i);
     471                 :        532 :         struct nvme_ctrlr *nvme_ctrlr = spdk_io_channel_iter_get_io_device(i);
     472                 :            : 
     473   [ #  #  #  # ]:        532 :         iter->i = i;
     474   [ #  #  #  #  :        532 :         iter->cpl(nvme_ctrlr, iter->ctx, status);
          #  #  #  #  #  
                #  #  # ]
     475                 :            : 
     476                 :        532 :         free(iter);
     477                 :        532 : }
     478                 :            : 
     479                 :            : void
     480                 :        532 : nvme_ctrlr_for_each_channel(struct nvme_ctrlr *nvme_ctrlr,
     481                 :            :                             nvme_ctrlr_for_each_channel_msg fn, void *ctx,
     482                 :            :                             nvme_ctrlr_for_each_channel_done cpl)
     483                 :            : {
     484                 :            :         struct nvme_ctrlr_channel_iter *iter;
     485                 :            : 
     486   [ +  -  +  - ]:        532 :         assert(nvme_ctrlr != NULL && fn != NULL);
     487                 :            : 
     488                 :        532 :         iter = calloc(1, sizeof(struct nvme_ctrlr_channel_iter));
     489         [ -  + ]:        532 :         if (iter == NULL) {
     490                 :          0 :                 SPDK_ERRLOG("Unable to allocate iterator\n");
     491         [ #  # ]:          0 :                 assert(false);
     492                 :            :                 return;
     493                 :            :         }
     494                 :            : 
     495   [ #  #  #  # ]:        532 :         iter->fn = fn;
     496   [ #  #  #  # ]:        532 :         iter->cpl = cpl;
     497   [ #  #  #  # ]:        532 :         iter->ctx = ctx;
     498                 :            : 
     499                 :        633 :         spdk_for_each_channel(nvme_ctrlr, nvme_ctrlr_each_channel_msg,
     500                 :        101 :                               iter, nvme_ctrlr_each_channel_cpl);
     501                 :        101 : }
     502                 :            : 
     503                 :            : struct nvme_bdev_channel_iter {
     504                 :            :         nvme_bdev_for_each_channel_msg fn;
     505                 :            :         nvme_bdev_for_each_channel_done cpl;
     506                 :            :         struct spdk_io_channel_iter *i;
     507                 :            :         void *ctx;
     508                 :            : };
     509                 :            : 
     510                 :            : void
     511                 :        343 : nvme_bdev_for_each_channel_continue(struct nvme_bdev_channel_iter *iter, int status)
     512                 :            : {
     513   [ #  #  #  # ]:        343 :         spdk_for_each_channel_continue(iter->i, status);
     514                 :        343 : }
     515                 :            : 
     516                 :            : static void
     517                 :        343 : nvme_bdev_each_channel_msg(struct spdk_io_channel_iter *i)
     518                 :            : {
     519                 :        343 :         struct nvme_bdev_channel_iter *iter = spdk_io_channel_iter_get_ctx(i);
     520                 :        343 :         struct nvme_bdev *nbdev = spdk_io_channel_iter_get_io_device(i);
     521                 :        343 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
     522                 :        343 :         struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(ch);
     523                 :            : 
     524   [ #  #  #  # ]:        343 :         iter->i = i;
     525   [ #  #  #  #  :        343 :         iter->fn(iter, nbdev, nbdev_ch, iter->ctx);
          #  #  #  #  #  
                #  #  # ]
     526                 :        343 : }
     527                 :            : 
     528                 :            : static void
     529                 :        313 : nvme_bdev_each_channel_cpl(struct spdk_io_channel_iter *i, int status)
     530                 :            : {
     531                 :        313 :         struct nvme_bdev_channel_iter *iter = spdk_io_channel_iter_get_ctx(i);
     532                 :        313 :         struct nvme_bdev *nbdev = spdk_io_channel_iter_get_io_device(i);
     533                 :            : 
     534   [ #  #  #  # ]:        313 :         iter->i = i;
     535   [ #  #  #  #  :        313 :         iter->cpl(nbdev, iter->ctx, status);
          #  #  #  #  #  
                #  #  # ]
     536                 :            : 
     537                 :        313 :         free(iter);
     538                 :        313 : }
     539                 :            : 
     540                 :            : void
     541                 :        313 : nvme_bdev_for_each_channel(struct nvme_bdev *nbdev,
     542                 :            :                            nvme_bdev_for_each_channel_msg fn, void *ctx,
     543                 :            :                            nvme_bdev_for_each_channel_done cpl)
     544                 :            : {
     545                 :            :         struct nvme_bdev_channel_iter *iter;
     546                 :            : 
     547   [ +  -  +  - ]:        313 :         assert(nbdev != NULL && fn != NULL);
     548                 :            : 
     549                 :        313 :         iter = calloc(1, sizeof(struct nvme_bdev_channel_iter));
     550         [ -  + ]:        313 :         if (iter == NULL) {
     551                 :          0 :                 SPDK_ERRLOG("Unable to allocate iterator\n");
     552         [ #  # ]:          0 :                 assert(false);
     553                 :            :                 return;
     554                 :            :         }
     555                 :            : 
     556   [ #  #  #  # ]:        313 :         iter->fn = fn;
     557   [ #  #  #  # ]:        313 :         iter->cpl = cpl;
     558   [ #  #  #  # ]:        313 :         iter->ctx = ctx;
     559                 :            : 
     560                 :        313 :         spdk_for_each_channel(nbdev, nvme_bdev_each_channel_msg, iter,
     561                 :            :                               nvme_bdev_each_channel_cpl);
     562                 :         62 : }
     563                 :            : 
     564                 :            : void
     565                 :       1455 : nvme_bdev_dump_trid_json(const struct spdk_nvme_transport_id *trid, struct spdk_json_write_ctx *w)
     566                 :            : {
     567                 :            :         const char *trtype_str;
     568                 :            :         const char *adrfam_str;
     569                 :            : 
     570   [ #  #  #  # ]:       1455 :         trtype_str = spdk_nvme_transport_id_trtype_str(trid->trtype);
     571         [ +  - ]:       1455 :         if (trtype_str) {
     572                 :       1455 :                 spdk_json_write_named_string(w, "trtype", trtype_str);
     573                 :          8 :         }
     574                 :            : 
     575   [ #  #  #  # ]:       1455 :         adrfam_str = spdk_nvme_transport_id_adrfam_str(trid->adrfam);
     576         [ +  + ]:       1455 :         if (adrfam_str) {
     577                 :        460 :                 spdk_json_write_named_string(w, "adrfam", adrfam_str);
     578                 :          0 :         }
     579                 :            : 
     580   [ +  -  #  #  :       1455 :         if (trid->traddr[0] != '\0') {
          #  #  #  #  #  
                      # ]
     581         [ #  # ]:       1455 :                 spdk_json_write_named_string(w, "traddr", trid->traddr);
     582                 :          8 :         }
     583                 :            : 
     584   [ +  +  #  #  :       1455 :         if (trid->trsvcid[0] != '\0') {
          #  #  #  #  #  
                      # ]
     585         [ #  # ]:        460 :                 spdk_json_write_named_string(w, "trsvcid", trid->trsvcid);
     586                 :          0 :         }
     587                 :            : 
     588   [ +  +  #  #  :       1455 :         if (trid->subnqn[0] != '\0') {
          #  #  #  #  #  
                      # ]
     589         [ #  # ]:        460 :                 spdk_json_write_named_string(w, "subnqn", trid->subnqn);
     590                 :          0 :         }
     591                 :       1455 : }
     592                 :            : 
     593                 :            : static void
     594                 :       1023 : nvme_bdev_ctrlr_delete(struct nvme_bdev_ctrlr *nbdev_ctrlr,
     595                 :            :                        struct nvme_ctrlr *nvme_ctrlr)
     596                 :            : {
     597                 :        261 :         SPDK_DTRACE_PROBE1(bdev_nvme_ctrlr_delete, nvme_ctrlr->nbdev_ctrlr->name);
     598         [ +  + ]:       1023 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     599                 :            : 
     600   [ +  +  +  -  :       1023 :         TAILQ_REMOVE(&nbdev_ctrlr->ctrlrs, nvme_ctrlr, tailq);
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
     601   [ +  +  +  -  :       1023 :         if (!TAILQ_EMPTY(&nbdev_ctrlr->ctrlrs)) {
             +  -  -  + ]
     602         [ -  + ]:         63 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
     603                 :            : 
     604                 :         63 :                 return;
     605                 :            :         }
     606   [ +  +  +  -  :        960 :         TAILQ_REMOVE(&g_nvme_bdev_ctrlrs, nbdev_ctrlr, tailq);
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     607                 :            : 
     608         [ +  + ]:        960 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     609                 :            : 
     610   [ +  +  +  -  :        960 :         assert(TAILQ_EMPTY(&nbdev_ctrlr->bdevs));
          +  -  +  -  #  
                      # ]
     611                 :            : 
     612   [ +  -  +  - ]:        960 :         free(nbdev_ctrlr->name);
     613                 :        960 :         free(nbdev_ctrlr);
     614                 :         72 : }
     615                 :            : 
     616                 :            : static void
     617                 :       1027 : _nvme_ctrlr_delete(struct nvme_ctrlr *nvme_ctrlr)
     618                 :            : {
     619                 :            :         struct nvme_path_id *path_id, *tmp_path;
     620                 :            :         struct nvme_ns *ns, *tmp_ns;
     621                 :            : 
     622   [ +  -  +  - ]:       1027 :         free(nvme_ctrlr->copied_ana_desc);
     623   [ +  -  +  - ]:       1027 :         spdk_free(nvme_ctrlr->ana_log_page);
     624                 :            : 
     625   [ +  +  +  -  :       1027 :         if (nvme_ctrlr->opal_dev) {
                   +  - ]
     626   [ #  #  #  # ]:         27 :                 spdk_opal_dev_destruct(nvme_ctrlr->opal_dev);
     627   [ #  #  #  # ]:         27 :                 nvme_ctrlr->opal_dev = NULL;
     628                 :          0 :         }
     629                 :            : 
     630   [ +  +  +  -  :       1027 :         if (nvme_ctrlr->nbdev_ctrlr) {
                   -  + ]
     631   [ +  -  +  - ]:       1023 :                 nvme_bdev_ctrlr_delete(nvme_ctrlr->nbdev_ctrlr, nvme_ctrlr);
     632                 :         72 :         }
     633                 :            : 
     634   [ +  +  +  +  :       1027 :         RB_FOREACH_SAFE(ns, nvme_ns_tree, &nvme_ctrlr->namespaces, tmp_ns) {
                   -  + ]
     635         [ #  # ]:          0 :                 RB_REMOVE(nvme_ns_tree, &nvme_ctrlr->namespaces, ns);
     636                 :          0 :                 nvme_ns_free(ns);
     637                 :          0 :         }
     638                 :            : 
     639   [ +  +  +  +  :       2056 :         TAILQ_FOREACH_SAFE(path_id, &nvme_ctrlr->trids, link, tmp_path) {
          +  -  +  +  +  
          -  +  -  +  -  
                   +  + ]
     640   [ +  +  +  -  :       1029 :                 TAILQ_REMOVE(&nvme_ctrlr->trids, path_id, link);
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
     641                 :       1029 :                 free(path_id);
     642                 :         73 :         }
     643                 :            : 
     644   [ +  +  +  - ]:       1027 :         pthread_mutex_destroy(&nvme_ctrlr->mutex);
     645   [ +  -  +  - ]:       1027 :         spdk_keyring_put_key(nvme_ctrlr->psk);
     646   [ +  -  +  - ]:       1027 :         spdk_keyring_put_key(nvme_ctrlr->dhchap_key);
     647   [ +  -  +  - ]:       1027 :         spdk_keyring_put_key(nvme_ctrlr->dhchap_ctrlr_key);
     648                 :       1027 :         free(nvme_ctrlr);
     649                 :            : 
     650         [ +  + ]:       1027 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     651   [ +  +  +  +  :       1027 :         if (g_bdev_nvme_module_finish && TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) {
                   +  + ]
     652         [ +  + ]:        388 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
     653                 :        388 :                 spdk_io_device_unregister(&g_nvme_bdev_ctrlrs, NULL);
     654                 :        388 :                 spdk_bdev_module_fini_done();
     655                 :        388 :                 return;
     656                 :            :         }
     657         [ -  + ]:        639 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     658                 :         73 : }
     659                 :            : 
     660                 :            : static int
     661                 :     175060 : nvme_detach_poller(void *arg)
     662                 :            : {
     663                 :     175060 :         struct nvme_ctrlr *nvme_ctrlr = arg;
     664                 :            :         int rc;
     665                 :            : 
     666   [ +  -  +  - ]:     175060 :         rc = spdk_nvme_detach_poll_async(nvme_ctrlr->detach_ctx);
     667         [ +  + ]:     175060 :         if (rc != -EAGAIN) {
     668         [ +  - ]:       1027 :                 spdk_poller_unregister(&nvme_ctrlr->reset_detach_poller);
     669                 :       1027 :                 _nvme_ctrlr_delete(nvme_ctrlr);
     670                 :         73 :         }
     671                 :            : 
     672                 :     175060 :         return SPDK_POLLER_BUSY;
     673                 :            : }
     674                 :            : 
     675                 :            : static void
     676                 :       1027 : nvme_ctrlr_delete(struct nvme_ctrlr *nvme_ctrlr)
     677                 :            : {
     678                 :            :         int rc;
     679                 :            : 
     680         [ +  - ]:       1027 :         spdk_poller_unregister(&nvme_ctrlr->reconnect_delay_timer);
     681                 :            : 
     682         [ +  + ]:       1027 :         if (spdk_interrupt_mode_is_enabled()) {
     683         [ #  # ]:          0 :                 spdk_interrupt_unregister(&nvme_ctrlr->intr);
     684                 :          0 :         }
     685                 :            : 
     686                 :            :         /* First, unregister the adminq poller, as the driver will poll adminq if necessary */
     687         [ +  - ]:       1027 :         spdk_poller_unregister(&nvme_ctrlr->adminq_timer_poller);
     688                 :            : 
     689                 :            :         /* If we got here, the reset/detach poller cannot be active */
     690   [ +  +  +  -  :       1027 :         assert(nvme_ctrlr->reset_detach_poller == NULL);
             +  -  #  # ]
     691   [ +  -  +  - ]:       1027 :         nvme_ctrlr->reset_detach_poller = SPDK_POLLER_REGISTER(nvme_detach_poller,
     692                 :            :                                           nvme_ctrlr, 1000);
     693   [ +  +  +  -  :       1027 :         if (nvme_ctrlr->reset_detach_poller == NULL) {
                   +  - ]
     694   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to register detach poller\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     695                 :          0 :                 goto error;
     696                 :            :         }
     697                 :            : 
     698   [ +  -  +  -  :       1027 :         rc = spdk_nvme_detach_async(nvme_ctrlr->ctrlr, &nvme_ctrlr->detach_ctx);
                   +  - ]
     699         [ -  + ]:       1027 :         if (rc != 0) {
     700   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to detach the NVMe controller\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     701                 :          0 :                 goto error;
     702                 :            :         }
     703                 :            : 
     704                 :       1027 :         return;
     705                 :          0 : error:
     706                 :            :         /* We don't have a good way to handle errors here, so just do what we can and delete the
     707                 :            :          * controller without detaching the underlying NVMe device.
     708                 :            :          */
     709         [ #  # ]:          0 :         spdk_poller_unregister(&nvme_ctrlr->reset_detach_poller);
     710                 :          0 :         _nvme_ctrlr_delete(nvme_ctrlr);
     711                 :         73 : }
     712                 :            : 
     713                 :            : static void
     714                 :       1023 : nvme_ctrlr_unregister_cb(void *io_device)
     715                 :            : {
     716                 :       1023 :         struct nvme_ctrlr *nvme_ctrlr = io_device;
     717                 :            : 
     718                 :       1023 :         nvme_ctrlr_delete(nvme_ctrlr);
     719                 :       1023 : }
     720                 :            : 
     721                 :            : static void
     722                 :       1023 : nvme_ctrlr_unregister(void *ctx)
     723                 :            : {
     724                 :       1023 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
     725                 :            : 
     726                 :       1023 :         spdk_io_device_unregister(nvme_ctrlr, nvme_ctrlr_unregister_cb);
     727                 :       1023 : }
     728                 :            : 
     729                 :            : static bool
     730                 :       4071 : nvme_ctrlr_can_be_unregistered(struct nvme_ctrlr *nvme_ctrlr)
     731                 :            : {
     732   [ +  +  +  + ]:       4071 :         if (!nvme_ctrlr->destruct) {
     733                 :       2018 :                 return false;
     734                 :            :         }
     735                 :            : 
     736   [ +  +  +  -  :       2053 :         if (nvme_ctrlr->ref > 0) {
                   +  + ]
     737                 :       1029 :                 return false;
     738                 :            :         }
     739                 :            : 
     740   [ +  +  -  + ]:       1024 :         if (nvme_ctrlr->resetting) {
     741                 :          1 :                 return false;
     742                 :            :         }
     743                 :            : 
     744   [ +  +  -  + ]:       1023 :         if (nvme_ctrlr->ana_log_page_updating) {
     745                 :          0 :                 return false;
     746                 :            :         }
     747                 :            : 
     748   [ +  +  -  + ]:       1023 :         if (nvme_ctrlr->io_path_cache_clearing) {
     749                 :          0 :                 return false;
     750                 :            :         }
     751                 :            : 
     752                 :       1023 :         return true;
     753                 :        294 : }
     754                 :            : 
     755                 :            : static void
     756                 :       3618 : nvme_ctrlr_put_ref(struct nvme_ctrlr *nvme_ctrlr)
     757                 :            : {
     758   [ +  +  +  - ]:       3618 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
     759                 :        784 :         SPDK_DTRACE_PROBE2(bdev_nvme_ctrlr_release, nvme_ctrlr->nbdev_ctrlr->name, nvme_ctrlr->ref);
     760                 :            : 
     761   [ +  +  +  -  :       3618 :         assert(nvme_ctrlr->ref > 0);
             +  -  #  # ]
     762   [ +  -  +  - ]:       3618 :         nvme_ctrlr->ref--;
     763                 :            : 
     764         [ +  + ]:       3618 :         if (!nvme_ctrlr_can_be_unregistered(nvme_ctrlr)) {
     765   [ +  +  +  - ]:       2596 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
     766                 :       2596 :                 return;
     767                 :            :         }
     768                 :            : 
     769   [ +  +  +  - ]:       1022 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
     770                 :            : 
     771   [ +  -  +  - ]:       1022 :         spdk_thread_exec_msg(nvme_ctrlr->thread, nvme_ctrlr_unregister, nvme_ctrlr);
     772                 :        215 : }
     773                 :            : 
     774                 :            : static void
     775                 :       2588 : nvme_ctrlr_get_ref(struct nvme_ctrlr *nvme_ctrlr)
     776                 :            : {
     777   [ +  +  +  - ]:       2588 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
     778   [ +  -  +  - ]:       2588 :         nvme_ctrlr->ref++;
     779   [ +  +  +  - ]:       2588 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
     780                 :       2588 : }
     781                 :            : 
     782                 :            : static void
     783                 :      55297 : bdev_nvme_clear_current_io_path(struct nvme_bdev_channel *nbdev_ch)
     784                 :            : {
     785   [ +  -  +  - ]:      55297 :         nbdev_ch->current_io_path = NULL;
     786   [ +  -  +  - ]:      55297 :         nbdev_ch->rr_counter = 0;
     787                 :      55297 : }
     788                 :            : 
     789                 :            : static struct nvme_io_path *
     790                 :         32 : _bdev_nvme_get_io_path(struct nvme_bdev_channel *nbdev_ch, struct nvme_ns *nvme_ns)
     791                 :            : {
     792                 :            :         struct nvme_io_path *io_path;
     793                 :            : 
     794   [ +  +  #  #  :         64 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     795   [ +  +  #  #  :         60 :                 if (io_path->nvme_ns == nvme_ns) {
                   #  # ]
     796                 :         28 :                         break;
     797                 :            :                 }
     798                 :          8 :         }
     799                 :            : 
     800                 :         32 :         return io_path;
     801                 :            : }
     802                 :            : 
     803                 :            : static struct nvme_io_path *
     804                 :       1606 : nvme_io_path_alloc(void)
     805                 :            : {
     806                 :            :         struct nvme_io_path *io_path;
     807                 :            : 
     808                 :       1606 :         io_path = calloc(1, sizeof(*io_path));
     809         [ +  + ]:       1606 :         if (io_path == NULL) {
     810                 :          0 :                 SPDK_ERRLOG("Failed to alloc io_path.\n");
     811                 :          0 :                 return NULL;
     812                 :            :         }
     813                 :            : 
     814   [ +  +  +  +  :       1606 :         if (g_opts.io_path_stat) {
                   +  - ]
     815   [ #  #  #  # ]:          0 :                 io_path->stat = calloc(1, sizeof(struct spdk_bdev_io_stat));
     816   [ #  #  #  #  :          0 :                 if (io_path->stat == NULL) {
                   #  # ]
     817                 :          0 :                         free(io_path);
     818                 :          0 :                         SPDK_ERRLOG("Failed to alloc io_path stat.\n");
     819                 :          0 :                         return NULL;
     820                 :            :                 }
     821   [ #  #  #  # ]:          0 :                 spdk_bdev_reset_io_stat(io_path->stat, SPDK_BDEV_RESET_STAT_MAXMIN);
     822                 :          0 :         }
     823                 :            : 
     824                 :       1606 :         return io_path;
     825                 :         60 : }
     826                 :            : 
     827                 :            : static void
     828                 :       1606 : nvme_io_path_free(struct nvme_io_path *io_path)
     829                 :            : {
     830   [ +  -  +  - ]:       1606 :         free(io_path->stat);
     831                 :       1606 :         free(io_path);
     832                 :       1606 : }
     833                 :            : 
     834                 :            : static int
     835                 :       1606 : _bdev_nvme_add_io_path(struct nvme_bdev_channel *nbdev_ch, struct nvme_ns *nvme_ns)
     836                 :            : {
     837                 :            :         struct nvme_io_path *io_path;
     838                 :            :         struct spdk_io_channel *ch;
     839                 :            :         struct nvme_ctrlr_channel *ctrlr_ch;
     840                 :            :         struct nvme_qpair *nvme_qpair;
     841                 :            : 
     842                 :       1606 :         io_path = nvme_io_path_alloc();
     843         [ +  + ]:       1606 :         if (io_path == NULL) {
     844                 :          0 :                 return -ENOMEM;
     845                 :            :         }
     846                 :            : 
     847   [ +  -  +  - ]:       1606 :         io_path->nvme_ns = nvme_ns;
     848                 :            : 
     849   [ +  -  +  - ]:       1606 :         ch = spdk_get_io_channel(nvme_ns->ctrlr);
     850         [ +  + ]:       1606 :         if (ch == NULL) {
     851                 :          0 :                 nvme_io_path_free(io_path);
     852                 :          0 :                 SPDK_ERRLOG("Failed to alloc io_channel.\n");
     853                 :          0 :                 return -ENOMEM;
     854                 :            :         }
     855                 :            : 
     856                 :       1606 :         ctrlr_ch = spdk_io_channel_get_ctx(ch);
     857                 :            : 
     858   [ +  -  +  - ]:       1606 :         nvme_qpair = ctrlr_ch->qpair;
     859   [ +  +  #  # ]:       1606 :         assert(nvme_qpair != NULL);
     860                 :            : 
     861   [ +  -  +  - ]:       1606 :         io_path->qpair = nvme_qpair;
     862   [ +  -  +  -  :       1606 :         TAILQ_INSERT_TAIL(&nvme_qpair->io_path_list, io_path, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     863                 :            : 
     864   [ +  -  +  - ]:       1606 :         io_path->nbdev_ch = nbdev_ch;
     865   [ +  -  +  -  :       1606 :         STAILQ_INSERT_TAIL(&nbdev_ch->io_path_list, io_path, stailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     866                 :            : 
     867                 :       1606 :         bdev_nvme_clear_current_io_path(nbdev_ch);
     868                 :            : 
     869                 :       1606 :         return 0;
     870                 :         60 : }
     871                 :            : 
     872                 :            : static void
     873                 :       1606 : bdev_nvme_clear_retry_io_path(struct nvme_bdev_channel *nbdev_ch,
     874                 :            :                               struct nvme_io_path *io_path)
     875                 :            : {
     876                 :            :         struct nvme_bdev_io *bio;
     877                 :            : 
     878   [ +  +  +  -  :       1610 :         TAILQ_FOREACH(bio, &nbdev_ch->retry_io_list, retry_link) {
          +  -  +  -  #  
             #  #  #  #  
                      # ]
     879   [ +  +  #  #  :          4 :                 if (bio->io_path == io_path) {
                   #  # ]
     880   [ #  #  #  # ]:          4 :                         bio->io_path = NULL;
     881                 :          1 :                 }
     882                 :          1 :         }
     883                 :       1606 : }
     884                 :            : 
     885                 :            : static void
     886                 :       1606 : _bdev_nvme_delete_io_path(struct nvme_bdev_channel *nbdev_ch, struct nvme_io_path *io_path)
     887                 :            : {
     888                 :            :         struct spdk_io_channel *ch;
     889                 :            :         struct nvme_qpair *nvme_qpair;
     890                 :            :         struct nvme_ctrlr_channel *ctrlr_ch;
     891                 :            :         struct nvme_bdev *nbdev;
     892                 :            : 
     893                 :       1606 :         nbdev = spdk_io_channel_get_io_device(spdk_io_channel_from_ctx(nbdev_ch));
     894                 :            : 
     895                 :            :         /* Add the statistics to nvme_ns before this path is destroyed. */
     896   [ +  +  +  - ]:       1606 :         pthread_mutex_lock(&nbdev->mutex);
     897   [ +  +  +  +  :       1606 :         if (nbdev->ref != 0 && io_path->nvme_ns->stat != NULL && io_path->stat != NULL) {
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  #  #  #  
                #  #  # ]
     898   [ #  #  #  #  :          0 :                 spdk_bdev_add_io_stat(io_path->nvme_ns->stat, io_path->stat);
          #  #  #  #  #  
                #  #  # ]
     899                 :          0 :         }
     900   [ +  +  +  - ]:       1606 :         pthread_mutex_unlock(&nbdev->mutex);
     901                 :            : 
     902                 :       1606 :         bdev_nvme_clear_current_io_path(nbdev_ch);
     903                 :       1606 :         bdev_nvme_clear_retry_io_path(nbdev_ch, io_path);
     904                 :            : 
     905   [ +  +  +  +  :       1608 :         STAILQ_REMOVE(&nbdev_ch->io_path_list, io_path, nvme_io_path, stailq);
          +  +  +  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     906   [ +  -  +  - ]:       1606 :         io_path->nbdev_ch = NULL;
     907                 :            : 
     908   [ +  -  +  - ]:       1606 :         nvme_qpair = io_path->qpair;
     909   [ +  +  #  # ]:       1606 :         assert(nvme_qpair != NULL);
     910                 :            : 
     911   [ +  -  +  - ]:       1606 :         ctrlr_ch = nvme_qpair->ctrlr_ch;
     912   [ +  +  #  # ]:       1606 :         assert(ctrlr_ch != NULL);
     913                 :            : 
     914                 :       1606 :         ch = spdk_io_channel_from_ctx(ctrlr_ch);
     915                 :       1606 :         spdk_put_io_channel(ch);
     916                 :            : 
     917                 :            :         /* After an io_path is removed, I/Os submitted to it may complete and update statistics
     918                 :            :          * of the io_path. To avoid heap-use-after-free error from this case, do not free the
     919                 :            :          * io_path here but free the io_path when the associated qpair is freed. It is ensured
     920                 :            :          * that all I/Os submitted to the io_path are completed when the associated qpair is freed.
     921                 :            :          */
     922                 :       1606 : }
     923                 :            : 
     924                 :            : static void
     925                 :       1552 : _bdev_nvme_delete_io_paths(struct nvme_bdev_channel *nbdev_ch)
     926                 :            : {
     927                 :            :         struct nvme_io_path *io_path, *tmp_io_path;
     928                 :            : 
     929   [ +  +  +  +  :       3150 :         STAILQ_FOREACH_SAFE(io_path, &nbdev_ch->io_path_list, stailq, tmp_io_path) {
          +  -  +  +  +  
          -  +  -  +  -  
                   +  + ]
     930                 :       1598 :                 _bdev_nvme_delete_io_path(nbdev_ch, io_path);
     931                 :         58 :         }
     932                 :       1552 : }
     933                 :            : 
     934                 :            : static int
     935                 :       1552 : bdev_nvme_create_bdev_channel_cb(void *io_device, void *ctx_buf)
     936                 :            : {
     937                 :       1552 :         struct nvme_bdev_channel *nbdev_ch = ctx_buf;
     938                 :       1552 :         struct nvme_bdev *nbdev = io_device;
     939                 :            :         struct nvme_ns *nvme_ns;
     940                 :            :         int rc;
     941                 :            : 
     942   [ +  -  +  -  :       1552 :         STAILQ_INIT(&nbdev_ch->io_path_list);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     943   [ +  -  +  -  :       1552 :         TAILQ_INIT(&nbdev_ch->retry_io_list);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     944                 :            : 
     945   [ +  +  +  - ]:       1552 :         pthread_mutex_lock(&nbdev->mutex);
     946                 :            : 
     947   [ +  -  +  -  :       1552 :         nbdev_ch->mp_policy = nbdev->mp_policy;
             +  -  +  - ]
     948   [ +  -  +  -  :       1552 :         nbdev_ch->mp_selector = nbdev->mp_selector;
             +  -  +  - ]
     949   [ +  -  +  -  :       1552 :         nbdev_ch->rr_min_io = nbdev->rr_min_io;
             +  -  +  - ]
     950                 :            : 
     951   [ +  +  +  -  :       3150 :         TAILQ_FOREACH(nvme_ns, &nbdev->nvme_ns_list, tailq) {
          +  -  +  +  +  
             -  +  -  +  
                      - ]
     952                 :       1598 :                 rc = _bdev_nvme_add_io_path(nbdev_ch, nvme_ns);
     953         [ +  + ]:       1598 :                 if (rc != 0) {
     954   [ #  #  #  # ]:          0 :                         pthread_mutex_unlock(&nbdev->mutex);
     955                 :            : 
     956                 :          0 :                         _bdev_nvme_delete_io_paths(nbdev_ch);
     957                 :          0 :                         return rc;
     958                 :            :                 }
     959                 :         58 :         }
     960   [ +  +  +  - ]:       1552 :         pthread_mutex_unlock(&nbdev->mutex);
     961                 :            : 
     962                 :       1552 :         return 0;
     963                 :         47 : }
     964                 :            : 
     965                 :            : /* If cpl != NULL, complete the bdev_io with nvme status based on 'cpl'.
     966                 :            :  * If cpl == NULL, complete the bdev_io with bdev status based on 'status'.
     967                 :            :  */
     968                 :            : static inline void
     969                 :   16704976 : __bdev_nvme_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status,
     970                 :            :                         const struct spdk_nvme_cpl *cpl)
     971                 :            : {
     972   [ +  +  +  +  :   16704976 :         spdk_trace_record(TRACE_BDEV_NVME_IO_DONE, 0, 0, (uintptr_t)bdev_io->driver_ctx,
          +  -  +  -  +  
          -  +  -  +  -  
             -  +  #  # ]
     973                 :            :                           (uintptr_t)bdev_io);
     974         [ +  + ]:   16704976 :         if (cpl) {
     975   [ +  -  +  -  :   16469397 :                 spdk_bdev_io_complete_nvme_status(bdev_io, cpl->cdw0, cpl->status.sct, cpl->status.sc);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     976                 :     680057 :         } else {
     977                 :     235351 :                 spdk_bdev_io_complete(bdev_io, status);
     978                 :            :         }
     979                 :   16704748 : }
     980                 :            : 
     981                 :            : static void bdev_nvme_abort_retry_ios(struct nvme_bdev_channel *nbdev_ch);
     982                 :            : 
     983                 :            : static void
     984                 :       1552 : bdev_nvme_destroy_bdev_channel_cb(void *io_device, void *ctx_buf)
     985                 :            : {
     986                 :       1552 :         struct nvme_bdev_channel *nbdev_ch = ctx_buf;
     987                 :            : 
     988                 :       1552 :         bdev_nvme_abort_retry_ios(nbdev_ch);
     989                 :       1552 :         _bdev_nvme_delete_io_paths(nbdev_ch);
     990                 :       1552 : }
     991                 :            : 
     992                 :            : static inline bool
     993                 :   16759485 : bdev_nvme_io_type_is_admin(enum spdk_bdev_io_type io_type)
     994                 :            : {
     995         [ +  + ]:   16759485 :         switch (io_type) {
     996                 :         64 :         case SPDK_BDEV_IO_TYPE_RESET:
     997                 :            :         case SPDK_BDEV_IO_TYPE_NVME_ADMIN:
     998                 :            :         case SPDK_BDEV_IO_TYPE_ABORT:
     999                 :         70 :                 return true;
    1000                 :   16078461 :         default:
    1001                 :   16759415 :                 break;
    1002                 :            :         }
    1003                 :            : 
    1004                 :   16759415 :         return false;
    1005                 :     680960 : }
    1006                 :            : 
    1007                 :            : static inline bool
    1008                 :    3866122 : nvme_ns_is_active(struct nvme_ns *nvme_ns)
    1009                 :            : {
    1010   [ +  +  +  +  :    3866122 :         if (spdk_unlikely(nvme_ns->ana_state_updating)) {
             +  -  -  + ]
    1011                 :        999 :                 return false;
    1012                 :            :         }
    1013                 :            : 
    1014   [ +  +  +  -  :    3865123 :         if (spdk_unlikely(nvme_ns->ns == NULL)) {
                   -  + ]
    1015                 :          0 :                 return false;
    1016                 :            :         }
    1017                 :            : 
    1018                 :    3865123 :         return true;
    1019                 :     306822 : }
    1020                 :            : 
    1021                 :            : static inline bool
    1022                 :    3866047 : nvme_ns_is_accessible(struct nvme_ns *nvme_ns)
    1023                 :            : {
    1024         [ +  + ]:    3866047 :         if (spdk_unlikely(!nvme_ns_is_active(nvme_ns))) {
    1025                 :        999 :                 return false;
    1026                 :            :         }
    1027                 :            : 
    1028   [ +  +  +  -  :    3865048 :         switch (nvme_ns->ana_state) {
                   +  - ]
    1029                 :    3311610 :         case SPDK_NVME_ANA_OPTIMIZED_STATE:
    1030                 :            :         case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
    1031                 :    3618383 :                 return true;
    1032                 :     246656 :         default:
    1033                 :     246665 :                 break;
    1034                 :            :         }
    1035                 :            : 
    1036                 :     246665 :         return false;
    1037                 :     306783 : }
    1038                 :            : 
    1039                 :            : static inline bool
    1040                 :    4155292 : nvme_qpair_is_connected(struct nvme_qpair *nvme_qpair)
    1041                 :            : {
    1042   [ +  +  +  -  :    4155292 :         if (spdk_unlikely(nvme_qpair->qpair == NULL)) {
                   -  + ]
    1043                 :     284684 :                 return false;
    1044                 :            :         }
    1045                 :            : 
    1046   [ +  +  +  -  :    3870608 :         if (spdk_unlikely(spdk_nvme_qpair_get_failure_reason(nvme_qpair->qpair) !=
                   -  + ]
    1047                 :            :                           SPDK_NVME_QPAIR_FAILURE_NONE)) {
    1048                 :        968 :                 return false;
    1049                 :            :         }
    1050                 :            : 
    1051   [ +  +  +  -  :    3869640 :         if (spdk_unlikely(nvme_qpair->ctrlr_ch->reset_iter != NULL)) {
          +  -  +  -  -  
                      + ]
    1052                 :       1238 :                 return false;
    1053                 :            :         }
    1054                 :            : 
    1055                 :    3868402 :         return true;
    1056                 :     306861 : }
    1057                 :            : 
    1058                 :            : static inline bool
    1059                 :    4103895 : nvme_io_path_is_available(struct nvme_io_path *io_path)
    1060                 :            : {
    1061   [ +  +  +  -  :    4103895 :         if (spdk_unlikely(!nvme_qpair_is_connected(io_path->qpair))) {
                   -  + ]
    1062                 :     237987 :                 return false;
    1063                 :            :         }
    1064                 :            : 
    1065   [ +  +  +  -  :    3865908 :         if (spdk_unlikely(!nvme_ns_is_accessible(io_path->nvme_ns))) {
                   -  + ]
    1066                 :     247634 :                 return false;
    1067                 :            :         }
    1068                 :            : 
    1069                 :    3618274 :         return true;
    1070                 :     306780 : }
    1071                 :            : 
    1072                 :            : static inline bool
    1073                 :      48903 : nvme_ctrlr_is_failed(struct nvme_ctrlr *nvme_ctrlr)
    1074                 :            : {
    1075   [ +  +  #  # ]:      48903 :         if (nvme_ctrlr->destruct) {
    1076                 :        256 :                 return true;
    1077                 :            :         }
    1078                 :            : 
    1079   [ +  +  #  # ]:      48647 :         if (nvme_ctrlr->fast_io_fail_timedout) {
    1080                 :      41902 :                 return true;
    1081                 :            :         }
    1082                 :            : 
    1083   [ +  +  #  # ]:       6745 :         if (nvme_ctrlr->resetting) {
    1084   [ +  +  #  #  :       3729 :                 if (nvme_ctrlr->opts.reconnect_delay_sec != 0) {
             #  #  #  # ]
    1085                 :        532 :                         return false;
    1086                 :            :                 } else {
    1087                 :       3197 :                         return true;
    1088                 :            :                 }
    1089                 :            :         }
    1090                 :            : 
    1091   [ +  +  #  # ]:       3016 :         if (nvme_ctrlr->reconnect_is_delayed) {
    1092                 :       2056 :                 return false;
    1093                 :            :         }
    1094                 :            : 
    1095   [ -  +  #  # ]:        960 :         if (nvme_ctrlr->disabled) {
    1096                 :          0 :                 return true;
    1097                 :            :         }
    1098                 :            : 
    1099   [ -  +  #  #  :        960 :         if (spdk_nvme_ctrlr_is_failed(nvme_ctrlr->ctrlr)) {
                   #  # ]
    1100                 :          0 :                 return true;
    1101                 :            :         } else {
    1102                 :        960 :                 return false;
    1103                 :            :         }
    1104                 :          9 : }
    1105                 :            : 
    1106                 :            : static bool
    1107                 :       2293 : nvme_ctrlr_is_available(struct nvme_ctrlr *nvme_ctrlr)
    1108                 :            : {
    1109   [ -  +  #  # ]:       2293 :         if (nvme_ctrlr->destruct) {
    1110                 :          0 :                 return false;
    1111                 :            :         }
    1112                 :            : 
    1113   [ +  +  #  #  :       2293 :         if (spdk_nvme_ctrlr_is_failed(nvme_ctrlr->ctrlr)) {
                   #  # ]
    1114                 :         12 :                 return false;
    1115                 :            :         }
    1116                 :            : 
    1117   [ +  +  -  +  :       2281 :         if (nvme_ctrlr->resetting || nvme_ctrlr->reconnect_is_delayed) {
             #  #  #  # ]
    1118                 :        270 :                 return false;
    1119                 :            :         }
    1120                 :            : 
    1121   [ +  +  #  # ]:       2011 :         if (nvme_ctrlr->disabled) {
    1122                 :          0 :                 return false;
    1123                 :            :         }
    1124                 :            : 
    1125                 :       2011 :         return true;
    1126                 :         33 : }
    1127                 :            : 
    1128                 :            : /* Simulate circular linked list. */
    1129                 :            : static inline struct nvme_io_path *
    1130                 :    2037073 : nvme_io_path_get_next(struct nvme_bdev_channel *nbdev_ch, struct nvme_io_path *prev_path)
    1131                 :            : {
    1132                 :            :         struct nvme_io_path *next_path;
    1133                 :            : 
    1134         [ +  + ]:    2037073 :         if (prev_path != NULL) {
    1135   [ #  #  #  #  :    1547410 :                 next_path = STAILQ_NEXT(prev_path, stailq);
                   #  # ]
    1136         [ +  + ]:    1547410 :                 if (next_path != NULL) {
    1137                 :     775568 :                         return next_path;
    1138                 :            :                 }
    1139                 :         25 :         }
    1140                 :            : 
    1141   [ +  -  +  -  :    1261505 :         return STAILQ_FIRST(&nbdev_ch->io_path_list);
                   +  - ]
    1142                 :        120 : }
    1143                 :            : 
    1144                 :            : static struct nvme_io_path *
    1145                 :     774709 : _bdev_nvme_find_io_path(struct nvme_bdev_channel *nbdev_ch)
    1146                 :            : {
    1147                 :     774709 :         struct nvme_io_path *io_path, *start, *non_optimized = NULL;
    1148                 :            : 
    1149   [ +  -  +  - ]:     774709 :         start = nvme_io_path_get_next(nbdev_ch, nbdev_ch->current_io_path);
    1150                 :            : 
    1151                 :     774709 :         io_path = start;
    1152                 :         87 :         do {
    1153         [ +  + ]:    1418693 :                 if (spdk_likely(nvme_io_path_is_available(io_path))) {
    1154   [ +  +  +  -  :     933118 :                         switch (io_path->nvme_ns->ana_state) {
          +  -  +  -  +  
                   -  - ]
    1155                 :     156263 :                         case SPDK_NVME_ANA_OPTIMIZED_STATE:
    1156   [ +  -  +  - ]:     156330 :                                 nbdev_ch->current_io_path = io_path;
    1157                 :     156330 :                                 return io_path;
    1158                 :     776778 :                         case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
    1159         [ +  + ]:     776788 :                                 if (non_optimized == NULL) {
    1160                 :     636825 :                                         non_optimized = io_path;
    1161                 :          7 :                                 }
    1162                 :     776788 :                                 break;
    1163                 :          0 :                         default:
    1164         [ #  # ]:          0 :                                 assert(false);
    1165                 :            :                                 break;
    1166                 :            :                         }
    1167                 :         10 :                 }
    1168                 :    1262363 :                 io_path = nvme_io_path_get_next(nbdev_ch, io_path);
    1169         [ +  + ]:    1262363 :         } while (io_path != start);
    1170                 :            : 
    1171   [ +  +  #  #  :     618379 :         if (nbdev_ch->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE) {
                   #  # ]
    1172                 :            :                 /* We come here only if there is no optimized path. Cache even non_optimized
    1173                 :            :                  * path for load balance across multiple non_optimized paths.
    1174                 :            :                  */
    1175   [ #  #  #  # ]:     130356 :                 nbdev_ch->current_io_path = non_optimized;
    1176                 :          1 :         }
    1177                 :            : 
    1178                 :     618379 :         return non_optimized;
    1179                 :         87 : }
    1180                 :            : 
    1181                 :            : static struct nvme_io_path *
    1182                 :         16 : _bdev_nvme_find_io_path_min_qd(struct nvme_bdev_channel *nbdev_ch)
    1183                 :            : {
    1184                 :            :         struct nvme_io_path *io_path;
    1185                 :         16 :         struct nvme_io_path *optimized = NULL, *non_optimized = NULL;
    1186                 :         16 :         uint32_t opt_min_qd = UINT32_MAX, non_opt_min_qd = UINT32_MAX;
    1187                 :            :         uint32_t num_outstanding_reqs;
    1188                 :            : 
    1189   [ +  +  #  #  :         64 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1190   [ -  +  #  #  :         48 :                 if (spdk_unlikely(!nvme_qpair_is_connected(io_path->qpair))) {
                   #  # ]
    1191                 :            :                         /* The device is currently resetting. */
    1192                 :          0 :                         continue;
    1193                 :            :                 }
    1194                 :            : 
    1195   [ -  +  #  #  :         48 :                 if (spdk_unlikely(!nvme_ns_is_active(io_path->nvme_ns))) {
                   #  # ]
    1196                 :          0 :                         continue;
    1197                 :            :                 }
    1198                 :            : 
    1199   [ #  #  #  #  :         48 :                 num_outstanding_reqs = spdk_nvme_qpair_get_num_outstanding_reqs(io_path->qpair->qpair);
             #  #  #  # ]
    1200   [ +  +  +  #  :         48 :                 switch (io_path->nvme_ns->ana_state) {
          #  #  #  #  #  
                   #  # ]
    1201                 :         18 :                 case SPDK_NVME_ANA_OPTIMIZED_STATE:
    1202         [ +  + ]:         24 :                         if (num_outstanding_reqs < opt_min_qd) {
    1203                 :         20 :                                 opt_min_qd = num_outstanding_reqs;
    1204                 :         20 :                                 optimized = io_path;
    1205                 :          5 :                         }
    1206                 :         24 :                         break;
    1207                 :          9 :                 case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
    1208         [ +  + ]:         12 :                         if (num_outstanding_reqs < non_opt_min_qd) {
    1209                 :         12 :                                 non_opt_min_qd = num_outstanding_reqs;
    1210                 :         12 :                                 non_optimized = io_path;
    1211                 :          3 :                         }
    1212                 :         12 :                         break;
    1213                 :          9 :                 default:
    1214                 :         12 :                         break;
    1215                 :            :                 }
    1216                 :         12 :         }
    1217                 :            : 
    1218                 :            :         /* don't cache io path for BDEV_NVME_MP_SELECTOR_QUEUE_DEPTH selector */
    1219         [ +  + ]:         16 :         if (optimized != NULL) {
    1220                 :         12 :                 return optimized;
    1221                 :            :         }
    1222                 :            : 
    1223                 :          4 :         return non_optimized;
    1224                 :          4 : }
    1225                 :            : 
    1226                 :            : static inline struct nvme_io_path *
    1227                 :   16709309 : bdev_nvme_find_io_path(struct nvme_bdev_channel *nbdev_ch)
    1228                 :            : {
    1229   [ +  +  +  -  :   16709309 :         if (spdk_likely(nbdev_ch->current_io_path != NULL)) {
                   +  + ]
    1230   [ +  +  +  -  :   16219630 :                 if (nbdev_ch->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE) {
                   -  + ]
    1231   [ +  -  +  - ]:   15934571 :                         return nbdev_ch->current_io_path;
    1232   [ +  +  #  #  :     285059 :                 } else if (nbdev_ch->mp_selector == BDEV_NVME_MP_SELECTOR_ROUND_ROBIN) {
                   #  # ]
    1233   [ +  +  #  #  :     285059 :                         if (++nbdev_ch->rr_counter < nbdev_ch->rr_min_io) {
             #  #  #  # ]
    1234   [ #  #  #  # ]:         12 :                                 return nbdev_ch->current_io_path;
    1235                 :            :                         }
    1236   [ #  #  #  # ]:     285047 :                         nbdev_ch->rr_counter = 0;
    1237                 :          7 :                 }
    1238                 :          7 :         }
    1239                 :            : 
    1240   [ +  +  +  +  :     774726 :         if (nbdev_ch->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE ||
             -  +  #  # ]
    1241   [ +  +  #  # ]:     285086 :             nbdev_ch->mp_selector == BDEV_NVME_MP_SELECTOR_ROUND_ROBIN) {
    1242                 :     774710 :                 return _bdev_nvme_find_io_path(nbdev_ch);
    1243                 :            :         } else {
    1244                 :         16 :                 return _bdev_nvme_find_io_path_min_qd(nbdev_ch);
    1245                 :            :         }
    1246                 :     678736 : }
    1247                 :            : 
    1248                 :            : /* Return true if there is any io_path whose qpair is active or ctrlr is not failed,
    1249                 :            :  * or false otherwise.
    1250                 :            :  *
    1251                 :            :  * If any io_path has an active qpair but find_io_path() returned NULL, its namespace
    1252                 :            :  * is likely to be non-accessible now but may become accessible.
    1253                 :            :  *
    1254                 :            :  * If any io_path has an unfailed ctrlr but find_io_path() returned NULL, the ctrlr
    1255                 :            :  * is likely to be resetting now but the reset may succeed. A ctrlr is set to unfailed
    1256                 :            :  * when starting to reset it but it is set to failed when the reset failed. Hence, if
    1257                 :            :  * a ctrlr is unfailed, it is likely that it works fine or is resetting.
    1258                 :            :  */
    1259                 :            : static bool
    1260                 :      51178 : any_io_path_may_become_available(struct nvme_bdev_channel *nbdev_ch)
    1261                 :            : {
    1262                 :            :         struct nvme_io_path *io_path;
    1263                 :            : 
    1264   [ +  +  +  +  :      51178 :         if (nbdev_ch->resetting) {
             #  #  #  # ]
    1265                 :          4 :                 return false;
    1266                 :            :         }
    1267                 :            : 
    1268   [ +  +  #  #  :      97041 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1269   [ -  +  +  +  :      51686 :                 if (io_path->nvme_ns->ana_transition_timedout) {
          #  #  #  #  #  
                #  #  # ]
    1270                 :        512 :                         continue;
    1271                 :            :                 }
    1272                 :            : 
    1273   [ +  +  +  +  :      51174 :                 if (nvme_qpair_is_connected(io_path->qpair) ||
             #  #  #  # ]
    1274   [ +  +  #  #  :      48903 :                     !nvme_ctrlr_is_failed(io_path->qpair->ctrlr)) {
             #  #  #  # ]
    1275                 :       5819 :                         return true;
    1276                 :            :                 }
    1277                 :          2 :         }
    1278                 :            : 
    1279                 :      45355 :         return false;
    1280                 :         15 : }
    1281                 :            : 
    1282                 :            : static void
    1283                 :       7026 : bdev_nvme_retry_io(struct nvme_bdev_channel *nbdev_ch, struct spdk_bdev_io *bdev_io)
    1284                 :            : {
    1285         [ #  # ]:       7026 :         struct nvme_bdev_io *nbdev_io = (struct nvme_bdev_io *)bdev_io->driver_ctx;
    1286                 :            :         struct spdk_io_channel *ch;
    1287                 :            : 
    1288   [ +  +  +  -  :       7026 :         if (nbdev_io->io_path != NULL && nvme_io_path_is_available(nbdev_io->io_path)) {
          #  #  #  #  #  
                #  #  # ]
    1289                 :       1211 :                 _bdev_nvme_submit_request(nbdev_ch, bdev_io);
    1290                 :          3 :         } else {
    1291                 :       5815 :                 ch = spdk_io_channel_from_ctx(nbdev_ch);
    1292                 :       5815 :                 bdev_nvme_submit_request(ch, bdev_io);
    1293                 :            :         }
    1294                 :       7026 : }
    1295                 :            : 
    1296                 :            : static int
    1297                 :       1362 : bdev_nvme_retry_ios(void *arg)
    1298                 :            : {
    1299                 :       1362 :         struct nvme_bdev_channel *nbdev_ch = arg;
    1300                 :            :         struct nvme_bdev_io *bio, *tmp_bio;
    1301                 :            :         uint64_t now, delay_us;
    1302                 :            : 
    1303                 :       1362 :         now = spdk_get_ticks();
    1304                 :            : 
    1305   [ +  +  +  +  :       8388 :         TAILQ_FOREACH_SAFE(bio, &nbdev_ch->retry_io_list, retry_link, tmp_bio) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1306   [ +  +  #  #  :       7121 :                 if (bio->retry_ticks > now) {
                   #  # ]
    1307                 :         95 :                         break;
    1308                 :            :                 }
    1309                 :            : 
    1310   [ +  +  #  #  :       7026 :                 TAILQ_REMOVE(&nbdev_ch->retry_io_list, bio, retry_link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    1311                 :            : 
    1312                 :       7026 :                 bdev_nvme_retry_io(nbdev_ch, spdk_bdev_io_from_ctx(bio));
    1313                 :         14 :         }
    1314                 :            : 
    1315         [ #  # ]:       1362 :         spdk_poller_unregister(&nbdev_ch->retry_io_poller);
    1316                 :            : 
    1317   [ #  #  #  #  :       1362 :         bio = TAILQ_FIRST(&nbdev_ch->retry_io_list);
                   #  # ]
    1318         [ +  + ]:       1362 :         if (bio != NULL) {
    1319   [ -  +  #  #  :        107 :                 delay_us = (bio->retry_ticks - now) * SPDK_SEC_TO_USEC / spdk_get_ticks_hz();
                   #  # ]
    1320                 :            : 
    1321   [ #  #  #  # ]:        107 :                 nbdev_ch->retry_io_poller = SPDK_POLLER_REGISTER(bdev_nvme_retry_ios, nbdev_ch,
    1322                 :            :                                             delay_us);
    1323                 :          4 :         }
    1324                 :            : 
    1325                 :       1362 :         return SPDK_POLLER_BUSY;
    1326                 :            : }
    1327                 :            : 
    1328                 :            : static void
    1329                 :       7034 : bdev_nvme_queue_retry_io(struct nvme_bdev_channel *nbdev_ch,
    1330                 :            :                          struct nvme_bdev_io *bio, uint64_t delay_ms)
    1331                 :            : {
    1332                 :            :         struct nvme_bdev_io *tmp_bio;
    1333                 :            : 
    1334   [ #  #  #  #  :       7034 :         bio->retry_ticks = spdk_get_ticks() + delay_ms * spdk_get_ticks_hz() / 1000ULL;
                   #  # ]
    1335                 :            : 
    1336   [ +  +  #  #  :      12968 :         TAILQ_FOREACH_REVERSE(tmp_bio, &nbdev_ch->retry_io_list, retry_io_head, retry_link) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1337   [ +  +  #  #  :      11690 :                 if (tmp_bio->retry_ticks <= bio->retry_ticks) {
          #  #  #  #  #  
                      # ]
    1338   [ +  +  #  #  :       5756 :                         TAILQ_INSERT_AFTER(&nbdev_ch->retry_io_list, tmp_bio, bio,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1339                 :            :                                            retry_link);
    1340                 :       5756 :                         return;
    1341                 :            :                 }
    1342                 :          0 :         }
    1343                 :            : 
    1344                 :            :         /* No earlier I/Os were found. This I/O must be the new head. */
    1345   [ +  +  #  #  :       1278 :         TAILQ_INSERT_HEAD(&nbdev_ch->retry_io_list, bio, retry_link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1346                 :            : 
    1347         [ #  # ]:       1278 :         spdk_poller_unregister(&nbdev_ch->retry_io_poller);
    1348                 :            : 
    1349   [ #  #  #  # ]:       1278 :         nbdev_ch->retry_io_poller = SPDK_POLLER_REGISTER(bdev_nvme_retry_ios, nbdev_ch,
    1350                 :            :                                     delay_ms * 1000ULL);
    1351                 :         16 : }
    1352                 :            : 
    1353                 :            : static void
    1354                 :       1713 : bdev_nvme_abort_retry_ios(struct nvme_bdev_channel *nbdev_ch)
    1355                 :            : {
    1356                 :            :         struct nvme_bdev_io *bio, *tmp_bio;
    1357                 :            : 
    1358   [ +  +  +  +  :       1717 :         TAILQ_FOREACH_SAFE(bio, &nbdev_ch->retry_io_list, retry_link, tmp_bio) {
          +  -  +  -  #  
          #  #  #  #  #  
                   +  - ]
    1359   [ -  +  #  #  :          4 :                 TAILQ_REMOVE(&nbdev_ch->retry_io_list, bio, retry_link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    1360                 :          4 :                 __bdev_nvme_io_complete(spdk_bdev_io_from_ctx(bio), SPDK_BDEV_IO_STATUS_ABORTED, NULL);
    1361                 :          1 :         }
    1362                 :            : 
    1363         [ +  - ]:       1713 :         spdk_poller_unregister(&nbdev_ch->retry_io_poller);
    1364                 :       1713 : }
    1365                 :            : 
    1366                 :            : static int
    1367                 :       2553 : bdev_nvme_abort_retry_io(struct nvme_bdev_channel *nbdev_ch,
    1368                 :            :                          struct nvme_bdev_io *bio_to_abort)
    1369                 :            : {
    1370                 :            :         struct nvme_bdev_io *bio;
    1371                 :            : 
    1372   [ +  +  #  #  :       2553 :         TAILQ_FOREACH(bio, &nbdev_ch->retry_io_list, retry_link) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1373         [ +  + ]:          4 :                 if (bio == bio_to_abort) {
    1374   [ -  +  #  #  :          4 :                         TAILQ_REMOVE(&nbdev_ch->retry_io_list, bio, retry_link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    1375                 :          4 :                         __bdev_nvme_io_complete(spdk_bdev_io_from_ctx(bio), SPDK_BDEV_IO_STATUS_ABORTED, NULL);
    1376                 :          4 :                         return 0;
    1377                 :            :                 }
    1378                 :          0 :         }
    1379                 :            : 
    1380                 :       2549 :         return -ENOENT;
    1381                 :          6 : }
    1382                 :            : 
    1383                 :            : static void
    1384                 :       2971 : bdev_nvme_update_nvme_error_stat(struct spdk_bdev_io *bdev_io, const struct spdk_nvme_cpl *cpl)
    1385                 :            : {
    1386                 :            :         struct nvme_bdev *nbdev;
    1387                 :            :         uint16_t sct, sc;
    1388                 :            : 
    1389   [ +  +  +  +  :       2971 :         assert(spdk_nvme_cpl_is_error(cpl));
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1390                 :            : 
    1391   [ #  #  #  #  :       2971 :         nbdev = bdev_io->bdev->ctxt;
             #  #  #  # ]
    1392                 :            : 
    1393   [ +  +  #  #  :       2971 :         if (nbdev->err_stat == NULL) {
                   #  # ]
    1394                 :       1784 :                 return;
    1395                 :            :         }
    1396                 :            : 
    1397   [ #  #  #  #  :       1187 :         sct = cpl->status.sct;
                   #  # ]
    1398   [ #  #  #  #  :       1187 :         sc = cpl->status.sc;
                   #  # ]
    1399                 :            : 
    1400   [ -  +  #  # ]:       1187 :         pthread_mutex_lock(&nbdev->mutex);
    1401                 :            : 
    1402   [ #  #  #  #  :       1187 :         nbdev->err_stat->status_type[sct]++;
          #  #  #  #  #  
                      # ]
    1403         [ +  - ]:       1187 :         switch (sct) {
    1404                 :       1187 :         case SPDK_NVME_SCT_GENERIC:
    1405                 :            :         case SPDK_NVME_SCT_COMMAND_SPECIFIC:
    1406                 :            :         case SPDK_NVME_SCT_MEDIA_ERROR:
    1407                 :            :         case SPDK_NVME_SCT_PATH:
    1408   [ #  #  #  #  :       1187 :                 nbdev->err_stat->status[sct][sc]++;
          #  #  #  #  #  
                #  #  # ]
    1409                 :       1187 :                 break;
    1410                 :          0 :         default:
    1411                 :          0 :                 break;
    1412                 :            :         }
    1413                 :            : 
    1414   [ -  +  #  # ]:       1187 :         pthread_mutex_unlock(&nbdev->mutex);
    1415                 :         14 : }
    1416                 :            : 
    1417                 :            : static inline void
    1418                 :   16469459 : bdev_nvme_update_io_path_stat(struct nvme_bdev_io *bio)
    1419                 :            : {
    1420                 :   16469459 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    1421   [ +  -  +  -  :   16469459 :         uint64_t num_blocks = bdev_io->u.bdev.num_blocks;
             +  -  +  - ]
    1422   [ +  -  +  -  :   16469459 :         uint32_t blocklen = bdev_io->bdev->blocklen;
             +  -  +  - ]
    1423                 :            :         struct spdk_bdev_io_stat *stat;
    1424                 :            :         uint64_t tsc_diff;
    1425                 :            : 
    1426   [ +  +  +  -  :   16469459 :         if (bio->io_path->stat == NULL) {
          +  -  +  -  +  
                      - ]
    1427                 :   16469459 :                 return;
    1428                 :            :         }
    1429                 :            : 
    1430   [ #  #  #  # ]:          0 :         tsc_diff = spdk_get_ticks() - bio->submit_tsc;
    1431   [ #  #  #  #  :          0 :         stat = bio->io_path->stat;
             #  #  #  # ]
    1432                 :            : 
    1433   [ #  #  #  #  :          0 :         switch (bdev_io->type) {
          #  #  #  #  #  
                      # ]
    1434                 :          0 :         case SPDK_BDEV_IO_TYPE_READ:
    1435   [ #  #  #  # ]:          0 :                 stat->bytes_read += num_blocks * blocklen;
    1436         [ #  # ]:          0 :                 stat->num_read_ops++;
    1437   [ #  #  #  # ]:          0 :                 stat->read_latency_ticks += tsc_diff;
    1438   [ #  #  #  #  :          0 :                 if (stat->max_read_latency_ticks < tsc_diff) {
                   #  # ]
    1439   [ #  #  #  # ]:          0 :                         stat->max_read_latency_ticks = tsc_diff;
    1440                 :          0 :                 }
    1441   [ #  #  #  #  :          0 :                 if (stat->min_read_latency_ticks > tsc_diff) {
                   #  # ]
    1442   [ #  #  #  # ]:          0 :                         stat->min_read_latency_ticks = tsc_diff;
    1443                 :          0 :                 }
    1444                 :          0 :                 break;
    1445                 :          0 :         case SPDK_BDEV_IO_TYPE_WRITE:
    1446   [ #  #  #  # ]:          0 :                 stat->bytes_written += num_blocks * blocklen;
    1447         [ #  # ]:          0 :                 stat->num_write_ops++;
    1448   [ #  #  #  # ]:          0 :                 stat->write_latency_ticks += tsc_diff;
    1449   [ #  #  #  #  :          0 :                 if (stat->max_write_latency_ticks < tsc_diff) {
                   #  # ]
    1450   [ #  #  #  # ]:          0 :                         stat->max_write_latency_ticks = tsc_diff;
    1451                 :          0 :                 }
    1452   [ #  #  #  #  :          0 :                 if (stat->min_write_latency_ticks > tsc_diff) {
                   #  # ]
    1453   [ #  #  #  # ]:          0 :                         stat->min_write_latency_ticks = tsc_diff;
    1454                 :          0 :                 }
    1455                 :          0 :                 break;
    1456                 :          0 :         case SPDK_BDEV_IO_TYPE_UNMAP:
    1457   [ #  #  #  # ]:          0 :                 stat->bytes_unmapped += num_blocks * blocklen;
    1458         [ #  # ]:          0 :                 stat->num_unmap_ops++;
    1459   [ #  #  #  # ]:          0 :                 stat->unmap_latency_ticks += tsc_diff;
    1460   [ #  #  #  #  :          0 :                 if (stat->max_unmap_latency_ticks < tsc_diff) {
                   #  # ]
    1461   [ #  #  #  # ]:          0 :                         stat->max_unmap_latency_ticks = tsc_diff;
    1462                 :          0 :                 }
    1463   [ #  #  #  #  :          0 :                 if (stat->min_unmap_latency_ticks > tsc_diff) {
                   #  # ]
    1464   [ #  #  #  # ]:          0 :                         stat->min_unmap_latency_ticks = tsc_diff;
    1465                 :          0 :                 }
    1466                 :          0 :                 break;
    1467                 :          0 :         case SPDK_BDEV_IO_TYPE_ZCOPY:
    1468                 :            :                 /* Track the data in the start phase only */
    1469   [ #  #  #  #  :          0 :                 if (!bdev_io->u.bdev.zcopy.start) {
          #  #  #  #  #  
                      # ]
    1470                 :          0 :                         break;
    1471                 :            :                 }
    1472   [ #  #  #  #  :          0 :                 if (bdev_io->u.bdev.zcopy.populate) {
          #  #  #  #  #  
                      # ]
    1473   [ #  #  #  # ]:          0 :                         stat->bytes_read += num_blocks * blocklen;
    1474         [ #  # ]:          0 :                         stat->num_read_ops++;
    1475   [ #  #  #  # ]:          0 :                         stat->read_latency_ticks += tsc_diff;
    1476   [ #  #  #  #  :          0 :                         if (stat->max_read_latency_ticks < tsc_diff) {
                   #  # ]
    1477   [ #  #  #  # ]:          0 :                                 stat->max_read_latency_ticks = tsc_diff;
    1478                 :          0 :                         }
    1479   [ #  #  #  #  :          0 :                         if (stat->min_read_latency_ticks > tsc_diff) {
                   #  # ]
    1480   [ #  #  #  # ]:          0 :                                 stat->min_read_latency_ticks = tsc_diff;
    1481                 :          0 :                         }
    1482                 :          0 :                 } else {
    1483   [ #  #  #  # ]:          0 :                         stat->bytes_written += num_blocks * blocklen;
    1484         [ #  # ]:          0 :                         stat->num_write_ops++;
    1485   [ #  #  #  # ]:          0 :                         stat->write_latency_ticks += tsc_diff;
    1486   [ #  #  #  #  :          0 :                         if (stat->max_write_latency_ticks < tsc_diff) {
                   #  # ]
    1487   [ #  #  #  # ]:          0 :                                 stat->max_write_latency_ticks = tsc_diff;
    1488                 :          0 :                         }
    1489   [ #  #  #  #  :          0 :                         if (stat->min_write_latency_ticks > tsc_diff) {
                   #  # ]
    1490   [ #  #  #  # ]:          0 :                                 stat->min_write_latency_ticks = tsc_diff;
    1491                 :          0 :                         }
    1492                 :            :                 }
    1493                 :          0 :                 break;
    1494                 :          0 :         case SPDK_BDEV_IO_TYPE_COPY:
    1495   [ #  #  #  # ]:          0 :                 stat->bytes_copied += num_blocks * blocklen;
    1496         [ #  # ]:          0 :                 stat->num_copy_ops++;
    1497   [ #  #  #  # ]:          0 :                 stat->copy_latency_ticks += tsc_diff;
    1498   [ #  #  #  #  :          0 :                 if (stat->max_copy_latency_ticks < tsc_diff) {
                   #  # ]
    1499   [ #  #  #  # ]:          0 :                         stat->max_copy_latency_ticks = tsc_diff;
    1500                 :          0 :                 }
    1501   [ #  #  #  #  :          0 :                 if (stat->min_copy_latency_ticks > tsc_diff) {
                   #  # ]
    1502   [ #  #  #  # ]:          0 :                         stat->min_copy_latency_ticks = tsc_diff;
    1503                 :          0 :                 }
    1504                 :          0 :                 break;
    1505                 :          0 :         default:
    1506                 :          0 :                 break;
    1507                 :            :         }
    1508                 :     680242 : }
    1509                 :            : 
    1510                 :            : static bool
    1511                 :       2906 : bdev_nvme_check_retry_io(struct nvme_bdev_io *bio,
    1512                 :            :                          const struct spdk_nvme_cpl *cpl,
    1513                 :            :                          struct nvme_bdev_channel *nbdev_ch,
    1514                 :            :                          uint64_t *_delay_ms)
    1515                 :            : {
    1516   [ #  #  #  # ]:       2906 :         struct nvme_io_path *io_path = bio->io_path;
    1517   [ #  #  #  #  :       2906 :         struct nvme_ctrlr *nvme_ctrlr = io_path->qpair->ctrlr;
             #  #  #  # ]
    1518                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    1519                 :            : 
    1520   [ +  +  -  +  :       2910 :         if (spdk_nvme_cpl_is_path_error(cpl) ||
          #  #  #  #  #  
                      # ]
    1521   [ +  +  +  +  :       2267 :             spdk_nvme_cpl_is_aborted_sq_deletion(cpl) ||
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1522         [ +  + ]:       1211 :             !nvme_io_path_is_available(io_path) ||
    1523         [ -  + ]:       1215 :             !nvme_ctrlr_is_available(nvme_ctrlr)) {
    1524                 :       1699 :                 bdev_nvme_clear_current_io_path(nbdev_ch);
    1525   [ #  #  #  # ]:       1699 :                 bio->io_path = NULL;
    1526   [ +  +  +  -  :       1699 :                 if (spdk_nvme_cpl_is_ana_error(cpl)) {
          +  +  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    1527         [ +  + ]:        631 :                         if (nvme_ctrlr_read_ana_log_page(nvme_ctrlr) == 0) {
    1528   [ #  #  #  #  :         10 :                                 io_path->nvme_ns->ana_state_updating = true;
             #  #  #  # ]
    1529                 :          1 :                         }
    1530                 :          1 :                 }
    1531         [ +  + ]:       1687 :                 if (!any_io_path_may_become_available(nbdev_ch)) {
    1532                 :          0 :                         return false;
    1533                 :            :                 }
    1534         [ #  # ]:       1687 :                 *_delay_ms = 0;
    1535                 :          3 :         } else {
    1536   [ #  #  #  # ]:       1215 :                 bio->retry_count++;
    1537                 :            : 
    1538   [ #  #  #  # ]:       1215 :                 cdata = spdk_nvme_ctrlr_get_data(nvme_ctrlr->ctrlr);
    1539                 :            : 
    1540   [ +  +  #  #  :       1215 :                 if (cpl->status.crd != 0) {
             #  #  #  # ]
    1541   [ #  #  #  #  :          4 :                         *_delay_ms = cdata->crdt[cpl->status.crd] * 100;
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1542                 :          1 :                 } else {
    1543         [ #  # ]:       1211 :                         *_delay_ms = 0;
    1544                 :            :                 }
    1545                 :            :         }
    1546                 :            : 
    1547                 :       2902 :         return true;
    1548                 :          7 : }
    1549                 :            : 
    1550                 :            : static inline void
    1551                 :   16473081 : bdev_nvme_io_complete_nvme_status(struct nvme_bdev_io *bio,
    1552                 :            :                                   const struct spdk_nvme_cpl *cpl)
    1553                 :            : {
    1554                 :   16473081 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    1555                 :            :         struct nvme_bdev_channel *nbdev_ch;
    1556                 :    6717126 :         uint64_t delay_ms;
    1557                 :            : 
    1558   [ +  +  +  -  :   16473081 :         assert(!bdev_nvme_io_type_is_admin(bdev_io->type));
             +  -  #  # ]
    1559                 :            : 
    1560   [ +  +  +  +  :   16473081 :         if (spdk_likely(spdk_nvme_cpl_is_success(cpl))) {
          +  -  -  +  +  
          -  +  -  +  -  
                   +  - ]
    1561                 :   16470102 :                 bdev_nvme_update_io_path_stat(bio);
    1562                 :   16470102 :                 goto complete;
    1563                 :            :         }
    1564                 :            : 
    1565                 :            :         /* Update error counts before deciding if retry is needed.
    1566                 :            :          * Hence, error counts may be more than the number of I/O errors.
    1567                 :            :          */
    1568                 :       2979 :         bdev_nvme_update_nvme_error_stat(bdev_io, cpl);
    1569                 :            : 
    1570   [ +  +  +  +  :       2986 :         if (cpl->status.dnr != 0 || spdk_nvme_cpl_is_aborted_by_request(cpl) ||
          +  +  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1571   [ +  +  +  +  :       2904 :             (g_opts.bdev_retry_count != -1 && bio->retry_count >= g_opts.bdev_retry_count)) {
          #  #  #  #  #  
                      # ]
    1572                 :         87 :                 goto complete;
    1573                 :            :         }
    1574                 :            : 
    1575                 :            :         /* At this point we don't know whether the sequence was successfully executed or not, so we
    1576                 :            :          * cannot retry the IO */
    1577   [ -  +  #  #  :       2902 :         if (bdev_io->u.bdev.accel_sequence != NULL) {
          #  #  #  #  #  
                      # ]
    1578                 :          0 :                 goto complete;
    1579                 :            :         }
    1580                 :            : 
    1581                 :       2902 :         nbdev_ch = spdk_io_channel_get_ctx(spdk_bdev_io_get_io_channel(bdev_io));
    1582                 :            : 
    1583         [ +  - ]:       2902 :         if (bdev_nvme_check_retry_io(bio, cpl, nbdev_ch, &delay_ms)) {
    1584                 :       2902 :                 bdev_nvme_queue_retry_io(nbdev_ch, bio, delay_ms);
    1585                 :       2902 :                 return;
    1586                 :            :         }
    1587                 :            : 
    1588                 :    6717105 : complete:
    1589   [ +  -  +  - ]:   16470171 :         bio->retry_count = 0;
    1590   [ +  -  +  - ]:   16470171 :         bio->submit_tsc = 0;
    1591   [ +  -  +  -  :   16470171 :         bdev_io->u.bdev.accel_sequence = NULL;
             +  -  +  - ]
    1592                 :   16470171 :         __bdev_nvme_io_complete(bdev_io, 0, cpl);
    1593                 :     680899 : }
    1594                 :            : 
    1595                 :            : static inline void
    1596                 :     236821 : bdev_nvme_io_complete(struct nvme_bdev_io *bio, int rc)
    1597                 :            : {
    1598                 :     236821 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    1599                 :            :         struct nvme_bdev_channel *nbdev_ch;
    1600                 :            :         enum spdk_bdev_io_status io_status;
    1601                 :            : 
    1602   [ -  +  #  #  :     236821 :         assert(!bdev_nvme_io_type_is_admin(bdev_io->type));
             #  #  #  # ]
    1603                 :            : 
    1604   [ +  +  +  + ]:     236821 :         switch (rc) {
    1605                 :     168616 :         case 0:
    1606                 :     168617 :                 io_status = SPDK_BDEV_IO_STATUS_SUCCESS;
    1607                 :     168617 :                 break;
    1608                 :      18713 :         case -ENOMEM:
    1609                 :      18713 :                 io_status = SPDK_BDEV_IO_STATUS_NOMEM;
    1610                 :      18713 :                 break;
    1611                 :      49479 :         case -ENXIO:
    1612   [ +  +  +  -  :      49494 :                 if (g_opts.bdev_retry_count == -1 || bio->retry_count < g_opts.bdev_retry_count) {
          #  #  #  #  #  
                #  #  # ]
    1613                 :      49491 :                         nbdev_ch = spdk_io_channel_get_ctx(spdk_bdev_io_get_io_channel(bdev_io));
    1614                 :            : 
    1615                 :      49491 :                         bdev_nvme_clear_current_io_path(nbdev_ch);
    1616   [ #  #  #  # ]:      49491 :                         bio->io_path = NULL;
    1617                 :            : 
    1618         [ +  + ]:      49491 :                         if (any_io_path_may_become_available(nbdev_ch)) {
    1619                 :       4132 :                                 bdev_nvme_queue_retry_io(nbdev_ch, bio, 1000ULL);
    1620                 :       4132 :                                 return;
    1621                 :            :                         }
    1622                 :          3 :                 }
    1623                 :            : 
    1624                 :            :         /* fallthrough */
    1625                 :            :         default:
    1626   [ #  #  #  #  :      45359 :                 spdk_accel_sequence_abort(bdev_io->u.bdev.accel_sequence);
             #  #  #  # ]
    1627   [ #  #  #  #  :      45359 :                 bdev_io->u.bdev.accel_sequence = NULL;
             #  #  #  # ]
    1628                 :      45359 :                 io_status = SPDK_BDEV_IO_STATUS_FAILED;
    1629                 :      45359 :                 break;
    1630                 :            :         }
    1631                 :            : 
    1632   [ #  #  #  # ]:     232689 :         bio->retry_count = 0;
    1633   [ #  #  #  # ]:     232689 :         bio->submit_tsc = 0;
    1634                 :     232689 :         __bdev_nvme_io_complete(bdev_io, io_status, NULL);
    1635                 :         13 : }
    1636                 :            : 
    1637                 :            : static inline void
    1638                 :         16 : bdev_nvme_admin_complete(struct nvme_bdev_io *bio, int rc)
    1639                 :            : {
    1640                 :         16 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    1641                 :            :         enum spdk_bdev_io_status io_status;
    1642                 :            : 
    1643   [ +  +  +  + ]:         16 :         switch (rc) {
    1644                 :          3 :         case 0:
    1645                 :          4 :                 io_status = SPDK_BDEV_IO_STATUS_SUCCESS;
    1646                 :          4 :                 break;
    1647                 :          0 :         case -ENOMEM:
    1648                 :          0 :                 io_status = SPDK_BDEV_IO_STATUS_NOMEM;
    1649                 :          0 :                 break;
    1650                 :         10 :         case -ENXIO:
    1651                 :            :         /* fallthrough */
    1652                 :            :         default:
    1653                 :         12 :                 io_status = SPDK_BDEV_IO_STATUS_FAILED;
    1654                 :         12 :                 break;
    1655                 :            :         }
    1656                 :            : 
    1657                 :         16 :         __bdev_nvme_io_complete(bdev_io, io_status, NULL);
    1658                 :         16 : }
    1659                 :            : 
    1660                 :            : static void
    1661                 :         53 : bdev_nvme_clear_io_path_caches_done(struct nvme_ctrlr *nvme_ctrlr,
    1662                 :            :                                     void *ctx, int status)
    1663                 :            : {
    1664   [ -  +  #  # ]:         53 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    1665                 :            : 
    1666   [ +  +  #  #  :         53 :         assert(nvme_ctrlr->io_path_cache_clearing == true);
                   #  # ]
    1667         [ #  # ]:         53 :         nvme_ctrlr->io_path_cache_clearing = false;
    1668                 :            : 
    1669         [ +  + ]:         53 :         if (!nvme_ctrlr_can_be_unregistered(nvme_ctrlr)) {
    1670   [ -  +  #  # ]:         53 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    1671                 :         53 :                 return;
    1672                 :            :         }
    1673                 :            : 
    1674   [ #  #  #  # ]:          0 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    1675                 :            : 
    1676                 :          0 :         nvme_ctrlr_unregister(nvme_ctrlr);
    1677                 :          3 : }
    1678                 :            : 
    1679                 :            : static void
    1680                 :       5887 : _bdev_nvme_clear_io_path_cache(struct nvme_qpair *nvme_qpair)
    1681                 :            : {
    1682                 :            :         struct nvme_io_path *io_path;
    1683                 :            : 
    1684   [ +  +  +  -  :       9966 :         TAILQ_FOREACH(io_path, &nvme_qpair->io_path_list, tailq) {
          +  -  +  +  +  
             -  +  -  +  
                      - ]
    1685   [ +  +  +  -  :       4079 :                 if (io_path->nbdev_ch == NULL) {
                   -  + ]
    1686                 :       3185 :                         continue;
    1687                 :            :                 }
    1688   [ #  #  #  # ]:        894 :                 bdev_nvme_clear_current_io_path(io_path->nbdev_ch);
    1689                 :        167 :         }
    1690                 :       5887 : }
    1691                 :            : 
    1692                 :            : static void
    1693                 :         36 : bdev_nvme_clear_io_path_cache(struct nvme_ctrlr_channel_iter *i,
    1694                 :            :                               struct nvme_ctrlr *nvme_ctrlr,
    1695                 :            :                               struct nvme_ctrlr_channel *ctrlr_ch,
    1696                 :            :                               void *ctx)
    1697                 :            : {
    1698   [ +  +  #  #  :         36 :         assert(ctrlr_ch->qpair != NULL);
             #  #  #  # ]
    1699                 :            : 
    1700   [ #  #  #  # ]:         36 :         _bdev_nvme_clear_io_path_cache(ctrlr_ch->qpair);
    1701                 :            : 
    1702                 :         36 :         nvme_ctrlr_for_each_channel_continue(i, 0);
    1703                 :         36 : }
    1704                 :            : 
    1705                 :            : static void
    1706                 :        319 : bdev_nvme_clear_io_path_caches(struct nvme_ctrlr *nvme_ctrlr)
    1707                 :            : {
    1708   [ -  +  #  # ]:        319 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    1709   [ +  +  +  + ]:        319 :         if (!nvme_ctrlr_is_available(nvme_ctrlr) ||
    1710         [ #  # ]:          3 :             nvme_ctrlr->io_path_cache_clearing) {
    1711   [ -  +  #  # ]:        266 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    1712                 :        266 :                 return;
    1713                 :            :         }
    1714                 :            : 
    1715         [ #  # ]:         53 :         nvme_ctrlr->io_path_cache_clearing = true;
    1716   [ -  +  #  # ]:         53 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    1717                 :            : 
    1718                 :         53 :         nvme_ctrlr_for_each_channel(nvme_ctrlr,
    1719                 :            :                                     bdev_nvme_clear_io_path_cache,
    1720                 :            :                                     NULL,
    1721                 :            :                                     bdev_nvme_clear_io_path_caches_done);
    1722                 :         15 : }
    1723                 :            : 
    1724                 :            : static struct nvme_qpair *
    1725                 :       1867 : nvme_poll_group_get_qpair(struct nvme_poll_group *group, struct spdk_nvme_qpair *qpair)
    1726                 :            : {
    1727                 :            :         struct nvme_qpair *nvme_qpair;
    1728                 :            : 
    1729   [ +  +  +  -  :       2025 :         TAILQ_FOREACH(nvme_qpair, &group->qpair_list, tailq) {
          +  -  -  +  #  
             #  #  #  #  
                      # ]
    1730   [ +  +  +  -  :       2025 :                 if (nvme_qpair->qpair == qpair) {
                   -  + ]
    1731                 :       1867 :                         break;
    1732                 :            :                 }
    1733                 :         17 :         }
    1734                 :            : 
    1735                 :       1867 :         return nvme_qpair;
    1736                 :            : }
    1737                 :            : 
    1738                 :            : static void nvme_qpair_delete(struct nvme_qpair *nvme_qpair);
    1739                 :            : 
    1740                 :            : static void
    1741                 :       1867 : bdev_nvme_disconnected_qpair_cb(struct spdk_nvme_qpair *qpair, void *poll_group_ctx)
    1742                 :            : {
    1743                 :       1867 :         struct nvme_poll_group *group = poll_group_ctx;
    1744                 :            :         struct nvme_qpair *nvme_qpair;
    1745                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    1746                 :            :         struct nvme_ctrlr_channel *ctrlr_ch;
    1747                 :            :         int status;
    1748                 :            : 
    1749                 :       1867 :         nvme_qpair = nvme_poll_group_get_qpair(group, qpair);
    1750         [ +  + ]:       1867 :         if (nvme_qpair == NULL) {
    1751                 :          0 :                 return;
    1752                 :            :         }
    1753                 :            : 
    1754   [ +  +  +  -  :       1867 :         if (nvme_qpair->qpair != NULL) {
                   -  + ]
    1755   [ +  -  +  - ]:       1867 :                 spdk_nvme_ctrlr_free_io_qpair(nvme_qpair->qpair);
    1756   [ +  -  +  - ]:       1867 :                 nvme_qpair->qpair = NULL;
    1757                 :        143 :         }
    1758                 :            : 
    1759                 :       1867 :         _bdev_nvme_clear_io_path_cache(nvme_qpair);
    1760                 :            : 
    1761   [ +  -  +  - ]:       1867 :         nvme_ctrlr = nvme_qpair->ctrlr;
    1762   [ +  -  +  - ]:       1867 :         ctrlr_ch = nvme_qpair->ctrlr_ch;
    1763                 :            : 
    1764         [ +  + ]:       1867 :         if (ctrlr_ch != NULL) {
    1765   [ +  +  #  #  :        338 :                 if (ctrlr_ch->reset_iter != NULL) {
                   #  # ]
    1766                 :            :                         /* We are in a full reset sequence. */
    1767   [ -  +  #  #  :        309 :                         if (ctrlr_ch->connect_poller != NULL) {
                   #  # ]
    1768                 :            :                                 /* qpair was failed to connect. Abort the reset sequence. */
    1769   [ #  #  #  #  :          0 :                                 NVME_CTRLR_INFOLOG(nvme_ctrlr,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1770                 :            :                                                    "qpair %p was failed to connect. abort the reset ctrlr sequence.\n",
    1771                 :            :                                                    qpair);
    1772         [ #  # ]:          0 :                                 spdk_poller_unregister(&ctrlr_ch->connect_poller);
    1773                 :          0 :                                 status = -1;
    1774                 :          0 :                         } else {
    1775                 :            :                                 /* qpair was completed to disconnect. Just move to the next ctrlr_channel. */
    1776   [ +  +  -  +  :        309 :                                 NVME_CTRLR_INFOLOG(nvme_ctrlr,
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1777                 :            :                                                    "qpair %p was disconnected and freed in a reset ctrlr sequence.\n",
    1778                 :            :                                                    qpair);
    1779                 :        309 :                                 status = 0;
    1780                 :            :                         }
    1781   [ #  #  #  # ]:        309 :                         nvme_ctrlr_for_each_channel_continue(ctrlr_ch->reset_iter, status);
    1782   [ #  #  #  # ]:        309 :                         ctrlr_ch->reset_iter = NULL;
    1783                 :         70 :                 } else {
    1784                 :            :                         /* qpair was disconnected unexpectedly. Reset controller for recovery. */
    1785   [ +  +  -  +  :         29 :                         NVME_CTRLR_INFOLOG(nvme_ctrlr, "qpair %p was disconnected and freed. reset controller.\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1786                 :            :                                            qpair);
    1787                 :         29 :                         bdev_nvme_failover_ctrlr(nvme_ctrlr);
    1788                 :            :                 }
    1789                 :         75 :         } else {
    1790                 :            :                 /* In this case, ctrlr_channel is already deleted. */
    1791   [ +  +  +  +  :       1529 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "qpair %p was disconnected and freed. delete nvme_qpair.\n",
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1792                 :            :                                    qpair);
    1793                 :       1529 :                 nvme_qpair_delete(nvme_qpair);
    1794                 :            :         }
    1795                 :        143 : }
    1796                 :            : 
    1797                 :            : static void
    1798                 :          0 : bdev_nvme_check_io_qpairs(struct nvme_poll_group *group)
    1799                 :            : {
    1800                 :            :         struct nvme_qpair *nvme_qpair;
    1801                 :            : 
    1802   [ #  #  #  #  :          0 :         TAILQ_FOREACH(nvme_qpair, &group->qpair_list, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1803   [ #  #  #  #  :          0 :                 if (nvme_qpair->qpair == NULL || nvme_qpair->ctrlr_ch == NULL) {
          #  #  #  #  #  
                #  #  # ]
    1804                 :          0 :                         continue;
    1805                 :            :                 }
    1806                 :            : 
    1807   [ #  #  #  #  :          0 :                 if (spdk_nvme_qpair_get_failure_reason(nvme_qpair->qpair) !=
                   #  # ]
    1808                 :            :                     SPDK_NVME_QPAIR_FAILURE_NONE) {
    1809                 :          0 :                         _bdev_nvme_clear_io_path_cache(nvme_qpair);
    1810                 :          0 :                 }
    1811                 :          0 :         }
    1812                 :          0 : }
    1813                 :            : 
    1814                 :            : static int
    1815                 :  346948826 : bdev_nvme_poll(void *arg)
    1816                 :            : {
    1817                 :  346948826 :         struct nvme_poll_group *group = arg;
    1818                 :            :         int64_t num_completions;
    1819                 :            : 
    1820   [ +  +  +  +  :  346948826 :         if (group->collect_spin_stat && group->start_ticks == 0) {
          +  -  -  +  #  
             #  #  #  #  
                      # ]
    1821   [ #  #  #  # ]:          0 :                 group->start_ticks = spdk_get_ticks();
    1822                 :          0 :         }
    1823                 :            : 
    1824   [ +  -  +  - ]:  346948826 :         num_completions = spdk_nvme_poll_group_process_completions(group->group, 0,
    1825                 :            :                           bdev_nvme_disconnected_qpair_cb);
    1826   [ +  +  +  +  :  346948826 :         if (group->collect_spin_stat) {
             +  -  +  - ]
    1827         [ #  # ]:          0 :                 if (num_completions > 0) {
    1828   [ #  #  #  #  :          0 :                         if (group->end_ticks != 0) {
                   #  # ]
    1829   [ #  #  #  #  :          0 :                                 group->spin_ticks += (group->end_ticks - group->start_ticks);
          #  #  #  #  #  
                #  #  # ]
    1830   [ #  #  #  # ]:          0 :                                 group->end_ticks = 0;
    1831                 :          0 :                         }
    1832   [ #  #  #  # ]:          0 :                         group->start_ticks = 0;
    1833                 :          0 :                 } else {
    1834   [ #  #  #  # ]:          0 :                         group->end_ticks = spdk_get_ticks();
    1835                 :            :                 }
    1836                 :          0 :         }
    1837                 :            : 
    1838         [ +  + ]:  346948826 :         if (spdk_unlikely(num_completions < 0)) {
    1839                 :          0 :                 bdev_nvme_check_io_qpairs(group);
    1840                 :          0 :         }
    1841                 :            : 
    1842                 :  346948826 :         return num_completions > 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE;
    1843                 :            : }
    1844                 :            : 
    1845                 :            : static int bdev_nvme_poll_adminq(void *arg);
    1846                 :            : 
    1847                 :            : static void
    1848                 :        714 : bdev_nvme_change_adminq_poll_period(struct nvme_ctrlr *nvme_ctrlr, uint64_t new_period_us)
    1849                 :            : {
    1850         [ -  + ]:        714 :         if (spdk_interrupt_mode_is_enabled()) {
    1851                 :          0 :                 return;
    1852                 :            :         }
    1853                 :            : 
    1854         [ #  # ]:        714 :         spdk_poller_unregister(&nvme_ctrlr->adminq_timer_poller);
    1855                 :            : 
    1856   [ #  #  #  # ]:        714 :         nvme_ctrlr->adminq_timer_poller = SPDK_POLLER_REGISTER(bdev_nvme_poll_adminq,
    1857                 :            :                                           nvme_ctrlr, new_period_us);
    1858                 :        146 : }
    1859                 :            : 
    1860                 :            : static int
    1861                 :     369069 : bdev_nvme_poll_adminq(void *arg)
    1862                 :            : {
    1863                 :            :         int32_t rc;
    1864                 :     369069 :         struct nvme_ctrlr *nvme_ctrlr = arg;
    1865                 :            :         nvme_ctrlr_disconnected_cb disconnected_cb;
    1866                 :            : 
    1867   [ +  +  #  # ]:     369069 :         assert(nvme_ctrlr != NULL);
    1868                 :            : 
    1869   [ +  -  +  - ]:     369069 :         rc = spdk_nvme_ctrlr_process_admin_completions(nvme_ctrlr->ctrlr);
    1870         [ +  + ]:     369069 :         if (rc < 0) {
    1871   [ #  #  #  # ]:        626 :                 disconnected_cb = nvme_ctrlr->disconnected_cb;
    1872   [ #  #  #  # ]:        626 :                 nvme_ctrlr->disconnected_cb = NULL;
    1873                 :            : 
    1874         [ +  + ]:        626 :                 if (disconnected_cb != NULL) {
    1875                 :        430 :                         bdev_nvme_change_adminq_poll_period(nvme_ctrlr,
    1876         [ #  # ]:         73 :                                                             g_opts.nvme_adminq_poll_period_us);
    1877   [ #  #  #  # ]:        357 :                         disconnected_cb(nvme_ctrlr);
    1878                 :         73 :                 } else {
    1879                 :        269 :                         bdev_nvme_failover_ctrlr(nvme_ctrlr);
    1880                 :            :                 }
    1881   [ +  +  +  -  :     368531 :         } else if (spdk_nvme_ctrlr_get_admin_qp_failure_reason(nvme_ctrlr->ctrlr) !=
                   +  - ]
    1882                 :            :                    SPDK_NVME_QPAIR_FAILURE_NONE) {
    1883                 :        276 :                 bdev_nvme_clear_io_path_caches(nvme_ctrlr);
    1884                 :         12 :         }
    1885                 :            : 
    1886                 :     369069 :         return rc == 0 ? SPDK_POLLER_IDLE : SPDK_POLLER_BUSY;
    1887                 :            : }
    1888                 :            : 
    1889                 :            : static void
    1890                 :        952 : nvme_bdev_free(void *io_device)
    1891                 :            : {
    1892                 :        952 :         struct nvme_bdev *nvme_disk = io_device;
    1893                 :            : 
    1894   [ +  +  +  - ]:        952 :         pthread_mutex_destroy(&nvme_disk->mutex);
    1895   [ +  -  +  -  :        952 :         free(nvme_disk->disk.name);
                   +  - ]
    1896   [ +  -  +  - ]:        952 :         free(nvme_disk->err_stat);
    1897                 :        952 :         free(nvme_disk);
    1898                 :        952 : }
    1899                 :            : 
    1900                 :            : static int
    1901                 :        948 : bdev_nvme_destruct(void *ctx)
    1902                 :            : {
    1903                 :        948 :         struct nvme_bdev *nvme_disk = ctx;
    1904                 :            :         struct nvme_ns *nvme_ns, *tmp_nvme_ns;
    1905                 :            : 
    1906                 :        208 :         SPDK_DTRACE_PROBE2(bdev_nvme_destruct, nvme_disk->nbdev_ctrlr->name, nvme_disk->nsid);
    1907                 :            : 
    1908   [ +  +  +  +  :       1902 :         TAILQ_FOREACH_SAFE(nvme_ns, &nvme_disk->nvme_ns_list, tailq, tmp_nvme_ns) {
          +  -  +  +  +  
          -  +  -  +  -  
                   +  + ]
    1909   [ +  +  +  -  :        954 :                 pthread_mutex_lock(&nvme_ns->ctrlr->mutex);
             +  -  +  - ]
    1910                 :            : 
    1911   [ +  -  +  - ]:        954 :                 nvme_ns->bdev = NULL;
    1912                 :            : 
    1913   [ +  +  +  -  :        954 :                 assert(nvme_ns->id > 0);
             +  -  #  # ]
    1914                 :            : 
    1915   [ +  +  +  -  :        954 :                 if (nvme_ctrlr_get_ns(nvme_ns->ctrlr, nvme_ns->id) == NULL) {
          +  -  +  -  +  
                      - ]
    1916   [ -  +  #  #  :        214 :                         pthread_mutex_unlock(&nvme_ns->ctrlr->mutex);
             #  #  #  # ]
    1917                 :            : 
    1918   [ #  #  #  # ]:        214 :                         nvme_ctrlr_put_ref(nvme_ns->ctrlr);
    1919                 :        214 :                         nvme_ns_free(nvme_ns);
    1920                 :          3 :                 } else {
    1921   [ +  +  +  -  :        740 :                         pthread_mutex_unlock(&nvme_ns->ctrlr->mutex);
             +  -  +  - ]
    1922                 :            :                 }
    1923                 :         50 :         }
    1924                 :            : 
    1925         [ +  + ]:        948 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    1926   [ +  +  +  -  :        948 :         TAILQ_REMOVE(&nvme_disk->nbdev_ctrlr->bdevs, nvme_disk, tailq);
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1927         [ +  + ]:        948 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    1928                 :            : 
    1929                 :        948 :         spdk_io_device_unregister(nvme_disk, nvme_bdev_free);
    1930                 :            : 
    1931                 :        948 :         return 0;
    1932                 :            : }
    1933                 :            : 
    1934                 :            : static int
    1935                 :       1871 : bdev_nvme_create_qpair(struct nvme_qpair *nvme_qpair)
    1936                 :            : {
    1937                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    1938                 :       1022 :         struct spdk_nvme_io_qpair_opts opts;
    1939                 :            :         struct spdk_nvme_qpair *qpair;
    1940                 :            :         int rc;
    1941                 :            : 
    1942   [ +  -  +  - ]:       1871 :         nvme_ctrlr = nvme_qpair->ctrlr;
    1943                 :            : 
    1944   [ +  -  +  - ]:       1871 :         spdk_nvme_ctrlr_get_default_io_qpair_opts(nvme_ctrlr->ctrlr, &opts, sizeof(opts));
    1945         [ +  - ]:       1871 :         opts.create_only = true;
    1946                 :            :         /* In interrupt mode qpairs must be created in sync mode, else it will never be connected.
    1947                 :            :          * delay_cmd_submit must be false as in interrupt mode requests cannot be submitted in
    1948                 :            :          * completion context.
    1949                 :            :          */
    1950         [ +  + ]:       1871 :         if (!spdk_interrupt_mode_is_enabled()) {
    1951                 :       1871 :                 opts.async_mode = true;
    1952   [ +  +  +  -  :       1871 :                 opts.delay_cmd_submit = g_opts.delay_cmd_submit;
             +  -  +  - ]
    1953                 :        144 :         }
    1954   [ +  +  +  -  :       1871 :         opts.io_queue_requests = spdk_max(g_opts.io_queue_requests, opts.io_queue_requests);
          -  +  #  #  +  
                -  +  - ]
    1955   [ +  -  +  - ]:       1871 :         g_opts.io_queue_requests = opts.io_queue_requests;
    1956                 :            : 
    1957   [ +  -  +  - ]:       1871 :         qpair = spdk_nvme_ctrlr_alloc_io_qpair(nvme_ctrlr->ctrlr, &opts, sizeof(opts));
    1958         [ +  + ]:       1871 :         if (qpair == NULL) {
    1959                 :          0 :                 return -1;
    1960                 :            :         }
    1961                 :            : 
    1962                 :        312 :         SPDK_DTRACE_PROBE3(bdev_nvme_create_qpair, nvme_ctrlr->nbdev_ctrlr->name,
    1963                 :            :                            spdk_nvme_qpair_get_id(qpair), spdk_thread_get_id(nvme_ctrlr->thread));
    1964                 :            : 
    1965   [ +  +  +  -  :       1871 :         assert(nvme_qpair->group != NULL);
             +  -  #  # ]
    1966                 :            : 
    1967   [ +  -  +  -  :       1871 :         rc = spdk_nvme_poll_group_add(nvme_qpair->group->group, qpair);
             +  -  +  - ]
    1968         [ -  + ]:       1871 :         if (rc != 0) {
    1969   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "Unable to begin polling on NVMe Channel.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    1970                 :          0 :                 goto err;
    1971                 :            :         }
    1972                 :            : 
    1973   [ +  -  +  - ]:       1871 :         rc = spdk_nvme_ctrlr_connect_io_qpair(nvme_ctrlr->ctrlr, qpair);
    1974         [ -  + ]:       1871 :         if (rc != 0) {
    1975   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "Unable to connect I/O qpair.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    1976                 :          0 :                 goto err;
    1977                 :            :         }
    1978                 :            : 
    1979   [ +  -  +  - ]:       1871 :         nvme_qpair->qpair = qpair;
    1980                 :            : 
    1981   [ +  +  +  +  :       1871 :         if (!g_opts.disable_auto_failback) {
                   -  + ]
    1982                 :       1723 :                 _bdev_nvme_clear_io_path_cache(nvme_qpair);
    1983                 :        107 :         }
    1984                 :            : 
    1985   [ +  +  +  +  :       1871 :         NVME_CTRLR_INFOLOG(nvme_ctrlr, "Connecting qpair %p:%u started.\n",
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1986                 :            :                            qpair, spdk_nvme_qpair_get_id(qpair));
    1987                 :            : 
    1988                 :       1871 :         return 0;
    1989                 :            : 
    1990                 :          0 : err:
    1991                 :          0 :         spdk_nvme_ctrlr_free_io_qpair(qpair);
    1992                 :            : 
    1993                 :          0 :         return rc;
    1994                 :        144 : }
    1995                 :            : 
    1996                 :            : static void bdev_nvme_reset_io_continue(void *cb_arg, int rc);
    1997                 :            : 
    1998                 :            : static void
    1999                 :        357 : bdev_nvme_complete_pending_resets(struct nvme_ctrlr *nvme_ctrlr, bool success)
    2000                 :            : {
    2001                 :        357 :         int rc = 0;
    2002                 :            :         struct nvme_bdev_io *bio;
    2003                 :            : 
    2004   [ +  +  #  # ]:        357 :         if (!success) {
    2005                 :        157 :                 rc = -1;
    2006                 :         33 :         }
    2007                 :            : 
    2008   [ +  +  #  #  :        405 :         while (!TAILQ_EMPTY(&nvme_ctrlr->pending_resets)) {
             #  #  #  # ]
    2009   [ #  #  #  #  :         48 :                 bio = TAILQ_FIRST(&nvme_ctrlr->pending_resets);
                   #  # ]
    2010   [ -  +  #  #  :         48 :                 TAILQ_REMOVE(&nvme_ctrlr->pending_resets, bio, retry_link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2011                 :            : 
    2012                 :         48 :                 bdev_nvme_reset_io_continue(bio, rc);
    2013                 :            :         }
    2014                 :        357 : }
    2015                 :            : 
    2016                 :            : /* This function marks the current trid as failed by storing the current ticks
    2017                 :            :  * and then sets the next trid to the active trid within a controller if exists.
    2018                 :            :  *
    2019                 :            :  * The purpose of the boolean return value is to request the caller to disconnect
    2020                 :            :  * the current trid now to try connecting the next trid.
    2021                 :            :  */
    2022                 :            : static bool
    2023                 :        288 : bdev_nvme_failover_trid(struct nvme_ctrlr *nvme_ctrlr, bool remove, bool start)
    2024                 :            : {
    2025                 :            :         struct nvme_path_id *path_id, *next_path;
    2026                 :            :         int rc __attribute__((unused));
    2027                 :            : 
    2028   [ #  #  #  #  :        288 :         path_id = TAILQ_FIRST(&nvme_ctrlr->trids);
                   #  # ]
    2029   [ +  +  #  # ]:        288 :         assert(path_id);
    2030   [ +  +  #  #  :        288 :         assert(path_id == nvme_ctrlr->active_path_id);
             #  #  #  # ]
    2031   [ #  #  #  #  :        288 :         next_path = TAILQ_NEXT(path_id, link);
                   #  # ]
    2032                 :            : 
    2033                 :            :         /* Update the last failed time. It means the trid is failed if its last
    2034                 :            :          * failed time is non-zero.
    2035                 :            :          */
    2036   [ #  #  #  # ]:        288 :         path_id->last_failed_tsc = spdk_get_ticks();
    2037                 :            : 
    2038         [ +  + ]:        288 :         if (next_path == NULL) {
    2039                 :            :                 /* There is no alternate trid within a controller. */
    2040                 :        240 :                 return false;
    2041                 :            :         }
    2042                 :            : 
    2043   [ +  +  +  +  :         48 :         if (!start && nvme_ctrlr->opts.reconnect_delay_sec == 0) {
          #  #  #  #  #  
                #  #  # ]
    2044                 :            :                 /* Connect is not retried in a controller reset sequence. Connecting
    2045                 :            :                  * the next trid will be done by the next bdev_nvme_failover_ctrlr() call.
    2046                 :            :                  */
    2047                 :         12 :                 return false;
    2048                 :            :         }
    2049                 :            : 
    2050   [ +  +  #  #  :         36 :         assert(path_id->trid.trtype != SPDK_NVME_TRANSPORT_PCIE);
          #  #  #  #  #  
                      # ]
    2051                 :            : 
    2052   [ +  -  #  #  :         36 :         NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Start failover from %s:%s to %s:%s\n",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2053                 :            :                              path_id->trid.traddr, path_id->trid.trsvcid,
    2054                 :            :                              next_path->trid.traddr, next_path->trid.trsvcid);
    2055                 :            : 
    2056   [ #  #  #  # ]:         36 :         spdk_nvme_ctrlr_fail(nvme_ctrlr->ctrlr);
    2057   [ #  #  #  # ]:         36 :         nvme_ctrlr->active_path_id = next_path;
    2058   [ #  #  #  #  :         36 :         rc = spdk_nvme_ctrlr_set_trid(nvme_ctrlr->ctrlr, &next_path->trid);
                   #  # ]
    2059   [ -  +  #  # ]:         36 :         assert(rc == 0);
    2060   [ +  -  #  #  :         36 :         TAILQ_REMOVE(&nvme_ctrlr->trids, path_id, link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2061   [ +  +  #  # ]:         36 :         if (!remove) {
    2062                 :            :                 /** Shuffle the old trid to the end of the list and use the new one.
    2063                 :            :                  * Allows for round robin through multiple connections.
    2064                 :            :                  */
    2065   [ #  #  #  #  :         27 :                 TAILQ_INSERT_TAIL(&nvme_ctrlr->trids, path_id, link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    2066                 :          6 :         } else {
    2067                 :          9 :                 free(path_id);
    2068                 :            :         }
    2069                 :            : 
    2070   [ +  +  +  +  :         36 :         if (start || next_path->last_failed_tsc == 0) {
          #  #  #  #  #  
                      # ]
    2071                 :            :                 /* bdev_nvme_failover_ctrlr() is just called or the next trid is not failed
    2072                 :            :                  * or used yet. Try the next trid now.
    2073                 :            :                  */
    2074                 :         32 :                 return true;
    2075                 :            :         }
    2076                 :            : 
    2077   [ -  +  -  +  :          5 :         if (spdk_get_ticks() > next_path->last_failed_tsc + spdk_get_ticks_hz() *
                   #  # ]
    2078   [ -  +  #  #  :          4 :             nvme_ctrlr->opts.reconnect_delay_sec) {
                   #  # ]
    2079                 :            :                 /* Enough backoff passed since the next trid failed. Try the next trid now. */
    2080                 :          0 :                 return true;
    2081                 :            :         }
    2082                 :            : 
    2083                 :            :         /* The next trid will be tried after reconnect_delay_sec seconds. */
    2084                 :          4 :         return false;
    2085                 :         62 : }
    2086                 :            : 
    2087                 :            : static bool
    2088                 :      47707 : bdev_nvme_check_ctrlr_loss_timeout(struct nvme_ctrlr *nvme_ctrlr)
    2089                 :            : {
    2090                 :            :         int32_t elapsed;
    2091                 :            : 
    2092   [ +  +  +  +  :      47707 :         if (nvme_ctrlr->opts.ctrlr_loss_timeout_sec == 0 ||
          #  #  #  #  #  
                      # ]
    2093   [ +  +  #  #  :      39573 :             nvme_ctrlr->opts.ctrlr_loss_timeout_sec == -1) {
                   #  # ]
    2094                 :       9904 :                 return false;
    2095                 :            :         }
    2096                 :            : 
    2097   [ -  +  #  #  :      37803 :         elapsed = (spdk_get_ticks() - nvme_ctrlr->reset_start_tsc) / spdk_get_ticks_hz();
                   #  # ]
    2098   [ +  +  #  #  :      37803 :         if (elapsed >= nvme_ctrlr->opts.ctrlr_loss_timeout_sec) {
             #  #  #  # ]
    2099                 :         38 :                 return true;
    2100                 :            :         } else {
    2101                 :      37765 :                 return false;
    2102                 :            :         }
    2103                 :        280 : }
    2104                 :            : 
    2105                 :            : static bool
    2106                 :         66 : bdev_nvme_check_fast_io_fail_timeout(struct nvme_ctrlr *nvme_ctrlr)
    2107                 :            : {
    2108                 :            :         uint32_t elapsed;
    2109                 :            : 
    2110   [ +  +  #  #  :         66 :         if (nvme_ctrlr->opts.fast_io_fail_timeout_sec == 0) {
             #  #  #  # ]
    2111                 :         43 :                 return false;
    2112                 :            :         }
    2113                 :            : 
    2114   [ -  +  #  #  :         23 :         elapsed = (spdk_get_ticks() - nvme_ctrlr->reset_start_tsc) / spdk_get_ticks_hz();
                   #  # ]
    2115   [ +  +  #  #  :         23 :         if (elapsed >= nvme_ctrlr->opts.fast_io_fail_timeout_sec) {
             #  #  #  # ]
    2116                 :         11 :                 return true;
    2117                 :            :         } else {
    2118                 :         12 :                 return false;
    2119                 :            :         }
    2120                 :         12 : }
    2121                 :            : 
    2122                 :            : static void bdev_nvme_reset_ctrlr_complete(struct nvme_ctrlr *nvme_ctrlr, bool success);
    2123                 :            : 
    2124                 :            : static void
    2125                 :        361 : nvme_ctrlr_disconnect(struct nvme_ctrlr *nvme_ctrlr, nvme_ctrlr_disconnected_cb cb_fn)
    2126                 :            : {
    2127                 :            :         int rc;
    2128                 :            : 
    2129   [ +  +  +  +  :        361 :         NVME_CTRLR_INFOLOG(nvme_ctrlr, "Start disconnecting ctrlr.\n");
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2130                 :            : 
    2131   [ #  #  #  # ]:        361 :         rc = spdk_nvme_ctrlr_disconnect(nvme_ctrlr->ctrlr);
    2132         [ +  + ]:        361 :         if (rc != 0) {
    2133   [ +  -  #  #  :          4 :                 NVME_CTRLR_WARNLOG(nvme_ctrlr, "disconnecting ctrlr failed.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2134                 :            : 
    2135                 :            :                 /* Disconnect fails if ctrlr is already resetting or removed. In this case,
    2136                 :            :                  * fail the reset sequence immediately.
    2137                 :            :                  */
    2138                 :          4 :                 bdev_nvme_reset_ctrlr_complete(nvme_ctrlr, false);
    2139                 :          4 :                 return;
    2140                 :            :         }
    2141                 :            : 
    2142                 :            :         /* spdk_nvme_ctrlr_disconnect() may complete asynchronously later by polling adminq.
    2143                 :            :          * Set callback here to execute the specified operation after ctrlr is really disconnected.
    2144                 :            :          */
    2145   [ +  +  #  #  :        357 :         assert(nvme_ctrlr->disconnected_cb == NULL);
             #  #  #  # ]
    2146   [ #  #  #  # ]:        357 :         nvme_ctrlr->disconnected_cb = cb_fn;
    2147                 :            : 
    2148                 :            :         /* During disconnection, reduce the period to poll adminq more often. */
    2149                 :        357 :         bdev_nvme_change_adminq_poll_period(nvme_ctrlr, 0);
    2150                 :         74 : }
    2151                 :            : 
    2152                 :            : enum bdev_nvme_op_after_reset {
    2153                 :            :         OP_NONE,
    2154                 :            :         OP_COMPLETE_PENDING_DESTRUCT,
    2155                 :            :         OP_DESTRUCT,
    2156                 :            :         OP_DELAYED_RECONNECT,
    2157                 :            :         OP_FAILOVER,
    2158                 :            : };
    2159                 :            : 
    2160                 :            : typedef enum bdev_nvme_op_after_reset _bdev_nvme_op_after_reset;
    2161                 :            : 
    2162                 :            : static _bdev_nvme_op_after_reset
    2163                 :        357 : bdev_nvme_check_op_after_reset(struct nvme_ctrlr *nvme_ctrlr, bool success)
    2164                 :            : {
    2165         [ +  + ]:        357 :         if (nvme_ctrlr_can_be_unregistered(nvme_ctrlr)) {
    2166                 :            :                 /* Complete pending destruct after reset completes. */
    2167                 :          1 :                 return OP_COMPLETE_PENDING_DESTRUCT;
    2168   [ +  +  #  # ]:        356 :         } else if (nvme_ctrlr->pending_failover) {
    2169         [ #  # ]:         12 :                 nvme_ctrlr->pending_failover = false;
    2170   [ #  #  #  # ]:         12 :                 nvme_ctrlr->reset_start_tsc = 0;
    2171                 :         12 :                 return OP_FAILOVER;
    2172   [ +  +  +  +  :        344 :         } else if (success || nvme_ctrlr->opts.reconnect_delay_sec == 0) {
          #  #  #  #  #  
                #  #  # ]
    2173   [ #  #  #  # ]:        263 :                 nvme_ctrlr->reset_start_tsc = 0;
    2174                 :        263 :                 return OP_NONE;
    2175         [ +  + ]:         81 :         } else if (bdev_nvme_check_ctrlr_loss_timeout(nvme_ctrlr)) {
    2176                 :         15 :                 return OP_DESTRUCT;
    2177                 :            :         } else {
    2178         [ +  + ]:         66 :                 if (bdev_nvme_check_fast_io_fail_timeout(nvme_ctrlr)) {
    2179         [ #  # ]:         11 :                         nvme_ctrlr->fast_io_fail_timedout = true;
    2180                 :          2 :                 }
    2181                 :         66 :                 return OP_DELAYED_RECONNECT;
    2182                 :            :         }
    2183                 :         73 : }
    2184                 :            : 
    2185                 :            : static int bdev_nvme_delete_ctrlr(struct nvme_ctrlr *nvme_ctrlr, bool hotplug);
    2186                 :            : static void bdev_nvme_reconnect_ctrlr(struct nvme_ctrlr *nvme_ctrlr);
    2187                 :            : 
    2188                 :            : static int
    2189                 :         54 : bdev_nvme_reconnect_delay_timer_expired(void *ctx)
    2190                 :            : {
    2191                 :         54 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    2192                 :            : 
    2193                 :         18 :         SPDK_DTRACE_PROBE1(bdev_nvme_ctrlr_reconnect_delay, nvme_ctrlr->nbdev_ctrlr->name);
    2194   [ -  +  #  # ]:         54 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2195                 :            : 
    2196         [ #  # ]:         54 :         spdk_poller_unregister(&nvme_ctrlr->reconnect_delay_timer);
    2197                 :            : 
    2198   [ +  +  #  # ]:         54 :         if (!nvme_ctrlr->reconnect_is_delayed) {
    2199   [ #  #  #  # ]:          0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2200                 :          0 :                 return SPDK_POLLER_BUSY;
    2201                 :            :         }
    2202                 :            : 
    2203         [ #  # ]:         54 :         nvme_ctrlr->reconnect_is_delayed = false;
    2204                 :            : 
    2205   [ -  +  #  # ]:         54 :         if (nvme_ctrlr->destruct) {
    2206   [ #  #  #  # ]:          0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2207                 :          0 :                 return SPDK_POLLER_BUSY;
    2208                 :            :         }
    2209                 :            : 
    2210   [ -  +  #  #  :         54 :         assert(nvme_ctrlr->resetting == false);
                   #  # ]
    2211         [ #  # ]:         54 :         nvme_ctrlr->resetting = true;
    2212                 :            : 
    2213   [ -  +  #  # ]:         54 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2214                 :            : 
    2215   [ #  #  #  # ]:         54 :         spdk_poller_resume(nvme_ctrlr->adminq_timer_poller);
    2216                 :            : 
    2217                 :         54 :         bdev_nvme_reconnect_ctrlr(nvme_ctrlr);
    2218                 :         54 :         return SPDK_POLLER_BUSY;
    2219                 :          9 : }
    2220                 :            : 
    2221                 :            : static void
    2222                 :         66 : bdev_nvme_start_reconnect_delay_timer(struct nvme_ctrlr *nvme_ctrlr)
    2223                 :            : {
    2224   [ #  #  #  # ]:         66 :         spdk_poller_pause(nvme_ctrlr->adminq_timer_poller);
    2225                 :            : 
    2226   [ +  +  #  #  :         66 :         assert(nvme_ctrlr->reconnect_is_delayed == false);
                   #  # ]
    2227         [ #  # ]:         66 :         nvme_ctrlr->reconnect_is_delayed = true;
    2228                 :            : 
    2229   [ +  +  #  #  :         66 :         assert(nvme_ctrlr->reconnect_delay_timer == NULL);
             #  #  #  # ]
    2230   [ #  #  #  #  :         66 :         nvme_ctrlr->reconnect_delay_timer = SPDK_POLLER_REGISTER(bdev_nvme_reconnect_delay_timer_expired,
          #  #  #  #  #  
                      # ]
    2231                 :            :                                             nvme_ctrlr,
    2232                 :            :                                             nvme_ctrlr->opts.reconnect_delay_sec * SPDK_SEC_TO_USEC);
    2233                 :         66 : }
    2234                 :            : 
    2235                 :            : static void remove_discovery_entry(struct nvme_ctrlr *nvme_ctrlr);
    2236                 :            : 
    2237                 :            : static void
    2238                 :        357 : bdev_nvme_reset_ctrlr_complete(struct nvme_ctrlr *nvme_ctrlr, bool success)
    2239                 :            : {
    2240   [ #  #  #  # ]:        357 :         bdev_nvme_ctrlr_op_cb ctrlr_op_cb_fn = nvme_ctrlr->ctrlr_op_cb_fn;
    2241   [ #  #  #  # ]:        357 :         void *ctrlr_op_cb_arg = nvme_ctrlr->ctrlr_op_cb_arg;
    2242                 :            :         enum bdev_nvme_op_after_reset op_after_reset;
    2243                 :            : 
    2244   [ +  +  #  #  :        357 :         assert(nvme_ctrlr->thread == spdk_get_thread());
             #  #  #  # ]
    2245                 :            : 
    2246   [ -  +  #  # ]:        357 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2247   [ +  +  #  # ]:        357 :         if (!success) {
    2248                 :            :                 /* Connecting the active trid failed. Set the next alternate trid to the
    2249                 :            :                  * active trid if it exists.
    2250                 :            :                  */
    2251         [ +  + ]:        165 :                 if (bdev_nvme_failover_trid(nvme_ctrlr, false, false)) {
    2252                 :            :                         /* The next alternate trid exists and is ready to try. Try it now. */
    2253   [ -  +  #  # ]:          8 :                         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2254                 :            : 
    2255   [ +  +  -  +  :          8 :                         NVME_CTRLR_INFOLOG(nvme_ctrlr, "Try the next alternate trid %s:%s now.\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2256                 :            :                                            nvme_ctrlr->active_path_id->trid.traddr,
    2257                 :            :                                            nvme_ctrlr->active_path_id->trid.trsvcid);
    2258                 :            : 
    2259                 :          8 :                         nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_reconnect_ctrlr);
    2260                 :          8 :                         return;
    2261                 :            :                 }
    2262                 :            : 
    2263                 :            :                 /* We came here if there is no alternate trid or if the next trid exists but
    2264                 :            :                  * is not ready to try. We will try the active trid after reconnect_delay_sec
    2265                 :            :                  * seconds if it is non-zero or at the next reset call otherwise.
    2266                 :            :                  */
    2267                 :         33 :         } else {
    2268                 :            :                 /* Connecting the active trid succeeded. Clear the last failed time because it
    2269                 :            :                  * means the trid is failed if its last failed time is non-zero.
    2270                 :            :                  */
    2271   [ #  #  #  #  :        192 :                 nvme_ctrlr->active_path_id->last_failed_tsc = 0;
             #  #  #  # ]
    2272                 :            :         }
    2273                 :            : 
    2274   [ +  +  +  +  :        349 :         NVME_CTRLR_INFOLOG(nvme_ctrlr, "Clear pending resets.\n");
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2275                 :            : 
    2276                 :            :         /* Make sure we clear any pending resets before returning. */
    2277         [ #  # ]:        349 :         bdev_nvme_complete_pending_resets(nvme_ctrlr, success);
    2278                 :            : 
    2279   [ +  +  #  # ]:        349 :         if (!success) {
    2280   [ +  -  #  #  :        157 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "Resetting controller failed.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2281                 :         33 :         } else {
    2282   [ +  +  #  #  :        192 :                 NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Resetting controller successful.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2283                 :            :         }
    2284                 :            : 
    2285         [ #  # ]:        349 :         nvme_ctrlr->resetting = false;
    2286         [ #  # ]:        349 :         nvme_ctrlr->dont_retry = false;
    2287         [ #  # ]:        349 :         nvme_ctrlr->in_failover = false;
    2288                 :            : 
    2289   [ #  #  #  # ]:        349 :         nvme_ctrlr->ctrlr_op_cb_fn = NULL;
    2290   [ #  #  #  # ]:        349 :         nvme_ctrlr->ctrlr_op_cb_arg = NULL;
    2291                 :            : 
    2292         [ #  # ]:        349 :         op_after_reset = bdev_nvme_check_op_after_reset(nvme_ctrlr, success);
    2293   [ -  +  #  # ]:        349 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2294                 :            : 
    2295                 :            :         /* Delay callbacks when the next operation is a failover. */
    2296   [ +  +  +  + ]:        349 :         if (ctrlr_op_cb_fn && op_after_reset != OP_FAILOVER) {
    2297   [ +  +  #  #  :        108 :                 ctrlr_op_cb_fn(ctrlr_op_cb_arg, success ? 0 : -1);
                   #  # ]
    2298                 :         19 :         }
    2299                 :            : 
    2300   [ +  +  +  +  :        349 :         switch (op_after_reset) {
                      + ]
    2301                 :          1 :         case OP_COMPLETE_PENDING_DESTRUCT:
    2302                 :          1 :                 nvme_ctrlr_unregister(nvme_ctrlr);
    2303                 :          1 :                 break;
    2304                 :         13 :         case OP_DESTRUCT:
    2305                 :         15 :                 bdev_nvme_delete_ctrlr(nvme_ctrlr, false);
    2306                 :         15 :                 remove_discovery_entry(nvme_ctrlr);
    2307                 :         15 :                 break;
    2308                 :         54 :         case OP_DELAYED_RECONNECT:
    2309                 :         66 :                 nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_start_reconnect_delay_timer);
    2310                 :         66 :                 break;
    2311                 :          9 :         case OP_FAILOVER:
    2312   [ #  #  #  # ]:         12 :                 nvme_ctrlr->ctrlr_op_cb_fn = ctrlr_op_cb_fn;
    2313   [ #  #  #  # ]:         12 :                 nvme_ctrlr->ctrlr_op_cb_arg = ctrlr_op_cb_arg;
    2314                 :         12 :                 bdev_nvme_failover_ctrlr(nvme_ctrlr);
    2315                 :         12 :                 break;
    2316                 :        201 :         default:
    2317                 :        255 :                 break;
    2318                 :            :         }
    2319                 :         73 : }
    2320                 :            : 
    2321                 :            : static void
    2322                 :          0 : bdev_nvme_reset_create_qpairs_failed(struct nvme_ctrlr *nvme_ctrlr, void *ctx, int status)
    2323                 :            : {
    2324                 :          0 :         bdev_nvme_reset_ctrlr_complete(nvme_ctrlr, false);
    2325                 :          0 : }
    2326                 :            : 
    2327                 :            : static void
    2328                 :        458 : bdev_nvme_reset_destroy_qpair(struct nvme_ctrlr_channel_iter *i,
    2329                 :            :                               struct nvme_ctrlr *nvme_ctrlr,
    2330                 :            :                               struct nvme_ctrlr_channel *ctrlr_ch, void *ctx)
    2331                 :            : {
    2332                 :            :         struct nvme_qpair *nvme_qpair;
    2333                 :            :         struct spdk_nvme_qpair *qpair;
    2334                 :            : 
    2335   [ #  #  #  # ]:        458 :         nvme_qpair = ctrlr_ch->qpair;
    2336   [ +  +  #  # ]:        458 :         assert(nvme_qpair != NULL);
    2337                 :            : 
    2338                 :        458 :         _bdev_nvme_clear_io_path_cache(nvme_qpair);
    2339                 :            : 
    2340   [ #  #  #  # ]:        458 :         qpair = nvme_qpair->qpair;
    2341         [ +  + ]:        458 :         if (qpair != NULL) {
    2342   [ +  +  -  +  :        309 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "Start disconnecting qpair %p:%u.\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2343                 :            :                                    qpair, spdk_nvme_qpair_get_id(qpair));
    2344                 :            : 
    2345   [ +  +  #  #  :        309 :                 if (nvme_qpair->ctrlr->dont_retry) {
             #  #  #  # ]
    2346                 :        245 :                         spdk_nvme_qpair_set_abort_dnr(qpair, true);
    2347                 :         54 :                 }
    2348                 :        309 :                 spdk_nvme_ctrlr_disconnect_io_qpair(qpair);
    2349                 :            : 
    2350                 :            :                 /* The current full reset sequence will move to the next
    2351                 :            :                  * ctrlr_channel after the qpair is actually disconnected.
    2352                 :            :                  */
    2353   [ -  +  #  #  :        309 :                 assert(ctrlr_ch->reset_iter == NULL);
             #  #  #  # ]
    2354   [ #  #  #  # ]:        309 :                 ctrlr_ch->reset_iter = i;
    2355                 :         70 :         } else {
    2356                 :        149 :                 nvme_ctrlr_for_each_channel_continue(i, 0);
    2357                 :            :         }
    2358                 :        458 : }
    2359                 :            : 
    2360                 :            : static void
    2361                 :        192 : bdev_nvme_reset_create_qpairs_done(struct nvme_ctrlr *nvme_ctrlr, void *ctx, int status)
    2362                 :            : {
    2363         [ +  + ]:        192 :         if (status == 0) {
    2364   [ +  +  -  +  :        192 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "qpairs were created after ctrlr reset.\n");
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2365                 :            : 
    2366                 :        192 :                 bdev_nvme_reset_ctrlr_complete(nvme_ctrlr, true);
    2367                 :         38 :         } else {
    2368   [ #  #  #  #  :          0 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "qpairs were failed to create after ctrlr reset.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2369                 :            : 
    2370                 :            :                 /* Delete the added qpairs and quiesce ctrlr to make the states clean. */
    2371                 :          0 :                 nvme_ctrlr_for_each_channel(nvme_ctrlr,
    2372                 :            :                                             bdev_nvme_reset_destroy_qpair,
    2373                 :            :                                             NULL,
    2374                 :            :                                             bdev_nvme_reset_create_qpairs_failed);
    2375                 :            :         }
    2376                 :        192 : }
    2377                 :            : 
    2378                 :            : static int
    2379                 :       3205 : bdev_nvme_reset_check_qpair_connected(void *ctx)
    2380                 :            : {
    2381                 :       3205 :         struct nvme_ctrlr_channel *ctrlr_ch = ctx;
    2382   [ #  #  #  # ]:       3205 :         struct nvme_qpair *nvme_qpair = ctrlr_ch->qpair;
    2383                 :            :         struct spdk_nvme_qpair *qpair;
    2384                 :            : 
    2385   [ +  +  #  #  :       3205 :         if (ctrlr_ch->reset_iter == NULL) {
                   #  # ]
    2386                 :            :                 /* qpair was already failed to connect and the reset sequence is being aborted. */
    2387   [ #  #  #  #  :          0 :                 assert(ctrlr_ch->connect_poller == NULL);
             #  #  #  # ]
    2388   [ #  #  #  #  :          0 :                 assert(nvme_qpair->qpair == NULL);
             #  #  #  # ]
    2389                 :            : 
    2390   [ #  #  #  #  :          0 :                 NVME_CTRLR_INFOLOG(nvme_qpair->ctrlr,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2391                 :            :                                    "qpair was already failed to connect. reset is being aborted.\n");
    2392                 :          0 :                 return SPDK_POLLER_BUSY;
    2393                 :            :         }
    2394                 :            : 
    2395   [ #  #  #  # ]:       3205 :         qpair = nvme_qpair->qpair;
    2396   [ +  +  #  # ]:       3205 :         assert(qpair != NULL);
    2397                 :            : 
    2398         [ +  + ]:       3205 :         if (!spdk_nvme_qpair_is_connected(qpair)) {
    2399                 :       2922 :                 return SPDK_POLLER_BUSY;
    2400                 :            :         }
    2401                 :            : 
    2402   [ +  +  -  +  :        283 :         NVME_CTRLR_INFOLOG(nvme_qpair->ctrlr, "qpair %p:%u was connected.\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2403                 :            :                            qpair, spdk_nvme_qpair_get_id(qpair));
    2404                 :            : 
    2405         [ #  # ]:        283 :         spdk_poller_unregister(&ctrlr_ch->connect_poller);
    2406                 :            : 
    2407                 :            :         /* qpair was completed to connect. Move to the next ctrlr_channel */
    2408   [ #  #  #  # ]:        283 :         nvme_ctrlr_for_each_channel_continue(ctrlr_ch->reset_iter, 0);
    2409   [ #  #  #  # ]:        283 :         ctrlr_ch->reset_iter = NULL;
    2410                 :            : 
    2411   [ +  +  +  +  :        283 :         if (!g_opts.disable_auto_failback) {
                   #  # ]
    2412                 :        215 :                 _bdev_nvme_clear_io_path_cache(nvme_qpair);
    2413                 :         45 :         }
    2414                 :            : 
    2415                 :        283 :         return SPDK_POLLER_BUSY;
    2416                 :         89 : }
    2417                 :            : 
    2418                 :            : static void
    2419                 :        283 : bdev_nvme_reset_create_qpair(struct nvme_ctrlr_channel_iter *i,
    2420                 :            :                              struct nvme_ctrlr *nvme_ctrlr,
    2421                 :            :                              struct nvme_ctrlr_channel *ctrlr_ch,
    2422                 :            :                              void *ctx)
    2423                 :            : {
    2424   [ #  #  #  # ]:        283 :         struct nvme_qpair *nvme_qpair = ctrlr_ch->qpair;
    2425                 :            :         struct spdk_nvme_qpair *qpair;
    2426                 :            :         int rc;
    2427                 :            : 
    2428                 :        283 :         rc = bdev_nvme_create_qpair(nvme_qpair);
    2429         [ +  + ]:        283 :         if (rc == 0) {
    2430   [ #  #  #  # ]:        283 :                 ctrlr_ch->connect_poller = SPDK_POLLER_REGISTER(bdev_nvme_reset_check_qpair_connected,
    2431                 :            :                                            ctrlr_ch, 0);
    2432                 :            : 
    2433   [ #  #  #  # ]:        283 :                 qpair = nvme_qpair->qpair;
    2434                 :            : 
    2435   [ +  +  -  +  :        283 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "Start checking qpair %p:%u to be connected.\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2436                 :            :                                    qpair, spdk_nvme_qpair_get_id(qpair));
    2437                 :            : 
    2438                 :            :                 /* The current full reset sequence will move to the next
    2439                 :            :                  * ctrlr_channel after the qpair is actually connected.
    2440                 :            :                  */
    2441   [ -  +  #  #  :        283 :                 assert(ctrlr_ch->reset_iter == NULL);
             #  #  #  # ]
    2442   [ #  #  #  # ]:        283 :                 ctrlr_ch->reset_iter = i;
    2443                 :         62 :         } else {
    2444                 :          0 :                 nvme_ctrlr_for_each_channel_continue(i, rc);
    2445                 :            :         }
    2446                 :        283 : }
    2447                 :            : 
    2448                 :            : static void
    2449                 :        192 : nvme_ctrlr_check_namespaces(struct nvme_ctrlr *nvme_ctrlr)
    2450                 :            : {
    2451   [ #  #  #  # ]:        192 :         struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
    2452                 :            :         struct nvme_ns *nvme_ns;
    2453                 :            : 
    2454         [ +  + ]:        215 :         for (nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
    2455         [ +  + ]:        335 :              nvme_ns != NULL;
    2456                 :        143 :              nvme_ns = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns)) {
    2457   [ +  +  #  #  :        143 :                 if (!spdk_nvme_ctrlr_is_active_ns(ctrlr, nvme_ns->id)) {
                   #  # ]
    2458   [ +  +  -  +  :          4 :                         SPDK_DEBUGLOG(bdev_nvme, "NSID %u was removed during reset.\n", nvme_ns->id);
          #  #  #  #  #  
                      # ]
    2459                 :            :                         /* NS can be added again. Just nullify nvme_ns->ns. */
    2460   [ #  #  #  # ]:          4 :                         nvme_ns->ns = NULL;
    2461                 :          1 :                 }
    2462                 :         23 :         }
    2463                 :        192 : }
    2464                 :            : 
    2465                 :            : 
    2466                 :            : static int
    2467                 :      47606 : bdev_nvme_reconnect_ctrlr_poll(void *arg)
    2468                 :            : {
    2469                 :      47606 :         struct nvme_ctrlr *nvme_ctrlr = arg;
    2470                 :            :         struct spdk_nvme_transport_id *trid;
    2471                 :      47606 :         int rc = -ETIMEDOUT;
    2472                 :            : 
    2473         [ +  + ]:      47606 :         if (bdev_nvme_check_ctrlr_loss_timeout(nvme_ctrlr)) {
    2474                 :            :                 /* Mark the ctrlr as failed. The next call to
    2475                 :            :                  * spdk_nvme_ctrlr_reconnect_poll_async() will then
    2476                 :            :                  * do the necessary cleanup and return failure.
    2477                 :            :                  */
    2478   [ #  #  #  # ]:         15 :                 spdk_nvme_ctrlr_fail(nvme_ctrlr->ctrlr);
    2479                 :          2 :         }
    2480                 :            : 
    2481   [ #  #  #  # ]:      47606 :         rc = spdk_nvme_ctrlr_reconnect_poll_async(nvme_ctrlr->ctrlr);
    2482         [ +  + ]:      47606 :         if (rc == -EAGAIN) {
    2483                 :      47253 :                 return SPDK_POLLER_BUSY;
    2484                 :            :         }
    2485                 :            : 
    2486         [ #  # ]:        353 :         spdk_poller_unregister(&nvme_ctrlr->reset_detach_poller);
    2487         [ +  + ]:        353 :         if (rc == 0) {
    2488   [ #  #  #  #  :        192 :                 trid = &nvme_ctrlr->active_path_id->trid;
                   #  # ]
    2489                 :            : 
    2490   [ +  +  #  #  :        192 :                 if (spdk_nvme_trtype_is_fabrics(trid->trtype)) {
                   #  # ]
    2491   [ +  +  -  +  :        154 :                         NVME_CTRLR_INFOLOG(nvme_ctrlr, "ctrlr was connected to %s:%s. Create qpairs.\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2492                 :            :                                            trid->traddr, trid->trsvcid);
    2493                 :         36 :                 } else {
    2494   [ +  +  -  +  :         38 :                         NVME_CTRLR_INFOLOG(nvme_ctrlr, "ctrlr was connected. Create qpairs.\n");
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2495                 :            :                 }
    2496                 :            : 
    2497                 :        192 :                 nvme_ctrlr_check_namespaces(nvme_ctrlr);
    2498                 :            : 
    2499                 :            :                 /* Recreate all of the I/O queue pairs */
    2500                 :        192 :                 nvme_ctrlr_for_each_channel(nvme_ctrlr,
    2501                 :            :                                             bdev_nvme_reset_create_qpair,
    2502                 :            :                                             NULL,
    2503                 :            :                                             bdev_nvme_reset_create_qpairs_done);
    2504                 :         38 :         } else {
    2505   [ +  +  +  +  :        161 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "ctrlr could not be connected.\n");
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2506                 :            : 
    2507                 :        161 :                 bdev_nvme_reset_ctrlr_complete(nvme_ctrlr, false);
    2508                 :            :         }
    2509                 :        353 :         return SPDK_POLLER_BUSY;
    2510                 :        261 : }
    2511                 :            : 
    2512                 :            : static void
    2513                 :        353 : bdev_nvme_reconnect_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
    2514                 :            : {
    2515   [ +  +  +  +  :        353 :         NVME_CTRLR_INFOLOG(nvme_ctrlr, "Start reconnecting ctrlr.\n");
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2516                 :            : 
    2517   [ #  #  #  # ]:        353 :         spdk_nvme_ctrlr_reconnect_async(nvme_ctrlr->ctrlr);
    2518                 :            : 
    2519                 :         35 :         SPDK_DTRACE_PROBE1(bdev_nvme_ctrlr_reconnect, nvme_ctrlr->nbdev_ctrlr->name);
    2520   [ +  +  #  #  :        353 :         assert(nvme_ctrlr->reset_detach_poller == NULL);
             #  #  #  # ]
    2521   [ #  #  #  # ]:        353 :         nvme_ctrlr->reset_detach_poller = SPDK_POLLER_REGISTER(bdev_nvme_reconnect_ctrlr_poll,
    2522                 :            :                                           nvme_ctrlr, 0);
    2523                 :        353 : }
    2524                 :            : 
    2525                 :            : static void
    2526                 :        283 : bdev_nvme_reset_destroy_qpair_done(struct nvme_ctrlr *nvme_ctrlr, void *ctx, int status)
    2527                 :            : {
    2528                 :         17 :         SPDK_DTRACE_PROBE1(bdev_nvme_ctrlr_reset, nvme_ctrlr->nbdev_ctrlr->name);
    2529   [ +  +  #  # ]:        283 :         assert(status == 0);
    2530                 :            : 
    2531   [ +  +  +  +  :        283 :         NVME_CTRLR_INFOLOG(nvme_ctrlr, "qpairs were deleted.\n");
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2532                 :            : 
    2533   [ +  +  #  #  :        283 :         if (!spdk_nvme_ctrlr_is_fabrics(nvme_ctrlr->ctrlr)) {
                   #  # ]
    2534                 :         38 :                 bdev_nvme_reconnect_ctrlr(nvme_ctrlr);
    2535                 :          2 :         } else {
    2536                 :        245 :                 nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_reconnect_ctrlr);
    2537                 :            :         }
    2538                 :        283 : }
    2539                 :            : 
    2540                 :            : static void
    2541                 :        283 : bdev_nvme_reset_destroy_qpairs(struct nvme_ctrlr *nvme_ctrlr)
    2542                 :            : {
    2543   [ +  +  +  +  :        283 :         NVME_CTRLR_INFOLOG(nvme_ctrlr, "Delete qpairs for reset.\n");
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2544                 :            : 
    2545                 :        283 :         nvme_ctrlr_for_each_channel(nvme_ctrlr,
    2546                 :            :                                     bdev_nvme_reset_destroy_qpair,
    2547                 :            :                                     NULL,
    2548                 :            :                                     bdev_nvme_reset_destroy_qpair_done);
    2549                 :        283 : }
    2550                 :            : 
    2551                 :            : static void
    2552                 :         12 : bdev_nvme_reconnect_ctrlr_now(void *ctx)
    2553                 :            : {
    2554                 :         12 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    2555                 :            : 
    2556   [ +  +  #  #  :         12 :         assert(nvme_ctrlr->resetting == true);
                   #  # ]
    2557   [ +  +  #  #  :         12 :         assert(nvme_ctrlr->thread == spdk_get_thread());
             #  #  #  # ]
    2558                 :            : 
    2559         [ #  # ]:         12 :         spdk_poller_unregister(&nvme_ctrlr->reconnect_delay_timer);
    2560                 :            : 
    2561   [ #  #  #  # ]:         12 :         spdk_poller_resume(nvme_ctrlr->adminq_timer_poller);
    2562                 :            : 
    2563                 :         12 :         bdev_nvme_reconnect_ctrlr(nvme_ctrlr);
    2564                 :         12 : }
    2565                 :            : 
    2566                 :            : static void
    2567                 :        283 : _bdev_nvme_reset_ctrlr(void *ctx)
    2568                 :            : {
    2569                 :        283 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    2570                 :            : 
    2571   [ +  +  #  #  :        283 :         assert(nvme_ctrlr->resetting == true);
                   #  # ]
    2572   [ -  +  #  #  :        283 :         assert(nvme_ctrlr->thread == spdk_get_thread());
             #  #  #  # ]
    2573                 :            : 
    2574   [ +  +  #  #  :        283 :         if (!spdk_nvme_ctrlr_is_fabrics(nvme_ctrlr->ctrlr)) {
                   #  # ]
    2575                 :         38 :                 nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_reset_destroy_qpairs);
    2576                 :          2 :         } else {
    2577                 :        245 :                 bdev_nvme_reset_destroy_qpairs(nvme_ctrlr);
    2578                 :            :         }
    2579                 :        283 : }
    2580                 :            : 
    2581                 :            : static int
    2582                 :        240 : bdev_nvme_reset_ctrlr_unsafe(struct nvme_ctrlr *nvme_ctrlr, spdk_msg_fn *msg_fn)
    2583                 :            : {
    2584   [ +  +  #  # ]:        240 :         if (nvme_ctrlr->destruct) {
    2585                 :         12 :                 return -ENXIO;
    2586                 :            :         }
    2587                 :            : 
    2588   [ +  +  #  # ]:        228 :         if (nvme_ctrlr->resetting) {
    2589   [ +  -  #  #  :         56 :                 NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Unable to perform reset, already in progress.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2590                 :         56 :                 return -EBUSY;
    2591                 :            :         }
    2592                 :            : 
    2593   [ +  +  #  # ]:        172 :         if (nvme_ctrlr->disabled) {
    2594   [ +  -  #  #  :          4 :                 NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Unable to perform reset. Controller is disabled.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2595                 :          4 :                 return -EALREADY;
    2596                 :            :         }
    2597                 :            : 
    2598         [ #  # ]:        168 :         nvme_ctrlr->resetting = true;
    2599         [ #  # ]:        168 :         nvme_ctrlr->dont_retry = true;
    2600                 :            : 
    2601   [ +  +  #  # ]:        168 :         if (nvme_ctrlr->reconnect_is_delayed) {
    2602   [ +  +  -  +  :          4 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "Reconnect is already scheduled.\n");
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2603         [ #  # ]:          4 :                 *msg_fn = bdev_nvme_reconnect_ctrlr_now;
    2604         [ #  # ]:          4 :                 nvme_ctrlr->reconnect_is_delayed = false;
    2605                 :          1 :         } else {
    2606         [ #  # ]:        164 :                 *msg_fn = _bdev_nvme_reset_ctrlr;
    2607   [ +  +  #  #  :        164 :                 assert(nvme_ctrlr->reset_start_tsc == 0);
             #  #  #  # ]
    2608                 :            :         }
    2609                 :            : 
    2610   [ #  #  #  # ]:        168 :         nvme_ctrlr->reset_start_tsc = spdk_get_ticks();
    2611                 :            : 
    2612                 :        168 :         return 0;
    2613                 :         52 : }
    2614                 :            : 
    2615                 :            : static int
    2616                 :        103 : bdev_nvme_reset_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
    2617                 :            : {
    2618                 :         75 :         spdk_msg_fn msg_fn;
    2619                 :            :         int rc;
    2620                 :            : 
    2621   [ -  +  #  # ]:        103 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2622                 :        103 :         rc = bdev_nvme_reset_ctrlr_unsafe(nvme_ctrlr, &msg_fn);
    2623   [ -  +  #  # ]:        103 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2624                 :            : 
    2625         [ +  + ]:        103 :         if (rc == 0) {
    2626   [ #  #  #  # ]:         83 :                 spdk_thread_send_msg(nvme_ctrlr->thread, msg_fn, nvme_ctrlr);
    2627                 :         20 :         }
    2628                 :            : 
    2629                 :        103 :         return rc;
    2630                 :            : }
    2631                 :            : 
    2632                 :            : static int
    2633                 :         12 : bdev_nvme_enable_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
    2634                 :            : {
    2635   [ -  +  #  # ]:         12 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2636   [ -  +  #  # ]:         12 :         if (nvme_ctrlr->destruct) {
    2637   [ #  #  #  # ]:          0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2638                 :          0 :                 return -ENXIO;
    2639                 :            :         }
    2640                 :            : 
    2641   [ -  +  #  # ]:         12 :         if (nvme_ctrlr->resetting) {
    2642   [ #  #  #  # ]:          0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2643                 :          0 :                 return -EBUSY;
    2644                 :            :         }
    2645                 :            : 
    2646   [ +  +  #  # ]:         12 :         if (!nvme_ctrlr->disabled) {
    2647   [ -  +  #  # ]:          4 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2648                 :          4 :                 return -EALREADY;
    2649                 :            :         }
    2650                 :            : 
    2651         [ #  # ]:          8 :         nvme_ctrlr->disabled = false;
    2652         [ #  # ]:          8 :         nvme_ctrlr->resetting = true;
    2653                 :            : 
    2654   [ #  #  #  # ]:          8 :         nvme_ctrlr->reset_start_tsc = spdk_get_ticks();
    2655                 :            : 
    2656   [ -  +  #  # ]:          8 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2657                 :            : 
    2658   [ #  #  #  # ]:          8 :         spdk_thread_send_msg(nvme_ctrlr->thread, bdev_nvme_reconnect_ctrlr_now, nvme_ctrlr);
    2659                 :          8 :         return 0;
    2660                 :          3 : }
    2661                 :            : 
    2662                 :            : static void
    2663                 :          8 : bdev_nvme_disable_ctrlr_complete(struct nvme_ctrlr *nvme_ctrlr)
    2664                 :            : {
    2665   [ #  #  #  # ]:          8 :         bdev_nvme_ctrlr_op_cb ctrlr_op_cb_fn = nvme_ctrlr->ctrlr_op_cb_fn;
    2666   [ #  #  #  # ]:          8 :         void *ctrlr_op_cb_arg = nvme_ctrlr->ctrlr_op_cb_arg;
    2667                 :            :         enum bdev_nvme_op_after_reset op_after_disable;
    2668                 :            : 
    2669   [ +  +  #  #  :          8 :         assert(nvme_ctrlr->thread == spdk_get_thread());
             #  #  #  # ]
    2670                 :            : 
    2671   [ #  #  #  # ]:          8 :         nvme_ctrlr->ctrlr_op_cb_fn = NULL;
    2672   [ #  #  #  # ]:          8 :         nvme_ctrlr->ctrlr_op_cb_arg = NULL;
    2673                 :            : 
    2674   [ -  +  #  # ]:          8 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2675                 :            : 
    2676         [ #  # ]:          8 :         nvme_ctrlr->resetting = false;
    2677         [ #  # ]:          8 :         nvme_ctrlr->dont_retry = false;
    2678                 :            : 
    2679                 :          8 :         op_after_disable = bdev_nvme_check_op_after_reset(nvme_ctrlr, true);
    2680                 :            : 
    2681         [ #  # ]:          8 :         nvme_ctrlr->disabled = true;
    2682   [ #  #  #  # ]:          8 :         spdk_poller_pause(nvme_ctrlr->adminq_timer_poller);
    2683                 :            : 
    2684                 :            :         /* Make sure we clear any pending resets before returning. */
    2685                 :          8 :         bdev_nvme_complete_pending_resets(nvme_ctrlr, true);
    2686                 :            : 
    2687   [ -  +  #  # ]:          8 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2688                 :            : 
    2689         [ +  + ]:          8 :         if (ctrlr_op_cb_fn) {
    2690   [ #  #  #  # ]:          0 :                 ctrlr_op_cb_fn(ctrlr_op_cb_arg, 0);
    2691                 :          0 :         }
    2692                 :            : 
    2693         [ +  + ]:          8 :         switch (op_after_disable) {
    2694                 :          0 :         case OP_COMPLETE_PENDING_DESTRUCT:
    2695                 :          0 :                 nvme_ctrlr_unregister(nvme_ctrlr);
    2696                 :          0 :                 break;
    2697                 :          6 :         default:
    2698                 :          8 :                 break;
    2699                 :            :         }
    2700                 :          8 : }
    2701                 :            : 
    2702                 :            : static void
    2703                 :          4 : bdev_nvme_disable_destroy_qpairs_done(struct nvme_ctrlr *nvme_ctrlr, void *ctx, int status)
    2704                 :            : {
    2705   [ +  +  #  # ]:          4 :         assert(status == 0);
    2706                 :            : 
    2707   [ +  +  #  #  :          4 :         if (!spdk_nvme_ctrlr_is_fabrics(nvme_ctrlr->ctrlr)) {
                   #  # ]
    2708                 :          0 :                 bdev_nvme_disable_ctrlr_complete(nvme_ctrlr);
    2709                 :          0 :         } else {
    2710                 :          4 :                 nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_disable_ctrlr_complete);
    2711                 :            :         }
    2712                 :          4 : }
    2713                 :            : 
    2714                 :            : static void
    2715                 :          4 : bdev_nvme_disable_destroy_qpairs(struct nvme_ctrlr *nvme_ctrlr)
    2716                 :            : {
    2717                 :          4 :         nvme_ctrlr_for_each_channel(nvme_ctrlr,
    2718                 :            :                                     bdev_nvme_reset_destroy_qpair,
    2719                 :            :                                     NULL,
    2720                 :            :                                     bdev_nvme_disable_destroy_qpairs_done);
    2721                 :          4 : }
    2722                 :            : 
    2723                 :            : static void
    2724                 :          4 : _bdev_nvme_cancel_reconnect_and_disable_ctrlr(void *ctx)
    2725                 :            : {
    2726                 :          4 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    2727                 :            : 
    2728   [ +  +  #  #  :          4 :         assert(nvme_ctrlr->resetting == true);
                   #  # ]
    2729   [ +  +  #  #  :          4 :         assert(nvme_ctrlr->thread == spdk_get_thread());
             #  #  #  # ]
    2730                 :            : 
    2731         [ #  # ]:          4 :         spdk_poller_unregister(&nvme_ctrlr->reconnect_delay_timer);
    2732                 :            : 
    2733                 :          4 :         bdev_nvme_disable_ctrlr_complete(nvme_ctrlr);
    2734                 :          4 : }
    2735                 :            : 
    2736                 :            : static void
    2737                 :          4 : _bdev_nvme_disconnect_and_disable_ctrlr(void *ctx)
    2738                 :            : {
    2739                 :          4 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    2740                 :            : 
    2741   [ +  +  #  #  :          4 :         assert(nvme_ctrlr->resetting == true);
                   #  # ]
    2742   [ -  +  #  #  :          4 :         assert(nvme_ctrlr->thread == spdk_get_thread());
             #  #  #  # ]
    2743                 :            : 
    2744   [ +  +  #  #  :          4 :         if (!spdk_nvme_ctrlr_is_fabrics(nvme_ctrlr->ctrlr)) {
                   #  # ]
    2745                 :          0 :                 nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_disable_destroy_qpairs);
    2746                 :          0 :         } else {
    2747                 :          4 :                 bdev_nvme_disable_destroy_qpairs(nvme_ctrlr);
    2748                 :            :         }
    2749                 :          4 : }
    2750                 :            : 
    2751                 :            : static int
    2752                 :         20 : bdev_nvme_disable_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
    2753                 :            : {
    2754                 :            :         spdk_msg_fn msg_fn;
    2755                 :            : 
    2756   [ -  +  #  # ]:         20 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2757   [ +  +  #  # ]:         20 :         if (nvme_ctrlr->destruct) {
    2758   [ -  +  #  # ]:          4 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2759                 :          4 :                 return -ENXIO;
    2760                 :            :         }
    2761                 :            : 
    2762   [ +  +  #  # ]:         16 :         if (nvme_ctrlr->resetting) {
    2763   [ -  +  #  # ]:          4 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2764                 :          4 :                 return -EBUSY;
    2765                 :            :         }
    2766                 :            : 
    2767   [ +  +  #  # ]:         12 :         if (nvme_ctrlr->disabled) {
    2768   [ -  +  #  # ]:          4 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2769                 :          4 :                 return -EALREADY;
    2770                 :            :         }
    2771                 :            : 
    2772         [ #  # ]:          8 :         nvme_ctrlr->resetting = true;
    2773         [ #  # ]:          8 :         nvme_ctrlr->dont_retry = true;
    2774                 :            : 
    2775   [ +  +  #  # ]:          8 :         if (nvme_ctrlr->reconnect_is_delayed) {
    2776                 :          4 :                 msg_fn = _bdev_nvme_cancel_reconnect_and_disable_ctrlr;
    2777         [ #  # ]:          4 :                 nvme_ctrlr->reconnect_is_delayed = false;
    2778                 :          1 :         } else {
    2779                 :          4 :                 msg_fn = _bdev_nvme_disconnect_and_disable_ctrlr;
    2780                 :            :         }
    2781                 :            : 
    2782   [ #  #  #  # ]:          8 :         nvme_ctrlr->reset_start_tsc = spdk_get_ticks();
    2783                 :            : 
    2784   [ -  +  #  # ]:          8 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2785                 :            : 
    2786   [ #  #  #  # ]:          8 :         spdk_thread_send_msg(nvme_ctrlr->thread, msg_fn, nvme_ctrlr);
    2787                 :          8 :         return 0;
    2788                 :          5 : }
    2789                 :            : 
    2790                 :            : static int
    2791                 :         31 : nvme_ctrlr_op(struct nvme_ctrlr *nvme_ctrlr, enum nvme_ctrlr_op op,
    2792                 :            :               bdev_nvme_ctrlr_op_cb cb_fn, void *cb_arg)
    2793                 :            : {
    2794                 :            :         int rc;
    2795                 :            : 
    2796   [ +  +  -  + ]:         31 :         switch (op) {
    2797                 :         21 :         case NVME_CTRLR_OP_RESET:
    2798                 :         27 :                 rc = bdev_nvme_reset_ctrlr(nvme_ctrlr);
    2799                 :         27 :                 break;
    2800                 :          0 :         case NVME_CTRLR_OP_ENABLE:
    2801                 :          0 :                 rc = bdev_nvme_enable_ctrlr(nvme_ctrlr);
    2802                 :          0 :                 break;
    2803                 :          0 :         case NVME_CTRLR_OP_DISABLE:
    2804                 :          0 :                 rc = bdev_nvme_disable_ctrlr(nvme_ctrlr);
    2805                 :          0 :                 break;
    2806                 :          3 :         default:
    2807                 :          4 :                 rc = -EINVAL;
    2808                 :          4 :                 break;
    2809                 :            :         }
    2810                 :            : 
    2811         [ +  + ]:         31 :         if (rc == 0) {
    2812   [ +  +  #  #  :         19 :                 assert(nvme_ctrlr->ctrlr_op_cb_fn == NULL);
             #  #  #  # ]
    2813   [ -  +  #  #  :         19 :                 assert(nvme_ctrlr->ctrlr_op_cb_arg == NULL);
             #  #  #  # ]
    2814   [ #  #  #  # ]:         19 :                 nvme_ctrlr->ctrlr_op_cb_fn = cb_fn;
    2815   [ #  #  #  # ]:         19 :                 nvme_ctrlr->ctrlr_op_cb_arg = cb_arg;
    2816                 :          4 :         }
    2817                 :         31 :         return rc;
    2818                 :            : }
    2819                 :            : 
    2820                 :            : struct nvme_ctrlr_op_rpc_ctx {
    2821                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    2822                 :            :         struct spdk_thread *orig_thread;
    2823                 :            :         enum nvme_ctrlr_op op;
    2824                 :            :         int rc;
    2825                 :            :         bdev_nvme_ctrlr_op_cb cb_fn;
    2826                 :            :         void *cb_arg;
    2827                 :            : };
    2828                 :            : 
    2829                 :            : static void
    2830                 :         16 : _nvme_ctrlr_op_rpc_complete(void *_ctx)
    2831                 :            : {
    2832                 :         16 :         struct nvme_ctrlr_op_rpc_ctx *ctx = _ctx;
    2833                 :            : 
    2834   [ +  +  #  # ]:         16 :         assert(ctx != NULL);
    2835   [ +  +  #  #  :         16 :         assert(ctx->cb_fn != NULL);
             #  #  #  # ]
    2836                 :            : 
    2837   [ #  #  #  #  :         16 :         ctx->cb_fn(ctx->cb_arg, ctx->rc);
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    2838                 :            : 
    2839                 :         16 :         free(ctx);
    2840                 :         16 : }
    2841                 :            : 
    2842                 :            : static void
    2843                 :         16 : nvme_ctrlr_op_rpc_complete(void *cb_arg, int rc)
    2844                 :            : {
    2845                 :         16 :         struct nvme_ctrlr_op_rpc_ctx *ctx = cb_arg;
    2846                 :            : 
    2847   [ #  #  #  # ]:         16 :         ctx->rc = rc;
    2848                 :            : 
    2849   [ #  #  #  # ]:         16 :         spdk_thread_send_msg(ctx->orig_thread, _nvme_ctrlr_op_rpc_complete, ctx);
    2850                 :         16 : }
    2851                 :            : 
    2852                 :            : void
    2853                 :         16 : nvme_ctrlr_op_rpc(struct nvme_ctrlr *nvme_ctrlr, enum nvme_ctrlr_op op,
    2854                 :            :                   bdev_nvme_ctrlr_op_cb cb_fn, void *cb_arg)
    2855                 :            : {
    2856                 :            :         struct nvme_ctrlr_op_rpc_ctx *ctx;
    2857                 :            :         int rc;
    2858                 :            : 
    2859   [ +  +  #  # ]:         16 :         assert(cb_fn != NULL);
    2860                 :            : 
    2861                 :         16 :         ctx = calloc(1, sizeof(*ctx));
    2862         [ +  + ]:         16 :         if (ctx == NULL) {
    2863   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to allocate nvme_ctrlr_op_rpc_ctx.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2864   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, -ENOMEM);
    2865                 :          0 :                 return;
    2866                 :            :         }
    2867                 :            : 
    2868   [ #  #  #  # ]:         16 :         ctx->orig_thread = spdk_get_thread();
    2869   [ #  #  #  # ]:         16 :         ctx->cb_fn = cb_fn;
    2870   [ #  #  #  # ]:         16 :         ctx->cb_arg = cb_arg;
    2871                 :            : 
    2872                 :         16 :         rc = nvme_ctrlr_op(nvme_ctrlr, op, nvme_ctrlr_op_rpc_complete, ctx);
    2873         [ +  + ]:         16 :         if (rc == 0) {
    2874                 :          4 :                 return;
    2875         [ +  + ]:         12 :         } else if (rc == -EALREADY) {
    2876                 :          0 :                 rc = 0;
    2877                 :          0 :         }
    2878                 :            : 
    2879                 :         12 :         nvme_ctrlr_op_rpc_complete(ctx, rc);
    2880                 :          4 : }
    2881                 :            : 
    2882                 :            : static void nvme_bdev_ctrlr_op_rpc_continue(void *cb_arg, int rc);
    2883                 :            : 
    2884                 :            : static void
    2885                 :         15 : _nvme_bdev_ctrlr_op_rpc_continue(void *_ctx)
    2886                 :            : {
    2887                 :         15 :         struct nvme_ctrlr_op_rpc_ctx *ctx = _ctx;
    2888                 :            :         struct nvme_ctrlr *prev_nvme_ctrlr, *next_nvme_ctrlr;
    2889                 :            :         int rc;
    2890                 :            : 
    2891   [ #  #  #  # ]:         15 :         prev_nvme_ctrlr = ctx->nvme_ctrlr;
    2892   [ #  #  #  # ]:         15 :         ctx->nvme_ctrlr = NULL;
    2893                 :            : 
    2894   [ -  +  #  #  :         15 :         if (ctx->rc != 0) {
                   #  # ]
    2895                 :          0 :                 goto complete;
    2896                 :            :         }
    2897                 :            : 
    2898   [ #  #  #  #  :         15 :         next_nvme_ctrlr = TAILQ_NEXT(prev_nvme_ctrlr, tailq);
                   #  # ]
    2899         [ +  + ]:         15 :         if (next_nvme_ctrlr == NULL) {
    2900                 :         11 :                 goto complete;
    2901                 :            :         }
    2902                 :            : 
    2903   [ #  #  #  # ]:          4 :         rc = nvme_ctrlr_op(next_nvme_ctrlr, ctx->op, nvme_bdev_ctrlr_op_rpc_continue, ctx);
    2904         [ +  + ]:          4 :         if (rc == 0) {
    2905   [ #  #  #  # ]:          4 :                 ctx->nvme_ctrlr = next_nvme_ctrlr;
    2906                 :          4 :                 return;
    2907         [ #  # ]:          0 :         } else if (rc == -EALREADY) {
    2908   [ #  #  #  # ]:          0 :                 ctx->nvme_ctrlr = next_nvme_ctrlr;
    2909                 :          0 :                 rc = 0;
    2910                 :          0 :         }
    2911                 :            : 
    2912   [ #  #  #  # ]:          0 :         ctx->rc = rc;
    2913                 :            : 
    2914                 :          9 : complete:
    2915   [ #  #  #  #  :         11 :         ctx->cb_fn(ctx->cb_arg, ctx->rc);
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    2916                 :         11 :         free(ctx);
    2917                 :          3 : }
    2918                 :            : 
    2919                 :            : static void
    2920                 :         15 : nvme_bdev_ctrlr_op_rpc_continue(void *cb_arg, int rc)
    2921                 :            : {
    2922                 :         15 :         struct nvme_ctrlr_op_rpc_ctx *ctx = cb_arg;
    2923                 :            : 
    2924   [ #  #  #  # ]:         15 :         ctx->rc = rc;
    2925                 :            : 
    2926   [ #  #  #  # ]:         15 :         spdk_thread_send_msg(ctx->orig_thread, _nvme_bdev_ctrlr_op_rpc_continue, ctx);
    2927                 :         15 : }
    2928                 :            : 
    2929                 :            : void
    2930                 :         11 : nvme_bdev_ctrlr_op_rpc(struct nvme_bdev_ctrlr *nbdev_ctrlr, enum nvme_ctrlr_op op,
    2931                 :            :                        bdev_nvme_ctrlr_op_cb cb_fn, void *cb_arg)
    2932                 :            : {
    2933                 :            :         struct nvme_ctrlr_op_rpc_ctx *ctx;
    2934                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    2935                 :            :         int rc;
    2936                 :            : 
    2937   [ +  +  #  # ]:         11 :         assert(cb_fn != NULL);
    2938                 :            : 
    2939                 :         11 :         ctx = calloc(1, sizeof(*ctx));
    2940         [ +  + ]:         11 :         if (ctx == NULL) {
    2941                 :          0 :                 SPDK_ERRLOG("Failed to allocate nvme_ctrlr_op_rpc_ctx.\n");
    2942   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, -ENOMEM);
    2943                 :          0 :                 return;
    2944                 :            :         }
    2945                 :            : 
    2946   [ #  #  #  # ]:         11 :         ctx->orig_thread = spdk_get_thread();
    2947   [ #  #  #  # ]:         11 :         ctx->op = op;
    2948   [ #  #  #  # ]:         11 :         ctx->cb_fn = cb_fn;
    2949   [ #  #  #  # ]:         11 :         ctx->cb_arg = cb_arg;
    2950                 :            : 
    2951   [ #  #  #  #  :         11 :         nvme_ctrlr = TAILQ_FIRST(&nbdev_ctrlr->ctrlrs);
                   #  # ]
    2952   [ -  +  #  # ]:         11 :         assert(nvme_ctrlr != NULL);
    2953                 :            : 
    2954                 :         11 :         rc = nvme_ctrlr_op(nvme_ctrlr, op, nvme_bdev_ctrlr_op_rpc_continue, ctx);
    2955         [ +  + ]:         11 :         if (rc == 0) {
    2956   [ #  #  #  # ]:         11 :                 ctx->nvme_ctrlr = nvme_ctrlr;
    2957                 :         11 :                 return;
    2958         [ #  # ]:          0 :         } else if (rc == -EALREADY) {
    2959   [ #  #  #  # ]:          0 :                 ctx->nvme_ctrlr = nvme_ctrlr;
    2960                 :          0 :                 rc = 0;
    2961                 :          0 :         }
    2962                 :            : 
    2963                 :          0 :         nvme_bdev_ctrlr_op_rpc_continue(ctx, rc);
    2964                 :          2 : }
    2965                 :            : 
    2966                 :            : static int _bdev_nvme_reset_io(struct nvme_io_path *io_path, struct nvme_bdev_io *bio);
    2967                 :            : 
    2968                 :            : static void
    2969                 :         97 : bdev_nvme_unfreeze_bdev_channel_done(struct nvme_bdev *nbdev, void *ctx, int status)
    2970                 :            : {
    2971                 :         97 :         struct nvme_bdev_io *bio = ctx;
    2972                 :            :         enum spdk_bdev_io_status io_status;
    2973                 :            : 
    2974   [ +  +  #  #  :         97 :         if (bio->cpl.cdw0 == 0) {
             #  #  #  # ]
    2975                 :         81 :                 io_status = SPDK_BDEV_IO_STATUS_SUCCESS;
    2976                 :         13 :         } else {
    2977                 :         16 :                 io_status = SPDK_BDEV_IO_STATUS_FAILED;
    2978                 :            :         }
    2979                 :            : 
    2980   [ +  +  -  +  :         97 :         NVME_BDEV_INFOLOG(nbdev, "reset_io %p completed, status:%d\n", bio, io_status);
          #  #  #  #  #  
                #  #  # ]
    2981                 :            : 
    2982                 :         97 :         __bdev_nvme_io_complete(spdk_bdev_io_from_ctx(bio), io_status, NULL);
    2983                 :         97 : }
    2984                 :            : 
    2985                 :            : static void
    2986                 :        161 : bdev_nvme_unfreeze_bdev_channel(struct nvme_bdev_channel_iter *i,
    2987                 :            :                                 struct nvme_bdev *nbdev,
    2988                 :            :                                 struct nvme_bdev_channel *nbdev_ch, void *ctx)
    2989                 :            : {
    2990                 :        161 :         bdev_nvme_abort_retry_ios(nbdev_ch);
    2991   [ #  #  #  # ]:        161 :         nbdev_ch->resetting = false;
    2992                 :            : 
    2993                 :        161 :         nvme_bdev_for_each_channel_continue(i, 0);
    2994                 :        161 : }
    2995                 :            : 
    2996                 :            : static void
    2997                 :         97 : bdev_nvme_reset_io_complete(struct nvme_bdev_io *bio)
    2998                 :            : {
    2999                 :         97 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    3000   [ #  #  #  #  :         97 :         struct nvme_bdev *nbdev = (struct nvme_bdev *)bdev_io->bdev->ctxt;
             #  #  #  # ]
    3001                 :            : 
    3002                 :            :         /* Abort all queued I/Os for retry. */
    3003                 :        114 :         nvme_bdev_for_each_channel(nbdev,
    3004                 :            :                                    bdev_nvme_unfreeze_bdev_channel,
    3005                 :         17 :                                    bio,
    3006                 :            :                                    bdev_nvme_unfreeze_bdev_channel_done);
    3007                 :         97 : }
    3008                 :            : 
    3009                 :            : static void
    3010                 :        137 : _bdev_nvme_reset_io_continue(void *ctx)
    3011                 :            : {
    3012                 :        137 :         struct nvme_bdev_io *bio = ctx;
    3013                 :            :         struct nvme_io_path *prev_io_path, *next_io_path;
    3014                 :            :         int rc;
    3015                 :            : 
    3016   [ #  #  #  # ]:        137 :         prev_io_path = bio->io_path;
    3017   [ #  #  #  # ]:        137 :         bio->io_path = NULL;
    3018                 :            : 
    3019   [ #  #  #  #  :        137 :         next_io_path = STAILQ_NEXT(prev_io_path, stailq);
                   #  # ]
    3020         [ +  + ]:        137 :         if (next_io_path == NULL) {
    3021                 :         97 :                 goto complete;
    3022                 :            :         }
    3023                 :            : 
    3024                 :         40 :         rc = _bdev_nvme_reset_io(next_io_path, bio);
    3025         [ +  - ]:         40 :         if (rc == 0) {
    3026                 :         40 :                 return;
    3027                 :            :         }
    3028                 :            : 
    3029                 :          0 : complete:
    3030                 :         97 :         bdev_nvme_reset_io_complete(bio);
    3031                 :         27 : }
    3032                 :            : 
    3033                 :            : static void
    3034                 :        137 : bdev_nvme_reset_io_continue(void *cb_arg, int rc)
    3035                 :            : {
    3036                 :        137 :         struct nvme_bdev_io *bio = cb_arg;
    3037                 :        137 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    3038   [ #  #  #  #  :        137 :         struct nvme_bdev *nbdev = (struct nvme_bdev *)bdev_io->bdev->ctxt;
             #  #  #  # ]
    3039                 :            : 
    3040   [ +  +  -  +  :        137 :         NVME_BDEV_INFOLOG(nbdev, "continue reset_io %p, rc:%d\n", bio, rc);
          #  #  #  #  #  
                #  #  # ]
    3041                 :            : 
    3042                 :            :         /* Reset status is initialized as "failed". Set to "success" once we have at least one
    3043                 :            :          * successfully reset nvme_ctrlr.
    3044                 :            :          */
    3045         [ +  + ]:        137 :         if (rc == 0) {
    3046   [ #  #  #  #  :         97 :                 bio->cpl.cdw0 = 0;
                   #  # ]
    3047                 :         17 :         }
    3048                 :            : 
    3049                 :        137 :         spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io), _bdev_nvme_reset_io_continue, bio);
    3050                 :        137 : }
    3051                 :            : 
    3052                 :            : static int
    3053                 :        137 : _bdev_nvme_reset_io(struct nvme_io_path *io_path, struct nvme_bdev_io *bio)
    3054                 :            : {
    3055                 :        137 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    3056   [ #  #  #  #  :        137 :         struct nvme_bdev *nbdev = (struct nvme_bdev *)bdev_io->bdev->ctxt;
             #  #  #  # ]
    3057   [ #  #  #  #  :        137 :         struct nvme_ctrlr *nvme_ctrlr = io_path->qpair->ctrlr;
             #  #  #  # ]
    3058                 :         97 :         spdk_msg_fn msg_fn;
    3059                 :            :         int rc;
    3060                 :            : 
    3061   [ +  +  #  #  :        137 :         assert(bio->io_path == NULL);
             #  #  #  # ]
    3062   [ #  #  #  # ]:        137 :         bio->io_path = io_path;
    3063                 :            : 
    3064   [ -  +  #  # ]:        137 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    3065                 :        137 :         rc = bdev_nvme_reset_ctrlr_unsafe(nvme_ctrlr, &msg_fn);
    3066         [ +  + ]:        137 :         if (rc == -EBUSY) {
    3067                 :            :                 /*
    3068                 :            :                  * Reset call is queued only if it is from the app framework. This is on purpose so that
    3069                 :            :                  * we don't interfere with the app framework reset strategy. i.e. we are deferring to the
    3070                 :            :                  * upper level. If they are in the middle of a reset, we won't try to schedule another one.
    3071                 :            :                  */
    3072   [ #  #  #  #  :         48 :                 TAILQ_INSERT_TAIL(&nvme_ctrlr->pending_resets, bio, retry_link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    3073                 :         12 :         }
    3074   [ -  +  #  # ]:        137 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    3075                 :            : 
    3076         [ +  + ]:        137 :         if (rc == 0) {
    3077   [ +  +  #  #  :         85 :                 assert(nvme_ctrlr->ctrlr_op_cb_fn == NULL);
             #  #  #  # ]
    3078   [ +  +  #  #  :         85 :                 assert(nvme_ctrlr->ctrlr_op_cb_arg == NULL);
             #  #  #  # ]
    3079   [ #  #  #  # ]:         85 :                 nvme_ctrlr->ctrlr_op_cb_fn = bdev_nvme_reset_io_continue;
    3080   [ #  #  #  # ]:         85 :                 nvme_ctrlr->ctrlr_op_cb_arg = bio;
    3081                 :            : 
    3082   [ #  #  #  # ]:         85 :                 spdk_thread_send_msg(nvme_ctrlr->thread, msg_fn, nvme_ctrlr);
    3083                 :            : 
    3084   [ +  +  -  +  :         85 :                 NVME_BDEV_INFOLOG(nbdev, "reset_io %p started resetting ctrlr [%s, %u].\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    3085                 :            :                                   bio, CTRLR_STRING(nvme_ctrlr), CTRLR_ID(nvme_ctrlr));
    3086         [ +  + ]:         66 :         } else if (rc == -EBUSY) {
    3087                 :         48 :                 rc = 0;
    3088                 :            : 
    3089   [ +  +  -  +  :         48 :                 NVME_BDEV_INFOLOG(nbdev, "reset_io %p was queued to ctrlr [%s, %u].\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    3090                 :            :                                   bio, CTRLR_STRING(nvme_ctrlr), CTRLR_ID(nvme_ctrlr));
    3091                 :         12 :         } else {
    3092   [ +  +  -  +  :          4 :                 NVME_BDEV_INFOLOG(nbdev, "reset_io %p could not reset ctrlr [%s, %u], rc:%d\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    3093                 :            :                                   bio, CTRLR_STRING(nvme_ctrlr), CTRLR_ID(nvme_ctrlr), rc);
    3094                 :            :         }
    3095                 :            : 
    3096                 :        137 :         return rc;
    3097                 :            : }
    3098                 :            : 
    3099                 :            : static void
    3100                 :         97 : bdev_nvme_freeze_bdev_channel_done(struct nvme_bdev *nbdev, void *ctx, int status)
    3101                 :            : {
    3102                 :         97 :         struct nvme_bdev_io *bio = ctx;
    3103                 :         97 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    3104                 :            :         struct nvme_bdev_channel *nbdev_ch;
    3105                 :            :         struct nvme_io_path *io_path;
    3106                 :            :         int rc;
    3107                 :            : 
    3108                 :         97 :         nbdev_ch = spdk_io_channel_get_ctx(spdk_bdev_io_get_io_channel(bdev_io));
    3109                 :            : 
    3110                 :            :         /* Initialize with failed status. With multipath it is enough to have at least one successful
    3111                 :            :          * nvme_ctrlr reset. If there is none, reset status will remain failed.
    3112                 :            :          */
    3113   [ #  #  #  #  :         97 :         bio->cpl.cdw0 = 1;
                   #  # ]
    3114                 :            : 
    3115                 :            :         /* Reset all nvme_ctrlrs of a bdev controller sequentially. */
    3116   [ #  #  #  #  :         97 :         io_path = STAILQ_FIRST(&nbdev_ch->io_path_list);
                   #  # ]
    3117   [ +  +  #  # ]:         97 :         assert(io_path != NULL);
    3118                 :            : 
    3119                 :         97 :         rc = _bdev_nvme_reset_io(io_path, bio);
    3120         [ +  + ]:         97 :         if (rc != 0) {
    3121                 :            :                 /* If the current nvme_ctrlr is disabled, skip it and move to the next nvme_ctrlr. */
    3122         [ +  + ]:          4 :                 rc = (rc == -EALREADY) ? 0 : rc;
    3123                 :            : 
    3124                 :          4 :                 bdev_nvme_reset_io_continue(bio, rc);
    3125                 :          1 :         }
    3126                 :         97 : }
    3127                 :            : 
    3128                 :            : static void
    3129                 :        153 : bdev_nvme_freeze_bdev_channel(struct nvme_bdev_channel_iter *i,
    3130                 :            :                               struct nvme_bdev *nbdev,
    3131                 :            :                               struct nvme_bdev_channel *nbdev_ch, void *ctx)
    3132                 :            : {
    3133   [ #  #  #  # ]:        153 :         nbdev_ch->resetting = true;
    3134                 :            : 
    3135                 :        153 :         nvme_bdev_for_each_channel_continue(i, 0);
    3136                 :        153 : }
    3137                 :            : 
    3138                 :            : static void
    3139                 :         93 : bdev_nvme_reset_io(struct nvme_bdev *nbdev, struct nvme_bdev_io *bio)
    3140                 :            : {
    3141   [ +  +  -  +  :         93 :         NVME_BDEV_INFOLOG(nbdev, "reset_io %p started.\n", bio);
          #  #  #  #  #  
                #  #  # ]
    3142                 :            : 
    3143                 :        109 :         nvme_bdev_for_each_channel(nbdev,
    3144                 :            :                                    bdev_nvme_freeze_bdev_channel,
    3145                 :         16 :                                    bio,
    3146                 :            :                                    bdev_nvme_freeze_bdev_channel_done);
    3147                 :         93 : }
    3148                 :            : 
    3149                 :            : static int
    3150                 :        347 : bdev_nvme_failover_ctrlr_unsafe(struct nvme_ctrlr *nvme_ctrlr, bool remove)
    3151                 :            : {
    3152   [ +  +  #  # ]:        347 :         if (nvme_ctrlr->destruct) {
    3153                 :            :                 /* Don't bother resetting if the controller is in the process of being destructed. */
    3154                 :        209 :                 return -ENXIO;
    3155                 :            :         }
    3156                 :            : 
    3157   [ +  +  #  # ]:        138 :         if (nvme_ctrlr->resetting) {
    3158   [ +  +  #  # ]:         15 :                 if (!nvme_ctrlr->in_failover) {
    3159   [ +  -  #  #  :         12 :                         NVME_CTRLR_NOTICELOG(nvme_ctrlr,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    3160                 :            :                                              "Reset is already in progress. Defer failover until reset completes.\n");
    3161                 :            : 
    3162                 :            :                         /* Defer failover until reset completes. */
    3163         [ #  # ]:         12 :                         nvme_ctrlr->pending_failover = true;
    3164                 :         12 :                         return -EINPROGRESS;
    3165                 :            :                 } else {
    3166   [ +  -  #  #  :          3 :                         NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Unable to perform failover, already in progress.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    3167                 :          3 :                         return -EBUSY;
    3168                 :            :                 }
    3169                 :            :         }
    3170                 :            : 
    3171         [ #  # ]:        123 :         bdev_nvme_failover_trid(nvme_ctrlr, remove, true);
    3172                 :            : 
    3173   [ +  +  #  # ]:        123 :         if (nvme_ctrlr->reconnect_is_delayed) {
    3174   [ +  -  #  #  :          4 :                 NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Reconnect is already scheduled.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    3175                 :            : 
    3176                 :            :                 /* We rely on the next reconnect for the failover. */
    3177                 :          4 :                 return -EALREADY;
    3178                 :            :         }
    3179                 :            : 
    3180   [ -  +  #  # ]:        119 :         if (nvme_ctrlr->disabled) {
    3181   [ #  #  #  #  :          0 :                 NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Controller is disabled.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    3182                 :            : 
    3183                 :            :                 /* We rely on the enablement for the failover. */
    3184                 :          0 :                 return -EALREADY;
    3185                 :            :         }
    3186                 :            : 
    3187         [ #  # ]:        119 :         nvme_ctrlr->resetting = true;
    3188         [ #  # ]:        119 :         nvme_ctrlr->in_failover = true;
    3189                 :            : 
    3190   [ +  +  #  #  :        119 :         assert(nvme_ctrlr->reset_start_tsc == 0);
             #  #  #  # ]
    3191   [ #  #  #  # ]:        119 :         nvme_ctrlr->reset_start_tsc = spdk_get_ticks();
    3192                 :            : 
    3193                 :        119 :         return 0;
    3194                 :         32 : }
    3195                 :            : 
    3196                 :            : static int
    3197                 :        338 : bdev_nvme_failover_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
    3198                 :            : {
    3199                 :            :         int rc;
    3200                 :            : 
    3201   [ -  +  #  # ]:        338 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    3202                 :        338 :         rc = bdev_nvme_failover_ctrlr_unsafe(nvme_ctrlr, false);
    3203   [ -  +  #  # ]:        338 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    3204                 :            : 
    3205         [ +  + ]:        338 :         if (rc == 0) {
    3206   [ #  #  #  # ]:        114 :                 spdk_thread_send_msg(nvme_ctrlr->thread, _bdev_nvme_reset_ctrlr, nvme_ctrlr);
    3207         [ +  + ]:        249 :         } else if (rc == -EALREADY) {
    3208                 :          0 :                 rc = 0;
    3209                 :          0 :         }
    3210                 :            : 
    3211                 :        338 :         return rc;
    3212                 :            : }
    3213                 :            : 
    3214                 :            : static int bdev_nvme_unmap(struct nvme_bdev_io *bio, uint64_t offset_blocks,
    3215                 :            :                            uint64_t num_blocks);
    3216                 :            : 
    3217                 :            : static int bdev_nvme_write_zeroes(struct nvme_bdev_io *bio, uint64_t offset_blocks,
    3218                 :            :                                   uint64_t num_blocks);
    3219                 :            : 
    3220                 :            : static int bdev_nvme_copy(struct nvme_bdev_io *bio, uint64_t dst_offset_blocks,
    3221                 :            :                           uint64_t src_offset_blocks,
    3222                 :            :                           uint64_t num_blocks);
    3223                 :            : 
    3224                 :            : static void
    3225                 :    2682573 : bdev_nvme_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
    3226                 :            :                      bool success)
    3227                 :            : {
    3228         [ #  # ]:    2682573 :         struct nvme_bdev_io *bio = (struct nvme_bdev_io *)bdev_io->driver_ctx;
    3229                 :            :         int ret;
    3230                 :            : 
    3231   [ +  +  #  # ]:    2682573 :         if (!success) {
    3232                 :          0 :                 ret = -EINVAL;
    3233                 :          0 :                 goto exit;
    3234                 :            :         }
    3235                 :            : 
    3236   [ +  +  #  #  :    2682573 :         if (spdk_unlikely(!nvme_io_path_is_available(bio->io_path))) {
                   #  # ]
    3237                 :          0 :                 ret = -ENXIO;
    3238                 :          0 :                 goto exit;
    3239                 :            :         }
    3240                 :            : 
    3241                 :    2989228 :         ret = bdev_nvme_readv(bio,
    3242   [ #  #  #  #  :     306655 :                               bdev_io->u.bdev.iovs,
             #  #  #  # ]
    3243   [ #  #  #  #  :     306655 :                               bdev_io->u.bdev.iovcnt,
             #  #  #  # ]
    3244   [ #  #  #  #  :     306655 :                               bdev_io->u.bdev.md_buf,
             #  #  #  # ]
    3245   [ #  #  #  #  :     306655 :                               bdev_io->u.bdev.num_blocks,
             #  #  #  # ]
    3246   [ #  #  #  #  :     306655 :                               bdev_io->u.bdev.offset_blocks,
             #  #  #  # ]
    3247   [ #  #  #  #  :     306655 :                               bdev_io->u.bdev.dif_check_flags,
             #  #  #  # ]
    3248   [ #  #  #  #  :     306655 :                               bdev_io->u.bdev.memory_domain,
             #  #  #  # ]
    3249   [ #  #  #  #  :     306655 :                               bdev_io->u.bdev.memory_domain_ctx,
             #  #  #  # ]
    3250   [ #  #  #  #  :     306655 :                               bdev_io->u.bdev.accel_sequence);
             #  #  #  # ]
    3251                 :            : 
    3252                 :    2375918 : exit:
    3253         [ +  + ]:    2682573 :         if (spdk_unlikely(ret != 0)) {
    3254                 :       9242 :                 bdev_nvme_io_complete(bio, ret);
    3255                 :          0 :         }
    3256                 :    2682573 : }
    3257                 :            : 
    3258                 :            : static inline void
    3259                 :   16663161 : _bdev_nvme_submit_request(struct nvme_bdev_channel *nbdev_ch, struct spdk_bdev_io *bdev_io)
    3260                 :            : {
    3261         [ +  - ]:   16663161 :         struct nvme_bdev_io *nbdev_io = (struct nvme_bdev_io *)bdev_io->driver_ctx;
    3262   [ +  -  +  - ]:   16663161 :         struct spdk_bdev *bdev = bdev_io->bdev;
    3263                 :            :         struct nvme_bdev_io *nbdev_io_to_abort;
    3264                 :   16663161 :         int rc = 0;
    3265                 :            : 
    3266   [ +  +  +  +  :   16663161 :         switch (bdev_io->type) {
          +  +  +  +  +  
          +  +  +  +  +  
          -  +  +  +  -  
                -  -  - ]
    3267                 :    7845766 :         case SPDK_BDEV_IO_TYPE_READ:
    3268   [ +  +  +  +  :    8152550 :                 if (bdev_io->u.bdev.iovs && bdev_io->u.bdev.iovs[0].iov_base) {
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   -  + ]
    3269                 :            : 
    3270                 :    5470106 :                         rc = bdev_nvme_readv(nbdev_io,
    3271   [ -  +  -  +  :        129 :                                              bdev_io->u.bdev.iovs,
             -  +  -  + ]
    3272   [ -  +  -  +  :        129 :                                              bdev_io->u.bdev.iovcnt,
             -  +  -  + ]
    3273   [ -  +  -  +  :        129 :                                              bdev_io->u.bdev.md_buf,
             -  +  -  + ]
    3274   [ -  +  -  +  :        129 :                                              bdev_io->u.bdev.num_blocks,
             -  +  -  + ]
    3275   [ -  +  -  +  :        129 :                                              bdev_io->u.bdev.offset_blocks,
             -  +  -  + ]
    3276   [ -  +  -  +  :        129 :                                              bdev_io->u.bdev.dif_check_flags,
             -  +  -  + ]
    3277   [ -  +  -  +  :        129 :                                              bdev_io->u.bdev.memory_domain,
             -  +  -  + ]
    3278   [ -  +  -  +  :        129 :                                              bdev_io->u.bdev.memory_domain_ctx,
             -  +  -  + ]
    3279   [ -  +  -  +  :        129 :                                              bdev_io->u.bdev.accel_sequence);
             -  +  -  + ]
    3280                 :        129 :                 } else {
    3281                 :    2989228 :                         spdk_bdev_io_get_buf(bdev_io, bdev_nvme_get_buf_cb,
    3282   [ #  #  #  #  :    2682573 :                                              bdev_io->u.bdev.num_blocks * bdev->blocklen);
          #  #  #  #  #  
                #  #  # ]
    3283                 :    2682573 :                         rc = 0;
    3284                 :            :                 }
    3285                 :    8152550 :                 break;
    3286                 :    7007541 :         case SPDK_BDEV_IO_TYPE_WRITE:
    3287                 :    7621833 :                 rc = bdev_nvme_writev(nbdev_io,
    3288   [ #  #  #  #  :     307146 :                                       bdev_io->u.bdev.iovs,
             #  #  #  # ]
    3289   [ #  #  #  #  :     307146 :                                       bdev_io->u.bdev.iovcnt,
             #  #  #  # ]
    3290   [ #  #  #  #  :     307146 :                                       bdev_io->u.bdev.md_buf,
             #  #  #  # ]
    3291   [ #  #  #  #  :     307146 :                                       bdev_io->u.bdev.num_blocks,
             #  #  #  # ]
    3292   [ #  #  #  #  :     307146 :                                       bdev_io->u.bdev.offset_blocks,
             #  #  #  # ]
    3293   [ #  #  #  #  :     307146 :                                       bdev_io->u.bdev.dif_check_flags,
             #  #  #  # ]
    3294   [ #  #  #  #  :     307146 :                                       bdev_io->u.bdev.memory_domain,
             #  #  #  # ]
    3295   [ #  #  #  #  :     307146 :                                       bdev_io->u.bdev.memory_domain_ctx,
             #  #  #  # ]
    3296   [ #  #  #  #  :     307146 :                                       bdev_io->u.bdev.accel_sequence,
             #  #  #  # ]
    3297   [ #  #  #  #  :     307146 :                                       bdev_io->u.bdev.nvme_cdw12,
                   #  # ]
    3298   [ #  #  #  #  :     307146 :                                       bdev_io->u.bdev.nvme_cdw13);
                   #  # ]
    3299                 :    7314687 :                 break;
    3300                 :         53 :         case SPDK_BDEV_IO_TYPE_COMPARE:
    3301                 :         59 :                 rc = bdev_nvme_comparev(nbdev_io,
    3302   [ #  #  #  #  :          3 :                                         bdev_io->u.bdev.iovs,
             #  #  #  # ]
    3303   [ #  #  #  #  :          3 :                                         bdev_io->u.bdev.iovcnt,
             #  #  #  # ]
    3304   [ #  #  #  #  :          3 :                                         bdev_io->u.bdev.md_buf,
             #  #  #  # ]
    3305   [ #  #  #  #  :          3 :                                         bdev_io->u.bdev.num_blocks,
             #  #  #  # ]
    3306   [ #  #  #  #  :          3 :                                         bdev_io->u.bdev.offset_blocks,
             #  #  #  # ]
    3307   [ #  #  #  #  :          3 :                                         bdev_io->u.bdev.dif_check_flags);
             #  #  #  # ]
    3308                 :         56 :                 break;
    3309                 :         16 :         case SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE:
    3310                 :         20 :                 rc = bdev_nvme_comparev_and_writev(nbdev_io,
    3311   [ #  #  #  #  :          2 :                                                    bdev_io->u.bdev.iovs,
             #  #  #  # ]
    3312   [ #  #  #  #  :          2 :                                                    bdev_io->u.bdev.iovcnt,
             #  #  #  # ]
    3313   [ #  #  #  #  :          2 :                                                    bdev_io->u.bdev.fused_iovs,
             #  #  #  # ]
    3314   [ #  #  #  #  :          2 :                                                    bdev_io->u.bdev.fused_iovcnt,
             #  #  #  # ]
    3315   [ #  #  #  #  :          2 :                                                    bdev_io->u.bdev.md_buf,
             #  #  #  # ]
    3316   [ #  #  #  #  :          2 :                                                    bdev_io->u.bdev.num_blocks,
             #  #  #  # ]
    3317   [ #  #  #  #  :          2 :                                                    bdev_io->u.bdev.offset_blocks,
             #  #  #  # ]
    3318   [ #  #  #  #  :          2 :                                                    bdev_io->u.bdev.dif_check_flags);
             #  #  #  # ]
    3319                 :         18 :                 break;
    3320                 :      17150 :         case SPDK_BDEV_IO_TYPE_UNMAP:
    3321                 :      17156 :                 rc = bdev_nvme_unmap(nbdev_io,
    3322   [ #  #  #  #  :          3 :                                      bdev_io->u.bdev.offset_blocks,
             #  #  #  # ]
    3323   [ #  #  #  #  :          3 :                                      bdev_io->u.bdev.num_blocks);
             #  #  #  # ]
    3324                 :      17153 :                 break;
    3325                 :     704988 :         case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
    3326                 :     838986 :                 rc =  bdev_nvme_write_zeroes(nbdev_io,
    3327   [ #  #  #  #  :      66999 :                                              bdev_io->u.bdev.offset_blocks,
             #  #  #  # ]
    3328   [ #  #  #  #  :      66999 :                                              bdev_io->u.bdev.num_blocks);
             #  #  #  # ]
    3329                 :     771987 :                 break;
    3330                 :         77 :         case SPDK_BDEV_IO_TYPE_RESET:
    3331   [ #  #  #  # ]:         93 :                 nbdev_io->io_path = NULL;
    3332   [ #  #  #  # ]:         93 :                 bdev_nvme_reset_io(bdev->ctxt, nbdev_io);
    3333                 :         93 :                 return;
    3334                 :            : 
    3335                 :     168616 :         case SPDK_BDEV_IO_TYPE_FLUSH:
    3336                 :     168617 :                 bdev_nvme_io_complete(nbdev_io, 0);
    3337                 :     168617 :                 return;
    3338                 :            : 
    3339                 :     235234 :         case SPDK_BDEV_IO_TYPE_ZONE_APPEND:
    3340                 :     235234 :                 rc = bdev_nvme_zone_appendv(nbdev_io,
    3341   [ #  #  #  #  :          0 :                                             bdev_io->u.bdev.iovs,
             #  #  #  # ]
    3342   [ #  #  #  #  :          0 :                                             bdev_io->u.bdev.iovcnt,
             #  #  #  # ]
    3343   [ #  #  #  #  :          0 :                                             bdev_io->u.bdev.md_buf,
             #  #  #  # ]
    3344   [ #  #  #  #  :          0 :                                             bdev_io->u.bdev.num_blocks,
             #  #  #  # ]
    3345   [ #  #  #  #  :          0 :                                             bdev_io->u.bdev.offset_blocks,
             #  #  #  # ]
    3346   [ #  #  #  #  :          0 :                                             bdev_io->u.bdev.dif_check_flags);
             #  #  #  # ]
    3347                 :     235234 :                 break;
    3348                 :          1 :         case SPDK_BDEV_IO_TYPE_GET_ZONE_INFO:
    3349                 :          1 :                 rc = bdev_nvme_get_zone_info(nbdev_io,
    3350   [ #  #  #  #  :          0 :                                              bdev_io->u.zone_mgmt.zone_id,
             #  #  #  # ]
    3351   [ #  #  #  #  :          0 :                                              bdev_io->u.zone_mgmt.num_zones,
             #  #  #  # ]
    3352   [ #  #  #  #  :          1 :                                              bdev_io->u.zone_mgmt.buf);
             #  #  #  # ]
    3353                 :          1 :                 break;
    3354                 :         43 :         case SPDK_BDEV_IO_TYPE_ZONE_MANAGEMENT:
    3355                 :         43 :                 rc = bdev_nvme_zone_management(nbdev_io,
    3356   [ #  #  #  #  :          0 :                                                bdev_io->u.zone_mgmt.zone_id,
             #  #  #  # ]
    3357   [ #  #  #  #  :          0 :                                                bdev_io->u.zone_mgmt.zone_action);
             #  #  #  # ]
    3358                 :         43 :                 break;
    3359                 :         64 :         case SPDK_BDEV_IO_TYPE_NVME_ADMIN:
    3360   [ #  #  #  # ]:         70 :                 nbdev_io->io_path = NULL;
    3361                 :         76 :                 bdev_nvme_admin_passthru(nbdev_ch,
    3362                 :          6 :                                          nbdev_io,
    3363   [ #  #  #  #  :          6 :                                          &bdev_io->u.nvme_passthru.cmd,
                   #  # ]
    3364   [ #  #  #  #  :          6 :                                          bdev_io->u.nvme_passthru.buf,
             #  #  #  # ]
    3365   [ #  #  #  #  :          6 :                                          bdev_io->u.nvme_passthru.nbytes);
             #  #  #  # ]
    3366                 :         70 :                 return;
    3367                 :            : 
    3368                 :         66 :         case SPDK_BDEV_IO_TYPE_NVME_IO:
    3369                 :         72 :                 rc = bdev_nvme_io_passthru(nbdev_io,
    3370   [ #  #  #  #  :          3 :                                            &bdev_io->u.nvme_passthru.cmd,
                   #  # ]
    3371   [ #  #  #  #  :          3 :                                            bdev_io->u.nvme_passthru.buf,
             #  #  #  # ]
    3372   [ #  #  #  #  :          3 :                                            bdev_io->u.nvme_passthru.nbytes);
             #  #  #  # ]
    3373                 :         69 :                 break;
    3374                 :          0 :         case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
    3375                 :          0 :                 rc = bdev_nvme_io_passthru_md(nbdev_io,
    3376   [ #  #  #  #  :          0 :                                               &bdev_io->u.nvme_passthru.cmd,
                   #  # ]
    3377   [ #  #  #  #  :          0 :                                               bdev_io->u.nvme_passthru.buf,
             #  #  #  # ]
    3378   [ #  #  #  #  :          0 :                                               bdev_io->u.nvme_passthru.nbytes,
             #  #  #  # ]
    3379   [ #  #  #  #  :          0 :                                               bdev_io->u.nvme_passthru.md_buf,
             #  #  #  # ]
    3380   [ #  #  #  #  :          0 :                                               bdev_io->u.nvme_passthru.md_len);
             #  #  #  # ]
    3381                 :          0 :                 break;
    3382                 :          0 :         case SPDK_BDEV_IO_TYPE_NVME_IOV_MD:
    3383                 :          0 :                 rc = bdev_nvme_iov_passthru_md(nbdev_io,
    3384   [ #  #  #  #  :          0 :                                                &bdev_io->u.nvme_passthru.cmd,
                   #  # ]
    3385   [ #  #  #  #  :          0 :                                                bdev_io->u.nvme_passthru.iovs,
             #  #  #  # ]
    3386   [ #  #  #  #  :          0 :                                                bdev_io->u.nvme_passthru.iovcnt,
             #  #  #  # ]
    3387   [ #  #  #  #  :          0 :                                                bdev_io->u.nvme_passthru.nbytes,
             #  #  #  # ]
    3388   [ #  #  #  #  :          0 :                                                bdev_io->u.nvme_passthru.md_buf,
             #  #  #  # ]
    3389   [ #  #  #  #  :          0 :                                                bdev_io->u.nvme_passthru.md_len);
             #  #  #  # ]
    3390                 :          0 :                 break;
    3391                 :       2547 :         case SPDK_BDEV_IO_TYPE_ABORT:
    3392   [ #  #  #  # ]:       2553 :                 nbdev_io->io_path = NULL;
    3393   [ #  #  #  #  :       2553 :                 nbdev_io_to_abort = (struct nvme_bdev_io *)bdev_io->u.abort.bio_to_abort->driver_ctx;
          #  #  #  #  #  
                      # ]
    3394                 :       2559 :                 bdev_nvme_abort(nbdev_ch,
    3395                 :          6 :                                 nbdev_io,
    3396                 :          6 :                                 nbdev_io_to_abort);
    3397                 :       2553 :                 return;
    3398                 :            : 
    3399                 :         29 :         case SPDK_BDEV_IO_TYPE_COPY:
    3400                 :         31 :                 rc = bdev_nvme_copy(nbdev_io,
    3401   [ #  #  #  #  :          1 :                                     bdev_io->u.bdev.offset_blocks,
             #  #  #  # ]
    3402   [ #  #  #  #  :          1 :                                     bdev_io->u.bdev.copy.src_offset_blocks,
          #  #  #  #  #  
                      # ]
    3403   [ #  #  #  #  :          1 :                                     bdev_io->u.bdev.num_blocks);
             #  #  #  # ]
    3404                 :         30 :                 break;
    3405                 :          0 :         default:
    3406                 :          0 :                 rc = -EINVAL;
    3407                 :          0 :                 break;
    3408                 :            :         }
    3409                 :            : 
    3410         [ +  + ]:   16491828 :         if (spdk_unlikely(rc != 0)) {
    3411                 :       9471 :                 bdev_nvme_io_complete(nbdev_io, rc);
    3412                 :          0 :         }
    3413                 :     680970 : }
    3414                 :            : 
    3415                 :            : static void
    3416                 :   16709290 : bdev_nvme_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
    3417                 :            : {
    3418                 :   16709290 :         struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(ch);
    3419         [ +  - ]:   16709290 :         struct nvme_bdev_io *nbdev_io = (struct nvme_bdev_io *)bdev_io->driver_ctx;
    3420                 :            : 
    3421   [ +  +  +  -  :   16709290 :         if (spdk_likely(nbdev_io->submit_tsc == 0)) {
                   -  + ]
    3422   [ +  -  +  - ]:   16687986 :                 nbdev_io->submit_tsc = spdk_bdev_io_get_submit_tsc(bdev_io);
    3423                 :     678826 :         } else {
    3424                 :            :                 /* There are cases where submit_tsc != 0, i.e. retry I/O.
    3425                 :            :                  * We need to update submit_tsc here.
    3426                 :            :                  */
    3427   [ #  #  #  # ]:      21304 :                 nbdev_io->submit_tsc = spdk_get_ticks();
    3428                 :            :         }
    3429                 :            : 
    3430   [ +  +  +  +  :   16709290 :         spdk_trace_record(TRACE_BDEV_NVME_IO_START, 0, 0, (uintptr_t)nbdev_io, (uintptr_t)bdev_io);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    3431   [ +  -  +  - ]:   16709290 :         nbdev_io->io_path = bdev_nvme_find_io_path(nbdev_ch);
    3432   [ +  +  +  -  :   16709290 :         if (spdk_unlikely(!nbdev_io->io_path)) {
                   +  - ]
    3433   [ +  +  #  #  :      49495 :                 if (!bdev_nvme_io_type_is_admin(bdev_io->type)) {
                   #  # ]
    3434                 :      49491 :                         bdev_nvme_io_complete(nbdev_io, -ENXIO);
    3435                 :      49491 :                         return;
    3436                 :            :                 }
    3437                 :            : 
    3438                 :            :                 /* Admin commands do not use the optimal I/O path.
    3439                 :            :                  * Simply fall through even if it is not found.
    3440                 :            :                  */
    3441                 :          1 :         }
    3442                 :            : 
    3443                 :   16659799 :         _bdev_nvme_submit_request(nbdev_ch, bdev_io);
    3444                 :     678828 : }
    3445                 :            : 
    3446                 :            : static bool
    3447                 :    2682871 : bdev_nvme_is_supported_csi(enum spdk_nvme_csi csi)
    3448                 :            : {
    3449      [ +  +  + ]:    2682871 :         switch (csi) {
    3450                 :    2548678 :         case SPDK_NVME_CSI_NVM:
    3451                 :    2682842 :                 return true;
    3452                 :         29 :         case SPDK_NVME_CSI_ZNS:
    3453                 :         29 :                 return true;
    3454                 :          0 :         default:
    3455                 :          0 :                 return false;
    3456                 :            :         }
    3457                 :     134164 : }
    3458                 :            : 
    3459                 :            : static bool
    3460                 :    2682871 : bdev_nvme_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
    3461                 :            : {
    3462                 :    2682871 :         struct nvme_bdev *nbdev = ctx;
    3463                 :            :         struct nvme_ns *nvme_ns;
    3464                 :            :         struct spdk_nvme_ns *ns;
    3465                 :            :         struct spdk_nvme_ctrlr *ctrlr;
    3466                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    3467                 :            : 
    3468   [ +  -  +  -  :    2682871 :         nvme_ns = TAILQ_FIRST(&nbdev->nvme_ns_list);
                   +  - ]
    3469   [ +  +  #  # ]:    2682871 :         assert(nvme_ns != NULL);
    3470   [ +  -  +  - ]:    2682871 :         ns = nvme_ns->ns;
    3471         [ +  + ]:    2682871 :         if (ns == NULL) {
    3472                 :          0 :                 return false;
    3473                 :            :         }
    3474                 :            : 
    3475         [ +  + ]:    2682871 :         if (!bdev_nvme_is_supported_csi(spdk_nvme_ns_get_csi(ns))) {
    3476      [ #  #  # ]:          0 :                 switch (io_type) {
    3477                 :          0 :                 case SPDK_BDEV_IO_TYPE_NVME_ADMIN:
    3478                 :            :                 case SPDK_BDEV_IO_TYPE_NVME_IO:
    3479                 :          0 :                         return true;
    3480                 :            : 
    3481                 :          0 :                 case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
    3482                 :          0 :                         return spdk_nvme_ns_get_md_size(ns) ? true : false;
    3483                 :            : 
    3484                 :          0 :                 default:
    3485                 :          0 :                         return false;
    3486                 :            :                 }
    3487                 :            :         }
    3488                 :            : 
    3489                 :    2682871 :         ctrlr = spdk_nvme_ns_get_ctrlr(ns);
    3490                 :            : 
    3491   [ +  +  +  +  :    2682871 :         switch (io_type) {
          +  +  +  +  +  
                      + ]
    3492                 :     200067 :         case SPDK_BDEV_IO_TYPE_READ:
    3493                 :            :         case SPDK_BDEV_IO_TYPE_WRITE:
    3494                 :            :         case SPDK_BDEV_IO_TYPE_RESET:
    3495                 :            :         case SPDK_BDEV_IO_TYPE_FLUSH:
    3496                 :            :         case SPDK_BDEV_IO_TYPE_NVME_ADMIN:
    3497                 :            :         case SPDK_BDEV_IO_TYPE_NVME_IO:
    3498                 :            :         case SPDK_BDEV_IO_TYPE_ABORT:
    3499                 :     200114 :                 return true;
    3500                 :            : 
    3501                 :       3702 :         case SPDK_BDEV_IO_TYPE_COMPARE:
    3502                 :       3708 :                 return spdk_nvme_ns_supports_compare(ns);
    3503                 :            : 
    3504                 :       1010 :         case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
    3505                 :       1012 :                 return spdk_nvme_ns_get_md_size(ns) ? true : false;
    3506                 :            : 
    3507                 :      11796 :         case SPDK_BDEV_IO_TYPE_UNMAP:
    3508                 :      11802 :                 cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    3509   [ #  #  #  # ]:      11802 :                 return cdata->oncs.dsm;
    3510                 :            : 
    3511                 :    2297485 :         case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
    3512                 :    2431518 :                 cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    3513   [ +  -  +  - ]:    2431518 :                 return cdata->oncs.write_zeroes;
    3514                 :            : 
    3515                 :       3686 :         case SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE:
    3516         [ +  + ]:       3692 :                 if (spdk_nvme_ctrlr_get_flags(ctrlr) &
    3517                 :            :                     SPDK_NVME_CTRLR_COMPARE_AND_WRITE_SUPPORTED) {
    3518                 :        131 :                         return true;
    3519                 :            :                 }
    3520                 :       3561 :                 return false;
    3521                 :            : 
    3522                 :       7252 :         case SPDK_BDEV_IO_TYPE_GET_ZONE_INFO:
    3523                 :            :         case SPDK_BDEV_IO_TYPE_ZONE_MANAGEMENT:
    3524                 :       7260 :                 return spdk_nvme_ns_get_csi(ns) == SPDK_NVME_CSI_ZNS;
    3525                 :            : 
    3526                 :       3627 :         case SPDK_BDEV_IO_TYPE_ZONE_APPEND:
    3527         [ +  + ]:       3633 :                 return spdk_nvme_ns_get_csi(ns) == SPDK_NVME_CSI_ZNS &&
    3528         [ +  - ]:          2 :                        spdk_nvme_ctrlr_get_flags(ctrlr) & SPDK_NVME_CTRLR_ZONE_APPEND_SUPPORTED;
    3529                 :            : 
    3530                 :       5560 :         case SPDK_BDEV_IO_TYPE_COPY:
    3531                 :       5596 :                 cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    3532   [ +  -  +  - ]:       5596 :                 return cdata->oncs.copy;
    3533                 :            : 
    3534                 :      14522 :         default:
    3535                 :      14538 :                 return false;
    3536                 :            :         }
    3537                 :     134164 : }
    3538                 :            : 
    3539                 :            : static int
    3540                 :       1588 : nvme_qpair_create(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ctrlr_channel *ctrlr_ch)
    3541                 :            : {
    3542                 :            :         struct nvme_qpair *nvme_qpair;
    3543                 :            :         struct spdk_io_channel *pg_ch;
    3544                 :            :         int rc;
    3545                 :            : 
    3546                 :       1588 :         nvme_qpair = calloc(1, sizeof(*nvme_qpair));
    3547         [ +  + ]:       1588 :         if (!nvme_qpair) {
    3548   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to alloc nvme_qpair.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    3549                 :          0 :                 return -1;
    3550                 :            :         }
    3551                 :            : 
    3552   [ +  -  +  -  :       1588 :         TAILQ_INIT(&nvme_qpair->io_path_list);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    3553                 :            : 
    3554   [ +  -  +  - ]:       1588 :         nvme_qpair->ctrlr = nvme_ctrlr;
    3555   [ +  -  +  - ]:       1588 :         nvme_qpair->ctrlr_ch = ctrlr_ch;
    3556                 :            : 
    3557                 :       1588 :         pg_ch = spdk_get_io_channel(&g_nvme_bdev_ctrlrs);
    3558         [ +  + ]:       1588 :         if (!pg_ch) {
    3559                 :          0 :                 free(nvme_qpair);
    3560                 :          0 :                 return -1;
    3561                 :            :         }
    3562                 :            : 
    3563   [ +  -  +  - ]:       1588 :         nvme_qpair->group = spdk_io_channel_get_ctx(pg_ch);
    3564                 :            : 
    3565                 :            : #ifdef SPDK_CONFIG_VTUNE
    3566                 :            :         nvme_qpair->group->collect_spin_stat = true;
    3567                 :            : #else
    3568   [ +  -  +  -  :       1588 :         nvme_qpair->group->collect_spin_stat = false;
             +  -  +  - ]
    3569                 :            : #endif
    3570                 :            : 
    3571   [ +  +  -  + ]:       1588 :         if (!nvme_ctrlr->disabled) {
    3572                 :            :                 /* If a nvme_ctrlr is disabled, don't try to create qpair for it. Qpair will
    3573                 :            :                  * be created when it's enabled.
    3574                 :            :                  */
    3575                 :       1588 :                 rc = bdev_nvme_create_qpair(nvme_qpair);
    3576         [ +  + ]:       1588 :                 if (rc != 0) {
    3577                 :            :                         /* nvme_ctrlr can't create IO qpair if connection is down.
    3578                 :            :                          * If reconnect_delay_sec is non-zero, creating IO qpair is retried
    3579                 :            :                          * after reconnect_delay_sec seconds. If bdev_retry_count is non-zero,
    3580                 :            :                          * submitted IO will be queued until IO qpair is successfully created.
    3581                 :            :                          *
    3582                 :            :                          * Hence, if both are satisfied, ignore the failure.
    3583                 :            :                          */
    3584   [ #  #  #  #  :          0 :                         if (nvme_ctrlr->opts.reconnect_delay_sec == 0 || g_opts.bdev_retry_count == 0) {
          #  #  #  #  #  
                #  #  # ]
    3585                 :          0 :                                 spdk_put_io_channel(pg_ch);
    3586                 :          0 :                                 free(nvme_qpair);
    3587                 :          0 :                                 return rc;
    3588                 :            :                         }
    3589                 :          0 :                 }
    3590                 :         82 :         }
    3591                 :            : 
    3592   [ +  -  +  -  :       1588 :         TAILQ_INSERT_TAIL(&nvme_qpair->group->qpair_list, nvme_qpair, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
    3593                 :            : 
    3594   [ +  -  +  - ]:       1588 :         ctrlr_ch->qpair = nvme_qpair;
    3595                 :            : 
    3596                 :       1588 :         nvme_ctrlr_get_ref(nvme_ctrlr);
    3597                 :            : 
    3598                 :       1588 :         return 0;
    3599                 :         82 : }
    3600                 :            : 
    3601                 :            : static int
    3602                 :       1588 : bdev_nvme_create_ctrlr_channel_cb(void *io_device, void *ctx_buf)
    3603                 :            : {
    3604                 :       1588 :         struct nvme_ctrlr *nvme_ctrlr = io_device;
    3605                 :       1588 :         struct nvme_ctrlr_channel *ctrlr_ch = ctx_buf;
    3606                 :            : 
    3607                 :       1588 :         return nvme_qpair_create(nvme_ctrlr, ctrlr_ch);
    3608                 :            : }
    3609                 :            : 
    3610                 :            : static void
    3611                 :       1588 : nvme_qpair_delete(struct nvme_qpair *nvme_qpair)
    3612                 :            : {
    3613                 :            :         struct nvme_io_path *io_path, *next;
    3614                 :            : 
    3615   [ +  +  +  -  :       1588 :         assert(nvme_qpair->group != NULL);
             +  -  #  # ]
    3616                 :            : 
    3617   [ +  +  +  +  :       3194 :         TAILQ_FOREACH_SAFE(io_path, &nvme_qpair->io_path_list, tailq, next) {
          +  -  +  +  +  
          -  +  -  +  -  
                   +  + ]
    3618   [ +  +  +  -  :       1606 :                 TAILQ_REMOVE(&nvme_qpair->io_path_list, io_path, tailq);
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
    3619                 :       1606 :                 nvme_io_path_free(io_path);
    3620                 :         60 :         }
    3621                 :            : 
    3622   [ +  +  +  -  :       1588 :         TAILQ_REMOVE(&nvme_qpair->group->qpair_list, nvme_qpair, tailq);
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    3623                 :            : 
    3624   [ +  -  +  - ]:       1588 :         spdk_put_io_channel(spdk_io_channel_from_ctx(nvme_qpair->group));
    3625                 :            : 
    3626   [ +  -  +  - ]:       1588 :         nvme_ctrlr_put_ref(nvme_qpair->ctrlr);
    3627                 :            : 
    3628                 :       1588 :         free(nvme_qpair);
    3629                 :       1588 : }
    3630                 :            : 
    3631                 :            : static void
    3632                 :       1588 : bdev_nvme_destroy_ctrlr_channel_cb(void *io_device, void *ctx_buf)
    3633                 :            : {
    3634                 :       1588 :         struct nvme_ctrlr_channel *ctrlr_ch = ctx_buf;
    3635                 :            :         struct nvme_qpair *nvme_qpair;
    3636                 :            : 
    3637   [ +  -  +  - ]:       1588 :         nvme_qpair = ctrlr_ch->qpair;
    3638   [ +  +  #  # ]:       1588 :         assert(nvme_qpair != NULL);
    3639                 :            : 
    3640                 :       1588 :         _bdev_nvme_clear_io_path_cache(nvme_qpair);
    3641                 :            : 
    3642   [ +  +  +  -  :       1588 :         if (nvme_qpair->qpair != NULL) {
                   +  - ]
    3643   [ +  +  +  -  :       1529 :                 if (ctrlr_ch->reset_iter == NULL) {
                   -  + ]
    3644   [ +  -  +  - ]:       1529 :                         spdk_nvme_ctrlr_disconnect_io_qpair(nvme_qpair->qpair);
    3645                 :         68 :                 } else {
    3646                 :            :                         /* Skip current ctrlr_channel in a full reset sequence because
    3647                 :            :                          * it is being deleted now. The qpair is already being disconnected.
    3648                 :            :                          * We do not have to restart disconnecting it.
    3649                 :            :                          */
    3650   [ #  #  #  # ]:          0 :                         nvme_ctrlr_for_each_channel_continue(ctrlr_ch->reset_iter, 0);
    3651                 :            :                 }
    3652                 :            : 
    3653                 :            :                 /* We cannot release a reference to the poll group now.
    3654                 :            :                  * The qpair may be disconnected asynchronously later.
    3655                 :            :                  * We need to poll it until it is actually disconnected.
    3656                 :            :                  * Just detach the qpair from the deleting ctrlr_channel.
    3657                 :            :                  */
    3658   [ +  -  +  - ]:       1529 :                 nvme_qpair->ctrlr_ch = NULL;
    3659                 :         68 :         } else {
    3660   [ -  +  #  #  :         59 :                 assert(ctrlr_ch->reset_iter == NULL);
             #  #  #  # ]
    3661                 :            : 
    3662                 :         59 :                 nvme_qpair_delete(nvme_qpair);
    3663                 :            :         }
    3664                 :       1588 : }
    3665                 :            : 
    3666                 :            : static inline struct spdk_io_channel *
    3667                 :     304706 : bdev_nvme_get_accel_channel(struct nvme_poll_group *group)
    3668                 :            : {
    3669   [ +  +  #  #  :     304706 :         if (spdk_unlikely(!group->accel_channel)) {
                   #  # ]
    3670   [ #  #  #  # ]:         19 :                 group->accel_channel = spdk_accel_get_io_channel();
    3671   [ -  +  #  #  :         19 :                 if (!group->accel_channel) {
                   #  # ]
    3672                 :          0 :                         SPDK_ERRLOG("Cannot get the accel_channel for bdev nvme polling group=%p\n",
    3673                 :            :                                     group);
    3674                 :          0 :                         return NULL;
    3675                 :            :                 }
    3676                 :          0 :         }
    3677                 :            : 
    3678   [ #  #  #  # ]:     304706 :         return group->accel_channel;
    3679                 :          0 : }
    3680                 :            : 
    3681                 :            : static void
    3682                 :     304706 : bdev_nvme_finish_sequence(void *seq, spdk_nvme_accel_completion_cb cb_fn, void *cb_arg)
    3683                 :            : {
    3684                 :     304706 :         spdk_accel_sequence_finish(seq, cb_fn, cb_arg);
    3685                 :     304706 : }
    3686                 :            : 
    3687                 :            : static void
    3688                 :          0 : bdev_nvme_abort_sequence(void *seq)
    3689                 :            : {
    3690                 :          0 :         spdk_accel_sequence_abort(seq);
    3691                 :          0 : }
    3692                 :            : 
    3693                 :            : static void
    3694                 :     152571 : bdev_nvme_reverse_sequence(void *seq)
    3695                 :            : {
    3696                 :     152571 :         spdk_accel_sequence_reverse(seq);
    3697                 :     152571 : }
    3698                 :            : 
    3699                 :            : static int
    3700                 :     304706 : bdev_nvme_append_crc32c(void *ctx, void **seq, uint32_t *dst, struct iovec *iovs, uint32_t iovcnt,
    3701                 :            :                         struct spdk_memory_domain *domain, void *domain_ctx, uint32_t seed,
    3702                 :            :                         spdk_nvme_accel_step_cb cb_fn, void *cb_arg)
    3703                 :            : {
    3704                 :            :         struct spdk_io_channel *ch;
    3705                 :     304706 :         struct nvme_poll_group *group = ctx;
    3706                 :            : 
    3707                 :     304706 :         ch = bdev_nvme_get_accel_channel(group);
    3708         [ -  + ]:     304706 :         if (spdk_unlikely(ch == NULL)) {
    3709                 :          0 :                 return -ENOMEM;
    3710                 :            :         }
    3711                 :            : 
    3712                 :     304706 :         return spdk_accel_append_crc32c((struct spdk_accel_sequence **)seq, ch, dst, iovs, iovcnt,
    3713                 :          0 :                                         domain, domain_ctx, seed, cb_fn, cb_arg);
    3714                 :          0 : }
    3715                 :            : 
    3716                 :            : static int
    3717                 :          0 : bdev_nvme_append_copy(void *ctx, void **seq, struct iovec *dst_iovs, uint32_t dst_iovcnt,
    3718                 :            :                       struct spdk_memory_domain *dst_domain, void *dst_domain_ctx,
    3719                 :            :                       struct iovec *src_iovs, uint32_t src_iovcnt,
    3720                 :            :                       struct spdk_memory_domain *src_domain, void *src_domain_ctx,
    3721                 :            :                       spdk_nvme_accel_step_cb cb_fn, void *cb_arg)
    3722                 :            : {
    3723                 :            :         struct spdk_io_channel *ch;
    3724                 :          0 :         struct nvme_poll_group *group = ctx;
    3725                 :            : 
    3726                 :          0 :         ch = bdev_nvme_get_accel_channel(group);
    3727         [ #  # ]:          0 :         if (spdk_unlikely(ch == NULL)) {
    3728                 :          0 :                 return -ENOMEM;
    3729                 :            :         }
    3730                 :            : 
    3731                 :          0 :         return spdk_accel_append_copy((struct spdk_accel_sequence **)seq, ch,
    3732                 :          0 :                                       dst_iovs, dst_iovcnt, dst_domain, dst_domain_ctx,
    3733                 :          0 :                                       src_iovs, src_iovcnt, src_domain, src_domain_ctx,
    3734                 :          0 :                                       cb_fn, cb_arg);
    3735                 :          0 : }
    3736                 :            : 
    3737                 :            : static struct spdk_nvme_accel_fn_table g_bdev_nvme_accel_fn_table = {
    3738                 :            :         .table_size             = sizeof(struct spdk_nvme_accel_fn_table),
    3739                 :            :         .append_crc32c          = bdev_nvme_append_crc32c,
    3740                 :            :         .append_copy            = bdev_nvme_append_copy,
    3741                 :            :         .finish_sequence        = bdev_nvme_finish_sequence,
    3742                 :            :         .reverse_sequence       = bdev_nvme_reverse_sequence,
    3743                 :            :         .abort_sequence         = bdev_nvme_abort_sequence,
    3744                 :            : };
    3745                 :            : 
    3746                 :            : static int
    3747                 :          0 : bdev_nvme_interrupt_wrapper(void *ctx)
    3748                 :            : {
    3749                 :            :         int num_events;
    3750                 :          0 :         struct nvme_poll_group *group = ctx;
    3751                 :            : 
    3752   [ #  #  #  # ]:          0 :         num_events = spdk_nvme_poll_group_wait(group->group, bdev_nvme_disconnected_qpair_cb);
    3753         [ #  # ]:          0 :         if (spdk_unlikely(num_events < 0)) {
    3754                 :          0 :                 bdev_nvme_check_io_qpairs(group);
    3755                 :          0 :         }
    3756                 :            : 
    3757                 :          0 :         return num_events;
    3758                 :            : }
    3759                 :            : 
    3760                 :            : static int
    3761                 :       1436 : bdev_nvme_create_poll_group_cb(void *io_device, void *ctx_buf)
    3762                 :            : {
    3763                 :       1436 :         struct nvme_poll_group *group = ctx_buf;
    3764                 :            :         uint64_t period;
    3765                 :            :         int fd;
    3766                 :            : 
    3767   [ +  -  +  -  :       1436 :         TAILQ_INIT(&group->qpair_list);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    3768                 :            : 
    3769   [ +  -  +  - ]:       1436 :         group->group = spdk_nvme_poll_group_create(group, &g_bdev_nvme_accel_fn_table);
    3770   [ +  +  +  -  :       1436 :         if (group->group == NULL) {
                   +  - ]
    3771                 :          0 :                 return -1;
    3772                 :            :         }
    3773                 :            : 
    3774   [ +  +  +  - ]:       1436 :         period = spdk_interrupt_mode_is_enabled() ? 0 : g_opts.nvme_ioq_poll_period_us;
    3775   [ +  -  +  - ]:       1436 :         group->poller = SPDK_POLLER_REGISTER(bdev_nvme_poll, group, period);
    3776                 :            : 
    3777   [ +  +  +  -  :       1436 :         if (group->poller == NULL) {
                   +  - ]
    3778   [ #  #  #  # ]:          0 :                 spdk_nvme_poll_group_destroy(group->group);
    3779                 :          0 :                 return -1;
    3780                 :            :         }
    3781                 :            : 
    3782         [ +  + ]:       1436 :         if (spdk_interrupt_mode_is_enabled()) {
    3783   [ #  #  #  # ]:          0 :                 spdk_poller_register_interrupt(group->poller, NULL, NULL);
    3784                 :            : 
    3785   [ #  #  #  # ]:          0 :                 fd = spdk_nvme_poll_group_get_fd(group->group);
    3786         [ #  # ]:          0 :                 if (fd < 0) {
    3787   [ #  #  #  # ]:          0 :                         spdk_nvme_poll_group_destroy(group->group);
    3788                 :          0 :                         return -1;
    3789                 :            :                 }
    3790                 :            : 
    3791   [ #  #  #  # ]:          0 :                 group->intr = SPDK_INTERRUPT_REGISTER(fd, bdev_nvme_interrupt_wrapper, group);
    3792   [ #  #  #  #  :          0 :                 if (!group->intr) {
                   #  # ]
    3793   [ #  #  #  # ]:          0 :                         spdk_nvme_poll_group_destroy(group->group);
    3794                 :          0 :                         return -1;
    3795                 :            :                 }
    3796                 :          0 :         }
    3797                 :            : 
    3798                 :       1436 :         return 0;
    3799                 :         67 : }
    3800                 :            : 
    3801                 :            : static void
    3802                 :       1436 : bdev_nvme_destroy_poll_group_cb(void *io_device, void *ctx_buf)
    3803                 :            : {
    3804                 :       1436 :         struct nvme_poll_group *group = ctx_buf;
    3805                 :            : 
    3806   [ +  +  +  -  :       1436 :         assert(TAILQ_EMPTY(&group->qpair_list));
          +  -  +  -  #  
                      # ]
    3807                 :            : 
    3808   [ +  +  +  -  :       1436 :         if (group->accel_channel) {
                   +  - ]
    3809   [ #  #  #  # ]:         19 :                 spdk_put_io_channel(group->accel_channel);
    3810                 :          0 :         }
    3811                 :            : 
    3812         [ +  + ]:       1436 :         if (spdk_interrupt_mode_is_enabled()) {
    3813         [ #  # ]:          0 :                 spdk_interrupt_unregister(&group->intr);
    3814                 :          0 :         }
    3815                 :            : 
    3816         [ +  - ]:       1436 :         spdk_poller_unregister(&group->poller);
    3817   [ +  +  +  -  :       1436 :         if (spdk_nvme_poll_group_destroy(group->group)) {
                   +  - ]
    3818                 :          0 :                 SPDK_ERRLOG("Unable to destroy a poll group for the NVMe bdev module.\n");
    3819         [ #  # ]:          0 :                 assert(false);
    3820                 :            :         }
    3821                 :       1436 : }
    3822                 :            : 
    3823                 :            : static struct spdk_io_channel *
    3824                 :       1458 : bdev_nvme_get_io_channel(void *ctx)
    3825                 :            : {
    3826                 :       1458 :         struct nvme_bdev *nvme_bdev = ctx;
    3827                 :            : 
    3828                 :       1458 :         return spdk_get_io_channel(nvme_bdev);
    3829                 :            : }
    3830                 :            : 
    3831                 :            : static void *
    3832                 :          0 : bdev_nvme_get_module_ctx(void *ctx)
    3833                 :            : {
    3834                 :          0 :         struct nvme_bdev *nvme_bdev = ctx;
    3835                 :            :         struct nvme_ns *nvme_ns;
    3836                 :            : 
    3837   [ #  #  #  #  :          0 :         if (!nvme_bdev || nvme_bdev->disk.module != &nvme_if) {
          #  #  #  #  #  
                      # ]
    3838                 :          0 :                 return NULL;
    3839                 :            :         }
    3840                 :            : 
    3841   [ #  #  #  #  :          0 :         nvme_ns = TAILQ_FIRST(&nvme_bdev->nvme_ns_list);
                   #  # ]
    3842         [ #  # ]:          0 :         if (!nvme_ns) {
    3843                 :          0 :                 return NULL;
    3844                 :            :         }
    3845                 :            : 
    3846   [ #  #  #  # ]:          0 :         return nvme_ns->ns;
    3847                 :          0 : }
    3848                 :            : 
    3849                 :            : static const char *
    3850                 :          0 : _nvme_ana_state_str(enum spdk_nvme_ana_state ana_state)
    3851                 :            : {
    3852   [ #  #  #  #  :          0 :         switch (ana_state) {
                   #  # ]
    3853                 :          0 :         case SPDK_NVME_ANA_OPTIMIZED_STATE:
    3854                 :          0 :                 return "optimized";
    3855                 :          0 :         case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
    3856                 :          0 :                 return "non_optimized";
    3857                 :          0 :         case SPDK_NVME_ANA_INACCESSIBLE_STATE:
    3858                 :          0 :                 return "inaccessible";
    3859                 :          0 :         case SPDK_NVME_ANA_PERSISTENT_LOSS_STATE:
    3860                 :          0 :                 return "persistent_loss";
    3861                 :          0 :         case SPDK_NVME_ANA_CHANGE_STATE:
    3862                 :          0 :                 return "change";
    3863                 :          0 :         default:
    3864                 :          0 :                 return NULL;
    3865                 :            :         }
    3866                 :          0 : }
    3867                 :            : 
    3868                 :            : static int
    3869                 :       7317 : bdev_nvme_get_memory_domains(void *ctx, struct spdk_memory_domain **domains, int array_size)
    3870                 :            : {
    3871                 :       7317 :         struct spdk_memory_domain **_domains = NULL;
    3872                 :       7317 :         struct nvme_bdev *nbdev = ctx;
    3873                 :            :         struct nvme_ns *nvme_ns;
    3874                 :       7317 :         int i = 0, _array_size = array_size;
    3875                 :       7317 :         int rc = 0;
    3876                 :            : 
    3877   [ +  +  +  -  :      14671 :         TAILQ_FOREACH(nvme_ns, &nbdev->nvme_ns_list, tailq) {
          +  -  +  +  +  
             -  +  -  +  
                      - ]
    3878   [ +  +  +  + ]:       7354 :                 if (domains && array_size >= i) {
    3879         [ #  # ]:        173 :                         _domains = &domains[i];
    3880                 :         11 :                 } else {
    3881                 :       7181 :                         _domains = NULL;
    3882                 :            :                 }
    3883   [ +  -  +  -  :       7354 :                 rc = spdk_nvme_ctrlr_get_memory_domains(nvme_ns->ctrlr->ctrlr, _domains, _array_size);
             +  -  +  - ]
    3884         [ +  + ]:       7354 :                 if (rc > 0) {
    3885         [ #  # ]:       1345 :                         i += rc;
    3886         [ +  + ]:       1345 :                         if (_array_size >= rc) {
    3887         [ #  # ]:        165 :                                 _array_size -= rc;
    3888                 :          9 :                         } else {
    3889                 :       1180 :                                 _array_size = 0;
    3890                 :            :                         }
    3891         [ +  + ]:       6022 :                 } else if (rc < 0) {
    3892                 :          0 :                         return rc;
    3893                 :            :                 }
    3894                 :         77 :         }
    3895                 :            : 
    3896                 :       7317 :         return i;
    3897                 :         71 : }
    3898                 :            : 
    3899                 :            : static const char *
    3900                 :        195 : nvme_ctrlr_get_state_str(struct nvme_ctrlr *nvme_ctrlr)
    3901                 :            : {
    3902   [ -  +  #  # ]:        195 :         if (nvme_ctrlr->destruct) {
    3903                 :          0 :                 return "deleting";
    3904   [ -  +  #  #  :        195 :         } else if (spdk_nvme_ctrlr_is_failed(nvme_ctrlr->ctrlr)) {
                   #  # ]
    3905                 :          0 :                 return "failed";
    3906   [ +  +  #  # ]:        195 :         } else if (nvme_ctrlr->resetting) {
    3907                 :          4 :                 return "resetting";
    3908   [ +  +  #  # ]:        191 :         } else if (nvme_ctrlr->reconnect_is_delayed > 0) {
    3909                 :          1 :                 return "reconnect_is_delayed";
    3910   [ +  +  #  # ]:        190 :         } else if (nvme_ctrlr->disabled) {
    3911                 :          0 :                 return "disabled";
    3912                 :            :         } else {
    3913                 :        190 :                 return "enabled";
    3914                 :            :         }
    3915                 :          1 : }
    3916                 :            : 
    3917                 :            : void
    3918                 :        195 : nvme_ctrlr_info_json(struct spdk_json_write_ctx *w, struct nvme_ctrlr *nvme_ctrlr)
    3919                 :        194 : {
    3920                 :            :         struct spdk_nvme_transport_id *trid;
    3921                 :            :         const struct spdk_nvme_ctrlr_opts *opts;
    3922                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    3923                 :            :         struct nvme_path_id *path_id;
    3924                 :            :         int32_t numa_id;
    3925                 :            : 
    3926                 :        195 :         spdk_json_write_object_begin(w);
    3927                 :            : 
    3928                 :        195 :         spdk_json_write_named_string(w, "state", nvme_ctrlr_get_state_str(nvme_ctrlr));
    3929                 :            : 
    3930                 :            : #ifdef SPDK_CONFIG_NVME_CUSE
    3931                 :        194 :         size_t cuse_name_size = 128;
    3932         [ -  + ]:        194 :         char cuse_name[cuse_name_size];
    3933                 :            : 
    3934   [ #  #  #  # ]:        194 :         int rc = spdk_nvme_cuse_get_ctrlr_name(nvme_ctrlr->ctrlr, cuse_name, &cuse_name_size);
    3935         [ +  + ]:        194 :         if (rc == 0) {
    3936                 :          3 :                 spdk_json_write_named_string(w, "cuse_device", cuse_name);
    3937                 :          0 :         }
    3938                 :            : #endif
    3939   [ #  #  #  #  :        195 :         trid = &nvme_ctrlr->active_path_id->trid;
                   #  # ]
    3940                 :        195 :         spdk_json_write_named_object_begin(w, "trid");
    3941                 :        195 :         nvme_bdev_dump_trid_json(trid, w);
    3942                 :        195 :         spdk_json_write_object_end(w);
    3943                 :            : 
    3944   [ #  #  #  #  :        195 :         path_id = TAILQ_NEXT(nvme_ctrlr->active_path_id, link);
          #  #  #  #  #  
                      # ]
    3945         [ +  + ]:        195 :         if (path_id != NULL) {
    3946                 :          3 :                 spdk_json_write_named_array_begin(w, "alternate_trids");
    3947                 :          0 :                 do {
    3948         [ #  # ]:          4 :                         trid = &path_id->trid;
    3949                 :          4 :                         spdk_json_write_object_begin(w);
    3950                 :          4 :                         nvme_bdev_dump_trid_json(trid, w);
    3951                 :          4 :                         spdk_json_write_object_end(w);
    3952                 :            : 
    3953   [ #  #  #  #  :          4 :                         path_id = TAILQ_NEXT(path_id, link);
                   #  # ]
    3954         [ +  + ]:          4 :                 } while (path_id != NULL);
    3955                 :          3 :                 spdk_json_write_array_end(w);
    3956                 :          0 :         }
    3957                 :            : 
    3958   [ #  #  #  # ]:        195 :         cdata = spdk_nvme_ctrlr_get_data(nvme_ctrlr->ctrlr);
    3959   [ #  #  #  # ]:        195 :         spdk_json_write_named_uint16(w, "cntlid", cdata->cntlid);
    3960                 :            : 
    3961   [ #  #  #  # ]:        195 :         opts = spdk_nvme_ctrlr_get_opts(nvme_ctrlr->ctrlr);
    3962                 :        195 :         spdk_json_write_named_object_begin(w, "host");
    3963         [ #  # ]:        195 :         spdk_json_write_named_string(w, "nqn", opts->hostnqn);
    3964         [ #  # ]:        195 :         spdk_json_write_named_string(w, "addr", opts->src_addr);
    3965         [ #  # ]:        195 :         spdk_json_write_named_string(w, "svcid", opts->src_svcid);
    3966                 :        195 :         spdk_json_write_object_end(w);
    3967                 :            : 
    3968   [ #  #  #  # ]:        195 :         numa_id = spdk_nvme_ctrlr_get_numa_id(nvme_ctrlr->ctrlr);
    3969         [ +  + ]:        195 :         if (numa_id != SPDK_ENV_NUMA_ID_ANY) {
    3970                 :          7 :                 spdk_json_write_named_uint32(w, "numa_id", numa_id);
    3971                 :          0 :         }
    3972                 :        195 :         spdk_json_write_object_end(w);
    3973                 :        195 : }
    3974                 :            : 
    3975                 :            : static void
    3976                 :       1014 : nvme_namespace_info_json(struct spdk_json_write_ctx *w,
    3977                 :            :                          struct nvme_ns *nvme_ns)
    3978                 :       1012 : {
    3979                 :            :         struct spdk_nvme_ns *ns;
    3980                 :            :         struct spdk_nvme_ctrlr *ctrlr;
    3981                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    3982                 :            :         const struct spdk_nvme_transport_id *trid;
    3983                 :            :         union spdk_nvme_vs_register vs;
    3984                 :            :         const struct spdk_nvme_ns_data *nsdata;
    3985                 :        893 :         char buf[128];
    3986                 :            : 
    3987   [ #  #  #  # ]:       1014 :         ns = nvme_ns->ns;
    3988         [ +  + ]:       1014 :         if (ns == NULL) {
    3989                 :          0 :                 return;
    3990                 :            :         }
    3991                 :            : 
    3992                 :       1014 :         ctrlr = spdk_nvme_ns_get_ctrlr(ns);
    3993                 :            : 
    3994                 :       1014 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    3995                 :       1014 :         trid = spdk_nvme_ctrlr_get_transport_id(ctrlr);
    3996                 :       1014 :         vs = spdk_nvme_ctrlr_get_regs_vs(ctrlr);
    3997                 :            : 
    3998                 :       1014 :         spdk_json_write_object_begin(w);
    3999                 :            : 
    4000   [ +  +  #  #  :       1014 :         if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
                   #  # ]
    4001         [ #  # ]:        885 :                 spdk_json_write_named_string(w, "pci_address", trid->traddr);
    4002                 :          2 :         }
    4003                 :            : 
    4004                 :       1014 :         spdk_json_write_named_object_begin(w, "trid");
    4005                 :            : 
    4006                 :       1014 :         nvme_bdev_dump_trid_json(trid, w);
    4007                 :            : 
    4008                 :       1014 :         spdk_json_write_object_end(w);
    4009                 :            : 
    4010                 :            : #ifdef SPDK_CONFIG_NVME_CUSE
    4011                 :       1012 :         size_t cuse_name_size = 128;
    4012         [ -  + ]:       1012 :         char cuse_name[cuse_name_size];
    4013                 :            : 
    4014                 :       1012 :         int rc = spdk_nvme_cuse_get_ns_name(ctrlr, spdk_nvme_ns_get_id(ns),
    4015                 :            :                                             cuse_name, &cuse_name_size);
    4016         [ +  + ]:       1012 :         if (rc == 0) {
    4017                 :          4 :                 spdk_json_write_named_string(w, "cuse_device", cuse_name);
    4018                 :          0 :         }
    4019                 :            : #endif
    4020                 :            : 
    4021                 :       1014 :         spdk_json_write_named_object_begin(w, "ctrlr_data");
    4022                 :            : 
    4023   [ #  #  #  # ]:       1014 :         spdk_json_write_named_uint16(w, "cntlid", cdata->cntlid);
    4024                 :            : 
    4025   [ #  #  #  # ]:       1014 :         spdk_json_write_named_string_fmt(w, "vendor_id", "0x%04x", cdata->vid);
    4026                 :            : 
    4027         [ -  + ]:       1014 :         snprintf(buf, sizeof(cdata->mn) + 1, "%s", cdata->mn);
    4028                 :       1014 :         spdk_str_trim(buf);
    4029                 :       1014 :         spdk_json_write_named_string(w, "model_number", buf);
    4030                 :            : 
    4031         [ -  + ]:       1014 :         snprintf(buf, sizeof(cdata->sn) + 1, "%s", cdata->sn);
    4032                 :       1014 :         spdk_str_trim(buf);
    4033                 :       1014 :         spdk_json_write_named_string(w, "serial_number", buf);
    4034                 :            : 
    4035         [ -  + ]:       1014 :         snprintf(buf, sizeof(cdata->fr) + 1, "%s", cdata->fr);
    4036                 :       1014 :         spdk_str_trim(buf);
    4037                 :       1014 :         spdk_json_write_named_string(w, "firmware_revision", buf);
    4038                 :            : 
    4039   [ +  +  #  #  :       1014 :         if (cdata->subnqn[0] != '\0') {
          #  #  #  #  #  
                      # ]
    4040         [ #  # ]:        294 :                 spdk_json_write_named_string(w, "subnqn", cdata->subnqn);
    4041                 :          2 :         }
    4042                 :            : 
    4043                 :       1014 :         spdk_json_write_named_object_begin(w, "oacs");
    4044                 :            : 
    4045   [ #  #  #  # ]:       1014 :         spdk_json_write_named_uint32(w, "security", cdata->oacs.security);
    4046   [ #  #  #  # ]:       1014 :         spdk_json_write_named_uint32(w, "format", cdata->oacs.format);
    4047   [ #  #  #  # ]:       1014 :         spdk_json_write_named_uint32(w, "firmware", cdata->oacs.firmware);
    4048   [ #  #  #  # ]:       1014 :         spdk_json_write_named_uint32(w, "ns_manage", cdata->oacs.ns_manage);
    4049                 :            : 
    4050                 :       1014 :         spdk_json_write_object_end(w);
    4051                 :            : 
    4052   [ #  #  #  # ]:       1014 :         spdk_json_write_named_bool(w, "multi_ctrlr", cdata->cmic.multi_ctrlr);
    4053   [ #  #  #  # ]:       1014 :         spdk_json_write_named_bool(w, "ana_reporting", cdata->cmic.ana_reporting);
    4054                 :            : 
    4055                 :       1014 :         spdk_json_write_object_end(w);
    4056                 :            : 
    4057                 :       1014 :         spdk_json_write_named_object_begin(w, "vs");
    4058                 :            : 
    4059                 :       1014 :         spdk_json_write_name(w, "nvme_version");
    4060         [ +  + ]:       1014 :         if (vs.bits.ter) {
    4061                 :          0 :                 spdk_json_write_string_fmt(w, "%u.%u.%u", vs.bits.mjr, vs.bits.mnr, vs.bits.ter);
    4062                 :          0 :         } else {
    4063                 :       1014 :                 spdk_json_write_string_fmt(w, "%u.%u", vs.bits.mjr, vs.bits.mnr);
    4064                 :            :         }
    4065                 :            : 
    4066                 :       1014 :         spdk_json_write_object_end(w);
    4067                 :            : 
    4068                 :       1014 :         nsdata = spdk_nvme_ns_get_data(ns);
    4069                 :            : 
    4070                 :       1014 :         spdk_json_write_named_object_begin(w, "ns_data");
    4071                 :            : 
    4072                 :       1014 :         spdk_json_write_named_uint32(w, "id", spdk_nvme_ns_get_id(ns));
    4073                 :            : 
    4074   [ +  +  #  #  :       1014 :         if (cdata->cmic.ana_reporting) {
                   #  # ]
    4075                 :          0 :                 spdk_json_write_named_string(w, "ana_state",
    4076   [ #  #  #  # ]:          0 :                                              _nvme_ana_state_str(nvme_ns->ana_state));
    4077                 :          0 :         }
    4078                 :            : 
    4079   [ #  #  #  # ]:       1014 :         spdk_json_write_named_bool(w, "can_share", nsdata->nmic.can_share);
    4080                 :            : 
    4081                 :       1014 :         spdk_json_write_object_end(w);
    4082                 :            : 
    4083   [ +  +  #  #  :       1014 :         if (cdata->oacs.security) {
                   #  # ]
    4084                 :         21 :                 spdk_json_write_named_object_begin(w, "security");
    4085                 :            : 
    4086   [ -  +  #  #  :         21 :                 spdk_json_write_named_bool(w, "opal", nvme_ns->bdev->opal);
          #  #  #  #  #  
                      # ]
    4087                 :            : 
    4088                 :         21 :                 spdk_json_write_object_end(w);
    4089                 :          0 :         }
    4090                 :            : 
    4091                 :       1014 :         spdk_json_write_object_end(w);
    4092                 :          2 : }
    4093                 :            : 
    4094                 :            : static const char *
    4095                 :       1012 : nvme_bdev_get_mp_policy_str(struct nvme_bdev *nbdev)
    4096                 :            : {
    4097   [ +  -  +  #  :       1012 :         switch (nbdev->mp_policy) {
                #  #  # ]
    4098                 :       1010 :         case BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE:
    4099                 :       1012 :                 return "active_passive";
    4100                 :          0 :         case BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE:
    4101                 :          0 :                 return "active_active";
    4102                 :          0 :         default:
    4103         [ #  # ]:          0 :                 assert(false);
    4104                 :            :                 return "invalid";
    4105                 :            :         }
    4106                 :          2 : }
    4107                 :            : 
    4108                 :            : static const char *
    4109                 :          0 : nvme_bdev_get_mp_selector_str(struct nvme_bdev *nbdev)
    4110                 :            : {
    4111   [ #  #  #  #  :          0 :         switch (nbdev->mp_selector) {
                #  #  # ]
    4112                 :          0 :         case BDEV_NVME_MP_SELECTOR_ROUND_ROBIN:
    4113                 :          0 :                 return "round_robin";
    4114                 :          0 :         case BDEV_NVME_MP_SELECTOR_QUEUE_DEPTH:
    4115                 :          0 :                 return "queue_depth";
    4116                 :          0 :         default:
    4117         [ #  # ]:          0 :                 assert(false);
    4118                 :            :                 return "invalid";
    4119                 :            :         }
    4120                 :          0 : }
    4121                 :            : 
    4122                 :            : static int
    4123                 :       1012 : bdev_nvme_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
    4124                 :            : {
    4125                 :       1012 :         struct nvme_bdev *nvme_bdev = ctx;
    4126                 :            :         struct nvme_ns *nvme_ns;
    4127                 :            : 
    4128   [ -  +  #  # ]:       1012 :         pthread_mutex_lock(&nvme_bdev->mutex);
    4129                 :       1012 :         spdk_json_write_named_array_begin(w, "nvme");
    4130   [ +  +  #  #  :       2026 :         TAILQ_FOREACH(nvme_ns, &nvme_bdev->nvme_ns_list, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    4131                 :       1014 :                 nvme_namespace_info_json(w, nvme_ns);
    4132                 :          2 :         }
    4133                 :       1012 :         spdk_json_write_array_end(w);
    4134                 :       1012 :         spdk_json_write_named_string(w, "mp_policy", nvme_bdev_get_mp_policy_str(nvme_bdev));
    4135   [ +  +  #  #  :       1012 :         if (nvme_bdev->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE) {
                   #  # ]
    4136                 :          0 :                 spdk_json_write_named_string(w, "selector", nvme_bdev_get_mp_selector_str(nvme_bdev));
    4137   [ #  #  #  #  :          0 :                 if (nvme_bdev->mp_selector == BDEV_NVME_MP_SELECTOR_ROUND_ROBIN) {
                   #  # ]
    4138   [ #  #  #  # ]:          0 :                         spdk_json_write_named_uint32(w, "rr_min_io", nvme_bdev->rr_min_io);
    4139                 :          0 :                 }
    4140                 :          0 :         }
    4141   [ -  +  #  # ]:       1012 :         pthread_mutex_unlock(&nvme_bdev->mutex);
    4142                 :            : 
    4143                 :       1012 :         return 0;
    4144                 :            : }
    4145                 :            : 
    4146                 :            : static void
    4147                 :        133 : bdev_nvme_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
    4148                 :            : {
    4149                 :            :         /* No config per bdev needed */
    4150                 :        133 : }
    4151                 :            : 
    4152                 :            : static uint64_t
    4153                 :          0 : bdev_nvme_get_spin_time(struct spdk_io_channel *ch)
    4154                 :            : {
    4155                 :          0 :         struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(ch);
    4156                 :            :         struct nvme_io_path *io_path;
    4157                 :            :         struct nvme_poll_group *group;
    4158                 :          0 :         uint64_t spin_time = 0;
    4159                 :            : 
    4160   [ #  #  #  #  :          0 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    4161   [ #  #  #  #  :          0 :                 group = io_path->qpair->group;
             #  #  #  # ]
    4162                 :            : 
    4163   [ #  #  #  #  :          0 :                 if (!group || !group->collect_spin_stat) {
          #  #  #  #  #  
                      # ]
    4164                 :          0 :                         continue;
    4165                 :            :                 }
    4166                 :            : 
    4167   [ #  #  #  #  :          0 :                 if (group->end_ticks != 0) {
                   #  # ]
    4168   [ #  #  #  #  :          0 :                         group->spin_ticks += (group->end_ticks - group->start_ticks);
          #  #  #  #  #  
                #  #  # ]
    4169   [ #  #  #  # ]:          0 :                         group->end_ticks = 0;
    4170                 :          0 :                 }
    4171                 :            : 
    4172   [ #  #  #  # ]:          0 :                 spin_time += group->spin_ticks;
    4173   [ #  #  #  # ]:          0 :                 group->start_ticks = 0;
    4174   [ #  #  #  # ]:          0 :                 group->spin_ticks = 0;
    4175                 :          0 :         }
    4176                 :            : 
    4177         [ #  # ]:          0 :         return (spin_time * 1000000ULL) / spdk_get_ticks_hz();
    4178                 :            : }
    4179                 :            : 
    4180                 :            : static void
    4181                 :          0 : bdev_nvme_reset_device_stat(void *ctx)
    4182                 :            : {
    4183                 :          0 :         struct nvme_bdev *nbdev = ctx;
    4184                 :            : 
    4185   [ #  #  #  #  :          0 :         if (nbdev->err_stat != NULL) {
                   #  # ]
    4186   [ #  #  #  #  :          0 :                 memset(nbdev->err_stat, 0, sizeof(struct nvme_error_stat));
                   #  # ]
    4187                 :          0 :         }
    4188                 :          0 : }
    4189                 :            : 
    4190                 :            : /* JSON string should be lowercases and underscore delimited string. */
    4191                 :            : static void
    4192                 :          8 : bdev_nvme_format_nvme_status(char *dst, const char *src)
    4193                 :            : {
    4194                 :          0 :         char tmp[256];
    4195                 :            : 
    4196                 :          8 :         spdk_strcpy_replace(dst, 256, src, " - ", "_");
    4197                 :          8 :         spdk_strcpy_replace(tmp, 256, dst, "-", "_");
    4198                 :          8 :         spdk_strcpy_replace(dst, 256, tmp, " ", "_");
    4199                 :          8 :         spdk_strlwr(dst);
    4200                 :          8 : }
    4201                 :            : 
    4202                 :            : static void
    4203                 :          5 : bdev_nvme_dump_device_stat_json(void *ctx, struct spdk_json_write_ctx *w)
    4204                 :            : {
    4205                 :          5 :         struct nvme_bdev *nbdev = ctx;
    4206                 :          5 :         struct spdk_nvme_status status = {};
    4207                 :            :         uint16_t sct, sc;
    4208                 :          0 :         char status_json[256];
    4209                 :            :         const char *status_str;
    4210                 :            : 
    4211   [ +  +  #  #  :          5 :         if (nbdev->err_stat == NULL) {
                   #  # ]
    4212                 :          1 :                 return;
    4213                 :            :         }
    4214                 :            : 
    4215                 :          4 :         spdk_json_write_named_object_begin(w, "nvme_error");
    4216                 :            : 
    4217                 :          4 :         spdk_json_write_named_object_begin(w, "status_type");
    4218         [ +  + ]:         36 :         for (sct = 0; sct < 8; sct++) {
    4219   [ +  +  #  #  :         32 :                 if (nbdev->err_stat->status_type[sct] == 0) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    4220                 :         28 :                         continue;
    4221                 :            :                 }
    4222                 :          4 :                 status.sct = sct;
    4223                 :            : 
    4224                 :          4 :                 status_str = spdk_nvme_cpl_get_status_type_string(&status);
    4225   [ -  +  #  # ]:          4 :                 assert(status_str != NULL);
    4226                 :          4 :                 bdev_nvme_format_nvme_status(status_json, status_str);
    4227                 :            : 
    4228   [ #  #  #  #  :          4 :                 spdk_json_write_named_uint32(w, status_json, nbdev->err_stat->status_type[sct]);
          #  #  #  #  #  
                #  #  # ]
    4229                 :          0 :         }
    4230                 :          4 :         spdk_json_write_object_end(w);
    4231                 :            : 
    4232                 :          4 :         spdk_json_write_named_object_begin(w, "status_code");
    4233         [ +  + ]:         20 :         for (sct = 0; sct < 4; sct++) {
    4234                 :         16 :                 status.sct = sct;
    4235         [ +  + ]:       4112 :                 for (sc = 0; sc < 256; sc++) {
    4236   [ +  +  #  #  :       4096 :                         if (nbdev->err_stat->status[sct][sc] == 0) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    4237                 :       4092 :                                 continue;
    4238                 :            :                         }
    4239                 :          4 :                         status.sc = sc;
    4240                 :            : 
    4241                 :          4 :                         status_str = spdk_nvme_cpl_get_status_string(&status);
    4242   [ -  +  #  # ]:          4 :                         assert(status_str != NULL);
    4243                 :          4 :                         bdev_nvme_format_nvme_status(status_json, status_str);
    4244                 :            : 
    4245   [ #  #  #  #  :          4 :                         spdk_json_write_named_uint32(w, status_json, nbdev->err_stat->status[sct][sc]);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    4246                 :          0 :                 }
    4247                 :          0 :         }
    4248                 :          4 :         spdk_json_write_object_end(w);
    4249                 :            : 
    4250                 :          4 :         spdk_json_write_object_end(w);
    4251                 :          0 : }
    4252                 :            : 
    4253                 :            : static bool
    4254                 :     101682 : bdev_nvme_accel_sequence_supported(void *ctx, enum spdk_bdev_io_type type)
    4255                 :            : {
    4256                 :     101682 :         struct nvme_bdev *nbdev = ctx;
    4257                 :            :         struct nvme_ns *nvme_ns;
    4258                 :            :         struct spdk_nvme_ctrlr *ctrlr;
    4259                 :            : 
    4260   [ +  +  +  + ]:     101682 :         if (!g_opts.allow_accel_sequence) {
    4261                 :     101598 :                 return false;
    4262                 :            :         }
    4263                 :            : 
    4264         [ +  + ]:         84 :         switch (type) {
    4265                 :          8 :         case SPDK_BDEV_IO_TYPE_WRITE:
    4266                 :            :         case SPDK_BDEV_IO_TYPE_READ:
    4267                 :          8 :                 break;
    4268                 :         76 :         default:
    4269                 :         76 :                 return false;
    4270                 :            :         }
    4271                 :            : 
    4272   [ #  #  #  #  :          8 :         nvme_ns = TAILQ_FIRST(&nbdev->nvme_ns_list);
                   #  # ]
    4273   [ -  +  #  # ]:          8 :         assert(nvme_ns != NULL);
    4274                 :            : 
    4275   [ #  #  #  #  :          8 :         ctrlr = nvme_ns->ctrlr->ctrlr;
             #  #  #  # ]
    4276   [ -  +  #  # ]:          8 :         assert(ctrlr != NULL);
    4277                 :            : 
    4278                 :          8 :         return spdk_nvme_ctrlr_get_flags(ctrlr) & SPDK_NVME_CTRLR_ACCEL_SEQUENCE_SUPPORTED;
    4279                 :       1281 : }
    4280                 :            : 
    4281                 :            : static const struct spdk_bdev_fn_table nvmelib_fn_table = {
    4282                 :            :         .destruct                       = bdev_nvme_destruct,
    4283                 :            :         .submit_request                 = bdev_nvme_submit_request,
    4284                 :            :         .io_type_supported              = bdev_nvme_io_type_supported,
    4285                 :            :         .get_io_channel                 = bdev_nvme_get_io_channel,
    4286                 :            :         .dump_info_json                 = bdev_nvme_dump_info_json,
    4287                 :            :         .write_config_json              = bdev_nvme_write_config_json,
    4288                 :            :         .get_spin_time                  = bdev_nvme_get_spin_time,
    4289                 :            :         .get_module_ctx                 = bdev_nvme_get_module_ctx,
    4290                 :            :         .get_memory_domains             = bdev_nvme_get_memory_domains,
    4291                 :            :         .accel_sequence_supported       = bdev_nvme_accel_sequence_supported,
    4292                 :            :         .reset_device_stat              = bdev_nvme_reset_device_stat,
    4293                 :            :         .dump_device_stat_json          = bdev_nvme_dump_device_stat_json,
    4294                 :            : };
    4295                 :            : 
    4296                 :            : typedef int (*bdev_nvme_parse_ana_log_page_cb)(
    4297                 :            :         const struct spdk_nvme_ana_group_descriptor *desc, void *cb_arg);
    4298                 :            : 
    4299                 :            : static int
    4300                 :        280 : bdev_nvme_parse_ana_log_page(struct nvme_ctrlr *nvme_ctrlr,
    4301                 :            :                              bdev_nvme_parse_ana_log_page_cb cb_fn, void *cb_arg)
    4302                 :            : {
    4303                 :            :         struct spdk_nvme_ana_group_descriptor *copied_desc;
    4304                 :            :         uint8_t *orig_desc;
    4305                 :            :         uint32_t i, desc_size, copy_len;
    4306                 :        280 :         int rc = 0;
    4307                 :            : 
    4308   [ +  +  #  #  :        280 :         if (nvme_ctrlr->ana_log_page == NULL) {
                   #  # ]
    4309                 :          0 :                 return -EINVAL;
    4310                 :            :         }
    4311                 :            : 
    4312   [ #  #  #  # ]:        280 :         copied_desc = nvme_ctrlr->copied_ana_desc;
    4313                 :            : 
    4314   [ #  #  #  #  :        280 :         orig_desc = (uint8_t *)nvme_ctrlr->ana_log_page + sizeof(struct spdk_nvme_ana_page);
                   #  # ]
    4315   [ #  #  #  # ]:        280 :         copy_len = nvme_ctrlr->max_ana_log_page_size - sizeof(struct spdk_nvme_ana_page);
    4316                 :            : 
    4317   [ +  +  #  #  :        430 :         for (i = 0; i < nvme_ctrlr->ana_log_page->num_ana_group_desc; i++) {
          #  #  #  #  #  
                      # ]
    4318   [ -  +  -  + ]:        380 :                 memcpy(copied_desc, orig_desc, copy_len);
    4319                 :            : 
    4320   [ #  #  #  # ]:        380 :                 rc = cb_fn(copied_desc, cb_arg);
    4321         [ +  + ]:        380 :                 if (rc != 0) {
    4322                 :        230 :                         break;
    4323                 :            :                 }
    4324                 :            : 
    4325                 :        150 :                 desc_size = sizeof(struct spdk_nvme_ana_group_descriptor) +
    4326   [ #  #  #  # ]:        150 :                             copied_desc->num_of_nsid * sizeof(uint32_t);
    4327         [ #  # ]:        150 :                 orig_desc += desc_size;
    4328                 :        150 :                 copy_len -= desc_size;
    4329                 :         30 :         }
    4330                 :            : 
    4331                 :        280 :         return rc;
    4332                 :         42 : }
    4333                 :            : 
    4334                 :            : static int
    4335                 :         21 : nvme_ns_ana_transition_timedout(void *ctx)
    4336                 :            : {
    4337                 :         21 :         struct nvme_ns *nvme_ns = ctx;
    4338                 :            : 
    4339         [ #  # ]:         21 :         spdk_poller_unregister(&nvme_ns->anatt_timer);
    4340   [ #  #  #  # ]:         21 :         nvme_ns->ana_transition_timedout = true;
    4341                 :            : 
    4342                 :         21 :         return SPDK_POLLER_BUSY;
    4343                 :            : }
    4344                 :            : 
    4345                 :            : static void
    4346                 :        296 : _nvme_ns_set_ana_state(struct nvme_ns *nvme_ns,
    4347                 :            :                        const struct spdk_nvme_ana_group_descriptor *desc)
    4348                 :            : {
    4349                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    4350                 :            : 
    4351   [ #  #  #  #  :        296 :         nvme_ns->ana_group_id = desc->ana_group_id;
             #  #  #  # ]
    4352   [ #  #  #  #  :        296 :         nvme_ns->ana_state = desc->ana_state;
                   #  # ]
    4353   [ #  #  #  # ]:        296 :         nvme_ns->ana_state_updating = false;
    4354                 :            : 
    4355   [ +  +  +  #  :        296 :         switch (nvme_ns->ana_state) {
                #  #  # ]
    4356                 :        220 :         case SPDK_NVME_ANA_OPTIMIZED_STATE:
    4357                 :            :         case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
    4358   [ #  #  #  # ]:        259 :                 nvme_ns->ana_transition_timedout = false;
    4359         [ #  # ]:        259 :                 spdk_poller_unregister(&nvme_ns->anatt_timer);
    4360                 :        259 :                 break;
    4361                 :            : 
    4362                 :         27 :         case SPDK_NVME_ANA_INACCESSIBLE_STATE:
    4363                 :            :         case SPDK_NVME_ANA_CHANGE_STATE:
    4364   [ +  +  #  #  :         33 :                 if (nvme_ns->anatt_timer != NULL) {
                   #  # ]
    4365                 :          7 :                         break;
    4366                 :            :                 }
    4367                 :            : 
    4368   [ #  #  #  #  :         26 :                 cdata = spdk_nvme_ctrlr_get_data(nvme_ns->ctrlr->ctrlr);
             #  #  #  # ]
    4369   [ #  #  #  #  :         26 :                 nvme_ns->anatt_timer = SPDK_POLLER_REGISTER(nvme_ns_ana_transition_timedout,
             #  #  #  # ]
    4370                 :            :                                        nvme_ns,
    4371                 :            :                                        cdata->anatt * SPDK_SEC_TO_USEC);
    4372                 :         26 :                 break;
    4373                 :          3 :         default:
    4374                 :          4 :                 break;
    4375                 :            :         }
    4376                 :        296 : }
    4377                 :            : 
    4378                 :            : static int
    4379                 :        322 : nvme_ns_set_ana_state(const struct spdk_nvme_ana_group_descriptor *desc, void *cb_arg)
    4380                 :            : {
    4381                 :        322 :         struct nvme_ns *nvme_ns = cb_arg;
    4382                 :            :         uint32_t i;
    4383                 :            : 
    4384   [ +  +  #  #  :        322 :         assert(nvme_ns->ns != NULL);
             #  #  #  # ]
    4385                 :            : 
    4386   [ +  +  #  #  :        410 :         for (i = 0; i < desc->num_of_nsid; i++) {
                   #  # ]
    4387   [ +  +  #  #  :        318 :                 if (desc->nsid[i] != spdk_nvme_ns_get_id(nvme_ns->ns)) {
          #  #  #  #  #  
                #  #  # ]
    4388                 :         88 :                         continue;
    4389                 :            :                 }
    4390                 :            : 
    4391                 :        230 :                 _nvme_ns_set_ana_state(nvme_ns, desc);
    4392                 :        230 :                 return 1;
    4393                 :            :         }
    4394                 :            : 
    4395                 :         92 :         return 0;
    4396                 :         60 : }
    4397                 :            : 
    4398                 :            : static int
    4399                 :         20 : nvme_generate_uuid(const char *sn, uint32_t nsid, struct spdk_uuid *uuid)
    4400                 :            : {
    4401                 :         20 :         int rc = 0;
    4402                 :         15 :         struct spdk_uuid new_uuid, namespace_uuid;
    4403                 :         20 :         char merged_str[SPDK_NVME_CTRLR_SN_LEN + NSID_STR_LEN + 1] = {'\0'};
    4404                 :            :         /* This namespace UUID was generated using uuid_generate() method. */
    4405                 :         20 :         const char *namespace_str = {"edaed2de-24bc-4b07-b559-f47ecbe730fd"};
    4406                 :            :         int size;
    4407                 :            : 
    4408   [ +  +  -  +  :         20 :         assert(strlen(sn) <= SPDK_NVME_CTRLR_SN_LEN);
                   #  # ]
    4409                 :            : 
    4410                 :         20 :         spdk_uuid_set_null(&new_uuid);
    4411                 :         20 :         spdk_uuid_set_null(&namespace_uuid);
    4412                 :            : 
    4413                 :         20 :         size = snprintf(merged_str, sizeof(merged_str), "%s%"PRIu32, sn, nsid);
    4414   [ +  -  -  + ]:         20 :         if (size <= 0 || (unsigned long)size >= sizeof(merged_str)) {
    4415                 :          0 :                 return -EINVAL;
    4416                 :            :         }
    4417                 :            : 
    4418                 :         20 :         spdk_uuid_parse(&namespace_uuid, namespace_str);
    4419                 :            : 
    4420                 :         20 :         rc = spdk_uuid_generate_sha1(&new_uuid, &namespace_uuid, merged_str, size);
    4421         [ +  + ]:         20 :         if (rc == 0) {
    4422   [ #  #  #  # ]:         20 :                 memcpy(uuid, &new_uuid, sizeof(struct spdk_uuid));
    4423                 :          5 :         }
    4424                 :            : 
    4425                 :         20 :         return rc;
    4426                 :          5 : }
    4427                 :            : 
    4428                 :            : static int
    4429                 :        952 : nvme_disk_create(struct spdk_bdev *disk, const char *base_name,
    4430                 :            :                  struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_ns *ns,
    4431                 :            :                  struct spdk_bdev_nvme_ctrlr_opts *bdev_opts, void *ctx)
    4432                 :            : {
    4433                 :            :         const struct spdk_uuid          *uuid;
    4434                 :            :         const uint8_t *nguid;
    4435                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    4436                 :            :         const struct spdk_nvme_ns_data  *nsdata;
    4437                 :            :         const struct spdk_nvme_ctrlr_opts *opts;
    4438                 :            :         enum spdk_nvme_csi              csi;
    4439                 :            :         uint32_t atomic_bs, phys_bs, bs;
    4440                 :        952 :         char sn_tmp[SPDK_NVME_CTRLR_SN_LEN + 1] = {'\0'};
    4441                 :            :         int rc;
    4442                 :            : 
    4443                 :        952 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    4444                 :        952 :         csi = spdk_nvme_ns_get_csi(ns);
    4445                 :        952 :         opts = spdk_nvme_ctrlr_get_opts(ctrlr);
    4446                 :            : 
    4447      [ +  +  - ]:        952 :         switch (csi) {
    4448                 :        900 :         case SPDK_NVME_CSI_NVM:
    4449   [ +  -  +  - ]:        950 :                 disk->product_name = "NVMe disk";
    4450                 :        950 :                 break;
    4451                 :          2 :         case SPDK_NVME_CSI_ZNS:
    4452   [ #  #  #  # ]:          2 :                 disk->product_name = "NVMe ZNS disk";
    4453   [ #  #  #  # ]:          2 :                 disk->zoned = true;
    4454   [ #  #  #  # ]:          2 :                 disk->zone_size = spdk_nvme_zns_ns_get_zone_size_sectors(ns);
    4455   [ #  #  #  #  :          2 :                 disk->max_zone_append_size = spdk_nvme_zns_ctrlr_get_max_zone_append_size(ctrlr) /
             #  #  #  # ]
    4456                 :          2 :                                              spdk_nvme_ns_get_extended_sector_size(ns);
    4457   [ #  #  #  # ]:          2 :                 disk->max_open_zones = spdk_nvme_zns_ns_get_max_open_zones(ns);
    4458   [ #  #  #  # ]:          2 :                 disk->max_active_zones = spdk_nvme_zns_ns_get_max_active_zones(ns);
    4459                 :          2 :                 break;
    4460                 :          0 :         default:
    4461   [ #  #  #  #  :          0 :                 if (bdev_opts->allow_unrecognized_csi) {
             #  #  #  # ]
    4462   [ #  #  #  # ]:          0 :                         disk->product_name = "NVMe Passthrough disk";
    4463                 :          0 :                         break;
    4464                 :            :                 }
    4465                 :          0 :                 SPDK_ERRLOG("unsupported CSI: %u\n", csi);
    4466                 :          0 :                 return -ENOTSUP;
    4467                 :            :         }
    4468                 :            : 
    4469                 :        952 :         nguid = spdk_nvme_ns_get_nguid(ns);
    4470         [ +  + ]:        952 :         if (!nguid) {
    4471                 :        825 :                 uuid = spdk_nvme_ns_get_uuid(ns);
    4472         [ +  + ]:        825 :                 if (uuid) {
    4473         [ #  # ]:        126 :                         disk->uuid = *uuid;
    4474   [ +  +  +  + ]:        711 :                 } else if (g_opts.generate_uuids) {
    4475         [ #  # ]:          0 :                         spdk_strcpy_pad(sn_tmp, cdata->sn, SPDK_NVME_CTRLR_SN_LEN, '\0');
    4476         [ #  # ]:          0 :                         rc = nvme_generate_uuid(sn_tmp, spdk_nvme_ns_get_id(ns), &disk->uuid);
    4477         [ #  # ]:          0 :                         if (rc < 0) {
    4478         [ #  # ]:          0 :                                 SPDK_ERRLOG("UUID generation failed (%s)\n", spdk_strerror(-rc));
    4479                 :          0 :                                 return rc;
    4480                 :            :                         }
    4481                 :          0 :                 }
    4482                 :         50 :         } else {
    4483   [ #  #  #  #  :        127 :                 memcpy(&disk->uuid, nguid, sizeof(disk->uuid));
                   #  # ]
    4484                 :            :         }
    4485                 :            : 
    4486   [ +  -  +  - ]:        952 :         disk->name = spdk_sprintf_alloc("%sn%d", base_name, spdk_nvme_ns_get_id(ns));
    4487   [ +  +  +  -  :        952 :         if (!disk->name) {
                   -  + ]
    4488                 :          0 :                 return -ENOMEM;
    4489                 :            :         }
    4490                 :            : 
    4491   [ +  -  +  - ]:        952 :         disk->write_cache = 0;
    4492   [ +  +  +  -  :        952 :         if (cdata->vwc.present) {
                   +  - ]
    4493                 :            :                 /* Enable if the Volatile Write Cache exists */
    4494   [ #  #  #  # ]:        718 :                 disk->write_cache = 1;
    4495                 :         10 :         }
    4496   [ +  +  +  -  :        952 :         if (cdata->oncs.write_zeroes) {
                   +  - ]
    4497   [ #  #  #  # ]:        740 :                 disk->max_write_zeroes = UINT16_MAX + 1;
    4498                 :         10 :         }
    4499   [ +  -  +  - ]:        952 :         disk->blocklen = spdk_nvme_ns_get_extended_sector_size(ns);
    4500   [ +  -  +  - ]:        952 :         disk->blockcnt = spdk_nvme_ns_get_num_sectors(ns);
    4501   [ +  -  +  - ]:        952 :         disk->max_segment_size = spdk_nvme_ctrlr_get_max_xfer_size(ctrlr);
    4502   [ +  -  +  -  :        952 :         disk->ctratt.raw = cdata->ctratt.raw;
          +  -  +  -  +  
                -  +  - ]
    4503   [ +  -  +  - ]:        952 :         disk->nsid = spdk_nvme_ns_get_id(ns);
    4504                 :            :         /* NVMe driver will split one request into multiple requests
    4505                 :            :          * based on MDTS and stripe boundary, the bdev layer will use
    4506                 :            :          * max_segment_size and max_num_segments to split one big IO
    4507                 :            :          * into multiple requests, then small request can't run out
    4508                 :            :          * of NVMe internal requests data structure.
    4509                 :            :          */
    4510   [ +  -  +  +  :        952 :         if (opts && opts->io_queue_requests) {
             +  -  -  + ]
    4511   [ +  -  +  -  :        796 :                 disk->max_num_segments = opts->io_queue_requests / 2;
          +  -  +  -  +  
                      - ]
    4512                 :         11 :         }
    4513         [ +  + ]:        952 :         if (spdk_nvme_ctrlr_get_flags(ctrlr) & SPDK_NVME_CTRLR_SGL_SUPPORTED) {
    4514                 :            :                 /* The nvme driver will try to split I/O that have too many
    4515                 :            :                  * SGEs, but it doesn't work if that last SGE doesn't end on
    4516                 :            :                  * an aggregate total that is block aligned. The bdev layer has
    4517                 :            :                  * a more robust splitting framework, so use that instead for
    4518                 :            :                  * this case. (See issue #3269.)
    4519                 :            :                  */
    4520                 :        740 :                 uint16_t max_sges = spdk_nvme_ctrlr_get_max_sges(ctrlr);
    4521                 :            : 
    4522   [ +  +  #  #  :        740 :                 if (disk->max_num_segments == 0) {
                   #  # ]
    4523   [ #  #  #  # ]:          0 :                         disk->max_num_segments = max_sges;
    4524                 :          0 :                 } else {
    4525   [ -  +  #  #  :        740 :                         disk->max_num_segments = spdk_min(disk->max_num_segments, max_sges);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    4526                 :            :                 }
    4527                 :         10 :         }
    4528   [ +  -  +  - ]:        952 :         disk->optimal_io_boundary = spdk_nvme_ns_get_optimal_io_boundary(ns);
    4529                 :            : 
    4530                 :        952 :         nsdata = spdk_nvme_ns_get_data(ns);
    4531                 :        952 :         bs = spdk_nvme_ns_get_sector_size(ns);
    4532                 :        952 :         atomic_bs = bs;
    4533                 :        952 :         phys_bs = bs;
    4534   [ +  +  +  -  :        952 :         if (nsdata->nabo == 0) {
                   -  + ]
    4535   [ +  +  +  +  :        952 :                 if (nsdata->nsfeat.ns_atomic_write_unit && nsdata->nawupf) {
          -  +  #  #  #  
                #  #  # ]
    4536   [ #  #  #  #  :          0 :                         atomic_bs = bs * (1 + nsdata->nawupf);
                   #  # ]
    4537                 :          0 :                 } else {
    4538   [ +  -  +  -  :        952 :                         atomic_bs = bs * (1 + cdata->awupf);
                   +  - ]
    4539                 :            :                 }
    4540                 :         50 :         }
    4541   [ +  +  +  -  :        952 :         if (nsdata->nsfeat.optperf) {
                   +  - ]
    4542   [ #  #  #  #  :        718 :                 phys_bs = bs * (1 + nsdata->npwg);
                   #  # ]
    4543                 :         10 :         }
    4544   [ -  +  +  -  :        952 :         disk->phys_blocklen = spdk_min(phys_bs, atomic_bs);
                   +  - ]
    4545                 :            : 
    4546   [ +  -  +  - ]:        952 :         disk->md_len = spdk_nvme_ns_get_md_size(ns);
    4547   [ +  +  +  -  :        952 :         if (disk->md_len != 0) {
                   -  + ]
    4548   [ #  #  #  #  :         47 :                 disk->md_interleave = nsdata->flbas.extended;
             #  #  #  # ]
    4549   [ #  #  #  # ]:         47 :                 disk->dif_type = (enum spdk_dif_type)spdk_nvme_ns_get_pi_type(ns);
    4550   [ -  +  #  #  :         47 :                 if (disk->dif_type != SPDK_DIF_DISABLE) {
                   #  # ]
    4551   [ #  #  #  #  :          0 :                         disk->dif_is_head_of_md = nsdata->dps.md_start;
             #  #  #  # ]
    4552   [ #  #  #  #  :          0 :                         disk->dif_check_flags = bdev_opts->prchk_flags;
             #  #  #  # ]
    4553   [ #  #  #  # ]:          0 :                         disk->dif_pi_format = (enum spdk_dif_pi_format)spdk_nvme_ns_get_pi_format(ns);
    4554                 :          0 :                 }
    4555                 :          0 :         }
    4556                 :            : 
    4557         [ +  + ]:        952 :         if (!(spdk_nvme_ctrlr_get_flags(ctrlr) &
    4558                 :            :               SPDK_NVME_CTRLR_COMPARE_AND_WRITE_SUPPORTED)) {
    4559   [ +  -  +  - ]:        847 :                 disk->acwu = 0;
    4560   [ +  -  #  #  :        155 :         } else if (nsdata->nsfeat.ns_atomic_write_unit) {
                   #  # ]
    4561   [ #  #  #  #  :        105 :                 disk->acwu = nsdata->nacwu + 1; /* 0-based */
          #  #  #  #  #  
                      # ]
    4562                 :          0 :         } else {
    4563   [ #  #  #  #  :          0 :                 disk->acwu = cdata->acwu + 1; /* 0-based */
          #  #  #  #  #  
                      # ]
    4564                 :            :         }
    4565                 :            : 
    4566   [ +  +  +  -  :        952 :         if (cdata->oncs.copy) {
                   +  - ]
    4567                 :            :                 /* For now bdev interface allows only single segment copy */
    4568   [ #  #  #  #  :        640 :                 disk->max_copy = nsdata->mssrl;
             #  #  #  # ]
    4569                 :         10 :         }
    4570                 :            : 
    4571   [ +  -  +  - ]:        952 :         disk->ctxt = ctx;
    4572   [ +  -  +  - ]:        952 :         disk->fn_table = &nvmelib_fn_table;
    4573   [ +  -  +  - ]:        952 :         disk->module = &nvme_if;
    4574                 :            : 
    4575   [ +  -  +  - ]:        952 :         disk->numa.id_valid = 1;
    4576   [ +  -  +  - ]:        952 :         disk->numa.id = spdk_nvme_ctrlr_get_numa_id(ctrlr);
    4577                 :            : 
    4578                 :        952 :         return 0;
    4579                 :         50 : }
    4580                 :            : 
    4581                 :            : static struct nvme_bdev *
    4582                 :        952 : nvme_bdev_alloc(void)
    4583                 :            : {
    4584                 :            :         struct nvme_bdev *bdev;
    4585                 :            :         int rc;
    4586                 :            : 
    4587                 :        952 :         bdev = calloc(1, sizeof(*bdev));
    4588         [ +  + ]:        952 :         if (!bdev) {
    4589                 :          0 :                 SPDK_ERRLOG("bdev calloc() failed\n");
    4590                 :          0 :                 return NULL;
    4591                 :            :         }
    4592                 :            : 
    4593   [ +  +  +  + ]:        952 :         if (g_opts.nvme_error_stat) {
    4594   [ #  #  #  # ]:          4 :                 bdev->err_stat = calloc(1, sizeof(struct nvme_error_stat));
    4595   [ -  +  #  #  :          4 :                 if (!bdev->err_stat) {
                   #  # ]
    4596                 :          0 :                         SPDK_ERRLOG("err_stat calloc() failed\n");
    4597                 :          0 :                         free(bdev);
    4598                 :          0 :                         return NULL;
    4599                 :            :                 }
    4600                 :          0 :         }
    4601                 :            : 
    4602   [ +  +  +  - ]:        952 :         rc = pthread_mutex_init(&bdev->mutex, NULL);
    4603         [ -  + ]:        952 :         if (rc != 0) {
    4604   [ #  #  #  # ]:          0 :                 free(bdev->err_stat);
    4605                 :          0 :                 free(bdev);
    4606                 :          0 :                 return NULL;
    4607                 :            :         }
    4608                 :            : 
    4609   [ -  +  -  + ]:        952 :         bdev->ref = 1;
    4610   [ -  +  -  + ]:        952 :         bdev->mp_policy = BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE;
    4611   [ -  +  -  + ]:        952 :         bdev->mp_selector = BDEV_NVME_MP_SELECTOR_ROUND_ROBIN;
    4612   [ -  +  -  + ]:        952 :         bdev->rr_min_io = UINT32_MAX;
    4613   [ -  +  -  +  :        952 :         TAILQ_INIT(&bdev->nvme_ns_list);
          -  +  -  +  -  
          +  -  +  -  +  
                   -  + ]
    4614                 :            : 
    4615                 :        952 :         return bdev;
    4616                 :         50 : }
    4617                 :            : 
    4618                 :            : static int
    4619                 :        952 : nvme_bdev_create(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *nvme_ns)
    4620                 :            : {
    4621                 :            :         struct nvme_bdev *bdev;
    4622   [ +  -  +  - ]:        952 :         struct nvme_bdev_ctrlr *nbdev_ctrlr = nvme_ctrlr->nbdev_ctrlr;
    4623                 :            :         int rc;
    4624                 :            : 
    4625                 :        952 :         bdev = nvme_bdev_alloc();
    4626         [ +  + ]:        952 :         if (bdev == NULL) {
    4627                 :          0 :                 SPDK_ERRLOG("Failed to allocate NVMe bdev\n");
    4628                 :          0 :                 return -ENOMEM;
    4629                 :            :         }
    4630                 :            : 
    4631   [ +  -  +  -  :        952 :         bdev->opal = nvme_ctrlr->opal_dev != NULL;
             +  -  +  - ]
    4632                 :            : 
    4633   [ +  -  +  -  :       1002 :         rc = nvme_disk_create(&bdev->disk, nbdev_ctrlr->name, nvme_ctrlr->ctrlr,
          +  -  +  -  +  
                      - ]
    4634   [ +  -  +  -  :         50 :                               nvme_ns->ns, &nvme_ctrlr->opts, bdev);
                   +  - ]
    4635         [ -  + ]:        952 :         if (rc != 0) {
    4636                 :          0 :                 SPDK_ERRLOG("Failed to create NVMe disk\n");
    4637                 :          0 :                 nvme_bdev_free(bdev);
    4638                 :          0 :                 return rc;
    4639                 :            :         }
    4640                 :            : 
    4641                 :       1002 :         spdk_io_device_register(bdev,
    4642                 :            :                                 bdev_nvme_create_bdev_channel_cb,
    4643                 :            :                                 bdev_nvme_destroy_bdev_channel_cb,
    4644                 :            :                                 sizeof(struct nvme_bdev_channel),
    4645   [ +  -  +  -  :        952 :                                 bdev->disk.name);
                   +  - ]
    4646                 :            : 
    4647   [ +  -  +  - ]:        952 :         nvme_ns->bdev = bdev;
    4648   [ +  -  +  -  :        952 :         bdev->nsid = nvme_ns->id;
             +  -  +  - ]
    4649   [ +  -  +  -  :        952 :         TAILQ_INSERT_TAIL(&bdev->nvme_ns_list, nvme_ns, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    4650                 :            : 
    4651   [ +  -  +  - ]:        952 :         bdev->nbdev_ctrlr = nbdev_ctrlr;
    4652   [ +  -  +  -  :        952 :         TAILQ_INSERT_TAIL(&nbdev_ctrlr->bdevs, bdev, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    4653                 :            : 
    4654         [ +  - ]:        952 :         rc = spdk_bdev_register(&bdev->disk);
    4655         [ +  + ]:        952 :         if (rc != 0) {
    4656                 :          4 :                 SPDK_ERRLOG("spdk_bdev_register() failed\n");
    4657                 :          4 :                 spdk_io_device_unregister(bdev, NULL);
    4658   [ #  #  #  # ]:          4 :                 nvme_ns->bdev = NULL;
    4659   [ +  +  #  #  :          4 :                 TAILQ_REMOVE(&nbdev_ctrlr->bdevs, bdev, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    4660                 :          4 :                 nvme_bdev_free(bdev);
    4661                 :          4 :                 return rc;
    4662                 :            :         }
    4663                 :            : 
    4664                 :        948 :         return 0;
    4665                 :         50 : }
    4666                 :            : 
    4667                 :            : static bool
    4668                 :        100 : bdev_nvme_compare_ns(struct spdk_nvme_ns *ns1, struct spdk_nvme_ns *ns2)
    4669                 :            : {
    4670                 :            :         const struct spdk_nvme_ns_data *nsdata1, *nsdata2;
    4671                 :            :         const struct spdk_uuid *uuid1, *uuid2;
    4672                 :            : 
    4673                 :        100 :         nsdata1 = spdk_nvme_ns_get_data(ns1);
    4674                 :        100 :         nsdata2 = spdk_nvme_ns_get_data(ns2);
    4675                 :        100 :         uuid1 = spdk_nvme_ns_get_uuid(ns1);
    4676                 :        100 :         uuid2 = spdk_nvme_ns_get_uuid(ns2);
    4677                 :            : 
    4678   [ +  +  -  +  :        170 :         return memcmp(nsdata1->nguid, nsdata2->nguid, sizeof(nsdata1->nguid)) == 0 &&
          #  #  #  #  #  
                      # ]
    4679   [ +  +  +  +  :         96 :                nsdata1->eui64 == nsdata2->eui64 &&
          #  #  #  #  #  
                      # ]
    4680   [ +  +  +  + ]:         92 :                ((uuid1 == NULL && uuid2 == NULL) ||
    4681   [ +  +  +  -  :        230 :                 (uuid1 != NULL && uuid2 != NULL && spdk_uuid_compare(uuid1, uuid2) == 0)) &&
             +  +  +  + ]
    4682                 :         80 :                spdk_nvme_ns_get_csi(ns1) == spdk_nvme_ns_get_csi(ns2);
    4683                 :            : }
    4684                 :            : 
    4685                 :            : static bool
    4686                 :         56 : hotplug_probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
    4687                 :            :                  struct spdk_nvme_ctrlr_opts *opts)
    4688                 :            : {
    4689                 :            :         struct nvme_probe_skip_entry *entry;
    4690                 :            : 
    4691   [ -  +  #  #  :         56 :         TAILQ_FOREACH(entry, &g_skipped_nvme_ctrlrs, tailq) {
             #  #  #  # ]
    4692   [ #  #  #  # ]:          0 :                 if (spdk_nvme_transport_id_compare(trid, &entry->trid) == 0) {
    4693                 :          0 :                         return false;
    4694                 :            :                 }
    4695                 :          0 :         }
    4696                 :            : 
    4697   [ #  #  #  #  :         56 :         opts->arbitration_burst = (uint8_t)g_opts.arbitration_burst;
                   #  # ]
    4698   [ #  #  #  #  :         56 :         opts->low_priority_weight = (uint8_t)g_opts.low_priority_weight;
                   #  # ]
    4699   [ #  #  #  #  :         56 :         opts->medium_priority_weight = (uint8_t)g_opts.medium_priority_weight;
                   #  # ]
    4700   [ #  #  #  #  :         56 :         opts->high_priority_weight = (uint8_t)g_opts.high_priority_weight;
                   #  # ]
    4701   [ #  #  #  # ]:         56 :         opts->disable_read_ana_log_page = true;
    4702                 :            : 
    4703   [ -  +  -  +  :         56 :         SPDK_DEBUGLOG(bdev_nvme, "Attaching to %s\n", trid->traddr);
             #  #  #  # ]
    4704                 :            : 
    4705                 :         56 :         return true;
    4706                 :          0 : }
    4707                 :            : 
    4708                 :            : static void
    4709                 :          0 : nvme_abort_cpl(void *ctx, const struct spdk_nvme_cpl *cpl)
    4710                 :            : {
    4711                 :          0 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    4712                 :            : 
    4713   [ #  #  #  #  :          0 :         if (spdk_nvme_cpl_is_error(cpl)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    4714   [ #  #  #  #  :          0 :                 NVME_CTRLR_WARNLOG(nvme_ctrlr, "Abort failed. Resetting controller. sc is %u, sct is %u.\n",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    4715                 :            :                                    cpl->status.sc, cpl->status.sct);
    4716                 :          0 :                 bdev_nvme_reset_ctrlr(nvme_ctrlr);
    4717   [ #  #  #  #  :          0 :         } else if (cpl->cdw0 & 0x1) {
                   #  # ]
    4718   [ #  #  #  #  :          0 :                 NVME_CTRLR_WARNLOG(nvme_ctrlr, "Specified command could not be aborted.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    4719                 :          0 :                 bdev_nvme_reset_ctrlr(nvme_ctrlr);
    4720                 :          0 :         }
    4721                 :          0 : }
    4722                 :            : 
    4723                 :            : static void
    4724                 :          0 : timeout_cb(void *cb_arg, struct spdk_nvme_ctrlr *ctrlr,
    4725                 :            :            struct spdk_nvme_qpair *qpair, uint16_t cid)
    4726                 :            : {
    4727                 :          0 :         struct nvme_ctrlr *nvme_ctrlr = cb_arg;
    4728                 :            :         union spdk_nvme_csts_register csts;
    4729                 :            :         int rc;
    4730                 :            : 
    4731   [ #  #  #  #  :          0 :         assert(nvme_ctrlr->ctrlr == ctrlr);
             #  #  #  # ]
    4732                 :            : 
    4733   [ #  #  #  #  :          0 :         NVME_CTRLR_WARNLOG(nvme_ctrlr, "Warning: Detected a timeout. ctrlr=%p qpair=%p cid=%u\n",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    4734                 :            :                            ctrlr, qpair, cid);
    4735                 :            : 
    4736                 :            :         /* Only try to read CSTS if it's a PCIe controller or we have a timeout on an I/O
    4737                 :            :          * queue.  (Note: qpair == NULL when there's an admin cmd timeout.)  Otherwise we
    4738                 :            :          * would submit another fabrics cmd on the admin queue to read CSTS and check for its
    4739                 :            :          * completion recursively.
    4740                 :            :          */
    4741   [ #  #  #  #  :          0 :         if (nvme_ctrlr->active_path_id->trid.trtype == SPDK_NVME_TRANSPORT_PCIE || qpair != NULL) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    4742                 :          0 :                 csts = spdk_nvme_ctrlr_get_regs_csts(ctrlr);
    4743         [ #  # ]:          0 :                 if (csts.bits.cfs) {
    4744   [ #  #  #  #  :          0 :                         NVME_CTRLR_ERRLOG(nvme_ctrlr, "Controller Fatal Status, reset required\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    4745                 :          0 :                         bdev_nvme_reset_ctrlr(nvme_ctrlr);
    4746                 :          0 :                         return;
    4747                 :            :                 }
    4748                 :          0 :         }
    4749                 :            : 
    4750   [ #  #  #  # ]:          0 :         switch (g_opts.action_on_timeout) {
    4751                 :          0 :         case SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT:
    4752         [ #  # ]:          0 :                 if (qpair) {
    4753                 :            :                         /* Don't send abort to ctrlr when ctrlr is not available. */
    4754   [ #  #  #  # ]:          0 :                         pthread_mutex_lock(&nvme_ctrlr->mutex);
    4755         [ #  # ]:          0 :                         if (!nvme_ctrlr_is_available(nvme_ctrlr)) {
    4756   [ #  #  #  # ]:          0 :                                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4757   [ #  #  #  #  :          0 :                                 NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Quit abort. Ctrlr is not available.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    4758                 :          0 :                                 return;
    4759                 :            :                         }
    4760   [ #  #  #  # ]:          0 :                         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4761                 :            : 
    4762                 :          0 :                         rc = spdk_nvme_ctrlr_cmd_abort(ctrlr, qpair, cid,
    4763                 :          0 :                                                        nvme_abort_cpl, nvme_ctrlr);
    4764         [ #  # ]:          0 :                         if (rc == 0) {
    4765                 :          0 :                                 return;
    4766                 :            :                         }
    4767                 :            : 
    4768   [ #  #  #  #  :          0 :                         NVME_CTRLR_ERRLOG(nvme_ctrlr, "Unable to send abort. Resetting, rc is %d.\n", rc);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    4769                 :          0 :                 }
    4770                 :            : 
    4771                 :            :         /* FALLTHROUGH */
    4772                 :            :         case SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET:
    4773                 :          0 :                 bdev_nvme_reset_ctrlr(nvme_ctrlr);
    4774                 :          0 :                 break;
    4775                 :          0 :         case SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE:
    4776   [ #  #  #  #  :          0 :                 NVME_CTRLR_DEBUGLOG(nvme_ctrlr, "No action for nvme controller timeout.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    4777                 :          0 :                 break;
    4778                 :          0 :         default:
    4779   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "An invalid timeout action value is found.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    4780                 :          0 :                 break;
    4781                 :            :         }
    4782                 :          0 : }
    4783                 :            : 
    4784                 :            : static struct nvme_ns *
    4785                 :       1008 : nvme_ns_alloc(void)
    4786                 :            : {
    4787                 :            :         struct nvme_ns *nvme_ns;
    4788                 :            : 
    4789                 :       1008 :         nvme_ns = calloc(1, sizeof(struct nvme_ns));
    4790         [ +  + ]:       1008 :         if (nvme_ns == NULL) {
    4791                 :          0 :                 return NULL;
    4792                 :            :         }
    4793                 :            : 
    4794   [ +  +  +  +  :       1008 :         if (g_opts.io_path_stat) {
                   +  - ]
    4795   [ #  #  #  # ]:          0 :                 nvme_ns->stat = calloc(1, sizeof(struct spdk_bdev_io_stat));
    4796   [ #  #  #  #  :          0 :                 if (nvme_ns->stat == NULL) {
                   #  # ]
    4797                 :          0 :                         free(nvme_ns);
    4798                 :          0 :                         return NULL;
    4799                 :            :                 }
    4800   [ #  #  #  # ]:          0 :                 spdk_bdev_reset_io_stat(nvme_ns->stat, SPDK_BDEV_RESET_STAT_MAXMIN);
    4801                 :          0 :         }
    4802                 :            : 
    4803                 :       1008 :         return nvme_ns;
    4804                 :         63 : }
    4805                 :            : 
    4806                 :            : static void
    4807                 :       1008 : nvme_ns_free(struct nvme_ns *nvme_ns)
    4808                 :            : {
    4809   [ +  -  +  - ]:       1008 :         free(nvme_ns->stat);
    4810                 :       1008 :         free(nvme_ns);
    4811                 :       1008 : }
    4812                 :            : 
    4813                 :            : static void
    4814                 :       1008 : nvme_ctrlr_populate_namespace_done(struct nvme_ns *nvme_ns, int rc)
    4815                 :            : {
    4816   [ +  -  +  - ]:       1008 :         struct nvme_ctrlr *nvme_ctrlr = nvme_ns->ctrlr;
    4817   [ +  -  +  - ]:       1008 :         struct nvme_async_probe_ctx *ctx = nvme_ns->probe_ctx;
    4818                 :            : 
    4819         [ +  + ]:       1008 :         if (rc == 0) {
    4820   [ -  +  -  + ]:       1000 :                 nvme_ns->probe_ctx = NULL;
    4821                 :       1000 :                 nvme_ctrlr_get_ref(nvme_ctrlr);
    4822                 :         61 :         } else {
    4823         [ #  # ]:          8 :                 RB_REMOVE(nvme_ns_tree, &nvme_ctrlr->namespaces, nvme_ns);
    4824                 :          8 :                 nvme_ns_free(nvme_ns);
    4825                 :            :         }
    4826                 :            : 
    4827         [ +  + ]:       1008 :         if (ctx) {
    4828         [ +  - ]:        947 :                 ctx->populates_in_progress--;
    4829   [ +  +  +  -  :        947 :                 if (ctx->populates_in_progress == 0) {
                   +  - ]
    4830                 :         51 :                         nvme_ctrlr_populate_namespaces_done(nvme_ctrlr, ctx);
    4831                 :         12 :                 }
    4832                 :         62 :         }
    4833                 :       1008 : }
    4834                 :            : 
    4835                 :            : static void
    4836                 :          8 : bdev_nvme_add_io_path(struct nvme_bdev_channel_iter *i,
    4837                 :            :                       struct nvme_bdev *nbdev,
    4838                 :            :                       struct nvme_bdev_channel *nbdev_ch, void *ctx)
    4839                 :            : {
    4840                 :          8 :         struct nvme_ns *nvme_ns = ctx;
    4841                 :            :         int rc;
    4842                 :            : 
    4843                 :          8 :         rc = _bdev_nvme_add_io_path(nbdev_ch, nvme_ns);
    4844         [ -  + ]:          8 :         if (rc != 0) {
    4845                 :          0 :                 SPDK_ERRLOG("Failed to add I/O path to bdev_channel dynamically.\n");
    4846                 :          0 :         }
    4847                 :            : 
    4848                 :          8 :         nvme_bdev_for_each_channel_continue(i, rc);
    4849                 :          8 : }
    4850                 :            : 
    4851                 :            : static void
    4852                 :          8 : bdev_nvme_delete_io_path(struct nvme_bdev_channel_iter *i,
    4853                 :            :                          struct nvme_bdev *nbdev,
    4854                 :            :                          struct nvme_bdev_channel *nbdev_ch, void *ctx)
    4855                 :            : {
    4856                 :          8 :         struct nvme_ns *nvme_ns = ctx;
    4857                 :            :         struct nvme_io_path *io_path;
    4858                 :            : 
    4859                 :          8 :         io_path = _bdev_nvme_get_io_path(nbdev_ch, nvme_ns);
    4860         [ +  - ]:          8 :         if (io_path != NULL) {
    4861                 :          8 :                 _bdev_nvme_delete_io_path(nbdev_ch, io_path);
    4862                 :          2 :         }
    4863                 :            : 
    4864                 :          8 :         nvme_bdev_for_each_channel_continue(i, 0);
    4865                 :          8 : }
    4866                 :            : 
    4867                 :            : static void
    4868                 :          0 : bdev_nvme_add_io_path_failed(struct nvme_bdev *nbdev, void *ctx, int status)
    4869                 :            : {
    4870                 :          0 :         struct nvme_ns *nvme_ns = ctx;
    4871                 :            : 
    4872                 :          0 :         nvme_ctrlr_populate_namespace_done(nvme_ns, -1);
    4873                 :          0 : }
    4874                 :            : 
    4875                 :            : static void
    4876                 :         52 : bdev_nvme_add_io_path_done(struct nvme_bdev *nbdev, void *ctx, int status)
    4877                 :            : {
    4878                 :         52 :         struct nvme_ns *nvme_ns = ctx;
    4879                 :            : 
    4880         [ +  - ]:         52 :         if (status == 0) {
    4881                 :         52 :                 nvme_ctrlr_populate_namespace_done(nvme_ns, 0);
    4882                 :         12 :         } else {
    4883                 :            :                 /* Delete the added io_paths and fail populating the namespace. */
    4884                 :          0 :                 nvme_bdev_for_each_channel(nbdev,
    4885                 :            :                                            bdev_nvme_delete_io_path,
    4886                 :          0 :                                            nvme_ns,
    4887                 :            :                                            bdev_nvme_add_io_path_failed);
    4888                 :            :         }
    4889                 :         52 : }
    4890                 :            : 
    4891                 :            : static int
    4892                 :         56 : nvme_bdev_add_ns(struct nvme_bdev *bdev, struct nvme_ns *nvme_ns)
    4893                 :            : {
    4894                 :            :         struct nvme_ns *tmp_ns;
    4895                 :            :         const struct spdk_nvme_ns_data *nsdata;
    4896                 :            : 
    4897   [ #  #  #  # ]:         56 :         nsdata = spdk_nvme_ns_get_data(nvme_ns->ns);
    4898   [ -  +  #  #  :         56 :         if (!nsdata->nmic.can_share) {
                   #  # ]
    4899                 :          0 :                 SPDK_ERRLOG("Namespace cannot be shared.\n");
    4900                 :          0 :                 return -EINVAL;
    4901                 :            :         }
    4902                 :            : 
    4903   [ -  +  #  # ]:         56 :         pthread_mutex_lock(&bdev->mutex);
    4904                 :            : 
    4905   [ #  #  #  #  :         56 :         tmp_ns = TAILQ_FIRST(&bdev->nvme_ns_list);
                   #  # ]
    4906   [ -  +  #  # ]:         56 :         assert(tmp_ns != NULL);
    4907                 :            : 
    4908   [ +  -  +  +  :         56 :         if (tmp_ns->ns != NULL && !bdev_nvme_compare_ns(nvme_ns->ns, tmp_ns->ns)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    4909   [ -  +  #  # ]:          4 :                 pthread_mutex_unlock(&bdev->mutex);
    4910                 :          4 :                 SPDK_ERRLOG("Namespaces are not identical.\n");
    4911                 :          4 :                 return -EINVAL;
    4912                 :            :         }
    4913                 :            : 
    4914   [ #  #  #  # ]:         52 :         bdev->ref++;
    4915   [ #  #  #  #  :         52 :         TAILQ_INSERT_TAIL(&bdev->nvme_ns_list, nvme_ns, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    4916   [ #  #  #  # ]:         52 :         nvme_ns->bdev = bdev;
    4917                 :            : 
    4918   [ -  +  #  # ]:         52 :         pthread_mutex_unlock(&bdev->mutex);
    4919                 :            : 
    4920                 :            :         /* Add nvme_io_path to nvme_bdev_channels dynamically. */
    4921                 :         64 :         nvme_bdev_for_each_channel(bdev,
    4922                 :            :                                    bdev_nvme_add_io_path,
    4923                 :         12 :                                    nvme_ns,
    4924                 :            :                                    bdev_nvme_add_io_path_done);
    4925                 :            : 
    4926                 :         52 :         return 0;
    4927                 :         13 : }
    4928                 :            : 
    4929                 :            : static void
    4930                 :       1008 : nvme_ctrlr_populate_namespace(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *nvme_ns)
    4931                 :            : {
    4932                 :            :         struct spdk_nvme_ns     *ns;
    4933                 :            :         struct nvme_bdev        *bdev;
    4934                 :       1008 :         int                     rc = 0;
    4935                 :            : 
    4936   [ +  -  +  -  :       1008 :         ns = spdk_nvme_ctrlr_get_ns(nvme_ctrlr->ctrlr, nvme_ns->id);
             +  -  +  - ]
    4937         [ -  + ]:       1008 :         if (!ns) {
    4938   [ #  #  #  #  :          0 :                 NVME_CTRLR_DEBUGLOG(nvme_ctrlr, "Invalid NS %d\n", nvme_ns->id);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    4939                 :          0 :                 rc = -EINVAL;
    4940                 :          0 :                 goto done;
    4941                 :            :         }
    4942                 :            : 
    4943   [ +  -  +  - ]:       1008 :         nvme_ns->ns = ns;
    4944   [ +  -  +  - ]:       1008 :         nvme_ns->ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
    4945                 :            : 
    4946   [ +  +  +  -  :       1008 :         if (nvme_ctrlr->ana_log_page != NULL) {
                   +  - ]
    4947                 :        234 :                 bdev_nvme_parse_ana_log_page(nvme_ctrlr, nvme_ns_set_ana_state, nvme_ns);
    4948                 :         38 :         }
    4949                 :            : 
    4950   [ +  -  +  -  :       1008 :         bdev = nvme_bdev_ctrlr_get_bdev(nvme_ctrlr->nbdev_ctrlr, nvme_ns->id);
             +  -  +  - ]
    4951         [ +  + ]:       1059 :         if (bdev == NULL) {
    4952                 :        952 :                 rc = nvme_bdev_create(nvme_ctrlr, nvme_ns);
    4953                 :         50 :         } else {
    4954                 :         56 :                 rc = nvme_bdev_add_ns(bdev, nvme_ns);
    4955         [ +  + ]:         56 :                 if (rc == 0) {
    4956                 :         52 :                         return;
    4957                 :            :                 }
    4958                 :            :         }
    4959                 :          3 : done:
    4960                 :        956 :         nvme_ctrlr_populate_namespace_done(nvme_ns, rc);
    4961                 :         63 : }
    4962                 :            : 
    4963                 :            : static void
    4964                 :       1000 : nvme_ctrlr_depopulate_namespace_done(struct nvme_ns *nvme_ns)
    4965                 :            : {
    4966   [ +  -  +  - ]:       1000 :         struct nvme_ctrlr *nvme_ctrlr = nvme_ns->ctrlr;
    4967                 :            : 
    4968   [ +  +  #  # ]:       1000 :         assert(nvme_ctrlr != NULL);
    4969                 :            : 
    4970   [ +  +  +  - ]:       1000 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    4971                 :            : 
    4972         [ +  - ]:       1000 :         RB_REMOVE(nvme_ns_tree, &nvme_ctrlr->namespaces, nvme_ns);
    4973                 :            : 
    4974   [ +  +  +  -  :       1000 :         if (nvme_ns->bdev != NULL) {
                   -  + ]
    4975   [ -  +  #  # ]:        214 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4976                 :        214 :                 return;
    4977                 :            :         }
    4978                 :            : 
    4979                 :        786 :         nvme_ns_free(nvme_ns);
    4980   [ +  +  +  - ]:        786 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4981                 :            : 
    4982                 :        786 :         nvme_ctrlr_put_ref(nvme_ctrlr);
    4983                 :         61 : }
    4984                 :            : 
    4985                 :            : static void
    4986                 :         46 : bdev_nvme_delete_io_path_done(struct nvme_bdev *nbdev, void *ctx, int status)
    4987                 :            : {
    4988                 :         46 :         struct nvme_ns *nvme_ns = ctx;
    4989                 :            : 
    4990                 :         46 :         nvme_ctrlr_depopulate_namespace_done(nvme_ns);
    4991                 :         46 : }
    4992                 :            : 
    4993                 :            : static void
    4994                 :       1000 : nvme_ctrlr_depopulate_namespace(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *nvme_ns)
    4995                 :            : {
    4996                 :            :         struct nvme_bdev *bdev;
    4997                 :            : 
    4998         [ +  - ]:       1000 :         spdk_poller_unregister(&nvme_ns->anatt_timer);
    4999                 :            : 
    5000   [ +  -  +  - ]:       1000 :         bdev = nvme_ns->bdev;
    5001         [ +  + ]:       1000 :         if (bdev != NULL) {
    5002   [ -  +  #  # ]:        400 :                 pthread_mutex_lock(&bdev->mutex);
    5003                 :            : 
    5004   [ +  +  #  #  :        400 :                 assert(bdev->ref > 0);
             #  #  #  # ]
    5005   [ #  #  #  # ]:        400 :                 bdev->ref--;
    5006   [ +  +  #  #  :        400 :                 if (bdev->ref == 0) {
                   #  # ]
    5007   [ -  +  #  # ]:        354 :                         pthread_mutex_unlock(&bdev->mutex);
    5008                 :            : 
    5009         [ #  # ]:        354 :                         spdk_bdev_unregister(&bdev->disk, NULL, NULL);
    5010                 :         38 :                 } else {
    5011                 :            :                         /* spdk_bdev_unregister() is not called until the last nvme_ns is
    5012                 :            :                          * depopulated. Hence we need to remove nvme_ns from bdev->nvme_ns_list
    5013                 :            :                          * and clear nvme_ns->bdev here.
    5014                 :            :                          */
    5015   [ +  +  #  #  :         46 :                         TAILQ_REMOVE(&bdev->nvme_ns_list, nvme_ns, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5016   [ #  #  #  # ]:         46 :                         nvme_ns->bdev = NULL;
    5017                 :            : 
    5018   [ -  +  #  # ]:         46 :                         pthread_mutex_unlock(&bdev->mutex);
    5019                 :            : 
    5020                 :            :                         /* Delete nvme_io_paths from nvme_bdev_channels dynamically. After that,
    5021                 :            :                          * we call depopulate_namespace_done() to avoid use-after-free.
    5022                 :            :                          */
    5023                 :         57 :                         nvme_bdev_for_each_channel(bdev,
    5024                 :            :                                                    bdev_nvme_delete_io_path,
    5025                 :         11 :                                                    nvme_ns,
    5026                 :            :                                                    bdev_nvme_delete_io_path_done);
    5027                 :         46 :                         return;
    5028                 :            :                 }
    5029                 :         38 :         }
    5030                 :            : 
    5031                 :        954 :         nvme_ctrlr_depopulate_namespace_done(nvme_ns);
    5032                 :         61 : }
    5033                 :            : 
    5034                 :            : static void
    5035                 :       1035 : nvme_ctrlr_populate_namespaces(struct nvme_ctrlr *nvme_ctrlr,
    5036                 :            :                                struct nvme_async_probe_ctx *ctx)
    5037                 :            : {
    5038   [ +  -  +  - ]:       1035 :         struct spdk_nvme_ctrlr  *ctrlr = nvme_ctrlr->ctrlr;
    5039                 :            :         struct nvme_ns  *nvme_ns, *next;
    5040                 :            :         struct spdk_nvme_ns     *ns;
    5041                 :            :         struct nvme_bdev        *bdev;
    5042                 :            :         uint32_t                nsid;
    5043                 :            :         int                     rc;
    5044                 :            :         uint64_t                num_sectors;
    5045                 :            : 
    5046         [ +  + ]:       1035 :         if (ctx) {
    5047                 :            :                 /* Initialize this count to 1 to handle the populate functions
    5048                 :            :                  * calling nvme_ctrlr_populate_namespace_done() immediately.
    5049                 :            :                  */
    5050   [ +  -  +  - ]:        911 :                 ctx->populates_in_progress = 1;
    5051                 :         58 :         }
    5052                 :            : 
    5053                 :            :         /* First loop over our existing namespaces and see if they have been
    5054                 :            :          * removed. */
    5055                 :       1035 :         nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
    5056         [ +  + ]:       1056 :         while (nvme_ns != NULL) {
    5057                 :         21 :                 next = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns);
    5058                 :            : 
    5059   [ +  +  #  #  :         21 :                 if (spdk_nvme_ctrlr_is_active_ns(ctrlr, nvme_ns->id)) {
                   #  # ]
    5060                 :            :                         /* NS is still there or added again. Its attributes may have changed. */
    5061   [ #  #  #  # ]:         14 :                         ns = spdk_nvme_ctrlr_get_ns(ctrlr, nvme_ns->id);
    5062   [ +  +  #  #  :         14 :                         if (nvme_ns->ns != ns) {
                   #  # ]
    5063   [ +  +  #  #  :          4 :                                 assert(nvme_ns->ns == NULL);
             #  #  #  # ]
    5064   [ #  #  #  # ]:          4 :                                 nvme_ns->ns = ns;
    5065   [ +  +  -  +  :          4 :                                 NVME_CTRLR_DEBUGLOG(nvme_ctrlr, "NSID %u was added\n", nvme_ns->id);
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5066                 :          1 :                         }
    5067                 :            : 
    5068                 :         14 :                         num_sectors = spdk_nvme_ns_get_num_sectors(ns);
    5069   [ #  #  #  # ]:         14 :                         bdev = nvme_ns->bdev;
    5070   [ +  +  #  # ]:         14 :                         assert(bdev != NULL);
    5071   [ +  +  #  #  :         14 :                         if (bdev->disk.blockcnt != num_sectors) {
             #  #  #  # ]
    5072   [ +  -  #  #  :          4 :                                 NVME_CTRLR_NOTICELOG(nvme_ctrlr,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    5073                 :            :                                                      "NSID %u is resized: bdev name %s, old size %" PRIu64 ", new size %" PRIu64 "\n",
    5074                 :            :                                                      nvme_ns->id,
    5075                 :            :                                                      bdev->disk.name,
    5076                 :            :                                                      bdev->disk.blockcnt,
    5077                 :            :                                                      num_sectors);
    5078         [ #  # ]:          4 :                                 rc = spdk_bdev_notify_blockcnt_change(&bdev->disk, num_sectors);
    5079         [ +  + ]:          4 :                                 if (rc != 0) {
    5080   [ #  #  #  #  :          0 :                                         NVME_CTRLR_ERRLOG(nvme_ctrlr,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    5081                 :            :                                                           "Could not change num blocks for nvme bdev: name %s, errno: %d.\n",
    5082                 :            :                                                           bdev->disk.name, rc);
    5083                 :          0 :                                 }
    5084                 :          1 :                         }
    5085                 :          3 :                 } else {
    5086                 :            :                         /* Namespace was removed */
    5087                 :          7 :                         nvme_ctrlr_depopulate_namespace(nvme_ctrlr, nvme_ns);
    5088                 :            :                 }
    5089                 :            : 
    5090                 :         21 :                 nvme_ns = next;
    5091                 :            :         }
    5092                 :            : 
    5093                 :            :         /* Loop through all of the namespaces at the nvme level and see if any of them are new */
    5094                 :       1035 :         nsid = spdk_nvme_ctrlr_get_first_active_ns(ctrlr);
    5095         [ +  + ]:       2057 :         while (nsid != 0) {
    5096                 :       1022 :                 nvme_ns = nvme_ctrlr_get_ns(nvme_ctrlr, nsid);
    5097                 :            : 
    5098         [ +  + ]:       1022 :                 if (nvme_ns == NULL) {
    5099                 :            :                         /* Found a new one */
    5100                 :       1008 :                         nvme_ns = nvme_ns_alloc();
    5101         [ +  + ]:       1008 :                         if (nvme_ns == NULL) {
    5102   [ #  #  #  #  :          0 :                                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to allocate namespace\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5103                 :            :                                 /* This just fails to attach the namespace. It may work on a future attempt. */
    5104                 :          0 :                                 continue;
    5105                 :            :                         }
    5106                 :            : 
    5107   [ +  -  +  - ]:       1008 :                         nvme_ns->id = nsid;
    5108   [ +  -  +  - ]:       1008 :                         nvme_ns->ctrlr = nvme_ctrlr;
    5109                 :            : 
    5110   [ +  -  +  - ]:       1008 :                         nvme_ns->bdev = NULL;
    5111                 :            : 
    5112         [ +  + ]:       1008 :                         if (ctx) {
    5113         [ +  - ]:        947 :                                 ctx->populates_in_progress++;
    5114                 :         62 :                         }
    5115   [ +  -  +  - ]:       1008 :                         nvme_ns->probe_ctx = ctx;
    5116                 :            : 
    5117         [ +  - ]:       1008 :                         RB_INSERT(nvme_ns_tree, &nvme_ctrlr->namespaces, nvme_ns);
    5118                 :            : 
    5119                 :       1008 :                         nvme_ctrlr_populate_namespace(nvme_ctrlr, nvme_ns);
    5120                 :         63 :                 }
    5121                 :            : 
    5122                 :       1022 :                 nsid = spdk_nvme_ctrlr_get_next_active_ns(ctrlr, nsid);
    5123                 :            :         }
    5124                 :            : 
    5125         [ +  + ]:       1035 :         if (ctx) {
    5126                 :            :                 /* Decrement this count now that the loop is over to account
    5127                 :            :                  * for the one we started with.  If the count is then 0, we
    5128                 :            :                  * know any populate_namespace functions completed immediately,
    5129                 :            :                  * so we'll kick the callback here.
    5130                 :            :                  */
    5131         [ +  - ]:        911 :                 ctx->populates_in_progress--;
    5132   [ +  +  +  -  :        911 :                 if (ctx->populates_in_progress == 0) {
                   -  + ]
    5133                 :        860 :                         nvme_ctrlr_populate_namespaces_done(nvme_ctrlr, ctx);
    5134                 :         46 :                 }
    5135                 :         58 :         }
    5136                 :            : 
    5137                 :       1035 : }
    5138                 :            : 
    5139                 :            : static void
    5140                 :       1027 : nvme_ctrlr_depopulate_namespaces(struct nvme_ctrlr *nvme_ctrlr)
    5141                 :            : {
    5142                 :            :         struct nvme_ns *nvme_ns, *tmp;
    5143                 :            : 
    5144   [ +  +  +  +  :       2020 :         RB_FOREACH_SAFE(nvme_ns, nvme_ns_tree, &nvme_ctrlr->namespaces, tmp) {
                   +  + ]
    5145                 :        993 :                 nvme_ctrlr_depopulate_namespace(nvme_ctrlr, nvme_ns);
    5146                 :         60 :         }
    5147                 :       1027 : }
    5148                 :            : 
    5149                 :            : static uint32_t
    5150                 :        887 : nvme_ctrlr_get_ana_log_page_size(struct nvme_ctrlr *nvme_ctrlr)
    5151                 :            : {
    5152   [ #  #  #  # ]:        887 :         struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
    5153                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    5154                 :        887 :         uint32_t nsid, ns_count = 0;
    5155                 :            : 
    5156                 :        887 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    5157                 :            : 
    5158         [ +  + ]:        932 :         for (nsid = spdk_nvme_ctrlr_get_first_active_ns(ctrlr);
    5159         [ +  + ]:       1806 :              nsid != 0; nsid = spdk_nvme_ctrlr_get_next_active_ns(ctrlr, nsid)) {
    5160                 :        919 :                 ns_count++;
    5161                 :         45 :         }
    5162                 :            : 
    5163   [ #  #  #  # ]:        924 :         return sizeof(struct spdk_nvme_ana_page) + cdata->nanagrpid *
    5164                 :        887 :                sizeof(struct spdk_nvme_ana_group_descriptor) + ns_count *
    5165                 :            :                sizeof(uint32_t);
    5166                 :            : }
    5167                 :            : 
    5168                 :            : static int
    5169                 :         58 : nvme_ctrlr_set_ana_states(const struct spdk_nvme_ana_group_descriptor *desc,
    5170                 :            :                           void *cb_arg)
    5171                 :            : {
    5172                 :         58 :         struct nvme_ctrlr *nvme_ctrlr = cb_arg;
    5173                 :            :         struct nvme_ns *nvme_ns;
    5174                 :            :         uint32_t i, nsid;
    5175                 :            : 
    5176   [ +  +  #  #  :        112 :         for (i = 0; i < desc->num_of_nsid; i++) {
                   #  # ]
    5177   [ #  #  #  #  :         54 :                 nsid = desc->nsid[i];
                   #  # ]
    5178         [ +  + ]:         54 :                 if (nsid == 0) {
    5179                 :          0 :                         continue;
    5180                 :            :                 }
    5181                 :            : 
    5182                 :         54 :                 nvme_ns = nvme_ctrlr_get_ns(nvme_ctrlr, nsid);
    5183                 :            : 
    5184         [ +  + ]:         54 :                 if (nvme_ns == NULL) {
    5185                 :            :                         /* Target told us that an inactive namespace had an ANA change */
    5186                 :          4 :                         continue;
    5187                 :            :                 }
    5188                 :            : 
    5189                 :         50 :                 _nvme_ns_set_ana_state(nvme_ns, desc);
    5190                 :          5 :         }
    5191                 :            : 
    5192                 :         58 :         return 0;
    5193                 :            : }
    5194                 :            : 
    5195                 :            : static void
    5196                 :          1 : bdev_nvme_disable_read_ana_log_page(struct nvme_ctrlr *nvme_ctrlr)
    5197                 :            : {
    5198                 :            :         struct nvme_ns *nvme_ns;
    5199                 :            : 
    5200   [ #  #  #  # ]:          1 :         spdk_free(nvme_ctrlr->ana_log_page);
    5201   [ #  #  #  # ]:          1 :         nvme_ctrlr->ana_log_page = NULL;
    5202                 :            : 
    5203         [ #  # ]:          1 :         for (nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
    5204         [ +  + ]:          2 :              nvme_ns != NULL;
    5205                 :          1 :              nvme_ns = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns)) {
    5206   [ #  #  #  # ]:          1 :                 nvme_ns->ana_state_updating = false;
    5207   [ #  #  #  # ]:          1 :                 nvme_ns->ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
    5208                 :          0 :         }
    5209                 :          1 : }
    5210                 :            : 
    5211                 :            : static void
    5212                 :         43 : nvme_ctrlr_read_ana_log_page_done(void *ctx, const struct spdk_nvme_cpl *cpl)
    5213                 :            : {
    5214                 :         43 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    5215                 :            : 
    5216   [ +  -  +  +  :         43 :         if (cpl != NULL && spdk_nvme_cpl_is_success(cpl)) {
          +  +  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    5217                 :         45 :                 bdev_nvme_parse_ana_log_page(nvme_ctrlr, nvme_ctrlr_set_ana_states,
    5218                 :          3 :                                              nvme_ctrlr);
    5219                 :          3 :         } else {
    5220                 :          1 :                 bdev_nvme_disable_read_ana_log_page(nvme_ctrlr);
    5221                 :            :         }
    5222                 :            : 
    5223   [ -  +  #  # ]:         43 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    5224                 :            : 
    5225   [ +  +  #  #  :         43 :         assert(nvme_ctrlr->ana_log_page_updating == true);
                   #  # ]
    5226         [ #  # ]:         43 :         nvme_ctrlr->ana_log_page_updating = false;
    5227                 :            : 
    5228         [ -  + ]:         43 :         if (nvme_ctrlr_can_be_unregistered(nvme_ctrlr)) {
    5229   [ #  #  #  # ]:          0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    5230                 :            : 
    5231                 :          0 :                 nvme_ctrlr_unregister(nvme_ctrlr);
    5232                 :          0 :         } else {
    5233   [ -  +  #  # ]:         43 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    5234                 :            : 
    5235                 :         43 :                 bdev_nvme_clear_io_path_caches(nvme_ctrlr);
    5236                 :            :         }
    5237                 :         43 : }
    5238                 :            : 
    5239                 :            : static int
    5240                 :        681 : nvme_ctrlr_read_ana_log_page(struct nvme_ctrlr *nvme_ctrlr)
    5241                 :            : {
    5242                 :            :         uint32_t ana_log_page_size;
    5243                 :            :         int rc;
    5244                 :            : 
    5245   [ +  +  #  #  :        681 :         if (nvme_ctrlr->ana_log_page == NULL) {
                   #  # ]
    5246                 :          0 :                 return -EINVAL;
    5247                 :            :         }
    5248                 :            : 
    5249                 :        681 :         ana_log_page_size = nvme_ctrlr_get_ana_log_page_size(nvme_ctrlr);
    5250                 :            : 
    5251   [ -  +  #  #  :        681 :         if (ana_log_page_size > nvme_ctrlr->max_ana_log_page_size) {
                   #  # ]
    5252   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    5253                 :            :                                   "ANA log page size %" PRIu32 " is larger than allowed %" PRIu32 "\n",
    5254                 :            :                                   ana_log_page_size, nvme_ctrlr->max_ana_log_page_size);
    5255                 :          0 :                 return -EINVAL;
    5256                 :            :         }
    5257                 :            : 
    5258   [ -  +  #  # ]:        681 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    5259   [ +  +  +  + ]:        681 :         if (!nvme_ctrlr_is_available(nvme_ctrlr) ||
    5260         [ #  # ]:          5 :             nvme_ctrlr->ana_log_page_updating) {
    5261   [ -  +  #  # ]:        638 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    5262                 :        638 :                 return -EBUSY;
    5263                 :            :         }
    5264                 :            : 
    5265         [ #  # ]:         43 :         nvme_ctrlr->ana_log_page_updating = true;
    5266   [ -  +  #  # ]:         43 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    5267                 :            : 
    5268   [ #  #  #  # ]:         46 :         rc = spdk_nvme_ctrlr_cmd_get_log_page(nvme_ctrlr->ctrlr,
    5269                 :            :                                               SPDK_NVME_LOG_ASYMMETRIC_NAMESPACE_ACCESS,
    5270                 :            :                                               SPDK_NVME_GLOBAL_NS_TAG,
    5271   [ #  #  #  # ]:         43 :                                               nvme_ctrlr->ana_log_page,
    5272                 :          3 :                                               ana_log_page_size, 0,
    5273                 :            :                                               nvme_ctrlr_read_ana_log_page_done,
    5274                 :          3 :                                               nvme_ctrlr);
    5275         [ -  + ]:         43 :         if (rc != 0) {
    5276                 :          0 :                 nvme_ctrlr_read_ana_log_page_done(nvme_ctrlr, NULL);
    5277                 :          0 :         }
    5278                 :            : 
    5279                 :         43 :         return rc;
    5280                 :          6 : }
    5281                 :            : 
    5282                 :            : static void
    5283                 :          0 : dummy_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *ctx)
    5284                 :            : {
    5285                 :          0 : }
    5286                 :            : 
    5287                 :            : struct bdev_nvme_set_preferred_path_ctx {
    5288                 :            :         struct spdk_bdev_desc *desc;
    5289                 :            :         struct nvme_ns *nvme_ns;
    5290                 :            :         bdev_nvme_set_preferred_path_cb cb_fn;
    5291                 :            :         void *cb_arg;
    5292                 :            : };
    5293                 :            : 
    5294                 :            : static void
    5295                 :         12 : bdev_nvme_set_preferred_path_done(struct nvme_bdev *nbdev, void *_ctx, int status)
    5296                 :            : {
    5297                 :         12 :         struct bdev_nvme_set_preferred_path_ctx *ctx = _ctx;
    5298                 :            : 
    5299   [ +  +  #  # ]:         12 :         assert(ctx != NULL);
    5300   [ +  +  #  #  :         12 :         assert(ctx->desc != NULL);
             #  #  #  # ]
    5301   [ +  +  #  #  :         12 :         assert(ctx->cb_fn != NULL);
             #  #  #  # ]
    5302                 :            : 
    5303   [ #  #  #  # ]:         12 :         spdk_bdev_close(ctx->desc);
    5304                 :            : 
    5305   [ #  #  #  #  :         12 :         ctx->cb_fn(ctx->cb_arg, status);
          #  #  #  #  #  
                #  #  # ]
    5306                 :            : 
    5307                 :         12 :         free(ctx);
    5308                 :         12 : }
    5309                 :            : 
    5310                 :            : static void
    5311                 :          8 : _bdev_nvme_set_preferred_path(struct nvme_bdev_channel_iter *i,
    5312                 :            :                               struct nvme_bdev *nbdev,
    5313                 :            :                               struct nvme_bdev_channel *nbdev_ch, void *_ctx)
    5314                 :            : {
    5315                 :          8 :         struct bdev_nvme_set_preferred_path_ctx *ctx = _ctx;
    5316                 :            :         struct nvme_io_path *io_path, *prev;
    5317                 :            : 
    5318                 :          8 :         prev = NULL;
    5319   [ +  +  #  #  :         12 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    5320   [ +  +  #  #  :         12 :                 if (io_path->nvme_ns == ctx->nvme_ns) {
          #  #  #  #  #  
                      # ]
    5321                 :          8 :                         break;
    5322                 :            :                 }
    5323                 :          4 :                 prev = io_path;
    5324                 :          1 :         }
    5325                 :            : 
    5326         [ +  - ]:          8 :         if (io_path != NULL) {
    5327         [ +  + ]:          8 :                 if (prev != NULL) {
    5328   [ -  +  #  #  :          4 :                         STAILQ_REMOVE_AFTER(&nbdev_ch->io_path_list, prev, stailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    5329   [ -  +  #  #  :          4 :                         STAILQ_INSERT_HEAD(&nbdev_ch->io_path_list, io_path, stailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    5330                 :          1 :                 }
    5331                 :            : 
    5332                 :            :                 /* We can set io_path to nbdev_ch->current_io_path directly here.
    5333                 :            :                  * However, it needs to be conditional. To simplify the code,
    5334                 :            :                  * just clear nbdev_ch->current_io_path and let find_io_path()
    5335                 :            :                  * fill it.
    5336                 :            :                  *
    5337                 :            :                  * Automatic failback may be disabled. Hence even if the io_path is
    5338                 :            :                  * already at the head, clear nbdev_ch->current_io_path.
    5339                 :            :                  */
    5340                 :          8 :                 bdev_nvme_clear_current_io_path(nbdev_ch);
    5341                 :          2 :         }
    5342                 :            : 
    5343                 :          8 :         nvme_bdev_for_each_channel_continue(i, 0);
    5344                 :          8 : }
    5345                 :            : 
    5346                 :            : static struct nvme_ns *
    5347                 :         12 : bdev_nvme_set_preferred_ns(struct nvme_bdev *nbdev, uint16_t cntlid)
    5348                 :            : {
    5349                 :            :         struct nvme_ns *nvme_ns, *prev;
    5350                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    5351                 :            : 
    5352                 :         12 :         prev = NULL;
    5353   [ +  +  #  #  :         24 :         TAILQ_FOREACH(nvme_ns, &nbdev->nvme_ns_list, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    5354   [ #  #  #  #  :         24 :                 cdata = spdk_nvme_ctrlr_get_data(nvme_ns->ctrlr->ctrlr);
             #  #  #  # ]
    5355                 :            : 
    5356   [ +  +  #  #  :         24 :                 if (cdata->cntlid == cntlid) {
                   #  # ]
    5357                 :         12 :                         break;
    5358                 :            :                 }
    5359                 :         12 :                 prev = nvme_ns;
    5360                 :          3 :         }
    5361                 :            : 
    5362   [ +  -  +  + ]:         12 :         if (nvme_ns != NULL && prev != NULL) {
    5363   [ +  +  #  #  :          8 :                 TAILQ_REMOVE(&nbdev->nvme_ns_list, nvme_ns, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5364   [ +  +  #  #  :          8 :                 TAILQ_INSERT_HEAD(&nbdev->nvme_ns_list, nvme_ns, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    5365                 :          2 :         }
    5366                 :            : 
    5367                 :         12 :         return nvme_ns;
    5368                 :            : }
    5369                 :            : 
    5370                 :            : /* This function supports only multipath mode. There is only a single I/O path
    5371                 :            :  * for each NVMe-oF controller. Hence, just move the matched I/O path to the
    5372                 :            :  * head of the I/O path list for each NVMe bdev channel.
    5373                 :            :  *
    5374                 :            :  * NVMe bdev channel may be acquired after completing this function. move the
    5375                 :            :  * matched namespace to the head of the namespace list for the NVMe bdev too.
    5376                 :            :  */
    5377                 :            : void
    5378                 :         12 : bdev_nvme_set_preferred_path(const char *name, uint16_t cntlid,
    5379                 :            :                              bdev_nvme_set_preferred_path_cb cb_fn, void *cb_arg)
    5380                 :            : {
    5381                 :            :         struct bdev_nvme_set_preferred_path_ctx *ctx;
    5382                 :            :         struct spdk_bdev *bdev;
    5383                 :            :         struct nvme_bdev *nbdev;
    5384                 :         12 :         int rc = 0;
    5385                 :            : 
    5386   [ +  +  #  # ]:         12 :         assert(cb_fn != NULL);
    5387                 :            : 
    5388                 :         12 :         ctx = calloc(1, sizeof(*ctx));
    5389         [ +  + ]:         12 :         if (ctx == NULL) {
    5390                 :          0 :                 SPDK_ERRLOG("Failed to alloc context.\n");
    5391                 :          0 :                 rc = -ENOMEM;
    5392                 :          0 :                 goto err_alloc;
    5393                 :            :         }
    5394                 :            : 
    5395   [ #  #  #  # ]:         12 :         ctx->cb_fn = cb_fn;
    5396   [ #  #  #  # ]:         12 :         ctx->cb_arg = cb_arg;
    5397                 :            : 
    5398         [ #  # ]:         12 :         rc = spdk_bdev_open_ext(name, false, dummy_bdev_event_cb, NULL, &ctx->desc);
    5399         [ -  + ]:         12 :         if (rc != 0) {
    5400                 :          0 :                 SPDK_ERRLOG("Failed to open bdev %s.\n", name);
    5401                 :          0 :                 goto err_open;
    5402                 :            :         }
    5403                 :            : 
    5404   [ #  #  #  # ]:         12 :         bdev = spdk_bdev_desc_get_bdev(ctx->desc);
    5405                 :            : 
    5406   [ -  +  #  #  :         12 :         if (bdev->module != &nvme_if) {
                   #  # ]
    5407                 :          0 :                 SPDK_ERRLOG("bdev %s is not registered in this module.\n", name);
    5408                 :          0 :                 rc = -ENODEV;
    5409                 :          0 :                 goto err_bdev;
    5410                 :            :         }
    5411                 :            : 
    5412                 :         12 :         nbdev = SPDK_CONTAINEROF(bdev, struct nvme_bdev, disk);
    5413                 :            : 
    5414   [ -  +  #  # ]:         12 :         pthread_mutex_lock(&nbdev->mutex);
    5415                 :            : 
    5416   [ #  #  #  # ]:         12 :         ctx->nvme_ns = bdev_nvme_set_preferred_ns(nbdev, cntlid);
    5417   [ +  +  #  #  :         12 :         if (ctx->nvme_ns == NULL) {
                   #  # ]
    5418   [ #  #  #  # ]:          0 :                 pthread_mutex_unlock(&nbdev->mutex);
    5419                 :            : 
    5420                 :          0 :                 SPDK_ERRLOG("bdev %s does not have namespace to controller %u.\n", name, cntlid);
    5421                 :          0 :                 rc = -ENODEV;
    5422                 :          0 :                 goto err_bdev;
    5423                 :            :         }
    5424                 :            : 
    5425   [ -  +  #  # ]:         12 :         pthread_mutex_unlock(&nbdev->mutex);
    5426                 :            : 
    5427                 :         15 :         nvme_bdev_for_each_channel(nbdev,
    5428                 :            :                                    _bdev_nvme_set_preferred_path,
    5429                 :          3 :                                    ctx,
    5430                 :            :                                    bdev_nvme_set_preferred_path_done);
    5431                 :         12 :         return;
    5432                 :            : 
    5433                 :          0 : err_bdev:
    5434   [ #  #  #  # ]:          0 :         spdk_bdev_close(ctx->desc);
    5435                 :          0 : err_open:
    5436                 :          0 :         free(ctx);
    5437                 :          0 : err_alloc:
    5438   [ #  #  #  # ]:          0 :         cb_fn(cb_arg, rc);
    5439                 :          3 : }
    5440                 :            : 
    5441                 :            : struct bdev_nvme_set_multipath_policy_ctx {
    5442                 :            :         struct spdk_bdev_desc *desc;
    5443                 :            :         spdk_bdev_nvme_set_multipath_policy_cb cb_fn;
    5444                 :            :         void *cb_arg;
    5445                 :            : };
    5446                 :            : 
    5447                 :            : static void
    5448                 :         13 : bdev_nvme_set_multipath_policy_done(struct nvme_bdev *nbdev, void *_ctx, int status)
    5449                 :            : {
    5450                 :         13 :         struct bdev_nvme_set_multipath_policy_ctx *ctx = _ctx;
    5451                 :            : 
    5452   [ +  +  #  # ]:         13 :         assert(ctx != NULL);
    5453   [ +  +  #  #  :         13 :         assert(ctx->desc != NULL);
             #  #  #  # ]
    5454   [ +  +  #  #  :         13 :         assert(ctx->cb_fn != NULL);
             #  #  #  # ]
    5455                 :            : 
    5456   [ #  #  #  # ]:         13 :         spdk_bdev_close(ctx->desc);
    5457                 :            : 
    5458   [ #  #  #  #  :         13 :         ctx->cb_fn(ctx->cb_arg, status);
          #  #  #  #  #  
                #  #  # ]
    5459                 :            : 
    5460                 :         13 :         free(ctx);
    5461                 :         13 : }
    5462                 :            : 
    5463                 :            : static void
    5464                 :          5 : _bdev_nvme_set_multipath_policy(struct nvme_bdev_channel_iter *i,
    5465                 :            :                                 struct nvme_bdev *nbdev,
    5466                 :            :                                 struct nvme_bdev_channel *nbdev_ch, void *ctx)
    5467                 :            : {
    5468   [ #  #  #  #  :          5 :         nbdev_ch->mp_policy = nbdev->mp_policy;
             #  #  #  # ]
    5469   [ #  #  #  #  :          5 :         nbdev_ch->mp_selector = nbdev->mp_selector;
             #  #  #  # ]
    5470   [ #  #  #  #  :          5 :         nbdev_ch->rr_min_io = nbdev->rr_min_io;
             #  #  #  # ]
    5471                 :          5 :         bdev_nvme_clear_current_io_path(nbdev_ch);
    5472                 :            : 
    5473                 :          5 :         nvme_bdev_for_each_channel_continue(i, 0);
    5474                 :          5 : }
    5475                 :            : 
    5476                 :            : void
    5477                 :         13 : spdk_bdev_nvme_set_multipath_policy(const char *name, enum spdk_bdev_nvme_multipath_policy policy,
    5478                 :            :                                     enum spdk_bdev_nvme_multipath_selector selector, uint32_t rr_min_io,
    5479                 :            :                                     spdk_bdev_nvme_set_multipath_policy_cb cb_fn, void *cb_arg)
    5480                 :            : {
    5481                 :            :         struct bdev_nvme_set_multipath_policy_ctx *ctx;
    5482                 :            :         struct spdk_bdev *bdev;
    5483                 :            :         struct nvme_bdev *nbdev;
    5484                 :            :         int rc;
    5485                 :            : 
    5486   [ +  +  #  # ]:         13 :         assert(cb_fn != NULL);
    5487                 :            : 
    5488      [ +  +  + ]:         13 :         switch (policy) {
    5489                 :          3 :         case BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE:
    5490                 :          4 :                 break;
    5491      [ +  +  - ]:          7 :         case BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE:
    5492      [ +  +  + ]:          3 :                 switch (selector) {
    5493                 :          4 :                 case BDEV_NVME_MP_SELECTOR_ROUND_ROBIN:
    5494         [ +  + ]:          5 :                         if (rr_min_io == UINT32_MAX) {
    5495                 :          1 :                                 rr_min_io = 1;
    5496         [ -  + ]:          4 :                         } else if (rr_min_io == 0) {
    5497                 :          0 :                                 rc = -EINVAL;
    5498                 :          0 :                                 goto exit;
    5499                 :            :                         }
    5500                 :          5 :                         break;
    5501                 :          3 :                 case BDEV_NVME_MP_SELECTOR_QUEUE_DEPTH:
    5502                 :          4 :                         break;
    5503                 :          0 :                 default:
    5504                 :          0 :                         rc = -EINVAL;
    5505                 :          0 :                         goto exit;
    5506                 :            :                 }
    5507                 :          9 :                 break;
    5508                 :          0 :         default:
    5509                 :          0 :                 rc = -EINVAL;
    5510                 :          0 :                 goto exit;
    5511                 :            :         }
    5512                 :            : 
    5513                 :         13 :         ctx = calloc(1, sizeof(*ctx));
    5514         [ +  + ]:         13 :         if (ctx == NULL) {
    5515                 :          0 :                 SPDK_ERRLOG("Failed to alloc context.\n");
    5516                 :          0 :                 rc = -ENOMEM;
    5517                 :          0 :                 goto exit;
    5518                 :            :         }
    5519                 :            : 
    5520   [ #  #  #  # ]:         13 :         ctx->cb_fn = cb_fn;
    5521   [ #  #  #  # ]:         13 :         ctx->cb_arg = cb_arg;
    5522                 :            : 
    5523         [ #  # ]:         13 :         rc = spdk_bdev_open_ext(name, false, dummy_bdev_event_cb, NULL, &ctx->desc);
    5524         [ -  + ]:         13 :         if (rc != 0) {
    5525                 :          0 :                 SPDK_ERRLOG("Failed to open bdev %s.\n", name);
    5526                 :          0 :                 rc = -ENODEV;
    5527                 :          0 :                 goto err_open;
    5528                 :            :         }
    5529                 :            : 
    5530   [ #  #  #  # ]:         13 :         bdev = spdk_bdev_desc_get_bdev(ctx->desc);
    5531   [ -  +  #  #  :         13 :         if (bdev->module != &nvme_if) {
                   #  # ]
    5532                 :          0 :                 SPDK_ERRLOG("bdev %s is not registered in this module.\n", name);
    5533                 :          0 :                 rc = -ENODEV;
    5534                 :          0 :                 goto err_module;
    5535                 :            :         }
    5536                 :         13 :         nbdev = SPDK_CONTAINEROF(bdev, struct nvme_bdev, disk);
    5537                 :            : 
    5538   [ -  +  #  # ]:         13 :         pthread_mutex_lock(&nbdev->mutex);
    5539   [ #  #  #  # ]:         13 :         nbdev->mp_policy = policy;
    5540   [ #  #  #  # ]:         13 :         nbdev->mp_selector = selector;
    5541   [ #  #  #  # ]:         13 :         nbdev->rr_min_io = rr_min_io;
    5542   [ -  +  #  # ]:         13 :         pthread_mutex_unlock(&nbdev->mutex);
    5543                 :            : 
    5544                 :         16 :         nvme_bdev_for_each_channel(nbdev,
    5545                 :            :                                    _bdev_nvme_set_multipath_policy,
    5546                 :          3 :                                    ctx,
    5547                 :            :                                    bdev_nvme_set_multipath_policy_done);
    5548                 :         13 :         return;
    5549                 :            : 
    5550                 :          0 : err_module:
    5551   [ #  #  #  # ]:          0 :         spdk_bdev_close(ctx->desc);
    5552                 :          0 : err_open:
    5553                 :          0 :         free(ctx);
    5554                 :          0 : exit:
    5555   [ #  #  #  # ]:          0 :         cb_fn(cb_arg, rc);
    5556                 :          3 : }
    5557                 :            : 
    5558                 :            : static void
    5559                 :         50 : aer_cb(void *arg, const struct spdk_nvme_cpl *cpl)
    5560                 :            : {
    5561                 :         50 :         struct nvme_ctrlr *nvme_ctrlr           = arg;
    5562                 :            :         union spdk_nvme_async_event_completion  event;
    5563                 :            : 
    5564   [ +  +  -  +  :         50 :         if (spdk_nvme_cpl_is_error(cpl)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    5565                 :          4 :                 SPDK_WARNLOG("AER request execute failed\n");
    5566                 :          4 :                 return;
    5567                 :            :         }
    5568                 :            : 
    5569   [ #  #  #  # ]:         46 :         event.raw = cpl->cdw0;
    5570   [ +  -  +  + ]:         46 :         if ((event.bits.async_event_type == SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) &&
    5571         [ +  + ]:         46 :             (event.bits.async_event_info == SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGED)) {
    5572                 :         12 :                 nvme_ctrlr_populate_namespaces(nvme_ctrlr, NULL);
    5573   [ +  -  -  + ]:         36 :         } else if ((event.bits.async_event_type == SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) &&
    5574         [ +  - ]:         34 :                    (event.bits.async_event_info == SPDK_NVME_ASYNC_EVENT_ANA_CHANGE)) {
    5575                 :         34 :                 nvme_ctrlr_read_ana_log_page(nvme_ctrlr);
    5576                 :          1 :         }
    5577                 :          3 : }
    5578                 :            : 
    5579                 :            : static void
    5580                 :        957 : free_nvme_async_probe_ctx(struct nvme_async_probe_ctx *ctx)
    5581                 :            : {
    5582   [ +  -  +  -  :        957 :         spdk_keyring_put_key(ctx->drv_opts.tls_psk);
                   +  - ]
    5583   [ +  -  +  -  :        957 :         spdk_keyring_put_key(ctx->drv_opts.dhchap_key);
                   +  - ]
    5584   [ +  -  +  -  :        957 :         spdk_keyring_put_key(ctx->drv_opts.dhchap_ctrlr_key);
                   +  - ]
    5585                 :        957 :         free(ctx);
    5586                 :        957 : }
    5587                 :            : 
    5588                 :            : static void
    5589                 :        954 : populate_namespaces_cb(struct nvme_async_probe_ctx *ctx, int rc)
    5590                 :            : {
    5591   [ +  +  +  -  :        954 :         if (ctx->cb_fn) {
                   +  - ]
    5592   [ +  -  +  -  :        954 :                 ctx->cb_fn(ctx->cb_ctx, ctx->reported_bdevs, rc);
          -  +  +  -  +  
          -  +  -  +  -  
                   +  - ]
    5593                 :         64 :         }
    5594                 :            : 
    5595   [ +  -  +  - ]:        954 :         ctx->namespaces_populated = true;
    5596   [ +  +  +  +  :        954 :         if (ctx->probe_done) {
             +  -  +  - ]
    5597                 :            :                 /* The probe was already completed, so we need to free the context
    5598                 :            :                  * here.  This can happen for cases like OCSSD, where we need to
    5599                 :            :                  * send additional commands to the SSD after attach.
    5600                 :            :                  */
    5601                 :        226 :                 free_nvme_async_probe_ctx(ctx);
    5602                 :         32 :         }
    5603                 :        954 : }
    5604                 :            : 
    5605                 :            : static int
    5606                 :      25131 : bdev_nvme_remove_poller(void *ctx)
    5607                 :            : {
    5608                 :      15210 :         struct spdk_nvme_transport_id trid_pcie;
    5609                 :            : 
    5610         [ +  + ]:      25131 :         if (TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) {
    5611                 :        360 :                 spdk_poller_unregister(&g_hotplug_poller);
    5612                 :        360 :                 return SPDK_POLLER_IDLE;
    5613                 :            :         }
    5614                 :            : 
    5615         [ +  + ]:      24771 :         memset(&trid_pcie, 0, sizeof(trid_pcie));
    5616                 :      24771 :         spdk_nvme_trid_populate_transport(&trid_pcie, SPDK_NVME_TRANSPORT_PCIE);
    5617                 :            : 
    5618         [ +  + ]:      24771 :         if (spdk_nvme_scan_attached(&trid_pcie)) {
    5619   [ #  #  #  # ]:          0 :                 SPDK_ERRLOG_RATELIMIT("spdk_nvme_scan_attached() failed\n");
    5620                 :          0 :         }
    5621                 :            : 
    5622                 :      24771 :         return SPDK_POLLER_BUSY;
    5623                 :        259 : }
    5624                 :            : 
    5625                 :            : static void
    5626                 :       1023 : nvme_ctrlr_create_done(struct nvme_ctrlr *nvme_ctrlr,
    5627                 :            :                        struct nvme_async_probe_ctx *ctx)
    5628                 :            : {
    5629   [ +  -  +  -  :       1023 :         struct spdk_nvme_transport_id *trid = &nvme_ctrlr->active_path_id->trid;
                   +  - ]
    5630                 :            : 
    5631   [ +  +  +  -  :       1023 :         if (spdk_nvme_trtype_is_fabrics(trid->trtype)) {
                   -  + ]
    5632   [ +  +  +  +  :        495 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "ctrlr was created to %s:%s\n",
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5633                 :            :                                    trid->traddr, trid->trsvcid);
    5634                 :         61 :         } else {
    5635   [ +  +  +  +  :        528 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "ctrlr was created\n");
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    5636                 :            :         }
    5637                 :            : 
    5638                 :       1095 :         spdk_io_device_register(nvme_ctrlr,
    5639                 :            :                                 bdev_nvme_create_ctrlr_channel_cb,
    5640                 :            :                                 bdev_nvme_destroy_ctrlr_channel_cb,
    5641                 :            :                                 sizeof(struct nvme_ctrlr_channel),
    5642   [ +  -  +  -  :       1023 :                                 nvme_ctrlr->nbdev_ctrlr->name);
             +  -  +  - ]
    5643                 :            : 
    5644                 :       1023 :         nvme_ctrlr_populate_namespaces(nvme_ctrlr, ctx);
    5645                 :            : 
    5646         [ +  + ]:       1023 :         if (g_hotplug_poller == NULL) {
    5647                 :        608 :                 g_hotplug_poller = SPDK_POLLER_REGISTER(bdev_nvme_remove_poller, NULL,
    5648                 :            :                                                         NVME_HOTPLUG_POLL_PERIOD_DEFAULT);
    5649                 :         13 :         }
    5650                 :       1023 : }
    5651                 :            : 
    5652                 :            : static void
    5653                 :        206 : nvme_ctrlr_init_ana_log_page_done(void *_ctx, const struct spdk_nvme_cpl *cpl)
    5654                 :            : {
    5655                 :        206 :         struct nvme_ctrlr *nvme_ctrlr = _ctx;
    5656   [ #  #  #  # ]:        206 :         struct nvme_async_probe_ctx *ctx = nvme_ctrlr->probe_ctx;
    5657                 :            : 
    5658   [ #  #  #  # ]:        206 :         nvme_ctrlr->probe_ctx = NULL;
    5659                 :            : 
    5660   [ +  -  +  +  :        206 :         if (spdk_nvme_cpl_is_error(cpl)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    5661                 :          0 :                 nvme_ctrlr_delete(nvme_ctrlr);
    5662                 :            : 
    5663         [ #  # ]:          0 :                 if (ctx != NULL) {
    5664   [ #  #  #  # ]:          0 :                         ctx->reported_bdevs = 0;
    5665                 :          0 :                         populate_namespaces_cb(ctx, -1);
    5666                 :          0 :                 }
    5667                 :          0 :                 return;
    5668                 :            :         }
    5669                 :            : 
    5670                 :        206 :         nvme_ctrlr_create_done(nvme_ctrlr, ctx);
    5671                 :         31 : }
    5672                 :            : 
    5673                 :            : static int
    5674                 :        206 : nvme_ctrlr_init_ana_log_page(struct nvme_ctrlr *nvme_ctrlr,
    5675                 :            :                              struct nvme_async_probe_ctx *ctx)
    5676                 :            : {
    5677   [ #  #  #  # ]:        206 :         struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
    5678                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    5679                 :            :         uint32_t ana_log_page_size;
    5680                 :            : 
    5681                 :        206 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    5682                 :            : 
    5683                 :            :         /* Set buffer size enough to include maximum number of allowed namespaces. */
    5684   [ #  #  #  # ]:        237 :         ana_log_page_size = sizeof(struct spdk_nvme_ana_page) + cdata->nanagrpid *
    5685   [ #  #  #  # ]:        206 :                             sizeof(struct spdk_nvme_ana_group_descriptor) + cdata->mnan *
    5686                 :            :                             sizeof(uint32_t);
    5687                 :            : 
    5688   [ #  #  #  # ]:        206 :         nvme_ctrlr->ana_log_page = spdk_zmalloc(ana_log_page_size, 64, NULL,
    5689                 :            :                                                 SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
    5690   [ +  +  #  #  :        206 :         if (nvme_ctrlr->ana_log_page == NULL) {
                   #  # ]
    5691   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "could not allocate ANA log page buffer\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5692                 :          0 :                 return -ENXIO;
    5693                 :            :         }
    5694                 :            : 
    5695                 :            :         /* Each descriptor in a ANA log page is not ensured to be 8-bytes aligned.
    5696                 :            :          * Hence copy each descriptor to a temporary area when parsing it.
    5697                 :            :          *
    5698                 :            :          * Allocate a buffer whose size is as large as ANA log page buffer because
    5699                 :            :          * we do not know the size of a descriptor until actually reading it.
    5700                 :            :          */
    5701   [ #  #  #  # ]:        206 :         nvme_ctrlr->copied_ana_desc = calloc(1, ana_log_page_size);
    5702   [ +  +  #  #  :        206 :         if (nvme_ctrlr->copied_ana_desc == NULL) {
                   #  # ]
    5703   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "could not allocate a buffer to parse ANA descriptor\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5704                 :          0 :                 return -ENOMEM;
    5705                 :            :         }
    5706                 :            : 
    5707   [ #  #  #  # ]:        206 :         nvme_ctrlr->max_ana_log_page_size = ana_log_page_size;
    5708                 :            : 
    5709   [ #  #  #  # ]:        206 :         nvme_ctrlr->probe_ctx = ctx;
    5710                 :            : 
    5711                 :            :         /* Then, set the read size only to include the current active namespaces. */
    5712                 :        206 :         ana_log_page_size = nvme_ctrlr_get_ana_log_page_size(nvme_ctrlr);
    5713                 :            : 
    5714   [ -  +  #  #  :        206 :         if (ana_log_page_size > nvme_ctrlr->max_ana_log_page_size) {
                   #  # ]
    5715   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "ANA log page size %" PRIu32 " is larger than allowed %" PRIu32 "\n",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    5716                 :            :                                   ana_log_page_size, nvme_ctrlr->max_ana_log_page_size);
    5717                 :          0 :                 return -EINVAL;
    5718                 :            :         }
    5719                 :            : 
    5720                 :        237 :         return spdk_nvme_ctrlr_cmd_get_log_page(ctrlr,
    5721                 :            :                                                 SPDK_NVME_LOG_ASYMMETRIC_NAMESPACE_ACCESS,
    5722                 :            :                                                 SPDK_NVME_GLOBAL_NS_TAG,
    5723   [ #  #  #  # ]:        206 :                                                 nvme_ctrlr->ana_log_page,
    5724                 :         31 :                                                 ana_log_page_size, 0,
    5725                 :            :                                                 nvme_ctrlr_init_ana_log_page_done,
    5726                 :         31 :                                                 nvme_ctrlr);
    5727                 :         31 : }
    5728                 :            : 
    5729                 :            : /* hostnqn and subnqn were already verified before attaching a controller.
    5730                 :            :  * Hence check only the multipath capability and cntlid here.
    5731                 :            :  */
    5732                 :            : static bool
    5733                 :         67 : bdev_nvme_check_multipath(struct nvme_bdev_ctrlr *nbdev_ctrlr, struct spdk_nvme_ctrlr *ctrlr)
    5734                 :            : {
    5735                 :            :         struct nvme_ctrlr *tmp;
    5736                 :            :         const struct spdk_nvme_ctrlr_data *cdata, *tmp_cdata;
    5737                 :            : 
    5738                 :         67 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    5739                 :            : 
    5740   [ -  +  #  #  :         67 :         if (!cdata->cmic.multi_ctrlr) {
                   #  # ]
    5741   [ #  #  #  # ]:          0 :                 SPDK_ERRLOG("Ctrlr%u does not support multipath.\n", cdata->cntlid);
    5742                 :          0 :                 return false;
    5743                 :            :         }
    5744                 :            : 
    5745   [ +  +  #  #  :        138 :         TAILQ_FOREACH(tmp, &nbdev_ctrlr->ctrlrs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    5746   [ #  #  #  # ]:         75 :                 tmp_cdata = spdk_nvme_ctrlr_get_data(tmp->ctrlr);
    5747                 :            : 
    5748   [ +  +  #  #  :         75 :                 if (!tmp_cdata->cmic.multi_ctrlr) {
                   #  # ]
    5749   [ #  #  #  #  :          0 :                         NVME_CTRLR_ERRLOG(tmp, "Ctrlr%u does not support multipath.\n", cdata->cntlid);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    5750                 :          0 :                         return false;
    5751                 :            :                 }
    5752   [ +  +  #  #  :         75 :                 if (cdata->cntlid == tmp_cdata->cntlid) {
          #  #  #  #  #  
                      # ]
    5753   [ +  -  #  #  :          4 :                         NVME_CTRLR_ERRLOG(tmp, "cntlid %u are duplicated.\n", tmp_cdata->cntlid);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    5754                 :          4 :                         return false;
    5755                 :            :                 }
    5756                 :         17 :         }
    5757                 :            : 
    5758                 :         63 :         return true;
    5759                 :         16 : }
    5760                 :            : 
    5761                 :            : 
    5762                 :            : static int
    5763                 :       1027 : nvme_bdev_ctrlr_create(const char *name, struct nvme_ctrlr *nvme_ctrlr)
    5764                 :            : {
    5765                 :            :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
    5766   [ +  -  +  - ]:       1027 :         struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
    5767                 :            :         struct nvme_ctrlr      *nctrlr;
    5768                 :       1027 :         int rc = 0;
    5769                 :            : 
    5770         [ +  + ]:       1027 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    5771                 :            : 
    5772                 :       1027 :         nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
    5773         [ +  + ]:       1027 :         if (nbdev_ctrlr != NULL) {
    5774         [ +  + ]:         67 :                 if (!bdev_nvme_check_multipath(nbdev_ctrlr, ctrlr)) {
    5775                 :          4 :                         rc = -EINVAL;
    5776                 :          4 :                         goto exit;
    5777                 :            :                 }
    5778   [ +  +  #  #  :        134 :                 TAILQ_FOREACH(nctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    5779   [ +  +  -  +  :         71 :                         if (nctrlr->opts.multipath != nvme_ctrlr->opts.multipath) {
          -  +  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    5780                 :            :                                 /* All controllers with the same name must be configured the same
    5781                 :            :                                  * way, either for multipath or failover. If the configuration doesn't
    5782                 :            :                                  * match - report error.
    5783                 :            :                                  */
    5784                 :          0 :                                 rc = -EINVAL;
    5785                 :          0 :                                 goto exit;
    5786                 :            :                         }
    5787                 :         17 :                 }
    5788                 :         15 :         } else {
    5789                 :        960 :                 nbdev_ctrlr = calloc(1, sizeof(*nbdev_ctrlr));
    5790         [ +  + ]:        960 :                 if (nbdev_ctrlr == NULL) {
    5791   [ #  #  #  #  :          0 :                         NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to allocate nvme_bdev_ctrlr.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5792                 :          0 :                         rc = -ENOMEM;
    5793                 :          0 :                         goto exit;
    5794                 :            :                 }
    5795   [ +  +  +  -  :        960 :                 nbdev_ctrlr->name = strdup(name);
                   +  - ]
    5796   [ +  +  +  -  :        960 :                 if (nbdev_ctrlr->name == NULL) {
                   +  - ]
    5797   [ #  #  #  #  :          0 :                         NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to allocate name of nvme_bdev_ctrlr.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5798                 :          0 :                         free(nbdev_ctrlr);
    5799                 :          0 :                         goto exit;
    5800                 :            :                 }
    5801   [ +  -  +  -  :        960 :                 TAILQ_INIT(&nbdev_ctrlr->ctrlrs);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    5802   [ +  -  +  -  :        960 :                 TAILQ_INIT(&nbdev_ctrlr->bdevs);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    5803   [ +  -  +  -  :        960 :                 TAILQ_INSERT_TAIL(&g_nvme_bdev_ctrlrs, nbdev_ctrlr, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    5804                 :            :         }
    5805   [ +  -  +  - ]:       1023 :         nvme_ctrlr->nbdev_ctrlr = nbdev_ctrlr;
    5806   [ +  -  +  -  :       1023 :         TAILQ_INSERT_TAIL(&nbdev_ctrlr->ctrlrs, nvme_ctrlr, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    5807                 :        954 : exit:
    5808         [ +  + ]:       1027 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    5809                 :       1027 :         return rc;
    5810                 :            : }
    5811                 :            : 
    5812                 :            : static int
    5813                 :       1027 : nvme_ctrlr_create(struct spdk_nvme_ctrlr *ctrlr,
    5814                 :            :                   const char *name,
    5815                 :            :                   const struct spdk_nvme_transport_id *trid,
    5816                 :            :                   struct nvme_async_probe_ctx *ctx)
    5817                 :            : {
    5818                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    5819                 :            :         struct nvme_path_id *path_id;
    5820                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    5821                 :       1027 :         struct spdk_event_handler_opts opts = {
    5822                 :            :                 .opts_size = SPDK_SIZEOF(&opts, fd_type),
    5823                 :            :         };
    5824                 :            :         uint64_t period;
    5825                 :            :         int fd, rc;
    5826                 :            : 
    5827                 :       1027 :         nvme_ctrlr = calloc(1, sizeof(*nvme_ctrlr));
    5828         [ +  + ]:       1027 :         if (nvme_ctrlr == NULL) {
    5829                 :          0 :                 SPDK_ERRLOG("Failed to allocate device struct\n");
    5830                 :          0 :                 return -ENOMEM;
    5831                 :            :         }
    5832                 :            : 
    5833   [ +  +  +  - ]:       1027 :         rc = pthread_mutex_init(&nvme_ctrlr->mutex, NULL);
    5834         [ -  + ]:       1027 :         if (rc != 0) {
    5835                 :          0 :                 free(nvme_ctrlr);
    5836                 :          0 :                 return rc;
    5837                 :            :         }
    5838                 :            : 
    5839   [ +  -  +  -  :       1027 :         TAILQ_INIT(&nvme_ctrlr->trids);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    5840   [ +  -  +  -  :       1027 :         TAILQ_INIT(&nvme_ctrlr->pending_resets);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    5841   [ +  -  +  -  :       1027 :         RB_INIT(&nvme_ctrlr->namespaces);
                   +  - ]
    5842                 :            : 
    5843                 :            :         /* Get another reference to the key, so the first one can be released from probe_ctx */
    5844         [ +  + ]:       1027 :         if (ctx != NULL) {
    5845   [ +  +  +  -  :        915 :                 if (ctx->drv_opts.tls_psk != NULL) {
             +  -  +  - ]
    5846   [ #  #  #  # ]:         13 :                         nvme_ctrlr->psk = spdk_keyring_get_key(
    5847   [ #  #  #  #  :          0 :                                                   spdk_key_get_name(ctx->drv_opts.tls_psk));
                   #  # ]
    5848   [ -  +  #  #  :         13 :                         if (nvme_ctrlr->psk == NULL) {
                   #  # ]
    5849                 :            :                                 /* Could only happen if the key was removed in the meantime */
    5850   [ #  #  #  #  :          0 :                                 SPDK_ERRLOG("Couldn't get a reference to the key '%s'\n",
                   #  # ]
    5851                 :            :                                             spdk_key_get_name(ctx->drv_opts.tls_psk));
    5852                 :          0 :                                 rc = -ENOKEY;
    5853                 :          0 :                                 goto err;
    5854                 :            :                         }
    5855                 :          0 :                 }
    5856                 :            : 
    5857   [ +  +  +  -  :        915 :                 if (ctx->drv_opts.dhchap_key != NULL) {
             +  -  +  - ]
    5858   [ #  #  #  # ]:        157 :                         nvme_ctrlr->dhchap_key = spdk_keyring_get_key(
    5859   [ #  #  #  #  :          0 :                                                          spdk_key_get_name(ctx->drv_opts.dhchap_key));
                   #  # ]
    5860   [ -  +  #  #  :        157 :                         if (nvme_ctrlr->dhchap_key == NULL) {
                   #  # ]
    5861   [ #  #  #  #  :          0 :                                 SPDK_ERRLOG("Couldn't get a reference to the key '%s'\n",
                   #  # ]
    5862                 :            :                                             spdk_key_get_name(ctx->drv_opts.dhchap_key));
    5863                 :          0 :                                 rc = -ENOKEY;
    5864                 :          0 :                                 goto err;
    5865                 :            :                         }
    5866                 :          0 :                 }
    5867                 :            : 
    5868   [ +  +  +  -  :        915 :                 if (ctx->drv_opts.dhchap_ctrlr_key != NULL) {
             +  -  +  - ]
    5869   [ #  #  #  # ]:        121 :                         nvme_ctrlr->dhchap_ctrlr_key =
    5870                 :        121 :                                 spdk_keyring_get_key(
    5871   [ #  #  #  #  :          0 :                                         spdk_key_get_name(ctx->drv_opts.dhchap_ctrlr_key));
                   #  # ]
    5872   [ -  +  #  #  :        121 :                         if (nvme_ctrlr->dhchap_ctrlr_key == NULL) {
                   #  # ]
    5873   [ #  #  #  #  :          0 :                                 SPDK_ERRLOG("Couldn't get a reference to the key '%s'\n",
                   #  # ]
    5874                 :            :                                             spdk_key_get_name(ctx->drv_opts.dhchap_ctrlr_key));
    5875                 :          0 :                                 rc = -ENOKEY;
    5876                 :          0 :                                 goto err;
    5877                 :            :                         }
    5878                 :          0 :                 }
    5879                 :         59 :         }
    5880                 :            : 
    5881                 :            :         /* Check if we manage to enable interrupts on the controller. */
    5882   [ -  +  -  -  :       1027 :         if (spdk_interrupt_mode_is_enabled() && ctx != NULL && !ctx->drv_opts.enable_interrupts) {
          -  -  -  -  #  
             #  #  #  #  
                      # ]
    5883                 :          0 :                 SPDK_ERRLOG("Failed to enable interrupts on the controller\n");
    5884                 :          0 :                 rc = -ENOTSUP;
    5885                 :          0 :                 goto err;
    5886                 :            :         }
    5887                 :            : 
    5888                 :       1027 :         path_id = calloc(1, sizeof(*path_id));
    5889         [ +  + ]:       1027 :         if (path_id == NULL) {
    5890                 :          0 :                 SPDK_ERRLOG("Failed to allocate trid entry pointer\n");
    5891                 :          0 :                 rc = -ENOMEM;
    5892                 :          0 :                 goto err;
    5893                 :            :         }
    5894                 :            : 
    5895         [ +  - ]:       1027 :         path_id->trid = *trid;
    5896         [ +  + ]:       1027 :         if (ctx != NULL) {
    5897   [ +  +  +  +  :        915 :                 memcpy(path_id->hostid.hostaddr, ctx->drv_opts.src_addr, sizeof(path_id->hostid.hostaddr));
          +  -  +  -  +  
                -  +  - ]
    5898   [ +  +  +  +  :        915 :                 memcpy(path_id->hostid.hostsvcid, ctx->drv_opts.src_svcid, sizeof(path_id->hostid.hostsvcid));
          +  -  +  -  +  
                -  +  - ]
    5899                 :         59 :         }
    5900   [ +  -  +  - ]:       1027 :         nvme_ctrlr->active_path_id = path_id;
    5901   [ +  +  +  -  :       1027 :         TAILQ_INSERT_HEAD(&nvme_ctrlr->trids, path_id, link);
          +  -  +  -  +  
          -  +  -  -  +  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    5902                 :            : 
    5903   [ +  -  +  - ]:       1027 :         nvme_ctrlr->thread = spdk_get_thread();
    5904   [ +  -  +  - ]:       1027 :         nvme_ctrlr->ctrlr = ctrlr;
    5905   [ +  -  +  - ]:       1027 :         nvme_ctrlr->ref = 1;
    5906                 :            : 
    5907         [ -  + ]:       1027 :         if (spdk_nvme_ctrlr_is_ocssd_supported(ctrlr)) {
    5908                 :          0 :                 SPDK_ERRLOG("OCSSDs are not supported");
    5909                 :          0 :                 rc = -ENOTSUP;
    5910                 :          0 :                 goto err;
    5911                 :            :         }
    5912                 :            : 
    5913         [ +  + ]:       1027 :         if (ctx != NULL) {
    5914   [ +  +  +  +  :        915 :                 memcpy(&nvme_ctrlr->opts, &ctx->bdev_opts, sizeof(ctx->bdev_opts));
             +  -  +  - ]
    5915                 :         59 :         } else {
    5916         [ #  # ]:        112 :                 spdk_bdev_nvme_get_default_ctrlr_opts(&nvme_ctrlr->opts);
    5917                 :            :         }
    5918                 :            : 
    5919   [ +  +  +  - ]:       1027 :         period = spdk_interrupt_mode_is_enabled() ? 0 : g_opts.nvme_adminq_poll_period_us;
    5920                 :            : 
    5921   [ +  -  +  - ]:       1027 :         nvme_ctrlr->adminq_timer_poller = SPDK_POLLER_REGISTER(bdev_nvme_poll_adminq, nvme_ctrlr,
    5922                 :            :                                           period);
    5923                 :            : 
    5924         [ +  + ]:       1027 :         if (spdk_interrupt_mode_is_enabled()) {
    5925   [ #  #  #  # ]:          0 :                 spdk_poller_register_interrupt(nvme_ctrlr->adminq_timer_poller, NULL, NULL);
    5926                 :            : 
    5927   [ #  #  #  # ]:          0 :                 fd = spdk_nvme_ctrlr_get_admin_qp_fd(nvme_ctrlr->ctrlr, &opts);
    5928         [ #  # ]:          0 :                 if (fd < 0) {
    5929                 :          0 :                         rc = fd;
    5930                 :          0 :                         goto err;
    5931                 :            :                 }
    5932                 :            : 
    5933   [ #  #  #  # ]:          0 :                 nvme_ctrlr->intr = SPDK_INTERRUPT_REGISTER_EXT(fd, bdev_nvme_poll_adminq,
    5934                 :            :                                    nvme_ctrlr, &opts);
    5935   [ #  #  #  #  :          0 :                 if (!nvme_ctrlr->intr) {
                   #  # ]
    5936                 :          0 :                         rc = -EINVAL;
    5937                 :          0 :                         goto err;
    5938                 :            :                 }
    5939                 :          0 :         }
    5940                 :            : 
    5941   [ +  +  +  - ]:       1027 :         if (g_opts.timeout_us > 0) {
    5942                 :            :                 /* Register timeout callback. Timeout values for IO vs. admin reqs can be different. */
    5943                 :            :                 /* If timeout_admin_us is 0 (not specified), admin uses same timeout as IO. */
    5944   [ #  #  #  # ]:          0 :                 uint64_t adm_timeout_us = (g_opts.timeout_admin_us == 0) ?
    5945   [ #  #  #  # ]:          0 :                                           g_opts.timeout_us : g_opts.timeout_admin_us;
    5946         [ #  # ]:          0 :                 spdk_nvme_ctrlr_register_timeout_callback(ctrlr, g_opts.timeout_us,
    5947                 :          0 :                                 adm_timeout_us, timeout_cb, nvme_ctrlr);
    5948                 :          0 :         }
    5949                 :            : 
    5950                 :       1027 :         spdk_nvme_ctrlr_register_aer_callback(ctrlr, aer_cb, nvme_ctrlr);
    5951                 :       1027 :         spdk_nvme_ctrlr_set_remove_cb(ctrlr, remove_cb, nvme_ctrlr);
    5952                 :            : 
    5953         [ +  + ]:       1027 :         if (spdk_nvme_ctrlr_get_flags(ctrlr) &
    5954                 :            :             SPDK_NVME_CTRLR_SECURITY_SEND_RECV_SUPPORTED) {
    5955   [ #  #  #  # ]:         27 :                 nvme_ctrlr->opal_dev = spdk_opal_dev_construct(ctrlr);
    5956                 :          0 :         }
    5957                 :            : 
    5958                 :       1027 :         rc = nvme_bdev_ctrlr_create(name, nvme_ctrlr);
    5959         [ +  + ]:       1027 :         if (rc != 0) {
    5960                 :          4 :                 goto err;
    5961                 :            :         }
    5962                 :            : 
    5963                 :       1023 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    5964                 :            : 
    5965   [ +  +  -  +  :       1023 :         if (cdata->cmic.ana_reporting) {
                   -  + ]
    5966                 :        206 :                 rc = nvme_ctrlr_init_ana_log_page(nvme_ctrlr, ctx);
    5967         [ +  - ]:        206 :                 if (rc == 0) {
    5968                 :        206 :                         return 0;
    5969                 :            :                 }
    5970                 :          0 :         } else {
    5971                 :        817 :                 nvme_ctrlr_create_done(nvme_ctrlr, ctx);
    5972                 :        817 :                 return 0;
    5973                 :            :         }
    5974                 :            : 
    5975                 :          3 : err:
    5976                 :          4 :         nvme_ctrlr_delete(nvme_ctrlr);
    5977                 :          4 :         return rc;
    5978                 :         73 : }
    5979                 :            : 
    5980                 :            : void
    5981                 :        916 : spdk_bdev_nvme_get_default_ctrlr_opts(struct spdk_bdev_nvme_ctrlr_opts *opts)
    5982                 :            : {
    5983   [ +  -  +  - ]:        916 :         opts->prchk_flags = 0;
    5984   [ +  -  +  -  :        916 :         opts->ctrlr_loss_timeout_sec = g_opts.ctrlr_loss_timeout_sec;
                   +  - ]
    5985   [ +  -  +  -  :        916 :         opts->reconnect_delay_sec = g_opts.reconnect_delay_sec;
                   +  - ]
    5986   [ +  -  +  -  :        916 :         opts->fast_io_fail_timeout_sec = g_opts.fast_io_fail_timeout_sec;
                   +  - ]
    5987   [ +  -  +  - ]:        916 :         opts->multipath = true;
    5988                 :        916 : }
    5989                 :            : 
    5990                 :            : static void
    5991                 :         56 : attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
    5992                 :            :           struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *drv_opts)
    5993                 :            : {
    5994                 :            :         char *name;
    5995                 :            : 
    5996         [ #  # ]:         56 :         name = spdk_sprintf_alloc("HotInNvme%d", g_hot_insert_nvme_controller_index++);
    5997         [ -  + ]:         56 :         if (!name) {
    5998                 :          0 :                 SPDK_ERRLOG("Failed to assign name to NVMe device\n");
    5999                 :          0 :                 return;
    6000                 :            :         }
    6001                 :            : 
    6002         [ +  - ]:         56 :         if (nvme_ctrlr_create(ctrlr, name, trid, NULL) == 0) {
    6003   [ -  +  -  +  :         56 :                 SPDK_DEBUGLOG(bdev_nvme, "Attached to %s (%s)\n", trid->traddr, name);
             #  #  #  # ]
    6004                 :          0 :         } else {
    6005         [ #  # ]:          0 :                 SPDK_ERRLOG("Failed to attach to %s (%s)\n", trid->traddr, name);
    6006                 :            :         }
    6007                 :            : 
    6008                 :         56 :         free(name);
    6009                 :          0 : }
    6010                 :            : 
    6011                 :            : static void
    6012                 :       1023 : _nvme_ctrlr_destruct(void *ctx)
    6013                 :            : {
    6014                 :       1023 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    6015                 :            : 
    6016                 :       1023 :         nvme_ctrlr_depopulate_namespaces(nvme_ctrlr);
    6017                 :       1023 :         nvme_ctrlr_put_ref(nvme_ctrlr);
    6018                 :       1023 : }
    6019                 :            : 
    6020                 :            : static int
    6021                 :        509 : bdev_nvme_delete_ctrlr_unsafe(struct nvme_ctrlr *nvme_ctrlr, bool hotplug)
    6022                 :            : {
    6023                 :            :         struct nvme_probe_skip_entry *entry;
    6024                 :            : 
    6025                 :            :         /* The controller's destruction was already started */
    6026   [ -  +  #  # ]:        509 :         if (nvme_ctrlr->destruct) {
    6027                 :          0 :                 return -EALREADY;
    6028                 :            :         }
    6029                 :            : 
    6030   [ +  +  +  +  :        509 :         if (!hotplug &&
                   #  # ]
    6031   [ +  +  #  #  :        461 :             nvme_ctrlr->active_path_id->trid.trtype == SPDK_NVME_TRANSPORT_PCIE) {
          #  #  #  #  #  
                      # ]
    6032                 :         44 :                 entry = calloc(1, sizeof(*entry));
    6033         [ -  + ]:         44 :                 if (!entry) {
    6034                 :          0 :                         return -ENOMEM;
    6035                 :            :                 }
    6036   [ #  #  #  #  :         44 :                 entry->trid = nvme_ctrlr->active_path_id->trid;
             #  #  #  # ]
    6037   [ #  #  #  #  :         44 :                 TAILQ_INSERT_TAIL(&g_skipped_nvme_ctrlrs, entry, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6038                 :          3 :         }
    6039                 :            : 
    6040         [ #  # ]:        509 :         nvme_ctrlr->destruct = true;
    6041                 :        509 :         return 0;
    6042                 :         61 : }
    6043                 :            : 
    6044                 :            : static int
    6045                 :         63 : bdev_nvme_delete_ctrlr(struct nvme_ctrlr *nvme_ctrlr, bool hotplug)
    6046                 :            : {
    6047                 :            :         int rc;
    6048                 :            : 
    6049   [ -  +  #  # ]:         63 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    6050         [ #  # ]:         63 :         rc = bdev_nvme_delete_ctrlr_unsafe(nvme_ctrlr, hotplug);
    6051   [ -  +  #  # ]:         63 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    6052                 :            : 
    6053         [ +  + ]:         63 :         if (rc == 0) {
    6054                 :         63 :                 _nvme_ctrlr_destruct(nvme_ctrlr);
    6055         [ #  # ]:          2 :         } else if (rc == -EALREADY) {
    6056                 :          0 :                 rc = 0;
    6057                 :          0 :         }
    6058                 :            : 
    6059                 :         63 :         return rc;
    6060                 :            : }
    6061                 :            : 
    6062                 :            : static void
    6063                 :         48 : remove_cb(void *cb_ctx, struct spdk_nvme_ctrlr *ctrlr)
    6064                 :            : {
    6065                 :         48 :         struct nvme_ctrlr *nvme_ctrlr = cb_ctx;
    6066                 :            : 
    6067                 :         48 :         bdev_nvme_delete_ctrlr(nvme_ctrlr, true);
    6068                 :         48 : }
    6069                 :            : 
    6070                 :            : static int
    6071                 :      19970 : bdev_nvme_hotplug_probe(void *arg)
    6072                 :            : {
    6073         [ +  + ]:      19970 :         if (g_hotplug_probe_ctx == NULL) {
    6074                 :          1 :                 spdk_poller_unregister(&g_hotplug_probe_poller);
    6075                 :          1 :                 return SPDK_POLLER_IDLE;
    6076                 :            :         }
    6077                 :            : 
    6078         [ +  + ]:      19969 :         if (spdk_nvme_probe_poll_async(g_hotplug_probe_ctx) != -EAGAIN) {
    6079                 :       3827 :                 g_hotplug_probe_ctx = NULL;
    6080                 :       3827 :                 spdk_poller_unregister(&g_hotplug_probe_poller);
    6081                 :          0 :         }
    6082                 :            : 
    6083                 :      19969 :         return SPDK_POLLER_BUSY;
    6084                 :          0 : }
    6085                 :            : 
    6086                 :            : static int
    6087                 :       3975 : bdev_nvme_hotplug(void *arg)
    6088                 :            : {
    6089                 :       1953 :         struct spdk_nvme_transport_id trid_pcie;
    6090                 :            : 
    6091         [ +  + ]:       3975 :         if (g_hotplug_probe_ctx) {
    6092                 :        147 :                 return SPDK_POLLER_BUSY;
    6093                 :            :         }
    6094                 :            : 
    6095         [ -  + ]:       3828 :         memset(&trid_pcie, 0, sizeof(trid_pcie));
    6096                 :       3828 :         spdk_nvme_trid_populate_transport(&trid_pcie, SPDK_NVME_TRANSPORT_PCIE);
    6097                 :            : 
    6098                 :       3828 :         g_hotplug_probe_ctx = spdk_nvme_probe_async(&trid_pcie, NULL,
    6099                 :            :                               hotplug_probe_cb, attach_cb, NULL);
    6100                 :            : 
    6101         [ +  - ]:       3828 :         if (g_hotplug_probe_ctx) {
    6102   [ -  +  #  # ]:       3828 :                 assert(g_hotplug_probe_poller == NULL);
    6103                 :       3828 :                 g_hotplug_probe_poller = SPDK_POLLER_REGISTER(bdev_nvme_hotplug_probe, NULL, 1000);
    6104                 :          0 :         }
    6105                 :            : 
    6106                 :       3828 :         return SPDK_POLLER_BUSY;
    6107                 :          0 : }
    6108                 :            : 
    6109                 :            : void
    6110                 :        470 : bdev_nvme_get_opts(struct spdk_bdev_nvme_opts *opts)
    6111                 :            : {
    6112                 :        470 :         *opts = g_opts;
    6113                 :        470 : }
    6114                 :            : 
    6115                 :            : static bool bdev_nvme_check_io_error_resiliency_params(int32_t ctrlr_loss_timeout_sec,
    6116                 :            :                 uint32_t reconnect_delay_sec,
    6117                 :            :                 uint32_t fast_io_fail_timeout_sec);
    6118                 :            : 
    6119                 :            : static int
    6120                 :        470 : bdev_nvme_validate_opts(const struct spdk_bdev_nvme_opts *opts)
    6121                 :            : {
    6122   [ +  +  +  +  :        470 :         if ((opts->timeout_us == 0) && (opts->timeout_admin_us != 0)) {
          +  -  +  -  +  
                -  +  - ]
    6123                 :            :                 /* Can't set timeout_admin_us without also setting timeout_us */
    6124                 :          0 :                 SPDK_WARNLOG("Invalid options: Can't have (timeout_us == 0) with (timeout_admin_us > 0)\n");
    6125                 :          0 :                 return -EINVAL;
    6126                 :            :         }
    6127                 :            : 
    6128   [ +  +  +  -  :        470 :         if (opts->bdev_retry_count < -1) {
                   -  + ]
    6129                 :          0 :                 SPDK_WARNLOG("Invalid option: bdev_retry_count can't be less than -1.\n");
    6130                 :          0 :                 return -EINVAL;
    6131                 :            :         }
    6132                 :            : 
    6133   [ +  +  +  -  :        513 :         if (!bdev_nvme_check_io_error_resiliency_params(opts->ctrlr_loss_timeout_sec,
                   +  - ]
    6134   [ +  -  +  - ]:        470 :                         opts->reconnect_delay_sec,
    6135   [ +  -  +  - ]:        470 :                         opts->fast_io_fail_timeout_sec)) {
    6136                 :          0 :                 return -EINVAL;
    6137                 :            :         }
    6138                 :            : 
    6139                 :        470 :         return 0;
    6140                 :         43 : }
    6141                 :            : 
    6142                 :            : int
    6143                 :        470 : bdev_nvme_set_opts(const struct spdk_bdev_nvme_opts *opts)
    6144                 :            : {
    6145                 :            :         int ret;
    6146                 :            : 
    6147                 :        470 :         ret = bdev_nvme_validate_opts(opts);
    6148         [ -  + ]:        470 :         if (ret) {
    6149                 :          0 :                 SPDK_WARNLOG("Failed to set nvme opts.\n");
    6150                 :          0 :                 return ret;
    6151                 :            :         }
    6152                 :            : 
    6153         [ +  + ]:        470 :         if (g_bdev_nvme_init_thread != NULL) {
    6154         [ -  + ]:        179 :                 if (!TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) {
    6155                 :          0 :                         return -EPERM;
    6156                 :            :                 }
    6157                 :          1 :         }
    6158                 :            : 
    6159   [ +  -  +  +  :        513 :         if (opts->rdma_srq_size != 0 ||
             +  -  -  + ]
    6160   [ +  -  +  -  :        470 :             opts->rdma_max_cq_size != 0 ||
                   +  - ]
    6161   [ -  +  -  + ]:        470 :             opts->rdma_cm_event_timeout_ms != 0) {
    6162                 :          0 :                 struct spdk_nvme_transport_opts drv_opts;
    6163                 :            : 
    6164                 :          0 :                 spdk_nvme_transport_get_opts(&drv_opts, sizeof(drv_opts));
    6165   [ #  #  #  #  :          0 :                 if (opts->rdma_srq_size != 0) {
                   #  # ]
    6166   [ #  #  #  # ]:          0 :                         drv_opts.rdma_srq_size = opts->rdma_srq_size;
    6167                 :          0 :                 }
    6168   [ #  #  #  #  :          0 :                 if (opts->rdma_max_cq_size != 0) {
                   #  # ]
    6169   [ #  #  #  #  :          0 :                         drv_opts.rdma_max_cq_size = opts->rdma_max_cq_size;
                   #  # ]
    6170                 :          0 :                 }
    6171   [ #  #  #  #  :          0 :                 if (opts->rdma_cm_event_timeout_ms != 0) {
                   #  # ]
    6172   [ #  #  #  #  :          0 :                         drv_opts.rdma_cm_event_timeout_ms = opts->rdma_cm_event_timeout_ms;
                   #  # ]
    6173                 :          0 :                 }
    6174                 :            : 
    6175                 :          0 :                 ret = spdk_nvme_transport_set_opts(&drv_opts, sizeof(drv_opts));
    6176         [ #  # ]:          0 :                 if (ret) {
    6177                 :          0 :                         SPDK_ERRLOG("Failed to set NVMe transport opts.\n");
    6178                 :          0 :                         return ret;
    6179                 :            :                 }
    6180                 :          0 :         }
    6181                 :            : 
    6182                 :        470 :         g_opts = *opts;
    6183                 :            : 
    6184                 :        470 :         return 0;
    6185                 :         43 : }
    6186                 :            : 
    6187                 :            : struct set_nvme_hotplug_ctx {
    6188                 :            :         uint64_t period_us;
    6189                 :            :         bool enabled;
    6190                 :            :         spdk_msg_fn fn;
    6191                 :            :         void *fn_ctx;
    6192                 :            : };
    6193                 :            : 
    6194                 :            : static void
    6195                 :        292 : set_nvme_hotplug_period_cb(void *_ctx)
    6196                 :            : {
    6197                 :        292 :         struct set_nvme_hotplug_ctx *ctx = _ctx;
    6198                 :            : 
    6199                 :        292 :         spdk_poller_unregister(&g_hotplug_poller);
    6200   [ +  +  +  +  :        292 :         if (ctx->enabled) {
             +  -  -  + ]
    6201   [ #  #  #  # ]:         12 :                 g_hotplug_poller = SPDK_POLLER_REGISTER(bdev_nvme_hotplug, NULL, ctx->period_us);
    6202                 :          0 :         } else {
    6203                 :        280 :                 g_hotplug_poller = SPDK_POLLER_REGISTER(bdev_nvme_remove_poller, NULL,
    6204                 :            :                                                         NVME_HOTPLUG_POLL_PERIOD_DEFAULT);
    6205                 :            :         }
    6206                 :            : 
    6207   [ +  -  +  - ]:        292 :         g_nvme_hotplug_poll_period_us = ctx->period_us;
    6208   [ +  +  +  -  :        292 :         g_nvme_hotplug_enabled = ctx->enabled;
                   +  - ]
    6209   [ +  +  +  -  :        292 :         if (ctx->fn) {
                   +  - ]
    6210   [ +  -  +  -  :        292 :                 ctx->fn(ctx->fn_ctx);
          -  +  +  -  +  
                -  +  - ]
    6211                 :         41 :         }
    6212                 :            : 
    6213                 :        292 :         free(ctx);
    6214                 :        292 : }
    6215                 :            : 
    6216                 :            : int
    6217                 :        292 : bdev_nvme_set_hotplug(bool enabled, uint64_t period_us, spdk_msg_fn cb, void *cb_ctx)
    6218                 :            : {
    6219                 :            :         struct set_nvme_hotplug_ctx *ctx;
    6220                 :            : 
    6221   [ +  +  -  +  :        292 :         if (enabled == true && !spdk_process_is_primary()) {
                   #  # ]
    6222                 :          0 :                 return -EPERM;
    6223                 :            :         }
    6224                 :            : 
    6225                 :        292 :         ctx = calloc(1, sizeof(*ctx));
    6226         [ +  + ]:        292 :         if (ctx == NULL) {
    6227                 :          0 :                 return -ENOMEM;
    6228                 :            :         }
    6229                 :            : 
    6230         [ +  + ]:        292 :         period_us = period_us == 0 ? NVME_HOTPLUG_POLL_PERIOD_DEFAULT : period_us;
    6231   [ +  -  -  +  :        292 :         ctx->period_us = spdk_min(period_us, NVME_HOTPLUG_POLL_PERIOD_MAX);
                   -  + ]
    6232   [ -  +  -  +  :        292 :         ctx->enabled = enabled;
                   -  + ]
    6233   [ -  +  -  + ]:        292 :         ctx->fn = cb;
    6234   [ -  +  -  + ]:        292 :         ctx->fn_ctx = cb_ctx;
    6235                 :            : 
    6236                 :        292 :         spdk_thread_send_msg(g_bdev_nvme_init_thread, set_nvme_hotplug_period_cb, ctx);
    6237                 :        292 :         return 0;
    6238                 :         41 : }
    6239                 :            : 
    6240                 :            : static void
    6241                 :        911 : nvme_ctrlr_populate_namespaces_done(struct nvme_ctrlr *nvme_ctrlr,
    6242                 :            :                                     struct nvme_async_probe_ctx *ctx)
    6243                 :            : {
    6244                 :            :         struct nvme_ns  *nvme_ns;
    6245                 :            :         struct nvme_bdev        *nvme_bdev;
    6246                 :            :         size_t                  j;
    6247                 :            : 
    6248   [ +  +  #  # ]:        911 :         assert(nvme_ctrlr != NULL);
    6249                 :            : 
    6250   [ +  +  +  -  :        911 :         if (ctx->names == NULL) {
                   +  - ]
    6251   [ #  #  #  # ]:         21 :                 ctx->reported_bdevs = 0;
    6252                 :         21 :                 populate_namespaces_cb(ctx, 0);
    6253                 :         21 :                 return;
    6254                 :            :         }
    6255                 :            : 
    6256                 :            :         /*
    6257                 :            :          * Report the new bdevs that were created in this call.
    6258                 :            :          * There can be more than one bdev per NVMe controller.
    6259                 :            :          */
    6260                 :        890 :         j = 0;
    6261                 :        890 :         nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
    6262         [ +  + ]:       1801 :         while (nvme_ns != NULL) {
    6263   [ +  -  +  - ]:        911 :                 nvme_bdev = nvme_ns->bdev;
    6264   [ +  -  +  -  :        911 :                 if (j < ctx->max_bdevs) {
                   +  - ]
    6265   [ +  -  +  -  :        911 :                         ctx->names[j] = nvme_bdev->disk.name;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    6266                 :        911 :                         j++;
    6267                 :         60 :                 } else {
    6268   [ #  #  #  #  :          0 :                         NVME_CTRLR_ERRLOG(nvme_ctrlr,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6269                 :            :                                           "Maximum number of namespaces supported per NVMe controller is %du. "
    6270                 :            :                                           "Unable to return all names of created bdevs\n",
    6271                 :            :                                           ctx->max_bdevs);
    6272   [ #  #  #  # ]:          0 :                         ctx->reported_bdevs = 0;
    6273                 :          0 :                         populate_namespaces_cb(ctx, -ERANGE);
    6274                 :          0 :                         return;
    6275                 :            :                 }
    6276                 :            : 
    6277                 :        911 :                 nvme_ns = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns);
    6278                 :            :         }
    6279                 :            : 
    6280   [ +  -  +  - ]:        890 :         ctx->reported_bdevs = j;
    6281                 :        890 :         populate_namespaces_cb(ctx, 0);
    6282                 :         58 : }
    6283                 :            : 
    6284                 :            : static int
    6285                 :         40 : bdev_nvme_check_secondary_trid(struct nvme_ctrlr *nvme_ctrlr,
    6286                 :            :                                struct spdk_nvme_ctrlr *new_ctrlr,
    6287                 :            :                                struct spdk_nvme_transport_id *trid)
    6288                 :            : {
    6289                 :            :         struct nvme_path_id *tmp_trid;
    6290                 :            : 
    6291   [ -  +  #  #  :         40 :         if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
                   #  # ]
    6292   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "PCIe failover is not supported.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    6293                 :          0 :                 return -ENOTSUP;
    6294                 :            :         }
    6295                 :            : 
    6296                 :            :         /* Currently we only support failover to the same transport type. */
    6297   [ -  +  #  #  :         40 :         if (nvme_ctrlr->active_path_id->trid.trtype != trid->trtype) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6298   [ #  #  #  #  :          0 :                 NVME_CTRLR_WARNLOG(nvme_ctrlr,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6299                 :            :                                    "Failover from trtype: %s to a different trtype: %s is not supported currently\n",
    6300                 :            :                                    spdk_nvme_transport_id_trtype_str(nvme_ctrlr->active_path_id->trid.trtype),
    6301                 :            :                                    spdk_nvme_transport_id_trtype_str(trid->trtype));
    6302                 :          0 :                 return -EINVAL;
    6303                 :            :         }
    6304                 :            : 
    6305                 :            : 
    6306                 :            :         /* Currently we only support failover to the same NQN. */
    6307   [ -  +  -  +  :         40 :         if (strncmp(trid->subnqn, nvme_ctrlr->active_path_id->trid.subnqn, SPDK_NVMF_NQN_MAX_LEN)) {
          -  +  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6308   [ #  #  #  #  :          0 :                 NVME_CTRLR_WARNLOG(nvme_ctrlr,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    6309                 :            :                                    "Failover from subnqn: %s to a different subnqn: %s is not supported currently\n",
    6310                 :            :                                    nvme_ctrlr->active_path_id->trid.subnqn, trid->subnqn);
    6311                 :          0 :                 return -EINVAL;
    6312                 :            :         }
    6313                 :            : 
    6314                 :            :         /* Skip all the other checks if we've already registered this path. */
    6315   [ +  +  #  #  :         94 :         TAILQ_FOREACH(tmp_trid, &nvme_ctrlr->trids, link) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6316   [ +  +  #  # ]:         54 :                 if (!spdk_nvme_transport_id_compare(&tmp_trid->trid, trid)) {
    6317   [ #  #  #  #  :          0 :                         NVME_CTRLR_WARNLOG(nvme_ctrlr, "This path (traddr: %s subnqn: %s) is already registered\n",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6318                 :            :                                            trid->traddr, trid->subnqn);
    6319                 :          0 :                         return -EALREADY;
    6320                 :            :                 }
    6321                 :         12 :         }
    6322                 :            : 
    6323                 :         40 :         return 0;
    6324                 :          9 : }
    6325                 :            : 
    6326                 :            : static int
    6327                 :         40 : bdev_nvme_check_secondary_namespace(struct nvme_ctrlr *nvme_ctrlr,
    6328                 :            :                                     struct spdk_nvme_ctrlr *new_ctrlr)
    6329                 :            : {
    6330                 :            :         struct nvme_ns *nvme_ns;
    6331                 :            :         struct spdk_nvme_ns *new_ns;
    6332                 :            : 
    6333                 :         40 :         nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
    6334         [ +  + ]:         44 :         while (nvme_ns != NULL) {
    6335   [ #  #  #  # ]:          4 :                 new_ns = spdk_nvme_ctrlr_get_ns(new_ctrlr, nvme_ns->id);
    6336   [ -  +  #  # ]:          4 :                 assert(new_ns != NULL);
    6337                 :            : 
    6338   [ -  +  #  #  :          4 :                 if (!bdev_nvme_compare_ns(nvme_ns->ns, new_ns)) {
                   #  # ]
    6339                 :          0 :                         return -EINVAL;
    6340                 :            :                 }
    6341                 :            : 
    6342                 :          4 :                 nvme_ns = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns);
    6343                 :            :         }
    6344                 :            : 
    6345                 :         40 :         return 0;
    6346                 :          9 : }
    6347                 :            : 
    6348                 :            : static int
    6349                 :         40 : _bdev_nvme_add_secondary_trid(struct nvme_ctrlr *nvme_ctrlr,
    6350                 :            :                               struct spdk_nvme_transport_id *trid)
    6351                 :            : {
    6352                 :            :         struct nvme_path_id *active_id, *new_trid, *tmp_trid;
    6353                 :            : 
    6354                 :         40 :         new_trid = calloc(1, sizeof(*new_trid));
    6355         [ +  + ]:         40 :         if (new_trid == NULL) {
    6356                 :          0 :                 return -ENOMEM;
    6357                 :            :         }
    6358         [ #  # ]:         40 :         new_trid->trid = *trid;
    6359                 :            : 
    6360   [ #  #  #  # ]:         40 :         active_id = nvme_ctrlr->active_path_id;
    6361   [ +  +  #  # ]:         40 :         assert(active_id != NULL);
    6362   [ -  +  #  #  :         40 :         assert(active_id == TAILQ_FIRST(&nvme_ctrlr->trids));
          #  #  #  #  #  
                      # ]
    6363                 :            : 
    6364                 :            :         /* Skip the active trid not to replace it until it is failed. */
    6365   [ #  #  #  #  :         40 :         tmp_trid = TAILQ_NEXT(active_id, link);
                   #  # ]
    6366         [ +  + ]:         40 :         if (tmp_trid == NULL) {
    6367                 :         26 :                 goto add_tail;
    6368                 :            :         }
    6369                 :            : 
    6370                 :            :         /* It means the trid is faled if its last failed time is non-zero.
    6371                 :            :          * Insert the new alternate trid before any failed trid.
    6372                 :            :          */
    6373   [ -  +  +  +  :         23 :         TAILQ_FOREACH_FROM(tmp_trid, &nvme_ctrlr->trids, link) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6374   [ +  +  #  #  :         14 :                 if (tmp_trid->last_failed_tsc != 0) {
                   #  # ]
    6375   [ #  #  #  #  :          5 :                         TAILQ_INSERT_BEFORE(tmp_trid, new_trid, link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6376                 :          5 :                         return 0;
    6377                 :            :                 }
    6378                 :          4 :         }
    6379                 :            : 
    6380                 :          7 : add_tail:
    6381   [ #  #  #  #  :         35 :         TAILQ_INSERT_TAIL(&nvme_ctrlr->trids, new_trid, link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6382                 :         35 :         return 0;
    6383                 :          9 : }
    6384                 :            : 
    6385                 :            : /* This is the case that a secondary path is added to an existing
    6386                 :            :  * nvme_ctrlr for failover. After checking if it can access the same
    6387                 :            :  * namespaces as the primary path, it is disconnected until failover occurs.
    6388                 :            :  */
    6389                 :            : static int
    6390                 :         40 : bdev_nvme_add_secondary_trid(struct nvme_ctrlr *nvme_ctrlr,
    6391                 :            :                              struct spdk_nvme_ctrlr *new_ctrlr,
    6392                 :            :                              struct spdk_nvme_transport_id *trid)
    6393                 :            : {
    6394                 :            :         int rc;
    6395                 :            : 
    6396   [ +  +  #  # ]:         40 :         assert(nvme_ctrlr != NULL);
    6397                 :            : 
    6398   [ -  +  #  # ]:         40 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    6399                 :            : 
    6400                 :         40 :         rc = bdev_nvme_check_secondary_trid(nvme_ctrlr, new_ctrlr, trid);
    6401         [ -  + ]:         40 :         if (rc != 0) {
    6402                 :          0 :                 goto exit;
    6403                 :            :         }
    6404                 :            : 
    6405                 :         40 :         rc = bdev_nvme_check_secondary_namespace(nvme_ctrlr, new_ctrlr);
    6406         [ -  + ]:         40 :         if (rc != 0) {
    6407                 :          0 :                 goto exit;
    6408                 :            :         }
    6409                 :            : 
    6410                 :         40 :         rc = _bdev_nvme_add_secondary_trid(nvme_ctrlr, trid);
    6411                 :            : 
    6412                 :         31 : exit:
    6413   [ -  +  #  # ]:         40 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    6414                 :            : 
    6415                 :         40 :         spdk_nvme_detach(new_ctrlr);
    6416                 :            : 
    6417                 :         40 :         return rc;
    6418                 :            : }
    6419                 :            : 
    6420                 :            : static void
    6421                 :        915 : connect_attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
    6422                 :            :                   struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
    6423                 :            : {
    6424                 :        915 :         struct spdk_nvme_ctrlr_opts *user_opts = cb_ctx;
    6425                 :            :         struct nvme_async_probe_ctx *ctx;
    6426                 :            :         int rc;
    6427                 :            : 
    6428                 :        915 :         ctx = SPDK_CONTAINEROF(user_opts, struct nvme_async_probe_ctx, drv_opts);
    6429   [ +  -  +  - ]:        915 :         ctx->ctrlr_attached = true;
    6430                 :            : 
    6431   [ +  -  +  -  :        915 :         rc = nvme_ctrlr_create(ctrlr, ctx->base_name, &ctx->trid, ctx);
                   +  - ]
    6432         [ +  + ]:        915 :         if (rc != 0) {
    6433   [ #  #  #  # ]:          4 :                 ctx->reported_bdevs = 0;
    6434                 :          4 :                 populate_namespaces_cb(ctx, rc);
    6435                 :          1 :         }
    6436                 :        915 : }
    6437                 :            : 
    6438                 :            : 
    6439                 :            : static void
    6440                 :         20 : connect_set_failover_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
    6441                 :            :                         struct spdk_nvme_ctrlr *ctrlr,
    6442                 :            :                         const struct spdk_nvme_ctrlr_opts *opts)
    6443                 :            : {
    6444                 :         20 :         struct spdk_nvme_ctrlr_opts *user_opts = cb_ctx;
    6445                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    6446                 :            :         struct nvme_async_probe_ctx *ctx;
    6447                 :            :         int rc;
    6448                 :            : 
    6449                 :         20 :         ctx = SPDK_CONTAINEROF(user_opts, struct nvme_async_probe_ctx, drv_opts);
    6450   [ #  #  #  # ]:         20 :         ctx->ctrlr_attached = true;
    6451                 :            : 
    6452   [ #  #  #  # ]:         20 :         nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->base_name);
    6453         [ +  - ]:         20 :         if (nvme_ctrlr) {
    6454         [ #  # ]:         20 :                 rc = bdev_nvme_add_secondary_trid(nvme_ctrlr, ctrlr, &ctx->trid);
    6455                 :          4 :         } else {
    6456                 :          0 :                 rc = -ENODEV;
    6457                 :            :         }
    6458                 :            : 
    6459   [ #  #  #  # ]:         20 :         ctx->reported_bdevs = 0;
    6460                 :         20 :         populate_namespaces_cb(ctx, rc);
    6461                 :         20 : }
    6462                 :            : 
    6463                 :            : static int
    6464                 :     142391 : bdev_nvme_async_poll(void *arg)
    6465                 :            : {
    6466                 :     142391 :         struct nvme_async_probe_ctx     *ctx = arg;
    6467                 :            :         int                             rc;
    6468                 :            : 
    6469   [ +  -  +  - ]:     142391 :         rc = spdk_nvme_probe_poll_async(ctx->probe_ctx);
    6470         [ +  + ]:     142391 :         if (spdk_unlikely(rc != -EAGAIN)) {
    6471   [ +  -  +  - ]:        954 :                 ctx->probe_done = true;
    6472         [ +  - ]:        954 :                 spdk_poller_unregister(&ctx->poller);
    6473   [ +  +  +  +  :        954 :                 if (!ctx->ctrlr_attached) {
             +  -  +  - ]
    6474                 :            :                         /* The probe is done, but no controller was attached.
    6475                 :            :                          * That means we had a failure, so report -EIO back to
    6476                 :            :                          * the caller (usually the RPC). populate_namespaces_cb()
    6477                 :            :                          * will take care of freeing the nvme_async_probe_ctx.
    6478                 :            :                          */
    6479   [ #  #  #  # ]:         19 :                         ctx->reported_bdevs = 0;
    6480                 :         19 :                         populate_namespaces_cb(ctx, -EIO);
    6481   [ +  +  +  +  :        936 :                 } else if (ctx->namespaces_populated) {
             +  -  -  + ]
    6482                 :            :                         /* The namespaces for the attached controller were all
    6483                 :            :                          * populated and the response was already sent to the
    6484                 :            :                          * caller (usually the RPC).  So free the context here.
    6485                 :            :                          */
    6486                 :        728 :                         free_nvme_async_probe_ctx(ctx);
    6487                 :         32 :                 }
    6488                 :         64 :         }
    6489                 :            : 
    6490                 :     142391 :         return SPDK_POLLER_BUSY;
    6491                 :            : }
    6492                 :            : 
    6493                 :            : static bool
    6494                 :       1503 : bdev_nvme_check_io_error_resiliency_params(int32_t ctrlr_loss_timeout_sec,
    6495                 :            :                 uint32_t reconnect_delay_sec,
    6496                 :            :                 uint32_t fast_io_fail_timeout_sec)
    6497                 :            : {
    6498         [ +  + ]:       1503 :         if (ctrlr_loss_timeout_sec < -1) {
    6499                 :          4 :                 SPDK_ERRLOG("ctrlr_loss_timeout_sec can't be less than -1.\n");
    6500                 :          4 :                 return false;
    6501         [ +  + ]:       1499 :         } else if (ctrlr_loss_timeout_sec == -1) {
    6502         [ +  + ]:         60 :                 if (reconnect_delay_sec == 0) {
    6503                 :          4 :                         SPDK_ERRLOG("reconnect_delay_sec can't be 0 if ctrlr_loss_timeout_sec is not 0.\n");
    6504                 :          4 :                         return false;
    6505   [ +  +  +  + ]:         56 :                 } else if (fast_io_fail_timeout_sec != 0 &&
    6506                 :          3 :                            fast_io_fail_timeout_sec < reconnect_delay_sec) {
    6507                 :          4 :                         SPDK_ERRLOG("reconnect_delay_sec can't be more than fast_io-fail_timeout_sec.\n");
    6508                 :          4 :                         return false;
    6509                 :            :                 }
    6510         [ +  + ]:       1451 :         } else if (ctrlr_loss_timeout_sec != 0) {
    6511         [ +  + ]:         53 :                 if (reconnect_delay_sec == 0) {
    6512                 :          4 :                         SPDK_ERRLOG("reconnect_delay_sec can't be 0 if ctrlr_loss_timeout_sec is not 0.\n");
    6513                 :          4 :                         return false;
    6514         [ +  + ]:         49 :                 } else if (reconnect_delay_sec > (uint32_t)ctrlr_loss_timeout_sec) {
    6515                 :          4 :                         SPDK_ERRLOG("reconnect_delay_sec can't be more than ctrlr_loss_timeout_sec.\n");
    6516                 :          4 :                         return false;
    6517         [ +  + ]:         45 :                 } else if (fast_io_fail_timeout_sec != 0) {
    6518         [ +  + ]:         27 :                         if (fast_io_fail_timeout_sec < reconnect_delay_sec) {
    6519                 :          4 :                                 SPDK_ERRLOG("reconnect_delay_sec can't be more than fast_io_fail_timeout_sec.\n");
    6520                 :          4 :                                 return false;
    6521         [ +  + ]:         23 :                         } else if (fast_io_fail_timeout_sec > (uint32_t)ctrlr_loss_timeout_sec) {
    6522                 :          4 :                                 SPDK_ERRLOG("fast_io_fail_timeout_sec can't be more than ctrlr_loss_timeout_sec.\n");
    6523                 :          4 :                                 return false;
    6524                 :            :                         }
    6525                 :          4 :                 }
    6526   [ +  +  +  + ]:       1393 :         } else if (reconnect_delay_sec != 0 || fast_io_fail_timeout_sec != 0) {
    6527                 :          8 :                 SPDK_ERRLOG("Both reconnect_delay_sec and fast_io_fail_timeout_sec must be 0 if ctrlr_loss_timeout_sec is 0.\n");
    6528                 :          8 :                 return false;
    6529                 :            :         }
    6530                 :            : 
    6531                 :       1467 :         return true;
    6532                 :        126 : }
    6533                 :            : 
    6534                 :            : int
    6535                 :        957 : spdk_bdev_nvme_create(struct spdk_nvme_transport_id *trid,
    6536                 :            :                       const char *base_name,
    6537                 :            :                       const char **names,
    6538                 :            :                       uint32_t count,
    6539                 :            :                       spdk_bdev_nvme_create_cb cb_fn,
    6540                 :            :                       void *cb_ctx,
    6541                 :            :                       struct spdk_nvme_ctrlr_opts *drv_opts,
    6542                 :            :                       struct spdk_bdev_nvme_ctrlr_opts *bdev_opts)
    6543                 :            : {
    6544                 :            :         struct nvme_probe_skip_entry *entry, *tmp;
    6545                 :            :         struct nvme_async_probe_ctx *ctx;
    6546                 :            :         spdk_nvme_attach_cb attach_cb;
    6547                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    6548                 :            :         int len;
    6549                 :            : 
    6550                 :            :         /* TODO expand this check to include both the host and target TRIDs.
    6551                 :            :          * Only if both are the same should we fail.
    6552                 :            :          */
    6553   [ +  +  -  + ]:        957 :         if (nvme_ctrlr_get(trid, drv_opts->hostnqn) != NULL) {
    6554   [ #  #  #  # ]:          0 :                 SPDK_ERRLOG("A controller with the provided trid (traddr: %s, hostnqn: %s) "
    6555                 :            :                             "already exists.\n", trid->traddr, drv_opts->hostnqn);
    6556                 :          0 :                 return -EEXIST;
    6557                 :            :         }
    6558                 :            : 
    6559         [ +  + ]:        957 :         len = strnlen(base_name, SPDK_CONTROLLER_NAME_MAX);
    6560                 :            : 
    6561   [ +  -  -  + ]:        957 :         if (len == 0 || len == SPDK_CONTROLLER_NAME_MAX) {
    6562                 :          0 :                 SPDK_ERRLOG("controller name must be between 1 and %d characters\n", SPDK_CONTROLLER_NAME_MAX - 1);
    6563                 :          0 :                 return -EINVAL;
    6564                 :            :         }
    6565                 :            : 
    6566   [ +  -  +  - ]:        958 :         if (bdev_opts != NULL &&
    6567   [ +  +  +  - ]:       1021 :             !bdev_nvme_check_io_error_resiliency_params(bdev_opts->ctrlr_loss_timeout_sec,
    6568   [ +  -  +  - ]:         64 :                             bdev_opts->reconnect_delay_sec,
    6569   [ +  -  +  - ]:         64 :                             bdev_opts->fast_io_fail_timeout_sec)) {
    6570                 :          0 :                 return -EINVAL;
    6571                 :            :         }
    6572                 :            : 
    6573                 :        957 :         ctx = calloc(1, sizeof(*ctx));
    6574         [ +  + ]:        957 :         if (!ctx) {
    6575                 :          0 :                 return -ENOMEM;
    6576                 :            :         }
    6577   [ +  -  +  - ]:        957 :         ctx->base_name = base_name;
    6578   [ +  -  +  - ]:        957 :         ctx->names = names;
    6579   [ +  -  +  - ]:        957 :         ctx->max_bdevs = count;
    6580   [ +  -  +  - ]:        957 :         ctx->cb_fn = cb_fn;
    6581   [ +  -  +  - ]:        957 :         ctx->cb_ctx = cb_ctx;
    6582         [ +  - ]:        957 :         ctx->trid = *trid;
    6583                 :            : 
    6584         [ +  + ]:        957 :         if (bdev_opts) {
    6585   [ +  +  +  +  :        957 :                 memcpy(&ctx->bdev_opts, bdev_opts, sizeof(*bdev_opts));
                   +  - ]
    6586                 :         64 :         } else {
    6587         [ #  # ]:          0 :                 spdk_bdev_nvme_get_default_ctrlr_opts(&ctx->bdev_opts);
    6588                 :            :         }
    6589                 :            : 
    6590   [ +  +  +  -  :        957 :         if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
                   -  + ]
    6591   [ +  +  +  -  :        472 :                 TAILQ_FOREACH_SAFE(entry, &g_skipped_nvme_ctrlrs, tailq, tmp) {
          #  #  #  #  +  
                      - ]
    6592   [ +  -  #  # ]:          2 :                         if (spdk_nvme_transport_id_compare(trid, &entry->trid) == 0) {
    6593   [ -  +  #  #  :          2 :                                 TAILQ_REMOVE(&g_skipped_nvme_ctrlrs, entry, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    6594                 :          2 :                                 free(entry);
    6595                 :          2 :                                 break;
    6596                 :            :                         }
    6597                 :          0 :                 }
    6598                 :         11 :         }
    6599                 :            : 
    6600   [ +  +  +  +  :        957 :         memcpy(&ctx->drv_opts, drv_opts, sizeof(*drv_opts));
                   +  - ]
    6601   [ +  -  +  -  :        957 :         ctx->drv_opts.transport_retry_count = g_opts.transport_retry_count;
             +  -  +  - ]
    6602   [ +  -  +  -  :        957 :         ctx->drv_opts.transport_ack_timeout = g_opts.transport_ack_timeout;
             +  -  +  - ]
    6603   [ +  -  +  -  :        957 :         ctx->drv_opts.keep_alive_timeout_ms = g_opts.keep_alive_timeout_ms;
             +  -  +  - ]
    6604   [ +  -  +  -  :        957 :         ctx->drv_opts.disable_read_ana_log_page = true;
                   +  - ]
    6605   [ +  -  +  -  :        957 :         ctx->drv_opts.transport_tos = g_opts.transport_tos;
             +  -  +  - ]
    6606                 :            : 
    6607         [ +  + ]:        957 :         if (spdk_interrupt_mode_is_enabled()) {
    6608   [ #  #  #  #  :          0 :                 if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
                   #  # ]
    6609   [ #  #  #  #  :          0 :                         ctx->drv_opts.enable_interrupts = true;
                   #  # ]
    6610                 :          0 :                 } else {
    6611                 :          0 :                         SPDK_ERRLOG("Interrupt mode is only supported with PCIe transport\n");
    6612                 :          0 :                         free_nvme_async_probe_ctx(ctx);
    6613                 :          0 :                         return -ENOTSUP;
    6614                 :            :                 }
    6615                 :          0 :         }
    6616                 :            : 
    6617   [ +  +  +  -  :        957 :         if (ctx->bdev_opts.psk != NULL) {
             +  -  +  - ]
    6618   [ #  #  #  #  :         21 :                 ctx->drv_opts.tls_psk = spdk_keyring_get_key(ctx->bdev_opts.psk);
          #  #  #  #  #  
                #  #  # ]
    6619   [ +  +  #  #  :         21 :                 if (ctx->drv_opts.tls_psk == NULL) {
             #  #  #  # ]
    6620   [ #  #  #  #  :          2 :                         SPDK_ERRLOG("Could not load PSK: %s\n", ctx->bdev_opts.psk);
                   #  # ]
    6621                 :          2 :                         free_nvme_async_probe_ctx(ctx);
    6622                 :          2 :                         return -ENOKEY;
    6623                 :            :                 }
    6624                 :          0 :         }
    6625                 :            : 
    6626   [ +  +  +  -  :        955 :         if (ctx->bdev_opts.dhchap_key != NULL) {
             +  -  +  - ]
    6627   [ #  #  #  #  :        166 :                 ctx->drv_opts.dhchap_key = spdk_keyring_get_key(ctx->bdev_opts.dhchap_key);
          #  #  #  #  #  
                #  #  # ]
    6628   [ -  +  #  #  :        166 :                 if (ctx->drv_opts.dhchap_key == NULL) {
             #  #  #  # ]
    6629   [ #  #  #  #  :          0 :                         SPDK_ERRLOG("Could not load DH-HMAC-CHAP key: %s\n",
                   #  # ]
    6630                 :            :                                     ctx->bdev_opts.dhchap_key);
    6631                 :          0 :                         free_nvme_async_probe_ctx(ctx);
    6632                 :          0 :                         return -ENOKEY;
    6633                 :            :                 }
    6634                 :            : 
    6635   [ #  #  #  #  :        166 :                 ctx->drv_opts.dhchap_digests = g_opts.dhchap_digests;
             #  #  #  # ]
    6636   [ #  #  #  #  :        166 :                 ctx->drv_opts.dhchap_dhgroups = g_opts.dhchap_dhgroups;
             #  #  #  # ]
    6637                 :          0 :         }
    6638   [ +  +  +  -  :        955 :         if (ctx->bdev_opts.dhchap_ctrlr_key != NULL) {
             +  -  +  - ]
    6639   [ #  #  #  #  :        125 :                 ctx->drv_opts.dhchap_ctrlr_key =
                   #  # ]
    6640   [ #  #  #  #  :        125 :                         spdk_keyring_get_key(ctx->bdev_opts.dhchap_ctrlr_key);
                   #  # ]
    6641   [ -  +  #  #  :        125 :                 if (ctx->drv_opts.dhchap_ctrlr_key == NULL) {
             #  #  #  # ]
    6642   [ #  #  #  #  :          0 :                         SPDK_ERRLOG("Could not load DH-HMAC-CHAP controller key: %s\n",
                   #  # ]
    6643                 :            :                                     ctx->bdev_opts.dhchap_ctrlr_key);
    6644                 :          0 :                         free_nvme_async_probe_ctx(ctx);
    6645                 :          0 :                         return -ENOKEY;
    6646                 :            :                 }
    6647                 :          0 :         }
    6648                 :            : 
    6649   [ +  +  +  +  :        955 :         if (nvme_bdev_ctrlr_get_by_name(base_name) == NULL || ctx->bdev_opts.multipath) {
          +  +  #  #  #  
                #  #  # ]
    6650                 :        935 :                 attach_cb = connect_attach_cb;
    6651                 :         60 :         } else {
    6652                 :         20 :                 attach_cb = connect_set_failover_cb;
    6653                 :            :         }
    6654                 :            : 
    6655   [ +  -  +  - ]:        955 :         nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->base_name);
    6656   [ +  +  +  +  :        955 :         if (nvme_ctrlr  && nvme_ctrlr->opts.multipath != ctx->bdev_opts.multipath) {
          -  +  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    6657                 :            :                 /* All controllers with the same name must be configured the same
    6658                 :            :                  * way, either for multipath or failover. If the configuration doesn't
    6659                 :            :                  * match - report error.
    6660                 :            :                  */
    6661                 :          0 :                 free_nvme_async_probe_ctx(ctx);
    6662                 :          0 :                 return -EINVAL;
    6663                 :            :         }
    6664                 :            : 
    6665   [ +  -  +  -  :        955 :         ctx->probe_ctx = spdk_nvme_connect_async(trid, &ctx->drv_opts, attach_cb);
                   +  - ]
    6666   [ +  +  +  -  :        955 :         if (ctx->probe_ctx == NULL) {
                   +  - ]
    6667         [ #  # ]:          1 :                 SPDK_ERRLOG("No controller was found with provided trid (traddr: %s)\n", trid->traddr);
    6668                 :          1 :                 free_nvme_async_probe_ctx(ctx);
    6669                 :          1 :                 return -ENODEV;
    6670                 :            :         }
    6671   [ +  -  +  - ]:        954 :         ctx->poller = SPDK_POLLER_REGISTER(bdev_nvme_async_poll, ctx, 1000);
    6672                 :            : 
    6673                 :        954 :         return 0;
    6674                 :         64 : }
    6675                 :            : 
    6676                 :            : struct bdev_nvme_delete_ctx {
    6677                 :            :         char                        *name;
    6678                 :            :         struct nvme_path_id         path_id;
    6679                 :            :         bdev_nvme_delete_done_fn    delete_done;
    6680                 :            :         void                        *delete_done_ctx;
    6681                 :            :         uint64_t                    timeout_ticks;
    6682                 :            :         struct spdk_poller          *poller;
    6683                 :            : };
    6684                 :            : 
    6685                 :            : static void
    6686                 :        212 : free_bdev_nvme_delete_ctx(struct bdev_nvme_delete_ctx *ctx)
    6687                 :            : {
    6688         [ +  + ]:        212 :         if (ctx != NULL) {
    6689   [ #  #  #  # ]:        208 :                 free(ctx->name);
    6690                 :        208 :                 free(ctx);
    6691                 :          4 :         }
    6692                 :        212 : }
    6693                 :            : 
    6694                 :            : static bool
    6695                 :      30420 : nvme_path_id_compare(struct nvme_path_id *p, const struct nvme_path_id *path_id)
    6696                 :            : {
    6697   [ +  +  #  #  :      30420 :         if (path_id->trid.trtype != 0) {
             #  #  #  # ]
    6698   [ -  +  #  #  :        122 :                 if (path_id->trid.trtype == SPDK_NVME_TRANSPORT_CUSTOM) {
             #  #  #  # ]
    6699   [ #  #  #  #  :          0 :                         if (strcasecmp(path_id->trid.trstring, p->trid.trstring) != 0) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6700                 :          0 :                                 return false;
    6701                 :            :                         }
    6702                 :          0 :                 } else {
    6703   [ -  +  #  #  :        122 :                         if (path_id->trid.trtype != p->trid.trtype) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6704                 :          0 :                                 return false;
    6705                 :            :                         }
    6706                 :            :                 }
    6707                 :         21 :         }
    6708                 :            : 
    6709   [ +  +  #  #  :      30420 :         if (!spdk_mem_all_zero(path_id->trid.traddr, sizeof(path_id->trid.traddr))) {
                   #  # ]
    6710   [ +  +  -  +  :        122 :                 if (strcasecmp(path_id->trid.traddr, p->trid.traddr) != 0) {
          +  +  #  #  #  
             #  #  #  #  
                      # ]
    6711                 :         44 :                         return false;
    6712                 :            :                 }
    6713                 :         10 :         }
    6714                 :            : 
    6715   [ +  +  #  #  :      30376 :         if (path_id->trid.adrfam != 0) {
             #  #  #  # ]
    6716   [ -  +  #  #  :         38 :                 if (path_id->trid.adrfam != p->trid.adrfam) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6717                 :          0 :                         return false;
    6718                 :            :                 }
    6719                 :          0 :         }
    6720                 :            : 
    6721   [ +  +  #  #  :      30376 :         if (!spdk_mem_all_zero(path_id->trid.trsvcid, sizeof(path_id->trid.trsvcid))) {
                   #  # ]
    6722   [ -  +  -  +  :         78 :                 if (strcasecmp(path_id->trid.trsvcid, p->trid.trsvcid) != 0) {
          +  +  #  #  #  
             #  #  #  #  
                      # ]
    6723                 :          7 :                         return false;
    6724                 :            :                 }
    6725                 :         10 :         }
    6726                 :            : 
    6727   [ +  +  #  #  :      30369 :         if (!spdk_mem_all_zero(path_id->trid.subnqn, sizeof(path_id->trid.subnqn))) {
                   #  # ]
    6728   [ -  +  -  +  :         71 :                 if (strcmp(path_id->trid.subnqn, p->trid.subnqn) != 0) {
          -  +  #  #  #  
             #  #  #  #  
                      # ]
    6729                 :          0 :                         return false;
    6730                 :            :                 }
    6731                 :         10 :         }
    6732                 :            : 
    6733   [ +  +  #  #  :      30369 :         if (!spdk_mem_all_zero(path_id->hostid.hostaddr, sizeof(path_id->hostid.hostaddr))) {
                   #  # ]
    6734   [ #  #  #  #  :          0 :                 if (strcmp(path_id->hostid.hostaddr, p->hostid.hostaddr) != 0) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6735                 :          0 :                         return false;
    6736                 :            :                 }
    6737                 :          0 :         }
    6738                 :            : 
    6739   [ +  +  #  #  :      30369 :         if (!spdk_mem_all_zero(path_id->hostid.hostsvcid, sizeof(path_id->hostid.hostsvcid))) {
                   #  # ]
    6740   [ #  #  #  #  :          0 :                 if (strcmp(path_id->hostid.hostsvcid, p->hostid.hostsvcid) != 0) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6741                 :          0 :                         return false;
    6742                 :            :                 }
    6743                 :          0 :         }
    6744                 :            : 
    6745                 :      30369 :         return true;
    6746                 :         94 : }
    6747                 :            : 
    6748                 :            : static bool
    6749                 :      30097 : nvme_path_id_exists(const char *name, const struct nvme_path_id *path_id)
    6750                 :            : {
    6751                 :            :         struct nvme_bdev_ctrlr  *nbdev_ctrlr;
    6752                 :            :         struct nvme_ctrlr       *ctrlr;
    6753                 :            :         struct nvme_path_id     *p;
    6754                 :            : 
    6755         [ -  + ]:      30097 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    6756                 :      30097 :         nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
    6757         [ +  + ]:      30097 :         if (!nbdev_ctrlr) {
    6758         [ -  + ]:        206 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6759                 :        206 :                 return false;
    6760                 :            :         }
    6761                 :            : 
    6762   [ +  +  #  #  :      29893 :         TAILQ_FOREACH(ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6763   [ -  +  #  # ]:      29891 :                 pthread_mutex_lock(&ctrlr->mutex);
    6764   [ +  +  #  #  :      29894 :                 TAILQ_FOREACH(p, &ctrlr->trids, link) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6765         [ +  + ]:      29892 :                         if (nvme_path_id_compare(p, path_id)) {
    6766   [ -  +  #  # ]:      29889 :                                 pthread_mutex_unlock(&ctrlr->mutex);
    6767         [ -  + ]:      29889 :                                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6768                 :      29889 :                                 return true;
    6769                 :            :                         }
    6770                 :          0 :                 }
    6771   [ -  +  #  # ]:          2 :                 pthread_mutex_unlock(&ctrlr->mutex);
    6772                 :          0 :         }
    6773         [ -  + ]:          2 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6774                 :            : 
    6775                 :          2 :         return false;
    6776                 :         20 : }
    6777                 :            : 
    6778                 :            : static int
    6779                 :      30097 : bdev_nvme_delete_complete_poll(void *arg)
    6780                 :            : {
    6781                 :      30097 :         struct bdev_nvme_delete_ctx     *ctx = arg;
    6782                 :      30097 :         int                             rc = 0;
    6783                 :            : 
    6784   [ +  +  #  #  :      30097 :         if (nvme_path_id_exists(ctx->name, &ctx->path_id)) {
             #  #  #  # ]
    6785   [ +  -  #  #  :      29889 :                 if (ctx->timeout_ticks > spdk_get_ticks()) {
                   #  # ]
    6786                 :      29889 :                         return SPDK_POLLER_BUSY;
    6787                 :            :                 }
    6788                 :            : 
    6789   [ #  #  #  # ]:          0 :                 SPDK_ERRLOG("NVMe path '%s' still exists after delete\n", ctx->name);
    6790                 :          0 :                 rc = -ETIMEDOUT;
    6791                 :          0 :         }
    6792                 :            : 
    6793         [ #  # ]:        208 :         spdk_poller_unregister(&ctx->poller);
    6794                 :            : 
    6795   [ #  #  #  #  :        208 :         ctx->delete_done(ctx->delete_done_ctx, rc);
          #  #  #  #  #  
                #  #  # ]
    6796                 :        208 :         free_bdev_nvme_delete_ctx(ctx);
    6797                 :            : 
    6798                 :        208 :         return SPDK_POLLER_BUSY;
    6799                 :         20 : }
    6800                 :            : 
    6801                 :            : static int
    6802                 :        485 : _bdev_nvme_delete(struct nvme_ctrlr *nvme_ctrlr, const struct nvme_path_id *path_id)
    6803                 :            : {
    6804                 :            :         struct nvme_path_id     *p, *t;
    6805                 :            :         spdk_msg_fn             msg_fn;
    6806                 :        485 :         int                     rc = -ENXIO;
    6807                 :            : 
    6808   [ -  +  #  # ]:        485 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    6809                 :            : 
    6810   [ +  +  -  +  :        528 :         TAILQ_FOREACH_REVERSE_SAFE(p, &nvme_ctrlr->trids, nvme_paths, link, t) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    6811   [ +  +  #  #  :        528 :                 if (p == TAILQ_FIRST(&nvme_ctrlr->trids)) {
             #  #  #  # ]
    6812                 :        485 :                         break;
    6813                 :            :                 }
    6814                 :            : 
    6815         [ +  + ]:         43 :                 if (!nvme_path_id_compare(p, path_id)) {
    6816                 :         14 :                         continue;
    6817                 :            :                 }
    6818                 :            : 
    6819                 :            :                 /* We are not using the specified path. */
    6820   [ +  +  #  #  :         29 :                 TAILQ_REMOVE(&nvme_ctrlr->trids, p, link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    6821                 :         29 :                 free(p);
    6822                 :         29 :                 rc = 0;
    6823                 :          7 :         }
    6824                 :            : 
    6825   [ +  -  +  + ]:        485 :         if (p == NULL || !nvme_path_id_compare(p, path_id)) {
    6826   [ -  +  #  # ]:         34 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    6827                 :         34 :                 return rc;
    6828                 :            :         }
    6829                 :            : 
    6830                 :            :         /* If we made it here, then this path is a match! Now we need to remove it. */
    6831                 :            : 
    6832                 :            :         /* This is the active path in use right now. The active path is always the first in the list. */
    6833   [ +  +  #  #  :        451 :         assert(p == nvme_ctrlr->active_path_id);
             #  #  #  # ]
    6834                 :            : 
    6835   [ +  +  #  #  :        451 :         if (!TAILQ_NEXT(p, link)) {
             #  #  #  # ]
    6836                 :            :                 /* The current path is the only path. */
    6837                 :        446 :                 msg_fn = _nvme_ctrlr_destruct;
    6838                 :        446 :                 rc = bdev_nvme_delete_ctrlr_unsafe(nvme_ctrlr, false);
    6839                 :         59 :         } else {
    6840                 :            :                 /* There is an alternative path. */
    6841                 :          5 :                 msg_fn = _bdev_nvme_reset_ctrlr;
    6842                 :          5 :                 rc = bdev_nvme_failover_ctrlr_unsafe(nvme_ctrlr, true);
    6843                 :            :         }
    6844                 :            : 
    6845   [ -  +  #  # ]:        451 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    6846                 :            : 
    6847         [ +  + ]:        451 :         if (rc == 0) {
    6848   [ #  #  #  # ]:        451 :                 spdk_thread_send_msg(nvme_ctrlr->thread, msg_fn, nvme_ctrlr);
    6849         [ #  # ]:         60 :         } else if (rc == -EALREADY) {
    6850                 :          0 :                 rc = 0;
    6851                 :          0 :         }
    6852                 :            : 
    6853                 :        451 :         return rc;
    6854                 :         68 : }
    6855                 :            : 
    6856                 :            : int
    6857                 :        424 : bdev_nvme_delete(const char *name, const struct nvme_path_id *path_id,
    6858                 :            :                  bdev_nvme_delete_done_fn delete_done, void *delete_done_ctx)
    6859                 :            : {
    6860                 :            :         struct nvme_bdev_ctrlr          *nbdev_ctrlr;
    6861                 :            :         struct nvme_ctrlr               *nvme_ctrlr, *tmp_nvme_ctrlr;
    6862                 :        424 :         struct bdev_nvme_delete_ctx     *ctx = NULL;
    6863                 :        424 :         int                             rc = -ENXIO, _rc;
    6864                 :            : 
    6865   [ +  -  -  + ]:        424 :         if (name == NULL || path_id == NULL) {
    6866                 :          0 :                 rc = -EINVAL;
    6867                 :          0 :                 goto exit;
    6868                 :            :         }
    6869                 :            : 
    6870         [ -  + ]:        424 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    6871                 :            : 
    6872                 :        424 :         nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
    6873         [ +  + ]:        424 :         if (nbdev_ctrlr == NULL) {
    6874         [ #  # ]:          0 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6875                 :            : 
    6876                 :          0 :                 SPDK_ERRLOG("Failed to find NVMe bdev controller\n");
    6877                 :          0 :                 rc = -ENODEV;
    6878                 :          0 :                 goto exit;
    6879                 :            :         }
    6880                 :            : 
    6881   [ +  +  +  +  :        909 :         TAILQ_FOREACH_SAFE(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq, tmp_nvme_ctrlr) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6882                 :        485 :                 _rc = _bdev_nvme_delete(nvme_ctrlr, path_id);
    6883   [ +  +  +  + ]:        485 :                 if (_rc < 0 && _rc != -ENXIO) {
    6884         [ #  # ]:          0 :                         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6885                 :          0 :                         rc = _rc;
    6886                 :          0 :                         goto exit;
    6887         [ +  + ]:        485 :                 } else if (_rc == 0) {
    6888                 :            :                         /* We traverse all remaining nvme_ctrlrs even if one nvme_ctrlr
    6889                 :            :                          * was deleted successfully. To remember the successful deletion,
    6890                 :            :                          * overwrite rc only if _rc is zero.
    6891                 :            :                          */
    6892                 :        460 :                         rc = 0;
    6893                 :         62 :                 }
    6894                 :         68 :         }
    6895                 :            : 
    6896         [ -  + ]:        424 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6897                 :            : 
    6898   [ +  +  +  + ]:        424 :         if (rc != 0 || delete_done == NULL) {
    6899                 :        216 :                 goto exit;
    6900                 :            :         }
    6901                 :            : 
    6902                 :        208 :         ctx = calloc(1, sizeof(*ctx));
    6903         [ +  + ]:        208 :         if (ctx == NULL) {
    6904                 :          0 :                 SPDK_ERRLOG("Failed to allocate context for bdev_nvme_delete\n");
    6905                 :          0 :                 rc = -ENOMEM;
    6906                 :          0 :                 goto exit;
    6907                 :            :         }
    6908                 :            : 
    6909   [ -  +  #  #  :        208 :         ctx->name = strdup(name);
                   #  # ]
    6910   [ +  +  #  #  :        208 :         if (ctx->name == NULL) {
                   #  # ]
    6911                 :          0 :                 SPDK_ERRLOG("Failed to copy controller name for deletion\n");
    6912                 :          0 :                 rc = -ENOMEM;
    6913                 :          0 :                 goto exit;
    6914                 :            :         }
    6915                 :            : 
    6916   [ #  #  #  # ]:        208 :         ctx->delete_done = delete_done;
    6917   [ #  #  #  # ]:        208 :         ctx->delete_done_ctx = delete_done_ctx;
    6918         [ #  # ]:        208 :         ctx->path_id = *path_id;
    6919   [ #  #  #  # ]:        208 :         ctx->timeout_ticks = spdk_get_ticks() + 10 * spdk_get_ticks_hz();
    6920   [ #  #  #  # ]:        208 :         ctx->poller = SPDK_POLLER_REGISTER(bdev_nvme_delete_complete_poll, ctx, 1000);
    6921   [ +  +  #  #  :        208 :         if (ctx->poller == NULL) {
                   #  # ]
    6922                 :          0 :                 SPDK_ERRLOG("Failed to register bdev_nvme_delete poller\n");
    6923                 :          0 :                 rc = -ENOMEM;
    6924                 :          0 :                 goto exit;
    6925                 :            :         }
    6926                 :            : 
    6927                 :        204 : exit:
    6928         [ +  + ]:        424 :         if (rc != 0) {
    6929                 :          4 :                 free_bdev_nvme_delete_ctx(ctx);
    6930                 :          1 :         }
    6931                 :            : 
    6932                 :        424 :         return rc;
    6933                 :            : }
    6934                 :            : 
    6935                 :            : #define DISCOVERY_INFOLOG(ctx, format, ...) \
    6936                 :            :         SPDK_INFOLOG(bdev_nvme, "Discovery[%s:%s] " format, ctx->trid.traddr, ctx->trid.trsvcid, ##__VA_ARGS__);
    6937                 :            : 
    6938                 :            : #define DISCOVERY_ERRLOG(ctx, format, ...) \
    6939                 :            :         SPDK_ERRLOG("Discovery[%s:%s] " format, ctx->trid.traddr, ctx->trid.trsvcid, ##__VA_ARGS__);
    6940                 :            : 
    6941                 :            : struct discovery_entry_ctx {
    6942                 :            :         char                                            name[128];
    6943                 :            :         struct spdk_nvme_transport_id                   trid;
    6944                 :            :         struct spdk_nvme_ctrlr_opts                     drv_opts;
    6945                 :            :         struct spdk_nvmf_discovery_log_page_entry       entry;
    6946                 :            :         TAILQ_ENTRY(discovery_entry_ctx)                tailq;
    6947                 :            :         struct discovery_ctx                            *ctx;
    6948                 :            : };
    6949                 :            : 
    6950                 :            : struct discovery_ctx {
    6951                 :            :         char                                    *name;
    6952                 :            :         spdk_bdev_nvme_start_discovery_fn       start_cb_fn;
    6953                 :            :         spdk_bdev_nvme_stop_discovery_fn        stop_cb_fn;
    6954                 :            :         void                                    *cb_ctx;
    6955                 :            :         struct spdk_nvme_probe_ctx              *probe_ctx;
    6956                 :            :         struct spdk_nvme_detach_ctx             *detach_ctx;
    6957                 :            :         struct spdk_nvme_ctrlr                  *ctrlr;
    6958                 :            :         struct spdk_nvme_transport_id           trid;
    6959                 :            :         struct discovery_entry_ctx              *entry_ctx_in_use;
    6960                 :            :         struct spdk_poller                      *poller;
    6961                 :            :         struct spdk_nvme_ctrlr_opts             drv_opts;
    6962                 :            :         struct spdk_bdev_nvme_ctrlr_opts        bdev_opts;
    6963                 :            :         struct spdk_nvmf_discovery_log_page     *log_page;
    6964                 :            :         TAILQ_ENTRY(discovery_ctx)              tailq;
    6965                 :            :         TAILQ_HEAD(, discovery_entry_ctx)       nvm_entry_ctxs;
    6966                 :            :         TAILQ_HEAD(, discovery_entry_ctx)       discovery_entry_ctxs;
    6967                 :            :         int                                     rc;
    6968                 :            :         bool                                    wait_for_attach;
    6969                 :            :         uint64_t                                timeout_ticks;
    6970                 :            :         /* Denotes that the discovery service is being started. We're waiting
    6971                 :            :          * for the initial connection to the discovery controller to be
    6972                 :            :          * established and attach discovered NVM ctrlrs.
    6973                 :            :          */
    6974                 :            :         bool                                    initializing;
    6975                 :            :         /* Denotes if a discovery is currently in progress for this context.
    6976                 :            :          * That includes connecting to newly discovered subsystems.  Used to
    6977                 :            :          * ensure we do not start a new discovery until an existing one is
    6978                 :            :          * complete.
    6979                 :            :          */
    6980                 :            :         bool                                    in_progress;
    6981                 :            : 
    6982                 :            :         /* Denotes if another discovery is needed after the one in progress
    6983                 :            :          * completes.  Set when we receive an AER completion while a discovery
    6984                 :            :          * is already in progress.
    6985                 :            :          */
    6986                 :            :         bool                                    pending;
    6987                 :            : 
    6988                 :            :         /* Signal to the discovery context poller that it should stop the
    6989                 :            :          * discovery service, including detaching from the current discovery
    6990                 :            :          * controller.
    6991                 :            :          */
    6992                 :            :         bool                                    stop;
    6993                 :            : 
    6994                 :            :         struct spdk_thread                      *calling_thread;
    6995                 :            :         uint32_t                                index;
    6996                 :            :         uint32_t                                attach_in_progress;
    6997                 :            :         char                                    *hostnqn;
    6998                 :            : 
    6999                 :            :         /* Denotes if the discovery service was started by the mdns discovery.
    7000                 :            :          */
    7001                 :            :         bool                                    from_mdns_discovery_service;
    7002                 :            : };
    7003                 :            : 
    7004                 :            : TAILQ_HEAD(discovery_ctxs, discovery_ctx);
    7005                 :            : static struct discovery_ctxs g_discovery_ctxs = TAILQ_HEAD_INITIALIZER(g_discovery_ctxs);
    7006                 :            : 
    7007                 :            : static void get_discovery_log_page(struct discovery_ctx *ctx);
    7008                 :            : 
    7009                 :            : static void
    7010                 :         22 : free_discovery_ctx(struct discovery_ctx *ctx)
    7011                 :            : {
    7012   [ #  #  #  # ]:         22 :         free(ctx->log_page);
    7013   [ #  #  #  # ]:         22 :         free(ctx->hostnqn);
    7014   [ #  #  #  # ]:         22 :         free(ctx->name);
    7015                 :         22 :         free(ctx);
    7016                 :         22 : }
    7017                 :            : 
    7018                 :            : static void
    7019                 :         23 : discovery_complete(struct discovery_ctx *ctx)
    7020                 :            : {
    7021   [ #  #  #  # ]:         23 :         ctx->initializing = false;
    7022   [ #  #  #  # ]:         23 :         ctx->in_progress = false;
    7023   [ -  +  -  +  :         23 :         if (ctx->pending) {
             #  #  #  # ]
    7024   [ #  #  #  # ]:          0 :                 ctx->pending = false;
    7025                 :          0 :                 get_discovery_log_page(ctx);
    7026                 :          0 :         }
    7027                 :         23 : }
    7028                 :            : 
    7029                 :            : static void
    7030                 :         77 : build_trid_from_log_page_entry(struct spdk_nvme_transport_id *trid,
    7031                 :            :                                struct spdk_nvmf_discovery_log_page_entry *entry)
    7032                 :            : {
    7033                 :            :         char *space;
    7034                 :            : 
    7035   [ #  #  #  #  :         77 :         trid->trtype = entry->trtype;
             #  #  #  # ]
    7036   [ #  #  #  #  :         77 :         trid->adrfam = entry->adrfam;
             #  #  #  # ]
    7037   [ -  +  -  +  :         77 :         memcpy(trid->traddr, entry->traddr, sizeof(entry->traddr));
             #  #  #  # ]
    7038   [ #  #  #  #  :         77 :         memcpy(trid->trsvcid, entry->trsvcid, sizeof(entry->trsvcid));
             #  #  #  # ]
    7039                 :            :         /* Because the source buffer (entry->subnqn) is longer than trid->subnqn, and
    7040                 :            :          * before call to this function trid->subnqn is zeroed out, we need
    7041                 :            :          * to copy sizeof(trid->subnqn) minus one byte to make sure the last character
    7042                 :            :          * remains 0. Then we can shorten the string (replace ' ' with 0) if required
    7043                 :            :          */
    7044   [ -  +  -  +  :         77 :         memcpy(trid->subnqn, entry->subnqn, sizeof(trid->subnqn) - 1);
             #  #  #  # ]
    7045                 :            : 
    7046                 :            :         /* We want the traddr, trsvcid and subnqn fields to be NULL-terminated.
    7047                 :            :          * But the log page entries typically pad them with spaces, not zeroes.
    7048                 :            :          * So add a NULL terminator to each of these fields at the appropriate
    7049                 :            :          * location.
    7050                 :            :          */
    7051   [ -  +  #  # ]:         77 :         space = strchr(trid->traddr, ' ');
    7052         [ +  - ]:         77 :         if (space) {
    7053         [ #  # ]:         77 :                 *space = 0;
    7054                 :          0 :         }
    7055   [ -  +  #  # ]:         77 :         space = strchr(trid->trsvcid, ' ');
    7056         [ +  - ]:         77 :         if (space) {
    7057         [ #  # ]:         77 :                 *space = 0;
    7058                 :          0 :         }
    7059   [ -  +  #  # ]:         77 :         space = strchr(trid->subnqn, ' ');
    7060         [ -  + ]:         77 :         if (space) {
    7061         [ #  # ]:          0 :                 *space = 0;
    7062                 :          0 :         }
    7063                 :         77 : }
    7064                 :            : 
    7065                 :            : static void
    7066                 :         22 : _stop_discovery(void *_ctx)
    7067                 :            : {
    7068                 :         22 :         struct discovery_ctx *ctx = _ctx;
    7069                 :            : 
    7070   [ -  +  #  #  :         22 :         if (ctx->attach_in_progress > 0) {
                   #  # ]
    7071                 :          0 :                 spdk_thread_send_msg(spdk_get_thread(), _stop_discovery, ctx);
    7072                 :          0 :                 return;
    7073                 :            :         }
    7074                 :            : 
    7075   [ #  #  #  # ]:         22 :         ctx->stop = true;
    7076                 :            : 
    7077   [ +  +  #  #  :         41 :         while (!TAILQ_EMPTY(&ctx->nvm_entry_ctxs)) {
             #  #  #  # ]
    7078                 :            :                 struct discovery_entry_ctx *entry_ctx;
    7079                 :         19 :                 struct nvme_path_id path = {};
    7080                 :            : 
    7081   [ #  #  #  #  :         19 :                 entry_ctx = TAILQ_FIRST(&ctx->nvm_entry_ctxs);
                   #  # ]
    7082         [ #  # ]:         19 :                 path.trid = entry_ctx->trid;
    7083         [ #  # ]:         19 :                 bdev_nvme_delete(entry_ctx->name, &path, NULL, NULL);
    7084   [ -  +  #  #  :         19 :                 TAILQ_REMOVE(&ctx->nvm_entry_ctxs, entry_ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7085                 :         19 :                 free(entry_ctx);
    7086                 :            :         }
    7087                 :            : 
    7088   [ +  +  #  #  :         51 :         while (!TAILQ_EMPTY(&ctx->discovery_entry_ctxs)) {
             #  #  #  # ]
    7089                 :            :                 struct discovery_entry_ctx *entry_ctx;
    7090                 :            : 
    7091   [ #  #  #  #  :         29 :                 entry_ctx = TAILQ_FIRST(&ctx->discovery_entry_ctxs);
                   #  # ]
    7092   [ +  +  #  #  :         29 :                 TAILQ_REMOVE(&ctx->discovery_entry_ctxs, entry_ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7093                 :         29 :                 free(entry_ctx);
    7094                 :            :         }
    7095                 :            : 
    7096   [ #  #  #  # ]:         22 :         free(ctx->entry_ctx_in_use);
    7097   [ #  #  #  # ]:         22 :         ctx->entry_ctx_in_use = NULL;
    7098                 :          0 : }
    7099                 :            : 
    7100                 :            : static void
    7101                 :         22 : stop_discovery(struct discovery_ctx *ctx, spdk_bdev_nvme_stop_discovery_fn cb_fn, void *cb_ctx)
    7102                 :            : {
    7103   [ #  #  #  # ]:         22 :         ctx->stop_cb_fn = cb_fn;
    7104   [ #  #  #  # ]:         22 :         ctx->cb_ctx = cb_ctx;
    7105                 :            : 
    7106   [ -  +  #  #  :         22 :         if (ctx->attach_in_progress > 0) {
                   #  # ]
    7107   [ #  #  #  #  :          0 :                 DISCOVERY_INFOLOG(ctx, "stopping discovery with attach_in_progress: %"PRIu32"\n",
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    7108                 :            :                                   ctx->attach_in_progress);
    7109                 :          0 :         }
    7110                 :            : 
    7111                 :         22 :         _stop_discovery(ctx);
    7112                 :         22 : }
    7113                 :            : 
    7114                 :            : static void
    7115                 :         15 : remove_discovery_entry(struct nvme_ctrlr *nvme_ctrlr)
    7116                 :            : {
    7117                 :            :         struct discovery_ctx *d_ctx;
    7118                 :            :         struct nvme_path_id *path_id;
    7119                 :         15 :         struct spdk_nvme_transport_id trid = {};
    7120                 :            :         struct discovery_entry_ctx *entry_ctx, *tmp;
    7121                 :            : 
    7122   [ #  #  #  #  :         15 :         path_id = TAILQ_FIRST(&nvme_ctrlr->trids);
                   #  # ]
    7123                 :            : 
    7124   [ +  +  #  #  :         16 :         TAILQ_FOREACH(d_ctx, &g_discovery_ctxs, tailq) {
             #  #  #  # ]
    7125   [ +  +  #  #  :          2 :                 TAILQ_FOREACH_SAFE(entry_ctx, &d_ctx->nvm_entry_ctxs, tailq, tmp) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7126         [ #  # ]:          1 :                         build_trid_from_log_page_entry(&trid, &entry_ctx->entry);
    7127   [ -  +  #  # ]:          1 :                         if (spdk_nvme_transport_id_compare(&trid, &path_id->trid) != 0) {
    7128                 :          0 :                                 continue;
    7129                 :            :                         }
    7130                 :            : 
    7131   [ -  +  #  #  :          1 :                         TAILQ_REMOVE(&d_ctx->nvm_entry_ctxs, entry_ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7132                 :          1 :                         free(entry_ctx);
    7133   [ -  +  +  -  :          1 :                         DISCOVERY_INFOLOG(d_ctx, "Remove discovery entry: %s:%s:%s\n",
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7134                 :            :                                           trid.subnqn, trid.traddr, trid.trsvcid);
    7135                 :            : 
    7136                 :            :                         /* Fail discovery ctrlr to force reattach attempt */
    7137   [ #  #  #  # ]:          1 :                         spdk_nvme_ctrlr_fail(d_ctx->ctrlr);
    7138                 :          0 :                 }
    7139                 :          0 :         }
    7140                 :         15 : }
    7141                 :            : 
    7142                 :            : static void
    7143                 :         23 : discovery_remove_controllers(struct discovery_ctx *ctx)
    7144                 :            : {
    7145   [ #  #  #  # ]:         23 :         struct spdk_nvmf_discovery_log_page *log_page = ctx->log_page;
    7146                 :            :         struct discovery_entry_ctx *entry_ctx, *tmp;
    7147                 :            :         struct spdk_nvmf_discovery_log_page_entry *new_entry, *old_entry;
    7148                 :         23 :         struct spdk_nvme_transport_id old_trid = {};
    7149                 :            :         uint64_t numrec, i;
    7150                 :            :         bool found;
    7151                 :            : 
    7152         [ #  # ]:         23 :         numrec = from_le64(&log_page->numrec);
    7153   [ +  +  #  #  :         47 :         TAILQ_FOREACH_SAFE(entry_ctx, &ctx->nvm_entry_ctxs, tailq, tmp) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7154                 :         24 :                 found = false;
    7155         [ #  # ]:         24 :                 old_entry = &entry_ctx->entry;
    7156                 :         24 :                 build_trid_from_log_page_entry(&old_trid, old_entry);
    7157         [ +  + ]:         57 :                 for (i = 0; i < numrec; i++) {
    7158   [ #  #  #  # ]:         56 :                         new_entry = &log_page->entries[i];
    7159   [ -  +  -  +  :         56 :                         if (!memcmp(old_entry, new_entry, sizeof(*old_entry))) {
                   +  + ]
    7160   [ -  +  +  +  :         23 :                                 DISCOVERY_INFOLOG(ctx, "NVM %s:%s:%s found again\n",
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7161                 :            :                                                   old_trid.subnqn, old_trid.traddr, old_trid.trsvcid);
    7162                 :         23 :                                 found = true;
    7163                 :         23 :                                 break;
    7164                 :            :                         }
    7165                 :          0 :                 }
    7166   [ +  +  #  # ]:         24 :                 if (!found) {
    7167                 :          1 :                         struct nvme_path_id path = {};
    7168                 :            : 
    7169   [ -  +  +  -  :          1 :                         DISCOVERY_INFOLOG(ctx, "NVM %s:%s:%s not found\n",
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7170                 :            :                                           old_trid.subnqn, old_trid.traddr, old_trid.trsvcid);
    7171                 :            : 
    7172         [ #  # ]:          1 :                         path.trid = entry_ctx->trid;
    7173         [ #  # ]:          1 :                         bdev_nvme_delete(entry_ctx->name, &path, NULL, NULL);
    7174   [ +  -  #  #  :          1 :                         TAILQ_REMOVE(&ctx->nvm_entry_ctxs, entry_ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7175                 :          1 :                         free(entry_ctx);
    7176                 :          0 :                 }
    7177                 :          0 :         }
    7178                 :         23 :         free(log_page);
    7179   [ #  #  #  # ]:         23 :         ctx->log_page = NULL;
    7180                 :         23 :         discovery_complete(ctx);
    7181                 :         23 : }
    7182                 :            : 
    7183                 :            : static void
    7184                 :         24 : complete_discovery_start(struct discovery_ctx *ctx, int status)
    7185                 :            : {
    7186   [ #  #  #  # ]:         24 :         ctx->timeout_ticks = 0;
    7187   [ #  #  #  # ]:         24 :         ctx->rc = status;
    7188   [ +  +  #  #  :         24 :         if (ctx->start_cb_fn) {
                   #  # ]
    7189   [ #  #  #  #  :         21 :                 ctx->start_cb_fn(ctx->cb_ctx, status);
          #  #  #  #  #  
                #  #  # ]
    7190   [ #  #  #  # ]:         21 :                 ctx->start_cb_fn = NULL;
    7191   [ #  #  #  # ]:         21 :                 ctx->cb_ctx = NULL;
    7192                 :          0 :         }
    7193                 :         24 : }
    7194                 :            : 
    7195                 :            : static void
    7196                 :         21 : discovery_attach_controller_done(void *cb_ctx, size_t bdev_count, int rc)
    7197                 :            : {
    7198                 :         21 :         struct discovery_entry_ctx *entry_ctx = cb_ctx;
    7199   [ #  #  #  # ]:         21 :         struct discovery_ctx *ctx = entry_ctx->ctx;
    7200                 :            : 
    7201   [ -  +  +  +  :         21 :         DISCOVERY_INFOLOG(ctx, "attach %s done\n", entry_ctx->name);
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7202         [ #  # ]:         21 :         ctx->attach_in_progress--;
    7203   [ +  -  #  #  :         21 :         if (ctx->attach_in_progress == 0) {
                   #  # ]
    7204   [ #  #  #  # ]:         21 :                 complete_discovery_start(ctx, ctx->rc);
    7205   [ -  +  +  +  :         21 :                 if (ctx->initializing && ctx->rc != 0) {
          -  +  #  #  #  
             #  #  #  #  
                      # ]
    7206   [ #  #  #  #  :          0 :                         DISCOVERY_ERRLOG(ctx, "stopping discovery due to errors: %d\n", ctx->rc);
          #  #  #  #  #  
                #  #  # ]
    7207   [ #  #  #  # ]:          0 :                         stop_discovery(ctx, NULL, ctx->cb_ctx);
    7208                 :          0 :                 } else {
    7209                 :         21 :                         discovery_remove_controllers(ctx);
    7210                 :            :                 }
    7211                 :          0 :         }
    7212                 :         21 : }
    7213                 :            : 
    7214                 :            : static struct discovery_entry_ctx *
    7215                 :         53 : create_discovery_entry_ctx(struct discovery_ctx *ctx, struct spdk_nvme_transport_id *trid)
    7216                 :            : {
    7217                 :            :         struct discovery_entry_ctx *new_ctx;
    7218                 :            : 
    7219                 :         53 :         new_ctx = calloc(1, sizeof(*new_ctx));
    7220         [ -  + ]:         53 :         if (new_ctx == NULL) {
    7221   [ #  #  #  #  :          0 :                 DISCOVERY_ERRLOG(ctx, "could not allocate new entry_ctx\n");
             #  #  #  # ]
    7222                 :          0 :                 return NULL;
    7223                 :            :         }
    7224                 :            : 
    7225   [ #  #  #  # ]:         53 :         new_ctx->ctx = ctx;
    7226   [ -  +  -  +  :         53 :         memcpy(&new_ctx->trid, trid, sizeof(*trid));
                   #  # ]
    7227         [ #  # ]:         53 :         spdk_nvme_ctrlr_get_default_ctrlr_opts(&new_ctx->drv_opts, sizeof(new_ctx->drv_opts));
    7228   [ -  +  #  #  :         53 :         snprintf(new_ctx->drv_opts.hostnqn, sizeof(new_ctx->drv_opts.hostnqn), "%s", ctx->hostnqn);
             #  #  #  # ]
    7229                 :         53 :         return new_ctx;
    7230                 :          0 : }
    7231                 :            : 
    7232                 :            : static void
    7233                 :         23 : discovery_log_page_cb(void *cb_arg, int rc, const struct spdk_nvme_cpl *cpl,
    7234                 :            :                       struct spdk_nvmf_discovery_log_page *log_page)
    7235                 :            : {
    7236                 :         23 :         struct discovery_ctx *ctx = cb_arg;
    7237                 :            :         struct discovery_entry_ctx *entry_ctx, *tmp;
    7238                 :            :         struct spdk_nvmf_discovery_log_page_entry *new_entry, *old_entry;
    7239                 :            :         uint64_t numrec, i;
    7240                 :            :         bool found;
    7241                 :            : 
    7242   [ +  -  +  -  :         23 :         if (rc || spdk_nvme_cpl_is_error(cpl)) {
          -  +  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    7243   [ #  #  #  #  :          0 :                 DISCOVERY_ERRLOG(ctx, "could not get discovery log page\n");
             #  #  #  # ]
    7244                 :          0 :                 return;
    7245                 :            :         }
    7246                 :            : 
    7247   [ #  #  #  # ]:         23 :         ctx->log_page = log_page;
    7248   [ -  +  #  #  :         23 :         assert(ctx->attach_in_progress == 0);
             #  #  #  # ]
    7249         [ #  # ]:         23 :         numrec = from_le64(&log_page->numrec);
    7250   [ +  +  #  #  :         27 :         TAILQ_FOREACH_SAFE(entry_ctx, &ctx->discovery_entry_ctxs, tailq, tmp) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7251   [ +  +  #  #  :          4 :                 TAILQ_REMOVE(&ctx->discovery_entry_ctxs, entry_ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7252                 :          4 :                 free(entry_ctx);
    7253                 :          0 :         }
    7254         [ +  + ]:         77 :         for (i = 0; i < numrec; i++) {
    7255                 :         54 :                 found = false;
    7256   [ #  #  #  # ]:         54 :                 new_entry = &log_page->entries[i];
    7257   [ +  +  #  #  :         54 :                 if (new_entry->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY_CURRENT ||
             #  #  #  # ]
    7258   [ -  +  #  # ]:         23 :                     new_entry->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
    7259                 :            :                         struct discovery_entry_ctx *new_ctx;
    7260                 :         31 :                         struct spdk_nvme_transport_id trid = {};
    7261                 :            : 
    7262                 :         31 :                         build_trid_from_log_page_entry(&trid, new_entry);
    7263                 :         31 :                         new_ctx = create_discovery_entry_ctx(ctx, &trid);
    7264         [ -  + ]:         31 :                         if (new_ctx == NULL) {
    7265   [ #  #  #  #  :          0 :                                 DISCOVERY_ERRLOG(ctx, "could not allocate new entry_ctx\n");
             #  #  #  # ]
    7266                 :          0 :                                 break;
    7267                 :            :                         }
    7268                 :            : 
    7269   [ #  #  #  #  :         31 :                         TAILQ_INSERT_TAIL(&ctx->discovery_entry_ctxs, new_ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7270                 :         31 :                         continue;
    7271                 :            :                 }
    7272   [ +  +  #  #  :         25 :                 TAILQ_FOREACH(entry_ctx, &ctx->nvm_entry_ctxs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7273         [ #  # ]:          4 :                         old_entry = &entry_ctx->entry;
    7274   [ -  +  -  +  :          4 :                         if (!memcmp(new_entry, old_entry, sizeof(*new_entry))) {
                   +  + ]
    7275                 :          2 :                                 found = true;
    7276                 :          2 :                                 break;
    7277                 :            :                         }
    7278                 :          0 :                 }
    7279   [ +  +  #  # ]:         23 :                 if (!found) {
    7280                 :         21 :                         struct discovery_entry_ctx *subnqn_ctx = NULL, *new_ctx;
    7281                 :            :                         struct discovery_ctx *d_ctx;
    7282                 :            : 
    7283   [ +  +  #  #  :         44 :                         TAILQ_FOREACH(d_ctx, &g_discovery_ctxs, tailq) {
             #  #  #  # ]
    7284   [ +  +  #  #  :         27 :                                 TAILQ_FOREACH(subnqn_ctx, &d_ctx->nvm_entry_ctxs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7285   [ -  +  -  +  :          4 :                                         if (!memcmp(subnqn_ctx->entry.subnqn, new_entry->subnqn,
          +  +  #  #  #  
                #  #  # ]
    7286                 :            :                                                     sizeof(new_entry->subnqn))) {
    7287                 :          1 :                                                 break;
    7288                 :            :                                         }
    7289                 :          0 :                                 }
    7290         [ +  + ]:         24 :                                 if (subnqn_ctx) {
    7291                 :          1 :                                         break;
    7292                 :            :                                 }
    7293                 :          0 :                         }
    7294                 :            : 
    7295                 :         21 :                         new_ctx = calloc(1, sizeof(*new_ctx));
    7296         [ -  + ]:         21 :                         if (new_ctx == NULL) {
    7297   [ #  #  #  #  :          0 :                                 DISCOVERY_ERRLOG(ctx, "could not allocate new entry_ctx\n");
             #  #  #  # ]
    7298                 :          0 :                                 break;
    7299                 :            :                         }
    7300                 :            : 
    7301   [ #  #  #  # ]:         21 :                         new_ctx->ctx = ctx;
    7302   [ -  +  -  +  :         21 :                         memcpy(&new_ctx->entry, new_entry, sizeof(*new_entry));
                   #  # ]
    7303         [ #  # ]:         21 :                         build_trid_from_log_page_entry(&new_ctx->trid, new_entry);
    7304         [ +  + ]:         21 :                         if (subnqn_ctx) {
    7305   [ #  #  #  # ]:          1 :                                 snprintf(new_ctx->name, sizeof(new_ctx->name), "%s", subnqn_ctx->name);
    7306   [ -  +  +  -  :          1 :                                 DISCOVERY_INFOLOG(ctx, "NVM %s:%s:%s new path for %s\n",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    7307                 :            :                                                   new_ctx->trid.subnqn, new_ctx->trid.traddr, new_ctx->trid.trsvcid,
    7308                 :            :                                                   new_ctx->name);
    7309                 :          0 :                         } else {
    7310   [ #  #  #  #  :         20 :                                 snprintf(new_ctx->name, sizeof(new_ctx->name), "%s%d", ctx->name, ctx->index++);
             #  #  #  # ]
    7311   [ -  +  +  +  :         20 :                                 DISCOVERY_INFOLOG(ctx, "NVM %s:%s:%s new subsystem %s\n",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    7312                 :            :                                                   new_ctx->trid.subnqn, new_ctx->trid.traddr, new_ctx->trid.trsvcid,
    7313                 :            :                                                   new_ctx->name);
    7314                 :            :                         }
    7315         [ #  # ]:         21 :                         spdk_nvme_ctrlr_get_default_ctrlr_opts(&new_ctx->drv_opts, sizeof(new_ctx->drv_opts));
    7316   [ #  #  #  #  :         21 :                         snprintf(new_ctx->drv_opts.hostnqn, sizeof(new_ctx->drv_opts.hostnqn), "%s", ctx->hostnqn);
             #  #  #  # ]
    7317   [ #  #  #  # ]:         21 :                         rc = spdk_bdev_nvme_create(&new_ctx->trid, new_ctx->name, NULL, 0,
    7318                 :          0 :                                                    discovery_attach_controller_done, new_ctx,
    7319   [ #  #  #  # ]:          0 :                                                    &new_ctx->drv_opts, &ctx->bdev_opts);
    7320         [ +  - ]:         21 :                         if (rc == 0) {
    7321   [ #  #  #  #  :         21 :                                 TAILQ_INSERT_TAIL(&ctx->nvm_entry_ctxs, new_ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7322         [ #  # ]:         21 :                                 ctx->attach_in_progress++;
    7323                 :          0 :                         } else {
    7324   [ #  #  #  #  :          0 :                                 DISCOVERY_ERRLOG(ctx, "spdk_bdev_nvme_create failed (%s)\n", spdk_strerror(-rc));
          #  #  #  #  #  
                      # ]
    7325                 :            :                         }
    7326                 :          0 :                 }
    7327                 :          0 :         }
    7328                 :            : 
    7329   [ +  +  #  #  :         23 :         if (ctx->attach_in_progress == 0) {
                   #  # ]
    7330                 :          2 :                 discovery_remove_controllers(ctx);
    7331                 :          0 :         }
    7332                 :          0 : }
    7333                 :            : 
    7334                 :            : static void
    7335                 :         23 : get_discovery_log_page(struct discovery_ctx *ctx)
    7336                 :            : {
    7337                 :            :         int rc;
    7338                 :            : 
    7339   [ -  +  -  +  :         23 :         assert(ctx->in_progress == false);
          #  #  #  #  #  
                      # ]
    7340   [ #  #  #  # ]:         23 :         ctx->in_progress = true;
    7341   [ #  #  #  # ]:         23 :         rc = spdk_nvme_ctrlr_get_discovery_log_page(ctx->ctrlr, discovery_log_page_cb, ctx);
    7342         [ -  + ]:         23 :         if (rc != 0) {
    7343   [ #  #  #  #  :          0 :                 DISCOVERY_ERRLOG(ctx, "could not get discovery log page\n");
             #  #  #  # ]
    7344                 :          0 :         }
    7345   [ -  +  +  +  :         23 :         DISCOVERY_INFOLOG(ctx, "sent discovery log page command\n");
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7346                 :         23 : }
    7347                 :            : 
    7348                 :            : static void
    7349                 :          2 : discovery_aer_cb(void *arg, const struct spdk_nvme_cpl *cpl)
    7350                 :            : {
    7351                 :          2 :         struct discovery_ctx *ctx = arg;
    7352   [ #  #  #  #  :          2 :         uint32_t log_page_id = (cpl->cdw0 & 0xFF0000) >> 16;
                   #  # ]
    7353                 :            : 
    7354   [ +  -  -  +  :          2 :         if (spdk_nvme_cpl_is_error(cpl)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7355   [ #  #  #  #  :          0 :                 DISCOVERY_ERRLOG(ctx, "aer failed\n");
             #  #  #  # ]
    7356                 :          0 :                 return;
    7357                 :            :         }
    7358                 :            : 
    7359         [ -  + ]:          2 :         if (log_page_id != SPDK_NVME_LOG_DISCOVERY) {
    7360   [ #  #  #  #  :          0 :                 DISCOVERY_ERRLOG(ctx, "unexpected log page 0x%x\n", log_page_id);
             #  #  #  # ]
    7361                 :          0 :                 return;
    7362                 :            :         }
    7363                 :            : 
    7364   [ -  +  +  -  :          2 :         DISCOVERY_INFOLOG(ctx, "got aer\n");
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7365   [ -  +  -  +  :          2 :         if (ctx->in_progress) {
             #  #  #  # ]
    7366   [ #  #  #  # ]:          0 :                 ctx->pending = true;
    7367                 :          0 :                 return;
    7368                 :            :         }
    7369                 :            : 
    7370                 :          2 :         get_discovery_log_page(ctx);
    7371                 :          0 : }
    7372                 :            : 
    7373                 :            : static void
    7374                 :         21 : discovery_attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
    7375                 :            :                     struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
    7376                 :            : {
    7377                 :         21 :         struct spdk_nvme_ctrlr_opts *user_opts = cb_ctx;
    7378                 :            :         struct discovery_ctx *ctx;
    7379                 :            : 
    7380                 :         21 :         ctx = SPDK_CONTAINEROF(user_opts, struct discovery_ctx, drv_opts);
    7381                 :            : 
    7382   [ -  +  +  +  :         21 :         DISCOVERY_INFOLOG(ctx, "discovery ctrlr attached\n");
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7383   [ #  #  #  # ]:         21 :         ctx->probe_ctx = NULL;
    7384   [ #  #  #  # ]:         21 :         ctx->ctrlr = ctrlr;
    7385                 :            : 
    7386   [ -  +  #  #  :         21 :         if (ctx->rc != 0) {
                   #  # ]
    7387   [ #  #  #  #  :          0 :                 DISCOVERY_ERRLOG(ctx, "encountered error while attaching discovery ctrlr: %d\n",
          #  #  #  #  #  
                #  #  # ]
    7388                 :            :                                  ctx->rc);
    7389                 :          0 :                 return;
    7390                 :            :         }
    7391                 :            : 
    7392   [ #  #  #  # ]:         21 :         spdk_nvme_ctrlr_register_aer_callback(ctx->ctrlr, discovery_aer_cb, ctx);
    7393                 :          0 : }
    7394                 :            : 
    7395                 :            : static int
    7396                 :      62900 : discovery_poller(void *arg)
    7397                 :            : {
    7398                 :      62900 :         struct discovery_ctx *ctx = arg;
    7399                 :            :         struct spdk_nvme_transport_id *trid;
    7400                 :            :         int rc;
    7401                 :            : 
    7402   [ +  +  #  #  :      62900 :         if (ctx->detach_ctx) {
                   #  # ]
    7403   [ #  #  #  # ]:        161 :                 rc = spdk_nvme_detach_poll_async(ctx->detach_ctx);
    7404         [ +  + ]:        161 :                 if (rc != -EAGAIN) {
    7405   [ #  #  #  # ]:         21 :                         ctx->detach_ctx = NULL;
    7406   [ #  #  #  # ]:         21 :                         ctx->ctrlr = NULL;
    7407                 :          0 :                 }
    7408   [ -  +  +  +  :      62739 :         } else if (ctx->stop) {
             #  #  #  # ]
    7409   [ +  +  #  #  :         40 :                 if (ctx->ctrlr != NULL) {
                   #  # ]
    7410   [ #  #  #  #  :         20 :                         rc = spdk_nvme_detach_async(ctx->ctrlr, &ctx->detach_ctx);
                   #  # ]
    7411         [ +  - ]:         20 :                         if (rc == 0) {
    7412                 :         20 :                                 return SPDK_POLLER_BUSY;
    7413                 :            :                         }
    7414   [ #  #  #  #  :          0 :                         DISCOVERY_ERRLOG(ctx, "could not detach discovery ctrlr\n");
             #  #  #  # ]
    7415                 :          0 :                 }
    7416         [ #  # ]:         20 :                 spdk_poller_unregister(&ctx->poller);
    7417   [ +  +  #  #  :         20 :                 TAILQ_REMOVE(&g_discovery_ctxs, ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    7418   [ -  +  #  #  :         20 :                 assert(ctx->start_cb_fn == NULL);
             #  #  #  # ]
    7419   [ +  +  #  #  :         20 :                 if (ctx->stop_cb_fn != NULL) {
                   #  # ]
    7420   [ #  #  #  #  :         19 :                         ctx->stop_cb_fn(ctx->cb_ctx);
          #  #  #  #  #  
                #  #  # ]
    7421                 :          0 :                 }
    7422                 :         20 :                 free_discovery_ctx(ctx);
    7423   [ +  +  +  +  :      62699 :         } else if (ctx->probe_ctx == NULL && ctx->ctrlr == NULL) {
          #  #  #  #  #  
                #  #  # ]
    7424   [ +  +  +  +  :         29 :                 if (ctx->timeout_ticks != 0 && ctx->timeout_ticks < spdk_get_ticks()) {
          #  #  #  #  #  
                #  #  # ]
    7425   [ #  #  #  #  :          2 :                         DISCOVERY_ERRLOG(ctx, "timed out while attaching discovery ctrlr\n");
             #  #  #  # ]
    7426   [ -  +  -  +  :          2 :                         assert(ctx->initializing);
          #  #  #  #  #  
                      # ]
    7427         [ #  # ]:          2 :                         spdk_poller_unregister(&ctx->poller);
    7428   [ -  +  #  #  :          2 :                         TAILQ_REMOVE(&g_discovery_ctxs, ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    7429                 :          2 :                         complete_discovery_start(ctx, -ETIMEDOUT);
    7430                 :          2 :                         stop_discovery(ctx, NULL, NULL);
    7431                 :          2 :                         free_discovery_ctx(ctx);
    7432                 :          2 :                         return SPDK_POLLER_BUSY;
    7433                 :            :                 }
    7434                 :            : 
    7435   [ -  +  #  #  :         27 :                 assert(ctx->entry_ctx_in_use == NULL);
             #  #  #  # ]
    7436   [ #  #  #  #  :         27 :                 ctx->entry_ctx_in_use = TAILQ_FIRST(&ctx->discovery_entry_ctxs);
          #  #  #  #  #  
                      # ]
    7437   [ +  +  #  #  :         27 :                 TAILQ_REMOVE(&ctx->discovery_entry_ctxs, ctx->entry_ctx_in_use, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7438   [ #  #  #  #  :         27 :                 trid = &ctx->entry_ctx_in_use->trid;
                   #  # ]
    7439                 :            : 
    7440                 :            :                 /* All controllers must be configured explicitely either for multipath or failover.
    7441                 :            :                  * While discovery use multipath mode, we need to set this in bdev options as well.
    7442                 :            :                  */
    7443   [ #  #  #  #  :         27 :                 ctx->bdev_opts.multipath = true;
                   #  # ]
    7444                 :            : 
    7445   [ #  #  #  #  :         27 :                 ctx->probe_ctx = spdk_nvme_connect_async(trid, &ctx->drv_opts, discovery_attach_cb);
                   #  # ]
    7446   [ +  +  #  #  :         27 :                 if (ctx->probe_ctx) {
                   #  # ]
    7447         [ #  # ]:         21 :                         spdk_poller_unregister(&ctx->poller);
    7448   [ #  #  #  # ]:         21 :                         ctx->poller = SPDK_POLLER_REGISTER(discovery_poller, ctx, 1000);
    7449                 :          0 :                 } else {
    7450   [ #  #  #  #  :          6 :                         DISCOVERY_ERRLOG(ctx, "could not start discovery connect\n");
             #  #  #  # ]
    7451   [ #  #  #  #  :          6 :                         TAILQ_INSERT_TAIL(&ctx->discovery_entry_ctxs, ctx->entry_ctx_in_use, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7452   [ #  #  #  # ]:          6 :                         ctx->entry_ctx_in_use = NULL;
    7453                 :            :                 }
    7454   [ +  +  #  #  :      62670 :         } else if (ctx->probe_ctx) {
                   #  # ]
    7455   [ +  +  -  +  :         21 :                 if (ctx->timeout_ticks != 0 && ctx->timeout_ticks < spdk_get_ticks()) {
          #  #  #  #  #  
                #  #  # ]
    7456   [ #  #  #  #  :          0 :                         DISCOVERY_ERRLOG(ctx, "timed out while attaching discovery ctrlr\n");
             #  #  #  # ]
    7457                 :          0 :                         complete_discovery_start(ctx, -ETIMEDOUT);
    7458                 :          0 :                         return SPDK_POLLER_BUSY;
    7459                 :            :                 }
    7460                 :            : 
    7461   [ #  #  #  # ]:         21 :                 rc = spdk_nvme_probe_poll_async(ctx->probe_ctx);
    7462         [ +  - ]:         21 :                 if (rc != -EAGAIN) {
    7463   [ -  +  #  #  :         21 :                         if (ctx->rc != 0) {
                   #  # ]
    7464   [ #  #  #  #  :          0 :                                 assert(ctx->initializing);
          #  #  #  #  #  
                      # ]
    7465   [ #  #  #  # ]:          0 :                                 stop_discovery(ctx, NULL, ctx->cb_ctx);
    7466                 :          0 :                         } else {
    7467   [ -  +  #  # ]:         21 :                                 assert(rc == 0);
    7468   [ -  +  +  +  :         21 :                                 DISCOVERY_INFOLOG(ctx, "discovery ctrlr connected\n");
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7469   [ #  #  #  # ]:         21 :                                 ctx->rc = rc;
    7470                 :         21 :                                 get_discovery_log_page(ctx);
    7471                 :            :                         }
    7472                 :          0 :                 }
    7473                 :          0 :         } else {
    7474   [ +  +  +  +  :      62649 :                 if (ctx->timeout_ticks != 0 && ctx->timeout_ticks < spdk_get_ticks()) {
          #  #  #  #  #  
                #  #  # ]
    7475   [ #  #  #  #  :          1 :                         DISCOVERY_ERRLOG(ctx, "timed out while attaching NVM ctrlrs\n");
             #  #  #  # ]
    7476                 :          1 :                         complete_discovery_start(ctx, -ETIMEDOUT);
    7477                 :            :                         /* We need to wait until all NVM ctrlrs are attached before we stop the
    7478                 :            :                          * discovery service to make sure we don't detach a ctrlr that is still
    7479                 :            :                          * being attached.
    7480                 :            :                          */
    7481   [ +  -  #  #  :          1 :                         if (ctx->attach_in_progress == 0) {
                   #  # ]
    7482   [ #  #  #  # ]:          1 :                                 stop_discovery(ctx, NULL, ctx->cb_ctx);
    7483                 :          1 :                                 return SPDK_POLLER_BUSY;
    7484                 :            :                         }
    7485                 :          0 :                 }
    7486                 :            : 
    7487   [ #  #  #  # ]:      62648 :                 rc = spdk_nvme_ctrlr_process_admin_completions(ctx->ctrlr);
    7488         [ +  + ]:      62648 :                 if (rc < 0) {
    7489         [ #  # ]:          1 :                         spdk_poller_unregister(&ctx->poller);
    7490   [ #  #  #  # ]:          1 :                         ctx->poller = SPDK_POLLER_REGISTER(discovery_poller, ctx, 1000 * 1000);
    7491   [ #  #  #  #  :          1 :                         TAILQ_INSERT_TAIL(&ctx->discovery_entry_ctxs, ctx->entry_ctx_in_use, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7492   [ #  #  #  # ]:          1 :                         ctx->entry_ctx_in_use = NULL;
    7493                 :            : 
    7494   [ #  #  #  #  :          1 :                         rc = spdk_nvme_detach_async(ctx->ctrlr, &ctx->detach_ctx);
                   #  # ]
    7495         [ -  + ]:          1 :                         if (rc != 0) {
    7496   [ #  #  #  #  :          0 :                                 DISCOVERY_ERRLOG(ctx, "could not detach discovery ctrlr\n");
             #  #  #  # ]
    7497   [ #  #  #  # ]:          0 :                                 ctx->ctrlr = NULL;
    7498                 :          0 :                         }
    7499                 :          0 :                 }
    7500                 :            :         }
    7501                 :            : 
    7502                 :      62877 :         return SPDK_POLLER_BUSY;
    7503                 :          0 : }
    7504                 :            : 
    7505                 :            : static void
    7506                 :         22 : start_discovery_poller(void *arg)
    7507                 :            : {
    7508                 :         22 :         struct discovery_ctx *ctx = arg;
    7509                 :            : 
    7510   [ #  #  #  #  :         22 :         TAILQ_INSERT_TAIL(&g_discovery_ctxs, ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7511   [ #  #  #  # ]:         22 :         ctx->poller = SPDK_POLLER_REGISTER(discovery_poller, ctx, 1000 * 1000);
    7512                 :         22 : }
    7513                 :            : 
    7514                 :            : int
    7515                 :         24 : bdev_nvme_start_discovery(struct spdk_nvme_transport_id *trid,
    7516                 :            :                           const char *base_name,
    7517                 :            :                           struct spdk_nvme_ctrlr_opts *drv_opts,
    7518                 :            :                           struct spdk_bdev_nvme_ctrlr_opts *bdev_opts,
    7519                 :            :                           uint64_t attach_timeout,
    7520                 :            :                           bool from_mdns,
    7521                 :            :                           spdk_bdev_nvme_start_discovery_fn cb_fn, void *cb_ctx)
    7522                 :            : {
    7523                 :            :         struct discovery_ctx *ctx;
    7524                 :            :         struct discovery_entry_ctx *discovery_entry_ctx;
    7525                 :            : 
    7526         [ #  # ]:         24 :         snprintf(trid->subnqn, sizeof(trid->subnqn), "%s", SPDK_NVMF_DISCOVERY_NQN);
    7527   [ +  +  #  #  :         30 :         TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
             #  #  #  # ]
    7528   [ -  +  -  +  :          8 :                 if (strcmp(ctx->name, base_name) == 0) {
          +  +  #  #  #  
                      # ]
    7529                 :          1 :                         return -EEXIST;
    7530                 :            :                 }
    7531                 :            : 
    7532   [ +  -  #  #  :          7 :                 if (ctx->entry_ctx_in_use != NULL) {
                   #  # ]
    7533   [ +  +  #  #  :          7 :                         if (!spdk_nvme_transport_id_compare(trid, &ctx->entry_ctx_in_use->trid)) {
             #  #  #  # ]
    7534                 :          1 :                                 return -EEXIST;
    7535                 :            :                         }
    7536                 :          0 :                 }
    7537                 :            : 
    7538   [ +  +  #  #  :         12 :                 TAILQ_FOREACH(discovery_entry_ctx, &ctx->discovery_entry_ctxs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7539   [ -  +  #  # ]:          6 :                         if (!spdk_nvme_transport_id_compare(trid, &discovery_entry_ctx->trid)) {
    7540                 :          0 :                                 return -EEXIST;
    7541                 :            :                         }
    7542                 :          0 :                 }
    7543                 :          0 :         }
    7544                 :            : 
    7545                 :         22 :         ctx = calloc(1, sizeof(*ctx));
    7546         [ -  + ]:         22 :         if (ctx == NULL) {
    7547                 :          0 :                 return -ENOMEM;
    7548                 :            :         }
    7549                 :            : 
    7550   [ -  +  #  #  :         22 :         ctx->name = strdup(base_name);
                   #  # ]
    7551   [ -  +  #  #  :         22 :         if (ctx->name == NULL) {
                   #  # ]
    7552                 :          0 :                 free_discovery_ctx(ctx);
    7553                 :          0 :                 return -ENOMEM;
    7554                 :            :         }
    7555   [ -  +  -  +  :         22 :         memcpy(&ctx->drv_opts, drv_opts, sizeof(*drv_opts));
                   #  # ]
    7556   [ -  +  -  +  :         22 :         memcpy(&ctx->bdev_opts, bdev_opts, sizeof(*bdev_opts));
                   #  # ]
    7557   [ #  #  #  #  :         22 :         ctx->from_mdns_discovery_service = from_mdns;
                   #  # ]
    7558   [ #  #  #  #  :         22 :         ctx->bdev_opts.from_discovery_service = true;
                   #  # ]
    7559   [ #  #  #  # ]:         22 :         ctx->calling_thread = spdk_get_thread();
    7560   [ #  #  #  # ]:         22 :         ctx->start_cb_fn = cb_fn;
    7561   [ #  #  #  # ]:         22 :         ctx->cb_ctx = cb_ctx;
    7562   [ #  #  #  # ]:         22 :         ctx->initializing = true;
    7563   [ +  +  #  #  :         22 :         if (ctx->start_cb_fn) {
                   #  # ]
    7564                 :            :                 /* We can use this when dumping json to denote if this RPC parameter
    7565                 :            :                  * was specified or not.
    7566                 :            :                  */
    7567   [ #  #  #  # ]:         21 :                 ctx->wait_for_attach = true;
    7568                 :          0 :         }
    7569         [ +  + ]:         22 :         if (attach_timeout != 0) {
    7570   [ #  #  #  # ]:         38 :                 ctx->timeout_ticks = spdk_get_ticks() + attach_timeout *
    7571   [ #  #  #  # ]:         19 :                                      spdk_get_ticks_hz() / 1000ull;
    7572                 :          0 :         }
    7573   [ #  #  #  #  :         22 :         TAILQ_INIT(&ctx->nvm_entry_ctxs);
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7574   [ #  #  #  #  :         22 :         TAILQ_INIT(&ctx->discovery_entry_ctxs);
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7575   [ -  +  -  +  :         22 :         memcpy(&ctx->trid, trid, sizeof(*trid));
                   #  # ]
    7576                 :            :         /* Even if user did not specify hostnqn, we can still strdup("\0"); */
    7577   [ -  +  #  #  :         22 :         ctx->hostnqn = strdup(ctx->drv_opts.hostnqn);
          #  #  #  #  #  
                      # ]
    7578   [ -  +  #  #  :         22 :         if (ctx->hostnqn == NULL) {
                   #  # ]
    7579                 :          0 :                 free_discovery_ctx(ctx);
    7580                 :          0 :                 return -ENOMEM;
    7581                 :            :         }
    7582                 :         22 :         discovery_entry_ctx = create_discovery_entry_ctx(ctx, trid);
    7583         [ -  + ]:         22 :         if (discovery_entry_ctx == NULL) {
    7584   [ #  #  #  #  :          0 :                 DISCOVERY_ERRLOG(ctx, "could not allocate new entry_ctx\n");
             #  #  #  # ]
    7585                 :          0 :                 free_discovery_ctx(ctx);
    7586                 :          0 :                 return -ENOMEM;
    7587                 :            :         }
    7588                 :            : 
    7589   [ #  #  #  #  :         22 :         TAILQ_INSERT_TAIL(&ctx->discovery_entry_ctxs, discovery_entry_ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7590                 :         22 :         spdk_thread_send_msg(g_bdev_nvme_init_thread, start_discovery_poller, ctx);
    7591                 :         22 :         return 0;
    7592                 :          0 : }
    7593                 :            : 
    7594                 :            : int
    7595                 :         17 : bdev_nvme_stop_discovery(const char *name, spdk_bdev_nvme_stop_discovery_fn cb_fn, void *cb_ctx)
    7596                 :            : {
    7597                 :            :         struct discovery_ctx *ctx;
    7598                 :            : 
    7599   [ +  -  #  #  :         18 :         TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
             #  #  #  # ]
    7600   [ -  +  -  +  :         18 :                 if (strcmp(name, ctx->name) == 0) {
          +  +  #  #  #  
                      # ]
    7601   [ -  +  -  +  :         17 :                         if (ctx->stop) {
             #  #  #  # ]
    7602                 :          0 :                                 return -EALREADY;
    7603                 :            :                         }
    7604                 :            :                         /* If we're still starting the discovery service and ->rc is non-zero, we're
    7605                 :            :                          * going to stop it as soon as we can
    7606                 :            :                          */
    7607   [ -  +  -  +  :         17 :                         if (ctx->initializing && ctx->rc != 0) {
          -  -  #  #  #  
             #  #  #  #  
                      # ]
    7608                 :          0 :                                 return -EALREADY;
    7609                 :            :                         }
    7610                 :         17 :                         stop_discovery(ctx, cb_fn, cb_ctx);
    7611                 :         17 :                         return 0;
    7612                 :            :                 }
    7613                 :          0 :         }
    7614                 :            : 
    7615                 :          0 :         return -ENOENT;
    7616                 :          0 : }
    7617                 :            : 
    7618                 :            : static int
    7619                 :       1492 : bdev_nvme_library_init(void)
    7620                 :            : {
    7621                 :       1492 :         g_bdev_nvme_init_thread = spdk_get_thread();
    7622                 :            : 
    7623                 :       1492 :         spdk_io_device_register(&g_nvme_bdev_ctrlrs, bdev_nvme_create_poll_group_cb,
    7624                 :            :                                 bdev_nvme_destroy_poll_group_cb,
    7625                 :            :                                 sizeof(struct nvme_poll_group),  "nvme_poll_groups");
    7626                 :            : 
    7627                 :       1492 :         return 0;
    7628                 :            : }
    7629                 :            : 
    7630                 :            : static void
    7631                 :       1492 : bdev_nvme_fini_destruct_ctrlrs(void)
    7632                 :            : {
    7633                 :            :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
    7634                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    7635                 :            : 
    7636         [ +  + ]:       1492 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    7637   [ +  +  +  -  :       1992 :         TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
             +  -  +  - ]
    7638   [ +  +  +  -  :       1002 :                 TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
          +  -  +  +  +  
             -  +  -  +  
                      - ]
    7639   [ +  +  +  - ]:        502 :                         pthread_mutex_lock(&nvme_ctrlr->mutex);
    7640   [ +  +  -  + ]:        502 :                         if (nvme_ctrlr->destruct) {
    7641                 :            :                                 /* This controller's destruction was already started
    7642                 :            :                                  * before the application started shutting down
    7643                 :            :                                  */
    7644   [ #  #  #  # ]:          0 :                                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    7645                 :          0 :                                 continue;
    7646                 :            :                         }
    7647         [ -  + ]:        502 :                         nvme_ctrlr->destruct = true;
    7648   [ -  +  -  + ]:        502 :                         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    7649                 :            : 
    7650   [ -  +  -  + ]:        509 :                         spdk_thread_send_msg(nvme_ctrlr->thread, _nvme_ctrlr_destruct,
    7651                 :          8 :                                              nvme_ctrlr);
    7652                 :          8 :                 }
    7653                 :          8 :         }
    7654                 :            : 
    7655                 :       1492 :         g_bdev_nvme_module_finish = true;
    7656         [ +  + ]:       1492 :         if (TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) {
    7657         [ -  + ]:       1104 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
    7658                 :       1104 :                 spdk_io_device_unregister(&g_nvme_bdev_ctrlrs, NULL);
    7659                 :       1104 :                 spdk_bdev_module_fini_done();
    7660                 :       1104 :                 return;
    7661                 :            :         }
    7662                 :            : 
    7663         [ +  + ]:        388 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    7664                 :         92 : }
    7665                 :            : 
    7666                 :            : static void
    7667                 :          2 : check_discovery_fini(void *arg)
    7668                 :            : {
    7669         [ +  - ]:          2 :         if (TAILQ_EMPTY(&g_discovery_ctxs)) {
    7670                 :          2 :                 bdev_nvme_fini_destruct_ctrlrs();
    7671                 :          0 :         }
    7672                 :          2 : }
    7673                 :            : 
    7674                 :            : static void
    7675                 :       1492 : bdev_nvme_library_fini(void)
    7676                 :            : {
    7677                 :            :         struct nvme_probe_skip_entry *entry, *entry_tmp;
    7678                 :            :         struct discovery_ctx *ctx;
    7679                 :            : 
    7680                 :       1492 :         spdk_poller_unregister(&g_hotplug_poller);
    7681                 :       1492 :         free(g_hotplug_probe_ctx);
    7682                 :       1492 :         g_hotplug_probe_ctx = NULL;
    7683                 :            : 
    7684   [ +  +  +  +  :       1534 :         TAILQ_FOREACH_SAFE(entry, &g_skipped_nvme_ctrlrs, tailq, entry_tmp) {
          #  #  #  #  -  
                      + ]
    7685   [ +  +  #  #  :         42 :                 TAILQ_REMOVE(&g_skipped_nvme_ctrlrs, entry, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    7686                 :         42 :                 free(entry);
    7687                 :          3 :         }
    7688                 :            : 
    7689   [ +  +  #  # ]:       1492 :         assert(spdk_get_thread() == g_bdev_nvme_init_thread);
    7690         [ +  + ]:       1492 :         if (TAILQ_EMPTY(&g_discovery_ctxs)) {
    7691                 :       1490 :                 bdev_nvme_fini_destruct_ctrlrs();
    7692                 :         92 :         } else {
    7693   [ +  +  #  #  :          4 :                 TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
             #  #  #  # ]
    7694                 :          2 :                         stop_discovery(ctx, check_discovery_fini, NULL);
    7695                 :          0 :                 }
    7696                 :            :         }
    7697                 :       1492 : }
    7698                 :            : 
    7699                 :            : static void
    7700                 :          0 : bdev_nvme_verify_pi_error(struct nvme_bdev_io *bio)
    7701                 :            : {
    7702                 :          0 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    7703   [ #  #  #  # ]:          0 :         struct spdk_bdev *bdev = bdev_io->bdev;
    7704                 :          0 :         struct spdk_dif_ctx dif_ctx;
    7705                 :          0 :         struct spdk_dif_error err_blk = {};
    7706                 :            :         int rc;
    7707                 :          0 :         struct spdk_dif_ctx_init_ext_opts dif_opts;
    7708                 :            : 
    7709                 :          0 :         dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
    7710   [ #  #  #  #  :          0 :         dif_opts.dif_pi_format = bdev->dif_pi_format;
                   #  # ]
    7711                 :          0 :         rc = spdk_dif_ctx_init(&dif_ctx,
    7712   [ #  #  #  #  :          0 :                                bdev->blocklen, bdev->md_len, bdev->md_interleave,
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7713   [ #  #  #  #  :          0 :                                bdev->dif_is_head_of_md, bdev->dif_type,
          #  #  #  #  #  
                      # ]
    7714   [ #  #  #  #  :          0 :                                bdev_io->u.bdev.dif_check_flags,
             #  #  #  # ]
    7715   [ #  #  #  #  :          0 :                                bdev_io->u.bdev.offset_blocks, 0, 0, 0, 0, &dif_opts);
             #  #  #  # ]
    7716         [ #  # ]:          0 :         if (rc != 0) {
    7717                 :          0 :                 SPDK_ERRLOG("Initialization of DIF context failed\n");
    7718                 :          0 :                 return;
    7719                 :            :         }
    7720                 :            : 
    7721   [ #  #  #  #  :          0 :         if (bdev->md_interleave) {
             #  #  #  # ]
    7722   [ #  #  #  #  :          0 :                 rc = spdk_dif_verify(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7723   [ #  #  #  #  :          0 :                                      bdev_io->u.bdev.num_blocks, &dif_ctx, &err_blk);
             #  #  #  # ]
    7724                 :          0 :         } else {
    7725                 :          0 :                 struct iovec md_iov = {
    7726   [ #  #  #  #  :          0 :                         .iov_base       = bdev_io->u.bdev.md_buf,
             #  #  #  # ]
    7727   [ #  #  #  #  :          0 :                         .iov_len        = bdev_io->u.bdev.num_blocks * bdev->md_len,
          #  #  #  #  #  
                #  #  # ]
    7728                 :            :                 };
    7729                 :            : 
    7730   [ #  #  #  #  :          0 :                 rc = spdk_dix_verify(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7731   [ #  #  #  #  :          0 :                                      &md_iov, bdev_io->u.bdev.num_blocks, &dif_ctx, &err_blk);
             #  #  #  # ]
    7732                 :            :         }
    7733                 :            : 
    7734         [ #  # ]:          0 :         if (rc != 0) {
    7735         [ #  # ]:          0 :                 SPDK_ERRLOG("DIF error detected. type=%d, offset=%" PRIu32 "\n",
    7736                 :            :                             err_blk.err_type, err_blk.err_offset);
    7737                 :          0 :         } else {
    7738                 :          0 :                 SPDK_ERRLOG("Hardware reported PI error but SPDK could not find any.\n");
    7739                 :            :         }
    7740                 :          0 : }
    7741                 :            : 
    7742                 :            : static void
    7743                 :          0 : bdev_nvme_no_pi_readv_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7744                 :            : {
    7745                 :          0 :         struct nvme_bdev_io *bio = ref;
    7746                 :            : 
    7747   [ #  #  #  #  :          0 :         if (spdk_nvme_cpl_is_success(cpl)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7748                 :            :                 /* Run PI verification for read data buffer. */
    7749                 :          0 :                 bdev_nvme_verify_pi_error(bio);
    7750                 :          0 :         }
    7751                 :            : 
    7752                 :            :         /* Return original completion status */
    7753         [ #  # ]:          0 :         bdev_nvme_io_complete_nvme_status(bio, &bio->cpl);
    7754                 :          0 : }
    7755                 :            : 
    7756                 :            : static void
    7757                 :    8143777 : bdev_nvme_readv_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7758                 :            : {
    7759                 :    8143777 :         struct nvme_bdev_io *bio = ref;
    7760                 :    8143777 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    7761                 :            :         int ret;
    7762                 :            : 
    7763   [ +  +  +  -  :    8143777 :         if (spdk_unlikely(spdk_nvme_cpl_is_pi_error(cpl))) {
          +  -  +  -  -  
          -  -  -  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  +  - ]
    7764   [ #  #  #  #  :          0 :                 SPDK_ERRLOG("readv completed with PI error (sct=%d, sc=%d)\n",
          #  #  #  #  #  
                #  #  # ]
    7765                 :            :                             cpl->status.sct, cpl->status.sc);
    7766                 :            : 
    7767                 :            :                 /* Save completion status to use after verifying PI error. */
    7768         [ #  # ]:          0 :                 bio->cpl = *cpl;
    7769                 :            : 
    7770   [ #  #  #  #  :          0 :                 if (spdk_likely(nvme_io_path_is_available(bio->io_path))) {
                   #  # ]
    7771                 :            :                         /* Read without PI checking to verify PI error. */
    7772                 :          0 :                         ret = bdev_nvme_no_pi_readv(bio,
    7773   [ #  #  #  #  :          0 :                                                     bdev_io->u.bdev.iovs,
             #  #  #  # ]
    7774   [ #  #  #  #  :          0 :                                                     bdev_io->u.bdev.iovcnt,
             #  #  #  # ]
    7775   [ #  #  #  #  :          0 :                                                     bdev_io->u.bdev.md_buf,
             #  #  #  # ]
    7776   [ #  #  #  #  :          0 :                                                     bdev_io->u.bdev.num_blocks,
             #  #  #  # ]
    7777   [ #  #  #  #  :          0 :                                                     bdev_io->u.bdev.offset_blocks);
             #  #  #  # ]
    7778         [ #  # ]:          0 :                         if (ret == 0) {
    7779                 :          0 :                                 return;
    7780                 :            :                         }
    7781                 :          0 :                 }
    7782                 :          0 :         }
    7783                 :            : 
    7784                 :    8143777 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7785                 :     307425 : }
    7786                 :            : 
    7787                 :            : static void
    7788                 :    7305721 : bdev_nvme_writev_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7789                 :            : {
    7790                 :    7305721 :         struct nvme_bdev_io *bio = ref;
    7791                 :            : 
    7792   [ +  +  -  -  :    7305721 :         if (spdk_unlikely(spdk_nvme_cpl_is_pi_error(cpl))) {
          -  -  +  -  -  
          -  -  -  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7793   [ #  #  #  #  :          0 :                 SPDK_ERRLOG("writev completed with PI error (sct=%d, sc=%d)\n",
          #  #  #  #  #  
                #  #  # ]
    7794                 :            :                             cpl->status.sct, cpl->status.sc);
    7795                 :            :                 /* Run PI verification for write data buffer if PI error is detected. */
    7796                 :          0 :                 bdev_nvme_verify_pi_error(bio);
    7797                 :          0 :         }
    7798                 :            : 
    7799                 :    7305721 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7800                 :    7305721 : }
    7801                 :            : 
    7802                 :            : static void
    7803                 :     235234 : bdev_nvme_zone_appendv_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7804                 :            : {
    7805                 :     235234 :         struct nvme_bdev_io *bio = ref;
    7806                 :     235234 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    7807                 :            : 
    7808                 :            :         /* spdk_bdev_io_get_append_location() requires that the ALBA is stored in offset_blocks.
    7809                 :            :          * Additionally, offset_blocks has to be set before calling bdev_nvme_verify_pi_error().
    7810                 :            :          */
    7811   [ #  #  #  #  :     235234 :         bdev_io->u.bdev.offset_blocks = *(uint64_t *)&cpl->cdw0;
          #  #  #  #  #  
                #  #  # ]
    7812                 :            : 
    7813   [ -  +  -  -  :     235234 :         if (spdk_nvme_cpl_is_pi_error(cpl)) {
          -  -  -  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7814   [ #  #  #  #  :          0 :                 SPDK_ERRLOG("zone append completed with PI error (sct=%d, sc=%d)\n",
          #  #  #  #  #  
                #  #  # ]
    7815                 :            :                             cpl->status.sct, cpl->status.sc);
    7816                 :            :                 /* Run PI verification for zone append data buffer if PI error is detected. */
    7817                 :          0 :                 bdev_nvme_verify_pi_error(bio);
    7818                 :          0 :         }
    7819                 :            : 
    7820                 :     235234 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7821                 :     235234 : }
    7822                 :            : 
    7823                 :            : static void
    7824                 :         56 : bdev_nvme_comparev_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7825                 :            : {
    7826                 :         56 :         struct nvme_bdev_io *bio = ref;
    7827                 :            : 
    7828   [ +  +  +  -  :         56 :         if (spdk_nvme_cpl_is_pi_error(cpl)) {
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7829   [ #  #  #  #  :          0 :                 SPDK_ERRLOG("comparev completed with PI error (sct=%d, sc=%d)\n",
          #  #  #  #  #  
                #  #  # ]
    7830                 :            :                             cpl->status.sct, cpl->status.sc);
    7831                 :            :                 /* Run PI verification for compare data buffer if PI error is detected. */
    7832                 :          0 :                 bdev_nvme_verify_pi_error(bio);
    7833                 :          0 :         }
    7834                 :            : 
    7835                 :         56 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7836                 :         56 : }
    7837                 :            : 
    7838                 :            : static void
    7839                 :         36 : bdev_nvme_comparev_and_writev_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7840                 :            : {
    7841                 :         36 :         struct nvme_bdev_io *bio = ref;
    7842                 :            : 
    7843                 :            :         /* Compare operation completion */
    7844   [ +  +  +  +  :         36 :         if (!bio->first_fused_completed) {
             #  #  #  # ]
    7845                 :            :                 /* Save compare result for write callback */
    7846         [ #  # ]:         18 :                 bio->cpl = *cpl;
    7847   [ #  #  #  # ]:         18 :                 bio->first_fused_completed = true;
    7848                 :         18 :                 return;
    7849                 :            :         }
    7850                 :            : 
    7851                 :            :         /* Write operation completion */
    7852   [ +  +  -  +  :         18 :         if (spdk_nvme_cpl_is_error(&bio->cpl)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    7853                 :            :                 /* If bio->cpl is already an error, it means the compare operation failed.  In that case,
    7854                 :            :                  * complete the IO with the compare operation's status.
    7855                 :            :                  */
    7856   [ +  +  +  -  :         12 :                 if (!spdk_nvme_cpl_is_error(cpl)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7857                 :          4 :                         SPDK_ERRLOG("Unexpected write success after compare failure.\n");
    7858                 :          1 :                 }
    7859                 :            : 
    7860         [ #  # ]:         12 :                 bdev_nvme_io_complete_nvme_status(bio, &bio->cpl);
    7861                 :          1 :         } else {
    7862                 :          6 :                 bdev_nvme_io_complete_nvme_status(bio, cpl);
    7863                 :            :         }
    7864                 :          4 : }
    7865                 :            : 
    7866                 :            : static void
    7867                 :     789239 : bdev_nvme_queued_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7868                 :            : {
    7869                 :     789239 :         struct nvme_bdev_io *bio = ref;
    7870                 :            : 
    7871                 :     789239 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7872                 :     789239 : }
    7873                 :            : 
    7874                 :            : static int
    7875                 :         40 : fill_zone_from_report(struct spdk_bdev_zone_info *info, struct spdk_nvme_zns_zone_desc *desc)
    7876                 :            : {
    7877   [ +  -  #  # ]:         40 :         switch (desc->zt) {
    7878                 :         40 :         case SPDK_NVME_ZONE_TYPE_SEQWR:
    7879   [ #  #  #  # ]:         40 :                 info->type = SPDK_BDEV_ZONE_TYPE_SEQWR;
    7880                 :         40 :                 break;
    7881                 :          0 :         default:
    7882         [ #  # ]:          0 :                 SPDK_ERRLOG("Invalid zone type: %#x in zone report\n", desc->zt);
    7883                 :          0 :                 return -EIO;
    7884                 :            :         }
    7885                 :            : 
    7886   [ +  -  -  -  :         40 :         switch (desc->zs) {
          -  -  -  -  #  
                      # ]
    7887                 :         40 :         case SPDK_NVME_ZONE_STATE_EMPTY:
    7888   [ #  #  #  # ]:         40 :                 info->state = SPDK_BDEV_ZONE_STATE_EMPTY;
    7889                 :         40 :                 break;
    7890                 :          0 :         case SPDK_NVME_ZONE_STATE_IOPEN:
    7891   [ #  #  #  # ]:          0 :                 info->state = SPDK_BDEV_ZONE_STATE_IMP_OPEN;
    7892                 :          0 :                 break;
    7893                 :          0 :         case SPDK_NVME_ZONE_STATE_EOPEN:
    7894   [ #  #  #  # ]:          0 :                 info->state = SPDK_BDEV_ZONE_STATE_EXP_OPEN;
    7895                 :          0 :                 break;
    7896                 :          0 :         case SPDK_NVME_ZONE_STATE_CLOSED:
    7897   [ #  #  #  # ]:          0 :                 info->state = SPDK_BDEV_ZONE_STATE_CLOSED;
    7898                 :          0 :                 break;
    7899                 :          0 :         case SPDK_NVME_ZONE_STATE_RONLY:
    7900   [ #  #  #  # ]:          0 :                 info->state = SPDK_BDEV_ZONE_STATE_READ_ONLY;
    7901                 :          0 :                 break;
    7902                 :          0 :         case SPDK_NVME_ZONE_STATE_FULL:
    7903   [ #  #  #  # ]:          0 :                 info->state = SPDK_BDEV_ZONE_STATE_FULL;
    7904                 :          0 :                 break;
    7905                 :          0 :         case SPDK_NVME_ZONE_STATE_OFFLINE:
    7906   [ #  #  #  # ]:          0 :                 info->state = SPDK_BDEV_ZONE_STATE_OFFLINE;
    7907                 :          0 :                 break;
    7908                 :          0 :         default:
    7909         [ #  # ]:          0 :                 SPDK_ERRLOG("Invalid zone state: %#x in zone report\n", desc->zs);
    7910                 :          0 :                 return -EIO;
    7911                 :            :         }
    7912                 :            : 
    7913   [ #  #  #  #  :         40 :         info->zone_id = desc->zslba;
             #  #  #  # ]
    7914   [ #  #  #  #  :         40 :         info->write_pointer = desc->wp;
             #  #  #  # ]
    7915   [ #  #  #  #  :         40 :         info->capacity = desc->zcap;
             #  #  #  # ]
    7916                 :            : 
    7917                 :         40 :         return 0;
    7918                 :          0 : }
    7919                 :            : 
    7920                 :            : static void
    7921                 :          1 : bdev_nvme_get_zone_info_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7922                 :            : {
    7923                 :          1 :         struct nvme_bdev_io *bio = ref;
    7924                 :          1 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    7925   [ #  #  #  #  :          1 :         uint64_t zone_id = bdev_io->u.zone_mgmt.zone_id;
             #  #  #  # ]
    7926   [ #  #  #  #  :          1 :         uint32_t zones_to_copy = bdev_io->u.zone_mgmt.num_zones;
             #  #  #  # ]
    7927   [ #  #  #  #  :          1 :         struct spdk_bdev_zone_info *info = bdev_io->u.zone_mgmt.buf;
             #  #  #  # ]
    7928                 :            :         uint64_t max_zones_per_buf, i;
    7929                 :            :         uint32_t zone_report_bufsize;
    7930                 :            :         struct spdk_nvme_ns *ns;
    7931                 :            :         struct spdk_nvme_qpair *qpair;
    7932                 :            :         int ret;
    7933                 :            : 
    7934   [ +  -  -  +  :          1 :         if (spdk_nvme_cpl_is_error(cpl)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7935                 :          0 :                 goto out_complete_io_nvme_cpl;
    7936                 :            :         }
    7937                 :            : 
    7938   [ -  +  #  #  :          1 :         if (spdk_unlikely(!nvme_io_path_is_available(bio->io_path))) {
                   #  # ]
    7939                 :          0 :                 ret = -ENXIO;
    7940                 :          0 :                 goto out_complete_io_ret;
    7941                 :            :         }
    7942                 :            : 
    7943   [ #  #  #  #  :          1 :         ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    7944   [ #  #  #  #  :          1 :         qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    7945                 :            : 
    7946                 :          1 :         zone_report_bufsize = spdk_nvme_ns_get_max_io_xfer_size(ns);
    7947         [ #  # ]:          1 :         max_zones_per_buf = (zone_report_bufsize - sizeof(*bio->zone_report_buf)) /
    7948                 :            :                             sizeof(bio->zone_report_buf->descs[0]);
    7949                 :            : 
    7950   [ -  +  #  #  :          1 :         if (bio->zone_report_buf->nr_zones > max_zones_per_buf) {
          #  #  #  #  #  
                      # ]
    7951                 :          0 :                 ret = -EINVAL;
    7952                 :          0 :                 goto out_complete_io_ret;
    7953                 :            :         }
    7954                 :            : 
    7955   [ -  +  #  #  :          1 :         if (!bio->zone_report_buf->nr_zones) {
          #  #  #  #  #  
                      # ]
    7956                 :          0 :                 ret = -EINVAL;
    7957                 :          0 :                 goto out_complete_io_ret;
    7958                 :            :         }
    7959                 :            : 
    7960   [ +  +  +  -  :         41 :         for (i = 0; i < bio->zone_report_buf->nr_zones && bio->handled_zones < zones_to_copy; i++) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7961   [ #  #  #  #  :         40 :                 ret = fill_zone_from_report(&info[bio->handled_zones],
                   #  # ]
    7962   [ #  #  #  #  :         40 :                                             &bio->zone_report_buf->descs[i]);
             #  #  #  # ]
    7963         [ -  + ]:         40 :                 if (ret) {
    7964                 :          0 :                         goto out_complete_io_ret;
    7965                 :            :                 }
    7966         [ #  # ]:         40 :                 bio->handled_zones++;
    7967                 :          0 :         }
    7968                 :            : 
    7969   [ -  +  #  #  :          1 :         if (bio->handled_zones < zones_to_copy) {
                   #  # ]
    7970                 :          0 :                 uint64_t zone_size_lba = spdk_nvme_zns_ns_get_zone_size_sectors(ns);
    7971   [ #  #  #  # ]:          0 :                 uint64_t slba = zone_id + (zone_size_lba * bio->handled_zones);
    7972                 :            : 
    7973   [ #  #  #  #  :          0 :                 memset(bio->zone_report_buf, 0, zone_report_bufsize);
                   #  # ]
    7974                 :          0 :                 ret = spdk_nvme_zns_report_zones(ns, qpair,
    7975   [ #  #  #  # ]:          0 :                                                  bio->zone_report_buf, zone_report_bufsize,
    7976                 :          0 :                                                  slba, SPDK_NVME_ZRA_LIST_ALL, true,
    7977                 :          0 :                                                  bdev_nvme_get_zone_info_done, bio);
    7978         [ #  # ]:          0 :                 if (!ret) {
    7979                 :          0 :                         return;
    7980                 :            :                 } else {
    7981                 :          0 :                         goto out_complete_io_ret;
    7982                 :            :                 }
    7983                 :            :         }
    7984                 :            : 
    7985                 :          1 : out_complete_io_nvme_cpl:
    7986   [ #  #  #  # ]:          1 :         free(bio->zone_report_buf);
    7987   [ #  #  #  # ]:          1 :         bio->zone_report_buf = NULL;
    7988                 :          1 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7989                 :          1 :         return;
    7990                 :            : 
    7991                 :          0 : out_complete_io_ret:
    7992   [ #  #  #  # ]:          0 :         free(bio->zone_report_buf);
    7993   [ #  #  #  # ]:          0 :         bio->zone_report_buf = NULL;
    7994                 :          0 :         bdev_nvme_io_complete(bio, ret);
    7995                 :          0 : }
    7996                 :            : 
    7997                 :            : static void
    7998                 :         43 : bdev_nvme_zone_management_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7999                 :            : {
    8000                 :         43 :         struct nvme_bdev_io *bio = ref;
    8001                 :            : 
    8002                 :         43 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    8003                 :         43 : }
    8004                 :            : 
    8005                 :            : static void
    8006                 :         66 : bdev_nvme_admin_passthru_complete_nvme_status(void *ctx)
    8007                 :            : {
    8008                 :         66 :         struct nvme_bdev_io *bio = ctx;
    8009                 :         66 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    8010         [ #  # ]:         66 :         const struct spdk_nvme_cpl *cpl = &bio->cpl;
    8011                 :            : 
    8012   [ +  +  #  #  :         66 :         assert(bdev_nvme_io_type_is_admin(bdev_io->type));
             #  #  #  # ]
    8013                 :            : 
    8014                 :         66 :         __bdev_nvme_io_complete(bdev_io, 0, cpl);
    8015                 :         66 : }
    8016                 :            : 
    8017                 :            : static void
    8018                 :       2541 : bdev_nvme_abort_complete(void *ctx)
    8019                 :            : {
    8020                 :       2541 :         struct nvme_bdev_io *bio = ctx;
    8021                 :       2541 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    8022                 :            : 
    8023   [ +  -  +  -  :       2541 :         if (spdk_nvme_cpl_is_abort_success(&bio->cpl)) {
          +  +  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    8024                 :         12 :                 __bdev_nvme_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS, NULL);
    8025                 :          3 :         } else {
    8026                 :       2529 :                 __bdev_nvme_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED, NULL);
    8027                 :            :         }
    8028                 :       2541 : }
    8029                 :            : 
    8030                 :            : static void
    8031                 :       2541 : bdev_nvme_abort_done(void *ref, const struct spdk_nvme_cpl *cpl)
    8032                 :            : {
    8033                 :       2541 :         struct nvme_bdev_io *bio = ref;
    8034                 :       2541 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    8035                 :            : 
    8036         [ #  # ]:       2541 :         bio->cpl = *cpl;
    8037                 :       2541 :         spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io), bdev_nvme_abort_complete, bio);
    8038                 :       2541 : }
    8039                 :            : 
    8040                 :            : static void
    8041                 :         66 : bdev_nvme_admin_passthru_done(void *ref, const struct spdk_nvme_cpl *cpl)
    8042                 :            : {
    8043                 :         66 :         struct nvme_bdev_io *bio = ref;
    8044                 :         66 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    8045                 :            : 
    8046         [ #  # ]:         66 :         bio->cpl = *cpl;
    8047                 :         71 :         spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io),
    8048                 :          5 :                              bdev_nvme_admin_passthru_complete_nvme_status, bio);
    8049                 :         66 : }
    8050                 :            : 
    8051                 :            : static void
    8052                 :    2541608 : bdev_nvme_queued_reset_sgl(void *ref, uint32_t sgl_offset)
    8053                 :            : {
    8054                 :    2541608 :         struct nvme_bdev_io *bio = ref;
    8055                 :            :         struct iovec *iov;
    8056                 :            : 
    8057   [ #  #  #  # ]:    2541608 :         bio->iov_offset = sgl_offset;
    8058   [ +  +  #  #  :    7289270 :         for (bio->iovpos = 0; bio->iovpos < bio->iovcnt; bio->iovpos++) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    8059   [ #  #  #  #  :    7289270 :                 iov = &bio->iovs[bio->iovpos];
          #  #  #  #  #  
                      # ]
    8060   [ +  +  #  #  :    7289270 :                 if (bio->iov_offset < iov->iov_len) {
          #  #  #  #  #  
                      # ]
    8061                 :    2541608 :                         break;
    8062                 :            :                 }
    8063                 :            : 
    8064   [ #  #  #  #  :    4747662 :                 bio->iov_offset -= iov->iov_len;
             #  #  #  # ]
    8065                 :         74 :         }
    8066                 :    2541608 : }
    8067                 :            : 
    8068                 :            : static int
    8069                 :    3430082 : bdev_nvme_queued_next_sge(void *ref, void **address, uint32_t *length)
    8070                 :            : {
    8071                 :    3430082 :         struct nvme_bdev_io *bio = ref;
    8072                 :            :         struct iovec *iov;
    8073                 :            : 
    8074   [ +  +  #  #  :    3430082 :         assert(bio->iovpos < bio->iovcnt);
          #  #  #  #  #  
                #  #  # ]
    8075                 :            : 
    8076   [ #  #  #  #  :    3430082 :         iov = &bio->iovs[bio->iovpos];
          #  #  #  #  #  
                      # ]
    8077                 :            : 
    8078   [ #  #  #  #  :    3430082 :         *address = iov->iov_base;
                   #  # ]
    8079   [ #  #  #  #  :    3430082 :         *length = iov->iov_len;
                   #  # ]
    8080                 :            : 
    8081   [ +  +  #  #  :    3430082 :         if (bio->iov_offset) {
                   #  # ]
    8082   [ -  +  #  #  :     369964 :                 assert(bio->iov_offset <= iov->iov_len);
          #  #  #  #  #  
                #  #  # ]
    8083   [ #  #  #  #  :     369964 :                 *address += bio->iov_offset;
                   #  # ]
    8084   [ #  #  #  #  :     369964 :                 *length -= bio->iov_offset;
                   #  # ]
    8085                 :          0 :         }
    8086                 :            : 
    8087   [ #  #  #  #  :    3430082 :         bio->iov_offset += *length;
                   #  # ]
    8088   [ +  +  #  #  :    3430082 :         if (bio->iov_offset == iov->iov_len) {
          #  #  #  #  #  
                      # ]
    8089   [ #  #  #  # ]:    3430082 :                 bio->iovpos++;
    8090   [ #  #  #  # ]:    3430082 :                 bio->iov_offset = 0;
    8091                 :        162 :         }
    8092                 :            : 
    8093                 :    3430082 :         return 0;
    8094                 :            : }
    8095                 :            : 
    8096                 :            : static void
    8097                 :         20 : bdev_nvme_queued_reset_fused_sgl(void *ref, uint32_t sgl_offset)
    8098                 :            : {
    8099                 :         20 :         struct nvme_bdev_io *bio = ref;
    8100                 :            :         struct iovec *iov;
    8101                 :            : 
    8102   [ #  #  #  # ]:         20 :         bio->fused_iov_offset = sgl_offset;
    8103   [ +  -  #  #  :         20 :         for (bio->fused_iovpos = 0; bio->fused_iovpos < bio->fused_iovcnt; bio->fused_iovpos++) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    8104   [ #  #  #  #  :         20 :                 iov = &bio->fused_iovs[bio->fused_iovpos];
          #  #  #  #  #  
                      # ]
    8105   [ +  -  #  #  :         20 :                 if (bio->fused_iov_offset < iov->iov_len) {
          #  #  #  #  #  
                      # ]
    8106                 :         20 :                         break;
    8107                 :            :                 }
    8108                 :            : 
    8109   [ #  #  #  #  :          0 :                 bio->fused_iov_offset -= iov->iov_len;
             #  #  #  # ]
    8110                 :          0 :         }
    8111                 :         20 : }
    8112                 :            : 
    8113                 :            : static int
    8114                 :         20 : bdev_nvme_queued_next_fused_sge(void *ref, void **address, uint32_t *length)
    8115                 :            : {
    8116                 :         20 :         struct nvme_bdev_io *bio = ref;
    8117                 :            :         struct iovec *iov;
    8118                 :            : 
    8119   [ -  +  #  #  :         20 :         assert(bio->fused_iovpos < bio->fused_iovcnt);
          #  #  #  #  #  
                #  #  # ]
    8120                 :            : 
    8121   [ #  #  #  #  :         20 :         iov = &bio->fused_iovs[bio->fused_iovpos];
          #  #  #  #  #  
                      # ]
    8122                 :            : 
    8123   [ #  #  #  #  :         20 :         *address = iov->iov_base;
                   #  # ]
    8124   [ #  #  #  #  :         20 :         *length = iov->iov_len;
                   #  # ]
    8125                 :            : 
    8126   [ -  +  #  #  :         20 :         if (bio->fused_iov_offset) {
                   #  # ]
    8127   [ #  #  #  #  :          0 :                 assert(bio->fused_iov_offset <= iov->iov_len);
          #  #  #  #  #  
                #  #  # ]
    8128   [ #  #  #  #  :          0 :                 *address += bio->fused_iov_offset;
                   #  # ]
    8129   [ #  #  #  #  :          0 :                 *length -= bio->fused_iov_offset;
                   #  # ]
    8130                 :          0 :         }
    8131                 :            : 
    8132   [ #  #  #  #  :         20 :         bio->fused_iov_offset += *length;
                   #  # ]
    8133   [ +  -  #  #  :         20 :         if (bio->fused_iov_offset == iov->iov_len) {
          #  #  #  #  #  
                      # ]
    8134   [ #  #  #  # ]:         20 :                 bio->fused_iovpos++;
    8135   [ #  #  #  # ]:         20 :                 bio->fused_iov_offset = 0;
    8136                 :          0 :         }
    8137                 :            : 
    8138                 :         20 :         return 0;
    8139                 :            : }
    8140                 :            : 
    8141                 :            : static int
    8142                 :          0 : bdev_nvme_no_pi_readv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
    8143                 :            :                       void *md, uint64_t lba_count, uint64_t lba)
    8144                 :            : {
    8145                 :            :         int rc;
    8146                 :            : 
    8147   [ #  #  #  #  :          0 :         SPDK_DEBUGLOG(bdev_nvme, "read %" PRIu64 " blocks with offset %#" PRIx64 " without PI check\n",
                   #  # ]
    8148                 :            :                       lba_count, lba);
    8149                 :            : 
    8150   [ #  #  #  # ]:          0 :         bio->iovs = iov;
    8151   [ #  #  #  # ]:          0 :         bio->iovcnt = iovcnt;
    8152   [ #  #  #  # ]:          0 :         bio->iovpos = 0;
    8153   [ #  #  #  # ]:          0 :         bio->iov_offset = 0;
    8154                 :            : 
    8155   [ #  #  #  #  :          0 :         rc = spdk_nvme_ns_cmd_readv_with_md(bio->io_path->nvme_ns->ns,
          #  #  #  #  #  
                #  #  # ]
    8156   [ #  #  #  #  :          0 :                                             bio->io_path->qpair->qpair,
          #  #  #  #  #  
                #  #  # ]
    8157                 :          0 :                                             lba, lba_count,
    8158                 :          0 :                                             bdev_nvme_no_pi_readv_done, bio, 0,
    8159                 :            :                                             bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge,
    8160                 :          0 :                                             md, 0, 0);
    8161                 :            : 
    8162   [ #  #  #  # ]:          0 :         if (rc != 0 && rc != -ENOMEM) {
    8163                 :          0 :                 SPDK_ERRLOG("no_pi_readv failed: rc = %d\n", rc);
    8164                 :          0 :         }
    8165                 :          0 :         return rc;
    8166                 :            : }
    8167                 :            : 
    8168                 :            : static int
    8169                 :    8152568 : bdev_nvme_readv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
    8170                 :            :                 void *md, uint64_t lba_count, uint64_t lba, uint32_t flags,
    8171                 :            :                 struct spdk_memory_domain *domain, void *domain_ctx,
    8172                 :            :                 struct spdk_accel_sequence *seq)
    8173                 :            : {
    8174   [ +  -  +  -  :    8152568 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          +  -  +  -  +  
                -  +  - ]
    8175   [ +  -  +  -  :    8152568 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          +  -  +  -  +  
                -  +  - ]
    8176                 :            :         int rc;
    8177                 :            : 
    8178   [ +  +  +  +  :    8152568 :         SPDK_DEBUGLOG(bdev_nvme, "read %" PRIu64 " blocks with offset %#" PRIx64 "\n",
                   +  - ]
    8179                 :            :                       lba_count, lba);
    8180                 :            : 
    8181   [ +  -  +  - ]:    8152568 :         bio->iovs = iov;
    8182   [ +  -  +  - ]:    8152568 :         bio->iovcnt = iovcnt;
    8183   [ +  -  +  - ]:    8152568 :         bio->iovpos = 0;
    8184   [ +  -  +  - ]:    8152568 :         bio->iov_offset = 0;
    8185                 :            : 
    8186   [ +  +  +  + ]:    8152568 :         if (domain != NULL || seq != NULL) {
    8187   [ #  #  #  #  :      51809 :                 bio->ext_opts.size = SPDK_SIZEOF(&bio->ext_opts, accel_sequence);
                   #  # ]
    8188   [ #  #  #  #  :      51809 :                 bio->ext_opts.memory_domain = domain;
                   #  # ]
    8189   [ #  #  #  #  :      51809 :                 bio->ext_opts.memory_domain_ctx = domain_ctx;
                   #  # ]
    8190   [ #  #  #  #  :      51809 :                 bio->ext_opts.io_flags = flags;
                   #  # ]
    8191   [ #  #  #  #  :      51809 :                 bio->ext_opts.metadata = md;
                   #  # ]
    8192   [ #  #  #  #  :      51809 :                 bio->ext_opts.accel_sequence = seq;
                   #  # ]
    8193                 :            : 
    8194         [ +  - ]:      51809 :                 if (iovcnt == 1) {
    8195   [ #  #  #  #  :      51800 :                         rc = spdk_nvme_ns_cmd_read_ext(ns, qpair, iov[0].iov_base, lba, lba_count, bdev_nvme_readv_done,
                   #  # ]
    8196         [ #  # ]:          1 :                                                        bio, &bio->ext_opts);
    8197                 :          1 :                 } else {
    8198                 :          0 :                         rc = spdk_nvme_ns_cmd_readv_ext(ns, qpair, lba, lba_count,
    8199                 :          0 :                                                         bdev_nvme_readv_done, bio,
    8200                 :            :                                                         bdev_nvme_queued_reset_sgl,
    8201                 :            :                                                         bdev_nvme_queued_next_sge,
    8202         [ #  # ]:          0 :                                                         &bio->ext_opts);
    8203                 :            :                 }
    8204         [ +  + ]:    8100770 :         } else if (iovcnt == 1) {
    8205   [ -  +  -  +  :    8282609 :                 rc = spdk_nvme_ns_cmd_read_with_md(ns, qpair, iov[0].iov_base,
                   -  + ]
    8206                 :     306796 :                                                    md, lba, lba_count, bdev_nvme_readv_done,
    8207                 :     306796 :                                                    bio, flags, 0, 0);
    8208                 :     306796 :         } else {
    8209                 :     124958 :                 rc = spdk_nvme_ns_cmd_readv_with_md(ns, qpair, lba, lba_count,
    8210                 :          5 :                                                     bdev_nvme_readv_done, bio, flags,
    8211                 :            :                                                     bdev_nvme_queued_reset_sgl,
    8212                 :          5 :                                                     bdev_nvme_queued_next_sge, md, 0, 0);
    8213                 :            :         }
    8214                 :            : 
    8215   [ +  +  +  + ]:    8152568 :         if (spdk_unlikely(rc != 0 && rc != -ENOMEM)) {
    8216                 :          0 :                 SPDK_ERRLOG("readv failed: rc = %d\n", rc);
    8217                 :          0 :         }
    8218                 :    8152568 :         return rc;
    8219                 :            : }
    8220                 :            : 
    8221                 :            : static int
    8222                 :    7314813 : bdev_nvme_writev(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
    8223                 :            :                  void *md, uint64_t lba_count, uint64_t lba, uint32_t flags,
    8224                 :            :                  struct spdk_memory_domain *domain, void *domain_ctx,
    8225                 :            :                  struct spdk_accel_sequence *seq,
    8226                 :            :                  union spdk_bdev_nvme_cdw12 cdw12, union spdk_bdev_nvme_cdw13 cdw13)
    8227                 :            : {
    8228   [ #  #  #  #  :    7314813 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8229   [ #  #  #  #  :    7314813 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8230                 :            :         int rc;
    8231                 :            : 
    8232   [ +  +  -  +  :    7314813 :         SPDK_DEBUGLOG(bdev_nvme, "write %" PRIu64 " blocks with offset %#" PRIx64 "\n",
                   #  # ]
    8233                 :            :                       lba_count, lba);
    8234                 :            : 
    8235   [ #  #  #  # ]:    7314813 :         bio->iovs = iov;
    8236   [ #  #  #  # ]:    7314813 :         bio->iovcnt = iovcnt;
    8237   [ #  #  #  # ]:    7314813 :         bio->iovpos = 0;
    8238   [ #  #  #  # ]:    7314813 :         bio->iov_offset = 0;
    8239                 :            : 
    8240   [ +  +  +  + ]:    7314813 :         if (domain != NULL || seq != NULL) {
    8241   [ #  #  #  #  :      51813 :                 bio->ext_opts.size = SPDK_SIZEOF(&bio->ext_opts, accel_sequence);
                   #  # ]
    8242   [ #  #  #  #  :      51813 :                 bio->ext_opts.memory_domain = domain;
                   #  # ]
    8243   [ #  #  #  #  :      51813 :                 bio->ext_opts.memory_domain_ctx = domain_ctx;
                   #  # ]
    8244   [ -  +  #  #  :      51813 :                 bio->ext_opts.io_flags = flags | SPDK_NVME_IO_FLAGS_DIRECTIVE(cdw12.write.dtype);
          #  #  #  #  #  
                      # ]
    8245   [ #  #  #  #  :      51813 :                 bio->ext_opts.cdw13 = cdw13.raw;
                   #  # ]
    8246   [ #  #  #  #  :      51813 :                 bio->ext_opts.metadata = md;
                   #  # ]
    8247   [ #  #  #  #  :      51813 :                 bio->ext_opts.accel_sequence = seq;
                   #  # ]
    8248                 :            : 
    8249         [ +  - ]:      51813 :                 if (iovcnt == 1) {
    8250   [ #  #  #  #  :      51795 :                         rc = spdk_nvme_ns_cmd_write_ext(ns, qpair, iov[0].iov_base, lba, lba_count, bdev_nvme_writev_done,
                   #  # ]
    8251         [ #  # ]:          0 :                                                         bio, &bio->ext_opts);
    8252                 :          0 :                 } else {
    8253                 :          0 :                         rc = spdk_nvme_ns_cmd_writev_ext(ns, qpair, lba, lba_count,
    8254                 :          0 :                                                          bdev_nvme_writev_done, bio,
    8255                 :            :                                                          bdev_nvme_queued_reset_sgl,
    8256                 :            :                                                          bdev_nvme_queued_next_sge,
    8257         [ #  # ]:          0 :                                                          &bio->ext_opts);
    8258                 :            :                 }
    8259         [ +  + ]:    7263018 :         } else if (iovcnt == 1) {
    8260   [ #  #  #  #  :    7278087 :                 rc = spdk_nvme_ns_cmd_write_with_md(ns, qpair, iov[0].iov_base,
                   #  # ]
    8261                 :     307270 :                                                     md, lba, lba_count, bdev_nvme_writev_done,
    8262                 :     307270 :                                                     bio, flags, 0, 0);
    8263                 :     307270 :         } else {
    8264                 :     292203 :                 rc = spdk_nvme_ns_cmd_writev_with_md(ns, qpair, lba, lba_count,
    8265                 :          2 :                                                      bdev_nvme_writev_done, bio, flags,
    8266                 :            :                                                      bdev_nvme_queued_reset_sgl,
    8267                 :          2 :                                                      bdev_nvme_queued_next_sge, md, 0, 0);
    8268                 :            :         }
    8269                 :            : 
    8270   [ +  +  -  + ]:    7314813 :         if (spdk_unlikely(rc != 0 && rc != -ENOMEM)) {
    8271                 :          0 :                 SPDK_ERRLOG("writev failed: rc = %d\n", rc);
    8272                 :          0 :         }
    8273                 :    7314813 :         return rc;
    8274                 :            : }
    8275                 :            : 
    8276                 :            : static int
    8277                 :     235234 : bdev_nvme_zone_appendv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
    8278                 :            :                        void *md, uint64_t lba_count, uint64_t zslba,
    8279                 :            :                        uint32_t flags)
    8280                 :            : {
    8281   [ #  #  #  #  :     235234 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8282   [ #  #  #  #  :     235234 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8283                 :            :         int rc;
    8284                 :            : 
    8285   [ -  +  #  #  :     235234 :         SPDK_DEBUGLOG(bdev_nvme, "zone append %" PRIu64 " blocks to zone start lba %#" PRIx64 "\n",
                   #  # ]
    8286                 :            :                       lba_count, zslba);
    8287                 :            : 
    8288   [ #  #  #  # ]:     235234 :         bio->iovs = iov;
    8289   [ #  #  #  # ]:     235234 :         bio->iovcnt = iovcnt;
    8290   [ #  #  #  # ]:     235234 :         bio->iovpos = 0;
    8291   [ #  #  #  # ]:     235234 :         bio->iov_offset = 0;
    8292                 :            : 
    8293         [ +  - ]:     235234 :         if (iovcnt == 1) {
    8294   [ #  #  #  #  :     235234 :                 rc = spdk_nvme_zns_zone_append_with_md(ns, qpair, iov[0].iov_base, md, zslba,
                   #  # ]
    8295                 :          0 :                                                        lba_count,
    8296                 :          0 :                                                        bdev_nvme_zone_appendv_done, bio,
    8297                 :          0 :                                                        flags,
    8298                 :            :                                                        0, 0);
    8299                 :          0 :         } else {
    8300                 :          0 :                 rc = spdk_nvme_zns_zone_appendv_with_md(ns, qpair, zslba, lba_count,
    8301                 :          0 :                                                         bdev_nvme_zone_appendv_done, bio, flags,
    8302                 :            :                                                         bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge,
    8303                 :          0 :                                                         md, 0, 0);
    8304                 :            :         }
    8305                 :            : 
    8306   [ -  +  -  - ]:     235234 :         if (rc != 0 && rc != -ENOMEM) {
    8307                 :          0 :                 SPDK_ERRLOG("zone append failed: rc = %d\n", rc);
    8308                 :          0 :         }
    8309                 :     235234 :         return rc;
    8310                 :            : }
    8311                 :            : 
    8312                 :            : static int
    8313                 :         56 : bdev_nvme_comparev(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
    8314                 :            :                    void *md, uint64_t lba_count, uint64_t lba,
    8315                 :            :                    uint32_t flags)
    8316                 :            : {
    8317                 :            :         int rc;
    8318                 :            : 
    8319   [ +  +  -  +  :         56 :         SPDK_DEBUGLOG(bdev_nvme, "compare %" PRIu64 " blocks with offset %#" PRIx64 "\n",
                   #  # ]
    8320                 :            :                       lba_count, lba);
    8321                 :            : 
    8322   [ #  #  #  # ]:         56 :         bio->iovs = iov;
    8323   [ #  #  #  # ]:         56 :         bio->iovcnt = iovcnt;
    8324   [ #  #  #  # ]:         56 :         bio->iovpos = 0;
    8325   [ #  #  #  # ]:         56 :         bio->iov_offset = 0;
    8326                 :            : 
    8327   [ #  #  #  #  :         59 :         rc = spdk_nvme_ns_cmd_comparev_with_md(bio->io_path->nvme_ns->ns,
          #  #  #  #  #  
                #  #  # ]
    8328   [ #  #  #  #  :         56 :                                                bio->io_path->qpair->qpair,
          #  #  #  #  #  
                #  #  # ]
    8329                 :          3 :                                                lba, lba_count,
    8330                 :          3 :                                                bdev_nvme_comparev_done, bio, flags,
    8331                 :            :                                                bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge,
    8332                 :          3 :                                                md, 0, 0);
    8333                 :            : 
    8334   [ -  +  -  - ]:         56 :         if (rc != 0 && rc != -ENOMEM) {
    8335                 :          0 :                 SPDK_ERRLOG("comparev failed: rc = %d\n", rc);
    8336                 :          0 :         }
    8337                 :         56 :         return rc;
    8338                 :            : }
    8339                 :            : 
    8340                 :            : static int
    8341                 :         18 : bdev_nvme_comparev_and_writev(struct nvme_bdev_io *bio, struct iovec *cmp_iov, int cmp_iovcnt,
    8342                 :            :                               struct iovec *write_iov, int write_iovcnt,
    8343                 :            :                               void *md, uint64_t lba_count, uint64_t lba, uint32_t flags)
    8344                 :            : {
    8345   [ #  #  #  #  :         18 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8346   [ #  #  #  #  :         18 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8347                 :         18 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    8348                 :            :         int rc;
    8349                 :            : 
    8350   [ +  +  -  +  :         18 :         SPDK_DEBUGLOG(bdev_nvme, "compare and write %" PRIu64 " blocks with offset %#" PRIx64 "\n",
                   #  # ]
    8351                 :            :                       lba_count, lba);
    8352                 :            : 
    8353   [ #  #  #  # ]:         18 :         bio->iovs = cmp_iov;
    8354   [ #  #  #  # ]:         18 :         bio->iovcnt = cmp_iovcnt;
    8355   [ #  #  #  # ]:         18 :         bio->iovpos = 0;
    8356   [ #  #  #  # ]:         18 :         bio->iov_offset = 0;
    8357   [ #  #  #  # ]:         18 :         bio->fused_iovs = write_iov;
    8358   [ #  #  #  # ]:         18 :         bio->fused_iovcnt = write_iovcnt;
    8359   [ #  #  #  # ]:         18 :         bio->fused_iovpos = 0;
    8360   [ #  #  #  # ]:         18 :         bio->fused_iov_offset = 0;
    8361                 :            : 
    8362   [ +  +  #  #  :         18 :         if (bdev_io->num_retries == 0) {
                   #  # ]
    8363   [ #  #  #  # ]:         18 :                 bio->first_fused_submitted = false;
    8364   [ #  #  #  # ]:         18 :                 bio->first_fused_completed = false;
    8365                 :          2 :         }
    8366                 :            : 
    8367   [ +  +  +  -  :         18 :         if (!bio->first_fused_submitted) {
             #  #  #  # ]
    8368   [ #  #  #  # ]:         18 :                 flags |= SPDK_NVME_IO_FLAGS_FUSE_FIRST;
    8369   [ -  +  #  # ]:         18 :                 memset(&bio->cpl, 0, sizeof(bio->cpl));
    8370                 :            : 
    8371                 :         20 :                 rc = spdk_nvme_ns_cmd_comparev_with_md(ns, qpair, lba, lba_count,
    8372                 :          2 :                                                        bdev_nvme_comparev_and_writev_done, bio, flags,
    8373                 :          2 :                                                        bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge, md, 0, 0);
    8374         [ +  + ]:         18 :                 if (rc == 0) {
    8375   [ #  #  #  # ]:         18 :                         bio->first_fused_submitted = true;
    8376   [ #  #  #  # ]:         18 :                         flags &= ~SPDK_NVME_IO_FLAGS_FUSE_FIRST;
    8377                 :          2 :                 } else {
    8378         [ #  # ]:          0 :                         if (rc != -ENOMEM) {
    8379                 :          0 :                                 SPDK_ERRLOG("compare failed: rc = %d\n", rc);
    8380                 :          0 :                         }
    8381                 :          0 :                         return rc;
    8382                 :            :                 }
    8383                 :          2 :         }
    8384                 :            : 
    8385   [ #  #  #  # ]:         18 :         flags |= SPDK_NVME_IO_FLAGS_FUSE_SECOND;
    8386                 :            : 
    8387                 :         20 :         rc = spdk_nvme_ns_cmd_writev_with_md(ns, qpair, lba, lba_count,
    8388                 :          2 :                                              bdev_nvme_comparev_and_writev_done, bio, flags,
    8389                 :          2 :                                              bdev_nvme_queued_reset_fused_sgl, bdev_nvme_queued_next_fused_sge, md, 0, 0);
    8390   [ -  +  -  - ]:         18 :         if (rc != 0 && rc != -ENOMEM) {
    8391                 :          0 :                 SPDK_ERRLOG("write failed: rc = %d\n", rc);
    8392                 :          0 :                 rc = 0;
    8393                 :          0 :         }
    8394                 :            : 
    8395                 :         18 :         return rc;
    8396                 :          2 : }
    8397                 :            : 
    8398                 :            : static int
    8399                 :      17153 : bdev_nvme_unmap(struct nvme_bdev_io *bio, uint64_t offset_blocks, uint64_t num_blocks)
    8400                 :            : {
    8401                 :       4722 :         struct spdk_nvme_dsm_range dsm_ranges[SPDK_NVME_DATASET_MANAGEMENT_MAX_RANGES];
    8402                 :            :         struct spdk_nvme_dsm_range *range;
    8403                 :            :         uint64_t offset, remaining;
    8404                 :            :         uint64_t num_ranges_u64;
    8405                 :            :         uint16_t num_ranges;
    8406                 :            :         int rc;
    8407                 :            : 
    8408         [ #  # ]:      17153 :         num_ranges_u64 = (num_blocks + SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS - 1) /
    8409                 :            :                          SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS;
    8410         [ -  + ]:      17153 :         if (num_ranges_u64 > SPDK_COUNTOF(dsm_ranges)) {
    8411                 :          0 :                 SPDK_ERRLOG("Unmap request for %" PRIu64 " blocks is too large\n", num_blocks);
    8412                 :          0 :                 return -EINVAL;
    8413                 :            :         }
    8414                 :      17153 :         num_ranges = (uint16_t)num_ranges_u64;
    8415                 :            : 
    8416                 :      17153 :         offset = offset_blocks;
    8417                 :      17153 :         remaining = num_blocks;
    8418   [ #  #  #  # ]:      17153 :         range = &dsm_ranges[0];
    8419                 :            : 
    8420                 :            :         /* Fill max-size ranges until the remaining blocks fit into one range */
    8421         [ +  + ]:      17160 :         while (remaining > SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS) {
    8422   [ #  #  #  #  :          7 :                 range->attributes.raw = 0;
                   #  # ]
    8423   [ #  #  #  # ]:          7 :                 range->length = SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS;
    8424   [ #  #  #  # ]:          7 :                 range->starting_lba = offset;
    8425                 :            : 
    8426                 :          7 :                 offset += SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS;
    8427                 :          7 :                 remaining -= SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS;
    8428         [ #  # ]:          7 :                 range++;
    8429                 :            :         }
    8430                 :            : 
    8431                 :            :         /* Final range describes the remaining blocks */
    8432   [ #  #  #  #  :      17153 :         range->attributes.raw = 0;
                   #  # ]
    8433   [ #  #  #  # ]:      17153 :         range->length = remaining;
    8434   [ #  #  #  # ]:      17153 :         range->starting_lba = offset;
    8435                 :            : 
    8436   [ #  #  #  #  :      17156 :         rc = spdk_nvme_ns_cmd_dataset_management(bio->io_path->nvme_ns->ns,
          #  #  #  #  #  
                #  #  # ]
    8437   [ #  #  #  #  :      17153 :                         bio->io_path->qpair->qpair,
          #  #  #  #  #  
                #  #  # ]
    8438                 :            :                         SPDK_NVME_DSM_ATTR_DEALLOCATE,
    8439                 :          3 :                         dsm_ranges, num_ranges,
    8440                 :          3 :                         bdev_nvme_queued_done, bio);
    8441                 :            : 
    8442                 :      17153 :         return rc;
    8443                 :          3 : }
    8444                 :            : 
    8445                 :            : static int
    8446                 :     771987 : bdev_nvme_write_zeroes(struct nvme_bdev_io *bio, uint64_t offset_blocks, uint64_t num_blocks)
    8447                 :            : {
    8448         [ -  + ]:     771987 :         if (num_blocks > UINT16_MAX + 1) {
    8449                 :          0 :                 SPDK_ERRLOG("NVMe write zeroes is limited to 16-bit block count\n");
    8450                 :          0 :                 return -EINVAL;
    8451                 :            :         }
    8452                 :            : 
    8453   [ #  #  #  #  :     838986 :         return spdk_nvme_ns_cmd_write_zeroes(bio->io_path->nvme_ns->ns,
          #  #  #  #  #  
                #  #  # ]
    8454   [ #  #  #  #  :     771987 :                                              bio->io_path->qpair->qpair,
          #  #  #  #  #  
                #  #  # ]
    8455                 :      66999 :                                              offset_blocks, num_blocks,
    8456                 :      66999 :                                              bdev_nvme_queued_done, bio,
    8457                 :            :                                              0);
    8458                 :      66999 : }
    8459                 :            : 
    8460                 :            : static int
    8461                 :          1 : bdev_nvme_get_zone_info(struct nvme_bdev_io *bio, uint64_t zone_id, uint32_t num_zones,
    8462                 :            :                         struct spdk_bdev_zone_info *info)
    8463                 :            : {
    8464   [ #  #  #  #  :          1 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8465   [ #  #  #  #  :          1 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8466                 :          1 :         uint32_t zone_report_bufsize = spdk_nvme_ns_get_max_io_xfer_size(ns);
    8467                 :          1 :         uint64_t zone_size = spdk_nvme_zns_ns_get_zone_size_sectors(ns);
    8468                 :          1 :         uint64_t total_zones = spdk_nvme_zns_ns_get_num_zones(ns);
    8469                 :            : 
    8470   [ -  +  #  # ]:          1 :         if (zone_id % zone_size != 0) {
    8471                 :          0 :                 return -EINVAL;
    8472                 :            :         }
    8473                 :            : 
    8474   [ +  -  -  + ]:          1 :         if (num_zones > total_zones || !num_zones) {
    8475                 :          0 :                 return -EINVAL;
    8476                 :            :         }
    8477                 :            : 
    8478   [ -  +  #  #  :          1 :         assert(!bio->zone_report_buf);
             #  #  #  # ]
    8479   [ #  #  #  # ]:          1 :         bio->zone_report_buf = calloc(1, zone_report_bufsize);
    8480   [ -  +  #  #  :          1 :         if (!bio->zone_report_buf) {
                   #  # ]
    8481                 :          0 :                 return -ENOMEM;
    8482                 :            :         }
    8483                 :            : 
    8484   [ #  #  #  # ]:          1 :         bio->handled_zones = 0;
    8485                 :            : 
    8486   [ #  #  #  # ]:          1 :         return spdk_nvme_zns_report_zones(ns, qpair, bio->zone_report_buf, zone_report_bufsize,
    8487                 :          0 :                                           zone_id, SPDK_NVME_ZRA_LIST_ALL, true,
    8488                 :          0 :                                           bdev_nvme_get_zone_info_done, bio);
    8489                 :          0 : }
    8490                 :            : 
    8491                 :            : static int
    8492                 :         43 : bdev_nvme_zone_management(struct nvme_bdev_io *bio, uint64_t zone_id,
    8493                 :            :                           enum spdk_bdev_zone_action action)
    8494                 :            : {
    8495   [ #  #  #  #  :         43 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8496   [ #  #  #  #  :         43 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8497                 :            : 
    8498   [ -  -  -  +  :         43 :         switch (action) {
                   -  - ]
    8499                 :          0 :         case SPDK_BDEV_ZONE_CLOSE:
    8500                 :          0 :                 return spdk_nvme_zns_close_zone(ns, qpair, zone_id, false,
    8501                 :          0 :                                                 bdev_nvme_zone_management_done, bio);
    8502                 :          0 :         case SPDK_BDEV_ZONE_FINISH:
    8503                 :          0 :                 return spdk_nvme_zns_finish_zone(ns, qpair, zone_id, false,
    8504                 :          0 :                                                  bdev_nvme_zone_management_done, bio);
    8505                 :          0 :         case SPDK_BDEV_ZONE_OPEN:
    8506                 :          0 :                 return spdk_nvme_zns_open_zone(ns, qpair, zone_id, false,
    8507                 :          0 :                                                bdev_nvme_zone_management_done, bio);
    8508                 :         43 :         case SPDK_BDEV_ZONE_RESET:
    8509                 :         43 :                 return spdk_nvme_zns_reset_zone(ns, qpair, zone_id, false,
    8510                 :          0 :                                                 bdev_nvme_zone_management_done, bio);
    8511                 :          0 :         case SPDK_BDEV_ZONE_OFFLINE:
    8512                 :          0 :                 return spdk_nvme_zns_offline_zone(ns, qpair, zone_id, false,
    8513                 :          0 :                                                   bdev_nvme_zone_management_done, bio);
    8514                 :          0 :         default:
    8515                 :          0 :                 return -EINVAL;
    8516                 :            :         }
    8517                 :          0 : }
    8518                 :            : 
    8519                 :            : static void
    8520                 :         70 : bdev_nvme_admin_passthru(struct nvme_bdev_channel *nbdev_ch, struct nvme_bdev_io *bio,
    8521                 :            :                          struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes)
    8522                 :            : {
    8523                 :            :         struct nvme_io_path *io_path;
    8524                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    8525                 :            :         uint32_t max_xfer_size;
    8526                 :         70 :         int rc = -ENXIO;
    8527                 :            : 
    8528                 :            :         /* Choose the first ctrlr which is not failed. */
    8529   [ +  +  #  #  :         82 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    8530   [ #  #  #  #  :         78 :                 nvme_ctrlr = io_path->qpair->ctrlr;
             #  #  #  # ]
    8531                 :            : 
    8532                 :            :                 /* We should skip any unavailable nvme_ctrlr rather than checking
    8533                 :            :                  * if the return value of spdk_nvme_ctrlr_cmd_admin_raw() is -ENXIO.
    8534                 :            :                  */
    8535         [ +  + ]:         78 :                 if (!nvme_ctrlr_is_available(nvme_ctrlr)) {
    8536                 :         12 :                         continue;
    8537                 :            :                 }
    8538                 :            : 
    8539   [ #  #  #  # ]:         66 :                 max_xfer_size = spdk_nvme_ctrlr_get_max_xfer_size(nvme_ctrlr->ctrlr);
    8540                 :            : 
    8541         [ +  + ]:         66 :                 if (nbytes > max_xfer_size) {
    8542                 :          0 :                         SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size);
    8543                 :          0 :                         rc = -EINVAL;
    8544                 :          0 :                         goto err;
    8545                 :            :                 }
    8546                 :            : 
    8547   [ #  #  #  # ]:         71 :                 rc = spdk_nvme_ctrlr_cmd_admin_raw(nvme_ctrlr->ctrlr, cmd, buf, (uint32_t)nbytes,
    8548                 :          5 :                                                    bdev_nvme_admin_passthru_done, bio);
    8549         [ +  + ]:         66 :                 if (rc == 0) {
    8550                 :         66 :                         return;
    8551                 :            :                 }
    8552                 :          1 :         }
    8553                 :            : 
    8554                 :          3 : err:
    8555                 :          4 :         bdev_nvme_admin_complete(bio, rc);
    8556                 :          6 : }
    8557                 :            : 
    8558                 :            : static int
    8559                 :         69 : bdev_nvme_io_passthru(struct nvme_bdev_io *bio, struct spdk_nvme_cmd *cmd,
    8560                 :            :                       void *buf, size_t nbytes)
    8561                 :            : {
    8562   [ #  #  #  #  :         69 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8563   [ #  #  #  #  :         69 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8564                 :         69 :         uint32_t max_xfer_size = spdk_nvme_ns_get_max_io_xfer_size(ns);
    8565                 :         69 :         struct spdk_nvme_ctrlr *ctrlr = spdk_nvme_ns_get_ctrlr(ns);
    8566                 :            : 
    8567         [ -  + ]:         69 :         if (nbytes > max_xfer_size) {
    8568                 :          0 :                 SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size);
    8569                 :          0 :                 return -EINVAL;
    8570                 :            :         }
    8571                 :            : 
    8572                 :            :         /*
    8573                 :            :          * Each NVMe bdev is a specific namespace, and all NVMe I/O commands require a nsid,
    8574                 :            :          * so fill it out automatically.
    8575                 :            :          */
    8576   [ #  #  #  # ]:         69 :         cmd->nsid = spdk_nvme_ns_get_id(ns);
    8577                 :            : 
    8578                 :         72 :         return spdk_nvme_ctrlr_cmd_io_raw(ctrlr, qpair, cmd, buf,
    8579                 :          3 :                                           (uint32_t)nbytes, bdev_nvme_queued_done, bio);
    8580                 :          3 : }
    8581                 :            : 
    8582                 :            : static int
    8583                 :          0 : bdev_nvme_io_passthru_md(struct nvme_bdev_io *bio, struct spdk_nvme_cmd *cmd,
    8584                 :            :                          void *buf, size_t nbytes, void *md_buf, size_t md_len)
    8585                 :            : {
    8586   [ #  #  #  #  :          0 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8587   [ #  #  #  #  :          0 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8588         [ #  # ]:          0 :         size_t nr_sectors = nbytes / spdk_nvme_ns_get_extended_sector_size(ns);
    8589                 :          0 :         uint32_t max_xfer_size = spdk_nvme_ns_get_max_io_xfer_size(ns);
    8590                 :          0 :         struct spdk_nvme_ctrlr *ctrlr = spdk_nvme_ns_get_ctrlr(ns);
    8591                 :            : 
    8592         [ #  # ]:          0 :         if (nbytes > max_xfer_size) {
    8593                 :          0 :                 SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size);
    8594                 :          0 :                 return -EINVAL;
    8595                 :            :         }
    8596                 :            : 
    8597         [ #  # ]:          0 :         if (md_len != nr_sectors * spdk_nvme_ns_get_md_size(ns)) {
    8598                 :          0 :                 SPDK_ERRLOG("invalid meta data buffer size\n");
    8599                 :          0 :                 return -EINVAL;
    8600                 :            :         }
    8601                 :            : 
    8602                 :            :         /*
    8603                 :            :          * Each NVMe bdev is a specific namespace, and all NVMe I/O commands require a nsid,
    8604                 :            :          * so fill it out automatically.
    8605                 :            :          */
    8606   [ #  #  #  # ]:          0 :         cmd->nsid = spdk_nvme_ns_get_id(ns);
    8607                 :            : 
    8608                 :          0 :         return spdk_nvme_ctrlr_cmd_io_raw_with_md(ctrlr, qpair, cmd, buf,
    8609                 :          0 :                         (uint32_t)nbytes, md_buf, bdev_nvme_queued_done, bio);
    8610                 :          0 : }
    8611                 :            : 
    8612                 :            : static int
    8613                 :          0 : bdev_nvme_iov_passthru_md(struct nvme_bdev_io *bio,
    8614                 :            :                           struct spdk_nvme_cmd *cmd, struct iovec *iov, int iovcnt,
    8615                 :            :                           size_t nbytes, void *md_buf, size_t md_len)
    8616                 :            : {
    8617   [ #  #  #  #  :          0 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8618   [ #  #  #  #  :          0 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8619         [ #  # ]:          0 :         size_t nr_sectors = nbytes / spdk_nvme_ns_get_extended_sector_size(ns);
    8620                 :          0 :         uint32_t max_xfer_size = spdk_nvme_ns_get_max_io_xfer_size(ns);
    8621                 :          0 :         struct spdk_nvme_ctrlr *ctrlr = spdk_nvme_ns_get_ctrlr(ns);
    8622                 :            : 
    8623   [ #  #  #  # ]:          0 :         bio->iovs = iov;
    8624   [ #  #  #  # ]:          0 :         bio->iovcnt = iovcnt;
    8625   [ #  #  #  # ]:          0 :         bio->iovpos = 0;
    8626   [ #  #  #  # ]:          0 :         bio->iov_offset = 0;
    8627                 :            : 
    8628         [ #  # ]:          0 :         if (nbytes > max_xfer_size) {
    8629                 :          0 :                 SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size);
    8630                 :          0 :                 return -EINVAL;
    8631                 :            :         }
    8632                 :            : 
    8633         [ #  # ]:          0 :         if (md_len != nr_sectors * spdk_nvme_ns_get_md_size(ns)) {
    8634                 :          0 :                 SPDK_ERRLOG("invalid meta data buffer size\n");
    8635                 :          0 :                 return -EINVAL;
    8636                 :            :         }
    8637                 :            : 
    8638                 :            :         /*
    8639                 :            :          * Each NVMe bdev is a specific namespace, and all NVMe I/O commands
    8640                 :            :          * require a nsid, so fill it out automatically.
    8641                 :            :          */
    8642   [ #  #  #  # ]:          0 :         cmd->nsid = spdk_nvme_ns_get_id(ns);
    8643                 :            : 
    8644                 :          0 :         return spdk_nvme_ctrlr_cmd_iov_raw_with_md(
    8645                 :          0 :                        ctrlr, qpair, cmd, (uint32_t)nbytes, md_buf, bdev_nvme_queued_done, bio,
    8646                 :            :                        bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge);
    8647                 :          0 : }
    8648                 :            : 
    8649                 :            : static void
    8650                 :       2553 : bdev_nvme_abort(struct nvme_bdev_channel *nbdev_ch, struct nvme_bdev_io *bio,
    8651                 :            :                 struct nvme_bdev_io *bio_to_abort)
    8652                 :            : {
    8653                 :            :         struct nvme_io_path *io_path;
    8654                 :       2553 :         int rc = 0;
    8655                 :            : 
    8656                 :       2553 :         rc = bdev_nvme_abort_retry_io(nbdev_ch, bio_to_abort);
    8657         [ +  + ]:       2553 :         if (rc == 0) {
    8658                 :          4 :                 bdev_nvme_admin_complete(bio, 0);
    8659                 :          4 :                 return;
    8660                 :            :         }
    8661                 :            : 
    8662   [ #  #  #  # ]:       2549 :         io_path = bio_to_abort->io_path;
    8663         [ +  + ]:       2549 :         if (io_path != NULL) {
    8664   [ #  #  #  #  :       2544 :                 rc = spdk_nvme_ctrlr_cmd_abort_ext(io_path->qpair->ctrlr->ctrlr,
          #  #  #  #  #  
                #  #  # ]
    8665   [ #  #  #  #  :       2541 :                                                    io_path->qpair->qpair,
             #  #  #  # ]
    8666                 :          3 :                                                    bio_to_abort,
    8667                 :          3 :                                                    bdev_nvme_abort_done, bio);
    8668                 :          3 :         } else {
    8669   [ +  +  #  #  :         12 :                 STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    8670   [ #  #  #  #  :         10 :                         rc = spdk_nvme_ctrlr_cmd_abort_ext(io_path->qpair->ctrlr->ctrlr,
          #  #  #  #  #  
                #  #  # ]
    8671                 :            :                                                            NULL,
    8672                 :          2 :                                                            bio_to_abort,
    8673                 :          2 :                                                            bdev_nvme_abort_done, bio);
    8674                 :            : 
    8675         [ +  + ]:          8 :                         if (rc != -ENOENT) {
    8676                 :          4 :                                 break;
    8677                 :            :                         }
    8678                 :          1 :                 }
    8679                 :            :         }
    8680                 :            : 
    8681         [ +  + ]:       2549 :         if (rc != 0) {
    8682                 :            :                 /* If no command was found or there was any error, complete the abort
    8683                 :            :                  * request with failure.
    8684                 :            :                  */
    8685                 :          8 :                 bdev_nvme_admin_complete(bio, rc);
    8686                 :          2 :         }
    8687                 :          6 : }
    8688                 :            : 
    8689                 :            : static int
    8690                 :         30 : bdev_nvme_copy(struct nvme_bdev_io *bio, uint64_t dst_offset_blocks, uint64_t src_offset_blocks,
    8691                 :            :                uint64_t num_blocks)
    8692                 :            : {
    8693                 :         32 :         struct spdk_nvme_scc_source_range range = {
    8694                 :          1 :                 .slba = src_offset_blocks,
    8695                 :         30 :                 .nlb = num_blocks - 1
    8696                 :            :         };
    8697                 :            : 
    8698   [ #  #  #  #  :         39 :         return spdk_nvme_ns_cmd_copy(bio->io_path->nvme_ns->ns,
          #  #  #  #  #  
                #  #  # ]
    8699   [ #  #  #  #  :         30 :                                      bio->io_path->qpair->qpair,
          #  #  #  #  #  
                #  #  # ]
    8700                 :          1 :                                      &range, 1, dst_offset_blocks,
    8701                 :          1 :                                      bdev_nvme_queued_done, bio);
    8702                 :            : }
    8703                 :            : 
    8704                 :            : static void
    8705                 :        142 : bdev_nvme_opts_config_json(struct spdk_json_write_ctx *w)
    8706                 :            : {
    8707                 :            :         const char *action;
    8708                 :            :         uint32_t i;
    8709                 :            : 
    8710         [ -  + ]:        142 :         if (g_opts.action_on_timeout == SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET) {
    8711                 :          0 :                 action = "reset";
    8712         [ +  + ]:        142 :         } else if (g_opts.action_on_timeout == SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT) {
    8713                 :          7 :                 action = "abort";
    8714                 :          1 :         } else {
    8715                 :        135 :                 action = "none";
    8716                 :            :         }
    8717                 :            : 
    8718                 :        142 :         spdk_json_write_object_begin(w);
    8719                 :            : 
    8720                 :        142 :         spdk_json_write_named_string(w, "method", "bdev_nvme_set_options");
    8721                 :            : 
    8722                 :        142 :         spdk_json_write_named_object_begin(w, "params");
    8723                 :        142 :         spdk_json_write_named_string(w, "action_on_timeout", action);
    8724         [ +  - ]:        142 :         spdk_json_write_named_uint64(w, "timeout_us", g_opts.timeout_us);
    8725         [ +  - ]:        142 :         spdk_json_write_named_uint64(w, "timeout_admin_us", g_opts.timeout_admin_us);
    8726         [ +  - ]:        142 :         spdk_json_write_named_uint32(w, "keep_alive_timeout_ms", g_opts.keep_alive_timeout_ms);
    8727         [ +  - ]:        142 :         spdk_json_write_named_uint32(w, "arbitration_burst", g_opts.arbitration_burst);
    8728         [ +  - ]:        142 :         spdk_json_write_named_uint32(w, "low_priority_weight", g_opts.low_priority_weight);
    8729         [ +  - ]:        142 :         spdk_json_write_named_uint32(w, "medium_priority_weight", g_opts.medium_priority_weight);
    8730         [ +  - ]:        142 :         spdk_json_write_named_uint32(w, "high_priority_weight", g_opts.high_priority_weight);
    8731         [ +  - ]:        142 :         spdk_json_write_named_uint64(w, "nvme_adminq_poll_period_us", g_opts.nvme_adminq_poll_period_us);
    8732         [ +  - ]:        142 :         spdk_json_write_named_uint64(w, "nvme_ioq_poll_period_us", g_opts.nvme_ioq_poll_period_us);
    8733         [ +  - ]:        142 :         spdk_json_write_named_uint32(w, "io_queue_requests", g_opts.io_queue_requests);
    8734   [ +  +  +  - ]:        142 :         spdk_json_write_named_bool(w, "delay_cmd_submit", g_opts.delay_cmd_submit);
    8735         [ +  - ]:        142 :         spdk_json_write_named_uint32(w, "transport_retry_count", g_opts.transport_retry_count);
    8736         [ +  - ]:        142 :         spdk_json_write_named_int32(w, "bdev_retry_count", g_opts.bdev_retry_count);
    8737         [ +  - ]:        142 :         spdk_json_write_named_uint8(w, "transport_ack_timeout", g_opts.transport_ack_timeout);
    8738         [ +  - ]:        142 :         spdk_json_write_named_int32(w, "ctrlr_loss_timeout_sec", g_opts.ctrlr_loss_timeout_sec);
    8739         [ +  - ]:        142 :         spdk_json_write_named_uint32(w, "reconnect_delay_sec", g_opts.reconnect_delay_sec);
    8740         [ +  - ]:        142 :         spdk_json_write_named_uint32(w, "fast_io_fail_timeout_sec", g_opts.fast_io_fail_timeout_sec);
    8741   [ +  +  +  - ]:        142 :         spdk_json_write_named_bool(w, "disable_auto_failback", g_opts.disable_auto_failback);
    8742         [ +  + ]:        142 :         spdk_json_write_named_bool(w, "generate_uuids", g_opts.generate_uuids);
    8743         [ +  - ]:        142 :         spdk_json_write_named_uint8(w, "transport_tos", g_opts.transport_tos);
    8744         [ +  + ]:        142 :         spdk_json_write_named_bool(w, "nvme_error_stat", g_opts.nvme_error_stat);
    8745         [ +  - ]:        142 :         spdk_json_write_named_uint32(w, "rdma_srq_size", g_opts.rdma_srq_size);
    8746   [ +  +  +  - ]:        142 :         spdk_json_write_named_bool(w, "io_path_stat", g_opts.io_path_stat);
    8747         [ +  + ]:        142 :         spdk_json_write_named_bool(w, "allow_accel_sequence", g_opts.allow_accel_sequence);
    8748         [ +  - ]:        142 :         spdk_json_write_named_uint32(w, "rdma_max_cq_size", g_opts.rdma_max_cq_size);
    8749         [ +  - ]:        142 :         spdk_json_write_named_uint16(w, "rdma_cm_event_timeout_ms", g_opts.rdma_cm_event_timeout_ms);
    8750                 :        142 :         spdk_json_write_named_array_begin(w, "dhchap_digests");
    8751         [ +  + ]:       4686 :         for (i = 0; i < 32; ++i) {
    8752   [ +  +  +  +  :       4544 :                 if (g_opts.dhchap_digests & SPDK_BIT(i)) {
                   +  + ]
    8753                 :        426 :                         spdk_json_write_string(w, spdk_nvme_dhchap_get_digest_name(i));
    8754                 :         33 :                 }
    8755                 :        352 :         }
    8756                 :        142 :         spdk_json_write_array_end(w);
    8757                 :        142 :         spdk_json_write_named_array_begin(w, "dhchap_dhgroups");
    8758         [ +  + ]:       4686 :         for (i = 0; i < 32; ++i) {
    8759   [ +  +  +  +  :       4544 :                 if (g_opts.dhchap_dhgroups & SPDK_BIT(i)) {
                   +  + ]
    8760                 :        852 :                         spdk_json_write_string(w, spdk_nvme_dhchap_get_dhgroup_name(i));
    8761                 :         66 :                 }
    8762                 :        352 :         }
    8763                 :            : 
    8764                 :        142 :         spdk_json_write_array_end(w);
    8765                 :        142 :         spdk_json_write_object_end(w);
    8766                 :            : 
    8767                 :        142 :         spdk_json_write_object_end(w);
    8768                 :        142 : }
    8769                 :            : 
    8770                 :            : static void
    8771                 :          0 : bdev_nvme_discovery_config_json(struct spdk_json_write_ctx *w, struct discovery_ctx *ctx)
    8772                 :            : {
    8773                 :          0 :         struct spdk_nvme_transport_id trid;
    8774                 :            : 
    8775                 :          0 :         spdk_json_write_object_begin(w);
    8776                 :            : 
    8777                 :          0 :         spdk_json_write_named_string(w, "method", "bdev_nvme_start_discovery");
    8778                 :            : 
    8779                 :          0 :         spdk_json_write_named_object_begin(w, "params");
    8780   [ #  #  #  # ]:          0 :         spdk_json_write_named_string(w, "name", ctx->name);
    8781   [ #  #  #  # ]:          0 :         spdk_json_write_named_string(w, "hostnqn", ctx->hostnqn);
    8782                 :            : 
    8783         [ #  # ]:          0 :         trid = ctx->trid;
    8784         [ #  # ]:          0 :         memset(trid.subnqn, 0, sizeof(trid.subnqn));
    8785                 :          0 :         nvme_bdev_dump_trid_json(&trid, w);
    8786                 :            : 
    8787   [ #  #  #  #  :          0 :         spdk_json_write_named_bool(w, "wait_for_attach", ctx->wait_for_attach);
                   #  # ]
    8788   [ #  #  #  #  :          0 :         spdk_json_write_named_int32(w, "ctrlr_loss_timeout_sec", ctx->bdev_opts.ctrlr_loss_timeout_sec);
                   #  # ]
    8789   [ #  #  #  #  :          0 :         spdk_json_write_named_uint32(w, "reconnect_delay_sec", ctx->bdev_opts.reconnect_delay_sec);
                   #  # ]
    8790                 :          0 :         spdk_json_write_named_uint32(w, "fast_io_fail_timeout_sec",
    8791   [ #  #  #  #  :          0 :                                      ctx->bdev_opts.fast_io_fail_timeout_sec);
                   #  # ]
    8792                 :          0 :         spdk_json_write_object_end(w);
    8793                 :            : 
    8794                 :          0 :         spdk_json_write_object_end(w);
    8795                 :          0 : }
    8796                 :            : 
    8797                 :            : #ifdef SPDK_CONFIG_NVME_CUSE
    8798                 :            : static void
    8799                 :         90 : nvme_ctrlr_cuse_config_json(struct spdk_json_write_ctx *w,
    8800                 :            :                             struct nvme_ctrlr *nvme_ctrlr)
    8801                 :         90 : {
    8802                 :         90 :         size_t cuse_name_size = 128;
    8803         [ -  + ]:         90 :         char cuse_name[cuse_name_size];
    8804                 :            : 
    8805   [ +  -  #  #  :         90 :         if (spdk_nvme_cuse_get_ctrlr_name(nvme_ctrlr->ctrlr,
             #  #  #  # ]
    8806                 :          0 :                                           cuse_name, &cuse_name_size) != 0) {
    8807                 :         90 :                 return;
    8808                 :            :         }
    8809                 :            : 
    8810                 :          0 :         spdk_json_write_object_begin(w);
    8811                 :            : 
    8812                 :          0 :         spdk_json_write_named_string(w, "method", "bdev_nvme_cuse_register");
    8813                 :            : 
    8814                 :          0 :         spdk_json_write_named_object_begin(w, "params");
    8815   [ #  #  #  #  :          0 :         spdk_json_write_named_string(w, "name", nvme_ctrlr->nbdev_ctrlr->name);
             #  #  #  # ]
    8816                 :          0 :         spdk_json_write_object_end(w);
    8817                 :            : 
    8818                 :          0 :         spdk_json_write_object_end(w);
    8819         [ #  # ]:          0 : }
    8820                 :            : #endif
    8821                 :            : 
    8822                 :            : static void
    8823                 :         95 : nvme_ctrlr_config_json(struct spdk_json_write_ctx *w,
    8824                 :            :                        struct nvme_ctrlr *nvme_ctrlr,
    8825                 :            :                        struct nvme_path_id *path_id)
    8826                 :            : {
    8827                 :            :         struct spdk_nvme_transport_id   *trid;
    8828                 :            :         const struct spdk_nvme_ctrlr_opts *opts;
    8829                 :            : 
    8830   [ -  +  -  +  :         95 :         if (nvme_ctrlr->opts.from_discovery_service) {
          #  #  #  #  #  
                      # ]
    8831                 :            :                 /* Do not emit an RPC for this - it will be implicitly
    8832                 :            :                  * covered by a separate bdev_nvme_start_discovery or
    8833                 :            :                  * bdev_nvme_start_mdns_discovery RPC.
    8834                 :            :                  */
    8835                 :          0 :                 return;
    8836                 :            :         }
    8837                 :            : 
    8838         [ #  # ]:         95 :         trid = &path_id->trid;
    8839                 :            : 
    8840                 :         95 :         spdk_json_write_object_begin(w);
    8841                 :            : 
    8842                 :         95 :         spdk_json_write_named_string(w, "method", "bdev_nvme_attach_controller");
    8843                 :            : 
    8844                 :         95 :         spdk_json_write_named_object_begin(w, "params");
    8845   [ #  #  #  #  :         95 :         spdk_json_write_named_string(w, "name", nvme_ctrlr->nbdev_ctrlr->name);
             #  #  #  # ]
    8846                 :         95 :         nvme_bdev_dump_trid_json(trid, w);
    8847                 :        100 :         spdk_json_write_named_bool(w, "prchk_reftag",
    8848   [ #  #  #  #  :         95 :                                    (nvme_ctrlr->opts.prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_REFTAG) != 0);
             #  #  #  # ]
    8849                 :        100 :         spdk_json_write_named_bool(w, "prchk_guard",
    8850   [ #  #  #  #  :         95 :                                    (nvme_ctrlr->opts.prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_GUARD) != 0);
             #  #  #  # ]
    8851   [ #  #  #  #  :         95 :         spdk_json_write_named_int32(w, "ctrlr_loss_timeout_sec", nvme_ctrlr->opts.ctrlr_loss_timeout_sec);
                   #  # ]
    8852   [ #  #  #  #  :         95 :         spdk_json_write_named_uint32(w, "reconnect_delay_sec", nvme_ctrlr->opts.reconnect_delay_sec);
                   #  # ]
    8853                 :        100 :         spdk_json_write_named_uint32(w, "fast_io_fail_timeout_sec",
    8854   [ #  #  #  #  :          5 :                                      nvme_ctrlr->opts.fast_io_fail_timeout_sec);
                   #  # ]
    8855   [ +  +  #  #  :         95 :         if (nvme_ctrlr->psk != NULL) {
                   #  # ]
    8856   [ #  #  #  # ]:          3 :                 spdk_json_write_named_string(w, "psk", spdk_key_get_name(nvme_ctrlr->psk));
    8857                 :          0 :         }
    8858   [ +  +  #  #  :         95 :         if (nvme_ctrlr->dhchap_key != NULL) {
                   #  # ]
    8859                 :          0 :                 spdk_json_write_named_string(w, "dhchap_key",
    8860   [ #  #  #  # ]:          0 :                                              spdk_key_get_name(nvme_ctrlr->dhchap_key));
    8861                 :          0 :         }
    8862   [ +  +  #  #  :         95 :         if (nvme_ctrlr->dhchap_ctrlr_key != NULL) {
                   #  # ]
    8863                 :          0 :                 spdk_json_write_named_string(w, "dhchap_ctrlr_key",
    8864   [ #  #  #  # ]:          0 :                                              spdk_key_get_name(nvme_ctrlr->dhchap_ctrlr_key));
    8865                 :          0 :         }
    8866   [ #  #  #  # ]:         95 :         opts = spdk_nvme_ctrlr_get_opts(nvme_ctrlr->ctrlr);
    8867         [ #  # ]:         95 :         spdk_json_write_named_string(w, "hostnqn", opts->hostnqn);
    8868   [ -  +  #  #  :         95 :         spdk_json_write_named_bool(w, "hdgst", opts->header_digest);
                   #  # ]
    8869   [ -  +  #  #  :         95 :         spdk_json_write_named_bool(w, "ddgst", opts->data_digest);
                   #  # ]
    8870   [ +  +  #  #  :         95 :         if (opts->src_addr[0] != '\0') {
          #  #  #  #  #  
                      # ]
    8871         [ #  # ]:          0 :                 spdk_json_write_named_string(w, "hostaddr", opts->src_addr);
    8872                 :          0 :         }
    8873   [ +  +  #  #  :         95 :         if (opts->src_svcid[0] != '\0') {
          #  #  #  #  #  
                      # ]
    8874         [ #  # ]:          0 :                 spdk_json_write_named_string(w, "hostsvcid", opts->src_svcid);
    8875                 :          0 :         }
    8876                 :            : 
    8877   [ +  +  +  -  :         95 :         if (nvme_ctrlr->opts.multipath) {
          #  #  #  #  #  
                      # ]
    8878                 :         95 :                 spdk_json_write_named_string(w, "multipath", "multipath");
    8879                 :          5 :         }
    8880                 :         95 :         spdk_json_write_object_end(w);
    8881                 :            : 
    8882                 :         95 :         spdk_json_write_object_end(w);
    8883                 :          5 : }
    8884                 :            : 
    8885                 :            : static void
    8886                 :        142 : bdev_nvme_hotplug_config_json(struct spdk_json_write_ctx *w)
    8887                 :            : {
    8888                 :        142 :         spdk_json_write_object_begin(w);
    8889                 :        142 :         spdk_json_write_named_string(w, "method", "bdev_nvme_set_hotplug");
    8890                 :            : 
    8891                 :        142 :         spdk_json_write_named_object_begin(w, "params");
    8892                 :        142 :         spdk_json_write_named_uint64(w, "period_us", g_nvme_hotplug_poll_period_us);
    8893         [ +  + ]:        142 :         spdk_json_write_named_bool(w, "enable", g_nvme_hotplug_enabled);
    8894                 :        142 :         spdk_json_write_object_end(w);
    8895                 :            : 
    8896                 :        142 :         spdk_json_write_object_end(w);
    8897                 :        142 : }
    8898                 :            : 
    8899                 :            : static int
    8900                 :        142 : bdev_nvme_config_json(struct spdk_json_write_ctx *w)
    8901                 :            : {
    8902                 :            :         struct nvme_bdev_ctrlr  *nbdev_ctrlr;
    8903                 :            :         struct nvme_ctrlr       *nvme_ctrlr;
    8904                 :            :         struct discovery_ctx    *ctx;
    8905                 :            :         struct nvme_path_id     *path_id;
    8906                 :            : 
    8907                 :        142 :         bdev_nvme_opts_config_json(w);
    8908                 :            : 
    8909         [ +  + ]:        142 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    8910                 :            : 
    8911   [ +  +  #  #  :        237 :         TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
             #  #  #  # ]
    8912   [ +  +  #  #  :        190 :                 TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    8913   [ #  #  #  # ]:         95 :                         path_id = nvme_ctrlr->active_path_id;
    8914   [ +  +  #  #  :         95 :                         assert(path_id == TAILQ_FIRST(&nvme_ctrlr->trids));
          #  #  #  #  #  
                      # ]
    8915                 :         95 :                         nvme_ctrlr_config_json(w, nvme_ctrlr, path_id);
    8916                 :            : 
    8917   [ #  #  #  #  :         95 :                         path_id = TAILQ_NEXT(path_id, link);
                   #  # ]
    8918         [ -  + ]:         95 :                         while (path_id != NULL) {
    8919                 :          0 :                                 nvme_ctrlr_config_json(w, nvme_ctrlr, path_id);
    8920   [ #  #  #  #  :          0 :                                 path_id = TAILQ_NEXT(path_id, link);
                   #  # ]
    8921                 :            :                         }
    8922                 :            : 
    8923                 :            : #ifdef SPDK_CONFIG_NVME_CUSE
    8924                 :         90 :                         nvme_ctrlr_cuse_config_json(w, nvme_ctrlr);
    8925                 :            : #endif
    8926                 :          5 :                 }
    8927                 :          5 :         }
    8928                 :            : 
    8929   [ -  +  #  #  :        142 :         TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
             #  #  #  # ]
    8930   [ #  #  #  #  :          0 :                 if (!ctx->from_mdns_discovery_service) {
             #  #  #  # ]
    8931                 :          0 :                         bdev_nvme_discovery_config_json(w, ctx);
    8932                 :          0 :                 }
    8933                 :          0 :         }
    8934                 :            : 
    8935                 :        142 :         bdev_nvme_mdns_discovery_config_json(w);
    8936                 :            : 
    8937                 :            :         /* Dump as last parameter to give all NVMe bdevs chance to be constructed
    8938                 :            :          * before enabling hotplug poller.
    8939                 :            :          */
    8940                 :        142 :         bdev_nvme_hotplug_config_json(w);
    8941                 :            : 
    8942         [ +  + ]:        142 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    8943                 :        142 :         return 0;
    8944                 :            : }
    8945                 :            : 
    8946                 :            : struct spdk_nvme_ctrlr *
    8947                 :         11 : bdev_nvme_get_ctrlr(struct spdk_bdev *bdev)
    8948                 :            : {
    8949                 :            :         struct nvme_bdev *nbdev;
    8950                 :            :         struct nvme_ns *nvme_ns;
    8951                 :            : 
    8952   [ +  -  -  +  :         11 :         if (!bdev || bdev->module != &nvme_if) {
             #  #  #  # ]
    8953                 :          0 :                 return NULL;
    8954                 :            :         }
    8955                 :            : 
    8956                 :         11 :         nbdev = SPDK_CONTAINEROF(bdev, struct nvme_bdev, disk);
    8957   [ #  #  #  #  :         11 :         nvme_ns = TAILQ_FIRST(&nbdev->nvme_ns_list);
                   #  # ]
    8958   [ +  +  #  # ]:         11 :         assert(nvme_ns != NULL);
    8959                 :            : 
    8960   [ #  #  #  #  :         11 :         return nvme_ns->ctrlr->ctrlr;
             #  #  #  # ]
    8961                 :          2 : }
    8962                 :            : 
    8963                 :            : static bool
    8964                 :        168 : nvme_io_path_is_current(struct nvme_io_path *io_path)
    8965                 :            : {
    8966                 :            :         const struct nvme_bdev_channel *nbdev_ch;
    8967                 :            :         bool current;
    8968                 :            : 
    8969         [ +  + ]:        168 :         if (!nvme_io_path_is_available(io_path)) {
    8970                 :         46 :                 return false;
    8971                 :            :         }
    8972                 :            : 
    8973   [ #  #  #  # ]:        122 :         nbdev_ch = io_path->nbdev_ch;
    8974         [ +  + ]:        122 :         if (nbdev_ch == NULL) {
    8975                 :          4 :                 current = false;
    8976   [ +  +  #  #  :        119 :         } else if (nbdev_ch->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE) {
                   #  # ]
    8977                 :         54 :                 struct nvme_io_path *optimized_io_path = NULL;
    8978                 :            : 
    8979   [ +  +  #  #  :        114 :                 STAILQ_FOREACH(optimized_io_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    8980   [ +  +  #  #  :         92 :                         if (optimized_io_path->nvme_ns->ana_state == SPDK_NVME_ANA_OPTIMIZED_STATE) {
          #  #  #  #  #  
                      # ]
    8981                 :         32 :                                 break;
    8982                 :            :                         }
    8983                 :          3 :                 }
    8984                 :            : 
    8985                 :            :                 /* A non-optimized path is only current if there are no optimized paths. */
    8986   [ +  +  +  +  :         54 :                 current = (io_path->nvme_ns->ana_state == SPDK_NVME_ANA_OPTIMIZED_STATE) ||
          #  #  #  #  #  
                      # ]
    8987                 :          2 :                           (optimized_io_path == NULL);
    8988                 :          3 :         } else {
    8989   [ +  +  #  #  :         64 :                 if (nbdev_ch->current_io_path) {
                   #  # ]
    8990   [ #  #  #  # ]:         34 :                         current = (io_path == nbdev_ch->current_io_path);
    8991                 :          1 :                 } else {
    8992                 :            :                         struct nvme_io_path *first_path;
    8993                 :            : 
    8994                 :            :                         /* We arrived here as there are no optimized paths for active-passive
    8995                 :            :                          * mode. Check if this io_path is the first one available on the list.
    8996                 :            :                          */
    8997                 :         30 :                         current = false;
    8998   [ +  +  #  #  :         30 :                         STAILQ_FOREACH(first_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    8999         [ +  - ]:         30 :                                 if (nvme_io_path_is_available(first_path)) {
    9000                 :         30 :                                         current = (io_path == first_path);
    9001                 :         30 :                                         break;
    9002                 :            :                                 }
    9003                 :          0 :                         }
    9004                 :            :                 }
    9005                 :            :         }
    9006                 :            : 
    9007         [ #  # ]:        122 :         return current;
    9008                 :         12 : }
    9009                 :            : 
    9010                 :            : static struct nvme_ctrlr *
    9011                 :         10 : bdev_nvme_next_ctrlr_unsafe(struct nvme_bdev_ctrlr *nbdev_ctrlr, struct nvme_ctrlr *prev)
    9012                 :            : {
    9013                 :            :         struct nvme_ctrlr *next;
    9014                 :            : 
    9015                 :            :         /* Must be called under g_bdev_nvme_mutex */
    9016   [ +  +  #  #  :         10 :         next = prev != NULL ? TAILQ_NEXT(prev, tailq) : TAILQ_FIRST(&nbdev_ctrlr->ctrlrs);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    9017         [ +  + ]:         10 :         while (next != NULL) {
    9018                 :            :                 /* ref can be 0 when the ctrlr was released, but hasn't been detached yet */
    9019   [ -  +  #  # ]:          7 :                 pthread_mutex_lock(&next->mutex);
    9020   [ +  -  #  #  :          7 :                 if (next->ref > 0) {
                   #  # ]
    9021   [ #  #  #  # ]:          7 :                         next->ref++;
    9022   [ -  +  #  # ]:          7 :                         pthread_mutex_unlock(&next->mutex);
    9023                 :          7 :                         return next;
    9024                 :            :                 }
    9025                 :            : 
    9026   [ #  #  #  # ]:          0 :                 pthread_mutex_unlock(&next->mutex);
    9027   [ #  #  #  #  :          0 :                 next = TAILQ_NEXT(next, tailq);
                   #  # ]
    9028                 :            :         }
    9029                 :            : 
    9030                 :          3 :         return NULL;
    9031                 :          0 : }
    9032                 :            : 
    9033                 :            : struct bdev_nvme_set_keys_ctx {
    9034                 :            :         struct nvme_ctrlr       *nctrlr;
    9035                 :            :         struct spdk_key         *dhchap_key;
    9036                 :            :         struct spdk_key         *dhchap_ctrlr_key;
    9037                 :            :         struct spdk_thread      *thread;
    9038                 :            :         bdev_nvme_set_keys_cb   cb_fn;
    9039                 :            :         void                    *cb_ctx;
    9040                 :            :         int                     status;
    9041                 :            : };
    9042                 :            : 
    9043                 :            : static void
    9044                 :          7 : bdev_nvme_free_set_keys_ctx(struct bdev_nvme_set_keys_ctx *ctx)
    9045                 :            : {
    9046         [ -  + ]:          7 :         if (ctx == NULL) {
    9047                 :          0 :                 return;
    9048                 :            :         }
    9049                 :            : 
    9050   [ #  #  #  # ]:          7 :         spdk_keyring_put_key(ctx->dhchap_key);
    9051   [ #  #  #  # ]:          7 :         spdk_keyring_put_key(ctx->dhchap_ctrlr_key);
    9052                 :          7 :         free(ctx);
    9053                 :          0 : }
    9054                 :            : 
    9055                 :            : static void
    9056                 :          7 : _bdev_nvme_set_keys_done(void *_ctx)
    9057                 :            : {
    9058                 :          7 :         struct bdev_nvme_set_keys_ctx *ctx = _ctx;
    9059                 :            : 
    9060   [ #  #  #  #  :          7 :         ctx->cb_fn(ctx->cb_ctx, ctx->status);
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    9061                 :            : 
    9062   [ +  +  #  #  :          7 :         if (ctx->nctrlr != NULL) {
                   #  # ]
    9063   [ #  #  #  # ]:          4 :                 nvme_ctrlr_put_ref(ctx->nctrlr);
    9064                 :          0 :         }
    9065                 :          7 :         bdev_nvme_free_set_keys_ctx(ctx);
    9066                 :          7 : }
    9067                 :            : 
    9068                 :            : static void
    9069                 :          7 : bdev_nvme_set_keys_done(struct bdev_nvme_set_keys_ctx *ctx, int status)
    9070                 :            : {
    9071   [ #  #  #  # ]:          7 :         ctx->status = status;
    9072   [ #  #  #  # ]:          7 :         spdk_thread_exec_msg(ctx->thread, _bdev_nvme_set_keys_done, ctx);
    9073                 :          7 : }
    9074                 :            : 
    9075                 :            : static void bdev_nvme_authenticate_ctrlr(struct bdev_nvme_set_keys_ctx *ctx);
    9076                 :            : 
    9077                 :            : static void
    9078                 :          3 : bdev_nvme_authenticate_ctrlr_continue(struct bdev_nvme_set_keys_ctx *ctx)
    9079                 :            : {
    9080                 :            :         struct nvme_ctrlr *next;
    9081                 :            : 
    9082         [ -  + ]:          3 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    9083   [ #  #  #  # ]:          3 :         next = bdev_nvme_next_ctrlr_unsafe(NULL, ctx->nctrlr);
    9084         [ -  + ]:          3 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    9085                 :            : 
    9086   [ #  #  #  # ]:          3 :         nvme_ctrlr_put_ref(ctx->nctrlr);
    9087   [ #  #  #  # ]:          3 :         ctx->nctrlr = next;
    9088                 :            : 
    9089         [ +  - ]:          3 :         if (next == NULL) {
    9090                 :          3 :                 bdev_nvme_set_keys_done(ctx, 0);
    9091                 :          0 :         } else {
    9092                 :          0 :                 bdev_nvme_authenticate_ctrlr(ctx);
    9093                 :            :         }
    9094                 :          3 : }
    9095                 :            : 
    9096                 :            : static void
    9097                 :          2 : bdev_nvme_authenticate_qpairs_done(struct spdk_io_channel_iter *i, int status)
    9098                 :            : {
    9099                 :          2 :         struct bdev_nvme_set_keys_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    9100                 :            : 
    9101         [ -  + ]:          2 :         if (status != 0) {
    9102                 :          0 :                 bdev_nvme_set_keys_done(ctx, status);
    9103                 :          0 :                 return;
    9104                 :            :         }
    9105                 :          2 :         bdev_nvme_authenticate_ctrlr_continue(ctx);
    9106                 :          0 : }
    9107                 :            : 
    9108                 :            : static void
    9109                 :          0 : bdev_nvme_authenticate_qpair_done(void *ctx, int status)
    9110                 :            : {
    9111                 :          0 :         spdk_for_each_channel_continue(ctx, status);
    9112                 :          0 : }
    9113                 :            : 
    9114                 :            : static void
    9115                 :          0 : bdev_nvme_authenticate_qpair(struct spdk_io_channel_iter *i)
    9116                 :            : {
    9117                 :          0 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
    9118                 :          0 :         struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch);
    9119   [ #  #  #  # ]:          0 :         struct nvme_qpair *qpair = ctrlr_ch->qpair;
    9120                 :            :         int rc;
    9121                 :            : 
    9122         [ #  # ]:          0 :         if (!nvme_qpair_is_connected(qpair)) {
    9123                 :          0 :                 spdk_for_each_channel_continue(i, 0);
    9124                 :          0 :                 return;
    9125                 :            :         }
    9126                 :            : 
    9127   [ #  #  #  # ]:          0 :         rc = spdk_nvme_qpair_authenticate(qpair->qpair, bdev_nvme_authenticate_qpair_done, i);
    9128         [ #  # ]:          0 :         if (rc != 0) {
    9129                 :          0 :                 spdk_for_each_channel_continue(i, rc);
    9130                 :          0 :         }
    9131                 :          0 : }
    9132                 :            : 
    9133                 :            : static void
    9134                 :          4 : bdev_nvme_authenticate_ctrlr_done(void *_ctx, int status)
    9135                 :            : {
    9136                 :          4 :         struct bdev_nvme_set_keys_ctx *ctx = _ctx;
    9137                 :            : 
    9138         [ +  + ]:          4 :         if (status != 0) {
    9139                 :          2 :                 bdev_nvme_set_keys_done(ctx, status);
    9140                 :          2 :                 return;
    9141                 :            :         }
    9142                 :            : 
    9143   [ #  #  #  # ]:          2 :         spdk_for_each_channel(ctx->nctrlr, bdev_nvme_authenticate_qpair, ctx,
    9144                 :            :                               bdev_nvme_authenticate_qpairs_done);
    9145                 :          0 : }
    9146                 :            : 
    9147                 :            : static void
    9148                 :          7 : bdev_nvme_authenticate_ctrlr(struct bdev_nvme_set_keys_ctx *ctx)
    9149                 :            : {
    9150                 :          7 :         struct spdk_nvme_ctrlr_key_opts opts = {};
    9151   [ #  #  #  # ]:          7 :         struct nvme_ctrlr *nctrlr = ctx->nctrlr;
    9152                 :            :         int rc;
    9153                 :            : 
    9154                 :          7 :         opts.size = SPDK_SIZEOF(&opts, dhchap_ctrlr_key);
    9155   [ #  #  #  #  :          7 :         opts.dhchap_key = ctx->dhchap_key;
                   #  # ]
    9156   [ #  #  #  #  :          7 :         opts.dhchap_ctrlr_key = ctx->dhchap_ctrlr_key;
                   #  # ]
    9157   [ #  #  #  # ]:          7 :         rc = spdk_nvme_ctrlr_set_keys(nctrlr->ctrlr, &opts);
    9158         [ -  + ]:          7 :         if (rc != 0) {
    9159                 :          0 :                 bdev_nvme_set_keys_done(ctx, rc);
    9160                 :          0 :                 return;
    9161                 :            :         }
    9162                 :            : 
    9163   [ +  +  #  #  :          7 :         if (ctx->dhchap_key != NULL) {
                   #  # ]
    9164   [ #  #  #  # ]:          6 :                 rc = spdk_nvme_ctrlr_authenticate(nctrlr->ctrlr,
    9165                 :          0 :                                                   bdev_nvme_authenticate_ctrlr_done, ctx);
    9166         [ +  + ]:          6 :                 if (rc != 0) {
    9167                 :          2 :                         bdev_nvme_set_keys_done(ctx, rc);
    9168                 :          0 :                 }
    9169                 :          0 :         } else {
    9170                 :          1 :                 bdev_nvme_authenticate_ctrlr_continue(ctx);
    9171                 :            :         }
    9172                 :          0 : }
    9173                 :            : 
    9174                 :            : int
    9175                 :          7 : bdev_nvme_set_keys(const char *name, const char *dhchap_key, const char *dhchap_ctrlr_key,
    9176                 :            :                    bdev_nvme_set_keys_cb cb_fn, void *cb_ctx)
    9177                 :            : {
    9178                 :            :         struct bdev_nvme_set_keys_ctx *ctx;
    9179                 :            :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
    9180                 :            :         struct nvme_ctrlr *nctrlr;
    9181                 :            : 
    9182                 :          7 :         ctx = calloc(1, sizeof(*ctx));
    9183         [ -  + ]:          7 :         if (ctx == NULL) {
    9184                 :          0 :                 return -ENOMEM;
    9185                 :            :         }
    9186                 :            : 
    9187         [ +  + ]:          7 :         if (dhchap_key != NULL) {
    9188   [ #  #  #  # ]:          6 :                 ctx->dhchap_key = spdk_keyring_get_key(dhchap_key);
    9189   [ -  +  #  #  :          6 :                 if (ctx->dhchap_key == NULL) {
                   #  # ]
    9190                 :          0 :                         SPDK_ERRLOG("Could not find key %s for bdev %s\n", dhchap_key, name);
    9191                 :          0 :                         bdev_nvme_free_set_keys_ctx(ctx);
    9192                 :          0 :                         return -ENOKEY;
    9193                 :            :                 }
    9194                 :          0 :         }
    9195         [ +  + ]:          7 :         if (dhchap_ctrlr_key != NULL) {
    9196   [ #  #  #  # ]:          6 :                 ctx->dhchap_ctrlr_key = spdk_keyring_get_key(dhchap_ctrlr_key);
    9197   [ -  +  #  #  :          6 :                 if (ctx->dhchap_ctrlr_key == NULL) {
                   #  # ]
    9198                 :          0 :                         SPDK_ERRLOG("Could not find key %s for bdev %s\n", dhchap_ctrlr_key, name);
    9199                 :          0 :                         bdev_nvme_free_set_keys_ctx(ctx);
    9200                 :          0 :                         return -ENOKEY;
    9201                 :            :                 }
    9202                 :          0 :         }
    9203                 :            : 
    9204         [ -  + ]:          7 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    9205                 :          7 :         nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
    9206         [ -  + ]:          7 :         if (nbdev_ctrlr == NULL) {
    9207                 :          0 :                 SPDK_ERRLOG("Could not find bdev_ctrlr %s\n", name);
    9208         [ #  # ]:          0 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
    9209                 :          0 :                 bdev_nvme_free_set_keys_ctx(ctx);
    9210                 :          0 :                 return -ENODEV;
    9211                 :            :         }
    9212                 :          7 :         nctrlr = bdev_nvme_next_ctrlr_unsafe(nbdev_ctrlr, NULL);
    9213         [ -  + ]:          7 :         if (nctrlr == NULL) {
    9214                 :          0 :                 SPDK_ERRLOG("Could not find any nvme_ctrlrs on bdev_ctrlr %s\n", name);
    9215         [ #  # ]:          0 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
    9216                 :          0 :                 bdev_nvme_free_set_keys_ctx(ctx);
    9217                 :          0 :                 return -ENODEV;
    9218                 :            :         }
    9219         [ -  + ]:          7 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    9220                 :            : 
    9221   [ #  #  #  # ]:          7 :         ctx->nctrlr = nctrlr;
    9222   [ #  #  #  # ]:          7 :         ctx->cb_fn = cb_fn;
    9223   [ #  #  #  # ]:          7 :         ctx->cb_ctx = cb_ctx;
    9224   [ #  #  #  # ]:          7 :         ctx->thread = spdk_get_thread();
    9225                 :            : 
    9226                 :          7 :         bdev_nvme_authenticate_ctrlr(ctx);
    9227                 :            : 
    9228                 :          7 :         return 0;
    9229                 :          0 : }
    9230                 :            : 
    9231                 :            : void
    9232                 :        120 : nvme_io_path_info_json(struct spdk_json_write_ctx *w, struct nvme_io_path *io_path)
    9233                 :            : {
    9234   [ #  #  #  # ]:        120 :         struct nvme_ns *nvme_ns = io_path->nvme_ns;
    9235   [ #  #  #  #  :        120 :         struct nvme_ctrlr *nvme_ctrlr = io_path->qpair->ctrlr;
             #  #  #  # ]
    9236                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    9237                 :            :         const struct spdk_nvme_transport_id *trid;
    9238                 :            :         const char *adrfam_str;
    9239                 :            : 
    9240                 :        120 :         spdk_json_write_object_begin(w);
    9241                 :            : 
    9242   [ #  #  #  #  :        120 :         spdk_json_write_named_string(w, "bdev_name", nvme_ns->bdev->disk.name);
          #  #  #  #  #  
                      # ]
    9243                 :            : 
    9244   [ #  #  #  # ]:        120 :         cdata = spdk_nvme_ctrlr_get_data(nvme_ctrlr->ctrlr);
    9245   [ #  #  #  # ]:        120 :         trid = spdk_nvme_ctrlr_get_transport_id(nvme_ctrlr->ctrlr);
    9246                 :            : 
    9247   [ #  #  #  # ]:        120 :         spdk_json_write_named_uint32(w, "cntlid", cdata->cntlid);
    9248                 :        120 :         spdk_json_write_named_bool(w, "current", nvme_io_path_is_current(io_path));
    9249   [ #  #  #  # ]:        120 :         spdk_json_write_named_bool(w, "connected", nvme_qpair_is_connected(io_path->qpair));
    9250                 :        120 :         spdk_json_write_named_bool(w, "accessible", nvme_ns_is_accessible(nvme_ns));
    9251                 :            : 
    9252                 :        120 :         spdk_json_write_named_object_begin(w, "transport");
    9253         [ #  # ]:        120 :         spdk_json_write_named_string(w, "trtype", trid->trstring);
    9254         [ #  # ]:        120 :         spdk_json_write_named_string(w, "traddr", trid->traddr);
    9255   [ +  -  #  #  :        120 :         if (trid->trsvcid[0] != '\0') {
          #  #  #  #  #  
                      # ]
    9256         [ #  # ]:        120 :                 spdk_json_write_named_string(w, "trsvcid", trid->trsvcid);
    9257                 :          0 :         }
    9258   [ #  #  #  # ]:        120 :         adrfam_str = spdk_nvme_transport_id_adrfam_str(trid->adrfam);
    9259         [ +  - ]:        120 :         if (adrfam_str) {
    9260                 :        120 :                 spdk_json_write_named_string(w, "adrfam", adrfam_str);
    9261                 :          0 :         }
    9262                 :        120 :         spdk_json_write_object_end(w);
    9263                 :            : 
    9264                 :        120 :         spdk_json_write_object_end(w);
    9265                 :        120 : }
    9266                 :            : 
    9267                 :            : void
    9268                 :         68 : bdev_nvme_get_discovery_info(struct spdk_json_write_ctx *w)
    9269                 :            : {
    9270                 :            :         struct discovery_ctx *ctx;
    9271                 :            :         struct discovery_entry_ctx *entry_ctx;
    9272                 :            : 
    9273                 :         68 :         spdk_json_write_array_begin(w);
    9274   [ +  +  #  #  :        129 :         TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
             #  #  #  # ]
    9275                 :         61 :                 spdk_json_write_object_begin(w);
    9276   [ #  #  #  # ]:         61 :                 spdk_json_write_named_string(w, "name", ctx->name);
    9277                 :            : 
    9278                 :         61 :                 spdk_json_write_named_object_begin(w, "trid");
    9279         [ #  # ]:         61 :                 nvme_bdev_dump_trid_json(&ctx->trid, w);
    9280                 :         61 :                 spdk_json_write_object_end(w);
    9281                 :            : 
    9282                 :         61 :                 spdk_json_write_named_array_begin(w, "referrals");
    9283   [ +  +  #  #  :        147 :                 TAILQ_FOREACH(entry_ctx, &ctx->discovery_entry_ctxs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    9284                 :         86 :                         spdk_json_write_object_begin(w);
    9285                 :         86 :                         spdk_json_write_named_object_begin(w, "trid");
    9286         [ #  # ]:         86 :                         nvme_bdev_dump_trid_json(&entry_ctx->trid, w);
    9287                 :         86 :                         spdk_json_write_object_end(w);
    9288                 :         86 :                         spdk_json_write_object_end(w);
    9289                 :          0 :                 }
    9290                 :         61 :                 spdk_json_write_array_end(w);
    9291                 :            : 
    9292                 :         61 :                 spdk_json_write_object_end(w);
    9293                 :          0 :         }
    9294                 :         68 :         spdk_json_write_array_end(w);
    9295                 :         68 : }
    9296                 :            : 
    9297                 :       1665 : SPDK_LOG_REGISTER_COMPONENT(bdev_nvme)
    9298                 :            : 
    9299                 :            : static void
    9300                 :       1525 : bdev_nvme_trace(void)
    9301                 :            : {
    9302                 :       1525 :         struct spdk_trace_tpoint_opts opts[] = {
    9303                 :            :                 {
    9304                 :            :                         "BDEV_NVME_IO_START", TRACE_BDEV_NVME_IO_START,
    9305                 :            :                         OWNER_TYPE_NONE, OBJECT_BDEV_NVME_IO, 1,
    9306                 :            :                         {{ "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 }}
    9307                 :            :                 },
    9308                 :            :                 {
    9309                 :            :                         "BDEV_NVME_IO_DONE", TRACE_BDEV_NVME_IO_DONE,
    9310                 :            :                         OWNER_TYPE_NONE, OBJECT_BDEV_NVME_IO, 0,
    9311                 :            :                         {{ "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 }}
    9312                 :            :                 }
    9313                 :            :         };
    9314                 :            : 
    9315                 :            : 
    9316                 :       1525 :         spdk_trace_register_object(OBJECT_BDEV_NVME_IO, 'N');
    9317                 :       1525 :         spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts));
    9318                 :       1525 :         spdk_trace_tpoint_register_relation(TRACE_NVME_PCIE_SUBMIT, OBJECT_BDEV_NVME_IO, 0);
    9319                 :       1525 :         spdk_trace_tpoint_register_relation(TRACE_NVME_TCP_SUBMIT, OBJECT_BDEV_NVME_IO, 0);
    9320                 :       1525 :         spdk_trace_tpoint_register_relation(TRACE_NVME_PCIE_COMPLETE, OBJECT_BDEV_NVME_IO, 0);
    9321                 :       1525 :         spdk_trace_tpoint_register_relation(TRACE_NVME_TCP_COMPLETE, OBJECT_BDEV_NVME_IO, 0);
    9322                 :       1525 : }
    9323                 :       1665 : SPDK_TRACE_REGISTER_FN(bdev_nvme_trace, "bdev_nvme", TRACE_GROUP_BDEV_NVME)

Generated by: LCOV version 1.15