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