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