xref: /openbmc/linux/drivers/nvme/target/loop.c (revision ae40e94f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NVMe over Fabrics loopback device.
4  * Copyright (c) 2015-2016 HGST, a Western Digital Company.
5  */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/scatterlist.h>
8 #include <linux/blk-mq.h>
9 #include <linux/nvme.h>
10 #include <linux/module.h>
11 #include <linux/parser.h>
12 #include "nvmet.h"
13 #include "../host/nvme.h"
14 #include "../host/fabrics.h"
15 
16 #define NVME_LOOP_MAX_SEGMENTS		256
17 
18 struct nvme_loop_iod {
19 	struct nvme_request	nvme_req;
20 	struct nvme_command	cmd;
21 	struct nvme_completion	rsp;
22 	struct nvmet_req	req;
23 	struct nvme_loop_queue	*queue;
24 	struct work_struct	work;
25 	struct sg_table		sg_table;
26 	struct scatterlist	first_sgl[];
27 };
28 
29 struct nvme_loop_ctrl {
30 	struct nvme_loop_queue	*queues;
31 
32 	struct blk_mq_tag_set	admin_tag_set;
33 
34 	struct list_head	list;
35 	struct blk_mq_tag_set	tag_set;
36 	struct nvme_loop_iod	async_event_iod;
37 	struct nvme_ctrl	ctrl;
38 
39 	struct nvmet_ctrl	*target_ctrl;
40 	struct nvmet_port	*port;
41 };
42 
43 static inline struct nvme_loop_ctrl *to_loop_ctrl(struct nvme_ctrl *ctrl)
44 {
45 	return container_of(ctrl, struct nvme_loop_ctrl, ctrl);
46 }
47 
48 enum nvme_loop_queue_flags {
49 	NVME_LOOP_Q_LIVE	= 0,
50 };
51 
52 struct nvme_loop_queue {
53 	struct nvmet_cq		nvme_cq;
54 	struct nvmet_sq		nvme_sq;
55 	struct nvme_loop_ctrl	*ctrl;
56 	unsigned long		flags;
57 };
58 
59 static LIST_HEAD(nvme_loop_ports);
60 static DEFINE_MUTEX(nvme_loop_ports_mutex);
61 
62 static LIST_HEAD(nvme_loop_ctrl_list);
63 static DEFINE_MUTEX(nvme_loop_ctrl_mutex);
64 
65 static void nvme_loop_queue_response(struct nvmet_req *nvme_req);
66 static void nvme_loop_delete_ctrl(struct nvmet_ctrl *ctrl);
67 
68 static const struct nvmet_fabrics_ops nvme_loop_ops;
69 
70 static inline int nvme_loop_queue_idx(struct nvme_loop_queue *queue)
71 {
72 	return queue - queue->ctrl->queues;
73 }
74 
75 static void nvme_loop_complete_rq(struct request *req)
76 {
77 	struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req);
78 
79 	nvme_cleanup_cmd(req);
80 	sg_free_table_chained(&iod->sg_table, true);
81 	nvme_complete_rq(req);
82 }
83 
84 static struct blk_mq_tags *nvme_loop_tagset(struct nvme_loop_queue *queue)
85 {
86 	u32 queue_idx = nvme_loop_queue_idx(queue);
87 
88 	if (queue_idx == 0)
89 		return queue->ctrl->admin_tag_set.tags[queue_idx];
90 	return queue->ctrl->tag_set.tags[queue_idx - 1];
91 }
92 
93 static void nvme_loop_queue_response(struct nvmet_req *req)
94 {
95 	struct nvme_loop_queue *queue =
96 		container_of(req->sq, struct nvme_loop_queue, nvme_sq);
97 	struct nvme_completion *cqe = req->rsp;
98 
99 	/*
100 	 * AEN requests are special as they don't time out and can
101 	 * survive any kind of queue freeze and often don't respond to
102 	 * aborts.  We don't even bother to allocate a struct request
103 	 * for them but rather special case them here.
104 	 */
105 	if (unlikely(nvme_loop_queue_idx(queue) == 0 &&
106 			cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH)) {
107 		nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
108 				&cqe->result);
109 	} else {
110 		struct request *rq;
111 
112 		rq = blk_mq_tag_to_rq(nvme_loop_tagset(queue), cqe->command_id);
113 		if (!rq) {
114 			dev_err(queue->ctrl->ctrl.device,
115 				"tag 0x%x on queue %d not found\n",
116 				cqe->command_id, nvme_loop_queue_idx(queue));
117 			return;
118 		}
119 
120 		nvme_end_request(rq, cqe->status, cqe->result);
121 	}
122 }
123 
124 static void nvme_loop_execute_work(struct work_struct *work)
125 {
126 	struct nvme_loop_iod *iod =
127 		container_of(work, struct nvme_loop_iod, work);
128 
129 	nvmet_req_execute(&iod->req);
130 }
131 
132 static enum blk_eh_timer_return
133 nvme_loop_timeout(struct request *rq, bool reserved)
134 {
135 	struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(rq);
136 
137 	/* queue error recovery */
138 	nvme_reset_ctrl(&iod->queue->ctrl->ctrl);
139 
140 	/* fail with DNR on admin cmd timeout */
141 	nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
142 
143 	return BLK_EH_DONE;
144 }
145 
146 static blk_status_t nvme_loop_queue_rq(struct blk_mq_hw_ctx *hctx,
147 		const struct blk_mq_queue_data *bd)
148 {
149 	struct nvme_ns *ns = hctx->queue->queuedata;
150 	struct nvme_loop_queue *queue = hctx->driver_data;
151 	struct request *req = bd->rq;
152 	struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req);
153 	bool queue_ready = test_bit(NVME_LOOP_Q_LIVE, &queue->flags);
154 	blk_status_t ret;
155 
156 	if (!nvmf_check_ready(&queue->ctrl->ctrl, req, queue_ready))
157 		return nvmf_fail_nonready_command(&queue->ctrl->ctrl, req);
158 
159 	ret = nvme_setup_cmd(ns, req, &iod->cmd);
160 	if (ret)
161 		return ret;
162 
163 	blk_mq_start_request(req);
164 	iod->cmd.common.flags |= NVME_CMD_SGL_METABUF;
165 	iod->req.port = queue->ctrl->port;
166 	if (!nvmet_req_init(&iod->req, &queue->nvme_cq,
167 			&queue->nvme_sq, &nvme_loop_ops))
168 		return BLK_STS_OK;
169 
170 	if (blk_rq_nr_phys_segments(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_payload_bytes(req);
180 	}
181 
182 	schedule_work(&iod->work);
183 	return BLK_STS_OK;
184 }
185 
186 static void nvme_loop_submit_async_event(struct nvme_ctrl *arg)
187 {
188 	struct nvme_loop_ctrl *ctrl = to_loop_ctrl(arg);
189 	struct nvme_loop_queue *queue = &ctrl->queues[0];
190 	struct nvme_loop_iod *iod = &ctrl->async_event_iod;
191 
192 	memset(&iod->cmd, 0, sizeof(iod->cmd));
193 	iod->cmd.common.opcode = nvme_admin_async_event;
194 	iod->cmd.common.command_id = NVME_AQ_BLK_MQ_DEPTH;
195 	iod->cmd.common.flags |= NVME_CMD_SGL_METABUF;
196 
197 	if (!nvmet_req_init(&iod->req, &queue->nvme_cq, &queue->nvme_sq,
198 			&nvme_loop_ops)) {
199 		dev_err(ctrl->ctrl.device, "failed async event work\n");
200 		return;
201 	}
202 
203 	schedule_work(&iod->work);
204 }
205 
206 static int nvme_loop_init_iod(struct nvme_loop_ctrl *ctrl,
207 		struct nvme_loop_iod *iod, unsigned int queue_idx)
208 {
209 	iod->req.cmd = &iod->cmd;
210 	iod->req.rsp = &iod->rsp;
211 	iod->queue = &ctrl->queues[queue_idx];
212 	INIT_WORK(&iod->work, nvme_loop_execute_work);
213 	return 0;
214 }
215 
216 static int nvme_loop_init_request(struct blk_mq_tag_set *set,
217 		struct request *req, unsigned int hctx_idx,
218 		unsigned int numa_node)
219 {
220 	struct nvme_loop_ctrl *ctrl = set->driver_data;
221 
222 	nvme_req(req)->ctrl = &ctrl->ctrl;
223 	return nvme_loop_init_iod(ctrl, blk_mq_rq_to_pdu(req),
224 			(set == &ctrl->tag_set) ? hctx_idx + 1 : 0);
225 }
226 
227 static int nvme_loop_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
228 		unsigned int hctx_idx)
229 {
230 	struct nvme_loop_ctrl *ctrl = data;
231 	struct nvme_loop_queue *queue = &ctrl->queues[hctx_idx + 1];
232 
233 	BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
234 
235 	hctx->driver_data = queue;
236 	return 0;
237 }
238 
239 static int nvme_loop_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
240 		unsigned int hctx_idx)
241 {
242 	struct nvme_loop_ctrl *ctrl = data;
243 	struct nvme_loop_queue *queue = &ctrl->queues[0];
244 
245 	BUG_ON(hctx_idx != 0);
246 
247 	hctx->driver_data = queue;
248 	return 0;
249 }
250 
251 static const struct blk_mq_ops nvme_loop_mq_ops = {
252 	.queue_rq	= nvme_loop_queue_rq,
253 	.complete	= nvme_loop_complete_rq,
254 	.init_request	= nvme_loop_init_request,
255 	.init_hctx	= nvme_loop_init_hctx,
256 	.timeout	= nvme_loop_timeout,
257 };
258 
259 static const struct blk_mq_ops nvme_loop_admin_mq_ops = {
260 	.queue_rq	= nvme_loop_queue_rq,
261 	.complete	= nvme_loop_complete_rq,
262 	.init_request	= nvme_loop_init_request,
263 	.init_hctx	= nvme_loop_init_admin_hctx,
264 	.timeout	= nvme_loop_timeout,
265 };
266 
267 static void nvme_loop_destroy_admin_queue(struct nvme_loop_ctrl *ctrl)
268 {
269 	clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags);
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 		clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags);
302 		nvmet_sq_destroy(&ctrl->queues[i].nvme_sq);
303 	}
304 }
305 
306 static int nvme_loop_init_io_queues(struct nvme_loop_ctrl *ctrl)
307 {
308 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
309 	unsigned int nr_io_queues;
310 	int ret, i;
311 
312 	nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
313 	ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
314 	if (ret || !nr_io_queues)
315 		return ret;
316 
317 	dev_info(ctrl->ctrl.device, "creating %d I/O queues.\n", nr_io_queues);
318 
319 	for (i = 1; i <= nr_io_queues; i++) {
320 		ctrl->queues[i].ctrl = ctrl;
321 		ret = nvmet_sq_init(&ctrl->queues[i].nvme_sq);
322 		if (ret)
323 			goto out_destroy_queues;
324 
325 		ctrl->ctrl.queue_count++;
326 	}
327 
328 	return 0;
329 
330 out_destroy_queues:
331 	nvme_loop_destroy_io_queues(ctrl);
332 	return ret;
333 }
334 
335 static int nvme_loop_connect_io_queues(struct nvme_loop_ctrl *ctrl)
336 {
337 	int i, ret;
338 
339 	for (i = 1; i < ctrl->ctrl.queue_count; i++) {
340 		ret = nvmf_connect_io_queue(&ctrl->ctrl, i, false);
341 		if (ret)
342 			return ret;
343 		set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags);
344 	}
345 
346 	return 0;
347 }
348 
349 static int nvme_loop_configure_admin_queue(struct nvme_loop_ctrl *ctrl)
350 {
351 	int error;
352 
353 	memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
354 	ctrl->admin_tag_set.ops = &nvme_loop_admin_mq_ops;
355 	ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
356 	ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
357 	ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
358 	ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_loop_iod) +
359 		SG_CHUNK_SIZE * sizeof(struct scatterlist);
360 	ctrl->admin_tag_set.driver_data = ctrl;
361 	ctrl->admin_tag_set.nr_hw_queues = 1;
362 	ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
363 	ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
364 
365 	ctrl->queues[0].ctrl = ctrl;
366 	error = nvmet_sq_init(&ctrl->queues[0].nvme_sq);
367 	if (error)
368 		return error;
369 	ctrl->ctrl.queue_count = 1;
370 
371 	error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
372 	if (error)
373 		goto out_free_sq;
374 	ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
375 
376 	ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
377 	if (IS_ERR(ctrl->ctrl.admin_q)) {
378 		error = PTR_ERR(ctrl->ctrl.admin_q);
379 		goto out_free_tagset;
380 	}
381 
382 	error = nvmf_connect_admin_queue(&ctrl->ctrl);
383 	if (error)
384 		goto out_cleanup_queue;
385 
386 	set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags);
387 
388 	error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->ctrl.cap);
389 	if (error) {
390 		dev_err(ctrl->ctrl.device,
391 			"prop_get NVME_REG_CAP failed\n");
392 		goto out_cleanup_queue;
393 	}
394 
395 	ctrl->ctrl.sqsize =
396 		min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
397 
398 	error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
399 	if (error)
400 		goto out_cleanup_queue;
401 
402 	ctrl->ctrl.max_hw_sectors =
403 		(NVME_LOOP_MAX_SEGMENTS - 1) << (PAGE_SHIFT - 9);
404 
405 	error = nvme_init_identify(&ctrl->ctrl);
406 	if (error)
407 		goto out_cleanup_queue;
408 
409 	return 0;
410 
411 out_cleanup_queue:
412 	blk_cleanup_queue(ctrl->ctrl.admin_q);
413 out_free_tagset:
414 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
415 out_free_sq:
416 	nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
417 	return error;
418 }
419 
420 static void nvme_loop_shutdown_ctrl(struct nvme_loop_ctrl *ctrl)
421 {
422 	if (ctrl->ctrl.queue_count > 1) {
423 		nvme_stop_queues(&ctrl->ctrl);
424 		blk_mq_tagset_busy_iter(&ctrl->tag_set,
425 					nvme_cancel_request, &ctrl->ctrl);
426 		nvme_loop_destroy_io_queues(ctrl);
427 	}
428 
429 	if (ctrl->ctrl.state == NVME_CTRL_LIVE)
430 		nvme_shutdown_ctrl(&ctrl->ctrl);
431 
432 	blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
433 	blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
434 				nvme_cancel_request, &ctrl->ctrl);
435 	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
436 	nvme_loop_destroy_admin_queue(ctrl);
437 }
438 
439 static void nvme_loop_delete_ctrl_host(struct nvme_ctrl *ctrl)
440 {
441 	nvme_loop_shutdown_ctrl(to_loop_ctrl(ctrl));
442 }
443 
444 static void nvme_loop_delete_ctrl(struct nvmet_ctrl *nctrl)
445 {
446 	struct nvme_loop_ctrl *ctrl;
447 
448 	mutex_lock(&nvme_loop_ctrl_mutex);
449 	list_for_each_entry(ctrl, &nvme_loop_ctrl_list, list) {
450 		if (ctrl->ctrl.cntlid == nctrl->cntlid)
451 			nvme_delete_ctrl(&ctrl->ctrl);
452 	}
453 	mutex_unlock(&nvme_loop_ctrl_mutex);
454 }
455 
456 static void nvme_loop_reset_ctrl_work(struct work_struct *work)
457 {
458 	struct nvme_loop_ctrl *ctrl =
459 		container_of(work, struct nvme_loop_ctrl, ctrl.reset_work);
460 	bool changed;
461 	int ret;
462 
463 	nvme_stop_ctrl(&ctrl->ctrl);
464 	nvme_loop_shutdown_ctrl(ctrl);
465 
466 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
467 		/* state change failure should never happen */
468 		WARN_ON_ONCE(1);
469 		return;
470 	}
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 	.get_address		= nvmf_get_address,
515 };
516 
517 static int nvme_loop_create_io_queues(struct nvme_loop_ctrl *ctrl)
518 {
519 	int ret;
520 
521 	ret = nvme_loop_init_io_queues(ctrl);
522 	if (ret)
523 		return ret;
524 
525 	memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
526 	ctrl->tag_set.ops = &nvme_loop_mq_ops;
527 	ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
528 	ctrl->tag_set.reserved_tags = 1; /* fabric connect */
529 	ctrl->tag_set.numa_node = NUMA_NO_NODE;
530 	ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
531 	ctrl->tag_set.cmd_size = sizeof(struct nvme_loop_iod) +
532 		SG_CHUNK_SIZE * sizeof(struct scatterlist);
533 	ctrl->tag_set.driver_data = ctrl;
534 	ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
535 	ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
536 	ctrl->ctrl.tagset = &ctrl->tag_set;
537 
538 	ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
539 	if (ret)
540 		goto out_destroy_queues;
541 
542 	ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
543 	if (IS_ERR(ctrl->ctrl.connect_q)) {
544 		ret = PTR_ERR(ctrl->ctrl.connect_q);
545 		goto out_free_tagset;
546 	}
547 
548 	ret = nvme_loop_connect_io_queues(ctrl);
549 	if (ret)
550 		goto out_cleanup_connect_q;
551 
552 	return 0;
553 
554 out_cleanup_connect_q:
555 	blk_cleanup_queue(ctrl->ctrl.connect_q);
556 out_free_tagset:
557 	blk_mq_free_tag_set(&ctrl->tag_set);
558 out_destroy_queues:
559 	nvme_loop_destroy_io_queues(ctrl);
560 	return ret;
561 }
562 
563 static struct nvmet_port *nvme_loop_find_port(struct nvme_ctrl *ctrl)
564 {
565 	struct nvmet_port *p, *found = NULL;
566 
567 	mutex_lock(&nvme_loop_ports_mutex);
568 	list_for_each_entry(p, &nvme_loop_ports, entry) {
569 		/* if no transport address is specified use the first port */
570 		if ((ctrl->opts->mask & NVMF_OPT_TRADDR) &&
571 		    strcmp(ctrl->opts->traddr, p->disc_addr.traddr))
572 			continue;
573 		found = p;
574 		break;
575 	}
576 	mutex_unlock(&nvme_loop_ports_mutex);
577 	return found;
578 }
579 
580 static struct nvme_ctrl *nvme_loop_create_ctrl(struct device *dev,
581 		struct nvmf_ctrl_options *opts)
582 {
583 	struct nvme_loop_ctrl *ctrl;
584 	bool changed;
585 	int ret;
586 
587 	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
588 	if (!ctrl)
589 		return ERR_PTR(-ENOMEM);
590 	ctrl->ctrl.opts = opts;
591 	INIT_LIST_HEAD(&ctrl->list);
592 
593 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_loop_reset_ctrl_work);
594 
595 	ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_loop_ctrl_ops,
596 				0 /* no quirks, we're perfect! */);
597 	if (ret)
598 		goto out_put_ctrl;
599 
600 	ret = -ENOMEM;
601 
602 	ctrl->ctrl.sqsize = opts->queue_size - 1;
603 	ctrl->ctrl.kato = opts->kato;
604 	ctrl->port = nvme_loop_find_port(&ctrl->ctrl);
605 
606 	ctrl->queues = kcalloc(opts->nr_io_queues + 1, sizeof(*ctrl->queues),
607 			GFP_KERNEL);
608 	if (!ctrl->queues)
609 		goto out_uninit_ctrl;
610 
611 	ret = nvme_loop_configure_admin_queue(ctrl);
612 	if (ret)
613 		goto out_free_queues;
614 
615 	if (opts->queue_size > ctrl->ctrl.maxcmd) {
616 		/* warn if maxcmd is lower than queue_size */
617 		dev_warn(ctrl->ctrl.device,
618 			"queue_size %zu > ctrl maxcmd %u, clamping down\n",
619 			opts->queue_size, ctrl->ctrl.maxcmd);
620 		opts->queue_size = ctrl->ctrl.maxcmd;
621 	}
622 
623 	if (opts->nr_io_queues) {
624 		ret = nvme_loop_create_io_queues(ctrl);
625 		if (ret)
626 			goto out_remove_admin_queue;
627 	}
628 
629 	nvme_loop_init_iod(ctrl, &ctrl->async_event_iod, 0);
630 
631 	dev_info(ctrl->ctrl.device,
632 		 "new ctrl: \"%s\"\n", ctrl->ctrl.opts->subsysnqn);
633 
634 	nvme_get_ctrl(&ctrl->ctrl);
635 
636 	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
637 	WARN_ON_ONCE(!changed);
638 
639 	mutex_lock(&nvme_loop_ctrl_mutex);
640 	list_add_tail(&ctrl->list, &nvme_loop_ctrl_list);
641 	mutex_unlock(&nvme_loop_ctrl_mutex);
642 
643 	nvme_start_ctrl(&ctrl->ctrl);
644 
645 	return &ctrl->ctrl;
646 
647 out_remove_admin_queue:
648 	nvme_loop_destroy_admin_queue(ctrl);
649 out_free_queues:
650 	kfree(ctrl->queues);
651 out_uninit_ctrl:
652 	nvme_uninit_ctrl(&ctrl->ctrl);
653 out_put_ctrl:
654 	nvme_put_ctrl(&ctrl->ctrl);
655 	if (ret > 0)
656 		ret = -EIO;
657 	return ERR_PTR(ret);
658 }
659 
660 static int nvme_loop_add_port(struct nvmet_port *port)
661 {
662 	mutex_lock(&nvme_loop_ports_mutex);
663 	list_add_tail(&port->entry, &nvme_loop_ports);
664 	mutex_unlock(&nvme_loop_ports_mutex);
665 	return 0;
666 }
667 
668 static void nvme_loop_remove_port(struct nvmet_port *port)
669 {
670 	mutex_lock(&nvme_loop_ports_mutex);
671 	list_del_init(&port->entry);
672 	mutex_unlock(&nvme_loop_ports_mutex);
673 }
674 
675 static const struct nvmet_fabrics_ops nvme_loop_ops = {
676 	.owner		= THIS_MODULE,
677 	.type		= NVMF_TRTYPE_LOOP,
678 	.add_port	= nvme_loop_add_port,
679 	.remove_port	= nvme_loop_remove_port,
680 	.queue_response = nvme_loop_queue_response,
681 	.delete_ctrl	= nvme_loop_delete_ctrl,
682 };
683 
684 static struct nvmf_transport_ops nvme_loop_transport = {
685 	.name		= "loop",
686 	.module		= THIS_MODULE,
687 	.create_ctrl	= nvme_loop_create_ctrl,
688 	.allowed_opts	= NVMF_OPT_TRADDR,
689 };
690 
691 static int __init nvme_loop_init_module(void)
692 {
693 	int ret;
694 
695 	ret = nvmet_register_transport(&nvme_loop_ops);
696 	if (ret)
697 		return ret;
698 
699 	ret = nvmf_register_transport(&nvme_loop_transport);
700 	if (ret)
701 		nvmet_unregister_transport(&nvme_loop_ops);
702 
703 	return ret;
704 }
705 
706 static void __exit nvme_loop_cleanup_module(void)
707 {
708 	struct nvme_loop_ctrl *ctrl, *next;
709 
710 	nvmf_unregister_transport(&nvme_loop_transport);
711 	nvmet_unregister_transport(&nvme_loop_ops);
712 
713 	mutex_lock(&nvme_loop_ctrl_mutex);
714 	list_for_each_entry_safe(ctrl, next, &nvme_loop_ctrl_list, list)
715 		nvme_delete_ctrl(&ctrl->ctrl);
716 	mutex_unlock(&nvme_loop_ctrl_mutex);
717 
718 	flush_workqueue(nvme_delete_wq);
719 }
720 
721 module_init(nvme_loop_init_module);
722 module_exit(nvme_loop_cleanup_module);
723 
724 MODULE_LICENSE("GPL v2");
725 MODULE_ALIAS("nvmet-transport-254"); /* 254 == NVMF_TRTYPE_LOOP */
726