1 /* 2 * NVMe over Fabrics loopback device. 3 * Copyright (c) 2015-2016 HGST, a Western Digital Company. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms and conditions of the GNU General Public License, 7 * version 2, as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 */ 14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 15 #include <linux/scatterlist.h> 16 #include <linux/blk-mq.h> 17 #include <linux/nvme.h> 18 #include <linux/module.h> 19 #include <linux/parser.h> 20 #include "nvmet.h" 21 #include "../host/nvme.h" 22 #include "../host/fabrics.h" 23 24 #define NVME_LOOP_MAX_SEGMENTS 256 25 26 /* 27 * We handle AEN commands ourselves and don't even let the 28 * block layer know about them. 29 */ 30 #define NVME_LOOP_NR_AEN_COMMANDS 1 31 #define NVME_LOOP_AQ_BLKMQ_DEPTH \ 32 (NVME_AQ_DEPTH - NVME_LOOP_NR_AEN_COMMANDS) 33 34 struct nvme_loop_iod { 35 struct nvme_request nvme_req; 36 struct nvme_command cmd; 37 struct nvme_completion rsp; 38 struct nvmet_req req; 39 struct nvme_loop_queue *queue; 40 struct work_struct work; 41 struct sg_table sg_table; 42 struct scatterlist first_sgl[]; 43 }; 44 45 struct nvme_loop_ctrl { 46 struct nvme_loop_queue *queues; 47 48 struct blk_mq_tag_set admin_tag_set; 49 50 struct list_head list; 51 struct blk_mq_tag_set tag_set; 52 struct nvme_loop_iod async_event_iod; 53 struct nvme_ctrl ctrl; 54 55 struct nvmet_ctrl *target_ctrl; 56 }; 57 58 static inline struct nvme_loop_ctrl *to_loop_ctrl(struct nvme_ctrl *ctrl) 59 { 60 return container_of(ctrl, struct nvme_loop_ctrl, ctrl); 61 } 62 63 struct nvme_loop_queue { 64 struct nvmet_cq nvme_cq; 65 struct nvmet_sq nvme_sq; 66 struct nvme_loop_ctrl *ctrl; 67 }; 68 69 static struct nvmet_port *nvmet_loop_port; 70 71 static LIST_HEAD(nvme_loop_ctrl_list); 72 static DEFINE_MUTEX(nvme_loop_ctrl_mutex); 73 74 static void nvme_loop_queue_response(struct nvmet_req *nvme_req); 75 static void nvme_loop_delete_ctrl(struct nvmet_ctrl *ctrl); 76 77 static struct nvmet_fabrics_ops nvme_loop_ops; 78 79 static inline int nvme_loop_queue_idx(struct nvme_loop_queue *queue) 80 { 81 return queue - queue->ctrl->queues; 82 } 83 84 static void nvme_loop_complete_rq(struct request *req) 85 { 86 struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req); 87 88 nvme_cleanup_cmd(req); 89 sg_free_table_chained(&iod->sg_table, true); 90 nvme_complete_rq(req); 91 } 92 93 static struct blk_mq_tags *nvme_loop_tagset(struct nvme_loop_queue *queue) 94 { 95 u32 queue_idx = nvme_loop_queue_idx(queue); 96 97 if (queue_idx == 0) 98 return queue->ctrl->admin_tag_set.tags[queue_idx]; 99 return queue->ctrl->tag_set.tags[queue_idx - 1]; 100 } 101 102 static void nvme_loop_queue_response(struct nvmet_req *req) 103 { 104 struct nvme_loop_queue *queue = 105 container_of(req->sq, struct nvme_loop_queue, nvme_sq); 106 struct nvme_completion *cqe = req->rsp; 107 108 /* 109 * AEN requests are special as they don't time out and can 110 * survive any kind of queue freeze and often don't respond to 111 * aborts. We don't even bother to allocate a struct request 112 * for them but rather special case them here. 113 */ 114 if (unlikely(nvme_loop_queue_idx(queue) == 0 && 115 cqe->command_id >= NVME_LOOP_AQ_BLKMQ_DEPTH)) { 116 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status, 117 &cqe->result); 118 } else { 119 struct request *rq; 120 121 rq = blk_mq_tag_to_rq(nvme_loop_tagset(queue), cqe->command_id); 122 if (!rq) { 123 dev_err(queue->ctrl->ctrl.device, 124 "tag 0x%x on queue %d not found\n", 125 cqe->command_id, nvme_loop_queue_idx(queue)); 126 return; 127 } 128 129 nvme_end_request(rq, cqe->status, cqe->result); 130 } 131 } 132 133 static void nvme_loop_execute_work(struct work_struct *work) 134 { 135 struct nvme_loop_iod *iod = 136 container_of(work, struct nvme_loop_iod, work); 137 138 iod->req.execute(&iod->req); 139 } 140 141 static enum blk_eh_timer_return 142 nvme_loop_timeout(struct request *rq, bool reserved) 143 { 144 struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(rq); 145 146 /* queue error recovery */ 147 nvme_reset_ctrl(&iod->queue->ctrl->ctrl); 148 149 /* fail with DNR on admin cmd timeout */ 150 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR; 151 152 return BLK_EH_HANDLED; 153 } 154 155 static blk_status_t nvme_loop_queue_rq(struct blk_mq_hw_ctx *hctx, 156 const struct blk_mq_queue_data *bd) 157 { 158 struct nvme_ns *ns = hctx->queue->queuedata; 159 struct nvme_loop_queue *queue = hctx->driver_data; 160 struct request *req = bd->rq; 161 struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req); 162 blk_status_t ret; 163 164 ret = nvme_setup_cmd(ns, req, &iod->cmd); 165 if (ret) 166 return ret; 167 168 iod->cmd.common.flags |= NVME_CMD_SGL_METABUF; 169 iod->req.port = nvmet_loop_port; 170 if (!nvmet_req_init(&iod->req, &queue->nvme_cq, 171 &queue->nvme_sq, &nvme_loop_ops)) { 172 nvme_cleanup_cmd(req); 173 blk_mq_start_request(req); 174 nvme_loop_queue_response(&iod->req); 175 return BLK_STS_OK; 176 } 177 178 if (blk_rq_bytes(req)) { 179 iod->sg_table.sgl = iod->first_sgl; 180 if (sg_alloc_table_chained(&iod->sg_table, 181 blk_rq_nr_phys_segments(req), 182 iod->sg_table.sgl)) 183 return BLK_STS_RESOURCE; 184 185 iod->req.sg = iod->sg_table.sgl; 186 iod->req.sg_cnt = blk_rq_map_sg(req->q, req, iod->sg_table.sgl); 187 } 188 189 blk_mq_start_request(req); 190 191 schedule_work(&iod->work); 192 return BLK_STS_OK; 193 } 194 195 static void nvme_loop_submit_async_event(struct nvme_ctrl *arg, int aer_idx) 196 { 197 struct nvme_loop_ctrl *ctrl = to_loop_ctrl(arg); 198 struct nvme_loop_queue *queue = &ctrl->queues[0]; 199 struct nvme_loop_iod *iod = &ctrl->async_event_iod; 200 201 memset(&iod->cmd, 0, sizeof(iod->cmd)); 202 iod->cmd.common.opcode = nvme_admin_async_event; 203 iod->cmd.common.command_id = NVME_LOOP_AQ_BLKMQ_DEPTH; 204 iod->cmd.common.flags |= NVME_CMD_SGL_METABUF; 205 206 if (!nvmet_req_init(&iod->req, &queue->nvme_cq, &queue->nvme_sq, 207 &nvme_loop_ops)) { 208 dev_err(ctrl->ctrl.device, "failed async event work\n"); 209 return; 210 } 211 212 schedule_work(&iod->work); 213 } 214 215 static int nvme_loop_init_iod(struct nvme_loop_ctrl *ctrl, 216 struct nvme_loop_iod *iod, unsigned int queue_idx) 217 { 218 iod->req.cmd = &iod->cmd; 219 iod->req.rsp = &iod->rsp; 220 iod->queue = &ctrl->queues[queue_idx]; 221 INIT_WORK(&iod->work, nvme_loop_execute_work); 222 return 0; 223 } 224 225 static int nvme_loop_init_request(struct blk_mq_tag_set *set, 226 struct request *req, unsigned int hctx_idx, 227 unsigned int numa_node) 228 { 229 struct nvme_loop_ctrl *ctrl = set->driver_data; 230 231 return nvme_loop_init_iod(ctrl, blk_mq_rq_to_pdu(req), 232 (set == &ctrl->tag_set) ? hctx_idx + 1 : 0); 233 } 234 235 static int nvme_loop_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, 236 unsigned int hctx_idx) 237 { 238 struct nvme_loop_ctrl *ctrl = data; 239 struct nvme_loop_queue *queue = &ctrl->queues[hctx_idx + 1]; 240 241 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count); 242 243 hctx->driver_data = queue; 244 return 0; 245 } 246 247 static int nvme_loop_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, 248 unsigned int hctx_idx) 249 { 250 struct nvme_loop_ctrl *ctrl = data; 251 struct nvme_loop_queue *queue = &ctrl->queues[0]; 252 253 BUG_ON(hctx_idx != 0); 254 255 hctx->driver_data = queue; 256 return 0; 257 } 258 259 static const struct blk_mq_ops nvme_loop_mq_ops = { 260 .queue_rq = nvme_loop_queue_rq, 261 .complete = nvme_loop_complete_rq, 262 .init_request = nvme_loop_init_request, 263 .init_hctx = nvme_loop_init_hctx, 264 .timeout = nvme_loop_timeout, 265 }; 266 267 static const struct blk_mq_ops nvme_loop_admin_mq_ops = { 268 .queue_rq = nvme_loop_queue_rq, 269 .complete = nvme_loop_complete_rq, 270 .init_request = nvme_loop_init_request, 271 .init_hctx = nvme_loop_init_admin_hctx, 272 .timeout = nvme_loop_timeout, 273 }; 274 275 static void nvme_loop_destroy_admin_queue(struct nvme_loop_ctrl *ctrl) 276 { 277 nvmet_sq_destroy(&ctrl->queues[0].nvme_sq); 278 blk_cleanup_queue(ctrl->ctrl.admin_q); 279 blk_mq_free_tag_set(&ctrl->admin_tag_set); 280 } 281 282 static void nvme_loop_free_ctrl(struct nvme_ctrl *nctrl) 283 { 284 struct nvme_loop_ctrl *ctrl = to_loop_ctrl(nctrl); 285 286 if (list_empty(&ctrl->list)) 287 goto free_ctrl; 288 289 mutex_lock(&nvme_loop_ctrl_mutex); 290 list_del(&ctrl->list); 291 mutex_unlock(&nvme_loop_ctrl_mutex); 292 293 if (nctrl->tagset) { 294 blk_cleanup_queue(ctrl->ctrl.connect_q); 295 blk_mq_free_tag_set(&ctrl->tag_set); 296 } 297 kfree(ctrl->queues); 298 nvmf_free_options(nctrl->opts); 299 free_ctrl: 300 kfree(ctrl); 301 } 302 303 static void nvme_loop_destroy_io_queues(struct nvme_loop_ctrl *ctrl) 304 { 305 int i; 306 307 for (i = 1; i < ctrl->ctrl.queue_count; i++) 308 nvmet_sq_destroy(&ctrl->queues[i].nvme_sq); 309 } 310 311 static int nvme_loop_init_io_queues(struct nvme_loop_ctrl *ctrl) 312 { 313 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; 314 unsigned int nr_io_queues; 315 int ret, i; 316 317 nr_io_queues = min(opts->nr_io_queues, num_online_cpus()); 318 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues); 319 if (ret || !nr_io_queues) 320 return ret; 321 322 dev_info(ctrl->ctrl.device, "creating %d I/O queues.\n", nr_io_queues); 323 324 for (i = 1; i <= nr_io_queues; i++) { 325 ctrl->queues[i].ctrl = ctrl; 326 ret = nvmet_sq_init(&ctrl->queues[i].nvme_sq); 327 if (ret) 328 goto out_destroy_queues; 329 330 ctrl->ctrl.queue_count++; 331 } 332 333 return 0; 334 335 out_destroy_queues: 336 nvme_loop_destroy_io_queues(ctrl); 337 return ret; 338 } 339 340 static int nvme_loop_connect_io_queues(struct nvme_loop_ctrl *ctrl) 341 { 342 int i, ret; 343 344 for (i = 1; i < ctrl->ctrl.queue_count; i++) { 345 ret = nvmf_connect_io_queue(&ctrl->ctrl, i); 346 if (ret) 347 return ret; 348 } 349 350 return 0; 351 } 352 353 static int nvme_loop_configure_admin_queue(struct nvme_loop_ctrl *ctrl) 354 { 355 int error; 356 357 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set)); 358 ctrl->admin_tag_set.ops = &nvme_loop_admin_mq_ops; 359 ctrl->admin_tag_set.queue_depth = NVME_LOOP_AQ_BLKMQ_DEPTH; 360 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */ 361 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE; 362 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_loop_iod) + 363 SG_CHUNK_SIZE * sizeof(struct scatterlist); 364 ctrl->admin_tag_set.driver_data = ctrl; 365 ctrl->admin_tag_set.nr_hw_queues = 1; 366 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT; 367 ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED; 368 369 ctrl->queues[0].ctrl = ctrl; 370 error = nvmet_sq_init(&ctrl->queues[0].nvme_sq); 371 if (error) 372 return error; 373 ctrl->ctrl.queue_count = 1; 374 375 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set); 376 if (error) 377 goto out_free_sq; 378 ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set; 379 380 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set); 381 if (IS_ERR(ctrl->ctrl.admin_q)) { 382 error = PTR_ERR(ctrl->ctrl.admin_q); 383 goto out_free_tagset; 384 } 385 386 error = nvmf_connect_admin_queue(&ctrl->ctrl); 387 if (error) 388 goto out_cleanup_queue; 389 390 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->ctrl.cap); 391 if (error) { 392 dev_err(ctrl->ctrl.device, 393 "prop_get NVME_REG_CAP failed\n"); 394 goto out_cleanup_queue; 395 } 396 397 ctrl->ctrl.sqsize = 398 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize); 399 400 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap); 401 if (error) 402 goto out_cleanup_queue; 403 404 ctrl->ctrl.max_hw_sectors = 405 (NVME_LOOP_MAX_SEGMENTS - 1) << (PAGE_SHIFT - 9); 406 407 error = nvme_init_identify(&ctrl->ctrl); 408 if (error) 409 goto out_cleanup_queue; 410 411 return 0; 412 413 out_cleanup_queue: 414 blk_cleanup_queue(ctrl->ctrl.admin_q); 415 out_free_tagset: 416 blk_mq_free_tag_set(&ctrl->admin_tag_set); 417 out_free_sq: 418 nvmet_sq_destroy(&ctrl->queues[0].nvme_sq); 419 return error; 420 } 421 422 static void nvme_loop_shutdown_ctrl(struct nvme_loop_ctrl *ctrl) 423 { 424 if (ctrl->ctrl.queue_count > 1) { 425 nvme_stop_queues(&ctrl->ctrl); 426 blk_mq_tagset_busy_iter(&ctrl->tag_set, 427 nvme_cancel_request, &ctrl->ctrl); 428 nvme_loop_destroy_io_queues(ctrl); 429 } 430 431 if (ctrl->ctrl.state == NVME_CTRL_LIVE) 432 nvme_shutdown_ctrl(&ctrl->ctrl); 433 434 blk_mq_quiesce_queue(ctrl->ctrl.admin_q); 435 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, 436 nvme_cancel_request, &ctrl->ctrl); 437 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); 438 nvme_loop_destroy_admin_queue(ctrl); 439 } 440 441 static void nvme_loop_delete_ctrl_host(struct nvme_ctrl *ctrl) 442 { 443 nvme_stop_ctrl(ctrl); 444 nvme_remove_namespaces(ctrl); 445 nvme_loop_shutdown_ctrl(to_loop_ctrl(ctrl)); 446 nvme_uninit_ctrl(ctrl); 447 nvme_put_ctrl(ctrl); 448 } 449 450 static void nvme_loop_delete_ctrl(struct nvmet_ctrl *nctrl) 451 { 452 struct nvme_loop_ctrl *ctrl; 453 454 mutex_lock(&nvme_loop_ctrl_mutex); 455 list_for_each_entry(ctrl, &nvme_loop_ctrl_list, list) { 456 if (ctrl->ctrl.cntlid == nctrl->cntlid) 457 nvme_delete_ctrl(&ctrl->ctrl); 458 } 459 mutex_unlock(&nvme_loop_ctrl_mutex); 460 } 461 462 static void nvme_loop_reset_ctrl_work(struct work_struct *work) 463 { 464 struct nvme_loop_ctrl *ctrl = 465 container_of(work, struct nvme_loop_ctrl, ctrl.reset_work); 466 bool changed; 467 int ret; 468 469 nvme_stop_ctrl(&ctrl->ctrl); 470 nvme_loop_shutdown_ctrl(ctrl); 471 472 ret = nvme_loop_configure_admin_queue(ctrl); 473 if (ret) 474 goto out_disable; 475 476 ret = nvme_loop_init_io_queues(ctrl); 477 if (ret) 478 goto out_destroy_admin; 479 480 ret = nvme_loop_connect_io_queues(ctrl); 481 if (ret) 482 goto out_destroy_io; 483 484 blk_mq_update_nr_hw_queues(&ctrl->tag_set, 485 ctrl->ctrl.queue_count - 1); 486 487 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); 488 WARN_ON_ONCE(!changed); 489 490 nvme_start_ctrl(&ctrl->ctrl); 491 492 return; 493 494 out_destroy_io: 495 nvme_loop_destroy_io_queues(ctrl); 496 out_destroy_admin: 497 nvme_loop_destroy_admin_queue(ctrl); 498 out_disable: 499 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n"); 500 nvme_uninit_ctrl(&ctrl->ctrl); 501 nvme_put_ctrl(&ctrl->ctrl); 502 } 503 504 static const struct nvme_ctrl_ops nvme_loop_ctrl_ops = { 505 .name = "loop", 506 .module = THIS_MODULE, 507 .flags = NVME_F_FABRICS, 508 .reg_read32 = nvmf_reg_read32, 509 .reg_read64 = nvmf_reg_read64, 510 .reg_write32 = nvmf_reg_write32, 511 .free_ctrl = nvme_loop_free_ctrl, 512 .submit_async_event = nvme_loop_submit_async_event, 513 .delete_ctrl = nvme_loop_delete_ctrl_host, 514 }; 515 516 static int nvme_loop_create_io_queues(struct nvme_loop_ctrl *ctrl) 517 { 518 int ret; 519 520 ret = nvme_loop_init_io_queues(ctrl); 521 if (ret) 522 return ret; 523 524 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set)); 525 ctrl->tag_set.ops = &nvme_loop_mq_ops; 526 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size; 527 ctrl->tag_set.reserved_tags = 1; /* fabric connect */ 528 ctrl->tag_set.numa_node = NUMA_NO_NODE; 529 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; 530 ctrl->tag_set.cmd_size = sizeof(struct nvme_loop_iod) + 531 SG_CHUNK_SIZE * sizeof(struct scatterlist); 532 ctrl->tag_set.driver_data = ctrl; 533 ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1; 534 ctrl->tag_set.timeout = NVME_IO_TIMEOUT; 535 ctrl->ctrl.tagset = &ctrl->tag_set; 536 537 ret = blk_mq_alloc_tag_set(&ctrl->tag_set); 538 if (ret) 539 goto out_destroy_queues; 540 541 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set); 542 if (IS_ERR(ctrl->ctrl.connect_q)) { 543 ret = PTR_ERR(ctrl->ctrl.connect_q); 544 goto out_free_tagset; 545 } 546 547 ret = nvme_loop_connect_io_queues(ctrl); 548 if (ret) 549 goto out_cleanup_connect_q; 550 551 return 0; 552 553 out_cleanup_connect_q: 554 blk_cleanup_queue(ctrl->ctrl.connect_q); 555 out_free_tagset: 556 blk_mq_free_tag_set(&ctrl->tag_set); 557 out_destroy_queues: 558 nvme_loop_destroy_io_queues(ctrl); 559 return ret; 560 } 561 562 static struct nvme_ctrl *nvme_loop_create_ctrl(struct device *dev, 563 struct nvmf_ctrl_options *opts) 564 { 565 struct nvme_loop_ctrl *ctrl; 566 bool changed; 567 int ret; 568 569 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); 570 if (!ctrl) 571 return ERR_PTR(-ENOMEM); 572 ctrl->ctrl.opts = opts; 573 INIT_LIST_HEAD(&ctrl->list); 574 575 INIT_WORK(&ctrl->ctrl.reset_work, nvme_loop_reset_ctrl_work); 576 577 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_loop_ctrl_ops, 578 0 /* no quirks, we're perfect! */); 579 if (ret) 580 goto out_put_ctrl; 581 582 ret = -ENOMEM; 583 584 ctrl->ctrl.sqsize = opts->queue_size - 1; 585 ctrl->ctrl.kato = opts->kato; 586 587 ctrl->queues = kcalloc(opts->nr_io_queues + 1, sizeof(*ctrl->queues), 588 GFP_KERNEL); 589 if (!ctrl->queues) 590 goto out_uninit_ctrl; 591 592 ret = nvme_loop_configure_admin_queue(ctrl); 593 if (ret) 594 goto out_free_queues; 595 596 if (opts->queue_size > ctrl->ctrl.maxcmd) { 597 /* warn if maxcmd is lower than queue_size */ 598 dev_warn(ctrl->ctrl.device, 599 "queue_size %zu > ctrl maxcmd %u, clamping down\n", 600 opts->queue_size, ctrl->ctrl.maxcmd); 601 opts->queue_size = ctrl->ctrl.maxcmd; 602 } 603 604 if (opts->nr_io_queues) { 605 ret = nvme_loop_create_io_queues(ctrl); 606 if (ret) 607 goto out_remove_admin_queue; 608 } 609 610 nvme_loop_init_iod(ctrl, &ctrl->async_event_iod, 0); 611 612 dev_info(ctrl->ctrl.device, 613 "new ctrl: \"%s\"\n", ctrl->ctrl.opts->subsysnqn); 614 615 nvme_get_ctrl(&ctrl->ctrl); 616 617 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); 618 WARN_ON_ONCE(!changed); 619 620 mutex_lock(&nvme_loop_ctrl_mutex); 621 list_add_tail(&ctrl->list, &nvme_loop_ctrl_list); 622 mutex_unlock(&nvme_loop_ctrl_mutex); 623 624 nvme_start_ctrl(&ctrl->ctrl); 625 626 return &ctrl->ctrl; 627 628 out_remove_admin_queue: 629 nvme_loop_destroy_admin_queue(ctrl); 630 out_free_queues: 631 kfree(ctrl->queues); 632 out_uninit_ctrl: 633 nvme_uninit_ctrl(&ctrl->ctrl); 634 out_put_ctrl: 635 nvme_put_ctrl(&ctrl->ctrl); 636 if (ret > 0) 637 ret = -EIO; 638 return ERR_PTR(ret); 639 } 640 641 static int nvme_loop_add_port(struct nvmet_port *port) 642 { 643 /* 644 * XXX: disalow adding more than one port so 645 * there is no connection rejections when a 646 * a subsystem is assigned to a port for which 647 * loop doesn't have a pointer. 648 * This scenario would be possible if we allowed 649 * more than one port to be added and a subsystem 650 * was assigned to a port other than nvmet_loop_port. 651 */ 652 653 if (nvmet_loop_port) 654 return -EPERM; 655 656 nvmet_loop_port = port; 657 return 0; 658 } 659 660 static void nvme_loop_remove_port(struct nvmet_port *port) 661 { 662 if (port == nvmet_loop_port) 663 nvmet_loop_port = NULL; 664 } 665 666 static struct nvmet_fabrics_ops nvme_loop_ops = { 667 .owner = THIS_MODULE, 668 .type = NVMF_TRTYPE_LOOP, 669 .add_port = nvme_loop_add_port, 670 .remove_port = nvme_loop_remove_port, 671 .queue_response = nvme_loop_queue_response, 672 .delete_ctrl = nvme_loop_delete_ctrl, 673 }; 674 675 static struct nvmf_transport_ops nvme_loop_transport = { 676 .name = "loop", 677 .create_ctrl = nvme_loop_create_ctrl, 678 }; 679 680 static int __init nvme_loop_init_module(void) 681 { 682 int ret; 683 684 ret = nvmet_register_transport(&nvme_loop_ops); 685 if (ret) 686 return ret; 687 688 ret = nvmf_register_transport(&nvme_loop_transport); 689 if (ret) 690 nvmet_unregister_transport(&nvme_loop_ops); 691 692 return ret; 693 } 694 695 static void __exit nvme_loop_cleanup_module(void) 696 { 697 struct nvme_loop_ctrl *ctrl, *next; 698 699 nvmf_unregister_transport(&nvme_loop_transport); 700 nvmet_unregister_transport(&nvme_loop_ops); 701 702 mutex_lock(&nvme_loop_ctrl_mutex); 703 list_for_each_entry_safe(ctrl, next, &nvme_loop_ctrl_list, list) 704 nvme_delete_ctrl(&ctrl->ctrl); 705 mutex_unlock(&nvme_loop_ctrl_mutex); 706 707 flush_workqueue(nvme_wq); 708 } 709 710 module_init(nvme_loop_init_module); 711 module_exit(nvme_loop_cleanup_module); 712 713 MODULE_LICENSE("GPL v2"); 714 MODULE_ALIAS("nvmet-transport-254"); /* 254 == NVMF_TRTYPE_LOOP */ 715