Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 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> |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 16 | #include <linux/blk-mq.h> |
| 17 | #include <linux/nvme.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/parser.h> |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 20 | #include "nvmet.h" |
| 21 | #include "../host/nvme.h" |
| 22 | #include "../host/fabrics.h" |
| 23 | |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 24 | #define NVME_LOOP_MAX_SEGMENTS 256 |
| 25 | |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 26 | struct nvme_loop_iod { |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 27 | struct nvme_request nvme_req; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 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 { |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 38 | struct nvme_loop_queue *queues; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 39 | |
| 40 | struct blk_mq_tag_set admin_tag_set; |
| 41 | |
| 42 | struct list_head list; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 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; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 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 | |
Sagi Grimberg | 9d7fab0 | 2017-10-24 15:25:22 +0300 | [diff] [blame] | 55 | enum nvme_loop_queue_flags { |
| 56 | NVME_LOOP_Q_LIVE = 0, |
| 57 | }; |
| 58 | |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 59 | struct nvme_loop_queue { |
| 60 | struct nvmet_cq nvme_cq; |
| 61 | struct nvmet_sq nvme_sq; |
| 62 | struct nvme_loop_ctrl *ctrl; |
Sagi Grimberg | 9d7fab0 | 2017-10-24 15:25:22 +0300 | [diff] [blame] | 63 | unsigned long flags; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 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); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 84 | |
| 85 | nvme_cleanup_cmd(req); |
| 86 | sg_free_table_chained(&iod->sg_table, true); |
Christoph Hellwig | 77f02a7 | 2017-03-30 13:41:32 +0200 | [diff] [blame] | 87 | nvme_complete_rq(req); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 88 | } |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 89 | |
Sagi Grimberg | 3b06837 | 2017-02-27 18:28:25 +0200 | [diff] [blame] | 90 | static struct blk_mq_tags *nvme_loop_tagset(struct nvme_loop_queue *queue) |
| 91 | { |
| 92 | u32 queue_idx = nvme_loop_queue_idx(queue); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 93 | |
Sagi Grimberg | 3b06837 | 2017-02-27 18:28:25 +0200 | [diff] [blame] | 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]; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 99 | static void nvme_loop_queue_response(struct nvmet_req *req) |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 100 | { |
Sagi Grimberg | 3b06837 | 2017-02-27 18:28:25 +0200 | [diff] [blame] | 101 | struct nvme_loop_queue *queue = |
| 102 | container_of(req->sq, struct nvme_loop_queue, nvme_sq); |
| 103 | struct nvme_completion *cqe = req->rsp; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 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 | */ |
Sagi Grimberg | 3b06837 | 2017-02-27 18:28:25 +0200 | [diff] [blame] | 111 | if (unlikely(nvme_loop_queue_idx(queue) == 0 && |
Keith Busch | 38dabe2 | 2017-11-07 15:13:10 -0700 | [diff] [blame] | 112 | cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH)) { |
Sagi Grimberg | 3b06837 | 2017-02-27 18:28:25 +0200 | [diff] [blame] | 113 | nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status, |
Christoph Hellwig | 7bf5853 | 2016-11-10 07:32:34 -0800 | [diff] [blame] | 114 | &cqe->result); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 115 | } else { |
Sagi Grimberg | 3b06837 | 2017-02-27 18:28:25 +0200 | [diff] [blame] | 116 | struct request *rq; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 117 | |
Sagi Grimberg | 3b06837 | 2017-02-27 18:28:25 +0200 | [diff] [blame] | 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 | |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 126 | nvme_end_request(rq, cqe->status, cqe->result); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 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 | |
Christoph Hellwig | 5e62d5c | 2017-11-09 14:29:58 +0100 | [diff] [blame] | 135 | nvmet_req_execute(&iod->req); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 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 */ |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 144 | nvme_reset_ctrl(&iod->queue->ctrl->ctrl); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 145 | |
| 146 | /* fail with DNR on admin cmd timeout */ |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 147 | nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 148 | |
| 149 | return BLK_EH_HANDLED; |
| 150 | } |
| 151 | |
Sagi Grimberg | 9d7fab0 | 2017-10-24 15:25:22 +0300 | [diff] [blame] | 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 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 160 | static blk_status_t nvme_loop_queue_rq(struct blk_mq_hw_ctx *hctx, |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 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); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 167 | blk_status_t ret; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 168 | |
Sagi Grimberg | 9d7fab0 | 2017-10-24 15:25:22 +0300 | [diff] [blame] | 169 | ret = nvme_loop_is_ready(queue, req); |
| 170 | if (unlikely(ret)) |
| 171 | return ret; |
| 172 | |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 173 | ret = nvme_setup_cmd(ns, req, &iod->cmd); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 174 | if (ret) |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 175 | return ret; |
| 176 | |
| 177 | iod->cmd.common.flags |= NVME_CMD_SGL_METABUF; |
| 178 | iod->req.port = nvmet_loop_port; |
| 179 | if (!nvmet_req_init(&iod->req, &queue->nvme_cq, |
| 180 | &queue->nvme_sq, &nvme_loop_ops)) { |
| 181 | nvme_cleanup_cmd(req); |
| 182 | blk_mq_start_request(req); |
| 183 | nvme_loop_queue_response(&iod->req); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 184 | return BLK_STS_OK; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | if (blk_rq_bytes(req)) { |
| 188 | iod->sg_table.sgl = iod->first_sgl; |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 189 | if (sg_alloc_table_chained(&iod->sg_table, |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 190 | blk_rq_nr_phys_segments(req), |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 191 | iod->sg_table.sgl)) |
| 192 | return BLK_STS_RESOURCE; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 193 | |
| 194 | iod->req.sg = iod->sg_table.sgl; |
| 195 | iod->req.sg_cnt = blk_rq_map_sg(req->q, req, iod->sg_table.sgl); |
Christoph Hellwig | 5e62d5c | 2017-11-09 14:29:58 +0100 | [diff] [blame] | 196 | iod->req.transfer_len = blk_rq_bytes(req); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 199 | blk_mq_start_request(req); |
| 200 | |
| 201 | schedule_work(&iod->work); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 202 | return BLK_STS_OK; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Keith Busch | ad22c35 | 2017-11-07 15:13:12 -0700 | [diff] [blame] | 205 | static void nvme_loop_submit_async_event(struct nvme_ctrl *arg) |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 206 | { |
| 207 | struct nvme_loop_ctrl *ctrl = to_loop_ctrl(arg); |
| 208 | struct nvme_loop_queue *queue = &ctrl->queues[0]; |
| 209 | struct nvme_loop_iod *iod = &ctrl->async_event_iod; |
| 210 | |
| 211 | memset(&iod->cmd, 0, sizeof(iod->cmd)); |
| 212 | iod->cmd.common.opcode = nvme_admin_async_event; |
Keith Busch | 38dabe2 | 2017-11-07 15:13:10 -0700 | [diff] [blame] | 213 | iod->cmd.common.command_id = NVME_AQ_BLK_MQ_DEPTH; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 214 | iod->cmd.common.flags |= NVME_CMD_SGL_METABUF; |
| 215 | |
| 216 | if (!nvmet_req_init(&iod->req, &queue->nvme_cq, &queue->nvme_sq, |
| 217 | &nvme_loop_ops)) { |
| 218 | dev_err(ctrl->ctrl.device, "failed async event work\n"); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | schedule_work(&iod->work); |
| 223 | } |
| 224 | |
| 225 | static int nvme_loop_init_iod(struct nvme_loop_ctrl *ctrl, |
| 226 | struct nvme_loop_iod *iod, unsigned int queue_idx) |
| 227 | { |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 228 | iod->req.cmd = &iod->cmd; |
| 229 | iod->req.rsp = &iod->rsp; |
| 230 | iod->queue = &ctrl->queues[queue_idx]; |
| 231 | INIT_WORK(&iod->work, nvme_loop_execute_work); |
| 232 | return 0; |
| 233 | } |
| 234 | |
Christoph Hellwig | d6296d39 | 2017-05-01 10:19:08 -0600 | [diff] [blame] | 235 | static int nvme_loop_init_request(struct blk_mq_tag_set *set, |
| 236 | struct request *req, unsigned int hctx_idx, |
| 237 | unsigned int numa_node) |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 238 | { |
Christoph Hellwig | 62b83b1 | 2017-06-13 09:15:21 +0200 | [diff] [blame] | 239 | struct nvme_loop_ctrl *ctrl = set->driver_data; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 240 | |
Christoph Hellwig | 62b83b1 | 2017-06-13 09:15:21 +0200 | [diff] [blame] | 241 | return nvme_loop_init_iod(ctrl, blk_mq_rq_to_pdu(req), |
| 242 | (set == &ctrl->tag_set) ? hctx_idx + 1 : 0); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static int nvme_loop_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 246 | unsigned int hctx_idx) |
| 247 | { |
| 248 | struct nvme_loop_ctrl *ctrl = data; |
| 249 | struct nvme_loop_queue *queue = &ctrl->queues[hctx_idx + 1]; |
| 250 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 251 | BUG_ON(hctx_idx >= ctrl->ctrl.queue_count); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 252 | |
| 253 | hctx->driver_data = queue; |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int nvme_loop_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 258 | unsigned int hctx_idx) |
| 259 | { |
| 260 | struct nvme_loop_ctrl *ctrl = data; |
| 261 | struct nvme_loop_queue *queue = &ctrl->queues[0]; |
| 262 | |
| 263 | BUG_ON(hctx_idx != 0); |
| 264 | |
| 265 | hctx->driver_data = queue; |
| 266 | return 0; |
| 267 | } |
| 268 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 269 | static const struct blk_mq_ops nvme_loop_mq_ops = { |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 270 | .queue_rq = nvme_loop_queue_rq, |
| 271 | .complete = nvme_loop_complete_rq, |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 272 | .init_request = nvme_loop_init_request, |
| 273 | .init_hctx = nvme_loop_init_hctx, |
| 274 | .timeout = nvme_loop_timeout, |
| 275 | }; |
| 276 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 277 | static const struct blk_mq_ops nvme_loop_admin_mq_ops = { |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 278 | .queue_rq = nvme_loop_queue_rq, |
| 279 | .complete = nvme_loop_complete_rq, |
Christoph Hellwig | 62b83b1 | 2017-06-13 09:15:21 +0200 | [diff] [blame] | 280 | .init_request = nvme_loop_init_request, |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 281 | .init_hctx = nvme_loop_init_admin_hctx, |
| 282 | .timeout = nvme_loop_timeout, |
| 283 | }; |
| 284 | |
| 285 | static void nvme_loop_destroy_admin_queue(struct nvme_loop_ctrl *ctrl) |
| 286 | { |
Sagi Grimberg | 9d7fab0 | 2017-10-24 15:25:22 +0300 | [diff] [blame] | 287 | clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags); |
Sagi Grimberg | e4c5d37 | 2017-02-27 18:44:45 +0200 | [diff] [blame] | 288 | nvmet_sq_destroy(&ctrl->queues[0].nvme_sq); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 289 | blk_cleanup_queue(ctrl->ctrl.admin_q); |
| 290 | blk_mq_free_tag_set(&ctrl->admin_tag_set); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static 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); |
| 310 | free_ctrl: |
| 311 | kfree(ctrl); |
| 312 | } |
| 313 | |
Sagi Grimberg | 945dd5b | 2017-03-13 13:27:51 +0200 | [diff] [blame] | 314 | static void nvme_loop_destroy_io_queues(struct nvme_loop_ctrl *ctrl) |
| 315 | { |
| 316 | int i; |
| 317 | |
Sagi Grimberg | 9d7fab0 | 2017-10-24 15:25:22 +0300 | [diff] [blame] | 318 | for (i = 1; i < ctrl->ctrl.queue_count; i++) { |
| 319 | clear_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags); |
Sagi Grimberg | 945dd5b | 2017-03-13 13:27:51 +0200 | [diff] [blame] | 320 | nvmet_sq_destroy(&ctrl->queues[i].nvme_sq); |
Sagi Grimberg | 9d7fab0 | 2017-10-24 15:25:22 +0300 | [diff] [blame] | 321 | } |
Sagi Grimberg | 945dd5b | 2017-03-13 13:27:51 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static int nvme_loop_init_io_queues(struct nvme_loop_ctrl *ctrl) |
| 325 | { |
| 326 | struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; |
| 327 | unsigned int nr_io_queues; |
| 328 | int ret, i; |
| 329 | |
| 330 | nr_io_queues = min(opts->nr_io_queues, num_online_cpus()); |
| 331 | ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues); |
| 332 | if (ret || !nr_io_queues) |
| 333 | return ret; |
| 334 | |
| 335 | dev_info(ctrl->ctrl.device, "creating %d I/O queues.\n", nr_io_queues); |
| 336 | |
| 337 | for (i = 1; i <= nr_io_queues; i++) { |
| 338 | ctrl->queues[i].ctrl = ctrl; |
| 339 | ret = nvmet_sq_init(&ctrl->queues[i].nvme_sq); |
| 340 | if (ret) |
| 341 | goto out_destroy_queues; |
| 342 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 343 | ctrl->ctrl.queue_count++; |
Sagi Grimberg | 945dd5b | 2017-03-13 13:27:51 +0200 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | return 0; |
| 347 | |
| 348 | out_destroy_queues: |
| 349 | nvme_loop_destroy_io_queues(ctrl); |
| 350 | return ret; |
| 351 | } |
| 352 | |
Sagi Grimberg | 297186d | 2017-03-13 15:43:44 +0200 | [diff] [blame] | 353 | static int nvme_loop_connect_io_queues(struct nvme_loop_ctrl *ctrl) |
| 354 | { |
| 355 | int i, ret; |
| 356 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 357 | for (i = 1; i < ctrl->ctrl.queue_count; i++) { |
Sagi Grimberg | 297186d | 2017-03-13 15:43:44 +0200 | [diff] [blame] | 358 | ret = nvmf_connect_io_queue(&ctrl->ctrl, i); |
| 359 | if (ret) |
| 360 | return ret; |
Sagi Grimberg | 9d7fab0 | 2017-10-24 15:25:22 +0300 | [diff] [blame] | 361 | set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[i].flags); |
Sagi Grimberg | 297186d | 2017-03-13 15:43:44 +0200 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 367 | static int nvme_loop_configure_admin_queue(struct nvme_loop_ctrl *ctrl) |
| 368 | { |
| 369 | int error; |
| 370 | |
| 371 | memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set)); |
| 372 | ctrl->admin_tag_set.ops = &nvme_loop_admin_mq_ops; |
Keith Busch | 38dabe2 | 2017-11-07 15:13:10 -0700 | [diff] [blame] | 373 | ctrl->admin_tag_set.queue_depth = NVME_AQ_MQ_TAG_DEPTH; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 374 | ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */ |
| 375 | ctrl->admin_tag_set.numa_node = NUMA_NO_NODE; |
| 376 | ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_loop_iod) + |
| 377 | SG_CHUNK_SIZE * sizeof(struct scatterlist); |
| 378 | ctrl->admin_tag_set.driver_data = ctrl; |
| 379 | ctrl->admin_tag_set.nr_hw_queues = 1; |
| 380 | ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT; |
Israel Rukshin | 86f36b9 | 2017-10-18 12:38:26 +0000 | [diff] [blame] | 381 | ctrl->admin_tag_set.flags = BLK_MQ_F_NO_SCHED; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 382 | |
| 383 | ctrl->queues[0].ctrl = ctrl; |
| 384 | error = nvmet_sq_init(&ctrl->queues[0].nvme_sq); |
| 385 | if (error) |
| 386 | return error; |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 387 | ctrl->ctrl.queue_count = 1; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 388 | |
| 389 | error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set); |
| 390 | if (error) |
| 391 | goto out_free_sq; |
Sagi Grimberg | 34b6c23 | 2017-07-10 09:22:29 +0300 | [diff] [blame] | 392 | ctrl->ctrl.admin_tagset = &ctrl->admin_tag_set; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 393 | |
| 394 | ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set); |
| 395 | if (IS_ERR(ctrl->ctrl.admin_q)) { |
| 396 | error = PTR_ERR(ctrl->ctrl.admin_q); |
| 397 | goto out_free_tagset; |
| 398 | } |
| 399 | |
| 400 | error = nvmf_connect_admin_queue(&ctrl->ctrl); |
| 401 | if (error) |
| 402 | goto out_cleanup_queue; |
| 403 | |
Sagi Grimberg | 9d7fab0 | 2017-10-24 15:25:22 +0300 | [diff] [blame] | 404 | set_bit(NVME_LOOP_Q_LIVE, &ctrl->queues[0].flags); |
| 405 | |
Sagi Grimberg | 20d0dfe | 2017-06-27 22:16:38 +0300 | [diff] [blame] | 406 | error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->ctrl.cap); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 407 | if (error) { |
| 408 | dev_err(ctrl->ctrl.device, |
| 409 | "prop_get NVME_REG_CAP failed\n"); |
| 410 | goto out_cleanup_queue; |
| 411 | } |
| 412 | |
| 413 | ctrl->ctrl.sqsize = |
Sagi Grimberg | 20d0dfe | 2017-06-27 22:16:38 +0300 | [diff] [blame] | 414 | min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 415 | |
Sagi Grimberg | 20d0dfe | 2017-06-27 22:16:38 +0300 | [diff] [blame] | 416 | error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 417 | if (error) |
| 418 | goto out_cleanup_queue; |
| 419 | |
| 420 | ctrl->ctrl.max_hw_sectors = |
| 421 | (NVME_LOOP_MAX_SEGMENTS - 1) << (PAGE_SHIFT - 9); |
| 422 | |
| 423 | error = nvme_init_identify(&ctrl->ctrl); |
| 424 | if (error) |
| 425 | goto out_cleanup_queue; |
| 426 | |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 427 | return 0; |
| 428 | |
| 429 | out_cleanup_queue: |
| 430 | blk_cleanup_queue(ctrl->ctrl.admin_q); |
| 431 | out_free_tagset: |
| 432 | blk_mq_free_tag_set(&ctrl->admin_tag_set); |
| 433 | out_free_sq: |
| 434 | nvmet_sq_destroy(&ctrl->queues[0].nvme_sq); |
| 435 | return error; |
| 436 | } |
| 437 | |
| 438 | static void nvme_loop_shutdown_ctrl(struct nvme_loop_ctrl *ctrl) |
| 439 | { |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 440 | if (ctrl->ctrl.queue_count > 1) { |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 441 | nvme_stop_queues(&ctrl->ctrl); |
| 442 | blk_mq_tagset_busy_iter(&ctrl->tag_set, |
| 443 | nvme_cancel_request, &ctrl->ctrl); |
Sagi Grimberg | 945dd5b | 2017-03-13 13:27:51 +0200 | [diff] [blame] | 444 | nvme_loop_destroy_io_queues(ctrl); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | if (ctrl->ctrl.state == NVME_CTRL_LIVE) |
| 448 | nvme_shutdown_ctrl(&ctrl->ctrl); |
| 449 | |
Sagi Grimberg | c1c0fff | 2017-07-02 15:40:17 +0300 | [diff] [blame] | 450 | blk_mq_quiesce_queue(ctrl->ctrl.admin_q); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 451 | blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, |
| 452 | nvme_cancel_request, &ctrl->ctrl); |
Sagi Grimberg | c1c0fff | 2017-07-02 15:40:17 +0300 | [diff] [blame] | 453 | blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 454 | nvme_loop_destroy_admin_queue(ctrl); |
| 455 | } |
| 456 | |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 457 | static void nvme_loop_delete_ctrl_host(struct nvme_ctrl *ctrl) |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 458 | { |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 459 | nvme_loop_shutdown_ctrl(to_loop_ctrl(ctrl)); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static void nvme_loop_delete_ctrl(struct nvmet_ctrl *nctrl) |
| 463 | { |
| 464 | struct nvme_loop_ctrl *ctrl; |
| 465 | |
| 466 | mutex_lock(&nvme_loop_ctrl_mutex); |
| 467 | list_for_each_entry(ctrl, &nvme_loop_ctrl_list, list) { |
| 468 | if (ctrl->ctrl.cntlid == nctrl->cntlid) |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 469 | nvme_delete_ctrl(&ctrl->ctrl); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 470 | } |
| 471 | mutex_unlock(&nvme_loop_ctrl_mutex); |
| 472 | } |
| 473 | |
| 474 | static void nvme_loop_reset_ctrl_work(struct work_struct *work) |
| 475 | { |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 476 | struct nvme_loop_ctrl *ctrl = |
| 477 | container_of(work, struct nvme_loop_ctrl, ctrl.reset_work); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 478 | bool changed; |
Sagi Grimberg | 297186d | 2017-03-13 15:43:44 +0200 | [diff] [blame] | 479 | int ret; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 480 | |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 481 | nvme_stop_ctrl(&ctrl->ctrl); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 482 | nvme_loop_shutdown_ctrl(ctrl); |
| 483 | |
| 484 | ret = nvme_loop_configure_admin_queue(ctrl); |
| 485 | if (ret) |
| 486 | goto out_disable; |
| 487 | |
Sagi Grimberg | 945dd5b | 2017-03-13 13:27:51 +0200 | [diff] [blame] | 488 | ret = nvme_loop_init_io_queues(ctrl); |
| 489 | if (ret) |
| 490 | goto out_destroy_admin; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 491 | |
Sagi Grimberg | 297186d | 2017-03-13 15:43:44 +0200 | [diff] [blame] | 492 | ret = nvme_loop_connect_io_queues(ctrl); |
| 493 | if (ret) |
| 494 | goto out_destroy_io; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 495 | |
Sagi Grimberg | 4368c39 | 2017-06-29 11:13:43 +0300 | [diff] [blame] | 496 | blk_mq_update_nr_hw_queues(&ctrl->tag_set, |
| 497 | ctrl->ctrl.queue_count - 1); |
| 498 | |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 499 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); |
| 500 | WARN_ON_ONCE(!changed); |
| 501 | |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 502 | nvme_start_ctrl(&ctrl->ctrl); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 503 | |
| 504 | return; |
| 505 | |
Sagi Grimberg | 945dd5b | 2017-03-13 13:27:51 +0200 | [diff] [blame] | 506 | out_destroy_io: |
| 507 | nvme_loop_destroy_io_queues(ctrl); |
| 508 | out_destroy_admin: |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 509 | nvme_loop_destroy_admin_queue(ctrl); |
| 510 | out_disable: |
| 511 | dev_warn(ctrl->ctrl.device, "Removing after reset failure\n"); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 512 | nvme_uninit_ctrl(&ctrl->ctrl); |
| 513 | nvme_put_ctrl(&ctrl->ctrl); |
| 514 | } |
| 515 | |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 516 | static const struct nvme_ctrl_ops nvme_loop_ctrl_ops = { |
| 517 | .name = "loop", |
| 518 | .module = THIS_MODULE, |
Christoph Hellwig | d3d5b87 | 2017-05-20 15:14:44 +0200 | [diff] [blame] | 519 | .flags = NVME_F_FABRICS, |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 520 | .reg_read32 = nvmf_reg_read32, |
| 521 | .reg_read64 = nvmf_reg_read64, |
| 522 | .reg_write32 = nvmf_reg_write32, |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 523 | .free_ctrl = nvme_loop_free_ctrl, |
| 524 | .submit_async_event = nvme_loop_submit_async_event, |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 525 | .delete_ctrl = nvme_loop_delete_ctrl_host, |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 526 | }; |
| 527 | |
| 528 | static int nvme_loop_create_io_queues(struct nvme_loop_ctrl *ctrl) |
| 529 | { |
Sagi Grimberg | 297186d | 2017-03-13 15:43:44 +0200 | [diff] [blame] | 530 | int ret; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 531 | |
Sagi Grimberg | 945dd5b | 2017-03-13 13:27:51 +0200 | [diff] [blame] | 532 | ret = nvme_loop_init_io_queues(ctrl); |
| 533 | if (ret) |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 534 | return ret; |
| 535 | |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 536 | memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set)); |
| 537 | ctrl->tag_set.ops = &nvme_loop_mq_ops; |
Jay Freyensee | eadb7cf | 2016-08-17 15:00:28 -0700 | [diff] [blame] | 538 | ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 539 | ctrl->tag_set.reserved_tags = 1; /* fabric connect */ |
| 540 | ctrl->tag_set.numa_node = NUMA_NO_NODE; |
| 541 | ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; |
| 542 | ctrl->tag_set.cmd_size = sizeof(struct nvme_loop_iod) + |
| 543 | SG_CHUNK_SIZE * sizeof(struct scatterlist); |
| 544 | ctrl->tag_set.driver_data = ctrl; |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 545 | ctrl->tag_set.nr_hw_queues = ctrl->ctrl.queue_count - 1; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 546 | ctrl->tag_set.timeout = NVME_IO_TIMEOUT; |
| 547 | ctrl->ctrl.tagset = &ctrl->tag_set; |
| 548 | |
| 549 | ret = blk_mq_alloc_tag_set(&ctrl->tag_set); |
| 550 | if (ret) |
| 551 | goto out_destroy_queues; |
| 552 | |
| 553 | ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set); |
| 554 | if (IS_ERR(ctrl->ctrl.connect_q)) { |
| 555 | ret = PTR_ERR(ctrl->ctrl.connect_q); |
| 556 | goto out_free_tagset; |
| 557 | } |
| 558 | |
Sagi Grimberg | 297186d | 2017-03-13 15:43:44 +0200 | [diff] [blame] | 559 | ret = nvme_loop_connect_io_queues(ctrl); |
| 560 | if (ret) |
| 561 | goto out_cleanup_connect_q; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 562 | |
| 563 | return 0; |
| 564 | |
| 565 | out_cleanup_connect_q: |
| 566 | blk_cleanup_queue(ctrl->ctrl.connect_q); |
| 567 | out_free_tagset: |
| 568 | blk_mq_free_tag_set(&ctrl->tag_set); |
| 569 | out_destroy_queues: |
Sagi Grimberg | 945dd5b | 2017-03-13 13:27:51 +0200 | [diff] [blame] | 570 | nvme_loop_destroy_io_queues(ctrl); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 571 | return ret; |
| 572 | } |
| 573 | |
| 574 | static struct nvme_ctrl *nvme_loop_create_ctrl(struct device *dev, |
| 575 | struct nvmf_ctrl_options *opts) |
| 576 | { |
| 577 | struct nvme_loop_ctrl *ctrl; |
| 578 | bool changed; |
| 579 | int ret; |
| 580 | |
| 581 | ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); |
| 582 | if (!ctrl) |
| 583 | return ERR_PTR(-ENOMEM); |
| 584 | ctrl->ctrl.opts = opts; |
| 585 | INIT_LIST_HEAD(&ctrl->list); |
| 586 | |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 587 | INIT_WORK(&ctrl->ctrl.reset_work, nvme_loop_reset_ctrl_work); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 588 | |
| 589 | ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_loop_ctrl_ops, |
| 590 | 0 /* no quirks, we're perfect! */); |
| 591 | if (ret) |
| 592 | goto out_put_ctrl; |
| 593 | |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 594 | ret = -ENOMEM; |
| 595 | |
Jay Freyensee | eadb7cf | 2016-08-17 15:00:28 -0700 | [diff] [blame] | 596 | ctrl->ctrl.sqsize = opts->queue_size - 1; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 597 | ctrl->ctrl.kato = opts->kato; |
| 598 | |
| 599 | ctrl->queues = kcalloc(opts->nr_io_queues + 1, sizeof(*ctrl->queues), |
| 600 | GFP_KERNEL); |
| 601 | if (!ctrl->queues) |
| 602 | goto out_uninit_ctrl; |
| 603 | |
| 604 | ret = nvme_loop_configure_admin_queue(ctrl); |
| 605 | if (ret) |
| 606 | goto out_free_queues; |
| 607 | |
| 608 | if (opts->queue_size > ctrl->ctrl.maxcmd) { |
| 609 | /* warn if maxcmd is lower than queue_size */ |
| 610 | dev_warn(ctrl->ctrl.device, |
| 611 | "queue_size %zu > ctrl maxcmd %u, clamping down\n", |
| 612 | opts->queue_size, ctrl->ctrl.maxcmd); |
| 613 | opts->queue_size = ctrl->ctrl.maxcmd; |
| 614 | } |
| 615 | |
| 616 | if (opts->nr_io_queues) { |
| 617 | ret = nvme_loop_create_io_queues(ctrl); |
| 618 | if (ret) |
| 619 | goto out_remove_admin_queue; |
| 620 | } |
| 621 | |
| 622 | nvme_loop_init_iod(ctrl, &ctrl->async_event_iod, 0); |
| 623 | |
| 624 | dev_info(ctrl->ctrl.device, |
| 625 | "new ctrl: \"%s\"\n", ctrl->ctrl.opts->subsysnqn); |
| 626 | |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 627 | nvme_get_ctrl(&ctrl->ctrl); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 628 | |
| 629 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); |
| 630 | WARN_ON_ONCE(!changed); |
| 631 | |
| 632 | mutex_lock(&nvme_loop_ctrl_mutex); |
| 633 | list_add_tail(&ctrl->list, &nvme_loop_ctrl_list); |
| 634 | mutex_unlock(&nvme_loop_ctrl_mutex); |
| 635 | |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 636 | nvme_start_ctrl(&ctrl->ctrl); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 637 | |
| 638 | return &ctrl->ctrl; |
| 639 | |
| 640 | out_remove_admin_queue: |
| 641 | nvme_loop_destroy_admin_queue(ctrl); |
| 642 | out_free_queues: |
| 643 | kfree(ctrl->queues); |
| 644 | out_uninit_ctrl: |
| 645 | nvme_uninit_ctrl(&ctrl->ctrl); |
| 646 | out_put_ctrl: |
| 647 | nvme_put_ctrl(&ctrl->ctrl); |
| 648 | if (ret > 0) |
| 649 | ret = -EIO; |
| 650 | return ERR_PTR(ret); |
| 651 | } |
| 652 | |
| 653 | static int nvme_loop_add_port(struct nvmet_port *port) |
| 654 | { |
| 655 | /* |
| 656 | * XXX: disalow adding more than one port so |
| 657 | * there is no connection rejections when a |
| 658 | * a subsystem is assigned to a port for which |
| 659 | * loop doesn't have a pointer. |
| 660 | * This scenario would be possible if we allowed |
| 661 | * more than one port to be added and a subsystem |
| 662 | * was assigned to a port other than nvmet_loop_port. |
| 663 | */ |
| 664 | |
| 665 | if (nvmet_loop_port) |
| 666 | return -EPERM; |
| 667 | |
| 668 | nvmet_loop_port = port; |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | static void nvme_loop_remove_port(struct nvmet_port *port) |
| 673 | { |
| 674 | if (port == nvmet_loop_port) |
| 675 | nvmet_loop_port = NULL; |
| 676 | } |
| 677 | |
| 678 | static struct nvmet_fabrics_ops nvme_loop_ops = { |
| 679 | .owner = THIS_MODULE, |
| 680 | .type = NVMF_TRTYPE_LOOP, |
| 681 | .add_port = nvme_loop_add_port, |
| 682 | .remove_port = nvme_loop_remove_port, |
| 683 | .queue_response = nvme_loop_queue_response, |
| 684 | .delete_ctrl = nvme_loop_delete_ctrl, |
| 685 | }; |
| 686 | |
| 687 | static struct nvmf_transport_ops nvme_loop_transport = { |
| 688 | .name = "loop", |
Roy Shterman | 0de5cd3 | 2017-12-25 14:18:30 +0200 | [diff] [blame] | 689 | .module = THIS_MODULE, |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 690 | .create_ctrl = nvme_loop_create_ctrl, |
| 691 | }; |
| 692 | |
| 693 | static int __init nvme_loop_init_module(void) |
| 694 | { |
| 695 | int ret; |
| 696 | |
| 697 | ret = nvmet_register_transport(&nvme_loop_ops); |
| 698 | if (ret) |
| 699 | return ret; |
Sagi Grimberg | d19eef0 | 2017-03-19 06:26:28 +0200 | [diff] [blame] | 700 | |
| 701 | ret = nvmf_register_transport(&nvme_loop_transport); |
| 702 | if (ret) |
| 703 | nvmet_unregister_transport(&nvme_loop_ops); |
| 704 | |
| 705 | return ret; |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | static void __exit nvme_loop_cleanup_module(void) |
| 709 | { |
| 710 | struct nvme_loop_ctrl *ctrl, *next; |
| 711 | |
| 712 | nvmf_unregister_transport(&nvme_loop_transport); |
| 713 | nvmet_unregister_transport(&nvme_loop_ops); |
| 714 | |
| 715 | mutex_lock(&nvme_loop_ctrl_mutex); |
| 716 | list_for_each_entry_safe(ctrl, next, &nvme_loop_ctrl_list, list) |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 717 | nvme_delete_ctrl(&ctrl->ctrl); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 718 | mutex_unlock(&nvme_loop_ctrl_mutex); |
| 719 | |
Roy Shterman | b227c59 | 2018-01-14 12:39:02 +0200 | [diff] [blame] | 720 | flush_workqueue(nvme_delete_wq); |
Christoph Hellwig | 3a85a5d | 2016-06-21 18:04:21 +0200 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | module_init(nvme_loop_init_module); |
| 724 | module_exit(nvme_loop_cleanup_module); |
| 725 | |
| 726 | MODULE_LICENSE("GPL v2"); |
| 727 | MODULE_ALIAS("nvmet-transport-254"); /* 254 == NVMF_TRTYPE_LOOP */ |