1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. All rights reserved. 5 * Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "bdev_nvme.h" 37 #include "bdev_ocssd.h" 38 39 #include "spdk/accel_engine.h" 40 #include "spdk/config.h" 41 #include "spdk/endian.h" 42 #include "spdk/bdev.h" 43 #include "spdk/json.h" 44 #include "spdk/nvme.h" 45 #include "spdk/nvme_ocssd.h" 46 #include "spdk/nvme_zns.h" 47 #include "spdk/thread.h" 48 #include "spdk/string.h" 49 #include "spdk/util.h" 50 51 #include "spdk/bdev_module.h" 52 #include "spdk/log.h" 53 54 #define SPDK_BDEV_NVME_DEFAULT_DELAY_CMD_SUBMIT true 55 #define SPDK_BDEV_NVME_DEFAULT_KEEP_ALIVE_TIMEOUT_IN_MS (10000) 56 57 static int bdev_nvme_config_json(struct spdk_json_write_ctx *w); 58 59 struct nvme_bdev_io { 60 /** array of iovecs to transfer. */ 61 struct iovec *iovs; 62 63 /** Number of iovecs in iovs array. */ 64 int iovcnt; 65 66 /** Current iovec position. */ 67 int iovpos; 68 69 /** Offset in current iovec. */ 70 uint32_t iov_offset; 71 72 /** array of iovecs to transfer. */ 73 struct iovec *fused_iovs; 74 75 /** Number of iovecs in iovs array. */ 76 int fused_iovcnt; 77 78 /** Current iovec position. */ 79 int fused_iovpos; 80 81 /** Offset in current iovec. */ 82 uint32_t fused_iov_offset; 83 84 /** Saved status for admin passthru completion event, PI error verification, or intermediate compare-and-write status */ 85 struct spdk_nvme_cpl cpl; 86 87 /** Originating thread */ 88 struct spdk_thread *orig_thread; 89 90 /** Keeps track if first of fused commands was submitted */ 91 bool first_fused_submitted; 92 93 /** Temporary pointer to zone report buffer */ 94 struct spdk_nvme_zns_zone_report *zone_report_buf; 95 96 /** Keep track of how many zones that have been copied to the spdk_bdev_zone_info struct */ 97 uint64_t handled_zones; 98 }; 99 100 struct nvme_probe_ctx { 101 size_t count; 102 struct spdk_nvme_transport_id trids[NVME_MAX_CONTROLLERS]; 103 struct spdk_nvme_host_id hostids[NVME_MAX_CONTROLLERS]; 104 const char *names[NVME_MAX_CONTROLLERS]; 105 uint32_t prchk_flags[NVME_MAX_CONTROLLERS]; 106 const char *hostnqn; 107 }; 108 109 struct nvme_probe_skip_entry { 110 struct spdk_nvme_transport_id trid; 111 TAILQ_ENTRY(nvme_probe_skip_entry) tailq; 112 }; 113 /* All the controllers deleted by users via RPC are skipped by hotplug monitor */ 114 static TAILQ_HEAD(, nvme_probe_skip_entry) g_skipped_nvme_ctrlrs = TAILQ_HEAD_INITIALIZER( 115 g_skipped_nvme_ctrlrs); 116 117 static struct spdk_bdev_nvme_opts g_opts = { 118 .action_on_timeout = SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE, 119 .timeout_us = 0, 120 .keep_alive_timeout_ms = SPDK_BDEV_NVME_DEFAULT_KEEP_ALIVE_TIMEOUT_IN_MS, 121 .retry_count = 4, 122 .arbitration_burst = 0, 123 .low_priority_weight = 0, 124 .medium_priority_weight = 0, 125 .high_priority_weight = 0, 126 .nvme_adminq_poll_period_us = 10000ULL, 127 .nvme_ioq_poll_period_us = 0, 128 .io_queue_requests = 0, 129 .delay_cmd_submit = SPDK_BDEV_NVME_DEFAULT_DELAY_CMD_SUBMIT, 130 }; 131 132 #define NVME_HOTPLUG_POLL_PERIOD_MAX 10000000ULL 133 #define NVME_HOTPLUG_POLL_PERIOD_DEFAULT 100000ULL 134 135 static int g_hot_insert_nvme_controller_index = 0; 136 static uint64_t g_nvme_hotplug_poll_period_us = NVME_HOTPLUG_POLL_PERIOD_DEFAULT; 137 static bool g_nvme_hotplug_enabled = false; 138 static struct spdk_thread *g_bdev_nvme_init_thread; 139 static struct spdk_poller *g_hotplug_poller; 140 static struct spdk_poller *g_hotplug_probe_poller; 141 static struct spdk_nvme_probe_ctx *g_hotplug_probe_ctx; 142 143 static void nvme_ctrlr_populate_namespaces(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, 144 struct nvme_async_probe_ctx *ctx); 145 static void nvme_ctrlr_populate_namespaces_done(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, 146 struct nvme_async_probe_ctx *ctx); 147 static int bdev_nvme_library_init(void); 148 static void bdev_nvme_library_fini(void); 149 static int bdev_nvme_readv(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 150 struct nvme_bdev_io *bio, 151 struct iovec *iov, int iovcnt, void *md, uint64_t lba_count, uint64_t lba, 152 uint32_t flags); 153 static int bdev_nvme_no_pi_readv(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 154 struct nvme_bdev_io *bio, 155 struct iovec *iov, int iovcnt, void *md, uint64_t lba_count, uint64_t lba); 156 static int bdev_nvme_writev(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 157 struct nvme_bdev_io *bio, 158 struct iovec *iov, int iovcnt, void *md, uint64_t lba_count, uint64_t lba, 159 uint32_t flags); 160 static int bdev_nvme_zone_appendv(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 161 struct nvme_bdev_io *bio, 162 struct iovec *iov, int iovcnt, void *md, uint64_t lba_count, 163 uint64_t zslba, uint32_t flags); 164 static int bdev_nvme_comparev(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 165 struct nvme_bdev_io *bio, 166 struct iovec *iov, int iovcnt, void *md, uint64_t lba_count, uint64_t lba, 167 uint32_t flags); 168 static int bdev_nvme_comparev_and_writev(struct spdk_nvme_ns *ns, 169 struct spdk_nvme_qpair *qpair, 170 struct nvme_bdev_io *bio, struct iovec *cmp_iov, int cmp_iovcnt, struct iovec *write_iov, 171 int write_iovcnt, void *md, uint64_t lba_count, uint64_t lba, 172 uint32_t flags); 173 static int bdev_nvme_get_zone_info(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 174 struct nvme_bdev_io *bio, uint64_t zone_id, uint32_t num_zones, 175 struct spdk_bdev_zone_info *info); 176 static int bdev_nvme_zone_management(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 177 struct nvme_bdev_io *bio, uint64_t zone_id, 178 enum spdk_bdev_zone_action action); 179 static int bdev_nvme_admin_passthru(struct nvme_io_path *io_path, 180 struct nvme_bdev_io *bio, 181 struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes); 182 static int bdev_nvme_io_passthru(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 183 struct nvme_bdev_io *bio, 184 struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes); 185 static int bdev_nvme_io_passthru_md(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 186 struct nvme_bdev_io *bio, 187 struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes, void *md_buf, size_t md_len); 188 static int bdev_nvme_abort(struct nvme_io_path *io_path, 189 struct nvme_bdev_io *bio, struct nvme_bdev_io *bio_to_abort); 190 static int bdev_nvme_reset(struct nvme_io_path *io_path, struct spdk_bdev_io *bdev_io); 191 static int bdev_nvme_failover(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, bool remove); 192 static void remove_cb(void *cb_ctx, struct spdk_nvme_ctrlr *ctrlr); 193 194 typedef void (*populate_namespace_fn)(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, 195 struct nvme_bdev_ns *nvme_ns, struct nvme_async_probe_ctx *ctx); 196 static void nvme_ctrlr_populate_standard_namespace(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, 197 struct nvme_bdev_ns *nvme_ns, struct nvme_async_probe_ctx *ctx); 198 199 static populate_namespace_fn g_populate_namespace_fn[] = { 200 NULL, 201 nvme_ctrlr_populate_standard_namespace, 202 bdev_ocssd_populate_namespace, 203 }; 204 205 typedef void (*depopulate_namespace_fn)(struct nvme_bdev_ns *nvme_ns); 206 static void nvme_ctrlr_depopulate_standard_namespace(struct nvme_bdev_ns *nvme_ns); 207 208 static depopulate_namespace_fn g_depopulate_namespace_fn[] = { 209 NULL, 210 nvme_ctrlr_depopulate_standard_namespace, 211 bdev_ocssd_depopulate_namespace, 212 }; 213 214 typedef void (*config_json_namespace_fn)(struct spdk_json_write_ctx *w, 215 struct nvme_bdev_ns *nvme_ns); 216 static void nvme_ctrlr_config_json_standard_namespace(struct spdk_json_write_ctx *w, 217 struct nvme_bdev_ns *nvme_ns); 218 219 static config_json_namespace_fn g_config_json_namespace_fn[] = { 220 NULL, 221 nvme_ctrlr_config_json_standard_namespace, 222 bdev_ocssd_namespace_config_json, 223 }; 224 225 struct spdk_nvme_qpair * 226 bdev_nvme_get_io_qpair(struct spdk_io_channel *io_path_ch) 227 { 228 struct nvme_io_path *io_path; 229 230 assert(io_path_ch != NULL); 231 232 io_path = spdk_io_channel_get_ctx(io_path_ch); 233 234 return io_path->qpair; 235 } 236 237 static int 238 bdev_nvme_get_ctx_size(void) 239 { 240 return sizeof(struct nvme_bdev_io); 241 } 242 243 static struct spdk_bdev_module nvme_if = { 244 .name = "nvme", 245 .async_fini = true, 246 .module_init = bdev_nvme_library_init, 247 .module_fini = bdev_nvme_library_fini, 248 .config_json = bdev_nvme_config_json, 249 .get_ctx_size = bdev_nvme_get_ctx_size, 250 251 }; 252 SPDK_BDEV_MODULE_REGISTER(nvme, &nvme_if) 253 254 static inline bool 255 bdev_nvme_find_io_path(struct nvme_bdev *nbdev, struct nvme_io_path *io_path, 256 struct spdk_nvme_ns **_ns, struct spdk_nvme_qpair **_qpair) 257 { 258 if (spdk_unlikely(io_path->qpair == NULL)) { 259 /* The device is currently resetting. */ 260 return false; 261 } 262 263 *_ns = nbdev->nvme_ns->ns; 264 *_qpair = io_path->qpair; 265 return true; 266 } 267 268 static inline bool 269 bdev_nvme_find_admin_path(struct nvme_io_path *io_path, 270 struct nvme_bdev_ctrlr **_nvme_bdev_ctrlr) 271 { 272 *_nvme_bdev_ctrlr = io_path->ctrlr; 273 return true; 274 } 275 276 static inline void 277 bdev_nvme_io_complete_nvme_status(struct nvme_bdev_io *bio, 278 const struct spdk_nvme_cpl *cpl) 279 { 280 spdk_bdev_io_complete_nvme_status(spdk_bdev_io_from_ctx(bio), cpl->cdw0, 281 cpl->status.sct, cpl->status.sc); 282 } 283 284 static inline void 285 bdev_nvme_io_complete(struct nvme_bdev_io *bio, int rc) 286 { 287 enum spdk_bdev_io_status io_status; 288 289 if (rc == 0) { 290 io_status = SPDK_BDEV_IO_STATUS_SUCCESS; 291 } else if (rc == -ENOMEM) { 292 io_status = SPDK_BDEV_IO_STATUS_NOMEM; 293 } else { 294 io_status = SPDK_BDEV_IO_STATUS_FAILED; 295 } 296 297 spdk_bdev_io_complete(spdk_bdev_io_from_ctx(bio), io_status); 298 } 299 300 static void 301 bdev_nvme_disconnected_qpair_cb(struct spdk_nvme_qpair *qpair, void *poll_group_ctx) 302 { 303 int rc; 304 305 SPDK_DEBUGLOG(bdev_nvme, "qpair %p is disconnected, attempting reconnect.\n", qpair); 306 /* 307 * Currently, just try to reconnect indefinitely. If we are doing a reset, the reset will 308 * reconnect a qpair and we will stop getting a callback for this one. 309 */ 310 rc = spdk_nvme_ctrlr_reconnect_io_qpair(qpair); 311 if (rc != 0) { 312 SPDK_WARNLOG("Failed to reconnect to qpair %p, errno %d\n", qpair, -rc); 313 } 314 } 315 316 static int 317 bdev_nvme_poll(void *arg) 318 { 319 struct nvme_bdev_poll_group *group = arg; 320 int64_t num_completions; 321 322 if (group->collect_spin_stat && group->start_ticks == 0) { 323 group->start_ticks = spdk_get_ticks(); 324 } 325 326 num_completions = spdk_nvme_poll_group_process_completions(group->group, 0, 327 bdev_nvme_disconnected_qpair_cb); 328 if (group->collect_spin_stat) { 329 if (num_completions > 0) { 330 if (group->end_ticks != 0) { 331 group->spin_ticks += (group->end_ticks - group->start_ticks); 332 group->end_ticks = 0; 333 } 334 group->start_ticks = 0; 335 } else { 336 group->end_ticks = spdk_get_ticks(); 337 } 338 } 339 340 return num_completions > 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE; 341 } 342 343 static int 344 bdev_nvme_poll_adminq(void *arg) 345 { 346 int32_t rc; 347 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr = arg; 348 349 assert(nvme_bdev_ctrlr != NULL); 350 351 rc = spdk_nvme_ctrlr_process_admin_completions(nvme_bdev_ctrlr->ctrlr); 352 if (rc < 0) { 353 bdev_nvme_failover(nvme_bdev_ctrlr, false); 354 } 355 356 return rc == 0 ? SPDK_POLLER_IDLE : SPDK_POLLER_BUSY; 357 } 358 359 static int 360 bdev_nvme_destruct(void *ctx) 361 { 362 struct nvme_bdev *nvme_disk = ctx; 363 struct nvme_bdev_ns *nvme_ns = nvme_disk->nvme_ns; 364 365 pthread_mutex_lock(&nvme_ns->ctrlr->mutex); 366 367 nvme_ns->bdev = NULL; 368 369 if (!nvme_ns->populated) { 370 pthread_mutex_unlock(&nvme_ns->ctrlr->mutex); 371 372 nvme_bdev_ctrlr_destruct(nvme_ns->ctrlr); 373 } else { 374 pthread_mutex_unlock(&nvme_ns->ctrlr->mutex); 375 } 376 377 free(nvme_disk->disk.name); 378 free(nvme_disk); 379 380 return 0; 381 } 382 383 static int 384 bdev_nvme_flush(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 385 struct nvme_bdev_io *bio, uint64_t offset, uint64_t nbytes) 386 { 387 bdev_nvme_io_complete(bio, 0); 388 389 return 0; 390 } 391 392 static int 393 bdev_nvme_create_qpair(struct nvme_io_path *io_path) 394 { 395 struct spdk_nvme_ctrlr *ctrlr = io_path->ctrlr->ctrlr; 396 struct spdk_nvme_io_qpair_opts opts; 397 struct spdk_nvme_qpair *qpair; 398 int rc; 399 400 spdk_nvme_ctrlr_get_default_io_qpair_opts(ctrlr, &opts, sizeof(opts)); 401 opts.delay_cmd_submit = g_opts.delay_cmd_submit; 402 opts.create_only = true; 403 opts.io_queue_requests = spdk_max(g_opts.io_queue_requests, opts.io_queue_requests); 404 g_opts.io_queue_requests = opts.io_queue_requests; 405 406 qpair = spdk_nvme_ctrlr_alloc_io_qpair(ctrlr, &opts, sizeof(opts)); 407 if (qpair == NULL) { 408 return -1; 409 } 410 411 assert(io_path->group != NULL); 412 413 rc = spdk_nvme_poll_group_add(io_path->group->group, qpair); 414 if (rc != 0) { 415 SPDK_ERRLOG("Unable to begin polling on NVMe Channel.\n"); 416 goto err; 417 } 418 419 rc = spdk_nvme_ctrlr_connect_io_qpair(ctrlr, qpair); 420 if (rc != 0) { 421 SPDK_ERRLOG("Unable to connect I/O qpair.\n"); 422 goto err; 423 } 424 425 io_path->qpair = qpair; 426 427 return 0; 428 429 err: 430 spdk_nvme_ctrlr_free_io_qpair(qpair); 431 432 return rc; 433 } 434 435 static int 436 bdev_nvme_destroy_qpair(struct nvme_io_path *io_path) 437 { 438 int rc; 439 440 if (io_path->qpair == NULL) { 441 return 0; 442 } 443 444 rc = spdk_nvme_ctrlr_free_io_qpair(io_path->qpair); 445 if (!rc) { 446 io_path->qpair = NULL; 447 } 448 return rc; 449 } 450 451 static void 452 _bdev_nvme_check_pending_destruct(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr) 453 { 454 pthread_mutex_lock(&nvme_bdev_ctrlr->mutex); 455 if (nvme_bdev_ctrlr->destruct_after_reset) { 456 assert(nvme_bdev_ctrlr->ref == 0 && nvme_bdev_ctrlr->destruct); 457 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 458 459 spdk_thread_send_msg(nvme_bdev_ctrlr->thread, nvme_bdev_ctrlr_unregister, 460 nvme_bdev_ctrlr); 461 } else { 462 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 463 } 464 } 465 466 static void 467 bdev_nvme_check_pending_destruct(struct spdk_io_channel_iter *i, int status) 468 { 469 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr = spdk_io_channel_iter_get_ctx(i); 470 471 _bdev_nvme_check_pending_destruct(nvme_bdev_ctrlr); 472 } 473 474 static void 475 _bdev_nvme_complete_pending_resets(struct nvme_io_path *io_path, 476 enum spdk_bdev_io_status status) 477 { 478 struct spdk_bdev_io *bdev_io; 479 480 while (!TAILQ_EMPTY(&io_path->pending_resets)) { 481 bdev_io = TAILQ_FIRST(&io_path->pending_resets); 482 TAILQ_REMOVE(&io_path->pending_resets, bdev_io, module_link); 483 spdk_bdev_io_complete(bdev_io, status); 484 } 485 } 486 487 static void 488 bdev_nvme_complete_pending_resets(struct spdk_io_channel_iter *i) 489 { 490 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 491 struct nvme_io_path *io_path = spdk_io_channel_get_ctx(_ch); 492 493 _bdev_nvme_complete_pending_resets(io_path, SPDK_BDEV_IO_STATUS_SUCCESS); 494 495 spdk_for_each_channel_continue(i, 0); 496 } 497 498 static void 499 bdev_nvme_abort_pending_resets(struct spdk_io_channel_iter *i) 500 { 501 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 502 struct nvme_io_path *io_path = spdk_io_channel_get_ctx(_ch); 503 504 _bdev_nvme_complete_pending_resets(io_path, SPDK_BDEV_IO_STATUS_FAILED); 505 506 spdk_for_each_channel_continue(i, 0); 507 } 508 509 static void 510 bdev_nvme_reset_io_complete(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, 511 struct spdk_bdev_io *bdev_io, int rc) 512 { 513 enum spdk_bdev_io_status io_status = SPDK_BDEV_IO_STATUS_SUCCESS; 514 515 if (rc) { 516 io_status = SPDK_BDEV_IO_STATUS_FAILED; 517 } 518 519 spdk_bdev_io_complete(bdev_io, io_status); 520 521 /* Make sure we clear any pending resets before returning. */ 522 spdk_for_each_channel(nvme_bdev_ctrlr, 523 rc == 0 ? bdev_nvme_complete_pending_resets : 524 bdev_nvme_abort_pending_resets, 525 nvme_bdev_ctrlr, 526 bdev_nvme_check_pending_destruct); 527 } 528 529 static void 530 _bdev_nvme_reset_complete(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, int rc) 531 { 532 struct nvme_bdev_ctrlr_trid *curr_trid; 533 struct spdk_bdev_io *bdev_io = nvme_bdev_ctrlr->reset_bdev_io; 534 535 nvme_bdev_ctrlr->reset_bdev_io = NULL; 536 537 if (rc) { 538 SPDK_ERRLOG("Resetting controller failed.\n"); 539 } else { 540 SPDK_NOTICELOG("Resetting controller successful.\n"); 541 } 542 543 pthread_mutex_lock(&nvme_bdev_ctrlr->mutex); 544 nvme_bdev_ctrlr->resetting = false; 545 nvme_bdev_ctrlr->failover_in_progress = false; 546 547 curr_trid = TAILQ_FIRST(&nvme_bdev_ctrlr->trids); 548 assert(curr_trid != NULL); 549 assert(&curr_trid->trid == nvme_bdev_ctrlr->connected_trid); 550 551 curr_trid->is_failed = rc != 0 ? true : false; 552 553 if (nvme_bdev_ctrlr->ref == 0 && nvme_bdev_ctrlr->destruct) { 554 /* Destruct ctrlr after clearing pending resets. */ 555 nvme_bdev_ctrlr->destruct_after_reset = true; 556 } 557 558 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 559 560 if (bdev_io) { 561 bdev_nvme_reset_io_complete(nvme_bdev_ctrlr, bdev_io, rc); 562 } else { 563 /* Make sure we clear any pending resets before returning. */ 564 spdk_for_each_channel(nvme_bdev_ctrlr, 565 rc == 0 ? bdev_nvme_complete_pending_resets : 566 bdev_nvme_abort_pending_resets, 567 nvme_bdev_ctrlr, 568 bdev_nvme_check_pending_destruct); 569 } 570 } 571 572 static void 573 _bdev_nvme_reset_create_qpairs_done(struct spdk_io_channel_iter *i, int status) 574 { 575 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr = spdk_io_channel_iter_get_ctx(i); 576 577 _bdev_nvme_reset_complete(nvme_bdev_ctrlr, status); 578 } 579 580 static void 581 _bdev_nvme_reset_create_qpair(struct spdk_io_channel_iter *i) 582 { 583 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 584 struct nvme_io_path *io_path = spdk_io_channel_get_ctx(_ch); 585 int rc; 586 587 rc = bdev_nvme_create_qpair(io_path); 588 589 spdk_for_each_channel_continue(i, rc); 590 } 591 592 static void 593 _bdev_nvme_reset_ctrlr(struct spdk_io_channel_iter *i, int status) 594 { 595 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr = spdk_io_channel_iter_get_ctx(i); 596 int rc; 597 598 if (status) { 599 rc = status; 600 goto err; 601 } 602 603 rc = spdk_nvme_ctrlr_reset(nvme_bdev_ctrlr->ctrlr); 604 if (rc != 0) { 605 goto err; 606 } 607 608 /* Recreate all of the I/O queue pairs */ 609 spdk_for_each_channel(nvme_bdev_ctrlr, 610 _bdev_nvme_reset_create_qpair, 611 nvme_bdev_ctrlr, 612 _bdev_nvme_reset_create_qpairs_done); 613 return; 614 615 err: 616 _bdev_nvme_reset_complete(nvme_bdev_ctrlr, rc); 617 } 618 619 static void 620 _bdev_nvme_reset_destroy_qpair(struct spdk_io_channel_iter *i) 621 { 622 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 623 struct nvme_io_path *io_path = spdk_io_channel_get_ctx(ch); 624 int rc; 625 626 rc = bdev_nvme_destroy_qpair(io_path); 627 628 spdk_for_each_channel_continue(i, rc); 629 } 630 631 static int 632 _bdev_nvme_reset(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr) 633 { 634 pthread_mutex_lock(&nvme_bdev_ctrlr->mutex); 635 if (nvme_bdev_ctrlr->destruct) { 636 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 637 return -EBUSY; 638 } 639 640 if (nvme_bdev_ctrlr->resetting) { 641 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 642 SPDK_NOTICELOG("Unable to perform reset, already in progress.\n"); 643 return -EAGAIN; 644 } 645 646 nvme_bdev_ctrlr->resetting = true; 647 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 648 649 /* First, delete all NVMe I/O queue pairs. */ 650 spdk_for_each_channel(nvme_bdev_ctrlr, 651 _bdev_nvme_reset_destroy_qpair, 652 nvme_bdev_ctrlr, 653 _bdev_nvme_reset_ctrlr); 654 655 return 0; 656 } 657 658 static int 659 bdev_nvme_reset(struct nvme_io_path *io_path, struct spdk_bdev_io *bdev_io) 660 { 661 int rc; 662 663 rc = _bdev_nvme_reset(io_path->ctrlr); 664 if (rc == 0) { 665 assert(io_path->ctrlr->reset_bdev_io == NULL); 666 io_path->ctrlr->reset_bdev_io = bdev_io; 667 } else if (rc == -EAGAIN) { 668 /* 669 * Reset call is queued only if it is from the app framework. This is on purpose so that 670 * we don't interfere with the app framework reset strategy. i.e. we are deferring to the 671 * upper level. If they are in the middle of a reset, we won't try to schedule another one. 672 */ 673 TAILQ_INSERT_TAIL(&io_path->pending_resets, bdev_io, module_link); 674 } else { 675 return rc; 676 } 677 678 return 0; 679 } 680 681 static int 682 _bdev_nvme_failover_start(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, bool remove) 683 { 684 struct nvme_bdev_ctrlr_trid *curr_trid = NULL, *next_trid = NULL; 685 int rc; 686 687 pthread_mutex_lock(&nvme_bdev_ctrlr->mutex); 688 if (nvme_bdev_ctrlr->destruct) { 689 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 690 /* Don't bother resetting if the controller is in the process of being destructed. */ 691 return -EBUSY; 692 } 693 694 curr_trid = TAILQ_FIRST(&nvme_bdev_ctrlr->trids); 695 assert(curr_trid); 696 assert(&curr_trid->trid == nvme_bdev_ctrlr->connected_trid); 697 next_trid = TAILQ_NEXT(curr_trid, link); 698 699 if (nvme_bdev_ctrlr->resetting) { 700 if (next_trid && !nvme_bdev_ctrlr->failover_in_progress) { 701 rc = -EAGAIN; 702 } else { 703 rc = -EBUSY; 704 } 705 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 706 SPDK_NOTICELOG("Unable to perform reset, already in progress.\n"); 707 return rc; 708 } 709 710 nvme_bdev_ctrlr->resetting = true; 711 curr_trid->is_failed = true; 712 713 if (next_trid) { 714 assert(curr_trid->trid.trtype != SPDK_NVME_TRANSPORT_PCIE); 715 716 SPDK_NOTICELOG("Start failover from %s:%s to %s:%s\n", curr_trid->trid.traddr, 717 curr_trid->trid.trsvcid, next_trid->trid.traddr, next_trid->trid.trsvcid); 718 719 nvme_bdev_ctrlr->failover_in_progress = true; 720 spdk_nvme_ctrlr_fail(nvme_bdev_ctrlr->ctrlr); 721 nvme_bdev_ctrlr->connected_trid = &next_trid->trid; 722 rc = spdk_nvme_ctrlr_set_trid(nvme_bdev_ctrlr->ctrlr, &next_trid->trid); 723 assert(rc == 0); 724 TAILQ_REMOVE(&nvme_bdev_ctrlr->trids, curr_trid, link); 725 if (!remove) { 726 /** Shuffle the old trid to the end of the list and use the new one. 727 * Allows for round robin through multiple connections. 728 */ 729 TAILQ_INSERT_TAIL(&nvme_bdev_ctrlr->trids, curr_trid, link); 730 } else { 731 free(curr_trid); 732 } 733 } 734 735 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 736 return 0; 737 } 738 739 static int 740 bdev_nvme_failover(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, bool remove) 741 { 742 int rc; 743 744 rc = _bdev_nvme_failover_start(nvme_bdev_ctrlr, remove); 745 if (rc == 0) { 746 /* First, delete all NVMe I/O queue pairs. */ 747 spdk_for_each_channel(nvme_bdev_ctrlr, 748 _bdev_nvme_reset_destroy_qpair, 749 nvme_bdev_ctrlr, 750 _bdev_nvme_reset_ctrlr); 751 } else if (rc != -EBUSY) { 752 return rc; 753 } 754 755 return 0; 756 } 757 758 static int 759 bdev_nvme_unmap(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 760 struct nvme_bdev_io *bio, 761 uint64_t offset_blocks, 762 uint64_t num_blocks); 763 764 static void 765 bdev_nvme_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, 766 bool success) 767 { 768 struct nvme_bdev_io *bio = (struct nvme_bdev_io *)bdev_io->driver_ctx; 769 struct spdk_bdev *bdev = bdev_io->bdev; 770 struct nvme_bdev *nbdev = (struct nvme_bdev *)bdev->ctxt; 771 struct nvme_io_path *io_path = spdk_io_channel_get_ctx(ch); 772 struct spdk_nvme_ns *ns; 773 struct spdk_nvme_qpair *qpair; 774 int ret; 775 776 if (!success) { 777 ret = -EINVAL; 778 goto exit; 779 } 780 781 if (spdk_unlikely(!bdev_nvme_find_io_path(nbdev, io_path, &ns, &qpair))) { 782 ret = -ENXIO; 783 goto exit; 784 } 785 786 ret = bdev_nvme_readv(ns, 787 qpair, 788 bio, 789 bdev_io->u.bdev.iovs, 790 bdev_io->u.bdev.iovcnt, 791 bdev_io->u.bdev.md_buf, 792 bdev_io->u.bdev.num_blocks, 793 bdev_io->u.bdev.offset_blocks, 794 bdev->dif_check_flags); 795 796 exit: 797 if (spdk_unlikely(ret != 0)) { 798 bdev_nvme_io_complete(bio, ret); 799 } 800 } 801 802 static void 803 bdev_nvme_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io) 804 { 805 struct nvme_io_path *io_path = spdk_io_channel_get_ctx(ch); 806 struct spdk_bdev *bdev = bdev_io->bdev; 807 struct nvme_bdev *nbdev = (struct nvme_bdev *)bdev->ctxt; 808 struct nvme_bdev_io *nbdev_io = (struct nvme_bdev_io *)bdev_io->driver_ctx; 809 struct nvme_bdev_io *nbdev_io_to_abort; 810 struct spdk_nvme_ns *ns; 811 struct spdk_nvme_qpair *qpair; 812 int rc = 0; 813 814 if (spdk_unlikely(!bdev_nvme_find_io_path(nbdev, io_path, &ns, &qpair))) { 815 rc = -ENXIO; 816 goto exit; 817 } 818 819 switch (bdev_io->type) { 820 case SPDK_BDEV_IO_TYPE_READ: 821 if (bdev_io->u.bdev.iovs && bdev_io->u.bdev.iovs[0].iov_base) { 822 rc = bdev_nvme_readv(ns, 823 qpair, 824 nbdev_io, 825 bdev_io->u.bdev.iovs, 826 bdev_io->u.bdev.iovcnt, 827 bdev_io->u.bdev.md_buf, 828 bdev_io->u.bdev.num_blocks, 829 bdev_io->u.bdev.offset_blocks, 830 bdev->dif_check_flags); 831 } else { 832 spdk_bdev_io_get_buf(bdev_io, bdev_nvme_get_buf_cb, 833 bdev_io->u.bdev.num_blocks * bdev->blocklen); 834 rc = 0; 835 } 836 break; 837 case SPDK_BDEV_IO_TYPE_WRITE: 838 rc = bdev_nvme_writev(ns, 839 qpair, 840 nbdev_io, 841 bdev_io->u.bdev.iovs, 842 bdev_io->u.bdev.iovcnt, 843 bdev_io->u.bdev.md_buf, 844 bdev_io->u.bdev.num_blocks, 845 bdev_io->u.bdev.offset_blocks, 846 bdev->dif_check_flags); 847 break; 848 case SPDK_BDEV_IO_TYPE_COMPARE: 849 rc = bdev_nvme_comparev(ns, 850 qpair, 851 nbdev_io, 852 bdev_io->u.bdev.iovs, 853 bdev_io->u.bdev.iovcnt, 854 bdev_io->u.bdev.md_buf, 855 bdev_io->u.bdev.num_blocks, 856 bdev_io->u.bdev.offset_blocks, 857 bdev->dif_check_flags); 858 break; 859 case SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE: 860 rc = bdev_nvme_comparev_and_writev(ns, 861 qpair, 862 nbdev_io, 863 bdev_io->u.bdev.iovs, 864 bdev_io->u.bdev.iovcnt, 865 bdev_io->u.bdev.fused_iovs, 866 bdev_io->u.bdev.fused_iovcnt, 867 bdev_io->u.bdev.md_buf, 868 bdev_io->u.bdev.num_blocks, 869 bdev_io->u.bdev.offset_blocks, 870 bdev->dif_check_flags); 871 break; 872 case SPDK_BDEV_IO_TYPE_UNMAP: 873 rc = bdev_nvme_unmap(ns, 874 qpair, 875 nbdev_io, 876 bdev_io->u.bdev.offset_blocks, 877 bdev_io->u.bdev.num_blocks); 878 break; 879 case SPDK_BDEV_IO_TYPE_RESET: 880 rc = bdev_nvme_reset(io_path, bdev_io); 881 break; 882 case SPDK_BDEV_IO_TYPE_FLUSH: 883 rc = bdev_nvme_flush(ns, 884 qpair, 885 nbdev_io, 886 bdev_io->u.bdev.offset_blocks, 887 bdev_io->u.bdev.num_blocks); 888 break; 889 case SPDK_BDEV_IO_TYPE_ZONE_APPEND: 890 rc = bdev_nvme_zone_appendv(ns, 891 qpair, 892 nbdev_io, 893 bdev_io->u.bdev.iovs, 894 bdev_io->u.bdev.iovcnt, 895 bdev_io->u.bdev.md_buf, 896 bdev_io->u.bdev.num_blocks, 897 bdev_io->u.bdev.offset_blocks, 898 bdev->dif_check_flags); 899 break; 900 case SPDK_BDEV_IO_TYPE_GET_ZONE_INFO: 901 rc = bdev_nvme_get_zone_info(ns, 902 qpair, 903 nbdev_io, 904 bdev_io->u.zone_mgmt.zone_id, 905 bdev_io->u.zone_mgmt.num_zones, 906 bdev_io->u.zone_mgmt.buf); 907 break; 908 case SPDK_BDEV_IO_TYPE_ZONE_MANAGEMENT: 909 rc = bdev_nvme_zone_management(ns, 910 qpair, 911 nbdev_io, 912 bdev_io->u.zone_mgmt.zone_id, 913 bdev_io->u.zone_mgmt.zone_action); 914 break; 915 case SPDK_BDEV_IO_TYPE_NVME_ADMIN: 916 rc = bdev_nvme_admin_passthru(io_path, 917 nbdev_io, 918 &bdev_io->u.nvme_passthru.cmd, 919 bdev_io->u.nvme_passthru.buf, 920 bdev_io->u.nvme_passthru.nbytes); 921 break; 922 case SPDK_BDEV_IO_TYPE_NVME_IO: 923 rc = bdev_nvme_io_passthru(ns, 924 qpair, 925 nbdev_io, 926 &bdev_io->u.nvme_passthru.cmd, 927 bdev_io->u.nvme_passthru.buf, 928 bdev_io->u.nvme_passthru.nbytes); 929 break; 930 case SPDK_BDEV_IO_TYPE_NVME_IO_MD: 931 rc = bdev_nvme_io_passthru_md(ns, 932 qpair, 933 nbdev_io, 934 &bdev_io->u.nvme_passthru.cmd, 935 bdev_io->u.nvme_passthru.buf, 936 bdev_io->u.nvme_passthru.nbytes, 937 bdev_io->u.nvme_passthru.md_buf, 938 bdev_io->u.nvme_passthru.md_len); 939 break; 940 case SPDK_BDEV_IO_TYPE_ABORT: 941 nbdev_io_to_abort = (struct nvme_bdev_io *)bdev_io->u.abort.bio_to_abort->driver_ctx; 942 rc = bdev_nvme_abort(io_path, 943 nbdev_io, 944 nbdev_io_to_abort); 945 break; 946 default: 947 rc = -EINVAL; 948 break; 949 } 950 951 exit: 952 if (spdk_unlikely(rc != 0)) { 953 bdev_nvme_io_complete(nbdev_io, rc); 954 } 955 } 956 957 static bool 958 bdev_nvme_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type) 959 { 960 struct nvme_bdev *nbdev = ctx; 961 struct nvme_bdev_ns *nvme_ns; 962 struct spdk_nvme_ns *ns; 963 struct spdk_nvme_ctrlr *ctrlr; 964 const struct spdk_nvme_ctrlr_data *cdata; 965 966 nvme_ns = nbdev->nvme_ns; 967 assert(nvme_ns != NULL); 968 ns = nvme_ns->ns; 969 ctrlr = spdk_nvme_ns_get_ctrlr(ns); 970 971 switch (io_type) { 972 case SPDK_BDEV_IO_TYPE_READ: 973 case SPDK_BDEV_IO_TYPE_WRITE: 974 case SPDK_BDEV_IO_TYPE_RESET: 975 case SPDK_BDEV_IO_TYPE_FLUSH: 976 case SPDK_BDEV_IO_TYPE_NVME_ADMIN: 977 case SPDK_BDEV_IO_TYPE_NVME_IO: 978 case SPDK_BDEV_IO_TYPE_ABORT: 979 return true; 980 981 case SPDK_BDEV_IO_TYPE_COMPARE: 982 return spdk_nvme_ns_supports_compare(ns); 983 984 case SPDK_BDEV_IO_TYPE_NVME_IO_MD: 985 return spdk_nvme_ns_get_md_size(ns) ? true : false; 986 987 case SPDK_BDEV_IO_TYPE_UNMAP: 988 cdata = spdk_nvme_ctrlr_get_data(ctrlr); 989 return cdata->oncs.dsm; 990 991 case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: 992 /* 993 * The NVMe controller write_zeroes function is currently not used by our driver. 994 * NVMe write zeroes is limited to 16-bit block count, and the bdev layer currently 995 * has no mechanism for reporting a max write zeroes block count, nor ability to 996 * split a write zeroes request. 997 */ 998 return false; 999 1000 case SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE: 1001 if (spdk_nvme_ctrlr_get_flags(ctrlr) & 1002 SPDK_NVME_CTRLR_COMPARE_AND_WRITE_SUPPORTED) { 1003 return true; 1004 } 1005 return false; 1006 1007 case SPDK_BDEV_IO_TYPE_GET_ZONE_INFO: 1008 case SPDK_BDEV_IO_TYPE_ZONE_MANAGEMENT: 1009 return spdk_nvme_ns_get_csi(ns) == SPDK_NVME_CSI_ZNS; 1010 1011 case SPDK_BDEV_IO_TYPE_ZONE_APPEND: 1012 return spdk_nvme_ns_get_csi(ns) == SPDK_NVME_CSI_ZNS && 1013 spdk_nvme_ctrlr_get_flags(ctrlr) & SPDK_NVME_CTRLR_ZONE_APPEND_SUPPORTED; 1014 1015 default: 1016 return false; 1017 } 1018 } 1019 1020 static int 1021 bdev_nvme_create_path_cb(void *io_device, void *ctx_buf) 1022 { 1023 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr = io_device; 1024 struct nvme_io_path *io_path = ctx_buf; 1025 struct spdk_io_channel *pg_ch; 1026 int rc; 1027 1028 pg_ch = spdk_get_io_channel(&g_nvme_bdev_ctrlrs); 1029 if (!pg_ch) { 1030 return -1; 1031 } 1032 1033 io_path->group = spdk_io_channel_get_ctx(pg_ch); 1034 1035 #ifdef SPDK_CONFIG_VTUNE 1036 io_path->group->collect_spin_stat = true; 1037 #else 1038 io_path->group->collect_spin_stat = false; 1039 #endif 1040 1041 TAILQ_INIT(&io_path->pending_resets); 1042 1043 if (spdk_nvme_ctrlr_is_ocssd_supported(nvme_bdev_ctrlr->ctrlr)) { 1044 rc = bdev_ocssd_create_io_channel(io_path); 1045 if (rc != 0) { 1046 goto err_ocssd_ch; 1047 } 1048 } 1049 1050 io_path->ctrlr = nvme_bdev_ctrlr; 1051 1052 rc = bdev_nvme_create_qpair(io_path); 1053 if (rc != 0) { 1054 goto err_qpair; 1055 } 1056 1057 return 0; 1058 1059 err_qpair: 1060 if (io_path->ocssd_ch) { 1061 bdev_ocssd_destroy_io_channel(io_path); 1062 } 1063 err_ocssd_ch: 1064 spdk_put_io_channel(pg_ch); 1065 1066 return rc; 1067 } 1068 1069 static void 1070 bdev_nvme_destroy_path_cb(void *io_device, void *ctx_buf) 1071 { 1072 struct nvme_io_path *io_path = ctx_buf; 1073 1074 assert(io_path->group != NULL); 1075 1076 if (io_path->ocssd_ch != NULL) { 1077 bdev_ocssd_destroy_io_channel(io_path); 1078 } 1079 1080 bdev_nvme_destroy_qpair(io_path); 1081 1082 spdk_put_io_channel(spdk_io_channel_from_ctx(io_path->group)); 1083 } 1084 1085 static void 1086 bdev_nvme_poll_group_submit_accel_crc32c(void *ctx, uint32_t *dst, struct iovec *iov, 1087 uint32_t iov_cnt, uint32_t seed, 1088 spdk_nvme_accel_completion_cb cb_fn, void *cb_arg) 1089 { 1090 struct nvme_bdev_poll_group *group = ctx; 1091 int rc; 1092 1093 assert(group->accel_channel != NULL); 1094 assert(cb_fn != NULL); 1095 1096 rc = spdk_accel_submit_crc32cv(group->accel_channel, dst, iov, iov_cnt, seed, cb_fn, cb_arg); 1097 if (rc) { 1098 /* For the two cases, spdk_accel_submit_crc32cv does not call the user's cb_fn */ 1099 if (rc == -ENOMEM || rc == -EINVAL) { 1100 cb_fn(cb_arg, rc); 1101 } 1102 SPDK_ERRLOG("Cannot complete the accelerated crc32c operation with iov=%p\n", iov); 1103 } 1104 } 1105 1106 static struct spdk_nvme_accel_fn_table g_bdev_nvme_accel_fn_table = { 1107 .table_size = sizeof(struct spdk_nvme_accel_fn_table), 1108 .submit_accel_crc32c = bdev_nvme_poll_group_submit_accel_crc32c, 1109 }; 1110 1111 static int 1112 bdev_nvme_poll_group_create_cb(void *io_device, void *ctx_buf) 1113 { 1114 struct nvme_bdev_poll_group *group = ctx_buf; 1115 1116 group->group = spdk_nvme_poll_group_create(group, &g_bdev_nvme_accel_fn_table); 1117 if (group->group == NULL) { 1118 return -1; 1119 } 1120 1121 group->accel_channel = spdk_accel_engine_get_io_channel(); 1122 if (!group->accel_channel) { 1123 spdk_nvme_poll_group_destroy(group->group); 1124 SPDK_ERRLOG("Cannot get the accel_channel for bdev nvme polling group=%p\n", 1125 group); 1126 return -1; 1127 } 1128 1129 group->poller = SPDK_POLLER_REGISTER(bdev_nvme_poll, group, g_opts.nvme_ioq_poll_period_us); 1130 1131 if (group->poller == NULL) { 1132 spdk_put_io_channel(group->accel_channel); 1133 spdk_nvme_poll_group_destroy(group->group); 1134 return -1; 1135 } 1136 1137 return 0; 1138 } 1139 1140 static void 1141 bdev_nvme_poll_group_destroy_cb(void *io_device, void *ctx_buf) 1142 { 1143 struct nvme_bdev_poll_group *group = ctx_buf; 1144 1145 if (group->accel_channel) { 1146 spdk_put_io_channel(group->accel_channel); 1147 } 1148 1149 spdk_poller_unregister(&group->poller); 1150 if (spdk_nvme_poll_group_destroy(group->group)) { 1151 SPDK_ERRLOG("Unable to destroy a poll group for the NVMe bdev module.\n"); 1152 assert(false); 1153 } 1154 } 1155 1156 static struct spdk_io_channel * 1157 bdev_nvme_get_io_channel(void *ctx) 1158 { 1159 struct nvme_bdev *nvme_bdev = ctx; 1160 1161 return spdk_get_io_channel(nvme_bdev->nvme_ns->ctrlr); 1162 } 1163 1164 static void * 1165 bdev_nvme_get_module_ctx(void *ctx) 1166 { 1167 struct nvme_bdev *nvme_bdev = ctx; 1168 1169 return bdev_nvme_get_ctrlr(&nvme_bdev->disk); 1170 } 1171 1172 static const char * 1173 _nvme_ana_state_str(enum spdk_nvme_ana_state ana_state) 1174 { 1175 switch (ana_state) { 1176 case SPDK_NVME_ANA_OPTIMIZED_STATE: 1177 return "optimized"; 1178 case SPDK_NVME_ANA_NON_OPTIMIZED_STATE: 1179 return "non_optimized"; 1180 case SPDK_NVME_ANA_INACCESSIBLE_STATE: 1181 return "inaccessible"; 1182 case SPDK_NVME_ANA_PERSISTENT_LOSS_STATE: 1183 return "persistent_loss"; 1184 case SPDK_NVME_ANA_CHANGE_STATE: 1185 return "change"; 1186 default: 1187 return NULL; 1188 } 1189 } 1190 1191 static int 1192 bdev_nvme_dump_info_json(void *ctx, struct spdk_json_write_ctx *w) 1193 { 1194 struct nvme_bdev *nvme_bdev = ctx; 1195 struct nvme_bdev_ns *nvme_ns; 1196 struct spdk_nvme_ns *ns; 1197 struct spdk_nvme_ctrlr *ctrlr; 1198 const struct spdk_nvme_ctrlr_data *cdata; 1199 const struct spdk_nvme_transport_id *trid; 1200 union spdk_nvme_vs_register vs; 1201 union spdk_nvme_csts_register csts; 1202 char buf[128]; 1203 1204 nvme_ns = nvme_bdev->nvme_ns; 1205 assert(nvme_ns != NULL); 1206 ns = nvme_ns->ns; 1207 ctrlr = spdk_nvme_ns_get_ctrlr(ns); 1208 1209 cdata = spdk_nvme_ctrlr_get_data(ctrlr); 1210 trid = spdk_nvme_ctrlr_get_transport_id(ctrlr); 1211 vs = spdk_nvme_ctrlr_get_regs_vs(ctrlr); 1212 csts = spdk_nvme_ctrlr_get_regs_csts(ctrlr); 1213 1214 spdk_json_write_named_object_begin(w, "nvme"); 1215 1216 if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) { 1217 spdk_json_write_named_string(w, "pci_address", trid->traddr); 1218 } 1219 1220 spdk_json_write_named_object_begin(w, "trid"); 1221 1222 nvme_bdev_dump_trid_json(trid, w); 1223 1224 spdk_json_write_object_end(w); 1225 1226 #ifdef SPDK_CONFIG_NVME_CUSE 1227 size_t cuse_name_size = 128; 1228 char cuse_name[cuse_name_size]; 1229 1230 int rc = spdk_nvme_cuse_get_ns_name(ctrlr, spdk_nvme_ns_get_id(ns), 1231 cuse_name, &cuse_name_size); 1232 if (rc == 0) { 1233 spdk_json_write_named_string(w, "cuse_device", cuse_name); 1234 } 1235 #endif 1236 1237 spdk_json_write_named_object_begin(w, "ctrlr_data"); 1238 1239 spdk_json_write_named_string_fmt(w, "vendor_id", "0x%04x", cdata->vid); 1240 1241 snprintf(buf, sizeof(cdata->mn) + 1, "%s", cdata->mn); 1242 spdk_str_trim(buf); 1243 spdk_json_write_named_string(w, "model_number", buf); 1244 1245 snprintf(buf, sizeof(cdata->sn) + 1, "%s", cdata->sn); 1246 spdk_str_trim(buf); 1247 spdk_json_write_named_string(w, "serial_number", buf); 1248 1249 snprintf(buf, sizeof(cdata->fr) + 1, "%s", cdata->fr); 1250 spdk_str_trim(buf); 1251 spdk_json_write_named_string(w, "firmware_revision", buf); 1252 1253 if (cdata->subnqn[0] != '\0') { 1254 spdk_json_write_named_string(w, "subnqn", cdata->subnqn); 1255 } 1256 1257 spdk_json_write_named_object_begin(w, "oacs"); 1258 1259 spdk_json_write_named_uint32(w, "security", cdata->oacs.security); 1260 spdk_json_write_named_uint32(w, "format", cdata->oacs.format); 1261 spdk_json_write_named_uint32(w, "firmware", cdata->oacs.firmware); 1262 spdk_json_write_named_uint32(w, "ns_manage", cdata->oacs.ns_manage); 1263 1264 spdk_json_write_object_end(w); 1265 1266 spdk_json_write_object_end(w); 1267 1268 spdk_json_write_named_object_begin(w, "vs"); 1269 1270 spdk_json_write_name(w, "nvme_version"); 1271 if (vs.bits.ter) { 1272 spdk_json_write_string_fmt(w, "%u.%u.%u", vs.bits.mjr, vs.bits.mnr, vs.bits.ter); 1273 } else { 1274 spdk_json_write_string_fmt(w, "%u.%u", vs.bits.mjr, vs.bits.mnr); 1275 } 1276 1277 spdk_json_write_object_end(w); 1278 1279 spdk_json_write_named_object_begin(w, "csts"); 1280 1281 spdk_json_write_named_uint32(w, "rdy", csts.bits.rdy); 1282 spdk_json_write_named_uint32(w, "cfs", csts.bits.cfs); 1283 1284 spdk_json_write_object_end(w); 1285 1286 spdk_json_write_named_object_begin(w, "ns_data"); 1287 1288 spdk_json_write_named_uint32(w, "id", spdk_nvme_ns_get_id(ns)); 1289 1290 if (cdata->cmic.ana_reporting) { 1291 spdk_json_write_named_string(w, "ana_state", 1292 _nvme_ana_state_str(spdk_nvme_ns_get_ana_state(ns))); 1293 } 1294 1295 spdk_json_write_object_end(w); 1296 1297 if (cdata->oacs.security) { 1298 spdk_json_write_named_object_begin(w, "security"); 1299 1300 spdk_json_write_named_bool(w, "opal", nvme_bdev->opal); 1301 1302 spdk_json_write_object_end(w); 1303 } 1304 1305 spdk_json_write_object_end(w); 1306 1307 return 0; 1308 } 1309 1310 static void 1311 bdev_nvme_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) 1312 { 1313 /* No config per bdev needed */ 1314 } 1315 1316 static uint64_t 1317 bdev_nvme_get_spin_time(struct spdk_io_channel *ch) 1318 { 1319 struct nvme_io_path *io_path = spdk_io_channel_get_ctx(ch); 1320 struct nvme_bdev_poll_group *group = io_path->group; 1321 uint64_t spin_time; 1322 1323 if (!group || !group->collect_spin_stat) { 1324 return 0; 1325 } 1326 1327 if (group->end_ticks != 0) { 1328 group->spin_ticks += (group->end_ticks - group->start_ticks); 1329 group->end_ticks = 0; 1330 } 1331 1332 spin_time = (group->spin_ticks * 1000000ULL) / spdk_get_ticks_hz(); 1333 group->start_ticks = 0; 1334 group->spin_ticks = 0; 1335 1336 return spin_time; 1337 } 1338 1339 static const struct spdk_bdev_fn_table nvmelib_fn_table = { 1340 .destruct = bdev_nvme_destruct, 1341 .submit_request = bdev_nvme_submit_request, 1342 .io_type_supported = bdev_nvme_io_type_supported, 1343 .get_io_channel = bdev_nvme_get_io_channel, 1344 .dump_info_json = bdev_nvme_dump_info_json, 1345 .write_config_json = bdev_nvme_write_config_json, 1346 .get_spin_time = bdev_nvme_get_spin_time, 1347 .get_module_ctx = bdev_nvme_get_module_ctx, 1348 }; 1349 1350 static int 1351 nvme_disk_create(struct spdk_bdev *disk, const char *base_name, 1352 struct spdk_nvme_ctrlr *ctrlr, struct spdk_nvme_ns *ns, 1353 uint32_t prchk_flags, void *ctx) 1354 { 1355 const struct spdk_uuid *uuid; 1356 const struct spdk_nvme_ctrlr_data *cdata; 1357 const struct spdk_nvme_ns_data *nsdata; 1358 int rc; 1359 enum spdk_nvme_csi csi; 1360 1361 cdata = spdk_nvme_ctrlr_get_data(ctrlr); 1362 csi = spdk_nvme_ns_get_csi(ns); 1363 1364 switch (csi) { 1365 case SPDK_NVME_CSI_NVM: 1366 disk->product_name = "NVMe disk"; 1367 break; 1368 case SPDK_NVME_CSI_ZNS: 1369 disk->product_name = "NVMe ZNS disk"; 1370 disk->zoned = true; 1371 disk->zone_size = spdk_nvme_zns_ns_get_zone_size_sectors(ns); 1372 disk->max_zone_append_size = spdk_nvme_zns_ctrlr_get_max_zone_append_size(ctrlr) / 1373 spdk_nvme_ns_get_extended_sector_size(ns); 1374 disk->max_open_zones = spdk_nvme_zns_ns_get_max_open_zones(ns); 1375 disk->max_active_zones = spdk_nvme_zns_ns_get_max_active_zones(ns); 1376 break; 1377 default: 1378 SPDK_ERRLOG("unsupported CSI: %u\n", csi); 1379 return -ENOTSUP; 1380 } 1381 1382 disk->name = spdk_sprintf_alloc("%sn%d", base_name, spdk_nvme_ns_get_id(ns)); 1383 if (!disk->name) { 1384 return -ENOMEM; 1385 } 1386 1387 disk->write_cache = 0; 1388 if (cdata->vwc.present) { 1389 /* Enable if the Volatile Write Cache exists */ 1390 disk->write_cache = 1; 1391 } 1392 disk->blocklen = spdk_nvme_ns_get_extended_sector_size(ns); 1393 disk->blockcnt = spdk_nvme_ns_get_num_sectors(ns); 1394 disk->optimal_io_boundary = spdk_nvme_ns_get_optimal_io_boundary(ns); 1395 1396 uuid = spdk_nvme_ns_get_uuid(ns); 1397 if (uuid != NULL) { 1398 disk->uuid = *uuid; 1399 } 1400 1401 nsdata = spdk_nvme_ns_get_data(ns); 1402 1403 disk->md_len = spdk_nvme_ns_get_md_size(ns); 1404 if (disk->md_len != 0) { 1405 disk->md_interleave = nsdata->flbas.extended; 1406 disk->dif_type = (enum spdk_dif_type)spdk_nvme_ns_get_pi_type(ns); 1407 if (disk->dif_type != SPDK_DIF_DISABLE) { 1408 disk->dif_is_head_of_md = nsdata->dps.md_start; 1409 disk->dif_check_flags = prchk_flags; 1410 } 1411 } 1412 1413 if (!(spdk_nvme_ctrlr_get_flags(ctrlr) & 1414 SPDK_NVME_CTRLR_COMPARE_AND_WRITE_SUPPORTED)) { 1415 disk->acwu = 0; 1416 } else if (nsdata->nsfeat.ns_atomic_write_unit) { 1417 disk->acwu = nsdata->nacwu; 1418 } else { 1419 disk->acwu = cdata->acwu; 1420 } 1421 1422 disk->ctxt = ctx; 1423 disk->fn_table = &nvmelib_fn_table; 1424 disk->module = &nvme_if; 1425 rc = spdk_bdev_register(disk); 1426 if (rc) { 1427 SPDK_ERRLOG("spdk_bdev_register() failed\n"); 1428 free(disk->name); 1429 return rc; 1430 } 1431 1432 return 0; 1433 } 1434 1435 static int 1436 nvme_bdev_create(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, struct nvme_bdev_ns *nvme_ns) 1437 { 1438 struct nvme_bdev *bdev; 1439 int rc; 1440 1441 bdev = calloc(1, sizeof(*bdev)); 1442 if (!bdev) { 1443 SPDK_ERRLOG("bdev calloc() failed\n"); 1444 return -ENOMEM; 1445 } 1446 1447 bdev->nvme_ns = nvme_ns; 1448 bdev->opal = nvme_bdev_ctrlr->opal_dev != NULL; 1449 1450 rc = nvme_disk_create(&bdev->disk, nvme_bdev_ctrlr->name, nvme_bdev_ctrlr->ctrlr, 1451 nvme_ns->ns, nvme_bdev_ctrlr->prchk_flags, bdev); 1452 if (rc != 0) { 1453 SPDK_ERRLOG("Failed to create NVMe disk\n"); 1454 free(bdev); 1455 return rc; 1456 } 1457 1458 nvme_ns->bdev = bdev; 1459 1460 return 0; 1461 } 1462 1463 static bool 1464 bdev_nvme_compare_ns(struct spdk_nvme_ns *ns1, struct spdk_nvme_ns *ns2) 1465 { 1466 const struct spdk_nvme_ns_data *nsdata1, *nsdata2; 1467 const struct spdk_uuid *uuid1, *uuid2; 1468 1469 nsdata1 = spdk_nvme_ns_get_data(ns1); 1470 nsdata2 = spdk_nvme_ns_get_data(ns2); 1471 uuid1 = spdk_nvme_ns_get_uuid(ns1); 1472 uuid2 = spdk_nvme_ns_get_uuid(ns2); 1473 1474 return memcmp(nsdata1->nguid, nsdata2->nguid, sizeof(nsdata1->nguid)) == 0 && 1475 nsdata1->eui64 == nsdata2->eui64 && 1476 uuid1 != NULL && uuid2 != NULL && spdk_uuid_compare(uuid1, uuid2) == 0; 1477 } 1478 1479 static void 1480 nvme_ctrlr_populate_standard_namespace(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, 1481 struct nvme_bdev_ns *nvme_ns, struct nvme_async_probe_ctx *ctx) 1482 { 1483 struct spdk_nvme_ctrlr *ctrlr = nvme_bdev_ctrlr->ctrlr; 1484 struct spdk_nvme_ns *ns; 1485 int rc = 0; 1486 1487 ns = spdk_nvme_ctrlr_get_ns(ctrlr, nvme_ns->id); 1488 if (!ns) { 1489 SPDK_DEBUGLOG(bdev_nvme, "Invalid NS %d\n", nvme_ns->id); 1490 rc = -EINVAL; 1491 goto done; 1492 } 1493 1494 nvme_ns->ns = ns; 1495 nvme_ns->populated = true; 1496 1497 rc = nvme_bdev_create(nvme_bdev_ctrlr, nvme_ns); 1498 done: 1499 nvme_ctrlr_populate_namespace_done(ctx, nvme_ns, rc); 1500 } 1501 1502 static bool 1503 hotplug_probe_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid, 1504 struct spdk_nvme_ctrlr_opts *opts) 1505 { 1506 struct nvme_probe_skip_entry *entry; 1507 1508 TAILQ_FOREACH(entry, &g_skipped_nvme_ctrlrs, tailq) { 1509 if (spdk_nvme_transport_id_compare(trid, &entry->trid) == 0) { 1510 return false; 1511 } 1512 } 1513 1514 opts->arbitration_burst = (uint8_t)g_opts.arbitration_burst; 1515 opts->low_priority_weight = (uint8_t)g_opts.low_priority_weight; 1516 opts->medium_priority_weight = (uint8_t)g_opts.medium_priority_weight; 1517 opts->high_priority_weight = (uint8_t)g_opts.high_priority_weight; 1518 1519 SPDK_DEBUGLOG(bdev_nvme, "Attaching to %s\n", trid->traddr); 1520 1521 return true; 1522 } 1523 1524 static void 1525 nvme_abort_cpl(void *ctx, const struct spdk_nvme_cpl *cpl) 1526 { 1527 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr = ctx; 1528 1529 if (spdk_nvme_cpl_is_error(cpl)) { 1530 SPDK_WARNLOG("Abort failed. Resetting controller. sc is %u, sct is %u.\n", cpl->status.sc, 1531 cpl->status.sct); 1532 _bdev_nvme_reset(nvme_bdev_ctrlr); 1533 } 1534 } 1535 1536 static void 1537 timeout_cb(void *cb_arg, struct spdk_nvme_ctrlr *ctrlr, 1538 struct spdk_nvme_qpair *qpair, uint16_t cid) 1539 { 1540 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr = cb_arg; 1541 union spdk_nvme_csts_register csts; 1542 int rc; 1543 1544 assert(nvme_bdev_ctrlr->ctrlr == ctrlr); 1545 1546 SPDK_WARNLOG("Warning: Detected a timeout. ctrlr=%p qpair=%p cid=%u\n", ctrlr, qpair, cid); 1547 1548 /* Only try to read CSTS if it's a PCIe controller or we have a timeout on an I/O 1549 * queue. (Note: qpair == NULL when there's an admin cmd timeout.) Otherwise we 1550 * would submit another fabrics cmd on the admin queue to read CSTS and check for its 1551 * completion recursively. 1552 */ 1553 if (nvme_bdev_ctrlr->connected_trid->trtype == SPDK_NVME_TRANSPORT_PCIE || qpair != NULL) { 1554 csts = spdk_nvme_ctrlr_get_regs_csts(ctrlr); 1555 if (csts.bits.cfs) { 1556 SPDK_ERRLOG("Controller Fatal Status, reset required\n"); 1557 _bdev_nvme_reset(nvme_bdev_ctrlr); 1558 return; 1559 } 1560 } 1561 1562 switch (g_opts.action_on_timeout) { 1563 case SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT: 1564 if (qpair) { 1565 /* Don't send abort to ctrlr when reset is running. */ 1566 pthread_mutex_lock(&nvme_bdev_ctrlr->mutex); 1567 if (nvme_bdev_ctrlr->resetting) { 1568 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 1569 SPDK_NOTICELOG("Quit abort. Ctrlr is in the process of reseting.\n"); 1570 return; 1571 } 1572 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 1573 1574 rc = spdk_nvme_ctrlr_cmd_abort(ctrlr, qpair, cid, 1575 nvme_abort_cpl, nvme_bdev_ctrlr); 1576 if (rc == 0) { 1577 return; 1578 } 1579 1580 SPDK_ERRLOG("Unable to send abort. Resetting, rc is %d.\n", rc); 1581 } 1582 1583 /* FALLTHROUGH */ 1584 case SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET: 1585 _bdev_nvme_reset(nvme_bdev_ctrlr); 1586 break; 1587 case SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE: 1588 SPDK_DEBUGLOG(bdev_nvme, "No action for nvme controller timeout.\n"); 1589 break; 1590 default: 1591 SPDK_ERRLOG("An invalid timeout action value is found.\n"); 1592 break; 1593 } 1594 } 1595 1596 static void 1597 nvme_ctrlr_depopulate_standard_namespace(struct nvme_bdev_ns *nvme_ns) 1598 { 1599 struct nvme_bdev *bdev; 1600 1601 bdev = nvme_ns->bdev; 1602 if (bdev != NULL) { 1603 spdk_bdev_unregister(&bdev->disk, NULL, NULL); 1604 } 1605 1606 nvme_ctrlr_depopulate_namespace_done(nvme_ns); 1607 } 1608 1609 static void 1610 nvme_ctrlr_populate_namespace(struct nvme_bdev_ctrlr *ctrlr, struct nvme_bdev_ns *nvme_ns, 1611 struct nvme_async_probe_ctx *ctx) 1612 { 1613 g_populate_namespace_fn[nvme_ns->type](ctrlr, nvme_ns, ctx); 1614 } 1615 1616 static void 1617 nvme_ctrlr_depopulate_namespace(struct nvme_bdev_ctrlr *ctrlr, struct nvme_bdev_ns *nvme_ns) 1618 { 1619 g_depopulate_namespace_fn[nvme_ns->type](nvme_ns); 1620 } 1621 1622 void 1623 nvme_ctrlr_populate_namespace_done(struct nvme_async_probe_ctx *ctx, 1624 struct nvme_bdev_ns *nvme_ns, int rc) 1625 { 1626 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr = nvme_ns->ctrlr; 1627 1628 assert(nvme_bdev_ctrlr != NULL); 1629 1630 if (rc == 0) { 1631 pthread_mutex_lock(&nvme_bdev_ctrlr->mutex); 1632 nvme_bdev_ctrlr->ref++; 1633 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 1634 } else { 1635 memset(nvme_ns, 0, sizeof(*nvme_ns)); 1636 } 1637 1638 if (ctx) { 1639 ctx->populates_in_progress--; 1640 if (ctx->populates_in_progress == 0) { 1641 nvme_ctrlr_populate_namespaces_done(nvme_bdev_ctrlr, ctx); 1642 } 1643 } 1644 } 1645 1646 static void 1647 nvme_ctrlr_populate_namespaces(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, 1648 struct nvme_async_probe_ctx *ctx) 1649 { 1650 struct spdk_nvme_ctrlr *ctrlr = nvme_bdev_ctrlr->ctrlr; 1651 struct nvme_bdev_ns *nvme_ns; 1652 struct spdk_nvme_ns *ns; 1653 struct nvme_bdev *bdev; 1654 uint32_t i; 1655 int rc; 1656 uint64_t num_sectors; 1657 bool ns_is_active; 1658 1659 if (ctx) { 1660 /* Initialize this count to 1 to handle the populate functions 1661 * calling nvme_ctrlr_populate_namespace_done() immediately. 1662 */ 1663 ctx->populates_in_progress = 1; 1664 } 1665 1666 for (i = 0; i < nvme_bdev_ctrlr->num_ns; i++) { 1667 uint32_t nsid = i + 1; 1668 1669 nvme_ns = nvme_bdev_ctrlr->namespaces[i]; 1670 ns_is_active = spdk_nvme_ctrlr_is_active_ns(ctrlr, nsid); 1671 1672 if (nvme_ns->populated && ns_is_active && nvme_ns->type == NVME_BDEV_NS_STANDARD) { 1673 /* NS is still there but attributes may have changed */ 1674 ns = spdk_nvme_ctrlr_get_ns(ctrlr, nsid); 1675 num_sectors = spdk_nvme_ns_get_num_sectors(ns); 1676 bdev = nvme_ns->bdev; 1677 assert(bdev != NULL); 1678 if (bdev->disk.blockcnt != num_sectors) { 1679 SPDK_NOTICELOG("NSID %u is resized: bdev name %s, old size %" PRIu64 ", new size %" PRIu64 "\n", 1680 nsid, 1681 bdev->disk.name, 1682 bdev->disk.blockcnt, 1683 num_sectors); 1684 rc = spdk_bdev_notify_blockcnt_change(&bdev->disk, num_sectors); 1685 if (rc != 0) { 1686 SPDK_ERRLOG("Could not change num blocks for nvme bdev: name %s, errno: %d.\n", 1687 bdev->disk.name, rc); 1688 } 1689 } 1690 } 1691 1692 if (!nvme_ns->populated && ns_is_active) { 1693 nvme_ns->id = nsid; 1694 nvme_ns->ctrlr = nvme_bdev_ctrlr; 1695 if (spdk_nvme_ctrlr_is_ocssd_supported(ctrlr)) { 1696 nvme_ns->type = NVME_BDEV_NS_OCSSD; 1697 } else { 1698 nvme_ns->type = NVME_BDEV_NS_STANDARD; 1699 } 1700 1701 nvme_ns->bdev = NULL; 1702 1703 if (ctx) { 1704 ctx->populates_in_progress++; 1705 } 1706 nvme_ctrlr_populate_namespace(nvme_bdev_ctrlr, nvme_ns, ctx); 1707 } 1708 1709 if (nvme_ns->populated && !ns_is_active) { 1710 nvme_ctrlr_depopulate_namespace(nvme_bdev_ctrlr, nvme_ns); 1711 } 1712 } 1713 1714 if (ctx) { 1715 /* Decrement this count now that the loop is over to account 1716 * for the one we started with. If the count is then 0, we 1717 * know any populate_namespace functions completed immediately, 1718 * so we'll kick the callback here. 1719 */ 1720 ctx->populates_in_progress--; 1721 if (ctx->populates_in_progress == 0) { 1722 nvme_ctrlr_populate_namespaces_done(nvme_bdev_ctrlr, ctx); 1723 } 1724 } 1725 1726 } 1727 1728 static void 1729 nvme_ctrlr_depopulate_namespaces(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr) 1730 { 1731 uint32_t i; 1732 struct nvme_bdev_ns *nvme_ns; 1733 1734 for (i = 0; i < nvme_bdev_ctrlr->num_ns; i++) { 1735 uint32_t nsid = i + 1; 1736 1737 nvme_ns = nvme_bdev_ctrlr->namespaces[nsid - 1]; 1738 if (nvme_ns->populated) { 1739 assert(nvme_ns->id == nsid); 1740 nvme_ctrlr_depopulate_namespace(nvme_bdev_ctrlr, nvme_ns); 1741 } 1742 } 1743 } 1744 1745 static void 1746 aer_cb(void *arg, const struct spdk_nvme_cpl *cpl) 1747 { 1748 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr = arg; 1749 union spdk_nvme_async_event_completion event; 1750 1751 if (spdk_nvme_cpl_is_error(cpl)) { 1752 SPDK_WARNLOG("AER request execute failed"); 1753 return; 1754 } 1755 1756 event.raw = cpl->cdw0; 1757 if ((event.bits.async_event_type == SPDK_NVME_ASYNC_EVENT_TYPE_NOTICE) && 1758 (event.bits.async_event_info == SPDK_NVME_ASYNC_EVENT_NS_ATTR_CHANGED)) { 1759 nvme_ctrlr_populate_namespaces(nvme_bdev_ctrlr, NULL); 1760 } else if ((event.bits.async_event_type == SPDK_NVME_ASYNC_EVENT_TYPE_VENDOR) && 1761 (event.bits.log_page_identifier == SPDK_OCSSD_LOG_CHUNK_NOTIFICATION) && 1762 spdk_nvme_ctrlr_is_ocssd_supported(nvme_bdev_ctrlr->ctrlr)) { 1763 bdev_ocssd_handle_chunk_notification(nvme_bdev_ctrlr); 1764 } 1765 } 1766 1767 static void 1768 populate_namespaces_cb(struct nvme_async_probe_ctx *ctx, size_t count, int rc) 1769 { 1770 if (ctx->cb_fn) { 1771 ctx->cb_fn(ctx->cb_ctx, count, rc); 1772 } 1773 1774 ctx->namespaces_populated = true; 1775 if (ctx->probe_done) { 1776 /* The probe was already completed, so we need to free the context 1777 * here. This can happen for cases like OCSSD, where we need to 1778 * send additional commands to the SSD after attach. 1779 */ 1780 free(ctx); 1781 } 1782 } 1783 1784 static int 1785 _nvme_bdev_ctrlr_create(struct spdk_nvme_ctrlr *ctrlr, 1786 const char *name, 1787 const struct spdk_nvme_transport_id *trid, 1788 uint32_t prchk_flags, 1789 struct nvme_bdev_ctrlr **_nvme_bdev_ctrlr) 1790 { 1791 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr; 1792 struct nvme_bdev_ctrlr_trid *trid_entry; 1793 uint32_t i; 1794 int rc; 1795 1796 nvme_bdev_ctrlr = calloc(1, sizeof(*nvme_bdev_ctrlr)); 1797 if (nvme_bdev_ctrlr == NULL) { 1798 SPDK_ERRLOG("Failed to allocate device struct\n"); 1799 return -ENOMEM; 1800 } 1801 1802 rc = pthread_mutex_init(&nvme_bdev_ctrlr->mutex, NULL); 1803 if (rc != 0) { 1804 goto err_init_mutex; 1805 } 1806 1807 TAILQ_INIT(&nvme_bdev_ctrlr->trids); 1808 nvme_bdev_ctrlr->num_ns = spdk_nvme_ctrlr_get_num_ns(ctrlr); 1809 if (nvme_bdev_ctrlr->num_ns != 0) { 1810 nvme_bdev_ctrlr->namespaces = calloc(nvme_bdev_ctrlr->num_ns, sizeof(struct nvme_bdev_ns *)); 1811 if (!nvme_bdev_ctrlr->namespaces) { 1812 SPDK_ERRLOG("Failed to allocate block namespaces pointer\n"); 1813 rc = -ENOMEM; 1814 goto err_alloc_namespaces; 1815 } 1816 } 1817 1818 trid_entry = calloc(1, sizeof(*trid_entry)); 1819 if (trid_entry == NULL) { 1820 SPDK_ERRLOG("Failed to allocate trid entry pointer\n"); 1821 rc = -ENOMEM; 1822 goto err_alloc_trid; 1823 } 1824 1825 trid_entry->trid = *trid; 1826 1827 for (i = 0; i < nvme_bdev_ctrlr->num_ns; i++) { 1828 nvme_bdev_ctrlr->namespaces[i] = calloc(1, sizeof(struct nvme_bdev_ns)); 1829 if (nvme_bdev_ctrlr->namespaces[i] == NULL) { 1830 SPDK_ERRLOG("Failed to allocate block namespace struct\n"); 1831 rc = -ENOMEM; 1832 goto err_alloc_namespace; 1833 } 1834 } 1835 1836 nvme_bdev_ctrlr->thread = spdk_get_thread(); 1837 nvme_bdev_ctrlr->adminq_timer_poller = NULL; 1838 nvme_bdev_ctrlr->ctrlr = ctrlr; 1839 nvme_bdev_ctrlr->ref = 1; 1840 nvme_bdev_ctrlr->connected_trid = &trid_entry->trid; 1841 nvme_bdev_ctrlr->name = strdup(name); 1842 if (nvme_bdev_ctrlr->name == NULL) { 1843 rc = -ENOMEM; 1844 goto err_alloc_name; 1845 } 1846 1847 if (spdk_nvme_ctrlr_is_ocssd_supported(nvme_bdev_ctrlr->ctrlr)) { 1848 rc = bdev_ocssd_init_ctrlr(nvme_bdev_ctrlr); 1849 if (spdk_unlikely(rc != 0)) { 1850 SPDK_ERRLOG("Unable to initialize OCSSD controller\n"); 1851 goto err_init_ocssd; 1852 } 1853 } 1854 1855 nvme_bdev_ctrlr->prchk_flags = prchk_flags; 1856 1857 spdk_io_device_register(nvme_bdev_ctrlr, 1858 bdev_nvme_create_path_cb, 1859 bdev_nvme_destroy_path_cb, 1860 sizeof(struct nvme_io_path), 1861 name); 1862 1863 nvme_bdev_ctrlr->adminq_timer_poller = SPDK_POLLER_REGISTER(bdev_nvme_poll_adminq, nvme_bdev_ctrlr, 1864 g_opts.nvme_adminq_poll_period_us); 1865 1866 TAILQ_INSERT_TAIL(&g_nvme_bdev_ctrlrs, nvme_bdev_ctrlr, tailq); 1867 1868 if (g_opts.timeout_us > 0) { 1869 spdk_nvme_ctrlr_register_timeout_callback(ctrlr, g_opts.timeout_us, 1870 timeout_cb, nvme_bdev_ctrlr); 1871 } 1872 1873 spdk_nvme_ctrlr_register_aer_callback(ctrlr, aer_cb, nvme_bdev_ctrlr); 1874 spdk_nvme_ctrlr_set_remove_cb(ctrlr, remove_cb, nvme_bdev_ctrlr); 1875 1876 if (spdk_nvme_ctrlr_get_flags(nvme_bdev_ctrlr->ctrlr) & 1877 SPDK_NVME_CTRLR_SECURITY_SEND_RECV_SUPPORTED) { 1878 nvme_bdev_ctrlr->opal_dev = spdk_opal_dev_construct(nvme_bdev_ctrlr->ctrlr); 1879 } 1880 1881 TAILQ_INSERT_HEAD(&nvme_bdev_ctrlr->trids, trid_entry, link); 1882 1883 if (_nvme_bdev_ctrlr != NULL) { 1884 *_nvme_bdev_ctrlr = nvme_bdev_ctrlr; 1885 } 1886 return 0; 1887 1888 err_init_ocssd: 1889 free(nvme_bdev_ctrlr->name); 1890 err_alloc_name: 1891 err_alloc_namespace: 1892 for (; i > 0; i--) { 1893 free(nvme_bdev_ctrlr->namespaces[i - 1]); 1894 } 1895 free(trid_entry); 1896 err_alloc_trid: 1897 free(nvme_bdev_ctrlr->namespaces); 1898 err_alloc_namespaces: 1899 pthread_mutex_destroy(&nvme_bdev_ctrlr->mutex); 1900 err_init_mutex: 1901 free(nvme_bdev_ctrlr); 1902 return rc; 1903 } 1904 1905 static void 1906 nvme_bdev_ctrlr_create(struct spdk_nvme_ctrlr *ctrlr, 1907 const char *name, 1908 const struct spdk_nvme_transport_id *trid, 1909 uint32_t prchk_flags, 1910 struct nvme_async_probe_ctx *ctx) 1911 { 1912 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr = NULL; 1913 int rc; 1914 1915 rc = _nvme_bdev_ctrlr_create(ctrlr, name, trid, prchk_flags, &nvme_bdev_ctrlr); 1916 if (rc != 0) { 1917 SPDK_ERRLOG("Failed to create new NVMe controller\n"); 1918 goto err; 1919 } 1920 1921 nvme_ctrlr_populate_namespaces(nvme_bdev_ctrlr, ctx); 1922 return; 1923 1924 err: 1925 if (ctx != NULL) { 1926 populate_namespaces_cb(ctx, 0, rc); 1927 } 1928 } 1929 1930 static void 1931 attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid, 1932 struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts) 1933 { 1934 struct nvme_probe_ctx *ctx = cb_ctx; 1935 char *name = NULL; 1936 uint32_t prchk_flags = 0; 1937 size_t i; 1938 1939 if (ctx) { 1940 for (i = 0; i < ctx->count; i++) { 1941 if (spdk_nvme_transport_id_compare(trid, &ctx->trids[i]) == 0) { 1942 prchk_flags = ctx->prchk_flags[i]; 1943 name = strdup(ctx->names[i]); 1944 break; 1945 } 1946 } 1947 } else { 1948 name = spdk_sprintf_alloc("HotInNvme%d", g_hot_insert_nvme_controller_index++); 1949 } 1950 if (!name) { 1951 SPDK_ERRLOG("Failed to assign name to NVMe device\n"); 1952 return; 1953 } 1954 1955 SPDK_DEBUGLOG(bdev_nvme, "Attached to %s (%s)\n", trid->traddr, name); 1956 1957 nvme_bdev_ctrlr_create(ctrlr, name, trid, prchk_flags, NULL); 1958 1959 free(name); 1960 } 1961 1962 static void 1963 _nvme_bdev_ctrlr_destruct(void *ctx) 1964 { 1965 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr = ctx; 1966 1967 nvme_ctrlr_depopulate_namespaces(nvme_bdev_ctrlr); 1968 nvme_bdev_ctrlr_destruct(nvme_bdev_ctrlr); 1969 } 1970 1971 static int 1972 _bdev_nvme_delete(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, bool hotplug) 1973 { 1974 struct nvme_probe_skip_entry *entry; 1975 1976 pthread_mutex_lock(&nvme_bdev_ctrlr->mutex); 1977 1978 /* The controller's destruction was already started */ 1979 if (nvme_bdev_ctrlr->destruct) { 1980 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 1981 return 0; 1982 } 1983 1984 if (!hotplug && 1985 nvme_bdev_ctrlr->connected_trid->trtype == SPDK_NVME_TRANSPORT_PCIE) { 1986 entry = calloc(1, sizeof(*entry)); 1987 if (!entry) { 1988 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 1989 return -ENOMEM; 1990 } 1991 entry->trid = *nvme_bdev_ctrlr->connected_trid; 1992 TAILQ_INSERT_TAIL(&g_skipped_nvme_ctrlrs, entry, tailq); 1993 } 1994 1995 nvme_bdev_ctrlr->destruct = true; 1996 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 1997 1998 _nvme_bdev_ctrlr_destruct(nvme_bdev_ctrlr); 1999 2000 return 0; 2001 } 2002 2003 static void 2004 remove_cb(void *cb_ctx, struct spdk_nvme_ctrlr *ctrlr) 2005 { 2006 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr = cb_ctx; 2007 2008 _bdev_nvme_delete(nvme_bdev_ctrlr, true); 2009 } 2010 2011 static int 2012 bdev_nvme_hotplug_probe(void *arg) 2013 { 2014 if (g_hotplug_probe_ctx == NULL) { 2015 spdk_poller_unregister(&g_hotplug_probe_poller); 2016 return SPDK_POLLER_IDLE; 2017 } 2018 2019 if (spdk_nvme_probe_poll_async(g_hotplug_probe_ctx) != -EAGAIN) { 2020 g_hotplug_probe_ctx = NULL; 2021 spdk_poller_unregister(&g_hotplug_probe_poller); 2022 } 2023 2024 return SPDK_POLLER_BUSY; 2025 } 2026 2027 static int 2028 bdev_nvme_hotplug(void *arg) 2029 { 2030 struct spdk_nvme_transport_id trid_pcie; 2031 2032 if (g_hotplug_probe_ctx) { 2033 return SPDK_POLLER_BUSY; 2034 } 2035 2036 memset(&trid_pcie, 0, sizeof(trid_pcie)); 2037 spdk_nvme_trid_populate_transport(&trid_pcie, SPDK_NVME_TRANSPORT_PCIE); 2038 2039 g_hotplug_probe_ctx = spdk_nvme_probe_async(&trid_pcie, NULL, 2040 hotplug_probe_cb, attach_cb, NULL); 2041 2042 if (g_hotplug_probe_ctx) { 2043 assert(g_hotplug_probe_poller == NULL); 2044 g_hotplug_probe_poller = SPDK_POLLER_REGISTER(bdev_nvme_hotplug_probe, NULL, 1000); 2045 } 2046 2047 return SPDK_POLLER_BUSY; 2048 } 2049 2050 void 2051 bdev_nvme_get_opts(struct spdk_bdev_nvme_opts *opts) 2052 { 2053 *opts = g_opts; 2054 } 2055 2056 int 2057 bdev_nvme_set_opts(const struct spdk_bdev_nvme_opts *opts) 2058 { 2059 if (g_bdev_nvme_init_thread != NULL) { 2060 if (!TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) { 2061 return -EPERM; 2062 } 2063 } 2064 2065 g_opts = *opts; 2066 2067 return 0; 2068 } 2069 2070 struct set_nvme_hotplug_ctx { 2071 uint64_t period_us; 2072 bool enabled; 2073 spdk_msg_fn fn; 2074 void *fn_ctx; 2075 }; 2076 2077 static void 2078 set_nvme_hotplug_period_cb(void *_ctx) 2079 { 2080 struct set_nvme_hotplug_ctx *ctx = _ctx; 2081 2082 spdk_poller_unregister(&g_hotplug_poller); 2083 if (ctx->enabled) { 2084 g_hotplug_poller = SPDK_POLLER_REGISTER(bdev_nvme_hotplug, NULL, ctx->period_us); 2085 } 2086 2087 g_nvme_hotplug_poll_period_us = ctx->period_us; 2088 g_nvme_hotplug_enabled = ctx->enabled; 2089 if (ctx->fn) { 2090 ctx->fn(ctx->fn_ctx); 2091 } 2092 2093 free(ctx); 2094 } 2095 2096 int 2097 bdev_nvme_set_hotplug(bool enabled, uint64_t period_us, spdk_msg_fn cb, void *cb_ctx) 2098 { 2099 struct set_nvme_hotplug_ctx *ctx; 2100 2101 if (enabled == true && !spdk_process_is_primary()) { 2102 return -EPERM; 2103 } 2104 2105 ctx = calloc(1, sizeof(*ctx)); 2106 if (ctx == NULL) { 2107 return -ENOMEM; 2108 } 2109 2110 period_us = period_us == 0 ? NVME_HOTPLUG_POLL_PERIOD_DEFAULT : period_us; 2111 ctx->period_us = spdk_min(period_us, NVME_HOTPLUG_POLL_PERIOD_MAX); 2112 ctx->enabled = enabled; 2113 ctx->fn = cb; 2114 ctx->fn_ctx = cb_ctx; 2115 2116 spdk_thread_send_msg(g_bdev_nvme_init_thread, set_nvme_hotplug_period_cb, ctx); 2117 return 0; 2118 } 2119 2120 static void 2121 nvme_ctrlr_populate_namespaces_done(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, 2122 struct nvme_async_probe_ctx *ctx) 2123 { 2124 struct nvme_bdev_ns *nvme_ns; 2125 struct nvme_bdev *nvme_bdev; 2126 uint32_t i, nsid; 2127 size_t j; 2128 2129 assert(nvme_bdev_ctrlr != NULL); 2130 2131 /* 2132 * Report the new bdevs that were created in this call. 2133 * There can be more than one bdev per NVMe controller. 2134 */ 2135 j = 0; 2136 for (i = 0; i < nvme_bdev_ctrlr->num_ns; i++) { 2137 nsid = i + 1; 2138 nvme_ns = nvme_bdev_ctrlr->namespaces[nsid - 1]; 2139 if (!nvme_ns->populated) { 2140 continue; 2141 } 2142 assert(nvme_ns->id == nsid); 2143 nvme_bdev = nvme_ns->bdev; 2144 if (nvme_bdev == NULL) { 2145 assert(nvme_ns->type == NVME_BDEV_NS_OCSSD); 2146 continue; 2147 } 2148 if (j < ctx->count) { 2149 ctx->names[j] = nvme_bdev->disk.name; 2150 j++; 2151 } else { 2152 SPDK_ERRLOG("Maximum number of namespaces supported per NVMe controller is %du. Unable to return all names of created bdevs\n", 2153 ctx->count); 2154 populate_namespaces_cb(ctx, 0, -ERANGE); 2155 return; 2156 } 2157 } 2158 2159 populate_namespaces_cb(ctx, j, 0); 2160 } 2161 2162 static int 2163 bdev_nvme_compare_trids(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, 2164 struct spdk_nvme_ctrlr *new_ctrlr, 2165 struct spdk_nvme_transport_id *trid) 2166 { 2167 struct nvme_bdev_ctrlr_trid *tmp_trid; 2168 2169 if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) { 2170 SPDK_ERRLOG("PCIe failover is not supported.\n"); 2171 return -ENOTSUP; 2172 } 2173 2174 /* Currently we only support failover to the same transport type. */ 2175 if (nvme_bdev_ctrlr->connected_trid->trtype != trid->trtype) { 2176 return -EINVAL; 2177 } 2178 2179 /* Currently we only support failover to the same NQN. */ 2180 if (strncmp(trid->subnqn, nvme_bdev_ctrlr->connected_trid->subnqn, SPDK_NVMF_NQN_MAX_LEN)) { 2181 return -EINVAL; 2182 } 2183 2184 /* Skip all the other checks if we've already registered this path. */ 2185 TAILQ_FOREACH(tmp_trid, &nvme_bdev_ctrlr->trids, link) { 2186 if (!spdk_nvme_transport_id_compare(&tmp_trid->trid, trid)) { 2187 return -EEXIST; 2188 } 2189 } 2190 2191 return 0; 2192 } 2193 2194 static int 2195 bdev_nvme_compare_namespaces(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, 2196 struct spdk_nvme_ctrlr *new_ctrlr) 2197 { 2198 uint32_t i, nsid; 2199 struct nvme_bdev_ns *nvme_ns; 2200 struct spdk_nvme_ns *new_ns; 2201 2202 if (spdk_nvme_ctrlr_get_num_ns(new_ctrlr) != nvme_bdev_ctrlr->num_ns) { 2203 return -EINVAL; 2204 } 2205 2206 for (i = 0; i < nvme_bdev_ctrlr->num_ns; i++) { 2207 nsid = i + 1; 2208 2209 nvme_ns = nvme_bdev_ctrlr->namespaces[i]; 2210 if (!nvme_ns->populated) { 2211 continue; 2212 } 2213 2214 new_ns = spdk_nvme_ctrlr_get_ns(new_ctrlr, nsid); 2215 assert(new_ns != NULL); 2216 2217 if (!bdev_nvme_compare_ns(nvme_ns->ns, new_ns)) { 2218 return -EINVAL; 2219 } 2220 } 2221 2222 return 0; 2223 } 2224 2225 static int 2226 _bdev_nvme_add_secondary_trid(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, 2227 struct spdk_nvme_transport_id *trid) 2228 { 2229 struct nvme_bdev_ctrlr_trid *new_trid, *tmp_trid; 2230 2231 new_trid = calloc(1, sizeof(*new_trid)); 2232 if (new_trid == NULL) { 2233 return -ENOMEM; 2234 } 2235 new_trid->trid = *trid; 2236 new_trid->is_failed = false; 2237 2238 TAILQ_FOREACH(tmp_trid, &nvme_bdev_ctrlr->trids, link) { 2239 if (tmp_trid->is_failed) { 2240 TAILQ_INSERT_BEFORE(tmp_trid, new_trid, link); 2241 return 0; 2242 } 2243 } 2244 2245 TAILQ_INSERT_TAIL(&nvme_bdev_ctrlr->trids, new_trid, link); 2246 return 0; 2247 } 2248 2249 /* This is the case that a secondary path is added to an existing 2250 * nvme_bdev_ctrlr for failover. After checking if it can access the same 2251 * namespaces as the primary path, it is disconnected until failover occurs. 2252 */ 2253 static void 2254 bdev_nvme_add_secondary_trid(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, 2255 struct spdk_nvme_ctrlr *new_ctrlr, 2256 struct spdk_nvme_transport_id *trid, 2257 struct nvme_async_probe_ctx *ctx) 2258 { 2259 int rc; 2260 2261 assert(nvme_bdev_ctrlr != NULL); 2262 2263 pthread_mutex_lock(&nvme_bdev_ctrlr->mutex); 2264 2265 rc = bdev_nvme_compare_trids(nvme_bdev_ctrlr, new_ctrlr, trid); 2266 if (rc != 0) { 2267 goto exit; 2268 } 2269 2270 rc = bdev_nvme_compare_namespaces(nvme_bdev_ctrlr, new_ctrlr); 2271 if (rc != 0) { 2272 goto exit; 2273 } 2274 2275 rc = _bdev_nvme_add_secondary_trid(nvme_bdev_ctrlr, trid); 2276 2277 exit: 2278 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 2279 2280 spdk_nvme_detach(new_ctrlr); 2281 2282 if (ctx != NULL) { 2283 populate_namespaces_cb(ctx, 0, rc); 2284 } 2285 } 2286 2287 static void 2288 connect_attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid, 2289 struct spdk_nvme_ctrlr *ctrlr, const struct spdk_nvme_ctrlr_opts *opts) 2290 { 2291 struct spdk_nvme_ctrlr_opts *user_opts = cb_ctx; 2292 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr; 2293 struct nvme_async_probe_ctx *ctx; 2294 2295 ctx = SPDK_CONTAINEROF(user_opts, struct nvme_async_probe_ctx, opts); 2296 ctx->ctrlr_attached = true; 2297 2298 nvme_bdev_ctrlr = nvme_bdev_ctrlr_get_by_name(ctx->base_name); 2299 if (nvme_bdev_ctrlr) { 2300 bdev_nvme_add_secondary_trid(nvme_bdev_ctrlr, ctrlr, &ctx->trid, ctx); 2301 return; 2302 } 2303 2304 nvme_bdev_ctrlr_create(ctrlr, ctx->base_name, &ctx->trid, ctx->prchk_flags, ctx); 2305 } 2306 2307 static int 2308 bdev_nvme_async_poll(void *arg) 2309 { 2310 struct nvme_async_probe_ctx *ctx = arg; 2311 int rc; 2312 2313 rc = spdk_nvme_probe_poll_async(ctx->probe_ctx); 2314 if (spdk_unlikely(rc != -EAGAIN)) { 2315 ctx->probe_done = true; 2316 spdk_poller_unregister(&ctx->poller); 2317 if (!ctx->ctrlr_attached) { 2318 /* The probe is done, but no controller was attached. 2319 * That means we had a failure, so report -EIO back to 2320 * the caller (usually the RPC). populate_namespaces_cb() 2321 * will take care of freeing the nvme_async_probe_ctx. 2322 */ 2323 populate_namespaces_cb(ctx, 0, -EIO); 2324 } else if (ctx->namespaces_populated) { 2325 /* The namespaces for the attached controller were all 2326 * populated and the response was already sent to the 2327 * caller (usually the RPC). So free the context here. 2328 */ 2329 free(ctx); 2330 } 2331 } 2332 2333 return SPDK_POLLER_BUSY; 2334 } 2335 2336 int 2337 bdev_nvme_create(struct spdk_nvme_transport_id *trid, 2338 struct spdk_nvme_host_id *hostid, 2339 const char *base_name, 2340 const char **names, 2341 uint32_t count, 2342 const char *hostnqn, 2343 uint32_t prchk_flags, 2344 spdk_bdev_create_nvme_fn cb_fn, 2345 void *cb_ctx, 2346 struct spdk_nvme_ctrlr_opts *opts) 2347 { 2348 struct nvme_probe_skip_entry *entry, *tmp; 2349 struct nvme_async_probe_ctx *ctx; 2350 2351 /* TODO expand this check to include both the host and target TRIDs. 2352 * Only if both are the same should we fail. 2353 */ 2354 if (nvme_bdev_ctrlr_get(trid) != NULL) { 2355 SPDK_ERRLOG("A controller with the provided trid (traddr: %s) already exists.\n", trid->traddr); 2356 return -EEXIST; 2357 } 2358 2359 ctx = calloc(1, sizeof(*ctx)); 2360 if (!ctx) { 2361 return -ENOMEM; 2362 } 2363 ctx->base_name = base_name; 2364 ctx->names = names; 2365 ctx->count = count; 2366 ctx->cb_fn = cb_fn; 2367 ctx->cb_ctx = cb_ctx; 2368 ctx->prchk_flags = prchk_flags; 2369 ctx->trid = *trid; 2370 2371 if (trid->trtype == SPDK_NVME_TRANSPORT_PCIE) { 2372 TAILQ_FOREACH_SAFE(entry, &g_skipped_nvme_ctrlrs, tailq, tmp) { 2373 if (spdk_nvme_transport_id_compare(trid, &entry->trid) == 0) { 2374 TAILQ_REMOVE(&g_skipped_nvme_ctrlrs, entry, tailq); 2375 free(entry); 2376 break; 2377 } 2378 } 2379 } 2380 2381 if (opts) { 2382 memcpy(&ctx->opts, opts, sizeof(*opts)); 2383 } else { 2384 spdk_nvme_ctrlr_get_default_ctrlr_opts(&ctx->opts, sizeof(ctx->opts)); 2385 } 2386 2387 ctx->opts.transport_retry_count = g_opts.retry_count; 2388 ctx->opts.keep_alive_timeout_ms = g_opts.keep_alive_timeout_ms; 2389 2390 if (hostnqn) { 2391 snprintf(ctx->opts.hostnqn, sizeof(ctx->opts.hostnqn), "%s", hostnqn); 2392 } 2393 2394 if (hostid->hostaddr[0] != '\0') { 2395 snprintf(ctx->opts.src_addr, sizeof(ctx->opts.src_addr), "%s", hostid->hostaddr); 2396 } 2397 2398 if (hostid->hostsvcid[0] != '\0') { 2399 snprintf(ctx->opts.src_svcid, sizeof(ctx->opts.src_svcid), "%s", hostid->hostsvcid); 2400 } 2401 2402 ctx->probe_ctx = spdk_nvme_connect_async(trid, &ctx->opts, connect_attach_cb); 2403 if (ctx->probe_ctx == NULL) { 2404 SPDK_ERRLOG("No controller was found with provided trid (traddr: %s)\n", trid->traddr); 2405 free(ctx); 2406 return -ENODEV; 2407 } 2408 ctx->poller = SPDK_POLLER_REGISTER(bdev_nvme_async_poll, ctx, 1000); 2409 2410 return 0; 2411 } 2412 2413 static int 2414 bdev_nvme_delete_secondary_trid(struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, 2415 const struct spdk_nvme_transport_id *trid) 2416 { 2417 struct nvme_bdev_ctrlr_trid *ctrlr_trid, *tmp_trid; 2418 2419 if (!spdk_nvme_transport_id_compare(trid, nvme_bdev_ctrlr->connected_trid)) { 2420 return -EBUSY; 2421 } 2422 2423 TAILQ_FOREACH_SAFE(ctrlr_trid, &nvme_bdev_ctrlr->trids, link, tmp_trid) { 2424 if (!spdk_nvme_transport_id_compare(&ctrlr_trid->trid, trid)) { 2425 TAILQ_REMOVE(&nvme_bdev_ctrlr->trids, ctrlr_trid, link); 2426 free(ctrlr_trid); 2427 return 0; 2428 } 2429 } 2430 2431 return -ENXIO; 2432 } 2433 2434 int 2435 bdev_nvme_delete(const char *name, const struct spdk_nvme_transport_id *trid) 2436 { 2437 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr; 2438 struct nvme_bdev_ctrlr_trid *ctrlr_trid; 2439 2440 if (name == NULL) { 2441 return -EINVAL; 2442 } 2443 2444 nvme_bdev_ctrlr = nvme_bdev_ctrlr_get_by_name(name); 2445 if (nvme_bdev_ctrlr == NULL) { 2446 SPDK_ERRLOG("Failed to find NVMe controller\n"); 2447 return -ENODEV; 2448 } 2449 2450 /* case 1: remove the controller itself. */ 2451 if (trid == NULL) { 2452 return _bdev_nvme_delete(nvme_bdev_ctrlr, false); 2453 } 2454 2455 /* case 2: we are currently using the path to be removed. */ 2456 if (!spdk_nvme_transport_id_compare(trid, nvme_bdev_ctrlr->connected_trid)) { 2457 ctrlr_trid = TAILQ_FIRST(&nvme_bdev_ctrlr->trids); 2458 assert(nvme_bdev_ctrlr->connected_trid == &ctrlr_trid->trid); 2459 /* case 2A: the current path is the only path. */ 2460 if (!TAILQ_NEXT(ctrlr_trid, link)) { 2461 return _bdev_nvme_delete(nvme_bdev_ctrlr, false); 2462 } 2463 2464 /* case 2B: there is an alternative path. */ 2465 return bdev_nvme_failover(nvme_bdev_ctrlr, true); 2466 } 2467 2468 /* case 3: We are not using the specified path. */ 2469 return bdev_nvme_delete_secondary_trid(nvme_bdev_ctrlr, trid); 2470 } 2471 2472 static int 2473 bdev_nvme_library_init(void) 2474 { 2475 g_bdev_nvme_init_thread = spdk_get_thread(); 2476 2477 spdk_io_device_register(&g_nvme_bdev_ctrlrs, bdev_nvme_poll_group_create_cb, 2478 bdev_nvme_poll_group_destroy_cb, 2479 sizeof(struct nvme_bdev_poll_group), "bdev_nvme_poll_groups"); 2480 2481 return 0; 2482 } 2483 2484 static void 2485 bdev_nvme_library_fini(void) 2486 { 2487 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr, *tmp; 2488 struct nvme_probe_skip_entry *entry, *entry_tmp; 2489 2490 spdk_poller_unregister(&g_hotplug_poller); 2491 free(g_hotplug_probe_ctx); 2492 g_hotplug_probe_ctx = NULL; 2493 2494 TAILQ_FOREACH_SAFE(entry, &g_skipped_nvme_ctrlrs, tailq, entry_tmp) { 2495 TAILQ_REMOVE(&g_skipped_nvme_ctrlrs, entry, tailq); 2496 free(entry); 2497 } 2498 2499 pthread_mutex_lock(&g_bdev_nvme_mutex); 2500 TAILQ_FOREACH_SAFE(nvme_bdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq, tmp) { 2501 pthread_mutex_lock(&nvme_bdev_ctrlr->mutex); 2502 if (nvme_bdev_ctrlr->destruct) { 2503 /* This controller's destruction was already started 2504 * before the application started shutting down 2505 */ 2506 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 2507 continue; 2508 } 2509 nvme_bdev_ctrlr->destruct = true; 2510 pthread_mutex_unlock(&nvme_bdev_ctrlr->mutex); 2511 2512 spdk_thread_send_msg(nvme_bdev_ctrlr->thread, _nvme_bdev_ctrlr_destruct, 2513 nvme_bdev_ctrlr); 2514 } 2515 2516 g_bdev_nvme_module_finish = true; 2517 if (TAILQ_EMPTY(&g_nvme_bdev_ctrlrs)) { 2518 pthread_mutex_unlock(&g_bdev_nvme_mutex); 2519 spdk_io_device_unregister(&g_nvme_bdev_ctrlrs, NULL); 2520 spdk_bdev_module_finish_done(); 2521 return; 2522 } 2523 2524 pthread_mutex_unlock(&g_bdev_nvme_mutex); 2525 } 2526 2527 static void 2528 bdev_nvme_verify_pi_error(struct nvme_bdev_io *bio) 2529 { 2530 struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio); 2531 struct spdk_bdev *bdev = bdev_io->bdev; 2532 struct spdk_dif_ctx dif_ctx; 2533 struct spdk_dif_error err_blk = {}; 2534 int rc; 2535 2536 rc = spdk_dif_ctx_init(&dif_ctx, 2537 bdev->blocklen, bdev->md_len, bdev->md_interleave, 2538 bdev->dif_is_head_of_md, bdev->dif_type, bdev->dif_check_flags, 2539 bdev_io->u.bdev.offset_blocks, 0, 0, 0, 0); 2540 if (rc != 0) { 2541 SPDK_ERRLOG("Initialization of DIF context failed\n"); 2542 return; 2543 } 2544 2545 if (bdev->md_interleave) { 2546 rc = spdk_dif_verify(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt, 2547 bdev_io->u.bdev.num_blocks, &dif_ctx, &err_blk); 2548 } else { 2549 struct iovec md_iov = { 2550 .iov_base = bdev_io->u.bdev.md_buf, 2551 .iov_len = bdev_io->u.bdev.num_blocks * bdev->md_len, 2552 }; 2553 2554 rc = spdk_dix_verify(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt, 2555 &md_iov, bdev_io->u.bdev.num_blocks, &dif_ctx, &err_blk); 2556 } 2557 2558 if (rc != 0) { 2559 SPDK_ERRLOG("DIF error detected. type=%d, offset=%" PRIu32 "\n", 2560 err_blk.err_type, err_blk.err_offset); 2561 } else { 2562 SPDK_ERRLOG("Hardware reported PI error but SPDK could not find any.\n"); 2563 } 2564 } 2565 2566 static void 2567 bdev_nvme_no_pi_readv_done(void *ref, const struct spdk_nvme_cpl *cpl) 2568 { 2569 struct nvme_bdev_io *bio = ref; 2570 2571 if (spdk_nvme_cpl_is_success(cpl)) { 2572 /* Run PI verification for read data buffer. */ 2573 bdev_nvme_verify_pi_error(bio); 2574 } 2575 2576 /* Return original completion status */ 2577 bdev_nvme_io_complete_nvme_status(bio, &bio->cpl); 2578 } 2579 2580 static void 2581 bdev_nvme_readv_done(void *ref, const struct spdk_nvme_cpl *cpl) 2582 { 2583 struct nvme_bdev_io *bio = ref; 2584 struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio); 2585 struct nvme_bdev *nbdev = (struct nvme_bdev *)bdev_io->bdev->ctxt; 2586 struct nvme_io_path *io_path; 2587 struct spdk_nvme_ns *ns; 2588 struct spdk_nvme_qpair *qpair; 2589 int ret; 2590 2591 if (spdk_unlikely(spdk_nvme_cpl_is_pi_error(cpl))) { 2592 SPDK_ERRLOG("readv completed with PI error (sct=%d, sc=%d)\n", 2593 cpl->status.sct, cpl->status.sc); 2594 2595 /* Save completion status to use after verifying PI error. */ 2596 bio->cpl = *cpl; 2597 2598 io_path = spdk_io_channel_get_ctx(spdk_bdev_io_get_io_channel(bdev_io)); 2599 2600 if (spdk_likely(bdev_nvme_find_io_path(nbdev, io_path, &ns, &qpair))) { 2601 /* Read without PI checking to verify PI error. */ 2602 ret = bdev_nvme_no_pi_readv(ns, 2603 qpair, 2604 bio, 2605 bdev_io->u.bdev.iovs, 2606 bdev_io->u.bdev.iovcnt, 2607 bdev_io->u.bdev.md_buf, 2608 bdev_io->u.bdev.num_blocks, 2609 bdev_io->u.bdev.offset_blocks); 2610 if (ret == 0) { 2611 return; 2612 } 2613 } 2614 } 2615 2616 bdev_nvme_io_complete_nvme_status(bio, cpl); 2617 } 2618 2619 static void 2620 bdev_nvme_writev_done(void *ref, const struct spdk_nvme_cpl *cpl) 2621 { 2622 struct nvme_bdev_io *bio = ref; 2623 2624 if (spdk_nvme_cpl_is_pi_error(cpl)) { 2625 SPDK_ERRLOG("writev completed with PI error (sct=%d, sc=%d)\n", 2626 cpl->status.sct, cpl->status.sc); 2627 /* Run PI verification for write data buffer if PI error is detected. */ 2628 bdev_nvme_verify_pi_error(bio); 2629 } 2630 2631 bdev_nvme_io_complete_nvme_status(bio, cpl); 2632 } 2633 2634 static void 2635 bdev_nvme_zone_appendv_done(void *ref, const struct spdk_nvme_cpl *cpl) 2636 { 2637 struct nvme_bdev_io *bio = ref; 2638 struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio); 2639 2640 /* spdk_bdev_io_get_append_location() requires that the ALBA is stored in offset_blocks. 2641 * Additionally, offset_blocks has to be set before calling bdev_nvme_verify_pi_error(). 2642 */ 2643 bdev_io->u.bdev.offset_blocks = *(uint64_t *)&cpl->cdw0; 2644 2645 if (spdk_nvme_cpl_is_pi_error(cpl)) { 2646 SPDK_ERRLOG("zone append completed with PI error (sct=%d, sc=%d)\n", 2647 cpl->status.sct, cpl->status.sc); 2648 /* Run PI verification for zone append data buffer if PI error is detected. */ 2649 bdev_nvme_verify_pi_error(bio); 2650 } 2651 2652 bdev_nvme_io_complete_nvme_status(bio, cpl); 2653 } 2654 2655 static void 2656 bdev_nvme_comparev_done(void *ref, const struct spdk_nvme_cpl *cpl) 2657 { 2658 struct nvme_bdev_io *bio = ref; 2659 2660 if (spdk_nvme_cpl_is_pi_error(cpl)) { 2661 SPDK_ERRLOG("comparev completed with PI error (sct=%d, sc=%d)\n", 2662 cpl->status.sct, cpl->status.sc); 2663 /* Run PI verification for compare data buffer if PI error is detected. */ 2664 bdev_nvme_verify_pi_error(bio); 2665 } 2666 2667 bdev_nvme_io_complete_nvme_status(bio, cpl); 2668 } 2669 2670 static void 2671 bdev_nvme_comparev_and_writev_done(void *ref, const struct spdk_nvme_cpl *cpl) 2672 { 2673 struct nvme_bdev_io *bio = ref; 2674 2675 /* Compare operation completion */ 2676 if ((cpl->cdw0 & 0xFF) == SPDK_NVME_OPC_COMPARE) { 2677 /* Save compare result for write callback */ 2678 bio->cpl = *cpl; 2679 return; 2680 } 2681 2682 /* Write operation completion */ 2683 if (spdk_nvme_cpl_is_error(&bio->cpl)) { 2684 /* If bio->cpl is already an error, it means the compare operation failed. In that case, 2685 * complete the IO with the compare operation's status. 2686 */ 2687 if (!spdk_nvme_cpl_is_error(cpl)) { 2688 SPDK_ERRLOG("Unexpected write success after compare failure.\n"); 2689 } 2690 2691 bdev_nvme_io_complete_nvme_status(bio, &bio->cpl); 2692 } else { 2693 bdev_nvme_io_complete_nvme_status(bio, cpl); 2694 } 2695 } 2696 2697 static void 2698 bdev_nvme_queued_done(void *ref, const struct spdk_nvme_cpl *cpl) 2699 { 2700 struct nvme_bdev_io *bio = ref; 2701 2702 bdev_nvme_io_complete_nvme_status(bio, cpl); 2703 } 2704 2705 static int 2706 fill_zone_from_report(struct spdk_bdev_zone_info *info, struct spdk_nvme_zns_zone_desc *desc) 2707 { 2708 switch (desc->zs) { 2709 case SPDK_NVME_ZONE_STATE_EMPTY: 2710 info->state = SPDK_BDEV_ZONE_STATE_EMPTY; 2711 break; 2712 case SPDK_NVME_ZONE_STATE_IOPEN: 2713 info->state = SPDK_BDEV_ZONE_STATE_IMP_OPEN; 2714 break; 2715 case SPDK_NVME_ZONE_STATE_EOPEN: 2716 info->state = SPDK_BDEV_ZONE_STATE_EXP_OPEN; 2717 break; 2718 case SPDK_NVME_ZONE_STATE_CLOSED: 2719 info->state = SPDK_BDEV_ZONE_STATE_CLOSED; 2720 break; 2721 case SPDK_NVME_ZONE_STATE_RONLY: 2722 info->state = SPDK_BDEV_ZONE_STATE_READ_ONLY; 2723 break; 2724 case SPDK_NVME_ZONE_STATE_FULL: 2725 info->state = SPDK_BDEV_ZONE_STATE_FULL; 2726 break; 2727 case SPDK_NVME_ZONE_STATE_OFFLINE: 2728 info->state = SPDK_BDEV_ZONE_STATE_OFFLINE; 2729 break; 2730 default: 2731 SPDK_ERRLOG("Invalid zone state: %#x in zone report\n", desc->zs); 2732 return -EIO; 2733 } 2734 2735 info->zone_id = desc->zslba; 2736 info->write_pointer = desc->wp; 2737 info->capacity = desc->zcap; 2738 2739 return 0; 2740 } 2741 2742 static void 2743 bdev_nvme_get_zone_info_done(void *ref, const struct spdk_nvme_cpl *cpl) 2744 { 2745 struct nvme_bdev_io *bio = ref; 2746 struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio); 2747 struct nvme_bdev *nbdev = (struct nvme_bdev *)bdev_io->bdev->ctxt; 2748 struct spdk_io_channel *ch = spdk_bdev_io_get_io_channel(bdev_io); 2749 struct nvme_io_path *io_path = spdk_io_channel_get_ctx(ch); 2750 uint64_t zone_id = bdev_io->u.zone_mgmt.zone_id; 2751 uint32_t zones_to_copy = bdev_io->u.zone_mgmt.num_zones; 2752 struct spdk_bdev_zone_info *info = bdev_io->u.zone_mgmt.buf; 2753 uint64_t max_zones_per_buf, i; 2754 uint32_t zone_report_bufsize; 2755 struct spdk_nvme_ns *ns; 2756 struct spdk_nvme_qpair *qpair; 2757 int ret; 2758 2759 if (spdk_nvme_cpl_is_error(cpl)) { 2760 goto out_complete_io_nvme_cpl; 2761 } 2762 2763 if (!bdev_nvme_find_io_path(nbdev, io_path, &ns, &qpair)) { 2764 ret = -ENXIO; 2765 goto out_complete_io_ret; 2766 } 2767 2768 zone_report_bufsize = spdk_nvme_ns_get_max_io_xfer_size(ns); 2769 max_zones_per_buf = (zone_report_bufsize - sizeof(*bio->zone_report_buf)) / 2770 sizeof(bio->zone_report_buf->descs[0]); 2771 2772 if (bio->zone_report_buf->nr_zones > max_zones_per_buf) { 2773 ret = -EINVAL; 2774 goto out_complete_io_ret; 2775 } 2776 2777 if (!bio->zone_report_buf->nr_zones) { 2778 ret = -EINVAL; 2779 goto out_complete_io_ret; 2780 } 2781 2782 for (i = 0; i < bio->zone_report_buf->nr_zones && bio->handled_zones < zones_to_copy; i++) { 2783 ret = fill_zone_from_report(&info[bio->handled_zones], 2784 &bio->zone_report_buf->descs[i]); 2785 if (ret) { 2786 goto out_complete_io_ret; 2787 } 2788 bio->handled_zones++; 2789 } 2790 2791 if (bio->handled_zones < zones_to_copy) { 2792 uint64_t zone_size_lba = spdk_nvme_zns_ns_get_zone_size_sectors(ns); 2793 uint64_t slba = zone_id + (zone_size_lba * bio->handled_zones); 2794 2795 memset(bio->zone_report_buf, 0, zone_report_bufsize); 2796 ret = spdk_nvme_zns_report_zones(ns, qpair, 2797 bio->zone_report_buf, zone_report_bufsize, 2798 slba, SPDK_NVME_ZRA_LIST_ALL, true, 2799 bdev_nvme_get_zone_info_done, bio); 2800 if (!ret) { 2801 return; 2802 } else { 2803 goto out_complete_io_ret; 2804 } 2805 } 2806 2807 out_complete_io_nvme_cpl: 2808 free(bio->zone_report_buf); 2809 bio->zone_report_buf = NULL; 2810 bdev_nvme_io_complete_nvme_status(bio, cpl); 2811 return; 2812 2813 out_complete_io_ret: 2814 free(bio->zone_report_buf); 2815 bio->zone_report_buf = NULL; 2816 bdev_nvme_io_complete(bio, ret); 2817 } 2818 2819 static void 2820 bdev_nvme_zone_management_done(void *ref, const struct spdk_nvme_cpl *cpl) 2821 { 2822 struct nvme_bdev_io *bio = ref; 2823 2824 bdev_nvme_io_complete_nvme_status(bio, cpl); 2825 } 2826 2827 static void 2828 bdev_nvme_admin_passthru_completion(void *ctx) 2829 { 2830 struct nvme_bdev_io *bio = ctx; 2831 2832 bdev_nvme_io_complete_nvme_status(bio, &bio->cpl); 2833 } 2834 2835 static void 2836 bdev_nvme_abort_completion(void *ctx) 2837 { 2838 struct nvme_bdev_io *bio = ctx; 2839 struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio); 2840 2841 if (spdk_nvme_cpl_is_abort_success(&bio->cpl)) { 2842 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS); 2843 } else { 2844 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 2845 } 2846 } 2847 2848 static void 2849 bdev_nvme_abort_done(void *ref, const struct spdk_nvme_cpl *cpl) 2850 { 2851 struct nvme_bdev_io *bio = ref; 2852 2853 bio->cpl = *cpl; 2854 spdk_thread_send_msg(bio->orig_thread, bdev_nvme_abort_completion, bio); 2855 } 2856 2857 static void 2858 bdev_nvme_admin_passthru_done(void *ref, const struct spdk_nvme_cpl *cpl) 2859 { 2860 struct nvme_bdev_io *bio = ref; 2861 2862 bio->cpl = *cpl; 2863 spdk_thread_send_msg(bio->orig_thread, bdev_nvme_admin_passthru_completion, bio); 2864 } 2865 2866 static void 2867 bdev_nvme_queued_reset_sgl(void *ref, uint32_t sgl_offset) 2868 { 2869 struct nvme_bdev_io *bio = ref; 2870 struct iovec *iov; 2871 2872 bio->iov_offset = sgl_offset; 2873 for (bio->iovpos = 0; bio->iovpos < bio->iovcnt; bio->iovpos++) { 2874 iov = &bio->iovs[bio->iovpos]; 2875 if (bio->iov_offset < iov->iov_len) { 2876 break; 2877 } 2878 2879 bio->iov_offset -= iov->iov_len; 2880 } 2881 } 2882 2883 static int 2884 bdev_nvme_queued_next_sge(void *ref, void **address, uint32_t *length) 2885 { 2886 struct nvme_bdev_io *bio = ref; 2887 struct iovec *iov; 2888 2889 assert(bio->iovpos < bio->iovcnt); 2890 2891 iov = &bio->iovs[bio->iovpos]; 2892 2893 *address = iov->iov_base; 2894 *length = iov->iov_len; 2895 2896 if (bio->iov_offset) { 2897 assert(bio->iov_offset <= iov->iov_len); 2898 *address += bio->iov_offset; 2899 *length -= bio->iov_offset; 2900 } 2901 2902 bio->iov_offset += *length; 2903 if (bio->iov_offset == iov->iov_len) { 2904 bio->iovpos++; 2905 bio->iov_offset = 0; 2906 } 2907 2908 return 0; 2909 } 2910 2911 static void 2912 bdev_nvme_queued_reset_fused_sgl(void *ref, uint32_t sgl_offset) 2913 { 2914 struct nvme_bdev_io *bio = ref; 2915 struct iovec *iov; 2916 2917 bio->fused_iov_offset = sgl_offset; 2918 for (bio->fused_iovpos = 0; bio->fused_iovpos < bio->fused_iovcnt; bio->fused_iovpos++) { 2919 iov = &bio->fused_iovs[bio->fused_iovpos]; 2920 if (bio->fused_iov_offset < iov->iov_len) { 2921 break; 2922 } 2923 2924 bio->fused_iov_offset -= iov->iov_len; 2925 } 2926 } 2927 2928 static int 2929 bdev_nvme_queued_next_fused_sge(void *ref, void **address, uint32_t *length) 2930 { 2931 struct nvme_bdev_io *bio = ref; 2932 struct iovec *iov; 2933 2934 assert(bio->fused_iovpos < bio->fused_iovcnt); 2935 2936 iov = &bio->fused_iovs[bio->fused_iovpos]; 2937 2938 *address = iov->iov_base; 2939 *length = iov->iov_len; 2940 2941 if (bio->fused_iov_offset) { 2942 assert(bio->fused_iov_offset <= iov->iov_len); 2943 *address += bio->fused_iov_offset; 2944 *length -= bio->fused_iov_offset; 2945 } 2946 2947 bio->fused_iov_offset += *length; 2948 if (bio->fused_iov_offset == iov->iov_len) { 2949 bio->fused_iovpos++; 2950 bio->fused_iov_offset = 0; 2951 } 2952 2953 return 0; 2954 } 2955 2956 static int 2957 bdev_nvme_no_pi_readv(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 2958 struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt, 2959 void *md, uint64_t lba_count, uint64_t lba) 2960 { 2961 int rc; 2962 2963 SPDK_DEBUGLOG(bdev_nvme, "read %" PRIu64 " blocks with offset %#" PRIx64 " without PI check\n", 2964 lba_count, lba); 2965 2966 bio->iovs = iov; 2967 bio->iovcnt = iovcnt; 2968 bio->iovpos = 0; 2969 bio->iov_offset = 0; 2970 2971 rc = spdk_nvme_ns_cmd_readv_with_md(ns, qpair, lba, lba_count, 2972 bdev_nvme_no_pi_readv_done, bio, 0, 2973 bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge, 2974 md, 0, 0); 2975 2976 if (rc != 0 && rc != -ENOMEM) { 2977 SPDK_ERRLOG("no_pi_readv failed: rc = %d\n", rc); 2978 } 2979 return rc; 2980 } 2981 2982 static int 2983 bdev_nvme_readv(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 2984 struct nvme_bdev_io *bio, struct iovec *iov, int iovcnt, 2985 void *md, uint64_t lba_count, uint64_t lba, uint32_t flags) 2986 { 2987 int rc; 2988 2989 SPDK_DEBUGLOG(bdev_nvme, "read %" PRIu64 " blocks with offset %#" PRIx64 "\n", 2990 lba_count, lba); 2991 2992 bio->iovs = iov; 2993 bio->iovcnt = iovcnt; 2994 bio->iovpos = 0; 2995 bio->iov_offset = 0; 2996 2997 if (iovcnt == 1) { 2998 rc = spdk_nvme_ns_cmd_read_with_md(ns, qpair, iov[0].iov_base, md, lba, 2999 lba_count, 3000 bdev_nvme_readv_done, bio, 3001 flags, 3002 0, 0); 3003 } else { 3004 rc = spdk_nvme_ns_cmd_readv_with_md(ns, qpair, lba, lba_count, 3005 bdev_nvme_readv_done, bio, flags, 3006 bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge, 3007 md, 0, 0); 3008 } 3009 3010 if (rc != 0 && rc != -ENOMEM) { 3011 SPDK_ERRLOG("readv failed: rc = %d\n", rc); 3012 } 3013 return rc; 3014 } 3015 3016 static int 3017 bdev_nvme_writev(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 3018 struct nvme_bdev_io *bio, 3019 struct iovec *iov, int iovcnt, void *md, uint64_t lba_count, uint64_t lba, 3020 uint32_t flags) 3021 { 3022 int rc; 3023 3024 SPDK_DEBUGLOG(bdev_nvme, "write %" PRIu64 " blocks with offset %#" PRIx64 "\n", 3025 lba_count, lba); 3026 3027 bio->iovs = iov; 3028 bio->iovcnt = iovcnt; 3029 bio->iovpos = 0; 3030 bio->iov_offset = 0; 3031 3032 if (iovcnt == 1) { 3033 rc = spdk_nvme_ns_cmd_write_with_md(ns, qpair, iov[0].iov_base, md, lba, 3034 lba_count, 3035 bdev_nvme_writev_done, bio, 3036 flags, 3037 0, 0); 3038 } else { 3039 rc = spdk_nvme_ns_cmd_writev_with_md(ns, qpair, lba, lba_count, 3040 bdev_nvme_writev_done, bio, flags, 3041 bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge, 3042 md, 0, 0); 3043 } 3044 3045 if (rc != 0 && rc != -ENOMEM) { 3046 SPDK_ERRLOG("writev failed: rc = %d\n", rc); 3047 } 3048 return rc; 3049 } 3050 3051 static int 3052 bdev_nvme_zone_appendv(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 3053 struct nvme_bdev_io *bio, 3054 struct iovec *iov, int iovcnt, void *md, uint64_t lba_count, uint64_t zslba, 3055 uint32_t flags) 3056 { 3057 int rc; 3058 3059 SPDK_DEBUGLOG(bdev_nvme, "zone append %" PRIu64 " blocks to zone start lba %#" PRIx64 "\n", 3060 lba_count, zslba); 3061 3062 bio->iovs = iov; 3063 bio->iovcnt = iovcnt; 3064 bio->iovpos = 0; 3065 bio->iov_offset = 0; 3066 3067 if (iovcnt == 1) { 3068 rc = spdk_nvme_zns_zone_append_with_md(ns, qpair, iov[0].iov_base, md, zslba, 3069 lba_count, 3070 bdev_nvme_zone_appendv_done, bio, 3071 flags, 3072 0, 0); 3073 } else { 3074 rc = spdk_nvme_zns_zone_appendv_with_md(ns, qpair, zslba, lba_count, 3075 bdev_nvme_zone_appendv_done, bio, flags, 3076 bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge, 3077 md, 0, 0); 3078 } 3079 3080 if (rc != 0 && rc != -ENOMEM) { 3081 SPDK_ERRLOG("zone append failed: rc = %d\n", rc); 3082 } 3083 return rc; 3084 } 3085 3086 static int 3087 bdev_nvme_comparev(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 3088 struct nvme_bdev_io *bio, 3089 struct iovec *iov, int iovcnt, void *md, uint64_t lba_count, uint64_t lba, 3090 uint32_t flags) 3091 { 3092 int rc; 3093 3094 SPDK_DEBUGLOG(bdev_nvme, "compare %" PRIu64 " blocks with offset %#" PRIx64 "\n", 3095 lba_count, lba); 3096 3097 bio->iovs = iov; 3098 bio->iovcnt = iovcnt; 3099 bio->iovpos = 0; 3100 bio->iov_offset = 0; 3101 3102 rc = spdk_nvme_ns_cmd_comparev_with_md(ns, qpair, lba, lba_count, 3103 bdev_nvme_comparev_done, bio, flags, 3104 bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge, 3105 md, 0, 0); 3106 3107 if (rc != 0 && rc != -ENOMEM) { 3108 SPDK_ERRLOG("comparev failed: rc = %d\n", rc); 3109 } 3110 return rc; 3111 } 3112 3113 static int 3114 bdev_nvme_comparev_and_writev(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 3115 struct nvme_bdev_io *bio, struct iovec *cmp_iov, int cmp_iovcnt, 3116 struct iovec *write_iov, int write_iovcnt, 3117 void *md, uint64_t lba_count, uint64_t lba, uint32_t flags) 3118 { 3119 struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(bio); 3120 int rc; 3121 3122 SPDK_DEBUGLOG(bdev_nvme, "compare and write %" PRIu64 " blocks with offset %#" PRIx64 "\n", 3123 lba_count, lba); 3124 3125 bio->iovs = cmp_iov; 3126 bio->iovcnt = cmp_iovcnt; 3127 bio->iovpos = 0; 3128 bio->iov_offset = 0; 3129 bio->fused_iovs = write_iov; 3130 bio->fused_iovcnt = write_iovcnt; 3131 bio->fused_iovpos = 0; 3132 bio->fused_iov_offset = 0; 3133 3134 if (bdev_io->num_retries == 0) { 3135 bio->first_fused_submitted = false; 3136 } 3137 3138 if (!bio->first_fused_submitted) { 3139 flags |= SPDK_NVME_IO_FLAGS_FUSE_FIRST; 3140 memset(&bio->cpl, 0, sizeof(bio->cpl)); 3141 3142 rc = spdk_nvme_ns_cmd_comparev_with_md(ns, qpair, lba, lba_count, 3143 bdev_nvme_comparev_and_writev_done, bio, flags, 3144 bdev_nvme_queued_reset_sgl, bdev_nvme_queued_next_sge, md, 0, 0); 3145 if (rc == 0) { 3146 bio->first_fused_submitted = true; 3147 flags &= ~SPDK_NVME_IO_FLAGS_FUSE_FIRST; 3148 } else { 3149 if (rc != -ENOMEM) { 3150 SPDK_ERRLOG("compare failed: rc = %d\n", rc); 3151 } 3152 return rc; 3153 } 3154 } 3155 3156 flags |= SPDK_NVME_IO_FLAGS_FUSE_SECOND; 3157 3158 rc = spdk_nvme_ns_cmd_writev_with_md(ns, qpair, lba, lba_count, 3159 bdev_nvme_comparev_and_writev_done, bio, flags, 3160 bdev_nvme_queued_reset_fused_sgl, bdev_nvme_queued_next_fused_sge, md, 0, 0); 3161 if (rc != 0 && rc != -ENOMEM) { 3162 SPDK_ERRLOG("write failed: rc = %d\n", rc); 3163 rc = 0; 3164 } 3165 3166 return rc; 3167 } 3168 3169 static int 3170 bdev_nvme_unmap(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 3171 struct nvme_bdev_io *bio, 3172 uint64_t offset_blocks, 3173 uint64_t num_blocks) 3174 { 3175 struct spdk_nvme_dsm_range dsm_ranges[SPDK_NVME_DATASET_MANAGEMENT_MAX_RANGES]; 3176 struct spdk_nvme_dsm_range *range; 3177 uint64_t offset, remaining; 3178 uint64_t num_ranges_u64; 3179 uint16_t num_ranges; 3180 int rc; 3181 3182 num_ranges_u64 = (num_blocks + SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS - 1) / 3183 SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS; 3184 if (num_ranges_u64 > SPDK_COUNTOF(dsm_ranges)) { 3185 SPDK_ERRLOG("Unmap request for %" PRIu64 " blocks is too large\n", num_blocks); 3186 return -EINVAL; 3187 } 3188 num_ranges = (uint16_t)num_ranges_u64; 3189 3190 offset = offset_blocks; 3191 remaining = num_blocks; 3192 range = &dsm_ranges[0]; 3193 3194 /* Fill max-size ranges until the remaining blocks fit into one range */ 3195 while (remaining > SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS) { 3196 range->attributes.raw = 0; 3197 range->length = SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS; 3198 range->starting_lba = offset; 3199 3200 offset += SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS; 3201 remaining -= SPDK_NVME_DATASET_MANAGEMENT_RANGE_MAX_BLOCKS; 3202 range++; 3203 } 3204 3205 /* Final range describes the remaining blocks */ 3206 range->attributes.raw = 0; 3207 range->length = remaining; 3208 range->starting_lba = offset; 3209 3210 rc = spdk_nvme_ns_cmd_dataset_management(ns, qpair, 3211 SPDK_NVME_DSM_ATTR_DEALLOCATE, 3212 dsm_ranges, num_ranges, 3213 bdev_nvme_queued_done, bio); 3214 3215 return rc; 3216 } 3217 3218 static int 3219 bdev_nvme_get_zone_info(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 3220 struct nvme_bdev_io *bio, uint64_t zone_id, uint32_t num_zones, 3221 struct spdk_bdev_zone_info *info) 3222 { 3223 uint32_t zone_report_bufsize = spdk_nvme_ns_get_max_io_xfer_size(ns); 3224 uint64_t zone_size = spdk_nvme_zns_ns_get_zone_size_sectors(ns); 3225 uint64_t total_zones = spdk_nvme_zns_ns_get_num_zones(ns); 3226 3227 if (zone_id % zone_size != 0) { 3228 return -EINVAL; 3229 } 3230 3231 if (num_zones > total_zones || !num_zones) { 3232 return -EINVAL; 3233 } 3234 3235 assert(!bio->zone_report_buf); 3236 bio->zone_report_buf = calloc(1, zone_report_bufsize); 3237 if (!bio->zone_report_buf) { 3238 return -ENOMEM; 3239 } 3240 3241 bio->handled_zones = 0; 3242 3243 return spdk_nvme_zns_report_zones(ns, qpair, bio->zone_report_buf, zone_report_bufsize, 3244 zone_id, SPDK_NVME_ZRA_LIST_ALL, true, 3245 bdev_nvme_get_zone_info_done, bio); 3246 } 3247 3248 static int 3249 bdev_nvme_zone_management(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 3250 struct nvme_bdev_io *bio, uint64_t zone_id, 3251 enum spdk_bdev_zone_action action) 3252 { 3253 switch (action) { 3254 case SPDK_BDEV_ZONE_CLOSE: 3255 return spdk_nvme_zns_close_zone(ns, qpair, zone_id, false, 3256 bdev_nvme_zone_management_done, bio); 3257 case SPDK_BDEV_ZONE_FINISH: 3258 return spdk_nvme_zns_finish_zone(ns, qpair, zone_id, false, 3259 bdev_nvme_zone_management_done, bio); 3260 case SPDK_BDEV_ZONE_OPEN: 3261 return spdk_nvme_zns_open_zone(ns, qpair, zone_id, false, 3262 bdev_nvme_zone_management_done, bio); 3263 case SPDK_BDEV_ZONE_RESET: 3264 return spdk_nvme_zns_reset_zone(ns, qpair, zone_id, false, 3265 bdev_nvme_zone_management_done, bio); 3266 case SPDK_BDEV_ZONE_OFFLINE: 3267 return spdk_nvme_zns_offline_zone(ns, qpair, zone_id, false, 3268 bdev_nvme_zone_management_done, bio); 3269 default: 3270 return -EINVAL; 3271 } 3272 } 3273 3274 static int 3275 bdev_nvme_admin_passthru(struct nvme_io_path *io_path, struct nvme_bdev_io *bio, 3276 struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes) 3277 { 3278 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr; 3279 uint32_t max_xfer_size; 3280 3281 if (!bdev_nvme_find_admin_path(io_path, &nvme_bdev_ctrlr)) { 3282 return -EINVAL; 3283 } 3284 3285 max_xfer_size = spdk_nvme_ctrlr_get_max_xfer_size(nvme_bdev_ctrlr->ctrlr); 3286 3287 if (nbytes > max_xfer_size) { 3288 SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size); 3289 return -EINVAL; 3290 } 3291 3292 bio->orig_thread = spdk_get_thread(); 3293 3294 return spdk_nvme_ctrlr_cmd_admin_raw(nvme_bdev_ctrlr->ctrlr, cmd, buf, 3295 (uint32_t)nbytes, bdev_nvme_admin_passthru_done, bio); 3296 } 3297 3298 static int 3299 bdev_nvme_io_passthru(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 3300 struct nvme_bdev_io *bio, 3301 struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes) 3302 { 3303 uint32_t max_xfer_size = spdk_nvme_ns_get_max_io_xfer_size(ns); 3304 struct spdk_nvme_ctrlr *ctrlr = spdk_nvme_ns_get_ctrlr(ns); 3305 3306 if (nbytes > max_xfer_size) { 3307 SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size); 3308 return -EINVAL; 3309 } 3310 3311 /* 3312 * Each NVMe bdev is a specific namespace, and all NVMe I/O commands require a nsid, 3313 * so fill it out automatically. 3314 */ 3315 cmd->nsid = spdk_nvme_ns_get_id(ns); 3316 3317 return spdk_nvme_ctrlr_cmd_io_raw(ctrlr, qpair, cmd, buf, 3318 (uint32_t)nbytes, bdev_nvme_queued_done, bio); 3319 } 3320 3321 static int 3322 bdev_nvme_io_passthru_md(struct spdk_nvme_ns *ns, struct spdk_nvme_qpair *qpair, 3323 struct nvme_bdev_io *bio, 3324 struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes, void *md_buf, size_t md_len) 3325 { 3326 size_t nr_sectors = nbytes / spdk_nvme_ns_get_extended_sector_size(ns); 3327 uint32_t max_xfer_size = spdk_nvme_ns_get_max_io_xfer_size(ns); 3328 struct spdk_nvme_ctrlr *ctrlr = spdk_nvme_ns_get_ctrlr(ns); 3329 3330 if (nbytes > max_xfer_size) { 3331 SPDK_ERRLOG("nbytes is greater than MDTS %" PRIu32 ".\n", max_xfer_size); 3332 return -EINVAL; 3333 } 3334 3335 if (md_len != nr_sectors * spdk_nvme_ns_get_md_size(ns)) { 3336 SPDK_ERRLOG("invalid meta data buffer size\n"); 3337 return -EINVAL; 3338 } 3339 3340 /* 3341 * Each NVMe bdev is a specific namespace, and all NVMe I/O commands require a nsid, 3342 * so fill it out automatically. 3343 */ 3344 cmd->nsid = spdk_nvme_ns_get_id(ns); 3345 3346 return spdk_nvme_ctrlr_cmd_io_raw_with_md(ctrlr, qpair, cmd, buf, 3347 (uint32_t)nbytes, md_buf, bdev_nvme_queued_done, bio); 3348 } 3349 3350 static int 3351 bdev_nvme_abort(struct nvme_io_path *io_path, struct nvme_bdev_io *bio, 3352 struct nvme_bdev_io *bio_to_abort) 3353 { 3354 int rc; 3355 3356 bio->orig_thread = spdk_get_thread(); 3357 3358 rc = spdk_nvme_ctrlr_cmd_abort_ext(io_path->ctrlr->ctrlr, 3359 io_path->qpair, 3360 bio_to_abort, 3361 bdev_nvme_abort_done, bio); 3362 if (rc == -ENOENT) { 3363 /* If no command was found in I/O qpair, the target command may be 3364 * admin command. 3365 */ 3366 rc = spdk_nvme_ctrlr_cmd_abort_ext(io_path->ctrlr->ctrlr, 3367 NULL, 3368 bio_to_abort, 3369 bdev_nvme_abort_done, bio); 3370 } 3371 3372 if (rc == -ENOENT) { 3373 /* If no command was found, complete the abort request with failure. */ 3374 bio->cpl.cdw0 |= 1U; 3375 bio->cpl.status.sc = SPDK_NVME_SC_SUCCESS; 3376 bio->cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3377 3378 bdev_nvme_abort_completion(bio); 3379 3380 rc = 0; 3381 } 3382 3383 return rc; 3384 } 3385 3386 static void 3387 nvme_ctrlr_config_json_standard_namespace(struct spdk_json_write_ctx *w, 3388 struct nvme_bdev_ns *nvme_ns) 3389 { 3390 /* nop */ 3391 } 3392 3393 static void 3394 nvme_namespace_config_json(struct spdk_json_write_ctx *w, struct nvme_bdev_ns *nvme_ns) 3395 { 3396 g_config_json_namespace_fn[nvme_ns->type](w, nvme_ns); 3397 } 3398 3399 static void 3400 bdev_nvme_opts_config_json(struct spdk_json_write_ctx *w) 3401 { 3402 const char *action; 3403 3404 if (g_opts.action_on_timeout == SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET) { 3405 action = "reset"; 3406 } else if (g_opts.action_on_timeout == SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT) { 3407 action = "abort"; 3408 } else { 3409 action = "none"; 3410 } 3411 3412 spdk_json_write_object_begin(w); 3413 3414 spdk_json_write_named_string(w, "method", "bdev_nvme_set_options"); 3415 3416 spdk_json_write_named_object_begin(w, "params"); 3417 spdk_json_write_named_string(w, "action_on_timeout", action); 3418 spdk_json_write_named_uint64(w, "timeout_us", g_opts.timeout_us); 3419 spdk_json_write_named_uint32(w, "keep_alive_timeout_ms", g_opts.keep_alive_timeout_ms); 3420 spdk_json_write_named_uint32(w, "retry_count", g_opts.retry_count); 3421 spdk_json_write_named_uint32(w, "arbitration_burst", g_opts.arbitration_burst); 3422 spdk_json_write_named_uint32(w, "low_priority_weight", g_opts.low_priority_weight); 3423 spdk_json_write_named_uint32(w, "medium_priority_weight", g_opts.medium_priority_weight); 3424 spdk_json_write_named_uint32(w, "high_priority_weight", g_opts.high_priority_weight); 3425 spdk_json_write_named_uint64(w, "nvme_adminq_poll_period_us", g_opts.nvme_adminq_poll_period_us); 3426 spdk_json_write_named_uint64(w, "nvme_ioq_poll_period_us", g_opts.nvme_ioq_poll_period_us); 3427 spdk_json_write_named_uint32(w, "io_queue_requests", g_opts.io_queue_requests); 3428 spdk_json_write_named_bool(w, "delay_cmd_submit", g_opts.delay_cmd_submit); 3429 spdk_json_write_object_end(w); 3430 3431 spdk_json_write_object_end(w); 3432 } 3433 3434 static void 3435 nvme_bdev_ctrlr_config_json(struct spdk_json_write_ctx *w, 3436 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr) 3437 { 3438 struct spdk_nvme_transport_id *trid; 3439 3440 trid = nvme_bdev_ctrlr->connected_trid; 3441 3442 spdk_json_write_object_begin(w); 3443 3444 spdk_json_write_named_string(w, "method", "bdev_nvme_attach_controller"); 3445 3446 spdk_json_write_named_object_begin(w, "params"); 3447 spdk_json_write_named_string(w, "name", nvme_bdev_ctrlr->name); 3448 nvme_bdev_dump_trid_json(trid, w); 3449 spdk_json_write_named_bool(w, "prchk_reftag", 3450 (nvme_bdev_ctrlr->prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_REFTAG) != 0); 3451 spdk_json_write_named_bool(w, "prchk_guard", 3452 (nvme_bdev_ctrlr->prchk_flags & SPDK_NVME_IO_FLAGS_PRCHK_GUARD) != 0); 3453 3454 spdk_json_write_object_end(w); 3455 3456 spdk_json_write_object_end(w); 3457 } 3458 3459 static void 3460 bdev_nvme_hotplug_config_json(struct spdk_json_write_ctx *w) 3461 { 3462 spdk_json_write_object_begin(w); 3463 spdk_json_write_named_string(w, "method", "bdev_nvme_set_hotplug"); 3464 3465 spdk_json_write_named_object_begin(w, "params"); 3466 spdk_json_write_named_uint64(w, "period_us", g_nvme_hotplug_poll_period_us); 3467 spdk_json_write_named_bool(w, "enable", g_nvme_hotplug_enabled); 3468 spdk_json_write_object_end(w); 3469 3470 spdk_json_write_object_end(w); 3471 } 3472 3473 static int 3474 bdev_nvme_config_json(struct spdk_json_write_ctx *w) 3475 { 3476 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr; 3477 uint32_t nsid; 3478 3479 bdev_nvme_opts_config_json(w); 3480 3481 pthread_mutex_lock(&g_bdev_nvme_mutex); 3482 3483 TAILQ_FOREACH(nvme_bdev_ctrlr, &g_nvme_bdev_ctrlrs, tailq) { 3484 nvme_bdev_ctrlr_config_json(w, nvme_bdev_ctrlr); 3485 3486 for (nsid = 0; nsid < nvme_bdev_ctrlr->num_ns; ++nsid) { 3487 if (!nvme_bdev_ctrlr->namespaces[nsid]->populated) { 3488 continue; 3489 } 3490 3491 nvme_namespace_config_json(w, nvme_bdev_ctrlr->namespaces[nsid]); 3492 } 3493 } 3494 3495 /* Dump as last parameter to give all NVMe bdevs chance to be constructed 3496 * before enabling hotplug poller. 3497 */ 3498 bdev_nvme_hotplug_config_json(w); 3499 3500 pthread_mutex_unlock(&g_bdev_nvme_mutex); 3501 return 0; 3502 } 3503 3504 struct spdk_nvme_ctrlr * 3505 bdev_nvme_get_ctrlr(struct spdk_bdev *bdev) 3506 { 3507 if (!bdev || bdev->module != &nvme_if) { 3508 return NULL; 3509 } 3510 3511 return SPDK_CONTAINEROF(bdev, struct nvme_bdev, disk)->nvme_ns->ctrlr->ctrlr; 3512 } 3513 3514 SPDK_LOG_REGISTER_COMPONENT(bdev_nvme) 3515