1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * NVMe over Fabrics RDMA target. 4 * Copyright (c) 2015-2016 HGST, a Western Digital Company. 5 */ 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 #include <linux/atomic.h> 8 #include <linux/ctype.h> 9 #include <linux/delay.h> 10 #include <linux/err.h> 11 #include <linux/init.h> 12 #include <linux/module.h> 13 #include <linux/nvme.h> 14 #include <linux/slab.h> 15 #include <linux/string.h> 16 #include <linux/wait.h> 17 #include <linux/inet.h> 18 #include <asm/unaligned.h> 19 20 #include <rdma/ib_verbs.h> 21 #include <rdma/rdma_cm.h> 22 #include <rdma/rw.h> 23 24 #include <linux/nvme-rdma.h> 25 #include "nvmet.h" 26 27 /* 28 * We allow at least 1 page, up to 4 SGEs, and up to 16KB of inline data 29 */ 30 #define NVMET_RDMA_DEFAULT_INLINE_DATA_SIZE PAGE_SIZE 31 #define NVMET_RDMA_MAX_INLINE_SGE 4 32 #define NVMET_RDMA_MAX_INLINE_DATA_SIZE max_t(int, SZ_16K, PAGE_SIZE) 33 34 /* Assume mpsmin == device_page_size == 4KB */ 35 #define NVMET_RDMA_MAX_MDTS 8 36 37 struct nvmet_rdma_cmd { 38 struct ib_sge sge[NVMET_RDMA_MAX_INLINE_SGE + 1]; 39 struct ib_cqe cqe; 40 struct ib_recv_wr wr; 41 struct scatterlist inline_sg[NVMET_RDMA_MAX_INLINE_SGE]; 42 struct nvme_command *nvme_cmd; 43 struct nvmet_rdma_queue *queue; 44 }; 45 46 enum { 47 NVMET_RDMA_REQ_INLINE_DATA = (1 << 0), 48 NVMET_RDMA_REQ_INVALIDATE_RKEY = (1 << 1), 49 }; 50 51 struct nvmet_rdma_rsp { 52 struct ib_sge send_sge; 53 struct ib_cqe send_cqe; 54 struct ib_send_wr send_wr; 55 56 struct nvmet_rdma_cmd *cmd; 57 struct nvmet_rdma_queue *queue; 58 59 struct ib_cqe read_cqe; 60 struct rdma_rw_ctx rw; 61 62 struct nvmet_req req; 63 64 bool allocated; 65 u8 n_rdma; 66 u32 flags; 67 u32 invalidate_rkey; 68 69 struct list_head wait_list; 70 struct list_head free_list; 71 }; 72 73 enum nvmet_rdma_queue_state { 74 NVMET_RDMA_Q_CONNECTING, 75 NVMET_RDMA_Q_LIVE, 76 NVMET_RDMA_Q_DISCONNECTING, 77 }; 78 79 struct nvmet_rdma_queue { 80 struct rdma_cm_id *cm_id; 81 struct nvmet_port *port; 82 struct ib_cq *cq; 83 atomic_t sq_wr_avail; 84 struct nvmet_rdma_device *dev; 85 spinlock_t state_lock; 86 enum nvmet_rdma_queue_state state; 87 struct nvmet_cq nvme_cq; 88 struct nvmet_sq nvme_sq; 89 90 struct nvmet_rdma_rsp *rsps; 91 struct list_head free_rsps; 92 spinlock_t rsps_lock; 93 struct nvmet_rdma_cmd *cmds; 94 95 struct work_struct release_work; 96 struct list_head rsp_wait_list; 97 struct list_head rsp_wr_wait_list; 98 spinlock_t rsp_wr_wait_lock; 99 100 int idx; 101 int host_qid; 102 int recv_queue_size; 103 int send_queue_size; 104 105 struct list_head queue_list; 106 }; 107 108 struct nvmet_rdma_port { 109 struct nvmet_port *nport; 110 struct sockaddr_storage addr; 111 struct rdma_cm_id *cm_id; 112 struct delayed_work repair_work; 113 }; 114 115 struct nvmet_rdma_device { 116 struct ib_device *device; 117 struct ib_pd *pd; 118 struct ib_srq *srq; 119 struct nvmet_rdma_cmd *srq_cmds; 120 size_t srq_size; 121 struct kref ref; 122 struct list_head entry; 123 int inline_data_size; 124 int inline_page_count; 125 }; 126 127 static bool nvmet_rdma_use_srq; 128 module_param_named(use_srq, nvmet_rdma_use_srq, bool, 0444); 129 MODULE_PARM_DESC(use_srq, "Use shared receive queue."); 130 131 static DEFINE_IDA(nvmet_rdma_queue_ida); 132 static LIST_HEAD(nvmet_rdma_queue_list); 133 static DEFINE_MUTEX(nvmet_rdma_queue_mutex); 134 135 static LIST_HEAD(device_list); 136 static DEFINE_MUTEX(device_list_mutex); 137 138 static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp); 139 static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc); 140 static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc); 141 static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc); 142 static void nvmet_rdma_qp_event(struct ib_event *event, void *priv); 143 static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue); 144 static void nvmet_rdma_free_rsp(struct nvmet_rdma_device *ndev, 145 struct nvmet_rdma_rsp *r); 146 static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev, 147 struct nvmet_rdma_rsp *r); 148 149 static const struct nvmet_fabrics_ops nvmet_rdma_ops; 150 151 static int num_pages(int len) 152 { 153 return 1 + (((len - 1) & PAGE_MASK) >> PAGE_SHIFT); 154 } 155 156 /* XXX: really should move to a generic header sooner or later.. */ 157 static inline u32 get_unaligned_le24(const u8 *p) 158 { 159 return (u32)p[0] | (u32)p[1] << 8 | (u32)p[2] << 16; 160 } 161 162 static inline bool nvmet_rdma_need_data_in(struct nvmet_rdma_rsp *rsp) 163 { 164 return nvme_is_write(rsp->req.cmd) && 165 rsp->req.transfer_len && 166 !(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA); 167 } 168 169 static inline bool nvmet_rdma_need_data_out(struct nvmet_rdma_rsp *rsp) 170 { 171 return !nvme_is_write(rsp->req.cmd) && 172 rsp->req.transfer_len && 173 !rsp->req.cqe->status && 174 !(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA); 175 } 176 177 static inline struct nvmet_rdma_rsp * 178 nvmet_rdma_get_rsp(struct nvmet_rdma_queue *queue) 179 { 180 struct nvmet_rdma_rsp *rsp; 181 unsigned long flags; 182 183 spin_lock_irqsave(&queue->rsps_lock, flags); 184 rsp = list_first_entry_or_null(&queue->free_rsps, 185 struct nvmet_rdma_rsp, free_list); 186 if (likely(rsp)) 187 list_del(&rsp->free_list); 188 spin_unlock_irqrestore(&queue->rsps_lock, flags); 189 190 if (unlikely(!rsp)) { 191 int ret; 192 193 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL); 194 if (unlikely(!rsp)) 195 return NULL; 196 ret = nvmet_rdma_alloc_rsp(queue->dev, rsp); 197 if (unlikely(ret)) { 198 kfree(rsp); 199 return NULL; 200 } 201 202 rsp->allocated = true; 203 } 204 205 return rsp; 206 } 207 208 static inline void 209 nvmet_rdma_put_rsp(struct nvmet_rdma_rsp *rsp) 210 { 211 unsigned long flags; 212 213 if (unlikely(rsp->allocated)) { 214 nvmet_rdma_free_rsp(rsp->queue->dev, rsp); 215 kfree(rsp); 216 return; 217 } 218 219 spin_lock_irqsave(&rsp->queue->rsps_lock, flags); 220 list_add_tail(&rsp->free_list, &rsp->queue->free_rsps); 221 spin_unlock_irqrestore(&rsp->queue->rsps_lock, flags); 222 } 223 224 static void nvmet_rdma_free_inline_pages(struct nvmet_rdma_device *ndev, 225 struct nvmet_rdma_cmd *c) 226 { 227 struct scatterlist *sg; 228 struct ib_sge *sge; 229 int i; 230 231 if (!ndev->inline_data_size) 232 return; 233 234 sg = c->inline_sg; 235 sge = &c->sge[1]; 236 237 for (i = 0; i < ndev->inline_page_count; i++, sg++, sge++) { 238 if (sge->length) 239 ib_dma_unmap_page(ndev->device, sge->addr, 240 sge->length, DMA_FROM_DEVICE); 241 if (sg_page(sg)) 242 __free_page(sg_page(sg)); 243 } 244 } 245 246 static int nvmet_rdma_alloc_inline_pages(struct nvmet_rdma_device *ndev, 247 struct nvmet_rdma_cmd *c) 248 { 249 struct scatterlist *sg; 250 struct ib_sge *sge; 251 struct page *pg; 252 int len; 253 int i; 254 255 if (!ndev->inline_data_size) 256 return 0; 257 258 sg = c->inline_sg; 259 sg_init_table(sg, ndev->inline_page_count); 260 sge = &c->sge[1]; 261 len = ndev->inline_data_size; 262 263 for (i = 0; i < ndev->inline_page_count; i++, sg++, sge++) { 264 pg = alloc_page(GFP_KERNEL); 265 if (!pg) 266 goto out_err; 267 sg_assign_page(sg, pg); 268 sge->addr = ib_dma_map_page(ndev->device, 269 pg, 0, PAGE_SIZE, DMA_FROM_DEVICE); 270 if (ib_dma_mapping_error(ndev->device, sge->addr)) 271 goto out_err; 272 sge->length = min_t(int, len, PAGE_SIZE); 273 sge->lkey = ndev->pd->local_dma_lkey; 274 len -= sge->length; 275 } 276 277 return 0; 278 out_err: 279 for (; i >= 0; i--, sg--, sge--) { 280 if (sge->length) 281 ib_dma_unmap_page(ndev->device, sge->addr, 282 sge->length, DMA_FROM_DEVICE); 283 if (sg_page(sg)) 284 __free_page(sg_page(sg)); 285 } 286 return -ENOMEM; 287 } 288 289 static int nvmet_rdma_alloc_cmd(struct nvmet_rdma_device *ndev, 290 struct nvmet_rdma_cmd *c, bool admin) 291 { 292 /* NVMe command / RDMA RECV */ 293 c->nvme_cmd = kmalloc(sizeof(*c->nvme_cmd), GFP_KERNEL); 294 if (!c->nvme_cmd) 295 goto out; 296 297 c->sge[0].addr = ib_dma_map_single(ndev->device, c->nvme_cmd, 298 sizeof(*c->nvme_cmd), DMA_FROM_DEVICE); 299 if (ib_dma_mapping_error(ndev->device, c->sge[0].addr)) 300 goto out_free_cmd; 301 302 c->sge[0].length = sizeof(*c->nvme_cmd); 303 c->sge[0].lkey = ndev->pd->local_dma_lkey; 304 305 if (!admin && nvmet_rdma_alloc_inline_pages(ndev, c)) 306 goto out_unmap_cmd; 307 308 c->cqe.done = nvmet_rdma_recv_done; 309 310 c->wr.wr_cqe = &c->cqe; 311 c->wr.sg_list = c->sge; 312 c->wr.num_sge = admin ? 1 : ndev->inline_page_count + 1; 313 314 return 0; 315 316 out_unmap_cmd: 317 ib_dma_unmap_single(ndev->device, c->sge[0].addr, 318 sizeof(*c->nvme_cmd), DMA_FROM_DEVICE); 319 out_free_cmd: 320 kfree(c->nvme_cmd); 321 322 out: 323 return -ENOMEM; 324 } 325 326 static void nvmet_rdma_free_cmd(struct nvmet_rdma_device *ndev, 327 struct nvmet_rdma_cmd *c, bool admin) 328 { 329 if (!admin) 330 nvmet_rdma_free_inline_pages(ndev, c); 331 ib_dma_unmap_single(ndev->device, c->sge[0].addr, 332 sizeof(*c->nvme_cmd), DMA_FROM_DEVICE); 333 kfree(c->nvme_cmd); 334 } 335 336 static struct nvmet_rdma_cmd * 337 nvmet_rdma_alloc_cmds(struct nvmet_rdma_device *ndev, 338 int nr_cmds, bool admin) 339 { 340 struct nvmet_rdma_cmd *cmds; 341 int ret = -EINVAL, i; 342 343 cmds = kcalloc(nr_cmds, sizeof(struct nvmet_rdma_cmd), GFP_KERNEL); 344 if (!cmds) 345 goto out; 346 347 for (i = 0; i < nr_cmds; i++) { 348 ret = nvmet_rdma_alloc_cmd(ndev, cmds + i, admin); 349 if (ret) 350 goto out_free; 351 } 352 353 return cmds; 354 355 out_free: 356 while (--i >= 0) 357 nvmet_rdma_free_cmd(ndev, cmds + i, admin); 358 kfree(cmds); 359 out: 360 return ERR_PTR(ret); 361 } 362 363 static void nvmet_rdma_free_cmds(struct nvmet_rdma_device *ndev, 364 struct nvmet_rdma_cmd *cmds, int nr_cmds, bool admin) 365 { 366 int i; 367 368 for (i = 0; i < nr_cmds; i++) 369 nvmet_rdma_free_cmd(ndev, cmds + i, admin); 370 kfree(cmds); 371 } 372 373 static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev, 374 struct nvmet_rdma_rsp *r) 375 { 376 /* NVMe CQE / RDMA SEND */ 377 r->req.cqe = kmalloc(sizeof(*r->req.cqe), GFP_KERNEL); 378 if (!r->req.cqe) 379 goto out; 380 381 r->send_sge.addr = ib_dma_map_single(ndev->device, r->req.cqe, 382 sizeof(*r->req.cqe), DMA_TO_DEVICE); 383 if (ib_dma_mapping_error(ndev->device, r->send_sge.addr)) 384 goto out_free_rsp; 385 386 r->req.p2p_client = &ndev->device->dev; 387 r->send_sge.length = sizeof(*r->req.cqe); 388 r->send_sge.lkey = ndev->pd->local_dma_lkey; 389 390 r->send_cqe.done = nvmet_rdma_send_done; 391 392 r->send_wr.wr_cqe = &r->send_cqe; 393 r->send_wr.sg_list = &r->send_sge; 394 r->send_wr.num_sge = 1; 395 r->send_wr.send_flags = IB_SEND_SIGNALED; 396 397 /* Data In / RDMA READ */ 398 r->read_cqe.done = nvmet_rdma_read_data_done; 399 return 0; 400 401 out_free_rsp: 402 kfree(r->req.cqe); 403 out: 404 return -ENOMEM; 405 } 406 407 static void nvmet_rdma_free_rsp(struct nvmet_rdma_device *ndev, 408 struct nvmet_rdma_rsp *r) 409 { 410 ib_dma_unmap_single(ndev->device, r->send_sge.addr, 411 sizeof(*r->req.cqe), DMA_TO_DEVICE); 412 kfree(r->req.cqe); 413 } 414 415 static int 416 nvmet_rdma_alloc_rsps(struct nvmet_rdma_queue *queue) 417 { 418 struct nvmet_rdma_device *ndev = queue->dev; 419 int nr_rsps = queue->recv_queue_size * 2; 420 int ret = -EINVAL, i; 421 422 queue->rsps = kcalloc(nr_rsps, sizeof(struct nvmet_rdma_rsp), 423 GFP_KERNEL); 424 if (!queue->rsps) 425 goto out; 426 427 for (i = 0; i < nr_rsps; i++) { 428 struct nvmet_rdma_rsp *rsp = &queue->rsps[i]; 429 430 ret = nvmet_rdma_alloc_rsp(ndev, rsp); 431 if (ret) 432 goto out_free; 433 434 list_add_tail(&rsp->free_list, &queue->free_rsps); 435 } 436 437 return 0; 438 439 out_free: 440 while (--i >= 0) { 441 struct nvmet_rdma_rsp *rsp = &queue->rsps[i]; 442 443 list_del(&rsp->free_list); 444 nvmet_rdma_free_rsp(ndev, rsp); 445 } 446 kfree(queue->rsps); 447 out: 448 return ret; 449 } 450 451 static void nvmet_rdma_free_rsps(struct nvmet_rdma_queue *queue) 452 { 453 struct nvmet_rdma_device *ndev = queue->dev; 454 int i, nr_rsps = queue->recv_queue_size * 2; 455 456 for (i = 0; i < nr_rsps; i++) { 457 struct nvmet_rdma_rsp *rsp = &queue->rsps[i]; 458 459 list_del(&rsp->free_list); 460 nvmet_rdma_free_rsp(ndev, rsp); 461 } 462 kfree(queue->rsps); 463 } 464 465 static int nvmet_rdma_post_recv(struct nvmet_rdma_device *ndev, 466 struct nvmet_rdma_cmd *cmd) 467 { 468 int ret; 469 470 ib_dma_sync_single_for_device(ndev->device, 471 cmd->sge[0].addr, cmd->sge[0].length, 472 DMA_FROM_DEVICE); 473 474 if (ndev->srq) 475 ret = ib_post_srq_recv(ndev->srq, &cmd->wr, NULL); 476 else 477 ret = ib_post_recv(cmd->queue->cm_id->qp, &cmd->wr, NULL); 478 479 if (unlikely(ret)) 480 pr_err("post_recv cmd failed\n"); 481 482 return ret; 483 } 484 485 static void nvmet_rdma_process_wr_wait_list(struct nvmet_rdma_queue *queue) 486 { 487 spin_lock(&queue->rsp_wr_wait_lock); 488 while (!list_empty(&queue->rsp_wr_wait_list)) { 489 struct nvmet_rdma_rsp *rsp; 490 bool ret; 491 492 rsp = list_entry(queue->rsp_wr_wait_list.next, 493 struct nvmet_rdma_rsp, wait_list); 494 list_del(&rsp->wait_list); 495 496 spin_unlock(&queue->rsp_wr_wait_lock); 497 ret = nvmet_rdma_execute_command(rsp); 498 spin_lock(&queue->rsp_wr_wait_lock); 499 500 if (!ret) { 501 list_add(&rsp->wait_list, &queue->rsp_wr_wait_list); 502 break; 503 } 504 } 505 spin_unlock(&queue->rsp_wr_wait_lock); 506 } 507 508 509 static void nvmet_rdma_release_rsp(struct nvmet_rdma_rsp *rsp) 510 { 511 struct nvmet_rdma_queue *queue = rsp->queue; 512 513 atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail); 514 515 if (rsp->n_rdma) { 516 rdma_rw_ctx_destroy(&rsp->rw, queue->cm_id->qp, 517 queue->cm_id->port_num, rsp->req.sg, 518 rsp->req.sg_cnt, nvmet_data_dir(&rsp->req)); 519 } 520 521 if (rsp->req.sg != rsp->cmd->inline_sg) 522 nvmet_req_free_sgl(&rsp->req); 523 524 if (unlikely(!list_empty_careful(&queue->rsp_wr_wait_list))) 525 nvmet_rdma_process_wr_wait_list(queue); 526 527 nvmet_rdma_put_rsp(rsp); 528 } 529 530 static void nvmet_rdma_error_comp(struct nvmet_rdma_queue *queue) 531 { 532 if (queue->nvme_sq.ctrl) { 533 nvmet_ctrl_fatal_error(queue->nvme_sq.ctrl); 534 } else { 535 /* 536 * we didn't setup the controller yet in case 537 * of admin connect error, just disconnect and 538 * cleanup the queue 539 */ 540 nvmet_rdma_queue_disconnect(queue); 541 } 542 } 543 544 static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc) 545 { 546 struct nvmet_rdma_rsp *rsp = 547 container_of(wc->wr_cqe, struct nvmet_rdma_rsp, send_cqe); 548 struct nvmet_rdma_queue *queue = cq->cq_context; 549 550 nvmet_rdma_release_rsp(rsp); 551 552 if (unlikely(wc->status != IB_WC_SUCCESS && 553 wc->status != IB_WC_WR_FLUSH_ERR)) { 554 pr_err("SEND for CQE 0x%p failed with status %s (%d).\n", 555 wc->wr_cqe, ib_wc_status_msg(wc->status), wc->status); 556 nvmet_rdma_error_comp(queue); 557 } 558 } 559 560 static void nvmet_rdma_queue_response(struct nvmet_req *req) 561 { 562 struct nvmet_rdma_rsp *rsp = 563 container_of(req, struct nvmet_rdma_rsp, req); 564 struct rdma_cm_id *cm_id = rsp->queue->cm_id; 565 struct ib_send_wr *first_wr; 566 567 if (rsp->flags & NVMET_RDMA_REQ_INVALIDATE_RKEY) { 568 rsp->send_wr.opcode = IB_WR_SEND_WITH_INV; 569 rsp->send_wr.ex.invalidate_rkey = rsp->invalidate_rkey; 570 } else { 571 rsp->send_wr.opcode = IB_WR_SEND; 572 } 573 574 if (nvmet_rdma_need_data_out(rsp)) 575 first_wr = rdma_rw_ctx_wrs(&rsp->rw, cm_id->qp, 576 cm_id->port_num, NULL, &rsp->send_wr); 577 else 578 first_wr = &rsp->send_wr; 579 580 nvmet_rdma_post_recv(rsp->queue->dev, rsp->cmd); 581 582 ib_dma_sync_single_for_device(rsp->queue->dev->device, 583 rsp->send_sge.addr, rsp->send_sge.length, 584 DMA_TO_DEVICE); 585 586 if (unlikely(ib_post_send(cm_id->qp, first_wr, NULL))) { 587 pr_err("sending cmd response failed\n"); 588 nvmet_rdma_release_rsp(rsp); 589 } 590 } 591 592 static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc) 593 { 594 struct nvmet_rdma_rsp *rsp = 595 container_of(wc->wr_cqe, struct nvmet_rdma_rsp, read_cqe); 596 struct nvmet_rdma_queue *queue = cq->cq_context; 597 598 WARN_ON(rsp->n_rdma <= 0); 599 atomic_add(rsp->n_rdma, &queue->sq_wr_avail); 600 rdma_rw_ctx_destroy(&rsp->rw, queue->cm_id->qp, 601 queue->cm_id->port_num, rsp->req.sg, 602 rsp->req.sg_cnt, nvmet_data_dir(&rsp->req)); 603 rsp->n_rdma = 0; 604 605 if (unlikely(wc->status != IB_WC_SUCCESS)) { 606 nvmet_req_uninit(&rsp->req); 607 nvmet_rdma_release_rsp(rsp); 608 if (wc->status != IB_WC_WR_FLUSH_ERR) { 609 pr_info("RDMA READ for CQE 0x%p failed with status %s (%d).\n", 610 wc->wr_cqe, ib_wc_status_msg(wc->status), wc->status); 611 nvmet_rdma_error_comp(queue); 612 } 613 return; 614 } 615 616 rsp->req.execute(&rsp->req); 617 } 618 619 static void nvmet_rdma_use_inline_sg(struct nvmet_rdma_rsp *rsp, u32 len, 620 u64 off) 621 { 622 int sg_count = num_pages(len); 623 struct scatterlist *sg; 624 int i; 625 626 sg = rsp->cmd->inline_sg; 627 for (i = 0; i < sg_count; i++, sg++) { 628 if (i < sg_count - 1) 629 sg_unmark_end(sg); 630 else 631 sg_mark_end(sg); 632 sg->offset = off; 633 sg->length = min_t(int, len, PAGE_SIZE - off); 634 len -= sg->length; 635 if (!i) 636 off = 0; 637 } 638 639 rsp->req.sg = rsp->cmd->inline_sg; 640 rsp->req.sg_cnt = sg_count; 641 } 642 643 static u16 nvmet_rdma_map_sgl_inline(struct nvmet_rdma_rsp *rsp) 644 { 645 struct nvme_sgl_desc *sgl = &rsp->req.cmd->common.dptr.sgl; 646 u64 off = le64_to_cpu(sgl->addr); 647 u32 len = le32_to_cpu(sgl->length); 648 649 if (!nvme_is_write(rsp->req.cmd)) { 650 rsp->req.error_loc = 651 offsetof(struct nvme_common_command, opcode); 652 return NVME_SC_INVALID_FIELD | NVME_SC_DNR; 653 } 654 655 if (off + len > rsp->queue->dev->inline_data_size) { 656 pr_err("invalid inline data offset!\n"); 657 return NVME_SC_SGL_INVALID_OFFSET | NVME_SC_DNR; 658 } 659 660 /* no data command? */ 661 if (!len) 662 return 0; 663 664 nvmet_rdma_use_inline_sg(rsp, len, off); 665 rsp->flags |= NVMET_RDMA_REQ_INLINE_DATA; 666 rsp->req.transfer_len += len; 667 return 0; 668 } 669 670 static u16 nvmet_rdma_map_sgl_keyed(struct nvmet_rdma_rsp *rsp, 671 struct nvme_keyed_sgl_desc *sgl, bool invalidate) 672 { 673 struct rdma_cm_id *cm_id = rsp->queue->cm_id; 674 u64 addr = le64_to_cpu(sgl->addr); 675 u32 key = get_unaligned_le32(sgl->key); 676 int ret; 677 678 rsp->req.transfer_len = get_unaligned_le24(sgl->length); 679 680 /* no data command? */ 681 if (!rsp->req.transfer_len) 682 return 0; 683 684 ret = nvmet_req_alloc_sgl(&rsp->req); 685 if (unlikely(ret < 0)) 686 goto error_out; 687 688 ret = rdma_rw_ctx_init(&rsp->rw, cm_id->qp, cm_id->port_num, 689 rsp->req.sg, rsp->req.sg_cnt, 0, addr, key, 690 nvmet_data_dir(&rsp->req)); 691 if (unlikely(ret < 0)) 692 goto error_out; 693 rsp->n_rdma += ret; 694 695 if (invalidate) { 696 rsp->invalidate_rkey = key; 697 rsp->flags |= NVMET_RDMA_REQ_INVALIDATE_RKEY; 698 } 699 700 return 0; 701 702 error_out: 703 rsp->req.transfer_len = 0; 704 return NVME_SC_INTERNAL; 705 } 706 707 static u16 nvmet_rdma_map_sgl(struct nvmet_rdma_rsp *rsp) 708 { 709 struct nvme_keyed_sgl_desc *sgl = &rsp->req.cmd->common.dptr.ksgl; 710 711 switch (sgl->type >> 4) { 712 case NVME_SGL_FMT_DATA_DESC: 713 switch (sgl->type & 0xf) { 714 case NVME_SGL_FMT_OFFSET: 715 return nvmet_rdma_map_sgl_inline(rsp); 716 default: 717 pr_err("invalid SGL subtype: %#x\n", sgl->type); 718 rsp->req.error_loc = 719 offsetof(struct nvme_common_command, dptr); 720 return NVME_SC_INVALID_FIELD | NVME_SC_DNR; 721 } 722 case NVME_KEY_SGL_FMT_DATA_DESC: 723 switch (sgl->type & 0xf) { 724 case NVME_SGL_FMT_ADDRESS | NVME_SGL_FMT_INVALIDATE: 725 return nvmet_rdma_map_sgl_keyed(rsp, sgl, true); 726 case NVME_SGL_FMT_ADDRESS: 727 return nvmet_rdma_map_sgl_keyed(rsp, sgl, false); 728 default: 729 pr_err("invalid SGL subtype: %#x\n", sgl->type); 730 rsp->req.error_loc = 731 offsetof(struct nvme_common_command, dptr); 732 return NVME_SC_INVALID_FIELD | NVME_SC_DNR; 733 } 734 default: 735 pr_err("invalid SGL type: %#x\n", sgl->type); 736 rsp->req.error_loc = offsetof(struct nvme_common_command, dptr); 737 return NVME_SC_SGL_INVALID_TYPE | NVME_SC_DNR; 738 } 739 } 740 741 static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp) 742 { 743 struct nvmet_rdma_queue *queue = rsp->queue; 744 745 if (unlikely(atomic_sub_return(1 + rsp->n_rdma, 746 &queue->sq_wr_avail) < 0)) { 747 pr_debug("IB send queue full (needed %d): queue %u cntlid %u\n", 748 1 + rsp->n_rdma, queue->idx, 749 queue->nvme_sq.ctrl->cntlid); 750 atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail); 751 return false; 752 } 753 754 if (nvmet_rdma_need_data_in(rsp)) { 755 if (rdma_rw_ctx_post(&rsp->rw, queue->cm_id->qp, 756 queue->cm_id->port_num, &rsp->read_cqe, NULL)) 757 nvmet_req_complete(&rsp->req, NVME_SC_DATA_XFER_ERROR); 758 } else { 759 rsp->req.execute(&rsp->req); 760 } 761 762 return true; 763 } 764 765 static void nvmet_rdma_handle_command(struct nvmet_rdma_queue *queue, 766 struct nvmet_rdma_rsp *cmd) 767 { 768 u16 status; 769 770 ib_dma_sync_single_for_cpu(queue->dev->device, 771 cmd->cmd->sge[0].addr, cmd->cmd->sge[0].length, 772 DMA_FROM_DEVICE); 773 ib_dma_sync_single_for_cpu(queue->dev->device, 774 cmd->send_sge.addr, cmd->send_sge.length, 775 DMA_TO_DEVICE); 776 777 if (!nvmet_req_init(&cmd->req, &queue->nvme_cq, 778 &queue->nvme_sq, &nvmet_rdma_ops)) 779 return; 780 781 status = nvmet_rdma_map_sgl(cmd); 782 if (status) 783 goto out_err; 784 785 if (unlikely(!nvmet_rdma_execute_command(cmd))) { 786 spin_lock(&queue->rsp_wr_wait_lock); 787 list_add_tail(&cmd->wait_list, &queue->rsp_wr_wait_list); 788 spin_unlock(&queue->rsp_wr_wait_lock); 789 } 790 791 return; 792 793 out_err: 794 nvmet_req_complete(&cmd->req, status); 795 } 796 797 static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc) 798 { 799 struct nvmet_rdma_cmd *cmd = 800 container_of(wc->wr_cqe, struct nvmet_rdma_cmd, cqe); 801 struct nvmet_rdma_queue *queue = cq->cq_context; 802 struct nvmet_rdma_rsp *rsp; 803 804 if (unlikely(wc->status != IB_WC_SUCCESS)) { 805 if (wc->status != IB_WC_WR_FLUSH_ERR) { 806 pr_err("RECV for CQE 0x%p failed with status %s (%d)\n", 807 wc->wr_cqe, ib_wc_status_msg(wc->status), 808 wc->status); 809 nvmet_rdma_error_comp(queue); 810 } 811 return; 812 } 813 814 if (unlikely(wc->byte_len < sizeof(struct nvme_command))) { 815 pr_err("Ctrl Fatal Error: capsule size less than 64 bytes\n"); 816 nvmet_rdma_error_comp(queue); 817 return; 818 } 819 820 cmd->queue = queue; 821 rsp = nvmet_rdma_get_rsp(queue); 822 if (unlikely(!rsp)) { 823 /* 824 * we get here only under memory pressure, 825 * silently drop and have the host retry 826 * as we can't even fail it. 827 */ 828 nvmet_rdma_post_recv(queue->dev, cmd); 829 return; 830 } 831 rsp->queue = queue; 832 rsp->cmd = cmd; 833 rsp->flags = 0; 834 rsp->req.cmd = cmd->nvme_cmd; 835 rsp->req.port = queue->port; 836 rsp->n_rdma = 0; 837 838 if (unlikely(queue->state != NVMET_RDMA_Q_LIVE)) { 839 unsigned long flags; 840 841 spin_lock_irqsave(&queue->state_lock, flags); 842 if (queue->state == NVMET_RDMA_Q_CONNECTING) 843 list_add_tail(&rsp->wait_list, &queue->rsp_wait_list); 844 else 845 nvmet_rdma_put_rsp(rsp); 846 spin_unlock_irqrestore(&queue->state_lock, flags); 847 return; 848 } 849 850 nvmet_rdma_handle_command(queue, rsp); 851 } 852 853 static void nvmet_rdma_destroy_srq(struct nvmet_rdma_device *ndev) 854 { 855 if (!ndev->srq) 856 return; 857 858 nvmet_rdma_free_cmds(ndev, ndev->srq_cmds, ndev->srq_size, false); 859 ib_destroy_srq(ndev->srq); 860 } 861 862 static int nvmet_rdma_init_srq(struct nvmet_rdma_device *ndev) 863 { 864 struct ib_srq_init_attr srq_attr = { NULL, }; 865 struct ib_srq *srq; 866 size_t srq_size; 867 int ret, i; 868 869 srq_size = 4095; /* XXX: tune */ 870 871 srq_attr.attr.max_wr = srq_size; 872 srq_attr.attr.max_sge = 1 + ndev->inline_page_count; 873 srq_attr.attr.srq_limit = 0; 874 srq_attr.srq_type = IB_SRQT_BASIC; 875 srq = ib_create_srq(ndev->pd, &srq_attr); 876 if (IS_ERR(srq)) { 877 /* 878 * If SRQs aren't supported we just go ahead and use normal 879 * non-shared receive queues. 880 */ 881 pr_info("SRQ requested but not supported.\n"); 882 return 0; 883 } 884 885 ndev->srq_cmds = nvmet_rdma_alloc_cmds(ndev, srq_size, false); 886 if (IS_ERR(ndev->srq_cmds)) { 887 ret = PTR_ERR(ndev->srq_cmds); 888 goto out_destroy_srq; 889 } 890 891 ndev->srq = srq; 892 ndev->srq_size = srq_size; 893 894 for (i = 0; i < srq_size; i++) { 895 ret = nvmet_rdma_post_recv(ndev, &ndev->srq_cmds[i]); 896 if (ret) 897 goto out_free_cmds; 898 } 899 900 return 0; 901 902 out_free_cmds: 903 nvmet_rdma_free_cmds(ndev, ndev->srq_cmds, ndev->srq_size, false); 904 out_destroy_srq: 905 ib_destroy_srq(srq); 906 return ret; 907 } 908 909 static void nvmet_rdma_free_dev(struct kref *ref) 910 { 911 struct nvmet_rdma_device *ndev = 912 container_of(ref, struct nvmet_rdma_device, ref); 913 914 mutex_lock(&device_list_mutex); 915 list_del(&ndev->entry); 916 mutex_unlock(&device_list_mutex); 917 918 nvmet_rdma_destroy_srq(ndev); 919 ib_dealloc_pd(ndev->pd); 920 921 kfree(ndev); 922 } 923 924 static struct nvmet_rdma_device * 925 nvmet_rdma_find_get_device(struct rdma_cm_id *cm_id) 926 { 927 struct nvmet_rdma_port *port = cm_id->context; 928 struct nvmet_port *nport = port->nport; 929 struct nvmet_rdma_device *ndev; 930 int inline_page_count; 931 int inline_sge_count; 932 int ret; 933 934 mutex_lock(&device_list_mutex); 935 list_for_each_entry(ndev, &device_list, entry) { 936 if (ndev->device->node_guid == cm_id->device->node_guid && 937 kref_get_unless_zero(&ndev->ref)) 938 goto out_unlock; 939 } 940 941 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL); 942 if (!ndev) 943 goto out_err; 944 945 inline_page_count = num_pages(nport->inline_data_size); 946 inline_sge_count = max(cm_id->device->attrs.max_sge_rd, 947 cm_id->device->attrs.max_recv_sge) - 1; 948 if (inline_page_count > inline_sge_count) { 949 pr_warn("inline_data_size %d cannot be supported by device %s. Reducing to %lu.\n", 950 nport->inline_data_size, cm_id->device->name, 951 inline_sge_count * PAGE_SIZE); 952 nport->inline_data_size = inline_sge_count * PAGE_SIZE; 953 inline_page_count = inline_sge_count; 954 } 955 ndev->inline_data_size = nport->inline_data_size; 956 ndev->inline_page_count = inline_page_count; 957 ndev->device = cm_id->device; 958 kref_init(&ndev->ref); 959 960 ndev->pd = ib_alloc_pd(ndev->device, 0); 961 if (IS_ERR(ndev->pd)) 962 goto out_free_dev; 963 964 if (nvmet_rdma_use_srq) { 965 ret = nvmet_rdma_init_srq(ndev); 966 if (ret) 967 goto out_free_pd; 968 } 969 970 list_add(&ndev->entry, &device_list); 971 out_unlock: 972 mutex_unlock(&device_list_mutex); 973 pr_debug("added %s.\n", ndev->device->name); 974 return ndev; 975 976 out_free_pd: 977 ib_dealloc_pd(ndev->pd); 978 out_free_dev: 979 kfree(ndev); 980 out_err: 981 mutex_unlock(&device_list_mutex); 982 return NULL; 983 } 984 985 static int nvmet_rdma_create_queue_ib(struct nvmet_rdma_queue *queue) 986 { 987 struct ib_qp_init_attr qp_attr; 988 struct nvmet_rdma_device *ndev = queue->dev; 989 int comp_vector, nr_cqe, ret, i, factor; 990 991 /* 992 * Spread the io queues across completion vectors, 993 * but still keep all admin queues on vector 0. 994 */ 995 comp_vector = !queue->host_qid ? 0 : 996 queue->idx % ndev->device->num_comp_vectors; 997 998 /* 999 * Reserve CQ slots for RECV + RDMA_READ/RDMA_WRITE + RDMA_SEND. 1000 */ 1001 nr_cqe = queue->recv_queue_size + 2 * queue->send_queue_size; 1002 1003 queue->cq = ib_alloc_cq(ndev->device, queue, 1004 nr_cqe + 1, comp_vector, 1005 IB_POLL_WORKQUEUE); 1006 if (IS_ERR(queue->cq)) { 1007 ret = PTR_ERR(queue->cq); 1008 pr_err("failed to create CQ cqe= %d ret= %d\n", 1009 nr_cqe + 1, ret); 1010 goto out; 1011 } 1012 1013 memset(&qp_attr, 0, sizeof(qp_attr)); 1014 qp_attr.qp_context = queue; 1015 qp_attr.event_handler = nvmet_rdma_qp_event; 1016 qp_attr.send_cq = queue->cq; 1017 qp_attr.recv_cq = queue->cq; 1018 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR; 1019 qp_attr.qp_type = IB_QPT_RC; 1020 /* +1 for drain */ 1021 qp_attr.cap.max_send_wr = queue->send_queue_size + 1; 1022 factor = rdma_rw_mr_factor(ndev->device, queue->cm_id->port_num, 1023 1 << NVMET_RDMA_MAX_MDTS); 1024 qp_attr.cap.max_rdma_ctxs = queue->send_queue_size * factor; 1025 qp_attr.cap.max_send_sge = max(ndev->device->attrs.max_sge_rd, 1026 ndev->device->attrs.max_send_sge); 1027 1028 if (ndev->srq) { 1029 qp_attr.srq = ndev->srq; 1030 } else { 1031 /* +1 for drain */ 1032 qp_attr.cap.max_recv_wr = 1 + queue->recv_queue_size; 1033 qp_attr.cap.max_recv_sge = 1 + ndev->inline_page_count; 1034 } 1035 1036 ret = rdma_create_qp(queue->cm_id, ndev->pd, &qp_attr); 1037 if (ret) { 1038 pr_err("failed to create_qp ret= %d\n", ret); 1039 goto err_destroy_cq; 1040 } 1041 1042 atomic_set(&queue->sq_wr_avail, qp_attr.cap.max_send_wr); 1043 1044 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n", 1045 __func__, queue->cq->cqe, qp_attr.cap.max_send_sge, 1046 qp_attr.cap.max_send_wr, queue->cm_id); 1047 1048 if (!ndev->srq) { 1049 for (i = 0; i < queue->recv_queue_size; i++) { 1050 queue->cmds[i].queue = queue; 1051 ret = nvmet_rdma_post_recv(ndev, &queue->cmds[i]); 1052 if (ret) 1053 goto err_destroy_qp; 1054 } 1055 } 1056 1057 out: 1058 return ret; 1059 1060 err_destroy_qp: 1061 rdma_destroy_qp(queue->cm_id); 1062 err_destroy_cq: 1063 ib_free_cq(queue->cq); 1064 goto out; 1065 } 1066 1067 static void nvmet_rdma_destroy_queue_ib(struct nvmet_rdma_queue *queue) 1068 { 1069 struct ib_qp *qp = queue->cm_id->qp; 1070 1071 ib_drain_qp(qp); 1072 rdma_destroy_id(queue->cm_id); 1073 ib_destroy_qp(qp); 1074 ib_free_cq(queue->cq); 1075 } 1076 1077 static void nvmet_rdma_free_queue(struct nvmet_rdma_queue *queue) 1078 { 1079 pr_debug("freeing queue %d\n", queue->idx); 1080 1081 nvmet_sq_destroy(&queue->nvme_sq); 1082 1083 nvmet_rdma_destroy_queue_ib(queue); 1084 if (!queue->dev->srq) { 1085 nvmet_rdma_free_cmds(queue->dev, queue->cmds, 1086 queue->recv_queue_size, 1087 !queue->host_qid); 1088 } 1089 nvmet_rdma_free_rsps(queue); 1090 ida_simple_remove(&nvmet_rdma_queue_ida, queue->idx); 1091 kfree(queue); 1092 } 1093 1094 static void nvmet_rdma_release_queue_work(struct work_struct *w) 1095 { 1096 struct nvmet_rdma_queue *queue = 1097 container_of(w, struct nvmet_rdma_queue, release_work); 1098 struct nvmet_rdma_device *dev = queue->dev; 1099 1100 nvmet_rdma_free_queue(queue); 1101 1102 kref_put(&dev->ref, nvmet_rdma_free_dev); 1103 } 1104 1105 static int 1106 nvmet_rdma_parse_cm_connect_req(struct rdma_conn_param *conn, 1107 struct nvmet_rdma_queue *queue) 1108 { 1109 struct nvme_rdma_cm_req *req; 1110 1111 req = (struct nvme_rdma_cm_req *)conn->private_data; 1112 if (!req || conn->private_data_len == 0) 1113 return NVME_RDMA_CM_INVALID_LEN; 1114 1115 if (le16_to_cpu(req->recfmt) != NVME_RDMA_CM_FMT_1_0) 1116 return NVME_RDMA_CM_INVALID_RECFMT; 1117 1118 queue->host_qid = le16_to_cpu(req->qid); 1119 1120 /* 1121 * req->hsqsize corresponds to our recv queue size plus 1 1122 * req->hrqsize corresponds to our send queue size 1123 */ 1124 queue->recv_queue_size = le16_to_cpu(req->hsqsize) + 1; 1125 queue->send_queue_size = le16_to_cpu(req->hrqsize); 1126 1127 if (!queue->host_qid && queue->recv_queue_size > NVME_AQ_DEPTH) 1128 return NVME_RDMA_CM_INVALID_HSQSIZE; 1129 1130 /* XXX: Should we enforce some kind of max for IO queues? */ 1131 1132 return 0; 1133 } 1134 1135 static int nvmet_rdma_cm_reject(struct rdma_cm_id *cm_id, 1136 enum nvme_rdma_cm_status status) 1137 { 1138 struct nvme_rdma_cm_rej rej; 1139 1140 pr_debug("rejecting connect request: status %d (%s)\n", 1141 status, nvme_rdma_cm_msg(status)); 1142 1143 rej.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); 1144 rej.sts = cpu_to_le16(status); 1145 1146 return rdma_reject(cm_id, (void *)&rej, sizeof(rej)); 1147 } 1148 1149 static struct nvmet_rdma_queue * 1150 nvmet_rdma_alloc_queue(struct nvmet_rdma_device *ndev, 1151 struct rdma_cm_id *cm_id, 1152 struct rdma_cm_event *event) 1153 { 1154 struct nvmet_rdma_queue *queue; 1155 int ret; 1156 1157 queue = kzalloc(sizeof(*queue), GFP_KERNEL); 1158 if (!queue) { 1159 ret = NVME_RDMA_CM_NO_RSC; 1160 goto out_reject; 1161 } 1162 1163 ret = nvmet_sq_init(&queue->nvme_sq); 1164 if (ret) { 1165 ret = NVME_RDMA_CM_NO_RSC; 1166 goto out_free_queue; 1167 } 1168 1169 ret = nvmet_rdma_parse_cm_connect_req(&event->param.conn, queue); 1170 if (ret) 1171 goto out_destroy_sq; 1172 1173 /* 1174 * Schedules the actual release because calling rdma_destroy_id from 1175 * inside a CM callback would trigger a deadlock. (great API design..) 1176 */ 1177 INIT_WORK(&queue->release_work, nvmet_rdma_release_queue_work); 1178 queue->dev = ndev; 1179 queue->cm_id = cm_id; 1180 1181 spin_lock_init(&queue->state_lock); 1182 queue->state = NVMET_RDMA_Q_CONNECTING; 1183 INIT_LIST_HEAD(&queue->rsp_wait_list); 1184 INIT_LIST_HEAD(&queue->rsp_wr_wait_list); 1185 spin_lock_init(&queue->rsp_wr_wait_lock); 1186 INIT_LIST_HEAD(&queue->free_rsps); 1187 spin_lock_init(&queue->rsps_lock); 1188 INIT_LIST_HEAD(&queue->queue_list); 1189 1190 queue->idx = ida_simple_get(&nvmet_rdma_queue_ida, 0, 0, GFP_KERNEL); 1191 if (queue->idx < 0) { 1192 ret = NVME_RDMA_CM_NO_RSC; 1193 goto out_destroy_sq; 1194 } 1195 1196 ret = nvmet_rdma_alloc_rsps(queue); 1197 if (ret) { 1198 ret = NVME_RDMA_CM_NO_RSC; 1199 goto out_ida_remove; 1200 } 1201 1202 if (!ndev->srq) { 1203 queue->cmds = nvmet_rdma_alloc_cmds(ndev, 1204 queue->recv_queue_size, 1205 !queue->host_qid); 1206 if (IS_ERR(queue->cmds)) { 1207 ret = NVME_RDMA_CM_NO_RSC; 1208 goto out_free_responses; 1209 } 1210 } 1211 1212 ret = nvmet_rdma_create_queue_ib(queue); 1213 if (ret) { 1214 pr_err("%s: creating RDMA queue failed (%d).\n", 1215 __func__, ret); 1216 ret = NVME_RDMA_CM_NO_RSC; 1217 goto out_free_cmds; 1218 } 1219 1220 return queue; 1221 1222 out_free_cmds: 1223 if (!ndev->srq) { 1224 nvmet_rdma_free_cmds(queue->dev, queue->cmds, 1225 queue->recv_queue_size, 1226 !queue->host_qid); 1227 } 1228 out_free_responses: 1229 nvmet_rdma_free_rsps(queue); 1230 out_ida_remove: 1231 ida_simple_remove(&nvmet_rdma_queue_ida, queue->idx); 1232 out_destroy_sq: 1233 nvmet_sq_destroy(&queue->nvme_sq); 1234 out_free_queue: 1235 kfree(queue); 1236 out_reject: 1237 nvmet_rdma_cm_reject(cm_id, ret); 1238 return NULL; 1239 } 1240 1241 static void nvmet_rdma_qp_event(struct ib_event *event, void *priv) 1242 { 1243 struct nvmet_rdma_queue *queue = priv; 1244 1245 switch (event->event) { 1246 case IB_EVENT_COMM_EST: 1247 rdma_notify(queue->cm_id, event->event); 1248 break; 1249 default: 1250 pr_err("received IB QP event: %s (%d)\n", 1251 ib_event_msg(event->event), event->event); 1252 break; 1253 } 1254 } 1255 1256 static int nvmet_rdma_cm_accept(struct rdma_cm_id *cm_id, 1257 struct nvmet_rdma_queue *queue, 1258 struct rdma_conn_param *p) 1259 { 1260 struct rdma_conn_param param = { }; 1261 struct nvme_rdma_cm_rep priv = { }; 1262 int ret = -ENOMEM; 1263 1264 param.rnr_retry_count = 7; 1265 param.flow_control = 1; 1266 param.initiator_depth = min_t(u8, p->initiator_depth, 1267 queue->dev->device->attrs.max_qp_init_rd_atom); 1268 param.private_data = &priv; 1269 param.private_data_len = sizeof(priv); 1270 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); 1271 priv.crqsize = cpu_to_le16(queue->recv_queue_size); 1272 1273 ret = rdma_accept(cm_id, ¶m); 1274 if (ret) 1275 pr_err("rdma_accept failed (error code = %d)\n", ret); 1276 1277 return ret; 1278 } 1279 1280 static int nvmet_rdma_queue_connect(struct rdma_cm_id *cm_id, 1281 struct rdma_cm_event *event) 1282 { 1283 struct nvmet_rdma_port *port = cm_id->context; 1284 struct nvmet_rdma_device *ndev; 1285 struct nvmet_rdma_queue *queue; 1286 int ret = -EINVAL; 1287 1288 ndev = nvmet_rdma_find_get_device(cm_id); 1289 if (!ndev) { 1290 nvmet_rdma_cm_reject(cm_id, NVME_RDMA_CM_NO_RSC); 1291 return -ECONNREFUSED; 1292 } 1293 1294 queue = nvmet_rdma_alloc_queue(ndev, cm_id, event); 1295 if (!queue) { 1296 ret = -ENOMEM; 1297 goto put_device; 1298 } 1299 queue->port = port->nport; 1300 1301 if (queue->host_qid == 0) { 1302 /* Let inflight controller teardown complete */ 1303 flush_scheduled_work(); 1304 } 1305 1306 ret = nvmet_rdma_cm_accept(cm_id, queue, &event->param.conn); 1307 if (ret) { 1308 schedule_work(&queue->release_work); 1309 /* Destroying rdma_cm id is not needed here */ 1310 return 0; 1311 } 1312 1313 mutex_lock(&nvmet_rdma_queue_mutex); 1314 list_add_tail(&queue->queue_list, &nvmet_rdma_queue_list); 1315 mutex_unlock(&nvmet_rdma_queue_mutex); 1316 1317 return 0; 1318 1319 put_device: 1320 kref_put(&ndev->ref, nvmet_rdma_free_dev); 1321 1322 return ret; 1323 } 1324 1325 static void nvmet_rdma_queue_established(struct nvmet_rdma_queue *queue) 1326 { 1327 unsigned long flags; 1328 1329 spin_lock_irqsave(&queue->state_lock, flags); 1330 if (queue->state != NVMET_RDMA_Q_CONNECTING) { 1331 pr_warn("trying to establish a connected queue\n"); 1332 goto out_unlock; 1333 } 1334 queue->state = NVMET_RDMA_Q_LIVE; 1335 1336 while (!list_empty(&queue->rsp_wait_list)) { 1337 struct nvmet_rdma_rsp *cmd; 1338 1339 cmd = list_first_entry(&queue->rsp_wait_list, 1340 struct nvmet_rdma_rsp, wait_list); 1341 list_del(&cmd->wait_list); 1342 1343 spin_unlock_irqrestore(&queue->state_lock, flags); 1344 nvmet_rdma_handle_command(queue, cmd); 1345 spin_lock_irqsave(&queue->state_lock, flags); 1346 } 1347 1348 out_unlock: 1349 spin_unlock_irqrestore(&queue->state_lock, flags); 1350 } 1351 1352 static void __nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue) 1353 { 1354 bool disconnect = false; 1355 unsigned long flags; 1356 1357 pr_debug("cm_id= %p queue->state= %d\n", queue->cm_id, queue->state); 1358 1359 spin_lock_irqsave(&queue->state_lock, flags); 1360 switch (queue->state) { 1361 case NVMET_RDMA_Q_CONNECTING: 1362 case NVMET_RDMA_Q_LIVE: 1363 queue->state = NVMET_RDMA_Q_DISCONNECTING; 1364 disconnect = true; 1365 break; 1366 case NVMET_RDMA_Q_DISCONNECTING: 1367 break; 1368 } 1369 spin_unlock_irqrestore(&queue->state_lock, flags); 1370 1371 if (disconnect) { 1372 rdma_disconnect(queue->cm_id); 1373 schedule_work(&queue->release_work); 1374 } 1375 } 1376 1377 static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue) 1378 { 1379 bool disconnect = false; 1380 1381 mutex_lock(&nvmet_rdma_queue_mutex); 1382 if (!list_empty(&queue->queue_list)) { 1383 list_del_init(&queue->queue_list); 1384 disconnect = true; 1385 } 1386 mutex_unlock(&nvmet_rdma_queue_mutex); 1387 1388 if (disconnect) 1389 __nvmet_rdma_queue_disconnect(queue); 1390 } 1391 1392 static void nvmet_rdma_queue_connect_fail(struct rdma_cm_id *cm_id, 1393 struct nvmet_rdma_queue *queue) 1394 { 1395 WARN_ON_ONCE(queue->state != NVMET_RDMA_Q_CONNECTING); 1396 1397 mutex_lock(&nvmet_rdma_queue_mutex); 1398 if (!list_empty(&queue->queue_list)) 1399 list_del_init(&queue->queue_list); 1400 mutex_unlock(&nvmet_rdma_queue_mutex); 1401 1402 pr_err("failed to connect queue %d\n", queue->idx); 1403 schedule_work(&queue->release_work); 1404 } 1405 1406 /** 1407 * nvme_rdma_device_removal() - Handle RDMA device removal 1408 * @cm_id: rdma_cm id, used for nvmet port 1409 * @queue: nvmet rdma queue (cm id qp_context) 1410 * 1411 * DEVICE_REMOVAL event notifies us that the RDMA device is about 1412 * to unplug. Note that this event can be generated on a normal 1413 * queue cm_id and/or a device bound listener cm_id (where in this 1414 * case queue will be null). 1415 * 1416 * We registered an ib_client to handle device removal for queues, 1417 * so we only need to handle the listening port cm_ids. In this case 1418 * we nullify the priv to prevent double cm_id destruction and destroying 1419 * the cm_id implicitely by returning a non-zero rc to the callout. 1420 */ 1421 static int nvmet_rdma_device_removal(struct rdma_cm_id *cm_id, 1422 struct nvmet_rdma_queue *queue) 1423 { 1424 struct nvmet_rdma_port *port; 1425 1426 if (queue) { 1427 /* 1428 * This is a queue cm_id. we have registered 1429 * an ib_client to handle queues removal 1430 * so don't interfear and just return. 1431 */ 1432 return 0; 1433 } 1434 1435 port = cm_id->context; 1436 1437 /* 1438 * This is a listener cm_id. Make sure that 1439 * future remove_port won't invoke a double 1440 * cm_id destroy. use atomic xchg to make sure 1441 * we don't compete with remove_port. 1442 */ 1443 if (xchg(&port->cm_id, NULL) != cm_id) 1444 return 0; 1445 1446 /* 1447 * We need to return 1 so that the core will destroy 1448 * it's own ID. What a great API design.. 1449 */ 1450 return 1; 1451 } 1452 1453 static int nvmet_rdma_cm_handler(struct rdma_cm_id *cm_id, 1454 struct rdma_cm_event *event) 1455 { 1456 struct nvmet_rdma_queue *queue = NULL; 1457 int ret = 0; 1458 1459 if (cm_id->qp) 1460 queue = cm_id->qp->qp_context; 1461 1462 pr_debug("%s (%d): status %d id %p\n", 1463 rdma_event_msg(event->event), event->event, 1464 event->status, cm_id); 1465 1466 switch (event->event) { 1467 case RDMA_CM_EVENT_CONNECT_REQUEST: 1468 ret = nvmet_rdma_queue_connect(cm_id, event); 1469 break; 1470 case RDMA_CM_EVENT_ESTABLISHED: 1471 nvmet_rdma_queue_established(queue); 1472 break; 1473 case RDMA_CM_EVENT_ADDR_CHANGE: 1474 if (!queue) { 1475 struct nvmet_rdma_port *port = cm_id->context; 1476 1477 schedule_delayed_work(&port->repair_work, 0); 1478 break; 1479 } 1480 /* FALLTHROUGH */ 1481 case RDMA_CM_EVENT_DISCONNECTED: 1482 case RDMA_CM_EVENT_TIMEWAIT_EXIT: 1483 nvmet_rdma_queue_disconnect(queue); 1484 break; 1485 case RDMA_CM_EVENT_DEVICE_REMOVAL: 1486 ret = nvmet_rdma_device_removal(cm_id, queue); 1487 break; 1488 case RDMA_CM_EVENT_REJECTED: 1489 pr_debug("Connection rejected: %s\n", 1490 rdma_reject_msg(cm_id, event->status)); 1491 /* FALLTHROUGH */ 1492 case RDMA_CM_EVENT_UNREACHABLE: 1493 case RDMA_CM_EVENT_CONNECT_ERROR: 1494 nvmet_rdma_queue_connect_fail(cm_id, queue); 1495 break; 1496 default: 1497 pr_err("received unrecognized RDMA CM event %d\n", 1498 event->event); 1499 break; 1500 } 1501 1502 return ret; 1503 } 1504 1505 static void nvmet_rdma_delete_ctrl(struct nvmet_ctrl *ctrl) 1506 { 1507 struct nvmet_rdma_queue *queue; 1508 1509 restart: 1510 mutex_lock(&nvmet_rdma_queue_mutex); 1511 list_for_each_entry(queue, &nvmet_rdma_queue_list, queue_list) { 1512 if (queue->nvme_sq.ctrl == ctrl) { 1513 list_del_init(&queue->queue_list); 1514 mutex_unlock(&nvmet_rdma_queue_mutex); 1515 1516 __nvmet_rdma_queue_disconnect(queue); 1517 goto restart; 1518 } 1519 } 1520 mutex_unlock(&nvmet_rdma_queue_mutex); 1521 } 1522 1523 static void nvmet_rdma_disable_port(struct nvmet_rdma_port *port) 1524 { 1525 struct rdma_cm_id *cm_id = xchg(&port->cm_id, NULL); 1526 1527 if (cm_id) 1528 rdma_destroy_id(cm_id); 1529 } 1530 1531 static int nvmet_rdma_enable_port(struct nvmet_rdma_port *port) 1532 { 1533 struct sockaddr *addr = (struct sockaddr *)&port->addr; 1534 struct rdma_cm_id *cm_id; 1535 int ret; 1536 1537 cm_id = rdma_create_id(&init_net, nvmet_rdma_cm_handler, port, 1538 RDMA_PS_TCP, IB_QPT_RC); 1539 if (IS_ERR(cm_id)) { 1540 pr_err("CM ID creation failed\n"); 1541 return PTR_ERR(cm_id); 1542 } 1543 1544 /* 1545 * Allow both IPv4 and IPv6 sockets to bind a single port 1546 * at the same time. 1547 */ 1548 ret = rdma_set_afonly(cm_id, 1); 1549 if (ret) { 1550 pr_err("rdma_set_afonly failed (%d)\n", ret); 1551 goto out_destroy_id; 1552 } 1553 1554 ret = rdma_bind_addr(cm_id, addr); 1555 if (ret) { 1556 pr_err("binding CM ID to %pISpcs failed (%d)\n", addr, ret); 1557 goto out_destroy_id; 1558 } 1559 1560 ret = rdma_listen(cm_id, 128); 1561 if (ret) { 1562 pr_err("listening to %pISpcs failed (%d)\n", addr, ret); 1563 goto out_destroy_id; 1564 } 1565 1566 port->cm_id = cm_id; 1567 return 0; 1568 1569 out_destroy_id: 1570 rdma_destroy_id(cm_id); 1571 return ret; 1572 } 1573 1574 static void nvmet_rdma_repair_port_work(struct work_struct *w) 1575 { 1576 struct nvmet_rdma_port *port = container_of(to_delayed_work(w), 1577 struct nvmet_rdma_port, repair_work); 1578 int ret; 1579 1580 nvmet_rdma_disable_port(port); 1581 ret = nvmet_rdma_enable_port(port); 1582 if (ret) 1583 schedule_delayed_work(&port->repair_work, 5 * HZ); 1584 } 1585 1586 static int nvmet_rdma_add_port(struct nvmet_port *nport) 1587 { 1588 struct nvmet_rdma_port *port; 1589 __kernel_sa_family_t af; 1590 int ret; 1591 1592 port = kzalloc(sizeof(*port), GFP_KERNEL); 1593 if (!port) 1594 return -ENOMEM; 1595 1596 nport->priv = port; 1597 port->nport = nport; 1598 INIT_DELAYED_WORK(&port->repair_work, nvmet_rdma_repair_port_work); 1599 1600 switch (nport->disc_addr.adrfam) { 1601 case NVMF_ADDR_FAMILY_IP4: 1602 af = AF_INET; 1603 break; 1604 case NVMF_ADDR_FAMILY_IP6: 1605 af = AF_INET6; 1606 break; 1607 default: 1608 pr_err("address family %d not supported\n", 1609 nport->disc_addr.adrfam); 1610 ret = -EINVAL; 1611 goto out_free_port; 1612 } 1613 1614 if (nport->inline_data_size < 0) { 1615 nport->inline_data_size = NVMET_RDMA_DEFAULT_INLINE_DATA_SIZE; 1616 } else if (nport->inline_data_size > NVMET_RDMA_MAX_INLINE_DATA_SIZE) { 1617 pr_warn("inline_data_size %u is too large, reducing to %u\n", 1618 nport->inline_data_size, 1619 NVMET_RDMA_MAX_INLINE_DATA_SIZE); 1620 nport->inline_data_size = NVMET_RDMA_MAX_INLINE_DATA_SIZE; 1621 } 1622 1623 ret = inet_pton_with_scope(&init_net, af, nport->disc_addr.traddr, 1624 nport->disc_addr.trsvcid, &port->addr); 1625 if (ret) { 1626 pr_err("malformed ip/port passed: %s:%s\n", 1627 nport->disc_addr.traddr, nport->disc_addr.trsvcid); 1628 goto out_free_port; 1629 } 1630 1631 ret = nvmet_rdma_enable_port(port); 1632 if (ret) 1633 goto out_free_port; 1634 1635 pr_info("enabling port %d (%pISpcs)\n", 1636 le16_to_cpu(nport->disc_addr.portid), 1637 (struct sockaddr *)&port->addr); 1638 1639 return 0; 1640 1641 out_free_port: 1642 kfree(port); 1643 return ret; 1644 } 1645 1646 static void nvmet_rdma_remove_port(struct nvmet_port *nport) 1647 { 1648 struct nvmet_rdma_port *port = nport->priv; 1649 1650 cancel_delayed_work_sync(&port->repair_work); 1651 nvmet_rdma_disable_port(port); 1652 kfree(port); 1653 } 1654 1655 static void nvmet_rdma_disc_port_addr(struct nvmet_req *req, 1656 struct nvmet_port *nport, char *traddr) 1657 { 1658 struct nvmet_rdma_port *port = nport->priv; 1659 struct rdma_cm_id *cm_id = port->cm_id; 1660 1661 if (inet_addr_is_any((struct sockaddr *)&cm_id->route.addr.src_addr)) { 1662 struct nvmet_rdma_rsp *rsp = 1663 container_of(req, struct nvmet_rdma_rsp, req); 1664 struct rdma_cm_id *req_cm_id = rsp->queue->cm_id; 1665 struct sockaddr *addr = (void *)&req_cm_id->route.addr.src_addr; 1666 1667 sprintf(traddr, "%pISc", addr); 1668 } else { 1669 memcpy(traddr, nport->disc_addr.traddr, NVMF_TRADDR_SIZE); 1670 } 1671 } 1672 1673 static u8 nvmet_rdma_get_mdts(const struct nvmet_ctrl *ctrl) 1674 { 1675 return NVMET_RDMA_MAX_MDTS; 1676 } 1677 1678 static const struct nvmet_fabrics_ops nvmet_rdma_ops = { 1679 .owner = THIS_MODULE, 1680 .type = NVMF_TRTYPE_RDMA, 1681 .msdbd = 1, 1682 .has_keyed_sgls = 1, 1683 .add_port = nvmet_rdma_add_port, 1684 .remove_port = nvmet_rdma_remove_port, 1685 .queue_response = nvmet_rdma_queue_response, 1686 .delete_ctrl = nvmet_rdma_delete_ctrl, 1687 .disc_traddr = nvmet_rdma_disc_port_addr, 1688 .get_mdts = nvmet_rdma_get_mdts, 1689 }; 1690 1691 static void nvmet_rdma_remove_one(struct ib_device *ib_device, void *client_data) 1692 { 1693 struct nvmet_rdma_queue *queue, *tmp; 1694 struct nvmet_rdma_device *ndev; 1695 bool found = false; 1696 1697 mutex_lock(&device_list_mutex); 1698 list_for_each_entry(ndev, &device_list, entry) { 1699 if (ndev->device == ib_device) { 1700 found = true; 1701 break; 1702 } 1703 } 1704 mutex_unlock(&device_list_mutex); 1705 1706 if (!found) 1707 return; 1708 1709 /* 1710 * IB Device that is used by nvmet controllers is being removed, 1711 * delete all queues using this device. 1712 */ 1713 mutex_lock(&nvmet_rdma_queue_mutex); 1714 list_for_each_entry_safe(queue, tmp, &nvmet_rdma_queue_list, 1715 queue_list) { 1716 if (queue->dev->device != ib_device) 1717 continue; 1718 1719 pr_info("Removing queue %d\n", queue->idx); 1720 list_del_init(&queue->queue_list); 1721 __nvmet_rdma_queue_disconnect(queue); 1722 } 1723 mutex_unlock(&nvmet_rdma_queue_mutex); 1724 1725 flush_scheduled_work(); 1726 } 1727 1728 static struct ib_client nvmet_rdma_ib_client = { 1729 .name = "nvmet_rdma", 1730 .remove = nvmet_rdma_remove_one 1731 }; 1732 1733 static int __init nvmet_rdma_init(void) 1734 { 1735 int ret; 1736 1737 ret = ib_register_client(&nvmet_rdma_ib_client); 1738 if (ret) 1739 return ret; 1740 1741 ret = nvmet_register_transport(&nvmet_rdma_ops); 1742 if (ret) 1743 goto err_ib_client; 1744 1745 return 0; 1746 1747 err_ib_client: 1748 ib_unregister_client(&nvmet_rdma_ib_client); 1749 return ret; 1750 } 1751 1752 static void __exit nvmet_rdma_exit(void) 1753 { 1754 nvmet_unregister_transport(&nvmet_rdma_ops); 1755 ib_unregister_client(&nvmet_rdma_ib_client); 1756 WARN_ON_ONCE(!list_empty(&nvmet_rdma_queue_list)); 1757 ida_destroy(&nvmet_rdma_queue_ida); 1758 } 1759 1760 module_init(nvmet_rdma_init); 1761 module_exit(nvmet_rdma_exit); 1762 1763 MODULE_LICENSE("GPL v2"); 1764 MODULE_ALIAS("nvmet-transport-1"); /* 1 == NVMF_TRTYPE_RDMA */ 1765