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> |
| 31 | #include <rdma/ib_cm.h> |
| 32 | #include <linux/nvme-rdma.h> |
| 33 | |
| 34 | #include "nvme.h" |
| 35 | #include "fabrics.h" |
| 36 | |
| 37 | |
| 38 | #define NVME_RDMA_CONNECT_TIMEOUT_MS 1000 /* 1 second */ |
| 39 | |
| 40 | #define NVME_RDMA_MAX_SEGMENT_SIZE 0xffffff /* 24-bit SGL field */ |
| 41 | |
| 42 | #define NVME_RDMA_MAX_SEGMENTS 256 |
| 43 | |
| 44 | #define NVME_RDMA_MAX_INLINE_SEGMENTS 1 |
| 45 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 46 | /* |
| 47 | * We handle AEN commands ourselves and don't even let the |
| 48 | * block layer know about them. |
| 49 | */ |
| 50 | #define NVME_RDMA_NR_AEN_COMMANDS 1 |
| 51 | #define NVME_RDMA_AQ_BLKMQ_DEPTH \ |
| 52 | (NVMF_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS) |
| 53 | |
| 54 | struct nvme_rdma_device { |
| 55 | struct ib_device *dev; |
| 56 | struct ib_pd *pd; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 57 | struct kref ref; |
| 58 | struct list_head entry; |
| 59 | }; |
| 60 | |
| 61 | struct nvme_rdma_qe { |
| 62 | struct ib_cqe cqe; |
| 63 | void *data; |
| 64 | u64 dma; |
| 65 | }; |
| 66 | |
| 67 | struct nvme_rdma_queue; |
| 68 | struct nvme_rdma_request { |
| 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 { |
| 83 | NVME_RDMA_Q_CONNECTED = (1 << 0), |
Steve Wise | f361e5a | 2016-09-02 09:01:27 -0700 | [diff] [blame] | 84 | NVME_RDMA_IB_QUEUE_ALLOCATED = (1 << 1), |
Sagi Grimberg | e89ca58 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 85 | NVME_RDMA_Q_DELETING = (1 << 2), |
Christoph Hellwig | 553cd9e | 2016-11-02 08:49:18 -0600 | [diff] [blame] | 86 | NVME_RDMA_Q_LIVE = (1 << 3), |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | struct nvme_rdma_queue { |
| 90 | struct nvme_rdma_qe *rsp_ring; |
Marta Rybczynska | d17cc7b | 2017-06-06 13:27:21 +0200 | [diff] [blame] | 91 | atomic_t sig_count; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 92 | int queue_size; |
| 93 | size_t cmnd_capsule_len; |
| 94 | struct nvme_rdma_ctrl *ctrl; |
| 95 | struct nvme_rdma_device *device; |
| 96 | struct ib_cq *ib_cq; |
| 97 | struct ib_qp *qp; |
| 98 | |
| 99 | unsigned long flags; |
| 100 | struct rdma_cm_id *cm_id; |
| 101 | int cm_error; |
| 102 | struct completion cm_done; |
| 103 | }; |
| 104 | |
| 105 | struct nvme_rdma_ctrl { |
| 106 | /* read and written in the hot path */ |
| 107 | spinlock_t lock; |
| 108 | |
| 109 | /* read only in the hot path */ |
| 110 | struct nvme_rdma_queue *queues; |
| 111 | u32 queue_count; |
| 112 | |
| 113 | /* other member variables */ |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 114 | struct blk_mq_tag_set tag_set; |
| 115 | struct work_struct delete_work; |
| 116 | struct work_struct reset_work; |
| 117 | struct work_struct err_work; |
| 118 | |
| 119 | struct nvme_rdma_qe async_event_sqe; |
| 120 | |
| 121 | int reconnect_delay; |
| 122 | struct delayed_work reconnect_work; |
| 123 | |
| 124 | struct list_head list; |
| 125 | |
| 126 | struct blk_mq_tag_set admin_tag_set; |
| 127 | struct nvme_rdma_device *device; |
| 128 | |
| 129 | u64 cap; |
| 130 | u32 max_fr_pages; |
| 131 | |
| 132 | union { |
| 133 | struct sockaddr addr; |
| 134 | struct sockaddr_in addr_in; |
| 135 | }; |
| 136 | |
| 137 | struct nvme_ctrl ctrl; |
| 138 | }; |
| 139 | |
| 140 | static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl) |
| 141 | { |
| 142 | return container_of(ctrl, struct nvme_rdma_ctrl, ctrl); |
| 143 | } |
| 144 | |
| 145 | static LIST_HEAD(device_list); |
| 146 | static DEFINE_MUTEX(device_list_mutex); |
| 147 | |
| 148 | static LIST_HEAD(nvme_rdma_ctrl_list); |
| 149 | static DEFINE_MUTEX(nvme_rdma_ctrl_mutex); |
| 150 | |
| 151 | static struct workqueue_struct *nvme_rdma_wq; |
| 152 | |
| 153 | /* |
| 154 | * Disabling this option makes small I/O goes faster, but is fundamentally |
| 155 | * unsafe. With it turned off we will have to register a global rkey that |
| 156 | * allows read and write access to all physical memory. |
| 157 | */ |
| 158 | static bool register_always = true; |
| 159 | module_param(register_always, bool, 0444); |
| 160 | MODULE_PARM_DESC(register_always, |
| 161 | "Use memory registration even for contiguous memory regions"); |
| 162 | |
| 163 | static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, |
| 164 | struct rdma_cm_event *event); |
| 165 | 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] | 166 | |
| 167 | /* XXX: really should move to a generic header sooner or later.. */ |
| 168 | static inline void put_unaligned_le24(u32 val, u8 *p) |
| 169 | { |
| 170 | *p++ = val; |
| 171 | *p++ = val >> 8; |
| 172 | *p++ = val >> 16; |
| 173 | } |
| 174 | |
| 175 | static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue) |
| 176 | { |
| 177 | return queue - queue->ctrl->queues; |
| 178 | } |
| 179 | |
| 180 | static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue) |
| 181 | { |
| 182 | return queue->cmnd_capsule_len - sizeof(struct nvme_command); |
| 183 | } |
| 184 | |
| 185 | static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe, |
| 186 | size_t capsule_size, enum dma_data_direction dir) |
| 187 | { |
| 188 | ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir); |
| 189 | kfree(qe->data); |
| 190 | } |
| 191 | |
| 192 | static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe, |
| 193 | size_t capsule_size, enum dma_data_direction dir) |
| 194 | { |
| 195 | qe->data = kzalloc(capsule_size, GFP_KERNEL); |
| 196 | if (!qe->data) |
| 197 | return -ENOMEM; |
| 198 | |
| 199 | qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir); |
| 200 | if (ib_dma_mapping_error(ibdev, qe->dma)) { |
| 201 | kfree(qe->data); |
| 202 | return -ENOMEM; |
| 203 | } |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static void nvme_rdma_free_ring(struct ib_device *ibdev, |
| 209 | struct nvme_rdma_qe *ring, size_t ib_queue_size, |
| 210 | size_t capsule_size, enum dma_data_direction dir) |
| 211 | { |
| 212 | int i; |
| 213 | |
| 214 | for (i = 0; i < ib_queue_size; i++) |
| 215 | nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir); |
| 216 | kfree(ring); |
| 217 | } |
| 218 | |
| 219 | static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev, |
| 220 | size_t ib_queue_size, size_t capsule_size, |
| 221 | enum dma_data_direction dir) |
| 222 | { |
| 223 | struct nvme_rdma_qe *ring; |
| 224 | int i; |
| 225 | |
| 226 | ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL); |
| 227 | if (!ring) |
| 228 | return NULL; |
| 229 | |
| 230 | for (i = 0; i < ib_queue_size; i++) { |
| 231 | if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir)) |
| 232 | goto out_free_ring; |
| 233 | } |
| 234 | |
| 235 | return ring; |
| 236 | |
| 237 | out_free_ring: |
| 238 | nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir); |
| 239 | return NULL; |
| 240 | } |
| 241 | |
| 242 | static void nvme_rdma_qp_event(struct ib_event *event, void *context) |
| 243 | { |
| 244 | pr_debug("QP event %d\n", event->event); |
| 245 | } |
| 246 | |
| 247 | static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue) |
| 248 | { |
| 249 | wait_for_completion_interruptible_timeout(&queue->cm_done, |
| 250 | msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1); |
| 251 | return queue->cm_error; |
| 252 | } |
| 253 | |
| 254 | static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor) |
| 255 | { |
| 256 | struct nvme_rdma_device *dev = queue->device; |
| 257 | struct ib_qp_init_attr init_attr; |
| 258 | int ret; |
| 259 | |
| 260 | memset(&init_attr, 0, sizeof(init_attr)); |
| 261 | init_attr.event_handler = nvme_rdma_qp_event; |
| 262 | /* +1 for drain */ |
| 263 | init_attr.cap.max_send_wr = factor * queue->queue_size + 1; |
| 264 | /* +1 for drain */ |
| 265 | init_attr.cap.max_recv_wr = queue->queue_size + 1; |
| 266 | init_attr.cap.max_recv_sge = 1; |
| 267 | init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS; |
| 268 | init_attr.sq_sig_type = IB_SIGNAL_REQ_WR; |
| 269 | init_attr.qp_type = IB_QPT_RC; |
| 270 | init_attr.send_cq = queue->ib_cq; |
| 271 | init_attr.recv_cq = queue->ib_cq; |
| 272 | |
| 273 | ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr); |
| 274 | |
| 275 | queue->qp = queue->cm_id->qp; |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | static int nvme_rdma_reinit_request(void *data, struct request *rq) |
| 280 | { |
| 281 | struct nvme_rdma_ctrl *ctrl = data; |
| 282 | struct nvme_rdma_device *dev = ctrl->device; |
| 283 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 284 | int ret = 0; |
| 285 | |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 286 | if (!req->mr->need_inval) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 287 | goto out; |
| 288 | |
| 289 | ib_dereg_mr(req->mr); |
| 290 | |
| 291 | req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG, |
| 292 | ctrl->max_fr_pages); |
| 293 | if (IS_ERR(req->mr)) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 294 | ret = PTR_ERR(req->mr); |
Wei Yongjun | 458a963 | 2016-07-12 11:06:17 +0000 | [diff] [blame] | 295 | req->mr = NULL; |
Colin Ian King | 1bda18d | 2016-09-05 16:24:38 +0100 | [diff] [blame] | 296 | goto out; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 297 | } |
| 298 | |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 299 | req->mr->need_inval = false; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 300 | |
| 301 | out: |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl *ctrl, |
| 306 | struct request *rq, unsigned int queue_idx) |
| 307 | { |
| 308 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 309 | struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx]; |
| 310 | struct nvme_rdma_device *dev = queue->device; |
| 311 | |
| 312 | if (req->mr) |
| 313 | ib_dereg_mr(req->mr); |
| 314 | |
| 315 | nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command), |
| 316 | DMA_TO_DEVICE); |
| 317 | } |
| 318 | |
| 319 | static void nvme_rdma_exit_request(void *data, struct request *rq, |
| 320 | unsigned int hctx_idx, unsigned int rq_idx) |
| 321 | { |
| 322 | return __nvme_rdma_exit_request(data, rq, hctx_idx + 1); |
| 323 | } |
| 324 | |
| 325 | static void nvme_rdma_exit_admin_request(void *data, struct request *rq, |
| 326 | unsigned int hctx_idx, unsigned int rq_idx) |
| 327 | { |
| 328 | return __nvme_rdma_exit_request(data, rq, 0); |
| 329 | } |
| 330 | |
| 331 | static int __nvme_rdma_init_request(struct nvme_rdma_ctrl *ctrl, |
| 332 | struct request *rq, unsigned int queue_idx) |
| 333 | { |
| 334 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 335 | struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx]; |
| 336 | struct nvme_rdma_device *dev = queue->device; |
| 337 | struct ib_device *ibdev = dev->dev; |
| 338 | int ret; |
| 339 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 340 | ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command), |
| 341 | DMA_TO_DEVICE); |
| 342 | if (ret) |
| 343 | return ret; |
| 344 | |
| 345 | req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG, |
| 346 | ctrl->max_fr_pages); |
| 347 | if (IS_ERR(req->mr)) { |
| 348 | ret = PTR_ERR(req->mr); |
| 349 | goto out_free_qe; |
| 350 | } |
| 351 | |
| 352 | req->queue = queue; |
| 353 | |
| 354 | return 0; |
| 355 | |
| 356 | out_free_qe: |
| 357 | nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command), |
| 358 | DMA_TO_DEVICE); |
| 359 | return -ENOMEM; |
| 360 | } |
| 361 | |
| 362 | static int nvme_rdma_init_request(void *data, struct request *rq, |
| 363 | unsigned int hctx_idx, unsigned int rq_idx, |
| 364 | unsigned int numa_node) |
| 365 | { |
| 366 | return __nvme_rdma_init_request(data, rq, hctx_idx + 1); |
| 367 | } |
| 368 | |
| 369 | static int nvme_rdma_init_admin_request(void *data, struct request *rq, |
| 370 | unsigned int hctx_idx, unsigned int rq_idx, |
| 371 | unsigned int numa_node) |
| 372 | { |
| 373 | return __nvme_rdma_init_request(data, rq, 0); |
| 374 | } |
| 375 | |
| 376 | static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 377 | unsigned int hctx_idx) |
| 378 | { |
| 379 | struct nvme_rdma_ctrl *ctrl = data; |
| 380 | struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1]; |
| 381 | |
| 382 | BUG_ON(hctx_idx >= ctrl->queue_count); |
| 383 | |
| 384 | hctx->driver_data = queue; |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 389 | unsigned int hctx_idx) |
| 390 | { |
| 391 | struct nvme_rdma_ctrl *ctrl = data; |
| 392 | struct nvme_rdma_queue *queue = &ctrl->queues[0]; |
| 393 | |
| 394 | BUG_ON(hctx_idx != 0); |
| 395 | |
| 396 | hctx->driver_data = queue; |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | static void nvme_rdma_free_dev(struct kref *ref) |
| 401 | { |
| 402 | struct nvme_rdma_device *ndev = |
| 403 | container_of(ref, struct nvme_rdma_device, ref); |
| 404 | |
| 405 | mutex_lock(&device_list_mutex); |
| 406 | list_del(&ndev->entry); |
| 407 | mutex_unlock(&device_list_mutex); |
| 408 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 409 | ib_dealloc_pd(ndev->pd); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 410 | kfree(ndev); |
| 411 | } |
| 412 | |
| 413 | static void nvme_rdma_dev_put(struct nvme_rdma_device *dev) |
| 414 | { |
| 415 | kref_put(&dev->ref, nvme_rdma_free_dev); |
| 416 | } |
| 417 | |
| 418 | static int nvme_rdma_dev_get(struct nvme_rdma_device *dev) |
| 419 | { |
| 420 | return kref_get_unless_zero(&dev->ref); |
| 421 | } |
| 422 | |
| 423 | static struct nvme_rdma_device * |
| 424 | nvme_rdma_find_get_device(struct rdma_cm_id *cm_id) |
| 425 | { |
| 426 | struct nvme_rdma_device *ndev; |
| 427 | |
| 428 | mutex_lock(&device_list_mutex); |
| 429 | list_for_each_entry(ndev, &device_list, entry) { |
| 430 | if (ndev->dev->node_guid == cm_id->device->node_guid && |
| 431 | nvme_rdma_dev_get(ndev)) |
| 432 | goto out_unlock; |
| 433 | } |
| 434 | |
| 435 | ndev = kzalloc(sizeof(*ndev), GFP_KERNEL); |
| 436 | if (!ndev) |
| 437 | goto out_err; |
| 438 | |
| 439 | ndev->dev = cm_id->device; |
| 440 | kref_init(&ndev->ref); |
| 441 | |
Christoph Hellwig | 11975e0 | 2016-09-05 12:56:20 +0200 | [diff] [blame] | 442 | ndev->pd = ib_alloc_pd(ndev->dev, |
| 443 | register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 444 | if (IS_ERR(ndev->pd)) |
| 445 | goto out_free_dev; |
| 446 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 447 | if (!(ndev->dev->attrs.device_cap_flags & |
| 448 | IB_DEVICE_MEM_MGT_EXTENSIONS)) { |
| 449 | dev_err(&ndev->dev->dev, |
| 450 | "Memory registrations not supported.\n"); |
Christoph Hellwig | 11975e0 | 2016-09-05 12:56:20 +0200 | [diff] [blame] | 451 | goto out_free_pd; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | list_add(&ndev->entry, &device_list); |
| 455 | out_unlock: |
| 456 | mutex_unlock(&device_list_mutex); |
| 457 | return ndev; |
| 458 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 459 | out_free_pd: |
| 460 | ib_dealloc_pd(ndev->pd); |
| 461 | out_free_dev: |
| 462 | kfree(ndev); |
| 463 | out_err: |
| 464 | mutex_unlock(&device_list_mutex); |
| 465 | return NULL; |
| 466 | } |
| 467 | |
| 468 | static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue) |
| 469 | { |
Steve Wise | f361e5a | 2016-09-02 09:01:27 -0700 | [diff] [blame] | 470 | struct nvme_rdma_device *dev; |
| 471 | struct ib_device *ibdev; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 472 | |
Steve Wise | f361e5a | 2016-09-02 09:01:27 -0700 | [diff] [blame] | 473 | if (!test_and_clear_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags)) |
| 474 | return; |
| 475 | |
| 476 | dev = queue->device; |
| 477 | ibdev = dev->dev; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 478 | rdma_destroy_qp(queue->cm_id); |
| 479 | ib_free_cq(queue->ib_cq); |
| 480 | |
| 481 | nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size, |
| 482 | sizeof(struct nvme_completion), DMA_FROM_DEVICE); |
| 483 | |
| 484 | nvme_rdma_dev_put(dev); |
| 485 | } |
| 486 | |
| 487 | static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue, |
| 488 | struct nvme_rdma_device *dev) |
| 489 | { |
| 490 | struct ib_device *ibdev = dev->dev; |
| 491 | const int send_wr_factor = 3; /* MR, SEND, INV */ |
| 492 | const int cq_factor = send_wr_factor + 1; /* + RECV */ |
| 493 | int comp_vector, idx = nvme_rdma_queue_idx(queue); |
| 494 | |
| 495 | int ret; |
| 496 | |
| 497 | queue->device = dev; |
| 498 | |
| 499 | /* |
| 500 | * The admin queue is barely used once the controller is live, so don't |
| 501 | * bother to spread it out. |
| 502 | */ |
| 503 | if (idx == 0) |
| 504 | comp_vector = 0; |
| 505 | else |
| 506 | comp_vector = idx % ibdev->num_comp_vectors; |
| 507 | |
| 508 | |
| 509 | /* +1 for ib_stop_cq */ |
| 510 | queue->ib_cq = ib_alloc_cq(dev->dev, queue, |
| 511 | cq_factor * queue->queue_size + 1, comp_vector, |
| 512 | IB_POLL_SOFTIRQ); |
| 513 | if (IS_ERR(queue->ib_cq)) { |
| 514 | ret = PTR_ERR(queue->ib_cq); |
| 515 | goto out; |
| 516 | } |
| 517 | |
| 518 | ret = nvme_rdma_create_qp(queue, send_wr_factor); |
| 519 | if (ret) |
| 520 | goto out_destroy_ib_cq; |
| 521 | |
| 522 | queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size, |
| 523 | sizeof(struct nvme_completion), DMA_FROM_DEVICE); |
| 524 | if (!queue->rsp_ring) { |
| 525 | ret = -ENOMEM; |
| 526 | goto out_destroy_qp; |
| 527 | } |
Steve Wise | f361e5a | 2016-09-02 09:01:27 -0700 | [diff] [blame] | 528 | set_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 529 | |
| 530 | return 0; |
| 531 | |
| 532 | out_destroy_qp: |
| 533 | ib_destroy_qp(queue->qp); |
| 534 | out_destroy_ib_cq: |
| 535 | ib_free_cq(queue->ib_cq); |
| 536 | out: |
| 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl, |
| 541 | int idx, size_t queue_size) |
| 542 | { |
| 543 | struct nvme_rdma_queue *queue; |
| 544 | int ret; |
| 545 | |
| 546 | queue = &ctrl->queues[idx]; |
| 547 | queue->ctrl = ctrl; |
| 548 | init_completion(&queue->cm_done); |
| 549 | |
| 550 | if (idx > 0) |
| 551 | queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16; |
| 552 | else |
| 553 | queue->cmnd_capsule_len = sizeof(struct nvme_command); |
| 554 | |
| 555 | queue->queue_size = queue_size; |
Marta Rybczynska | d17cc7b | 2017-06-06 13:27:21 +0200 | [diff] [blame] | 556 | atomic_set(&queue->sig_count, 0); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 557 | |
| 558 | queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue, |
| 559 | RDMA_PS_TCP, IB_QPT_RC); |
| 560 | if (IS_ERR(queue->cm_id)) { |
| 561 | dev_info(ctrl->ctrl.device, |
| 562 | "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id)); |
| 563 | return PTR_ERR(queue->cm_id); |
| 564 | } |
| 565 | |
| 566 | queue->cm_error = -ETIMEDOUT; |
| 567 | ret = rdma_resolve_addr(queue->cm_id, NULL, &ctrl->addr, |
| 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: |
Steve Wise | f361e5a | 2016-09-02 09:01:27 -0700 | [diff] [blame] | 588 | nvme_rdma_destroy_queue_ib(queue); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 589 | rdma_destroy_id(queue->cm_id); |
| 590 | return ret; |
| 591 | } |
| 592 | |
| 593 | static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue) |
| 594 | { |
| 595 | rdma_disconnect(queue->cm_id); |
| 596 | ib_drain_qp(queue->qp); |
| 597 | } |
| 598 | |
| 599 | static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue) |
| 600 | { |
| 601 | nvme_rdma_destroy_queue_ib(queue); |
| 602 | rdma_destroy_id(queue->cm_id); |
| 603 | } |
| 604 | |
| 605 | static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue) |
| 606 | { |
Sagi Grimberg | e89ca58 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 607 | if (test_and_set_bit(NVME_RDMA_Q_DELETING, &queue->flags)) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 608 | return; |
| 609 | nvme_rdma_stop_queue(queue); |
| 610 | nvme_rdma_free_queue(queue); |
| 611 | } |
| 612 | |
| 613 | static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl) |
| 614 | { |
| 615 | int i; |
| 616 | |
| 617 | for (i = 1; i < ctrl->queue_count; i++) |
| 618 | nvme_rdma_stop_and_free_queue(&ctrl->queues[i]); |
| 619 | } |
| 620 | |
| 621 | static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl) |
| 622 | { |
| 623 | int i, ret = 0; |
| 624 | |
| 625 | for (i = 1; i < ctrl->queue_count; i++) { |
| 626 | ret = nvmf_connect_io_queue(&ctrl->ctrl, i); |
Steve Wise | c8dbc37 | 2016-11-08 09:16:02 -0800 | [diff] [blame] | 627 | if (ret) { |
| 628 | dev_info(ctrl->ctrl.device, |
| 629 | "failed to connect i/o queue: %d\n", ret); |
| 630 | goto out_free_queues; |
| 631 | } |
Christoph Hellwig | 553cd9e | 2016-11-02 08:49:18 -0600 | [diff] [blame] | 632 | set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 633 | } |
| 634 | |
Steve Wise | c8dbc37 | 2016-11-08 09:16:02 -0800 | [diff] [blame] | 635 | return 0; |
| 636 | |
| 637 | out_free_queues: |
| 638 | nvme_rdma_free_io_queues(ctrl); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 639 | return ret; |
| 640 | } |
| 641 | |
| 642 | static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl) |
| 643 | { |
Sagi Grimberg | 9b6f9da | 2017-03-09 13:26:07 +0200 | [diff] [blame] | 644 | struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; |
| 645 | unsigned int nr_io_queues; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 646 | int i, ret; |
| 647 | |
Sagi Grimberg | 9b6f9da | 2017-03-09 13:26:07 +0200 | [diff] [blame] | 648 | nr_io_queues = min(opts->nr_io_queues, num_online_cpus()); |
| 649 | ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues); |
| 650 | if (ret) |
| 651 | return ret; |
| 652 | |
| 653 | ctrl->queue_count = nr_io_queues + 1; |
| 654 | if (ctrl->queue_count < 2) |
| 655 | return 0; |
| 656 | |
| 657 | dev_info(ctrl->ctrl.device, |
| 658 | "creating %d I/O queues.\n", nr_io_queues); |
| 659 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 660 | for (i = 1; i < ctrl->queue_count; i++) { |
Jay Freyensee | c5af865 | 2016-08-17 15:00:27 -0700 | [diff] [blame] | 661 | ret = nvme_rdma_init_queue(ctrl, i, |
| 662 | ctrl->ctrl.opts->queue_size); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 663 | if (ret) { |
| 664 | dev_info(ctrl->ctrl.device, |
| 665 | "failed to initialize i/o queue: %d\n", ret); |
| 666 | goto out_free_queues; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | |
| 672 | out_free_queues: |
Steve Wise | f361e5a | 2016-09-02 09:01:27 -0700 | [diff] [blame] | 673 | for (i--; i >= 1; i--) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 674 | nvme_rdma_stop_and_free_queue(&ctrl->queues[i]); |
| 675 | |
| 676 | return ret; |
| 677 | } |
| 678 | |
| 679 | static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl) |
| 680 | { |
| 681 | nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe, |
| 682 | sizeof(struct nvme_command), DMA_TO_DEVICE); |
| 683 | nvme_rdma_stop_and_free_queue(&ctrl->queues[0]); |
| 684 | blk_cleanup_queue(ctrl->ctrl.admin_q); |
| 685 | blk_mq_free_tag_set(&ctrl->admin_tag_set); |
| 686 | nvme_rdma_dev_put(ctrl->device); |
| 687 | } |
| 688 | |
| 689 | static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl) |
| 690 | { |
| 691 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); |
| 692 | |
| 693 | if (list_empty(&ctrl->list)) |
| 694 | goto free_ctrl; |
| 695 | |
| 696 | mutex_lock(&nvme_rdma_ctrl_mutex); |
| 697 | list_del(&ctrl->list); |
| 698 | mutex_unlock(&nvme_rdma_ctrl_mutex); |
| 699 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 700 | kfree(ctrl->queues); |
| 701 | nvmf_free_options(nctrl->opts); |
| 702 | free_ctrl: |
| 703 | kfree(ctrl); |
| 704 | } |
| 705 | |
| 706 | static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work) |
| 707 | { |
| 708 | struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work), |
| 709 | struct nvme_rdma_ctrl, reconnect_work); |
| 710 | bool changed; |
| 711 | int ret; |
| 712 | |
| 713 | if (ctrl->queue_count > 1) { |
| 714 | nvme_rdma_free_io_queues(ctrl); |
| 715 | |
| 716 | ret = blk_mq_reinit_tagset(&ctrl->tag_set); |
| 717 | if (ret) |
| 718 | goto requeue; |
| 719 | } |
| 720 | |
| 721 | nvme_rdma_stop_and_free_queue(&ctrl->queues[0]); |
| 722 | |
| 723 | ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set); |
| 724 | if (ret) |
| 725 | goto requeue; |
| 726 | |
| 727 | ret = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH); |
| 728 | if (ret) |
| 729 | goto requeue; |
| 730 | |
| 731 | blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true); |
| 732 | |
| 733 | ret = nvmf_connect_admin_queue(&ctrl->ctrl); |
| 734 | if (ret) |
| 735 | goto stop_admin_q; |
| 736 | |
Christoph Hellwig | 553cd9e | 2016-11-02 08:49:18 -0600 | [diff] [blame] | 737 | set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags); |
| 738 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 739 | ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap); |
| 740 | if (ret) |
| 741 | goto stop_admin_q; |
| 742 | |
| 743 | nvme_start_keep_alive(&ctrl->ctrl); |
| 744 | |
| 745 | if (ctrl->queue_count > 1) { |
| 746 | ret = nvme_rdma_init_io_queues(ctrl); |
| 747 | if (ret) |
| 748 | goto stop_admin_q; |
| 749 | |
| 750 | ret = nvme_rdma_connect_io_queues(ctrl); |
| 751 | if (ret) |
| 752 | goto stop_admin_q; |
| 753 | } |
| 754 | |
| 755 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); |
| 756 | WARN_ON_ONCE(!changed); |
| 757 | |
Sagi Grimberg | 5f372eb | 2016-07-31 18:43:15 +0300 | [diff] [blame] | 758 | if (ctrl->queue_count > 1) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 759 | nvme_start_queues(&ctrl->ctrl); |
Sagi Grimberg | 5f372eb | 2016-07-31 18:43:15 +0300 | [diff] [blame] | 760 | nvme_queue_scan(&ctrl->ctrl); |
Sagi Grimberg | 3ef1b4b | 2016-08-04 13:46:19 +0300 | [diff] [blame] | 761 | nvme_queue_async_events(&ctrl->ctrl); |
Sagi Grimberg | 5f372eb | 2016-07-31 18:43:15 +0300 | [diff] [blame] | 762 | } |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 763 | |
| 764 | dev_info(ctrl->ctrl.device, "Successfully reconnected\n"); |
| 765 | |
| 766 | return; |
| 767 | |
| 768 | stop_admin_q: |
| 769 | blk_mq_stop_hw_queues(ctrl->ctrl.admin_q); |
| 770 | requeue: |
| 771 | /* Make sure we are not resetting/deleting */ |
| 772 | if (ctrl->ctrl.state == NVME_CTRL_RECONNECTING) { |
| 773 | dev_info(ctrl->ctrl.device, |
| 774 | "Failed reconnect attempt, requeueing...\n"); |
| 775 | queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work, |
| 776 | ctrl->reconnect_delay * HZ); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | static void nvme_rdma_error_recovery_work(struct work_struct *work) |
| 781 | { |
| 782 | struct nvme_rdma_ctrl *ctrl = container_of(work, |
| 783 | struct nvme_rdma_ctrl, err_work); |
Sagi Grimberg | e89ca58 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 784 | int i; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 785 | |
| 786 | nvme_stop_keep_alive(&ctrl->ctrl); |
Sagi Grimberg | e89ca58 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 787 | |
Christoph Hellwig | 553cd9e | 2016-11-02 08:49:18 -0600 | [diff] [blame] | 788 | for (i = 0; i < ctrl->queue_count; i++) { |
Sagi Grimberg | e89ca58 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 789 | clear_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[i].flags); |
Christoph Hellwig | 553cd9e | 2016-11-02 08:49:18 -0600 | [diff] [blame] | 790 | clear_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags); |
| 791 | } |
Sagi Grimberg | e89ca58 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 792 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 793 | if (ctrl->queue_count > 1) |
| 794 | nvme_stop_queues(&ctrl->ctrl); |
| 795 | blk_mq_stop_hw_queues(ctrl->ctrl.admin_q); |
| 796 | |
| 797 | /* We must take care of fastfail/requeue all our inflight requests */ |
| 798 | if (ctrl->queue_count > 1) |
| 799 | blk_mq_tagset_busy_iter(&ctrl->tag_set, |
| 800 | nvme_cancel_request, &ctrl->ctrl); |
| 801 | blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, |
| 802 | nvme_cancel_request, &ctrl->ctrl); |
| 803 | |
| 804 | dev_info(ctrl->ctrl.device, "reconnecting in %d seconds\n", |
| 805 | ctrl->reconnect_delay); |
| 806 | |
| 807 | queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work, |
| 808 | ctrl->reconnect_delay * HZ); |
| 809 | } |
| 810 | |
| 811 | static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl) |
| 812 | { |
| 813 | if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING)) |
| 814 | return; |
| 815 | |
| 816 | queue_work(nvme_rdma_wq, &ctrl->err_work); |
| 817 | } |
| 818 | |
| 819 | static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc, |
| 820 | const char *op) |
| 821 | { |
| 822 | struct nvme_rdma_queue *queue = cq->cq_context; |
| 823 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; |
| 824 | |
| 825 | if (ctrl->ctrl.state == NVME_CTRL_LIVE) |
| 826 | dev_info(ctrl->ctrl.device, |
| 827 | "%s for CQE 0x%p failed with status %s (%d)\n", |
| 828 | op, wc->wr_cqe, |
| 829 | ib_wc_status_msg(wc->status), wc->status); |
| 830 | nvme_rdma_error_recovery(ctrl); |
| 831 | } |
| 832 | |
| 833 | static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc) |
| 834 | { |
| 835 | if (unlikely(wc->status != IB_WC_SUCCESS)) |
| 836 | nvme_rdma_wr_error(cq, wc, "MEMREG"); |
| 837 | } |
| 838 | |
| 839 | static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc) |
| 840 | { |
| 841 | if (unlikely(wc->status != IB_WC_SUCCESS)) |
| 842 | nvme_rdma_wr_error(cq, wc, "LOCAL_INV"); |
| 843 | } |
| 844 | |
| 845 | static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue, |
| 846 | struct nvme_rdma_request *req) |
| 847 | { |
| 848 | struct ib_send_wr *bad_wr; |
| 849 | struct ib_send_wr wr = { |
| 850 | .opcode = IB_WR_LOCAL_INV, |
| 851 | .next = NULL, |
| 852 | .num_sge = 0, |
| 853 | .send_flags = 0, |
| 854 | .ex.invalidate_rkey = req->mr->rkey, |
| 855 | }; |
| 856 | |
| 857 | req->reg_cqe.done = nvme_rdma_inv_rkey_done; |
| 858 | wr.wr_cqe = &req->reg_cqe; |
| 859 | |
| 860 | return ib_post_send(queue->qp, &wr, &bad_wr); |
| 861 | } |
| 862 | |
| 863 | static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue, |
| 864 | struct request *rq) |
| 865 | { |
| 866 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 867 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; |
| 868 | struct nvme_rdma_device *dev = queue->device; |
| 869 | struct ib_device *ibdev = dev->dev; |
| 870 | int res; |
| 871 | |
| 872 | if (!blk_rq_bytes(rq)) |
| 873 | return; |
| 874 | |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 875 | if (req->mr->need_inval) { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 876 | res = nvme_rdma_inv_rkey(queue, req); |
| 877 | if (res < 0) { |
| 878 | dev_err(ctrl->ctrl.device, |
| 879 | "Queueing INV WR for rkey %#x failed (%d)\n", |
| 880 | req->mr->rkey, res); |
| 881 | nvme_rdma_error_recovery(queue->ctrl); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | ib_dma_unmap_sg(ibdev, req->sg_table.sgl, |
| 886 | req->nents, rq_data_dir(rq) == |
| 887 | WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 888 | |
| 889 | nvme_cleanup_cmd(rq); |
| 890 | sg_free_table_chained(&req->sg_table, true); |
| 891 | } |
| 892 | |
| 893 | static int nvme_rdma_set_sg_null(struct nvme_command *c) |
| 894 | { |
| 895 | struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; |
| 896 | |
| 897 | sg->addr = 0; |
| 898 | put_unaligned_le24(0, sg->length); |
| 899 | put_unaligned_le32(0, sg->key); |
| 900 | sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; |
| 901 | return 0; |
| 902 | } |
| 903 | |
| 904 | static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue, |
| 905 | struct nvme_rdma_request *req, struct nvme_command *c) |
| 906 | { |
| 907 | struct nvme_sgl_desc *sg = &c->common.dptr.sgl; |
| 908 | |
| 909 | req->sge[1].addr = sg_dma_address(req->sg_table.sgl); |
| 910 | req->sge[1].length = sg_dma_len(req->sg_table.sgl); |
| 911 | req->sge[1].lkey = queue->device->pd->local_dma_lkey; |
| 912 | |
| 913 | sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff); |
| 914 | sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl)); |
| 915 | sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET; |
| 916 | |
| 917 | req->inline_data = true; |
| 918 | req->num_sge++; |
| 919 | return 0; |
| 920 | } |
| 921 | |
| 922 | static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue, |
| 923 | struct nvme_rdma_request *req, struct nvme_command *c) |
| 924 | { |
| 925 | struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; |
| 926 | |
| 927 | sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl)); |
| 928 | put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length); |
Christoph Hellwig | 11975e0 | 2016-09-05 12:56:20 +0200 | [diff] [blame] | 929 | put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 930 | sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; |
| 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue, |
| 935 | struct nvme_rdma_request *req, struct nvme_command *c, |
| 936 | int count) |
| 937 | { |
| 938 | struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; |
| 939 | int nr; |
| 940 | |
| 941 | nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE); |
| 942 | if (nr < count) { |
| 943 | if (nr < 0) |
| 944 | return nr; |
| 945 | return -EINVAL; |
| 946 | } |
| 947 | |
| 948 | ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey)); |
| 949 | |
| 950 | req->reg_cqe.done = nvme_rdma_memreg_done; |
| 951 | memset(&req->reg_wr, 0, sizeof(req->reg_wr)); |
| 952 | req->reg_wr.wr.opcode = IB_WR_REG_MR; |
| 953 | req->reg_wr.wr.wr_cqe = &req->reg_cqe; |
| 954 | req->reg_wr.wr.num_sge = 0; |
| 955 | req->reg_wr.mr = req->mr; |
| 956 | req->reg_wr.key = req->mr->rkey; |
| 957 | req->reg_wr.access = IB_ACCESS_LOCAL_WRITE | |
| 958 | IB_ACCESS_REMOTE_READ | |
| 959 | IB_ACCESS_REMOTE_WRITE; |
| 960 | |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 961 | req->mr->need_inval = true; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 962 | |
| 963 | sg->addr = cpu_to_le64(req->mr->iova); |
| 964 | put_unaligned_le24(req->mr->length, sg->length); |
| 965 | put_unaligned_le32(req->mr->rkey, sg->key); |
| 966 | sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) | |
| 967 | NVME_SGL_FMT_INVALIDATE; |
| 968 | |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | static int nvme_rdma_map_data(struct nvme_rdma_queue *queue, |
| 973 | struct request *rq, unsigned int map_len, |
| 974 | struct nvme_command *c) |
| 975 | { |
| 976 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 977 | struct nvme_rdma_device *dev = queue->device; |
| 978 | struct ib_device *ibdev = dev->dev; |
| 979 | int nents, count; |
| 980 | int ret; |
| 981 | |
| 982 | req->num_sge = 1; |
| 983 | req->inline_data = false; |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 984 | req->mr->need_inval = false; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 985 | |
| 986 | c->common.flags |= NVME_CMD_SGL_METABUF; |
| 987 | |
| 988 | if (!blk_rq_bytes(rq)) |
| 989 | return nvme_rdma_set_sg_null(c); |
| 990 | |
| 991 | req->sg_table.sgl = req->first_sgl; |
| 992 | ret = sg_alloc_table_chained(&req->sg_table, rq->nr_phys_segments, |
| 993 | req->sg_table.sgl); |
| 994 | if (ret) |
| 995 | return -ENOMEM; |
| 996 | |
| 997 | nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl); |
| 998 | BUG_ON(nents > rq->nr_phys_segments); |
| 999 | req->nents = nents; |
| 1000 | |
| 1001 | count = ib_dma_map_sg(ibdev, req->sg_table.sgl, nents, |
| 1002 | rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 1003 | if (unlikely(count <= 0)) { |
| 1004 | sg_free_table_chained(&req->sg_table, true); |
| 1005 | return -EIO; |
| 1006 | } |
| 1007 | |
| 1008 | if (count == 1) { |
| 1009 | if (rq_data_dir(rq) == WRITE && |
| 1010 | map_len <= nvme_rdma_inline_data_size(queue) && |
| 1011 | nvme_rdma_queue_idx(queue)) |
| 1012 | return nvme_rdma_map_sg_inline(queue, req, c); |
| 1013 | |
Christoph Hellwig | 11975e0 | 2016-09-05 12:56:20 +0200 | [diff] [blame] | 1014 | if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1015 | return nvme_rdma_map_sg_single(queue, req, c); |
| 1016 | } |
| 1017 | |
| 1018 | return nvme_rdma_map_sg_fr(queue, req, c, count); |
| 1019 | } |
| 1020 | |
| 1021 | static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc) |
| 1022 | { |
| 1023 | if (unlikely(wc->status != IB_WC_SUCCESS)) |
| 1024 | nvme_rdma_wr_error(cq, wc, "SEND"); |
| 1025 | } |
| 1026 | |
Marta Rybczynska | d17cc7b | 2017-06-06 13:27:21 +0200 | [diff] [blame] | 1027 | /* |
| 1028 | * We want to signal completion at least every queue depth/2. This returns the |
| 1029 | * largest power of two that is not above half of (queue size + 1) to optimize |
| 1030 | * (avoid divisions). |
| 1031 | */ |
| 1032 | static inline bool nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue) |
Marta Rybczynska | ae05780 | 2017-04-10 17:12:34 +0200 | [diff] [blame] | 1033 | { |
Marta Rybczynska | d17cc7b | 2017-06-06 13:27:21 +0200 | [diff] [blame] | 1034 | int limit = 1 << ilog2((queue->queue_size + 1) / 2); |
Marta Rybczynska | ae05780 | 2017-04-10 17:12:34 +0200 | [diff] [blame] | 1035 | |
Marta Rybczynska | d17cc7b | 2017-06-06 13:27:21 +0200 | [diff] [blame] | 1036 | return (atomic_inc_return(&queue->sig_count) & (limit - 1)) == 0; |
Marta Rybczynska | ae05780 | 2017-04-10 17:12:34 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1039 | static int nvme_rdma_post_send(struct nvme_rdma_queue *queue, |
| 1040 | struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge, |
| 1041 | struct ib_send_wr *first, bool flush) |
| 1042 | { |
| 1043 | struct ib_send_wr wr, *bad_wr; |
| 1044 | int ret; |
| 1045 | |
| 1046 | sge->addr = qe->dma; |
| 1047 | sge->length = sizeof(struct nvme_command), |
| 1048 | sge->lkey = queue->device->pd->local_dma_lkey; |
| 1049 | |
| 1050 | qe->cqe.done = nvme_rdma_send_done; |
| 1051 | |
| 1052 | wr.next = NULL; |
| 1053 | wr.wr_cqe = &qe->cqe; |
| 1054 | wr.sg_list = sge; |
| 1055 | wr.num_sge = num_sge; |
| 1056 | wr.opcode = IB_WR_SEND; |
| 1057 | wr.send_flags = 0; |
| 1058 | |
| 1059 | /* |
| 1060 | * Unsignalled send completions are another giant desaster in the |
| 1061 | * IB Verbs spec: If we don't regularly post signalled sends |
| 1062 | * the send queue will fill up and only a QP reset will rescue us. |
| 1063 | * Would have been way to obvious to handle this in hardware or |
| 1064 | * at least the RDMA stack.. |
| 1065 | * |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1066 | * Always signal the flushes. The magic request used for the flush |
| 1067 | * sequencer is not allocated in our driver's tagset and it's |
| 1068 | * triggered to be freed by blk_cleanup_queue(). So we need to |
| 1069 | * always mark it as signaled to ensure that the "wr_cqe", which is |
| 1070 | * embeded in request's payload, is not freed when __ib_process_cq() |
| 1071 | * calls wr_cqe->done(). |
| 1072 | */ |
Marta Rybczynska | ae05780 | 2017-04-10 17:12:34 +0200 | [diff] [blame] | 1073 | if (nvme_rdma_queue_sig_limit(queue) || flush) |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1074 | wr.send_flags |= IB_SEND_SIGNALED; |
| 1075 | |
| 1076 | if (first) |
| 1077 | first->next = ≀ |
| 1078 | else |
| 1079 | first = ≀ |
| 1080 | |
| 1081 | ret = ib_post_send(queue->qp, first, &bad_wr); |
| 1082 | if (ret) { |
| 1083 | dev_err(queue->ctrl->ctrl.device, |
| 1084 | "%s failed with error code %d\n", __func__, ret); |
| 1085 | } |
| 1086 | return ret; |
| 1087 | } |
| 1088 | |
| 1089 | static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue, |
| 1090 | struct nvme_rdma_qe *qe) |
| 1091 | { |
| 1092 | struct ib_recv_wr wr, *bad_wr; |
| 1093 | struct ib_sge list; |
| 1094 | int ret; |
| 1095 | |
| 1096 | list.addr = qe->dma; |
| 1097 | list.length = sizeof(struct nvme_completion); |
| 1098 | list.lkey = queue->device->pd->local_dma_lkey; |
| 1099 | |
| 1100 | qe->cqe.done = nvme_rdma_recv_done; |
| 1101 | |
| 1102 | wr.next = NULL; |
| 1103 | wr.wr_cqe = &qe->cqe; |
| 1104 | wr.sg_list = &list; |
| 1105 | wr.num_sge = 1; |
| 1106 | |
| 1107 | ret = ib_post_recv(queue->qp, &wr, &bad_wr); |
| 1108 | if (ret) { |
| 1109 | dev_err(queue->ctrl->ctrl.device, |
| 1110 | "%s failed with error code %d\n", __func__, ret); |
| 1111 | } |
| 1112 | return ret; |
| 1113 | } |
| 1114 | |
| 1115 | static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue) |
| 1116 | { |
| 1117 | u32 queue_idx = nvme_rdma_queue_idx(queue); |
| 1118 | |
| 1119 | if (queue_idx == 0) |
| 1120 | return queue->ctrl->admin_tag_set.tags[queue_idx]; |
| 1121 | return queue->ctrl->tag_set.tags[queue_idx - 1]; |
| 1122 | } |
| 1123 | |
| 1124 | static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx) |
| 1125 | { |
| 1126 | struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg); |
| 1127 | struct nvme_rdma_queue *queue = &ctrl->queues[0]; |
| 1128 | struct ib_device *dev = queue->device->dev; |
| 1129 | struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe; |
| 1130 | struct nvme_command *cmd = sqe->data; |
| 1131 | struct ib_sge sge; |
| 1132 | int ret; |
| 1133 | |
| 1134 | if (WARN_ON_ONCE(aer_idx != 0)) |
| 1135 | return; |
| 1136 | |
| 1137 | ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE); |
| 1138 | |
| 1139 | memset(cmd, 0, sizeof(*cmd)); |
| 1140 | cmd->common.opcode = nvme_admin_async_event; |
| 1141 | cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH; |
| 1142 | cmd->common.flags |= NVME_CMD_SGL_METABUF; |
| 1143 | nvme_rdma_set_sg_null(cmd); |
| 1144 | |
| 1145 | ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd), |
| 1146 | DMA_TO_DEVICE); |
| 1147 | |
| 1148 | ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false); |
| 1149 | WARN_ON_ONCE(ret); |
| 1150 | } |
| 1151 | |
| 1152 | static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue, |
| 1153 | struct nvme_completion *cqe, struct ib_wc *wc, int tag) |
| 1154 | { |
| 1155 | u16 status = le16_to_cpu(cqe->status); |
| 1156 | struct request *rq; |
| 1157 | struct nvme_rdma_request *req; |
| 1158 | int ret = 0; |
| 1159 | |
| 1160 | status >>= 1; |
| 1161 | |
| 1162 | rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id); |
| 1163 | if (!rq) { |
| 1164 | dev_err(queue->ctrl->ctrl.device, |
| 1165 | "tag 0x%x on QP %#x not found\n", |
| 1166 | cqe->command_id, queue->qp->qp_num); |
| 1167 | nvme_rdma_error_recovery(queue->ctrl); |
| 1168 | return ret; |
| 1169 | } |
| 1170 | req = blk_mq_rq_to_pdu(rq); |
| 1171 | |
| 1172 | if (rq->cmd_type == REQ_TYPE_DRV_PRIV && rq->special) |
| 1173 | memcpy(rq->special, cqe, sizeof(*cqe)); |
| 1174 | |
| 1175 | if (rq->tag == tag) |
| 1176 | ret = 1; |
| 1177 | |
| 1178 | if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) && |
| 1179 | wc->ex.invalidate_rkey == req->mr->rkey) |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 1180 | req->mr->need_inval = false; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1181 | |
| 1182 | blk_mq_complete_request(rq, status); |
| 1183 | |
| 1184 | return ret; |
| 1185 | } |
| 1186 | |
| 1187 | static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag) |
| 1188 | { |
| 1189 | struct nvme_rdma_qe *qe = |
| 1190 | container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe); |
| 1191 | struct nvme_rdma_queue *queue = cq->cq_context; |
| 1192 | struct ib_device *ibdev = queue->device->dev; |
| 1193 | struct nvme_completion *cqe = qe->data; |
| 1194 | const size_t len = sizeof(struct nvme_completion); |
| 1195 | int ret = 0; |
| 1196 | |
| 1197 | if (unlikely(wc->status != IB_WC_SUCCESS)) { |
| 1198 | nvme_rdma_wr_error(cq, wc, "RECV"); |
| 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE); |
| 1203 | /* |
| 1204 | * AEN requests are special as they don't time out and can |
| 1205 | * survive any kind of queue freeze and often don't respond to |
| 1206 | * aborts. We don't even bother to allocate a struct request |
| 1207 | * for them but rather special case them here. |
| 1208 | */ |
| 1209 | if (unlikely(nvme_rdma_queue_idx(queue) == 0 && |
| 1210 | cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH)) |
| 1211 | nvme_complete_async_event(&queue->ctrl->ctrl, cqe); |
| 1212 | else |
| 1213 | ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag); |
| 1214 | ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE); |
| 1215 | |
| 1216 | nvme_rdma_post_recv(queue, qe); |
| 1217 | return ret; |
| 1218 | } |
| 1219 | |
| 1220 | static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc) |
| 1221 | { |
| 1222 | __nvme_rdma_recv_done(cq, wc, -1); |
| 1223 | } |
| 1224 | |
| 1225 | static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue) |
| 1226 | { |
| 1227 | int ret, i; |
| 1228 | |
| 1229 | for (i = 0; i < queue->queue_size; i++) { |
| 1230 | ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]); |
| 1231 | if (ret) |
| 1232 | goto out_destroy_queue_ib; |
| 1233 | } |
| 1234 | |
| 1235 | return 0; |
| 1236 | |
| 1237 | out_destroy_queue_ib: |
| 1238 | nvme_rdma_destroy_queue_ib(queue); |
| 1239 | return ret; |
| 1240 | } |
| 1241 | |
| 1242 | static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue, |
| 1243 | struct rdma_cm_event *ev) |
| 1244 | { |
| 1245 | if (ev->param.conn.private_data_len) { |
| 1246 | struct nvme_rdma_cm_rej *rej = |
| 1247 | (struct nvme_rdma_cm_rej *)ev->param.conn.private_data; |
| 1248 | |
| 1249 | dev_err(queue->ctrl->ctrl.device, |
| 1250 | "Connect rejected, status %d.", le16_to_cpu(rej->sts)); |
| 1251 | /* XXX: Think of something clever to do here... */ |
| 1252 | } else { |
| 1253 | dev_err(queue->ctrl->ctrl.device, |
| 1254 | "Connect rejected, no private data.\n"); |
| 1255 | } |
| 1256 | |
| 1257 | return -ECONNRESET; |
| 1258 | } |
| 1259 | |
| 1260 | static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue) |
| 1261 | { |
| 1262 | struct nvme_rdma_device *dev; |
| 1263 | int ret; |
| 1264 | |
| 1265 | dev = nvme_rdma_find_get_device(queue->cm_id); |
| 1266 | if (!dev) { |
| 1267 | dev_err(queue->cm_id->device->dma_device, |
| 1268 | "no client data found!\n"); |
| 1269 | return -ECONNREFUSED; |
| 1270 | } |
| 1271 | |
| 1272 | ret = nvme_rdma_create_queue_ib(queue, dev); |
| 1273 | if (ret) { |
| 1274 | nvme_rdma_dev_put(dev); |
| 1275 | goto out; |
| 1276 | } |
| 1277 | |
| 1278 | ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS); |
| 1279 | if (ret) { |
| 1280 | dev_err(queue->ctrl->ctrl.device, |
| 1281 | "rdma_resolve_route failed (%d).\n", |
| 1282 | queue->cm_error); |
| 1283 | goto out_destroy_queue; |
| 1284 | } |
| 1285 | |
| 1286 | return 0; |
| 1287 | |
| 1288 | out_destroy_queue: |
| 1289 | nvme_rdma_destroy_queue_ib(queue); |
| 1290 | out: |
| 1291 | return ret; |
| 1292 | } |
| 1293 | |
| 1294 | static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue) |
| 1295 | { |
| 1296 | struct nvme_rdma_ctrl *ctrl = queue->ctrl; |
| 1297 | struct rdma_conn_param param = { }; |
Roland Dreier | 0b857b4 | 2016-07-31 00:27:39 -0700 | [diff] [blame] | 1298 | struct nvme_rdma_cm_req priv = { }; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1299 | int ret; |
| 1300 | |
| 1301 | param.qp_num = queue->qp->qp_num; |
| 1302 | param.flow_control = 1; |
| 1303 | |
| 1304 | param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom; |
Sagi Grimberg | 2ac17c2 | 2016-06-22 15:06:00 +0300 | [diff] [blame] | 1305 | /* maximum retry count */ |
| 1306 | param.retry_count = 7; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1307 | param.rnr_retry_count = 7; |
| 1308 | param.private_data = &priv; |
| 1309 | param.private_data_len = sizeof(priv); |
| 1310 | |
| 1311 | priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); |
| 1312 | priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue)); |
Jay Freyensee | f994d9d | 2016-08-17 15:00:26 -0700 | [diff] [blame] | 1313 | /* |
| 1314 | * set the admin queue depth to the minimum size |
| 1315 | * specified by the Fabrics standard. |
| 1316 | */ |
| 1317 | if (priv.qid == 0) { |
| 1318 | priv.hrqsize = cpu_to_le16(NVMF_AQ_DEPTH); |
| 1319 | priv.hsqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1); |
| 1320 | } else { |
Jay Freyensee | c5af865 | 2016-08-17 15:00:27 -0700 | [diff] [blame] | 1321 | /* |
| 1322 | * current interpretation of the fabrics spec |
| 1323 | * is at minimum you make hrqsize sqsize+1, or a |
| 1324 | * 1's based representation of sqsize. |
| 1325 | */ |
Jay Freyensee | f994d9d | 2016-08-17 15:00:26 -0700 | [diff] [blame] | 1326 | priv.hrqsize = cpu_to_le16(queue->queue_size); |
Jay Freyensee | c5af865 | 2016-08-17 15:00:27 -0700 | [diff] [blame] | 1327 | priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize); |
Jay Freyensee | f994d9d | 2016-08-17 15:00:26 -0700 | [diff] [blame] | 1328 | } |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1329 | |
| 1330 | ret = rdma_connect(queue->cm_id, ¶m); |
| 1331 | if (ret) { |
| 1332 | dev_err(ctrl->ctrl.device, |
| 1333 | "rdma_connect failed (%d).\n", ret); |
| 1334 | goto out_destroy_queue_ib; |
| 1335 | } |
| 1336 | |
| 1337 | return 0; |
| 1338 | |
| 1339 | out_destroy_queue_ib: |
| 1340 | nvme_rdma_destroy_queue_ib(queue); |
| 1341 | return ret; |
| 1342 | } |
| 1343 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1344 | static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, |
| 1345 | struct rdma_cm_event *ev) |
| 1346 | { |
| 1347 | struct nvme_rdma_queue *queue = cm_id->context; |
| 1348 | int cm_error = 0; |
| 1349 | |
| 1350 | dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n", |
| 1351 | rdma_event_msg(ev->event), ev->event, |
| 1352 | ev->status, cm_id); |
| 1353 | |
| 1354 | switch (ev->event) { |
| 1355 | case RDMA_CM_EVENT_ADDR_RESOLVED: |
| 1356 | cm_error = nvme_rdma_addr_resolved(queue); |
| 1357 | break; |
| 1358 | case RDMA_CM_EVENT_ROUTE_RESOLVED: |
| 1359 | cm_error = nvme_rdma_route_resolved(queue); |
| 1360 | break; |
| 1361 | case RDMA_CM_EVENT_ESTABLISHED: |
| 1362 | queue->cm_error = nvme_rdma_conn_established(queue); |
| 1363 | /* complete cm_done regardless of success/failure */ |
| 1364 | complete(&queue->cm_done); |
| 1365 | return 0; |
| 1366 | case RDMA_CM_EVENT_REJECTED: |
| 1367 | cm_error = nvme_rdma_conn_rejected(queue, ev); |
| 1368 | break; |
| 1369 | case RDMA_CM_EVENT_ADDR_ERROR: |
| 1370 | case RDMA_CM_EVENT_ROUTE_ERROR: |
| 1371 | case RDMA_CM_EVENT_CONNECT_ERROR: |
| 1372 | case RDMA_CM_EVENT_UNREACHABLE: |
| 1373 | dev_dbg(queue->ctrl->ctrl.device, |
| 1374 | "CM error event %d\n", ev->event); |
| 1375 | cm_error = -ECONNRESET; |
| 1376 | break; |
| 1377 | case RDMA_CM_EVENT_DISCONNECTED: |
| 1378 | case RDMA_CM_EVENT_ADDR_CHANGE: |
| 1379 | case RDMA_CM_EVENT_TIMEWAIT_EXIT: |
| 1380 | dev_dbg(queue->ctrl->ctrl.device, |
| 1381 | "disconnect received - connection closed\n"); |
| 1382 | nvme_rdma_error_recovery(queue->ctrl); |
| 1383 | break; |
| 1384 | case RDMA_CM_EVENT_DEVICE_REMOVAL: |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 1385 | /* device removal is handled via the ib_client API */ |
| 1386 | break; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1387 | default: |
| 1388 | dev_err(queue->ctrl->ctrl.device, |
| 1389 | "Unexpected RDMA CM event (%d)\n", ev->event); |
| 1390 | nvme_rdma_error_recovery(queue->ctrl); |
| 1391 | break; |
| 1392 | } |
| 1393 | |
| 1394 | if (cm_error) { |
| 1395 | queue->cm_error = cm_error; |
| 1396 | complete(&queue->cm_done); |
| 1397 | } |
| 1398 | |
| 1399 | return 0; |
| 1400 | } |
| 1401 | |
| 1402 | static enum blk_eh_timer_return |
| 1403 | nvme_rdma_timeout(struct request *rq, bool reserved) |
| 1404 | { |
| 1405 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 1406 | |
| 1407 | /* queue error recovery */ |
| 1408 | nvme_rdma_error_recovery(req->queue->ctrl); |
| 1409 | |
| 1410 | /* fail with DNR on cmd timeout */ |
| 1411 | rq->errors = NVME_SC_ABORT_REQ | NVME_SC_DNR; |
| 1412 | |
| 1413 | return BLK_EH_HANDLED; |
| 1414 | } |
| 1415 | |
Christoph Hellwig | 553cd9e | 2016-11-02 08:49:18 -0600 | [diff] [blame] | 1416 | /* |
| 1417 | * We cannot accept any other command until the Connect command has completed. |
| 1418 | */ |
| 1419 | static inline bool nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue, |
| 1420 | struct request *rq) |
| 1421 | { |
| 1422 | if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) { |
| 1423 | struct nvme_command *cmd = (struct nvme_command *)rq->cmd; |
| 1424 | |
| 1425 | if (rq->cmd_type != REQ_TYPE_DRV_PRIV || |
| 1426 | cmd->common.opcode != nvme_fabrics_command || |
| 1427 | cmd->fabrics.fctype != nvme_fabrics_type_connect) |
| 1428 | return false; |
| 1429 | } |
| 1430 | |
| 1431 | return true; |
| 1432 | } |
| 1433 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1434 | static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 1435 | const struct blk_mq_queue_data *bd) |
| 1436 | { |
| 1437 | struct nvme_ns *ns = hctx->queue->queuedata; |
| 1438 | struct nvme_rdma_queue *queue = hctx->driver_data; |
| 1439 | struct request *rq = bd->rq; |
| 1440 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 1441 | struct nvme_rdma_qe *sqe = &req->sqe; |
| 1442 | struct nvme_command *c = sqe->data; |
| 1443 | bool flush = false; |
| 1444 | struct ib_device *dev; |
| 1445 | unsigned int map_len; |
| 1446 | int ret; |
| 1447 | |
| 1448 | WARN_ON_ONCE(rq->tag < 0); |
| 1449 | |
Christoph Hellwig | 553cd9e | 2016-11-02 08:49:18 -0600 | [diff] [blame] | 1450 | if (!nvme_rdma_queue_is_ready(queue, rq)) |
| 1451 | return BLK_MQ_RQ_QUEUE_BUSY; |
| 1452 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1453 | dev = queue->device->dev; |
| 1454 | ib_dma_sync_single_for_cpu(dev, sqe->dma, |
| 1455 | sizeof(struct nvme_command), DMA_TO_DEVICE); |
| 1456 | |
| 1457 | ret = nvme_setup_cmd(ns, rq, c); |
| 1458 | if (ret) |
| 1459 | return ret; |
| 1460 | |
| 1461 | c->common.command_id = rq->tag; |
| 1462 | blk_mq_start_request(rq); |
| 1463 | |
| 1464 | map_len = nvme_map_len(rq); |
| 1465 | ret = nvme_rdma_map_data(queue, rq, map_len, c); |
| 1466 | if (ret < 0) { |
| 1467 | dev_err(queue->ctrl->ctrl.device, |
| 1468 | "Failed to map data (%d)\n", ret); |
| 1469 | nvme_cleanup_cmd(rq); |
| 1470 | goto err; |
| 1471 | } |
| 1472 | |
| 1473 | ib_dma_sync_single_for_device(dev, sqe->dma, |
| 1474 | sizeof(struct nvme_command), DMA_TO_DEVICE); |
| 1475 | |
| 1476 | if (rq->cmd_type == REQ_TYPE_FS && req_op(rq) == REQ_OP_FLUSH) |
| 1477 | flush = true; |
| 1478 | ret = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge, |
Sagi Grimberg | f5b7b55 | 2016-08-24 12:25:56 +0300 | [diff] [blame] | 1479 | req->mr->need_inval ? &req->reg_wr.wr : NULL, flush); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1480 | if (ret) { |
| 1481 | nvme_rdma_unmap_data(queue, rq); |
| 1482 | goto err; |
| 1483 | } |
| 1484 | |
| 1485 | return BLK_MQ_RQ_QUEUE_OK; |
| 1486 | err: |
| 1487 | return (ret == -ENOMEM || ret == -EAGAIN) ? |
| 1488 | BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR; |
| 1489 | } |
| 1490 | |
| 1491 | static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag) |
| 1492 | { |
| 1493 | struct nvme_rdma_queue *queue = hctx->driver_data; |
| 1494 | struct ib_cq *cq = queue->ib_cq; |
| 1495 | struct ib_wc wc; |
| 1496 | int found = 0; |
| 1497 | |
| 1498 | ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); |
| 1499 | while (ib_poll_cq(cq, 1, &wc) > 0) { |
| 1500 | struct ib_cqe *cqe = wc.wr_cqe; |
| 1501 | |
| 1502 | if (cqe) { |
| 1503 | if (cqe->done == nvme_rdma_recv_done) |
| 1504 | found |= __nvme_rdma_recv_done(cq, &wc, tag); |
| 1505 | else |
| 1506 | cqe->done(cq, &wc); |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | return found; |
| 1511 | } |
| 1512 | |
| 1513 | static void nvme_rdma_complete_rq(struct request *rq) |
| 1514 | { |
| 1515 | struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); |
| 1516 | struct nvme_rdma_queue *queue = req->queue; |
| 1517 | int error = 0; |
| 1518 | |
| 1519 | nvme_rdma_unmap_data(queue, rq); |
| 1520 | |
| 1521 | if (unlikely(rq->errors)) { |
| 1522 | if (nvme_req_needs_retry(rq, rq->errors)) { |
| 1523 | nvme_requeue_req(rq); |
| 1524 | return; |
| 1525 | } |
| 1526 | |
| 1527 | if (rq->cmd_type == REQ_TYPE_DRV_PRIV) |
| 1528 | error = rq->errors; |
| 1529 | else |
| 1530 | error = nvme_error_status(rq->errors); |
| 1531 | } |
| 1532 | |
| 1533 | blk_mq_end_request(rq, error); |
| 1534 | } |
| 1535 | |
| 1536 | static struct blk_mq_ops nvme_rdma_mq_ops = { |
| 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 | |
| 1547 | static struct blk_mq_ops nvme_rdma_admin_mq_ops = { |
| 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 = |
| 1613 | min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize); |
| 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, |
| 1796 | .is_fabrics = true, |
| 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 | |
| 1864 | static int nvme_rdma_parse_ipaddr(struct sockaddr_in *in_addr, char *p) |
| 1865 | { |
| 1866 | u8 *addr = (u8 *)&in_addr->sin_addr.s_addr; |
| 1867 | size_t buflen = strlen(p); |
| 1868 | |
| 1869 | /* XXX: handle IPv6 addresses */ |
| 1870 | |
| 1871 | if (buflen > INET_ADDRSTRLEN) |
| 1872 | return -EINVAL; |
| 1873 | if (in4_pton(p, buflen, addr, '\0', NULL) == 0) |
| 1874 | return -EINVAL; |
| 1875 | in_addr->sin_family = AF_INET; |
| 1876 | return 0; |
| 1877 | } |
| 1878 | |
| 1879 | static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev, |
| 1880 | struct nvmf_ctrl_options *opts) |
| 1881 | { |
| 1882 | struct nvme_rdma_ctrl *ctrl; |
| 1883 | int ret; |
| 1884 | bool changed; |
| 1885 | |
| 1886 | ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); |
| 1887 | if (!ctrl) |
| 1888 | return ERR_PTR(-ENOMEM); |
| 1889 | ctrl->ctrl.opts = opts; |
| 1890 | INIT_LIST_HEAD(&ctrl->list); |
| 1891 | |
| 1892 | ret = nvme_rdma_parse_ipaddr(&ctrl->addr_in, opts->traddr); |
| 1893 | if (ret) { |
| 1894 | pr_err("malformed IP address passed: %s\n", opts->traddr); |
| 1895 | goto out_free_ctrl; |
| 1896 | } |
| 1897 | |
| 1898 | if (opts->mask & NVMF_OPT_TRSVCID) { |
| 1899 | u16 port; |
| 1900 | |
| 1901 | ret = kstrtou16(opts->trsvcid, 0, &port); |
| 1902 | if (ret) |
| 1903 | goto out_free_ctrl; |
| 1904 | |
| 1905 | ctrl->addr_in.sin_port = cpu_to_be16(port); |
| 1906 | } else { |
| 1907 | ctrl->addr_in.sin_port = cpu_to_be16(NVME_RDMA_IP_PORT); |
| 1908 | } |
| 1909 | |
| 1910 | ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops, |
| 1911 | 0 /* no quirks, we're perfect! */); |
| 1912 | if (ret) |
| 1913 | goto out_free_ctrl; |
| 1914 | |
| 1915 | ctrl->reconnect_delay = opts->reconnect_delay; |
| 1916 | INIT_DELAYED_WORK(&ctrl->reconnect_work, |
| 1917 | nvme_rdma_reconnect_ctrl_work); |
| 1918 | INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work); |
| 1919 | INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work); |
| 1920 | INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work); |
| 1921 | spin_lock_init(&ctrl->lock); |
| 1922 | |
| 1923 | ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */ |
Jay Freyensee | c5af865 | 2016-08-17 15:00:27 -0700 | [diff] [blame] | 1924 | ctrl->ctrl.sqsize = opts->queue_size - 1; |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 1925 | ctrl->ctrl.kato = opts->kato; |
| 1926 | |
| 1927 | ret = -ENOMEM; |
| 1928 | ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues), |
| 1929 | GFP_KERNEL); |
| 1930 | if (!ctrl->queues) |
| 1931 | goto out_uninit_ctrl; |
| 1932 | |
| 1933 | ret = nvme_rdma_configure_admin_queue(ctrl); |
| 1934 | if (ret) |
| 1935 | goto out_kfree_queues; |
| 1936 | |
| 1937 | /* sanity check icdoff */ |
| 1938 | if (ctrl->ctrl.icdoff) { |
| 1939 | dev_err(ctrl->ctrl.device, "icdoff is not supported!\n"); |
| 1940 | goto out_remove_admin_queue; |
| 1941 | } |
| 1942 | |
| 1943 | /* sanity check keyed sgls */ |
| 1944 | if (!(ctrl->ctrl.sgls & (1 << 20))) { |
| 1945 | dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n"); |
| 1946 | goto out_remove_admin_queue; |
| 1947 | } |
| 1948 | |
| 1949 | if (opts->queue_size > ctrl->ctrl.maxcmd) { |
| 1950 | /* warn if maxcmd is lower than queue_size */ |
| 1951 | dev_warn(ctrl->ctrl.device, |
| 1952 | "queue_size %zu > ctrl maxcmd %u, clamping down\n", |
| 1953 | opts->queue_size, ctrl->ctrl.maxcmd); |
| 1954 | opts->queue_size = ctrl->ctrl.maxcmd; |
| 1955 | } |
| 1956 | |
| 1957 | if (opts->nr_io_queues) { |
| 1958 | ret = nvme_rdma_create_io_queues(ctrl); |
| 1959 | if (ret) |
| 1960 | goto out_remove_admin_queue; |
| 1961 | } |
| 1962 | |
| 1963 | changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); |
| 1964 | WARN_ON_ONCE(!changed); |
| 1965 | |
| 1966 | dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n", |
| 1967 | ctrl->ctrl.opts->subsysnqn, &ctrl->addr); |
| 1968 | |
| 1969 | kref_get(&ctrl->ctrl.kref); |
| 1970 | |
| 1971 | mutex_lock(&nvme_rdma_ctrl_mutex); |
| 1972 | list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list); |
| 1973 | mutex_unlock(&nvme_rdma_ctrl_mutex); |
| 1974 | |
| 1975 | if (opts->nr_io_queues) { |
| 1976 | nvme_queue_scan(&ctrl->ctrl); |
| 1977 | nvme_queue_async_events(&ctrl->ctrl); |
| 1978 | } |
| 1979 | |
| 1980 | return &ctrl->ctrl; |
| 1981 | |
| 1982 | out_remove_admin_queue: |
| 1983 | nvme_stop_keep_alive(&ctrl->ctrl); |
| 1984 | nvme_rdma_destroy_admin_queue(ctrl); |
| 1985 | out_kfree_queues: |
| 1986 | kfree(ctrl->queues); |
| 1987 | out_uninit_ctrl: |
| 1988 | nvme_uninit_ctrl(&ctrl->ctrl); |
| 1989 | nvme_put_ctrl(&ctrl->ctrl); |
| 1990 | if (ret > 0) |
| 1991 | ret = -EIO; |
| 1992 | return ERR_PTR(ret); |
| 1993 | out_free_ctrl: |
| 1994 | kfree(ctrl); |
| 1995 | return ERR_PTR(ret); |
| 1996 | } |
| 1997 | |
| 1998 | static struct nvmf_transport_ops nvme_rdma_transport = { |
| 1999 | .name = "rdma", |
| 2000 | .required_opts = NVMF_OPT_TRADDR, |
Sagi Grimberg | 2ac17c2 | 2016-06-22 15:06:00 +0300 | [diff] [blame] | 2001 | .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY, |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2002 | .create_ctrl = nvme_rdma_create_ctrl, |
| 2003 | }; |
| 2004 | |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2005 | static void nvme_rdma_add_one(struct ib_device *ib_device) |
| 2006 | { |
| 2007 | } |
| 2008 | |
| 2009 | static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data) |
| 2010 | { |
| 2011 | struct nvme_rdma_ctrl *ctrl; |
| 2012 | |
| 2013 | /* Delete all controllers using this device */ |
| 2014 | mutex_lock(&nvme_rdma_ctrl_mutex); |
| 2015 | list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) { |
| 2016 | if (ctrl->device->dev != ib_device) |
| 2017 | continue; |
| 2018 | dev_info(ctrl->ctrl.device, |
| 2019 | "Removing ctrl: NQN \"%s\", addr %pISp\n", |
| 2020 | ctrl->ctrl.opts->subsysnqn, &ctrl->addr); |
| 2021 | __nvme_rdma_del_ctrl(ctrl); |
| 2022 | } |
| 2023 | mutex_unlock(&nvme_rdma_ctrl_mutex); |
| 2024 | |
| 2025 | flush_workqueue(nvme_rdma_wq); |
| 2026 | } |
| 2027 | |
| 2028 | static struct ib_client nvme_rdma_ib_client = { |
| 2029 | .name = "nvme_rdma", |
| 2030 | .add = nvme_rdma_add_one, |
| 2031 | .remove = nvme_rdma_remove_one |
| 2032 | }; |
| 2033 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2034 | static int __init nvme_rdma_init_module(void) |
| 2035 | { |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2036 | int ret; |
| 2037 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2038 | nvme_rdma_wq = create_workqueue("nvme_rdma_wq"); |
| 2039 | if (!nvme_rdma_wq) |
| 2040 | return -ENOMEM; |
| 2041 | |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2042 | ret = ib_register_client(&nvme_rdma_ib_client); |
| 2043 | if (ret) { |
| 2044 | destroy_workqueue(nvme_rdma_wq); |
| 2045 | return ret; |
| 2046 | } |
| 2047 | |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2048 | nvmf_register_transport(&nvme_rdma_transport); |
| 2049 | return 0; |
| 2050 | } |
| 2051 | |
| 2052 | static void __exit nvme_rdma_cleanup_module(void) |
| 2053 | { |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2054 | nvmf_unregister_transport(&nvme_rdma_transport); |
Steve Wise | e87a911 | 2016-09-02 09:01:54 -0700 | [diff] [blame] | 2055 | ib_unregister_client(&nvme_rdma_ib_client); |
Christoph Hellwig | 7110230 | 2016-07-06 21:55:52 +0900 | [diff] [blame] | 2056 | destroy_workqueue(nvme_rdma_wq); |
| 2057 | } |
| 2058 | |
| 2059 | module_init(nvme_rdma_init_module); |
| 2060 | module_exit(nvme_rdma_cleanup_module); |
| 2061 | |
| 2062 | MODULE_LICENSE("GPL v2"); |