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