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