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