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