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