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 : 2046 : nvme_ns_cmp(struct nvme_ns *ns1, struct nvme_ns *ns2)
261 : : {
262 [ + + + - : 2046 : return ns1->id < ns2->id ? -1 : ns1->id > ns2->id;
+ - + - -
+ + - + -
+ - + - ]
263 : : }
264 : :
265 [ + + + + : 22813 : RB_GENERATE_STATIC(nvme_ns_tree, nvme_ns, node, nvme_ns_cmp);
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + - +
- + + - -
- - + # #
# # # # -
+ + + + -
+ + - + #
# # # # #
# # # # #
# # # # #
# # # # +
- + + + +
+ + + - +
+ + + - +
+ + + + +
+ + + + -
# # + - +
- + - + -
+ - + + +
- + - + +
+ - + - +
- - + # #
# # - + +
+ + - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # +
- + - + -
- + + - +
- # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # -
+ # # # #
# # # # #
# # # - +
- + # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
+ - + - -
+ # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
- + + - +
- # # # #
# # # # #
# # # # #
- + + - +
- + - + -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
266 : :
267 : : struct spdk_nvme_qpair *
268 : 4 : bdev_nvme_get_io_qpair(struct spdk_io_channel *ctrlr_io_ch)
269 : : {
270 : : struct nvme_ctrlr_channel *ctrlr_ch;
271 : :
272 [ + + # # ]: 4 : assert(ctrlr_io_ch != NULL);
273 : :
274 : 4 : ctrlr_ch = spdk_io_channel_get_ctx(ctrlr_io_ch);
275 : :
276 [ # # # # : 4 : return ctrlr_ch->qpair->qpair;
# # # # ]
277 : : }
278 : :
279 : : static int
280 : 1960 : bdev_nvme_get_ctx_size(void)
281 : : {
282 : 1960 : 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 : 2149 : 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 : 44584 : nvme_bdev_ctrlr_get_by_name(const char *name)
302 : : {
303 : : struct nvme_bdev_ctrlr *nbdev_ctrlr;
304 : :
305 [ + + # # : 46497 : TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
# # # # ]
306 [ + + + + : 39153 : if (strcmp(name, nbdev_ctrlr->name) == 0) {
+ + + - -
+ ]
307 : 37240 : break;
308 : : }
309 : 0 : }
310 : :
311 : 44584 : return nbdev_ctrlr;
312 : : }
313 : :
314 : : static struct nvme_ctrlr *
315 : 731 : 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 [ + + # # : 1390 : TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
# # # # #
# # # #
# ]
322 [ # # # # ]: 795 : opts = spdk_nvme_ctrlr_get_opts(nvme_ctrlr->ctrlr);
323 [ + + + - : 795 : if (spdk_nvme_transport_id_compare(trid, &nvme_ctrlr->active_path_id->trid) == 0 &&
# # # # #
# ]
324 [ + + - + : 139 : strcmp(hostnqn, opts->hostnqn) == 0) {
+ + ]
325 : 136 : break;
326 : : }
327 : 41 : }
328 : :
329 : 731 : 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 : 1643 : nvme_bdev_ctrlr_get_bdev(struct nvme_bdev_ctrlr *nbdev_ctrlr, uint32_t nsid)
351 : : {
352 : : struct nvme_bdev *nbdev;
353 : :
354 [ + + ]: 1643 : pthread_mutex_lock(&g_bdev_nvme_mutex);
355 [ + + + - : 1941 : TAILQ_FOREACH(nbdev, &nbdev_ctrlr->bdevs, tailq) {
+ - + - #
# # # #
# ]
356 [ + + # # : 456 : if (nbdev->nsid == nsid) {
# # ]
357 : 158 : break;
358 : : }
359 : 34 : }
360 [ + + ]: 1643 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
361 : :
362 : 1643 : return nbdev;
363 : : }
364 : :
365 : : struct nvme_ns *
366 : 3366 : nvme_ctrlr_get_ns(struct nvme_ctrlr *nvme_ctrlr, uint32_t nsid)
367 : : {
368 : 1142 : struct nvme_ns ns;
369 : :
370 [ + + # # ]: 3366 : assert(nsid > 0);
371 : :
372 : 3366 : ns.id = nsid;
373 [ + - ]: 3366 : return RB_FIND(nvme_ns_tree, &nvme_ctrlr->namespaces, &ns);
374 : : }
375 : :
376 : : struct nvme_ns *
377 : 3726 : nvme_ctrlr_get_first_active_ns(struct nvme_ctrlr *nvme_ctrlr)
378 : : {
379 [ + - ]: 3726 : return RB_MIN(nvme_ns_tree, &nvme_ctrlr->namespaces);
380 : : }
381 : :
382 : : struct nvme_ns *
383 : 1644 : nvme_ctrlr_get_next_active_ns(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *ns)
384 : : {
385 [ - + ]: 1644 : if (ns == NULL) {
386 : 0 : return NULL;
387 : : }
388 : :
389 : 1644 : return RB_NEXT(nvme_ns_tree, &nvme_ctrlr->namespaces, ns);
390 : 87 : }
391 : :
392 : : static struct nvme_ctrlr *
393 : 1756 : nvme_ctrlr_get(const struct spdk_nvme_transport_id *trid, const char *hostnqn)
394 : : {
395 : : struct nvme_bdev_ctrlr *nbdev_ctrlr;
396 : 1756 : struct nvme_ctrlr *nvme_ctrlr = NULL;
397 : :
398 [ + + ]: 1756 : pthread_mutex_lock(&g_bdev_nvme_mutex);
399 [ + + # # : 2327 : TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
# # # # ]
400 : 571 : nvme_ctrlr = nvme_bdev_ctrlr_get_ctrlr(nbdev_ctrlr, trid, hostnqn);
401 [ - + ]: 571 : if (nvme_ctrlr != NULL) {
402 : 0 : break;
403 : : }
404 : 19 : }
405 [ + + ]: 1756 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
406 : :
407 : 1756 : return nvme_ctrlr;
408 : : }
409 : :
410 : : struct nvme_ctrlr *
411 : 3603 : nvme_ctrlr_get_by_name(const char *name)
412 : : {
413 : : struct nvme_bdev_ctrlr *nbdev_ctrlr;
414 : 3603 : struct nvme_ctrlr *nvme_ctrlr = NULL;
415 : :
416 [ + + ]: 3603 : if (name == NULL) {
417 : 0 : return NULL;
418 : : }
419 : :
420 [ + + ]: 3603 : pthread_mutex_lock(&g_bdev_nvme_mutex);
421 : 3603 : nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
422 [ + + ]: 3603 : if (nbdev_ctrlr != NULL) {
423 [ + - + - : 347 : nvme_ctrlr = TAILQ_FIRST(&nbdev_ctrlr->ctrlrs);
+ - ]
424 : 63 : }
425 [ + + ]: 3603 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
426 : :
427 : 3603 : return nvme_ctrlr;
428 : 151 : }
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 : 1 : }
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 : 2585 : nvme_ctrlr_for_each_channel_continue(struct nvme_ctrlr_channel_iter *iter, int status)
451 : : {
452 [ # # # # ]: 2585 : spdk_for_each_channel_continue(iter->i, status);
453 : 2585 : }
454 : :
455 : : static void
456 : 2585 : nvme_ctrlr_each_channel_msg(struct spdk_io_channel_iter *i)
457 : : {
458 : 2585 : struct nvme_ctrlr_channel_iter *iter = spdk_io_channel_iter_get_ctx(i);
459 : 2585 : struct nvme_ctrlr *nvme_ctrlr = spdk_io_channel_iter_get_io_device(i);
460 : 2585 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
461 : 2585 : struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch);
462 : :
463 [ # # # # ]: 2585 : iter->i = i;
464 [ # # # # : 2585 : iter->fn(iter, nvme_ctrlr, ctrlr_ch, iter->ctx);
# # # # #
# # # ]
465 : 2585 : }
466 : :
467 : : static void
468 : 2443 : nvme_ctrlr_each_channel_cpl(struct spdk_io_channel_iter *i, int status)
469 : : {
470 : 2443 : struct nvme_ctrlr_channel_iter *iter = spdk_io_channel_iter_get_ctx(i);
471 : 2443 : struct nvme_ctrlr *nvme_ctrlr = spdk_io_channel_iter_get_io_device(i);
472 : :
473 [ # # # # ]: 2443 : iter->i = i;
474 [ # # # # : 2443 : iter->cpl(nvme_ctrlr, iter->ctx, status);
# # # # #
# # # ]
475 : :
476 : 2443 : free(iter);
477 : 2443 : }
478 : :
479 : : void
480 : 2443 : 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 [ + - + - ]: 2443 : assert(nvme_ctrlr != NULL && fn != NULL);
487 : :
488 : 2443 : iter = calloc(1, sizeof(struct nvme_ctrlr_channel_iter));
489 [ - + ]: 2443 : if (iter == NULL) {
490 : 0 : SPDK_ERRLOG("Unable to allocate iterator\n");
491 [ # # ]: 0 : assert(false);
492 : : return;
493 : : }
494 : :
495 [ # # # # ]: 2443 : iter->fn = fn;
496 [ # # # # ]: 2443 : iter->cpl = cpl;
497 [ # # # # ]: 2443 : iter->ctx = ctx;
498 : :
499 : 2544 : spdk_for_each_channel(nvme_ctrlr, nvme_ctrlr_each_channel_msg,
500 : 101 : iter, nvme_ctrlr_each_channel_cpl);
501 : 101 : }
502 : :
503 : : struct nvme_bdev_channel_iter {
504 : : nvme_bdev_for_each_channel_msg fn;
505 : : nvme_bdev_for_each_channel_done cpl;
506 : : struct spdk_io_channel_iter *i;
507 : : void *ctx;
508 : : };
509 : :
510 : : void
511 : 360 : nvme_bdev_for_each_channel_continue(struct nvme_bdev_channel_iter *iter, int status)
512 : : {
513 [ # # # # ]: 360 : spdk_for_each_channel_continue(iter->i, status);
514 : 360 : }
515 : :
516 : : static void
517 : 360 : nvme_bdev_each_channel_msg(struct spdk_io_channel_iter *i)
518 : : {
519 : 360 : struct nvme_bdev_channel_iter *iter = spdk_io_channel_iter_get_ctx(i);
520 : 360 : struct nvme_bdev *nbdev = spdk_io_channel_iter_get_io_device(i);
521 : 360 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
522 : 360 : struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(ch);
523 : :
524 [ # # # # ]: 360 : iter->i = i;
525 [ # # # # : 360 : iter->fn(iter, nbdev, nbdev_ch, iter->ctx);
# # # # #
# # # ]
526 : 360 : }
527 : :
528 : : static void
529 : 354 : nvme_bdev_each_channel_cpl(struct spdk_io_channel_iter *i, int status)
530 : : {
531 : 354 : struct nvme_bdev_channel_iter *iter = spdk_io_channel_iter_get_ctx(i);
532 : 354 : struct nvme_bdev *nbdev = spdk_io_channel_iter_get_io_device(i);
533 : :
534 [ # # # # ]: 354 : iter->i = i;
535 [ # # # # : 354 : iter->cpl(nbdev, iter->ctx, status);
# # # # #
# # # ]
536 : :
537 : 354 : free(iter);
538 : 354 : }
539 : :
540 : : void
541 : 354 : 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 [ + - + - ]: 354 : assert(nbdev != NULL && fn != NULL);
548 : :
549 : 354 : iter = calloc(1, sizeof(struct nvme_bdev_channel_iter));
550 [ - + ]: 354 : if (iter == NULL) {
551 : 0 : SPDK_ERRLOG("Unable to allocate iterator\n");
552 [ # # ]: 0 : assert(false);
553 : : return;
554 : : }
555 : :
556 [ # # # # ]: 354 : iter->fn = fn;
557 [ # # # # ]: 354 : iter->cpl = cpl;
558 [ # # # # ]: 354 : iter->ctx = ctx;
559 : :
560 : 354 : spdk_for_each_channel(nbdev, nvme_bdev_each_channel_msg, iter,
561 : : nvme_bdev_each_channel_cpl);
562 : 62 : }
563 : :
564 : : void
565 : 2180 : 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 [ # # # # ]: 2180 : trtype_str = spdk_nvme_transport_id_trtype_str(trid->trtype);
571 [ + - ]: 2180 : if (trtype_str) {
572 : 2180 : spdk_json_write_named_string(w, "trtype", trtype_str);
573 : 8 : }
574 : :
575 [ # # # # ]: 2180 : adrfam_str = spdk_nvme_transport_id_adrfam_str(trid->adrfam);
576 [ + + ]: 2180 : if (adrfam_str) {
577 : 1161 : spdk_json_write_named_string(w, "adrfam", adrfam_str);
578 : 0 : }
579 : :
580 [ + - # # : 2180 : if (trid->traddr[0] != '\0') {
# # # # #
# ]
581 [ # # ]: 2180 : spdk_json_write_named_string(w, "traddr", trid->traddr);
582 : 8 : }
583 : :
584 [ + + # # : 2180 : if (trid->trsvcid[0] != '\0') {
# # # # #
# ]
585 [ # # ]: 1161 : spdk_json_write_named_string(w, "trsvcid", trid->trsvcid);
586 : 0 : }
587 : :
588 [ + + # # : 2180 : if (trid->subnqn[0] != '\0') {
# # # # #
# ]
589 [ # # ]: 1161 : spdk_json_write_named_string(w, "subnqn", trid->subnqn);
590 : 0 : }
591 : 2180 : }
592 : :
593 : : static void
594 : 1764 : nvme_bdev_ctrlr_delete(struct nvme_bdev_ctrlr *nbdev_ctrlr,
595 : : struct nvme_ctrlr *nvme_ctrlr)
596 : : {
597 : 514 : SPDK_DTRACE_PROBE1(bdev_nvme_ctrlr_delete, nvme_ctrlr->nbdev_ctrlr->name);
598 [ + + ]: 1764 : pthread_mutex_lock(&g_bdev_nvme_mutex);
599 : :
600 [ + + + - : 1764 : TAILQ_REMOVE(&nbdev_ctrlr->ctrlrs, nvme_ctrlr, tailq);
+ - - + #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
601 [ + + + - : 1764 : if (!TAILQ_EMPTY(&nbdev_ctrlr->ctrlrs)) {
+ - - + ]
602 [ - + ]: 73 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
603 : :
604 : 73 : return;
605 : : }
606 [ + + + - : 1691 : TAILQ_REMOVE(&g_nvme_bdev_ctrlrs, nbdev_ctrlr, tailq);
+ - - + #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
607 : :
608 [ + + ]: 1691 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
609 : :
610 [ + + + - : 1691 : assert(TAILQ_EMPTY(&nbdev_ctrlr->bdevs));
+ - + - #
# ]
611 : :
612 [ + - + - ]: 1691 : free(nbdev_ctrlr->name);
613 : 1691 : free(nbdev_ctrlr);
614 : 72 : }
615 : :
616 : : static void
617 : 1768 : _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 [ + - + - ]: 1768 : free(nvme_ctrlr->copied_ana_desc);
623 [ + - + - ]: 1768 : spdk_free(nvme_ctrlr->ana_log_page);
624 : :
625 [ + + + - : 1768 : if (nvme_ctrlr->opal_dev) {
+ - ]
626 [ # # # # ]: 25 : spdk_opal_dev_destruct(nvme_ctrlr->opal_dev);
627 [ # # # # ]: 25 : nvme_ctrlr->opal_dev = NULL;
628 : 0 : }
629 : :
630 [ + + + - : 1768 : if (nvme_ctrlr->nbdev_ctrlr) {
- + ]
631 [ + - + - ]: 1764 : nvme_bdev_ctrlr_delete(nvme_ctrlr->nbdev_ctrlr, nvme_ctrlr);
632 : 72 : }
633 : :
634 [ + + + + : 1768 : 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 [ + + + + : 3544 : TAILQ_FOREACH_SAFE(path_id, &nvme_ctrlr->trids, link, tmp_path) {
+ - + + +
- + - + -
+ + ]
640 [ + + + - : 1776 : TAILQ_REMOVE(&nvme_ctrlr->trids, path_id, link);
+ - - + #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
641 : 1776 : free(path_id);
642 : 73 : }
643 : :
644 [ + + + - ]: 1768 : pthread_mutex_destroy(&nvme_ctrlr->mutex);
645 [ + - + - ]: 1768 : spdk_keyring_put_key(nvme_ctrlr->psk);
646 [ + - + - ]: 1768 : spdk_keyring_put_key(nvme_ctrlr->dhchap_key);
647 [ + - + - ]: 1768 : spdk_keyring_put_key(nvme_ctrlr->dhchap_ctrlr_key);
648 : 1768 : free(nvme_ctrlr);
649 : :
650 [ + + ]: 1768 : pthread_mutex_lock(&g_bdev_nvme_mutex);
651 [ + + + + : 1768 : if (g_bdev_nvme_module_finish && TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) {
+ + ]
652 [ + + ]: 536 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
653 : 536 : spdk_io_device_unregister(&g_nvme_bdev_ctrlrs, NULL);
654 : 536 : spdk_bdev_module_fini_done();
655 : 536 : return;
656 : : }
657 [ - + ]: 1232 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
658 : 73 : }
659 : :
660 : : static int
661 : 155023 : nvme_detach_poller(void *arg)
662 : : {
663 : 155023 : struct nvme_ctrlr *nvme_ctrlr = arg;
664 : : int rc;
665 : :
666 [ + - + - ]: 155023 : rc = spdk_nvme_detach_poll_async(nvme_ctrlr->detach_ctx);
667 [ + + ]: 155023 : if (rc != -EAGAIN) {
668 [ + - ]: 1768 : spdk_poller_unregister(&nvme_ctrlr->reset_detach_poller);
669 : 1768 : _nvme_ctrlr_delete(nvme_ctrlr);
670 : 73 : }
671 : :
672 : 155023 : return SPDK_POLLER_BUSY;
673 : : }
674 : :
675 : : static void
676 : 1768 : nvme_ctrlr_delete(struct nvme_ctrlr *nvme_ctrlr)
677 : : {
678 : : int rc;
679 : :
680 [ + - ]: 1768 : spdk_poller_unregister(&nvme_ctrlr->reconnect_delay_timer);
681 : :
682 [ + + ]: 1768 : 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 [ + - ]: 1768 : spdk_poller_unregister(&nvme_ctrlr->adminq_timer_poller);
688 : :
689 : : /* If we got here, the reset/detach poller cannot be active */
690 [ + + + - : 1768 : assert(nvme_ctrlr->reset_detach_poller == NULL);
+ - # # ]
691 [ + - + - ]: 1768 : nvme_ctrlr->reset_detach_poller = SPDK_POLLER_REGISTER(nvme_detach_poller,
692 : : nvme_ctrlr, 1000);
693 [ + + + - : 1768 : 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 [ + - + - : 1768 : rc = spdk_nvme_detach_async(nvme_ctrlr->ctrlr, &nvme_ctrlr->detach_ctx);
+ - ]
699 [ - + ]: 1768 : if (rc != 0) {
700 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to detach the NVMe controller\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
701 : 0 : goto error;
702 : : }
703 : :
704 : 1768 : return;
705 : 0 : error:
706 : : /* We don't have a good way to handle errors here, so just do what we can and delete the
707 : : * controller without detaching the underlying NVMe device.
708 : : */
709 [ # # ]: 0 : spdk_poller_unregister(&nvme_ctrlr->reset_detach_poller);
710 : 0 : _nvme_ctrlr_delete(nvme_ctrlr);
711 : 73 : }
712 : :
713 : : static void
714 : 1764 : nvme_ctrlr_unregister_cb(void *io_device)
715 : : {
716 : 1764 : struct nvme_ctrlr *nvme_ctrlr = io_device;
717 : :
718 : 1764 : nvme_ctrlr_delete(nvme_ctrlr);
719 : 1764 : }
720 : :
721 : : static void
722 : 1764 : nvme_ctrlr_unregister(void *ctx)
723 : : {
724 : 1764 : struct nvme_ctrlr *nvme_ctrlr = ctx;
725 : :
726 : 1764 : spdk_io_device_unregister(nvme_ctrlr, nvme_ctrlr_unregister_cb);
727 : 1764 : }
728 : :
729 : : static bool
730 : 8166 : nvme_ctrlr_can_be_unregistered(struct nvme_ctrlr *nvme_ctrlr)
731 : : {
732 [ + + + + ]: 8166 : if (!nvme_ctrlr->destruct) {
733 : 4780 : return false;
734 : : }
735 : :
736 [ + + + - : 3386 : if (nvme_ctrlr->ref > 0) {
+ + ]
737 : 1607 : return false;
738 : : }
739 : :
740 [ + + - + ]: 1779 : if (nvme_ctrlr->resetting) {
741 : 15 : return false;
742 : : }
743 : :
744 [ + + - + ]: 1764 : if (nvme_ctrlr->ana_log_page_updating) {
745 : 0 : return false;
746 : : }
747 : :
748 [ + + - + ]: 1764 : if (nvme_ctrlr->io_path_cache_clearing) {
749 : 0 : return false;
750 : : }
751 : :
752 : 1764 : return true;
753 : 294 : }
754 : :
755 : : static void
756 : 5730 : nvme_ctrlr_put_ref(struct nvme_ctrlr *nvme_ctrlr)
757 : : {
758 [ + + + - ]: 5730 : pthread_mutex_lock(&nvme_ctrlr->mutex);
759 : 1519 : SPDK_DTRACE_PROBE2(bdev_nvme_ctrlr_release, nvme_ctrlr->nbdev_ctrlr->name, nvme_ctrlr->ref);
760 : :
761 [ + + + - : 5730 : assert(nvme_ctrlr->ref > 0);
+ - # # ]
762 [ + - + - ]: 5730 : nvme_ctrlr->ref--;
763 : :
764 [ + + ]: 5730 : if (!nvme_ctrlr_can_be_unregistered(nvme_ctrlr)) {
765 [ + + + - ]: 3981 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
766 : 3981 : return;
767 : : }
768 : :
769 [ + + + - ]: 1749 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
770 : :
771 [ + - + - ]: 1749 : spdk_thread_exec_msg(nvme_ctrlr->thread, nvme_ctrlr_unregister, nvme_ctrlr);
772 : 215 : }
773 : :
774 : : static void
775 : 3938 : nvme_ctrlr_get_ref(struct nvme_ctrlr *nvme_ctrlr)
776 : : {
777 [ + + + - ]: 3938 : pthread_mutex_lock(&nvme_ctrlr->mutex);
778 [ + - + - ]: 3938 : nvme_ctrlr->ref++;
779 [ + + + - ]: 3938 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
780 : 3938 : }
781 : :
782 : : static void
783 : 496186 : bdev_nvme_clear_current_io_path(struct nvme_bdev_channel *nbdev_ch)
784 : : {
785 [ + - + - ]: 496186 : nbdev_ch->current_io_path = NULL;
786 [ + - + - ]: 496186 : nbdev_ch->rr_counter = 0;
787 : 496186 : }
788 : :
789 : : static struct nvme_io_path *
790 : 32 : _bdev_nvme_get_io_path(struct nvme_bdev_channel *nbdev_ch, struct nvme_ns *nvme_ns)
791 : : {
792 : : struct nvme_io_path *io_path;
793 : :
794 [ + + # # : 64 : STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
# # # # #
# # # #
# ]
795 [ + + # # : 60 : if (io_path->nvme_ns == nvme_ns) {
# # ]
796 : 28 : break;
797 : : }
798 : 8 : }
799 : :
800 : 32 : return io_path;
801 : : }
802 : :
803 : : static struct nvme_io_path *
804 : 2431 : nvme_io_path_alloc(void)
805 : : {
806 : : struct nvme_io_path *io_path;
807 : :
808 : 2431 : io_path = calloc(1, sizeof(*io_path));
809 [ + + ]: 2431 : if (io_path == NULL) {
810 : 0 : SPDK_ERRLOG("Failed to alloc io_path.\n");
811 : 0 : return NULL;
812 : : }
813 : :
814 [ + + + + ]: 2431 : 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 : 2431 : return io_path;
825 : 60 : }
826 : :
827 : : static void
828 : 2431 : nvme_io_path_free(struct nvme_io_path *io_path)
829 : : {
830 [ + - + - ]: 2431 : free(io_path->stat);
831 : 2431 : free(io_path);
832 : 2431 : }
833 : :
834 : : static int
835 : 2431 : _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 : 2431 : io_path = nvme_io_path_alloc();
843 [ + + ]: 2431 : if (io_path == NULL) {
844 : 0 : return -ENOMEM;
845 : : }
846 : :
847 [ + - + - ]: 2431 : io_path->nvme_ns = nvme_ns;
848 : :
849 [ + - + - ]: 2431 : ch = spdk_get_io_channel(nvme_ns->ctrlr);
850 [ + + ]: 2431 : 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 : 2431 : ctrlr_ch = spdk_io_channel_get_ctx(ch);
857 : :
858 [ + - + - ]: 2431 : nvme_qpair = ctrlr_ch->qpair;
859 [ + + # # ]: 2431 : assert(nvme_qpair != NULL);
860 : :
861 [ + - + - ]: 2431 : io_path->qpair = nvme_qpair;
862 [ + - + - : 2431 : TAILQ_INSERT_TAIL(&nvme_qpair->io_path_list, io_path, tailq);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
863 : :
864 [ + - + - ]: 2431 : io_path->nbdev_ch = nbdev_ch;
865 [ + - + - : 2431 : STAILQ_INSERT_TAIL(&nbdev_ch->io_path_list, io_path, stailq);
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
866 : :
867 : 2431 : bdev_nvme_clear_current_io_path(nbdev_ch);
868 : :
869 : 2431 : return 0;
870 : 60 : }
871 : :
872 : : static void
873 : 2431 : 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 [ + + + - : 2435 : TAILQ_FOREACH(bio, &nbdev_ch->retry_io_list, retry_link) {
+ - + - #
# # # #
# ]
879 [ + + # # : 4 : if (bio->io_path == io_path) {
# # ]
880 [ # # # # ]: 4 : bio->io_path = NULL;
881 : 1 : }
882 : 1 : }
883 : 2431 : }
884 : :
885 : : static void
886 : 2431 : _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 : 2431 : 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 [ + + + - ]: 2431 : pthread_mutex_lock(&nbdev->mutex);
897 [ + + + + : 2431 : 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 [ + + + - ]: 2431 : pthread_mutex_unlock(&nbdev->mutex);
901 : :
902 : 2431 : bdev_nvme_clear_current_io_path(nbdev_ch);
903 : 2431 : bdev_nvme_clear_retry_io_path(nbdev_ch, io_path);
904 : :
905 [ + + + + : 2433 : STAILQ_REMOVE(&nbdev_ch->io_path_list, io_path, nvme_io_path, stailq);
+ + + + +
- + - + -
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
906 [ + - + - ]: 2431 : io_path->nbdev_ch = NULL;
907 : :
908 [ + - + - ]: 2431 : nvme_qpair = io_path->qpair;
909 [ + + # # ]: 2431 : assert(nvme_qpair != NULL);
910 : :
911 [ + - + - ]: 2431 : ctrlr_ch = nvme_qpair->ctrlr_ch;
912 [ + + # # ]: 2431 : assert(ctrlr_ch != NULL);
913 : :
914 : 2431 : ch = spdk_io_channel_from_ctx(ctrlr_ch);
915 : 2431 : 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 : 2431 : }
923 : :
924 : : static void
925 : 2373 : _bdev_nvme_delete_io_paths(struct nvme_bdev_channel *nbdev_ch)
926 : : {
927 : : struct nvme_io_path *io_path, *tmp_io_path;
928 : :
929 [ + + + + : 4796 : STAILQ_FOREACH_SAFE(io_path, &nbdev_ch->io_path_list, stailq, tmp_io_path) {
+ - + + +
- + - + -
+ + ]
930 : 2423 : _bdev_nvme_delete_io_path(nbdev_ch, io_path);
931 : 58 : }
932 : 2373 : }
933 : :
934 : : static int
935 : 2373 : bdev_nvme_create_bdev_channel_cb(void *io_device, void *ctx_buf)
936 : : {
937 : 2373 : struct nvme_bdev_channel *nbdev_ch = ctx_buf;
938 : 2373 : struct nvme_bdev *nbdev = io_device;
939 : : struct nvme_ns *nvme_ns;
940 : : int rc;
941 : :
942 [ + - + - : 2373 : STAILQ_INIT(&nbdev_ch->io_path_list);
+ - + - +
- + - + -
+ - ]
943 [ + - + - : 2373 : TAILQ_INIT(&nbdev_ch->retry_io_list);
+ - + - +
- + - + -
+ - ]
944 : :
945 [ + + + - ]: 2373 : pthread_mutex_lock(&nbdev->mutex);
946 : :
947 [ + - + - : 2373 : nbdev_ch->mp_policy = nbdev->mp_policy;
+ - + - ]
948 [ + - + - : 2373 : nbdev_ch->mp_selector = nbdev->mp_selector;
+ - + - ]
949 [ + - + - : 2373 : nbdev_ch->rr_min_io = nbdev->rr_min_io;
+ - + - ]
950 : :
951 [ + + + - : 4796 : TAILQ_FOREACH(nvme_ns, &nbdev->nvme_ns_list, tailq) {
+ - + + +
- + - +
- ]
952 : 2423 : rc = _bdev_nvme_add_io_path(nbdev_ch, nvme_ns);
953 [ + + ]: 2423 : if (rc != 0) {
954 [ # # # # ]: 0 : pthread_mutex_unlock(&nbdev->mutex);
955 : :
956 : 0 : _bdev_nvme_delete_io_paths(nbdev_ch);
957 : 0 : return rc;
958 : : }
959 : 58 : }
960 [ + + + - ]: 2373 : pthread_mutex_unlock(&nbdev->mutex);
961 : :
962 : 2373 : return 0;
963 : 47 : }
964 : :
965 : : /* If cpl != NULL, complete the bdev_io with nvme status based on 'cpl'.
966 : : * If cpl == NULL, complete the bdev_io with bdev status based on 'status'.
967 : : */
968 : : static inline void
969 : 28888001 : __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 [ + + + + : 28888001 : spdk_trace_record(TRACE_BDEV_NVME_IO_DONE, 0, 0, (uintptr_t)bdev_io->driver_ctx,
+ - + - +
- + - + -
- + # # ]
973 : : (uintptr_t)bdev_io);
974 [ + + ]: 28888001 : if (cpl) {
975 [ + - + - : 26940844 : spdk_bdev_io_complete_nvme_status(bdev_io, cpl->cdw0, cpl->status.sct, cpl->status.sc);
+ - + - +
- + - + -
+ - ]
976 : 361697 : } else {
977 : 1947157 : spdk_bdev_io_complete(bdev_io, status);
978 : : }
979 : 28888001 : }
980 : :
981 : : static void bdev_nvme_abort_retry_ios(struct nvme_bdev_channel *nbdev_ch);
982 : :
983 : : static void
984 : 2373 : bdev_nvme_destroy_bdev_channel_cb(void *io_device, void *ctx_buf)
985 : : {
986 : 2373 : struct nvme_bdev_channel *nbdev_ch = ctx_buf;
987 : :
988 : 2373 : bdev_nvme_abort_retry_ios(nbdev_ch);
989 : 2373 : _bdev_nvme_delete_io_paths(nbdev_ch);
990 : 2373 : }
991 : :
992 : : static inline bool
993 : 29382562 : bdev_nvme_io_type_is_admin(enum spdk_bdev_io_type io_type)
994 : : {
995 [ + + ]: 29382562 : switch (io_type) {
996 : 136 : case SPDK_BDEV_IO_TYPE_RESET:
997 : : case SPDK_BDEV_IO_TYPE_NVME_ADMIN:
998 : : case SPDK_BDEV_IO_TYPE_ABORT:
999 : 142 : return true;
1000 : 29020484 : default:
1001 : 29382420 : break;
1002 : : }
1003 : :
1004 : 29382420 : return false;
1005 : 361942 : }
1006 : :
1007 : : static inline bool
1008 : 10366433 : nvme_ns_is_active(struct nvme_ns *nvme_ns)
1009 : : {
1010 [ + + + + : 10366433 : if (spdk_unlikely(nvme_ns->ana_state_updating)) {
+ - - + ]
1011 : 3263 : return false;
1012 : : }
1013 : :
1014 [ + + + - : 10363170 : if (spdk_unlikely(nvme_ns->ns == NULL)) {
- + ]
1015 : 0 : return false;
1016 : : }
1017 : :
1018 : 10363170 : return true;
1019 : 157451 : }
1020 : :
1021 : : static inline bool
1022 : 10366367 : nvme_ns_is_accessible(struct nvme_ns *nvme_ns)
1023 : : {
1024 [ + + ]: 10366367 : if (spdk_unlikely(!nvme_ns_is_active(nvme_ns))) {
1025 : 3263 : return false;
1026 : : }
1027 : :
1028 [ + + + - : 10363104 : switch (nvme_ns->ana_state) {
+ - ]
1029 : 9431607 : case SPDK_NVME_ANA_OPTIMIZED_STATE:
1030 : : case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
1031 : 9589018 : return true;
1032 : 774077 : default:
1033 : 774086 : break;
1034 : : }
1035 : :
1036 : 774086 : return false;
1037 : 157421 : }
1038 : :
1039 : : static inline bool
1040 : 11694646 : nvme_qpair_is_connected(struct nvme_qpair *nvme_qpair)
1041 : : {
1042 [ + + + - : 11694646 : if (spdk_unlikely(nvme_qpair->qpair == NULL)) {
- + ]
1043 : 1307612 : return false;
1044 : : }
1045 : :
1046 [ + + + - : 10387034 : if (spdk_unlikely(spdk_nvme_qpair_get_failure_reason(nvme_qpair->qpair) !=
- + ]
1047 : : SPDK_NVME_QPAIR_FAILURE_NONE)) {
1048 : 2952 : return false;
1049 : : }
1050 : :
1051 [ + + + - : 10384082 : if (spdk_unlikely(nvme_qpair->ctrlr_ch->reset_iter != NULL)) {
+ - + - -
+ ]
1052 : 8814 : return false;
1053 : : }
1054 : :
1055 : 10375268 : return true;
1056 : 157486 : }
1057 : :
1058 : : static inline bool
1059 : 11205568 : nvme_io_path_is_available(struct nvme_io_path *io_path)
1060 : : {
1061 [ + + + - : 11205568 : if (spdk_unlikely(!nvme_qpair_is_connected(io_path->qpair))) {
- + ]
1062 : 839687 : return false;
1063 : : }
1064 : :
1065 [ + + + - : 10365881 : if (spdk_unlikely(!nvme_ns_is_accessible(io_path->nvme_ns))) {
- + ]
1066 : 777229 : return false;
1067 : : }
1068 : :
1069 : 9588652 : return true;
1070 : 157431 : }
1071 : :
1072 : : static inline bool
1073 : 479691 : nvme_ctrlr_is_failed(struct nvme_ctrlr *nvme_ctrlr)
1074 : : {
1075 [ + + # # ]: 479691 : if (nvme_ctrlr->destruct) {
1076 : 512 : return true;
1077 : : }
1078 : :
1079 [ + + # # ]: 479179 : if (nvme_ctrlr->fast_io_fail_timedout) {
1080 : 79636 : return true;
1081 : : }
1082 : :
1083 [ + + # # ]: 399543 : if (nvme_ctrlr->resetting) {
1084 [ + + # # : 273922 : if (nvme_ctrlr->opts.reconnect_delay_sec != 0) {
# # # # ]
1085 : 1044 : return false;
1086 : : } else {
1087 : 272878 : return true;
1088 : : }
1089 : : }
1090 : :
1091 [ + + # # ]: 125621 : if (nvme_ctrlr->reconnect_is_delayed) {
1092 : 4104 : return false;
1093 : : }
1094 : :
1095 [ - + # # ]: 121517 : if (nvme_ctrlr->disabled) {
1096 : 0 : return true;
1097 : : }
1098 : :
1099 [ + + # # : 121517 : if (spdk_nvme_ctrlr_is_failed(nvme_ctrlr->ctrlr)) {
# # ]
1100 : 119213 : return true;
1101 : : } else {
1102 : 2304 : return false;
1103 : : }
1104 : 9 : }
1105 : :
1106 : : static bool
1107 : 13896 : nvme_ctrlr_is_available(struct nvme_ctrlr *nvme_ctrlr)
1108 : : {
1109 [ + + # # ]: 13896 : if (nvme_ctrlr->destruct) {
1110 : 960 : return false;
1111 : : }
1112 : :
1113 [ + + # # : 12936 : if (spdk_nvme_ctrlr_is_failed(nvme_ctrlr->ctrlr)) {
# # ]
1114 : 13 : return false;
1115 : : }
1116 : :
1117 [ + + - + : 12923 : if (nvme_ctrlr->resetting || nvme_ctrlr->reconnect_is_delayed) {
# # # # ]
1118 : 4744 : return false;
1119 : : }
1120 : :
1121 [ + + # # ]: 8179 : if (nvme_ctrlr->disabled) {
1122 : 0 : return false;
1123 : : }
1124 : :
1125 : 8179 : return true;
1126 : 33 : }
1127 : :
1128 : : /* Simulate circular linked list. */
1129 : : static inline struct nvme_io_path *
1130 : 6978848 : 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 [ + + ]: 6978848 : if (prev_path != NULL) {
1135 [ # # # # : 5345463 : next_path = STAILQ_NEXT(prev_path, stailq);
# # ]
1136 [ + + ]: 5345463 : if (next_path != NULL) {
1137 : 2551967 : return next_path;
1138 : : }
1139 : 25 : }
1140 : :
1141 [ + - + - : 4426881 : return STAILQ_FIRST(&nbdev_ch->io_path_list);
+ - ]
1142 : 120 : }
1143 : :
1144 : : static struct nvme_io_path *
1145 : 2812523 : _bdev_nvme_find_io_path(struct nvme_bdev_channel *nbdev_ch)
1146 : : {
1147 : 2812523 : struct nvme_io_path *io_path, *start, *non_optimized = NULL;
1148 : :
1149 [ + - + - ]: 2812523 : start = nvme_io_path_get_next(nbdev_ch, nbdev_ch->current_io_path);
1150 : :
1151 : 2812523 : io_path = start;
1152 : 88 : do {
1153 [ + + ]: 4825389 : if (spdk_likely(nvme_io_path_is_available(io_path))) {
1154 [ + + + - : 3208609 : switch (io_path->nvme_ns->ana_state) {
+ - + - +
- - ]
1155 : 658996 : case SPDK_NVME_ANA_OPTIMIZED_STATE:
1156 [ + - + - ]: 659064 : nbdev_ch->current_io_path = io_path;
1157 : 659064 : return io_path;
1158 : 2549535 : case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
1159 [ + + ]: 2549545 : if (non_optimized == NULL) {
1160 : 1982662 : non_optimized = io_path;
1161 : 7 : }
1162 : 2549545 : break;
1163 : 0 : default:
1164 [ # # ]: 0 : assert(false);
1165 : : break;
1166 : : }
1167 : 10 : }
1168 : 4166325 : io_path = nvme_io_path_get_next(nbdev_ch, io_path);
1169 [ + + ]: 4166325 : } while (io_path != start);
1170 : :
1171 [ + + # # : 2153459 : 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 [ # # # # ]: 522604 : nbdev_ch->current_io_path = non_optimized;
1176 : 1 : }
1177 : :
1178 : 2153459 : return non_optimized;
1179 : 88 : }
1180 : :
1181 : : static struct nvme_io_path *
1182 : 16 : _bdev_nvme_find_io_path_min_qd(struct nvme_bdev_channel *nbdev_ch)
1183 : : {
1184 : : struct nvme_io_path *io_path;
1185 : 16 : struct nvme_io_path *optimized = NULL, *non_optimized = NULL;
1186 : 16 : uint32_t opt_min_qd = UINT32_MAX, non_opt_min_qd = UINT32_MAX;
1187 : : uint32_t num_outstanding_reqs;
1188 : :
1189 [ + + # # : 64 : STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
# # # # #
# # # #
# ]
1190 [ - + # # : 48 : if (spdk_unlikely(!nvme_qpair_is_connected(io_path->qpair))) {
# # ]
1191 : : /* The device is currently resetting. */
1192 : 0 : continue;
1193 : : }
1194 : :
1195 [ - + # # : 48 : if (spdk_unlikely(!nvme_ns_is_active(io_path->nvme_ns))) {
# # ]
1196 : 0 : continue;
1197 : : }
1198 : :
1199 [ # # # # : 48 : num_outstanding_reqs = spdk_nvme_qpair_get_num_outstanding_reqs(io_path->qpair->qpair);
# # # # ]
1200 [ + + + # : 48 : switch (io_path->nvme_ns->ana_state) {
# # # # #
# # ]
1201 : 18 : case SPDK_NVME_ANA_OPTIMIZED_STATE:
1202 [ + + ]: 24 : if (num_outstanding_reqs < opt_min_qd) {
1203 : 20 : opt_min_qd = num_outstanding_reqs;
1204 : 20 : optimized = io_path;
1205 : 5 : }
1206 : 24 : break;
1207 : 9 : case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
1208 [ + + ]: 12 : if (num_outstanding_reqs < non_opt_min_qd) {
1209 : 12 : non_opt_min_qd = num_outstanding_reqs;
1210 : 12 : non_optimized = io_path;
1211 : 3 : }
1212 : 12 : break;
1213 : 9 : default:
1214 : 12 : break;
1215 : : }
1216 : 12 : }
1217 : :
1218 : : /* don't cache io path for BDEV_NVME_MP_SELECTOR_QUEUE_DEPTH selector */
1219 [ + + ]: 16 : if (optimized != NULL) {
1220 : 12 : return optimized;
1221 : : }
1222 : :
1223 : 4 : return non_optimized;
1224 : 4 : }
1225 : :
1226 : : static inline struct nvme_io_path *
1227 : 28904465 : bdev_nvme_find_io_path(struct nvme_bdev_channel *nbdev_ch)
1228 : : {
1229 [ + + + - : 28904465 : if (spdk_likely(nbdev_ch->current_io_path != NULL)) {
+ + ]
1230 [ + + + - : 27271064 : if (nbdev_ch->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE) {
- + ]
1231 [ + - + - ]: 26091914 : return nbdev_ch->current_io_path;
1232 [ + + # # : 1179150 : } else if (nbdev_ch->mp_selector == BDEV_NVME_MP_SELECTOR_ROUND_ROBIN) {
# # ]
1233 [ + + # # : 1179150 : if (++nbdev_ch->rr_counter < nbdev_ch->rr_min_io) {
# # # # ]
1234 [ # # # # ]: 12 : return nbdev_ch->current_io_path;
1235 : : }
1236 [ # # # # ]: 1179138 : nbdev_ch->rr_counter = 0;
1237 : 7 : }
1238 : 7 : }
1239 : :
1240 [ + + + + : 2812539 : if (nbdev_ch->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE ||
- + # # ]
1241 [ + + # # ]: 1179213 : nbdev_ch->mp_selector == BDEV_NVME_MP_SELECTOR_ROUND_ROBIN) {
1242 : 2812523 : return _bdev_nvme_find_io_path(nbdev_ch);
1243 : : } else {
1244 : 16 : return _bdev_nvme_find_io_path_min_qd(nbdev_ch);
1245 : : }
1246 : 361816 : }
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 : 488525 : any_io_path_may_become_available(struct nvme_bdev_channel *nbdev_ch)
1261 : : {
1262 : : struct nvme_io_path *io_path;
1263 : :
1264 [ + + + + : 488525 : if (nbdev_ch->resetting) {
# # # # ]
1265 : 4 : return false;
1266 : : }
1267 : :
1268 [ + + # # : 961656 : STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
# # # # #
# # # #
# ]
1269 [ - + + + : 489417 : if (io_path->nvme_ns->ana_transition_timedout) {
# # # # #
# # # ]
1270 : 896 : continue;
1271 : : }
1272 : :
1273 [ + + + + : 488521 : if (nvme_qpair_is_connected(io_path->qpair) ||
# # # # ]
1274 [ + + # # : 479691 : !nvme_ctrlr_is_failed(io_path->qpair->ctrlr)) {
# # # # ]
1275 : 16282 : return true;
1276 : : }
1277 : 2 : }
1278 : :
1279 : 472239 : return false;
1280 : 15 : }
1281 : :
1282 : : static void
1283 : 20032 : bdev_nvme_retry_io(struct nvme_bdev_channel *nbdev_ch, struct spdk_bdev_io *bdev_io)
1284 : : {
1285 [ # # ]: 20032 : struct nvme_bdev_io *nbdev_io = (struct nvme_bdev_io *)bdev_io->driver_ctx;
1286 : : struct spdk_io_channel *ch;
1287 : :
1288 [ + + + - : 20032 : if (nbdev_io->io_path != NULL && nvme_io_path_is_available(nbdev_io->io_path)) {
# # # # #
# # # ]
1289 : 3754 : _bdev_nvme_submit_request(nbdev_ch, bdev_io);
1290 : 3 : } else {
1291 : 16278 : ch = spdk_io_channel_from_ctx(nbdev_ch);
1292 : 16278 : bdev_nvme_submit_request(ch, bdev_io);
1293 : : }
1294 : 20032 : }
1295 : :
1296 : : static int
1297 : 4643 : bdev_nvme_retry_ios(void *arg)
1298 : : {
1299 : 4643 : struct nvme_bdev_channel *nbdev_ch = arg;
1300 : : struct nvme_bdev_io *bio, *tmp_bio;
1301 : : uint64_t now, delay_us;
1302 : :
1303 : 4643 : now = spdk_get_ticks();
1304 : :
1305 [ + + + + : 24675 : TAILQ_FOREACH_SAFE(bio, &nbdev_ch->retry_io_list, retry_link, tmp_bio) {
# # # # #
# # # # #
# # ]
1306 [ + + # # : 20790 : if (bio->retry_ticks > now) {
# # ]
1307 : 758 : break;
1308 : : }
1309 : :
1310 [ + + # # : 20032 : TAILQ_REMOVE(&nbdev_ch->retry_io_list, bio, retry_link);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
1311 : :
1312 : 20032 : bdev_nvme_retry_io(nbdev_ch, spdk_bdev_io_from_ctx(bio));
1313 : 14 : }
1314 : :
1315 [ # # ]: 4643 : spdk_poller_unregister(&nbdev_ch->retry_io_poller);
1316 : :
1317 [ # # # # : 4643 : bio = TAILQ_FIRST(&nbdev_ch->retry_io_list);
# # ]
1318 [ + + ]: 4643 : if (bio != NULL) {
1319 [ - + # # : 771 : delay_us = (bio->retry_ticks - now) * SPDK_SEC_TO_USEC / spdk_get_ticks_hz();
# # ]
1320 : :
1321 [ # # # # ]: 771 : nbdev_ch->retry_io_poller = SPDK_POLLER_REGISTER(bdev_nvme_retry_ios, nbdev_ch,
1322 : : delay_us);
1323 : 4 : }
1324 : :
1325 : 4643 : return SPDK_POLLER_BUSY;
1326 : : }
1327 : :
1328 : : static void
1329 : 20040 : 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 [ # # # # : 20040 : bio->retry_ticks = spdk_get_ticks() + delay_ms * spdk_get_ticks_hz() / 1000ULL;
# # ]
1335 : :
1336 [ + + # # : 47405 : TAILQ_FOREACH_REVERSE(tmp_bio, &nbdev_ch->retry_io_list, retry_io_head, retry_link) {
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
1337 [ + + # # : 43499 : if (tmp_bio->retry_ticks <= bio->retry_ticks) {
# # # # #
# ]
1338 [ + + # # : 16134 : TAILQ_INSERT_AFTER(&nbdev_ch->retry_io_list, tmp_bio, bio,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
1339 : : retry_link);
1340 : 16134 : return;
1341 : : }
1342 : 0 : }
1343 : :
1344 : : /* No earlier I/Os were found. This I/O must be the new head. */
1345 [ + + # # : 3906 : TAILQ_INSERT_HEAD(&nbdev_ch->retry_io_list, bio, retry_link);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
1346 : :
1347 [ # # ]: 3906 : spdk_poller_unregister(&nbdev_ch->retry_io_poller);
1348 : :
1349 [ # # # # ]: 3906 : nbdev_ch->retry_io_poller = SPDK_POLLER_REGISTER(bdev_nvme_retry_ios, nbdev_ch,
1350 : : delay_ms * 1000ULL);
1351 : 16 : }
1352 : :
1353 : : static void
1354 : 2541 : bdev_nvme_abort_retry_ios(struct nvme_bdev_channel *nbdev_ch)
1355 : : {
1356 : : struct nvme_bdev_io *bio, *tmp_bio;
1357 : :
1358 [ + + + + : 2545 : TAILQ_FOREACH_SAFE(bio, &nbdev_ch->retry_io_list, retry_link, tmp_bio) {
+ - + - #
# # # # #
+ - ]
1359 [ - + # # : 4 : TAILQ_REMOVE(&nbdev_ch->retry_io_list, bio, retry_link);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
1360 : 4 : __bdev_nvme_io_complete(spdk_bdev_io_from_ctx(bio), SPDK_BDEV_IO_STATUS_ABORTED, NULL);
1361 : 1 : }
1362 : :
1363 [ + - ]: 2541 : spdk_poller_unregister(&nbdev_ch->retry_io_poller);
1364 : 2541 : }
1365 : :
1366 : : static int
1367 : 7330 : 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 [ + + # # : 7330 : TAILQ_FOREACH(bio, &nbdev_ch->retry_io_list, retry_link) {
# # # # #
# # # #
# ]
1373 [ + + ]: 4 : if (bio == bio_to_abort) {
1374 [ - + # # : 4 : TAILQ_REMOVE(&nbdev_ch->retry_io_list, bio, retry_link);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
1375 : 4 : __bdev_nvme_io_complete(spdk_bdev_io_from_ctx(bio), SPDK_BDEV_IO_STATUS_ABORTED, NULL);
1376 : 4 : return 0;
1377 : : }
1378 : 0 : }
1379 : :
1380 : 7326 : return -ENOENT;
1381 : 6 : }
1382 : :
1383 : : static void
1384 : 10615 : 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 [ + + + + : 10615 : assert(spdk_nvme_cpl_is_error(cpl));
# # # # #
# # # # #
# # # # ]
1390 : :
1391 [ # # # # : 10615 : nbdev = bdev_io->bdev->ctxt;
# # # # ]
1392 : :
1393 [ + + # # : 10615 : if (nbdev->err_stat == NULL) {
# # ]
1394 : 6927 : return;
1395 : : }
1396 : :
1397 [ # # # # : 3688 : sct = cpl->status.sct;
# # ]
1398 [ # # # # : 3688 : sc = cpl->status.sc;
# # ]
1399 : :
1400 [ - + # # ]: 3688 : pthread_mutex_lock(&nbdev->mutex);
1401 : :
1402 [ # # # # : 3688 : nbdev->err_stat->status_type[sct]++;
# # # # #
# ]
1403 [ + - ]: 3688 : switch (sct) {
1404 : 3688 : 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 [ # # # # : 3688 : nbdev->err_stat->status[sct][sc]++;
# # # # #
# # # ]
1409 : 3688 : break;
1410 : 0 : default:
1411 : 0 : break;
1412 : : }
1413 : :
1414 [ - + # # ]: 3688 : pthread_mutex_unlock(&nbdev->mutex);
1415 : 14 : }
1416 : :
1417 : : static inline void
1418 : 26940647 : bdev_nvme_update_io_path_stat(struct nvme_bdev_io *bio)
1419 : : {
1420 : 26940647 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
1421 [ + - + - : 26940647 : uint64_t num_blocks = bdev_io->u.bdev.num_blocks;
+ - + - ]
1422 [ + - + - : 26940647 : uint32_t blocklen = bdev_io->bdev->blocklen;
+ - + - ]
1423 : : struct spdk_bdev_io_stat *stat;
1424 : : uint64_t tsc_diff;
1425 : :
1426 [ + + + - : 26940647 : if (bio->io_path->stat == NULL) {
+ - + - +
- ]
1427 : 26940647 : 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 : 361773 : }
1509 : :
1510 : : static bool
1511 : 10536 : 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 [ # # # # ]: 10536 : struct nvme_io_path *io_path = bio->io_path;
1517 [ # # # # : 10536 : struct nvme_ctrlr *nvme_ctrlr = io_path->qpair->ctrlr;
# # # # ]
1518 : : const struct spdk_nvme_ctrlr_data *cdata;
1519 : :
1520 [ + + - + : 10540 : if (spdk_nvme_cpl_is_path_error(cpl) ||
# # # # #
# ]
1521 [ + + + + : 7927 : spdk_nvme_cpl_is_aborted_sq_deletion(cpl) ||
# # # # #
# # # #
# ]
1522 [ + + ]: 3754 : !nvme_io_path_is_available(io_path) ||
1523 [ - + ]: 3758 : !nvme_ctrlr_is_available(nvme_ctrlr)) {
1524 : 6786 : bdev_nvme_clear_current_io_path(nbdev_ch);
1525 [ # # # # ]: 6786 : bio->io_path = NULL;
1526 [ + + + - : 6786 : if (spdk_nvme_cpl_is_ana_error(cpl)) {
+ + - + #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
1527 [ + + ]: 2601 : if (nvme_ctrlr_read_ana_log_page(nvme_ctrlr) == 0) {
1528 [ # # # # : 18 : io_path->nvme_ns->ana_state_updating = true;
# # # # ]
1529 : 1 : }
1530 : 1 : }
1531 [ + + ]: 6774 : if (!any_io_path_may_become_available(nbdev_ch)) {
1532 : 64 : return false;
1533 : : }
1534 [ # # ]: 6710 : *_delay_ms = 0;
1535 : 3 : } else {
1536 [ # # # # ]: 3758 : bio->retry_count++;
1537 : :
1538 [ # # # # ]: 3758 : cdata = spdk_nvme_ctrlr_get_data(nvme_ctrlr->ctrlr);
1539 : :
1540 [ + + # # : 3758 : if (cpl->status.crd != 0) {
# # # # ]
1541 [ # # # # : 4 : *_delay_ms = cdata->crdt[cpl->status.crd] * 100;
# # # # #
# # # # #
# # # # ]
1542 : 1 : } else {
1543 [ # # ]: 3754 : *_delay_ms = 0;
1544 : : }
1545 : : }
1546 : :
1547 : 10468 : return true;
1548 : 7 : }
1549 : :
1550 : : static inline void
1551 : 26951366 : bdev_nvme_io_complete_nvme_status(struct nvme_bdev_io *bio,
1552 : : const struct spdk_nvme_cpl *cpl)
1553 : : {
1554 : 26951366 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
1555 : : struct nvme_bdev_channel *nbdev_ch;
1556 : 6913614 : uint64_t delay_ms;
1557 : :
1558 [ + + + - : 26951366 : assert(!bdev_nvme_io_type_is_admin(bdev_io->type));
+ - # # ]
1559 : :
1560 [ + + + + : 26951366 : if (spdk_likely(spdk_nvme_cpl_is_success(cpl))) {
+ - - + +
- + - + -
+ - ]
1561 : 26940743 : bdev_nvme_update_io_path_stat(bio);
1562 : 26940743 : 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 : 10623 : bdev_nvme_update_nvme_error_stat(bdev_io, cpl);
1569 : :
1570 [ + + + + : 10630 : if (cpl->status.dnr != 0 || spdk_nvme_cpl_is_aborted_by_request(cpl) ||
+ + # # #
# # # # #
# # # # #
# # # #
# ]
1571 [ + + + + : 10548 : (g_opts.bdev_retry_count != -1 && bio->retry_count >= g_opts.bdev_retry_count)) {
# # # # #
# ]
1572 : 101 : 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 [ - + # # : 10532 : if (bdev_io->u.bdev.accel_sequence != NULL) {
# # # # #
# ]
1578 : 0 : goto complete;
1579 : : }
1580 : :
1581 : 10532 : nbdev_ch = spdk_io_channel_get_ctx(spdk_bdev_io_get_io_channel(bdev_io));
1582 : :
1583 [ + + ]: 10532 : if (bdev_nvme_check_retry_io(bio, cpl, nbdev_ch, &delay_ms)) {
1584 : 10468 : bdev_nvme_queue_retry_io(nbdev_ch, bio, delay_ms);
1585 : 10468 : return;
1586 : : }
1587 : :
1588 : 6913657 : complete:
1589 [ + - + - ]: 26940890 : bio->retry_count = 0;
1590 [ + - + - ]: 26940890 : bio->submit_tsc = 0;
1591 [ + - + - : 26940890 : bdev_io->u.bdev.accel_sequence = NULL;
+ - + - ]
1592 : 26940890 : __bdev_nvme_io_complete(bdev_io, 0, cpl);
1593 : 361883 : }
1594 : :
1595 : : static inline void
1596 : 1949283 : bdev_nvme_io_complete(struct nvme_bdev_io *bio, int rc)
1597 : : {
1598 : 1949283 : 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 [ - + # # : 1949283 : assert(!bdev_nvme_io_type_is_admin(bdev_io->type));
# # # # ]
1603 : :
1604 [ + + + + ]: 1949283 : switch (rc) {
1605 : 1289864 : case 0:
1606 : 1289865 : io_status = SPDK_BDEV_IO_STATUS_SUCCESS;
1607 : 1289865 : break;
1608 : 177667 : case -ENOMEM:
1609 : 177667 : io_status = SPDK_BDEV_IO_STATUS_NOMEM;
1610 : 177667 : break;
1611 : 481739 : case -ENXIO:
1612 [ + + + - : 481754 : if (g_opts.bdev_retry_count == -1 || bio->retry_count < g_opts.bdev_retry_count) {
# # # # #
# # # ]
1613 : 481751 : nbdev_ch = spdk_io_channel_get_ctx(spdk_bdev_io_get_io_channel(bdev_io));
1614 : :
1615 : 481751 : bdev_nvme_clear_current_io_path(nbdev_ch);
1616 [ # # # # ]: 481751 : bio->io_path = NULL;
1617 : :
1618 [ + + ]: 481751 : if (any_io_path_may_become_available(nbdev_ch)) {
1619 : 9572 : bdev_nvme_queue_retry_io(nbdev_ch, bio, 1000ULL);
1620 : 9572 : return;
1621 : : }
1622 : 3 : }
1623 : :
1624 : : /* fallthrough */
1625 : : default:
1626 [ # # # # : 472179 : spdk_accel_sequence_abort(bdev_io->u.bdev.accel_sequence);
# # # # ]
1627 [ # # # # : 472179 : bdev_io->u.bdev.accel_sequence = NULL;
# # # # ]
1628 : 472179 : io_status = SPDK_BDEV_IO_STATUS_FAILED;
1629 : 472179 : break;
1630 : : }
1631 : :
1632 [ # # # # ]: 1939711 : bio->retry_count = 0;
1633 [ # # # # ]: 1939711 : bio->submit_tsc = 0;
1634 : 1939711 : __bdev_nvme_io_complete(bdev_io, io_status, NULL);
1635 : 13 : }
1636 : :
1637 : : static inline void
1638 : 16 : bdev_nvme_admin_complete(struct nvme_bdev_io *bio, int rc)
1639 : : {
1640 : 16 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
1641 : : enum spdk_bdev_io_status io_status;
1642 : :
1643 [ + + + + ]: 16 : switch (rc) {
1644 : 3 : case 0:
1645 : 4 : io_status = SPDK_BDEV_IO_STATUS_SUCCESS;
1646 : 4 : break;
1647 : 0 : case -ENOMEM:
1648 : 0 : io_status = SPDK_BDEV_IO_STATUS_NOMEM;
1649 : 0 : break;
1650 : 10 : case -ENXIO:
1651 : : /* fallthrough */
1652 : : default:
1653 : 12 : io_status = SPDK_BDEV_IO_STATUS_FAILED;
1654 : 12 : break;
1655 : : }
1656 : :
1657 : 16 : __bdev_nvme_io_complete(bdev_io, io_status, NULL);
1658 : 16 : }
1659 : :
1660 : : static void
1661 : 1564 : bdev_nvme_clear_io_path_caches_done(struct nvme_ctrlr *nvme_ctrlr,
1662 : : void *ctx, int status)
1663 : : {
1664 [ - + # # ]: 1564 : pthread_mutex_lock(&nvme_ctrlr->mutex);
1665 : :
1666 [ + + # # : 1564 : assert(nvme_ctrlr->io_path_cache_clearing == true);
# # ]
1667 [ # # ]: 1564 : nvme_ctrlr->io_path_cache_clearing = false;
1668 : :
1669 [ + + ]: 1564 : if (!nvme_ctrlr_can_be_unregistered(nvme_ctrlr)) {
1670 [ - + # # ]: 1564 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
1671 : 1564 : return;
1672 : : }
1673 : :
1674 [ # # # # ]: 0 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
1675 : :
1676 : 0 : nvme_ctrlr_unregister(nvme_ctrlr);
1677 : 3 : }
1678 : :
1679 : : static void
1680 : 10178 : _bdev_nvme_clear_io_path_cache(struct nvme_qpair *nvme_qpair)
1681 : : {
1682 : : struct nvme_io_path *io_path;
1683 : :
1684 [ + + + - : 17771 : TAILQ_FOREACH(io_path, &nvme_qpair->io_path_list, tailq) {
+ - + + +
- + - +
- ]
1685 [ + + + - : 7593 : if (io_path->nbdev_ch == NULL) {
- + ]
1686 : 4810 : continue;
1687 : : }
1688 [ # # # # ]: 2783 : bdev_nvme_clear_current_io_path(io_path->nbdev_ch);
1689 : 167 : }
1690 : 10178 : }
1691 : :
1692 : : static void
1693 : 1513 : 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 [ + + # # : 1513 : assert(ctrlr_ch->qpair != NULL);
# # # # ]
1699 : :
1700 [ # # # # ]: 1513 : _bdev_nvme_clear_io_path_cache(ctrlr_ch->qpair);
1701 : :
1702 : 1513 : nvme_ctrlr_for_each_channel_continue(i, 0);
1703 : 1513 : }
1704 : :
1705 : : static void
1706 : 7267 : bdev_nvme_clear_io_path_caches(struct nvme_ctrlr *nvme_ctrlr)
1707 : : {
1708 [ - + # # ]: 7267 : pthread_mutex_lock(&nvme_ctrlr->mutex);
1709 [ + + + + ]: 7267 : if (!nvme_ctrlr_is_available(nvme_ctrlr) ||
1710 [ # # ]: 3 : nvme_ctrlr->io_path_cache_clearing) {
1711 [ - + # # ]: 5703 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
1712 : 5703 : return;
1713 : : }
1714 : :
1715 [ # # ]: 1564 : nvme_ctrlr->io_path_cache_clearing = true;
1716 [ - + # # ]: 1564 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
1717 : :
1718 : 1564 : nvme_ctrlr_for_each_channel(nvme_ctrlr,
1719 : : bdev_nvme_clear_io_path_cache,
1720 : : NULL,
1721 : : bdev_nvme_clear_io_path_caches_done);
1722 : 15 : }
1723 : :
1724 : : static struct nvme_qpair *
1725 : 2697 : nvme_poll_group_get_qpair(struct nvme_poll_group *group, struct spdk_nvme_qpair *qpair)
1726 : : {
1727 : : struct nvme_qpair *nvme_qpair;
1728 : :
1729 [ + + + - : 2855 : TAILQ_FOREACH(nvme_qpair, &group->qpair_list, tailq) {
+ - - + #
# # # #
# ]
1730 [ + + + - : 2855 : if (nvme_qpair->qpair == qpair) {
- + ]
1731 : 2697 : break;
1732 : : }
1733 : 17 : }
1734 : :
1735 : 2697 : return nvme_qpair;
1736 : : }
1737 : :
1738 : : static void nvme_qpair_delete(struct nvme_qpair *nvme_qpair);
1739 : :
1740 : : static void
1741 : 2697 : bdev_nvme_disconnected_qpair_cb(struct spdk_nvme_qpair *qpair, void *poll_group_ctx)
1742 : : {
1743 : 2697 : 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 : 2697 : nvme_qpair = nvme_poll_group_get_qpair(group, qpair);
1750 [ + + ]: 2697 : if (nvme_qpair == NULL) {
1751 : 0 : return;
1752 : : }
1753 : :
1754 [ + + + - : 2697 : if (nvme_qpair->qpair != NULL) {
- + ]
1755 [ + - + - ]: 2697 : spdk_nvme_ctrlr_free_io_qpair(nvme_qpair->qpair);
1756 [ + - + - ]: 2697 : nvme_qpair->qpair = NULL;
1757 : 143 : }
1758 : :
1759 : 2697 : _bdev_nvme_clear_io_path_cache(nvme_qpair);
1760 : :
1761 [ + - + - ]: 2697 : nvme_ctrlr = nvme_qpair->ctrlr;
1762 [ + - + - ]: 2697 : ctrlr_ch = nvme_qpair->ctrlr_ch;
1763 : :
1764 [ + + ]: 2697 : if (ctrlr_ch != NULL) {
1765 [ + + # # : 385 : if (ctrlr_ch->reset_iter != NULL) {
# # ]
1766 : : /* We are in a full reset sequence. */
1767 [ - + # # : 322 : 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 [ + + - + : 322 : NVME_CTRLR_INFOLOG(nvme_ctrlr,
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
1777 : : "qpair %p was disconnected and freed in a reset ctrlr sequence.\n",
1778 : : qpair);
1779 : 322 : status = 0;
1780 : : }
1781 [ # # # # ]: 322 : nvme_ctrlr_for_each_channel_continue(ctrlr_ch->reset_iter, status);
1782 [ # # # # ]: 322 : ctrlr_ch->reset_iter = NULL;
1783 : 70 : } else {
1784 : : /* qpair was disconnected unexpectedly. Reset controller for recovery. */
1785 [ + + - + : 63 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "qpair %p was disconnected and freed. reset controller.\n",
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
1786 : : qpair);
1787 : 63 : bdev_nvme_failover_ctrlr(nvme_ctrlr);
1788 : : }
1789 : 75 : } else {
1790 : : /* In this case, ctrlr_channel is already deleted. */
1791 [ + + + + : 2312 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "qpair %p was disconnected and freed. delete nvme_qpair.\n",
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
1792 : : qpair);
1793 : 2312 : nvme_qpair_delete(nvme_qpair);
1794 : : }
1795 : 143 : }
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 : 522585957 : bdev_nvme_poll(void *arg)
1816 : : {
1817 : 522585957 : struct nvme_poll_group *group = arg;
1818 : : int64_t num_completions;
1819 : :
1820 [ + + + + : 522585957 : if (group->collect_spin_stat && group->start_ticks == 0) {
+ - - + #
# # # #
# ]
1821 [ # # # # ]: 0 : group->start_ticks = spdk_get_ticks();
1822 : 0 : }
1823 : :
1824 [ + - + - ]: 522585957 : num_completions = spdk_nvme_poll_group_process_completions(group->group, 0,
1825 : : bdev_nvme_disconnected_qpair_cb);
1826 [ + + + + : 522585957 : 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 [ + + ]: 522585957 : if (spdk_unlikely(num_completions < 0)) {
1839 : 15 : bdev_nvme_check_io_qpairs(group);
1840 : 0 : }
1841 : :
1842 : 522585957 : 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 : 1510 : bdev_nvme_change_adminq_poll_period(struct nvme_ctrlr *nvme_ctrlr, uint64_t new_period_us)
1849 : : {
1850 [ - + ]: 1510 : if (spdk_interrupt_mode_is_enabled()) {
1851 : 0 : return;
1852 : : }
1853 : :
1854 [ # # ]: 1510 : spdk_poller_unregister(&nvme_ctrlr->adminq_timer_poller);
1855 : :
1856 [ # # # # ]: 1510 : nvme_ctrlr->adminq_timer_poller = SPDK_POLLER_REGISTER(bdev_nvme_poll_adminq,
1857 : : nvme_ctrlr, new_period_us);
1858 : 146 : }
1859 : :
1860 : : static int
1861 : 512778 : bdev_nvme_poll_adminq(void *arg)
1862 : : {
1863 : : int32_t rc;
1864 : 512778 : struct nvme_ctrlr *nvme_ctrlr = arg;
1865 : : nvme_ctrlr_disconnected_cb disconnected_cb;
1866 : :
1867 [ + + # # ]: 512778 : assert(nvme_ctrlr != NULL);
1868 : :
1869 [ + - + - ]: 512778 : rc = spdk_nvme_ctrlr_process_admin_completions(nvme_ctrlr->ctrlr);
1870 [ + + ]: 512778 : if (rc < 0) {
1871 [ # # # # ]: 1585 : disconnected_cb = nvme_ctrlr->disconnected_cb;
1872 [ # # # # ]: 1585 : nvme_ctrlr->disconnected_cb = NULL;
1873 : :
1874 [ + + ]: 1585 : if (disconnected_cb != NULL) {
1875 : 828 : bdev_nvme_change_adminq_poll_period(nvme_ctrlr,
1876 [ # # ]: 73 : g_opts.nvme_adminq_poll_period_us);
1877 [ # # # # ]: 755 : disconnected_cb(nvme_ctrlr);
1878 : 73 : } else {
1879 : 830 : bdev_nvme_failover_ctrlr(nvme_ctrlr);
1880 : : }
1881 [ + + + - : 511281 : } else if (spdk_nvme_ctrlr_get_admin_qp_failure_reason(nvme_ctrlr->ctrlr) !=
+ - ]
1882 : : SPDK_NVME_QPAIR_FAILURE_NONE) {
1883 : 7150 : bdev_nvme_clear_io_path_caches(nvme_ctrlr);
1884 : 12 : }
1885 : :
1886 : 512778 : return rc == 0 ? SPDK_POLLER_IDLE : SPDK_POLLER_BUSY;
1887 : : }
1888 : :
1889 : : static void
1890 : 1481 : nvme_bdev_free(void *io_device)
1891 : : {
1892 : 1481 : struct nvme_bdev *nbdev = io_device;
1893 : :
1894 [ + + + - ]: 1481 : pthread_mutex_destroy(&nbdev->mutex);
1895 [ + - + - : 1481 : free(nbdev->disk.name);
+ - ]
1896 [ + - + - ]: 1481 : free(nbdev->err_stat);
1897 : 1481 : free(nbdev);
1898 : 1481 : }
1899 : :
1900 : : static int
1901 : 1475 : bdev_nvme_destruct(void *ctx)
1902 : : {
1903 : 1475 : struct nvme_bdev *nbdev = ctx;
1904 : : struct nvme_ns *nvme_ns, *tmp_nvme_ns;
1905 : :
1906 : 394 : SPDK_DTRACE_PROBE2(bdev_nvme_destruct, nbdev->nbdev_ctrlr->name, nbdev->nsid);
1907 : :
1908 [ + + + - ]: 1475 : pthread_mutex_lock(&nbdev->mutex);
1909 : :
1910 [ + + + + : 2960 : TAILQ_FOREACH_SAFE(nvme_ns, &nbdev->nvme_ns_list, tailq, tmp_nvme_ns) {
+ - + + +
- + - + -
+ + ]
1911 [ + + + - : 1485 : pthread_mutex_lock(&nvme_ns->ctrlr->mutex);
+ - + - ]
1912 : :
1913 [ + - + - ]: 1485 : nvme_ns->bdev = NULL;
1914 : :
1915 [ + + + - : 1485 : assert(nvme_ns->id > 0);
+ - # # ]
1916 : :
1917 [ + + + - : 1485 : if (nvme_ctrlr_get_ns(nvme_ns->ctrlr, nvme_ns->id) == NULL) {
+ - + - +
- ]
1918 [ - + # # : 515 : pthread_mutex_unlock(&nvme_ns->ctrlr->mutex);
# # # # ]
1919 : :
1920 [ # # # # ]: 515 : nvme_ctrlr_put_ref(nvme_ns->ctrlr);
1921 : 515 : nvme_ns_free(nvme_ns);
1922 : 3 : } else {
1923 [ + + + - : 970 : pthread_mutex_unlock(&nvme_ns->ctrlr->mutex);
+ - + - ]
1924 : : }
1925 : 50 : }
1926 : :
1927 [ + + + - ]: 1475 : pthread_mutex_unlock(&nbdev->mutex);
1928 : :
1929 [ + + ]: 1475 : pthread_mutex_lock(&g_bdev_nvme_mutex);
1930 [ + + + - : 1475 : TAILQ_REMOVE(&nbdev->nbdev_ctrlr->bdevs, nbdev, tailq);
+ - - + #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
1931 [ + + ]: 1475 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
1932 : :
1933 : 1475 : spdk_io_device_unregister(nbdev, nvme_bdev_free);
1934 : :
1935 : 1475 : return 0;
1936 : : }
1937 : :
1938 : : static int
1939 : 2701 : bdev_nvme_create_qpair(struct nvme_qpair *nvme_qpair)
1940 : : {
1941 : : struct nvme_ctrlr *nvme_ctrlr;
1942 : 1021 : struct spdk_nvme_io_qpair_opts opts;
1943 : : struct spdk_nvme_qpair *qpair;
1944 : : int rc;
1945 : :
1946 [ + - + - ]: 2701 : nvme_ctrlr = nvme_qpair->ctrlr;
1947 : :
1948 [ + - + - ]: 2701 : spdk_nvme_ctrlr_get_default_io_qpair_opts(nvme_ctrlr->ctrlr, &opts, sizeof(opts));
1949 [ + - ]: 2701 : opts.create_only = true;
1950 : : /* In interrupt mode qpairs must be created in sync mode, else it will never be connected.
1951 : : * delay_cmd_submit must be false as in interrupt mode requests cannot be submitted in
1952 : : * completion context.
1953 : : */
1954 [ + + ]: 2701 : if (!spdk_interrupt_mode_is_enabled()) {
1955 : 2699 : opts.async_mode = true;
1956 [ + + + - : 2699 : opts.delay_cmd_submit = g_opts.delay_cmd_submit;
+ - + - ]
1957 : 144 : }
1958 [ + + + - : 2701 : opts.io_queue_requests = spdk_max(g_opts.io_queue_requests, opts.io_queue_requests);
- + # # +
- + - ]
1959 [ + - + - ]: 2701 : g_opts.io_queue_requests = opts.io_queue_requests;
1960 : :
1961 [ + - + - ]: 2701 : qpair = spdk_nvme_ctrlr_alloc_io_qpair(nvme_ctrlr->ctrlr, &opts, sizeof(opts));
1962 [ + + ]: 2701 : if (qpair == NULL) {
1963 : 0 : return -1;
1964 : : }
1965 : :
1966 : 601 : SPDK_DTRACE_PROBE3(bdev_nvme_create_qpair, nvme_ctrlr->nbdev_ctrlr->name,
1967 : : spdk_nvme_qpair_get_id(qpair), spdk_thread_get_id(nvme_ctrlr->thread));
1968 : :
1969 [ + + + - : 2701 : assert(nvme_qpair->group != NULL);
+ - # # ]
1970 : :
1971 [ + - + - : 2701 : rc = spdk_nvme_poll_group_add(nvme_qpair->group->group, qpair);
+ - + - ]
1972 [ - + ]: 2701 : if (rc != 0) {
1973 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr, "Unable to begin polling on NVMe Channel.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
1974 : 0 : goto err;
1975 : : }
1976 : :
1977 [ + - + - ]: 2701 : rc = spdk_nvme_ctrlr_connect_io_qpair(nvme_ctrlr->ctrlr, qpair);
1978 [ - + ]: 2701 : if (rc != 0) {
1979 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr, "Unable to connect I/O qpair.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
1980 : 0 : goto err;
1981 : : }
1982 : :
1983 [ + - + - ]: 2701 : nvme_qpair->qpair = qpair;
1984 : :
1985 [ + + + + ]: 2701 : if (!g_opts.disable_auto_failback) {
1986 : 2553 : _bdev_nvme_clear_io_path_cache(nvme_qpair);
1987 : 107 : }
1988 : :
1989 [ + + + + : 2701 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "Connecting qpair %p:%u started.\n",
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
1990 : : qpair, spdk_nvme_qpair_get_id(qpair));
1991 : :
1992 : 2701 : return 0;
1993 : :
1994 : 0 : err:
1995 : 0 : spdk_nvme_ctrlr_free_io_qpair(qpair);
1996 : :
1997 : 0 : return rc;
1998 : 144 : }
1999 : :
2000 : : static void bdev_nvme_reset_io_continue(void *cb_arg, int rc);
2001 : :
2002 : : static void
2003 : 755 : bdev_nvme_complete_pending_resets(struct nvme_ctrlr *nvme_ctrlr, bool success)
2004 : : {
2005 : 755 : int rc = 0;
2006 : : struct nvme_bdev_io *bio;
2007 : :
2008 [ + + # # ]: 755 : if (!success) {
2009 : 526 : rc = -1;
2010 : 33 : }
2011 : :
2012 [ + + # # : 803 : while (!TAILQ_EMPTY(&nvme_ctrlr->pending_resets)) {
# # # # ]
2013 [ # # # # : 48 : bio = TAILQ_FIRST(&nvme_ctrlr->pending_resets);
# # ]
2014 [ - + # # : 48 : TAILQ_REMOVE(&nvme_ctrlr->pending_resets, bio, retry_link);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
2015 : :
2016 : 48 : bdev_nvme_reset_io_continue(bio, rc);
2017 : : }
2018 : 755 : }
2019 : :
2020 : : /* This function marks the current trid as failed by storing the current ticks
2021 : : * and then sets the next trid to the active trid within a controller if exists.
2022 : : *
2023 : : * The purpose of the boolean return value is to request the caller to disconnect
2024 : : * the current trid now to try connecting the next trid.
2025 : : */
2026 : : static bool
2027 : 1018 : bdev_nvme_failover_trid(struct nvme_ctrlr *nvme_ctrlr, bool remove, bool start)
2028 : : {
2029 : : struct nvme_path_id *path_id, *next_path;
2030 : : int rc __attribute__((unused));
2031 : :
2032 [ # # # # : 1018 : path_id = TAILQ_FIRST(&nvme_ctrlr->trids);
# # ]
2033 [ + + # # ]: 1018 : assert(path_id);
2034 [ + + # # : 1018 : assert(path_id == nvme_ctrlr->active_path_id);
# # # # ]
2035 [ # # # # : 1018 : next_path = TAILQ_NEXT(path_id, link);
# # ]
2036 : :
2037 : : /* Update the last failed time. It means the trid is failed if its last
2038 : : * failed time is non-zero.
2039 : : */
2040 [ # # # # ]: 1018 : path_id->last_failed_tsc = spdk_get_ticks();
2041 : :
2042 [ + + ]: 1018 : if (next_path == NULL) {
2043 : : /* There is no alternate trid within a controller. */
2044 : 958 : return false;
2045 : : }
2046 : :
2047 [ + + + + : 60 : if (!start && nvme_ctrlr->opts.reconnect_delay_sec == 0) {
# # # # #
# # # ]
2048 : : /* Connect is not retried in a controller reset sequence. Connecting
2049 : : * the next trid will be done by the next bdev_nvme_failover_ctrlr() call.
2050 : : */
2051 : 12 : return false;
2052 : : }
2053 : :
2054 [ + + # # : 48 : assert(path_id->trid.trtype != SPDK_NVME_TRANSPORT_PCIE);
# # # # #
# ]
2055 : :
2056 [ + - # # : 48 : NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Start failover from %s:%s to %s:%s\n",
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2057 : : path_id->trid.traddr, path_id->trid.trsvcid,
2058 : : next_path->trid.traddr, next_path->trid.trsvcid);
2059 : :
2060 [ # # # # ]: 48 : spdk_nvme_ctrlr_fail(nvme_ctrlr->ctrlr);
2061 [ # # # # ]: 48 : nvme_ctrlr->active_path_id = next_path;
2062 [ # # # # : 48 : rc = spdk_nvme_ctrlr_set_trid(nvme_ctrlr->ctrlr, &next_path->trid);
# # ]
2063 [ - + # # ]: 48 : assert(rc == 0);
2064 [ + - # # : 48 : TAILQ_REMOVE(&nvme_ctrlr->trids, path_id, link);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
2065 [ + + # # ]: 48 : if (!remove) {
2066 : : /** Shuffle the old trid to the end of the list and use the new one.
2067 : : * Allows for round robin through multiple connections.
2068 : : */
2069 [ # # # # : 36 : TAILQ_INSERT_TAIL(&nvme_ctrlr->trids, path_id, link);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
2070 : 6 : } else {
2071 : 12 : free(path_id);
2072 : : }
2073 : :
2074 [ + + + + : 48 : if (start || next_path->last_failed_tsc == 0) {
# # # # #
# ]
2075 : : /* bdev_nvme_failover_ctrlr() is just called or the next trid is not failed
2076 : : * or used yet. Try the next trid now.
2077 : : */
2078 : 44 : return true;
2079 : : }
2080 : :
2081 [ - + - + : 5 : if (spdk_get_ticks() > next_path->last_failed_tsc + spdk_get_ticks_hz() *
# # ]
2082 [ - + # # : 4 : nvme_ctrlr->opts.reconnect_delay_sec) {
# # ]
2083 : : /* Enough backoff passed since the next trid failed. Try the next trid now. */
2084 : 0 : return true;
2085 : : }
2086 : :
2087 : : /* The next trid will be tried after reconnect_delay_sec seconds. */
2088 : 4 : return false;
2089 : 62 : }
2090 : :
2091 : : static bool
2092 : 749747 : bdev_nvme_check_ctrlr_loss_timeout(struct nvme_ctrlr *nvme_ctrlr)
2093 : : {
2094 : : int32_t elapsed;
2095 : :
2096 [ + + + + : 749747 : if (nvme_ctrlr->opts.ctrlr_loss_timeout_sec == 0 ||
# # # # #
# ]
2097 [ + + # # : 225852 : nvme_ctrlr->opts.ctrlr_loss_timeout_sec == -1) {
# # ]
2098 : 530014 : return false;
2099 : : }
2100 : :
2101 [ - + # # : 219733 : elapsed = (spdk_get_ticks() - nvme_ctrlr->reset_start_tsc) / spdk_get_ticks_hz();
# # ]
2102 [ + + # # : 219733 : if (elapsed >= nvme_ctrlr->opts.ctrlr_loss_timeout_sec) {
# # # # ]
2103 : 70 : return true;
2104 : : } else {
2105 : 219663 : return false;
2106 : : }
2107 : 241 : }
2108 : :
2109 : : static bool
2110 : 93 : bdev_nvme_check_fast_io_fail_timeout(struct nvme_ctrlr *nvme_ctrlr)
2111 : : {
2112 : : uint32_t elapsed;
2113 : :
2114 [ + + # # : 93 : if (nvme_ctrlr->opts.fast_io_fail_timeout_sec == 0) {
# # # # ]
2115 : 62 : return false;
2116 : : }
2117 : :
2118 [ - + # # : 31 : elapsed = (spdk_get_ticks() - nvme_ctrlr->reset_start_tsc) / spdk_get_ticks_hz();
# # ]
2119 [ + + # # : 31 : if (elapsed >= nvme_ctrlr->opts.fast_io_fail_timeout_sec) {
# # # # ]
2120 : 15 : return true;
2121 : : } else {
2122 : 16 : return false;
2123 : : }
2124 : 12 : }
2125 : :
2126 : : static void bdev_nvme_reset_ctrlr_complete(struct nvme_ctrlr *nvme_ctrlr, bool success);
2127 : :
2128 : : static void
2129 : 759 : nvme_ctrlr_disconnect(struct nvme_ctrlr *nvme_ctrlr, nvme_ctrlr_disconnected_cb cb_fn)
2130 : : {
2131 : : int rc;
2132 : :
2133 [ + + + + : 759 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "Start disconnecting ctrlr.\n");
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2134 : :
2135 [ # # # # ]: 759 : rc = spdk_nvme_ctrlr_disconnect(nvme_ctrlr->ctrlr);
2136 [ + + ]: 759 : if (rc != 0) {
2137 [ + - # # : 4 : NVME_CTRLR_WARNLOG(nvme_ctrlr, "disconnecting ctrlr failed.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
2138 : :
2139 : : /* Disconnect fails if ctrlr is already resetting or removed. In this case,
2140 : : * fail the reset sequence immediately.
2141 : : */
2142 : 4 : bdev_nvme_reset_ctrlr_complete(nvme_ctrlr, false);
2143 : 4 : return;
2144 : : }
2145 : :
2146 : : /* spdk_nvme_ctrlr_disconnect() may complete asynchronously later by polling adminq.
2147 : : * Set callback here to execute the specified operation after ctrlr is really disconnected.
2148 : : */
2149 [ + + # # : 755 : assert(nvme_ctrlr->disconnected_cb == NULL);
# # # # ]
2150 [ # # # # ]: 755 : nvme_ctrlr->disconnected_cb = cb_fn;
2151 : :
2152 : : /* During disconnection, reduce the period to poll adminq more often. */
2153 : 755 : bdev_nvme_change_adminq_poll_period(nvme_ctrlr, 0);
2154 : 74 : }
2155 : :
2156 : : enum bdev_nvme_op_after_reset {
2157 : : OP_NONE,
2158 : : OP_COMPLETE_PENDING_DESTRUCT,
2159 : : OP_DESTRUCT,
2160 : : OP_DELAYED_RECONNECT,
2161 : : OP_FAILOVER,
2162 : : };
2163 : :
2164 : : typedef enum bdev_nvme_op_after_reset _bdev_nvme_op_after_reset;
2165 : :
2166 : : static _bdev_nvme_op_after_reset
2167 : 755 : bdev_nvme_check_op_after_reset(struct nvme_ctrlr *nvme_ctrlr, bool success)
2168 : : {
2169 [ + + ]: 755 : if (nvme_ctrlr_can_be_unregistered(nvme_ctrlr)) {
2170 : : /* Complete pending destruct after reset completes. */
2171 : 15 : return OP_COMPLETE_PENDING_DESTRUCT;
2172 [ + + # # ]: 740 : } else if (nvme_ctrlr->pending_failover) {
2173 [ # # ]: 12 : nvme_ctrlr->pending_failover = false;
2174 [ # # # # ]: 12 : nvme_ctrlr->reset_start_tsc = 0;
2175 : 12 : return OP_FAILOVER;
2176 [ + + + + : 728 : } else if (success || nvme_ctrlr->opts.reconnect_delay_sec == 0) {
# # # # #
# # # ]
2177 [ # # # # ]: 604 : nvme_ctrlr->reset_start_tsc = 0;
2178 : 604 : return OP_NONE;
2179 [ + + ]: 124 : } else if (bdev_nvme_check_ctrlr_loss_timeout(nvme_ctrlr)) {
2180 : 31 : return OP_DESTRUCT;
2181 : : } else {
2182 [ + + ]: 93 : if (bdev_nvme_check_fast_io_fail_timeout(nvme_ctrlr)) {
2183 [ # # ]: 15 : nvme_ctrlr->fast_io_fail_timedout = true;
2184 : 2 : }
2185 : 93 : return OP_DELAYED_RECONNECT;
2186 : : }
2187 : 73 : }
2188 : :
2189 : : static int bdev_nvme_delete_ctrlr(struct nvme_ctrlr *nvme_ctrlr, bool hotplug);
2190 : : static void bdev_nvme_reconnect_ctrlr(struct nvme_ctrlr *nvme_ctrlr);
2191 : :
2192 : : static int
2193 : 81 : bdev_nvme_reconnect_delay_timer_expired(void *ctx)
2194 : : {
2195 : 81 : struct nvme_ctrlr *nvme_ctrlr = ctx;
2196 : :
2197 : 36 : SPDK_DTRACE_PROBE1(bdev_nvme_ctrlr_reconnect_delay, nvme_ctrlr->nbdev_ctrlr->name);
2198 [ - + # # ]: 81 : pthread_mutex_lock(&nvme_ctrlr->mutex);
2199 : :
2200 [ # # ]: 81 : spdk_poller_unregister(&nvme_ctrlr->reconnect_delay_timer);
2201 : :
2202 [ + + # # ]: 81 : if (!nvme_ctrlr->reconnect_is_delayed) {
2203 [ # # # # ]: 0 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
2204 : 0 : return SPDK_POLLER_BUSY;
2205 : : }
2206 : :
2207 [ # # ]: 81 : nvme_ctrlr->reconnect_is_delayed = false;
2208 : :
2209 [ - + # # ]: 81 : if (nvme_ctrlr->destruct) {
2210 [ # # # # ]: 0 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
2211 : 0 : return SPDK_POLLER_BUSY;
2212 : : }
2213 : :
2214 [ - + # # : 81 : assert(nvme_ctrlr->resetting == false);
# # ]
2215 [ # # ]: 81 : nvme_ctrlr->resetting = true;
2216 : :
2217 [ - + # # ]: 81 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
2218 : :
2219 [ # # # # ]: 81 : spdk_poller_resume(nvme_ctrlr->adminq_timer_poller);
2220 : :
2221 : 81 : bdev_nvme_reconnect_ctrlr(nvme_ctrlr);
2222 : 81 : return SPDK_POLLER_BUSY;
2223 : 9 : }
2224 : :
2225 : : static void
2226 : 93 : bdev_nvme_start_reconnect_delay_timer(struct nvme_ctrlr *nvme_ctrlr)
2227 : : {
2228 [ # # # # ]: 93 : spdk_poller_pause(nvme_ctrlr->adminq_timer_poller);
2229 : :
2230 [ + + # # : 93 : assert(nvme_ctrlr->reconnect_is_delayed == false);
# # ]
2231 [ # # ]: 93 : nvme_ctrlr->reconnect_is_delayed = true;
2232 : :
2233 [ + + # # : 93 : assert(nvme_ctrlr->reconnect_delay_timer == NULL);
# # # # ]
2234 [ # # # # : 93 : nvme_ctrlr->reconnect_delay_timer = SPDK_POLLER_REGISTER(bdev_nvme_reconnect_delay_timer_expired,
# # # # #
# ]
2235 : : nvme_ctrlr,
2236 : : nvme_ctrlr->opts.reconnect_delay_sec * SPDK_SEC_TO_USEC);
2237 : 93 : }
2238 : :
2239 : : static void remove_discovery_entry(struct nvme_ctrlr *nvme_ctrlr);
2240 : :
2241 : : static void
2242 : 755 : bdev_nvme_reset_ctrlr_complete(struct nvme_ctrlr *nvme_ctrlr, bool success)
2243 : : {
2244 [ # # # # ]: 755 : bdev_nvme_ctrlr_op_cb ctrlr_op_cb_fn = nvme_ctrlr->ctrlr_op_cb_fn;
2245 [ # # # # ]: 755 : void *ctrlr_op_cb_arg = nvme_ctrlr->ctrlr_op_cb_arg;
2246 : : enum bdev_nvme_op_after_reset op_after_reset;
2247 : :
2248 [ + + # # : 755 : assert(nvme_ctrlr->thread == spdk_get_thread());
# # # # ]
2249 : :
2250 [ - + # # ]: 755 : pthread_mutex_lock(&nvme_ctrlr->mutex);
2251 [ + + # # ]: 755 : if (!success) {
2252 : : /* Connecting the active trid failed. Set the next alternate trid to the
2253 : : * active trid if it exists.
2254 : : */
2255 [ + + ]: 534 : if (bdev_nvme_failover_trid(nvme_ctrlr, false, false)) {
2256 : : /* The next alternate trid exists and is ready to try. Try it now. */
2257 [ - + # # ]: 8 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
2258 : :
2259 [ + + - + : 8 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "Try the next alternate trid %s:%s now.\n",
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
2260 : : nvme_ctrlr->active_path_id->trid.traddr,
2261 : : nvme_ctrlr->active_path_id->trid.trsvcid);
2262 : :
2263 : 8 : nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_reconnect_ctrlr);
2264 : 8 : return;
2265 : : }
2266 : :
2267 : : /* We came here if there is no alternate trid or if the next trid exists but
2268 : : * is not ready to try. We will try the active trid after reconnect_delay_sec
2269 : : * seconds if it is non-zero or at the next reset call otherwise.
2270 : : */
2271 : 33 : } else {
2272 : : /* Connecting the active trid succeeded. Clear the last failed time because it
2273 : : * means the trid is failed if its last failed time is non-zero.
2274 : : */
2275 [ # # # # : 221 : nvme_ctrlr->active_path_id->last_failed_tsc = 0;
# # # # ]
2276 : : }
2277 : :
2278 [ + + + + : 747 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "Clear pending resets.\n");
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2279 : :
2280 : : /* Make sure we clear any pending resets before returning. */
2281 [ # # ]: 747 : bdev_nvme_complete_pending_resets(nvme_ctrlr, success);
2282 : :
2283 [ + + # # ]: 747 : if (!success) {
2284 [ + - # # : 526 : NVME_CTRLR_ERRLOG(nvme_ctrlr, "Resetting controller failed.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
2285 : 33 : } else {
2286 [ + + # # : 221 : NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Resetting controller successful.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
2287 : : }
2288 : :
2289 [ # # ]: 747 : nvme_ctrlr->resetting = false;
2290 [ # # ]: 747 : nvme_ctrlr->dont_retry = false;
2291 [ # # ]: 747 : nvme_ctrlr->in_failover = false;
2292 : :
2293 [ # # # # ]: 747 : nvme_ctrlr->ctrlr_op_cb_fn = NULL;
2294 [ # # # # ]: 747 : nvme_ctrlr->ctrlr_op_cb_arg = NULL;
2295 : :
2296 [ # # ]: 747 : op_after_reset = bdev_nvme_check_op_after_reset(nvme_ctrlr, success);
2297 [ - + # # ]: 747 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
2298 : :
2299 : : /* Delay callbacks when the next operation is a failover. */
2300 [ + + + + ]: 747 : if (ctrlr_op_cb_fn && op_after_reset != OP_FAILOVER) {
2301 [ + + # # : 118 : ctrlr_op_cb_fn(ctrlr_op_cb_arg, success ? 0 : -1);
# # ]
2302 : 19 : }
2303 : :
2304 [ + + + + : 747 : switch (op_after_reset) {
+ ]
2305 : 15 : case OP_COMPLETE_PENDING_DESTRUCT:
2306 : 15 : nvme_ctrlr_unregister(nvme_ctrlr);
2307 : 15 : break;
2308 : 29 : case OP_DESTRUCT:
2309 : 31 : bdev_nvme_delete_ctrlr(nvme_ctrlr, false);
2310 : 31 : remove_discovery_entry(nvme_ctrlr);
2311 : 31 : break;
2312 : 81 : case OP_DELAYED_RECONNECT:
2313 : 93 : nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_start_reconnect_delay_timer);
2314 : 93 : break;
2315 : 9 : case OP_FAILOVER:
2316 [ # # # # ]: 12 : nvme_ctrlr->ctrlr_op_cb_fn = ctrlr_op_cb_fn;
2317 [ # # # # ]: 12 : nvme_ctrlr->ctrlr_op_cb_arg = ctrlr_op_cb_arg;
2318 : 12 : bdev_nvme_failover_ctrlr(nvme_ctrlr);
2319 : 12 : break;
2320 : 542 : default:
2321 : 596 : break;
2322 : : }
2323 : 73 : }
2324 : :
2325 : : static void
2326 : 0 : bdev_nvme_reset_create_qpairs_failed(struct nvme_ctrlr *nvme_ctrlr, void *ctx, int status)
2327 : : {
2328 : 0 : bdev_nvme_reset_ctrlr_complete(nvme_ctrlr, false);
2329 : 0 : }
2330 : :
2331 : : static void
2332 : 768 : bdev_nvme_reset_destroy_qpair(struct nvme_ctrlr_channel_iter *i,
2333 : : struct nvme_ctrlr *nvme_ctrlr,
2334 : : struct nvme_ctrlr_channel *ctrlr_ch, void *ctx)
2335 : : {
2336 : : struct nvme_qpair *nvme_qpair;
2337 : : struct spdk_nvme_qpair *qpair;
2338 : :
2339 [ # # # # ]: 768 : nvme_qpair = ctrlr_ch->qpair;
2340 [ + + # # ]: 768 : assert(nvme_qpair != NULL);
2341 : :
2342 : 768 : _bdev_nvme_clear_io_path_cache(nvme_qpair);
2343 : :
2344 [ # # # # ]: 768 : qpair = nvme_qpair->qpair;
2345 [ + + ]: 768 : if (qpair != NULL) {
2346 [ + + - + : 322 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "Start disconnecting qpair %p:%u.\n",
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2347 : : qpair, spdk_nvme_qpair_get_id(qpair));
2348 : :
2349 [ + + # # : 322 : if (nvme_qpair->ctrlr->dont_retry) {
# # # # ]
2350 : 252 : spdk_nvme_qpair_set_abort_dnr(qpair, true);
2351 : 54 : }
2352 : 322 : spdk_nvme_ctrlr_disconnect_io_qpair(qpair);
2353 : :
2354 : : /* The current full reset sequence will move to the next
2355 : : * ctrlr_channel after the qpair is actually disconnected.
2356 : : */
2357 [ - + # # : 322 : assert(ctrlr_ch->reset_iter == NULL);
# # # # ]
2358 [ # # # # ]: 322 : ctrlr_ch->reset_iter = i;
2359 : 70 : } else {
2360 : 446 : nvme_ctrlr_for_each_channel_continue(i, 0);
2361 : : }
2362 : 768 : }
2363 : :
2364 : : static void
2365 : 221 : bdev_nvme_reset_create_qpairs_done(struct nvme_ctrlr *nvme_ctrlr, void *ctx, int status)
2366 : : {
2367 [ + + ]: 221 : if (status == 0) {
2368 [ + + - + : 221 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "qpairs were created after ctrlr reset.\n");
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2369 : :
2370 : 221 : bdev_nvme_reset_ctrlr_complete(nvme_ctrlr, true);
2371 : 38 : } else {
2372 [ # # # # : 0 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "qpairs were failed to create after ctrlr reset.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2373 : :
2374 : : /* Delete the added qpairs and quiesce ctrlr to make the states clean. */
2375 : 0 : nvme_ctrlr_for_each_channel(nvme_ctrlr,
2376 : : bdev_nvme_reset_destroy_qpair,
2377 : : NULL,
2378 : : bdev_nvme_reset_create_qpairs_failed);
2379 : : }
2380 : 221 : }
2381 : :
2382 : : static int
2383 : 13197 : bdev_nvme_reset_check_qpair_connected(void *ctx)
2384 : : {
2385 : 13197 : struct nvme_ctrlr_channel *ctrlr_ch = ctx;
2386 [ # # # # ]: 13197 : struct nvme_qpair *nvme_qpair = ctrlr_ch->qpair;
2387 : : struct spdk_nvme_qpair *qpair;
2388 : :
2389 [ + + # # : 13197 : if (ctrlr_ch->reset_iter == NULL) {
# # ]
2390 : : /* qpair was already failed to connect and the reset sequence is being aborted. */
2391 [ # # # # : 0 : assert(ctrlr_ch->connect_poller == NULL);
# # # # ]
2392 [ # # # # : 0 : assert(nvme_qpair->qpair == NULL);
# # # # ]
2393 : :
2394 [ # # # # : 0 : NVME_CTRLR_INFOLOG(nvme_qpair->ctrlr,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
2395 : : "qpair was already failed to connect. reset is being aborted.\n");
2396 : 0 : return SPDK_POLLER_BUSY;
2397 : : }
2398 : :
2399 [ # # # # ]: 13197 : qpair = nvme_qpair->qpair;
2400 [ + + # # ]: 13197 : assert(qpair != NULL);
2401 : :
2402 [ + + ]: 13197 : if (!spdk_nvme_qpair_is_connected(qpair)) {
2403 : 12893 : return SPDK_POLLER_BUSY;
2404 : : }
2405 : :
2406 [ + + - + : 304 : NVME_CTRLR_INFOLOG(nvme_qpair->ctrlr, "qpair %p:%u was connected.\n",
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
2407 : : qpair, spdk_nvme_qpair_get_id(qpair));
2408 : :
2409 [ # # ]: 304 : spdk_poller_unregister(&ctrlr_ch->connect_poller);
2410 : :
2411 : : /* qpair was completed to connect. Move to the next ctrlr_channel */
2412 [ # # # # ]: 304 : nvme_ctrlr_for_each_channel_continue(ctrlr_ch->reset_iter, 0);
2413 [ # # # # ]: 304 : ctrlr_ch->reset_iter = NULL;
2414 : :
2415 [ + + + + ]: 304 : if (!g_opts.disable_auto_failback) {
2416 : 236 : _bdev_nvme_clear_io_path_cache(nvme_qpair);
2417 : 45 : }
2418 : :
2419 : 304 : return SPDK_POLLER_BUSY;
2420 : 67 : }
2421 : :
2422 : : static void
2423 : 304 : bdev_nvme_reset_create_qpair(struct nvme_ctrlr_channel_iter *i,
2424 : : struct nvme_ctrlr *nvme_ctrlr,
2425 : : struct nvme_ctrlr_channel *ctrlr_ch,
2426 : : void *ctx)
2427 : : {
2428 [ # # # # ]: 304 : struct nvme_qpair *nvme_qpair = ctrlr_ch->qpair;
2429 : : struct spdk_nvme_qpair *qpair;
2430 : 304 : int rc = 0;
2431 : :
2432 [ + + # # : 304 : if (nvme_qpair->qpair == NULL) {
# # ]
2433 : 304 : rc = bdev_nvme_create_qpair(nvme_qpair);
2434 : 62 : }
2435 [ + + ]: 304 : if (rc == 0) {
2436 [ # # # # ]: 304 : ctrlr_ch->connect_poller = SPDK_POLLER_REGISTER(bdev_nvme_reset_check_qpair_connected,
2437 : : ctrlr_ch, 0);
2438 : :
2439 [ # # # # ]: 304 : qpair = nvme_qpair->qpair;
2440 : :
2441 [ + + - + : 304 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "Start checking qpair %p:%u to be connected.\n",
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2442 : : qpair, spdk_nvme_qpair_get_id(qpair));
2443 : :
2444 : : /* The current full reset sequence will move to the next
2445 : : * ctrlr_channel after the qpair is actually connected.
2446 : : */
2447 [ - + # # : 304 : assert(ctrlr_ch->reset_iter == NULL);
# # # # ]
2448 [ # # # # ]: 304 : ctrlr_ch->reset_iter = i;
2449 : 62 : } else {
2450 : 0 : nvme_ctrlr_for_each_channel_continue(i, rc);
2451 : : }
2452 : 304 : }
2453 : :
2454 : : static void
2455 : 221 : nvme_ctrlr_check_namespaces(struct nvme_ctrlr *nvme_ctrlr)
2456 : : {
2457 [ # # # # ]: 221 : struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
2458 : : struct nvme_ns *nvme_ns;
2459 : :
2460 [ + + ]: 244 : for (nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
2461 [ + + ]: 391 : nvme_ns != NULL;
2462 : 170 : nvme_ns = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns)) {
2463 [ + + # # : 170 : if (!spdk_nvme_ctrlr_is_active_ns(ctrlr, nvme_ns->id)) {
# # ]
2464 [ + + - + : 4 : SPDK_DEBUGLOG(bdev_nvme, "NSID %u was removed during reset.\n", nvme_ns->id);
# # # # #
# ]
2465 : : /* NS can be added again. Just nullify nvme_ns->ns. */
2466 [ # # # # ]: 4 : nvme_ns->ns = NULL;
2467 : 1 : }
2468 : 23 : }
2469 : 221 : }
2470 : :
2471 : :
2472 : : static int
2473 : 749603 : bdev_nvme_reconnect_ctrlr_poll(void *arg)
2474 : : {
2475 : 749603 : struct nvme_ctrlr *nvme_ctrlr = arg;
2476 : : struct spdk_nvme_transport_id *trid;
2477 : 749603 : int rc = -ETIMEDOUT;
2478 : :
2479 [ + + ]: 749603 : if (bdev_nvme_check_ctrlr_loss_timeout(nvme_ctrlr)) {
2480 : : /* Mark the ctrlr as failed. The next call to
2481 : : * spdk_nvme_ctrlr_reconnect_poll_async() will then
2482 : : * do the necessary cleanup and return failure.
2483 : : */
2484 [ # # # # ]: 31 : spdk_nvme_ctrlr_fail(nvme_ctrlr->ctrlr);
2485 : 2 : }
2486 : :
2487 [ # # # # ]: 749603 : rc = spdk_nvme_ctrlr_reconnect_poll_async(nvme_ctrlr->ctrlr);
2488 [ + + ]: 749603 : if (rc == -EAGAIN) {
2489 : 748852 : return SPDK_POLLER_BUSY;
2490 : : }
2491 : :
2492 [ # # ]: 751 : spdk_poller_unregister(&nvme_ctrlr->reset_detach_poller);
2493 [ + + ]: 751 : if (rc == 0) {
2494 [ # # # # : 221 : trid = &nvme_ctrlr->active_path_id->trid;
# # ]
2495 : :
2496 [ + + # # : 221 : if (spdk_nvme_trtype_is_fabrics(trid->trtype)) {
# # ]
2497 [ + + - + : 183 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "ctrlr was connected to %s:%s. Create qpairs.\n",
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
2498 : : trid->traddr, trid->trsvcid);
2499 : 36 : } else {
2500 [ + + - + : 38 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "ctrlr was connected. Create qpairs.\n");
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2501 : : }
2502 : :
2503 : 221 : nvme_ctrlr_check_namespaces(nvme_ctrlr);
2504 : :
2505 : : /* Recreate all of the I/O queue pairs */
2506 : 221 : nvme_ctrlr_for_each_channel(nvme_ctrlr,
2507 : : bdev_nvme_reset_create_qpair,
2508 : : NULL,
2509 : : bdev_nvme_reset_create_qpairs_done);
2510 : 38 : } else {
2511 [ + + + + : 530 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "ctrlr could not be connected.\n");
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2512 : :
2513 : 530 : bdev_nvme_reset_ctrlr_complete(nvme_ctrlr, false);
2514 : : }
2515 : 751 : return SPDK_POLLER_BUSY;
2516 : 222 : }
2517 : :
2518 : : static void
2519 : 751 : bdev_nvme_reconnect_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
2520 : : {
2521 [ + + + + : 751 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "Start reconnecting ctrlr.\n");
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2522 : :
2523 [ # # # # ]: 751 : spdk_nvme_ctrlr_reconnect_async(nvme_ctrlr->ctrlr);
2524 : :
2525 : 103 : SPDK_DTRACE_PROBE1(bdev_nvme_ctrlr_reconnect, nvme_ctrlr->nbdev_ctrlr->name);
2526 [ + + # # : 751 : assert(nvme_ctrlr->reset_detach_poller == NULL);
# # # # ]
2527 [ # # # # ]: 751 : nvme_ctrlr->reset_detach_poller = SPDK_POLLER_REGISTER(bdev_nvme_reconnect_ctrlr_poll,
2528 : : nvme_ctrlr, 0);
2529 : 751 : }
2530 : :
2531 : : static void
2532 : 654 : bdev_nvme_reset_destroy_qpair_done(struct nvme_ctrlr *nvme_ctrlr, void *ctx, int status)
2533 : : {
2534 : 67 : SPDK_DTRACE_PROBE1(bdev_nvme_ctrlr_reset, nvme_ctrlr->nbdev_ctrlr->name);
2535 [ + + # # ]: 654 : assert(status == 0);
2536 : :
2537 [ + + + + : 654 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "qpairs were deleted.\n");
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2538 : :
2539 [ + + # # : 654 : if (!spdk_nvme_ctrlr_is_fabrics(nvme_ctrlr->ctrlr)) {
# # ]
2540 : 38 : bdev_nvme_reconnect_ctrlr(nvme_ctrlr);
2541 : 2 : } else {
2542 : 616 : nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_reconnect_ctrlr);
2543 : : }
2544 : 654 : }
2545 : :
2546 : : static void
2547 : 654 : bdev_nvme_reset_destroy_qpairs(struct nvme_ctrlr *nvme_ctrlr)
2548 : : {
2549 [ + + + + : 654 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "Delete qpairs for reset.\n");
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2550 : :
2551 : 654 : nvme_ctrlr_for_each_channel(nvme_ctrlr,
2552 : : bdev_nvme_reset_destroy_qpair,
2553 : : NULL,
2554 : : bdev_nvme_reset_destroy_qpair_done);
2555 : 654 : }
2556 : :
2557 : : static void
2558 : 12 : bdev_nvme_reconnect_ctrlr_now(void *ctx)
2559 : : {
2560 : 12 : struct nvme_ctrlr *nvme_ctrlr = ctx;
2561 : :
2562 [ + + # # : 12 : assert(nvme_ctrlr->resetting == true);
# # ]
2563 [ + + # # : 12 : assert(nvme_ctrlr->thread == spdk_get_thread());
# # # # ]
2564 : :
2565 [ # # ]: 12 : spdk_poller_unregister(&nvme_ctrlr->reconnect_delay_timer);
2566 : :
2567 [ # # # # ]: 12 : spdk_poller_resume(nvme_ctrlr->adminq_timer_poller);
2568 : :
2569 : 12 : bdev_nvme_reconnect_ctrlr(nvme_ctrlr);
2570 : 12 : }
2571 : :
2572 : : static void
2573 : 654 : _bdev_nvme_reset_ctrlr(void *ctx)
2574 : : {
2575 : 654 : struct nvme_ctrlr *nvme_ctrlr = ctx;
2576 : :
2577 [ + + # # : 654 : assert(nvme_ctrlr->resetting == true);
# # ]
2578 [ - + # # : 654 : assert(nvme_ctrlr->thread == spdk_get_thread());
# # # # ]
2579 : :
2580 [ + + # # : 654 : if (!spdk_nvme_ctrlr_is_fabrics(nvme_ctrlr->ctrlr)) {
# # ]
2581 : 38 : nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_reset_destroy_qpairs);
2582 : 2 : } else {
2583 : 616 : bdev_nvme_reset_destroy_qpairs(nvme_ctrlr);
2584 : : }
2585 : 654 : }
2586 : :
2587 : : static int
2588 : 250 : bdev_nvme_reset_ctrlr_unsafe(struct nvme_ctrlr *nvme_ctrlr, spdk_msg_fn *msg_fn)
2589 : : {
2590 [ + + # # ]: 250 : if (nvme_ctrlr->destruct) {
2591 : 12 : return -ENXIO;
2592 : : }
2593 : :
2594 [ + + # # ]: 238 : if (nvme_ctrlr->resetting) {
2595 [ + - # # : 56 : NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Unable to perform reset, already in progress.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
2596 : 56 : return -EBUSY;
2597 : : }
2598 : :
2599 [ + + # # ]: 182 : if (nvme_ctrlr->disabled) {
2600 [ + - # # : 4 : NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Unable to perform reset. Controller is disabled.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
2601 : 4 : return -EALREADY;
2602 : : }
2603 : :
2604 [ # # ]: 178 : nvme_ctrlr->resetting = true;
2605 [ # # ]: 178 : nvme_ctrlr->dont_retry = true;
2606 : :
2607 [ + + # # ]: 178 : if (nvme_ctrlr->reconnect_is_delayed) {
2608 [ + + - + : 4 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "Reconnect is already scheduled.\n");
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
2609 [ # # ]: 4 : *msg_fn = bdev_nvme_reconnect_ctrlr_now;
2610 [ # # ]: 4 : nvme_ctrlr->reconnect_is_delayed = false;
2611 : 1 : } else {
2612 [ # # ]: 174 : *msg_fn = _bdev_nvme_reset_ctrlr;
2613 [ + + # # : 174 : assert(nvme_ctrlr->reset_start_tsc == 0);
# # # # ]
2614 : : }
2615 : :
2616 [ # # # # ]: 178 : nvme_ctrlr->reset_start_tsc = spdk_get_ticks();
2617 : :
2618 : 178 : return 0;
2619 : 52 : }
2620 : :
2621 : : static int
2622 : 106 : bdev_nvme_reset_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
2623 : : {
2624 : 75 : spdk_msg_fn msg_fn;
2625 : : int rc;
2626 : :
2627 [ - + # # ]: 106 : pthread_mutex_lock(&nvme_ctrlr->mutex);
2628 : 106 : rc = bdev_nvme_reset_ctrlr_unsafe(nvme_ctrlr, &msg_fn);
2629 [ - + # # ]: 106 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
2630 : :
2631 [ + + ]: 106 : if (rc == 0) {
2632 [ # # # # ]: 86 : spdk_thread_send_msg(nvme_ctrlr->thread, msg_fn, nvme_ctrlr);
2633 : 20 : }
2634 : :
2635 : 106 : return rc;
2636 : : }
2637 : :
2638 : : static int
2639 : 12 : bdev_nvme_enable_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
2640 : : {
2641 [ - + # # ]: 12 : pthread_mutex_lock(&nvme_ctrlr->mutex);
2642 [ - + # # ]: 12 : if (nvme_ctrlr->destruct) {
2643 [ # # # # ]: 0 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
2644 : 0 : return -ENXIO;
2645 : : }
2646 : :
2647 [ - + # # ]: 12 : if (nvme_ctrlr->resetting) {
2648 [ # # # # ]: 0 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
2649 : 0 : return -EBUSY;
2650 : : }
2651 : :
2652 [ + + # # ]: 12 : if (!nvme_ctrlr->disabled) {
2653 [ - + # # ]: 4 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
2654 : 4 : return -EALREADY;
2655 : : }
2656 : :
2657 [ # # ]: 8 : nvme_ctrlr->disabled = false;
2658 [ # # ]: 8 : nvme_ctrlr->resetting = true;
2659 : :
2660 [ # # # # ]: 8 : nvme_ctrlr->reset_start_tsc = spdk_get_ticks();
2661 : :
2662 [ - + # # ]: 8 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
2663 : :
2664 [ # # # # ]: 8 : spdk_thread_send_msg(nvme_ctrlr->thread, bdev_nvme_reconnect_ctrlr_now, nvme_ctrlr);
2665 : 8 : return 0;
2666 : 3 : }
2667 : :
2668 : : static void
2669 : 8 : bdev_nvme_disable_ctrlr_complete(struct nvme_ctrlr *nvme_ctrlr)
2670 : : {
2671 [ # # # # ]: 8 : bdev_nvme_ctrlr_op_cb ctrlr_op_cb_fn = nvme_ctrlr->ctrlr_op_cb_fn;
2672 [ # # # # ]: 8 : void *ctrlr_op_cb_arg = nvme_ctrlr->ctrlr_op_cb_arg;
2673 : : enum bdev_nvme_op_after_reset op_after_disable;
2674 : :
2675 [ + + # # : 8 : assert(nvme_ctrlr->thread == spdk_get_thread());
# # # # ]
2676 : :
2677 [ # # # # ]: 8 : nvme_ctrlr->ctrlr_op_cb_fn = NULL;
2678 [ # # # # ]: 8 : nvme_ctrlr->ctrlr_op_cb_arg = NULL;
2679 : :
2680 [ - + # # ]: 8 : pthread_mutex_lock(&nvme_ctrlr->mutex);
2681 : :
2682 [ # # ]: 8 : nvme_ctrlr->resetting = false;
2683 [ # # ]: 8 : nvme_ctrlr->dont_retry = false;
2684 : :
2685 : 8 : op_after_disable = bdev_nvme_check_op_after_reset(nvme_ctrlr, true);
2686 : :
2687 [ # # ]: 8 : nvme_ctrlr->disabled = true;
2688 [ # # # # ]: 8 : spdk_poller_pause(nvme_ctrlr->adminq_timer_poller);
2689 : :
2690 : : /* Make sure we clear any pending resets before returning. */
2691 : 8 : bdev_nvme_complete_pending_resets(nvme_ctrlr, true);
2692 : :
2693 [ - + # # ]: 8 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
2694 : :
2695 [ + + ]: 8 : if (ctrlr_op_cb_fn) {
2696 [ # # # # ]: 0 : ctrlr_op_cb_fn(ctrlr_op_cb_arg, 0);
2697 : 0 : }
2698 : :
2699 [ + + ]: 8 : switch (op_after_disable) {
2700 : 0 : case OP_COMPLETE_PENDING_DESTRUCT:
2701 : 0 : nvme_ctrlr_unregister(nvme_ctrlr);
2702 : 0 : break;
2703 : 6 : default:
2704 : 8 : break;
2705 : : }
2706 : 8 : }
2707 : :
2708 : : static void
2709 : 4 : bdev_nvme_disable_destroy_qpairs_done(struct nvme_ctrlr *nvme_ctrlr, void *ctx, int status)
2710 : : {
2711 [ + + # # ]: 4 : assert(status == 0);
2712 : :
2713 [ + + # # : 4 : if (!spdk_nvme_ctrlr_is_fabrics(nvme_ctrlr->ctrlr)) {
# # ]
2714 : 0 : bdev_nvme_disable_ctrlr_complete(nvme_ctrlr);
2715 : 0 : } else {
2716 : 4 : nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_disable_ctrlr_complete);
2717 : : }
2718 : 4 : }
2719 : :
2720 : : static void
2721 : 4 : bdev_nvme_disable_destroy_qpairs(struct nvme_ctrlr *nvme_ctrlr)
2722 : : {
2723 : 4 : nvme_ctrlr_for_each_channel(nvme_ctrlr,
2724 : : bdev_nvme_reset_destroy_qpair,
2725 : : NULL,
2726 : : bdev_nvme_disable_destroy_qpairs_done);
2727 : 4 : }
2728 : :
2729 : : static void
2730 : 4 : _bdev_nvme_cancel_reconnect_and_disable_ctrlr(void *ctx)
2731 : : {
2732 : 4 : struct nvme_ctrlr *nvme_ctrlr = ctx;
2733 : :
2734 [ + + # # : 4 : assert(nvme_ctrlr->resetting == true);
# # ]
2735 [ + + # # : 4 : assert(nvme_ctrlr->thread == spdk_get_thread());
# # # # ]
2736 : :
2737 [ # # ]: 4 : spdk_poller_unregister(&nvme_ctrlr->reconnect_delay_timer);
2738 : :
2739 : 4 : bdev_nvme_disable_ctrlr_complete(nvme_ctrlr);
2740 : 4 : }
2741 : :
2742 : : static void
2743 : 4 : _bdev_nvme_disconnect_and_disable_ctrlr(void *ctx)
2744 : : {
2745 : 4 : struct nvme_ctrlr *nvme_ctrlr = ctx;
2746 : :
2747 [ + + # # : 4 : assert(nvme_ctrlr->resetting == true);
# # ]
2748 [ - + # # : 4 : assert(nvme_ctrlr->thread == spdk_get_thread());
# # # # ]
2749 : :
2750 [ + + # # : 4 : if (!spdk_nvme_ctrlr_is_fabrics(nvme_ctrlr->ctrlr)) {
# # ]
2751 : 0 : nvme_ctrlr_disconnect(nvme_ctrlr, bdev_nvme_disable_destroy_qpairs);
2752 : 0 : } else {
2753 : 4 : bdev_nvme_disable_destroy_qpairs(nvme_ctrlr);
2754 : : }
2755 : 4 : }
2756 : :
2757 : : static int
2758 : 20 : bdev_nvme_disable_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
2759 : : {
2760 : : spdk_msg_fn msg_fn;
2761 : :
2762 [ - + # # ]: 20 : pthread_mutex_lock(&nvme_ctrlr->mutex);
2763 [ + + # # ]: 20 : if (nvme_ctrlr->destruct) {
2764 [ - + # # ]: 4 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
2765 : 4 : return -ENXIO;
2766 : : }
2767 : :
2768 [ + + # # ]: 16 : if (nvme_ctrlr->resetting) {
2769 [ - + # # ]: 4 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
2770 : 4 : return -EBUSY;
2771 : : }
2772 : :
2773 [ + + # # ]: 12 : if (nvme_ctrlr->disabled) {
2774 [ - + # # ]: 4 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
2775 : 4 : return -EALREADY;
2776 : : }
2777 : :
2778 [ # # ]: 8 : nvme_ctrlr->resetting = true;
2779 [ # # ]: 8 : nvme_ctrlr->dont_retry = true;
2780 : :
2781 [ + + # # ]: 8 : if (nvme_ctrlr->reconnect_is_delayed) {
2782 : 4 : msg_fn = _bdev_nvme_cancel_reconnect_and_disable_ctrlr;
2783 [ # # ]: 4 : nvme_ctrlr->reconnect_is_delayed = false;
2784 : 1 : } else {
2785 : 4 : msg_fn = _bdev_nvme_disconnect_and_disable_ctrlr;
2786 : : }
2787 : :
2788 [ # # # # ]: 8 : nvme_ctrlr->reset_start_tsc = spdk_get_ticks();
2789 : :
2790 [ - + # # ]: 8 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
2791 : :
2792 [ # # # # ]: 8 : spdk_thread_send_msg(nvme_ctrlr->thread, msg_fn, nvme_ctrlr);
2793 : 8 : return 0;
2794 : 5 : }
2795 : :
2796 : : static int
2797 : 34 : nvme_ctrlr_op(struct nvme_ctrlr *nvme_ctrlr, enum nvme_ctrlr_op op,
2798 : : bdev_nvme_ctrlr_op_cb cb_fn, void *cb_arg)
2799 : : {
2800 : : int rc;
2801 : :
2802 [ + + - + ]: 34 : switch (op) {
2803 : 24 : case NVME_CTRLR_OP_RESET:
2804 : 30 : rc = bdev_nvme_reset_ctrlr(nvme_ctrlr);
2805 : 30 : break;
2806 : 0 : case NVME_CTRLR_OP_ENABLE:
2807 : 0 : rc = bdev_nvme_enable_ctrlr(nvme_ctrlr);
2808 : 0 : break;
2809 : 0 : case NVME_CTRLR_OP_DISABLE:
2810 : 0 : rc = bdev_nvme_disable_ctrlr(nvme_ctrlr);
2811 : 0 : break;
2812 : 3 : default:
2813 : 4 : rc = -EINVAL;
2814 : 4 : break;
2815 : : }
2816 : :
2817 [ + + ]: 34 : if (rc == 0) {
2818 [ + + # # : 22 : assert(nvme_ctrlr->ctrlr_op_cb_fn == NULL);
# # # # ]
2819 [ - + # # : 22 : assert(nvme_ctrlr->ctrlr_op_cb_arg == NULL);
# # # # ]
2820 [ # # # # ]: 22 : nvme_ctrlr->ctrlr_op_cb_fn = cb_fn;
2821 [ # # # # ]: 22 : nvme_ctrlr->ctrlr_op_cb_arg = cb_arg;
2822 : 4 : }
2823 : 34 : return rc;
2824 : : }
2825 : :
2826 : : struct nvme_ctrlr_op_rpc_ctx {
2827 : : struct nvme_ctrlr *nvme_ctrlr;
2828 : : struct spdk_thread *orig_thread;
2829 : : enum nvme_ctrlr_op op;
2830 : : int rc;
2831 : : bdev_nvme_ctrlr_op_cb cb_fn;
2832 : : void *cb_arg;
2833 : : };
2834 : :
2835 : : static void
2836 : 16 : _nvme_ctrlr_op_rpc_complete(void *_ctx)
2837 : : {
2838 : 16 : struct nvme_ctrlr_op_rpc_ctx *ctx = _ctx;
2839 : :
2840 [ + + # # ]: 16 : assert(ctx != NULL);
2841 [ + + # # : 16 : assert(ctx->cb_fn != NULL);
# # # # ]
2842 : :
2843 [ # # # # : 16 : ctx->cb_fn(ctx->cb_arg, ctx->rc);
# # # # #
# # # # #
# # ]
2844 : :
2845 : 16 : free(ctx);
2846 : 16 : }
2847 : :
2848 : : static void
2849 : 16 : nvme_ctrlr_op_rpc_complete(void *cb_arg, int rc)
2850 : : {
2851 : 16 : struct nvme_ctrlr_op_rpc_ctx *ctx = cb_arg;
2852 : :
2853 [ # # # # ]: 16 : ctx->rc = rc;
2854 : :
2855 [ # # # # ]: 16 : spdk_thread_send_msg(ctx->orig_thread, _nvme_ctrlr_op_rpc_complete, ctx);
2856 : 16 : }
2857 : :
2858 : : void
2859 : 16 : nvme_ctrlr_op_rpc(struct nvme_ctrlr *nvme_ctrlr, enum nvme_ctrlr_op op,
2860 : : bdev_nvme_ctrlr_op_cb cb_fn, void *cb_arg)
2861 : : {
2862 : : struct nvme_ctrlr_op_rpc_ctx *ctx;
2863 : : int rc;
2864 : :
2865 [ + + # # ]: 16 : assert(cb_fn != NULL);
2866 : :
2867 : 16 : ctx = calloc(1, sizeof(*ctx));
2868 [ + + ]: 16 : if (ctx == NULL) {
2869 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to allocate nvme_ctrlr_op_rpc_ctx.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
2870 [ # # # # ]: 0 : cb_fn(cb_arg, -ENOMEM);
2871 : 0 : return;
2872 : : }
2873 : :
2874 [ # # # # ]: 16 : ctx->orig_thread = spdk_get_thread();
2875 [ # # # # ]: 16 : ctx->cb_fn = cb_fn;
2876 [ # # # # ]: 16 : ctx->cb_arg = cb_arg;
2877 : :
2878 : 16 : rc = nvme_ctrlr_op(nvme_ctrlr, op, nvme_ctrlr_op_rpc_complete, ctx);
2879 [ + + ]: 16 : if (rc == 0) {
2880 : 4 : return;
2881 [ + + ]: 12 : } else if (rc == -EALREADY) {
2882 : 0 : rc = 0;
2883 : 0 : }
2884 : :
2885 : 12 : nvme_ctrlr_op_rpc_complete(ctx, rc);
2886 : 4 : }
2887 : :
2888 : : static void nvme_bdev_ctrlr_op_rpc_continue(void *cb_arg, int rc);
2889 : :
2890 : : static void
2891 : 18 : _nvme_bdev_ctrlr_op_rpc_continue(void *_ctx)
2892 : : {
2893 : 18 : struct nvme_ctrlr_op_rpc_ctx *ctx = _ctx;
2894 : : struct nvme_ctrlr *prev_nvme_ctrlr, *next_nvme_ctrlr;
2895 : : int rc;
2896 : :
2897 [ # # # # ]: 18 : prev_nvme_ctrlr = ctx->nvme_ctrlr;
2898 [ # # # # ]: 18 : ctx->nvme_ctrlr = NULL;
2899 : :
2900 [ - + # # : 18 : if (ctx->rc != 0) {
# # ]
2901 : 0 : goto complete;
2902 : : }
2903 : :
2904 [ # # # # : 18 : next_nvme_ctrlr = TAILQ_NEXT(prev_nvme_ctrlr, tailq);
# # ]
2905 [ + + ]: 18 : if (next_nvme_ctrlr == NULL) {
2906 : 14 : goto complete;
2907 : : }
2908 : :
2909 [ # # # # ]: 4 : rc = nvme_ctrlr_op(next_nvme_ctrlr, ctx->op, nvme_bdev_ctrlr_op_rpc_continue, ctx);
2910 [ + + ]: 4 : if (rc == 0) {
2911 [ # # # # ]: 4 : ctx->nvme_ctrlr = next_nvme_ctrlr;
2912 : 4 : return;
2913 [ # # ]: 0 : } else if (rc == -EALREADY) {
2914 [ # # # # ]: 0 : ctx->nvme_ctrlr = next_nvme_ctrlr;
2915 : 0 : rc = 0;
2916 : 0 : }
2917 : :
2918 [ # # # # ]: 0 : ctx->rc = rc;
2919 : :
2920 : 12 : complete:
2921 [ # # # # : 14 : ctx->cb_fn(ctx->cb_arg, ctx->rc);
# # # # #
# # # # #
# # ]
2922 : 14 : free(ctx);
2923 : 3 : }
2924 : :
2925 : : static void
2926 : 18 : nvme_bdev_ctrlr_op_rpc_continue(void *cb_arg, int rc)
2927 : : {
2928 : 18 : struct nvme_ctrlr_op_rpc_ctx *ctx = cb_arg;
2929 : :
2930 [ # # # # ]: 18 : ctx->rc = rc;
2931 : :
2932 [ # # # # ]: 18 : spdk_thread_send_msg(ctx->orig_thread, _nvme_bdev_ctrlr_op_rpc_continue, ctx);
2933 : 18 : }
2934 : :
2935 : : void
2936 : 14 : nvme_bdev_ctrlr_op_rpc(struct nvme_bdev_ctrlr *nbdev_ctrlr, enum nvme_ctrlr_op op,
2937 : : bdev_nvme_ctrlr_op_cb cb_fn, void *cb_arg)
2938 : : {
2939 : : struct nvme_ctrlr_op_rpc_ctx *ctx;
2940 : : struct nvme_ctrlr *nvme_ctrlr;
2941 : : int rc;
2942 : :
2943 [ + + # # ]: 14 : assert(cb_fn != NULL);
2944 : :
2945 : 14 : ctx = calloc(1, sizeof(*ctx));
2946 [ + + ]: 14 : if (ctx == NULL) {
2947 : 0 : SPDK_ERRLOG("Failed to allocate nvme_ctrlr_op_rpc_ctx.\n");
2948 [ # # # # ]: 0 : cb_fn(cb_arg, -ENOMEM);
2949 : 0 : return;
2950 : : }
2951 : :
2952 [ # # # # ]: 14 : ctx->orig_thread = spdk_get_thread();
2953 [ # # # # ]: 14 : ctx->op = op;
2954 [ # # # # ]: 14 : ctx->cb_fn = cb_fn;
2955 [ # # # # ]: 14 : ctx->cb_arg = cb_arg;
2956 : :
2957 [ # # # # : 14 : nvme_ctrlr = TAILQ_FIRST(&nbdev_ctrlr->ctrlrs);
# # ]
2958 [ - + # # ]: 14 : assert(nvme_ctrlr != NULL);
2959 : :
2960 : 14 : rc = nvme_ctrlr_op(nvme_ctrlr, op, nvme_bdev_ctrlr_op_rpc_continue, ctx);
2961 [ + + ]: 14 : if (rc == 0) {
2962 [ # # # # ]: 14 : ctx->nvme_ctrlr = nvme_ctrlr;
2963 : 14 : return;
2964 [ # # ]: 0 : } else if (rc == -EALREADY) {
2965 [ # # # # ]: 0 : ctx->nvme_ctrlr = nvme_ctrlr;
2966 : 0 : rc = 0;
2967 : 0 : }
2968 : :
2969 : 0 : nvme_bdev_ctrlr_op_rpc_continue(ctx, rc);
2970 : 2 : }
2971 : :
2972 : : static int _bdev_nvme_reset_io(struct nvme_io_path *io_path, struct nvme_bdev_io *bio);
2973 : :
2974 : : static void
2975 : 104 : bdev_nvme_unfreeze_bdev_channel_done(struct nvme_bdev *nbdev, void *ctx, int status)
2976 : : {
2977 : 104 : struct nvme_bdev_io *bio = ctx;
2978 : : enum spdk_bdev_io_status io_status;
2979 : :
2980 [ + + # # : 104 : if (bio->cpl.cdw0 == 0) {
# # # # ]
2981 : 88 : io_status = SPDK_BDEV_IO_STATUS_SUCCESS;
2982 : 13 : } else {
2983 : 16 : io_status = SPDK_BDEV_IO_STATUS_FAILED;
2984 : : }
2985 : :
2986 [ + + - + : 104 : NVME_BDEV_INFOLOG(nbdev, "reset_io %p completed, status:%d\n", bio, io_status);
# # # # #
# # # ]
2987 : :
2988 : 104 : __bdev_nvme_io_complete(spdk_bdev_io_from_ctx(bio), io_status, NULL);
2989 : 104 : }
2990 : :
2991 : : static void
2992 : 168 : bdev_nvme_unfreeze_bdev_channel(struct nvme_bdev_channel_iter *i,
2993 : : struct nvme_bdev *nbdev,
2994 : : struct nvme_bdev_channel *nbdev_ch, void *ctx)
2995 : : {
2996 : 168 : bdev_nvme_abort_retry_ios(nbdev_ch);
2997 [ # # # # ]: 168 : nbdev_ch->resetting = false;
2998 : :
2999 : 168 : nvme_bdev_for_each_channel_continue(i, 0);
3000 : 168 : }
3001 : :
3002 : : static void
3003 : 104 : bdev_nvme_reset_io_complete(struct nvme_bdev_io *bio)
3004 : : {
3005 : 104 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
3006 [ # # # # : 104 : struct nvme_bdev *nbdev = (struct nvme_bdev *)bdev_io->bdev->ctxt;
# # # # ]
3007 : :
3008 : : /* Abort all queued I/Os for retry. */
3009 : 121 : nvme_bdev_for_each_channel(nbdev,
3010 : : bdev_nvme_unfreeze_bdev_channel,
3011 : 17 : bio,
3012 : : bdev_nvme_unfreeze_bdev_channel_done);
3013 : 104 : }
3014 : :
3015 : : static void
3016 : 144 : _bdev_nvme_reset_io_continue(void *ctx)
3017 : : {
3018 : 144 : struct nvme_bdev_io *bio = ctx;
3019 : : struct nvme_io_path *prev_io_path, *next_io_path;
3020 : : int rc;
3021 : :
3022 [ # # # # ]: 144 : prev_io_path = bio->io_path;
3023 [ # # # # ]: 144 : bio->io_path = NULL;
3024 : :
3025 [ # # # # : 144 : next_io_path = STAILQ_NEXT(prev_io_path, stailq);
# # ]
3026 [ + + ]: 144 : if (next_io_path == NULL) {
3027 : 104 : goto complete;
3028 : : }
3029 : :
3030 : 40 : rc = _bdev_nvme_reset_io(next_io_path, bio);
3031 [ + - ]: 40 : if (rc == 0) {
3032 : 40 : return;
3033 : : }
3034 : :
3035 : 0 : complete:
3036 : 104 : bdev_nvme_reset_io_complete(bio);
3037 : 27 : }
3038 : :
3039 : : static void
3040 : 144 : bdev_nvme_reset_io_continue(void *cb_arg, int rc)
3041 : : {
3042 : 144 : struct nvme_bdev_io *bio = cb_arg;
3043 : 144 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
3044 [ # # # # : 144 : struct nvme_bdev *nbdev = (struct nvme_bdev *)bdev_io->bdev->ctxt;
# # # # ]
3045 : :
3046 [ + + - + : 144 : NVME_BDEV_INFOLOG(nbdev, "continue reset_io %p, rc:%d\n", bio, rc);
# # # # #
# # # ]
3047 : :
3048 : : /* Reset status is initialized as "failed". Set to "success" once we have at least one
3049 : : * successfully reset nvme_ctrlr.
3050 : : */
3051 [ + + ]: 144 : if (rc == 0) {
3052 [ # # # # : 104 : bio->cpl.cdw0 = 0;
# # ]
3053 : 17 : }
3054 : :
3055 : 144 : spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io), _bdev_nvme_reset_io_continue, bio);
3056 : 144 : }
3057 : :
3058 : : static int
3059 : 144 : _bdev_nvme_reset_io(struct nvme_io_path *io_path, struct nvme_bdev_io *bio)
3060 : : {
3061 : 144 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
3062 [ # # # # : 144 : struct nvme_bdev *nbdev = (struct nvme_bdev *)bdev_io->bdev->ctxt;
# # # # ]
3063 [ # # # # : 144 : struct nvme_ctrlr *nvme_ctrlr = io_path->qpair->ctrlr;
# # # # ]
3064 : 97 : spdk_msg_fn msg_fn;
3065 : : int rc;
3066 : :
3067 [ + + # # : 144 : assert(bio->io_path == NULL);
# # # # ]
3068 [ # # # # ]: 144 : bio->io_path = io_path;
3069 : :
3070 [ - + # # ]: 144 : pthread_mutex_lock(&nvme_ctrlr->mutex);
3071 : 144 : rc = bdev_nvme_reset_ctrlr_unsafe(nvme_ctrlr, &msg_fn);
3072 [ + + ]: 144 : if (rc == -EBUSY) {
3073 : : /*
3074 : : * Reset call is queued only if it is from the app framework. This is on purpose so that
3075 : : * we don't interfere with the app framework reset strategy. i.e. we are deferring to the
3076 : : * upper level. If they are in the middle of a reset, we won't try to schedule another one.
3077 : : */
3078 [ # # # # : 48 : TAILQ_INSERT_TAIL(&nvme_ctrlr->pending_resets, bio, retry_link);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
3079 : 12 : }
3080 [ - + # # ]: 144 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
3081 : :
3082 [ + + ]: 144 : if (rc == 0) {
3083 [ + + # # : 92 : assert(nvme_ctrlr->ctrlr_op_cb_fn == NULL);
# # # # ]
3084 [ + + # # : 92 : assert(nvme_ctrlr->ctrlr_op_cb_arg == NULL);
# # # # ]
3085 [ # # # # ]: 92 : nvme_ctrlr->ctrlr_op_cb_fn = bdev_nvme_reset_io_continue;
3086 [ # # # # ]: 92 : nvme_ctrlr->ctrlr_op_cb_arg = bio;
3087 : :
3088 [ # # # # ]: 92 : spdk_thread_send_msg(nvme_ctrlr->thread, msg_fn, nvme_ctrlr);
3089 : :
3090 [ + + - + : 92 : NVME_BDEV_INFOLOG(nbdev, "reset_io %p started resetting ctrlr [%s, %u].\n",
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
3091 : : bio, CTRLR_STRING(nvme_ctrlr), CTRLR_ID(nvme_ctrlr));
3092 [ + + ]: 66 : } else if (rc == -EBUSY) {
3093 : 48 : rc = 0;
3094 : :
3095 [ + + - + : 48 : NVME_BDEV_INFOLOG(nbdev, "reset_io %p was queued to ctrlr [%s, %u].\n",
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
3096 : : bio, CTRLR_STRING(nvme_ctrlr), CTRLR_ID(nvme_ctrlr));
3097 : 12 : } else {
3098 [ + + - + : 4 : NVME_BDEV_INFOLOG(nbdev, "reset_io %p could not reset ctrlr [%s, %u], rc:%d\n",
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
3099 : : bio, CTRLR_STRING(nvme_ctrlr), CTRLR_ID(nvme_ctrlr), rc);
3100 : : }
3101 : :
3102 : 144 : return rc;
3103 : : }
3104 : :
3105 : : static void
3106 : 104 : bdev_nvme_freeze_bdev_channel_done(struct nvme_bdev *nbdev, void *ctx, int status)
3107 : : {
3108 : 104 : struct nvme_bdev_io *bio = ctx;
3109 : 104 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
3110 : : struct nvme_bdev_channel *nbdev_ch;
3111 : : struct nvme_io_path *io_path;
3112 : : int rc;
3113 : :
3114 : 104 : nbdev_ch = spdk_io_channel_get_ctx(spdk_bdev_io_get_io_channel(bdev_io));
3115 : :
3116 : : /* Initialize with failed status. With multipath it is enough to have at least one successful
3117 : : * nvme_ctrlr reset. If there is none, reset status will remain failed.
3118 : : */
3119 [ # # # # : 104 : bio->cpl.cdw0 = 1;
# # ]
3120 : :
3121 : : /* Reset all nvme_ctrlrs of a bdev controller sequentially. */
3122 [ # # # # : 104 : io_path = STAILQ_FIRST(&nbdev_ch->io_path_list);
# # ]
3123 [ + + # # ]: 104 : assert(io_path != NULL);
3124 : :
3125 : 104 : rc = _bdev_nvme_reset_io(io_path, bio);
3126 [ + + ]: 104 : if (rc != 0) {
3127 : : /* If the current nvme_ctrlr is disabled, skip it and move to the next nvme_ctrlr. */
3128 [ + + ]: 4 : rc = (rc == -EALREADY) ? 0 : rc;
3129 : :
3130 : 4 : bdev_nvme_reset_io_continue(bio, rc);
3131 : 1 : }
3132 : 104 : }
3133 : :
3134 : : static void
3135 : 160 : bdev_nvme_freeze_bdev_channel(struct nvme_bdev_channel_iter *i,
3136 : : struct nvme_bdev *nbdev,
3137 : : struct nvme_bdev_channel *nbdev_ch, void *ctx)
3138 : : {
3139 [ # # # # ]: 160 : nbdev_ch->resetting = true;
3140 : :
3141 : 160 : nvme_bdev_for_each_channel_continue(i, 0);
3142 : 160 : }
3143 : :
3144 : : static void
3145 : 100 : bdev_nvme_reset_io(struct nvme_bdev *nbdev, struct nvme_bdev_io *bio)
3146 : : {
3147 [ + + - + : 100 : NVME_BDEV_INFOLOG(nbdev, "reset_io %p started.\n", bio);
# # # # #
# # # ]
3148 : :
3149 : 116 : nvme_bdev_for_each_channel(nbdev,
3150 : : bdev_nvme_freeze_bdev_channel,
3151 : 16 : bio,
3152 : : bdev_nvme_freeze_bdev_channel_done);
3153 : 100 : }
3154 : :
3155 : : static int
3156 : 945 : bdev_nvme_failover_ctrlr_unsafe(struct nvme_ctrlr *nvme_ctrlr, bool remove)
3157 : : {
3158 [ + + # # ]: 945 : if (nvme_ctrlr->destruct) {
3159 : : /* Don't bother resetting if the controller is in the process of being destructed. */
3160 : 411 : return -ENXIO;
3161 : : }
3162 : :
3163 [ + + # # ]: 534 : if (nvme_ctrlr->resetting) {
3164 [ + + # # ]: 50 : if (!nvme_ctrlr->in_failover) {
3165 [ + - # # : 12 : NVME_CTRLR_NOTICELOG(nvme_ctrlr,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
3166 : : "Reset is already in progress. Defer failover until reset completes.\n");
3167 : :
3168 : : /* Defer failover until reset completes. */
3169 [ # # ]: 12 : nvme_ctrlr->pending_failover = true;
3170 : 12 : return -EINPROGRESS;
3171 : : } else {
3172 [ + - # # : 38 : NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Unable to perform failover, already in progress.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
3173 : 38 : return -EBUSY;
3174 : : }
3175 : : }
3176 : :
3177 [ # # ]: 484 : bdev_nvme_failover_trid(nvme_ctrlr, remove, true);
3178 : :
3179 [ + + # # ]: 484 : if (nvme_ctrlr->reconnect_is_delayed) {
3180 [ + - # # : 4 : NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Reconnect is already scheduled.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
3181 : :
3182 : : /* We rely on the next reconnect for the failover. */
3183 : 4 : return -EALREADY;
3184 : : }
3185 : :
3186 [ - + # # ]: 480 : if (nvme_ctrlr->disabled) {
3187 [ # # # # : 0 : NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Controller is disabled.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
3188 : :
3189 : : /* We rely on the enablement for the failover. */
3190 : 0 : return -EALREADY;
3191 : : }
3192 : :
3193 [ # # ]: 480 : nvme_ctrlr->resetting = true;
3194 [ # # ]: 480 : nvme_ctrlr->in_failover = true;
3195 : :
3196 [ + + # # : 480 : assert(nvme_ctrlr->reset_start_tsc == 0);
# # # # ]
3197 [ # # # # ]: 480 : nvme_ctrlr->reset_start_tsc = spdk_get_ticks();
3198 : :
3199 : 480 : return 0;
3200 : 32 : }
3201 : :
3202 : : static int
3203 : 933 : bdev_nvme_failover_ctrlr(struct nvme_ctrlr *nvme_ctrlr)
3204 : : {
3205 : : int rc;
3206 : :
3207 [ - + # # ]: 933 : pthread_mutex_lock(&nvme_ctrlr->mutex);
3208 : 933 : rc = bdev_nvme_failover_ctrlr_unsafe(nvme_ctrlr, false);
3209 [ - + # # ]: 933 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
3210 : :
3211 [ + + ]: 933 : if (rc == 0) {
3212 [ # # # # ]: 472 : spdk_thread_send_msg(nvme_ctrlr->thread, _bdev_nvme_reset_ctrlr, nvme_ctrlr);
3213 [ + + ]: 486 : } else if (rc == -EALREADY) {
3214 : 0 : rc = 0;
3215 : 0 : }
3216 : :
3217 : 933 : return rc;
3218 : : }
3219 : :
3220 : : static int bdev_nvme_unmap(struct nvme_bdev_io *bio, uint64_t offset_blocks,
3221 : : uint64_t num_blocks);
3222 : :
3223 : : static int bdev_nvme_write_zeroes(struct nvme_bdev_io *bio, uint64_t offset_blocks,
3224 : : uint64_t num_blocks);
3225 : :
3226 : : static int bdev_nvme_copy(struct nvme_bdev_io *bio, uint64_t dst_offset_blocks,
3227 : : uint64_t src_offset_blocks,
3228 : : uint64_t num_blocks);
3229 : :
3230 : : static void
3231 : 6372050 : bdev_nvme_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
3232 : : bool success)
3233 : : {
3234 [ # # ]: 6372050 : struct nvme_bdev_io *bio = (struct nvme_bdev_io *)bdev_io->driver_ctx;
3235 : : int ret;
3236 : :
3237 [ + + # # ]: 6372050 : if (!success) {
3238 : 0 : ret = -EINVAL;
3239 : 0 : goto exit;
3240 : : }
3241 : :
3242 [ + + # # : 6372050 : if (spdk_unlikely(!nvme_io_path_is_available(bio->io_path))) {
# # ]
3243 : 0 : ret = -ENXIO;
3244 : 0 : goto exit;
3245 : : }
3246 : :
3247 : 6529355 : ret = bdev_nvme_readv(bio,
3248 [ # # # # : 157305 : bdev_io->u.bdev.iovs,
# # # # ]
3249 [ # # # # : 157305 : bdev_io->u.bdev.iovcnt,
# # # # ]
3250 [ # # # # : 157305 : bdev_io->u.bdev.md_buf,
# # # # ]
3251 [ # # # # : 157305 : bdev_io->u.bdev.num_blocks,
# # # # ]
3252 [ # # # # : 157305 : bdev_io->u.bdev.offset_blocks,
# # # # ]
3253 [ # # # # : 157305 : bdev_io->u.bdev.dif_check_flags,
# # # # ]
3254 [ # # # # : 157305 : bdev_io->u.bdev.memory_domain,
# # # # ]
3255 [ # # # # : 157305 : bdev_io->u.bdev.memory_domain_ctx,
# # # # ]
3256 [ # # # # : 157305 : bdev_io->u.bdev.accel_sequence);
# # # # ]
3257 : :
3258 : 6214745 : exit:
3259 [ + + ]: 6372050 : if (spdk_unlikely(ret != 0)) {
3260 : 88879 : bdev_nvme_io_complete(bio, ret);
3261 : 0 : }
3262 : 6372050 : }
3263 : :
3264 : : static inline void
3265 : 28426827 : _bdev_nvme_submit_request(struct nvme_bdev_channel *nbdev_ch, struct spdk_bdev_io *bdev_io)
3266 : : {
3267 [ + - ]: 28426827 : struct nvme_bdev_io *nbdev_io = (struct nvme_bdev_io *)bdev_io->driver_ctx;
3268 [ + - + - ]: 28426827 : struct spdk_bdev *bdev = bdev_io->bdev;
3269 : : struct nvme_bdev_io *nbdev_io_to_abort;
3270 : 28426827 : int rc = 0;
3271 : :
3272 [ + + + + : 28426827 : switch (bdev_io->type) {
+ + + + +
+ + + + +
- + + + -
- - - ]
3273 : 12658757 : case SPDK_BDEV_IO_TYPE_READ:
3274 [ + + + + : 12816284 : if (bdev_io->u.bdev.iovs && bdev_io->u.bdev.iovs[0].iov_base) {
+ - + - +
- + - + -
+ - + - +
- + - + -
- + ]
3275 : :
3276 : 6444270 : rc = bdev_nvme_readv(nbdev_io,
3277 [ - + - + : 129 : bdev_io->u.bdev.iovs,
- + - + ]
3278 [ - + - + : 129 : bdev_io->u.bdev.iovcnt,
- + - + ]
3279 [ - + - + : 129 : bdev_io->u.bdev.md_buf,
- + - + ]
3280 [ - + - + : 129 : bdev_io->u.bdev.num_blocks,
- + - + ]
3281 [ - + - + : 129 : bdev_io->u.bdev.offset_blocks,
- + - + ]
3282 [ - + - + : 129 : bdev_io->u.bdev.dif_check_flags,
- + - + ]
3283 [ - + - + : 129 : bdev_io->u.bdev.memory_domain,
- + - + ]
3284 [ - + - + : 129 : bdev_io->u.bdev.memory_domain_ctx,
- + - + ]
3285 [ - + - + : 129 : bdev_io->u.bdev.accel_sequence);
- + - + ]
3286 : 129 : } else {
3287 : 6529541 : spdk_bdev_io_get_buf(bdev_io, bdev_nvme_get_buf_cb,
3288 [ # # # # : 6372143 : bdev_io->u.bdev.num_blocks * bdev->blocklen);
# # # # #
# # # ]
3289 : 6372143 : rc = 0;
3290 : : }
3291 : 12816284 : break;
3292 : 13210420 : case SPDK_BDEV_IO_TYPE_WRITE:
3293 : 13525492 : rc = bdev_nvme_writev(nbdev_io,
3294 [ # # # # : 157536 : bdev_io->u.bdev.iovs,
# # # # ]
3295 [ # # # # : 157536 : bdev_io->u.bdev.iovcnt,
# # # # ]
3296 [ # # # # : 157536 : bdev_io->u.bdev.md_buf,
# # # # ]
3297 [ # # # # : 157536 : bdev_io->u.bdev.num_blocks,
# # # # ]
3298 [ # # # # : 157536 : bdev_io->u.bdev.offset_blocks,
# # # # ]
3299 [ # # # # : 157536 : bdev_io->u.bdev.dif_check_flags,
# # # # ]
3300 [ # # # # : 157536 : bdev_io->u.bdev.memory_domain,
# # # # ]
3301 [ # # # # : 157536 : bdev_io->u.bdev.memory_domain_ctx,
# # # # ]
3302 [ # # # # : 157536 : bdev_io->u.bdev.accel_sequence,
# # # # ]
3303 [ # # # # : 157536 : bdev_io->u.bdev.nvme_cdw12,
# # ]
3304 [ # # # # : 157536 : bdev_io->u.bdev.nvme_cdw13);
# # ]
3305 : 13367956 : break;
3306 : 53 : case SPDK_BDEV_IO_TYPE_COMPARE:
3307 : 59 : rc = bdev_nvme_comparev(nbdev_io,
3308 [ # # # # : 3 : bdev_io->u.bdev.iovs,
# # # # ]
3309 [ # # # # : 3 : bdev_io->u.bdev.iovcnt,
# # # # ]
3310 [ # # # # : 3 : bdev_io->u.bdev.md_buf,
# # # # ]
3311 [ # # # # : 3 : bdev_io->u.bdev.num_blocks,
# # # # ]
3312 [ # # # # : 3 : bdev_io->u.bdev.offset_blocks,
# # # # ]
3313 [ # # # # : 3 : bdev_io->u.bdev.dif_check_flags);
# # # # ]
3314 : 56 : break;
3315 : 51 : case SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE:
3316 : 55 : rc = bdev_nvme_comparev_and_writev(nbdev_io,
3317 [ # # # # : 2 : bdev_io->u.bdev.iovs,
# # # # ]
3318 [ # # # # : 2 : bdev_io->u.bdev.iovcnt,
# # # # ]
3319 [ # # # # : 2 : bdev_io->u.bdev.fused_iovs,
# # # # ]
3320 [ # # # # : 2 : bdev_io->u.bdev.fused_iovcnt,
# # # # ]
3321 [ # # # # : 2 : bdev_io->u.bdev.md_buf,
# # # # ]
3322 [ # # # # : 2 : bdev_io->u.bdev.num_blocks,
# # # # ]
3323 [ # # # # : 2 : bdev_io->u.bdev.offset_blocks,
# # # # ]
3324 [ # # # # : 2 : bdev_io->u.bdev.dif_check_flags);
# # # # ]
3325 : 53 : break;
3326 : 66786 : case SPDK_BDEV_IO_TYPE_UNMAP:
3327 : 66792 : rc = bdev_nvme_unmap(nbdev_io,
3328 [ # # # # : 3 : bdev_io->u.bdev.offset_blocks,
# # # # ]
3329 [ # # # # : 3 : bdev_io->u.bdev.num_blocks);
# # # # ]
3330 : 66789 : break;
3331 : 698264 : case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
3332 : 792610 : rc = bdev_nvme_write_zeroes(nbdev_io,
3333 [ # # # # : 47173 : bdev_io->u.bdev.offset_blocks,
# # # # ]
3334 [ # # # # : 47173 : bdev_io->u.bdev.num_blocks);
# # # # ]
3335 : 745437 : break;
3336 : 84 : case SPDK_BDEV_IO_TYPE_RESET:
3337 [ # # # # ]: 100 : nbdev_io->io_path = NULL;
3338 [ # # # # ]: 100 : bdev_nvme_reset_io(bdev->ctxt, nbdev_io);
3339 : 100 : return;
3340 : :
3341 : 1289864 : case SPDK_BDEV_IO_TYPE_FLUSH:
3342 : 1289865 : bdev_nvme_io_complete(nbdev_io, 0);
3343 : 1289865 : return;
3344 : :
3345 : 132625 : case SPDK_BDEV_IO_TYPE_ZONE_APPEND:
3346 : 132625 : rc = bdev_nvme_zone_appendv(nbdev_io,
3347 [ # # # # : 0 : bdev_io->u.bdev.iovs,
# # # # ]
3348 [ # # # # : 0 : bdev_io->u.bdev.iovcnt,
# # # # ]
3349 [ # # # # : 0 : bdev_io->u.bdev.md_buf,
# # # # ]
3350 [ # # # # : 0 : bdev_io->u.bdev.num_blocks,
# # # # ]
3351 [ # # # # : 0 : bdev_io->u.bdev.offset_blocks,
# # # # ]
3352 [ # # # # : 0 : bdev_io->u.bdev.dif_check_flags);
# # # # ]
3353 : 132625 : break;
3354 : 1 : case SPDK_BDEV_IO_TYPE_GET_ZONE_INFO:
3355 : 1 : rc = bdev_nvme_get_zone_info(nbdev_io,
3356 [ # # # # : 0 : bdev_io->u.zone_mgmt.zone_id,
# # # # ]
3357 [ # # # # : 0 : bdev_io->u.zone_mgmt.num_zones,
# # # # ]
3358 [ # # # # : 1 : bdev_io->u.zone_mgmt.buf);
# # # # ]
3359 : 1 : break;
3360 : 41 : case SPDK_BDEV_IO_TYPE_ZONE_MANAGEMENT:
3361 : 41 : rc = bdev_nvme_zone_management(nbdev_io,
3362 [ # # # # : 0 : bdev_io->u.zone_mgmt.zone_id,
# # # # ]
3363 [ # # # # : 0 : bdev_io->u.zone_mgmt.zone_action);
# # # # ]
3364 : 41 : break;
3365 : 136 : case SPDK_BDEV_IO_TYPE_NVME_ADMIN:
3366 [ # # # # ]: 142 : nbdev_io->io_path = NULL;
3367 : 148 : bdev_nvme_admin_passthru(nbdev_ch,
3368 : 6 : nbdev_io,
3369 [ # # # # : 6 : &bdev_io->u.nvme_passthru.cmd,
# # ]
3370 [ # # # # : 6 : bdev_io->u.nvme_passthru.buf,
# # # # ]
3371 [ # # # # : 6 : bdev_io->u.nvme_passthru.nbytes);
# # # # ]
3372 : 142 : return;
3373 : :
3374 : 108 : case SPDK_BDEV_IO_TYPE_NVME_IO:
3375 : 114 : rc = bdev_nvme_io_passthru(nbdev_io,
3376 [ # # # # : 3 : &bdev_io->u.nvme_passthru.cmd,
# # ]
3377 [ # # # # : 3 : bdev_io->u.nvme_passthru.buf,
# # # # ]
3378 [ # # # # : 3 : bdev_io->u.nvme_passthru.nbytes);
# # # # ]
3379 : 111 : break;
3380 : 0 : case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
3381 : 0 : rc = bdev_nvme_io_passthru_md(nbdev_io,
3382 [ # # # # : 0 : &bdev_io->u.nvme_passthru.cmd,
# # ]
3383 [ # # # # : 0 : bdev_io->u.nvme_passthru.buf,
# # # # ]
3384 [ # # # # : 0 : bdev_io->u.nvme_passthru.nbytes,
# # # # ]
3385 [ # # # # : 0 : bdev_io->u.nvme_passthru.md_buf,
# # # # ]
3386 [ # # # # : 0 : bdev_io->u.nvme_passthru.md_len);
# # # # ]
3387 : 0 : break;
3388 : 0 : case SPDK_BDEV_IO_TYPE_NVME_IOV_MD:
3389 : 0 : rc = bdev_nvme_iov_passthru_md(nbdev_io,
3390 [ # # # # : 0 : &bdev_io->u.nvme_passthru.cmd,
# # ]
3391 [ # # # # : 0 : bdev_io->u.nvme_passthru.iovs,
# # # # ]
3392 [ # # # # : 0 : bdev_io->u.nvme_passthru.iovcnt,
# # # # ]
3393 [ # # # # : 0 : bdev_io->u.nvme_passthru.nbytes,
# # # # ]
3394 [ # # # # : 0 : bdev_io->u.nvme_passthru.md_buf,
# # # # ]
3395 [ # # # # : 0 : bdev_io->u.nvme_passthru.md_len);
# # # # ]
3396 : 0 : break;
3397 : 7324 : case SPDK_BDEV_IO_TYPE_ABORT:
3398 [ # # # # ]: 7330 : nbdev_io->io_path = NULL;
3399 [ # # # # : 7330 : nbdev_io_to_abort = (struct nvme_bdev_io *)bdev_io->u.abort.bio_to_abort->driver_ctx;
# # # # #
# ]
3400 : 7336 : bdev_nvme_abort(nbdev_ch,
3401 : 6 : nbdev_io,
3402 : 6 : nbdev_io_to_abort);
3403 : 7330 : return;
3404 : :
3405 : 36 : case SPDK_BDEV_IO_TYPE_COPY:
3406 : 38 : rc = bdev_nvme_copy(nbdev_io,
3407 [ # # # # : 1 : bdev_io->u.bdev.offset_blocks,
# # # # ]
3408 [ # # # # : 1 : bdev_io->u.bdev.copy.src_offset_blocks,
# # # # #
# ]
3409 [ # # # # : 1 : bdev_io->u.bdev.num_blocks);
# # # # ]
3410 : 37 : break;
3411 : 0 : default:
3412 : 0 : rc = -EINVAL;
3413 : 0 : break;
3414 : : }
3415 : :
3416 [ + + ]: 27129390 : if (spdk_unlikely(rc != 0)) {
3417 : 88788 : bdev_nvme_io_complete(nbdev_io, rc);
3418 : 0 : }
3419 : 362277 : }
3420 : :
3421 : : static void
3422 : 28904328 : bdev_nvme_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
3423 : : {
3424 : 28904328 : struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(ch);
3425 [ + - ]: 28904328 : struct nvme_bdev_io *nbdev_io = (struct nvme_bdev_io *)bdev_io->driver_ctx;
3426 : :
3427 [ + + + - : 28904328 : if (spdk_likely(nbdev_io->submit_tsc == 0)) {
- + ]
3428 [ + - + - ]: 28866317 : nbdev_io->submit_tsc = spdk_bdev_io_get_submit_tsc(bdev_io);
3429 : 361788 : } else {
3430 : : /* There are cases where submit_tsc != 0, i.e. retry I/O.
3431 : : * We need to update submit_tsc here.
3432 : : */
3433 [ # # # # ]: 38011 : nbdev_io->submit_tsc = spdk_get_ticks();
3434 : : }
3435 : :
3436 [ + + + + : 28904328 : spdk_trace_record(TRACE_BDEV_NVME_IO_START, 0, 0, (uintptr_t)nbdev_io, (uintptr_t)bdev_io);
+ - + - +
- + - + -
+ - ]
3437 [ + - + - ]: 28904328 : nbdev_io->io_path = bdev_nvme_find_io_path(nbdev_ch);
3438 [ + + + - : 28904328 : if (spdk_unlikely(!nbdev_io->io_path)) {
+ - ]
3439 [ + + # # : 481755 : if (!bdev_nvme_io_type_is_admin(bdev_io->type)) {
# # ]
3440 : 481751 : bdev_nvme_io_complete(nbdev_io, -ENXIO);
3441 : 481751 : return;
3442 : : }
3443 : :
3444 : : /* Admin commands do not use the optimal I/O path.
3445 : : * Simply fall through even if it is not found.
3446 : : */
3447 : 1 : }
3448 : :
3449 : 28422577 : _bdev_nvme_submit_request(nbdev_ch, bdev_io);
3450 : 361790 : }
3451 : :
3452 : : static bool
3453 : 3928702 : bdev_nvme_is_supported_csi(enum spdk_nvme_csi csi)
3454 : : {
3455 [ + + + ]: 3928702 : switch (csi) {
3456 : 3834161 : case SPDK_NVME_CSI_NVM:
3457 : 3928673 : return true;
3458 : 29 : case SPDK_NVME_CSI_ZNS:
3459 : 29 : return true;
3460 : 0 : default:
3461 : 0 : return false;
3462 : : }
3463 : 94512 : }
3464 : :
3465 : : static bool
3466 : 3928702 : bdev_nvme_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type)
3467 : : {
3468 : 3928702 : struct nvme_bdev *nbdev = ctx;
3469 : : struct nvme_ns *nvme_ns;
3470 : : struct spdk_nvme_ns *ns;
3471 : : struct spdk_nvme_ctrlr *ctrlr;
3472 : : const struct spdk_nvme_ctrlr_data *cdata;
3473 : :
3474 [ + - + - : 3928702 : nvme_ns = TAILQ_FIRST(&nbdev->nvme_ns_list);
+ - ]
3475 [ + + # # ]: 3928702 : assert(nvme_ns != NULL);
3476 [ + - + - ]: 3928702 : ns = nvme_ns->ns;
3477 [ + + ]: 3928702 : if (ns == NULL) {
3478 : 0 : return false;
3479 : : }
3480 : :
3481 [ + + ]: 3928702 : if (!bdev_nvme_is_supported_csi(spdk_nvme_ns_get_csi(ns))) {
3482 [ # # # ]: 0 : switch (io_type) {
3483 : 0 : case SPDK_BDEV_IO_TYPE_NVME_ADMIN:
3484 : : case SPDK_BDEV_IO_TYPE_NVME_IO:
3485 : 0 : return true;
3486 : :
3487 : 0 : case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
3488 : 0 : return spdk_nvme_ns_get_md_size(ns) ? true : false;
3489 : :
3490 : 0 : default:
3491 : 0 : return false;
3492 : : }
3493 : : }
3494 : :
3495 : 3928702 : ctrlr = spdk_nvme_ns_get_ctrlr(ns);
3496 : :
3497 [ + + + + : 3928702 : switch (io_type) {
+ + + + +
+ ]
3498 : 1513038 : case SPDK_BDEV_IO_TYPE_READ:
3499 : : case SPDK_BDEV_IO_TYPE_WRITE:
3500 : : case SPDK_BDEV_IO_TYPE_RESET:
3501 : : case SPDK_BDEV_IO_TYPE_FLUSH:
3502 : : case SPDK_BDEV_IO_TYPE_NVME_ADMIN:
3503 : : case SPDK_BDEV_IO_TYPE_NVME_IO:
3504 : : case SPDK_BDEV_IO_TYPE_ABORT:
3505 : 1513085 : return true;
3506 : :
3507 : 3803 : case SPDK_BDEV_IO_TYPE_COMPARE:
3508 : 3809 : return spdk_nvme_ns_supports_compare(ns);
3509 : :
3510 : 1111 : case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
3511 : 1113 : return spdk_nvme_ns_get_md_size(ns) ? true : false;
3512 : :
3513 : 12062 : case SPDK_BDEV_IO_TYPE_UNMAP:
3514 : 12068 : cdata = spdk_nvme_ctrlr_get_data(ctrlr);
3515 [ # # # # ]: 12068 : return cdata->oncs.dsm;
3516 : :
3517 : 2267520 : case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
3518 : 2361901 : cdata = spdk_nvme_ctrlr_get_data(ctrlr);
3519 [ + - + - ]: 2361901 : return cdata->oncs.write_zeroes;
3520 : :
3521 : 3801 : case SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE:
3522 [ + + ]: 3807 : if (spdk_nvme_ctrlr_get_flags(ctrlr) &
3523 : : SPDK_NVME_CTRLR_COMPARE_AND_WRITE_SUPPORTED) {
3524 : 246 : return true;
3525 : : }
3526 : 3561 : return false;
3527 : :
3528 : 7454 : case SPDK_BDEV_IO_TYPE_GET_ZONE_INFO:
3529 : : case SPDK_BDEV_IO_TYPE_ZONE_MANAGEMENT:
3530 : 7462 : return spdk_nvme_ns_get_csi(ns) == SPDK_NVME_CSI_ZNS;
3531 : :
3532 : 3728 : case SPDK_BDEV_IO_TYPE_ZONE_APPEND:
3533 [ + + ]: 3734 : return spdk_nvme_ns_get_csi(ns) == SPDK_NVME_CSI_ZNS &&
3534 [ + - ]: 2 : spdk_nvme_ctrlr_get_flags(ctrlr) & SPDK_NVME_CTRLR_ZONE_APPEND_SUPPORTED;
3535 : :
3536 : 6731 : case SPDK_BDEV_IO_TYPE_COPY:
3537 : 6767 : cdata = spdk_nvme_ctrlr_get_data(ctrlr);
3538 [ + - + - ]: 6767 : return cdata->oncs.copy;
3539 : :
3540 : 14942 : default:
3541 : 14958 : return false;
3542 : : }
3543 : 94512 : }
3544 : :
3545 : : static int
3546 : 2397 : nvme_qpair_create(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ctrlr_channel *ctrlr_ch)
3547 : : {
3548 : : struct nvme_qpair *nvme_qpair;
3549 : : struct spdk_io_channel *pg_ch;
3550 : : int rc;
3551 : :
3552 : 2397 : nvme_qpair = calloc(1, sizeof(*nvme_qpair));
3553 [ + + ]: 2397 : if (!nvme_qpair) {
3554 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to alloc nvme_qpair.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
3555 : 0 : return -1;
3556 : : }
3557 : :
3558 [ + - + - : 2397 : TAILQ_INIT(&nvme_qpair->io_path_list);
+ - + - +
- + - + -
+ - ]
3559 : :
3560 [ + - + - ]: 2397 : nvme_qpair->ctrlr = nvme_ctrlr;
3561 [ + - + - ]: 2397 : nvme_qpair->ctrlr_ch = ctrlr_ch;
3562 : :
3563 : 2397 : pg_ch = spdk_get_io_channel(&g_nvme_bdev_ctrlrs);
3564 [ + + ]: 2397 : if (!pg_ch) {
3565 : 0 : free(nvme_qpair);
3566 : 0 : return -1;
3567 : : }
3568 : :
3569 [ + - + - ]: 2397 : nvme_qpair->group = spdk_io_channel_get_ctx(pg_ch);
3570 : :
3571 : : #ifdef SPDK_CONFIG_VTUNE
3572 : : nvme_qpair->group->collect_spin_stat = true;
3573 : : #else
3574 [ + - + - : 2397 : nvme_qpair->group->collect_spin_stat = false;
+ - + - ]
3575 : : #endif
3576 : :
3577 [ + + - + ]: 2397 : if (!nvme_ctrlr->disabled) {
3578 : : /* If a nvme_ctrlr is disabled, don't try to create qpair for it. Qpair will
3579 : : * be created when it's enabled.
3580 : : */
3581 : 2397 : rc = bdev_nvme_create_qpair(nvme_qpair);
3582 [ + + ]: 2397 : if (rc != 0) {
3583 : : /* nvme_ctrlr can't create IO qpair if connection is down.
3584 : : * If reconnect_delay_sec is non-zero, creating IO qpair is retried
3585 : : * after reconnect_delay_sec seconds. If bdev_retry_count is non-zero,
3586 : : * submitted IO will be queued until IO qpair is successfully created.
3587 : : *
3588 : : * Hence, if both are satisfied, ignore the failure.
3589 : : */
3590 [ # # # # : 0 : if (nvme_ctrlr->opts.reconnect_delay_sec == 0 || g_opts.bdev_retry_count == 0) {
# # # # #
# # # ]
3591 : 0 : spdk_put_io_channel(pg_ch);
3592 : 0 : free(nvme_qpair);
3593 : 0 : return rc;
3594 : : }
3595 : 0 : }
3596 : 82 : }
3597 : :
3598 [ + - + - : 2397 : TAILQ_INSERT_TAIL(&nvme_qpair->group->qpair_list, nvme_qpair, tailq);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
3599 : :
3600 [ + - + - ]: 2397 : ctrlr_ch->qpair = nvme_qpair;
3601 : :
3602 : 2397 : nvme_ctrlr_get_ref(nvme_ctrlr);
3603 : :
3604 : 2397 : return 0;
3605 : 82 : }
3606 : :
3607 : : static int
3608 : 2397 : bdev_nvme_create_ctrlr_channel_cb(void *io_device, void *ctx_buf)
3609 : : {
3610 : 2397 : struct nvme_ctrlr *nvme_ctrlr = io_device;
3611 : 2397 : struct nvme_ctrlr_channel *ctrlr_ch = ctx_buf;
3612 : :
3613 : 2397 : return nvme_qpair_create(nvme_ctrlr, ctrlr_ch);
3614 : : }
3615 : :
3616 : : static void
3617 : 2397 : nvme_qpair_delete(struct nvme_qpair *nvme_qpair)
3618 : : {
3619 : : struct nvme_io_path *io_path, *next;
3620 : :
3621 [ + + + - : 2397 : assert(nvme_qpair->group != NULL);
+ - # # ]
3622 : :
3623 [ + + + + : 4828 : TAILQ_FOREACH_SAFE(io_path, &nvme_qpair->io_path_list, tailq, next) {
+ - + + +
- + - + -
+ + ]
3624 [ + + + - : 2431 : TAILQ_REMOVE(&nvme_qpair->io_path_list, io_path, tailq);
+ - - + #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
3625 : 2431 : nvme_io_path_free(io_path);
3626 : 60 : }
3627 : :
3628 [ + + + - : 2397 : TAILQ_REMOVE(&nvme_qpair->group->qpair_list, nvme_qpair, tailq);
+ - - + #
# # # # #
# # # # #
# # # # #
# # + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
3629 : :
3630 [ + - + - ]: 2397 : spdk_put_io_channel(spdk_io_channel_from_ctx(nvme_qpair->group));
3631 : :
3632 [ + - + - ]: 2397 : nvme_ctrlr_put_ref(nvme_qpair->ctrlr);
3633 : :
3634 : 2397 : free(nvme_qpair);
3635 : 2397 : }
3636 : :
3637 : : static void
3638 : 2397 : bdev_nvme_destroy_ctrlr_channel_cb(void *io_device, void *ctx_buf)
3639 : : {
3640 : 2397 : struct nvme_ctrlr_channel *ctrlr_ch = ctx_buf;
3641 : : struct nvme_qpair *nvme_qpair;
3642 : :
3643 [ + - + - ]: 2397 : nvme_qpair = ctrlr_ch->qpair;
3644 [ + + # # ]: 2397 : assert(nvme_qpair != NULL);
3645 : :
3646 : 2397 : _bdev_nvme_clear_io_path_cache(nvme_qpair);
3647 : :
3648 [ + + + - : 2397 : if (nvme_qpair->qpair != NULL) {
+ - ]
3649 : : /* Always try to disconnect the qpair, even if a reset is in progress.
3650 : : * The qpair may have been created after the reset process started.
3651 : : */
3652 [ + - + - ]: 2312 : spdk_nvme_ctrlr_disconnect_io_qpair(nvme_qpair->qpair);
3653 [ + + + - : 2312 : if (ctrlr_ch->reset_iter) {
- + ]
3654 : : /* Skip current ctrlr_channel in a full reset sequence because
3655 : : * it is being deleted now.
3656 : : */
3657 [ # # # # ]: 0 : nvme_ctrlr_for_each_channel_continue(ctrlr_ch->reset_iter, 0);
3658 : 0 : }
3659 : :
3660 : : /* We cannot release a reference to the poll group now.
3661 : : * The qpair may be disconnected asynchronously later.
3662 : : * We need to poll it until it is actually disconnected.
3663 : : * Just detach the qpair from the deleting ctrlr_channel.
3664 : : */
3665 [ + - + - ]: 2312 : nvme_qpair->ctrlr_ch = NULL;
3666 : 68 : } else {
3667 [ - + # # : 85 : assert(ctrlr_ch->reset_iter == NULL);
# # # # ]
3668 : :
3669 : 85 : nvme_qpair_delete(nvme_qpair);
3670 : : }
3671 : 2397 : }
3672 : :
3673 : : static inline struct spdk_io_channel *
3674 : 845443 : bdev_nvme_get_accel_channel(struct nvme_poll_group *group)
3675 : : {
3676 [ + + # # : 845443 : if (spdk_unlikely(!group->accel_channel)) {
# # ]
3677 [ # # # # ]: 53 : group->accel_channel = spdk_accel_get_io_channel();
3678 [ - + # # : 53 : if (!group->accel_channel) {
# # ]
3679 : 0 : SPDK_ERRLOG("Cannot get the accel_channel for bdev nvme polling group=%p\n",
3680 : : group);
3681 : 0 : return NULL;
3682 : : }
3683 : 0 : }
3684 : :
3685 [ # # # # ]: 845443 : return group->accel_channel;
3686 : 0 : }
3687 : :
3688 : : static void
3689 : 845443 : bdev_nvme_finish_sequence(void *seq, spdk_nvme_accel_completion_cb cb_fn, void *cb_arg)
3690 : : {
3691 : 845443 : spdk_accel_sequence_finish(seq, cb_fn, cb_arg);
3692 : 845443 : }
3693 : :
3694 : : static void
3695 : 0 : bdev_nvme_abort_sequence(void *seq)
3696 : : {
3697 : 0 : spdk_accel_sequence_abort(seq);
3698 : 0 : }
3699 : :
3700 : : static void
3701 : 411385 : bdev_nvme_reverse_sequence(void *seq)
3702 : : {
3703 : 411385 : spdk_accel_sequence_reverse(seq);
3704 : 411385 : }
3705 : :
3706 : : static int
3707 : 845443 : bdev_nvme_append_crc32c(void *ctx, void **seq, uint32_t *dst, struct iovec *iovs, uint32_t iovcnt,
3708 : : struct spdk_memory_domain *domain, void *domain_ctx, uint32_t seed,
3709 : : spdk_nvme_accel_step_cb cb_fn, void *cb_arg)
3710 : : {
3711 : : struct spdk_io_channel *ch;
3712 : 845443 : struct nvme_poll_group *group = ctx;
3713 : :
3714 : 845443 : ch = bdev_nvme_get_accel_channel(group);
3715 [ - + ]: 845443 : if (spdk_unlikely(ch == NULL)) {
3716 : 0 : return -ENOMEM;
3717 : : }
3718 : :
3719 : 845443 : return spdk_accel_append_crc32c((struct spdk_accel_sequence **)seq, ch, dst, iovs, iovcnt,
3720 : 0 : domain, domain_ctx, seed, cb_fn, cb_arg);
3721 : 0 : }
3722 : :
3723 : : static int
3724 : 0 : bdev_nvme_append_copy(void *ctx, void **seq, struct iovec *dst_iovs, uint32_t dst_iovcnt,
3725 : : struct spdk_memory_domain *dst_domain, void *dst_domain_ctx,
3726 : : struct iovec *src_iovs, uint32_t src_iovcnt,
3727 : : struct spdk_memory_domain *src_domain, void *src_domain_ctx,
3728 : : spdk_nvme_accel_step_cb cb_fn, void *cb_arg)
3729 : : {
3730 : : struct spdk_io_channel *ch;
3731 : 0 : struct nvme_poll_group *group = ctx;
3732 : :
3733 : 0 : ch = bdev_nvme_get_accel_channel(group);
3734 [ # # ]: 0 : if (spdk_unlikely(ch == NULL)) {
3735 : 0 : return -ENOMEM;
3736 : : }
3737 : :
3738 : 0 : return spdk_accel_append_copy((struct spdk_accel_sequence **)seq, ch,
3739 : 0 : dst_iovs, dst_iovcnt, dst_domain, dst_domain_ctx,
3740 : 0 : src_iovs, src_iovcnt, src_domain, src_domain_ctx,
3741 : 0 : cb_fn, cb_arg);
3742 : 0 : }
3743 : :
3744 : : static struct spdk_nvme_accel_fn_table g_bdev_nvme_accel_fn_table = {
3745 : : .table_size = sizeof(struct spdk_nvme_accel_fn_table),
3746 : : .append_crc32c = bdev_nvme_append_crc32c,
3747 : : .append_copy = bdev_nvme_append_copy,
3748 : : .finish_sequence = bdev_nvme_finish_sequence,
3749 : : .reverse_sequence = bdev_nvme_reverse_sequence,
3750 : : .abort_sequence = bdev_nvme_abort_sequence,
3751 : : };
3752 : :
3753 : : static int
3754 : 46926 : bdev_nvme_interrupt_wrapper(void *ctx)
3755 : : {
3756 : : int num_events;
3757 : 46926 : struct nvme_poll_group *group = ctx;
3758 : :
3759 [ # # # # ]: 46926 : num_events = spdk_nvme_poll_group_wait(group->group, bdev_nvme_disconnected_qpair_cb);
3760 [ - + ]: 46926 : if (spdk_unlikely(num_events < 0)) {
3761 : 0 : bdev_nvme_check_io_qpairs(group);
3762 : 0 : }
3763 : :
3764 : 46926 : return num_events;
3765 : : }
3766 : :
3767 : : static int
3768 : 2238 : bdev_nvme_create_poll_group_cb(void *io_device, void *ctx_buf)
3769 : : {
3770 : 2238 : struct nvme_poll_group *group = ctx_buf;
3771 : : uint64_t period;
3772 : : int fd;
3773 : :
3774 [ + - + - : 2238 : TAILQ_INIT(&group->qpair_list);
+ - + - +
- + - + -
+ - ]
3775 : :
3776 [ + - + - ]: 2238 : group->group = spdk_nvme_poll_group_create(group, &g_bdev_nvme_accel_fn_table);
3777 [ + + + - : 2238 : if (group->group == NULL) {
+ - ]
3778 : 0 : return -1;
3779 : : }
3780 : :
3781 [ + + + - ]: 2238 : period = spdk_interrupt_mode_is_enabled() ? 0 : g_opts.nvme_ioq_poll_period_us;
3782 [ + - + - ]: 2238 : group->poller = SPDK_POLLER_REGISTER(bdev_nvme_poll, group, period);
3783 : :
3784 [ + + + - : 2238 : if (group->poller == NULL) {
+ - ]
3785 [ # # # # ]: 0 : spdk_nvme_poll_group_destroy(group->group);
3786 : 0 : return -1;
3787 : : }
3788 : :
3789 [ + + ]: 2238 : if (spdk_interrupt_mode_is_enabled()) {
3790 [ # # # # ]: 2 : spdk_poller_register_interrupt(group->poller, NULL, NULL);
3791 : :
3792 [ # # # # ]: 2 : fd = spdk_nvme_poll_group_get_fd(group->group);
3793 [ - + ]: 2 : if (fd < 0) {
3794 [ # # # # ]: 0 : spdk_nvme_poll_group_destroy(group->group);
3795 : 0 : return -1;
3796 : : }
3797 : :
3798 [ # # # # ]: 2 : group->intr = SPDK_INTERRUPT_REGISTER(fd, bdev_nvme_interrupt_wrapper, group);
3799 [ - + # # : 2 : if (!group->intr) {
# # ]
3800 [ # # # # ]: 0 : spdk_nvme_poll_group_destroy(group->group);
3801 : 0 : return -1;
3802 : : }
3803 : 0 : }
3804 : :
3805 : 2238 : return 0;
3806 : 67 : }
3807 : :
3808 : : static void
3809 : 2238 : bdev_nvme_destroy_poll_group_cb(void *io_device, void *ctx_buf)
3810 : : {
3811 : 2238 : struct nvme_poll_group *group = ctx_buf;
3812 : :
3813 [ + + + - : 2238 : assert(TAILQ_EMPTY(&group->qpair_list));
+ - + - #
# ]
3814 : :
3815 [ + + + - : 2238 : if (group->accel_channel) {
+ - ]
3816 [ # # # # ]: 53 : spdk_put_io_channel(group->accel_channel);
3817 : 0 : }
3818 : :
3819 [ + + ]: 2238 : if (spdk_interrupt_mode_is_enabled()) {
3820 [ # # ]: 2 : spdk_interrupt_unregister(&group->intr);
3821 : 0 : }
3822 : :
3823 [ + - ]: 2238 : spdk_poller_unregister(&group->poller);
3824 [ + + + - : 2238 : if (spdk_nvme_poll_group_destroy(group->group)) {
+ - ]
3825 : 0 : SPDK_ERRLOG("Unable to destroy a poll group for the NVMe bdev module.\n");
3826 [ # # ]: 0 : assert(false);
3827 : : }
3828 : 2238 : }
3829 : :
3830 : : static struct spdk_io_channel *
3831 : 2279 : bdev_nvme_get_io_channel(void *ctx)
3832 : : {
3833 : 2279 : struct nvme_bdev *nbdev = ctx;
3834 : :
3835 : 2279 : return spdk_get_io_channel(nbdev);
3836 : : }
3837 : :
3838 : : static void *
3839 : 0 : bdev_nvme_get_module_ctx(void *ctx)
3840 : : {
3841 : 0 : struct nvme_bdev *nbdev = ctx;
3842 : : struct nvme_ns *nvme_ns;
3843 : :
3844 [ # # # # : 0 : if (!nbdev || nbdev->disk.module != &nvme_if) {
# # # # #
# ]
3845 : 0 : return NULL;
3846 : : }
3847 : :
3848 [ # # # # : 0 : nvme_ns = TAILQ_FIRST(&nbdev->nvme_ns_list);
# # ]
3849 [ # # ]: 0 : if (!nvme_ns) {
3850 : 0 : return NULL;
3851 : : }
3852 : :
3853 [ # # # # ]: 0 : return nvme_ns->ns;
3854 : 0 : }
3855 : :
3856 : : static const char *
3857 : 0 : _nvme_ana_state_str(enum spdk_nvme_ana_state ana_state)
3858 : : {
3859 [ # # # # : 0 : switch (ana_state) {
# # ]
3860 : 0 : case SPDK_NVME_ANA_OPTIMIZED_STATE:
3861 : 0 : return "optimized";
3862 : 0 : case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
3863 : 0 : return "non_optimized";
3864 : 0 : case SPDK_NVME_ANA_INACCESSIBLE_STATE:
3865 : 0 : return "inaccessible";
3866 : 0 : case SPDK_NVME_ANA_PERSISTENT_LOSS_STATE:
3867 : 0 : return "persistent_loss";
3868 : 0 : case SPDK_NVME_ANA_CHANGE_STATE:
3869 : 0 : return "change";
3870 : 0 : default:
3871 : 0 : return NULL;
3872 : : }
3873 : 0 : }
3874 : :
3875 : : static int
3876 : 10395 : bdev_nvme_get_memory_domains(void *ctx, struct spdk_memory_domain **domains, int array_size)
3877 : : {
3878 : 10395 : struct spdk_memory_domain **_domains = NULL;
3879 : 10395 : struct nvme_bdev *nbdev = ctx;
3880 : : struct nvme_ns *nvme_ns;
3881 : 10395 : int i = 0, _array_size = array_size;
3882 : 10395 : int rc = 0;
3883 : :
3884 [ + + + - : 20866 : TAILQ_FOREACH(nvme_ns, &nbdev->nvme_ns_list, tailq) {
+ - + + +
- + - +
- ]
3885 [ + + + + ]: 10471 : if (domains && array_size >= i) {
3886 [ # # ]: 285 : _domains = &domains[i];
3887 : 11 : } else {
3888 : 10186 : _domains = NULL;
3889 : : }
3890 [ + - + - : 10471 : rc = spdk_nvme_ctrlr_get_memory_domains(nvme_ns->ctrlr->ctrlr, _domains, _array_size);
+ - + - ]
3891 [ + + ]: 10471 : if (rc > 0) {
3892 [ # # ]: 4342 : i += rc;
3893 [ + + ]: 4342 : if (_array_size >= rc) {
3894 [ # # ]: 277 : _array_size -= rc;
3895 : 9 : } else {
3896 : 4065 : _array_size = 0;
3897 : : }
3898 [ + + ]: 6142 : } else if (rc < 0) {
3899 : 0 : return rc;
3900 : : }
3901 : 77 : }
3902 : :
3903 : 10395 : return i;
3904 : 71 : }
3905 : :
3906 : : static const char *
3907 : 745 : nvme_ctrlr_get_state_str(struct nvme_ctrlr *nvme_ctrlr)
3908 : : {
3909 [ - + # # ]: 745 : if (nvme_ctrlr->destruct) {
3910 : 0 : return "deleting";
3911 [ - + # # : 745 : } else if (spdk_nvme_ctrlr_is_failed(nvme_ctrlr->ctrlr)) {
# # ]
3912 : 0 : return "failed";
3913 [ + + # # ]: 745 : } else if (nvme_ctrlr->resetting) {
3914 : 12 : return "resetting";
3915 [ + + # # ]: 733 : } else if (nvme_ctrlr->reconnect_is_delayed > 0) {
3916 : 5 : return "reconnect_is_delayed";
3917 [ + + # # ]: 728 : } else if (nvme_ctrlr->disabled) {
3918 : 0 : return "disabled";
3919 : : } else {
3920 : 728 : return "enabled";
3921 : : }
3922 : 1 : }
3923 : :
3924 : : void
3925 : 745 : nvme_ctrlr_info_json(struct spdk_json_write_ctx *w, struct nvme_ctrlr *nvme_ctrlr)
3926 : 744 : {
3927 : : struct spdk_nvme_transport_id *trid;
3928 : : const struct spdk_nvme_ctrlr_opts *opts;
3929 : : const struct spdk_nvme_ctrlr_data *cdata;
3930 : : struct nvme_path_id *path_id;
3931 : : int32_t numa_id;
3932 : :
3933 : 745 : spdk_json_write_object_begin(w);
3934 : :
3935 : 745 : spdk_json_write_named_string(w, "state", nvme_ctrlr_get_state_str(nvme_ctrlr));
3936 : :
3937 : : #ifdef SPDK_CONFIG_NVME_CUSE
3938 : 744 : size_t cuse_name_size = 128;
3939 [ - + ]: 744 : char cuse_name[cuse_name_size];
3940 : :
3941 [ # # # # ]: 744 : int rc = spdk_nvme_cuse_get_ctrlr_name(nvme_ctrlr->ctrlr, cuse_name, &cuse_name_size);
3942 [ + + ]: 744 : if (rc == 0) {
3943 : 3 : spdk_json_write_named_string(w, "cuse_device", cuse_name);
3944 : 0 : }
3945 : : #endif
3946 [ # # # # : 745 : trid = &nvme_ctrlr->active_path_id->trid;
# # ]
3947 : 745 : spdk_json_write_named_object_begin(w, "trid");
3948 : 745 : nvme_bdev_dump_trid_json(trid, w);
3949 : 745 : spdk_json_write_object_end(w);
3950 : :
3951 [ # # # # : 745 : path_id = TAILQ_NEXT(nvme_ctrlr->active_path_id, link);
# # # # #
# ]
3952 [ + + ]: 745 : if (path_id != NULL) {
3953 : 12 : spdk_json_write_named_array_begin(w, "alternate_trids");
3954 : 0 : do {
3955 [ # # ]: 16 : trid = &path_id->trid;
3956 : 16 : spdk_json_write_object_begin(w);
3957 : 16 : nvme_bdev_dump_trid_json(trid, w);
3958 : 16 : spdk_json_write_object_end(w);
3959 : :
3960 [ # # # # : 16 : path_id = TAILQ_NEXT(path_id, link);
# # ]
3961 [ + + ]: 16 : } while (path_id != NULL);
3962 : 12 : spdk_json_write_array_end(w);
3963 : 0 : }
3964 : :
3965 [ # # # # ]: 745 : cdata = spdk_nvme_ctrlr_get_data(nvme_ctrlr->ctrlr);
3966 [ # # # # ]: 745 : spdk_json_write_named_uint16(w, "cntlid", cdata->cntlid);
3967 : :
3968 [ # # # # ]: 745 : opts = spdk_nvme_ctrlr_get_opts(nvme_ctrlr->ctrlr);
3969 : 745 : spdk_json_write_named_object_begin(w, "host");
3970 [ # # ]: 745 : spdk_json_write_named_string(w, "nqn", opts->hostnqn);
3971 [ # # ]: 745 : spdk_json_write_named_string(w, "addr", opts->src_addr);
3972 [ # # ]: 745 : spdk_json_write_named_string(w, "svcid", opts->src_svcid);
3973 : 745 : spdk_json_write_object_end(w);
3974 : :
3975 [ # # # # ]: 745 : numa_id = spdk_nvme_ctrlr_get_numa_id(nvme_ctrlr->ctrlr);
3976 [ + + ]: 745 : if (numa_id != SPDK_ENV_NUMA_ID_ANY) {
3977 : 357 : spdk_json_write_named_uint32(w, "numa_id", numa_id);
3978 : 0 : }
3979 : 745 : spdk_json_write_object_end(w);
3980 : 745 : }
3981 : :
3982 : : static void
3983 : 1123 : nvme_namespace_info_json(struct spdk_json_write_ctx *w,
3984 : : struct nvme_ns *nvme_ns)
3985 : 1121 : {
3986 : : struct spdk_nvme_ns *ns;
3987 : : struct spdk_nvme_ctrlr *ctrlr;
3988 : : const struct spdk_nvme_ctrlr_data *cdata;
3989 : : const struct spdk_nvme_transport_id *trid;
3990 : : union spdk_nvme_vs_register vs;
3991 : : const struct spdk_nvme_ns_data *nsdata;
3992 : 895 : char buf[128];
3993 : :
3994 [ # # # # ]: 1123 : ns = nvme_ns->ns;
3995 [ + + ]: 1123 : if (ns == NULL) {
3996 : 0 : return;
3997 : : }
3998 : :
3999 : 1123 : ctrlr = spdk_nvme_ns_get_ctrlr(ns);
4000 : :
4001 : 1123 : cdata = spdk_nvme_ctrlr_get_data(ctrlr);
4002 : 1123 : trid = spdk_nvme_ctrlr_get_transport_id(ctrlr);
4003 : 1123 : vs = spdk_nvme_ctrlr_get_regs_vs(ctrlr);
4004 : :
4005 : 1123 : spdk_json_write_object_begin(w);
4006 : :
4007 [ + + # # : 1123 : if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
# # ]
4008 [ # # ]: 885 : spdk_json_write_named_string(w, "pci_address", trid->traddr);
4009 : 2 : }
4010 : :
4011 : 1123 : spdk_json_write_named_object_begin(w, "trid");
4012 : :
4013 : 1123 : nvme_bdev_dump_trid_json(trid, w);
4014 : :
4015 : 1123 : spdk_json_write_object_end(w);
4016 : :
4017 : : #ifdef SPDK_CONFIG_NVME_CUSE
4018 : 1121 : size_t cuse_name_size = 128;
4019 [ - + ]: 1121 : char cuse_name[cuse_name_size];
4020 : :
4021 : 1121 : int rc = spdk_nvme_cuse_get_ns_name(ctrlr, spdk_nvme_ns_get_id(ns),
4022 : : cuse_name, &cuse_name_size);
4023 [ + + ]: 1121 : if (rc == 0) {
4024 : 4 : spdk_json_write_named_string(w, "cuse_device", cuse_name);
4025 : 0 : }
4026 : : #endif
4027 : :
4028 : 1123 : spdk_json_write_named_object_begin(w, "ctrlr_data");
4029 : :
4030 [ # # # # ]: 1123 : spdk_json_write_named_uint16(w, "cntlid", cdata->cntlid);
4031 : :
4032 [ # # # # ]: 1123 : spdk_json_write_named_string_fmt(w, "vendor_id", "0x%04x", cdata->vid);
4033 : :
4034 [ - + ]: 1123 : snprintf(buf, sizeof(cdata->mn) + 1, "%s", cdata->mn);
4035 : 1123 : spdk_str_trim(buf);
4036 : 1123 : spdk_json_write_named_string(w, "model_number", buf);
4037 : :
4038 [ - + ]: 1123 : snprintf(buf, sizeof(cdata->sn) + 1, "%s", cdata->sn);
4039 : 1123 : spdk_str_trim(buf);
4040 : 1123 : spdk_json_write_named_string(w, "serial_number", buf);
4041 : :
4042 [ - + ]: 1123 : snprintf(buf, sizeof(cdata->fr) + 1, "%s", cdata->fr);
4043 : 1123 : spdk_str_trim(buf);
4044 : 1123 : spdk_json_write_named_string(w, "firmware_revision", buf);
4045 : :
4046 [ + + # # : 1123 : if (cdata->subnqn[0] != '\0') {
# # # # #
# ]
4047 [ # # ]: 380 : spdk_json_write_named_string(w, "subnqn", cdata->subnqn);
4048 : 2 : }
4049 : :
4050 : 1123 : spdk_json_write_named_object_begin(w, "oacs");
4051 : :
4052 [ # # # # ]: 1123 : spdk_json_write_named_uint32(w, "security", cdata->oacs.security);
4053 [ # # # # ]: 1123 : spdk_json_write_named_uint32(w, "format", cdata->oacs.format);
4054 [ # # # # ]: 1123 : spdk_json_write_named_uint32(w, "firmware", cdata->oacs.firmware);
4055 [ # # # # ]: 1123 : spdk_json_write_named_uint32(w, "ns_manage", cdata->oacs.ns_manage);
4056 : :
4057 : 1123 : spdk_json_write_object_end(w);
4058 : :
4059 [ # # # # ]: 1123 : spdk_json_write_named_bool(w, "multi_ctrlr", cdata->cmic.multi_ctrlr);
4060 [ # # # # ]: 1123 : spdk_json_write_named_bool(w, "ana_reporting", cdata->cmic.ana_reporting);
4061 : :
4062 : 1123 : spdk_json_write_object_end(w);
4063 : :
4064 : 1123 : spdk_json_write_named_object_begin(w, "vs");
4065 : :
4066 : 1123 : spdk_json_write_name(w, "nvme_version");
4067 [ + + ]: 1123 : if (vs.bits.ter) {
4068 : 0 : spdk_json_write_string_fmt(w, "%u.%u.%u", vs.bits.mjr, vs.bits.mnr, vs.bits.ter);
4069 : 0 : } else {
4070 : 1123 : spdk_json_write_string_fmt(w, "%u.%u", vs.bits.mjr, vs.bits.mnr);
4071 : : }
4072 : :
4073 : 1123 : spdk_json_write_object_end(w);
4074 : :
4075 : 1123 : nsdata = spdk_nvme_ns_get_data(ns);
4076 : :
4077 : 1123 : spdk_json_write_named_object_begin(w, "ns_data");
4078 : :
4079 : 1123 : spdk_json_write_named_uint32(w, "id", spdk_nvme_ns_get_id(ns));
4080 : :
4081 [ + + # # : 1123 : if (cdata->cmic.ana_reporting) {
# # ]
4082 : 0 : spdk_json_write_named_string(w, "ana_state",
4083 [ # # # # ]: 0 : _nvme_ana_state_str(nvme_ns->ana_state));
4084 : 0 : }
4085 : :
4086 [ # # # # ]: 1123 : spdk_json_write_named_bool(w, "can_share", nsdata->nmic.can_share);
4087 : :
4088 : 1123 : spdk_json_write_object_end(w);
4089 : :
4090 [ + + # # : 1123 : if (cdata->oacs.security) {
# # ]
4091 : 709 : spdk_json_write_named_object_begin(w, "security");
4092 : :
4093 [ - + # # : 709 : spdk_json_write_named_bool(w, "opal", nvme_ns->bdev->opal);
# # # # #
# ]
4094 : :
4095 : 709 : spdk_json_write_object_end(w);
4096 : 0 : }
4097 : :
4098 : 1123 : spdk_json_write_object_end(w);
4099 : 2 : }
4100 : :
4101 : : static const char *
4102 : 1113 : nvme_bdev_get_mp_policy_str(struct nvme_bdev *nbdev)
4103 : : {
4104 [ + - + # : 1113 : switch (nbdev->mp_policy) {
# # # ]
4105 : 1111 : case BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE:
4106 : 1113 : return "active_passive";
4107 : 0 : case BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE:
4108 : 0 : return "active_active";
4109 : 0 : default:
4110 [ # # ]: 0 : assert(false);
4111 : : return "invalid";
4112 : : }
4113 : 2 : }
4114 : :
4115 : : static const char *
4116 : 0 : nvme_bdev_get_mp_selector_str(struct nvme_bdev *nbdev)
4117 : : {
4118 [ # # # # : 0 : switch (nbdev->mp_selector) {
# # # ]
4119 : 0 : case BDEV_NVME_MP_SELECTOR_ROUND_ROBIN:
4120 : 0 : return "round_robin";
4121 : 0 : case BDEV_NVME_MP_SELECTOR_QUEUE_DEPTH:
4122 : 0 : return "queue_depth";
4123 : 0 : default:
4124 [ # # ]: 0 : assert(false);
4125 : : return "invalid";
4126 : : }
4127 : 0 : }
4128 : :
4129 : : static int
4130 : 1113 : bdev_nvme_dump_info_json(void *ctx, struct spdk_json_write_ctx *w)
4131 : : {
4132 : 1113 : struct nvme_bdev *nbdev = ctx;
4133 : : struct nvme_ns *nvme_ns;
4134 : :
4135 [ - + # # ]: 1113 : pthread_mutex_lock(&nbdev->mutex);
4136 : 1113 : spdk_json_write_named_array_begin(w, "nvme");
4137 [ + + # # : 2236 : TAILQ_FOREACH(nvme_ns, &nbdev->nvme_ns_list, tailq) {
# # # # #
# # # #
# ]
4138 : 1123 : nvme_namespace_info_json(w, nvme_ns);
4139 : 2 : }
4140 : 1113 : spdk_json_write_array_end(w);
4141 : 1113 : spdk_json_write_named_string(w, "mp_policy", nvme_bdev_get_mp_policy_str(nbdev));
4142 [ + + # # : 1113 : if (nbdev->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE) {
# # ]
4143 : 0 : spdk_json_write_named_string(w, "selector", nvme_bdev_get_mp_selector_str(nbdev));
4144 [ # # # # : 0 : if (nbdev->mp_selector == BDEV_NVME_MP_SELECTOR_ROUND_ROBIN) {
# # ]
4145 [ # # # # ]: 0 : spdk_json_write_named_uint32(w, "rr_min_io", nbdev->rr_min_io);
4146 : 0 : }
4147 : 0 : }
4148 [ - + # # ]: 1113 : pthread_mutex_unlock(&nbdev->mutex);
4149 : :
4150 : 1113 : return 0;
4151 : : }
4152 : :
4153 : : static void
4154 : 169 : bdev_nvme_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
4155 : : {
4156 : : /* No config per bdev needed */
4157 : 169 : }
4158 : :
4159 : : static uint64_t
4160 : 0 : bdev_nvme_get_spin_time(struct spdk_io_channel *ch)
4161 : : {
4162 : 0 : struct nvme_bdev_channel *nbdev_ch = spdk_io_channel_get_ctx(ch);
4163 : : struct nvme_io_path *io_path;
4164 : : struct nvme_poll_group *group;
4165 : 0 : uint64_t spin_time = 0;
4166 : :
4167 [ # # # # : 0 : STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
# # # # #
# # # #
# ]
4168 [ # # # # : 0 : group = io_path->qpair->group;
# # # # ]
4169 : :
4170 [ # # # # : 0 : if (!group || !group->collect_spin_stat) {
# # # # #
# ]
4171 : 0 : continue;
4172 : : }
4173 : :
4174 [ # # # # : 0 : if (group->end_ticks != 0) {
# # ]
4175 [ # # # # : 0 : group->spin_ticks += (group->end_ticks - group->start_ticks);
# # # # #
# # # ]
4176 [ # # # # ]: 0 : group->end_ticks = 0;
4177 : 0 : }
4178 : :
4179 [ # # # # ]: 0 : spin_time += group->spin_ticks;
4180 [ # # # # ]: 0 : group->start_ticks = 0;
4181 [ # # # # ]: 0 : group->spin_ticks = 0;
4182 : 0 : }
4183 : :
4184 [ # # ]: 0 : return (spin_time * 1000000ULL) / spdk_get_ticks_hz();
4185 : : }
4186 : :
4187 : : static void
4188 : 0 : bdev_nvme_reset_device_stat(void *ctx)
4189 : : {
4190 : 0 : struct nvme_bdev *nbdev = ctx;
4191 : :
4192 [ # # # # : 0 : if (nbdev->err_stat != NULL) {
# # ]
4193 [ # # # # : 0 : memset(nbdev->err_stat, 0, sizeof(struct nvme_error_stat));
# # ]
4194 : 0 : }
4195 : 0 : }
4196 : :
4197 : : /* JSON string should be lowercases and underscore delimited string. */
4198 : : static void
4199 : 24 : bdev_nvme_format_nvme_status(char *dst, const char *src)
4200 : : {
4201 : 0 : char tmp[256];
4202 : :
4203 : 24 : spdk_strcpy_replace(dst, 256, src, " - ", "_");
4204 : 24 : spdk_strcpy_replace(tmp, 256, dst, "-", "_");
4205 : 24 : spdk_strcpy_replace(dst, 256, tmp, " ", "_");
4206 : 24 : spdk_strlwr(dst);
4207 : 24 : }
4208 : :
4209 : : static void
4210 : 31 : bdev_nvme_dump_device_stat_json(void *ctx, struct spdk_json_write_ctx *w)
4211 : : {
4212 : 31 : struct nvme_bdev *nbdev = ctx;
4213 : 31 : struct spdk_nvme_status status = {};
4214 : : uint16_t sct, sc;
4215 : 0 : char status_json[256];
4216 : : const char *status_str;
4217 : :
4218 [ + + # # : 31 : if (nbdev->err_stat == NULL) {
# # ]
4219 : 19 : return;
4220 : : }
4221 : :
4222 : 12 : spdk_json_write_named_object_begin(w, "nvme_error");
4223 : :
4224 : 12 : spdk_json_write_named_object_begin(w, "status_type");
4225 [ + + ]: 108 : for (sct = 0; sct < 8; sct++) {
4226 [ + + # # : 96 : if (nbdev->err_stat->status_type[sct] == 0) {
# # # # #
# # # #
# ]
4227 : 84 : continue;
4228 : : }
4229 : 12 : status.sct = sct;
4230 : :
4231 : 12 : status_str = spdk_nvme_cpl_get_status_type_string(&status);
4232 [ - + # # ]: 12 : assert(status_str != NULL);
4233 : 12 : bdev_nvme_format_nvme_status(status_json, status_str);
4234 : :
4235 [ # # # # : 12 : spdk_json_write_named_uint32(w, status_json, nbdev->err_stat->status_type[sct]);
# # # # #
# # # ]
4236 : 0 : }
4237 : 12 : spdk_json_write_object_end(w);
4238 : :
4239 : 12 : spdk_json_write_named_object_begin(w, "status_code");
4240 [ + + ]: 60 : for (sct = 0; sct < 4; sct++) {
4241 : 48 : status.sct = sct;
4242 [ + + ]: 12336 : for (sc = 0; sc < 256; sc++) {
4243 [ + + # # : 12288 : if (nbdev->err_stat->status[sct][sc] == 0) {
# # # # #
# # # # #
# # ]
4244 : 12276 : continue;
4245 : : }
4246 : 12 : status.sc = sc;
4247 : :
4248 : 12 : status_str = spdk_nvme_cpl_get_status_string(&status);
4249 [ - + # # ]: 12 : assert(status_str != NULL);
4250 : 12 : bdev_nvme_format_nvme_status(status_json, status_str);
4251 : :
4252 [ # # # # : 12 : spdk_json_write_named_uint32(w, status_json, nbdev->err_stat->status[sct][sc]);
# # # # #
# # # #
# ]
4253 : 0 : }
4254 : 0 : }
4255 : 12 : spdk_json_write_object_end(w);
4256 : :
4257 : 12 : spdk_json_write_object_end(w);
4258 : 0 : }
4259 : :
4260 : : static bool
4261 : 161658 : bdev_nvme_accel_sequence_supported(void *ctx, enum spdk_bdev_io_type type)
4262 : : {
4263 : 161658 : struct nvme_bdev *nbdev = ctx;
4264 : : struct nvme_ns *nvme_ns;
4265 : : struct spdk_nvme_ctrlr *ctrlr;
4266 : :
4267 [ + + + + : 161658 : if (!g_opts.allow_accel_sequence) {
- + ]
4268 : 161574 : return false;
4269 : : }
4270 : :
4271 [ + + ]: 84 : switch (type) {
4272 : 8 : case SPDK_BDEV_IO_TYPE_WRITE:
4273 : : case SPDK_BDEV_IO_TYPE_READ:
4274 : 8 : break;
4275 : 76 : default:
4276 : 76 : return false;
4277 : : }
4278 : :
4279 [ # # # # : 8 : nvme_ns = TAILQ_FIRST(&nbdev->nvme_ns_list);
# # ]
4280 [ - + # # ]: 8 : assert(nvme_ns != NULL);
4281 : :
4282 [ # # # # : 8 : ctrlr = nvme_ns->ctrlr->ctrlr;
# # # # ]
4283 [ - + # # ]: 8 : assert(ctrlr != NULL);
4284 : :
4285 : 8 : return spdk_nvme_ctrlr_get_flags(ctrlr) & SPDK_NVME_CTRLR_ACCEL_SEQUENCE_SUPPORTED;
4286 : 1281 : }
4287 : :
4288 : : static const struct spdk_bdev_fn_table nvmelib_fn_table = {
4289 : : .destruct = bdev_nvme_destruct,
4290 : : .submit_request = bdev_nvme_submit_request,
4291 : : .io_type_supported = bdev_nvme_io_type_supported,
4292 : : .get_io_channel = bdev_nvme_get_io_channel,
4293 : : .dump_info_json = bdev_nvme_dump_info_json,
4294 : : .write_config_json = bdev_nvme_write_config_json,
4295 : : .get_spin_time = bdev_nvme_get_spin_time,
4296 : : .get_module_ctx = bdev_nvme_get_module_ctx,
4297 : : .get_memory_domains = bdev_nvme_get_memory_domains,
4298 : : .accel_sequence_supported = bdev_nvme_accel_sequence_supported,
4299 : : .reset_device_stat = bdev_nvme_reset_device_stat,
4300 : : .dump_device_stat_json = bdev_nvme_dump_device_stat_json,
4301 : : };
4302 : :
4303 : : typedef int (*bdev_nvme_parse_ana_log_page_cb)(
4304 : : const struct spdk_nvme_ana_group_descriptor *desc, void *cb_arg);
4305 : :
4306 : : static int
4307 : 597 : bdev_nvme_parse_ana_log_page(struct nvme_ctrlr *nvme_ctrlr,
4308 : : bdev_nvme_parse_ana_log_page_cb cb_fn, void *cb_arg)
4309 : : {
4310 : : struct spdk_nvme_ana_group_descriptor *copied_desc;
4311 : : uint8_t *orig_desc;
4312 : : uint32_t i, desc_size, copy_len;
4313 : 597 : int rc = 0;
4314 : :
4315 [ + + # # : 597 : if (nvme_ctrlr->ana_log_page == NULL) {
# # ]
4316 : 0 : return -EINVAL;
4317 : : }
4318 : :
4319 [ # # # # ]: 597 : copied_desc = nvme_ctrlr->copied_ana_desc;
4320 : :
4321 [ # # # # : 597 : orig_desc = (uint8_t *)nvme_ctrlr->ana_log_page + sizeof(struct spdk_nvme_ana_page);
# # ]
4322 [ # # # # ]: 597 : copy_len = nvme_ctrlr->max_ana_log_page_size - sizeof(struct spdk_nvme_ana_page);
4323 : :
4324 [ + + # # : 822 : for (i = 0; i < nvme_ctrlr->ana_log_page->num_ana_group_desc; i++) {
# # # # #
# ]
4325 [ - + - + ]: 697 : memcpy(copied_desc, orig_desc, copy_len);
4326 : :
4327 [ # # # # ]: 697 : rc = cb_fn(copied_desc, cb_arg);
4328 [ + + ]: 697 : if (rc != 0) {
4329 : 472 : break;
4330 : : }
4331 : :
4332 : 225 : desc_size = sizeof(struct spdk_nvme_ana_group_descriptor) +
4333 [ # # # # ]: 225 : copied_desc->num_of_nsid * sizeof(uint32_t);
4334 [ # # ]: 225 : orig_desc += desc_size;
4335 : 225 : copy_len -= desc_size;
4336 : 30 : }
4337 : :
4338 : 597 : return rc;
4339 : 42 : }
4340 : :
4341 : : static int
4342 : 22 : nvme_ns_ana_transition_timedout(void *ctx)
4343 : : {
4344 : 22 : struct nvme_ns *nvme_ns = ctx;
4345 : :
4346 [ # # ]: 22 : spdk_poller_unregister(&nvme_ns->anatt_timer);
4347 [ # # # # ]: 22 : nvme_ns->ana_transition_timedout = true;
4348 : :
4349 : 22 : return SPDK_POLLER_BUSY;
4350 : : }
4351 : :
4352 : : static void
4353 : 613 : _nvme_ns_set_ana_state(struct nvme_ns *nvme_ns,
4354 : : const struct spdk_nvme_ana_group_descriptor *desc)
4355 : : {
4356 : : const struct spdk_nvme_ctrlr_data *cdata;
4357 : :
4358 [ # # # # : 613 : nvme_ns->ana_group_id = desc->ana_group_id;
# # # # ]
4359 [ # # # # : 613 : nvme_ns->ana_state = desc->ana_state;
# # ]
4360 [ # # # # ]: 613 : nvme_ns->ana_state_updating = false;
4361 : :
4362 [ + + + # : 613 : switch (nvme_ns->ana_state) {
# # # ]
4363 : 513 : case SPDK_NVME_ANA_OPTIMIZED_STATE:
4364 : : case SPDK_NVME_ANA_NON_OPTIMIZED_STATE:
4365 [ # # # # ]: 552 : nvme_ns->ana_transition_timedout = false;
4366 [ # # ]: 552 : spdk_poller_unregister(&nvme_ns->anatt_timer);
4367 : 552 : break;
4368 : :
4369 : 51 : case SPDK_NVME_ANA_INACCESSIBLE_STATE:
4370 : : case SPDK_NVME_ANA_CHANGE_STATE:
4371 [ + + # # : 57 : if (nvme_ns->anatt_timer != NULL) {
# # ]
4372 : 19 : break;
4373 : : }
4374 : :
4375 [ # # # # : 38 : cdata = spdk_nvme_ctrlr_get_data(nvme_ns->ctrlr->ctrlr);
# # # # ]
4376 [ # # # # : 38 : nvme_ns->anatt_timer = SPDK_POLLER_REGISTER(nvme_ns_ana_transition_timedout,
# # # # ]
4377 : : nvme_ns,
4378 : : cdata->anatt * SPDK_SEC_TO_USEC);
4379 : 38 : break;
4380 : 3 : default:
4381 : 4 : break;
4382 : : }
4383 : 613 : }
4384 : :
4385 : : static int
4386 : 564 : nvme_ns_set_ana_state(const struct spdk_nvme_ana_group_descriptor *desc, void *cb_arg)
4387 : : {
4388 : 564 : struct nvme_ns *nvme_ns = cb_arg;
4389 : : uint32_t i;
4390 : :
4391 [ + + # # : 564 : assert(nvme_ns->ns != NULL);
# # # # ]
4392 : :
4393 [ + + # # : 652 : for (i = 0; i < desc->num_of_nsid; i++) {
# # ]
4394 [ + + # # : 560 : if (desc->nsid[i] != spdk_nvme_ns_get_id(nvme_ns->ns)) {
# # # # #
# # # ]
4395 : 88 : continue;
4396 : : }
4397 : :
4398 : 472 : _nvme_ns_set_ana_state(nvme_ns, desc);
4399 : 472 : return 1;
4400 : : }
4401 : :
4402 : 92 : return 0;
4403 : 60 : }
4404 : :
4405 : : static int
4406 : 20 : nvme_generate_uuid(const char *sn, uint32_t nsid, struct spdk_uuid *uuid)
4407 : : {
4408 : 20 : int rc = 0;
4409 : 15 : struct spdk_uuid new_uuid, namespace_uuid;
4410 : 20 : char merged_str[SPDK_NVME_CTRLR_SN_LEN + NSID_STR_LEN + 1] = {'\0'};
4411 : : /* This namespace UUID was generated using uuid_generate() method. */
4412 : 20 : const char *namespace_str = {"edaed2de-24bc-4b07-b559-f47ecbe730fd"};
4413 : : int size;
4414 : :
4415 [ + + - + : 20 : assert(strlen(sn) <= SPDK_NVME_CTRLR_SN_LEN);
# # ]
4416 : :
4417 : 20 : spdk_uuid_set_null(&new_uuid);
4418 : 20 : spdk_uuid_set_null(&namespace_uuid);
4419 : :
4420 : 20 : size = snprintf(merged_str, sizeof(merged_str), "%s%"PRIu32, sn, nsid);
4421 [ + - - + ]: 20 : if (size <= 0 || (unsigned long)size >= sizeof(merged_str)) {
4422 : 0 : return -EINVAL;
4423 : : }
4424 : :
4425 : 20 : spdk_uuid_parse(&namespace_uuid, namespace_str);
4426 : :
4427 : 20 : rc = spdk_uuid_generate_sha1(&new_uuid, &namespace_uuid, merged_str, size);
4428 [ + + ]: 20 : if (rc == 0) {
4429 [ # # # # ]: 20 : memcpy(uuid, &new_uuid, sizeof(struct spdk_uuid));
4430 : 5 : }
4431 : :
4432 : 20 : return rc;
4433 : 5 : }
4434 : :
4435 : : static int
4436 : 1481 : nbdev_create(struct spdk_bdev *disk, const char *base_name,
4437 : : struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_ns *ns,
4438 : : struct spdk_bdev_nvme_ctrlr_opts *bdev_opts, void *ctx)
4439 : : {
4440 : : const struct spdk_uuid *uuid;
4441 : : const uint8_t *nguid;
4442 : : const struct spdk_nvme_ctrlr_data *cdata;
4443 : : const struct spdk_nvme_ns_data *nsdata;
4444 : : const struct spdk_nvme_ctrlr_opts *opts;
4445 : : enum spdk_nvme_csi csi;
4446 : : uint32_t atomic_bs, phys_bs, bs;
4447 : 1481 : char sn_tmp[SPDK_NVME_CTRLR_SN_LEN + 1] = {'\0'};
4448 : : int rc;
4449 : :
4450 : 1481 : cdata = spdk_nvme_ctrlr_get_data(ctrlr);
4451 : 1481 : csi = spdk_nvme_ns_get_csi(ns);
4452 : 1481 : opts = spdk_nvme_ctrlr_get_opts(ctrlr);
4453 : :
4454 [ + + - ]: 1481 : switch (csi) {
4455 : 1429 : case SPDK_NVME_CSI_NVM:
4456 [ + - + - ]: 1479 : disk->product_name = "NVMe disk";
4457 : 1479 : break;
4458 : 2 : case SPDK_NVME_CSI_ZNS:
4459 [ # # # # ]: 2 : disk->product_name = "NVMe ZNS disk";
4460 [ # # # # ]: 2 : disk->zoned = true;
4461 [ # # # # ]: 2 : disk->zone_size = spdk_nvme_zns_ns_get_zone_size_sectors(ns);
4462 [ # # # # : 2 : disk->max_zone_append_size = spdk_nvme_zns_ctrlr_get_max_zone_append_size(ctrlr) /
# # # # ]
4463 : 2 : spdk_nvme_ns_get_extended_sector_size(ns);
4464 [ # # # # ]: 2 : disk->max_open_zones = spdk_nvme_zns_ns_get_max_open_zones(ns);
4465 [ # # # # ]: 2 : disk->max_active_zones = spdk_nvme_zns_ns_get_max_active_zones(ns);
4466 : 2 : break;
4467 : 0 : default:
4468 [ # # # # : 0 : if (bdev_opts->allow_unrecognized_csi) {
# # # # ]
4469 [ # # # # ]: 0 : disk->product_name = "NVMe Passthrough disk";
4470 : 0 : break;
4471 : : }
4472 : 0 : SPDK_ERRLOG("unsupported CSI: %u\n", csi);
4473 : 0 : return -ENOTSUP;
4474 : : }
4475 : :
4476 : 1481 : nguid = spdk_nvme_ns_get_nguid(ns);
4477 [ + + ]: 1481 : if (!nguid) {
4478 : 1099 : uuid = spdk_nvme_ns_get_uuid(ns);
4479 [ + + ]: 1099 : if (uuid) {
4480 [ # # ]: 360 : disk->uuid = *uuid;
4481 [ + + + + : 751 : } else if (g_opts.generate_uuids) {
+ - ]
4482 [ # # ]: 0 : spdk_strcpy_pad(sn_tmp, cdata->sn, SPDK_NVME_CTRLR_SN_LEN, '\0');
4483 [ # # ]: 0 : rc = nvme_generate_uuid(sn_tmp, spdk_nvme_ns_get_id(ns), &disk->uuid);
4484 [ # # ]: 0 : if (rc < 0) {
4485 [ # # ]: 0 : SPDK_ERRLOG("UUID generation failed (%s)\n", spdk_strerror(-rc));
4486 : 0 : return rc;
4487 : : }
4488 : 0 : }
4489 : 50 : } else {
4490 [ # # # # : 382 : memcpy(&disk->uuid, nguid, sizeof(disk->uuid));
# # ]
4491 : : }
4492 : :
4493 [ + - + - ]: 1481 : disk->name = spdk_sprintf_alloc("%sn%d", base_name, spdk_nvme_ns_get_id(ns));
4494 [ + + + - : 1481 : if (!disk->name) {
- + ]
4495 : 0 : return -ENOMEM;
4496 : : }
4497 : :
4498 [ + - + - ]: 1481 : disk->write_cache = 0;
4499 [ + + + - : 1481 : if (cdata->vwc.present) {
+ - ]
4500 : : /* Enable if the Volatile Write Cache exists */
4501 [ # # # # ]: 1235 : disk->write_cache = 1;
4502 : 10 : }
4503 [ + + + - : 1481 : if (cdata->oncs.write_zeroes) {
+ - ]
4504 [ # # # # ]: 1243 : disk->max_write_zeroes = UINT16_MAX + 1;
4505 : 10 : }
4506 [ + - + - ]: 1481 : disk->blocklen = spdk_nvme_ns_get_extended_sector_size(ns);
4507 [ + - + - ]: 1481 : disk->blockcnt = spdk_nvme_ns_get_num_sectors(ns);
4508 [ + - + - ]: 1481 : disk->max_segment_size = spdk_nvme_ctrlr_get_max_xfer_size(ctrlr);
4509 [ + - + - : 1481 : disk->ctratt.raw = cdata->ctratt.raw;
+ - + - +
- + - ]
4510 [ + - + - ]: 1481 : disk->nsid = spdk_nvme_ns_get_id(ns);
4511 : : /* NVMe driver will split one request into multiple requests
4512 : : * based on MDTS and stripe boundary, the bdev layer will use
4513 : : * max_segment_size and max_num_segments to split one big IO
4514 : : * into multiple requests, then small request can't run out
4515 : : * of NVMe internal requests data structure.
4516 : : */
4517 [ + - + + : 1481 : if (opts && opts->io_queue_requests) {
+ - - + ]
4518 [ + - + - : 1325 : disk->max_num_segments = opts->io_queue_requests / 2;
+ - + - +
- ]
4519 : 11 : }
4520 [ + + ]: 1481 : if (spdk_nvme_ctrlr_get_flags(ctrlr) & SPDK_NVME_CTRLR_SGL_SUPPORTED) {
4521 : : /* The nvme driver will try to split I/O that have too many
4522 : : * SGEs, but it doesn't work if that last SGE doesn't end on
4523 : : * an aggregate total that is block aligned. The bdev layer has
4524 : : * a more robust splitting framework, so use that instead for
4525 : : * this case. (See issue #3269.)
4526 : : */
4527 : 1235 : uint16_t max_sges = spdk_nvme_ctrlr_get_max_sges(ctrlr);
4528 : :
4529 [ + + # # : 1235 : if (disk->max_num_segments == 0) {
# # ]
4530 [ # # # # ]: 0 : disk->max_num_segments = max_sges;
4531 : 0 : } else {
4532 [ - + # # : 1235 : disk->max_num_segments = spdk_min(disk->max_num_segments, max_sges);
# # # # #
# # # #
# ]
4533 : : }
4534 : 10 : }
4535 [ + - + - ]: 1481 : disk->optimal_io_boundary = spdk_nvme_ns_get_optimal_io_boundary(ns);
4536 : :
4537 : 1481 : nsdata = spdk_nvme_ns_get_data(ns);
4538 : 1481 : bs = spdk_nvme_ns_get_sector_size(ns);
4539 : 1481 : atomic_bs = bs;
4540 : 1481 : phys_bs = bs;
4541 [ + + + - : 1481 : if (nsdata->nabo == 0) {
- + ]
4542 [ + + + + : 1481 : if (nsdata->nsfeat.ns_atomic_write_unit && nsdata->nawupf) {
- + # # #
# # # ]
4543 [ # # # # : 8 : atomic_bs = bs * (1 + nsdata->nawupf);
# # ]
4544 : 0 : } else {
4545 [ + - + - : 1473 : atomic_bs = bs * (1 + cdata->awupf);
+ - ]
4546 : : }
4547 : 50 : }
4548 [ + + + - : 1481 : if (nsdata->nsfeat.optperf) {
+ - ]
4549 [ # # # # : 1243 : phys_bs = bs * (1 + nsdata->npwg);
# # ]
4550 : 10 : }
4551 [ - + + - : 1481 : disk->phys_blocklen = spdk_min(phys_bs, atomic_bs);
+ - ]
4552 : :
4553 [ + - + - ]: 1481 : disk->md_len = spdk_nvme_ns_get_md_size(ns);
4554 [ + + + - : 1481 : if (disk->md_len != 0) {
- + ]
4555 [ # # # # : 47 : disk->md_interleave = nsdata->flbas.extended;
# # # # ]
4556 [ # # # # ]: 47 : disk->dif_type = (enum spdk_dif_type)spdk_nvme_ns_get_pi_type(ns);
4557 [ - + # # : 47 : if (disk->dif_type != SPDK_DIF_DISABLE) {
# # ]
4558 [ # # # # : 0 : disk->dif_is_head_of_md = nsdata->dps.md_start;
# # # # ]
4559 [ # # # # : 0 : disk->dif_check_flags = bdev_opts->prchk_flags;
# # # # ]
4560 [ # # # # ]: 0 : disk->dif_pi_format = (enum spdk_dif_pi_format)spdk_nvme_ns_get_pi_format(ns);
4561 : 0 : }
4562 : 0 : }
4563 : :
4564 [ + + ]: 1481 : if (!(spdk_nvme_ctrlr_get_flags(ctrlr) &
4565 : : SPDK_NVME_CTRLR_COMPARE_AND_WRITE_SUPPORTED)) {
4566 [ + - + - ]: 1107 : disk->acwu = 0;
4567 [ + - # # : 424 : } else if (nsdata->nsfeat.ns_atomic_write_unit) {
# # ]
4568 [ # # # # : 374 : disk->acwu = nsdata->nacwu + 1; /* 0-based */
# # # # #
# ]
4569 : 0 : } else {
4570 [ # # # # : 0 : disk->acwu = cdata->acwu + 1; /* 0-based */
# # # # #
# ]
4571 : : }
4572 : :
4573 [ + + + - : 1481 : if (cdata->oncs.copy) {
+ - ]
4574 : : /* For now bdev interface allows only single segment copy */
4575 [ # # # # : 923 : disk->max_copy = nsdata->mssrl;
# # # # ]
4576 : 10 : }
4577 : :
4578 [ + - + - ]: 1481 : disk->ctxt = ctx;
4579 [ + - + - ]: 1481 : disk->fn_table = &nvmelib_fn_table;
4580 [ + - + - ]: 1481 : disk->module = &nvme_if;
4581 : :
4582 [ + - + - ]: 1481 : disk->numa.id_valid = 1;
4583 [ + - + - ]: 1481 : disk->numa.id = spdk_nvme_ctrlr_get_numa_id(ctrlr);
4584 : :
4585 : 1481 : return 0;
4586 : 50 : }
4587 : :
4588 : : static struct nvme_bdev *
4589 : 1481 : nvme_bdev_alloc(void)
4590 : : {
4591 : : struct nvme_bdev *nbdev;
4592 : : int rc;
4593 : :
4594 : 1481 : nbdev = calloc(1, sizeof(*nbdev));
4595 [ + + ]: 1481 : if (!nbdev) {
4596 : 0 : SPDK_ERRLOG("nbdev calloc() failed\n");
4597 : 0 : return NULL;
4598 : : }
4599 : :
4600 [ + + + + : 1481 : if (g_opts.nvme_error_stat) {
+ - ]
4601 [ # # # # ]: 12 : nbdev->err_stat = calloc(1, sizeof(struct nvme_error_stat));
4602 [ - + # # : 12 : if (!nbdev->err_stat) {
# # ]
4603 : 0 : SPDK_ERRLOG("err_stat calloc() failed\n");
4604 : 0 : free(nbdev);
4605 : 0 : return NULL;
4606 : : }
4607 : 0 : }
4608 : :
4609 [ + + + - ]: 1481 : rc = pthread_mutex_init(&nbdev->mutex, NULL);
4610 [ - + ]: 1481 : if (rc != 0) {
4611 [ # # # # ]: 0 : free(nbdev->err_stat);
4612 : 0 : free(nbdev);
4613 : 0 : return NULL;
4614 : : }
4615 : :
4616 [ - + - + ]: 1481 : nbdev->ref = 1;
4617 [ - + - + ]: 1481 : nbdev->mp_policy = BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE;
4618 [ - + - + ]: 1481 : nbdev->mp_selector = BDEV_NVME_MP_SELECTOR_ROUND_ROBIN;
4619 [ - + - + ]: 1481 : nbdev->rr_min_io = UINT32_MAX;
4620 [ - + - + : 1481 : TAILQ_INIT(&nbdev->nvme_ns_list);
- + - + -
+ - + - +
- + ]
4621 : :
4622 : 1481 : return nbdev;
4623 : 50 : }
4624 : :
4625 : : static int
4626 : 1481 : nvme_bdev_create(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *nvme_ns)
4627 : : {
4628 : : struct nvme_bdev *nbdev;
4629 [ + - + - ]: 1481 : struct nvme_bdev_ctrlr *nbdev_ctrlr = nvme_ctrlr->nbdev_ctrlr;
4630 : : int rc;
4631 : :
4632 : 1481 : nbdev = nvme_bdev_alloc();
4633 [ + + ]: 1481 : if (nbdev == NULL) {
4634 : 0 : SPDK_ERRLOG("Failed to allocate NVMe bdev\n");
4635 : 0 : return -ENOMEM;
4636 : : }
4637 : :
4638 [ + - + - : 1481 : nbdev->opal = nvme_ctrlr->opal_dev != NULL;
+ - + - ]
4639 : :
4640 [ + - + - : 1531 : rc = nbdev_create(&nbdev->disk, nbdev_ctrlr->name, nvme_ctrlr->ctrlr,
+ - + - +
- ]
4641 [ + - + - : 50 : nvme_ns->ns, &nvme_ctrlr->opts, nbdev);
+ - ]
4642 [ - + ]: 1481 : if (rc != 0) {
4643 : 0 : SPDK_ERRLOG("Failed to create NVMe disk\n");
4644 : 0 : nvme_bdev_free(nbdev);
4645 : 0 : return rc;
4646 : : }
4647 : :
4648 : 1531 : spdk_io_device_register(nbdev,
4649 : : bdev_nvme_create_bdev_channel_cb,
4650 : : bdev_nvme_destroy_bdev_channel_cb,
4651 : : sizeof(struct nvme_bdev_channel),
4652 [ + - + - : 1481 : nbdev->disk.name);
+ - ]
4653 : :
4654 [ + - + - ]: 1481 : nvme_ns->bdev = nbdev;
4655 [ + - + - : 1481 : nbdev->nsid = nvme_ns->id;
+ - + - ]
4656 [ + - + - : 1481 : TAILQ_INSERT_TAIL(&nbdev->nvme_ns_list, nvme_ns, tailq);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
4657 : :
4658 [ + + ]: 1481 : pthread_mutex_lock(&g_bdev_nvme_mutex);
4659 : :
4660 [ + - + - ]: 1481 : nbdev->nbdev_ctrlr = nbdev_ctrlr;
4661 [ + - + - : 1481 : TAILQ_INSERT_TAIL(&nbdev_ctrlr->bdevs, nbdev, tailq);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
4662 : :
4663 [ + - ]: 1481 : rc = spdk_bdev_register(&nbdev->disk);
4664 [ + + ]: 1481 : if (rc != 0) {
4665 : 6 : SPDK_ERRLOG("spdk_bdev_register() failed\n");
4666 : 6 : spdk_io_device_unregister(nbdev, NULL);
4667 [ # # # # ]: 6 : nvme_ns->bdev = NULL;
4668 : :
4669 [ + + # # : 6 : TAILQ_REMOVE(&nbdev_ctrlr->bdevs, nbdev, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
4670 : :
4671 [ - + ]: 6 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
4672 : :
4673 : 6 : nvme_bdev_free(nbdev);
4674 : 6 : return rc;
4675 : : }
4676 : :
4677 [ + + ]: 1475 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
4678 : :
4679 : 1475 : return 0;
4680 : 50 : }
4681 : :
4682 : : static bool
4683 : 126 : bdev_nvme_compare_ns(struct spdk_nvme_ns *ns1, struct spdk_nvme_ns *ns2)
4684 : : {
4685 : : const struct spdk_nvme_ns_data *nsdata1, *nsdata2;
4686 : : const struct spdk_uuid *uuid1, *uuid2;
4687 : :
4688 : 126 : nsdata1 = spdk_nvme_ns_get_data(ns1);
4689 : 126 : nsdata2 = spdk_nvme_ns_get_data(ns2);
4690 : 126 : uuid1 = spdk_nvme_ns_get_uuid(ns1);
4691 : 126 : uuid2 = spdk_nvme_ns_get_uuid(ns2);
4692 : :
4693 [ + + - + : 196 : return memcmp(nsdata1->nguid, nsdata2->nguid, sizeof(nsdata1->nguid)) == 0 &&
# # # # #
# ]
4694 [ + + + + : 122 : nsdata1->eui64 == nsdata2->eui64 &&
# # # # #
# ]
4695 [ + + + + ]: 118 : ((uuid1 == NULL && uuid2 == NULL) ||
4696 [ + + + - : 308 : (uuid1 != NULL && uuid2 != NULL && spdk_uuid_compare(uuid1, uuid2) == 0)) &&
+ + + + ]
4697 : 106 : spdk_nvme_ns_get_csi(ns1) == spdk_nvme_ns_get_csi(ns2);
4698 : : }
4699 : :
4700 : : static bool
4701 : 56 : hotplug_probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
4702 : : struct spdk_nvme_ctrlr_opts *opts)
4703 : : {
4704 : : struct nvme_probe_skip_entry *entry;
4705 : :
4706 [ - + # # : 56 : TAILQ_FOREACH(entry, &g_skipped_nvme_ctrlrs, tailq) {
# # # # ]
4707 [ # # # # ]: 0 : if (spdk_nvme_transport_id_compare(trid, &entry->trid) == 0) {
4708 : 0 : return false;
4709 : : }
4710 : 0 : }
4711 : :
4712 [ # # # # : 56 : opts->arbitration_burst = (uint8_t)g_opts.arbitration_burst;
# # ]
4713 [ # # # # : 56 : opts->low_priority_weight = (uint8_t)g_opts.low_priority_weight;
# # ]
4714 [ # # # # : 56 : opts->medium_priority_weight = (uint8_t)g_opts.medium_priority_weight;
# # ]
4715 [ # # # # : 56 : opts->high_priority_weight = (uint8_t)g_opts.high_priority_weight;
# # ]
4716 [ # # # # ]: 56 : opts->disable_read_ana_log_page = true;
4717 : :
4718 [ - + - + : 56 : SPDK_DEBUGLOG(bdev_nvme, "Attaching to %s\n", trid->traddr);
# # # # ]
4719 : :
4720 : 56 : return true;
4721 : 0 : }
4722 : :
4723 : : static void
4724 : 0 : nvme_abort_cpl(void *ctx, const struct spdk_nvme_cpl *cpl)
4725 : : {
4726 : 0 : struct nvme_ctrlr *nvme_ctrlr = ctx;
4727 : :
4728 [ # # # # : 0 : if (spdk_nvme_cpl_is_error(cpl)) {
# # # # #
# # # # #
# # ]
4729 [ # # # # : 0 : NVME_CTRLR_WARNLOG(nvme_ctrlr, "Abort failed. Resetting controller. sc is %u, sct is %u.\n",
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
4730 : : cpl->status.sc, cpl->status.sct);
4731 : 0 : bdev_nvme_reset_ctrlr(nvme_ctrlr);
4732 [ # # # # : 0 : } else if (cpl->cdw0 & 0x1) {
# # ]
4733 [ # # # # : 0 : NVME_CTRLR_WARNLOG(nvme_ctrlr, "Specified command could not be aborted.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
4734 : 0 : bdev_nvme_reset_ctrlr(nvme_ctrlr);
4735 : 0 : }
4736 : 0 : }
4737 : :
4738 : : static void
4739 : 0 : timeout_cb(void *cb_arg, struct spdk_nvme_ctrlr *ctrlr,
4740 : : struct spdk_nvme_qpair *qpair, uint16_t cid)
4741 : : {
4742 : 0 : struct nvme_ctrlr *nvme_ctrlr = cb_arg;
4743 : : union spdk_nvme_csts_register csts;
4744 : : int rc;
4745 : :
4746 [ # # # # : 0 : assert(nvme_ctrlr->ctrlr == ctrlr);
# # # # ]
4747 : :
4748 [ # # # # : 0 : NVME_CTRLR_WARNLOG(nvme_ctrlr, "Warning: Detected a timeout. ctrlr=%p qpair=%p cid=%u\n",
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
4749 : : ctrlr, qpair, cid);
4750 : :
4751 : : /* Only try to read CSTS if it's a PCIe controller or we have a timeout on an I/O
4752 : : * queue. (Note: qpair == NULL when there's an admin cmd timeout.) Otherwise we
4753 : : * would submit another fabrics cmd on the admin queue to read CSTS and check for its
4754 : : * completion recursively.
4755 : : */
4756 [ # # # # : 0 : if (nvme_ctrlr->active_path_id->trid.trtype == SPDK_NVME_TRANSPORT_PCIE || qpair != NULL) {
# # # # #
# # # #
# ]
4757 : 0 : csts = spdk_nvme_ctrlr_get_regs_csts(ctrlr);
4758 [ # # ]: 0 : if (csts.bits.cfs) {
4759 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr, "Controller Fatal Status, reset required\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
4760 : 0 : bdev_nvme_reset_ctrlr(nvme_ctrlr);
4761 : 0 : return;
4762 : : }
4763 : 0 : }
4764 : :
4765 [ # # # # : 0 : switch (g_opts.action_on_timeout) {
# # ]
4766 : 0 : case SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT:
4767 [ # # ]: 0 : if (qpair) {
4768 : : /* Don't send abort to ctrlr when ctrlr is not available. */
4769 [ # # # # ]: 0 : pthread_mutex_lock(&nvme_ctrlr->mutex);
4770 [ # # ]: 0 : if (!nvme_ctrlr_is_available(nvme_ctrlr)) {
4771 [ # # # # ]: 0 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
4772 [ # # # # : 0 : NVME_CTRLR_NOTICELOG(nvme_ctrlr, "Quit abort. Ctrlr is not available.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
4773 : 0 : return;
4774 : : }
4775 [ # # # # ]: 0 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
4776 : :
4777 : 0 : rc = spdk_nvme_ctrlr_cmd_abort(ctrlr, qpair, cid,
4778 : 0 : nvme_abort_cpl, nvme_ctrlr);
4779 [ # # ]: 0 : if (rc == 0) {
4780 : 0 : return;
4781 : : }
4782 : :
4783 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr, "Unable to send abort. Resetting, rc is %d.\n", rc);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
4784 : 0 : }
4785 : :
4786 : : /* FALLTHROUGH */
4787 : : case SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET:
4788 : 0 : bdev_nvme_reset_ctrlr(nvme_ctrlr);
4789 : 0 : break;
4790 : 0 : case SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE:
4791 [ # # # # : 0 : NVME_CTRLR_DEBUGLOG(nvme_ctrlr, "No action for nvme controller timeout.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
4792 : 0 : break;
4793 : 0 : default:
4794 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr, "An invalid timeout action value is found.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
4795 : 0 : break;
4796 : : }
4797 : 0 : }
4798 : :
4799 : : static struct nvme_ns *
4800 : 1551 : nvme_ns_alloc(void)
4801 : : {
4802 : : struct nvme_ns *nvme_ns;
4803 : :
4804 : 1551 : nvme_ns = calloc(1, sizeof(struct nvme_ns));
4805 [ + + ]: 1551 : if (nvme_ns == NULL) {
4806 : 0 : return NULL;
4807 : : }
4808 : :
4809 [ + + + + ]: 1551 : if (g_opts.io_path_stat) {
4810 [ # # # # ]: 0 : nvme_ns->stat = calloc(1, sizeof(struct spdk_bdev_io_stat));
4811 [ # # # # : 0 : if (nvme_ns->stat == NULL) {
# # ]
4812 : 0 : free(nvme_ns);
4813 : 0 : return NULL;
4814 : : }
4815 [ # # # # ]: 0 : spdk_bdev_reset_io_stat(nvme_ns->stat, SPDK_BDEV_RESET_STAT_MAXMIN);
4816 : 0 : }
4817 : :
4818 : 1551 : return nvme_ns;
4819 : 63 : }
4820 : :
4821 : : static void
4822 : 1551 : nvme_ns_free(struct nvme_ns *nvme_ns)
4823 : : {
4824 [ + - + - ]: 1551 : free(nvme_ns->stat);
4825 : 1551 : free(nvme_ns);
4826 : 1551 : }
4827 : :
4828 : : static void
4829 : 1551 : nvme_ctrlr_populate_namespace_done(struct nvme_ns *nvme_ns, int rc)
4830 : : {
4831 [ + - + - ]: 1551 : struct nvme_ctrlr *nvme_ctrlr = nvme_ns->ctrlr;
4832 [ + - + - ]: 1551 : struct nvme_async_probe_ctx *ctx = nvme_ns->probe_ctx;
4833 : :
4834 [ + + ]: 1551 : if (rc == 0) {
4835 [ - + - + ]: 1541 : nvme_ns->probe_ctx = NULL;
4836 : 1541 : nvme_ctrlr_get_ref(nvme_ctrlr);
4837 : 61 : } else {
4838 [ - + # # ]: 10 : pthread_mutex_lock(&nvme_ctrlr->mutex);
4839 [ # # ]: 10 : RB_REMOVE(nvme_ns_tree, &nvme_ctrlr->namespaces, nvme_ns);
4840 [ - + # # ]: 10 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
4841 : :
4842 : 10 : nvme_ns_free(nvme_ns);
4843 : : }
4844 : :
4845 [ + + ]: 1551 : if (ctx) {
4846 [ + - ]: 1486 : ctx->populates_in_progress--;
4847 [ + + + - : 1486 : if (ctx->populates_in_progress == 0) {
+ - ]
4848 : 61 : nvme_ctrlr_populate_namespaces_done(nvme_ctrlr, ctx);
4849 : 12 : }
4850 : 62 : }
4851 : 1551 : }
4852 : :
4853 : : static void
4854 : 8 : bdev_nvme_add_io_path(struct nvme_bdev_channel_iter *i,
4855 : : struct nvme_bdev *nbdev,
4856 : : struct nvme_bdev_channel *nbdev_ch, void *ctx)
4857 : : {
4858 : 8 : struct nvme_ns *nvme_ns = ctx;
4859 : : int rc;
4860 : :
4861 : 8 : rc = _bdev_nvme_add_io_path(nbdev_ch, nvme_ns);
4862 [ - + ]: 8 : if (rc != 0) {
4863 : 0 : SPDK_ERRLOG("Failed to add I/O path to bdev_channel dynamically.\n");
4864 : 0 : }
4865 : :
4866 : 8 : nvme_bdev_for_each_channel_continue(i, rc);
4867 : 8 : }
4868 : :
4869 : : static void
4870 : 8 : bdev_nvme_delete_io_path(struct nvme_bdev_channel_iter *i,
4871 : : struct nvme_bdev *nbdev,
4872 : : struct nvme_bdev_channel *nbdev_ch, void *ctx)
4873 : : {
4874 : 8 : struct nvme_ns *nvme_ns = ctx;
4875 : : struct nvme_io_path *io_path;
4876 : :
4877 : 8 : io_path = _bdev_nvme_get_io_path(nbdev_ch, nvme_ns);
4878 [ + - ]: 8 : if (io_path != NULL) {
4879 : 8 : _bdev_nvme_delete_io_path(nbdev_ch, io_path);
4880 : 2 : }
4881 : :
4882 : 8 : nvme_bdev_for_each_channel_continue(i, 0);
4883 : 8 : }
4884 : :
4885 : : static void
4886 : 0 : bdev_nvme_add_io_path_failed(struct nvme_bdev *nbdev, void *ctx, int status)
4887 : : {
4888 : 0 : struct nvme_ns *nvme_ns = ctx;
4889 : :
4890 : 0 : nvme_ctrlr_populate_namespace_done(nvme_ns, -1);
4891 : 0 : }
4892 : :
4893 : : static void
4894 : 66 : bdev_nvme_add_io_path_done(struct nvme_bdev *nbdev, void *ctx, int status)
4895 : : {
4896 : 66 : struct nvme_ns *nvme_ns = ctx;
4897 : :
4898 [ + - ]: 66 : if (status == 0) {
4899 : 66 : nvme_ctrlr_populate_namespace_done(nvme_ns, 0);
4900 : 12 : } else {
4901 : : /* Delete the added io_paths and fail populating the namespace. */
4902 : 0 : nvme_bdev_for_each_channel(nbdev,
4903 : : bdev_nvme_delete_io_path,
4904 : 0 : nvme_ns,
4905 : : bdev_nvme_add_io_path_failed);
4906 : : }
4907 : 66 : }
4908 : :
4909 : : static int
4910 : 70 : nvme_bdev_add_ns(struct nvme_bdev *nbdev, struct nvme_ns *nvme_ns)
4911 : : {
4912 : : struct nvme_ns *tmp_ns;
4913 : : const struct spdk_nvme_ns_data *nsdata;
4914 : :
4915 [ # # # # ]: 70 : nsdata = spdk_nvme_ns_get_data(nvme_ns->ns);
4916 [ - + # # : 70 : if (!nsdata->nmic.can_share) {
# # ]
4917 : 0 : SPDK_ERRLOG("Namespace cannot be shared.\n");
4918 : 0 : return -EINVAL;
4919 : : }
4920 : :
4921 [ - + # # ]: 70 : pthread_mutex_lock(&nbdev->mutex);
4922 : :
4923 [ # # # # : 70 : tmp_ns = TAILQ_FIRST(&nbdev->nvme_ns_list);
# # ]
4924 [ - + # # ]: 70 : assert(tmp_ns != NULL);
4925 : :
4926 [ + - + + : 70 : if (tmp_ns->ns != NULL && !bdev_nvme_compare_ns(nvme_ns->ns, tmp_ns->ns)) {
# # # # #
# # # # #
# # ]
4927 [ - + # # ]: 4 : pthread_mutex_unlock(&nbdev->mutex);
4928 : 4 : SPDK_ERRLOG("Namespaces are not identical.\n");
4929 : 4 : return -EINVAL;
4930 : : }
4931 : :
4932 [ # # # # ]: 66 : nbdev->ref++;
4933 [ # # # # : 66 : TAILQ_INSERT_TAIL(&nbdev->nvme_ns_list, nvme_ns, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
4934 [ # # # # ]: 66 : nvme_ns->bdev = nbdev;
4935 : :
4936 [ - + # # ]: 66 : pthread_mutex_unlock(&nbdev->mutex);
4937 : :
4938 : : /* Add nvme_io_path to nvme_bdev_channels dynamically. */
4939 : 78 : nvme_bdev_for_each_channel(nbdev,
4940 : : bdev_nvme_add_io_path,
4941 : 12 : nvme_ns,
4942 : : bdev_nvme_add_io_path_done);
4943 : :
4944 : 66 : return 0;
4945 : 13 : }
4946 : :
4947 : : static void
4948 : 1551 : nvme_ctrlr_populate_namespace(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *nvme_ns)
4949 : : {
4950 : : struct spdk_nvme_ns *ns;
4951 : : struct nvme_bdev *bdev;
4952 : 1551 : int rc = 0;
4953 : :
4954 [ + - + - : 1551 : ns = spdk_nvme_ctrlr_get_ns(nvme_ctrlr->ctrlr, nvme_ns->id);
+ - + - ]
4955 [ - + ]: 1551 : if (!ns) {
4956 [ # # # # : 0 : NVME_CTRLR_DEBUGLOG(nvme_ctrlr, "Invalid NS %d\n", nvme_ns->id);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
4957 : 0 : rc = -EINVAL;
4958 : 0 : goto done;
4959 : : }
4960 : :
4961 [ + - + - ]: 1551 : nvme_ns->ns = ns;
4962 [ + - + - ]: 1551 : nvme_ns->ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
4963 : :
4964 [ + + + - : 1551 : if (nvme_ctrlr->ana_log_page != NULL) {
+ - ]
4965 : 476 : bdev_nvme_parse_ana_log_page(nvme_ctrlr, nvme_ns_set_ana_state, nvme_ns);
4966 : 38 : }
4967 : :
4968 [ + - + - : 1551 : bdev = nvme_bdev_ctrlr_get_bdev(nvme_ctrlr->nbdev_ctrlr, nvme_ns->id);
+ - + - ]
4969 [ + + ]: 1602 : if (bdev == NULL) {
4970 : 1481 : rc = nvme_bdev_create(nvme_ctrlr, nvme_ns);
4971 : 50 : } else {
4972 : 70 : rc = nvme_bdev_add_ns(bdev, nvme_ns);
4973 [ + + ]: 70 : if (rc == 0) {
4974 : 66 : return;
4975 : : }
4976 : : }
4977 : 3 : done:
4978 : 1485 : nvme_ctrlr_populate_namespace_done(nvme_ns, rc);
4979 : 63 : }
4980 : :
4981 : : static void
4982 : 1541 : nvme_ctrlr_depopulate_namespace_done(struct nvme_ns *nvme_ns)
4983 : : {
4984 [ + - + - ]: 1541 : struct nvme_ctrlr *nvme_ctrlr = nvme_ns->ctrlr;
4985 : :
4986 [ + + # # ]: 1541 : assert(nvme_ctrlr != NULL);
4987 : :
4988 [ + + + - ]: 1541 : pthread_mutex_lock(&nvme_ctrlr->mutex);
4989 : :
4990 [ + - ]: 1541 : RB_REMOVE(nvme_ns_tree, &nvme_ctrlr->namespaces, nvme_ns);
4991 : :
4992 [ + + + - : 1541 : if (nvme_ns->bdev != NULL) {
- + ]
4993 [ - + # # ]: 515 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
4994 : 515 : return;
4995 : : }
4996 : :
4997 : 1026 : nvme_ns_free(nvme_ns);
4998 [ + + + - ]: 1026 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
4999 : :
5000 : 1026 : nvme_ctrlr_put_ref(nvme_ctrlr);
5001 : 61 : }
5002 : :
5003 : : static void
5004 : 56 : bdev_nvme_delete_io_path_done(struct nvme_bdev *nbdev, void *ctx, int status)
5005 : : {
5006 : 56 : struct nvme_ns *nvme_ns = ctx;
5007 : :
5008 : 56 : nvme_ctrlr_depopulate_namespace_done(nvme_ns);
5009 : 56 : }
5010 : :
5011 : : static void
5012 : 1541 : nvme_ctrlr_depopulate_namespace(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *nvme_ns)
5013 : : {
5014 : : struct nvme_bdev *nbdev;
5015 : :
5016 [ + - ]: 1541 : spdk_poller_unregister(&nvme_ns->anatt_timer);
5017 : :
5018 [ + - + - ]: 1541 : nbdev = nvme_ns->bdev;
5019 [ + + ]: 1541 : if (nbdev != NULL) {
5020 [ - + # # ]: 711 : pthread_mutex_lock(&nbdev->mutex);
5021 : :
5022 [ + + # # : 711 : assert(nbdev->ref > 0);
# # # # ]
5023 [ # # # # ]: 711 : nbdev->ref--;
5024 [ + + # # : 711 : if (nbdev->ref == 0) {
# # ]
5025 [ - + # # ]: 655 : pthread_mutex_unlock(&nbdev->mutex);
5026 : :
5027 [ # # ]: 655 : spdk_bdev_unregister(&nbdev->disk, NULL, NULL);
5028 : 38 : } else {
5029 : : /* spdk_bdev_unregister() is not called until the last nvme_ns is
5030 : : * depopulated. Hence we need to remove nvme_ns from bdev->nvme_ns_list
5031 : : * and clear nvme_ns->bdev here.
5032 : : */
5033 [ + + # # : 56 : TAILQ_REMOVE(&nbdev->nvme_ns_list, nvme_ns, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
5034 : :
5035 [ - + # # : 56 : pthread_mutex_lock(&nvme_ns->ctrlr->mutex);
# # # # ]
5036 [ # # # # ]: 56 : nvme_ns->bdev = NULL;
5037 [ - + # # : 56 : pthread_mutex_unlock(&nvme_ns->ctrlr->mutex);
# # # # ]
5038 : :
5039 [ - + # # ]: 56 : pthread_mutex_unlock(&nbdev->mutex);
5040 : :
5041 : : /* Delete nvme_io_paths from nvme_bdev_channels dynamically. After that,
5042 : : * we call depopulate_namespace_done() to avoid use-after-free.
5043 : : */
5044 : 67 : nvme_bdev_for_each_channel(nbdev,
5045 : : bdev_nvme_delete_io_path,
5046 : 11 : nvme_ns,
5047 : : bdev_nvme_delete_io_path_done);
5048 : 56 : return;
5049 : : }
5050 : 38 : }
5051 : :
5052 : 1485 : nvme_ctrlr_depopulate_namespace_done(nvme_ns);
5053 : 61 : }
5054 : :
5055 : : static void
5056 : 1798 : nvme_ctrlr_populate_namespaces(struct nvme_ctrlr *nvme_ctrlr,
5057 : : struct nvme_async_probe_ctx *ctx)
5058 : : {
5059 [ + - + - ]: 1798 : struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
5060 : : struct nvme_ns *nvme_ns, *next;
5061 : : struct spdk_nvme_ns *ns;
5062 : : struct nvme_bdev *nbdev;
5063 : : uint32_t nsid;
5064 : : int rc;
5065 : : uint64_t num_sectors;
5066 : :
5067 [ + + ]: 1798 : if (ctx) {
5068 : : /* Initialize this count to 1 to handle the populate functions
5069 : : * calling nvme_ctrlr_populate_namespace_done() immediately.
5070 : : */
5071 [ + - + - ]: 1652 : ctx->populates_in_progress = 1;
5072 : 58 : }
5073 : :
5074 : : /* First loop over our existing namespaces and see if they have been
5075 : : * removed. */
5076 : 1798 : nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
5077 [ + + ]: 1832 : while (nvme_ns != NULL) {
5078 : 34 : next = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns);
5079 : :
5080 [ + + # # : 34 : if (spdk_nvme_ctrlr_is_active_ns(ctrlr, nvme_ns->id)) {
# # ]
5081 : : /* NS is still there or added again. Its attributes may have changed. */
5082 [ # # # # ]: 21 : ns = spdk_nvme_ctrlr_get_ns(ctrlr, nvme_ns->id);
5083 [ + + # # : 21 : if (nvme_ns->ns != ns) {
# # ]
5084 [ + + # # : 4 : assert(nvme_ns->ns == NULL);
# # # # ]
5085 [ # # # # ]: 4 : nvme_ns->ns = ns;
5086 [ + + - + : 4 : NVME_CTRLR_DEBUGLOG(nvme_ctrlr, "NSID %u was added\n", nvme_ns->id);
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
5087 : 1 : }
5088 : :
5089 : 21 : num_sectors = spdk_nvme_ns_get_num_sectors(ns);
5090 [ # # # # ]: 21 : nbdev = nvme_ns->bdev;
5091 [ + + # # ]: 21 : assert(nbdev != NULL);
5092 [ + + # # : 21 : if (nbdev->disk.blockcnt != num_sectors) {
# # # # ]
5093 [ + - # # : 4 : NVME_CTRLR_NOTICELOG(nvme_ctrlr,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
5094 : : "NSID %u is resized: bdev name %s, old size %" PRIu64 ", new size %" PRIu64 "\n",
5095 : : nvme_ns->id,
5096 : : nbdev->disk.name,
5097 : : nbdev->disk.blockcnt,
5098 : : num_sectors);
5099 [ # # ]: 4 : rc = spdk_bdev_notify_blockcnt_change(&nbdev->disk, num_sectors);
5100 [ + + ]: 4 : if (rc != 0) {
5101 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
5102 : : "Could not change num blocks for nvme bdev: name %s, errno: %d.\n",
5103 : : nbdev->disk.name, rc);
5104 : 0 : }
5105 : 1 : }
5106 : 3 : } else {
5107 : : /* Namespace was removed */
5108 : 13 : nvme_ctrlr_depopulate_namespace(nvme_ctrlr, nvme_ns);
5109 : : }
5110 : :
5111 : 34 : nvme_ns = next;
5112 : : }
5113 : :
5114 : : /* Loop through all of the namespaces at the nvme level and see if any of them are new */
5115 : 1798 : nsid = spdk_nvme_ctrlr_get_first_active_ns(ctrlr);
5116 [ + + ]: 3370 : while (nsid != 0) {
5117 : 1572 : nvme_ns = nvme_ctrlr_get_ns(nvme_ctrlr, nsid);
5118 : :
5119 [ + + ]: 1572 : if (nvme_ns == NULL) {
5120 : : /* Found a new one */
5121 : 1551 : nvme_ns = nvme_ns_alloc();
5122 [ + + ]: 1551 : if (nvme_ns == NULL) {
5123 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to allocate namespace\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
5124 : : /* This just fails to attach the namespace. It may work on a future attempt. */
5125 : 0 : continue;
5126 : : }
5127 : :
5128 [ + - + - ]: 1551 : nvme_ns->id = nsid;
5129 [ + - + - ]: 1551 : nvme_ns->ctrlr = nvme_ctrlr;
5130 : :
5131 [ + - + - ]: 1551 : nvme_ns->bdev = NULL;
5132 : :
5133 [ + + ]: 1551 : if (ctx) {
5134 [ + - ]: 1486 : ctx->populates_in_progress++;
5135 : 62 : }
5136 [ + - + - ]: 1551 : nvme_ns->probe_ctx = ctx;
5137 : :
5138 [ + + + - ]: 1551 : pthread_mutex_lock(&nvme_ctrlr->mutex);
5139 [ + - ]: 1551 : RB_INSERT(nvme_ns_tree, &nvme_ctrlr->namespaces, nvme_ns);
5140 [ + + + - ]: 1551 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
5141 : :
5142 : 1551 : nvme_ctrlr_populate_namespace(nvme_ctrlr, nvme_ns);
5143 : 63 : }
5144 : :
5145 : 1572 : nsid = spdk_nvme_ctrlr_get_next_active_ns(ctrlr, nsid);
5146 : : }
5147 : :
5148 [ + + ]: 1798 : if (ctx) {
5149 : : /* Decrement this count now that the loop is over to account
5150 : : * for the one we started with. If the count is then 0, we
5151 : : * know any populate_namespace functions completed immediately,
5152 : : * so we'll kick the callback here.
5153 : : */
5154 [ + - ]: 1652 : ctx->populates_in_progress--;
5155 [ + + + - : 1652 : if (ctx->populates_in_progress == 0) {
- + ]
5156 : 1591 : nvme_ctrlr_populate_namespaces_done(nvme_ctrlr, ctx);
5157 : 46 : }
5158 : 58 : }
5159 : :
5160 : 1798 : }
5161 : :
5162 : : static void
5163 : 1768 : nvme_ctrlr_depopulate_namespaces(struct nvme_ctrlr *nvme_ctrlr)
5164 : : {
5165 : : struct nvme_ns *nvme_ns, *tmp;
5166 : :
5167 [ + + + + : 3296 : RB_FOREACH_SAFE(nvme_ns, nvme_ns_tree, &nvme_ctrlr->namespaces, tmp) {
+ + ]
5168 : 1528 : nvme_ctrlr_depopulate_namespace(nvme_ctrlr, nvme_ns);
5169 : 60 : }
5170 : 1768 : }
5171 : :
5172 : : static uint32_t
5173 : 3169 : nvme_ctrlr_get_ana_log_page_size(struct nvme_ctrlr *nvme_ctrlr)
5174 : : {
5175 [ # # # # ]: 3169 : struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
5176 : : const struct spdk_nvme_ctrlr_data *cdata;
5177 : 3169 : uint32_t nsid, ns_count = 0;
5178 : :
5179 : 3169 : cdata = spdk_nvme_ctrlr_get_data(ctrlr);
5180 : :
5181 [ + + ]: 3214 : for (nsid = spdk_nvme_ctrlr_get_first_active_ns(ctrlr);
5182 [ + + ]: 6370 : nsid != 0; nsid = spdk_nvme_ctrlr_get_next_active_ns(ctrlr, nsid)) {
5183 : 3201 : ns_count++;
5184 : 45 : }
5185 : :
5186 [ # # # # ]: 3206 : return sizeof(struct spdk_nvme_ana_page) + cdata->nanagrpid *
5187 : 3169 : sizeof(struct spdk_nvme_ana_group_descriptor) + ns_count *
5188 : : sizeof(uint32_t);
5189 : : }
5190 : :
5191 : : static int
5192 : 133 : nvme_ctrlr_set_ana_states(const struct spdk_nvme_ana_group_descriptor *desc,
5193 : : void *cb_arg)
5194 : : {
5195 : 133 : struct nvme_ctrlr *nvme_ctrlr = cb_arg;
5196 : : struct nvme_ns *nvme_ns;
5197 : : uint32_t i, nsid;
5198 : :
5199 [ + + # # : 262 : for (i = 0; i < desc->num_of_nsid; i++) {
# # ]
5200 [ # # # # : 129 : nsid = desc->nsid[i];
# # ]
5201 [ + + ]: 129 : if (nsid == 0) {
5202 : 0 : continue;
5203 : : }
5204 : :
5205 : 129 : nvme_ns = nvme_ctrlr_get_ns(nvme_ctrlr, nsid);
5206 : :
5207 [ + + ]: 129 : if (nvme_ns == NULL) {
5208 : : /* Target told us that an inactive namespace had an ANA change */
5209 : 4 : continue;
5210 : : }
5211 : :
5212 : 125 : _nvme_ns_set_ana_state(nvme_ns, desc);
5213 : 5 : }
5214 : :
5215 : 133 : return 0;
5216 : : }
5217 : :
5218 : : static void
5219 : 0 : bdev_nvme_disable_read_ana_log_page(struct nvme_ctrlr *nvme_ctrlr)
5220 : : {
5221 : : struct nvme_ns *nvme_ns;
5222 : :
5223 [ # # # # ]: 0 : spdk_free(nvme_ctrlr->ana_log_page);
5224 [ # # # # ]: 0 : nvme_ctrlr->ana_log_page = NULL;
5225 : :
5226 [ # # ]: 0 : for (nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
5227 [ # # ]: 0 : nvme_ns != NULL;
5228 : 0 : nvme_ns = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns)) {
5229 [ # # # # ]: 0 : nvme_ns->ana_state_updating = false;
5230 [ # # # # ]: 0 : nvme_ns->ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
5231 : 0 : }
5232 : 0 : }
5233 : :
5234 : : static void
5235 : 117 : nvme_ctrlr_read_ana_log_page_done(void *ctx, const struct spdk_nvme_cpl *cpl)
5236 : : {
5237 : 117 : struct nvme_ctrlr *nvme_ctrlr = ctx;
5238 : :
5239 [ + - + - : 117 : if (cpl != NULL && spdk_nvme_cpl_is_success(cpl)) {
+ + # # #
# # # # #
# # # # ]
5240 : 120 : bdev_nvme_parse_ana_log_page(nvme_ctrlr, nvme_ctrlr_set_ana_states,
5241 : 3 : nvme_ctrlr);
5242 : 3 : } else {
5243 : 0 : bdev_nvme_disable_read_ana_log_page(nvme_ctrlr);
5244 : : }
5245 : :
5246 [ - + # # ]: 117 : pthread_mutex_lock(&nvme_ctrlr->mutex);
5247 : :
5248 [ + + # # : 117 : assert(nvme_ctrlr->ana_log_page_updating == true);
# # ]
5249 [ # # ]: 117 : nvme_ctrlr->ana_log_page_updating = false;
5250 : :
5251 [ - + ]: 117 : if (nvme_ctrlr_can_be_unregistered(nvme_ctrlr)) {
5252 [ # # # # ]: 0 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
5253 : :
5254 : 0 : nvme_ctrlr_unregister(nvme_ctrlr);
5255 : 0 : } else {
5256 [ - + # # ]: 117 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
5257 : :
5258 : 117 : bdev_nvme_clear_io_path_caches(nvme_ctrlr);
5259 : : }
5260 : 117 : }
5261 : :
5262 : : static int
5263 : 2721 : nvme_ctrlr_read_ana_log_page(struct nvme_ctrlr *nvme_ctrlr)
5264 : : {
5265 : : uint32_t ana_log_page_size;
5266 : : int rc;
5267 : :
5268 [ + + # # : 2721 : if (nvme_ctrlr->ana_log_page == NULL) {
# # ]
5269 : 0 : return -EINVAL;
5270 : : }
5271 : :
5272 : 2721 : ana_log_page_size = nvme_ctrlr_get_ana_log_page_size(nvme_ctrlr);
5273 : :
5274 [ - + # # : 2721 : if (ana_log_page_size > nvme_ctrlr->max_ana_log_page_size) {
# # ]
5275 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
5276 : : "ANA log page size %" PRIu32 " is larger than allowed %" PRIu32 "\n",
5277 : : ana_log_page_size, nvme_ctrlr->max_ana_log_page_size);
5278 : 0 : return -EINVAL;
5279 : : }
5280 : :
5281 [ - + # # ]: 2721 : pthread_mutex_lock(&nvme_ctrlr->mutex);
5282 [ + + + + ]: 2721 : if (!nvme_ctrlr_is_available(nvme_ctrlr) ||
5283 [ # # ]: 5 : nvme_ctrlr->ana_log_page_updating) {
5284 [ - + # # ]: 2604 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
5285 : 2604 : return -EBUSY;
5286 : : }
5287 : :
5288 [ # # ]: 117 : nvme_ctrlr->ana_log_page_updating = true;
5289 [ - + # # ]: 117 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
5290 : :
5291 [ # # # # ]: 120 : rc = spdk_nvme_ctrlr_cmd_get_log_page(nvme_ctrlr->ctrlr,
5292 : : SPDK_NVME_LOG_ASYMMETRIC_NAMESPACE_ACCESS,
5293 : : SPDK_NVME_GLOBAL_NS_TAG,
5294 [ # # # # ]: 117 : nvme_ctrlr->ana_log_page,
5295 : 3 : ana_log_page_size, 0,
5296 : : nvme_ctrlr_read_ana_log_page_done,
5297 : 3 : nvme_ctrlr);
5298 [ - + ]: 117 : if (rc != 0) {
5299 : 0 : nvme_ctrlr_read_ana_log_page_done(nvme_ctrlr, NULL);
5300 : 0 : }
5301 : :
5302 : 117 : return rc;
5303 : 6 : }
5304 : :
5305 : : static void
5306 : 0 : dummy_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *ctx)
5307 : : {
5308 : 0 : }
5309 : :
5310 : : struct bdev_nvme_set_preferred_path_ctx {
5311 : : struct spdk_bdev_desc *desc;
5312 : : struct nvme_ns *nvme_ns;
5313 : : bdev_nvme_set_preferred_path_cb cb_fn;
5314 : : void *cb_arg;
5315 : : };
5316 : :
5317 : : static void
5318 : 12 : bdev_nvme_set_preferred_path_done(struct nvme_bdev *nbdev, void *_ctx, int status)
5319 : : {
5320 : 12 : struct bdev_nvme_set_preferred_path_ctx *ctx = _ctx;
5321 : :
5322 [ + + # # ]: 12 : assert(ctx != NULL);
5323 [ + + # # : 12 : assert(ctx->desc != NULL);
# # # # ]
5324 [ + + # # : 12 : assert(ctx->cb_fn != NULL);
# # # # ]
5325 : :
5326 [ # # # # ]: 12 : spdk_bdev_close(ctx->desc);
5327 : :
5328 [ # # # # : 12 : ctx->cb_fn(ctx->cb_arg, status);
# # # # #
# # # ]
5329 : :
5330 : 12 : free(ctx);
5331 : 12 : }
5332 : :
5333 : : static void
5334 : 8 : _bdev_nvme_set_preferred_path(struct nvme_bdev_channel_iter *i,
5335 : : struct nvme_bdev *nbdev,
5336 : : struct nvme_bdev_channel *nbdev_ch, void *_ctx)
5337 : : {
5338 : 8 : struct bdev_nvme_set_preferred_path_ctx *ctx = _ctx;
5339 : : struct nvme_io_path *io_path, *prev;
5340 : :
5341 : 8 : prev = NULL;
5342 [ + + # # : 12 : STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
# # # # #
# # # #
# ]
5343 [ + + # # : 12 : if (io_path->nvme_ns == ctx->nvme_ns) {
# # # # #
# ]
5344 : 8 : break;
5345 : : }
5346 : 4 : prev = io_path;
5347 : 1 : }
5348 : :
5349 [ + - ]: 8 : if (io_path != NULL) {
5350 [ + + ]: 8 : if (prev != NULL) {
5351 [ - + # # : 4 : STAILQ_REMOVE_AFTER(&nbdev_ch->io_path_list, prev, stailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
5352 [ - + # # : 4 : STAILQ_INSERT_HEAD(&nbdev_ch->io_path_list, io_path, stailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
5353 : 1 : }
5354 : :
5355 : : /* We can set io_path to nbdev_ch->current_io_path directly here.
5356 : : * However, it needs to be conditional. To simplify the code,
5357 : : * just clear nbdev_ch->current_io_path and let find_io_path()
5358 : : * fill it.
5359 : : *
5360 : : * Automatic failback may be disabled. Hence even if the io_path is
5361 : : * already at the head, clear nbdev_ch->current_io_path.
5362 : : */
5363 : 8 : bdev_nvme_clear_current_io_path(nbdev_ch);
5364 : 2 : }
5365 : :
5366 : 8 : nvme_bdev_for_each_channel_continue(i, 0);
5367 : 8 : }
5368 : :
5369 : : static struct nvme_ns *
5370 : 12 : bdev_nvme_set_preferred_ns(struct nvme_bdev *nbdev, uint16_t cntlid)
5371 : : {
5372 : : struct nvme_ns *nvme_ns, *prev;
5373 : : const struct spdk_nvme_ctrlr_data *cdata;
5374 : :
5375 : 12 : prev = NULL;
5376 [ + + # # : 24 : TAILQ_FOREACH(nvme_ns, &nbdev->nvme_ns_list, tailq) {
# # # # #
# # # #
# ]
5377 [ # # # # : 24 : cdata = spdk_nvme_ctrlr_get_data(nvme_ns->ctrlr->ctrlr);
# # # # ]
5378 : :
5379 [ + + # # : 24 : if (cdata->cntlid == cntlid) {
# # ]
5380 : 12 : break;
5381 : : }
5382 : 12 : prev = nvme_ns;
5383 : 3 : }
5384 : :
5385 [ + - + + ]: 12 : if (nvme_ns != NULL && prev != NULL) {
5386 [ + + # # : 8 : TAILQ_REMOVE(&nbdev->nvme_ns_list, nvme_ns, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
5387 [ + + # # : 8 : TAILQ_INSERT_HEAD(&nbdev->nvme_ns_list, nvme_ns, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
5388 : 2 : }
5389 : :
5390 : 12 : return nvme_ns;
5391 : : }
5392 : :
5393 : : /* This function supports only multipath mode. There is only a single I/O path
5394 : : * for each NVMe-oF controller. Hence, just move the matched I/O path to the
5395 : : * head of the I/O path list for each NVMe bdev channel.
5396 : : *
5397 : : * NVMe bdev channel may be acquired after completing this function. move the
5398 : : * matched namespace to the head of the namespace list for the NVMe bdev too.
5399 : : */
5400 : : void
5401 : 12 : bdev_nvme_set_preferred_path(const char *name, uint16_t cntlid,
5402 : : bdev_nvme_set_preferred_path_cb cb_fn, void *cb_arg)
5403 : : {
5404 : : struct bdev_nvme_set_preferred_path_ctx *ctx;
5405 : : struct spdk_bdev *bdev;
5406 : : struct nvme_bdev *nbdev;
5407 : 12 : int rc = 0;
5408 : :
5409 [ + + # # ]: 12 : assert(cb_fn != NULL);
5410 : :
5411 : 12 : ctx = calloc(1, sizeof(*ctx));
5412 [ + + ]: 12 : if (ctx == NULL) {
5413 : 0 : SPDK_ERRLOG("Failed to alloc context.\n");
5414 : 0 : rc = -ENOMEM;
5415 : 0 : goto err_alloc;
5416 : : }
5417 : :
5418 [ # # # # ]: 12 : ctx->cb_fn = cb_fn;
5419 [ # # # # ]: 12 : ctx->cb_arg = cb_arg;
5420 : :
5421 [ # # ]: 12 : rc = spdk_bdev_open_ext(name, false, dummy_bdev_event_cb, NULL, &ctx->desc);
5422 [ - + ]: 12 : if (rc != 0) {
5423 : 0 : SPDK_ERRLOG("Failed to open bdev %s.\n", name);
5424 : 0 : goto err_open;
5425 : : }
5426 : :
5427 [ # # # # ]: 12 : bdev = spdk_bdev_desc_get_bdev(ctx->desc);
5428 : :
5429 [ - + # # : 12 : if (bdev->module != &nvme_if) {
# # ]
5430 : 0 : SPDK_ERRLOG("bdev %s is not registered in this module.\n", name);
5431 : 0 : rc = -ENODEV;
5432 : 0 : goto err_bdev;
5433 : : }
5434 : :
5435 : 12 : nbdev = SPDK_CONTAINEROF(bdev, struct nvme_bdev, disk);
5436 : :
5437 [ - + # # ]: 12 : pthread_mutex_lock(&nbdev->mutex);
5438 : :
5439 [ # # # # ]: 12 : ctx->nvme_ns = bdev_nvme_set_preferred_ns(nbdev, cntlid);
5440 [ + + # # : 12 : if (ctx->nvme_ns == NULL) {
# # ]
5441 [ # # # # ]: 0 : pthread_mutex_unlock(&nbdev->mutex);
5442 : :
5443 : 0 : SPDK_ERRLOG("bdev %s does not have namespace to controller %u.\n", name, cntlid);
5444 : 0 : rc = -ENODEV;
5445 : 0 : goto err_bdev;
5446 : : }
5447 : :
5448 [ - + # # ]: 12 : pthread_mutex_unlock(&nbdev->mutex);
5449 : :
5450 : 15 : nvme_bdev_for_each_channel(nbdev,
5451 : : _bdev_nvme_set_preferred_path,
5452 : 3 : ctx,
5453 : : bdev_nvme_set_preferred_path_done);
5454 : 12 : return;
5455 : :
5456 : 0 : err_bdev:
5457 [ # # # # ]: 0 : spdk_bdev_close(ctx->desc);
5458 : 0 : err_open:
5459 : 0 : free(ctx);
5460 : 0 : err_alloc:
5461 [ # # # # ]: 0 : cb_fn(cb_arg, rc);
5462 : 3 : }
5463 : :
5464 : : struct bdev_nvme_set_multipath_policy_ctx {
5465 : : struct spdk_bdev_desc *desc;
5466 : : spdk_bdev_nvme_set_multipath_policy_cb cb_fn;
5467 : : void *cb_arg;
5468 : : };
5469 : :
5470 : : static void
5471 : 16 : bdev_nvme_set_multipath_policy_done(struct nvme_bdev *nbdev, void *_ctx, int status)
5472 : : {
5473 : 16 : struct bdev_nvme_set_multipath_policy_ctx *ctx = _ctx;
5474 : :
5475 [ + + # # ]: 16 : assert(ctx != NULL);
5476 [ + + # # : 16 : assert(ctx->desc != NULL);
# # # # ]
5477 [ + + # # : 16 : assert(ctx->cb_fn != NULL);
# # # # ]
5478 : :
5479 [ # # # # ]: 16 : spdk_bdev_close(ctx->desc);
5480 : :
5481 [ # # # # : 16 : ctx->cb_fn(ctx->cb_arg, status);
# # # # #
# # # ]
5482 : :
5483 : 16 : free(ctx);
5484 : 16 : }
5485 : :
5486 : : static void
5487 : 8 : _bdev_nvme_set_multipath_policy(struct nvme_bdev_channel_iter *i,
5488 : : struct nvme_bdev *nbdev,
5489 : : struct nvme_bdev_channel *nbdev_ch, void *ctx)
5490 : : {
5491 [ # # # # : 8 : nbdev_ch->mp_policy = nbdev->mp_policy;
# # # # ]
5492 [ # # # # : 8 : nbdev_ch->mp_selector = nbdev->mp_selector;
# # # # ]
5493 [ # # # # : 8 : nbdev_ch->rr_min_io = nbdev->rr_min_io;
# # # # ]
5494 : 8 : bdev_nvme_clear_current_io_path(nbdev_ch);
5495 : :
5496 : 8 : nvme_bdev_for_each_channel_continue(i, 0);
5497 : 8 : }
5498 : :
5499 : : void
5500 : 16 : spdk_bdev_nvme_set_multipath_policy(const char *name, enum spdk_bdev_nvme_multipath_policy policy,
5501 : : enum spdk_bdev_nvme_multipath_selector selector, uint32_t rr_min_io,
5502 : : spdk_bdev_nvme_set_multipath_policy_cb cb_fn, void *cb_arg)
5503 : : {
5504 : : struct bdev_nvme_set_multipath_policy_ctx *ctx;
5505 : : struct spdk_bdev *bdev;
5506 : : struct nvme_bdev *nbdev;
5507 : : int rc;
5508 : :
5509 [ + + # # ]: 16 : assert(cb_fn != NULL);
5510 : :
5511 [ + + + ]: 16 : switch (policy) {
5512 : 3 : case BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE:
5513 : 4 : break;
5514 [ + + - ]: 10 : case BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE:
5515 [ + + + ]: 6 : switch (selector) {
5516 : 7 : case BDEV_NVME_MP_SELECTOR_ROUND_ROBIN:
5517 [ + + ]: 8 : if (rr_min_io == UINT32_MAX) {
5518 : 4 : rr_min_io = 1;
5519 [ - + ]: 4 : } else if (rr_min_io == 0) {
5520 : 0 : rc = -EINVAL;
5521 : 0 : goto exit;
5522 : : }
5523 : 8 : break;
5524 : 3 : case BDEV_NVME_MP_SELECTOR_QUEUE_DEPTH:
5525 : 4 : break;
5526 : 0 : default:
5527 : 0 : rc = -EINVAL;
5528 : 0 : goto exit;
5529 : : }
5530 : 12 : break;
5531 : 0 : default:
5532 : 0 : rc = -EINVAL;
5533 : 0 : goto exit;
5534 : : }
5535 : :
5536 : 16 : ctx = calloc(1, sizeof(*ctx));
5537 [ + + ]: 16 : if (ctx == NULL) {
5538 : 0 : SPDK_ERRLOG("Failed to alloc context.\n");
5539 : 0 : rc = -ENOMEM;
5540 : 0 : goto exit;
5541 : : }
5542 : :
5543 [ # # # # ]: 16 : ctx->cb_fn = cb_fn;
5544 [ # # # # ]: 16 : ctx->cb_arg = cb_arg;
5545 : :
5546 [ # # ]: 16 : rc = spdk_bdev_open_ext(name, false, dummy_bdev_event_cb, NULL, &ctx->desc);
5547 [ - + ]: 16 : if (rc != 0) {
5548 : 0 : SPDK_ERRLOG("Failed to open bdev %s.\n", name);
5549 : 0 : rc = -ENODEV;
5550 : 0 : goto err_open;
5551 : : }
5552 : :
5553 [ # # # # ]: 16 : bdev = spdk_bdev_desc_get_bdev(ctx->desc);
5554 [ - + # # : 16 : if (bdev->module != &nvme_if) {
# # ]
5555 : 0 : SPDK_ERRLOG("bdev %s is not registered in this module.\n", name);
5556 : 0 : rc = -ENODEV;
5557 : 0 : goto err_module;
5558 : : }
5559 : 16 : nbdev = SPDK_CONTAINEROF(bdev, struct nvme_bdev, disk);
5560 : :
5561 [ - + # # ]: 16 : pthread_mutex_lock(&nbdev->mutex);
5562 [ # # # # ]: 16 : nbdev->mp_policy = policy;
5563 [ # # # # ]: 16 : nbdev->mp_selector = selector;
5564 [ # # # # ]: 16 : nbdev->rr_min_io = rr_min_io;
5565 [ - + # # ]: 16 : pthread_mutex_unlock(&nbdev->mutex);
5566 : :
5567 : 19 : nvme_bdev_for_each_channel(nbdev,
5568 : : _bdev_nvme_set_multipath_policy,
5569 : 3 : ctx,
5570 : : bdev_nvme_set_multipath_policy_done);
5571 : 16 : return;
5572 : :
5573 : 0 : err_module:
5574 [ # # # # ]: 0 : spdk_bdev_close(ctx->desc);
5575 : 0 : err_open:
5576 : 0 : free(ctx);
5577 : 0 : exit:
5578 [ # # # # ]: 0 : cb_fn(cb_arg, rc);
5579 : 3 : }
5580 : :
5581 : : static void
5582 : 138 : aer_cb(void *arg, const struct spdk_nvme_cpl *cpl)
5583 : : {
5584 : 138 : struct nvme_ctrlr *nvme_ctrlr = arg;
5585 : : union spdk_nvme_async_event_completion event;
5586 : :
5587 [ + - - + : 138 : if (spdk_nvme_cpl_is_error(cpl)) {
# # # # #
# # # # #
# # ]
5588 : 0 : SPDK_WARNLOG("AER request execute failed\n");
5589 : 0 : return;
5590 : : }
5591 : :
5592 [ # # # # ]: 138 : event.raw = cpl->cdw0;
5593 [ + - + + ]: 138 : if ((event.bits.async_event_type == SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) &&
5594 [ + + ]: 138 : (event.bits.async_event_info == SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGED)) {
5595 : 34 : nvme_ctrlr_populate_namespaces(nvme_ctrlr, NULL);
5596 [ + - - + ]: 106 : } else if ((event.bits.async_event_type == SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) &&
5597 [ + - ]: 104 : (event.bits.async_event_info == SPDK_NVME_ASYNC_EVENT_ANA_CHANGE)) {
5598 : 104 : nvme_ctrlr_read_ana_log_page(nvme_ctrlr);
5599 : 1 : }
5600 : 3 : }
5601 : :
5602 : : static void
5603 : 1756 : free_nvme_async_probe_ctx(struct nvme_async_probe_ctx *ctx)
5604 : : {
5605 [ + - + - : 1756 : spdk_keyring_put_key(ctx->drv_opts.tls_psk);
+ - ]
5606 [ + - + - : 1756 : spdk_keyring_put_key(ctx->drv_opts.dhchap_key);
+ - ]
5607 [ + - + - : 1756 : spdk_keyring_put_key(ctx->drv_opts.dhchap_ctrlr_key);
+ - ]
5608 [ + - + - ]: 1756 : free(ctx->base_name);
5609 : 1756 : free(ctx);
5610 : 1756 : }
5611 : :
5612 : : static void
5613 : 1747 : populate_namespaces_cb(struct nvme_async_probe_ctx *ctx, int rc)
5614 : : {
5615 [ + + + - : 1747 : if (ctx->cb_fn) {
+ - ]
5616 [ + - + - : 1747 : ctx->cb_fn(ctx->cb_ctx, ctx->reported_bdevs, rc);
- + + - +
- + - + -
+ - ]
5617 : 64 : }
5618 : :
5619 [ + - + - ]: 1747 : ctx->namespaces_populated = true;
5620 [ + + + + : 1747 : if (ctx->probe_done) {
+ - + - ]
5621 : : /* The probe was already completed, so we need to free the context
5622 : : * here. This can happen for cases like OCSSD, where we need to
5623 : : * send additional commands to the SSD after attach.
5624 : : */
5625 : 514 : free_nvme_async_probe_ctx(ctx);
5626 : 32 : }
5627 : 1747 : }
5628 : :
5629 : : static int
5630 : 37254 : bdev_nvme_remove_poller(void *ctx)
5631 : : {
5632 : 15538 : struct spdk_nvme_transport_id trid_pcie;
5633 : :
5634 [ + + ]: 37254 : if (TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) {
5635 : 869 : spdk_poller_unregister(&g_hotplug_poller);
5636 : 869 : return SPDK_POLLER_IDLE;
5637 : : }
5638 : :
5639 [ + + ]: 36385 : memset(&trid_pcie, 0, sizeof(trid_pcie));
5640 : 36385 : spdk_nvme_trid_populate_transport(&trid_pcie, SPDK_NVME_TRANSPORT_PCIE);
5641 : :
5642 [ + + ]: 36385 : if (spdk_nvme_scan_attached(&trid_pcie)) {
5643 [ # # # # ]: 0 : SPDK_ERRLOG_RATELIMIT("spdk_nvme_scan_attached() failed\n");
5644 : 0 : }
5645 : :
5646 : 36385 : return SPDK_POLLER_BUSY;
5647 : 257 : }
5648 : :
5649 : : static void
5650 : 1764 : nvme_ctrlr_create_done(struct nvme_ctrlr *nvme_ctrlr,
5651 : : struct nvme_async_probe_ctx *ctx)
5652 : : {
5653 [ + - + - : 1764 : struct spdk_nvme_transport_id *trid = &nvme_ctrlr->active_path_id->trid;
+ - ]
5654 : :
5655 [ + + + - : 1764 : if (spdk_nvme_trtype_is_fabrics(trid->trtype)) {
- + ]
5656 [ + + + + : 1216 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "ctrlr was created to %s:%s\n",
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
5657 : : trid->traddr, trid->trsvcid);
5658 : 61 : } else {
5659 [ + + + + : 548 : NVME_CTRLR_INFOLOG(nvme_ctrlr, "ctrlr was created\n");
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
5660 : : }
5661 : :
5662 : 1836 : spdk_io_device_register(nvme_ctrlr,
5663 : : bdev_nvme_create_ctrlr_channel_cb,
5664 : : bdev_nvme_destroy_ctrlr_channel_cb,
5665 : : sizeof(struct nvme_ctrlr_channel),
5666 [ + - + - : 1764 : nvme_ctrlr->nbdev_ctrlr->name);
+ - + - ]
5667 : :
5668 : 1764 : nvme_ctrlr_populate_namespaces(nvme_ctrlr, ctx);
5669 : :
5670 [ + + ]: 1764 : if (g_hotplug_poller == NULL) {
5671 : 1262 : g_hotplug_poller = SPDK_POLLER_REGISTER(bdev_nvme_remove_poller, NULL,
5672 : : NVME_HOTPLUG_POLL_PERIOD_DEFAULT);
5673 : 13 : }
5674 : 1764 : }
5675 : :
5676 : : static void
5677 : 448 : nvme_ctrlr_init_ana_log_page_done(void *_ctx, const struct spdk_nvme_cpl *cpl)
5678 : : {
5679 : 448 : struct nvme_ctrlr *nvme_ctrlr = _ctx;
5680 [ # # # # ]: 448 : struct nvme_async_probe_ctx *ctx = nvme_ctrlr->probe_ctx;
5681 : :
5682 [ # # # # ]: 448 : nvme_ctrlr->probe_ctx = NULL;
5683 : :
5684 [ + - + + : 448 : if (spdk_nvme_cpl_is_error(cpl)) {
# # # # #
# # # # #
# # ]
5685 : 0 : nvme_ctrlr_delete(nvme_ctrlr);
5686 : :
5687 [ # # ]: 0 : if (ctx != NULL) {
5688 [ # # # # ]: 0 : ctx->reported_bdevs = 0;
5689 : 0 : populate_namespaces_cb(ctx, -1);
5690 : 0 : }
5691 : 0 : return;
5692 : : }
5693 : :
5694 : 448 : nvme_ctrlr_create_done(nvme_ctrlr, ctx);
5695 : 31 : }
5696 : :
5697 : : static int
5698 : 448 : nvme_ctrlr_init_ana_log_page(struct nvme_ctrlr *nvme_ctrlr,
5699 : : struct nvme_async_probe_ctx *ctx)
5700 : : {
5701 [ # # # # ]: 448 : struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
5702 : : const struct spdk_nvme_ctrlr_data *cdata;
5703 : : uint32_t ana_log_page_size;
5704 : :
5705 : 448 : cdata = spdk_nvme_ctrlr_get_data(ctrlr);
5706 : :
5707 : : /* Set buffer size enough to include maximum number of allowed namespaces. */
5708 [ # # # # ]: 479 : ana_log_page_size = sizeof(struct spdk_nvme_ana_page) + cdata->nanagrpid *
5709 [ # # # # ]: 448 : sizeof(struct spdk_nvme_ana_group_descriptor) + cdata->mnan *
5710 : : sizeof(uint32_t);
5711 : :
5712 [ # # # # ]: 448 : nvme_ctrlr->ana_log_page = spdk_zmalloc(ana_log_page_size, 64, NULL,
5713 : : SPDK_ENV_NUMA_ID_ANY, SPDK_MALLOC_DMA);
5714 [ + + # # : 448 : if (nvme_ctrlr->ana_log_page == NULL) {
# # ]
5715 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr, "could not allocate ANA log page buffer\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
5716 : 0 : return -ENXIO;
5717 : : }
5718 : :
5719 : : /* Each descriptor in a ANA log page is not ensured to be 8-bytes aligned.
5720 : : * Hence copy each descriptor to a temporary area when parsing it.
5721 : : *
5722 : : * Allocate a buffer whose size is as large as ANA log page buffer because
5723 : : * we do not know the size of a descriptor until actually reading it.
5724 : : */
5725 [ # # # # ]: 448 : nvme_ctrlr->copied_ana_desc = calloc(1, ana_log_page_size);
5726 [ + + # # : 448 : if (nvme_ctrlr->copied_ana_desc == NULL) {
# # ]
5727 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr, "could not allocate a buffer to parse ANA descriptor\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
5728 : 0 : return -ENOMEM;
5729 : : }
5730 : :
5731 [ # # # # ]: 448 : nvme_ctrlr->max_ana_log_page_size = ana_log_page_size;
5732 : :
5733 [ # # # # ]: 448 : nvme_ctrlr->probe_ctx = ctx;
5734 : :
5735 : : /* Then, set the read size only to include the current active namespaces. */
5736 : 448 : ana_log_page_size = nvme_ctrlr_get_ana_log_page_size(nvme_ctrlr);
5737 : :
5738 [ - + # # : 448 : if (ana_log_page_size > nvme_ctrlr->max_ana_log_page_size) {
# # ]
5739 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr, "ANA log page size %" PRIu32 " is larger than allowed %" PRIu32 "\n",
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
5740 : : ana_log_page_size, nvme_ctrlr->max_ana_log_page_size);
5741 : 0 : return -EINVAL;
5742 : : }
5743 : :
5744 : 479 : return spdk_nvme_ctrlr_cmd_get_log_page(ctrlr,
5745 : : SPDK_NVME_LOG_ASYMMETRIC_NAMESPACE_ACCESS,
5746 : : SPDK_NVME_GLOBAL_NS_TAG,
5747 [ # # # # ]: 448 : nvme_ctrlr->ana_log_page,
5748 : 31 : ana_log_page_size, 0,
5749 : : nvme_ctrlr_init_ana_log_page_done,
5750 : 31 : nvme_ctrlr);
5751 : 31 : }
5752 : :
5753 : : /* hostnqn and subnqn were already verified before attaching a controller.
5754 : : * Hence check only the multipath capability and cntlid here.
5755 : : */
5756 : : static bool
5757 : 77 : bdev_nvme_check_multipath(struct nvme_bdev_ctrlr *nbdev_ctrlr, struct spdk_nvme_ctrlr *ctrlr)
5758 : : {
5759 : : struct nvme_ctrlr *tmp;
5760 : : const struct spdk_nvme_ctrlr_data *cdata, *tmp_cdata;
5761 : :
5762 : 77 : cdata = spdk_nvme_ctrlr_get_data(ctrlr);
5763 : :
5764 [ - + # # : 77 : if (!cdata->cmic.multi_ctrlr) {
# # ]
5765 [ # # # # ]: 0 : SPDK_ERRLOG("Ctrlr%u does not support multipath.\n", cdata->cntlid);
5766 : 0 : return false;
5767 : : }
5768 : :
5769 [ + + # # : 158 : TAILQ_FOREACH(tmp, &nbdev_ctrlr->ctrlrs, tailq) {
# # # # #
# # # #
# ]
5770 [ # # # # ]: 85 : tmp_cdata = spdk_nvme_ctrlr_get_data(tmp->ctrlr);
5771 : :
5772 [ + + # # : 85 : if (!tmp_cdata->cmic.multi_ctrlr) {
# # ]
5773 [ # # # # : 0 : NVME_CTRLR_ERRLOG(tmp, "Ctrlr%u does not support multipath.\n", cdata->cntlid);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
5774 : 0 : return false;
5775 : : }
5776 [ + + # # : 85 : if (cdata->cntlid == tmp_cdata->cntlid) {
# # # # #
# ]
5777 [ + - # # : 4 : NVME_CTRLR_ERRLOG(tmp, "cntlid %u are duplicated.\n", tmp_cdata->cntlid);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
5778 : 4 : return false;
5779 : : }
5780 : 17 : }
5781 : :
5782 : 73 : return true;
5783 : 16 : }
5784 : :
5785 : :
5786 : : static int
5787 : 1768 : nvme_bdev_ctrlr_create(const char *name, struct nvme_ctrlr *nvme_ctrlr)
5788 : : {
5789 : : struct nvme_bdev_ctrlr *nbdev_ctrlr;
5790 [ + - + - ]: 1768 : struct spdk_nvme_ctrlr *ctrlr = nvme_ctrlr->ctrlr;
5791 : : struct nvme_ctrlr *nctrlr;
5792 : 1768 : int rc = 0;
5793 : :
5794 [ + + ]: 1768 : pthread_mutex_lock(&g_bdev_nvme_mutex);
5795 : :
5796 : 1768 : nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
5797 [ + + ]: 1768 : if (nbdev_ctrlr != NULL) {
5798 [ + + ]: 77 : if (!bdev_nvme_check_multipath(nbdev_ctrlr, ctrlr)) {
5799 : 4 : rc = -EINVAL;
5800 : 4 : goto exit;
5801 : : }
5802 [ + + # # : 154 : TAILQ_FOREACH(nctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
# # # # #
# # # #
# ]
5803 [ + + - + : 81 : if (nctrlr->opts.multipath != nvme_ctrlr->opts.multipath) {
- + # # #
# # # # #
# # # # ]
5804 : : /* All controllers with the same name must be configured the same
5805 : : * way, either for multipath or failover. If the configuration doesn't
5806 : : * match - report error.
5807 : : */
5808 : 0 : rc = -EINVAL;
5809 : 0 : goto exit;
5810 : : }
5811 : 17 : }
5812 : 15 : } else {
5813 : 1691 : nbdev_ctrlr = calloc(1, sizeof(*nbdev_ctrlr));
5814 [ + + ]: 1691 : if (nbdev_ctrlr == NULL) {
5815 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to allocate nvme_bdev_ctrlr.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
5816 : 0 : rc = -ENOMEM;
5817 : 0 : goto exit;
5818 : : }
5819 [ + + + - : 1691 : nbdev_ctrlr->name = strdup(name);
+ - ]
5820 [ + + + - : 1691 : if (nbdev_ctrlr->name == NULL) {
+ - ]
5821 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr, "Failed to allocate name of nvme_bdev_ctrlr.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
5822 : 0 : free(nbdev_ctrlr);
5823 : 0 : goto exit;
5824 : : }
5825 [ + - + - : 1691 : TAILQ_INIT(&nbdev_ctrlr->ctrlrs);
+ - + - +
- + - + -
+ - ]
5826 [ + - + - : 1691 : TAILQ_INIT(&nbdev_ctrlr->bdevs);
+ - + - +
- + - + -
+ - ]
5827 [ + - + - : 1691 : TAILQ_INSERT_TAIL(&g_nvme_bdev_ctrlrs, nbdev_ctrlr, tailq);
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
5828 : : }
5829 [ + - + - ]: 1764 : nvme_ctrlr->nbdev_ctrlr = nbdev_ctrlr;
5830 [ + - + - : 1764 : TAILQ_INSERT_TAIL(&nbdev_ctrlr->ctrlrs, nvme_ctrlr, tailq);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
5831 : 1695 : exit:
5832 [ + + ]: 1768 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
5833 : 1768 : return rc;
5834 : : }
5835 : :
5836 : : static int
5837 : 1768 : nvme_ctrlr_create(struct spdk_nvme_ctrlr *ctrlr,
5838 : : const char *name,
5839 : : const struct spdk_nvme_transport_id *trid,
5840 : : struct nvme_async_probe_ctx *ctx)
5841 : : {
5842 : : struct nvme_ctrlr *nvme_ctrlr;
5843 : : struct nvme_path_id *path_id;
5844 : : const struct spdk_nvme_ctrlr_data *cdata;
5845 : 1768 : struct spdk_event_handler_opts opts = {
5846 : : .opts_size = SPDK_SIZEOF(&opts, fd_type),
5847 : : };
5848 : : uint64_t period;
5849 : : int fd, rc;
5850 : :
5851 : 1768 : nvme_ctrlr = calloc(1, sizeof(*nvme_ctrlr));
5852 [ + + ]: 1768 : if (nvme_ctrlr == NULL) {
5853 : 0 : SPDK_ERRLOG("Failed to allocate device struct\n");
5854 : 0 : return -ENOMEM;
5855 : : }
5856 : :
5857 [ + + + - ]: 1768 : rc = pthread_mutex_init(&nvme_ctrlr->mutex, NULL);
5858 [ - + ]: 1768 : if (rc != 0) {
5859 : 0 : free(nvme_ctrlr);
5860 : 0 : return rc;
5861 : : }
5862 : :
5863 [ + - + - : 1768 : TAILQ_INIT(&nvme_ctrlr->trids);
+ - + - +
- + - + -
+ - ]
5864 [ + - + - : 1768 : TAILQ_INIT(&nvme_ctrlr->pending_resets);
+ - + - +
- + - + -
+ - ]
5865 [ + - + - : 1768 : RB_INIT(&nvme_ctrlr->namespaces);
+ - ]
5866 : :
5867 : : /* Get another reference to the key, so the first one can be released from probe_ctx */
5868 [ + + ]: 1768 : if (ctx != NULL) {
5869 [ + + + - : 1656 : if (ctx->drv_opts.tls_psk != NULL) {
+ - + - ]
5870 [ # # # # ]: 42 : nvme_ctrlr->psk = spdk_keyring_get_key(
5871 [ # # # # : 0 : spdk_key_get_name(ctx->drv_opts.tls_psk));
# # ]
5872 [ - + # # : 42 : if (nvme_ctrlr->psk == NULL) {
# # ]
5873 : : /* Could only happen if the key was removed in the meantime */
5874 [ # # # # : 0 : SPDK_ERRLOG("Couldn't get a reference to the key '%s'\n",
# # ]
5875 : : spdk_key_get_name(ctx->drv_opts.tls_psk));
5876 : 0 : rc = -ENOKEY;
5877 : 0 : goto err;
5878 : : }
5879 : 0 : }
5880 : :
5881 [ + + + - : 1656 : if (ctx->drv_opts.dhchap_key != NULL) {
+ - + - ]
5882 [ # # # # ]: 628 : nvme_ctrlr->dhchap_key = spdk_keyring_get_key(
5883 [ # # # # : 0 : spdk_key_get_name(ctx->drv_opts.dhchap_key));
# # ]
5884 [ - + # # : 628 : if (nvme_ctrlr->dhchap_key == NULL) {
# # ]
5885 [ # # # # : 0 : SPDK_ERRLOG("Couldn't get a reference to the key '%s'\n",
# # ]
5886 : : spdk_key_get_name(ctx->drv_opts.dhchap_key));
5887 : 0 : rc = -ENOKEY;
5888 : 0 : goto err;
5889 : : }
5890 : 0 : }
5891 : :
5892 [ + + + - : 1656 : if (ctx->drv_opts.dhchap_ctrlr_key != NULL) {
+ - + - ]
5893 [ # # # # ]: 484 : nvme_ctrlr->dhchap_ctrlr_key =
5894 : 484 : spdk_keyring_get_key(
5895 [ # # # # : 0 : spdk_key_get_name(ctx->drv_opts.dhchap_ctrlr_key));
# # ]
5896 [ - + # # : 484 : if (nvme_ctrlr->dhchap_ctrlr_key == NULL) {
# # ]
5897 [ # # # # : 0 : SPDK_ERRLOG("Couldn't get a reference to the key '%s'\n",
# # ]
5898 : : spdk_key_get_name(ctx->drv_opts.dhchap_ctrlr_key));
5899 : 0 : rc = -ENOKEY;
5900 : 0 : goto err;
5901 : : }
5902 : 0 : }
5903 : 59 : }
5904 : :
5905 : : /* Check if we manage to enable interrupts on the controller. */
5906 [ + + + - : 1768 : if (spdk_interrupt_mode_is_enabled() && ctx != NULL && !ctx->drv_opts.enable_interrupts) {
- + - + #
# # # #
# ]
5907 : 0 : SPDK_ERRLOG("Failed to enable interrupts on the controller\n");
5908 : 0 : rc = -ENOTSUP;
5909 : 0 : goto err;
5910 : : }
5911 : :
5912 : 1768 : path_id = calloc(1, sizeof(*path_id));
5913 [ + + ]: 1768 : if (path_id == NULL) {
5914 : 0 : SPDK_ERRLOG("Failed to allocate trid entry pointer\n");
5915 : 0 : rc = -ENOMEM;
5916 : 0 : goto err;
5917 : : }
5918 : :
5919 [ + - ]: 1768 : path_id->trid = *trid;
5920 [ + + ]: 1768 : if (ctx != NULL) {
5921 [ + + + + : 1656 : memcpy(path_id->hostid.hostaddr, ctx->drv_opts.src_addr, sizeof(path_id->hostid.hostaddr));
+ - + - +
- + - ]
5922 [ + + + + : 1656 : memcpy(path_id->hostid.hostsvcid, ctx->drv_opts.src_svcid, sizeof(path_id->hostid.hostsvcid));
+ - + - +
- + - ]
5923 : 59 : }
5924 [ + - + - ]: 1768 : nvme_ctrlr->active_path_id = path_id;
5925 [ + + + - : 1768 : TAILQ_INSERT_HEAD(&nvme_ctrlr->trids, path_id, link);
+ - + - +
- + - - +
# # # # #
# # # # #
# # # # #
# + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
5926 : :
5927 [ + - + - ]: 1768 : nvme_ctrlr->thread = spdk_get_thread();
5928 [ + - + - ]: 1768 : nvme_ctrlr->ctrlr = ctrlr;
5929 [ + - + - ]: 1768 : nvme_ctrlr->ref = 1;
5930 : :
5931 [ - + ]: 1768 : if (spdk_nvme_ctrlr_is_ocssd_supported(ctrlr)) {
5932 : 0 : SPDK_ERRLOG("OCSSDs are not supported");
5933 : 0 : rc = -ENOTSUP;
5934 : 0 : goto err;
5935 : : }
5936 : :
5937 [ + + ]: 1768 : if (ctx != NULL) {
5938 [ + + + + : 1656 : memcpy(&nvme_ctrlr->opts, &ctx->bdev_opts, sizeof(ctx->bdev_opts));
+ - + - ]
5939 : 59 : } else {
5940 [ # # ]: 112 : spdk_bdev_nvme_get_default_ctrlr_opts(&nvme_ctrlr->opts);
5941 : : }
5942 : :
5943 [ + + + - ]: 1768 : period = spdk_interrupt_mode_is_enabled() ? 0 : g_opts.nvme_adminq_poll_period_us;
5944 : :
5945 [ + - + - ]: 1768 : nvme_ctrlr->adminq_timer_poller = SPDK_POLLER_REGISTER(bdev_nvme_poll_adminq, nvme_ctrlr,
5946 : : period);
5947 : :
5948 [ + + ]: 1768 : if (spdk_interrupt_mode_is_enabled()) {
5949 [ # # # # ]: 1 : spdk_poller_register_interrupt(nvme_ctrlr->adminq_timer_poller, NULL, NULL);
5950 : :
5951 [ # # # # ]: 1 : fd = spdk_nvme_ctrlr_get_admin_qp_fd(nvme_ctrlr->ctrlr, &opts);
5952 [ - + ]: 1 : if (fd < 0) {
5953 : 0 : rc = fd;
5954 : 0 : goto err;
5955 : : }
5956 : :
5957 [ # # # # ]: 1 : nvme_ctrlr->intr = SPDK_INTERRUPT_REGISTER_EXT(fd, bdev_nvme_poll_adminq,
5958 : : nvme_ctrlr, &opts);
5959 [ - + # # : 1 : if (!nvme_ctrlr->intr) {
# # ]
5960 : 0 : rc = -EINVAL;
5961 : 0 : goto err;
5962 : : }
5963 : 0 : }
5964 : :
5965 [ + + + - ]: 1768 : if (g_opts.timeout_us > 0) {
5966 : : /* Register timeout callback. Timeout values for IO vs. admin reqs can be different. */
5967 : : /* If timeout_admin_us is 0 (not specified), admin uses same timeout as IO. */
5968 [ # # # # ]: 0 : uint64_t adm_timeout_us = (g_opts.timeout_admin_us == 0) ?
5969 [ # # # # ]: 0 : g_opts.timeout_us : g_opts.timeout_admin_us;
5970 [ # # ]: 0 : spdk_nvme_ctrlr_register_timeout_callback(ctrlr, g_opts.timeout_us,
5971 : 0 : adm_timeout_us, timeout_cb, nvme_ctrlr);
5972 : 0 : }
5973 : :
5974 : 1768 : spdk_nvme_ctrlr_register_aer_callback(ctrlr, aer_cb, nvme_ctrlr);
5975 : 1768 : spdk_nvme_ctrlr_set_remove_cb(ctrlr, remove_cb, nvme_ctrlr);
5976 : :
5977 [ + + ]: 1768 : if (spdk_nvme_ctrlr_get_flags(ctrlr) &
5978 : : SPDK_NVME_CTRLR_SECURITY_SEND_RECV_SUPPORTED) {
5979 [ # # # # ]: 25 : nvme_ctrlr->opal_dev = spdk_opal_dev_construct(ctrlr);
5980 : 0 : }
5981 : :
5982 : 1768 : rc = nvme_bdev_ctrlr_create(name, nvme_ctrlr);
5983 [ + + ]: 1768 : if (rc != 0) {
5984 : 4 : goto err;
5985 : : }
5986 : :
5987 : 1764 : cdata = spdk_nvme_ctrlr_get_data(ctrlr);
5988 : :
5989 [ + + - + : 1764 : if (cdata->cmic.ana_reporting) {
- + ]
5990 : 448 : rc = nvme_ctrlr_init_ana_log_page(nvme_ctrlr, ctx);
5991 [ + - ]: 448 : if (rc == 0) {
5992 : 448 : return 0;
5993 : : }
5994 : 0 : } else {
5995 : 1316 : nvme_ctrlr_create_done(nvme_ctrlr, ctx);
5996 : 1316 : return 0;
5997 : : }
5998 : :
5999 : 3 : err:
6000 : 4 : nvme_ctrlr_delete(nvme_ctrlr);
6001 : 4 : return rc;
6002 : 73 : }
6003 : :
6004 : : void
6005 : 1707 : spdk_bdev_nvme_get_default_ctrlr_opts(struct spdk_bdev_nvme_ctrlr_opts *opts)
6006 : : {
6007 [ + - + - ]: 1707 : opts->prchk_flags = 0;
6008 [ + - + - : 1707 : opts->ctrlr_loss_timeout_sec = g_opts.ctrlr_loss_timeout_sec;
+ - ]
6009 [ + - + - : 1707 : opts->reconnect_delay_sec = g_opts.reconnect_delay_sec;
+ - ]
6010 [ + - + - : 1707 : opts->fast_io_fail_timeout_sec = g_opts.fast_io_fail_timeout_sec;
+ - ]
6011 [ + - + - ]: 1707 : opts->multipath = true;
6012 : 1707 : }
6013 : :
6014 : : static void
6015 : 56 : attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
6016 : : struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *drv_opts)
6017 : : {
6018 : : char *name;
6019 : :
6020 [ # # ]: 56 : name = spdk_sprintf_alloc("HotInNvme%d", g_hot_insert_nvme_controller_index++);
6021 [ - + ]: 56 : if (!name) {
6022 : 0 : SPDK_ERRLOG("Failed to assign name to NVMe device\n");
6023 : 0 : return;
6024 : : }
6025 : :
6026 [ + - ]: 56 : if (nvme_ctrlr_create(ctrlr, name, trid, NULL) == 0) {
6027 [ - + - + : 56 : SPDK_DEBUGLOG(bdev_nvme, "Attached to %s (%s)\n", trid->traddr, name);
# # # # ]
6028 : 0 : } else {
6029 [ # # ]: 0 : SPDK_ERRLOG("Failed to attach to %s (%s)\n", trid->traddr, name);
6030 : : }
6031 : :
6032 : 56 : free(name);
6033 : 0 : }
6034 : :
6035 : : static void
6036 : 1764 : _nvme_ctrlr_destruct(void *ctx)
6037 : : {
6038 : 1764 : struct nvme_ctrlr *nvme_ctrlr = ctx;
6039 : :
6040 : 1764 : nvme_ctrlr_depopulate_namespaces(nvme_ctrlr);
6041 : 1764 : nvme_ctrlr_put_ref(nvme_ctrlr);
6042 : 1764 : }
6043 : :
6044 : : static int
6045 : 1027 : bdev_nvme_delete_ctrlr_unsafe(struct nvme_ctrlr *nvme_ctrlr, bool hotplug)
6046 : : {
6047 : : struct nvme_probe_skip_entry *entry;
6048 : :
6049 : : /* The controller's destruction was already started */
6050 [ - + # # ]: 1027 : if (nvme_ctrlr->destruct) {
6051 : 0 : return -EALREADY;
6052 : : }
6053 : :
6054 [ + + + + : 1027 : if (!hotplug &&
# # ]
6055 [ + + # # : 979 : nvme_ctrlr->active_path_id->trid.trtype == SPDK_NVME_TRANSPORT_PCIE) {
# # # # #
# ]
6056 : 50 : entry = calloc(1, sizeof(*entry));
6057 [ - + ]: 50 : if (!entry) {
6058 : 0 : return -ENOMEM;
6059 : : }
6060 [ # # # # : 50 : entry->trid = nvme_ctrlr->active_path_id->trid;
# # # # ]
6061 [ # # # # : 50 : TAILQ_INSERT_TAIL(&g_skipped_nvme_ctrlrs, entry, tailq);
# # # # #
# # # # #
# # # # #
# # # #
# ]
6062 : 3 : }
6063 : :
6064 [ # # ]: 1027 : nvme_ctrlr->destruct = true;
6065 : 1027 : return 0;
6066 : 61 : }
6067 : :
6068 : : static int
6069 : 79 : bdev_nvme_delete_ctrlr(struct nvme_ctrlr *nvme_ctrlr, bool hotplug)
6070 : : {
6071 : : int rc;
6072 : :
6073 [ - + # # ]: 79 : pthread_mutex_lock(&nvme_ctrlr->mutex);
6074 [ # # ]: 79 : rc = bdev_nvme_delete_ctrlr_unsafe(nvme_ctrlr, hotplug);
6075 [ - + # # ]: 79 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
6076 : :
6077 [ + + ]: 79 : if (rc == 0) {
6078 : 79 : _nvme_ctrlr_destruct(nvme_ctrlr);
6079 [ # # ]: 2 : } else if (rc == -EALREADY) {
6080 : 0 : rc = 0;
6081 : 0 : }
6082 : :
6083 : 79 : return rc;
6084 : : }
6085 : :
6086 : : static void
6087 : 48 : remove_cb(void *cb_ctx, struct spdk_nvme_ctrlr *ctrlr)
6088 : : {
6089 : 48 : struct nvme_ctrlr *nvme_ctrlr = cb_ctx;
6090 : :
6091 : 48 : bdev_nvme_delete_ctrlr(nvme_ctrlr, true);
6092 : 48 : }
6093 : :
6094 : : static int
6095 : 20065 : bdev_nvme_hotplug_probe(void *arg)
6096 : : {
6097 [ - + ]: 20065 : if (g_hotplug_probe_ctx == NULL) {
6098 : 0 : spdk_poller_unregister(&g_hotplug_probe_poller);
6099 : 0 : return SPDK_POLLER_IDLE;
6100 : : }
6101 : :
6102 [ + + ]: 20065 : if (spdk_nvme_probe_poll_async(g_hotplug_probe_ctx) != -EAGAIN) {
6103 : 3824 : g_hotplug_probe_ctx = NULL;
6104 : 3824 : spdk_poller_unregister(&g_hotplug_probe_poller);
6105 : 0 : }
6106 : :
6107 : 20065 : return SPDK_POLLER_BUSY;
6108 : 0 : }
6109 : :
6110 : : static int
6111 : 3971 : bdev_nvme_hotplug(void *arg)
6112 : : {
6113 : 1959 : struct spdk_nvme_transport_id trid_pcie;
6114 : :
6115 [ + + ]: 3971 : if (g_hotplug_probe_ctx) {
6116 : 147 : return SPDK_POLLER_BUSY;
6117 : : }
6118 : :
6119 [ - + ]: 3824 : memset(&trid_pcie, 0, sizeof(trid_pcie));
6120 : 3824 : spdk_nvme_trid_populate_transport(&trid_pcie, SPDK_NVME_TRANSPORT_PCIE);
6121 : :
6122 : 3824 : g_hotplug_probe_ctx = spdk_nvme_probe_async(&trid_pcie, NULL,
6123 : : hotplug_probe_cb, attach_cb, NULL);
6124 : :
6125 [ + - ]: 3824 : if (g_hotplug_probe_ctx) {
6126 [ - + # # ]: 3824 : assert(g_hotplug_probe_poller == NULL);
6127 : 3824 : g_hotplug_probe_poller = SPDK_POLLER_REGISTER(bdev_nvme_hotplug_probe, NULL, 1000);
6128 : 0 : }
6129 : :
6130 : 3824 : return SPDK_POLLER_BUSY;
6131 : 0 : }
6132 : :
6133 : : void
6134 : 1032 : spdk_bdev_nvme_get_opts(struct spdk_bdev_nvme_opts *opts, size_t opts_size)
6135 : : {
6136 [ + + ]: 1032 : if (!opts) {
6137 : 0 : SPDK_ERRLOG("opts should not be NULL\n");
6138 : 0 : return;
6139 : : }
6140 : :
6141 [ + + ]: 1032 : if (!opts_size) {
6142 : 0 : SPDK_ERRLOG("opts_size should not be zero value\n");
6143 : 0 : return;
6144 : : }
6145 : :
6146 [ + - + - ]: 1032 : opts->opts_size = opts_size;
6147 : :
6148 : : #define SET_FIELD(field, defval) \
6149 : : opts->field = SPDK_GET_FIELD(&g_opts, field, defval, opts_size); \
6150 : :
6151 [ + + + - : 1032 : SET_FIELD(action_on_timeout, 0);
+ - + - ]
6152 [ + + + - : 1032 : SET_FIELD(keep_alive_timeout_ms, 0);
+ - + - ]
6153 [ + + + - : 1032 : SET_FIELD(timeout_us, 0);
+ - + - ]
6154 [ + + + - : 1032 : SET_FIELD(timeout_admin_us, 0);
+ - + - ]
6155 [ + + + - : 1032 : SET_FIELD(transport_retry_count, 0);
+ - + - ]
6156 [ + + + - : 1032 : SET_FIELD(arbitration_burst, 0);
+ - + - ]
6157 [ + + + - : 1032 : SET_FIELD(low_priority_weight, 0);
+ - + - ]
6158 [ + + + - : 1032 : SET_FIELD(medium_priority_weight, 0);
+ - + - ]
6159 [ + + + - : 1032 : SET_FIELD(high_priority_weight, 0);
+ - + - ]
6160 [ + + + - : 1032 : SET_FIELD(io_queue_requests, 0);
+ - + - ]
6161 [ + + + - : 1032 : SET_FIELD(nvme_adminq_poll_period_us, 0);
+ - + - ]
6162 [ + + + - : 1032 : SET_FIELD(nvme_ioq_poll_period_us, 0);
+ - + - ]
6163 [ + + + + : 1032 : SET_FIELD(delay_cmd_submit, 0);
+ - + - +
- ]
6164 [ + + + - : 1032 : SET_FIELD(bdev_retry_count, 0);
+ - + - ]
6165 [ + + + - : 1032 : SET_FIELD(ctrlr_loss_timeout_sec, 0);
+ - + - ]
6166 [ + + + - : 1032 : SET_FIELD(reconnect_delay_sec, 0);
+ - + - ]
6167 [ + + + - : 1032 : SET_FIELD(fast_io_fail_timeout_sec, 0);
+ - + - ]
6168 [ + + + - : 1032 : SET_FIELD(transport_ack_timeout, 0);
+ - + - ]
6169 [ + + + + : 1032 : SET_FIELD(disable_auto_failback, false);
+ + + - ]
6170 [ + + + + : 1032 : SET_FIELD(generate_uuids, false);
+ + + - +
- ]
6171 [ + + + - : 1032 : SET_FIELD(transport_tos, 0);
+ - ]
6172 [ + + + + : 1032 : SET_FIELD(nvme_error_stat, false);
+ + + - +
- ]
6173 [ + + + + : 1032 : SET_FIELD(io_path_stat, false);
+ + + - ]
6174 [ + + + + : 1032 : SET_FIELD(allow_accel_sequence, false);
+ + + - +
- ]
6175 [ + + + - : 1032 : SET_FIELD(rdma_srq_size, 0);
+ - + - ]
6176 [ + + + - : 1032 : SET_FIELD(rdma_max_cq_size, 0);
+ - + - ]
6177 [ + + + - : 1032 : SET_FIELD(rdma_cm_event_timeout_ms, 0);
+ - + - ]
6178 [ + + + - : 1032 : SET_FIELD(dhchap_digests, 0);
+ - + - ]
6179 [ + - + - : 1032 : SET_FIELD(dhchap_dhgroups, 0);
+ - + - ]
6180 : :
6181 : : #undef SET_FIELD
6182 : :
6183 : : /* Do not remove this statement, you should always update this statement when you adding a new field,
6184 : : * and do not forget to add the SET_FIELD statement for your added field. */
6185 : : SPDK_STATIC_ASSERT(sizeof(struct spdk_bdev_nvme_opts) == 120, "Incorrect size");
6186 : 43 : }
6187 : :
6188 : : static bool bdev_nvme_check_io_error_resiliency_params(int32_t ctrlr_loss_timeout_sec,
6189 : : uint32_t reconnect_delay_sec,
6190 : : uint32_t fast_io_fail_timeout_sec);
6191 : :
6192 : : static int
6193 : 1032 : bdev_nvme_validate_opts(const struct spdk_bdev_nvme_opts *opts)
6194 : : {
6195 [ + + + + : 1032 : if ((opts->timeout_us == 0) && (opts->timeout_admin_us != 0)) {
+ - + - +
- + - ]
6196 : : /* Can't set timeout_admin_us without also setting timeout_us */
6197 : 0 : SPDK_WARNLOG("Invalid options: Can't have (timeout_us == 0) with (timeout_admin_us > 0)\n");
6198 : 0 : return -EINVAL;
6199 : : }
6200 : :
6201 [ + + + - : 1032 : if (opts->bdev_retry_count < -1) {
- + ]
6202 : 0 : SPDK_WARNLOG("Invalid option: bdev_retry_count can't be less than -1.\n");
6203 : 0 : return -EINVAL;
6204 : : }
6205 : :
6206 [ + + + - : 1075 : if (!bdev_nvme_check_io_error_resiliency_params(opts->ctrlr_loss_timeout_sec,
+ - ]
6207 [ + - + - ]: 1032 : opts->reconnect_delay_sec,
6208 [ + - + - ]: 1032 : opts->fast_io_fail_timeout_sec)) {
6209 : 0 : return -EINVAL;
6210 : : }
6211 : :
6212 : 1032 : return 0;
6213 : 43 : }
6214 : :
6215 : : int
6216 : 1032 : spdk_bdev_nvme_set_opts(const struct spdk_bdev_nvme_opts *opts)
6217 : : {
6218 [ + + ]: 1032 : if (!opts) {
6219 : 0 : SPDK_ERRLOG("opts cannot be NULL\n");
6220 : 0 : return -1;
6221 : : }
6222 : :
6223 [ + + + - : 1032 : if (!opts->opts_size) {
- + ]
6224 : 0 : SPDK_ERRLOG("opts_size inside opts cannot be zero value\n");
6225 : 0 : return -1;
6226 : : }
6227 : :
6228 : : int ret;
6229 : :
6230 : 1032 : ret = bdev_nvme_validate_opts(opts);
6231 [ - + ]: 1032 : if (ret) {
6232 : 0 : SPDK_WARNLOG("Failed to set nvme opts.\n");
6233 : 0 : return ret;
6234 : : }
6235 : :
6236 [ + + ]: 1032 : if (g_bdev_nvme_init_thread != NULL) {
6237 [ - + ]: 653 : if (!TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) {
6238 : 0 : return -EPERM;
6239 : : }
6240 : 1 : }
6241 : :
6242 [ + - + + : 1075 : if (opts->rdma_srq_size != 0 ||
+ - - + ]
6243 [ + - + - : 1032 : opts->rdma_max_cq_size != 0 ||
+ - ]
6244 [ - + - + ]: 1032 : opts->rdma_cm_event_timeout_ms != 0) {
6245 : 0 : struct spdk_nvme_transport_opts drv_opts;
6246 : :
6247 : 0 : spdk_nvme_transport_get_opts(&drv_opts, sizeof(drv_opts));
6248 [ # # # # : 0 : if (opts->rdma_srq_size != 0) {
# # ]
6249 [ # # # # ]: 0 : drv_opts.rdma_srq_size = opts->rdma_srq_size;
6250 : 0 : }
6251 [ # # # # : 0 : if (opts->rdma_max_cq_size != 0) {
# # ]
6252 [ # # # # : 0 : drv_opts.rdma_max_cq_size = opts->rdma_max_cq_size;
# # ]
6253 : 0 : }
6254 [ # # # # : 0 : if (opts->rdma_cm_event_timeout_ms != 0) {
# # ]
6255 [ # # # # : 0 : drv_opts.rdma_cm_event_timeout_ms = opts->rdma_cm_event_timeout_ms;
# # ]
6256 : 0 : }
6257 : :
6258 : 0 : ret = spdk_nvme_transport_set_opts(&drv_opts, sizeof(drv_opts));
6259 [ # # ]: 0 : if (ret) {
6260 : 0 : SPDK_ERRLOG("Failed to set NVMe transport opts.\n");
6261 : 0 : return ret;
6262 : : }
6263 : 0 : }
6264 : :
6265 : : #define SET_FIELD(field, defval) \
6266 : : g_opts.field = SPDK_GET_FIELD(opts, field, defval, opts->opts_size); \
6267 : :
6268 [ + - + - : 1032 : SET_FIELD(action_on_timeout, 0);
+ - + - +
- + - ]
6269 [ + - + - : 1032 : SET_FIELD(keep_alive_timeout_ms, 0);
+ - + - +
- + - ]
6270 [ + - + - : 1032 : SET_FIELD(timeout_us, 0);
+ - + - +
- + - ]
6271 [ + - + - : 1032 : SET_FIELD(timeout_admin_us, 0);
+ - + - +
- + - ]
6272 [ + - + - : 1032 : SET_FIELD(transport_retry_count, 0);
+ - + - +
- + - ]
6273 [ + - + - : 1032 : SET_FIELD(arbitration_burst, 0);
+ - + - +
- + - ]
6274 [ + - + - : 1032 : SET_FIELD(low_priority_weight, 0);
+ - + - +
- + - ]
6275 [ + - + - : 1032 : SET_FIELD(medium_priority_weight, 0);
+ - + - +
- + - ]
6276 [ + - + - : 1032 : SET_FIELD(high_priority_weight, 0);
+ - + - +
- + - ]
6277 [ + - + - : 1032 : SET_FIELD(io_queue_requests, 0);
+ - + - +
- + - ]
6278 [ + - + - : 1032 : SET_FIELD(nvme_adminq_poll_period_us, 0);
+ - + - +
- + - ]
6279 [ + - + - : 1032 : SET_FIELD(nvme_ioq_poll_period_us, 0);
+ - + - +
- + - ]
6280 [ + - + + : 1032 : SET_FIELD(delay_cmd_submit, 0);
+ - + - +
- + - +
- ]
6281 [ + - + - : 1032 : SET_FIELD(bdev_retry_count, 0);
+ - + - +
- + - ]
6282 [ + - + - : 1032 : SET_FIELD(ctrlr_loss_timeout_sec, 0);
+ - + - +
- + - ]
6283 [ + - + - : 1032 : SET_FIELD(reconnect_delay_sec, 0);
+ - + - +
- + - ]
6284 [ + - + - : 1032 : SET_FIELD(fast_io_fail_timeout_sec, 0);
+ - + - +
- + - ]
6285 [ + - + - : 1032 : SET_FIELD(transport_ack_timeout, 0);
+ - + - +
- + - ]
6286 [ + - + + : 1032 : SET_FIELD(disable_auto_failback, false);
+ + + - +
- + - ]
6287 [ + - + + : 1032 : SET_FIELD(generate_uuids, false);
+ + + - +
- + - +
- ]
6288 [ + - + - : 1032 : SET_FIELD(transport_tos, 0);
+ - + - +
- ]
6289 [ + - + + : 1032 : SET_FIELD(nvme_error_stat, false);
+ + + - +
- + - +
- ]
6290 [ + - + + : 1032 : SET_FIELD(io_path_stat, false);
+ + + - +
- + - ]
6291 [ + - + + : 1032 : SET_FIELD(allow_accel_sequence, false);
+ + + - +
- + - +
- ]
6292 [ + - + - : 1032 : SET_FIELD(rdma_srq_size, 0);
+ - + - +
- + - ]
6293 [ + - + - : 1032 : SET_FIELD(rdma_max_cq_size, 0);
+ - + - +
- + - ]
6294 [ + - + - : 1032 : SET_FIELD(rdma_cm_event_timeout_ms, 0);
+ - + - +
- + - ]
6295 [ + - + - : 1032 : SET_FIELD(dhchap_digests, 0);
+ - + - +
- + - ]
6296 [ + - + - : 1032 : SET_FIELD(dhchap_dhgroups, 0);
+ - + - +
- + - ]
6297 : :
6298 [ + - + - ]: 1032 : g_opts.opts_size = opts->opts_size;
6299 : :
6300 : : #undef SET_FIELD
6301 : :
6302 : 1032 : return 0;
6303 : 43 : }
6304 : :
6305 : : struct set_nvme_hotplug_ctx {
6306 : : uint64_t period_us;
6307 : : bool enabled;
6308 : : spdk_msg_fn fn;
6309 : : void *fn_ctx;
6310 : : };
6311 : :
6312 : : static void
6313 : 311 : set_nvme_hotplug_period_cb(void *_ctx)
6314 : : {
6315 : 311 : struct set_nvme_hotplug_ctx *ctx = _ctx;
6316 : :
6317 : 311 : spdk_poller_unregister(&g_hotplug_poller);
6318 [ + + + + : 311 : if (ctx->enabled) {
+ - - + ]
6319 [ # # # # ]: 12 : g_hotplug_poller = SPDK_POLLER_REGISTER(bdev_nvme_hotplug, NULL, ctx->period_us);
6320 : 0 : } else {
6321 : 299 : g_hotplug_poller = SPDK_POLLER_REGISTER(bdev_nvme_remove_poller, NULL,
6322 : : NVME_HOTPLUG_POLL_PERIOD_DEFAULT);
6323 : : }
6324 : :
6325 [ + - + - ]: 311 : g_nvme_hotplug_poll_period_us = ctx->period_us;
6326 [ + + + - : 311 : g_nvme_hotplug_enabled = ctx->enabled;
+ - ]
6327 [ + + + - : 311 : if (ctx->fn) {
+ - ]
6328 [ + - + - : 311 : ctx->fn(ctx->fn_ctx);
- + + - +
- + - ]
6329 : 41 : }
6330 : :
6331 : 311 : free(ctx);
6332 : 311 : }
6333 : :
6334 : : int
6335 : 311 : bdev_nvme_set_hotplug(bool enabled, uint64_t period_us, spdk_msg_fn cb, void *cb_ctx)
6336 : : {
6337 : : struct set_nvme_hotplug_ctx *ctx;
6338 : :
6339 [ + + - + : 311 : if (enabled == true && !spdk_process_is_primary()) {
# # ]
6340 : 0 : return -EPERM;
6341 : : }
6342 : :
6343 : 311 : ctx = calloc(1, sizeof(*ctx));
6344 [ + + ]: 311 : if (ctx == NULL) {
6345 : 0 : return -ENOMEM;
6346 : : }
6347 : :
6348 [ + + ]: 311 : period_us = period_us == 0 ? NVME_HOTPLUG_POLL_PERIOD_DEFAULT : period_us;
6349 [ + - - + : 311 : ctx->period_us = spdk_min(period_us, NVME_HOTPLUG_POLL_PERIOD_MAX);
- + ]
6350 [ - + - + : 311 : ctx->enabled = enabled;
- + ]
6351 [ - + - + ]: 311 : ctx->fn = cb;
6352 [ - + - + ]: 311 : ctx->fn_ctx = cb_ctx;
6353 : :
6354 : 311 : spdk_thread_send_msg(g_bdev_nvme_init_thread, set_nvme_hotplug_period_cb, ctx);
6355 : 311 : return 0;
6356 : 41 : }
6357 : :
6358 : : static void
6359 : 1652 : nvme_ctrlr_populate_namespaces_done(struct nvme_ctrlr *nvme_ctrlr,
6360 : : struct nvme_async_probe_ctx *ctx)
6361 : : {
6362 : : struct nvme_ns *nvme_ns;
6363 : : struct nvme_bdev *nvme_bdev;
6364 : : size_t j;
6365 : :
6366 [ + + # # ]: 1652 : assert(nvme_ctrlr != NULL);
6367 : :
6368 [ + + + - : 1652 : if (ctx->names == NULL) {
+ - ]
6369 [ # # # # ]: 37 : ctx->reported_bdevs = 0;
6370 : 37 : populate_namespaces_cb(ctx, 0);
6371 : 37 : return;
6372 : : }
6373 : :
6374 : : /*
6375 : : * Report the new bdevs that were created in this call.
6376 : : * There can be more than one bdev per NVMe controller.
6377 : : */
6378 : 1615 : j = 0;
6379 : :
6380 [ + + + - ]: 1615 : pthread_mutex_lock(&nvme_ctrlr->mutex);
6381 : :
6382 : 1615 : nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
6383 [ + + ]: 3039 : while (nvme_ns != NULL) {
6384 [ + - + - ]: 1424 : nvme_bdev = nvme_ns->bdev;
6385 [ + - + - : 1424 : if (j < ctx->max_bdevs) {
+ - ]
6386 [ + - + - : 1424 : ctx->names[j] = nvme_bdev->disk.name;
+ - + - +
- + - +
- ]
6387 : 1424 : j++;
6388 : 60 : } else {
6389 [ # # # # ]: 0 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
6390 : :
6391 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
6392 : : "Maximum number of namespaces supported per NVMe controller is %du. "
6393 : : "Unable to return all names of created bdevs\n",
6394 : : ctx->max_bdevs);
6395 [ # # # # ]: 0 : ctx->reported_bdevs = 0;
6396 : 0 : populate_namespaces_cb(ctx, -ERANGE);
6397 : 0 : return;
6398 : : }
6399 : :
6400 : 1424 : nvme_ns = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns);
6401 : : }
6402 : :
6403 [ + + + - ]: 1615 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
6404 : :
6405 [ + - + - ]: 1615 : ctx->reported_bdevs = j;
6406 : 1615 : populate_namespaces_cb(ctx, 0);
6407 : 58 : }
6408 : :
6409 : : static int
6410 : 52 : bdev_nvme_check_secondary_trid(struct nvme_ctrlr *nvme_ctrlr,
6411 : : struct spdk_nvme_ctrlr *new_ctrlr,
6412 : : struct spdk_nvme_transport_id *trid)
6413 : : {
6414 : : struct nvme_path_id *tmp_trid;
6415 : :
6416 [ - + # # : 52 : if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
# # ]
6417 [ # # # # : 0 : NVME_CTRLR_ERRLOG(nvme_ctrlr, "PCIe failover is not supported.\n");
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
6418 : 0 : return -ENOTSUP;
6419 : : }
6420 : :
6421 : : /* Currently we only support failover to the same transport type. */
6422 [ - + # # : 52 : if (nvme_ctrlr->active_path_id->trid.trtype != trid->trtype) {
# # # # #
# # # # #
# # ]
6423 [ # # # # : 0 : NVME_CTRLR_WARNLOG(nvme_ctrlr,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
6424 : : "Failover from trtype: %s to a different trtype: %s is not supported currently\n",
6425 : : spdk_nvme_transport_id_trtype_str(nvme_ctrlr->active_path_id->trid.trtype),
6426 : : spdk_nvme_transport_id_trtype_str(trid->trtype));
6427 : 0 : return -EINVAL;
6428 : : }
6429 : :
6430 : :
6431 : : /* Currently we only support failover to the same NQN. */
6432 [ - + - + : 52 : if (strncmp(trid->subnqn, nvme_ctrlr->active_path_id->trid.subnqn, SPDK_NVMF_NQN_MAX_LEN)) {
- + # # #
# # # # #
# # ]
6433 [ # # # # : 0 : NVME_CTRLR_WARNLOG(nvme_ctrlr,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
6434 : : "Failover from subnqn: %s to a different subnqn: %s is not supported currently\n",
6435 : : nvme_ctrlr->active_path_id->trid.subnqn, trid->subnqn);
6436 : 0 : return -EINVAL;
6437 : : }
6438 : :
6439 : : /* Skip all the other checks if we've already registered this path. */
6440 [ + + # # : 124 : TAILQ_FOREACH(tmp_trid, &nvme_ctrlr->trids, link) {
# # # # #
# # # #
# ]
6441 [ + + # # ]: 72 : if (!spdk_nvme_transport_id_compare(&tmp_trid->trid, trid)) {
6442 [ # # # # : 0 : NVME_CTRLR_WARNLOG(nvme_ctrlr, "This path (traddr: %s subnqn: %s) is already registered\n",
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
6443 : : trid->traddr, trid->subnqn);
6444 : 0 : return -EALREADY;
6445 : : }
6446 : 12 : }
6447 : :
6448 : 52 : return 0;
6449 : 9 : }
6450 : :
6451 : : static int
6452 : 52 : bdev_nvme_check_secondary_namespace(struct nvme_ctrlr *nvme_ctrlr,
6453 : : struct spdk_nvme_ctrlr *new_ctrlr)
6454 : : {
6455 : : struct nvme_ns *nvme_ns;
6456 : : struct spdk_nvme_ns *new_ns;
6457 : :
6458 : 52 : nvme_ns = nvme_ctrlr_get_first_active_ns(nvme_ctrlr);
6459 [ + + ]: 68 : while (nvme_ns != NULL) {
6460 [ # # # # ]: 16 : new_ns = spdk_nvme_ctrlr_get_ns(new_ctrlr, nvme_ns->id);
6461 [ - + # # ]: 16 : assert(new_ns != NULL);
6462 : :
6463 [ - + # # : 16 : if (!bdev_nvme_compare_ns(nvme_ns->ns, new_ns)) {
# # ]
6464 : 0 : return -EINVAL;
6465 : : }
6466 : :
6467 : 16 : nvme_ns = nvme_ctrlr_get_next_active_ns(nvme_ctrlr, nvme_ns);
6468 : : }
6469 : :
6470 : 52 : return 0;
6471 : 9 : }
6472 : :
6473 : : static int
6474 : 52 : _bdev_nvme_add_secondary_trid(struct nvme_ctrlr *nvme_ctrlr,
6475 : : struct spdk_nvme_transport_id *trid)
6476 : : {
6477 : : struct nvme_path_id *active_id, *new_trid, *tmp_trid;
6478 : :
6479 : 52 : new_trid = calloc(1, sizeof(*new_trid));
6480 [ + + ]: 52 : if (new_trid == NULL) {
6481 : 0 : return -ENOMEM;
6482 : : }
6483 [ # # ]: 52 : new_trid->trid = *trid;
6484 : :
6485 [ # # # # ]: 52 : active_id = nvme_ctrlr->active_path_id;
6486 [ + + # # ]: 52 : assert(active_id != NULL);
6487 [ - + # # : 52 : assert(active_id == TAILQ_FIRST(&nvme_ctrlr->trids));
# # # # #
# ]
6488 : :
6489 : : /* Skip the active trid not to replace it until it is failed. */
6490 [ # # # # : 52 : tmp_trid = TAILQ_NEXT(active_id, link);
# # ]
6491 [ + + ]: 52 : if (tmp_trid == NULL) {
6492 : 32 : goto add_tail;
6493 : : }
6494 : :
6495 : : /* It means the trid is faled if its last failed time is non-zero.
6496 : : * Insert the new alternate trid before any failed trid.
6497 : : */
6498 [ - + + + : 32 : TAILQ_FOREACH_FROM(tmp_trid, &nvme_ctrlr->trids, link) {
# # # # #
# # # # #
# # ]
6499 [ + + # # : 20 : if (tmp_trid->last_failed_tsc != 0) {
# # ]
6500 [ # # # # : 8 : TAILQ_INSERT_BEFORE(tmp_trid, new_trid, link);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
6501 : 8 : return 0;
6502 : : }
6503 : 4 : }
6504 : :
6505 : 10 : add_tail:
6506 [ # # # # : 44 : TAILQ_INSERT_TAIL(&nvme_ctrlr->trids, new_trid, link);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
6507 : 44 : return 0;
6508 : 9 : }
6509 : :
6510 : : /* This is the case that a secondary path is added to an existing
6511 : : * nvme_ctrlr for failover. After checking if it can access the same
6512 : : * namespaces as the primary path, it is disconnected until failover occurs.
6513 : : */
6514 : : static int
6515 : 52 : bdev_nvme_add_secondary_trid(struct nvme_ctrlr *nvme_ctrlr,
6516 : : struct spdk_nvme_ctrlr *new_ctrlr,
6517 : : struct spdk_nvme_transport_id *trid)
6518 : : {
6519 : : int rc;
6520 : :
6521 [ + + # # ]: 52 : assert(nvme_ctrlr != NULL);
6522 : :
6523 [ - + # # ]: 52 : pthread_mutex_lock(&nvme_ctrlr->mutex);
6524 : :
6525 : 52 : rc = bdev_nvme_check_secondary_trid(nvme_ctrlr, new_ctrlr, trid);
6526 [ - + ]: 52 : if (rc != 0) {
6527 : 0 : goto exit;
6528 : : }
6529 : :
6530 : 52 : rc = bdev_nvme_check_secondary_namespace(nvme_ctrlr, new_ctrlr);
6531 [ - + ]: 52 : if (rc != 0) {
6532 : 0 : goto exit;
6533 : : }
6534 : :
6535 : 52 : rc = _bdev_nvme_add_secondary_trid(nvme_ctrlr, trid);
6536 : :
6537 : 43 : exit:
6538 [ - + # # ]: 52 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
6539 : :
6540 : 52 : spdk_nvme_detach(new_ctrlr);
6541 : :
6542 : 52 : return rc;
6543 : : }
6544 : :
6545 : : static void
6546 : 1656 : connect_attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
6547 : : struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
6548 : : {
6549 : 1656 : struct spdk_nvme_ctrlr_opts *user_opts = cb_ctx;
6550 : : struct nvme_async_probe_ctx *ctx;
6551 : : int rc;
6552 : :
6553 : 1656 : ctx = SPDK_CONTAINEROF(user_opts, struct nvme_async_probe_ctx, drv_opts);
6554 [ + - + - ]: 1656 : ctx->ctrlr_attached = true;
6555 : :
6556 [ + - + - : 1656 : rc = nvme_ctrlr_create(ctrlr, ctx->base_name, &ctx->trid, ctx);
+ - ]
6557 [ + + ]: 1656 : if (rc != 0) {
6558 [ # # # # ]: 4 : ctx->reported_bdevs = 0;
6559 : 4 : populate_namespaces_cb(ctx, rc);
6560 : 1 : }
6561 : 1656 : }
6562 : :
6563 : :
6564 : : static void
6565 : 32 : connect_set_failover_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
6566 : : struct spdk_nvme_ctrlr *ctrlr,
6567 : : const struct spdk_nvme_ctrlr_opts *opts)
6568 : : {
6569 : 32 : struct spdk_nvme_ctrlr_opts *user_opts = cb_ctx;
6570 : : struct nvme_ctrlr *nvme_ctrlr;
6571 : : struct nvme_async_probe_ctx *ctx;
6572 : : int rc;
6573 : :
6574 : 32 : ctx = SPDK_CONTAINEROF(user_opts, struct nvme_async_probe_ctx, drv_opts);
6575 [ # # # # ]: 32 : ctx->ctrlr_attached = true;
6576 : :
6577 [ # # # # ]: 32 : nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->base_name);
6578 [ + - ]: 32 : if (nvme_ctrlr) {
6579 [ # # ]: 32 : rc = bdev_nvme_add_secondary_trid(nvme_ctrlr, ctrlr, &ctx->trid);
6580 : 4 : } else {
6581 : 0 : rc = -ENODEV;
6582 : : }
6583 : :
6584 [ # # # # ]: 32 : ctx->reported_bdevs = 0;
6585 : 32 : populate_namespaces_cb(ctx, rc);
6586 : 32 : }
6587 : :
6588 : : static int
6589 : 270532 : bdev_nvme_async_poll(void *arg)
6590 : : {
6591 : 270532 : struct nvme_async_probe_ctx *ctx = arg;
6592 : : int rc;
6593 : :
6594 [ + - + - ]: 270532 : rc = spdk_nvme_probe_poll_async(ctx->probe_ctx);
6595 [ + + ]: 270532 : if (spdk_unlikely(rc != -EAGAIN)) {
6596 [ + - + - ]: 1747 : ctx->probe_done = true;
6597 [ + - ]: 1747 : spdk_poller_unregister(&ctx->poller);
6598 [ + + + + : 1747 : if (!ctx->ctrlr_attached) {
+ - + - ]
6599 : : /* The probe is done, but no controller was attached.
6600 : : * That means we had a failure, so report -EIO back to
6601 : : * the caller (usually the RPC). populate_namespaces_cb()
6602 : : * will take care of freeing the nvme_async_probe_ctx.
6603 : : */
6604 [ # # # # ]: 59 : ctx->reported_bdevs = 0;
6605 : 59 : populate_namespaces_cb(ctx, -EIO);
6606 [ + + + + : 1689 : } else if (ctx->namespaces_populated) {
+ - - + ]
6607 : : /* The namespaces for the attached controller were all
6608 : : * populated and the response was already sent to the
6609 : : * caller (usually the RPC). So free the context here.
6610 : : */
6611 : 1233 : free_nvme_async_probe_ctx(ctx);
6612 : 32 : }
6613 : 64 : }
6614 : :
6615 : 270532 : return SPDK_POLLER_BUSY;
6616 : : }
6617 : :
6618 : : static bool
6619 : 2864 : bdev_nvme_check_io_error_resiliency_params(int32_t ctrlr_loss_timeout_sec,
6620 : : uint32_t reconnect_delay_sec,
6621 : : uint32_t fast_io_fail_timeout_sec)
6622 : : {
6623 [ + + ]: 2864 : if (ctrlr_loss_timeout_sec < -1) {
6624 : 4 : SPDK_ERRLOG("ctrlr_loss_timeout_sec can't be less than -1.\n");
6625 : 4 : return false;
6626 [ + + ]: 2860 : } else if (ctrlr_loss_timeout_sec == -1) {
6627 [ + + ]: 68 : if (reconnect_delay_sec == 0) {
6628 : 4 : SPDK_ERRLOG("reconnect_delay_sec can't be 0 if ctrlr_loss_timeout_sec is not 0.\n");
6629 : 4 : return false;
6630 [ + + + + ]: 64 : } else if (fast_io_fail_timeout_sec != 0 &&
6631 : 3 : fast_io_fail_timeout_sec < reconnect_delay_sec) {
6632 : 4 : SPDK_ERRLOG("reconnect_delay_sec can't be more than fast_io-fail_timeout_sec.\n");
6633 : 4 : return false;
6634 : : }
6635 [ + + ]: 2804 : } else if (ctrlr_loss_timeout_sec != 0) {
6636 [ + + ]: 72 : if (reconnect_delay_sec == 0) {
6637 : 4 : SPDK_ERRLOG("reconnect_delay_sec can't be 0 if ctrlr_loss_timeout_sec is not 0.\n");
6638 : 4 : return false;
6639 [ + + ]: 68 : } else if (reconnect_delay_sec > (uint32_t)ctrlr_loss_timeout_sec) {
6640 : 4 : SPDK_ERRLOG("reconnect_delay_sec can't be more than ctrlr_loss_timeout_sec.\n");
6641 : 4 : return false;
6642 [ + + ]: 64 : } else if (fast_io_fail_timeout_sec != 0) {
6643 [ + + ]: 32 : if (fast_io_fail_timeout_sec < reconnect_delay_sec) {
6644 : 4 : SPDK_ERRLOG("reconnect_delay_sec can't be more than fast_io_fail_timeout_sec.\n");
6645 : 4 : return false;
6646 [ + + ]: 28 : } else if (fast_io_fail_timeout_sec > (uint32_t)ctrlr_loss_timeout_sec) {
6647 : 4 : SPDK_ERRLOG("fast_io_fail_timeout_sec can't be more than ctrlr_loss_timeout_sec.\n");
6648 : 4 : return false;
6649 : : }
6650 : 4 : }
6651 [ + + + + ]: 2727 : } else if (reconnect_delay_sec != 0 || fast_io_fail_timeout_sec != 0) {
6652 : 8 : SPDK_ERRLOG("Both reconnect_delay_sec and fast_io_fail_timeout_sec must be 0 if ctrlr_loss_timeout_sec is 0.\n");
6653 : 8 : return false;
6654 : : }
6655 : :
6656 : 2828 : return true;
6657 : 126 : }
6658 : :
6659 : : int
6660 : 1756 : spdk_bdev_nvme_create(struct spdk_nvme_transport_id *trid,
6661 : : const char *base_name,
6662 : : const char **names,
6663 : : uint32_t count,
6664 : : spdk_bdev_nvme_create_cb cb_fn,
6665 : : void *cb_ctx,
6666 : : struct spdk_nvme_ctrlr_opts *drv_opts,
6667 : : struct spdk_bdev_nvme_ctrlr_opts *bdev_opts)
6668 : : {
6669 : : struct nvme_probe_skip_entry *entry, *tmp;
6670 : : struct nvme_async_probe_ctx *ctx;
6671 : : spdk_nvme_attach_cb attach_cb;
6672 : : struct nvme_ctrlr *nvme_ctrlr;
6673 : : int len;
6674 : :
6675 : : /* TODO expand this check to include both the host and target TRIDs.
6676 : : * Only if both are the same should we fail.
6677 : : */
6678 [ + + - + ]: 1756 : if (nvme_ctrlr_get(trid, drv_opts->hostnqn) != NULL) {
6679 [ # # # # ]: 0 : SPDK_ERRLOG("A controller with the provided trid (traddr: %s, hostnqn: %s) "
6680 : : "already exists.\n", trid->traddr, drv_opts->hostnqn);
6681 : 0 : return -EEXIST;
6682 : : }
6683 : :
6684 [ + + ]: 1756 : len = strnlen(base_name, SPDK_CONTROLLER_NAME_MAX);
6685 : :
6686 [ + - - + ]: 1756 : if (len == 0 || len == SPDK_CONTROLLER_NAME_MAX) {
6687 : 0 : SPDK_ERRLOG("controller name must be between 1 and %d characters\n", SPDK_CONTROLLER_NAME_MAX - 1);
6688 : 0 : return -EINVAL;
6689 : : }
6690 : :
6691 [ + - + - ]: 1757 : if (bdev_opts != NULL &&
6692 [ + + + - ]: 1820 : !bdev_nvme_check_io_error_resiliency_params(bdev_opts->ctrlr_loss_timeout_sec,
6693 [ + - + - ]: 64 : bdev_opts->reconnect_delay_sec,
6694 [ + - + - ]: 64 : bdev_opts->fast_io_fail_timeout_sec)) {
6695 : 0 : return -EINVAL;
6696 : : }
6697 : :
6698 : 1756 : ctx = calloc(1, sizeof(*ctx));
6699 [ + + ]: 1756 : if (!ctx) {
6700 : 0 : return -ENOMEM;
6701 : : }
6702 [ + + + - : 1756 : ctx->base_name = strdup(base_name);
+ - ]
6703 [ + + + - : 1756 : if (!ctx->base_name) {
+ - ]
6704 : 0 : free(ctx);
6705 : 0 : return -ENOMEM;
6706 : : }
6707 [ + - + - ]: 1756 : ctx->names = names;
6708 [ + - + - ]: 1756 : ctx->max_bdevs = count;
6709 [ + - + - ]: 1756 : ctx->cb_fn = cb_fn;
6710 [ + - + - ]: 1756 : ctx->cb_ctx = cb_ctx;
6711 [ + - ]: 1756 : ctx->trid = *trid;
6712 : :
6713 [ + + ]: 1756 : if (bdev_opts) {
6714 [ + + + + : 1756 : memcpy(&ctx->bdev_opts, bdev_opts, sizeof(*bdev_opts));
+ - ]
6715 : 64 : } else {
6716 [ # # ]: 0 : spdk_bdev_nvme_get_default_ctrlr_opts(&ctx->bdev_opts);
6717 : : }
6718 : :
6719 [ + + + - : 1756 : if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
- + ]
6720 [ + + + - : 492 : TAILQ_FOREACH_SAFE(entry, &g_skipped_nvme_ctrlrs, tailq, tmp) {
# # # # +
- ]
6721 [ + - # # ]: 2 : if (spdk_nvme_transport_id_compare(trid, &entry->trid) == 0) {
6722 [ - + # # : 2 : TAILQ_REMOVE(&g_skipped_nvme_ctrlrs, entry, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
6723 : 2 : free(entry);
6724 : 2 : break;
6725 : : }
6726 : 0 : }
6727 : 11 : }
6728 : :
6729 [ + + + + : 1756 : memcpy(&ctx->drv_opts, drv_opts, sizeof(*drv_opts));
+ - ]
6730 [ + - + - : 1756 : ctx->drv_opts.transport_retry_count = g_opts.transport_retry_count;
+ - + - ]
6731 [ + - + - : 1756 : ctx->drv_opts.transport_ack_timeout = g_opts.transport_ack_timeout;
+ - + - ]
6732 [ + - + - : 1756 : ctx->drv_opts.keep_alive_timeout_ms = g_opts.keep_alive_timeout_ms;
+ - + - ]
6733 [ + - + - : 1756 : ctx->drv_opts.disable_read_ana_log_page = true;
+ - ]
6734 [ + - + - : 1756 : ctx->drv_opts.transport_tos = g_opts.transport_tos;
+ - ]
6735 : :
6736 [ + + ]: 1756 : if (spdk_interrupt_mode_is_enabled()) {
6737 [ + - # # : 1 : if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) {
# # ]
6738 [ # # # # : 1 : ctx->drv_opts.enable_interrupts = true;
# # ]
6739 : 0 : } else {
6740 : 0 : SPDK_ERRLOG("Interrupt mode is only supported with PCIe transport\n");
6741 : 0 : free_nvme_async_probe_ctx(ctx);
6742 : 0 : return -ENOTSUP;
6743 : : }
6744 : 0 : }
6745 : :
6746 [ + + + - : 1756 : if (ctx->bdev_opts.psk != NULL) {
+ - + - ]
6747 [ # # # # : 66 : ctx->drv_opts.tls_psk = spdk_keyring_get_key(ctx->bdev_opts.psk);
# # # # #
# # # ]
6748 [ + + # # : 66 : if (ctx->drv_opts.tls_psk == NULL) {
# # # # ]
6749 [ # # # # : 6 : SPDK_ERRLOG("Could not load PSK: %s\n", ctx->bdev_opts.psk);
# # ]
6750 : 6 : free_nvme_async_probe_ctx(ctx);
6751 : 6 : return -ENOKEY;
6752 : : }
6753 : 0 : }
6754 : :
6755 [ + + + - : 1750 : if (ctx->bdev_opts.dhchap_key != NULL) {
+ - + - ]
6756 [ # # # # : 664 : ctx->drv_opts.dhchap_key = spdk_keyring_get_key(ctx->bdev_opts.dhchap_key);
# # # # #
# # # ]
6757 [ - + # # : 664 : if (ctx->drv_opts.dhchap_key == NULL) {
# # # # ]
6758 [ # # # # : 0 : SPDK_ERRLOG("Could not load DH-HMAC-CHAP key: %s\n",
# # ]
6759 : : ctx->bdev_opts.dhchap_key);
6760 : 0 : free_nvme_async_probe_ctx(ctx);
6761 : 0 : return -ENOKEY;
6762 : : }
6763 : :
6764 [ # # # # : 664 : ctx->drv_opts.dhchap_digests = g_opts.dhchap_digests;
# # # # ]
6765 [ # # # # : 664 : ctx->drv_opts.dhchap_dhgroups = g_opts.dhchap_dhgroups;
# # # # ]
6766 : 0 : }
6767 [ + + + - : 1750 : if (ctx->bdev_opts.dhchap_ctrlr_key != NULL) {
+ - + - ]
6768 [ # # # # : 500 : ctx->drv_opts.dhchap_ctrlr_key =
# # ]
6769 [ # # # # : 500 : spdk_keyring_get_key(ctx->bdev_opts.dhchap_ctrlr_key);
# # ]
6770 [ - + # # : 500 : if (ctx->drv_opts.dhchap_ctrlr_key == NULL) {
# # # # ]
6771 [ # # # # : 0 : SPDK_ERRLOG("Could not load DH-HMAC-CHAP controller key: %s\n",
# # ]
6772 : : ctx->bdev_opts.dhchap_ctrlr_key);
6773 : 0 : free_nvme_async_probe_ctx(ctx);
6774 : 0 : return -ENOKEY;
6775 : : }
6776 : 0 : }
6777 : :
6778 [ + + + + : 1750 : if (nvme_bdev_ctrlr_get_by_name(base_name) == NULL || ctx->bdev_opts.multipath) {
+ + # # #
# # # ]
6779 : 1718 : attach_cb = connect_attach_cb;
6780 : 60 : } else {
6781 : 32 : attach_cb = connect_set_failover_cb;
6782 : : }
6783 : :
6784 [ + - + - ]: 1750 : nvme_ctrlr = nvme_ctrlr_get_by_name(ctx->base_name);
6785 [ + + + + : 1750 : if (nvme_ctrlr && nvme_ctrlr->opts.multipath != ctx->bdev_opts.multipath) {
- + - + #
# # # # #
# # # # #
# ]
6786 : : /* All controllers with the same name must be configured the same
6787 : : * way, either for multipath or failover. If the configuration doesn't
6788 : : * match - report error.
6789 : : */
6790 : 0 : free_nvme_async_probe_ctx(ctx);
6791 : 0 : return -EINVAL;
6792 : : }
6793 : :
6794 [ + - + - : 1750 : ctx->probe_ctx = spdk_nvme_connect_async(trid, &ctx->drv_opts, attach_cb);
+ - ]
6795 [ + + + - : 1750 : if (ctx->probe_ctx == NULL) {
+ - ]
6796 [ # # ]: 3 : SPDK_ERRLOG("No controller was found with provided trid (traddr: %s)\n", trid->traddr);
6797 : 3 : free_nvme_async_probe_ctx(ctx);
6798 : 3 : return -ENODEV;
6799 : : }
6800 [ + - + - ]: 1747 : ctx->poller = SPDK_POLLER_REGISTER(bdev_nvme_async_poll, ctx, 1000);
6801 : :
6802 : 1747 : return 0;
6803 : 64 : }
6804 : :
6805 : : struct bdev_nvme_delete_ctx {
6806 : : char *name;
6807 : : struct nvme_path_id path_id;
6808 : : bdev_nvme_delete_done_fn delete_done;
6809 : : void *delete_done_ctx;
6810 : : uint64_t timeout_ticks;
6811 : : struct spdk_poller *poller;
6812 : : };
6813 : :
6814 : : static void
6815 : 706 : free_bdev_nvme_delete_ctx(struct bdev_nvme_delete_ctx *ctx)
6816 : : {
6817 [ + + ]: 706 : if (ctx != NULL) {
6818 [ # # # # ]: 702 : free(ctx->name);
6819 : 702 : free(ctx);
6820 : 4 : }
6821 : 706 : }
6822 : :
6823 : : static bool
6824 : 36695 : nvme_path_id_compare(struct nvme_path_id *p, const struct nvme_path_id *path_id)
6825 : : {
6826 [ + + # # : 36695 : if (path_id->trid.trtype != 0) {
# # # # ]
6827 [ - + # # : 226 : if (path_id->trid.trtype == SPDK_NVME_TRANSPORT_CUSTOM) {
# # # # ]
6828 [ # # # # : 0 : if (strcasecmp(path_id->trid.trstring, p->trid.trstring) != 0) {
# # # # #
# # # #
# ]
6829 : 0 : return false;
6830 : : }
6831 : 0 : } else {
6832 [ - + # # : 226 : if (path_id->trid.trtype != p->trid.trtype) {
# # # # #
# # # #
# ]
6833 : 0 : return false;
6834 : : }
6835 : : }
6836 : 21 : }
6837 : :
6838 [ + + # # : 36695 : if (!spdk_mem_all_zero(path_id->trid.traddr, sizeof(path_id->trid.traddr))) {
# # ]
6839 [ + + - + : 226 : if (strcasecmp(path_id->trid.traddr, p->trid.traddr) != 0) {
+ + # # #
# # # #
# ]
6840 : 44 : return false;
6841 : : }
6842 : 10 : }
6843 : :
6844 [ + + # # : 36651 : if (path_id->trid.adrfam != 0) {
# # # # ]
6845 [ - + # # : 142 : if (path_id->trid.adrfam != p->trid.adrfam) {
# # # # #
# # # #
# ]
6846 : 0 : return false;
6847 : : }
6848 : 0 : }
6849 : :
6850 [ + + # # : 36651 : if (!spdk_mem_all_zero(path_id->trid.trsvcid, sizeof(path_id->trid.trsvcid))) {
# # ]
6851 [ - + - + : 182 : if (strcasecmp(path_id->trid.trsvcid, p->trid.trsvcid) != 0) {
+ + # # #
# # # #
# ]
6852 : 49 : return false;
6853 : : }
6854 : 10 : }
6855 : :
6856 [ + + # # : 36602 : if (!spdk_mem_all_zero(path_id->trid.subnqn, sizeof(path_id->trid.subnqn))) {
# # ]
6857 [ - + - + : 133 : if (strcmp(path_id->trid.subnqn, p->trid.subnqn) != 0) {
- + # # #
# # # #
# ]
6858 : 0 : return false;
6859 : : }
6860 : 10 : }
6861 : :
6862 [ + + # # : 36602 : if (!spdk_mem_all_zero(path_id->hostid.hostaddr, sizeof(path_id->hostid.hostaddr))) {
# # ]
6863 [ # # # # : 0 : if (strcmp(path_id->hostid.hostaddr, p->hostid.hostaddr) != 0) {
# # # # #
# # # #
# ]
6864 : 0 : return false;
6865 : : }
6866 : 0 : }
6867 : :
6868 [ + + # # : 36602 : if (!spdk_mem_all_zero(path_id->hostid.hostsvcid, sizeof(path_id->hostid.hostsvcid))) {
# # ]
6869 [ # # # # : 0 : if (strcmp(path_id->hostid.hostsvcid, p->hostid.hostsvcid) != 0) {
# # # # #
# # # #
# ]
6870 : 0 : return false;
6871 : : }
6872 : 0 : }
6873 : :
6874 : 36602 : return true;
6875 : 94 : }
6876 : :
6877 : : static bool
6878 : 36316 : nvme_path_id_exists(const char *name, const struct nvme_path_id *path_id)
6879 : : {
6880 : : struct nvme_bdev_ctrlr *nbdev_ctrlr;
6881 : : struct nvme_ctrlr *ctrlr;
6882 : : struct nvme_path_id *p;
6883 : :
6884 [ - + ]: 36316 : pthread_mutex_lock(&g_bdev_nvme_mutex);
6885 : 36316 : nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
6886 [ + + ]: 36316 : if (!nbdev_ctrlr) {
6887 [ - + ]: 692 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
6888 : 692 : return false;
6889 : : }
6890 : :
6891 [ + + # # : 35650 : TAILQ_FOREACH(ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
# # # # #
# # # #
# ]
6892 [ - + # # ]: 35640 : pthread_mutex_lock(&ctrlr->mutex);
6893 [ + + # # : 35670 : TAILQ_FOREACH(p, &ctrlr->trids, link) {
# # # # #
# # # #
# ]
6894 [ + + ]: 35644 : if (nvme_path_id_compare(p, path_id)) {
6895 [ - + # # ]: 35614 : pthread_mutex_unlock(&ctrlr->mutex);
6896 [ - + ]: 35614 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
6897 : 35614 : return true;
6898 : : }
6899 : 0 : }
6900 [ - + # # ]: 26 : pthread_mutex_unlock(&ctrlr->mutex);
6901 : 0 : }
6902 [ - + ]: 10 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
6903 : :
6904 : 10 : return false;
6905 : 20 : }
6906 : :
6907 : : static int
6908 : 36316 : bdev_nvme_delete_complete_poll(void *arg)
6909 : : {
6910 : 36316 : struct bdev_nvme_delete_ctx *ctx = arg;
6911 : 36316 : int rc = 0;
6912 : :
6913 [ + + # # : 36316 : if (nvme_path_id_exists(ctx->name, &ctx->path_id)) {
# # # # ]
6914 [ + - # # : 35614 : if (ctx->timeout_ticks > spdk_get_ticks()) {
# # ]
6915 : 35614 : return SPDK_POLLER_BUSY;
6916 : : }
6917 : :
6918 [ # # # # ]: 0 : SPDK_ERRLOG("NVMe path '%s' still exists after delete\n", ctx->name);
6919 : 0 : rc = -ETIMEDOUT;
6920 : 0 : }
6921 : :
6922 [ # # ]: 702 : spdk_poller_unregister(&ctx->poller);
6923 : :
6924 [ # # # # : 702 : ctx->delete_done(ctx->delete_done_ctx, rc);
# # # # #
# # # ]
6925 : 702 : free_bdev_nvme_delete_ctx(ctx);
6926 : :
6927 : 702 : return SPDK_POLLER_BUSY;
6928 : 20 : }
6929 : :
6930 : : static int
6931 : 999 : _bdev_nvme_delete(struct nvme_ctrlr *nvme_ctrlr, const struct nvme_path_id *path_id)
6932 : : {
6933 : : struct nvme_path_id *p, *t;
6934 : : spdk_msg_fn msg_fn;
6935 : 999 : int rc = -ENXIO;
6936 : :
6937 [ - + # # ]: 999 : pthread_mutex_lock(&nvme_ctrlr->mutex);
6938 : :
6939 [ + + - + : 1051 : TAILQ_FOREACH_REVERSE_SAFE(p, &nvme_ctrlr->trids, nvme_paths, link, t) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
6940 [ + + # # : 1051 : if (p == TAILQ_FIRST(&nvme_ctrlr->trids)) {
# # # # ]
6941 : 999 : break;
6942 : : }
6943 : :
6944 [ + + ]: 52 : if (!nvme_path_id_compare(p, path_id)) {
6945 : 20 : continue;
6946 : : }
6947 : :
6948 : : /* We are not using the specified path. */
6949 [ + + # # : 32 : TAILQ_REMOVE(&nvme_ctrlr->trids, p, link);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
6950 : 32 : free(p);
6951 : 32 : rc = 0;
6952 : 7 : }
6953 : :
6954 [ + - + + ]: 999 : if (p == NULL || !nvme_path_id_compare(p, path_id)) {
6955 [ - + # # ]: 43 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
6956 : 43 : return rc;
6957 : : }
6958 : :
6959 : : /* If we made it here, then this path is a match! Now we need to remove it. */
6960 : :
6961 : : /* This is the active path in use right now. The active path is always the first in the list. */
6962 [ + + # # : 956 : assert(p == nvme_ctrlr->active_path_id);
# # # # ]
6963 : :
6964 [ + + # # : 956 : if (!TAILQ_NEXT(p, link)) {
# # # # ]
6965 : : /* The current path is the only path. */
6966 : 948 : msg_fn = _nvme_ctrlr_destruct;
6967 : 948 : rc = bdev_nvme_delete_ctrlr_unsafe(nvme_ctrlr, false);
6968 : 59 : } else {
6969 : : /* There is an alternative path. */
6970 : 8 : msg_fn = _bdev_nvme_reset_ctrlr;
6971 : 8 : rc = bdev_nvme_failover_ctrlr_unsafe(nvme_ctrlr, true);
6972 : : }
6973 : :
6974 [ - + # # ]: 956 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
6975 : :
6976 [ + + ]: 956 : if (rc == 0) {
6977 [ # # # # ]: 956 : spdk_thread_send_msg(nvme_ctrlr->thread, msg_fn, nvme_ctrlr);
6978 [ # # ]: 60 : } else if (rc == -EALREADY) {
6979 : 0 : rc = 0;
6980 : 0 : }
6981 : :
6982 : 956 : return rc;
6983 : 68 : }
6984 : :
6985 : : int
6986 : 932 : bdev_nvme_delete(const char *name, const struct nvme_path_id *path_id,
6987 : : bdev_nvme_delete_done_fn delete_done, void *delete_done_ctx)
6988 : : {
6989 : : struct nvme_bdev_ctrlr *nbdev_ctrlr;
6990 : : struct nvme_ctrlr *nvme_ctrlr, *tmp_nvme_ctrlr;
6991 : 932 : struct bdev_nvme_delete_ctx *ctx = NULL;
6992 : 932 : int rc = -ENXIO, _rc;
6993 : :
6994 [ + - - + ]: 932 : if (name == NULL || path_id == NULL) {
6995 : 0 : rc = -EINVAL;
6996 : 0 : goto exit;
6997 : : }
6998 : :
6999 [ - + ]: 932 : pthread_mutex_lock(&g_bdev_nvme_mutex);
7000 : :
7001 : 932 : nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
7002 [ + + ]: 932 : if (nbdev_ctrlr == NULL) {
7003 [ # # ]: 0 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
7004 : :
7005 : 0 : SPDK_ERRLOG("Failed to find NVMe bdev controller\n");
7006 : 0 : rc = -ENODEV;
7007 : 0 : goto exit;
7008 : : }
7009 : :
7010 [ + + + + : 1931 : TAILQ_FOREACH_SAFE(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq, tmp_nvme_ctrlr) {
# # # # #
# # # # #
# # ]
7011 : 999 : _rc = _bdev_nvme_delete(nvme_ctrlr, path_id);
7012 [ + + + + ]: 999 : if (_rc < 0 && _rc != -ENXIO) {
7013 [ # # ]: 0 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
7014 : 0 : rc = _rc;
7015 : 0 : goto exit;
7016 [ + + ]: 999 : } else if (_rc == 0) {
7017 : : /* We traverse all remaining nvme_ctrlrs even if one nvme_ctrlr
7018 : : * was deleted successfully. To remember the successful deletion,
7019 : : * overwrite rc only if _rc is zero.
7020 : : */
7021 : 968 : rc = 0;
7022 : 62 : }
7023 : 68 : }
7024 : :
7025 [ - + ]: 932 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
7026 : :
7027 [ + + + + ]: 932 : if (rc != 0 || delete_done == NULL) {
7028 : 230 : goto exit;
7029 : : }
7030 : :
7031 : 702 : ctx = calloc(1, sizeof(*ctx));
7032 [ + + ]: 702 : if (ctx == NULL) {
7033 : 0 : SPDK_ERRLOG("Failed to allocate context for bdev_nvme_delete\n");
7034 : 0 : rc = -ENOMEM;
7035 : 0 : goto exit;
7036 : : }
7037 : :
7038 [ - + # # : 702 : ctx->name = strdup(name);
# # ]
7039 [ + + # # : 702 : if (ctx->name == NULL) {
# # ]
7040 : 0 : SPDK_ERRLOG("Failed to copy controller name for deletion\n");
7041 : 0 : rc = -ENOMEM;
7042 : 0 : goto exit;
7043 : : }
7044 : :
7045 [ # # # # ]: 702 : ctx->delete_done = delete_done;
7046 [ # # # # ]: 702 : ctx->delete_done_ctx = delete_done_ctx;
7047 [ # # ]: 702 : ctx->path_id = *path_id;
7048 [ # # # # ]: 702 : ctx->timeout_ticks = spdk_get_ticks() + 10 * spdk_get_ticks_hz();
7049 [ # # # # ]: 702 : ctx->poller = SPDK_POLLER_REGISTER(bdev_nvme_delete_complete_poll, ctx, 1000);
7050 [ + + # # : 702 : if (ctx->poller == NULL) {
# # ]
7051 : 0 : SPDK_ERRLOG("Failed to register bdev_nvme_delete poller\n");
7052 : 0 : rc = -ENOMEM;
7053 : 0 : goto exit;
7054 : : }
7055 : :
7056 : 698 : exit:
7057 [ + + ]: 932 : if (rc != 0) {
7058 : 4 : free_bdev_nvme_delete_ctx(ctx);
7059 : 1 : }
7060 : :
7061 : 932 : return rc;
7062 : : }
7063 : :
7064 : : #define DISCOVERY_INFOLOG(ctx, format, ...) \
7065 : : SPDK_INFOLOG(bdev_nvme, "Discovery[%s:%s] " format, ctx->trid.traddr, ctx->trid.trsvcid, ##__VA_ARGS__);
7066 : :
7067 : : #define DISCOVERY_ERRLOG(ctx, format, ...) \
7068 : : SPDK_ERRLOG("Discovery[%s:%s] " format, ctx->trid.traddr, ctx->trid.trsvcid, ##__VA_ARGS__);
7069 : :
7070 : : struct discovery_entry_ctx {
7071 : : char name[128];
7072 : : struct spdk_nvme_transport_id trid;
7073 : : struct spdk_nvme_ctrlr_opts drv_opts;
7074 : : struct spdk_nvmf_discovery_log_page_entry entry;
7075 : : TAILQ_ENTRY(discovery_entry_ctx) tailq;
7076 : : struct discovery_ctx *ctx;
7077 : : };
7078 : :
7079 : : struct discovery_ctx {
7080 : : char *name;
7081 : : spdk_bdev_nvme_start_discovery_fn start_cb_fn;
7082 : : spdk_bdev_nvme_stop_discovery_fn stop_cb_fn;
7083 : : void *cb_ctx;
7084 : : struct spdk_nvme_probe_ctx *probe_ctx;
7085 : : struct spdk_nvme_detach_ctx *detach_ctx;
7086 : : struct spdk_nvme_ctrlr *ctrlr;
7087 : : struct spdk_nvme_transport_id trid;
7088 : : struct discovery_entry_ctx *entry_ctx_in_use;
7089 : : struct spdk_poller *poller;
7090 : : struct spdk_nvme_ctrlr_opts drv_opts;
7091 : : struct spdk_bdev_nvme_ctrlr_opts bdev_opts;
7092 : : struct spdk_nvmf_discovery_log_page *log_page;
7093 : : TAILQ_ENTRY(discovery_ctx) tailq;
7094 : : TAILQ_HEAD(, discovery_entry_ctx) nvm_entry_ctxs;
7095 : : TAILQ_HEAD(, discovery_entry_ctx) discovery_entry_ctxs;
7096 : : int rc;
7097 : : bool wait_for_attach;
7098 : : uint64_t timeout_ticks;
7099 : : /* Denotes that the discovery service is being started. We're waiting
7100 : : * for the initial connection to the discovery controller to be
7101 : : * established and attach discovered NVM ctrlrs.
7102 : : */
7103 : : bool initializing;
7104 : : /* Denotes if a discovery is currently in progress for this context.
7105 : : * That includes connecting to newly discovered subsystems. Used to
7106 : : * ensure we do not start a new discovery until an existing one is
7107 : : * complete.
7108 : : */
7109 : : bool in_progress;
7110 : :
7111 : : /* Denotes if another discovery is needed after the one in progress
7112 : : * completes. Set when we receive an AER completion while a discovery
7113 : : * is already in progress.
7114 : : */
7115 : : bool pending;
7116 : :
7117 : : /* Signal to the discovery context poller that it should stop the
7118 : : * discovery service, including detaching from the current discovery
7119 : : * controller.
7120 : : */
7121 : : bool stop;
7122 : :
7123 : : struct spdk_thread *calling_thread;
7124 : : uint32_t index;
7125 : : uint32_t attach_in_progress;
7126 : : char *hostnqn;
7127 : :
7128 : : /* Denotes if the discovery service was started by the mdns discovery.
7129 : : */
7130 : : bool from_mdns_discovery_service;
7131 : : };
7132 : :
7133 : : TAILQ_HEAD(discovery_ctxs, discovery_ctx);
7134 : : static struct discovery_ctxs g_discovery_ctxs = TAILQ_HEAD_INITIALIZER(g_discovery_ctxs);
7135 : :
7136 : : static void get_discovery_log_page(struct discovery_ctx *ctx);
7137 : :
7138 : : static void
7139 : 34 : free_discovery_ctx(struct discovery_ctx *ctx)
7140 : : {
7141 [ # # # # ]: 34 : free(ctx->log_page);
7142 [ # # # # ]: 34 : free(ctx->hostnqn);
7143 [ # # # # ]: 34 : free(ctx->name);
7144 : 34 : free(ctx);
7145 : 34 : }
7146 : :
7147 : : static void
7148 : 49 : discovery_complete(struct discovery_ctx *ctx)
7149 : : {
7150 [ # # # # ]: 49 : ctx->initializing = false;
7151 [ # # # # ]: 49 : ctx->in_progress = false;
7152 [ - + + + : 49 : if (ctx->pending) {
# # # # ]
7153 [ # # # # ]: 5 : ctx->pending = false;
7154 : 5 : get_discovery_log_page(ctx);
7155 : 0 : }
7156 : 49 : }
7157 : :
7158 : : static void
7159 : 161 : build_trid_from_log_page_entry(struct spdk_nvme_transport_id *trid,
7160 : : struct spdk_nvmf_discovery_log_page_entry *entry)
7161 : : {
7162 : : char *space;
7163 : :
7164 [ # # # # : 161 : trid->trtype = entry->trtype;
# # # # ]
7165 [ # # # # : 161 : trid->adrfam = entry->adrfam;
# # # # ]
7166 [ - + - + : 161 : memcpy(trid->traddr, entry->traddr, sizeof(entry->traddr));
# # # # ]
7167 [ # # # # : 161 : memcpy(trid->trsvcid, entry->trsvcid, sizeof(entry->trsvcid));
# # # # ]
7168 : : /* Because the source buffer (entry->subnqn) is longer than trid->subnqn, and
7169 : : * before call to this function trid->subnqn is zeroed out, we need
7170 : : * to copy sizeof(trid->subnqn) minus one byte to make sure the last character
7171 : : * remains 0. Then we can shorten the string (replace ' ' with 0) if required
7172 : : */
7173 [ - + - + : 161 : memcpy(trid->subnqn, entry->subnqn, sizeof(trid->subnqn) - 1);
# # # # ]
7174 : :
7175 : : /* We want the traddr, trsvcid and subnqn fields to be NULL-terminated.
7176 : : * But the log page entries typically pad them with spaces, not zeroes.
7177 : : * So add a NULL terminator to each of these fields at the appropriate
7178 : : * location.
7179 : : */
7180 [ - + # # ]: 161 : space = strchr(trid->traddr, ' ');
7181 [ + - ]: 161 : if (space) {
7182 [ # # ]: 161 : *space = 0;
7183 : 0 : }
7184 [ - + # # ]: 161 : space = strchr(trid->trsvcid, ' ');
7185 [ + - ]: 161 : if (space) {
7186 [ # # ]: 161 : *space = 0;
7187 : 0 : }
7188 [ - + # # ]: 161 : space = strchr(trid->subnqn, ' ');
7189 [ - + ]: 161 : if (space) {
7190 [ # # ]: 0 : *space = 0;
7191 : 0 : }
7192 : 161 : }
7193 : :
7194 : : static void
7195 : 34 : _stop_discovery(void *_ctx)
7196 : : {
7197 : 34 : struct discovery_ctx *ctx = _ctx;
7198 : :
7199 [ - + # # : 34 : if (ctx->attach_in_progress > 0) {
# # ]
7200 : 0 : spdk_thread_send_msg(spdk_get_thread(), _stop_discovery, ctx);
7201 : 0 : return;
7202 : : }
7203 : :
7204 [ # # # # ]: 34 : ctx->stop = true;
7205 : :
7206 [ + + # # : 63 : while (!TAILQ_EMPTY(&ctx->nvm_entry_ctxs)) {
# # # # ]
7207 : : struct discovery_entry_ctx *entry_ctx;
7208 : 29 : struct nvme_path_id path = {};
7209 : :
7210 [ # # # # : 29 : entry_ctx = TAILQ_FIRST(&ctx->nvm_entry_ctxs);
# # ]
7211 [ # # ]: 29 : path.trid = entry_ctx->trid;
7212 [ # # ]: 29 : bdev_nvme_delete(entry_ctx->name, &path, NULL, NULL);
7213 [ - + # # : 29 : TAILQ_REMOVE(&ctx->nvm_entry_ctxs, entry_ctx, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
7214 : 29 : free(entry_ctx);
7215 : : }
7216 : :
7217 [ + + # # : 77 : while (!TAILQ_EMPTY(&ctx->discovery_entry_ctxs)) {
# # # # ]
7218 : : struct discovery_entry_ctx *entry_ctx;
7219 : :
7220 [ # # # # : 43 : entry_ctx = TAILQ_FIRST(&ctx->discovery_entry_ctxs);
# # ]
7221 [ + + # # : 43 : TAILQ_REMOVE(&ctx->discovery_entry_ctxs, entry_ctx, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
7222 : 43 : free(entry_ctx);
7223 : : }
7224 : :
7225 [ # # # # ]: 34 : free(ctx->entry_ctx_in_use);
7226 [ # # # # ]: 34 : ctx->entry_ctx_in_use = NULL;
7227 : 0 : }
7228 : :
7229 : : static void
7230 : 34 : stop_discovery(struct discovery_ctx *ctx, spdk_bdev_nvme_stop_discovery_fn cb_fn, void *cb_ctx)
7231 : : {
7232 [ # # # # ]: 34 : ctx->stop_cb_fn = cb_fn;
7233 [ # # # # ]: 34 : ctx->cb_ctx = cb_ctx;
7234 : :
7235 [ - + # # : 34 : if (ctx->attach_in_progress > 0) {
# # ]
7236 [ # # # # : 0 : DISCOVERY_INFOLOG(ctx, "stopping discovery with attach_in_progress: %"PRIu32"\n",
# # # # #
# # # # #
# # # # ]
7237 : : ctx->attach_in_progress);
7238 : 0 : }
7239 : :
7240 : 34 : _stop_discovery(ctx);
7241 : 34 : }
7242 : :
7243 : : static void
7244 : 31 : remove_discovery_entry(struct nvme_ctrlr *nvme_ctrlr)
7245 : : {
7246 : : struct discovery_ctx *d_ctx;
7247 : : struct nvme_path_id *path_id;
7248 : 31 : struct spdk_nvme_transport_id trid = {};
7249 : : struct discovery_entry_ctx *entry_ctx, *tmp;
7250 : :
7251 [ # # # # : 31 : path_id = TAILQ_FIRST(&nvme_ctrlr->trids);
# # ]
7252 : :
7253 [ + + # # : 34 : TAILQ_FOREACH(d_ctx, &g_discovery_ctxs, tailq) {
# # # # ]
7254 [ + + # # : 6 : TAILQ_FOREACH_SAFE(entry_ctx, &d_ctx->nvm_entry_ctxs, tailq, tmp) {
# # # # #
# # # # #
# # ]
7255 [ # # ]: 3 : build_trid_from_log_page_entry(&trid, &entry_ctx->entry);
7256 [ - + # # ]: 3 : if (spdk_nvme_transport_id_compare(&trid, &path_id->trid) != 0) {
7257 : 0 : continue;
7258 : : }
7259 : :
7260 [ - + # # : 3 : TAILQ_REMOVE(&d_ctx->nvm_entry_ctxs, entry_ctx, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
7261 : 3 : free(entry_ctx);
7262 [ - + + - : 3 : DISCOVERY_INFOLOG(d_ctx, "Remove discovery entry: %s:%s:%s\n",
# # # # #
# # # #
# ]
7263 : : trid.subnqn, trid.traddr, trid.trsvcid);
7264 : :
7265 : : /* Fail discovery ctrlr to force reattach attempt */
7266 [ # # # # ]: 3 : spdk_nvme_ctrlr_fail(d_ctx->ctrlr);
7267 : 0 : }
7268 : 0 : }
7269 : 31 : }
7270 : :
7271 : : static void
7272 : 49 : discovery_remove_controllers(struct discovery_ctx *ctx)
7273 : : {
7274 [ # # # # ]: 49 : struct spdk_nvmf_discovery_log_page *log_page = ctx->log_page;
7275 : : struct discovery_entry_ctx *entry_ctx, *tmp;
7276 : : struct spdk_nvmf_discovery_log_page_entry *new_entry, *old_entry;
7277 : 49 : struct spdk_nvme_transport_id old_trid = {};
7278 : : uint64_t numrec, i;
7279 : : bool found;
7280 : :
7281 [ # # ]: 49 : numrec = from_le64(&log_page->numrec);
7282 [ + + # # : 109 : TAILQ_FOREACH_SAFE(entry_ctx, &ctx->nvm_entry_ctxs, tailq, tmp) {
# # # # #
# # # # #
# # ]
7283 : 60 : found = false;
7284 [ # # ]: 60 : old_entry = &entry_ctx->entry;
7285 : 60 : build_trid_from_log_page_entry(&old_trid, old_entry);
7286 [ + + ]: 143 : for (i = 0; i < numrec; i++) {
7287 [ # # # # ]: 138 : new_entry = &log_page->entries[i];
7288 [ - + - + : 138 : if (!memcmp(old_entry, new_entry, sizeof(*old_entry))) {
+ + ]
7289 [ - + + + : 55 : DISCOVERY_INFOLOG(ctx, "NVM %s:%s:%s found again\n",
# # # # #
# # # #
# ]
7290 : : old_trid.subnqn, old_trid.traddr, old_trid.trsvcid);
7291 : 55 : found = true;
7292 : 55 : break;
7293 : : }
7294 : 0 : }
7295 [ + + # # ]: 60 : if (!found) {
7296 : 5 : struct nvme_path_id path = {};
7297 : :
7298 [ - + + - : 5 : DISCOVERY_INFOLOG(ctx, "NVM %s:%s:%s not found\n",
# # # # #
# # # #
# ]
7299 : : old_trid.subnqn, old_trid.traddr, old_trid.trsvcid);
7300 : :
7301 [ # # ]: 5 : path.trid = entry_ctx->trid;
7302 [ # # ]: 5 : bdev_nvme_delete(entry_ctx->name, &path, NULL, NULL);
7303 [ + - # # : 5 : TAILQ_REMOVE(&ctx->nvm_entry_ctxs, entry_ctx, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
7304 : 5 : free(entry_ctx);
7305 : 0 : }
7306 : 0 : }
7307 : 49 : free(log_page);
7308 [ # # # # ]: 49 : ctx->log_page = NULL;
7309 : 49 : discovery_complete(ctx);
7310 : 49 : }
7311 : :
7312 : : static void
7313 : 42 : complete_discovery_start(struct discovery_ctx *ctx, int status)
7314 : : {
7315 [ # # # # ]: 42 : ctx->timeout_ticks = 0;
7316 [ # # # # ]: 42 : ctx->rc = status;
7317 [ + + # # : 42 : if (ctx->start_cb_fn) {
# # ]
7318 [ # # # # : 27 : ctx->start_cb_fn(ctx->cb_ctx, status);
# # # # #
# # # ]
7319 [ # # # # ]: 27 : ctx->start_cb_fn = NULL;
7320 [ # # # # ]: 27 : ctx->cb_ctx = NULL;
7321 : 0 : }
7322 : 42 : }
7323 : :
7324 : : static void
7325 : 37 : discovery_attach_controller_done(void *cb_ctx, size_t bdev_count, int rc)
7326 : : {
7327 : 37 : struct discovery_entry_ctx *entry_ctx = cb_ctx;
7328 [ # # # # ]: 37 : struct discovery_ctx *ctx = entry_ctx->ctx;
7329 : :
7330 [ - + + + : 37 : DISCOVERY_INFOLOG(ctx, "attach %s done\n", entry_ctx->name);
# # # # #
# # # # #
# # ]
7331 [ # # ]: 37 : ctx->attach_in_progress--;
7332 [ + - # # : 37 : if (ctx->attach_in_progress == 0) {
# # ]
7333 [ # # # # ]: 37 : complete_discovery_start(ctx, ctx->rc);
7334 [ - + + + : 37 : if (ctx->initializing && ctx->rc != 0) {
- + # # #
# # # #
# ]
7335 [ # # # # : 0 : DISCOVERY_ERRLOG(ctx, "stopping discovery due to errors: %d\n", ctx->rc);
# # # # #
# # # ]
7336 [ # # # # ]: 0 : stop_discovery(ctx, NULL, ctx->cb_ctx);
7337 : 0 : } else {
7338 : 37 : discovery_remove_controllers(ctx);
7339 : : }
7340 : 0 : }
7341 : 37 : }
7342 : :
7343 : : static struct discovery_entry_ctx *
7344 : 95 : create_discovery_entry_ctx(struct discovery_ctx *ctx, struct spdk_nvme_transport_id *trid)
7345 : : {
7346 : : struct discovery_entry_ctx *new_ctx;
7347 : :
7348 : 95 : new_ctx = calloc(1, sizeof(*new_ctx));
7349 [ - + ]: 95 : if (new_ctx == NULL) {
7350 [ # # # # : 0 : DISCOVERY_ERRLOG(ctx, "could not allocate new entry_ctx\n");
# # # # ]
7351 : 0 : return NULL;
7352 : : }
7353 : :
7354 [ # # # # ]: 95 : new_ctx->ctx = ctx;
7355 [ - + - + : 95 : memcpy(&new_ctx->trid, trid, sizeof(*trid));
# # ]
7356 [ # # ]: 95 : spdk_nvme_ctrlr_get_default_ctrlr_opts(&new_ctx->drv_opts, sizeof(new_ctx->drv_opts));
7357 [ - + # # : 95 : snprintf(new_ctx->drv_opts.hostnqn, sizeof(new_ctx->drv_opts.hostnqn), "%s", ctx->hostnqn);
# # # # ]
7358 : 95 : return new_ctx;
7359 : 0 : }
7360 : :
7361 : : static void
7362 : 49 : discovery_log_page_cb(void *cb_arg, int rc, const struct spdk_nvme_cpl *cpl,
7363 : : struct spdk_nvmf_discovery_log_page *log_page)
7364 : : {
7365 : 49 : struct discovery_ctx *ctx = cb_arg;
7366 : : struct discovery_entry_ctx *entry_ctx, *tmp;
7367 : : struct spdk_nvmf_discovery_log_page_entry *new_entry, *old_entry;
7368 : : uint64_t numrec, i;
7369 : : bool found;
7370 : :
7371 [ + - + - : 49 : if (rc || spdk_nvme_cpl_is_error(cpl)) {
- + # # #
# # # # #
# # # # ]
7372 [ # # # # : 0 : DISCOVERY_ERRLOG(ctx, "could not get discovery log page\n");
# # # # ]
7373 : 0 : return;
7374 : : }
7375 : :
7376 [ # # # # ]: 49 : ctx->log_page = log_page;
7377 [ - + # # : 49 : assert(ctx->attach_in_progress == 0);
# # # # ]
7378 [ # # ]: 49 : numrec = from_le64(&log_page->numrec);
7379 [ + + # # : 71 : TAILQ_FOREACH_SAFE(entry_ctx, &ctx->discovery_entry_ctxs, tailq, tmp) {
# # # # #
# # # # #
# # ]
7380 [ + + # # : 22 : TAILQ_REMOVE(&ctx->discovery_entry_ctxs, entry_ctx, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
7381 : 22 : free(entry_ctx);
7382 : 0 : }
7383 [ + + ]: 165 : for (i = 0; i < numrec; i++) {
7384 : 116 : found = false;
7385 [ # # # # ]: 116 : new_entry = &log_page->entries[i];
7386 [ + + # # : 116 : if (new_entry->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY_CURRENT ||
# # # # ]
7387 [ - + # # ]: 55 : new_entry->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
7388 : : struct discovery_entry_ctx *new_ctx;
7389 : 61 : struct spdk_nvme_transport_id trid = {};
7390 : :
7391 : 61 : build_trid_from_log_page_entry(&trid, new_entry);
7392 : 61 : new_ctx = create_discovery_entry_ctx(ctx, &trid);
7393 [ - + ]: 61 : if (new_ctx == NULL) {
7394 [ # # # # : 0 : DISCOVERY_ERRLOG(ctx, "could not allocate new entry_ctx\n");
# # # # ]
7395 : 0 : break;
7396 : : }
7397 : :
7398 [ # # # # : 61 : TAILQ_INSERT_TAIL(&ctx->discovery_entry_ctxs, new_ctx, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
7399 : 61 : continue;
7400 : : }
7401 [ + + # # : 67 : TAILQ_FOREACH(entry_ctx, &ctx->nvm_entry_ctxs, tailq) {
# # # # #
# # # #
# ]
7402 [ # # ]: 30 : old_entry = &entry_ctx->entry;
7403 [ - + - + : 30 : if (!memcmp(new_entry, old_entry, sizeof(*new_entry))) {
+ + ]
7404 : 18 : found = true;
7405 : 18 : break;
7406 : : }
7407 : 0 : }
7408 [ + + # # ]: 55 : if (!found) {
7409 : 37 : struct discovery_entry_ctx *subnqn_ctx = NULL, *new_ctx;
7410 : : struct discovery_ctx *d_ctx;
7411 : :
7412 [ + + # # : 77 : TAILQ_FOREACH(d_ctx, &g_discovery_ctxs, tailq) {
# # # # ]
7413 [ + + # # : 51 : TAILQ_FOREACH(subnqn_ctx, &d_ctx->nvm_entry_ctxs, tailq) {
# # # # #
# # # #
# ]
7414 [ - + - + : 11 : if (!memcmp(subnqn_ctx->entry.subnqn, new_entry->subnqn,
+ + # # #
# # # ]
7415 : : sizeof(new_entry->subnqn))) {
7416 : 5 : break;
7417 : : }
7418 : 0 : }
7419 [ + + ]: 45 : if (subnqn_ctx) {
7420 : 5 : break;
7421 : : }
7422 : 0 : }
7423 : :
7424 : 37 : new_ctx = calloc(1, sizeof(*new_ctx));
7425 [ - + ]: 37 : if (new_ctx == NULL) {
7426 [ # # # # : 0 : DISCOVERY_ERRLOG(ctx, "could not allocate new entry_ctx\n");
# # # # ]
7427 : 0 : break;
7428 : : }
7429 : :
7430 [ # # # # ]: 37 : new_ctx->ctx = ctx;
7431 [ - + - + : 37 : memcpy(&new_ctx->entry, new_entry, sizeof(*new_entry));
# # ]
7432 [ # # ]: 37 : build_trid_from_log_page_entry(&new_ctx->trid, new_entry);
7433 [ + + ]: 37 : if (subnqn_ctx) {
7434 [ # # # # ]: 5 : snprintf(new_ctx->name, sizeof(new_ctx->name), "%s", subnqn_ctx->name);
7435 [ - + + - : 5 : DISCOVERY_INFOLOG(ctx, "NVM %s:%s:%s new path for %s\n",
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
7436 : : new_ctx->trid.subnqn, new_ctx->trid.traddr, new_ctx->trid.trsvcid,
7437 : : new_ctx->name);
7438 : 0 : } else {
7439 [ # # # # : 32 : snprintf(new_ctx->name, sizeof(new_ctx->name), "%s%d", ctx->name, ctx->index++);
# # # # ]
7440 [ - + + + : 32 : DISCOVERY_INFOLOG(ctx, "NVM %s:%s:%s new subsystem %s\n",
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
7441 : : new_ctx->trid.subnqn, new_ctx->trid.traddr, new_ctx->trid.trsvcid,
7442 : : new_ctx->name);
7443 : : }
7444 [ # # ]: 37 : spdk_nvme_ctrlr_get_default_ctrlr_opts(&new_ctx->drv_opts, sizeof(new_ctx->drv_opts));
7445 [ # # # # : 37 : snprintf(new_ctx->drv_opts.hostnqn, sizeof(new_ctx->drv_opts.hostnqn), "%s", ctx->hostnqn);
# # # # ]
7446 [ # # # # ]: 37 : rc = spdk_bdev_nvme_create(&new_ctx->trid, new_ctx->name, NULL, 0,
7447 : 0 : discovery_attach_controller_done, new_ctx,
7448 [ # # # # ]: 0 : &new_ctx->drv_opts, &ctx->bdev_opts);
7449 [ + - ]: 37 : if (rc == 0) {
7450 [ # # # # : 37 : TAILQ_INSERT_TAIL(&ctx->nvm_entry_ctxs, new_ctx, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
7451 [ # # ]: 37 : ctx->attach_in_progress++;
7452 : 0 : } else {
7453 [ # # # # : 0 : DISCOVERY_ERRLOG(ctx, "spdk_bdev_nvme_create failed (%s)\n", spdk_strerror(-rc));
# # # # #
# ]
7454 : : }
7455 : 0 : }
7456 : 0 : }
7457 : :
7458 [ + + # # : 49 : if (ctx->attach_in_progress == 0) {
# # ]
7459 : 12 : discovery_remove_controllers(ctx);
7460 : 0 : }
7461 : 0 : }
7462 : :
7463 : : static void
7464 : 49 : get_discovery_log_page(struct discovery_ctx *ctx)
7465 : : {
7466 : : int rc;
7467 : :
7468 [ - + - + : 49 : assert(ctx->in_progress == false);
# # # # #
# ]
7469 [ # # # # ]: 49 : ctx->in_progress = true;
7470 [ # # # # ]: 49 : rc = spdk_nvme_ctrlr_get_discovery_log_page(ctx->ctrlr, discovery_log_page_cb, ctx);
7471 [ - + ]: 49 : if (rc != 0) {
7472 [ # # # # : 0 : DISCOVERY_ERRLOG(ctx, "could not get discovery log page\n");
# # # # ]
7473 : 0 : }
7474 [ - + + + : 49 : DISCOVERY_INFOLOG(ctx, "sent discovery log page command\n");
# # # # #
# # # #
# ]
7475 : 49 : }
7476 : :
7477 : : static void
7478 : 16 : discovery_aer_cb(void *arg, const struct spdk_nvme_cpl *cpl)
7479 : : {
7480 : 16 : struct discovery_ctx *ctx = arg;
7481 [ # # # # : 16 : uint32_t log_page_id = (cpl->cdw0 & 0xFF0000) >> 16;
# # ]
7482 : :
7483 [ + - - + : 16 : if (spdk_nvme_cpl_is_error(cpl)) {
# # # # #
# # # # #
# # ]
7484 [ # # # # : 0 : DISCOVERY_ERRLOG(ctx, "aer failed\n");
# # # # ]
7485 : 0 : return;
7486 : : }
7487 : :
7488 [ - + ]: 16 : if (log_page_id != SPDK_NVME_LOG_DISCOVERY) {
7489 [ # # # # : 0 : DISCOVERY_ERRLOG(ctx, "unexpected log page 0x%x\n", log_page_id);
# # # # ]
7490 : 0 : return;
7491 : : }
7492 : :
7493 [ - + + - : 16 : DISCOVERY_INFOLOG(ctx, "got aer\n");
# # # # #
# # # #
# ]
7494 [ - + + + : 16 : if (ctx->in_progress) {
# # # # ]
7495 [ # # # # ]: 5 : ctx->pending = true;
7496 : 5 : return;
7497 : : }
7498 : :
7499 : 11 : get_discovery_log_page(ctx);
7500 : 0 : }
7501 : :
7502 : : static void
7503 : 33 : discovery_attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
7504 : : struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts)
7505 : : {
7506 : 33 : struct spdk_nvme_ctrlr_opts *user_opts = cb_ctx;
7507 : : struct discovery_ctx *ctx;
7508 : :
7509 : 33 : ctx = SPDK_CONTAINEROF(user_opts, struct discovery_ctx, drv_opts);
7510 : :
7511 [ - + + + : 33 : DISCOVERY_INFOLOG(ctx, "discovery ctrlr attached\n");
# # # # #
# # # #
# ]
7512 [ # # # # ]: 33 : ctx->probe_ctx = NULL;
7513 [ # # # # ]: 33 : ctx->ctrlr = ctrlr;
7514 : :
7515 [ - + # # : 33 : if (ctx->rc != 0) {
# # ]
7516 [ # # # # : 0 : DISCOVERY_ERRLOG(ctx, "encountered error while attaching discovery ctrlr: %d\n",
# # # # #
# # # ]
7517 : : ctx->rc);
7518 : 0 : return;
7519 : : }
7520 : :
7521 [ # # # # ]: 33 : spdk_nvme_ctrlr_register_aer_callback(ctx->ctrlr, discovery_aer_cb, ctx);
7522 : 0 : }
7523 : :
7524 : : static int
7525 : 114140 : discovery_poller(void *arg)
7526 : : {
7527 : 114140 : struct discovery_ctx *ctx = arg;
7528 : : struct spdk_nvme_transport_id *trid;
7529 : : int rc;
7530 : :
7531 [ + + # # : 114140 : if (ctx->detach_ctx) {
# # ]
7532 [ # # # # ]: 243 : rc = spdk_nvme_detach_poll_async(ctx->detach_ctx);
7533 [ + + ]: 243 : if (rc != -EAGAIN) {
7534 [ # # # # ]: 33 : ctx->detach_ctx = NULL;
7535 [ # # # # ]: 33 : ctx->ctrlr = NULL;
7536 : 0 : }
7537 [ - + + + : 113897 : } else if (ctx->stop) {
# # # # ]
7538 [ + + # # : 60 : if (ctx->ctrlr != NULL) {
# # ]
7539 [ # # # # : 30 : rc = spdk_nvme_detach_async(ctx->ctrlr, &ctx->detach_ctx);
# # ]
7540 [ + - ]: 30 : if (rc == 0) {
7541 : 30 : return SPDK_POLLER_BUSY;
7542 : : }
7543 [ # # # # : 0 : DISCOVERY_ERRLOG(ctx, "could not detach discovery ctrlr\n");
# # # # ]
7544 : 0 : }
7545 [ # # ]: 30 : spdk_poller_unregister(&ctx->poller);
7546 [ + + # # : 30 : TAILQ_REMOVE(&g_discovery_ctxs, ctx, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
7547 [ - + # # : 30 : assert(ctx->start_cb_fn == NULL);
# # # # ]
7548 [ + + # # : 30 : if (ctx->stop_cb_fn != NULL) {
# # ]
7549 [ # # # # : 25 : ctx->stop_cb_fn(ctx->cb_ctx);
# # # # #
# # # ]
7550 : 0 : }
7551 : 30 : free_discovery_ctx(ctx);
7552 [ + + + + : 113837 : } else if (ctx->probe_ctx == NULL && ctx->ctrlr == NULL) {
# # # # #
# # # ]
7553 [ + + + + : 50 : if (ctx->timeout_ticks != 0 && ctx->timeout_ticks < spdk_get_ticks()) {
# # # # #
# # # ]
7554 [ # # # # : 4 : DISCOVERY_ERRLOG(ctx, "timed out while attaching discovery ctrlr\n");
# # # # ]
7555 [ - + - + : 4 : assert(ctx->initializing);
# # # # #
# ]
7556 [ # # ]: 4 : spdk_poller_unregister(&ctx->poller);
7557 [ - + # # : 4 : TAILQ_REMOVE(&g_discovery_ctxs, ctx, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
7558 : 4 : complete_discovery_start(ctx, -ETIMEDOUT);
7559 : 4 : stop_discovery(ctx, NULL, NULL);
7560 : 4 : free_discovery_ctx(ctx);
7561 : 4 : return SPDK_POLLER_BUSY;
7562 : : }
7563 : :
7564 [ - + # # : 46 : assert(ctx->entry_ctx_in_use == NULL);
# # # # ]
7565 [ # # # # : 46 : ctx->entry_ctx_in_use = TAILQ_FIRST(&ctx->discovery_entry_ctxs);
# # # # #
# ]
7566 [ + + # # : 46 : TAILQ_REMOVE(&ctx->discovery_entry_ctxs, ctx->entry_ctx_in_use, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
7567 [ # # # # : 46 : trid = &ctx->entry_ctx_in_use->trid;
# # ]
7568 : :
7569 : : /* All controllers must be configured explicitely either for multipath or failover.
7570 : : * While discovery use multipath mode, we need to set this in bdev options as well.
7571 : : */
7572 [ # # # # : 46 : ctx->bdev_opts.multipath = true;
# # ]
7573 : :
7574 [ # # # # : 46 : ctx->probe_ctx = spdk_nvme_connect_async(trid, &ctx->drv_opts, discovery_attach_cb);
# # ]
7575 [ + + # # : 46 : if (ctx->probe_ctx) {
# # ]
7576 [ # # ]: 33 : spdk_poller_unregister(&ctx->poller);
7577 [ # # # # ]: 33 : ctx->poller = SPDK_POLLER_REGISTER(discovery_poller, ctx, 1000);
7578 : 0 : } else {
7579 [ # # # # : 13 : DISCOVERY_ERRLOG(ctx, "could not start discovery connect\n");
# # # # ]
7580 [ # # # # : 13 : TAILQ_INSERT_TAIL(&ctx->discovery_entry_ctxs, ctx->entry_ctx_in_use, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
7581 [ # # # # ]: 13 : ctx->entry_ctx_in_use = NULL;
7582 : : }
7583 [ + + # # : 113787 : } else if (ctx->probe_ctx) {
# # ]
7584 [ + + - + : 33 : if (ctx->timeout_ticks != 0 && ctx->timeout_ticks < spdk_get_ticks()) {
# # # # #
# # # ]
7585 [ # # # # : 0 : DISCOVERY_ERRLOG(ctx, "timed out while attaching discovery ctrlr\n");
# # # # ]
7586 : 0 : complete_discovery_start(ctx, -ETIMEDOUT);
7587 : 0 : return SPDK_POLLER_BUSY;
7588 : : }
7589 : :
7590 [ # # # # ]: 33 : rc = spdk_nvme_probe_poll_async(ctx->probe_ctx);
7591 [ + - ]: 33 : if (rc != -EAGAIN) {
7592 [ - + # # : 33 : if (ctx->rc != 0) {
# # ]
7593 [ # # # # : 0 : assert(ctx->initializing);
# # # # #
# ]
7594 [ # # # # ]: 0 : stop_discovery(ctx, NULL, ctx->cb_ctx);
7595 : 0 : } else {
7596 [ - + # # ]: 33 : assert(rc == 0);
7597 [ - + + + : 33 : DISCOVERY_INFOLOG(ctx, "discovery ctrlr connected\n");
# # # # #
# # # #
# ]
7598 [ # # # # ]: 33 : ctx->rc = rc;
7599 : 33 : get_discovery_log_page(ctx);
7600 : : }
7601 : 0 : }
7602 : 0 : } else {
7603 [ + + + + : 113754 : if (ctx->timeout_ticks != 0 && ctx->timeout_ticks < spdk_get_ticks()) {
# # # # #
# # # ]
7604 [ # # # # : 1 : DISCOVERY_ERRLOG(ctx, "timed out while attaching NVM ctrlrs\n");
# # # # ]
7605 : 1 : complete_discovery_start(ctx, -ETIMEDOUT);
7606 : : /* We need to wait until all NVM ctrlrs are attached before we stop the
7607 : : * discovery service to make sure we don't detach a ctrlr that is still
7608 : : * being attached.
7609 : : */
7610 [ + - # # : 1 : if (ctx->attach_in_progress == 0) {
# # ]
7611 [ # # # # ]: 1 : stop_discovery(ctx, NULL, ctx->cb_ctx);
7612 : 1 : return SPDK_POLLER_BUSY;
7613 : : }
7614 : 0 : }
7615 : :
7616 [ # # # # ]: 113753 : rc = spdk_nvme_ctrlr_process_admin_completions(ctx->ctrlr);
7617 [ + + ]: 113753 : if (rc < 0) {
7618 [ # # ]: 3 : spdk_poller_unregister(&ctx->poller);
7619 [ # # # # ]: 3 : ctx->poller = SPDK_POLLER_REGISTER(discovery_poller, ctx, 1000 * 1000);
7620 [ # # # # : 3 : TAILQ_INSERT_TAIL(&ctx->discovery_entry_ctxs, ctx->entry_ctx_in_use, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
7621 [ # # # # ]: 3 : ctx->entry_ctx_in_use = NULL;
7622 : :
7623 [ # # # # : 3 : rc = spdk_nvme_detach_async(ctx->ctrlr, &ctx->detach_ctx);
# # ]
7624 [ - + ]: 3 : if (rc != 0) {
7625 [ # # # # : 0 : DISCOVERY_ERRLOG(ctx, "could not detach discovery ctrlr\n");
# # # # ]
7626 [ # # # # ]: 0 : ctx->ctrlr = NULL;
7627 : 0 : }
7628 : 0 : }
7629 : : }
7630 : :
7631 : 114105 : return SPDK_POLLER_BUSY;
7632 : 0 : }
7633 : :
7634 : : static void
7635 : 34 : start_discovery_poller(void *arg)
7636 : : {
7637 : 34 : struct discovery_ctx *ctx = arg;
7638 : :
7639 [ # # # # : 34 : TAILQ_INSERT_TAIL(&g_discovery_ctxs, ctx, tailq);
# # # # #
# # # # #
# # # # #
# # # #
# ]
7640 [ # # # # ]: 34 : ctx->poller = SPDK_POLLER_REGISTER(discovery_poller, ctx, 1000 * 1000);
7641 : 34 : }
7642 : :
7643 : : int
7644 : 40 : bdev_nvme_start_discovery(struct spdk_nvme_transport_id *trid,
7645 : : const char *base_name,
7646 : : struct spdk_nvme_ctrlr_opts *drv_opts,
7647 : : struct spdk_bdev_nvme_ctrlr_opts *bdev_opts,
7648 : : uint64_t attach_timeout,
7649 : : bool from_mdns,
7650 : : spdk_bdev_nvme_start_discovery_fn cb_fn, void *cb_ctx)
7651 : : {
7652 : : struct discovery_ctx *ctx;
7653 : : struct discovery_entry_ctx *discovery_entry_ctx;
7654 : :
7655 [ # # ]: 40 : snprintf(trid->subnqn, sizeof(trid->subnqn), "%s", SPDK_NVMF_DISCOVERY_NQN);
7656 [ + + # # : 50 : TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
# # # # ]
7657 [ - + - + : 16 : if (strcmp(ctx->name, base_name) == 0) {
+ + # # #
# ]
7658 : 3 : return -EEXIST;
7659 : : }
7660 : :
7661 [ + + # # : 13 : if (ctx->entry_ctx_in_use != NULL) {
# # ]
7662 [ + + # # : 11 : if (!spdk_nvme_transport_id_compare(trid, &ctx->entry_ctx_in_use->trid)) {
# # # # ]
7663 : 3 : return -EEXIST;
7664 : : }
7665 : 0 : }
7666 : :
7667 [ + + # # : 20 : TAILQ_FOREACH(discovery_entry_ctx, &ctx->discovery_entry_ctxs, tailq) {
# # # # #
# # # #
# ]
7668 [ - + # # ]: 10 : if (!spdk_nvme_transport_id_compare(trid, &discovery_entry_ctx->trid)) {
7669 : 0 : return -EEXIST;
7670 : : }
7671 : 0 : }
7672 : 0 : }
7673 : :
7674 : 34 : ctx = calloc(1, sizeof(*ctx));
7675 [ - + ]: 34 : if (ctx == NULL) {
7676 : 0 : return -ENOMEM;
7677 : : }
7678 : :
7679 [ - + # # : 34 : ctx->name = strdup(base_name);
# # ]
7680 [ - + # # : 34 : if (ctx->name == NULL) {
# # ]
7681 : 0 : free_discovery_ctx(ctx);
7682 : 0 : return -ENOMEM;
7683 : : }
7684 [ - + - + : 34 : memcpy(&ctx->drv_opts, drv_opts, sizeof(*drv_opts));
# # ]
7685 [ - + - + : 34 : memcpy(&ctx->bdev_opts, bdev_opts, sizeof(*bdev_opts));
# # ]
7686 [ # # # # : 34 : ctx->from_mdns_discovery_service = from_mdns;
# # ]
7687 [ # # # # : 34 : ctx->bdev_opts.from_discovery_service = true;
# # ]
7688 [ # # # # ]: 34 : ctx->calling_thread = spdk_get_thread();
7689 [ # # # # ]: 34 : ctx->start_cb_fn = cb_fn;
7690 [ # # # # ]: 34 : ctx->cb_ctx = cb_ctx;
7691 [ # # # # ]: 34 : ctx->initializing = true;
7692 [ + + # # : 34 : if (ctx->start_cb_fn) {
# # ]
7693 : : /* We can use this when dumping json to denote if this RPC parameter
7694 : : * was specified or not.
7695 : : */
7696 [ # # # # ]: 27 : ctx->wait_for_attach = true;
7697 : 0 : }
7698 [ + + ]: 34 : if (attach_timeout != 0) {
7699 [ # # # # ]: 42 : ctx->timeout_ticks = spdk_get_ticks() + attach_timeout *
7700 [ # # # # ]: 21 : spdk_get_ticks_hz() / 1000ull;
7701 : 0 : }
7702 [ # # # # : 34 : TAILQ_INIT(&ctx->nvm_entry_ctxs);
# # # # #
# # # # #
# # ]
7703 [ # # # # : 34 : TAILQ_INIT(&ctx->discovery_entry_ctxs);
# # # # #
# # # # #
# # ]
7704 [ - + - + : 34 : memcpy(&ctx->trid, trid, sizeof(*trid));
# # ]
7705 : : /* Even if user did not specify hostnqn, we can still strdup("\0"); */
7706 [ - + # # : 34 : ctx->hostnqn = strdup(ctx->drv_opts.hostnqn);
# # # # #
# ]
7707 [ - + # # : 34 : if (ctx->hostnqn == NULL) {
# # ]
7708 : 0 : free_discovery_ctx(ctx);
7709 : 0 : return -ENOMEM;
7710 : : }
7711 : 34 : discovery_entry_ctx = create_discovery_entry_ctx(ctx, trid);
7712 [ - + ]: 34 : if (discovery_entry_ctx == NULL) {
7713 [ # # # # : 0 : DISCOVERY_ERRLOG(ctx, "could not allocate new entry_ctx\n");
# # # # ]
7714 : 0 : free_discovery_ctx(ctx);
7715 : 0 : return -ENOMEM;
7716 : : }
7717 : :
7718 [ # # # # : 34 : TAILQ_INSERT_TAIL(&ctx->discovery_entry_ctxs, discovery_entry_ctx, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
7719 : 34 : spdk_thread_send_msg(g_bdev_nvme_init_thread, start_discovery_poller, ctx);
7720 : 34 : return 0;
7721 : 0 : }
7722 : :
7723 : : int
7724 : 23 : bdev_nvme_stop_discovery(const char *name, spdk_bdev_nvme_stop_discovery_fn cb_fn, void *cb_ctx)
7725 : : {
7726 : : struct discovery_ctx *ctx;
7727 : :
7728 [ + - # # : 26 : TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
# # # # ]
7729 [ - + - + : 26 : if (strcmp(name, ctx->name) == 0) {
+ + # # #
# ]
7730 [ - + - + : 23 : if (ctx->stop) {
# # # # ]
7731 : 0 : return -EALREADY;
7732 : : }
7733 : : /* If we're still starting the discovery service and ->rc is non-zero, we're
7734 : : * going to stop it as soon as we can
7735 : : */
7736 [ - + - + : 23 : if (ctx->initializing && ctx->rc != 0) {
- - # # #
# # # #
# ]
7737 : 0 : return -EALREADY;
7738 : : }
7739 : 23 : stop_discovery(ctx, cb_fn, cb_ctx);
7740 : 23 : return 0;
7741 : : }
7742 : 0 : }
7743 : :
7744 : 0 : return -ENOENT;
7745 : 0 : }
7746 : :
7747 : : static int
7748 : 1964 : bdev_nvme_library_init(void)
7749 : : {
7750 : 1964 : g_bdev_nvme_init_thread = spdk_get_thread();
7751 : :
7752 : 1964 : spdk_io_device_register(&g_nvme_bdev_ctrlrs, bdev_nvme_create_poll_group_cb,
7753 : : bdev_nvme_destroy_poll_group_cb,
7754 : : sizeof(struct nvme_poll_group), "nvme_poll_groups");
7755 : :
7756 : 1964 : return 0;
7757 : : }
7758 : :
7759 : : static void
7760 : 1964 : bdev_nvme_fini_destruct_ctrlrs(void)
7761 : : {
7762 : : struct nvme_bdev_ctrlr *nbdev_ctrlr;
7763 : : struct nvme_ctrlr *nvme_ctrlr;
7764 : :
7765 [ + + ]: 1964 : pthread_mutex_lock(&g_bdev_nvme_mutex);
7766 [ + + + - : 2683 : TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
+ - + - ]
7767 [ + + + - : 1444 : TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
+ - + + +
- + - +
- ]
7768 [ + + + - ]: 725 : pthread_mutex_lock(&nvme_ctrlr->mutex);
7769 [ + + - + ]: 725 : if (nvme_ctrlr->destruct) {
7770 : : /* This controller's destruction was already started
7771 : : * before the application started shutting down
7772 : : */
7773 [ # # # # ]: 0 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
7774 : 0 : continue;
7775 : : }
7776 [ - + ]: 725 : nvme_ctrlr->destruct = true;
7777 [ - + - + ]: 725 : pthread_mutex_unlock(&nvme_ctrlr->mutex);
7778 : :
7779 [ - + - + ]: 732 : spdk_thread_send_msg(nvme_ctrlr->thread, _nvme_ctrlr_destruct,
7780 : 8 : nvme_ctrlr);
7781 : 8 : }
7782 : 8 : }
7783 : :
7784 : 1964 : g_bdev_nvme_module_finish = true;
7785 [ + + ]: 1964 : if (TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) {
7786 [ - + ]: 1428 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
7787 : 1428 : spdk_io_device_unregister(&g_nvme_bdev_ctrlrs, NULL);
7788 : 1428 : spdk_bdev_module_fini_done();
7789 : 1428 : return;
7790 : : }
7791 : :
7792 [ + + ]: 536 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
7793 : 93 : }
7794 : :
7795 : : static void
7796 : 6 : check_discovery_fini(void *arg)
7797 : : {
7798 [ + - ]: 6 : if (TAILQ_EMPTY(&g_discovery_ctxs)) {
7799 : 6 : bdev_nvme_fini_destruct_ctrlrs();
7800 : 0 : }
7801 : 6 : }
7802 : :
7803 : : static void
7804 : 1964 : bdev_nvme_library_fini(void)
7805 : : {
7806 : : struct nvme_probe_skip_entry *entry, *entry_tmp;
7807 : : struct discovery_ctx *ctx;
7808 : :
7809 : 1964 : spdk_poller_unregister(&g_hotplug_poller);
7810 : 1964 : free(g_hotplug_probe_ctx);
7811 : 1964 : g_hotplug_probe_ctx = NULL;
7812 : :
7813 [ + + + + : 2012 : TAILQ_FOREACH_SAFE(entry, &g_skipped_nvme_ctrlrs, tailq, entry_tmp) {
# # # # -
+ ]
7814 [ + + # # : 48 : TAILQ_REMOVE(&g_skipped_nvme_ctrlrs, entry, tailq);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
7815 : 48 : free(entry);
7816 : 3 : }
7817 : :
7818 [ + + # # ]: 1964 : assert(spdk_get_thread() == g_bdev_nvme_init_thread);
7819 [ + + ]: 1964 : if (TAILQ_EMPTY(&g_discovery_ctxs)) {
7820 : 1958 : bdev_nvme_fini_destruct_ctrlrs();
7821 : 93 : } else {
7822 [ + + # # : 12 : TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
# # # # ]
7823 : 6 : stop_discovery(ctx, check_discovery_fini, NULL);
7824 : 0 : }
7825 : : }
7826 : 1964 : }
7827 : :
7828 : : static void
7829 : 0 : bdev_nvme_verify_pi_error(struct nvme_bdev_io *bio)
7830 : : {
7831 : 0 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
7832 [ # # # # ]: 0 : struct spdk_bdev *bdev = bdev_io->bdev;
7833 : 0 : struct spdk_dif_ctx dif_ctx;
7834 : 0 : struct spdk_dif_error err_blk = {};
7835 : : int rc;
7836 : 0 : struct spdk_dif_ctx_init_ext_opts dif_opts;
7837 : :
7838 : 0 : dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format);
7839 [ # # # # : 0 : dif_opts.dif_pi_format = bdev->dif_pi_format;
# # ]
7840 : 0 : rc = spdk_dif_ctx_init(&dif_ctx,
7841 [ # # # # : 0 : bdev->blocklen, bdev->md_len, bdev->md_interleave,
# # # # #
# # # #
# ]
7842 [ # # # # : 0 : bdev->dif_is_head_of_md, bdev->dif_type,
# # # # #
# ]
7843 [ # # # # : 0 : bdev_io->u.bdev.dif_check_flags,
# # # # ]
7844 [ # # # # : 0 : bdev_io->u.bdev.offset_blocks, 0, 0, 0, 0, &dif_opts);
# # # # ]
7845 [ # # ]: 0 : if (rc != 0) {
7846 : 0 : SPDK_ERRLOG("Initialization of DIF context failed\n");
7847 : 0 : return;
7848 : : }
7849 : :
7850 [ # # # # : 0 : if (bdev->md_interleave) {
# # # # ]
7851 [ # # # # : 0 : rc = spdk_dif_verify(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
# # # # #
# # # # #
# # ]
7852 [ # # # # : 0 : bdev_io->u.bdev.num_blocks, &dif_ctx, &err_blk);
# # # # ]
7853 : 0 : } else {
7854 : 0 : struct iovec md_iov = {
7855 [ # # # # : 0 : .iov_base = bdev_io->u.bdev.md_buf,
# # # # ]
7856 [ # # # # : 0 : .iov_len = bdev_io->u.bdev.num_blocks * bdev->md_len,
# # # # #
# # # ]
7857 : : };
7858 : :
7859 [ # # # # : 0 : rc = spdk_dix_verify(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
# # # # #
# # # # #
# # ]
7860 [ # # # # : 0 : &md_iov, bdev_io->u.bdev.num_blocks, &dif_ctx, &err_blk);
# # # # ]
7861 : : }
7862 : :
7863 [ # # ]: 0 : if (rc != 0) {
7864 [ # # ]: 0 : SPDK_ERRLOG("DIF error detected. type=%d, offset=%" PRIu32 "\n",
7865 : : err_blk.err_type, err_blk.err_offset);
7866 : 0 : } else {
7867 : 0 : SPDK_ERRLOG("Hardware reported PI error but SPDK could not find any.\n");
7868 : : }
7869 : 0 : }
7870 : :
7871 : : static void
7872 : 0 : bdev_nvme_no_pi_readv_done(void *ref, const struct spdk_nvme_cpl *cpl)
7873 : : {
7874 : 0 : struct nvme_bdev_io *bio = ref;
7875 : :
7876 [ # # # # : 0 : if (spdk_nvme_cpl_is_success(cpl)) {
# # # # #
# # # # #
# # ]
7877 : : /* Run PI verification for read data buffer. */
7878 : 0 : bdev_nvme_verify_pi_error(bio);
7879 : 0 : }
7880 : :
7881 : : /* Return original completion status */
7882 [ # # ]: 0 : bdev_nvme_io_complete_nvme_status(bio, &bio->cpl);
7883 : 0 : }
7884 : :
7885 : : static void
7886 : 12727402 : bdev_nvme_readv_done(void *ref, const struct spdk_nvme_cpl *cpl)
7887 : : {
7888 : 12727402 : struct nvme_bdev_io *bio = ref;
7889 : 12727402 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
7890 : : int ret;
7891 : :
7892 [ + + + - : 12727402 : if (spdk_unlikely(spdk_nvme_cpl_is_pi_error(cpl))) {
+ - + - -
- - - # #
# # # # #
# # # # #
# # # # #
# + - ]
7893 [ # # # # : 0 : SPDK_ERRLOG("readv completed with PI error (sct=%d, sc=%d)\n",
# # # # #
# # # ]
7894 : : cpl->status.sct, cpl->status.sc);
7895 : :
7896 : : /* Save completion status to use after verifying PI error. */
7897 [ # # ]: 0 : bio->cpl = *cpl;
7898 : :
7899 [ # # # # : 0 : if (spdk_likely(nvme_io_path_is_available(bio->io_path))) {
# # ]
7900 : : /* Read without PI checking to verify PI error. */
7901 : 0 : ret = bdev_nvme_no_pi_readv(bio,
7902 [ # # # # : 0 : bdev_io->u.bdev.iovs,
# # # # ]
7903 [ # # # # : 0 : bdev_io->u.bdev.iovcnt,
# # # # ]
7904 [ # # # # : 0 : bdev_io->u.bdev.md_buf,
# # # # ]
7905 [ # # # # : 0 : bdev_io->u.bdev.num_blocks,
# # # # ]
7906 [ # # # # : 0 : bdev_io->u.bdev.offset_blocks);
# # # # ]
7907 [ # # ]: 0 : if (ret == 0) {
7908 : 0 : return;
7909 : : }
7910 : 0 : }
7911 : 0 : }
7912 : :
7913 : 12727402 : bdev_nvme_io_complete_nvme_status(bio, cpl);
7914 : 157627 : }
7915 : :
7916 : : static void
7917 : 13279307 : bdev_nvme_writev_done(void *ref, const struct spdk_nvme_cpl *cpl)
7918 : : {
7919 : 13279307 : struct nvme_bdev_io *bio = ref;
7920 : :
7921 [ + + - - : 13279307 : if (spdk_unlikely(spdk_nvme_cpl_is_pi_error(cpl))) {
- - + - -
- - - # #
# # # # #
# # # # #
# # # # #
# # # ]
7922 [ # # # # : 0 : SPDK_ERRLOG("writev completed with PI error (sct=%d, sc=%d)\n",
# # # # #
# # # ]
7923 : : cpl->status.sct, cpl->status.sc);
7924 : : /* Run PI verification for write data buffer if PI error is detected. */
7925 : 0 : bdev_nvme_verify_pi_error(bio);
7926 : 0 : }
7927 : :
7928 : 13279307 : bdev_nvme_io_complete_nvme_status(bio, cpl);
7929 : 13279307 : }
7930 : :
7931 : : static void
7932 : 132625 : bdev_nvme_zone_appendv_done(void *ref, const struct spdk_nvme_cpl *cpl)
7933 : : {
7934 : 132625 : struct nvme_bdev_io *bio = ref;
7935 : 132625 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
7936 : :
7937 : : /* spdk_bdev_io_get_append_location() requires that the ALBA is stored in offset_blocks.
7938 : : * Additionally, offset_blocks has to be set before calling bdev_nvme_verify_pi_error().
7939 : : */
7940 [ # # # # : 132625 : bdev_io->u.bdev.offset_blocks = *(uint64_t *)&cpl->cdw0;
# # # # #
# # # ]
7941 : :
7942 [ - + - - : 132625 : if (spdk_nvme_cpl_is_pi_error(cpl)) {
- - - - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
7943 [ # # # # : 0 : SPDK_ERRLOG("zone append completed with PI error (sct=%d, sc=%d)\n",
# # # # #
# # # ]
7944 : : cpl->status.sct, cpl->status.sc);
7945 : : /* Run PI verification for zone append data buffer if PI error is detected. */
7946 : 0 : bdev_nvme_verify_pi_error(bio);
7947 : 0 : }
7948 : :
7949 : 132625 : bdev_nvme_io_complete_nvme_status(bio, cpl);
7950 : 132625 : }
7951 : :
7952 : : static void
7953 : 56 : bdev_nvme_comparev_done(void *ref, const struct spdk_nvme_cpl *cpl)
7954 : : {
7955 : 56 : struct nvme_bdev_io *bio = ref;
7956 : :
7957 [ + + + - : 56 : if (spdk_nvme_cpl_is_pi_error(cpl)) {
+ - - + #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
7958 [ # # # # : 0 : SPDK_ERRLOG("comparev completed with PI error (sct=%d, sc=%d)\n",
# # # # #
# # # ]
7959 : : cpl->status.sct, cpl->status.sc);
7960 : : /* Run PI verification for compare data buffer if PI error is detected. */
7961 : 0 : bdev_nvme_verify_pi_error(bio);
7962 : 0 : }
7963 : :
7964 : 56 : bdev_nvme_io_complete_nvme_status(bio, cpl);
7965 : 56 : }
7966 : :
7967 : : static void
7968 : 106 : bdev_nvme_comparev_and_writev_done(void *ref, const struct spdk_nvme_cpl *cpl)
7969 : : {
7970 : 106 : struct nvme_bdev_io *bio = ref;
7971 : :
7972 : : /* Compare operation completion */
7973 [ + + + + : 106 : if (!bio->first_fused_completed) {
# # # # ]
7974 : : /* Save compare result for write callback */
7975 [ # # ]: 53 : bio->cpl = *cpl;
7976 [ # # # # ]: 53 : bio->first_fused_completed = true;
7977 : 53 : return;
7978 : : }
7979 : :
7980 : : /* Write operation completion */
7981 [ + + - + : 53 : if (spdk_nvme_cpl_is_error(&bio->cpl)) {
# # # # #
# # # # #
# # # # #
# ]
7982 : : /* If bio->cpl is already an error, it means the compare operation failed. In that case,
7983 : : * complete the IO with the compare operation's status.
7984 : : */
7985 [ + + + - : 40 : if (!spdk_nvme_cpl_is_error(cpl)) {
# # # # #
# # # # #
# # ]
7986 : 4 : SPDK_ERRLOG("Unexpected write success after compare failure.\n");
7987 : 1 : }
7988 : :
7989 [ # # ]: 40 : bdev_nvme_io_complete_nvme_status(bio, &bio->cpl);
7990 : 1 : } else {
7991 : 13 : bdev_nvme_io_complete_nvme_status(bio, cpl);
7992 : : }
7993 : 4 : }
7994 : :
7995 : : static void
7996 : 812374 : bdev_nvme_queued_done(void *ref, const struct spdk_nvme_cpl *cpl)
7997 : : {
7998 : 812374 : struct nvme_bdev_io *bio = ref;
7999 : :
8000 : 812374 : bdev_nvme_io_complete_nvme_status(bio, cpl);
8001 : 812374 : }
8002 : :
8003 : : static int
8004 : 40 : fill_zone_from_report(struct spdk_bdev_zone_info *info, struct spdk_nvme_zns_zone_desc *desc)
8005 : : {
8006 [ + - # # ]: 40 : switch (desc->zt) {
8007 : 40 : case SPDK_NVME_ZONE_TYPE_SEQWR:
8008 [ # # # # ]: 40 : info->type = SPDK_BDEV_ZONE_TYPE_SEQWR;
8009 : 40 : break;
8010 : 0 : default:
8011 [ # # ]: 0 : SPDK_ERRLOG("Invalid zone type: %#x in zone report\n", desc->zt);
8012 : 0 : return -EIO;
8013 : : }
8014 : :
8015 [ + - - - : 40 : switch (desc->zs) {
- - - - #
# ]
8016 : 40 : case SPDK_NVME_ZONE_STATE_EMPTY:
8017 [ # # # # ]: 40 : info->state = SPDK_BDEV_ZONE_STATE_EMPTY;
8018 : 40 : break;
8019 : 0 : case SPDK_NVME_ZONE_STATE_IOPEN:
8020 [ # # # # ]: 0 : info->state = SPDK_BDEV_ZONE_STATE_IMP_OPEN;
8021 : 0 : break;
8022 : 0 : case SPDK_NVME_ZONE_STATE_EOPEN:
8023 [ # # # # ]: 0 : info->state = SPDK_BDEV_ZONE_STATE_EXP_OPEN;
8024 : 0 : break;
8025 : 0 : case SPDK_NVME_ZONE_STATE_CLOSED:
8026 [ # # # # ]: 0 : info->state = SPDK_BDEV_ZONE_STATE_CLOSED;
8027 : 0 : break;
8028 : 0 : case SPDK_NVME_ZONE_STATE_RONLY:
8029 [ # # # # ]: 0 : info->state = SPDK_BDEV_ZONE_STATE_READ_ONLY;
8030 : 0 : break;
8031 : 0 : case SPDK_NVME_ZONE_STATE_FULL:
8032 [ # # # # ]: 0 : info->state = SPDK_BDEV_ZONE_STATE_FULL;
8033 : 0 : break;
8034 : 0 : case SPDK_NVME_ZONE_STATE_OFFLINE:
8035 [ # # # # ]: 0 : info->state = SPDK_BDEV_ZONE_STATE_OFFLINE;
8036 : 0 : break;
8037 : 0 : default:
8038 [ # # ]: 0 : SPDK_ERRLOG("Invalid zone state: %#x in zone report\n", desc->zs);
8039 : 0 : return -EIO;
8040 : : }
8041 : :
8042 [ # # # # : 40 : info->zone_id = desc->zslba;
# # # # ]
8043 [ # # # # : 40 : info->write_pointer = desc->wp;
# # # # ]
8044 [ # # # # : 40 : info->capacity = desc->zcap;
# # # # ]
8045 : :
8046 : 40 : return 0;
8047 : 0 : }
8048 : :
8049 : : static void
8050 : 1 : bdev_nvme_get_zone_info_done(void *ref, const struct spdk_nvme_cpl *cpl)
8051 : : {
8052 : 1 : struct nvme_bdev_io *bio = ref;
8053 : 1 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
8054 [ # # # # : 1 : uint64_t zone_id = bdev_io->u.zone_mgmt.zone_id;
# # # # ]
8055 [ # # # # : 1 : uint32_t zones_to_copy = bdev_io->u.zone_mgmt.num_zones;
# # # # ]
8056 [ # # # # : 1 : struct spdk_bdev_zone_info *info = bdev_io->u.zone_mgmt.buf;
# # # # ]
8057 : : uint64_t max_zones_per_buf, i;
8058 : : uint32_t zone_report_bufsize;
8059 : : struct spdk_nvme_ns *ns;
8060 : : struct spdk_nvme_qpair *qpair;
8061 : : int ret;
8062 : :
8063 [ + - - + : 1 : if (spdk_nvme_cpl_is_error(cpl)) {
# # # # #
# # # # #
# # ]
8064 : 0 : goto out_complete_io_nvme_cpl;
8065 : : }
8066 : :
8067 [ - + # # : 1 : if (spdk_unlikely(!nvme_io_path_is_available(bio->io_path))) {
# # ]
8068 : 0 : ret = -ENXIO;
8069 : 0 : goto out_complete_io_ret;
8070 : : }
8071 : :
8072 [ # # # # : 1 : ns = bio->io_path->nvme_ns->ns;
# # # # #
# # # ]
8073 [ # # # # : 1 : qpair = bio->io_path->qpair->qpair;
# # # # #
# # # ]
8074 : :
8075 : 1 : zone_report_bufsize = spdk_nvme_ns_get_max_io_xfer_size(ns);
8076 [ # # ]: 1 : max_zones_per_buf = (zone_report_bufsize - sizeof(*bio->zone_report_buf)) /
8077 : : sizeof(bio->zone_report_buf->descs[0]);
8078 : :
8079 [ - + # # : 1 : if (bio->zone_report_buf->nr_zones > max_zones_per_buf) {
# # # # #
# ]
8080 : 0 : ret = -EINVAL;
8081 : 0 : goto out_complete_io_ret;
8082 : : }
8083 : :
8084 [ - + # # : 1 : if (!bio->zone_report_buf->nr_zones) {
# # # # #
# ]
8085 : 0 : ret = -EINVAL;
8086 : 0 : goto out_complete_io_ret;
8087 : : }
8088 : :
8089 [ + + + - : 41 : for (i = 0; i < bio->zone_report_buf->nr_zones && bio->handled_zones < zones_to_copy; i++) {
# # # # #
# # # # #
# # ]
8090 [ # # # # : 40 : ret = fill_zone_from_report(&info[bio->handled_zones],
# # ]
8091 [ # # # # : 40 : &bio->zone_report_buf->descs[i]);
# # # # ]
8092 [ - + ]: 40 : if (ret) {
8093 : 0 : goto out_complete_io_ret;
8094 : : }
8095 [ # # ]: 40 : bio->handled_zones++;
8096 : 0 : }
8097 : :
8098 [ - + # # : 1 : if (bio->handled_zones < zones_to_copy) {
# # ]
8099 : 0 : uint64_t zone_size_lba = spdk_nvme_zns_ns_get_zone_size_sectors(ns);
8100 [ # # # # ]: 0 : uint64_t slba = zone_id + (zone_size_lba * bio->handled_zones);
8101 : :
8102 [ # # # # : 0 : memset(bio->zone_report_buf, 0, zone_report_bufsize);
# # ]
8103 : 0 : ret = spdk_nvme_zns_report_zones(ns, qpair,
8104 [ # # # # ]: 0 : bio->zone_report_buf, zone_report_bufsize,
8105 : 0 : slba, SPDK_NVME_ZRA_LIST_ALL, true,
8106 : 0 : bdev_nvme_get_zone_info_done, bio);
8107 [ # # ]: 0 : if (!ret) {
8108 : 0 : return;
8109 : : } else {
8110 : 0 : goto out_complete_io_ret;
8111 : : }
8112 : : }
8113 : :
8114 : 1 : out_complete_io_nvme_cpl:
8115 [ # # # # ]: 1 : free(bio->zone_report_buf);
8116 [ # # # # ]: 1 : bio->zone_report_buf = NULL;
8117 : 1 : bdev_nvme_io_complete_nvme_status(bio, cpl);
8118 : 1 : return;
8119 : :
8120 : 0 : out_complete_io_ret:
8121 [ # # # # ]: 0 : free(bio->zone_report_buf);
8122 [ # # # # ]: 0 : bio->zone_report_buf = NULL;
8123 : 0 : bdev_nvme_io_complete(bio, ret);
8124 : 0 : }
8125 : :
8126 : : static void
8127 : 41 : bdev_nvme_zone_management_done(void *ref, const struct spdk_nvme_cpl *cpl)
8128 : : {
8129 : 41 : struct nvme_bdev_io *bio = ref;
8130 : :
8131 : 41 : bdev_nvme_io_complete_nvme_status(bio, cpl);
8132 : 41 : }
8133 : :
8134 : : static void
8135 : 138 : bdev_nvme_admin_passthru_complete_nvme_status(void *ctx)
8136 : : {
8137 : 138 : struct nvme_bdev_io *bio = ctx;
8138 : 138 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
8139 [ # # ]: 138 : const struct spdk_nvme_cpl *cpl = &bio->cpl;
8140 : :
8141 [ + + # # : 138 : assert(bdev_nvme_io_type_is_admin(bdev_io->type));
# # # # ]
8142 : :
8143 : 138 : __bdev_nvme_io_complete(bdev_io, 0, cpl);
8144 : 138 : }
8145 : :
8146 : : static void
8147 : 7318 : bdev_nvme_abort_complete(void *ctx)
8148 : : {
8149 : 7318 : struct nvme_bdev_io *bio = ctx;
8150 : 7318 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
8151 : :
8152 [ + - + - : 7318 : if (spdk_nvme_cpl_is_abort_success(&bio->cpl)) {
+ + # # #
# # # # #
# # # # #
# # # # #
# # # # ]
8153 : 12 : __bdev_nvme_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS, NULL);
8154 : 3 : } else {
8155 : 7306 : __bdev_nvme_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED, NULL);
8156 : : }
8157 : 7318 : }
8158 : :
8159 : : static void
8160 : 7318 : bdev_nvme_abort_done(void *ref, const struct spdk_nvme_cpl *cpl)
8161 : : {
8162 : 7318 : struct nvme_bdev_io *bio = ref;
8163 : 7318 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
8164 : :
8165 [ # # ]: 7318 : bio->cpl = *cpl;
8166 : 7318 : spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io), bdev_nvme_abort_complete, bio);
8167 : 7318 : }
8168 : :
8169 : : static void
8170 : 138 : bdev_nvme_admin_passthru_done(void *ref, const struct spdk_nvme_cpl *cpl)
8171 : : {
8172 : 138 : struct nvme_bdev_io *bio = ref;
8173 : 138 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
8174 : :
8175 [ # # ]: 138 : bio->cpl = *cpl;
8176 : 143 : spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io),
8177 : 5 : bdev_nvme_admin_passthru_complete_nvme_status, bio);
8178 : 138 : }
8179 : :
8180 : : static void
8181 : 2119701 : bdev_nvme_queued_reset_sgl(void *ref, uint32_t sgl_offset)
8182 : : {
8183 : 2119701 : struct nvme_bdev_io *bio = ref;
8184 : : struct iovec *iov;
8185 : :
8186 [ # # # # ]: 2119701 : bio->iov_offset = sgl_offset;
8187 [ + + # # : 6742653 : for (bio->iovpos = 0; bio->iovpos < bio->iovcnt; bio->iovpos++) {
# # # # #
# # # # #
# # # # ]
8188 [ # # # # : 6742653 : iov = &bio->iovs[bio->iovpos];
# # # # #
# ]
8189 [ + + # # : 6742653 : if (bio->iov_offset < iov->iov_len) {
# # # # #
# ]
8190 : 2119701 : break;
8191 : : }
8192 : :
8193 [ # # # # : 4622952 : bio->iov_offset -= iov->iov_len;
# # # # ]
8194 : 74 : }
8195 : 2119701 : }
8196 : :
8197 : : static int
8198 : 3789400 : bdev_nvme_queued_next_sge(void *ref, void **address, uint32_t *length)
8199 : : {
8200 : 3789400 : struct nvme_bdev_io *bio = ref;
8201 : : struct iovec *iov;
8202 : :
8203 [ + + # # : 3789400 : assert(bio->iovpos < bio->iovcnt);
# # # # #
# # # ]
8204 : :
8205 [ # # # # : 3789400 : iov = &bio->iovs[bio->iovpos];
# # # # #
# ]
8206 : :
8207 [ # # # # : 3789400 : *address = iov->iov_base;
# # ]
8208 [ # # # # : 3789400 : *length = iov->iov_len;
# # ]
8209 : :
8210 [ + + # # : 3789400 : if (bio->iov_offset) {
# # ]
8211 [ - + # # : 346254 : assert(bio->iov_offset <= iov->iov_len);
# # # # #
# # # ]
8212 [ # # # # : 346254 : *address += bio->iov_offset;
# # ]
8213 [ # # # # : 346254 : *length -= bio->iov_offset;
# # ]
8214 : 0 : }
8215 : :
8216 [ # # # # : 3789400 : bio->iov_offset += *length;
# # ]
8217 [ + + # # : 3789400 : if (bio->iov_offset == iov->iov_len) {
# # # # #
# ]
8218 [ # # # # ]: 3789400 : bio->iovpos++;
8219 [ # # # # ]: 3789400 : bio->iov_offset = 0;
8220 : 162 : }
8221 : :
8222 : 3789400 : return 0;
8223 : : }
8224 : :
8225 : : static void
8226 : 90 : bdev_nvme_queued_reset_fused_sgl(void *ref, uint32_t sgl_offset)
8227 : : {
8228 : 90 : struct nvme_bdev_io *bio = ref;
8229 : : struct iovec *iov;
8230 : :
8231 [ # # # # ]: 90 : bio->fused_iov_offset = sgl_offset;
8232 [ + - # # : 90 : for (bio->fused_iovpos = 0; bio->fused_iovpos < bio->fused_iovcnt; bio->fused_iovpos++) {
# # # # #
# # # # #
# # # # ]
8233 [ # # # # : 90 : iov = &bio->fused_iovs[bio->fused_iovpos];
# # # # #
# ]
8234 [ + - # # : 90 : if (bio->fused_iov_offset < iov->iov_len) {
# # # # #
# ]
8235 : 90 : break;
8236 : : }
8237 : :
8238 [ # # # # : 0 : bio->fused_iov_offset -= iov->iov_len;
# # # # ]
8239 : 0 : }
8240 : 90 : }
8241 : :
8242 : : static int
8243 : 90 : bdev_nvme_queued_next_fused_sge(void *ref, void **address, uint32_t *length)
8244 : : {
8245 : 90 : struct nvme_bdev_io *bio = ref;
8246 : : struct iovec *iov;
8247 : :
8248 [ - + # # : 90 : assert(bio->fused_iovpos < bio->fused_iovcnt);
# # # # #
# # # ]
8249 : :
8250 [ # # # # : 90 : iov = &bio->fused_iovs[bio->fused_iovpos];
# # # # #
# ]
8251 : :
8252 [ # # # # : 90 : *address = iov->iov_base;
# # ]
8253 [ # # # # : 90 : *length = iov->iov_len;
# # ]
8254 : :
8255 [ - + # # : 90 : if (bio->fused_iov_offset) {
# # ]
8256 [ # # # # : 0 : assert(bio->fused_iov_offset <= iov->iov_len);
# # # # #
# # # ]
8257 [ # # # # : 0 : *address += bio->fused_iov_offset;
# # ]
8258 [ # # # # : 0 : *length -= bio->fused_iov_offset;
# # ]
8259 : 0 : }
8260 : :
8261 [ # # # # : 90 : bio->fused_iov_offset += *length;
# # ]
8262 [ + - # # : 90 : if (bio->fused_iov_offset == iov->iov_len) {
# # # # #
# ]
8263 [ # # # # ]: 90 : bio->fused_iovpos++;
8264 [ # # # # ]: 90 : bio->fused_iov_offset = 0;
8265 : 0 : }
8266 : :
8267 : 90 : return 0;
8268 : : }
8269 : :
8270 : : static int
8271 : 0 : bdev_nvme_no_pi_readv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
8272 : : void *md, uint64_t lba_count, uint64_t lba)
8273 : : {
8274 : : int rc;
8275 : :
8276 [ # # # # : 0 : SPDK_DEBUGLOG(bdev_nvme, "read %" PRIu64 " blocks with offset %#" PRIx64 " without PI check\n",
# # ]
8277 : : lba_count, lba);
8278 : :
8279 [ # # # # ]: 0 : bio->iovs = iov;
8280 [ # # # # ]: 0 : bio->iovcnt = iovcnt;
8281 [ # # # # ]: 0 : bio->iovpos = 0;
8282 [ # # # # ]: 0 : bio->iov_offset = 0;
8283 : :
8284 [ # # # # : 0 : rc = spdk_nvme_ns_cmd_readv_with_md(bio->io_path->nvme_ns->ns,
# # # # #
# # # ]
8285 [ # # # # : 0 : bio->io_path->qpair->qpair,
# # # # #
# # # ]
8286 : 0 : lba, lba_count,
8287 : 0 : bdev_nvme_no_pi_readv_done, bio, 0,
8288 : : bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge,
8289 : 0 : md, 0, 0);
8290 : :
8291 [ # # # # ]: 0 : if (rc != 0 && rc != -ENOMEM) {
8292 : 0 : SPDK_ERRLOG("no_pi_readv failed: rc = %d\n", rc);
8293 : 0 : }
8294 : 0 : return rc;
8295 : : }
8296 : :
8297 : : static int
8298 : 12816227 : bdev_nvme_readv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
8299 : : void *md, uint64_t lba_count, uint64_t lba, uint32_t flags,
8300 : : struct spdk_memory_domain *domain, void *domain_ctx,
8301 : : struct spdk_accel_sequence *seq)
8302 : : {
8303 [ + - + - : 12816227 : struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
+ - + - +
- + - ]
8304 [ + - + - : 12816227 : struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
+ - + - +
- + - ]
8305 : : int rc;
8306 : :
8307 [ + + + + : 12816227 : SPDK_DEBUGLOG(bdev_nvme, "read %" PRIu64 " blocks with offset %#" PRIx64 "\n",
+ - ]
8308 : : lba_count, lba);
8309 : :
8310 [ + - + - ]: 12816227 : bio->iovs = iov;
8311 [ + - + - ]: 12816227 : bio->iovcnt = iovcnt;
8312 [ + - + - ]: 12816227 : bio->iovpos = 0;
8313 [ + - + - ]: 12816227 : bio->iov_offset = 0;
8314 : :
8315 [ + + + + ]: 12816227 : if (domain != NULL || seq != NULL) {
8316 [ # # # # : 354679 : bio->ext_opts.size = SPDK_SIZEOF(&bio->ext_opts, accel_sequence);
# # ]
8317 [ # # # # : 354679 : bio->ext_opts.memory_domain = domain;
# # ]
8318 [ # # # # : 354679 : bio->ext_opts.memory_domain_ctx = domain_ctx;
# # ]
8319 [ # # # # : 354679 : bio->ext_opts.io_flags = flags;
# # ]
8320 [ # # # # : 354679 : bio->ext_opts.metadata = md;
# # ]
8321 [ # # # # : 354679 : bio->ext_opts.accel_sequence = seq;
# # ]
8322 : :
8323 [ + - ]: 354679 : if (iovcnt == 1) {
8324 [ # # # # : 354634 : rc = spdk_nvme_ns_cmd_read_ext(ns, qpair, iov[0].iov_base, lba, lba_count, bdev_nvme_readv_done,
# # ]
8325 [ # # ]: 1 : bio, &bio->ext_opts);
8326 : 1 : } else {
8327 : 0 : rc = spdk_nvme_ns_cmd_readv_ext(ns, qpair, lba, lba_count,
8328 : 0 : bdev_nvme_readv_done, bio,
8329 : : bdev_nvme_queued_reset_sgl,
8330 : : bdev_nvme_queued_next_sge,
8331 [ # # ]: 0 : &bio->ext_opts);
8332 : : }
8333 [ + + ]: 12461549 : } else if (iovcnt == 1) {
8334 [ - + - + : 12417188 : rc = spdk_nvme_ns_cmd_read_with_md(ns, qpair, iov[0].iov_base,
- + ]
8335 : 157418 : md, lba, lba_count, bdev_nvme_readv_done,
8336 : 157418 : bio, flags, 0, 0);
8337 : 157418 : } else {
8338 : 201780 : rc = spdk_nvme_ns_cmd_readv_with_md(ns, qpair, lba, lba_count,
8339 : 5 : bdev_nvme_readv_done, bio, flags,
8340 : : bdev_nvme_queued_reset_sgl,
8341 : 5 : bdev_nvme_queued_next_sge, md, 0, 0);
8342 : : }
8343 : :
8344 [ + + + + ]: 12816181 : if (spdk_unlikely(rc != 0 && rc != -ENOMEM)) {
8345 : 0 : SPDK_ERRLOG("readv failed: rc = %d\n", rc);
8346 : 0 : }
8347 : 12816181 : return rc;
8348 : : }
8349 : :
8350 : : static int
8351 : 13368012 : bdev_nvme_writev(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
8352 : : void *md, uint64_t lba_count, uint64_t lba, uint32_t flags,
8353 : : struct spdk_memory_domain *domain, void *domain_ctx,
8354 : : struct spdk_accel_sequence *seq,
8355 : : union spdk_bdev_nvme_cdw12 cdw12, union spdk_bdev_nvme_cdw13 cdw13)
8356 : : {
8357 [ # # # # : 13368012 : struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
# # # # #
# # # ]
8358 [ # # # # : 13368012 : struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
# # # # #
# # # ]
8359 : : int rc;
8360 : :
8361 [ + + - + : 13368012 : SPDK_DEBUGLOG(bdev_nvme, "write %" PRIu64 " blocks with offset %#" PRIx64 "\n",
# # ]
8362 : : lba_count, lba);
8363 : :
8364 [ # # # # ]: 13368012 : bio->iovs = iov;
8365 [ # # # # ]: 13368012 : bio->iovcnt = iovcnt;
8366 [ # # # # ]: 13368012 : bio->iovpos = 0;
8367 [ # # # # ]: 13368012 : bio->iov_offset = 0;
8368 : :
8369 [ + + + + ]: 13368012 : if (domain != NULL || seq != NULL) {
8370 [ # # # # : 196403 : bio->ext_opts.size = SPDK_SIZEOF(&bio->ext_opts, accel_sequence);
# # ]
8371 [ # # # # : 196403 : bio->ext_opts.memory_domain = domain;
# # ]
8372 [ # # # # : 196403 : bio->ext_opts.memory_domain_ctx = domain_ctx;
# # ]
8373 [ - + # # : 196403 : bio->ext_opts.io_flags = flags | SPDK_NVME_IO_FLAGS_DIRECTIVE(cdw12.write.dtype);
# # # # #
# ]
8374 [ # # # # : 196403 : bio->ext_opts.cdw13 = cdw13.raw;
# # ]
8375 [ # # # # : 196403 : bio->ext_opts.metadata = md;
# # ]
8376 [ # # # # : 196403 : bio->ext_opts.accel_sequence = seq;
# # ]
8377 : :
8378 [ + - ]: 196403 : if (iovcnt == 1) {
8379 [ # # # # : 196389 : rc = spdk_nvme_ns_cmd_write_ext(ns, qpair, iov[0].iov_base, lba, lba_count, bdev_nvme_writev_done,
# # ]
8380 [ # # ]: 0 : bio, &bio->ext_opts);
8381 : 0 : } else {
8382 : 0 : rc = spdk_nvme_ns_cmd_writev_ext(ns, qpair, lba, lba_count,
8383 : 0 : bdev_nvme_writev_done, bio,
8384 : : bdev_nvme_queued_reset_sgl,
8385 : : bdev_nvme_queued_next_sge,
8386 [ # # ]: 0 : &bio->ext_opts);
8387 : : }
8388 [ + + ]: 13171623 : } else if (iovcnt == 1) {
8389 [ # # # # : 13209479 : rc = spdk_nvme_ns_cmd_write_with_md(ns, qpair, iov[0].iov_base,
# # ]
8390 : 157590 : md, lba, lba_count, bdev_nvme_writev_done,
8391 : 157590 : bio, flags, 0, 0);
8392 : 157590 : } else {
8393 : 119736 : rc = spdk_nvme_ns_cmd_writev_with_md(ns, qpair, lba, lba_count,
8394 : 2 : bdev_nvme_writev_done, bio, flags,
8395 : : bdev_nvme_queued_reset_sgl,
8396 : 2 : bdev_nvme_queued_next_sge, md, 0, 0);
8397 : : }
8398 : :
8399 [ + + - + ]: 13368012 : if (spdk_unlikely(rc != 0 && rc != -ENOMEM)) {
8400 : 0 : SPDK_ERRLOG("writev failed: rc = %d\n", rc);
8401 : 0 : }
8402 : 13368012 : return rc;
8403 : : }
8404 : :
8405 : : static int
8406 : 132625 : bdev_nvme_zone_appendv(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
8407 : : void *md, uint64_t lba_count, uint64_t zslba,
8408 : : uint32_t flags)
8409 : : {
8410 [ # # # # : 132625 : struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
# # # # #
# # # ]
8411 [ # # # # : 132625 : struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
# # # # #
# # # ]
8412 : : int rc;
8413 : :
8414 [ - + # # : 132625 : SPDK_DEBUGLOG(bdev_nvme, "zone append %" PRIu64 " blocks to zone start lba %#" PRIx64 "\n",
# # ]
8415 : : lba_count, zslba);
8416 : :
8417 [ # # # # ]: 132625 : bio->iovs = iov;
8418 [ # # # # ]: 132625 : bio->iovcnt = iovcnt;
8419 [ # # # # ]: 132625 : bio->iovpos = 0;
8420 [ # # # # ]: 132625 : bio->iov_offset = 0;
8421 : :
8422 [ + - ]: 132625 : if (iovcnt == 1) {
8423 [ # # # # : 132625 : rc = spdk_nvme_zns_zone_append_with_md(ns, qpair, iov[0].iov_base, md, zslba,
# # ]
8424 : 0 : lba_count,
8425 : 0 : bdev_nvme_zone_appendv_done, bio,
8426 : 0 : flags,
8427 : : 0, 0);
8428 : 0 : } else {
8429 : 0 : rc = spdk_nvme_zns_zone_appendv_with_md(ns, qpair, zslba, lba_count,
8430 : 0 : bdev_nvme_zone_appendv_done, bio, flags,
8431 : : bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge,
8432 : 0 : md, 0, 0);
8433 : : }
8434 : :
8435 [ - + - - ]: 132625 : if (rc != 0 && rc != -ENOMEM) {
8436 : 0 : SPDK_ERRLOG("zone append failed: rc = %d\n", rc);
8437 : 0 : }
8438 : 132625 : return rc;
8439 : : }
8440 : :
8441 : : static int
8442 : 56 : bdev_nvme_comparev(struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt,
8443 : : void *md, uint64_t lba_count, uint64_t lba,
8444 : : uint32_t flags)
8445 : : {
8446 : : int rc;
8447 : :
8448 [ + + - + : 56 : SPDK_DEBUGLOG(bdev_nvme, "compare %" PRIu64 " blocks with offset %#" PRIx64 "\n",
# # ]
8449 : : lba_count, lba);
8450 : :
8451 [ # # # # ]: 56 : bio->iovs = iov;
8452 [ # # # # ]: 56 : bio->iovcnt = iovcnt;
8453 [ # # # # ]: 56 : bio->iovpos = 0;
8454 [ # # # # ]: 56 : bio->iov_offset = 0;
8455 : :
8456 [ # # # # : 59 : rc = spdk_nvme_ns_cmd_comparev_with_md(bio->io_path->nvme_ns->ns,
# # # # #
# # # ]
8457 [ # # # # : 56 : bio->io_path->qpair->qpair,
# # # # #
# # # ]
8458 : 3 : lba, lba_count,
8459 : 3 : bdev_nvme_comparev_done, bio, flags,
8460 : : bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge,
8461 : 3 : md, 0, 0);
8462 : :
8463 [ - + - - ]: 56 : if (rc != 0 && rc != -ENOMEM) {
8464 : 0 : SPDK_ERRLOG("comparev failed: rc = %d\n", rc);
8465 : 0 : }
8466 : 56 : return rc;
8467 : : }
8468 : :
8469 : : static int
8470 : 53 : bdev_nvme_comparev_and_writev(struct nvme_bdev_io *bio, struct iovec *cmp_iov, int cmp_iovcnt,
8471 : : struct iovec *write_iov, int write_iovcnt,
8472 : : void *md, uint64_t lba_count, uint64_t lba, uint32_t flags)
8473 : : {
8474 [ # # # # : 53 : struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
# # # # #
# # # ]
8475 [ # # # # : 53 : struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
# # # # #
# # # ]
8476 : 53 : struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio);
8477 : : int rc;
8478 : :
8479 [ + + - + : 53 : SPDK_DEBUGLOG(bdev_nvme, "compare and write %" PRIu64 " blocks with offset %#" PRIx64 "\n",
# # ]
8480 : : lba_count, lba);
8481 : :
8482 [ # # # # ]: 53 : bio->iovs = cmp_iov;
8483 [ # # # # ]: 53 : bio->iovcnt = cmp_iovcnt;
8484 [ # # # # ]: 53 : bio->iovpos = 0;
8485 [ # # # # ]: 53 : bio->iov_offset = 0;
8486 [ # # # # ]: 53 : bio->fused_iovs = write_iov;
8487 [ # # # # ]: 53 : bio->fused_iovcnt = write_iovcnt;
8488 [ # # # # ]: 53 : bio->fused_iovpos = 0;
8489 [ # # # # ]: 53 : bio->fused_iov_offset = 0;
8490 : :
8491 [ + + # # : 53 : if (bdev_io->num_retries == 0) {
# # ]
8492 [ # # # # ]: 53 : bio->first_fused_submitted = false;
8493 [ # # # # ]: 53 : bio->first_fused_completed = false;
8494 : 2 : }
8495 : :
8496 [ + + + - : 53 : if (!bio->first_fused_submitted) {
# # # # ]
8497 [ # # # # ]: 53 : flags |= SPDK_NVME_IO_FLAGS_FUSE_FIRST;
8498 [ - + # # ]: 53 : memset(&bio->cpl, 0, sizeof(bio->cpl));
8499 : :
8500 : 55 : rc = spdk_nvme_ns_cmd_comparev_with_md(ns, qpair, lba, lba_count,
8501 : 2 : bdev_nvme_comparev_and_writev_done, bio, flags,
8502 : 2 : bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge, md, 0, 0);
8503 [ + + ]: 53 : if (rc == 0) {
8504 [ # # # # ]: 53 : bio->first_fused_submitted = true;
8505 [ # # # # ]: 53 : flags &= ~SPDK_NVME_IO_FLAGS_FUSE_FIRST;
8506 : 2 : } else {
8507 [ # # ]: 0 : if (rc != -ENOMEM) {
8508 : 0 : SPDK_ERRLOG("compare failed: rc = %d\n", rc);
8509 : 0 : }
8510 : 0 : return rc;
8511 : : }
8512 : 2 : }
8513 : :
8514 [ # # # # ]: 53 : flags |= SPDK_NVME_IO_FLAGS_FUSE_SECOND;
8515 : :
8516 : 55 : rc = spdk_nvme_ns_cmd_writev_with_md(ns, qpair, lba, lba_count,
8517 : 2 : bdev_nvme_comparev_and_writev_done, bio, flags,
8518 : 2 : bdev_nvme_queued_reset_fused_sgl, bdev_nvme_queued_next_fused_sge, md, 0, 0);
8519 [ - + - - ]: 53 : if (rc != 0 && rc != -ENOMEM) {
8520 : 0 : SPDK_ERRLOG("write failed: rc = %d\n", rc);
8521 : 0 : rc = 0;
8522 : 0 : }
8523 : :
8524 : 53 : return rc;
8525 : 2 : }
8526 : :
8527 : : static int
8528 : 66789 : bdev_nvme_unmap(struct nvme_bdev_io *bio, uint64_t offset_blocks, uint64_t num_blocks)
8529 : : {
8530 : 4863 : struct spdk_nvme_dsm_range dsm_ranges[SPDK_NVME_DATASET_MANAGEMENT_MAX_RANGES];
8531 : : struct spdk_nvme_dsm_range *range;
8532 : : uint64_t offset, remaining;
8533 : : uint64_t num_ranges_u64;
8534 : : uint16_t num_ranges;
8535 : : int rc;
8536 : :
8537 [ # # ]: 66789 : num_ranges_u64 = (num_blocks + SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS - 1) /
8538 : : SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS;
8539 [ - + ]: 66789 : if (num_ranges_u64 > SPDK_COUNTOF(dsm_ranges)) {
8540 : 0 : SPDK_ERRLOG("Unmap request for %" PRIu64 " blocks is too large\n", num_blocks);
8541 : 0 : return -EINVAL;
8542 : : }
8543 : 66789 : num_ranges = (uint16_t)num_ranges_u64;
8544 : :
8545 : 66789 : offset = offset_blocks;
8546 : 66789 : remaining = num_blocks;
8547 [ # # # # ]: 66789 : range = &dsm_ranges[0];
8548 : :
8549 : : /* Fill max-size ranges until the remaining blocks fit into one range */
8550 [ - + ]: 66789 : while (remaining > SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS) {
8551 [ # # # # : 0 : range->attributes.raw = 0;
# # ]
8552 [ # # # # ]: 0 : range->length = SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS;
8553 [ # # # # ]: 0 : range->starting_lba = offset;
8554 : :
8555 : 0 : offset += SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS;
8556 : 0 : remaining -= SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS;
8557 [ # # ]: 0 : range++;
8558 : : }
8559 : :
8560 : : /* Final range describes the remaining blocks */
8561 [ # # # # : 66789 : range->attributes.raw = 0;
# # ]
8562 [ # # # # ]: 66789 : range->length = remaining;
8563 [ # # # # ]: 66789 : range->starting_lba = offset;
8564 : :
8565 [ # # # # : 66792 : rc = spdk_nvme_ns_cmd_dataset_management(bio->io_path->nvme_ns->ns,
# # # # #
# # # ]
8566 [ # # # # : 66789 : bio->io_path->qpair->qpair,
# # # # #
# # # ]
8567 : : SPDK_NVME_DSM_ATTR_DEALLOCATE,
8568 : 3 : dsm_ranges, num_ranges,
8569 : 3 : bdev_nvme_queued_done, bio);
8570 : :
8571 : 66789 : return rc;
8572 : 3 : }
8573 : :
8574 : : static int
8575 : 745437 : bdev_nvme_write_zeroes(struct nvme_bdev_io *bio, uint64_t offset_blocks, uint64_t num_blocks)
8576 : : {
8577 [ - + ]: 745437 : if (num_blocks > UINT16_MAX + 1) {
8578 : 0 : SPDK_ERRLOG("NVMe write zeroes is limited to 16-bit block count\n");
8579 : 0 : return -EINVAL;
8580 : : }
8581 : :
8582 [ # # # # : 792610 : return spdk_nvme_ns_cmd_write_zeroes(bio->io_path->nvme_ns->ns,
# # # # #
# # # ]
8583 [ # # # # : 745437 : bio->io_path->qpair->qpair,
# # # # #
# # # ]
8584 : 47173 : offset_blocks, num_blocks,
8585 : 47173 : bdev_nvme_queued_done, bio,
8586 : : 0);
8587 : 47173 : }
8588 : :
8589 : : static int
8590 : 1 : bdev_nvme_get_zone_info(struct nvme_bdev_io *bio, uint64_t zone_id, uint32_t num_zones,
8591 : : struct spdk_bdev_zone_info *info)
8592 : : {
8593 [ # # # # : 1 : struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
# # # # #
# # # ]
8594 [ # # # # : 1 : struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
# # # # #
# # # ]
8595 : 1 : uint32_t zone_report_bufsize = spdk_nvme_ns_get_max_io_xfer_size(ns);
8596 : 1 : uint64_t zone_size = spdk_nvme_zns_ns_get_zone_size_sectors(ns);
8597 : 1 : uint64_t total_zones = spdk_nvme_zns_ns_get_num_zones(ns);
8598 : :
8599 [ - + # # ]: 1 : if (zone_id % zone_size != 0) {
8600 : 0 : return -EINVAL;
8601 : : }
8602 : :
8603 [ + - - + ]: 1 : if (num_zones > total_zones || !num_zones) {
8604 : 0 : return -EINVAL;
8605 : : }
8606 : :
8607 [ - + # # : 1 : assert(!bio->zone_report_buf);
# # # # ]
8608 [ # # # # ]: 1 : bio->zone_report_buf = calloc(1, zone_report_bufsize);
8609 [ - + # # : 1 : if (!bio->zone_report_buf) {
# # ]
8610 : 0 : return -ENOMEM;
8611 : : }
8612 : :
8613 [ # # # # ]: 1 : bio->handled_zones = 0;
8614 : :
8615 [ # # # # ]: 1 : return spdk_nvme_zns_report_zones(ns, qpair, bio->zone_report_buf, zone_report_bufsize,
8616 : 0 : zone_id, SPDK_NVME_ZRA_LIST_ALL, true,
8617 : 0 : bdev_nvme_get_zone_info_done, bio);
8618 : 0 : }
8619 : :
8620 : : static int
8621 : 41 : bdev_nvme_zone_management(struct nvme_bdev_io *bio, uint64_t zone_id,
8622 : : enum spdk_bdev_zone_action action)
8623 : : {
8624 [ # # # # : 41 : struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
# # # # #
# # # ]
8625 [ # # # # : 41 : struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
# # # # #
# # # ]
8626 : :
8627 [ - - - + : 41 : switch (action) {
- - ]
8628 : 0 : case SPDK_BDEV_ZONE_CLOSE:
8629 : 0 : return spdk_nvme_zns_close_zone(ns, qpair, zone_id, false,
8630 : 0 : bdev_nvme_zone_management_done, bio);
8631 : 0 : case SPDK_BDEV_ZONE_FINISH:
8632 : 0 : return spdk_nvme_zns_finish_zone(ns, qpair, zone_id, false,
8633 : 0 : bdev_nvme_zone_management_done, bio);
8634 : 0 : case SPDK_BDEV_ZONE_OPEN:
8635 : 0 : return spdk_nvme_zns_open_zone(ns, qpair, zone_id, false,
8636 : 0 : bdev_nvme_zone_management_done, bio);
8637 : 41 : case SPDK_BDEV_ZONE_RESET:
8638 : 41 : return spdk_nvme_zns_reset_zone(ns, qpair, zone_id, false,
8639 : 0 : bdev_nvme_zone_management_done, bio);
8640 : 0 : case SPDK_BDEV_ZONE_OFFLINE:
8641 : 0 : return spdk_nvme_zns_offline_zone(ns, qpair, zone_id, false,
8642 : 0 : bdev_nvme_zone_management_done, bio);
8643 : 0 : default:
8644 : 0 : return -EINVAL;
8645 : : }
8646 : 0 : }
8647 : :
8648 : : static void
8649 : 142 : bdev_nvme_admin_passthru(struct nvme_bdev_channel *nbdev_ch, struct nvme_bdev_io *bio,
8650 : : struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes)
8651 : : {
8652 : : struct nvme_io_path *io_path;
8653 : : struct nvme_ctrlr *nvme_ctrlr;
8654 : : uint32_t max_xfer_size;
8655 : 142 : int rc = -ENXIO;
8656 : :
8657 : : /* Choose the first ctrlr which is not failed. */
8658 [ + + # # : 154 : STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
# # # # #
# # # #
# ]
8659 [ # # # # : 150 : nvme_ctrlr = io_path->qpair->ctrlr;
# # # # ]
8660 : :
8661 : : /* We should skip any unavailable nvme_ctrlr rather than checking
8662 : : * if the return value of spdk_nvme_ctrlr_cmd_admin_raw() is -ENXIO.
8663 : : */
8664 [ + + ]: 150 : if (!nvme_ctrlr_is_available(nvme_ctrlr)) {
8665 : 12 : continue;
8666 : : }
8667 : :
8668 [ # # # # ]: 138 : max_xfer_size = spdk_nvme_ctrlr_get_max_xfer_size(nvme_ctrlr->ctrlr);
8669 : :
8670 [ + + ]: 138 : if (nbytes > max_xfer_size) {
8671 : 0 : SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size);
8672 : 0 : rc = -EINVAL;
8673 : 0 : goto err;
8674 : : }
8675 : :
8676 [ # # # # ]: 143 : rc = spdk_nvme_ctrlr_cmd_admin_raw(nvme_ctrlr->ctrlr, cmd, buf, (uint32_t)nbytes,
8677 : 5 : bdev_nvme_admin_passthru_done, bio);
8678 [ + + ]: 138 : if (rc == 0) {
8679 : 138 : return;
8680 : : }
8681 : 1 : }
8682 : :
8683 : 3 : err:
8684 : 4 : bdev_nvme_admin_complete(bio, rc);
8685 : 6 : }
8686 : :
8687 : : static int
8688 : 111 : bdev_nvme_io_passthru(struct nvme_bdev_io *bio, struct spdk_nvme_cmd *cmd,
8689 : : void *buf, size_t nbytes)
8690 : : {
8691 [ # # # # : 111 : struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
# # # # #
# # # ]
8692 [ # # # # : 111 : struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
# # # # #
# # # ]
8693 : 111 : uint32_t max_xfer_size = spdk_nvme_ns_get_max_io_xfer_size(ns);
8694 : 111 : struct spdk_nvme_ctrlr *ctrlr = spdk_nvme_ns_get_ctrlr(ns);
8695 : :
8696 [ - + ]: 111 : if (nbytes > max_xfer_size) {
8697 : 0 : SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size);
8698 : 0 : return -EINVAL;
8699 : : }
8700 : :
8701 : : /*
8702 : : * Each NVMe bdev is a specific namespace, and all NVMe I/O commands require a nsid,
8703 : : * so fill it out automatically.
8704 : : */
8705 [ # # # # ]: 111 : cmd->nsid = spdk_nvme_ns_get_id(ns);
8706 : :
8707 : 114 : return spdk_nvme_ctrlr_cmd_io_raw(ctrlr, qpair, cmd, buf,
8708 : 3 : (uint32_t)nbytes, bdev_nvme_queued_done, bio);
8709 : 3 : }
8710 : :
8711 : : static int
8712 : 0 : bdev_nvme_io_passthru_md(struct nvme_bdev_io *bio, struct spdk_nvme_cmd *cmd,
8713 : : void *buf, size_t nbytes, void *md_buf, size_t md_len)
8714 : : {
8715 [ # # # # : 0 : struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
# # # # #
# # # ]
8716 [ # # # # : 0 : struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
# # # # #
# # # ]
8717 [ # # ]: 0 : size_t nr_sectors = nbytes / spdk_nvme_ns_get_extended_sector_size(ns);
8718 : 0 : uint32_t max_xfer_size = spdk_nvme_ns_get_max_io_xfer_size(ns);
8719 : 0 : struct spdk_nvme_ctrlr *ctrlr = spdk_nvme_ns_get_ctrlr(ns);
8720 : :
8721 [ # # ]: 0 : if (nbytes > max_xfer_size) {
8722 : 0 : SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size);
8723 : 0 : return -EINVAL;
8724 : : }
8725 : :
8726 [ # # ]: 0 : if (md_len != nr_sectors * spdk_nvme_ns_get_md_size(ns)) {
8727 : 0 : SPDK_ERRLOG("invalid meta data buffer size\n");
8728 : 0 : return -EINVAL;
8729 : : }
8730 : :
8731 : : /*
8732 : : * Each NVMe bdev is a specific namespace, and all NVMe I/O commands require a nsid,
8733 : : * so fill it out automatically.
8734 : : */
8735 [ # # # # ]: 0 : cmd->nsid = spdk_nvme_ns_get_id(ns);
8736 : :
8737 : 0 : return spdk_nvme_ctrlr_cmd_io_raw_with_md(ctrlr, qpair, cmd, buf,
8738 : 0 : (uint32_t)nbytes, md_buf, bdev_nvme_queued_done, bio);
8739 : 0 : }
8740 : :
8741 : : static int
8742 : 0 : bdev_nvme_iov_passthru_md(struct nvme_bdev_io *bio,
8743 : : struct spdk_nvme_cmd *cmd, struct iovec *iov, int iovcnt,
8744 : : size_t nbytes, void *md_buf, size_t md_len)
8745 : : {
8746 [ # # # # : 0 : struct spdk_nvme_ns *ns = bio->io_path->nvme_ns->ns;
# # # # #
# # # ]
8747 [ # # # # : 0 : struct spdk_nvme_qpair *qpair = bio->io_path->qpair->qpair;
# # # # #
# # # ]
8748 [ # # ]: 0 : size_t nr_sectors = nbytes / spdk_nvme_ns_get_extended_sector_size(ns);
8749 : 0 : uint32_t max_xfer_size = spdk_nvme_ns_get_max_io_xfer_size(ns);
8750 : 0 : struct spdk_nvme_ctrlr *ctrlr = spdk_nvme_ns_get_ctrlr(ns);
8751 : :
8752 [ # # # # ]: 0 : bio->iovs = iov;
8753 [ # # # # ]: 0 : bio->iovcnt = iovcnt;
8754 [ # # # # ]: 0 : bio->iovpos = 0;
8755 [ # # # # ]: 0 : bio->iov_offset = 0;
8756 : :
8757 [ # # ]: 0 : if (nbytes > max_xfer_size) {
8758 : 0 : SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size);
8759 : 0 : return -EINVAL;
8760 : : }
8761 : :
8762 [ # # ]: 0 : if (md_len != nr_sectors * spdk_nvme_ns_get_md_size(ns)) {
8763 : 0 : SPDK_ERRLOG("invalid meta data buffer size\n");
8764 : 0 : return -EINVAL;
8765 : : }
8766 : :
8767 : : /*
8768 : : * Each NVMe bdev is a specific namespace, and all NVMe I/O commands
8769 : : * require a nsid, so fill it out automatically.
8770 : : */
8771 [ # # # # ]: 0 : cmd->nsid = spdk_nvme_ns_get_id(ns);
8772 : :
8773 : 0 : return spdk_nvme_ctrlr_cmd_iov_raw_with_md(
8774 : 0 : ctrlr, qpair, cmd, (uint32_t)nbytes, md_buf, bdev_nvme_queued_done, bio,
8775 : : bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge);
8776 : 0 : }
8777 : :
8778 : : static void
8779 : 7330 : bdev_nvme_abort(struct nvme_bdev_channel *nbdev_ch, struct nvme_bdev_io *bio,
8780 : : struct nvme_bdev_io *bio_to_abort)
8781 : : {
8782 : : struct nvme_io_path *io_path;
8783 : 7330 : int rc = 0;
8784 : :
8785 : 7330 : rc = bdev_nvme_abort_retry_io(nbdev_ch, bio_to_abort);
8786 [ + + ]: 7330 : if (rc == 0) {
8787 : 4 : bdev_nvme_admin_complete(bio, 0);
8788 : 4 : return;
8789 : : }
8790 : :
8791 [ # # # # ]: 7326 : io_path = bio_to_abort->io_path;
8792 [ + + ]: 7326 : if (io_path != NULL) {
8793 [ # # # # : 7321 : rc = spdk_nvme_ctrlr_cmd_abort_ext(io_path->qpair->ctrlr->ctrlr,
# # # # #
# # # ]
8794 [ # # # # : 7318 : io_path->qpair->qpair,
# # # # ]
8795 : 3 : bio_to_abort,
8796 : 3 : bdev_nvme_abort_done, bio);
8797 : 3 : } else {
8798 [ + + # # : 12 : STAILQ_FOREACH(io_path, &nbdev_ch->io_path_list, stailq) {
# # # # #
# # # #
# ]
8799 [ # # # # : 10 : rc = spdk_nvme_ctrlr_cmd_abort_ext(io_path->qpair->ctrlr->ctrlr,
# # # # #
# # # ]
8800 : : NULL,
8801 : 2 : bio_to_abort,
8802 : 2 : bdev_nvme_abort_done, bio);
8803 : :
8804 [ + + ]: 8 : if (rc != -ENOENT) {
8805 : 4 : break;
8806 : : }
8807 : 1 : }
8808 : : }
8809 : :
8810 [ + + ]: 7326 : if (rc != 0) {
8811 : : /* If no command was found or there was any error, complete the abort
8812 : : * request with failure.
8813 : : */
8814 : 8 : bdev_nvme_admin_complete(bio, rc);
8815 : 2 : }
8816 : 6 : }
8817 : :
8818 : : static int
8819 : 37 : bdev_nvme_copy(struct nvme_bdev_io *bio, uint64_t dst_offset_blocks, uint64_t src_offset_blocks,
8820 : : uint64_t num_blocks)
8821 : : {
8822 : 39 : struct spdk_nvme_scc_source_range range = {
8823 : 1 : .slba = src_offset_blocks,
8824 : 37 : .nlb = num_blocks - 1
8825 : : };
8826 : :
8827 [ # # # # : 46 : return spdk_nvme_ns_cmd_copy(bio->io_path->nvme_ns->ns,
# # # # #
# # # ]
8828 [ # # # # : 37 : bio->io_path->qpair->qpair,
# # # # #
# # # ]
8829 : 1 : &range, 1, dst_offset_blocks,
8830 : 1 : bdev_nvme_queued_done, bio);
8831 : : }
8832 : :
8833 : : static void
8834 : 197 : bdev_nvme_opts_config_json(struct spdk_json_write_ctx *w)
8835 : : {
8836 : : const char *action;
8837 : : uint32_t i;
8838 : :
8839 [ + + - + ]: 197 : if (g_opts.action_on_timeout == SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET) {
8840 : 0 : action = "reset";
8841 [ + + - + ]: 197 : } else if (g_opts.action_on_timeout == SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT) {
8842 : 7 : action = "abort";
8843 : 1 : } else {
8844 : 190 : action = "none";
8845 : : }
8846 : :
8847 : 197 : spdk_json_write_object_begin(w);
8848 : :
8849 : 197 : spdk_json_write_named_string(w, "method", "bdev_nvme_set_options");
8850 : :
8851 : 197 : spdk_json_write_named_object_begin(w, "params");
8852 : 197 : spdk_json_write_named_string(w, "action_on_timeout", action);
8853 [ + - ]: 197 : spdk_json_write_named_uint64(w, "timeout_us", g_opts.timeout_us);
8854 [ + - ]: 197 : spdk_json_write_named_uint64(w, "timeout_admin_us", g_opts.timeout_admin_us);
8855 [ + - ]: 197 : spdk_json_write_named_uint32(w, "keep_alive_timeout_ms", g_opts.keep_alive_timeout_ms);
8856 [ + - ]: 197 : spdk_json_write_named_uint32(w, "arbitration_burst", g_opts.arbitration_burst);
8857 [ + - ]: 197 : spdk_json_write_named_uint32(w, "low_priority_weight", g_opts.low_priority_weight);
8858 [ + - ]: 197 : spdk_json_write_named_uint32(w, "medium_priority_weight", g_opts.medium_priority_weight);
8859 [ + - ]: 197 : spdk_json_write_named_uint32(w, "high_priority_weight", g_opts.high_priority_weight);
8860 [ + - ]: 197 : spdk_json_write_named_uint64(w, "nvme_adminq_poll_period_us", g_opts.nvme_adminq_poll_period_us);
8861 [ + - ]: 197 : spdk_json_write_named_uint64(w, "nvme_ioq_poll_period_us", g_opts.nvme_ioq_poll_period_us);
8862 [ + - ]: 197 : spdk_json_write_named_uint32(w, "io_queue_requests", g_opts.io_queue_requests);
8863 [ + + + - ]: 197 : spdk_json_write_named_bool(w, "delay_cmd_submit", g_opts.delay_cmd_submit);
8864 [ + - ]: 197 : spdk_json_write_named_uint32(w, "transport_retry_count", g_opts.transport_retry_count);
8865 [ + - ]: 197 : spdk_json_write_named_int32(w, "bdev_retry_count", g_opts.bdev_retry_count);
8866 [ + - ]: 197 : spdk_json_write_named_uint8(w, "transport_ack_timeout", g_opts.transport_ack_timeout);
8867 [ + - ]: 197 : spdk_json_write_named_int32(w, "ctrlr_loss_timeout_sec", g_opts.ctrlr_loss_timeout_sec);
8868 [ + - ]: 197 : spdk_json_write_named_uint32(w, "reconnect_delay_sec", g_opts.reconnect_delay_sec);
8869 [ + - ]: 197 : spdk_json_write_named_uint32(w, "fast_io_fail_timeout_sec", g_opts.fast_io_fail_timeout_sec);
8870 [ + + ]: 197 : spdk_json_write_named_bool(w, "disable_auto_failback", g_opts.disable_auto_failback);
8871 [ + + + - ]: 197 : spdk_json_write_named_bool(w, "generate_uuids", g_opts.generate_uuids);
8872 : 197 : spdk_json_write_named_uint8(w, "transport_tos", g_opts.transport_tos);
8873 [ + + + - ]: 197 : spdk_json_write_named_bool(w, "nvme_error_stat", g_opts.nvme_error_stat);
8874 [ + - ]: 197 : spdk_json_write_named_uint32(w, "rdma_srq_size", g_opts.rdma_srq_size);
8875 [ + + ]: 197 : spdk_json_write_named_bool(w, "io_path_stat", g_opts.io_path_stat);
8876 [ + + + - ]: 197 : spdk_json_write_named_bool(w, "allow_accel_sequence", g_opts.allow_accel_sequence);
8877 [ + - ]: 197 : spdk_json_write_named_uint32(w, "rdma_max_cq_size", g_opts.rdma_max_cq_size);
8878 [ + - ]: 197 : spdk_json_write_named_uint16(w, "rdma_cm_event_timeout_ms", g_opts.rdma_cm_event_timeout_ms);
8879 : 197 : spdk_json_write_named_array_begin(w, "dhchap_digests");
8880 [ + + ]: 6501 : for (i = 0; i < 32; ++i) {
8881 [ + + + + : 6304 : if (g_opts.dhchap_digests & SPDK_BIT(i)) {
+ + ]
8882 : 591 : spdk_json_write_string(w, spdk_nvme_dhchap_get_digest_name(i));
8883 : 33 : }
8884 : 352 : }
8885 : 197 : spdk_json_write_array_end(w);
8886 : 197 : spdk_json_write_named_array_begin(w, "dhchap_dhgroups");
8887 [ + + ]: 6501 : for (i = 0; i < 32; ++i) {
8888 [ + + + + : 6304 : if (g_opts.dhchap_dhgroups & SPDK_BIT(i)) {
+ + ]
8889 : 1182 : spdk_json_write_string(w, spdk_nvme_dhchap_get_dhgroup_name(i));
8890 : 66 : }
8891 : 352 : }
8892 : :
8893 : 197 : spdk_json_write_array_end(w);
8894 : 197 : spdk_json_write_object_end(w);
8895 : :
8896 : 197 : spdk_json_write_object_end(w);
8897 : 197 : }
8898 : :
8899 : : static void
8900 : 0 : bdev_nvme_discovery_config_json(struct spdk_json_write_ctx *w, struct discovery_ctx *ctx)
8901 : : {
8902 : 0 : struct spdk_nvme_transport_id trid;
8903 : :
8904 : 0 : spdk_json_write_object_begin(w);
8905 : :
8906 : 0 : spdk_json_write_named_string(w, "method", "bdev_nvme_start_discovery");
8907 : :
8908 : 0 : spdk_json_write_named_object_begin(w, "params");
8909 [ # # # # ]: 0 : spdk_json_write_named_string(w, "name", ctx->name);
8910 [ # # # # ]: 0 : spdk_json_write_named_string(w, "hostnqn", ctx->hostnqn);
8911 : :
8912 [ # # ]: 0 : trid = ctx->trid;
8913 [ # # ]: 0 : memset(trid.subnqn, 0, sizeof(trid.subnqn));
8914 : 0 : nvme_bdev_dump_trid_json(&trid, w);
8915 : :
8916 [ # # # # : 0 : spdk_json_write_named_bool(w, "wait_for_attach", ctx->wait_for_attach);
# # ]
8917 [ # # # # : 0 : spdk_json_write_named_int32(w, "ctrlr_loss_timeout_sec", ctx->bdev_opts.ctrlr_loss_timeout_sec);
# # ]
8918 [ # # # # : 0 : spdk_json_write_named_uint32(w, "reconnect_delay_sec", ctx->bdev_opts.reconnect_delay_sec);
# # ]
8919 : 0 : spdk_json_write_named_uint32(w, "fast_io_fail_timeout_sec",
8920 [ # # # # : 0 : ctx->bdev_opts.fast_io_fail_timeout_sec);
# # ]
8921 : 0 : spdk_json_write_object_end(w);
8922 : :
8923 : 0 : spdk_json_write_object_end(w);
8924 : 0 : }
8925 : :
8926 : : #ifdef SPDK_CONFIG_NVME_CUSE
8927 : : static void
8928 : 116 : nvme_ctrlr_cuse_config_json(struct spdk_json_write_ctx *w,
8929 : : struct nvme_ctrlr *nvme_ctrlr)
8930 : 116 : {
8931 : 116 : size_t cuse_name_size = 128;
8932 [ - + ]: 116 : char cuse_name[cuse_name_size];
8933 : :
8934 [ + - # # : 116 : if (spdk_nvme_cuse_get_ctrlr_name(nvme_ctrlr->ctrlr,
# # # # ]
8935 : 0 : cuse_name, &cuse_name_size) != 0) {
8936 : 116 : return;
8937 : : }
8938 : :
8939 : 0 : spdk_json_write_object_begin(w);
8940 : :
8941 : 0 : spdk_json_write_named_string(w, "method", "bdev_nvme_cuse_register");
8942 : :
8943 : 0 : spdk_json_write_named_object_begin(w, "params");
8944 [ # # # # : 0 : spdk_json_write_named_string(w, "name", nvme_ctrlr->nbdev_ctrlr->name);
# # # # ]
8945 : 0 : spdk_json_write_object_end(w);
8946 : :
8947 : 0 : spdk_json_write_object_end(w);
8948 [ # # ]: 0 : }
8949 : : #endif
8950 : :
8951 : : static void
8952 : 121 : nvme_ctrlr_config_json(struct spdk_json_write_ctx *w,
8953 : : struct nvme_ctrlr *nvme_ctrlr,
8954 : : struct nvme_path_id *path_id)
8955 : : {
8956 : : struct spdk_nvme_transport_id *trid;
8957 : : const struct spdk_nvme_ctrlr_opts *opts;
8958 : :
8959 [ - + - + : 121 : if (nvme_ctrlr->opts.from_discovery_service) {
# # # # #
# ]
8960 : : /* Do not emit an RPC for this - it will be implicitly
8961 : : * covered by a separate bdev_nvme_start_discovery or
8962 : : * bdev_nvme_start_mdns_discovery RPC.
8963 : : */
8964 : 0 : return;
8965 : : }
8966 : :
8967 [ # # ]: 121 : trid = &path_id->trid;
8968 : :
8969 : 121 : spdk_json_write_object_begin(w);
8970 : :
8971 : 121 : spdk_json_write_named_string(w, "method", "bdev_nvme_attach_controller");
8972 : :
8973 : 121 : spdk_json_write_named_object_begin(w, "params");
8974 [ # # # # : 121 : spdk_json_write_named_string(w, "name", nvme_ctrlr->nbdev_ctrlr->name);
# # # # ]
8975 : 121 : nvme_bdev_dump_trid_json(trid, w);
8976 : 126 : spdk_json_write_named_bool(w, "prchk_reftag",
8977 [ # # # # : 121 : (nvme_ctrlr->opts.prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_REFTAG) != 0);
# # # # ]
8978 : 126 : spdk_json_write_named_bool(w, "prchk_guard",
8979 [ # # # # : 121 : (nvme_ctrlr->opts.prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_GUARD) != 0);
# # # # ]
8980 [ # # # # : 121 : spdk_json_write_named_int32(w, "ctrlr_loss_timeout_sec", nvme_ctrlr->opts.ctrlr_loss_timeout_sec);
# # ]
8981 [ # # # # : 121 : spdk_json_write_named_uint32(w, "reconnect_delay_sec", nvme_ctrlr->opts.reconnect_delay_sec);
# # ]
8982 : 126 : spdk_json_write_named_uint32(w, "fast_io_fail_timeout_sec",
8983 [ # # # # : 5 : nvme_ctrlr->opts.fast_io_fail_timeout_sec);
# # ]
8984 [ + + # # : 121 : if (nvme_ctrlr->psk != NULL) {
# # ]
8985 [ # # # # ]: 9 : spdk_json_write_named_string(w, "psk", spdk_key_get_name(nvme_ctrlr->psk));
8986 : 0 : }
8987 [ + + # # : 121 : if (nvme_ctrlr->dhchap_key != NULL) {
# # ]
8988 : 0 : spdk_json_write_named_string(w, "dhchap_key",
8989 [ # # # # ]: 0 : spdk_key_get_name(nvme_ctrlr->dhchap_key));
8990 : 0 : }
8991 [ + + # # : 121 : if (nvme_ctrlr->dhchap_ctrlr_key != NULL) {
# # ]
8992 : 0 : spdk_json_write_named_string(w, "dhchap_ctrlr_key",
8993 [ # # # # ]: 0 : spdk_key_get_name(nvme_ctrlr->dhchap_ctrlr_key));
8994 : 0 : }
8995 [ # # # # ]: 121 : opts = spdk_nvme_ctrlr_get_opts(nvme_ctrlr->ctrlr);
8996 [ # # ]: 121 : spdk_json_write_named_string(w, "hostnqn", opts->hostnqn);
8997 [ - + # # : 121 : spdk_json_write_named_bool(w, "hdgst", opts->header_digest);
# # ]
8998 [ - + # # : 121 : spdk_json_write_named_bool(w, "ddgst", opts->data_digest);
# # ]
8999 [ + + # # : 121 : if (opts->src_addr[0] != '\0') {
# # # # #
# ]
9000 [ # # ]: 0 : spdk_json_write_named_string(w, "hostaddr", opts->src_addr);
9001 : 0 : }
9002 [ + + # # : 121 : if (opts->src_svcid[0] != '\0') {
# # # # #
# ]
9003 [ # # ]: 0 : spdk_json_write_named_string(w, "hostsvcid", opts->src_svcid);
9004 : 0 : }
9005 : :
9006 [ + + + - : 121 : if (nvme_ctrlr->opts.multipath) {
# # # # #
# ]
9007 : 121 : spdk_json_write_named_string(w, "multipath", "multipath");
9008 : 5 : }
9009 : 121 : spdk_json_write_object_end(w);
9010 : :
9011 : 121 : spdk_json_write_object_end(w);
9012 : 5 : }
9013 : :
9014 : : static void
9015 : 197 : bdev_nvme_hotplug_config_json(struct spdk_json_write_ctx *w)
9016 : : {
9017 : 197 : spdk_json_write_object_begin(w);
9018 : 197 : spdk_json_write_named_string(w, "method", "bdev_nvme_set_hotplug");
9019 : :
9020 : 197 : spdk_json_write_named_object_begin(w, "params");
9021 : 197 : spdk_json_write_named_uint64(w, "period_us", g_nvme_hotplug_poll_period_us);
9022 [ + + ]: 197 : spdk_json_write_named_bool(w, "enable", g_nvme_hotplug_enabled);
9023 : 197 : spdk_json_write_object_end(w);
9024 : :
9025 : 197 : spdk_json_write_object_end(w);
9026 : 197 : }
9027 : :
9028 : : static int
9029 : 197 : bdev_nvme_config_json(struct spdk_json_write_ctx *w)
9030 : : {
9031 : : struct nvme_bdev_ctrlr *nbdev_ctrlr;
9032 : : struct nvme_ctrlr *nvme_ctrlr;
9033 : : struct discovery_ctx *ctx;
9034 : : struct nvme_path_id *path_id;
9035 : :
9036 : 197 : bdev_nvme_opts_config_json(w);
9037 : :
9038 [ + + ]: 197 : pthread_mutex_lock(&g_bdev_nvme_mutex);
9039 : :
9040 [ + + # # : 318 : TAILQ_FOREACH(nbdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) {
# # # # ]
9041 [ + + # # : 242 : TAILQ_FOREACH(nvme_ctrlr, &nbdev_ctrlr->ctrlrs, tailq) {
# # # # #
# # # #
# ]
9042 [ # # # # ]: 121 : path_id = nvme_ctrlr->active_path_id;
9043 [ + + # # : 121 : assert(path_id == TAILQ_FIRST(&nvme_ctrlr->trids));
# # # # #
# ]
9044 : 121 : nvme_ctrlr_config_json(w, nvme_ctrlr, path_id);
9045 : :
9046 [ # # # # : 121 : path_id = TAILQ_NEXT(path_id, link);
# # ]
9047 [ - + ]: 121 : while (path_id != NULL) {
9048 : 0 : nvme_ctrlr_config_json(w, nvme_ctrlr, path_id);
9049 [ # # # # : 0 : path_id = TAILQ_NEXT(path_id, link);
# # ]
9050 : : }
9051 : :
9052 : : #ifdef SPDK_CONFIG_NVME_CUSE
9053 : 116 : nvme_ctrlr_cuse_config_json(w, nvme_ctrlr);
9054 : : #endif
9055 : 5 : }
9056 : 5 : }
9057 : :
9058 [ - + # # : 197 : TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
# # # # ]
9059 [ # # # # : 0 : if (!ctx->from_mdns_discovery_service) {
# # # # ]
9060 : 0 : bdev_nvme_discovery_config_json(w, ctx);
9061 : 0 : }
9062 : 0 : }
9063 : :
9064 : 197 : bdev_nvme_mdns_discovery_config_json(w);
9065 : :
9066 : : /* Dump as last parameter to give all NVMe bdevs chance to be constructed
9067 : : * before enabling hotplug poller.
9068 : : */
9069 : 197 : bdev_nvme_hotplug_config_json(w);
9070 : :
9071 [ + + ]: 197 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
9072 : 197 : return 0;
9073 : : }
9074 : :
9075 : : struct spdk_nvme_ctrlr *
9076 : 11 : bdev_nvme_get_ctrlr(struct spdk_bdev *bdev)
9077 : : {
9078 : : struct nvme_bdev *nbdev;
9079 : : struct nvme_ns *nvme_ns;
9080 : :
9081 [ + - - + : 11 : if (!bdev || bdev->module != &nvme_if) {
# # # # ]
9082 : 0 : return NULL;
9083 : : }
9084 : :
9085 : 11 : nbdev = SPDK_CONTAINEROF(bdev, struct nvme_bdev, disk);
9086 [ # # # # : 11 : nvme_ns = TAILQ_FIRST(&nbdev->nvme_ns_list);
# # ]
9087 [ + + # # ]: 11 : assert(nvme_ns != NULL);
9088 : :
9089 [ # # # # : 11 : return nvme_ns->ctrlr->ctrlr;
# # # # ]
9090 : 2 : }
9091 : :
9092 : : static bool
9093 : 528 : nvme_io_path_is_current(struct nvme_io_path *io_path)
9094 : : {
9095 : : const struct nvme_bdev_channel *nbdev_ch;
9096 : : bool current;
9097 : :
9098 [ + + ]: 528 : if (!nvme_io_path_is_available(io_path)) {
9099 : 136 : return false;
9100 : : }
9101 : :
9102 [ # # # # ]: 392 : nbdev_ch = io_path->nbdev_ch;
9103 [ + + ]: 392 : if (nbdev_ch == NULL) {
9104 : 4 : current = false;
9105 [ + + # # : 389 : } else if (nbdev_ch->mp_policy == BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE) {
# # ]
9106 : 180 : struct nvme_io_path *optimized_io_path = NULL;
9107 : :
9108 [ + + # # : 384 : STAILQ_FOREACH(optimized_io_path, &nbdev_ch->io_path_list, stailq) {
# # # # #
# # # #
# ]
9109 [ + + # # : 308 : if (optimized_io_path->nvme_ns->ana_state == SPDK_NVME_ANA_OPTIMIZED_STATE) {
# # # # #
# ]
9110 : 104 : break;
9111 : : }
9112 : 3 : }
9113 : :
9114 : : /* A non-optimized path is only current if there are no optimized paths. */
9115 [ + + + + : 180 : current = (io_path->nvme_ns->ana_state == SPDK_NVME_ANA_OPTIMIZED_STATE) ||
# # # # #
# ]
9116 : 2 : (optimized_io_path == NULL);
9117 : 3 : } else {
9118 [ + + # # : 208 : if (nbdev_ch->current_io_path) {
# # ]
9119 [ # # # # ]: 124 : current = (io_path == nbdev_ch->current_io_path);
9120 : 1 : } else {
9121 : : struct nvme_io_path *first_path;
9122 : :
9123 : : /* We arrived here as there are no optimized paths for active-passive
9124 : : * mode. Check if this io_path is the first one available on the list.
9125 : : */
9126 : 84 : current = false;
9127 [ + + # # : 84 : STAILQ_FOREACH(first_path, &nbdev_ch->io_path_list, stailq) {
# # # # #
# # # #
# ]
9128 [ + - ]: 84 : if (nvme_io_path_is_available(first_path)) {
9129 : 84 : current = (io_path == first_path);
9130 : 84 : break;
9131 : : }
9132 : 0 : }
9133 : : }
9134 : : }
9135 : :
9136 [ # # ]: 392 : return current;
9137 : 12 : }
9138 : :
9139 : : static struct nvme_ctrlr *
9140 : 40 : bdev_nvme_next_ctrlr_unsafe(struct nvme_bdev_ctrlr *nbdev_ctrlr, struct nvme_ctrlr *prev)
9141 : : {
9142 : : struct nvme_ctrlr *next;
9143 : :
9144 : : /* Must be called under g_bdev_nvme_mutex */
9145 [ + + # # : 40 : next = prev != NULL ? TAILQ_NEXT(prev, tailq) : TAILQ_FIRST(&nbdev_ctrlr->ctrlrs);
# # # # #
# # # #
# ]
9146 [ + + ]: 40 : while (next != NULL) {
9147 : : /* ref can be 0 when the ctrlr was released, but hasn't been detached yet */
9148 [ - + # # ]: 28 : pthread_mutex_lock(&next->mutex);
9149 [ + - # # : 28 : if (next->ref > 0) {
# # ]
9150 [ # # # # ]: 28 : next->ref++;
9151 [ - + # # ]: 28 : pthread_mutex_unlock(&next->mutex);
9152 : 28 : return next;
9153 : : }
9154 : :
9155 [ # # # # ]: 0 : pthread_mutex_unlock(&next->mutex);
9156 [ # # # # : 0 : next = TAILQ_NEXT(next, tailq);
# # ]
9157 : : }
9158 : :
9159 : 12 : return NULL;
9160 : 0 : }
9161 : :
9162 : : struct bdev_nvme_set_keys_ctx {
9163 : : struct nvme_ctrlr *nctrlr;
9164 : : struct spdk_key *dhchap_key;
9165 : : struct spdk_key *dhchap_ctrlr_key;
9166 : : struct spdk_thread *thread;
9167 : : bdev_nvme_set_keys_cb cb_fn;
9168 : : void *cb_ctx;
9169 : : int status;
9170 : : };
9171 : :
9172 : : static void
9173 : 28 : bdev_nvme_free_set_keys_ctx(struct bdev_nvme_set_keys_ctx *ctx)
9174 : : {
9175 [ - + ]: 28 : if (ctx == NULL) {
9176 : 0 : return;
9177 : : }
9178 : :
9179 [ # # # # ]: 28 : spdk_keyring_put_key(ctx->dhchap_key);
9180 [ # # # # ]: 28 : spdk_keyring_put_key(ctx->dhchap_ctrlr_key);
9181 : 28 : free(ctx);
9182 : 0 : }
9183 : :
9184 : : static void
9185 : 28 : _bdev_nvme_set_keys_done(void *_ctx)
9186 : : {
9187 : 28 : struct bdev_nvme_set_keys_ctx *ctx = _ctx;
9188 : :
9189 [ # # # # : 28 : ctx->cb_fn(ctx->cb_ctx, ctx->status);
# # # # #
# # # # #
# # ]
9190 : :
9191 [ + + # # : 28 : if (ctx->nctrlr != NULL) {
# # ]
9192 [ # # # # ]: 16 : nvme_ctrlr_put_ref(ctx->nctrlr);
9193 : 0 : }
9194 : 28 : bdev_nvme_free_set_keys_ctx(ctx);
9195 : 28 : }
9196 : :
9197 : : static void
9198 : 28 : bdev_nvme_set_keys_done(struct bdev_nvme_set_keys_ctx *ctx, int status)
9199 : : {
9200 [ # # # # ]: 28 : ctx->status = status;
9201 [ # # # # ]: 28 : spdk_thread_exec_msg(ctx->thread, _bdev_nvme_set_keys_done, ctx);
9202 : 28 : }
9203 : :
9204 : : static void bdev_nvme_authenticate_ctrlr(struct bdev_nvme_set_keys_ctx *ctx);
9205 : :
9206 : : static void
9207 : 12 : bdev_nvme_authenticate_ctrlr_continue(struct bdev_nvme_set_keys_ctx *ctx)
9208 : : {
9209 : : struct nvme_ctrlr *next;
9210 : :
9211 [ - + ]: 12 : pthread_mutex_lock(&g_bdev_nvme_mutex);
9212 [ # # # # ]: 12 : next = bdev_nvme_next_ctrlr_unsafe(NULL, ctx->nctrlr);
9213 [ - + ]: 12 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
9214 : :
9215 [ # # # # ]: 12 : nvme_ctrlr_put_ref(ctx->nctrlr);
9216 [ # # # # ]: 12 : ctx->nctrlr = next;
9217 : :
9218 [ + - ]: 12 : if (next == NULL) {
9219 : 12 : bdev_nvme_set_keys_done(ctx, 0);
9220 : 0 : } else {
9221 : 0 : bdev_nvme_authenticate_ctrlr(ctx);
9222 : : }
9223 : 12 : }
9224 : :
9225 : : static void
9226 : 8 : bdev_nvme_authenticate_qpairs_done(struct spdk_io_channel_iter *i, int status)
9227 : : {
9228 : 8 : struct bdev_nvme_set_keys_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
9229 : :
9230 [ - + ]: 8 : if (status != 0) {
9231 : 0 : bdev_nvme_set_keys_done(ctx, status);
9232 : 0 : return;
9233 : : }
9234 : 8 : bdev_nvme_authenticate_ctrlr_continue(ctx);
9235 : 0 : }
9236 : :
9237 : : static void
9238 : 0 : bdev_nvme_authenticate_qpair_done(void *ctx, int status)
9239 : : {
9240 : 0 : spdk_for_each_channel_continue(ctx, status);
9241 : 0 : }
9242 : :
9243 : : static void
9244 : 0 : bdev_nvme_authenticate_qpair(struct spdk_io_channel_iter *i)
9245 : : {
9246 : 0 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
9247 : 0 : struct nvme_ctrlr_channel *ctrlr_ch = spdk_io_channel_get_ctx(ch);
9248 [ # # # # ]: 0 : struct nvme_qpair *qpair = ctrlr_ch->qpair;
9249 : : int rc;
9250 : :
9251 [ # # ]: 0 : if (!nvme_qpair_is_connected(qpair)) {
9252 : 0 : spdk_for_each_channel_continue(i, 0);
9253 : 0 : return;
9254 : : }
9255 : :
9256 [ # # # # ]: 0 : rc = spdk_nvme_qpair_authenticate(qpair->qpair, bdev_nvme_authenticate_qpair_done, i);
9257 [ # # ]: 0 : if (rc != 0) {
9258 : 0 : spdk_for_each_channel_continue(i, rc);
9259 : 0 : }
9260 : 0 : }
9261 : :
9262 : : static void
9263 : 20 : bdev_nvme_authenticate_ctrlr_done(void *_ctx, int status)
9264 : : {
9265 : 20 : struct bdev_nvme_set_keys_ctx *ctx = _ctx;
9266 : :
9267 [ + + ]: 20 : if (status != 0) {
9268 : 12 : bdev_nvme_set_keys_done(ctx, status);
9269 : 12 : return;
9270 : : }
9271 : :
9272 [ # # # # ]: 8 : spdk_for_each_channel(ctx->nctrlr, bdev_nvme_authenticate_qpair, ctx,
9273 : : bdev_nvme_authenticate_qpairs_done);
9274 : 0 : }
9275 : :
9276 : : static void
9277 : 28 : bdev_nvme_authenticate_ctrlr(struct bdev_nvme_set_keys_ctx *ctx)
9278 : : {
9279 : 28 : struct spdk_nvme_ctrlr_key_opts opts = {};
9280 [ # # # # ]: 28 : struct nvme_ctrlr *nctrlr = ctx->nctrlr;
9281 : : int rc;
9282 : :
9283 : 28 : opts.size = SPDK_SIZEOF(&opts, dhchap_ctrlr_key);
9284 [ # # # # : 28 : opts.dhchap_key = ctx->dhchap_key;
# # ]
9285 [ # # # # : 28 : opts.dhchap_ctrlr_key = ctx->dhchap_ctrlr_key;
# # ]
9286 [ # # # # ]: 28 : rc = spdk_nvme_ctrlr_set_keys(nctrlr->ctrlr, &opts);
9287 [ - + ]: 28 : if (rc != 0) {
9288 : 0 : bdev_nvme_set_keys_done(ctx, rc);
9289 : 0 : return;
9290 : : }
9291 : :
9292 [ + + # # : 28 : if (ctx->dhchap_key != NULL) {
# # ]
9293 [ # # # # ]: 24 : rc = spdk_nvme_ctrlr_authenticate(nctrlr->ctrlr,
9294 : 0 : bdev_nvme_authenticate_ctrlr_done, ctx);
9295 [ + + ]: 24 : if (rc != 0) {
9296 : 4 : bdev_nvme_set_keys_done(ctx, rc);
9297 : 0 : }
9298 : 0 : } else {
9299 : 4 : bdev_nvme_authenticate_ctrlr_continue(ctx);
9300 : : }
9301 : 0 : }
9302 : :
9303 : : int
9304 : 28 : bdev_nvme_set_keys(const char *name, const char *dhchap_key, const char *dhchap_ctrlr_key,
9305 : : bdev_nvme_set_keys_cb cb_fn, void *cb_ctx)
9306 : : {
9307 : : struct bdev_nvme_set_keys_ctx *ctx;
9308 : : struct nvme_bdev_ctrlr *nbdev_ctrlr;
9309 : : struct nvme_ctrlr *nctrlr;
9310 : :
9311 : 28 : ctx = calloc(1, sizeof(*ctx));
9312 [ - + ]: 28 : if (ctx == NULL) {
9313 : 0 : return -ENOMEM;
9314 : : }
9315 : :
9316 [ + + ]: 28 : if (dhchap_key != NULL) {
9317 [ # # # # ]: 24 : ctx->dhchap_key = spdk_keyring_get_key(dhchap_key);
9318 [ - + # # : 24 : if (ctx->dhchap_key == NULL) {
# # ]
9319 : 0 : SPDK_ERRLOG("Could not find key %s for bdev %s\n", dhchap_key, name);
9320 : 0 : bdev_nvme_free_set_keys_ctx(ctx);
9321 : 0 : return -ENOKEY;
9322 : : }
9323 : 0 : }
9324 [ + + ]: 28 : if (dhchap_ctrlr_key != NULL) {
9325 [ # # # # ]: 24 : ctx->dhchap_ctrlr_key = spdk_keyring_get_key(dhchap_ctrlr_key);
9326 [ - + # # : 24 : if (ctx->dhchap_ctrlr_key == NULL) {
# # ]
9327 : 0 : SPDK_ERRLOG("Could not find key %s for bdev %s\n", dhchap_ctrlr_key, name);
9328 : 0 : bdev_nvme_free_set_keys_ctx(ctx);
9329 : 0 : return -ENOKEY;
9330 : : }
9331 : 0 : }
9332 : :
9333 [ - + ]: 28 : pthread_mutex_lock(&g_bdev_nvme_mutex);
9334 : 28 : nbdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name);
9335 [ - + ]: 28 : if (nbdev_ctrlr == NULL) {
9336 : 0 : SPDK_ERRLOG("Could not find bdev_ctrlr %s\n", name);
9337 [ # # ]: 0 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
9338 : 0 : bdev_nvme_free_set_keys_ctx(ctx);
9339 : 0 : return -ENODEV;
9340 : : }
9341 : 28 : nctrlr = bdev_nvme_next_ctrlr_unsafe(nbdev_ctrlr, NULL);
9342 [ - + ]: 28 : if (nctrlr == NULL) {
9343 : 0 : SPDK_ERRLOG("Could not find any nvme_ctrlrs on bdev_ctrlr %s\n", name);
9344 [ # # ]: 0 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
9345 : 0 : bdev_nvme_free_set_keys_ctx(ctx);
9346 : 0 : return -ENODEV;
9347 : : }
9348 [ - + ]: 28 : pthread_mutex_unlock(&g_bdev_nvme_mutex);
9349 : :
9350 [ # # # # ]: 28 : ctx->nctrlr = nctrlr;
9351 [ # # # # ]: 28 : ctx->cb_fn = cb_fn;
9352 [ # # # # ]: 28 : ctx->cb_ctx = cb_ctx;
9353 [ # # # # ]: 28 : ctx->thread = spdk_get_thread();
9354 : :
9355 : 28 : bdev_nvme_authenticate_ctrlr(ctx);
9356 : :
9357 : 28 : return 0;
9358 : 0 : }
9359 : :
9360 : : void
9361 : 480 : nvme_io_path_info_json(struct spdk_json_write_ctx *w, struct nvme_io_path *io_path)
9362 : : {
9363 [ # # # # ]: 480 : struct nvme_ns *nvme_ns = io_path->nvme_ns;
9364 [ # # # # : 480 : struct nvme_ctrlr *nvme_ctrlr = io_path->qpair->ctrlr;
# # # # ]
9365 : : const struct spdk_nvme_ctrlr_data *cdata;
9366 : : const struct spdk_nvme_transport_id *trid;
9367 : : const char *adrfam_str;
9368 : :
9369 : 480 : spdk_json_write_object_begin(w);
9370 : :
9371 [ # # # # : 480 : spdk_json_write_named_string(w, "bdev_name", nvme_ns->bdev->disk.name);
# # # # #
# ]
9372 : :
9373 [ # # # # ]: 480 : cdata = spdk_nvme_ctrlr_get_data(nvme_ctrlr->ctrlr);
9374 [ # # # # ]: 480 : trid = spdk_nvme_ctrlr_get_transport_id(nvme_ctrlr->ctrlr);
9375 : :
9376 [ # # # # ]: 480 : spdk_json_write_named_uint32(w, "cntlid", cdata->cntlid);
9377 : 480 : spdk_json_write_named_bool(w, "current", nvme_io_path_is_current(io_path));
9378 [ # # # # ]: 480 : spdk_json_write_named_bool(w, "connected", nvme_qpair_is_connected(io_path->qpair));
9379 : 480 : spdk_json_write_named_bool(w, "accessible", nvme_ns_is_accessible(nvme_ns));
9380 : :
9381 : 480 : spdk_json_write_named_object_begin(w, "transport");
9382 [ # # ]: 480 : spdk_json_write_named_string(w, "trtype", trid->trstring);
9383 [ # # ]: 480 : spdk_json_write_named_string(w, "traddr", trid->traddr);
9384 [ + - # # : 480 : if (trid->trsvcid[0] != '\0') {
# # # # #
# ]
9385 [ # # ]: 480 : spdk_json_write_named_string(w, "trsvcid", trid->trsvcid);
9386 : 0 : }
9387 [ # # # # ]: 480 : adrfam_str = spdk_nvme_transport_id_adrfam_str(trid->adrfam);
9388 [ + - ]: 480 : if (adrfam_str) {
9389 : 480 : spdk_json_write_named_string(w, "adrfam", adrfam_str);
9390 : 0 : }
9391 : 480 : spdk_json_write_object_end(w);
9392 : :
9393 : 480 : spdk_json_write_object_end(w);
9394 : 480 : }
9395 : :
9396 : : void
9397 : 77 : bdev_nvme_get_discovery_info(struct spdk_json_write_ctx *w)
9398 : : {
9399 : : struct discovery_ctx *ctx;
9400 : : struct discovery_entry_ctx *entry_ctx;
9401 : :
9402 : 77 : spdk_json_write_array_begin(w);
9403 [ + + # # : 150 : TAILQ_FOREACH(ctx, &g_discovery_ctxs, tailq) {
# # # # ]
9404 : 73 : spdk_json_write_object_begin(w);
9405 [ # # # # ]: 73 : spdk_json_write_named_string(w, "name", ctx->name);
9406 : :
9407 : 73 : spdk_json_write_named_object_begin(w, "trid");
9408 [ # # ]: 73 : nvme_bdev_dump_trid_json(&ctx->trid, w);
9409 : 73 : spdk_json_write_object_end(w);
9410 : :
9411 : 73 : spdk_json_write_named_array_begin(w, "referrals");
9412 [ + + # # : 171 : TAILQ_FOREACH(entry_ctx, &ctx->discovery_entry_ctxs, tailq) {
# # # # #
# # # #
# ]
9413 : 98 : spdk_json_write_object_begin(w);
9414 : 98 : spdk_json_write_named_object_begin(w, "trid");
9415 [ # # ]: 98 : nvme_bdev_dump_trid_json(&entry_ctx->trid, w);
9416 : 98 : spdk_json_write_object_end(w);
9417 : 98 : spdk_json_write_object_end(w);
9418 : 0 : }
9419 : 73 : spdk_json_write_array_end(w);
9420 : :
9421 : 73 : spdk_json_write_object_end(w);
9422 : 0 : }
9423 : 77 : spdk_json_write_array_end(w);
9424 : 77 : }
9425 : :
9426 : 2149 : SPDK_LOG_REGISTER_COMPONENT(bdev_nvme)
9427 : :
9428 : : static void
9429 : 1973 : bdev_nvme_trace(void)
9430 : : {
9431 : 1973 : struct spdk_trace_tpoint_opts opts[] = {
9432 : : {
9433 : : "BDEV_NVME_IO_START", TRACE_BDEV_NVME_IO_START,
9434 : : OWNER_TYPE_NONE, OBJECT_BDEV_NVME_IO, 1,
9435 : : {{ "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 }}
9436 : : },
9437 : : {
9438 : : "BDEV_NVME_IO_DONE", TRACE_BDEV_NVME_IO_DONE,
9439 : : OWNER_TYPE_NONE, OBJECT_BDEV_NVME_IO, 0,
9440 : : {{ "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 }}
9441 : : }
9442 : : };
9443 : :
9444 : :
9445 : 1973 : spdk_trace_register_object(OBJECT_BDEV_NVME_IO, 'N');
9446 : 1973 : spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts));
9447 : 1973 : spdk_trace_tpoint_register_relation(TRACE_NVME_PCIE_SUBMIT, OBJECT_BDEV_NVME_IO, 0);
9448 : 1973 : spdk_trace_tpoint_register_relation(TRACE_NVME_TCP_SUBMIT, OBJECT_BDEV_NVME_IO, 0);
9449 : 1973 : spdk_trace_tpoint_register_relation(TRACE_NVME_PCIE_COMPLETE, OBJECT_BDEV_NVME_IO, 0);
9450 : 1973 : spdk_trace_tpoint_register_relation(TRACE_NVME_TCP_COMPLETE, OBJECT_BDEV_NVME_IO, 0);
9451 : 1973 : }
9452 : 2149 : SPDK_TRACE_REGISTER_FN(bdev_nvme_trace, "bdev_nvme", TRACE_GROUP_BDEV_NVME)
|