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