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