Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1 | /* |
| 2 | * NVMe over Fabrics RDMA host code. |
| 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 |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/string.h> |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 20 | #include <linux/atomic.h> |
| 21 | #include <linux/blk-mq.h> |
Sagi Grimberg | 0b36658 | 2017-07-13 11:09:44 +0300 | [diff] [blame] | 22 | #include <linux/blk-mq-rdma.h> |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 23 | #include <linux/types.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/mutex.h> |
| 26 | #include <linux/scatterlist.h> |
| 27 | #include <linux/nvme.h> |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
| 29 | |
| 30 | #include <rdma/ib_verbs.h> |
| 31 | #include <rdma/rdma_cm.h> |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 32 | #include <linux/nvme-rdma.h> |
| 33 | |
| 34 | #include "nvme.h" |
| 35 | #include "fabrics.h" |
| 36 | |
| 37 | |
Sagi Grimberg | 782d820 | 2017-03-21 16:32:38 +0200 | [diff] [blame] | 38 | #define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */ |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 39 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 40 | #define NVME_RDMA_MAX_SEGMENTS 256 |
| 41 | |
| 42 | #define NVME_RDMA_MAX_INLINE_SEGMENTS 1 |
| 43 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 44 | /* |
| 45 | * We handle AEN commands ourselves and don't even let the |
| 46 | * block layer know about them. |
| 47 | */ |
| 48 | #define NVME_RDMA_NR_AEN_COMMANDS 1 |
| 49 | #define NVME_RDMA_AQ_BLKMQ_DEPTH \ |
Sagi Grimberg | 7aa1f42 | 2017-06-18 16:15:59 +0300 | [diff] [blame] | 50 | (NVME_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 51 | |
| 52 | struct nvme_rdma_device { |
Max Gurtovoy | f87c89a | 2017-10-23 12:59:27 +0300 | [diff] [blame] | 53 | struct ib_device *dev; |
| 54 | struct ib_pd *pd; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 55 | struct kref ref; |
| 56 | struct list_head entry; |
| 57 | }; |
| 58 | |
| 59 | struct nvme_rdma_qe { |
| 60 | struct ib_cqe cqe; |
| 61 | void *data; |
| 62 | u64 dma; |
| 63 | }; |
| 64 | |
| 65 | struct nvme_rdma_queue; |
| 66 | struct nvme_rdma_request { |
Christoph Hellwig | d49187e | 2016-11-10 07:32:33 -0800 | [diff] [blame] | 67 | struct nvme_request req; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 68 | struct ib_mr *mr; |
| 69 | struct nvme_rdma_qe sqe; |
| 70 | struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS]; |
| 71 | u32 num_sge; |
| 72 | int nents; |
| 73 | bool inline_data; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 74 | struct ib_reg_wr reg_wr; |
| 75 | struct ib_cqe reg_cqe; |
| 76 | struct nvme_rdma_queue *queue; |
| 77 | struct sg_table sg_table; |
| 78 | struct scatterlist first_sgl[]; |
| 79 | }; |
| 80 | |
| 81 | enum nvme_rdma_queue_flags { |
Sagi Grimberg | 5013e98 | 2017-10-11 15:29:12 +0300 | [diff] [blame] | 82 | NVME_RDMA_Q_ALLOCATED = 0, |
| 83 | NVME_RDMA_Q_LIVE = 1, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | struct nvme_rdma_queue { |
| 87 | struct nvme_rdma_qe *rsp_ring; |
Marta Rybczynska | 5e599d7 | 2017-06-06 13:27:21 +0200 | [diff] [blame] | 88 | atomic_t sig_count; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 89 | int queue_size; |
| 90 | size_t cmnd_capsule_len; |
| 91 | struct nvme_rdma_ctrl *ctrl; |
| 92 | struct nvme_rdma_device *device; |
| 93 | struct ib_cq *ib_cq; |
| 94 | struct ib_qp *qp; |
| 95 | |
| 96 | unsigned long flags; |
| 97 | struct rdma_cm_id *cm_id; |
| 98 | int cm_error; |
| 99 | struct completion cm_done; |
| 100 | }; |
| 101 | |
| 102 | struct nvme_rdma_ctrl { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 103 | /* read only in the hot path */ |
| 104 | struct nvme_rdma_queue *queues; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 105 | |
| 106 | /* other member variables */ |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 107 | struct blk_mq_tag_set tag_set; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 108 | struct work_struct err_work; |
| 109 | |
| 110 | struct nvme_rdma_qe async_event_sqe; |
| 111 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 112 | struct delayed_work reconnect_work; |
| 113 | |
| 114 | struct list_head list; |
| 115 | |
| 116 | struct blk_mq_tag_set admin_tag_set; |
| 117 | struct nvme_rdma_device *device; |
| 118 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 119 | u32 max_fr_pages; |
| 120 | |
Sagi Grimberg | 0928f9b | 2017-02-05 21:49:32 +0200 | [diff] [blame] | 121 | struct sockaddr_storage addr; |
| 122 | struct sockaddr_storage src_addr; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 123 | |
| 124 | struct nvme_ctrl ctrl; |
| 125 | }; |
| 126 | |
| 127 | static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl) |
| 128 | { |
| 129 | return container_of(ctrl, struct nvme_rdma_ctrl, ctrl); |
| 130 | } |
| 131 | |
| 132 | static LIST_HEAD(device_list); |
| 133 | static DEFINE_MUTEX(device_list_mutex); |
| 134 | |
| 135 | static LIST_HEAD(nvme_rdma_ctrl_list); |
| 136 | static DEFINE_MUTEX(nvme_rdma_ctrl_mutex); |
| 137 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 138 | /* |
| 139 | * Disabling this option makes small I/O goes faster, but is fundamentally |
| 140 | * unsafe. With it turned off we will have to register a global rkey that |
| 141 | * allows read and write access to all physical memory. |
| 142 | */ |
| 143 | static bool register_always = true; |
| 144 | module_param(register_always, bool, 0444); |
| 145 | MODULE_PARM_DESC(register_always, |
| 146 | "Use memory registration even for contiguous memory regions"); |
| 147 | |
| 148 | static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, |
| 149 | struct rdma_cm_event *event); |
| 150 | static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 151 | |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 152 | static const struct blk_mq_ops nvme_rdma_mq_ops; |
| 153 | static const struct blk_mq_ops nvme_rdma_admin_mq_ops; |
| 154 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 155 | /* XXX: really should move to a generic header sooner or later.. */ |
| 156 | static inline void put_unaligned_le24(u32 val, u8 *p) |
| 157 | { |
| 158 | *p++ = val; |
| 159 | *p++ = val >> 8; |
| 160 | *p++ = val >> 16; |
| 161 | } |
| 162 | |
| 163 | static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue) |
| 164 | { |
| 165 | return queue - queue->ctrl->queues; |
| 166 | } |
| 167 | |
| 168 | static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue) |
| 169 | { |
| 170 | return queue->cmnd_capsule_len - sizeof(struct nvme_command); |
| 171 | } |
| 172 | |
| 173 | static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe, |
| 174 | size_t capsule_size, enum dma_data_direction dir) |
| 175 | { |
| 176 | ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir); |
| 177 | kfree(qe->data); |
| 178 | } |
| 179 | |
| 180 | static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe, |
| 181 | size_t capsule_size, enum dma_data_direction dir) |
| 182 | { |
| 183 | qe->data = kzalloc(capsule_size, GFP_KERNEL); |
| 184 | if (!qe->data) |
| 185 | return -ENOMEM; |
| 186 | |
| 187 | qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir); |
| 188 | if (ib_dma_mapping_error(ibdev, qe->dma)) { |
| 189 | kfree(qe->data); |
| 190 | return -ENOMEM; |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static void nvme_rdma_free_ring(struct ib_device *ibdev, |
| 197 | struct nvme_rdma_qe *ring, size_t ib_queue_size, |
| 198 | size_t capsule_size, enum dma_data_direction dir) |
| 199 | { |
| 200 | int i; |
| 201 | |
| 202 | for (i = 0; i < ib_queue_size; i++) |
| 203 | nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir); |
| 204 | kfree(ring); |
| 205 | } |
| 206 | |
| 207 | static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev, |
| 208 | size_t ib_queue_size, size_t capsule_size, |
| 209 | enum dma_data_direction dir) |
| 210 | { |
| 211 | struct nvme_rdma_qe *ring; |
| 212 | int i; |
| 213 | |
| 214 | ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL); |
| 215 | if (!ring) |
| 216 | return NULL; |
| 217 | |
| 218 | for (i = 0; i < ib_queue_size; i++) { |
| 219 | if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir)) |
| 220 | goto out_free_ring; |
| 221 | } |
| 222 | |
| 223 | return ring; |
| 224 | |
| 225 | out_free_ring: |
| 226 | nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir); |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | static void nvme_rdma_qp_event(struct ib_event *event, void *context) |
| 231 | { |
Max Gurtovoy | 27a4bee | 2016-11-23 11:38:48 +0200 | [diff] [blame] | 232 | pr_debug("QP event %s (%d)\n", |
| 233 | ib_event_msg(event->event), event->event); |
| 234 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue) |
| 238 | { |
| 239 | wait_for_completion_interruptible_timeout(&queue->cm_done, |
| 240 | msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1); |
| 241 | return queue->cm_error; |
| 242 | } |
| 243 | |
| 244 | static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor) |
| 245 | { |
| 246 | struct nvme_rdma_device *dev = queue->device; |
| 247 | struct ib_qp_init_attr init_attr; |
| 248 | int ret; |
| 249 | |
| 250 | memset(&init_attr, 0, sizeof(init_attr)); |
| 251 | init_attr.event_handler = nvme_rdma_qp_event; |
| 252 | /* +1 for drain */ |
| 253 | init_attr.cap.max_send_wr = factor * queue->queue_size + 1; |
| 254 | /* +1 for drain */ |
| 255 | init_attr.cap.max_recv_wr = queue->queue_size + 1; |
| 256 | init_attr.cap.max_recv_sge = 1; |
| 257 | init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS; |
| 258 | init_attr.sq_sig_type = IB_SIGNAL_REQ_WR; |
| 259 | init_attr.qp_type = IB_QPT_RC; |
| 260 | init_attr.send_cq = queue->ib_cq; |
| 261 | init_attr.recv_cq = queue->ib_cq; |
| 262 | |
| 263 | ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr); |
| 264 | |
| 265 | queue->qp = queue->cm_id->qp; |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | static int nvme_rdma_reinit_request(void *data, struct request *rq) |
| 270 | { |
| 271 | struct nvme_rdma_ctrl *ctrl = data; |
| 272 | struct nvme_rdma_device *dev = ctrl->device; |
| 273 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 274 | int ret = 0; |
| 275 | |
Sagi Grimberg | 0fc176d | 2017-10-11 15:29:09 +0300 | [diff] [blame] | 276 | if (WARN_ON_ONCE(!req->mr)) |
| 277 | return 0; |
| 278 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 279 | ib_dereg_mr(req->mr); |
| 280 | |
| 281 | req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG, |
| 282 | ctrl->max_fr_pages); |
| 283 | if (IS_ERR(req->mr)) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 284 | ret = PTR_ERR(req->mr); |
Wei Yongjun | 458a963 | 2016-07-12 11:06:17 +0000 | [diff] [blame] | 285 | req->mr = NULL; |
Colin Ian King | 1bda18d | 2016-09-05 16:24:38 +0100 | [diff] [blame] | 286 | goto out; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 287 | } |
| 288 | |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 289 | req->mr->need_inval = false; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 290 | |
| 291 | out: |
| 292 | return ret; |
| 293 | } |
| 294 | |
Christoph Hellwig | 385475e | 2017-06-13 09:15:19 +0200 | [diff] [blame] | 295 | static void nvme_rdma_exit_request(struct blk_mq_tag_set *set, |
| 296 | struct request *rq, unsigned int hctx_idx) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 297 | { |
Christoph Hellwig | 385475e | 2017-06-13 09:15:19 +0200 | [diff] [blame] | 298 | struct nvme_rdma_ctrl *ctrl = set->driver_data; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 299 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
Christoph Hellwig | 385475e | 2017-06-13 09:15:19 +0200 | [diff] [blame] | 300 | int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 301 | struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx]; |
| 302 | struct nvme_rdma_device *dev = queue->device; |
| 303 | |
| 304 | if (req->mr) |
| 305 | ib_dereg_mr(req->mr); |
| 306 | |
| 307 | nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command), |
| 308 | DMA_TO_DEVICE); |
| 309 | } |
| 310 | |
Christoph Hellwig | 385475e | 2017-06-13 09:15:19 +0200 | [diff] [blame] | 311 | static int nvme_rdma_init_request(struct blk_mq_tag_set *set, |
| 312 | struct request *rq, unsigned int hctx_idx, |
| 313 | unsigned int numa_node) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 314 | { |
Christoph Hellwig | 385475e | 2017-06-13 09:15:19 +0200 | [diff] [blame] | 315 | struct nvme_rdma_ctrl *ctrl = set->driver_data; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 316 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
Christoph Hellwig | 385475e | 2017-06-13 09:15:19 +0200 | [diff] [blame] | 317 | int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 318 | struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx]; |
| 319 | struct nvme_rdma_device *dev = queue->device; |
| 320 | struct ib_device *ibdev = dev->dev; |
| 321 | int ret; |
| 322 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 323 | ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command), |
| 324 | DMA_TO_DEVICE); |
| 325 | if (ret) |
| 326 | return ret; |
| 327 | |
| 328 | req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG, |
| 329 | ctrl->max_fr_pages); |
| 330 | if (IS_ERR(req->mr)) { |
| 331 | ret = PTR_ERR(req->mr); |
| 332 | goto out_free_qe; |
| 333 | } |
| 334 | |
| 335 | req->queue = queue; |
| 336 | |
| 337 | return 0; |
| 338 | |
| 339 | out_free_qe: |
| 340 | nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command), |
| 341 | DMA_TO_DEVICE); |
| 342 | return -ENOMEM; |
| 343 | } |
| 344 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 345 | static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 346 | unsigned int hctx_idx) |
| 347 | { |
| 348 | struct nvme_rdma_ctrl *ctrl = data; |
| 349 | struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1]; |
| 350 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 351 | BUG_ON(hctx_idx >= ctrl->ctrl.queue_count); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 352 | |
| 353 | hctx->driver_data = queue; |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 358 | unsigned int hctx_idx) |
| 359 | { |
| 360 | struct nvme_rdma_ctrl *ctrl = data; |
| 361 | struct nvme_rdma_queue *queue = &ctrl->queues[0]; |
| 362 | |
| 363 | BUG_ON(hctx_idx != 0); |
| 364 | |
| 365 | hctx->driver_data = queue; |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static void nvme_rdma_free_dev(struct kref *ref) |
| 370 | { |
| 371 | struct nvme_rdma_device *ndev = |
| 372 | container_of(ref, struct nvme_rdma_device, ref); |
| 373 | |
| 374 | mutex_lock(&device_list_mutex); |
| 375 | list_del(&ndev->entry); |
| 376 | mutex_unlock(&device_list_mutex); |
| 377 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 378 | ib_dealloc_pd(ndev->pd); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 379 | kfree(ndev); |
| 380 | } |
| 381 | |
| 382 | static void nvme_rdma_dev_put(struct nvme_rdma_device *dev) |
| 383 | { |
| 384 | kref_put(&dev->ref, nvme_rdma_free_dev); |
| 385 | } |
| 386 | |
| 387 | static int nvme_rdma_dev_get(struct nvme_rdma_device *dev) |
| 388 | { |
| 389 | return kref_get_unless_zero(&dev->ref); |
| 390 | } |
| 391 | |
| 392 | static struct nvme_rdma_device * |
| 393 | nvme_rdma_find_get_device(struct rdma_cm_id *cm_id) |
| 394 | { |
| 395 | struct nvme_rdma_device *ndev; |
| 396 | |
| 397 | mutex_lock(&device_list_mutex); |
| 398 | list_for_each_entry(ndev, &device_list, entry) { |
| 399 | if (ndev->dev->node_guid == cm_id->device->node_guid && |
| 400 | nvme_rdma_dev_get(ndev)) |
| 401 | goto out_unlock; |
| 402 | } |
| 403 | |
| 404 | ndev = kzalloc(sizeof(*ndev), GFP_KERNEL); |
| 405 | if (!ndev) |
| 406 | goto out_err; |
| 407 | |
| 408 | ndev->dev = cm_id->device; |
| 409 | kref_init(&ndev->ref); |
| 410 | |
Christoph Hellwig | 11975e0 | 2016-09-05 12:56:20 +0200 | [diff] [blame] | 411 | ndev->pd = ib_alloc_pd(ndev->dev, |
| 412 | register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 413 | if (IS_ERR(ndev->pd)) |
| 414 | goto out_free_dev; |
| 415 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 416 | if (!(ndev->dev->attrs.device_cap_flags & |
| 417 | IB_DEVICE_MEM_MGT_EXTENSIONS)) { |
| 418 | dev_err(&ndev->dev->dev, |
| 419 | "Memory registrations not supported.\n"); |
Christoph Hellwig | 11975e0 | 2016-09-05 12:56:20 +0200 | [diff] [blame] | 420 | goto out_free_pd; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | list_add(&ndev->entry, &device_list); |
| 424 | out_unlock: |
| 425 | mutex_unlock(&device_list_mutex); |
| 426 | return ndev; |
| 427 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 428 | out_free_pd: |
| 429 | ib_dealloc_pd(ndev->pd); |
| 430 | out_free_dev: |
| 431 | kfree(ndev); |
| 432 | out_err: |
| 433 | mutex_unlock(&device_list_mutex); |
| 434 | return NULL; |
| 435 | } |
| 436 | |
| 437 | static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue) |
| 438 | { |
Sagi Grimberg | 0c5b43b | 2017-10-11 15:29:08 +0300 | [diff] [blame] | 439 | struct nvme_rdma_device *dev = queue->device; |
| 440 | struct ib_device *ibdev = dev->dev; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 441 | |
| 442 | rdma_destroy_qp(queue->cm_id); |
| 443 | ib_free_cq(queue->ib_cq); |
| 444 | |
| 445 | nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size, |
| 446 | sizeof(struct nvme_completion), DMA_FROM_DEVICE); |
| 447 | |
| 448 | nvme_rdma_dev_put(dev); |
| 449 | } |
| 450 | |
Sagi Grimberg | ca6e95b | 2017-05-04 13:33:09 +0300 | [diff] [blame] | 451 | static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 452 | { |
Sagi Grimberg | ca6e95b | 2017-05-04 13:33:09 +0300 | [diff] [blame] | 453 | struct ib_device *ibdev; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 454 | const int send_wr_factor = 3; /* MR, SEND, INV */ |
| 455 | const int cq_factor = send_wr_factor + 1; /* + RECV */ |
| 456 | int comp_vector, idx = nvme_rdma_queue_idx(queue); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 457 | int ret; |
| 458 | |
Sagi Grimberg | ca6e95b | 2017-05-04 13:33:09 +0300 | [diff] [blame] | 459 | queue->device = nvme_rdma_find_get_device(queue->cm_id); |
| 460 | if (!queue->device) { |
| 461 | dev_err(queue->cm_id->device->dev.parent, |
| 462 | "no client data found!\n"); |
| 463 | return -ECONNREFUSED; |
| 464 | } |
| 465 | ibdev = queue->device->dev; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 466 | |
| 467 | /* |
Sagi Grimberg | 0b36658 | 2017-07-13 11:09:44 +0300 | [diff] [blame] | 468 | * Spread I/O queues completion vectors according their queue index. |
| 469 | * Admin queues can always go on completion vector 0. |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 470 | */ |
Sagi Grimberg | 0b36658 | 2017-07-13 11:09:44 +0300 | [diff] [blame] | 471 | comp_vector = idx == 0 ? idx : idx - 1; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 472 | |
| 473 | /* +1 for ib_stop_cq */ |
Sagi Grimberg | ca6e95b | 2017-05-04 13:33:09 +0300 | [diff] [blame] | 474 | queue->ib_cq = ib_alloc_cq(ibdev, queue, |
| 475 | cq_factor * queue->queue_size + 1, |
| 476 | comp_vector, IB_POLL_SOFTIRQ); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 477 | if (IS_ERR(queue->ib_cq)) { |
| 478 | ret = PTR_ERR(queue->ib_cq); |
Sagi Grimberg | ca6e95b | 2017-05-04 13:33:09 +0300 | [diff] [blame] | 479 | goto out_put_dev; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | ret = nvme_rdma_create_qp(queue, send_wr_factor); |
| 483 | if (ret) |
| 484 | goto out_destroy_ib_cq; |
| 485 | |
| 486 | queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size, |
| 487 | sizeof(struct nvme_completion), DMA_FROM_DEVICE); |
| 488 | if (!queue->rsp_ring) { |
| 489 | ret = -ENOMEM; |
| 490 | goto out_destroy_qp; |
| 491 | } |
| 492 | |
| 493 | return 0; |
| 494 | |
| 495 | out_destroy_qp: |
Max Gurtovoy | 1f61def | 2017-11-06 16:18:51 +0200 | [diff] [blame^] | 496 | rdma_destroy_qp(queue->cm_id); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 497 | out_destroy_ib_cq: |
| 498 | ib_free_cq(queue->ib_cq); |
Sagi Grimberg | ca6e95b | 2017-05-04 13:33:09 +0300 | [diff] [blame] | 499 | out_put_dev: |
| 500 | nvme_rdma_dev_put(queue->device); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 501 | return ret; |
| 502 | } |
| 503 | |
Sagi Grimberg | 41e8cfa | 2017-07-10 09:22:36 +0300 | [diff] [blame] | 504 | static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 505 | int idx, size_t queue_size) |
| 506 | { |
| 507 | struct nvme_rdma_queue *queue; |
Max Gurtovoy | 8f4e8da | 2017-02-19 20:08:03 +0200 | [diff] [blame] | 508 | struct sockaddr *src_addr = NULL; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 509 | int ret; |
| 510 | |
| 511 | queue = &ctrl->queues[idx]; |
| 512 | queue->ctrl = ctrl; |
| 513 | init_completion(&queue->cm_done); |
| 514 | |
| 515 | if (idx > 0) |
| 516 | queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16; |
| 517 | else |
| 518 | queue->cmnd_capsule_len = sizeof(struct nvme_command); |
| 519 | |
| 520 | queue->queue_size = queue_size; |
Marta Rybczynska | 5e599d7 | 2017-06-06 13:27:21 +0200 | [diff] [blame] | 521 | atomic_set(&queue->sig_count, 0); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 522 | |
| 523 | queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue, |
| 524 | RDMA_PS_TCP, IB_QPT_RC); |
| 525 | if (IS_ERR(queue->cm_id)) { |
| 526 | dev_info(ctrl->ctrl.device, |
| 527 | "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id)); |
| 528 | return PTR_ERR(queue->cm_id); |
| 529 | } |
| 530 | |
Max Gurtovoy | 8f4e8da | 2017-02-19 20:08:03 +0200 | [diff] [blame] | 531 | if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR) |
Sagi Grimberg | 0928f9b | 2017-02-05 21:49:32 +0200 | [diff] [blame] | 532 | src_addr = (struct sockaddr *)&ctrl->src_addr; |
Max Gurtovoy | 8f4e8da | 2017-02-19 20:08:03 +0200 | [diff] [blame] | 533 | |
Sagi Grimberg | 0928f9b | 2017-02-05 21:49:32 +0200 | [diff] [blame] | 534 | queue->cm_error = -ETIMEDOUT; |
| 535 | ret = rdma_resolve_addr(queue->cm_id, src_addr, |
| 536 | (struct sockaddr *)&ctrl->addr, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 537 | NVME_RDMA_CONNECT_TIMEOUT_MS); |
| 538 | if (ret) { |
| 539 | dev_info(ctrl->ctrl.device, |
| 540 | "rdma_resolve_addr failed (%d).\n", ret); |
| 541 | goto out_destroy_cm_id; |
| 542 | } |
| 543 | |
| 544 | ret = nvme_rdma_wait_for_cm(queue); |
| 545 | if (ret) { |
| 546 | dev_info(ctrl->ctrl.device, |
Sagi Grimberg | d8bfcee | 2017-10-11 15:29:07 +0300 | [diff] [blame] | 547 | "rdma connection establishment failed (%d)\n", ret); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 548 | goto out_destroy_cm_id; |
| 549 | } |
| 550 | |
Sagi Grimberg | 5013e98 | 2017-10-11 15:29:12 +0300 | [diff] [blame] | 551 | set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 552 | |
| 553 | return 0; |
| 554 | |
| 555 | out_destroy_cm_id: |
| 556 | rdma_destroy_id(queue->cm_id); |
| 557 | return ret; |
| 558 | } |
| 559 | |
| 560 | static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue) |
| 561 | { |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 562 | if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags)) |
| 563 | return; |
| 564 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 565 | rdma_disconnect(queue->cm_id); |
| 566 | ib_drain_qp(queue->qp); |
| 567 | } |
| 568 | |
| 569 | static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue) |
| 570 | { |
Sagi Grimberg | 5013e98 | 2017-10-11 15:29:12 +0300 | [diff] [blame] | 571 | if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags)) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 572 | return; |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 573 | |
| 574 | nvme_rdma_destroy_queue_ib(queue); |
| 575 | rdma_destroy_id(queue->cm_id); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl) |
| 579 | { |
| 580 | int i; |
| 581 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 582 | for (i = 1; i < ctrl->ctrl.queue_count; i++) |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 583 | nvme_rdma_free_queue(&ctrl->queues[i]); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 584 | } |
| 585 | |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 586 | static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl) |
| 587 | { |
| 588 | int i; |
| 589 | |
| 590 | for (i = 1; i < ctrl->ctrl.queue_count; i++) |
| 591 | nvme_rdma_stop_queue(&ctrl->queues[i]); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 592 | } |
| 593 | |
Sagi Grimberg | 68e16fc | 2017-07-10 09:22:37 +0300 | [diff] [blame] | 594 | static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx) |
| 595 | { |
| 596 | int ret; |
| 597 | |
| 598 | if (idx) |
| 599 | ret = nvmf_connect_io_queue(&ctrl->ctrl, idx); |
| 600 | else |
| 601 | ret = nvmf_connect_admin_queue(&ctrl->ctrl); |
| 602 | |
| 603 | if (!ret) |
| 604 | set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[idx].flags); |
| 605 | else |
| 606 | dev_info(ctrl->ctrl.device, |
| 607 | "failed to connect queue: %d ret=%d\n", idx, ret); |
| 608 | return ret; |
| 609 | } |
| 610 | |
| 611 | static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 612 | { |
| 613 | int i, ret = 0; |
| 614 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 615 | for (i = 1; i < ctrl->ctrl.queue_count; i++) { |
Sagi Grimberg | 68e16fc | 2017-07-10 09:22:37 +0300 | [diff] [blame] | 616 | ret = nvme_rdma_start_queue(ctrl, i); |
| 617 | if (ret) |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 618 | goto out_stop_queues; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 619 | } |
| 620 | |
Steve Wise | c8dbc37 | 2016-11-08 09:16:02 -0800 | [diff] [blame] | 621 | return 0; |
| 622 | |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 623 | out_stop_queues: |
Sagi Grimberg | 68e16fc | 2017-07-10 09:22:37 +0300 | [diff] [blame] | 624 | for (i--; i >= 1; i--) |
| 625 | nvme_rdma_stop_queue(&ctrl->queues[i]); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 626 | return ret; |
| 627 | } |
| 628 | |
Sagi Grimberg | 41e8cfa | 2017-07-10 09:22:36 +0300 | [diff] [blame] | 629 | static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 630 | { |
Sagi Grimberg | c248c64 | 2017-03-09 13:26:07 +0200 | [diff] [blame] | 631 | struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; |
Sagi Grimberg | 0b36658 | 2017-07-13 11:09:44 +0300 | [diff] [blame] | 632 | struct ib_device *ibdev = ctrl->device->dev; |
Sagi Grimberg | c248c64 | 2017-03-09 13:26:07 +0200 | [diff] [blame] | 633 | unsigned int nr_io_queues; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 634 | int i, ret; |
| 635 | |
Sagi Grimberg | c248c64 | 2017-03-09 13:26:07 +0200 | [diff] [blame] | 636 | nr_io_queues = min(opts->nr_io_queues, num_online_cpus()); |
Sagi Grimberg | 0b36658 | 2017-07-13 11:09:44 +0300 | [diff] [blame] | 637 | |
| 638 | /* |
| 639 | * we map queues according to the device irq vectors for |
| 640 | * optimal locality so we don't need more queues than |
| 641 | * completion vectors. |
| 642 | */ |
| 643 | nr_io_queues = min_t(unsigned int, nr_io_queues, |
| 644 | ibdev->num_comp_vectors); |
| 645 | |
Sagi Grimberg | c248c64 | 2017-03-09 13:26:07 +0200 | [diff] [blame] | 646 | ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues); |
| 647 | if (ret) |
| 648 | return ret; |
| 649 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 650 | ctrl->ctrl.queue_count = nr_io_queues + 1; |
| 651 | if (ctrl->ctrl.queue_count < 2) |
Sagi Grimberg | c248c64 | 2017-03-09 13:26:07 +0200 | [diff] [blame] | 652 | return 0; |
| 653 | |
| 654 | dev_info(ctrl->ctrl.device, |
| 655 | "creating %d I/O queues.\n", nr_io_queues); |
| 656 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 657 | for (i = 1; i < ctrl->ctrl.queue_count; i++) { |
Sagi Grimberg | 41e8cfa | 2017-07-10 09:22:36 +0300 | [diff] [blame] | 658 | ret = nvme_rdma_alloc_queue(ctrl, i, |
| 659 | ctrl->ctrl.sqsize + 1); |
| 660 | if (ret) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 661 | goto out_free_queues; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | return 0; |
| 665 | |
| 666 | out_free_queues: |
Steve Wise | f361e5a | 2016-09-02 09:01:27 -0700 | [diff] [blame] | 667 | for (i--; i >= 1; i--) |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 668 | nvme_rdma_free_queue(&ctrl->queues[i]); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 669 | |
| 670 | return ret; |
| 671 | } |
| 672 | |
Sagi Grimberg | 60070c7 | 2017-10-11 15:29:06 +0300 | [diff] [blame] | 673 | static void nvme_rdma_free_tagset(struct nvme_ctrl *nctrl, |
| 674 | struct blk_mq_tag_set *set) |
Sagi Grimberg | b28a308 | 2017-07-10 09:22:30 +0300 | [diff] [blame] | 675 | { |
| 676 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); |
Sagi Grimberg | b28a308 | 2017-07-10 09:22:30 +0300 | [diff] [blame] | 677 | |
| 678 | blk_mq_free_tag_set(set); |
| 679 | nvme_rdma_dev_put(ctrl->device); |
| 680 | } |
| 681 | |
| 682 | static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl, |
| 683 | bool admin) |
| 684 | { |
| 685 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); |
| 686 | struct blk_mq_tag_set *set; |
| 687 | int ret; |
| 688 | |
| 689 | if (admin) { |
| 690 | set = &ctrl->admin_tag_set; |
| 691 | memset(set, 0, sizeof(*set)); |
| 692 | set->ops = &nvme_rdma_admin_mq_ops; |
| 693 | set->queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH; |
| 694 | set->reserved_tags = 2; /* connect + keep-alive */ |
| 695 | set->numa_node = NUMA_NO_NODE; |
| 696 | set->cmd_size = sizeof(struct nvme_rdma_request) + |
| 697 | SG_CHUNK_SIZE * sizeof(struct scatterlist); |
| 698 | set->driver_data = ctrl; |
| 699 | set->nr_hw_queues = 1; |
| 700 | set->timeout = ADMIN_TIMEOUT; |
Israel Rukshin | 94f29d4 | 2017-10-18 12:38:24 +0000 | [diff] [blame] | 701 | set->flags = BLK_MQ_F_NO_SCHED; |
Sagi Grimberg | b28a308 | 2017-07-10 09:22:30 +0300 | [diff] [blame] | 702 | } else { |
| 703 | set = &ctrl->tag_set; |
| 704 | memset(set, 0, sizeof(*set)); |
| 705 | set->ops = &nvme_rdma_mq_ops; |
| 706 | set->queue_depth = nctrl->opts->queue_size; |
| 707 | set->reserved_tags = 1; /* fabric connect */ |
| 708 | set->numa_node = NUMA_NO_NODE; |
| 709 | set->flags = BLK_MQ_F_SHOULD_MERGE; |
| 710 | set->cmd_size = sizeof(struct nvme_rdma_request) + |
| 711 | SG_CHUNK_SIZE * sizeof(struct scatterlist); |
| 712 | set->driver_data = ctrl; |
| 713 | set->nr_hw_queues = nctrl->queue_count - 1; |
| 714 | set->timeout = NVME_IO_TIMEOUT; |
| 715 | } |
| 716 | |
| 717 | ret = blk_mq_alloc_tag_set(set); |
| 718 | if (ret) |
| 719 | goto out; |
| 720 | |
| 721 | /* |
| 722 | * We need a reference on the device as long as the tag_set is alive, |
| 723 | * as the MRs in the request structures need a valid ib_device. |
| 724 | */ |
| 725 | ret = nvme_rdma_dev_get(ctrl->device); |
| 726 | if (!ret) { |
| 727 | ret = -EINVAL; |
| 728 | goto out_free_tagset; |
| 729 | } |
| 730 | |
| 731 | return set; |
| 732 | |
| 733 | out_free_tagset: |
| 734 | blk_mq_free_tag_set(set); |
| 735 | out: |
| 736 | return ERR_PTR(ret); |
| 737 | } |
| 738 | |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 739 | static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl, |
| 740 | bool remove) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 741 | { |
| 742 | nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe, |
| 743 | sizeof(struct nvme_command), DMA_TO_DEVICE); |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 744 | nvme_rdma_stop_queue(&ctrl->queues[0]); |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 745 | if (remove) { |
| 746 | blk_cleanup_queue(ctrl->ctrl.admin_q); |
Sagi Grimberg | 60070c7 | 2017-10-11 15:29:06 +0300 | [diff] [blame] | 747 | nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset); |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 748 | } |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 749 | nvme_rdma_free_queue(&ctrl->queues[0]); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 750 | } |
| 751 | |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 752 | static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl, |
| 753 | bool new) |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 754 | { |
| 755 | int error; |
| 756 | |
Sagi Grimberg | 41e8cfa | 2017-07-10 09:22:36 +0300 | [diff] [blame] | 757 | error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH); |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 758 | if (error) |
| 759 | return error; |
| 760 | |
| 761 | ctrl->device = ctrl->queues[0].device; |
| 762 | |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 763 | ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS, |
| 764 | ctrl->device->dev->attrs.max_fast_reg_page_list_len); |
| 765 | |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 766 | if (new) { |
| 767 | ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true); |
| 768 | if (IS_ERR(ctrl->ctrl.admin_tagset)) |
| 769 | goto out_free_queue; |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 770 | |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 771 | ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set); |
| 772 | if (IS_ERR(ctrl->ctrl.admin_q)) { |
| 773 | error = PTR_ERR(ctrl->ctrl.admin_q); |
| 774 | goto out_free_tagset; |
| 775 | } |
| 776 | } else { |
Sagi Grimberg | 31b8446 | 2017-10-11 12:53:07 +0300 | [diff] [blame] | 777 | error = nvme_reinit_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset); |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 778 | if (error) |
| 779 | goto out_free_queue; |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 780 | } |
| 781 | |
Sagi Grimberg | 68e16fc | 2017-07-10 09:22:37 +0300 | [diff] [blame] | 782 | error = nvme_rdma_start_queue(ctrl, 0); |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 783 | if (error) |
| 784 | goto out_cleanup_queue; |
| 785 | |
Sagi Grimberg | 09fdc23 | 2017-07-10 09:22:39 +0300 | [diff] [blame] | 786 | error = ctrl->ctrl.ops->reg_read64(&ctrl->ctrl, NVME_REG_CAP, |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 787 | &ctrl->ctrl.cap); |
| 788 | if (error) { |
| 789 | dev_err(ctrl->ctrl.device, |
| 790 | "prop_get NVME_REG_CAP failed\n"); |
| 791 | goto out_cleanup_queue; |
| 792 | } |
| 793 | |
| 794 | ctrl->ctrl.sqsize = |
| 795 | min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize); |
| 796 | |
| 797 | error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap); |
| 798 | if (error) |
| 799 | goto out_cleanup_queue; |
| 800 | |
| 801 | ctrl->ctrl.max_hw_sectors = |
Linus Torvalds | 126e76f | 2017-09-09 12:49:01 -0700 | [diff] [blame] | 802 | (ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9); |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 803 | |
| 804 | error = nvme_init_identify(&ctrl->ctrl); |
| 805 | if (error) |
| 806 | goto out_cleanup_queue; |
| 807 | |
| 808 | error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev, |
| 809 | &ctrl->async_event_sqe, sizeof(struct nvme_command), |
| 810 | DMA_TO_DEVICE); |
| 811 | if (error) |
| 812 | goto out_cleanup_queue; |
| 813 | |
| 814 | return 0; |
| 815 | |
| 816 | out_cleanup_queue: |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 817 | if (new) |
| 818 | blk_cleanup_queue(ctrl->ctrl.admin_q); |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 819 | out_free_tagset: |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 820 | if (new) |
Sagi Grimberg | 60070c7 | 2017-10-11 15:29:06 +0300 | [diff] [blame] | 821 | nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset); |
Sagi Grimberg | 90af351 | 2017-07-10 09:22:28 +0300 | [diff] [blame] | 822 | out_free_queue: |
| 823 | nvme_rdma_free_queue(&ctrl->queues[0]); |
| 824 | return error; |
| 825 | } |
| 826 | |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 827 | static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl, |
| 828 | bool remove) |
| 829 | { |
| 830 | nvme_rdma_stop_io_queues(ctrl); |
| 831 | if (remove) { |
| 832 | blk_cleanup_queue(ctrl->ctrl.connect_q); |
Sagi Grimberg | 60070c7 | 2017-10-11 15:29:06 +0300 | [diff] [blame] | 833 | nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset); |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 834 | } |
| 835 | nvme_rdma_free_io_queues(ctrl); |
| 836 | } |
| 837 | |
| 838 | static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new) |
| 839 | { |
| 840 | int ret; |
| 841 | |
Sagi Grimberg | 41e8cfa | 2017-07-10 09:22:36 +0300 | [diff] [blame] | 842 | ret = nvme_rdma_alloc_io_queues(ctrl); |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 843 | if (ret) |
| 844 | return ret; |
| 845 | |
| 846 | if (new) { |
| 847 | ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false); |
| 848 | if (IS_ERR(ctrl->ctrl.tagset)) |
| 849 | goto out_free_io_queues; |
| 850 | |
| 851 | ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set); |
| 852 | if (IS_ERR(ctrl->ctrl.connect_q)) { |
| 853 | ret = PTR_ERR(ctrl->ctrl.connect_q); |
| 854 | goto out_free_tag_set; |
| 855 | } |
| 856 | } else { |
Sagi Grimberg | 31b8446 | 2017-10-11 12:53:07 +0300 | [diff] [blame] | 857 | ret = nvme_reinit_tagset(&ctrl->ctrl, ctrl->ctrl.tagset); |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 858 | if (ret) |
| 859 | goto out_free_io_queues; |
| 860 | |
| 861 | blk_mq_update_nr_hw_queues(&ctrl->tag_set, |
| 862 | ctrl->ctrl.queue_count - 1); |
| 863 | } |
| 864 | |
Sagi Grimberg | 68e16fc | 2017-07-10 09:22:37 +0300 | [diff] [blame] | 865 | ret = nvme_rdma_start_io_queues(ctrl); |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 866 | if (ret) |
| 867 | goto out_cleanup_connect_q; |
| 868 | |
| 869 | return 0; |
| 870 | |
| 871 | out_cleanup_connect_q: |
| 872 | if (new) |
| 873 | blk_cleanup_queue(ctrl->ctrl.connect_q); |
| 874 | out_free_tag_set: |
| 875 | if (new) |
Sagi Grimberg | 60070c7 | 2017-10-11 15:29:06 +0300 | [diff] [blame] | 876 | nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset); |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 877 | out_free_io_queues: |
| 878 | nvme_rdma_free_io_queues(ctrl); |
| 879 | return ret; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl) |
| 883 | { |
| 884 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); |
| 885 | |
| 886 | if (list_empty(&ctrl->list)) |
| 887 | goto free_ctrl; |
| 888 | |
| 889 | mutex_lock(&nvme_rdma_ctrl_mutex); |
| 890 | list_del(&ctrl->list); |
| 891 | mutex_unlock(&nvme_rdma_ctrl_mutex); |
| 892 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 893 | kfree(ctrl->queues); |
| 894 | nvmf_free_options(nctrl->opts); |
| 895 | free_ctrl: |
| 896 | kfree(ctrl); |
| 897 | } |
| 898 | |
Sagi Grimberg | fd8563c | 2017-03-18 20:58:29 +0200 | [diff] [blame] | 899 | static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl) |
| 900 | { |
| 901 | /* If we are resetting/deleting then do nothing */ |
| 902 | if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) { |
| 903 | WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW || |
| 904 | ctrl->ctrl.state == NVME_CTRL_LIVE); |
| 905 | return; |
| 906 | } |
| 907 | |
| 908 | if (nvmf_should_reconnect(&ctrl->ctrl)) { |
| 909 | dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n", |
| 910 | ctrl->ctrl.opts->reconnect_delay); |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 911 | queue_delayed_work(nvme_wq, &ctrl->reconnect_work, |
Sagi Grimberg | fd8563c | 2017-03-18 20:58:29 +0200 | [diff] [blame] | 912 | ctrl->ctrl.opts->reconnect_delay * HZ); |
| 913 | } else { |
| 914 | dev_info(ctrl->ctrl.device, "Removing controller...\n"); |
Sagi Grimberg | 12fa130 | 2017-10-29 14:21:01 +0200 | [diff] [blame] | 915 | nvme_delete_ctrl(&ctrl->ctrl); |
Sagi Grimberg | fd8563c | 2017-03-18 20:58:29 +0200 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 919 | static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work) |
| 920 | { |
| 921 | struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work), |
| 922 | struct nvme_rdma_ctrl, reconnect_work); |
| 923 | bool changed; |
| 924 | int ret; |
| 925 | |
Sagi Grimberg | fdf9dfa | 2017-05-04 13:33:15 +0300 | [diff] [blame] | 926 | ++ctrl->ctrl.nr_reconnects; |
Sagi Grimberg | fd8563c | 2017-03-18 20:58:29 +0200 | [diff] [blame] | 927 | |
Sagi Grimberg | 31fdf18 | 2017-08-28 21:40:06 +0200 | [diff] [blame] | 928 | ret = nvme_rdma_configure_admin_queue(ctrl, false); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 929 | if (ret) |
Sagi Grimberg | e818a5b | 2017-06-05 20:35:56 +0300 | [diff] [blame] | 930 | goto requeue; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 931 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 932 | if (ctrl->ctrl.queue_count > 1) { |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 933 | ret = nvme_rdma_configure_io_queues(ctrl, false); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 934 | if (ret) |
Sagi Grimberg | 5e1fe61 | 2017-10-11 15:29:11 +0300 | [diff] [blame] | 935 | goto destroy_admin; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); |
Sagi Grimberg | 0a960afd | 2017-09-21 17:01:37 +0300 | [diff] [blame] | 939 | if (!changed) { |
| 940 | /* state change failure is ok if we're in DELETING state */ |
| 941 | WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING); |
| 942 | return; |
| 943 | } |
| 944 | |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 945 | nvme_start_ctrl(&ctrl->ctrl); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 946 | |
Sagi Grimberg | 5e1fe61 | 2017-10-11 15:29:11 +0300 | [diff] [blame] | 947 | dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n", |
| 948 | ctrl->ctrl.nr_reconnects); |
| 949 | |
| 950 | ctrl->ctrl.nr_reconnects = 0; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 951 | |
| 952 | return; |
| 953 | |
Sagi Grimberg | 5e1fe61 | 2017-10-11 15:29:11 +0300 | [diff] [blame] | 954 | destroy_admin: |
| 955 | nvme_rdma_destroy_admin_queue(ctrl, false); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 956 | requeue: |
Sagi Grimberg | fd8563c | 2017-03-18 20:58:29 +0200 | [diff] [blame] | 957 | dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n", |
Sagi Grimberg | fdf9dfa | 2017-05-04 13:33:15 +0300 | [diff] [blame] | 958 | ctrl->ctrl.nr_reconnects); |
Sagi Grimberg | fd8563c | 2017-03-18 20:58:29 +0200 | [diff] [blame] | 959 | nvme_rdma_reconnect_or_remove(ctrl); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | static void nvme_rdma_error_recovery_work(struct work_struct *work) |
| 963 | { |
| 964 | struct nvme_rdma_ctrl *ctrl = container_of(work, |
| 965 | struct nvme_rdma_ctrl, err_work); |
| 966 | |
Sagi Grimberg | e4d753d | 2017-09-21 17:01:38 +0300 | [diff] [blame] | 967 | nvme_stop_keep_alive(&ctrl->ctrl); |
Sagi Grimberg | e89ca58 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 968 | |
Sagi Grimberg | 148b4e7 | 2017-07-10 09:22:35 +0300 | [diff] [blame] | 969 | if (ctrl->ctrl.queue_count > 1) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 970 | nvme_stop_queues(&ctrl->ctrl); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 971 | blk_mq_tagset_busy_iter(&ctrl->tag_set, |
| 972 | nvme_cancel_request, &ctrl->ctrl); |
Sagi Grimberg | 5e1fe61 | 2017-10-11 15:29:11 +0300 | [diff] [blame] | 973 | nvme_rdma_destroy_io_queues(ctrl, false); |
| 974 | } |
| 975 | |
| 976 | blk_mq_quiesce_queue(ctrl->ctrl.admin_q); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 977 | blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, |
| 978 | nvme_cancel_request, &ctrl->ctrl); |
Sagi Grimberg | 5e1fe61 | 2017-10-11 15:29:11 +0300 | [diff] [blame] | 979 | nvme_rdma_destroy_admin_queue(ctrl, false); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 980 | |
Sagi Grimberg | e818a5b | 2017-06-05 20:35:56 +0300 | [diff] [blame] | 981 | /* |
| 982 | * queues are not a live anymore, so restart the queues to fail fast |
| 983 | * new IO |
| 984 | */ |
Sagi Grimberg | fb051339 | 2017-07-02 15:33:32 +0300 | [diff] [blame] | 985 | blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); |
Sagi Grimberg | e818a5b | 2017-06-05 20:35:56 +0300 | [diff] [blame] | 986 | nvme_start_queues(&ctrl->ctrl); |
| 987 | |
Sagi Grimberg | fd8563c | 2017-03-18 20:58:29 +0200 | [diff] [blame] | 988 | nvme_rdma_reconnect_or_remove(ctrl); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl) |
| 992 | { |
| 993 | if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING)) |
| 994 | return; |
| 995 | |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 996 | queue_work(nvme_wq, &ctrl->err_work); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc, |
| 1000 | const char *op) |
| 1001 | { |
| 1002 | struct nvme_rdma_queue *queue = cq->cq_context; |
| 1003 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; |
| 1004 | |
| 1005 | if (ctrl->ctrl.state == NVME_CTRL_LIVE) |
| 1006 | dev_info(ctrl->ctrl.device, |
| 1007 | "%s for CQE 0x%p failed with status %s (%d)\n", |
| 1008 | op, wc->wr_cqe, |
| 1009 | ib_wc_status_msg(wc->status), wc->status); |
| 1010 | nvme_rdma_error_recovery(ctrl); |
| 1011 | } |
| 1012 | |
| 1013 | static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc) |
| 1014 | { |
| 1015 | if (unlikely(wc->status != IB_WC_SUCCESS)) |
| 1016 | nvme_rdma_wr_error(cq, wc, "MEMREG"); |
| 1017 | } |
| 1018 | |
| 1019 | static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc) |
| 1020 | { |
| 1021 | if (unlikely(wc->status != IB_WC_SUCCESS)) |
| 1022 | nvme_rdma_wr_error(cq, wc, "LOCAL_INV"); |
| 1023 | } |
| 1024 | |
| 1025 | static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue, |
| 1026 | struct nvme_rdma_request *req) |
| 1027 | { |
| 1028 | struct ib_send_wr *bad_wr; |
| 1029 | struct ib_send_wr wr = { |
| 1030 | .opcode = IB_WR_LOCAL_INV, |
| 1031 | .next = NULL, |
| 1032 | .num_sge = 0, |
| 1033 | .send_flags = 0, |
| 1034 | .ex.invalidate_rkey = req->mr->rkey, |
| 1035 | }; |
| 1036 | |
| 1037 | req->reg_cqe.done = nvme_rdma_inv_rkey_done; |
| 1038 | wr.wr_cqe = &req->reg_cqe; |
| 1039 | |
| 1040 | return ib_post_send(queue->qp, &wr, &bad_wr); |
| 1041 | } |
| 1042 | |
| 1043 | static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue, |
| 1044 | struct request *rq) |
| 1045 | { |
| 1046 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 1047 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; |
| 1048 | struct nvme_rdma_device *dev = queue->device; |
| 1049 | struct ib_device *ibdev = dev->dev; |
| 1050 | int res; |
| 1051 | |
| 1052 | if (!blk_rq_bytes(rq)) |
| 1053 | return; |
| 1054 | |
Sagi Grimberg | 60a5188 | 2017-10-11 15:29:10 +0300 | [diff] [blame] | 1055 | if (req->mr->need_inval && test_bit(NVME_RDMA_Q_LIVE, &req->queue->flags)) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1056 | res = nvme_rdma_inv_rkey(queue, req); |
Max Gurtovoy | a7b7c7a | 2017-08-14 15:29:26 +0300 | [diff] [blame] | 1057 | if (unlikely(res < 0)) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1058 | dev_err(ctrl->ctrl.device, |
| 1059 | "Queueing INV WR for rkey %#x failed (%d)\n", |
| 1060 | req->mr->rkey, res); |
| 1061 | nvme_rdma_error_recovery(queue->ctrl); |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | ib_dma_unmap_sg(ibdev, req->sg_table.sgl, |
| 1066 | req->nents, rq_data_dir(rq) == |
| 1067 | WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 1068 | |
| 1069 | nvme_cleanup_cmd(rq); |
| 1070 | sg_free_table_chained(&req->sg_table, true); |
| 1071 | } |
| 1072 | |
| 1073 | static int nvme_rdma_set_sg_null(struct nvme_command *c) |
| 1074 | { |
| 1075 | struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; |
| 1076 | |
| 1077 | sg->addr = 0; |
| 1078 | put_unaligned_le24(0, sg->length); |
| 1079 | put_unaligned_le32(0, sg->key); |
| 1080 | sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue, |
| 1085 | struct nvme_rdma_request *req, struct nvme_command *c) |
| 1086 | { |
| 1087 | struct nvme_sgl_desc *sg = &c->common.dptr.sgl; |
| 1088 | |
| 1089 | req->sge[1].addr = sg_dma_address(req->sg_table.sgl); |
| 1090 | req->sge[1].length = sg_dma_len(req->sg_table.sgl); |
| 1091 | req->sge[1].lkey = queue->device->pd->local_dma_lkey; |
| 1092 | |
| 1093 | sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff); |
| 1094 | sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl)); |
| 1095 | sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET; |
| 1096 | |
| 1097 | req->inline_data = true; |
| 1098 | req->num_sge++; |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue, |
| 1103 | struct nvme_rdma_request *req, struct nvme_command *c) |
| 1104 | { |
| 1105 | struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; |
| 1106 | |
| 1107 | sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl)); |
| 1108 | put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length); |
Christoph Hellwig | 11975e0 | 2016-09-05 12:56:20 +0200 | [diff] [blame] | 1109 | put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1110 | sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue, |
| 1115 | struct nvme_rdma_request *req, struct nvme_command *c, |
| 1116 | int count) |
| 1117 | { |
| 1118 | struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; |
| 1119 | int nr; |
| 1120 | |
Max Gurtovoy | b925a2d | 2017-08-28 12:52:27 +0300 | [diff] [blame] | 1121 | /* |
| 1122 | * Align the MR to a 4K page size to match the ctrl page size and |
| 1123 | * the block virtual boundary. |
| 1124 | */ |
| 1125 | nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K); |
Max Gurtovoy | a7b7c7a | 2017-08-14 15:29:26 +0300 | [diff] [blame] | 1126 | if (unlikely(nr < count)) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1127 | if (nr < 0) |
| 1128 | return nr; |
| 1129 | return -EINVAL; |
| 1130 | } |
| 1131 | |
| 1132 | ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey)); |
| 1133 | |
| 1134 | req->reg_cqe.done = nvme_rdma_memreg_done; |
| 1135 | memset(&req->reg_wr, 0, sizeof(req->reg_wr)); |
| 1136 | req->reg_wr.wr.opcode = IB_WR_REG_MR; |
| 1137 | req->reg_wr.wr.wr_cqe = &req->reg_cqe; |
| 1138 | req->reg_wr.wr.num_sge = 0; |
| 1139 | req->reg_wr.mr = req->mr; |
| 1140 | req->reg_wr.key = req->mr->rkey; |
| 1141 | req->reg_wr.access = IB_ACCESS_LOCAL_WRITE | |
| 1142 | IB_ACCESS_REMOTE_READ | |
| 1143 | IB_ACCESS_REMOTE_WRITE; |
| 1144 | |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 1145 | req->mr->need_inval = true; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1146 | |
| 1147 | sg->addr = cpu_to_le64(req->mr->iova); |
| 1148 | put_unaligned_le24(req->mr->length, sg->length); |
| 1149 | put_unaligned_le32(req->mr->rkey, sg->key); |
| 1150 | sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) | |
| 1151 | NVME_SGL_FMT_INVALIDATE; |
| 1152 | |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
| 1156 | static int nvme_rdma_map_data(struct nvme_rdma_queue *queue, |
Christoph Hellwig | b131c61 | 2017-01-13 12:29:12 +0100 | [diff] [blame] | 1157 | struct request *rq, struct nvme_command *c) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1158 | { |
| 1159 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 1160 | struct nvme_rdma_device *dev = queue->device; |
| 1161 | struct ib_device *ibdev = dev->dev; |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 1162 | int count, ret; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1163 | |
| 1164 | req->num_sge = 1; |
| 1165 | req->inline_data = false; |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 1166 | req->mr->need_inval = false; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1167 | |
| 1168 | c->common.flags |= NVME_CMD_SGL_METABUF; |
| 1169 | |
| 1170 | if (!blk_rq_bytes(rq)) |
| 1171 | return nvme_rdma_set_sg_null(c); |
| 1172 | |
| 1173 | req->sg_table.sgl = req->first_sgl; |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 1174 | ret = sg_alloc_table_chained(&req->sg_table, |
| 1175 | blk_rq_nr_phys_segments(rq), req->sg_table.sgl); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1176 | if (ret) |
| 1177 | return -ENOMEM; |
| 1178 | |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 1179 | req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1180 | |
Christoph Hellwig | f9d03f9 | 2016-12-08 15:20:32 -0700 | [diff] [blame] | 1181 | count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1182 | rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 1183 | if (unlikely(count <= 0)) { |
| 1184 | sg_free_table_chained(&req->sg_table, true); |
| 1185 | return -EIO; |
| 1186 | } |
| 1187 | |
| 1188 | if (count == 1) { |
Christoph Hellwig | b131c61 | 2017-01-13 12:29:12 +0100 | [diff] [blame] | 1189 | if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) && |
| 1190 | blk_rq_payload_bytes(rq) <= |
| 1191 | nvme_rdma_inline_data_size(queue)) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1192 | return nvme_rdma_map_sg_inline(queue, req, c); |
| 1193 | |
Christoph Hellwig | 11975e0 | 2016-09-05 12:56:20 +0200 | [diff] [blame] | 1194 | if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1195 | return nvme_rdma_map_sg_single(queue, req, c); |
| 1196 | } |
| 1197 | |
| 1198 | return nvme_rdma_map_sg_fr(queue, req, c, count); |
| 1199 | } |
| 1200 | |
| 1201 | static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc) |
| 1202 | { |
| 1203 | if (unlikely(wc->status != IB_WC_SUCCESS)) |
| 1204 | nvme_rdma_wr_error(cq, wc, "SEND"); |
| 1205 | } |
| 1206 | |
Marta Rybczynska | 5e599d7 | 2017-06-06 13:27:21 +0200 | [diff] [blame] | 1207 | /* |
| 1208 | * We want to signal completion at least every queue depth/2. This returns the |
| 1209 | * largest power of two that is not above half of (queue size + 1) to optimize |
| 1210 | * (avoid divisions). |
| 1211 | */ |
| 1212 | static inline bool nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue) |
Marta Rybczynska | 0544f54 | 2017-04-10 17:12:34 +0200 | [diff] [blame] | 1213 | { |
Marta Rybczynska | 5e599d7 | 2017-06-06 13:27:21 +0200 | [diff] [blame] | 1214 | int limit = 1 << ilog2((queue->queue_size + 1) / 2); |
Marta Rybczynska | 0544f54 | 2017-04-10 17:12:34 +0200 | [diff] [blame] | 1215 | |
Marta Rybczynska | 5e599d7 | 2017-06-06 13:27:21 +0200 | [diff] [blame] | 1216 | return (atomic_inc_return(&queue->sig_count) & (limit - 1)) == 0; |
Marta Rybczynska | 0544f54 | 2017-04-10 17:12:34 +0200 | [diff] [blame] | 1217 | } |
| 1218 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1219 | static int nvme_rdma_post_send(struct nvme_rdma_queue *queue, |
| 1220 | struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge, |
| 1221 | struct ib_send_wr *first, bool flush) |
| 1222 | { |
| 1223 | struct ib_send_wr wr, *bad_wr; |
| 1224 | int ret; |
| 1225 | |
| 1226 | sge->addr = qe->dma; |
| 1227 | sge->length = sizeof(struct nvme_command), |
| 1228 | sge->lkey = queue->device->pd->local_dma_lkey; |
| 1229 | |
| 1230 | qe->cqe.done = nvme_rdma_send_done; |
| 1231 | |
| 1232 | wr.next = NULL; |
| 1233 | wr.wr_cqe = &qe->cqe; |
| 1234 | wr.sg_list = sge; |
| 1235 | wr.num_sge = num_sge; |
| 1236 | wr.opcode = IB_WR_SEND; |
| 1237 | wr.send_flags = 0; |
| 1238 | |
| 1239 | /* |
| 1240 | * Unsignalled send completions are another giant desaster in the |
| 1241 | * IB Verbs spec: If we don't regularly post signalled sends |
| 1242 | * the send queue will fill up and only a QP reset will rescue us. |
| 1243 | * Would have been way to obvious to handle this in hardware or |
| 1244 | * at least the RDMA stack.. |
| 1245 | * |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1246 | * Always signal the flushes. The magic request used for the flush |
| 1247 | * sequencer is not allocated in our driver's tagset and it's |
| 1248 | * triggered to be freed by blk_cleanup_queue(). So we need to |
| 1249 | * always mark it as signaled to ensure that the "wr_cqe", which is |
Masahiro Yamada | b43daed | 2017-02-27 14:29:09 -0800 | [diff] [blame] | 1250 | * embedded in request's payload, is not freed when __ib_process_cq() |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1251 | * calls wr_cqe->done(). |
| 1252 | */ |
Marta Rybczynska | 0544f54 | 2017-04-10 17:12:34 +0200 | [diff] [blame] | 1253 | if (nvme_rdma_queue_sig_limit(queue) || flush) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1254 | wr.send_flags |= IB_SEND_SIGNALED; |
| 1255 | |
| 1256 | if (first) |
| 1257 | first->next = ≀ |
| 1258 | else |
| 1259 | first = ≀ |
| 1260 | |
| 1261 | ret = ib_post_send(queue->qp, first, &bad_wr); |
Max Gurtovoy | a7b7c7a | 2017-08-14 15:29:26 +0300 | [diff] [blame] | 1262 | if (unlikely(ret)) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1263 | dev_err(queue->ctrl->ctrl.device, |
| 1264 | "%s failed with error code %d\n", __func__, ret); |
| 1265 | } |
| 1266 | return ret; |
| 1267 | } |
| 1268 | |
| 1269 | static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue, |
| 1270 | struct nvme_rdma_qe *qe) |
| 1271 | { |
| 1272 | struct ib_recv_wr wr, *bad_wr; |
| 1273 | struct ib_sge list; |
| 1274 | int ret; |
| 1275 | |
| 1276 | list.addr = qe->dma; |
| 1277 | list.length = sizeof(struct nvme_completion); |
| 1278 | list.lkey = queue->device->pd->local_dma_lkey; |
| 1279 | |
| 1280 | qe->cqe.done = nvme_rdma_recv_done; |
| 1281 | |
| 1282 | wr.next = NULL; |
| 1283 | wr.wr_cqe = &qe->cqe; |
| 1284 | wr.sg_list = &list; |
| 1285 | wr.num_sge = 1; |
| 1286 | |
| 1287 | ret = ib_post_recv(queue->qp, &wr, &bad_wr); |
Max Gurtovoy | a7b7c7a | 2017-08-14 15:29:26 +0300 | [diff] [blame] | 1288 | if (unlikely(ret)) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1289 | dev_err(queue->ctrl->ctrl.device, |
| 1290 | "%s failed with error code %d\n", __func__, ret); |
| 1291 | } |
| 1292 | return ret; |
| 1293 | } |
| 1294 | |
| 1295 | static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue) |
| 1296 | { |
| 1297 | u32 queue_idx = nvme_rdma_queue_idx(queue); |
| 1298 | |
| 1299 | if (queue_idx == 0) |
| 1300 | return queue->ctrl->admin_tag_set.tags[queue_idx]; |
| 1301 | return queue->ctrl->tag_set.tags[queue_idx - 1]; |
| 1302 | } |
| 1303 | |
| 1304 | static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx) |
| 1305 | { |
| 1306 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg); |
| 1307 | struct nvme_rdma_queue *queue = &ctrl->queues[0]; |
| 1308 | struct ib_device *dev = queue->device->dev; |
| 1309 | struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe; |
| 1310 | struct nvme_command *cmd = sqe->data; |
| 1311 | struct ib_sge sge; |
| 1312 | int ret; |
| 1313 | |
| 1314 | if (WARN_ON_ONCE(aer_idx != 0)) |
| 1315 | return; |
| 1316 | |
| 1317 | ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE); |
| 1318 | |
| 1319 | memset(cmd, 0, sizeof(*cmd)); |
| 1320 | cmd->common.opcode = nvme_admin_async_event; |
| 1321 | cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH; |
| 1322 | cmd->common.flags |= NVME_CMD_SGL_METABUF; |
| 1323 | nvme_rdma_set_sg_null(cmd); |
| 1324 | |
| 1325 | ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd), |
| 1326 | DMA_TO_DEVICE); |
| 1327 | |
| 1328 | ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false); |
| 1329 | WARN_ON_ONCE(ret); |
| 1330 | } |
| 1331 | |
| 1332 | static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue, |
| 1333 | struct nvme_completion *cqe, struct ib_wc *wc, int tag) |
| 1334 | { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1335 | struct request *rq; |
| 1336 | struct nvme_rdma_request *req; |
| 1337 | int ret = 0; |
| 1338 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1339 | rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id); |
| 1340 | if (!rq) { |
| 1341 | dev_err(queue->ctrl->ctrl.device, |
| 1342 | "tag 0x%x on QP %#x not found\n", |
| 1343 | cqe->command_id, queue->qp->qp_num); |
| 1344 | nvme_rdma_error_recovery(queue->ctrl); |
| 1345 | return ret; |
| 1346 | } |
| 1347 | req = blk_mq_rq_to_pdu(rq); |
| 1348 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1349 | if (rq->tag == tag) |
| 1350 | ret = 1; |
| 1351 | |
| 1352 | if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) && |
| 1353 | wc->ex.invalidate_rkey == req->mr->rkey) |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 1354 | req->mr->need_inval = false; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1355 | |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 1356 | nvme_end_request(rq, cqe->status, cqe->result); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1357 | return ret; |
| 1358 | } |
| 1359 | |
| 1360 | static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag) |
| 1361 | { |
| 1362 | struct nvme_rdma_qe *qe = |
| 1363 | container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe); |
| 1364 | struct nvme_rdma_queue *queue = cq->cq_context; |
| 1365 | struct ib_device *ibdev = queue->device->dev; |
| 1366 | struct nvme_completion *cqe = qe->data; |
| 1367 | const size_t len = sizeof(struct nvme_completion); |
| 1368 | int ret = 0; |
| 1369 | |
| 1370 | if (unlikely(wc->status != IB_WC_SUCCESS)) { |
| 1371 | nvme_rdma_wr_error(cq, wc, "RECV"); |
| 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE); |
| 1376 | /* |
| 1377 | * AEN requests are special as they don't time out and can |
| 1378 | * survive any kind of queue freeze and often don't respond to |
| 1379 | * aborts. We don't even bother to allocate a struct request |
| 1380 | * for them but rather special case them here. |
| 1381 | */ |
| 1382 | if (unlikely(nvme_rdma_queue_idx(queue) == 0 && |
| 1383 | cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH)) |
Christoph Hellwig | 7bf5853 | 2016-11-10 07:32:34 -0800 | [diff] [blame] | 1384 | nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status, |
| 1385 | &cqe->result); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1386 | else |
| 1387 | ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag); |
| 1388 | ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE); |
| 1389 | |
| 1390 | nvme_rdma_post_recv(queue, qe); |
| 1391 | return ret; |
| 1392 | } |
| 1393 | |
| 1394 | static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc) |
| 1395 | { |
| 1396 | __nvme_rdma_recv_done(cq, wc, -1); |
| 1397 | } |
| 1398 | |
| 1399 | static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue) |
| 1400 | { |
| 1401 | int ret, i; |
| 1402 | |
| 1403 | for (i = 0; i < queue->queue_size; i++) { |
| 1404 | ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]); |
| 1405 | if (ret) |
| 1406 | goto out_destroy_queue_ib; |
| 1407 | } |
| 1408 | |
| 1409 | return 0; |
| 1410 | |
| 1411 | out_destroy_queue_ib: |
| 1412 | nvme_rdma_destroy_queue_ib(queue); |
| 1413 | return ret; |
| 1414 | } |
| 1415 | |
| 1416 | static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue, |
| 1417 | struct rdma_cm_event *ev) |
| 1418 | { |
Steve Wise | 7f03953 | 2016-10-26 12:36:47 -0700 | [diff] [blame] | 1419 | struct rdma_cm_id *cm_id = queue->cm_id; |
| 1420 | int status = ev->status; |
| 1421 | const char *rej_msg; |
| 1422 | const struct nvme_rdma_cm_rej *rej_data; |
| 1423 | u8 rej_data_len; |
| 1424 | |
| 1425 | rej_msg = rdma_reject_msg(cm_id, status); |
| 1426 | rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len); |
| 1427 | |
| 1428 | if (rej_data && rej_data_len >= sizeof(u16)) { |
| 1429 | u16 sts = le16_to_cpu(rej_data->sts); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1430 | |
| 1431 | dev_err(queue->ctrl->ctrl.device, |
Steve Wise | 7f03953 | 2016-10-26 12:36:47 -0700 | [diff] [blame] | 1432 | "Connect rejected: status %d (%s) nvme status %d (%s).\n", |
| 1433 | status, rej_msg, sts, nvme_rdma_cm_msg(sts)); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1434 | } else { |
| 1435 | dev_err(queue->ctrl->ctrl.device, |
Steve Wise | 7f03953 | 2016-10-26 12:36:47 -0700 | [diff] [blame] | 1436 | "Connect rejected: status %d (%s).\n", status, rej_msg); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | return -ECONNRESET; |
| 1440 | } |
| 1441 | |
| 1442 | static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue) |
| 1443 | { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1444 | int ret; |
| 1445 | |
Sagi Grimberg | ca6e95b | 2017-05-04 13:33:09 +0300 | [diff] [blame] | 1446 | ret = nvme_rdma_create_queue_ib(queue); |
| 1447 | if (ret) |
| 1448 | return ret; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1449 | |
| 1450 | ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS); |
| 1451 | if (ret) { |
| 1452 | dev_err(queue->ctrl->ctrl.device, |
| 1453 | "rdma_resolve_route failed (%d).\n", |
| 1454 | queue->cm_error); |
| 1455 | goto out_destroy_queue; |
| 1456 | } |
| 1457 | |
| 1458 | return 0; |
| 1459 | |
| 1460 | out_destroy_queue: |
| 1461 | nvme_rdma_destroy_queue_ib(queue); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1462 | return ret; |
| 1463 | } |
| 1464 | |
| 1465 | static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue) |
| 1466 | { |
| 1467 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; |
| 1468 | struct rdma_conn_param param = { }; |
Roland Dreier | 0b857b4 | 2016-07-31 00:27:39 -0700 | [diff] [blame] | 1469 | struct nvme_rdma_cm_req priv = { }; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1470 | int ret; |
| 1471 | |
| 1472 | param.qp_num = queue->qp->qp_num; |
| 1473 | param.flow_control = 1; |
| 1474 | |
| 1475 | param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom; |
Sagi Grimberg | 2ac17c2 | 2016-06-22 15:06:00 +0300 | [diff] [blame] | 1476 | /* maximum retry count */ |
| 1477 | param.retry_count = 7; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1478 | param.rnr_retry_count = 7; |
| 1479 | param.private_data = &priv; |
| 1480 | param.private_data_len = sizeof(priv); |
| 1481 | |
| 1482 | priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); |
| 1483 | priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue)); |
Jay Freyensee | f994d9d | 2016-08-17 15:00:26 -0700 | [diff] [blame] | 1484 | /* |
| 1485 | * set the admin queue depth to the minimum size |
| 1486 | * specified by the Fabrics standard. |
| 1487 | */ |
| 1488 | if (priv.qid == 0) { |
Sagi Grimberg | 7aa1f42 | 2017-06-18 16:15:59 +0300 | [diff] [blame] | 1489 | priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH); |
| 1490 | priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1); |
Jay Freyensee | f994d9d | 2016-08-17 15:00:26 -0700 | [diff] [blame] | 1491 | } else { |
Jay Freyensee | c5af865 | 2016-08-17 15:00:27 -0700 | [diff] [blame] | 1492 | /* |
| 1493 | * current interpretation of the fabrics spec |
| 1494 | * is at minimum you make hrqsize sqsize+1, or a |
| 1495 | * 1's based representation of sqsize. |
| 1496 | */ |
Jay Freyensee | f994d9d | 2016-08-17 15:00:26 -0700 | [diff] [blame] | 1497 | priv.hrqsize = cpu_to_le16(queue->queue_size); |
Jay Freyensee | c5af865 | 2016-08-17 15:00:27 -0700 | [diff] [blame] | 1498 | priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize); |
Jay Freyensee | f994d9d | 2016-08-17 15:00:26 -0700 | [diff] [blame] | 1499 | } |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1500 | |
| 1501 | ret = rdma_connect(queue->cm_id, ¶m); |
| 1502 | if (ret) { |
| 1503 | dev_err(ctrl->ctrl.device, |
| 1504 | "rdma_connect failed (%d).\n", ret); |
| 1505 | goto out_destroy_queue_ib; |
| 1506 | } |
| 1507 | |
| 1508 | return 0; |
| 1509 | |
| 1510 | out_destroy_queue_ib: |
| 1511 | nvme_rdma_destroy_queue_ib(queue); |
| 1512 | return ret; |
| 1513 | } |
| 1514 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1515 | static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, |
| 1516 | struct rdma_cm_event *ev) |
| 1517 | { |
| 1518 | struct nvme_rdma_queue *queue = cm_id->context; |
| 1519 | int cm_error = 0; |
| 1520 | |
| 1521 | dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n", |
| 1522 | rdma_event_msg(ev->event), ev->event, |
| 1523 | ev->status, cm_id); |
| 1524 | |
| 1525 | switch (ev->event) { |
| 1526 | case RDMA_CM_EVENT_ADDR_RESOLVED: |
| 1527 | cm_error = nvme_rdma_addr_resolved(queue); |
| 1528 | break; |
| 1529 | case RDMA_CM_EVENT_ROUTE_RESOLVED: |
| 1530 | cm_error = nvme_rdma_route_resolved(queue); |
| 1531 | break; |
| 1532 | case RDMA_CM_EVENT_ESTABLISHED: |
| 1533 | queue->cm_error = nvme_rdma_conn_established(queue); |
| 1534 | /* complete cm_done regardless of success/failure */ |
| 1535 | complete(&queue->cm_done); |
| 1536 | return 0; |
| 1537 | case RDMA_CM_EVENT_REJECTED: |
Sagi Grimberg | abf87d5 | 2017-05-04 13:33:10 +0300 | [diff] [blame] | 1538 | nvme_rdma_destroy_queue_ib(queue); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1539 | cm_error = nvme_rdma_conn_rejected(queue, ev); |
| 1540 | break; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1541 | case RDMA_CM_EVENT_ROUTE_ERROR: |
| 1542 | case RDMA_CM_EVENT_CONNECT_ERROR: |
| 1543 | case RDMA_CM_EVENT_UNREACHABLE: |
Sagi Grimberg | abf87d5 | 2017-05-04 13:33:10 +0300 | [diff] [blame] | 1544 | nvme_rdma_destroy_queue_ib(queue); |
| 1545 | case RDMA_CM_EVENT_ADDR_ERROR: |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1546 | dev_dbg(queue->ctrl->ctrl.device, |
| 1547 | "CM error event %d\n", ev->event); |
| 1548 | cm_error = -ECONNRESET; |
| 1549 | break; |
| 1550 | case RDMA_CM_EVENT_DISCONNECTED: |
| 1551 | case RDMA_CM_EVENT_ADDR_CHANGE: |
| 1552 | case RDMA_CM_EVENT_TIMEWAIT_EXIT: |
| 1553 | dev_dbg(queue->ctrl->ctrl.device, |
| 1554 | "disconnect received - connection closed\n"); |
| 1555 | nvme_rdma_error_recovery(queue->ctrl); |
| 1556 | break; |
| 1557 | case RDMA_CM_EVENT_DEVICE_REMOVAL: |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 1558 | /* device removal is handled via the ib_client API */ |
| 1559 | break; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1560 | default: |
| 1561 | dev_err(queue->ctrl->ctrl.device, |
| 1562 | "Unexpected RDMA CM event (%d)\n", ev->event); |
| 1563 | nvme_rdma_error_recovery(queue->ctrl); |
| 1564 | break; |
| 1565 | } |
| 1566 | |
| 1567 | if (cm_error) { |
| 1568 | queue->cm_error = cm_error; |
| 1569 | complete(&queue->cm_done); |
| 1570 | } |
| 1571 | |
| 1572 | return 0; |
| 1573 | } |
| 1574 | |
| 1575 | static enum blk_eh_timer_return |
| 1576 | nvme_rdma_timeout(struct request *rq, bool reserved) |
| 1577 | { |
| 1578 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 1579 | |
Nitzan Carmi | e62a538 | 2017-10-22 09:37:04 +0000 | [diff] [blame] | 1580 | dev_warn(req->queue->ctrl->ctrl.device, |
| 1581 | "I/O %d QID %d timeout, reset controller\n", |
| 1582 | rq->tag, nvme_rdma_queue_idx(req->queue)); |
| 1583 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1584 | /* queue error recovery */ |
| 1585 | nvme_rdma_error_recovery(req->queue->ctrl); |
| 1586 | |
| 1587 | /* fail with DNR on cmd timeout */ |
Christoph Hellwig | 27fa9bc | 2017-04-20 16:02:57 +0200 | [diff] [blame] | 1588 | nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1589 | |
| 1590 | return BLK_EH_HANDLED; |
| 1591 | } |
| 1592 | |
Christoph Hellwig | 553cd9e | 2016-11-02 08:49:18 -0600 | [diff] [blame] | 1593 | /* |
| 1594 | * We cannot accept any other command until the Connect command has completed. |
| 1595 | */ |
Christoph Hellwig | a104c9f | 2017-06-12 18:26:06 +0200 | [diff] [blame] | 1596 | static inline blk_status_t |
| 1597 | nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue, struct request *rq) |
Christoph Hellwig | 553cd9e | 2016-11-02 08:49:18 -0600 | [diff] [blame] | 1598 | { |
| 1599 | if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) { |
Christoph Hellwig | 1392370e | 2017-01-03 14:29:02 +0300 | [diff] [blame] | 1600 | struct nvme_command *cmd = nvme_req(rq)->cmd; |
Christoph Hellwig | 553cd9e | 2016-11-02 08:49:18 -0600 | [diff] [blame] | 1601 | |
Christoph Hellwig | 57292b5 | 2017-01-31 16:57:29 +0100 | [diff] [blame] | 1602 | if (!blk_rq_is_passthrough(rq) || |
Christoph Hellwig | 553cd9e | 2016-11-02 08:49:18 -0600 | [diff] [blame] | 1603 | cmd->common.opcode != nvme_fabrics_command || |
Sagi Grimberg | e818a5b | 2017-06-05 20:35:56 +0300 | [diff] [blame] | 1604 | cmd->fabrics.fctype != nvme_fabrics_type_connect) { |
| 1605 | /* |
| 1606 | * reconnecting state means transport disruption, which |
| 1607 | * can take a long time and even might fail permanently, |
| 1608 | * so we can't let incoming I/O be requeued forever. |
| 1609 | * fail it fast to allow upper layers a chance to |
| 1610 | * failover. |
| 1611 | */ |
| 1612 | if (queue->ctrl->ctrl.state == NVME_CTRL_RECONNECTING) |
Christoph Hellwig | a104c9f | 2017-06-12 18:26:06 +0200 | [diff] [blame] | 1613 | return BLK_STS_IOERR; |
| 1614 | return BLK_STS_RESOURCE; /* try again later */ |
Sagi Grimberg | e818a5b | 2017-06-05 20:35:56 +0300 | [diff] [blame] | 1615 | } |
Christoph Hellwig | 553cd9e | 2016-11-02 08:49:18 -0600 | [diff] [blame] | 1616 | } |
| 1617 | |
Sagi Grimberg | e818a5b | 2017-06-05 20:35:56 +0300 | [diff] [blame] | 1618 | return 0; |
Christoph Hellwig | 553cd9e | 2016-11-02 08:49:18 -0600 | [diff] [blame] | 1619 | } |
| 1620 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 1621 | static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1622 | const struct blk_mq_queue_data *bd) |
| 1623 | { |
| 1624 | struct nvme_ns *ns = hctx->queue->queuedata; |
| 1625 | struct nvme_rdma_queue *queue = hctx->driver_data; |
| 1626 | struct request *rq = bd->rq; |
| 1627 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 1628 | struct nvme_rdma_qe *sqe = &req->sqe; |
| 1629 | struct nvme_command *c = sqe->data; |
| 1630 | bool flush = false; |
| 1631 | struct ib_device *dev; |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 1632 | blk_status_t ret; |
| 1633 | int err; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1634 | |
| 1635 | WARN_ON_ONCE(rq->tag < 0); |
| 1636 | |
Sagi Grimberg | e818a5b | 2017-06-05 20:35:56 +0300 | [diff] [blame] | 1637 | ret = nvme_rdma_queue_is_ready(queue, rq); |
| 1638 | if (unlikely(ret)) |
Christoph Hellwig | a104c9f | 2017-06-12 18:26:06 +0200 | [diff] [blame] | 1639 | return ret; |
Christoph Hellwig | 553cd9e | 2016-11-02 08:49:18 -0600 | [diff] [blame] | 1640 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1641 | dev = queue->device->dev; |
| 1642 | ib_dma_sync_single_for_cpu(dev, sqe->dma, |
| 1643 | sizeof(struct nvme_command), DMA_TO_DEVICE); |
| 1644 | |
| 1645 | ret = nvme_setup_cmd(ns, rq, c); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 1646 | if (ret) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1647 | return ret; |
| 1648 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1649 | blk_mq_start_request(rq); |
| 1650 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 1651 | err = nvme_rdma_map_data(queue, rq, c); |
Max Gurtovoy | a7b7c7a | 2017-08-14 15:29:26 +0300 | [diff] [blame] | 1652 | if (unlikely(err < 0)) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1653 | dev_err(queue->ctrl->ctrl.device, |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 1654 | "Failed to map data (%d)\n", err); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1655 | nvme_cleanup_cmd(rq); |
| 1656 | goto err; |
| 1657 | } |
| 1658 | |
| 1659 | ib_dma_sync_single_for_device(dev, sqe->dma, |
| 1660 | sizeof(struct nvme_command), DMA_TO_DEVICE); |
| 1661 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 1662 | if (req_op(rq) == REQ_OP_FLUSH) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1663 | flush = true; |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 1664 | err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge, |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 1665 | req->mr->need_inval ? &req->reg_wr.wr : NULL, flush); |
Max Gurtovoy | a7b7c7a | 2017-08-14 15:29:26 +0300 | [diff] [blame] | 1666 | if (unlikely(err)) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1667 | nvme_rdma_unmap_data(queue, rq); |
| 1668 | goto err; |
| 1669 | } |
| 1670 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 1671 | return BLK_STS_OK; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1672 | err: |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 1673 | if (err == -ENOMEM || err == -EAGAIN) |
| 1674 | return BLK_STS_RESOURCE; |
| 1675 | return BLK_STS_IOERR; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag) |
| 1679 | { |
| 1680 | struct nvme_rdma_queue *queue = hctx->driver_data; |
| 1681 | struct ib_cq *cq = queue->ib_cq; |
| 1682 | struct ib_wc wc; |
| 1683 | int found = 0; |
| 1684 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1685 | while (ib_poll_cq(cq, 1, &wc) > 0) { |
| 1686 | struct ib_cqe *cqe = wc.wr_cqe; |
| 1687 | |
| 1688 | if (cqe) { |
| 1689 | if (cqe->done == nvme_rdma_recv_done) |
| 1690 | found |= __nvme_rdma_recv_done(cq, &wc, tag); |
| 1691 | else |
| 1692 | cqe->done(cq, &wc); |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | return found; |
| 1697 | } |
| 1698 | |
| 1699 | static void nvme_rdma_complete_rq(struct request *rq) |
| 1700 | { |
| 1701 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1702 | |
Christoph Hellwig | 77f02a7 | 2017-03-30 13:41:32 +0200 | [diff] [blame] | 1703 | nvme_rdma_unmap_data(req->queue, rq); |
| 1704 | nvme_complete_rq(rq); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1705 | } |
| 1706 | |
Sagi Grimberg | 0b36658 | 2017-07-13 11:09:44 +0300 | [diff] [blame] | 1707 | static int nvme_rdma_map_queues(struct blk_mq_tag_set *set) |
| 1708 | { |
| 1709 | struct nvme_rdma_ctrl *ctrl = set->driver_data; |
| 1710 | |
| 1711 | return blk_mq_rdma_map_queues(set, ctrl->device->dev, 0); |
| 1712 | } |
| 1713 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 1714 | static const struct blk_mq_ops nvme_rdma_mq_ops = { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1715 | .queue_rq = nvme_rdma_queue_rq, |
| 1716 | .complete = nvme_rdma_complete_rq, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1717 | .init_request = nvme_rdma_init_request, |
| 1718 | .exit_request = nvme_rdma_exit_request, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1719 | .init_hctx = nvme_rdma_init_hctx, |
| 1720 | .poll = nvme_rdma_poll, |
| 1721 | .timeout = nvme_rdma_timeout, |
Sagi Grimberg | 0b36658 | 2017-07-13 11:09:44 +0300 | [diff] [blame] | 1722 | .map_queues = nvme_rdma_map_queues, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1723 | }; |
| 1724 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 1725 | static const struct blk_mq_ops nvme_rdma_admin_mq_ops = { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1726 | .queue_rq = nvme_rdma_queue_rq, |
| 1727 | .complete = nvme_rdma_complete_rq, |
Christoph Hellwig | 385475e | 2017-06-13 09:15:19 +0200 | [diff] [blame] | 1728 | .init_request = nvme_rdma_init_request, |
| 1729 | .exit_request = nvme_rdma_exit_request, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1730 | .init_hctx = nvme_rdma_init_admin_hctx, |
| 1731 | .timeout = nvme_rdma_timeout, |
| 1732 | }; |
| 1733 | |
Sagi Grimberg | 18398af | 2017-07-10 09:22:31 +0300 | [diff] [blame] | 1734 | static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1735 | { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1736 | cancel_work_sync(&ctrl->err_work); |
| 1737 | cancel_delayed_work_sync(&ctrl->reconnect_work); |
| 1738 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 1739 | if (ctrl->ctrl.queue_count > 1) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1740 | nvme_stop_queues(&ctrl->ctrl); |
| 1741 | blk_mq_tagset_busy_iter(&ctrl->tag_set, |
| 1742 | nvme_cancel_request, &ctrl->ctrl); |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 1743 | nvme_rdma_destroy_io_queues(ctrl, shutdown); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1744 | } |
| 1745 | |
Sagi Grimberg | 18398af | 2017-07-10 09:22:31 +0300 | [diff] [blame] | 1746 | if (shutdown) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1747 | nvme_shutdown_ctrl(&ctrl->ctrl); |
Sagi Grimberg | 18398af | 2017-07-10 09:22:31 +0300 | [diff] [blame] | 1748 | else |
| 1749 | nvme_disable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1750 | |
Sagi Grimberg | fb051339 | 2017-07-02 15:33:32 +0300 | [diff] [blame] | 1751 | blk_mq_quiesce_queue(ctrl->ctrl.admin_q); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1752 | blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, |
| 1753 | nvme_cancel_request, &ctrl->ctrl); |
Sagi Grimberg | fb051339 | 2017-07-02 15:33:32 +0300 | [diff] [blame] | 1754 | blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 1755 | nvme_rdma_destroy_admin_queue(ctrl, shutdown); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1756 | } |
| 1757 | |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 1758 | static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1759 | { |
Christoph Hellwig | e9bc258 | 2017-10-29 10:44:30 +0200 | [diff] [blame] | 1760 | nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1761 | } |
| 1762 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1763 | static void nvme_rdma_reset_ctrl_work(struct work_struct *work) |
| 1764 | { |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 1765 | struct nvme_rdma_ctrl *ctrl = |
| 1766 | container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1767 | int ret; |
| 1768 | bool changed; |
| 1769 | |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 1770 | nvme_stop_ctrl(&ctrl->ctrl); |
Sagi Grimberg | 18398af | 2017-07-10 09:22:31 +0300 | [diff] [blame] | 1771 | nvme_rdma_shutdown_ctrl(ctrl, false); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1772 | |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 1773 | ret = nvme_rdma_configure_admin_queue(ctrl, false); |
Sagi Grimberg | 370ae6e | 2017-07-10 09:22:38 +0300 | [diff] [blame] | 1774 | if (ret) |
| 1775 | goto out_fail; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1776 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 1777 | if (ctrl->ctrl.queue_count > 1) { |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 1778 | ret = nvme_rdma_configure_io_queues(ctrl, false); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1779 | if (ret) |
Sagi Grimberg | 370ae6e | 2017-07-10 09:22:38 +0300 | [diff] [blame] | 1780 | goto out_fail; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); |
Sagi Grimberg | 0ad0bfa | 2017-10-11 12:49:51 +0300 | [diff] [blame] | 1784 | if (!changed) { |
| 1785 | /* state change failure is ok if we're in DELETING state */ |
| 1786 | WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING); |
| 1787 | return; |
| 1788 | } |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1789 | |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 1790 | nvme_start_ctrl(&ctrl->ctrl); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1791 | |
| 1792 | return; |
| 1793 | |
Sagi Grimberg | 370ae6e | 2017-07-10 09:22:38 +0300 | [diff] [blame] | 1794 | out_fail: |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1795 | dev_warn(ctrl->ctrl.device, "Removing after reset failure\n"); |
Christoph Hellwig | e9bc258 | 2017-10-29 10:44:30 +0200 | [diff] [blame] | 1796 | nvme_remove_namespaces(&ctrl->ctrl); |
| 1797 | nvme_rdma_shutdown_ctrl(ctrl, true); |
| 1798 | nvme_uninit_ctrl(&ctrl->ctrl); |
| 1799 | nvme_put_ctrl(&ctrl->ctrl); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1800 | } |
| 1801 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1802 | static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = { |
| 1803 | .name = "rdma", |
| 1804 | .module = THIS_MODULE, |
Christoph Hellwig | d3d5b87 | 2017-05-20 15:14:44 +0200 | [diff] [blame] | 1805 | .flags = NVME_F_FABRICS, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1806 | .reg_read32 = nvmf_reg_read32, |
| 1807 | .reg_read64 = nvmf_reg_read64, |
| 1808 | .reg_write32 = nvmf_reg_write32, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1809 | .free_ctrl = nvme_rdma_free_ctrl, |
| 1810 | .submit_async_event = nvme_rdma_submit_async_event, |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 1811 | .delete_ctrl = nvme_rdma_delete_ctrl, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1812 | .get_address = nvmf_get_address, |
Sagi Grimberg | 31b8446 | 2017-10-11 12:53:07 +0300 | [diff] [blame] | 1813 | .reinit_request = nvme_rdma_reinit_request, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1814 | }; |
| 1815 | |
James Smart | 36e835f | 2017-10-20 16:17:09 -0700 | [diff] [blame] | 1816 | static inline bool |
| 1817 | __nvme_rdma_options_match(struct nvme_rdma_ctrl *ctrl, |
| 1818 | struct nvmf_ctrl_options *opts) |
| 1819 | { |
| 1820 | char *stdport = __stringify(NVME_RDMA_IP_PORT); |
| 1821 | |
| 1822 | |
| 1823 | if (!nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts) || |
| 1824 | strcmp(opts->traddr, ctrl->ctrl.opts->traddr)) |
| 1825 | return false; |
| 1826 | |
| 1827 | if (opts->mask & NVMF_OPT_TRSVCID && |
| 1828 | ctrl->ctrl.opts->mask & NVMF_OPT_TRSVCID) { |
| 1829 | if (strcmp(opts->trsvcid, ctrl->ctrl.opts->trsvcid)) |
| 1830 | return false; |
| 1831 | } else if (opts->mask & NVMF_OPT_TRSVCID) { |
| 1832 | if (strcmp(opts->trsvcid, stdport)) |
| 1833 | return false; |
| 1834 | } else if (ctrl->ctrl.opts->mask & NVMF_OPT_TRSVCID) { |
| 1835 | if (strcmp(stdport, ctrl->ctrl.opts->trsvcid)) |
| 1836 | return false; |
| 1837 | } |
| 1838 | /* else, it's a match as both have stdport. Fall to next checks */ |
| 1839 | |
| 1840 | /* |
| 1841 | * checking the local address is rough. In most cases, one |
| 1842 | * is not specified and the host port is selected by the stack. |
| 1843 | * |
| 1844 | * Assume no match if: |
| 1845 | * local address is specified and address is not the same |
| 1846 | * local address is not specified but remote is, or vice versa |
| 1847 | * (admin using specific host_traddr when it matters). |
| 1848 | */ |
| 1849 | if (opts->mask & NVMF_OPT_HOST_TRADDR && |
| 1850 | ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR) { |
| 1851 | if (strcmp(opts->host_traddr, ctrl->ctrl.opts->host_traddr)) |
| 1852 | return false; |
| 1853 | } else if (opts->mask & NVMF_OPT_HOST_TRADDR || |
| 1854 | ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR) |
| 1855 | return false; |
| 1856 | /* |
| 1857 | * if neither controller had an host port specified, assume it's |
| 1858 | * a match as everything else matched. |
| 1859 | */ |
| 1860 | |
| 1861 | return true; |
| 1862 | } |
| 1863 | |
| 1864 | /* |
| 1865 | * Fails a connection request if it matches an existing controller |
| 1866 | * (association) with the same tuple: |
| 1867 | * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN> |
| 1868 | * |
| 1869 | * if local address is not specified in the request, it will match an |
| 1870 | * existing controller with all the other parameters the same and no |
| 1871 | * local port address specified as well. |
| 1872 | * |
| 1873 | * The ports don't need to be compared as they are intrinsically |
| 1874 | * already matched by the port pointers supplied. |
| 1875 | */ |
| 1876 | static bool |
| 1877 | nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts) |
| 1878 | { |
| 1879 | struct nvme_rdma_ctrl *ctrl; |
| 1880 | bool found = false; |
| 1881 | |
| 1882 | mutex_lock(&nvme_rdma_ctrl_mutex); |
| 1883 | list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) { |
| 1884 | found = __nvme_rdma_options_match(ctrl, opts); |
| 1885 | if (found) |
| 1886 | break; |
| 1887 | } |
| 1888 | mutex_unlock(&nvme_rdma_ctrl_mutex); |
| 1889 | |
| 1890 | return found; |
| 1891 | } |
| 1892 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1893 | static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev, |
| 1894 | struct nvmf_ctrl_options *opts) |
| 1895 | { |
| 1896 | struct nvme_rdma_ctrl *ctrl; |
| 1897 | int ret; |
| 1898 | bool changed; |
Sagi Grimberg | 0928f9b | 2017-02-05 21:49:32 +0200 | [diff] [blame] | 1899 | char *port; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1900 | |
| 1901 | ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); |
| 1902 | if (!ctrl) |
| 1903 | return ERR_PTR(-ENOMEM); |
| 1904 | ctrl->ctrl.opts = opts; |
| 1905 | INIT_LIST_HEAD(&ctrl->list); |
| 1906 | |
Sagi Grimberg | 0928f9b | 2017-02-05 21:49:32 +0200 | [diff] [blame] | 1907 | if (opts->mask & NVMF_OPT_TRSVCID) |
| 1908 | port = opts->trsvcid; |
| 1909 | else |
| 1910 | port = __stringify(NVME_RDMA_IP_PORT); |
| 1911 | |
| 1912 | ret = inet_pton_with_scope(&init_net, AF_UNSPEC, |
| 1913 | opts->traddr, port, &ctrl->addr); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1914 | if (ret) { |
Sagi Grimberg | 0928f9b | 2017-02-05 21:49:32 +0200 | [diff] [blame] | 1915 | pr_err("malformed address passed: %s:%s\n", opts->traddr, port); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1916 | goto out_free_ctrl; |
| 1917 | } |
| 1918 | |
Max Gurtovoy | 8f4e8da | 2017-02-19 20:08:03 +0200 | [diff] [blame] | 1919 | if (opts->mask & NVMF_OPT_HOST_TRADDR) { |
Sagi Grimberg | 0928f9b | 2017-02-05 21:49:32 +0200 | [diff] [blame] | 1920 | ret = inet_pton_with_scope(&init_net, AF_UNSPEC, |
| 1921 | opts->host_traddr, NULL, &ctrl->src_addr); |
Max Gurtovoy | 8f4e8da | 2017-02-19 20:08:03 +0200 | [diff] [blame] | 1922 | if (ret) { |
Sagi Grimberg | 0928f9b | 2017-02-05 21:49:32 +0200 | [diff] [blame] | 1923 | pr_err("malformed src address passed: %s\n", |
Max Gurtovoy | 8f4e8da | 2017-02-19 20:08:03 +0200 | [diff] [blame] | 1924 | opts->host_traddr); |
| 1925 | goto out_free_ctrl; |
| 1926 | } |
| 1927 | } |
| 1928 | |
James Smart | 36e835f | 2017-10-20 16:17:09 -0700 | [diff] [blame] | 1929 | if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) { |
| 1930 | ret = -EALREADY; |
| 1931 | goto out_free_ctrl; |
| 1932 | } |
| 1933 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1934 | ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops, |
| 1935 | 0 /* no quirks, we're perfect! */); |
| 1936 | if (ret) |
| 1937 | goto out_free_ctrl; |
| 1938 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1939 | INIT_DELAYED_WORK(&ctrl->reconnect_work, |
| 1940 | nvme_rdma_reconnect_ctrl_work); |
| 1941 | INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work); |
Christoph Hellwig | d86c4d8 | 2017-06-15 15:41:08 +0200 | [diff] [blame] | 1942 | INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1943 | |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 1944 | ctrl->ctrl.queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */ |
Jay Freyensee | c5af865 | 2016-08-17 15:00:27 -0700 | [diff] [blame] | 1945 | ctrl->ctrl.sqsize = opts->queue_size - 1; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1946 | ctrl->ctrl.kato = opts->kato; |
| 1947 | |
| 1948 | ret = -ENOMEM; |
Sagi Grimberg | d858e5f | 2017-04-24 10:58:29 +0300 | [diff] [blame] | 1949 | ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues), |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1950 | GFP_KERNEL); |
| 1951 | if (!ctrl->queues) |
| 1952 | goto out_uninit_ctrl; |
| 1953 | |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 1954 | ret = nvme_rdma_configure_admin_queue(ctrl, true); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1955 | if (ret) |
| 1956 | goto out_kfree_queues; |
| 1957 | |
| 1958 | /* sanity check icdoff */ |
| 1959 | if (ctrl->ctrl.icdoff) { |
| 1960 | dev_err(ctrl->ctrl.device, "icdoff is not supported!\n"); |
Dan Carpenter | bb472ba | 2017-06-14 13:46:45 +0300 | [diff] [blame] | 1961 | ret = -EINVAL; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1962 | goto out_remove_admin_queue; |
| 1963 | } |
| 1964 | |
| 1965 | /* sanity check keyed sgls */ |
| 1966 | if (!(ctrl->ctrl.sgls & (1 << 20))) { |
| 1967 | dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n"); |
Dan Carpenter | bb472ba | 2017-06-14 13:46:45 +0300 | [diff] [blame] | 1968 | ret = -EINVAL; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1969 | goto out_remove_admin_queue; |
| 1970 | } |
| 1971 | |
| 1972 | if (opts->queue_size > ctrl->ctrl.maxcmd) { |
| 1973 | /* warn if maxcmd is lower than queue_size */ |
| 1974 | dev_warn(ctrl->ctrl.device, |
| 1975 | "queue_size %zu > ctrl maxcmd %u, clamping down\n", |
| 1976 | opts->queue_size, ctrl->ctrl.maxcmd); |
| 1977 | opts->queue_size = ctrl->ctrl.maxcmd; |
| 1978 | } |
| 1979 | |
Samuel Jones | 76c08bf | 2016-10-25 09:22:34 +0200 | [diff] [blame] | 1980 | if (opts->queue_size > ctrl->ctrl.sqsize + 1) { |
| 1981 | /* warn if sqsize is lower than queue_size */ |
| 1982 | dev_warn(ctrl->ctrl.device, |
| 1983 | "queue_size %zu > ctrl sqsize %u, clamping down\n", |
| 1984 | opts->queue_size, ctrl->ctrl.sqsize + 1); |
| 1985 | opts->queue_size = ctrl->ctrl.sqsize + 1; |
| 1986 | } |
| 1987 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1988 | if (opts->nr_io_queues) { |
Sagi Grimberg | a57bd54 | 2017-08-28 21:41:10 +0200 | [diff] [blame] | 1989 | ret = nvme_rdma_configure_io_queues(ctrl, true); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1990 | if (ret) |
| 1991 | goto out_remove_admin_queue; |
| 1992 | } |
| 1993 | |
| 1994 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); |
| 1995 | WARN_ON_ONCE(!changed); |
| 1996 | |
Sagi Grimberg | 0928f9b | 2017-02-05 21:49:32 +0200 | [diff] [blame] | 1997 | dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n", |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1998 | ctrl->ctrl.opts->subsysnqn, &ctrl->addr); |
| 1999 | |
Christoph Hellwig | d22524a | 2017-10-18 13:25:42 +0200 | [diff] [blame] | 2000 | nvme_get_ctrl(&ctrl->ctrl); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2001 | |
| 2002 | mutex_lock(&nvme_rdma_ctrl_mutex); |
| 2003 | list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list); |
| 2004 | mutex_unlock(&nvme_rdma_ctrl_mutex); |
| 2005 | |
Sagi Grimberg | d09f2b4 | 2017-07-02 10:56:43 +0300 | [diff] [blame] | 2006 | nvme_start_ctrl(&ctrl->ctrl); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2007 | |
| 2008 | return &ctrl->ctrl; |
| 2009 | |
| 2010 | out_remove_admin_queue: |
Sagi Grimberg | 3f02fff | 2017-07-10 09:22:32 +0300 | [diff] [blame] | 2011 | nvme_rdma_destroy_admin_queue(ctrl, true); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2012 | out_kfree_queues: |
| 2013 | kfree(ctrl->queues); |
| 2014 | out_uninit_ctrl: |
| 2015 | nvme_uninit_ctrl(&ctrl->ctrl); |
| 2016 | nvme_put_ctrl(&ctrl->ctrl); |
| 2017 | if (ret > 0) |
| 2018 | ret = -EIO; |
| 2019 | return ERR_PTR(ret); |
| 2020 | out_free_ctrl: |
| 2021 | kfree(ctrl); |
| 2022 | return ERR_PTR(ret); |
| 2023 | } |
| 2024 | |
| 2025 | static struct nvmf_transport_ops nvme_rdma_transport = { |
| 2026 | .name = "rdma", |
| 2027 | .required_opts = NVMF_OPT_TRADDR, |
Max Gurtovoy | 8f4e8da | 2017-02-19 20:08:03 +0200 | [diff] [blame] | 2028 | .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY | |
Sagi Grimberg | fd8563c | 2017-03-18 20:58:29 +0200 | [diff] [blame] | 2029 | NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2030 | .create_ctrl = nvme_rdma_create_ctrl, |
| 2031 | }; |
| 2032 | |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2033 | static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data) |
| 2034 | { |
| 2035 | struct nvme_rdma_ctrl *ctrl; |
| 2036 | |
| 2037 | /* Delete all controllers using this device */ |
| 2038 | mutex_lock(&nvme_rdma_ctrl_mutex); |
| 2039 | list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) { |
| 2040 | if (ctrl->device->dev != ib_device) |
| 2041 | continue; |
| 2042 | dev_info(ctrl->ctrl.device, |
| 2043 | "Removing ctrl: NQN \"%s\", addr %pISp\n", |
| 2044 | ctrl->ctrl.opts->subsysnqn, &ctrl->addr); |
Christoph Hellwig | c5017e8 | 2017-10-29 10:44:29 +0200 | [diff] [blame] | 2045 | nvme_delete_ctrl(&ctrl->ctrl); |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2046 | } |
| 2047 | mutex_unlock(&nvme_rdma_ctrl_mutex); |
| 2048 | |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 2049 | flush_workqueue(nvme_wq); |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | static struct ib_client nvme_rdma_ib_client = { |
| 2053 | .name = "nvme_rdma", |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2054 | .remove = nvme_rdma_remove_one |
| 2055 | }; |
| 2056 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2057 | static int __init nvme_rdma_init_module(void) |
| 2058 | { |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2059 | int ret; |
| 2060 | |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2061 | ret = ib_register_client(&nvme_rdma_ib_client); |
Sagi Grimberg | a56c79c | 2017-03-19 06:21:42 +0200 | [diff] [blame] | 2062 | if (ret) |
Sagi Grimberg | 9a6327d | 2017-06-07 20:31:55 +0200 | [diff] [blame] | 2063 | return ret; |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2064 | |
Sagi Grimberg | a56c79c | 2017-03-19 06:21:42 +0200 | [diff] [blame] | 2065 | ret = nvmf_register_transport(&nvme_rdma_transport); |
| 2066 | if (ret) |
| 2067 | goto err_unreg_client; |
| 2068 | |
| 2069 | return 0; |
| 2070 | |
| 2071 | err_unreg_client: |
| 2072 | ib_unregister_client(&nvme_rdma_ib_client); |
Sagi Grimberg | a56c79c | 2017-03-19 06:21:42 +0200 | [diff] [blame] | 2073 | return ret; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | static void __exit nvme_rdma_cleanup_module(void) |
| 2077 | { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2078 | nvmf_unregister_transport(&nvme_rdma_transport); |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2079 | ib_unregister_client(&nvme_rdma_ib_client); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | module_init(nvme_rdma_init_module); |
| 2083 | module_exit(nvme_rdma_cleanup_module); |
| 2084 | |
| 2085 | MODULE_LICENSE("GPL v2"); |