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_idx && ctrl->ctrl.max_integrity_segments) 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 = ndev->dev->attrs.max_send_sge; 406 if (ndev->num_inline_segments) 407 ndev->num_inline_segments--; 408 ndev->num_inline_segments = min(ndev->num_inline_segments, NVME_RDMA_MAX_INLINE_SEGMENTS); 409 list_add(&ndev->entry, &device_list); 410 out_unlock: 411 mutex_unlock(&device_list_mutex); 412 return ndev; 413 414 out_free_pd: 415 ib_dealloc_pd(ndev->pd); 416 out_free_dev: 417 kfree(ndev); 418 out_err: 419 mutex_unlock(&device_list_mutex); 420 return NULL; 421 } 422 423 static void nvme_rdma_free_cq(struct nvme_rdma_queue *queue) 424 { 425 if (nvme_rdma_poll_queue(queue)) 426 ib_free_cq(queue->ib_cq); 427 else 428 ib_cq_pool_put(queue->ib_cq, queue->cq_size); 429 } 430 431 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue) 432 { 433 struct nvme_rdma_device *dev; 434 struct ib_device *ibdev; 435 436 if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags)) 437 return; 438 439 dev = queue->device; 440 ibdev = dev->dev; 441 442 if (queue->pi_support) 443 ib_mr_pool_destroy(queue->qp, &queue->qp->sig_mrs); 444 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs); 445 446 /* 447 * The cm_id object might have been destroyed during RDMA connection 448 * establishment error flow to avoid getting other cma events, thus 449 * the destruction of the QP shouldn't use rdma_cm API. 450 */ 451 ib_destroy_qp(queue->qp); 452 nvme_rdma_free_cq(queue); 453 454 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size, 455 sizeof(struct nvme_completion), DMA_FROM_DEVICE); 456 457 nvme_rdma_dev_put(dev); 458 } 459 460 static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev, bool pi_support) 461 { 462 u32 max_page_list_len; 463 464 if (pi_support) 465 max_page_list_len = ibdev->attrs.max_pi_fast_reg_page_list_len; 466 else 467 max_page_list_len = ibdev->attrs.max_fast_reg_page_list_len; 468 469 return min_t(u32, NVME_RDMA_MAX_SEGMENTS, max_page_list_len - 1); 470 } 471 472 static int nvme_rdma_create_cq(struct ib_device *ibdev, 473 struct nvme_rdma_queue *queue) 474 { 475 int ret, comp_vector, idx = nvme_rdma_queue_idx(queue); 476 477 /* 478 * Spread I/O queues completion vectors according their queue index. 479 * Admin queues can always go on completion vector 0. 480 */ 481 comp_vector = (idx == 0 ? idx : idx - 1) % ibdev->num_comp_vectors; 482 483 /* Polling queues need direct cq polling context */ 484 if (nvme_rdma_poll_queue(queue)) 485 queue->ib_cq = ib_alloc_cq(ibdev, queue, queue->cq_size, 486 comp_vector, IB_POLL_DIRECT); 487 else 488 queue->ib_cq = ib_cq_pool_get(ibdev, queue->cq_size, 489 comp_vector, IB_POLL_SOFTIRQ); 490 491 if (IS_ERR(queue->ib_cq)) { 492 ret = PTR_ERR(queue->ib_cq); 493 return ret; 494 } 495 496 return 0; 497 } 498 499 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue) 500 { 501 struct ib_device *ibdev; 502 const int send_wr_factor = 3; /* MR, SEND, INV */ 503 const int cq_factor = send_wr_factor + 1; /* + RECV */ 504 int ret, pages_per_mr; 505 506 queue->device = nvme_rdma_find_get_device(queue->cm_id); 507 if (!queue->device) { 508 dev_err(queue->cm_id->device->dev.parent, 509 "no client data found!\n"); 510 return -ECONNREFUSED; 511 } 512 ibdev = queue->device->dev; 513 514 /* +1 for ib_drain_qp */ 515 queue->cq_size = cq_factor * queue->queue_size + 1; 516 517 ret = nvme_rdma_create_cq(ibdev, queue); 518 if (ret) 519 goto out_put_dev; 520 521 ret = nvme_rdma_create_qp(queue, send_wr_factor); 522 if (ret) 523 goto out_destroy_ib_cq; 524 525 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size, 526 sizeof(struct nvme_completion), DMA_FROM_DEVICE); 527 if (!queue->rsp_ring) { 528 ret = -ENOMEM; 529 goto out_destroy_qp; 530 } 531 532 /* 533 * Currently we don't use SG_GAPS MR's so if the first entry is 534 * misaligned we'll end up using two entries for a single data page, 535 * so one additional entry is required. 536 */ 537 pages_per_mr = nvme_rdma_get_max_fr_pages(ibdev, queue->pi_support) + 1; 538 ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs, 539 queue->queue_size, 540 IB_MR_TYPE_MEM_REG, 541 pages_per_mr, 0); 542 if (ret) { 543 dev_err(queue->ctrl->ctrl.device, 544 "failed to initialize MR pool sized %d for QID %d\n", 545 queue->queue_size, nvme_rdma_queue_idx(queue)); 546 goto out_destroy_ring; 547 } 548 549 if (queue->pi_support) { 550 ret = ib_mr_pool_init(queue->qp, &queue->qp->sig_mrs, 551 queue->queue_size, IB_MR_TYPE_INTEGRITY, 552 pages_per_mr, pages_per_mr); 553 if (ret) { 554 dev_err(queue->ctrl->ctrl.device, 555 "failed to initialize PI MR pool sized %d for QID %d\n", 556 queue->queue_size, nvme_rdma_queue_idx(queue)); 557 goto out_destroy_mr_pool; 558 } 559 } 560 561 set_bit(NVME_RDMA_Q_TR_READY, &queue->flags); 562 563 return 0; 564 565 out_destroy_mr_pool: 566 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs); 567 out_destroy_ring: 568 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size, 569 sizeof(struct nvme_completion), DMA_FROM_DEVICE); 570 out_destroy_qp: 571 rdma_destroy_qp(queue->cm_id); 572 out_destroy_ib_cq: 573 nvme_rdma_free_cq(queue); 574 out_put_dev: 575 nvme_rdma_dev_put(queue->device); 576 return ret; 577 } 578 579 static int nvme_rdma_alloc_queue(struct nvme_rdma_queue *queue) 580 { 581 struct nvme_rdma_ctrl *ctrl = queue->ctrl; 582 int idx = nvme_rdma_queue_idx(queue); 583 struct sockaddr *src_addr = NULL; 584 int ret; 585 586 mutex_init(&queue->queue_lock); 587 if (idx && ctrl->ctrl.max_integrity_segments) 588 queue->pi_support = true; 589 else 590 queue->pi_support = false; 591 init_completion(&queue->cm_done); 592 593 if (idx > 0) 594 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16; 595 else 596 queue->cmnd_capsule_len = sizeof(struct nvme_command); 597 598 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue, 599 RDMA_PS_TCP, IB_QPT_RC); 600 if (IS_ERR(queue->cm_id)) { 601 dev_info(ctrl->ctrl.device, 602 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id)); 603 ret = PTR_ERR(queue->cm_id); 604 goto out_destroy_mutex; 605 } 606 607 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR) 608 src_addr = (struct sockaddr *)&ctrl->src_addr; 609 610 queue->cm_error = -ETIMEDOUT; 611 ret = rdma_resolve_addr(queue->cm_id, src_addr, 612 (struct sockaddr *)&ctrl->addr, 613 NVME_RDMA_CM_TIMEOUT_MS); 614 if (ret) { 615 dev_info(ctrl->ctrl.device, 616 "rdma_resolve_addr failed (%d).\n", ret); 617 goto out_destroy_cm_id; 618 } 619 620 ret = nvme_rdma_wait_for_cm(queue); 621 if (ret) { 622 dev_info(ctrl->ctrl.device, 623 "rdma connection establishment failed (%d)\n", ret); 624 goto out_destroy_cm_id; 625 } 626 627 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags); 628 629 return 0; 630 631 out_destroy_cm_id: 632 rdma_destroy_id(queue->cm_id); 633 nvme_rdma_destroy_queue_ib(queue); 634 out_destroy_mutex: 635 mutex_destroy(&queue->queue_lock); 636 return ret; 637 } 638 639 static void __nvme_rdma_stop_queue(struct nvme_rdma_queue *queue) 640 { 641 rdma_disconnect(queue->cm_id); 642 ib_drain_qp(queue->qp); 643 } 644 645 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue) 646 { 647 if (!test_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags)) 648 return; 649 650 mutex_lock(&queue->queue_lock); 651 if (test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags)) 652 __nvme_rdma_stop_queue(queue); 653 mutex_unlock(&queue->queue_lock); 654 } 655 656 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue) 657 { 658 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags)) 659 return; 660 661 rdma_destroy_id(queue->cm_id); 662 nvme_rdma_destroy_queue_ib(queue); 663 mutex_destroy(&queue->queue_lock); 664 } 665 666 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl) 667 { 668 int i; 669 670 for (i = 1; i < ctrl->ctrl.queue_count; i++) 671 nvme_rdma_free_queue(&ctrl->queues[i]); 672 } 673 674 static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl) 675 { 676 int i; 677 678 for (i = 1; i < ctrl->ctrl.queue_count; i++) 679 nvme_rdma_stop_queue(&ctrl->queues[i]); 680 } 681 682 static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx) 683 { 684 struct nvme_rdma_queue *queue = &ctrl->queues[idx]; 685 int ret; 686 687 if (idx) 688 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx); 689 else 690 ret = nvmf_connect_admin_queue(&ctrl->ctrl); 691 692 if (!ret) { 693 set_bit(NVME_RDMA_Q_LIVE, &queue->flags); 694 } else { 695 if (test_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags)) 696 __nvme_rdma_stop_queue(queue); 697 dev_info(ctrl->ctrl.device, 698 "failed to connect queue: %d ret=%d\n", idx, ret); 699 } 700 return ret; 701 } 702 703 static void nvme_rdma_setup_queue_async(void *data, async_cookie_t cookie) 704 { 705 struct nvme_rdma_setup_ctx *ctx = data; 706 struct nvme_rdma_queue *queue; 707 int ret; 708 709 queue = ctx->queue; 710 ret = nvme_rdma_alloc_queue(queue); 711 if (ret) 712 goto out_err; 713 714 ret = nvme_rdma_start_queue(queue->ctrl, nvme_rdma_queue_idx(queue)); 715 if (ret) 716 goto out_err; 717 718 return; 719 out_err: 720 WRITE_ONCE(*ctx->err, ret); 721 } 722 723 static int nvme_rdma_setup_io_queues(struct nvme_rdma_ctrl *ctrl, 724 unsigned int first, unsigned int last, size_t queue_size) 725 { 726 ASYNC_DOMAIN_EXCLUSIVE(queue_domain); 727 struct nvme_rdma_setup_ctx *ctxs; 728 int nr_queues = last - first; 729 int err = 0, i, ret; 730 731 ctxs = kmalloc_objs(*ctxs, nr_queues); 732 if (!ctxs) 733 return -ENOMEM; 734 735 for (i = 0; i < nr_queues; i++) { 736 struct nvme_rdma_queue *queue = &ctrl->queues[first + i]; 737 738 queue->queue_size = queue_size; 739 740 ctxs[i].queue = queue; 741 ctxs[i].err = &err; 742 async_schedule_domain(nvme_rdma_setup_queue_async, &ctxs[i], 743 &queue_domain); 744 } 745 746 async_synchronize_full_domain(&queue_domain); 747 kfree(ctxs); 748 749 ret = READ_ONCE(err); 750 if (ret) 751 goto out_free_queues; 752 753 return 0; 754 out_free_queues: 755 for (i = 0; i < nr_queues; i++) { 756 struct nvme_rdma_queue *queue = 757 &ctrl->queues[first + i]; 758 759 if (test_bit(NVME_RDMA_Q_LIVE, &queue->flags)) 760 nvme_rdma_stop_queue(queue); 761 if (test_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags)) 762 nvme_rdma_free_queue(queue); 763 } 764 765 return ret; 766 } 767 768 static int nvme_rdma_alloc_tag_set(struct nvme_ctrl *ctrl) 769 { 770 unsigned int cmd_size = sizeof(struct nvme_rdma_request) + 771 NVME_RDMA_DATA_SGL_SIZE; 772 773 if (ctrl->max_integrity_segments) 774 cmd_size += sizeof(struct nvme_rdma_sgl) + 775 NVME_RDMA_METADATA_SGL_SIZE; 776 777 return nvme_alloc_io_tag_set(ctrl, &to_rdma_ctrl(ctrl)->tag_set, 778 &nvme_rdma_mq_ops, 779 ctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2, 780 cmd_size); 781 } 782 783 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl) 784 { 785 if (ctrl->async_event_sqe.data) { 786 cancel_work_sync(&ctrl->ctrl.async_event_work); 787 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe, 788 sizeof(struct nvme_command), DMA_TO_DEVICE); 789 ctrl->async_event_sqe.data = NULL; 790 } 791 nvme_rdma_free_queue(&ctrl->queues[0]); 792 } 793 794 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl, 795 bool new) 796 { 797 bool pi_capable = false; 798 int error; 799 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 return; 1750 } 1751 } else if (req->mr) { 1752 int ret; 1753 1754 ret = nvme_rdma_inv_rkey(queue, req); 1755 if (unlikely(ret < 0)) { 1756 dev_err(queue->ctrl->ctrl.device, 1757 "Queueing INV WR for rkey %#x failed (%d)\n", 1758 req->mr->rkey, ret); 1759 nvme_rdma_error_recovery(queue->ctrl); 1760 } 1761 /* the local invalidation completion will end the request */ 1762 return; 1763 } 1764 1765 nvme_rdma_end_request(req); 1766 } 1767 1768 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc) 1769 { 1770 struct nvme_rdma_qe *qe = 1771 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe); 1772 struct nvme_rdma_queue *queue = wc->qp->qp_context; 1773 struct ib_device *ibdev = queue->device->dev; 1774 struct nvme_completion *cqe = qe->data; 1775 const size_t len = sizeof(struct nvme_completion); 1776 1777 if (unlikely(wc->status != IB_WC_SUCCESS)) { 1778 nvme_rdma_wr_error(cq, wc, "RECV"); 1779 return; 1780 } 1781 1782 /* sanity checking for received data length */ 1783 if (unlikely(wc->byte_len < len)) { 1784 dev_err(queue->ctrl->ctrl.device, 1785 "Unexpected nvme completion length(%d)\n", wc->byte_len); 1786 nvme_rdma_error_recovery(queue->ctrl); 1787 return; 1788 } 1789 1790 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE); 1791 /* 1792 * AEN requests are special as they don't time out and can 1793 * survive any kind of queue freeze and often don't respond to 1794 * aborts. We don't even bother to allocate a struct request 1795 * for them but rather special case them here. 1796 */ 1797 if (unlikely(nvme_is_aen_req(nvme_rdma_queue_idx(queue), 1798 cqe->command_id))) 1799 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status, 1800 &cqe->result); 1801 else 1802 nvme_rdma_process_nvme_rsp(queue, cqe, wc); 1803 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE); 1804 1805 nvme_rdma_post_recv(queue, qe); 1806 } 1807 1808 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue) 1809 { 1810 int ret, i; 1811 1812 for (i = 0; i < queue->queue_size; i++) { 1813 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]); 1814 if (ret) 1815 return ret; 1816 } 1817 1818 return 0; 1819 } 1820 1821 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue, 1822 struct rdma_cm_event *ev) 1823 { 1824 struct rdma_cm_id *cm_id = queue->cm_id; 1825 int status = ev->status; 1826 const char *rej_msg; 1827 const struct nvme_rdma_cm_rej *rej_data; 1828 u8 rej_data_len; 1829 1830 rej_msg = rdma_reject_msg(cm_id, status); 1831 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len); 1832 1833 if (rej_data && rej_data_len >= sizeof(u16)) { 1834 u16 sts = le16_to_cpu(rej_data->sts); 1835 1836 dev_err(queue->ctrl->ctrl.device, 1837 "Connect rejected: status %d (%s) nvme status %d (%s).\n", 1838 status, rej_msg, sts, nvme_rdma_cm_msg(sts)); 1839 } else { 1840 dev_err(queue->ctrl->ctrl.device, 1841 "Connect rejected: status %d (%s).\n", status, rej_msg); 1842 } 1843 1844 return -ECONNRESET; 1845 } 1846 1847 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue) 1848 { 1849 struct nvme_ctrl *ctrl = &queue->ctrl->ctrl; 1850 int ret; 1851 1852 ret = nvme_rdma_create_queue_ib(queue); 1853 if (ret) 1854 return ret; 1855 1856 if (ctrl->opts->tos >= 0) 1857 rdma_set_service_type(queue->cm_id, ctrl->opts->tos); 1858 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CM_TIMEOUT_MS); 1859 if (ret) { 1860 dev_err(ctrl->device, "rdma_resolve_route failed (%d).\n", 1861 queue->cm_error); 1862 goto out_destroy_queue; 1863 } 1864 1865 return 0; 1866 1867 out_destroy_queue: 1868 nvme_rdma_destroy_queue_ib(queue); 1869 return ret; 1870 } 1871 1872 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue) 1873 { 1874 struct nvme_rdma_ctrl *ctrl = queue->ctrl; 1875 struct rdma_conn_param param = { }; 1876 struct nvme_rdma_cm_req priv = { }; 1877 int ret; 1878 1879 param.qp_num = queue->qp->qp_num; 1880 param.flow_control = 1; 1881 1882 param.responder_resources = min(queue->device->dev->attrs.max_qp_rd_atom, U8_MAX); 1883 /* maximum retry count */ 1884 param.retry_count = 7; 1885 param.rnr_retry_count = 7; 1886 param.private_data = &priv; 1887 param.private_data_len = sizeof(priv); 1888 1889 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); 1890 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue)); 1891 /* 1892 * set the admin queue depth to the minimum size 1893 * specified by the Fabrics standard. 1894 */ 1895 if (priv.qid == 0) { 1896 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH); 1897 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1); 1898 } else { 1899 /* 1900 * current interpretation of the fabrics spec 1901 * is at minimum you make hrqsize sqsize+1, or a 1902 * 1's based representation of sqsize. 1903 */ 1904 priv.hrqsize = cpu_to_le16(queue->queue_size); 1905 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize); 1906 /* cntlid should only be set when creating an I/O queue */ 1907 priv.cntlid = cpu_to_le16(ctrl->ctrl.cntlid); 1908 } 1909 1910 ret = rdma_connect_locked(queue->cm_id, ¶m); 1911 if (ret) { 1912 dev_err(ctrl->ctrl.device, 1913 "rdma_connect_locked failed (%d).\n", ret); 1914 return ret; 1915 } 1916 1917 return 0; 1918 } 1919 1920 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, 1921 struct rdma_cm_event *ev) 1922 { 1923 struct nvme_rdma_queue *queue = cm_id->context; 1924 int cm_error = 0; 1925 1926 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n", 1927 rdma_event_msg(ev->event), ev->event, 1928 ev->status, cm_id); 1929 1930 switch (ev->event) { 1931 case RDMA_CM_EVENT_ADDR_RESOLVED: 1932 cm_error = nvme_rdma_addr_resolved(queue); 1933 break; 1934 case RDMA_CM_EVENT_ROUTE_RESOLVED: 1935 cm_error = nvme_rdma_route_resolved(queue); 1936 break; 1937 case RDMA_CM_EVENT_ESTABLISHED: 1938 queue->cm_error = nvme_rdma_conn_established(queue); 1939 /* complete cm_done regardless of success/failure */ 1940 complete(&queue->cm_done); 1941 return 0; 1942 case RDMA_CM_EVENT_REJECTED: 1943 cm_error = nvme_rdma_conn_rejected(queue, ev); 1944 break; 1945 case RDMA_CM_EVENT_ROUTE_ERROR: 1946 case RDMA_CM_EVENT_CONNECT_ERROR: 1947 case RDMA_CM_EVENT_UNREACHABLE: 1948 case RDMA_CM_EVENT_ADDR_ERROR: 1949 dev_dbg(queue->ctrl->ctrl.device, 1950 "CM error event %d\n", ev->event); 1951 cm_error = -ECONNRESET; 1952 break; 1953 case RDMA_CM_EVENT_DISCONNECTED: 1954 case RDMA_CM_EVENT_ADDR_CHANGE: 1955 case RDMA_CM_EVENT_TIMEWAIT_EXIT: 1956 dev_dbg(queue->ctrl->ctrl.device, 1957 "disconnect received - connection closed\n"); 1958 nvme_rdma_error_recovery(queue->ctrl); 1959 break; 1960 case RDMA_CM_EVENT_DEVICE_REMOVAL: 1961 /* device removal is handled via the ib_client API */ 1962 break; 1963 default: 1964 dev_err(queue->ctrl->ctrl.device, 1965 "Unexpected RDMA CM event (%d)\n", ev->event); 1966 nvme_rdma_error_recovery(queue->ctrl); 1967 break; 1968 } 1969 1970 if (cm_error) { 1971 queue->cm_error = cm_error; 1972 complete(&queue->cm_done); 1973 } 1974 1975 return 0; 1976 } 1977 1978 static void nvme_rdma_complete_timed_out(struct request *rq) 1979 { 1980 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1981 struct nvme_rdma_queue *queue = req->queue; 1982 1983 nvme_rdma_stop_queue(queue); 1984 nvmf_complete_timed_out_request(rq); 1985 } 1986 1987 static enum blk_eh_timer_return nvme_rdma_timeout(struct request *rq) 1988 { 1989 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1990 struct nvme_rdma_queue *queue = req->queue; 1991 struct nvme_rdma_ctrl *ctrl = queue->ctrl; 1992 struct nvme_command *cmd = req->req.cmd; 1993 int qid = nvme_rdma_queue_idx(queue); 1994 1995 dev_warn(ctrl->ctrl.device, 1996 "I/O tag %d (%04x) opcode %#x (%s) QID %d timeout\n", 1997 rq->tag, nvme_cid(rq), cmd->common.opcode, 1998 nvme_fabrics_opcode_str(qid, cmd), qid); 1999 2000 if (nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_LIVE) { 2001 /* 2002 * If we are resetting, connecting or deleting we should 2003 * complete immediately because we may block controller 2004 * teardown or setup sequence 2005 * - ctrl disable/shutdown fabrics requests 2006 * - connect requests 2007 * - initialization admin requests 2008 * - I/O requests that entered after unquiescing and 2009 * the controller stopped responding 2010 * 2011 * All other requests should be cancelled by the error 2012 * recovery work, so it's fine that we fail it here. 2013 */ 2014 nvme_rdma_complete_timed_out(rq); 2015 return BLK_EH_DONE; 2016 } 2017 2018 /* 2019 * LIVE state should trigger the normal error recovery which will 2020 * handle completing this request. 2021 */ 2022 nvme_rdma_error_recovery(ctrl); 2023 return BLK_EH_RESET_TIMER; 2024 } 2025 2026 static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx, 2027 const struct blk_mq_queue_data *bd) 2028 { 2029 struct nvme_ns *ns = hctx->queue->queuedata; 2030 struct nvme_rdma_queue *queue = hctx->driver_data; 2031 struct request *rq = bd->rq; 2032 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 2033 struct nvme_rdma_qe *sqe = &req->sqe; 2034 struct nvme_command *c = nvme_req(rq)->cmd; 2035 struct ib_device *dev; 2036 bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags); 2037 blk_status_t ret; 2038 int err = 0; 2039 2040 WARN_ON_ONCE(rq->tag < 0); 2041 2042 if (!nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready)) 2043 return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq); 2044 2045 dev = queue->device->dev; 2046 2047 req->sqe.dma = ib_dma_map_single(dev, req->sqe.data, 2048 sizeof(struct nvme_command), 2049 DMA_TO_DEVICE); 2050 err = ib_dma_mapping_error(dev, req->sqe.dma); 2051 if (unlikely(err)) 2052 return BLK_STS_RESOURCE; 2053 2054 ib_dma_sync_single_for_cpu(dev, sqe->dma, 2055 sizeof(struct nvme_command), DMA_TO_DEVICE); 2056 2057 ret = nvme_setup_cmd(ns, rq); 2058 if (ret) 2059 goto unmap_qe; 2060 2061 nvme_start_request(rq); 2062 2063 if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) && 2064 queue->pi_support && 2065 (c->common.opcode == nvme_cmd_write || 2066 c->common.opcode == nvme_cmd_read) && 2067 nvme_ns_has_pi(ns->head)) 2068 req->use_sig_mr = true; 2069 else 2070 req->use_sig_mr = false; 2071 2072 err = nvme_rdma_map_data(queue, rq, c); 2073 if (unlikely(err < 0)) { 2074 dev_err(queue->ctrl->ctrl.device, 2075 "Failed to map data (%d)\n", err); 2076 goto err; 2077 } 2078 2079 sqe->cqe.done = nvme_rdma_send_done; 2080 2081 ib_dma_sync_single_for_device(dev, sqe->dma, 2082 sizeof(struct nvme_command), DMA_TO_DEVICE); 2083 2084 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge, 2085 req->mr ? &req->reg_wr.wr : NULL); 2086 if (unlikely(err)) 2087 goto err_unmap; 2088 2089 return BLK_STS_OK; 2090 2091 err_unmap: 2092 nvme_rdma_unmap_data(queue, rq); 2093 err: 2094 if (err != -EIO) { 2095 nvme_cleanup_cmd(rq); 2096 if (err == -ENOMEM || err == -EAGAIN) 2097 ret = BLK_STS_RESOURCE; 2098 else 2099 ret = BLK_STS_IOERR; 2100 } 2101 unmap_qe: 2102 ib_dma_unmap_single(dev, req->sqe.dma, sizeof(struct nvme_command), 2103 DMA_TO_DEVICE); 2104 if (err == -EIO) 2105 return nvme_host_path_error(rq); 2106 return ret; 2107 } 2108 2109 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob) 2110 { 2111 struct nvme_rdma_queue *queue = hctx->driver_data; 2112 2113 return ib_process_cq_direct(queue->ib_cq, -1); 2114 } 2115 2116 static void nvme_rdma_check_pi_status(struct nvme_rdma_request *req) 2117 { 2118 struct request *rq = blk_mq_rq_from_pdu(req); 2119 struct ib_mr_status mr_status; 2120 int ret; 2121 2122 ret = ib_check_mr_status(req->mr, IB_MR_CHECK_SIG_STATUS, &mr_status); 2123 if (ret) { 2124 pr_err("ib_check_mr_status failed, ret %d\n", ret); 2125 nvme_req(rq)->status = NVME_SC_INVALID_PI; 2126 return; 2127 } 2128 2129 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) { 2130 switch (mr_status.sig_err.err_type) { 2131 case IB_SIG_BAD_GUARD: 2132 nvme_req(rq)->status = NVME_SC_GUARD_CHECK; 2133 break; 2134 case IB_SIG_BAD_REFTAG: 2135 nvme_req(rq)->status = NVME_SC_REFTAG_CHECK; 2136 break; 2137 case IB_SIG_BAD_APPTAG: 2138 nvme_req(rq)->status = NVME_SC_APPTAG_CHECK; 2139 break; 2140 } 2141 pr_err("PI error found type %d expected 0x%x vs actual 0x%x\n", 2142 mr_status.sig_err.err_type, mr_status.sig_err.expected, 2143 mr_status.sig_err.actual); 2144 } 2145 } 2146 2147 static void nvme_rdma_complete_rq(struct request *rq) 2148 { 2149 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 2150 struct nvme_rdma_queue *queue = req->queue; 2151 struct ib_device *ibdev = queue->device->dev; 2152 2153 if (req->use_sig_mr) 2154 nvme_rdma_check_pi_status(req); 2155 2156 nvme_rdma_unmap_data(queue, rq); 2157 ib_dma_unmap_single(ibdev, req->sqe.dma, sizeof(struct nvme_command), 2158 DMA_TO_DEVICE); 2159 nvme_complete_rq(rq); 2160 } 2161 2162 static void nvme_rdma_map_queues(struct blk_mq_tag_set *set) 2163 { 2164 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(set->driver_data); 2165 2166 nvmf_map_queues(set, &ctrl->ctrl, ctrl->io_queues); 2167 } 2168 2169 static const struct blk_mq_ops nvme_rdma_mq_ops = { 2170 .queue_rq = nvme_rdma_queue_rq, 2171 .complete = nvme_rdma_complete_rq, 2172 .init_request = nvme_rdma_init_request, 2173 .exit_request = nvme_rdma_exit_request, 2174 .init_hctx = nvme_rdma_init_hctx, 2175 .timeout = nvme_rdma_timeout, 2176 .map_queues = nvme_rdma_map_queues, 2177 .poll = nvme_rdma_poll, 2178 }; 2179 2180 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = { 2181 .queue_rq = nvme_rdma_queue_rq, 2182 .complete = nvme_rdma_complete_rq, 2183 .init_request = nvme_rdma_init_request, 2184 .exit_request = nvme_rdma_exit_request, 2185 .init_hctx = nvme_rdma_init_admin_hctx, 2186 .timeout = nvme_rdma_timeout, 2187 }; 2188 2189 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown) 2190 { 2191 nvme_rdma_teardown_io_queues(ctrl, shutdown); 2192 nvme_quiesce_admin_queue(&ctrl->ctrl); 2193 nvme_disable_ctrl(&ctrl->ctrl, shutdown); 2194 nvme_rdma_teardown_admin_queue(ctrl, shutdown); 2195 } 2196 2197 static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl) 2198 { 2199 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true); 2200 } 2201 2202 static void nvme_rdma_reset_ctrl_work(struct work_struct *work) 2203 { 2204 struct nvme_rdma_ctrl *ctrl = 2205 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work); 2206 int ret; 2207 2208 nvme_stop_ctrl(&ctrl->ctrl); 2209 nvme_rdma_shutdown_ctrl(ctrl, false); 2210 2211 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) { 2212 /* state change failure should never happen */ 2213 WARN_ON_ONCE(1); 2214 return; 2215 } 2216 2217 ret = nvme_rdma_setup_ctrl(ctrl, false); 2218 if (ret) 2219 goto out_fail; 2220 2221 return; 2222 2223 out_fail: 2224 ++ctrl->ctrl.nr_reconnects; 2225 nvme_rdma_reconnect_or_remove(ctrl, ret); 2226 } 2227 2228 static bool nvme_rdma_supports_pci_p2pdma(struct nvme_ctrl *ctrl) 2229 { 2230 struct nvme_rdma_ctrl *r_ctrl = to_rdma_ctrl(ctrl); 2231 2232 return ib_dma_pci_p2p_dma_supported(r_ctrl->device->dev); 2233 } 2234 2235 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = { 2236 .name = "rdma", 2237 .module = THIS_MODULE, 2238 .flags = NVME_F_FABRICS | NVME_F_METADATA_SUPPORTED, 2239 .reg_read32 = nvmf_reg_read32, 2240 .reg_read64 = nvmf_reg_read64, 2241 .reg_write32 = nvmf_reg_write32, 2242 .subsystem_reset = nvmf_subsystem_reset, 2243 .free_ctrl = nvme_rdma_free_ctrl, 2244 .submit_async_event = nvme_rdma_submit_async_event, 2245 .delete_ctrl = nvme_rdma_delete_ctrl, 2246 .get_address = nvmf_get_address, 2247 .stop_ctrl = nvme_rdma_stop_ctrl, 2248 .get_virt_boundary = nvme_get_virt_boundary, 2249 .supports_pci_p2pdma = nvme_rdma_supports_pci_p2pdma, 2250 }; 2251 2252 /* 2253 * Fails a connection request if it matches an existing controller 2254 * (association) with the same tuple: 2255 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN> 2256 * 2257 * if local address is not specified in the request, it will match an 2258 * existing controller with all the other parameters the same and no 2259 * local port address specified as well. 2260 * 2261 * The ports don't need to be compared as they are intrinsically 2262 * already matched by the port pointers supplied. 2263 */ 2264 static bool 2265 nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts) 2266 { 2267 struct nvme_rdma_ctrl *ctrl; 2268 bool found = false; 2269 2270 mutex_lock(&nvme_rdma_ctrl_mutex); 2271 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) { 2272 found = nvmf_ip_options_match(&ctrl->ctrl, opts); 2273 if (found) 2274 break; 2275 } 2276 mutex_unlock(&nvme_rdma_ctrl_mutex); 2277 2278 return found; 2279 } 2280 2281 static struct nvme_rdma_ctrl *nvme_rdma_alloc_ctrl(struct device *dev, 2282 struct nvmf_ctrl_options *opts) 2283 { 2284 struct nvme_rdma_ctrl *ctrl; 2285 int ret, i; 2286 2287 ctrl = kzalloc_obj(*ctrl); 2288 if (!ctrl) 2289 return ERR_PTR(-ENOMEM); 2290 ctrl->ctrl.opts = opts; 2291 /* 2292 * Safe to init list while allocating ctrl object. 2293 */ 2294 context_unsafe(INIT_LIST_HEAD(&ctrl->list)); 2295 2296 if (!(opts->mask & NVMF_OPT_TRSVCID)) { 2297 opts->trsvcid = 2298 kstrdup(__stringify(NVME_RDMA_IP_PORT), GFP_KERNEL); 2299 if (!opts->trsvcid) { 2300 ret = -ENOMEM; 2301 goto out_free_ctrl; 2302 } 2303 opts->mask |= NVMF_OPT_TRSVCID; 2304 } 2305 2306 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, 2307 opts->traddr, opts->trsvcid, &ctrl->addr); 2308 if (ret) { 2309 pr_err("malformed address passed: %s:%s\n", 2310 opts->traddr, opts->trsvcid); 2311 goto out_free_ctrl; 2312 } 2313 2314 if (opts->mask & NVMF_OPT_HOST_TRADDR) { 2315 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, 2316 opts->host_traddr, NULL, &ctrl->src_addr); 2317 if (ret) { 2318 pr_err("malformed src address passed: %s\n", 2319 opts->host_traddr); 2320 goto out_free_ctrl; 2321 } 2322 } 2323 2324 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) { 2325 ret = -EALREADY; 2326 goto out_free_ctrl; 2327 } 2328 2329 INIT_DELAYED_WORK(&ctrl->reconnect_work, 2330 nvme_rdma_reconnect_ctrl_work); 2331 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work); 2332 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work); 2333 2334 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues + 2335 opts->nr_poll_queues + 1; 2336 ctrl->ctrl.sqsize = opts->queue_size - 1; 2337 ctrl->ctrl.kato = opts->kato; 2338 2339 ret = -ENOMEM; 2340 ctrl->queues = kzalloc_objs(*ctrl->queues, ctrl->ctrl.queue_count); 2341 if (!ctrl->queues) 2342 goto out_free_ctrl; 2343 2344 for (i = 0; i < ctrl->ctrl.queue_count; i++) 2345 ctrl->queues[i].ctrl = ctrl; 2346 2347 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops, 2348 0 /* no quirks, we're perfect! */); 2349 if (ret) 2350 goto out_kfree_queues; 2351 2352 return ctrl; 2353 2354 out_kfree_queues: 2355 kfree(ctrl->queues); 2356 out_free_ctrl: 2357 kfree(ctrl); 2358 return ERR_PTR(ret); 2359 } 2360 2361 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev, 2362 struct nvmf_ctrl_options *opts) 2363 { 2364 struct nvme_rdma_ctrl *ctrl; 2365 bool changed; 2366 int ret; 2367 2368 ctrl = nvme_rdma_alloc_ctrl(dev, opts); 2369 if (IS_ERR(ctrl)) 2370 return ERR_CAST(ctrl); 2371 2372 ret = nvme_add_ctrl(&ctrl->ctrl); 2373 if (ret) 2374 goto out_put_ctrl; 2375 2376 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING); 2377 WARN_ON_ONCE(!changed); 2378 2379 ret = nvme_rdma_setup_ctrl(ctrl, true); 2380 if (ret) 2381 goto out_uninit_ctrl; 2382 2383 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs, hostnqn: %s\n", 2384 nvmf_ctrl_subsysnqn(&ctrl->ctrl), &ctrl->addr, opts->host->nqn); 2385 2386 mutex_lock(&nvme_rdma_ctrl_mutex); 2387 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list); 2388 mutex_unlock(&nvme_rdma_ctrl_mutex); 2389 2390 return &ctrl->ctrl; 2391 2392 out_uninit_ctrl: 2393 nvme_uninit_ctrl(&ctrl->ctrl); 2394 out_put_ctrl: 2395 nvme_put_ctrl(&ctrl->ctrl); 2396 if (ret > 0) 2397 ret = -EIO; 2398 return ERR_PTR(ret); 2399 } 2400 2401 static struct nvmf_transport_ops nvme_rdma_transport = { 2402 .name = "rdma", 2403 .module = THIS_MODULE, 2404 .required_opts = NVMF_OPT_TRADDR, 2405 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY | 2406 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO | 2407 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES | 2408 NVMF_OPT_TOS, 2409 .create_ctrl = nvme_rdma_create_ctrl, 2410 }; 2411 2412 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data) 2413 { 2414 struct nvme_rdma_ctrl *ctrl; 2415 struct nvme_rdma_device *ndev; 2416 bool found = false; 2417 2418 mutex_lock(&device_list_mutex); 2419 list_for_each_entry(ndev, &device_list, entry) { 2420 if (ndev->dev == ib_device) { 2421 found = true; 2422 break; 2423 } 2424 } 2425 mutex_unlock(&device_list_mutex); 2426 2427 if (!found) 2428 return; 2429 2430 /* Delete all controllers using this device */ 2431 mutex_lock(&nvme_rdma_ctrl_mutex); 2432 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) { 2433 if (ctrl->device->dev != ib_device) 2434 continue; 2435 nvme_delete_ctrl(&ctrl->ctrl); 2436 } 2437 mutex_unlock(&nvme_rdma_ctrl_mutex); 2438 2439 flush_workqueue(nvme_delete_wq); 2440 } 2441 2442 static struct ib_client nvme_rdma_ib_client = { 2443 .name = "nvme_rdma", 2444 .remove = nvme_rdma_remove_one 2445 }; 2446 2447 static int __init nvme_rdma_init_module(void) 2448 { 2449 int ret; 2450 2451 ret = ib_register_client(&nvme_rdma_ib_client); 2452 if (ret) 2453 return ret; 2454 2455 ret = nvmf_register_transport(&nvme_rdma_transport); 2456 if (ret) 2457 goto err_unreg_client; 2458 2459 return 0; 2460 2461 err_unreg_client: 2462 ib_unregister_client(&nvme_rdma_ib_client); 2463 return ret; 2464 } 2465 2466 static void __exit nvme_rdma_cleanup_module(void) 2467 { 2468 struct nvme_rdma_ctrl *ctrl; 2469 2470 nvmf_unregister_transport(&nvme_rdma_transport); 2471 ib_unregister_client(&nvme_rdma_ib_client); 2472 2473 mutex_lock(&nvme_rdma_ctrl_mutex); 2474 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) 2475 nvme_delete_ctrl(&ctrl->ctrl); 2476 mutex_unlock(&nvme_rdma_ctrl_mutex); 2477 flush_workqueue(nvme_delete_wq); 2478 } 2479 2480 module_init(nvme_rdma_init_module); 2481 module_exit(nvme_rdma_cleanup_module); 2482 2483 MODULE_DESCRIPTION("NVMe host RDMA transport driver"); 2484 MODULE_LICENSE("GPL v2"); 2485 MODULE_ALIAS("nvme-rdma"); 2486