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