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 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true); 757 758 ret = nvmf_connect_admin_queue(&ctrl->ctrl); 759 if (ret) 760 goto stop_admin_q; 761 762 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags); 763 764 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap); 765 if (ret) 766 goto stop_admin_q; 767 768 nvme_start_keep_alive(&ctrl->ctrl); 769 770 if (ctrl->queue_count > 1) { 771 ret = nvme_rdma_init_io_queues(ctrl); 772 if (ret) 773 goto stop_admin_q; 774 775 ret = nvme_rdma_connect_io_queues(ctrl); 776 if (ret) 777 goto stop_admin_q; 778 } 779 780 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); 781 WARN_ON_ONCE(!changed); 782 ctrl->ctrl.opts->nr_reconnects = 0; 783 784 if (ctrl->queue_count > 1) { 785 nvme_start_queues(&ctrl->ctrl); 786 nvme_queue_scan(&ctrl->ctrl); 787 nvme_queue_async_events(&ctrl->ctrl); 788 } 789 790 dev_info(ctrl->ctrl.device, "Successfully reconnected\n"); 791 792 return; 793 794 stop_admin_q: 795 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q); 796 requeue: 797 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n", 798 ctrl->ctrl.opts->nr_reconnects); 799 nvme_rdma_reconnect_or_remove(ctrl); 800 } 801 802 static void nvme_rdma_error_recovery_work(struct work_struct *work) 803 { 804 struct nvme_rdma_ctrl *ctrl = container_of(work, 805 struct nvme_rdma_ctrl, err_work); 806 int i; 807 808 nvme_stop_keep_alive(&ctrl->ctrl); 809 810 for (i = 0; i < ctrl->queue_count; i++) { 811 clear_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[i].flags); 812 clear_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags); 813 } 814 815 if (ctrl->queue_count > 1) 816 nvme_stop_queues(&ctrl->ctrl); 817 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q); 818 819 /* We must take care of fastfail/requeue all our inflight requests */ 820 if (ctrl->queue_count > 1) 821 blk_mq_tagset_busy_iter(&ctrl->tag_set, 822 nvme_cancel_request, &ctrl->ctrl); 823 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, 824 nvme_cancel_request, &ctrl->ctrl); 825 826 nvme_rdma_reconnect_or_remove(ctrl); 827 } 828 829 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl) 830 { 831 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING)) 832 return; 833 834 queue_work(nvme_rdma_wq, &ctrl->err_work); 835 } 836 837 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc, 838 const char *op) 839 { 840 struct nvme_rdma_queue *queue = cq->cq_context; 841 struct nvme_rdma_ctrl *ctrl = queue->ctrl; 842 843 if (ctrl->ctrl.state == NVME_CTRL_LIVE) 844 dev_info(ctrl->ctrl.device, 845 "%s for CQE 0x%p failed with status %s (%d)\n", 846 op, wc->wr_cqe, 847 ib_wc_status_msg(wc->status), wc->status); 848 nvme_rdma_error_recovery(ctrl); 849 } 850 851 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc) 852 { 853 if (unlikely(wc->status != IB_WC_SUCCESS)) 854 nvme_rdma_wr_error(cq, wc, "MEMREG"); 855 } 856 857 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc) 858 { 859 if (unlikely(wc->status != IB_WC_SUCCESS)) 860 nvme_rdma_wr_error(cq, wc, "LOCAL_INV"); 861 } 862 863 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue, 864 struct nvme_rdma_request *req) 865 { 866 struct ib_send_wr *bad_wr; 867 struct ib_send_wr wr = { 868 .opcode = IB_WR_LOCAL_INV, 869 .next = NULL, 870 .num_sge = 0, 871 .send_flags = 0, 872 .ex.invalidate_rkey = req->mr->rkey, 873 }; 874 875 req->reg_cqe.done = nvme_rdma_inv_rkey_done; 876 wr.wr_cqe = &req->reg_cqe; 877 878 return ib_post_send(queue->qp, &wr, &bad_wr); 879 } 880 881 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue, 882 struct request *rq) 883 { 884 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 885 struct nvme_rdma_ctrl *ctrl = queue->ctrl; 886 struct nvme_rdma_device *dev = queue->device; 887 struct ib_device *ibdev = dev->dev; 888 int res; 889 890 if (!blk_rq_bytes(rq)) 891 return; 892 893 if (req->mr->need_inval) { 894 res = nvme_rdma_inv_rkey(queue, req); 895 if (res < 0) { 896 dev_err(ctrl->ctrl.device, 897 "Queueing INV WR for rkey %#x failed (%d)\n", 898 req->mr->rkey, res); 899 nvme_rdma_error_recovery(queue->ctrl); 900 } 901 } 902 903 ib_dma_unmap_sg(ibdev, req->sg_table.sgl, 904 req->nents, rq_data_dir(rq) == 905 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 906 907 nvme_cleanup_cmd(rq); 908 sg_free_table_chained(&req->sg_table, true); 909 } 910 911 static int nvme_rdma_set_sg_null(struct nvme_command *c) 912 { 913 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; 914 915 sg->addr = 0; 916 put_unaligned_le24(0, sg->length); 917 put_unaligned_le32(0, sg->key); 918 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; 919 return 0; 920 } 921 922 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue, 923 struct nvme_rdma_request *req, struct nvme_command *c) 924 { 925 struct nvme_sgl_desc *sg = &c->common.dptr.sgl; 926 927 req->sge[1].addr = sg_dma_address(req->sg_table.sgl); 928 req->sge[1].length = sg_dma_len(req->sg_table.sgl); 929 req->sge[1].lkey = queue->device->pd->local_dma_lkey; 930 931 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff); 932 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl)); 933 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET; 934 935 req->inline_data = true; 936 req->num_sge++; 937 return 0; 938 } 939 940 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue, 941 struct nvme_rdma_request *req, struct nvme_command *c) 942 { 943 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; 944 945 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl)); 946 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length); 947 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key); 948 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4; 949 return 0; 950 } 951 952 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue, 953 struct nvme_rdma_request *req, struct nvme_command *c, 954 int count) 955 { 956 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl; 957 int nr; 958 959 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE); 960 if (nr < count) { 961 if (nr < 0) 962 return nr; 963 return -EINVAL; 964 } 965 966 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey)); 967 968 req->reg_cqe.done = nvme_rdma_memreg_done; 969 memset(&req->reg_wr, 0, sizeof(req->reg_wr)); 970 req->reg_wr.wr.opcode = IB_WR_REG_MR; 971 req->reg_wr.wr.wr_cqe = &req->reg_cqe; 972 req->reg_wr.wr.num_sge = 0; 973 req->reg_wr.mr = req->mr; 974 req->reg_wr.key = req->mr->rkey; 975 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE | 976 IB_ACCESS_REMOTE_READ | 977 IB_ACCESS_REMOTE_WRITE; 978 979 req->mr->need_inval = true; 980 981 sg->addr = cpu_to_le64(req->mr->iova); 982 put_unaligned_le24(req->mr->length, sg->length); 983 put_unaligned_le32(req->mr->rkey, sg->key); 984 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) | 985 NVME_SGL_FMT_INVALIDATE; 986 987 return 0; 988 } 989 990 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue, 991 struct request *rq, struct nvme_command *c) 992 { 993 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 994 struct nvme_rdma_device *dev = queue->device; 995 struct ib_device *ibdev = dev->dev; 996 int count, ret; 997 998 req->num_sge = 1; 999 req->inline_data = false; 1000 req->mr->need_inval = false; 1001 1002 c->common.flags |= NVME_CMD_SGL_METABUF; 1003 1004 if (!blk_rq_bytes(rq)) 1005 return nvme_rdma_set_sg_null(c); 1006 1007 req->sg_table.sgl = req->first_sgl; 1008 ret = sg_alloc_table_chained(&req->sg_table, 1009 blk_rq_nr_phys_segments(rq), req->sg_table.sgl); 1010 if (ret) 1011 return -ENOMEM; 1012 1013 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl); 1014 1015 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents, 1016 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 1017 if (unlikely(count <= 0)) { 1018 sg_free_table_chained(&req->sg_table, true); 1019 return -EIO; 1020 } 1021 1022 if (count == 1) { 1023 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) && 1024 blk_rq_payload_bytes(rq) <= 1025 nvme_rdma_inline_data_size(queue)) 1026 return nvme_rdma_map_sg_inline(queue, req, c); 1027 1028 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) 1029 return nvme_rdma_map_sg_single(queue, req, c); 1030 } 1031 1032 return nvme_rdma_map_sg_fr(queue, req, c, count); 1033 } 1034 1035 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc) 1036 { 1037 if (unlikely(wc->status != IB_WC_SUCCESS)) 1038 nvme_rdma_wr_error(cq, wc, "SEND"); 1039 } 1040 1041 static inline int nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue) 1042 { 1043 int sig_limit; 1044 1045 /* 1046 * We signal completion every queue depth/2 and also handle the 1047 * degenerated case of a device with queue_depth=1, where we 1048 * would need to signal every message. 1049 */ 1050 sig_limit = max(queue->queue_size / 2, 1); 1051 return (++queue->sig_count % sig_limit) == 0; 1052 } 1053 1054 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue, 1055 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge, 1056 struct ib_send_wr *first, bool flush) 1057 { 1058 struct ib_send_wr wr, *bad_wr; 1059 int ret; 1060 1061 sge->addr = qe->dma; 1062 sge->length = sizeof(struct nvme_command), 1063 sge->lkey = queue->device->pd->local_dma_lkey; 1064 1065 qe->cqe.done = nvme_rdma_send_done; 1066 1067 wr.next = NULL; 1068 wr.wr_cqe = &qe->cqe; 1069 wr.sg_list = sge; 1070 wr.num_sge = num_sge; 1071 wr.opcode = IB_WR_SEND; 1072 wr.send_flags = 0; 1073 1074 /* 1075 * Unsignalled send completions are another giant desaster in the 1076 * IB Verbs spec: If we don't regularly post signalled sends 1077 * the send queue will fill up and only a QP reset will rescue us. 1078 * Would have been way to obvious to handle this in hardware or 1079 * at least the RDMA stack.. 1080 * 1081 * Always signal the flushes. The magic request used for the flush 1082 * sequencer is not allocated in our driver's tagset and it's 1083 * triggered to be freed by blk_cleanup_queue(). So we need to 1084 * always mark it as signaled to ensure that the "wr_cqe", which is 1085 * embedded in request's payload, is not freed when __ib_process_cq() 1086 * calls wr_cqe->done(). 1087 */ 1088 if (nvme_rdma_queue_sig_limit(queue) || flush) 1089 wr.send_flags |= IB_SEND_SIGNALED; 1090 1091 if (first) 1092 first->next = ≀ 1093 else 1094 first = ≀ 1095 1096 ret = ib_post_send(queue->qp, first, &bad_wr); 1097 if (ret) { 1098 dev_err(queue->ctrl->ctrl.device, 1099 "%s failed with error code %d\n", __func__, ret); 1100 } 1101 return ret; 1102 } 1103 1104 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue, 1105 struct nvme_rdma_qe *qe) 1106 { 1107 struct ib_recv_wr wr, *bad_wr; 1108 struct ib_sge list; 1109 int ret; 1110 1111 list.addr = qe->dma; 1112 list.length = sizeof(struct nvme_completion); 1113 list.lkey = queue->device->pd->local_dma_lkey; 1114 1115 qe->cqe.done = nvme_rdma_recv_done; 1116 1117 wr.next = NULL; 1118 wr.wr_cqe = &qe->cqe; 1119 wr.sg_list = &list; 1120 wr.num_sge = 1; 1121 1122 ret = ib_post_recv(queue->qp, &wr, &bad_wr); 1123 if (ret) { 1124 dev_err(queue->ctrl->ctrl.device, 1125 "%s failed with error code %d\n", __func__, ret); 1126 } 1127 return ret; 1128 } 1129 1130 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue) 1131 { 1132 u32 queue_idx = nvme_rdma_queue_idx(queue); 1133 1134 if (queue_idx == 0) 1135 return queue->ctrl->admin_tag_set.tags[queue_idx]; 1136 return queue->ctrl->tag_set.tags[queue_idx - 1]; 1137 } 1138 1139 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx) 1140 { 1141 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg); 1142 struct nvme_rdma_queue *queue = &ctrl->queues[0]; 1143 struct ib_device *dev = queue->device->dev; 1144 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe; 1145 struct nvme_command *cmd = sqe->data; 1146 struct ib_sge sge; 1147 int ret; 1148 1149 if (WARN_ON_ONCE(aer_idx != 0)) 1150 return; 1151 1152 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE); 1153 1154 memset(cmd, 0, sizeof(*cmd)); 1155 cmd->common.opcode = nvme_admin_async_event; 1156 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH; 1157 cmd->common.flags |= NVME_CMD_SGL_METABUF; 1158 nvme_rdma_set_sg_null(cmd); 1159 1160 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd), 1161 DMA_TO_DEVICE); 1162 1163 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false); 1164 WARN_ON_ONCE(ret); 1165 } 1166 1167 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue, 1168 struct nvme_completion *cqe, struct ib_wc *wc, int tag) 1169 { 1170 struct request *rq; 1171 struct nvme_rdma_request *req; 1172 int ret = 0; 1173 1174 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id); 1175 if (!rq) { 1176 dev_err(queue->ctrl->ctrl.device, 1177 "tag 0x%x on QP %#x not found\n", 1178 cqe->command_id, queue->qp->qp_num); 1179 nvme_rdma_error_recovery(queue->ctrl); 1180 return ret; 1181 } 1182 req = blk_mq_rq_to_pdu(rq); 1183 1184 if (rq->tag == tag) 1185 ret = 1; 1186 1187 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) && 1188 wc->ex.invalidate_rkey == req->mr->rkey) 1189 req->mr->need_inval = false; 1190 1191 nvme_end_request(rq, cqe->status, cqe->result); 1192 return ret; 1193 } 1194 1195 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag) 1196 { 1197 struct nvme_rdma_qe *qe = 1198 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe); 1199 struct nvme_rdma_queue *queue = cq->cq_context; 1200 struct ib_device *ibdev = queue->device->dev; 1201 struct nvme_completion *cqe = qe->data; 1202 const size_t len = sizeof(struct nvme_completion); 1203 int ret = 0; 1204 1205 if (unlikely(wc->status != IB_WC_SUCCESS)) { 1206 nvme_rdma_wr_error(cq, wc, "RECV"); 1207 return 0; 1208 } 1209 1210 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE); 1211 /* 1212 * AEN requests are special as they don't time out and can 1213 * survive any kind of queue freeze and often don't respond to 1214 * aborts. We don't even bother to allocate a struct request 1215 * for them but rather special case them here. 1216 */ 1217 if (unlikely(nvme_rdma_queue_idx(queue) == 0 && 1218 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH)) 1219 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status, 1220 &cqe->result); 1221 else 1222 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag); 1223 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE); 1224 1225 nvme_rdma_post_recv(queue, qe); 1226 return ret; 1227 } 1228 1229 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc) 1230 { 1231 __nvme_rdma_recv_done(cq, wc, -1); 1232 } 1233 1234 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue) 1235 { 1236 int ret, i; 1237 1238 for (i = 0; i < queue->queue_size; i++) { 1239 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]); 1240 if (ret) 1241 goto out_destroy_queue_ib; 1242 } 1243 1244 return 0; 1245 1246 out_destroy_queue_ib: 1247 nvme_rdma_destroy_queue_ib(queue); 1248 return ret; 1249 } 1250 1251 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue, 1252 struct rdma_cm_event *ev) 1253 { 1254 struct rdma_cm_id *cm_id = queue->cm_id; 1255 int status = ev->status; 1256 const char *rej_msg; 1257 const struct nvme_rdma_cm_rej *rej_data; 1258 u8 rej_data_len; 1259 1260 rej_msg = rdma_reject_msg(cm_id, status); 1261 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len); 1262 1263 if (rej_data && rej_data_len >= sizeof(u16)) { 1264 u16 sts = le16_to_cpu(rej_data->sts); 1265 1266 dev_err(queue->ctrl->ctrl.device, 1267 "Connect rejected: status %d (%s) nvme status %d (%s).\n", 1268 status, rej_msg, sts, nvme_rdma_cm_msg(sts)); 1269 } else { 1270 dev_err(queue->ctrl->ctrl.device, 1271 "Connect rejected: status %d (%s).\n", status, rej_msg); 1272 } 1273 1274 return -ECONNRESET; 1275 } 1276 1277 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue) 1278 { 1279 struct nvme_rdma_device *dev; 1280 int ret; 1281 1282 dev = nvme_rdma_find_get_device(queue->cm_id); 1283 if (!dev) { 1284 dev_err(queue->cm_id->device->dev.parent, 1285 "no client data found!\n"); 1286 return -ECONNREFUSED; 1287 } 1288 1289 ret = nvme_rdma_create_queue_ib(queue, dev); 1290 if (ret) { 1291 nvme_rdma_dev_put(dev); 1292 goto out; 1293 } 1294 1295 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS); 1296 if (ret) { 1297 dev_err(queue->ctrl->ctrl.device, 1298 "rdma_resolve_route failed (%d).\n", 1299 queue->cm_error); 1300 goto out_destroy_queue; 1301 } 1302 1303 return 0; 1304 1305 out_destroy_queue: 1306 nvme_rdma_destroy_queue_ib(queue); 1307 out: 1308 return ret; 1309 } 1310 1311 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue) 1312 { 1313 struct nvme_rdma_ctrl *ctrl = queue->ctrl; 1314 struct rdma_conn_param param = { }; 1315 struct nvme_rdma_cm_req priv = { }; 1316 int ret; 1317 1318 param.qp_num = queue->qp->qp_num; 1319 param.flow_control = 1; 1320 1321 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom; 1322 /* maximum retry count */ 1323 param.retry_count = 7; 1324 param.rnr_retry_count = 7; 1325 param.private_data = &priv; 1326 param.private_data_len = sizeof(priv); 1327 1328 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); 1329 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue)); 1330 /* 1331 * set the admin queue depth to the minimum size 1332 * specified by the Fabrics standard. 1333 */ 1334 if (priv.qid == 0) { 1335 priv.hrqsize = cpu_to_le16(NVMF_AQ_DEPTH); 1336 priv.hsqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1); 1337 } else { 1338 /* 1339 * current interpretation of the fabrics spec 1340 * is at minimum you make hrqsize sqsize+1, or a 1341 * 1's based representation of sqsize. 1342 */ 1343 priv.hrqsize = cpu_to_le16(queue->queue_size); 1344 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize); 1345 } 1346 1347 ret = rdma_connect(queue->cm_id, ¶m); 1348 if (ret) { 1349 dev_err(ctrl->ctrl.device, 1350 "rdma_connect failed (%d).\n", ret); 1351 goto out_destroy_queue_ib; 1352 } 1353 1354 return 0; 1355 1356 out_destroy_queue_ib: 1357 nvme_rdma_destroy_queue_ib(queue); 1358 return ret; 1359 } 1360 1361 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id, 1362 struct rdma_cm_event *ev) 1363 { 1364 struct nvme_rdma_queue *queue = cm_id->context; 1365 int cm_error = 0; 1366 1367 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n", 1368 rdma_event_msg(ev->event), ev->event, 1369 ev->status, cm_id); 1370 1371 switch (ev->event) { 1372 case RDMA_CM_EVENT_ADDR_RESOLVED: 1373 cm_error = nvme_rdma_addr_resolved(queue); 1374 break; 1375 case RDMA_CM_EVENT_ROUTE_RESOLVED: 1376 cm_error = nvme_rdma_route_resolved(queue); 1377 break; 1378 case RDMA_CM_EVENT_ESTABLISHED: 1379 queue->cm_error = nvme_rdma_conn_established(queue); 1380 /* complete cm_done regardless of success/failure */ 1381 complete(&queue->cm_done); 1382 return 0; 1383 case RDMA_CM_EVENT_REJECTED: 1384 cm_error = nvme_rdma_conn_rejected(queue, ev); 1385 break; 1386 case RDMA_CM_EVENT_ADDR_ERROR: 1387 case RDMA_CM_EVENT_ROUTE_ERROR: 1388 case RDMA_CM_EVENT_CONNECT_ERROR: 1389 case RDMA_CM_EVENT_UNREACHABLE: 1390 dev_dbg(queue->ctrl->ctrl.device, 1391 "CM error event %d\n", ev->event); 1392 cm_error = -ECONNRESET; 1393 break; 1394 case RDMA_CM_EVENT_DISCONNECTED: 1395 case RDMA_CM_EVENT_ADDR_CHANGE: 1396 case RDMA_CM_EVENT_TIMEWAIT_EXIT: 1397 dev_dbg(queue->ctrl->ctrl.device, 1398 "disconnect received - connection closed\n"); 1399 nvme_rdma_error_recovery(queue->ctrl); 1400 break; 1401 case RDMA_CM_EVENT_DEVICE_REMOVAL: 1402 /* device removal is handled via the ib_client API */ 1403 break; 1404 default: 1405 dev_err(queue->ctrl->ctrl.device, 1406 "Unexpected RDMA CM event (%d)\n", ev->event); 1407 nvme_rdma_error_recovery(queue->ctrl); 1408 break; 1409 } 1410 1411 if (cm_error) { 1412 queue->cm_error = cm_error; 1413 complete(&queue->cm_done); 1414 } 1415 1416 return 0; 1417 } 1418 1419 static enum blk_eh_timer_return 1420 nvme_rdma_timeout(struct request *rq, bool reserved) 1421 { 1422 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1423 1424 /* queue error recovery */ 1425 nvme_rdma_error_recovery(req->queue->ctrl); 1426 1427 /* fail with DNR on cmd timeout */ 1428 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR; 1429 1430 return BLK_EH_HANDLED; 1431 } 1432 1433 /* 1434 * We cannot accept any other command until the Connect command has completed. 1435 */ 1436 static inline bool nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue, 1437 struct request *rq) 1438 { 1439 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) { 1440 struct nvme_command *cmd = nvme_req(rq)->cmd; 1441 1442 if (!blk_rq_is_passthrough(rq) || 1443 cmd->common.opcode != nvme_fabrics_command || 1444 cmd->fabrics.fctype != nvme_fabrics_type_connect) 1445 return false; 1446 } 1447 1448 return true; 1449 } 1450 1451 static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx, 1452 const struct blk_mq_queue_data *bd) 1453 { 1454 struct nvme_ns *ns = hctx->queue->queuedata; 1455 struct nvme_rdma_queue *queue = hctx->driver_data; 1456 struct request *rq = bd->rq; 1457 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1458 struct nvme_rdma_qe *sqe = &req->sqe; 1459 struct nvme_command *c = sqe->data; 1460 bool flush = false; 1461 struct ib_device *dev; 1462 int ret; 1463 1464 WARN_ON_ONCE(rq->tag < 0); 1465 1466 if (!nvme_rdma_queue_is_ready(queue, rq)) 1467 return BLK_MQ_RQ_QUEUE_BUSY; 1468 1469 dev = queue->device->dev; 1470 ib_dma_sync_single_for_cpu(dev, sqe->dma, 1471 sizeof(struct nvme_command), DMA_TO_DEVICE); 1472 1473 ret = nvme_setup_cmd(ns, rq, c); 1474 if (ret != BLK_MQ_RQ_QUEUE_OK) 1475 return ret; 1476 1477 blk_mq_start_request(rq); 1478 1479 ret = nvme_rdma_map_data(queue, rq, c); 1480 if (ret < 0) { 1481 dev_err(queue->ctrl->ctrl.device, 1482 "Failed to map data (%d)\n", ret); 1483 nvme_cleanup_cmd(rq); 1484 goto err; 1485 } 1486 1487 ib_dma_sync_single_for_device(dev, sqe->dma, 1488 sizeof(struct nvme_command), DMA_TO_DEVICE); 1489 1490 if (req_op(rq) == REQ_OP_FLUSH) 1491 flush = true; 1492 ret = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge, 1493 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush); 1494 if (ret) { 1495 nvme_rdma_unmap_data(queue, rq); 1496 goto err; 1497 } 1498 1499 return BLK_MQ_RQ_QUEUE_OK; 1500 err: 1501 return (ret == -ENOMEM || ret == -EAGAIN) ? 1502 BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR; 1503 } 1504 1505 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag) 1506 { 1507 struct nvme_rdma_queue *queue = hctx->driver_data; 1508 struct ib_cq *cq = queue->ib_cq; 1509 struct ib_wc wc; 1510 int found = 0; 1511 1512 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); 1513 while (ib_poll_cq(cq, 1, &wc) > 0) { 1514 struct ib_cqe *cqe = wc.wr_cqe; 1515 1516 if (cqe) { 1517 if (cqe->done == nvme_rdma_recv_done) 1518 found |= __nvme_rdma_recv_done(cq, &wc, tag); 1519 else 1520 cqe->done(cq, &wc); 1521 } 1522 } 1523 1524 return found; 1525 } 1526 1527 static void nvme_rdma_complete_rq(struct request *rq) 1528 { 1529 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq); 1530 1531 nvme_rdma_unmap_data(req->queue, rq); 1532 nvme_complete_rq(rq); 1533 } 1534 1535 static const struct blk_mq_ops nvme_rdma_mq_ops = { 1536 .queue_rq = nvme_rdma_queue_rq, 1537 .complete = nvme_rdma_complete_rq, 1538 .init_request = nvme_rdma_init_request, 1539 .exit_request = nvme_rdma_exit_request, 1540 .reinit_request = nvme_rdma_reinit_request, 1541 .init_hctx = nvme_rdma_init_hctx, 1542 .poll = nvme_rdma_poll, 1543 .timeout = nvme_rdma_timeout, 1544 }; 1545 1546 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = { 1547 .queue_rq = nvme_rdma_queue_rq, 1548 .complete = nvme_rdma_complete_rq, 1549 .init_request = nvme_rdma_init_admin_request, 1550 .exit_request = nvme_rdma_exit_admin_request, 1551 .reinit_request = nvme_rdma_reinit_request, 1552 .init_hctx = nvme_rdma_init_admin_hctx, 1553 .timeout = nvme_rdma_timeout, 1554 }; 1555 1556 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl) 1557 { 1558 int error; 1559 1560 error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH); 1561 if (error) 1562 return error; 1563 1564 ctrl->device = ctrl->queues[0].device; 1565 1566 /* 1567 * We need a reference on the device as long as the tag_set is alive, 1568 * as the MRs in the request structures need a valid ib_device. 1569 */ 1570 error = -EINVAL; 1571 if (!nvme_rdma_dev_get(ctrl->device)) 1572 goto out_free_queue; 1573 1574 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS, 1575 ctrl->device->dev->attrs.max_fast_reg_page_list_len); 1576 1577 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set)); 1578 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops; 1579 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH; 1580 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */ 1581 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE; 1582 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) + 1583 SG_CHUNK_SIZE * sizeof(struct scatterlist); 1584 ctrl->admin_tag_set.driver_data = ctrl; 1585 ctrl->admin_tag_set.nr_hw_queues = 1; 1586 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT; 1587 1588 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set); 1589 if (error) 1590 goto out_put_dev; 1591 1592 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set); 1593 if (IS_ERR(ctrl->ctrl.admin_q)) { 1594 error = PTR_ERR(ctrl->ctrl.admin_q); 1595 goto out_free_tagset; 1596 } 1597 1598 error = nvmf_connect_admin_queue(&ctrl->ctrl); 1599 if (error) 1600 goto out_cleanup_queue; 1601 1602 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags); 1603 1604 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap); 1605 if (error) { 1606 dev_err(ctrl->ctrl.device, 1607 "prop_get NVME_REG_CAP failed\n"); 1608 goto out_cleanup_queue; 1609 } 1610 1611 ctrl->ctrl.sqsize = 1612 min_t(int, NVME_CAP_MQES(ctrl->cap), ctrl->ctrl.sqsize); 1613 1614 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap); 1615 if (error) 1616 goto out_cleanup_queue; 1617 1618 ctrl->ctrl.max_hw_sectors = 1619 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9); 1620 1621 error = nvme_init_identify(&ctrl->ctrl); 1622 if (error) 1623 goto out_cleanup_queue; 1624 1625 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev, 1626 &ctrl->async_event_sqe, sizeof(struct nvme_command), 1627 DMA_TO_DEVICE); 1628 if (error) 1629 goto out_cleanup_queue; 1630 1631 nvme_start_keep_alive(&ctrl->ctrl); 1632 1633 return 0; 1634 1635 out_cleanup_queue: 1636 blk_cleanup_queue(ctrl->ctrl.admin_q); 1637 out_free_tagset: 1638 /* disconnect and drain the queue before freeing the tagset */ 1639 nvme_rdma_stop_queue(&ctrl->queues[0]); 1640 blk_mq_free_tag_set(&ctrl->admin_tag_set); 1641 out_put_dev: 1642 nvme_rdma_dev_put(ctrl->device); 1643 out_free_queue: 1644 nvme_rdma_free_queue(&ctrl->queues[0]); 1645 return error; 1646 } 1647 1648 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl) 1649 { 1650 nvme_stop_keep_alive(&ctrl->ctrl); 1651 cancel_work_sync(&ctrl->err_work); 1652 cancel_delayed_work_sync(&ctrl->reconnect_work); 1653 1654 if (ctrl->queue_count > 1) { 1655 nvme_stop_queues(&ctrl->ctrl); 1656 blk_mq_tagset_busy_iter(&ctrl->tag_set, 1657 nvme_cancel_request, &ctrl->ctrl); 1658 nvme_rdma_free_io_queues(ctrl); 1659 } 1660 1661 if (test_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[0].flags)) 1662 nvme_shutdown_ctrl(&ctrl->ctrl); 1663 1664 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q); 1665 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, 1666 nvme_cancel_request, &ctrl->ctrl); 1667 nvme_rdma_destroy_admin_queue(ctrl); 1668 } 1669 1670 static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown) 1671 { 1672 nvme_uninit_ctrl(&ctrl->ctrl); 1673 if (shutdown) 1674 nvme_rdma_shutdown_ctrl(ctrl); 1675 1676 if (ctrl->ctrl.tagset) { 1677 blk_cleanup_queue(ctrl->ctrl.connect_q); 1678 blk_mq_free_tag_set(&ctrl->tag_set); 1679 nvme_rdma_dev_put(ctrl->device); 1680 } 1681 1682 nvme_put_ctrl(&ctrl->ctrl); 1683 } 1684 1685 static void nvme_rdma_del_ctrl_work(struct work_struct *work) 1686 { 1687 struct nvme_rdma_ctrl *ctrl = container_of(work, 1688 struct nvme_rdma_ctrl, delete_work); 1689 1690 __nvme_rdma_remove_ctrl(ctrl, true); 1691 } 1692 1693 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl) 1694 { 1695 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING)) 1696 return -EBUSY; 1697 1698 if (!queue_work(nvme_rdma_wq, &ctrl->delete_work)) 1699 return -EBUSY; 1700 1701 return 0; 1702 } 1703 1704 static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl) 1705 { 1706 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); 1707 int ret = 0; 1708 1709 /* 1710 * Keep a reference until all work is flushed since 1711 * __nvme_rdma_del_ctrl can free the ctrl mem 1712 */ 1713 if (!kref_get_unless_zero(&ctrl->ctrl.kref)) 1714 return -EBUSY; 1715 ret = __nvme_rdma_del_ctrl(ctrl); 1716 if (!ret) 1717 flush_work(&ctrl->delete_work); 1718 nvme_put_ctrl(&ctrl->ctrl); 1719 return ret; 1720 } 1721 1722 static void nvme_rdma_remove_ctrl_work(struct work_struct *work) 1723 { 1724 struct nvme_rdma_ctrl *ctrl = container_of(work, 1725 struct nvme_rdma_ctrl, delete_work); 1726 1727 __nvme_rdma_remove_ctrl(ctrl, false); 1728 } 1729 1730 static void nvme_rdma_reset_ctrl_work(struct work_struct *work) 1731 { 1732 struct nvme_rdma_ctrl *ctrl = container_of(work, 1733 struct nvme_rdma_ctrl, reset_work); 1734 int ret; 1735 bool changed; 1736 1737 nvme_rdma_shutdown_ctrl(ctrl); 1738 1739 ret = nvme_rdma_configure_admin_queue(ctrl); 1740 if (ret) { 1741 /* ctrl is already shutdown, just remove the ctrl */ 1742 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work); 1743 goto del_dead_ctrl; 1744 } 1745 1746 if (ctrl->queue_count > 1) { 1747 ret = blk_mq_reinit_tagset(&ctrl->tag_set); 1748 if (ret) 1749 goto del_dead_ctrl; 1750 1751 ret = nvme_rdma_init_io_queues(ctrl); 1752 if (ret) 1753 goto del_dead_ctrl; 1754 1755 ret = nvme_rdma_connect_io_queues(ctrl); 1756 if (ret) 1757 goto del_dead_ctrl; 1758 } 1759 1760 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); 1761 WARN_ON_ONCE(!changed); 1762 1763 if (ctrl->queue_count > 1) { 1764 nvme_start_queues(&ctrl->ctrl); 1765 nvme_queue_scan(&ctrl->ctrl); 1766 nvme_queue_async_events(&ctrl->ctrl); 1767 } 1768 1769 return; 1770 1771 del_dead_ctrl: 1772 /* Deleting this dead controller... */ 1773 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n"); 1774 WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work)); 1775 } 1776 1777 static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl) 1778 { 1779 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl); 1780 1781 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING)) 1782 return -EBUSY; 1783 1784 if (!queue_work(nvme_rdma_wq, &ctrl->reset_work)) 1785 return -EBUSY; 1786 1787 flush_work(&ctrl->reset_work); 1788 1789 return 0; 1790 } 1791 1792 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = { 1793 .name = "rdma", 1794 .module = THIS_MODULE, 1795 .flags = NVME_F_FABRICS, 1796 .reg_read32 = nvmf_reg_read32, 1797 .reg_read64 = nvmf_reg_read64, 1798 .reg_write32 = nvmf_reg_write32, 1799 .reset_ctrl = nvme_rdma_reset_ctrl, 1800 .free_ctrl = nvme_rdma_free_ctrl, 1801 .submit_async_event = nvme_rdma_submit_async_event, 1802 .delete_ctrl = nvme_rdma_del_ctrl, 1803 .get_subsysnqn = nvmf_get_subsysnqn, 1804 .get_address = nvmf_get_address, 1805 }; 1806 1807 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl) 1808 { 1809 int ret; 1810 1811 ret = nvme_rdma_init_io_queues(ctrl); 1812 if (ret) 1813 return ret; 1814 1815 /* 1816 * We need a reference on the device as long as the tag_set is alive, 1817 * as the MRs in the request structures need a valid ib_device. 1818 */ 1819 ret = -EINVAL; 1820 if (!nvme_rdma_dev_get(ctrl->device)) 1821 goto out_free_io_queues; 1822 1823 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set)); 1824 ctrl->tag_set.ops = &nvme_rdma_mq_ops; 1825 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size; 1826 ctrl->tag_set.reserved_tags = 1; /* fabric connect */ 1827 ctrl->tag_set.numa_node = NUMA_NO_NODE; 1828 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; 1829 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) + 1830 SG_CHUNK_SIZE * sizeof(struct scatterlist); 1831 ctrl->tag_set.driver_data = ctrl; 1832 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1; 1833 ctrl->tag_set.timeout = NVME_IO_TIMEOUT; 1834 1835 ret = blk_mq_alloc_tag_set(&ctrl->tag_set); 1836 if (ret) 1837 goto out_put_dev; 1838 ctrl->ctrl.tagset = &ctrl->tag_set; 1839 1840 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set); 1841 if (IS_ERR(ctrl->ctrl.connect_q)) { 1842 ret = PTR_ERR(ctrl->ctrl.connect_q); 1843 goto out_free_tag_set; 1844 } 1845 1846 ret = nvme_rdma_connect_io_queues(ctrl); 1847 if (ret) 1848 goto out_cleanup_connect_q; 1849 1850 return 0; 1851 1852 out_cleanup_connect_q: 1853 blk_cleanup_queue(ctrl->ctrl.connect_q); 1854 out_free_tag_set: 1855 blk_mq_free_tag_set(&ctrl->tag_set); 1856 out_put_dev: 1857 nvme_rdma_dev_put(ctrl->device); 1858 out_free_io_queues: 1859 nvme_rdma_free_io_queues(ctrl); 1860 return ret; 1861 } 1862 1863 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev, 1864 struct nvmf_ctrl_options *opts) 1865 { 1866 struct nvme_rdma_ctrl *ctrl; 1867 int ret; 1868 bool changed; 1869 char *port; 1870 1871 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); 1872 if (!ctrl) 1873 return ERR_PTR(-ENOMEM); 1874 ctrl->ctrl.opts = opts; 1875 INIT_LIST_HEAD(&ctrl->list); 1876 1877 if (opts->mask & NVMF_OPT_TRSVCID) 1878 port = opts->trsvcid; 1879 else 1880 port = __stringify(NVME_RDMA_IP_PORT); 1881 1882 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, 1883 opts->traddr, port, &ctrl->addr); 1884 if (ret) { 1885 pr_err("malformed address passed: %s:%s\n", opts->traddr, port); 1886 goto out_free_ctrl; 1887 } 1888 1889 if (opts->mask & NVMF_OPT_HOST_TRADDR) { 1890 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, 1891 opts->host_traddr, NULL, &ctrl->src_addr); 1892 if (ret) { 1893 pr_err("malformed src address passed: %s\n", 1894 opts->host_traddr); 1895 goto out_free_ctrl; 1896 } 1897 } 1898 1899 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops, 1900 0 /* no quirks, we're perfect! */); 1901 if (ret) 1902 goto out_free_ctrl; 1903 1904 INIT_DELAYED_WORK(&ctrl->reconnect_work, 1905 nvme_rdma_reconnect_ctrl_work); 1906 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work); 1907 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work); 1908 INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work); 1909 spin_lock_init(&ctrl->lock); 1910 1911 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */ 1912 ctrl->ctrl.sqsize = opts->queue_size - 1; 1913 ctrl->ctrl.kato = opts->kato; 1914 1915 ret = -ENOMEM; 1916 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues), 1917 GFP_KERNEL); 1918 if (!ctrl->queues) 1919 goto out_uninit_ctrl; 1920 1921 ret = nvme_rdma_configure_admin_queue(ctrl); 1922 if (ret) 1923 goto out_kfree_queues; 1924 1925 /* sanity check icdoff */ 1926 if (ctrl->ctrl.icdoff) { 1927 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n"); 1928 goto out_remove_admin_queue; 1929 } 1930 1931 /* sanity check keyed sgls */ 1932 if (!(ctrl->ctrl.sgls & (1 << 20))) { 1933 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n"); 1934 goto out_remove_admin_queue; 1935 } 1936 1937 if (opts->queue_size > ctrl->ctrl.maxcmd) { 1938 /* warn if maxcmd is lower than queue_size */ 1939 dev_warn(ctrl->ctrl.device, 1940 "queue_size %zu > ctrl maxcmd %u, clamping down\n", 1941 opts->queue_size, ctrl->ctrl.maxcmd); 1942 opts->queue_size = ctrl->ctrl.maxcmd; 1943 } 1944 1945 if (opts->queue_size > ctrl->ctrl.sqsize + 1) { 1946 /* warn if sqsize is lower than queue_size */ 1947 dev_warn(ctrl->ctrl.device, 1948 "queue_size %zu > ctrl sqsize %u, clamping down\n", 1949 opts->queue_size, ctrl->ctrl.sqsize + 1); 1950 opts->queue_size = ctrl->ctrl.sqsize + 1; 1951 } 1952 1953 if (opts->nr_io_queues) { 1954 ret = nvme_rdma_create_io_queues(ctrl); 1955 if (ret) 1956 goto out_remove_admin_queue; 1957 } 1958 1959 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE); 1960 WARN_ON_ONCE(!changed); 1961 1962 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n", 1963 ctrl->ctrl.opts->subsysnqn, &ctrl->addr); 1964 1965 kref_get(&ctrl->ctrl.kref); 1966 1967 mutex_lock(&nvme_rdma_ctrl_mutex); 1968 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list); 1969 mutex_unlock(&nvme_rdma_ctrl_mutex); 1970 1971 if (opts->nr_io_queues) { 1972 nvme_queue_scan(&ctrl->ctrl); 1973 nvme_queue_async_events(&ctrl->ctrl); 1974 } 1975 1976 return &ctrl->ctrl; 1977 1978 out_remove_admin_queue: 1979 nvme_stop_keep_alive(&ctrl->ctrl); 1980 nvme_rdma_destroy_admin_queue(ctrl); 1981 out_kfree_queues: 1982 kfree(ctrl->queues); 1983 out_uninit_ctrl: 1984 nvme_uninit_ctrl(&ctrl->ctrl); 1985 nvme_put_ctrl(&ctrl->ctrl); 1986 if (ret > 0) 1987 ret = -EIO; 1988 return ERR_PTR(ret); 1989 out_free_ctrl: 1990 kfree(ctrl); 1991 return ERR_PTR(ret); 1992 } 1993 1994 static struct nvmf_transport_ops nvme_rdma_transport = { 1995 .name = "rdma", 1996 .required_opts = NVMF_OPT_TRADDR, 1997 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY | 1998 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO, 1999 .create_ctrl = nvme_rdma_create_ctrl, 2000 }; 2001 2002 static void nvme_rdma_add_one(struct ib_device *ib_device) 2003 { 2004 } 2005 2006 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data) 2007 { 2008 struct nvme_rdma_ctrl *ctrl; 2009 2010 /* Delete all controllers using this device */ 2011 mutex_lock(&nvme_rdma_ctrl_mutex); 2012 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) { 2013 if (ctrl->device->dev != ib_device) 2014 continue; 2015 dev_info(ctrl->ctrl.device, 2016 "Removing ctrl: NQN \"%s\", addr %pISp\n", 2017 ctrl->ctrl.opts->subsysnqn, &ctrl->addr); 2018 __nvme_rdma_del_ctrl(ctrl); 2019 } 2020 mutex_unlock(&nvme_rdma_ctrl_mutex); 2021 2022 flush_workqueue(nvme_rdma_wq); 2023 } 2024 2025 static struct ib_client nvme_rdma_ib_client = { 2026 .name = "nvme_rdma", 2027 .add = nvme_rdma_add_one, 2028 .remove = nvme_rdma_remove_one 2029 }; 2030 2031 static int __init nvme_rdma_init_module(void) 2032 { 2033 int ret; 2034 2035 nvme_rdma_wq = create_workqueue("nvme_rdma_wq"); 2036 if (!nvme_rdma_wq) 2037 return -ENOMEM; 2038 2039 ret = ib_register_client(&nvme_rdma_ib_client); 2040 if (ret) 2041 goto err_destroy_wq; 2042 2043 ret = nvmf_register_transport(&nvme_rdma_transport); 2044 if (ret) 2045 goto err_unreg_client; 2046 2047 return 0; 2048 2049 err_unreg_client: 2050 ib_unregister_client(&nvme_rdma_ib_client); 2051 err_destroy_wq: 2052 destroy_workqueue(nvme_rdma_wq); 2053 return ret; 2054 } 2055 2056 static void __exit nvme_rdma_cleanup_module(void) 2057 { 2058 nvmf_unregister_transport(&nvme_rdma_transport); 2059 ib_unregister_client(&nvme_rdma_ib_client); 2060 destroy_workqueue(nvme_rdma_wq); 2061 } 2062 2063 module_init(nvme_rdma_init_module); 2064 module_exit(nvme_rdma_cleanup_module); 2065 2066 MODULE_LICENSE("GPL v2"); 2067