2 * NVMe over Fabrics RDMA host code.
3 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <rdma/mr_pool.h>
19 #include <linux/err.h>
20 #include <linux/string.h>
21 #include <linux/atomic.h>
22 #include <linux/blk-mq.h>
23 #include <linux/blk-mq-rdma.h>
24 #include <linux/types.h>
25 #include <linux/list.h>
26 #include <linux/mutex.h>
27 #include <linux/scatterlist.h>
28 #include <linux/nvme.h>
29 #include <asm/unaligned.h>
31 #include <rdma/ib_verbs.h>
32 #include <rdma/rdma_cm.h>
33 #include <linux/nvme-rdma.h>
39 #define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
41 #define NVME_RDMA_MAX_SEGMENTS 256
43 #define NVME_RDMA_MAX_INLINE_SEGMENTS 1
45 struct nvme_rdma_device
{
46 struct ib_device
*dev
;
49 struct list_head entry
;
58 struct nvme_rdma_queue
;
59 struct nvme_rdma_request
{
60 struct nvme_request req
;
62 struct nvme_rdma_qe sqe
;
63 union nvme_result result
;
66 struct ib_sge sge
[1 + NVME_RDMA_MAX_INLINE_SEGMENTS
];
69 struct ib_reg_wr reg_wr
;
70 struct ib_cqe reg_cqe
;
71 struct nvme_rdma_queue
*queue
;
72 struct sg_table sg_table
;
73 struct scatterlist first_sgl
[];
76 enum nvme_rdma_queue_flags
{
77 NVME_RDMA_Q_ALLOCATED
= 0,
79 NVME_RDMA_Q_TR_READY
= 2,
82 struct nvme_rdma_queue
{
83 struct nvme_rdma_qe
*rsp_ring
;
85 size_t cmnd_capsule_len
;
86 struct nvme_rdma_ctrl
*ctrl
;
87 struct nvme_rdma_device
*device
;
92 struct rdma_cm_id
*cm_id
;
94 struct completion cm_done
;
97 struct nvme_rdma_ctrl
{
98 /* read only in the hot path */
99 struct nvme_rdma_queue
*queues
;
101 /* other member variables */
102 struct blk_mq_tag_set tag_set
;
103 struct work_struct err_work
;
105 struct nvme_rdma_qe async_event_sqe
;
107 struct delayed_work reconnect_work
;
109 struct list_head list
;
111 struct blk_mq_tag_set admin_tag_set
;
112 struct nvme_rdma_device
*device
;
116 struct sockaddr_storage addr
;
117 struct sockaddr_storage src_addr
;
119 struct nvme_ctrl ctrl
;
122 static inline struct nvme_rdma_ctrl
*to_rdma_ctrl(struct nvme_ctrl
*ctrl
)
124 return container_of(ctrl
, struct nvme_rdma_ctrl
, ctrl
);
127 static LIST_HEAD(device_list
);
128 static DEFINE_MUTEX(device_list_mutex
);
130 static LIST_HEAD(nvme_rdma_ctrl_list
);
131 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex
);
134 * Disabling this option makes small I/O goes faster, but is fundamentally
135 * unsafe. With it turned off we will have to register a global rkey that
136 * allows read and write access to all physical memory.
138 static bool register_always
= true;
139 module_param(register_always
, bool, 0444);
140 MODULE_PARM_DESC(register_always
,
141 "Use memory registration even for contiguous memory regions");
143 static int nvme_rdma_cm_handler(struct rdma_cm_id
*cm_id
,
144 struct rdma_cm_event
*event
);
145 static void nvme_rdma_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
);
147 static const struct blk_mq_ops nvme_rdma_mq_ops
;
148 static const struct blk_mq_ops nvme_rdma_admin_mq_ops
;
150 /* XXX: really should move to a generic header sooner or later.. */
151 static inline void put_unaligned_le24(u32 val
, u8
*p
)
158 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue
*queue
)
160 return queue
- queue
->ctrl
->queues
;
163 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue
*queue
)
165 return queue
->cmnd_capsule_len
- sizeof(struct nvme_command
);
168 static void nvme_rdma_free_qe(struct ib_device
*ibdev
, struct nvme_rdma_qe
*qe
,
169 size_t capsule_size
, enum dma_data_direction dir
)
171 ib_dma_unmap_single(ibdev
, qe
->dma
, capsule_size
, dir
);
175 static int nvme_rdma_alloc_qe(struct ib_device
*ibdev
, struct nvme_rdma_qe
*qe
,
176 size_t capsule_size
, enum dma_data_direction dir
)
178 qe
->data
= kzalloc(capsule_size
, GFP_KERNEL
);
182 qe
->dma
= ib_dma_map_single(ibdev
, qe
->data
, capsule_size
, dir
);
183 if (ib_dma_mapping_error(ibdev
, qe
->dma
)) {
191 static void nvme_rdma_free_ring(struct ib_device
*ibdev
,
192 struct nvme_rdma_qe
*ring
, size_t ib_queue_size
,
193 size_t capsule_size
, enum dma_data_direction dir
)
197 for (i
= 0; i
< ib_queue_size
; i
++)
198 nvme_rdma_free_qe(ibdev
, &ring
[i
], capsule_size
, dir
);
202 static struct nvme_rdma_qe
*nvme_rdma_alloc_ring(struct ib_device
*ibdev
,
203 size_t ib_queue_size
, size_t capsule_size
,
204 enum dma_data_direction dir
)
206 struct nvme_rdma_qe
*ring
;
209 ring
= kcalloc(ib_queue_size
, sizeof(struct nvme_rdma_qe
), GFP_KERNEL
);
213 for (i
= 0; i
< ib_queue_size
; i
++) {
214 if (nvme_rdma_alloc_qe(ibdev
, &ring
[i
], capsule_size
, dir
))
221 nvme_rdma_free_ring(ibdev
, ring
, i
, capsule_size
, dir
);
225 static void nvme_rdma_qp_event(struct ib_event
*event
, void *context
)
227 pr_debug("QP event %s (%d)\n",
228 ib_event_msg(event
->event
), event
->event
);
232 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue
*queue
)
234 wait_for_completion_interruptible_timeout(&queue
->cm_done
,
235 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS
) + 1);
236 return queue
->cm_error
;
239 static int nvme_rdma_create_qp(struct nvme_rdma_queue
*queue
, const int factor
)
241 struct nvme_rdma_device
*dev
= queue
->device
;
242 struct ib_qp_init_attr init_attr
;
245 memset(&init_attr
, 0, sizeof(init_attr
));
246 init_attr
.event_handler
= nvme_rdma_qp_event
;
248 init_attr
.cap
.max_send_wr
= factor
* queue
->queue_size
+ 1;
250 init_attr
.cap
.max_recv_wr
= queue
->queue_size
+ 1;
251 init_attr
.cap
.max_recv_sge
= 1;
252 init_attr
.cap
.max_send_sge
= 1 + NVME_RDMA_MAX_INLINE_SEGMENTS
;
253 init_attr
.sq_sig_type
= IB_SIGNAL_REQ_WR
;
254 init_attr
.qp_type
= IB_QPT_RC
;
255 init_attr
.send_cq
= queue
->ib_cq
;
256 init_attr
.recv_cq
= queue
->ib_cq
;
258 ret
= rdma_create_qp(queue
->cm_id
, dev
->pd
, &init_attr
);
260 queue
->qp
= queue
->cm_id
->qp
;
264 static void nvme_rdma_exit_request(struct blk_mq_tag_set
*set
,
265 struct request
*rq
, unsigned int hctx_idx
)
267 struct nvme_rdma_ctrl
*ctrl
= set
->driver_data
;
268 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
269 int queue_idx
= (set
== &ctrl
->tag_set
) ? hctx_idx
+ 1 : 0;
270 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[queue_idx
];
271 struct nvme_rdma_device
*dev
= queue
->device
;
273 nvme_rdma_free_qe(dev
->dev
, &req
->sqe
, sizeof(struct nvme_command
),
277 static int nvme_rdma_init_request(struct blk_mq_tag_set
*set
,
278 struct request
*rq
, unsigned int hctx_idx
,
279 unsigned int numa_node
)
281 struct nvme_rdma_ctrl
*ctrl
= set
->driver_data
;
282 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
283 int queue_idx
= (set
== &ctrl
->tag_set
) ? hctx_idx
+ 1 : 0;
284 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[queue_idx
];
285 struct nvme_rdma_device
*dev
= queue
->device
;
286 struct ib_device
*ibdev
= dev
->dev
;
289 ret
= nvme_rdma_alloc_qe(ibdev
, &req
->sqe
, sizeof(struct nvme_command
),
299 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
300 unsigned int hctx_idx
)
302 struct nvme_rdma_ctrl
*ctrl
= data
;
303 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[hctx_idx
+ 1];
305 BUG_ON(hctx_idx
>= ctrl
->ctrl
.queue_count
);
307 hctx
->driver_data
= queue
;
311 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
312 unsigned int hctx_idx
)
314 struct nvme_rdma_ctrl
*ctrl
= data
;
315 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[0];
317 BUG_ON(hctx_idx
!= 0);
319 hctx
->driver_data
= queue
;
323 static void nvme_rdma_free_dev(struct kref
*ref
)
325 struct nvme_rdma_device
*ndev
=
326 container_of(ref
, struct nvme_rdma_device
, ref
);
328 mutex_lock(&device_list_mutex
);
329 list_del(&ndev
->entry
);
330 mutex_unlock(&device_list_mutex
);
332 ib_dealloc_pd(ndev
->pd
);
336 static void nvme_rdma_dev_put(struct nvme_rdma_device
*dev
)
338 kref_put(&dev
->ref
, nvme_rdma_free_dev
);
341 static int nvme_rdma_dev_get(struct nvme_rdma_device
*dev
)
343 return kref_get_unless_zero(&dev
->ref
);
346 static struct nvme_rdma_device
*
347 nvme_rdma_find_get_device(struct rdma_cm_id
*cm_id
)
349 struct nvme_rdma_device
*ndev
;
351 mutex_lock(&device_list_mutex
);
352 list_for_each_entry(ndev
, &device_list
, entry
) {
353 if (ndev
->dev
->node_guid
== cm_id
->device
->node_guid
&&
354 nvme_rdma_dev_get(ndev
))
358 ndev
= kzalloc(sizeof(*ndev
), GFP_KERNEL
);
362 ndev
->dev
= cm_id
->device
;
363 kref_init(&ndev
->ref
);
365 ndev
->pd
= ib_alloc_pd(ndev
->dev
,
366 register_always
? 0 : IB_PD_UNSAFE_GLOBAL_RKEY
);
367 if (IS_ERR(ndev
->pd
))
370 if (!(ndev
->dev
->attrs
.device_cap_flags
&
371 IB_DEVICE_MEM_MGT_EXTENSIONS
)) {
372 dev_err(&ndev
->dev
->dev
,
373 "Memory registrations not supported.\n");
377 list_add(&ndev
->entry
, &device_list
);
379 mutex_unlock(&device_list_mutex
);
383 ib_dealloc_pd(ndev
->pd
);
387 mutex_unlock(&device_list_mutex
);
391 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue
*queue
)
393 struct nvme_rdma_device
*dev
;
394 struct ib_device
*ibdev
;
396 if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY
, &queue
->flags
))
402 ib_mr_pool_destroy(queue
->qp
, &queue
->qp
->rdma_mrs
);
405 * The cm_id object might have been destroyed during RDMA connection
406 * establishment error flow to avoid getting other cma events, thus
407 * the destruction of the QP shouldn't use rdma_cm API.
409 ib_destroy_qp(queue
->qp
);
410 ib_free_cq(queue
->ib_cq
);
412 nvme_rdma_free_ring(ibdev
, queue
->rsp_ring
, queue
->queue_size
,
413 sizeof(struct nvme_completion
), DMA_FROM_DEVICE
);
415 nvme_rdma_dev_put(dev
);
418 static int nvme_rdma_get_max_fr_pages(struct ib_device
*ibdev
)
420 return min_t(u32
, NVME_RDMA_MAX_SEGMENTS
,
421 ibdev
->attrs
.max_fast_reg_page_list_len
);
424 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue
*queue
)
426 struct ib_device
*ibdev
;
427 const int send_wr_factor
= 3; /* MR, SEND, INV */
428 const int cq_factor
= send_wr_factor
+ 1; /* + RECV */
429 int comp_vector
, idx
= nvme_rdma_queue_idx(queue
);
432 queue
->device
= nvme_rdma_find_get_device(queue
->cm_id
);
433 if (!queue
->device
) {
434 dev_err(queue
->cm_id
->device
->dev
.parent
,
435 "no client data found!\n");
436 return -ECONNREFUSED
;
438 ibdev
= queue
->device
->dev
;
441 * Spread I/O queues completion vectors according their queue index.
442 * Admin queues can always go on completion vector 0.
444 comp_vector
= idx
== 0 ? idx
: idx
- 1;
446 /* +1 for ib_stop_cq */
447 queue
->ib_cq
= ib_alloc_cq(ibdev
, queue
,
448 cq_factor
* queue
->queue_size
+ 1,
449 comp_vector
, IB_POLL_SOFTIRQ
);
450 if (IS_ERR(queue
->ib_cq
)) {
451 ret
= PTR_ERR(queue
->ib_cq
);
455 ret
= nvme_rdma_create_qp(queue
, send_wr_factor
);
457 goto out_destroy_ib_cq
;
459 queue
->rsp_ring
= nvme_rdma_alloc_ring(ibdev
, queue
->queue_size
,
460 sizeof(struct nvme_completion
), DMA_FROM_DEVICE
);
461 if (!queue
->rsp_ring
) {
466 ret
= ib_mr_pool_init(queue
->qp
, &queue
->qp
->rdma_mrs
,
469 nvme_rdma_get_max_fr_pages(ibdev
));
471 dev_err(queue
->ctrl
->ctrl
.device
,
472 "failed to initialize MR pool sized %d for QID %d\n",
473 queue
->queue_size
, idx
);
474 goto out_destroy_ring
;
477 set_bit(NVME_RDMA_Q_TR_READY
, &queue
->flags
);
482 nvme_rdma_free_ring(ibdev
, queue
->rsp_ring
, queue
->queue_size
,
483 sizeof(struct nvme_completion
), DMA_FROM_DEVICE
);
485 rdma_destroy_qp(queue
->cm_id
);
487 ib_free_cq(queue
->ib_cq
);
489 nvme_rdma_dev_put(queue
->device
);
493 static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl
*ctrl
,
494 int idx
, size_t queue_size
)
496 struct nvme_rdma_queue
*queue
;
497 struct sockaddr
*src_addr
= NULL
;
500 queue
= &ctrl
->queues
[idx
];
502 init_completion(&queue
->cm_done
);
505 queue
->cmnd_capsule_len
= ctrl
->ctrl
.ioccsz
* 16;
507 queue
->cmnd_capsule_len
= sizeof(struct nvme_command
);
509 queue
->queue_size
= queue_size
;
511 queue
->cm_id
= rdma_create_id(&init_net
, nvme_rdma_cm_handler
, queue
,
512 RDMA_PS_TCP
, IB_QPT_RC
);
513 if (IS_ERR(queue
->cm_id
)) {
514 dev_info(ctrl
->ctrl
.device
,
515 "failed to create CM ID: %ld\n", PTR_ERR(queue
->cm_id
));
516 return PTR_ERR(queue
->cm_id
);
519 if (ctrl
->ctrl
.opts
->mask
& NVMF_OPT_HOST_TRADDR
)
520 src_addr
= (struct sockaddr
*)&ctrl
->src_addr
;
522 queue
->cm_error
= -ETIMEDOUT
;
523 ret
= rdma_resolve_addr(queue
->cm_id
, src_addr
,
524 (struct sockaddr
*)&ctrl
->addr
,
525 NVME_RDMA_CONNECT_TIMEOUT_MS
);
527 dev_info(ctrl
->ctrl
.device
,
528 "rdma_resolve_addr failed (%d).\n", ret
);
529 goto out_destroy_cm_id
;
532 ret
= nvme_rdma_wait_for_cm(queue
);
534 dev_info(ctrl
->ctrl
.device
,
535 "rdma connection establishment failed (%d)\n", ret
);
536 goto out_destroy_cm_id
;
539 set_bit(NVME_RDMA_Q_ALLOCATED
, &queue
->flags
);
544 rdma_destroy_id(queue
->cm_id
);
545 nvme_rdma_destroy_queue_ib(queue
);
549 static void nvme_rdma_stop_queue(struct nvme_rdma_queue
*queue
)
551 if (!test_and_clear_bit(NVME_RDMA_Q_LIVE
, &queue
->flags
))
554 rdma_disconnect(queue
->cm_id
);
555 ib_drain_qp(queue
->qp
);
558 static void nvme_rdma_free_queue(struct nvme_rdma_queue
*queue
)
560 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED
, &queue
->flags
))
563 nvme_rdma_destroy_queue_ib(queue
);
564 rdma_destroy_id(queue
->cm_id
);
567 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl
*ctrl
)
571 for (i
= 1; i
< ctrl
->ctrl
.queue_count
; i
++)
572 nvme_rdma_free_queue(&ctrl
->queues
[i
]);
575 static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl
*ctrl
)
579 for (i
= 1; i
< ctrl
->ctrl
.queue_count
; i
++)
580 nvme_rdma_stop_queue(&ctrl
->queues
[i
]);
583 static int nvme_rdma_start_queue(struct nvme_rdma_ctrl
*ctrl
, int idx
)
588 ret
= nvmf_connect_io_queue(&ctrl
->ctrl
, idx
);
590 ret
= nvmf_connect_admin_queue(&ctrl
->ctrl
);
593 set_bit(NVME_RDMA_Q_LIVE
, &ctrl
->queues
[idx
].flags
);
595 dev_info(ctrl
->ctrl
.device
,
596 "failed to connect queue: %d ret=%d\n", idx
, ret
);
600 static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl
*ctrl
)
604 for (i
= 1; i
< ctrl
->ctrl
.queue_count
; i
++) {
605 ret
= nvme_rdma_start_queue(ctrl
, i
);
607 goto out_stop_queues
;
613 for (i
--; i
>= 1; i
--)
614 nvme_rdma_stop_queue(&ctrl
->queues
[i
]);
618 static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl
*ctrl
)
620 struct nvmf_ctrl_options
*opts
= ctrl
->ctrl
.opts
;
621 struct ib_device
*ibdev
= ctrl
->device
->dev
;
622 unsigned int nr_io_queues
;
625 nr_io_queues
= min(opts
->nr_io_queues
, num_online_cpus());
628 * we map queues according to the device irq vectors for
629 * optimal locality so we don't need more queues than
630 * completion vectors.
632 nr_io_queues
= min_t(unsigned int, nr_io_queues
,
633 ibdev
->num_comp_vectors
);
635 ret
= nvme_set_queue_count(&ctrl
->ctrl
, &nr_io_queues
);
639 ctrl
->ctrl
.queue_count
= nr_io_queues
+ 1;
640 if (ctrl
->ctrl
.queue_count
< 2)
643 dev_info(ctrl
->ctrl
.device
,
644 "creating %d I/O queues.\n", nr_io_queues
);
646 for (i
= 1; i
< ctrl
->ctrl
.queue_count
; i
++) {
647 ret
= nvme_rdma_alloc_queue(ctrl
, i
,
648 ctrl
->ctrl
.sqsize
+ 1);
650 goto out_free_queues
;
656 for (i
--; i
>= 1; i
--)
657 nvme_rdma_free_queue(&ctrl
->queues
[i
]);
662 static void nvme_rdma_free_tagset(struct nvme_ctrl
*nctrl
,
663 struct blk_mq_tag_set
*set
)
665 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(nctrl
);
667 blk_mq_free_tag_set(set
);
668 nvme_rdma_dev_put(ctrl
->device
);
671 static struct blk_mq_tag_set
*nvme_rdma_alloc_tagset(struct nvme_ctrl
*nctrl
,
674 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(nctrl
);
675 struct blk_mq_tag_set
*set
;
679 set
= &ctrl
->admin_tag_set
;
680 memset(set
, 0, sizeof(*set
));
681 set
->ops
= &nvme_rdma_admin_mq_ops
;
682 set
->queue_depth
= NVME_AQ_MQ_TAG_DEPTH
;
683 set
->reserved_tags
= 2; /* connect + keep-alive */
684 set
->numa_node
= NUMA_NO_NODE
;
685 set
->cmd_size
= sizeof(struct nvme_rdma_request
) +
686 SG_CHUNK_SIZE
* sizeof(struct scatterlist
);
687 set
->driver_data
= ctrl
;
688 set
->nr_hw_queues
= 1;
689 set
->timeout
= ADMIN_TIMEOUT
;
690 set
->flags
= BLK_MQ_F_NO_SCHED
;
692 set
= &ctrl
->tag_set
;
693 memset(set
, 0, sizeof(*set
));
694 set
->ops
= &nvme_rdma_mq_ops
;
695 set
->queue_depth
= nctrl
->sqsize
+ 1;
696 set
->reserved_tags
= 1; /* fabric connect */
697 set
->numa_node
= NUMA_NO_NODE
;
698 set
->flags
= BLK_MQ_F_SHOULD_MERGE
;
699 set
->cmd_size
= sizeof(struct nvme_rdma_request
) +
700 SG_CHUNK_SIZE
* sizeof(struct scatterlist
);
701 set
->driver_data
= ctrl
;
702 set
->nr_hw_queues
= nctrl
->queue_count
- 1;
703 set
->timeout
= NVME_IO_TIMEOUT
;
706 ret
= blk_mq_alloc_tag_set(set
);
711 * We need a reference on the device as long as the tag_set is alive,
712 * as the MRs in the request structures need a valid ib_device.
714 ret
= nvme_rdma_dev_get(ctrl
->device
);
717 goto out_free_tagset
;
723 blk_mq_free_tag_set(set
);
728 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl
*ctrl
,
732 blk_cleanup_queue(ctrl
->ctrl
.admin_q
);
733 nvme_rdma_free_tagset(&ctrl
->ctrl
, ctrl
->ctrl
.admin_tagset
);
735 if (ctrl
->async_event_sqe
.data
) {
736 nvme_rdma_free_qe(ctrl
->device
->dev
, &ctrl
->async_event_sqe
,
737 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
738 ctrl
->async_event_sqe
.data
= NULL
;
740 nvme_rdma_free_queue(&ctrl
->queues
[0]);
743 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl
*ctrl
,
748 error
= nvme_rdma_alloc_queue(ctrl
, 0, NVME_AQ_DEPTH
);
752 ctrl
->device
= ctrl
->queues
[0].device
;
754 ctrl
->max_fr_pages
= nvme_rdma_get_max_fr_pages(ctrl
->device
->dev
);
756 error
= nvme_rdma_alloc_qe(ctrl
->device
->dev
, &ctrl
->async_event_sqe
,
757 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
762 ctrl
->ctrl
.admin_tagset
= nvme_rdma_alloc_tagset(&ctrl
->ctrl
, true);
763 if (IS_ERR(ctrl
->ctrl
.admin_tagset
)) {
764 error
= PTR_ERR(ctrl
->ctrl
.admin_tagset
);
765 goto out_free_async_qe
;
768 ctrl
->ctrl
.admin_q
= blk_mq_init_queue(&ctrl
->admin_tag_set
);
769 if (IS_ERR(ctrl
->ctrl
.admin_q
)) {
770 error
= PTR_ERR(ctrl
->ctrl
.admin_q
);
771 goto out_free_tagset
;
775 error
= nvme_rdma_start_queue(ctrl
, 0);
777 goto out_cleanup_queue
;
779 error
= ctrl
->ctrl
.ops
->reg_read64(&ctrl
->ctrl
, NVME_REG_CAP
,
782 dev_err(ctrl
->ctrl
.device
,
783 "prop_get NVME_REG_CAP failed\n");
788 min_t(int, NVME_CAP_MQES(ctrl
->ctrl
.cap
), ctrl
->ctrl
.sqsize
);
790 error
= nvme_enable_ctrl(&ctrl
->ctrl
, ctrl
->ctrl
.cap
);
794 ctrl
->ctrl
.max_hw_sectors
=
795 (ctrl
->max_fr_pages
- 1) << (ilog2(SZ_4K
) - 9);
797 error
= nvme_init_identify(&ctrl
->ctrl
);
804 nvme_rdma_stop_queue(&ctrl
->queues
[0]);
807 blk_cleanup_queue(ctrl
->ctrl
.admin_q
);
810 nvme_rdma_free_tagset(&ctrl
->ctrl
, ctrl
->ctrl
.admin_tagset
);
812 nvme_rdma_free_qe(ctrl
->device
->dev
, &ctrl
->async_event_sqe
,
813 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
815 nvme_rdma_free_queue(&ctrl
->queues
[0]);
819 static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl
*ctrl
,
823 blk_cleanup_queue(ctrl
->ctrl
.connect_q
);
824 nvme_rdma_free_tagset(&ctrl
->ctrl
, ctrl
->ctrl
.tagset
);
826 nvme_rdma_free_io_queues(ctrl
);
829 static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl
*ctrl
, bool new)
833 ret
= nvme_rdma_alloc_io_queues(ctrl
);
838 ctrl
->ctrl
.tagset
= nvme_rdma_alloc_tagset(&ctrl
->ctrl
, false);
839 if (IS_ERR(ctrl
->ctrl
.tagset
)) {
840 ret
= PTR_ERR(ctrl
->ctrl
.tagset
);
841 goto out_free_io_queues
;
844 ctrl
->ctrl
.connect_q
= blk_mq_init_queue(&ctrl
->tag_set
);
845 if (IS_ERR(ctrl
->ctrl
.connect_q
)) {
846 ret
= PTR_ERR(ctrl
->ctrl
.connect_q
);
847 goto out_free_tag_set
;
850 blk_mq_update_nr_hw_queues(&ctrl
->tag_set
,
851 ctrl
->ctrl
.queue_count
- 1);
854 ret
= nvme_rdma_start_io_queues(ctrl
);
856 goto out_cleanup_connect_q
;
860 out_cleanup_connect_q
:
862 blk_cleanup_queue(ctrl
->ctrl
.connect_q
);
865 nvme_rdma_free_tagset(&ctrl
->ctrl
, ctrl
->ctrl
.tagset
);
867 nvme_rdma_free_io_queues(ctrl
);
871 static void nvme_rdma_stop_ctrl(struct nvme_ctrl
*nctrl
)
873 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(nctrl
);
875 cancel_work_sync(&ctrl
->err_work
);
876 cancel_delayed_work_sync(&ctrl
->reconnect_work
);
879 static void nvme_rdma_free_ctrl(struct nvme_ctrl
*nctrl
)
881 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(nctrl
);
883 if (list_empty(&ctrl
->list
))
886 mutex_lock(&nvme_rdma_ctrl_mutex
);
887 list_del(&ctrl
->list
);
888 mutex_unlock(&nvme_rdma_ctrl_mutex
);
890 nvmf_free_options(nctrl
->opts
);
896 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl
*ctrl
)
898 /* If we are resetting/deleting then do nothing */
899 if (ctrl
->ctrl
.state
!= NVME_CTRL_CONNECTING
) {
900 WARN_ON_ONCE(ctrl
->ctrl
.state
== NVME_CTRL_NEW
||
901 ctrl
->ctrl
.state
== NVME_CTRL_LIVE
);
905 if (nvmf_should_reconnect(&ctrl
->ctrl
)) {
906 dev_info(ctrl
->ctrl
.device
, "Reconnecting in %d seconds...\n",
907 ctrl
->ctrl
.opts
->reconnect_delay
);
908 queue_delayed_work(nvme_wq
, &ctrl
->reconnect_work
,
909 ctrl
->ctrl
.opts
->reconnect_delay
* HZ
);
911 nvme_delete_ctrl(&ctrl
->ctrl
);
915 static void nvme_rdma_reconnect_ctrl_work(struct work_struct
*work
)
917 struct nvme_rdma_ctrl
*ctrl
= container_of(to_delayed_work(work
),
918 struct nvme_rdma_ctrl
, reconnect_work
);
922 ++ctrl
->ctrl
.nr_reconnects
;
924 ret
= nvme_rdma_configure_admin_queue(ctrl
, false);
928 if (ctrl
->ctrl
.queue_count
> 1) {
929 ret
= nvme_rdma_configure_io_queues(ctrl
, false);
934 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_LIVE
);
936 /* state change failure is ok if we're in DELETING state */
937 WARN_ON_ONCE(ctrl
->ctrl
.state
!= NVME_CTRL_DELETING
);
941 nvme_start_ctrl(&ctrl
->ctrl
);
943 dev_info(ctrl
->ctrl
.device
, "Successfully reconnected (%d attempts)\n",
944 ctrl
->ctrl
.nr_reconnects
);
946 ctrl
->ctrl
.nr_reconnects
= 0;
951 nvme_rdma_stop_queue(&ctrl
->queues
[0]);
952 nvme_rdma_destroy_admin_queue(ctrl
, false);
954 dev_info(ctrl
->ctrl
.device
, "Failed reconnect attempt %d\n",
955 ctrl
->ctrl
.nr_reconnects
);
956 nvme_rdma_reconnect_or_remove(ctrl
);
959 static void nvme_rdma_error_recovery_work(struct work_struct
*work
)
961 struct nvme_rdma_ctrl
*ctrl
= container_of(work
,
962 struct nvme_rdma_ctrl
, err_work
);
964 nvme_stop_keep_alive(&ctrl
->ctrl
);
966 if (ctrl
->ctrl
.queue_count
> 1) {
967 nvme_stop_queues(&ctrl
->ctrl
);
968 nvme_rdma_stop_io_queues(ctrl
);
969 blk_mq_tagset_busy_iter(&ctrl
->tag_set
,
970 nvme_cancel_request
, &ctrl
->ctrl
);
971 nvme_rdma_destroy_io_queues(ctrl
, false);
974 blk_mq_quiesce_queue(ctrl
->ctrl
.admin_q
);
975 nvme_rdma_stop_queue(&ctrl
->queues
[0]);
976 blk_mq_tagset_busy_iter(&ctrl
->admin_tag_set
,
977 nvme_cancel_request
, &ctrl
->ctrl
);
978 nvme_rdma_destroy_admin_queue(ctrl
, false);
981 * queues are not a live anymore, so restart the queues to fail fast
984 blk_mq_unquiesce_queue(ctrl
->ctrl
.admin_q
);
985 nvme_start_queues(&ctrl
->ctrl
);
987 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_CONNECTING
)) {
988 /* state change failure is ok if we're in DELETING state */
989 WARN_ON_ONCE(ctrl
->ctrl
.state
!= NVME_CTRL_DELETING
);
993 nvme_rdma_reconnect_or_remove(ctrl
);
996 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl
*ctrl
)
998 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_RESETTING
))
1001 queue_work(nvme_wq
, &ctrl
->err_work
);
1004 static void nvme_rdma_wr_error(struct ib_cq
*cq
, struct ib_wc
*wc
,
1007 struct nvme_rdma_queue
*queue
= cq
->cq_context
;
1008 struct nvme_rdma_ctrl
*ctrl
= queue
->ctrl
;
1010 if (ctrl
->ctrl
.state
== NVME_CTRL_LIVE
)
1011 dev_info(ctrl
->ctrl
.device
,
1012 "%s for CQE 0x%p failed with status %s (%d)\n",
1014 ib_wc_status_msg(wc
->status
), wc
->status
);
1015 nvme_rdma_error_recovery(ctrl
);
1018 static void nvme_rdma_memreg_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
1020 if (unlikely(wc
->status
!= IB_WC_SUCCESS
))
1021 nvme_rdma_wr_error(cq
, wc
, "MEMREG");
1024 static void nvme_rdma_inv_rkey_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
1026 struct nvme_rdma_request
*req
=
1027 container_of(wc
->wr_cqe
, struct nvme_rdma_request
, reg_cqe
);
1028 struct request
*rq
= blk_mq_rq_from_pdu(req
);
1030 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
1031 nvme_rdma_wr_error(cq
, wc
, "LOCAL_INV");
1035 if (refcount_dec_and_test(&req
->ref
))
1036 nvme_end_request(rq
, req
->status
, req
->result
);
1040 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue
*queue
,
1041 struct nvme_rdma_request
*req
)
1043 struct ib_send_wr
*bad_wr
;
1044 struct ib_send_wr wr
= {
1045 .opcode
= IB_WR_LOCAL_INV
,
1048 .send_flags
= IB_SEND_SIGNALED
,
1049 .ex
.invalidate_rkey
= req
->mr
->rkey
,
1052 req
->reg_cqe
.done
= nvme_rdma_inv_rkey_done
;
1053 wr
.wr_cqe
= &req
->reg_cqe
;
1055 return ib_post_send(queue
->qp
, &wr
, &bad_wr
);
1058 static void nvme_rdma_unmap_data(struct nvme_rdma_queue
*queue
,
1061 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1062 struct nvme_rdma_device
*dev
= queue
->device
;
1063 struct ib_device
*ibdev
= dev
->dev
;
1065 if (!blk_rq_payload_bytes(rq
))
1069 ib_mr_pool_put(queue
->qp
, &queue
->qp
->rdma_mrs
, req
->mr
);
1073 ib_dma_unmap_sg(ibdev
, req
->sg_table
.sgl
,
1074 req
->nents
, rq_data_dir(rq
) ==
1075 WRITE
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
1077 nvme_cleanup_cmd(rq
);
1078 sg_free_table_chained(&req
->sg_table
, true);
1081 static int nvme_rdma_set_sg_null(struct nvme_command
*c
)
1083 struct nvme_keyed_sgl_desc
*sg
= &c
->common
.dptr
.ksgl
;
1086 put_unaligned_le24(0, sg
->length
);
1087 put_unaligned_le32(0, sg
->key
);
1088 sg
->type
= NVME_KEY_SGL_FMT_DATA_DESC
<< 4;
1092 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue
*queue
,
1093 struct nvme_rdma_request
*req
, struct nvme_command
*c
)
1095 struct nvme_sgl_desc
*sg
= &c
->common
.dptr
.sgl
;
1097 req
->sge
[1].addr
= sg_dma_address(req
->sg_table
.sgl
);
1098 req
->sge
[1].length
= sg_dma_len(req
->sg_table
.sgl
);
1099 req
->sge
[1].lkey
= queue
->device
->pd
->local_dma_lkey
;
1101 sg
->addr
= cpu_to_le64(queue
->ctrl
->ctrl
.icdoff
);
1102 sg
->length
= cpu_to_le32(sg_dma_len(req
->sg_table
.sgl
));
1103 sg
->type
= (NVME_SGL_FMT_DATA_DESC
<< 4) | NVME_SGL_FMT_OFFSET
;
1109 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue
*queue
,
1110 struct nvme_rdma_request
*req
, struct nvme_command
*c
)
1112 struct nvme_keyed_sgl_desc
*sg
= &c
->common
.dptr
.ksgl
;
1114 sg
->addr
= cpu_to_le64(sg_dma_address(req
->sg_table
.sgl
));
1115 put_unaligned_le24(sg_dma_len(req
->sg_table
.sgl
), sg
->length
);
1116 put_unaligned_le32(queue
->device
->pd
->unsafe_global_rkey
, sg
->key
);
1117 sg
->type
= NVME_KEY_SGL_FMT_DATA_DESC
<< 4;
1121 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue
*queue
,
1122 struct nvme_rdma_request
*req
, struct nvme_command
*c
,
1125 struct nvme_keyed_sgl_desc
*sg
= &c
->common
.dptr
.ksgl
;
1128 req
->mr
= ib_mr_pool_get(queue
->qp
, &queue
->qp
->rdma_mrs
);
1129 if (WARN_ON_ONCE(!req
->mr
))
1133 * Align the MR to a 4K page size to match the ctrl page size and
1134 * the block virtual boundary.
1136 nr
= ib_map_mr_sg(req
->mr
, req
->sg_table
.sgl
, count
, NULL
, SZ_4K
);
1137 if (unlikely(nr
< count
)) {
1138 ib_mr_pool_put(queue
->qp
, &queue
->qp
->rdma_mrs
, req
->mr
);
1145 ib_update_fast_reg_key(req
->mr
, ib_inc_rkey(req
->mr
->rkey
));
1147 req
->reg_cqe
.done
= nvme_rdma_memreg_done
;
1148 memset(&req
->reg_wr
, 0, sizeof(req
->reg_wr
));
1149 req
->reg_wr
.wr
.opcode
= IB_WR_REG_MR
;
1150 req
->reg_wr
.wr
.wr_cqe
= &req
->reg_cqe
;
1151 req
->reg_wr
.wr
.num_sge
= 0;
1152 req
->reg_wr
.mr
= req
->mr
;
1153 req
->reg_wr
.key
= req
->mr
->rkey
;
1154 req
->reg_wr
.access
= IB_ACCESS_LOCAL_WRITE
|
1155 IB_ACCESS_REMOTE_READ
|
1156 IB_ACCESS_REMOTE_WRITE
;
1158 sg
->addr
= cpu_to_le64(req
->mr
->iova
);
1159 put_unaligned_le24(req
->mr
->length
, sg
->length
);
1160 put_unaligned_le32(req
->mr
->rkey
, sg
->key
);
1161 sg
->type
= (NVME_KEY_SGL_FMT_DATA_DESC
<< 4) |
1162 NVME_SGL_FMT_INVALIDATE
;
1167 static int nvme_rdma_map_data(struct nvme_rdma_queue
*queue
,
1168 struct request
*rq
, struct nvme_command
*c
)
1170 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1171 struct nvme_rdma_device
*dev
= queue
->device
;
1172 struct ib_device
*ibdev
= dev
->dev
;
1176 refcount_set(&req
->ref
, 2); /* send and recv completions */
1178 c
->common
.flags
|= NVME_CMD_SGL_METABUF
;
1180 if (!blk_rq_payload_bytes(rq
))
1181 return nvme_rdma_set_sg_null(c
);
1183 req
->sg_table
.sgl
= req
->first_sgl
;
1184 ret
= sg_alloc_table_chained(&req
->sg_table
,
1185 blk_rq_nr_phys_segments(rq
), req
->sg_table
.sgl
);
1189 req
->nents
= blk_rq_map_sg(rq
->q
, rq
, req
->sg_table
.sgl
);
1191 count
= ib_dma_map_sg(ibdev
, req
->sg_table
.sgl
, req
->nents
,
1192 rq_data_dir(rq
) == WRITE
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
1193 if (unlikely(count
<= 0)) {
1195 goto out_free_table
;
1199 if (rq_data_dir(rq
) == WRITE
&& nvme_rdma_queue_idx(queue
) &&
1200 blk_rq_payload_bytes(rq
) <=
1201 nvme_rdma_inline_data_size(queue
)) {
1202 ret
= nvme_rdma_map_sg_inline(queue
, req
, c
);
1206 if (dev
->pd
->flags
& IB_PD_UNSAFE_GLOBAL_RKEY
) {
1207 ret
= nvme_rdma_map_sg_single(queue
, req
, c
);
1212 ret
= nvme_rdma_map_sg_fr(queue
, req
, c
, count
);
1220 ib_dma_unmap_sg(ibdev
, req
->sg_table
.sgl
,
1221 req
->nents
, rq_data_dir(rq
) ==
1222 WRITE
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
1224 sg_free_table_chained(&req
->sg_table
, true);
1228 static void nvme_rdma_send_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
1230 struct nvme_rdma_qe
*qe
=
1231 container_of(wc
->wr_cqe
, struct nvme_rdma_qe
, cqe
);
1232 struct nvme_rdma_request
*req
=
1233 container_of(qe
, struct nvme_rdma_request
, sqe
);
1234 struct request
*rq
= blk_mq_rq_from_pdu(req
);
1236 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
1237 nvme_rdma_wr_error(cq
, wc
, "SEND");
1241 if (refcount_dec_and_test(&req
->ref
))
1242 nvme_end_request(rq
, req
->status
, req
->result
);
1245 static int nvme_rdma_post_send(struct nvme_rdma_queue
*queue
,
1246 struct nvme_rdma_qe
*qe
, struct ib_sge
*sge
, u32 num_sge
,
1247 struct ib_send_wr
*first
)
1249 struct ib_send_wr wr
, *bad_wr
;
1252 sge
->addr
= qe
->dma
;
1253 sge
->length
= sizeof(struct nvme_command
),
1254 sge
->lkey
= queue
->device
->pd
->local_dma_lkey
;
1257 wr
.wr_cqe
= &qe
->cqe
;
1259 wr
.num_sge
= num_sge
;
1260 wr
.opcode
= IB_WR_SEND
;
1261 wr
.send_flags
= IB_SEND_SIGNALED
;
1268 ret
= ib_post_send(queue
->qp
, first
, &bad_wr
);
1269 if (unlikely(ret
)) {
1270 dev_err(queue
->ctrl
->ctrl
.device
,
1271 "%s failed with error code %d\n", __func__
, ret
);
1276 static int nvme_rdma_post_recv(struct nvme_rdma_queue
*queue
,
1277 struct nvme_rdma_qe
*qe
)
1279 struct ib_recv_wr wr
, *bad_wr
;
1283 list
.addr
= qe
->dma
;
1284 list
.length
= sizeof(struct nvme_completion
);
1285 list
.lkey
= queue
->device
->pd
->local_dma_lkey
;
1287 qe
->cqe
.done
= nvme_rdma_recv_done
;
1290 wr
.wr_cqe
= &qe
->cqe
;
1294 ret
= ib_post_recv(queue
->qp
, &wr
, &bad_wr
);
1295 if (unlikely(ret
)) {
1296 dev_err(queue
->ctrl
->ctrl
.device
,
1297 "%s failed with error code %d\n", __func__
, ret
);
1302 static struct blk_mq_tags
*nvme_rdma_tagset(struct nvme_rdma_queue
*queue
)
1304 u32 queue_idx
= nvme_rdma_queue_idx(queue
);
1307 return queue
->ctrl
->admin_tag_set
.tags
[queue_idx
];
1308 return queue
->ctrl
->tag_set
.tags
[queue_idx
- 1];
1311 static void nvme_rdma_async_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
1313 if (unlikely(wc
->status
!= IB_WC_SUCCESS
))
1314 nvme_rdma_wr_error(cq
, wc
, "ASYNC");
1317 static void nvme_rdma_submit_async_event(struct nvme_ctrl
*arg
)
1319 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(arg
);
1320 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[0];
1321 struct ib_device
*dev
= queue
->device
->dev
;
1322 struct nvme_rdma_qe
*sqe
= &ctrl
->async_event_sqe
;
1323 struct nvme_command
*cmd
= sqe
->data
;
1327 ib_dma_sync_single_for_cpu(dev
, sqe
->dma
, sizeof(*cmd
), DMA_TO_DEVICE
);
1329 memset(cmd
, 0, sizeof(*cmd
));
1330 cmd
->common
.opcode
= nvme_admin_async_event
;
1331 cmd
->common
.command_id
= NVME_AQ_BLK_MQ_DEPTH
;
1332 cmd
->common
.flags
|= NVME_CMD_SGL_METABUF
;
1333 nvme_rdma_set_sg_null(cmd
);
1335 sqe
->cqe
.done
= nvme_rdma_async_done
;
1337 ib_dma_sync_single_for_device(dev
, sqe
->dma
, sizeof(*cmd
),
1340 ret
= nvme_rdma_post_send(queue
, sqe
, &sge
, 1, NULL
);
1344 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue
*queue
,
1345 struct nvme_completion
*cqe
, struct ib_wc
*wc
, int tag
)
1348 struct nvme_rdma_request
*req
;
1351 rq
= blk_mq_tag_to_rq(nvme_rdma_tagset(queue
), cqe
->command_id
);
1353 dev_err(queue
->ctrl
->ctrl
.device
,
1354 "tag 0x%x on QP %#x not found\n",
1355 cqe
->command_id
, queue
->qp
->qp_num
);
1356 nvme_rdma_error_recovery(queue
->ctrl
);
1359 req
= blk_mq_rq_to_pdu(rq
);
1361 req
->status
= cqe
->status
;
1362 req
->result
= cqe
->result
;
1364 if (wc
->wc_flags
& IB_WC_WITH_INVALIDATE
) {
1365 if (unlikely(wc
->ex
.invalidate_rkey
!= req
->mr
->rkey
)) {
1366 dev_err(queue
->ctrl
->ctrl
.device
,
1367 "Bogus remote invalidation for rkey %#x\n",
1369 nvme_rdma_error_recovery(queue
->ctrl
);
1371 } else if (req
->mr
) {
1372 ret
= nvme_rdma_inv_rkey(queue
, req
);
1373 if (unlikely(ret
< 0)) {
1374 dev_err(queue
->ctrl
->ctrl
.device
,
1375 "Queueing INV WR for rkey %#x failed (%d)\n",
1376 req
->mr
->rkey
, ret
);
1377 nvme_rdma_error_recovery(queue
->ctrl
);
1379 /* the local invalidation completion will end the request */
1383 if (refcount_dec_and_test(&req
->ref
)) {
1386 nvme_end_request(rq
, req
->status
, req
->result
);
1392 static int __nvme_rdma_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
, int tag
)
1394 struct nvme_rdma_qe
*qe
=
1395 container_of(wc
->wr_cqe
, struct nvme_rdma_qe
, cqe
);
1396 struct nvme_rdma_queue
*queue
= cq
->cq_context
;
1397 struct ib_device
*ibdev
= queue
->device
->dev
;
1398 struct nvme_completion
*cqe
= qe
->data
;
1399 const size_t len
= sizeof(struct nvme_completion
);
1402 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
1403 nvme_rdma_wr_error(cq
, wc
, "RECV");
1407 ib_dma_sync_single_for_cpu(ibdev
, qe
->dma
, len
, DMA_FROM_DEVICE
);
1409 * AEN requests are special as they don't time out and can
1410 * survive any kind of queue freeze and often don't respond to
1411 * aborts. We don't even bother to allocate a struct request
1412 * for them but rather special case them here.
1414 if (unlikely(nvme_rdma_queue_idx(queue
) == 0 &&
1415 cqe
->command_id
>= NVME_AQ_BLK_MQ_DEPTH
))
1416 nvme_complete_async_event(&queue
->ctrl
->ctrl
, cqe
->status
,
1419 ret
= nvme_rdma_process_nvme_rsp(queue
, cqe
, wc
, tag
);
1420 ib_dma_sync_single_for_device(ibdev
, qe
->dma
, len
, DMA_FROM_DEVICE
);
1422 nvme_rdma_post_recv(queue
, qe
);
1426 static void nvme_rdma_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
1428 __nvme_rdma_recv_done(cq
, wc
, -1);
1431 static int nvme_rdma_conn_established(struct nvme_rdma_queue
*queue
)
1435 for (i
= 0; i
< queue
->queue_size
; i
++) {
1436 ret
= nvme_rdma_post_recv(queue
, &queue
->rsp_ring
[i
]);
1438 goto out_destroy_queue_ib
;
1443 out_destroy_queue_ib
:
1444 nvme_rdma_destroy_queue_ib(queue
);
1448 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue
*queue
,
1449 struct rdma_cm_event
*ev
)
1451 struct rdma_cm_id
*cm_id
= queue
->cm_id
;
1452 int status
= ev
->status
;
1453 const char *rej_msg
;
1454 const struct nvme_rdma_cm_rej
*rej_data
;
1457 rej_msg
= rdma_reject_msg(cm_id
, status
);
1458 rej_data
= rdma_consumer_reject_data(cm_id
, ev
, &rej_data_len
);
1460 if (rej_data
&& rej_data_len
>= sizeof(u16
)) {
1461 u16 sts
= le16_to_cpu(rej_data
->sts
);
1463 dev_err(queue
->ctrl
->ctrl
.device
,
1464 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1465 status
, rej_msg
, sts
, nvme_rdma_cm_msg(sts
));
1467 dev_err(queue
->ctrl
->ctrl
.device
,
1468 "Connect rejected: status %d (%s).\n", status
, rej_msg
);
1474 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue
*queue
)
1478 ret
= nvme_rdma_create_queue_ib(queue
);
1482 ret
= rdma_resolve_route(queue
->cm_id
, NVME_RDMA_CONNECT_TIMEOUT_MS
);
1484 dev_err(queue
->ctrl
->ctrl
.device
,
1485 "rdma_resolve_route failed (%d).\n",
1487 goto out_destroy_queue
;
1493 nvme_rdma_destroy_queue_ib(queue
);
1497 static int nvme_rdma_route_resolved(struct nvme_rdma_queue
*queue
)
1499 struct nvme_rdma_ctrl
*ctrl
= queue
->ctrl
;
1500 struct rdma_conn_param param
= { };
1501 struct nvme_rdma_cm_req priv
= { };
1504 param
.qp_num
= queue
->qp
->qp_num
;
1505 param
.flow_control
= 1;
1507 param
.responder_resources
= queue
->device
->dev
->attrs
.max_qp_rd_atom
;
1508 /* maximum retry count */
1509 param
.retry_count
= 7;
1510 param
.rnr_retry_count
= 7;
1511 param
.private_data
= &priv
;
1512 param
.private_data_len
= sizeof(priv
);
1514 priv
.recfmt
= cpu_to_le16(NVME_RDMA_CM_FMT_1_0
);
1515 priv
.qid
= cpu_to_le16(nvme_rdma_queue_idx(queue
));
1517 * set the admin queue depth to the minimum size
1518 * specified by the Fabrics standard.
1520 if (priv
.qid
== 0) {
1521 priv
.hrqsize
= cpu_to_le16(NVME_AQ_DEPTH
);
1522 priv
.hsqsize
= cpu_to_le16(NVME_AQ_DEPTH
- 1);
1525 * current interpretation of the fabrics spec
1526 * is at minimum you make hrqsize sqsize+1, or a
1527 * 1's based representation of sqsize.
1529 priv
.hrqsize
= cpu_to_le16(queue
->queue_size
);
1530 priv
.hsqsize
= cpu_to_le16(queue
->ctrl
->ctrl
.sqsize
);
1533 ret
= rdma_connect(queue
->cm_id
, ¶m
);
1535 dev_err(ctrl
->ctrl
.device
,
1536 "rdma_connect failed (%d).\n", ret
);
1537 goto out_destroy_queue_ib
;
1542 out_destroy_queue_ib
:
1543 nvme_rdma_destroy_queue_ib(queue
);
1547 static int nvme_rdma_cm_handler(struct rdma_cm_id
*cm_id
,
1548 struct rdma_cm_event
*ev
)
1550 struct nvme_rdma_queue
*queue
= cm_id
->context
;
1553 dev_dbg(queue
->ctrl
->ctrl
.device
, "%s (%d): status %d id %p\n",
1554 rdma_event_msg(ev
->event
), ev
->event
,
1557 switch (ev
->event
) {
1558 case RDMA_CM_EVENT_ADDR_RESOLVED
:
1559 cm_error
= nvme_rdma_addr_resolved(queue
);
1561 case RDMA_CM_EVENT_ROUTE_RESOLVED
:
1562 cm_error
= nvme_rdma_route_resolved(queue
);
1564 case RDMA_CM_EVENT_ESTABLISHED
:
1565 queue
->cm_error
= nvme_rdma_conn_established(queue
);
1566 /* complete cm_done regardless of success/failure */
1567 complete(&queue
->cm_done
);
1569 case RDMA_CM_EVENT_REJECTED
:
1570 nvme_rdma_destroy_queue_ib(queue
);
1571 cm_error
= nvme_rdma_conn_rejected(queue
, ev
);
1573 case RDMA_CM_EVENT_ROUTE_ERROR
:
1574 case RDMA_CM_EVENT_CONNECT_ERROR
:
1575 case RDMA_CM_EVENT_UNREACHABLE
:
1576 nvme_rdma_destroy_queue_ib(queue
);
1577 case RDMA_CM_EVENT_ADDR_ERROR
:
1578 dev_dbg(queue
->ctrl
->ctrl
.device
,
1579 "CM error event %d\n", ev
->event
);
1580 cm_error
= -ECONNRESET
;
1582 case RDMA_CM_EVENT_DISCONNECTED
:
1583 case RDMA_CM_EVENT_ADDR_CHANGE
:
1584 case RDMA_CM_EVENT_TIMEWAIT_EXIT
:
1585 dev_dbg(queue
->ctrl
->ctrl
.device
,
1586 "disconnect received - connection closed\n");
1587 nvme_rdma_error_recovery(queue
->ctrl
);
1589 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
1590 /* device removal is handled via the ib_client API */
1593 dev_err(queue
->ctrl
->ctrl
.device
,
1594 "Unexpected RDMA CM event (%d)\n", ev
->event
);
1595 nvme_rdma_error_recovery(queue
->ctrl
);
1600 queue
->cm_error
= cm_error
;
1601 complete(&queue
->cm_done
);
1607 static enum blk_eh_timer_return
1608 nvme_rdma_timeout(struct request
*rq
, bool reserved
)
1610 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1612 dev_warn(req
->queue
->ctrl
->ctrl
.device
,
1613 "I/O %d QID %d timeout, reset controller\n",
1614 rq
->tag
, nvme_rdma_queue_idx(req
->queue
));
1616 /* queue error recovery */
1617 nvme_rdma_error_recovery(req
->queue
->ctrl
);
1619 /* fail with DNR on cmd timeout */
1620 nvme_req(rq
)->status
= NVME_SC_ABORT_REQ
| NVME_SC_DNR
;
1625 static blk_status_t
nvme_rdma_queue_rq(struct blk_mq_hw_ctx
*hctx
,
1626 const struct blk_mq_queue_data
*bd
)
1628 struct nvme_ns
*ns
= hctx
->queue
->queuedata
;
1629 struct nvme_rdma_queue
*queue
= hctx
->driver_data
;
1630 struct request
*rq
= bd
->rq
;
1631 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1632 struct nvme_rdma_qe
*sqe
= &req
->sqe
;
1633 struct nvme_command
*c
= sqe
->data
;
1634 struct ib_device
*dev
;
1635 bool queue_ready
= test_bit(NVME_RDMA_Q_LIVE
, &queue
->flags
);
1639 WARN_ON_ONCE(rq
->tag
< 0);
1641 if (!nvmf_check_ready(&queue
->ctrl
->ctrl
, rq
, queue_ready
))
1642 return nvmf_fail_nonready_command(rq
);
1644 dev
= queue
->device
->dev
;
1645 ib_dma_sync_single_for_cpu(dev
, sqe
->dma
,
1646 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
1648 ret
= nvme_setup_cmd(ns
, rq
, c
);
1652 blk_mq_start_request(rq
);
1654 err
= nvme_rdma_map_data(queue
, rq
, c
);
1655 if (unlikely(err
< 0)) {
1656 dev_err(queue
->ctrl
->ctrl
.device
,
1657 "Failed to map data (%d)\n", err
);
1658 nvme_cleanup_cmd(rq
);
1662 sqe
->cqe
.done
= nvme_rdma_send_done
;
1664 ib_dma_sync_single_for_device(dev
, sqe
->dma
,
1665 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
1667 err
= nvme_rdma_post_send(queue
, sqe
, req
->sge
, req
->num_sge
,
1668 req
->mr
? &req
->reg_wr
.wr
: NULL
);
1669 if (unlikely(err
)) {
1670 nvme_rdma_unmap_data(queue
, rq
);
1676 if (err
== -ENOMEM
|| err
== -EAGAIN
)
1677 return BLK_STS_RESOURCE
;
1678 return BLK_STS_IOERR
;
1681 static int nvme_rdma_poll(struct blk_mq_hw_ctx
*hctx
, unsigned int tag
)
1683 struct nvme_rdma_queue
*queue
= hctx
->driver_data
;
1684 struct ib_cq
*cq
= queue
->ib_cq
;
1688 while (ib_poll_cq(cq
, 1, &wc
) > 0) {
1689 struct ib_cqe
*cqe
= wc
.wr_cqe
;
1692 if (cqe
->done
== nvme_rdma_recv_done
)
1693 found
|= __nvme_rdma_recv_done(cq
, &wc
, tag
);
1702 static void nvme_rdma_complete_rq(struct request
*rq
)
1704 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1706 nvme_rdma_unmap_data(req
->queue
, rq
);
1707 nvme_complete_rq(rq
);
1710 static int nvme_rdma_map_queues(struct blk_mq_tag_set
*set
)
1712 struct nvme_rdma_ctrl
*ctrl
= set
->driver_data
;
1714 return blk_mq_rdma_map_queues(set
, ctrl
->device
->dev
, 0);
1717 static const struct blk_mq_ops nvme_rdma_mq_ops
= {
1718 .queue_rq
= nvme_rdma_queue_rq
,
1719 .complete
= nvme_rdma_complete_rq
,
1720 .init_request
= nvme_rdma_init_request
,
1721 .exit_request
= nvme_rdma_exit_request
,
1722 .init_hctx
= nvme_rdma_init_hctx
,
1723 .poll
= nvme_rdma_poll
,
1724 .timeout
= nvme_rdma_timeout
,
1725 .map_queues
= nvme_rdma_map_queues
,
1728 static const struct blk_mq_ops nvme_rdma_admin_mq_ops
= {
1729 .queue_rq
= nvme_rdma_queue_rq
,
1730 .complete
= nvme_rdma_complete_rq
,
1731 .init_request
= nvme_rdma_init_request
,
1732 .exit_request
= nvme_rdma_exit_request
,
1733 .init_hctx
= nvme_rdma_init_admin_hctx
,
1734 .timeout
= nvme_rdma_timeout
,
1737 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl
*ctrl
, bool shutdown
)
1739 if (ctrl
->ctrl
.queue_count
> 1) {
1740 nvme_stop_queues(&ctrl
->ctrl
);
1741 nvme_rdma_stop_io_queues(ctrl
);
1742 blk_mq_tagset_busy_iter(&ctrl
->tag_set
,
1743 nvme_cancel_request
, &ctrl
->ctrl
);
1744 nvme_rdma_destroy_io_queues(ctrl
, shutdown
);
1748 nvme_shutdown_ctrl(&ctrl
->ctrl
);
1750 nvme_disable_ctrl(&ctrl
->ctrl
, ctrl
->ctrl
.cap
);
1752 blk_mq_quiesce_queue(ctrl
->ctrl
.admin_q
);
1753 nvme_rdma_stop_queue(&ctrl
->queues
[0]);
1754 blk_mq_tagset_busy_iter(&ctrl
->admin_tag_set
,
1755 nvme_cancel_request
, &ctrl
->ctrl
);
1756 blk_mq_unquiesce_queue(ctrl
->ctrl
.admin_q
);
1757 nvme_rdma_destroy_admin_queue(ctrl
, shutdown
);
1760 static void nvme_rdma_delete_ctrl(struct nvme_ctrl
*ctrl
)
1762 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl
), true);
1765 static void nvme_rdma_reset_ctrl_work(struct work_struct
*work
)
1767 struct nvme_rdma_ctrl
*ctrl
=
1768 container_of(work
, struct nvme_rdma_ctrl
, ctrl
.reset_work
);
1772 nvme_stop_ctrl(&ctrl
->ctrl
);
1773 nvme_rdma_shutdown_ctrl(ctrl
, false);
1775 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_CONNECTING
)) {
1776 /* state change failure should never happen */
1781 ret
= nvme_rdma_configure_admin_queue(ctrl
, false);
1785 if (ctrl
->ctrl
.queue_count
> 1) {
1786 ret
= nvme_rdma_configure_io_queues(ctrl
, false);
1791 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_LIVE
);
1793 /* state change failure is ok if we're in DELETING state */
1794 WARN_ON_ONCE(ctrl
->ctrl
.state
!= NVME_CTRL_DELETING
);
1798 nvme_start_ctrl(&ctrl
->ctrl
);
1803 ++ctrl
->ctrl
.nr_reconnects
;
1804 nvme_rdma_reconnect_or_remove(ctrl
);
1807 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops
= {
1809 .module
= THIS_MODULE
,
1810 .flags
= NVME_F_FABRICS
,
1811 .reg_read32
= nvmf_reg_read32
,
1812 .reg_read64
= nvmf_reg_read64
,
1813 .reg_write32
= nvmf_reg_write32
,
1814 .free_ctrl
= nvme_rdma_free_ctrl
,
1815 .submit_async_event
= nvme_rdma_submit_async_event
,
1816 .delete_ctrl
= nvme_rdma_delete_ctrl
,
1817 .get_address
= nvmf_get_address
,
1818 .stop_ctrl
= nvme_rdma_stop_ctrl
,
1822 __nvme_rdma_options_match(struct nvme_rdma_ctrl
*ctrl
,
1823 struct nvmf_ctrl_options
*opts
)
1825 char *stdport
= __stringify(NVME_RDMA_IP_PORT
);
1828 if (!nvmf_ctlr_matches_baseopts(&ctrl
->ctrl
, opts
) ||
1829 strcmp(opts
->traddr
, ctrl
->ctrl
.opts
->traddr
))
1832 if (opts
->mask
& NVMF_OPT_TRSVCID
&&
1833 ctrl
->ctrl
.opts
->mask
& NVMF_OPT_TRSVCID
) {
1834 if (strcmp(opts
->trsvcid
, ctrl
->ctrl
.opts
->trsvcid
))
1836 } else if (opts
->mask
& NVMF_OPT_TRSVCID
) {
1837 if (strcmp(opts
->trsvcid
, stdport
))
1839 } else if (ctrl
->ctrl
.opts
->mask
& NVMF_OPT_TRSVCID
) {
1840 if (strcmp(stdport
, ctrl
->ctrl
.opts
->trsvcid
))
1843 /* else, it's a match as both have stdport. Fall to next checks */
1846 * checking the local address is rough. In most cases, one
1847 * is not specified and the host port is selected by the stack.
1849 * Assume no match if:
1850 * local address is specified and address is not the same
1851 * local address is not specified but remote is, or vice versa
1852 * (admin using specific host_traddr when it matters).
1854 if (opts
->mask
& NVMF_OPT_HOST_TRADDR
&&
1855 ctrl
->ctrl
.opts
->mask
& NVMF_OPT_HOST_TRADDR
) {
1856 if (strcmp(opts
->host_traddr
, ctrl
->ctrl
.opts
->host_traddr
))
1858 } else if (opts
->mask
& NVMF_OPT_HOST_TRADDR
||
1859 ctrl
->ctrl
.opts
->mask
& NVMF_OPT_HOST_TRADDR
)
1862 * if neither controller had an host port specified, assume it's
1863 * a match as everything else matched.
1870 * Fails a connection request if it matches an existing controller
1871 * (association) with the same tuple:
1872 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
1874 * if local address is not specified in the request, it will match an
1875 * existing controller with all the other parameters the same and no
1876 * local port address specified as well.
1878 * The ports don't need to be compared as they are intrinsically
1879 * already matched by the port pointers supplied.
1882 nvme_rdma_existing_controller(struct nvmf_ctrl_options
*opts
)
1884 struct nvme_rdma_ctrl
*ctrl
;
1887 mutex_lock(&nvme_rdma_ctrl_mutex
);
1888 list_for_each_entry(ctrl
, &nvme_rdma_ctrl_list
, list
) {
1889 found
= __nvme_rdma_options_match(ctrl
, opts
);
1893 mutex_unlock(&nvme_rdma_ctrl_mutex
);
1898 static struct nvme_ctrl
*nvme_rdma_create_ctrl(struct device
*dev
,
1899 struct nvmf_ctrl_options
*opts
)
1901 struct nvme_rdma_ctrl
*ctrl
;
1906 ctrl
= kzalloc(sizeof(*ctrl
), GFP_KERNEL
);
1908 return ERR_PTR(-ENOMEM
);
1909 ctrl
->ctrl
.opts
= opts
;
1910 INIT_LIST_HEAD(&ctrl
->list
);
1912 if (opts
->mask
& NVMF_OPT_TRSVCID
)
1913 port
= opts
->trsvcid
;
1915 port
= __stringify(NVME_RDMA_IP_PORT
);
1917 ret
= inet_pton_with_scope(&init_net
, AF_UNSPEC
,
1918 opts
->traddr
, port
, &ctrl
->addr
);
1920 pr_err("malformed address passed: %s:%s\n", opts
->traddr
, port
);
1924 if (opts
->mask
& NVMF_OPT_HOST_TRADDR
) {
1925 ret
= inet_pton_with_scope(&init_net
, AF_UNSPEC
,
1926 opts
->host_traddr
, NULL
, &ctrl
->src_addr
);
1928 pr_err("malformed src address passed: %s\n",
1934 if (!opts
->duplicate_connect
&& nvme_rdma_existing_controller(opts
)) {
1939 INIT_DELAYED_WORK(&ctrl
->reconnect_work
,
1940 nvme_rdma_reconnect_ctrl_work
);
1941 INIT_WORK(&ctrl
->err_work
, nvme_rdma_error_recovery_work
);
1942 INIT_WORK(&ctrl
->ctrl
.reset_work
, nvme_rdma_reset_ctrl_work
);
1944 ctrl
->ctrl
.queue_count
= opts
->nr_io_queues
+ 1; /* +1 for admin queue */
1945 ctrl
->ctrl
.sqsize
= opts
->queue_size
- 1;
1946 ctrl
->ctrl
.kato
= opts
->kato
;
1949 ctrl
->queues
= kcalloc(ctrl
->ctrl
.queue_count
, sizeof(*ctrl
->queues
),
1954 ret
= nvme_init_ctrl(&ctrl
->ctrl
, dev
, &nvme_rdma_ctrl_ops
,
1955 0 /* no quirks, we're perfect! */);
1957 goto out_kfree_queues
;
1959 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_CONNECTING
);
1960 WARN_ON_ONCE(!changed
);
1962 ret
= nvme_rdma_configure_admin_queue(ctrl
, true);
1964 goto out_uninit_ctrl
;
1966 /* sanity check icdoff */
1967 if (ctrl
->ctrl
.icdoff
) {
1968 dev_err(ctrl
->ctrl
.device
, "icdoff is not supported!\n");
1970 goto out_remove_admin_queue
;
1973 /* sanity check keyed sgls */
1974 if (!(ctrl
->ctrl
.sgls
& (1 << 2))) {
1975 dev_err(ctrl
->ctrl
.device
,
1976 "Mandatory keyed sgls are not supported!\n");
1978 goto out_remove_admin_queue
;
1981 /* only warn if argument is too large here, will clamp later */
1982 if (opts
->queue_size
> ctrl
->ctrl
.sqsize
+ 1) {
1983 dev_warn(ctrl
->ctrl
.device
,
1984 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1985 opts
->queue_size
, ctrl
->ctrl
.sqsize
+ 1);
1988 /* warn if maxcmd is lower than sqsize+1 */
1989 if (ctrl
->ctrl
.sqsize
+ 1 > ctrl
->ctrl
.maxcmd
) {
1990 dev_warn(ctrl
->ctrl
.device
,
1991 "sqsize %u > ctrl maxcmd %u, clamping down\n",
1992 ctrl
->ctrl
.sqsize
+ 1, ctrl
->ctrl
.maxcmd
);
1993 ctrl
->ctrl
.sqsize
= ctrl
->ctrl
.maxcmd
- 1;
1996 if (opts
->nr_io_queues
) {
1997 ret
= nvme_rdma_configure_io_queues(ctrl
, true);
1999 goto out_remove_admin_queue
;
2002 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_LIVE
);
2003 WARN_ON_ONCE(!changed
);
2005 dev_info(ctrl
->ctrl
.device
, "new ctrl: NQN \"%s\", addr %pISpcs\n",
2006 ctrl
->ctrl
.opts
->subsysnqn
, &ctrl
->addr
);
2008 nvme_get_ctrl(&ctrl
->ctrl
);
2010 mutex_lock(&nvme_rdma_ctrl_mutex
);
2011 list_add_tail(&ctrl
->list
, &nvme_rdma_ctrl_list
);
2012 mutex_unlock(&nvme_rdma_ctrl_mutex
);
2014 nvme_start_ctrl(&ctrl
->ctrl
);
2018 out_remove_admin_queue
:
2019 nvme_rdma_stop_queue(&ctrl
->queues
[0]);
2020 nvme_rdma_destroy_admin_queue(ctrl
, true);
2022 nvme_uninit_ctrl(&ctrl
->ctrl
);
2023 nvme_put_ctrl(&ctrl
->ctrl
);
2026 return ERR_PTR(ret
);
2028 kfree(ctrl
->queues
);
2031 return ERR_PTR(ret
);
2034 static struct nvmf_transport_ops nvme_rdma_transport
= {
2036 .module
= THIS_MODULE
,
2037 .required_opts
= NVMF_OPT_TRADDR
,
2038 .allowed_opts
= NVMF_OPT_TRSVCID
| NVMF_OPT_RECONNECT_DELAY
|
2039 NVMF_OPT_HOST_TRADDR
| NVMF_OPT_CTRL_LOSS_TMO
,
2040 .create_ctrl
= nvme_rdma_create_ctrl
,
2043 static void nvme_rdma_remove_one(struct ib_device
*ib_device
, void *client_data
)
2045 struct nvme_rdma_ctrl
*ctrl
;
2046 struct nvme_rdma_device
*ndev
;
2049 mutex_lock(&device_list_mutex
);
2050 list_for_each_entry(ndev
, &device_list
, entry
) {
2051 if (ndev
->dev
== ib_device
) {
2056 mutex_unlock(&device_list_mutex
);
2061 /* Delete all controllers using this device */
2062 mutex_lock(&nvme_rdma_ctrl_mutex
);
2063 list_for_each_entry(ctrl
, &nvme_rdma_ctrl_list
, list
) {
2064 if (ctrl
->device
->dev
!= ib_device
)
2066 nvme_delete_ctrl(&ctrl
->ctrl
);
2068 mutex_unlock(&nvme_rdma_ctrl_mutex
);
2070 flush_workqueue(nvme_delete_wq
);
2073 static struct ib_client nvme_rdma_ib_client
= {
2074 .name
= "nvme_rdma",
2075 .remove
= nvme_rdma_remove_one
2078 static int __init
nvme_rdma_init_module(void)
2082 ret
= ib_register_client(&nvme_rdma_ib_client
);
2086 ret
= nvmf_register_transport(&nvme_rdma_transport
);
2088 goto err_unreg_client
;
2093 ib_unregister_client(&nvme_rdma_ib_client
);
2097 static void __exit
nvme_rdma_cleanup_module(void)
2099 nvmf_unregister_transport(&nvme_rdma_transport
);
2100 ib_unregister_client(&nvme_rdma_ib_client
);
2103 module_init(nvme_rdma_init_module
);
2104 module_exit(nvme_rdma_cleanup_module
);
2106 MODULE_LICENSE("GPL v2");