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