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