forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-31 f70575805708cabdedea7498aaa3f710fde4d920
kernel/drivers/nvme/target/loop.c
....@@ -1,15 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * NVMe over Fabrics loopback device.
34 * 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.
135 */
146 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
157 #include <linux/scatterlist.h>
....@@ -26,7 +18,7 @@
2618 struct nvme_loop_iod {
2719 struct nvme_request nvme_req;
2820 struct nvme_command cmd;
29
- struct nvme_completion rsp;
21
+ struct nvme_completion cqe;
3022 struct nvmet_req req;
3123 struct nvme_loop_queue *queue;
3224 struct work_struct work;
....@@ -44,7 +36,6 @@
4436 struct nvme_loop_iod async_event_iod;
4537 struct nvme_ctrl ctrl;
4638
47
- struct nvmet_ctrl *target_ctrl;
4839 struct nvmet_port *port;
4940 };
5041
....@@ -84,8 +75,7 @@
8475 {
8576 struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(req);
8677
87
- nvme_cleanup_cmd(req);
88
- sg_free_table_chained(&iod->sg_table, true);
78
+ sg_free_table_chained(&iod->sg_table, NVME_INLINE_SG_CNT);
8979 nvme_complete_rq(req);
9080 }
9181
....@@ -102,7 +92,7 @@
10292 {
10393 struct nvme_loop_queue *queue =
10494 container_of(req->sq, struct nvme_loop_queue, nvme_sq);
105
- struct nvme_completion *cqe = req->rsp;
95
+ struct nvme_completion *cqe = req->cqe;
10696
10797 /*
10898 * AEN requests are special as they don't time out and can
....@@ -110,22 +100,23 @@
110100 * aborts. We don't even bother to allocate a struct request
111101 * for them but rather special case them here.
112102 */
113
- if (unlikely(nvme_loop_queue_idx(queue) == 0 &&
114
- cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH)) {
103
+ if (unlikely(nvme_is_aen_req(nvme_loop_queue_idx(queue),
104
+ cqe->command_id))) {
115105 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
116106 &cqe->result);
117107 } else {
118108 struct request *rq;
119109
120
- rq = blk_mq_tag_to_rq(nvme_loop_tagset(queue), cqe->command_id);
110
+ rq = nvme_find_rq(nvme_loop_tagset(queue), cqe->command_id);
121111 if (!rq) {
122112 dev_err(queue->ctrl->ctrl.device,
123
- "tag 0x%x on queue %d not found\n",
113
+ "got bad command_id %#x on queue %d\n",
124114 cqe->command_id, nvme_loop_queue_idx(queue));
125115 return;
126116 }
127117
128
- nvme_end_request(rq, cqe->status, cqe->result);
118
+ if (!nvme_try_complete_req(rq, cqe->status, cqe->result))
119
+ nvme_loop_complete_rq(rq);
129120 }
130121 }
131122
....@@ -134,21 +125,7 @@
134125 struct nvme_loop_iod *iod =
135126 container_of(work, struct nvme_loop_iod, work);
136127
137
- nvmet_req_execute(&iod->req);
138
-}
139
-
140
-static enum blk_eh_timer_return
141
-nvme_loop_timeout(struct request *rq, bool reserved)
142
-{
143
- struct nvme_loop_iod *iod = blk_mq_rq_to_pdu(rq);
144
-
145
- /* queue error recovery */
146
- nvme_reset_ctrl(&iod->queue->ctrl->ctrl);
147
-
148
- /* fail with DNR on admin cmd timeout */
149
- nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
150
-
151
- return BLK_EH_DONE;
128
+ iod->req.execute(&iod->req);
152129 }
153130
154131 static blk_status_t nvme_loop_queue_rq(struct blk_mq_hw_ctx *hctx,
....@@ -179,8 +156,10 @@
179156 iod->sg_table.sgl = iod->first_sgl;
180157 if (sg_alloc_table_chained(&iod->sg_table,
181158 blk_rq_nr_phys_segments(req),
182
- iod->sg_table.sgl))
159
+ iod->sg_table.sgl, NVME_INLINE_SG_CNT)) {
160
+ nvme_cleanup_cmd(req);
183161 return BLK_STS_RESOURCE;
162
+ }
184163
185164 iod->req.sg = iod->sg_table.sgl;
186165 iod->req.sg_cnt = blk_rq_map_sg(req->q, req, iod->sg_table.sgl);
....@@ -215,7 +194,7 @@
215194 struct nvme_loop_iod *iod, unsigned int queue_idx)
216195 {
217196 iod->req.cmd = &iod->cmd;
218
- iod->req.rsp = &iod->rsp;
197
+ iod->req.cqe = &iod->cqe;
219198 iod->queue = &ctrl->queues[queue_idx];
220199 INIT_WORK(&iod->work, nvme_loop_execute_work);
221200 return 0;
....@@ -261,7 +240,6 @@
261240 .complete = nvme_loop_complete_rq,
262241 .init_request = nvme_loop_init_request,
263242 .init_hctx = nvme_loop_init_hctx,
264
- .timeout = nvme_loop_timeout,
265243 };
266244
267245 static const struct blk_mq_ops nvme_loop_admin_mq_ops = {
....@@ -269,7 +247,6 @@
269247 .complete = nvme_loop_complete_rq,
270248 .init_request = nvme_loop_init_request,
271249 .init_hctx = nvme_loop_init_admin_hctx,
272
- .timeout = nvme_loop_timeout,
273250 };
274251
275252 static void nvme_loop_destroy_admin_queue(struct nvme_loop_ctrl *ctrl)
....@@ -278,6 +255,7 @@
278255 return;
279256 nvmet_sq_destroy(&ctrl->queues[0].nvme_sq);
280257 blk_cleanup_queue(ctrl->ctrl.admin_q);
258
+ blk_cleanup_queue(ctrl->ctrl.fabrics_q);
281259 blk_mq_free_tag_set(&ctrl->admin_tag_set);
282260 }
283261
....@@ -347,7 +325,7 @@
347325 int i, ret;
348326
349327 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
350
- ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
328
+ ret = nvmf_connect_io_queue(&ctrl->ctrl, i, false);
351329 if (ret)
352330 return ret;
353331 set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags);
....@@ -364,9 +342,9 @@
364342 ctrl->admin_tag_set.ops = &nvme_loop_admin_mq_ops;
365343 ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH;
366344 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
367
- ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
345
+ ctrl->admin_tag_set.numa_node = ctrl->ctrl.numa_node;
368346 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_loop_iod) +
369
- SG_CHUNK_SIZE * sizeof(struct scatterlist);
347
+ NVME_INLINE_SG_CNT * sizeof(struct scatterlist);
370348 ctrl->admin_tag_set.driver_data = ctrl;
371349 ctrl->admin_tag_set.nr_hw_queues = 1;
372350 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
....@@ -383,10 +361,16 @@
383361 goto out_free_sq;
384362 ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set;
385363
364
+ 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
+
386370 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
387371 if (IS_ERR(ctrl->ctrl.admin_q)) {
388372 error = PTR_ERR(ctrl->ctrl.admin_q);
389
- goto out_free_tagset;
373
+ goto out_cleanup_fabrics_q;
390374 }
391375
392376 error = nvmf_connect_admin_queue(&ctrl->ctrl);
....@@ -395,22 +379,14 @@
395379
396380 set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags);
397381
398
- error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->ctrl.cap);
399
- if (error) {
400
- dev_err(ctrl->ctrl.device,
401
- "prop_get NVME_REG_CAP failed\n");
402
- goto out_cleanup_queue;
403
- }
404
-
405
- ctrl->ctrl.sqsize =
406
- min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
407
-
408
- error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
382
+ error = nvme_enable_ctrl(&ctrl->ctrl);
409383 if (error)
410384 goto out_cleanup_queue;
411385
412386 ctrl->ctrl.max_hw_sectors =
413387 (NVME_LOOP_MAX_SEGMENTS - 1) << (PAGE_SHIFT - 9);
388
+
389
+ blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
414390
415391 error = nvme_init_identify(&ctrl->ctrl);
416392 if (error)
....@@ -421,6 +397,8 @@
421397 out_cleanup_queue:
422398 clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags);
423399 blk_cleanup_queue(ctrl->ctrl.admin_q);
400
+out_cleanup_fabrics_q:
401
+ blk_cleanup_queue(ctrl->ctrl.fabrics_q);
424402 out_free_tagset:
425403 blk_mq_free_tag_set(&ctrl->admin_tag_set);
426404 out_free_sq:
....@@ -434,16 +412,17 @@
434412 nvme_stop_queues(&ctrl->ctrl);
435413 blk_mq_tagset_busy_iter(&ctrl->tag_set,
436414 nvme_cancel_request, &ctrl->ctrl);
415
+ blk_mq_tagset_wait_completed_request(&ctrl->tag_set);
437416 nvme_loop_destroy_io_queues(ctrl);
438417 }
439418
419
+ blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
440420 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
441421 nvme_shutdown_ctrl(&ctrl->ctrl);
442422
443
- blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
444423 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
445424 nvme_cancel_request, &ctrl->ctrl);
446
- blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
425
+ blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set);
447426 nvme_loop_destroy_admin_queue(ctrl);
448427 }
449428
....@@ -468,15 +447,16 @@
468447 {
469448 struct nvme_loop_ctrl *ctrl =
470449 container_of(work, struct nvme_loop_ctrl, ctrl.reset_work);
471
- bool changed;
472450 int ret;
473451
474452 nvme_stop_ctrl(&ctrl->ctrl);
475453 nvme_loop_shutdown_ctrl(ctrl);
476454
477455 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
478
- /* state change failure should never happen */
479
- WARN_ON_ONCE(1);
456
+ 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);
480460 return;
481461 }
482462
....@@ -495,8 +475,8 @@
495475 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
496476 ctrl->ctrl.queue_count - 1);
497477
498
- changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
499
- WARN_ON_ONCE(!changed);
478
+ if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE))
479
+ WARN_ON_ONCE(1);
500480
501481 nvme_start_ctrl(&ctrl->ctrl);
502482
....@@ -509,7 +489,6 @@
509489 out_disable:
510490 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
511491 nvme_uninit_ctrl(&ctrl->ctrl);
512
- nvme_put_ctrl(&ctrl->ctrl);
513492 }
514493
515494 static const struct nvme_ctrl_ops nvme_loop_ctrl_ops = {
....@@ -537,10 +516,10 @@
537516 ctrl->tag_set.ops = &nvme_loop_mq_ops;
538517 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
539518 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
540
- ctrl->tag_set.numa_node = NUMA_NO_NODE;
519
+ ctrl->tag_set.numa_node = ctrl->ctrl.numa_node;
541520 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
542521 ctrl->tag_set.cmd_size = sizeof(struct nvme_loop_iod) +
543
- SG_CHUNK_SIZE * sizeof(struct scatterlist);
522
+ NVME_INLINE_SG_CNT * sizeof(struct scatterlist);
544523 ctrl->tag_set.driver_data = ctrl;
545524 ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1;
546525 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
....@@ -592,7 +571,6 @@
592571 struct nvmf_ctrl_options *opts)
593572 {
594573 struct nvme_loop_ctrl *ctrl;
595
- bool changed;
596574 int ret;
597575
598576 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
....@@ -605,8 +583,13 @@
605583
606584 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_loop_ctrl_ops,
607585 0 /* no quirks, we're perfect! */);
608
- if (ret)
609
- goto out_put_ctrl;
586
+ 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);
610593
611594 ret = -ENOMEM;
612595
....@@ -642,10 +625,8 @@
642625 dev_info(ctrl->ctrl.device,
643626 "new ctrl: \"%s\"\n", ctrl->ctrl.opts->subsysnqn);
644627
645
- nvme_get_ctrl(&ctrl->ctrl);
646
-
647
- changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
648
- WARN_ON_ONCE(!changed);
628
+ if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE))
629
+ WARN_ON_ONCE(1);
649630
650631 mutex_lock(&nvme_loop_ctrl_mutex);
651632 list_add_tail(&ctrl->list, &nvme_loop_ctrl_list);
....@@ -661,8 +642,8 @@
661642 kfree(ctrl->queues);
662643 out_uninit_ctrl:
663644 nvme_uninit_ctrl(&ctrl->ctrl);
664
-out_put_ctrl:
665645 nvme_put_ctrl(&ctrl->ctrl);
646
+out:
666647 if (ret > 0)
667648 ret = -EIO;
668649 return ERR_PTR(ret);