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