blob: ff3258c3eb8b6d1f962b46a4e51b8eba463b0d68 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * NVMe over Fabrics loopback device.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005 */
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
18struct nvme_loop_iod {
19 struct nvme_request nvme_req;
20 struct nvme_command cmd;
David Brazdil0f672f62019-12-10 10:32:29 +000021 struct nvme_completion cqe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000022 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
29struct 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039 struct nvmet_port *port;
40};
41
42static inline struct nvme_loop_ctrl *to_loop_ctrl(struct nvme_ctrl *ctrl)
43{
44 return container_of(ctrl, struct nvme_loop_ctrl, ctrl);
45}
46
47enum nvme_loop_queue_flags {
48 NVME_LOOP_Q_LIVE = 0,
49};
50
51struct nvme_loop_queue {
52 struct nvmet_cq nvme_cq;
53 struct nvmet_sq nvme_sq;
54 struct nvme_loop_ctrl *ctrl;
55 unsigned long flags;
56};
57
58static LIST_HEAD(nvme_loop_ports);
59static DEFINE_MUTEX(nvme_loop_ports_mutex);
60
61static LIST_HEAD(nvme_loop_ctrl_list);
62static DEFINE_MUTEX(nvme_loop_ctrl_mutex);
63
64static void nvme_loop_queue_response(struct nvmet_req *nvme_req);
65static void nvme_loop_delete_ctrl(struct nvmet_ctrl *ctrl);
66
67static const struct nvmet_fabrics_ops nvme_loop_ops;
68
69static inline int nvme_loop_queue_idx(struct nvme_loop_queue *queue)
70{
71 return queue - queue->ctrl->queues;
72}
73
74static void nvme_loop_complete_rq(struct request *req)
75{
76 struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req);
77
Olivier Deprez157378f2022-04-04 15:47:50 +020078 sg_free_table_chained(&iod->sg_table, NVME_INLINE_SG_CNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000079 nvme_complete_rq(req);
80}
81
82static struct blk_mq_tags *nvme_loop_tagset(struct nvme_loop_queue *queue)
83{
84 u32 queue_idx = nvme_loop_queue_idx(queue);
85
86 if (queue_idx == 0)
87 return queue->ctrl->admin_tag_set.tags[queue_idx];
88 return queue->ctrl->tag_set.tags[queue_idx - 1];
89}
90
91static void nvme_loop_queue_response(struct nvmet_req *req)
92{
93 struct nvme_loop_queue *queue =
94 container_of(req->sq, struct nvme_loop_queue, nvme_sq);
David Brazdil0f672f62019-12-10 10:32:29 +000095 struct nvme_completion *cqe = req->cqe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000096
97 /*
98 * AEN requests are special as they don't time out and can
99 * survive any kind of queue freeze and often don't respond to
100 * aborts. We don't even bother to allocate a struct request
101 * for them but rather special case them here.
102 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200103 if (unlikely(nvme_is_aen_req(nvme_loop_queue_idx(queue),
104 cqe->command_id))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
106 &cqe->result);
107 } else {
108 struct request *rq;
109
Olivier Deprez157378f2022-04-04 15:47:50 +0200110 rq = nvme_find_rq(nvme_loop_tagset(queue), cqe->command_id);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000111 if (!rq) {
112 dev_err(queue->ctrl->ctrl.device,
Olivier Deprez157378f2022-04-04 15:47:50 +0200113 "got bad command_id %#x on queue %d\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000114 cqe->command_id, nvme_loop_queue_idx(queue));
115 return;
116 }
117
Olivier Deprez157378f2022-04-04 15:47:50 +0200118 if (!nvme_try_complete_req(rq, cqe->status, cqe->result))
119 nvme_loop_complete_rq(rq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120 }
121}
122
123static void nvme_loop_execute_work(struct work_struct *work)
124{
125 struct nvme_loop_iod *iod =
126 container_of(work, struct nvme_loop_iod, work);
127
Olivier Deprez157378f2022-04-04 15:47:50 +0200128 iod->req.execute(&iod->req);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000129}
130
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000131static blk_status_t nvme_loop_queue_rq(struct blk_mq_hw_ctx *hctx,
132 const struct blk_mq_queue_data *bd)
133{
134 struct nvme_ns *ns = hctx->queue->queuedata;
135 struct nvme_loop_queue *queue = hctx->driver_data;
136 struct request *req = bd->rq;
137 struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req);
138 bool queue_ready = test_bit(NVME_LOOP_Q_LIVE, &queue->flags);
139 blk_status_t ret;
140
141 if (!nvmf_check_ready(&queue->ctrl->ctrl, req, queue_ready))
142 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, req);
143
144 ret = nvme_setup_cmd(ns, req, &iod->cmd);
145 if (ret)
146 return ret;
147
148 blk_mq_start_request(req);
149 iod->cmd.common.flags |= NVME_CMD_SGL_METABUF;
150 iod->req.port = queue->ctrl->port;
151 if (!nvmet_req_init(&iod->req, &queue->nvme_cq,
152 &queue->nvme_sq, &nvme_loop_ops))
153 return BLK_STS_OK;
154
155 if (blk_rq_nr_phys_segments(req)) {
156 iod->sg_table.sgl = iod->first_sgl;
157 if (sg_alloc_table_chained(&iod->sg_table,
158 blk_rq_nr_phys_segments(req),
Olivier Deprez157378f2022-04-04 15:47:50 +0200159 iod->sg_table.sgl, NVME_INLINE_SG_CNT)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000160 nvme_cleanup_cmd(req);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000161 return BLK_STS_RESOURCE;
David Brazdil0f672f62019-12-10 10:32:29 +0000162 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000163
164 iod->req.sg = iod->sg_table.sgl;
165 iod->req.sg_cnt = blk_rq_map_sg(req->q, req, iod->sg_table.sgl);
166 iod->req.transfer_len = blk_rq_payload_bytes(req);
167 }
168
169 schedule_work(&iod->work);
170 return BLK_STS_OK;
171}
172
173static void nvme_loop_submit_async_event(struct nvme_ctrl *arg)
174{
175 struct nvme_loop_ctrl *ctrl = to_loop_ctrl(arg);
176 struct nvme_loop_queue *queue = &ctrl->queues[0];
177 struct nvme_loop_iod *iod = &ctrl->async_event_iod;
178
179 memset(&iod->cmd, 0, sizeof(iod->cmd));
180 iod->cmd.common.opcode = nvme_admin_async_event;
181 iod->cmd.common.command_id = NVME_AQ_BLK_MQ_DEPTH;
182 iod->cmd.common.flags |= NVME_CMD_SGL_METABUF;
183
184 if (!nvmet_req_init(&iod->req, &queue->nvme_cq, &queue->nvme_sq,
185 &nvme_loop_ops)) {
186 dev_err(ctrl->ctrl.device, "failed async event work\n");
187 return;
188 }
189
190 schedule_work(&iod->work);
191}
192
193static int nvme_loop_init_iod(struct nvme_loop_ctrl *ctrl,
194 struct nvme_loop_iod *iod, unsigned int queue_idx)
195{
196 iod->req.cmd = &iod->cmd;
David Brazdil0f672f62019-12-10 10:32:29 +0000197 iod->req.cqe = &iod->cqe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000198 iod->queue = &ctrl->queues[queue_idx];
199 INIT_WORK(&iod->work, nvme_loop_execute_work);
200 return 0;
201}
202
203static int nvme_loop_init_request(struct blk_mq_tag_set *set,
204 struct request *req, unsigned int hctx_idx,
205 unsigned int numa_node)
206{
207 struct nvme_loop_ctrl *ctrl = set->driver_data;
208
209 nvme_req(req)->ctrl = &ctrl->ctrl;
210 return nvme_loop_init_iod(ctrl, blk_mq_rq_to_pdu(req),
211 (set == &ctrl->tag_set) ? hctx_idx + 1 : 0);
212}
213
214static int nvme_loop_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
215 unsigned int hctx_idx)
216{
217 struct nvme_loop_ctrl *ctrl = data;
218 struct nvme_loop_queue *queue = &ctrl->queues[hctx_idx + 1];
219
220 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
221
222 hctx->driver_data = queue;
223 return 0;
224}
225
226static int nvme_loop_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
227 unsigned int hctx_idx)
228{
229 struct nvme_loop_ctrl *ctrl = data;
230 struct nvme_loop_queue *queue = &ctrl->queues[0];
231
232 BUG_ON(hctx_idx != 0);
233
234 hctx->driver_data = queue;
235 return 0;
236}
237
238static const struct blk_mq_ops nvme_loop_mq_ops = {
239 .queue_rq = nvme_loop_queue_rq,
240 .complete = nvme_loop_complete_rq,
241 .init_request = nvme_loop_init_request,
242 .init_hctx = nvme_loop_init_hctx,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000243};
244
245static const struct blk_mq_ops nvme_loop_admin_mq_ops = {
246 .queue_rq = nvme_loop_queue_rq,
247 .complete = nvme_loop_complete_rq,
248 .init_request = nvme_loop_init_request,
249 .init_hctx = nvme_loop_init_admin_hctx,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000250};
251
252static void nvme_loop_destroy_admin_queue(struct nvme_loop_ctrl *ctrl)
253{
Olivier Deprez0e641232021-09-23 10:07:05 +0200254 if (!test_and_clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags))
255 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000256 nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
257 blk_cleanup_queue(ctrl->ctrl.admin_q);
David Brazdil0f672f62019-12-10 10:32:29 +0000258 blk_cleanup_queue(ctrl->ctrl.fabrics_q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000259 blk_mq_free_tag_set(&ctrl->admin_tag_set);
260}
261
262static void nvme_loop_free_ctrl(struct nvme_ctrl *nctrl)
263{
264 struct nvme_loop_ctrl *ctrl = to_loop_ctrl(nctrl);
265
266 if (list_empty(&ctrl->list))
267 goto free_ctrl;
268
269 mutex_lock(&nvme_loop_ctrl_mutex);
270 list_del(&ctrl->list);
271 mutex_unlock(&nvme_loop_ctrl_mutex);
272
273 if (nctrl->tagset) {
274 blk_cleanup_queue(ctrl->ctrl.connect_q);
275 blk_mq_free_tag_set(&ctrl->tag_set);
276 }
277 kfree(ctrl->queues);
278 nvmf_free_options(nctrl->opts);
279free_ctrl:
280 kfree(ctrl);
281}
282
283static void nvme_loop_destroy_io_queues(struct nvme_loop_ctrl *ctrl)
284{
285 int i;
286
287 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
288 clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags);
289 nvmet_sq_destroy(&ctrl->queues[i].nvme_sq);
290 }
Olivier Deprez0e641232021-09-23 10:07:05 +0200291 ctrl->ctrl.queue_count = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000292}
293
294static int nvme_loop_init_io_queues(struct nvme_loop_ctrl *ctrl)
295{
296 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
297 unsigned int nr_io_queues;
298 int ret, i;
299
300 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
301 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
302 if (ret || !nr_io_queues)
303 return ret;
304
305 dev_info(ctrl->ctrl.device, "creating %d I/O queues.\n", nr_io_queues);
306
307 for (i = 1; i <= nr_io_queues; i++) {
308 ctrl->queues[i].ctrl = ctrl;
309 ret = nvmet_sq_init(&ctrl->queues[i].nvme_sq);
310 if (ret)
311 goto out_destroy_queues;
312
313 ctrl->ctrl.queue_count++;
314 }
315
316 return 0;
317
318out_destroy_queues:
319 nvme_loop_destroy_io_queues(ctrl);
320 return ret;
321}
322
323static int nvme_loop_connect_io_queues(struct nvme_loop_ctrl *ctrl)
324{
325 int i, ret;
326
327 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
David Brazdil0f672f62019-12-10 10:32:29 +0000328 ret = nvmf_connect_io_queue(&ctrl->ctrl, i, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000329 if (ret)
330 return ret;
331 set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags);
332 }
333
334 return 0;
335}
336
337static int nvme_loop_configure_admin_queue(struct nvme_loop_ctrl *ctrl)
338{
339 int error;
340
341 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
342 ctrl->admin_tag_set.ops = &nvme_loop_admin_mq_ops;
343 ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
344 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
Olivier Deprez157378f2022-04-04 15:47:50 +0200345 ctrl->admin_tag_set.numa_node = ctrl->ctrl.numa_node;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000346 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_loop_iod) +
Olivier Deprez157378f2022-04-04 15:47:50 +0200347 NVME_INLINE_SG_CNT * sizeof(struct scatterlist);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000348 ctrl->admin_tag_set.driver_data = ctrl;
349 ctrl->admin_tag_set.nr_hw_queues = 1;
350 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
351 ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED;
352
353 ctrl->queues[0].ctrl = ctrl;
354 error = nvmet_sq_init(&ctrl->queues[0].nvme_sq);
355 if (error)
356 return error;
357 ctrl->ctrl.queue_count = 1;
358
359 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
360 if (error)
361 goto out_free_sq;
362 ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
363
David Brazdil0f672f62019-12-10 10:32:29 +0000364 ctrl->ctrl.fabrics_q = blk_mq_init_queue(&ctrl->admin_tag_set);
365 if (IS_ERR(ctrl->ctrl.fabrics_q)) {
366 error = PTR_ERR(ctrl->ctrl.fabrics_q);
367 goto out_free_tagset;
368 }
369
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000370 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
371 if (IS_ERR(ctrl->ctrl.admin_q)) {
372 error = PTR_ERR(ctrl->ctrl.admin_q);
David Brazdil0f672f62019-12-10 10:32:29 +0000373 goto out_cleanup_fabrics_q;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000374 }
375
376 error = nvmf_connect_admin_queue(&ctrl->ctrl);
377 if (error)
378 goto out_cleanup_queue;
379
380 set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags);
381
David Brazdil0f672f62019-12-10 10:32:29 +0000382 error = nvme_enable_ctrl(&ctrl->ctrl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000383 if (error)
384 goto out_cleanup_queue;
385
386 ctrl->ctrl.max_hw_sectors =
387 (NVME_LOOP_MAX_SEGMENTS - 1) << (PAGE_SHIFT - 9);
388
David Brazdil0f672f62019-12-10 10:32:29 +0000389 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
390
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000391 error = nvme_init_identify(&ctrl->ctrl);
392 if (error)
393 goto out_cleanup_queue;
394
395 return 0;
396
397out_cleanup_queue:
Olivier Deprez0e641232021-09-23 10:07:05 +0200398 clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000399 blk_cleanup_queue(ctrl->ctrl.admin_q);
David Brazdil0f672f62019-12-10 10:32:29 +0000400out_cleanup_fabrics_q:
401 blk_cleanup_queue(ctrl->ctrl.fabrics_q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000402out_free_tagset:
403 blk_mq_free_tag_set(&ctrl->admin_tag_set);
404out_free_sq:
405 nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
406 return error;
407}
408
409static void nvme_loop_shutdown_ctrl(struct nvme_loop_ctrl *ctrl)
410{
411 if (ctrl->ctrl.queue_count > 1) {
412 nvme_stop_queues(&ctrl->ctrl);
413 blk_mq_tagset_busy_iter(&ctrl->tag_set,
414 nvme_cancel_request, &ctrl->ctrl);
David Brazdil0f672f62019-12-10 10:32:29 +0000415 blk_mq_tagset_wait_completed_request(&ctrl->tag_set);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000416 nvme_loop_destroy_io_queues(ctrl);
417 }
418
David Brazdil0f672f62019-12-10 10:32:29 +0000419 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000420 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
421 nvme_shutdown_ctrl(&ctrl->ctrl);
422
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000423 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
424 nvme_cancel_request, &ctrl->ctrl);
David Brazdil0f672f62019-12-10 10:32:29 +0000425 blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000426 nvme_loop_destroy_admin_queue(ctrl);
427}
428
429static void nvme_loop_delete_ctrl_host(struct nvme_ctrl *ctrl)
430{
431 nvme_loop_shutdown_ctrl(to_loop_ctrl(ctrl));
432}
433
434static void nvme_loop_delete_ctrl(struct nvmet_ctrl *nctrl)
435{
436 struct nvme_loop_ctrl *ctrl;
437
438 mutex_lock(&nvme_loop_ctrl_mutex);
439 list_for_each_entry(ctrl, &nvme_loop_ctrl_list, list) {
440 if (ctrl->ctrl.cntlid == nctrl->cntlid)
441 nvme_delete_ctrl(&ctrl->ctrl);
442 }
443 mutex_unlock(&nvme_loop_ctrl_mutex);
444}
445
446static void nvme_loop_reset_ctrl_work(struct work_struct *work)
447{
448 struct nvme_loop_ctrl *ctrl =
449 container_of(work, struct nvme_loop_ctrl, ctrl.reset_work);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000450 int ret;
451
452 nvme_stop_ctrl(&ctrl->ctrl);
453 nvme_loop_shutdown_ctrl(ctrl);
454
455 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200456 if (ctrl->ctrl.state != NVME_CTRL_DELETING &&
457 ctrl->ctrl.state != NVME_CTRL_DELETING_NOIO)
458 /* state change failure for non-deleted ctrl? */
459 WARN_ON_ONCE(1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000460 return;
461 }
462
463 ret = nvme_loop_configure_admin_queue(ctrl);
464 if (ret)
465 goto out_disable;
466
467 ret = nvme_loop_init_io_queues(ctrl);
468 if (ret)
469 goto out_destroy_admin;
470
471 ret = nvme_loop_connect_io_queues(ctrl);
472 if (ret)
473 goto out_destroy_io;
474
475 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
476 ctrl->ctrl.queue_count - 1);
477
Olivier Deprez157378f2022-04-04 15:47:50 +0200478 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE))
479 WARN_ON_ONCE(1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000480
481 nvme_start_ctrl(&ctrl->ctrl);
482
483 return;
484
485out_destroy_io:
486 nvme_loop_destroy_io_queues(ctrl);
487out_destroy_admin:
488 nvme_loop_destroy_admin_queue(ctrl);
489out_disable:
490 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
491 nvme_uninit_ctrl(&ctrl->ctrl);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000492}
493
494static const struct nvme_ctrl_ops nvme_loop_ctrl_ops = {
495 .name = "loop",
496 .module = THIS_MODULE,
497 .flags = NVME_F_FABRICS,
498 .reg_read32 = nvmf_reg_read32,
499 .reg_read64 = nvmf_reg_read64,
500 .reg_write32 = nvmf_reg_write32,
501 .free_ctrl = nvme_loop_free_ctrl,
502 .submit_async_event = nvme_loop_submit_async_event,
503 .delete_ctrl = nvme_loop_delete_ctrl_host,
504 .get_address = nvmf_get_address,
505};
506
507static int nvme_loop_create_io_queues(struct nvme_loop_ctrl *ctrl)
508{
509 int ret;
510
511 ret = nvme_loop_init_io_queues(ctrl);
512 if (ret)
513 return ret;
514
515 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
516 ctrl->tag_set.ops = &nvme_loop_mq_ops;
517 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
518 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
Olivier Deprez157378f2022-04-04 15:47:50 +0200519 ctrl->tag_set.numa_node = ctrl->ctrl.numa_node;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000520 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
521 ctrl->tag_set.cmd_size = sizeof(struct nvme_loop_iod) +
Olivier Deprez157378f2022-04-04 15:47:50 +0200522 NVME_INLINE_SG_CNT * sizeof(struct scatterlist);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000523 ctrl->tag_set.driver_data = ctrl;
524 ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
525 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
526 ctrl->ctrl.tagset = &ctrl->tag_set;
527
528 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
529 if (ret)
530 goto out_destroy_queues;
531
532 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
533 if (IS_ERR(ctrl->ctrl.connect_q)) {
534 ret = PTR_ERR(ctrl->ctrl.connect_q);
535 goto out_free_tagset;
536 }
537
538 ret = nvme_loop_connect_io_queues(ctrl);
539 if (ret)
540 goto out_cleanup_connect_q;
541
542 return 0;
543
544out_cleanup_connect_q:
545 blk_cleanup_queue(ctrl->ctrl.connect_q);
546out_free_tagset:
547 blk_mq_free_tag_set(&ctrl->tag_set);
548out_destroy_queues:
549 nvme_loop_destroy_io_queues(ctrl);
550 return ret;
551}
552
553static struct nvmet_port *nvme_loop_find_port(struct nvme_ctrl *ctrl)
554{
555 struct nvmet_port *p, *found = NULL;
556
557 mutex_lock(&nvme_loop_ports_mutex);
558 list_for_each_entry(p, &nvme_loop_ports, entry) {
559 /* if no transport address is specified use the first port */
560 if ((ctrl->opts->mask & NVMF_OPT_TRADDR) &&
561 strcmp(ctrl->opts->traddr, p->disc_addr.traddr))
562 continue;
563 found = p;
564 break;
565 }
566 mutex_unlock(&nvme_loop_ports_mutex);
567 return found;
568}
569
570static struct nvme_ctrl *nvme_loop_create_ctrl(struct device *dev,
571 struct nvmf_ctrl_options *opts)
572{
573 struct nvme_loop_ctrl *ctrl;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000574 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! */);
Olivier Deprez157378f2022-04-04 15:47:50 +0200586 if (ret) {
587 kfree(ctrl);
588 goto out;
589 }
590
591 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
592 WARN_ON_ONCE(1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000593
594 ret = -ENOMEM;
595
596 ctrl->ctrl.sqsize = opts->queue_size - 1;
597 ctrl->ctrl.kato = opts->kato;
598 ctrl->port = nvme_loop_find_port(&ctrl->ctrl);
599
600 ctrl->queues = kcalloc(opts->nr_io_queues + 1, sizeof(*ctrl->queues),
601 GFP_KERNEL);
602 if (!ctrl->queues)
603 goto out_uninit_ctrl;
604
605 ret = nvme_loop_configure_admin_queue(ctrl);
606 if (ret)
607 goto out_free_queues;
608
609 if (opts->queue_size > ctrl->ctrl.maxcmd) {
610 /* warn if maxcmd is lower than queue_size */
611 dev_warn(ctrl->ctrl.device,
612 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
613 opts->queue_size, ctrl->ctrl.maxcmd);
614 opts->queue_size = ctrl->ctrl.maxcmd;
615 }
616
617 if (opts->nr_io_queues) {
618 ret = nvme_loop_create_io_queues(ctrl);
619 if (ret)
620 goto out_remove_admin_queue;
621 }
622
623 nvme_loop_init_iod(ctrl, &ctrl->async_event_iod, 0);
624
625 dev_info(ctrl->ctrl.device,
626 "new ctrl: \"%s\"\n", ctrl->ctrl.opts->subsysnqn);
627
Olivier Deprez157378f2022-04-04 15:47:50 +0200628 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE))
629 WARN_ON_ONCE(1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000630
631 mutex_lock(&nvme_loop_ctrl_mutex);
632 list_add_tail(&ctrl->list, &nvme_loop_ctrl_list);
633 mutex_unlock(&nvme_loop_ctrl_mutex);
634
635 nvme_start_ctrl(&ctrl->ctrl);
636
637 return &ctrl->ctrl;
638
639out_remove_admin_queue:
640 nvme_loop_destroy_admin_queue(ctrl);
641out_free_queues:
642 kfree(ctrl->queues);
643out_uninit_ctrl:
644 nvme_uninit_ctrl(&ctrl->ctrl);
Olivier Deprez0e641232021-09-23 10:07:05 +0200645 nvme_put_ctrl(&ctrl->ctrl);
Olivier Deprez157378f2022-04-04 15:47:50 +0200646out:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000647 if (ret > 0)
648 ret = -EIO;
649 return ERR_PTR(ret);
650}
651
652static int nvme_loop_add_port(struct nvmet_port *port)
653{
654 mutex_lock(&nvme_loop_ports_mutex);
655 list_add_tail(&port->entry, &nvme_loop_ports);
656 mutex_unlock(&nvme_loop_ports_mutex);
657 return 0;
658}
659
660static void nvme_loop_remove_port(struct nvmet_port *port)
661{
662 mutex_lock(&nvme_loop_ports_mutex);
663 list_del_init(&port->entry);
664 mutex_unlock(&nvme_loop_ports_mutex);
David Brazdil0f672f62019-12-10 10:32:29 +0000665
666 /*
667 * Ensure any ctrls that are in the process of being
668 * deleted are in fact deleted before we return
669 * and free the port. This is to prevent active
670 * ctrls from using a port after it's freed.
671 */
672 flush_workqueue(nvme_delete_wq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000673}
674
675static 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
684static 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
691static 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
706static 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
721module_init(nvme_loop_init_module);
722module_exit(nvme_loop_cleanup_module);
723
724MODULE_LICENSE("GPL v2");
725MODULE_ALIAS("nvmet-transport-254"); /* 254 == NVMF_TRTYPE_LOOP */