xref: /openbmc/linux/drivers/nvme/target/loop.c (revision d22524a4782a943bb02a9cf6885ac470210aabfc)
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 
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 
59 static 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 
64 struct nvme_loop_queue {
65 	struct nvmet_cq		nvme_cq;
66 	struct nvmet_sq		nvme_sq;
67 	struct nvme_loop_ctrl	*ctrl;
68 };
69 
70 static struct nvmet_port *nvmet_loop_port;
71 
72 static LIST_HEAD(nvme_loop_ctrl_list);
73 static DEFINE_MUTEX(nvme_loop_ctrl_mutex);
74 
75 static void nvme_loop_queue_response(struct nvmet_req *nvme_req);
76 static void nvme_loop_delete_ctrl(struct nvmet_ctrl *ctrl);
77 
78 static struct nvmet_fabrics_ops nvme_loop_ops;
79 
80 static inline int nvme_loop_queue_idx(struct nvme_loop_queue *queue)
81 {
82 	return queue - queue->ctrl->queues;
83 }
84 
85 static void nvme_loop_complete_rq(struct request *req)
86 {
87 	struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req);
88 
89 	nvme_cleanup_cmd(req);
90 	sg_free_table_chained(&iod->sg_table, true);
91 	nvme_complete_rq(req);
92 }
93 
94 static struct blk_mq_tags *nvme_loop_tagset(struct nvme_loop_queue *queue)
95 {
96 	u32 queue_idx = nvme_loop_queue_idx(queue);
97 
98 	if (queue_idx == 0)
99 		return queue->ctrl->admin_tag_set.tags[queue_idx];
100 	return queue->ctrl->tag_set.tags[queue_idx - 1];
101 }
102 
103 static void nvme_loop_queue_response(struct nvmet_req *req)
104 {
105 	struct nvme_loop_queue *queue =
106 		container_of(req->sq, struct nvme_loop_queue, nvme_sq);
107 	struct nvme_completion *cqe = req->rsp;
108 
109 	/*
110 	 * AEN requests are special as they don't time out and can
111 	 * survive any kind of queue freeze and often don't respond to
112 	 * aborts.  We don't even bother to allocate a struct request
113 	 * for them but rather special case them here.
114 	 */
115 	if (unlikely(nvme_loop_queue_idx(queue) == 0 &&
116 			cqe->command_id >= NVME_LOOP_AQ_BLKMQ_DEPTH)) {
117 		nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
118 				&cqe->result);
119 	} else {
120 		struct request *rq;
121 
122 		rq = blk_mq_tag_to_rq(nvme_loop_tagset(queue), cqe->command_id);
123 		if (!rq) {
124 			dev_err(queue->ctrl->ctrl.device,
125 				"tag 0x%x on queue %d not found\n",
126 				cqe->command_id, nvme_loop_queue_idx(queue));
127 			return;
128 		}
129 
130 		nvme_end_request(rq, cqe->status, cqe->result);
131 	}
132 }
133 
134 static void nvme_loop_execute_work(struct work_struct *work)
135 {
136 	struct nvme_loop_iod *iod =
137 		container_of(work, struct nvme_loop_iod, work);
138 
139 	iod->req.execute(&iod->req);
140 }
141 
142 static enum blk_eh_timer_return
143 nvme_loop_timeout(struct request *rq, bool reserved)
144 {
145 	struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(rq);
146 
147 	/* queue error recovery */
148 	nvme_reset_ctrl(&iod->queue->ctrl->ctrl);
149 
150 	/* fail with DNR on admin cmd timeout */
151 	nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
152 
153 	return BLK_EH_HANDLED;
154 }
155 
156 static blk_status_t nvme_loop_queue_rq(struct blk_mq_hw_ctx *hctx,
157 		const struct blk_mq_queue_data *bd)
158 {
159 	struct nvme_ns *ns = hctx->queue->queuedata;
160 	struct nvme_loop_queue *queue = hctx->driver_data;
161 	struct request *req = bd->rq;
162 	struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req);
163 	blk_status_t ret;
164 
165 	ret = nvme_setup_cmd(ns, req, &iod->cmd);
166 	if (ret)
167 		return ret;
168 
169 	iod->cmd.common.flags |= NVME_CMD_SGL_METABUF;
170 	iod->req.port = nvmet_loop_port;
171 	if (!nvmet_req_init(&iod->req, &queue->nvme_cq,
172 			&queue->nvme_sq, &nvme_loop_ops)) {
173 		nvme_cleanup_cmd(req);
174 		blk_mq_start_request(req);
175 		nvme_loop_queue_response(&iod->req);
176 		return BLK_STS_OK;
177 	}
178 
179 	if (blk_rq_bytes(req)) {
180 		iod->sg_table.sgl = iod->first_sgl;
181 		if (sg_alloc_table_chained(&iod->sg_table,
182 				blk_rq_nr_phys_segments(req),
183 				iod->sg_table.sgl))
184 			return BLK_STS_RESOURCE;
185 
186 		iod->req.sg = iod->sg_table.sgl;
187 		iod->req.sg_cnt = blk_rq_map_sg(req->q, req, iod->sg_table.sgl);
188 	}
189 
190 	blk_mq_start_request(req);
191 
192 	schedule_work(&iod->work);
193 	return BLK_STS_OK;
194 }
195 
196 static void nvme_loop_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
197 {
198 	struct nvme_loop_ctrl *ctrl = to_loop_ctrl(arg);
199 	struct nvme_loop_queue *queue = &ctrl->queues[0];
200 	struct nvme_loop_iod *iod = &ctrl->async_event_iod;
201 
202 	memset(&iod->cmd, 0, sizeof(iod->cmd));
203 	iod->cmd.common.opcode = nvme_admin_async_event;
204 	iod->cmd.common.command_id = NVME_LOOP_AQ_BLKMQ_DEPTH;
205 	iod->cmd.common.flags |= NVME_CMD_SGL_METABUF;
206 
207 	if (!nvmet_req_init(&iod->req, &queue->nvme_cq, &queue->nvme_sq,
208 			&nvme_loop_ops)) {
209 		dev_err(ctrl->ctrl.device, "failed async event work\n");
210 		return;
211 	}
212 
213 	schedule_work(&iod->work);
214 }
215 
216 static int nvme_loop_init_iod(struct nvme_loop_ctrl *ctrl,
217 		struct nvme_loop_iod *iod, unsigned int queue_idx)
218 {
219 	iod->req.cmd = &iod->cmd;
220 	iod->req.rsp = &iod->rsp;
221 	iod->queue = &ctrl->queues[queue_idx];
222 	INIT_WORK(&iod->work, nvme_loop_execute_work);
223 	return 0;
224 }
225 
226 static int nvme_loop_init_request(struct blk_mq_tag_set *set,
227 		struct request *req, unsigned int hctx_idx,
228 		unsigned int numa_node)
229 {
230 	struct nvme_loop_ctrl *ctrl = set->driver_data;
231 
232 	return nvme_loop_init_iod(ctrl, blk_mq_rq_to_pdu(req),
233 			(set == &ctrl->tag_set) ? hctx_idx + 1 : 0);
234 }
235 
236 static int nvme_loop_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
237 		unsigned int hctx_idx)
238 {
239 	struct nvme_loop_ctrl *ctrl = data;
240 	struct nvme_loop_queue *queue = &ctrl->queues[hctx_idx + 1];
241 
242 	BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
243 
244 	hctx->driver_data = queue;
245 	return 0;
246 }
247 
248 static int nvme_loop_init_admin_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[0];
253 
254 	BUG_ON(hctx_idx != 0);
255 
256 	hctx->driver_data = queue;
257 	return 0;
258 }
259 
260 static const struct blk_mq_ops nvme_loop_mq_ops = {
261 	.queue_rq	= nvme_loop_queue_rq,
262 	.complete	= nvme_loop_complete_rq,
263 	.init_request	= nvme_loop_init_request,
264 	.init_hctx	= nvme_loop_init_hctx,
265 	.timeout	= nvme_loop_timeout,
266 };
267 
268 static const struct blk_mq_ops nvme_loop_admin_mq_ops = {
269 	.queue_rq	= nvme_loop_queue_rq,
270 	.complete	= nvme_loop_complete_rq,
271 	.init_request	= nvme_loop_init_request,
272 	.init_hctx	= nvme_loop_init_admin_hctx,
273 	.timeout	= nvme_loop_timeout,
274 };
275 
276 static void nvme_loop_destroy_admin_queue(struct nvme_loop_ctrl *ctrl)
277 {
278 	nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
279 	blk_cleanup_queue(ctrl->ctrl.admin_q);
280 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
281 }
282 
283 static void nvme_loop_free_ctrl(struct nvme_ctrl *nctrl)
284 {
285 	struct nvme_loop_ctrl *ctrl = to_loop_ctrl(nctrl);
286 
287 	if (list_empty(&ctrl->list))
288 		goto free_ctrl;
289 
290 	mutex_lock(&nvme_loop_ctrl_mutex);
291 	list_del(&ctrl->list);
292 	mutex_unlock(&nvme_loop_ctrl_mutex);
293 
294 	if (nctrl->tagset) {
295 		blk_cleanup_queue(ctrl->ctrl.connect_q);
296 		blk_mq_free_tag_set(&ctrl->tag_set);
297 	}
298 	kfree(ctrl->queues);
299 	nvmf_free_options(nctrl->opts);
300 free_ctrl:
301 	kfree(ctrl);
302 }
303 
304 static void nvme_loop_destroy_io_queues(struct nvme_loop_ctrl *ctrl)
305 {
306 	int i;
307 
308 	for (i = 1; i < ctrl->ctrl.queue_count; i++)
309 		nvmet_sq_destroy(&ctrl->queues[i].nvme_sq);
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 	}
350 
351 	return 0;
352 }
353 
354 static int nvme_loop_configure_admin_queue(struct nvme_loop_ctrl *ctrl)
355 {
356 	int error;
357 
358 	memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
359 	ctrl->admin_tag_set.ops = &nvme_loop_admin_mq_ops;
360 	ctrl->admin_tag_set.queue_depth = NVME_LOOP_AQ_BLKMQ_DEPTH;
361 	ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
362 	ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
363 	ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_loop_iod) +
364 		SG_CHUNK_SIZE * sizeof(struct scatterlist);
365 	ctrl->admin_tag_set.driver_data = ctrl;
366 	ctrl->admin_tag_set.nr_hw_queues = 1;
367 	ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
368 	ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
369 
370 	ctrl->queues[0].ctrl = ctrl;
371 	error = nvmet_sq_init(&ctrl->queues[0].nvme_sq);
372 	if (error)
373 		return error;
374 	ctrl->ctrl.queue_count = 1;
375 
376 	error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
377 	if (error)
378 		goto out_free_sq;
379 	ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
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->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->ctrl.cap), ctrl->ctrl.sqsize);
400 
401 	error = nvme_enable_ctrl(&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 	return 0;
413 
414 out_cleanup_queue:
415 	blk_cleanup_queue(ctrl->ctrl.admin_q);
416 out_free_tagset:
417 	blk_mq_free_tag_set(&ctrl->admin_tag_set);
418 out_free_sq:
419 	nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
420 	return error;
421 }
422 
423 static void nvme_loop_shutdown_ctrl(struct nvme_loop_ctrl *ctrl)
424 {
425 	if (ctrl->ctrl.queue_count > 1) {
426 		nvme_stop_queues(&ctrl->ctrl);
427 		blk_mq_tagset_busy_iter(&ctrl->tag_set,
428 					nvme_cancel_request, &ctrl->ctrl);
429 		nvme_loop_destroy_io_queues(ctrl);
430 	}
431 
432 	if (ctrl->ctrl.state == NVME_CTRL_LIVE)
433 		nvme_shutdown_ctrl(&ctrl->ctrl);
434 
435 	blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
436 	blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
437 				nvme_cancel_request, &ctrl->ctrl);
438 	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
439 	nvme_loop_destroy_admin_queue(ctrl);
440 }
441 
442 static void nvme_loop_del_ctrl_work(struct work_struct *work)
443 {
444 	struct nvme_loop_ctrl *ctrl = container_of(work,
445 				struct nvme_loop_ctrl, delete_work);
446 
447 	nvme_stop_ctrl(&ctrl->ctrl);
448 	nvme_remove_namespaces(&ctrl->ctrl);
449 	nvme_loop_shutdown_ctrl(ctrl);
450 	nvme_uninit_ctrl(&ctrl->ctrl);
451 	nvme_put_ctrl(&ctrl->ctrl);
452 }
453 
454 static int __nvme_loop_del_ctrl(struct nvme_loop_ctrl *ctrl)
455 {
456 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
457 		return -EBUSY;
458 
459 	if (!queue_work(nvme_wq, &ctrl->delete_work))
460 		return -EBUSY;
461 
462 	return 0;
463 }
464 
465 static int nvme_loop_del_ctrl(struct nvme_ctrl *nctrl)
466 {
467 	struct nvme_loop_ctrl *ctrl = to_loop_ctrl(nctrl);
468 	int ret;
469 
470 	ret = __nvme_loop_del_ctrl(ctrl);
471 	if (ret)
472 		return ret;
473 
474 	flush_work(&ctrl->delete_work);
475 
476 	return 0;
477 }
478 
479 static void nvme_loop_delete_ctrl(struct nvmet_ctrl *nctrl)
480 {
481 	struct nvme_loop_ctrl *ctrl;
482 
483 	mutex_lock(&nvme_loop_ctrl_mutex);
484 	list_for_each_entry(ctrl, &nvme_loop_ctrl_list, list) {
485 		if (ctrl->ctrl.cntlid == nctrl->cntlid)
486 			__nvme_loop_del_ctrl(ctrl);
487 	}
488 	mutex_unlock(&nvme_loop_ctrl_mutex);
489 }
490 
491 static void nvme_loop_reset_ctrl_work(struct work_struct *work)
492 {
493 	struct nvme_loop_ctrl *ctrl =
494 		container_of(work, struct nvme_loop_ctrl, ctrl.reset_work);
495 	bool changed;
496 	int ret;
497 
498 	nvme_stop_ctrl(&ctrl->ctrl);
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 	blk_mq_update_nr_hw_queues(&ctrl->tag_set,
514 			ctrl->ctrl.queue_count - 1);
515 
516 	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
517 	WARN_ON_ONCE(!changed);
518 
519 	nvme_start_ctrl(&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->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 	nvme_get_ctrl(&ctrl->ctrl);
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 	nvme_start_ctrl(&ctrl->ctrl);
655 
656 	return &ctrl->ctrl;
657 
658 out_remove_admin_queue:
659 	nvme_loop_destroy_admin_queue(ctrl);
660 out_free_queues:
661 	kfree(ctrl->queues);
662 out_uninit_ctrl:
663 	nvme_uninit_ctrl(&ctrl->ctrl);
664 out_put_ctrl:
665 	nvme_put_ctrl(&ctrl->ctrl);
666 	if (ret > 0)
667 		ret = -EIO;
668 	return ERR_PTR(ret);
669 }
670 
671 static int nvme_loop_add_port(struct nvmet_port *port)
672 {
673 	/*
674 	 * XXX: disalow adding more than one port so
675 	 * there is no connection rejections when a
676 	 * a subsystem is assigned to a port for which
677 	 * loop doesn't have a pointer.
678 	 * This scenario would be possible if we allowed
679 	 * more than one port to be added and a subsystem
680 	 * was assigned to a port other than nvmet_loop_port.
681 	 */
682 
683 	if (nvmet_loop_port)
684 		return -EPERM;
685 
686 	nvmet_loop_port = port;
687 	return 0;
688 }
689 
690 static void nvme_loop_remove_port(struct nvmet_port *port)
691 {
692 	if (port == nvmet_loop_port)
693 		nvmet_loop_port = NULL;
694 }
695 
696 static struct nvmet_fabrics_ops nvme_loop_ops = {
697 	.owner		= THIS_MODULE,
698 	.type		= NVMF_TRTYPE_LOOP,
699 	.add_port	= nvme_loop_add_port,
700 	.remove_port	= nvme_loop_remove_port,
701 	.queue_response = nvme_loop_queue_response,
702 	.delete_ctrl	= nvme_loop_delete_ctrl,
703 };
704 
705 static struct nvmf_transport_ops nvme_loop_transport = {
706 	.name		= "loop",
707 	.create_ctrl	= nvme_loop_create_ctrl,
708 };
709 
710 static int __init nvme_loop_init_module(void)
711 {
712 	int ret;
713 
714 	ret = nvmet_register_transport(&nvme_loop_ops);
715 	if (ret)
716 		return ret;
717 
718 	ret = nvmf_register_transport(&nvme_loop_transport);
719 	if (ret)
720 		nvmet_unregister_transport(&nvme_loop_ops);
721 
722 	return ret;
723 }
724 
725 static void __exit nvme_loop_cleanup_module(void)
726 {
727 	struct nvme_loop_ctrl *ctrl, *next;
728 
729 	nvmf_unregister_transport(&nvme_loop_transport);
730 	nvmet_unregister_transport(&nvme_loop_ops);
731 
732 	mutex_lock(&nvme_loop_ctrl_mutex);
733 	list_for_each_entry_safe(ctrl, next, &nvme_loop_ctrl_list, list)
734 		__nvme_loop_del_ctrl(ctrl);
735 	mutex_unlock(&nvme_loop_ctrl_mutex);
736 
737 	flush_workqueue(nvme_wq);
738 }
739 
740 module_init(nvme_loop_init_module);
741 module_exit(nvme_loop_cleanup_module);
742 
743 MODULE_LICENSE("GPL v2");
744 MODULE_ALIAS("nvmet-transport-254"); /* 254 == NVMF_TRTYPE_LOOP */
745