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