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/blk-integrity.h> 9 #include <linux/ctype.h> 10 #include <linux/delay.h> 11 #include <linux/err.h> 12 #include <linux/init.h> 13 #include <linux/module.h> 14 #include <linux/nvme.h> 15 #include <linux/slab.h> 16 #include <linux/string.h> 17 #include <linux/wait.h> 18 #include <linux/inet.h> 19 #include <linux/unaligned.h> 20 21 #include <rdma/ib_verbs.h> 22 #include <rdma/rdma_cm.h> 23 #include <rdma/rw.h> 24 #include <rdma/ib_cm.h> 25 26 #include <linux/nvme-rdma.h> 27 #include "nvmet.h" 28 29 /* 30 * We allow at least 1 page, up to 4 SGEs, and up to 16KB of inline data 31 */ 32 #define NVMET_RDMA_DEFAULT_INLINE_DATA_SIZE PAGE_SIZE 33 #define NVMET_RDMA_MAX_INLINE_SGE 4 34 #define NVMET_RDMA_MAX_INLINE_DATA_SIZE max_t(int, SZ_16K, PAGE_SIZE) 35 36 /* Assume mpsmin == device_page_size == 4KB */ 37 #define NVMET_RDMA_MAX_MDTS 8 38 #define NVMET_RDMA_MAX_METADATA_MDTS 5 39 40 #define NVMET_RDMA_BACKLOG 128 41 42 #define NVMET_RDMA_DISCRETE_RSP_TAG -1 43 44 struct nvmet_rdma_srq; 45 46 struct nvmet_rdma_cmd { 47 struct ib_sge sge[NVMET_RDMA_MAX_INLINE_SGE + 1]; 48 struct ib_cqe cqe; 49 struct ib_recv_wr wr; 50 struct scatterlist inline_sg[NVMET_RDMA_MAX_INLINE_SGE]; 51 struct nvme_command *nvme_cmd; 52 struct nvmet_rdma_queue *queue; 53 struct nvmet_rdma_srq *nsrq; 54 }; 55 56 enum { 57 NVMET_RDMA_REQ_INLINE_DATA = (1 << 0), 58 }; 59 60 struct nvmet_rdma_rsp { 61 struct ib_sge send_sge; 62 struct ib_cqe send_cqe; 63 struct ib_send_wr send_wr; 64 65 struct nvmet_rdma_cmd *cmd; 66 struct nvmet_rdma_queue *queue; 67 68 struct ib_cqe read_cqe; 69 struct ib_cqe write_cqe; 70 struct rdma_rw_ctx rw; 71 72 struct nvmet_req req; 73 74 u8 n_rdma; 75 u32 flags; 76 u32 invalidate_rkey; 77 78 struct list_head wait_list; 79 int tag; 80 }; 81 82 enum nvmet_rdma_queue_state { 83 NVMET_RDMA_Q_CONNECTING, 84 NVMET_RDMA_Q_LIVE, 85 NVMET_RDMA_Q_DISCONNECTING, 86 }; 87 88 struct nvmet_rdma_queue { 89 struct rdma_cm_id *cm_id; 90 struct ib_qp *qp; 91 struct nvmet_port *port; 92 struct ib_cq *cq; 93 atomic_t sq_wr_avail; 94 struct nvmet_rdma_device *dev; 95 struct nvmet_rdma_srq *nsrq; 96 spinlock_t state_lock; 97 enum nvmet_rdma_queue_state state; 98 struct nvmet_cq nvme_cq; 99 struct nvmet_sq nvme_sq; 100 101 struct nvmet_rdma_rsp *rsps; 102 struct sbitmap rsp_tags; 103 struct nvmet_rdma_cmd *cmds; 104 105 struct work_struct release_work; 106 struct list_head rsp_wait_list; 107 struct list_head rsp_wr_wait_list; 108 spinlock_t rsp_wr_wait_lock; 109 110 int idx; 111 int host_qid; 112 int comp_vector; 113 int recv_queue_size; 114 int send_queue_size; 115 116 struct list_head queue_list; 117 }; 118 119 struct nvmet_rdma_port { 120 struct nvmet_port *nport; 121 struct sockaddr_storage addr; 122 struct rdma_cm_id *cm_id; 123 struct delayed_work repair_work; 124 }; 125 126 struct nvmet_rdma_srq { 127 struct ib_srq *srq; 128 struct nvmet_rdma_cmd *cmds; 129 struct nvmet_rdma_device *ndev; 130 }; 131 132 struct nvmet_rdma_device { 133 struct ib_device *device; 134 struct ib_pd *pd; 135 struct nvmet_rdma_srq **srqs; 136 int srq_count; 137 size_t srq_size; 138 struct kref ref; 139 struct list_head entry; 140 int inline_data_size; 141 int inline_page_count; 142 }; 143 144 static bool nvmet_rdma_use_srq; 145 module_param_named(use_srq, nvmet_rdma_use_srq, bool, 0444); 146 MODULE_PARM_DESC(use_srq, "Use shared receive queue."); 147 148 static int srq_size_set(const char *val, const struct kernel_param *kp); 149 static const struct kernel_param_ops srq_size_ops = { 150 .set = srq_size_set, 151 .get = param_get_uint, 152 }; 153 154 static unsigned int nvmet_rdma_srq_size = 1024; 155 module_param_cb(srq_size, &srq_size_ops, &nvmet_rdma_srq_size, 0644); 156 MODULE_PARM_DESC(srq_size, "set Shared Receive Queue (SRQ) size, should >= 256 (default: 1024)"); 157 158 static DEFINE_IDA(nvmet_rdma_queue_ida); 159 static LIST_HEAD(nvmet_rdma_queue_list); 160 static DEFINE_MUTEX(nvmet_rdma_queue_mutex); 161 162 static LIST_HEAD(device_list); 163 static DEFINE_MUTEX(device_list_mutex); 164 165 static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp); 166 static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc); 167 static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc); 168 static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc); 169 static void nvmet_rdma_write_data_done(struct ib_cq *cq, struct ib_wc *wc); 170 static void nvmet_rdma_qp_event(struct ib_event *event, void *priv); 171 static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue); 172 static void nvmet_rdma_free_rsp(struct nvmet_rdma_device *ndev, 173 struct nvmet_rdma_rsp *r); 174 static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev, 175 struct nvmet_rdma_rsp *r, 176 int tag); 177 178 static const struct nvmet_fabrics_ops nvmet_rdma_ops; 179 180 static int srq_size_set(const char *val, const struct kernel_param *kp) 181 { 182 unsigned int n; 183 int ret; 184 185 ret = kstrtouint(val, 10, &n); 186 if (ret != 0 || n < 256) 187 return -EINVAL; 188 189 return param_set_uint(val, kp); 190 } 191 192 static int num_pages(int len) 193 { 194 return 1 + (((len - 1) & PAGE_MASK) >> PAGE_SHIFT); 195 } 196 197 static inline bool nvmet_rdma_need_data_in(struct nvmet_rdma_rsp *rsp) 198 { 199 return nvme_is_write(rsp->req.cmd) && 200 rsp->req.transfer_len && 201 !(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA); 202 } 203 204 static inline bool nvmet_rdma_need_data_out(struct nvmet_rdma_rsp *rsp) 205 { 206 return !nvme_is_write(rsp->req.cmd) && 207 rsp->req.transfer_len && 208 !rsp->req.cqe->status && 209 !(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA); 210 } 211 212 static inline struct nvmet_rdma_rsp * 213 nvmet_rdma_get_rsp(struct nvmet_rdma_queue *queue) 214 { 215 struct nvmet_rdma_rsp *rsp = NULL; 216 int tag; 217 218 tag = sbitmap_get(&queue->rsp_tags); 219 if (tag >= 0) 220 rsp = &queue->rsps[tag]; 221 222 if (unlikely(!rsp)) { 223 int ret; 224 225 rsp = kzalloc_obj(*rsp); 226 if (unlikely(!rsp)) 227 return NULL; 228 ret = nvmet_rdma_alloc_rsp(queue->dev, rsp, 229 NVMET_RDMA_DISCRETE_RSP_TAG); 230 if (unlikely(ret)) { 231 kfree(rsp); 232 return NULL; 233 } 234 } 235 236 return rsp; 237 } 238 239 static inline void 240 nvmet_rdma_put_rsp(struct nvmet_rdma_rsp *rsp) 241 { 242 if (unlikely(rsp->tag == NVMET_RDMA_DISCRETE_RSP_TAG)) { 243 nvmet_rdma_free_rsp(rsp->queue->dev, rsp); 244 kfree(rsp); 245 return; 246 } 247 248 sbitmap_clear_bit(&rsp->queue->rsp_tags, rsp->tag); 249 } 250 251 static void nvmet_rdma_free_inline_pages(struct nvmet_rdma_device *ndev, 252 struct nvmet_rdma_cmd *c) 253 { 254 struct scatterlist *sg; 255 struct ib_sge *sge; 256 int i; 257 258 if (!ndev->inline_data_size) 259 return; 260 261 sg = c->inline_sg; 262 sge = &c->sge[1]; 263 264 for (i = 0; i < ndev->inline_page_count; i++, sg++, sge++) { 265 if (sge->length) 266 ib_dma_unmap_page(ndev->device, sge->addr, 267 sge->length, DMA_FROM_DEVICE); 268 if (sg_page(sg)) 269 __free_page(sg_page(sg)); 270 } 271 } 272 273 static int nvmet_rdma_alloc_inline_pages(struct nvmet_rdma_device *ndev, 274 struct nvmet_rdma_cmd *c) 275 { 276 struct scatterlist *sg; 277 struct ib_sge *sge; 278 struct page *pg; 279 int len; 280 int i; 281 282 if (!ndev->inline_data_size) 283 return 0; 284 285 sg = c->inline_sg; 286 sg_init_table(sg, ndev->inline_page_count); 287 sge = &c->sge[1]; 288 len = ndev->inline_data_size; 289 290 for (i = 0; i < ndev->inline_page_count; i++, sg++, sge++) { 291 pg = alloc_page(GFP_KERNEL); 292 if (!pg) 293 goto out_err; 294 sg_assign_page(sg, pg); 295 sge->addr = ib_dma_map_page(ndev->device, 296 pg, 0, PAGE_SIZE, DMA_FROM_DEVICE); 297 if (ib_dma_mapping_error(ndev->device, sge->addr)) 298 goto out_err; 299 sge->length = min_t(int, len, PAGE_SIZE); 300 sge->lkey = ndev->pd->local_dma_lkey; 301 len -= sge->length; 302 } 303 304 return 0; 305 out_err: 306 for (; i >= 0; i--, sg--, sge--) { 307 if (sge->length) 308 ib_dma_unmap_page(ndev->device, sge->addr, 309 sge->length, DMA_FROM_DEVICE); 310 if (sg_page(sg)) 311 __free_page(sg_page(sg)); 312 } 313 return -ENOMEM; 314 } 315 316 static int nvmet_rdma_alloc_cmd(struct nvmet_rdma_device *ndev, 317 struct nvmet_rdma_cmd *c, bool admin) 318 { 319 /* NVMe command / RDMA RECV */ 320 c->nvme_cmd = kmalloc_obj(*c->nvme_cmd); 321 if (!c->nvme_cmd) 322 goto out; 323 324 c->sge[0].addr = ib_dma_map_single(ndev->device, c->nvme_cmd, 325 sizeof(*c->nvme_cmd), DMA_FROM_DEVICE); 326 if (ib_dma_mapping_error(ndev->device, c->sge[0].addr)) 327 goto out_free_cmd; 328 329 c->sge[0].length = sizeof(*c->nvme_cmd); 330 c->sge[0].lkey = ndev->pd->local_dma_lkey; 331 332 if (!admin && nvmet_rdma_alloc_inline_pages(ndev, c)) 333 goto out_unmap_cmd; 334 335 c->cqe.done = nvmet_rdma_recv_done; 336 337 c->wr.wr_cqe = &c->cqe; 338 c->wr.sg_list = c->sge; 339 c->wr.num_sge = admin ? 1 : ndev->inline_page_count + 1; 340 341 return 0; 342 343 out_unmap_cmd: 344 ib_dma_unmap_single(ndev->device, c->sge[0].addr, 345 sizeof(*c->nvme_cmd), DMA_FROM_DEVICE); 346 out_free_cmd: 347 kfree(c->nvme_cmd); 348 349 out: 350 return -ENOMEM; 351 } 352 353 static void nvmet_rdma_free_cmd(struct nvmet_rdma_device *ndev, 354 struct nvmet_rdma_cmd *c, bool admin) 355 { 356 if (!admin) 357 nvmet_rdma_free_inline_pages(ndev, c); 358 ib_dma_unmap_single(ndev->device, c->sge[0].addr, 359 sizeof(*c->nvme_cmd), DMA_FROM_DEVICE); 360 kfree(c->nvme_cmd); 361 } 362 363 static struct nvmet_rdma_cmd * 364 nvmet_rdma_alloc_cmds(struct nvmet_rdma_device *ndev, 365 int nr_cmds, bool admin) 366 { 367 struct nvmet_rdma_cmd *cmds; 368 int ret = -EINVAL, i; 369 370 cmds = kvzalloc_objs(struct nvmet_rdma_cmd, nr_cmds); 371 if (!cmds) 372 goto out; 373 374 for (i = 0; i < nr_cmds; i++) { 375 ret = nvmet_rdma_alloc_cmd(ndev, cmds + i, admin); 376 if (ret) 377 goto out_free; 378 } 379 380 return cmds; 381 382 out_free: 383 while (--i >= 0) 384 nvmet_rdma_free_cmd(ndev, cmds + i, admin); 385 kvfree(cmds); 386 out: 387 return ERR_PTR(ret); 388 } 389 390 static void nvmet_rdma_free_cmds(struct nvmet_rdma_device *ndev, 391 struct nvmet_rdma_cmd *cmds, int nr_cmds, bool admin) 392 { 393 int i; 394 395 for (i = 0; i < nr_cmds; i++) 396 nvmet_rdma_free_cmd(ndev, cmds + i, admin); 397 kvfree(cmds); 398 } 399 400 static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev, 401 struct nvmet_rdma_rsp *r, int tag) 402 { 403 /* NVMe CQE / RDMA SEND */ 404 r->req.cqe = kmalloc_obj(*r->req.cqe); 405 if (!r->req.cqe) 406 goto out; 407 408 r->send_sge.addr = ib_dma_map_single(ndev->device, r->req.cqe, 409 sizeof(*r->req.cqe), DMA_TO_DEVICE); 410 if (ib_dma_mapping_error(ndev->device, r->send_sge.addr)) 411 goto out_free_rsp; 412 413 if (ib_dma_pci_p2p_dma_supported(ndev->device)) 414 r->req.p2p_client = &ndev->device->dev; 415 r->send_sge.length = sizeof(*r->req.cqe); 416 r->send_sge.lkey = ndev->pd->local_dma_lkey; 417 418 r->send_cqe.done = nvmet_rdma_send_done; 419 420 r->send_wr.wr_cqe = &r->send_cqe; 421 r->send_wr.sg_list = &r->send_sge; 422 r->send_wr.num_sge = 1; 423 r->send_wr.send_flags = IB_SEND_SIGNALED; 424 425 /* Data In / RDMA READ */ 426 r->read_cqe.done = nvmet_rdma_read_data_done; 427 /* Data Out / RDMA WRITE */ 428 r->write_cqe.done = nvmet_rdma_write_data_done; 429 r->tag = tag; 430 431 return 0; 432 433 out_free_rsp: 434 kfree(r->req.cqe); 435 out: 436 return -ENOMEM; 437 } 438 439 static void nvmet_rdma_free_rsp(struct nvmet_rdma_device *ndev, 440 struct nvmet_rdma_rsp *r) 441 { 442 ib_dma_unmap_single(ndev->device, r->send_sge.addr, 443 sizeof(*r->req.cqe), DMA_TO_DEVICE); 444 kfree(r->req.cqe); 445 } 446 447 static int 448 nvmet_rdma_alloc_rsps(struct nvmet_rdma_queue *queue) 449 { 450 struct nvmet_rdma_device *ndev = queue->dev; 451 int nr_rsps = queue->recv_queue_size * 2; 452 int ret = -ENOMEM, i; 453 454 if (sbitmap_init_node(&queue->rsp_tags, nr_rsps, -1, GFP_KERNEL, 455 NUMA_NO_NODE, false, true)) 456 goto out; 457 458 queue->rsps = kvzalloc_objs(struct nvmet_rdma_rsp, nr_rsps); 459 if (!queue->rsps) 460 goto out_free_sbitmap; 461 462 for (i = 0; i < nr_rsps; i++) { 463 struct nvmet_rdma_rsp *rsp = &queue->rsps[i]; 464 465 ret = nvmet_rdma_alloc_rsp(ndev, rsp, i); 466 if (ret) 467 goto out_free; 468 } 469 470 return 0; 471 472 out_free: 473 while (--i >= 0) 474 nvmet_rdma_free_rsp(ndev, &queue->rsps[i]); 475 kvfree(queue->rsps); 476 out_free_sbitmap: 477 sbitmap_free(&queue->rsp_tags); 478 out: 479 return ret; 480 } 481 482 static void nvmet_rdma_free_rsps(struct nvmet_rdma_queue *queue) 483 { 484 struct nvmet_rdma_device *ndev = queue->dev; 485 int i, nr_rsps = queue->recv_queue_size * 2; 486 487 for (i = 0; i < nr_rsps; i++) 488 nvmet_rdma_free_rsp(ndev, &queue->rsps[i]); 489 kvfree(queue->rsps); 490 sbitmap_free(&queue->rsp_tags); 491 } 492 493 static int nvmet_rdma_post_recv(struct nvmet_rdma_device *ndev, 494 struct nvmet_rdma_cmd *cmd) 495 { 496 int ret; 497 498 ib_dma_sync_single_for_device(ndev->device, 499 cmd->sge[0].addr, cmd->sge[0].length, 500 DMA_FROM_DEVICE); 501 502 if (cmd->nsrq) 503 ret = ib_post_srq_recv(cmd->nsrq->srq, &cmd->wr, NULL); 504 else 505 ret = ib_post_recv(cmd->queue->qp, &cmd->wr, NULL); 506 507 if (unlikely(ret)) 508 pr_err("post_recv cmd failed\n"); 509 510 return ret; 511 } 512 513 static void nvmet_rdma_process_wr_wait_list(struct nvmet_rdma_queue *queue) 514 { 515 unsigned long flags; 516 517 spin_lock_irqsave(&queue->rsp_wr_wait_lock, flags); 518 while (!list_empty(&queue->rsp_wr_wait_list)) { 519 struct nvmet_rdma_rsp *rsp; 520 bool ret; 521 522 rsp = list_entry(queue->rsp_wr_wait_list.next, 523 struct nvmet_rdma_rsp, wait_list); 524 list_del(&rsp->wait_list); 525 526 spin_unlock_irqrestore(&queue->rsp_wr_wait_lock, flags); 527 ret = nvmet_rdma_execute_command(rsp); 528 spin_lock_irqsave(&queue->rsp_wr_wait_lock, flags); 529 530 if (!ret) { 531 list_add(&rsp->wait_list, &queue->rsp_wr_wait_list); 532 break; 533 } 534 } 535 spin_unlock_irqrestore(&queue->rsp_wr_wait_lock, flags); 536 } 537 538 static u16 nvmet_rdma_check_pi_status(struct ib_mr *sig_mr) 539 { 540 struct ib_mr_status mr_status; 541 int ret; 542 u16 status = 0; 543 544 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status); 545 if (ret) { 546 pr_err("ib_check_mr_status failed, ret %d\n", ret); 547 return NVME_SC_INVALID_PI; 548 } 549 550 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) { 551 switch (mr_status.sig_err.err_type) { 552 case IB_SIG_BAD_GUARD: 553 status = NVME_SC_GUARD_CHECK; 554 break; 555 case IB_SIG_BAD_REFTAG: 556 status = NVME_SC_REFTAG_CHECK; 557 break; 558 case IB_SIG_BAD_APPTAG: 559 status = NVME_SC_APPTAG_CHECK; 560 break; 561 } 562 pr_err("PI error found type %d expected 0x%x vs actual 0x%x\n", 563 mr_status.sig_err.err_type, 564 mr_status.sig_err.expected, 565 mr_status.sig_err.actual); 566 } 567 568 return status; 569 } 570 571 static void nvmet_rdma_set_sig_domain(struct blk_integrity *bi, 572 struct nvme_command *cmd, struct ib_sig_domain *domain, 573 u16 control, u8 pi_type) 574 { 575 domain->sig_type = IB_SIG_TYPE_T10_DIF; 576 domain->sig.dif.bg_type = IB_T10DIF_CRC; 577 domain->sig.dif.pi_interval = 1 << bi->interval_exp; 578 domain->sig.dif.ref_tag = le32_to_cpu(cmd->rw.reftag); 579 if (control & NVME_RW_PRINFO_PRCHK_REF) 580 domain->sig.dif.ref_remap = true; 581 582 domain->sig.dif.app_tag = le16_to_cpu(cmd->rw.lbat); 583 domain->sig.dif.apptag_check_mask = le16_to_cpu(cmd->rw.lbatm); 584 domain->sig.dif.app_escape = true; 585 if (pi_type == NVME_NS_DPS_PI_TYPE3) 586 domain->sig.dif.ref_escape = true; 587 } 588 589 static void nvmet_rdma_set_sig_attrs(struct nvmet_req *req, 590 struct ib_sig_attrs *sig_attrs) 591 { 592 struct nvme_command *cmd = req->cmd; 593 u16 control = le16_to_cpu(cmd->rw.control); 594 u8 pi_type = req->ns->pi_type; 595 struct blk_integrity *bi; 596 597 bi = bdev_get_integrity(req->ns->bdev); 598 599 memset(sig_attrs, 0, sizeof(*sig_attrs)); 600 601 if (control & NVME_RW_PRINFO_PRACT) { 602 /* for WRITE_INSERT/READ_STRIP no wire domain */ 603 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE; 604 nvmet_rdma_set_sig_domain(bi, cmd, &sig_attrs->mem, control, 605 pi_type); 606 /* Clear the PRACT bit since HCA will generate/verify the PI */ 607 control &= ~NVME_RW_PRINFO_PRACT; 608 cmd->rw.control = cpu_to_le16(control); 609 /* PI is added by the HW */ 610 req->transfer_len += req->metadata_len; 611 } else { 612 /* for WRITE_PASS/READ_PASS both wire/memory domains exist */ 613 nvmet_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control, 614 pi_type); 615 nvmet_rdma_set_sig_domain(bi, cmd, &sig_attrs->mem, control, 616 pi_type); 617 } 618 619 if (control & NVME_RW_PRINFO_PRCHK_REF) 620 sig_attrs->check_mask |= IB_SIG_CHECK_REFTAG; 621 if (control & NVME_RW_PRINFO_PRCHK_GUARD) 622 sig_attrs->check_mask |= IB_SIG_CHECK_GUARD; 623 if (control & NVME_RW_PRINFO_PRCHK_APP) 624 sig_attrs->check_mask |= IB_SIG_CHECK_APPTAG; 625 } 626 627 static int nvmet_rdma_rw_ctx_init(struct nvmet_rdma_rsp *rsp, u64 addr, u32 key, 628 struct ib_sig_attrs *sig_attrs) 629 { 630 struct rdma_cm_id *cm_id = rsp->queue->cm_id; 631 struct nvmet_req *req = &rsp->req; 632 int ret; 633 634 if (req->metadata_len) 635 ret = rdma_rw_ctx_signature_init(&rsp->rw, cm_id->qp, 636 cm_id->port_num, req->sg, req->sg_cnt, 637 req->metadata_sg, req->metadata_sg_cnt, sig_attrs, 638 addr, key, nvmet_data_dir(req)); 639 else 640 ret = rdma_rw_ctx_init(&rsp->rw, cm_id->qp, cm_id->port_num, 641 req->sg, req->sg_cnt, 0, addr, key, 642 nvmet_data_dir(req)); 643 644 return ret; 645 } 646 647 static void nvmet_rdma_rw_ctx_destroy(struct nvmet_rdma_rsp *rsp) 648 { 649 struct rdma_cm_id *cm_id = rsp->queue->cm_id; 650 struct nvmet_req *req = &rsp->req; 651 652 if (req->metadata_len) 653 rdma_rw_ctx_destroy_signature(&rsp->rw, cm_id->qp, 654 cm_id->port_num, req->sg, req->sg_cnt, 655 req->metadata_sg, req->metadata_sg_cnt, 656 nvmet_data_dir(req)); 657 else 658 rdma_rw_ctx_destroy(&rsp->rw, cm_id->qp, cm_id->port_num, 659 req->sg, req->sg_cnt, nvmet_data_dir(req)); 660 } 661 662 static void nvmet_rdma_free_rsp_resources(struct nvmet_rdma_rsp *rsp) 663 { 664 struct nvmet_rdma_queue *queue = rsp->queue; 665 666 if (rsp->n_rdma) 667 nvmet_rdma_rw_ctx_destroy(rsp); 668 669 if (rsp->req.sg < rsp->cmd->inline_sg || 670 rsp->req.sg >= rsp->cmd->inline_sg + queue->dev->inline_page_count) 671 nvmet_req_free_sgls(&rsp->req); 672 } 673 674 static void nvmet_rdma_release_rsp(struct nvmet_rdma_rsp *rsp) 675 { 676 struct nvmet_rdma_queue *queue = rsp->queue; 677 678 atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail); 679 680 nvmet_rdma_free_rsp_resources(rsp); 681 682 if (unlikely(!list_empty_careful(&queue->rsp_wr_wait_list))) 683 nvmet_rdma_process_wr_wait_list(queue); 684 685 nvmet_rdma_put_rsp(rsp); 686 } 687 688 static void nvmet_rdma_error_comp(struct nvmet_rdma_queue *queue) 689 { 690 if (queue->nvme_sq.ctrl) { 691 nvmet_ctrl_fatal_error(queue->nvme_sq.ctrl); 692 } else { 693 /* 694 * we didn't setup the controller yet in case 695 * of admin connect error, just disconnect and 696 * cleanup the queue 697 */ 698 nvmet_rdma_queue_disconnect(queue); 699 } 700 } 701 702 static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc) 703 { 704 struct nvmet_rdma_rsp *rsp = 705 container_of(wc->wr_cqe, struct nvmet_rdma_rsp, send_cqe); 706 struct nvmet_rdma_queue *queue = wc->qp->qp_context; 707 708 nvmet_rdma_release_rsp(rsp); 709 710 if (unlikely(wc->status != IB_WC_SUCCESS && 711 wc->status != IB_WC_WR_FLUSH_ERR)) { 712 pr_err("SEND for CQE 0x%p failed with status %s (%d).\n", 713 wc->wr_cqe, ib_wc_status_msg(wc->status), wc->status); 714 nvmet_rdma_error_comp(queue); 715 } 716 } 717 718 static void nvmet_rdma_queue_response(struct nvmet_req *req) 719 { 720 struct nvmet_rdma_rsp *rsp = 721 container_of(req, struct nvmet_rdma_rsp, req); 722 struct rdma_cm_id *cm_id = rsp->queue->cm_id; 723 struct ib_send_wr *first_wr; 724 725 if (rsp->invalidate_rkey) { 726 rsp->send_wr.opcode = IB_WR_SEND_WITH_INV; 727 rsp->send_wr.ex.invalidate_rkey = rsp->invalidate_rkey; 728 } else { 729 rsp->send_wr.opcode = IB_WR_SEND; 730 } 731 732 if (nvmet_rdma_need_data_out(rsp)) { 733 if (rsp->req.metadata_len) 734 first_wr = rdma_rw_ctx_wrs(&rsp->rw, cm_id->qp, 735 cm_id->port_num, &rsp->write_cqe, NULL); 736 else 737 first_wr = rdma_rw_ctx_wrs(&rsp->rw, cm_id->qp, 738 cm_id->port_num, NULL, &rsp->send_wr); 739 } else { 740 first_wr = &rsp->send_wr; 741 } 742 743 nvmet_rdma_post_recv(rsp->queue->dev, rsp->cmd); 744 745 ib_dma_sync_single_for_device(rsp->queue->dev->device, 746 rsp->send_sge.addr, rsp->send_sge.length, 747 DMA_TO_DEVICE); 748 749 if (unlikely(ib_post_send(cm_id->qp, first_wr, NULL))) { 750 pr_err("sending cmd response failed\n"); 751 nvmet_rdma_release_rsp(rsp); 752 } 753 } 754 755 static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc) 756 { 757 struct nvmet_rdma_rsp *rsp = 758 container_of(wc->wr_cqe, struct nvmet_rdma_rsp, read_cqe); 759 struct nvmet_rdma_queue *queue = wc->qp->qp_context; 760 u16 status = 0; 761 762 WARN_ON(rsp->n_rdma <= 0); 763 atomic_add(rsp->n_rdma, &queue->sq_wr_avail); 764 rsp->n_rdma = 0; 765 766 if (unlikely(wc->status != IB_WC_SUCCESS)) { 767 nvmet_rdma_rw_ctx_destroy(rsp); 768 nvmet_req_uninit(&rsp->req); 769 nvmet_rdma_release_rsp(rsp); 770 if (wc->status != IB_WC_WR_FLUSH_ERR) { 771 pr_info("RDMA READ for CQE 0x%p failed with status %s (%d).\n", 772 wc->wr_cqe, ib_wc_status_msg(wc->status), wc->status); 773 nvmet_rdma_error_comp(queue); 774 } 775 return; 776 } 777 778 if (rsp->req.metadata_len) 779 status = nvmet_rdma_check_pi_status(rsp->rw.reg->mr); 780 nvmet_rdma_rw_ctx_destroy(rsp); 781 782 if (unlikely(status)) 783 nvmet_req_complete(&rsp->req, status); 784 else 785 rsp->req.execute(&rsp->req); 786 } 787 788 static void nvmet_rdma_write_data_done(struct ib_cq *cq, struct ib_wc *wc) 789 { 790 struct nvmet_rdma_rsp *rsp = 791 container_of(wc->wr_cqe, struct nvmet_rdma_rsp, write_cqe); 792 struct nvmet_rdma_queue *queue = wc->qp->qp_context; 793 struct rdma_cm_id *cm_id = rsp->queue->cm_id; 794 u16 status; 795 796 if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY)) 797 return; 798 799 WARN_ON(rsp->n_rdma <= 0); 800 atomic_add(rsp->n_rdma, &queue->sq_wr_avail); 801 rsp->n_rdma = 0; 802 803 if (unlikely(wc->status != IB_WC_SUCCESS)) { 804 nvmet_rdma_rw_ctx_destroy(rsp); 805 /* 806 * Unlike in nvmet_rdma_read_data_done(), the request has been 807 * completed and its references are put: no nvmet_req_uninit(). 808 */ 809 nvmet_rdma_release_rsp(rsp); 810 if (wc->status != IB_WC_WR_FLUSH_ERR) { 811 pr_info("RDMA WRITE for CQE failed with status %s (%d).\n", 812 ib_wc_status_msg(wc->status), wc->status); 813 nvmet_rdma_error_comp(queue); 814 } 815 return; 816 } 817 818 /* 819 * Upon RDMA completion check the signature status 820 * - if succeeded send good NVMe response 821 * - if failed send bad NVMe response with appropriate error 822 */ 823 status = nvmet_rdma_check_pi_status(rsp->rw.reg->mr); 824 if (unlikely(status)) 825 rsp->req.cqe->status = cpu_to_le16(status << 1); 826 nvmet_rdma_rw_ctx_destroy(rsp); 827 828 if (unlikely(ib_post_send(cm_id->qp, &rsp->send_wr, NULL))) { 829 pr_err("sending cmd response failed\n"); 830 nvmet_rdma_release_rsp(rsp); 831 } 832 } 833 834 static void nvmet_rdma_use_inline_sg(struct nvmet_rdma_rsp *rsp, u32 len, 835 u64 off) 836 { 837 u64 page_off = off % PAGE_SIZE; 838 u64 page_idx = off / PAGE_SIZE; 839 int sg_count = num_pages(page_off + len); 840 struct scatterlist *sg; 841 int i; 842 843 sg = &rsp->cmd->inline_sg[page_idx]; 844 for (i = 0; i < sg_count; i++, sg++) { 845 if (i < sg_count - 1) 846 sg_unmark_end(sg); 847 else 848 sg_mark_end(sg); 849 sg->offset = page_off; 850 sg->length = min_t(u64, len, PAGE_SIZE - page_off); 851 len -= sg->length; 852 page_off = 0; 853 } 854 855 rsp->req.sg = &rsp->cmd->inline_sg[page_idx]; 856 rsp->req.sg_cnt = sg_count; 857 } 858 859 static u16 nvmet_rdma_map_sgl_inline(struct nvmet_rdma_rsp *rsp) 860 { 861 struct nvme_sgl_desc *sgl = &rsp->req.cmd->common.dptr.sgl; 862 u64 off = le64_to_cpu(sgl->addr); 863 u32 len = le32_to_cpu(sgl->length); 864 865 if (!nvme_is_write(rsp->req.cmd)) { 866 rsp->req.error_loc = 867 offsetof(struct nvme_common_command, opcode); 868 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 869 } 870 871 if (off > (u64)rsp->queue->dev->inline_data_size || 872 len > (u64)rsp->queue->dev->inline_data_size - off) { 873 pr_err("invalid inline data offset!\n"); 874 return NVME_SC_SGL_INVALID_OFFSET | NVME_STATUS_DNR; 875 } 876 877 /* no data command? */ 878 if (!len) 879 return 0; 880 881 nvmet_rdma_use_inline_sg(rsp, len, off); 882 rsp->flags |= NVMET_RDMA_REQ_INLINE_DATA; 883 rsp->req.transfer_len += len; 884 return 0; 885 } 886 887 static u16 nvmet_rdma_map_sgl_keyed(struct nvmet_rdma_rsp *rsp, 888 struct nvme_keyed_sgl_desc *sgl, bool invalidate) 889 { 890 u64 addr = le64_to_cpu(sgl->addr); 891 u32 key = get_unaligned_le32(sgl->key); 892 struct ib_sig_attrs sig_attrs; 893 int ret; 894 895 rsp->req.transfer_len = get_unaligned_le24(sgl->length); 896 897 /* no data command? */ 898 if (!rsp->req.transfer_len) 899 return 0; 900 901 if (rsp->req.metadata_len) 902 nvmet_rdma_set_sig_attrs(&rsp->req, &sig_attrs); 903 904 ret = nvmet_req_alloc_sgls(&rsp->req); 905 if (unlikely(ret < 0)) 906 goto error_out; 907 908 ret = nvmet_rdma_rw_ctx_init(rsp, addr, key, &sig_attrs); 909 if (unlikely(ret < 0)) 910 goto error_out; 911 rsp->n_rdma += ret; 912 913 if (invalidate) 914 rsp->invalidate_rkey = key; 915 916 return 0; 917 918 error_out: 919 rsp->req.transfer_len = 0; 920 return NVME_SC_INTERNAL; 921 } 922 923 static u16 nvmet_rdma_map_sgl(struct nvmet_rdma_rsp *rsp) 924 { 925 struct nvme_keyed_sgl_desc *sgl = &rsp->req.cmd->common.dptr.ksgl; 926 927 switch (sgl->type >> 4) { 928 case NVME_SGL_FMT_DATA_DESC: 929 switch (sgl->type & 0xf) { 930 case NVME_SGL_FMT_OFFSET: 931 return nvmet_rdma_map_sgl_inline(rsp); 932 default: 933 pr_err("invalid SGL subtype: %#x\n", sgl->type); 934 rsp->req.error_loc = 935 offsetof(struct nvme_common_command, dptr); 936 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 937 } 938 case NVME_KEY_SGL_FMT_DATA_DESC: 939 switch (sgl->type & 0xf) { 940 case NVME_SGL_FMT_ADDRESS | NVME_SGL_FMT_INVALIDATE: 941 return nvmet_rdma_map_sgl_keyed(rsp, sgl, true); 942 case NVME_SGL_FMT_ADDRESS: 943 return nvmet_rdma_map_sgl_keyed(rsp, sgl, false); 944 default: 945 pr_err("invalid SGL subtype: %#x\n", sgl->type); 946 rsp->req.error_loc = 947 offsetof(struct nvme_common_command, dptr); 948 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 949 } 950 default: 951 pr_err("invalid SGL type: %#x\n", sgl->type); 952 rsp->req.error_loc = offsetof(struct nvme_common_command, dptr); 953 return NVME_SC_SGL_INVALID_TYPE | NVME_STATUS_DNR; 954 } 955 } 956 957 static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp) 958 { 959 struct nvmet_rdma_queue *queue = rsp->queue; 960 961 if (unlikely(atomic_sub_return(1 + rsp->n_rdma, 962 &queue->sq_wr_avail) < 0)) { 963 pr_debug("IB send queue full (needed %d): queue %u cntlid %u\n", 964 1 + rsp->n_rdma, queue->idx, 965 queue->nvme_sq.ctrl->cntlid); 966 atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail); 967 return false; 968 } 969 970 if (nvmet_rdma_need_data_in(rsp)) { 971 if (rdma_rw_ctx_post(&rsp->rw, queue->qp, 972 queue->cm_id->port_num, &rsp->read_cqe, NULL)) 973 nvmet_req_complete(&rsp->req, NVME_SC_DATA_XFER_ERROR); 974 } else { 975 rsp->req.execute(&rsp->req); 976 } 977 978 return true; 979 } 980 981 static void nvmet_rdma_handle_command(struct nvmet_rdma_queue *queue, 982 struct nvmet_rdma_rsp *cmd) 983 { 984 unsigned long flags; 985 u16 status; 986 987 ib_dma_sync_single_for_cpu(queue->dev->device, 988 cmd->cmd->sge[0].addr, cmd->cmd->sge[0].length, 989 DMA_FROM_DEVICE); 990 ib_dma_sync_single_for_cpu(queue->dev->device, 991 cmd->send_sge.addr, cmd->send_sge.length, 992 DMA_TO_DEVICE); 993 994 if (!nvmet_req_init(&cmd->req, &queue->nvme_sq, &nvmet_rdma_ops)) 995 return; 996 997 status = nvmet_rdma_map_sgl(cmd); 998 if (status) 999 goto out_err; 1000 1001 if (unlikely(!nvmet_rdma_execute_command(cmd))) { 1002 spin_lock_irqsave(&queue->rsp_wr_wait_lock, flags); 1003 list_add_tail(&cmd->wait_list, &queue->rsp_wr_wait_list); 1004 spin_unlock_irqrestore(&queue->rsp_wr_wait_lock, flags); 1005 } 1006 1007 return; 1008 1009 out_err: 1010 nvmet_req_complete(&cmd->req, status); 1011 } 1012 1013 static bool nvmet_rdma_recv_not_live(struct nvmet_rdma_queue *queue, 1014 struct nvmet_rdma_rsp *rsp) 1015 { 1016 unsigned long flags; 1017 bool ret = true; 1018 1019 spin_lock_irqsave(&queue->state_lock, flags); 1020 /* 1021 * recheck queue state is not live to prevent a race condition 1022 * with RDMA_CM_EVENT_ESTABLISHED handler. 1023 */ 1024 if (queue->state == NVMET_RDMA_Q_LIVE) 1025 ret = false; 1026 else if (queue->state == NVMET_RDMA_Q_CONNECTING) 1027 list_add_tail(&rsp->wait_list, &queue->rsp_wait_list); 1028 else 1029 nvmet_rdma_put_rsp(rsp); 1030 spin_unlock_irqrestore(&queue->state_lock, flags); 1031 return ret; 1032 } 1033 1034 static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc) 1035 { 1036 struct nvmet_rdma_cmd *cmd = 1037 container_of(wc->wr_cqe, struct nvmet_rdma_cmd, cqe); 1038 struct nvmet_rdma_queue *queue = wc->qp->qp_context; 1039 struct nvmet_rdma_rsp *rsp; 1040 1041 if (unlikely(wc->status != IB_WC_SUCCESS)) { 1042 if (wc->status != IB_WC_WR_FLUSH_ERR) { 1043 pr_err("RECV for CQE 0x%p failed with status %s (%d)\n", 1044 wc->wr_cqe, ib_wc_status_msg(wc->status), 1045 wc->status); 1046 nvmet_rdma_error_comp(queue); 1047 } 1048 return; 1049 } 1050 1051 if (unlikely(wc->byte_len < sizeof(struct nvme_command))) { 1052 pr_err("Ctrl Fatal Error: capsule size less than 64 bytes\n"); 1053 nvmet_rdma_error_comp(queue); 1054 return; 1055 } 1056 1057 cmd->queue = queue; 1058 rsp = nvmet_rdma_get_rsp(queue); 1059 if (unlikely(!rsp)) { 1060 /* 1061 * we get here only under memory pressure, 1062 * silently drop and have the host retry 1063 * as we can't even fail it. 1064 */ 1065 nvmet_rdma_post_recv(queue->dev, cmd); 1066 return; 1067 } 1068 rsp->queue = queue; 1069 rsp->cmd = cmd; 1070 rsp->flags = 0; 1071 rsp->req.cmd = cmd->nvme_cmd; 1072 rsp->req.port = queue->port; 1073 rsp->n_rdma = 0; 1074 rsp->invalidate_rkey = 0; 1075 1076 if (unlikely(queue->state != NVMET_RDMA_Q_LIVE) && 1077 nvmet_rdma_recv_not_live(queue, rsp)) 1078 return; 1079 1080 nvmet_rdma_handle_command(queue, rsp); 1081 } 1082 1083 static void nvmet_rdma_destroy_srq(struct nvmet_rdma_srq *nsrq) 1084 { 1085 nvmet_rdma_free_cmds(nsrq->ndev, nsrq->cmds, nsrq->ndev->srq_size, 1086 false); 1087 ib_destroy_srq(nsrq->srq); 1088 1089 kfree(nsrq); 1090 } 1091 1092 static void nvmet_rdma_destroy_srqs(struct nvmet_rdma_device *ndev) 1093 { 1094 int i; 1095 1096 if (!ndev->srqs) 1097 return; 1098 1099 for (i = 0; i < ndev->srq_count; i++) 1100 nvmet_rdma_destroy_srq(ndev->srqs[i]); 1101 1102 kfree(ndev->srqs); 1103 } 1104 1105 static struct nvmet_rdma_srq * 1106 nvmet_rdma_init_srq(struct nvmet_rdma_device *ndev) 1107 { 1108 struct ib_srq_init_attr srq_attr = { NULL, }; 1109 size_t srq_size = ndev->srq_size; 1110 struct nvmet_rdma_srq *nsrq; 1111 struct ib_srq *srq; 1112 int ret, i; 1113 1114 nsrq = kzalloc_obj(*nsrq); 1115 if (!nsrq) 1116 return ERR_PTR(-ENOMEM); 1117 1118 srq_attr.attr.max_wr = srq_size; 1119 srq_attr.attr.max_sge = 1 + ndev->inline_page_count; 1120 srq_attr.attr.srq_limit = 0; 1121 srq_attr.srq_type = IB_SRQT_BASIC; 1122 srq = ib_create_srq(ndev->pd, &srq_attr); 1123 if (IS_ERR(srq)) { 1124 ret = PTR_ERR(srq); 1125 goto out_free; 1126 } 1127 1128 nsrq->cmds = nvmet_rdma_alloc_cmds(ndev, srq_size, false); 1129 if (IS_ERR(nsrq->cmds)) { 1130 ret = PTR_ERR(nsrq->cmds); 1131 goto out_destroy_srq; 1132 } 1133 1134 nsrq->srq = srq; 1135 nsrq->ndev = ndev; 1136 1137 for (i = 0; i < srq_size; i++) { 1138 nsrq->cmds[i].nsrq = nsrq; 1139 ret = nvmet_rdma_post_recv(ndev, &nsrq->cmds[i]); 1140 if (ret) 1141 goto out_free_cmds; 1142 } 1143 1144 return nsrq; 1145 1146 out_free_cmds: 1147 nvmet_rdma_free_cmds(ndev, nsrq->cmds, srq_size, false); 1148 out_destroy_srq: 1149 ib_destroy_srq(srq); 1150 out_free: 1151 kfree(nsrq); 1152 return ERR_PTR(ret); 1153 } 1154 1155 static int nvmet_rdma_init_srqs(struct nvmet_rdma_device *ndev) 1156 { 1157 int i, ret; 1158 1159 if (!ndev->device->attrs.max_srq_wr || !ndev->device->attrs.max_srq) { 1160 /* 1161 * If SRQs aren't supported we just go ahead and use normal 1162 * non-shared receive queues. 1163 */ 1164 pr_info("SRQ requested but not supported.\n"); 1165 return 0; 1166 } 1167 1168 ndev->srq_size = min(ndev->device->attrs.max_srq_wr, 1169 nvmet_rdma_srq_size); 1170 ndev->srq_count = min_t(u32, ndev->device->num_comp_vectors, 1171 ndev->device->attrs.max_srq); 1172 1173 ndev->srqs = kzalloc_objs(*ndev->srqs, ndev->srq_count); 1174 if (!ndev->srqs) 1175 return -ENOMEM; 1176 1177 for (i = 0; i < ndev->srq_count; i++) { 1178 ndev->srqs[i] = nvmet_rdma_init_srq(ndev); 1179 if (IS_ERR(ndev->srqs[i])) { 1180 ret = PTR_ERR(ndev->srqs[i]); 1181 goto err_srq; 1182 } 1183 } 1184 1185 return 0; 1186 1187 err_srq: 1188 while (--i >= 0) 1189 nvmet_rdma_destroy_srq(ndev->srqs[i]); 1190 kfree(ndev->srqs); 1191 return ret; 1192 } 1193 1194 static void nvmet_rdma_free_dev(struct kref *ref) 1195 { 1196 struct nvmet_rdma_device *ndev = 1197 container_of(ref, struct nvmet_rdma_device, ref); 1198 1199 nvmet_rdma_destroy_srqs(ndev); 1200 ib_dealloc_pd(ndev->pd); 1201 1202 /* 1203 * Only delete from the device list once all RDMA resources are torn 1204 * down because nvmet_rdma_remove_one waits for the release work only 1205 * if the device still is on the list. 1206 */ 1207 mutex_lock(&device_list_mutex); 1208 list_del(&ndev->entry); 1209 mutex_unlock(&device_list_mutex); 1210 1211 kfree(ndev); 1212 } 1213 1214 static struct nvmet_rdma_device * 1215 nvmet_rdma_find_get_device(struct rdma_cm_id *cm_id) 1216 { 1217 struct nvmet_rdma_port *port = cm_id->context; 1218 struct nvmet_port *nport = port->nport; 1219 struct nvmet_rdma_device *ndev; 1220 int inline_page_count; 1221 u32 inline_sge_count; 1222 int ret; 1223 1224 mutex_lock(&device_list_mutex); 1225 list_for_each_entry(ndev, &device_list, entry) { 1226 if (ndev->device->node_guid == cm_id->device->node_guid && 1227 kref_get_unless_zero(&ndev->ref)) 1228 goto out_unlock; 1229 } 1230 1231 ndev = kzalloc_obj(*ndev); 1232 if (!ndev) 1233 goto out_err; 1234 1235 inline_page_count = num_pages(nport->inline_data_size); 1236 inline_sge_count = max(cm_id->device->attrs.max_sge_rd, 1237 cm_id->device->attrs.max_recv_sge); 1238 if (inline_sge_count) 1239 inline_sge_count--; 1240 if (inline_page_count > inline_sge_count) { 1241 pr_warn("inline_data_size %d cannot be supported by device %s. Reducing to %lu.\n", 1242 nport->inline_data_size, cm_id->device->name, 1243 inline_sge_count * PAGE_SIZE); 1244 nport->inline_data_size = inline_sge_count * PAGE_SIZE; 1245 inline_page_count = inline_sge_count; 1246 } 1247 ndev->inline_data_size = nport->inline_data_size; 1248 ndev->inline_page_count = inline_page_count; 1249 1250 if (nport->pi_enable && !(cm_id->device->attrs.kernel_cap_flags & 1251 IBK_INTEGRITY_HANDOVER)) { 1252 pr_warn("T10-PI is not supported by device %s. Disabling it\n", 1253 cm_id->device->name); 1254 nport->pi_enable = false; 1255 } 1256 1257 ndev->device = cm_id->device; 1258 kref_init(&ndev->ref); 1259 1260 ndev->pd = ib_alloc_pd(ndev->device, 0); 1261 if (IS_ERR(ndev->pd)) 1262 goto out_free_dev; 1263 1264 if (nvmet_rdma_use_srq) { 1265 ret = nvmet_rdma_init_srqs(ndev); 1266 if (ret) 1267 goto out_free_pd; 1268 } 1269 1270 list_add(&ndev->entry, &device_list); 1271 out_unlock: 1272 mutex_unlock(&device_list_mutex); 1273 pr_debug("added %s.\n", ndev->device->name); 1274 return ndev; 1275 1276 out_free_pd: 1277 ib_dealloc_pd(ndev->pd); 1278 out_free_dev: 1279 kfree(ndev); 1280 out_err: 1281 mutex_unlock(&device_list_mutex); 1282 return NULL; 1283 } 1284 1285 static int nvmet_rdma_create_queue_ib(struct nvmet_rdma_queue *queue) 1286 { 1287 struct ib_qp_init_attr qp_attr = { }; 1288 struct nvmet_rdma_device *ndev = queue->dev; 1289 int nr_cqe, ret, i, factor; 1290 1291 /* 1292 * Reserve CQ slots for RECV + RDMA_READ/RDMA_WRITE + RDMA_SEND. 1293 */ 1294 nr_cqe = queue->recv_queue_size + 2 * queue->send_queue_size; 1295 1296 queue->cq = ib_cq_pool_get(ndev->device, nr_cqe + 1, 1297 queue->comp_vector, IB_POLL_WORKQUEUE); 1298 if (IS_ERR(queue->cq)) { 1299 ret = PTR_ERR(queue->cq); 1300 pr_err("failed to create CQ cqe= %d ret= %d\n", 1301 nr_cqe + 1, ret); 1302 goto out; 1303 } 1304 1305 qp_attr.qp_context = queue; 1306 qp_attr.event_handler = nvmet_rdma_qp_event; 1307 qp_attr.send_cq = queue->cq; 1308 qp_attr.recv_cq = queue->cq; 1309 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR; 1310 qp_attr.qp_type = IB_QPT_RC; 1311 /* +1 for drain */ 1312 qp_attr.cap.max_send_wr = queue->send_queue_size + 1; 1313 factor = rdma_rw_mr_factor(ndev->device, queue->cm_id->port_num, 1314 1 << NVMET_RDMA_MAX_MDTS); 1315 qp_attr.cap.max_rdma_ctxs = queue->send_queue_size * factor; 1316 qp_attr.cap.max_send_sge = max(ndev->device->attrs.max_sge_rd, 1317 ndev->device->attrs.max_send_sge); 1318 1319 if (queue->nsrq) { 1320 qp_attr.srq = queue->nsrq->srq; 1321 } else { 1322 /* +1 for drain */ 1323 qp_attr.cap.max_recv_wr = 1 + queue->recv_queue_size; 1324 qp_attr.cap.max_recv_sge = 1 + ndev->inline_page_count; 1325 } 1326 1327 if (queue->port->pi_enable && queue->host_qid) 1328 qp_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN; 1329 1330 ret = rdma_create_qp(queue->cm_id, ndev->pd, &qp_attr); 1331 if (ret) { 1332 pr_err("failed to create_qp ret= %d\n", ret); 1333 goto err_destroy_cq; 1334 } 1335 queue->qp = queue->cm_id->qp; 1336 1337 atomic_set(&queue->sq_wr_avail, qp_attr.cap.max_send_wr); 1338 1339 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n", 1340 __func__, queue->cq->cqe, qp_attr.cap.max_send_sge, 1341 qp_attr.cap.max_send_wr, queue->cm_id); 1342 1343 if (!queue->nsrq) { 1344 for (i = 0; i < queue->recv_queue_size; i++) { 1345 queue->cmds[i].queue = queue; 1346 ret = nvmet_rdma_post_recv(ndev, &queue->cmds[i]); 1347 if (ret) 1348 goto err_destroy_qp; 1349 } 1350 } 1351 1352 out: 1353 return ret; 1354 1355 err_destroy_qp: 1356 rdma_destroy_qp(queue->cm_id); 1357 err_destroy_cq: 1358 ib_cq_pool_put(queue->cq, nr_cqe + 1); 1359 goto out; 1360 } 1361 1362 static bool nvmet_rdma_reclaim_rsp(struct sbitmap *sb, unsigned int bitnr, 1363 void *data) 1364 { 1365 struct nvmet_rdma_queue *queue = data; 1366 1367 nvmet_rdma_free_rsp_resources(&queue->rsps[bitnr]); 1368 1369 return true; 1370 } 1371 1372 static void nvmet_rdma_destroy_queue_ib(struct nvmet_rdma_queue *queue) 1373 { 1374 ib_drain_qp(queue->qp); 1375 1376 /* 1377 * Reclaim resources of a response that is still in-flight when the 1378 * queue is being torn down. This happens when the connection was 1379 * forcefully disconnected while an I/O is in flight. 1380 */ 1381 sbitmap_for_each_set(&queue->rsp_tags, nvmet_rdma_reclaim_rsp, queue); 1382 1383 if (queue->cm_id) 1384 rdma_destroy_id(queue->cm_id); 1385 ib_destroy_qp(queue->qp); 1386 ib_cq_pool_put(queue->cq, queue->recv_queue_size + 2 * 1387 queue->send_queue_size + 1); 1388 } 1389 1390 static void nvmet_rdma_free_queue(struct nvmet_rdma_queue *queue) 1391 { 1392 pr_debug("freeing queue %d\n", queue->idx); 1393 1394 nvmet_sq_destroy(&queue->nvme_sq); 1395 nvmet_cq_put(&queue->nvme_cq); 1396 1397 nvmet_rdma_destroy_queue_ib(queue); 1398 if (!queue->nsrq) { 1399 nvmet_rdma_free_cmds(queue->dev, queue->cmds, 1400 queue->recv_queue_size, 1401 !queue->host_qid); 1402 } 1403 nvmet_rdma_free_rsps(queue); 1404 ida_free(&nvmet_rdma_queue_ida, queue->idx); 1405 kfree(queue); 1406 } 1407 1408 static void nvmet_rdma_release_queue_work(struct work_struct *w) 1409 { 1410 struct nvmet_rdma_queue *queue = 1411 container_of(w, struct nvmet_rdma_queue, release_work); 1412 struct nvmet_rdma_device *dev = queue->dev; 1413 1414 nvmet_rdma_free_queue(queue); 1415 1416 kref_put(&dev->ref, nvmet_rdma_free_dev); 1417 } 1418 1419 static int 1420 nvmet_rdma_parse_cm_connect_req(struct rdma_conn_param *conn, 1421 struct nvmet_rdma_queue *queue) 1422 { 1423 struct nvme_rdma_cm_req *req; 1424 1425 req = (struct nvme_rdma_cm_req *)conn->private_data; 1426 if (!req || conn->private_data_len < sizeof(*req)) 1427 return NVME_RDMA_CM_INVALID_LEN; 1428 1429 if (le16_to_cpu(req->recfmt) != NVME_RDMA_CM_FMT_1_0) 1430 return NVME_RDMA_CM_INVALID_RECFMT; 1431 1432 queue->host_qid = le16_to_cpu(req->qid); 1433 1434 /* 1435 * req->hsqsize corresponds to our recv queue size plus 1 1436 * req->hrqsize corresponds to our send queue size 1437 */ 1438 queue->recv_queue_size = le16_to_cpu(req->hsqsize) + 1; 1439 queue->send_queue_size = le16_to_cpu(req->hrqsize); 1440 1441 if (!queue->host_qid && queue->recv_queue_size > NVME_AQ_DEPTH) 1442 return NVME_RDMA_CM_INVALID_HSQSIZE; 1443 1444 /* XXX: Should we enforce some kind of max for IO queues? */ 1445 1446 return 0; 1447 } 1448 1449 static int nvmet_rdma_cm_reject(struct rdma_cm_id *cm_id, 1450 enum nvme_rdma_cm_status status) 1451 { 1452 struct nvme_rdma_cm_rej rej; 1453 1454 pr_debug("rejecting connect request: status %d (%s)\n", 1455 status, nvme_rdma_cm_msg(status)); 1456 1457 rej.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); 1458 rej.sts = cpu_to_le16(status); 1459 1460 return rdma_reject(cm_id, (void *)&rej, sizeof(rej), 1461 IB_CM_REJ_CONSUMER_DEFINED); 1462 } 1463 1464 static struct nvmet_rdma_queue * 1465 nvmet_rdma_alloc_queue(struct nvmet_rdma_device *ndev, 1466 struct rdma_cm_id *cm_id, 1467 struct rdma_cm_event *event) 1468 { 1469 struct nvmet_rdma_port *port = cm_id->context; 1470 struct nvmet_rdma_queue *queue; 1471 int ret; 1472 1473 queue = kzalloc_obj(*queue); 1474 if (!queue) { 1475 ret = NVME_RDMA_CM_NO_RSC; 1476 goto out_reject; 1477 } 1478 1479 nvmet_cq_init(&queue->nvme_cq); 1480 ret = nvmet_sq_init(&queue->nvme_sq, &queue->nvme_cq); 1481 if (ret) { 1482 ret = NVME_RDMA_CM_NO_RSC; 1483 goto out_free_queue; 1484 } 1485 1486 ret = nvmet_rdma_parse_cm_connect_req(&event->param.conn, queue); 1487 if (ret) 1488 goto out_destroy_sq; 1489 1490 /* 1491 * Schedules the actual release because calling rdma_destroy_id from 1492 * inside a CM callback would trigger a deadlock. (great API design..) 1493 */ 1494 INIT_WORK(&queue->release_work, nvmet_rdma_release_queue_work); 1495 queue->dev = ndev; 1496 queue->cm_id = cm_id; 1497 queue->port = port->nport; 1498 1499 spin_lock_init(&queue->state_lock); 1500 queue->state = NVMET_RDMA_Q_CONNECTING; 1501 INIT_LIST_HEAD(&queue->rsp_wait_list); 1502 INIT_LIST_HEAD(&queue->rsp_wr_wait_list); 1503 spin_lock_init(&queue->rsp_wr_wait_lock); 1504 INIT_LIST_HEAD(&queue->queue_list); 1505 1506 queue->idx = ida_alloc(&nvmet_rdma_queue_ida, GFP_KERNEL); 1507 if (queue->idx < 0) { 1508 ret = NVME_RDMA_CM_NO_RSC; 1509 goto out_destroy_sq; 1510 } 1511 1512 /* 1513 * Spread the io queues across completion vectors, 1514 * but still keep all admin queues on vector 0. 1515 */ 1516 queue->comp_vector = !queue->host_qid ? 0 : 1517 queue->idx % ndev->device->num_comp_vectors; 1518 1519 1520 ret = nvmet_rdma_alloc_rsps(queue); 1521 if (ret) { 1522 ret = NVME_RDMA_CM_NO_RSC; 1523 goto out_ida_remove; 1524 } 1525 1526 if (ndev->srqs) { 1527 queue->nsrq = ndev->srqs[queue->comp_vector % ndev->srq_count]; 1528 } else { 1529 queue->cmds = nvmet_rdma_alloc_cmds(ndev, 1530 queue->recv_queue_size, 1531 !queue->host_qid); 1532 if (IS_ERR(queue->cmds)) { 1533 ret = NVME_RDMA_CM_NO_RSC; 1534 goto out_free_responses; 1535 } 1536 } 1537 1538 ret = nvmet_rdma_create_queue_ib(queue); 1539 if (ret) { 1540 pr_err("%s: creating RDMA queue failed (%d).\n", 1541 __func__, ret); 1542 ret = NVME_RDMA_CM_NO_RSC; 1543 goto out_free_cmds; 1544 } 1545 1546 return queue; 1547 1548 out_free_cmds: 1549 if (!queue->nsrq) { 1550 nvmet_rdma_free_cmds(queue->dev, queue->cmds, 1551 queue->recv_queue_size, 1552 !queue->host_qid); 1553 } 1554 out_free_responses: 1555 nvmet_rdma_free_rsps(queue); 1556 out_ida_remove: 1557 ida_free(&nvmet_rdma_queue_ida, queue->idx); 1558 out_destroy_sq: 1559 nvmet_sq_destroy(&queue->nvme_sq); 1560 out_free_queue: 1561 nvmet_cq_put(&queue->nvme_cq); 1562 kfree(queue); 1563 out_reject: 1564 nvmet_rdma_cm_reject(cm_id, ret); 1565 return NULL; 1566 } 1567 1568 static void nvmet_rdma_qp_event(struct ib_event *event, void *priv) 1569 { 1570 struct nvmet_rdma_queue *queue = priv; 1571 1572 switch (event->event) { 1573 case IB_EVENT_COMM_EST: 1574 rdma_notify(queue->cm_id, event->event); 1575 break; 1576 case IB_EVENT_QP_LAST_WQE_REACHED: 1577 pr_debug("received last WQE reached event for queue=0x%p\n", 1578 queue); 1579 break; 1580 default: 1581 pr_err("received IB QP event: %s (%d)\n", 1582 ib_event_msg(event->event), event->event); 1583 break; 1584 } 1585 } 1586 1587 static int nvmet_rdma_cm_accept(struct rdma_cm_id *cm_id, 1588 struct nvmet_rdma_queue *queue, 1589 struct rdma_conn_param *p) 1590 { 1591 struct rdma_conn_param param = { }; 1592 struct nvme_rdma_cm_rep priv = { }; 1593 int ret = -ENOMEM; 1594 1595 param.rnr_retry_count = 7; 1596 param.flow_control = 1; 1597 param.initiator_depth = min3(p->initiator_depth, 1598 queue->dev->device->attrs.max_qp_init_rd_atom, 1599 U8_MAX); 1600 param.private_data = &priv; 1601 param.private_data_len = sizeof(priv); 1602 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0); 1603 priv.crqsize = cpu_to_le16(queue->recv_queue_size); 1604 1605 ret = rdma_accept(cm_id, ¶m); 1606 if (ret) 1607 pr_err("rdma_accept failed (error code = %d)\n", ret); 1608 1609 return ret; 1610 } 1611 1612 static int nvmet_rdma_queue_connect(struct rdma_cm_id *cm_id, 1613 struct rdma_cm_event *event) 1614 { 1615 struct nvmet_rdma_device *ndev; 1616 struct nvmet_rdma_queue *queue; 1617 int ret = -EINVAL; 1618 1619 ndev = nvmet_rdma_find_get_device(cm_id); 1620 if (!ndev) { 1621 nvmet_rdma_cm_reject(cm_id, NVME_RDMA_CM_NO_RSC); 1622 return -ECONNREFUSED; 1623 } 1624 1625 queue = nvmet_rdma_alloc_queue(ndev, cm_id, event); 1626 if (!queue) { 1627 ret = -ENOMEM; 1628 goto put_device; 1629 } 1630 1631 if (queue->host_qid == 0) { 1632 struct nvmet_rdma_queue *q; 1633 int pending = 0; 1634 1635 /* Check for pending controller teardown */ 1636 mutex_lock(&nvmet_rdma_queue_mutex); 1637 list_for_each_entry(q, &nvmet_rdma_queue_list, queue_list) { 1638 if (q->nvme_sq.ctrl == queue->nvme_sq.ctrl && 1639 q->state == NVMET_RDMA_Q_DISCONNECTING) 1640 pending++; 1641 } 1642 mutex_unlock(&nvmet_rdma_queue_mutex); 1643 if (pending > NVMET_RDMA_BACKLOG) { 1644 ret = NVME_SC_CONNECT_CTRL_BUSY; 1645 goto free_queue; 1646 } 1647 } 1648 1649 ret = nvmet_rdma_cm_accept(cm_id, queue, &event->param.conn); 1650 if (ret) 1651 goto free_queue; 1652 1653 mutex_lock(&nvmet_rdma_queue_mutex); 1654 list_add_tail(&queue->queue_list, &nvmet_rdma_queue_list); 1655 mutex_unlock(&nvmet_rdma_queue_mutex); 1656 1657 return 0; 1658 1659 free_queue: 1660 /* 1661 * Don't destroy the cm_id in free path, as we implicitly 1662 * destroy the cm_id here with non-zero ret code. 1663 */ 1664 queue->cm_id = NULL; 1665 nvmet_rdma_free_queue(queue); 1666 put_device: 1667 kref_put(&ndev->ref, nvmet_rdma_free_dev); 1668 1669 return ret; 1670 } 1671 1672 static void nvmet_rdma_queue_established(struct nvmet_rdma_queue *queue) 1673 { 1674 unsigned long flags; 1675 1676 spin_lock_irqsave(&queue->state_lock, flags); 1677 if (queue->state != NVMET_RDMA_Q_CONNECTING) { 1678 pr_warn("trying to establish a connected queue\n"); 1679 goto out_unlock; 1680 } 1681 queue->state = NVMET_RDMA_Q_LIVE; 1682 1683 while (!list_empty(&queue->rsp_wait_list)) { 1684 struct nvmet_rdma_rsp *cmd; 1685 1686 cmd = list_first_entry(&queue->rsp_wait_list, 1687 struct nvmet_rdma_rsp, wait_list); 1688 list_del(&cmd->wait_list); 1689 1690 spin_unlock_irqrestore(&queue->state_lock, flags); 1691 nvmet_rdma_handle_command(queue, cmd); 1692 spin_lock_irqsave(&queue->state_lock, flags); 1693 } 1694 1695 out_unlock: 1696 spin_unlock_irqrestore(&queue->state_lock, flags); 1697 } 1698 1699 static void __nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue) 1700 { 1701 bool disconnect = false, live = false; 1702 unsigned long flags; 1703 1704 pr_debug("cm_id= %p queue->state= %d\n", queue->cm_id, queue->state); 1705 1706 spin_lock_irqsave(&queue->state_lock, flags); 1707 switch (queue->state) { 1708 case NVMET_RDMA_Q_CONNECTING: 1709 while (!list_empty(&queue->rsp_wait_list)) { 1710 struct nvmet_rdma_rsp *rsp; 1711 1712 rsp = list_first_entry(&queue->rsp_wait_list, 1713 struct nvmet_rdma_rsp, 1714 wait_list); 1715 list_del(&rsp->wait_list); 1716 nvmet_rdma_put_rsp(rsp); 1717 } 1718 fallthrough; 1719 case NVMET_RDMA_Q_LIVE: 1720 live = queue->state == NVMET_RDMA_Q_LIVE; 1721 queue->state = NVMET_RDMA_Q_DISCONNECTING; 1722 disconnect = true; 1723 break; 1724 case NVMET_RDMA_Q_DISCONNECTING: 1725 break; 1726 } 1727 spin_unlock_irqrestore(&queue->state_lock, flags); 1728 1729 if (disconnect) { 1730 /* 1731 * Over iWARP rdma_disconnect() sleeps until the CM has handled 1732 * the ESTABLISHED event of this connection. The CM handles the 1733 * events of all connections one at a time, and our handler 1734 * takes nvmet_rdma_queue_mutex, which callers of this function 1735 * hold. So a queue that has not seen the event is left to the 1736 * release work: ib_drain_qp() moves the queue pair to the error 1737 * state, which ends the connection. 1738 */ 1739 if (live) 1740 rdma_disconnect(queue->cm_id); 1741 queue_work(nvmet_wq, &queue->release_work); 1742 } 1743 } 1744 1745 static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue) 1746 { 1747 /* 1748 * Queue the release work with &nvmet_rdma_queue_mutex held, so that 1749 * nvmet_rdma_remove_one() either finds the queue on the list or the 1750 * work has already been queued. 1751 */ 1752 mutex_lock(&nvmet_rdma_queue_mutex); 1753 if (!list_empty(&queue->queue_list)) { 1754 list_del_init(&queue->queue_list); 1755 __nvmet_rdma_queue_disconnect(queue); 1756 } 1757 mutex_unlock(&nvmet_rdma_queue_mutex); 1758 } 1759 1760 static void nvmet_rdma_queue_connect_fail(struct rdma_cm_id *cm_id, 1761 struct nvmet_rdma_queue *queue) 1762 { 1763 WARN_ON_ONCE(queue->state != NVMET_RDMA_Q_CONNECTING); 1764 1765 mutex_lock(&nvmet_rdma_queue_mutex); 1766 if (!list_empty(&queue->queue_list)) 1767 list_del_init(&queue->queue_list); 1768 mutex_unlock(&nvmet_rdma_queue_mutex); 1769 1770 pr_err("failed to connect queue %d\n", queue->idx); 1771 queue_work(nvmet_wq, &queue->release_work); 1772 } 1773 1774 /** 1775 * nvmet_rdma_device_removal() - Handle RDMA device removal 1776 * @cm_id: rdma_cm id, used for nvmet port 1777 * @queue: nvmet rdma queue (cm id qp_context) 1778 * 1779 * DEVICE_REMOVAL event notifies us that the RDMA device is about 1780 * to unplug. Note that this event can be generated on a normal 1781 * queue cm_id and/or a device bound listener cm_id (where in this 1782 * case queue will be null). 1783 * 1784 * The RDMA CM waits until each cm_id of the device is destroyed, so a queue 1785 * has to go now. Our ib_client also removes the queues, but it cannot be 1786 * relied on here: built in, it is called after the RDMA CM's client. 1787 * 1788 * For a listening port cm_id we nullify the priv to prevent double cm_id 1789 * destruction and destroying the cm_id implicitly by returning a non-zero 1790 * rc to the callout. 1791 */ 1792 static int nvmet_rdma_device_removal(struct rdma_cm_id *cm_id, 1793 struct nvmet_rdma_queue *queue) 1794 { 1795 struct nvmet_rdma_port *port; 1796 1797 if (queue) { 1798 nvmet_rdma_queue_disconnect(queue); 1799 return 0; 1800 } 1801 1802 port = cm_id->context; 1803 1804 /* 1805 * This is a listener cm_id. Make sure that 1806 * future remove_port won't invoke a double 1807 * cm_id destroy. use atomic xchg to make sure 1808 * we don't compete with remove_port. 1809 */ 1810 if (xchg(&port->cm_id, NULL) != cm_id) 1811 return 0; 1812 1813 /* 1814 * We need to return 1 so that the core will destroy 1815 * its own ID. What a great API design.. 1816 */ 1817 return 1; 1818 } 1819 1820 static int nvmet_rdma_cm_handler(struct rdma_cm_id *cm_id, 1821 struct rdma_cm_event *event) 1822 { 1823 struct nvmet_rdma_queue *queue = NULL; 1824 int ret = 0; 1825 1826 if (cm_id->qp) 1827 queue = cm_id->qp->qp_context; 1828 1829 pr_debug("%s (%d): status %d id %p\n", 1830 rdma_event_msg(event->event), event->event, 1831 event->status, cm_id); 1832 1833 switch (event->event) { 1834 case RDMA_CM_EVENT_CONNECT_REQUEST: 1835 ret = nvmet_rdma_queue_connect(cm_id, event); 1836 break; 1837 case RDMA_CM_EVENT_ESTABLISHED: 1838 nvmet_rdma_queue_established(queue); 1839 break; 1840 case RDMA_CM_EVENT_ADDR_CHANGE: 1841 if (!queue) { 1842 struct nvmet_rdma_port *port = cm_id->context; 1843 1844 queue_delayed_work(nvmet_wq, &port->repair_work, 0); 1845 break; 1846 } 1847 fallthrough; 1848 case RDMA_CM_EVENT_DISCONNECTED: 1849 case RDMA_CM_EVENT_TIMEWAIT_EXIT: 1850 nvmet_rdma_queue_disconnect(queue); 1851 break; 1852 case RDMA_CM_EVENT_DEVICE_REMOVAL: 1853 ret = nvmet_rdma_device_removal(cm_id, queue); 1854 break; 1855 case RDMA_CM_EVENT_REJECTED: 1856 pr_debug("Connection rejected: %s\n", 1857 rdma_reject_msg(cm_id, event->status)); 1858 fallthrough; 1859 case RDMA_CM_EVENT_UNREACHABLE: 1860 case RDMA_CM_EVENT_CONNECT_ERROR: 1861 nvmet_rdma_queue_connect_fail(cm_id, queue); 1862 break; 1863 default: 1864 pr_err("received unrecognized RDMA CM event %d\n", 1865 event->event); 1866 break; 1867 } 1868 1869 return ret; 1870 } 1871 1872 static void nvmet_rdma_delete_ctrl(struct nvmet_ctrl *ctrl) 1873 { 1874 struct nvmet_rdma_queue *queue, *n; 1875 1876 mutex_lock(&nvmet_rdma_queue_mutex); 1877 list_for_each_entry_safe(queue, n, &nvmet_rdma_queue_list, queue_list) { 1878 if (queue->nvme_sq.ctrl != ctrl) 1879 continue; 1880 list_del_init(&queue->queue_list); 1881 __nvmet_rdma_queue_disconnect(queue); 1882 } 1883 mutex_unlock(&nvmet_rdma_queue_mutex); 1884 } 1885 1886 static void nvmet_rdma_destroy_port_queues(struct nvmet_rdma_port *port) 1887 { 1888 struct nvmet_rdma_queue *queue, *tmp; 1889 struct nvmet_port *nport = port->nport; 1890 1891 mutex_lock(&nvmet_rdma_queue_mutex); 1892 list_for_each_entry_safe(queue, tmp, &nvmet_rdma_queue_list, 1893 queue_list) { 1894 if (queue->port != nport) 1895 continue; 1896 1897 list_del_init(&queue->queue_list); 1898 __nvmet_rdma_queue_disconnect(queue); 1899 } 1900 mutex_unlock(&nvmet_rdma_queue_mutex); 1901 } 1902 1903 static void nvmet_rdma_disable_port(struct nvmet_rdma_port *port) 1904 { 1905 struct rdma_cm_id *cm_id = xchg(&port->cm_id, NULL); 1906 1907 if (cm_id) 1908 rdma_destroy_id(cm_id); 1909 1910 /* 1911 * Destroy the remaining queues, which are not belong to any 1912 * controller yet. Do it here after the RDMA-CM was destroyed 1913 * guarantees that no new queue will be created. 1914 */ 1915 nvmet_rdma_destroy_port_queues(port); 1916 } 1917 1918 static int nvmet_rdma_enable_port(struct nvmet_rdma_port *port) 1919 { 1920 struct sockaddr *addr = (struct sockaddr *)&port->addr; 1921 struct rdma_cm_id *cm_id; 1922 int ret; 1923 1924 cm_id = rdma_create_id(&init_net, nvmet_rdma_cm_handler, port, 1925 RDMA_PS_TCP, IB_QPT_RC); 1926 if (IS_ERR(cm_id)) { 1927 pr_err("CM ID creation failed\n"); 1928 return PTR_ERR(cm_id); 1929 } 1930 1931 /* 1932 * Allow both IPv4 and IPv6 sockets to bind a single port 1933 * at the same time. 1934 */ 1935 ret = rdma_set_afonly(cm_id, 1); 1936 if (ret) { 1937 pr_err("rdma_set_afonly failed (%d)\n", ret); 1938 goto out_destroy_id; 1939 } 1940 1941 ret = rdma_bind_addr(cm_id, addr); 1942 if (ret) { 1943 pr_err("binding CM ID to %pISpcs failed (%d)\n", addr, ret); 1944 goto out_destroy_id; 1945 } 1946 1947 ret = rdma_listen(cm_id, NVMET_RDMA_BACKLOG); 1948 if (ret) { 1949 pr_err("listening to %pISpcs failed (%d)\n", addr, ret); 1950 goto out_destroy_id; 1951 } 1952 1953 port->cm_id = cm_id; 1954 return 0; 1955 1956 out_destroy_id: 1957 rdma_destroy_id(cm_id); 1958 return ret; 1959 } 1960 1961 static void nvmet_rdma_repair_port_work(struct work_struct *w) 1962 { 1963 struct nvmet_rdma_port *port = container_of(to_delayed_work(w), 1964 struct nvmet_rdma_port, repair_work); 1965 int ret; 1966 1967 nvmet_rdma_disable_port(port); 1968 ret = nvmet_rdma_enable_port(port); 1969 if (ret) 1970 queue_delayed_work(nvmet_wq, &port->repair_work, 5 * HZ); 1971 } 1972 1973 static int nvmet_rdma_add_port(struct nvmet_port *nport) 1974 { 1975 struct nvmet_rdma_port *port; 1976 __kernel_sa_family_t af; 1977 int ret; 1978 1979 port = kzalloc_obj(*port); 1980 if (!port) 1981 return -ENOMEM; 1982 1983 nport->priv = port; 1984 port->nport = nport; 1985 INIT_DELAYED_WORK(&port->repair_work, nvmet_rdma_repair_port_work); 1986 1987 switch (nport->disc_addr.adrfam) { 1988 case NVMF_ADDR_FAMILY_IP4: 1989 af = AF_INET; 1990 break; 1991 case NVMF_ADDR_FAMILY_IP6: 1992 af = AF_INET6; 1993 break; 1994 default: 1995 pr_err("address family %d not supported\n", 1996 nport->disc_addr.adrfam); 1997 ret = -EINVAL; 1998 goto out_free_port; 1999 } 2000 2001 if (nport->inline_data_size < 0) { 2002 nport->inline_data_size = NVMET_RDMA_DEFAULT_INLINE_DATA_SIZE; 2003 } else if (nport->inline_data_size > NVMET_RDMA_MAX_INLINE_DATA_SIZE) { 2004 pr_warn("inline_data_size %u is too large, reducing to %u\n", 2005 nport->inline_data_size, 2006 NVMET_RDMA_MAX_INLINE_DATA_SIZE); 2007 nport->inline_data_size = NVMET_RDMA_MAX_INLINE_DATA_SIZE; 2008 } 2009 2010 if (nport->max_queue_size < 0) { 2011 nport->max_queue_size = NVME_RDMA_DEFAULT_QUEUE_SIZE; 2012 } else if (nport->max_queue_size > NVME_RDMA_MAX_QUEUE_SIZE) { 2013 pr_warn("max_queue_size %u is too large, reducing to %u\n", 2014 nport->max_queue_size, NVME_RDMA_MAX_QUEUE_SIZE); 2015 nport->max_queue_size = NVME_RDMA_MAX_QUEUE_SIZE; 2016 } 2017 2018 ret = inet_pton_with_scope(&init_net, af, nport->disc_addr.traddr, 2019 nport->disc_addr.trsvcid, &port->addr); 2020 if (ret) { 2021 pr_err("malformed ip/port passed: %s:%s\n", 2022 nport->disc_addr.traddr, nport->disc_addr.trsvcid); 2023 goto out_free_port; 2024 } 2025 2026 ret = nvmet_rdma_enable_port(port); 2027 if (ret) 2028 goto out_free_port; 2029 2030 pr_info("enabling port %d (%pISpcs)\n", 2031 le16_to_cpu(nport->disc_addr.portid), 2032 (struct sockaddr *)&port->addr); 2033 2034 return 0; 2035 2036 out_free_port: 2037 kfree(port); 2038 return ret; 2039 } 2040 2041 static void nvmet_rdma_remove_port(struct nvmet_port *nport) 2042 { 2043 struct nvmet_rdma_port *port = nport->priv; 2044 2045 cancel_delayed_work_sync(&port->repair_work); 2046 nvmet_rdma_disable_port(port); 2047 kfree(port); 2048 } 2049 2050 static void nvmet_rdma_disc_port_addr(struct nvmet_req *req, 2051 struct nvmet_port *nport, char *traddr) 2052 { 2053 struct nvmet_rdma_port *port = nport->priv; 2054 struct rdma_cm_id *cm_id = port->cm_id; 2055 2056 if (inet_addr_is_any(&cm_id->route.addr.src_addr)) { 2057 struct nvmet_rdma_rsp *rsp = 2058 container_of(req, struct nvmet_rdma_rsp, req); 2059 struct rdma_cm_id *req_cm_id = rsp->queue->cm_id; 2060 struct sockaddr *addr = (void *)&req_cm_id->route.addr.src_addr; 2061 2062 sprintf(traddr, "%pISc", addr); 2063 } else { 2064 memcpy(traddr, nport->disc_addr.traddr, NVMF_TRADDR_SIZE); 2065 } 2066 } 2067 2068 static ssize_t nvmet_rdma_host_port_addr(struct nvmet_ctrl *ctrl, 2069 char *traddr, size_t traddr_len) 2070 { 2071 struct nvmet_sq *nvme_sq = ctrl->sqs[0]; 2072 struct nvmet_rdma_queue *queue = 2073 container_of(nvme_sq, struct nvmet_rdma_queue, nvme_sq); 2074 2075 return snprintf(traddr, traddr_len, "%pISc", 2076 (struct sockaddr *)&queue->cm_id->route.addr.dst_addr); 2077 } 2078 2079 static u8 nvmet_rdma_get_mdts(const struct nvmet_ctrl *ctrl) 2080 { 2081 if (ctrl->pi_support) 2082 return NVMET_RDMA_MAX_METADATA_MDTS; 2083 return NVMET_RDMA_MAX_MDTS; 2084 } 2085 2086 static u16 nvmet_rdma_get_max_queue_size(const struct nvmet_ctrl *ctrl) 2087 { 2088 if (ctrl->pi_support) 2089 return NVME_RDMA_MAX_METADATA_QUEUE_SIZE; 2090 return NVME_RDMA_MAX_QUEUE_SIZE; 2091 } 2092 2093 static const struct nvmet_fabrics_ops nvmet_rdma_ops = { 2094 .owner = THIS_MODULE, 2095 .type = NVMF_TRTYPE_RDMA, 2096 .msdbd = 1, 2097 .flags = NVMF_KEYED_SGLS | NVMF_METADATA_SUPPORTED, 2098 .add_port = nvmet_rdma_add_port, 2099 .remove_port = nvmet_rdma_remove_port, 2100 .queue_response = nvmet_rdma_queue_response, 2101 .delete_ctrl = nvmet_rdma_delete_ctrl, 2102 .disc_traddr = nvmet_rdma_disc_port_addr, 2103 .host_traddr = nvmet_rdma_host_port_addr, 2104 .get_mdts = nvmet_rdma_get_mdts, 2105 .get_max_queue_size = nvmet_rdma_get_max_queue_size, 2106 }; 2107 2108 static void nvmet_rdma_remove_one(struct ib_device *ib_device, void *client_data) 2109 { 2110 struct nvmet_rdma_queue *queue, *tmp; 2111 struct nvmet_rdma_device *ndev; 2112 bool found = false; 2113 2114 mutex_lock(&device_list_mutex); 2115 list_for_each_entry(ndev, &device_list, entry) { 2116 if (ndev->device == ib_device) { 2117 found = true; 2118 break; 2119 } 2120 } 2121 mutex_unlock(&device_list_mutex); 2122 2123 if (!found) 2124 return; 2125 2126 /* 2127 * IB Device that is used by nvmet controllers is being removed, 2128 * delete all queues using this device. 2129 */ 2130 mutex_lock(&nvmet_rdma_queue_mutex); 2131 list_for_each_entry_safe(queue, tmp, &nvmet_rdma_queue_list, 2132 queue_list) { 2133 if (queue->dev->device != ib_device) 2134 continue; 2135 2136 pr_info("Removing queue %d\n", queue->idx); 2137 list_del_init(&queue->queue_list); 2138 __nvmet_rdma_queue_disconnect(queue); 2139 } 2140 mutex_unlock(&nvmet_rdma_queue_mutex); 2141 2142 flush_workqueue(nvmet_wq); 2143 flush_workqueue(nvmet_aen_wq); 2144 } 2145 2146 static struct ib_client nvmet_rdma_ib_client = { 2147 .name = "nvmet_rdma", 2148 .remove = nvmet_rdma_remove_one 2149 }; 2150 2151 static int __init nvmet_rdma_init(void) 2152 { 2153 int ret; 2154 2155 ret = ib_register_client(&nvmet_rdma_ib_client); 2156 if (ret) 2157 return ret; 2158 2159 ret = nvmet_register_transport(&nvmet_rdma_ops); 2160 if (ret) 2161 goto err_ib_client; 2162 2163 return 0; 2164 2165 err_ib_client: 2166 ib_unregister_client(&nvmet_rdma_ib_client); 2167 return ret; 2168 } 2169 2170 static void __exit nvmet_rdma_exit(void) 2171 { 2172 nvmet_unregister_transport(&nvmet_rdma_ops); 2173 ib_unregister_client(&nvmet_rdma_ib_client); 2174 /* 2175 * nvmet_rdma_remove_one() does not flush for a device that has left 2176 * device_list, and the release work that took it off still has to 2177 * return from nvmet_rdma_free_dev(). 2178 */ 2179 flush_workqueue(nvmet_wq); 2180 WARN_ON_ONCE(!list_empty(&nvmet_rdma_queue_list)); 2181 ida_destroy(&nvmet_rdma_queue_ida); 2182 } 2183 2184 module_init(nvmet_rdma_init); 2185 module_exit(nvmet_rdma_exit); 2186 2187 MODULE_DESCRIPTION("NVMe target RDMA transport driver"); 2188 MODULE_LICENSE("GPL v2"); 2189 MODULE_ALIAS("nvmet-transport-1"); /* 1 == NVMF_TRTYPE_RDMA */ 2190