1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. All rights reserved. 5 * Copyright (c) 2018-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 "spdk/bdev.h" 37 #include "spdk/bit_array.h" 38 #include "spdk/conf.h" 39 #include "spdk/thread.h" 40 #include "spdk/nvmf.h" 41 #include "spdk/trace.h" 42 #include "spdk/endian.h" 43 #include "spdk/string.h" 44 45 #include "spdk_internal/log.h" 46 47 #include "nvmf_internal.h" 48 #include "transport.h" 49 50 SPDK_LOG_REGISTER_COMPONENT("nvmf", SPDK_LOG_NVMF) 51 52 #define SPDK_NVMF_DEFAULT_MAX_SUBSYSTEMS 1024 53 54 static TAILQ_HEAD(, spdk_nvmf_tgt) g_nvmf_tgts = TAILQ_HEAD_INITIALIZER(g_nvmf_tgts); 55 56 typedef void (*nvmf_qpair_disconnect_cpl)(void *ctx, int status); 57 static void spdk_nvmf_tgt_destroy_poll_group(void *io_device, void *ctx_buf); 58 59 /* supplied to a single call to nvmf_qpair_disconnect */ 60 struct nvmf_qpair_disconnect_ctx { 61 struct spdk_nvmf_qpair *qpair; 62 struct spdk_nvmf_ctrlr *ctrlr; 63 nvmf_qpair_disconnect_cb cb_fn; 64 struct spdk_thread *thread; 65 void *ctx; 66 uint16_t qid; 67 }; 68 69 /* 70 * There are several times when we need to iterate through the list of all qpairs and selectively delete them. 71 * In order to do this sequentially without overlap, we must provide a context to recover the next qpair from 72 * to enable calling nvmf_qpair_disconnect on the next desired qpair. 73 */ 74 struct nvmf_qpair_disconnect_many_ctx { 75 struct spdk_nvmf_subsystem *subsystem; 76 struct spdk_nvmf_poll_group *group; 77 spdk_nvmf_poll_group_mod_done cpl_fn; 78 void *cpl_ctx; 79 }; 80 81 static void 82 spdk_nvmf_qpair_set_state(struct spdk_nvmf_qpair *qpair, 83 enum spdk_nvmf_qpair_state state) 84 { 85 assert(qpair != NULL); 86 assert(qpair->group->thread == spdk_get_thread()); 87 88 qpair->state = state; 89 } 90 91 static int 92 spdk_nvmf_poll_group_poll(void *ctx) 93 { 94 struct spdk_nvmf_poll_group *group = ctx; 95 int rc; 96 int count = 0; 97 struct spdk_nvmf_transport_poll_group *tgroup; 98 99 TAILQ_FOREACH(tgroup, &group->tgroups, link) { 100 rc = spdk_nvmf_transport_poll_group_poll(tgroup); 101 if (rc < 0) { 102 return -1; 103 } 104 count += rc; 105 } 106 107 return count; 108 } 109 110 static int 111 spdk_nvmf_tgt_create_poll_group(void *io_device, void *ctx_buf) 112 { 113 struct spdk_nvmf_tgt *tgt = io_device; 114 struct spdk_nvmf_poll_group *group = ctx_buf; 115 struct spdk_nvmf_transport *transport; 116 uint32_t sid; 117 118 TAILQ_INIT(&group->tgroups); 119 TAILQ_INIT(&group->qpairs); 120 121 TAILQ_FOREACH(transport, &tgt->transports, link) { 122 spdk_nvmf_poll_group_add_transport(group, transport); 123 } 124 125 group->num_sgroups = tgt->max_subsystems; 126 group->sgroups = calloc(tgt->max_subsystems, sizeof(struct spdk_nvmf_subsystem_poll_group)); 127 if (!group->sgroups) { 128 return -ENOMEM; 129 } 130 131 for (sid = 0; sid < tgt->max_subsystems; sid++) { 132 struct spdk_nvmf_subsystem *subsystem; 133 134 subsystem = tgt->subsystems[sid]; 135 if (!subsystem) { 136 continue; 137 } 138 139 if (spdk_nvmf_poll_group_add_subsystem(group, subsystem, NULL, NULL) != 0) { 140 spdk_nvmf_tgt_destroy_poll_group(io_device, ctx_buf); 141 return -1; 142 } 143 } 144 145 group->poller = spdk_poller_register(spdk_nvmf_poll_group_poll, group, 0); 146 group->thread = spdk_get_thread(); 147 148 return 0; 149 } 150 151 static void 152 spdk_nvmf_tgt_destroy_poll_group(void *io_device, void *ctx_buf) 153 { 154 struct spdk_nvmf_poll_group *group = ctx_buf; 155 struct spdk_nvmf_transport_poll_group *tgroup, *tmp; 156 struct spdk_nvmf_subsystem_poll_group *sgroup; 157 uint32_t sid, nsid; 158 159 TAILQ_FOREACH_SAFE(tgroup, &group->tgroups, link, tmp) { 160 TAILQ_REMOVE(&group->tgroups, tgroup, link); 161 spdk_nvmf_transport_poll_group_destroy(tgroup); 162 } 163 164 for (sid = 0; sid < group->num_sgroups; sid++) { 165 sgroup = &group->sgroups[sid]; 166 167 for (nsid = 0; nsid < sgroup->num_ns; nsid++) { 168 if (sgroup->ns_info[nsid].channel) { 169 spdk_put_io_channel(sgroup->ns_info[nsid].channel); 170 sgroup->ns_info[nsid].channel = NULL; 171 } 172 } 173 174 free(sgroup->ns_info); 175 } 176 177 free(group->sgroups); 178 } 179 180 static void 181 _nvmf_tgt_disconnect_next_qpair(void *ctx) 182 { 183 struct spdk_nvmf_qpair *qpair; 184 struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx; 185 struct spdk_nvmf_poll_group *group = qpair_ctx->group; 186 struct spdk_io_channel *ch; 187 int rc = 0; 188 189 qpair = TAILQ_FIRST(&group->qpairs); 190 191 if (qpair) { 192 rc = spdk_nvmf_qpair_disconnect(qpair, _nvmf_tgt_disconnect_next_qpair, ctx); 193 } 194 195 if (!qpair || rc != 0) { 196 /* When the refcount from the channels reaches 0, spdk_nvmf_tgt_destroy_poll_group will be called. */ 197 ch = spdk_io_channel_from_ctx(group); 198 spdk_put_io_channel(ch); 199 free(qpair_ctx); 200 } 201 } 202 203 static void 204 spdk_nvmf_tgt_destroy_poll_group_qpairs(struct spdk_nvmf_poll_group *group) 205 { 206 struct nvmf_qpair_disconnect_many_ctx *ctx; 207 208 ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_many_ctx)); 209 210 if (!ctx) { 211 SPDK_ERRLOG("Failed to allocate memory for destroy poll group ctx\n"); 212 return; 213 } 214 215 spdk_poller_unregister(&group->poller); 216 217 ctx->group = group; 218 _nvmf_tgt_disconnect_next_qpair(ctx); 219 } 220 221 struct spdk_nvmf_tgt * 222 spdk_nvmf_tgt_create(struct spdk_nvmf_target_opts *opts) 223 { 224 struct spdk_nvmf_tgt *tgt, *tmp_tgt; 225 226 if (strnlen(opts->name, NVMF_TGT_NAME_MAX_LENGTH) == NVMF_TGT_NAME_MAX_LENGTH) { 227 SPDK_ERRLOG("Provided target name exceeds the max length of %u.\n", NVMF_TGT_NAME_MAX_LENGTH); 228 return NULL; 229 } 230 231 TAILQ_FOREACH(tmp_tgt, &g_nvmf_tgts, link) { 232 if (!strncmp(opts->name, tmp_tgt->name, strlen(tmp_tgt->name))) { 233 SPDK_ERRLOG("Provided target name must be unique.\n"); 234 return NULL; 235 } 236 } 237 238 tgt = calloc(1, sizeof(*tgt)); 239 if (!tgt) { 240 return NULL; 241 } 242 243 snprintf(tgt->name, NVMF_TGT_NAME_MAX_LENGTH, "%s", opts->name); 244 245 if (!opts || !opts->max_subsystems) { 246 tgt->max_subsystems = SPDK_NVMF_DEFAULT_MAX_SUBSYSTEMS; 247 } else { 248 tgt->max_subsystems = opts->max_subsystems; 249 } 250 251 tgt->discovery_genctr = 0; 252 TAILQ_INIT(&tgt->transports); 253 254 tgt->subsystems = calloc(tgt->max_subsystems, sizeof(struct spdk_nvmf_subsystem *)); 255 if (!tgt->subsystems) { 256 free(tgt); 257 return NULL; 258 } 259 260 TAILQ_INSERT_HEAD(&g_nvmf_tgts, tgt, link); 261 262 spdk_io_device_register(tgt, 263 spdk_nvmf_tgt_create_poll_group, 264 spdk_nvmf_tgt_destroy_poll_group, 265 sizeof(struct spdk_nvmf_poll_group), 266 tgt->name); 267 268 return tgt; 269 } 270 271 static void 272 spdk_nvmf_tgt_destroy_cb(void *io_device) 273 { 274 struct spdk_nvmf_tgt *tgt = io_device; 275 struct spdk_nvmf_transport *transport, *transport_tmp; 276 spdk_nvmf_tgt_destroy_done_fn *destroy_cb_fn; 277 void *destroy_cb_arg; 278 uint32_t i; 279 280 if (tgt->subsystems) { 281 for (i = 0; i < tgt->max_subsystems; i++) { 282 if (tgt->subsystems[i]) { 283 spdk_nvmf_subsystem_destroy(tgt->subsystems[i]); 284 } 285 } 286 free(tgt->subsystems); 287 } 288 289 TAILQ_FOREACH_SAFE(transport, &tgt->transports, link, transport_tmp) { 290 TAILQ_REMOVE(&tgt->transports, transport, link); 291 spdk_nvmf_transport_destroy(transport); 292 } 293 294 destroy_cb_fn = tgt->destroy_cb_fn; 295 destroy_cb_arg = tgt->destroy_cb_arg; 296 297 free(tgt); 298 299 if (destroy_cb_fn) { 300 destroy_cb_fn(destroy_cb_arg, 0); 301 } 302 } 303 304 void 305 spdk_nvmf_tgt_destroy(struct spdk_nvmf_tgt *tgt, 306 spdk_nvmf_tgt_destroy_done_fn cb_fn, 307 void *cb_arg) 308 { 309 tgt->destroy_cb_fn = cb_fn; 310 tgt->destroy_cb_arg = cb_arg; 311 312 TAILQ_REMOVE(&g_nvmf_tgts, tgt, link); 313 314 spdk_io_device_unregister(tgt, spdk_nvmf_tgt_destroy_cb); 315 } 316 317 const char * 318 spdk_nvmf_tgt_get_name(struct spdk_nvmf_tgt *tgt) 319 { 320 return tgt->name; 321 } 322 323 struct spdk_nvmf_tgt * 324 spdk_nvmf_get_tgt(const char *name) 325 { 326 struct spdk_nvmf_tgt *tgt; 327 uint32_t num_targets = 0; 328 329 TAILQ_FOREACH(tgt, &g_nvmf_tgts, link) { 330 if (name) { 331 if (!strncmp(tgt->name, name, strlen(tgt->name))) { 332 return tgt; 333 } 334 } 335 num_targets++; 336 } 337 338 /* 339 * special case. If there is only one target and 340 * no name was specified, return the only available 341 * target. If there is more than one target, name must 342 * be specified. 343 */ 344 if (!name && num_targets == 1) { 345 return TAILQ_FIRST(&g_nvmf_tgts); 346 } 347 348 return NULL; 349 } 350 351 struct spdk_nvmf_tgt * 352 spdk_nvmf_get_first_tgt(void) 353 { 354 return TAILQ_FIRST(&g_nvmf_tgts); 355 } 356 357 struct spdk_nvmf_tgt * 358 spdk_nvmf_get_next_tgt(struct spdk_nvmf_tgt *prev) 359 { 360 return TAILQ_NEXT(prev, link); 361 } 362 363 static void 364 spdk_nvmf_write_subsystem_config_json(struct spdk_json_write_ctx *w, 365 struct spdk_nvmf_subsystem *subsystem) 366 { 367 struct spdk_nvmf_host *host; 368 struct spdk_nvmf_listener *listener; 369 const struct spdk_nvme_transport_id *trid; 370 struct spdk_nvmf_ns *ns; 371 struct spdk_nvmf_ns_opts ns_opts; 372 uint32_t max_namespaces; 373 char uuid_str[SPDK_UUID_STRING_LEN]; 374 const char *trtype; 375 const char *adrfam; 376 377 if (spdk_nvmf_subsystem_get_type(subsystem) != SPDK_NVMF_SUBTYPE_NVME) { 378 return; 379 } 380 381 /* { */ 382 spdk_json_write_object_begin(w); 383 spdk_json_write_named_string(w, "method", "nvmf_create_subsystem"); 384 385 /* "params" : { */ 386 spdk_json_write_named_object_begin(w, "params"); 387 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem)); 388 spdk_json_write_named_bool(w, "allow_any_host", spdk_nvmf_subsystem_get_allow_any_host(subsystem)); 389 spdk_json_write_named_string(w, "serial_number", spdk_nvmf_subsystem_get_sn(subsystem)); 390 spdk_json_write_named_string(w, "model_number", spdk_nvmf_subsystem_get_mn(subsystem)); 391 392 max_namespaces = spdk_nvmf_subsystem_get_max_namespaces(subsystem); 393 if (max_namespaces != 0) { 394 spdk_json_write_named_uint32(w, "max_namespaces", max_namespaces); 395 } 396 397 /* } "params" */ 398 spdk_json_write_object_end(w); 399 400 /* } */ 401 spdk_json_write_object_end(w); 402 403 for (listener = spdk_nvmf_subsystem_get_first_listener(subsystem); listener != NULL; 404 listener = spdk_nvmf_subsystem_get_next_listener(subsystem, listener)) { 405 trid = spdk_nvmf_listener_get_trid(listener); 406 407 trtype = spdk_nvme_transport_id_trtype_str(trid->trtype); 408 adrfam = spdk_nvme_transport_id_adrfam_str(trid->adrfam); 409 410 spdk_json_write_object_begin(w); 411 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_listener"); 412 413 /* "params" : { */ 414 spdk_json_write_named_object_begin(w, "params"); 415 416 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem)); 417 418 /* "listen_address" : { */ 419 spdk_json_write_named_object_begin(w, "listen_address"); 420 421 spdk_json_write_named_string(w, "trtype", trtype); 422 if (adrfam) { 423 spdk_json_write_named_string(w, "adrfam", adrfam); 424 } 425 426 spdk_json_write_named_string(w, "traddr", trid->traddr); 427 spdk_json_write_named_string(w, "trsvcid", trid->trsvcid); 428 /* } "listen_address" */ 429 spdk_json_write_object_end(w); 430 431 /* } "params" */ 432 spdk_json_write_object_end(w); 433 434 /* } */ 435 spdk_json_write_object_end(w); 436 } 437 438 for (host = spdk_nvmf_subsystem_get_first_host(subsystem); host != NULL; 439 host = spdk_nvmf_subsystem_get_next_host(subsystem, host)) { 440 441 spdk_json_write_object_begin(w); 442 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_host"); 443 444 /* "params" : { */ 445 spdk_json_write_named_object_begin(w, "params"); 446 447 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem)); 448 spdk_json_write_named_string(w, "host", spdk_nvmf_host_get_nqn(host)); 449 450 /* } "params" */ 451 spdk_json_write_object_end(w); 452 453 /* } */ 454 spdk_json_write_object_end(w); 455 } 456 457 for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL; 458 ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) { 459 spdk_nvmf_ns_get_opts(ns, &ns_opts, sizeof(ns_opts)); 460 461 spdk_json_write_object_begin(w); 462 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_ns"); 463 464 /* "params" : { */ 465 spdk_json_write_named_object_begin(w, "params"); 466 467 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem)); 468 469 /* "namespace" : { */ 470 spdk_json_write_named_object_begin(w, "namespace"); 471 472 spdk_json_write_named_uint32(w, "nsid", spdk_nvmf_ns_get_id(ns)); 473 spdk_json_write_named_string(w, "bdev_name", spdk_bdev_get_name(spdk_nvmf_ns_get_bdev(ns))); 474 475 if (!spdk_mem_all_zero(ns_opts.nguid, sizeof(ns_opts.nguid))) { 476 SPDK_STATIC_ASSERT(sizeof(ns_opts.nguid) == sizeof(uint64_t) * 2, "size mismatch"); 477 spdk_json_write_named_string_fmt(w, "nguid", "%016"PRIX64"%016"PRIX64, from_be64(&ns_opts.nguid[0]), 478 from_be64(&ns_opts.nguid[8])); 479 } 480 481 if (!spdk_mem_all_zero(ns_opts.eui64, sizeof(ns_opts.eui64))) { 482 SPDK_STATIC_ASSERT(sizeof(ns_opts.eui64) == sizeof(uint64_t), "size mismatch"); 483 spdk_json_write_named_string_fmt(w, "eui64", "%016"PRIX64, from_be64(&ns_opts.eui64)); 484 } 485 486 if (!spdk_mem_all_zero(&ns_opts.uuid, sizeof(ns_opts.uuid))) { 487 spdk_uuid_fmt_lower(uuid_str, sizeof(uuid_str), &ns_opts.uuid); 488 spdk_json_write_named_string(w, "uuid", uuid_str); 489 } 490 491 /* "namespace" */ 492 spdk_json_write_object_end(w); 493 494 /* } "params" */ 495 spdk_json_write_object_end(w); 496 497 /* } */ 498 spdk_json_write_object_end(w); 499 } 500 } 501 502 void 503 spdk_nvmf_tgt_write_config_json(struct spdk_json_write_ctx *w, struct spdk_nvmf_tgt *tgt) 504 { 505 struct spdk_nvmf_subsystem *subsystem; 506 struct spdk_nvmf_transport *transport; 507 508 spdk_json_write_object_begin(w); 509 spdk_json_write_named_string(w, "method", "nvmf_set_max_subsystems"); 510 511 spdk_json_write_named_object_begin(w, "params"); 512 spdk_json_write_named_uint32(w, "max_subsystems", tgt->max_subsystems); 513 spdk_json_write_object_end(w); 514 515 spdk_json_write_object_end(w); 516 517 /* write transports */ 518 TAILQ_FOREACH(transport, &tgt->transports, link) { 519 spdk_json_write_object_begin(w); 520 spdk_json_write_named_string(w, "method", "nvmf_create_transport"); 521 522 spdk_json_write_named_object_begin(w, "params"); 523 spdk_json_write_named_string(w, "trtype", spdk_nvme_transport_id_trtype_str(transport->ops->type)); 524 spdk_json_write_named_uint32(w, "max_queue_depth", transport->opts.max_queue_depth); 525 spdk_json_write_named_uint32(w, "max_qpairs_per_ctrlr", transport->opts.max_qpairs_per_ctrlr); 526 spdk_json_write_named_uint32(w, "in_capsule_data_size", transport->opts.in_capsule_data_size); 527 spdk_json_write_named_uint32(w, "max_io_size", transport->opts.max_io_size); 528 spdk_json_write_named_uint32(w, "io_unit_size", transport->opts.io_unit_size); 529 spdk_json_write_named_uint32(w, "max_aq_depth", transport->opts.max_aq_depth); 530 if (transport->ops->type == SPDK_NVME_TRANSPORT_RDMA) { 531 spdk_json_write_named_uint32(w, "max_srq_depth", transport->opts.max_srq_depth); 532 } 533 spdk_json_write_object_end(w); 534 535 spdk_json_write_object_end(w); 536 } 537 538 subsystem = spdk_nvmf_subsystem_get_first(tgt); 539 while (subsystem) { 540 spdk_nvmf_write_subsystem_config_json(w, subsystem); 541 subsystem = spdk_nvmf_subsystem_get_next(subsystem); 542 } 543 } 544 545 void 546 spdk_nvmf_tgt_listen(struct spdk_nvmf_tgt *tgt, 547 struct spdk_nvme_transport_id *trid, 548 spdk_nvmf_tgt_listen_done_fn cb_fn, 549 void *cb_arg) 550 { 551 struct spdk_nvmf_transport *transport; 552 const char *trtype; 553 int rc; 554 555 transport = spdk_nvmf_tgt_get_transport(tgt, trid->trtype); 556 if (!transport) { 557 trtype = spdk_nvme_transport_id_trtype_str(trid->trtype); 558 if (trtype != NULL) { 559 SPDK_ERRLOG("Unable to listen on transport %s. The transport must be created first.\n", trtype); 560 } else { 561 SPDK_ERRLOG("The specified trtype %d is unknown. Please make sure that it is properly registered.\n", 562 trid->trtype); 563 } 564 cb_fn(cb_arg, -EINVAL); 565 return; 566 } 567 568 rc = spdk_nvmf_transport_listen(transport, trid); 569 if (rc < 0) { 570 SPDK_ERRLOG("Unable to listen on address '%s'\n", trid->traddr); 571 cb_fn(cb_arg, rc); 572 return; 573 } 574 575 tgt->discovery_genctr++; 576 577 cb_fn(cb_arg, 0); 578 } 579 580 struct spdk_nvmf_tgt_add_transport_ctx { 581 struct spdk_nvmf_tgt *tgt; 582 struct spdk_nvmf_transport *transport; 583 spdk_nvmf_tgt_add_transport_done_fn cb_fn; 584 void *cb_arg; 585 }; 586 587 static void 588 _spdk_nvmf_tgt_add_transport_done(struct spdk_io_channel_iter *i, int status) 589 { 590 struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 591 592 ctx->cb_fn(ctx->cb_arg, status); 593 594 free(ctx); 595 } 596 597 static void 598 _spdk_nvmf_tgt_add_transport(struct spdk_io_channel_iter *i) 599 { 600 struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 601 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 602 struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch); 603 int rc; 604 605 rc = spdk_nvmf_poll_group_add_transport(group, ctx->transport); 606 spdk_for_each_channel_continue(i, rc); 607 } 608 609 void spdk_nvmf_tgt_add_transport(struct spdk_nvmf_tgt *tgt, 610 struct spdk_nvmf_transport *transport, 611 spdk_nvmf_tgt_add_transport_done_fn cb_fn, 612 void *cb_arg) 613 { 614 struct spdk_nvmf_tgt_add_transport_ctx *ctx; 615 616 if (spdk_nvmf_tgt_get_transport(tgt, transport->ops->type)) { 617 cb_fn(cb_arg, -EEXIST); 618 return; /* transport already created */ 619 } 620 621 transport->tgt = tgt; 622 TAILQ_INSERT_TAIL(&tgt->transports, transport, link); 623 624 ctx = calloc(1, sizeof(*ctx)); 625 if (!ctx) { 626 cb_fn(cb_arg, -ENOMEM); 627 return; 628 } 629 630 ctx->tgt = tgt; 631 ctx->transport = transport; 632 ctx->cb_fn = cb_fn; 633 ctx->cb_arg = cb_arg; 634 635 spdk_for_each_channel(tgt, 636 _spdk_nvmf_tgt_add_transport, 637 ctx, 638 _spdk_nvmf_tgt_add_transport_done); 639 } 640 641 struct spdk_nvmf_subsystem * 642 spdk_nvmf_tgt_find_subsystem(struct spdk_nvmf_tgt *tgt, const char *subnqn) 643 { 644 struct spdk_nvmf_subsystem *subsystem; 645 uint32_t sid; 646 647 if (!subnqn) { 648 return NULL; 649 } 650 651 for (sid = 0; sid < tgt->max_subsystems; sid++) { 652 subsystem = tgt->subsystems[sid]; 653 if (subsystem == NULL) { 654 continue; 655 } 656 657 if (strcmp(subnqn, subsystem->subnqn) == 0) { 658 return subsystem; 659 } 660 } 661 662 return NULL; 663 } 664 665 struct spdk_nvmf_transport * 666 spdk_nvmf_tgt_get_transport(struct spdk_nvmf_tgt *tgt, enum spdk_nvme_transport_type type) 667 { 668 struct spdk_nvmf_transport *transport; 669 670 TAILQ_FOREACH(transport, &tgt->transports, link) { 671 if (transport->ops->type == type) { 672 return transport; 673 } 674 } 675 676 return NULL; 677 } 678 679 void 680 spdk_nvmf_tgt_accept(struct spdk_nvmf_tgt *tgt, new_qpair_fn cb_fn) 681 { 682 struct spdk_nvmf_transport *transport, *tmp; 683 684 TAILQ_FOREACH_SAFE(transport, &tgt->transports, link, tmp) { 685 spdk_nvmf_transport_accept(transport, cb_fn); 686 } 687 } 688 689 struct spdk_nvmf_poll_group * 690 spdk_nvmf_poll_group_create(struct spdk_nvmf_tgt *tgt) 691 { 692 struct spdk_io_channel *ch; 693 694 ch = spdk_get_io_channel(tgt); 695 if (!ch) { 696 SPDK_ERRLOG("Unable to get I/O channel for target\n"); 697 return NULL; 698 } 699 700 return spdk_io_channel_get_ctx(ch); 701 } 702 703 void 704 spdk_nvmf_poll_group_destroy(struct spdk_nvmf_poll_group *group) 705 { 706 /* This function will put the io_channel associated with this poll group */ 707 spdk_nvmf_tgt_destroy_poll_group_qpairs(group); 708 } 709 710 int 711 spdk_nvmf_poll_group_add(struct spdk_nvmf_poll_group *group, 712 struct spdk_nvmf_qpair *qpair) 713 { 714 int rc = -1; 715 struct spdk_nvmf_transport_poll_group *tgroup; 716 717 TAILQ_INIT(&qpair->outstanding); 718 qpair->group = group; 719 720 TAILQ_FOREACH(tgroup, &group->tgroups, link) { 721 if (tgroup->transport == qpair->transport) { 722 rc = spdk_nvmf_transport_poll_group_add(tgroup, qpair); 723 break; 724 } 725 } 726 727 /* We add the qpair to the group only it is succesfully added into the tgroup */ 728 if (rc == 0) { 729 TAILQ_INSERT_TAIL(&group->qpairs, qpair, link); 730 spdk_nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_ACTIVE); 731 } 732 733 return rc; 734 } 735 736 static 737 void _nvmf_ctrlr_destruct(void *ctx) 738 { 739 struct spdk_nvmf_ctrlr *ctrlr = ctx; 740 741 spdk_nvmf_ctrlr_destruct(ctrlr); 742 } 743 744 static void 745 _spdk_nvmf_ctrlr_free_from_qpair(void *ctx) 746 { 747 struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx; 748 struct spdk_nvmf_ctrlr *ctrlr = qpair_ctx->ctrlr; 749 uint32_t count; 750 751 spdk_bit_array_clear(ctrlr->qpair_mask, qpair_ctx->qid); 752 count = spdk_bit_array_count_set(ctrlr->qpair_mask); 753 if (count == 0) { 754 spdk_bit_array_free(&ctrlr->qpair_mask); 755 756 spdk_thread_send_msg(ctrlr->subsys->thread, _nvmf_ctrlr_destruct, ctrlr); 757 } 758 759 if (qpair_ctx->cb_fn) { 760 spdk_thread_send_msg(qpair_ctx->thread, qpair_ctx->cb_fn, qpair_ctx->ctx); 761 } 762 free(qpair_ctx); 763 } 764 765 static void 766 _spdk_nvmf_qpair_destroy(void *ctx, int status) 767 { 768 struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx; 769 struct spdk_nvmf_qpair *qpair = qpair_ctx->qpair; 770 struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr; 771 struct spdk_nvmf_transport_poll_group *tgroup; 772 struct spdk_nvmf_request *req, *tmp; 773 struct spdk_nvmf_subsystem_poll_group *sgroup; 774 int rc; 775 776 assert(qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING); 777 spdk_nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_ERROR); 778 qpair_ctx->qid = qpair->qid; 779 780 /* Find the tgroup and remove the qpair from the tgroup */ 781 TAILQ_FOREACH(tgroup, &qpair->group->tgroups, link) { 782 if (tgroup->transport == qpair->transport) { 783 rc = spdk_nvmf_transport_poll_group_remove(tgroup, qpair); 784 if (rc && (rc != ENOTSUP)) { 785 SPDK_ERRLOG("Cannot remove qpair=%p from transport group=%p\n", 786 qpair, tgroup); 787 } 788 break; 789 } 790 } 791 792 if (ctrlr) { 793 sgroup = &qpair->group->sgroups[ctrlr->subsys->id]; 794 TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) { 795 if (req->qpair == qpair) { 796 TAILQ_REMOVE(&sgroup->queued, req, link); 797 if (spdk_nvmf_transport_req_free(req)) { 798 SPDK_ERRLOG("Transport request free error!\n"); 799 } 800 } 801 } 802 } 803 804 TAILQ_REMOVE(&qpair->group->qpairs, qpair, link); 805 806 spdk_nvmf_transport_qpair_fini(qpair); 807 808 if (!ctrlr || !ctrlr->thread) { 809 if (qpair_ctx->cb_fn) { 810 spdk_thread_send_msg(qpair_ctx->thread, qpair_ctx->cb_fn, qpair_ctx->ctx); 811 } 812 free(qpair_ctx); 813 return; 814 } 815 816 qpair_ctx->ctrlr = ctrlr; 817 spdk_thread_send_msg(ctrlr->thread, _spdk_nvmf_ctrlr_free_from_qpair, qpair_ctx); 818 819 } 820 821 int 822 spdk_nvmf_qpair_disconnect(struct spdk_nvmf_qpair *qpair, nvmf_qpair_disconnect_cb cb_fn, void *ctx) 823 { 824 struct nvmf_qpair_disconnect_ctx *qpair_ctx; 825 826 /* If we get a qpair in the uninitialized state, we can just destroy it immediately */ 827 if (qpair->state == SPDK_NVMF_QPAIR_UNINITIALIZED) { 828 spdk_nvmf_transport_qpair_fini(qpair); 829 if (cb_fn) { 830 cb_fn(ctx); 831 } 832 return 0; 833 } 834 835 /* The queue pair must be disconnected from the thread that owns it */ 836 assert(qpair->group->thread == spdk_get_thread()); 837 838 if (qpair->state != SPDK_NVMF_QPAIR_ACTIVE) { 839 /* This can occur if the connection is killed by the target, 840 * which results in a notification that the connection 841 * died. Send a message to defer the processing of this 842 * callback. This allows the stack to unwind in the case 843 * where a bunch of connections are disconnected in 844 * a loop. */ 845 if (cb_fn) { 846 spdk_thread_send_msg(qpair->group->thread, cb_fn, ctx); 847 } 848 return 0; 849 } 850 851 assert(qpair->state == SPDK_NVMF_QPAIR_ACTIVE); 852 spdk_nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_DEACTIVATING); 853 854 qpair_ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_ctx)); 855 if (!qpair_ctx) { 856 SPDK_ERRLOG("Unable to allocate context for nvmf_qpair_disconnect\n"); 857 return -ENOMEM; 858 } 859 860 qpair_ctx->qpair = qpair; 861 qpair_ctx->cb_fn = cb_fn; 862 qpair_ctx->thread = qpair->group->thread; 863 qpair_ctx->ctx = ctx; 864 865 /* Check for outstanding I/O */ 866 if (!TAILQ_EMPTY(&qpair->outstanding)) { 867 qpair->state_cb = _spdk_nvmf_qpair_destroy; 868 qpair->state_cb_arg = qpair_ctx; 869 spdk_nvmf_qpair_free_aer(qpair); 870 return 0; 871 } 872 873 _spdk_nvmf_qpair_destroy(qpair_ctx, 0); 874 875 return 0; 876 } 877 878 int 879 spdk_nvmf_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair, 880 struct spdk_nvme_transport_id *trid) 881 { 882 return spdk_nvmf_transport_qpair_get_peer_trid(qpair, trid); 883 } 884 885 int 886 spdk_nvmf_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair, 887 struct spdk_nvme_transport_id *trid) 888 { 889 return spdk_nvmf_transport_qpair_get_local_trid(qpair, trid); 890 } 891 892 int 893 spdk_nvmf_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair, 894 struct spdk_nvme_transport_id *trid) 895 { 896 return spdk_nvmf_transport_qpair_get_listen_trid(qpair, trid); 897 } 898 899 int 900 spdk_nvmf_poll_group_add_transport(struct spdk_nvmf_poll_group *group, 901 struct spdk_nvmf_transport *transport) 902 { 903 struct spdk_nvmf_transport_poll_group *tgroup; 904 905 TAILQ_FOREACH(tgroup, &group->tgroups, link) { 906 if (tgroup->transport == transport) { 907 /* Transport already in the poll group */ 908 return 0; 909 } 910 } 911 912 tgroup = spdk_nvmf_transport_poll_group_create(transport); 913 if (!tgroup) { 914 SPDK_ERRLOG("Unable to create poll group for transport\n"); 915 return -1; 916 } 917 918 tgroup->group = group; 919 TAILQ_INSERT_TAIL(&group->tgroups, tgroup, link); 920 921 return 0; 922 } 923 924 static int 925 poll_group_update_subsystem(struct spdk_nvmf_poll_group *group, 926 struct spdk_nvmf_subsystem *subsystem) 927 { 928 struct spdk_nvmf_subsystem_poll_group *sgroup; 929 uint32_t new_num_ns, old_num_ns; 930 uint32_t i, j; 931 struct spdk_nvmf_ns *ns; 932 struct spdk_nvmf_registrant *reg, *tmp; 933 struct spdk_io_channel *ch; 934 struct spdk_nvmf_subsystem_pg_ns_info *ns_info; 935 struct spdk_nvmf_ctrlr *ctrlr; 936 bool ns_changed; 937 938 /* Make sure our poll group has memory for this subsystem allocated */ 939 if (subsystem->id >= group->num_sgroups) { 940 return -ENOMEM; 941 } 942 943 sgroup = &group->sgroups[subsystem->id]; 944 945 /* Make sure the array of namespace information is the correct size */ 946 new_num_ns = subsystem->max_nsid; 947 old_num_ns = sgroup->num_ns; 948 949 ns_changed = false; 950 951 if (old_num_ns == 0) { 952 if (new_num_ns > 0) { 953 /* First allocation */ 954 sgroup->ns_info = calloc(new_num_ns, sizeof(struct spdk_nvmf_subsystem_pg_ns_info)); 955 if (!sgroup->ns_info) { 956 return -ENOMEM; 957 } 958 } 959 } else if (new_num_ns > old_num_ns) { 960 void *buf; 961 962 /* Make the array larger */ 963 buf = realloc(sgroup->ns_info, new_num_ns * sizeof(struct spdk_nvmf_subsystem_pg_ns_info)); 964 if (!buf) { 965 return -ENOMEM; 966 } 967 968 sgroup->ns_info = buf; 969 970 /* Null out the new namespace information slots */ 971 for (i = old_num_ns; i < new_num_ns; i++) { 972 memset(&sgroup->ns_info[i], 0, sizeof(struct spdk_nvmf_subsystem_pg_ns_info)); 973 } 974 } else if (new_num_ns < old_num_ns) { 975 void *buf; 976 977 /* Free the extra I/O channels */ 978 for (i = new_num_ns; i < old_num_ns; i++) { 979 ns_info = &sgroup->ns_info[i]; 980 981 if (ns_info->channel) { 982 spdk_put_io_channel(ns_info->channel); 983 ns_info->channel = NULL; 984 } 985 } 986 987 /* Make the array smaller */ 988 if (new_num_ns > 0) { 989 buf = realloc(sgroup->ns_info, new_num_ns * sizeof(struct spdk_nvmf_subsystem_pg_ns_info)); 990 if (!buf) { 991 return -ENOMEM; 992 } 993 sgroup->ns_info = buf; 994 } else { 995 free(sgroup->ns_info); 996 sgroup->ns_info = NULL; 997 } 998 } 999 1000 sgroup->num_ns = new_num_ns; 1001 1002 /* Detect bdevs that were added or removed */ 1003 for (i = 0; i < sgroup->num_ns; i++) { 1004 ns = subsystem->ns[i]; 1005 ns_info = &sgroup->ns_info[i]; 1006 ch = ns_info->channel; 1007 1008 if (ns == NULL && ch == NULL) { 1009 /* Both NULL. Leave empty */ 1010 } else if (ns == NULL && ch != NULL) { 1011 /* There was a channel here, but the namespace is gone. */ 1012 ns_changed = true; 1013 spdk_put_io_channel(ch); 1014 ns_info->channel = NULL; 1015 } else if (ns != NULL && ch == NULL) { 1016 /* A namespace appeared but there is no channel yet */ 1017 ns_changed = true; 1018 ch = spdk_bdev_get_io_channel(ns->desc); 1019 if (ch == NULL) { 1020 SPDK_ERRLOG("Could not allocate I/O channel.\n"); 1021 return -ENOMEM; 1022 } 1023 ns_info->channel = ch; 1024 } else if (spdk_uuid_compare(&ns_info->uuid, spdk_bdev_get_uuid(ns->bdev)) != 0) { 1025 /* A namespace was here before, but was replaced by a new one. */ 1026 ns_changed = true; 1027 spdk_put_io_channel(ns_info->channel); 1028 memset(ns_info, 0, sizeof(*ns_info)); 1029 1030 ch = spdk_bdev_get_io_channel(ns->desc); 1031 if (ch == NULL) { 1032 SPDK_ERRLOG("Could not allocate I/O channel.\n"); 1033 return -ENOMEM; 1034 } 1035 ns_info->channel = ch; 1036 } 1037 1038 if (ns == NULL) { 1039 memset(ns_info, 0, sizeof(*ns_info)); 1040 } else { 1041 ns_info->uuid = *spdk_bdev_get_uuid(ns->bdev); 1042 ns_info->crkey = ns->crkey; 1043 ns_info->rtype = ns->rtype; 1044 if (ns->holder) { 1045 ns_info->holder_id = ns->holder->hostid; 1046 } 1047 1048 memset(&ns_info->reg_hostid, 0, SPDK_NVMF_MAX_NUM_REGISTRANTS * sizeof(struct spdk_uuid)); 1049 j = 0; 1050 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 1051 if (j >= SPDK_NVMF_MAX_NUM_REGISTRANTS) { 1052 SPDK_ERRLOG("Maximum %u registrants can support.\n", SPDK_NVMF_MAX_NUM_REGISTRANTS); 1053 return -EINVAL; 1054 } 1055 ns_info->reg_hostid[j++] = reg->hostid; 1056 } 1057 } 1058 } 1059 1060 if (ns_changed) { 1061 TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) { 1062 if (ctrlr->admin_qpair->group == group) { 1063 spdk_nvmf_ctrlr_async_event_ns_notice(ctrlr); 1064 } 1065 } 1066 } 1067 1068 return 0; 1069 } 1070 1071 int 1072 spdk_nvmf_poll_group_update_subsystem(struct spdk_nvmf_poll_group *group, 1073 struct spdk_nvmf_subsystem *subsystem) 1074 { 1075 return poll_group_update_subsystem(group, subsystem); 1076 } 1077 1078 int 1079 spdk_nvmf_poll_group_add_subsystem(struct spdk_nvmf_poll_group *group, 1080 struct spdk_nvmf_subsystem *subsystem, 1081 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg) 1082 { 1083 int rc = 0; 1084 struct spdk_nvmf_subsystem_poll_group *sgroup = &group->sgroups[subsystem->id]; 1085 1086 TAILQ_INIT(&sgroup->queued); 1087 1088 rc = poll_group_update_subsystem(group, subsystem); 1089 if (rc) { 1090 spdk_nvmf_poll_group_remove_subsystem(group, subsystem, NULL, NULL); 1091 goto fini; 1092 } 1093 1094 sgroup->state = SPDK_NVMF_SUBSYSTEM_ACTIVE; 1095 fini: 1096 if (cb_fn) { 1097 cb_fn(cb_arg, rc); 1098 } 1099 1100 return rc; 1101 } 1102 1103 static void 1104 _nvmf_poll_group_remove_subsystem_cb(void *ctx, int status) 1105 { 1106 struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx; 1107 struct spdk_nvmf_subsystem *subsystem; 1108 struct spdk_nvmf_poll_group *group; 1109 struct spdk_nvmf_subsystem_poll_group *sgroup; 1110 spdk_nvmf_poll_group_mod_done cpl_fn = NULL; 1111 void *cpl_ctx = NULL; 1112 uint32_t nsid; 1113 1114 group = qpair_ctx->group; 1115 subsystem = qpair_ctx->subsystem; 1116 cpl_fn = qpair_ctx->cpl_fn; 1117 cpl_ctx = qpair_ctx->cpl_ctx; 1118 sgroup = &group->sgroups[subsystem->id]; 1119 1120 if (status) { 1121 goto fini; 1122 } 1123 1124 for (nsid = 0; nsid < sgroup->num_ns; nsid++) { 1125 if (sgroup->ns_info[nsid].channel) { 1126 spdk_put_io_channel(sgroup->ns_info[nsid].channel); 1127 sgroup->ns_info[nsid].channel = NULL; 1128 } 1129 } 1130 1131 sgroup->num_ns = 0; 1132 free(sgroup->ns_info); 1133 sgroup->ns_info = NULL; 1134 fini: 1135 free(qpair_ctx); 1136 if (cpl_fn) { 1137 cpl_fn(cpl_ctx, status); 1138 } 1139 } 1140 1141 static void 1142 _nvmf_subsystem_disconnect_next_qpair(void *ctx) 1143 { 1144 struct spdk_nvmf_qpair *qpair; 1145 struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx; 1146 struct spdk_nvmf_subsystem *subsystem; 1147 struct spdk_nvmf_poll_group *group; 1148 int rc = 0; 1149 1150 group = qpair_ctx->group; 1151 subsystem = qpair_ctx->subsystem; 1152 1153 TAILQ_FOREACH(qpair, &group->qpairs, link) { 1154 if ((qpair->ctrlr != NULL) && (qpair->ctrlr->subsys == subsystem)) { 1155 break; 1156 } 1157 } 1158 1159 if (qpair) { 1160 rc = spdk_nvmf_qpair_disconnect(qpair, _nvmf_subsystem_disconnect_next_qpair, qpair_ctx); 1161 } 1162 1163 if (!qpair || rc != 0) { 1164 _nvmf_poll_group_remove_subsystem_cb(ctx, rc); 1165 } 1166 return; 1167 } 1168 1169 void 1170 spdk_nvmf_poll_group_remove_subsystem(struct spdk_nvmf_poll_group *group, 1171 struct spdk_nvmf_subsystem *subsystem, 1172 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg) 1173 { 1174 struct spdk_nvmf_qpair *qpair; 1175 struct spdk_nvmf_subsystem_poll_group *sgroup; 1176 struct nvmf_qpair_disconnect_many_ctx *ctx; 1177 int rc = 0; 1178 1179 ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_many_ctx)); 1180 1181 if (!ctx) { 1182 SPDK_ERRLOG("Unable to allocate memory for context to remove poll subsystem\n"); 1183 goto fini; 1184 } 1185 1186 ctx->group = group; 1187 ctx->subsystem = subsystem; 1188 ctx->cpl_fn = cb_fn; 1189 ctx->cpl_ctx = cb_arg; 1190 1191 sgroup = &group->sgroups[subsystem->id]; 1192 sgroup->state = SPDK_NVMF_SUBSYSTEM_INACTIVE; 1193 1194 TAILQ_FOREACH(qpair, &group->qpairs, link) { 1195 if ((qpair->ctrlr != NULL) && (qpair->ctrlr->subsys == subsystem)) { 1196 break; 1197 } 1198 } 1199 1200 if (qpair) { 1201 rc = spdk_nvmf_qpair_disconnect(qpair, _nvmf_subsystem_disconnect_next_qpair, ctx); 1202 } else { 1203 /* call the callback immediately. It will handle any channel iteration */ 1204 _nvmf_poll_group_remove_subsystem_cb(ctx, 0); 1205 } 1206 1207 if (rc != 0) { 1208 free(ctx); 1209 goto fini; 1210 } 1211 1212 return; 1213 fini: 1214 if (cb_fn) { 1215 cb_fn(cb_arg, rc); 1216 } 1217 } 1218 1219 void 1220 spdk_nvmf_poll_group_pause_subsystem(struct spdk_nvmf_poll_group *group, 1221 struct spdk_nvmf_subsystem *subsystem, 1222 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg) 1223 { 1224 struct spdk_nvmf_subsystem_poll_group *sgroup; 1225 int rc = 0; 1226 1227 if (subsystem->id >= group->num_sgroups) { 1228 rc = -1; 1229 goto fini; 1230 } 1231 1232 sgroup = &group->sgroups[subsystem->id]; 1233 if (sgroup == NULL) { 1234 rc = -1; 1235 goto fini; 1236 } 1237 1238 assert(sgroup->state == SPDK_NVMF_SUBSYSTEM_ACTIVE); 1239 sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSING; 1240 1241 if (sgroup->io_outstanding > 0) { 1242 sgroup->cb_fn = cb_fn; 1243 sgroup->cb_arg = cb_arg; 1244 return; 1245 } 1246 1247 assert(sgroup->io_outstanding == 0); 1248 sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSED; 1249 fini: 1250 if (cb_fn) { 1251 cb_fn(cb_arg, rc); 1252 } 1253 } 1254 1255 void 1256 spdk_nvmf_poll_group_resume_subsystem(struct spdk_nvmf_poll_group *group, 1257 struct spdk_nvmf_subsystem *subsystem, 1258 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg) 1259 { 1260 struct spdk_nvmf_request *req, *tmp; 1261 struct spdk_nvmf_subsystem_poll_group *sgroup; 1262 int rc = 0; 1263 1264 if (subsystem->id >= group->num_sgroups) { 1265 rc = -1; 1266 goto fini; 1267 } 1268 1269 sgroup = &group->sgroups[subsystem->id]; 1270 1271 assert(sgroup->state == SPDK_NVMF_SUBSYSTEM_PAUSED); 1272 1273 rc = poll_group_update_subsystem(group, subsystem); 1274 if (rc) { 1275 goto fini; 1276 } 1277 1278 sgroup->state = SPDK_NVMF_SUBSYSTEM_ACTIVE; 1279 1280 /* Release all queued requests */ 1281 TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) { 1282 TAILQ_REMOVE(&sgroup->queued, req, link); 1283 spdk_nvmf_request_exec(req); 1284 } 1285 fini: 1286 if (cb_fn) { 1287 cb_fn(cb_arg, rc); 1288 } 1289 } 1290 1291 1292 struct spdk_nvmf_poll_group * 1293 spdk_nvmf_get_optimal_poll_group(struct spdk_nvmf_qpair *qpair) 1294 { 1295 struct spdk_nvmf_transport_poll_group *tgroup; 1296 1297 tgroup = spdk_nvmf_transport_get_optimal_poll_group(qpair->transport, qpair); 1298 1299 if (tgroup == NULL) { 1300 return NULL; 1301 } 1302 1303 return tgroup->group; 1304 } 1305 1306 int 1307 spdk_nvmf_poll_group_get_stat(struct spdk_nvmf_tgt *tgt, 1308 struct spdk_nvmf_poll_group_stat *stat) 1309 { 1310 struct spdk_io_channel *ch; 1311 struct spdk_nvmf_poll_group *group; 1312 1313 if (tgt == NULL || stat == NULL) { 1314 return -EINVAL; 1315 } 1316 1317 ch = spdk_get_io_channel(tgt); 1318 group = spdk_io_channel_get_ctx(ch); 1319 *stat = group->stat; 1320 spdk_put_io_channel(ch); 1321 return 0; 1322 } 1323