]>
Commit | Line | Data |
---|---|---|
d0ad6904 | 1 | // SPDX-License-Identifier: GPL-2.0 |
3a85a5de CH |
2 | /* |
3 | * NVMe over Fabrics loopback device. | |
4 | * Copyright (c) 2015-2016 HGST, a Western Digital Company. | |
3a85a5de CH |
5 | */ |
6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
7 | #include <linux/scatterlist.h> | |
3a85a5de CH |
8 | #include <linux/blk-mq.h> |
9 | #include <linux/nvme.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/parser.h> | |
3a85a5de CH |
12 | #include "nvmet.h" |
13 | #include "../host/nvme.h" | |
14 | #include "../host/fabrics.h" | |
15 | ||
3a85a5de CH |
16 | #define NVME_LOOP_MAX_SEGMENTS 256 |
17 | ||
3a85a5de | 18 | struct nvme_loop_iod { |
d49187e9 | 19 | struct nvme_request nvme_req; |
3a85a5de CH |
20 | struct nvme_command cmd; |
21 | struct nvme_completion rsp; | |
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 { | |
3a85a5de | 30 | struct nvme_loop_queue *queues; |
3a85a5de CH |
31 | |
32 | struct blk_mq_tag_set admin_tag_set; | |
33 | ||
34 | struct list_head list; | |
3a85a5de CH |
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; | |
fe4a9791 | 40 | struct nvmet_port *port; |
3a85a5de CH |
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 | ||
9d7fab04 SG |
48 | enum nvme_loop_queue_flags { |
49 | NVME_LOOP_Q_LIVE = 0, | |
50 | }; | |
51 | ||
3a85a5de CH |
52 | struct nvme_loop_queue { |
53 | struct nvmet_cq nvme_cq; | |
54 | struct nvmet_sq nvme_sq; | |
55 | struct nvme_loop_ctrl *ctrl; | |
9d7fab04 | 56 | unsigned long flags; |
3a85a5de CH |
57 | }; |
58 | ||
fe4a9791 CH |
59 | static LIST_HEAD(nvme_loop_ports); |
60 | static DEFINE_MUTEX(nvme_loop_ports_mutex); | |
3a85a5de CH |
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 | ||
e929f06d | 68 | static const struct nvmet_fabrics_ops nvme_loop_ops; |
3a85a5de CH |
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); | |
3a85a5de CH |
78 | |
79 | nvme_cleanup_cmd(req); | |
80 | sg_free_table_chained(&iod->sg_table, true); | |
77f02a7a | 81 | nvme_complete_rq(req); |
3a85a5de | 82 | } |
3a85a5de | 83 | |
3b068376 SG |
84 | static struct blk_mq_tags *nvme_loop_tagset(struct nvme_loop_queue *queue) |
85 | { | |
86 | u32 queue_idx = nvme_loop_queue_idx(queue); | |
3a85a5de | 87 | |
3b068376 SG |
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]; | |
3a85a5de CH |
91 | } |
92 | ||
d49187e9 | 93 | static void nvme_loop_queue_response(struct nvmet_req *req) |
3a85a5de | 94 | { |
3b068376 SG |
95 | struct nvme_loop_queue *queue = |
96 | container_of(req->sq, struct nvme_loop_queue, nvme_sq); | |
97 | struct nvme_completion *cqe = req->rsp; | |
3a85a5de CH |
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 | */ | |
3b068376 | 105 | if (unlikely(nvme_loop_queue_idx(queue) == 0 && |
38dabe21 | 106 | cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH)) { |
3b068376 | 107 | nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status, |
7bf58533 | 108 | &cqe->result); |
3a85a5de | 109 | } else { |
3b068376 | 110 | struct request *rq; |
3b068376 SG |
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 | } | |
3a85a5de | 119 | |
27fa9bc5 | 120 | nvme_end_request(rq, cqe->status, cqe->result); |
3a85a5de CH |
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 | ||
5e62d5c9 | 129 | nvmet_req_execute(&iod->req); |
3a85a5de CH |
130 | } |
131 | ||
132 | static enum blk_eh_timer_return | |
133 | nvme_loop_timeout(struct request *rq, bool reserved) | |
134 | { | |
135 | struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(rq); | |
136 | ||
137 | /* queue error recovery */ | |
d86c4d8e | 138 | nvme_reset_ctrl(&iod->queue->ctrl->ctrl); |
3a85a5de CH |
139 | |
140 | /* fail with DNR on admin cmd timeout */ | |
27fa9bc5 | 141 | nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR; |
3a85a5de | 142 | |
db8c48e4 | 143 | return BLK_EH_DONE; |
3a85a5de CH |
144 | } |
145 | ||
fc17b653 | 146 | static blk_status_t nvme_loop_queue_rq(struct blk_mq_hw_ctx *hctx, |
3a85a5de CH |
147 | const struct blk_mq_queue_data *bd) |
148 | { | |
149 | struct nvme_ns *ns = hctx->queue->queuedata; | |
150 | struct nvme_loop_queue *queue = hctx->driver_data; | |
151 | struct request *req = bd->rq; | |
152 | struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req); | |
3bc32bb1 | 153 | bool queue_ready = test_bit(NVME_LOOP_Q_LIVE, &queue->flags); |
fc17b653 | 154 | blk_status_t ret; |
3a85a5de | 155 | |
3bc32bb1 | 156 | if (!nvmf_check_ready(&queue->ctrl->ctrl, req, queue_ready)) |
6cdefc6e | 157 | return nvmf_fail_nonready_command(&queue->ctrl->ctrl, req); |
9d7fab04 | 158 | |
3a85a5de | 159 | ret = nvme_setup_cmd(ns, req, &iod->cmd); |
fc17b653 | 160 | if (ret) |
3a85a5de CH |
161 | return ret; |
162 | ||
11d9ea6f | 163 | blk_mq_start_request(req); |
3a85a5de | 164 | iod->cmd.common.flags |= NVME_CMD_SGL_METABUF; |
fe4a9791 | 165 | iod->req.port = queue->ctrl->port; |
3a85a5de | 166 | if (!nvmet_req_init(&iod->req, &queue->nvme_cq, |
11d9ea6f | 167 | &queue->nvme_sq, &nvme_loop_ops)) |
fc17b653 | 168 | return BLK_STS_OK; |
3a85a5de | 169 | |
eb464833 | 170 | if (blk_rq_nr_phys_segments(req)) { |
3a85a5de | 171 | iod->sg_table.sgl = iod->first_sgl; |
fc17b653 | 172 | if (sg_alloc_table_chained(&iod->sg_table, |
f9d03f96 | 173 | blk_rq_nr_phys_segments(req), |
fc17b653 CH |
174 | iod->sg_table.sgl)) |
175 | return BLK_STS_RESOURCE; | |
3a85a5de CH |
176 | |
177 | iod->req.sg = iod->sg_table.sgl; | |
178 | iod->req.sg_cnt = blk_rq_map_sg(req->q, req, iod->sg_table.sgl); | |
796b0b8d | 179 | iod->req.transfer_len = blk_rq_payload_bytes(req); |
3a85a5de CH |
180 | } |
181 | ||
3a85a5de | 182 | schedule_work(&iod->work); |
fc17b653 | 183 | return BLK_STS_OK; |
3a85a5de CH |
184 | } |
185 | ||
ad22c355 | 186 | static void nvme_loop_submit_async_event(struct nvme_ctrl *arg) |
3a85a5de CH |
187 | { |
188 | struct nvme_loop_ctrl *ctrl = to_loop_ctrl(arg); | |
189 | struct nvme_loop_queue *queue = &ctrl->queues[0]; | |
190 | struct nvme_loop_iod *iod = &ctrl->async_event_iod; | |
191 | ||
192 | memset(&iod->cmd, 0, sizeof(iod->cmd)); | |
193 | iod->cmd.common.opcode = nvme_admin_async_event; | |
38dabe21 | 194 | iod->cmd.common.command_id = NVME_AQ_BLK_MQ_DEPTH; |
3a85a5de CH |
195 | iod->cmd.common.flags |= NVME_CMD_SGL_METABUF; |
196 | ||
197 | if (!nvmet_req_init(&iod->req, &queue->nvme_cq, &queue->nvme_sq, | |
198 | &nvme_loop_ops)) { | |
199 | dev_err(ctrl->ctrl.device, "failed async event work\n"); | |
200 | return; | |
201 | } | |
202 | ||
203 | schedule_work(&iod->work); | |
204 | } | |
205 | ||
206 | static int nvme_loop_init_iod(struct nvme_loop_ctrl *ctrl, | |
207 | struct nvme_loop_iod *iod, unsigned int queue_idx) | |
208 | { | |
3a85a5de CH |
209 | iod->req.cmd = &iod->cmd; |
210 | iod->req.rsp = &iod->rsp; | |
211 | iod->queue = &ctrl->queues[queue_idx]; | |
212 | INIT_WORK(&iod->work, nvme_loop_execute_work); | |
213 | return 0; | |
214 | } | |
215 | ||
d6296d39 CH |
216 | static int nvme_loop_init_request(struct blk_mq_tag_set *set, |
217 | struct request *req, unsigned int hctx_idx, | |
218 | unsigned int numa_node) | |
3a85a5de | 219 | { |
62b83b18 | 220 | struct nvme_loop_ctrl *ctrl = set->driver_data; |
3a85a5de | 221 | |
59e29ce6 | 222 | nvme_req(req)->ctrl = &ctrl->ctrl; |
62b83b18 CH |
223 | return nvme_loop_init_iod(ctrl, blk_mq_rq_to_pdu(req), |
224 | (set == &ctrl->tag_set) ? hctx_idx + 1 : 0); | |
3a85a5de CH |
225 | } |
226 | ||
227 | static int nvme_loop_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, | |
228 | unsigned int hctx_idx) | |
229 | { | |
230 | struct nvme_loop_ctrl *ctrl = data; | |
231 | struct nvme_loop_queue *queue = &ctrl->queues[hctx_idx + 1]; | |
232 | ||
d858e5f0 | 233 | BUG_ON(hctx_idx >= ctrl->ctrl.queue_count); |
3a85a5de CH |
234 | |
235 | hctx->driver_data = queue; | |
236 | return 0; | |
237 | } | |
238 | ||
239 | static int nvme_loop_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, | |
240 | unsigned int hctx_idx) | |
241 | { | |
242 | struct nvme_loop_ctrl *ctrl = data; | |
243 | struct nvme_loop_queue *queue = &ctrl->queues[0]; | |
244 | ||
245 | BUG_ON(hctx_idx != 0); | |
246 | ||
247 | hctx->driver_data = queue; | |
248 | return 0; | |
249 | } | |
250 | ||
f363b089 | 251 | static const struct blk_mq_ops nvme_loop_mq_ops = { |
3a85a5de CH |
252 | .queue_rq = nvme_loop_queue_rq, |
253 | .complete = nvme_loop_complete_rq, | |
3a85a5de CH |
254 | .init_request = nvme_loop_init_request, |
255 | .init_hctx = nvme_loop_init_hctx, | |
256 | .timeout = nvme_loop_timeout, | |
257 | }; | |
258 | ||
f363b089 | 259 | static const struct blk_mq_ops nvme_loop_admin_mq_ops = { |
3a85a5de CH |
260 | .queue_rq = nvme_loop_queue_rq, |
261 | .complete = nvme_loop_complete_rq, | |
62b83b18 | 262 | .init_request = nvme_loop_init_request, |
3a85a5de CH |
263 | .init_hctx = nvme_loop_init_admin_hctx, |
264 | .timeout = nvme_loop_timeout, | |
265 | }; | |
266 | ||
267 | static void nvme_loop_destroy_admin_queue(struct nvme_loop_ctrl *ctrl) | |
268 | { | |
9d7fab04 | 269 | clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags); |
e4c5d376 | 270 | nvmet_sq_destroy(&ctrl->queues[0].nvme_sq); |
3a85a5de CH |
271 | blk_cleanup_queue(ctrl->ctrl.admin_q); |
272 | blk_mq_free_tag_set(&ctrl->admin_tag_set); | |
3a85a5de CH |
273 | } |
274 | ||
275 | static void nvme_loop_free_ctrl(struct nvme_ctrl *nctrl) | |
276 | { | |
277 | struct nvme_loop_ctrl *ctrl = to_loop_ctrl(nctrl); | |
278 | ||
279 | if (list_empty(&ctrl->list)) | |
280 | goto free_ctrl; | |
281 | ||
282 | mutex_lock(&nvme_loop_ctrl_mutex); | |
283 | list_del(&ctrl->list); | |
284 | mutex_unlock(&nvme_loop_ctrl_mutex); | |
285 | ||
286 | if (nctrl->tagset) { | |
287 | blk_cleanup_queue(ctrl->ctrl.connect_q); | |
288 | blk_mq_free_tag_set(&ctrl->tag_set); | |
289 | } | |
290 | kfree(ctrl->queues); | |
291 | nvmf_free_options(nctrl->opts); | |
292 | free_ctrl: | |
293 | kfree(ctrl); | |
294 | } | |
295 | ||
945dd5ba SG |
296 | static void nvme_loop_destroy_io_queues(struct nvme_loop_ctrl *ctrl) |
297 | { | |
298 | int i; | |
299 | ||
9d7fab04 SG |
300 | for (i = 1; i < ctrl->ctrl.queue_count; i++) { |
301 | clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags); | |
945dd5ba | 302 | nvmet_sq_destroy(&ctrl->queues[i].nvme_sq); |
9d7fab04 | 303 | } |
945dd5ba SG |
304 | } |
305 | ||
306 | static int nvme_loop_init_io_queues(struct nvme_loop_ctrl *ctrl) | |
307 | { | |
308 | struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; | |
309 | unsigned int nr_io_queues; | |
310 | int ret, i; | |
311 | ||
312 | nr_io_queues = min(opts->nr_io_queues, num_online_cpus()); | |
313 | ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues); | |
314 | if (ret || !nr_io_queues) | |
315 | return ret; | |
316 | ||
317 | dev_info(ctrl->ctrl.device, "creating %d I/O queues.\n", nr_io_queues); | |
318 | ||
319 | for (i = 1; i <= nr_io_queues; i++) { | |
320 | ctrl->queues[i].ctrl = ctrl; | |
321 | ret = nvmet_sq_init(&ctrl->queues[i].nvme_sq); | |
322 | if (ret) | |
323 | goto out_destroy_queues; | |
324 | ||
d858e5f0 | 325 | ctrl->ctrl.queue_count++; |
945dd5ba SG |
326 | } |
327 | ||
328 | return 0; | |
329 | ||
330 | out_destroy_queues: | |
331 | nvme_loop_destroy_io_queues(ctrl); | |
332 | return ret; | |
333 | } | |
334 | ||
297186d6 SG |
335 | static int nvme_loop_connect_io_queues(struct nvme_loop_ctrl *ctrl) |
336 | { | |
337 | int i, ret; | |
338 | ||
d858e5f0 | 339 | for (i = 1; i < ctrl->ctrl.queue_count; i++) { |
26c68227 | 340 | ret = nvmf_connect_io_queue(&ctrl->ctrl, i, false); |
297186d6 SG |
341 | if (ret) |
342 | return ret; | |
9d7fab04 | 343 | set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags); |
297186d6 SG |
344 | } |
345 | ||
346 | return 0; | |
347 | } | |
348 | ||
3a85a5de CH |
349 | static int nvme_loop_configure_admin_queue(struct nvme_loop_ctrl *ctrl) |
350 | { | |
351 | int error; | |
352 | ||
353 | memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set)); | |
354 | ctrl->admin_tag_set.ops = &nvme_loop_admin_mq_ops; | |
38dabe21 | 355 | ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH; |
3a85a5de CH |
356 | ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */ |
357 | ctrl->admin_tag_set.numa_node = NUMA_NO_NODE; | |
358 | ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_loop_iod) + | |
359 | SG_CHUNK_SIZE * sizeof(struct scatterlist); | |
360 | ctrl->admin_tag_set.driver_data = ctrl; | |
361 | ctrl->admin_tag_set.nr_hw_queues = 1; | |
362 | ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT; | |
86f36b9c | 363 | ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED; |
3a85a5de CH |
364 | |
365 | ctrl->queues[0].ctrl = ctrl; | |
366 | error = nvmet_sq_init(&ctrl->queues[0].nvme_sq); | |
367 | if (error) | |
368 | return error; | |
d858e5f0 | 369 | ctrl->ctrl.queue_count = 1; |
3a85a5de CH |
370 | |
371 | error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set); | |
372 | if (error) | |
373 | goto out_free_sq; | |
34b6c231 | 374 | ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set; |
3a85a5de CH |
375 | |
376 | ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set); | |
377 | if (IS_ERR(ctrl->ctrl.admin_q)) { | |
378 | error = PTR_ERR(ctrl->ctrl.admin_q); | |
379 | goto out_free_tagset; | |
380 | } | |
381 | ||
382 | error = nvmf_connect_admin_queue(&ctrl->ctrl); | |
383 | if (error) | |
384 | goto out_cleanup_queue; | |
385 | ||
9d7fab04 SG |
386 | set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags); |
387 | ||
20d0dfe6 | 388 | error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->ctrl.cap); |
3a85a5de CH |
389 | if (error) { |
390 | dev_err(ctrl->ctrl.device, | |
391 | "prop_get NVME_REG_CAP failed\n"); | |
392 | goto out_cleanup_queue; | |
393 | } | |
394 | ||
395 | ctrl->ctrl.sqsize = | |
20d0dfe6 | 396 | min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize); |
3a85a5de | 397 | |
20d0dfe6 | 398 | error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap); |
3a85a5de CH |
399 | if (error) |
400 | goto out_cleanup_queue; | |
401 | ||
402 | ctrl->ctrl.max_hw_sectors = | |
403 | (NVME_LOOP_MAX_SEGMENTS - 1) << (PAGE_SHIFT - 9); | |
404 | ||
405 | error = nvme_init_identify(&ctrl->ctrl); | |
406 | if (error) | |
407 | goto out_cleanup_queue; | |
408 | ||
3a85a5de CH |
409 | return 0; |
410 | ||
411 | out_cleanup_queue: | |
412 | blk_cleanup_queue(ctrl->ctrl.admin_q); | |
413 | out_free_tagset: | |
414 | blk_mq_free_tag_set(&ctrl->admin_tag_set); | |
415 | out_free_sq: | |
416 | nvmet_sq_destroy(&ctrl->queues[0].nvme_sq); | |
417 | return error; | |
418 | } | |
419 | ||
420 | static void nvme_loop_shutdown_ctrl(struct nvme_loop_ctrl *ctrl) | |
421 | { | |
d858e5f0 | 422 | if (ctrl->ctrl.queue_count > 1) { |
3a85a5de CH |
423 | nvme_stop_queues(&ctrl->ctrl); |
424 | blk_mq_tagset_busy_iter(&ctrl->tag_set, | |
425 | nvme_cancel_request, &ctrl->ctrl); | |
945dd5ba | 426 | nvme_loop_destroy_io_queues(ctrl); |
3a85a5de CH |
427 | } |
428 | ||
429 | if (ctrl->ctrl.state == NVME_CTRL_LIVE) | |
430 | nvme_shutdown_ctrl(&ctrl->ctrl); | |
431 | ||
c1c0ffff | 432 | blk_mq_quiesce_queue(ctrl->ctrl.admin_q); |
3a85a5de CH |
433 | blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, |
434 | nvme_cancel_request, &ctrl->ctrl); | |
c1c0ffff | 435 | blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); |
3a85a5de CH |
436 | nvme_loop_destroy_admin_queue(ctrl); |
437 | } | |
438 | ||
c5017e85 | 439 | static void nvme_loop_delete_ctrl_host(struct nvme_ctrl *ctrl) |
3a85a5de | 440 | { |
c5017e85 | 441 | nvme_loop_shutdown_ctrl(to_loop_ctrl(ctrl)); |
3a85a5de CH |
442 | } |
443 | ||
444 | static void nvme_loop_delete_ctrl(struct nvmet_ctrl *nctrl) | |
445 | { | |
446 | struct nvme_loop_ctrl *ctrl; | |
447 | ||
448 | mutex_lock(&nvme_loop_ctrl_mutex); | |
449 | list_for_each_entry(ctrl, &nvme_loop_ctrl_list, list) { | |
450 | if (ctrl->ctrl.cntlid == nctrl->cntlid) | |
c5017e85 | 451 | nvme_delete_ctrl(&ctrl->ctrl); |
3a85a5de CH |
452 | } |
453 | mutex_unlock(&nvme_loop_ctrl_mutex); | |
454 | } | |
455 | ||
456 | static void nvme_loop_reset_ctrl_work(struct work_struct *work) | |
457 | { | |
d86c4d8e CH |
458 | struct nvme_loop_ctrl *ctrl = |
459 | container_of(work, struct nvme_loop_ctrl, ctrl.reset_work); | |
3a85a5de | 460 | bool changed; |
297186d6 | 461 | int ret; |
3a85a5de | 462 | |
d09f2b45 | 463 | nvme_stop_ctrl(&ctrl->ctrl); |
3a85a5de CH |
464 | nvme_loop_shutdown_ctrl(ctrl); |
465 | ||
8bfc3b4c JT |
466 | if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) { |
467 | /* state change failure should never happen */ | |
468 | WARN_ON_ONCE(1); | |
469 | return; | |
470 | } | |
471 | ||
3a85a5de CH |
472 | ret = nvme_loop_configure_admin_queue(ctrl); |
473 | if (ret) | |
474 | goto out_disable; | |
475 | ||
945dd5ba SG |
476 | ret = nvme_loop_init_io_queues(ctrl); |
477 | if (ret) | |
478 | goto out_destroy_admin; | |
3a85a5de | 479 | |
297186d6 SG |
480 | ret = nvme_loop_connect_io_queues(ctrl); |
481 | if (ret) | |
482 | goto out_destroy_io; | |
3a85a5de | 483 | |
4368c39b SG |
484 | blk_mq_update_nr_hw_queues(&ctrl->tag_set, |
485 | ctrl->ctrl.queue_count - 1); | |
486 | ||
3a85a5de CH |
487 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); |
488 | WARN_ON_ONCE(!changed); | |
489 | ||
d09f2b45 | 490 | nvme_start_ctrl(&ctrl->ctrl); |
3a85a5de CH |
491 | |
492 | return; | |
493 | ||
945dd5ba SG |
494 | out_destroy_io: |
495 | nvme_loop_destroy_io_queues(ctrl); | |
496 | out_destroy_admin: | |
3a85a5de CH |
497 | nvme_loop_destroy_admin_queue(ctrl); |
498 | out_disable: | |
499 | dev_warn(ctrl->ctrl.device, "Removing after reset failure\n"); | |
3a85a5de CH |
500 | nvme_uninit_ctrl(&ctrl->ctrl); |
501 | nvme_put_ctrl(&ctrl->ctrl); | |
502 | } | |
503 | ||
3a85a5de CH |
504 | static const struct nvme_ctrl_ops nvme_loop_ctrl_ops = { |
505 | .name = "loop", | |
506 | .module = THIS_MODULE, | |
d3d5b87d | 507 | .flags = NVME_F_FABRICS, |
3a85a5de CH |
508 | .reg_read32 = nvmf_reg_read32, |
509 | .reg_read64 = nvmf_reg_read64, | |
510 | .reg_write32 = nvmf_reg_write32, | |
3a85a5de CH |
511 | .free_ctrl = nvme_loop_free_ctrl, |
512 | .submit_async_event = nvme_loop_submit_async_event, | |
c5017e85 | 513 | .delete_ctrl = nvme_loop_delete_ctrl_host, |
fe4a9791 | 514 | .get_address = nvmf_get_address, |
3a85a5de CH |
515 | }; |
516 | ||
517 | static int nvme_loop_create_io_queues(struct nvme_loop_ctrl *ctrl) | |
518 | { | |
297186d6 | 519 | int ret; |
3a85a5de | 520 | |
945dd5ba SG |
521 | ret = nvme_loop_init_io_queues(ctrl); |
522 | if (ret) | |
3a85a5de CH |
523 | return ret; |
524 | ||
3a85a5de CH |
525 | memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set)); |
526 | ctrl->tag_set.ops = &nvme_loop_mq_ops; | |
eadb7cf4 | 527 | ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size; |
3a85a5de CH |
528 | ctrl->tag_set.reserved_tags = 1; /* fabric connect */ |
529 | ctrl->tag_set.numa_node = NUMA_NO_NODE; | |
530 | ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; | |
531 | ctrl->tag_set.cmd_size = sizeof(struct nvme_loop_iod) + | |
532 | SG_CHUNK_SIZE * sizeof(struct scatterlist); | |
533 | ctrl->tag_set.driver_data = ctrl; | |
d858e5f0 | 534 | ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1; |
3a85a5de CH |
535 | ctrl->tag_set.timeout = NVME_IO_TIMEOUT; |
536 | ctrl->ctrl.tagset = &ctrl->tag_set; | |
537 | ||
538 | ret = blk_mq_alloc_tag_set(&ctrl->tag_set); | |
539 | if (ret) | |
540 | goto out_destroy_queues; | |
541 | ||
542 | ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set); | |
543 | if (IS_ERR(ctrl->ctrl.connect_q)) { | |
544 | ret = PTR_ERR(ctrl->ctrl.connect_q); | |
545 | goto out_free_tagset; | |
546 | } | |
547 | ||
297186d6 SG |
548 | ret = nvme_loop_connect_io_queues(ctrl); |
549 | if (ret) | |
550 | goto out_cleanup_connect_q; | |
3a85a5de CH |
551 | |
552 | return 0; | |
553 | ||
554 | out_cleanup_connect_q: | |
555 | blk_cleanup_queue(ctrl->ctrl.connect_q); | |
556 | out_free_tagset: | |
557 | blk_mq_free_tag_set(&ctrl->tag_set); | |
558 | out_destroy_queues: | |
945dd5ba | 559 | nvme_loop_destroy_io_queues(ctrl); |
3a85a5de CH |
560 | return ret; |
561 | } | |
562 | ||
fe4a9791 CH |
563 | static struct nvmet_port *nvme_loop_find_port(struct nvme_ctrl *ctrl) |
564 | { | |
565 | struct nvmet_port *p, *found = NULL; | |
566 | ||
567 | mutex_lock(&nvme_loop_ports_mutex); | |
568 | list_for_each_entry(p, &nvme_loop_ports, entry) { | |
569 | /* if no transport address is specified use the first port */ | |
570 | if ((ctrl->opts->mask & NVMF_OPT_TRADDR) && | |
571 | strcmp(ctrl->opts->traddr, p->disc_addr.traddr)) | |
572 | continue; | |
573 | found = p; | |
574 | break; | |
575 | } | |
576 | mutex_unlock(&nvme_loop_ports_mutex); | |
577 | return found; | |
578 | } | |
579 | ||
3a85a5de CH |
580 | static struct nvme_ctrl *nvme_loop_create_ctrl(struct device *dev, |
581 | struct nvmf_ctrl_options *opts) | |
582 | { | |
583 | struct nvme_loop_ctrl *ctrl; | |
584 | bool changed; | |
585 | int ret; | |
586 | ||
587 | ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); | |
588 | if (!ctrl) | |
589 | return ERR_PTR(-ENOMEM); | |
590 | ctrl->ctrl.opts = opts; | |
591 | INIT_LIST_HEAD(&ctrl->list); | |
592 | ||
d86c4d8e | 593 | INIT_WORK(&ctrl->ctrl.reset_work, nvme_loop_reset_ctrl_work); |
3a85a5de CH |
594 | |
595 | ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_loop_ctrl_ops, | |
596 | 0 /* no quirks, we're perfect! */); | |
597 | if (ret) | |
598 | goto out_put_ctrl; | |
599 | ||
3a85a5de CH |
600 | ret = -ENOMEM; |
601 | ||
eadb7cf4 | 602 | ctrl->ctrl.sqsize = opts->queue_size - 1; |
3a85a5de | 603 | ctrl->ctrl.kato = opts->kato; |
fe4a9791 | 604 | ctrl->port = nvme_loop_find_port(&ctrl->ctrl); |
3a85a5de CH |
605 | |
606 | ctrl->queues = kcalloc(opts->nr_io_queues + 1, sizeof(*ctrl->queues), | |
607 | GFP_KERNEL); | |
608 | if (!ctrl->queues) | |
609 | goto out_uninit_ctrl; | |
610 | ||
611 | ret = nvme_loop_configure_admin_queue(ctrl); | |
612 | if (ret) | |
613 | goto out_free_queues; | |
614 | ||
615 | if (opts->queue_size > ctrl->ctrl.maxcmd) { | |
616 | /* warn if maxcmd is lower than queue_size */ | |
617 | dev_warn(ctrl->ctrl.device, | |
618 | "queue_size %zu > ctrl maxcmd %u, clamping down\n", | |
619 | opts->queue_size, ctrl->ctrl.maxcmd); | |
620 | opts->queue_size = ctrl->ctrl.maxcmd; | |
621 | } | |
622 | ||
623 | if (opts->nr_io_queues) { | |
624 | ret = nvme_loop_create_io_queues(ctrl); | |
625 | if (ret) | |
626 | goto out_remove_admin_queue; | |
627 | } | |
628 | ||
629 | nvme_loop_init_iod(ctrl, &ctrl->async_event_iod, 0); | |
630 | ||
631 | dev_info(ctrl->ctrl.device, | |
632 | "new ctrl: \"%s\"\n", ctrl->ctrl.opts->subsysnqn); | |
633 | ||
d22524a4 | 634 | nvme_get_ctrl(&ctrl->ctrl); |
3a85a5de CH |
635 | |
636 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); | |
637 | WARN_ON_ONCE(!changed); | |
638 | ||
639 | mutex_lock(&nvme_loop_ctrl_mutex); | |
640 | list_add_tail(&ctrl->list, &nvme_loop_ctrl_list); | |
641 | mutex_unlock(&nvme_loop_ctrl_mutex); | |
642 | ||
d09f2b45 | 643 | nvme_start_ctrl(&ctrl->ctrl); |
3a85a5de CH |
644 | |
645 | return &ctrl->ctrl; | |
646 | ||
647 | out_remove_admin_queue: | |
648 | nvme_loop_destroy_admin_queue(ctrl); | |
649 | out_free_queues: | |
650 | kfree(ctrl->queues); | |
651 | out_uninit_ctrl: | |
652 | nvme_uninit_ctrl(&ctrl->ctrl); | |
653 | out_put_ctrl: | |
654 | nvme_put_ctrl(&ctrl->ctrl); | |
655 | if (ret > 0) | |
656 | ret = -EIO; | |
657 | return ERR_PTR(ret); | |
658 | } | |
659 | ||
660 | static int nvme_loop_add_port(struct nvmet_port *port) | |
661 | { | |
fe4a9791 CH |
662 | mutex_lock(&nvme_loop_ports_mutex); |
663 | list_add_tail(&port->entry, &nvme_loop_ports); | |
664 | mutex_unlock(&nvme_loop_ports_mutex); | |
3a85a5de CH |
665 | return 0; |
666 | } | |
667 | ||
668 | static void nvme_loop_remove_port(struct nvmet_port *port) | |
669 | { | |
fe4a9791 CH |
670 | mutex_lock(&nvme_loop_ports_mutex); |
671 | list_del_init(&port->entry); | |
672 | mutex_unlock(&nvme_loop_ports_mutex); | |
3a85a5de CH |
673 | } |
674 | ||
e929f06d | 675 | static const struct nvmet_fabrics_ops nvme_loop_ops = { |
3a85a5de CH |
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 | ||
684 | static struct nvmf_transport_ops nvme_loop_transport = { | |
685 | .name = "loop", | |
0de5cd36 | 686 | .module = THIS_MODULE, |
3a85a5de | 687 | .create_ctrl = nvme_loop_create_ctrl, |
fe4a9791 | 688 | .allowed_opts = NVMF_OPT_TRADDR, |
3a85a5de CH |
689 | }; |
690 | ||
691 | static 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; | |
d19eef02 SG |
698 | |
699 | ret = nvmf_register_transport(&nvme_loop_transport); | |
700 | if (ret) | |
701 | nvmet_unregister_transport(&nvme_loop_ops); | |
702 | ||
703 | return ret; | |
3a85a5de CH |
704 | } |
705 | ||
706 | static 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) | |
c5017e85 | 715 | nvme_delete_ctrl(&ctrl->ctrl); |
3a85a5de CH |
716 | mutex_unlock(&nvme_loop_ctrl_mutex); |
717 | ||
b227c59b | 718 | flush_workqueue(nvme_delete_wq); |
3a85a5de CH |
719 | } |
720 | ||
721 | module_init(nvme_loop_init_module); | |
722 | module_exit(nvme_loop_cleanup_module); | |
723 | ||
724 | MODULE_LICENSE("GPL v2"); | |
725 | MODULE_ALIAS("nvmet-transport-254"); /* 254 == NVMF_TRTYPE_LOOP */ |