xref: /openbmc/linux/drivers/nvme/target/loop.c (revision e0f6d1a5)
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 enum nvme_loop_queue_flags {
56 	NVME_LOOP_Q_LIVE	= 0,
57 };
58 
59 struct nvme_loop_queue {
60 	struct nvmet_cq		nvme_cq;
61 	struct nvmet_sq		nvme_sq;
62 	struct nvme_loop_ctrl	*ctrl;
63 	unsigned long		flags;
64 };
65 
66 static struct nvmet_port *nvmet_loop_port;
67 
68 static LIST_HEAD(nvme_loop_ctrl_list);
69 static DEFINE_MUTEX(nvme_loop_ctrl_mutex);
70 
71 static void nvme_loop_queue_response(struct nvmet_req *nvme_req);
72 static void nvme_loop_delete_ctrl(struct nvmet_ctrl *ctrl);
73 
74 static const struct nvmet_fabrics_ops nvme_loop_ops;
75 
76 static inline int nvme_loop_queue_idx(struct nvme_loop_queue *queue)
77 {
78 	return queue - queue->ctrl->queues;
79 }
80 
81 static void nvme_loop_complete_rq(struct request *req)
82 {
83 	struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req);
84 
85 	nvme_cleanup_cmd(req);
86 	sg_free_table_chained(&iod->sg_table, true);
87 	nvme_complete_rq(req);
88 }
89 
90 static struct blk_mq_tags *nvme_loop_tagset(struct nvme_loop_queue *queue)
91 {
92 	u32 queue_idx = nvme_loop_queue_idx(queue);
93 
94 	if (queue_idx == 0)
95 		return queue->ctrl->admin_tag_set.tags[queue_idx];
96 	return queue->ctrl->tag_set.tags[queue_idx - 1];
97 }
98 
99 static void nvme_loop_queue_response(struct nvmet_req *req)
100 {
101 	struct nvme_loop_queue *queue =
102 		container_of(req->sq, struct nvme_loop_queue, nvme_sq);
103 	struct nvme_completion *cqe = req->rsp;
104 
105 	/*
106 	 * AEN requests are special as they don't time out and can
107 	 * survive any kind of queue freeze and often don't respond to
108 	 * aborts.  We don't even bother to allocate a struct request
109 	 * for them but rather special case them here.
110 	 */
111 	if (unlikely(nvme_loop_queue_idx(queue) == 0 &&
112 			cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH)) {
113 		nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
114 				&cqe->result);
115 	} else {
116 		struct request *rq;
117 
118 		rq = blk_mq_tag_to_rq(nvme_loop_tagset(queue), cqe->command_id);
119 		if (!rq) {
120 			dev_err(queue->ctrl->ctrl.device,
121 				"tag 0x%x on queue %d not found\n",
122 				cqe->command_id, nvme_loop_queue_idx(queue));
123 			return;
124 		}
125 
126 		nvme_end_request(rq, cqe->status, cqe->result);
127 	}
128 }
129 
130 static void nvme_loop_execute_work(struct work_struct *work)
131 {
132 	struct nvme_loop_iod *iod =
133 		container_of(work, struct nvme_loop_iod, work);
134 
135 	nvmet_req_execute(&iod->req);
136 }
137 
138 static enum blk_eh_timer_return
139 nvme_loop_timeout(struct request *rq, bool reserved)
140 {
141 	struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(rq);
142 
143 	/* queue error recovery */
144 	nvme_reset_ctrl(&iod->queue->ctrl->ctrl);
145 
146 	/* fail with DNR on admin cmd timeout */
147 	nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
148 
149 	return BLK_EH_HANDLED;
150 }
151 
152 static blk_status_t nvme_loop_queue_rq(struct blk_mq_hw_ctx *hctx,
153 		const struct blk_mq_queue_data *bd)
154 {
155 	struct nvme_ns *ns = hctx->queue->queuedata;
156 	struct nvme_loop_queue *queue = hctx->driver_data;
157 	struct request *req = bd->rq;
158 	struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req);
159 	blk_status_t ret;
160 
161 	ret = nvmf_check_if_ready(&queue->ctrl->ctrl, req,
162 		test_bit(NVME_LOOP_Q_LIVE, &queue->flags), true);
163 	if (unlikely(ret))
164 		return ret;
165 
166 	ret = nvme_setup_cmd(ns, req, &iod->cmd);
167 	if (ret)
168 		return ret;
169 
170 	blk_mq_start_request(req);
171 	iod->cmd.common.flags |= NVME_CMD_SGL_METABUF;
172 	iod->req.port = nvmet_loop_port;
173 	if (!nvmet_req_init(&iod->req, &queue->nvme_cq,
174 			&queue->nvme_sq, &nvme_loop_ops))
175 		return BLK_STS_OK;
176 
177 	if (blk_rq_payload_bytes(req)) {
178 		iod->sg_table.sgl = iod->first_sgl;
179 		if (sg_alloc_table_chained(&iod->sg_table,
180 				blk_rq_nr_phys_segments(req),
181 				iod->sg_table.sgl))
182 			return BLK_STS_RESOURCE;
183 
184 		iod->req.sg = iod->sg_table.sgl;
185 		iod->req.sg_cnt = blk_rq_map_sg(req->q, req, iod->sg_table.sgl);
186 		iod->req.transfer_len = blk_rq_payload_bytes(req);
187 	}
188 
189 	schedule_work(&iod->work);
190 	return BLK_STS_OK;
191 }
192 
193 static void nvme_loop_submit_async_event(struct nvme_ctrl *arg)
194 {
195 	struct nvme_loop_ctrl *ctrl = to_loop_ctrl(arg);
196 	struct nvme_loop_queue *queue = &ctrl->queues[0];
197 	struct nvme_loop_iod *iod = &ctrl->async_event_iod;
198 
199 	memset(&iod->cmd, 0, sizeof(iod->cmd));
200 	iod->cmd.common.opcode = nvme_admin_async_event;
201 	iod->cmd.common.command_id = NVME_AQ_BLK_MQ_DEPTH;
202 	iod->cmd.common.flags |= NVME_CMD_SGL_METABUF;
203 
204 	if (!nvmet_req_init(&iod->req, &queue->nvme_cq, &queue->nvme_sq,
205 			&nvme_loop_ops)) {
206 		dev_err(ctrl->ctrl.device, "failed async event work\n");
207 		return;
208 	}
209 
210 	schedule_work(&iod->work);
211 }
212 
213 static int nvme_loop_init_iod(struct nvme_loop_ctrl *ctrl,
214 		struct nvme_loop_iod *iod, unsigned int queue_idx)
215 {
216 	iod->req.cmd = &iod->cmd;
217 	iod->req.rsp = &iod->rsp;
218 	iod->queue = &ctrl->queues[queue_idx];
219 	INIT_WORK(&iod->work, nvme_loop_execute_work);
220 	return 0;
221 }
222 
223 static int nvme_loop_init_request(struct blk_mq_tag_set *set,
224 		struct request *req, unsigned int hctx_idx,
225 		unsigned int numa_node)
226 {
227 	struct nvme_loop_ctrl *ctrl = set->driver_data;
228 
229 	return nvme_loop_init_iod(ctrl, blk_mq_rq_to_pdu(req),
230 			(set == &ctrl->tag_set) ? hctx_idx + 1 : 0);
231 }
232 
233 static int nvme_loop_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
234 		unsigned int hctx_idx)
235 {
236 	struct nvme_loop_ctrl *ctrl = data;
237 	struct nvme_loop_queue *queue = &ctrl->queues[hctx_idx + 1];
238 
239 	BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
240 
241 	hctx->driver_data = queue;
242 	return 0;
243 }
244 
245 static int nvme_loop_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
246 		unsigned int hctx_idx)
247 {
248 	struct nvme_loop_ctrl *ctrl = data;
249 	struct nvme_loop_queue *queue = &ctrl->queues[0];
250 
251 	BUG_ON(hctx_idx != 0);
252 
253 	hctx->driver_data = queue;
254 	return 0;
255 }
256 
257 static const struct blk_mq_ops nvme_loop_mq_ops = {
258 	.queue_rq	= nvme_loop_queue_rq,
259 	.complete	= nvme_loop_complete_rq,
260 	.init_request	= nvme_loop_init_request,
261 	.init_hctx	= nvme_loop_init_hctx,
262 	.timeout	= nvme_loop_timeout,
263 };
264 
265 static const struct blk_mq_ops nvme_loop_admin_mq_ops = {
266 	.queue_rq	= nvme_loop_queue_rq,
267 	.complete	= nvme_loop_complete_rq,
268 	.init_request	= nvme_loop_init_request,
269 	.init_hctx	= nvme_loop_init_admin_hctx,
270 	.timeout	= nvme_loop_timeout,
271 };
272 
273 static void nvme_loop_destroy_admin_queue(struct nvme_loop_ctrl *ctrl)
274 {
275 	clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags);
276 	nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
277 	blk_cleanup_queue(ctrl->ctrl.admin_q);
278 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
279 }
280 
281 static void nvme_loop_free_ctrl(struct nvme_ctrl *nctrl)
282 {
283 	struct nvme_loop_ctrl *ctrl = to_loop_ctrl(nctrl);
284 
285 	if (list_empty(&ctrl->list))
286 		goto free_ctrl;
287 
288 	mutex_lock(&nvme_loop_ctrl_mutex);
289 	list_del(&ctrl->list);
290 	mutex_unlock(&nvme_loop_ctrl_mutex);
291 
292 	if (nctrl->tagset) {
293 		blk_cleanup_queue(ctrl->ctrl.connect_q);
294 		blk_mq_free_tag_set(&ctrl->tag_set);
295 	}
296 	kfree(ctrl->queues);
297 	nvmf_free_options(nctrl->opts);
298 free_ctrl:
299 	kfree(ctrl);
300 }
301 
302 static void nvme_loop_destroy_io_queues(struct nvme_loop_ctrl *ctrl)
303 {
304 	int i;
305 
306 	for (i = 1; i < ctrl->ctrl.queue_count; i++) {
307 		clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags);
308 		nvmet_sq_destroy(&ctrl->queues[i].nvme_sq);
309 	}
310 }
311 
312 static int nvme_loop_init_io_queues(struct nvme_loop_ctrl *ctrl)
313 {
314 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
315 	unsigned int nr_io_queues;
316 	int ret, i;
317 
318 	nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
319 	ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
320 	if (ret || !nr_io_queues)
321 		return ret;
322 
323 	dev_info(ctrl->ctrl.device, "creating %d I/O queues.\n", nr_io_queues);
324 
325 	for (i = 1; i <= nr_io_queues; i++) {
326 		ctrl->queues[i].ctrl = ctrl;
327 		ret = nvmet_sq_init(&ctrl->queues[i].nvme_sq);
328 		if (ret)
329 			goto out_destroy_queues;
330 
331 		ctrl->ctrl.queue_count++;
332 	}
333 
334 	return 0;
335 
336 out_destroy_queues:
337 	nvme_loop_destroy_io_queues(ctrl);
338 	return ret;
339 }
340 
341 static int nvme_loop_connect_io_queues(struct nvme_loop_ctrl *ctrl)
342 {
343 	int i, ret;
344 
345 	for (i = 1; i < ctrl->ctrl.queue_count; i++) {
346 		ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
347 		if (ret)
348 			return ret;
349 		set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags);
350 	}
351 
352 	return 0;
353 }
354 
355 static int nvme_loop_configure_admin_queue(struct nvme_loop_ctrl *ctrl)
356 {
357 	int error;
358 
359 	memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
360 	ctrl->admin_tag_set.ops = &nvme_loop_admin_mq_ops;
361 	ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
362 	ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
363 	ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
364 	ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_loop_iod) +
365 		SG_CHUNK_SIZE * sizeof(struct scatterlist);
366 	ctrl->admin_tag_set.driver_data = ctrl;
367 	ctrl->admin_tag_set.nr_hw_queues = 1;
368 	ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
369 	ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
370 
371 	ctrl->queues[0].ctrl = ctrl;
372 	error = nvmet_sq_init(&ctrl->queues[0].nvme_sq);
373 	if (error)
374 		return error;
375 	ctrl->ctrl.queue_count = 1;
376 
377 	error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
378 	if (error)
379 		goto out_free_sq;
380 	ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
381 
382 	ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
383 	if (IS_ERR(ctrl->ctrl.admin_q)) {
384 		error = PTR_ERR(ctrl->ctrl.admin_q);
385 		goto out_free_tagset;
386 	}
387 
388 	error = nvmf_connect_admin_queue(&ctrl->ctrl);
389 	if (error)
390 		goto out_cleanup_queue;
391 
392 	set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags);
393 
394 	error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->ctrl.cap);
395 	if (error) {
396 		dev_err(ctrl->ctrl.device,
397 			"prop_get NVME_REG_CAP failed\n");
398 		goto out_cleanup_queue;
399 	}
400 
401 	ctrl->ctrl.sqsize =
402 		min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
403 
404 	error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
405 	if (error)
406 		goto out_cleanup_queue;
407 
408 	ctrl->ctrl.max_hw_sectors =
409 		(NVME_LOOP_MAX_SEGMENTS - 1) << (PAGE_SHIFT - 9);
410 
411 	error = nvme_init_identify(&ctrl->ctrl);
412 	if (error)
413 		goto out_cleanup_queue;
414 
415 	return 0;
416 
417 out_cleanup_queue:
418 	blk_cleanup_queue(ctrl->ctrl.admin_q);
419 out_free_tagset:
420 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
421 out_free_sq:
422 	nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
423 	return error;
424 }
425 
426 static void nvme_loop_shutdown_ctrl(struct nvme_loop_ctrl *ctrl)
427 {
428 	if (ctrl->ctrl.queue_count > 1) {
429 		nvme_stop_queues(&ctrl->ctrl);
430 		blk_mq_tagset_busy_iter(&ctrl->tag_set,
431 					nvme_cancel_request, &ctrl->ctrl);
432 		nvme_loop_destroy_io_queues(ctrl);
433 	}
434 
435 	if (ctrl->ctrl.state == NVME_CTRL_LIVE)
436 		nvme_shutdown_ctrl(&ctrl->ctrl);
437 
438 	blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
439 	blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
440 				nvme_cancel_request, &ctrl->ctrl);
441 	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
442 	nvme_loop_destroy_admin_queue(ctrl);
443 }
444 
445 static void nvme_loop_delete_ctrl_host(struct nvme_ctrl *ctrl)
446 {
447 	nvme_loop_shutdown_ctrl(to_loop_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 const 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 	.module		= THIS_MODULE,
678 	.create_ctrl	= nvme_loop_create_ctrl,
679 };
680 
681 static int __init nvme_loop_init_module(void)
682 {
683 	int ret;
684 
685 	ret = nvmet_register_transport(&nvme_loop_ops);
686 	if (ret)
687 		return ret;
688 
689 	ret = nvmf_register_transport(&nvme_loop_transport);
690 	if (ret)
691 		nvmet_unregister_transport(&nvme_loop_ops);
692 
693 	return ret;
694 }
695 
696 static void __exit nvme_loop_cleanup_module(void)
697 {
698 	struct nvme_loop_ctrl *ctrl, *next;
699 
700 	nvmf_unregister_transport(&nvme_loop_transport);
701 	nvmet_unregister_transport(&nvme_loop_ops);
702 
703 	mutex_lock(&nvme_loop_ctrl_mutex);
704 	list_for_each_entry_safe(ctrl, next, &nvme_loop_ctrl_list, list)
705 		nvme_delete_ctrl(&ctrl->ctrl);
706 	mutex_unlock(&nvme_loop_ctrl_mutex);
707 
708 	flush_workqueue(nvme_delete_wq);
709 }
710 
711 module_init(nvme_loop_init_module);
712 module_exit(nvme_loop_cleanup_module);
713 
714 MODULE_LICENSE("GPL v2");
715 MODULE_ALIAS("nvmet-transport-254"); /* 254 == NVMF_TRTYPE_LOOP */
716