xref: /openbmc/linux/drivers/nvme/target/loop.c (revision 6355592e)
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	cqe;
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, SG_CHUNK_SIZE);
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->cqe;
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 blk_status_t nvme_loop_queue_rq(struct blk_mq_hw_ctx *hctx,
133 		const struct blk_mq_queue_data *bd)
134 {
135 	struct nvme_ns *ns = hctx->queue->queuedata;
136 	struct nvme_loop_queue *queue = hctx->driver_data;
137 	struct request *req = bd->rq;
138 	struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req);
139 	bool queue_ready = test_bit(NVME_LOOP_Q_LIVE, &queue->flags);
140 	blk_status_t ret;
141 
142 	if (!nvmf_check_ready(&queue->ctrl->ctrl, req, queue_ready))
143 		return nvmf_fail_nonready_command(&queue->ctrl->ctrl, req);
144 
145 	ret = nvme_setup_cmd(ns, req, &iod->cmd);
146 	if (ret)
147 		return ret;
148 
149 	blk_mq_start_request(req);
150 	iod->cmd.common.flags |= NVME_CMD_SGL_METABUF;
151 	iod->req.port = queue->ctrl->port;
152 	if (!nvmet_req_init(&iod->req, &queue->nvme_cq,
153 			&queue->nvme_sq, &nvme_loop_ops))
154 		return BLK_STS_OK;
155 
156 	if (blk_rq_nr_phys_segments(req)) {
157 		iod->sg_table.sgl = iod->first_sgl;
158 		if (sg_alloc_table_chained(&iod->sg_table,
159 				blk_rq_nr_phys_segments(req),
160 				iod->sg_table.sgl, SG_CHUNK_SIZE))
161 			return BLK_STS_RESOURCE;
162 
163 		iod->req.sg = iod->sg_table.sgl;
164 		iod->req.sg_cnt = blk_rq_map_sg(req->q, req, iod->sg_table.sgl);
165 		iod->req.transfer_len = blk_rq_payload_bytes(req);
166 	}
167 
168 	schedule_work(&iod->work);
169 	return BLK_STS_OK;
170 }
171 
172 static void nvme_loop_submit_async_event(struct nvme_ctrl *arg)
173 {
174 	struct nvme_loop_ctrl *ctrl = to_loop_ctrl(arg);
175 	struct nvme_loop_queue *queue = &ctrl->queues[0];
176 	struct nvme_loop_iod *iod = &ctrl->async_event_iod;
177 
178 	memset(&iod->cmd, 0, sizeof(iod->cmd));
179 	iod->cmd.common.opcode = nvme_admin_async_event;
180 	iod->cmd.common.command_id = NVME_AQ_BLK_MQ_DEPTH;
181 	iod->cmd.common.flags |= NVME_CMD_SGL_METABUF;
182 
183 	if (!nvmet_req_init(&iod->req, &queue->nvme_cq, &queue->nvme_sq,
184 			&nvme_loop_ops)) {
185 		dev_err(ctrl->ctrl.device, "failed async event work\n");
186 		return;
187 	}
188 
189 	schedule_work(&iod->work);
190 }
191 
192 static int nvme_loop_init_iod(struct nvme_loop_ctrl *ctrl,
193 		struct nvme_loop_iod *iod, unsigned int queue_idx)
194 {
195 	iod->req.cmd = &iod->cmd;
196 	iod->req.cqe = &iod->cqe;
197 	iod->queue = &ctrl->queues[queue_idx];
198 	INIT_WORK(&iod->work, nvme_loop_execute_work);
199 	return 0;
200 }
201 
202 static int nvme_loop_init_request(struct blk_mq_tag_set *set,
203 		struct request *req, unsigned int hctx_idx,
204 		unsigned int numa_node)
205 {
206 	struct nvme_loop_ctrl *ctrl = set->driver_data;
207 
208 	nvme_req(req)->ctrl = &ctrl->ctrl;
209 	return nvme_loop_init_iod(ctrl, blk_mq_rq_to_pdu(req),
210 			(set == &ctrl->tag_set) ? hctx_idx + 1 : 0);
211 }
212 
213 static int nvme_loop_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
214 		unsigned int hctx_idx)
215 {
216 	struct nvme_loop_ctrl *ctrl = data;
217 	struct nvme_loop_queue *queue = &ctrl->queues[hctx_idx + 1];
218 
219 	BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
220 
221 	hctx->driver_data = queue;
222 	return 0;
223 }
224 
225 static int nvme_loop_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
226 		unsigned int hctx_idx)
227 {
228 	struct nvme_loop_ctrl *ctrl = data;
229 	struct nvme_loop_queue *queue = &ctrl->queues[0];
230 
231 	BUG_ON(hctx_idx != 0);
232 
233 	hctx->driver_data = queue;
234 	return 0;
235 }
236 
237 static const struct blk_mq_ops nvme_loop_mq_ops = {
238 	.queue_rq	= nvme_loop_queue_rq,
239 	.complete	= nvme_loop_complete_rq,
240 	.init_request	= nvme_loop_init_request,
241 	.init_hctx	= nvme_loop_init_hctx,
242 };
243 
244 static const struct blk_mq_ops nvme_loop_admin_mq_ops = {
245 	.queue_rq	= nvme_loop_queue_rq,
246 	.complete	= nvme_loop_complete_rq,
247 	.init_request	= nvme_loop_init_request,
248 	.init_hctx	= nvme_loop_init_admin_hctx,
249 };
250 
251 static void nvme_loop_destroy_admin_queue(struct nvme_loop_ctrl *ctrl)
252 {
253 	clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags);
254 	nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
255 	blk_cleanup_queue(ctrl->ctrl.admin_q);
256 	blk_cleanup_queue(ctrl->ctrl.fabrics_q);
257 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
258 }
259 
260 static void nvme_loop_free_ctrl(struct nvme_ctrl *nctrl)
261 {
262 	struct nvme_loop_ctrl *ctrl = to_loop_ctrl(nctrl);
263 
264 	if (list_empty(&ctrl->list))
265 		goto free_ctrl;
266 
267 	mutex_lock(&nvme_loop_ctrl_mutex);
268 	list_del(&ctrl->list);
269 	mutex_unlock(&nvme_loop_ctrl_mutex);
270 
271 	if (nctrl->tagset) {
272 		blk_cleanup_queue(ctrl->ctrl.connect_q);
273 		blk_mq_free_tag_set(&ctrl->tag_set);
274 	}
275 	kfree(ctrl->queues);
276 	nvmf_free_options(nctrl->opts);
277 free_ctrl:
278 	kfree(ctrl);
279 }
280 
281 static void nvme_loop_destroy_io_queues(struct nvme_loop_ctrl *ctrl)
282 {
283 	int i;
284 
285 	for (i = 1; i < ctrl->ctrl.queue_count; i++) {
286 		clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags);
287 		nvmet_sq_destroy(&ctrl->queues[i].nvme_sq);
288 	}
289 }
290 
291 static int nvme_loop_init_io_queues(struct nvme_loop_ctrl *ctrl)
292 {
293 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
294 	unsigned int nr_io_queues;
295 	int ret, i;
296 
297 	nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
298 	ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
299 	if (ret || !nr_io_queues)
300 		return ret;
301 
302 	dev_info(ctrl->ctrl.device, "creating %d I/O queues.\n", nr_io_queues);
303 
304 	for (i = 1; i <= nr_io_queues; i++) {
305 		ctrl->queues[i].ctrl = ctrl;
306 		ret = nvmet_sq_init(&ctrl->queues[i].nvme_sq);
307 		if (ret)
308 			goto out_destroy_queues;
309 
310 		ctrl->ctrl.queue_count++;
311 	}
312 
313 	return 0;
314 
315 out_destroy_queues:
316 	nvme_loop_destroy_io_queues(ctrl);
317 	return ret;
318 }
319 
320 static int nvme_loop_connect_io_queues(struct nvme_loop_ctrl *ctrl)
321 {
322 	int i, ret;
323 
324 	for (i = 1; i < ctrl->ctrl.queue_count; i++) {
325 		ret = nvmf_connect_io_queue(&ctrl->ctrl, i, false);
326 		if (ret)
327 			return ret;
328 		set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags);
329 	}
330 
331 	return 0;
332 }
333 
334 static int nvme_loop_configure_admin_queue(struct nvme_loop_ctrl *ctrl)
335 {
336 	int error;
337 
338 	memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
339 	ctrl->admin_tag_set.ops = &nvme_loop_admin_mq_ops;
340 	ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
341 	ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
342 	ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
343 	ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_loop_iod) +
344 		SG_CHUNK_SIZE * sizeof(struct scatterlist);
345 	ctrl->admin_tag_set.driver_data = ctrl;
346 	ctrl->admin_tag_set.nr_hw_queues = 1;
347 	ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
348 	ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
349 
350 	ctrl->queues[0].ctrl = ctrl;
351 	error = nvmet_sq_init(&ctrl->queues[0].nvme_sq);
352 	if (error)
353 		return error;
354 	ctrl->ctrl.queue_count = 1;
355 
356 	error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
357 	if (error)
358 		goto out_free_sq;
359 	ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
360 
361 	ctrl->ctrl.fabrics_q = blk_mq_init_queue(&ctrl->admin_tag_set);
362 	if (IS_ERR(ctrl->ctrl.fabrics_q)) {
363 		error = PTR_ERR(ctrl->ctrl.fabrics_q);
364 		goto out_free_tagset;
365 	}
366 
367 	ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
368 	if (IS_ERR(ctrl->ctrl.admin_q)) {
369 		error = PTR_ERR(ctrl->ctrl.admin_q);
370 		goto out_cleanup_fabrics_q;
371 	}
372 
373 	error = nvmf_connect_admin_queue(&ctrl->ctrl);
374 	if (error)
375 		goto out_cleanup_queue;
376 
377 	set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags);
378 
379 	error = nvme_enable_ctrl(&ctrl->ctrl);
380 	if (error)
381 		goto out_cleanup_queue;
382 
383 	ctrl->ctrl.max_hw_sectors =
384 		(NVME_LOOP_MAX_SEGMENTS - 1) << (PAGE_SHIFT - 9);
385 
386 	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
387 
388 	error = nvme_init_identify(&ctrl->ctrl);
389 	if (error)
390 		goto out_cleanup_queue;
391 
392 	return 0;
393 
394 out_cleanup_queue:
395 	blk_cleanup_queue(ctrl->ctrl.admin_q);
396 out_cleanup_fabrics_q:
397 	blk_cleanup_queue(ctrl->ctrl.fabrics_q);
398 out_free_tagset:
399 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
400 out_free_sq:
401 	nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
402 	return error;
403 }
404 
405 static void nvme_loop_shutdown_ctrl(struct nvme_loop_ctrl *ctrl)
406 {
407 	if (ctrl->ctrl.queue_count > 1) {
408 		nvme_stop_queues(&ctrl->ctrl);
409 		blk_mq_tagset_busy_iter(&ctrl->tag_set,
410 					nvme_cancel_request, &ctrl->ctrl);
411 		blk_mq_tagset_wait_completed_request(&ctrl->tag_set);
412 		nvme_loop_destroy_io_queues(ctrl);
413 	}
414 
415 	blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
416 	if (ctrl->ctrl.state == NVME_CTRL_LIVE)
417 		nvme_shutdown_ctrl(&ctrl->ctrl);
418 
419 	blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
420 				nvme_cancel_request, &ctrl->ctrl);
421 	blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set);
422 	nvme_loop_destroy_admin_queue(ctrl);
423 }
424 
425 static void nvme_loop_delete_ctrl_host(struct nvme_ctrl *ctrl)
426 {
427 	nvme_loop_shutdown_ctrl(to_loop_ctrl(ctrl));
428 }
429 
430 static void nvme_loop_delete_ctrl(struct nvmet_ctrl *nctrl)
431 {
432 	struct nvme_loop_ctrl *ctrl;
433 
434 	mutex_lock(&nvme_loop_ctrl_mutex);
435 	list_for_each_entry(ctrl, &nvme_loop_ctrl_list, list) {
436 		if (ctrl->ctrl.cntlid == nctrl->cntlid)
437 			nvme_delete_ctrl(&ctrl->ctrl);
438 	}
439 	mutex_unlock(&nvme_loop_ctrl_mutex);
440 }
441 
442 static void nvme_loop_reset_ctrl_work(struct work_struct *work)
443 {
444 	struct nvme_loop_ctrl *ctrl =
445 		container_of(work, struct nvme_loop_ctrl, ctrl.reset_work);
446 	bool changed;
447 	int ret;
448 
449 	nvme_stop_ctrl(&ctrl->ctrl);
450 	nvme_loop_shutdown_ctrl(ctrl);
451 
452 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
453 		/* state change failure should never happen */
454 		WARN_ON_ONCE(1);
455 		return;
456 	}
457 
458 	ret = nvme_loop_configure_admin_queue(ctrl);
459 	if (ret)
460 		goto out_disable;
461 
462 	ret = nvme_loop_init_io_queues(ctrl);
463 	if (ret)
464 		goto out_destroy_admin;
465 
466 	ret = nvme_loop_connect_io_queues(ctrl);
467 	if (ret)
468 		goto out_destroy_io;
469 
470 	blk_mq_update_nr_hw_queues(&ctrl->tag_set,
471 			ctrl->ctrl.queue_count - 1);
472 
473 	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
474 	WARN_ON_ONCE(!changed);
475 
476 	nvme_start_ctrl(&ctrl->ctrl);
477 
478 	return;
479 
480 out_destroy_io:
481 	nvme_loop_destroy_io_queues(ctrl);
482 out_destroy_admin:
483 	nvme_loop_destroy_admin_queue(ctrl);
484 out_disable:
485 	dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
486 	nvme_uninit_ctrl(&ctrl->ctrl);
487 	nvme_put_ctrl(&ctrl->ctrl);
488 }
489 
490 static const struct nvme_ctrl_ops nvme_loop_ctrl_ops = {
491 	.name			= "loop",
492 	.module			= THIS_MODULE,
493 	.flags			= NVME_F_FABRICS,
494 	.reg_read32		= nvmf_reg_read32,
495 	.reg_read64		= nvmf_reg_read64,
496 	.reg_write32		= nvmf_reg_write32,
497 	.free_ctrl		= nvme_loop_free_ctrl,
498 	.submit_async_event	= nvme_loop_submit_async_event,
499 	.delete_ctrl		= nvme_loop_delete_ctrl_host,
500 	.get_address		= nvmf_get_address,
501 };
502 
503 static int nvme_loop_create_io_queues(struct nvme_loop_ctrl *ctrl)
504 {
505 	int ret;
506 
507 	ret = nvme_loop_init_io_queues(ctrl);
508 	if (ret)
509 		return ret;
510 
511 	memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
512 	ctrl->tag_set.ops = &nvme_loop_mq_ops;
513 	ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
514 	ctrl->tag_set.reserved_tags = 1; /* fabric connect */
515 	ctrl->tag_set.numa_node = NUMA_NO_NODE;
516 	ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
517 	ctrl->tag_set.cmd_size = sizeof(struct nvme_loop_iod) +
518 		SG_CHUNK_SIZE * sizeof(struct scatterlist);
519 	ctrl->tag_set.driver_data = ctrl;
520 	ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
521 	ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
522 	ctrl->ctrl.tagset = &ctrl->tag_set;
523 
524 	ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
525 	if (ret)
526 		goto out_destroy_queues;
527 
528 	ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
529 	if (IS_ERR(ctrl->ctrl.connect_q)) {
530 		ret = PTR_ERR(ctrl->ctrl.connect_q);
531 		goto out_free_tagset;
532 	}
533 
534 	ret = nvme_loop_connect_io_queues(ctrl);
535 	if (ret)
536 		goto out_cleanup_connect_q;
537 
538 	return 0;
539 
540 out_cleanup_connect_q:
541 	blk_cleanup_queue(ctrl->ctrl.connect_q);
542 out_free_tagset:
543 	blk_mq_free_tag_set(&ctrl->tag_set);
544 out_destroy_queues:
545 	nvme_loop_destroy_io_queues(ctrl);
546 	return ret;
547 }
548 
549 static struct nvmet_port *nvme_loop_find_port(struct nvme_ctrl *ctrl)
550 {
551 	struct nvmet_port *p, *found = NULL;
552 
553 	mutex_lock(&nvme_loop_ports_mutex);
554 	list_for_each_entry(p, &nvme_loop_ports, entry) {
555 		/* if no transport address is specified use the first port */
556 		if ((ctrl->opts->mask & NVMF_OPT_TRADDR) &&
557 		    strcmp(ctrl->opts->traddr, p->disc_addr.traddr))
558 			continue;
559 		found = p;
560 		break;
561 	}
562 	mutex_unlock(&nvme_loop_ports_mutex);
563 	return found;
564 }
565 
566 static struct nvme_ctrl *nvme_loop_create_ctrl(struct device *dev,
567 		struct nvmf_ctrl_options *opts)
568 {
569 	struct nvme_loop_ctrl *ctrl;
570 	bool changed;
571 	int ret;
572 
573 	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
574 	if (!ctrl)
575 		return ERR_PTR(-ENOMEM);
576 	ctrl->ctrl.opts = opts;
577 	INIT_LIST_HEAD(&ctrl->list);
578 
579 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_loop_reset_ctrl_work);
580 
581 	ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_loop_ctrl_ops,
582 				0 /* no quirks, we're perfect! */);
583 	if (ret)
584 		goto out_put_ctrl;
585 
586 	ret = -ENOMEM;
587 
588 	ctrl->ctrl.sqsize = opts->queue_size - 1;
589 	ctrl->ctrl.kato = opts->kato;
590 	ctrl->port = nvme_loop_find_port(&ctrl->ctrl);
591 
592 	ctrl->queues = kcalloc(opts->nr_io_queues + 1, sizeof(*ctrl->queues),
593 			GFP_KERNEL);
594 	if (!ctrl->queues)
595 		goto out_uninit_ctrl;
596 
597 	ret = nvme_loop_configure_admin_queue(ctrl);
598 	if (ret)
599 		goto out_free_queues;
600 
601 	if (opts->queue_size > ctrl->ctrl.maxcmd) {
602 		/* warn if maxcmd is lower than queue_size */
603 		dev_warn(ctrl->ctrl.device,
604 			"queue_size %zu > ctrl maxcmd %u, clamping down\n",
605 			opts->queue_size, ctrl->ctrl.maxcmd);
606 		opts->queue_size = ctrl->ctrl.maxcmd;
607 	}
608 
609 	if (opts->nr_io_queues) {
610 		ret = nvme_loop_create_io_queues(ctrl);
611 		if (ret)
612 			goto out_remove_admin_queue;
613 	}
614 
615 	nvme_loop_init_iod(ctrl, &ctrl->async_event_iod, 0);
616 
617 	dev_info(ctrl->ctrl.device,
618 		 "new ctrl: \"%s\"\n", ctrl->ctrl.opts->subsysnqn);
619 
620 	nvme_get_ctrl(&ctrl->ctrl);
621 
622 	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
623 	WARN_ON_ONCE(!changed);
624 
625 	mutex_lock(&nvme_loop_ctrl_mutex);
626 	list_add_tail(&ctrl->list, &nvme_loop_ctrl_list);
627 	mutex_unlock(&nvme_loop_ctrl_mutex);
628 
629 	nvme_start_ctrl(&ctrl->ctrl);
630 
631 	return &ctrl->ctrl;
632 
633 out_remove_admin_queue:
634 	nvme_loop_destroy_admin_queue(ctrl);
635 out_free_queues:
636 	kfree(ctrl->queues);
637 out_uninit_ctrl:
638 	nvme_uninit_ctrl(&ctrl->ctrl);
639 out_put_ctrl:
640 	nvme_put_ctrl(&ctrl->ctrl);
641 	if (ret > 0)
642 		ret = -EIO;
643 	return ERR_PTR(ret);
644 }
645 
646 static int nvme_loop_add_port(struct nvmet_port *port)
647 {
648 	mutex_lock(&nvme_loop_ports_mutex);
649 	list_add_tail(&port->entry, &nvme_loop_ports);
650 	mutex_unlock(&nvme_loop_ports_mutex);
651 	return 0;
652 }
653 
654 static void nvme_loop_remove_port(struct nvmet_port *port)
655 {
656 	mutex_lock(&nvme_loop_ports_mutex);
657 	list_del_init(&port->entry);
658 	mutex_unlock(&nvme_loop_ports_mutex);
659 
660 	/*
661 	 * Ensure any ctrls that are in the process of being
662 	 * deleted are in fact deleted before we return
663 	 * and free the port. This is to prevent active
664 	 * ctrls from using a port after it's freed.
665 	 */
666 	flush_workqueue(nvme_delete_wq);
667 }
668 
669 static const struct nvmet_fabrics_ops nvme_loop_ops = {
670 	.owner		= THIS_MODULE,
671 	.type		= NVMF_TRTYPE_LOOP,
672 	.add_port	= nvme_loop_add_port,
673 	.remove_port	= nvme_loop_remove_port,
674 	.queue_response = nvme_loop_queue_response,
675 	.delete_ctrl	= nvme_loop_delete_ctrl,
676 };
677 
678 static struct nvmf_transport_ops nvme_loop_transport = {
679 	.name		= "loop",
680 	.module		= THIS_MODULE,
681 	.create_ctrl	= nvme_loop_create_ctrl,
682 	.allowed_opts	= NVMF_OPT_TRADDR,
683 };
684 
685 static int __init nvme_loop_init_module(void)
686 {
687 	int ret;
688 
689 	ret = nvmet_register_transport(&nvme_loop_ops);
690 	if (ret)
691 		return ret;
692 
693 	ret = nvmf_register_transport(&nvme_loop_transport);
694 	if (ret)
695 		nvmet_unregister_transport(&nvme_loop_ops);
696 
697 	return ret;
698 }
699 
700 static void __exit nvme_loop_cleanup_module(void)
701 {
702 	struct nvme_loop_ctrl *ctrl, *next;
703 
704 	nvmf_unregister_transport(&nvme_loop_transport);
705 	nvmet_unregister_transport(&nvme_loop_ops);
706 
707 	mutex_lock(&nvme_loop_ctrl_mutex);
708 	list_for_each_entry_safe(ctrl, next, &nvme_loop_ctrl_list, list)
709 		nvme_delete_ctrl(&ctrl->ctrl);
710 	mutex_unlock(&nvme_loop_ctrl_mutex);
711 
712 	flush_workqueue(nvme_delete_wq);
713 }
714 
715 module_init(nvme_loop_init_module);
716 module_exit(nvme_loop_cleanup_module);
717 
718 MODULE_LICENSE("GPL v2");
719 MODULE_ALIAS("nvmet-transport-254"); /* 254 == NVMF_TRTYPE_LOOP */
720