1 // SPDX-License-Identifier: GPL-2.0
3 * NVMe over Fabrics RDMA host code.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <rdma/mr_pool.h>
11 #include <linux/err.h>
12 #include <linux/string.h>
13 #include <linux/atomic.h>
14 #include <linux/blk-mq.h>
15 #include <linux/blk-mq-rdma.h>
16 #include <linux/types.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <linux/scatterlist.h>
20 #include <linux/nvme.h>
21 #include <asm/unaligned.h>
23 #include <rdma/ib_verbs.h>
24 #include <rdma/rdma_cm.h>
25 #include <linux/nvme-rdma.h>
31 #define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
33 #define NVME_RDMA_MAX_SEGMENTS 256
35 #define NVME_RDMA_MAX_INLINE_SEGMENTS 4
37 struct nvme_rdma_device
{
38 struct ib_device
*dev
;
41 struct list_head entry
;
42 unsigned int num_inline_segments
;
51 struct nvme_rdma_queue
;
52 struct nvme_rdma_request
{
53 struct nvme_request req
;
55 struct nvme_rdma_qe sqe
;
56 union nvme_result result
;
59 struct ib_sge sge
[1 + NVME_RDMA_MAX_INLINE_SEGMENTS
];
62 struct ib_reg_wr reg_wr
;
63 struct ib_cqe reg_cqe
;
64 struct nvme_rdma_queue
*queue
;
65 struct sg_table sg_table
;
66 struct scatterlist first_sgl
[];
69 enum nvme_rdma_queue_flags
{
70 NVME_RDMA_Q_ALLOCATED
= 0,
72 NVME_RDMA_Q_TR_READY
= 2,
75 struct nvme_rdma_queue
{
76 struct nvme_rdma_qe
*rsp_ring
;
78 size_t cmnd_capsule_len
;
79 struct nvme_rdma_ctrl
*ctrl
;
80 struct nvme_rdma_device
*device
;
85 struct rdma_cm_id
*cm_id
;
87 struct completion cm_done
;
90 struct nvme_rdma_ctrl
{
91 /* read only in the hot path */
92 struct nvme_rdma_queue
*queues
;
94 /* other member variables */
95 struct blk_mq_tag_set tag_set
;
96 struct work_struct err_work
;
98 struct nvme_rdma_qe async_event_sqe
;
100 struct delayed_work reconnect_work
;
102 struct list_head list
;
104 struct blk_mq_tag_set admin_tag_set
;
105 struct nvme_rdma_device
*device
;
109 struct sockaddr_storage addr
;
110 struct sockaddr_storage src_addr
;
112 struct nvme_ctrl ctrl
;
113 bool use_inline_data
;
114 u32 io_queues
[HCTX_MAX_TYPES
];
117 static inline struct nvme_rdma_ctrl
*to_rdma_ctrl(struct nvme_ctrl
*ctrl
)
119 return container_of(ctrl
, struct nvme_rdma_ctrl
, ctrl
);
122 static LIST_HEAD(device_list
);
123 static DEFINE_MUTEX(device_list_mutex
);
125 static LIST_HEAD(nvme_rdma_ctrl_list
);
126 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex
);
129 * Disabling this option makes small I/O goes faster, but is fundamentally
130 * unsafe. With it turned off we will have to register a global rkey that
131 * allows read and write access to all physical memory.
133 static bool register_always
= true;
134 module_param(register_always
, bool, 0444);
135 MODULE_PARM_DESC(register_always
,
136 "Use memory registration even for contiguous memory regions");
138 static int nvme_rdma_cm_handler(struct rdma_cm_id
*cm_id
,
139 struct rdma_cm_event
*event
);
140 static void nvme_rdma_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
);
142 static const struct blk_mq_ops nvme_rdma_mq_ops
;
143 static const struct blk_mq_ops nvme_rdma_admin_mq_ops
;
145 /* XXX: really should move to a generic header sooner or later.. */
146 static inline void put_unaligned_le24(u32 val
, u8
*p
)
153 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue
*queue
)
155 return queue
- queue
->ctrl
->queues
;
158 static bool nvme_rdma_poll_queue(struct nvme_rdma_queue
*queue
)
160 return nvme_rdma_queue_idx(queue
) >
161 queue
->ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] +
162 queue
->ctrl
->io_queues
[HCTX_TYPE_READ
];
165 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue
*queue
)
167 return queue
->cmnd_capsule_len
- sizeof(struct nvme_command
);
170 static void nvme_rdma_free_qe(struct ib_device
*ibdev
, struct nvme_rdma_qe
*qe
,
171 size_t capsule_size
, enum dma_data_direction dir
)
173 ib_dma_unmap_single(ibdev
, qe
->dma
, capsule_size
, dir
);
177 static int nvme_rdma_alloc_qe(struct ib_device
*ibdev
, struct nvme_rdma_qe
*qe
,
178 size_t capsule_size
, enum dma_data_direction dir
)
180 qe
->data
= kzalloc(capsule_size
, GFP_KERNEL
);
184 qe
->dma
= ib_dma_map_single(ibdev
, qe
->data
, capsule_size
, dir
);
185 if (ib_dma_mapping_error(ibdev
, qe
->dma
)) {
194 static void nvme_rdma_free_ring(struct ib_device
*ibdev
,
195 struct nvme_rdma_qe
*ring
, size_t ib_queue_size
,
196 size_t capsule_size
, enum dma_data_direction dir
)
200 for (i
= 0; i
< ib_queue_size
; i
++)
201 nvme_rdma_free_qe(ibdev
, &ring
[i
], capsule_size
, dir
);
205 static struct nvme_rdma_qe
*nvme_rdma_alloc_ring(struct ib_device
*ibdev
,
206 size_t ib_queue_size
, size_t capsule_size
,
207 enum dma_data_direction dir
)
209 struct nvme_rdma_qe
*ring
;
212 ring
= kcalloc(ib_queue_size
, sizeof(struct nvme_rdma_qe
), GFP_KERNEL
);
217 * Bind the CQEs (post recv buffers) DMA mapping to the RDMA queue
218 * lifetime. It's safe, since any chage in the underlying RDMA device
219 * will issue error recovery and queue re-creation.
221 for (i
= 0; i
< ib_queue_size
; i
++) {
222 if (nvme_rdma_alloc_qe(ibdev
, &ring
[i
], capsule_size
, dir
))
229 nvme_rdma_free_ring(ibdev
, ring
, i
, capsule_size
, dir
);
233 static void nvme_rdma_qp_event(struct ib_event
*event
, void *context
)
235 pr_debug("QP event %s (%d)\n",
236 ib_event_msg(event
->event
), event
->event
);
240 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue
*queue
)
244 ret
= wait_for_completion_interruptible_timeout(&queue
->cm_done
,
245 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS
) + 1);
250 WARN_ON_ONCE(queue
->cm_error
> 0);
251 return queue
->cm_error
;
254 static int nvme_rdma_create_qp(struct nvme_rdma_queue
*queue
, const int factor
)
256 struct nvme_rdma_device
*dev
= queue
->device
;
257 struct ib_qp_init_attr init_attr
;
260 memset(&init_attr
, 0, sizeof(init_attr
));
261 init_attr
.event_handler
= nvme_rdma_qp_event
;
263 init_attr
.cap
.max_send_wr
= factor
* queue
->queue_size
+ 1;
265 init_attr
.cap
.max_recv_wr
= queue
->queue_size
+ 1;
266 init_attr
.cap
.max_recv_sge
= 1;
267 init_attr
.cap
.max_send_sge
= 1 + dev
->num_inline_segments
;
268 init_attr
.sq_sig_type
= IB_SIGNAL_REQ_WR
;
269 init_attr
.qp_type
= IB_QPT_RC
;
270 init_attr
.send_cq
= queue
->ib_cq
;
271 init_attr
.recv_cq
= queue
->ib_cq
;
273 ret
= rdma_create_qp(queue
->cm_id
, dev
->pd
, &init_attr
);
275 queue
->qp
= queue
->cm_id
->qp
;
279 static void nvme_rdma_exit_request(struct blk_mq_tag_set
*set
,
280 struct request
*rq
, unsigned int hctx_idx
)
282 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
284 kfree(req
->sqe
.data
);
287 static int nvme_rdma_init_request(struct blk_mq_tag_set
*set
,
288 struct request
*rq
, unsigned int hctx_idx
,
289 unsigned int numa_node
)
291 struct nvme_rdma_ctrl
*ctrl
= set
->driver_data
;
292 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
293 int queue_idx
= (set
== &ctrl
->tag_set
) ? hctx_idx
+ 1 : 0;
294 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[queue_idx
];
296 nvme_req(rq
)->ctrl
= &ctrl
->ctrl
;
297 req
->sqe
.data
= kzalloc(sizeof(struct nvme_command
), GFP_KERNEL
);
306 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
307 unsigned int hctx_idx
)
309 struct nvme_rdma_ctrl
*ctrl
= data
;
310 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[hctx_idx
+ 1];
312 BUG_ON(hctx_idx
>= ctrl
->ctrl
.queue_count
);
314 hctx
->driver_data
= queue
;
318 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
319 unsigned int hctx_idx
)
321 struct nvme_rdma_ctrl
*ctrl
= data
;
322 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[0];
324 BUG_ON(hctx_idx
!= 0);
326 hctx
->driver_data
= queue
;
330 static void nvme_rdma_free_dev(struct kref
*ref
)
332 struct nvme_rdma_device
*ndev
=
333 container_of(ref
, struct nvme_rdma_device
, ref
);
335 mutex_lock(&device_list_mutex
);
336 list_del(&ndev
->entry
);
337 mutex_unlock(&device_list_mutex
);
339 ib_dealloc_pd(ndev
->pd
);
343 static void nvme_rdma_dev_put(struct nvme_rdma_device
*dev
)
345 kref_put(&dev
->ref
, nvme_rdma_free_dev
);
348 static int nvme_rdma_dev_get(struct nvme_rdma_device
*dev
)
350 return kref_get_unless_zero(&dev
->ref
);
353 static struct nvme_rdma_device
*
354 nvme_rdma_find_get_device(struct rdma_cm_id
*cm_id
)
356 struct nvme_rdma_device
*ndev
;
358 mutex_lock(&device_list_mutex
);
359 list_for_each_entry(ndev
, &device_list
, entry
) {
360 if (ndev
->dev
->node_guid
== cm_id
->device
->node_guid
&&
361 nvme_rdma_dev_get(ndev
))
365 ndev
= kzalloc(sizeof(*ndev
), GFP_KERNEL
);
369 ndev
->dev
= cm_id
->device
;
370 kref_init(&ndev
->ref
);
372 ndev
->pd
= ib_alloc_pd(ndev
->dev
,
373 register_always
? 0 : IB_PD_UNSAFE_GLOBAL_RKEY
);
374 if (IS_ERR(ndev
->pd
))
377 if (!(ndev
->dev
->attrs
.device_cap_flags
&
378 IB_DEVICE_MEM_MGT_EXTENSIONS
)) {
379 dev_err(&ndev
->dev
->dev
,
380 "Memory registrations not supported.\n");
384 ndev
->num_inline_segments
= min(NVME_RDMA_MAX_INLINE_SEGMENTS
,
385 ndev
->dev
->attrs
.max_send_sge
- 1);
386 list_add(&ndev
->entry
, &device_list
);
388 mutex_unlock(&device_list_mutex
);
392 ib_dealloc_pd(ndev
->pd
);
396 mutex_unlock(&device_list_mutex
);
400 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue
*queue
)
402 struct nvme_rdma_device
*dev
;
403 struct ib_device
*ibdev
;
405 if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY
, &queue
->flags
))
411 ib_mr_pool_destroy(queue
->qp
, &queue
->qp
->rdma_mrs
);
414 * The cm_id object might have been destroyed during RDMA connection
415 * establishment error flow to avoid getting other cma events, thus
416 * the destruction of the QP shouldn't use rdma_cm API.
418 ib_destroy_qp(queue
->qp
);
419 ib_free_cq(queue
->ib_cq
);
421 nvme_rdma_free_ring(ibdev
, queue
->rsp_ring
, queue
->queue_size
,
422 sizeof(struct nvme_completion
), DMA_FROM_DEVICE
);
424 nvme_rdma_dev_put(dev
);
427 static int nvme_rdma_get_max_fr_pages(struct ib_device
*ibdev
)
429 return min_t(u32
, NVME_RDMA_MAX_SEGMENTS
,
430 ibdev
->attrs
.max_fast_reg_page_list_len
- 1);
433 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue
*queue
)
435 struct ib_device
*ibdev
;
436 const int send_wr_factor
= 3; /* MR, SEND, INV */
437 const int cq_factor
= send_wr_factor
+ 1; /* + RECV */
438 int comp_vector
, idx
= nvme_rdma_queue_idx(queue
);
439 enum ib_poll_context poll_ctx
;
440 int ret
, pages_per_mr
;
442 queue
->device
= nvme_rdma_find_get_device(queue
->cm_id
);
443 if (!queue
->device
) {
444 dev_err(queue
->cm_id
->device
->dev
.parent
,
445 "no client data found!\n");
446 return -ECONNREFUSED
;
448 ibdev
= queue
->device
->dev
;
451 * Spread I/O queues completion vectors according their queue index.
452 * Admin queues can always go on completion vector 0.
454 comp_vector
= idx
== 0 ? idx
: idx
- 1;
456 /* Polling queues need direct cq polling context */
457 if (nvme_rdma_poll_queue(queue
))
458 poll_ctx
= IB_POLL_DIRECT
;
460 poll_ctx
= IB_POLL_SOFTIRQ
;
462 /* +1 for ib_stop_cq */
463 queue
->ib_cq
= ib_alloc_cq(ibdev
, queue
,
464 cq_factor
* queue
->queue_size
+ 1,
465 comp_vector
, poll_ctx
);
466 if (IS_ERR(queue
->ib_cq
)) {
467 ret
= PTR_ERR(queue
->ib_cq
);
471 ret
= nvme_rdma_create_qp(queue
, send_wr_factor
);
473 goto out_destroy_ib_cq
;
475 queue
->rsp_ring
= nvme_rdma_alloc_ring(ibdev
, queue
->queue_size
,
476 sizeof(struct nvme_completion
), DMA_FROM_DEVICE
);
477 if (!queue
->rsp_ring
) {
483 * Currently we don't use SG_GAPS MR's so if the first entry is
484 * misaligned we'll end up using two entries for a single data page,
485 * so one additional entry is required.
487 pages_per_mr
= nvme_rdma_get_max_fr_pages(ibdev
) + 1;
488 ret
= ib_mr_pool_init(queue
->qp
, &queue
->qp
->rdma_mrs
,
493 dev_err(queue
->ctrl
->ctrl
.device
,
494 "failed to initialize MR pool sized %d for QID %d\n",
495 queue
->queue_size
, idx
);
496 goto out_destroy_ring
;
499 set_bit(NVME_RDMA_Q_TR_READY
, &queue
->flags
);
504 nvme_rdma_free_ring(ibdev
, queue
->rsp_ring
, queue
->queue_size
,
505 sizeof(struct nvme_completion
), DMA_FROM_DEVICE
);
507 rdma_destroy_qp(queue
->cm_id
);
509 ib_free_cq(queue
->ib_cq
);
511 nvme_rdma_dev_put(queue
->device
);
515 static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl
*ctrl
,
516 int idx
, size_t queue_size
)
518 struct nvme_rdma_queue
*queue
;
519 struct sockaddr
*src_addr
= NULL
;
522 queue
= &ctrl
->queues
[idx
];
524 init_completion(&queue
->cm_done
);
527 queue
->cmnd_capsule_len
= ctrl
->ctrl
.ioccsz
* 16;
529 queue
->cmnd_capsule_len
= sizeof(struct nvme_command
);
531 queue
->queue_size
= queue_size
;
533 queue
->cm_id
= rdma_create_id(&init_net
, nvme_rdma_cm_handler
, queue
,
534 RDMA_PS_TCP
, IB_QPT_RC
);
535 if (IS_ERR(queue
->cm_id
)) {
536 dev_info(ctrl
->ctrl
.device
,
537 "failed to create CM ID: %ld\n", PTR_ERR(queue
->cm_id
));
538 return PTR_ERR(queue
->cm_id
);
541 if (ctrl
->ctrl
.opts
->mask
& NVMF_OPT_HOST_TRADDR
)
542 src_addr
= (struct sockaddr
*)&ctrl
->src_addr
;
544 queue
->cm_error
= -ETIMEDOUT
;
545 ret
= rdma_resolve_addr(queue
->cm_id
, src_addr
,
546 (struct sockaddr
*)&ctrl
->addr
,
547 NVME_RDMA_CONNECT_TIMEOUT_MS
);
549 dev_info(ctrl
->ctrl
.device
,
550 "rdma_resolve_addr failed (%d).\n", ret
);
551 goto out_destroy_cm_id
;
554 ret
= nvme_rdma_wait_for_cm(queue
);
556 dev_info(ctrl
->ctrl
.device
,
557 "rdma connection establishment failed (%d)\n", ret
);
558 goto out_destroy_cm_id
;
561 set_bit(NVME_RDMA_Q_ALLOCATED
, &queue
->flags
);
566 rdma_destroy_id(queue
->cm_id
);
567 nvme_rdma_destroy_queue_ib(queue
);
571 static void __nvme_rdma_stop_queue(struct nvme_rdma_queue
*queue
)
573 rdma_disconnect(queue
->cm_id
);
574 ib_drain_qp(queue
->qp
);
577 static void nvme_rdma_stop_queue(struct nvme_rdma_queue
*queue
)
579 if (!test_and_clear_bit(NVME_RDMA_Q_LIVE
, &queue
->flags
))
581 __nvme_rdma_stop_queue(queue
);
584 static void nvme_rdma_free_queue(struct nvme_rdma_queue
*queue
)
586 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED
, &queue
->flags
))
589 nvme_rdma_destroy_queue_ib(queue
);
590 rdma_destroy_id(queue
->cm_id
);
593 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl
*ctrl
)
597 for (i
= 1; i
< ctrl
->ctrl
.queue_count
; i
++)
598 nvme_rdma_free_queue(&ctrl
->queues
[i
]);
601 static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl
*ctrl
)
605 for (i
= 1; i
< ctrl
->ctrl
.queue_count
; i
++)
606 nvme_rdma_stop_queue(&ctrl
->queues
[i
]);
609 static int nvme_rdma_start_queue(struct nvme_rdma_ctrl
*ctrl
, int idx
)
611 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[idx
];
612 bool poll
= nvme_rdma_poll_queue(queue
);
616 ret
= nvmf_connect_io_queue(&ctrl
->ctrl
, idx
, poll
);
618 ret
= nvmf_connect_admin_queue(&ctrl
->ctrl
);
621 set_bit(NVME_RDMA_Q_LIVE
, &queue
->flags
);
623 if (test_bit(NVME_RDMA_Q_ALLOCATED
, &queue
->flags
))
624 __nvme_rdma_stop_queue(queue
);
625 dev_info(ctrl
->ctrl
.device
,
626 "failed to connect queue: %d ret=%d\n", idx
, ret
);
631 static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl
*ctrl
)
635 for (i
= 1; i
< ctrl
->ctrl
.queue_count
; i
++) {
636 ret
= nvme_rdma_start_queue(ctrl
, i
);
638 goto out_stop_queues
;
644 for (i
--; i
>= 1; i
--)
645 nvme_rdma_stop_queue(&ctrl
->queues
[i
]);
649 static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl
*ctrl
)
651 struct nvmf_ctrl_options
*opts
= ctrl
->ctrl
.opts
;
652 struct ib_device
*ibdev
= ctrl
->device
->dev
;
653 unsigned int nr_io_queues
, nr_default_queues
;
654 unsigned int nr_read_queues
, nr_poll_queues
;
657 nr_read_queues
= min_t(unsigned int, ibdev
->num_comp_vectors
,
658 min(opts
->nr_io_queues
, num_online_cpus()));
659 nr_default_queues
= min_t(unsigned int, ibdev
->num_comp_vectors
,
660 min(opts
->nr_write_queues
, num_online_cpus()));
661 nr_poll_queues
= min(opts
->nr_poll_queues
, num_online_cpus());
662 nr_io_queues
= nr_read_queues
+ nr_default_queues
+ nr_poll_queues
;
664 ret
= nvme_set_queue_count(&ctrl
->ctrl
, &nr_io_queues
);
668 ctrl
->ctrl
.queue_count
= nr_io_queues
+ 1;
669 if (ctrl
->ctrl
.queue_count
< 2)
672 dev_info(ctrl
->ctrl
.device
,
673 "creating %d I/O queues.\n", nr_io_queues
);
675 if (opts
->nr_write_queues
&& nr_read_queues
< nr_io_queues
) {
677 * separate read/write queues
678 * hand out dedicated default queues only after we have
679 * sufficient read queues.
681 ctrl
->io_queues
[HCTX_TYPE_READ
] = nr_read_queues
;
682 nr_io_queues
-= ctrl
->io_queues
[HCTX_TYPE_READ
];
683 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] =
684 min(nr_default_queues
, nr_io_queues
);
685 nr_io_queues
-= ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
688 * shared read/write queues
689 * either no write queues were requested, or we don't have
690 * sufficient queue count to have dedicated default queues.
692 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] =
693 min(nr_read_queues
, nr_io_queues
);
694 nr_io_queues
-= ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
697 if (opts
->nr_poll_queues
&& nr_io_queues
) {
698 /* map dedicated poll queues only if we have queues left */
699 ctrl
->io_queues
[HCTX_TYPE_POLL
] =
700 min(nr_poll_queues
, nr_io_queues
);
703 for (i
= 1; i
< ctrl
->ctrl
.queue_count
; i
++) {
704 ret
= nvme_rdma_alloc_queue(ctrl
, i
,
705 ctrl
->ctrl
.sqsize
+ 1);
707 goto out_free_queues
;
713 for (i
--; i
>= 1; i
--)
714 nvme_rdma_free_queue(&ctrl
->queues
[i
]);
719 static struct blk_mq_tag_set
*nvme_rdma_alloc_tagset(struct nvme_ctrl
*nctrl
,
722 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(nctrl
);
723 struct blk_mq_tag_set
*set
;
727 set
= &ctrl
->admin_tag_set
;
728 memset(set
, 0, sizeof(*set
));
729 set
->ops
= &nvme_rdma_admin_mq_ops
;
730 set
->queue_depth
= NVME_AQ_MQ_TAG_DEPTH
;
731 set
->reserved_tags
= 2; /* connect + keep-alive */
732 set
->numa_node
= nctrl
->numa_node
;
733 set
->cmd_size
= sizeof(struct nvme_rdma_request
) +
734 NVME_INLINE_SG_CNT
* sizeof(struct scatterlist
);
735 set
->driver_data
= ctrl
;
736 set
->nr_hw_queues
= 1;
737 set
->timeout
= ADMIN_TIMEOUT
;
738 set
->flags
= BLK_MQ_F_NO_SCHED
;
740 set
= &ctrl
->tag_set
;
741 memset(set
, 0, sizeof(*set
));
742 set
->ops
= &nvme_rdma_mq_ops
;
743 set
->queue_depth
= nctrl
->sqsize
+ 1;
744 set
->reserved_tags
= 1; /* fabric connect */
745 set
->numa_node
= nctrl
->numa_node
;
746 set
->flags
= BLK_MQ_F_SHOULD_MERGE
;
747 set
->cmd_size
= sizeof(struct nvme_rdma_request
) +
748 NVME_INLINE_SG_CNT
* sizeof(struct scatterlist
);
749 set
->driver_data
= ctrl
;
750 set
->nr_hw_queues
= nctrl
->queue_count
- 1;
751 set
->timeout
= NVME_IO_TIMEOUT
;
752 set
->nr_maps
= nctrl
->opts
->nr_poll_queues
? HCTX_MAX_TYPES
: 2;
755 ret
= blk_mq_alloc_tag_set(set
);
762 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl
*ctrl
,
766 blk_cleanup_queue(ctrl
->ctrl
.admin_q
);
767 blk_cleanup_queue(ctrl
->ctrl
.fabrics_q
);
768 blk_mq_free_tag_set(ctrl
->ctrl
.admin_tagset
);
770 if (ctrl
->async_event_sqe
.data
) {
771 nvme_rdma_free_qe(ctrl
->device
->dev
, &ctrl
->async_event_sqe
,
772 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
773 ctrl
->async_event_sqe
.data
= NULL
;
775 nvme_rdma_free_queue(&ctrl
->queues
[0]);
778 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl
*ctrl
,
783 error
= nvme_rdma_alloc_queue(ctrl
, 0, NVME_AQ_DEPTH
);
787 ctrl
->device
= ctrl
->queues
[0].device
;
788 ctrl
->ctrl
.numa_node
= dev_to_node(ctrl
->device
->dev
->dma_device
);
790 ctrl
->max_fr_pages
= nvme_rdma_get_max_fr_pages(ctrl
->device
->dev
);
793 * Bind the async event SQE DMA mapping to the admin queue lifetime.
794 * It's safe, since any chage in the underlying RDMA device will issue
795 * error recovery and queue re-creation.
797 error
= nvme_rdma_alloc_qe(ctrl
->device
->dev
, &ctrl
->async_event_sqe
,
798 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
803 ctrl
->ctrl
.admin_tagset
= nvme_rdma_alloc_tagset(&ctrl
->ctrl
, true);
804 if (IS_ERR(ctrl
->ctrl
.admin_tagset
)) {
805 error
= PTR_ERR(ctrl
->ctrl
.admin_tagset
);
806 goto out_free_async_qe
;
809 ctrl
->ctrl
.fabrics_q
= blk_mq_init_queue(&ctrl
->admin_tag_set
);
810 if (IS_ERR(ctrl
->ctrl
.fabrics_q
)) {
811 error
= PTR_ERR(ctrl
->ctrl
.fabrics_q
);
812 goto out_free_tagset
;
815 ctrl
->ctrl
.admin_q
= blk_mq_init_queue(&ctrl
->admin_tag_set
);
816 if (IS_ERR(ctrl
->ctrl
.admin_q
)) {
817 error
= PTR_ERR(ctrl
->ctrl
.admin_q
);
818 goto out_cleanup_fabrics_q
;
822 error
= nvme_rdma_start_queue(ctrl
, 0);
824 goto out_cleanup_queue
;
826 error
= nvme_enable_ctrl(&ctrl
->ctrl
);
830 ctrl
->ctrl
.max_segments
= ctrl
->max_fr_pages
;
831 ctrl
->ctrl
.max_hw_sectors
= ctrl
->max_fr_pages
<< (ilog2(SZ_4K
) - 9);
833 blk_mq_unquiesce_queue(ctrl
->ctrl
.admin_q
);
835 error
= nvme_init_identify(&ctrl
->ctrl
);
842 nvme_rdma_stop_queue(&ctrl
->queues
[0]);
845 blk_cleanup_queue(ctrl
->ctrl
.admin_q
);
846 out_cleanup_fabrics_q
:
848 blk_cleanup_queue(ctrl
->ctrl
.fabrics_q
);
851 blk_mq_free_tag_set(ctrl
->ctrl
.admin_tagset
);
853 nvme_rdma_free_qe(ctrl
->device
->dev
, &ctrl
->async_event_sqe
,
854 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
855 ctrl
->async_event_sqe
.data
= NULL
;
857 nvme_rdma_free_queue(&ctrl
->queues
[0]);
861 static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl
*ctrl
,
865 blk_cleanup_queue(ctrl
->ctrl
.connect_q
);
866 blk_mq_free_tag_set(ctrl
->ctrl
.tagset
);
868 nvme_rdma_free_io_queues(ctrl
);
871 static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl
*ctrl
, bool new)
875 ret
= nvme_rdma_alloc_io_queues(ctrl
);
880 ctrl
->ctrl
.tagset
= nvme_rdma_alloc_tagset(&ctrl
->ctrl
, false);
881 if (IS_ERR(ctrl
->ctrl
.tagset
)) {
882 ret
= PTR_ERR(ctrl
->ctrl
.tagset
);
883 goto out_free_io_queues
;
886 ctrl
->ctrl
.connect_q
= blk_mq_init_queue(&ctrl
->tag_set
);
887 if (IS_ERR(ctrl
->ctrl
.connect_q
)) {
888 ret
= PTR_ERR(ctrl
->ctrl
.connect_q
);
889 goto out_free_tag_set
;
892 blk_mq_update_nr_hw_queues(&ctrl
->tag_set
,
893 ctrl
->ctrl
.queue_count
- 1);
896 ret
= nvme_rdma_start_io_queues(ctrl
);
898 goto out_cleanup_connect_q
;
902 out_cleanup_connect_q
:
904 blk_cleanup_queue(ctrl
->ctrl
.connect_q
);
907 blk_mq_free_tag_set(ctrl
->ctrl
.tagset
);
909 nvme_rdma_free_io_queues(ctrl
);
913 static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl
*ctrl
,
916 blk_mq_quiesce_queue(ctrl
->ctrl
.admin_q
);
917 nvme_rdma_stop_queue(&ctrl
->queues
[0]);
918 if (ctrl
->ctrl
.admin_tagset
) {
919 blk_mq_tagset_busy_iter(ctrl
->ctrl
.admin_tagset
,
920 nvme_cancel_request
, &ctrl
->ctrl
);
921 blk_mq_tagset_wait_completed_request(ctrl
->ctrl
.admin_tagset
);
924 blk_mq_unquiesce_queue(ctrl
->ctrl
.admin_q
);
925 nvme_rdma_destroy_admin_queue(ctrl
, remove
);
928 static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl
*ctrl
,
931 if (ctrl
->ctrl
.queue_count
> 1) {
932 nvme_stop_queues(&ctrl
->ctrl
);
933 nvme_rdma_stop_io_queues(ctrl
);
934 if (ctrl
->ctrl
.tagset
) {
935 blk_mq_tagset_busy_iter(ctrl
->ctrl
.tagset
,
936 nvme_cancel_request
, &ctrl
->ctrl
);
937 blk_mq_tagset_wait_completed_request(ctrl
->ctrl
.tagset
);
940 nvme_start_queues(&ctrl
->ctrl
);
941 nvme_rdma_destroy_io_queues(ctrl
, remove
);
945 static void nvme_rdma_free_ctrl(struct nvme_ctrl
*nctrl
)
947 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(nctrl
);
949 if (list_empty(&ctrl
->list
))
952 mutex_lock(&nvme_rdma_ctrl_mutex
);
953 list_del(&ctrl
->list
);
954 mutex_unlock(&nvme_rdma_ctrl_mutex
);
956 nvmf_free_options(nctrl
->opts
);
962 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl
*ctrl
)
964 /* If we are resetting/deleting then do nothing */
965 if (ctrl
->ctrl
.state
!= NVME_CTRL_CONNECTING
) {
966 WARN_ON_ONCE(ctrl
->ctrl
.state
== NVME_CTRL_NEW
||
967 ctrl
->ctrl
.state
== NVME_CTRL_LIVE
);
971 if (nvmf_should_reconnect(&ctrl
->ctrl
)) {
972 dev_info(ctrl
->ctrl
.device
, "Reconnecting in %d seconds...\n",
973 ctrl
->ctrl
.opts
->reconnect_delay
);
974 queue_delayed_work(nvme_wq
, &ctrl
->reconnect_work
,
975 ctrl
->ctrl
.opts
->reconnect_delay
* HZ
);
977 nvme_delete_ctrl(&ctrl
->ctrl
);
981 static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl
*ctrl
, bool new)
986 ret
= nvme_rdma_configure_admin_queue(ctrl
, new);
990 if (ctrl
->ctrl
.icdoff
) {
991 dev_err(ctrl
->ctrl
.device
, "icdoff is not supported!\n");
995 if (!(ctrl
->ctrl
.sgls
& (1 << 2))) {
996 dev_err(ctrl
->ctrl
.device
,
997 "Mandatory keyed sgls are not supported!\n");
1001 if (ctrl
->ctrl
.opts
->queue_size
> ctrl
->ctrl
.sqsize
+ 1) {
1002 dev_warn(ctrl
->ctrl
.device
,
1003 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1004 ctrl
->ctrl
.opts
->queue_size
, ctrl
->ctrl
.sqsize
+ 1);
1007 if (ctrl
->ctrl
.sqsize
+ 1 > ctrl
->ctrl
.maxcmd
) {
1008 dev_warn(ctrl
->ctrl
.device
,
1009 "sqsize %u > ctrl maxcmd %u, clamping down\n",
1010 ctrl
->ctrl
.sqsize
+ 1, ctrl
->ctrl
.maxcmd
);
1011 ctrl
->ctrl
.sqsize
= ctrl
->ctrl
.maxcmd
- 1;
1014 if (ctrl
->ctrl
.sgls
& (1 << 20))
1015 ctrl
->use_inline_data
= true;
1017 if (ctrl
->ctrl
.queue_count
> 1) {
1018 ret
= nvme_rdma_configure_io_queues(ctrl
, new);
1023 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_LIVE
);
1025 /* state change failure is ok if we're in DELETING state */
1026 WARN_ON_ONCE(ctrl
->ctrl
.state
!= NVME_CTRL_DELETING
);
1031 nvme_start_ctrl(&ctrl
->ctrl
);
1035 if (ctrl
->ctrl
.queue_count
> 1)
1036 nvme_rdma_destroy_io_queues(ctrl
, new);
1038 nvme_rdma_stop_queue(&ctrl
->queues
[0]);
1039 nvme_rdma_destroy_admin_queue(ctrl
, new);
1043 static void nvme_rdma_reconnect_ctrl_work(struct work_struct
*work
)
1045 struct nvme_rdma_ctrl
*ctrl
= container_of(to_delayed_work(work
),
1046 struct nvme_rdma_ctrl
, reconnect_work
);
1048 ++ctrl
->ctrl
.nr_reconnects
;
1050 if (nvme_rdma_setup_ctrl(ctrl
, false))
1053 dev_info(ctrl
->ctrl
.device
, "Successfully reconnected (%d attempts)\n",
1054 ctrl
->ctrl
.nr_reconnects
);
1056 ctrl
->ctrl
.nr_reconnects
= 0;
1061 dev_info(ctrl
->ctrl
.device
, "Failed reconnect attempt %d\n",
1062 ctrl
->ctrl
.nr_reconnects
);
1063 nvme_rdma_reconnect_or_remove(ctrl
);
1066 static void nvme_rdma_error_recovery_work(struct work_struct
*work
)
1068 struct nvme_rdma_ctrl
*ctrl
= container_of(work
,
1069 struct nvme_rdma_ctrl
, err_work
);
1071 nvme_stop_keep_alive(&ctrl
->ctrl
);
1072 nvme_rdma_teardown_io_queues(ctrl
, false);
1073 nvme_start_queues(&ctrl
->ctrl
);
1074 nvme_rdma_teardown_admin_queue(ctrl
, false);
1075 blk_mq_unquiesce_queue(ctrl
->ctrl
.admin_q
);
1077 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_CONNECTING
)) {
1078 /* state change failure is ok if we're in DELETING state */
1079 WARN_ON_ONCE(ctrl
->ctrl
.state
!= NVME_CTRL_DELETING
);
1083 nvme_rdma_reconnect_or_remove(ctrl
);
1086 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl
*ctrl
)
1088 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_RESETTING
))
1091 queue_work(nvme_wq
, &ctrl
->err_work
);
1094 static void nvme_rdma_wr_error(struct ib_cq
*cq
, struct ib_wc
*wc
,
1097 struct nvme_rdma_queue
*queue
= cq
->cq_context
;
1098 struct nvme_rdma_ctrl
*ctrl
= queue
->ctrl
;
1100 if (ctrl
->ctrl
.state
== NVME_CTRL_LIVE
)
1101 dev_info(ctrl
->ctrl
.device
,
1102 "%s for CQE 0x%p failed with status %s (%d)\n",
1104 ib_wc_status_msg(wc
->status
), wc
->status
);
1105 nvme_rdma_error_recovery(ctrl
);
1108 static void nvme_rdma_memreg_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
1110 if (unlikely(wc
->status
!= IB_WC_SUCCESS
))
1111 nvme_rdma_wr_error(cq
, wc
, "MEMREG");
1114 static void nvme_rdma_inv_rkey_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
1116 struct nvme_rdma_request
*req
=
1117 container_of(wc
->wr_cqe
, struct nvme_rdma_request
, reg_cqe
);
1118 struct request
*rq
= blk_mq_rq_from_pdu(req
);
1120 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
1121 nvme_rdma_wr_error(cq
, wc
, "LOCAL_INV");
1125 if (refcount_dec_and_test(&req
->ref
))
1126 nvme_end_request(rq
, req
->status
, req
->result
);
1130 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue
*queue
,
1131 struct nvme_rdma_request
*req
)
1133 struct ib_send_wr wr
= {
1134 .opcode
= IB_WR_LOCAL_INV
,
1137 .send_flags
= IB_SEND_SIGNALED
,
1138 .ex
.invalidate_rkey
= req
->mr
->rkey
,
1141 req
->reg_cqe
.done
= nvme_rdma_inv_rkey_done
;
1142 wr
.wr_cqe
= &req
->reg_cqe
;
1144 return ib_post_send(queue
->qp
, &wr
, NULL
);
1147 static void nvme_rdma_unmap_data(struct nvme_rdma_queue
*queue
,
1150 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1151 struct nvme_rdma_device
*dev
= queue
->device
;
1152 struct ib_device
*ibdev
= dev
->dev
;
1154 if (!blk_rq_nr_phys_segments(rq
))
1158 ib_mr_pool_put(queue
->qp
, &queue
->qp
->rdma_mrs
, req
->mr
);
1162 ib_dma_unmap_sg(ibdev
, req
->sg_table
.sgl
, req
->nents
, rq_dma_dir(rq
));
1163 sg_free_table_chained(&req
->sg_table
, NVME_INLINE_SG_CNT
);
1166 static int nvme_rdma_set_sg_null(struct nvme_command
*c
)
1168 struct nvme_keyed_sgl_desc
*sg
= &c
->common
.dptr
.ksgl
;
1171 put_unaligned_le24(0, sg
->length
);
1172 put_unaligned_le32(0, sg
->key
);
1173 sg
->type
= NVME_KEY_SGL_FMT_DATA_DESC
<< 4;
1177 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue
*queue
,
1178 struct nvme_rdma_request
*req
, struct nvme_command
*c
,
1181 struct nvme_sgl_desc
*sg
= &c
->common
.dptr
.sgl
;
1182 struct scatterlist
*sgl
= req
->sg_table
.sgl
;
1183 struct ib_sge
*sge
= &req
->sge
[1];
1187 for (i
= 0; i
< count
; i
++, sgl
++, sge
++) {
1188 sge
->addr
= sg_dma_address(sgl
);
1189 sge
->length
= sg_dma_len(sgl
);
1190 sge
->lkey
= queue
->device
->pd
->local_dma_lkey
;
1194 sg
->addr
= cpu_to_le64(queue
->ctrl
->ctrl
.icdoff
);
1195 sg
->length
= cpu_to_le32(len
);
1196 sg
->type
= (NVME_SGL_FMT_DATA_DESC
<< 4) | NVME_SGL_FMT_OFFSET
;
1198 req
->num_sge
+= count
;
1202 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue
*queue
,
1203 struct nvme_rdma_request
*req
, struct nvme_command
*c
)
1205 struct nvme_keyed_sgl_desc
*sg
= &c
->common
.dptr
.ksgl
;
1207 sg
->addr
= cpu_to_le64(sg_dma_address(req
->sg_table
.sgl
));
1208 put_unaligned_le24(sg_dma_len(req
->sg_table
.sgl
), sg
->length
);
1209 put_unaligned_le32(queue
->device
->pd
->unsafe_global_rkey
, sg
->key
);
1210 sg
->type
= NVME_KEY_SGL_FMT_DATA_DESC
<< 4;
1214 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue
*queue
,
1215 struct nvme_rdma_request
*req
, struct nvme_command
*c
,
1218 struct nvme_keyed_sgl_desc
*sg
= &c
->common
.dptr
.ksgl
;
1221 req
->mr
= ib_mr_pool_get(queue
->qp
, &queue
->qp
->rdma_mrs
);
1222 if (WARN_ON_ONCE(!req
->mr
))
1226 * Align the MR to a 4K page size to match the ctrl page size and
1227 * the block virtual boundary.
1229 nr
= ib_map_mr_sg(req
->mr
, req
->sg_table
.sgl
, count
, NULL
, SZ_4K
);
1230 if (unlikely(nr
< count
)) {
1231 ib_mr_pool_put(queue
->qp
, &queue
->qp
->rdma_mrs
, req
->mr
);
1238 ib_update_fast_reg_key(req
->mr
, ib_inc_rkey(req
->mr
->rkey
));
1240 req
->reg_cqe
.done
= nvme_rdma_memreg_done
;
1241 memset(&req
->reg_wr
, 0, sizeof(req
->reg_wr
));
1242 req
->reg_wr
.wr
.opcode
= IB_WR_REG_MR
;
1243 req
->reg_wr
.wr
.wr_cqe
= &req
->reg_cqe
;
1244 req
->reg_wr
.wr
.num_sge
= 0;
1245 req
->reg_wr
.mr
= req
->mr
;
1246 req
->reg_wr
.key
= req
->mr
->rkey
;
1247 req
->reg_wr
.access
= IB_ACCESS_LOCAL_WRITE
|
1248 IB_ACCESS_REMOTE_READ
|
1249 IB_ACCESS_REMOTE_WRITE
;
1251 sg
->addr
= cpu_to_le64(req
->mr
->iova
);
1252 put_unaligned_le24(req
->mr
->length
, sg
->length
);
1253 put_unaligned_le32(req
->mr
->rkey
, sg
->key
);
1254 sg
->type
= (NVME_KEY_SGL_FMT_DATA_DESC
<< 4) |
1255 NVME_SGL_FMT_INVALIDATE
;
1260 static int nvme_rdma_map_data(struct nvme_rdma_queue
*queue
,
1261 struct request
*rq
, struct nvme_command
*c
)
1263 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1264 struct nvme_rdma_device
*dev
= queue
->device
;
1265 struct ib_device
*ibdev
= dev
->dev
;
1269 refcount_set(&req
->ref
, 2); /* send and recv completions */
1271 c
->common
.flags
|= NVME_CMD_SGL_METABUF
;
1273 if (!blk_rq_nr_phys_segments(rq
))
1274 return nvme_rdma_set_sg_null(c
);
1276 req
->sg_table
.sgl
= req
->first_sgl
;
1277 ret
= sg_alloc_table_chained(&req
->sg_table
,
1278 blk_rq_nr_phys_segments(rq
), req
->sg_table
.sgl
,
1279 NVME_INLINE_SG_CNT
);
1283 req
->nents
= blk_rq_map_sg(rq
->q
, rq
, req
->sg_table
.sgl
);
1285 count
= ib_dma_map_sg(ibdev
, req
->sg_table
.sgl
, req
->nents
,
1287 if (unlikely(count
<= 0)) {
1289 goto out_free_table
;
1292 if (count
<= dev
->num_inline_segments
) {
1293 if (rq_data_dir(rq
) == WRITE
&& nvme_rdma_queue_idx(queue
) &&
1294 queue
->ctrl
->use_inline_data
&&
1295 blk_rq_payload_bytes(rq
) <=
1296 nvme_rdma_inline_data_size(queue
)) {
1297 ret
= nvme_rdma_map_sg_inline(queue
, req
, c
, count
);
1301 if (count
== 1 && dev
->pd
->flags
& IB_PD_UNSAFE_GLOBAL_RKEY
) {
1302 ret
= nvme_rdma_map_sg_single(queue
, req
, c
);
1307 ret
= nvme_rdma_map_sg_fr(queue
, req
, c
, count
);
1315 ib_dma_unmap_sg(ibdev
, req
->sg_table
.sgl
, req
->nents
, rq_dma_dir(rq
));
1317 sg_free_table_chained(&req
->sg_table
, NVME_INLINE_SG_CNT
);
1321 static void nvme_rdma_send_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
1323 struct nvme_rdma_qe
*qe
=
1324 container_of(wc
->wr_cqe
, struct nvme_rdma_qe
, cqe
);
1325 struct nvme_rdma_request
*req
=
1326 container_of(qe
, struct nvme_rdma_request
, sqe
);
1327 struct request
*rq
= blk_mq_rq_from_pdu(req
);
1329 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
1330 nvme_rdma_wr_error(cq
, wc
, "SEND");
1334 if (refcount_dec_and_test(&req
->ref
))
1335 nvme_end_request(rq
, req
->status
, req
->result
);
1338 static int nvme_rdma_post_send(struct nvme_rdma_queue
*queue
,
1339 struct nvme_rdma_qe
*qe
, struct ib_sge
*sge
, u32 num_sge
,
1340 struct ib_send_wr
*first
)
1342 struct ib_send_wr wr
;
1345 sge
->addr
= qe
->dma
;
1346 sge
->length
= sizeof(struct nvme_command
),
1347 sge
->lkey
= queue
->device
->pd
->local_dma_lkey
;
1350 wr
.wr_cqe
= &qe
->cqe
;
1352 wr
.num_sge
= num_sge
;
1353 wr
.opcode
= IB_WR_SEND
;
1354 wr
.send_flags
= IB_SEND_SIGNALED
;
1361 ret
= ib_post_send(queue
->qp
, first
, NULL
);
1362 if (unlikely(ret
)) {
1363 dev_err(queue
->ctrl
->ctrl
.device
,
1364 "%s failed with error code %d\n", __func__
, ret
);
1369 static int nvme_rdma_post_recv(struct nvme_rdma_queue
*queue
,
1370 struct nvme_rdma_qe
*qe
)
1372 struct ib_recv_wr wr
;
1376 list
.addr
= qe
->dma
;
1377 list
.length
= sizeof(struct nvme_completion
);
1378 list
.lkey
= queue
->device
->pd
->local_dma_lkey
;
1380 qe
->cqe
.done
= nvme_rdma_recv_done
;
1383 wr
.wr_cqe
= &qe
->cqe
;
1387 ret
= ib_post_recv(queue
->qp
, &wr
, NULL
);
1388 if (unlikely(ret
)) {
1389 dev_err(queue
->ctrl
->ctrl
.device
,
1390 "%s failed with error code %d\n", __func__
, ret
);
1395 static struct blk_mq_tags
*nvme_rdma_tagset(struct nvme_rdma_queue
*queue
)
1397 u32 queue_idx
= nvme_rdma_queue_idx(queue
);
1400 return queue
->ctrl
->admin_tag_set
.tags
[queue_idx
];
1401 return queue
->ctrl
->tag_set
.tags
[queue_idx
- 1];
1404 static void nvme_rdma_async_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
1406 if (unlikely(wc
->status
!= IB_WC_SUCCESS
))
1407 nvme_rdma_wr_error(cq
, wc
, "ASYNC");
1410 static void nvme_rdma_submit_async_event(struct nvme_ctrl
*arg
)
1412 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(arg
);
1413 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[0];
1414 struct ib_device
*dev
= queue
->device
->dev
;
1415 struct nvme_rdma_qe
*sqe
= &ctrl
->async_event_sqe
;
1416 struct nvme_command
*cmd
= sqe
->data
;
1420 ib_dma_sync_single_for_cpu(dev
, sqe
->dma
, sizeof(*cmd
), DMA_TO_DEVICE
);
1422 memset(cmd
, 0, sizeof(*cmd
));
1423 cmd
->common
.opcode
= nvme_admin_async_event
;
1424 cmd
->common
.command_id
= NVME_AQ_BLK_MQ_DEPTH
;
1425 cmd
->common
.flags
|= NVME_CMD_SGL_METABUF
;
1426 nvme_rdma_set_sg_null(cmd
);
1428 sqe
->cqe
.done
= nvme_rdma_async_done
;
1430 ib_dma_sync_single_for_device(dev
, sqe
->dma
, sizeof(*cmd
),
1433 ret
= nvme_rdma_post_send(queue
, sqe
, &sge
, 1, NULL
);
1437 static void nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue
*queue
,
1438 struct nvme_completion
*cqe
, struct ib_wc
*wc
)
1441 struct nvme_rdma_request
*req
;
1443 rq
= blk_mq_tag_to_rq(nvme_rdma_tagset(queue
), cqe
->command_id
);
1445 dev_err(queue
->ctrl
->ctrl
.device
,
1446 "tag 0x%x on QP %#x not found\n",
1447 cqe
->command_id
, queue
->qp
->qp_num
);
1448 nvme_rdma_error_recovery(queue
->ctrl
);
1451 req
= blk_mq_rq_to_pdu(rq
);
1453 req
->status
= cqe
->status
;
1454 req
->result
= cqe
->result
;
1456 if (wc
->wc_flags
& IB_WC_WITH_INVALIDATE
) {
1457 if (unlikely(wc
->ex
.invalidate_rkey
!= req
->mr
->rkey
)) {
1458 dev_err(queue
->ctrl
->ctrl
.device
,
1459 "Bogus remote invalidation for rkey %#x\n",
1461 nvme_rdma_error_recovery(queue
->ctrl
);
1463 } else if (req
->mr
) {
1466 ret
= nvme_rdma_inv_rkey(queue
, req
);
1467 if (unlikely(ret
< 0)) {
1468 dev_err(queue
->ctrl
->ctrl
.device
,
1469 "Queueing INV WR for rkey %#x failed (%d)\n",
1470 req
->mr
->rkey
, ret
);
1471 nvme_rdma_error_recovery(queue
->ctrl
);
1473 /* the local invalidation completion will end the request */
1477 if (refcount_dec_and_test(&req
->ref
))
1478 nvme_end_request(rq
, req
->status
, req
->result
);
1481 static void nvme_rdma_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
1483 struct nvme_rdma_qe
*qe
=
1484 container_of(wc
->wr_cqe
, struct nvme_rdma_qe
, cqe
);
1485 struct nvme_rdma_queue
*queue
= cq
->cq_context
;
1486 struct ib_device
*ibdev
= queue
->device
->dev
;
1487 struct nvme_completion
*cqe
= qe
->data
;
1488 const size_t len
= sizeof(struct nvme_completion
);
1490 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
1491 nvme_rdma_wr_error(cq
, wc
, "RECV");
1495 ib_dma_sync_single_for_cpu(ibdev
, qe
->dma
, len
, DMA_FROM_DEVICE
);
1497 * AEN requests are special as they don't time out and can
1498 * survive any kind of queue freeze and often don't respond to
1499 * aborts. We don't even bother to allocate a struct request
1500 * for them but rather special case them here.
1502 if (unlikely(nvme_is_aen_req(nvme_rdma_queue_idx(queue
),
1504 nvme_complete_async_event(&queue
->ctrl
->ctrl
, cqe
->status
,
1507 nvme_rdma_process_nvme_rsp(queue
, cqe
, wc
);
1508 ib_dma_sync_single_for_device(ibdev
, qe
->dma
, len
, DMA_FROM_DEVICE
);
1510 nvme_rdma_post_recv(queue
, qe
);
1513 static int nvme_rdma_conn_established(struct nvme_rdma_queue
*queue
)
1517 for (i
= 0; i
< queue
->queue_size
; i
++) {
1518 ret
= nvme_rdma_post_recv(queue
, &queue
->rsp_ring
[i
]);
1520 goto out_destroy_queue_ib
;
1525 out_destroy_queue_ib
:
1526 nvme_rdma_destroy_queue_ib(queue
);
1530 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue
*queue
,
1531 struct rdma_cm_event
*ev
)
1533 struct rdma_cm_id
*cm_id
= queue
->cm_id
;
1534 int status
= ev
->status
;
1535 const char *rej_msg
;
1536 const struct nvme_rdma_cm_rej
*rej_data
;
1539 rej_msg
= rdma_reject_msg(cm_id
, status
);
1540 rej_data
= rdma_consumer_reject_data(cm_id
, ev
, &rej_data_len
);
1542 if (rej_data
&& rej_data_len
>= sizeof(u16
)) {
1543 u16 sts
= le16_to_cpu(rej_data
->sts
);
1545 dev_err(queue
->ctrl
->ctrl
.device
,
1546 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1547 status
, rej_msg
, sts
, nvme_rdma_cm_msg(sts
));
1549 dev_err(queue
->ctrl
->ctrl
.device
,
1550 "Connect rejected: status %d (%s).\n", status
, rej_msg
);
1556 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue
*queue
)
1558 struct nvme_ctrl
*ctrl
= &queue
->ctrl
->ctrl
;
1561 ret
= nvme_rdma_create_queue_ib(queue
);
1565 if (ctrl
->opts
->tos
>= 0)
1566 rdma_set_service_type(queue
->cm_id
, ctrl
->opts
->tos
);
1567 ret
= rdma_resolve_route(queue
->cm_id
, NVME_RDMA_CONNECT_TIMEOUT_MS
);
1569 dev_err(ctrl
->device
, "rdma_resolve_route failed (%d).\n",
1571 goto out_destroy_queue
;
1577 nvme_rdma_destroy_queue_ib(queue
);
1581 static int nvme_rdma_route_resolved(struct nvme_rdma_queue
*queue
)
1583 struct nvme_rdma_ctrl
*ctrl
= queue
->ctrl
;
1584 struct rdma_conn_param param
= { };
1585 struct nvme_rdma_cm_req priv
= { };
1588 param
.qp_num
= queue
->qp
->qp_num
;
1589 param
.flow_control
= 1;
1591 param
.responder_resources
= queue
->device
->dev
->attrs
.max_qp_rd_atom
;
1592 /* maximum retry count */
1593 param
.retry_count
= 7;
1594 param
.rnr_retry_count
= 7;
1595 param
.private_data
= &priv
;
1596 param
.private_data_len
= sizeof(priv
);
1598 priv
.recfmt
= cpu_to_le16(NVME_RDMA_CM_FMT_1_0
);
1599 priv
.qid
= cpu_to_le16(nvme_rdma_queue_idx(queue
));
1601 * set the admin queue depth to the minimum size
1602 * specified by the Fabrics standard.
1604 if (priv
.qid
== 0) {
1605 priv
.hrqsize
= cpu_to_le16(NVME_AQ_DEPTH
);
1606 priv
.hsqsize
= cpu_to_le16(NVME_AQ_DEPTH
- 1);
1609 * current interpretation of the fabrics spec
1610 * is at minimum you make hrqsize sqsize+1, or a
1611 * 1's based representation of sqsize.
1613 priv
.hrqsize
= cpu_to_le16(queue
->queue_size
);
1614 priv
.hsqsize
= cpu_to_le16(queue
->ctrl
->ctrl
.sqsize
);
1617 ret
= rdma_connect(queue
->cm_id
, ¶m
);
1619 dev_err(ctrl
->ctrl
.device
,
1620 "rdma_connect failed (%d).\n", ret
);
1621 goto out_destroy_queue_ib
;
1626 out_destroy_queue_ib
:
1627 nvme_rdma_destroy_queue_ib(queue
);
1631 static int nvme_rdma_cm_handler(struct rdma_cm_id
*cm_id
,
1632 struct rdma_cm_event
*ev
)
1634 struct nvme_rdma_queue
*queue
= cm_id
->context
;
1637 dev_dbg(queue
->ctrl
->ctrl
.device
, "%s (%d): status %d id %p\n",
1638 rdma_event_msg(ev
->event
), ev
->event
,
1641 switch (ev
->event
) {
1642 case RDMA_CM_EVENT_ADDR_RESOLVED
:
1643 cm_error
= nvme_rdma_addr_resolved(queue
);
1645 case RDMA_CM_EVENT_ROUTE_RESOLVED
:
1646 cm_error
= nvme_rdma_route_resolved(queue
);
1648 case RDMA_CM_EVENT_ESTABLISHED
:
1649 queue
->cm_error
= nvme_rdma_conn_established(queue
);
1650 /* complete cm_done regardless of success/failure */
1651 complete(&queue
->cm_done
);
1653 case RDMA_CM_EVENT_REJECTED
:
1654 nvme_rdma_destroy_queue_ib(queue
);
1655 cm_error
= nvme_rdma_conn_rejected(queue
, ev
);
1657 case RDMA_CM_EVENT_ROUTE_ERROR
:
1658 case RDMA_CM_EVENT_CONNECT_ERROR
:
1659 case RDMA_CM_EVENT_UNREACHABLE
:
1660 nvme_rdma_destroy_queue_ib(queue
);
1662 case RDMA_CM_EVENT_ADDR_ERROR
:
1663 dev_dbg(queue
->ctrl
->ctrl
.device
,
1664 "CM error event %d\n", ev
->event
);
1665 cm_error
= -ECONNRESET
;
1667 case RDMA_CM_EVENT_DISCONNECTED
:
1668 case RDMA_CM_EVENT_ADDR_CHANGE
:
1669 case RDMA_CM_EVENT_TIMEWAIT_EXIT
:
1670 dev_dbg(queue
->ctrl
->ctrl
.device
,
1671 "disconnect received - connection closed\n");
1672 nvme_rdma_error_recovery(queue
->ctrl
);
1674 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
1675 /* device removal is handled via the ib_client API */
1678 dev_err(queue
->ctrl
->ctrl
.device
,
1679 "Unexpected RDMA CM event (%d)\n", ev
->event
);
1680 nvme_rdma_error_recovery(queue
->ctrl
);
1685 queue
->cm_error
= cm_error
;
1686 complete(&queue
->cm_done
);
1692 static enum blk_eh_timer_return
1693 nvme_rdma_timeout(struct request
*rq
, bool reserved
)
1695 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1696 struct nvme_rdma_queue
*queue
= req
->queue
;
1697 struct nvme_rdma_ctrl
*ctrl
= queue
->ctrl
;
1699 dev_warn(ctrl
->ctrl
.device
, "I/O %d QID %d timeout\n",
1700 rq
->tag
, nvme_rdma_queue_idx(queue
));
1703 * Restart the timer if a controller reset is already scheduled. Any
1704 * timed out commands would be handled before entering the connecting
1707 if (ctrl
->ctrl
.state
== NVME_CTRL_RESETTING
)
1708 return BLK_EH_RESET_TIMER
;
1710 if (ctrl
->ctrl
.state
!= NVME_CTRL_LIVE
) {
1712 * Teardown immediately if controller times out while starting
1713 * or we are already started error recovery. all outstanding
1714 * requests are completed on shutdown, so we return BLK_EH_DONE.
1716 flush_work(&ctrl
->err_work
);
1717 nvme_rdma_teardown_io_queues(ctrl
, false);
1718 nvme_rdma_teardown_admin_queue(ctrl
, false);
1722 dev_warn(ctrl
->ctrl
.device
, "starting error recovery\n");
1723 nvme_rdma_error_recovery(ctrl
);
1725 return BLK_EH_RESET_TIMER
;
1728 static blk_status_t
nvme_rdma_queue_rq(struct blk_mq_hw_ctx
*hctx
,
1729 const struct blk_mq_queue_data
*bd
)
1731 struct nvme_ns
*ns
= hctx
->queue
->queuedata
;
1732 struct nvme_rdma_queue
*queue
= hctx
->driver_data
;
1733 struct request
*rq
= bd
->rq
;
1734 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1735 struct nvme_rdma_qe
*sqe
= &req
->sqe
;
1736 struct nvme_command
*c
= sqe
->data
;
1737 struct ib_device
*dev
;
1738 bool queue_ready
= test_bit(NVME_RDMA_Q_LIVE
, &queue
->flags
);
1742 WARN_ON_ONCE(rq
->tag
< 0);
1744 if (!nvmf_check_ready(&queue
->ctrl
->ctrl
, rq
, queue_ready
))
1745 return nvmf_fail_nonready_command(&queue
->ctrl
->ctrl
, rq
);
1747 dev
= queue
->device
->dev
;
1749 req
->sqe
.dma
= ib_dma_map_single(dev
, req
->sqe
.data
,
1750 sizeof(struct nvme_command
),
1752 err
= ib_dma_mapping_error(dev
, req
->sqe
.dma
);
1754 return BLK_STS_RESOURCE
;
1756 ib_dma_sync_single_for_cpu(dev
, sqe
->dma
,
1757 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
1759 ret
= nvme_setup_cmd(ns
, rq
, c
);
1763 blk_mq_start_request(rq
);
1765 err
= nvme_rdma_map_data(queue
, rq
, c
);
1766 if (unlikely(err
< 0)) {
1767 dev_err(queue
->ctrl
->ctrl
.device
,
1768 "Failed to map data (%d)\n", err
);
1772 sqe
->cqe
.done
= nvme_rdma_send_done
;
1774 ib_dma_sync_single_for_device(dev
, sqe
->dma
,
1775 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
1777 err
= nvme_rdma_post_send(queue
, sqe
, req
->sge
, req
->num_sge
,
1778 req
->mr
? &req
->reg_wr
.wr
: NULL
);
1785 nvme_rdma_unmap_data(queue
, rq
);
1787 if (err
== -ENOMEM
|| err
== -EAGAIN
)
1788 ret
= BLK_STS_RESOURCE
;
1790 ret
= BLK_STS_IOERR
;
1791 nvme_cleanup_cmd(rq
);
1793 ib_dma_unmap_single(dev
, req
->sqe
.dma
, sizeof(struct nvme_command
),
1798 static int nvme_rdma_poll(struct blk_mq_hw_ctx
*hctx
)
1800 struct nvme_rdma_queue
*queue
= hctx
->driver_data
;
1802 return ib_process_cq_direct(queue
->ib_cq
, -1);
1805 static void nvme_rdma_complete_rq(struct request
*rq
)
1807 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1808 struct nvme_rdma_queue
*queue
= req
->queue
;
1809 struct ib_device
*ibdev
= queue
->device
->dev
;
1811 nvme_rdma_unmap_data(queue
, rq
);
1812 ib_dma_unmap_single(ibdev
, req
->sqe
.dma
, sizeof(struct nvme_command
),
1814 nvme_complete_rq(rq
);
1817 static int nvme_rdma_map_queues(struct blk_mq_tag_set
*set
)
1819 struct nvme_rdma_ctrl
*ctrl
= set
->driver_data
;
1820 struct nvmf_ctrl_options
*opts
= ctrl
->ctrl
.opts
;
1822 if (opts
->nr_write_queues
&& ctrl
->io_queues
[HCTX_TYPE_READ
]) {
1823 /* separate read/write queues */
1824 set
->map
[HCTX_TYPE_DEFAULT
].nr_queues
=
1825 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
1826 set
->map
[HCTX_TYPE_DEFAULT
].queue_offset
= 0;
1827 set
->map
[HCTX_TYPE_READ
].nr_queues
=
1828 ctrl
->io_queues
[HCTX_TYPE_READ
];
1829 set
->map
[HCTX_TYPE_READ
].queue_offset
=
1830 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
1832 /* shared read/write queues */
1833 set
->map
[HCTX_TYPE_DEFAULT
].nr_queues
=
1834 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
1835 set
->map
[HCTX_TYPE_DEFAULT
].queue_offset
= 0;
1836 set
->map
[HCTX_TYPE_READ
].nr_queues
=
1837 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
1838 set
->map
[HCTX_TYPE_READ
].queue_offset
= 0;
1840 blk_mq_rdma_map_queues(&set
->map
[HCTX_TYPE_DEFAULT
],
1841 ctrl
->device
->dev
, 0);
1842 blk_mq_rdma_map_queues(&set
->map
[HCTX_TYPE_READ
],
1843 ctrl
->device
->dev
, 0);
1845 if (opts
->nr_poll_queues
&& ctrl
->io_queues
[HCTX_TYPE_POLL
]) {
1846 /* map dedicated poll queues only if we have queues left */
1847 set
->map
[HCTX_TYPE_POLL
].nr_queues
=
1848 ctrl
->io_queues
[HCTX_TYPE_POLL
];
1849 set
->map
[HCTX_TYPE_POLL
].queue_offset
=
1850 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] +
1851 ctrl
->io_queues
[HCTX_TYPE_READ
];
1852 blk_mq_map_queues(&set
->map
[HCTX_TYPE_POLL
]);
1855 dev_info(ctrl
->ctrl
.device
,
1856 "mapped %d/%d/%d default/read/poll queues.\n",
1857 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
],
1858 ctrl
->io_queues
[HCTX_TYPE_READ
],
1859 ctrl
->io_queues
[HCTX_TYPE_POLL
]);
1864 static const struct blk_mq_ops nvme_rdma_mq_ops
= {
1865 .queue_rq
= nvme_rdma_queue_rq
,
1866 .complete
= nvme_rdma_complete_rq
,
1867 .init_request
= nvme_rdma_init_request
,
1868 .exit_request
= nvme_rdma_exit_request
,
1869 .init_hctx
= nvme_rdma_init_hctx
,
1870 .timeout
= nvme_rdma_timeout
,
1871 .map_queues
= nvme_rdma_map_queues
,
1872 .poll
= nvme_rdma_poll
,
1875 static const struct blk_mq_ops nvme_rdma_admin_mq_ops
= {
1876 .queue_rq
= nvme_rdma_queue_rq
,
1877 .complete
= nvme_rdma_complete_rq
,
1878 .init_request
= nvme_rdma_init_request
,
1879 .exit_request
= nvme_rdma_exit_request
,
1880 .init_hctx
= nvme_rdma_init_admin_hctx
,
1881 .timeout
= nvme_rdma_timeout
,
1884 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl
*ctrl
, bool shutdown
)
1886 cancel_work_sync(&ctrl
->err_work
);
1887 cancel_delayed_work_sync(&ctrl
->reconnect_work
);
1889 nvme_rdma_teardown_io_queues(ctrl
, shutdown
);
1890 blk_mq_quiesce_queue(ctrl
->ctrl
.admin_q
);
1892 nvme_shutdown_ctrl(&ctrl
->ctrl
);
1894 nvme_disable_ctrl(&ctrl
->ctrl
);
1895 nvme_rdma_teardown_admin_queue(ctrl
, shutdown
);
1898 static void nvme_rdma_delete_ctrl(struct nvme_ctrl
*ctrl
)
1900 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl
), true);
1903 static void nvme_rdma_reset_ctrl_work(struct work_struct
*work
)
1905 struct nvme_rdma_ctrl
*ctrl
=
1906 container_of(work
, struct nvme_rdma_ctrl
, ctrl
.reset_work
);
1908 nvme_stop_ctrl(&ctrl
->ctrl
);
1909 nvme_rdma_shutdown_ctrl(ctrl
, false);
1911 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_CONNECTING
)) {
1912 /* state change failure should never happen */
1917 if (nvme_rdma_setup_ctrl(ctrl
, false))
1923 ++ctrl
->ctrl
.nr_reconnects
;
1924 nvme_rdma_reconnect_or_remove(ctrl
);
1927 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops
= {
1929 .module
= THIS_MODULE
,
1930 .flags
= NVME_F_FABRICS
,
1931 .reg_read32
= nvmf_reg_read32
,
1932 .reg_read64
= nvmf_reg_read64
,
1933 .reg_write32
= nvmf_reg_write32
,
1934 .free_ctrl
= nvme_rdma_free_ctrl
,
1935 .submit_async_event
= nvme_rdma_submit_async_event
,
1936 .delete_ctrl
= nvme_rdma_delete_ctrl
,
1937 .get_address
= nvmf_get_address
,
1941 * Fails a connection request if it matches an existing controller
1942 * (association) with the same tuple:
1943 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
1945 * if local address is not specified in the request, it will match an
1946 * existing controller with all the other parameters the same and no
1947 * local port address specified as well.
1949 * The ports don't need to be compared as they are intrinsically
1950 * already matched by the port pointers supplied.
1953 nvme_rdma_existing_controller(struct nvmf_ctrl_options
*opts
)
1955 struct nvme_rdma_ctrl
*ctrl
;
1958 mutex_lock(&nvme_rdma_ctrl_mutex
);
1959 list_for_each_entry(ctrl
, &nvme_rdma_ctrl_list
, list
) {
1960 found
= nvmf_ip_options_match(&ctrl
->ctrl
, opts
);
1964 mutex_unlock(&nvme_rdma_ctrl_mutex
);
1969 static struct nvme_ctrl
*nvme_rdma_create_ctrl(struct device
*dev
,
1970 struct nvmf_ctrl_options
*opts
)
1972 struct nvme_rdma_ctrl
*ctrl
;
1976 ctrl
= kzalloc(sizeof(*ctrl
), GFP_KERNEL
);
1978 return ERR_PTR(-ENOMEM
);
1979 ctrl
->ctrl
.opts
= opts
;
1980 INIT_LIST_HEAD(&ctrl
->list
);
1982 if (!(opts
->mask
& NVMF_OPT_TRSVCID
)) {
1984 kstrdup(__stringify(NVME_RDMA_IP_PORT
), GFP_KERNEL
);
1985 if (!opts
->trsvcid
) {
1989 opts
->mask
|= NVMF_OPT_TRSVCID
;
1992 ret
= inet_pton_with_scope(&init_net
, AF_UNSPEC
,
1993 opts
->traddr
, opts
->trsvcid
, &ctrl
->addr
);
1995 pr_err("malformed address passed: %s:%s\n",
1996 opts
->traddr
, opts
->trsvcid
);
2000 if (opts
->mask
& NVMF_OPT_HOST_TRADDR
) {
2001 ret
= inet_pton_with_scope(&init_net
, AF_UNSPEC
,
2002 opts
->host_traddr
, NULL
, &ctrl
->src_addr
);
2004 pr_err("malformed src address passed: %s\n",
2010 if (!opts
->duplicate_connect
&& nvme_rdma_existing_controller(opts
)) {
2015 INIT_DELAYED_WORK(&ctrl
->reconnect_work
,
2016 nvme_rdma_reconnect_ctrl_work
);
2017 INIT_WORK(&ctrl
->err_work
, nvme_rdma_error_recovery_work
);
2018 INIT_WORK(&ctrl
->ctrl
.reset_work
, nvme_rdma_reset_ctrl_work
);
2020 ctrl
->ctrl
.queue_count
= opts
->nr_io_queues
+ opts
->nr_write_queues
+
2021 opts
->nr_poll_queues
+ 1;
2022 ctrl
->ctrl
.sqsize
= opts
->queue_size
- 1;
2023 ctrl
->ctrl
.kato
= opts
->kato
;
2026 ctrl
->queues
= kcalloc(ctrl
->ctrl
.queue_count
, sizeof(*ctrl
->queues
),
2031 ret
= nvme_init_ctrl(&ctrl
->ctrl
, dev
, &nvme_rdma_ctrl_ops
,
2032 0 /* no quirks, we're perfect! */);
2034 goto out_kfree_queues
;
2036 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_CONNECTING
);
2037 WARN_ON_ONCE(!changed
);
2039 ret
= nvme_rdma_setup_ctrl(ctrl
, true);
2041 goto out_uninit_ctrl
;
2043 dev_info(ctrl
->ctrl
.device
, "new ctrl: NQN \"%s\", addr %pISpcs\n",
2044 ctrl
->ctrl
.opts
->subsysnqn
, &ctrl
->addr
);
2046 nvme_get_ctrl(&ctrl
->ctrl
);
2048 mutex_lock(&nvme_rdma_ctrl_mutex
);
2049 list_add_tail(&ctrl
->list
, &nvme_rdma_ctrl_list
);
2050 mutex_unlock(&nvme_rdma_ctrl_mutex
);
2055 nvme_uninit_ctrl(&ctrl
->ctrl
);
2056 nvme_put_ctrl(&ctrl
->ctrl
);
2059 return ERR_PTR(ret
);
2061 kfree(ctrl
->queues
);
2064 return ERR_PTR(ret
);
2067 static struct nvmf_transport_ops nvme_rdma_transport
= {
2069 .module
= THIS_MODULE
,
2070 .required_opts
= NVMF_OPT_TRADDR
,
2071 .allowed_opts
= NVMF_OPT_TRSVCID
| NVMF_OPT_RECONNECT_DELAY
|
2072 NVMF_OPT_HOST_TRADDR
| NVMF_OPT_CTRL_LOSS_TMO
|
2073 NVMF_OPT_NR_WRITE_QUEUES
| NVMF_OPT_NR_POLL_QUEUES
|
2075 .create_ctrl
= nvme_rdma_create_ctrl
,
2078 static void nvme_rdma_remove_one(struct ib_device
*ib_device
, void *client_data
)
2080 struct nvme_rdma_ctrl
*ctrl
;
2081 struct nvme_rdma_device
*ndev
;
2084 mutex_lock(&device_list_mutex
);
2085 list_for_each_entry(ndev
, &device_list
, entry
) {
2086 if (ndev
->dev
== ib_device
) {
2091 mutex_unlock(&device_list_mutex
);
2096 /* Delete all controllers using this device */
2097 mutex_lock(&nvme_rdma_ctrl_mutex
);
2098 list_for_each_entry(ctrl
, &nvme_rdma_ctrl_list
, list
) {
2099 if (ctrl
->device
->dev
!= ib_device
)
2101 nvme_delete_ctrl(&ctrl
->ctrl
);
2103 mutex_unlock(&nvme_rdma_ctrl_mutex
);
2105 flush_workqueue(nvme_delete_wq
);
2108 static struct ib_client nvme_rdma_ib_client
= {
2109 .name
= "nvme_rdma",
2110 .remove
= nvme_rdma_remove_one
2113 static int __init
nvme_rdma_init_module(void)
2117 ret
= ib_register_client(&nvme_rdma_ib_client
);
2121 ret
= nvmf_register_transport(&nvme_rdma_transport
);
2123 goto err_unreg_client
;
2128 ib_unregister_client(&nvme_rdma_ib_client
);
2132 static void __exit
nvme_rdma_cleanup_module(void)
2134 struct nvme_rdma_ctrl
*ctrl
;
2136 nvmf_unregister_transport(&nvme_rdma_transport
);
2137 ib_unregister_client(&nvme_rdma_ib_client
);
2139 mutex_lock(&nvme_rdma_ctrl_mutex
);
2140 list_for_each_entry(ctrl
, &nvme_rdma_ctrl_list
, list
)
2141 nvme_delete_ctrl(&ctrl
->ctrl
);
2142 mutex_unlock(&nvme_rdma_ctrl_mutex
);
2143 flush_workqueue(nvme_delete_wq
);
2146 module_init(nvme_rdma_init_module
);
2147 module_exit(nvme_rdma_cleanup_module
);
2149 MODULE_LICENSE("GPL v2");