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