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 <linux/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 #define NVMET_RDMA_DISCRETE_RSP_TAG -1
44 struct nvmet_rdma_srq
;
46 struct nvmet_rdma_cmd
{
47 struct ib_sge sge
[NVMET_RDMA_MAX_INLINE_SGE
+ 1];
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
;
57 NVMET_RDMA_REQ_INLINE_DATA
= (1 << 0),
60 struct nvmet_rdma_rsp
{
61 struct ib_sge send_sge
;
62 struct ib_cqe send_cqe
;
63 struct ib_send_wr send_wr
;
65 struct nvmet_rdma_cmd
*cmd
;
66 struct nvmet_rdma_queue
*queue
;
68 struct ib_cqe read_cqe
;
69 struct ib_cqe write_cqe
;
70 struct rdma_rw_ctx rw
;
79 struct list_head wait_list
;
83 enum nvmet_rdma_queue_state
{
84 NVMET_RDMA_Q_CONNECTING
,
86 NVMET_RDMA_Q_DISCONNECTING
,
89 struct nvmet_rdma_queue
{
90 struct rdma_cm_id
*cm_id
;
92 struct nvmet_port
*port
;
95 struct nvmet_rdma_device
*dev
;
96 struct nvmet_rdma_srq
*nsrq
;
97 spinlock_t state_lock
;
98 enum nvmet_rdma_queue_state state
;
99 struct nvmet_cq nvme_cq
;
100 struct nvmet_sq nvme_sq
;
102 struct nvmet_rdma_rsp
*rsps
;
103 struct sbitmap rsp_tags
;
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
,
179 static const struct nvmet_fabrics_ops nvmet_rdma_ops
;
181 static int srq_size_set(const char *val
, const struct kernel_param
*kp
)
185 ret
= kstrtoint(val
, 10, &n
);
186 if (ret
!= 0 || n
< 256)
189 return param_set_int(val
, kp
);
192 static int num_pages(int len
)
194 return 1 + (((len
- 1) & PAGE_MASK
) >> PAGE_SHIFT
);
197 static inline bool nvmet_rdma_need_data_in(struct nvmet_rdma_rsp
*rsp
)
199 return nvme_is_write(rsp
->req
.cmd
) &&
200 rsp
->req
.transfer_len
&&
201 !(rsp
->flags
& NVMET_RDMA_REQ_INLINE_DATA
);
204 static inline bool nvmet_rdma_need_data_out(struct nvmet_rdma_rsp
*rsp
)
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
);
212 static inline struct nvmet_rdma_rsp
*
213 nvmet_rdma_get_rsp(struct nvmet_rdma_queue
*queue
)
215 struct nvmet_rdma_rsp
*rsp
= NULL
;
218 tag
= sbitmap_get(&queue
->rsp_tags
);
220 rsp
= &queue
->rsps
[tag
];
222 if (unlikely(!rsp
)) {
225 rsp
= kzalloc(sizeof(*rsp
), GFP_KERNEL
);
228 ret
= nvmet_rdma_alloc_rsp(queue
->dev
, rsp
,
229 NVMET_RDMA_DISCRETE_RSP_TAG
);
240 nvmet_rdma_put_rsp(struct nvmet_rdma_rsp
*rsp
)
242 if (unlikely(rsp
->tag
== NVMET_RDMA_DISCRETE_RSP_TAG
)) {
243 nvmet_rdma_free_rsp(rsp
->queue
->dev
, rsp
);
248 sbitmap_clear_bit(&rsp
->queue
->rsp_tags
, rsp
->tag
);
251 static void nvmet_rdma_free_inline_pages(struct nvmet_rdma_device
*ndev
,
252 struct nvmet_rdma_cmd
*c
)
254 struct scatterlist
*sg
;
258 if (!ndev
->inline_data_size
)
264 for (i
= 0; i
< ndev
->inline_page_count
; i
++, sg
++, sge
++) {
266 ib_dma_unmap_page(ndev
->device
, sge
->addr
,
267 sge
->length
, DMA_FROM_DEVICE
);
269 __free_page(sg_page(sg
));
273 static int nvmet_rdma_alloc_inline_pages(struct nvmet_rdma_device
*ndev
,
274 struct nvmet_rdma_cmd
*c
)
276 struct scatterlist
*sg
;
282 if (!ndev
->inline_data_size
)
286 sg_init_table(sg
, ndev
->inline_page_count
);
288 len
= ndev
->inline_data_size
;
290 for (i
= 0; i
< ndev
->inline_page_count
; i
++, sg
++, sge
++) {
291 pg
= alloc_page(GFP_KERNEL
);
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
))
299 sge
->length
= min_t(int, len
, PAGE_SIZE
);
300 sge
->lkey
= ndev
->pd
->local_dma_lkey
;
306 for (; i
>= 0; i
--, sg
--, sge
--) {
308 ib_dma_unmap_page(ndev
->device
, sge
->addr
,
309 sge
->length
, DMA_FROM_DEVICE
);
311 __free_page(sg_page(sg
));
316 static int nvmet_rdma_alloc_cmd(struct nvmet_rdma_device
*ndev
,
317 struct nvmet_rdma_cmd
*c
, bool admin
)
319 /* NVMe command / RDMA RECV */
320 c
->nvme_cmd
= kmalloc(sizeof(*c
->nvme_cmd
), GFP_KERNEL
);
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
))
329 c
->sge
[0].length
= sizeof(*c
->nvme_cmd
);
330 c
->sge
[0].lkey
= ndev
->pd
->local_dma_lkey
;
332 if (!admin
&& nvmet_rdma_alloc_inline_pages(ndev
, c
))
335 c
->cqe
.done
= nvmet_rdma_recv_done
;
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;
344 ib_dma_unmap_single(ndev
->device
, c
->sge
[0].addr
,
345 sizeof(*c
->nvme_cmd
), DMA_FROM_DEVICE
);
353 static void nvmet_rdma_free_cmd(struct nvmet_rdma_device
*ndev
,
354 struct nvmet_rdma_cmd
*c
, bool 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
);
363 static struct nvmet_rdma_cmd
*
364 nvmet_rdma_alloc_cmds(struct nvmet_rdma_device
*ndev
,
365 int nr_cmds
, bool admin
)
367 struct nvmet_rdma_cmd
*cmds
;
368 int ret
= -EINVAL
, i
;
370 cmds
= kcalloc(nr_cmds
, sizeof(struct nvmet_rdma_cmd
), GFP_KERNEL
);
374 for (i
= 0; i
< nr_cmds
; i
++) {
375 ret
= nvmet_rdma_alloc_cmd(ndev
, cmds
+ i
, admin
);
384 nvmet_rdma_free_cmd(ndev
, cmds
+ i
, admin
);
390 static void nvmet_rdma_free_cmds(struct nvmet_rdma_device
*ndev
,
391 struct nvmet_rdma_cmd
*cmds
, int nr_cmds
, bool admin
)
395 for (i
= 0; i
< nr_cmds
; i
++)
396 nvmet_rdma_free_cmd(ndev
, cmds
+ i
, admin
);
400 static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device
*ndev
,
401 struct nvmet_rdma_rsp
*r
, int tag
)
403 /* NVMe CQE / RDMA SEND */
404 r
->req
.cqe
= kmalloc(sizeof(*r
->req
.cqe
), GFP_KERNEL
);
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
))
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
;
418 r
->send_cqe
.done
= nvmet_rdma_send_done
;
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
;
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
;
439 static void nvmet_rdma_free_rsp(struct nvmet_rdma_device
*ndev
,
440 struct nvmet_rdma_rsp
*r
)
442 ib_dma_unmap_single(ndev
->device
, r
->send_sge
.addr
,
443 sizeof(*r
->req
.cqe
), DMA_TO_DEVICE
);
448 nvmet_rdma_alloc_rsps(struct nvmet_rdma_queue
*queue
)
450 struct nvmet_rdma_device
*ndev
= queue
->dev
;
451 int nr_rsps
= queue
->recv_queue_size
* 2;
452 int ret
= -ENOMEM
, i
;
454 if (sbitmap_init_node(&queue
->rsp_tags
, nr_rsps
, -1, GFP_KERNEL
,
455 NUMA_NO_NODE
, false, true))
458 queue
->rsps
= kcalloc(nr_rsps
, sizeof(struct nvmet_rdma_rsp
),
461 goto out_free_sbitmap
;
463 for (i
= 0; i
< nr_rsps
; i
++) {
464 struct nvmet_rdma_rsp
*rsp
= &queue
->rsps
[i
];
466 ret
= nvmet_rdma_alloc_rsp(ndev
, rsp
, i
);
475 nvmet_rdma_free_rsp(ndev
, &queue
->rsps
[i
]);
478 sbitmap_free(&queue
->rsp_tags
);
483 static void nvmet_rdma_free_rsps(struct nvmet_rdma_queue
*queue
)
485 struct nvmet_rdma_device
*ndev
= queue
->dev
;
486 int i
, nr_rsps
= queue
->recv_queue_size
* 2;
488 for (i
= 0; i
< nr_rsps
; i
++)
489 nvmet_rdma_free_rsp(ndev
, &queue
->rsps
[i
]);
491 sbitmap_free(&queue
->rsp_tags
);
494 static int nvmet_rdma_post_recv(struct nvmet_rdma_device
*ndev
,
495 struct nvmet_rdma_cmd
*cmd
)
499 ib_dma_sync_single_for_device(ndev
->device
,
500 cmd
->sge
[0].addr
, cmd
->sge
[0].length
,
504 ret
= ib_post_srq_recv(cmd
->nsrq
->srq
, &cmd
->wr
, NULL
);
506 ret
= ib_post_recv(cmd
->queue
->qp
, &cmd
->wr
, NULL
);
509 pr_err("post_recv cmd failed\n");
514 static void nvmet_rdma_process_wr_wait_list(struct nvmet_rdma_queue
*queue
)
516 spin_lock(&queue
->rsp_wr_wait_lock
);
517 while (!list_empty(&queue
->rsp_wr_wait_list
)) {
518 struct nvmet_rdma_rsp
*rsp
;
521 rsp
= list_entry(queue
->rsp_wr_wait_list
.next
,
522 struct nvmet_rdma_rsp
, wait_list
);
523 list_del(&rsp
->wait_list
);
525 spin_unlock(&queue
->rsp_wr_wait_lock
);
526 ret
= nvmet_rdma_execute_command(rsp
);
527 spin_lock(&queue
->rsp_wr_wait_lock
);
530 list_add(&rsp
->wait_list
, &queue
->rsp_wr_wait_list
);
534 spin_unlock(&queue
->rsp_wr_wait_lock
);
537 static u16
nvmet_rdma_check_pi_status(struct ib_mr
*sig_mr
)
539 struct ib_mr_status mr_status
;
543 ret
= ib_check_mr_status(sig_mr
, IB_MR_CHECK_SIG_STATUS
, &mr_status
);
545 pr_err("ib_check_mr_status failed, ret %d\n", ret
);
546 return NVME_SC_INVALID_PI
;
549 if (mr_status
.fail_status
& IB_MR_CHECK_SIG_STATUS
) {
550 switch (mr_status
.sig_err
.err_type
) {
551 case IB_SIG_BAD_GUARD
:
552 status
= NVME_SC_GUARD_CHECK
;
554 case IB_SIG_BAD_REFTAG
:
555 status
= NVME_SC_REFTAG_CHECK
;
557 case IB_SIG_BAD_APPTAG
:
558 status
= NVME_SC_APPTAG_CHECK
;
561 pr_err("PI error found type %d expected 0x%x vs actual 0x%x\n",
562 mr_status
.sig_err
.err_type
,
563 mr_status
.sig_err
.expected
,
564 mr_status
.sig_err
.actual
);
570 static void nvmet_rdma_set_sig_domain(struct blk_integrity
*bi
,
571 struct nvme_command
*cmd
, struct ib_sig_domain
*domain
,
572 u16 control
, u8 pi_type
)
574 domain
->sig_type
= IB_SIG_TYPE_T10_DIF
;
575 domain
->sig
.dif
.bg_type
= IB_T10DIF_CRC
;
576 domain
->sig
.dif
.pi_interval
= 1 << bi
->interval_exp
;
577 domain
->sig
.dif
.ref_tag
= le32_to_cpu(cmd
->rw
.reftag
);
578 if (control
& NVME_RW_PRINFO_PRCHK_REF
)
579 domain
->sig
.dif
.ref_remap
= true;
581 domain
->sig
.dif
.app_tag
= le16_to_cpu(cmd
->rw
.lbat
);
582 domain
->sig
.dif
.apptag_check_mask
= le16_to_cpu(cmd
->rw
.lbatm
);
583 domain
->sig
.dif
.app_escape
= true;
584 if (pi_type
== NVME_NS_DPS_PI_TYPE3
)
585 domain
->sig
.dif
.ref_escape
= true;
588 static void nvmet_rdma_set_sig_attrs(struct nvmet_req
*req
,
589 struct ib_sig_attrs
*sig_attrs
)
591 struct nvme_command
*cmd
= req
->cmd
;
592 u16 control
= le16_to_cpu(cmd
->rw
.control
);
593 u8 pi_type
= req
->ns
->pi_type
;
594 struct blk_integrity
*bi
;
596 bi
= bdev_get_integrity(req
->ns
->bdev
);
598 memset(sig_attrs
, 0, sizeof(*sig_attrs
));
600 if (control
& NVME_RW_PRINFO_PRACT
) {
601 /* for WRITE_INSERT/READ_STRIP no wire domain */
602 sig_attrs
->wire
.sig_type
= IB_SIG_TYPE_NONE
;
603 nvmet_rdma_set_sig_domain(bi
, cmd
, &sig_attrs
->mem
, control
,
605 /* Clear the PRACT bit since HCA will generate/verify the PI */
606 control
&= ~NVME_RW_PRINFO_PRACT
;
607 cmd
->rw
.control
= cpu_to_le16(control
);
608 /* PI is added by the HW */
609 req
->transfer_len
+= req
->metadata_len
;
611 /* for WRITE_PASS/READ_PASS both wire/memory domains exist */
612 nvmet_rdma_set_sig_domain(bi
, cmd
, &sig_attrs
->wire
, control
,
614 nvmet_rdma_set_sig_domain(bi
, cmd
, &sig_attrs
->mem
, control
,
618 if (control
& NVME_RW_PRINFO_PRCHK_REF
)
619 sig_attrs
->check_mask
|= IB_SIG_CHECK_REFTAG
;
620 if (control
& NVME_RW_PRINFO_PRCHK_GUARD
)
621 sig_attrs
->check_mask
|= IB_SIG_CHECK_GUARD
;
622 if (control
& NVME_RW_PRINFO_PRCHK_APP
)
623 sig_attrs
->check_mask
|= IB_SIG_CHECK_APPTAG
;
626 static int nvmet_rdma_rw_ctx_init(struct nvmet_rdma_rsp
*rsp
, u64 addr
, u32 key
,
627 struct ib_sig_attrs
*sig_attrs
)
629 struct rdma_cm_id
*cm_id
= rsp
->queue
->cm_id
;
630 struct nvmet_req
*req
= &rsp
->req
;
633 if (req
->metadata_len
)
634 ret
= rdma_rw_ctx_signature_init(&rsp
->rw
, cm_id
->qp
,
635 cm_id
->port_num
, req
->sg
, req
->sg_cnt
,
636 req
->metadata_sg
, req
->metadata_sg_cnt
, sig_attrs
,
637 addr
, key
, nvmet_data_dir(req
));
639 ret
= rdma_rw_ctx_init(&rsp
->rw
, cm_id
->qp
, cm_id
->port_num
,
640 req
->sg
, req
->sg_cnt
, 0, addr
, key
,
641 nvmet_data_dir(req
));
646 static void nvmet_rdma_rw_ctx_destroy(struct nvmet_rdma_rsp
*rsp
)
648 struct rdma_cm_id
*cm_id
= rsp
->queue
->cm_id
;
649 struct nvmet_req
*req
= &rsp
->req
;
651 if (req
->metadata_len
)
652 rdma_rw_ctx_destroy_signature(&rsp
->rw
, cm_id
->qp
,
653 cm_id
->port_num
, req
->sg
, req
->sg_cnt
,
654 req
->metadata_sg
, req
->metadata_sg_cnt
,
655 nvmet_data_dir(req
));
657 rdma_rw_ctx_destroy(&rsp
->rw
, cm_id
->qp
, cm_id
->port_num
,
658 req
->sg
, req
->sg_cnt
, nvmet_data_dir(req
));
661 static void nvmet_rdma_release_rsp(struct nvmet_rdma_rsp
*rsp
)
663 struct nvmet_rdma_queue
*queue
= rsp
->queue
;
665 atomic_add(1 + rsp
->n_rdma
, &queue
->sq_wr_avail
);
668 nvmet_rdma_rw_ctx_destroy(rsp
);
670 if (rsp
->req
.sg
!= rsp
->cmd
->inline_sg
)
671 nvmet_req_free_sgls(&rsp
->req
);
673 if (unlikely(!list_empty_careful(&queue
->rsp_wr_wait_list
)))
674 nvmet_rdma_process_wr_wait_list(queue
);
676 nvmet_rdma_put_rsp(rsp
);
679 static void nvmet_rdma_error_comp(struct nvmet_rdma_queue
*queue
)
681 if (queue
->nvme_sq
.ctrl
) {
682 nvmet_ctrl_fatal_error(queue
->nvme_sq
.ctrl
);
685 * we didn't setup the controller yet in case
686 * of admin connect error, just disconnect and
689 nvmet_rdma_queue_disconnect(queue
);
693 static void nvmet_rdma_send_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
695 struct nvmet_rdma_rsp
*rsp
=
696 container_of(wc
->wr_cqe
, struct nvmet_rdma_rsp
, send_cqe
);
697 struct nvmet_rdma_queue
*queue
= wc
->qp
->qp_context
;
699 nvmet_rdma_release_rsp(rsp
);
701 if (unlikely(wc
->status
!= IB_WC_SUCCESS
&&
702 wc
->status
!= IB_WC_WR_FLUSH_ERR
)) {
703 pr_err("SEND for CQE 0x%p failed with status %s (%d).\n",
704 wc
->wr_cqe
, ib_wc_status_msg(wc
->status
), wc
->status
);
705 nvmet_rdma_error_comp(queue
);
709 static void nvmet_rdma_queue_response(struct nvmet_req
*req
)
711 struct nvmet_rdma_rsp
*rsp
=
712 container_of(req
, struct nvmet_rdma_rsp
, req
);
713 struct rdma_cm_id
*cm_id
= rsp
->queue
->cm_id
;
714 struct ib_send_wr
*first_wr
;
716 if (rsp
->invalidate_rkey
) {
717 rsp
->send_wr
.opcode
= IB_WR_SEND_WITH_INV
;
718 rsp
->send_wr
.ex
.invalidate_rkey
= rsp
->invalidate_rkey
;
720 rsp
->send_wr
.opcode
= IB_WR_SEND
;
723 if (nvmet_rdma_need_data_out(rsp
)) {
724 if (rsp
->req
.metadata_len
)
725 first_wr
= rdma_rw_ctx_wrs(&rsp
->rw
, cm_id
->qp
,
726 cm_id
->port_num
, &rsp
->write_cqe
, NULL
);
728 first_wr
= rdma_rw_ctx_wrs(&rsp
->rw
, cm_id
->qp
,
729 cm_id
->port_num
, NULL
, &rsp
->send_wr
);
731 first_wr
= &rsp
->send_wr
;
734 nvmet_rdma_post_recv(rsp
->queue
->dev
, rsp
->cmd
);
736 ib_dma_sync_single_for_device(rsp
->queue
->dev
->device
,
737 rsp
->send_sge
.addr
, rsp
->send_sge
.length
,
740 if (unlikely(ib_post_send(cm_id
->qp
, first_wr
, NULL
))) {
741 pr_err("sending cmd response failed\n");
742 nvmet_rdma_release_rsp(rsp
);
746 static void nvmet_rdma_read_data_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
748 struct nvmet_rdma_rsp
*rsp
=
749 container_of(wc
->wr_cqe
, struct nvmet_rdma_rsp
, read_cqe
);
750 struct nvmet_rdma_queue
*queue
= wc
->qp
->qp_context
;
753 WARN_ON(rsp
->n_rdma
<= 0);
754 atomic_add(rsp
->n_rdma
, &queue
->sq_wr_avail
);
757 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
758 nvmet_rdma_rw_ctx_destroy(rsp
);
759 nvmet_req_uninit(&rsp
->req
);
760 nvmet_rdma_release_rsp(rsp
);
761 if (wc
->status
!= IB_WC_WR_FLUSH_ERR
) {
762 pr_info("RDMA READ for CQE 0x%p failed with status %s (%d).\n",
763 wc
->wr_cqe
, ib_wc_status_msg(wc
->status
), wc
->status
);
764 nvmet_rdma_error_comp(queue
);
769 if (rsp
->req
.metadata_len
)
770 status
= nvmet_rdma_check_pi_status(rsp
->rw
.reg
->mr
);
771 nvmet_rdma_rw_ctx_destroy(rsp
);
773 if (unlikely(status
))
774 nvmet_req_complete(&rsp
->req
, status
);
776 rsp
->req
.execute(&rsp
->req
);
779 static void nvmet_rdma_write_data_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
781 struct nvmet_rdma_rsp
*rsp
=
782 container_of(wc
->wr_cqe
, struct nvmet_rdma_rsp
, write_cqe
);
783 struct nvmet_rdma_queue
*queue
= wc
->qp
->qp_context
;
784 struct rdma_cm_id
*cm_id
= rsp
->queue
->cm_id
;
787 if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY
))
790 WARN_ON(rsp
->n_rdma
<= 0);
791 atomic_add(rsp
->n_rdma
, &queue
->sq_wr_avail
);
794 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
795 nvmet_rdma_rw_ctx_destroy(rsp
);
796 nvmet_req_uninit(&rsp
->req
);
797 nvmet_rdma_release_rsp(rsp
);
798 if (wc
->status
!= IB_WC_WR_FLUSH_ERR
) {
799 pr_info("RDMA WRITE for CQE failed with status %s (%d).\n",
800 ib_wc_status_msg(wc
->status
), wc
->status
);
801 nvmet_rdma_error_comp(queue
);
807 * Upon RDMA completion check the signature status
808 * - if succeeded send good NVMe response
809 * - if failed send bad NVMe response with appropriate error
811 status
= nvmet_rdma_check_pi_status(rsp
->rw
.reg
->mr
);
812 if (unlikely(status
))
813 rsp
->req
.cqe
->status
= cpu_to_le16(status
<< 1);
814 nvmet_rdma_rw_ctx_destroy(rsp
);
816 if (unlikely(ib_post_send(cm_id
->qp
, &rsp
->send_wr
, NULL
))) {
817 pr_err("sending cmd response failed\n");
818 nvmet_rdma_release_rsp(rsp
);
822 static void nvmet_rdma_use_inline_sg(struct nvmet_rdma_rsp
*rsp
, u32 len
,
825 int sg_count
= num_pages(len
);
826 struct scatterlist
*sg
;
829 sg
= rsp
->cmd
->inline_sg
;
830 for (i
= 0; i
< sg_count
; i
++, sg
++) {
831 if (i
< sg_count
- 1)
836 sg
->length
= min_t(int, len
, PAGE_SIZE
- off
);
842 rsp
->req
.sg
= rsp
->cmd
->inline_sg
;
843 rsp
->req
.sg_cnt
= sg_count
;
846 static u16
nvmet_rdma_map_sgl_inline(struct nvmet_rdma_rsp
*rsp
)
848 struct nvme_sgl_desc
*sgl
= &rsp
->req
.cmd
->common
.dptr
.sgl
;
849 u64 off
= le64_to_cpu(sgl
->addr
);
850 u32 len
= le32_to_cpu(sgl
->length
);
852 if (!nvme_is_write(rsp
->req
.cmd
)) {
854 offsetof(struct nvme_common_command
, opcode
);
855 return NVME_SC_INVALID_FIELD
| NVME_STATUS_DNR
;
858 if (off
+ len
> rsp
->queue
->dev
->inline_data_size
) {
859 pr_err("invalid inline data offset!\n");
860 return NVME_SC_SGL_INVALID_OFFSET
| NVME_STATUS_DNR
;
863 /* no data command? */
867 nvmet_rdma_use_inline_sg(rsp
, len
, off
);
868 rsp
->flags
|= NVMET_RDMA_REQ_INLINE_DATA
;
869 rsp
->req
.transfer_len
+= len
;
873 static u16
nvmet_rdma_map_sgl_keyed(struct nvmet_rdma_rsp
*rsp
,
874 struct nvme_keyed_sgl_desc
*sgl
, bool invalidate
)
876 u64 addr
= le64_to_cpu(sgl
->addr
);
877 u32 key
= get_unaligned_le32(sgl
->key
);
878 struct ib_sig_attrs sig_attrs
;
881 rsp
->req
.transfer_len
= get_unaligned_le24(sgl
->length
);
883 /* no data command? */
884 if (!rsp
->req
.transfer_len
)
887 if (rsp
->req
.metadata_len
)
888 nvmet_rdma_set_sig_attrs(&rsp
->req
, &sig_attrs
);
890 ret
= nvmet_req_alloc_sgls(&rsp
->req
);
891 if (unlikely(ret
< 0))
894 ret
= nvmet_rdma_rw_ctx_init(rsp
, addr
, key
, &sig_attrs
);
895 if (unlikely(ret
< 0))
900 rsp
->invalidate_rkey
= key
;
905 rsp
->req
.transfer_len
= 0;
906 return NVME_SC_INTERNAL
;
909 static u16
nvmet_rdma_map_sgl(struct nvmet_rdma_rsp
*rsp
)
911 struct nvme_keyed_sgl_desc
*sgl
= &rsp
->req
.cmd
->common
.dptr
.ksgl
;
913 switch (sgl
->type
>> 4) {
914 case NVME_SGL_FMT_DATA_DESC
:
915 switch (sgl
->type
& 0xf) {
916 case NVME_SGL_FMT_OFFSET
:
917 return nvmet_rdma_map_sgl_inline(rsp
);
919 pr_err("invalid SGL subtype: %#x\n", sgl
->type
);
921 offsetof(struct nvme_common_command
, dptr
);
922 return NVME_SC_INVALID_FIELD
| NVME_STATUS_DNR
;
924 case NVME_KEY_SGL_FMT_DATA_DESC
:
925 switch (sgl
->type
& 0xf) {
926 case NVME_SGL_FMT_ADDRESS
| NVME_SGL_FMT_INVALIDATE
:
927 return nvmet_rdma_map_sgl_keyed(rsp
, sgl
, true);
928 case NVME_SGL_FMT_ADDRESS
:
929 return nvmet_rdma_map_sgl_keyed(rsp
, sgl
, false);
931 pr_err("invalid SGL subtype: %#x\n", sgl
->type
);
933 offsetof(struct nvme_common_command
, dptr
);
934 return NVME_SC_INVALID_FIELD
| NVME_STATUS_DNR
;
937 pr_err("invalid SGL type: %#x\n", sgl
->type
);
938 rsp
->req
.error_loc
= offsetof(struct nvme_common_command
, dptr
);
939 return NVME_SC_SGL_INVALID_TYPE
| NVME_STATUS_DNR
;
943 static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp
*rsp
)
945 struct nvmet_rdma_queue
*queue
= rsp
->queue
;
947 if (unlikely(atomic_sub_return(1 + rsp
->n_rdma
,
948 &queue
->sq_wr_avail
) < 0)) {
949 pr_debug("IB send queue full (needed %d): queue %u cntlid %u\n",
950 1 + rsp
->n_rdma
, queue
->idx
,
951 queue
->nvme_sq
.ctrl
->cntlid
);
952 atomic_add(1 + rsp
->n_rdma
, &queue
->sq_wr_avail
);
956 if (nvmet_rdma_need_data_in(rsp
)) {
957 if (rdma_rw_ctx_post(&rsp
->rw
, queue
->qp
,
958 queue
->cm_id
->port_num
, &rsp
->read_cqe
, NULL
))
959 nvmet_req_complete(&rsp
->req
, NVME_SC_DATA_XFER_ERROR
);
961 rsp
->req
.execute(&rsp
->req
);
967 static void nvmet_rdma_handle_command(struct nvmet_rdma_queue
*queue
,
968 struct nvmet_rdma_rsp
*cmd
)
972 ib_dma_sync_single_for_cpu(queue
->dev
->device
,
973 cmd
->cmd
->sge
[0].addr
, cmd
->cmd
->sge
[0].length
,
975 ib_dma_sync_single_for_cpu(queue
->dev
->device
,
976 cmd
->send_sge
.addr
, cmd
->send_sge
.length
,
979 if (!nvmet_req_init(&cmd
->req
, &queue
->nvme_cq
,
980 &queue
->nvme_sq
, &nvmet_rdma_ops
))
983 status
= nvmet_rdma_map_sgl(cmd
);
987 if (unlikely(!nvmet_rdma_execute_command(cmd
))) {
988 spin_lock(&queue
->rsp_wr_wait_lock
);
989 list_add_tail(&cmd
->wait_list
, &queue
->rsp_wr_wait_list
);
990 spin_unlock(&queue
->rsp_wr_wait_lock
);
996 nvmet_req_complete(&cmd
->req
, status
);
999 static void nvmet_rdma_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
1001 struct nvmet_rdma_cmd
*cmd
=
1002 container_of(wc
->wr_cqe
, struct nvmet_rdma_cmd
, cqe
);
1003 struct nvmet_rdma_queue
*queue
= wc
->qp
->qp_context
;
1004 struct nvmet_rdma_rsp
*rsp
;
1006 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
1007 if (wc
->status
!= IB_WC_WR_FLUSH_ERR
) {
1008 pr_err("RECV for CQE 0x%p failed with status %s (%d)\n",
1009 wc
->wr_cqe
, ib_wc_status_msg(wc
->status
),
1011 nvmet_rdma_error_comp(queue
);
1016 if (unlikely(wc
->byte_len
< sizeof(struct nvme_command
))) {
1017 pr_err("Ctrl Fatal Error: capsule size less than 64 bytes\n");
1018 nvmet_rdma_error_comp(queue
);
1023 rsp
= nvmet_rdma_get_rsp(queue
);
1024 if (unlikely(!rsp
)) {
1026 * we get here only under memory pressure,
1027 * silently drop and have the host retry
1028 * as we can't even fail it.
1030 nvmet_rdma_post_recv(queue
->dev
, cmd
);
1036 rsp
->req
.cmd
= cmd
->nvme_cmd
;
1037 rsp
->req
.port
= queue
->port
;
1039 rsp
->invalidate_rkey
= 0;
1041 if (unlikely(queue
->state
!= NVMET_RDMA_Q_LIVE
)) {
1042 unsigned long flags
;
1044 spin_lock_irqsave(&queue
->state_lock
, flags
);
1045 if (queue
->state
== NVMET_RDMA_Q_CONNECTING
)
1046 list_add_tail(&rsp
->wait_list
, &queue
->rsp_wait_list
);
1048 nvmet_rdma_put_rsp(rsp
);
1049 spin_unlock_irqrestore(&queue
->state_lock
, flags
);
1053 nvmet_rdma_handle_command(queue
, rsp
);
1056 static void nvmet_rdma_destroy_srq(struct nvmet_rdma_srq
*nsrq
)
1058 nvmet_rdma_free_cmds(nsrq
->ndev
, nsrq
->cmds
, nsrq
->ndev
->srq_size
,
1060 ib_destroy_srq(nsrq
->srq
);
1065 static void nvmet_rdma_destroy_srqs(struct nvmet_rdma_device
*ndev
)
1072 for (i
= 0; i
< ndev
->srq_count
; i
++)
1073 nvmet_rdma_destroy_srq(ndev
->srqs
[i
]);
1078 static struct nvmet_rdma_srq
*
1079 nvmet_rdma_init_srq(struct nvmet_rdma_device
*ndev
)
1081 struct ib_srq_init_attr srq_attr
= { NULL
, };
1082 size_t srq_size
= ndev
->srq_size
;
1083 struct nvmet_rdma_srq
*nsrq
;
1087 nsrq
= kzalloc(sizeof(*nsrq
), GFP_KERNEL
);
1089 return ERR_PTR(-ENOMEM
);
1091 srq_attr
.attr
.max_wr
= srq_size
;
1092 srq_attr
.attr
.max_sge
= 1 + ndev
->inline_page_count
;
1093 srq_attr
.attr
.srq_limit
= 0;
1094 srq_attr
.srq_type
= IB_SRQT_BASIC
;
1095 srq
= ib_create_srq(ndev
->pd
, &srq_attr
);
1101 nsrq
->cmds
= nvmet_rdma_alloc_cmds(ndev
, srq_size
, false);
1102 if (IS_ERR(nsrq
->cmds
)) {
1103 ret
= PTR_ERR(nsrq
->cmds
);
1104 goto out_destroy_srq
;
1110 for (i
= 0; i
< srq_size
; i
++) {
1111 nsrq
->cmds
[i
].nsrq
= nsrq
;
1112 ret
= nvmet_rdma_post_recv(ndev
, &nsrq
->cmds
[i
]);
1120 nvmet_rdma_free_cmds(ndev
, nsrq
->cmds
, srq_size
, false);
1122 ib_destroy_srq(srq
);
1125 return ERR_PTR(ret
);
1128 static int nvmet_rdma_init_srqs(struct nvmet_rdma_device
*ndev
)
1132 if (!ndev
->device
->attrs
.max_srq_wr
|| !ndev
->device
->attrs
.max_srq
) {
1134 * If SRQs aren't supported we just go ahead and use normal
1135 * non-shared receive queues.
1137 pr_info("SRQ requested but not supported.\n");
1141 ndev
->srq_size
= min(ndev
->device
->attrs
.max_srq_wr
,
1142 nvmet_rdma_srq_size
);
1143 ndev
->srq_count
= min(ndev
->device
->num_comp_vectors
,
1144 ndev
->device
->attrs
.max_srq
);
1146 ndev
->srqs
= kcalloc(ndev
->srq_count
, sizeof(*ndev
->srqs
), GFP_KERNEL
);
1150 for (i
= 0; i
< ndev
->srq_count
; i
++) {
1151 ndev
->srqs
[i
] = nvmet_rdma_init_srq(ndev
);
1152 if (IS_ERR(ndev
->srqs
[i
])) {
1153 ret
= PTR_ERR(ndev
->srqs
[i
]);
1162 nvmet_rdma_destroy_srq(ndev
->srqs
[i
]);
1167 static void nvmet_rdma_free_dev(struct kref
*ref
)
1169 struct nvmet_rdma_device
*ndev
=
1170 container_of(ref
, struct nvmet_rdma_device
, ref
);
1172 mutex_lock(&device_list_mutex
);
1173 list_del(&ndev
->entry
);
1174 mutex_unlock(&device_list_mutex
);
1176 nvmet_rdma_destroy_srqs(ndev
);
1177 ib_dealloc_pd(ndev
->pd
);
1182 static struct nvmet_rdma_device
*
1183 nvmet_rdma_find_get_device(struct rdma_cm_id
*cm_id
)
1185 struct nvmet_rdma_port
*port
= cm_id
->context
;
1186 struct nvmet_port
*nport
= port
->nport
;
1187 struct nvmet_rdma_device
*ndev
;
1188 int inline_page_count
;
1189 int inline_sge_count
;
1192 mutex_lock(&device_list_mutex
);
1193 list_for_each_entry(ndev
, &device_list
, entry
) {
1194 if (ndev
->device
->node_guid
== cm_id
->device
->node_guid
&&
1195 kref_get_unless_zero(&ndev
->ref
))
1199 ndev
= kzalloc(sizeof(*ndev
), GFP_KERNEL
);
1203 inline_page_count
= num_pages(nport
->inline_data_size
);
1204 inline_sge_count
= max(cm_id
->device
->attrs
.max_sge_rd
,
1205 cm_id
->device
->attrs
.max_recv_sge
) - 1;
1206 if (inline_page_count
> inline_sge_count
) {
1207 pr_warn("inline_data_size %d cannot be supported by device %s. Reducing to %lu.\n",
1208 nport
->inline_data_size
, cm_id
->device
->name
,
1209 inline_sge_count
* PAGE_SIZE
);
1210 nport
->inline_data_size
= inline_sge_count
* PAGE_SIZE
;
1211 inline_page_count
= inline_sge_count
;
1213 ndev
->inline_data_size
= nport
->inline_data_size
;
1214 ndev
->inline_page_count
= inline_page_count
;
1216 if (nport
->pi_enable
&& !(cm_id
->device
->attrs
.kernel_cap_flags
&
1217 IBK_INTEGRITY_HANDOVER
)) {
1218 pr_warn("T10-PI is not supported by device %s. Disabling it\n",
1219 cm_id
->device
->name
);
1220 nport
->pi_enable
= false;
1223 ndev
->device
= cm_id
->device
;
1224 kref_init(&ndev
->ref
);
1226 ndev
->pd
= ib_alloc_pd(ndev
->device
, 0);
1227 if (IS_ERR(ndev
->pd
))
1230 if (nvmet_rdma_use_srq
) {
1231 ret
= nvmet_rdma_init_srqs(ndev
);
1236 list_add(&ndev
->entry
, &device_list
);
1238 mutex_unlock(&device_list_mutex
);
1239 pr_debug("added %s.\n", ndev
->device
->name
);
1243 ib_dealloc_pd(ndev
->pd
);
1247 mutex_unlock(&device_list_mutex
);
1251 static int nvmet_rdma_create_queue_ib(struct nvmet_rdma_queue
*queue
)
1253 struct ib_qp_init_attr qp_attr
= { };
1254 struct nvmet_rdma_device
*ndev
= queue
->dev
;
1255 int nr_cqe
, ret
, i
, factor
;
1258 * Reserve CQ slots for RECV + RDMA_READ/RDMA_WRITE + RDMA_SEND.
1260 nr_cqe
= queue
->recv_queue_size
+ 2 * queue
->send_queue_size
;
1262 queue
->cq
= ib_cq_pool_get(ndev
->device
, nr_cqe
+ 1,
1263 queue
->comp_vector
, IB_POLL_WORKQUEUE
);
1264 if (IS_ERR(queue
->cq
)) {
1265 ret
= PTR_ERR(queue
->cq
);
1266 pr_err("failed to create CQ cqe= %d ret= %d\n",
1271 qp_attr
.qp_context
= queue
;
1272 qp_attr
.event_handler
= nvmet_rdma_qp_event
;
1273 qp_attr
.send_cq
= queue
->cq
;
1274 qp_attr
.recv_cq
= queue
->cq
;
1275 qp_attr
.sq_sig_type
= IB_SIGNAL_REQ_WR
;
1276 qp_attr
.qp_type
= IB_QPT_RC
;
1278 qp_attr
.cap
.max_send_wr
= queue
->send_queue_size
+ 1;
1279 factor
= rdma_rw_mr_factor(ndev
->device
, queue
->cm_id
->port_num
,
1280 1 << NVMET_RDMA_MAX_MDTS
);
1281 qp_attr
.cap
.max_rdma_ctxs
= queue
->send_queue_size
* factor
;
1282 qp_attr
.cap
.max_send_sge
= max(ndev
->device
->attrs
.max_sge_rd
,
1283 ndev
->device
->attrs
.max_send_sge
);
1286 qp_attr
.srq
= queue
->nsrq
->srq
;
1289 qp_attr
.cap
.max_recv_wr
= 1 + queue
->recv_queue_size
;
1290 qp_attr
.cap
.max_recv_sge
= 1 + ndev
->inline_page_count
;
1293 if (queue
->port
->pi_enable
&& queue
->host_qid
)
1294 qp_attr
.create_flags
|= IB_QP_CREATE_INTEGRITY_EN
;
1296 ret
= rdma_create_qp(queue
->cm_id
, ndev
->pd
, &qp_attr
);
1298 pr_err("failed to create_qp ret= %d\n", ret
);
1299 goto err_destroy_cq
;
1301 queue
->qp
= queue
->cm_id
->qp
;
1303 atomic_set(&queue
->sq_wr_avail
, qp_attr
.cap
.max_send_wr
);
1305 pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1306 __func__
, queue
->cq
->cqe
, qp_attr
.cap
.max_send_sge
,
1307 qp_attr
.cap
.max_send_wr
, queue
->cm_id
);
1310 for (i
= 0; i
< queue
->recv_queue_size
; i
++) {
1311 queue
->cmds
[i
].queue
= queue
;
1312 ret
= nvmet_rdma_post_recv(ndev
, &queue
->cmds
[i
]);
1314 goto err_destroy_qp
;
1322 rdma_destroy_qp(queue
->cm_id
);
1324 ib_cq_pool_put(queue
->cq
, nr_cqe
+ 1);
1328 static void nvmet_rdma_destroy_queue_ib(struct nvmet_rdma_queue
*queue
)
1330 ib_drain_qp(queue
->qp
);
1332 rdma_destroy_id(queue
->cm_id
);
1333 ib_destroy_qp(queue
->qp
);
1334 ib_cq_pool_put(queue
->cq
, queue
->recv_queue_size
+ 2 *
1335 queue
->send_queue_size
+ 1);
1338 static void nvmet_rdma_free_queue(struct nvmet_rdma_queue
*queue
)
1340 pr_debug("freeing queue %d\n", queue
->idx
);
1342 nvmet_sq_destroy(&queue
->nvme_sq
);
1344 nvmet_rdma_destroy_queue_ib(queue
);
1346 nvmet_rdma_free_cmds(queue
->dev
, queue
->cmds
,
1347 queue
->recv_queue_size
,
1350 nvmet_rdma_free_rsps(queue
);
1351 ida_free(&nvmet_rdma_queue_ida
, queue
->idx
);
1355 static void nvmet_rdma_release_queue_work(struct work_struct
*w
)
1357 struct nvmet_rdma_queue
*queue
=
1358 container_of(w
, struct nvmet_rdma_queue
, release_work
);
1359 struct nvmet_rdma_device
*dev
= queue
->dev
;
1361 nvmet_rdma_free_queue(queue
);
1363 kref_put(&dev
->ref
, nvmet_rdma_free_dev
);
1367 nvmet_rdma_parse_cm_connect_req(struct rdma_conn_param
*conn
,
1368 struct nvmet_rdma_queue
*queue
)
1370 struct nvme_rdma_cm_req
*req
;
1372 req
= (struct nvme_rdma_cm_req
*)conn
->private_data
;
1373 if (!req
|| conn
->private_data_len
== 0)
1374 return NVME_RDMA_CM_INVALID_LEN
;
1376 if (le16_to_cpu(req
->recfmt
) != NVME_RDMA_CM_FMT_1_0
)
1377 return NVME_RDMA_CM_INVALID_RECFMT
;
1379 queue
->host_qid
= le16_to_cpu(req
->qid
);
1382 * req->hsqsize corresponds to our recv queue size plus 1
1383 * req->hrqsize corresponds to our send queue size
1385 queue
->recv_queue_size
= le16_to_cpu(req
->hsqsize
) + 1;
1386 queue
->send_queue_size
= le16_to_cpu(req
->hrqsize
);
1388 if (!queue
->host_qid
&& queue
->recv_queue_size
> NVME_AQ_DEPTH
)
1389 return NVME_RDMA_CM_INVALID_HSQSIZE
;
1391 /* XXX: Should we enforce some kind of max for IO queues? */
1396 static int nvmet_rdma_cm_reject(struct rdma_cm_id
*cm_id
,
1397 enum nvme_rdma_cm_status status
)
1399 struct nvme_rdma_cm_rej rej
;
1401 pr_debug("rejecting connect request: status %d (%s)\n",
1402 status
, nvme_rdma_cm_msg(status
));
1404 rej
.recfmt
= cpu_to_le16(NVME_RDMA_CM_FMT_1_0
);
1405 rej
.sts
= cpu_to_le16(status
);
1407 return rdma_reject(cm_id
, (void *)&rej
, sizeof(rej
),
1408 IB_CM_REJ_CONSUMER_DEFINED
);
1411 static struct nvmet_rdma_queue
*
1412 nvmet_rdma_alloc_queue(struct nvmet_rdma_device
*ndev
,
1413 struct rdma_cm_id
*cm_id
,
1414 struct rdma_cm_event
*event
)
1416 struct nvmet_rdma_port
*port
= cm_id
->context
;
1417 struct nvmet_rdma_queue
*queue
;
1420 queue
= kzalloc(sizeof(*queue
), GFP_KERNEL
);
1422 ret
= NVME_RDMA_CM_NO_RSC
;
1426 ret
= nvmet_sq_init(&queue
->nvme_sq
);
1428 ret
= NVME_RDMA_CM_NO_RSC
;
1429 goto out_free_queue
;
1432 ret
= nvmet_rdma_parse_cm_connect_req(&event
->param
.conn
, queue
);
1434 goto out_destroy_sq
;
1437 * Schedules the actual release because calling rdma_destroy_id from
1438 * inside a CM callback would trigger a deadlock. (great API design..)
1440 INIT_WORK(&queue
->release_work
, nvmet_rdma_release_queue_work
);
1442 queue
->cm_id
= cm_id
;
1443 queue
->port
= port
->nport
;
1445 spin_lock_init(&queue
->state_lock
);
1446 queue
->state
= NVMET_RDMA_Q_CONNECTING
;
1447 INIT_LIST_HEAD(&queue
->rsp_wait_list
);
1448 INIT_LIST_HEAD(&queue
->rsp_wr_wait_list
);
1449 spin_lock_init(&queue
->rsp_wr_wait_lock
);
1450 INIT_LIST_HEAD(&queue
->queue_list
);
1452 queue
->idx
= ida_alloc(&nvmet_rdma_queue_ida
, GFP_KERNEL
);
1453 if (queue
->idx
< 0) {
1454 ret
= NVME_RDMA_CM_NO_RSC
;
1455 goto out_destroy_sq
;
1459 * Spread the io queues across completion vectors,
1460 * but still keep all admin queues on vector 0.
1462 queue
->comp_vector
= !queue
->host_qid
? 0 :
1463 queue
->idx
% ndev
->device
->num_comp_vectors
;
1466 ret
= nvmet_rdma_alloc_rsps(queue
);
1468 ret
= NVME_RDMA_CM_NO_RSC
;
1469 goto out_ida_remove
;
1473 queue
->nsrq
= ndev
->srqs
[queue
->comp_vector
% ndev
->srq_count
];
1475 queue
->cmds
= nvmet_rdma_alloc_cmds(ndev
,
1476 queue
->recv_queue_size
,
1478 if (IS_ERR(queue
->cmds
)) {
1479 ret
= NVME_RDMA_CM_NO_RSC
;
1480 goto out_free_responses
;
1484 ret
= nvmet_rdma_create_queue_ib(queue
);
1486 pr_err("%s: creating RDMA queue failed (%d).\n",
1488 ret
= NVME_RDMA_CM_NO_RSC
;
1496 nvmet_rdma_free_cmds(queue
->dev
, queue
->cmds
,
1497 queue
->recv_queue_size
,
1501 nvmet_rdma_free_rsps(queue
);
1503 ida_free(&nvmet_rdma_queue_ida
, queue
->idx
);
1505 nvmet_sq_destroy(&queue
->nvme_sq
);
1509 nvmet_rdma_cm_reject(cm_id
, ret
);
1513 static void nvmet_rdma_qp_event(struct ib_event
*event
, void *priv
)
1515 struct nvmet_rdma_queue
*queue
= priv
;
1517 switch (event
->event
) {
1518 case IB_EVENT_COMM_EST
:
1519 rdma_notify(queue
->cm_id
, event
->event
);
1521 case IB_EVENT_QP_LAST_WQE_REACHED
:
1522 pr_debug("received last WQE reached event for queue=0x%p\n",
1526 pr_err("received IB QP event: %s (%d)\n",
1527 ib_event_msg(event
->event
), event
->event
);
1532 static int nvmet_rdma_cm_accept(struct rdma_cm_id
*cm_id
,
1533 struct nvmet_rdma_queue
*queue
,
1534 struct rdma_conn_param
*p
)
1536 struct rdma_conn_param param
= { };
1537 struct nvme_rdma_cm_rep priv
= { };
1540 param
.rnr_retry_count
= 7;
1541 param
.flow_control
= 1;
1542 param
.initiator_depth
= min_t(u8
, p
->initiator_depth
,
1543 queue
->dev
->device
->attrs
.max_qp_init_rd_atom
);
1544 param
.private_data
= &priv
;
1545 param
.private_data_len
= sizeof(priv
);
1546 priv
.recfmt
= cpu_to_le16(NVME_RDMA_CM_FMT_1_0
);
1547 priv
.crqsize
= cpu_to_le16(queue
->recv_queue_size
);
1549 ret
= rdma_accept(cm_id
, ¶m
);
1551 pr_err("rdma_accept failed (error code = %d)\n", ret
);
1556 static int nvmet_rdma_queue_connect(struct rdma_cm_id
*cm_id
,
1557 struct rdma_cm_event
*event
)
1559 struct nvmet_rdma_device
*ndev
;
1560 struct nvmet_rdma_queue
*queue
;
1563 ndev
= nvmet_rdma_find_get_device(cm_id
);
1565 nvmet_rdma_cm_reject(cm_id
, NVME_RDMA_CM_NO_RSC
);
1566 return -ECONNREFUSED
;
1569 queue
= nvmet_rdma_alloc_queue(ndev
, cm_id
, event
);
1575 if (queue
->host_qid
== 0) {
1576 struct nvmet_rdma_queue
*q
;
1579 /* Check for pending controller teardown */
1580 mutex_lock(&nvmet_rdma_queue_mutex
);
1581 list_for_each_entry(q
, &nvmet_rdma_queue_list
, queue_list
) {
1582 if (q
->nvme_sq
.ctrl
== queue
->nvme_sq
.ctrl
&&
1583 q
->state
== NVMET_RDMA_Q_DISCONNECTING
)
1586 mutex_unlock(&nvmet_rdma_queue_mutex
);
1587 if (pending
> NVMET_RDMA_BACKLOG
)
1588 return NVME_SC_CONNECT_CTRL_BUSY
;
1591 ret
= nvmet_rdma_cm_accept(cm_id
, queue
, &event
->param
.conn
);
1594 * Don't destroy the cm_id in free path, as we implicitly
1595 * destroy the cm_id here with non-zero ret code.
1597 queue
->cm_id
= NULL
;
1601 mutex_lock(&nvmet_rdma_queue_mutex
);
1602 list_add_tail(&queue
->queue_list
, &nvmet_rdma_queue_list
);
1603 mutex_unlock(&nvmet_rdma_queue_mutex
);
1608 nvmet_rdma_free_queue(queue
);
1610 kref_put(&ndev
->ref
, nvmet_rdma_free_dev
);
1615 static void nvmet_rdma_queue_established(struct nvmet_rdma_queue
*queue
)
1617 unsigned long flags
;
1619 spin_lock_irqsave(&queue
->state_lock
, flags
);
1620 if (queue
->state
!= NVMET_RDMA_Q_CONNECTING
) {
1621 pr_warn("trying to establish a connected queue\n");
1624 queue
->state
= NVMET_RDMA_Q_LIVE
;
1626 while (!list_empty(&queue
->rsp_wait_list
)) {
1627 struct nvmet_rdma_rsp
*cmd
;
1629 cmd
= list_first_entry(&queue
->rsp_wait_list
,
1630 struct nvmet_rdma_rsp
, wait_list
);
1631 list_del(&cmd
->wait_list
);
1633 spin_unlock_irqrestore(&queue
->state_lock
, flags
);
1634 nvmet_rdma_handle_command(queue
, cmd
);
1635 spin_lock_irqsave(&queue
->state_lock
, flags
);
1639 spin_unlock_irqrestore(&queue
->state_lock
, flags
);
1642 static void __nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue
*queue
)
1644 bool disconnect
= false;
1645 unsigned long flags
;
1647 pr_debug("cm_id= %p queue->state= %d\n", queue
->cm_id
, queue
->state
);
1649 spin_lock_irqsave(&queue
->state_lock
, flags
);
1650 switch (queue
->state
) {
1651 case NVMET_RDMA_Q_CONNECTING
:
1652 while (!list_empty(&queue
->rsp_wait_list
)) {
1653 struct nvmet_rdma_rsp
*rsp
;
1655 rsp
= list_first_entry(&queue
->rsp_wait_list
,
1656 struct nvmet_rdma_rsp
,
1658 list_del(&rsp
->wait_list
);
1659 nvmet_rdma_put_rsp(rsp
);
1662 case NVMET_RDMA_Q_LIVE
:
1663 queue
->state
= NVMET_RDMA_Q_DISCONNECTING
;
1666 case NVMET_RDMA_Q_DISCONNECTING
:
1669 spin_unlock_irqrestore(&queue
->state_lock
, flags
);
1672 rdma_disconnect(queue
->cm_id
);
1673 queue_work(nvmet_wq
, &queue
->release_work
);
1677 static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue
*queue
)
1679 bool disconnect
= false;
1681 mutex_lock(&nvmet_rdma_queue_mutex
);
1682 if (!list_empty(&queue
->queue_list
)) {
1683 list_del_init(&queue
->queue_list
);
1686 mutex_unlock(&nvmet_rdma_queue_mutex
);
1689 __nvmet_rdma_queue_disconnect(queue
);
1692 static void nvmet_rdma_queue_connect_fail(struct rdma_cm_id
*cm_id
,
1693 struct nvmet_rdma_queue
*queue
)
1695 WARN_ON_ONCE(queue
->state
!= NVMET_RDMA_Q_CONNECTING
);
1697 mutex_lock(&nvmet_rdma_queue_mutex
);
1698 if (!list_empty(&queue
->queue_list
))
1699 list_del_init(&queue
->queue_list
);
1700 mutex_unlock(&nvmet_rdma_queue_mutex
);
1702 pr_err("failed to connect queue %d\n", queue
->idx
);
1703 queue_work(nvmet_wq
, &queue
->release_work
);
1707 * nvmet_rdma_device_removal() - Handle RDMA device removal
1708 * @cm_id: rdma_cm id, used for nvmet port
1709 * @queue: nvmet rdma queue (cm id qp_context)
1711 * DEVICE_REMOVAL event notifies us that the RDMA device is about
1712 * to unplug. Note that this event can be generated on a normal
1713 * queue cm_id and/or a device bound listener cm_id (where in this
1714 * case queue will be null).
1716 * We registered an ib_client to handle device removal for queues,
1717 * so we only need to handle the listening port cm_ids. In this case
1718 * we nullify the priv to prevent double cm_id destruction and destroying
1719 * the cm_id implicitely by returning a non-zero rc to the callout.
1721 static int nvmet_rdma_device_removal(struct rdma_cm_id
*cm_id
,
1722 struct nvmet_rdma_queue
*queue
)
1724 struct nvmet_rdma_port
*port
;
1728 * This is a queue cm_id. we have registered
1729 * an ib_client to handle queues removal
1730 * so don't interfear and just return.
1735 port
= cm_id
->context
;
1738 * This is a listener cm_id. Make sure that
1739 * future remove_port won't invoke a double
1740 * cm_id destroy. use atomic xchg to make sure
1741 * we don't compete with remove_port.
1743 if (xchg(&port
->cm_id
, NULL
) != cm_id
)
1747 * We need to return 1 so that the core will destroy
1748 * it's own ID. What a great API design..
1753 static int nvmet_rdma_cm_handler(struct rdma_cm_id
*cm_id
,
1754 struct rdma_cm_event
*event
)
1756 struct nvmet_rdma_queue
*queue
= NULL
;
1760 queue
= cm_id
->qp
->qp_context
;
1762 pr_debug("%s (%d): status %d id %p\n",
1763 rdma_event_msg(event
->event
), event
->event
,
1764 event
->status
, cm_id
);
1766 switch (event
->event
) {
1767 case RDMA_CM_EVENT_CONNECT_REQUEST
:
1768 ret
= nvmet_rdma_queue_connect(cm_id
, event
);
1770 case RDMA_CM_EVENT_ESTABLISHED
:
1771 nvmet_rdma_queue_established(queue
);
1773 case RDMA_CM_EVENT_ADDR_CHANGE
:
1775 struct nvmet_rdma_port
*port
= cm_id
->context
;
1777 queue_delayed_work(nvmet_wq
, &port
->repair_work
, 0);
1781 case RDMA_CM_EVENT_DISCONNECTED
:
1782 case RDMA_CM_EVENT_TIMEWAIT_EXIT
:
1783 nvmet_rdma_queue_disconnect(queue
);
1785 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
1786 ret
= nvmet_rdma_device_removal(cm_id
, queue
);
1788 case RDMA_CM_EVENT_REJECTED
:
1789 pr_debug("Connection rejected: %s\n",
1790 rdma_reject_msg(cm_id
, event
->status
));
1792 case RDMA_CM_EVENT_UNREACHABLE
:
1793 case RDMA_CM_EVENT_CONNECT_ERROR
:
1794 nvmet_rdma_queue_connect_fail(cm_id
, queue
);
1797 pr_err("received unrecognized RDMA CM event %d\n",
1805 static void nvmet_rdma_delete_ctrl(struct nvmet_ctrl
*ctrl
)
1807 struct nvmet_rdma_queue
*queue
, *n
;
1809 mutex_lock(&nvmet_rdma_queue_mutex
);
1810 list_for_each_entry_safe(queue
, n
, &nvmet_rdma_queue_list
, queue_list
) {
1811 if (queue
->nvme_sq
.ctrl
!= ctrl
)
1813 list_del_init(&queue
->queue_list
);
1814 __nvmet_rdma_queue_disconnect(queue
);
1816 mutex_unlock(&nvmet_rdma_queue_mutex
);
1819 static void nvmet_rdma_destroy_port_queues(struct nvmet_rdma_port
*port
)
1821 struct nvmet_rdma_queue
*queue
, *tmp
;
1822 struct nvmet_port
*nport
= port
->nport
;
1824 mutex_lock(&nvmet_rdma_queue_mutex
);
1825 list_for_each_entry_safe(queue
, tmp
, &nvmet_rdma_queue_list
,
1827 if (queue
->port
!= nport
)
1830 list_del_init(&queue
->queue_list
);
1831 __nvmet_rdma_queue_disconnect(queue
);
1833 mutex_unlock(&nvmet_rdma_queue_mutex
);
1836 static void nvmet_rdma_disable_port(struct nvmet_rdma_port
*port
)
1838 struct rdma_cm_id
*cm_id
= xchg(&port
->cm_id
, NULL
);
1841 rdma_destroy_id(cm_id
);
1844 * Destroy the remaining queues, which are not belong to any
1845 * controller yet. Do it here after the RDMA-CM was destroyed
1846 * guarantees that no new queue will be created.
1848 nvmet_rdma_destroy_port_queues(port
);
1851 static int nvmet_rdma_enable_port(struct nvmet_rdma_port
*port
)
1853 struct sockaddr
*addr
= (struct sockaddr
*)&port
->addr
;
1854 struct rdma_cm_id
*cm_id
;
1857 cm_id
= rdma_create_id(&init_net
, nvmet_rdma_cm_handler
, port
,
1858 RDMA_PS_TCP
, IB_QPT_RC
);
1859 if (IS_ERR(cm_id
)) {
1860 pr_err("CM ID creation failed\n");
1861 return PTR_ERR(cm_id
);
1865 * Allow both IPv4 and IPv6 sockets to bind a single port
1868 ret
= rdma_set_afonly(cm_id
, 1);
1870 pr_err("rdma_set_afonly failed (%d)\n", ret
);
1871 goto out_destroy_id
;
1874 ret
= rdma_bind_addr(cm_id
, addr
);
1876 pr_err("binding CM ID to %pISpcs failed (%d)\n", addr
, ret
);
1877 goto out_destroy_id
;
1880 ret
= rdma_listen(cm_id
, NVMET_RDMA_BACKLOG
);
1882 pr_err("listening to %pISpcs failed (%d)\n", addr
, ret
);
1883 goto out_destroy_id
;
1886 port
->cm_id
= cm_id
;
1890 rdma_destroy_id(cm_id
);
1894 static void nvmet_rdma_repair_port_work(struct work_struct
*w
)
1896 struct nvmet_rdma_port
*port
= container_of(to_delayed_work(w
),
1897 struct nvmet_rdma_port
, repair_work
);
1900 nvmet_rdma_disable_port(port
);
1901 ret
= nvmet_rdma_enable_port(port
);
1903 queue_delayed_work(nvmet_wq
, &port
->repair_work
, 5 * HZ
);
1906 static int nvmet_rdma_add_port(struct nvmet_port
*nport
)
1908 struct nvmet_rdma_port
*port
;
1909 __kernel_sa_family_t af
;
1912 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
1917 port
->nport
= nport
;
1918 INIT_DELAYED_WORK(&port
->repair_work
, nvmet_rdma_repair_port_work
);
1920 switch (nport
->disc_addr
.adrfam
) {
1921 case NVMF_ADDR_FAMILY_IP4
:
1924 case NVMF_ADDR_FAMILY_IP6
:
1928 pr_err("address family %d not supported\n",
1929 nport
->disc_addr
.adrfam
);
1934 if (nport
->inline_data_size
< 0) {
1935 nport
->inline_data_size
= NVMET_RDMA_DEFAULT_INLINE_DATA_SIZE
;
1936 } else if (nport
->inline_data_size
> NVMET_RDMA_MAX_INLINE_DATA_SIZE
) {
1937 pr_warn("inline_data_size %u is too large, reducing to %u\n",
1938 nport
->inline_data_size
,
1939 NVMET_RDMA_MAX_INLINE_DATA_SIZE
);
1940 nport
->inline_data_size
= NVMET_RDMA_MAX_INLINE_DATA_SIZE
;
1943 if (nport
->max_queue_size
< 0) {
1944 nport
->max_queue_size
= NVME_RDMA_DEFAULT_QUEUE_SIZE
;
1945 } else if (nport
->max_queue_size
> NVME_RDMA_MAX_QUEUE_SIZE
) {
1946 pr_warn("max_queue_size %u is too large, reducing to %u\n",
1947 nport
->max_queue_size
, NVME_RDMA_MAX_QUEUE_SIZE
);
1948 nport
->max_queue_size
= NVME_RDMA_MAX_QUEUE_SIZE
;
1951 ret
= inet_pton_with_scope(&init_net
, af
, nport
->disc_addr
.traddr
,
1952 nport
->disc_addr
.trsvcid
, &port
->addr
);
1954 pr_err("malformed ip/port passed: %s:%s\n",
1955 nport
->disc_addr
.traddr
, nport
->disc_addr
.trsvcid
);
1959 ret
= nvmet_rdma_enable_port(port
);
1963 pr_info("enabling port %d (%pISpcs)\n",
1964 le16_to_cpu(nport
->disc_addr
.portid
),
1965 (struct sockaddr
*)&port
->addr
);
1974 static void nvmet_rdma_remove_port(struct nvmet_port
*nport
)
1976 struct nvmet_rdma_port
*port
= nport
->priv
;
1978 cancel_delayed_work_sync(&port
->repair_work
);
1979 nvmet_rdma_disable_port(port
);
1983 static void nvmet_rdma_disc_port_addr(struct nvmet_req
*req
,
1984 struct nvmet_port
*nport
, char *traddr
)
1986 struct nvmet_rdma_port
*port
= nport
->priv
;
1987 struct rdma_cm_id
*cm_id
= port
->cm_id
;
1989 if (inet_addr_is_any((struct sockaddr
*)&cm_id
->route
.addr
.src_addr
)) {
1990 struct nvmet_rdma_rsp
*rsp
=
1991 container_of(req
, struct nvmet_rdma_rsp
, req
);
1992 struct rdma_cm_id
*req_cm_id
= rsp
->queue
->cm_id
;
1993 struct sockaddr
*addr
= (void *)&req_cm_id
->route
.addr
.src_addr
;
1995 sprintf(traddr
, "%pISc", addr
);
1997 memcpy(traddr
, nport
->disc_addr
.traddr
, NVMF_TRADDR_SIZE
);
2001 static ssize_t
nvmet_rdma_host_port_addr(struct nvmet_ctrl
*ctrl
,
2002 char *traddr
, size_t traddr_len
)
2004 struct nvmet_sq
*nvme_sq
= ctrl
->sqs
[0];
2005 struct nvmet_rdma_queue
*queue
=
2006 container_of(nvme_sq
, struct nvmet_rdma_queue
, nvme_sq
);
2008 return snprintf(traddr
, traddr_len
, "%pISc",
2009 (struct sockaddr
*)&queue
->cm_id
->route
.addr
.dst_addr
);
2012 static u8
nvmet_rdma_get_mdts(const struct nvmet_ctrl
*ctrl
)
2014 if (ctrl
->pi_support
)
2015 return NVMET_RDMA_MAX_METADATA_MDTS
;
2016 return NVMET_RDMA_MAX_MDTS
;
2019 static u16
nvmet_rdma_get_max_queue_size(const struct nvmet_ctrl
*ctrl
)
2021 if (ctrl
->pi_support
)
2022 return NVME_RDMA_MAX_METADATA_QUEUE_SIZE
;
2023 return NVME_RDMA_MAX_QUEUE_SIZE
;
2026 static const struct nvmet_fabrics_ops nvmet_rdma_ops
= {
2027 .owner
= THIS_MODULE
,
2028 .type
= NVMF_TRTYPE_RDMA
,
2030 .flags
= NVMF_KEYED_SGLS
| NVMF_METADATA_SUPPORTED
,
2031 .add_port
= nvmet_rdma_add_port
,
2032 .remove_port
= nvmet_rdma_remove_port
,
2033 .queue_response
= nvmet_rdma_queue_response
,
2034 .delete_ctrl
= nvmet_rdma_delete_ctrl
,
2035 .disc_traddr
= nvmet_rdma_disc_port_addr
,
2036 .host_traddr
= nvmet_rdma_host_port_addr
,
2037 .get_mdts
= nvmet_rdma_get_mdts
,
2038 .get_max_queue_size
= nvmet_rdma_get_max_queue_size
,
2041 static void nvmet_rdma_remove_one(struct ib_device
*ib_device
, void *client_data
)
2043 struct nvmet_rdma_queue
*queue
, *tmp
;
2044 struct nvmet_rdma_device
*ndev
;
2047 mutex_lock(&device_list_mutex
);
2048 list_for_each_entry(ndev
, &device_list
, entry
) {
2049 if (ndev
->device
== ib_device
) {
2054 mutex_unlock(&device_list_mutex
);
2060 * IB Device that is used by nvmet controllers is being removed,
2061 * delete all queues using this device.
2063 mutex_lock(&nvmet_rdma_queue_mutex
);
2064 list_for_each_entry_safe(queue
, tmp
, &nvmet_rdma_queue_list
,
2066 if (queue
->dev
->device
!= ib_device
)
2069 pr_info("Removing queue %d\n", queue
->idx
);
2070 list_del_init(&queue
->queue_list
);
2071 __nvmet_rdma_queue_disconnect(queue
);
2073 mutex_unlock(&nvmet_rdma_queue_mutex
);
2075 flush_workqueue(nvmet_wq
);
2078 static struct ib_client nvmet_rdma_ib_client
= {
2079 .name
= "nvmet_rdma",
2080 .remove
= nvmet_rdma_remove_one
2083 static int __init
nvmet_rdma_init(void)
2087 ret
= ib_register_client(&nvmet_rdma_ib_client
);
2091 ret
= nvmet_register_transport(&nvmet_rdma_ops
);
2098 ib_unregister_client(&nvmet_rdma_ib_client
);
2102 static void __exit
nvmet_rdma_exit(void)
2104 nvmet_unregister_transport(&nvmet_rdma_ops
);
2105 ib_unregister_client(&nvmet_rdma_ib_client
);
2106 WARN_ON_ONCE(!list_empty(&nvmet_rdma_queue_list
));
2107 ida_destroy(&nvmet_rdma_queue_ida
);
2110 module_init(nvmet_rdma_init
);
2111 module_exit(nvmet_rdma_exit
);
2113 MODULE_DESCRIPTION("NVMe target RDMA transport driver");
2114 MODULE_LICENSE("GPL v2");
2115 MODULE_ALIAS("nvmet-transport-1"); /* 1 == NVMF_TRTYPE_RDMA */