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