1 /* 2 * NVMe over Fabrics RDMA host code. 3 * Copyright (c) 2015-2016 HGST, a Western Digital Company. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms and conditions of the GNU General Public License, 7 * version 2, as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 */ 14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 15 #include <linux/module.h> 16 #include <linux/init.h> 17 #include <linux/slab.h> 18 #include <linux/err.h> 19 #include <linux/string.h> 20 #include <linux/atomic.h> 21 #include <linux/blk-mq.h> 22 #include <linux/types.h> 23 #include <linux/list.h> 24 #include <linux/mutex.h> 25 #include <linux/scatterlist.h> 26 #include <linux/nvme.h> 27 #include <asm/unaligned.h> 28 29 #include <rdma/ib_verbs.h> 30 #include <rdma/rdma_cm.h> 31 #include <linux/nvme-rdma.h> 32 33 #include "nvme.h" 34 #include "fabrics.h" 35 36 37 #define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */ 38 39 #define NVME_RDMA_MAX_SEGMENT_SIZE 0xffffff /* 24-bit SGL field */ 40 41 #define NVME_RDMA_MAX_SEGMENTS 256 42 43 #define NVME_RDMA_MAX_INLINE_SEGMENTS 1 44 45 /* 46 * We handle AEN commands ourselves and don't even let the 47 * block layer know about them. 48 */ 49 #define NVME_RDMA_NR_AEN_COMMANDS 1 50 #define NVME_RDMA_AQ_BLKMQ_DEPTH \ 51 (NVMF_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS) 52 53 struct nvme_rdma_device { 54 struct ib_device *dev; 55 struct ib_pd *pd; 56 struct kref ref; 57 struct list_head entry; 58 }; 59 60 struct nvme_rdma_qe { 61 struct ib_cqe cqe; 62 void *data; 63 u64 dma; 64 }; 65 66 struct nvme_rdma_queue; 67 struct nvme_rdma_request { 68 struct nvme_request req; 69 struct ib_mr *mr; 70 struct nvme_rdma_qe sqe; 71 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS]; 72 u32 num_sge; 73 int nents; 74 bool inline_data; 75 struct ib_reg_wr reg_wr; 76 struct ib_cqe reg_cqe; 77 struct nvme_rdma_queue *queue; 78 struct sg_table sg_table; 79 struct scatterlist first_sgl[]; 80 }; 81 82 enum nvme_rdma_queue_flags { 83 NVME_RDMA_Q_CONNECTED = (1 << 0), 84 NVME_RDMA_IB_QUEUE_ALLOCATED = (1 << 1), 85 NVME_RDMA_Q_DELETING = (1 << 2), 86 NVME_RDMA_Q_LIVE = (1 << 3), 87 }; 88 89 struct nvme_rdma_queue { 90 struct nvme_rdma_qe *rsp_ring; 91 u8 sig_count; 92 int queue_size; 93 size_t cmnd_capsule_len; 94 struct nvme_rdma_ctrl *ctrl; 95 struct nvme_rdma_device *device; 96 struct ib_cq *ib_cq; 97 struct ib_qp *qp; 98 99 unsigned long flags; 100 struct rdma_cm_id *cm_id; 101 int cm_error; 102 struct completion cm_done; 103 }; 104 105 struct nvme_rdma_ctrl { 106 /* read and written in the hot path */ 107 spinlock_t lock; 108 109 /* read only in the hot path */ 110 struct nvme_rdma_queue *queues; 111 u32 queue_count; 112 113 /* other member variables */ 114 struct blk_mq_tag_set tag_set; 115 struct work_struct delete_work; 116 struct work_struct reset_work; 117 struct work_struct err_work; 118 119 struct nvme_rdma_qe async_event_sqe; 120 121 struct delayed_work reconnect_work; 122 123 struct list_head list; 124 125 struct blk_mq_tag_set admin_tag_set; 126 struct nvme_rdma_device *device; 127 128 u64 cap; 129 u32 max_fr_pages; 130 131 struct sockaddr_storage addr; 132 struct sockaddr_storage src_addr; 133 134 struct nvme_ctrl ctrl; 135 }; 136 137 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl) 138 { 139 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl); 140 } 141 142 static LIST_HEAD(device_list); 143 static DEFINE_MUTEX(device_list_mutex); 144 145 static LIST_HEAD(nvme_rdma_ctrl_list); 146 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex); 147 148 static struct workqueue_struct *nvme_rdma_wq; 149 150 /* 151 * Disabling this option makes small I/O goes faster, but is fundamentally 152 * unsafe. With it turned off we will have to register a global rkey that 153 * allows read and write access to all physical memory. 154 */ 155 static bool register_always = true; 156 module_param(register_always, bool, 0444); 157 MODULE_PARM_DESC(register_always, 158 "Use memory registration even for contiguous memory regions"); 159 160 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, 161 struct rdma_cm_event *event); 162 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc); 163 164 /* XXX: really should move to a generic header sooner or later.. */ 165 static inline void put_unaligned_le24(u32 val, u8 *p) 166 { 167 *p++ = val; 168 *p++ = val >> 8; 169 *p++ = val >> 16; 170 } 171 172 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue) 173 { 174 return queue - queue->ctrl->queues; 175 } 176 177 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue) 178 { 179 return queue->cmnd_capsule_len - sizeof(struct nvme_command); 180 } 181 182 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe, 183 size_t capsule_size, enum dma_data_direction dir) 184 { 185 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir); 186 kfree(qe->data); 187 } 188 189 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe, 190 size_t capsule_size, enum dma_data_direction dir) 191 { 192 qe->data = kzalloc(capsule_size, GFP_KERNEL); 193 if (!qe->data) 194 return -ENOMEM; 195 196 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir); 197 if (ib_dma_mapping_error(ibdev, qe->dma)) { 198 kfree(qe->data); 199 return -ENOMEM; 200 } 201 202 return 0; 203 } 204 205 static void nvme_rdma_free_ring(struct ib_device *ibdev, 206 struct nvme_rdma_qe *ring, size_t ib_queue_size, 207 size_t capsule_size, enum dma_data_direction dir) 208 { 209 int i; 210 211 for (i = 0; i < ib_queue_size; i++) 212 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir); 213 kfree(ring); 214 } 215 216 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev, 217 size_t ib_queue_size, size_t capsule_size, 218 enum dma_data_direction dir) 219 { 220 struct nvme_rdma_qe *ring; 221 int i; 222 223 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL); 224 if (!ring) 225 return NULL; 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 wait_for_completion_interruptible_timeout(&queue->cm_done, 249 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1); 250 return queue->cm_error; 251 } 252 253 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor) 254 { 255 struct nvme_rdma_device *dev = queue->device; 256 struct ib_qp_init_attr init_attr; 257 int ret; 258 259 memset(&init_attr, 0, sizeof(init_attr)); 260 init_attr.event_handler = nvme_rdma_qp_event; 261 /* +1 for drain */ 262 init_attr.cap.max_send_wr = factor * queue->queue_size + 1; 263 /* +1 for drain */ 264 init_attr.cap.max_recv_wr = queue->queue_size + 1; 265 init_attr.cap.max_recv_sge = 1; 266 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS; 267 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR; 268 init_attr.qp_type = IB_QPT_RC; 269 init_attr.send_cq = queue->ib_cq; 270 init_attr.recv_cq = queue->ib_cq; 271 272 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr); 273 274 queue->qp = queue->cm_id->qp; 275 return ret; 276 } 277 278 static int nvme_rdma_reinit_request(void *data, struct request *rq) 279 { 280 struct nvme_rdma_ctrl *ctrl = data; 281 struct nvme_rdma_device *dev = ctrl->device; 282 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 283 int ret = 0; 284 285 if (!req->mr->need_inval) 286 goto out; 287 288 ib_dereg_mr(req->mr); 289 290 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG, 291 ctrl->max_fr_pages); 292 if (IS_ERR(req->mr)) { 293 ret = PTR_ERR(req->mr); 294 req->mr = NULL; 295 goto out; 296 } 297 298 req->mr->need_inval = false; 299 300 out: 301 return ret; 302 } 303 304 static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl *ctrl, 305 struct request *rq, unsigned int queue_idx) 306 { 307 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 308 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx]; 309 struct nvme_rdma_device *dev = queue->device; 310 311 if (req->mr) 312 ib_dereg_mr(req->mr); 313 314 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command), 315 DMA_TO_DEVICE); 316 } 317 318 static void nvme_rdma_exit_request(struct blk_mq_tag_set *set, 319 struct request *rq, unsigned int hctx_idx) 320 { 321 return __nvme_rdma_exit_request(set->driver_data, rq, hctx_idx + 1); 322 } 323 324 static void nvme_rdma_exit_admin_request(struct blk_mq_tag_set *set, 325 struct request *rq, unsigned int hctx_idx) 326 { 327 return __nvme_rdma_exit_request(set->driver_data, rq, 0); 328 } 329 330 static int __nvme_rdma_init_request(struct nvme_rdma_ctrl *ctrl, 331 struct request *rq, unsigned int queue_idx) 332 { 333 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 334 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx]; 335 struct nvme_rdma_device *dev = queue->device; 336 struct ib_device *ibdev = dev->dev; 337 int ret; 338 339 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command), 340 DMA_TO_DEVICE); 341 if (ret) 342 return ret; 343 344 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG, 345 ctrl->max_fr_pages); 346 if (IS_ERR(req->mr)) { 347 ret = PTR_ERR(req->mr); 348 goto out_free_qe; 349 } 350 351 req->queue = queue; 352 353 return 0; 354 355 out_free_qe: 356 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command), 357 DMA_TO_DEVICE); 358 return -ENOMEM; 359 } 360 361 static int nvme_rdma_init_request(struct blk_mq_tag_set *set, 362 struct request *rq, unsigned int hctx_idx, 363 unsigned int numa_node) 364 { 365 return __nvme_rdma_init_request(set->driver_data, rq, hctx_idx + 1); 366 } 367 368 static int nvme_rdma_init_admin_request(struct blk_mq_tag_set *set, 369 struct request *rq, unsigned int hctx_idx, 370 unsigned int numa_node) 371 { 372 return __nvme_rdma_init_request(set->driver_data, rq, 0); 373 } 374 375 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, 376 unsigned int hctx_idx) 377 { 378 struct nvme_rdma_ctrl *ctrl = data; 379 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1]; 380 381 BUG_ON(hctx_idx >= ctrl->queue_count); 382 383 hctx->driver_data = queue; 384 return 0; 385 } 386 387 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, 388 unsigned int hctx_idx) 389 { 390 struct nvme_rdma_ctrl *ctrl = data; 391 struct nvme_rdma_queue *queue = &ctrl->queues[0]; 392 393 BUG_ON(hctx_idx != 0); 394 395 hctx->driver_data = queue; 396 return 0; 397 } 398 399 static void nvme_rdma_free_dev(struct kref *ref) 400 { 401 struct nvme_rdma_device *ndev = 402 container_of(ref, struct nvme_rdma_device, ref); 403 404 mutex_lock(&device_list_mutex); 405 list_del(&ndev->entry); 406 mutex_unlock(&device_list_mutex); 407 408 ib_dealloc_pd(ndev->pd); 409 kfree(ndev); 410 } 411 412 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev) 413 { 414 kref_put(&dev->ref, nvme_rdma_free_dev); 415 } 416 417 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev) 418 { 419 return kref_get_unless_zero(&dev->ref); 420 } 421 422 static struct nvme_rdma_device * 423 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id) 424 { 425 struct nvme_rdma_device *ndev; 426 427 mutex_lock(&device_list_mutex); 428 list_for_each_entry(ndev, &device_list, entry) { 429 if (ndev->dev->node_guid == cm_id->device->node_guid && 430 nvme_rdma_dev_get(ndev)) 431 goto out_unlock; 432 } 433 434 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL); 435 if (!ndev) 436 goto out_err; 437 438 ndev->dev = cm_id->device; 439 kref_init(&ndev->ref); 440 441 ndev->pd = ib_alloc_pd(ndev->dev, 442 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY); 443 if (IS_ERR(ndev->pd)) 444 goto out_free_dev; 445 446 if (!(ndev->dev->attrs.device_cap_flags & 447 IB_DEVICE_MEM_MGT_EXTENSIONS)) { 448 dev_err(&ndev->dev->dev, 449 "Memory registrations not supported.\n"); 450 goto out_free_pd; 451 } 452 453 list_add(&ndev->entry, &device_list); 454 out_unlock: 455 mutex_unlock(&device_list_mutex); 456 return ndev; 457 458 out_free_pd: 459 ib_dealloc_pd(ndev->pd); 460 out_free_dev: 461 kfree(ndev); 462 out_err: 463 mutex_unlock(&device_list_mutex); 464 return NULL; 465 } 466 467 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue) 468 { 469 struct nvme_rdma_device *dev; 470 struct ib_device *ibdev; 471 472 if (!test_and_clear_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags)) 473 return; 474 475 dev = queue->device; 476 ibdev = dev->dev; 477 rdma_destroy_qp(queue->cm_id); 478 ib_free_cq(queue->ib_cq); 479 480 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size, 481 sizeof(struct nvme_completion), DMA_FROM_DEVICE); 482 483 nvme_rdma_dev_put(dev); 484 } 485 486 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue, 487 struct nvme_rdma_device *dev) 488 { 489 struct ib_device *ibdev = dev->dev; 490 const int send_wr_factor = 3; /* MR, SEND, INV */ 491 const int cq_factor = send_wr_factor + 1; /* + RECV */ 492 int comp_vector, idx = nvme_rdma_queue_idx(queue); 493 494 int ret; 495 496 queue->device = dev; 497 498 /* 499 * The admin queue is barely used once the controller is live, so don't 500 * bother to spread it out. 501 */ 502 if (idx == 0) 503 comp_vector = 0; 504 else 505 comp_vector = idx % ibdev->num_comp_vectors; 506 507 508 /* +1 for ib_stop_cq */ 509 queue->ib_cq = ib_alloc_cq(dev->dev, queue, 510 cq_factor * queue->queue_size + 1, comp_vector, 511 IB_POLL_SOFTIRQ); 512 if (IS_ERR(queue->ib_cq)) { 513 ret = PTR_ERR(queue->ib_cq); 514 goto out; 515 } 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 set_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags); 528 529 return 0; 530 531 out_destroy_qp: 532 ib_destroy_qp(queue->qp); 533 out_destroy_ib_cq: 534 ib_free_cq(queue->ib_cq); 535 out: 536 return ret; 537 } 538 539 static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl, 540 int idx, size_t queue_size) 541 { 542 struct nvme_rdma_queue *queue; 543 struct sockaddr *src_addr = NULL; 544 int ret; 545 546 queue = &ctrl->queues[idx]; 547 queue->ctrl = ctrl; 548 init_completion(&queue->cm_done); 549 550 if (idx > 0) 551 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16; 552 else 553 queue->cmnd_capsule_len = sizeof(struct nvme_command); 554 555 queue->queue_size = queue_size; 556 557 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue, 558 RDMA_PS_TCP, IB_QPT_RC); 559 if (IS_ERR(queue->cm_id)) { 560 dev_info(ctrl->ctrl.device, 561 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id)); 562 return PTR_ERR(queue->cm_id); 563 } 564 565 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR) 566 src_addr = (struct sockaddr *)&ctrl->src_addr; 567 568 queue->cm_error = -ETIMEDOUT; 569 ret = rdma_resolve_addr(queue->cm_id, src_addr, 570 (struct sockaddr *)&ctrl->addr, 571 NVME_RDMA_CONNECT_TIMEOUT_MS); 572 if (ret) { 573 dev_info(ctrl->ctrl.device, 574 "rdma_resolve_addr failed (%d).\n", ret); 575 goto out_destroy_cm_id; 576 } 577 578 ret = nvme_rdma_wait_for_cm(queue); 579 if (ret) { 580 dev_info(ctrl->ctrl.device, 581 "rdma_resolve_addr wait failed (%d).\n", ret); 582 goto out_destroy_cm_id; 583 } 584 585 clear_bit(NVME_RDMA_Q_DELETING, &queue->flags); 586 set_bit(NVME_RDMA_Q_CONNECTED, &queue->flags); 587 588 return 0; 589 590 out_destroy_cm_id: 591 nvme_rdma_destroy_queue_ib(queue); 592 rdma_destroy_id(queue->cm_id); 593 return ret; 594 } 595 596 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue) 597 { 598 rdma_disconnect(queue->cm_id); 599 ib_drain_qp(queue->qp); 600 } 601 602 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue) 603 { 604 nvme_rdma_destroy_queue_ib(queue); 605 rdma_destroy_id(queue->cm_id); 606 } 607 608 static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue) 609 { 610 if (test_and_set_bit(NVME_RDMA_Q_DELETING, &queue->flags)) 611 return; 612 nvme_rdma_stop_queue(queue); 613 nvme_rdma_free_queue(queue); 614 } 615 616 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl) 617 { 618 int i; 619 620 for (i = 1; i < ctrl->queue_count; i++) 621 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]); 622 } 623 624 static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl) 625 { 626 int i, ret = 0; 627 628 for (i = 1; i < ctrl->queue_count; i++) { 629 ret = nvmf_connect_io_queue(&ctrl->ctrl, i); 630 if (ret) { 631 dev_info(ctrl->ctrl.device, 632 "failed to connect i/o queue: %d\n", ret); 633 goto out_free_queues; 634 } 635 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags); 636 } 637 638 return 0; 639 640 out_free_queues: 641 nvme_rdma_free_io_queues(ctrl); 642 return ret; 643 } 644 645 static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl) 646 { 647 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts; 648 unsigned int nr_io_queues; 649 int i, ret; 650 651 nr_io_queues = min(opts->nr_io_queues, num_online_cpus()); 652 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues); 653 if (ret) 654 return ret; 655 656 ctrl->queue_count = nr_io_queues + 1; 657 if (ctrl->queue_count < 2) 658 return 0; 659 660 dev_info(ctrl->ctrl.device, 661 "creating %d I/O queues.\n", nr_io_queues); 662 663 for (i = 1; i < ctrl->queue_count; i++) { 664 ret = nvme_rdma_init_queue(ctrl, i, 665 ctrl->ctrl.opts->queue_size); 666 if (ret) { 667 dev_info(ctrl->ctrl.device, 668 "failed to initialize i/o queue: %d\n", ret); 669 goto out_free_queues; 670 } 671 } 672 673 return 0; 674 675 out_free_queues: 676 for (i--; i >= 1; i--) 677 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]); 678 679 return ret; 680 } 681 682 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl) 683 { 684 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe, 685 sizeof(struct nvme_command), DMA_TO_DEVICE); 686 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]); 687 blk_cleanup_queue(ctrl->ctrl.admin_q); 688 blk_mq_free_tag_set(&ctrl->admin_tag_set); 689 nvme_rdma_dev_put(ctrl->device); 690 } 691 692 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl) 693 { 694 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); 695 696 if (list_empty(&ctrl->list)) 697 goto free_ctrl; 698 699 mutex_lock(&nvme_rdma_ctrl_mutex); 700 list_del(&ctrl->list); 701 mutex_unlock(&nvme_rdma_ctrl_mutex); 702 703 kfree(ctrl->queues); 704 nvmf_free_options(nctrl->opts); 705 free_ctrl: 706 kfree(ctrl); 707 } 708 709 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl) 710 { 711 /* If we are resetting/deleting then do nothing */ 712 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) { 713 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW || 714 ctrl->ctrl.state == NVME_CTRL_LIVE); 715 return; 716 } 717 718 if (nvmf_should_reconnect(&ctrl->ctrl)) { 719 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n", 720 ctrl->ctrl.opts->reconnect_delay); 721 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work, 722 ctrl->ctrl.opts->reconnect_delay * HZ); 723 } else { 724 dev_info(ctrl->ctrl.device, "Removing controller...\n"); 725 queue_work(nvme_rdma_wq, &ctrl->delete_work); 726 } 727 } 728 729 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work) 730 { 731 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work), 732 struct nvme_rdma_ctrl, reconnect_work); 733 bool changed; 734 int ret; 735 736 ++ctrl->ctrl.opts->nr_reconnects; 737 738 if (ctrl->queue_count > 1) { 739 nvme_rdma_free_io_queues(ctrl); 740 741 ret = blk_mq_reinit_tagset(&ctrl->tag_set); 742 if (ret) 743 goto requeue; 744 } 745 746 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]); 747 748 ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set); 749 if (ret) 750 goto requeue; 751 752 ret = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH); 753 if (ret) 754 goto requeue; 755 756 ret = nvmf_connect_admin_queue(&ctrl->ctrl); 757 if (ret) 758 goto requeue; 759 760 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags); 761 762 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap); 763 if (ret) 764 goto requeue; 765 766 nvme_start_keep_alive(&ctrl->ctrl); 767 768 if (ctrl->queue_count > 1) { 769 ret = nvme_rdma_init_io_queues(ctrl); 770 if (ret) 771 goto requeue; 772 773 ret = nvme_rdma_connect_io_queues(ctrl); 774 if (ret) 775 goto requeue; 776 } 777 778 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); 779 WARN_ON_ONCE(!changed); 780 ctrl->ctrl.opts->nr_reconnects = 0; 781 782 if (ctrl->queue_count > 1) { 783 nvme_queue_scan(&ctrl->ctrl); 784 nvme_queue_async_events(&ctrl->ctrl); 785 } 786 787 dev_info(ctrl->ctrl.device, "Successfully reconnected\n"); 788 789 return; 790 791 requeue: 792 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n", 793 ctrl->ctrl.opts->nr_reconnects); 794 nvme_rdma_reconnect_or_remove(ctrl); 795 } 796 797 static void nvme_rdma_error_recovery_work(struct work_struct *work) 798 { 799 struct nvme_rdma_ctrl *ctrl = container_of(work, 800 struct nvme_rdma_ctrl, err_work); 801 int i; 802 803 nvme_stop_keep_alive(&ctrl->ctrl); 804 805 for (i = 0; i < ctrl->queue_count; i++) { 806 clear_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[i].flags); 807 clear_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags); 808 } 809 810 if (ctrl->queue_count > 1) 811 nvme_stop_queues(&ctrl->ctrl); 812 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q); 813 814 /* We must take care of fastfail/requeue all our inflight requests */ 815 if (ctrl->queue_count > 1) 816 blk_mq_tagset_busy_iter(&ctrl->tag_set, 817 nvme_cancel_request, &ctrl->ctrl); 818 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, 819 nvme_cancel_request, &ctrl->ctrl); 820 821 /* 822 * queues are not a live anymore, so restart the queues to fail fast 823 * new IO 824 */ 825 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true); 826 nvme_start_queues(&ctrl->ctrl); 827 828 nvme_rdma_reconnect_or_remove(ctrl); 829 } 830 831 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl) 832 { 833 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING)) 834 return; 835 836 queue_work(nvme_rdma_wq, &ctrl->err_work); 837 } 838 839 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc, 840 const char *op) 841 { 842 struct nvme_rdma_queue *queue = cq->cq_context; 843 struct nvme_rdma_ctrl *ctrl = queue->ctrl; 844 845 if (ctrl->ctrl.state == NVME_CTRL_LIVE) 846 dev_info(ctrl->ctrl.device, 847 "%s for CQE 0x%p failed with status %s (%d)\n", 848 op, wc->wr_cqe, 849 ib_wc_status_msg(wc->status), wc->status); 850 nvme_rdma_error_recovery(ctrl); 851 } 852 853 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc) 854 { 855 if (unlikely(wc->status != IB_WC_SUCCESS)) 856 nvme_rdma_wr_error(cq, wc, "MEMREG"); 857 } 858 859 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc) 860 { 861 if (unlikely(wc->status != IB_WC_SUCCESS)) 862 nvme_rdma_wr_error(cq, wc, "LOCAL_INV"); 863 } 864 865 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue, 866 struct nvme_rdma_request *req) 867 { 868 struct ib_send_wr *bad_wr; 869 struct ib_send_wr wr = { 870 .opcode = IB_WR_LOCAL_INV, 871 .next = NULL, 872 .num_sge = 0, 873 .send_flags = 0, 874 .ex.invalidate_rkey = req->mr->rkey, 875 }; 876 877 req->reg_cqe.done = nvme_rdma_inv_rkey_done; 878 wr.wr_cqe = &req->reg_cqe; 879 880 return ib_post_send(queue->qp, &wr, &bad_wr); 881 } 882 883 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue, 884 struct request *rq) 885 { 886 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 887 struct nvme_rdma_ctrl *ctrl = queue->ctrl; 888 struct nvme_rdma_device *dev = queue->device; 889 struct ib_device *ibdev = dev->dev; 890 int res; 891 892 if (!blk_rq_bytes(rq)) 893 return; 894 895 if (req->mr->need_inval) { 896 res = nvme_rdma_inv_rkey(queue, req); 897 if (res < 0) { 898 dev_err(ctrl->ctrl.device, 899 "Queueing INV WR for rkey %#x failed (%d)\n", 900 req->mr->rkey, res); 901 nvme_rdma_error_recovery(queue->ctrl); 902 } 903 } 904 905 ib_dma_unmap_sg(ibdev, req->sg_table.sgl, 906 req->nents, rq_data_dir(rq) == 907 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 908 909 nvme_cleanup_cmd(rq); 910 sg_free_table_chained(&req->sg_table, true); 911 } 912 913 static int nvme_rdma_set_sg_null(struct nvme_command *c) 914 { 915 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; 916 917 sg->addr = 0; 918 put_unaligned_le24(0, sg->length); 919 put_unaligned_le32(0, sg->key); 920 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; 921 return 0; 922 } 923 924 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue, 925 struct nvme_rdma_request *req, struct nvme_command *c) 926 { 927 struct nvme_sgl_desc *sg = &c->common.dptr.sgl; 928 929 req->sge[1].addr = sg_dma_address(req->sg_table.sgl); 930 req->sge[1].length = sg_dma_len(req->sg_table.sgl); 931 req->sge[1].lkey = queue->device->pd->local_dma_lkey; 932 933 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff); 934 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl)); 935 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET; 936 937 req->inline_data = true; 938 req->num_sge++; 939 return 0; 940 } 941 942 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue, 943 struct nvme_rdma_request *req, struct nvme_command *c) 944 { 945 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; 946 947 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl)); 948 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length); 949 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key); 950 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; 951 return 0; 952 } 953 954 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue, 955 struct nvme_rdma_request *req, struct nvme_command *c, 956 int count) 957 { 958 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; 959 int nr; 960 961 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE); 962 if (nr < count) { 963 if (nr < 0) 964 return nr; 965 return -EINVAL; 966 } 967 968 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey)); 969 970 req->reg_cqe.done = nvme_rdma_memreg_done; 971 memset(&req->reg_wr, 0, sizeof(req->reg_wr)); 972 req->reg_wr.wr.opcode = IB_WR_REG_MR; 973 req->reg_wr.wr.wr_cqe = &req->reg_cqe; 974 req->reg_wr.wr.num_sge = 0; 975 req->reg_wr.mr = req->mr; 976 req->reg_wr.key = req->mr->rkey; 977 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE | 978 IB_ACCESS_REMOTE_READ | 979 IB_ACCESS_REMOTE_WRITE; 980 981 req->mr->need_inval = true; 982 983 sg->addr = cpu_to_le64(req->mr->iova); 984 put_unaligned_le24(req->mr->length, sg->length); 985 put_unaligned_le32(req->mr->rkey, sg->key); 986 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) | 987 NVME_SGL_FMT_INVALIDATE; 988 989 return 0; 990 } 991 992 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue, 993 struct request *rq, struct nvme_command *c) 994 { 995 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 996 struct nvme_rdma_device *dev = queue->device; 997 struct ib_device *ibdev = dev->dev; 998 int count, ret; 999 1000 req->num_sge = 1; 1001 req->inline_data = false; 1002 req->mr->need_inval = false; 1003 1004 c->common.flags |= NVME_CMD_SGL_METABUF; 1005 1006 if (!blk_rq_bytes(rq)) 1007 return nvme_rdma_set_sg_null(c); 1008 1009 req->sg_table.sgl = req->first_sgl; 1010 ret = sg_alloc_table_chained(&req->sg_table, 1011 blk_rq_nr_phys_segments(rq), req->sg_table.sgl); 1012 if (ret) 1013 return -ENOMEM; 1014 1015 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl); 1016 1017 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents, 1018 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 1019 if (unlikely(count <= 0)) { 1020 sg_free_table_chained(&req->sg_table, true); 1021 return -EIO; 1022 } 1023 1024 if (count == 1) { 1025 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) && 1026 blk_rq_payload_bytes(rq) <= 1027 nvme_rdma_inline_data_size(queue)) 1028 return nvme_rdma_map_sg_inline(queue, req, c); 1029 1030 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) 1031 return nvme_rdma_map_sg_single(queue, req, c); 1032 } 1033 1034 return nvme_rdma_map_sg_fr(queue, req, c, count); 1035 } 1036 1037 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc) 1038 { 1039 if (unlikely(wc->status != IB_WC_SUCCESS)) 1040 nvme_rdma_wr_error(cq, wc, "SEND"); 1041 } 1042 1043 static inline int nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue) 1044 { 1045 int sig_limit; 1046 1047 /* 1048 * We signal completion every queue depth/2 and also handle the 1049 * degenerated case of a device with queue_depth=1, where we 1050 * would need to signal every message. 1051 */ 1052 sig_limit = max(queue->queue_size / 2, 1); 1053 return (++queue->sig_count % sig_limit) == 0; 1054 } 1055 1056 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue, 1057 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge, 1058 struct ib_send_wr *first, bool flush) 1059 { 1060 struct ib_send_wr wr, *bad_wr; 1061 int ret; 1062 1063 sge->addr = qe->dma; 1064 sge->length = sizeof(struct nvme_command), 1065 sge->lkey = queue->device->pd->local_dma_lkey; 1066 1067 qe->cqe.done = nvme_rdma_send_done; 1068 1069 wr.next = NULL; 1070 wr.wr_cqe = &qe->cqe; 1071 wr.sg_list = sge; 1072 wr.num_sge = num_sge; 1073 wr.opcode = IB_WR_SEND; 1074 wr.send_flags = 0; 1075 1076 /* 1077 * Unsignalled send completions are another giant desaster in the 1078 * IB Verbs spec: If we don't regularly post signalled sends 1079 * the send queue will fill up and only a QP reset will rescue us. 1080 * Would have been way to obvious to handle this in hardware or 1081 * at least the RDMA stack.. 1082 * 1083 * Always signal the flushes. The magic request used for the flush 1084 * sequencer is not allocated in our driver's tagset and it's 1085 * triggered to be freed by blk_cleanup_queue(). So we need to 1086 * always mark it as signaled to ensure that the "wr_cqe", which is 1087 * embedded in request's payload, is not freed when __ib_process_cq() 1088 * calls wr_cqe->done(). 1089 */ 1090 if (nvme_rdma_queue_sig_limit(queue) || flush) 1091 wr.send_flags |= IB_SEND_SIGNALED; 1092 1093 if (first) 1094 first->next = ≀ 1095 else 1096 first = ≀ 1097 1098 ret = ib_post_send(queue->qp, first, &bad_wr); 1099 if (ret) { 1100 dev_err(queue->ctrl->ctrl.device, 1101 "%s failed with error code %d\n", __func__, ret); 1102 } 1103 return ret; 1104 } 1105 1106 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue, 1107 struct nvme_rdma_qe *qe) 1108 { 1109 struct ib_recv_wr wr, *bad_wr; 1110 struct ib_sge list; 1111 int ret; 1112 1113 list.addr = qe->dma; 1114 list.length = sizeof(struct nvme_completion); 1115 list.lkey = queue->device->pd->local_dma_lkey; 1116 1117 qe->cqe.done = nvme_rdma_recv_done; 1118 1119 wr.next = NULL; 1120 wr.wr_cqe = &qe->cqe; 1121 wr.sg_list = &list; 1122 wr.num_sge = 1; 1123 1124 ret = ib_post_recv(queue->qp, &wr, &bad_wr); 1125 if (ret) { 1126 dev_err(queue->ctrl->ctrl.device, 1127 "%s failed with error code %d\n", __func__, ret); 1128 } 1129 return ret; 1130 } 1131 1132 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue) 1133 { 1134 u32 queue_idx = nvme_rdma_queue_idx(queue); 1135 1136 if (queue_idx == 0) 1137 return queue->ctrl->admin_tag_set.tags[queue_idx]; 1138 return queue->ctrl->tag_set.tags[queue_idx - 1]; 1139 } 1140 1141 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx) 1142 { 1143 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg); 1144 struct nvme_rdma_queue *queue = &ctrl->queues[0]; 1145 struct ib_device *dev = queue->device->dev; 1146 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe; 1147 struct nvme_command *cmd = sqe->data; 1148 struct ib_sge sge; 1149 int ret; 1150 1151 if (WARN_ON_ONCE(aer_idx != 0)) 1152 return; 1153 1154 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE); 1155 1156 memset(cmd, 0, sizeof(*cmd)); 1157 cmd->common.opcode = nvme_admin_async_event; 1158 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH; 1159 cmd->common.flags |= NVME_CMD_SGL_METABUF; 1160 nvme_rdma_set_sg_null(cmd); 1161 1162 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd), 1163 DMA_TO_DEVICE); 1164 1165 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false); 1166 WARN_ON_ONCE(ret); 1167 } 1168 1169 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue, 1170 struct nvme_completion *cqe, struct ib_wc *wc, int tag) 1171 { 1172 struct request *rq; 1173 struct nvme_rdma_request *req; 1174 int ret = 0; 1175 1176 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id); 1177 if (!rq) { 1178 dev_err(queue->ctrl->ctrl.device, 1179 "tag 0x%x on QP %#x not found\n", 1180 cqe->command_id, queue->qp->qp_num); 1181 nvme_rdma_error_recovery(queue->ctrl); 1182 return ret; 1183 } 1184 req = blk_mq_rq_to_pdu(rq); 1185 1186 if (rq->tag == tag) 1187 ret = 1; 1188 1189 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) && 1190 wc->ex.invalidate_rkey == req->mr->rkey) 1191 req->mr->need_inval = false; 1192 1193 nvme_end_request(rq, cqe->status, cqe->result); 1194 return ret; 1195 } 1196 1197 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag) 1198 { 1199 struct nvme_rdma_qe *qe = 1200 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe); 1201 struct nvme_rdma_queue *queue = cq->cq_context; 1202 struct ib_device *ibdev = queue->device->dev; 1203 struct nvme_completion *cqe = qe->data; 1204 const size_t len = sizeof(struct nvme_completion); 1205 int ret = 0; 1206 1207 if (unlikely(wc->status != IB_WC_SUCCESS)) { 1208 nvme_rdma_wr_error(cq, wc, "RECV"); 1209 return 0; 1210 } 1211 1212 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE); 1213 /* 1214 * AEN requests are special as they don't time out and can 1215 * survive any kind of queue freeze and often don't respond to 1216 * aborts. We don't even bother to allocate a struct request 1217 * for them but rather special case them here. 1218 */ 1219 if (unlikely(nvme_rdma_queue_idx(queue) == 0 && 1220 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH)) 1221 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status, 1222 &cqe->result); 1223 else 1224 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag); 1225 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE); 1226 1227 nvme_rdma_post_recv(queue, qe); 1228 return ret; 1229 } 1230 1231 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc) 1232 { 1233 __nvme_rdma_recv_done(cq, wc, -1); 1234 } 1235 1236 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue) 1237 { 1238 int ret, i; 1239 1240 for (i = 0; i < queue->queue_size; i++) { 1241 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]); 1242 if (ret) 1243 goto out_destroy_queue_ib; 1244 } 1245 1246 return 0; 1247 1248 out_destroy_queue_ib: 1249 nvme_rdma_destroy_queue_ib(queue); 1250 return ret; 1251 } 1252 1253 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue, 1254 struct rdma_cm_event *ev) 1255 { 1256 struct rdma_cm_id *cm_id = queue->cm_id; 1257 int status = ev->status; 1258 const char *rej_msg; 1259 const struct nvme_rdma_cm_rej *rej_data; 1260 u8 rej_data_len; 1261 1262 rej_msg = rdma_reject_msg(cm_id, status); 1263 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len); 1264 1265 if (rej_data && rej_data_len >= sizeof(u16)) { 1266 u16 sts = le16_to_cpu(rej_data->sts); 1267 1268 dev_err(queue->ctrl->ctrl.device, 1269 "Connect rejected: status %d (%s) nvme status %d (%s).\n", 1270 status, rej_msg, sts, nvme_rdma_cm_msg(sts)); 1271 } else { 1272 dev_err(queue->ctrl->ctrl.device, 1273 "Connect rejected: status %d (%s).\n", status, rej_msg); 1274 } 1275 1276 return -ECONNRESET; 1277 } 1278 1279 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue) 1280 { 1281 struct nvme_rdma_device *dev; 1282 int ret; 1283 1284 dev = nvme_rdma_find_get_device(queue->cm_id); 1285 if (!dev) { 1286 dev_err(queue->cm_id->device->dev.parent, 1287 "no client data found!\n"); 1288 return -ECONNREFUSED; 1289 } 1290 1291 ret = nvme_rdma_create_queue_ib(queue, dev); 1292 if (ret) { 1293 nvme_rdma_dev_put(dev); 1294 goto out; 1295 } 1296 1297 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS); 1298 if (ret) { 1299 dev_err(queue->ctrl->ctrl.device, 1300 "rdma_resolve_route failed (%d).\n", 1301 queue->cm_error); 1302 goto out_destroy_queue; 1303 } 1304 1305 return 0; 1306 1307 out_destroy_queue: 1308 nvme_rdma_destroy_queue_ib(queue); 1309 out: 1310 return ret; 1311 } 1312 1313 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue) 1314 { 1315 struct nvme_rdma_ctrl *ctrl = queue->ctrl; 1316 struct rdma_conn_param param = { }; 1317 struct nvme_rdma_cm_req priv = { }; 1318 int ret; 1319 1320 param.qp_num = queue->qp->qp_num; 1321 param.flow_control = 1; 1322 1323 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom; 1324 /* maximum retry count */ 1325 param.retry_count = 7; 1326 param.rnr_retry_count = 7; 1327 param.private_data = &priv; 1328 param.private_data_len = sizeof(priv); 1329 1330 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); 1331 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue)); 1332 /* 1333 * set the admin queue depth to the minimum size 1334 * specified by the Fabrics standard. 1335 */ 1336 if (priv.qid == 0) { 1337 priv.hrqsize = cpu_to_le16(NVMF_AQ_DEPTH); 1338 priv.hsqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1); 1339 } else { 1340 /* 1341 * current interpretation of the fabrics spec 1342 * is at minimum you make hrqsize sqsize+1, or a 1343 * 1's based representation of sqsize. 1344 */ 1345 priv.hrqsize = cpu_to_le16(queue->queue_size); 1346 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize); 1347 } 1348 1349 ret = rdma_connect(queue->cm_id, ¶m); 1350 if (ret) { 1351 dev_err(ctrl->ctrl.device, 1352 "rdma_connect failed (%d).\n", ret); 1353 goto out_destroy_queue_ib; 1354 } 1355 1356 return 0; 1357 1358 out_destroy_queue_ib: 1359 nvme_rdma_destroy_queue_ib(queue); 1360 return ret; 1361 } 1362 1363 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, 1364 struct rdma_cm_event *ev) 1365 { 1366 struct nvme_rdma_queue *queue = cm_id->context; 1367 int cm_error = 0; 1368 1369 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n", 1370 rdma_event_msg(ev->event), ev->event, 1371 ev->status, cm_id); 1372 1373 switch (ev->event) { 1374 case RDMA_CM_EVENT_ADDR_RESOLVED: 1375 cm_error = nvme_rdma_addr_resolved(queue); 1376 break; 1377 case RDMA_CM_EVENT_ROUTE_RESOLVED: 1378 cm_error = nvme_rdma_route_resolved(queue); 1379 break; 1380 case RDMA_CM_EVENT_ESTABLISHED: 1381 queue->cm_error = nvme_rdma_conn_established(queue); 1382 /* complete cm_done regardless of success/failure */ 1383 complete(&queue->cm_done); 1384 return 0; 1385 case RDMA_CM_EVENT_REJECTED: 1386 cm_error = nvme_rdma_conn_rejected(queue, ev); 1387 break; 1388 case RDMA_CM_EVENT_ADDR_ERROR: 1389 case RDMA_CM_EVENT_ROUTE_ERROR: 1390 case RDMA_CM_EVENT_CONNECT_ERROR: 1391 case RDMA_CM_EVENT_UNREACHABLE: 1392 dev_dbg(queue->ctrl->ctrl.device, 1393 "CM error event %d\n", ev->event); 1394 cm_error = -ECONNRESET; 1395 break; 1396 case RDMA_CM_EVENT_DISCONNECTED: 1397 case RDMA_CM_EVENT_ADDR_CHANGE: 1398 case RDMA_CM_EVENT_TIMEWAIT_EXIT: 1399 dev_dbg(queue->ctrl->ctrl.device, 1400 "disconnect received - connection closed\n"); 1401 nvme_rdma_error_recovery(queue->ctrl); 1402 break; 1403 case RDMA_CM_EVENT_DEVICE_REMOVAL: 1404 /* device removal is handled via the ib_client API */ 1405 break; 1406 default: 1407 dev_err(queue->ctrl->ctrl.device, 1408 "Unexpected RDMA CM event (%d)\n", ev->event); 1409 nvme_rdma_error_recovery(queue->ctrl); 1410 break; 1411 } 1412 1413 if (cm_error) { 1414 queue->cm_error = cm_error; 1415 complete(&queue->cm_done); 1416 } 1417 1418 return 0; 1419 } 1420 1421 static enum blk_eh_timer_return 1422 nvme_rdma_timeout(struct request *rq, bool reserved) 1423 { 1424 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1425 1426 /* queue error recovery */ 1427 nvme_rdma_error_recovery(req->queue->ctrl); 1428 1429 /* fail with DNR on cmd timeout */ 1430 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR; 1431 1432 return BLK_EH_HANDLED; 1433 } 1434 1435 /* 1436 * We cannot accept any other command until the Connect command has completed. 1437 */ 1438 static inline int nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue, 1439 struct request *rq) 1440 { 1441 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) { 1442 struct nvme_command *cmd = nvme_req(rq)->cmd; 1443 1444 if (!blk_rq_is_passthrough(rq) || 1445 cmd->common.opcode != nvme_fabrics_command || 1446 cmd->fabrics.fctype != nvme_fabrics_type_connect) { 1447 /* 1448 * reconnecting state means transport disruption, which 1449 * can take a long time and even might fail permanently, 1450 * so we can't let incoming I/O be requeued forever. 1451 * fail it fast to allow upper layers a chance to 1452 * failover. 1453 */ 1454 if (queue->ctrl->ctrl.state == NVME_CTRL_RECONNECTING) 1455 return -EIO; 1456 else 1457 return -EAGAIN; 1458 } 1459 } 1460 1461 return 0; 1462 } 1463 1464 static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx, 1465 const struct blk_mq_queue_data *bd) 1466 { 1467 struct nvme_ns *ns = hctx->queue->queuedata; 1468 struct nvme_rdma_queue *queue = hctx->driver_data; 1469 struct request *rq = bd->rq; 1470 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1471 struct nvme_rdma_qe *sqe = &req->sqe; 1472 struct nvme_command *c = sqe->data; 1473 bool flush = false; 1474 struct ib_device *dev; 1475 int ret; 1476 1477 WARN_ON_ONCE(rq->tag < 0); 1478 1479 ret = nvme_rdma_queue_is_ready(queue, rq); 1480 if (unlikely(ret)) 1481 goto err; 1482 1483 dev = queue->device->dev; 1484 ib_dma_sync_single_for_cpu(dev, sqe->dma, 1485 sizeof(struct nvme_command), DMA_TO_DEVICE); 1486 1487 ret = nvme_setup_cmd(ns, rq, c); 1488 if (ret != BLK_MQ_RQ_QUEUE_OK) 1489 return ret; 1490 1491 blk_mq_start_request(rq); 1492 1493 ret = nvme_rdma_map_data(queue, rq, c); 1494 if (ret < 0) { 1495 dev_err(queue->ctrl->ctrl.device, 1496 "Failed to map data (%d)\n", ret); 1497 nvme_cleanup_cmd(rq); 1498 goto err; 1499 } 1500 1501 ib_dma_sync_single_for_device(dev, sqe->dma, 1502 sizeof(struct nvme_command), DMA_TO_DEVICE); 1503 1504 if (req_op(rq) == REQ_OP_FLUSH) 1505 flush = true; 1506 ret = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge, 1507 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush); 1508 if (ret) { 1509 nvme_rdma_unmap_data(queue, rq); 1510 goto err; 1511 } 1512 1513 return BLK_MQ_RQ_QUEUE_OK; 1514 err: 1515 return (ret == -ENOMEM || ret == -EAGAIN) ? 1516 BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR; 1517 } 1518 1519 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag) 1520 { 1521 struct nvme_rdma_queue *queue = hctx->driver_data; 1522 struct ib_cq *cq = queue->ib_cq; 1523 struct ib_wc wc; 1524 int found = 0; 1525 1526 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); 1527 while (ib_poll_cq(cq, 1, &wc) > 0) { 1528 struct ib_cqe *cqe = wc.wr_cqe; 1529 1530 if (cqe) { 1531 if (cqe->done == nvme_rdma_recv_done) 1532 found |= __nvme_rdma_recv_done(cq, &wc, tag); 1533 else 1534 cqe->done(cq, &wc); 1535 } 1536 } 1537 1538 return found; 1539 } 1540 1541 static void nvme_rdma_complete_rq(struct request *rq) 1542 { 1543 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1544 1545 nvme_rdma_unmap_data(req->queue, rq); 1546 nvme_complete_rq(rq); 1547 } 1548 1549 static const struct blk_mq_ops nvme_rdma_mq_ops = { 1550 .queue_rq = nvme_rdma_queue_rq, 1551 .complete = nvme_rdma_complete_rq, 1552 .init_request = nvme_rdma_init_request, 1553 .exit_request = nvme_rdma_exit_request, 1554 .reinit_request = nvme_rdma_reinit_request, 1555 .init_hctx = nvme_rdma_init_hctx, 1556 .poll = nvme_rdma_poll, 1557 .timeout = nvme_rdma_timeout, 1558 }; 1559 1560 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = { 1561 .queue_rq = nvme_rdma_queue_rq, 1562 .complete = nvme_rdma_complete_rq, 1563 .init_request = nvme_rdma_init_admin_request, 1564 .exit_request = nvme_rdma_exit_admin_request, 1565 .reinit_request = nvme_rdma_reinit_request, 1566 .init_hctx = nvme_rdma_init_admin_hctx, 1567 .timeout = nvme_rdma_timeout, 1568 }; 1569 1570 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl) 1571 { 1572 int error; 1573 1574 error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH); 1575 if (error) 1576 return error; 1577 1578 ctrl->device = ctrl->queues[0].device; 1579 1580 /* 1581 * We need a reference on the device as long as the tag_set is alive, 1582 * as the MRs in the request structures need a valid ib_device. 1583 */ 1584 error = -EINVAL; 1585 if (!nvme_rdma_dev_get(ctrl->device)) 1586 goto out_free_queue; 1587 1588 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS, 1589 ctrl->device->dev->attrs.max_fast_reg_page_list_len); 1590 1591 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set)); 1592 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops; 1593 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH; 1594 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */ 1595 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE; 1596 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) + 1597 SG_CHUNK_SIZE * sizeof(struct scatterlist); 1598 ctrl->admin_tag_set.driver_data = ctrl; 1599 ctrl->admin_tag_set.nr_hw_queues = 1; 1600 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT; 1601 1602 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set); 1603 if (error) 1604 goto out_put_dev; 1605 1606 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set); 1607 if (IS_ERR(ctrl->ctrl.admin_q)) { 1608 error = PTR_ERR(ctrl->ctrl.admin_q); 1609 goto out_free_tagset; 1610 } 1611 1612 error = nvmf_connect_admin_queue(&ctrl->ctrl); 1613 if (error) 1614 goto out_cleanup_queue; 1615 1616 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags); 1617 1618 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap); 1619 if (error) { 1620 dev_err(ctrl->ctrl.device, 1621 "prop_get NVME_REG_CAP failed\n"); 1622 goto out_cleanup_queue; 1623 } 1624 1625 ctrl->ctrl.sqsize = 1626 min_t(int, NVME_CAP_MQES(ctrl->cap), ctrl->ctrl.sqsize); 1627 1628 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap); 1629 if (error) 1630 goto out_cleanup_queue; 1631 1632 ctrl->ctrl.max_hw_sectors = 1633 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9); 1634 1635 error = nvme_init_identify(&ctrl->ctrl); 1636 if (error) 1637 goto out_cleanup_queue; 1638 1639 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev, 1640 &ctrl->async_event_sqe, sizeof(struct nvme_command), 1641 DMA_TO_DEVICE); 1642 if (error) 1643 goto out_cleanup_queue; 1644 1645 nvme_start_keep_alive(&ctrl->ctrl); 1646 1647 return 0; 1648 1649 out_cleanup_queue: 1650 blk_cleanup_queue(ctrl->ctrl.admin_q); 1651 out_free_tagset: 1652 /* disconnect and drain the queue before freeing the tagset */ 1653 nvme_rdma_stop_queue(&ctrl->queues[0]); 1654 blk_mq_free_tag_set(&ctrl->admin_tag_set); 1655 out_put_dev: 1656 nvme_rdma_dev_put(ctrl->device); 1657 out_free_queue: 1658 nvme_rdma_free_queue(&ctrl->queues[0]); 1659 return error; 1660 } 1661 1662 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl) 1663 { 1664 nvme_stop_keep_alive(&ctrl->ctrl); 1665 cancel_work_sync(&ctrl->err_work); 1666 cancel_delayed_work_sync(&ctrl->reconnect_work); 1667 1668 if (ctrl->queue_count > 1) { 1669 nvme_stop_queues(&ctrl->ctrl); 1670 blk_mq_tagset_busy_iter(&ctrl->tag_set, 1671 nvme_cancel_request, &ctrl->ctrl); 1672 nvme_rdma_free_io_queues(ctrl); 1673 } 1674 1675 if (test_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[0].flags)) 1676 nvme_shutdown_ctrl(&ctrl->ctrl); 1677 1678 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q); 1679 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, 1680 nvme_cancel_request, &ctrl->ctrl); 1681 nvme_rdma_destroy_admin_queue(ctrl); 1682 } 1683 1684 static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown) 1685 { 1686 nvme_uninit_ctrl(&ctrl->ctrl); 1687 if (shutdown) 1688 nvme_rdma_shutdown_ctrl(ctrl); 1689 1690 if (ctrl->ctrl.tagset) { 1691 blk_cleanup_queue(ctrl->ctrl.connect_q); 1692 blk_mq_free_tag_set(&ctrl->tag_set); 1693 nvme_rdma_dev_put(ctrl->device); 1694 } 1695 1696 nvme_put_ctrl(&ctrl->ctrl); 1697 } 1698 1699 static void nvme_rdma_del_ctrl_work(struct work_struct *work) 1700 { 1701 struct nvme_rdma_ctrl *ctrl = container_of(work, 1702 struct nvme_rdma_ctrl, delete_work); 1703 1704 __nvme_rdma_remove_ctrl(ctrl, true); 1705 } 1706 1707 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl) 1708 { 1709 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING)) 1710 return -EBUSY; 1711 1712 if (!queue_work(nvme_rdma_wq, &ctrl->delete_work)) 1713 return -EBUSY; 1714 1715 return 0; 1716 } 1717 1718 static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl) 1719 { 1720 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); 1721 int ret = 0; 1722 1723 /* 1724 * Keep a reference until all work is flushed since 1725 * __nvme_rdma_del_ctrl can free the ctrl mem 1726 */ 1727 if (!kref_get_unless_zero(&ctrl->ctrl.kref)) 1728 return -EBUSY; 1729 ret = __nvme_rdma_del_ctrl(ctrl); 1730 if (!ret) 1731 flush_work(&ctrl->delete_work); 1732 nvme_put_ctrl(&ctrl->ctrl); 1733 return ret; 1734 } 1735 1736 static void nvme_rdma_remove_ctrl_work(struct work_struct *work) 1737 { 1738 struct nvme_rdma_ctrl *ctrl = container_of(work, 1739 struct nvme_rdma_ctrl, delete_work); 1740 1741 __nvme_rdma_remove_ctrl(ctrl, false); 1742 } 1743 1744 static void nvme_rdma_reset_ctrl_work(struct work_struct *work) 1745 { 1746 struct nvme_rdma_ctrl *ctrl = container_of(work, 1747 struct nvme_rdma_ctrl, reset_work); 1748 int ret; 1749 bool changed; 1750 1751 nvme_rdma_shutdown_ctrl(ctrl); 1752 1753 ret = nvme_rdma_configure_admin_queue(ctrl); 1754 if (ret) { 1755 /* ctrl is already shutdown, just remove the ctrl */ 1756 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work); 1757 goto del_dead_ctrl; 1758 } 1759 1760 if (ctrl->queue_count > 1) { 1761 ret = blk_mq_reinit_tagset(&ctrl->tag_set); 1762 if (ret) 1763 goto del_dead_ctrl; 1764 1765 ret = nvme_rdma_init_io_queues(ctrl); 1766 if (ret) 1767 goto del_dead_ctrl; 1768 1769 ret = nvme_rdma_connect_io_queues(ctrl); 1770 if (ret) 1771 goto del_dead_ctrl; 1772 } 1773 1774 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); 1775 WARN_ON_ONCE(!changed); 1776 1777 if (ctrl->queue_count > 1) { 1778 nvme_start_queues(&ctrl->ctrl); 1779 nvme_queue_scan(&ctrl->ctrl); 1780 nvme_queue_async_events(&ctrl->ctrl); 1781 } 1782 1783 return; 1784 1785 del_dead_ctrl: 1786 /* Deleting this dead controller... */ 1787 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n"); 1788 WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work)); 1789 } 1790 1791 static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl) 1792 { 1793 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); 1794 1795 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING)) 1796 return -EBUSY; 1797 1798 if (!queue_work(nvme_rdma_wq, &ctrl->reset_work)) 1799 return -EBUSY; 1800 1801 flush_work(&ctrl->reset_work); 1802 1803 return 0; 1804 } 1805 1806 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = { 1807 .name = "rdma", 1808 .module = THIS_MODULE, 1809 .flags = NVME_F_FABRICS, 1810 .reg_read32 = nvmf_reg_read32, 1811 .reg_read64 = nvmf_reg_read64, 1812 .reg_write32 = nvmf_reg_write32, 1813 .reset_ctrl = nvme_rdma_reset_ctrl, 1814 .free_ctrl = nvme_rdma_free_ctrl, 1815 .submit_async_event = nvme_rdma_submit_async_event, 1816 .delete_ctrl = nvme_rdma_del_ctrl, 1817 .get_subsysnqn = nvmf_get_subsysnqn, 1818 .get_address = nvmf_get_address, 1819 }; 1820 1821 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl) 1822 { 1823 int ret; 1824 1825 ret = nvme_rdma_init_io_queues(ctrl); 1826 if (ret) 1827 return ret; 1828 1829 /* 1830 * We need a reference on the device as long as the tag_set is alive, 1831 * as the MRs in the request structures need a valid ib_device. 1832 */ 1833 ret = -EINVAL; 1834 if (!nvme_rdma_dev_get(ctrl->device)) 1835 goto out_free_io_queues; 1836 1837 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set)); 1838 ctrl->tag_set.ops = &nvme_rdma_mq_ops; 1839 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size; 1840 ctrl->tag_set.reserved_tags = 1; /* fabric connect */ 1841 ctrl->tag_set.numa_node = NUMA_NO_NODE; 1842 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; 1843 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) + 1844 SG_CHUNK_SIZE * sizeof(struct scatterlist); 1845 ctrl->tag_set.driver_data = ctrl; 1846 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1; 1847 ctrl->tag_set.timeout = NVME_IO_TIMEOUT; 1848 1849 ret = blk_mq_alloc_tag_set(&ctrl->tag_set); 1850 if (ret) 1851 goto out_put_dev; 1852 ctrl->ctrl.tagset = &ctrl->tag_set; 1853 1854 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set); 1855 if (IS_ERR(ctrl->ctrl.connect_q)) { 1856 ret = PTR_ERR(ctrl->ctrl.connect_q); 1857 goto out_free_tag_set; 1858 } 1859 1860 ret = nvme_rdma_connect_io_queues(ctrl); 1861 if (ret) 1862 goto out_cleanup_connect_q; 1863 1864 return 0; 1865 1866 out_cleanup_connect_q: 1867 blk_cleanup_queue(ctrl->ctrl.connect_q); 1868 out_free_tag_set: 1869 blk_mq_free_tag_set(&ctrl->tag_set); 1870 out_put_dev: 1871 nvme_rdma_dev_put(ctrl->device); 1872 out_free_io_queues: 1873 nvme_rdma_free_io_queues(ctrl); 1874 return ret; 1875 } 1876 1877 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev, 1878 struct nvmf_ctrl_options *opts) 1879 { 1880 struct nvme_rdma_ctrl *ctrl; 1881 int ret; 1882 bool changed; 1883 char *port; 1884 1885 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); 1886 if (!ctrl) 1887 return ERR_PTR(-ENOMEM); 1888 ctrl->ctrl.opts = opts; 1889 INIT_LIST_HEAD(&ctrl->list); 1890 1891 if (opts->mask & NVMF_OPT_TRSVCID) 1892 port = opts->trsvcid; 1893 else 1894 port = __stringify(NVME_RDMA_IP_PORT); 1895 1896 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, 1897 opts->traddr, port, &ctrl->addr); 1898 if (ret) { 1899 pr_err("malformed address passed: %s:%s\n", opts->traddr, port); 1900 goto out_free_ctrl; 1901 } 1902 1903 if (opts->mask & NVMF_OPT_HOST_TRADDR) { 1904 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, 1905 opts->host_traddr, NULL, &ctrl->src_addr); 1906 if (ret) { 1907 pr_err("malformed src address passed: %s\n", 1908 opts->host_traddr); 1909 goto out_free_ctrl; 1910 } 1911 } 1912 1913 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops, 1914 0 /* no quirks, we're perfect! */); 1915 if (ret) 1916 goto out_free_ctrl; 1917 1918 INIT_DELAYED_WORK(&ctrl->reconnect_work, 1919 nvme_rdma_reconnect_ctrl_work); 1920 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work); 1921 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work); 1922 INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work); 1923 spin_lock_init(&ctrl->lock); 1924 1925 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */ 1926 ctrl->ctrl.sqsize = opts->queue_size - 1; 1927 ctrl->ctrl.kato = opts->kato; 1928 1929 ret = -ENOMEM; 1930 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues), 1931 GFP_KERNEL); 1932 if (!ctrl->queues) 1933 goto out_uninit_ctrl; 1934 1935 ret = nvme_rdma_configure_admin_queue(ctrl); 1936 if (ret) 1937 goto out_kfree_queues; 1938 1939 /* sanity check icdoff */ 1940 if (ctrl->ctrl.icdoff) { 1941 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n"); 1942 goto out_remove_admin_queue; 1943 } 1944 1945 /* sanity check keyed sgls */ 1946 if (!(ctrl->ctrl.sgls & (1 << 20))) { 1947 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n"); 1948 goto out_remove_admin_queue; 1949 } 1950 1951 if (opts->queue_size > ctrl->ctrl.maxcmd) { 1952 /* warn if maxcmd is lower than queue_size */ 1953 dev_warn(ctrl->ctrl.device, 1954 "queue_size %zu > ctrl maxcmd %u, clamping down\n", 1955 opts->queue_size, ctrl->ctrl.maxcmd); 1956 opts->queue_size = ctrl->ctrl.maxcmd; 1957 } 1958 1959 if (opts->queue_size > ctrl->ctrl.sqsize + 1) { 1960 /* warn if sqsize is lower than queue_size */ 1961 dev_warn(ctrl->ctrl.device, 1962 "queue_size %zu > ctrl sqsize %u, clamping down\n", 1963 opts->queue_size, ctrl->ctrl.sqsize + 1); 1964 opts->queue_size = ctrl->ctrl.sqsize + 1; 1965 } 1966 1967 if (opts->nr_io_queues) { 1968 ret = nvme_rdma_create_io_queues(ctrl); 1969 if (ret) 1970 goto out_remove_admin_queue; 1971 } 1972 1973 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); 1974 WARN_ON_ONCE(!changed); 1975 1976 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n", 1977 ctrl->ctrl.opts->subsysnqn, &ctrl->addr); 1978 1979 kref_get(&ctrl->ctrl.kref); 1980 1981 mutex_lock(&nvme_rdma_ctrl_mutex); 1982 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list); 1983 mutex_unlock(&nvme_rdma_ctrl_mutex); 1984 1985 if (opts->nr_io_queues) { 1986 nvme_queue_scan(&ctrl->ctrl); 1987 nvme_queue_async_events(&ctrl->ctrl); 1988 } 1989 1990 return &ctrl->ctrl; 1991 1992 out_remove_admin_queue: 1993 nvme_stop_keep_alive(&ctrl->ctrl); 1994 nvme_rdma_destroy_admin_queue(ctrl); 1995 out_kfree_queues: 1996 kfree(ctrl->queues); 1997 out_uninit_ctrl: 1998 nvme_uninit_ctrl(&ctrl->ctrl); 1999 nvme_put_ctrl(&ctrl->ctrl); 2000 if (ret > 0) 2001 ret = -EIO; 2002 return ERR_PTR(ret); 2003 out_free_ctrl: 2004 kfree(ctrl); 2005 return ERR_PTR(ret); 2006 } 2007 2008 static struct nvmf_transport_ops nvme_rdma_transport = { 2009 .name = "rdma", 2010 .required_opts = NVMF_OPT_TRADDR, 2011 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY | 2012 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO, 2013 .create_ctrl = nvme_rdma_create_ctrl, 2014 }; 2015 2016 static void nvme_rdma_add_one(struct ib_device *ib_device) 2017 { 2018 } 2019 2020 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data) 2021 { 2022 struct nvme_rdma_ctrl *ctrl; 2023 2024 /* Delete all controllers using this device */ 2025 mutex_lock(&nvme_rdma_ctrl_mutex); 2026 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) { 2027 if (ctrl->device->dev != ib_device) 2028 continue; 2029 dev_info(ctrl->ctrl.device, 2030 "Removing ctrl: NQN \"%s\", addr %pISp\n", 2031 ctrl->ctrl.opts->subsysnqn, &ctrl->addr); 2032 __nvme_rdma_del_ctrl(ctrl); 2033 } 2034 mutex_unlock(&nvme_rdma_ctrl_mutex); 2035 2036 flush_workqueue(nvme_rdma_wq); 2037 } 2038 2039 static struct ib_client nvme_rdma_ib_client = { 2040 .name = "nvme_rdma", 2041 .add = nvme_rdma_add_one, 2042 .remove = nvme_rdma_remove_one 2043 }; 2044 2045 static int __init nvme_rdma_init_module(void) 2046 { 2047 int ret; 2048 2049 nvme_rdma_wq = create_workqueue("nvme_rdma_wq"); 2050 if (!nvme_rdma_wq) 2051 return -ENOMEM; 2052 2053 ret = ib_register_client(&nvme_rdma_ib_client); 2054 if (ret) 2055 goto err_destroy_wq; 2056 2057 ret = nvmf_register_transport(&nvme_rdma_transport); 2058 if (ret) 2059 goto err_unreg_client; 2060 2061 return 0; 2062 2063 err_unreg_client: 2064 ib_unregister_client(&nvme_rdma_ib_client); 2065 err_destroy_wq: 2066 destroy_workqueue(nvme_rdma_wq); 2067 return ret; 2068 } 2069 2070 static void __exit nvme_rdma_cleanup_module(void) 2071 { 2072 nvmf_unregister_transport(&nvme_rdma_transport); 2073 ib_unregister_client(&nvme_rdma_ib_client); 2074 destroy_workqueue(nvme_rdma_wq); 2075 } 2076 2077 module_init(nvme_rdma_init_module); 2078 module_exit(nvme_rdma_cleanup_module); 2079 2080 MODULE_LICENSE("GPL v2"); 2081