1 // SPDX-License-Identifier: GPL-2.0
3 * NVMe over Fabrics RDMA target.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
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 <asm/unaligned.h>
21 #include <rdma/ib_verbs.h>
22 #include <rdma/rdma_cm.h>
24 #include <rdma/ib_cm.h>
26 #include <linux/nvme-rdma.h>
30 * We allow at least 1 page, up to 4 SGEs, and up to 16KB of inline data
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)
36 /* Assume mpsmin == device_page_size == 4KB */
37 #define NVMET_RDMA_MAX_MDTS 8
38 #define NVMET_RDMA_MAX_METADATA_MDTS 5
40 #define NVMET_RDMA_BACKLOG 128
42 struct nvmet_rdma_srq;
44 struct nvmet_rdma_cmd {
45 struct ib_sge sge[NVMET_RDMA_MAX_INLINE_SGE + 1];
48 struct scatterlist inline_sg[NVMET_RDMA_MAX_INLINE_SGE];
49 struct nvme_command *nvme_cmd;
50 struct nvmet_rdma_queue *queue;
51 struct nvmet_rdma_srq *nsrq;
55 NVMET_RDMA_REQ_INLINE_DATA = (1 << 0),
56 NVMET_RDMA_REQ_INVALIDATE_RKEY = (1 << 1),
59 struct nvmet_rdma_rsp {
60 struct ib_sge send_sge;
61 struct ib_cqe send_cqe;
62 struct ib_send_wr send_wr;
64 struct nvmet_rdma_cmd *cmd;
65 struct nvmet_rdma_queue *queue;
67 struct ib_cqe read_cqe;
68 struct ib_cqe write_cqe;
69 struct rdma_rw_ctx rw;
78 struct list_head wait_list;
79 struct list_head free_list;
82 enum nvmet_rdma_queue_state {
83 NVMET_RDMA_Q_CONNECTING,
85 NVMET_RDMA_Q_DISCONNECTING,
88 struct nvmet_rdma_queue {
89 struct rdma_cm_id *cm_id;
91 struct nvmet_port *port;
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;
101 struct nvmet_rdma_rsp *rsps;
102 struct list_head free_rsps;
103 spinlock_t rsps_lock;
104 struct nvmet_rdma_cmd *cmds;
106 struct work_struct release_work;
107 struct list_head rsp_wait_list;
108 struct list_head rsp_wr_wait_list;
109 spinlock_t rsp_wr_wait_lock;
117 struct list_head queue_list;
120 struct nvmet_rdma_port {
121 struct nvmet_port *nport;
122 struct sockaddr_storage addr;
123 struct rdma_cm_id *cm_id;
124 struct delayed_work repair_work;
127 struct nvmet_rdma_srq {
129 struct nvmet_rdma_cmd *cmds;
130 struct nvmet_rdma_device *ndev;
133 struct nvmet_rdma_device {
134 struct ib_device *device;
136 struct nvmet_rdma_srq **srqs;
140 struct list_head entry;
141 int inline_data_size;
142 int inline_page_count;
145 static bool nvmet_rdma_use_srq;
146 module_param_named(use_srq, nvmet_rdma_use_srq, bool, 0444);
147 MODULE_PARM_DESC(use_srq, "Use shared receive queue.");
149 static int srq_size_set(const char *val, const struct kernel_param *kp);
150 static const struct kernel_param_ops srq_size_ops = {
152 .get = param_get_int,
155 static int nvmet_rdma_srq_size = 1024;
156 module_param_cb(srq_size, &srq_size_ops, &nvmet_rdma_srq_size, 0644);
157 MODULE_PARM_DESC(srq_size, "set Shared Receive Queue (SRQ) size, should >= 256 (default: 1024)");
159 static DEFINE_IDA(nvmet_rdma_queue_ida);
160 static LIST_HEAD(nvmet_rdma_queue_list);
161 static DEFINE_MUTEX(nvmet_rdma_queue_mutex);
163 static LIST_HEAD(device_list);
164 static DEFINE_MUTEX(device_list_mutex);
166 static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp);
167 static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc);
168 static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
169 static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc);
170 static void nvmet_rdma_write_data_done(struct ib_cq *cq, struct ib_wc *wc);
171 static void nvmet_rdma_qp_event(struct ib_event *event, void *priv);
172 static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue);
173 static void nvmet_rdma_free_rsp(struct nvmet_rdma_device *ndev,
174 struct nvmet_rdma_rsp *r);
175 static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev,
176 struct nvmet_rdma_rsp *r);
178 static const struct nvmet_fabrics_ops nvmet_rdma_ops;
180 static int srq_size_set(const char *val, const struct kernel_param *kp)
184 ret = kstrtoint(val, 10, &n);
185 if (ret != 0 || n < 256)
188 return param_set_int(val, kp);
191 static int num_pages(int len)
193 return 1 + (((len - 1) & PAGE_MASK) >> PAGE_SHIFT);
196 static inline bool nvmet_rdma_need_data_in(struct nvmet_rdma_rsp *rsp)
198 return nvme_is_write(rsp->req.cmd) &&
199 rsp->req.transfer_len &&
200 !(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA);
203 static inline bool nvmet_rdma_need_data_out(struct nvmet_rdma_rsp *rsp)
205 return !nvme_is_write(rsp->req.cmd) &&
206 rsp->req.transfer_len &&
207 !rsp->req.cqe->status &&
208 !(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA);
211 static inline struct nvmet_rdma_rsp *
212 nvmet_rdma_get_rsp(struct nvmet_rdma_queue *queue)
214 struct nvmet_rdma_rsp *rsp;
217 spin_lock_irqsave(&queue->rsps_lock, flags);
218 rsp = list_first_entry_or_null(&queue->free_rsps,
219 struct nvmet_rdma_rsp, free_list);
221 list_del(&rsp->free_list);
222 spin_unlock_irqrestore(&queue->rsps_lock, flags);
224 if (unlikely(!rsp)) {
227 rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
230 ret = nvmet_rdma_alloc_rsp(queue->dev, rsp);
236 rsp->allocated = true;
243 nvmet_rdma_put_rsp(struct nvmet_rdma_rsp *rsp)
247 if (unlikely(rsp->allocated)) {
248 nvmet_rdma_free_rsp(rsp->queue->dev, rsp);
253 spin_lock_irqsave(&rsp->queue->rsps_lock, flags);
254 list_add_tail(&rsp->free_list, &rsp->queue->free_rsps);
255 spin_unlock_irqrestore(&rsp->queue->rsps_lock, flags);
258 static void nvmet_rdma_free_inline_pages(struct nvmet_rdma_device *ndev,
259 struct nvmet_rdma_cmd *c)
261 struct scatterlist *sg;
265 if (!ndev->inline_data_size)
271 for (i = 0; i < ndev->inline_page_count; i++, sg++, sge++) {
273 ib_dma_unmap_page(ndev->device, sge->addr,
274 sge->length, DMA_FROM_DEVICE);
276 __free_page(sg_page(sg));
280 static int nvmet_rdma_alloc_inline_pages(struct nvmet_rdma_device *ndev,
281 struct nvmet_rdma_cmd *c)
283 struct scatterlist *sg;
289 if (!ndev->inline_data_size)
293 sg_init_table(sg, ndev->inline_page_count);
295 len = ndev->inline_data_size;
297 for (i = 0; i < ndev->inline_page_count; i++, sg++, sge++) {
298 pg = alloc_page(GFP_KERNEL);
301 sg_assign_page(sg, pg);
302 sge->addr = ib_dma_map_page(ndev->device,
303 pg, 0, PAGE_SIZE, DMA_FROM_DEVICE);
304 if (ib_dma_mapping_error(ndev->device, sge->addr))
306 sge->length = min_t(int, len, PAGE_SIZE);
307 sge->lkey = ndev->pd->local_dma_lkey;
313 for (; i >= 0; i--, sg--, sge--) {
315 ib_dma_unmap_page(ndev->device, sge->addr,
316 sge->length, DMA_FROM_DEVICE);
318 __free_page(sg_page(sg));
323 static int nvmet_rdma_alloc_cmd(struct nvmet_rdma_device *ndev,
324 struct nvmet_rdma_cmd *c, bool admin)
326 /* NVMe command / RDMA RECV */
327 c->nvme_cmd = kmalloc(sizeof(*c->nvme_cmd), GFP_KERNEL);
331 c->sge[0].addr = ib_dma_map_single(ndev->device, c->nvme_cmd,
332 sizeof(*c->nvme_cmd), DMA_FROM_DEVICE);
333 if (ib_dma_mapping_error(ndev->device, c->sge[0].addr))
336 c->sge[0].length = sizeof(*c->nvme_cmd);
337 c->sge[0].lkey = ndev->pd->local_dma_lkey;
339 if (!admin && nvmet_rdma_alloc_inline_pages(ndev, c))
342 c->cqe.done = nvmet_rdma_recv_done;
344 c->wr.wr_cqe = &c->cqe;
345 c->wr.sg_list = c->sge;
346 c->wr.num_sge = admin ? 1 : ndev->inline_page_count + 1;
351 ib_dma_unmap_single(ndev->device, c->sge[0].addr,
352 sizeof(*c->nvme_cmd), DMA_FROM_DEVICE);
360 static void nvmet_rdma_free_cmd(struct nvmet_rdma_device *ndev,
361 struct nvmet_rdma_cmd *c, bool admin)
364 nvmet_rdma_free_inline_pages(ndev, c);
365 ib_dma_unmap_single(ndev->device, c->sge[0].addr,
366 sizeof(*c->nvme_cmd), DMA_FROM_DEVICE);
370 static struct nvmet_rdma_cmd *
371 nvmet_rdma_alloc_cmds(struct nvmet_rdma_device *ndev,
372 int nr_cmds, bool admin)
374 struct nvmet_rdma_cmd *cmds;
375 int ret = -EINVAL, i;
377 cmds = kcalloc(nr_cmds, sizeof(struct nvmet_rdma_cmd), GFP_KERNEL);
381 for (i = 0; i < nr_cmds; i++) {
382 ret = nvmet_rdma_alloc_cmd(ndev, cmds + i, admin);
391 nvmet_rdma_free_cmd(ndev, cmds + i, admin);
397 static void nvmet_rdma_free_cmds(struct nvmet_rdma_device *ndev,
398 struct nvmet_rdma_cmd *cmds, int nr_cmds, bool admin)
402 for (i = 0; i < nr_cmds; i++)
403 nvmet_rdma_free_cmd(ndev, cmds + i, admin);
407 static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev,
408 struct nvmet_rdma_rsp *r)
410 /* NVMe CQE / RDMA SEND */
411 r->req.cqe = kmalloc(sizeof(*r->req.cqe), GFP_KERNEL);
415 r->send_sge.addr = ib_dma_map_single(ndev->device, r->req.cqe,
416 sizeof(*r->req.cqe), DMA_TO_DEVICE);
417 if (ib_dma_mapping_error(ndev->device, r->send_sge.addr))
420 if (ib_dma_pci_p2p_dma_supported(ndev->device))
421 r->req.p2p_client = &ndev->device->dev;
422 r->send_sge.length = sizeof(*r->req.cqe);
423 r->send_sge.lkey = ndev->pd->local_dma_lkey;
425 r->send_cqe.done = nvmet_rdma_send_done;
427 r->send_wr.wr_cqe = &r->send_cqe;
428 r->send_wr.sg_list = &r->send_sge;
429 r->send_wr.num_sge = 1;
430 r->send_wr.send_flags = IB_SEND_SIGNALED;
432 /* Data In / RDMA READ */
433 r->read_cqe.done = nvmet_rdma_read_data_done;
434 /* Data Out / RDMA WRITE */
435 r->write_cqe.done = nvmet_rdma_write_data_done;
445 static void nvmet_rdma_free_rsp(struct nvmet_rdma_device *ndev,
446 struct nvmet_rdma_rsp *r)
448 ib_dma_unmap_single(ndev->device, r->send_sge.addr,
449 sizeof(*r->req.cqe), DMA_TO_DEVICE);
454 nvmet_rdma_alloc_rsps(struct nvmet_rdma_queue *queue)
456 struct nvmet_rdma_device *ndev = queue->dev;
457 int nr_rsps = queue->recv_queue_size * 2;
458 int ret = -EINVAL, i;
460 queue->rsps = kcalloc(nr_rsps, sizeof(struct nvmet_rdma_rsp),
465 for (i = 0; i < nr_rsps; i++) {
466 struct nvmet_rdma_rsp *rsp = &queue->rsps[i];
468 ret = nvmet_rdma_alloc_rsp(ndev, rsp);
472 list_add_tail(&rsp->free_list, &queue->free_rsps);
479 struct nvmet_rdma_rsp *rsp = &queue->rsps[i];
481 list_del(&rsp->free_list);
482 nvmet_rdma_free_rsp(ndev, rsp);
489 static void nvmet_rdma_free_rsps(struct nvmet_rdma_queue *queue)
491 struct nvmet_rdma_device *ndev = queue->dev;
492 int i, nr_rsps = queue->recv_queue_size * 2;
494 for (i = 0; i < nr_rsps; i++) {
495 struct nvmet_rdma_rsp *rsp = &queue->rsps[i];
497 list_del(&rsp->free_list);
498 nvmet_rdma_free_rsp(ndev, rsp);
503 static int nvmet_rdma_post_recv(struct nvmet_rdma_device *ndev,
504 struct nvmet_rdma_cmd *cmd)
508 ib_dma_sync_single_for_device(ndev->device,
509 cmd->sge[0].addr, cmd->sge[0].length,
513 ret = ib_post_srq_recv(cmd->nsrq->srq, &cmd->wr, NULL);
515 ret = ib_post_recv(cmd->queue->qp, &cmd->wr, NULL);
518 pr_err("post_recv cmd failed\n");
523 static void nvmet_rdma_process_wr_wait_list(struct nvmet_rdma_queue *queue)
525 spin_lock(&queue->rsp_wr_wait_lock);
526 while (!list_empty(&queue->rsp_wr_wait_list)) {
527 struct nvmet_rdma_rsp *rsp;
530 rsp = list_entry(queue->rsp_wr_wait_list.next,
531 struct nvmet_rdma_rsp, wait_list);
532 list_del(&rsp->wait_list);
534 spin_unlock(&queue->rsp_wr_wait_lock);
535 ret = nvmet_rdma_execute_command(rsp);
536 spin_lock(&queue->rsp_wr_wait_lock);
539 list_add(&rsp->wait_list, &queue->rsp_wr_wait_list);
543 spin_unlock(&queue->rsp_wr_wait_lock);
546 static u16 nvmet_rdma_check_pi_status(struct ib_mr *sig_mr)
548 struct ib_mr_status mr_status;
552 ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
554 pr_err("ib_check_mr_status failed, ret %d\n", ret);
555 return NVME_SC_INVALID_PI;
558 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
559 switch (mr_status.sig_err.err_type) {
560 case IB_SIG_BAD_GUARD:
561 status = NVME_SC_GUARD_CHECK;
563 case IB_SIG_BAD_REFTAG:
564 status = NVME_SC_REFTAG_CHECK;
566 case IB_SIG_BAD_APPTAG:
567 status = NVME_SC_APPTAG_CHECK;
570 pr_err("PI error found type %d expected 0x%x vs actual 0x%x\n",
571 mr_status.sig_err.err_type,
572 mr_status.sig_err.expected,
573 mr_status.sig_err.actual);
579 static void nvmet_rdma_set_sig_domain(struct blk_integrity *bi,
580 struct nvme_command *cmd, struct ib_sig_domain *domain,
581 u16 control, u8 pi_type)
583 domain->sig_type = IB_SIG_TYPE_T10_DIF;
584 domain->sig.dif.bg_type = IB_T10DIF_CRC;
585 domain->sig.dif.pi_interval = 1 << bi->interval_exp;
586 domain->sig.dif.ref_tag = le32_to_cpu(cmd->rw.reftag);
587 if (control & NVME_RW_PRINFO_PRCHK_REF)
588 domain->sig.dif.ref_remap = true;
590 domain->sig.dif.app_tag = le16_to_cpu(cmd->rw.apptag);
591 domain->sig.dif.apptag_check_mask = le16_to_cpu(cmd->rw.appmask);
592 domain->sig.dif.app_escape = true;
593 if (pi_type == NVME_NS_DPS_PI_TYPE3)
594 domain->sig.dif.ref_escape = true;
597 static void nvmet_rdma_set_sig_attrs(struct nvmet_req *req,
598 struct ib_sig_attrs *sig_attrs)
600 struct nvme_command *cmd = req->cmd;
601 u16 control = le16_to_cpu(cmd->rw.control);
602 u8 pi_type = req->ns->pi_type;
603 struct blk_integrity *bi;
605 bi = bdev_get_integrity(req->ns->bdev);
607 memset(sig_attrs, 0, sizeof(*sig_attrs));
609 if (control & NVME_RW_PRINFO_PRACT) {
610 /* for WRITE_INSERT/READ_STRIP no wire domain */
611 sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
612 nvmet_rdma_set_sig_domain(bi, cmd, &sig_attrs->mem, control,
614 /* Clear the PRACT bit since HCA will generate/verify the PI */
615 control &= ~NVME_RW_PRINFO_PRACT;
616 cmd->rw.control = cpu_to_le16(control);
617 /* PI is added by the HW */
618 req->transfer_len += req->metadata_len;
620 /* for WRITE_PASS/READ_PASS both wire/memory domains exist */
621 nvmet_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control,
623 nvmet_rdma_set_sig_domain(bi, cmd, &sig_attrs->mem, control,
627 if (control & NVME_RW_PRINFO_PRCHK_REF)
628 sig_attrs->check_mask |= IB_SIG_CHECK_REFTAG;
629 if (control & NVME_RW_PRINFO_PRCHK_GUARD)
630 sig_attrs->check_mask |= IB_SIG_CHECK_GUARD;
631 if (control & NVME_RW_PRINFO_PRCHK_APP)
632 sig_attrs->check_mask |= IB_SIG_CHECK_APPTAG;
635 static int nvmet_rdma_rw_ctx_init(struct nvmet_rdma_rsp *rsp, u64 addr, u32 key,
636 struct ib_sig_attrs *sig_attrs)
638 struct rdma_cm_id *cm_id = rsp->queue->cm_id;
639 struct nvmet_req *req = &rsp->req;
642 if (req->metadata_len)
643 ret = rdma_rw_ctx_signature_init(&rsp->rw, cm_id->qp,
644 cm_id->port_num, req->sg, req->sg_cnt,
645 req->metadata_sg, req->metadata_sg_cnt, sig_attrs,
646 addr, key, nvmet_data_dir(req));
648 ret = rdma_rw_ctx_init(&rsp->rw, cm_id->qp, cm_id->port_num,
649 req->sg, req->sg_cnt, 0, addr, key,
650 nvmet_data_dir(req));
655 static void nvmet_rdma_rw_ctx_destroy(struct nvmet_rdma_rsp *rsp)
657 struct rdma_cm_id *cm_id = rsp->queue->cm_id;
658 struct nvmet_req *req = &rsp->req;
660 if (req->metadata_len)
661 rdma_rw_ctx_destroy_signature(&rsp->rw, cm_id->qp,
662 cm_id->port_num, req->sg, req->sg_cnt,
663 req->metadata_sg, req->metadata_sg_cnt,
664 nvmet_data_dir(req));
666 rdma_rw_ctx_destroy(&rsp->rw, cm_id->qp, cm_id->port_num,
667 req->sg, req->sg_cnt, nvmet_data_dir(req));
670 static void nvmet_rdma_release_rsp(struct nvmet_rdma_rsp *rsp)
672 struct nvmet_rdma_queue *queue = rsp->queue;
674 atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail);
677 nvmet_rdma_rw_ctx_destroy(rsp);
679 if (rsp->req.sg != rsp->cmd->inline_sg)
680 nvmet_req_free_sgls(&rsp->req);
682 if (unlikely(!list_empty_careful(&queue->rsp_wr_wait_list)))
683 nvmet_rdma_process_wr_wait_list(queue);
685 nvmet_rdma_put_rsp(rsp);
688 static void nvmet_rdma_error_comp(struct nvmet_rdma_queue *queue)
690 if (queue->nvme_sq.ctrl) {
691 nvmet_ctrl_fatal_error(queue->nvme_sq.ctrl);
694 * we didn't setup the controller yet in case
695 * of admin connect error, just disconnect and
698 nvmet_rdma_queue_disconnect(queue);
702 static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
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;
708 nvmet_rdma_release_rsp(rsp);
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);
718 static void nvmet_rdma_queue_response(struct nvmet_req *req)
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;
725 if (rsp->flags & NVMET_RDMA_REQ_INVALIDATE_RKEY) {
726 rsp->send_wr.opcode = IB_WR_SEND_WITH_INV;
727 rsp->send_wr.ex.invalidate_rkey = rsp->invalidate_rkey;
729 rsp->send_wr.opcode = IB_WR_SEND;
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);
737 first_wr = rdma_rw_ctx_wrs(&rsp->rw, cm_id->qp,
738 cm_id->port_num, NULL, &rsp->send_wr);
740 first_wr = &rsp->send_wr;
743 nvmet_rdma_post_recv(rsp->queue->dev, rsp->cmd);
745 ib_dma_sync_single_for_device(rsp->queue->dev->device,
746 rsp->send_sge.addr, rsp->send_sge.length,
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);
755 static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc)
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;
762 WARN_ON(rsp->n_rdma <= 0);
763 atomic_add(rsp->n_rdma, &queue->sq_wr_avail);
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);
778 if (rsp->req.metadata_len)
779 status = nvmet_rdma_check_pi_status(rsp->rw.reg->mr);
780 nvmet_rdma_rw_ctx_destroy(rsp);
782 if (unlikely(status))
783 nvmet_req_complete(&rsp->req, status);
785 rsp->req.execute(&rsp->req);
788 static void nvmet_rdma_write_data_done(struct ib_cq *cq, struct ib_wc *wc)
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;
796 if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
799 WARN_ON(rsp->n_rdma <= 0);
800 atomic_add(rsp->n_rdma, &queue->sq_wr_avail);
803 if (unlikely(wc->status != IB_WC_SUCCESS)) {
804 nvmet_rdma_rw_ctx_destroy(rsp);
805 nvmet_req_uninit(&rsp->req);
806 nvmet_rdma_release_rsp(rsp);
807 if (wc->status != IB_WC_WR_FLUSH_ERR) {
808 pr_info("RDMA WRITE for CQE failed with status %s (%d).\n",
809 ib_wc_status_msg(wc->status), wc->status);
810 nvmet_rdma_error_comp(queue);
816 * Upon RDMA completion check the signature status
817 * - if succeeded send good NVMe response
818 * - if failed send bad NVMe response with appropriate error
820 status = nvmet_rdma_check_pi_status(rsp->rw.reg->mr);
821 if (unlikely(status))
822 rsp->req.cqe->status = cpu_to_le16(status << 1);
823 nvmet_rdma_rw_ctx_destroy(rsp);
825 if (unlikely(ib_post_send(cm_id->qp, &rsp->send_wr, NULL))) {
826 pr_err("sending cmd response failed\n");
827 nvmet_rdma_release_rsp(rsp);
831 static void nvmet_rdma_use_inline_sg(struct nvmet_rdma_rsp *rsp, u32 len,
834 int sg_count = num_pages(len);
835 struct scatterlist *sg;
838 sg = rsp->cmd->inline_sg;
839 for (i = 0; i < sg_count; i++, sg++) {
840 if (i < sg_count - 1)
845 sg->length = min_t(int, len, PAGE_SIZE - off);
851 rsp->req.sg = rsp->cmd->inline_sg;
852 rsp->req.sg_cnt = sg_count;
855 static u16 nvmet_rdma_map_sgl_inline(struct nvmet_rdma_rsp *rsp)
857 struct nvme_sgl_desc *sgl = &rsp->req.cmd->common.dptr.sgl;
858 u64 off = le64_to_cpu(sgl->addr);
859 u32 len = le32_to_cpu(sgl->length);
861 if (!nvme_is_write(rsp->req.cmd)) {
863 offsetof(struct nvme_common_command, opcode);
864 return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
867 if (off + len > rsp->queue->dev->inline_data_size) {
868 pr_err("invalid inline data offset!\n");
869 return NVME_SC_SGL_INVALID_OFFSET | NVME_SC_DNR;
872 /* no data command? */
876 nvmet_rdma_use_inline_sg(rsp, len, off);
877 rsp->flags |= NVMET_RDMA_REQ_INLINE_DATA;
878 rsp->req.transfer_len += len;
882 static u16 nvmet_rdma_map_sgl_keyed(struct nvmet_rdma_rsp *rsp,
883 struct nvme_keyed_sgl_desc *sgl, bool invalidate)
885 u64 addr = le64_to_cpu(sgl->addr);
886 u32 key = get_unaligned_le32(sgl->key);
887 struct ib_sig_attrs sig_attrs;
890 rsp->req.transfer_len = get_unaligned_le24(sgl->length);
892 /* no data command? */
893 if (!rsp->req.transfer_len)
896 if (rsp->req.metadata_len)
897 nvmet_rdma_set_sig_attrs(&rsp->req, &sig_attrs);
899 ret = nvmet_req_alloc_sgls(&rsp->req);
900 if (unlikely(ret < 0))
903 ret = nvmet_rdma_rw_ctx_init(rsp, addr, key, &sig_attrs);
904 if (unlikely(ret < 0))
909 rsp->invalidate_rkey = key;
910 rsp->flags |= NVMET_RDMA_REQ_INVALIDATE_RKEY;
916 rsp->req.transfer_len = 0;
917 return NVME_SC_INTERNAL;
920 static u16 nvmet_rdma_map_sgl(struct nvmet_rdma_rsp *rsp)
922 struct nvme_keyed_sgl_desc *sgl = &rsp->req.cmd->common.dptr.ksgl;
924 switch (sgl->type >> 4) {
925 case NVME_SGL_FMT_DATA_DESC:
926 switch (sgl->type & 0xf) {
927 case NVME_SGL_FMT_OFFSET:
928 return nvmet_rdma_map_sgl_inline(rsp);
930 pr_err("invalid SGL subtype: %#x\n", sgl->type);
932 offsetof(struct nvme_common_command, dptr);
933 return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
935 case NVME_KEY_SGL_FMT_DATA_DESC:
936 switch (sgl->type & 0xf) {
937 case NVME_SGL_FMT_ADDRESS | NVME_SGL_FMT_INVALIDATE:
938 return nvmet_rdma_map_sgl_keyed(rsp, sgl, true);
939 case NVME_SGL_FMT_ADDRESS:
940 return nvmet_rdma_map_sgl_keyed(rsp, sgl, false);
942 pr_err("invalid SGL subtype: %#x\n", sgl->type);
944 offsetof(struct nvme_common_command, dptr);
945 return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
948 pr_err("invalid SGL type: %#x\n", sgl->type);
949 rsp->req.error_loc = offsetof(struct nvme_common_command, dptr);
950 return NVME_SC_SGL_INVALID_TYPE | NVME_SC_DNR;
954 static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp)
956 struct nvmet_rdma_queue *queue = rsp->queue;
958 if (unlikely(atomic_sub_return(1 + rsp->n_rdma,
959 &queue->sq_wr_avail) < 0)) {
960 pr_debug("IB send queue full (needed %d): queue %u cntlid %u\n",
961 1 + rsp->n_rdma, queue->idx,
962 queue->nvme_sq.ctrl->cntlid);
963 atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail);
967 if (nvmet_rdma_need_data_in(rsp)) {
968 if (rdma_rw_ctx_post(&rsp->rw, queue->qp,
969 queue->cm_id->port_num, &rsp->read_cqe, NULL))
970 nvmet_req_complete(&rsp->req, NVME_SC_DATA_XFER_ERROR);
972 rsp->req.execute(&rsp->req);
978 static void nvmet_rdma_handle_command(struct nvmet_rdma_queue *queue,
979 struct nvmet_rdma_rsp *cmd)
983 ib_dma_sync_single_for_cpu(queue->dev->device,
984 cmd->cmd->sge[0].addr, cmd->cmd->sge[0].length,
986 ib_dma_sync_single_for_cpu(queue->dev->device,
987 cmd->send_sge.addr, cmd->send_sge.length,
990 if (!nvmet_req_init(&cmd->req, &queue->nvme_cq,
991 &queue->nvme_sq, &nvmet_rdma_ops))
994 status = nvmet_rdma_map_sgl(cmd);
998 if (unlikely(!nvmet_rdma_execute_command(cmd))) {
999 spin_lock(&queue->rsp_wr_wait_lock);
1000 list_add_tail(&cmd->wait_list, &queue->rsp_wr_wait_list);
1001 spin_unlock(&queue->rsp_wr_wait_lock);
1007 nvmet_req_complete(&cmd->req, status);
1010 static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1012 struct nvmet_rdma_cmd *cmd =
1013 container_of(wc->wr_cqe, struct nvmet_rdma_cmd, cqe);
1014 struct nvmet_rdma_queue *queue = wc->qp->qp_context;
1015 struct nvmet_rdma_rsp *rsp;
1017 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1018 if (wc->status != IB_WC_WR_FLUSH_ERR) {
1019 pr_err("RECV for CQE 0x%p failed with status %s (%d)\n",
1020 wc->wr_cqe, ib_wc_status_msg(wc->status),
1022 nvmet_rdma_error_comp(queue);
1027 if (unlikely(wc->byte_len < sizeof(struct nvme_command))) {
1028 pr_err("Ctrl Fatal Error: capsule size less than 64 bytes\n");
1029 nvmet_rdma_error_comp(queue);
1034 rsp = nvmet_rdma_get_rsp(queue);
1035 if (unlikely(!rsp)) {
1037 * we get here only under memory pressure,
1038 * silently drop and have the host retry
1039 * as we can't even fail it.
1041 nvmet_rdma_post_recv(queue->dev, cmd);
1047 rsp->req.cmd = cmd->nvme_cmd;
1048 rsp->req.port = queue->port;
1051 if (unlikely(queue->state != NVMET_RDMA_Q_LIVE)) {
1052 unsigned long flags;
1054 spin_lock_irqsave(&queue->state_lock, flags);
1055 if (queue->state == NVMET_RDMA_Q_CONNECTING)
1056 list_add_tail(&rsp->wait_list, &queue->rsp_wait_list);
1058 nvmet_rdma_put_rsp(rsp);
1059 spin_unlock_irqrestore(&queue->state_lock, flags);
1063 nvmet_rdma_handle_command(queue, rsp);
1066 static void nvmet_rdma_destroy_srq(struct nvmet_rdma_srq *nsrq)
1068 nvmet_rdma_free_cmds(nsrq->ndev, nsrq->cmds, nsrq->ndev->srq_size,
1070 ib_destroy_srq(nsrq->srq);
1075 static void nvmet_rdma_destroy_srqs(struct nvmet_rdma_device *ndev)
1082 for (i = 0; i < ndev->srq_count; i++)
1083 nvmet_rdma_destroy_srq(ndev->srqs[i]);
1088 static struct nvmet_rdma_srq *
1089 nvmet_rdma_init_srq(struct nvmet_rdma_device *ndev)
1091 struct ib_srq_init_attr srq_attr = { NULL, };
1092 size_t srq_size = ndev->srq_size;
1093 struct nvmet_rdma_srq *nsrq;
1097 nsrq = kzalloc(sizeof(*nsrq), GFP_KERNEL);
1099 return ERR_PTR(-ENOMEM);
1101 srq_attr.attr.max_wr = srq_size;
1102 srq_attr.attr.max_sge = 1 + ndev->inline_page_count;
1103 srq_attr.attr.srq_limit = 0;
1104 srq_attr.srq_type = IB_SRQT_BASIC;
1105 srq = ib_create_srq(ndev->pd, &srq_attr);
1111 nsrq->cmds = nvmet_rdma_alloc_cmds(ndev, srq_size, false);
1112 if (IS_ERR(nsrq->cmds)) {
1113 ret = PTR_ERR(nsrq->cmds);
1114 goto out_destroy_srq;
1120 for (i = 0; i < srq_size; i++) {
1121 nsrq->cmds[i].nsrq = nsrq;
1122 ret = nvmet_rdma_post_recv(ndev, &nsrq->cmds[i]);
1130 nvmet_rdma_free_cmds(ndev, nsrq->cmds, srq_size, false);
1132 ib_destroy_srq(srq);
1135 return ERR_PTR(ret);
1138 static int nvmet_rdma_init_srqs(struct nvmet_rdma_device *ndev)
1142 if (!ndev->device->attrs.max_srq_wr || !ndev->device->attrs.max_srq) {
1144 * If SRQs aren't supported we just go ahead and use normal
1145 * non-shared receive queues.
1147 pr_info("SRQ requested but not supported.\n");
1151 ndev->srq_size = min(ndev->device->attrs.max_srq_wr,
1152 nvmet_rdma_srq_size);
1153 ndev->srq_count = min(ndev->device->num_comp_vectors,
1154 ndev->device->attrs.max_srq);
1156 ndev->srqs = kcalloc(ndev->srq_count, sizeof(*ndev->srqs), GFP_KERNEL);
1160 for (i = 0; i < ndev->srq_count; i++) {
1161 ndev->srqs[i] = nvmet_rdma_init_srq(ndev);
1162 if (IS_ERR(ndev->srqs[i])) {
1163 ret = PTR_ERR(ndev->srqs[i]);
1172 nvmet_rdma_destroy_srq(ndev->srqs[i]);
1177 static void nvmet_rdma_free_dev(struct kref *ref)
1179 struct nvmet_rdma_device *ndev =
1180 container_of(ref, struct nvmet_rdma_device, ref);
1182 mutex_lock(&device_list_mutex);
1183 list_del(&ndev->entry);
1184 mutex_unlock(&device_list_mutex);
1186 nvmet_rdma_destroy_srqs(ndev);
1187 ib_dealloc_pd(ndev->pd);
1192 static struct nvmet_rdma_device *
1193 nvmet_rdma_find_get_device(struct rdma_cm_id *cm_id)
1195 struct nvmet_rdma_port *port = cm_id->context;
1196 struct nvmet_port *nport = port->nport;
1197 struct nvmet_rdma_device *ndev;
1198 int inline_page_count;
1199 int inline_sge_count;
1202 mutex_lock(&device_list_mutex);
1203 list_for_each_entry(ndev, &device_list, entry) {
1204 if (ndev->device->node_guid == cm_id->device->node_guid &&
1205 kref_get_unless_zero(&ndev->ref))
1209 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
1213 inline_page_count = num_pages(nport->inline_data_size);
1214 inline_sge_count = max(cm_id->device->attrs.max_sge_rd,
1215 cm_id->device->attrs.max_recv_sge) - 1;
1216 if (inline_page_count > inline_sge_count) {
1217 pr_warn("inline_data_size %d cannot be supported by device %s. Reducing to %lu.\n",
1218 nport->inline_data_size, cm_id->device->name,
1219 inline_sge_count * PAGE_SIZE);
1220 nport->inline_data_size = inline_sge_count * PAGE_SIZE;
1221 inline_page_count = inline_sge_count;
1223 ndev->inline_data_size = nport->inline_data_size;
1224 ndev->inline_page_count = inline_page_count;
1226 if (nport->pi_enable && !(cm_id->device->attrs.kernel_cap_flags &
1227 IBK_INTEGRITY_HANDOVER)) {
1228 pr_warn("T10-PI is not supported by device %s. Disabling it\n",
1229 cm_id->device->name);
1230 nport->pi_enable = false;
1233 ndev->device = cm_id->device;
1234 kref_init(&ndev->ref);
1236 ndev->pd = ib_alloc_pd(ndev->device, 0);
1237 if (IS_ERR(ndev->pd))
1240 if (nvmet_rdma_use_srq) {
1241 ret = nvmet_rdma_init_srqs(ndev);
1246 list_add(&ndev->entry, &device_list);
1248 mutex_unlock(&device_list_mutex);
1249 pr_debug("added %s.\n", ndev->device->name);
1253 ib_dealloc_pd(ndev->pd);
1257 mutex_unlock(&device_list_mutex);
1261 static int nvmet_rdma_create_queue_ib(struct nvmet_rdma_queue *queue)
1263 struct ib_qp_init_attr qp_attr = { };
1264 struct nvmet_rdma_device *ndev = queue->dev;
1265 int nr_cqe, ret, i, factor;
1268 * Reserve CQ slots for RECV + RDMA_READ/RDMA_WRITE + RDMA_SEND.
1270 nr_cqe = queue->recv_queue_size + 2 * queue->send_queue_size;
1272 queue->cq = ib_cq_pool_get(ndev->device, nr_cqe + 1,
1273 queue->comp_vector, IB_POLL_WORKQUEUE);
1274 if (IS_ERR(queue->cq)) {
1275 ret = PTR_ERR(queue->cq);
1276 pr_err("failed to create CQ cqe= %d ret= %d\n",
1281 qp_attr.qp_context = queue;
1282 qp_attr.event_handler = nvmet_rdma_qp_event;
1283 qp_attr.send_cq = queue->cq;
1284 qp_attr.recv_cq = queue->cq;
1285 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
1286 qp_attr.qp_type = IB_QPT_RC;
1288 qp_attr.cap.max_send_wr = queue->send_queue_size + 1;
1289 factor = rdma_rw_mr_factor(ndev->device, queue->cm_id->port_num,
1290 1 << NVMET_RDMA_MAX_MDTS);
1291 qp_attr.cap.max_rdma_ctxs = queue->send_queue_size * factor;
1292 qp_attr.cap.max_send_sge = max(ndev->device->attrs.max_sge_rd,
1293 ndev->device->attrs.max_send_sge);
1296 qp_attr.srq = queue->nsrq->srq;
1299 qp_attr.cap.max_recv_wr = 1 + queue->recv_queue_size;
1300 qp_attr.cap.max_recv_sge = 1 + ndev->inline_page_count;
1303 if (queue->port->pi_enable && queue->host_qid)
1304 qp_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN;
1306 ret = rdma_create_qp(queue->cm_id, ndev->pd, &qp_attr);
1308 pr_err("failed to create_qp ret= %d\n", ret);
1309 goto err_destroy_cq;
1311 queue->qp = queue->cm_id->qp;
1313 atomic_set(&queue->sq_wr_avail, qp_attr.cap.max_send_wr);
1315 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1316 __func__, queue->cq->cqe, qp_attr.cap.max_send_sge,
1317 qp_attr.cap.max_send_wr, queue->cm_id);
1320 for (i = 0; i < queue->recv_queue_size; i++) {
1321 queue->cmds[i].queue = queue;
1322 ret = nvmet_rdma_post_recv(ndev, &queue->cmds[i]);
1324 goto err_destroy_qp;
1332 rdma_destroy_qp(queue->cm_id);
1334 ib_cq_pool_put(queue->cq, nr_cqe + 1);
1338 static void nvmet_rdma_destroy_queue_ib(struct nvmet_rdma_queue *queue)
1340 ib_drain_qp(queue->qp);
1342 rdma_destroy_id(queue->cm_id);
1343 ib_destroy_qp(queue->qp);
1344 ib_cq_pool_put(queue->cq, queue->recv_queue_size + 2 *
1345 queue->send_queue_size + 1);
1348 static void nvmet_rdma_free_queue(struct nvmet_rdma_queue *queue)
1350 pr_debug("freeing queue %d\n", queue->idx);
1352 nvmet_sq_destroy(&queue->nvme_sq);
1354 nvmet_rdma_destroy_queue_ib(queue);
1356 nvmet_rdma_free_cmds(queue->dev, queue->cmds,
1357 queue->recv_queue_size,
1360 nvmet_rdma_free_rsps(queue);
1361 ida_free(&nvmet_rdma_queue_ida, queue->idx);
1365 static void nvmet_rdma_release_queue_work(struct work_struct *w)
1367 struct nvmet_rdma_queue *queue =
1368 container_of(w, struct nvmet_rdma_queue, release_work);
1369 struct nvmet_rdma_device *dev = queue->dev;
1371 nvmet_rdma_free_queue(queue);
1373 kref_put(&dev->ref, nvmet_rdma_free_dev);
1377 nvmet_rdma_parse_cm_connect_req(struct rdma_conn_param *conn,
1378 struct nvmet_rdma_queue *queue)
1380 struct nvme_rdma_cm_req *req;
1382 req = (struct nvme_rdma_cm_req *)conn->private_data;
1383 if (!req || conn->private_data_len == 0)
1384 return NVME_RDMA_CM_INVALID_LEN;
1386 if (le16_to_cpu(req->recfmt) != NVME_RDMA_CM_FMT_1_0)
1387 return NVME_RDMA_CM_INVALID_RECFMT;
1389 queue->host_qid = le16_to_cpu(req->qid);
1392 * req->hsqsize corresponds to our recv queue size plus 1
1393 * req->hrqsize corresponds to our send queue size
1395 queue->recv_queue_size = le16_to_cpu(req->hsqsize) + 1;
1396 queue->send_queue_size = le16_to_cpu(req->hrqsize);
1398 if (!queue->host_qid && queue->recv_queue_size > NVME_AQ_DEPTH)
1399 return NVME_RDMA_CM_INVALID_HSQSIZE;
1401 /* XXX: Should we enforce some kind of max for IO queues? */
1406 static int nvmet_rdma_cm_reject(struct rdma_cm_id *cm_id,
1407 enum nvme_rdma_cm_status status)
1409 struct nvme_rdma_cm_rej rej;
1411 pr_debug("rejecting connect request: status %d (%s)\n",
1412 status, nvme_rdma_cm_msg(status));
1414 rej.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1415 rej.sts = cpu_to_le16(status);
1417 return rdma_reject(cm_id, (void *)&rej, sizeof(rej),
1418 IB_CM_REJ_CONSUMER_DEFINED);
1421 static struct nvmet_rdma_queue *
1422 nvmet_rdma_alloc_queue(struct nvmet_rdma_device *ndev,
1423 struct rdma_cm_id *cm_id,
1424 struct rdma_cm_event *event)
1426 struct nvmet_rdma_port *port = cm_id->context;
1427 struct nvmet_rdma_queue *queue;
1430 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
1432 ret = NVME_RDMA_CM_NO_RSC;
1436 ret = nvmet_sq_init(&queue->nvme_sq);
1438 ret = NVME_RDMA_CM_NO_RSC;
1439 goto out_free_queue;
1442 ret = nvmet_rdma_parse_cm_connect_req(&event->param.conn, queue);
1444 goto out_destroy_sq;
1447 * Schedules the actual release because calling rdma_destroy_id from
1448 * inside a CM callback would trigger a deadlock. (great API design..)
1450 INIT_WORK(&queue->release_work, nvmet_rdma_release_queue_work);
1452 queue->cm_id = cm_id;
1453 queue->port = port->nport;
1455 spin_lock_init(&queue->state_lock);
1456 queue->state = NVMET_RDMA_Q_CONNECTING;
1457 INIT_LIST_HEAD(&queue->rsp_wait_list);
1458 INIT_LIST_HEAD(&queue->rsp_wr_wait_list);
1459 spin_lock_init(&queue->rsp_wr_wait_lock);
1460 INIT_LIST_HEAD(&queue->free_rsps);
1461 spin_lock_init(&queue->rsps_lock);
1462 INIT_LIST_HEAD(&queue->queue_list);
1464 queue->idx = ida_alloc(&nvmet_rdma_queue_ida, GFP_KERNEL);
1465 if (queue->idx < 0) {
1466 ret = NVME_RDMA_CM_NO_RSC;
1467 goto out_destroy_sq;
1471 * Spread the io queues across completion vectors,
1472 * but still keep all admin queues on vector 0.
1474 queue->comp_vector = !queue->host_qid ? 0 :
1475 queue->idx % ndev->device->num_comp_vectors;
1478 ret = nvmet_rdma_alloc_rsps(queue);
1480 ret = NVME_RDMA_CM_NO_RSC;
1481 goto out_ida_remove;
1485 queue->nsrq = ndev->srqs[queue->comp_vector % ndev->srq_count];
1487 queue->cmds = nvmet_rdma_alloc_cmds(ndev,
1488 queue->recv_queue_size,
1490 if (IS_ERR(queue->cmds)) {
1491 ret = NVME_RDMA_CM_NO_RSC;
1492 goto out_free_responses;
1496 ret = nvmet_rdma_create_queue_ib(queue);
1498 pr_err("%s: creating RDMA queue failed (%d).\n",
1500 ret = NVME_RDMA_CM_NO_RSC;
1508 nvmet_rdma_free_cmds(queue->dev, queue->cmds,
1509 queue->recv_queue_size,
1513 nvmet_rdma_free_rsps(queue);
1515 ida_free(&nvmet_rdma_queue_ida, queue->idx);
1517 nvmet_sq_destroy(&queue->nvme_sq);
1521 nvmet_rdma_cm_reject(cm_id, ret);
1525 static void nvmet_rdma_qp_event(struct ib_event *event, void *priv)
1527 struct nvmet_rdma_queue *queue = priv;
1529 switch (event->event) {
1530 case IB_EVENT_COMM_EST:
1531 rdma_notify(queue->cm_id, event->event);
1533 case IB_EVENT_QP_LAST_WQE_REACHED:
1534 pr_debug("received last WQE reached event for queue=0x%p\n",
1538 pr_err("received IB QP event: %s (%d)\n",
1539 ib_event_msg(event->event), event->event);
1544 static int nvmet_rdma_cm_accept(struct rdma_cm_id *cm_id,
1545 struct nvmet_rdma_queue *queue,
1546 struct rdma_conn_param *p)
1548 struct rdma_conn_param param = { };
1549 struct nvme_rdma_cm_rep priv = { };
1552 param.rnr_retry_count = 7;
1553 param.flow_control = 1;
1554 param.initiator_depth = min_t(u8, p->initiator_depth,
1555 queue->dev->device->attrs.max_qp_init_rd_atom);
1556 param.private_data = &priv;
1557 param.private_data_len = sizeof(priv);
1558 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1559 priv.crqsize = cpu_to_le16(queue->recv_queue_size);
1561 ret = rdma_accept(cm_id, ¶m);
1563 pr_err("rdma_accept failed (error code = %d)\n", ret);
1568 static int nvmet_rdma_queue_connect(struct rdma_cm_id *cm_id,
1569 struct rdma_cm_event *event)
1571 struct nvmet_rdma_device *ndev;
1572 struct nvmet_rdma_queue *queue;
1575 ndev = nvmet_rdma_find_get_device(cm_id);
1577 nvmet_rdma_cm_reject(cm_id, NVME_RDMA_CM_NO_RSC);
1578 return -ECONNREFUSED;
1581 queue = nvmet_rdma_alloc_queue(ndev, cm_id, event);
1587 if (queue->host_qid == 0) {
1588 struct nvmet_rdma_queue *q;
1591 /* Check for pending controller teardown */
1592 mutex_lock(&nvmet_rdma_queue_mutex);
1593 list_for_each_entry(q, &nvmet_rdma_queue_list, queue_list) {
1594 if (q->nvme_sq.ctrl == queue->nvme_sq.ctrl &&
1595 q->state == NVMET_RDMA_Q_DISCONNECTING)
1598 mutex_unlock(&nvmet_rdma_queue_mutex);
1599 if (pending > NVMET_RDMA_BACKLOG)
1600 return NVME_SC_CONNECT_CTRL_BUSY;
1603 ret = nvmet_rdma_cm_accept(cm_id, queue, &event->param.conn);
1606 * Don't destroy the cm_id in free path, as we implicitly
1607 * destroy the cm_id here with non-zero ret code.
1609 queue->cm_id = NULL;
1613 mutex_lock(&nvmet_rdma_queue_mutex);
1614 list_add_tail(&queue->queue_list, &nvmet_rdma_queue_list);
1615 mutex_unlock(&nvmet_rdma_queue_mutex);
1620 nvmet_rdma_free_queue(queue);
1622 kref_put(&ndev->ref, nvmet_rdma_free_dev);
1627 static void nvmet_rdma_queue_established(struct nvmet_rdma_queue *queue)
1629 unsigned long flags;
1631 spin_lock_irqsave(&queue->state_lock, flags);
1632 if (queue->state != NVMET_RDMA_Q_CONNECTING) {
1633 pr_warn("trying to establish a connected queue\n");
1636 queue->state = NVMET_RDMA_Q_LIVE;
1638 while (!list_empty(&queue->rsp_wait_list)) {
1639 struct nvmet_rdma_rsp *cmd;
1641 cmd = list_first_entry(&queue->rsp_wait_list,
1642 struct nvmet_rdma_rsp, wait_list);
1643 list_del(&cmd->wait_list);
1645 spin_unlock_irqrestore(&queue->state_lock, flags);
1646 nvmet_rdma_handle_command(queue, cmd);
1647 spin_lock_irqsave(&queue->state_lock, flags);
1651 spin_unlock_irqrestore(&queue->state_lock, flags);
1654 static void __nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue)
1656 bool disconnect = false;
1657 unsigned long flags;
1659 pr_debug("cm_id= %p queue->state= %d\n", queue->cm_id, queue->state);
1661 spin_lock_irqsave(&queue->state_lock, flags);
1662 switch (queue->state) {
1663 case NVMET_RDMA_Q_CONNECTING:
1664 while (!list_empty(&queue->rsp_wait_list)) {
1665 struct nvmet_rdma_rsp *rsp;
1667 rsp = list_first_entry(&queue->rsp_wait_list,
1668 struct nvmet_rdma_rsp,
1670 list_del(&rsp->wait_list);
1671 nvmet_rdma_put_rsp(rsp);
1674 case NVMET_RDMA_Q_LIVE:
1675 queue->state = NVMET_RDMA_Q_DISCONNECTING;
1678 case NVMET_RDMA_Q_DISCONNECTING:
1681 spin_unlock_irqrestore(&queue->state_lock, flags);
1684 rdma_disconnect(queue->cm_id);
1685 queue_work(nvmet_wq, &queue->release_work);
1689 static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue)
1691 bool disconnect = false;
1693 mutex_lock(&nvmet_rdma_queue_mutex);
1694 if (!list_empty(&queue->queue_list)) {
1695 list_del_init(&queue->queue_list);
1698 mutex_unlock(&nvmet_rdma_queue_mutex);
1701 __nvmet_rdma_queue_disconnect(queue);
1704 static void nvmet_rdma_queue_connect_fail(struct rdma_cm_id *cm_id,
1705 struct nvmet_rdma_queue *queue)
1707 WARN_ON_ONCE(queue->state != NVMET_RDMA_Q_CONNECTING);
1709 mutex_lock(&nvmet_rdma_queue_mutex);
1710 if (!list_empty(&queue->queue_list))
1711 list_del_init(&queue->queue_list);
1712 mutex_unlock(&nvmet_rdma_queue_mutex);
1714 pr_err("failed to connect queue %d\n", queue->idx);
1715 queue_work(nvmet_wq, &queue->release_work);
1719 * nvmet_rdma_device_removal() - Handle RDMA device removal
1720 * @cm_id: rdma_cm id, used for nvmet port
1721 * @queue: nvmet rdma queue (cm id qp_context)
1723 * DEVICE_REMOVAL event notifies us that the RDMA device is about
1724 * to unplug. Note that this event can be generated on a normal
1725 * queue cm_id and/or a device bound listener cm_id (where in this
1726 * case queue will be null).
1728 * We registered an ib_client to handle device removal for queues,
1729 * so we only need to handle the listening port cm_ids. In this case
1730 * we nullify the priv to prevent double cm_id destruction and destroying
1731 * the cm_id implicitely by returning a non-zero rc to the callout.
1733 static int nvmet_rdma_device_removal(struct rdma_cm_id *cm_id,
1734 struct nvmet_rdma_queue *queue)
1736 struct nvmet_rdma_port *port;
1740 * This is a queue cm_id. we have registered
1741 * an ib_client to handle queues removal
1742 * so don't interfear and just return.
1747 port = cm_id->context;
1750 * This is a listener cm_id. Make sure that
1751 * future remove_port won't invoke a double
1752 * cm_id destroy. use atomic xchg to make sure
1753 * we don't compete with remove_port.
1755 if (xchg(&port->cm_id, NULL) != cm_id)
1759 * We need to return 1 so that the core will destroy
1760 * it's own ID. What a great API design..
1765 static int nvmet_rdma_cm_handler(struct rdma_cm_id *cm_id,
1766 struct rdma_cm_event *event)
1768 struct nvmet_rdma_queue *queue = NULL;
1772 queue = cm_id->qp->qp_context;
1774 pr_debug("%s (%d): status %d id %p\n",
1775 rdma_event_msg(event->event), event->event,
1776 event->status, cm_id);
1778 switch (event->event) {
1779 case RDMA_CM_EVENT_CONNECT_REQUEST:
1780 ret = nvmet_rdma_queue_connect(cm_id, event);
1782 case RDMA_CM_EVENT_ESTABLISHED:
1783 nvmet_rdma_queue_established(queue);
1785 case RDMA_CM_EVENT_ADDR_CHANGE:
1787 struct nvmet_rdma_port *port = cm_id->context;
1789 queue_delayed_work(nvmet_wq, &port->repair_work, 0);
1793 case RDMA_CM_EVENT_DISCONNECTED:
1794 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1795 nvmet_rdma_queue_disconnect(queue);
1797 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1798 ret = nvmet_rdma_device_removal(cm_id, queue);
1800 case RDMA_CM_EVENT_REJECTED:
1801 pr_debug("Connection rejected: %s\n",
1802 rdma_reject_msg(cm_id, event->status));
1804 case RDMA_CM_EVENT_UNREACHABLE:
1805 case RDMA_CM_EVENT_CONNECT_ERROR:
1806 nvmet_rdma_queue_connect_fail(cm_id, queue);
1809 pr_err("received unrecognized RDMA CM event %d\n",
1817 static void nvmet_rdma_delete_ctrl(struct nvmet_ctrl *ctrl)
1819 struct nvmet_rdma_queue *queue;
1822 mutex_lock(&nvmet_rdma_queue_mutex);
1823 list_for_each_entry(queue, &nvmet_rdma_queue_list, queue_list) {
1824 if (queue->nvme_sq.ctrl == ctrl) {
1825 list_del_init(&queue->queue_list);
1826 mutex_unlock(&nvmet_rdma_queue_mutex);
1828 __nvmet_rdma_queue_disconnect(queue);
1832 mutex_unlock(&nvmet_rdma_queue_mutex);
1835 static void nvmet_rdma_destroy_port_queues(struct nvmet_rdma_port *port)
1837 struct nvmet_rdma_queue *queue, *tmp;
1838 struct nvmet_port *nport = port->nport;
1840 mutex_lock(&nvmet_rdma_queue_mutex);
1841 list_for_each_entry_safe(queue, tmp, &nvmet_rdma_queue_list,
1843 if (queue->port != nport)
1846 list_del_init(&queue->queue_list);
1847 __nvmet_rdma_queue_disconnect(queue);
1849 mutex_unlock(&nvmet_rdma_queue_mutex);
1852 static void nvmet_rdma_disable_port(struct nvmet_rdma_port *port)
1854 struct rdma_cm_id *cm_id = xchg(&port->cm_id, NULL);
1857 rdma_destroy_id(cm_id);
1860 * Destroy the remaining queues, which are not belong to any
1861 * controller yet. Do it here after the RDMA-CM was destroyed
1862 * guarantees that no new queue will be created.
1864 nvmet_rdma_destroy_port_queues(port);
1867 static int nvmet_rdma_enable_port(struct nvmet_rdma_port *port)
1869 struct sockaddr *addr = (struct sockaddr *)&port->addr;
1870 struct rdma_cm_id *cm_id;
1873 cm_id = rdma_create_id(&init_net, nvmet_rdma_cm_handler, port,
1874 RDMA_PS_TCP, IB_QPT_RC);
1875 if (IS_ERR(cm_id)) {
1876 pr_err("CM ID creation failed\n");
1877 return PTR_ERR(cm_id);
1881 * Allow both IPv4 and IPv6 sockets to bind a single port
1884 ret = rdma_set_afonly(cm_id, 1);
1886 pr_err("rdma_set_afonly failed (%d)\n", ret);
1887 goto out_destroy_id;
1890 ret = rdma_bind_addr(cm_id, addr);
1892 pr_err("binding CM ID to %pISpcs failed (%d)\n", addr, ret);
1893 goto out_destroy_id;
1896 ret = rdma_listen(cm_id, NVMET_RDMA_BACKLOG);
1898 pr_err("listening to %pISpcs failed (%d)\n", addr, ret);
1899 goto out_destroy_id;
1902 port->cm_id = cm_id;
1906 rdma_destroy_id(cm_id);
1910 static void nvmet_rdma_repair_port_work(struct work_struct *w)
1912 struct nvmet_rdma_port *port = container_of(to_delayed_work(w),
1913 struct nvmet_rdma_port, repair_work);
1916 nvmet_rdma_disable_port(port);
1917 ret = nvmet_rdma_enable_port(port);
1919 queue_delayed_work(nvmet_wq, &port->repair_work, 5 * HZ);
1922 static int nvmet_rdma_add_port(struct nvmet_port *nport)
1924 struct nvmet_rdma_port *port;
1925 __kernel_sa_family_t af;
1928 port = kzalloc(sizeof(*port), GFP_KERNEL);
1933 port->nport = nport;
1934 INIT_DELAYED_WORK(&port->repair_work, nvmet_rdma_repair_port_work);
1936 switch (nport->disc_addr.adrfam) {
1937 case NVMF_ADDR_FAMILY_IP4:
1940 case NVMF_ADDR_FAMILY_IP6:
1944 pr_err("address family %d not supported\n",
1945 nport->disc_addr.adrfam);
1950 if (nport->inline_data_size < 0) {
1951 nport->inline_data_size = NVMET_RDMA_DEFAULT_INLINE_DATA_SIZE;
1952 } else if (nport->inline_data_size > NVMET_RDMA_MAX_INLINE_DATA_SIZE) {
1953 pr_warn("inline_data_size %u is too large, reducing to %u\n",
1954 nport->inline_data_size,
1955 NVMET_RDMA_MAX_INLINE_DATA_SIZE);
1956 nport->inline_data_size = NVMET_RDMA_MAX_INLINE_DATA_SIZE;
1959 ret = inet_pton_with_scope(&init_net, af, nport->disc_addr.traddr,
1960 nport->disc_addr.trsvcid, &port->addr);
1962 pr_err("malformed ip/port passed: %s:%s\n",
1963 nport->disc_addr.traddr, nport->disc_addr.trsvcid);
1967 ret = nvmet_rdma_enable_port(port);
1971 pr_info("enabling port %d (%pISpcs)\n",
1972 le16_to_cpu(nport->disc_addr.portid),
1973 (struct sockaddr *)&port->addr);
1982 static void nvmet_rdma_remove_port(struct nvmet_port *nport)
1984 struct nvmet_rdma_port *port = nport->priv;
1986 cancel_delayed_work_sync(&port->repair_work);
1987 nvmet_rdma_disable_port(port);
1991 static void nvmet_rdma_disc_port_addr(struct nvmet_req *req,
1992 struct nvmet_port *nport, char *traddr)
1994 struct nvmet_rdma_port *port = nport->priv;
1995 struct rdma_cm_id *cm_id = port->cm_id;
1997 if (inet_addr_is_any((struct sockaddr *)&cm_id->route.addr.src_addr)) {
1998 struct nvmet_rdma_rsp *rsp =
1999 container_of(req, struct nvmet_rdma_rsp, req);
2000 struct rdma_cm_id *req_cm_id = rsp->queue->cm_id;
2001 struct sockaddr *addr = (void *)&req_cm_id->route.addr.src_addr;
2003 sprintf(traddr, "%pISc", addr);
2005 memcpy(traddr, nport->disc_addr.traddr, NVMF_TRADDR_SIZE);
2009 static u8 nvmet_rdma_get_mdts(const struct nvmet_ctrl *ctrl)
2011 if (ctrl->pi_support)
2012 return NVMET_RDMA_MAX_METADATA_MDTS;
2013 return NVMET_RDMA_MAX_MDTS;
2016 static u16 nvmet_rdma_get_max_queue_size(const struct nvmet_ctrl *ctrl)
2018 return NVME_RDMA_MAX_QUEUE_SIZE;
2021 static const struct nvmet_fabrics_ops nvmet_rdma_ops = {
2022 .owner = THIS_MODULE,
2023 .type = NVMF_TRTYPE_RDMA,
2025 .flags = NVMF_KEYED_SGLS | NVMF_METADATA_SUPPORTED,
2026 .add_port = nvmet_rdma_add_port,
2027 .remove_port = nvmet_rdma_remove_port,
2028 .queue_response = nvmet_rdma_queue_response,
2029 .delete_ctrl = nvmet_rdma_delete_ctrl,
2030 .disc_traddr = nvmet_rdma_disc_port_addr,
2031 .get_mdts = nvmet_rdma_get_mdts,
2032 .get_max_queue_size = nvmet_rdma_get_max_queue_size,
2035 static void nvmet_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2037 struct nvmet_rdma_queue *queue, *tmp;
2038 struct nvmet_rdma_device *ndev;
2041 mutex_lock(&device_list_mutex);
2042 list_for_each_entry(ndev, &device_list, entry) {
2043 if (ndev->device == ib_device) {
2048 mutex_unlock(&device_list_mutex);
2054 * IB Device that is used by nvmet controllers is being removed,
2055 * delete all queues using this device.
2057 mutex_lock(&nvmet_rdma_queue_mutex);
2058 list_for_each_entry_safe(queue, tmp, &nvmet_rdma_queue_list,
2060 if (queue->dev->device != ib_device)
2063 pr_info("Removing queue %d\n", queue->idx);
2064 list_del_init(&queue->queue_list);
2065 __nvmet_rdma_queue_disconnect(queue);
2067 mutex_unlock(&nvmet_rdma_queue_mutex);
2069 flush_workqueue(nvmet_wq);
2072 static struct ib_client nvmet_rdma_ib_client = {
2073 .name = "nvmet_rdma",
2074 .remove = nvmet_rdma_remove_one
2077 static int __init nvmet_rdma_init(void)
2081 ret = ib_register_client(&nvmet_rdma_ib_client);
2085 ret = nvmet_register_transport(&nvmet_rdma_ops);
2092 ib_unregister_client(&nvmet_rdma_ib_client);
2096 static void __exit nvmet_rdma_exit(void)
2098 nvmet_unregister_transport(&nvmet_rdma_ops);
2099 ib_unregister_client(&nvmet_rdma_ib_client);
2100 WARN_ON_ONCE(!list_empty(&nvmet_rdma_queue_list));
2101 ida_destroy(&nvmet_rdma_queue_ida);
2104 module_init(nvmet_rdma_init);
2105 module_exit(nvmet_rdma_exit);
2107 MODULE_DESCRIPTION("NVMe target RDMA transport driver");
2108 MODULE_LICENSE("GPL v2");
2109 MODULE_ALIAS("nvmet-transport-1"); /* 1 == NVMF_TRTYPE_RDMA */