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