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-integrity.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_CM_TIMEOUT_MS 3000 /* 3 second */ 32 33 #define NVME_RDMA_MAX_SEGMENTS 256 34 35 #define NVME_RDMA_MAX_INLINE_SEGMENTS 4 36 37 #define NVME_RDMA_DATA_SGL_SIZE \ 38 (sizeof(struct scatterlist) * NVME_INLINE_SG_CNT) 39 #define NVME_RDMA_METADATA_SGL_SIZE \ 40 (sizeof(struct scatterlist) * NVME_INLINE_METADATA_SG_CNT) 41 42 struct nvme_rdma_device { 43 struct ib_device *dev; 44 struct ib_pd *pd; 45 struct kref ref; 46 struct list_head entry; 47 unsigned int num_inline_segments; 48 }; 49 50 struct nvme_rdma_qe { 51 struct ib_cqe cqe; 52 void *data; 53 u64 dma; 54 }; 55 56 struct nvme_rdma_sgl { 57 int nents; 58 struct sg_table sg_table; 59 }; 60 61 struct nvme_rdma_queue; 62 struct nvme_rdma_request { 63 struct nvme_request req; 64 struct ib_mr *mr; 65 struct nvme_rdma_qe sqe; 66 union nvme_result result; 67 __le16 status; 68 refcount_t ref; 69 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS]; 70 u32 num_sge; 71 struct ib_reg_wr reg_wr; 72 struct ib_cqe reg_cqe; 73 struct nvme_rdma_queue *queue; 74 struct nvme_rdma_sgl data_sgl; 75 struct nvme_rdma_sgl *metadata_sgl; 76 bool use_sig_mr; 77 }; 78 79 enum nvme_rdma_queue_flags { 80 NVME_RDMA_Q_ALLOCATED = 0, 81 NVME_RDMA_Q_LIVE = 1, 82 NVME_RDMA_Q_TR_READY = 2, 83 }; 84 85 struct nvme_rdma_queue { 86 struct nvme_rdma_qe *rsp_ring; 87 int queue_size; 88 size_t cmnd_capsule_len; 89 struct nvme_rdma_ctrl *ctrl; 90 struct nvme_rdma_device *device; 91 struct ib_cq *ib_cq; 92 struct ib_qp *qp; 93 94 unsigned long flags; 95 struct rdma_cm_id *cm_id; 96 int cm_error; 97 struct completion cm_done; 98 bool pi_support; 99 int cq_size; 100 struct mutex queue_lock; 101 }; 102 103 struct nvme_rdma_ctrl { 104 /* read only in the hot path */ 105 struct nvme_rdma_queue *queues; 106 107 /* other member variables */ 108 struct blk_mq_tag_set tag_set; 109 struct work_struct err_work; 110 111 struct nvme_rdma_qe async_event_sqe; 112 113 struct delayed_work reconnect_work; 114 115 struct list_head list; 116 117 struct blk_mq_tag_set admin_tag_set; 118 struct nvme_rdma_device *device; 119 120 u32 max_fr_pages; 121 122 struct sockaddr_storage addr; 123 struct sockaddr_storage src_addr; 124 125 struct nvme_ctrl ctrl; 126 bool use_inline_data; 127 u32 io_queues[HCTX_MAX_TYPES]; 128 }; 129 130 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl) 131 { 132 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl); 133 } 134 135 static LIST_HEAD(device_list); 136 static DEFINE_MUTEX(device_list_mutex); 137 138 static LIST_HEAD(nvme_rdma_ctrl_list); 139 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex); 140 141 /* 142 * Disabling this option makes small I/O goes faster, but is fundamentally 143 * unsafe. With it turned off we will have to register a global rkey that 144 * allows read and write access to all physical memory. 145 */ 146 static bool register_always = true; 147 module_param(register_always, bool, 0444); 148 MODULE_PARM_DESC(register_always, 149 "Use memory registration even for contiguous memory regions"); 150 151 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, 152 struct rdma_cm_event *event); 153 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc); 154 static void nvme_rdma_complete_rq(struct request *rq); 155 156 static const struct blk_mq_ops nvme_rdma_mq_ops; 157 static const struct blk_mq_ops nvme_rdma_admin_mq_ops; 158 159 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue) 160 { 161 return queue - queue->ctrl->queues; 162 } 163 164 static bool nvme_rdma_poll_queue(struct nvme_rdma_queue *queue) 165 { 166 return nvme_rdma_queue_idx(queue) > 167 queue->ctrl->io_queues[HCTX_TYPE_DEFAULT] + 168 queue->ctrl->io_queues[HCTX_TYPE_READ]; 169 } 170 171 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue) 172 { 173 return queue->cmnd_capsule_len - sizeof(struct nvme_command); 174 } 175 176 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe, 177 size_t capsule_size, enum dma_data_direction dir) 178 { 179 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir); 180 kfree(qe->data); 181 } 182 183 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe, 184 size_t capsule_size, enum dma_data_direction dir) 185 { 186 qe->data = kzalloc(capsule_size, GFP_KERNEL); 187 if (!qe->data) 188 return -ENOMEM; 189 190 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir); 191 if (ib_dma_mapping_error(ibdev, qe->dma)) { 192 kfree(qe->data); 193 qe->data = NULL; 194 return -ENOMEM; 195 } 196 197 return 0; 198 } 199 200 static void nvme_rdma_free_ring(struct ib_device *ibdev, 201 struct nvme_rdma_qe *ring, size_t ib_queue_size, 202 size_t capsule_size, enum dma_data_direction dir) 203 { 204 int i; 205 206 for (i = 0; i < ib_queue_size; i++) 207 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir); 208 kfree(ring); 209 } 210 211 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev, 212 size_t ib_queue_size, size_t capsule_size, 213 enum dma_data_direction dir) 214 { 215 struct nvme_rdma_qe *ring; 216 int i; 217 218 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL); 219 if (!ring) 220 return NULL; 221 222 /* 223 * Bind the CQEs (post recv buffers) DMA mapping to the RDMA queue 224 * lifetime. It's safe, since any chage in the underlying RDMA device 225 * will issue error recovery and queue re-creation. 226 */ 227 for (i = 0; i < ib_queue_size; i++) { 228 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir)) 229 goto out_free_ring; 230 } 231 232 return ring; 233 234 out_free_ring: 235 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir); 236 return NULL; 237 } 238 239 static void nvme_rdma_qp_event(struct ib_event *event, void *context) 240 { 241 pr_debug("QP event %s (%d)\n", 242 ib_event_msg(event->event), event->event); 243 244 } 245 246 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue) 247 { 248 int ret; 249 250 ret = wait_for_completion_interruptible(&queue->cm_done); 251 if (ret) 252 return ret; 253 WARN_ON_ONCE(queue->cm_error > 0); 254 return queue->cm_error; 255 } 256 257 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor) 258 { 259 struct nvme_rdma_device *dev = queue->device; 260 struct ib_qp_init_attr init_attr; 261 int ret; 262 263 memset(&init_attr, 0, sizeof(init_attr)); 264 init_attr.event_handler = nvme_rdma_qp_event; 265 /* +1 for drain */ 266 init_attr.cap.max_send_wr = factor * queue->queue_size + 1; 267 /* +1 for drain */ 268 init_attr.cap.max_recv_wr = queue->queue_size + 1; 269 init_attr.cap.max_recv_sge = 1; 270 init_attr.cap.max_send_sge = 1 + dev->num_inline_segments; 271 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR; 272 init_attr.qp_type = IB_QPT_RC; 273 init_attr.send_cq = queue->ib_cq; 274 init_attr.recv_cq = queue->ib_cq; 275 if (queue->pi_support) 276 init_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN; 277 init_attr.qp_context = queue; 278 279 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr); 280 281 queue->qp = queue->cm_id->qp; 282 return ret; 283 } 284 285 static void nvme_rdma_exit_request(struct blk_mq_tag_set *set, 286 struct request *rq, unsigned int hctx_idx) 287 { 288 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 289 290 kfree(req->sqe.data); 291 } 292 293 static int nvme_rdma_init_request(struct blk_mq_tag_set *set, 294 struct request *rq, unsigned int hctx_idx, 295 unsigned int numa_node) 296 { 297 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(set->driver_data); 298 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 299 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0; 300 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx]; 301 302 nvme_req(rq)->ctrl = &ctrl->ctrl; 303 req->sqe.data = kzalloc(sizeof(struct nvme_command), GFP_KERNEL); 304 if (!req->sqe.data) 305 return -ENOMEM; 306 307 /* metadata nvme_rdma_sgl struct is located after command's data SGL */ 308 if (queue->pi_support) 309 req->metadata_sgl = (void *)nvme_req(rq) + 310 sizeof(struct nvme_rdma_request) + 311 NVME_RDMA_DATA_SGL_SIZE; 312 313 req->queue = queue; 314 nvme_req(rq)->cmd = req->sqe.data; 315 316 return 0; 317 } 318 319 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, 320 unsigned int hctx_idx) 321 { 322 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(data); 323 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1]; 324 325 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count); 326 327 hctx->driver_data = queue; 328 return 0; 329 } 330 331 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, 332 unsigned int hctx_idx) 333 { 334 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(data); 335 struct nvme_rdma_queue *queue = &ctrl->queues[0]; 336 337 BUG_ON(hctx_idx != 0); 338 339 hctx->driver_data = queue; 340 return 0; 341 } 342 343 static void nvme_rdma_free_dev(struct kref *ref) 344 { 345 struct nvme_rdma_device *ndev = 346 container_of(ref, struct nvme_rdma_device, ref); 347 348 mutex_lock(&device_list_mutex); 349 list_del(&ndev->entry); 350 mutex_unlock(&device_list_mutex); 351 352 ib_dealloc_pd(ndev->pd); 353 kfree(ndev); 354 } 355 356 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev) 357 { 358 kref_put(&dev->ref, nvme_rdma_free_dev); 359 } 360 361 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev) 362 { 363 return kref_get_unless_zero(&dev->ref); 364 } 365 366 static struct nvme_rdma_device * 367 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id) 368 { 369 struct nvme_rdma_device *ndev; 370 371 mutex_lock(&device_list_mutex); 372 list_for_each_entry(ndev, &device_list, entry) { 373 if (ndev->dev->node_guid == cm_id->device->node_guid && 374 nvme_rdma_dev_get(ndev)) 375 goto out_unlock; 376 } 377 378 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL); 379 if (!ndev) 380 goto out_err; 381 382 ndev->dev = cm_id->device; 383 kref_init(&ndev->ref); 384 385 ndev->pd = ib_alloc_pd(ndev->dev, 386 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY); 387 if (IS_ERR(ndev->pd)) 388 goto out_free_dev; 389 390 if (!(ndev->dev->attrs.device_cap_flags & 391 IB_DEVICE_MEM_MGT_EXTENSIONS)) { 392 dev_err(&ndev->dev->dev, 393 "Memory registrations not supported.\n"); 394 goto out_free_pd; 395 } 396 397 ndev->num_inline_segments = min(NVME_RDMA_MAX_INLINE_SEGMENTS, 398 ndev->dev->attrs.max_send_sge - 1); 399 list_add(&ndev->entry, &device_list); 400 out_unlock: 401 mutex_unlock(&device_list_mutex); 402 return ndev; 403 404 out_free_pd: 405 ib_dealloc_pd(ndev->pd); 406 out_free_dev: 407 kfree(ndev); 408 out_err: 409 mutex_unlock(&device_list_mutex); 410 return NULL; 411 } 412 413 static void nvme_rdma_free_cq(struct nvme_rdma_queue *queue) 414 { 415 if (nvme_rdma_poll_queue(queue)) 416 ib_free_cq(queue->ib_cq); 417 else 418 ib_cq_pool_put(queue->ib_cq, queue->cq_size); 419 } 420 421 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue) 422 { 423 struct nvme_rdma_device *dev; 424 struct ib_device *ibdev; 425 426 if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags)) 427 return; 428 429 dev = queue->device; 430 ibdev = dev->dev; 431 432 if (queue->pi_support) 433 ib_mr_pool_destroy(queue->qp, &queue->qp->sig_mrs); 434 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs); 435 436 /* 437 * The cm_id object might have been destroyed during RDMA connection 438 * establishment error flow to avoid getting other cma events, thus 439 * the destruction of the QP shouldn't use rdma_cm API. 440 */ 441 ib_destroy_qp(queue->qp); 442 nvme_rdma_free_cq(queue); 443 444 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size, 445 sizeof(struct nvme_completion), DMA_FROM_DEVICE); 446 447 nvme_rdma_dev_put(dev); 448 } 449 450 static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev, bool pi_support) 451 { 452 u32 max_page_list_len; 453 454 if (pi_support) 455 max_page_list_len = ibdev->attrs.max_pi_fast_reg_page_list_len; 456 else 457 max_page_list_len = ibdev->attrs.max_fast_reg_page_list_len; 458 459 return min_t(u32, NVME_RDMA_MAX_SEGMENTS, max_page_list_len - 1); 460 } 461 462 static int nvme_rdma_create_cq(struct ib_device *ibdev, 463 struct nvme_rdma_queue *queue) 464 { 465 int ret, comp_vector, idx = nvme_rdma_queue_idx(queue); 466 467 /* 468 * Spread I/O queues completion vectors according their queue index. 469 * Admin queues can always go on completion vector 0. 470 */ 471 comp_vector = (idx == 0 ? idx : idx - 1) % ibdev->num_comp_vectors; 472 473 /* Polling queues need direct cq polling context */ 474 if (nvme_rdma_poll_queue(queue)) 475 queue->ib_cq = ib_alloc_cq(ibdev, queue, queue->cq_size, 476 comp_vector, IB_POLL_DIRECT); 477 else 478 queue->ib_cq = ib_cq_pool_get(ibdev, queue->cq_size, 479 comp_vector, IB_POLL_SOFTIRQ); 480 481 if (IS_ERR(queue->ib_cq)) { 482 ret = PTR_ERR(queue->ib_cq); 483 return ret; 484 } 485 486 return 0; 487 } 488 489 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue) 490 { 491 struct ib_device *ibdev; 492 const int send_wr_factor = 3; /* MR, SEND, INV */ 493 const int cq_factor = send_wr_factor + 1; /* + RECV */ 494 int ret, pages_per_mr; 495 496 queue->device = nvme_rdma_find_get_device(queue->cm_id); 497 if (!queue->device) { 498 dev_err(queue->cm_id->device->dev.parent, 499 "no client data found!\n"); 500 return -ECONNREFUSED; 501 } 502 ibdev = queue->device->dev; 503 504 /* +1 for ib_drain_qp */ 505 queue->cq_size = cq_factor * queue->queue_size + 1; 506 507 ret = nvme_rdma_create_cq(ibdev, queue); 508 if (ret) 509 goto out_put_dev; 510 511 ret = nvme_rdma_create_qp(queue, send_wr_factor); 512 if (ret) 513 goto out_destroy_ib_cq; 514 515 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size, 516 sizeof(struct nvme_completion), DMA_FROM_DEVICE); 517 if (!queue->rsp_ring) { 518 ret = -ENOMEM; 519 goto out_destroy_qp; 520 } 521 522 /* 523 * Currently we don't use SG_GAPS MR's so if the first entry is 524 * misaligned we'll end up using two entries for a single data page, 525 * so one additional entry is required. 526 */ 527 pages_per_mr = nvme_rdma_get_max_fr_pages(ibdev, queue->pi_support) + 1; 528 ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs, 529 queue->queue_size, 530 IB_MR_TYPE_MEM_REG, 531 pages_per_mr, 0); 532 if (ret) { 533 dev_err(queue->ctrl->ctrl.device, 534 "failed to initialize MR pool sized %d for QID %d\n", 535 queue->queue_size, nvme_rdma_queue_idx(queue)); 536 goto out_destroy_ring; 537 } 538 539 if (queue->pi_support) { 540 ret = ib_mr_pool_init(queue->qp, &queue->qp->sig_mrs, 541 queue->queue_size, IB_MR_TYPE_INTEGRITY, 542 pages_per_mr, pages_per_mr); 543 if (ret) { 544 dev_err(queue->ctrl->ctrl.device, 545 "failed to initialize PI MR pool sized %d for QID %d\n", 546 queue->queue_size, nvme_rdma_queue_idx(queue)); 547 goto out_destroy_mr_pool; 548 } 549 } 550 551 set_bit(NVME_RDMA_Q_TR_READY, &queue->flags); 552 553 return 0; 554 555 out_destroy_mr_pool: 556 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs); 557 out_destroy_ring: 558 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size, 559 sizeof(struct nvme_completion), DMA_FROM_DEVICE); 560 out_destroy_qp: 561 rdma_destroy_qp(queue->cm_id); 562 out_destroy_ib_cq: 563 nvme_rdma_free_cq(queue); 564 out_put_dev: 565 nvme_rdma_dev_put(queue->device); 566 return ret; 567 } 568 569 static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl, 570 int idx, size_t queue_size) 571 { 572 struct nvme_rdma_queue *queue; 573 struct sockaddr *src_addr = NULL; 574 int ret; 575 576 queue = &ctrl->queues[idx]; 577 mutex_init(&queue->queue_lock); 578 queue->ctrl = ctrl; 579 if (idx && ctrl->ctrl.max_integrity_segments) 580 queue->pi_support = true; 581 else 582 queue->pi_support = false; 583 init_completion(&queue->cm_done); 584 585 if (idx > 0) 586 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16; 587 else 588 queue->cmnd_capsule_len = sizeof(struct nvme_command); 589 590 queue->queue_size = queue_size; 591 592 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue, 593 RDMA_PS_TCP, IB_QPT_RC); 594 if (IS_ERR(queue->cm_id)) { 595 dev_info(ctrl->ctrl.device, 596 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id)); 597 ret = PTR_ERR(queue->cm_id); 598 goto out_destroy_mutex; 599 } 600 601 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR) 602 src_addr = (struct sockaddr *)&ctrl->src_addr; 603 604 queue->cm_error = -ETIMEDOUT; 605 ret = rdma_resolve_addr(queue->cm_id, src_addr, 606 (struct sockaddr *)&ctrl->addr, 607 NVME_RDMA_CM_TIMEOUT_MS); 608 if (ret) { 609 dev_info(ctrl->ctrl.device, 610 "rdma_resolve_addr failed (%d).\n", ret); 611 goto out_destroy_cm_id; 612 } 613 614 ret = nvme_rdma_wait_for_cm(queue); 615 if (ret) { 616 dev_info(ctrl->ctrl.device, 617 "rdma connection establishment failed (%d)\n", ret); 618 goto out_destroy_cm_id; 619 } 620 621 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags); 622 623 return 0; 624 625 out_destroy_cm_id: 626 rdma_destroy_id(queue->cm_id); 627 nvme_rdma_destroy_queue_ib(queue); 628 out_destroy_mutex: 629 mutex_destroy(&queue->queue_lock); 630 return ret; 631 } 632 633 static void __nvme_rdma_stop_queue(struct nvme_rdma_queue *queue) 634 { 635 rdma_disconnect(queue->cm_id); 636 ib_drain_qp(queue->qp); 637 } 638 639 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue) 640 { 641 if (!test_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags)) 642 return; 643 644 mutex_lock(&queue->queue_lock); 645 if (test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags)) 646 __nvme_rdma_stop_queue(queue); 647 mutex_unlock(&queue->queue_lock); 648 } 649 650 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue) 651 { 652 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags)) 653 return; 654 655 rdma_destroy_id(queue->cm_id); 656 nvme_rdma_destroy_queue_ib(queue); 657 mutex_destroy(&queue->queue_lock); 658 } 659 660 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl) 661 { 662 int i; 663 664 for (i = 1; i < ctrl->ctrl.queue_count; i++) 665 nvme_rdma_free_queue(&ctrl->queues[i]); 666 } 667 668 static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl) 669 { 670 int i; 671 672 for (i = 1; i < ctrl->ctrl.queue_count; i++) 673 nvme_rdma_stop_queue(&ctrl->queues[i]); 674 } 675 676 static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx) 677 { 678 struct nvme_rdma_queue *queue = &ctrl->queues[idx]; 679 int ret; 680 681 if (idx) 682 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx); 683 else 684 ret = nvmf_connect_admin_queue(&ctrl->ctrl); 685 686 if (!ret) { 687 set_bit(NVME_RDMA_Q_LIVE, &queue->flags); 688 } else { 689 if (test_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags)) 690 __nvme_rdma_stop_queue(queue); 691 dev_info(ctrl->ctrl.device, 692 "failed to connect queue: %d ret=%d\n", idx, ret); 693 } 694 return ret; 695 } 696 697 static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl, 698 int first, int last) 699 { 700 int i, ret = 0; 701 702 for (i = first; i < last; i++) { 703 ret = nvme_rdma_start_queue(ctrl, i); 704 if (ret) 705 goto out_stop_queues; 706 } 707 708 return 0; 709 710 out_stop_queues: 711 for (i--; i >= first; i--) 712 nvme_rdma_stop_queue(&ctrl->queues[i]); 713 return ret; 714 } 715 716 static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl) 717 { 718 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; 719 unsigned int nr_io_queues; 720 int i, ret; 721 722 nr_io_queues = nvmf_nr_io_queues(opts); 723 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues); 724 if (ret) 725 return ret; 726 727 if (nr_io_queues == 0) { 728 dev_err(ctrl->ctrl.device, 729 "unable to set any I/O queues\n"); 730 return -ENOMEM; 731 } 732 733 ctrl->ctrl.queue_count = nr_io_queues + 1; 734 dev_info(ctrl->ctrl.device, 735 "creating %d I/O queues.\n", nr_io_queues); 736 737 nvmf_set_io_queues(opts, nr_io_queues, ctrl->io_queues); 738 for (i = 1; i < ctrl->ctrl.queue_count; i++) { 739 ret = nvme_rdma_alloc_queue(ctrl, i, 740 ctrl->ctrl.sqsize + 1); 741 if (ret) 742 goto out_free_queues; 743 } 744 745 return 0; 746 747 out_free_queues: 748 for (i--; i >= 1; i--) 749 nvme_rdma_free_queue(&ctrl->queues[i]); 750 751 return ret; 752 } 753 754 static int nvme_rdma_alloc_tag_set(struct nvme_ctrl *ctrl) 755 { 756 unsigned int cmd_size = sizeof(struct nvme_rdma_request) + 757 NVME_RDMA_DATA_SGL_SIZE; 758 759 if (ctrl->max_integrity_segments) 760 cmd_size += sizeof(struct nvme_rdma_sgl) + 761 NVME_RDMA_METADATA_SGL_SIZE; 762 763 return nvme_alloc_io_tag_set(ctrl, &to_rdma_ctrl(ctrl)->tag_set, 764 &nvme_rdma_mq_ops, 765 ctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2, 766 cmd_size); 767 } 768 769 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl) 770 { 771 if (ctrl->async_event_sqe.data) { 772 cancel_work_sync(&ctrl->ctrl.async_event_work); 773 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe, 774 sizeof(struct nvme_command), DMA_TO_DEVICE); 775 ctrl->async_event_sqe.data = NULL; 776 } 777 nvme_rdma_free_queue(&ctrl->queues[0]); 778 } 779 780 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl, 781 bool new) 782 { 783 bool pi_capable = false; 784 int error; 785 786 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH); 787 if (error) 788 return error; 789 790 ctrl->device = ctrl->queues[0].device; 791 ctrl->ctrl.numa_node = ibdev_to_node(ctrl->device->dev); 792 793 /* T10-PI support */ 794 if (ctrl->device->dev->attrs.kernel_cap_flags & 795 IBK_INTEGRITY_HANDOVER) 796 pi_capable = true; 797 798 ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev, 799 pi_capable); 800 801 /* 802 * Bind the async event SQE DMA mapping to the admin queue lifetime. 803 * It's safe, since any chage in the underlying RDMA device will issue 804 * error recovery and queue re-creation. 805 */ 806 error = nvme_rdma_alloc_qe(ctrl->device->dev, &ctrl->async_event_sqe, 807 sizeof(struct nvme_command), DMA_TO_DEVICE); 808 if (error) 809 goto out_free_queue; 810 811 if (new) { 812 error = nvme_alloc_admin_tag_set(&ctrl->ctrl, 813 &ctrl->admin_tag_set, &nvme_rdma_admin_mq_ops, 814 sizeof(struct nvme_rdma_request) + 815 NVME_RDMA_DATA_SGL_SIZE); 816 if (error) 817 goto out_free_async_qe; 818 819 } 820 821 error = nvme_rdma_start_queue(ctrl, 0); 822 if (error) 823 goto out_remove_admin_tag_set; 824 825 error = nvme_enable_ctrl(&ctrl->ctrl); 826 if (error) 827 goto out_stop_queue; 828 829 ctrl->ctrl.max_segments = ctrl->max_fr_pages; 830 ctrl->ctrl.max_hw_sectors = ctrl->max_fr_pages << (ilog2(SZ_4K) - 9); 831 if (pi_capable) 832 ctrl->ctrl.max_integrity_segments = ctrl->max_fr_pages; 833 else 834 ctrl->ctrl.max_integrity_segments = 0; 835 836 nvme_unquiesce_admin_queue(&ctrl->ctrl); 837 838 error = nvme_init_ctrl_finish(&ctrl->ctrl, false); 839 if (error) 840 goto out_quiesce_queue; 841 842 return 0; 843 844 out_quiesce_queue: 845 nvme_quiesce_admin_queue(&ctrl->ctrl); 846 blk_sync_queue(ctrl->ctrl.admin_q); 847 out_stop_queue: 848 nvme_rdma_stop_queue(&ctrl->queues[0]); 849 nvme_cancel_admin_tagset(&ctrl->ctrl); 850 out_remove_admin_tag_set: 851 if (new) 852 nvme_remove_admin_tag_set(&ctrl->ctrl); 853 out_free_async_qe: 854 if (ctrl->async_event_sqe.data) { 855 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe, 856 sizeof(struct nvme_command), DMA_TO_DEVICE); 857 ctrl->async_event_sqe.data = NULL; 858 } 859 out_free_queue: 860 nvme_rdma_free_queue(&ctrl->queues[0]); 861 return error; 862 } 863 864 static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new) 865 { 866 int ret, nr_queues; 867 868 ret = nvme_rdma_alloc_io_queues(ctrl); 869 if (ret) 870 return ret; 871 872 if (new) { 873 ret = nvme_rdma_alloc_tag_set(&ctrl->ctrl); 874 if (ret) 875 goto out_free_io_queues; 876 } 877 878 /* 879 * Only start IO queues for which we have allocated the tagset 880 * and limitted it to the available queues. On reconnects, the 881 * queue number might have changed. 882 */ 883 nr_queues = min(ctrl->tag_set.nr_hw_queues + 1, ctrl->ctrl.queue_count); 884 ret = nvme_rdma_start_io_queues(ctrl, 1, nr_queues); 885 if (ret) 886 goto out_cleanup_tagset; 887 888 if (!new) { 889 nvme_start_freeze(&ctrl->ctrl); 890 nvme_unquiesce_io_queues(&ctrl->ctrl); 891 if (!nvme_wait_freeze_timeout(&ctrl->ctrl, NVME_IO_TIMEOUT)) { 892 /* 893 * If we timed out waiting for freeze we are likely to 894 * be stuck. Fail the controller initialization just 895 * to be safe. 896 */ 897 ret = -ENODEV; 898 nvme_unfreeze(&ctrl->ctrl); 899 goto out_wait_freeze_timed_out; 900 } 901 blk_mq_update_nr_hw_queues(ctrl->ctrl.tagset, 902 ctrl->ctrl.queue_count - 1); 903 nvme_unfreeze(&ctrl->ctrl); 904 } 905 906 /* 907 * If the number of queues has increased (reconnect case) 908 * start all new queues now. 909 */ 910 ret = nvme_rdma_start_io_queues(ctrl, nr_queues, 911 ctrl->tag_set.nr_hw_queues + 1); 912 if (ret) 913 goto out_wait_freeze_timed_out; 914 915 return 0; 916 917 out_wait_freeze_timed_out: 918 nvme_quiesce_io_queues(&ctrl->ctrl); 919 nvme_sync_io_queues(&ctrl->ctrl); 920 nvme_rdma_stop_io_queues(ctrl); 921 out_cleanup_tagset: 922 nvme_cancel_tagset(&ctrl->ctrl); 923 if (new) 924 nvme_remove_io_tag_set(&ctrl->ctrl); 925 out_free_io_queues: 926 nvme_rdma_free_io_queues(ctrl); 927 return ret; 928 } 929 930 static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl *ctrl, 931 bool remove) 932 { 933 nvme_quiesce_admin_queue(&ctrl->ctrl); 934 blk_sync_queue(ctrl->ctrl.admin_q); 935 nvme_rdma_stop_queue(&ctrl->queues[0]); 936 nvme_cancel_admin_tagset(&ctrl->ctrl); 937 if (remove) { 938 nvme_unquiesce_admin_queue(&ctrl->ctrl); 939 nvme_remove_admin_tag_set(&ctrl->ctrl); 940 } 941 nvme_rdma_destroy_admin_queue(ctrl); 942 } 943 944 static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl *ctrl, 945 bool remove) 946 { 947 if (ctrl->ctrl.queue_count > 1) { 948 nvme_quiesce_io_queues(&ctrl->ctrl); 949 nvme_sync_io_queues(&ctrl->ctrl); 950 nvme_rdma_stop_io_queues(ctrl); 951 nvme_cancel_tagset(&ctrl->ctrl); 952 if (remove) { 953 nvme_unquiesce_io_queues(&ctrl->ctrl); 954 nvme_remove_io_tag_set(&ctrl->ctrl); 955 } 956 nvme_rdma_free_io_queues(ctrl); 957 } 958 } 959 960 static void nvme_rdma_stop_ctrl(struct nvme_ctrl *nctrl) 961 { 962 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); 963 964 flush_work(&ctrl->err_work); 965 cancel_delayed_work_sync(&ctrl->reconnect_work); 966 } 967 968 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl) 969 { 970 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); 971 972 if (list_empty(&ctrl->list)) 973 goto free_ctrl; 974 975 mutex_lock(&nvme_rdma_ctrl_mutex); 976 list_del(&ctrl->list); 977 mutex_unlock(&nvme_rdma_ctrl_mutex); 978 979 nvmf_free_options(nctrl->opts); 980 free_ctrl: 981 kfree(ctrl->queues); 982 kfree(ctrl); 983 } 984 985 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl) 986 { 987 enum nvme_ctrl_state state = nvme_ctrl_state(&ctrl->ctrl); 988 989 /* If we are resetting/deleting then do nothing */ 990 if (state != NVME_CTRL_CONNECTING) { 991 WARN_ON_ONCE(state == NVME_CTRL_NEW || state == NVME_CTRL_LIVE); 992 return; 993 } 994 995 if (nvmf_should_reconnect(&ctrl->ctrl)) { 996 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n", 997 ctrl->ctrl.opts->reconnect_delay); 998 queue_delayed_work(nvme_wq, &ctrl->reconnect_work, 999 ctrl->ctrl.opts->reconnect_delay * HZ); 1000 } else { 1001 nvme_delete_ctrl(&ctrl->ctrl); 1002 } 1003 } 1004 1005 static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new) 1006 { 1007 int ret; 1008 bool changed; 1009 u16 max_queue_size; 1010 1011 ret = nvme_rdma_configure_admin_queue(ctrl, new); 1012 if (ret) 1013 return ret; 1014 1015 if (ctrl->ctrl.icdoff) { 1016 ret = -EOPNOTSUPP; 1017 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n"); 1018 goto destroy_admin; 1019 } 1020 1021 if (!(ctrl->ctrl.sgls & (1 << 2))) { 1022 ret = -EOPNOTSUPP; 1023 dev_err(ctrl->ctrl.device, 1024 "Mandatory keyed sgls are not supported!\n"); 1025 goto destroy_admin; 1026 } 1027 1028 if (ctrl->ctrl.opts->queue_size > ctrl->ctrl.sqsize + 1) { 1029 dev_warn(ctrl->ctrl.device, 1030 "queue_size %zu > ctrl sqsize %u, clamping down\n", 1031 ctrl->ctrl.opts->queue_size, ctrl->ctrl.sqsize + 1); 1032 } 1033 1034 if (ctrl->ctrl.max_integrity_segments) 1035 max_queue_size = NVME_RDMA_MAX_METADATA_QUEUE_SIZE; 1036 else 1037 max_queue_size = NVME_RDMA_MAX_QUEUE_SIZE; 1038 1039 if (ctrl->ctrl.sqsize + 1 > max_queue_size) { 1040 dev_warn(ctrl->ctrl.device, 1041 "ctrl sqsize %u > max queue size %u, clamping down\n", 1042 ctrl->ctrl.sqsize + 1, max_queue_size); 1043 ctrl->ctrl.sqsize = max_queue_size - 1; 1044 } 1045 1046 if (ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) { 1047 dev_warn(ctrl->ctrl.device, 1048 "sqsize %u > ctrl maxcmd %u, clamping down\n", 1049 ctrl->ctrl.sqsize + 1, ctrl->ctrl.maxcmd); 1050 ctrl->ctrl.sqsize = ctrl->ctrl.maxcmd - 1; 1051 } 1052 1053 if (ctrl->ctrl.sgls & (1 << 20)) 1054 ctrl->use_inline_data = true; 1055 1056 if (ctrl->ctrl.queue_count > 1) { 1057 ret = nvme_rdma_configure_io_queues(ctrl, new); 1058 if (ret) 1059 goto destroy_admin; 1060 } 1061 1062 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); 1063 if (!changed) { 1064 /* 1065 * state change failure is ok if we started ctrl delete, 1066 * unless we're during creation of a new controller to 1067 * avoid races with teardown flow. 1068 */ 1069 enum nvme_ctrl_state state = nvme_ctrl_state(&ctrl->ctrl); 1070 1071 WARN_ON_ONCE(state != NVME_CTRL_DELETING && 1072 state != NVME_CTRL_DELETING_NOIO); 1073 WARN_ON_ONCE(new); 1074 ret = -EINVAL; 1075 goto destroy_io; 1076 } 1077 1078 nvme_start_ctrl(&ctrl->ctrl); 1079 return 0; 1080 1081 destroy_io: 1082 if (ctrl->ctrl.queue_count > 1) { 1083 nvme_quiesce_io_queues(&ctrl->ctrl); 1084 nvme_sync_io_queues(&ctrl->ctrl); 1085 nvme_rdma_stop_io_queues(ctrl); 1086 nvme_cancel_tagset(&ctrl->ctrl); 1087 if (new) 1088 nvme_remove_io_tag_set(&ctrl->ctrl); 1089 nvme_rdma_free_io_queues(ctrl); 1090 } 1091 destroy_admin: 1092 nvme_stop_keep_alive(&ctrl->ctrl); 1093 nvme_quiesce_admin_queue(&ctrl->ctrl); 1094 blk_sync_queue(ctrl->ctrl.admin_q); 1095 nvme_rdma_stop_queue(&ctrl->queues[0]); 1096 nvme_cancel_admin_tagset(&ctrl->ctrl); 1097 if (new) 1098 nvme_remove_admin_tag_set(&ctrl->ctrl); 1099 nvme_rdma_destroy_admin_queue(ctrl); 1100 return ret; 1101 } 1102 1103 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work) 1104 { 1105 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work), 1106 struct nvme_rdma_ctrl, reconnect_work); 1107 1108 ++ctrl->ctrl.nr_reconnects; 1109 1110 if (nvme_rdma_setup_ctrl(ctrl, false)) 1111 goto requeue; 1112 1113 dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n", 1114 ctrl->ctrl.nr_reconnects); 1115 1116 ctrl->ctrl.nr_reconnects = 0; 1117 1118 return; 1119 1120 requeue: 1121 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n", 1122 ctrl->ctrl.nr_reconnects); 1123 nvme_rdma_reconnect_or_remove(ctrl); 1124 } 1125 1126 static void nvme_rdma_error_recovery_work(struct work_struct *work) 1127 { 1128 struct nvme_rdma_ctrl *ctrl = container_of(work, 1129 struct nvme_rdma_ctrl, err_work); 1130 1131 nvme_stop_keep_alive(&ctrl->ctrl); 1132 flush_work(&ctrl->ctrl.async_event_work); 1133 nvme_rdma_teardown_io_queues(ctrl, false); 1134 nvme_unquiesce_io_queues(&ctrl->ctrl); 1135 nvme_rdma_teardown_admin_queue(ctrl, false); 1136 nvme_unquiesce_admin_queue(&ctrl->ctrl); 1137 nvme_auth_stop(&ctrl->ctrl); 1138 1139 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) { 1140 /* state change failure is ok if we started ctrl delete */ 1141 enum nvme_ctrl_state state = nvme_ctrl_state(&ctrl->ctrl); 1142 1143 WARN_ON_ONCE(state != NVME_CTRL_DELETING && 1144 state != NVME_CTRL_DELETING_NOIO); 1145 return; 1146 } 1147 1148 nvme_rdma_reconnect_or_remove(ctrl); 1149 } 1150 1151 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl) 1152 { 1153 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING)) 1154 return; 1155 1156 dev_warn(ctrl->ctrl.device, "starting error recovery\n"); 1157 queue_work(nvme_reset_wq, &ctrl->err_work); 1158 } 1159 1160 static void nvme_rdma_end_request(struct nvme_rdma_request *req) 1161 { 1162 struct request *rq = blk_mq_rq_from_pdu(req); 1163 1164 if (!refcount_dec_and_test(&req->ref)) 1165 return; 1166 if (!nvme_try_complete_req(rq, req->status, req->result)) 1167 nvme_rdma_complete_rq(rq); 1168 } 1169 1170 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc, 1171 const char *op) 1172 { 1173 struct nvme_rdma_queue *queue = wc->qp->qp_context; 1174 struct nvme_rdma_ctrl *ctrl = queue->ctrl; 1175 1176 if (nvme_ctrl_state(&ctrl->ctrl) == NVME_CTRL_LIVE) 1177 dev_info(ctrl->ctrl.device, 1178 "%s for CQE 0x%p failed with status %s (%d)\n", 1179 op, wc->wr_cqe, 1180 ib_wc_status_msg(wc->status), wc->status); 1181 nvme_rdma_error_recovery(ctrl); 1182 } 1183 1184 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc) 1185 { 1186 if (unlikely(wc->status != IB_WC_SUCCESS)) 1187 nvme_rdma_wr_error(cq, wc, "MEMREG"); 1188 } 1189 1190 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc) 1191 { 1192 struct nvme_rdma_request *req = 1193 container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe); 1194 1195 if (unlikely(wc->status != IB_WC_SUCCESS)) 1196 nvme_rdma_wr_error(cq, wc, "LOCAL_INV"); 1197 else 1198 nvme_rdma_end_request(req); 1199 } 1200 1201 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue, 1202 struct nvme_rdma_request *req) 1203 { 1204 struct ib_send_wr wr = { 1205 .opcode = IB_WR_LOCAL_INV, 1206 .next = NULL, 1207 .num_sge = 0, 1208 .send_flags = IB_SEND_SIGNALED, 1209 .ex.invalidate_rkey = req->mr->rkey, 1210 }; 1211 1212 req->reg_cqe.done = nvme_rdma_inv_rkey_done; 1213 wr.wr_cqe = &req->reg_cqe; 1214 1215 return ib_post_send(queue->qp, &wr, NULL); 1216 } 1217 1218 static void nvme_rdma_dma_unmap_req(struct ib_device *ibdev, struct request *rq) 1219 { 1220 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1221 1222 if (blk_integrity_rq(rq)) { 1223 ib_dma_unmap_sg(ibdev, req->metadata_sgl->sg_table.sgl, 1224 req->metadata_sgl->nents, rq_dma_dir(rq)); 1225 sg_free_table_chained(&req->metadata_sgl->sg_table, 1226 NVME_INLINE_METADATA_SG_CNT); 1227 } 1228 1229 ib_dma_unmap_sg(ibdev, req->data_sgl.sg_table.sgl, req->data_sgl.nents, 1230 rq_dma_dir(rq)); 1231 sg_free_table_chained(&req->data_sgl.sg_table, NVME_INLINE_SG_CNT); 1232 } 1233 1234 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue, 1235 struct request *rq) 1236 { 1237 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1238 struct nvme_rdma_device *dev = queue->device; 1239 struct ib_device *ibdev = dev->dev; 1240 struct list_head *pool = &queue->qp->rdma_mrs; 1241 1242 if (!blk_rq_nr_phys_segments(rq)) 1243 return; 1244 1245 if (req->use_sig_mr) 1246 pool = &queue->qp->sig_mrs; 1247 1248 if (req->mr) { 1249 ib_mr_pool_put(queue->qp, pool, req->mr); 1250 req->mr = NULL; 1251 } 1252 1253 nvme_rdma_dma_unmap_req(ibdev, rq); 1254 } 1255 1256 static int nvme_rdma_set_sg_null(struct nvme_command *c) 1257 { 1258 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; 1259 1260 sg->addr = 0; 1261 put_unaligned_le24(0, sg->length); 1262 put_unaligned_le32(0, sg->key); 1263 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; 1264 return 0; 1265 } 1266 1267 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue, 1268 struct nvme_rdma_request *req, struct nvme_command *c, 1269 int count) 1270 { 1271 struct nvme_sgl_desc *sg = &c->common.dptr.sgl; 1272 struct ib_sge *sge = &req->sge[1]; 1273 struct scatterlist *sgl; 1274 u32 len = 0; 1275 int i; 1276 1277 for_each_sg(req->data_sgl.sg_table.sgl, sgl, count, i) { 1278 sge->addr = sg_dma_address(sgl); 1279 sge->length = sg_dma_len(sgl); 1280 sge->lkey = queue->device->pd->local_dma_lkey; 1281 len += sge->length; 1282 sge++; 1283 } 1284 1285 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff); 1286 sg->length = cpu_to_le32(len); 1287 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET; 1288 1289 req->num_sge += count; 1290 return 0; 1291 } 1292 1293 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue, 1294 struct nvme_rdma_request *req, struct nvme_command *c) 1295 { 1296 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; 1297 1298 sg->addr = cpu_to_le64(sg_dma_address(req->data_sgl.sg_table.sgl)); 1299 put_unaligned_le24(sg_dma_len(req->data_sgl.sg_table.sgl), sg->length); 1300 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key); 1301 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; 1302 return 0; 1303 } 1304 1305 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue, 1306 struct nvme_rdma_request *req, struct nvme_command *c, 1307 int count) 1308 { 1309 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; 1310 int nr; 1311 1312 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs); 1313 if (WARN_ON_ONCE(!req->mr)) 1314 return -EAGAIN; 1315 1316 /* 1317 * Align the MR to a 4K page size to match the ctrl page size and 1318 * the block virtual boundary. 1319 */ 1320 nr = ib_map_mr_sg(req->mr, req->data_sgl.sg_table.sgl, count, NULL, 1321 SZ_4K); 1322 if (unlikely(nr < count)) { 1323 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr); 1324 req->mr = NULL; 1325 if (nr < 0) 1326 return nr; 1327 return -EINVAL; 1328 } 1329 1330 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey)); 1331 1332 req->reg_cqe.done = nvme_rdma_memreg_done; 1333 memset(&req->reg_wr, 0, sizeof(req->reg_wr)); 1334 req->reg_wr.wr.opcode = IB_WR_REG_MR; 1335 req->reg_wr.wr.wr_cqe = &req->reg_cqe; 1336 req->reg_wr.wr.num_sge = 0; 1337 req->reg_wr.mr = req->mr; 1338 req->reg_wr.key = req->mr->rkey; 1339 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE | 1340 IB_ACCESS_REMOTE_READ | 1341 IB_ACCESS_REMOTE_WRITE; 1342 1343 sg->addr = cpu_to_le64(req->mr->iova); 1344 put_unaligned_le24(req->mr->length, sg->length); 1345 put_unaligned_le32(req->mr->rkey, sg->key); 1346 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) | 1347 NVME_SGL_FMT_INVALIDATE; 1348 1349 return 0; 1350 } 1351 1352 static void nvme_rdma_set_sig_domain(struct blk_integrity *bi, 1353 struct nvme_command *cmd, struct ib_sig_domain *domain, 1354 u16 control, u8 pi_type) 1355 { 1356 domain->sig_type = IB_SIG_TYPE_T10_DIF; 1357 domain->sig.dif.bg_type = IB_T10DIF_CRC; 1358 domain->sig.dif.pi_interval = 1 << bi->interval_exp; 1359 domain->sig.dif.ref_tag = le32_to_cpu(cmd->rw.reftag); 1360 if (control & NVME_RW_PRINFO_PRCHK_REF) 1361 domain->sig.dif.ref_remap = true; 1362 1363 domain->sig.dif.app_tag = le16_to_cpu(cmd->rw.apptag); 1364 domain->sig.dif.apptag_check_mask = le16_to_cpu(cmd->rw.appmask); 1365 domain->sig.dif.app_escape = true; 1366 if (pi_type == NVME_NS_DPS_PI_TYPE3) 1367 domain->sig.dif.ref_escape = true; 1368 } 1369 1370 static void nvme_rdma_set_sig_attrs(struct blk_integrity *bi, 1371 struct nvme_command *cmd, struct ib_sig_attrs *sig_attrs, 1372 u8 pi_type) 1373 { 1374 u16 control = le16_to_cpu(cmd->rw.control); 1375 1376 memset(sig_attrs, 0, sizeof(*sig_attrs)); 1377 if (control & NVME_RW_PRINFO_PRACT) { 1378 /* for WRITE_INSERT/READ_STRIP no memory domain */ 1379 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE; 1380 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control, 1381 pi_type); 1382 /* Clear the PRACT bit since HCA will generate/verify the PI */ 1383 control &= ~NVME_RW_PRINFO_PRACT; 1384 cmd->rw.control = cpu_to_le16(control); 1385 } else { 1386 /* for WRITE_PASS/READ_PASS both wire/memory domains exist */ 1387 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control, 1388 pi_type); 1389 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->mem, control, 1390 pi_type); 1391 } 1392 } 1393 1394 static void nvme_rdma_set_prot_checks(struct nvme_command *cmd, u8 *mask) 1395 { 1396 *mask = 0; 1397 if (le16_to_cpu(cmd->rw.control) & NVME_RW_PRINFO_PRCHK_REF) 1398 *mask |= IB_SIG_CHECK_REFTAG; 1399 if (le16_to_cpu(cmd->rw.control) & NVME_RW_PRINFO_PRCHK_GUARD) 1400 *mask |= IB_SIG_CHECK_GUARD; 1401 } 1402 1403 static void nvme_rdma_sig_done(struct ib_cq *cq, struct ib_wc *wc) 1404 { 1405 if (unlikely(wc->status != IB_WC_SUCCESS)) 1406 nvme_rdma_wr_error(cq, wc, "SIG"); 1407 } 1408 1409 static int nvme_rdma_map_sg_pi(struct nvme_rdma_queue *queue, 1410 struct nvme_rdma_request *req, struct nvme_command *c, 1411 int count, int pi_count) 1412 { 1413 struct nvme_rdma_sgl *sgl = &req->data_sgl; 1414 struct ib_reg_wr *wr = &req->reg_wr; 1415 struct request *rq = blk_mq_rq_from_pdu(req); 1416 struct nvme_ns *ns = rq->q->queuedata; 1417 struct bio *bio = rq->bio; 1418 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; 1419 struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk); 1420 u32 xfer_len; 1421 int nr; 1422 1423 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->sig_mrs); 1424 if (WARN_ON_ONCE(!req->mr)) 1425 return -EAGAIN; 1426 1427 nr = ib_map_mr_sg_pi(req->mr, sgl->sg_table.sgl, count, NULL, 1428 req->metadata_sgl->sg_table.sgl, pi_count, NULL, 1429 SZ_4K); 1430 if (unlikely(nr)) 1431 goto mr_put; 1432 1433 nvme_rdma_set_sig_attrs(bi, c, req->mr->sig_attrs, ns->head->pi_type); 1434 nvme_rdma_set_prot_checks(c, &req->mr->sig_attrs->check_mask); 1435 1436 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey)); 1437 1438 req->reg_cqe.done = nvme_rdma_sig_done; 1439 memset(wr, 0, sizeof(*wr)); 1440 wr->wr.opcode = IB_WR_REG_MR_INTEGRITY; 1441 wr->wr.wr_cqe = &req->reg_cqe; 1442 wr->wr.num_sge = 0; 1443 wr->wr.send_flags = 0; 1444 wr->mr = req->mr; 1445 wr->key = req->mr->rkey; 1446 wr->access = IB_ACCESS_LOCAL_WRITE | 1447 IB_ACCESS_REMOTE_READ | 1448 IB_ACCESS_REMOTE_WRITE; 1449 1450 sg->addr = cpu_to_le64(req->mr->iova); 1451 xfer_len = req->mr->length; 1452 /* Check if PI is added by the HW */ 1453 if (!pi_count) 1454 xfer_len += (xfer_len >> bi->interval_exp) * ns->head->pi_size; 1455 put_unaligned_le24(xfer_len, sg->length); 1456 put_unaligned_le32(req->mr->rkey, sg->key); 1457 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; 1458 1459 return 0; 1460 1461 mr_put: 1462 ib_mr_pool_put(queue->qp, &queue->qp->sig_mrs, req->mr); 1463 req->mr = NULL; 1464 if (nr < 0) 1465 return nr; 1466 return -EINVAL; 1467 } 1468 1469 static int nvme_rdma_dma_map_req(struct ib_device *ibdev, struct request *rq, 1470 int *count, int *pi_count) 1471 { 1472 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1473 int ret; 1474 1475 req->data_sgl.sg_table.sgl = (struct scatterlist *)(req + 1); 1476 ret = sg_alloc_table_chained(&req->data_sgl.sg_table, 1477 blk_rq_nr_phys_segments(rq), req->data_sgl.sg_table.sgl, 1478 NVME_INLINE_SG_CNT); 1479 if (ret) 1480 return -ENOMEM; 1481 1482 req->data_sgl.nents = blk_rq_map_sg(rq->q, rq, 1483 req->data_sgl.sg_table.sgl); 1484 1485 *count = ib_dma_map_sg(ibdev, req->data_sgl.sg_table.sgl, 1486 req->data_sgl.nents, rq_dma_dir(rq)); 1487 if (unlikely(*count <= 0)) { 1488 ret = -EIO; 1489 goto out_free_table; 1490 } 1491 1492 if (blk_integrity_rq(rq)) { 1493 req->metadata_sgl->sg_table.sgl = 1494 (struct scatterlist *)(req->metadata_sgl + 1); 1495 ret = sg_alloc_table_chained(&req->metadata_sgl->sg_table, 1496 blk_rq_count_integrity_sg(rq->q, rq->bio), 1497 req->metadata_sgl->sg_table.sgl, 1498 NVME_INLINE_METADATA_SG_CNT); 1499 if (unlikely(ret)) { 1500 ret = -ENOMEM; 1501 goto out_unmap_sg; 1502 } 1503 1504 req->metadata_sgl->nents = blk_rq_map_integrity_sg(rq->q, 1505 rq->bio, req->metadata_sgl->sg_table.sgl); 1506 *pi_count = ib_dma_map_sg(ibdev, 1507 req->metadata_sgl->sg_table.sgl, 1508 req->metadata_sgl->nents, 1509 rq_dma_dir(rq)); 1510 if (unlikely(*pi_count <= 0)) { 1511 ret = -EIO; 1512 goto out_free_pi_table; 1513 } 1514 } 1515 1516 return 0; 1517 1518 out_free_pi_table: 1519 sg_free_table_chained(&req->metadata_sgl->sg_table, 1520 NVME_INLINE_METADATA_SG_CNT); 1521 out_unmap_sg: 1522 ib_dma_unmap_sg(ibdev, req->data_sgl.sg_table.sgl, req->data_sgl.nents, 1523 rq_dma_dir(rq)); 1524 out_free_table: 1525 sg_free_table_chained(&req->data_sgl.sg_table, NVME_INLINE_SG_CNT); 1526 return ret; 1527 } 1528 1529 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue, 1530 struct request *rq, struct nvme_command *c) 1531 { 1532 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1533 struct nvme_rdma_device *dev = queue->device; 1534 struct ib_device *ibdev = dev->dev; 1535 int pi_count = 0; 1536 int count, ret; 1537 1538 req->num_sge = 1; 1539 refcount_set(&req->ref, 2); /* send and recv completions */ 1540 1541 c->common.flags |= NVME_CMD_SGL_METABUF; 1542 1543 if (!blk_rq_nr_phys_segments(rq)) 1544 return nvme_rdma_set_sg_null(c); 1545 1546 ret = nvme_rdma_dma_map_req(ibdev, rq, &count, &pi_count); 1547 if (unlikely(ret)) 1548 return ret; 1549 1550 if (req->use_sig_mr) { 1551 ret = nvme_rdma_map_sg_pi(queue, req, c, count, pi_count); 1552 goto out; 1553 } 1554 1555 if (count <= dev->num_inline_segments) { 1556 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) && 1557 queue->ctrl->use_inline_data && 1558 blk_rq_payload_bytes(rq) <= 1559 nvme_rdma_inline_data_size(queue)) { 1560 ret = nvme_rdma_map_sg_inline(queue, req, c, count); 1561 goto out; 1562 } 1563 1564 if (count == 1 && dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) { 1565 ret = nvme_rdma_map_sg_single(queue, req, c); 1566 goto out; 1567 } 1568 } 1569 1570 ret = nvme_rdma_map_sg_fr(queue, req, c, count); 1571 out: 1572 if (unlikely(ret)) 1573 goto out_dma_unmap_req; 1574 1575 return 0; 1576 1577 out_dma_unmap_req: 1578 nvme_rdma_dma_unmap_req(ibdev, rq); 1579 return ret; 1580 } 1581 1582 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc) 1583 { 1584 struct nvme_rdma_qe *qe = 1585 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe); 1586 struct nvme_rdma_request *req = 1587 container_of(qe, struct nvme_rdma_request, sqe); 1588 1589 if (unlikely(wc->status != IB_WC_SUCCESS)) 1590 nvme_rdma_wr_error(cq, wc, "SEND"); 1591 else 1592 nvme_rdma_end_request(req); 1593 } 1594 1595 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue, 1596 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge, 1597 struct ib_send_wr *first) 1598 { 1599 struct ib_send_wr wr; 1600 int ret; 1601 1602 sge->addr = qe->dma; 1603 sge->length = sizeof(struct nvme_command); 1604 sge->lkey = queue->device->pd->local_dma_lkey; 1605 1606 wr.next = NULL; 1607 wr.wr_cqe = &qe->cqe; 1608 wr.sg_list = sge; 1609 wr.num_sge = num_sge; 1610 wr.opcode = IB_WR_SEND; 1611 wr.send_flags = IB_SEND_SIGNALED; 1612 1613 if (first) 1614 first->next = ≀ 1615 else 1616 first = ≀ 1617 1618 ret = ib_post_send(queue->qp, first, NULL); 1619 if (unlikely(ret)) { 1620 dev_err(queue->ctrl->ctrl.device, 1621 "%s failed with error code %d\n", __func__, ret); 1622 } 1623 return ret; 1624 } 1625 1626 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue, 1627 struct nvme_rdma_qe *qe) 1628 { 1629 struct ib_recv_wr wr; 1630 struct ib_sge list; 1631 int ret; 1632 1633 list.addr = qe->dma; 1634 list.length = sizeof(struct nvme_completion); 1635 list.lkey = queue->device->pd->local_dma_lkey; 1636 1637 qe->cqe.done = nvme_rdma_recv_done; 1638 1639 wr.next = NULL; 1640 wr.wr_cqe = &qe->cqe; 1641 wr.sg_list = &list; 1642 wr.num_sge = 1; 1643 1644 ret = ib_post_recv(queue->qp, &wr, NULL); 1645 if (unlikely(ret)) { 1646 dev_err(queue->ctrl->ctrl.device, 1647 "%s failed with error code %d\n", __func__, ret); 1648 } 1649 return ret; 1650 } 1651 1652 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue) 1653 { 1654 u32 queue_idx = nvme_rdma_queue_idx(queue); 1655 1656 if (queue_idx == 0) 1657 return queue->ctrl->admin_tag_set.tags[queue_idx]; 1658 return queue->ctrl->tag_set.tags[queue_idx - 1]; 1659 } 1660 1661 static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc) 1662 { 1663 if (unlikely(wc->status != IB_WC_SUCCESS)) 1664 nvme_rdma_wr_error(cq, wc, "ASYNC"); 1665 } 1666 1667 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg) 1668 { 1669 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg); 1670 struct nvme_rdma_queue *queue = &ctrl->queues[0]; 1671 struct ib_device *dev = queue->device->dev; 1672 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe; 1673 struct nvme_command *cmd = sqe->data; 1674 struct ib_sge sge; 1675 int ret; 1676 1677 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE); 1678 1679 memset(cmd, 0, sizeof(*cmd)); 1680 cmd->common.opcode = nvme_admin_async_event; 1681 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH; 1682 cmd->common.flags |= NVME_CMD_SGL_METABUF; 1683 nvme_rdma_set_sg_null(cmd); 1684 1685 sqe->cqe.done = nvme_rdma_async_done; 1686 1687 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd), 1688 DMA_TO_DEVICE); 1689 1690 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL); 1691 WARN_ON_ONCE(ret); 1692 } 1693 1694 static void nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue, 1695 struct nvme_completion *cqe, struct ib_wc *wc) 1696 { 1697 struct request *rq; 1698 struct nvme_rdma_request *req; 1699 1700 rq = nvme_find_rq(nvme_rdma_tagset(queue), cqe->command_id); 1701 if (!rq) { 1702 dev_err(queue->ctrl->ctrl.device, 1703 "got bad command_id %#x on QP %#x\n", 1704 cqe->command_id, queue->qp->qp_num); 1705 nvme_rdma_error_recovery(queue->ctrl); 1706 return; 1707 } 1708 req = blk_mq_rq_to_pdu(rq); 1709 1710 req->status = cqe->status; 1711 req->result = cqe->result; 1712 1713 if (wc->wc_flags & IB_WC_WITH_INVALIDATE) { 1714 if (unlikely(!req->mr || 1715 wc->ex.invalidate_rkey != req->mr->rkey)) { 1716 dev_err(queue->ctrl->ctrl.device, 1717 "Bogus remote invalidation for rkey %#x\n", 1718 req->mr ? req->mr->rkey : 0); 1719 nvme_rdma_error_recovery(queue->ctrl); 1720 } 1721 } else if (req->mr) { 1722 int ret; 1723 1724 ret = nvme_rdma_inv_rkey(queue, req); 1725 if (unlikely(ret < 0)) { 1726 dev_err(queue->ctrl->ctrl.device, 1727 "Queueing INV WR for rkey %#x failed (%d)\n", 1728 req->mr->rkey, ret); 1729 nvme_rdma_error_recovery(queue->ctrl); 1730 } 1731 /* the local invalidation completion will end the request */ 1732 return; 1733 } 1734 1735 nvme_rdma_end_request(req); 1736 } 1737 1738 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc) 1739 { 1740 struct nvme_rdma_qe *qe = 1741 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe); 1742 struct nvme_rdma_queue *queue = wc->qp->qp_context; 1743 struct ib_device *ibdev = queue->device->dev; 1744 struct nvme_completion *cqe = qe->data; 1745 const size_t len = sizeof(struct nvme_completion); 1746 1747 if (unlikely(wc->status != IB_WC_SUCCESS)) { 1748 nvme_rdma_wr_error(cq, wc, "RECV"); 1749 return; 1750 } 1751 1752 /* sanity checking for received data length */ 1753 if (unlikely(wc->byte_len < len)) { 1754 dev_err(queue->ctrl->ctrl.device, 1755 "Unexpected nvme completion length(%d)\n", wc->byte_len); 1756 nvme_rdma_error_recovery(queue->ctrl); 1757 return; 1758 } 1759 1760 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE); 1761 /* 1762 * AEN requests are special as they don't time out and can 1763 * survive any kind of queue freeze and often don't respond to 1764 * aborts. We don't even bother to allocate a struct request 1765 * for them but rather special case them here. 1766 */ 1767 if (unlikely(nvme_is_aen_req(nvme_rdma_queue_idx(queue), 1768 cqe->command_id))) 1769 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status, 1770 &cqe->result); 1771 else 1772 nvme_rdma_process_nvme_rsp(queue, cqe, wc); 1773 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE); 1774 1775 nvme_rdma_post_recv(queue, qe); 1776 } 1777 1778 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue) 1779 { 1780 int ret, i; 1781 1782 for (i = 0; i < queue->queue_size; i++) { 1783 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]); 1784 if (ret) 1785 return ret; 1786 } 1787 1788 return 0; 1789 } 1790 1791 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue, 1792 struct rdma_cm_event *ev) 1793 { 1794 struct rdma_cm_id *cm_id = queue->cm_id; 1795 int status = ev->status; 1796 const char *rej_msg; 1797 const struct nvme_rdma_cm_rej *rej_data; 1798 u8 rej_data_len; 1799 1800 rej_msg = rdma_reject_msg(cm_id, status); 1801 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len); 1802 1803 if (rej_data && rej_data_len >= sizeof(u16)) { 1804 u16 sts = le16_to_cpu(rej_data->sts); 1805 1806 dev_err(queue->ctrl->ctrl.device, 1807 "Connect rejected: status %d (%s) nvme status %d (%s).\n", 1808 status, rej_msg, sts, nvme_rdma_cm_msg(sts)); 1809 } else { 1810 dev_err(queue->ctrl->ctrl.device, 1811 "Connect rejected: status %d (%s).\n", status, rej_msg); 1812 } 1813 1814 return -ECONNRESET; 1815 } 1816 1817 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue) 1818 { 1819 struct nvme_ctrl *ctrl = &queue->ctrl->ctrl; 1820 int ret; 1821 1822 ret = nvme_rdma_create_queue_ib(queue); 1823 if (ret) 1824 return ret; 1825 1826 if (ctrl->opts->tos >= 0) 1827 rdma_set_service_type(queue->cm_id, ctrl->opts->tos); 1828 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CM_TIMEOUT_MS); 1829 if (ret) { 1830 dev_err(ctrl->device, "rdma_resolve_route failed (%d).\n", 1831 queue->cm_error); 1832 goto out_destroy_queue; 1833 } 1834 1835 return 0; 1836 1837 out_destroy_queue: 1838 nvme_rdma_destroy_queue_ib(queue); 1839 return ret; 1840 } 1841 1842 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue) 1843 { 1844 struct nvme_rdma_ctrl *ctrl = queue->ctrl; 1845 struct rdma_conn_param param = { }; 1846 struct nvme_rdma_cm_req priv = { }; 1847 int ret; 1848 1849 param.qp_num = queue->qp->qp_num; 1850 param.flow_control = 1; 1851 1852 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom; 1853 /* maximum retry count */ 1854 param.retry_count = 7; 1855 param.rnr_retry_count = 7; 1856 param.private_data = &priv; 1857 param.private_data_len = sizeof(priv); 1858 1859 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); 1860 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue)); 1861 /* 1862 * set the admin queue depth to the minimum size 1863 * specified by the Fabrics standard. 1864 */ 1865 if (priv.qid == 0) { 1866 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH); 1867 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1); 1868 } else { 1869 /* 1870 * current interpretation of the fabrics spec 1871 * is at minimum you make hrqsize sqsize+1, or a 1872 * 1's based representation of sqsize. 1873 */ 1874 priv.hrqsize = cpu_to_le16(queue->queue_size); 1875 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize); 1876 } 1877 1878 ret = rdma_connect_locked(queue->cm_id, ¶m); 1879 if (ret) { 1880 dev_err(ctrl->ctrl.device, 1881 "rdma_connect_locked failed (%d).\n", ret); 1882 return ret; 1883 } 1884 1885 return 0; 1886 } 1887 1888 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, 1889 struct rdma_cm_event *ev) 1890 { 1891 struct nvme_rdma_queue *queue = cm_id->context; 1892 int cm_error = 0; 1893 1894 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n", 1895 rdma_event_msg(ev->event), ev->event, 1896 ev->status, cm_id); 1897 1898 switch (ev->event) { 1899 case RDMA_CM_EVENT_ADDR_RESOLVED: 1900 cm_error = nvme_rdma_addr_resolved(queue); 1901 break; 1902 case RDMA_CM_EVENT_ROUTE_RESOLVED: 1903 cm_error = nvme_rdma_route_resolved(queue); 1904 break; 1905 case RDMA_CM_EVENT_ESTABLISHED: 1906 queue->cm_error = nvme_rdma_conn_established(queue); 1907 /* complete cm_done regardless of success/failure */ 1908 complete(&queue->cm_done); 1909 return 0; 1910 case RDMA_CM_EVENT_REJECTED: 1911 cm_error = nvme_rdma_conn_rejected(queue, ev); 1912 break; 1913 case RDMA_CM_EVENT_ROUTE_ERROR: 1914 case RDMA_CM_EVENT_CONNECT_ERROR: 1915 case RDMA_CM_EVENT_UNREACHABLE: 1916 case RDMA_CM_EVENT_ADDR_ERROR: 1917 dev_dbg(queue->ctrl->ctrl.device, 1918 "CM error event %d\n", ev->event); 1919 cm_error = -ECONNRESET; 1920 break; 1921 case RDMA_CM_EVENT_DISCONNECTED: 1922 case RDMA_CM_EVENT_ADDR_CHANGE: 1923 case RDMA_CM_EVENT_TIMEWAIT_EXIT: 1924 dev_dbg(queue->ctrl->ctrl.device, 1925 "disconnect received - connection closed\n"); 1926 nvme_rdma_error_recovery(queue->ctrl); 1927 break; 1928 case RDMA_CM_EVENT_DEVICE_REMOVAL: 1929 /* device removal is handled via the ib_client API */ 1930 break; 1931 default: 1932 dev_err(queue->ctrl->ctrl.device, 1933 "Unexpected RDMA CM event (%d)\n", ev->event); 1934 nvme_rdma_error_recovery(queue->ctrl); 1935 break; 1936 } 1937 1938 if (cm_error) { 1939 queue->cm_error = cm_error; 1940 complete(&queue->cm_done); 1941 } 1942 1943 return 0; 1944 } 1945 1946 static void nvme_rdma_complete_timed_out(struct request *rq) 1947 { 1948 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1949 struct nvme_rdma_queue *queue = req->queue; 1950 1951 nvme_rdma_stop_queue(queue); 1952 nvmf_complete_timed_out_request(rq); 1953 } 1954 1955 static enum blk_eh_timer_return nvme_rdma_timeout(struct request *rq) 1956 { 1957 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1958 struct nvme_rdma_queue *queue = req->queue; 1959 struct nvme_rdma_ctrl *ctrl = queue->ctrl; 1960 struct nvme_command *cmd = req->req.cmd; 1961 int qid = nvme_rdma_queue_idx(queue); 1962 1963 dev_warn(ctrl->ctrl.device, 1964 "I/O tag %d (%04x) opcode %#x (%s) QID %d timeout\n", 1965 rq->tag, nvme_cid(rq), cmd->common.opcode, 1966 nvme_fabrics_opcode_str(qid, cmd), qid); 1967 1968 if (nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_LIVE) { 1969 /* 1970 * If we are resetting, connecting or deleting we should 1971 * complete immediately because we may block controller 1972 * teardown or setup sequence 1973 * - ctrl disable/shutdown fabrics requests 1974 * - connect requests 1975 * - initialization admin requests 1976 * - I/O requests that entered after unquiescing and 1977 * the controller stopped responding 1978 * 1979 * All other requests should be cancelled by the error 1980 * recovery work, so it's fine that we fail it here. 1981 */ 1982 nvme_rdma_complete_timed_out(rq); 1983 return BLK_EH_DONE; 1984 } 1985 1986 /* 1987 * LIVE state should trigger the normal error recovery which will 1988 * handle completing this request. 1989 */ 1990 nvme_rdma_error_recovery(ctrl); 1991 return BLK_EH_RESET_TIMER; 1992 } 1993 1994 static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx, 1995 const struct blk_mq_queue_data *bd) 1996 { 1997 struct nvme_ns *ns = hctx->queue->queuedata; 1998 struct nvme_rdma_queue *queue = hctx->driver_data; 1999 struct request *rq = bd->rq; 2000 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 2001 struct nvme_rdma_qe *sqe = &req->sqe; 2002 struct nvme_command *c = nvme_req(rq)->cmd; 2003 struct ib_device *dev; 2004 bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags); 2005 blk_status_t ret; 2006 int err; 2007 2008 WARN_ON_ONCE(rq->tag < 0); 2009 2010 if (!nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready)) 2011 return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq); 2012 2013 dev = queue->device->dev; 2014 2015 req->sqe.dma = ib_dma_map_single(dev, req->sqe.data, 2016 sizeof(struct nvme_command), 2017 DMA_TO_DEVICE); 2018 err = ib_dma_mapping_error(dev, req->sqe.dma); 2019 if (unlikely(err)) 2020 return BLK_STS_RESOURCE; 2021 2022 ib_dma_sync_single_for_cpu(dev, sqe->dma, 2023 sizeof(struct nvme_command), DMA_TO_DEVICE); 2024 2025 ret = nvme_setup_cmd(ns, rq); 2026 if (ret) 2027 goto unmap_qe; 2028 2029 nvme_start_request(rq); 2030 2031 if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) && 2032 queue->pi_support && 2033 (c->common.opcode == nvme_cmd_write || 2034 c->common.opcode == nvme_cmd_read) && 2035 nvme_ns_has_pi(ns->head)) 2036 req->use_sig_mr = true; 2037 else 2038 req->use_sig_mr = false; 2039 2040 err = nvme_rdma_map_data(queue, rq, c); 2041 if (unlikely(err < 0)) { 2042 dev_err(queue->ctrl->ctrl.device, 2043 "Failed to map data (%d)\n", err); 2044 goto err; 2045 } 2046 2047 sqe->cqe.done = nvme_rdma_send_done; 2048 2049 ib_dma_sync_single_for_device(dev, sqe->dma, 2050 sizeof(struct nvme_command), DMA_TO_DEVICE); 2051 2052 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge, 2053 req->mr ? &req->reg_wr.wr : NULL); 2054 if (unlikely(err)) 2055 goto err_unmap; 2056 2057 return BLK_STS_OK; 2058 2059 err_unmap: 2060 nvme_rdma_unmap_data(queue, rq); 2061 err: 2062 if (err == -EIO) 2063 ret = nvme_host_path_error(rq); 2064 else if (err == -ENOMEM || err == -EAGAIN) 2065 ret = BLK_STS_RESOURCE; 2066 else 2067 ret = BLK_STS_IOERR; 2068 nvme_cleanup_cmd(rq); 2069 unmap_qe: 2070 ib_dma_unmap_single(dev, req->sqe.dma, sizeof(struct nvme_command), 2071 DMA_TO_DEVICE); 2072 return ret; 2073 } 2074 2075 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob) 2076 { 2077 struct nvme_rdma_queue *queue = hctx->driver_data; 2078 2079 return ib_process_cq_direct(queue->ib_cq, -1); 2080 } 2081 2082 static void nvme_rdma_check_pi_status(struct nvme_rdma_request *req) 2083 { 2084 struct request *rq = blk_mq_rq_from_pdu(req); 2085 struct ib_mr_status mr_status; 2086 int ret; 2087 2088 ret = ib_check_mr_status(req->mr, IB_MR_CHECK_SIG_STATUS, &mr_status); 2089 if (ret) { 2090 pr_err("ib_check_mr_status failed, ret %d\n", ret); 2091 nvme_req(rq)->status = NVME_SC_INVALID_PI; 2092 return; 2093 } 2094 2095 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) { 2096 switch (mr_status.sig_err.err_type) { 2097 case IB_SIG_BAD_GUARD: 2098 nvme_req(rq)->status = NVME_SC_GUARD_CHECK; 2099 break; 2100 case IB_SIG_BAD_REFTAG: 2101 nvme_req(rq)->status = NVME_SC_REFTAG_CHECK; 2102 break; 2103 case IB_SIG_BAD_APPTAG: 2104 nvme_req(rq)->status = NVME_SC_APPTAG_CHECK; 2105 break; 2106 } 2107 pr_err("PI error found type %d expected 0x%x vs actual 0x%x\n", 2108 mr_status.sig_err.err_type, mr_status.sig_err.expected, 2109 mr_status.sig_err.actual); 2110 } 2111 } 2112 2113 static void nvme_rdma_complete_rq(struct request *rq) 2114 { 2115 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 2116 struct nvme_rdma_queue *queue = req->queue; 2117 struct ib_device *ibdev = queue->device->dev; 2118 2119 if (req->use_sig_mr) 2120 nvme_rdma_check_pi_status(req); 2121 2122 nvme_rdma_unmap_data(queue, rq); 2123 ib_dma_unmap_single(ibdev, req->sqe.dma, sizeof(struct nvme_command), 2124 DMA_TO_DEVICE); 2125 nvme_complete_rq(rq); 2126 } 2127 2128 static void nvme_rdma_map_queues(struct blk_mq_tag_set *set) 2129 { 2130 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(set->driver_data); 2131 2132 nvmf_map_queues(set, &ctrl->ctrl, ctrl->io_queues); 2133 } 2134 2135 static const struct blk_mq_ops nvme_rdma_mq_ops = { 2136 .queue_rq = nvme_rdma_queue_rq, 2137 .complete = nvme_rdma_complete_rq, 2138 .init_request = nvme_rdma_init_request, 2139 .exit_request = nvme_rdma_exit_request, 2140 .init_hctx = nvme_rdma_init_hctx, 2141 .timeout = nvme_rdma_timeout, 2142 .map_queues = nvme_rdma_map_queues, 2143 .poll = nvme_rdma_poll, 2144 }; 2145 2146 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = { 2147 .queue_rq = nvme_rdma_queue_rq, 2148 .complete = nvme_rdma_complete_rq, 2149 .init_request = nvme_rdma_init_request, 2150 .exit_request = nvme_rdma_exit_request, 2151 .init_hctx = nvme_rdma_init_admin_hctx, 2152 .timeout = nvme_rdma_timeout, 2153 }; 2154 2155 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown) 2156 { 2157 nvme_rdma_teardown_io_queues(ctrl, shutdown); 2158 nvme_quiesce_admin_queue(&ctrl->ctrl); 2159 nvme_disable_ctrl(&ctrl->ctrl, shutdown); 2160 nvme_rdma_teardown_admin_queue(ctrl, shutdown); 2161 } 2162 2163 static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl) 2164 { 2165 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true); 2166 } 2167 2168 static void nvme_rdma_reset_ctrl_work(struct work_struct *work) 2169 { 2170 struct nvme_rdma_ctrl *ctrl = 2171 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work); 2172 2173 nvme_stop_ctrl(&ctrl->ctrl); 2174 nvme_rdma_shutdown_ctrl(ctrl, false); 2175 2176 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) { 2177 /* state change failure should never happen */ 2178 WARN_ON_ONCE(1); 2179 return; 2180 } 2181 2182 if (nvme_rdma_setup_ctrl(ctrl, false)) 2183 goto out_fail; 2184 2185 return; 2186 2187 out_fail: 2188 ++ctrl->ctrl.nr_reconnects; 2189 nvme_rdma_reconnect_or_remove(ctrl); 2190 } 2191 2192 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = { 2193 .name = "rdma", 2194 .module = THIS_MODULE, 2195 .flags = NVME_F_FABRICS | NVME_F_METADATA_SUPPORTED, 2196 .reg_read32 = nvmf_reg_read32, 2197 .reg_read64 = nvmf_reg_read64, 2198 .reg_write32 = nvmf_reg_write32, 2199 .free_ctrl = nvme_rdma_free_ctrl, 2200 .submit_async_event = nvme_rdma_submit_async_event, 2201 .delete_ctrl = nvme_rdma_delete_ctrl, 2202 .get_address = nvmf_get_address, 2203 .stop_ctrl = nvme_rdma_stop_ctrl, 2204 }; 2205 2206 /* 2207 * Fails a connection request if it matches an existing controller 2208 * (association) with the same tuple: 2209 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN> 2210 * 2211 * if local address is not specified in the request, it will match an 2212 * existing controller with all the other parameters the same and no 2213 * local port address specified as well. 2214 * 2215 * The ports don't need to be compared as they are intrinsically 2216 * already matched by the port pointers supplied. 2217 */ 2218 static bool 2219 nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts) 2220 { 2221 struct nvme_rdma_ctrl *ctrl; 2222 bool found = false; 2223 2224 mutex_lock(&nvme_rdma_ctrl_mutex); 2225 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) { 2226 found = nvmf_ip_options_match(&ctrl->ctrl, opts); 2227 if (found) 2228 break; 2229 } 2230 mutex_unlock(&nvme_rdma_ctrl_mutex); 2231 2232 return found; 2233 } 2234 2235 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev, 2236 struct nvmf_ctrl_options *opts) 2237 { 2238 struct nvme_rdma_ctrl *ctrl; 2239 int ret; 2240 bool changed; 2241 2242 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); 2243 if (!ctrl) 2244 return ERR_PTR(-ENOMEM); 2245 ctrl->ctrl.opts = opts; 2246 INIT_LIST_HEAD(&ctrl->list); 2247 2248 if (!(opts->mask & NVMF_OPT_TRSVCID)) { 2249 opts->trsvcid = 2250 kstrdup(__stringify(NVME_RDMA_IP_PORT), GFP_KERNEL); 2251 if (!opts->trsvcid) { 2252 ret = -ENOMEM; 2253 goto out_free_ctrl; 2254 } 2255 opts->mask |= NVMF_OPT_TRSVCID; 2256 } 2257 2258 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, 2259 opts->traddr, opts->trsvcid, &ctrl->addr); 2260 if (ret) { 2261 pr_err("malformed address passed: %s:%s\n", 2262 opts->traddr, opts->trsvcid); 2263 goto out_free_ctrl; 2264 } 2265 2266 if (opts->mask & NVMF_OPT_HOST_TRADDR) { 2267 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, 2268 opts->host_traddr, NULL, &ctrl->src_addr); 2269 if (ret) { 2270 pr_err("malformed src address passed: %s\n", 2271 opts->host_traddr); 2272 goto out_free_ctrl; 2273 } 2274 } 2275 2276 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) { 2277 ret = -EALREADY; 2278 goto out_free_ctrl; 2279 } 2280 2281 INIT_DELAYED_WORK(&ctrl->reconnect_work, 2282 nvme_rdma_reconnect_ctrl_work); 2283 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work); 2284 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work); 2285 2286 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues + 2287 opts->nr_poll_queues + 1; 2288 ctrl->ctrl.sqsize = opts->queue_size - 1; 2289 ctrl->ctrl.kato = opts->kato; 2290 2291 ret = -ENOMEM; 2292 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues), 2293 GFP_KERNEL); 2294 if (!ctrl->queues) 2295 goto out_free_ctrl; 2296 2297 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops, 2298 0 /* no quirks, we're perfect! */); 2299 if (ret) 2300 goto out_kfree_queues; 2301 2302 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING); 2303 WARN_ON_ONCE(!changed); 2304 2305 ret = nvme_rdma_setup_ctrl(ctrl, true); 2306 if (ret) 2307 goto out_uninit_ctrl; 2308 2309 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs, hostnqn: %s\n", 2310 nvmf_ctrl_subsysnqn(&ctrl->ctrl), &ctrl->addr, opts->host->nqn); 2311 2312 mutex_lock(&nvme_rdma_ctrl_mutex); 2313 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list); 2314 mutex_unlock(&nvme_rdma_ctrl_mutex); 2315 2316 return &ctrl->ctrl; 2317 2318 out_uninit_ctrl: 2319 nvme_uninit_ctrl(&ctrl->ctrl); 2320 nvme_put_ctrl(&ctrl->ctrl); 2321 if (ret > 0) 2322 ret = -EIO; 2323 return ERR_PTR(ret); 2324 out_kfree_queues: 2325 kfree(ctrl->queues); 2326 out_free_ctrl: 2327 kfree(ctrl); 2328 return ERR_PTR(ret); 2329 } 2330 2331 static struct nvmf_transport_ops nvme_rdma_transport = { 2332 .name = "rdma", 2333 .module = THIS_MODULE, 2334 .required_opts = NVMF_OPT_TRADDR, 2335 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY | 2336 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO | 2337 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES | 2338 NVMF_OPT_TOS, 2339 .create_ctrl = nvme_rdma_create_ctrl, 2340 }; 2341 2342 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data) 2343 { 2344 struct nvme_rdma_ctrl *ctrl; 2345 struct nvme_rdma_device *ndev; 2346 bool found = false; 2347 2348 mutex_lock(&device_list_mutex); 2349 list_for_each_entry(ndev, &device_list, entry) { 2350 if (ndev->dev == ib_device) { 2351 found = true; 2352 break; 2353 } 2354 } 2355 mutex_unlock(&device_list_mutex); 2356 2357 if (!found) 2358 return; 2359 2360 /* Delete all controllers using this device */ 2361 mutex_lock(&nvme_rdma_ctrl_mutex); 2362 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) { 2363 if (ctrl->device->dev != ib_device) 2364 continue; 2365 nvme_delete_ctrl(&ctrl->ctrl); 2366 } 2367 mutex_unlock(&nvme_rdma_ctrl_mutex); 2368 2369 flush_workqueue(nvme_delete_wq); 2370 } 2371 2372 static struct ib_client nvme_rdma_ib_client = { 2373 .name = "nvme_rdma", 2374 .remove = nvme_rdma_remove_one 2375 }; 2376 2377 static int __init nvme_rdma_init_module(void) 2378 { 2379 int ret; 2380 2381 ret = ib_register_client(&nvme_rdma_ib_client); 2382 if (ret) 2383 return ret; 2384 2385 ret = nvmf_register_transport(&nvme_rdma_transport); 2386 if (ret) 2387 goto err_unreg_client; 2388 2389 return 0; 2390 2391 err_unreg_client: 2392 ib_unregister_client(&nvme_rdma_ib_client); 2393 return ret; 2394 } 2395 2396 static void __exit nvme_rdma_cleanup_module(void) 2397 { 2398 struct nvme_rdma_ctrl *ctrl; 2399 2400 nvmf_unregister_transport(&nvme_rdma_transport); 2401 ib_unregister_client(&nvme_rdma_ib_client); 2402 2403 mutex_lock(&nvme_rdma_ctrl_mutex); 2404 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) 2405 nvme_delete_ctrl(&ctrl->ctrl); 2406 mutex_unlock(&nvme_rdma_ctrl_mutex); 2407 flush_workqueue(nvme_delete_wq); 2408 } 2409 2410 module_init(nvme_rdma_init_module); 2411 module_exit(nvme_rdma_cleanup_module); 2412 2413 MODULE_DESCRIPTION("NVMe host RDMA transport driver"); 2414 MODULE_LICENSE("GPL v2"); 2415