LCOV - code coverage report
Current view: top level - spdk/module/bdev/nvme - bdev_nvme.c (source / functions) Hit Total Coverage
Test: Combined Lines: 3774 5261 71.7 %
Date: 2024-11-20 00:59:18 Functions: 306 330 92.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 3689 20146 18.3 %

           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                 :            :         .keep_alive_timeout_ms = SPDK_BDEV_NVME_DEFAULT_KEEP_ALIVE_TIMEOUT_IN_MS,
     166                 :            :         .timeout_us = 0,
     167                 :            :         .timeout_admin_us = 0,
     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                 :            :         .io_queue_requests = 0,
     174                 :            :         .nvme_adminq_poll_period_us = 10000ULL,
     175                 :            :         .nvme_ioq_poll_period_us = 0,
     176                 :            :         .delay_cmd_submit = SPDK_BDEV_NVME_DEFAULT_DELAY_CMD_SUBMIT,
     177                 :            :         .bdev_retry_count = 3,
     178                 :            :         .ctrlr_loss_timeout_sec = 0,
     179                 :            :         .reconnect_delay_sec = 0,
     180                 :            :         .fast_io_fail_timeout_sec = 0,
     181                 :            :         .transport_ack_timeout = 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                 :       1903 : nvme_ns_cmp(struct nvme_ns *ns1, struct nvme_ns *ns2)
     261                 :            : {
     262   [ +  +  +  -  :       1903 :         return ns1->id < ns2->id ? -1 : ns1->id > ns2->id;
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
     263                 :            : }
     264                 :            : 
     265   [ +  +  +  +  :      22073 : RB_GENERATE_STATIC(nvme_ns_tree, nvme_ns, node, nvme_ns_cmp);
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  -  
          +  +  -  +  -  
          +  +  +  +  +  
          +  +  -  +  -  
          +  -  -  -  -  
          -  -  +  +  +  
          +  +  +  +  -  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  +  #  #  
          #  #  #  #  #  
          #  +  -  +  -  
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  +  
          -  +  -  +  -  
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  -  
          +  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  -  +  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  -  
          +  #  #  #  #  
          #  #  #  #  #  
          #  #  #  -  +  
          -  +  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          +  -  +  -  -  
          +  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          -  +  +  -  +  
          -  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          -  +  +  -  +  
          -  +  -  +  -  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     266                 :            : 
     267                 :            : struct spdk_nvme_qpair *
     268                 :          3 : bdev_nvme_get_io_qpair(struct spdk_io_channel *ctrlr_io_ch)
     269                 :            : {
     270                 :            :         struct nvme_ctrlr_channel *ctrlr_ch;
     271                 :            : 
     272   [ -  +  #  # ]:          3 :         assert(ctrlr_io_ch != NULL);
     273                 :            : 
     274                 :          3 :         ctrlr_ch = spdk_io_channel_get_ctx(ctrlr_io_ch);
     275                 :            : 
     276   [ #  #  #  #  :          3 :         return ctrlr_ch->qpair->qpair;
             #  #  #  # ]
     277                 :            : }
     278                 :            : 
     279                 :            : static int
     280                 :       1950 : bdev_nvme_get_ctx_size(void)
     281                 :            : {
     282                 :       1950 :         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                 :       2137 : 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                 :      47841 : nvme_bdev_ctrlr_get_by_name(const char *name)
     302                 :            : {
     303                 :            :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
     304                 :            : 
     305   [ +  +  #  #  :      49784 :         TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
             #  #  #  # ]
     306   [ +  +  +  +  :      42498 :                 if (strcmp(name, nbdev_ctrlr->name) == 0) {
          +  +  +  -  -  
                      + ]
     307                 :      40555 :                         break;
     308                 :            :                 }
     309                 :          0 :         }
     310                 :            : 
     311                 :      47841 :         return nbdev_ctrlr;
     312                 :            : }
     313                 :            : 
     314                 :            : static struct nvme_ctrlr *
     315                 :        679 : 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   [ +  +  #  #  :       1304 :         TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     322   [ #  #  #  # ]:        727 :                 opts = spdk_nvme_ctrlr_get_opts(nvme_ctrlr->ctrlr);
     323   [ +  +  #  #  :        727 :                 if (spdk_nvme_transport_id_compare(trid, &nvme_ctrlr->active_path_id->trid) == 0 &&
          #  #  #  #  #  
                      # ]
     324   [ +  +  -  +  :        105 :                     strcmp(hostnqn, opts->hostnqn) == 0) {
                   +  + ]
     325                 :        102 :                         break;
     326                 :            :                 }
     327                 :          0 :         }
     328                 :            : 
     329                 :        679 :         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                 :       1596 : nvme_bdev_ctrlr_get_bdev(struct nvme_bdev_ctrlr *nbdev_ctrlr, uint32_t nsid)
     351                 :            : {
     352                 :            :         struct nvme_bdev *bdev;
     353                 :            : 
     354         [ +  + ]:       1596 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     355   [ +  +  +  -  :       1866 :         TAILQ_FOREACH(bdev, &nbdev_ctrlr->bdevs, tailq) {
          +  -  +  -  #  
             #  #  #  #  
                      # ]
     356   [ +  +  #  #  :        393 :                 if (bdev->nsid == nsid) {
                   #  # ]
     357                 :        123 :                         break;
     358                 :            :                 }
     359                 :          0 :         }
     360         [ +  + ]:       1596 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     361                 :            : 
     362                 :       1596 :         return bdev;
     363                 :            : }
     364                 :            : 
     365                 :            : struct nvme_ns *
     366                 :       3274 : nvme_ctrlr_get_ns(struct nvme_ctrlr *nvme_ctrlr, uint32_t nsid)
     367                 :            : {
     368                 :       1186 :         struct nvme_ns ns;
     369                 :            : 
     370   [ +  +  #  # ]:       3274 :         assert(nsid > 0);
     371                 :            : 
     372                 :       3274 :         ns.id = nsid;
     373         [ +  - ]:       3274 :         return RB_FIND(nvme_ns_tree, &nvme_ctrlr->namespaces, &ns);
     374                 :            : }
     375                 :            : 
     376                 :            : struct nvme_ns *
     377                 :       3611 : nvme_ctrlr_get_first_active_ns(struct nvme_ctrlr *nvme_ctrlr)
     378                 :            : {
     379         [ +  - ]:       3611 :         return RB_MIN(nvme_ns_tree, &nvme_ctrlr->namespaces);
     380                 :            : }
     381                 :            : 
     382                 :            : struct nvme_ns *
     383                 :       1599 : nvme_ctrlr_get_next_active_ns(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *ns)
     384                 :            : {
     385         [ -  + ]:       1599 :         if (ns == NULL) {
     386                 :          0 :                 return NULL;
     387                 :            :         }
     388                 :            : 
     389                 :       1599 :         return RB_NEXT(nvme_ns_tree, &nvme_ctrlr->namespaces, ns);
     390                 :          1 : }
     391                 :            : 
     392                 :            : static struct nvme_ctrlr *
     393                 :       1727 : nvme_ctrlr_get(const struct spdk_nvme_transport_id *trid, const char *hostnqn)
     394                 :            : {
     395                 :            :         struct nvme_bdev_ctrlr  *nbdev_ctrlr;
     396                 :       1727 :         struct nvme_ctrlr       *nvme_ctrlr = NULL;
     397                 :            : 
     398         [ +  + ]:       1727 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     399   [ +  +  #  #  :       2286 :         TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
             #  #  #  # ]
     400                 :        559 :                 nvme_ctrlr = nvme_bdev_ctrlr_get_ctrlr(nbdev_ctrlr, trid, hostnqn);
     401         [ -  + ]:        559 :                 if (nvme_ctrlr != NULL) {
     402                 :          0 :                         break;
     403                 :            :                 }
     404                 :          0 :         }
     405         [ +  + ]:       1727 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     406                 :            : 
     407                 :       1727 :         return nvme_ctrlr;
     408                 :            : }
     409                 :            : 
     410                 :            : struct nvme_ctrlr *
     411                 :       3523 : nvme_ctrlr_get_by_name(const char *name)
     412                 :            : {
     413                 :            :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
     414                 :       3523 :         struct nvme_ctrlr *nvme_ctrlr = NULL;
     415                 :            : 
     416         [ +  + ]:       3523 :         if (name == NULL) {
     417                 :          0 :                 return NULL;
     418                 :            :         }
     419                 :            : 
     420         [ +  + ]:       3523 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     421                 :       3523 :         nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
     422         [ +  + ]:       3523 :         if (nbdev_ctrlr != NULL) {
     423   [ +  -  +  -  :        285 :                 nvme_ctrlr = TAILQ_FIRST(&nbdev_ctrlr->ctrlrs);
                   +  - ]
     424                 :          1 :         }
     425         [ +  + ]:       3523 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     426                 :            : 
     427                 :       3523 :         return nvme_ctrlr;
     428                 :          3 : }
     429                 :            : 
     430                 :            : void
     431                 :        764 : nvme_bdev_ctrlr_for_each(nvme_bdev_ctrlr_for_each_fn fn, void *ctx)
     432                 :            : {
     433                 :            :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
     434                 :            : 
     435         [ -  + ]:        764 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     436   [ +  +  #  #  :       1483 :         TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
             #  #  #  # ]
     437   [ #  #  #  # ]:        719 :                 fn(nbdev_ctrlr, ctx);
     438                 :          0 :         }
     439         [ -  + ]:        764 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     440                 :        764 : }
     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                 :       2400 : nvme_ctrlr_for_each_channel_continue(struct nvme_ctrlr_channel_iter *iter, int status)
     451                 :            : {
     452   [ #  #  #  # ]:       2400 :         spdk_for_each_channel_continue(iter->i, status);
     453                 :       2400 : }
     454                 :            : 
     455                 :            : static void
     456                 :       2400 : nvme_ctrlr_each_channel_msg(struct spdk_io_channel_iter *i)
     457                 :            : {
     458                 :       2400 :         struct nvme_ctrlr_channel_iter *iter = spdk_io_channel_iter_get_ctx(i);
     459                 :       2400 :         struct nvme_ctrlr *nvme_ctrlr = spdk_io_channel_iter_get_io_device(i);
     460                 :       2400 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
     461                 :       2400 :         struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch);
     462                 :            : 
     463   [ #  #  #  # ]:       2400 :         iter->i = i;
     464   [ #  #  #  #  :       2400 :         iter->fn(iter, nvme_ctrlr, ctrlr_ch, iter->ctx);
          #  #  #  #  #  
                #  #  # ]
     465                 :       2400 : }
     466                 :            : 
     467                 :            : static void
     468                 :       2315 : nvme_ctrlr_each_channel_cpl(struct spdk_io_channel_iter *i, int status)
     469                 :            : {
     470                 :       2315 :         struct nvme_ctrlr_channel_iter *iter = spdk_io_channel_iter_get_ctx(i);
     471                 :       2315 :         struct nvme_ctrlr *nvme_ctrlr = spdk_io_channel_iter_get_io_device(i);
     472                 :            : 
     473   [ #  #  #  # ]:       2315 :         iter->i = i;
     474   [ #  #  #  #  :       2315 :         iter->cpl(nvme_ctrlr, iter->ctx, status);
          #  #  #  #  #  
                #  #  # ]
     475                 :            : 
     476                 :       2315 :         free(iter);
     477                 :       2315 : }
     478                 :            : 
     479                 :            : void
     480                 :       2315 : 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   [ +  -  +  - ]:       2315 :         assert(nvme_ctrlr != NULL && fn != NULL);
     487                 :            : 
     488                 :       2315 :         iter = calloc(1, sizeof(struct nvme_ctrlr_channel_iter));
     489         [ -  + ]:       2315 :         if (iter == NULL) {
     490                 :          0 :                 SPDK_ERRLOG("Unable to allocate iterator\n");
     491         [ #  # ]:          0 :                 assert(false);
     492                 :            :                 return;
     493                 :            :         }
     494                 :            : 
     495   [ #  #  #  # ]:       2315 :         iter->fn = fn;
     496   [ #  #  #  # ]:       2315 :         iter->cpl = cpl;
     497   [ #  #  #  # ]:       2315 :         iter->ctx = ctx;
     498                 :            : 
     499                 :       2315 :         spdk_for_each_channel(nvme_ctrlr, nvme_ctrlr_each_channel_msg,
     500                 :          0 :                               iter, nvme_ctrlr_each_channel_cpl);
     501                 :          0 : }
     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                 :        301 : nvme_bdev_for_each_channel_continue(struct nvme_bdev_channel_iter *iter, int status)
     512                 :            : {
     513   [ #  #  #  # ]:        301 :         spdk_for_each_channel_continue(iter->i, status);
     514                 :        301 : }
     515                 :            : 
     516                 :            : static void
     517                 :        301 : nvme_bdev_each_channel_msg(struct spdk_io_channel_iter *i)
     518                 :            : {
     519                 :        301 :         struct nvme_bdev_channel_iter *iter = spdk_io_channel_iter_get_ctx(i);
     520                 :        301 :         struct nvme_bdev *nbdev = spdk_io_channel_iter_get_io_device(i);
     521                 :        301 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
     522                 :        301 :         struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(ch);
     523                 :            : 
     524   [ #  #  #  # ]:        301 :         iter->i = i;
     525   [ #  #  #  #  :        301 :         iter->fn(iter, nbdev, nbdev_ch, iter->ctx);
          #  #  #  #  #  
                #  #  # ]
     526                 :        301 : }
     527                 :            : 
     528                 :            : static void
     529                 :        296 : nvme_bdev_each_channel_cpl(struct spdk_io_channel_iter *i, int status)
     530                 :            : {
     531                 :        296 :         struct nvme_bdev_channel_iter *iter = spdk_io_channel_iter_get_ctx(i);
     532                 :        296 :         struct nvme_bdev *nbdev = spdk_io_channel_iter_get_io_device(i);
     533                 :            : 
     534   [ #  #  #  # ]:        296 :         iter->i = i;
     535   [ #  #  #  #  :        296 :         iter->cpl(nbdev, iter->ctx, status);
          #  #  #  #  #  
                #  #  # ]
     536                 :            : 
     537                 :        296 :         free(iter);
     538                 :        296 : }
     539                 :            : 
     540                 :            : void
     541                 :        296 : 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   [ +  -  +  - ]:        296 :         assert(nbdev != NULL && fn != NULL);
     548                 :            : 
     549                 :        296 :         iter = calloc(1, sizeof(struct nvme_bdev_channel_iter));
     550         [ -  + ]:        296 :         if (iter == NULL) {
     551                 :          0 :                 SPDK_ERRLOG("Unable to allocate iterator\n");
     552         [ #  # ]:          0 :                 assert(false);
     553                 :            :                 return;
     554                 :            :         }
     555                 :            : 
     556   [ #  #  #  # ]:        296 :         iter->fn = fn;
     557   [ #  #  #  # ]:        296 :         iter->cpl = cpl;
     558   [ #  #  #  # ]:        296 :         iter->ctx = ctx;
     559                 :            : 
     560                 :        296 :         spdk_for_each_channel(nbdev, nvme_bdev_each_channel_msg, iter,
     561                 :            :                               nvme_bdev_each_channel_cpl);
     562                 :          0 : }
     563                 :            : 
     564                 :            : void
     565                 :       2185 : 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   [ #  #  #  # ]:       2185 :         trtype_str = spdk_nvme_transport_id_trtype_str(trid->trtype);
     571         [ +  - ]:       2185 :         if (trtype_str) {
     572                 :       2185 :                 spdk_json_write_named_string(w, "trtype", trtype_str);
     573                 :          0 :         }
     574                 :            : 
     575   [ #  #  #  # ]:       2185 :         adrfam_str = spdk_nvme_transport_id_adrfam_str(trid->adrfam);
     576         [ +  + ]:       2185 :         if (adrfam_str) {
     577                 :       1152 :                 spdk_json_write_named_string(w, "adrfam", adrfam_str);
     578                 :          0 :         }
     579                 :            : 
     580   [ +  -  #  #  :       2185 :         if (trid->traddr[0] != '\0') {
          #  #  #  #  #  
                      # ]
     581         [ #  # ]:       2185 :                 spdk_json_write_named_string(w, "traddr", trid->traddr);
     582                 :          0 :         }
     583                 :            : 
     584   [ +  +  #  #  :       2185 :         if (trid->trsvcid[0] != '\0') {
          #  #  #  #  #  
                      # ]
     585         [ #  # ]:       1152 :                 spdk_json_write_named_string(w, "trsvcid", trid->trsvcid);
     586                 :          0 :         }
     587                 :            : 
     588   [ +  +  #  #  :       2185 :         if (trid->subnqn[0] != '\0') {
          #  #  #  #  #  
                      # ]
     589         [ #  # ]:       1152 :                 spdk_json_write_named_string(w, "subnqn", trid->subnqn);
     590                 :          0 :         }
     591                 :       2185 : }
     592                 :            : 
     593                 :            : static void
     594                 :       1727 : nvme_bdev_ctrlr_delete(struct nvme_bdev_ctrlr *nbdev_ctrlr,
     595                 :            :                        struct nvme_ctrlr *nvme_ctrlr)
     596                 :            : {
     597                 :        516 :         SPDK_DTRACE_PROBE1(bdev_nvme_ctrlr_delete, nvme_ctrlr->nbdev_ctrlr->name);
     598         [ +  + ]:       1727 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     599                 :            : 
     600   [ +  +  +  -  :       1727 :         TAILQ_REMOVE(&nbdev_ctrlr->ctrlrs, nvme_ctrlr, tailq);
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
     601   [ +  +  +  -  :       1727 :         if (!TAILQ_EMPTY(&nbdev_ctrlr->ctrlrs)) {
             +  -  -  + ]
     602         [ -  + ]:         58 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
     603                 :            : 
     604                 :         58 :                 return;
     605                 :            :         }
     606   [ +  +  +  -  :       1669 :         TAILQ_REMOVE(&g_nvme_bdev_ctrlrs, nbdev_ctrlr, tailq);
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     607                 :            : 
     608         [ +  + ]:       1669 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     609                 :            : 
     610   [ +  +  +  -  :       1669 :         assert(TAILQ_EMPTY(&nbdev_ctrlr->bdevs));
          +  -  +  -  #  
                      # ]
     611                 :            : 
     612   [ +  -  +  - ]:       1669 :         free(nbdev_ctrlr->name);
     613                 :       1669 :         free(nbdev_ctrlr);
     614                 :          1 : }
     615                 :            : 
     616                 :            : static void
     617                 :       1730 : _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   [ +  -  +  - ]:       1730 :         free(nvme_ctrlr->copied_ana_desc);
     623   [ +  -  +  - ]:       1730 :         spdk_free(nvme_ctrlr->ana_log_page);
     624                 :            : 
     625   [ +  +  +  -  :       1730 :         if (nvme_ctrlr->opal_dev) {
                   +  - ]
     626   [ #  #  #  # ]:         32 :                 spdk_opal_dev_destruct(nvme_ctrlr->opal_dev);
     627   [ #  #  #  # ]:         32 :                 nvme_ctrlr->opal_dev = NULL;
     628                 :          0 :         }
     629                 :            : 
     630   [ +  +  +  -  :       1730 :         if (nvme_ctrlr->nbdev_ctrlr) {
                   -  + ]
     631   [ +  -  +  - ]:       1727 :                 nvme_bdev_ctrlr_delete(nvme_ctrlr->nbdev_ctrlr, nvme_ctrlr);
     632                 :          1 :         }
     633                 :            : 
     634   [ +  +  +  -  :       1730 :         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   [ +  +  +  -  :       3468 :         TAILQ_FOREACH_SAFE(path_id, &nvme_ctrlr->trids, link, tmp_path) {
          +  -  +  +  +  
          -  +  -  +  -  
                   +  + ]
     640   [ +  +  +  -  :       1738 :                 TAILQ_REMOVE(&nvme_ctrlr->trids, path_id, link);
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
     641                 :       1738 :                 free(path_id);
     642                 :          1 :         }
     643                 :            : 
     644   [ +  +  +  - ]:       1730 :         pthread_mutex_destroy(&nvme_ctrlr->mutex);
     645   [ +  -  +  - ]:       1730 :         spdk_keyring_put_key(nvme_ctrlr->psk);
     646   [ +  -  +  - ]:       1730 :         spdk_keyring_put_key(nvme_ctrlr->dhchap_key);
     647   [ +  -  +  - ]:       1730 :         spdk_keyring_put_key(nvme_ctrlr->dhchap_ctrlr_key);
     648                 :       1730 :         free(nvme_ctrlr);
     649                 :            : 
     650         [ +  + ]:       1730 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
     651   [ +  +  +  +  :       1730 :         if (g_bdev_nvme_module_finish && TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) {
                   +  + ]
     652         [ +  + ]:        547 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
     653                 :        547 :                 spdk_io_device_unregister(&g_nvme_bdev_ctrlrs, NULL);
     654                 :        547 :                 spdk_bdev_module_fini_done();
     655                 :        547 :                 return;
     656                 :            :         }
     657         [ -  + ]:       1183 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
     658                 :          1 : }
     659                 :            : 
     660                 :            : static int
     661                 :     218373 : nvme_detach_poller(void *arg)
     662                 :            : {
     663                 :     218373 :         struct nvme_ctrlr *nvme_ctrlr = arg;
     664                 :            :         int rc;
     665                 :            : 
     666   [ +  -  +  - ]:     218373 :         rc = spdk_nvme_detach_poll_async(nvme_ctrlr->detach_ctx);
     667         [ +  + ]:     218373 :         if (rc != -EAGAIN) {
     668         [ +  - ]:       1730 :                 spdk_poller_unregister(&nvme_ctrlr->reset_detach_poller);
     669                 :       1730 :                 _nvme_ctrlr_delete(nvme_ctrlr);
     670                 :          1 :         }
     671                 :            : 
     672                 :     218373 :         return SPDK_POLLER_BUSY;
     673                 :            : }
     674                 :            : 
     675                 :            : static void
     676                 :       1730 : nvme_ctrlr_delete(struct nvme_ctrlr *nvme_ctrlr)
     677                 :            : {
     678                 :            :         int rc;
     679                 :            : 
     680         [ +  - ]:       1730 :         spdk_poller_unregister(&nvme_ctrlr->reconnect_delay_timer);
     681                 :            : 
     682         [ +  + ]:       1730 :         if (spdk_interrupt_mode_is_enabled()) {
     683         [ #  # ]:          1 :                 spdk_interrupt_unregister(&nvme_ctrlr->intr);
     684                 :          0 :         }
     685                 :            : 
     686                 :            :         /* First, unregister the adminq poller, as the driver will poll adminq if necessary */
     687         [ +  - ]:       1730 :         spdk_poller_unregister(&nvme_ctrlr->adminq_timer_poller);
     688                 :            : 
     689                 :            :         /* If we got here, the reset/detach poller cannot be active */
     690   [ +  +  +  -  :       1730 :         assert(nvme_ctrlr->reset_detach_poller == NULL);
             +  -  #  # ]
     691   [ +  -  +  - ]:       1730 :         nvme_ctrlr->reset_detach_poller = SPDK_POLLER_REGISTER(nvme_detach_poller,
     692                 :            :                                           nvme_ctrlr, 1000);
     693   [ +  +  +  -  :       1730 :         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   [ +  -  +  -  :       1730 :         rc = spdk_nvme_detach_async(nvme_ctrlr->ctrlr, &nvme_ctrlr->detach_ctx);
                   +  - ]
     699         [ -  + ]:       1730 :         if (rc != 0) {
     700   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to detach the NVMe controller\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     701                 :          0 :                 goto error;
     702                 :            :         }
     703                 :            : 
     704                 :       1730 :         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                 :          1 : }
     712                 :            : 
     713                 :            : static void
     714                 :       1727 : nvme_ctrlr_unregister_cb(void *io_device)
     715                 :            : {
     716                 :       1727 :         struct nvme_ctrlr *nvme_ctrlr = io_device;
     717                 :            : 
     718                 :       1727 :         nvme_ctrlr_delete(nvme_ctrlr);
     719                 :       1727 : }
     720                 :            : 
     721                 :            : static void
     722                 :       1727 : nvme_ctrlr_unregister(void *ctx)
     723                 :            : {
     724                 :       1727 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
     725                 :            : 
     726                 :       1727 :         spdk_io_device_unregister(nvme_ctrlr, nvme_ctrlr_unregister_cb);
     727                 :       1727 : }
     728                 :            : 
     729                 :            : static bool
     730                 :       8064 : nvme_ctrlr_can_be_unregistered(struct nvme_ctrlr *nvme_ctrlr)
     731                 :            : {
     732   [ +  +  +  + ]:       8064 :         if (!nvme_ctrlr->destruct) {
     733                 :       4742 :                 return false;
     734                 :            :         }
     735                 :            : 
     736   [ +  +  +  -  :       3322 :         if (nvme_ctrlr->ref > 0) {
                   +  + ]
     737                 :       1580 :                 return false;
     738                 :            :         }
     739                 :            : 
     740   [ +  +  -  + ]:       1742 :         if (nvme_ctrlr->resetting) {
     741                 :         15 :                 return false;
     742                 :            :         }
     743                 :            : 
     744   [ +  +  -  + ]:       1727 :         if (nvme_ctrlr->ana_log_page_updating) {
     745                 :          0 :                 return false;
     746                 :            :         }
     747                 :            : 
     748   [ -  +  -  + ]:       1727 :         if (nvme_ctrlr->io_path_cache_clearing) {
     749                 :          0 :                 return false;
     750                 :            :         }
     751                 :            : 
     752                 :       1727 :         return true;
     753                 :          3 : }
     754                 :            : 
     755                 :            : static void
     756                 :       5739 : nvme_ctrlr_put_ref(struct nvme_ctrlr *nvme_ctrlr)
     757                 :            : {
     758   [ +  +  +  - ]:       5739 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
     759                 :       1553 :         SPDK_DTRACE_PROBE2(bdev_nvme_ctrlr_release, nvme_ctrlr->nbdev_ctrlr->name, nvme_ctrlr->ref);
     760                 :            : 
     761   [ +  +  +  -  :       5739 :         assert(nvme_ctrlr->ref > 0);
             +  -  #  # ]
     762   [ +  -  +  - ]:       5739 :         nvme_ctrlr->ref--;
     763                 :            : 
     764         [ +  + ]:       5739 :         if (!nvme_ctrlr_can_be_unregistered(nvme_ctrlr)) {
     765   [ +  +  +  - ]:       4027 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
     766                 :       4027 :                 return;
     767                 :            :         }
     768                 :            : 
     769   [ +  +  +  - ]:       1712 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
     770                 :            : 
     771   [ +  -  +  - ]:       1712 :         spdk_thread_exec_msg(nvme_ctrlr->thread, nvme_ctrlr_unregister, nvme_ctrlr);
     772                 :          3 : }
     773                 :            : 
     774                 :            : static void
     775                 :       3984 : nvme_ctrlr_get_ref(struct nvme_ctrlr *nvme_ctrlr)
     776                 :            : {
     777   [ +  +  +  - ]:       3984 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
     778   [ +  -  +  - ]:       3984 :         nvme_ctrlr->ref++;
     779   [ +  +  +  - ]:       3984 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
     780                 :       3984 : }
     781                 :            : 
     782                 :            : static void
     783                 :     422199 : bdev_nvme_clear_current_io_path(struct nvme_bdev_channel *nbdev_ch)
     784                 :            : {
     785   [ +  -  +  - ]:     422199 :         nbdev_ch->current_io_path = NULL;
     786   [ +  -  +  - ]:     422199 :         nbdev_ch->rr_counter = 0;
     787                 :     422199 : }
     788                 :            : 
     789                 :            : static struct nvme_io_path *
     790                 :         24 : _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   [ +  +  #  #  :         48 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     795   [ +  +  #  #  :         45 :                 if (io_path->nvme_ns == nvme_ns) {
                   #  # ]
     796                 :         21 :                         break;
     797                 :            :                 }
     798                 :          0 :         }
     799                 :            : 
     800                 :         24 :         return io_path;
     801                 :            : }
     802                 :            : 
     803                 :            : static struct nvme_io_path *
     804                 :       2525 : nvme_io_path_alloc(void)
     805                 :            : {
     806                 :            :         struct nvme_io_path *io_path;
     807                 :            : 
     808                 :       2525 :         io_path = calloc(1, sizeof(*io_path));
     809         [ +  + ]:       2525 :         if (io_path == NULL) {
     810                 :          0 :                 SPDK_ERRLOG("Failed to alloc io_path.\n");
     811                 :          0 :                 return NULL;
     812                 :            :         }
     813                 :            : 
     814   [ +  +  +  + ]:       2525 :         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                 :       2525 :         return io_path;
     825                 :          1 : }
     826                 :            : 
     827                 :            : static void
     828                 :       2525 : nvme_io_path_free(struct nvme_io_path *io_path)
     829                 :            : {
     830   [ +  -  +  - ]:       2525 :         free(io_path->stat);
     831                 :       2525 :         free(io_path);
     832                 :       2525 : }
     833                 :            : 
     834                 :            : static int
     835                 :       2525 : _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                 :       2525 :         io_path = nvme_io_path_alloc();
     843         [ +  + ]:       2525 :         if (io_path == NULL) {
     844                 :          0 :                 return -ENOMEM;
     845                 :            :         }
     846                 :            : 
     847   [ +  -  +  - ]:       2525 :         io_path->nvme_ns = nvme_ns;
     848                 :            : 
     849   [ +  -  +  - ]:       2525 :         ch = spdk_get_io_channel(nvme_ns->ctrlr);
     850         [ +  + ]:       2525 :         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                 :       2525 :         ctrlr_ch = spdk_io_channel_get_ctx(ch);
     857                 :            : 
     858   [ +  -  +  - ]:       2525 :         nvme_qpair = ctrlr_ch->qpair;
     859   [ +  +  #  # ]:       2525 :         assert(nvme_qpair != NULL);
     860                 :            : 
     861   [ +  -  +  - ]:       2525 :         io_path->qpair = nvme_qpair;
     862   [ +  -  +  -  :       2525 :         TAILQ_INSERT_TAIL(&nvme_qpair->io_path_list, io_path, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     863                 :            : 
     864   [ +  -  +  - ]:       2525 :         io_path->nbdev_ch = nbdev_ch;
     865   [ +  -  +  -  :       2525 :         STAILQ_INSERT_TAIL(&nbdev_ch->io_path_list, io_path, stailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     866                 :            : 
     867                 :       2525 :         bdev_nvme_clear_current_io_path(nbdev_ch);
     868                 :            : 
     869                 :       2525 :         return 0;
     870                 :          1 : }
     871                 :            : 
     872                 :            : static void
     873                 :       2525 : 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   [ +  +  +  -  :       2528 :         TAILQ_FOREACH(bio, &nbdev_ch->retry_io_list, retry_link) {
          +  -  +  -  #  
             #  #  #  #  
                      # ]
     879   [ +  -  #  #  :          3 :                 if (bio->io_path == io_path) {
                   #  # ]
     880   [ #  #  #  # ]:          3 :                         bio->io_path = NULL;
     881                 :          0 :                 }
     882                 :          0 :         }
     883                 :       2525 : }
     884                 :            : 
     885                 :            : static void
     886                 :       2525 : _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                 :       2525 :         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   [ +  +  +  - ]:       2525 :         pthread_mutex_lock(&nbdev->mutex);
     897   [ +  +  +  +  :       2525 :         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   [ +  +  +  - ]:       2525 :         pthread_mutex_unlock(&nbdev->mutex);
     901                 :            : 
     902                 :       2525 :         bdev_nvme_clear_current_io_path(nbdev_ch);
     903                 :       2525 :         bdev_nvme_clear_retry_io_path(nbdev_ch, io_path);
     904                 :            : 
     905   [ +  +  +  +  :       2525 :         STAILQ_REMOVE(&nbdev_ch->io_path_list, io_path, nvme_io_path, stailq);
          +  +  +  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     906   [ +  -  +  - ]:       2525 :         io_path->nbdev_ch = NULL;
     907                 :            : 
     908   [ +  -  +  - ]:       2525 :         nvme_qpair = io_path->qpair;
     909   [ +  +  #  # ]:       2525 :         assert(nvme_qpair != NULL);
     910                 :            : 
     911   [ +  -  +  - ]:       2525 :         ctrlr_ch = nvme_qpair->ctrlr_ch;
     912   [ +  +  #  # ]:       2525 :         assert(ctrlr_ch != NULL);
     913                 :            : 
     914                 :       2525 :         ch = spdk_io_channel_from_ctx(ctrlr_ch);
     915                 :       2525 :         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                 :       2525 : }
     923                 :            : 
     924                 :            : static void
     925                 :       2480 : _bdev_nvme_delete_io_paths(struct nvme_bdev_channel *nbdev_ch)
     926                 :            : {
     927                 :            :         struct nvme_io_path *io_path, *tmp_io_path;
     928                 :            : 
     929   [ +  +  +  -  :       4999 :         STAILQ_FOREACH_SAFE(io_path, &nbdev_ch->io_path_list, stailq, tmp_io_path) {
          +  -  +  +  +  
          -  +  -  +  -  
                   +  + ]
     930                 :       2519 :                 _bdev_nvme_delete_io_path(nbdev_ch, io_path);
     931                 :          1 :         }
     932                 :       2480 : }
     933                 :            : 
     934                 :            : static int
     935                 :       2480 : bdev_nvme_create_bdev_channel_cb(void *io_device, void *ctx_buf)
     936                 :            : {
     937                 :       2480 :         struct nvme_bdev_channel *nbdev_ch = ctx_buf;
     938                 :       2480 :         struct nvme_bdev *nbdev = io_device;
     939                 :            :         struct nvme_ns *nvme_ns;
     940                 :            :         int rc;
     941                 :            : 
     942   [ +  -  +  -  :       2480 :         STAILQ_INIT(&nbdev_ch->io_path_list);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     943   [ +  -  +  -  :       2480 :         TAILQ_INIT(&nbdev_ch->retry_io_list);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     944                 :            : 
     945   [ +  +  +  - ]:       2480 :         pthread_mutex_lock(&nbdev->mutex);
     946                 :            : 
     947   [ +  -  +  -  :       2480 :         nbdev_ch->mp_policy = nbdev->mp_policy;
             +  -  +  - ]
     948   [ +  -  +  -  :       2480 :         nbdev_ch->mp_selector = nbdev->mp_selector;
             +  -  +  - ]
     949   [ +  -  +  -  :       2480 :         nbdev_ch->rr_min_io = nbdev->rr_min_io;
             +  -  +  - ]
     950                 :            : 
     951   [ +  +  +  -  :       4999 :         TAILQ_FOREACH(nvme_ns, &nbdev->nvme_ns_list, tailq) {
          +  -  +  +  +  
             -  +  -  +  
                      - ]
     952                 :       2519 :                 rc = _bdev_nvme_add_io_path(nbdev_ch, nvme_ns);
     953         [ +  + ]:       2519 :                 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                 :          1 :         }
     960   [ +  +  +  - ]:       2480 :         pthread_mutex_unlock(&nbdev->mutex);
     961                 :            : 
     962                 :       2480 :         return 0;
     963                 :          1 : }
     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                 :   45287431 : __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   [ +  +  +  +  :   45287431 :         spdk_trace_record(TRACE_BDEV_NVME_IO_DONE, 0, 0, (uintptr_t)bdev_io->driver_ctx,
          +  -  +  -  +  
          -  +  -  +  -  
             -  +  #  # ]
     973                 :            :                           (uintptr_t)bdev_io);
     974         [ +  + ]:   45287431 :         if (cpl) {
     975   [ +  -  +  -  :   42683723 :                 spdk_bdev_io_complete_nvme_status(bdev_io, cpl->cdw0, cpl->status.sct, cpl->status.sc);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     976                 :          3 :         } else {
     977                 :    2603708 :                 spdk_bdev_io_complete(bdev_io, status);
     978                 :            :         }
     979                 :   45287431 : }
     980                 :            : 
     981                 :            : static void bdev_nvme_abort_retry_ios(struct nvme_bdev_channel *nbdev_ch);
     982                 :            : 
     983                 :            : static void
     984                 :       2480 : bdev_nvme_destroy_bdev_channel_cb(void *io_device, void *ctx_buf)
     985                 :            : {
     986                 :       2480 :         struct nvme_bdev_channel *nbdev_ch = ctx_buf;
     987                 :            : 
     988                 :       2480 :         bdev_nvme_abort_retry_ios(nbdev_ch);
     989                 :       2480 :         _bdev_nvme_delete_io_paths(nbdev_ch);
     990                 :       2480 : }
     991                 :            : 
     992                 :            : static inline bool
     993                 :   45707314 : bdev_nvme_io_type_is_admin(enum spdk_bdev_io_type io_type)
     994                 :            : {
     995         [ +  + ]:   45707314 :         switch (io_type) {
     996                 :        138 :         case SPDK_BDEV_IO_TYPE_RESET:
     997                 :            :         case SPDK_BDEV_IO_TYPE_NVME_ADMIN:
     998                 :            :         case SPDK_BDEV_IO_TYPE_ABORT:
     999                 :        138 :                 return true;
    1000                 :   45707173 :         default:
    1001                 :   45707176 :                 break;
    1002                 :            :         }
    1003                 :            : 
    1004                 :   45707176 :         return false;
    1005                 :          3 : }
    1006                 :            : 
    1007                 :            : static inline bool
    1008                 :   10106568 : nvme_ns_is_active(struct nvme_ns *nvme_ns)
    1009                 :            : {
    1010   [ +  +  +  +  :   10106568 :         if (spdk_unlikely(nvme_ns->ana_state_updating)) {
             +  -  -  + ]
    1011                 :       3444 :                 return false;
    1012                 :            :         }
    1013                 :            : 
    1014   [ +  +  +  -  :   10103124 :         if (spdk_unlikely(nvme_ns->ns == NULL)) {
                   -  + ]
    1015                 :          0 :                 return false;
    1016                 :            :         }
    1017                 :            : 
    1018                 :   10103124 :         return true;
    1019                 :          1 : }
    1020                 :            : 
    1021                 :            : static inline bool
    1022                 :   10106532 : nvme_ns_is_accessible(struct nvme_ns *nvme_ns)
    1023                 :            : {
    1024         [ +  + ]:   10106532 :         if (spdk_unlikely(!nvme_ns_is_active(nvme_ns))) {
    1025                 :       3444 :                 return false;
    1026                 :            :         }
    1027                 :            : 
    1028   [ +  +  +  -  :   10103088 :         switch (nvme_ns->ana_state) {
                   +  - ]
    1029                 :    9303619 :         case SPDK_NVME_ANA_OPTIMIZED_STATE:
    1030                 :            :         case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
    1031                 :    9303620 :                 return true;
    1032                 :     799468 :         default:
    1033                 :     799468 :                 break;
    1034                 :            :         }
    1035                 :            : 
    1036                 :     799468 :         return false;
    1037                 :          1 : }
    1038                 :            : 
    1039                 :            : static inline bool
    1040                 :   11317608 : nvme_qpair_is_connected(struct nvme_qpair *nvme_qpair)
    1041                 :            : {
    1042   [ +  +  +  -  :   11317608 :         if (spdk_unlikely(nvme_qpair->qpair == NULL)) {
                   -  + ]
    1043                 :    1189606 :                 return false;
    1044                 :            :         }
    1045                 :            : 
    1046   [ +  +  +  -  :   10128002 :         if (spdk_unlikely(spdk_nvme_qpair_get_failure_reason(nvme_qpair->qpair) !=
                   -  + ]
    1047                 :            :                           SPDK_NVME_QPAIR_FAILURE_NONE)) {
    1048                 :       3339 :                 return false;
    1049                 :            :         }
    1050                 :            : 
    1051   [ +  +  +  -  :   10124663 :         if (spdk_unlikely(nvme_qpair->ctrlr_ch->reset_iter != NULL)) {
          +  -  +  -  -  
                      + ]
    1052                 :      10264 :                 return false;
    1053                 :            :         }
    1054                 :            : 
    1055                 :   10114399 :         return true;
    1056                 :          1 : }
    1057                 :            : 
    1058                 :            : static inline bool
    1059                 :   10902571 : nvme_io_path_is_available(struct nvme_io_path *io_path)
    1060                 :            : {
    1061   [ +  +  +  -  :   10902571 :         if (spdk_unlikely(!nvme_qpair_is_connected(io_path->qpair))) {
                   -  + ]
    1062                 :     796519 :                 return false;
    1063                 :            :         }
    1064                 :            : 
    1065   [ +  +  +  -  :   10106052 :         if (spdk_unlikely(!nvme_ns_is_accessible(io_path->nvme_ns))) {
                   -  + ]
    1066                 :     802792 :                 return false;
    1067                 :            :         }
    1068                 :            : 
    1069                 :    9303260 :         return true;
    1070                 :          1 : }
    1071                 :            : 
    1072                 :            : static inline bool
    1073                 :     406690 : nvme_ctrlr_is_failed(struct nvme_ctrlr *nvme_ctrlr)
    1074                 :            : {
    1075   [ +  +  #  # ]:     406690 :         if (nvme_ctrlr->destruct) {
    1076                 :        512 :                 return true;
    1077                 :            :         }
    1078                 :            : 
    1079   [ +  +  #  # ]:     406178 :         if (nvme_ctrlr->fast_io_fail_timedout) {
    1080                 :      86282 :                 return true;
    1081                 :            :         }
    1082                 :            : 
    1083   [ +  +  #  # ]:     319896 :         if (nvme_ctrlr->resetting) {
    1084   [ +  +  #  #  :     225448 :                 if (nvme_ctrlr->opts.reconnect_delay_sec != 0) {
             #  #  #  # ]
    1085                 :       1039 :                         return false;
    1086                 :            :                 } else {
    1087                 :     224409 :                         return true;
    1088                 :            :                 }
    1089                 :            :         }
    1090                 :            : 
    1091   [ +  +  #  # ]:      94448 :         if (nvme_ctrlr->reconnect_is_delayed) {
    1092                 :       4102 :                 return false;
    1093                 :            :         }
    1094                 :            : 
    1095   [ -  +  #  # ]:      90346 :         if (nvme_ctrlr->disabled) {
    1096                 :          0 :                 return true;
    1097                 :            :         }
    1098                 :            : 
    1099   [ +  +  #  #  :      90346 :         if (spdk_nvme_ctrlr_is_failed(nvme_ctrlr->ctrlr)) {
                   #  # ]
    1100                 :      88293 :                 return true;
    1101                 :            :         } else {
    1102                 :       2053 :                 return false;
    1103                 :            :         }
    1104                 :          0 : }
    1105                 :            : 
    1106                 :            : static bool
    1107                 :      12692 : nvme_ctrlr_is_available(struct nvme_ctrlr *nvme_ctrlr)
    1108                 :            : {
    1109   [ +  +  #  # ]:      12692 :         if (nvme_ctrlr->destruct) {
    1110                 :        966 :                 return false;
    1111                 :            :         }
    1112                 :            : 
    1113   [ +  +  #  #  :      11726 :         if (spdk_nvme_ctrlr_is_failed(nvme_ctrlr->ctrlr)) {
                   #  # ]
    1114                 :         10 :                 return false;
    1115                 :            :         }
    1116                 :            : 
    1117   [ +  +  -  +  :      11716 :         if (nvme_ctrlr->resetting || nvme_ctrlr->reconnect_is_delayed) {
             #  #  #  # ]
    1118                 :       4796 :                 return false;
    1119                 :            :         }
    1120                 :            : 
    1121   [ -  +  #  # ]:       6920 :         if (nvme_ctrlr->disabled) {
    1122                 :          0 :                 return false;
    1123                 :            :         }
    1124                 :            : 
    1125                 :       6920 :         return true;
    1126                 :          0 : }
    1127                 :            : 
    1128                 :            : /* Simulate circular linked list. */
    1129                 :            : static inline struct nvme_io_path *
    1130                 :    7021122 : 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         [ +  + ]:    7021122 :         if (prev_path != NULL) {
    1135   [ #  #  #  #  :    5403393 :                 next_path = STAILQ_NEXT(prev_path, stailq);
                   #  # ]
    1136         [ +  + ]:    5403393 :                 if (next_path != NULL) {
    1137                 :    2620563 :                         return next_path;
    1138                 :            :                 }
    1139                 :          0 :         }
    1140                 :            : 
    1141   [ +  -  +  -  :    4400559 :         return STAILQ_FIRST(&nbdev_ch->io_path_list);
                   +  - ]
    1142                 :          1 : }
    1143                 :            : 
    1144                 :            : static struct nvme_io_path *
    1145                 :    2789773 : _bdev_nvme_find_io_path(struct nvme_bdev_channel *nbdev_ch)
    1146                 :            : {
    1147                 :    2789773 :         struct nvme_io_path *io_path, *start, *non_optimized = NULL;
    1148                 :            : 
    1149   [ +  -  +  - ]:    2789773 :         start = nvme_io_path_get_next(nbdev_ch, nbdev_ch->current_io_path);
    1150                 :            : 
    1151                 :    2789773 :         io_path = start;
    1152                 :          1 :         do {
    1153         [ +  + ]:    4868452 :                 if (spdk_likely(nvme_io_path_is_available(io_path))) {
    1154   [ +  +  +  -  :    3269273 :                         switch (io_path->nvme_ns->ana_state) {
          +  -  +  -  +  
                   -  - ]
    1155                 :     637102 :                         case SPDK_NVME_ANA_OPTIMIZED_STATE:
    1156   [ +  -  +  - ]:     637103 :                                 nbdev_ch->current_io_path = io_path;
    1157                 :     637103 :                                 return io_path;
    1158                 :    2632170 :                         case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
    1159         [ +  + ]:    2632170 :                                 if (non_optimized == NULL) {
    1160                 :    2049601 :                                         non_optimized = io_path;
    1161                 :          0 :                                 }
    1162                 :    2632170 :                                 break;
    1163                 :          0 :                         default:
    1164         [ #  # ]:          0 :                                 assert(false);
    1165                 :            :                                 break;
    1166                 :            :                         }
    1167                 :          0 :                 }
    1168                 :    4231349 :                 io_path = nvme_io_path_get_next(nbdev_ch, io_path);
    1169         [ +  + ]:    4231349 :         } while (io_path != start);
    1170                 :            : 
    1171   [ +  +  #  #  :    2152670 :         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   [ #  #  #  # ]:     537559 :                 nbdev_ch->current_io_path = non_optimized;
    1176                 :          0 :         }
    1177                 :            : 
    1178                 :    2152670 :         return non_optimized;
    1179                 :          1 : }
    1180                 :            : 
    1181                 :            : static struct nvme_io_path *
    1182                 :         12 : _bdev_nvme_find_io_path_min_qd(struct nvme_bdev_channel *nbdev_ch)
    1183                 :            : {
    1184                 :            :         struct nvme_io_path *io_path;
    1185                 :         12 :         struct nvme_io_path *optimized = NULL, *non_optimized = NULL;
    1186                 :         12 :         uint32_t opt_min_qd = UINT32_MAX, non_opt_min_qd = UINT32_MAX;
    1187                 :            :         uint32_t num_outstanding_reqs;
    1188                 :            : 
    1189   [ +  +  #  #  :         48 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1190   [ -  +  #  #  :         36 :                 if (spdk_unlikely(!nvme_qpair_is_connected(io_path->qpair))) {
                   #  # ]
    1191                 :            :                         /* The device is currently resetting. */
    1192                 :          0 :                         continue;
    1193                 :            :                 }
    1194                 :            : 
    1195   [ -  +  #  #  :         36 :                 if (spdk_unlikely(!nvme_ns_is_active(io_path->nvme_ns))) {
                   #  # ]
    1196                 :          0 :                         continue;
    1197                 :            :                 }
    1198                 :            : 
    1199   [ #  #  #  #  :         36 :                 num_outstanding_reqs = spdk_nvme_qpair_get_num_outstanding_reqs(io_path->qpair->qpair);
             #  #  #  # ]
    1200   [ +  +  +  #  :         36 :                 switch (io_path->nvme_ns->ana_state) {
          #  #  #  #  #  
                   #  # ]
    1201                 :         18 :                 case SPDK_NVME_ANA_OPTIMIZED_STATE:
    1202         [ +  + ]:         18 :                         if (num_outstanding_reqs < opt_min_qd) {
    1203                 :         15 :                                 opt_min_qd = num_outstanding_reqs;
    1204                 :         15 :                                 optimized = io_path;
    1205                 :          0 :                         }
    1206                 :         18 :                         break;
    1207                 :          9 :                 case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
    1208         [ +  - ]:          9 :                         if (num_outstanding_reqs < non_opt_min_qd) {
    1209                 :          9 :                                 non_opt_min_qd = num_outstanding_reqs;
    1210                 :          9 :                                 non_optimized = io_path;
    1211                 :          0 :                         }
    1212                 :          9 :                         break;
    1213                 :          9 :                 default:
    1214                 :          9 :                         break;
    1215                 :            :                 }
    1216                 :          0 :         }
    1217                 :            : 
    1218                 :            :         /* don't cache io path for BDEV_NVME_MP_SELECTOR_QUEUE_DEPTH selector */
    1219         [ +  + ]:         12 :         if (optimized != NULL) {
    1220                 :          9 :                 return optimized;
    1221                 :            :         }
    1222                 :            : 
    1223                 :          3 :         return non_optimized;
    1224                 :          0 : }
    1225                 :            : 
    1226                 :            : static inline struct nvme_io_path *
    1227                 :   45302561 : bdev_nvme_find_io_path(struct nvme_bdev_channel *nbdev_ch)
    1228                 :            : {
    1229   [ +  +  +  -  :   45302561 :         if (spdk_likely(nbdev_ch->current_io_path != NULL)) {
                   +  + ]
    1230   [ +  +  +  -  :   43684820 :                 if (nbdev_ch->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE) {
                   -  + ]
    1231   [ +  -  +  - ]:   42512767 :                         return nbdev_ch->current_io_path;
    1232   [ +  -  #  #  :    1172053 :                 } else if (nbdev_ch->mp_selector == BDEV_NVME_MP_SELECTOR_ROUND_ROBIN) {
                   #  # ]
    1233   [ +  +  #  #  :    1172053 :                         if (++nbdev_ch->rr_counter < nbdev_ch->rr_min_io) {
             #  #  #  # ]
    1234   [ #  #  #  # ]:          9 :                                 return nbdev_ch->current_io_path;
    1235                 :            :                         }
    1236   [ #  #  #  # ]:    1172044 :                         nbdev_ch->rr_counter = 0;
    1237                 :          0 :                 }
    1238                 :          0 :         }
    1239                 :            : 
    1240   [ +  +  +  -  :    2789785 :         if (nbdev_ch->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE ||
             -  +  #  # ]
    1241   [ +  +  #  # ]:    1172122 :             nbdev_ch->mp_selector == BDEV_NVME_MP_SELECTOR_ROUND_ROBIN) {
    1242                 :    2789773 :                 return _bdev_nvme_find_io_path(nbdev_ch);
    1243                 :            :         } else {
    1244                 :         12 :                 return _bdev_nvme_find_io_path_min_qd(nbdev_ch);
    1245                 :            :         }
    1246                 :          3 : }
    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                 :     414524 : any_io_path_may_become_available(struct nvme_bdev_channel *nbdev_ch)
    1261                 :            : {
    1262                 :            :         struct nvme_io_path *io_path;
    1263                 :            : 
    1264   [ +  +  +  +  :     414524 :         if (nbdev_ch->resetting) {
             #  #  #  # ]
    1265                 :          3 :                 return false;
    1266                 :            :         }
    1267                 :            : 
    1268   [ +  +  #  #  :     815041 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1269   [ -  +  +  +  :     415545 :                 if (io_path->nvme_ns->ana_transition_timedout) {
          #  #  #  #  #  
                #  #  # ]
    1270                 :       1024 :                         continue;
    1271                 :            :                 }
    1272                 :            : 
    1273   [ +  +  #  #  :     414521 :                 if (nvme_qpair_is_connected(io_path->qpair) ||
             #  #  #  # ]
    1274   [ +  +  #  #  :     406690 :                     !nvme_ctrlr_is_failed(io_path->qpair->ctrlr)) {
             #  #  #  # ]
    1275                 :      15025 :                         return true;
    1276                 :            :                 }
    1277                 :          0 :         }
    1278                 :            : 
    1279                 :     399496 :         return false;
    1280                 :          0 : }
    1281                 :            : 
    1282                 :            : static void
    1283                 :      18523 : bdev_nvme_retry_io(struct nvme_bdev_channel *nbdev_ch, struct spdk_bdev_io *bdev_io)
    1284                 :            : {
    1285         [ #  # ]:      18523 :         struct nvme_bdev_io *nbdev_io = (struct nvme_bdev_io *)bdev_io->driver_ctx;
    1286                 :            :         struct spdk_io_channel *ch;
    1287                 :            : 
    1288   [ +  +  +  -  :      18523 :         if (nbdev_io->io_path != NULL && nvme_io_path_is_available(nbdev_io->io_path)) {
          #  #  #  #  #  
                #  #  # ]
    1289                 :       3501 :                 _bdev_nvme_submit_request(nbdev_ch, bdev_io);
    1290                 :          0 :         } else {
    1291                 :      15022 :                 ch = spdk_io_channel_from_ctx(nbdev_ch);
    1292                 :      15022 :                 bdev_nvme_submit_request(ch, bdev_io);
    1293                 :            :         }
    1294                 :      18523 : }
    1295                 :            : 
    1296                 :            : static int
    1297                 :       4355 : bdev_nvme_retry_ios(void *arg)
    1298                 :            : {
    1299                 :       4355 :         struct nvme_bdev_channel *nbdev_ch = arg;
    1300                 :            :         struct nvme_bdev_io *bio, *tmp_bio;
    1301                 :            :         uint64_t now, delay_us;
    1302                 :            : 
    1303                 :       4355 :         now = spdk_get_ticks();
    1304                 :            : 
    1305   [ +  +  #  #  :      22878 :         TAILQ_FOREACH_SAFE(bio, &nbdev_ch->retry_io_list, retry_link, tmp_bio) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1306   [ +  +  #  #  :      19271 :                 if (bio->retry_ticks > now) {
                   #  # ]
    1307                 :        748 :                         break;
    1308                 :            :                 }
    1309                 :            : 
    1310   [ +  +  #  #  :      18523 :                 TAILQ_REMOVE(&nbdev_ch->retry_io_list, bio, retry_link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    1311                 :            : 
    1312                 :      18523 :                 bdev_nvme_retry_io(nbdev_ch, spdk_bdev_io_from_ctx(bio));
    1313                 :          0 :         }
    1314                 :            : 
    1315         [ #  # ]:       4355 :         spdk_poller_unregister(&nbdev_ch->retry_io_poller);
    1316                 :            : 
    1317   [ #  #  #  #  :       4355 :         bio = TAILQ_FIRST(&nbdev_ch->retry_io_list);
                   #  # ]
    1318         [ +  + ]:       4355 :         if (bio != NULL) {
    1319   [ -  +  #  #  :        757 :                 delay_us = (bio->retry_ticks - now) * SPDK_SEC_TO_USEC / spdk_get_ticks_hz();
                   #  # ]
    1320                 :            : 
    1321   [ #  #  #  # ]:        757 :                 nbdev_ch->retry_io_poller = SPDK_POLLER_REGISTER(bdev_nvme_retry_ios, nbdev_ch,
    1322                 :            :                                             delay_us);
    1323                 :          0 :         }
    1324                 :            : 
    1325                 :       4355 :         return SPDK_POLLER_BUSY;
    1326                 :            : }
    1327                 :            : 
    1328                 :            : static void
    1329                 :      18529 : 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   [ #  #  #  #  :      18529 :         bio->retry_ticks = spdk_get_ticks() + delay_ms * spdk_get_ticks_hz() / 1000ULL;
                   #  # ]
    1335                 :            : 
    1336   [ +  +  #  #  :      45694 :         TAILQ_FOREACH_REVERSE(tmp_bio, &nbdev_ch->retry_io_list, retry_io_head, retry_link) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1337   [ +  +  #  #  :      42066 :                 if (tmp_bio->retry_ticks <= bio->retry_ticks) {
          #  #  #  #  #  
                      # ]
    1338   [ +  +  #  #  :      14901 :                         TAILQ_INSERT_AFTER(&nbdev_ch->retry_io_list, tmp_bio, bio,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1339                 :            :                                            retry_link);
    1340                 :      14901 :                         return;
    1341                 :            :                 }
    1342                 :          0 :         }
    1343                 :            : 
    1344                 :            :         /* No earlier I/Os were found. This I/O must be the new head. */
    1345   [ +  +  #  #  :       3628 :         TAILQ_INSERT_HEAD(&nbdev_ch->retry_io_list, bio, retry_link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1346                 :            : 
    1347         [ #  # ]:       3628 :         spdk_poller_unregister(&nbdev_ch->retry_io_poller);
    1348                 :            : 
    1349   [ #  #  #  # ]:       3628 :         nbdev_ch->retry_io_poller = SPDK_POLLER_REGISTER(bdev_nvme_retry_ios, nbdev_ch,
    1350                 :            :                                     delay_ms * 1000ULL);
    1351                 :          0 : }
    1352                 :            : 
    1353                 :            : static void
    1354                 :       2621 : bdev_nvme_abort_retry_ios(struct nvme_bdev_channel *nbdev_ch)
    1355                 :            : {
    1356                 :            :         struct nvme_bdev_io *bio, *tmp_bio;
    1357                 :            : 
    1358   [ +  +  +  -  :       2624 :         TAILQ_FOREACH_SAFE(bio, &nbdev_ch->retry_io_list, retry_link, tmp_bio) {
          +  -  +  -  #  
          #  #  #  #  #  
                   +  - ]
    1359   [ -  +  #  #  :          3 :                 TAILQ_REMOVE(&nbdev_ch->retry_io_list, bio, retry_link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    1360                 :          3 :                 __bdev_nvme_io_complete(spdk_bdev_io_from_ctx(bio), SPDK_BDEV_IO_STATUS_ABORTED, NULL);
    1361                 :          0 :         }
    1362                 :            : 
    1363         [ +  - ]:       2621 :         spdk_poller_unregister(&nbdev_ch->retry_io_poller);
    1364                 :       2621 : }
    1365                 :            : 
    1366                 :            : static int
    1367                 :       7293 : 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   [ +  +  #  #  :       7293 :         TAILQ_FOREACH(bio, &nbdev_ch->retry_io_list, retry_link) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1373         [ +  - ]:          3 :                 if (bio == bio_to_abort) {
    1374   [ -  +  #  #  :          3 :                         TAILQ_REMOVE(&nbdev_ch->retry_io_list, bio, retry_link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    1375                 :          3 :                         __bdev_nvme_io_complete(spdk_bdev_io_from_ctx(bio), SPDK_BDEV_IO_STATUS_ABORTED, NULL);
    1376                 :          3 :                         return 0;
    1377                 :            :                 }
    1378                 :          0 :         }
    1379                 :            : 
    1380                 :       7290 :         return -ENOENT;
    1381                 :          0 : }
    1382                 :            : 
    1383                 :            : static void
    1384                 :       9362 : 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   [ +  +  -  +  :       9362 :         assert(spdk_nvme_cpl_is_error(cpl));
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1390                 :            : 
    1391   [ #  #  #  #  :       9362 :         nbdev = bdev_io->bdev->ctxt;
             #  #  #  # ]
    1392                 :            : 
    1393   [ +  +  #  #  :       9362 :         if (nbdev->err_stat == NULL) {
                   #  # ]
    1394                 :       5924 :                 return;
    1395                 :            :         }
    1396                 :            : 
    1397   [ #  #  #  #  :       3438 :         sct = cpl->status.sct;
                   #  # ]
    1398   [ #  #  #  #  :       3438 :         sc = cpl->status.sc;
                   #  # ]
    1399                 :            : 
    1400   [ -  +  #  # ]:       3438 :         pthread_mutex_lock(&nbdev->mutex);
    1401                 :            : 
    1402   [ #  #  #  #  :       3438 :         nbdev->err_stat->status_type[sct]++;
          #  #  #  #  #  
                      # ]
    1403         [ +  - ]:       3438 :         switch (sct) {
    1404                 :       3438 :         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   [ #  #  #  #  :       3438 :                 nbdev->err_stat->status[sct][sc]++;
          #  #  #  #  #  
                #  #  # ]
    1409                 :       3438 :                 break;
    1410                 :          0 :         default:
    1411                 :          0 :                 break;
    1412                 :            :         }
    1413                 :            : 
    1414   [ -  +  #  # ]:       3438 :         pthread_mutex_unlock(&nbdev->mutex);
    1415                 :          0 : }
    1416                 :            : 
    1417                 :            : static inline void
    1418                 :   42683192 : bdev_nvme_update_io_path_stat(struct nvme_bdev_io *bio)
    1419                 :            : {
    1420                 :   42683192 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    1421   [ +  -  +  -  :   42683192 :         uint64_t num_blocks = bdev_io->u.bdev.num_blocks;
             +  -  +  - ]
    1422   [ +  -  +  -  :   42683192 :         uint32_t blocklen = bdev_io->bdev->blocklen;
             +  -  +  - ]
    1423                 :            :         struct spdk_bdev_io_stat *stat;
    1424                 :            :         uint64_t tsc_diff;
    1425                 :            : 
    1426   [ +  -  +  -  :   42683192 :         if (bio->io_path->stat == NULL) {
          +  -  +  -  +  
                      - ]
    1427                 :   42683192 :                 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                 :          3 : }
    1509                 :            : 
    1510                 :            : static bool
    1511                 :       9286 : 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   [ #  #  #  # ]:       9286 :         struct nvme_io_path *io_path = bio->io_path;
    1517   [ #  #  #  #  :       9286 :         struct nvme_ctrlr *nvme_ctrlr = io_path->qpair->ctrlr;
             #  #  #  # ]
    1518                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    1519                 :            : 
    1520   [ +  +  #  #  :       9286 :         if (spdk_nvme_cpl_is_path_error(cpl) ||
          #  #  #  #  #  
                      # ]
    1521   [ +  +  +  +  :       7664 :             spdk_nvme_cpl_is_aborted_sq_deletion(cpl) ||
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1522         [ +  - ]:       3504 :             !nvme_io_path_is_available(io_path) ||
    1523         [ -  + ]:       3504 :             !nvme_ctrlr_is_available(nvme_ctrlr)) {
    1524                 :       5782 :                 bdev_nvme_clear_current_io_path(nbdev_ch);
    1525   [ #  #  #  # ]:       5782 :                 bio->io_path = NULL;
    1526   [ +  +  +  -  :       5782 :                 if (spdk_nvme_cpl_is_ana_error(cpl)) {
          +  +  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    1527         [ +  + ]:       1619 :                         if (nvme_ctrlr_read_ana_log_page(nvme_ctrlr) == 0) {
    1528   [ #  #  #  #  :         18 :                                 io_path->nvme_ns->ana_state_updating = true;
             #  #  #  # ]
    1529                 :          0 :                         }
    1530                 :          0 :                 }
    1531         [ +  + ]:       5782 :                 if (!any_io_path_may_become_available(nbdev_ch)) {
    1532                 :        320 :                         return false;
    1533                 :            :                 }
    1534         [ #  # ]:       5462 :                 *_delay_ms = 0;
    1535                 :          0 :         } else {
    1536   [ #  #  #  # ]:       3504 :                 bio->retry_count++;
    1537                 :            : 
    1538   [ #  #  #  # ]:       3504 :                 cdata = spdk_nvme_ctrlr_get_data(nvme_ctrlr->ctrlr);
    1539                 :            : 
    1540   [ +  +  #  #  :       3504 :                 if (cpl->status.crd != 0) {
             #  #  #  # ]
    1541   [ #  #  #  #  :          3 :                         *_delay_ms = cdata->crdt[cpl->status.crd] * 100;
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1542                 :          0 :                 } else {
    1543         [ #  # ]:       3501 :                         *_delay_ms = 0;
    1544                 :            :                 }
    1545                 :            :         }
    1546                 :            : 
    1547                 :       8966 :         return true;
    1548                 :          0 : }
    1549                 :            : 
    1550                 :            : static inline void
    1551                 :   42692554 : bdev_nvme_io_complete_nvme_status(struct nvme_bdev_io *bio,
    1552                 :            :                                   const struct spdk_nvme_cpl *cpl)
    1553                 :            : {
    1554                 :   42692554 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    1555                 :            :         struct nvme_bdev_channel *nbdev_ch;
    1556                 :   15311399 :         uint64_t delay_ms;
    1557                 :            : 
    1558   [ +  +  +  -  :   42692554 :         assert(!bdev_nvme_io_type_is_admin(bdev_io->type));
             +  -  #  # ]
    1559                 :            : 
    1560   [ +  +  +  +  :   42692554 :         if (spdk_likely(spdk_nvme_cpl_is_success(cpl))) {
          +  -  -  +  +  
          -  +  -  +  -  
                   +  - ]
    1561                 :   42683192 :                 bdev_nvme_update_io_path_stat(bio);
    1562                 :   42683192 :                 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                 :       9362 :         bdev_nvme_update_nvme_error_stat(bdev_io, cpl);
    1569                 :            : 
    1570   [ +  +  +  +  :       9362 :         if (cpl->status.dnr != 0 || spdk_nvme_cpl_is_aborted_by_request(cpl) ||
          +  +  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1571   [ +  +  +  +  :       9307 :             (g_opts.bdev_retry_count != -1 && bio->retry_count >= g_opts.bdev_retry_count)) {
          #  #  #  #  #  
                      # ]
    1572                 :         76 :                 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   [ -  +  #  #  :       9286 :         if (bdev_io->u.bdev.accel_sequence != NULL) {
          #  #  #  #  #  
                      # ]
    1578                 :          0 :                 goto complete;
    1579                 :            :         }
    1580                 :            : 
    1581                 :       9286 :         nbdev_ch = spdk_io_channel_get_ctx(spdk_bdev_io_get_io_channel(bdev_io));
    1582                 :            : 
    1583         [ +  + ]:       9286 :         if (bdev_nvme_check_retry_io(bio, cpl, nbdev_ch, &delay_ms)) {
    1584                 :       8966 :                 bdev_nvme_queue_retry_io(nbdev_ch, bio, delay_ms);
    1585                 :       8966 :                 return;
    1586                 :            :         }
    1587                 :            : 
    1588                 :   15311698 : complete:
    1589   [ +  -  +  - ]:   42683588 :         bio->retry_count = 0;
    1590   [ +  -  +  - ]:   42683588 :         bio->submit_tsc = 0;
    1591   [ +  -  +  -  :   42683588 :         bdev_io->u.bdev.accel_sequence = NULL;
             +  -  +  - ]
    1592                 :   42683588 :         __bdev_nvme_io_complete(bdev_io, 0, cpl);
    1593                 :          3 : }
    1594                 :            : 
    1595                 :            : static inline void
    1596                 :    2605880 : bdev_nvme_io_complete(struct nvme_bdev_io *bio, int rc)
    1597                 :            : {
    1598                 :    2605880 :         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   [ -  +  #  #  :    2605880 :         assert(!bdev_nvme_io_type_is_admin(bdev_io->type));
             #  #  #  # ]
    1603                 :            : 
    1604   [ +  +  +  - ]:    2605880 :         switch (rc) {
    1605                 :    2026622 :         case 0:
    1606                 :    2026622 :                 io_status = SPDK_BDEV_IO_STATUS_SUCCESS;
    1607                 :    2026622 :                 break;
    1608                 :     170516 :         case -ENOMEM:
    1609                 :     170516 :                 io_status = SPDK_BDEV_IO_STATUS_NOMEM;
    1610                 :     170516 :                 break;
    1611                 :     408742 :         case -ENXIO:
    1612   [ +  +  +  -  :     408742 :                 if (g_opts.bdev_retry_count == -1 || bio->retry_count < g_opts.bdev_retry_count) {
          #  #  #  #  #  
                #  #  # ]
    1613                 :     408742 :                         nbdev_ch = spdk_io_channel_get_ctx(spdk_bdev_io_get_io_channel(bdev_io));
    1614                 :            : 
    1615                 :     408742 :                         bdev_nvme_clear_current_io_path(nbdev_ch);
    1616   [ #  #  #  # ]:     408742 :                         bio->io_path = NULL;
    1617                 :            : 
    1618         [ +  + ]:     408742 :                         if (any_io_path_may_become_available(nbdev_ch)) {
    1619                 :       9563 :                                 bdev_nvme_queue_retry_io(nbdev_ch, bio, 1000ULL);
    1620                 :       9563 :                                 return;
    1621                 :            :                         }
    1622                 :          0 :                 }
    1623                 :            : 
    1624                 :            :         /* fallthrough */
    1625                 :            :         default:
    1626   [ #  #  #  #  :     399179 :                 spdk_accel_sequence_abort(bdev_io->u.bdev.accel_sequence);
             #  #  #  # ]
    1627   [ #  #  #  #  :     399179 :                 bdev_io->u.bdev.accel_sequence = NULL;
             #  #  #  # ]
    1628                 :     399179 :                 io_status = SPDK_BDEV_IO_STATUS_FAILED;
    1629                 :     399179 :                 break;
    1630                 :            :         }
    1631                 :            : 
    1632   [ #  #  #  # ]:    2596317 :         bio->retry_count = 0;
    1633   [ #  #  #  # ]:    2596317 :         bio->submit_tsc = 0;
    1634                 :    2596317 :         __bdev_nvme_io_complete(bdev_io, io_status, NULL);
    1635                 :          0 : }
    1636                 :            : 
    1637                 :            : static inline void
    1638                 :         12 : bdev_nvme_admin_complete(struct nvme_bdev_io *bio, int rc)
    1639                 :            : {
    1640                 :         12 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    1641                 :            :         enum spdk_bdev_io_status io_status;
    1642                 :            : 
    1643   [ +  -  +  # ]:         12 :         switch (rc) {
    1644                 :          3 :         case 0:
    1645                 :          3 :                 io_status = SPDK_BDEV_IO_STATUS_SUCCESS;
    1646                 :          3 :                 break;
    1647                 :          0 :         case -ENOMEM:
    1648                 :          0 :                 io_status = SPDK_BDEV_IO_STATUS_NOMEM;
    1649                 :          0 :                 break;
    1650                 :          9 :         case -ENXIO:
    1651                 :            :         /* fallthrough */
    1652                 :            :         default:
    1653                 :          9 :                 io_status = SPDK_BDEV_IO_STATUS_FAILED;
    1654                 :          9 :                 break;
    1655                 :            :         }
    1656                 :            : 
    1657                 :         12 :         __bdev_nvme_io_complete(bdev_io, io_status, NULL);
    1658                 :         12 : }
    1659                 :            : 
    1660                 :            : static void
    1661                 :       1545 : bdev_nvme_clear_io_path_caches_done(struct nvme_ctrlr *nvme_ctrlr,
    1662                 :            :                                     void *ctx, int status)
    1663                 :            : {
    1664   [ -  +  #  # ]:       1545 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    1665                 :            : 
    1666   [ -  +  #  #  :       1545 :         assert(nvme_ctrlr->io_path_cache_clearing == true);
                   #  # ]
    1667         [ #  # ]:       1545 :         nvme_ctrlr->io_path_cache_clearing = false;
    1668                 :            : 
    1669         [ +  - ]:       1545 :         if (!nvme_ctrlr_can_be_unregistered(nvme_ctrlr)) {
    1670   [ -  +  #  # ]:       1545 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    1671                 :       1545 :                 return;
    1672                 :            :         }
    1673                 :            : 
    1674   [ #  #  #  # ]:          0 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    1675                 :            : 
    1676                 :          0 :         nvme_ctrlr_unregister(nvme_ctrlr);
    1677                 :          0 : }
    1678                 :            : 
    1679                 :            : static void
    1680                 :      10140 : _bdev_nvme_clear_io_path_cache(struct nvme_qpair *nvme_qpair)
    1681                 :            : {
    1682                 :            :         struct nvme_io_path *io_path;
    1683                 :            : 
    1684   [ +  +  +  -  :      17755 :         TAILQ_FOREACH(io_path, &nvme_qpair->io_path_list, tailq) {
          +  -  +  +  +  
             -  +  -  +  
                      - ]
    1685   [ +  +  +  -  :       7615 :                 if (io_path->nbdev_ch == NULL) {
                   -  + ]
    1686                 :       5003 :                         continue;
    1687                 :            :                 }
    1688   [ #  #  #  # ]:       2612 :                 bdev_nvme_clear_current_io_path(io_path->nbdev_ch);
    1689                 :          0 :         }
    1690                 :      10140 : }
    1691                 :            : 
    1692                 :            : static void
    1693                 :       1498 : 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   [ -  +  #  #  :       1498 :         assert(ctrlr_ch->qpair != NULL);
             #  #  #  # ]
    1699                 :            : 
    1700   [ #  #  #  # ]:       1498 :         _bdev_nvme_clear_io_path_cache(ctrlr_ch->qpair);
    1701                 :            : 
    1702                 :       1498 :         nvme_ctrlr_for_each_channel_continue(i, 0);
    1703                 :       1498 : }
    1704                 :            : 
    1705                 :            : static void
    1706                 :       7310 : bdev_nvme_clear_io_path_caches(struct nvme_ctrlr *nvme_ctrlr)
    1707                 :            : {
    1708   [ -  +  #  # ]:       7310 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    1709   [ +  +  +  + ]:       7310 :         if (!nvme_ctrlr_is_available(nvme_ctrlr) ||
    1710         [ #  # ]:          0 :             nvme_ctrlr->io_path_cache_clearing) {
    1711   [ -  +  #  # ]:       5765 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    1712                 :       5765 :                 return;
    1713                 :            :         }
    1714                 :            : 
    1715         [ #  # ]:       1545 :         nvme_ctrlr->io_path_cache_clearing = true;
    1716   [ -  +  #  # ]:       1545 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    1717                 :            : 
    1718                 :       1545 :         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                 :          0 : }
    1723                 :            : 
    1724                 :            : static struct nvme_qpair *
    1725                 :       2710 : nvme_poll_group_get_qpair(struct nvme_poll_group *group, struct spdk_nvme_qpair *qpair)
    1726                 :            : {
    1727                 :            :         struct nvme_qpair *nvme_qpair;
    1728                 :            : 
    1729   [ +  -  +  -  :       2862 :         TAILQ_FOREACH(nvme_qpair, &group->qpair_list, tailq) {
          +  -  -  +  #  
             #  #  #  #  
                      # ]
    1730   [ +  +  +  -  :       2862 :                 if (nvme_qpair->qpair == qpair) {
                   -  + ]
    1731                 :       2710 :                         break;
    1732                 :            :                 }
    1733                 :          0 :         }
    1734                 :            : 
    1735                 :       2710 :         return nvme_qpair;
    1736                 :            : }
    1737                 :            : 
    1738                 :            : static void nvme_qpair_delete(struct nvme_qpair *nvme_qpair);
    1739                 :            : 
    1740                 :            : static void
    1741                 :       2710 : bdev_nvme_disconnected_qpair_cb(struct spdk_nvme_qpair *qpair, void *poll_group_ctx)
    1742                 :            : {
    1743                 :       2710 :         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                 :       2710 :         nvme_qpair = nvme_poll_group_get_qpair(group, qpair);
    1750         [ +  + ]:       2710 :         if (nvme_qpair == NULL) {
    1751                 :          0 :                 return;
    1752                 :            :         }
    1753                 :            : 
    1754   [ +  -  +  -  :       2710 :         if (nvme_qpair->qpair != NULL) {
                   -  + ]
    1755   [ +  -  +  - ]:       2710 :                 spdk_nvme_ctrlr_free_io_qpair(nvme_qpair->qpair);
    1756   [ +  -  +  - ]:       2710 :                 nvme_qpair->qpair = NULL;
    1757                 :          1 :         }
    1758                 :            : 
    1759                 :       2710 :         _bdev_nvme_clear_io_path_cache(nvme_qpair);
    1760                 :            : 
    1761   [ +  -  +  - ]:       2710 :         nvme_ctrlr = nvme_qpair->ctrlr;
    1762   [ +  -  +  - ]:       2710 :         ctrlr_ch = nvme_qpair->ctrlr_ch;
    1763                 :            : 
    1764         [ +  + ]:       2710 :         if (ctrlr_ch != NULL) {
    1765   [ +  +  #  #  :        316 :                 if (ctrlr_ch->reset_iter != NULL) {
                   #  # ]
    1766                 :            :                         /* We are in a full reset sequence. */
    1767   [ -  +  #  #  :        258 :                         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   [ -  +  -  +  :        258 :                                 NVME_CTRLR_INFOLOG(nvme_ctrlr,
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1777                 :            :                                                    "qpair %p was disconnected and freed in a reset ctrlr sequence.\n",
    1778                 :            :                                                    qpair);
    1779                 :        258 :                                 status = 0;
    1780                 :            :                         }
    1781   [ #  #  #  # ]:        258 :                         nvme_ctrlr_for_each_channel_continue(ctrlr_ch->reset_iter, status);
    1782   [ #  #  #  # ]:        258 :                         ctrlr_ch->reset_iter = NULL;
    1783                 :          0 :                 } else {
    1784                 :            :                         /* qpair was disconnected unexpectedly. Reset controller for recovery. */
    1785   [ -  +  -  +  :         58 :                         NVME_CTRLR_INFOLOG(nvme_ctrlr, "qpair %p was disconnected and freed. reset controller.\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1786                 :            :                                            qpair);
    1787                 :         58 :                         bdev_nvme_failover_ctrlr(nvme_ctrlr);
    1788                 :            :                 }
    1789                 :          0 :         } else {
    1790                 :            :                 /* In this case, ctrlr_channel is already deleted. */
    1791   [ +  +  +  +  :       2394 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "qpair %p was disconnected and freed. delete nvme_qpair.\n",
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1792                 :            :                                    qpair);
    1793                 :       2394 :                 nvme_qpair_delete(nvme_qpair);
    1794                 :            :         }
    1795                 :          1 : }
    1796                 :            : 
    1797                 :            : static void
    1798                 :         15 : bdev_nvme_check_io_qpairs(struct nvme_poll_group *group)
    1799                 :            : {
    1800                 :            :         struct nvme_qpair *nvme_qpair;
    1801                 :            : 
    1802   [ +  +  #  #  :         30 :         TAILQ_FOREACH(nvme_qpair, &group->qpair_list, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1803   [ +  +  -  +  :         15 :                 if (nvme_qpair->qpair == NULL || nvme_qpair->ctrlr_ch == NULL) {
          #  #  #  #  #  
                #  #  # ]
    1804                 :          1 :                         continue;
    1805                 :            :                 }
    1806                 :            : 
    1807   [ +  -  #  #  :         14 :                 if (spdk_nvme_qpair_get_failure_reason(nvme_qpair->qpair) !=
                   #  # ]
    1808                 :            :                     SPDK_NVME_QPAIR_FAILURE_NONE) {
    1809                 :         14 :                         _bdev_nvme_clear_io_path_cache(nvme_qpair);
    1810                 :          0 :                 }
    1811                 :          0 :         }
    1812                 :         15 : }
    1813                 :            : 
    1814                 :            : static int
    1815                 : 1218698950 : bdev_nvme_poll(void *arg)
    1816                 :            : {
    1817                 : 1218698950 :         struct nvme_poll_group *group = arg;
    1818                 :            :         int64_t num_completions;
    1819                 :            : 
    1820   [ +  +  +  +  : 1218698950 :         if (group->collect_spin_stat && group->start_ticks == 0) {
          +  -  -  +  #  
             #  #  #  #  
                      # ]
    1821   [ #  #  #  # ]:          0 :                 group->start_ticks = spdk_get_ticks();
    1822                 :          0 :         }
    1823                 :            : 
    1824   [ +  -  +  - ]: 1218698950 :         num_completions = spdk_nvme_poll_group_process_completions(group->group, 0,
    1825                 :            :                           bdev_nvme_disconnected_qpair_cb);
    1826   [ +  +  +  +  : 1218698950 :         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         [ +  + ]: 1218698950 :         if (spdk_unlikely(num_completions < 0)) {
    1839                 :         15 :                 bdev_nvme_check_io_qpairs(group);
    1840                 :          0 :         }
    1841                 :            : 
    1842                 : 1218698950 :         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                 :       1336 : bdev_nvme_change_adminq_poll_period(struct nvme_ctrlr *nvme_ctrlr, uint64_t new_period_us)
    1849                 :            : {
    1850         [ -  + ]:       1336 :         if (spdk_interrupt_mode_is_enabled()) {
    1851                 :          0 :                 return;
    1852                 :            :         }
    1853                 :            : 
    1854         [ #  # ]:       1336 :         spdk_poller_unregister(&nvme_ctrlr->adminq_timer_poller);
    1855                 :            : 
    1856   [ #  #  #  # ]:       1336 :         nvme_ctrlr->adminq_timer_poller = SPDK_POLLER_REGISTER(bdev_nvme_poll_adminq,
    1857                 :            :                                           nvme_ctrlr, new_period_us);
    1858                 :          0 : }
    1859                 :            : 
    1860                 :            : static int
    1861                 :     933489 : bdev_nvme_poll_adminq(void *arg)
    1862                 :            : {
    1863                 :            :         int32_t rc;
    1864                 :     933489 :         struct nvme_ctrlr *nvme_ctrlr = arg;
    1865                 :            :         nvme_ctrlr_disconnected_cb disconnected_cb;
    1866                 :            : 
    1867   [ +  +  #  # ]:     933489 :         assert(nvme_ctrlr != NULL);
    1868                 :            : 
    1869   [ +  -  +  - ]:     933489 :         rc = spdk_nvme_ctrlr_process_admin_completions(nvme_ctrlr->ctrlr);
    1870         [ +  + ]:     933489 :         if (rc < 0) {
    1871   [ #  #  #  # ]:       1480 :                 disconnected_cb = nvme_ctrlr->disconnected_cb;
    1872   [ #  #  #  # ]:       1480 :                 nvme_ctrlr->disconnected_cb = NULL;
    1873                 :            : 
    1874         [ +  + ]:       1480 :                 if (disconnected_cb != NULL) {
    1875                 :        668 :                         bdev_nvme_change_adminq_poll_period(nvme_ctrlr,
    1876         [ #  # ]:          0 :                                                             g_opts.nvme_adminq_poll_period_us);
    1877   [ #  #  #  # ]:        668 :                         disconnected_cb(nvme_ctrlr);
    1878                 :          0 :                 } else {
    1879                 :        812 :                         bdev_nvme_failover_ctrlr(nvme_ctrlr);
    1880                 :            :                 }
    1881   [ +  +  +  -  :     932009 :         } else if (spdk_nvme_ctrlr_get_admin_qp_failure_reason(nvme_ctrlr->ctrlr) !=
                   +  - ]
    1882                 :            :                    SPDK_NVME_QPAIR_FAILURE_NONE) {
    1883                 :       7198 :                 bdev_nvme_clear_io_path_caches(nvme_ctrlr);
    1884                 :          0 :         }
    1885                 :            : 
    1886                 :     933489 :         return rc == 0 ? SPDK_POLLER_IDLE : SPDK_POLLER_BUSY;
    1887                 :            : }
    1888                 :            : 
    1889                 :            : static void
    1890                 :       1470 : nvme_bdev_free(void *io_device)
    1891                 :            : {
    1892                 :       1470 :         struct nvme_bdev *nvme_disk = io_device;
    1893                 :            : 
    1894   [ +  +  +  - ]:       1470 :         pthread_mutex_destroy(&nvme_disk->mutex);
    1895   [ +  -  +  -  :       1470 :         free(nvme_disk->disk.name);
                   +  - ]
    1896   [ +  -  +  - ]:       1470 :         free(nvme_disk->err_stat);
    1897                 :       1470 :         free(nvme_disk);
    1898                 :       1470 : }
    1899                 :            : 
    1900                 :            : static int
    1901                 :       1465 : bdev_nvme_destruct(void *ctx)
    1902                 :            : {
    1903                 :       1465 :         struct nvme_bdev *nvme_disk = ctx;
    1904                 :            :         struct nvme_ns *nvme_ns, *tmp_nvme_ns;
    1905                 :            : 
    1906                 :        396 :         SPDK_DTRACE_PROBE2(bdev_nvme_destruct, nvme_disk->nbdev_ctrlr->name, nvme_disk->nsid);
    1907                 :            : 
    1908   [ +  +  +  -  :       2939 :         TAILQ_FOREACH_SAFE(nvme_ns, &nvme_disk->nvme_ns_list, tailq, tmp_nvme_ns) {
          +  -  +  +  +  
          -  +  -  +  -  
                   +  + ]
    1909   [ +  +  +  -  :       1474 :                 pthread_mutex_lock(&nvme_ns->ctrlr->mutex);
             +  -  +  - ]
    1910                 :            : 
    1911   [ +  -  +  - ]:       1474 :                 nvme_ns->bdev = NULL;
    1912                 :            : 
    1913   [ +  +  +  -  :       1474 :                 assert(nvme_ns->id > 0);
             +  -  #  # ]
    1914                 :            : 
    1915   [ +  +  +  -  :       1474 :                 if (nvme_ctrlr_get_ns(nvme_ns->ctrlr, nvme_ns->id) == NULL) {
          +  -  +  -  +  
                      - ]
    1916   [ -  +  #  #  :        523 :                         pthread_mutex_unlock(&nvme_ns->ctrlr->mutex);
             #  #  #  # ]
    1917                 :            : 
    1918   [ #  #  #  # ]:        523 :                         nvme_ctrlr_put_ref(nvme_ns->ctrlr);
    1919                 :        523 :                         nvme_ns_free(nvme_ns);
    1920                 :          0 :                 } else {
    1921   [ +  +  +  -  :        951 :                         pthread_mutex_unlock(&nvme_ns->ctrlr->mutex);
             +  -  +  - ]
    1922                 :            :                 }
    1923                 :          1 :         }
    1924                 :            : 
    1925         [ +  + ]:       1465 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    1926   [ +  +  +  -  :       1465 :         TAILQ_REMOVE(&nvme_disk->nbdev_ctrlr->bdevs, nvme_disk, tailq);
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1927         [ +  + ]:       1465 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    1928                 :            : 
    1929                 :       1465 :         spdk_io_device_unregister(nvme_disk, nvme_bdev_free);
    1930                 :            : 
    1931                 :       1465 :         return 0;
    1932                 :            : }
    1933                 :            : 
    1934                 :            : static int
    1935                 :       2713 : bdev_nvme_create_qpair(struct nvme_qpair *nvme_qpair)
    1936                 :            : {
    1937                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    1938                 :       1096 :         struct spdk_nvme_io_qpair_opts opts;
    1939                 :            :         struct spdk_nvme_qpair *qpair;
    1940                 :            :         int rc;
    1941                 :            : 
    1942   [ +  -  +  - ]:       2713 :         nvme_ctrlr = nvme_qpair->ctrlr;
    1943                 :            : 
    1944   [ +  -  +  - ]:       2713 :         spdk_nvme_ctrlr_get_default_io_qpair_opts(nvme_ctrlr->ctrlr, &opts, sizeof(opts));
    1945         [ +  - ]:       2713 :         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         [ +  + ]:       2713 :         if (!spdk_interrupt_mode_is_enabled()) {
    1951                 :       2711 :                 opts.async_mode = true;
    1952   [ +  +  +  -  :       2711 :                 opts.delay_cmd_submit = g_opts.delay_cmd_submit;
             +  -  +  - ]
    1953                 :          1 :         }
    1954   [ +  -  +  -  :       2713 :         opts.io_queue_requests = spdk_max(g_opts.io_queue_requests, opts.io_queue_requests);
          -  +  #  #  +  
                -  +  - ]
    1955   [ +  -  +  - ]:       2713 :         g_opts.io_queue_requests = opts.io_queue_requests;
    1956                 :            : 
    1957   [ +  -  +  - ]:       2713 :         qpair = spdk_nvme_ctrlr_alloc_io_qpair(nvme_ctrlr->ctrlr, &opts, sizeof(opts));
    1958         [ +  + ]:       2713 :         if (qpair == NULL) {
    1959                 :          0 :                 return -1;
    1960                 :            :         }
    1961                 :            : 
    1962                 :        631 :         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   [ +  +  +  -  :       2713 :         assert(nvme_qpair->group != NULL);
             +  -  #  # ]
    1966                 :            : 
    1967   [ +  -  +  -  :       2713 :         rc = spdk_nvme_poll_group_add(nvme_qpair->group->group, qpair);
             +  -  +  - ]
    1968         [ -  + ]:       2713 :         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   [ +  -  +  - ]:       2713 :         rc = spdk_nvme_ctrlr_connect_io_qpair(nvme_ctrlr->ctrlr, qpair);
    1974         [ -  + ]:       2713 :         if (rc != 0) {
    1975   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "Unable to connect I/O qpair.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    1976                 :          0 :                 goto err;
    1977                 :            :         }
    1978                 :            : 
    1979   [ +  -  +  - ]:       2713 :         nvme_qpair->qpair = qpair;
    1980                 :            : 
    1981   [ +  +  +  + ]:       2713 :         if (!g_opts.disable_auto_failback) {
    1982                 :       2602 :                 _bdev_nvme_clear_io_path_cache(nvme_qpair);
    1983                 :          1 :         }
    1984                 :            : 
    1985   [ +  +  +  +  :       2713 :         NVME_CTRLR_INFOLOG(nvme_ctrlr, "Connecting qpair %p:%u started.\n",
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1986                 :            :                            qpair, spdk_nvme_qpair_get_id(qpair));
    1987                 :            : 
    1988                 :       2713 :         return 0;
    1989                 :            : 
    1990                 :          0 : err:
    1991                 :          0 :         spdk_nvme_ctrlr_free_io_qpair(qpair);
    1992                 :            : 
    1993                 :          0 :         return rc;
    1994                 :          1 : }
    1995                 :            : 
    1996                 :            : static void bdev_nvme_reset_io_continue(void *cb_arg, int rc);
    1997                 :            : 
    1998                 :            : static void
    1999                 :        668 : bdev_nvme_complete_pending_resets(struct nvme_ctrlr *nvme_ctrlr, bool success)
    2000                 :            : {
    2001                 :        668 :         int rc = 0;
    2002                 :            :         struct nvme_bdev_io *bio;
    2003                 :            : 
    2004   [ +  +  #  # ]:        668 :         if (!success) {
    2005                 :        476 :                 rc = -1;
    2006                 :          0 :         }
    2007                 :            : 
    2008   [ +  +  #  #  :        704 :         while (!TAILQ_EMPTY(&nvme_ctrlr->pending_resets)) {
             #  #  #  # ]
    2009   [ #  #  #  #  :         36 :                 bio = TAILQ_FIRST(&nvme_ctrlr->pending_resets);
                   #  # ]
    2010   [ -  +  #  #  :         36 :                 TAILQ_REMOVE(&nvme_ctrlr->pending_resets, bio, retry_link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2011                 :            : 
    2012                 :         36 :                 bdev_nvme_reset_io_continue(bio, rc);
    2013                 :            :         }
    2014                 :        668 : }
    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                 :        923 : 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   [ #  #  #  #  :        923 :         path_id = TAILQ_FIRST(&nvme_ctrlr->trids);
                   #  # ]
    2029   [ -  +  #  # ]:        923 :         assert(path_id);
    2030   [ -  +  #  #  :        923 :         assert(path_id == nvme_ctrlr->active_path_id);
             #  #  #  # ]
    2031   [ #  #  #  #  :        923 :         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   [ #  #  #  # ]:        923 :         path_id->last_failed_tsc = spdk_get_ticks();
    2037                 :            : 
    2038         [ +  + ]:        923 :         if (next_path == NULL) {
    2039                 :            :                 /* There is no alternate trid within a controller. */
    2040                 :        874 :                 return false;
    2041                 :            :         }
    2042                 :            : 
    2043   [ +  +  +  +  :         49 :         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                 :          9 :                 return false;
    2048                 :            :         }
    2049                 :            : 
    2050   [ -  +  #  #  :         40 :         assert(path_id->trid.trtype != SPDK_NVME_TRANSPORT_PCIE);
          #  #  #  #  #  
                      # ]
    2051                 :            : 
    2052   [ +  -  #  #  :         40 :         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   [ #  #  #  # ]:         40 :         spdk_nvme_ctrlr_fail(nvme_ctrlr->ctrlr);
    2057   [ #  #  #  # ]:         40 :         nvme_ctrlr->active_path_id = next_path;
    2058   [ #  #  #  #  :         40 :         rc = spdk_nvme_ctrlr_set_trid(nvme_ctrlr->ctrlr, &next_path->trid);
                   #  # ]
    2059   [ -  +  #  # ]:         40 :         assert(rc == 0);
    2060   [ +  -  #  #  :         40 :         TAILQ_REMOVE(&nvme_ctrlr->trids, path_id, link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2061   [ +  +  #  # ]:         40 :         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   [ #  #  #  #  :         30 :                 TAILQ_INSERT_TAIL(&nvme_ctrlr->trids, path_id, link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    2066                 :          0 :         } else {
    2067                 :         10 :                 free(path_id);
    2068                 :            :         }
    2069                 :            : 
    2070   [ +  +  +  +  :         40 :         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                 :         37 :                 return true;
    2075                 :            :         }
    2076                 :            : 
    2077   [ #  #  #  #  :          3 :         if (spdk_get_ticks() > next_path->last_failed_tsc + spdk_get_ticks_hz() *
                   #  # ]
    2078   [ -  +  #  #  :          3 :             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                 :          3 :         return false;
    2085                 :          0 : }
    2086                 :            : 
    2087                 :            : static bool
    2088                 :     707632 : bdev_nvme_check_ctrlr_loss_timeout(struct nvme_ctrlr *nvme_ctrlr)
    2089                 :            : {
    2090                 :            :         int32_t elapsed;
    2091                 :            : 
    2092   [ +  +  #  #  :     707632 :         if (nvme_ctrlr->opts.ctrlr_loss_timeout_sec == 0 ||
          #  #  #  #  #  
                      # ]
    2093   [ +  +  #  #  :     219220 :             nvme_ctrlr->opts.ctrlr_loss_timeout_sec == -1) {
                   #  # ]
    2094                 :     492310 :                 return false;
    2095                 :            :         }
    2096                 :            : 
    2097   [ -  +  #  #  :     215322 :         elapsed = (spdk_get_ticks() - nvme_ctrlr->reset_start_tsc) / spdk_get_ticks_hz();
                   #  # ]
    2098   [ +  +  #  #  :     215322 :         if (elapsed >= nvme_ctrlr->opts.ctrlr_loss_timeout_sec) {
             #  #  #  # ]
    2099                 :         64 :                 return true;
    2100                 :            :         } else {
    2101                 :     215258 :                 return false;
    2102                 :            :         }
    2103                 :          0 : }
    2104                 :            : 
    2105                 :            : static bool
    2106                 :         81 : bdev_nvme_check_fast_io_fail_timeout(struct nvme_ctrlr *nvme_ctrlr)
    2107                 :            : {
    2108                 :            :         uint32_t elapsed;
    2109                 :            : 
    2110   [ +  +  #  #  :         81 :         if (nvme_ctrlr->opts.fast_io_fail_timeout_sec == 0) {
             #  #  #  # ]
    2111                 :         54 :                 return false;
    2112                 :            :         }
    2113                 :            : 
    2114   [ -  +  #  #  :         27 :         elapsed = (spdk_get_ticks() - nvme_ctrlr->reset_start_tsc) / spdk_get_ticks_hz();
                   #  # ]
    2115   [ +  +  #  #  :         27 :         if (elapsed >= nvme_ctrlr->opts.fast_io_fail_timeout_sec) {
             #  #  #  # ]
    2116                 :         13 :                 return true;
    2117                 :            :         } else {
    2118                 :         14 :                 return false;
    2119                 :            :         }
    2120                 :          0 : }
    2121                 :            : 
    2122                 :            : static void bdev_nvme_reset_ctrlr_complete(struct nvme_ctrlr *nvme_ctrlr, bool success);
    2123                 :            : 
    2124                 :            : static void
    2125                 :        671 : nvme_ctrlr_disconnect(struct nvme_ctrlr *nvme_ctrlr, nvme_ctrlr_disconnected_cb cb_fn)
    2126                 :            : {
    2127                 :            :         int rc;
    2128                 :            : 
    2129   [ -  +  +  +  :        671 :         NVME_CTRLR_INFOLOG(nvme_ctrlr, "Start disconnecting ctrlr.\n");
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2130                 :            : 
    2131   [ #  #  #  # ]:        671 :         rc = spdk_nvme_ctrlr_disconnect(nvme_ctrlr->ctrlr);
    2132         [ +  + ]:        671 :         if (rc != 0) {
    2133   [ +  -  #  #  :          3 :                 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                 :          3 :                 bdev_nvme_reset_ctrlr_complete(nvme_ctrlr, false);
    2139                 :          3 :                 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   [ -  +  #  #  :        668 :         assert(nvme_ctrlr->disconnected_cb == NULL);
             #  #  #  # ]
    2146   [ #  #  #  # ]:        668 :         nvme_ctrlr->disconnected_cb = cb_fn;
    2147                 :            : 
    2148                 :            :         /* During disconnection, reduce the period to poll adminq more often. */
    2149                 :        668 :         bdev_nvme_change_adminq_poll_period(nvme_ctrlr, 0);
    2150                 :          0 : }
    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                 :        668 : bdev_nvme_check_op_after_reset(struct nvme_ctrlr *nvme_ctrlr, bool success)
    2164                 :            : {
    2165         [ +  + ]:        668 :         if (nvme_ctrlr_can_be_unregistered(nvme_ctrlr)) {
    2166                 :            :                 /* Complete pending destruct after reset completes. */
    2167                 :         15 :                 return OP_COMPLETE_PENDING_DESTRUCT;
    2168   [ +  +  #  # ]:        653 :         } else if (nvme_ctrlr->pending_failover) {
    2169         [ #  # ]:          9 :                 nvme_ctrlr->pending_failover = false;
    2170   [ #  #  #  # ]:          9 :                 nvme_ctrlr->reset_start_tsc = 0;
    2171                 :          9 :                 return OP_FAILOVER;
    2172   [ +  +  +  +  :        644 :         } else if (success || nvme_ctrlr->opts.reconnect_delay_sec == 0) {
          #  #  #  #  #  
                #  #  # ]
    2173   [ #  #  #  # ]:        534 :                 nvme_ctrlr->reset_start_tsc = 0;
    2174                 :        534 :                 return OP_NONE;
    2175         [ +  + ]:        110 :         } else if (bdev_nvme_check_ctrlr_loss_timeout(nvme_ctrlr)) {
    2176                 :         29 :                 return OP_DESTRUCT;
    2177                 :            :         } else {
    2178         [ +  + ]:         81 :                 if (bdev_nvme_check_fast_io_fail_timeout(nvme_ctrlr)) {
    2179         [ #  # ]:         13 :                         nvme_ctrlr->fast_io_fail_timedout = true;
    2180                 :          0 :                 }
    2181                 :         81 :                 return OP_DELAYED_RECONNECT;
    2182                 :            :         }
    2183                 :          0 : }
    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                 :         72 : bdev_nvme_reconnect_delay_timer_expired(void *ctx)
    2190                 :            : {
    2191                 :         72 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    2192                 :            : 
    2193                 :         36 :         SPDK_DTRACE_PROBE1(bdev_nvme_ctrlr_reconnect_delay, nvme_ctrlr->nbdev_ctrlr->name);
    2194   [ -  +  #  # ]:         72 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2195                 :            : 
    2196         [ #  # ]:         72 :         spdk_poller_unregister(&nvme_ctrlr->reconnect_delay_timer);
    2197                 :            : 
    2198   [ -  +  #  # ]:         72 :         if (!nvme_ctrlr->reconnect_is_delayed) {
    2199   [ #  #  #  # ]:          0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2200                 :          0 :                 return SPDK_POLLER_BUSY;
    2201                 :            :         }
    2202                 :            : 
    2203         [ #  # ]:         72 :         nvme_ctrlr->reconnect_is_delayed = false;
    2204                 :            : 
    2205   [ -  +  #  # ]:         72 :         if (nvme_ctrlr->destruct) {
    2206   [ #  #  #  # ]:          0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2207                 :          0 :                 return SPDK_POLLER_BUSY;
    2208                 :            :         }
    2209                 :            : 
    2210   [ -  +  #  #  :         72 :         assert(nvme_ctrlr->resetting == false);
                   #  # ]
    2211         [ #  # ]:         72 :         nvme_ctrlr->resetting = true;
    2212                 :            : 
    2213   [ -  +  #  # ]:         72 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2214                 :            : 
    2215   [ #  #  #  # ]:         72 :         spdk_poller_resume(nvme_ctrlr->adminq_timer_poller);
    2216                 :            : 
    2217                 :         72 :         bdev_nvme_reconnect_ctrlr(nvme_ctrlr);
    2218                 :         72 :         return SPDK_POLLER_BUSY;
    2219                 :          0 : }
    2220                 :            : 
    2221                 :            : static void
    2222                 :         81 : bdev_nvme_start_reconnect_delay_timer(struct nvme_ctrlr *nvme_ctrlr)
    2223                 :            : {
    2224   [ #  #  #  # ]:         81 :         spdk_poller_pause(nvme_ctrlr->adminq_timer_poller);
    2225                 :            : 
    2226   [ -  +  #  #  :         81 :         assert(nvme_ctrlr->reconnect_is_delayed == false);
                   #  # ]
    2227         [ #  # ]:         81 :         nvme_ctrlr->reconnect_is_delayed = true;
    2228                 :            : 
    2229   [ -  +  #  #  :         81 :         assert(nvme_ctrlr->reconnect_delay_timer == NULL);
             #  #  #  # ]
    2230   [ #  #  #  #  :         81 :         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                 :         81 : }
    2234                 :            : 
    2235                 :            : static void remove_discovery_entry(struct nvme_ctrlr *nvme_ctrlr);
    2236                 :            : 
    2237                 :            : static void
    2238                 :        668 : bdev_nvme_reset_ctrlr_complete(struct nvme_ctrlr *nvme_ctrlr, bool success)
    2239                 :            : {
    2240   [ #  #  #  # ]:        668 :         bdev_nvme_ctrlr_op_cb ctrlr_op_cb_fn = nvme_ctrlr->ctrlr_op_cb_fn;
    2241   [ #  #  #  # ]:        668 :         void *ctrlr_op_cb_arg = nvme_ctrlr->ctrlr_op_cb_arg;
    2242                 :            :         enum bdev_nvme_op_after_reset op_after_reset;
    2243                 :            : 
    2244   [ -  +  #  #  :        668 :         assert(nvme_ctrlr->thread == spdk_get_thread());
             #  #  #  # ]
    2245                 :            : 
    2246   [ -  +  #  # ]:        668 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2247   [ +  +  #  # ]:        668 :         if (!success) {
    2248                 :            :                 /* Connecting the active trid failed. Set the next alternate trid to the
    2249                 :            :                  * active trid if it exists.
    2250                 :            :                  */
    2251         [ +  + ]:        482 :                 if (bdev_nvme_failover_trid(nvme_ctrlr, false, false)) {
    2252                 :            :                         /* The next alternate trid exists and is ready to try. Try it now. */
    2253   [ -  +  #  # ]:          6 :                         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2254                 :            : 
    2255   [ -  +  -  +  :          6 :                         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                 :          6 :                         nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_reconnect_ctrlr);
    2260                 :          6 :                         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                 :          0 :         } 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   [ #  #  #  #  :        186 :                 nvme_ctrlr->active_path_id->last_failed_tsc = 0;
             #  #  #  # ]
    2272                 :            :         }
    2273                 :            : 
    2274   [ -  +  +  +  :        662 :         NVME_CTRLR_INFOLOG(nvme_ctrlr, "Clear pending resets.\n");
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2275                 :            : 
    2276                 :            :         /* Make sure we clear any pending resets before returning. */
    2277         [ #  # ]:        662 :         bdev_nvme_complete_pending_resets(nvme_ctrlr, success);
    2278                 :            : 
    2279   [ +  +  #  # ]:        662 :         if (!success) {
    2280   [ +  -  #  #  :        476 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "Resetting controller failed.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2281                 :          0 :         } else {
    2282   [ +  +  #  #  :        186 :                 NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Resetting controller successful.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2283                 :            :         }
    2284                 :            : 
    2285         [ #  # ]:        662 :         nvme_ctrlr->resetting = false;
    2286         [ #  # ]:        662 :         nvme_ctrlr->dont_retry = false;
    2287         [ #  # ]:        662 :         nvme_ctrlr->in_failover = false;
    2288                 :            : 
    2289   [ #  #  #  # ]:        662 :         nvme_ctrlr->ctrlr_op_cb_fn = NULL;
    2290   [ #  #  #  # ]:        662 :         nvme_ctrlr->ctrlr_op_cb_arg = NULL;
    2291                 :            : 
    2292         [ #  # ]:        662 :         op_after_reset = bdev_nvme_check_op_after_reset(nvme_ctrlr, success);
    2293   [ -  +  #  # ]:        662 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2294                 :            : 
    2295                 :            :         /* Delay callbacks when the next operation is a failover. */
    2296   [ +  +  +  - ]:        662 :         if (ctrlr_op_cb_fn && op_after_reset != OP_FAILOVER) {
    2297   [ +  +  #  #  :        101 :                 ctrlr_op_cb_fn(ctrlr_op_cb_arg, success ? 0 : -1);
                   #  # ]
    2298                 :          0 :         }
    2299                 :            : 
    2300   [ +  +  +  +  :        662 :         switch (op_after_reset) {
                      + ]
    2301                 :         15 :         case OP_COMPLETE_PENDING_DESTRUCT:
    2302                 :         15 :                 nvme_ctrlr_unregister(nvme_ctrlr);
    2303                 :         15 :                 break;
    2304                 :         29 :         case OP_DESTRUCT:
    2305                 :         29 :                 bdev_nvme_delete_ctrlr(nvme_ctrlr, false);
    2306                 :         29 :                 remove_discovery_entry(nvme_ctrlr);
    2307                 :         29 :                 break;
    2308                 :         81 :         case OP_DELAYED_RECONNECT:
    2309                 :         81 :                 nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_start_reconnect_delay_timer);
    2310                 :         81 :                 break;
    2311                 :          9 :         case OP_FAILOVER:
    2312   [ #  #  #  # ]:          9 :                 nvme_ctrlr->ctrlr_op_cb_fn = ctrlr_op_cb_fn;
    2313   [ #  #  #  # ]:          9 :                 nvme_ctrlr->ctrlr_op_cb_arg = ctrlr_op_cb_arg;
    2314                 :          9 :                 bdev_nvme_failover_ctrlr(nvme_ctrlr);
    2315                 :          9 :                 break;
    2316                 :        528 :         default:
    2317                 :        528 :                 break;
    2318                 :            :         }
    2319                 :          0 : }
    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                 :        654 : 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   [ #  #  #  # ]:        654 :         nvme_qpair = ctrlr_ch->qpair;
    2336   [ -  +  #  # ]:        654 :         assert(nvme_qpair != NULL);
    2337                 :            : 
    2338                 :        654 :         _bdev_nvme_clear_io_path_cache(nvme_qpair);
    2339                 :            : 
    2340   [ #  #  #  # ]:        654 :         qpair = nvme_qpair->qpair;
    2341         [ +  + ]:        654 :         if (qpair != NULL) {
    2342   [ -  +  -  +  :        258 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "Start disconnecting qpair %p:%u.\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2343                 :            :                                    qpair, spdk_nvme_qpair_get_id(qpair));
    2344                 :            : 
    2345   [ +  +  #  #  :        258 :                 if (nvme_qpair->ctrlr->dont_retry) {
             #  #  #  # ]
    2346                 :        204 :                         spdk_nvme_qpair_set_abort_dnr(qpair, true);
    2347                 :          0 :                 }
    2348                 :        258 :                 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   [ -  +  #  #  :        258 :                 assert(ctrlr_ch->reset_iter == NULL);
             #  #  #  # ]
    2354   [ #  #  #  # ]:        258 :                 ctrlr_ch->reset_iter = i;
    2355                 :          0 :         } else {
    2356                 :        396 :                 nvme_ctrlr_for_each_channel_continue(i, 0);
    2357                 :            :         }
    2358                 :        654 : }
    2359                 :            : 
    2360                 :            : static void
    2361                 :        186 : bdev_nvme_reset_create_qpairs_done(struct nvme_ctrlr *nvme_ctrlr, void *ctx, int status)
    2362                 :            : {
    2363         [ +  - ]:        186 :         if (status == 0) {
    2364   [ -  +  -  +  :        186 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "qpairs were created after ctrlr reset.\n");
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2365                 :            : 
    2366                 :        186 :                 bdev_nvme_reset_ctrlr_complete(nvme_ctrlr, true);
    2367                 :          0 :         } 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                 :        186 : }
    2377                 :            : 
    2378                 :            : static int
    2379                 :      15610 : bdev_nvme_reset_check_qpair_connected(void *ctx)
    2380                 :            : {
    2381                 :      15610 :         struct nvme_ctrlr_channel *ctrlr_ch = ctx;
    2382   [ #  #  #  # ]:      15610 :         struct nvme_qpair *nvme_qpair = ctrlr_ch->qpair;
    2383                 :            :         struct spdk_nvme_qpair *qpair;
    2384                 :            : 
    2385   [ -  +  #  #  :      15610 :         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   [ #  #  #  # ]:      15610 :         qpair = nvme_qpair->qpair;
    2396   [ -  +  #  # ]:      15610 :         assert(qpair != NULL);
    2397                 :            : 
    2398         [ +  + ]:      15610 :         if (!spdk_nvme_qpair_is_connected(qpair)) {
    2399                 :      15362 :                 return SPDK_POLLER_BUSY;
    2400                 :            :         }
    2401                 :            : 
    2402   [ -  +  -  +  :        248 :         NVME_CTRLR_INFOLOG(nvme_qpair->ctrlr, "qpair %p:%u was connected.\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2403                 :            :                            qpair, spdk_nvme_qpair_get_id(qpair));
    2404                 :            : 
    2405         [ #  # ]:        248 :         spdk_poller_unregister(&ctrlr_ch->connect_poller);
    2406                 :            : 
    2407                 :            :         /* qpair was completed to connect. Move to the next ctrlr_channel */
    2408   [ #  #  #  # ]:        248 :         nvme_ctrlr_for_each_channel_continue(ctrlr_ch->reset_iter, 0);
    2409   [ #  #  #  # ]:        248 :         ctrlr_ch->reset_iter = NULL;
    2410                 :            : 
    2411   [ +  +  +  + ]:        248 :         if (!g_opts.disable_auto_failback) {
    2412                 :        197 :                 _bdev_nvme_clear_io_path_cache(nvme_qpair);
    2413                 :          0 :         }
    2414                 :            : 
    2415                 :        248 :         return SPDK_POLLER_BUSY;
    2416                 :          0 : }
    2417                 :            : 
    2418                 :            : static void
    2419                 :        248 : 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   [ #  #  #  # ]:        248 :         struct nvme_qpair *nvme_qpair = ctrlr_ch->qpair;
    2425                 :            :         struct spdk_nvme_qpair *qpair;
    2426                 :        248 :         int rc = 0;
    2427                 :            : 
    2428   [ +  -  #  #  :        248 :         if (nvme_qpair->qpair == NULL) {
                   #  # ]
    2429                 :        248 :                 rc = bdev_nvme_create_qpair(nvme_qpair);
    2430                 :          0 :         }
    2431         [ +  - ]:        248 :         if (rc == 0) {
    2432   [ #  #  #  # ]:        248 :                 ctrlr_ch->connect_poller = SPDK_POLLER_REGISTER(bdev_nvme_reset_check_qpair_connected,
    2433                 :            :                                            ctrlr_ch, 0);
    2434                 :            : 
    2435   [ #  #  #  # ]:        248 :                 qpair = nvme_qpair->qpair;
    2436                 :            : 
    2437   [ -  +  -  +  :        248 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "Start checking qpair %p:%u to be connected.\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2438                 :            :                                    qpair, spdk_nvme_qpair_get_id(qpair));
    2439                 :            : 
    2440                 :            :                 /* The current full reset sequence will move to the next
    2441                 :            :                  * ctrlr_channel after the qpair is actually connected.
    2442                 :            :                  */
    2443   [ -  +  #  #  :        248 :                 assert(ctrlr_ch->reset_iter == NULL);
             #  #  #  # ]
    2444   [ #  #  #  # ]:        248 :                 ctrlr_ch->reset_iter = i;
    2445                 :          0 :         } else {
    2446                 :          0 :                 nvme_ctrlr_for_each_channel_continue(i, rc);
    2447                 :            :         }
    2448                 :        248 : }
    2449                 :            : 
    2450                 :            : static void
    2451                 :        186 : nvme_ctrlr_check_namespaces(struct nvme_ctrlr *nvme_ctrlr)
    2452                 :            : {
    2453   [ #  #  #  # ]:        186 :         struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
    2454                 :            :         struct nvme_ns *nvme_ns;
    2455                 :            : 
    2456         [ #  # ]:        186 :         for (nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
    2457         [ +  + ]:        335 :              nvme_ns != NULL;
    2458                 :        149 :              nvme_ns = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns)) {
    2459   [ +  +  #  #  :        149 :                 if (!spdk_nvme_ctrlr_is_active_ns(ctrlr, nvme_ns->id)) {
                   #  # ]
    2460   [ -  +  -  +  :          3 :                         SPDK_DEBUGLOG(bdev_nvme, "NSID %u was removed during reset.\n", nvme_ns->id);
          #  #  #  #  #  
                      # ]
    2461                 :            :                         /* NS can be added again. Just nullify nvme_ns->ns. */
    2462   [ #  #  #  # ]:          3 :                         nvme_ns->ns = NULL;
    2463                 :          0 :                 }
    2464                 :          0 :         }
    2465                 :        186 : }
    2466                 :            : 
    2467                 :            : 
    2468                 :            : static int
    2469                 :     707507 : bdev_nvme_reconnect_ctrlr_poll(void *arg)
    2470                 :            : {
    2471                 :     707507 :         struct nvme_ctrlr *nvme_ctrlr = arg;
    2472                 :            :         struct spdk_nvme_transport_id *trid;
    2473                 :     707507 :         int rc = -ETIMEDOUT;
    2474                 :            : 
    2475         [ +  + ]:     707507 :         if (bdev_nvme_check_ctrlr_loss_timeout(nvme_ctrlr)) {
    2476                 :            :                 /* Mark the ctrlr as failed. The next call to
    2477                 :            :                  * spdk_nvme_ctrlr_reconnect_poll_async() will then
    2478                 :            :                  * do the necessary cleanup and return failure.
    2479                 :            :                  */
    2480   [ #  #  #  # ]:         29 :                 spdk_nvme_ctrlr_fail(nvme_ctrlr->ctrlr);
    2481                 :          0 :         }
    2482                 :            : 
    2483   [ #  #  #  # ]:     707507 :         rc = spdk_nvme_ctrlr_reconnect_poll_async(nvme_ctrlr->ctrlr);
    2484         [ +  + ]:     707507 :         if (rc == -EAGAIN) {
    2485                 :     706842 :                 return SPDK_POLLER_BUSY;
    2486                 :            :         }
    2487                 :            : 
    2488         [ #  # ]:        665 :         spdk_poller_unregister(&nvme_ctrlr->reset_detach_poller);
    2489         [ +  + ]:        665 :         if (rc == 0) {
    2490   [ #  #  #  #  :        186 :                 trid = &nvme_ctrlr->active_path_id->trid;
                   #  # ]
    2491                 :            : 
    2492   [ +  +  #  #  :        186 :                 if (spdk_nvme_trtype_is_fabrics(trid->trtype)) {
                   #  # ]
    2493   [ -  +  -  +  :        148 :                         NVME_CTRLR_INFOLOG(nvme_ctrlr, "ctrlr was connected to %s:%s. Create qpairs.\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2494                 :            :                                            trid->traddr, trid->trsvcid);
    2495                 :          0 :                 } else {
    2496   [ -  +  -  +  :         38 :                         NVME_CTRLR_INFOLOG(nvme_ctrlr, "ctrlr was connected. Create qpairs.\n");
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2497                 :            :                 }
    2498                 :            : 
    2499                 :        186 :                 nvme_ctrlr_check_namespaces(nvme_ctrlr);
    2500                 :            : 
    2501                 :            :                 /* Recreate all of the I/O queue pairs */
    2502                 :        186 :                 nvme_ctrlr_for_each_channel(nvme_ctrlr,
    2503                 :            :                                             bdev_nvme_reset_create_qpair,
    2504                 :            :                                             NULL,
    2505                 :            :                                             bdev_nvme_reset_create_qpairs_done);
    2506                 :          0 :         } else {
    2507   [ -  +  +  +  :        479 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "ctrlr could not be connected.\n");
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2508                 :            : 
    2509                 :        479 :                 bdev_nvme_reset_ctrlr_complete(nvme_ctrlr, false);
    2510                 :            :         }
    2511                 :        665 :         return SPDK_POLLER_BUSY;
    2512                 :          0 : }
    2513                 :            : 
    2514                 :            : static void
    2515                 :        665 : bdev_nvme_reconnect_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
    2516                 :            : {
    2517   [ -  +  +  +  :        665 :         NVME_CTRLR_INFOLOG(nvme_ctrlr, "Start reconnecting ctrlr.\n");
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2518                 :            : 
    2519   [ #  #  #  # ]:        665 :         spdk_nvme_ctrlr_reconnect_async(nvme_ctrlr->ctrlr);
    2520                 :            : 
    2521                 :        103 :         SPDK_DTRACE_PROBE1(bdev_nvme_ctrlr_reconnect, nvme_ctrlr->nbdev_ctrlr->name);
    2522   [ -  +  #  #  :        665 :         assert(nvme_ctrlr->reset_detach_poller == NULL);
             #  #  #  # ]
    2523   [ #  #  #  # ]:        665 :         nvme_ctrlr->reset_detach_poller = SPDK_POLLER_REGISTER(bdev_nvme_reconnect_ctrlr_poll,
    2524                 :            :                                           nvme_ctrlr, 0);
    2525                 :        665 : }
    2526                 :            : 
    2527                 :            : static void
    2528                 :        581 : bdev_nvme_reset_destroy_qpair_done(struct nvme_ctrlr *nvme_ctrlr, void *ctx, int status)
    2529                 :            : {
    2530                 :         67 :         SPDK_DTRACE_PROBE1(bdev_nvme_ctrlr_reset, nvme_ctrlr->nbdev_ctrlr->name);
    2531   [ -  +  #  # ]:        581 :         assert(status == 0);
    2532                 :            : 
    2533   [ -  +  +  +  :        581 :         NVME_CTRLR_INFOLOG(nvme_ctrlr, "qpairs were deleted.\n");
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2534                 :            : 
    2535   [ +  +  #  #  :        581 :         if (!spdk_nvme_ctrlr_is_fabrics(nvme_ctrlr->ctrlr)) {
                   #  # ]
    2536                 :         38 :                 bdev_nvme_reconnect_ctrlr(nvme_ctrlr);
    2537                 :          0 :         } else {
    2538                 :        543 :                 nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_reconnect_ctrlr);
    2539                 :            :         }
    2540                 :        581 : }
    2541                 :            : 
    2542                 :            : static void
    2543                 :        581 : bdev_nvme_reset_destroy_qpairs(struct nvme_ctrlr *nvme_ctrlr)
    2544                 :            : {
    2545   [ -  +  +  +  :        581 :         NVME_CTRLR_INFOLOG(nvme_ctrlr, "Delete qpairs for reset.\n");
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2546                 :            : 
    2547                 :        581 :         nvme_ctrlr_for_each_channel(nvme_ctrlr,
    2548                 :            :                                     bdev_nvme_reset_destroy_qpair,
    2549                 :            :                                     NULL,
    2550                 :            :                                     bdev_nvme_reset_destroy_qpair_done);
    2551                 :        581 : }
    2552                 :            : 
    2553                 :            : static void
    2554                 :          9 : bdev_nvme_reconnect_ctrlr_now(void *ctx)
    2555                 :            : {
    2556                 :          9 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    2557                 :            : 
    2558   [ -  +  #  #  :          9 :         assert(nvme_ctrlr->resetting == true);
                   #  # ]
    2559   [ -  +  #  #  :          9 :         assert(nvme_ctrlr->thread == spdk_get_thread());
             #  #  #  # ]
    2560                 :            : 
    2561         [ #  # ]:          9 :         spdk_poller_unregister(&nvme_ctrlr->reconnect_delay_timer);
    2562                 :            : 
    2563   [ #  #  #  # ]:          9 :         spdk_poller_resume(nvme_ctrlr->adminq_timer_poller);
    2564                 :            : 
    2565                 :          9 :         bdev_nvme_reconnect_ctrlr(nvme_ctrlr);
    2566                 :          9 : }
    2567                 :            : 
    2568                 :            : static void
    2569                 :        581 : _bdev_nvme_reset_ctrlr(void *ctx)
    2570                 :            : {
    2571                 :        581 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    2572                 :            : 
    2573   [ -  +  #  #  :        581 :         assert(nvme_ctrlr->resetting == true);
                   #  # ]
    2574   [ -  +  #  #  :        581 :         assert(nvme_ctrlr->thread == spdk_get_thread());
             #  #  #  # ]
    2575                 :            : 
    2576   [ +  +  #  #  :        581 :         if (!spdk_nvme_ctrlr_is_fabrics(nvme_ctrlr->ctrlr)) {
                   #  # ]
    2577                 :         38 :                 nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_reset_destroy_qpairs);
    2578                 :          0 :         } else {
    2579                 :        543 :                 bdev_nvme_reset_destroy_qpairs(nvme_ctrlr);
    2580                 :            :         }
    2581                 :        581 : }
    2582                 :            : 
    2583                 :            : static int
    2584                 :        200 : bdev_nvme_reset_ctrlr_unsafe(struct nvme_ctrlr *nvme_ctrlr, spdk_msg_fn *msg_fn)
    2585                 :            : {
    2586   [ +  +  #  # ]:        200 :         if (nvme_ctrlr->destruct) {
    2587                 :          9 :                 return -ENXIO;
    2588                 :            :         }
    2589                 :            : 
    2590   [ +  +  #  # ]:        191 :         if (nvme_ctrlr->resetting) {
    2591   [ +  -  #  #  :         42 :                 NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Unable to perform reset, already in progress.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2592                 :         42 :                 return -EBUSY;
    2593                 :            :         }
    2594                 :            : 
    2595   [ +  +  #  # ]:        149 :         if (nvme_ctrlr->disabled) {
    2596   [ +  -  #  #  :          3 :                 NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Unable to perform reset. Controller is disabled.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2597                 :          3 :                 return -EALREADY;
    2598                 :            :         }
    2599                 :            : 
    2600         [ #  # ]:        146 :         nvme_ctrlr->resetting = true;
    2601         [ #  # ]:        146 :         nvme_ctrlr->dont_retry = true;
    2602                 :            : 
    2603   [ +  +  #  # ]:        146 :         if (nvme_ctrlr->reconnect_is_delayed) {
    2604   [ -  +  -  +  :          3 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "Reconnect is already scheduled.\n");
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2605         [ #  # ]:          3 :                 *msg_fn = bdev_nvme_reconnect_ctrlr_now;
    2606         [ #  # ]:          3 :                 nvme_ctrlr->reconnect_is_delayed = false;
    2607                 :          0 :         } else {
    2608         [ #  # ]:        143 :                 *msg_fn = _bdev_nvme_reset_ctrlr;
    2609   [ -  +  #  #  :        143 :                 assert(nvme_ctrlr->reset_start_tsc == 0);
             #  #  #  # ]
    2610                 :            :         }
    2611                 :            : 
    2612   [ #  #  #  # ]:        146 :         nvme_ctrlr->reset_start_tsc = spdk_get_ticks();
    2613                 :            : 
    2614                 :        146 :         return 0;
    2615                 :          0 : }
    2616                 :            : 
    2617                 :            : static int
    2618                 :         81 : bdev_nvme_reset_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
    2619                 :            : {
    2620                 :         75 :         spdk_msg_fn msg_fn;
    2621                 :            :         int rc;
    2622                 :            : 
    2623   [ -  +  #  # ]:         81 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2624                 :         81 :         rc = bdev_nvme_reset_ctrlr_unsafe(nvme_ctrlr, &msg_fn);
    2625   [ -  +  #  # ]:         81 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2626                 :            : 
    2627         [ +  + ]:         81 :         if (rc == 0) {
    2628   [ #  #  #  # ]:         66 :                 spdk_thread_send_msg(nvme_ctrlr->thread, msg_fn, nvme_ctrlr);
    2629                 :          0 :         }
    2630                 :            : 
    2631                 :         81 :         return rc;
    2632                 :            : }
    2633                 :            : 
    2634                 :            : static int
    2635                 :          9 : bdev_nvme_enable_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
    2636                 :            : {
    2637   [ -  +  #  # ]:          9 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2638   [ -  +  #  # ]:          9 :         if (nvme_ctrlr->destruct) {
    2639   [ #  #  #  # ]:          0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2640                 :          0 :                 return -ENXIO;
    2641                 :            :         }
    2642                 :            : 
    2643   [ -  +  #  # ]:          9 :         if (nvme_ctrlr->resetting) {
    2644   [ #  #  #  # ]:          0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2645                 :          0 :                 return -EBUSY;
    2646                 :            :         }
    2647                 :            : 
    2648   [ +  +  #  # ]:          9 :         if (!nvme_ctrlr->disabled) {
    2649   [ -  +  #  # ]:          3 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2650                 :          3 :                 return -EALREADY;
    2651                 :            :         }
    2652                 :            : 
    2653         [ #  # ]:          6 :         nvme_ctrlr->disabled = false;
    2654         [ #  # ]:          6 :         nvme_ctrlr->resetting = true;
    2655                 :            : 
    2656   [ #  #  #  # ]:          6 :         nvme_ctrlr->reset_start_tsc = spdk_get_ticks();
    2657                 :            : 
    2658   [ -  +  #  # ]:          6 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2659                 :            : 
    2660   [ #  #  #  # ]:          6 :         spdk_thread_send_msg(nvme_ctrlr->thread, bdev_nvme_reconnect_ctrlr_now, nvme_ctrlr);
    2661                 :          6 :         return 0;
    2662                 :          0 : }
    2663                 :            : 
    2664                 :            : static void
    2665                 :          6 : bdev_nvme_disable_ctrlr_complete(struct nvme_ctrlr *nvme_ctrlr)
    2666                 :            : {
    2667   [ #  #  #  # ]:          6 :         bdev_nvme_ctrlr_op_cb ctrlr_op_cb_fn = nvme_ctrlr->ctrlr_op_cb_fn;
    2668   [ #  #  #  # ]:          6 :         void *ctrlr_op_cb_arg = nvme_ctrlr->ctrlr_op_cb_arg;
    2669                 :            :         enum bdev_nvme_op_after_reset op_after_disable;
    2670                 :            : 
    2671   [ -  +  #  #  :          6 :         assert(nvme_ctrlr->thread == spdk_get_thread());
             #  #  #  # ]
    2672                 :            : 
    2673   [ #  #  #  # ]:          6 :         nvme_ctrlr->ctrlr_op_cb_fn = NULL;
    2674   [ #  #  #  # ]:          6 :         nvme_ctrlr->ctrlr_op_cb_arg = NULL;
    2675                 :            : 
    2676   [ -  +  #  # ]:          6 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2677                 :            : 
    2678         [ #  # ]:          6 :         nvme_ctrlr->resetting = false;
    2679         [ #  # ]:          6 :         nvme_ctrlr->dont_retry = false;
    2680                 :            : 
    2681                 :          6 :         op_after_disable = bdev_nvme_check_op_after_reset(nvme_ctrlr, true);
    2682                 :            : 
    2683         [ #  # ]:          6 :         nvme_ctrlr->disabled = true;
    2684   [ #  #  #  # ]:          6 :         spdk_poller_pause(nvme_ctrlr->adminq_timer_poller);
    2685                 :            : 
    2686                 :            :         /* Make sure we clear any pending resets before returning. */
    2687                 :          6 :         bdev_nvme_complete_pending_resets(nvme_ctrlr, true);
    2688                 :            : 
    2689   [ -  +  #  # ]:          6 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2690                 :            : 
    2691         [ -  + ]:          6 :         if (ctrlr_op_cb_fn) {
    2692   [ #  #  #  # ]:          0 :                 ctrlr_op_cb_fn(ctrlr_op_cb_arg, 0);
    2693                 :          0 :         }
    2694                 :            : 
    2695         [ -  + ]:          6 :         switch (op_after_disable) {
    2696                 :          0 :         case OP_COMPLETE_PENDING_DESTRUCT:
    2697                 :          0 :                 nvme_ctrlr_unregister(nvme_ctrlr);
    2698                 :          0 :                 break;
    2699                 :          6 :         default:
    2700                 :          6 :                 break;
    2701                 :            :         }
    2702                 :          6 : }
    2703                 :            : 
    2704                 :            : static void
    2705                 :          3 : bdev_nvme_disable_destroy_qpairs_done(struct nvme_ctrlr *nvme_ctrlr, void *ctx, int status)
    2706                 :            : {
    2707   [ -  +  #  # ]:          3 :         assert(status == 0);
    2708                 :            : 
    2709   [ -  +  #  #  :          3 :         if (!spdk_nvme_ctrlr_is_fabrics(nvme_ctrlr->ctrlr)) {
                   #  # ]
    2710                 :          0 :                 bdev_nvme_disable_ctrlr_complete(nvme_ctrlr);
    2711                 :          0 :         } else {
    2712                 :          3 :                 nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_disable_ctrlr_complete);
    2713                 :            :         }
    2714                 :          3 : }
    2715                 :            : 
    2716                 :            : static void
    2717                 :          3 : bdev_nvme_disable_destroy_qpairs(struct nvme_ctrlr *nvme_ctrlr)
    2718                 :            : {
    2719                 :          3 :         nvme_ctrlr_for_each_channel(nvme_ctrlr,
    2720                 :            :                                     bdev_nvme_reset_destroy_qpair,
    2721                 :            :                                     NULL,
    2722                 :            :                                     bdev_nvme_disable_destroy_qpairs_done);
    2723                 :          3 : }
    2724                 :            : 
    2725                 :            : static void
    2726                 :          3 : _bdev_nvme_cancel_reconnect_and_disable_ctrlr(void *ctx)
    2727                 :            : {
    2728                 :          3 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    2729                 :            : 
    2730   [ -  +  #  #  :          3 :         assert(nvme_ctrlr->resetting == true);
                   #  # ]
    2731   [ -  +  #  #  :          3 :         assert(nvme_ctrlr->thread == spdk_get_thread());
             #  #  #  # ]
    2732                 :            : 
    2733         [ #  # ]:          3 :         spdk_poller_unregister(&nvme_ctrlr->reconnect_delay_timer);
    2734                 :            : 
    2735                 :          3 :         bdev_nvme_disable_ctrlr_complete(nvme_ctrlr);
    2736                 :          3 : }
    2737                 :            : 
    2738                 :            : static void
    2739                 :          3 : _bdev_nvme_disconnect_and_disable_ctrlr(void *ctx)
    2740                 :            : {
    2741                 :          3 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    2742                 :            : 
    2743   [ -  +  #  #  :          3 :         assert(nvme_ctrlr->resetting == true);
                   #  # ]
    2744   [ -  +  #  #  :          3 :         assert(nvme_ctrlr->thread == spdk_get_thread());
             #  #  #  # ]
    2745                 :            : 
    2746   [ -  +  #  #  :          3 :         if (!spdk_nvme_ctrlr_is_fabrics(nvme_ctrlr->ctrlr)) {
                   #  # ]
    2747                 :          0 :                 nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_disable_destroy_qpairs);
    2748                 :          0 :         } else {
    2749                 :          3 :                 bdev_nvme_disable_destroy_qpairs(nvme_ctrlr);
    2750                 :            :         }
    2751                 :          3 : }
    2752                 :            : 
    2753                 :            : static int
    2754                 :         15 : bdev_nvme_disable_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
    2755                 :            : {
    2756                 :            :         spdk_msg_fn msg_fn;
    2757                 :            : 
    2758   [ -  +  #  # ]:         15 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    2759   [ +  +  #  # ]:         15 :         if (nvme_ctrlr->destruct) {
    2760   [ -  +  #  # ]:          3 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2761                 :          3 :                 return -ENXIO;
    2762                 :            :         }
    2763                 :            : 
    2764   [ +  +  #  # ]:         12 :         if (nvme_ctrlr->resetting) {
    2765   [ -  +  #  # ]:          3 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2766                 :          3 :                 return -EBUSY;
    2767                 :            :         }
    2768                 :            : 
    2769   [ +  +  #  # ]:          9 :         if (nvme_ctrlr->disabled) {
    2770   [ -  +  #  # ]:          3 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2771                 :          3 :                 return -EALREADY;
    2772                 :            :         }
    2773                 :            : 
    2774         [ #  # ]:          6 :         nvme_ctrlr->resetting = true;
    2775         [ #  # ]:          6 :         nvme_ctrlr->dont_retry = true;
    2776                 :            : 
    2777   [ +  +  #  # ]:          6 :         if (nvme_ctrlr->reconnect_is_delayed) {
    2778                 :          3 :                 msg_fn = _bdev_nvme_cancel_reconnect_and_disable_ctrlr;
    2779         [ #  # ]:          3 :                 nvme_ctrlr->reconnect_is_delayed = false;
    2780                 :          0 :         } else {
    2781                 :          3 :                 msg_fn = _bdev_nvme_disconnect_and_disable_ctrlr;
    2782                 :            :         }
    2783                 :            : 
    2784   [ #  #  #  # ]:          6 :         nvme_ctrlr->reset_start_tsc = spdk_get_ticks();
    2785                 :            : 
    2786   [ -  +  #  # ]:          6 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    2787                 :            : 
    2788   [ #  #  #  # ]:          6 :         spdk_thread_send_msg(nvme_ctrlr->thread, msg_fn, nvme_ctrlr);
    2789                 :          6 :         return 0;
    2790                 :          0 : }
    2791                 :            : 
    2792                 :            : static int
    2793                 :         27 : nvme_ctrlr_op(struct nvme_ctrlr *nvme_ctrlr, enum nvme_ctrlr_op op,
    2794                 :            :               bdev_nvme_ctrlr_op_cb cb_fn, void *cb_arg)
    2795                 :            : {
    2796                 :            :         int rc;
    2797                 :            : 
    2798   [ +  -  -  + ]:         27 :         switch (op) {
    2799                 :         24 :         case NVME_CTRLR_OP_RESET:
    2800                 :         24 :                 rc = bdev_nvme_reset_ctrlr(nvme_ctrlr);
    2801                 :         24 :                 break;
    2802                 :          0 :         case NVME_CTRLR_OP_ENABLE:
    2803                 :          0 :                 rc = bdev_nvme_enable_ctrlr(nvme_ctrlr);
    2804                 :          0 :                 break;
    2805                 :          0 :         case NVME_CTRLR_OP_DISABLE:
    2806                 :          0 :                 rc = bdev_nvme_disable_ctrlr(nvme_ctrlr);
    2807                 :          0 :                 break;
    2808                 :          3 :         default:
    2809                 :          3 :                 rc = -EINVAL;
    2810                 :          3 :                 break;
    2811                 :            :         }
    2812                 :            : 
    2813         [ +  + ]:         27 :         if (rc == 0) {
    2814   [ -  +  #  #  :         18 :                 assert(nvme_ctrlr->ctrlr_op_cb_fn == NULL);
             #  #  #  # ]
    2815   [ -  +  #  #  :         18 :                 assert(nvme_ctrlr->ctrlr_op_cb_arg == NULL);
             #  #  #  # ]
    2816   [ #  #  #  # ]:         18 :                 nvme_ctrlr->ctrlr_op_cb_fn = cb_fn;
    2817   [ #  #  #  # ]:         18 :                 nvme_ctrlr->ctrlr_op_cb_arg = cb_arg;
    2818                 :          0 :         }
    2819                 :         27 :         return rc;
    2820                 :            : }
    2821                 :            : 
    2822                 :            : struct nvme_ctrlr_op_rpc_ctx {
    2823                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    2824                 :            :         struct spdk_thread *orig_thread;
    2825                 :            :         enum nvme_ctrlr_op op;
    2826                 :            :         int rc;
    2827                 :            :         bdev_nvme_ctrlr_op_cb cb_fn;
    2828                 :            :         void *cb_arg;
    2829                 :            : };
    2830                 :            : 
    2831                 :            : static void
    2832                 :         12 : _nvme_ctrlr_op_rpc_complete(void *_ctx)
    2833                 :            : {
    2834                 :         12 :         struct nvme_ctrlr_op_rpc_ctx *ctx = _ctx;
    2835                 :            : 
    2836   [ -  +  #  # ]:         12 :         assert(ctx != NULL);
    2837   [ -  +  #  #  :         12 :         assert(ctx->cb_fn != NULL);
             #  #  #  # ]
    2838                 :            : 
    2839   [ #  #  #  #  :         12 :         ctx->cb_fn(ctx->cb_arg, ctx->rc);
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    2840                 :            : 
    2841                 :         12 :         free(ctx);
    2842                 :         12 : }
    2843                 :            : 
    2844                 :            : static void
    2845                 :         12 : nvme_ctrlr_op_rpc_complete(void *cb_arg, int rc)
    2846                 :            : {
    2847                 :         12 :         struct nvme_ctrlr_op_rpc_ctx *ctx = cb_arg;
    2848                 :            : 
    2849   [ #  #  #  # ]:         12 :         ctx->rc = rc;
    2850                 :            : 
    2851   [ #  #  #  # ]:         12 :         spdk_thread_send_msg(ctx->orig_thread, _nvme_ctrlr_op_rpc_complete, ctx);
    2852                 :         12 : }
    2853                 :            : 
    2854                 :            : void
    2855                 :         12 : nvme_ctrlr_op_rpc(struct nvme_ctrlr *nvme_ctrlr, enum nvme_ctrlr_op op,
    2856                 :            :                   bdev_nvme_ctrlr_op_cb cb_fn, void *cb_arg)
    2857                 :            : {
    2858                 :            :         struct nvme_ctrlr_op_rpc_ctx *ctx;
    2859                 :            :         int rc;
    2860                 :            : 
    2861   [ -  +  #  # ]:         12 :         assert(cb_fn != NULL);
    2862                 :            : 
    2863                 :         12 :         ctx = calloc(1, sizeof(*ctx));
    2864         [ -  + ]:         12 :         if (ctx == NULL) {
    2865   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to allocate nvme_ctrlr_op_rpc_ctx.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    2866   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, -ENOMEM);
    2867                 :          0 :                 return;
    2868                 :            :         }
    2869                 :            : 
    2870   [ #  #  #  # ]:         12 :         ctx->orig_thread = spdk_get_thread();
    2871   [ #  #  #  # ]:         12 :         ctx->cb_fn = cb_fn;
    2872   [ #  #  #  # ]:         12 :         ctx->cb_arg = cb_arg;
    2873                 :            : 
    2874                 :         12 :         rc = nvme_ctrlr_op(nvme_ctrlr, op, nvme_ctrlr_op_rpc_complete, ctx);
    2875         [ +  + ]:         12 :         if (rc == 0) {
    2876                 :          3 :                 return;
    2877         [ -  + ]:          9 :         } else if (rc == -EALREADY) {
    2878                 :          0 :                 rc = 0;
    2879                 :          0 :         }
    2880                 :            : 
    2881                 :          9 :         nvme_ctrlr_op_rpc_complete(ctx, rc);
    2882                 :          0 : }
    2883                 :            : 
    2884                 :            : static void nvme_bdev_ctrlr_op_rpc_continue(void *cb_arg, int rc);
    2885                 :            : 
    2886                 :            : static void
    2887                 :         15 : _nvme_bdev_ctrlr_op_rpc_continue(void *_ctx)
    2888                 :            : {
    2889                 :         15 :         struct nvme_ctrlr_op_rpc_ctx *ctx = _ctx;
    2890                 :            :         struct nvme_ctrlr *prev_nvme_ctrlr, *next_nvme_ctrlr;
    2891                 :            :         int rc;
    2892                 :            : 
    2893   [ #  #  #  # ]:         15 :         prev_nvme_ctrlr = ctx->nvme_ctrlr;
    2894   [ #  #  #  # ]:         15 :         ctx->nvme_ctrlr = NULL;
    2895                 :            : 
    2896   [ -  +  #  #  :         15 :         if (ctx->rc != 0) {
                   #  # ]
    2897                 :          0 :                 goto complete;
    2898                 :            :         }
    2899                 :            : 
    2900   [ #  #  #  #  :         15 :         next_nvme_ctrlr = TAILQ_NEXT(prev_nvme_ctrlr, tailq);
                   #  # ]
    2901         [ +  + ]:         15 :         if (next_nvme_ctrlr == NULL) {
    2902                 :         12 :                 goto complete;
    2903                 :            :         }
    2904                 :            : 
    2905   [ #  #  #  # ]:          3 :         rc = nvme_ctrlr_op(next_nvme_ctrlr, ctx->op, nvme_bdev_ctrlr_op_rpc_continue, ctx);
    2906         [ +  - ]:          3 :         if (rc == 0) {
    2907   [ #  #  #  # ]:          3 :                 ctx->nvme_ctrlr = next_nvme_ctrlr;
    2908                 :          3 :                 return;
    2909         [ #  # ]:          0 :         } else if (rc == -EALREADY) {
    2910   [ #  #  #  # ]:          0 :                 ctx->nvme_ctrlr = next_nvme_ctrlr;
    2911                 :          0 :                 rc = 0;
    2912                 :          0 :         }
    2913                 :            : 
    2914   [ #  #  #  # ]:          0 :         ctx->rc = rc;
    2915                 :            : 
    2916                 :         12 : complete:
    2917   [ #  #  #  #  :         12 :         ctx->cb_fn(ctx->cb_arg, ctx->rc);
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    2918                 :         12 :         free(ctx);
    2919                 :          0 : }
    2920                 :            : 
    2921                 :            : static void
    2922                 :         15 : nvme_bdev_ctrlr_op_rpc_continue(void *cb_arg, int rc)
    2923                 :            : {
    2924                 :         15 :         struct nvme_ctrlr_op_rpc_ctx *ctx = cb_arg;
    2925                 :            : 
    2926   [ #  #  #  # ]:         15 :         ctx->rc = rc;
    2927                 :            : 
    2928   [ #  #  #  # ]:         15 :         spdk_thread_send_msg(ctx->orig_thread, _nvme_bdev_ctrlr_op_rpc_continue, ctx);
    2929                 :         15 : }
    2930                 :            : 
    2931                 :            : void
    2932                 :         12 : nvme_bdev_ctrlr_op_rpc(struct nvme_bdev_ctrlr *nbdev_ctrlr, enum nvme_ctrlr_op op,
    2933                 :            :                        bdev_nvme_ctrlr_op_cb cb_fn, void *cb_arg)
    2934                 :            : {
    2935                 :            :         struct nvme_ctrlr_op_rpc_ctx *ctx;
    2936                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    2937                 :            :         int rc;
    2938                 :            : 
    2939   [ -  +  #  # ]:         12 :         assert(cb_fn != NULL);
    2940                 :            : 
    2941                 :         12 :         ctx = calloc(1, sizeof(*ctx));
    2942         [ -  + ]:         12 :         if (ctx == NULL) {
    2943                 :          0 :                 SPDK_ERRLOG("Failed to allocate nvme_ctrlr_op_rpc_ctx.\n");
    2944   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, -ENOMEM);
    2945                 :          0 :                 return;
    2946                 :            :         }
    2947                 :            : 
    2948   [ #  #  #  # ]:         12 :         ctx->orig_thread = spdk_get_thread();
    2949   [ #  #  #  # ]:         12 :         ctx->op = op;
    2950   [ #  #  #  # ]:         12 :         ctx->cb_fn = cb_fn;
    2951   [ #  #  #  # ]:         12 :         ctx->cb_arg = cb_arg;
    2952                 :            : 
    2953   [ #  #  #  #  :         12 :         nvme_ctrlr = TAILQ_FIRST(&nbdev_ctrlr->ctrlrs);
                   #  # ]
    2954   [ -  +  #  # ]:         12 :         assert(nvme_ctrlr != NULL);
    2955                 :            : 
    2956                 :         12 :         rc = nvme_ctrlr_op(nvme_ctrlr, op, nvme_bdev_ctrlr_op_rpc_continue, ctx);
    2957         [ +  - ]:         12 :         if (rc == 0) {
    2958   [ #  #  #  # ]:         12 :                 ctx->nvme_ctrlr = nvme_ctrlr;
    2959                 :         12 :                 return;
    2960         [ #  # ]:          0 :         } else if (rc == -EALREADY) {
    2961   [ #  #  #  # ]:          0 :                 ctx->nvme_ctrlr = nvme_ctrlr;
    2962                 :          0 :                 rc = 0;
    2963                 :          0 :         }
    2964                 :            : 
    2965                 :          0 :         nvme_bdev_ctrlr_op_rpc_continue(ctx, rc);
    2966                 :          0 : }
    2967                 :            : 
    2968                 :            : static int _bdev_nvme_reset_io(struct nvme_io_path *io_path, struct nvme_bdev_io *bio);
    2969                 :            : 
    2970                 :            : static void
    2971                 :         89 : bdev_nvme_unfreeze_bdev_channel_done(struct nvme_bdev *nbdev, void *ctx, int status)
    2972                 :            : {
    2973                 :         89 :         struct nvme_bdev_io *bio = ctx;
    2974                 :            :         enum spdk_bdev_io_status io_status;
    2975                 :            : 
    2976   [ +  +  #  #  :         89 :         if (bio->cpl.cdw0 == 0) {
             #  #  #  # ]
    2977                 :         77 :                 io_status = SPDK_BDEV_IO_STATUS_SUCCESS;
    2978                 :          0 :         } else {
    2979                 :         12 :                 io_status = SPDK_BDEV_IO_STATUS_FAILED;
    2980                 :            :         }
    2981                 :            : 
    2982   [ -  +  -  +  :         89 :         NVME_BDEV_INFOLOG(nbdev, "reset_io %p completed, status:%d\n", bio, io_status);
          #  #  #  #  #  
                #  #  # ]
    2983                 :            : 
    2984                 :         89 :         __bdev_nvme_io_complete(spdk_bdev_io_from_ctx(bio), io_status, NULL);
    2985                 :         89 : }
    2986                 :            : 
    2987                 :            : static void
    2988                 :        141 : bdev_nvme_unfreeze_bdev_channel(struct nvme_bdev_channel_iter *i,
    2989                 :            :                                 struct nvme_bdev *nbdev,
    2990                 :            :                                 struct nvme_bdev_channel *nbdev_ch, void *ctx)
    2991                 :            : {
    2992                 :        141 :         bdev_nvme_abort_retry_ios(nbdev_ch);
    2993   [ #  #  #  # ]:        141 :         nbdev_ch->resetting = false;
    2994                 :            : 
    2995                 :        141 :         nvme_bdev_for_each_channel_continue(i, 0);
    2996                 :        141 : }
    2997                 :            : 
    2998                 :            : static void
    2999                 :         89 : bdev_nvme_reset_io_complete(struct nvme_bdev_io *bio)
    3000                 :            : {
    3001                 :         89 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    3002   [ #  #  #  #  :         89 :         struct nvme_bdev *nbdev = (struct nvme_bdev *)bdev_io->bdev->ctxt;
             #  #  #  # ]
    3003                 :            : 
    3004                 :            :         /* Abort all queued I/Os for retry. */
    3005                 :         89 :         nvme_bdev_for_each_channel(nbdev,
    3006                 :            :                                    bdev_nvme_unfreeze_bdev_channel,
    3007                 :          0 :                                    bio,
    3008                 :            :                                    bdev_nvme_unfreeze_bdev_channel_done);
    3009                 :         89 : }
    3010                 :            : 
    3011                 :            : static void
    3012                 :        119 : _bdev_nvme_reset_io_continue(void *ctx)
    3013                 :            : {
    3014                 :        119 :         struct nvme_bdev_io *bio = ctx;
    3015                 :            :         struct nvme_io_path *prev_io_path, *next_io_path;
    3016                 :            :         int rc;
    3017                 :            : 
    3018   [ #  #  #  # ]:        119 :         prev_io_path = bio->io_path;
    3019   [ #  #  #  # ]:        119 :         bio->io_path = NULL;
    3020                 :            : 
    3021   [ #  #  #  #  :        119 :         next_io_path = STAILQ_NEXT(prev_io_path, stailq);
                   #  # ]
    3022         [ +  + ]:        119 :         if (next_io_path == NULL) {
    3023                 :         89 :                 goto complete;
    3024                 :            :         }
    3025                 :            : 
    3026                 :         30 :         rc = _bdev_nvme_reset_io(next_io_path, bio);
    3027         [ +  - ]:         30 :         if (rc == 0) {
    3028                 :         30 :                 return;
    3029                 :            :         }
    3030                 :            : 
    3031                 :          0 : complete:
    3032                 :         89 :         bdev_nvme_reset_io_complete(bio);
    3033                 :          0 : }
    3034                 :            : 
    3035                 :            : static void
    3036                 :        119 : bdev_nvme_reset_io_continue(void *cb_arg, int rc)
    3037                 :            : {
    3038                 :        119 :         struct nvme_bdev_io *bio = cb_arg;
    3039                 :        119 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    3040   [ #  #  #  #  :        119 :         struct nvme_bdev *nbdev = (struct nvme_bdev *)bdev_io->bdev->ctxt;
             #  #  #  # ]
    3041                 :            : 
    3042   [ -  +  -  +  :        119 :         NVME_BDEV_INFOLOG(nbdev, "continue reset_io %p, rc:%d\n", bio, rc);
          #  #  #  #  #  
                #  #  # ]
    3043                 :            : 
    3044                 :            :         /* Reset status is initialized as "failed". Set to "success" once we have at least one
    3045                 :            :          * successfully reset nvme_ctrlr.
    3046                 :            :          */
    3047         [ +  + ]:        119 :         if (rc == 0) {
    3048   [ #  #  #  #  :         89 :                 bio->cpl.cdw0 = 0;
                   #  # ]
    3049                 :          0 :         }
    3050                 :            : 
    3051                 :        119 :         spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io), _bdev_nvme_reset_io_continue, bio);
    3052                 :        119 : }
    3053                 :            : 
    3054                 :            : static int
    3055                 :        119 : _bdev_nvme_reset_io(struct nvme_io_path *io_path, struct nvme_bdev_io *bio)
    3056                 :            : {
    3057                 :        119 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    3058   [ #  #  #  #  :        119 :         struct nvme_bdev *nbdev = (struct nvme_bdev *)bdev_io->bdev->ctxt;
             #  #  #  # ]
    3059   [ #  #  #  #  :        119 :         struct nvme_ctrlr *nvme_ctrlr = io_path->qpair->ctrlr;
             #  #  #  # ]
    3060                 :         99 :         spdk_msg_fn msg_fn;
    3061                 :            :         int rc;
    3062                 :            : 
    3063   [ -  +  #  #  :        119 :         assert(bio->io_path == NULL);
             #  #  #  # ]
    3064   [ #  #  #  # ]:        119 :         bio->io_path = io_path;
    3065                 :            : 
    3066   [ -  +  #  # ]:        119 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    3067                 :        119 :         rc = bdev_nvme_reset_ctrlr_unsafe(nvme_ctrlr, &msg_fn);
    3068         [ +  + ]:        119 :         if (rc == -EBUSY) {
    3069                 :            :                 /*
    3070                 :            :                  * Reset call is queued only if it is from the app framework. This is on purpose so that
    3071                 :            :                  * we don't interfere with the app framework reset strategy. i.e. we are deferring to the
    3072                 :            :                  * upper level. If they are in the middle of a reset, we won't try to schedule another one.
    3073                 :            :                  */
    3074   [ #  #  #  #  :         36 :                 TAILQ_INSERT_TAIL(&nvme_ctrlr->pending_resets, bio, retry_link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    3075                 :          0 :         }
    3076   [ -  +  #  # ]:        119 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    3077                 :            : 
    3078         [ +  + ]:        119 :         if (rc == 0) {
    3079   [ -  +  #  #  :         80 :                 assert(nvme_ctrlr->ctrlr_op_cb_fn == NULL);
             #  #  #  # ]
    3080   [ -  +  #  #  :         80 :                 assert(nvme_ctrlr->ctrlr_op_cb_arg == NULL);
             #  #  #  # ]
    3081   [ #  #  #  # ]:         80 :                 nvme_ctrlr->ctrlr_op_cb_fn = bdev_nvme_reset_io_continue;
    3082   [ #  #  #  # ]:         80 :                 nvme_ctrlr->ctrlr_op_cb_arg = bio;
    3083                 :            : 
    3084   [ #  #  #  # ]:         80 :                 spdk_thread_send_msg(nvme_ctrlr->thread, msg_fn, nvme_ctrlr);
    3085                 :            : 
    3086   [ -  +  -  +  :         80 :                 NVME_BDEV_INFOLOG(nbdev, "reset_io %p started resetting ctrlr [%s, %u].\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    3087                 :            :                                   bio, CTRLR_STRING(nvme_ctrlr), CTRLR_ID(nvme_ctrlr));
    3088         [ +  + ]:         39 :         } else if (rc == -EBUSY) {
    3089                 :         36 :                 rc = 0;
    3090                 :            : 
    3091   [ -  +  -  +  :         36 :                 NVME_BDEV_INFOLOG(nbdev, "reset_io %p was queued to ctrlr [%s, %u].\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    3092                 :            :                                   bio, CTRLR_STRING(nvme_ctrlr), CTRLR_ID(nvme_ctrlr));
    3093                 :          0 :         } else {
    3094   [ -  +  -  +  :          3 :                 NVME_BDEV_INFOLOG(nbdev, "reset_io %p could not reset ctrlr [%s, %u], rc:%d\n",
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    3095                 :            :                                   bio, CTRLR_STRING(nvme_ctrlr), CTRLR_ID(nvme_ctrlr), rc);
    3096                 :            :         }
    3097                 :            : 
    3098                 :        119 :         return rc;
    3099                 :            : }
    3100                 :            : 
    3101                 :            : static void
    3102                 :         89 : bdev_nvme_freeze_bdev_channel_done(struct nvme_bdev *nbdev, void *ctx, int status)
    3103                 :            : {
    3104                 :         89 :         struct nvme_bdev_io *bio = ctx;
    3105                 :         89 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    3106                 :            :         struct nvme_bdev_channel *nbdev_ch;
    3107                 :            :         struct nvme_io_path *io_path;
    3108                 :            :         int rc;
    3109                 :            : 
    3110                 :         89 :         nbdev_ch = spdk_io_channel_get_ctx(spdk_bdev_io_get_io_channel(bdev_io));
    3111                 :            : 
    3112                 :            :         /* Initialize with failed status. With multipath it is enough to have at least one successful
    3113                 :            :          * nvme_ctrlr reset. If there is none, reset status will remain failed.
    3114                 :            :          */
    3115   [ #  #  #  #  :         89 :         bio->cpl.cdw0 = 1;
                   #  # ]
    3116                 :            : 
    3117                 :            :         /* Reset all nvme_ctrlrs of a bdev controller sequentially. */
    3118   [ #  #  #  #  :         89 :         io_path = STAILQ_FIRST(&nbdev_ch->io_path_list);
                   #  # ]
    3119   [ -  +  #  # ]:         89 :         assert(io_path != NULL);
    3120                 :            : 
    3121                 :         89 :         rc = _bdev_nvme_reset_io(io_path, bio);
    3122         [ +  + ]:         89 :         if (rc != 0) {
    3123                 :            :                 /* If the current nvme_ctrlr is disabled, skip it and move to the next nvme_ctrlr. */
    3124         [ -  + ]:          3 :                 rc = (rc == -EALREADY) ? 0 : rc;
    3125                 :            : 
    3126                 :          3 :                 bdev_nvme_reset_io_continue(bio, rc);
    3127                 :          0 :         }
    3128                 :         89 : }
    3129                 :            : 
    3130                 :            : static void
    3131                 :        135 : bdev_nvme_freeze_bdev_channel(struct nvme_bdev_channel_iter *i,
    3132                 :            :                               struct nvme_bdev *nbdev,
    3133                 :            :                               struct nvme_bdev_channel *nbdev_ch, void *ctx)
    3134                 :            : {
    3135   [ #  #  #  # ]:        135 :         nbdev_ch->resetting = true;
    3136                 :            : 
    3137                 :        135 :         nvme_bdev_for_each_channel_continue(i, 0);
    3138                 :        135 : }
    3139                 :            : 
    3140                 :            : static void
    3141                 :         86 : bdev_nvme_reset_io(struct nvme_bdev *nbdev, struct nvme_bdev_io *bio)
    3142                 :            : {
    3143   [ -  +  -  +  :         86 :         NVME_BDEV_INFOLOG(nbdev, "reset_io %p started.\n", bio);
          #  #  #  #  #  
                #  #  # ]
    3144                 :            : 
    3145                 :         86 :         nvme_bdev_for_each_channel(nbdev,
    3146                 :            :                                    bdev_nvme_freeze_bdev_channel,
    3147                 :          0 :                                    bio,
    3148                 :            :                                    bdev_nvme_freeze_bdev_channel_done);
    3149                 :         86 : }
    3150                 :            : 
    3151                 :            : static int
    3152                 :        910 : bdev_nvme_failover_ctrlr_unsafe(struct nvme_ctrlr *nvme_ctrlr, bool remove)
    3153                 :            : {
    3154   [ +  +  #  # ]:        910 :         if (nvme_ctrlr->destruct) {
    3155                 :            :                 /* Don't bother resetting if the controller is in the process of being destructed. */
    3156                 :        413 :                 return -ENXIO;
    3157                 :            :         }
    3158                 :            : 
    3159   [ +  +  #  # ]:        497 :         if (nvme_ctrlr->resetting) {
    3160   [ +  +  #  # ]:         56 :                 if (!nvme_ctrlr->in_failover) {
    3161   [ +  -  #  #  :          9 :                         NVME_CTRLR_NOTICELOG(nvme_ctrlr,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    3162                 :            :                                              "Reset is already in progress. Defer failover until reset completes.\n");
    3163                 :            : 
    3164                 :            :                         /* Defer failover until reset completes. */
    3165         [ #  # ]:          9 :                         nvme_ctrlr->pending_failover = true;
    3166                 :          9 :                         return -EINPROGRESS;
    3167                 :            :                 } else {
    3168   [ +  -  #  #  :         47 :                         NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Unable to perform failover, already in progress.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    3169                 :         47 :                         return -EBUSY;
    3170                 :            :                 }
    3171                 :            :         }
    3172                 :            : 
    3173         [ #  # ]:        441 :         bdev_nvme_failover_trid(nvme_ctrlr, remove, true);
    3174                 :            : 
    3175   [ +  +  #  # ]:        441 :         if (nvme_ctrlr->reconnect_is_delayed) {
    3176   [ +  -  #  #  :          3 :                 NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Reconnect is already scheduled.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    3177                 :            : 
    3178                 :            :                 /* We rely on the next reconnect for the failover. */
    3179                 :          3 :                 return -EALREADY;
    3180                 :            :         }
    3181                 :            : 
    3182   [ -  +  #  # ]:        438 :         if (nvme_ctrlr->disabled) {
    3183   [ #  #  #  #  :          0 :                 NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Controller is disabled.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    3184                 :            : 
    3185                 :            :                 /* We rely on the enablement for the failover. */
    3186                 :          0 :                 return -EALREADY;
    3187                 :            :         }
    3188                 :            : 
    3189         [ #  # ]:        438 :         nvme_ctrlr->resetting = true;
    3190         [ #  # ]:        438 :         nvme_ctrlr->in_failover = true;
    3191                 :            : 
    3192   [ -  +  #  #  :        438 :         assert(nvme_ctrlr->reset_start_tsc == 0);
             #  #  #  # ]
    3193   [ #  #  #  # ]:        438 :         nvme_ctrlr->reset_start_tsc = spdk_get_ticks();
    3194                 :            : 
    3195                 :        438 :         return 0;
    3196                 :          0 : }
    3197                 :            : 
    3198                 :            : static int
    3199                 :        900 : bdev_nvme_failover_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
    3200                 :            : {
    3201                 :            :         int rc;
    3202                 :            : 
    3203   [ -  +  #  # ]:        900 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    3204                 :        900 :         rc = bdev_nvme_failover_ctrlr_unsafe(nvme_ctrlr, false);
    3205   [ -  +  #  # ]:        900 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    3206                 :            : 
    3207         [ +  + ]:        900 :         if (rc == 0) {
    3208   [ #  #  #  # ]:        431 :                 spdk_thread_send_msg(nvme_ctrlr->thread, _bdev_nvme_reset_ctrlr, nvme_ctrlr);
    3209         [ -  + ]:        469 :         } else if (rc == -EALREADY) {
    3210                 :          0 :                 rc = 0;
    3211                 :          0 :         }
    3212                 :            : 
    3213                 :        900 :         return rc;
    3214                 :            : }
    3215                 :            : 
    3216                 :            : static int bdev_nvme_unmap(struct nvme_bdev_io *bio, uint64_t offset_blocks,
    3217                 :            :                            uint64_t num_blocks);
    3218                 :            : 
    3219                 :            : static int bdev_nvme_write_zeroes(struct nvme_bdev_io *bio, uint64_t offset_blocks,
    3220                 :            :                                   uint64_t num_blocks);
    3221                 :            : 
    3222                 :            : static int bdev_nvme_copy(struct nvme_bdev_io *bio, uint64_t dst_offset_blocks,
    3223                 :            :                           uint64_t src_offset_blocks,
    3224                 :            :                           uint64_t num_blocks);
    3225                 :            : 
    3226                 :            : static void
    3227                 :    6026516 : bdev_nvme_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
    3228                 :            :                      bool success)
    3229                 :            : {
    3230         [ #  # ]:    6026516 :         struct nvme_bdev_io *bio = (struct nvme_bdev_io *)bdev_io->driver_ctx;
    3231                 :            :         int ret;
    3232                 :            : 
    3233   [ -  +  #  # ]:    6026516 :         if (!success) {
    3234                 :          0 :                 ret = -EINVAL;
    3235                 :          0 :                 goto exit;
    3236                 :            :         }
    3237                 :            : 
    3238   [ -  +  #  #  :    6026516 :         if (spdk_unlikely(!nvme_io_path_is_available(bio->io_path))) {
                   #  # ]
    3239                 :          0 :                 ret = -ENXIO;
    3240                 :          0 :                 goto exit;
    3241                 :            :         }
    3242                 :            : 
    3243                 :    6026516 :         ret = bdev_nvme_readv(bio,
    3244   [ #  #  #  #  :          0 :                               bdev_io->u.bdev.iovs,
             #  #  #  # ]
    3245   [ #  #  #  #  :          0 :                               bdev_io->u.bdev.iovcnt,
             #  #  #  # ]
    3246   [ #  #  #  #  :          0 :                               bdev_io->u.bdev.md_buf,
             #  #  #  # ]
    3247   [ #  #  #  #  :          0 :                               bdev_io->u.bdev.num_blocks,
             #  #  #  # ]
    3248   [ #  #  #  #  :          0 :                               bdev_io->u.bdev.offset_blocks,
             #  #  #  # ]
    3249   [ #  #  #  #  :          0 :                               bdev_io->u.bdev.dif_check_flags,
             #  #  #  # ]
    3250   [ #  #  #  #  :          0 :                               bdev_io->u.bdev.memory_domain,
             #  #  #  # ]
    3251   [ #  #  #  #  :          0 :                               bdev_io->u.bdev.memory_domain_ctx,
             #  #  #  # ]
    3252   [ #  #  #  #  :          0 :                               bdev_io->u.bdev.accel_sequence);
             #  #  #  # ]
    3253                 :            : 
    3254                 :    6026516 : exit:
    3255         [ +  + ]:    6026516 :         if (spdk_unlikely(ret != 0)) {
    3256                 :      81644 :                 bdev_nvme_io_complete(bio, ret);
    3257                 :          0 :         }
    3258                 :    6026516 : }
    3259                 :            : 
    3260                 :            : static inline void
    3261                 :   44897209 : _bdev_nvme_submit_request(struct nvme_bdev_channel *nbdev_ch, struct spdk_bdev_io *bdev_io)
    3262                 :            : {
    3263         [ +  - ]:   44897209 :         struct nvme_bdev_io *nbdev_io = (struct nvme_bdev_io *)bdev_io->driver_ctx;
    3264   [ +  -  +  - ]:   44897209 :         struct spdk_bdev *bdev = bdev_io->bdev;
    3265                 :            :         struct nvme_bdev_io *nbdev_io_to_abort;
    3266                 :   44897209 :         int rc = 0;
    3267                 :            : 
    3268   [ +  +  +  +  :   44897209 :         switch (bdev_io->type) {
          +  +  +  +  +  
          +  +  +  +  -  
          -  +  +  -  -  
                -  -  - ]
    3269                 :   21378366 :         case SPDK_BDEV_IO_TYPE_READ:
    3270   [ +  +  +  +  :   21378369 :                 if (bdev_io->u.bdev.iovs && bdev_io->u.bdev.iovs[0].iov_base) {
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   -  + ]
    3271                 :            : 
    3272                 :   15351856 :                         rc = bdev_nvme_readv(nbdev_io,
    3273   [ -  +  -  +  :          3 :                                              bdev_io->u.bdev.iovs,
             -  +  -  + ]
    3274   [ -  +  -  +  :          3 :                                              bdev_io->u.bdev.iovcnt,
             -  +  -  + ]
    3275   [ -  +  -  +  :          3 :                                              bdev_io->u.bdev.md_buf,
             -  +  -  + ]
    3276   [ -  +  -  +  :          3 :                                              bdev_io->u.bdev.num_blocks,
             -  +  -  + ]
    3277   [ -  +  -  +  :          3 :                                              bdev_io->u.bdev.offset_blocks,
             -  +  -  + ]
    3278   [ -  +  -  +  :          3 :                                              bdev_io->u.bdev.dif_check_flags,
             -  +  -  + ]
    3279   [ -  +  -  +  :          3 :                                              bdev_io->u.bdev.memory_domain,
             -  +  -  + ]
    3280   [ -  +  -  +  :          3 :                                              bdev_io->u.bdev.memory_domain_ctx,
             -  +  -  + ]
    3281   [ -  +  -  +  :          3 :                                              bdev_io->u.bdev.accel_sequence);
             -  +  -  + ]
    3282                 :          3 :                 } else {
    3283                 :    6026516 :                         spdk_bdev_io_get_buf(bdev_io, bdev_nvme_get_buf_cb,
    3284   [ #  #  #  #  :    6026516 :                                              bdev_io->u.bdev.num_blocks * bdev->blocklen);
          #  #  #  #  #  
                #  #  # ]
    3285                 :    6026516 :                         rc = 0;
    3286                 :            :                 }
    3287                 :   21378369 :                 break;
    3288                 :   20266596 :         case SPDK_BDEV_IO_TYPE_WRITE:
    3289                 :   20266596 :                 rc = bdev_nvme_writev(nbdev_io,
    3290   [ #  #  #  #  :          0 :                                       bdev_io->u.bdev.iovs,
             #  #  #  # ]
    3291   [ #  #  #  #  :          0 :                                       bdev_io->u.bdev.iovcnt,
             #  #  #  # ]
    3292   [ #  #  #  #  :          0 :                                       bdev_io->u.bdev.md_buf,
             #  #  #  # ]
    3293   [ #  #  #  #  :          0 :                                       bdev_io->u.bdev.num_blocks,
             #  #  #  # ]
    3294   [ #  #  #  #  :          0 :                                       bdev_io->u.bdev.offset_blocks,
             #  #  #  # ]
    3295   [ #  #  #  #  :          0 :                                       bdev_io->u.bdev.dif_check_flags,
             #  #  #  # ]
    3296   [ #  #  #  #  :          0 :                                       bdev_io->u.bdev.memory_domain,
             #  #  #  # ]
    3297   [ #  #  #  #  :          0 :                                       bdev_io->u.bdev.memory_domain_ctx,
             #  #  #  # ]
    3298   [ #  #  #  #  :          0 :                                       bdev_io->u.bdev.accel_sequence,
             #  #  #  # ]
    3299   [ #  #  #  #  :          0 :                                       bdev_io->u.bdev.nvme_cdw12,
                   #  # ]
    3300   [ #  #  #  #  :          0 :                                       bdev_io->u.bdev.nvme_cdw13);
                   #  # ]
    3301                 :   20266596 :                 break;
    3302                 :         53 :         case SPDK_BDEV_IO_TYPE_COMPARE:
    3303                 :         53 :                 rc = bdev_nvme_comparev(nbdev_io,
    3304   [ #  #  #  #  :          0 :                                         bdev_io->u.bdev.iovs,
             #  #  #  # ]
    3305   [ #  #  #  #  :          0 :                                         bdev_io->u.bdev.iovcnt,
             #  #  #  # ]
    3306   [ #  #  #  #  :          0 :                                         bdev_io->u.bdev.md_buf,
             #  #  #  # ]
    3307   [ #  #  #  #  :          0 :                                         bdev_io->u.bdev.num_blocks,
             #  #  #  # ]
    3308   [ #  #  #  #  :          0 :                                         bdev_io->u.bdev.offset_blocks,
             #  #  #  # ]
    3309   [ #  #  #  #  :          0 :                                         bdev_io->u.bdev.dif_check_flags);
             #  #  #  # ]
    3310                 :         53 :                 break;
    3311                 :         51 :         case SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE:
    3312                 :         51 :                 rc = bdev_nvme_comparev_and_writev(nbdev_io,
    3313   [ #  #  #  #  :          0 :                                                    bdev_io->u.bdev.iovs,
             #  #  #  # ]
    3314   [ #  #  #  #  :          0 :                                                    bdev_io->u.bdev.iovcnt,
             #  #  #  # ]
    3315   [ #  #  #  #  :          0 :                                                    bdev_io->u.bdev.fused_iovs,
             #  #  #  # ]
    3316   [ #  #  #  #  :          0 :                                                    bdev_io->u.bdev.fused_iovcnt,
             #  #  #  # ]
    3317   [ #  #  #  #  :          0 :                                                    bdev_io->u.bdev.md_buf,
             #  #  #  # ]
    3318   [ #  #  #  #  :          0 :                                                    bdev_io->u.bdev.num_blocks,
             #  #  #  # ]
    3319   [ #  #  #  #  :          0 :                                                    bdev_io->u.bdev.offset_blocks,
             #  #  #  # ]
    3320   [ #  #  #  #  :          0 :                                                    bdev_io->u.bdev.dif_check_flags);
             #  #  #  # ]
    3321                 :         51 :                 break;
    3322                 :     259832 :         case SPDK_BDEV_IO_TYPE_UNMAP:
    3323                 :     259832 :                 rc = bdev_nvme_unmap(nbdev_io,
    3324   [ #  #  #  #  :          0 :                                      bdev_io->u.bdev.offset_blocks,
             #  #  #  # ]
    3325   [ #  #  #  #  :          0 :                                      bdev_io->u.bdev.num_blocks);
             #  #  #  # ]
    3326                 :     259832 :                 break;
    3327                 :     689727 :         case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
    3328                 :     689727 :                 rc =  bdev_nvme_write_zeroes(nbdev_io,
    3329   [ #  #  #  #  :          0 :                                              bdev_io->u.bdev.offset_blocks,
             #  #  #  # ]
    3330   [ #  #  #  #  :          0 :                                              bdev_io->u.bdev.num_blocks);
             #  #  #  # ]
    3331                 :     689727 :                 break;
    3332                 :         86 :         case SPDK_BDEV_IO_TYPE_RESET:
    3333   [ #  #  #  # ]:         86 :                 nbdev_io->io_path = NULL;
    3334   [ #  #  #  # ]:         86 :                 bdev_nvme_reset_io(bdev->ctxt, nbdev_io);
    3335                 :         86 :                 return;
    3336                 :            : 
    3337                 :    2026622 :         case SPDK_BDEV_IO_TYPE_FLUSH:
    3338                 :    2026622 :                 bdev_nvme_io_complete(nbdev_io, 0);
    3339                 :    2026622 :                 return;
    3340                 :            : 
    3341                 :     268251 :         case SPDK_BDEV_IO_TYPE_ZONE_APPEND:
    3342                 :     268251 :                 rc = bdev_nvme_zone_appendv(nbdev_io,
    3343   [ #  #  #  #  :          0 :                                             bdev_io->u.bdev.iovs,
             #  #  #  # ]
    3344   [ #  #  #  #  :          0 :                                             bdev_io->u.bdev.iovcnt,
             #  #  #  # ]
    3345   [ #  #  #  #  :          0 :                                             bdev_io->u.bdev.md_buf,
             #  #  #  # ]
    3346   [ #  #  #  #  :          0 :                                             bdev_io->u.bdev.num_blocks,
             #  #  #  # ]
    3347   [ #  #  #  #  :          0 :                                             bdev_io->u.bdev.offset_blocks,
             #  #  #  # ]
    3348   [ #  #  #  #  :          0 :                                             bdev_io->u.bdev.dif_check_flags);
             #  #  #  # ]
    3349                 :     268251 :                 break;
    3350                 :          1 :         case SPDK_BDEV_IO_TYPE_GET_ZONE_INFO:
    3351                 :          1 :                 rc = bdev_nvme_get_zone_info(nbdev_io,
    3352   [ #  #  #  #  :          0 :                                              bdev_io->u.zone_mgmt.zone_id,
             #  #  #  # ]
    3353   [ #  #  #  #  :          0 :                                              bdev_io->u.zone_mgmt.num_zones,
             #  #  #  # ]
    3354   [ #  #  #  #  :          1 :                                              bdev_io->u.zone_mgmt.buf);
             #  #  #  # ]
    3355                 :          1 :                 break;
    3356                 :         46 :         case SPDK_BDEV_IO_TYPE_ZONE_MANAGEMENT:
    3357                 :         46 :                 rc = bdev_nvme_zone_management(nbdev_io,
    3358   [ #  #  #  #  :          0 :                                                bdev_io->u.zone_mgmt.zone_id,
             #  #  #  # ]
    3359   [ #  #  #  #  :          0 :                                                bdev_io->u.zone_mgmt.zone_action);
             #  #  #  # ]
    3360                 :         46 :                 break;
    3361                 :        138 :         case SPDK_BDEV_IO_TYPE_NVME_ADMIN:
    3362   [ #  #  #  # ]:        138 :                 nbdev_io->io_path = NULL;
    3363                 :        138 :                 bdev_nvme_admin_passthru(nbdev_ch,
    3364                 :          0 :                                          nbdev_io,
    3365   [ #  #  #  #  :          0 :                                          &bdev_io->u.nvme_passthru.cmd,
                   #  # ]
    3366   [ #  #  #  #  :          0 :                                          bdev_io->u.nvme_passthru.buf,
             #  #  #  # ]
    3367   [ #  #  #  #  :          0 :                                          bdev_io->u.nvme_passthru.nbytes);
             #  #  #  # ]
    3368                 :        138 :                 return;
    3369                 :            : 
    3370                 :        108 :         case SPDK_BDEV_IO_TYPE_NVME_IO:
    3371                 :        108 :                 rc = bdev_nvme_io_passthru(nbdev_io,
    3372   [ #  #  #  #  :          0 :                                            &bdev_io->u.nvme_passthru.cmd,
                   #  # ]
    3373   [ #  #  #  #  :          0 :                                            bdev_io->u.nvme_passthru.buf,
             #  #  #  # ]
    3374   [ #  #  #  #  :          0 :                                            bdev_io->u.nvme_passthru.nbytes);
             #  #  #  # ]
    3375                 :        108 :                 break;
    3376                 :          0 :         case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
    3377                 :          0 :                 rc = bdev_nvme_io_passthru_md(nbdev_io,
    3378   [ #  #  #  #  :          0 :                                               &bdev_io->u.nvme_passthru.cmd,
                   #  # ]
    3379   [ #  #  #  #  :          0 :                                               bdev_io->u.nvme_passthru.buf,
             #  #  #  # ]
    3380   [ #  #  #  #  :          0 :                                               bdev_io->u.nvme_passthru.nbytes,
             #  #  #  # ]
    3381   [ #  #  #  #  :          0 :                                               bdev_io->u.nvme_passthru.md_buf,
             #  #  #  # ]
    3382   [ #  #  #  #  :          0 :                                               bdev_io->u.nvme_passthru.md_len);
             #  #  #  # ]
    3383                 :          0 :                 break;
    3384                 :          0 :         case SPDK_BDEV_IO_TYPE_NVME_IOV_MD:
    3385                 :          0 :                 rc = bdev_nvme_iov_passthru_md(nbdev_io,
    3386   [ #  #  #  #  :          0 :                                                &bdev_io->u.nvme_passthru.cmd,
                   #  # ]
    3387   [ #  #  #  #  :          0 :                                                bdev_io->u.nvme_passthru.iovs,
             #  #  #  # ]
    3388   [ #  #  #  #  :          0 :                                                bdev_io->u.nvme_passthru.iovcnt,
             #  #  #  # ]
    3389   [ #  #  #  #  :          0 :                                                bdev_io->u.nvme_passthru.nbytes,
             #  #  #  # ]
    3390   [ #  #  #  #  :          0 :                                                bdev_io->u.nvme_passthru.md_buf,
             #  #  #  # ]
    3391   [ #  #  #  #  :          0 :                                                bdev_io->u.nvme_passthru.md_len);
             #  #  #  # ]
    3392                 :          0 :                 break;
    3393                 :       7293 :         case SPDK_BDEV_IO_TYPE_ABORT:
    3394   [ #  #  #  # ]:       7293 :                 nbdev_io->io_path = NULL;
    3395   [ #  #  #  #  :       7293 :                 nbdev_io_to_abort = (struct nvme_bdev_io *)bdev_io->u.abort.bio_to_abort->driver_ctx;
          #  #  #  #  #  
                      # ]
    3396                 :       7293 :                 bdev_nvme_abort(nbdev_ch,
    3397                 :          0 :                                 nbdev_io,
    3398                 :          0 :                                 nbdev_io_to_abort);
    3399                 :       7293 :                 return;
    3400                 :            : 
    3401                 :         36 :         case SPDK_BDEV_IO_TYPE_COPY:
    3402                 :         36 :                 rc = bdev_nvme_copy(nbdev_io,
    3403   [ #  #  #  #  :          0 :                                     bdev_io->u.bdev.offset_blocks,
             #  #  #  # ]
    3404   [ #  #  #  #  :          0 :                                     bdev_io->u.bdev.copy.src_offset_blocks,
          #  #  #  #  #  
                      # ]
    3405   [ #  #  #  #  :          0 :                                     bdev_io->u.bdev.num_blocks);
             #  #  #  # ]
    3406                 :         36 :                 break;
    3407                 :          0 :         default:
    3408                 :          0 :                 rc = -EINVAL;
    3409                 :          0 :                 break;
    3410                 :            :         }
    3411                 :            : 
    3412         [ +  + ]:   42863070 :         if (spdk_unlikely(rc != 0)) {
    3413                 :      88872 :                 bdev_nvme_io_complete(nbdev_io, rc);
    3414                 :          0 :         }
    3415                 :          3 : }
    3416                 :            : 
    3417                 :            : static void
    3418                 :   45302450 : bdev_nvme_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
    3419                 :            : {
    3420                 :   45302450 :         struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(ch);
    3421         [ +  - ]:   45302450 :         struct nvme_bdev_io *nbdev_io = (struct nvme_bdev_io *)bdev_io->driver_ctx;
    3422                 :            : 
    3423   [ +  +  +  -  :   45302450 :         if (spdk_likely(nbdev_io->submit_tsc == 0)) {
                   -  + ]
    3424   [ +  -  +  - ]:   45257660 :                 nbdev_io->submit_tsc = spdk_bdev_io_get_submit_tsc(bdev_io);
    3425                 :          3 :         } else {
    3426                 :            :                 /* There are cases where submit_tsc != 0, i.e. retry I/O.
    3427                 :            :                  * We need to update submit_tsc here.
    3428                 :            :                  */
    3429   [ #  #  #  # ]:      44790 :                 nbdev_io->submit_tsc = spdk_get_ticks();
    3430                 :            :         }
    3431                 :            : 
    3432   [ +  +  +  +  :   45302450 :         spdk_trace_record(TRACE_BDEV_NVME_IO_START, 0, 0, (uintptr_t)nbdev_io, (uintptr_t)bdev_io);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    3433   [ +  -  +  - ]:   45302450 :         nbdev_io->io_path = bdev_nvme_find_io_path(nbdev_ch);
    3434   [ +  +  +  -  :   45302450 :         if (spdk_unlikely(!nbdev_io->io_path)) {
                   +  - ]
    3435   [ +  +  #  #  :     408745 :                 if (!bdev_nvme_io_type_is_admin(bdev_io->type)) {
                   #  # ]
    3436                 :     408742 :                         bdev_nvme_io_complete(nbdev_io, -ENXIO);
    3437                 :     408742 :                         return;
    3438                 :            :                 }
    3439                 :            : 
    3440                 :            :                 /* Admin commands do not use the optimal I/O path.
    3441                 :            :                  * Simply fall through even if it is not found.
    3442                 :            :                  */
    3443                 :          0 :         }
    3444                 :            : 
    3445                 :   44893708 :         _bdev_nvme_submit_request(nbdev_ch, bdev_io);
    3446                 :          3 : }
    3447                 :            : 
    3448                 :            : static bool
    3449                 :    2688965 : bdev_nvme_is_supported_csi(enum spdk_nvme_csi csi)
    3450                 :            : {
    3451      [ +  +  + ]:    2688965 :         switch (csi) {
    3452                 :    2688932 :         case SPDK_NVME_CSI_NVM:
    3453                 :    2688936 :                 return true;
    3454                 :         29 :         case SPDK_NVME_CSI_ZNS:
    3455                 :         29 :                 return true;
    3456                 :          0 :         default:
    3457                 :          0 :                 return false;
    3458                 :            :         }
    3459                 :          4 : }
    3460                 :            : 
    3461                 :            : static bool
    3462                 :    2688965 : bdev_nvme_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
    3463                 :            : {
    3464                 :    2688965 :         struct nvme_bdev *nbdev = ctx;
    3465                 :            :         struct nvme_ns *nvme_ns;
    3466                 :            :         struct spdk_nvme_ns *ns;
    3467                 :            :         struct spdk_nvme_ctrlr *ctrlr;
    3468                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    3469                 :            : 
    3470   [ +  -  +  -  :    2688965 :         nvme_ns = TAILQ_FIRST(&nbdev->nvme_ns_list);
                   +  - ]
    3471   [ +  +  #  # ]:    2688965 :         assert(nvme_ns != NULL);
    3472   [ +  -  +  - ]:    2688965 :         ns = nvme_ns->ns;
    3473         [ +  + ]:    2688965 :         if (ns == NULL) {
    3474                 :          0 :                 return false;
    3475                 :            :         }
    3476                 :            : 
    3477         [ +  + ]:    2688965 :         if (!bdev_nvme_is_supported_csi(spdk_nvme_ns_get_csi(ns))) {
    3478      [ #  #  # ]:          0 :                 switch (io_type) {
    3479                 :          0 :                 case SPDK_BDEV_IO_TYPE_NVME_ADMIN:
    3480                 :            :                 case SPDK_BDEV_IO_TYPE_NVME_IO:
    3481                 :          0 :                         return true;
    3482                 :            : 
    3483                 :          0 :                 case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
    3484                 :          0 :                         return spdk_nvme_ns_get_md_size(ns) ? true : false;
    3485                 :            : 
    3486                 :          0 :                 default:
    3487                 :          0 :                         return false;
    3488                 :            :                 }
    3489                 :            :         }
    3490                 :            : 
    3491                 :    2688965 :         ctrlr = spdk_nvme_ns_get_ctrlr(ns);
    3492                 :            : 
    3493   [ +  +  +  +  :    2688965 :         switch (io_type) {
          +  +  +  +  +  
                      + ]
    3494                 :     223725 :         case SPDK_BDEV_IO_TYPE_READ:
    3495                 :            :         case SPDK_BDEV_IO_TYPE_WRITE:
    3496                 :            :         case SPDK_BDEV_IO_TYPE_RESET:
    3497                 :            :         case SPDK_BDEV_IO_TYPE_FLUSH:
    3498                 :            :         case SPDK_BDEV_IO_TYPE_NVME_ADMIN:
    3499                 :            :         case SPDK_BDEV_IO_TYPE_NVME_IO:
    3500                 :            :         case SPDK_BDEV_IO_TYPE_ABORT:
    3501                 :     223726 :                 return true;
    3502                 :            : 
    3503                 :       3815 :         case SPDK_BDEV_IO_TYPE_COMPARE:
    3504                 :       3815 :                 return spdk_nvme_ns_supports_compare(ns);
    3505                 :            : 
    3506                 :       1123 :         case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
    3507                 :       1123 :                 return spdk_nvme_ns_get_md_size(ns) ? true : false;
    3508                 :            : 
    3509                 :      85875 :         case SPDK_BDEV_IO_TYPE_UNMAP:
    3510                 :      85875 :                 cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    3511   [ #  #  #  # ]:      85875 :                 return cdata->oncs.dsm;
    3512                 :            : 
    3513                 :    2337541 :         case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
    3514                 :    2337542 :                 cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    3515   [ +  -  +  - ]:    2337542 :                 return cdata->oncs.write_zeroes;
    3516                 :            : 
    3517                 :       3813 :         case SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE:
    3518         [ +  + ]:       3813 :                 if (spdk_nvme_ctrlr_get_flags(ctrlr) &
    3519                 :            :                     SPDK_NVME_CTRLR_COMPARE_AND_WRITE_SUPPORTED) {
    3520                 :        238 :                         return true;
    3521                 :            :                 }
    3522                 :       3575 :                 return false;
    3523                 :            : 
    3524                 :       7478 :         case SPDK_BDEV_IO_TYPE_GET_ZONE_INFO:
    3525                 :            :         case SPDK_BDEV_IO_TYPE_ZONE_MANAGEMENT:
    3526                 :       7478 :                 return spdk_nvme_ns_get_csi(ns) == SPDK_NVME_CSI_ZNS;
    3527                 :            : 
    3528                 :       3740 :         case SPDK_BDEV_IO_TYPE_ZONE_APPEND:
    3529         [ +  + ]:       3742 :                 return spdk_nvme_ns_get_csi(ns) == SPDK_NVME_CSI_ZNS &&
    3530         [ +  - ]:          2 :                        spdk_nvme_ctrlr_get_flags(ctrlr) & SPDK_NVME_CTRLR_ZONE_APPEND_SUPPORTED;
    3531                 :            : 
    3532                 :       6860 :         case SPDK_BDEV_IO_TYPE_COPY:
    3533                 :       6862 :                 cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    3534   [ +  -  +  - ]:       6862 :                 return cdata->oncs.copy;
    3535                 :            : 
    3536                 :      14991 :         default:
    3537                 :      14991 :                 return false;
    3538                 :            :         }
    3539                 :          4 : }
    3540                 :            : 
    3541                 :            : static int
    3542                 :       2465 : nvme_qpair_create(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ctrlr_channel *ctrlr_ch)
    3543                 :            : {
    3544                 :            :         struct nvme_qpair *nvme_qpair;
    3545                 :            :         struct spdk_io_channel *pg_ch;
    3546                 :            :         int rc;
    3547                 :            : 
    3548                 :       2465 :         nvme_qpair = calloc(1, sizeof(*nvme_qpair));
    3549         [ +  + ]:       2465 :         if (!nvme_qpair) {
    3550   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to alloc nvme_qpair.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    3551                 :          0 :                 return -1;
    3552                 :            :         }
    3553                 :            : 
    3554   [ +  -  +  -  :       2465 :         TAILQ_INIT(&nvme_qpair->io_path_list);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    3555                 :            : 
    3556   [ +  -  +  - ]:       2465 :         nvme_qpair->ctrlr = nvme_ctrlr;
    3557   [ +  -  +  - ]:       2465 :         nvme_qpair->ctrlr_ch = ctrlr_ch;
    3558                 :            : 
    3559                 :       2465 :         pg_ch = spdk_get_io_channel(&g_nvme_bdev_ctrlrs);
    3560         [ +  + ]:       2465 :         if (!pg_ch) {
    3561                 :          0 :                 free(nvme_qpair);
    3562                 :          0 :                 return -1;
    3563                 :            :         }
    3564                 :            : 
    3565   [ +  -  +  - ]:       2465 :         nvme_qpair->group = spdk_io_channel_get_ctx(pg_ch);
    3566                 :            : 
    3567                 :            : #ifdef SPDK_CONFIG_VTUNE
    3568                 :            :         nvme_qpair->group->collect_spin_stat = true;
    3569                 :            : #else
    3570   [ +  -  +  -  :       2465 :         nvme_qpair->group->collect_spin_stat = false;
             +  -  +  - ]
    3571                 :            : #endif
    3572                 :            : 
    3573   [ +  -  -  + ]:       2465 :         if (!nvme_ctrlr->disabled) {
    3574                 :            :                 /* If a nvme_ctrlr is disabled, don't try to create qpair for it. Qpair will
    3575                 :            :                  * be created when it's enabled.
    3576                 :            :                  */
    3577                 :       2465 :                 rc = bdev_nvme_create_qpair(nvme_qpair);
    3578         [ +  + ]:       2465 :                 if (rc != 0) {
    3579                 :            :                         /* nvme_ctrlr can't create IO qpair if connection is down.
    3580                 :            :                          * If reconnect_delay_sec is non-zero, creating IO qpair is retried
    3581                 :            :                          * after reconnect_delay_sec seconds. If bdev_retry_count is non-zero,
    3582                 :            :                          * submitted IO will be queued until IO qpair is successfully created.
    3583                 :            :                          *
    3584                 :            :                          * Hence, if both are satisfied, ignore the failure.
    3585                 :            :                          */
    3586   [ #  #  #  #  :          0 :                         if (nvme_ctrlr->opts.reconnect_delay_sec == 0 || g_opts.bdev_retry_count == 0) {
          #  #  #  #  #  
                #  #  # ]
    3587                 :          0 :                                 spdk_put_io_channel(pg_ch);
    3588                 :          0 :                                 free(nvme_qpair);
    3589                 :          0 :                                 return rc;
    3590                 :            :                         }
    3591                 :          0 :                 }
    3592                 :          1 :         }
    3593                 :            : 
    3594   [ +  -  +  -  :       2465 :         TAILQ_INSERT_TAIL(&nvme_qpair->group->qpair_list, nvme_qpair, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
    3595                 :            : 
    3596   [ +  -  +  - ]:       2465 :         ctrlr_ch->qpair = nvme_qpair;
    3597                 :            : 
    3598                 :       2465 :         nvme_ctrlr_get_ref(nvme_ctrlr);
    3599                 :            : 
    3600                 :       2465 :         return 0;
    3601                 :          1 : }
    3602                 :            : 
    3603                 :            : static int
    3604                 :       2465 : bdev_nvme_create_ctrlr_channel_cb(void *io_device, void *ctx_buf)
    3605                 :            : {
    3606                 :       2465 :         struct nvme_ctrlr *nvme_ctrlr = io_device;
    3607                 :       2465 :         struct nvme_ctrlr_channel *ctrlr_ch = ctx_buf;
    3608                 :            : 
    3609                 :       2465 :         return nvme_qpair_create(nvme_ctrlr, ctrlr_ch);
    3610                 :            : }
    3611                 :            : 
    3612                 :            : static void
    3613                 :       2465 : nvme_qpair_delete(struct nvme_qpair *nvme_qpair)
    3614                 :            : {
    3615                 :            :         struct nvme_io_path *io_path, *next;
    3616                 :            : 
    3617   [ +  +  +  -  :       2465 :         assert(nvme_qpair->group != NULL);
             +  -  #  # ]
    3618                 :            : 
    3619   [ +  +  +  -  :       4990 :         TAILQ_FOREACH_SAFE(io_path, &nvme_qpair->io_path_list, tailq, next) {
          +  -  +  +  +  
          -  +  -  +  -  
                   +  + ]
    3620   [ +  +  +  -  :       2525 :                 TAILQ_REMOVE(&nvme_qpair->io_path_list, io_path, tailq);
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
    3621                 :       2525 :                 nvme_io_path_free(io_path);
    3622                 :          1 :         }
    3623                 :            : 
    3624   [ +  +  +  -  :       2465 :         TAILQ_REMOVE(&nvme_qpair->group->qpair_list, nvme_qpair, tailq);
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    3625                 :            : 
    3626   [ +  -  +  - ]:       2465 :         spdk_put_io_channel(spdk_io_channel_from_ctx(nvme_qpair->group));
    3627                 :            : 
    3628   [ +  -  +  - ]:       2465 :         nvme_ctrlr_put_ref(nvme_qpair->ctrlr);
    3629                 :            : 
    3630                 :       2465 :         free(nvme_qpair);
    3631                 :       2465 : }
    3632                 :            : 
    3633                 :            : static void
    3634                 :       2465 : bdev_nvme_destroy_ctrlr_channel_cb(void *io_device, void *ctx_buf)
    3635                 :            : {
    3636                 :       2465 :         struct nvme_ctrlr_channel *ctrlr_ch = ctx_buf;
    3637                 :            :         struct nvme_qpair *nvme_qpair;
    3638                 :            : 
    3639   [ +  -  +  - ]:       2465 :         nvme_qpair = ctrlr_ch->qpair;
    3640   [ +  +  #  # ]:       2465 :         assert(nvme_qpair != NULL);
    3641                 :            : 
    3642                 :       2465 :         _bdev_nvme_clear_io_path_cache(nvme_qpair);
    3643                 :            : 
    3644   [ +  +  +  -  :       2465 :         if (nvme_qpair->qpair != NULL) {
                   +  - ]
    3645   [ +  -  +  -  :       2394 :                 if (ctrlr_ch->reset_iter == NULL) {
                   -  + ]
    3646   [ +  -  +  - ]:       2394 :                         spdk_nvme_ctrlr_disconnect_io_qpair(nvme_qpair->qpair);
    3647                 :          1 :                 } else {
    3648                 :            :                         /* Skip current ctrlr_channel in a full reset sequence because
    3649                 :            :                          * it is being deleted now. The qpair is already being disconnected.
    3650                 :            :                          * We do not have to restart disconnecting it.
    3651                 :            :                          */
    3652   [ #  #  #  # ]:          0 :                         nvme_ctrlr_for_each_channel_continue(ctrlr_ch->reset_iter, 0);
    3653                 :            :                 }
    3654                 :            : 
    3655                 :            :                 /* We cannot release a reference to the poll group now.
    3656                 :            :                  * The qpair may be disconnected asynchronously later.
    3657                 :            :                  * We need to poll it until it is actually disconnected.
    3658                 :            :                  * Just detach the qpair from the deleting ctrlr_channel.
    3659                 :            :                  */
    3660   [ +  -  +  - ]:       2394 :                 nvme_qpair->ctrlr_ch = NULL;
    3661                 :          1 :         } else {
    3662   [ -  +  #  #  :         71 :                 assert(ctrlr_ch->reset_iter == NULL);
             #  #  #  # ]
    3663                 :            : 
    3664                 :         71 :                 nvme_qpair_delete(nvme_qpair);
    3665                 :            :         }
    3666                 :       2465 : }
    3667                 :            : 
    3668                 :            : static inline struct spdk_io_channel *
    3669                 :     822479 : bdev_nvme_get_accel_channel(struct nvme_poll_group *group)
    3670                 :            : {
    3671   [ +  +  #  #  :     822479 :         if (spdk_unlikely(!group->accel_channel)) {
                   #  # ]
    3672   [ #  #  #  # ]:         53 :                 group->accel_channel = spdk_accel_get_io_channel();
    3673   [ -  +  #  #  :         53 :                 if (!group->accel_channel) {
                   #  # ]
    3674                 :          0 :                         SPDK_ERRLOG("Cannot get the accel_channel for bdev nvme polling group=%p\n",
    3675                 :            :                                     group);
    3676                 :          0 :                         return NULL;
    3677                 :            :                 }
    3678                 :          0 :         }
    3679                 :            : 
    3680   [ #  #  #  # ]:     822479 :         return group->accel_channel;
    3681                 :          0 : }
    3682                 :            : 
    3683                 :            : static void
    3684                 :     822479 : bdev_nvme_finish_sequence(void *seq, spdk_nvme_accel_completion_cb cb_fn, void *cb_arg)
    3685                 :            : {
    3686                 :     822479 :         spdk_accel_sequence_finish(seq, cb_fn, cb_arg);
    3687                 :     822479 : }
    3688                 :            : 
    3689                 :            : static void
    3690                 :          0 : bdev_nvme_abort_sequence(void *seq)
    3691                 :            : {
    3692                 :          0 :         spdk_accel_sequence_abort(seq);
    3693                 :          0 : }
    3694                 :            : 
    3695                 :            : static void
    3696                 :     402979 : bdev_nvme_reverse_sequence(void *seq)
    3697                 :            : {
    3698                 :     402979 :         spdk_accel_sequence_reverse(seq);
    3699                 :     402979 : }
    3700                 :            : 
    3701                 :            : static int
    3702                 :     822479 : bdev_nvme_append_crc32c(void *ctx, void **seq, uint32_t *dst, struct iovec *iovs, uint32_t iovcnt,
    3703                 :            :                         struct spdk_memory_domain *domain, void *domain_ctx, uint32_t seed,
    3704                 :            :                         spdk_nvme_accel_step_cb cb_fn, void *cb_arg)
    3705                 :            : {
    3706                 :            :         struct spdk_io_channel *ch;
    3707                 :     822479 :         struct nvme_poll_group *group = ctx;
    3708                 :            : 
    3709                 :     822479 :         ch = bdev_nvme_get_accel_channel(group);
    3710         [ -  + ]:     822479 :         if (spdk_unlikely(ch == NULL)) {
    3711                 :          0 :                 return -ENOMEM;
    3712                 :            :         }
    3713                 :            : 
    3714                 :     822479 :         return spdk_accel_append_crc32c((struct spdk_accel_sequence **)seq, ch, dst, iovs, iovcnt,
    3715                 :          0 :                                         domain, domain_ctx, seed, cb_fn, cb_arg);
    3716                 :          0 : }
    3717                 :            : 
    3718                 :            : static int
    3719                 :          0 : bdev_nvme_append_copy(void *ctx, void **seq, struct iovec *dst_iovs, uint32_t dst_iovcnt,
    3720                 :            :                       struct spdk_memory_domain *dst_domain, void *dst_domain_ctx,
    3721                 :            :                       struct iovec *src_iovs, uint32_t src_iovcnt,
    3722                 :            :                       struct spdk_memory_domain *src_domain, void *src_domain_ctx,
    3723                 :            :                       spdk_nvme_accel_step_cb cb_fn, void *cb_arg)
    3724                 :            : {
    3725                 :            :         struct spdk_io_channel *ch;
    3726                 :          0 :         struct nvme_poll_group *group = ctx;
    3727                 :            : 
    3728                 :          0 :         ch = bdev_nvme_get_accel_channel(group);
    3729         [ #  # ]:          0 :         if (spdk_unlikely(ch == NULL)) {
    3730                 :          0 :                 return -ENOMEM;
    3731                 :            :         }
    3732                 :            : 
    3733                 :          0 :         return spdk_accel_append_copy((struct spdk_accel_sequence **)seq, ch,
    3734                 :          0 :                                       dst_iovs, dst_iovcnt, dst_domain, dst_domain_ctx,
    3735                 :          0 :                                       src_iovs, src_iovcnt, src_domain, src_domain_ctx,
    3736                 :          0 :                                       cb_fn, cb_arg);
    3737                 :          0 : }
    3738                 :            : 
    3739                 :            : static struct spdk_nvme_accel_fn_table g_bdev_nvme_accel_fn_table = {
    3740                 :            :         .table_size             = sizeof(struct spdk_nvme_accel_fn_table),
    3741                 :            :         .append_crc32c          = bdev_nvme_append_crc32c,
    3742                 :            :         .append_copy            = bdev_nvme_append_copy,
    3743                 :            :         .finish_sequence        = bdev_nvme_finish_sequence,
    3744                 :            :         .reverse_sequence       = bdev_nvme_reverse_sequence,
    3745                 :            :         .abort_sequence         = bdev_nvme_abort_sequence,
    3746                 :            : };
    3747                 :            : 
    3748                 :            : static int
    3749                 :     120537 : bdev_nvme_interrupt_wrapper(void *ctx)
    3750                 :            : {
    3751                 :            :         int num_events;
    3752                 :     120537 :         struct nvme_poll_group *group = ctx;
    3753                 :            : 
    3754   [ #  #  #  # ]:     120537 :         num_events = spdk_nvme_poll_group_wait(group->group, bdev_nvme_disconnected_qpair_cb);
    3755         [ -  + ]:     120537 :         if (spdk_unlikely(num_events < 0)) {
    3756                 :          0 :                 bdev_nvme_check_io_qpairs(group);
    3757                 :          0 :         }
    3758                 :            : 
    3759                 :     120537 :         return num_events;
    3760                 :            : }
    3761                 :            : 
    3762                 :            : static int
    3763                 :       2309 : bdev_nvme_create_poll_group_cb(void *io_device, void *ctx_buf)
    3764                 :            : {
    3765                 :       2309 :         struct nvme_poll_group *group = ctx_buf;
    3766                 :            :         uint64_t period;
    3767                 :            :         int fd;
    3768                 :            : 
    3769   [ +  -  +  -  :       2309 :         TAILQ_INIT(&group->qpair_list);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    3770                 :            : 
    3771   [ +  -  +  - ]:       2309 :         group->group = spdk_nvme_poll_group_create(group, &g_bdev_nvme_accel_fn_table);
    3772   [ +  +  +  -  :       2309 :         if (group->group == NULL) {
                   +  - ]
    3773                 :          0 :                 return -1;
    3774                 :            :         }
    3775                 :            : 
    3776   [ +  +  +  - ]:       2309 :         period = spdk_interrupt_mode_is_enabled() ? 0 : g_opts.nvme_ioq_poll_period_us;
    3777   [ +  -  +  - ]:       2309 :         group->poller = SPDK_POLLER_REGISTER(bdev_nvme_poll, group, period);
    3778                 :            : 
    3779   [ +  +  +  -  :       2309 :         if (group->poller == NULL) {
                   +  - ]
    3780   [ #  #  #  # ]:          0 :                 spdk_nvme_poll_group_destroy(group->group);
    3781                 :          0 :                 return -1;
    3782                 :            :         }
    3783                 :            : 
    3784         [ +  + ]:       2309 :         if (spdk_interrupt_mode_is_enabled()) {
    3785   [ #  #  #  # ]:          2 :                 spdk_poller_register_interrupt(group->poller, NULL, NULL);
    3786                 :            : 
    3787   [ #  #  #  # ]:          2 :                 fd = spdk_nvme_poll_group_get_fd(group->group);
    3788         [ -  + ]:          2 :                 if (fd < 0) {
    3789   [ #  #  #  # ]:          0 :                         spdk_nvme_poll_group_destroy(group->group);
    3790                 :          0 :                         return -1;
    3791                 :            :                 }
    3792                 :            : 
    3793   [ #  #  #  # ]:          2 :                 group->intr = SPDK_INTERRUPT_REGISTER(fd, bdev_nvme_interrupt_wrapper, group);
    3794   [ -  +  #  #  :          2 :                 if (!group->intr) {
                   #  # ]
    3795   [ #  #  #  # ]:          0 :                         spdk_nvme_poll_group_destroy(group->group);
    3796                 :          0 :                         return -1;
    3797                 :            :                 }
    3798                 :          0 :         }
    3799                 :            : 
    3800                 :       2309 :         return 0;
    3801                 :          1 : }
    3802                 :            : 
    3803                 :            : static void
    3804                 :       2309 : bdev_nvme_destroy_poll_group_cb(void *io_device, void *ctx_buf)
    3805                 :            : {
    3806                 :       2309 :         struct nvme_poll_group *group = ctx_buf;
    3807                 :            : 
    3808   [ +  +  +  -  :       2309 :         assert(TAILQ_EMPTY(&group->qpair_list));
          +  -  +  -  #  
                      # ]
    3809                 :            : 
    3810   [ +  +  +  -  :       2309 :         if (group->accel_channel) {
                   +  - ]
    3811   [ #  #  #  # ]:         53 :                 spdk_put_io_channel(group->accel_channel);
    3812                 :          0 :         }
    3813                 :            : 
    3814         [ +  + ]:       2309 :         if (spdk_interrupt_mode_is_enabled()) {
    3815         [ #  # ]:          2 :                 spdk_interrupt_unregister(&group->intr);
    3816                 :          0 :         }
    3817                 :            : 
    3818         [ +  - ]:       2309 :         spdk_poller_unregister(&group->poller);
    3819   [ +  +  +  -  :       2309 :         if (spdk_nvme_poll_group_destroy(group->group)) {
                   +  - ]
    3820                 :          0 :                 SPDK_ERRLOG("Unable to destroy a poll group for the NVMe bdev module.\n");
    3821         [ #  # ]:          0 :                 assert(false);
    3822                 :            :         }
    3823                 :       2309 : }
    3824                 :            : 
    3825                 :            : static struct spdk_io_channel *
    3826                 :       2412 : bdev_nvme_get_io_channel(void *ctx)
    3827                 :            : {
    3828                 :       2412 :         struct nvme_bdev *nvme_bdev = ctx;
    3829                 :            : 
    3830                 :       2412 :         return spdk_get_io_channel(nvme_bdev);
    3831                 :            : }
    3832                 :            : 
    3833                 :            : static void *
    3834                 :          0 : bdev_nvme_get_module_ctx(void *ctx)
    3835                 :            : {
    3836                 :          0 :         struct nvme_bdev *nvme_bdev = ctx;
    3837                 :            :         struct nvme_ns *nvme_ns;
    3838                 :            : 
    3839   [ #  #  #  #  :          0 :         if (!nvme_bdev || nvme_bdev->disk.module != &nvme_if) {
          #  #  #  #  #  
                      # ]
    3840                 :          0 :                 return NULL;
    3841                 :            :         }
    3842                 :            : 
    3843   [ #  #  #  #  :          0 :         nvme_ns = TAILQ_FIRST(&nvme_bdev->nvme_ns_list);
                   #  # ]
    3844         [ #  # ]:          0 :         if (!nvme_ns) {
    3845                 :          0 :                 return NULL;
    3846                 :            :         }
    3847                 :            : 
    3848   [ #  #  #  # ]:          0 :         return nvme_ns->ns;
    3849                 :          0 : }
    3850                 :            : 
    3851                 :            : static const char *
    3852                 :          0 : _nvme_ana_state_str(enum spdk_nvme_ana_state ana_state)
    3853                 :            : {
    3854   [ #  #  #  #  :          0 :         switch (ana_state) {
                   #  # ]
    3855                 :          0 :         case SPDK_NVME_ANA_OPTIMIZED_STATE:
    3856                 :          0 :                 return "optimized";
    3857                 :          0 :         case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
    3858                 :          0 :                 return "non_optimized";
    3859                 :          0 :         case SPDK_NVME_ANA_INACCESSIBLE_STATE:
    3860                 :          0 :                 return "inaccessible";
    3861                 :          0 :         case SPDK_NVME_ANA_PERSISTENT_LOSS_STATE:
    3862                 :          0 :                 return "persistent_loss";
    3863                 :          0 :         case SPDK_NVME_ANA_CHANGE_STATE:
    3864                 :          0 :                 return "change";
    3865                 :          0 :         default:
    3866                 :          0 :                 return NULL;
    3867                 :            :         }
    3868                 :          0 : }
    3869                 :            : 
    3870                 :            : static int
    3871                 :      10715 : bdev_nvme_get_memory_domains(void *ctx, struct spdk_memory_domain **domains, int array_size)
    3872                 :            : {
    3873                 :      10715 :         struct spdk_memory_domain **_domains = NULL;
    3874                 :      10715 :         struct nvme_bdev *nbdev = ctx;
    3875                 :            :         struct nvme_ns *nvme_ns;
    3876                 :      10715 :         int i = 0, _array_size = array_size;
    3877                 :      10715 :         int rc = 0;
    3878                 :            : 
    3879   [ +  +  +  -  :      21494 :         TAILQ_FOREACH(nvme_ns, &nbdev->nvme_ns_list, tailq) {
          +  -  +  +  +  
             -  +  -  +  
                      - ]
    3880   [ +  +  +  + ]:      10779 :                 if (domains && array_size >= i) {
    3881         [ #  # ]:        264 :                         _domains = &domains[i];
    3882                 :          0 :                 } else {
    3883                 :      10515 :                         _domains = NULL;
    3884                 :            :                 }
    3885   [ +  -  +  -  :      10779 :                 rc = spdk_nvme_ctrlr_get_memory_domains(nvme_ns->ctrlr->ctrlr, _domains, _array_size);
             +  -  +  - ]
    3886         [ +  + ]:      10779 :                 if (rc > 0) {
    3887         [ #  # ]:       4306 :                         i += rc;
    3888         [ +  + ]:       4306 :                         if (_array_size >= rc) {
    3889         [ #  # ]:        258 :                                 _array_size -= rc;
    3890                 :          0 :                         } else {
    3891                 :       4048 :                                 _array_size = 0;
    3892                 :            :                         }
    3893         [ +  + ]:       6473 :                 } else if (rc < 0) {
    3894                 :          0 :                         return rc;
    3895                 :            :                 }
    3896                 :          4 :         }
    3897                 :            : 
    3898                 :      10715 :         return i;
    3899                 :          4 : }
    3900                 :            : 
    3901                 :            : static const char *
    3902                 :        745 : nvme_ctrlr_get_state_str(struct nvme_ctrlr *nvme_ctrlr)
    3903                 :            : {
    3904   [ -  +  #  # ]:        745 :         if (nvme_ctrlr->destruct) {
    3905                 :          0 :                 return "deleting";
    3906   [ -  +  #  #  :        745 :         } else if (spdk_nvme_ctrlr_is_failed(nvme_ctrlr->ctrlr)) {
                   #  # ]
    3907                 :          0 :                 return "failed";
    3908   [ +  +  #  # ]:        745 :         } else if (nvme_ctrlr->resetting) {
    3909                 :         11 :                 return "resetting";
    3910   [ +  +  #  # ]:        734 :         } else if (nvme_ctrlr->reconnect_is_delayed > 0) {
    3911                 :          8 :                 return "reconnect_is_delayed";
    3912   [ -  +  #  # ]:        726 :         } else if (nvme_ctrlr->disabled) {
    3913                 :          0 :                 return "disabled";
    3914                 :            :         } else {
    3915                 :        726 :                 return "enabled";
    3916                 :            :         }
    3917                 :          0 : }
    3918                 :            : 
    3919                 :            : void
    3920                 :        745 : nvme_ctrlr_info_json(struct spdk_json_write_ctx *w, struct nvme_ctrlr *nvme_ctrlr)
    3921                 :        745 : {
    3922                 :            :         struct spdk_nvme_transport_id *trid;
    3923                 :            :         const struct spdk_nvme_ctrlr_opts *opts;
    3924                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    3925                 :            :         struct nvme_path_id *path_id;
    3926                 :            :         int32_t numa_id;
    3927                 :            : 
    3928                 :        745 :         spdk_json_write_object_begin(w);
    3929                 :            : 
    3930                 :        745 :         spdk_json_write_named_string(w, "state", nvme_ctrlr_get_state_str(nvme_ctrlr));
    3931                 :            : 
    3932                 :            : #ifdef SPDK_CONFIG_NVME_CUSE
    3933                 :        745 :         size_t cuse_name_size = 128;
    3934         [ -  + ]:        745 :         char cuse_name[cuse_name_size];
    3935                 :            : 
    3936   [ #  #  #  # ]:        745 :         int rc = spdk_nvme_cuse_get_ctrlr_name(nvme_ctrlr->ctrlr, cuse_name, &cuse_name_size);
    3937         [ +  + ]:        745 :         if (rc == 0) {
    3938                 :          3 :                 spdk_json_write_named_string(w, "cuse_device", cuse_name);
    3939                 :          0 :         }
    3940                 :            : #endif
    3941   [ #  #  #  #  :        745 :         trid = &nvme_ctrlr->active_path_id->trid;
                   #  # ]
    3942                 :        745 :         spdk_json_write_named_object_begin(w, "trid");
    3943                 :        745 :         nvme_bdev_dump_trid_json(trid, w);
    3944                 :        745 :         spdk_json_write_object_end(w);
    3945                 :            : 
    3946   [ #  #  #  #  :        745 :         path_id = TAILQ_NEXT(nvme_ctrlr->active_path_id, link);
          #  #  #  #  #  
                      # ]
    3947         [ +  + ]:        745 :         if (path_id != NULL) {
    3948                 :         12 :                 spdk_json_write_named_array_begin(w, "alternate_trids");
    3949                 :          0 :                 do {
    3950         [ #  # ]:         16 :                         trid = &path_id->trid;
    3951                 :         16 :                         spdk_json_write_object_begin(w);
    3952                 :         16 :                         nvme_bdev_dump_trid_json(trid, w);
    3953                 :         16 :                         spdk_json_write_object_end(w);
    3954                 :            : 
    3955   [ #  #  #  #  :         16 :                         path_id = TAILQ_NEXT(path_id, link);
                   #  # ]
    3956         [ +  + ]:         16 :                 } while (path_id != NULL);
    3957                 :         12 :                 spdk_json_write_array_end(w);
    3958                 :          0 :         }
    3959                 :            : 
    3960   [ #  #  #  # ]:        745 :         cdata = spdk_nvme_ctrlr_get_data(nvme_ctrlr->ctrlr);
    3961   [ #  #  #  # ]:        745 :         spdk_json_write_named_uint16(w, "cntlid", cdata->cntlid);
    3962                 :            : 
    3963   [ #  #  #  # ]:        745 :         opts = spdk_nvme_ctrlr_get_opts(nvme_ctrlr->ctrlr);
    3964                 :        745 :         spdk_json_write_named_object_begin(w, "host");
    3965         [ #  # ]:        745 :         spdk_json_write_named_string(w, "nqn", opts->hostnqn);
    3966         [ #  # ]:        745 :         spdk_json_write_named_string(w, "addr", opts->src_addr);
    3967         [ #  # ]:        745 :         spdk_json_write_named_string(w, "svcid", opts->src_svcid);
    3968                 :        745 :         spdk_json_write_object_end(w);
    3969                 :            : 
    3970   [ #  #  #  # ]:        745 :         numa_id = spdk_nvme_ctrlr_get_numa_id(nvme_ctrlr->ctrlr);
    3971         [ +  + ]:        745 :         if (numa_id != SPDK_ENV_NUMA_ID_ANY) {
    3972                 :        359 :                 spdk_json_write_named_uint32(w, "numa_id", numa_id);
    3973                 :          0 :         }
    3974                 :        745 :         spdk_json_write_object_end(w);
    3975                 :        745 : }
    3976                 :            : 
    3977                 :            : static void
    3978                 :       1131 : nvme_namespace_info_json(struct spdk_json_write_ctx *w,
    3979                 :            :                          struct nvme_ns *nvme_ns)
    3980                 :       1131 : {
    3981                 :            :         struct spdk_nvme_ns *ns;
    3982                 :            :         struct spdk_nvme_ctrlr *ctrlr;
    3983                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    3984                 :            :         const struct spdk_nvme_transport_id *trid;
    3985                 :            :         union spdk_nvme_vs_register vs;
    3986                 :            :         const struct spdk_nvme_ns_data *nsdata;
    3987                 :        898 :         char buf[128];
    3988                 :            : 
    3989   [ #  #  #  # ]:       1131 :         ns = nvme_ns->ns;
    3990         [ -  + ]:       1131 :         if (ns == NULL) {
    3991                 :          0 :                 return;
    3992                 :            :         }
    3993                 :            : 
    3994                 :       1131 :         ctrlr = spdk_nvme_ns_get_ctrlr(ns);
    3995                 :            : 
    3996                 :       1131 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    3997                 :       1131 :         trid = spdk_nvme_ctrlr_get_transport_id(ctrlr);
    3998                 :       1131 :         vs = spdk_nvme_ctrlr_get_regs_vs(ctrlr);
    3999                 :            : 
    4000                 :       1131 :         spdk_json_write_object_begin(w);
    4001                 :            : 
    4002   [ +  +  #  #  :       1131 :         if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
                   #  # ]
    4003         [ #  # ]:        903 :                 spdk_json_write_named_string(w, "pci_address", trid->traddr);
    4004                 :          0 :         }
    4005                 :            : 
    4006                 :       1131 :         spdk_json_write_named_object_begin(w, "trid");
    4007                 :            : 
    4008                 :       1131 :         nvme_bdev_dump_trid_json(trid, w);
    4009                 :            : 
    4010                 :       1131 :         spdk_json_write_object_end(w);
    4011                 :            : 
    4012                 :            : #ifdef SPDK_CONFIG_NVME_CUSE
    4013                 :       1131 :         size_t cuse_name_size = 128;
    4014         [ -  + ]:       1131 :         char cuse_name[cuse_name_size];
    4015                 :            : 
    4016                 :       1131 :         int rc = spdk_nvme_cuse_get_ns_name(ctrlr, spdk_nvme_ns_get_id(ns),
    4017                 :            :                                             cuse_name, &cuse_name_size);
    4018         [ +  + ]:       1131 :         if (rc == 0) {
    4019                 :          4 :                 spdk_json_write_named_string(w, "cuse_device", cuse_name);
    4020                 :          0 :         }
    4021                 :            : #endif
    4022                 :            : 
    4023                 :       1131 :         spdk_json_write_named_object_begin(w, "ctrlr_data");
    4024                 :            : 
    4025   [ #  #  #  # ]:       1131 :         spdk_json_write_named_uint16(w, "cntlid", cdata->cntlid);
    4026                 :            : 
    4027   [ #  #  #  # ]:       1131 :         spdk_json_write_named_string_fmt(w, "vendor_id", "0x%04x", cdata->vid);
    4028                 :            : 
    4029         [ -  + ]:       1131 :         snprintf(buf, sizeof(cdata->mn) + 1, "%s", cdata->mn);
    4030                 :       1131 :         spdk_str_trim(buf);
    4031                 :       1131 :         spdk_json_write_named_string(w, "model_number", buf);
    4032                 :            : 
    4033         [ -  + ]:       1131 :         snprintf(buf, sizeof(cdata->sn) + 1, "%s", cdata->sn);
    4034                 :       1131 :         spdk_str_trim(buf);
    4035                 :       1131 :         spdk_json_write_named_string(w, "serial_number", buf);
    4036                 :            : 
    4037         [ -  + ]:       1131 :         snprintf(buf, sizeof(cdata->fr) + 1, "%s", cdata->fr);
    4038                 :       1131 :         spdk_str_trim(buf);
    4039                 :       1131 :         spdk_json_write_named_string(w, "firmware_revision", buf);
    4040                 :            : 
    4041   [ +  +  #  #  :       1131 :         if (cdata->subnqn[0] != '\0') {
          #  #  #  #  #  
                      # ]
    4042         [ #  # ]:       1092 :                 spdk_json_write_named_string(w, "subnqn", cdata->subnqn);
    4043                 :          0 :         }
    4044                 :            : 
    4045                 :       1131 :         spdk_json_write_named_object_begin(w, "oacs");
    4046                 :            : 
    4047   [ #  #  #  # ]:       1131 :         spdk_json_write_named_uint32(w, "security", cdata->oacs.security);
    4048   [ #  #  #  # ]:       1131 :         spdk_json_write_named_uint32(w, "format", cdata->oacs.format);
    4049   [ #  #  #  # ]:       1131 :         spdk_json_write_named_uint32(w, "firmware", cdata->oacs.firmware);
    4050   [ #  #  #  # ]:       1131 :         spdk_json_write_named_uint32(w, "ns_manage", cdata->oacs.ns_manage);
    4051                 :            : 
    4052                 :       1131 :         spdk_json_write_object_end(w);
    4053                 :            : 
    4054   [ #  #  #  # ]:       1131 :         spdk_json_write_named_bool(w, "multi_ctrlr", cdata->cmic.multi_ctrlr);
    4055   [ #  #  #  # ]:       1131 :         spdk_json_write_named_bool(w, "ana_reporting", cdata->cmic.ana_reporting);
    4056                 :            : 
    4057                 :       1131 :         spdk_json_write_object_end(w);
    4058                 :            : 
    4059                 :       1131 :         spdk_json_write_named_object_begin(w, "vs");
    4060                 :            : 
    4061                 :       1131 :         spdk_json_write_name(w, "nvme_version");
    4062         [ -  + ]:       1131 :         if (vs.bits.ter) {
    4063                 :          0 :                 spdk_json_write_string_fmt(w, "%u.%u.%u", vs.bits.mjr, vs.bits.mnr, vs.bits.ter);
    4064                 :          0 :         } else {
    4065                 :       1131 :                 spdk_json_write_string_fmt(w, "%u.%u", vs.bits.mjr, vs.bits.mnr);
    4066                 :            :         }
    4067                 :            : 
    4068                 :       1131 :         spdk_json_write_object_end(w);
    4069                 :            : 
    4070                 :       1131 :         nsdata = spdk_nvme_ns_get_data(ns);
    4071                 :            : 
    4072                 :       1131 :         spdk_json_write_named_object_begin(w, "ns_data");
    4073                 :            : 
    4074                 :       1131 :         spdk_json_write_named_uint32(w, "id", spdk_nvme_ns_get_id(ns));
    4075                 :            : 
    4076   [ -  +  #  #  :       1131 :         if (cdata->cmic.ana_reporting) {
                   #  # ]
    4077                 :          0 :                 spdk_json_write_named_string(w, "ana_state",
    4078   [ #  #  #  # ]:          0 :                                              _nvme_ana_state_str(nvme_ns->ana_state));
    4079                 :          0 :         }
    4080                 :            : 
    4081   [ #  #  #  # ]:       1131 :         spdk_json_write_named_bool(w, "can_share", nsdata->nmic.can_share);
    4082                 :            : 
    4083                 :       1131 :         spdk_json_write_object_end(w);
    4084                 :            : 
    4085   [ +  +  #  #  :       1131 :         if (cdata->oacs.security) {
                   #  # ]
    4086                 :        714 :                 spdk_json_write_named_object_begin(w, "security");
    4087                 :            : 
    4088   [ -  +  #  #  :        714 :                 spdk_json_write_named_bool(w, "opal", nvme_ns->bdev->opal);
          #  #  #  #  #  
                      # ]
    4089                 :            : 
    4090                 :        714 :                 spdk_json_write_object_end(w);
    4091                 :          0 :         }
    4092                 :            : 
    4093                 :       1131 :         spdk_json_write_object_end(w);
    4094                 :          0 : }
    4095                 :            : 
    4096                 :            : static const char *
    4097                 :       1123 : nvme_bdev_get_mp_policy_str(struct nvme_bdev *nbdev)
    4098                 :            : {
    4099   [ +  -  -  #  :       1123 :         switch (nbdev->mp_policy) {
                #  #  # ]
    4100                 :       1123 :         case BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE:
    4101                 :       1123 :                 return "active_passive";
    4102                 :          0 :         case BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE:
    4103                 :          0 :                 return "active_active";
    4104                 :          0 :         default:
    4105         [ #  # ]:          0 :                 assert(false);
    4106                 :            :                 return "invalid";
    4107                 :            :         }
    4108                 :          0 : }
    4109                 :            : 
    4110                 :            : static const char *
    4111                 :          0 : nvme_bdev_get_mp_selector_str(struct nvme_bdev *nbdev)
    4112                 :            : {
    4113   [ #  #  #  #  :          0 :         switch (nbdev->mp_selector) {
                #  #  # ]
    4114                 :          0 :         case BDEV_NVME_MP_SELECTOR_ROUND_ROBIN:
    4115                 :          0 :                 return "round_robin";
    4116                 :          0 :         case BDEV_NVME_MP_SELECTOR_QUEUE_DEPTH:
    4117                 :          0 :                 return "queue_depth";
    4118                 :          0 :         default:
    4119         [ #  # ]:          0 :                 assert(false);
    4120                 :            :                 return "invalid";
    4121                 :            :         }
    4122                 :          0 : }
    4123                 :            : 
    4124                 :            : static int
    4125                 :       1123 : bdev_nvme_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
    4126                 :            : {
    4127                 :       1123 :         struct nvme_bdev *nvme_bdev = ctx;
    4128                 :            :         struct nvme_ns *nvme_ns;
    4129                 :            : 
    4130   [ -  +  #  # ]:       1123 :         pthread_mutex_lock(&nvme_bdev->mutex);
    4131                 :       1123 :         spdk_json_write_named_array_begin(w, "nvme");
    4132   [ +  +  #  #  :       2254 :         TAILQ_FOREACH(nvme_ns, &nvme_bdev->nvme_ns_list, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    4133                 :       1131 :                 nvme_namespace_info_json(w, nvme_ns);
    4134                 :          0 :         }
    4135                 :       1123 :         spdk_json_write_array_end(w);
    4136                 :       1123 :         spdk_json_write_named_string(w, "mp_policy", nvme_bdev_get_mp_policy_str(nvme_bdev));
    4137   [ -  +  #  #  :       1123 :         if (nvme_bdev->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE) {
                   #  # ]
    4138                 :          0 :                 spdk_json_write_named_string(w, "selector", nvme_bdev_get_mp_selector_str(nvme_bdev));
    4139   [ #  #  #  #  :          0 :                 if (nvme_bdev->mp_selector == BDEV_NVME_MP_SELECTOR_ROUND_ROBIN) {
                   #  # ]
    4140   [ #  #  #  # ]:          0 :                         spdk_json_write_named_uint32(w, "rr_min_io", nvme_bdev->rr_min_io);
    4141                 :          0 :                 }
    4142                 :          0 :         }
    4143   [ -  +  #  # ]:       1123 :         pthread_mutex_unlock(&nvme_bdev->mutex);
    4144                 :            : 
    4145                 :       1123 :         return 0;
    4146                 :            : }
    4147                 :            : 
    4148                 :            : static void
    4149                 :        166 : bdev_nvme_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
    4150                 :            : {
    4151                 :            :         /* No config per bdev needed */
    4152                 :        166 : }
    4153                 :            : 
    4154                 :            : static uint64_t
    4155                 :          0 : bdev_nvme_get_spin_time(struct spdk_io_channel *ch)
    4156                 :            : {
    4157                 :          0 :         struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(ch);
    4158                 :            :         struct nvme_io_path *io_path;
    4159                 :            :         struct nvme_poll_group *group;
    4160                 :          0 :         uint64_t spin_time = 0;
    4161                 :            : 
    4162   [ #  #  #  #  :          0 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    4163   [ #  #  #  #  :          0 :                 group = io_path->qpair->group;
             #  #  #  # ]
    4164                 :            : 
    4165   [ #  #  #  #  :          0 :                 if (!group || !group->collect_spin_stat) {
          #  #  #  #  #  
                      # ]
    4166                 :          0 :                         continue;
    4167                 :            :                 }
    4168                 :            : 
    4169   [ #  #  #  #  :          0 :                 if (group->end_ticks != 0) {
                   #  # ]
    4170   [ #  #  #  #  :          0 :                         group->spin_ticks += (group->end_ticks - group->start_ticks);
          #  #  #  #  #  
                #  #  # ]
    4171   [ #  #  #  # ]:          0 :                         group->end_ticks = 0;
    4172                 :          0 :                 }
    4173                 :            : 
    4174   [ #  #  #  # ]:          0 :                 spin_time += group->spin_ticks;
    4175   [ #  #  #  # ]:          0 :                 group->start_ticks = 0;
    4176   [ #  #  #  # ]:          0 :                 group->spin_ticks = 0;
    4177                 :          0 :         }
    4178                 :            : 
    4179         [ #  # ]:          0 :         return (spin_time * 1000000ULL) / spdk_get_ticks_hz();
    4180                 :            : }
    4181                 :            : 
    4182                 :            : static void
    4183                 :          0 : bdev_nvme_reset_device_stat(void *ctx)
    4184                 :            : {
    4185                 :          0 :         struct nvme_bdev *nbdev = ctx;
    4186                 :            : 
    4187   [ #  #  #  #  :          0 :         if (nbdev->err_stat != NULL) {
                   #  # ]
    4188   [ #  #  #  #  :          0 :                 memset(nbdev->err_stat, 0, sizeof(struct nvme_error_stat));
                   #  # ]
    4189                 :          0 :         }
    4190                 :          0 : }
    4191                 :            : 
    4192                 :            : /* JSON string should be lowercases and underscore delimited string. */
    4193                 :            : static void
    4194                 :         24 : bdev_nvme_format_nvme_status(char *dst, const char *src)
    4195                 :            : {
    4196                 :          0 :         char tmp[256];
    4197                 :            : 
    4198                 :         24 :         spdk_strcpy_replace(dst, 256, src, " - ", "_");
    4199                 :         24 :         spdk_strcpy_replace(tmp, 256, dst, "-", "_");
    4200                 :         24 :         spdk_strcpy_replace(dst, 256, tmp, " ", "_");
    4201                 :         24 :         spdk_strlwr(dst);
    4202                 :         24 : }
    4203                 :            : 
    4204                 :            : static void
    4205                 :         28 : bdev_nvme_dump_device_stat_json(void *ctx, struct spdk_json_write_ctx *w)
    4206                 :            : {
    4207                 :         28 :         struct nvme_bdev *nbdev = ctx;
    4208                 :         28 :         struct spdk_nvme_status status = {};
    4209                 :            :         uint16_t sct, sc;
    4210                 :          0 :         char status_json[256];
    4211                 :            :         const char *status_str;
    4212                 :            : 
    4213   [ +  +  #  #  :         28 :         if (nbdev->err_stat == NULL) {
                   #  # ]
    4214                 :         16 :                 return;
    4215                 :            :         }
    4216                 :            : 
    4217                 :         12 :         spdk_json_write_named_object_begin(w, "nvme_error");
    4218                 :            : 
    4219                 :         12 :         spdk_json_write_named_object_begin(w, "status_type");
    4220         [ +  + ]:        108 :         for (sct = 0; sct < 8; sct++) {
    4221   [ +  +  #  #  :         96 :                 if (nbdev->err_stat->status_type[sct] == 0) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    4222                 :         84 :                         continue;
    4223                 :            :                 }
    4224                 :         12 :                 status.sct = sct;
    4225                 :            : 
    4226                 :         12 :                 status_str = spdk_nvme_cpl_get_status_type_string(&status);
    4227   [ -  +  #  # ]:         12 :                 assert(status_str != NULL);
    4228                 :         12 :                 bdev_nvme_format_nvme_status(status_json, status_str);
    4229                 :            : 
    4230   [ #  #  #  #  :         12 :                 spdk_json_write_named_uint32(w, status_json, nbdev->err_stat->status_type[sct]);
          #  #  #  #  #  
                #  #  # ]
    4231                 :          0 :         }
    4232                 :         12 :         spdk_json_write_object_end(w);
    4233                 :            : 
    4234                 :         12 :         spdk_json_write_named_object_begin(w, "status_code");
    4235         [ +  + ]:         60 :         for (sct = 0; sct < 4; sct++) {
    4236                 :         48 :                 status.sct = sct;
    4237         [ +  + ]:      12336 :                 for (sc = 0; sc < 256; sc++) {
    4238   [ +  +  #  #  :      12288 :                         if (nbdev->err_stat->status[sct][sc] == 0) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    4239                 :      12276 :                                 continue;
    4240                 :            :                         }
    4241                 :         12 :                         status.sc = sc;
    4242                 :            : 
    4243                 :         12 :                         status_str = spdk_nvme_cpl_get_status_string(&status);
    4244   [ -  +  #  # ]:         12 :                         assert(status_str != NULL);
    4245                 :         12 :                         bdev_nvme_format_nvme_status(status_json, status_str);
    4246                 :            : 
    4247   [ #  #  #  #  :         12 :                         spdk_json_write_named_uint32(w, status_json, nbdev->err_stat->status[sct][sc]);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    4248                 :          0 :                 }
    4249                 :          0 :         }
    4250                 :         12 :         spdk_json_write_object_end(w);
    4251                 :            : 
    4252                 :         12 :         spdk_json_write_object_end(w);
    4253                 :          0 : }
    4254                 :            : 
    4255                 :            : static bool
    4256                 :     164430 : bdev_nvme_accel_sequence_supported(void *ctx, enum spdk_bdev_io_type type)
    4257                 :            : {
    4258                 :     164430 :         struct nvme_bdev *nbdev = ctx;
    4259                 :            :         struct nvme_ns *nvme_ns;
    4260                 :            :         struct spdk_nvme_ctrlr *ctrlr;
    4261                 :            : 
    4262   [ +  +  +  +  :     164430 :         if (!g_opts.allow_accel_sequence) {
                   -  + ]
    4263                 :     164346 :                 return false;
    4264                 :            :         }
    4265                 :            : 
    4266         [ +  + ]:         84 :         switch (type) {
    4267                 :          8 :         case SPDK_BDEV_IO_TYPE_WRITE:
    4268                 :            :         case SPDK_BDEV_IO_TYPE_READ:
    4269                 :          8 :                 break;
    4270                 :         76 :         default:
    4271                 :         76 :                 return false;
    4272                 :            :         }
    4273                 :            : 
    4274   [ #  #  #  #  :          8 :         nvme_ns = TAILQ_FIRST(&nbdev->nvme_ns_list);
                   #  # ]
    4275   [ -  +  #  # ]:          8 :         assert(nvme_ns != NULL);
    4276                 :            : 
    4277   [ #  #  #  #  :          8 :         ctrlr = nvme_ns->ctrlr->ctrlr;
             #  #  #  # ]
    4278   [ -  +  #  # ]:          8 :         assert(ctrlr != NULL);
    4279                 :            : 
    4280                 :          8 :         return spdk_nvme_ctrlr_get_flags(ctrlr) & SPDK_NVME_CTRLR_ACCEL_SEQUENCE_SUPPORTED;
    4281                 :         84 : }
    4282                 :            : 
    4283                 :            : static const struct spdk_bdev_fn_table nvmelib_fn_table = {
    4284                 :            :         .destruct                       = bdev_nvme_destruct,
    4285                 :            :         .submit_request                 = bdev_nvme_submit_request,
    4286                 :            :         .io_type_supported              = bdev_nvme_io_type_supported,
    4287                 :            :         .get_io_channel                 = bdev_nvme_get_io_channel,
    4288                 :            :         .dump_info_json                 = bdev_nvme_dump_info_json,
    4289                 :            :         .write_config_json              = bdev_nvme_write_config_json,
    4290                 :            :         .get_spin_time                  = bdev_nvme_get_spin_time,
    4291                 :            :         .get_module_ctx                 = bdev_nvme_get_module_ctx,
    4292                 :            :         .get_memory_domains             = bdev_nvme_get_memory_domains,
    4293                 :            :         .accel_sequence_supported       = bdev_nvme_accel_sequence_supported,
    4294                 :            :         .reset_device_stat              = bdev_nvme_reset_device_stat,
    4295                 :            :         .dump_device_stat_json          = bdev_nvme_dump_device_stat_json,
    4296                 :            : };
    4297                 :            : 
    4298                 :            : typedef int (*bdev_nvme_parse_ana_log_page_cb)(
    4299                 :            :         const struct spdk_nvme_ana_group_descriptor *desc, void *cb_arg);
    4300                 :            : 
    4301                 :            : static int
    4302                 :        552 : bdev_nvme_parse_ana_log_page(struct nvme_ctrlr *nvme_ctrlr,
    4303                 :            :                              bdev_nvme_parse_ana_log_page_cb cb_fn, void *cb_arg)
    4304                 :            : {
    4305                 :            :         struct spdk_nvme_ana_group_descriptor *copied_desc;
    4306                 :            :         uint8_t *orig_desc;
    4307                 :            :         uint32_t i, desc_size, copy_len;
    4308                 :        552 :         int rc = 0;
    4309                 :            : 
    4310   [ -  +  #  #  :        552 :         if (nvme_ctrlr->ana_log_page == NULL) {
                   #  # ]
    4311                 :          0 :                 return -EINVAL;
    4312                 :            :         }
    4313                 :            : 
    4314   [ #  #  #  # ]:        552 :         copied_desc = nvme_ctrlr->copied_ana_desc;
    4315                 :            : 
    4316   [ #  #  #  #  :        552 :         orig_desc = (uint8_t *)nvme_ctrlr->ana_log_page + sizeof(struct spdk_nvme_ana_page);
                   #  # ]
    4317   [ #  #  #  # ]:        552 :         copy_len = nvme_ctrlr->max_ana_log_page_size - sizeof(struct spdk_nvme_ana_page);
    4318                 :            : 
    4319   [ +  +  #  #  :        744 :         for (i = 0; i < nvme_ctrlr->ana_log_page->num_ana_group_desc; i++) {
          #  #  #  #  #  
                      # ]
    4320   [ -  +  -  + ]:        627 :                 memcpy(copied_desc, orig_desc, copy_len);
    4321                 :            : 
    4322   [ #  #  #  # ]:        627 :                 rc = cb_fn(copied_desc, cb_arg);
    4323         [ +  + ]:        627 :                 if (rc != 0) {
    4324                 :        435 :                         break;
    4325                 :            :                 }
    4326                 :            : 
    4327                 :        192 :                 desc_size = sizeof(struct spdk_nvme_ana_group_descriptor) +
    4328   [ #  #  #  # ]:        192 :                             copied_desc->num_of_nsid * sizeof(uint32_t);
    4329         [ #  # ]:        192 :                 orig_desc += desc_size;
    4330                 :        192 :                 copy_len -= desc_size;
    4331                 :          0 :         }
    4332                 :            : 
    4333                 :        552 :         return rc;
    4334                 :          0 : }
    4335                 :            : 
    4336                 :            : static int
    4337                 :         17 : nvme_ns_ana_transition_timedout(void *ctx)
    4338                 :            : {
    4339                 :         17 :         struct nvme_ns *nvme_ns = ctx;
    4340                 :            : 
    4341         [ #  # ]:         17 :         spdk_poller_unregister(&nvme_ns->anatt_timer);
    4342   [ #  #  #  # ]:         17 :         nvme_ns->ana_transition_timedout = true;
    4343                 :            : 
    4344                 :         17 :         return SPDK_POLLER_BUSY;
    4345                 :            : }
    4346                 :            : 
    4347                 :            : static void
    4348                 :        564 : _nvme_ns_set_ana_state(struct nvme_ns *nvme_ns,
    4349                 :            :                        const struct spdk_nvme_ana_group_descriptor *desc)
    4350                 :            : {
    4351                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    4352                 :            : 
    4353   [ #  #  #  #  :        564 :         nvme_ns->ana_group_id = desc->ana_group_id;
             #  #  #  # ]
    4354   [ #  #  #  #  :        564 :         nvme_ns->ana_state = desc->ana_state;
                   #  # ]
    4355   [ #  #  #  # ]:        564 :         nvme_ns->ana_state_updating = false;
    4356                 :            : 
    4357   [ +  +  +  #  :        564 :         switch (nvme_ns->ana_state) {
                #  #  # ]
    4358                 :        513 :         case SPDK_NVME_ANA_OPTIMIZED_STATE:
    4359                 :            :         case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
    4360   [ #  #  #  # ]:        513 :                 nvme_ns->ana_transition_timedout = false;
    4361         [ #  # ]:        513 :                 spdk_poller_unregister(&nvme_ns->anatt_timer);
    4362                 :        513 :                 break;
    4363                 :            : 
    4364                 :         48 :         case SPDK_NVME_ANA_INACCESSIBLE_STATE:
    4365                 :            :         case SPDK_NVME_ANA_CHANGE_STATE:
    4366   [ +  +  #  #  :         48 :                 if (nvme_ns->anatt_timer != NULL) {
                   #  # ]
    4367                 :         15 :                         break;
    4368                 :            :                 }
    4369                 :            : 
    4370   [ #  #  #  #  :         33 :                 cdata = spdk_nvme_ctrlr_get_data(nvme_ns->ctrlr->ctrlr);
             #  #  #  # ]
    4371   [ #  #  #  #  :         33 :                 nvme_ns->anatt_timer = SPDK_POLLER_REGISTER(nvme_ns_ana_transition_timedout,
             #  #  #  # ]
    4372                 :            :                                        nvme_ns,
    4373                 :            :                                        cdata->anatt * SPDK_SEC_TO_USEC);
    4374                 :         33 :                 break;
    4375                 :          3 :         default:
    4376                 :          3 :                 break;
    4377                 :            :         }
    4378                 :        564 : }
    4379                 :            : 
    4380                 :            : static int
    4381                 :        504 : nvme_ns_set_ana_state(const struct spdk_nvme_ana_group_descriptor *desc, void *cb_arg)
    4382                 :            : {
    4383                 :        504 :         struct nvme_ns *nvme_ns = cb_arg;
    4384                 :            :         uint32_t i;
    4385                 :            : 
    4386   [ -  +  #  #  :        504 :         assert(nvme_ns->ns != NULL);
             #  #  #  # ]
    4387                 :            : 
    4388   [ +  +  #  #  :        570 :         for (i = 0; i < desc->num_of_nsid; i++) {
                   #  # ]
    4389   [ +  +  #  #  :        501 :                 if (desc->nsid[i] != spdk_nvme_ns_get_id(nvme_ns->ns)) {
          #  #  #  #  #  
                #  #  # ]
    4390                 :         66 :                         continue;
    4391                 :            :                 }
    4392                 :            : 
    4393                 :        435 :                 _nvme_ns_set_ana_state(nvme_ns, desc);
    4394                 :        435 :                 return 1;
    4395                 :            :         }
    4396                 :            : 
    4397                 :         69 :         return 0;
    4398                 :          0 : }
    4399                 :            : 
    4400                 :            : static int
    4401                 :         15 : nvme_generate_uuid(const char *sn, uint32_t nsid, struct spdk_uuid *uuid)
    4402                 :            : {
    4403                 :         15 :         int rc = 0;
    4404                 :         15 :         struct spdk_uuid new_uuid, namespace_uuid;
    4405                 :         15 :         char merged_str[SPDK_NVME_CTRLR_SN_LEN + NSID_STR_LEN + 1] = {'\0'};
    4406                 :            :         /* This namespace UUID was generated using uuid_generate() method. */
    4407                 :         15 :         const char *namespace_str = {"edaed2de-24bc-4b07-b559-f47ecbe730fd"};
    4408                 :            :         int size;
    4409                 :            : 
    4410   [ -  +  -  +  :         15 :         assert(strlen(sn) <= SPDK_NVME_CTRLR_SN_LEN);
                   #  # ]
    4411                 :            : 
    4412                 :         15 :         spdk_uuid_set_null(&new_uuid);
    4413                 :         15 :         spdk_uuid_set_null(&namespace_uuid);
    4414                 :            : 
    4415                 :         15 :         size = snprintf(merged_str, sizeof(merged_str), "%s%"PRIu32, sn, nsid);
    4416   [ +  -  -  + ]:         15 :         if (size <= 0 || (unsigned long)size >= sizeof(merged_str)) {
    4417                 :          0 :                 return -EINVAL;
    4418                 :            :         }
    4419                 :            : 
    4420                 :         15 :         spdk_uuid_parse(&namespace_uuid, namespace_str);
    4421                 :            : 
    4422                 :         15 :         rc = spdk_uuid_generate_sha1(&new_uuid, &namespace_uuid, merged_str, size);
    4423         [ +  - ]:         15 :         if (rc == 0) {
    4424   [ #  #  #  # ]:         15 :                 memcpy(uuid, &new_uuid, sizeof(struct spdk_uuid));
    4425                 :          0 :         }
    4426                 :            : 
    4427                 :         15 :         return rc;
    4428                 :          0 : }
    4429                 :            : 
    4430                 :            : static int
    4431                 :       1470 : nvme_disk_create(struct spdk_bdev *disk, const char *base_name,
    4432                 :            :                  struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_ns *ns,
    4433                 :            :                  struct spdk_bdev_nvme_ctrlr_opts *bdev_opts, void *ctx)
    4434                 :            : {
    4435                 :            :         const struct spdk_uuid          *uuid;
    4436                 :            :         const uint8_t *nguid;
    4437                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    4438                 :            :         const struct spdk_nvme_ns_data  *nsdata;
    4439                 :            :         const struct spdk_nvme_ctrlr_opts *opts;
    4440                 :            :         enum spdk_nvme_csi              csi;
    4441                 :            :         uint32_t atomic_bs, phys_bs, bs;
    4442                 :       1470 :         char sn_tmp[SPDK_NVME_CTRLR_SN_LEN + 1] = {'\0'};
    4443                 :            :         int rc;
    4444                 :            : 
    4445                 :       1470 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    4446                 :       1470 :         csi = spdk_nvme_ns_get_csi(ns);
    4447                 :       1470 :         opts = spdk_nvme_ctrlr_get_opts(ctrlr);
    4448                 :            : 
    4449      [ +  +  - ]:       1470 :         switch (csi) {
    4450                 :       1467 :         case SPDK_NVME_CSI_NVM:
    4451   [ +  -  +  - ]:       1468 :                 disk->product_name = "NVMe disk";
    4452                 :       1468 :                 break;
    4453                 :          2 :         case SPDK_NVME_CSI_ZNS:
    4454   [ #  #  #  # ]:          2 :                 disk->product_name = "NVMe ZNS disk";
    4455   [ #  #  #  # ]:          2 :                 disk->zoned = true;
    4456   [ #  #  #  # ]:          2 :                 disk->zone_size = spdk_nvme_zns_ns_get_zone_size_sectors(ns);
    4457   [ #  #  #  #  :          2 :                 disk->max_zone_append_size = spdk_nvme_zns_ctrlr_get_max_zone_append_size(ctrlr) /
             #  #  #  # ]
    4458                 :          2 :                                              spdk_nvme_ns_get_extended_sector_size(ns);
    4459   [ #  #  #  # ]:          2 :                 disk->max_open_zones = spdk_nvme_zns_ns_get_max_open_zones(ns);
    4460   [ #  #  #  # ]:          2 :                 disk->max_active_zones = spdk_nvme_zns_ns_get_max_active_zones(ns);
    4461                 :          2 :                 break;
    4462                 :          0 :         default:
    4463   [ #  #  #  #  :          0 :                 if (bdev_opts->allow_unrecognized_csi) {
             #  #  #  # ]
    4464   [ #  #  #  # ]:          0 :                         disk->product_name = "NVMe Passthrough disk";
    4465                 :          0 :                         break;
    4466                 :            :                 }
    4467                 :          0 :                 SPDK_ERRLOG("unsupported CSI: %u\n", csi);
    4468                 :          0 :                 return -ENOTSUP;
    4469                 :            :         }
    4470                 :            : 
    4471                 :       1470 :         nguid = spdk_nvme_ns_get_nguid(ns);
    4472         [ +  + ]:       1470 :         if (!nguid) {
    4473                 :       1075 :                 uuid = spdk_nvme_ns_get_uuid(ns);
    4474         [ +  + ]:       1075 :                 if (uuid) {
    4475         [ #  # ]:        348 :                         disk->uuid = *uuid;
    4476   [ +  +  +  +  :        727 :                 } else if (g_opts.generate_uuids) {
                   +  - ]
    4477         [ #  # ]:          0 :                         spdk_strcpy_pad(sn_tmp, cdata->sn, SPDK_NVME_CTRLR_SN_LEN, '\0');
    4478         [ #  # ]:          0 :                         rc = nvme_generate_uuid(sn_tmp, spdk_nvme_ns_get_id(ns), &disk->uuid);
    4479         [ #  # ]:          0 :                         if (rc < 0) {
    4480         [ #  # ]:          0 :                                 SPDK_ERRLOG("UUID generation failed (%s)\n", spdk_strerror(-rc));
    4481                 :          0 :                                 return rc;
    4482                 :            :                         }
    4483                 :          0 :                 }
    4484                 :          1 :         } else {
    4485   [ #  #  #  #  :        395 :                 memcpy(&disk->uuid, nguid, sizeof(disk->uuid));
                   #  # ]
    4486                 :            :         }
    4487                 :            : 
    4488   [ +  -  +  - ]:       1470 :         disk->name = spdk_sprintf_alloc("%sn%d", base_name, spdk_nvme_ns_get_id(ns));
    4489   [ +  +  +  -  :       1470 :         if (!disk->name) {
                   -  + ]
    4490                 :          0 :                 return -ENOMEM;
    4491                 :            :         }
    4492                 :            : 
    4493   [ +  -  +  - ]:       1470 :         disk->write_cache = 0;
    4494   [ +  +  +  -  :       1470 :         if (cdata->vwc.present) {
                   +  - ]
    4495                 :            :                 /* Enable if the Volatile Write Cache exists */
    4496   [ #  #  #  # ]:       1247 :                 disk->write_cache = 1;
    4497                 :          0 :         }
    4498   [ +  +  +  -  :       1470 :         if (cdata->oncs.write_zeroes) {
                   +  - ]
    4499   [ #  #  #  # ]:       1266 :                 disk->max_write_zeroes = UINT16_MAX + 1;
    4500                 :          0 :         }
    4501   [ +  -  +  - ]:       1470 :         disk->blocklen = spdk_nvme_ns_get_extended_sector_size(ns);
    4502   [ +  -  +  - ]:       1470 :         disk->blockcnt = spdk_nvme_ns_get_num_sectors(ns);
    4503   [ +  -  +  - ]:       1470 :         disk->max_segment_size = spdk_nvme_ctrlr_get_max_xfer_size(ctrlr);
    4504   [ +  -  +  -  :       1470 :         disk->ctratt.raw = cdata->ctratt.raw;
          +  -  +  -  +  
                -  +  - ]
    4505   [ +  -  +  - ]:       1470 :         disk->nsid = spdk_nvme_ns_get_id(ns);
    4506                 :            :         /* NVMe driver will split one request into multiple requests
    4507                 :            :          * based on MDTS and stripe boundary, the bdev layer will use
    4508                 :            :          * max_segment_size and max_num_segments to split one big IO
    4509                 :            :          * into multiple requests, then small request can't run out
    4510                 :            :          * of NVMe internal requests data structure.
    4511                 :            :          */
    4512   [ +  -  +  +  :       1470 :         if (opts && opts->io_queue_requests) {
             +  -  -  + ]
    4513   [ +  -  +  -  :       1353 :                 disk->max_num_segments = opts->io_queue_requests / 2;
          +  -  +  -  +  
                      - ]
    4514                 :          1 :         }
    4515         [ +  + ]:       1470 :         if (spdk_nvme_ctrlr_get_flags(ctrlr) & SPDK_NVME_CTRLR_SGL_SUPPORTED) {
    4516                 :            :                 /* The nvme driver will try to split I/O that have too many
    4517                 :            :                  * SGEs, but it doesn't work if that last SGE doesn't end on
    4518                 :            :                  * an aggregate total that is block aligned. The bdev layer has
    4519                 :            :                  * a more robust splitting framework, so use that instead for
    4520                 :            :                  * this case. (See issue #3269.)
    4521                 :            :                  */
    4522                 :       1247 :                 uint16_t max_sges = spdk_nvme_ctrlr_get_max_sges(ctrlr);
    4523                 :            : 
    4524   [ -  +  #  #  :       1247 :                 if (disk->max_num_segments == 0) {
                   #  # ]
    4525   [ #  #  #  # ]:          0 :                         disk->max_num_segments = max_sges;
    4526                 :          0 :                 } else {
    4527   [ #  #  #  #  :       1247 :                         disk->max_num_segments = spdk_min(disk->max_num_segments, max_sges);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    4528                 :            :                 }
    4529                 :          0 :         }
    4530   [ +  -  +  - ]:       1470 :         disk->optimal_io_boundary = spdk_nvme_ns_get_optimal_io_boundary(ns);
    4531                 :            : 
    4532                 :       1470 :         nsdata = spdk_nvme_ns_get_data(ns);
    4533                 :       1470 :         bs = spdk_nvme_ns_get_sector_size(ns);
    4534                 :       1470 :         atomic_bs = bs;
    4535                 :       1470 :         phys_bs = bs;
    4536   [ +  -  +  -  :       1470 :         if (nsdata->nabo == 0) {
                   -  + ]
    4537   [ +  +  +  +  :       1470 :                 if (nsdata->nsfeat.ns_atomic_write_unit && nsdata->nawupf) {
          -  +  #  #  #  
                #  #  # ]
    4538   [ #  #  #  #  :         21 :                         atomic_bs = bs * (1 + nsdata->nawupf);
                   #  # ]
    4539                 :          0 :                 } else {
    4540   [ +  -  +  -  :       1449 :                         atomic_bs = bs * (1 + cdata->awupf);
                   +  - ]
    4541                 :            :                 }
    4542                 :          1 :         }
    4543   [ +  +  +  -  :       1470 :         if (nsdata->nsfeat.optperf) {
                   +  - ]
    4544   [ #  #  #  #  :       1266 :                 phys_bs = bs * (1 + nsdata->npwg);
                   #  # ]
    4545                 :          0 :         }
    4546   [ -  +  +  -  :       1470 :         disk->phys_blocklen = spdk_min(phys_bs, atomic_bs);
                   +  - ]
    4547                 :            : 
    4548   [ +  -  +  - ]:       1470 :         disk->md_len = spdk_nvme_ns_get_md_size(ns);
    4549   [ +  +  +  -  :       1470 :         if (disk->md_len != 0) {
                   -  + ]
    4550   [ #  #  #  #  :         52 :                 disk->md_interleave = nsdata->flbas.extended;
             #  #  #  # ]
    4551   [ #  #  #  # ]:         52 :                 disk->dif_type = (enum spdk_dif_type)spdk_nvme_ns_get_pi_type(ns);
    4552   [ -  +  #  #  :         52 :                 if (disk->dif_type != SPDK_DIF_DISABLE) {
                   #  # ]
    4553   [ #  #  #  #  :          0 :                         disk->dif_is_head_of_md = nsdata->dps.md_start;
             #  #  #  # ]
    4554   [ #  #  #  #  :          0 :                         disk->dif_check_flags = bdev_opts->prchk_flags;
             #  #  #  # ]
    4555   [ #  #  #  # ]:          0 :                         disk->dif_pi_format = (enum spdk_dif_pi_format)spdk_nvme_ns_get_pi_format(ns);
    4556                 :          0 :                 }
    4557                 :          0 :         }
    4558                 :            : 
    4559         [ +  + ]:       1470 :         if (!(spdk_nvme_ctrlr_get_flags(ctrlr) &
    4560                 :            :               SPDK_NVME_CTRLR_COMPARE_AND_WRITE_SUPPORTED)) {
    4561   [ +  -  +  - ]:       1094 :                 disk->acwu = 0;
    4562   [ +  -  #  #  :        377 :         } else if (nsdata->nsfeat.ns_atomic_write_unit) {
                   #  # ]
    4563   [ #  #  #  #  :        376 :                 disk->acwu = nsdata->nacwu + 1; /* 0-based */
          #  #  #  #  #  
                      # ]
    4564                 :          0 :         } else {
    4565   [ #  #  #  #  :          0 :                 disk->acwu = cdata->acwu + 1; /* 0-based */
          #  #  #  #  #  
                      # ]
    4566                 :            :         }
    4567                 :            : 
    4568   [ +  +  +  -  :       1470 :         if (cdata->oncs.copy) {
                   +  - ]
    4569                 :            :                 /* For now bdev interface allows only single segment copy */
    4570   [ #  #  #  #  :        935 :                 disk->max_copy = nsdata->mssrl;
             #  #  #  # ]
    4571                 :          0 :         }
    4572                 :            : 
    4573   [ +  -  +  - ]:       1470 :         disk->ctxt = ctx;
    4574   [ +  -  +  - ]:       1470 :         disk->fn_table = &nvmelib_fn_table;
    4575   [ +  -  +  - ]:       1470 :         disk->module = &nvme_if;
    4576                 :            : 
    4577   [ +  -  +  - ]:       1470 :         disk->numa.id_valid = 1;
    4578   [ +  -  +  - ]:       1470 :         disk->numa.id = spdk_nvme_ctrlr_get_numa_id(ctrlr);
    4579                 :            : 
    4580                 :       1470 :         return 0;
    4581                 :          1 : }
    4582                 :            : 
    4583                 :            : static struct nvme_bdev *
    4584                 :       1470 : nvme_bdev_alloc(void)
    4585                 :            : {
    4586                 :            :         struct nvme_bdev *bdev;
    4587                 :            :         int rc;
    4588                 :            : 
    4589                 :       1470 :         bdev = calloc(1, sizeof(*bdev));
    4590         [ +  + ]:       1470 :         if (!bdev) {
    4591                 :          0 :                 SPDK_ERRLOG("bdev calloc() failed\n");
    4592                 :          0 :                 return NULL;
    4593                 :            :         }
    4594                 :            : 
    4595   [ +  +  +  +  :       1470 :         if (g_opts.nvme_error_stat) {
                   +  - ]
    4596   [ #  #  #  # ]:         12 :                 bdev->err_stat = calloc(1, sizeof(struct nvme_error_stat));
    4597   [ -  +  #  #  :         12 :                 if (!bdev->err_stat) {
                   #  # ]
    4598                 :          0 :                         SPDK_ERRLOG("err_stat calloc() failed\n");
    4599                 :          0 :                         free(bdev);
    4600                 :          0 :                         return NULL;
    4601                 :            :                 }
    4602                 :          0 :         }
    4603                 :            : 
    4604   [ +  +  +  - ]:       1470 :         rc = pthread_mutex_init(&bdev->mutex, NULL);
    4605         [ -  + ]:       1470 :         if (rc != 0) {
    4606   [ #  #  #  # ]:          0 :                 free(bdev->err_stat);
    4607                 :          0 :                 free(bdev);
    4608                 :          0 :                 return NULL;
    4609                 :            :         }
    4610                 :            : 
    4611   [ -  +  -  + ]:       1470 :         bdev->ref = 1;
    4612   [ -  +  -  + ]:       1470 :         bdev->mp_policy = BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE;
    4613   [ -  +  -  + ]:       1470 :         bdev->mp_selector = BDEV_NVME_MP_SELECTOR_ROUND_ROBIN;
    4614   [ -  +  -  + ]:       1470 :         bdev->rr_min_io = UINT32_MAX;
    4615   [ -  +  -  +  :       1470 :         TAILQ_INIT(&bdev->nvme_ns_list);
          -  +  -  +  -  
          +  -  +  -  +  
                   -  + ]
    4616                 :            : 
    4617                 :       1470 :         return bdev;
    4618                 :          1 : }
    4619                 :            : 
    4620                 :            : static int
    4621                 :       1470 : nvme_bdev_create(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *nvme_ns)
    4622                 :            : {
    4623                 :            :         struct nvme_bdev *bdev;
    4624   [ +  -  +  - ]:       1470 :         struct nvme_bdev_ctrlr *nbdev_ctrlr = nvme_ctrlr->nbdev_ctrlr;
    4625                 :            :         int rc;
    4626                 :            : 
    4627                 :       1470 :         bdev = nvme_bdev_alloc();
    4628         [ +  + ]:       1470 :         if (bdev == NULL) {
    4629                 :          0 :                 SPDK_ERRLOG("Failed to allocate NVMe bdev\n");
    4630                 :          0 :                 return -ENOMEM;
    4631                 :            :         }
    4632                 :            : 
    4633   [ +  -  +  -  :       1470 :         bdev->opal = nvme_ctrlr->opal_dev != NULL;
             +  -  +  - ]
    4634                 :            : 
    4635   [ +  -  +  -  :       1471 :         rc = nvme_disk_create(&bdev->disk, nbdev_ctrlr->name, nvme_ctrlr->ctrlr,
          +  -  +  -  +  
                      - ]
    4636   [ +  -  +  -  :          1 :                               nvme_ns->ns, &nvme_ctrlr->opts, bdev);
                   +  - ]
    4637         [ -  + ]:       1470 :         if (rc != 0) {
    4638                 :          0 :                 SPDK_ERRLOG("Failed to create NVMe disk\n");
    4639                 :          0 :                 nvme_bdev_free(bdev);
    4640                 :          0 :                 return rc;
    4641                 :            :         }
    4642                 :            : 
    4643                 :       1471 :         spdk_io_device_register(bdev,
    4644                 :            :                                 bdev_nvme_create_bdev_channel_cb,
    4645                 :            :                                 bdev_nvme_destroy_bdev_channel_cb,
    4646                 :            :                                 sizeof(struct nvme_bdev_channel),
    4647   [ +  -  +  -  :       1470 :                                 bdev->disk.name);
                   +  - ]
    4648                 :            : 
    4649   [ +  -  +  - ]:       1470 :         nvme_ns->bdev = bdev;
    4650   [ +  -  +  -  :       1470 :         bdev->nsid = nvme_ns->id;
             +  -  +  - ]
    4651   [ +  -  +  -  :       1470 :         TAILQ_INSERT_TAIL(&bdev->nvme_ns_list, nvme_ns, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    4652                 :            : 
    4653   [ +  -  +  - ]:       1470 :         bdev->nbdev_ctrlr = nbdev_ctrlr;
    4654   [ +  -  +  -  :       1470 :         TAILQ_INSERT_TAIL(&nbdev_ctrlr->bdevs, bdev, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    4655                 :            : 
    4656         [ +  - ]:       1470 :         rc = spdk_bdev_register(&bdev->disk);
    4657         [ +  + ]:       1470 :         if (rc != 0) {
    4658                 :          5 :                 SPDK_ERRLOG("spdk_bdev_register() failed\n");
    4659                 :          5 :                 spdk_io_device_unregister(bdev, NULL);
    4660   [ #  #  #  # ]:          5 :                 nvme_ns->bdev = NULL;
    4661   [ -  +  #  #  :          5 :                 TAILQ_REMOVE(&nbdev_ctrlr->bdevs, bdev, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    4662                 :          5 :                 nvme_bdev_free(bdev);
    4663                 :          5 :                 return rc;
    4664                 :            :         }
    4665                 :            : 
    4666                 :       1465 :         return 0;
    4667                 :          1 : }
    4668                 :            : 
    4669                 :            : static bool
    4670                 :        103 : bdev_nvme_compare_ns(struct spdk_nvme_ns *ns1, struct spdk_nvme_ns *ns2)
    4671                 :            : {
    4672                 :            :         const struct spdk_nvme_ns_data *nsdata1, *nsdata2;
    4673                 :            :         const struct spdk_uuid *uuid1, *uuid2;
    4674                 :            : 
    4675                 :        103 :         nsdata1 = spdk_nvme_ns_get_data(ns1);
    4676                 :        103 :         nsdata2 = spdk_nvme_ns_get_data(ns2);
    4677                 :        103 :         uuid1 = spdk_nvme_ns_get_uuid(ns1);
    4678                 :        103 :         uuid2 = spdk_nvme_ns_get_uuid(ns2);
    4679                 :            : 
    4680   [ -  +  -  +  :        125 :         return memcmp(nsdata1->nguid, nsdata2->nguid, sizeof(nsdata1->nguid)) == 0 &&
          #  #  #  #  #  
                      # ]
    4681   [ +  +  +  +  :        100 :                nsdata1->eui64 == nsdata2->eui64 &&
          #  #  #  #  #  
                      # ]
    4682   [ +  +  +  + ]:         97 :                ((uuid1 == NULL && uuid2 == NULL) ||
    4683   [ +  +  +  -  :        279 :                 (uuid1 != NULL && uuid2 != NULL && spdk_uuid_compare(uuid1, uuid2) == 0)) &&
             +  +  +  + ]
    4684                 :         88 :                spdk_nvme_ns_get_csi(ns1) == spdk_nvme_ns_get_csi(ns2);
    4685                 :            : }
    4686                 :            : 
    4687                 :            : static bool
    4688                 :         56 : hotplug_probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
    4689                 :            :                  struct spdk_nvme_ctrlr_opts *opts)
    4690                 :            : {
    4691                 :            :         struct nvme_probe_skip_entry *entry;
    4692                 :            : 
    4693   [ -  +  #  #  :         56 :         TAILQ_FOREACH(entry, &g_skipped_nvme_ctrlrs, tailq) {
             #  #  #  # ]
    4694   [ #  #  #  # ]:          0 :                 if (spdk_nvme_transport_id_compare(trid, &entry->trid) == 0) {
    4695                 :          0 :                         return false;
    4696                 :            :                 }
    4697                 :          0 :         }
    4698                 :            : 
    4699   [ #  #  #  #  :         56 :         opts->arbitration_burst = (uint8_t)g_opts.arbitration_burst;
                   #  # ]
    4700   [ #  #  #  #  :         56 :         opts->low_priority_weight = (uint8_t)g_opts.low_priority_weight;
                   #  # ]
    4701   [ #  #  #  #  :         56 :         opts->medium_priority_weight = (uint8_t)g_opts.medium_priority_weight;
                   #  # ]
    4702   [ #  #  #  #  :         56 :         opts->high_priority_weight = (uint8_t)g_opts.high_priority_weight;
                   #  # ]
    4703   [ #  #  #  # ]:         56 :         opts->disable_read_ana_log_page = true;
    4704                 :            : 
    4705   [ -  +  -  +  :         56 :         SPDK_DEBUGLOG(bdev_nvme, "Attaching to %s\n", trid->traddr);
             #  #  #  # ]
    4706                 :            : 
    4707                 :         56 :         return true;
    4708                 :          0 : }
    4709                 :            : 
    4710                 :            : static void
    4711                 :          0 : nvme_abort_cpl(void *ctx, const struct spdk_nvme_cpl *cpl)
    4712                 :            : {
    4713                 :          0 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    4714                 :            : 
    4715   [ #  #  #  #  :          0 :         if (spdk_nvme_cpl_is_error(cpl)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    4716   [ #  #  #  #  :          0 :                 NVME_CTRLR_WARNLOG(nvme_ctrlr, "Abort failed. Resetting controller. sc is %u, sct is %u.\n",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    4717                 :            :                                    cpl->status.sc, cpl->status.sct);
    4718                 :          0 :                 bdev_nvme_reset_ctrlr(nvme_ctrlr);
    4719   [ #  #  #  #  :          0 :         } else if (cpl->cdw0 & 0x1) {
                   #  # ]
    4720   [ #  #  #  #  :          0 :                 NVME_CTRLR_WARNLOG(nvme_ctrlr, "Specified command could not be aborted.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    4721                 :          0 :                 bdev_nvme_reset_ctrlr(nvme_ctrlr);
    4722                 :          0 :         }
    4723                 :          0 : }
    4724                 :            : 
    4725                 :            : static void
    4726                 :          0 : timeout_cb(void *cb_arg, struct spdk_nvme_ctrlr *ctrlr,
    4727                 :            :            struct spdk_nvme_qpair *qpair, uint16_t cid)
    4728                 :            : {
    4729                 :          0 :         struct nvme_ctrlr *nvme_ctrlr = cb_arg;
    4730                 :            :         union spdk_nvme_csts_register csts;
    4731                 :            :         int rc;
    4732                 :            : 
    4733   [ #  #  #  #  :          0 :         assert(nvme_ctrlr->ctrlr == ctrlr);
             #  #  #  # ]
    4734                 :            : 
    4735   [ #  #  #  #  :          0 :         NVME_CTRLR_WARNLOG(nvme_ctrlr, "Warning: Detected a timeout. ctrlr=%p qpair=%p cid=%u\n",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    4736                 :            :                            ctrlr, qpair, cid);
    4737                 :            : 
    4738                 :            :         /* Only try to read CSTS if it's a PCIe controller or we have a timeout on an I/O
    4739                 :            :          * queue.  (Note: qpair == NULL when there's an admin cmd timeout.)  Otherwise we
    4740                 :            :          * would submit another fabrics cmd on the admin queue to read CSTS and check for its
    4741                 :            :          * completion recursively.
    4742                 :            :          */
    4743   [ #  #  #  #  :          0 :         if (nvme_ctrlr->active_path_id->trid.trtype == SPDK_NVME_TRANSPORT_PCIE || qpair != NULL) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    4744                 :          0 :                 csts = spdk_nvme_ctrlr_get_regs_csts(ctrlr);
    4745         [ #  # ]:          0 :                 if (csts.bits.cfs) {
    4746   [ #  #  #  #  :          0 :                         NVME_CTRLR_ERRLOG(nvme_ctrlr, "Controller Fatal Status, reset required\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    4747                 :          0 :                         bdev_nvme_reset_ctrlr(nvme_ctrlr);
    4748                 :          0 :                         return;
    4749                 :            :                 }
    4750                 :          0 :         }
    4751                 :            : 
    4752   [ #  #  #  #  :          0 :         switch (g_opts.action_on_timeout) {
                   #  # ]
    4753                 :          0 :         case SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT:
    4754         [ #  # ]:          0 :                 if (qpair) {
    4755                 :            :                         /* Don't send abort to ctrlr when ctrlr is not available. */
    4756   [ #  #  #  # ]:          0 :                         pthread_mutex_lock(&nvme_ctrlr->mutex);
    4757         [ #  # ]:          0 :                         if (!nvme_ctrlr_is_available(nvme_ctrlr)) {
    4758   [ #  #  #  # ]:          0 :                                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4759   [ #  #  #  #  :          0 :                                 NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Quit abort. Ctrlr is not available.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    4760                 :          0 :                                 return;
    4761                 :            :                         }
    4762   [ #  #  #  # ]:          0 :                         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4763                 :            : 
    4764                 :          0 :                         rc = spdk_nvme_ctrlr_cmd_abort(ctrlr, qpair, cid,
    4765                 :          0 :                                                        nvme_abort_cpl, nvme_ctrlr);
    4766         [ #  # ]:          0 :                         if (rc == 0) {
    4767                 :          0 :                                 return;
    4768                 :            :                         }
    4769                 :            : 
    4770   [ #  #  #  #  :          0 :                         NVME_CTRLR_ERRLOG(nvme_ctrlr, "Unable to send abort. Resetting, rc is %d.\n", rc);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    4771                 :          0 :                 }
    4772                 :            : 
    4773                 :            :         /* FALLTHROUGH */
    4774                 :            :         case SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET:
    4775                 :          0 :                 bdev_nvme_reset_ctrlr(nvme_ctrlr);
    4776                 :          0 :                 break;
    4777                 :          0 :         case SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE:
    4778   [ #  #  #  #  :          0 :                 NVME_CTRLR_DEBUGLOG(nvme_ctrlr, "No action for nvme controller timeout.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    4779                 :          0 :                 break;
    4780                 :          0 :         default:
    4781   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "An invalid timeout action value is found.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    4782                 :          0 :                 break;
    4783                 :            :         }
    4784                 :          0 : }
    4785                 :            : 
    4786                 :            : static struct nvme_ns *
    4787                 :       1527 : nvme_ns_alloc(void)
    4788                 :            : {
    4789                 :            :         struct nvme_ns *nvme_ns;
    4790                 :            : 
    4791                 :       1527 :         nvme_ns = calloc(1, sizeof(struct nvme_ns));
    4792         [ +  + ]:       1527 :         if (nvme_ns == NULL) {
    4793                 :          0 :                 return NULL;
    4794                 :            :         }
    4795                 :            : 
    4796   [ +  +  +  + ]:       1527 :         if (g_opts.io_path_stat) {
    4797   [ #  #  #  # ]:          0 :                 nvme_ns->stat = calloc(1, sizeof(struct spdk_bdev_io_stat));
    4798   [ #  #  #  #  :          0 :                 if (nvme_ns->stat == NULL) {
                   #  # ]
    4799                 :          0 :                         free(nvme_ns);
    4800                 :          0 :                         return NULL;
    4801                 :            :                 }
    4802   [ #  #  #  # ]:          0 :                 spdk_bdev_reset_io_stat(nvme_ns->stat, SPDK_BDEV_RESET_STAT_MAXMIN);
    4803                 :          0 :         }
    4804                 :            : 
    4805                 :       1527 :         return nvme_ns;
    4806                 :          1 : }
    4807                 :            : 
    4808                 :            : static void
    4809                 :       1527 : nvme_ns_free(struct nvme_ns *nvme_ns)
    4810                 :            : {
    4811   [ +  -  +  - ]:       1527 :         free(nvme_ns->stat);
    4812                 :       1527 :         free(nvme_ns);
    4813                 :       1527 : }
    4814                 :            : 
    4815                 :            : static void
    4816                 :       1527 : nvme_ctrlr_populate_namespace_done(struct nvme_ns *nvme_ns, int rc)
    4817                 :            : {
    4818   [ +  -  +  - ]:       1527 :         struct nvme_ctrlr *nvme_ctrlr = nvme_ns->ctrlr;
    4819   [ +  -  +  - ]:       1527 :         struct nvme_async_probe_ctx *ctx = nvme_ns->probe_ctx;
    4820                 :            : 
    4821         [ +  + ]:       1527 :         if (rc == 0) {
    4822   [ -  +  -  + ]:       1519 :                 nvme_ns->probe_ctx = NULL;
    4823                 :       1519 :                 nvme_ctrlr_get_ref(nvme_ctrlr);
    4824                 :          1 :         } else {
    4825         [ #  # ]:          8 :                 RB_REMOVE(nvme_ns_tree, &nvme_ctrlr->namespaces, nvme_ns);
    4826                 :          8 :                 nvme_ns_free(nvme_ns);
    4827                 :            :         }
    4828                 :            : 
    4829         [ +  + ]:       1527 :         if (ctx) {
    4830         [ +  - ]:       1463 :                 ctx->populates_in_progress--;
    4831   [ +  +  +  -  :       1463 :                 if (ctx->populates_in_progress == 0) {
                   +  - ]
    4832                 :         49 :                         nvme_ctrlr_populate_namespaces_done(nvme_ctrlr, ctx);
    4833                 :          0 :                 }
    4834                 :          1 :         }
    4835                 :       1527 : }
    4836                 :            : 
    4837                 :            : static void
    4838                 :          6 : bdev_nvme_add_io_path(struct nvme_bdev_channel_iter *i,
    4839                 :            :                       struct nvme_bdev *nbdev,
    4840                 :            :                       struct nvme_bdev_channel *nbdev_ch, void *ctx)
    4841                 :            : {
    4842                 :          6 :         struct nvme_ns *nvme_ns = ctx;
    4843                 :            :         int rc;
    4844                 :            : 
    4845                 :          6 :         rc = _bdev_nvme_add_io_path(nbdev_ch, nvme_ns);
    4846         [ -  + ]:          6 :         if (rc != 0) {
    4847                 :          0 :                 SPDK_ERRLOG("Failed to add I/O path to bdev_channel dynamically.\n");
    4848                 :          0 :         }
    4849                 :            : 
    4850                 :          6 :         nvme_bdev_for_each_channel_continue(i, rc);
    4851                 :          6 : }
    4852                 :            : 
    4853                 :            : static void
    4854                 :          6 : bdev_nvme_delete_io_path(struct nvme_bdev_channel_iter *i,
    4855                 :            :                          struct nvme_bdev *nbdev,
    4856                 :            :                          struct nvme_bdev_channel *nbdev_ch, void *ctx)
    4857                 :            : {
    4858                 :          6 :         struct nvme_ns *nvme_ns = ctx;
    4859                 :            :         struct nvme_io_path *io_path;
    4860                 :            : 
    4861                 :          6 :         io_path = _bdev_nvme_get_io_path(nbdev_ch, nvme_ns);
    4862         [ +  - ]:          6 :         if (io_path != NULL) {
    4863                 :          6 :                 _bdev_nvme_delete_io_path(nbdev_ch, io_path);
    4864                 :          0 :         }
    4865                 :            : 
    4866                 :          6 :         nvme_bdev_for_each_channel_continue(i, 0);
    4867                 :          6 : }
    4868                 :            : 
    4869                 :            : static void
    4870                 :          0 : bdev_nvme_add_io_path_failed(struct nvme_bdev *nbdev, void *ctx, int status)
    4871                 :            : {
    4872                 :          0 :         struct nvme_ns *nvme_ns = ctx;
    4873                 :            : 
    4874                 :          0 :         nvme_ctrlr_populate_namespace_done(nvme_ns, -1);
    4875                 :          0 : }
    4876                 :            : 
    4877                 :            : static void
    4878                 :         54 : bdev_nvme_add_io_path_done(struct nvme_bdev *nbdev, void *ctx, int status)
    4879                 :            : {
    4880                 :         54 :         struct nvme_ns *nvme_ns = ctx;
    4881                 :            : 
    4882         [ +  - ]:         54 :         if (status == 0) {
    4883                 :         54 :                 nvme_ctrlr_populate_namespace_done(nvme_ns, 0);
    4884                 :          0 :         } else {
    4885                 :            :                 /* Delete the added io_paths and fail populating the namespace. */
    4886                 :          0 :                 nvme_bdev_for_each_channel(nbdev,
    4887                 :            :                                            bdev_nvme_delete_io_path,
    4888                 :          0 :                                            nvme_ns,
    4889                 :            :                                            bdev_nvme_add_io_path_failed);
    4890                 :            :         }
    4891                 :         54 : }
    4892                 :            : 
    4893                 :            : static int
    4894                 :         57 : nvme_bdev_add_ns(struct nvme_bdev *bdev, struct nvme_ns *nvme_ns)
    4895                 :            : {
    4896                 :            :         struct nvme_ns *tmp_ns;
    4897                 :            :         const struct spdk_nvme_ns_data *nsdata;
    4898                 :            : 
    4899   [ #  #  #  # ]:         57 :         nsdata = spdk_nvme_ns_get_data(nvme_ns->ns);
    4900   [ -  +  #  #  :         57 :         if (!nsdata->nmic.can_share) {
                   #  # ]
    4901                 :          0 :                 SPDK_ERRLOG("Namespace cannot be shared.\n");
    4902                 :          0 :                 return -EINVAL;
    4903                 :            :         }
    4904                 :            : 
    4905   [ -  +  #  # ]:         57 :         pthread_mutex_lock(&bdev->mutex);
    4906                 :            : 
    4907   [ #  #  #  #  :         57 :         tmp_ns = TAILQ_FIRST(&bdev->nvme_ns_list);
                   #  # ]
    4908   [ -  +  #  # ]:         57 :         assert(tmp_ns != NULL);
    4909                 :            : 
    4910   [ +  -  +  +  :         57 :         if (tmp_ns->ns != NULL && !bdev_nvme_compare_ns(nvme_ns->ns, tmp_ns->ns)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    4911   [ -  +  #  # ]:          3 :                 pthread_mutex_unlock(&bdev->mutex);
    4912                 :          3 :                 SPDK_ERRLOG("Namespaces are not identical.\n");
    4913                 :          3 :                 return -EINVAL;
    4914                 :            :         }
    4915                 :            : 
    4916   [ #  #  #  # ]:         54 :         bdev->ref++;
    4917   [ #  #  #  #  :         54 :         TAILQ_INSERT_TAIL(&bdev->nvme_ns_list, nvme_ns, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    4918   [ #  #  #  # ]:         54 :         nvme_ns->bdev = bdev;
    4919                 :            : 
    4920   [ -  +  #  # ]:         54 :         pthread_mutex_unlock(&bdev->mutex);
    4921                 :            : 
    4922                 :            :         /* Add nvme_io_path to nvme_bdev_channels dynamically. */
    4923                 :         54 :         nvme_bdev_for_each_channel(bdev,
    4924                 :            :                                    bdev_nvme_add_io_path,
    4925                 :          0 :                                    nvme_ns,
    4926                 :            :                                    bdev_nvme_add_io_path_done);
    4927                 :            : 
    4928                 :         54 :         return 0;
    4929                 :          0 : }
    4930                 :            : 
    4931                 :            : static void
    4932                 :       1527 : nvme_ctrlr_populate_namespace(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *nvme_ns)
    4933                 :            : {
    4934                 :            :         struct spdk_nvme_ns     *ns;
    4935                 :            :         struct nvme_bdev        *bdev;
    4936                 :       1527 :         int                     rc = 0;
    4937                 :            : 
    4938   [ +  -  +  -  :       1527 :         ns = spdk_nvme_ctrlr_get_ns(nvme_ctrlr->ctrlr, nvme_ns->id);
             +  -  +  - ]
    4939         [ -  + ]:       1527 :         if (!ns) {
    4940   [ #  #  #  #  :          0 :                 NVME_CTRLR_DEBUGLOG(nvme_ctrlr, "Invalid NS %d\n", nvme_ns->id);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    4941                 :          0 :                 rc = -EINVAL;
    4942                 :          0 :                 goto done;
    4943                 :            :         }
    4944                 :            : 
    4945   [ +  -  +  - ]:       1527 :         nvme_ns->ns = ns;
    4946   [ +  -  +  - ]:       1527 :         nvme_ns->ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
    4947                 :            : 
    4948   [ +  +  +  -  :       1527 :         if (nvme_ctrlr->ana_log_page != NULL) {
                   +  - ]
    4949                 :        438 :                 bdev_nvme_parse_ana_log_page(nvme_ctrlr, nvme_ns_set_ana_state, nvme_ns);
    4950                 :          0 :         }
    4951                 :            : 
    4952   [ +  -  +  -  :       1527 :         bdev = nvme_bdev_ctrlr_get_bdev(nvme_ctrlr->nbdev_ctrlr, nvme_ns->id);
             +  -  +  - ]
    4953         [ +  + ]:       1528 :         if (bdev == NULL) {
    4954                 :       1470 :                 rc = nvme_bdev_create(nvme_ctrlr, nvme_ns);
    4955                 :          1 :         } else {
    4956                 :         57 :                 rc = nvme_bdev_add_ns(bdev, nvme_ns);
    4957         [ +  + ]:         57 :                 if (rc == 0) {
    4958                 :         54 :                         return;
    4959                 :            :                 }
    4960                 :            :         }
    4961                 :          3 : done:
    4962                 :       1473 :         nvme_ctrlr_populate_namespace_done(nvme_ns, rc);
    4963                 :          1 : }
    4964                 :            : 
    4965                 :            : static void
    4966                 :       1519 : nvme_ctrlr_depopulate_namespace_done(struct nvme_ns *nvme_ns)
    4967                 :            : {
    4968   [ +  -  +  - ]:       1519 :         struct nvme_ctrlr *nvme_ctrlr = nvme_ns->ctrlr;
    4969                 :            : 
    4970   [ +  +  #  # ]:       1519 :         assert(nvme_ctrlr != NULL);
    4971                 :            : 
    4972   [ +  +  +  - ]:       1519 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    4973                 :            : 
    4974         [ +  - ]:       1519 :         RB_REMOVE(nvme_ns_tree, &nvme_ctrlr->namespaces, nvme_ns);
    4975                 :            : 
    4976   [ +  +  +  -  :       1519 :         if (nvme_ns->bdev != NULL) {
                   -  + ]
    4977   [ -  +  #  # ]:        523 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4978                 :        523 :                 return;
    4979                 :            :         }
    4980                 :            : 
    4981                 :        996 :         nvme_ns_free(nvme_ns);
    4982   [ +  +  +  - ]:        996 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    4983                 :            : 
    4984                 :        996 :         nvme_ctrlr_put_ref(nvme_ctrlr);
    4985                 :          1 : }
    4986                 :            : 
    4987                 :            : static void
    4988                 :         45 : bdev_nvme_delete_io_path_done(struct nvme_bdev *nbdev, void *ctx, int status)
    4989                 :            : {
    4990                 :         45 :         struct nvme_ns *nvme_ns = ctx;
    4991                 :            : 
    4992                 :         45 :         nvme_ctrlr_depopulate_namespace_done(nvme_ns);
    4993                 :         45 : }
    4994                 :            : 
    4995                 :            : static void
    4996                 :       1519 : nvme_ctrlr_depopulate_namespace(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *nvme_ns)
    4997                 :            : {
    4998                 :            :         struct nvme_bdev *bdev;
    4999                 :            : 
    5000         [ +  - ]:       1519 :         spdk_poller_unregister(&nvme_ns->anatt_timer);
    5001                 :            : 
    5002   [ +  -  +  - ]:       1519 :         bdev = nvme_ns->bdev;
    5003         [ +  + ]:       1519 :         if (bdev != NULL) {
    5004   [ -  +  #  # ]:        673 :                 pthread_mutex_lock(&bdev->mutex);
    5005                 :            : 
    5006   [ -  +  #  #  :        673 :                 assert(bdev->ref > 0);
             #  #  #  # ]
    5007   [ #  #  #  # ]:        673 :                 bdev->ref--;
    5008   [ +  +  #  #  :        673 :                 if (bdev->ref == 0) {
                   #  # ]
    5009   [ -  +  #  # ]:        628 :                         pthread_mutex_unlock(&bdev->mutex);
    5010                 :            : 
    5011         [ #  # ]:        628 :                         spdk_bdev_unregister(&bdev->disk, NULL, NULL);
    5012                 :          0 :                 } else {
    5013                 :            :                         /* spdk_bdev_unregister() is not called until the last nvme_ns is
    5014                 :            :                          * depopulated. Hence we need to remove nvme_ns from bdev->nvme_ns_list
    5015                 :            :                          * and clear nvme_ns->bdev here.
    5016                 :            :                          */
    5017   [ +  +  #  #  :         45 :                         TAILQ_REMOVE(&bdev->nvme_ns_list, nvme_ns, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5018   [ #  #  #  # ]:         45 :                         nvme_ns->bdev = NULL;
    5019                 :            : 
    5020   [ -  +  #  # ]:         45 :                         pthread_mutex_unlock(&bdev->mutex);
    5021                 :            : 
    5022                 :            :                         /* Delete nvme_io_paths from nvme_bdev_channels dynamically. After that,
    5023                 :            :                          * we call depopulate_namespace_done() to avoid use-after-free.
    5024                 :            :                          */
    5025                 :         45 :                         nvme_bdev_for_each_channel(bdev,
    5026                 :            :                                                    bdev_nvme_delete_io_path,
    5027                 :          0 :                                                    nvme_ns,
    5028                 :            :                                                    bdev_nvme_delete_io_path_done);
    5029                 :         45 :                         return;
    5030                 :            :                 }
    5031                 :          0 :         }
    5032                 :            : 
    5033                 :       1474 :         nvme_ctrlr_depopulate_namespace_done(nvme_ns);
    5034                 :          1 : }
    5035                 :            : 
    5036                 :            : static void
    5037                 :       1759 : nvme_ctrlr_populate_namespaces(struct nvme_ctrlr *nvme_ctrlr,
    5038                 :            :                                struct nvme_async_probe_ctx *ctx)
    5039                 :            : {
    5040   [ +  -  +  - ]:       1759 :         struct spdk_nvme_ctrlr  *ctrlr = nvme_ctrlr->ctrlr;
    5041                 :            :         struct nvme_ns  *nvme_ns, *next;
    5042                 :            :         struct spdk_nvme_ns     *ns;
    5043                 :            :         struct nvme_bdev        *bdev;
    5044                 :            :         uint32_t                nsid;
    5045                 :            :         int                     rc;
    5046                 :            :         uint64_t                num_sectors;
    5047                 :            : 
    5048         [ +  + ]:       1759 :         if (ctx) {
    5049                 :            :                 /* Initialize this count to 1 to handle the populate functions
    5050                 :            :                  * calling nvme_ctrlr_populate_namespace_done() immediately.
    5051                 :            :                  */
    5052   [ +  -  +  - ]:       1629 :                 ctx->populates_in_progress = 1;
    5053                 :          1 :         }
    5054                 :            : 
    5055                 :            :         /* First loop over our existing namespaces and see if they have been
    5056                 :            :          * removed. */
    5057                 :       1759 :         nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
    5058         [ +  + ]:       1789 :         while (nvme_ns != NULL) {
    5059                 :         30 :                 next = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns);
    5060                 :            : 
    5061   [ +  +  #  #  :         30 :                 if (spdk_nvme_ctrlr_is_active_ns(ctrlr, nvme_ns->id)) {
                   #  # ]
    5062                 :            :                         /* NS is still there or added again. Its attributes may have changed. */
    5063   [ #  #  #  # ]:         18 :                         ns = spdk_nvme_ctrlr_get_ns(ctrlr, nvme_ns->id);
    5064   [ +  +  #  #  :         18 :                         if (nvme_ns->ns != ns) {
                   #  # ]
    5065   [ -  +  #  #  :          3 :                                 assert(nvme_ns->ns == NULL);
             #  #  #  # ]
    5066   [ #  #  #  # ]:          3 :                                 nvme_ns->ns = ns;
    5067   [ -  +  -  +  :          3 :                                 NVME_CTRLR_DEBUGLOG(nvme_ctrlr, "NSID %u was added\n", nvme_ns->id);
          -  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5068                 :          0 :                         }
    5069                 :            : 
    5070                 :         18 :                         num_sectors = spdk_nvme_ns_get_num_sectors(ns);
    5071   [ #  #  #  # ]:         18 :                         bdev = nvme_ns->bdev;
    5072   [ -  +  #  # ]:         18 :                         assert(bdev != NULL);
    5073   [ +  +  #  #  :         18 :                         if (bdev->disk.blockcnt != num_sectors) {
             #  #  #  # ]
    5074   [ +  -  #  #  :          3 :                                 NVME_CTRLR_NOTICELOG(nvme_ctrlr,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    5075                 :            :                                                      "NSID %u is resized: bdev name %s, old size %" PRIu64 ", new size %" PRIu64 "\n",
    5076                 :            :                                                      nvme_ns->id,
    5077                 :            :                                                      bdev->disk.name,
    5078                 :            :                                                      bdev->disk.blockcnt,
    5079                 :            :                                                      num_sectors);
    5080         [ #  # ]:          3 :                                 rc = spdk_bdev_notify_blockcnt_change(&bdev->disk, num_sectors);
    5081         [ -  + ]:          3 :                                 if (rc != 0) {
    5082   [ #  #  #  #  :          0 :                                         NVME_CTRLR_ERRLOG(nvme_ctrlr,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    5083                 :            :                                                           "Could not change num blocks for nvme bdev: name %s, errno: %d.\n",
    5084                 :            :                                                           bdev->disk.name, rc);
    5085                 :          0 :                                 }
    5086                 :          0 :                         }
    5087                 :          0 :                 } else {
    5088                 :            :                         /* Namespace was removed */
    5089                 :         12 :                         nvme_ctrlr_depopulate_namespace(nvme_ctrlr, nvme_ns);
    5090                 :            :                 }
    5091                 :            : 
    5092                 :         30 :                 nvme_ns = next;
    5093                 :            :         }
    5094                 :            : 
    5095                 :            :         /* Loop through all of the namespaces at the nvme level and see if any of them are new */
    5096                 :       1759 :         nsid = spdk_nvme_ctrlr_get_first_active_ns(ctrlr);
    5097         [ +  + ]:       3304 :         while (nsid != 0) {
    5098                 :       1545 :                 nvme_ns = nvme_ctrlr_get_ns(nvme_ctrlr, nsid);
    5099                 :            : 
    5100         [ +  + ]:       1545 :                 if (nvme_ns == NULL) {
    5101                 :            :                         /* Found a new one */
    5102                 :       1527 :                         nvme_ns = nvme_ns_alloc();
    5103         [ +  + ]:       1527 :                         if (nvme_ns == NULL) {
    5104   [ #  #  #  #  :          0 :                                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to allocate namespace\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5105                 :            :                                 /* This just fails to attach the namespace. It may work on a future attempt. */
    5106                 :          0 :                                 continue;
    5107                 :            :                         }
    5108                 :            : 
    5109   [ +  -  +  - ]:       1527 :                         nvme_ns->id = nsid;
    5110   [ +  -  +  - ]:       1527 :                         nvme_ns->ctrlr = nvme_ctrlr;
    5111                 :            : 
    5112   [ +  -  +  - ]:       1527 :                         nvme_ns->bdev = NULL;
    5113                 :            : 
    5114         [ +  + ]:       1527 :                         if (ctx) {
    5115         [ +  - ]:       1463 :                                 ctx->populates_in_progress++;
    5116                 :          1 :                         }
    5117   [ +  -  +  - ]:       1527 :                         nvme_ns->probe_ctx = ctx;
    5118                 :            : 
    5119         [ +  - ]:       1527 :                         RB_INSERT(nvme_ns_tree, &nvme_ctrlr->namespaces, nvme_ns);
    5120                 :            : 
    5121                 :       1527 :                         nvme_ctrlr_populate_namespace(nvme_ctrlr, nvme_ns);
    5122                 :          1 :                 }
    5123                 :            : 
    5124                 :       1545 :                 nsid = spdk_nvme_ctrlr_get_next_active_ns(ctrlr, nsid);
    5125                 :            :         }
    5126                 :            : 
    5127         [ +  + ]:       1759 :         if (ctx) {
    5128                 :            :                 /* Decrement this count now that the loop is over to account
    5129                 :            :                  * for the one we started with.  If the count is then 0, we
    5130                 :            :                  * know any populate_namespace functions completed immediately,
    5131                 :            :                  * so we'll kick the callback here.
    5132                 :            :                  */
    5133         [ +  - ]:       1629 :                 ctx->populates_in_progress--;
    5134   [ +  +  +  -  :       1629 :                 if (ctx->populates_in_progress == 0) {
                   -  + ]
    5135                 :       1580 :                         nvme_ctrlr_populate_namespaces_done(nvme_ctrlr, ctx);
    5136                 :          1 :                 }
    5137                 :          1 :         }
    5138                 :            : 
    5139                 :       1759 : }
    5140                 :            : 
    5141                 :            : static void
    5142                 :       1730 : nvme_ctrlr_depopulate_namespaces(struct nvme_ctrlr *nvme_ctrlr)
    5143                 :            : {
    5144                 :            :         struct nvme_ns *nvme_ns, *tmp;
    5145                 :            : 
    5146   [ +  +  +  +  :       3237 :         RB_FOREACH_SAFE(nvme_ns, nvme_ns_tree, &nvme_ctrlr->namespaces, tmp) {
                   +  + ]
    5147                 :       1507 :                 nvme_ctrlr_depopulate_namespace(nvme_ctrlr, nvme_ns);
    5148                 :          1 :         }
    5149                 :       1730 : }
    5150                 :            : 
    5151                 :            : static uint32_t
    5152                 :       2151 : nvme_ctrlr_get_ana_log_page_size(struct nvme_ctrlr *nvme_ctrlr)
    5153                 :            : {
    5154   [ #  #  #  # ]:       2151 :         struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
    5155                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    5156                 :       2151 :         uint32_t nsid, ns_count = 0;
    5157                 :            : 
    5158                 :       2151 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    5159                 :            : 
    5160         [ #  # ]:       2151 :         for (nsid = spdk_nvme_ctrlr_get_first_active_ns(ctrlr);
    5161         [ +  + ]:       4326 :              nsid != 0; nsid = spdk_nvme_ctrlr_get_next_active_ns(ctrlr, nsid)) {
    5162                 :       2175 :                 ns_count++;
    5163                 :          0 :         }
    5164                 :            : 
    5165   [ #  #  #  # ]:       2151 :         return sizeof(struct spdk_nvme_ana_page) + cdata->nanagrpid *
    5166                 :       2151 :                sizeof(struct spdk_nvme_ana_group_descriptor) + ns_count *
    5167                 :            :                sizeof(uint32_t);
    5168                 :            : }
    5169                 :            : 
    5170                 :            : static int
    5171                 :        123 : nvme_ctrlr_set_ana_states(const struct spdk_nvme_ana_group_descriptor *desc,
    5172                 :            :                           void *cb_arg)
    5173                 :            : {
    5174                 :        123 :         struct nvme_ctrlr *nvme_ctrlr = cb_arg;
    5175                 :            :         struct nvme_ns *nvme_ns;
    5176                 :            :         uint32_t i, nsid;
    5177                 :            : 
    5178   [ +  +  #  #  :        243 :         for (i = 0; i < desc->num_of_nsid; i++) {
                   #  # ]
    5179   [ #  #  #  #  :        120 :                 nsid = desc->nsid[i];
                   #  # ]
    5180         [ -  + ]:        120 :                 if (nsid == 0) {
    5181                 :          0 :                         continue;
    5182                 :            :                 }
    5183                 :            : 
    5184                 :        120 :                 nvme_ns = nvme_ctrlr_get_ns(nvme_ctrlr, nsid);
    5185                 :            : 
    5186         [ +  + ]:        120 :                 if (nvme_ns == NULL) {
    5187                 :            :                         /* Target told us that an inactive namespace had an ANA change */
    5188                 :          3 :                         continue;
    5189                 :            :                 }
    5190                 :            : 
    5191                 :        117 :                 _nvme_ns_set_ana_state(nvme_ns, desc);
    5192                 :          0 :         }
    5193                 :            : 
    5194                 :        123 :         return 0;
    5195                 :            : }
    5196                 :            : 
    5197                 :            : static void
    5198                 :          1 : bdev_nvme_disable_read_ana_log_page(struct nvme_ctrlr *nvme_ctrlr)
    5199                 :            : {
    5200                 :            :         struct nvme_ns *nvme_ns;
    5201                 :            : 
    5202   [ #  #  #  # ]:          1 :         spdk_free(nvme_ctrlr->ana_log_page);
    5203   [ #  #  #  # ]:          1 :         nvme_ctrlr->ana_log_page = NULL;
    5204                 :            : 
    5205         [ #  # ]:          1 :         for (nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
    5206         [ +  + ]:          2 :              nvme_ns != NULL;
    5207                 :          1 :              nvme_ns = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns)) {
    5208   [ #  #  #  # ]:          1 :                 nvme_ns->ana_state_updating = false;
    5209   [ #  #  #  # ]:          1 :                 nvme_ns->ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
    5210                 :          0 :         }
    5211                 :          1 : }
    5212                 :            : 
    5213                 :            : static void
    5214                 :        112 : nvme_ctrlr_read_ana_log_page_done(void *ctx, const struct spdk_nvme_cpl *cpl)
    5215                 :            : {
    5216                 :        112 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    5217                 :            : 
    5218   [ +  -  +  +  :        112 :         if (cpl != NULL && spdk_nvme_cpl_is_success(cpl)) {
          +  -  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    5219                 :        111 :                 bdev_nvme_parse_ana_log_page(nvme_ctrlr, nvme_ctrlr_set_ana_states,
    5220                 :          0 :                                              nvme_ctrlr);
    5221                 :          0 :         } else {
    5222                 :          1 :                 bdev_nvme_disable_read_ana_log_page(nvme_ctrlr);
    5223                 :            :         }
    5224                 :            : 
    5225   [ -  +  #  # ]:        112 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    5226                 :            : 
    5227   [ -  +  #  #  :        112 :         assert(nvme_ctrlr->ana_log_page_updating == true);
                   #  # ]
    5228         [ #  # ]:        112 :         nvme_ctrlr->ana_log_page_updating = false;
    5229                 :            : 
    5230         [ -  + ]:        112 :         if (nvme_ctrlr_can_be_unregistered(nvme_ctrlr)) {
    5231   [ #  #  #  # ]:          0 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    5232                 :            : 
    5233                 :          0 :                 nvme_ctrlr_unregister(nvme_ctrlr);
    5234                 :          0 :         } else {
    5235   [ -  +  #  # ]:        112 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    5236                 :            : 
    5237                 :        112 :                 bdev_nvme_clear_io_path_caches(nvme_ctrlr);
    5238                 :            :         }
    5239                 :        112 : }
    5240                 :            : 
    5241                 :            : static int
    5242                 :       1734 : nvme_ctrlr_read_ana_log_page(struct nvme_ctrlr *nvme_ctrlr)
    5243                 :            : {
    5244                 :            :         uint32_t ana_log_page_size;
    5245                 :            :         int rc;
    5246                 :            : 
    5247   [ -  +  #  #  :       1734 :         if (nvme_ctrlr->ana_log_page == NULL) {
                   #  # ]
    5248                 :          0 :                 return -EINVAL;
    5249                 :            :         }
    5250                 :            : 
    5251                 :       1734 :         ana_log_page_size = nvme_ctrlr_get_ana_log_page_size(nvme_ctrlr);
    5252                 :            : 
    5253   [ -  +  #  #  :       1734 :         if (ana_log_page_size > nvme_ctrlr->max_ana_log_page_size) {
                   #  # ]
    5254   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    5255                 :            :                                   "ANA log page size %" PRIu32 " is larger than allowed %" PRIu32 "\n",
    5256                 :            :                                   ana_log_page_size, nvme_ctrlr->max_ana_log_page_size);
    5257                 :          0 :                 return -EINVAL;
    5258                 :            :         }
    5259                 :            : 
    5260   [ -  +  #  # ]:       1734 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    5261   [ +  +  +  + ]:       1734 :         if (!nvme_ctrlr_is_available(nvme_ctrlr) ||
    5262         [ #  # ]:          0 :             nvme_ctrlr->ana_log_page_updating) {
    5263   [ -  +  #  # ]:       1622 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    5264                 :       1622 :                 return -EBUSY;
    5265                 :            :         }
    5266                 :            : 
    5267         [ #  # ]:        112 :         nvme_ctrlr->ana_log_page_updating = true;
    5268   [ -  +  #  # ]:        112 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    5269                 :            : 
    5270   [ #  #  #  # ]:        112 :         rc = spdk_nvme_ctrlr_cmd_get_log_page(nvme_ctrlr->ctrlr,
    5271                 :            :                                               SPDK_NVME_LOG_ASYMMETRIC_NAMESPACE_ACCESS,
    5272                 :            :                                               SPDK_NVME_GLOBAL_NS_TAG,
    5273   [ #  #  #  # ]:        112 :                                               nvme_ctrlr->ana_log_page,
    5274                 :          0 :                                               ana_log_page_size, 0,
    5275                 :            :                                               nvme_ctrlr_read_ana_log_page_done,
    5276                 :          0 :                                               nvme_ctrlr);
    5277         [ -  + ]:        112 :         if (rc != 0) {
    5278                 :          0 :                 nvme_ctrlr_read_ana_log_page_done(nvme_ctrlr, NULL);
    5279                 :          0 :         }
    5280                 :            : 
    5281                 :        112 :         return rc;
    5282                 :          0 : }
    5283                 :            : 
    5284                 :            : static void
    5285                 :          0 : dummy_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *ctx)
    5286                 :            : {
    5287                 :          0 : }
    5288                 :            : 
    5289                 :            : struct bdev_nvme_set_preferred_path_ctx {
    5290                 :            :         struct spdk_bdev_desc *desc;
    5291                 :            :         struct nvme_ns *nvme_ns;
    5292                 :            :         bdev_nvme_set_preferred_path_cb cb_fn;
    5293                 :            :         void *cb_arg;
    5294                 :            : };
    5295                 :            : 
    5296                 :            : static void
    5297                 :          9 : bdev_nvme_set_preferred_path_done(struct nvme_bdev *nbdev, void *_ctx, int status)
    5298                 :            : {
    5299                 :          9 :         struct bdev_nvme_set_preferred_path_ctx *ctx = _ctx;
    5300                 :            : 
    5301   [ -  +  #  # ]:          9 :         assert(ctx != NULL);
    5302   [ -  +  #  #  :          9 :         assert(ctx->desc != NULL);
             #  #  #  # ]
    5303   [ -  +  #  #  :          9 :         assert(ctx->cb_fn != NULL);
             #  #  #  # ]
    5304                 :            : 
    5305   [ #  #  #  # ]:          9 :         spdk_bdev_close(ctx->desc);
    5306                 :            : 
    5307   [ #  #  #  #  :          9 :         ctx->cb_fn(ctx->cb_arg, status);
          #  #  #  #  #  
                #  #  # ]
    5308                 :            : 
    5309                 :          9 :         free(ctx);
    5310                 :          9 : }
    5311                 :            : 
    5312                 :            : static void
    5313                 :          6 : _bdev_nvme_set_preferred_path(struct nvme_bdev_channel_iter *i,
    5314                 :            :                               struct nvme_bdev *nbdev,
    5315                 :            :                               struct nvme_bdev_channel *nbdev_ch, void *_ctx)
    5316                 :            : {
    5317                 :          6 :         struct bdev_nvme_set_preferred_path_ctx *ctx = _ctx;
    5318                 :            :         struct nvme_io_path *io_path, *prev;
    5319                 :            : 
    5320                 :          6 :         prev = NULL;
    5321   [ +  -  #  #  :          9 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    5322   [ +  +  #  #  :          9 :                 if (io_path->nvme_ns == ctx->nvme_ns) {
          #  #  #  #  #  
                      # ]
    5323                 :          6 :                         break;
    5324                 :            :                 }
    5325                 :          3 :                 prev = io_path;
    5326                 :          0 :         }
    5327                 :            : 
    5328         [ +  - ]:          6 :         if (io_path != NULL) {
    5329         [ +  + ]:          6 :                 if (prev != NULL) {
    5330   [ -  +  #  #  :          3 :                         STAILQ_REMOVE_AFTER(&nbdev_ch->io_path_list, prev, stailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    5331   [ -  +  #  #  :          3 :                         STAILQ_INSERT_HEAD(&nbdev_ch->io_path_list, io_path, stailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    5332                 :          0 :                 }
    5333                 :            : 
    5334                 :            :                 /* We can set io_path to nbdev_ch->current_io_path directly here.
    5335                 :            :                  * However, it needs to be conditional. To simplify the code,
    5336                 :            :                  * just clear nbdev_ch->current_io_path and let find_io_path()
    5337                 :            :                  * fill it.
    5338                 :            :                  *
    5339                 :            :                  * Automatic failback may be disabled. Hence even if the io_path is
    5340                 :            :                  * already at the head, clear nbdev_ch->current_io_path.
    5341                 :            :                  */
    5342                 :          6 :                 bdev_nvme_clear_current_io_path(nbdev_ch);
    5343                 :          0 :         }
    5344                 :            : 
    5345                 :          6 :         nvme_bdev_for_each_channel_continue(i, 0);
    5346                 :          6 : }
    5347                 :            : 
    5348                 :            : static struct nvme_ns *
    5349                 :          9 : bdev_nvme_set_preferred_ns(struct nvme_bdev *nbdev, uint16_t cntlid)
    5350                 :            : {
    5351                 :            :         struct nvme_ns *nvme_ns, *prev;
    5352                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    5353                 :            : 
    5354                 :          9 :         prev = NULL;
    5355   [ +  -  #  #  :         18 :         TAILQ_FOREACH(nvme_ns, &nbdev->nvme_ns_list, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    5356   [ #  #  #  #  :         18 :                 cdata = spdk_nvme_ctrlr_get_data(nvme_ns->ctrlr->ctrlr);
             #  #  #  # ]
    5357                 :            : 
    5358   [ +  +  #  #  :         18 :                 if (cdata->cntlid == cntlid) {
                   #  # ]
    5359                 :          9 :                         break;
    5360                 :            :                 }
    5361                 :          9 :                 prev = nvme_ns;
    5362                 :          0 :         }
    5363                 :            : 
    5364   [ +  -  +  + ]:          9 :         if (nvme_ns != NULL && prev != NULL) {
    5365   [ +  +  #  #  :          6 :                 TAILQ_REMOVE(&nbdev->nvme_ns_list, nvme_ns, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5366   [ +  -  #  #  :          6 :                 TAILQ_INSERT_HEAD(&nbdev->nvme_ns_list, nvme_ns, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    5367                 :          0 :         }
    5368                 :            : 
    5369                 :          9 :         return nvme_ns;
    5370                 :            : }
    5371                 :            : 
    5372                 :            : /* This function supports only multipath mode. There is only a single I/O path
    5373                 :            :  * for each NVMe-oF controller. Hence, just move the matched I/O path to the
    5374                 :            :  * head of the I/O path list for each NVMe bdev channel.
    5375                 :            :  *
    5376                 :            :  * NVMe bdev channel may be acquired after completing this function. move the
    5377                 :            :  * matched namespace to the head of the namespace list for the NVMe bdev too.
    5378                 :            :  */
    5379                 :            : void
    5380                 :          9 : bdev_nvme_set_preferred_path(const char *name, uint16_t cntlid,
    5381                 :            :                              bdev_nvme_set_preferred_path_cb cb_fn, void *cb_arg)
    5382                 :            : {
    5383                 :            :         struct bdev_nvme_set_preferred_path_ctx *ctx;
    5384                 :            :         struct spdk_bdev *bdev;
    5385                 :            :         struct nvme_bdev *nbdev;
    5386                 :          9 :         int rc = 0;
    5387                 :            : 
    5388   [ -  +  #  # ]:          9 :         assert(cb_fn != NULL);
    5389                 :            : 
    5390                 :          9 :         ctx = calloc(1, sizeof(*ctx));
    5391         [ -  + ]:          9 :         if (ctx == NULL) {
    5392                 :          0 :                 SPDK_ERRLOG("Failed to alloc context.\n");
    5393                 :          0 :                 rc = -ENOMEM;
    5394                 :          0 :                 goto err_alloc;
    5395                 :            :         }
    5396                 :            : 
    5397   [ #  #  #  # ]:          9 :         ctx->cb_fn = cb_fn;
    5398   [ #  #  #  # ]:          9 :         ctx->cb_arg = cb_arg;
    5399                 :            : 
    5400         [ #  # ]:          9 :         rc = spdk_bdev_open_ext(name, false, dummy_bdev_event_cb, NULL, &ctx->desc);
    5401         [ -  + ]:          9 :         if (rc != 0) {
    5402                 :          0 :                 SPDK_ERRLOG("Failed to open bdev %s.\n", name);
    5403                 :          0 :                 goto err_open;
    5404                 :            :         }
    5405                 :            : 
    5406   [ #  #  #  # ]:          9 :         bdev = spdk_bdev_desc_get_bdev(ctx->desc);
    5407                 :            : 
    5408   [ -  +  #  #  :          9 :         if (bdev->module != &nvme_if) {
                   #  # ]
    5409                 :          0 :                 SPDK_ERRLOG("bdev %s is not registered in this module.\n", name);
    5410                 :          0 :                 rc = -ENODEV;
    5411                 :          0 :                 goto err_bdev;
    5412                 :            :         }
    5413                 :            : 
    5414                 :          9 :         nbdev = SPDK_CONTAINEROF(bdev, struct nvme_bdev, disk);
    5415                 :            : 
    5416   [ -  +  #  # ]:          9 :         pthread_mutex_lock(&nbdev->mutex);
    5417                 :            : 
    5418   [ #  #  #  # ]:          9 :         ctx->nvme_ns = bdev_nvme_set_preferred_ns(nbdev, cntlid);
    5419   [ -  +  #  #  :          9 :         if (ctx->nvme_ns == NULL) {
                   #  # ]
    5420   [ #  #  #  # ]:          0 :                 pthread_mutex_unlock(&nbdev->mutex);
    5421                 :            : 
    5422                 :          0 :                 SPDK_ERRLOG("bdev %s does not have namespace to controller %u.\n", name, cntlid);
    5423                 :          0 :                 rc = -ENODEV;
    5424                 :          0 :                 goto err_bdev;
    5425                 :            :         }
    5426                 :            : 
    5427   [ -  +  #  # ]:          9 :         pthread_mutex_unlock(&nbdev->mutex);
    5428                 :            : 
    5429                 :          9 :         nvme_bdev_for_each_channel(nbdev,
    5430                 :            :                                    _bdev_nvme_set_preferred_path,
    5431                 :          0 :                                    ctx,
    5432                 :            :                                    bdev_nvme_set_preferred_path_done);
    5433                 :          9 :         return;
    5434                 :            : 
    5435                 :          0 : err_bdev:
    5436   [ #  #  #  # ]:          0 :         spdk_bdev_close(ctx->desc);
    5437                 :          0 : err_open:
    5438                 :          0 :         free(ctx);
    5439                 :          0 : err_alloc:
    5440   [ #  #  #  # ]:          0 :         cb_fn(cb_arg, rc);
    5441                 :          0 : }
    5442                 :            : 
    5443                 :            : struct bdev_nvme_set_multipath_policy_ctx {
    5444                 :            :         struct spdk_bdev_desc *desc;
    5445                 :            :         spdk_bdev_nvme_set_multipath_policy_cb cb_fn;
    5446                 :            :         void *cb_arg;
    5447                 :            : };
    5448                 :            : 
    5449                 :            : static void
    5450                 :         13 : bdev_nvme_set_multipath_policy_done(struct nvme_bdev *nbdev, void *_ctx, int status)
    5451                 :            : {
    5452                 :         13 :         struct bdev_nvme_set_multipath_policy_ctx *ctx = _ctx;
    5453                 :            : 
    5454   [ -  +  #  # ]:         13 :         assert(ctx != NULL);
    5455   [ -  +  #  #  :         13 :         assert(ctx->desc != NULL);
             #  #  #  # ]
    5456   [ -  +  #  #  :         13 :         assert(ctx->cb_fn != NULL);
             #  #  #  # ]
    5457                 :            : 
    5458   [ #  #  #  # ]:         13 :         spdk_bdev_close(ctx->desc);
    5459                 :            : 
    5460   [ #  #  #  #  :         13 :         ctx->cb_fn(ctx->cb_arg, status);
          #  #  #  #  #  
                #  #  # ]
    5461                 :            : 
    5462                 :         13 :         free(ctx);
    5463                 :         13 : }
    5464                 :            : 
    5465                 :            : static void
    5466                 :          7 : _bdev_nvme_set_multipath_policy(struct nvme_bdev_channel_iter *i,
    5467                 :            :                                 struct nvme_bdev *nbdev,
    5468                 :            :                                 struct nvme_bdev_channel *nbdev_ch, void *ctx)
    5469                 :            : {
    5470   [ #  #  #  #  :          7 :         nbdev_ch->mp_policy = nbdev->mp_policy;
             #  #  #  # ]
    5471   [ #  #  #  #  :          7 :         nbdev_ch->mp_selector = nbdev->mp_selector;
             #  #  #  # ]
    5472   [ #  #  #  #  :          7 :         nbdev_ch->rr_min_io = nbdev->rr_min_io;
             #  #  #  # ]
    5473                 :          7 :         bdev_nvme_clear_current_io_path(nbdev_ch);
    5474                 :            : 
    5475                 :          7 :         nvme_bdev_for_each_channel_continue(i, 0);
    5476                 :          7 : }
    5477                 :            : 
    5478                 :            : void
    5479                 :         13 : spdk_bdev_nvme_set_multipath_policy(const char *name, enum spdk_bdev_nvme_multipath_policy policy,
    5480                 :            :                                     enum spdk_bdev_nvme_multipath_selector selector, uint32_t rr_min_io,
    5481                 :            :                                     spdk_bdev_nvme_set_multipath_policy_cb cb_fn, void *cb_arg)
    5482                 :            : {
    5483                 :            :         struct bdev_nvme_set_multipath_policy_ctx *ctx;
    5484                 :            :         struct spdk_bdev *bdev;
    5485                 :            :         struct nvme_bdev *nbdev;
    5486                 :            :         int rc;
    5487                 :            : 
    5488   [ -  +  #  # ]:         13 :         assert(cb_fn != NULL);
    5489                 :            : 
    5490      [ +  +  - ]:         13 :         switch (policy) {
    5491                 :          3 :         case BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE:
    5492                 :          3 :                 break;
    5493      [ +  +  - ]:         10 :         case BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE:
    5494      [ +  -  - ]:          4 :                 switch (selector) {
    5495                 :          7 :                 case BDEV_NVME_MP_SELECTOR_ROUND_ROBIN:
    5496         [ +  + ]:          7 :                         if (rr_min_io == UINT32_MAX) {
    5497                 :          4 :                                 rr_min_io = 1;
    5498         [ -  + ]:          3 :                         } else if (rr_min_io == 0) {
    5499                 :          0 :                                 rc = -EINVAL;
    5500                 :          0 :                                 goto exit;
    5501                 :            :                         }
    5502                 :          7 :                         break;
    5503                 :          3 :                 case BDEV_NVME_MP_SELECTOR_QUEUE_DEPTH:
    5504                 :          3 :                         break;
    5505                 :          0 :                 default:
    5506                 :          0 :                         rc = -EINVAL;
    5507                 :          0 :                         goto exit;
    5508                 :            :                 }
    5509                 :         10 :                 break;
    5510                 :          0 :         default:
    5511                 :          0 :                 rc = -EINVAL;
    5512                 :          0 :                 goto exit;
    5513                 :            :         }
    5514                 :            : 
    5515                 :         13 :         ctx = calloc(1, sizeof(*ctx));
    5516         [ -  + ]:         13 :         if (ctx == NULL) {
    5517                 :          0 :                 SPDK_ERRLOG("Failed to alloc context.\n");
    5518                 :          0 :                 rc = -ENOMEM;
    5519                 :          0 :                 goto exit;
    5520                 :            :         }
    5521                 :            : 
    5522   [ #  #  #  # ]:         13 :         ctx->cb_fn = cb_fn;
    5523   [ #  #  #  # ]:         13 :         ctx->cb_arg = cb_arg;
    5524                 :            : 
    5525         [ #  # ]:         13 :         rc = spdk_bdev_open_ext(name, false, dummy_bdev_event_cb, NULL, &ctx->desc);
    5526         [ -  + ]:         13 :         if (rc != 0) {
    5527                 :          0 :                 SPDK_ERRLOG("Failed to open bdev %s.\n", name);
    5528                 :          0 :                 rc = -ENODEV;
    5529                 :          0 :                 goto err_open;
    5530                 :            :         }
    5531                 :            : 
    5532   [ #  #  #  # ]:         13 :         bdev = spdk_bdev_desc_get_bdev(ctx->desc);
    5533   [ -  +  #  #  :         13 :         if (bdev->module != &nvme_if) {
                   #  # ]
    5534                 :          0 :                 SPDK_ERRLOG("bdev %s is not registered in this module.\n", name);
    5535                 :          0 :                 rc = -ENODEV;
    5536                 :          0 :                 goto err_module;
    5537                 :            :         }
    5538                 :         13 :         nbdev = SPDK_CONTAINEROF(bdev, struct nvme_bdev, disk);
    5539                 :            : 
    5540   [ -  +  #  # ]:         13 :         pthread_mutex_lock(&nbdev->mutex);
    5541   [ #  #  #  # ]:         13 :         nbdev->mp_policy = policy;
    5542   [ #  #  #  # ]:         13 :         nbdev->mp_selector = selector;
    5543   [ #  #  #  # ]:         13 :         nbdev->rr_min_io = rr_min_io;
    5544   [ -  +  #  # ]:         13 :         pthread_mutex_unlock(&nbdev->mutex);
    5545                 :            : 
    5546                 :         13 :         nvme_bdev_for_each_channel(nbdev,
    5547                 :            :                                    _bdev_nvme_set_multipath_policy,
    5548                 :          0 :                                    ctx,
    5549                 :            :                                    bdev_nvme_set_multipath_policy_done);
    5550                 :         13 :         return;
    5551                 :            : 
    5552                 :          0 : err_module:
    5553   [ #  #  #  # ]:          0 :         spdk_bdev_close(ctx->desc);
    5554                 :          0 : err_open:
    5555                 :          0 :         free(ctx);
    5556                 :          0 : exit:
    5557   [ #  #  #  # ]:          0 :         cb_fn(cb_arg, rc);
    5558                 :          0 : }
    5559                 :            : 
    5560                 :            : static void
    5561                 :        139 : aer_cb(void *arg, const struct spdk_nvme_cpl *cpl)
    5562                 :            : {
    5563                 :        139 :         struct nvme_ctrlr *nvme_ctrlr           = arg;
    5564                 :            :         union spdk_nvme_async_event_completion  event;
    5565                 :            : 
    5566   [ +  +  -  +  :        139 :         if (spdk_nvme_cpl_is_error(cpl)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    5567                 :          4 :                 SPDK_WARNLOG("AER request execute failed\n");
    5568                 :          4 :                 return;
    5569                 :            :         }
    5570                 :            : 
    5571   [ #  #  #  # ]:        135 :         event.raw = cpl->cdw0;
    5572   [ +  -  #  # ]:        135 :         if ((event.bits.async_event_type == SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) &&
    5573         [ +  + ]:        135 :             (event.bits.async_event_info == SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGED)) {
    5574                 :         32 :                 nvme_ctrlr_populate_namespaces(nvme_ctrlr, NULL);
    5575   [ +  -  #  # ]:        103 :         } else if ((event.bits.async_event_type == SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) &&
    5576         [ +  - ]:        103 :                    (event.bits.async_event_info == SPDK_NVME_ASYNC_EVENT_ANA_CHANGE)) {
    5577                 :        103 :                 nvme_ctrlr_read_ana_log_page(nvme_ctrlr);
    5578                 :          0 :         }
    5579                 :          0 : }
    5580                 :            : 
    5581                 :            : static void
    5582                 :       1727 : free_nvme_async_probe_ctx(struct nvme_async_probe_ctx *ctx)
    5583                 :            : {
    5584   [ +  -  +  -  :       1727 :         spdk_keyring_put_key(ctx->drv_opts.tls_psk);
                   +  - ]
    5585   [ +  -  +  -  :       1727 :         spdk_keyring_put_key(ctx->drv_opts.dhchap_key);
                   +  - ]
    5586   [ +  -  +  -  :       1727 :         spdk_keyring_put_key(ctx->drv_opts.dhchap_ctrlr_key);
                   +  - ]
    5587                 :       1727 :         free(ctx);
    5588                 :       1727 : }
    5589                 :            : 
    5590                 :            : static void
    5591                 :       1718 : populate_namespaces_cb(struct nvme_async_probe_ctx *ctx, int rc)
    5592                 :            : {
    5593   [ +  -  +  -  :       1718 :         if (ctx->cb_fn) {
                   +  - ]
    5594   [ +  -  +  -  :       1718 :                 ctx->cb_fn(ctx->cb_ctx, ctx->reported_bdevs, rc);
          -  +  +  -  +  
          -  +  -  +  -  
                   +  - ]
    5595                 :          1 :         }
    5596                 :            : 
    5597   [ +  -  +  - ]:       1718 :         ctx->namespaces_populated = true;
    5598   [ +  +  +  +  :       1718 :         if (ctx->probe_done) {
             +  -  +  - ]
    5599                 :            :                 /* The probe was already completed, so we need to free the context
    5600                 :            :                  * here.  This can happen for cases like OCSSD, where we need to
    5601                 :            :                  * send additional commands to the SSD after attach.
    5602                 :            :                  */
    5603                 :        482 :                 free_nvme_async_probe_ctx(ctx);
    5604                 :          0 :         }
    5605                 :       1718 : }
    5606                 :            : 
    5607                 :            : static int
    5608                 :      67416 : bdev_nvme_remove_poller(void *ctx)
    5609                 :            : {
    5610                 :      31509 :         struct spdk_nvme_transport_id trid_pcie;
    5611                 :            : 
    5612         [ +  + ]:      67416 :         if (TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) {
    5613                 :        867 :                 spdk_poller_unregister(&g_hotplug_poller);
    5614                 :        867 :                 return SPDK_POLLER_IDLE;
    5615                 :            :         }
    5616                 :            : 
    5617         [ +  + ]:      66549 :         memset(&trid_pcie, 0, sizeof(trid_pcie));
    5618                 :      66549 :         spdk_nvme_trid_populate_transport(&trid_pcie, SPDK_NVME_TRANSPORT_PCIE);
    5619                 :            : 
    5620         [ +  + ]:      66549 :         if (spdk_nvme_scan_attached(&trid_pcie)) {
    5621   [ #  #  #  # ]:          0 :                 SPDK_ERRLOG_RATELIMIT("spdk_nvme_scan_attached() failed\n");
    5622                 :          0 :         }
    5623                 :            : 
    5624                 :      66549 :         return SPDK_POLLER_BUSY;
    5625                 :         29 : }
    5626                 :            : 
    5627                 :            : static void
    5628                 :       1727 : nvme_ctrlr_create_done(struct nvme_ctrlr *nvme_ctrlr,
    5629                 :            :                        struct nvme_async_probe_ctx *ctx)
    5630                 :            : {
    5631   [ +  -  +  -  :       1727 :         struct spdk_nvme_transport_id *trid = &nvme_ctrlr->active_path_id->trid;
                   +  - ]
    5632                 :            : 
    5633   [ +  +  +  -  :       1727 :         if (spdk_nvme_trtype_is_fabrics(trid->trtype)) {
                   -  + ]
    5634   [ -  +  +  +  :       1157 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "ctrlr was created to %s:%s\n",
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5635                 :            :                                    trid->traddr, trid->trsvcid);
    5636                 :          0 :         } else {
    5637   [ +  +  +  +  :        570 :                 NVME_CTRLR_INFOLOG(nvme_ctrlr, "ctrlr was created\n");
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    5638                 :            :         }
    5639                 :            : 
    5640                 :       1728 :         spdk_io_device_register(nvme_ctrlr,
    5641                 :            :                                 bdev_nvme_create_ctrlr_channel_cb,
    5642                 :            :                                 bdev_nvme_destroy_ctrlr_channel_cb,
    5643                 :            :                                 sizeof(struct nvme_ctrlr_channel),
    5644   [ +  -  +  -  :       1727 :                                 nvme_ctrlr->nbdev_ctrlr->name);
             +  -  +  - ]
    5645                 :            : 
    5646                 :       1727 :         nvme_ctrlr_populate_namespaces(nvme_ctrlr, ctx);
    5647                 :            : 
    5648         [ +  + ]:       1727 :         if (g_hotplug_poller == NULL) {
    5649                 :       1277 :                 g_hotplug_poller = SPDK_POLLER_REGISTER(bdev_nvme_remove_poller, NULL,
    5650                 :            :                                                         NVME_HOTPLUG_POLL_PERIOD_DEFAULT);
    5651                 :          1 :         }
    5652                 :       1727 : }
    5653                 :            : 
    5654                 :            : static void
    5655                 :        417 : nvme_ctrlr_init_ana_log_page_done(void *_ctx, const struct spdk_nvme_cpl *cpl)
    5656                 :            : {
    5657                 :        417 :         struct nvme_ctrlr *nvme_ctrlr = _ctx;
    5658   [ #  #  #  # ]:        417 :         struct nvme_async_probe_ctx *ctx = nvme_ctrlr->probe_ctx;
    5659                 :            : 
    5660   [ #  #  #  # ]:        417 :         nvme_ctrlr->probe_ctx = NULL;
    5661                 :            : 
    5662   [ +  -  -  +  :        417 :         if (spdk_nvme_cpl_is_error(cpl)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    5663                 :          0 :                 nvme_ctrlr_delete(nvme_ctrlr);
    5664                 :            : 
    5665         [ #  # ]:          0 :                 if (ctx != NULL) {
    5666   [ #  #  #  # ]:          0 :                         ctx->reported_bdevs = 0;
    5667                 :          0 :                         populate_namespaces_cb(ctx, -1);
    5668                 :          0 :                 }
    5669                 :          0 :                 return;
    5670                 :            :         }
    5671                 :            : 
    5672                 :        417 :         nvme_ctrlr_create_done(nvme_ctrlr, ctx);
    5673                 :          0 : }
    5674                 :            : 
    5675                 :            : static int
    5676                 :        417 : nvme_ctrlr_init_ana_log_page(struct nvme_ctrlr *nvme_ctrlr,
    5677                 :            :                              struct nvme_async_probe_ctx *ctx)
    5678                 :            : {
    5679   [ #  #  #  # ]:        417 :         struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
    5680                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    5681                 :            :         uint32_t ana_log_page_size;
    5682                 :            : 
    5683                 :        417 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    5684                 :            : 
    5685                 :            :         /* Set buffer size enough to include maximum number of allowed namespaces. */
    5686   [ #  #  #  # ]:        417 :         ana_log_page_size = sizeof(struct spdk_nvme_ana_page) + cdata->nanagrpid *
    5687   [ #  #  #  # ]:        417 :                             sizeof(struct spdk_nvme_ana_group_descriptor) + cdata->mnan *
    5688                 :            :                             sizeof(uint32_t);
    5689                 :            : 
    5690   [ #  #  #  # ]:        417 :         nvme_ctrlr->ana_log_page = spdk_zmalloc(ana_log_page_size, 64, NULL,
    5691                 :            :                                                 SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
    5692   [ -  +  #  #  :        417 :         if (nvme_ctrlr->ana_log_page == NULL) {
                   #  # ]
    5693   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "could not allocate ANA log page buffer\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5694                 :          0 :                 return -ENXIO;
    5695                 :            :         }
    5696                 :            : 
    5697                 :            :         /* Each descriptor in a ANA log page is not ensured to be 8-bytes aligned.
    5698                 :            :          * Hence copy each descriptor to a temporary area when parsing it.
    5699                 :            :          *
    5700                 :            :          * Allocate a buffer whose size is as large as ANA log page buffer because
    5701                 :            :          * we do not know the size of a descriptor until actually reading it.
    5702                 :            :          */
    5703   [ #  #  #  # ]:        417 :         nvme_ctrlr->copied_ana_desc = calloc(1, ana_log_page_size);
    5704   [ -  +  #  #  :        417 :         if (nvme_ctrlr->copied_ana_desc == NULL) {
                   #  # ]
    5705   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "could not allocate a buffer to parse ANA descriptor\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5706                 :          0 :                 return -ENOMEM;
    5707                 :            :         }
    5708                 :            : 
    5709   [ #  #  #  # ]:        417 :         nvme_ctrlr->max_ana_log_page_size = ana_log_page_size;
    5710                 :            : 
    5711   [ #  #  #  # ]:        417 :         nvme_ctrlr->probe_ctx = ctx;
    5712                 :            : 
    5713                 :            :         /* Then, set the read size only to include the current active namespaces. */
    5714                 :        417 :         ana_log_page_size = nvme_ctrlr_get_ana_log_page_size(nvme_ctrlr);
    5715                 :            : 
    5716   [ -  +  #  #  :        417 :         if (ana_log_page_size > nvme_ctrlr->max_ana_log_page_size) {
                   #  # ]
    5717   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "ANA log page size %" PRIu32 " is larger than allowed %" PRIu32 "\n",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    5718                 :            :                                   ana_log_page_size, nvme_ctrlr->max_ana_log_page_size);
    5719                 :          0 :                 return -EINVAL;
    5720                 :            :         }
    5721                 :            : 
    5722                 :        417 :         return spdk_nvme_ctrlr_cmd_get_log_page(ctrlr,
    5723                 :            :                                                 SPDK_NVME_LOG_ASYMMETRIC_NAMESPACE_ACCESS,
    5724                 :            :                                                 SPDK_NVME_GLOBAL_NS_TAG,
    5725   [ #  #  #  # ]:        417 :                                                 nvme_ctrlr->ana_log_page,
    5726                 :          0 :                                                 ana_log_page_size, 0,
    5727                 :            :                                                 nvme_ctrlr_init_ana_log_page_done,
    5728                 :          0 :                                                 nvme_ctrlr);
    5729                 :          0 : }
    5730                 :            : 
    5731                 :            : /* hostnqn and subnqn were already verified before attaching a controller.
    5732                 :            :  * Hence check only the multipath capability and cntlid here.
    5733                 :            :  */
    5734                 :            : static bool
    5735                 :         61 : bdev_nvme_check_multipath(struct nvme_bdev_ctrlr *nbdev_ctrlr, struct spdk_nvme_ctrlr *ctrlr)
    5736                 :            : {
    5737                 :            :         struct nvme_ctrlr *tmp;
    5738                 :            :         const struct spdk_nvme_ctrlr_data *cdata, *tmp_cdata;
    5739                 :            : 
    5740                 :         61 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    5741                 :            : 
    5742   [ -  +  #  #  :         61 :         if (!cdata->cmic.multi_ctrlr) {
                   #  # ]
    5743   [ #  #  #  # ]:          0 :                 SPDK_ERRLOG("Ctrlr%u does not support multipath.\n", cdata->cntlid);
    5744                 :          0 :                 return false;
    5745                 :            :         }
    5746                 :            : 
    5747   [ +  +  #  #  :        125 :         TAILQ_FOREACH(tmp, &nbdev_ctrlr->ctrlrs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    5748   [ #  #  #  # ]:         67 :                 tmp_cdata = spdk_nvme_ctrlr_get_data(tmp->ctrlr);
    5749                 :            : 
    5750   [ -  +  #  #  :         67 :                 if (!tmp_cdata->cmic.multi_ctrlr) {
                   #  # ]
    5751   [ #  #  #  #  :          0 :                         NVME_CTRLR_ERRLOG(tmp, "Ctrlr%u does not support multipath.\n", cdata->cntlid);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    5752                 :          0 :                         return false;
    5753                 :            :                 }
    5754   [ +  +  #  #  :         67 :                 if (cdata->cntlid == tmp_cdata->cntlid) {
          #  #  #  #  #  
                      # ]
    5755   [ +  -  #  #  :          3 :                         NVME_CTRLR_ERRLOG(tmp, "cntlid %u are duplicated.\n", tmp_cdata->cntlid);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    5756                 :          3 :                         return false;
    5757                 :            :                 }
    5758                 :          0 :         }
    5759                 :            : 
    5760                 :         58 :         return true;
    5761                 :          0 : }
    5762                 :            : 
    5763                 :            : 
    5764                 :            : static int
    5765                 :       1730 : nvme_bdev_ctrlr_create(const char *name, struct nvme_ctrlr *nvme_ctrlr)
    5766                 :            : {
    5767                 :            :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
    5768   [ +  -  +  - ]:       1730 :         struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
    5769                 :            :         struct nvme_ctrlr      *nctrlr;
    5770                 :       1730 :         int rc = 0;
    5771                 :            : 
    5772         [ +  + ]:       1730 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    5773                 :            : 
    5774                 :       1730 :         nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
    5775         [ +  + ]:       1730 :         if (nbdev_ctrlr != NULL) {
    5776         [ +  + ]:         61 :                 if (!bdev_nvme_check_multipath(nbdev_ctrlr, ctrlr)) {
    5777                 :          3 :                         rc = -EINVAL;
    5778                 :          3 :                         goto exit;
    5779                 :            :                 }
    5780   [ +  +  #  #  :        122 :                 TAILQ_FOREACH(nctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    5781   [ -  +  -  +  :         64 :                         if (nctrlr->opts.multipath != nvme_ctrlr->opts.multipath) {
          -  +  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    5782                 :            :                                 /* All controllers with the same name must be configured the same
    5783                 :            :                                  * way, either for multipath or failover. If the configuration doesn't
    5784                 :            :                                  * match - report error.
    5785                 :            :                                  */
    5786                 :          0 :                                 rc = -EINVAL;
    5787                 :          0 :                                 goto exit;
    5788                 :            :                         }
    5789                 :          0 :                 }
    5790                 :          0 :         } else {
    5791                 :       1669 :                 nbdev_ctrlr = calloc(1, sizeof(*nbdev_ctrlr));
    5792         [ +  + ]:       1669 :                 if (nbdev_ctrlr == NULL) {
    5793   [ #  #  #  #  :          0 :                         NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to allocate nvme_bdev_ctrlr.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5794                 :          0 :                         rc = -ENOMEM;
    5795                 :          0 :                         goto exit;
    5796                 :            :                 }
    5797   [ +  +  +  -  :       1669 :                 nbdev_ctrlr->name = strdup(name);
                   +  - ]
    5798   [ +  +  +  -  :       1669 :                 if (nbdev_ctrlr->name == NULL) {
                   +  - ]
    5799   [ #  #  #  #  :          0 :                         NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to allocate name of nvme_bdev_ctrlr.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    5800                 :          0 :                         free(nbdev_ctrlr);
    5801                 :          0 :                         goto exit;
    5802                 :            :                 }
    5803   [ +  -  +  -  :       1669 :                 TAILQ_INIT(&nbdev_ctrlr->ctrlrs);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    5804   [ +  -  +  -  :       1669 :                 TAILQ_INIT(&nbdev_ctrlr->bdevs);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    5805   [ +  -  +  -  :       1669 :                 TAILQ_INSERT_TAIL(&g_nvme_bdev_ctrlrs, nbdev_ctrlr, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    5806                 :            :         }
    5807   [ +  -  +  - ]:       1727 :         nvme_ctrlr->nbdev_ctrlr = nbdev_ctrlr;
    5808   [ +  -  +  -  :       1727 :         TAILQ_INSERT_TAIL(&nbdev_ctrlr->ctrlrs, nvme_ctrlr, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    5809                 :       1729 : exit:
    5810         [ +  + ]:       1730 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    5811                 :       1730 :         return rc;
    5812                 :            : }
    5813                 :            : 
    5814                 :            : static int
    5815                 :       1730 : nvme_ctrlr_create(struct spdk_nvme_ctrlr *ctrlr,
    5816                 :            :                   const char *name,
    5817                 :            :                   const struct spdk_nvme_transport_id *trid,
    5818                 :            :                   struct nvme_async_probe_ctx *ctx)
    5819                 :            : {
    5820                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    5821                 :            :         struct nvme_path_id *path_id;
    5822                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    5823                 :       1730 :         struct spdk_event_handler_opts opts = {
    5824                 :            :                 .opts_size = SPDK_SIZEOF(&opts, fd_type),
    5825                 :            :         };
    5826                 :            :         uint64_t period;
    5827                 :            :         int fd, rc;
    5828                 :            : 
    5829                 :       1730 :         nvme_ctrlr = calloc(1, sizeof(*nvme_ctrlr));
    5830         [ +  + ]:       1730 :         if (nvme_ctrlr == NULL) {
    5831                 :          0 :                 SPDK_ERRLOG("Failed to allocate device struct\n");
    5832                 :          0 :                 return -ENOMEM;
    5833                 :            :         }
    5834                 :            : 
    5835   [ +  +  +  - ]:       1730 :         rc = pthread_mutex_init(&nvme_ctrlr->mutex, NULL);
    5836         [ -  + ]:       1730 :         if (rc != 0) {
    5837                 :          0 :                 free(nvme_ctrlr);
    5838                 :          0 :                 return rc;
    5839                 :            :         }
    5840                 :            : 
    5841   [ +  -  +  -  :       1730 :         TAILQ_INIT(&nvme_ctrlr->trids);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    5842   [ +  -  +  -  :       1730 :         TAILQ_INIT(&nvme_ctrlr->pending_resets);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    5843   [ +  -  +  -  :       1730 :         RB_INIT(&nvme_ctrlr->namespaces);
                   +  - ]
    5844                 :            : 
    5845                 :            :         /* Get another reference to the key, so the first one can be released from probe_ctx */
    5846         [ +  + ]:       1730 :         if (ctx != NULL) {
    5847   [ +  +  +  -  :       1632 :                 if (ctx->drv_opts.tls_psk != NULL) {
             +  -  +  - ]
    5848   [ #  #  #  # ]:         42 :                         nvme_ctrlr->psk = spdk_keyring_get_key(
    5849   [ #  #  #  #  :          0 :                                                   spdk_key_get_name(ctx->drv_opts.tls_psk));
                   #  # ]
    5850   [ -  +  #  #  :         42 :                         if (nvme_ctrlr->psk == NULL) {
                   #  # ]
    5851                 :            :                                 /* Could only happen if the key was removed in the meantime */
    5852   [ #  #  #  #  :          0 :                                 SPDK_ERRLOG("Couldn't get a reference to the key '%s'\n",
                   #  # ]
    5853                 :            :                                             spdk_key_get_name(ctx->drv_opts.tls_psk));
    5854                 :          0 :                                 rc = -ENOKEY;
    5855                 :          0 :                                 goto err;
    5856                 :            :                         }
    5857                 :          0 :                 }
    5858                 :            : 
    5859   [ +  +  +  -  :       1632 :                 if (ctx->drv_opts.dhchap_key != NULL) {
             +  -  +  - ]
    5860   [ #  #  #  # ]:        628 :                         nvme_ctrlr->dhchap_key = spdk_keyring_get_key(
    5861   [ #  #  #  #  :          0 :                                                          spdk_key_get_name(ctx->drv_opts.dhchap_key));
                   #  # ]
    5862   [ -  +  #  #  :        628 :                         if (nvme_ctrlr->dhchap_key == NULL) {
                   #  # ]
    5863   [ #  #  #  #  :          0 :                                 SPDK_ERRLOG("Couldn't get a reference to the key '%s'\n",
                   #  # ]
    5864                 :            :                                             spdk_key_get_name(ctx->drv_opts.dhchap_key));
    5865                 :          0 :                                 rc = -ENOKEY;
    5866                 :          0 :                                 goto err;
    5867                 :            :                         }
    5868                 :          0 :                 }
    5869                 :            : 
    5870   [ +  +  +  -  :       1632 :                 if (ctx->drv_opts.dhchap_ctrlr_key != NULL) {
             +  -  +  - ]
    5871   [ #  #  #  # ]:        484 :                         nvme_ctrlr->dhchap_ctrlr_key =
    5872                 :        484 :                                 spdk_keyring_get_key(
    5873   [ #  #  #  #  :          0 :                                         spdk_key_get_name(ctx->drv_opts.dhchap_ctrlr_key));
                   #  # ]
    5874   [ -  +  #  #  :        484 :                         if (nvme_ctrlr->dhchap_ctrlr_key == NULL) {
                   #  # ]
    5875   [ #  #  #  #  :          0 :                                 SPDK_ERRLOG("Couldn't get a reference to the key '%s'\n",
                   #  # ]
    5876                 :            :                                             spdk_key_get_name(ctx->drv_opts.dhchap_ctrlr_key));
    5877                 :          0 :                                 rc = -ENOKEY;
    5878                 :          0 :                                 goto err;
    5879                 :            :                         }
    5880                 :          0 :                 }
    5881                 :          1 :         }
    5882                 :            : 
    5883                 :            :         /* Check if we manage to enable interrupts on the controller. */
    5884   [ +  +  +  -  :       1730 :         if (spdk_interrupt_mode_is_enabled() && ctx != NULL && !ctx->drv_opts.enable_interrupts) {
          -  +  -  +  #  
             #  #  #  #  
                      # ]
    5885                 :          0 :                 SPDK_ERRLOG("Failed to enable interrupts on the controller\n");
    5886                 :          0 :                 rc = -ENOTSUP;
    5887                 :          0 :                 goto err;
    5888                 :            :         }
    5889                 :            : 
    5890                 :       1730 :         path_id = calloc(1, sizeof(*path_id));
    5891         [ -  + ]:       1730 :         if (path_id == NULL) {
    5892                 :          0 :                 SPDK_ERRLOG("Failed to allocate trid entry pointer\n");
    5893                 :          0 :                 rc = -ENOMEM;
    5894                 :          0 :                 goto err;
    5895                 :            :         }
    5896                 :            : 
    5897         [ +  - ]:       1730 :         path_id->trid = *trid;
    5898         [ +  + ]:       1730 :         if (ctx != NULL) {
    5899   [ +  +  +  +  :       1632 :                 memcpy(path_id->hostid.hostaddr, ctx->drv_opts.src_addr, sizeof(path_id->hostid.hostaddr));
          +  -  +  -  +  
                -  +  - ]
    5900   [ +  +  +  +  :       1632 :                 memcpy(path_id->hostid.hostsvcid, ctx->drv_opts.src_svcid, sizeof(path_id->hostid.hostsvcid));
          +  -  +  -  +  
                -  +  - ]
    5901                 :          1 :         }
    5902   [ +  -  +  - ]:       1730 :         nvme_ctrlr->active_path_id = path_id;
    5903   [ +  +  +  -  :       1730 :         TAILQ_INSERT_HEAD(&nvme_ctrlr->trids, path_id, link);
          +  -  +  -  +  
          -  +  -  -  +  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    5904                 :            : 
    5905   [ +  -  +  - ]:       1730 :         nvme_ctrlr->thread = spdk_get_thread();
    5906   [ +  -  +  - ]:       1730 :         nvme_ctrlr->ctrlr = ctrlr;
    5907   [ +  -  +  - ]:       1730 :         nvme_ctrlr->ref = 1;
    5908                 :            : 
    5909         [ -  + ]:       1730 :         if (spdk_nvme_ctrlr_is_ocssd_supported(ctrlr)) {
    5910                 :          0 :                 SPDK_ERRLOG("OCSSDs are not supported");
    5911                 :          0 :                 rc = -ENOTSUP;
    5912                 :          0 :                 goto err;
    5913                 :            :         }
    5914                 :            : 
    5915         [ +  + ]:       1730 :         if (ctx != NULL) {
    5916   [ +  +  +  +  :       1632 :                 memcpy(&nvme_ctrlr->opts, &ctx->bdev_opts, sizeof(ctx->bdev_opts));
             +  -  +  - ]
    5917                 :          1 :         } else {
    5918         [ #  # ]:         98 :                 spdk_bdev_nvme_get_default_ctrlr_opts(&nvme_ctrlr->opts);
    5919                 :            :         }
    5920                 :            : 
    5921   [ +  +  +  - ]:       1730 :         period = spdk_interrupt_mode_is_enabled() ? 0 : g_opts.nvme_adminq_poll_period_us;
    5922                 :            : 
    5923   [ +  -  +  - ]:       1730 :         nvme_ctrlr->adminq_timer_poller = SPDK_POLLER_REGISTER(bdev_nvme_poll_adminq, nvme_ctrlr,
    5924                 :            :                                           period);
    5925                 :            : 
    5926         [ +  + ]:       1730 :         if (spdk_interrupt_mode_is_enabled()) {
    5927   [ #  #  #  # ]:          1 :                 spdk_poller_register_interrupt(nvme_ctrlr->adminq_timer_poller, NULL, NULL);
    5928                 :            : 
    5929   [ #  #  #  # ]:          1 :                 fd = spdk_nvme_ctrlr_get_admin_qp_fd(nvme_ctrlr->ctrlr, &opts);
    5930         [ -  + ]:          1 :                 if (fd < 0) {
    5931                 :          0 :                         rc = fd;
    5932                 :          0 :                         goto err;
    5933                 :            :                 }
    5934                 :            : 
    5935   [ #  #  #  # ]:          1 :                 nvme_ctrlr->intr = SPDK_INTERRUPT_REGISTER_EXT(fd, bdev_nvme_poll_adminq,
    5936                 :            :                                    nvme_ctrlr, &opts);
    5937   [ -  +  #  #  :          1 :                 if (!nvme_ctrlr->intr) {
                   #  # ]
    5938                 :          0 :                         rc = -EINVAL;
    5939                 :          0 :                         goto err;
    5940                 :            :                 }
    5941                 :          0 :         }
    5942                 :            : 
    5943   [ +  +  +  - ]:       1730 :         if (g_opts.timeout_us > 0) {
    5944                 :            :                 /* Register timeout callback. Timeout values for IO vs. admin reqs can be different. */
    5945                 :            :                 /* If timeout_admin_us is 0 (not specified), admin uses same timeout as IO. */
    5946   [ #  #  #  # ]:          0 :                 uint64_t adm_timeout_us = (g_opts.timeout_admin_us == 0) ?
    5947   [ #  #  #  # ]:          0 :                                           g_opts.timeout_us : g_opts.timeout_admin_us;
    5948         [ #  # ]:          0 :                 spdk_nvme_ctrlr_register_timeout_callback(ctrlr, g_opts.timeout_us,
    5949                 :          0 :                                 adm_timeout_us, timeout_cb, nvme_ctrlr);
    5950                 :          0 :         }
    5951                 :            : 
    5952                 :       1730 :         spdk_nvme_ctrlr_register_aer_callback(ctrlr, aer_cb, nvme_ctrlr);
    5953                 :       1730 :         spdk_nvme_ctrlr_set_remove_cb(ctrlr, remove_cb, nvme_ctrlr);
    5954                 :            : 
    5955         [ +  + ]:       1730 :         if (spdk_nvme_ctrlr_get_flags(ctrlr) &
    5956                 :            :             SPDK_NVME_CTRLR_SECURITY_SEND_RECV_SUPPORTED) {
    5957   [ #  #  #  # ]:         32 :                 nvme_ctrlr->opal_dev = spdk_opal_dev_construct(ctrlr);
    5958                 :          0 :         }
    5959                 :            : 
    5960                 :       1730 :         rc = nvme_bdev_ctrlr_create(name, nvme_ctrlr);
    5961         [ +  + ]:       1730 :         if (rc != 0) {
    5962                 :          3 :                 goto err;
    5963                 :            :         }
    5964                 :            : 
    5965                 :       1727 :         cdata = spdk_nvme_ctrlr_get_data(ctrlr);
    5966                 :            : 
    5967   [ +  +  -  +  :       1727 :         if (cdata->cmic.ana_reporting) {
                   -  + ]
    5968                 :        417 :                 rc = nvme_ctrlr_init_ana_log_page(nvme_ctrlr, ctx);
    5969         [ +  - ]:        417 :                 if (rc == 0) {
    5970                 :        417 :                         return 0;
    5971                 :            :                 }
    5972                 :          0 :         } else {
    5973                 :       1310 :                 nvme_ctrlr_create_done(nvme_ctrlr, ctx);
    5974                 :       1310 :                 return 0;
    5975                 :            :         }
    5976                 :            : 
    5977                 :          3 : err:
    5978                 :          3 :         nvme_ctrlr_delete(nvme_ctrlr);
    5979                 :          3 :         return rc;
    5980                 :          1 : }
    5981                 :            : 
    5982                 :            : void
    5983                 :       1697 : spdk_bdev_nvme_get_default_ctrlr_opts(struct spdk_bdev_nvme_ctrlr_opts *opts)
    5984                 :            : {
    5985   [ +  -  +  - ]:       1697 :         opts->prchk_flags = 0;
    5986   [ +  -  +  -  :       1697 :         opts->ctrlr_loss_timeout_sec = g_opts.ctrlr_loss_timeout_sec;
                   +  - ]
    5987   [ +  -  +  -  :       1697 :         opts->reconnect_delay_sec = g_opts.reconnect_delay_sec;
                   +  - ]
    5988   [ +  -  +  -  :       1697 :         opts->fast_io_fail_timeout_sec = g_opts.fast_io_fail_timeout_sec;
                   +  - ]
    5989   [ +  -  +  - ]:       1697 :         opts->multipath = true;
    5990                 :       1697 : }
    5991                 :            : 
    5992                 :            : static void
    5993                 :         56 : attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
    5994                 :            :           struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *drv_opts)
    5995                 :            : {
    5996                 :            :         char *name;
    5997                 :            : 
    5998         [ #  # ]:         56 :         name = spdk_sprintf_alloc("HotInNvme%d", g_hot_insert_nvme_controller_index++);
    5999         [ -  + ]:         56 :         if (!name) {
    6000                 :          0 :                 SPDK_ERRLOG("Failed to assign name to NVMe device\n");
    6001                 :          0 :                 return;
    6002                 :            :         }
    6003                 :            : 
    6004         [ +  - ]:         56 :         if (nvme_ctrlr_create(ctrlr, name, trid, NULL) == 0) {
    6005   [ -  +  -  +  :         56 :                 SPDK_DEBUGLOG(bdev_nvme, "Attached to %s (%s)\n", trid->traddr, name);
             #  #  #  # ]
    6006                 :          0 :         } else {
    6007         [ #  # ]:          0 :                 SPDK_ERRLOG("Failed to attach to %s (%s)\n", trid->traddr, name);
    6008                 :            :         }
    6009                 :            : 
    6010                 :         56 :         free(name);
    6011                 :          0 : }
    6012                 :            : 
    6013                 :            : static void
    6014                 :       1727 : _nvme_ctrlr_destruct(void *ctx)
    6015                 :            : {
    6016                 :       1727 :         struct nvme_ctrlr *nvme_ctrlr = ctx;
    6017                 :            : 
    6018                 :       1727 :         nvme_ctrlr_depopulate_namespaces(nvme_ctrlr);
    6019                 :       1727 :         nvme_ctrlr_put_ref(nvme_ctrlr);
    6020                 :       1727 : }
    6021                 :            : 
    6022                 :            : static int
    6023                 :        977 : bdev_nvme_delete_ctrlr_unsafe(struct nvme_ctrlr *nvme_ctrlr, bool hotplug)
    6024                 :            : {
    6025                 :            :         struct nvme_probe_skip_entry *entry;
    6026                 :            : 
    6027                 :            :         /* The controller's destruction was already started */
    6028   [ -  +  #  # ]:        977 :         if (nvme_ctrlr->destruct) {
    6029                 :          0 :                 return -EALREADY;
    6030                 :            :         }
    6031                 :            : 
    6032   [ +  +  #  #  :        977 :         if (!hotplug &&
                   #  # ]
    6033   [ +  +  #  #  :        929 :             nvme_ctrlr->active_path_id->trid.trtype == SPDK_NVME_TRANSPORT_PCIE) {
          #  #  #  #  #  
                      # ]
    6034                 :         56 :                 entry = calloc(1, sizeof(*entry));
    6035         [ -  + ]:         56 :                 if (!entry) {
    6036                 :          0 :                         return -ENOMEM;
    6037                 :            :                 }
    6038   [ #  #  #  #  :         56 :                 entry->trid = nvme_ctrlr->active_path_id->trid;
             #  #  #  # ]
    6039   [ #  #  #  #  :         56 :                 TAILQ_INSERT_TAIL(&g_skipped_nvme_ctrlrs, entry, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6040                 :          0 :         }
    6041                 :            : 
    6042         [ #  # ]:        977 :         nvme_ctrlr->destruct = true;
    6043                 :        977 :         return 0;
    6044                 :          0 : }
    6045                 :            : 
    6046                 :            : static int
    6047                 :         77 : bdev_nvme_delete_ctrlr(struct nvme_ctrlr *nvme_ctrlr, bool hotplug)
    6048                 :            : {
    6049                 :            :         int rc;
    6050                 :            : 
    6051   [ -  +  #  # ]:         77 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    6052         [ #  # ]:         77 :         rc = bdev_nvme_delete_ctrlr_unsafe(nvme_ctrlr, hotplug);
    6053   [ -  +  #  # ]:         77 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    6054                 :            : 
    6055         [ +  - ]:         77 :         if (rc == 0) {
    6056                 :         77 :                 _nvme_ctrlr_destruct(nvme_ctrlr);
    6057         [ #  # ]:          0 :         } else if (rc == -EALREADY) {
    6058                 :          0 :                 rc = 0;
    6059                 :          0 :         }
    6060                 :            : 
    6061                 :         77 :         return rc;
    6062                 :            : }
    6063                 :            : 
    6064                 :            : static void
    6065                 :         48 : remove_cb(void *cb_ctx, struct spdk_nvme_ctrlr *ctrlr)
    6066                 :            : {
    6067                 :         48 :         struct nvme_ctrlr *nvme_ctrlr = cb_ctx;
    6068                 :            : 
    6069                 :         48 :         bdev_nvme_delete_ctrlr(nvme_ctrlr, true);
    6070                 :         48 : }
    6071                 :            : 
    6072                 :            : static int
    6073                 :      20088 : bdev_nvme_hotplug_probe(void *arg)
    6074                 :            : {
    6075         [ -  + ]:      20088 :         if (g_hotplug_probe_ctx == NULL) {
    6076                 :          0 :                 spdk_poller_unregister(&g_hotplug_probe_poller);
    6077                 :          0 :                 return SPDK_POLLER_IDLE;
    6078                 :            :         }
    6079                 :            : 
    6080         [ +  + ]:      20088 :         if (spdk_nvme_probe_poll_async(g_hotplug_probe_ctx) != -EAGAIN) {
    6081                 :       3904 :                 g_hotplug_probe_ctx = NULL;
    6082                 :       3904 :                 spdk_poller_unregister(&g_hotplug_probe_poller);
    6083                 :          0 :         }
    6084                 :            : 
    6085                 :      20088 :         return SPDK_POLLER_BUSY;
    6086                 :          0 : }
    6087                 :            : 
    6088                 :            : static int
    6089                 :       4051 : bdev_nvme_hotplug(void *arg)
    6090                 :            : {
    6091                 :       1954 :         struct spdk_nvme_transport_id trid_pcie;
    6092                 :            : 
    6093         [ +  + ]:       4051 :         if (g_hotplug_probe_ctx) {
    6094                 :        147 :                 return SPDK_POLLER_BUSY;
    6095                 :            :         }
    6096                 :            : 
    6097         [ -  + ]:       3904 :         memset(&trid_pcie, 0, sizeof(trid_pcie));
    6098                 :       3904 :         spdk_nvme_trid_populate_transport(&trid_pcie, SPDK_NVME_TRANSPORT_PCIE);
    6099                 :            : 
    6100                 :       3904 :         g_hotplug_probe_ctx = spdk_nvme_probe_async(&trid_pcie, NULL,
    6101                 :            :                               hotplug_probe_cb, attach_cb, NULL);
    6102                 :            : 
    6103         [ +  - ]:       3904 :         if (g_hotplug_probe_ctx) {
    6104   [ -  +  #  # ]:       3904 :                 assert(g_hotplug_probe_poller == NULL);
    6105                 :       3904 :                 g_hotplug_probe_poller = SPDK_POLLER_REGISTER(bdev_nvme_hotplug_probe, NULL, 1000);
    6106                 :          0 :         }
    6107                 :            : 
    6108                 :       3904 :         return SPDK_POLLER_BUSY;
    6109                 :          0 : }
    6110                 :            : 
    6111                 :            : void
    6112                 :       1022 : spdk_bdev_nvme_get_opts(struct spdk_bdev_nvme_opts *opts, size_t opts_size)
    6113                 :            : {
    6114         [ +  + ]:       1022 :         if (!opts) {
    6115                 :          0 :                 SPDK_ERRLOG("opts should not be NULL\n");
    6116                 :          0 :                 return;
    6117                 :            :         }
    6118                 :            : 
    6119         [ +  + ]:       1022 :         if (!opts_size) {
    6120                 :          0 :                 SPDK_ERRLOG("opts_size should not be zero value\n");
    6121                 :          0 :                 return;
    6122                 :            :         }
    6123                 :            : 
    6124   [ +  -  +  - ]:       1022 :         opts->opts_size = opts_size;
    6125                 :            : 
    6126                 :            : #define SET_FIELD(field, defval) \
    6127                 :            :                 opts->field = SPDK_GET_FIELD(&g_opts, field, defval, opts_size); \
    6128                 :            : 
    6129   [ +  -  +  -  :       1022 :         SET_FIELD(action_on_timeout, 0);
             +  -  +  - ]
    6130   [ +  -  +  -  :       1022 :         SET_FIELD(keep_alive_timeout_ms, 0);
             +  -  +  - ]
    6131   [ +  -  +  -  :       1022 :         SET_FIELD(timeout_us, 0);
             +  -  +  - ]
    6132   [ +  -  +  -  :       1022 :         SET_FIELD(timeout_admin_us, 0);
             +  -  +  - ]
    6133   [ +  -  +  -  :       1022 :         SET_FIELD(transport_retry_count, 0);
             +  -  +  - ]
    6134   [ +  -  +  -  :       1022 :         SET_FIELD(arbitration_burst, 0);
             +  -  +  - ]
    6135   [ +  -  +  -  :       1022 :         SET_FIELD(low_priority_weight, 0);
             +  -  +  - ]
    6136   [ +  -  +  -  :       1022 :         SET_FIELD(medium_priority_weight, 0);
             +  -  +  - ]
    6137   [ +  -  +  -  :       1022 :         SET_FIELD(high_priority_weight, 0);
             +  -  +  - ]
    6138   [ +  -  +  -  :       1022 :         SET_FIELD(io_queue_requests, 0);
             +  -  +  - ]
    6139   [ +  -  +  -  :       1022 :         SET_FIELD(nvme_adminq_poll_period_us, 0);
             +  -  +  - ]
    6140   [ +  -  +  -  :       1022 :         SET_FIELD(nvme_ioq_poll_period_us, 0);
             +  -  +  - ]
    6141   [ +  -  +  +  :       1022 :         SET_FIELD(delay_cmd_submit, 0);
          +  -  +  -  +  
                      - ]
    6142   [ +  -  +  -  :       1022 :         SET_FIELD(bdev_retry_count, 0);
             +  -  +  - ]
    6143   [ +  -  +  -  :       1022 :         SET_FIELD(ctrlr_loss_timeout_sec, 0);
             +  -  +  - ]
    6144   [ +  -  +  -  :       1022 :         SET_FIELD(reconnect_delay_sec, 0);
             +  -  +  - ]
    6145   [ +  -  +  -  :       1022 :         SET_FIELD(fast_io_fail_timeout_sec, 0);
             +  -  +  - ]
    6146   [ +  -  +  -  :       1022 :         SET_FIELD(transport_ack_timeout, 0);
             +  -  +  - ]
    6147   [ +  -  +  +  :       1022 :         SET_FIELD(disable_auto_failback, false);
             +  +  +  - ]
    6148   [ +  -  +  +  :       1022 :         SET_FIELD(generate_uuids, false);
          +  +  +  -  +  
                      - ]
    6149   [ +  -  +  -  :       1022 :         SET_FIELD(transport_tos, 0);
                   +  - ]
    6150   [ +  -  +  +  :       1022 :         SET_FIELD(nvme_error_stat, false);
          +  +  +  -  +  
                      - ]
    6151   [ +  -  +  +  :       1022 :         SET_FIELD(io_path_stat, false);
             +  +  +  - ]
    6152   [ +  -  +  +  :       1022 :         SET_FIELD(allow_accel_sequence, false);
          +  +  +  -  +  
                      - ]
    6153   [ +  -  +  -  :       1022 :         SET_FIELD(rdma_srq_size, 0);
             +  -  +  - ]
    6154   [ +  -  +  -  :       1022 :         SET_FIELD(rdma_max_cq_size, 0);
             +  -  +  - ]
    6155   [ +  -  +  -  :       1022 :         SET_FIELD(rdma_cm_event_timeout_ms, 0);
             +  -  +  - ]
    6156   [ +  -  +  -  :       1022 :         SET_FIELD(dhchap_digests, 0);
             +  -  +  - ]
    6157   [ +  -  +  -  :       1022 :         SET_FIELD(dhchap_dhgroups, 0);
             +  -  +  - ]
    6158                 :            : 
    6159                 :            : #undef SET_FIELD
    6160                 :            : 
    6161                 :            :         /* Do not remove this statement, you should always update this statement when you adding a new field,
    6162                 :            :          * and do not forget to add the SET_FIELD statement for your added field. */
    6163                 :            :         SPDK_STATIC_ASSERT(sizeof(struct spdk_bdev_nvme_opts) == 120, "Incorrect size");
    6164                 :         27 : }
    6165                 :            : 
    6166                 :            : static bool bdev_nvme_check_io_error_resiliency_params(int32_t ctrlr_loss_timeout_sec,
    6167                 :            :                 uint32_t reconnect_delay_sec,
    6168                 :            :                 uint32_t fast_io_fail_timeout_sec);
    6169                 :            : 
    6170                 :            : static int
    6171                 :       1022 : bdev_nvme_validate_opts(const struct spdk_bdev_nvme_opts *opts)
    6172                 :            : {
    6173   [ +  +  +  +  :       1022 :         if ((opts->timeout_us == 0) && (opts->timeout_admin_us != 0)) {
          +  -  +  -  +  
                -  +  - ]
    6174                 :            :                 /* Can't set timeout_admin_us without also setting timeout_us */
    6175                 :          0 :                 SPDK_WARNLOG("Invalid options: Can't have (timeout_us == 0) with (timeout_admin_us > 0)\n");
    6176                 :          0 :                 return -EINVAL;
    6177                 :            :         }
    6178                 :            : 
    6179   [ +  +  +  -  :       1022 :         if (opts->bdev_retry_count < -1) {
                   -  + ]
    6180                 :          0 :                 SPDK_WARNLOG("Invalid option: bdev_retry_count can't be less than -1.\n");
    6181                 :          0 :                 return -EINVAL;
    6182                 :            :         }
    6183                 :            : 
    6184   [ +  +  +  -  :       1049 :         if (!bdev_nvme_check_io_error_resiliency_params(opts->ctrlr_loss_timeout_sec,
                   +  - ]
    6185   [ +  -  +  - ]:       1022 :                         opts->reconnect_delay_sec,
    6186   [ +  -  +  - ]:       1022 :                         opts->fast_io_fail_timeout_sec)) {
    6187                 :          0 :                 return -EINVAL;
    6188                 :            :         }
    6189                 :            : 
    6190                 :       1022 :         return 0;
    6191                 :         27 : }
    6192                 :            : 
    6193                 :            : int
    6194                 :       1022 : spdk_bdev_nvme_set_opts(const struct spdk_bdev_nvme_opts *opts)
    6195                 :            : {
    6196         [ +  + ]:       1022 :         if (!opts) {
    6197                 :          0 :                 SPDK_ERRLOG("opts cannot be NULL\n");
    6198                 :          0 :                 return -1;
    6199                 :            :         }
    6200                 :            : 
    6201   [ +  +  +  -  :       1022 :         if (!opts->opts_size) {
                   -  + ]
    6202                 :          0 :                 SPDK_ERRLOG("opts_size inside opts cannot be zero value\n");
    6203                 :          0 :                 return -1;
    6204                 :            :         }
    6205                 :            : 
    6206                 :            :         int ret;
    6207                 :            : 
    6208                 :       1022 :         ret = bdev_nvme_validate_opts(opts);
    6209         [ -  + ]:       1022 :         if (ret) {
    6210                 :          0 :                 SPDK_WARNLOG("Failed to set nvme opts.\n");
    6211                 :          0 :                 return ret;
    6212                 :            :         }
    6213                 :            : 
    6214         [ +  + ]:       1022 :         if (g_bdev_nvme_init_thread != NULL) {
    6215         [ -  + ]:        652 :                 if (!TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) {
    6216                 :          0 :                         return -EPERM;
    6217                 :            :                 }
    6218                 :          0 :         }
    6219                 :            : 
    6220   [ +  -  +  -  :       1049 :         if (opts->rdma_srq_size != 0 ||
             +  -  -  + ]
    6221   [ +  -  +  -  :       1022 :             opts->rdma_max_cq_size != 0 ||
                   +  - ]
    6222   [ -  +  -  + ]:       1022 :             opts->rdma_cm_event_timeout_ms != 0) {
    6223                 :          0 :                 struct spdk_nvme_transport_opts drv_opts;
    6224                 :            : 
    6225                 :          0 :                 spdk_nvme_transport_get_opts(&drv_opts, sizeof(drv_opts));
    6226   [ #  #  #  #  :          0 :                 if (opts->rdma_srq_size != 0) {
                   #  # ]
    6227   [ #  #  #  # ]:          0 :                         drv_opts.rdma_srq_size = opts->rdma_srq_size;
    6228                 :          0 :                 }
    6229   [ #  #  #  #  :          0 :                 if (opts->rdma_max_cq_size != 0) {
                   #  # ]
    6230   [ #  #  #  #  :          0 :                         drv_opts.rdma_max_cq_size = opts->rdma_max_cq_size;
                   #  # ]
    6231                 :          0 :                 }
    6232   [ #  #  #  #  :          0 :                 if (opts->rdma_cm_event_timeout_ms != 0) {
                   #  # ]
    6233   [ #  #  #  #  :          0 :                         drv_opts.rdma_cm_event_timeout_ms = opts->rdma_cm_event_timeout_ms;
                   #  # ]
    6234                 :          0 :                 }
    6235                 :            : 
    6236                 :          0 :                 ret = spdk_nvme_transport_set_opts(&drv_opts, sizeof(drv_opts));
    6237         [ #  # ]:          0 :                 if (ret) {
    6238                 :          0 :                         SPDK_ERRLOG("Failed to set NVMe transport opts.\n");
    6239                 :          0 :                         return ret;
    6240                 :            :                 }
    6241                 :          0 :         }
    6242                 :            : 
    6243                 :            : #define SET_FIELD(field, defval) \
    6244                 :            :                 g_opts.field = SPDK_GET_FIELD(opts, field, defval, opts->opts_size); \
    6245                 :            : 
    6246   [ +  -  +  -  :       1022 :         SET_FIELD(action_on_timeout, 0);
          +  -  +  -  +  
                -  +  - ]
    6247   [ +  -  +  -  :       1022 :         SET_FIELD(keep_alive_timeout_ms, 0);
          +  -  +  -  +  
                -  +  - ]
    6248   [ +  -  +  -  :       1022 :         SET_FIELD(timeout_us, 0);
          +  -  +  -  +  
                -  +  - ]
    6249   [ +  -  +  -  :       1022 :         SET_FIELD(timeout_admin_us, 0);
          +  -  +  -  +  
                -  +  - ]
    6250   [ +  -  +  -  :       1022 :         SET_FIELD(transport_retry_count, 0);
          +  -  +  -  +  
                -  +  - ]
    6251   [ +  -  +  -  :       1022 :         SET_FIELD(arbitration_burst, 0);
          +  -  +  -  +  
                -  +  - ]
    6252   [ +  -  +  -  :       1022 :         SET_FIELD(low_priority_weight, 0);
          +  -  +  -  +  
                -  +  - ]
    6253   [ +  -  +  -  :       1022 :         SET_FIELD(medium_priority_weight, 0);
          +  -  +  -  +  
                -  +  - ]
    6254   [ +  -  +  -  :       1022 :         SET_FIELD(high_priority_weight, 0);
          +  -  +  -  +  
                -  +  - ]
    6255   [ +  -  +  -  :       1022 :         SET_FIELD(io_queue_requests, 0);
          +  -  +  -  +  
                -  +  - ]
    6256   [ +  -  +  -  :       1022 :         SET_FIELD(nvme_adminq_poll_period_us, 0);
          +  -  +  -  +  
                -  +  - ]
    6257   [ +  -  +  -  :       1022 :         SET_FIELD(nvme_ioq_poll_period_us, 0);
          +  -  +  -  +  
                -  +  - ]
    6258   [ +  -  +  +  :       1022 :         SET_FIELD(delay_cmd_submit, 0);
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    6259   [ +  -  +  -  :       1022 :         SET_FIELD(bdev_retry_count, 0);
          +  -  +  -  +  
                -  +  - ]
    6260   [ +  -  +  -  :       1022 :         SET_FIELD(ctrlr_loss_timeout_sec, 0);
          +  -  +  -  +  
                -  +  - ]
    6261   [ +  -  +  -  :       1022 :         SET_FIELD(reconnect_delay_sec, 0);
          +  -  +  -  +  
                -  +  - ]
    6262   [ +  -  +  -  :       1022 :         SET_FIELD(fast_io_fail_timeout_sec, 0);
          +  -  +  -  +  
                -  +  - ]
    6263   [ +  -  +  -  :       1022 :         SET_FIELD(transport_ack_timeout, 0);
          +  -  +  -  +  
                -  +  - ]
    6264   [ +  -  +  +  :       1022 :         SET_FIELD(disable_auto_failback, false);
          +  +  +  -  +  
                -  +  - ]
    6265   [ +  -  +  +  :       1022 :         SET_FIELD(generate_uuids, false);
          +  +  +  -  +  
             -  +  -  +  
                      - ]
    6266   [ +  -  +  -  :       1022 :         SET_FIELD(transport_tos, 0);
          +  -  +  -  +  
                      - ]
    6267   [ +  -  +  +  :       1022 :         SET_FIELD(nvme_error_stat, false);
          +  +  +  -  +  
             -  +  -  +  
                      - ]
    6268   [ +  -  +  +  :       1022 :         SET_FIELD(io_path_stat, false);
          +  +  +  -  +  
                -  +  - ]
    6269   [ +  -  +  +  :       1022 :         SET_FIELD(allow_accel_sequence, false);
          +  +  +  -  +  
             -  +  -  +  
                      - ]
    6270   [ +  -  +  -  :       1022 :         SET_FIELD(rdma_srq_size, 0);
          +  -  +  -  +  
                -  +  - ]
    6271   [ +  -  +  -  :       1022 :         SET_FIELD(rdma_max_cq_size, 0);
          +  -  +  -  +  
                -  +  - ]
    6272   [ +  -  +  -  :       1022 :         SET_FIELD(rdma_cm_event_timeout_ms, 0);
          +  -  +  -  +  
                -  +  - ]
    6273   [ +  -  +  -  :       1022 :         SET_FIELD(dhchap_digests, 0);
          +  -  +  -  +  
                -  +  - ]
    6274   [ +  -  +  -  :       1022 :         SET_FIELD(dhchap_dhgroups, 0);
          +  -  +  -  +  
                -  +  - ]
    6275                 :            : 
    6276   [ +  -  +  - ]:       1022 :         g_opts.opts_size = opts->opts_size;
    6277                 :            : 
    6278                 :            : #undef SET_FIELD
    6279                 :            : 
    6280                 :       1022 :         return 0;
    6281                 :         27 : }
    6282                 :            : 
    6283                 :            : struct set_nvme_hotplug_ctx {
    6284                 :            :         uint64_t period_us;
    6285                 :            :         bool enabled;
    6286                 :            :         spdk_msg_fn fn;
    6287                 :            :         void *fn_ctx;
    6288                 :            : };
    6289                 :            : 
    6290                 :            : static void
    6291                 :        301 : set_nvme_hotplug_period_cb(void *_ctx)
    6292                 :            : {
    6293                 :        301 :         struct set_nvme_hotplug_ctx *ctx = _ctx;
    6294                 :            : 
    6295                 :        301 :         spdk_poller_unregister(&g_hotplug_poller);
    6296   [ +  +  +  +  :        301 :         if (ctx->enabled) {
             +  -  -  + ]
    6297   [ #  #  #  # ]:         12 :                 g_hotplug_poller = SPDK_POLLER_REGISTER(bdev_nvme_hotplug, NULL, ctx->period_us);
    6298                 :          0 :         } else {
    6299                 :        289 :                 g_hotplug_poller = SPDK_POLLER_REGISTER(bdev_nvme_remove_poller, NULL,
    6300                 :            :                                                         NVME_HOTPLUG_POLL_PERIOD_DEFAULT);
    6301                 :            :         }
    6302                 :            : 
    6303   [ +  -  +  - ]:        301 :         g_nvme_hotplug_poll_period_us = ctx->period_us;
    6304   [ +  +  +  -  :        301 :         g_nvme_hotplug_enabled = ctx->enabled;
                   +  - ]
    6305   [ +  -  +  -  :        301 :         if (ctx->fn) {
                   +  - ]
    6306   [ +  -  +  -  :        301 :                 ctx->fn(ctx->fn_ctx);
          -  +  +  -  +  
                -  +  - ]
    6307                 :         26 :         }
    6308                 :            : 
    6309                 :        301 :         free(ctx);
    6310                 :        301 : }
    6311                 :            : 
    6312                 :            : int
    6313                 :        301 : bdev_nvme_set_hotplug(bool enabled, uint64_t period_us, spdk_msg_fn cb, void *cb_ctx)
    6314                 :            : {
    6315                 :            :         struct set_nvme_hotplug_ctx *ctx;
    6316                 :            : 
    6317   [ +  +  -  +  :        301 :         if (enabled == true && !spdk_process_is_primary()) {
                   #  # ]
    6318                 :          0 :                 return -EPERM;
    6319                 :            :         }
    6320                 :            : 
    6321                 :        301 :         ctx = calloc(1, sizeof(*ctx));
    6322         [ +  + ]:        301 :         if (ctx == NULL) {
    6323                 :          0 :                 return -ENOMEM;
    6324                 :            :         }
    6325                 :            : 
    6326         [ +  + ]:        301 :         period_us = period_us == 0 ? NVME_HOTPLUG_POLL_PERIOD_DEFAULT : period_us;
    6327   [ +  -  -  +  :        301 :         ctx->period_us = spdk_min(period_us, NVME_HOTPLUG_POLL_PERIOD_MAX);
                   -  + ]
    6328   [ -  +  -  +  :        301 :         ctx->enabled = enabled;
                   -  + ]
    6329   [ -  +  -  + ]:        301 :         ctx->fn = cb;
    6330   [ -  +  -  + ]:        301 :         ctx->fn_ctx = cb_ctx;
    6331                 :            : 
    6332                 :        301 :         spdk_thread_send_msg(g_bdev_nvme_init_thread, set_nvme_hotplug_period_cb, ctx);
    6333                 :        301 :         return 0;
    6334                 :         26 : }
    6335                 :            : 
    6336                 :            : static void
    6337                 :       1629 : nvme_ctrlr_populate_namespaces_done(struct nvme_ctrlr *nvme_ctrlr,
    6338                 :            :                                     struct nvme_async_probe_ctx *ctx)
    6339                 :            : {
    6340                 :            :         struct nvme_ns  *nvme_ns;
    6341                 :            :         struct nvme_bdev        *nvme_bdev;
    6342                 :            :         size_t                  j;
    6343                 :            : 
    6344   [ +  +  #  # ]:       1629 :         assert(nvme_ctrlr != NULL);
    6345                 :            : 
    6346   [ +  +  +  -  :       1629 :         if (ctx->names == NULL) {
                   +  - ]
    6347   [ #  #  #  # ]:         37 :                 ctx->reported_bdevs = 0;
    6348                 :         37 :                 populate_namespaces_cb(ctx, 0);
    6349                 :         37 :                 return;
    6350                 :            :         }
    6351                 :            : 
    6352                 :            :         /*
    6353                 :            :          * Report the new bdevs that were created in this call.
    6354                 :            :          * There can be more than one bdev per NVMe controller.
    6355                 :            :          */
    6356                 :       1592 :         j = 0;
    6357                 :       1592 :         nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
    6358         [ +  + ]:       2995 :         while (nvme_ns != NULL) {
    6359   [ +  -  +  - ]:       1403 :                 nvme_bdev = nvme_ns->bdev;
    6360   [ +  -  +  -  :       1403 :                 if (j < ctx->max_bdevs) {
                   +  - ]
    6361   [ +  -  +  -  :       1403 :                         ctx->names[j] = nvme_bdev->disk.name;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    6362                 :       1403 :                         j++;
    6363                 :          1 :                 } else {
    6364   [ #  #  #  #  :          0 :                         NVME_CTRLR_ERRLOG(nvme_ctrlr,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6365                 :            :                                           "Maximum number of namespaces supported per NVMe controller is %du. "
    6366                 :            :                                           "Unable to return all names of created bdevs\n",
    6367                 :            :                                           ctx->max_bdevs);
    6368   [ #  #  #  # ]:          0 :                         ctx->reported_bdevs = 0;
    6369                 :          0 :                         populate_namespaces_cb(ctx, -ERANGE);
    6370                 :          0 :                         return;
    6371                 :            :                 }
    6372                 :            : 
    6373                 :       1403 :                 nvme_ns = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns);
    6374                 :            :         }
    6375                 :            : 
    6376   [ +  -  +  - ]:       1592 :         ctx->reported_bdevs = j;
    6377                 :       1592 :         populate_namespaces_cb(ctx, 0);
    6378                 :          1 : }
    6379                 :            : 
    6380                 :            : static int
    6381                 :         43 : bdev_nvme_check_secondary_trid(struct nvme_ctrlr *nvme_ctrlr,
    6382                 :            :                                struct spdk_nvme_ctrlr *new_ctrlr,
    6383                 :            :                                struct spdk_nvme_transport_id *trid)
    6384                 :            : {
    6385                 :            :         struct nvme_path_id *tmp_trid;
    6386                 :            : 
    6387   [ -  +  #  #  :         43 :         if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
                   #  # ]
    6388   [ #  #  #  #  :          0 :                 NVME_CTRLR_ERRLOG(nvme_ctrlr, "PCIe failover is not supported.\n");
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    6389                 :          0 :                 return -ENOTSUP;
    6390                 :            :         }
    6391                 :            : 
    6392                 :            :         /* Currently we only support failover to the same transport type. */
    6393   [ -  +  #  #  :         43 :         if (nvme_ctrlr->active_path_id->trid.trtype != trid->trtype) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6394   [ #  #  #  #  :          0 :                 NVME_CTRLR_WARNLOG(nvme_ctrlr,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6395                 :            :                                    "Failover from trtype: %s to a different trtype: %s is not supported currently\n",
    6396                 :            :                                    spdk_nvme_transport_id_trtype_str(nvme_ctrlr->active_path_id->trid.trtype),
    6397                 :            :                                    spdk_nvme_transport_id_trtype_str(trid->trtype));
    6398                 :          0 :                 return -EINVAL;
    6399                 :            :         }
    6400                 :            : 
    6401                 :            : 
    6402                 :            :         /* Currently we only support failover to the same NQN. */
    6403   [ -  +  -  +  :         43 :         if (strncmp(trid->subnqn, nvme_ctrlr->active_path_id->trid.subnqn, SPDK_NVMF_NQN_MAX_LEN)) {
          -  +  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6404   [ #  #  #  #  :          0 :                 NVME_CTRLR_WARNLOG(nvme_ctrlr,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    6405                 :            :                                    "Failover from subnqn: %s to a different subnqn: %s is not supported currently\n",
    6406                 :            :                                    nvme_ctrlr->active_path_id->trid.subnqn, trid->subnqn);
    6407                 :          0 :                 return -EINVAL;
    6408                 :            :         }
    6409                 :            : 
    6410                 :            :         /* Skip all the other checks if we've already registered this path. */
    6411   [ +  +  #  #  :        103 :         TAILQ_FOREACH(tmp_trid, &nvme_ctrlr->trids, link) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6412   [ -  +  #  # ]:         60 :                 if (!spdk_nvme_transport_id_compare(&tmp_trid->trid, trid)) {
    6413   [ #  #  #  #  :          0 :                         NVME_CTRLR_WARNLOG(nvme_ctrlr, "This path (traddr: %s subnqn: %s) is already registered\n",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6414                 :            :                                            trid->traddr, trid->subnqn);
    6415                 :          0 :                         return -EALREADY;
    6416                 :            :                 }
    6417                 :          0 :         }
    6418                 :            : 
    6419                 :         43 :         return 0;
    6420                 :          0 : }
    6421                 :            : 
    6422                 :            : static int
    6423                 :         43 : bdev_nvme_check_secondary_namespace(struct nvme_ctrlr *nvme_ctrlr,
    6424                 :            :                                     struct spdk_nvme_ctrlr *new_ctrlr)
    6425                 :            : {
    6426                 :            :         struct nvme_ns *nvme_ns;
    6427                 :            :         struct spdk_nvme_ns *new_ns;
    6428                 :            : 
    6429                 :         43 :         nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
    6430         [ +  + ]:         59 :         while (nvme_ns != NULL) {
    6431   [ #  #  #  # ]:         16 :                 new_ns = spdk_nvme_ctrlr_get_ns(new_ctrlr, nvme_ns->id);
    6432   [ -  +  #  # ]:         16 :                 assert(new_ns != NULL);
    6433                 :            : 
    6434   [ -  +  #  #  :         16 :                 if (!bdev_nvme_compare_ns(nvme_ns->ns, new_ns)) {
                   #  # ]
    6435                 :          0 :                         return -EINVAL;
    6436                 :            :                 }
    6437                 :            : 
    6438                 :         16 :                 nvme_ns = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns);
    6439                 :            :         }
    6440                 :            : 
    6441                 :         43 :         return 0;
    6442                 :          0 : }
    6443                 :            : 
    6444                 :            : static int
    6445                 :         43 : _bdev_nvme_add_secondary_trid(struct nvme_ctrlr *nvme_ctrlr,
    6446                 :            :                               struct spdk_nvme_transport_id *trid)
    6447                 :            : {
    6448                 :            :         struct nvme_path_id *active_id, *new_trid, *tmp_trid;
    6449                 :            : 
    6450                 :         43 :         new_trid = calloc(1, sizeof(*new_trid));
    6451         [ -  + ]:         43 :         if (new_trid == NULL) {
    6452                 :          0 :                 return -ENOMEM;
    6453                 :            :         }
    6454         [ #  # ]:         43 :         new_trid->trid = *trid;
    6455                 :            : 
    6456   [ #  #  #  # ]:         43 :         active_id = nvme_ctrlr->active_path_id;
    6457   [ -  +  #  # ]:         43 :         assert(active_id != NULL);
    6458   [ -  +  #  #  :         43 :         assert(active_id == TAILQ_FIRST(&nvme_ctrlr->trids));
          #  #  #  #  #  
                      # ]
    6459                 :            : 
    6460                 :            :         /* Skip the active trid not to replace it until it is failed. */
    6461   [ #  #  #  #  :         43 :         tmp_trid = TAILQ_NEXT(active_id, link);
                   #  # ]
    6462         [ +  + ]:         43 :         if (tmp_trid == NULL) {
    6463                 :         26 :                 goto add_tail;
    6464                 :            :         }
    6465                 :            : 
    6466                 :            :         /* It means the trid is faled if its last failed time is non-zero.
    6467                 :            :          * Insert the new alternate trid before any failed trid.
    6468                 :            :          */
    6469   [ -  +  +  +  :         27 :         TAILQ_FOREACH_FROM(tmp_trid, &nvme_ctrlr->trids, link) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6470   [ +  +  #  #  :         17 :                 if (tmp_trid->last_failed_tsc != 0) {
                   #  # ]
    6471   [ #  #  #  #  :          7 :                         TAILQ_INSERT_BEFORE(tmp_trid, new_trid, link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6472                 :          7 :                         return 0;
    6473                 :            :                 }
    6474                 :          0 :         }
    6475                 :            : 
    6476                 :         10 : add_tail:
    6477   [ #  #  #  #  :         36 :         TAILQ_INSERT_TAIL(&nvme_ctrlr->trids, new_trid, link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6478                 :         36 :         return 0;
    6479                 :          0 : }
    6480                 :            : 
    6481                 :            : /* This is the case that a secondary path is added to an existing
    6482                 :            :  * nvme_ctrlr for failover. After checking if it can access the same
    6483                 :            :  * namespaces as the primary path, it is disconnected until failover occurs.
    6484                 :            :  */
    6485                 :            : static int
    6486                 :         43 : bdev_nvme_add_secondary_trid(struct nvme_ctrlr *nvme_ctrlr,
    6487                 :            :                              struct spdk_nvme_ctrlr *new_ctrlr,
    6488                 :            :                              struct spdk_nvme_transport_id *trid)
    6489                 :            : {
    6490                 :            :         int rc;
    6491                 :            : 
    6492   [ -  +  #  # ]:         43 :         assert(nvme_ctrlr != NULL);
    6493                 :            : 
    6494   [ -  +  #  # ]:         43 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    6495                 :            : 
    6496                 :         43 :         rc = bdev_nvme_check_secondary_trid(nvme_ctrlr, new_ctrlr, trid);
    6497         [ -  + ]:         43 :         if (rc != 0) {
    6498                 :          0 :                 goto exit;
    6499                 :            :         }
    6500                 :            : 
    6501                 :         43 :         rc = bdev_nvme_check_secondary_namespace(nvme_ctrlr, new_ctrlr);
    6502         [ -  + ]:         43 :         if (rc != 0) {
    6503                 :          0 :                 goto exit;
    6504                 :            :         }
    6505                 :            : 
    6506                 :         43 :         rc = _bdev_nvme_add_secondary_trid(nvme_ctrlr, trid);
    6507                 :            : 
    6508                 :         43 : exit:
    6509   [ -  +  #  # ]:         43 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    6510                 :            : 
    6511                 :         43 :         spdk_nvme_detach(new_ctrlr);
    6512                 :            : 
    6513                 :         43 :         return rc;
    6514                 :            : }
    6515                 :            : 
    6516                 :            : static void
    6517                 :       1632 : connect_attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
    6518                 :            :                   struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
    6519                 :            : {
    6520                 :       1632 :         struct spdk_nvme_ctrlr_opts *user_opts = cb_ctx;
    6521                 :            :         struct nvme_async_probe_ctx *ctx;
    6522                 :            :         int rc;
    6523                 :            : 
    6524                 :       1632 :         ctx = SPDK_CONTAINEROF(user_opts, struct nvme_async_probe_ctx, drv_opts);
    6525   [ +  -  +  - ]:       1632 :         ctx->ctrlr_attached = true;
    6526                 :            : 
    6527   [ +  -  +  -  :       1632 :         rc = nvme_ctrlr_create(ctrlr, ctx->base_name, &ctx->trid, ctx);
                   +  - ]
    6528         [ +  + ]:       1632 :         if (rc != 0) {
    6529   [ #  #  #  # ]:          3 :                 ctx->reported_bdevs = 0;
    6530                 :          3 :                 populate_namespaces_cb(ctx, rc);
    6531                 :          0 :         }
    6532                 :       1632 : }
    6533                 :            : 
    6534                 :            : 
    6535                 :            : static void
    6536                 :         28 : connect_set_failover_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
    6537                 :            :                         struct spdk_nvme_ctrlr *ctrlr,
    6538                 :            :                         const struct spdk_nvme_ctrlr_opts *opts)
    6539                 :            : {
    6540                 :         28 :         struct spdk_nvme_ctrlr_opts *user_opts = cb_ctx;
    6541                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    6542                 :            :         struct nvme_async_probe_ctx *ctx;
    6543                 :            :         int rc;
    6544                 :            : 
    6545                 :         28 :         ctx = SPDK_CONTAINEROF(user_opts, struct nvme_async_probe_ctx, drv_opts);
    6546   [ #  #  #  # ]:         28 :         ctx->ctrlr_attached = true;
    6547                 :            : 
    6548   [ #  #  #  # ]:         28 :         nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->base_name);
    6549         [ +  - ]:         28 :         if (nvme_ctrlr) {
    6550         [ #  # ]:         28 :                 rc = bdev_nvme_add_secondary_trid(nvme_ctrlr, ctrlr, &ctx->trid);
    6551                 :          0 :         } else {
    6552                 :          0 :                 rc = -ENODEV;
    6553                 :            :         }
    6554                 :            : 
    6555   [ #  #  #  # ]:         28 :         ctx->reported_bdevs = 0;
    6556                 :         28 :         populate_namespaces_cb(ctx, rc);
    6557                 :         28 : }
    6558                 :            : 
    6559                 :            : static int
    6560                 :     283611 : bdev_nvme_async_poll(void *arg)
    6561                 :            : {
    6562                 :     283611 :         struct nvme_async_probe_ctx     *ctx = arg;
    6563                 :            :         int                             rc;
    6564                 :            : 
    6565   [ +  -  +  - ]:     283611 :         rc = spdk_nvme_probe_poll_async(ctx->probe_ctx);
    6566         [ +  + ]:     283611 :         if (spdk_unlikely(rc != -EAGAIN)) {
    6567   [ +  -  +  - ]:       1718 :                 ctx->probe_done = true;
    6568         [ +  - ]:       1718 :                 spdk_poller_unregister(&ctx->poller);
    6569   [ +  +  +  +  :       1718 :                 if (!ctx->ctrlr_attached) {
             +  -  +  - ]
    6570                 :            :                         /* The probe is done, but no controller was attached.
    6571                 :            :                          * That means we had a failure, so report -EIO back to
    6572                 :            :                          * the caller (usually the RPC). populate_namespaces_cb()
    6573                 :            :                          * will take care of freeing the nvme_async_probe_ctx.
    6574                 :            :                          */
    6575   [ #  #  #  # ]:         58 :                         ctx->reported_bdevs = 0;
    6576                 :         58 :                         populate_namespaces_cb(ctx, -EIO);
    6577   [ +  +  +  +  :       1660 :                 } else if (ctx->namespaces_populated) {
             +  -  -  + ]
    6578                 :            :                         /* The namespaces for the attached controller were all
    6579                 :            :                          * populated and the response was already sent to the
    6580                 :            :                          * caller (usually the RPC).  So free the context here.
    6581                 :            :                          */
    6582                 :       1236 :                         free_nvme_async_probe_ctx(ctx);
    6583                 :          1 :                 }
    6584                 :          1 :         }
    6585                 :            : 
    6586                 :     283611 :         return SPDK_POLLER_BUSY;
    6587                 :            : }
    6588                 :            : 
    6589                 :            : static bool
    6590                 :       2806 : bdev_nvme_check_io_error_resiliency_params(int32_t ctrlr_loss_timeout_sec,
    6591                 :            :                 uint32_t reconnect_delay_sec,
    6592                 :            :                 uint32_t fast_io_fail_timeout_sec)
    6593                 :            : {
    6594         [ +  + ]:       2806 :         if (ctrlr_loss_timeout_sec < -1) {
    6595                 :          3 :                 SPDK_ERRLOG("ctrlr_loss_timeout_sec can't be less than -1.\n");
    6596                 :          3 :                 return false;
    6597         [ +  + ]:       2803 :         } else if (ctrlr_loss_timeout_sec == -1) {
    6598         [ +  + ]:         54 :                 if (reconnect_delay_sec == 0) {
    6599                 :          3 :                         SPDK_ERRLOG("reconnect_delay_sec can't be 0 if ctrlr_loss_timeout_sec is not 0.\n");
    6600                 :          3 :                         return false;
    6601   [ +  +  +  + ]:         51 :                 } else if (fast_io_fail_timeout_sec != 0 &&
    6602                 :          0 :                            fast_io_fail_timeout_sec < reconnect_delay_sec) {
    6603                 :          3 :                         SPDK_ERRLOG("reconnect_delay_sec can't be more than fast_io-fail_timeout_sec.\n");
    6604                 :          3 :                         return false;
    6605                 :            :                 }
    6606         [ +  + ]:       2749 :         } else if (ctrlr_loss_timeout_sec != 0) {
    6607         [ +  + ]:         61 :                 if (reconnect_delay_sec == 0) {
    6608                 :          3 :                         SPDK_ERRLOG("reconnect_delay_sec can't be 0 if ctrlr_loss_timeout_sec is not 0.\n");
    6609                 :          3 :                         return false;
    6610         [ +  + ]:         58 :                 } else if (reconnect_delay_sec > (uint32_t)ctrlr_loss_timeout_sec) {
    6611                 :          3 :                         SPDK_ERRLOG("reconnect_delay_sec can't be more than ctrlr_loss_timeout_sec.\n");
    6612                 :          3 :                         return false;
    6613         [ +  + ]:         55 :                 } else if (fast_io_fail_timeout_sec != 0) {
    6614         [ +  + ]:         26 :                         if (fast_io_fail_timeout_sec < reconnect_delay_sec) {
    6615                 :          3 :                                 SPDK_ERRLOG("reconnect_delay_sec can't be more than fast_io_fail_timeout_sec.\n");
    6616                 :          3 :                                 return false;
    6617         [ +  + ]:         23 :                         } else if (fast_io_fail_timeout_sec > (uint32_t)ctrlr_loss_timeout_sec) {
    6618                 :          3 :                                 SPDK_ERRLOG("fast_io_fail_timeout_sec can't be more than ctrlr_loss_timeout_sec.\n");
    6619                 :          3 :                                 return false;
    6620                 :            :                         }
    6621                 :          0 :                 }
    6622   [ +  +  +  + ]:       2688 :         } else if (reconnect_delay_sec != 0 || fast_io_fail_timeout_sec != 0) {
    6623                 :          6 :                 SPDK_ERRLOG("Both reconnect_delay_sec and fast_io_fail_timeout_sec must be 0 if ctrlr_loss_timeout_sec is 0.\n");
    6624                 :          6 :                 return false;
    6625                 :            :         }
    6626                 :            : 
    6627                 :       2779 :         return true;
    6628                 :         28 : }
    6629                 :            : 
    6630                 :            : int
    6631                 :       1727 : spdk_bdev_nvme_create(struct spdk_nvme_transport_id *trid,
    6632                 :            :                       const char *base_name,
    6633                 :            :                       const char **names,
    6634                 :            :                       uint32_t count,
    6635                 :            :                       spdk_bdev_nvme_create_cb cb_fn,
    6636                 :            :                       void *cb_ctx,
    6637                 :            :                       struct spdk_nvme_ctrlr_opts *drv_opts,
    6638                 :            :                       struct spdk_bdev_nvme_ctrlr_opts *bdev_opts)
    6639                 :            : {
    6640                 :            :         struct nvme_probe_skip_entry *entry, *tmp;
    6641                 :            :         struct nvme_async_probe_ctx *ctx;
    6642                 :            :         spdk_nvme_attach_cb attach_cb;
    6643                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    6644                 :            :         int len;
    6645                 :            : 
    6646                 :            :         /* TODO expand this check to include both the host and target TRIDs.
    6647                 :            :          * Only if both are the same should we fail.
    6648                 :            :          */
    6649   [ +  +  -  + ]:       1727 :         if (nvme_ctrlr_get(trid, drv_opts->hostnqn) != NULL) {
    6650   [ #  #  #  # ]:          0 :                 SPDK_ERRLOG("A controller with the provided trid (traddr: %s, hostnqn: %s) "
    6651                 :            :                             "already exists.\n", trid->traddr, drv_opts->hostnqn);
    6652                 :          0 :                 return -EEXIST;
    6653                 :            :         }
    6654                 :            : 
    6655         [ +  + ]:       1727 :         len = strnlen(base_name, SPDK_CONTROLLER_NAME_MAX);
    6656                 :            : 
    6657   [ +  -  -  + ]:       1727 :         if (len == 0 || len == SPDK_CONTROLLER_NAME_MAX) {
    6658                 :          0 :                 SPDK_ERRLOG("controller name must be between 1 and %d characters\n", SPDK_CONTROLLER_NAME_MAX - 1);
    6659                 :          0 :                 return -EINVAL;
    6660                 :            :         }
    6661                 :            : 
    6662   [ +  -  +  - ]:       1728 :         if (bdev_opts != NULL &&
    6663   [ +  +  +  - ]:       1728 :             !bdev_nvme_check_io_error_resiliency_params(bdev_opts->ctrlr_loss_timeout_sec,
    6664   [ +  -  +  - ]:          1 :                             bdev_opts->reconnect_delay_sec,
    6665   [ +  -  +  - ]:          1 :                             bdev_opts->fast_io_fail_timeout_sec)) {
    6666                 :          0 :                 return -EINVAL;
    6667                 :            :         }
    6668                 :            : 
    6669                 :       1727 :         ctx = calloc(1, sizeof(*ctx));
    6670         [ +  + ]:       1727 :         if (!ctx) {
    6671                 :          0 :                 return -ENOMEM;
    6672                 :            :         }
    6673   [ +  -  +  - ]:       1727 :         ctx->base_name = base_name;
    6674   [ +  -  +  - ]:       1727 :         ctx->names = names;
    6675   [ +  -  +  - ]:       1727 :         ctx->max_bdevs = count;
    6676   [ +  -  +  - ]:       1727 :         ctx->cb_fn = cb_fn;
    6677   [ +  -  +  - ]:       1727 :         ctx->cb_ctx = cb_ctx;
    6678         [ +  - ]:       1727 :         ctx->trid = *trid;
    6679                 :            : 
    6680         [ +  + ]:       1727 :         if (bdev_opts) {
    6681   [ +  +  +  +  :       1727 :                 memcpy(&ctx->bdev_opts, bdev_opts, sizeof(*bdev_opts));
                   +  - ]
    6682                 :          1 :         } else {
    6683         [ #  # ]:          0 :                 spdk_bdev_nvme_get_default_ctrlr_opts(&ctx->bdev_opts);
    6684                 :            :         }
    6685                 :            : 
    6686   [ +  +  +  -  :       1727 :         if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
                   -  + ]
    6687   [ +  +  #  #  :        514 :                 TAILQ_FOREACH_SAFE(entry, &g_skipped_nvme_ctrlrs, tailq, tmp) {
          #  #  #  #  +  
                      - ]
    6688   [ +  -  #  # ]:          2 :                         if (spdk_nvme_transport_id_compare(trid, &entry->trid) == 0) {
    6689   [ -  +  #  #  :          2 :                                 TAILQ_REMOVE(&g_skipped_nvme_ctrlrs, entry, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    6690                 :          2 :                                 free(entry);
    6691                 :          2 :                                 break;
    6692                 :            :                         }
    6693                 :          0 :                 }
    6694                 :          1 :         }
    6695                 :            : 
    6696   [ +  +  +  +  :       1727 :         memcpy(&ctx->drv_opts, drv_opts, sizeof(*drv_opts));
                   +  - ]
    6697   [ +  -  +  -  :       1727 :         ctx->drv_opts.transport_retry_count = g_opts.transport_retry_count;
             +  -  +  - ]
    6698   [ +  -  +  -  :       1727 :         ctx->drv_opts.transport_ack_timeout = g_opts.transport_ack_timeout;
             +  -  +  - ]
    6699   [ +  -  +  -  :       1727 :         ctx->drv_opts.keep_alive_timeout_ms = g_opts.keep_alive_timeout_ms;
             +  -  +  - ]
    6700   [ +  -  +  -  :       1727 :         ctx->drv_opts.disable_read_ana_log_page = true;
                   +  - ]
    6701   [ +  -  +  -  :       1727 :         ctx->drv_opts.transport_tos = g_opts.transport_tos;
                   +  - ]
    6702                 :            : 
    6703         [ +  + ]:       1727 :         if (spdk_interrupt_mode_is_enabled()) {
    6704   [ +  -  #  #  :          1 :                 if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
                   #  # ]
    6705   [ #  #  #  #  :          1 :                         ctx->drv_opts.enable_interrupts = true;
                   #  # ]
    6706                 :          0 :                 } else {
    6707                 :          0 :                         SPDK_ERRLOG("Interrupt mode is only supported with PCIe transport\n");
    6708                 :          0 :                         free_nvme_async_probe_ctx(ctx);
    6709                 :          0 :                         return -ENOTSUP;
    6710                 :            :                 }
    6711                 :          0 :         }
    6712                 :            : 
    6713   [ +  +  +  -  :       1727 :         if (ctx->bdev_opts.psk != NULL) {
             +  -  +  - ]
    6714   [ #  #  #  #  :         66 :                 ctx->drv_opts.tls_psk = spdk_keyring_get_key(ctx->bdev_opts.psk);
          #  #  #  #  #  
                #  #  # ]
    6715   [ +  +  #  #  :         66 :                 if (ctx->drv_opts.tls_psk == NULL) {
             #  #  #  # ]
    6716   [ #  #  #  #  :          6 :                         SPDK_ERRLOG("Could not load PSK: %s\n", ctx->bdev_opts.psk);
                   #  # ]
    6717                 :          6 :                         free_nvme_async_probe_ctx(ctx);
    6718                 :          6 :                         return -ENOKEY;
    6719                 :            :                 }
    6720                 :          0 :         }
    6721                 :            : 
    6722   [ +  +  +  -  :       1721 :         if (ctx->bdev_opts.dhchap_key != NULL) {
             +  -  +  - ]
    6723   [ #  #  #  #  :        664 :                 ctx->drv_opts.dhchap_key = spdk_keyring_get_key(ctx->bdev_opts.dhchap_key);
          #  #  #  #  #  
                #  #  # ]
    6724   [ -  +  #  #  :        664 :                 if (ctx->drv_opts.dhchap_key == NULL) {
             #  #  #  # ]
    6725   [ #  #  #  #  :          0 :                         SPDK_ERRLOG("Could not load DH-HMAC-CHAP key: %s\n",
                   #  # ]
    6726                 :            :                                     ctx->bdev_opts.dhchap_key);
    6727                 :          0 :                         free_nvme_async_probe_ctx(ctx);
    6728                 :          0 :                         return -ENOKEY;
    6729                 :            :                 }
    6730                 :            : 
    6731   [ #  #  #  #  :        664 :                 ctx->drv_opts.dhchap_digests = g_opts.dhchap_digests;
             #  #  #  # ]
    6732   [ #  #  #  #  :        664 :                 ctx->drv_opts.dhchap_dhgroups = g_opts.dhchap_dhgroups;
             #  #  #  # ]
    6733                 :          0 :         }
    6734   [ +  +  +  -  :       1721 :         if (ctx->bdev_opts.dhchap_ctrlr_key != NULL) {
             +  -  +  - ]
    6735   [ #  #  #  #  :        500 :                 ctx->drv_opts.dhchap_ctrlr_key =
                   #  # ]
    6736   [ #  #  #  #  :        500 :                         spdk_keyring_get_key(ctx->bdev_opts.dhchap_ctrlr_key);
                   #  # ]
    6737   [ -  +  #  #  :        500 :                 if (ctx->drv_opts.dhchap_ctrlr_key == NULL) {
             #  #  #  # ]
    6738   [ #  #  #  #  :          0 :                         SPDK_ERRLOG("Could not load DH-HMAC-CHAP controller key: %s\n",
                   #  # ]
    6739                 :            :                                     ctx->bdev_opts.dhchap_ctrlr_key);
    6740                 :          0 :                         free_nvme_async_probe_ctx(ctx);
    6741                 :          0 :                         return -ENOKEY;
    6742                 :            :                 }
    6743                 :          0 :         }
    6744                 :            : 
    6745   [ +  +  +  +  :       1721 :         if (nvme_bdev_ctrlr_get_by_name(base_name) == NULL || ctx->bdev_opts.multipath) {
          +  +  #  #  #  
                #  #  # ]
    6746                 :       1693 :                 attach_cb = connect_attach_cb;
    6747                 :          1 :         } else {
    6748                 :         28 :                 attach_cb = connect_set_failover_cb;
    6749                 :            :         }
    6750                 :            : 
    6751   [ +  -  +  - ]:       1721 :         nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->base_name);
    6752   [ +  +  -  +  :       1721 :         if (nvme_ctrlr  && nvme_ctrlr->opts.multipath != ctx->bdev_opts.multipath) {
          -  +  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    6753                 :            :                 /* All controllers with the same name must be configured the same
    6754                 :            :                  * way, either for multipath or failover. If the configuration doesn't
    6755                 :            :                  * match - report error.
    6756                 :            :                  */
    6757                 :          0 :                 free_nvme_async_probe_ctx(ctx);
    6758                 :          0 :                 return -EINVAL;
    6759                 :            :         }
    6760                 :            : 
    6761   [ +  -  +  -  :       1721 :         ctx->probe_ctx = spdk_nvme_connect_async(trid, &ctx->drv_opts, attach_cb);
                   +  - ]
    6762   [ +  +  +  -  :       1721 :         if (ctx->probe_ctx == NULL) {
                   +  - ]
    6763         [ #  # ]:          3 :                 SPDK_ERRLOG("No controller was found with provided trid (traddr: %s)\n", trid->traddr);
    6764                 :          3 :                 free_nvme_async_probe_ctx(ctx);
    6765                 :          3 :                 return -ENODEV;
    6766                 :            :         }
    6767   [ +  -  +  - ]:       1718 :         ctx->poller = SPDK_POLLER_REGISTER(bdev_nvme_async_poll, ctx, 1000);
    6768                 :            : 
    6769                 :       1718 :         return 0;
    6770                 :          1 : }
    6771                 :            : 
    6772                 :            : struct bdev_nvme_delete_ctx {
    6773                 :            :         char                        *name;
    6774                 :            :         struct nvme_path_id         path_id;
    6775                 :            :         bdev_nvme_delete_done_fn    delete_done;
    6776                 :            :         void                        *delete_done_ctx;
    6777                 :            :         uint64_t                    timeout_ticks;
    6778                 :            :         struct spdk_poller          *poller;
    6779                 :            : };
    6780                 :            : 
    6781                 :            : static void
    6782                 :        712 : free_bdev_nvme_delete_ctx(struct bdev_nvme_delete_ctx *ctx)
    6783                 :            : {
    6784         [ +  + ]:        712 :         if (ctx != NULL) {
    6785   [ #  #  #  # ]:        709 :                 free(ctx->name);
    6786                 :        709 :                 free(ctx);
    6787                 :          0 :         }
    6788                 :        712 : }
    6789                 :            : 
    6790                 :            : static bool
    6791                 :      40108 : nvme_path_id_compare(struct nvme_path_id *p, const struct nvme_path_id *path_id)
    6792                 :            : {
    6793   [ +  +  #  #  :      40108 :         if (path_id->trid.trtype != 0) {
             #  #  #  # ]
    6794   [ -  +  #  #  :        205 :                 if (path_id->trid.trtype == SPDK_NVME_TRANSPORT_CUSTOM) {
             #  #  #  # ]
    6795   [ #  #  #  #  :          0 :                         if (strcasecmp(path_id->trid.trstring, p->trid.trstring) != 0) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6796                 :          0 :                                 return false;
    6797                 :            :                         }
    6798                 :          0 :                 } else {
    6799   [ -  +  #  #  :        205 :                         if (path_id->trid.trtype != p->trid.trtype) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6800                 :          0 :                                 return false;
    6801                 :            :                         }
    6802                 :            :                 }
    6803                 :          0 :         }
    6804                 :            : 
    6805   [ +  +  #  #  :      40108 :         if (!spdk_mem_all_zero(path_id->trid.traddr, sizeof(path_id->trid.traddr))) {
                   #  # ]
    6806   [ +  +  -  +  :        205 :                 if (strcasecmp(path_id->trid.traddr, p->trid.traddr) != 0) {
          +  +  #  #  #  
             #  #  #  #  
                      # ]
    6807                 :         33 :                         return false;
    6808                 :            :                 }
    6809                 :          0 :         }
    6810                 :            : 
    6811   [ +  +  #  #  :      40075 :         if (path_id->trid.adrfam != 0) {
             #  #  #  # ]
    6812   [ -  +  #  #  :        142 :                 if (path_id->trid.adrfam != p->trid.adrfam) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6813                 :          0 :                         return false;
    6814                 :            :                 }
    6815                 :          0 :         }
    6816                 :            : 
    6817   [ +  +  #  #  :      40075 :         if (!spdk_mem_all_zero(path_id->trid.trsvcid, sizeof(path_id->trid.trsvcid))) {
                   #  # ]
    6818   [ -  +  -  +  :        172 :                 if (strcasecmp(path_id->trid.trsvcid, p->trid.trsvcid) != 0) {
          +  +  #  #  #  
             #  #  #  #  
                      # ]
    6819                 :         49 :                         return false;
    6820                 :            :                 }
    6821                 :          0 :         }
    6822                 :            : 
    6823   [ +  +  #  #  :      40026 :         if (!spdk_mem_all_zero(path_id->trid.subnqn, sizeof(path_id->trid.subnqn))) {
                   #  # ]
    6824   [ -  +  -  +  :        123 :                 if (strcmp(path_id->trid.subnqn, p->trid.subnqn) != 0) {
          -  +  #  #  #  
             #  #  #  #  
                      # ]
    6825                 :          0 :                         return false;
    6826                 :            :                 }
    6827                 :          0 :         }
    6828                 :            : 
    6829   [ -  +  #  #  :      40026 :         if (!spdk_mem_all_zero(path_id->hostid.hostaddr, sizeof(path_id->hostid.hostaddr))) {
                   #  # ]
    6830   [ #  #  #  #  :          0 :                 if (strcmp(path_id->hostid.hostaddr, p->hostid.hostaddr) != 0) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6831                 :          0 :                         return false;
    6832                 :            :                 }
    6833                 :          0 :         }
    6834                 :            : 
    6835   [ -  +  #  #  :      40026 :         if (!spdk_mem_all_zero(path_id->hostid.hostsvcid, sizeof(path_id->hostid.hostsvcid))) {
                   #  # ]
    6836   [ #  #  #  #  :          0 :                 if (strcmp(path_id->hostid.hostsvcid, p->hostid.hostsvcid) != 0) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6837                 :          0 :                         return false;
    6838                 :            :                 }
    6839                 :          0 :         }
    6840                 :            : 
    6841                 :      40026 :         return true;
    6842                 :          0 : }
    6843                 :            : 
    6844                 :            : static bool
    6845                 :      39803 : nvme_path_id_exists(const char *name, const struct nvme_path_id *path_id)
    6846                 :            : {
    6847                 :            :         struct nvme_bdev_ctrlr  *nbdev_ctrlr;
    6848                 :            :         struct nvme_ctrlr       *ctrlr;
    6849                 :            :         struct nvme_path_id     *p;
    6850                 :            : 
    6851         [ -  + ]:      39803 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    6852                 :      39803 :         nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
    6853         [ +  + ]:      39803 :         if (!nbdev_ctrlr) {
    6854         [ -  + ]:        699 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6855                 :        699 :                 return false;
    6856                 :            :         }
    6857                 :            : 
    6858   [ +  +  #  #  :      39130 :         TAILQ_FOREACH(ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6859   [ -  +  #  # ]:      39120 :                 pthread_mutex_lock(&ctrlr->mutex);
    6860   [ +  +  #  #  :      39150 :                 TAILQ_FOREACH(p, &ctrlr->trids, link) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    6861         [ +  + ]:      39124 :                         if (nvme_path_id_compare(p, path_id)) {
    6862   [ -  +  #  # ]:      39094 :                                 pthread_mutex_unlock(&ctrlr->mutex);
    6863         [ -  + ]:      39094 :                                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6864                 :      39094 :                                 return true;
    6865                 :            :                         }
    6866                 :          0 :                 }
    6867   [ -  +  #  # ]:         26 :                 pthread_mutex_unlock(&ctrlr->mutex);
    6868                 :          0 :         }
    6869         [ -  + ]:         10 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6870                 :            : 
    6871                 :         10 :         return false;
    6872                 :          0 : }
    6873                 :            : 
    6874                 :            : static int
    6875                 :      39803 : bdev_nvme_delete_complete_poll(void *arg)
    6876                 :            : {
    6877                 :      39803 :         struct bdev_nvme_delete_ctx     *ctx = arg;
    6878                 :      39803 :         int                             rc = 0;
    6879                 :            : 
    6880   [ +  +  #  #  :      39803 :         if (nvme_path_id_exists(ctx->name, &ctx->path_id)) {
             #  #  #  # ]
    6881   [ +  -  #  #  :      39094 :                 if (ctx->timeout_ticks > spdk_get_ticks()) {
                   #  # ]
    6882                 :      39094 :                         return SPDK_POLLER_BUSY;
    6883                 :            :                 }
    6884                 :            : 
    6885   [ #  #  #  # ]:          0 :                 SPDK_ERRLOG("NVMe path '%s' still exists after delete\n", ctx->name);
    6886                 :          0 :                 rc = -ETIMEDOUT;
    6887                 :          0 :         }
    6888                 :            : 
    6889         [ #  # ]:        709 :         spdk_poller_unregister(&ctx->poller);
    6890                 :            : 
    6891   [ #  #  #  #  :        709 :         ctx->delete_done(ctx->delete_done_ctx, rc);
          #  #  #  #  #  
                #  #  # ]
    6892                 :        709 :         free_bdev_nvme_delete_ctx(ctx);
    6893                 :            : 
    6894                 :        709 :         return SPDK_POLLER_BUSY;
    6895                 :          0 : }
    6896                 :            : 
    6897                 :            : static int
    6898                 :        942 : _bdev_nvme_delete(struct nvme_ctrlr *nvme_ctrlr, const struct nvme_path_id *path_id)
    6899                 :            : {
    6900                 :            :         struct nvme_path_id     *p, *t;
    6901                 :            :         spdk_msg_fn             msg_fn;
    6902                 :        942 :         int                     rc = -ENXIO;
    6903                 :            : 
    6904   [ -  +  #  # ]:        942 :         pthread_mutex_lock(&nvme_ctrlr->mutex);
    6905                 :            : 
    6906   [ +  -  #  #  :        984 :         TAILQ_FOREACH_REVERSE_SAFE(p, &nvme_ctrlr->trids, nvme_paths, link, t) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    6907   [ +  +  #  #  :        984 :                 if (p == TAILQ_FIRST(&nvme_ctrlr->trids)) {
             #  #  #  # ]
    6908                 :        942 :                         break;
    6909                 :            :                 }
    6910                 :            : 
    6911         [ +  + ]:         42 :                 if (!nvme_path_id_compare(p, path_id)) {
    6912                 :         17 :                         continue;
    6913                 :            :                 }
    6914                 :            : 
    6915                 :            :                 /* We are not using the specified path. */
    6916   [ +  +  #  #  :         25 :                 TAILQ_REMOVE(&nvme_ctrlr->trids, p, link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    6917                 :         25 :                 free(p);
    6918                 :         25 :                 rc = 0;
    6919                 :          0 :         }
    6920                 :            : 
    6921   [ +  -  +  + ]:        942 :         if (p == NULL || !nvme_path_id_compare(p, path_id)) {
    6922   [ -  +  #  # ]:         35 :                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    6923                 :         35 :                 return rc;
    6924                 :            :         }
    6925                 :            : 
    6926                 :            :         /* If we made it here, then this path is a match! Now we need to remove it. */
    6927                 :            : 
    6928                 :            :         /* This is the active path in use right now. The active path is always the first in the list. */
    6929   [ -  +  #  #  :        907 :         assert(p == nvme_ctrlr->active_path_id);
             #  #  #  # ]
    6930                 :            : 
    6931   [ +  +  #  #  :        907 :         if (!TAILQ_NEXT(p, link)) {
             #  #  #  # ]
    6932                 :            :                 /* The current path is the only path. */
    6933                 :        900 :                 msg_fn = _nvme_ctrlr_destruct;
    6934                 :        900 :                 rc = bdev_nvme_delete_ctrlr_unsafe(nvme_ctrlr, false);
    6935                 :          0 :         } else {
    6936                 :            :                 /* There is an alternative path. */
    6937                 :          7 :                 msg_fn = _bdev_nvme_reset_ctrlr;
    6938                 :          7 :                 rc = bdev_nvme_failover_ctrlr_unsafe(nvme_ctrlr, true);
    6939                 :            :         }
    6940                 :            : 
    6941   [ -  +  #  # ]:        907 :         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    6942                 :            : 
    6943         [ +  - ]:        907 :         if (rc == 0) {
    6944   [ #  #  #  # ]:        907 :                 spdk_thread_send_msg(nvme_ctrlr->thread, msg_fn, nvme_ctrlr);
    6945         [ #  # ]:          0 :         } else if (rc == -EALREADY) {
    6946                 :          0 :                 rc = 0;
    6947                 :          0 :         }
    6948                 :            : 
    6949                 :        907 :         return rc;
    6950                 :          0 : }
    6951                 :            : 
    6952                 :            : int
    6953                 :        890 : bdev_nvme_delete(const char *name, const struct nvme_path_id *path_id,
    6954                 :            :                  bdev_nvme_delete_done_fn delete_done, void *delete_done_ctx)
    6955                 :            : {
    6956                 :            :         struct nvme_bdev_ctrlr          *nbdev_ctrlr;
    6957                 :            :         struct nvme_ctrlr               *nvme_ctrlr, *tmp_nvme_ctrlr;
    6958                 :        890 :         struct bdev_nvme_delete_ctx     *ctx = NULL;
    6959                 :        890 :         int                             rc = -ENXIO, _rc;
    6960                 :            : 
    6961   [ +  -  -  + ]:        890 :         if (name == NULL || path_id == NULL) {
    6962                 :          0 :                 rc = -EINVAL;
    6963                 :          0 :                 goto exit;
    6964                 :            :         }
    6965                 :            : 
    6966         [ -  + ]:        890 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    6967                 :            : 
    6968                 :        890 :         nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
    6969         [ -  + ]:        890 :         if (nbdev_ctrlr == NULL) {
    6970         [ #  # ]:          0 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6971                 :            : 
    6972                 :          0 :                 SPDK_ERRLOG("Failed to find NVMe bdev controller\n");
    6973                 :          0 :                 rc = -ENODEV;
    6974                 :          0 :                 goto exit;
    6975                 :            :         }
    6976                 :            : 
    6977   [ +  +  #  #  :       1832 :         TAILQ_FOREACH_SAFE(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq, tmp_nvme_ctrlr) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    6978                 :        942 :                 _rc = _bdev_nvme_delete(nvme_ctrlr, path_id);
    6979   [ +  +  -  + ]:        942 :                 if (_rc < 0 && _rc != -ENXIO) {
    6980         [ #  # ]:          0 :                         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6981                 :          0 :                         rc = _rc;
    6982                 :          0 :                         goto exit;
    6983         [ +  + ]:        942 :                 } else if (_rc == 0) {
    6984                 :            :                         /* We traverse all remaining nvme_ctrlrs even if one nvme_ctrlr
    6985                 :            :                          * was deleted successfully. To remember the successful deletion,
    6986                 :            :                          * overwrite rc only if _rc is zero.
    6987                 :            :                          */
    6988                 :        917 :                         rc = 0;
    6989                 :          0 :                 }
    6990                 :          0 :         }
    6991                 :            : 
    6992         [ -  + ]:        890 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    6993                 :            : 
    6994   [ +  +  +  + ]:        890 :         if (rc != 0 || delete_done == NULL) {
    6995                 :        181 :                 goto exit;
    6996                 :            :         }
    6997                 :            : 
    6998                 :        709 :         ctx = calloc(1, sizeof(*ctx));
    6999         [ -  + ]:        709 :         if (ctx == NULL) {
    7000                 :          0 :                 SPDK_ERRLOG("Failed to allocate context for bdev_nvme_delete\n");
    7001                 :          0 :                 rc = -ENOMEM;
    7002                 :          0 :                 goto exit;
    7003                 :            :         }
    7004                 :            : 
    7005   [ -  +  #  #  :        709 :         ctx->name = strdup(name);
                   #  # ]
    7006   [ -  +  #  #  :        709 :         if (ctx->name == NULL) {
                   #  # ]
    7007                 :          0 :                 SPDK_ERRLOG("Failed to copy controller name for deletion\n");
    7008                 :          0 :                 rc = -ENOMEM;
    7009                 :          0 :                 goto exit;
    7010                 :            :         }
    7011                 :            : 
    7012   [ #  #  #  # ]:        709 :         ctx->delete_done = delete_done;
    7013   [ #  #  #  # ]:        709 :         ctx->delete_done_ctx = delete_done_ctx;
    7014         [ #  # ]:        709 :         ctx->path_id = *path_id;
    7015   [ #  #  #  # ]:        709 :         ctx->timeout_ticks = spdk_get_ticks() + 10 * spdk_get_ticks_hz();
    7016   [ #  #  #  # ]:        709 :         ctx->poller = SPDK_POLLER_REGISTER(bdev_nvme_delete_complete_poll, ctx, 1000);
    7017   [ +  -  #  #  :        709 :         if (ctx->poller == NULL) {
                   #  # ]
    7018                 :          0 :                 SPDK_ERRLOG("Failed to register bdev_nvme_delete poller\n");
    7019                 :          0 :                 rc = -ENOMEM;
    7020                 :          0 :                 goto exit;
    7021                 :            :         }
    7022                 :            : 
    7023                 :        709 : exit:
    7024         [ +  + ]:        890 :         if (rc != 0) {
    7025                 :          3 :                 free_bdev_nvme_delete_ctx(ctx);
    7026                 :          0 :         }
    7027                 :            : 
    7028                 :        890 :         return rc;
    7029                 :            : }
    7030                 :            : 
    7031                 :            : #define DISCOVERY_INFOLOG(ctx, format, ...) \
    7032                 :            :         SPDK_INFOLOG(bdev_nvme, "Discovery[%s:%s] " format, ctx->trid.traddr, ctx->trid.trsvcid, ##__VA_ARGS__);
    7033                 :            : 
    7034                 :            : #define DISCOVERY_ERRLOG(ctx, format, ...) \
    7035                 :            :         SPDK_ERRLOG("Discovery[%s:%s] " format, ctx->trid.traddr, ctx->trid.trsvcid, ##__VA_ARGS__);
    7036                 :            : 
    7037                 :            : struct discovery_entry_ctx {
    7038                 :            :         char                                            name[128];
    7039                 :            :         struct spdk_nvme_transport_id                   trid;
    7040                 :            :         struct spdk_nvme_ctrlr_opts                     drv_opts;
    7041                 :            :         struct spdk_nvmf_discovery_log_page_entry       entry;
    7042                 :            :         TAILQ_ENTRY(discovery_entry_ctx)                tailq;
    7043                 :            :         struct discovery_ctx                            *ctx;
    7044                 :            : };
    7045                 :            : 
    7046                 :            : struct discovery_ctx {
    7047                 :            :         char                                    *name;
    7048                 :            :         spdk_bdev_nvme_start_discovery_fn       start_cb_fn;
    7049                 :            :         spdk_bdev_nvme_stop_discovery_fn        stop_cb_fn;
    7050                 :            :         void                                    *cb_ctx;
    7051                 :            :         struct spdk_nvme_probe_ctx              *probe_ctx;
    7052                 :            :         struct spdk_nvme_detach_ctx             *detach_ctx;
    7053                 :            :         struct spdk_nvme_ctrlr                  *ctrlr;
    7054                 :            :         struct spdk_nvme_transport_id           trid;
    7055                 :            :         struct discovery_entry_ctx              *entry_ctx_in_use;
    7056                 :            :         struct spdk_poller                      *poller;
    7057                 :            :         struct spdk_nvme_ctrlr_opts             drv_opts;
    7058                 :            :         struct spdk_bdev_nvme_ctrlr_opts        bdev_opts;
    7059                 :            :         struct spdk_nvmf_discovery_log_page     *log_page;
    7060                 :            :         TAILQ_ENTRY(discovery_ctx)              tailq;
    7061                 :            :         TAILQ_HEAD(, discovery_entry_ctx)       nvm_entry_ctxs;
    7062                 :            :         TAILQ_HEAD(, discovery_entry_ctx)       discovery_entry_ctxs;
    7063                 :            :         int                                     rc;
    7064                 :            :         bool                                    wait_for_attach;
    7065                 :            :         uint64_t                                timeout_ticks;
    7066                 :            :         /* Denotes that the discovery service is being started. We're waiting
    7067                 :            :          * for the initial connection to the discovery controller to be
    7068                 :            :          * established and attach discovered NVM ctrlrs.
    7069                 :            :          */
    7070                 :            :         bool                                    initializing;
    7071                 :            :         /* Denotes if a discovery is currently in progress for this context.
    7072                 :            :          * That includes connecting to newly discovered subsystems.  Used to
    7073                 :            :          * ensure we do not start a new discovery until an existing one is
    7074                 :            :          * complete.
    7075                 :            :          */
    7076                 :            :         bool                                    in_progress;
    7077                 :            : 
    7078                 :            :         /* Denotes if another discovery is needed after the one in progress
    7079                 :            :          * completes.  Set when we receive an AER completion while a discovery
    7080                 :            :          * is already in progress.
    7081                 :            :          */
    7082                 :            :         bool                                    pending;
    7083                 :            : 
    7084                 :            :         /* Signal to the discovery context poller that it should stop the
    7085                 :            :          * discovery service, including detaching from the current discovery
    7086                 :            :          * controller.
    7087                 :            :          */
    7088                 :            :         bool                                    stop;
    7089                 :            : 
    7090                 :            :         struct spdk_thread                      *calling_thread;
    7091                 :            :         uint32_t                                index;
    7092                 :            :         uint32_t                                attach_in_progress;
    7093                 :            :         char                                    *hostnqn;
    7094                 :            : 
    7095                 :            :         /* Denotes if the discovery service was started by the mdns discovery.
    7096                 :            :          */
    7097                 :            :         bool                                    from_mdns_discovery_service;
    7098                 :            : };
    7099                 :            : 
    7100                 :            : TAILQ_HEAD(discovery_ctxs, discovery_ctx);
    7101                 :            : static struct discovery_ctxs g_discovery_ctxs = TAILQ_HEAD_INITIALIZER(g_discovery_ctxs);
    7102                 :            : 
    7103                 :            : static void get_discovery_log_page(struct discovery_ctx *ctx);
    7104                 :            : 
    7105                 :            : static void
    7106                 :         34 : free_discovery_ctx(struct discovery_ctx *ctx)
    7107                 :            : {
    7108   [ #  #  #  # ]:         34 :         free(ctx->log_page);
    7109   [ #  #  #  # ]:         34 :         free(ctx->hostnqn);
    7110   [ #  #  #  # ]:         34 :         free(ctx->name);
    7111                 :         34 :         free(ctx);
    7112                 :         34 : }
    7113                 :            : 
    7114                 :            : static void
    7115                 :         49 : discovery_complete(struct discovery_ctx *ctx)
    7116                 :            : {
    7117   [ #  #  #  # ]:         49 :         ctx->initializing = false;
    7118   [ #  #  #  # ]:         49 :         ctx->in_progress = false;
    7119   [ -  +  +  +  :         49 :         if (ctx->pending) {
             #  #  #  # ]
    7120   [ #  #  #  # ]:          5 :                 ctx->pending = false;
    7121                 :          5 :                 get_discovery_log_page(ctx);
    7122                 :          0 :         }
    7123                 :         49 : }
    7124                 :            : 
    7125                 :            : static void
    7126                 :        161 : build_trid_from_log_page_entry(struct spdk_nvme_transport_id *trid,
    7127                 :            :                                struct spdk_nvmf_discovery_log_page_entry *entry)
    7128                 :            : {
    7129                 :            :         char *space;
    7130                 :            : 
    7131   [ #  #  #  #  :        161 :         trid->trtype = entry->trtype;
             #  #  #  # ]
    7132   [ #  #  #  #  :        161 :         trid->adrfam = entry->adrfam;
             #  #  #  # ]
    7133   [ -  +  -  +  :        161 :         memcpy(trid->traddr, entry->traddr, sizeof(entry->traddr));
             #  #  #  # ]
    7134   [ -  +  -  +  :        161 :         memcpy(trid->trsvcid, entry->trsvcid, sizeof(entry->trsvcid));
             #  #  #  # ]
    7135                 :            :         /* Because the source buffer (entry->subnqn) is longer than trid->subnqn, and
    7136                 :            :          * before call to this function trid->subnqn is zeroed out, we need
    7137                 :            :          * to copy sizeof(trid->subnqn) minus one byte to make sure the last character
    7138                 :            :          * remains 0. Then we can shorten the string (replace ' ' with 0) if required
    7139                 :            :          */
    7140   [ -  +  -  +  :        161 :         memcpy(trid->subnqn, entry->subnqn, sizeof(trid->subnqn) - 1);
             #  #  #  # ]
    7141                 :            : 
    7142                 :            :         /* We want the traddr, trsvcid and subnqn fields to be NULL-terminated.
    7143                 :            :          * But the log page entries typically pad them with spaces, not zeroes.
    7144                 :            :          * So add a NULL terminator to each of these fields at the appropriate
    7145                 :            :          * location.
    7146                 :            :          */
    7147   [ -  +  #  # ]:        161 :         space = strchr(trid->traddr, ' ');
    7148         [ +  - ]:        161 :         if (space) {
    7149         [ #  # ]:        161 :                 *space = 0;
    7150                 :          0 :         }
    7151   [ -  +  #  # ]:        161 :         space = strchr(trid->trsvcid, ' ');
    7152         [ +  - ]:        161 :         if (space) {
    7153         [ #  # ]:        161 :                 *space = 0;
    7154                 :          0 :         }
    7155   [ -  +  #  # ]:        161 :         space = strchr(trid->subnqn, ' ');
    7156         [ -  + ]:        161 :         if (space) {
    7157         [ #  # ]:          0 :                 *space = 0;
    7158                 :          0 :         }
    7159                 :        161 : }
    7160                 :            : 
    7161                 :            : static void
    7162                 :         34 : _stop_discovery(void *_ctx)
    7163                 :            : {
    7164                 :         34 :         struct discovery_ctx *ctx = _ctx;
    7165                 :            : 
    7166   [ -  +  #  #  :         34 :         if (ctx->attach_in_progress > 0) {
                   #  # ]
    7167                 :          0 :                 spdk_thread_send_msg(spdk_get_thread(), _stop_discovery, ctx);
    7168                 :          0 :                 return;
    7169                 :            :         }
    7170                 :            : 
    7171   [ #  #  #  # ]:         34 :         ctx->stop = true;
    7172                 :            : 
    7173   [ +  +  #  #  :         63 :         while (!TAILQ_EMPTY(&ctx->nvm_entry_ctxs)) {
             #  #  #  # ]
    7174                 :            :                 struct discovery_entry_ctx *entry_ctx;
    7175                 :         29 :                 struct nvme_path_id path = {};
    7176                 :            : 
    7177   [ #  #  #  #  :         29 :                 entry_ctx = TAILQ_FIRST(&ctx->nvm_entry_ctxs);
                   #  # ]
    7178         [ #  # ]:         29 :                 path.trid = entry_ctx->trid;
    7179         [ #  # ]:         29 :                 bdev_nvme_delete(entry_ctx->name, &path, NULL, NULL);
    7180   [ -  +  #  #  :         29 :                 TAILQ_REMOVE(&ctx->nvm_entry_ctxs, entry_ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7181                 :         29 :                 free(entry_ctx);
    7182                 :            :         }
    7183                 :            : 
    7184   [ +  +  #  #  :         77 :         while (!TAILQ_EMPTY(&ctx->discovery_entry_ctxs)) {
             #  #  #  # ]
    7185                 :            :                 struct discovery_entry_ctx *entry_ctx;
    7186                 :            : 
    7187   [ #  #  #  #  :         43 :                 entry_ctx = TAILQ_FIRST(&ctx->discovery_entry_ctxs);
                   #  # ]
    7188   [ +  +  #  #  :         43 :                 TAILQ_REMOVE(&ctx->discovery_entry_ctxs, entry_ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7189                 :         43 :                 free(entry_ctx);
    7190                 :            :         }
    7191                 :            : 
    7192   [ #  #  #  # ]:         34 :         free(ctx->entry_ctx_in_use);
    7193   [ #  #  #  # ]:         34 :         ctx->entry_ctx_in_use = NULL;
    7194                 :          0 : }
    7195                 :            : 
    7196                 :            : static void
    7197                 :         34 : stop_discovery(struct discovery_ctx *ctx, spdk_bdev_nvme_stop_discovery_fn cb_fn, void *cb_ctx)
    7198                 :            : {
    7199   [ #  #  #  # ]:         34 :         ctx->stop_cb_fn = cb_fn;
    7200   [ #  #  #  # ]:         34 :         ctx->cb_ctx = cb_ctx;
    7201                 :            : 
    7202   [ -  +  #  #  :         34 :         if (ctx->attach_in_progress > 0) {
                   #  # ]
    7203   [ #  #  #  #  :          0 :                 DISCOVERY_INFOLOG(ctx, "stopping discovery with attach_in_progress: %"PRIu32"\n",
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    7204                 :            :                                   ctx->attach_in_progress);
    7205                 :          0 :         }
    7206                 :            : 
    7207                 :         34 :         _stop_discovery(ctx);
    7208                 :         34 : }
    7209                 :            : 
    7210                 :            : static void
    7211                 :         29 : remove_discovery_entry(struct nvme_ctrlr *nvme_ctrlr)
    7212                 :            : {
    7213                 :            :         struct discovery_ctx *d_ctx;
    7214                 :            :         struct nvme_path_id *path_id;
    7215                 :         29 :         struct spdk_nvme_transport_id trid = {};
    7216                 :            :         struct discovery_entry_ctx *entry_ctx, *tmp;
    7217                 :            : 
    7218   [ #  #  #  #  :         29 :         path_id = TAILQ_FIRST(&nvme_ctrlr->trids);
                   #  # ]
    7219                 :            : 
    7220   [ +  +  #  #  :         32 :         TAILQ_FOREACH(d_ctx, &g_discovery_ctxs, tailq) {
             #  #  #  # ]
    7221   [ +  +  #  #  :          6 :                 TAILQ_FOREACH_SAFE(entry_ctx, &d_ctx->nvm_entry_ctxs, tailq, tmp) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7222         [ #  # ]:          3 :                         build_trid_from_log_page_entry(&trid, &entry_ctx->entry);
    7223   [ -  +  #  # ]:          3 :                         if (spdk_nvme_transport_id_compare(&trid, &path_id->trid) != 0) {
    7224                 :          0 :                                 continue;
    7225                 :            :                         }
    7226                 :            : 
    7227   [ -  +  #  #  :          3 :                         TAILQ_REMOVE(&d_ctx->nvm_entry_ctxs, entry_ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7228                 :          3 :                         free(entry_ctx);
    7229   [ -  +  +  -  :          3 :                         DISCOVERY_INFOLOG(d_ctx, "Remove discovery entry: %s:%s:%s\n",
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7230                 :            :                                           trid.subnqn, trid.traddr, trid.trsvcid);
    7231                 :            : 
    7232                 :            :                         /* Fail discovery ctrlr to force reattach attempt */
    7233   [ #  #  #  # ]:          3 :                         spdk_nvme_ctrlr_fail(d_ctx->ctrlr);
    7234                 :          0 :                 }
    7235                 :          0 :         }
    7236                 :         29 : }
    7237                 :            : 
    7238                 :            : static void
    7239                 :         49 : discovery_remove_controllers(struct discovery_ctx *ctx)
    7240                 :            : {
    7241   [ #  #  #  # ]:         49 :         struct spdk_nvmf_discovery_log_page *log_page = ctx->log_page;
    7242                 :            :         struct discovery_entry_ctx *entry_ctx, *tmp;
    7243                 :            :         struct spdk_nvmf_discovery_log_page_entry *new_entry, *old_entry;
    7244                 :         49 :         struct spdk_nvme_transport_id old_trid = {};
    7245                 :            :         uint64_t numrec, i;
    7246                 :            :         bool found;
    7247                 :            : 
    7248         [ #  # ]:         49 :         numrec = from_le64(&log_page->numrec);
    7249   [ +  +  #  #  :        109 :         TAILQ_FOREACH_SAFE(entry_ctx, &ctx->nvm_entry_ctxs, tailq, tmp) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7250                 :         60 :                 found = false;
    7251         [ #  # ]:         60 :                 old_entry = &entry_ctx->entry;
    7252                 :         60 :                 build_trid_from_log_page_entry(&old_trid, old_entry);
    7253         [ +  + ]:        143 :                 for (i = 0; i < numrec; i++) {
    7254   [ #  #  #  # ]:        138 :                         new_entry = &log_page->entries[i];
    7255   [ -  +  -  +  :        138 :                         if (!memcmp(old_entry, new_entry, sizeof(*old_entry))) {
                   +  + ]
    7256   [ -  +  +  +  :         55 :                                 DISCOVERY_INFOLOG(ctx, "NVM %s:%s:%s found again\n",
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7257                 :            :                                                   old_trid.subnqn, old_trid.traddr, old_trid.trsvcid);
    7258                 :         55 :                                 found = true;
    7259                 :         55 :                                 break;
    7260                 :            :                         }
    7261                 :          0 :                 }
    7262   [ +  +  #  # ]:         60 :                 if (!found) {
    7263                 :          5 :                         struct nvme_path_id path = {};
    7264                 :            : 
    7265   [ -  +  +  -  :          5 :                         DISCOVERY_INFOLOG(ctx, "NVM %s:%s:%s not found\n",
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7266                 :            :                                           old_trid.subnqn, old_trid.traddr, old_trid.trsvcid);
    7267                 :            : 
    7268         [ #  # ]:          5 :                         path.trid = entry_ctx->trid;
    7269         [ #  # ]:          5 :                         bdev_nvme_delete(entry_ctx->name, &path, NULL, NULL);
    7270   [ +  -  #  #  :          5 :                         TAILQ_REMOVE(&ctx->nvm_entry_ctxs, entry_ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7271                 :          5 :                         free(entry_ctx);
    7272                 :          0 :                 }
    7273                 :          0 :         }
    7274                 :         49 :         free(log_page);
    7275   [ #  #  #  # ]:         49 :         ctx->log_page = NULL;
    7276                 :         49 :         discovery_complete(ctx);
    7277                 :         49 : }
    7278                 :            : 
    7279                 :            : static void
    7280                 :         42 : complete_discovery_start(struct discovery_ctx *ctx, int status)
    7281                 :            : {
    7282   [ #  #  #  # ]:         42 :         ctx->timeout_ticks = 0;
    7283   [ #  #  #  # ]:         42 :         ctx->rc = status;
    7284   [ +  +  #  #  :         42 :         if (ctx->start_cb_fn) {
                   #  # ]
    7285   [ #  #  #  #  :         27 :                 ctx->start_cb_fn(ctx->cb_ctx, status);
          #  #  #  #  #  
                #  #  # ]
    7286   [ #  #  #  # ]:         27 :                 ctx->start_cb_fn = NULL;
    7287   [ #  #  #  # ]:         27 :                 ctx->cb_ctx = NULL;
    7288                 :          0 :         }
    7289                 :         42 : }
    7290                 :            : 
    7291                 :            : static void
    7292                 :         37 : discovery_attach_controller_done(void *cb_ctx, size_t bdev_count, int rc)
    7293                 :            : {
    7294                 :         37 :         struct discovery_entry_ctx *entry_ctx = cb_ctx;
    7295   [ #  #  #  # ]:         37 :         struct discovery_ctx *ctx = entry_ctx->ctx;
    7296                 :            : 
    7297   [ -  +  +  +  :         37 :         DISCOVERY_INFOLOG(ctx, "attach %s done\n", entry_ctx->name);
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7298         [ #  # ]:         37 :         ctx->attach_in_progress--;
    7299   [ +  -  #  #  :         37 :         if (ctx->attach_in_progress == 0) {
                   #  # ]
    7300   [ #  #  #  # ]:         37 :                 complete_discovery_start(ctx, ctx->rc);
    7301   [ -  +  +  +  :         37 :                 if (ctx->initializing && ctx->rc != 0) {
          -  +  #  #  #  
             #  #  #  #  
                      # ]
    7302   [ #  #  #  #  :          0 :                         DISCOVERY_ERRLOG(ctx, "stopping discovery due to errors: %d\n", ctx->rc);
          #  #  #  #  #  
                #  #  # ]
    7303   [ #  #  #  # ]:          0 :                         stop_discovery(ctx, NULL, ctx->cb_ctx);
    7304                 :          0 :                 } else {
    7305                 :         37 :                         discovery_remove_controllers(ctx);
    7306                 :            :                 }
    7307                 :          0 :         }
    7308                 :         37 : }
    7309                 :            : 
    7310                 :            : static struct discovery_entry_ctx *
    7311                 :         95 : create_discovery_entry_ctx(struct discovery_ctx *ctx, struct spdk_nvme_transport_id *trid)
    7312                 :            : {
    7313                 :            :         struct discovery_entry_ctx *new_ctx;
    7314                 :            : 
    7315                 :         95 :         new_ctx = calloc(1, sizeof(*new_ctx));
    7316         [ -  + ]:         95 :         if (new_ctx == NULL) {
    7317   [ #  #  #  #  :          0 :                 DISCOVERY_ERRLOG(ctx, "could not allocate new entry_ctx\n");
             #  #  #  # ]
    7318                 :          0 :                 return NULL;
    7319                 :            :         }
    7320                 :            : 
    7321   [ #  #  #  # ]:         95 :         new_ctx->ctx = ctx;
    7322   [ -  +  -  +  :         95 :         memcpy(&new_ctx->trid, trid, sizeof(*trid));
                   #  # ]
    7323         [ #  # ]:         95 :         spdk_nvme_ctrlr_get_default_ctrlr_opts(&new_ctx->drv_opts, sizeof(new_ctx->drv_opts));
    7324   [ -  +  #  #  :         95 :         snprintf(new_ctx->drv_opts.hostnqn, sizeof(new_ctx->drv_opts.hostnqn), "%s", ctx->hostnqn);
             #  #  #  # ]
    7325                 :         95 :         return new_ctx;
    7326                 :          0 : }
    7327                 :            : 
    7328                 :            : static void
    7329                 :         49 : discovery_log_page_cb(void *cb_arg, int rc, const struct spdk_nvme_cpl *cpl,
    7330                 :            :                       struct spdk_nvmf_discovery_log_page *log_page)
    7331                 :            : {
    7332                 :         49 :         struct discovery_ctx *ctx = cb_arg;
    7333                 :            :         struct discovery_entry_ctx *entry_ctx, *tmp;
    7334                 :            :         struct spdk_nvmf_discovery_log_page_entry *new_entry, *old_entry;
    7335                 :            :         uint64_t numrec, i;
    7336                 :            :         bool found;
    7337                 :            : 
    7338   [ +  -  +  -  :         49 :         if (rc || spdk_nvme_cpl_is_error(cpl)) {
          -  +  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    7339   [ #  #  #  #  :          0 :                 DISCOVERY_ERRLOG(ctx, "could not get discovery log page\n");
             #  #  #  # ]
    7340                 :          0 :                 return;
    7341                 :            :         }
    7342                 :            : 
    7343   [ #  #  #  # ]:         49 :         ctx->log_page = log_page;
    7344   [ -  +  #  #  :         49 :         assert(ctx->attach_in_progress == 0);
             #  #  #  # ]
    7345         [ #  # ]:         49 :         numrec = from_le64(&log_page->numrec);
    7346   [ +  +  #  #  :         71 :         TAILQ_FOREACH_SAFE(entry_ctx, &ctx->discovery_entry_ctxs, tailq, tmp) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7347   [ +  +  #  #  :         22 :                 TAILQ_REMOVE(&ctx->discovery_entry_ctxs, entry_ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7348                 :         22 :                 free(entry_ctx);
    7349                 :          0 :         }
    7350         [ +  + ]:        165 :         for (i = 0; i < numrec; i++) {
    7351                 :        116 :                 found = false;
    7352   [ #  #  #  # ]:        116 :                 new_entry = &log_page->entries[i];
    7353   [ +  +  #  #  :        116 :                 if (new_entry->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY_CURRENT ||
             #  #  #  # ]
    7354   [ -  +  #  # ]:         55 :                     new_entry->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
    7355                 :            :                         struct discovery_entry_ctx *new_ctx;
    7356                 :         61 :                         struct spdk_nvme_transport_id trid = {};
    7357                 :            : 
    7358                 :         61 :                         build_trid_from_log_page_entry(&trid, new_entry);
    7359                 :         61 :                         new_ctx = create_discovery_entry_ctx(ctx, &trid);
    7360         [ -  + ]:         61 :                         if (new_ctx == NULL) {
    7361   [ #  #  #  #  :          0 :                                 DISCOVERY_ERRLOG(ctx, "could not allocate new entry_ctx\n");
             #  #  #  # ]
    7362                 :          0 :                                 break;
    7363                 :            :                         }
    7364                 :            : 
    7365   [ #  #  #  #  :         61 :                         TAILQ_INSERT_TAIL(&ctx->discovery_entry_ctxs, new_ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7366                 :         61 :                         continue;
    7367                 :            :                 }
    7368   [ +  +  #  #  :         67 :                 TAILQ_FOREACH(entry_ctx, &ctx->nvm_entry_ctxs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7369         [ #  # ]:         30 :                         old_entry = &entry_ctx->entry;
    7370   [ -  +  -  +  :         30 :                         if (!memcmp(new_entry, old_entry, sizeof(*new_entry))) {
                   +  + ]
    7371                 :         18 :                                 found = true;
    7372                 :         18 :                                 break;
    7373                 :            :                         }
    7374                 :          0 :                 }
    7375   [ +  +  #  # ]:         55 :                 if (!found) {
    7376                 :         37 :                         struct discovery_entry_ctx *subnqn_ctx = NULL, *new_ctx;
    7377                 :            :                         struct discovery_ctx *d_ctx;
    7378                 :            : 
    7379   [ +  +  #  #  :         77 :                         TAILQ_FOREACH(d_ctx, &g_discovery_ctxs, tailq) {
             #  #  #  # ]
    7380   [ +  +  #  #  :         51 :                                 TAILQ_FOREACH(subnqn_ctx, &d_ctx->nvm_entry_ctxs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7381   [ -  +  -  +  :         11 :                                         if (!memcmp(subnqn_ctx->entry.subnqn, new_entry->subnqn,
          +  +  #  #  #  
                #  #  # ]
    7382                 :            :                                                     sizeof(new_entry->subnqn))) {
    7383                 :          5 :                                                 break;
    7384                 :            :                                         }
    7385                 :          0 :                                 }
    7386         [ +  + ]:         45 :                                 if (subnqn_ctx) {
    7387                 :          5 :                                         break;
    7388                 :            :                                 }
    7389                 :          0 :                         }
    7390                 :            : 
    7391                 :         37 :                         new_ctx = calloc(1, sizeof(*new_ctx));
    7392         [ -  + ]:         37 :                         if (new_ctx == NULL) {
    7393   [ #  #  #  #  :          0 :                                 DISCOVERY_ERRLOG(ctx, "could not allocate new entry_ctx\n");
             #  #  #  # ]
    7394                 :          0 :                                 break;
    7395                 :            :                         }
    7396                 :            : 
    7397   [ #  #  #  # ]:         37 :                         new_ctx->ctx = ctx;
    7398   [ -  +  -  +  :         37 :                         memcpy(&new_ctx->entry, new_entry, sizeof(*new_entry));
                   #  # ]
    7399         [ #  # ]:         37 :                         build_trid_from_log_page_entry(&new_ctx->trid, new_entry);
    7400         [ +  + ]:         37 :                         if (subnqn_ctx) {
    7401   [ #  #  #  # ]:          5 :                                 snprintf(new_ctx->name, sizeof(new_ctx->name), "%s", subnqn_ctx->name);
    7402   [ -  +  +  -  :          5 :                                 DISCOVERY_INFOLOG(ctx, "NVM %s:%s:%s new path for %s\n",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    7403                 :            :                                                   new_ctx->trid.subnqn, new_ctx->trid.traddr, new_ctx->trid.trsvcid,
    7404                 :            :                                                   new_ctx->name);
    7405                 :          0 :                         } else {
    7406   [ #  #  #  #  :         32 :                                 snprintf(new_ctx->name, sizeof(new_ctx->name), "%s%d", ctx->name, ctx->index++);
             #  #  #  # ]
    7407   [ -  +  +  +  :         32 :                                 DISCOVERY_INFOLOG(ctx, "NVM %s:%s:%s new subsystem %s\n",
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    7408                 :            :                                                   new_ctx->trid.subnqn, new_ctx->trid.traddr, new_ctx->trid.trsvcid,
    7409                 :            :                                                   new_ctx->name);
    7410                 :            :                         }
    7411         [ #  # ]:         37 :                         spdk_nvme_ctrlr_get_default_ctrlr_opts(&new_ctx->drv_opts, sizeof(new_ctx->drv_opts));
    7412   [ #  #  #  #  :         37 :                         snprintf(new_ctx->drv_opts.hostnqn, sizeof(new_ctx->drv_opts.hostnqn), "%s", ctx->hostnqn);
             #  #  #  # ]
    7413   [ #  #  #  # ]:         37 :                         rc = spdk_bdev_nvme_create(&new_ctx->trid, new_ctx->name, NULL, 0,
    7414                 :          0 :                                                    discovery_attach_controller_done, new_ctx,
    7415   [ #  #  #  # ]:          0 :                                                    &new_ctx->drv_opts, &ctx->bdev_opts);
    7416         [ +  - ]:         37 :                         if (rc == 0) {
    7417   [ #  #  #  #  :         37 :                                 TAILQ_INSERT_TAIL(&ctx->nvm_entry_ctxs, new_ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7418         [ #  # ]:         37 :                                 ctx->attach_in_progress++;
    7419                 :          0 :                         } else {
    7420   [ #  #  #  #  :          0 :                                 DISCOVERY_ERRLOG(ctx, "spdk_bdev_nvme_create failed (%s)\n", spdk_strerror(-rc));
          #  #  #  #  #  
                      # ]
    7421                 :            :                         }
    7422                 :          0 :                 }
    7423                 :          0 :         }
    7424                 :            : 
    7425   [ +  +  #  #  :         49 :         if (ctx->attach_in_progress == 0) {
                   #  # ]
    7426                 :         12 :                 discovery_remove_controllers(ctx);
    7427                 :          0 :         }
    7428                 :          0 : }
    7429                 :            : 
    7430                 :            : static void
    7431                 :         49 : get_discovery_log_page(struct discovery_ctx *ctx)
    7432                 :            : {
    7433                 :            :         int rc;
    7434                 :            : 
    7435   [ -  +  -  +  :         49 :         assert(ctx->in_progress == false);
          #  #  #  #  #  
                      # ]
    7436   [ #  #  #  # ]:         49 :         ctx->in_progress = true;
    7437   [ #  #  #  # ]:         49 :         rc = spdk_nvme_ctrlr_get_discovery_log_page(ctx->ctrlr, discovery_log_page_cb, ctx);
    7438         [ -  + ]:         49 :         if (rc != 0) {
    7439   [ #  #  #  #  :          0 :                 DISCOVERY_ERRLOG(ctx, "could not get discovery log page\n");
             #  #  #  # ]
    7440                 :          0 :         }
    7441   [ -  +  +  +  :         49 :         DISCOVERY_INFOLOG(ctx, "sent discovery log page command\n");
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7442                 :         49 : }
    7443                 :            : 
    7444                 :            : static void
    7445                 :         16 : discovery_aer_cb(void *arg, const struct spdk_nvme_cpl *cpl)
    7446                 :            : {
    7447                 :         16 :         struct discovery_ctx *ctx = arg;
    7448   [ #  #  #  #  :         16 :         uint32_t log_page_id = (cpl->cdw0 & 0xFF0000) >> 16;
                   #  # ]
    7449                 :            : 
    7450   [ +  -  -  +  :         16 :         if (spdk_nvme_cpl_is_error(cpl)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7451   [ #  #  #  #  :          0 :                 DISCOVERY_ERRLOG(ctx, "aer failed\n");
             #  #  #  # ]
    7452                 :          0 :                 return;
    7453                 :            :         }
    7454                 :            : 
    7455         [ -  + ]:         16 :         if (log_page_id != SPDK_NVME_LOG_DISCOVERY) {
    7456   [ #  #  #  #  :          0 :                 DISCOVERY_ERRLOG(ctx, "unexpected log page 0x%x\n", log_page_id);
             #  #  #  # ]
    7457                 :          0 :                 return;
    7458                 :            :         }
    7459                 :            : 
    7460   [ -  +  +  -  :         16 :         DISCOVERY_INFOLOG(ctx, "got aer\n");
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7461   [ -  +  +  +  :         16 :         if (ctx->in_progress) {
             #  #  #  # ]
    7462   [ #  #  #  # ]:          5 :                 ctx->pending = true;
    7463                 :          5 :                 return;
    7464                 :            :         }
    7465                 :            : 
    7466                 :         11 :         get_discovery_log_page(ctx);
    7467                 :          0 : }
    7468                 :            : 
    7469                 :            : static void
    7470                 :         33 : discovery_attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
    7471                 :            :                     struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
    7472                 :            : {
    7473                 :         33 :         struct spdk_nvme_ctrlr_opts *user_opts = cb_ctx;
    7474                 :            :         struct discovery_ctx *ctx;
    7475                 :            : 
    7476                 :         33 :         ctx = SPDK_CONTAINEROF(user_opts, struct discovery_ctx, drv_opts);
    7477                 :            : 
    7478   [ -  +  +  +  :         33 :         DISCOVERY_INFOLOG(ctx, "discovery ctrlr attached\n");
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7479   [ #  #  #  # ]:         33 :         ctx->probe_ctx = NULL;
    7480   [ #  #  #  # ]:         33 :         ctx->ctrlr = ctrlr;
    7481                 :            : 
    7482   [ -  +  #  #  :         33 :         if (ctx->rc != 0) {
                   #  # ]
    7483   [ #  #  #  #  :          0 :                 DISCOVERY_ERRLOG(ctx, "encountered error while attaching discovery ctrlr: %d\n",
          #  #  #  #  #  
                #  #  # ]
    7484                 :            :                                  ctx->rc);
    7485                 :          0 :                 return;
    7486                 :            :         }
    7487                 :            : 
    7488   [ #  #  #  # ]:         33 :         spdk_nvme_ctrlr_register_aer_callback(ctx->ctrlr, discovery_aer_cb, ctx);
    7489                 :          0 : }
    7490                 :            : 
    7491                 :            : static int
    7492                 :     109725 : discovery_poller(void *arg)
    7493                 :            : {
    7494                 :     109725 :         struct discovery_ctx *ctx = arg;
    7495                 :            :         struct spdk_nvme_transport_id *trid;
    7496                 :            :         int rc;
    7497                 :            : 
    7498   [ +  +  #  #  :     109725 :         if (ctx->detach_ctx) {
                   #  # ]
    7499   [ #  #  #  # ]:        243 :                 rc = spdk_nvme_detach_poll_async(ctx->detach_ctx);
    7500         [ +  + ]:        243 :                 if (rc != -EAGAIN) {
    7501   [ #  #  #  # ]:         33 :                         ctx->detach_ctx = NULL;
    7502   [ #  #  #  # ]:         33 :                         ctx->ctrlr = NULL;
    7503                 :          0 :                 }
    7504   [ -  +  +  +  :     109482 :         } else if (ctx->stop) {
             #  #  #  # ]
    7505   [ +  +  #  #  :         60 :                 if (ctx->ctrlr != NULL) {
                   #  # ]
    7506   [ #  #  #  #  :         30 :                         rc = spdk_nvme_detach_async(ctx->ctrlr, &ctx->detach_ctx);
                   #  # ]
    7507         [ +  - ]:         30 :                         if (rc == 0) {
    7508                 :         30 :                                 return SPDK_POLLER_BUSY;
    7509                 :            :                         }
    7510   [ #  #  #  #  :          0 :                         DISCOVERY_ERRLOG(ctx, "could not detach discovery ctrlr\n");
             #  #  #  # ]
    7511                 :          0 :                 }
    7512         [ #  # ]:         30 :                 spdk_poller_unregister(&ctx->poller);
    7513   [ +  +  #  #  :         30 :                 TAILQ_REMOVE(&g_discovery_ctxs, ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    7514   [ -  +  #  #  :         30 :                 assert(ctx->start_cb_fn == NULL);
             #  #  #  # ]
    7515   [ +  +  #  #  :         30 :                 if (ctx->stop_cb_fn != NULL) {
                   #  # ]
    7516   [ #  #  #  #  :         25 :                         ctx->stop_cb_fn(ctx->cb_ctx);
          #  #  #  #  #  
                #  #  # ]
    7517                 :          0 :                 }
    7518                 :         30 :                 free_discovery_ctx(ctx);
    7519   [ +  +  +  +  :     109422 :         } else if (ctx->probe_ctx == NULL && ctx->ctrlr == NULL) {
          #  #  #  #  #  
                #  #  # ]
    7520   [ +  +  +  +  :         50 :                 if (ctx->timeout_ticks != 0 && ctx->timeout_ticks < spdk_get_ticks()) {
          #  #  #  #  #  
                #  #  # ]
    7521   [ #  #  #  #  :          4 :                         DISCOVERY_ERRLOG(ctx, "timed out while attaching discovery ctrlr\n");
             #  #  #  # ]
    7522   [ -  +  -  +  :          4 :                         assert(ctx->initializing);
          #  #  #  #  #  
                      # ]
    7523         [ #  # ]:          4 :                         spdk_poller_unregister(&ctx->poller);
    7524   [ -  +  #  #  :          4 :                         TAILQ_REMOVE(&g_discovery_ctxs, ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    7525                 :          4 :                         complete_discovery_start(ctx, -ETIMEDOUT);
    7526                 :          4 :                         stop_discovery(ctx, NULL, NULL);
    7527                 :          4 :                         free_discovery_ctx(ctx);
    7528                 :          4 :                         return SPDK_POLLER_BUSY;
    7529                 :            :                 }
    7530                 :            : 
    7531   [ -  +  #  #  :         46 :                 assert(ctx->entry_ctx_in_use == NULL);
             #  #  #  # ]
    7532   [ #  #  #  #  :         46 :                 ctx->entry_ctx_in_use = TAILQ_FIRST(&ctx->discovery_entry_ctxs);
          #  #  #  #  #  
                      # ]
    7533   [ +  +  #  #  :         46 :                 TAILQ_REMOVE(&ctx->discovery_entry_ctxs, ctx->entry_ctx_in_use, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7534   [ #  #  #  #  :         46 :                 trid = &ctx->entry_ctx_in_use->trid;
                   #  # ]
    7535                 :            : 
    7536                 :            :                 /* All controllers must be configured explicitely either for multipath or failover.
    7537                 :            :                  * While discovery use multipath mode, we need to set this in bdev options as well.
    7538                 :            :                  */
    7539   [ #  #  #  #  :         46 :                 ctx->bdev_opts.multipath = true;
                   #  # ]
    7540                 :            : 
    7541   [ #  #  #  #  :         46 :                 ctx->probe_ctx = spdk_nvme_connect_async(trid, &ctx->drv_opts, discovery_attach_cb);
                   #  # ]
    7542   [ +  +  #  #  :         46 :                 if (ctx->probe_ctx) {
                   #  # ]
    7543         [ #  # ]:         33 :                         spdk_poller_unregister(&ctx->poller);
    7544   [ #  #  #  # ]:         33 :                         ctx->poller = SPDK_POLLER_REGISTER(discovery_poller, ctx, 1000);
    7545                 :          0 :                 } else {
    7546   [ #  #  #  #  :         13 :                         DISCOVERY_ERRLOG(ctx, "could not start discovery connect\n");
             #  #  #  # ]
    7547   [ #  #  #  #  :         13 :                         TAILQ_INSERT_TAIL(&ctx->discovery_entry_ctxs, ctx->entry_ctx_in_use, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7548   [ #  #  #  # ]:         13 :                         ctx->entry_ctx_in_use = NULL;
    7549                 :            :                 }
    7550   [ +  +  #  #  :     109372 :         } else if (ctx->probe_ctx) {
                   #  # ]
    7551   [ +  +  -  +  :         33 :                 if (ctx->timeout_ticks != 0 && ctx->timeout_ticks < spdk_get_ticks()) {
          #  #  #  #  #  
                #  #  # ]
    7552   [ #  #  #  #  :          0 :                         DISCOVERY_ERRLOG(ctx, "timed out while attaching discovery ctrlr\n");
             #  #  #  # ]
    7553                 :          0 :                         complete_discovery_start(ctx, -ETIMEDOUT);
    7554                 :          0 :                         return SPDK_POLLER_BUSY;
    7555                 :            :                 }
    7556                 :            : 
    7557   [ #  #  #  # ]:         33 :                 rc = spdk_nvme_probe_poll_async(ctx->probe_ctx);
    7558         [ +  - ]:         33 :                 if (rc != -EAGAIN) {
    7559   [ -  +  #  #  :         33 :                         if (ctx->rc != 0) {
                   #  # ]
    7560   [ #  #  #  #  :          0 :                                 assert(ctx->initializing);
          #  #  #  #  #  
                      # ]
    7561   [ #  #  #  # ]:          0 :                                 stop_discovery(ctx, NULL, ctx->cb_ctx);
    7562                 :          0 :                         } else {
    7563   [ -  +  #  # ]:         33 :                                 assert(rc == 0);
    7564   [ -  +  +  +  :         33 :                                 DISCOVERY_INFOLOG(ctx, "discovery ctrlr connected\n");
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7565   [ #  #  #  # ]:         33 :                                 ctx->rc = rc;
    7566                 :         33 :                                 get_discovery_log_page(ctx);
    7567                 :            :                         }
    7568                 :          0 :                 }
    7569                 :          0 :         } else {
    7570   [ +  +  +  +  :     109339 :                 if (ctx->timeout_ticks != 0 && ctx->timeout_ticks < spdk_get_ticks()) {
          #  #  #  #  #  
                #  #  # ]
    7571   [ #  #  #  #  :          1 :                         DISCOVERY_ERRLOG(ctx, "timed out while attaching NVM ctrlrs\n");
             #  #  #  # ]
    7572                 :          1 :                         complete_discovery_start(ctx, -ETIMEDOUT);
    7573                 :            :                         /* We need to wait until all NVM ctrlrs are attached before we stop the
    7574                 :            :                          * discovery service to make sure we don't detach a ctrlr that is still
    7575                 :            :                          * being attached.
    7576                 :            :                          */
    7577   [ +  -  #  #  :          1 :                         if (ctx->attach_in_progress == 0) {
                   #  # ]
    7578   [ #  #  #  # ]:          1 :                                 stop_discovery(ctx, NULL, ctx->cb_ctx);
    7579                 :          1 :                                 return SPDK_POLLER_BUSY;
    7580                 :            :                         }
    7581                 :          0 :                 }
    7582                 :            : 
    7583   [ #  #  #  # ]:     109338 :                 rc = spdk_nvme_ctrlr_process_admin_completions(ctx->ctrlr);
    7584         [ +  + ]:     109338 :                 if (rc < 0) {
    7585         [ #  # ]:          3 :                         spdk_poller_unregister(&ctx->poller);
    7586   [ #  #  #  # ]:          3 :                         ctx->poller = SPDK_POLLER_REGISTER(discovery_poller, ctx, 1000 * 1000);
    7587   [ #  #  #  #  :          3 :                         TAILQ_INSERT_TAIL(&ctx->discovery_entry_ctxs, ctx->entry_ctx_in_use, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7588   [ #  #  #  # ]:          3 :                         ctx->entry_ctx_in_use = NULL;
    7589                 :            : 
    7590   [ #  #  #  #  :          3 :                         rc = spdk_nvme_detach_async(ctx->ctrlr, &ctx->detach_ctx);
                   #  # ]
    7591         [ -  + ]:          3 :                         if (rc != 0) {
    7592   [ #  #  #  #  :          0 :                                 DISCOVERY_ERRLOG(ctx, "could not detach discovery ctrlr\n");
             #  #  #  # ]
    7593   [ #  #  #  # ]:          0 :                                 ctx->ctrlr = NULL;
    7594                 :          0 :                         }
    7595                 :          0 :                 }
    7596                 :            :         }
    7597                 :            : 
    7598                 :     109690 :         return SPDK_POLLER_BUSY;
    7599                 :          0 : }
    7600                 :            : 
    7601                 :            : static void
    7602                 :         34 : start_discovery_poller(void *arg)
    7603                 :            : {
    7604                 :         34 :         struct discovery_ctx *ctx = arg;
    7605                 :            : 
    7606   [ #  #  #  #  :         34 :         TAILQ_INSERT_TAIL(&g_discovery_ctxs, ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7607   [ #  #  #  # ]:         34 :         ctx->poller = SPDK_POLLER_REGISTER(discovery_poller, ctx, 1000 * 1000);
    7608                 :         34 : }
    7609                 :            : 
    7610                 :            : int
    7611                 :         40 : bdev_nvme_start_discovery(struct spdk_nvme_transport_id *trid,
    7612                 :            :                           const char *base_name,
    7613                 :            :                           struct spdk_nvme_ctrlr_opts *drv_opts,
    7614                 :            :                           struct spdk_bdev_nvme_ctrlr_opts *bdev_opts,
    7615                 :            :                           uint64_t attach_timeout,
    7616                 :            :                           bool from_mdns,
    7617                 :            :                           spdk_bdev_nvme_start_discovery_fn cb_fn, void *cb_ctx)
    7618                 :            : {
    7619                 :            :         struct discovery_ctx *ctx;
    7620                 :            :         struct discovery_entry_ctx *discovery_entry_ctx;
    7621                 :            : 
    7622         [ #  # ]:         40 :         snprintf(trid->subnqn, sizeof(trid->subnqn), "%s", SPDK_NVMF_DISCOVERY_NQN);
    7623   [ +  +  #  #  :         50 :         TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
             #  #  #  # ]
    7624   [ -  +  -  +  :         16 :                 if (strcmp(ctx->name, base_name) == 0) {
          +  +  #  #  #  
                      # ]
    7625                 :          3 :                         return -EEXIST;
    7626                 :            :                 }
    7627                 :            : 
    7628   [ +  +  #  #  :         13 :                 if (ctx->entry_ctx_in_use != NULL) {
                   #  # ]
    7629   [ +  +  #  #  :         11 :                         if (!spdk_nvme_transport_id_compare(trid, &ctx->entry_ctx_in_use->trid)) {
             #  #  #  # ]
    7630                 :          3 :                                 return -EEXIST;
    7631                 :            :                         }
    7632                 :          0 :                 }
    7633                 :            : 
    7634   [ +  +  #  #  :         20 :                 TAILQ_FOREACH(discovery_entry_ctx, &ctx->discovery_entry_ctxs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7635   [ -  +  #  # ]:         10 :                         if (!spdk_nvme_transport_id_compare(trid, &discovery_entry_ctx->trid)) {
    7636                 :          0 :                                 return -EEXIST;
    7637                 :            :                         }
    7638                 :          0 :                 }
    7639                 :          0 :         }
    7640                 :            : 
    7641                 :         34 :         ctx = calloc(1, sizeof(*ctx));
    7642         [ -  + ]:         34 :         if (ctx == NULL) {
    7643                 :          0 :                 return -ENOMEM;
    7644                 :            :         }
    7645                 :            : 
    7646   [ -  +  #  #  :         34 :         ctx->name = strdup(base_name);
                   #  # ]
    7647   [ -  +  #  #  :         34 :         if (ctx->name == NULL) {
                   #  # ]
    7648                 :          0 :                 free_discovery_ctx(ctx);
    7649                 :          0 :                 return -ENOMEM;
    7650                 :            :         }
    7651   [ -  +  -  +  :         34 :         memcpy(&ctx->drv_opts, drv_opts, sizeof(*drv_opts));
                   #  # ]
    7652   [ -  +  -  +  :         34 :         memcpy(&ctx->bdev_opts, bdev_opts, sizeof(*bdev_opts));
                   #  # ]
    7653   [ #  #  #  #  :         34 :         ctx->from_mdns_discovery_service = from_mdns;
                   #  # ]
    7654   [ #  #  #  #  :         34 :         ctx->bdev_opts.from_discovery_service = true;
                   #  # ]
    7655   [ #  #  #  # ]:         34 :         ctx->calling_thread = spdk_get_thread();
    7656   [ #  #  #  # ]:         34 :         ctx->start_cb_fn = cb_fn;
    7657   [ #  #  #  # ]:         34 :         ctx->cb_ctx = cb_ctx;
    7658   [ #  #  #  # ]:         34 :         ctx->initializing = true;
    7659   [ +  +  #  #  :         34 :         if (ctx->start_cb_fn) {
                   #  # ]
    7660                 :            :                 /* We can use this when dumping json to denote if this RPC parameter
    7661                 :            :                  * was specified or not.
    7662                 :            :                  */
    7663   [ #  #  #  # ]:         27 :                 ctx->wait_for_attach = true;
    7664                 :          0 :         }
    7665         [ +  + ]:         34 :         if (attach_timeout != 0) {
    7666   [ #  #  #  # ]:         42 :                 ctx->timeout_ticks = spdk_get_ticks() + attach_timeout *
    7667   [ #  #  #  # ]:         21 :                                      spdk_get_ticks_hz() / 1000ull;
    7668                 :          0 :         }
    7669   [ #  #  #  #  :         34 :         TAILQ_INIT(&ctx->nvm_entry_ctxs);
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7670   [ #  #  #  #  :         34 :         TAILQ_INIT(&ctx->discovery_entry_ctxs);
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7671   [ -  +  -  +  :         34 :         memcpy(&ctx->trid, trid, sizeof(*trid));
                   #  # ]
    7672                 :            :         /* Even if user did not specify hostnqn, we can still strdup("\0"); */
    7673   [ -  +  #  #  :         34 :         ctx->hostnqn = strdup(ctx->drv_opts.hostnqn);
          #  #  #  #  #  
                      # ]
    7674   [ -  +  #  #  :         34 :         if (ctx->hostnqn == NULL) {
                   #  # ]
    7675                 :          0 :                 free_discovery_ctx(ctx);
    7676                 :          0 :                 return -ENOMEM;
    7677                 :            :         }
    7678                 :         34 :         discovery_entry_ctx = create_discovery_entry_ctx(ctx, trid);
    7679         [ -  + ]:         34 :         if (discovery_entry_ctx == NULL) {
    7680   [ #  #  #  #  :          0 :                 DISCOVERY_ERRLOG(ctx, "could not allocate new entry_ctx\n");
             #  #  #  # ]
    7681                 :          0 :                 free_discovery_ctx(ctx);
    7682                 :          0 :                 return -ENOMEM;
    7683                 :            :         }
    7684                 :            : 
    7685   [ #  #  #  #  :         34 :         TAILQ_INSERT_TAIL(&ctx->discovery_entry_ctxs, discovery_entry_ctx, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7686                 :         34 :         spdk_thread_send_msg(g_bdev_nvme_init_thread, start_discovery_poller, ctx);
    7687                 :         34 :         return 0;
    7688                 :          0 : }
    7689                 :            : 
    7690                 :            : int
    7691                 :         23 : bdev_nvme_stop_discovery(const char *name, spdk_bdev_nvme_stop_discovery_fn cb_fn, void *cb_ctx)
    7692                 :            : {
    7693                 :            :         struct discovery_ctx *ctx;
    7694                 :            : 
    7695   [ +  -  #  #  :         26 :         TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
             #  #  #  # ]
    7696   [ -  +  -  +  :         26 :                 if (strcmp(name, ctx->name) == 0) {
          +  +  #  #  #  
                      # ]
    7697   [ -  +  -  +  :         23 :                         if (ctx->stop) {
             #  #  #  # ]
    7698                 :          0 :                                 return -EALREADY;
    7699                 :            :                         }
    7700                 :            :                         /* If we're still starting the discovery service and ->rc is non-zero, we're
    7701                 :            :                          * going to stop it as soon as we can
    7702                 :            :                          */
    7703   [ -  +  -  +  :         23 :                         if (ctx->initializing && ctx->rc != 0) {
          -  -  #  #  #  
             #  #  #  #  
                      # ]
    7704                 :          0 :                                 return -EALREADY;
    7705                 :            :                         }
    7706                 :         23 :                         stop_discovery(ctx, cb_fn, cb_ctx);
    7707                 :         23 :                         return 0;
    7708                 :            :                 }
    7709                 :          0 :         }
    7710                 :            : 
    7711                 :          0 :         return -ENOENT;
    7712                 :          0 : }
    7713                 :            : 
    7714                 :            : static int
    7715                 :       1953 : bdev_nvme_library_init(void)
    7716                 :            : {
    7717                 :       1953 :         g_bdev_nvme_init_thread = spdk_get_thread();
    7718                 :            : 
    7719                 :       1953 :         spdk_io_device_register(&g_nvme_bdev_ctrlrs, bdev_nvme_create_poll_group_cb,
    7720                 :            :                                 bdev_nvme_destroy_poll_group_cb,
    7721                 :            :                                 sizeof(struct nvme_poll_group),  "nvme_poll_groups");
    7722                 :            : 
    7723                 :       1953 :         return 0;
    7724                 :            : }
    7725                 :            : 
    7726                 :            : static void
    7727                 :       1953 : bdev_nvme_fini_destruct_ctrlrs(void)
    7728                 :            : {
    7729                 :            :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
    7730                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    7731                 :            : 
    7732         [ +  + ]:       1953 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    7733   [ +  +  +  -  :       2688 :         TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
             +  -  +  - ]
    7734   [ +  +  +  -  :       1476 :                 TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
          +  -  +  +  +  
             -  +  -  +  
                      - ]
    7735   [ +  +  +  - ]:        741 :                         pthread_mutex_lock(&nvme_ctrlr->mutex);
    7736   [ +  +  -  + ]:        741 :                         if (nvme_ctrlr->destruct) {
    7737                 :            :                                 /* This controller's destruction was already started
    7738                 :            :                                  * before the application started shutting down
    7739                 :            :                                  */
    7740   [ #  #  #  # ]:          0 :                                 pthread_mutex_unlock(&nvme_ctrlr->mutex);
    7741                 :          0 :                                 continue;
    7742                 :            :                         }
    7743         [ -  + ]:        741 :                         nvme_ctrlr->destruct = true;
    7744   [ -  +  -  + ]:        741 :                         pthread_mutex_unlock(&nvme_ctrlr->mutex);
    7745                 :            : 
    7746   [ -  +  -  + ]:        741 :                         spdk_thread_send_msg(nvme_ctrlr->thread, _nvme_ctrlr_destruct,
    7747                 :          1 :                                              nvme_ctrlr);
    7748                 :          1 :                 }
    7749                 :          1 :         }
    7750                 :            : 
    7751                 :       1953 :         g_bdev_nvme_module_finish = true;
    7752         [ +  + ]:       1953 :         if (TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) {
    7753         [ -  + ]:       1406 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
    7754                 :       1406 :                 spdk_io_device_unregister(&g_nvme_bdev_ctrlrs, NULL);
    7755                 :       1406 :                 spdk_bdev_module_fini_done();
    7756                 :       1406 :                 return;
    7757                 :            :         }
    7758                 :            : 
    7759         [ +  + ]:        547 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    7760                 :         54 : }
    7761                 :            : 
    7762                 :            : static void
    7763                 :          6 : check_discovery_fini(void *arg)
    7764                 :            : {
    7765         [ +  - ]:          6 :         if (TAILQ_EMPTY(&g_discovery_ctxs)) {
    7766                 :          6 :                 bdev_nvme_fini_destruct_ctrlrs();
    7767                 :          0 :         }
    7768                 :          6 : }
    7769                 :            : 
    7770                 :            : static void
    7771                 :       1953 : bdev_nvme_library_fini(void)
    7772                 :            : {
    7773                 :            :         struct nvme_probe_skip_entry *entry, *entry_tmp;
    7774                 :            :         struct discovery_ctx *ctx;
    7775                 :            : 
    7776                 :       1953 :         spdk_poller_unregister(&g_hotplug_poller);
    7777                 :       1953 :         free(g_hotplug_probe_ctx);
    7778                 :       1953 :         g_hotplug_probe_ctx = NULL;
    7779                 :            : 
    7780   [ +  +  #  #  :       2007 :         TAILQ_FOREACH_SAFE(entry, &g_skipped_nvme_ctrlrs, tailq, entry_tmp) {
          #  #  #  #  -  
                      + ]
    7781   [ +  +  #  #  :         54 :                 TAILQ_REMOVE(&g_skipped_nvme_ctrlrs, entry, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    7782                 :         54 :                 free(entry);
    7783                 :          0 :         }
    7784                 :            : 
    7785   [ +  +  #  # ]:       1953 :         assert(spdk_get_thread() == g_bdev_nvme_init_thread);
    7786         [ +  + ]:       1953 :         if (TAILQ_EMPTY(&g_discovery_ctxs)) {
    7787                 :       1947 :                 bdev_nvme_fini_destruct_ctrlrs();
    7788                 :         54 :         } else {
    7789   [ +  +  #  #  :         12 :                 TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
             #  #  #  # ]
    7790                 :          6 :                         stop_discovery(ctx, check_discovery_fini, NULL);
    7791                 :          0 :                 }
    7792                 :            :         }
    7793                 :       1953 : }
    7794                 :            : 
    7795                 :            : static void
    7796                 :          0 : bdev_nvme_verify_pi_error(struct nvme_bdev_io *bio)
    7797                 :            : {
    7798                 :          0 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    7799   [ #  #  #  # ]:          0 :         struct spdk_bdev *bdev = bdev_io->bdev;
    7800                 :          0 :         struct spdk_dif_ctx dif_ctx;
    7801                 :          0 :         struct spdk_dif_error err_blk = {};
    7802                 :            :         int rc;
    7803                 :          0 :         struct spdk_dif_ctx_init_ext_opts dif_opts;
    7804                 :            : 
    7805                 :          0 :         dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
    7806   [ #  #  #  #  :          0 :         dif_opts.dif_pi_format = bdev->dif_pi_format;
                   #  # ]
    7807                 :          0 :         rc = spdk_dif_ctx_init(&dif_ctx,
    7808   [ #  #  #  #  :          0 :                                bdev->blocklen, bdev->md_len, bdev->md_interleave,
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    7809   [ #  #  #  #  :          0 :                                bdev->dif_is_head_of_md, bdev->dif_type,
          #  #  #  #  #  
                      # ]
    7810   [ #  #  #  #  :          0 :                                bdev_io->u.bdev.dif_check_flags,
             #  #  #  # ]
    7811   [ #  #  #  #  :          0 :                                bdev_io->u.bdev.offset_blocks, 0, 0, 0, 0, &dif_opts);
             #  #  #  # ]
    7812         [ #  # ]:          0 :         if (rc != 0) {
    7813                 :          0 :                 SPDK_ERRLOG("Initialization of DIF context failed\n");
    7814                 :          0 :                 return;
    7815                 :            :         }
    7816                 :            : 
    7817   [ #  #  #  #  :          0 :         if (bdev->md_interleave) {
             #  #  #  # ]
    7818   [ #  #  #  #  :          0 :                 rc = spdk_dif_verify(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7819   [ #  #  #  #  :          0 :                                      bdev_io->u.bdev.num_blocks, &dif_ctx, &err_blk);
             #  #  #  # ]
    7820                 :          0 :         } else {
    7821                 :          0 :                 struct iovec md_iov = {
    7822   [ #  #  #  #  :          0 :                         .iov_base       = bdev_io->u.bdev.md_buf,
             #  #  #  # ]
    7823   [ #  #  #  #  :          0 :                         .iov_len        = bdev_io->u.bdev.num_blocks * bdev->md_len,
          #  #  #  #  #  
                #  #  # ]
    7824                 :            :                 };
    7825                 :            : 
    7826   [ #  #  #  #  :          0 :                 rc = spdk_dix_verify(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7827   [ #  #  #  #  :          0 :                                      &md_iov, bdev_io->u.bdev.num_blocks, &dif_ctx, &err_blk);
             #  #  #  # ]
    7828                 :            :         }
    7829                 :            : 
    7830         [ #  # ]:          0 :         if (rc != 0) {
    7831         [ #  # ]:          0 :                 SPDK_ERRLOG("DIF error detected. type=%d, offset=%" PRIu32 "\n",
    7832                 :            :                             err_blk.err_type, err_blk.err_offset);
    7833                 :          0 :         } else {
    7834                 :          0 :                 SPDK_ERRLOG("Hardware reported PI error but SPDK could not find any.\n");
    7835                 :            :         }
    7836                 :          0 : }
    7837                 :            : 
    7838                 :            : static void
    7839                 :          0 : bdev_nvme_no_pi_readv_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7840                 :            : {
    7841                 :          0 :         struct nvme_bdev_io *bio = ref;
    7842                 :            : 
    7843   [ #  #  #  #  :          0 :         if (spdk_nvme_cpl_is_success(cpl)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7844                 :            :                 /* Run PI verification for read data buffer. */
    7845                 :          0 :                 bdev_nvme_verify_pi_error(bio);
    7846                 :          0 :         }
    7847                 :            : 
    7848                 :            :         /* Return original completion status */
    7849         [ #  # ]:          0 :         bdev_nvme_io_complete_nvme_status(bio, &bio->cpl);
    7850                 :          0 : }
    7851                 :            : 
    7852                 :            : static void
    7853                 :   21296569 : bdev_nvme_readv_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7854                 :            : {
    7855                 :   21296569 :         struct nvme_bdev_io *bio = ref;
    7856                 :   21296569 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    7857                 :            :         int ret;
    7858                 :            : 
    7859   [ +  +  +  -  :   21296569 :         if (spdk_unlikely(spdk_nvme_cpl_is_pi_error(cpl))) {
          +  -  +  -  -  
          -  -  -  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  +  - ]
    7860   [ #  #  #  #  :          0 :                 SPDK_ERRLOG("readv completed with PI error (sct=%d, sc=%d)\n",
          #  #  #  #  #  
                #  #  # ]
    7861                 :            :                             cpl->status.sct, cpl->status.sc);
    7862                 :            : 
    7863                 :            :                 /* Save completion status to use after verifying PI error. */
    7864         [ #  # ]:          0 :                 bio->cpl = *cpl;
    7865                 :            : 
    7866   [ #  #  #  #  :          0 :                 if (spdk_likely(nvme_io_path_is_available(bio->io_path))) {
                   #  # ]
    7867                 :            :                         /* Read without PI checking to verify PI error. */
    7868                 :          0 :                         ret = bdev_nvme_no_pi_readv(bio,
    7869   [ #  #  #  #  :          0 :                                                     bdev_io->u.bdev.iovs,
             #  #  #  # ]
    7870   [ #  #  #  #  :          0 :                                                     bdev_io->u.bdev.iovcnt,
             #  #  #  # ]
    7871   [ #  #  #  #  :          0 :                                                     bdev_io->u.bdev.md_buf,
             #  #  #  # ]
    7872   [ #  #  #  #  :          0 :                                                     bdev_io->u.bdev.num_blocks,
             #  #  #  # ]
    7873   [ #  #  #  #  :          0 :                                                     bdev_io->u.bdev.offset_blocks);
             #  #  #  # ]
    7874         [ #  # ]:          0 :                         if (ret == 0) {
    7875                 :          0 :                                 return;
    7876                 :            :                         }
    7877                 :          0 :                 }
    7878                 :          0 :         }
    7879                 :            : 
    7880                 :   21296569 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7881                 :          3 : }
    7882                 :            : 
    7883                 :            : static void
    7884                 :   20181337 : bdev_nvme_writev_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7885                 :            : {
    7886                 :   20181337 :         struct nvme_bdev_io *bio = ref;
    7887                 :            : 
    7888   [ -  +  -  -  :   20181337 :         if (spdk_unlikely(spdk_nvme_cpl_is_pi_error(cpl))) {
          -  -  -  -  -  
          -  -  -  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7889   [ #  #  #  #  :          0 :                 SPDK_ERRLOG("writev completed with PI error (sct=%d, sc=%d)\n",
          #  #  #  #  #  
                #  #  # ]
    7890                 :            :                             cpl->status.sct, cpl->status.sc);
    7891                 :            :                 /* Run PI verification for write data buffer if PI error is detected. */
    7892                 :          0 :                 bdev_nvme_verify_pi_error(bio);
    7893                 :          0 :         }
    7894                 :            : 
    7895                 :   20181337 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7896                 :   20181337 : }
    7897                 :            : 
    7898                 :            : static void
    7899                 :     268251 : bdev_nvme_zone_appendv_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7900                 :            : {
    7901                 :     268251 :         struct nvme_bdev_io *bio = ref;
    7902                 :     268251 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    7903                 :            : 
    7904                 :            :         /* spdk_bdev_io_get_append_location() requires that the ALBA is stored in offset_blocks.
    7905                 :            :          * Additionally, offset_blocks has to be set before calling bdev_nvme_verify_pi_error().
    7906                 :            :          */
    7907   [ #  #  #  #  :     268251 :         bdev_io->u.bdev.offset_blocks = *(uint64_t *)&cpl->cdw0;
          #  #  #  #  #  
                #  #  # ]
    7908                 :            : 
    7909   [ -  +  -  -  :     268251 :         if (spdk_nvme_cpl_is_pi_error(cpl)) {
          -  -  -  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7910   [ #  #  #  #  :          0 :                 SPDK_ERRLOG("zone append completed with PI error (sct=%d, sc=%d)\n",
          #  #  #  #  #  
                #  #  # ]
    7911                 :            :                             cpl->status.sct, cpl->status.sc);
    7912                 :            :                 /* Run PI verification for zone append data buffer if PI error is detected. */
    7913                 :          0 :                 bdev_nvme_verify_pi_error(bio);
    7914                 :          0 :         }
    7915                 :            : 
    7916                 :     268251 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7917                 :     268251 : }
    7918                 :            : 
    7919                 :            : static void
    7920                 :         53 : bdev_nvme_comparev_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7921                 :            : {
    7922                 :         53 :         struct nvme_bdev_io *bio = ref;
    7923                 :            : 
    7924   [ +  +  +  -  :         53 :         if (spdk_nvme_cpl_is_pi_error(cpl)) {
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
    7925   [ #  #  #  #  :          0 :                 SPDK_ERRLOG("comparev completed with PI error (sct=%d, sc=%d)\n",
          #  #  #  #  #  
                #  #  # ]
    7926                 :            :                             cpl->status.sct, cpl->status.sc);
    7927                 :            :                 /* Run PI verification for compare data buffer if PI error is detected. */
    7928                 :          0 :                 bdev_nvme_verify_pi_error(bio);
    7929                 :          0 :         }
    7930                 :            : 
    7931                 :         53 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7932                 :         53 : }
    7933                 :            : 
    7934                 :            : static void
    7935                 :        102 : bdev_nvme_comparev_and_writev_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7936                 :            : {
    7937                 :        102 :         struct nvme_bdev_io *bio = ref;
    7938                 :            : 
    7939                 :            :         /* Compare operation completion */
    7940   [ +  +  +  +  :        102 :         if (!bio->first_fused_completed) {
             #  #  #  # ]
    7941                 :            :                 /* Save compare result for write callback */
    7942         [ #  # ]:         51 :                 bio->cpl = *cpl;
    7943   [ #  #  #  # ]:         51 :                 bio->first_fused_completed = true;
    7944                 :         51 :                 return;
    7945                 :            :         }
    7946                 :            : 
    7947                 :            :         /* Write operation completion */
    7948   [ +  +  -  +  :         51 :         if (spdk_nvme_cpl_is_error(&bio->cpl)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    7949                 :            :                 /* If bio->cpl is already an error, it means the compare operation failed.  In that case,
    7950                 :            :                  * complete the IO with the compare operation's status.
    7951                 :            :                  */
    7952   [ +  +  +  -  :         39 :                 if (!spdk_nvme_cpl_is_error(cpl)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7953                 :          3 :                         SPDK_ERRLOG("Unexpected write success after compare failure.\n");
    7954                 :          0 :                 }
    7955                 :            : 
    7956         [ #  # ]:         39 :                 bdev_nvme_io_complete_nvme_status(bio, &bio->cpl);
    7957                 :          0 :         } else {
    7958                 :         12 :                 bdev_nvme_io_complete_nvme_status(bio, cpl);
    7959                 :            :         }
    7960                 :          0 : }
    7961                 :            : 
    7962                 :            : static void
    7963                 :     946246 : bdev_nvme_queued_done(void *ref, const struct spdk_nvme_cpl *cpl)
    7964                 :            : {
    7965                 :     946246 :         struct nvme_bdev_io *bio = ref;
    7966                 :            : 
    7967                 :     946246 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    7968                 :     946246 : }
    7969                 :            : 
    7970                 :            : static int
    7971                 :         40 : fill_zone_from_report(struct spdk_bdev_zone_info *info, struct spdk_nvme_zns_zone_desc *desc)
    7972                 :            : {
    7973   [ +  -  #  # ]:         40 :         switch (desc->zt) {
    7974                 :         40 :         case SPDK_NVME_ZONE_TYPE_SEQWR:
    7975   [ #  #  #  # ]:         40 :                 info->type = SPDK_BDEV_ZONE_TYPE_SEQWR;
    7976                 :         40 :                 break;
    7977                 :          0 :         default:
    7978         [ #  # ]:          0 :                 SPDK_ERRLOG("Invalid zone type: %#x in zone report\n", desc->zt);
    7979                 :          0 :                 return -EIO;
    7980                 :            :         }
    7981                 :            : 
    7982   [ +  -  -  -  :         40 :         switch (desc->zs) {
          -  -  -  -  #  
                      # ]
    7983                 :         40 :         case SPDK_NVME_ZONE_STATE_EMPTY:
    7984   [ #  #  #  # ]:         40 :                 info->state = SPDK_BDEV_ZONE_STATE_EMPTY;
    7985                 :         40 :                 break;
    7986                 :          0 :         case SPDK_NVME_ZONE_STATE_IOPEN:
    7987   [ #  #  #  # ]:          0 :                 info->state = SPDK_BDEV_ZONE_STATE_IMP_OPEN;
    7988                 :          0 :                 break;
    7989                 :          0 :         case SPDK_NVME_ZONE_STATE_EOPEN:
    7990   [ #  #  #  # ]:          0 :                 info->state = SPDK_BDEV_ZONE_STATE_EXP_OPEN;
    7991                 :          0 :                 break;
    7992                 :          0 :         case SPDK_NVME_ZONE_STATE_CLOSED:
    7993   [ #  #  #  # ]:          0 :                 info->state = SPDK_BDEV_ZONE_STATE_CLOSED;
    7994                 :          0 :                 break;
    7995                 :          0 :         case SPDK_NVME_ZONE_STATE_RONLY:
    7996   [ #  #  #  # ]:          0 :                 info->state = SPDK_BDEV_ZONE_STATE_READ_ONLY;
    7997                 :          0 :                 break;
    7998                 :          0 :         case SPDK_NVME_ZONE_STATE_FULL:
    7999   [ #  #  #  # ]:          0 :                 info->state = SPDK_BDEV_ZONE_STATE_FULL;
    8000                 :          0 :                 break;
    8001                 :          0 :         case SPDK_NVME_ZONE_STATE_OFFLINE:
    8002   [ #  #  #  # ]:          0 :                 info->state = SPDK_BDEV_ZONE_STATE_OFFLINE;
    8003                 :          0 :                 break;
    8004                 :          0 :         default:
    8005         [ #  # ]:          0 :                 SPDK_ERRLOG("Invalid zone state: %#x in zone report\n", desc->zs);
    8006                 :          0 :                 return -EIO;
    8007                 :            :         }
    8008                 :            : 
    8009   [ #  #  #  #  :         40 :         info->zone_id = desc->zslba;
             #  #  #  # ]
    8010   [ #  #  #  #  :         40 :         info->write_pointer = desc->wp;
             #  #  #  # ]
    8011   [ #  #  #  #  :         40 :         info->capacity = desc->zcap;
             #  #  #  # ]
    8012                 :            : 
    8013                 :         40 :         return 0;
    8014                 :          0 : }
    8015                 :            : 
    8016                 :            : static void
    8017                 :          1 : bdev_nvme_get_zone_info_done(void *ref, const struct spdk_nvme_cpl *cpl)
    8018                 :            : {
    8019                 :          1 :         struct nvme_bdev_io *bio = ref;
    8020                 :          1 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    8021   [ #  #  #  #  :          1 :         uint64_t zone_id = bdev_io->u.zone_mgmt.zone_id;
             #  #  #  # ]
    8022   [ #  #  #  #  :          1 :         uint32_t zones_to_copy = bdev_io->u.zone_mgmt.num_zones;
             #  #  #  # ]
    8023   [ #  #  #  #  :          1 :         struct spdk_bdev_zone_info *info = bdev_io->u.zone_mgmt.buf;
             #  #  #  # ]
    8024                 :            :         uint64_t max_zones_per_buf, i;
    8025                 :            :         uint32_t zone_report_bufsize;
    8026                 :            :         struct spdk_nvme_ns *ns;
    8027                 :            :         struct spdk_nvme_qpair *qpair;
    8028                 :            :         int ret;
    8029                 :            : 
    8030   [ +  -  -  +  :          1 :         if (spdk_nvme_cpl_is_error(cpl)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    8031                 :          0 :                 goto out_complete_io_nvme_cpl;
    8032                 :            :         }
    8033                 :            : 
    8034   [ -  +  #  #  :          1 :         if (spdk_unlikely(!nvme_io_path_is_available(bio->io_path))) {
                   #  # ]
    8035                 :          0 :                 ret = -ENXIO;
    8036                 :          0 :                 goto out_complete_io_ret;
    8037                 :            :         }
    8038                 :            : 
    8039   [ #  #  #  #  :          1 :         ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8040   [ #  #  #  #  :          1 :         qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8041                 :            : 
    8042                 :          1 :         zone_report_bufsize = spdk_nvme_ns_get_max_io_xfer_size(ns);
    8043         [ #  # ]:          1 :         max_zones_per_buf = (zone_report_bufsize - sizeof(*bio->zone_report_buf)) /
    8044                 :            :                             sizeof(bio->zone_report_buf->descs[0]);
    8045                 :            : 
    8046   [ -  +  #  #  :          1 :         if (bio->zone_report_buf->nr_zones > max_zones_per_buf) {
          #  #  #  #  #  
                      # ]
    8047                 :          0 :                 ret = -EINVAL;
    8048                 :          0 :                 goto out_complete_io_ret;
    8049                 :            :         }
    8050                 :            : 
    8051   [ -  +  #  #  :          1 :         if (!bio->zone_report_buf->nr_zones) {
          #  #  #  #  #  
                      # ]
    8052                 :          0 :                 ret = -EINVAL;
    8053                 :          0 :                 goto out_complete_io_ret;
    8054                 :            :         }
    8055                 :            : 
    8056   [ +  +  +  -  :         41 :         for (i = 0; i < bio->zone_report_buf->nr_zones && bio->handled_zones < zones_to_copy; i++) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    8057   [ #  #  #  #  :         40 :                 ret = fill_zone_from_report(&info[bio->handled_zones],
                   #  # ]
    8058   [ #  #  #  #  :         40 :                                             &bio->zone_report_buf->descs[i]);
             #  #  #  # ]
    8059         [ -  + ]:         40 :                 if (ret) {
    8060                 :          0 :                         goto out_complete_io_ret;
    8061                 :            :                 }
    8062         [ #  # ]:         40 :                 bio->handled_zones++;
    8063                 :          0 :         }
    8064                 :            : 
    8065   [ -  +  #  #  :          1 :         if (bio->handled_zones < zones_to_copy) {
                   #  # ]
    8066                 :          0 :                 uint64_t zone_size_lba = spdk_nvme_zns_ns_get_zone_size_sectors(ns);
    8067   [ #  #  #  # ]:          0 :                 uint64_t slba = zone_id + (zone_size_lba * bio->handled_zones);
    8068                 :            : 
    8069   [ #  #  #  #  :          0 :                 memset(bio->zone_report_buf, 0, zone_report_bufsize);
                   #  # ]
    8070                 :          0 :                 ret = spdk_nvme_zns_report_zones(ns, qpair,
    8071   [ #  #  #  # ]:          0 :                                                  bio->zone_report_buf, zone_report_bufsize,
    8072                 :          0 :                                                  slba, SPDK_NVME_ZRA_LIST_ALL, true,
    8073                 :          0 :                                                  bdev_nvme_get_zone_info_done, bio);
    8074         [ #  # ]:          0 :                 if (!ret) {
    8075                 :          0 :                         return;
    8076                 :            :                 } else {
    8077                 :          0 :                         goto out_complete_io_ret;
    8078                 :            :                 }
    8079                 :            :         }
    8080                 :            : 
    8081                 :          1 : out_complete_io_nvme_cpl:
    8082   [ #  #  #  # ]:          1 :         free(bio->zone_report_buf);
    8083   [ #  #  #  # ]:          1 :         bio->zone_report_buf = NULL;
    8084                 :          1 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    8085                 :          1 :         return;
    8086                 :            : 
    8087                 :          0 : out_complete_io_ret:
    8088   [ #  #  #  # ]:          0 :         free(bio->zone_report_buf);
    8089   [ #  #  #  # ]:          0 :         bio->zone_report_buf = NULL;
    8090                 :          0 :         bdev_nvme_io_complete(bio, ret);
    8091                 :          0 : }
    8092                 :            : 
    8093                 :            : static void
    8094                 :         46 : bdev_nvme_zone_management_done(void *ref, const struct spdk_nvme_cpl *cpl)
    8095                 :            : {
    8096                 :         46 :         struct nvme_bdev_io *bio = ref;
    8097                 :            : 
    8098                 :         46 :         bdev_nvme_io_complete_nvme_status(bio, cpl);
    8099                 :         46 : }
    8100                 :            : 
    8101                 :            : static void
    8102                 :        135 : bdev_nvme_admin_passthru_complete_nvme_status(void *ctx)
    8103                 :            : {
    8104                 :        135 :         struct nvme_bdev_io *bio = ctx;
    8105                 :        135 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    8106         [ #  # ]:        135 :         const struct spdk_nvme_cpl *cpl = &bio->cpl;
    8107                 :            : 
    8108   [ -  +  #  #  :        135 :         assert(bdev_nvme_io_type_is_admin(bdev_io->type));
             #  #  #  # ]
    8109                 :            : 
    8110                 :        135 :         __bdev_nvme_io_complete(bdev_io, 0, cpl);
    8111                 :        135 : }
    8112                 :            : 
    8113                 :            : static void
    8114                 :       7284 : bdev_nvme_abort_complete(void *ctx)
    8115                 :            : {
    8116                 :       7284 :         struct nvme_bdev_io *bio = ctx;
    8117                 :       7284 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    8118                 :            : 
    8119   [ +  -  +  -  :       7284 :         if (spdk_nvme_cpl_is_abort_success(&bio->cpl)) {
          +  +  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    8120                 :          9 :                 __bdev_nvme_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS, NULL);
    8121                 :          0 :         } else {
    8122                 :       7275 :                 __bdev_nvme_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED, NULL);
    8123                 :            :         }
    8124                 :       7284 : }
    8125                 :            : 
    8126                 :            : static void
    8127                 :       7284 : bdev_nvme_abort_done(void *ref, const struct spdk_nvme_cpl *cpl)
    8128                 :            : {
    8129                 :       7284 :         struct nvme_bdev_io *bio = ref;
    8130                 :       7284 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    8131                 :            : 
    8132         [ #  # ]:       7284 :         bio->cpl = *cpl;
    8133                 :       7284 :         spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io), bdev_nvme_abort_complete, bio);
    8134                 :       7284 : }
    8135                 :            : 
    8136                 :            : static void
    8137                 :        135 : bdev_nvme_admin_passthru_done(void *ref, const struct spdk_nvme_cpl *cpl)
    8138                 :            : {
    8139                 :        135 :         struct nvme_bdev_io *bio = ref;
    8140                 :        135 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    8141                 :            : 
    8142         [ #  # ]:        135 :         bio->cpl = *cpl;
    8143                 :        135 :         spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io),
    8144                 :          0 :                              bdev_nvme_admin_passthru_complete_nvme_status, bio);
    8145                 :        135 : }
    8146                 :            : 
    8147                 :            : static void
    8148                 :    7991375 : bdev_nvme_queued_reset_sgl(void *ref, uint32_t sgl_offset)
    8149                 :            : {
    8150                 :    7991375 :         struct nvme_bdev_io *bio = ref;
    8151                 :            :         struct iovec *iov;
    8152                 :            : 
    8153   [ #  #  #  # ]:    7991375 :         bio->iov_offset = sgl_offset;
    8154   [ +  -  #  #  :   15899497 :         for (bio->iovpos = 0; bio->iovpos < bio->iovcnt; bio->iovpos++) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    8155   [ #  #  #  #  :   15899497 :                 iov = &bio->iovs[bio->iovpos];
          #  #  #  #  #  
                      # ]
    8156   [ +  +  #  #  :   15899497 :                 if (bio->iov_offset < iov->iov_len) {
          #  #  #  #  #  
                      # ]
    8157                 :    7991375 :                         break;
    8158                 :            :                 }
    8159                 :            : 
    8160   [ #  #  #  #  :    7908122 :                 bio->iov_offset -= iov->iov_len;
             #  #  #  # ]
    8161                 :          0 :         }
    8162                 :    7991375 : }
    8163                 :            : 
    8164                 :            : static int
    8165                 :   22668380 : bdev_nvme_queued_next_sge(void *ref, void **address, uint32_t *length)
    8166                 :            : {
    8167                 :   22668380 :         struct nvme_bdev_io *bio = ref;
    8168                 :            :         struct iovec *iov;
    8169                 :            : 
    8170   [ -  +  #  #  :   22668380 :         assert(bio->iovpos < bio->iovcnt);
          #  #  #  #  #  
                #  #  # ]
    8171                 :            : 
    8172   [ #  #  #  #  :   22668380 :         iov = &bio->iovs[bio->iovpos];
          #  #  #  #  #  
                      # ]
    8173                 :            : 
    8174   [ #  #  #  #  :   22668380 :         *address = iov->iov_base;
                   #  # ]
    8175   [ #  #  #  #  :   22668380 :         *length = iov->iov_len;
                   #  # ]
    8176                 :            : 
    8177   [ +  +  #  #  :   22668380 :         if (bio->iov_offset) {
                   #  # ]
    8178   [ -  +  #  #  :     113078 :                 assert(bio->iov_offset <= iov->iov_len);
          #  #  #  #  #  
                #  #  # ]
    8179   [ #  #  #  #  :     113078 :                 *address += bio->iov_offset;
                   #  # ]
    8180   [ #  #  #  #  :     113078 :                 *length -= bio->iov_offset;
                   #  # ]
    8181                 :          0 :         }
    8182                 :            : 
    8183   [ #  #  #  #  :   22668380 :         bio->iov_offset += *length;
                   #  # ]
    8184   [ +  -  #  #  :   22668380 :         if (bio->iov_offset == iov->iov_len) {
          #  #  #  #  #  
                      # ]
    8185   [ #  #  #  # ]:   22668380 :                 bio->iovpos++;
    8186   [ #  #  #  # ]:   22668380 :                 bio->iov_offset = 0;
    8187                 :          0 :         }
    8188                 :            : 
    8189                 :   22668380 :         return 0;
    8190                 :            : }
    8191                 :            : 
    8192                 :            : static void
    8193                 :         90 : bdev_nvme_queued_reset_fused_sgl(void *ref, uint32_t sgl_offset)
    8194                 :            : {
    8195                 :         90 :         struct nvme_bdev_io *bio = ref;
    8196                 :            :         struct iovec *iov;
    8197                 :            : 
    8198   [ #  #  #  # ]:         90 :         bio->fused_iov_offset = sgl_offset;
    8199   [ +  -  #  #  :         90 :         for (bio->fused_iovpos = 0; bio->fused_iovpos < bio->fused_iovcnt; bio->fused_iovpos++) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    8200   [ #  #  #  #  :         90 :                 iov = &bio->fused_iovs[bio->fused_iovpos];
          #  #  #  #  #  
                      # ]
    8201   [ +  -  #  #  :         90 :                 if (bio->fused_iov_offset < iov->iov_len) {
          #  #  #  #  #  
                      # ]
    8202                 :         90 :                         break;
    8203                 :            :                 }
    8204                 :            : 
    8205   [ #  #  #  #  :          0 :                 bio->fused_iov_offset -= iov->iov_len;
             #  #  #  # ]
    8206                 :          0 :         }
    8207                 :         90 : }
    8208                 :            : 
    8209                 :            : static int
    8210                 :         90 : bdev_nvme_queued_next_fused_sge(void *ref, void **address, uint32_t *length)
    8211                 :            : {
    8212                 :         90 :         struct nvme_bdev_io *bio = ref;
    8213                 :            :         struct iovec *iov;
    8214                 :            : 
    8215   [ -  +  #  #  :         90 :         assert(bio->fused_iovpos < bio->fused_iovcnt);
          #  #  #  #  #  
                #  #  # ]
    8216                 :            : 
    8217   [ #  #  #  #  :         90 :         iov = &bio->fused_iovs[bio->fused_iovpos];
          #  #  #  #  #  
                      # ]
    8218                 :            : 
    8219   [ #  #  #  #  :         90 :         *address = iov->iov_base;
                   #  # ]
    8220   [ #  #  #  #  :         90 :         *length = iov->iov_len;
                   #  # ]
    8221                 :            : 
    8222   [ -  +  #  #  :         90 :         if (bio->fused_iov_offset) {
                   #  # ]
    8223   [ #  #  #  #  :          0 :                 assert(bio->fused_iov_offset <= iov->iov_len);
          #  #  #  #  #  
                #  #  # ]
    8224   [ #  #  #  #  :          0 :                 *address += bio->fused_iov_offset;
                   #  # ]
    8225   [ #  #  #  #  :          0 :                 *length -= bio->fused_iov_offset;
                   #  # ]
    8226                 :          0 :         }
    8227                 :            : 
    8228   [ #  #  #  #  :         90 :         bio->fused_iov_offset += *length;
                   #  # ]
    8229   [ +  -  #  #  :         90 :         if (bio->fused_iov_offset == iov->iov_len) {
          #  #  #  #  #  
                      # ]
    8230   [ #  #  #  # ]:         90 :                 bio->fused_iovpos++;
    8231   [ #  #  #  # ]:         90 :                 bio->fused_iov_offset = 0;
    8232                 :          0 :         }
    8233                 :            : 
    8234                 :         90 :         return 0;
    8235                 :            : }
    8236                 :            : 
    8237                 :            : static int
    8238                 :          0 : bdev_nvme_no_pi_readv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
    8239                 :            :                       void *md, uint64_t lba_count, uint64_t lba)
    8240                 :            : {
    8241                 :            :         int rc;
    8242                 :            : 
    8243   [ #  #  #  #  :          0 :         SPDK_DEBUGLOG(bdev_nvme, "read %" PRIu64 " blocks with offset %#" PRIx64 " without PI check\n",
                   #  # ]
    8244                 :            :                       lba_count, lba);
    8245                 :            : 
    8246   [ #  #  #  # ]:          0 :         bio->iovs = iov;
    8247   [ #  #  #  # ]:          0 :         bio->iovcnt = iovcnt;
    8248   [ #  #  #  # ]:          0 :         bio->iovpos = 0;
    8249   [ #  #  #  # ]:          0 :         bio->iov_offset = 0;
    8250                 :            : 
    8251   [ #  #  #  #  :          0 :         rc = spdk_nvme_ns_cmd_readv_with_md(bio->io_path->nvme_ns->ns,
          #  #  #  #  #  
                #  #  # ]
    8252   [ #  #  #  #  :          0 :                                             bio->io_path->qpair->qpair,
          #  #  #  #  #  
                #  #  # ]
    8253                 :          0 :                                             lba, lba_count,
    8254                 :          0 :                                             bdev_nvme_no_pi_readv_done, bio, 0,
    8255                 :            :                                             bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge,
    8256                 :          0 :                                             md, 0, 0);
    8257                 :            : 
    8258   [ #  #  #  # ]:          0 :         if (rc != 0 && rc != -ENOMEM) {
    8259                 :          0 :                 SPDK_ERRLOG("no_pi_readv failed: rc = %d\n", rc);
    8260                 :          0 :         }
    8261                 :          0 :         return rc;
    8262                 :            : }
    8263                 :            : 
    8264                 :            : static int
    8265                 :   21378369 : bdev_nvme_readv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
    8266                 :            :                 void *md, uint64_t lba_count, uint64_t lba, uint32_t flags,
    8267                 :            :                 struct spdk_memory_domain *domain, void *domain_ctx,
    8268                 :            :                 struct spdk_accel_sequence *seq)
    8269                 :            : {
    8270   [ +  -  +  -  :   21378369 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          +  -  +  -  +  
                -  +  - ]
    8271   [ +  -  +  -  :   21378369 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          +  -  +  -  +  
                -  +  - ]
    8272                 :            :         int rc;
    8273                 :            : 
    8274   [ +  +  +  +  :   21378369 :         SPDK_DEBUGLOG(bdev_nvme, "read %" PRIu64 " blocks with offset %#" PRIx64 "\n",
                   +  - ]
    8275                 :            :                       lba_count, lba);
    8276                 :            : 
    8277   [ +  -  +  - ]:   21378369 :         bio->iovs = iov;
    8278   [ +  -  +  - ]:   21378369 :         bio->iovcnt = iovcnt;
    8279   [ +  -  +  - ]:   21378369 :         bio->iovpos = 0;
    8280   [ +  -  +  - ]:   21378369 :         bio->iov_offset = 0;
    8281                 :            : 
    8282   [ +  +  +  + ]:   21378369 :         if (domain != NULL || seq != NULL) {
    8283   [ #  #  #  #  :     367100 :                 bio->ext_opts.size = SPDK_SIZEOF(&bio->ext_opts, accel_sequence);
                   #  # ]
    8284   [ #  #  #  #  :     367100 :                 bio->ext_opts.memory_domain = domain;
                   #  # ]
    8285   [ #  #  #  #  :     367100 :                 bio->ext_opts.memory_domain_ctx = domain_ctx;
                   #  # ]
    8286   [ #  #  #  #  :     367100 :                 bio->ext_opts.io_flags = flags;
                   #  # ]
    8287   [ #  #  #  #  :     367100 :                 bio->ext_opts.metadata = md;
                   #  # ]
    8288   [ #  #  #  #  :     367100 :                 bio->ext_opts.accel_sequence = seq;
                   #  # ]
    8289                 :            : 
    8290         [ +  - ]:     367100 :                 if (iovcnt == 1) {
    8291   [ #  #  #  #  :     367100 :                         rc = spdk_nvme_ns_cmd_read_ext(ns, qpair, iov[0].iov_base, lba, lba_count, bdev_nvme_readv_done,
                   #  # ]
    8292         [ #  # ]:          0 :                                                        bio, &bio->ext_opts);
    8293                 :          0 :                 } else {
    8294                 :          0 :                         rc = spdk_nvme_ns_cmd_readv_ext(ns, qpair, lba, lba_count,
    8295                 :          0 :                                                         bdev_nvme_readv_done, bio,
    8296                 :            :                                                         bdev_nvme_queued_reset_sgl,
    8297                 :            :                                                         bdev_nvme_queued_next_sge,
    8298         [ #  # ]:          0 :                                                         &bio->ext_opts);
    8299                 :            :                 }
    8300         [ +  + ]:   21011269 :         } else if (iovcnt == 1) {
    8301   [ -  +  -  +  :   20181196 :                 rc = spdk_nvme_ns_cmd_read_with_md(ns, qpair, iov[0].iov_base,
                   -  + ]
    8302                 :          3 :                                                    md, lba, lba_count, bdev_nvme_readv_done,
    8303                 :          3 :                                                    bio, flags, 0, 0);
    8304                 :          3 :         } else {
    8305                 :     830073 :                 rc = spdk_nvme_ns_cmd_readv_with_md(ns, qpair, lba, lba_count,
    8306                 :          0 :                                                     bdev_nvme_readv_done, bio, flags,
    8307                 :            :                                                     bdev_nvme_queued_reset_sgl,
    8308                 :          0 :                                                     bdev_nvme_queued_next_sge, md, 0, 0);
    8309                 :            :         }
    8310                 :            : 
    8311   [ +  +  +  + ]:   21378369 :         if (spdk_unlikely(rc != 0 && rc != -ENOMEM)) {
    8312                 :          0 :                 SPDK_ERRLOG("readv failed: rc = %d\n", rc);
    8313                 :          0 :         }
    8314                 :   21378369 :         return rc;
    8315                 :            : }
    8316                 :            : 
    8317                 :            : static int
    8318                 :   20266596 : bdev_nvme_writev(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
    8319                 :            :                  void *md, uint64_t lba_count, uint64_t lba, uint32_t flags,
    8320                 :            :                  struct spdk_memory_domain *domain, void *domain_ctx,
    8321                 :            :                  struct spdk_accel_sequence *seq,
    8322                 :            :                  union spdk_bdev_nvme_cdw12 cdw12, union spdk_bdev_nvme_cdw13 cdw13)
    8323                 :            : {
    8324   [ #  #  #  #  :   20266596 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8325   [ #  #  #  #  :   20266596 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8326                 :            :         int rc;
    8327                 :            : 
    8328   [ -  +  -  +  :   20266596 :         SPDK_DEBUGLOG(bdev_nvme, "write %" PRIu64 " blocks with offset %#" PRIx64 "\n",
                   #  # ]
    8329                 :            :                       lba_count, lba);
    8330                 :            : 
    8331   [ #  #  #  # ]:   20266596 :         bio->iovs = iov;
    8332   [ #  #  #  # ]:   20266596 :         bio->iovcnt = iovcnt;
    8333   [ #  #  #  # ]:   20266596 :         bio->iovpos = 0;
    8334   [ #  #  #  # ]:   20266596 :         bio->iov_offset = 0;
    8335                 :            : 
    8336   [ +  +  -  + ]:   20266596 :         if (domain != NULL || seq != NULL) {
    8337   [ #  #  #  #  :     202653 :                 bio->ext_opts.size = SPDK_SIZEOF(&bio->ext_opts, accel_sequence);
                   #  # ]
    8338   [ #  #  #  #  :     202653 :                 bio->ext_opts.memory_domain = domain;
                   #  # ]
    8339   [ #  #  #  #  :     202653 :                 bio->ext_opts.memory_domain_ctx = domain_ctx;
                   #  # ]
    8340   [ -  +  #  #  :     202653 :                 bio->ext_opts.io_flags = flags | SPDK_NVME_IO_FLAGS_DIRECTIVE(cdw12.write.dtype);
          #  #  #  #  #  
                      # ]
    8341   [ #  #  #  #  :     202653 :                 bio->ext_opts.cdw13 = cdw13.raw;
                   #  # ]
    8342   [ #  #  #  #  :     202653 :                 bio->ext_opts.metadata = md;
                   #  # ]
    8343   [ #  #  #  #  :     202653 :                 bio->ext_opts.accel_sequence = seq;
                   #  # ]
    8344                 :            : 
    8345         [ +  - ]:     202653 :                 if (iovcnt == 1) {
    8346   [ #  #  #  #  :     202653 :                         rc = spdk_nvme_ns_cmd_write_ext(ns, qpair, iov[0].iov_base, lba, lba_count, bdev_nvme_writev_done,
                   #  # ]
    8347         [ #  # ]:          0 :                                                         bio, &bio->ext_opts);
    8348                 :          0 :                 } else {
    8349                 :          0 :                         rc = spdk_nvme_ns_cmd_writev_ext(ns, qpair, lba, lba_count,
    8350                 :          0 :                                                          bdev_nvme_writev_done, bio,
    8351                 :            :                                                          bdev_nvme_queued_reset_sgl,
    8352                 :            :                                                          bdev_nvme_queued_next_sge,
    8353         [ #  # ]:          0 :                                                          &bio->ext_opts);
    8354                 :            :                 }
    8355         [ +  + ]:   20063943 :         } else if (iovcnt == 1) {
    8356   [ #  #  #  #  :   18630927 :                 rc = spdk_nvme_ns_cmd_write_with_md(ns, qpair, iov[0].iov_base,
                   #  # ]
    8357                 :          0 :                                                     md, lba, lba_count, bdev_nvme_writev_done,
    8358                 :          0 :                                                     bio, flags, 0, 0);
    8359                 :          0 :         } else {
    8360                 :    1433016 :                 rc = spdk_nvme_ns_cmd_writev_with_md(ns, qpair, lba, lba_count,
    8361                 :          0 :                                                      bdev_nvme_writev_done, bio, flags,
    8362                 :            :                                                      bdev_nvme_queued_reset_sgl,
    8363                 :          0 :                                                      bdev_nvme_queued_next_sge, md, 0, 0);
    8364                 :            :         }
    8365                 :            : 
    8366   [ +  +  -  + ]:   20266596 :         if (spdk_unlikely(rc != 0 && rc != -ENOMEM)) {
    8367                 :          0 :                 SPDK_ERRLOG("writev failed: rc = %d\n", rc);
    8368                 :          0 :         }
    8369                 :   20266596 :         return rc;
    8370                 :            : }
    8371                 :            : 
    8372                 :            : static int
    8373                 :     268251 : bdev_nvme_zone_appendv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
    8374                 :            :                        void *md, uint64_t lba_count, uint64_t zslba,
    8375                 :            :                        uint32_t flags)
    8376                 :            : {
    8377   [ #  #  #  #  :     268251 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8378   [ #  #  #  #  :     268251 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8379                 :            :         int rc;
    8380                 :            : 
    8381   [ -  +  #  #  :     268251 :         SPDK_DEBUGLOG(bdev_nvme, "zone append %" PRIu64 " blocks to zone start lba %#" PRIx64 "\n",
                   #  # ]
    8382                 :            :                       lba_count, zslba);
    8383                 :            : 
    8384   [ #  #  #  # ]:     268251 :         bio->iovs = iov;
    8385   [ #  #  #  # ]:     268251 :         bio->iovcnt = iovcnt;
    8386   [ #  #  #  # ]:     268251 :         bio->iovpos = 0;
    8387   [ #  #  #  # ]:     268251 :         bio->iov_offset = 0;
    8388                 :            : 
    8389         [ +  - ]:     268251 :         if (iovcnt == 1) {
    8390   [ #  #  #  #  :     268251 :                 rc = spdk_nvme_zns_zone_append_with_md(ns, qpair, iov[0].iov_base, md, zslba,
                   #  # ]
    8391                 :          0 :                                                        lba_count,
    8392                 :          0 :                                                        bdev_nvme_zone_appendv_done, bio,
    8393                 :          0 :                                                        flags,
    8394                 :            :                                                        0, 0);
    8395                 :          0 :         } else {
    8396                 :          0 :                 rc = spdk_nvme_zns_zone_appendv_with_md(ns, qpair, zslba, lba_count,
    8397                 :          0 :                                                         bdev_nvme_zone_appendv_done, bio, flags,
    8398                 :            :                                                         bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge,
    8399                 :          0 :                                                         md, 0, 0);
    8400                 :            :         }
    8401                 :            : 
    8402   [ -  +  -  - ]:     268251 :         if (rc != 0 && rc != -ENOMEM) {
    8403                 :          0 :                 SPDK_ERRLOG("zone append failed: rc = %d\n", rc);
    8404                 :          0 :         }
    8405                 :     268251 :         return rc;
    8406                 :            : }
    8407                 :            : 
    8408                 :            : static int
    8409                 :         53 : bdev_nvme_comparev(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
    8410                 :            :                    void *md, uint64_t lba_count, uint64_t lba,
    8411                 :            :                    uint32_t flags)
    8412                 :            : {
    8413                 :            :         int rc;
    8414                 :            : 
    8415   [ -  +  -  +  :         53 :         SPDK_DEBUGLOG(bdev_nvme, "compare %" PRIu64 " blocks with offset %#" PRIx64 "\n",
                   #  # ]
    8416                 :            :                       lba_count, lba);
    8417                 :            : 
    8418   [ #  #  #  # ]:         53 :         bio->iovs = iov;
    8419   [ #  #  #  # ]:         53 :         bio->iovcnt = iovcnt;
    8420   [ #  #  #  # ]:         53 :         bio->iovpos = 0;
    8421   [ #  #  #  # ]:         53 :         bio->iov_offset = 0;
    8422                 :            : 
    8423   [ #  #  #  #  :         53 :         rc = spdk_nvme_ns_cmd_comparev_with_md(bio->io_path->nvme_ns->ns,
          #  #  #  #  #  
                #  #  # ]
    8424   [ #  #  #  #  :         53 :                                                bio->io_path->qpair->qpair,
          #  #  #  #  #  
                #  #  # ]
    8425                 :          0 :                                                lba, lba_count,
    8426                 :          0 :                                                bdev_nvme_comparev_done, bio, flags,
    8427                 :            :                                                bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge,
    8428                 :          0 :                                                md, 0, 0);
    8429                 :            : 
    8430   [ -  +  -  - ]:         53 :         if (rc != 0 && rc != -ENOMEM) {
    8431                 :          0 :                 SPDK_ERRLOG("comparev failed: rc = %d\n", rc);
    8432                 :          0 :         }
    8433                 :         53 :         return rc;
    8434                 :            : }
    8435                 :            : 
    8436                 :            : static int
    8437                 :         51 : bdev_nvme_comparev_and_writev(struct nvme_bdev_io *bio, struct iovec *cmp_iov, int cmp_iovcnt,
    8438                 :            :                               struct iovec *write_iov, int write_iovcnt,
    8439                 :            :                               void *md, uint64_t lba_count, uint64_t lba, uint32_t flags)
    8440                 :            : {
    8441   [ #  #  #  #  :         51 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8442   [ #  #  #  #  :         51 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8443                 :         51 :         struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
    8444                 :            :         int rc;
    8445                 :            : 
    8446   [ -  +  -  +  :         51 :         SPDK_DEBUGLOG(bdev_nvme, "compare and write %" PRIu64 " blocks with offset %#" PRIx64 "\n",
                   #  # ]
    8447                 :            :                       lba_count, lba);
    8448                 :            : 
    8449   [ #  #  #  # ]:         51 :         bio->iovs = cmp_iov;
    8450   [ #  #  #  # ]:         51 :         bio->iovcnt = cmp_iovcnt;
    8451   [ #  #  #  # ]:         51 :         bio->iovpos = 0;
    8452   [ #  #  #  # ]:         51 :         bio->iov_offset = 0;
    8453   [ #  #  #  # ]:         51 :         bio->fused_iovs = write_iov;
    8454   [ #  #  #  # ]:         51 :         bio->fused_iovcnt = write_iovcnt;
    8455   [ #  #  #  # ]:         51 :         bio->fused_iovpos = 0;
    8456   [ #  #  #  # ]:         51 :         bio->fused_iov_offset = 0;
    8457                 :            : 
    8458   [ +  -  #  #  :         51 :         if (bdev_io->num_retries == 0) {
                   #  # ]
    8459   [ #  #  #  # ]:         51 :                 bio->first_fused_submitted = false;
    8460   [ #  #  #  # ]:         51 :                 bio->first_fused_completed = false;
    8461                 :          0 :         }
    8462                 :            : 
    8463   [ +  +  +  -  :         51 :         if (!bio->first_fused_submitted) {
             #  #  #  # ]
    8464   [ #  #  #  # ]:         51 :                 flags |= SPDK_NVME_IO_FLAGS_FUSE_FIRST;
    8465   [ -  +  #  # ]:         51 :                 memset(&bio->cpl, 0, sizeof(bio->cpl));
    8466                 :            : 
    8467                 :         51 :                 rc = spdk_nvme_ns_cmd_comparev_with_md(ns, qpair, lba, lba_count,
    8468                 :          0 :                                                        bdev_nvme_comparev_and_writev_done, bio, flags,
    8469                 :          0 :                                                        bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge, md, 0, 0);
    8470         [ +  - ]:         51 :                 if (rc == 0) {
    8471   [ #  #  #  # ]:         51 :                         bio->first_fused_submitted = true;
    8472   [ #  #  #  # ]:         51 :                         flags &= ~SPDK_NVME_IO_FLAGS_FUSE_FIRST;
    8473                 :          0 :                 } else {
    8474         [ #  # ]:          0 :                         if (rc != -ENOMEM) {
    8475                 :          0 :                                 SPDK_ERRLOG("compare failed: rc = %d\n", rc);
    8476                 :          0 :                         }
    8477                 :          0 :                         return rc;
    8478                 :            :                 }
    8479                 :          0 :         }
    8480                 :            : 
    8481   [ #  #  #  # ]:         51 :         flags |= SPDK_NVME_IO_FLAGS_FUSE_SECOND;
    8482                 :            : 
    8483                 :         51 :         rc = spdk_nvme_ns_cmd_writev_with_md(ns, qpair, lba, lba_count,
    8484                 :          0 :                                              bdev_nvme_comparev_and_writev_done, bio, flags,
    8485                 :          0 :                                              bdev_nvme_queued_reset_fused_sgl, bdev_nvme_queued_next_fused_sge, md, 0, 0);
    8486   [ -  +  -  - ]:         51 :         if (rc != 0 && rc != -ENOMEM) {
    8487                 :          0 :                 SPDK_ERRLOG("write failed: rc = %d\n", rc);
    8488                 :          0 :                 rc = 0;
    8489                 :          0 :         }
    8490                 :            : 
    8491                 :         51 :         return rc;
    8492                 :          0 : }
    8493                 :            : 
    8494                 :            : static int
    8495                 :     259832 : bdev_nvme_unmap(struct nvme_bdev_io *bio, uint64_t offset_blocks, uint64_t num_blocks)
    8496                 :            : {
    8497                 :     167916 :         struct spdk_nvme_dsm_range dsm_ranges[SPDK_NVME_DATASET_MANAGEMENT_MAX_RANGES];
    8498                 :            :         struct spdk_nvme_dsm_range *range;
    8499                 :            :         uint64_t offset, remaining;
    8500                 :            :         uint64_t num_ranges_u64;
    8501                 :            :         uint16_t num_ranges;
    8502                 :            :         int rc;
    8503                 :            : 
    8504         [ #  # ]:     259832 :         num_ranges_u64 = (num_blocks + SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS - 1) /
    8505                 :            :                          SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS;
    8506         [ -  + ]:     259832 :         if (num_ranges_u64 > SPDK_COUNTOF(dsm_ranges)) {
    8507                 :          0 :                 SPDK_ERRLOG("Unmap request for %" PRIu64 " blocks is too large\n", num_blocks);
    8508                 :          0 :                 return -EINVAL;
    8509                 :            :         }
    8510                 :     259832 :         num_ranges = (uint16_t)num_ranges_u64;
    8511                 :            : 
    8512                 :     259832 :         offset = offset_blocks;
    8513                 :     259832 :         remaining = num_blocks;
    8514   [ #  #  #  # ]:     259832 :         range = &dsm_ranges[0];
    8515                 :            : 
    8516                 :            :         /* Fill max-size ranges until the remaining blocks fit into one range */
    8517         [ +  + ]:     259834 :         while (remaining > SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS) {
    8518   [ #  #  #  #  :          2 :                 range->attributes.raw = 0;
                   #  # ]
    8519   [ #  #  #  # ]:          2 :                 range->length = SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS;
    8520   [ #  #  #  # ]:          2 :                 range->starting_lba = offset;
    8521                 :            : 
    8522                 :          2 :                 offset += SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS;
    8523                 :          2 :                 remaining -= SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS;
    8524         [ #  # ]:          2 :                 range++;
    8525                 :            :         }
    8526                 :            : 
    8527                 :            :         /* Final range describes the remaining blocks */
    8528   [ #  #  #  #  :     259832 :         range->attributes.raw = 0;
                   #  # ]
    8529   [ #  #  #  # ]:     259832 :         range->length = remaining;
    8530   [ #  #  #  # ]:     259832 :         range->starting_lba = offset;
    8531                 :            : 
    8532   [ #  #  #  #  :     259832 :         rc = spdk_nvme_ns_cmd_dataset_management(bio->io_path->nvme_ns->ns,
          #  #  #  #  #  
                #  #  # ]
    8533   [ #  #  #  #  :     259832 :                         bio->io_path->qpair->qpair,
          #  #  #  #  #  
                #  #  # ]
    8534                 :            :                         SPDK_NVME_DSM_ATTR_DEALLOCATE,
    8535                 :          0 :                         dsm_ranges, num_ranges,
    8536                 :          0 :                         bdev_nvme_queued_done, bio);
    8537                 :            : 
    8538                 :     259832 :         return rc;
    8539                 :          0 : }
    8540                 :            : 
    8541                 :            : static int
    8542                 :     689727 : bdev_nvme_write_zeroes(struct nvme_bdev_io *bio, uint64_t offset_blocks, uint64_t num_blocks)
    8543                 :            : {
    8544         [ -  + ]:     689727 :         if (num_blocks > UINT16_MAX + 1) {
    8545                 :          0 :                 SPDK_ERRLOG("NVMe write zeroes is limited to 16-bit block count\n");
    8546                 :          0 :                 return -EINVAL;
    8547                 :            :         }
    8548                 :            : 
    8549   [ #  #  #  #  :     689727 :         return spdk_nvme_ns_cmd_write_zeroes(bio->io_path->nvme_ns->ns,
          #  #  #  #  #  
                #  #  # ]
    8550   [ #  #  #  #  :     689727 :                                              bio->io_path->qpair->qpair,
          #  #  #  #  #  
                #  #  # ]
    8551                 :          0 :                                              offset_blocks, num_blocks,
    8552                 :          0 :                                              bdev_nvme_queued_done, bio,
    8553                 :            :                                              0);
    8554                 :          0 : }
    8555                 :            : 
    8556                 :            : static int
    8557                 :          1 : bdev_nvme_get_zone_info(struct nvme_bdev_io *bio, uint64_t zone_id, uint32_t num_zones,
    8558                 :            :                         struct spdk_bdev_zone_info *info)
    8559                 :            : {
    8560   [ #  #  #  #  :          1 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8561   [ #  #  #  #  :          1 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8562                 :          1 :         uint32_t zone_report_bufsize = spdk_nvme_ns_get_max_io_xfer_size(ns);
    8563                 :          1 :         uint64_t zone_size = spdk_nvme_zns_ns_get_zone_size_sectors(ns);
    8564                 :          1 :         uint64_t total_zones = spdk_nvme_zns_ns_get_num_zones(ns);
    8565                 :            : 
    8566   [ -  +  #  # ]:          1 :         if (zone_id % zone_size != 0) {
    8567                 :          0 :                 return -EINVAL;
    8568                 :            :         }
    8569                 :            : 
    8570   [ +  -  -  + ]:          1 :         if (num_zones > total_zones || !num_zones) {
    8571                 :          0 :                 return -EINVAL;
    8572                 :            :         }
    8573                 :            : 
    8574   [ -  +  #  #  :          1 :         assert(!bio->zone_report_buf);
             #  #  #  # ]
    8575   [ #  #  #  # ]:          1 :         bio->zone_report_buf = calloc(1, zone_report_bufsize);
    8576   [ -  +  #  #  :          1 :         if (!bio->zone_report_buf) {
                   #  # ]
    8577                 :          0 :                 return -ENOMEM;
    8578                 :            :         }
    8579                 :            : 
    8580   [ #  #  #  # ]:          1 :         bio->handled_zones = 0;
    8581                 :            : 
    8582   [ #  #  #  # ]:          1 :         return spdk_nvme_zns_report_zones(ns, qpair, bio->zone_report_buf, zone_report_bufsize,
    8583                 :          0 :                                           zone_id, SPDK_NVME_ZRA_LIST_ALL, true,
    8584                 :          0 :                                           bdev_nvme_get_zone_info_done, bio);
    8585                 :          0 : }
    8586                 :            : 
    8587                 :            : static int
    8588                 :         46 : bdev_nvme_zone_management(struct nvme_bdev_io *bio, uint64_t zone_id,
    8589                 :            :                           enum spdk_bdev_zone_action action)
    8590                 :            : {
    8591   [ #  #  #  #  :         46 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8592   [ #  #  #  #  :         46 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8593                 :            : 
    8594   [ -  -  -  +  :         46 :         switch (action) {
                   -  - ]
    8595                 :          0 :         case SPDK_BDEV_ZONE_CLOSE:
    8596                 :          0 :                 return spdk_nvme_zns_close_zone(ns, qpair, zone_id, false,
    8597                 :          0 :                                                 bdev_nvme_zone_management_done, bio);
    8598                 :          0 :         case SPDK_BDEV_ZONE_FINISH:
    8599                 :          0 :                 return spdk_nvme_zns_finish_zone(ns, qpair, zone_id, false,
    8600                 :          0 :                                                  bdev_nvme_zone_management_done, bio);
    8601                 :          0 :         case SPDK_BDEV_ZONE_OPEN:
    8602                 :          0 :                 return spdk_nvme_zns_open_zone(ns, qpair, zone_id, false,
    8603                 :          0 :                                                bdev_nvme_zone_management_done, bio);
    8604                 :         46 :         case SPDK_BDEV_ZONE_RESET:
    8605                 :         46 :                 return spdk_nvme_zns_reset_zone(ns, qpair, zone_id, false,
    8606                 :          0 :                                                 bdev_nvme_zone_management_done, bio);
    8607                 :          0 :         case SPDK_BDEV_ZONE_OFFLINE:
    8608                 :          0 :                 return spdk_nvme_zns_offline_zone(ns, qpair, zone_id, false,
    8609                 :          0 :                                                   bdev_nvme_zone_management_done, bio);
    8610                 :          0 :         default:
    8611                 :          0 :                 return -EINVAL;
    8612                 :            :         }
    8613                 :          0 : }
    8614                 :            : 
    8615                 :            : static void
    8616                 :        138 : bdev_nvme_admin_passthru(struct nvme_bdev_channel *nbdev_ch, struct nvme_bdev_io *bio,
    8617                 :            :                          struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes)
    8618                 :            : {
    8619                 :            :         struct nvme_io_path *io_path;
    8620                 :            :         struct nvme_ctrlr *nvme_ctrlr;
    8621                 :            :         uint32_t max_xfer_size;
    8622                 :        138 :         int rc = -ENXIO;
    8623                 :            : 
    8624                 :            :         /* Choose the first ctrlr which is not failed. */
    8625   [ +  +  #  #  :        147 :         STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    8626   [ #  #  #  #  :        144 :                 nvme_ctrlr = io_path->qpair->ctrlr;
             #  #  #  # ]
    8627                 :            : 
    8628                 :            :                 /* We should skip any unavailable nvme_ctrlr rather than checking
    8629                 :            :                  * if the return value of spdk_nvme_ctrlr_cmd_admin_raw() is -ENXIO.
    8630                 :            :                  */
    8631         [ +  + ]:        144 :                 if (!nvme_ctrlr_is_available(nvme_ctrlr)) {
    8632                 :          9 :                         continue;
    8633                 :            :                 }
    8634                 :            : 
    8635   [ #  #  #  # ]:        135 :                 max_xfer_size = spdk_nvme_ctrlr_get_max_xfer_size(nvme_ctrlr->ctrlr);
    8636                 :            : 
    8637         [ -  + ]:        135 :                 if (nbytes > max_xfer_size) {
    8638                 :          0 :                         SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size);
    8639                 :          0 :                         rc = -EINVAL;
    8640                 :          0 :                         goto err;
    8641                 :            :                 }
    8642                 :            : 
    8643   [ #  #  #  # ]:        135 :                 rc = spdk_nvme_ctrlr_cmd_admin_raw(nvme_ctrlr->ctrlr, cmd, buf, (uint32_t)nbytes,
    8644                 :          0 :                                                    bdev_nvme_admin_passthru_done, bio);
    8645         [ +  - ]:        135 :                 if (rc == 0) {
    8646                 :        135 :                         return;
    8647                 :            :                 }
    8648                 :          0 :         }
    8649                 :            : 
    8650                 :          3 : err:
    8651                 :          3 :         bdev_nvme_admin_complete(bio, rc);
    8652                 :          0 : }
    8653                 :            : 
    8654                 :            : static int
    8655                 :        108 : bdev_nvme_io_passthru(struct nvme_bdev_io *bio, struct spdk_nvme_cmd *cmd,
    8656                 :            :                       void *buf, size_t nbytes)
    8657                 :            : {
    8658   [ #  #  #  #  :        108 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8659   [ #  #  #  #  :        108 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8660                 :        108 :         uint32_t max_xfer_size = spdk_nvme_ns_get_max_io_xfer_size(ns);
    8661                 :        108 :         struct spdk_nvme_ctrlr *ctrlr = spdk_nvme_ns_get_ctrlr(ns);
    8662                 :            : 
    8663         [ -  + ]:        108 :         if (nbytes > max_xfer_size) {
    8664                 :          0 :                 SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size);
    8665                 :          0 :                 return -EINVAL;
    8666                 :            :         }
    8667                 :            : 
    8668                 :            :         /*
    8669                 :            :          * Each NVMe bdev is a specific namespace, and all NVMe I/O commands require a nsid,
    8670                 :            :          * so fill it out automatically.
    8671                 :            :          */
    8672   [ #  #  #  # ]:        108 :         cmd->nsid = spdk_nvme_ns_get_id(ns);
    8673                 :            : 
    8674                 :        108 :         return spdk_nvme_ctrlr_cmd_io_raw(ctrlr, qpair, cmd, buf,
    8675                 :          0 :                                           (uint32_t)nbytes, bdev_nvme_queued_done, bio);
    8676                 :          0 : }
    8677                 :            : 
    8678                 :            : static int
    8679                 :          0 : bdev_nvme_io_passthru_md(struct nvme_bdev_io *bio, struct spdk_nvme_cmd *cmd,
    8680                 :            :                          void *buf, size_t nbytes, void *md_buf, size_t md_len)
    8681                 :            : {
    8682   [ #  #  #  #  :          0 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8683   [ #  #  #  #  :          0 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8684         [ #  # ]:          0 :         size_t nr_sectors = nbytes / spdk_nvme_ns_get_extended_sector_size(ns);
    8685                 :          0 :         uint32_t max_xfer_size = spdk_nvme_ns_get_max_io_xfer_size(ns);
    8686                 :          0 :         struct spdk_nvme_ctrlr *ctrlr = spdk_nvme_ns_get_ctrlr(ns);
    8687                 :            : 
    8688         [ #  # ]:          0 :         if (nbytes > max_xfer_size) {
    8689                 :          0 :                 SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size);
    8690                 :          0 :                 return -EINVAL;
    8691                 :            :         }
    8692                 :            : 
    8693         [ #  # ]:          0 :         if (md_len != nr_sectors * spdk_nvme_ns_get_md_size(ns)) {
    8694                 :          0 :                 SPDK_ERRLOG("invalid meta data buffer size\n");
    8695                 :          0 :                 return -EINVAL;
    8696                 :            :         }
    8697                 :            : 
    8698                 :            :         /*
    8699                 :            :          * Each NVMe bdev is a specific namespace, and all NVMe I/O commands require a nsid,
    8700                 :            :          * so fill it out automatically.
    8701                 :            :          */
    8702   [ #  #  #  # ]:          0 :         cmd->nsid = spdk_nvme_ns_get_id(ns);
    8703                 :            : 
    8704                 :          0 :         return spdk_nvme_ctrlr_cmd_io_raw_with_md(ctrlr, qpair, cmd, buf,
    8705                 :          0 :                         (uint32_t)nbytes, md_buf, bdev_nvme_queued_done, bio);
    8706                 :          0 : }
    8707                 :            : 
    8708                 :            : static int
    8709                 :          0 : bdev_nvme_iov_passthru_md(struct nvme_bdev_io *bio,
    8710                 :            :                           struct spdk_nvme_cmd *cmd, struct iovec *iov, int iovcnt,
    8711                 :            :                           size_t nbytes, void *md_buf, size_t md_len)
    8712                 :            : {
    8713   [ #  #  #  #  :          0 :         struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
          #  #  #  #  #  
                #  #  # ]
    8714   [ #  #  #  #  :          0 :         struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
          #  #  #  #  #  
                #  #  # ]
    8715         [ #  # ]:          0 :         size_t nr_sectors = nbytes / spdk_nvme_ns_get_extended_sector_size(ns);
    8716                 :          0 :         uint32_t max_xfer_size = spdk_nvme_ns_get_max_io_xfer_size(ns);
    8717                 :          0 :         struct spdk_nvme_ctrlr *ctrlr = spdk_nvme_ns_get_ctrlr(ns);
    8718                 :            : 
    8719   [ #  #  #  # ]:          0 :         bio->iovs = iov;
    8720   [ #  #  #  # ]:          0 :         bio->iovcnt = iovcnt;
    8721   [ #  #  #  # ]:          0 :         bio->iovpos = 0;
    8722   [ #  #  #  # ]:          0 :         bio->iov_offset = 0;
    8723                 :            : 
    8724         [ #  # ]:          0 :         if (nbytes > max_xfer_size) {
    8725                 :          0 :                 SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size);
    8726                 :          0 :                 return -EINVAL;
    8727                 :            :         }
    8728                 :            : 
    8729         [ #  # ]:          0 :         if (md_len != nr_sectors * spdk_nvme_ns_get_md_size(ns)) {
    8730                 :          0 :                 SPDK_ERRLOG("invalid meta data buffer size\n");
    8731                 :          0 :                 return -EINVAL;
    8732                 :            :         }
    8733                 :            : 
    8734                 :            :         /*
    8735                 :            :          * Each NVMe bdev is a specific namespace, and all NVMe I/O commands
    8736                 :            :          * require a nsid, so fill it out automatically.
    8737                 :            :          */
    8738   [ #  #  #  # ]:          0 :         cmd->nsid = spdk_nvme_ns_get_id(ns);
    8739                 :            : 
    8740                 :          0 :         return spdk_nvme_ctrlr_cmd_iov_raw_with_md(
    8741                 :          0 :                        ctrlr, qpair, cmd, (uint32_t)nbytes, md_buf, bdev_nvme_queued_done, bio,
    8742                 :            :                        bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge);
    8743                 :          0 : }
    8744                 :            : 
    8745                 :            : static void
    8746                 :       7293 : bdev_nvme_abort(struct nvme_bdev_channel *nbdev_ch, struct nvme_bdev_io *bio,
    8747                 :            :                 struct nvme_bdev_io *bio_to_abort)
    8748                 :            : {
    8749                 :            :         struct nvme_io_path *io_path;
    8750                 :       7293 :         int rc = 0;
    8751                 :            : 
    8752                 :       7293 :         rc = bdev_nvme_abort_retry_io(nbdev_ch, bio_to_abort);
    8753         [ +  + ]:       7293 :         if (rc == 0) {
    8754                 :          3 :                 bdev_nvme_admin_complete(bio, 0);
    8755                 :          3 :                 return;
    8756                 :            :         }
    8757                 :            : 
    8758   [ #  #  #  # ]:       7290 :         io_path = bio_to_abort->io_path;
    8759         [ +  + ]:       7290 :         if (io_path != NULL) {
    8760   [ #  #  #  #  :       7284 :                 rc = spdk_nvme_ctrlr_cmd_abort_ext(io_path->qpair->ctrlr->ctrlr,
          #  #  #  #  #  
                #  #  # ]
    8761   [ #  #  #  #  :       7284 :                                                    io_path->qpair->qpair,
             #  #  #  # ]
    8762                 :          0 :                                                    bio_to_abort,
    8763                 :          0 :                                                    bdev_nvme_abort_done, bio);
    8764                 :          0 :         } else {
    8765   [ +  +  #  #  :          9 :                 STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    8766   [ #  #  #  #  :          6 :                         rc = spdk_nvme_ctrlr_cmd_abort_ext(io_path->qpair->ctrlr->ctrlr,
          #  #  #  #  #  
                #  #  # ]
    8767                 :            :                                                            NULL,
    8768                 :          0 :                                                            bio_to_abort,
    8769                 :          0 :                                                            bdev_nvme_abort_done, bio);
    8770                 :            : 
    8771         [ +  + ]:          6 :                         if (rc != -ENOENT) {
    8772                 :          3 :                                 break;
    8773                 :            :                         }
    8774                 :          0 :                 }
    8775                 :            :         }
    8776                 :            : 
    8777         [ +  + ]:       7290 :         if (rc != 0) {
    8778                 :            :                 /* If no command was found or there was any error, complete the abort
    8779                 :            :                  * request with failure.
    8780                 :            :                  */
    8781                 :          6 :                 bdev_nvme_admin_complete(bio, rc);
    8782                 :          0 :         }
    8783                 :          0 : }
    8784                 :            : 
    8785                 :            : static int
    8786                 :         36 : bdev_nvme_copy(struct nvme_bdev_io *bio, uint64_t dst_offset_blocks, uint64_t src_offset_blocks,
    8787                 :            :                uint64_t num_blocks)
    8788                 :            : {
    8789                 :         36 :         struct spdk_nvme_scc_source_range range = {
    8790                 :          0 :                 .slba = src_offset_blocks,
    8791                 :         36 :                 .nlb = num_blocks - 1
    8792                 :            :         };
    8793                 :            : 
    8794   [ #  #  #  #  :         44 :         return spdk_nvme_ns_cmd_copy(bio->io_path->nvme_ns->ns,
          #  #  #  #  #  
                #  #  # ]
    8795   [ #  #  #  #  :         36 :                                      bio->io_path->qpair->qpair,
          #  #  #  #  #  
                #  #  # ]
    8796                 :          0 :                                      &range, 1, dst_offset_blocks,
    8797                 :          0 :                                      bdev_nvme_queued_done, bio);
    8798                 :            : }
    8799                 :            : 
    8800                 :            : static void
    8801                 :        165 : bdev_nvme_opts_config_json(struct spdk_json_write_ctx *w)
    8802                 :            : {
    8803                 :            :         const char *action;
    8804                 :            :         uint32_t i;
    8805                 :            : 
    8806   [ +  +  -  + ]:        165 :         if (g_opts.action_on_timeout == SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET) {
    8807                 :          0 :                 action = "reset";
    8808   [ +  +  -  + ]:        165 :         } else if (g_opts.action_on_timeout == SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT) {
    8809                 :          6 :                 action = "abort";
    8810                 :          0 :         } else {
    8811                 :        159 :                 action = "none";
    8812                 :            :         }
    8813                 :            : 
    8814                 :        165 :         spdk_json_write_object_begin(w);
    8815                 :            : 
    8816                 :        165 :         spdk_json_write_named_string(w, "method", "bdev_nvme_set_options");
    8817                 :            : 
    8818                 :        165 :         spdk_json_write_named_object_begin(w, "params");
    8819                 :        165 :         spdk_json_write_named_string(w, "action_on_timeout", action);
    8820         [ +  - ]:        165 :         spdk_json_write_named_uint64(w, "timeout_us", g_opts.timeout_us);
    8821         [ +  - ]:        165 :         spdk_json_write_named_uint64(w, "timeout_admin_us", g_opts.timeout_admin_us);
    8822         [ +  - ]:        165 :         spdk_json_write_named_uint32(w, "keep_alive_timeout_ms", g_opts.keep_alive_timeout_ms);
    8823         [ +  - ]:        165 :         spdk_json_write_named_uint32(w, "arbitration_burst", g_opts.arbitration_burst);
    8824         [ +  - ]:        165 :         spdk_json_write_named_uint32(w, "low_priority_weight", g_opts.low_priority_weight);
    8825         [ +  - ]:        165 :         spdk_json_write_named_uint32(w, "medium_priority_weight", g_opts.medium_priority_weight);
    8826         [ +  - ]:        165 :         spdk_json_write_named_uint32(w, "high_priority_weight", g_opts.high_priority_weight);
    8827         [ +  - ]:        165 :         spdk_json_write_named_uint64(w, "nvme_adminq_poll_period_us", g_opts.nvme_adminq_poll_period_us);
    8828         [ +  - ]:        165 :         spdk_json_write_named_uint64(w, "nvme_ioq_poll_period_us", g_opts.nvme_ioq_poll_period_us);
    8829         [ +  - ]:        165 :         spdk_json_write_named_uint32(w, "io_queue_requests", g_opts.io_queue_requests);
    8830   [ +  +  +  - ]:        165 :         spdk_json_write_named_bool(w, "delay_cmd_submit", g_opts.delay_cmd_submit);
    8831         [ +  - ]:        165 :         spdk_json_write_named_uint32(w, "transport_retry_count", g_opts.transport_retry_count);
    8832         [ +  - ]:        165 :         spdk_json_write_named_int32(w, "bdev_retry_count", g_opts.bdev_retry_count);
    8833         [ +  - ]:        165 :         spdk_json_write_named_uint8(w, "transport_ack_timeout", g_opts.transport_ack_timeout);
    8834         [ +  - ]:        165 :         spdk_json_write_named_int32(w, "ctrlr_loss_timeout_sec", g_opts.ctrlr_loss_timeout_sec);
    8835         [ +  - ]:        165 :         spdk_json_write_named_uint32(w, "reconnect_delay_sec", g_opts.reconnect_delay_sec);
    8836         [ +  - ]:        165 :         spdk_json_write_named_uint32(w, "fast_io_fail_timeout_sec", g_opts.fast_io_fail_timeout_sec);
    8837         [ +  + ]:        165 :         spdk_json_write_named_bool(w, "disable_auto_failback", g_opts.disable_auto_failback);
    8838   [ +  +  +  - ]:        165 :         spdk_json_write_named_bool(w, "generate_uuids", g_opts.generate_uuids);
    8839                 :        165 :         spdk_json_write_named_uint8(w, "transport_tos", g_opts.transport_tos);
    8840   [ +  +  +  - ]:        165 :         spdk_json_write_named_bool(w, "nvme_error_stat", g_opts.nvme_error_stat);
    8841         [ +  - ]:        165 :         spdk_json_write_named_uint32(w, "rdma_srq_size", g_opts.rdma_srq_size);
    8842         [ +  + ]:        165 :         spdk_json_write_named_bool(w, "io_path_stat", g_opts.io_path_stat);
    8843   [ +  +  +  - ]:        165 :         spdk_json_write_named_bool(w, "allow_accel_sequence", g_opts.allow_accel_sequence);
    8844         [ +  - ]:        165 :         spdk_json_write_named_uint32(w, "rdma_max_cq_size", g_opts.rdma_max_cq_size);
    8845         [ +  - ]:        165 :         spdk_json_write_named_uint16(w, "rdma_cm_event_timeout_ms", g_opts.rdma_cm_event_timeout_ms);
    8846                 :        165 :         spdk_json_write_named_array_begin(w, "dhchap_digests");
    8847         [ +  + ]:       5445 :         for (i = 0; i < 32; ++i) {
    8848   [ +  +  +  +  :       5280 :                 if (g_opts.dhchap_digests & SPDK_BIT(i)) {
                   +  + ]
    8849                 :        495 :                         spdk_json_write_string(w, spdk_nvme_dhchap_get_digest_name(i));
    8850                 :          3 :                 }
    8851                 :         32 :         }
    8852                 :        165 :         spdk_json_write_array_end(w);
    8853                 :        165 :         spdk_json_write_named_array_begin(w, "dhchap_dhgroups");
    8854         [ +  + ]:       5445 :         for (i = 0; i < 32; ++i) {
    8855   [ +  +  +  +  :       5280 :                 if (g_opts.dhchap_dhgroups & SPDK_BIT(i)) {
                   +  + ]
    8856                 :        990 :                         spdk_json_write_string(w, spdk_nvme_dhchap_get_dhgroup_name(i));
    8857                 :          6 :                 }
    8858                 :         32 :         }
    8859                 :            : 
    8860                 :        165 :         spdk_json_write_array_end(w);
    8861                 :        165 :         spdk_json_write_object_end(w);
    8862                 :            : 
    8863                 :        165 :         spdk_json_write_object_end(w);
    8864                 :        165 : }
    8865                 :            : 
    8866                 :            : static void
    8867                 :          0 : bdev_nvme_discovery_config_json(struct spdk_json_write_ctx *w, struct discovery_ctx *ctx)
    8868                 :            : {
    8869                 :          0 :         struct spdk_nvme_transport_id trid;
    8870                 :            : 
    8871                 :          0 :         spdk_json_write_object_begin(w);
    8872                 :            : 
    8873                 :          0 :         spdk_json_write_named_string(w, "method", "bdev_nvme_start_discovery");
    8874                 :            : 
    8875                 :          0 :         spdk_json_write_named_object_begin(w, "params");
    8876   [ #  #  #  # ]:          0 :         spdk_json_write_named_string(w, "name", ctx->name);
    8877   [ #  #  #  # ]:          0 :         spdk_json_write_named_string(w, "hostnqn", ctx->hostnqn);
    8878                 :            : 
    8879         [ #  # ]:          0 :         trid = ctx->trid;
    8880         [ #  # ]:          0 :         memset(trid.subnqn, 0, sizeof(trid.subnqn));
    8881                 :          0 :         nvme_bdev_dump_trid_json(&trid, w);
    8882                 :            : 
    8883   [ #  #  #  #  :          0 :         spdk_json_write_named_bool(w, "wait_for_attach", ctx->wait_for_attach);
                   #  # ]
    8884   [ #  #  #  #  :          0 :         spdk_json_write_named_int32(w, "ctrlr_loss_timeout_sec", ctx->bdev_opts.ctrlr_loss_timeout_sec);
                   #  # ]
    8885   [ #  #  #  #  :          0 :         spdk_json_write_named_uint32(w, "reconnect_delay_sec", ctx->bdev_opts.reconnect_delay_sec);
                   #  # ]
    8886                 :          0 :         spdk_json_write_named_uint32(w, "fast_io_fail_timeout_sec",
    8887   [ #  #  #  #  :          0 :                                      ctx->bdev_opts.fast_io_fail_timeout_sec);
                   #  # ]
    8888                 :          0 :         spdk_json_write_object_end(w);
    8889                 :            : 
    8890                 :          0 :         spdk_json_write_object_end(w);
    8891                 :          0 : }
    8892                 :            : 
    8893                 :            : #ifdef SPDK_CONFIG_NVME_CUSE
    8894                 :            : static void
    8895                 :        118 : nvme_ctrlr_cuse_config_json(struct spdk_json_write_ctx *w,
    8896                 :            :                             struct nvme_ctrlr *nvme_ctrlr)
    8897                 :        118 : {
    8898                 :        118 :         size_t cuse_name_size = 128;
    8899         [ -  + ]:        118 :         char cuse_name[cuse_name_size];
    8900                 :            : 
    8901   [ +  -  #  #  :        118 :         if (spdk_nvme_cuse_get_ctrlr_name(nvme_ctrlr->ctrlr,
             #  #  #  # ]
    8902                 :          0 :                                           cuse_name, &cuse_name_size) != 0) {
    8903                 :        118 :                 return;
    8904                 :            :         }
    8905                 :            : 
    8906                 :          0 :         spdk_json_write_object_begin(w);
    8907                 :            : 
    8908                 :          0 :         spdk_json_write_named_string(w, "method", "bdev_nvme_cuse_register");
    8909                 :            : 
    8910                 :          0 :         spdk_json_write_named_object_begin(w, "params");
    8911   [ #  #  #  #  :          0 :         spdk_json_write_named_string(w, "name", nvme_ctrlr->nbdev_ctrlr->name);
             #  #  #  # ]
    8912                 :          0 :         spdk_json_write_object_end(w);
    8913                 :            : 
    8914                 :          0 :         spdk_json_write_object_end(w);
    8915         [ #  # ]:          0 : }
    8916                 :            : #endif
    8917                 :            : 
    8918                 :            : static void
    8919                 :        118 : nvme_ctrlr_config_json(struct spdk_json_write_ctx *w,
    8920                 :            :                        struct nvme_ctrlr *nvme_ctrlr,
    8921                 :            :                        struct nvme_path_id *path_id)
    8922                 :            : {
    8923                 :            :         struct spdk_nvme_transport_id   *trid;
    8924                 :            :         const struct spdk_nvme_ctrlr_opts *opts;
    8925                 :            : 
    8926   [ -  +  -  +  :        118 :         if (nvme_ctrlr->opts.from_discovery_service) {
          #  #  #  #  #  
                      # ]
    8927                 :            :                 /* Do not emit an RPC for this - it will be implicitly
    8928                 :            :                  * covered by a separate bdev_nvme_start_discovery or
    8929                 :            :                  * bdev_nvme_start_mdns_discovery RPC.
    8930                 :            :                  */
    8931                 :          0 :                 return;
    8932                 :            :         }
    8933                 :            : 
    8934         [ #  # ]:        118 :         trid = &path_id->trid;
    8935                 :            : 
    8936                 :        118 :         spdk_json_write_object_begin(w);
    8937                 :            : 
    8938                 :        118 :         spdk_json_write_named_string(w, "method", "bdev_nvme_attach_controller");
    8939                 :            : 
    8940                 :        118 :         spdk_json_write_named_object_begin(w, "params");
    8941   [ #  #  #  #  :        118 :         spdk_json_write_named_string(w, "name", nvme_ctrlr->nbdev_ctrlr->name);
             #  #  #  # ]
    8942                 :        118 :         nvme_bdev_dump_trid_json(trid, w);
    8943                 :        118 :         spdk_json_write_named_bool(w, "prchk_reftag",
    8944   [ #  #  #  #  :        118 :                                    (nvme_ctrlr->opts.prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_REFTAG) != 0);
             #  #  #  # ]
    8945                 :        118 :         spdk_json_write_named_bool(w, "prchk_guard",
    8946   [ #  #  #  #  :        118 :                                    (nvme_ctrlr->opts.prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_GUARD) != 0);
             #  #  #  # ]
    8947   [ #  #  #  #  :        118 :         spdk_json_write_named_int32(w, "ctrlr_loss_timeout_sec", nvme_ctrlr->opts.ctrlr_loss_timeout_sec);
                   #  # ]
    8948   [ #  #  #  #  :        118 :         spdk_json_write_named_uint32(w, "reconnect_delay_sec", nvme_ctrlr->opts.reconnect_delay_sec);
                   #  # ]
    8949                 :        118 :         spdk_json_write_named_uint32(w, "fast_io_fail_timeout_sec",
    8950   [ #  #  #  #  :          0 :                                      nvme_ctrlr->opts.fast_io_fail_timeout_sec);
                   #  # ]
    8951   [ +  +  #  #  :        118 :         if (nvme_ctrlr->psk != NULL) {
                   #  # ]
    8952   [ #  #  #  # ]:          9 :                 spdk_json_write_named_string(w, "psk", spdk_key_get_name(nvme_ctrlr->psk));
    8953                 :          0 :         }
    8954   [ -  +  #  #  :        118 :         if (nvme_ctrlr->dhchap_key != NULL) {
                   #  # ]
    8955                 :          0 :                 spdk_json_write_named_string(w, "dhchap_key",
    8956   [ #  #  #  # ]:          0 :                                              spdk_key_get_name(nvme_ctrlr->dhchap_key));
    8957                 :          0 :         }
    8958   [ -  +  #  #  :        118 :         if (nvme_ctrlr->dhchap_ctrlr_key != NULL) {
                   #  # ]
    8959                 :          0 :                 spdk_json_write_named_string(w, "dhchap_ctrlr_key",
    8960   [ #  #  #  # ]:          0 :                                              spdk_key_get_name(nvme_ctrlr->dhchap_ctrlr_key));
    8961                 :          0 :         }
    8962   [ #  #  #  # ]:        118 :         opts = spdk_nvme_ctrlr_get_opts(nvme_ctrlr->ctrlr);
    8963         [ #  # ]:        118 :         spdk_json_write_named_string(w, "hostnqn", opts->hostnqn);
    8964   [ -  +  #  #  :        118 :         spdk_json_write_named_bool(w, "hdgst", opts->header_digest);
                   #  # ]
    8965   [ -  +  #  #  :        118 :         spdk_json_write_named_bool(w, "ddgst", opts->data_digest);
                   #  # ]
    8966   [ -  +  #  #  :        118 :         if (opts->src_addr[0] != '\0') {
          #  #  #  #  #  
                      # ]
    8967         [ #  # ]:          0 :                 spdk_json_write_named_string(w, "hostaddr", opts->src_addr);
    8968                 :          0 :         }
    8969   [ -  +  #  #  :        118 :         if (opts->src_svcid[0] != '\0') {
          #  #  #  #  #  
                      # ]
    8970         [ #  # ]:          0 :                 spdk_json_write_named_string(w, "hostsvcid", opts->src_svcid);
    8971                 :          0 :         }
    8972                 :            : 
    8973   [ +  +  +  -  :        118 :         if (nvme_ctrlr->opts.multipath) {
          #  #  #  #  #  
                      # ]
    8974                 :        118 :                 spdk_json_write_named_string(w, "multipath", "multipath");
    8975                 :          0 :         }
    8976                 :        118 :         spdk_json_write_object_end(w);
    8977                 :            : 
    8978                 :        118 :         spdk_json_write_object_end(w);
    8979                 :          0 : }
    8980                 :            : 
    8981                 :            : static void
    8982                 :        165 : bdev_nvme_hotplug_config_json(struct spdk_json_write_ctx *w)
    8983                 :            : {
    8984                 :        165 :         spdk_json_write_object_begin(w);
    8985                 :        165 :         spdk_json_write_named_string(w, "method", "bdev_nvme_set_hotplug");
    8986                 :            : 
    8987                 :        165 :         spdk_json_write_named_object_begin(w, "params");
    8988                 :        165 :         spdk_json_write_named_uint64(w, "period_us", g_nvme_hotplug_poll_period_us);
    8989         [ +  + ]:        165 :         spdk_json_write_named_bool(w, "enable", g_nvme_hotplug_enabled);
    8990                 :        165 :         spdk_json_write_object_end(w);
    8991                 :            : 
    8992                 :        165 :         spdk_json_write_object_end(w);
    8993                 :        165 : }
    8994                 :            : 
    8995                 :            : static int
    8996                 :        165 : bdev_nvme_config_json(struct spdk_json_write_ctx *w)
    8997                 :            : {
    8998                 :            :         struct nvme_bdev_ctrlr  *nbdev_ctrlr;
    8999                 :            :         struct nvme_ctrlr       *nvme_ctrlr;
    9000                 :            :         struct discovery_ctx    *ctx;
    9001                 :            :         struct nvme_path_id     *path_id;
    9002                 :            : 
    9003                 :        165 :         bdev_nvme_opts_config_json(w);
    9004                 :            : 
    9005         [ +  + ]:        165 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    9006                 :            : 
    9007   [ +  +  #  #  :        283 :         TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
             #  #  #  # ]
    9008   [ +  +  #  #  :        236 :                 TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    9009   [ #  #  #  # ]:        118 :                         path_id = nvme_ctrlr->active_path_id;
    9010   [ -  +  #  #  :        118 :                         assert(path_id == TAILQ_FIRST(&nvme_ctrlr->trids));
          #  #  #  #  #  
                      # ]
    9011                 :        118 :                         nvme_ctrlr_config_json(w, nvme_ctrlr, path_id);
    9012                 :            : 
    9013   [ #  #  #  #  :        118 :                         path_id = TAILQ_NEXT(path_id, link);
                   #  # ]
    9014         [ -  + ]:        118 :                         while (path_id != NULL) {
    9015                 :          0 :                                 nvme_ctrlr_config_json(w, nvme_ctrlr, path_id);
    9016   [ #  #  #  #  :          0 :                                 path_id = TAILQ_NEXT(path_id, link);
                   #  # ]
    9017                 :            :                         }
    9018                 :            : 
    9019                 :            : #ifdef SPDK_CONFIG_NVME_CUSE
    9020                 :        118 :                         nvme_ctrlr_cuse_config_json(w, nvme_ctrlr);
    9021                 :            : #endif
    9022                 :          0 :                 }
    9023                 :          0 :         }
    9024                 :            : 
    9025   [ -  +  #  #  :        165 :         TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
             #  #  #  # ]
    9026   [ #  #  #  #  :          0 :                 if (!ctx->from_mdns_discovery_service) {
             #  #  #  # ]
    9027                 :          0 :                         bdev_nvme_discovery_config_json(w, ctx);
    9028                 :          0 :                 }
    9029                 :          0 :         }
    9030                 :            : 
    9031                 :        165 :         bdev_nvme_mdns_discovery_config_json(w);
    9032                 :            : 
    9033                 :            :         /* Dump as last parameter to give all NVMe bdevs chance to be constructed
    9034                 :            :          * before enabling hotplug poller.
    9035                 :            :          */
    9036                 :        165 :         bdev_nvme_hotplug_config_json(w);
    9037                 :            : 
    9038         [ +  + ]:        165 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    9039                 :        165 :         return 0;
    9040                 :            : }
    9041                 :            : 
    9042                 :            : struct spdk_nvme_ctrlr *
    9043                 :          9 : bdev_nvme_get_ctrlr(struct spdk_bdev *bdev)
    9044                 :            : {
    9045                 :            :         struct nvme_bdev *nbdev;
    9046                 :            :         struct nvme_ns *nvme_ns;
    9047                 :            : 
    9048   [ +  -  -  +  :          9 :         if (!bdev || bdev->module != &nvme_if) {
             #  #  #  # ]
    9049                 :          0 :                 return NULL;
    9050                 :            :         }
    9051                 :            : 
    9052                 :          9 :         nbdev = SPDK_CONTAINEROF(bdev, struct nvme_bdev, disk);
    9053   [ #  #  #  #  :          9 :         nvme_ns = TAILQ_FIRST(&nbdev->nvme_ns_list);
                   #  # ]
    9054   [ -  +  #  # ]:          9 :         assert(nvme_ns != NULL);
    9055                 :            : 
    9056   [ #  #  #  #  :          9 :         return nvme_ns->ctrlr->ctrlr;
             #  #  #  # ]
    9057                 :          0 : }
    9058                 :            : 
    9059                 :            : static bool
    9060                 :        516 : nvme_io_path_is_current(struct nvme_io_path *io_path)
    9061                 :            : {
    9062                 :            :         const struct nvme_bdev_channel *nbdev_ch;
    9063                 :            :         bool current;
    9064                 :            : 
    9065         [ +  + ]:        516 :         if (!nvme_io_path_is_available(io_path)) {
    9066                 :        132 :                 return false;
    9067                 :            :         }
    9068                 :            : 
    9069   [ #  #  #  # ]:        384 :         nbdev_ch = io_path->nbdev_ch;
    9070         [ +  + ]:        384 :         if (nbdev_ch == NULL) {
    9071                 :          3 :                 current = false;
    9072   [ +  +  #  #  :        381 :         } else if (nbdev_ch->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE) {
                   #  # ]
    9073                 :        177 :                 struct nvme_io_path *optimized_io_path = NULL;
    9074                 :            : 
    9075   [ +  +  #  #  :        378 :                 STAILQ_FOREACH(optimized_io_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    9076   [ +  +  #  #  :        303 :                         if (optimized_io_path->nvme_ns->ana_state == SPDK_NVME_ANA_OPTIMIZED_STATE) {
          #  #  #  #  #  
                      # ]
    9077                 :        102 :                                 break;
    9078                 :            :                         }
    9079                 :          0 :                 }
    9080                 :            : 
    9081                 :            :                 /* A non-optimized path is only current if there are no optimized paths. */
    9082   [ +  +  +  +  :        177 :                 current = (io_path->nvme_ns->ana_state == SPDK_NVME_ANA_OPTIMIZED_STATE) ||
          #  #  #  #  #  
                      # ]
    9083                 :          0 :                           (optimized_io_path == NULL);
    9084                 :          0 :         } else {
    9085   [ +  +  #  #  :        204 :                 if (nbdev_ch->current_io_path) {
                   #  # ]
    9086   [ #  #  #  # ]:        123 :                         current = (io_path == nbdev_ch->current_io_path);
    9087                 :          0 :                 } else {
    9088                 :            :                         struct nvme_io_path *first_path;
    9089                 :            : 
    9090                 :            :                         /* We arrived here as there are no optimized paths for active-passive
    9091                 :            :                          * mode. Check if this io_path is the first one available on the list.
    9092                 :            :                          */
    9093                 :         81 :                         current = false;
    9094   [ +  -  #  #  :         81 :                         STAILQ_FOREACH(first_path, &nbdev_ch->io_path_list, stailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    9095         [ +  - ]:         81 :                                 if (nvme_io_path_is_available(first_path)) {
    9096                 :         81 :                                         current = (io_path == first_path);
    9097                 :         81 :                                         break;
    9098                 :            :                                 }
    9099                 :          0 :                         }
    9100                 :            :                 }
    9101                 :            :         }
    9102                 :            : 
    9103         [ #  # ]:        384 :         return current;
    9104                 :          0 : }
    9105                 :            : 
    9106                 :            : static struct nvme_ctrlr *
    9107                 :         40 : bdev_nvme_next_ctrlr_unsafe(struct nvme_bdev_ctrlr *nbdev_ctrlr, struct nvme_ctrlr *prev)
    9108                 :            : {
    9109                 :            :         struct nvme_ctrlr *next;
    9110                 :            : 
    9111                 :            :         /* Must be called under g_bdev_nvme_mutex */
    9112   [ +  +  #  #  :         40 :         next = prev != NULL ? TAILQ_NEXT(prev, tailq) : TAILQ_FIRST(&nbdev_ctrlr->ctrlrs);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    9113         [ +  + ]:         40 :         while (next != NULL) {
    9114                 :            :                 /* ref can be 0 when the ctrlr was released, but hasn't been detached yet */
    9115   [ -  +  #  # ]:         28 :                 pthread_mutex_lock(&next->mutex);
    9116   [ +  -  #  #  :         28 :                 if (next->ref > 0) {
                   #  # ]
    9117   [ #  #  #  # ]:         28 :                         next->ref++;
    9118   [ -  +  #  # ]:         28 :                         pthread_mutex_unlock(&next->mutex);
    9119                 :         28 :                         return next;
    9120                 :            :                 }
    9121                 :            : 
    9122   [ #  #  #  # ]:          0 :                 pthread_mutex_unlock(&next->mutex);
    9123   [ #  #  #  #  :          0 :                 next = TAILQ_NEXT(next, tailq);
                   #  # ]
    9124                 :            :         }
    9125                 :            : 
    9126                 :         12 :         return NULL;
    9127                 :          0 : }
    9128                 :            : 
    9129                 :            : struct bdev_nvme_set_keys_ctx {
    9130                 :            :         struct nvme_ctrlr       *nctrlr;
    9131                 :            :         struct spdk_key         *dhchap_key;
    9132                 :            :         struct spdk_key         *dhchap_ctrlr_key;
    9133                 :            :         struct spdk_thread      *thread;
    9134                 :            :         bdev_nvme_set_keys_cb   cb_fn;
    9135                 :            :         void                    *cb_ctx;
    9136                 :            :         int                     status;
    9137                 :            : };
    9138                 :            : 
    9139                 :            : static void
    9140                 :         28 : bdev_nvme_free_set_keys_ctx(struct bdev_nvme_set_keys_ctx *ctx)
    9141                 :            : {
    9142         [ -  + ]:         28 :         if (ctx == NULL) {
    9143                 :          0 :                 return;
    9144                 :            :         }
    9145                 :            : 
    9146   [ #  #  #  # ]:         28 :         spdk_keyring_put_key(ctx->dhchap_key);
    9147   [ #  #  #  # ]:         28 :         spdk_keyring_put_key(ctx->dhchap_ctrlr_key);
    9148                 :         28 :         free(ctx);
    9149                 :          0 : }
    9150                 :            : 
    9151                 :            : static void
    9152                 :         28 : _bdev_nvme_set_keys_done(void *_ctx)
    9153                 :            : {
    9154                 :         28 :         struct bdev_nvme_set_keys_ctx *ctx = _ctx;
    9155                 :            : 
    9156   [ #  #  #  #  :         28 :         ctx->cb_fn(ctx->cb_ctx, ctx->status);
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    9157                 :            : 
    9158   [ +  +  #  #  :         28 :         if (ctx->nctrlr != NULL) {
                   #  # ]
    9159   [ #  #  #  # ]:         16 :                 nvme_ctrlr_put_ref(ctx->nctrlr);
    9160                 :          0 :         }
    9161                 :         28 :         bdev_nvme_free_set_keys_ctx(ctx);
    9162                 :         28 : }
    9163                 :            : 
    9164                 :            : static void
    9165                 :         28 : bdev_nvme_set_keys_done(struct bdev_nvme_set_keys_ctx *ctx, int status)
    9166                 :            : {
    9167   [ #  #  #  # ]:         28 :         ctx->status = status;
    9168   [ #  #  #  # ]:         28 :         spdk_thread_exec_msg(ctx->thread, _bdev_nvme_set_keys_done, ctx);
    9169                 :         28 : }
    9170                 :            : 
    9171                 :            : static void bdev_nvme_authenticate_ctrlr(struct bdev_nvme_set_keys_ctx *ctx);
    9172                 :            : 
    9173                 :            : static void
    9174                 :         12 : bdev_nvme_authenticate_ctrlr_continue(struct bdev_nvme_set_keys_ctx *ctx)
    9175                 :            : {
    9176                 :            :         struct nvme_ctrlr *next;
    9177                 :            : 
    9178         [ -  + ]:         12 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    9179   [ #  #  #  # ]:         12 :         next = bdev_nvme_next_ctrlr_unsafe(NULL, ctx->nctrlr);
    9180         [ -  + ]:         12 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    9181                 :            : 
    9182   [ #  #  #  # ]:         12 :         nvme_ctrlr_put_ref(ctx->nctrlr);
    9183   [ #  #  #  # ]:         12 :         ctx->nctrlr = next;
    9184                 :            : 
    9185         [ +  - ]:         12 :         if (next == NULL) {
    9186                 :         12 :                 bdev_nvme_set_keys_done(ctx, 0);
    9187                 :          0 :         } else {
    9188                 :          0 :                 bdev_nvme_authenticate_ctrlr(ctx);
    9189                 :            :         }
    9190                 :         12 : }
    9191                 :            : 
    9192                 :            : static void
    9193                 :          8 : bdev_nvme_authenticate_qpairs_done(struct spdk_io_channel_iter *i, int status)
    9194                 :            : {
    9195                 :          8 :         struct bdev_nvme_set_keys_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    9196                 :            : 
    9197         [ -  + ]:          8 :         if (status != 0) {
    9198                 :          0 :                 bdev_nvme_set_keys_done(ctx, status);
    9199                 :          0 :                 return;
    9200                 :            :         }
    9201                 :          8 :         bdev_nvme_authenticate_ctrlr_continue(ctx);
    9202                 :          0 : }
    9203                 :            : 
    9204                 :            : static void
    9205                 :          0 : bdev_nvme_authenticate_qpair_done(void *ctx, int status)
    9206                 :            : {
    9207                 :          0 :         spdk_for_each_channel_continue(ctx, status);
    9208                 :          0 : }
    9209                 :            : 
    9210                 :            : static void
    9211                 :          0 : bdev_nvme_authenticate_qpair(struct spdk_io_channel_iter *i)
    9212                 :            : {
    9213                 :          0 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
    9214                 :          0 :         struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch);
    9215   [ #  #  #  # ]:          0 :         struct nvme_qpair *qpair = ctrlr_ch->qpair;
    9216                 :            :         int rc;
    9217                 :            : 
    9218         [ #  # ]:          0 :         if (!nvme_qpair_is_connected(qpair)) {
    9219                 :          0 :                 spdk_for_each_channel_continue(i, 0);
    9220                 :          0 :                 return;
    9221                 :            :         }
    9222                 :            : 
    9223   [ #  #  #  # ]:          0 :         rc = spdk_nvme_qpair_authenticate(qpair->qpair, bdev_nvme_authenticate_qpair_done, i);
    9224         [ #  # ]:          0 :         if (rc != 0) {
    9225                 :          0 :                 spdk_for_each_channel_continue(i, rc);
    9226                 :          0 :         }
    9227                 :          0 : }
    9228                 :            : 
    9229                 :            : static void
    9230                 :         20 : bdev_nvme_authenticate_ctrlr_done(void *_ctx, int status)
    9231                 :            : {
    9232                 :         20 :         struct bdev_nvme_set_keys_ctx *ctx = _ctx;
    9233                 :            : 
    9234         [ +  + ]:         20 :         if (status != 0) {
    9235                 :         12 :                 bdev_nvme_set_keys_done(ctx, status);
    9236                 :         12 :                 return;
    9237                 :            :         }
    9238                 :            : 
    9239   [ #  #  #  # ]:          8 :         spdk_for_each_channel(ctx->nctrlr, bdev_nvme_authenticate_qpair, ctx,
    9240                 :            :                               bdev_nvme_authenticate_qpairs_done);
    9241                 :          0 : }
    9242                 :            : 
    9243                 :            : static void
    9244                 :         28 : bdev_nvme_authenticate_ctrlr(struct bdev_nvme_set_keys_ctx *ctx)
    9245                 :            : {
    9246                 :         28 :         struct spdk_nvme_ctrlr_key_opts opts = {};
    9247   [ #  #  #  # ]:         28 :         struct nvme_ctrlr *nctrlr = ctx->nctrlr;
    9248                 :            :         int rc;
    9249                 :            : 
    9250                 :         28 :         opts.size = SPDK_SIZEOF(&opts, dhchap_ctrlr_key);
    9251   [ #  #  #  #  :         28 :         opts.dhchap_key = ctx->dhchap_key;
                   #  # ]
    9252   [ #  #  #  #  :         28 :         opts.dhchap_ctrlr_key = ctx->dhchap_ctrlr_key;
                   #  # ]
    9253   [ #  #  #  # ]:         28 :         rc = spdk_nvme_ctrlr_set_keys(nctrlr->ctrlr, &opts);
    9254         [ -  + ]:         28 :         if (rc != 0) {
    9255                 :          0 :                 bdev_nvme_set_keys_done(ctx, rc);
    9256                 :          0 :                 return;
    9257                 :            :         }
    9258                 :            : 
    9259   [ +  +  #  #  :         28 :         if (ctx->dhchap_key != NULL) {
                   #  # ]
    9260   [ #  #  #  # ]:         24 :                 rc = spdk_nvme_ctrlr_authenticate(nctrlr->ctrlr,
    9261                 :          0 :                                                   bdev_nvme_authenticate_ctrlr_done, ctx);
    9262         [ +  + ]:         24 :                 if (rc != 0) {
    9263                 :          4 :                         bdev_nvme_set_keys_done(ctx, rc);
    9264                 :          0 :                 }
    9265                 :          0 :         } else {
    9266                 :          4 :                 bdev_nvme_authenticate_ctrlr_continue(ctx);
    9267                 :            :         }
    9268                 :          0 : }
    9269                 :            : 
    9270                 :            : int
    9271                 :         28 : bdev_nvme_set_keys(const char *name, const char *dhchap_key, const char *dhchap_ctrlr_key,
    9272                 :            :                    bdev_nvme_set_keys_cb cb_fn, void *cb_ctx)
    9273                 :            : {
    9274                 :            :         struct bdev_nvme_set_keys_ctx *ctx;
    9275                 :            :         struct nvme_bdev_ctrlr *nbdev_ctrlr;
    9276                 :            :         struct nvme_ctrlr *nctrlr;
    9277                 :            : 
    9278                 :         28 :         ctx = calloc(1, sizeof(*ctx));
    9279         [ -  + ]:         28 :         if (ctx == NULL) {
    9280                 :          0 :                 return -ENOMEM;
    9281                 :            :         }
    9282                 :            : 
    9283         [ +  + ]:         28 :         if (dhchap_key != NULL) {
    9284   [ #  #  #  # ]:         24 :                 ctx->dhchap_key = spdk_keyring_get_key(dhchap_key);
    9285   [ -  +  #  #  :         24 :                 if (ctx->dhchap_key == NULL) {
                   #  # ]
    9286                 :          0 :                         SPDK_ERRLOG("Could not find key %s for bdev %s\n", dhchap_key, name);
    9287                 :          0 :                         bdev_nvme_free_set_keys_ctx(ctx);
    9288                 :          0 :                         return -ENOKEY;
    9289                 :            :                 }
    9290                 :          0 :         }
    9291         [ +  + ]:         28 :         if (dhchap_ctrlr_key != NULL) {
    9292   [ #  #  #  # ]:         24 :                 ctx->dhchap_ctrlr_key = spdk_keyring_get_key(dhchap_ctrlr_key);
    9293   [ -  +  #  #  :         24 :                 if (ctx->dhchap_ctrlr_key == NULL) {
                   #  # ]
    9294                 :          0 :                         SPDK_ERRLOG("Could not find key %s for bdev %s\n", dhchap_ctrlr_key, name);
    9295                 :          0 :                         bdev_nvme_free_set_keys_ctx(ctx);
    9296                 :          0 :                         return -ENOKEY;
    9297                 :            :                 }
    9298                 :          0 :         }
    9299                 :            : 
    9300         [ -  + ]:         28 :         pthread_mutex_lock(&g_bdev_nvme_mutex);
    9301                 :         28 :         nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
    9302         [ -  + ]:         28 :         if (nbdev_ctrlr == NULL) {
    9303                 :          0 :                 SPDK_ERRLOG("Could not find bdev_ctrlr %s\n", name);
    9304         [ #  # ]:          0 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
    9305                 :          0 :                 bdev_nvme_free_set_keys_ctx(ctx);
    9306                 :          0 :                 return -ENODEV;
    9307                 :            :         }
    9308                 :         28 :         nctrlr = bdev_nvme_next_ctrlr_unsafe(nbdev_ctrlr, NULL);
    9309         [ -  + ]:         28 :         if (nctrlr == NULL) {
    9310                 :          0 :                 SPDK_ERRLOG("Could not find any nvme_ctrlrs on bdev_ctrlr %s\n", name);
    9311         [ #  # ]:          0 :                 pthread_mutex_unlock(&g_bdev_nvme_mutex);
    9312                 :          0 :                 bdev_nvme_free_set_keys_ctx(ctx);
    9313                 :          0 :                 return -ENODEV;
    9314                 :            :         }
    9315         [ -  + ]:         28 :         pthread_mutex_unlock(&g_bdev_nvme_mutex);
    9316                 :            : 
    9317   [ #  #  #  # ]:         28 :         ctx->nctrlr = nctrlr;
    9318   [ #  #  #  # ]:         28 :         ctx->cb_fn = cb_fn;
    9319   [ #  #  #  # ]:         28 :         ctx->cb_ctx = cb_ctx;
    9320   [ #  #  #  # ]:         28 :         ctx->thread = spdk_get_thread();
    9321                 :            : 
    9322                 :         28 :         bdev_nvme_authenticate_ctrlr(ctx);
    9323                 :            : 
    9324                 :         28 :         return 0;
    9325                 :          0 : }
    9326                 :            : 
    9327                 :            : void
    9328                 :        480 : nvme_io_path_info_json(struct spdk_json_write_ctx *w, struct nvme_io_path *io_path)
    9329                 :            : {
    9330   [ #  #  #  # ]:        480 :         struct nvme_ns *nvme_ns = io_path->nvme_ns;
    9331   [ #  #  #  #  :        480 :         struct nvme_ctrlr *nvme_ctrlr = io_path->qpair->ctrlr;
             #  #  #  # ]
    9332                 :            :         const struct spdk_nvme_ctrlr_data *cdata;
    9333                 :            :         const struct spdk_nvme_transport_id *trid;
    9334                 :            :         const char *adrfam_str;
    9335                 :            : 
    9336                 :        480 :         spdk_json_write_object_begin(w);
    9337                 :            : 
    9338   [ #  #  #  #  :        480 :         spdk_json_write_named_string(w, "bdev_name", nvme_ns->bdev->disk.name);
          #  #  #  #  #  
                      # ]
    9339                 :            : 
    9340   [ #  #  #  # ]:        480 :         cdata = spdk_nvme_ctrlr_get_data(nvme_ctrlr->ctrlr);
    9341   [ #  #  #  # ]:        480 :         trid = spdk_nvme_ctrlr_get_transport_id(nvme_ctrlr->ctrlr);
    9342                 :            : 
    9343   [ #  #  #  # ]:        480 :         spdk_json_write_named_uint32(w, "cntlid", cdata->cntlid);
    9344                 :        480 :         spdk_json_write_named_bool(w, "current", nvme_io_path_is_current(io_path));
    9345   [ #  #  #  # ]:        480 :         spdk_json_write_named_bool(w, "connected", nvme_qpair_is_connected(io_path->qpair));
    9346                 :        480 :         spdk_json_write_named_bool(w, "accessible", nvme_ns_is_accessible(nvme_ns));
    9347                 :            : 
    9348                 :        480 :         spdk_json_write_named_object_begin(w, "transport");
    9349         [ #  # ]:        480 :         spdk_json_write_named_string(w, "trtype", trid->trstring);
    9350         [ #  # ]:        480 :         spdk_json_write_named_string(w, "traddr", trid->traddr);
    9351   [ +  -  #  #  :        480 :         if (trid->trsvcid[0] != '\0') {
          #  #  #  #  #  
                      # ]
    9352         [ #  # ]:        480 :                 spdk_json_write_named_string(w, "trsvcid", trid->trsvcid);
    9353                 :          0 :         }
    9354   [ #  #  #  # ]:        480 :         adrfam_str = spdk_nvme_transport_id_adrfam_str(trid->adrfam);
    9355         [ +  - ]:        480 :         if (adrfam_str) {
    9356                 :        480 :                 spdk_json_write_named_string(w, "adrfam", adrfam_str);
    9357                 :          0 :         }
    9358                 :        480 :         spdk_json_write_object_end(w);
    9359                 :            : 
    9360                 :        480 :         spdk_json_write_object_end(w);
    9361                 :        480 : }
    9362                 :            : 
    9363                 :            : void
    9364                 :         77 : bdev_nvme_get_discovery_info(struct spdk_json_write_ctx *w)
    9365                 :            : {
    9366                 :            :         struct discovery_ctx *ctx;
    9367                 :            :         struct discovery_entry_ctx *entry_ctx;
    9368                 :            : 
    9369                 :         77 :         spdk_json_write_array_begin(w);
    9370   [ +  +  #  #  :        150 :         TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
             #  #  #  # ]
    9371                 :         73 :                 spdk_json_write_object_begin(w);
    9372   [ #  #  #  # ]:         73 :                 spdk_json_write_named_string(w, "name", ctx->name);
    9373                 :            : 
    9374                 :         73 :                 spdk_json_write_named_object_begin(w, "trid");
    9375         [ #  # ]:         73 :                 nvme_bdev_dump_trid_json(&ctx->trid, w);
    9376                 :         73 :                 spdk_json_write_object_end(w);
    9377                 :            : 
    9378                 :         73 :                 spdk_json_write_named_array_begin(w, "referrals");
    9379   [ +  +  #  #  :        171 :                 TAILQ_FOREACH(entry_ctx, &ctx->discovery_entry_ctxs, tailq) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    9380                 :         98 :                         spdk_json_write_object_begin(w);
    9381                 :         98 :                         spdk_json_write_named_object_begin(w, "trid");
    9382         [ #  # ]:         98 :                         nvme_bdev_dump_trid_json(&entry_ctx->trid, w);
    9383                 :         98 :                         spdk_json_write_object_end(w);
    9384                 :         98 :                         spdk_json_write_object_end(w);
    9385                 :          0 :                 }
    9386                 :         73 :                 spdk_json_write_array_end(w);
    9387                 :            : 
    9388                 :         73 :                 spdk_json_write_object_end(w);
    9389                 :          0 :         }
    9390                 :         77 :         spdk_json_write_array_end(w);
    9391                 :         77 : }
    9392                 :            : 
    9393                 :       2137 : SPDK_LOG_REGISTER_COMPONENT(bdev_nvme)
    9394                 :            : 
    9395                 :            : static void
    9396                 :       1974 : bdev_nvme_trace(void)
    9397                 :            : {
    9398                 :       1974 :         struct spdk_trace_tpoint_opts opts[] = {
    9399                 :            :                 {
    9400                 :            :                         "BDEV_NVME_IO_START", TRACE_BDEV_NVME_IO_START,
    9401                 :            :                         OWNER_TYPE_NONE, OBJECT_BDEV_NVME_IO, 1,
    9402                 :            :                         {{ "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 }}
    9403                 :            :                 },
    9404                 :            :                 {
    9405                 :            :                         "BDEV_NVME_IO_DONE", TRACE_BDEV_NVME_IO_DONE,
    9406                 :            :                         OWNER_TYPE_NONE, OBJECT_BDEV_NVME_IO, 0,
    9407                 :            :                         {{ "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 }}
    9408                 :            :                 }
    9409                 :            :         };
    9410                 :            : 
    9411                 :            : 
    9412                 :       1974 :         spdk_trace_register_object(OBJECT_BDEV_NVME_IO, 'N');
    9413                 :       1974 :         spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts));
    9414                 :       1974 :         spdk_trace_tpoint_register_relation(TRACE_NVME_PCIE_SUBMIT, OBJECT_BDEV_NVME_IO, 0);
    9415                 :       1974 :         spdk_trace_tpoint_register_relation(TRACE_NVME_TCP_SUBMIT, OBJECT_BDEV_NVME_IO, 0);
    9416                 :       1974 :         spdk_trace_tpoint_register_relation(TRACE_NVME_PCIE_COMPLETE, OBJECT_BDEV_NVME_IO, 0);
    9417                 :       1974 :         spdk_trace_tpoint_register_relation(TRACE_NVME_TCP_COMPLETE, OBJECT_BDEV_NVME_IO, 0);
    9418                 :       1974 : }
    9419                 :       2137 : SPDK_TRACE_REGISTER_FN(bdev_nvme_trace, "bdev_nvme", TRACE_GROUP_BDEV_NVME)

Generated by: LCOV version 1.15