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