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 struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl, 701 bool admin) 702 { 703 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); 704 struct blk_mq_tag_set *set; 705 int ret; 706 707 if (admin) { 708 set = &ctrl->admin_tag_set; 709 memset(set, 0, sizeof(*set)); 710 set->ops = &nvme_rdma_admin_mq_ops; 711 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH; 712 set->reserved_tags = 2; /* connect + keep-alive */ 713 set->numa_node = nctrl->numa_node; 714 set->cmd_size = sizeof(struct nvme_rdma_request) + 715 SG_CHUNK_SIZE * sizeof(struct scatterlist); 716 set->driver_data = ctrl; 717 set->nr_hw_queues = 1; 718 set->timeout = ADMIN_TIMEOUT; 719 set->flags = BLK_MQ_F_NO_SCHED; 720 } else { 721 set = &ctrl->tag_set; 722 memset(set, 0, sizeof(*set)); 723 set->ops = &nvme_rdma_mq_ops; 724 set->queue_depth = nctrl->sqsize + 1; 725 set->reserved_tags = 1; /* fabric connect */ 726 set->numa_node = nctrl->numa_node; 727 set->flags = BLK_MQ_F_SHOULD_MERGE; 728 set->cmd_size = sizeof(struct nvme_rdma_request) + 729 SG_CHUNK_SIZE * sizeof(struct scatterlist); 730 set->driver_data = ctrl; 731 set->nr_hw_queues = nctrl->queue_count - 1; 732 set->timeout = NVME_IO_TIMEOUT; 733 set->nr_maps = nctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2; 734 } 735 736 ret = blk_mq_alloc_tag_set(set); 737 if (ret) 738 return ERR_PTR(ret); 739 740 return set; 741 } 742 743 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl, 744 bool remove) 745 { 746 if (remove) { 747 blk_cleanup_queue(ctrl->ctrl.admin_q); 748 blk_mq_free_tag_set(ctrl->ctrl.admin_tagset); 749 } 750 if (ctrl->async_event_sqe.data) { 751 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe, 752 sizeof(struct nvme_command), DMA_TO_DEVICE); 753 ctrl->async_event_sqe.data = NULL; 754 } 755 nvme_rdma_free_queue(&ctrl->queues[0]); 756 } 757 758 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl, 759 bool new) 760 { 761 int error; 762 763 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH); 764 if (error) 765 return error; 766 767 ctrl->device = ctrl->queues[0].device; 768 ctrl->ctrl.numa_node = dev_to_node(ctrl->device->dev->dma_device); 769 770 ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev); 771 772 error = nvme_rdma_alloc_qe(ctrl->device->dev, &ctrl->async_event_sqe, 773 sizeof(struct nvme_command), DMA_TO_DEVICE); 774 if (error) 775 goto out_free_queue; 776 777 if (new) { 778 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true); 779 if (IS_ERR(ctrl->ctrl.admin_tagset)) { 780 error = PTR_ERR(ctrl->ctrl.admin_tagset); 781 goto out_free_async_qe; 782 } 783 784 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set); 785 if (IS_ERR(ctrl->ctrl.admin_q)) { 786 error = PTR_ERR(ctrl->ctrl.admin_q); 787 goto out_free_tagset; 788 } 789 } 790 791 error = nvme_rdma_start_queue(ctrl, 0); 792 if (error) 793 goto out_cleanup_queue; 794 795 error = ctrl->ctrl.ops->reg_read64(&ctrl->ctrl, NVME_REG_CAP, 796 &ctrl->ctrl.cap); 797 if (error) { 798 dev_err(ctrl->ctrl.device, 799 "prop_get NVME_REG_CAP failed\n"); 800 goto out_stop_queue; 801 } 802 803 ctrl->ctrl.sqsize = 804 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize); 805 806 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap); 807 if (error) 808 goto out_stop_queue; 809 810 ctrl->ctrl.max_hw_sectors = 811 (ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9); 812 813 error = nvme_init_identify(&ctrl->ctrl); 814 if (error) 815 goto out_stop_queue; 816 817 return 0; 818 819 out_stop_queue: 820 nvme_rdma_stop_queue(&ctrl->queues[0]); 821 out_cleanup_queue: 822 if (new) 823 blk_cleanup_queue(ctrl->ctrl.admin_q); 824 out_free_tagset: 825 if (new) 826 blk_mq_free_tag_set(ctrl->ctrl.admin_tagset); 827 out_free_async_qe: 828 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe, 829 sizeof(struct nvme_command), DMA_TO_DEVICE); 830 ctrl->async_event_sqe.data = NULL; 831 out_free_queue: 832 nvme_rdma_free_queue(&ctrl->queues[0]); 833 return error; 834 } 835 836 static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl, 837 bool remove) 838 { 839 if (remove) { 840 blk_cleanup_queue(ctrl->ctrl.connect_q); 841 blk_mq_free_tag_set(ctrl->ctrl.tagset); 842 } 843 nvme_rdma_free_io_queues(ctrl); 844 } 845 846 static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new) 847 { 848 int ret; 849 850 ret = nvme_rdma_alloc_io_queues(ctrl); 851 if (ret) 852 return ret; 853 854 if (new) { 855 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false); 856 if (IS_ERR(ctrl->ctrl.tagset)) { 857 ret = PTR_ERR(ctrl->ctrl.tagset); 858 goto out_free_io_queues; 859 } 860 861 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set); 862 if (IS_ERR(ctrl->ctrl.connect_q)) { 863 ret = PTR_ERR(ctrl->ctrl.connect_q); 864 goto out_free_tag_set; 865 } 866 } else { 867 blk_mq_update_nr_hw_queues(&ctrl->tag_set, 868 ctrl->ctrl.queue_count - 1); 869 } 870 871 ret = nvme_rdma_start_io_queues(ctrl); 872 if (ret) 873 goto out_cleanup_connect_q; 874 875 return 0; 876 877 out_cleanup_connect_q: 878 if (new) 879 blk_cleanup_queue(ctrl->ctrl.connect_q); 880 out_free_tag_set: 881 if (new) 882 blk_mq_free_tag_set(ctrl->ctrl.tagset); 883 out_free_io_queues: 884 nvme_rdma_free_io_queues(ctrl); 885 return ret; 886 } 887 888 static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl *ctrl, 889 bool remove) 890 { 891 blk_mq_quiesce_queue(ctrl->ctrl.admin_q); 892 nvme_rdma_stop_queue(&ctrl->queues[0]); 893 if (ctrl->ctrl.admin_tagset) 894 blk_mq_tagset_busy_iter(ctrl->ctrl.admin_tagset, 895 nvme_cancel_request, &ctrl->ctrl); 896 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q); 897 nvme_rdma_destroy_admin_queue(ctrl, remove); 898 } 899 900 static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl *ctrl, 901 bool remove) 902 { 903 if (ctrl->ctrl.queue_count > 1) { 904 nvme_stop_queues(&ctrl->ctrl); 905 nvme_rdma_stop_io_queues(ctrl); 906 if (ctrl->ctrl.tagset) 907 blk_mq_tagset_busy_iter(ctrl->ctrl.tagset, 908 nvme_cancel_request, &ctrl->ctrl); 909 if (remove) 910 nvme_start_queues(&ctrl->ctrl); 911 nvme_rdma_destroy_io_queues(ctrl, remove); 912 } 913 } 914 915 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl) 916 { 917 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); 918 919 if (list_empty(&ctrl->list)) 920 goto free_ctrl; 921 922 mutex_lock(&nvme_rdma_ctrl_mutex); 923 list_del(&ctrl->list); 924 mutex_unlock(&nvme_rdma_ctrl_mutex); 925 926 nvmf_free_options(nctrl->opts); 927 free_ctrl: 928 kfree(ctrl->queues); 929 kfree(ctrl); 930 } 931 932 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl) 933 { 934 /* If we are resetting/deleting then do nothing */ 935 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) { 936 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW || 937 ctrl->ctrl.state == NVME_CTRL_LIVE); 938 return; 939 } 940 941 if (nvmf_should_reconnect(&ctrl->ctrl)) { 942 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n", 943 ctrl->ctrl.opts->reconnect_delay); 944 queue_delayed_work(nvme_wq, &ctrl->reconnect_work, 945 ctrl->ctrl.opts->reconnect_delay * HZ); 946 } else { 947 nvme_delete_ctrl(&ctrl->ctrl); 948 } 949 } 950 951 static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new) 952 { 953 int ret = -EINVAL; 954 bool changed; 955 956 ret = nvme_rdma_configure_admin_queue(ctrl, new); 957 if (ret) 958 return ret; 959 960 if (ctrl->ctrl.icdoff) { 961 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n"); 962 goto destroy_admin; 963 } 964 965 if (!(ctrl->ctrl.sgls & (1 << 2))) { 966 dev_err(ctrl->ctrl.device, 967 "Mandatory keyed sgls are not supported!\n"); 968 goto destroy_admin; 969 } 970 971 if (ctrl->ctrl.opts->queue_size > ctrl->ctrl.sqsize + 1) { 972 dev_warn(ctrl->ctrl.device, 973 "queue_size %zu > ctrl sqsize %u, clamping down\n", 974 ctrl->ctrl.opts->queue_size, ctrl->ctrl.sqsize + 1); 975 } 976 977 if (ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) { 978 dev_warn(ctrl->ctrl.device, 979 "sqsize %u > ctrl maxcmd %u, clamping down\n", 980 ctrl->ctrl.sqsize + 1, ctrl->ctrl.maxcmd); 981 ctrl->ctrl.sqsize = ctrl->ctrl.maxcmd - 1; 982 } 983 984 if (ctrl->ctrl.sgls & (1 << 20)) 985 ctrl->use_inline_data = true; 986 987 if (ctrl->ctrl.queue_count > 1) { 988 ret = nvme_rdma_configure_io_queues(ctrl, new); 989 if (ret) 990 goto destroy_admin; 991 } 992 993 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); 994 if (!changed) { 995 /* state change failure is ok if we're in DELETING state */ 996 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING); 997 ret = -EINVAL; 998 goto destroy_io; 999 } 1000 1001 nvme_start_ctrl(&ctrl->ctrl); 1002 return 0; 1003 1004 destroy_io: 1005 if (ctrl->ctrl.queue_count > 1) 1006 nvme_rdma_destroy_io_queues(ctrl, new); 1007 destroy_admin: 1008 nvme_rdma_stop_queue(&ctrl->queues[0]); 1009 nvme_rdma_destroy_admin_queue(ctrl, new); 1010 return ret; 1011 } 1012 1013 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work) 1014 { 1015 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work), 1016 struct nvme_rdma_ctrl, reconnect_work); 1017 1018 ++ctrl->ctrl.nr_reconnects; 1019 1020 if (nvme_rdma_setup_ctrl(ctrl, false)) 1021 goto requeue; 1022 1023 dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n", 1024 ctrl->ctrl.nr_reconnects); 1025 1026 ctrl->ctrl.nr_reconnects = 0; 1027 1028 return; 1029 1030 requeue: 1031 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n", 1032 ctrl->ctrl.nr_reconnects); 1033 nvme_rdma_reconnect_or_remove(ctrl); 1034 } 1035 1036 static void nvme_rdma_error_recovery_work(struct work_struct *work) 1037 { 1038 struct nvme_rdma_ctrl *ctrl = container_of(work, 1039 struct nvme_rdma_ctrl, err_work); 1040 1041 nvme_stop_keep_alive(&ctrl->ctrl); 1042 nvme_rdma_teardown_io_queues(ctrl, false); 1043 nvme_start_queues(&ctrl->ctrl); 1044 nvme_rdma_teardown_admin_queue(ctrl, false); 1045 1046 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) { 1047 /* state change failure is ok if we're in DELETING state */ 1048 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING); 1049 return; 1050 } 1051 1052 nvme_rdma_reconnect_or_remove(ctrl); 1053 } 1054 1055 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl) 1056 { 1057 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING)) 1058 return; 1059 1060 queue_work(nvme_wq, &ctrl->err_work); 1061 } 1062 1063 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc, 1064 const char *op) 1065 { 1066 struct nvme_rdma_queue *queue = cq->cq_context; 1067 struct nvme_rdma_ctrl *ctrl = queue->ctrl; 1068 1069 if (ctrl->ctrl.state == NVME_CTRL_LIVE) 1070 dev_info(ctrl->ctrl.device, 1071 "%s for CQE 0x%p failed with status %s (%d)\n", 1072 op, wc->wr_cqe, 1073 ib_wc_status_msg(wc->status), wc->status); 1074 nvme_rdma_error_recovery(ctrl); 1075 } 1076 1077 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc) 1078 { 1079 if (unlikely(wc->status != IB_WC_SUCCESS)) 1080 nvme_rdma_wr_error(cq, wc, "MEMREG"); 1081 } 1082 1083 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc) 1084 { 1085 struct nvme_rdma_request *req = 1086 container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe); 1087 struct request *rq = blk_mq_rq_from_pdu(req); 1088 1089 if (unlikely(wc->status != IB_WC_SUCCESS)) { 1090 nvme_rdma_wr_error(cq, wc, "LOCAL_INV"); 1091 return; 1092 } 1093 1094 if (refcount_dec_and_test(&req->ref)) 1095 nvme_end_request(rq, req->status, req->result); 1096 1097 } 1098 1099 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue, 1100 struct nvme_rdma_request *req) 1101 { 1102 struct ib_send_wr wr = { 1103 .opcode = IB_WR_LOCAL_INV, 1104 .next = NULL, 1105 .num_sge = 0, 1106 .send_flags = IB_SEND_SIGNALED, 1107 .ex.invalidate_rkey = req->mr->rkey, 1108 }; 1109 1110 req->reg_cqe.done = nvme_rdma_inv_rkey_done; 1111 wr.wr_cqe = &req->reg_cqe; 1112 1113 return ib_post_send(queue->qp, &wr, NULL); 1114 } 1115 1116 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue, 1117 struct request *rq) 1118 { 1119 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1120 struct nvme_rdma_device *dev = queue->device; 1121 struct ib_device *ibdev = dev->dev; 1122 1123 if (!blk_rq_nr_phys_segments(rq)) 1124 return; 1125 1126 if (req->mr) { 1127 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr); 1128 req->mr = NULL; 1129 } 1130 1131 ib_dma_unmap_sg(ibdev, req->sg_table.sgl, 1132 req->nents, rq_data_dir(rq) == 1133 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 1134 1135 nvme_cleanup_cmd(rq); 1136 sg_free_table_chained(&req->sg_table, true); 1137 } 1138 1139 static int nvme_rdma_set_sg_null(struct nvme_command *c) 1140 { 1141 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; 1142 1143 sg->addr = 0; 1144 put_unaligned_le24(0, sg->length); 1145 put_unaligned_le32(0, sg->key); 1146 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; 1147 return 0; 1148 } 1149 1150 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue, 1151 struct nvme_rdma_request *req, struct nvme_command *c, 1152 int count) 1153 { 1154 struct nvme_sgl_desc *sg = &c->common.dptr.sgl; 1155 struct scatterlist *sgl = req->sg_table.sgl; 1156 struct ib_sge *sge = &req->sge[1]; 1157 u32 len = 0; 1158 int i; 1159 1160 for (i = 0; i < count; i++, sgl++, sge++) { 1161 sge->addr = sg_dma_address(sgl); 1162 sge->length = sg_dma_len(sgl); 1163 sge->lkey = queue->device->pd->local_dma_lkey; 1164 len += sge->length; 1165 } 1166 1167 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff); 1168 sg->length = cpu_to_le32(len); 1169 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET; 1170 1171 req->num_sge += count; 1172 return 0; 1173 } 1174 1175 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue, 1176 struct nvme_rdma_request *req, struct nvme_command *c) 1177 { 1178 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; 1179 1180 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl)); 1181 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length); 1182 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key); 1183 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; 1184 return 0; 1185 } 1186 1187 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue, 1188 struct nvme_rdma_request *req, struct nvme_command *c, 1189 int count) 1190 { 1191 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; 1192 int nr; 1193 1194 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs); 1195 if (WARN_ON_ONCE(!req->mr)) 1196 return -EAGAIN; 1197 1198 /* 1199 * Align the MR to a 4K page size to match the ctrl page size and 1200 * the block virtual boundary. 1201 */ 1202 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K); 1203 if (unlikely(nr < count)) { 1204 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr); 1205 req->mr = NULL; 1206 if (nr < 0) 1207 return nr; 1208 return -EINVAL; 1209 } 1210 1211 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey)); 1212 1213 req->reg_cqe.done = nvme_rdma_memreg_done; 1214 memset(&req->reg_wr, 0, sizeof(req->reg_wr)); 1215 req->reg_wr.wr.opcode = IB_WR_REG_MR; 1216 req->reg_wr.wr.wr_cqe = &req->reg_cqe; 1217 req->reg_wr.wr.num_sge = 0; 1218 req->reg_wr.mr = req->mr; 1219 req->reg_wr.key = req->mr->rkey; 1220 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE | 1221 IB_ACCESS_REMOTE_READ | 1222 IB_ACCESS_REMOTE_WRITE; 1223 1224 sg->addr = cpu_to_le64(req->mr->iova); 1225 put_unaligned_le24(req->mr->length, sg->length); 1226 put_unaligned_le32(req->mr->rkey, sg->key); 1227 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) | 1228 NVME_SGL_FMT_INVALIDATE; 1229 1230 return 0; 1231 } 1232 1233 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue, 1234 struct request *rq, struct nvme_command *c) 1235 { 1236 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1237 struct nvme_rdma_device *dev = queue->device; 1238 struct ib_device *ibdev = dev->dev; 1239 int count, ret; 1240 1241 req->num_sge = 1; 1242 refcount_set(&req->ref, 2); /* send and recv completions */ 1243 1244 c->common.flags |= NVME_CMD_SGL_METABUF; 1245 1246 if (!blk_rq_nr_phys_segments(rq)) 1247 return nvme_rdma_set_sg_null(c); 1248 1249 req->sg_table.sgl = req->first_sgl; 1250 ret = sg_alloc_table_chained(&req->sg_table, 1251 blk_rq_nr_phys_segments(rq), req->sg_table.sgl); 1252 if (ret) 1253 return -ENOMEM; 1254 1255 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl); 1256 1257 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents, 1258 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 1259 if (unlikely(count <= 0)) { 1260 ret = -EIO; 1261 goto out_free_table; 1262 } 1263 1264 if (count <= dev->num_inline_segments) { 1265 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) && 1266 queue->ctrl->use_inline_data && 1267 blk_rq_payload_bytes(rq) <= 1268 nvme_rdma_inline_data_size(queue)) { 1269 ret = nvme_rdma_map_sg_inline(queue, req, c, count); 1270 goto out; 1271 } 1272 1273 if (count == 1 && dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) { 1274 ret = nvme_rdma_map_sg_single(queue, req, c); 1275 goto out; 1276 } 1277 } 1278 1279 ret = nvme_rdma_map_sg_fr(queue, req, c, count); 1280 out: 1281 if (unlikely(ret)) 1282 goto out_unmap_sg; 1283 1284 return 0; 1285 1286 out_unmap_sg: 1287 ib_dma_unmap_sg(ibdev, req->sg_table.sgl, 1288 req->nents, rq_data_dir(rq) == 1289 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 1290 out_free_table: 1291 sg_free_table_chained(&req->sg_table, true); 1292 return ret; 1293 } 1294 1295 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc) 1296 { 1297 struct nvme_rdma_qe *qe = 1298 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe); 1299 struct nvme_rdma_request *req = 1300 container_of(qe, struct nvme_rdma_request, sqe); 1301 struct request *rq = blk_mq_rq_from_pdu(req); 1302 1303 if (unlikely(wc->status != IB_WC_SUCCESS)) { 1304 nvme_rdma_wr_error(cq, wc, "SEND"); 1305 return; 1306 } 1307 1308 if (refcount_dec_and_test(&req->ref)) 1309 nvme_end_request(rq, req->status, req->result); 1310 } 1311 1312 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue, 1313 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge, 1314 struct ib_send_wr *first) 1315 { 1316 struct ib_send_wr wr; 1317 int ret; 1318 1319 sge->addr = qe->dma; 1320 sge->length = sizeof(struct nvme_command), 1321 sge->lkey = queue->device->pd->local_dma_lkey; 1322 1323 wr.next = NULL; 1324 wr.wr_cqe = &qe->cqe; 1325 wr.sg_list = sge; 1326 wr.num_sge = num_sge; 1327 wr.opcode = IB_WR_SEND; 1328 wr.send_flags = IB_SEND_SIGNALED; 1329 1330 if (first) 1331 first->next = ≀ 1332 else 1333 first = ≀ 1334 1335 ret = ib_post_send(queue->qp, first, NULL); 1336 if (unlikely(ret)) { 1337 dev_err(queue->ctrl->ctrl.device, 1338 "%s failed with error code %d\n", __func__, ret); 1339 } 1340 return ret; 1341 } 1342 1343 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue, 1344 struct nvme_rdma_qe *qe) 1345 { 1346 struct ib_recv_wr wr; 1347 struct ib_sge list; 1348 int ret; 1349 1350 list.addr = qe->dma; 1351 list.length = sizeof(struct nvme_completion); 1352 list.lkey = queue->device->pd->local_dma_lkey; 1353 1354 qe->cqe.done = nvme_rdma_recv_done; 1355 1356 wr.next = NULL; 1357 wr.wr_cqe = &qe->cqe; 1358 wr.sg_list = &list; 1359 wr.num_sge = 1; 1360 1361 ret = ib_post_recv(queue->qp, &wr, NULL); 1362 if (unlikely(ret)) { 1363 dev_err(queue->ctrl->ctrl.device, 1364 "%s failed with error code %d\n", __func__, ret); 1365 } 1366 return ret; 1367 } 1368 1369 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue) 1370 { 1371 u32 queue_idx = nvme_rdma_queue_idx(queue); 1372 1373 if (queue_idx == 0) 1374 return queue->ctrl->admin_tag_set.tags[queue_idx]; 1375 return queue->ctrl->tag_set.tags[queue_idx - 1]; 1376 } 1377 1378 static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc) 1379 { 1380 if (unlikely(wc->status != IB_WC_SUCCESS)) 1381 nvme_rdma_wr_error(cq, wc, "ASYNC"); 1382 } 1383 1384 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg) 1385 { 1386 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg); 1387 struct nvme_rdma_queue *queue = &ctrl->queues[0]; 1388 struct ib_device *dev = queue->device->dev; 1389 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe; 1390 struct nvme_command *cmd = sqe->data; 1391 struct ib_sge sge; 1392 int ret; 1393 1394 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE); 1395 1396 memset(cmd, 0, sizeof(*cmd)); 1397 cmd->common.opcode = nvme_admin_async_event; 1398 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH; 1399 cmd->common.flags |= NVME_CMD_SGL_METABUF; 1400 nvme_rdma_set_sg_null(cmd); 1401 1402 sqe->cqe.done = nvme_rdma_async_done; 1403 1404 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd), 1405 DMA_TO_DEVICE); 1406 1407 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL); 1408 WARN_ON_ONCE(ret); 1409 } 1410 1411 static void nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue, 1412 struct nvme_completion *cqe, struct ib_wc *wc) 1413 { 1414 struct request *rq; 1415 struct nvme_rdma_request *req; 1416 1417 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id); 1418 if (!rq) { 1419 dev_err(queue->ctrl->ctrl.device, 1420 "tag 0x%x on QP %#x not found\n", 1421 cqe->command_id, queue->qp->qp_num); 1422 nvme_rdma_error_recovery(queue->ctrl); 1423 return; 1424 } 1425 req = blk_mq_rq_to_pdu(rq); 1426 1427 req->status = cqe->status; 1428 req->result = cqe->result; 1429 1430 if (wc->wc_flags & IB_WC_WITH_INVALIDATE) { 1431 if (unlikely(wc->ex.invalidate_rkey != req->mr->rkey)) { 1432 dev_err(queue->ctrl->ctrl.device, 1433 "Bogus remote invalidation for rkey %#x\n", 1434 req->mr->rkey); 1435 nvme_rdma_error_recovery(queue->ctrl); 1436 } 1437 } else if (req->mr) { 1438 int ret; 1439 1440 ret = nvme_rdma_inv_rkey(queue, req); 1441 if (unlikely(ret < 0)) { 1442 dev_err(queue->ctrl->ctrl.device, 1443 "Queueing INV WR for rkey %#x failed (%d)\n", 1444 req->mr->rkey, ret); 1445 nvme_rdma_error_recovery(queue->ctrl); 1446 } 1447 /* the local invalidation completion will end the request */ 1448 return; 1449 } 1450 1451 if (refcount_dec_and_test(&req->ref)) 1452 nvme_end_request(rq, req->status, req->result); 1453 } 1454 1455 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc) 1456 { 1457 struct nvme_rdma_qe *qe = 1458 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe); 1459 struct nvme_rdma_queue *queue = cq->cq_context; 1460 struct ib_device *ibdev = queue->device->dev; 1461 struct nvme_completion *cqe = qe->data; 1462 const size_t len = sizeof(struct nvme_completion); 1463 1464 if (unlikely(wc->status != IB_WC_SUCCESS)) { 1465 nvme_rdma_wr_error(cq, wc, "RECV"); 1466 return; 1467 } 1468 1469 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE); 1470 /* 1471 * AEN requests are special as they don't time out and can 1472 * survive any kind of queue freeze and often don't respond to 1473 * aborts. We don't even bother to allocate a struct request 1474 * for them but rather special case them here. 1475 */ 1476 if (unlikely(nvme_rdma_queue_idx(queue) == 0 && 1477 cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH)) 1478 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status, 1479 &cqe->result); 1480 else 1481 nvme_rdma_process_nvme_rsp(queue, cqe, wc); 1482 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE); 1483 1484 nvme_rdma_post_recv(queue, qe); 1485 } 1486 1487 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue) 1488 { 1489 int ret, i; 1490 1491 for (i = 0; i < queue->queue_size; i++) { 1492 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]); 1493 if (ret) 1494 goto out_destroy_queue_ib; 1495 } 1496 1497 return 0; 1498 1499 out_destroy_queue_ib: 1500 nvme_rdma_destroy_queue_ib(queue); 1501 return ret; 1502 } 1503 1504 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue, 1505 struct rdma_cm_event *ev) 1506 { 1507 struct rdma_cm_id *cm_id = queue->cm_id; 1508 int status = ev->status; 1509 const char *rej_msg; 1510 const struct nvme_rdma_cm_rej *rej_data; 1511 u8 rej_data_len; 1512 1513 rej_msg = rdma_reject_msg(cm_id, status); 1514 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len); 1515 1516 if (rej_data && rej_data_len >= sizeof(u16)) { 1517 u16 sts = le16_to_cpu(rej_data->sts); 1518 1519 dev_err(queue->ctrl->ctrl.device, 1520 "Connect rejected: status %d (%s) nvme status %d (%s).\n", 1521 status, rej_msg, sts, nvme_rdma_cm_msg(sts)); 1522 } else { 1523 dev_err(queue->ctrl->ctrl.device, 1524 "Connect rejected: status %d (%s).\n", status, rej_msg); 1525 } 1526 1527 return -ECONNRESET; 1528 } 1529 1530 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue) 1531 { 1532 int ret; 1533 1534 ret = nvme_rdma_create_queue_ib(queue); 1535 if (ret) 1536 return ret; 1537 1538 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS); 1539 if (ret) { 1540 dev_err(queue->ctrl->ctrl.device, 1541 "rdma_resolve_route failed (%d).\n", 1542 queue->cm_error); 1543 goto out_destroy_queue; 1544 } 1545 1546 return 0; 1547 1548 out_destroy_queue: 1549 nvme_rdma_destroy_queue_ib(queue); 1550 return ret; 1551 } 1552 1553 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue) 1554 { 1555 struct nvme_rdma_ctrl *ctrl = queue->ctrl; 1556 struct rdma_conn_param param = { }; 1557 struct nvme_rdma_cm_req priv = { }; 1558 int ret; 1559 1560 param.qp_num = queue->qp->qp_num; 1561 param.flow_control = 1; 1562 1563 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom; 1564 /* maximum retry count */ 1565 param.retry_count = 7; 1566 param.rnr_retry_count = 7; 1567 param.private_data = &priv; 1568 param.private_data_len = sizeof(priv); 1569 1570 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); 1571 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue)); 1572 /* 1573 * set the admin queue depth to the minimum size 1574 * specified by the Fabrics standard. 1575 */ 1576 if (priv.qid == 0) { 1577 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH); 1578 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1); 1579 } else { 1580 /* 1581 * current interpretation of the fabrics spec 1582 * is at minimum you make hrqsize sqsize+1, or a 1583 * 1's based representation of sqsize. 1584 */ 1585 priv.hrqsize = cpu_to_le16(queue->queue_size); 1586 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize); 1587 } 1588 1589 ret = rdma_connect(queue->cm_id, ¶m); 1590 if (ret) { 1591 dev_err(ctrl->ctrl.device, 1592 "rdma_connect failed (%d).\n", ret); 1593 goto out_destroy_queue_ib; 1594 } 1595 1596 return 0; 1597 1598 out_destroy_queue_ib: 1599 nvme_rdma_destroy_queue_ib(queue); 1600 return ret; 1601 } 1602 1603 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, 1604 struct rdma_cm_event *ev) 1605 { 1606 struct nvme_rdma_queue *queue = cm_id->context; 1607 int cm_error = 0; 1608 1609 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n", 1610 rdma_event_msg(ev->event), ev->event, 1611 ev->status, cm_id); 1612 1613 switch (ev->event) { 1614 case RDMA_CM_EVENT_ADDR_RESOLVED: 1615 cm_error = nvme_rdma_addr_resolved(queue); 1616 break; 1617 case RDMA_CM_EVENT_ROUTE_RESOLVED: 1618 cm_error = nvme_rdma_route_resolved(queue); 1619 break; 1620 case RDMA_CM_EVENT_ESTABLISHED: 1621 queue->cm_error = nvme_rdma_conn_established(queue); 1622 /* complete cm_done regardless of success/failure */ 1623 complete(&queue->cm_done); 1624 return 0; 1625 case RDMA_CM_EVENT_REJECTED: 1626 nvme_rdma_destroy_queue_ib(queue); 1627 cm_error = nvme_rdma_conn_rejected(queue, ev); 1628 break; 1629 case RDMA_CM_EVENT_ROUTE_ERROR: 1630 case RDMA_CM_EVENT_CONNECT_ERROR: 1631 case RDMA_CM_EVENT_UNREACHABLE: 1632 nvme_rdma_destroy_queue_ib(queue); 1633 /* fall through */ 1634 case RDMA_CM_EVENT_ADDR_ERROR: 1635 dev_dbg(queue->ctrl->ctrl.device, 1636 "CM error event %d\n", ev->event); 1637 cm_error = -ECONNRESET; 1638 break; 1639 case RDMA_CM_EVENT_DISCONNECTED: 1640 case RDMA_CM_EVENT_ADDR_CHANGE: 1641 case RDMA_CM_EVENT_TIMEWAIT_EXIT: 1642 dev_dbg(queue->ctrl->ctrl.device, 1643 "disconnect received - connection closed\n"); 1644 nvme_rdma_error_recovery(queue->ctrl); 1645 break; 1646 case RDMA_CM_EVENT_DEVICE_REMOVAL: 1647 /* device removal is handled via the ib_client API */ 1648 break; 1649 default: 1650 dev_err(queue->ctrl->ctrl.device, 1651 "Unexpected RDMA CM event (%d)\n", ev->event); 1652 nvme_rdma_error_recovery(queue->ctrl); 1653 break; 1654 } 1655 1656 if (cm_error) { 1657 queue->cm_error = cm_error; 1658 complete(&queue->cm_done); 1659 } 1660 1661 return 0; 1662 } 1663 1664 static enum blk_eh_timer_return 1665 nvme_rdma_timeout(struct request *rq, bool reserved) 1666 { 1667 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1668 struct nvme_rdma_queue *queue = req->queue; 1669 struct nvme_rdma_ctrl *ctrl = queue->ctrl; 1670 1671 dev_warn(ctrl->ctrl.device, "I/O %d QID %d timeout\n", 1672 rq->tag, nvme_rdma_queue_idx(queue)); 1673 1674 if (ctrl->ctrl.state != NVME_CTRL_LIVE) { 1675 /* 1676 * Teardown immediately if controller times out while starting 1677 * or we are already started error recovery. all outstanding 1678 * requests are completed on shutdown, so we return BLK_EH_DONE. 1679 */ 1680 flush_work(&ctrl->err_work); 1681 nvme_rdma_teardown_io_queues(ctrl, false); 1682 nvme_rdma_teardown_admin_queue(ctrl, false); 1683 return BLK_EH_DONE; 1684 } 1685 1686 dev_warn(ctrl->ctrl.device, "starting error recovery\n"); 1687 nvme_rdma_error_recovery(ctrl); 1688 1689 return BLK_EH_RESET_TIMER; 1690 } 1691 1692 static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx, 1693 const struct blk_mq_queue_data *bd) 1694 { 1695 struct nvme_ns *ns = hctx->queue->queuedata; 1696 struct nvme_rdma_queue *queue = hctx->driver_data; 1697 struct request *rq = bd->rq; 1698 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1699 struct nvme_rdma_qe *sqe = &req->sqe; 1700 struct nvme_command *c = sqe->data; 1701 struct ib_device *dev; 1702 bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags); 1703 blk_status_t ret; 1704 int err; 1705 1706 WARN_ON_ONCE(rq->tag < 0); 1707 1708 if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready)) 1709 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq); 1710 1711 dev = queue->device->dev; 1712 ib_dma_sync_single_for_cpu(dev, sqe->dma, 1713 sizeof(struct nvme_command), DMA_TO_DEVICE); 1714 1715 ret = nvme_setup_cmd(ns, rq, c); 1716 if (ret) 1717 return ret; 1718 1719 blk_mq_start_request(rq); 1720 1721 err = nvme_rdma_map_data(queue, rq, c); 1722 if (unlikely(err < 0)) { 1723 dev_err(queue->ctrl->ctrl.device, 1724 "Failed to map data (%d)\n", err); 1725 nvme_cleanup_cmd(rq); 1726 goto err; 1727 } 1728 1729 sqe->cqe.done = nvme_rdma_send_done; 1730 1731 ib_dma_sync_single_for_device(dev, sqe->dma, 1732 sizeof(struct nvme_command), DMA_TO_DEVICE); 1733 1734 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge, 1735 req->mr ? &req->reg_wr.wr : NULL); 1736 if (unlikely(err)) { 1737 nvme_rdma_unmap_data(queue, rq); 1738 goto err; 1739 } 1740 1741 return BLK_STS_OK; 1742 err: 1743 if (err == -ENOMEM || err == -EAGAIN) 1744 return BLK_STS_RESOURCE; 1745 return BLK_STS_IOERR; 1746 } 1747 1748 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx) 1749 { 1750 struct nvme_rdma_queue *queue = hctx->driver_data; 1751 1752 return ib_process_cq_direct(queue->ib_cq, -1); 1753 } 1754 1755 static void nvme_rdma_complete_rq(struct request *rq) 1756 { 1757 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1758 1759 nvme_rdma_unmap_data(req->queue, rq); 1760 nvme_complete_rq(rq); 1761 } 1762 1763 static int nvme_rdma_map_queues(struct blk_mq_tag_set *set) 1764 { 1765 struct nvme_rdma_ctrl *ctrl = set->driver_data; 1766 1767 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0; 1768 set->map[HCTX_TYPE_DEFAULT].nr_queues = 1769 ctrl->io_queues[HCTX_TYPE_DEFAULT]; 1770 set->map[HCTX_TYPE_READ].nr_queues = ctrl->io_queues[HCTX_TYPE_READ]; 1771 if (ctrl->ctrl.opts->nr_write_queues) { 1772 /* separate read/write queues */ 1773 set->map[HCTX_TYPE_READ].queue_offset = 1774 ctrl->io_queues[HCTX_TYPE_DEFAULT]; 1775 } else { 1776 /* mixed read/write queues */ 1777 set->map[HCTX_TYPE_READ].queue_offset = 0; 1778 } 1779 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_DEFAULT], 1780 ctrl->device->dev, 0); 1781 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_READ], 1782 ctrl->device->dev, 0); 1783 1784 if (ctrl->ctrl.opts->nr_poll_queues) { 1785 set->map[HCTX_TYPE_POLL].nr_queues = 1786 ctrl->io_queues[HCTX_TYPE_POLL]; 1787 set->map[HCTX_TYPE_POLL].queue_offset = 1788 ctrl->io_queues[HCTX_TYPE_DEFAULT]; 1789 if (ctrl->ctrl.opts->nr_write_queues) 1790 set->map[HCTX_TYPE_POLL].queue_offset += 1791 ctrl->io_queues[HCTX_TYPE_READ]; 1792 blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]); 1793 } 1794 return 0; 1795 } 1796 1797 static const struct blk_mq_ops nvme_rdma_mq_ops = { 1798 .queue_rq = nvme_rdma_queue_rq, 1799 .complete = nvme_rdma_complete_rq, 1800 .init_request = nvme_rdma_init_request, 1801 .exit_request = nvme_rdma_exit_request, 1802 .init_hctx = nvme_rdma_init_hctx, 1803 .timeout = nvme_rdma_timeout, 1804 .map_queues = nvme_rdma_map_queues, 1805 .poll = nvme_rdma_poll, 1806 }; 1807 1808 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = { 1809 .queue_rq = nvme_rdma_queue_rq, 1810 .complete = nvme_rdma_complete_rq, 1811 .init_request = nvme_rdma_init_request, 1812 .exit_request = nvme_rdma_exit_request, 1813 .init_hctx = nvme_rdma_init_admin_hctx, 1814 .timeout = nvme_rdma_timeout, 1815 }; 1816 1817 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown) 1818 { 1819 cancel_work_sync(&ctrl->err_work); 1820 cancel_delayed_work_sync(&ctrl->reconnect_work); 1821 1822 nvme_rdma_teardown_io_queues(ctrl, shutdown); 1823 if (shutdown) 1824 nvme_shutdown_ctrl(&ctrl->ctrl); 1825 else 1826 nvme_disable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap); 1827 nvme_rdma_teardown_admin_queue(ctrl, shutdown); 1828 } 1829 1830 static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl) 1831 { 1832 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true); 1833 } 1834 1835 static void nvme_rdma_reset_ctrl_work(struct work_struct *work) 1836 { 1837 struct nvme_rdma_ctrl *ctrl = 1838 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work); 1839 1840 nvme_stop_ctrl(&ctrl->ctrl); 1841 nvme_rdma_shutdown_ctrl(ctrl, false); 1842 1843 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) { 1844 /* state change failure should never happen */ 1845 WARN_ON_ONCE(1); 1846 return; 1847 } 1848 1849 if (nvme_rdma_setup_ctrl(ctrl, false)) 1850 goto out_fail; 1851 1852 return; 1853 1854 out_fail: 1855 ++ctrl->ctrl.nr_reconnects; 1856 nvme_rdma_reconnect_or_remove(ctrl); 1857 } 1858 1859 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = { 1860 .name = "rdma", 1861 .module = THIS_MODULE, 1862 .flags = NVME_F_FABRICS, 1863 .reg_read32 = nvmf_reg_read32, 1864 .reg_read64 = nvmf_reg_read64, 1865 .reg_write32 = nvmf_reg_write32, 1866 .free_ctrl = nvme_rdma_free_ctrl, 1867 .submit_async_event = nvme_rdma_submit_async_event, 1868 .delete_ctrl = nvme_rdma_delete_ctrl, 1869 .get_address = nvmf_get_address, 1870 }; 1871 1872 /* 1873 * Fails a connection request if it matches an existing controller 1874 * (association) with the same tuple: 1875 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN> 1876 * 1877 * if local address is not specified in the request, it will match an 1878 * existing controller with all the other parameters the same and no 1879 * local port address specified as well. 1880 * 1881 * The ports don't need to be compared as they are intrinsically 1882 * already matched by the port pointers supplied. 1883 */ 1884 static bool 1885 nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts) 1886 { 1887 struct nvme_rdma_ctrl *ctrl; 1888 bool found = false; 1889 1890 mutex_lock(&nvme_rdma_ctrl_mutex); 1891 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) { 1892 found = nvmf_ip_options_match(&ctrl->ctrl, opts); 1893 if (found) 1894 break; 1895 } 1896 mutex_unlock(&nvme_rdma_ctrl_mutex); 1897 1898 return found; 1899 } 1900 1901 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev, 1902 struct nvmf_ctrl_options *opts) 1903 { 1904 struct nvme_rdma_ctrl *ctrl; 1905 int ret; 1906 bool changed; 1907 1908 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); 1909 if (!ctrl) 1910 return ERR_PTR(-ENOMEM); 1911 ctrl->ctrl.opts = opts; 1912 INIT_LIST_HEAD(&ctrl->list); 1913 1914 if (!(opts->mask & NVMF_OPT_TRSVCID)) { 1915 opts->trsvcid = 1916 kstrdup(__stringify(NVME_RDMA_IP_PORT), GFP_KERNEL); 1917 if (!opts->trsvcid) { 1918 ret = -ENOMEM; 1919 goto out_free_ctrl; 1920 } 1921 opts->mask |= NVMF_OPT_TRSVCID; 1922 } 1923 1924 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, 1925 opts->traddr, opts->trsvcid, &ctrl->addr); 1926 if (ret) { 1927 pr_err("malformed address passed: %s:%s\n", 1928 opts->traddr, opts->trsvcid); 1929 goto out_free_ctrl; 1930 } 1931 1932 if (opts->mask & NVMF_OPT_HOST_TRADDR) { 1933 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, 1934 opts->host_traddr, NULL, &ctrl->src_addr); 1935 if (ret) { 1936 pr_err("malformed src address passed: %s\n", 1937 opts->host_traddr); 1938 goto out_free_ctrl; 1939 } 1940 } 1941 1942 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) { 1943 ret = -EALREADY; 1944 goto out_free_ctrl; 1945 } 1946 1947 INIT_DELAYED_WORK(&ctrl->reconnect_work, 1948 nvme_rdma_reconnect_ctrl_work); 1949 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work); 1950 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work); 1951 1952 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues + 1953 opts->nr_poll_queues + 1; 1954 ctrl->ctrl.sqsize = opts->queue_size - 1; 1955 ctrl->ctrl.kato = opts->kato; 1956 1957 ret = -ENOMEM; 1958 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues), 1959 GFP_KERNEL); 1960 if (!ctrl->queues) 1961 goto out_free_ctrl; 1962 1963 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops, 1964 0 /* no quirks, we're perfect! */); 1965 if (ret) 1966 goto out_kfree_queues; 1967 1968 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING); 1969 WARN_ON_ONCE(!changed); 1970 1971 ret = nvme_rdma_setup_ctrl(ctrl, true); 1972 if (ret) 1973 goto out_uninit_ctrl; 1974 1975 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n", 1976 ctrl->ctrl.opts->subsysnqn, &ctrl->addr); 1977 1978 nvme_get_ctrl(&ctrl->ctrl); 1979 1980 mutex_lock(&nvme_rdma_ctrl_mutex); 1981 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list); 1982 mutex_unlock(&nvme_rdma_ctrl_mutex); 1983 1984 return &ctrl->ctrl; 1985 1986 out_uninit_ctrl: 1987 nvme_uninit_ctrl(&ctrl->ctrl); 1988 nvme_put_ctrl(&ctrl->ctrl); 1989 if (ret > 0) 1990 ret = -EIO; 1991 return ERR_PTR(ret); 1992 out_kfree_queues: 1993 kfree(ctrl->queues); 1994 out_free_ctrl: 1995 kfree(ctrl); 1996 return ERR_PTR(ret); 1997 } 1998 1999 static struct nvmf_transport_ops nvme_rdma_transport = { 2000 .name = "rdma", 2001 .module = THIS_MODULE, 2002 .required_opts = NVMF_OPT_TRADDR, 2003 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY | 2004 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO | 2005 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES, 2006 .create_ctrl = nvme_rdma_create_ctrl, 2007 }; 2008 2009 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data) 2010 { 2011 struct nvme_rdma_ctrl *ctrl; 2012 struct nvme_rdma_device *ndev; 2013 bool found = false; 2014 2015 mutex_lock(&device_list_mutex); 2016 list_for_each_entry(ndev, &device_list, entry) { 2017 if (ndev->dev == ib_device) { 2018 found = true; 2019 break; 2020 } 2021 } 2022 mutex_unlock(&device_list_mutex); 2023 2024 if (!found) 2025 return; 2026 2027 /* Delete all controllers using this device */ 2028 mutex_lock(&nvme_rdma_ctrl_mutex); 2029 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) { 2030 if (ctrl->device->dev != ib_device) 2031 continue; 2032 nvme_delete_ctrl(&ctrl->ctrl); 2033 } 2034 mutex_unlock(&nvme_rdma_ctrl_mutex); 2035 2036 flush_workqueue(nvme_delete_wq); 2037 } 2038 2039 static struct ib_client nvme_rdma_ib_client = { 2040 .name = "nvme_rdma", 2041 .remove = nvme_rdma_remove_one 2042 }; 2043 2044 static int __init nvme_rdma_init_module(void) 2045 { 2046 int ret; 2047 2048 ret = ib_register_client(&nvme_rdma_ib_client); 2049 if (ret) 2050 return ret; 2051 2052 ret = nvmf_register_transport(&nvme_rdma_transport); 2053 if (ret) 2054 goto err_unreg_client; 2055 2056 return 0; 2057 2058 err_unreg_client: 2059 ib_unregister_client(&nvme_rdma_ib_client); 2060 return ret; 2061 } 2062 2063 static void __exit nvme_rdma_cleanup_module(void) 2064 { 2065 nvmf_unregister_transport(&nvme_rdma_transport); 2066 ib_unregister_client(&nvme_rdma_ib_client); 2067 } 2068 2069 module_init(nvme_rdma_init_module); 2070 module_exit(nvme_rdma_cleanup_module); 2071 2072 MODULE_LICENSE("GPL v2"); 2073