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