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 <linux/err.h>
19 #include <linux/string.h>
20 #include <linux/atomic.h>
21 #include <linux/blk-mq.h>
22 #include <linux/types.h>
23 #include <linux/list.h>
24 #include <linux/mutex.h>
25 #include <linux/scatterlist.h>
26 #include <linux/nvme.h>
27 #include <asm/unaligned.h>
29 #include <rdma/ib_verbs.h>
30 #include <rdma/rdma_cm.h>
31 #include <rdma/ib_cm.h>
32 #include <linux/nvme-rdma.h>
38 #define NVME_RDMA_CONNECT_TIMEOUT_MS 1000 /* 1 second */
40 #define NVME_RDMA_MAX_SEGMENT_SIZE 0xffffff /* 24-bit SGL field */
42 #define NVME_RDMA_MAX_SEGMENTS 256
44 #define NVME_RDMA_MAX_INLINE_SEGMENTS 1
47 * We handle AEN commands ourselves and don't even let the
48 * block layer know about them.
50 #define NVME_RDMA_NR_AEN_COMMANDS 1
51 #define NVME_RDMA_AQ_BLKMQ_DEPTH \
52 (NVMF_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
54 struct nvme_rdma_device
{
55 struct ib_device
*dev
;
58 struct list_head entry
;
67 struct nvme_rdma_queue
;
68 struct nvme_rdma_request
{
69 struct nvme_request req
;
71 struct nvme_rdma_qe sqe
;
72 struct ib_sge sge
[1 + NVME_RDMA_MAX_INLINE_SEGMENTS
];
76 struct ib_reg_wr reg_wr
;
77 struct ib_cqe reg_cqe
;
78 struct nvme_rdma_queue
*queue
;
79 struct sg_table sg_table
;
80 struct scatterlist first_sgl
[];
83 enum nvme_rdma_queue_flags
{
84 NVME_RDMA_Q_CONNECTED
= (1 << 0),
85 NVME_RDMA_IB_QUEUE_ALLOCATED
= (1 << 1),
86 NVME_RDMA_Q_DELETING
= (1 << 2),
89 struct nvme_rdma_queue
{
90 struct nvme_rdma_qe
*rsp_ring
;
93 size_t cmnd_capsule_len
;
94 struct nvme_rdma_ctrl
*ctrl
;
95 struct nvme_rdma_device
*device
;
100 struct rdma_cm_id
*cm_id
;
102 struct completion cm_done
;
105 struct nvme_rdma_ctrl
{
106 /* read and written in the hot path */
109 /* read only in the hot path */
110 struct nvme_rdma_queue
*queues
;
113 /* other member variables */
114 struct blk_mq_tag_set tag_set
;
115 struct work_struct delete_work
;
116 struct work_struct reset_work
;
117 struct work_struct err_work
;
119 struct nvme_rdma_qe async_event_sqe
;
122 struct delayed_work reconnect_work
;
124 struct list_head list
;
126 struct blk_mq_tag_set admin_tag_set
;
127 struct nvme_rdma_device
*device
;
133 struct sockaddr addr
;
134 struct sockaddr_in addr_in
;
137 struct nvme_ctrl ctrl
;
140 static inline struct nvme_rdma_ctrl
*to_rdma_ctrl(struct nvme_ctrl
*ctrl
)
142 return container_of(ctrl
, struct nvme_rdma_ctrl
, ctrl
);
145 static LIST_HEAD(device_list
);
146 static DEFINE_MUTEX(device_list_mutex
);
148 static LIST_HEAD(nvme_rdma_ctrl_list
);
149 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex
);
151 static struct workqueue_struct
*nvme_rdma_wq
;
154 * Disabling this option makes small I/O goes faster, but is fundamentally
155 * unsafe. With it turned off we will have to register a global rkey that
156 * allows read and write access to all physical memory.
158 static bool register_always
= true;
159 module_param(register_always
, bool, 0444);
160 MODULE_PARM_DESC(register_always
,
161 "Use memory registration even for contiguous memory regions");
163 static int nvme_rdma_cm_handler(struct rdma_cm_id
*cm_id
,
164 struct rdma_cm_event
*event
);
165 static void nvme_rdma_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
);
167 /* XXX: really should move to a generic header sooner or later.. */
168 static inline void put_unaligned_le24(u32 val
, u8
*p
)
175 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue
*queue
)
177 return queue
- queue
->ctrl
->queues
;
180 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue
*queue
)
182 return queue
->cmnd_capsule_len
- sizeof(struct nvme_command
);
185 static void nvme_rdma_free_qe(struct ib_device
*ibdev
, struct nvme_rdma_qe
*qe
,
186 size_t capsule_size
, enum dma_data_direction dir
)
188 ib_dma_unmap_single(ibdev
, qe
->dma
, capsule_size
, dir
);
192 static int nvme_rdma_alloc_qe(struct ib_device
*ibdev
, struct nvme_rdma_qe
*qe
,
193 size_t capsule_size
, enum dma_data_direction dir
)
195 qe
->data
= kzalloc(capsule_size
, GFP_KERNEL
);
199 qe
->dma
= ib_dma_map_single(ibdev
, qe
->data
, capsule_size
, dir
);
200 if (ib_dma_mapping_error(ibdev
, qe
->dma
)) {
208 static void nvme_rdma_free_ring(struct ib_device
*ibdev
,
209 struct nvme_rdma_qe
*ring
, size_t ib_queue_size
,
210 size_t capsule_size
, enum dma_data_direction dir
)
214 for (i
= 0; i
< ib_queue_size
; i
++)
215 nvme_rdma_free_qe(ibdev
, &ring
[i
], capsule_size
, dir
);
219 static struct nvme_rdma_qe
*nvme_rdma_alloc_ring(struct ib_device
*ibdev
,
220 size_t ib_queue_size
, size_t capsule_size
,
221 enum dma_data_direction dir
)
223 struct nvme_rdma_qe
*ring
;
226 ring
= kcalloc(ib_queue_size
, sizeof(struct nvme_rdma_qe
), GFP_KERNEL
);
230 for (i
= 0; i
< ib_queue_size
; i
++) {
231 if (nvme_rdma_alloc_qe(ibdev
, &ring
[i
], capsule_size
, dir
))
238 nvme_rdma_free_ring(ibdev
, ring
, i
, capsule_size
, dir
);
242 static void nvme_rdma_qp_event(struct ib_event
*event
, void *context
)
244 pr_debug("QP event %d\n", event
->event
);
247 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue
*queue
)
249 wait_for_completion_interruptible_timeout(&queue
->cm_done
,
250 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS
) + 1);
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 + NVME_RDMA_MAX_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 int nvme_rdma_reinit_request(void *data
, struct request
*rq
)
281 struct nvme_rdma_ctrl
*ctrl
= data
;
282 struct nvme_rdma_device
*dev
= ctrl
->device
;
283 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
286 if (!req
->mr
->need_inval
)
289 ib_dereg_mr(req
->mr
);
291 req
->mr
= ib_alloc_mr(dev
->pd
, IB_MR_TYPE_MEM_REG
,
293 if (IS_ERR(req
->mr
)) {
294 ret
= PTR_ERR(req
->mr
);
299 req
->mr
->need_inval
= false;
305 static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl
*ctrl
,
306 struct request
*rq
, unsigned int queue_idx
)
308 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
309 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[queue_idx
];
310 struct nvme_rdma_device
*dev
= queue
->device
;
313 ib_dereg_mr(req
->mr
);
315 nvme_rdma_free_qe(dev
->dev
, &req
->sqe
, sizeof(struct nvme_command
),
319 static void nvme_rdma_exit_request(void *data
, struct request
*rq
,
320 unsigned int hctx_idx
, unsigned int rq_idx
)
322 return __nvme_rdma_exit_request(data
, rq
, hctx_idx
+ 1);
325 static void nvme_rdma_exit_admin_request(void *data
, struct request
*rq
,
326 unsigned int hctx_idx
, unsigned int rq_idx
)
328 return __nvme_rdma_exit_request(data
, rq
, 0);
331 static int __nvme_rdma_init_request(struct nvme_rdma_ctrl
*ctrl
,
332 struct request
*rq
, unsigned int queue_idx
)
334 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
335 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[queue_idx
];
336 struct nvme_rdma_device
*dev
= queue
->device
;
337 struct ib_device
*ibdev
= dev
->dev
;
340 BUG_ON(queue_idx
>= ctrl
->queue_count
);
342 ret
= nvme_rdma_alloc_qe(ibdev
, &req
->sqe
, sizeof(struct nvme_command
),
347 req
->mr
= ib_alloc_mr(dev
->pd
, IB_MR_TYPE_MEM_REG
,
349 if (IS_ERR(req
->mr
)) {
350 ret
= PTR_ERR(req
->mr
);
359 nvme_rdma_free_qe(dev
->dev
, &req
->sqe
, sizeof(struct nvme_command
),
364 static int nvme_rdma_init_request(void *data
, struct request
*rq
,
365 unsigned int hctx_idx
, unsigned int rq_idx
,
366 unsigned int numa_node
)
368 return __nvme_rdma_init_request(data
, rq
, hctx_idx
+ 1);
371 static int nvme_rdma_init_admin_request(void *data
, struct request
*rq
,
372 unsigned int hctx_idx
, unsigned int rq_idx
,
373 unsigned int numa_node
)
375 return __nvme_rdma_init_request(data
, rq
, 0);
378 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
379 unsigned int hctx_idx
)
381 struct nvme_rdma_ctrl
*ctrl
= data
;
382 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[hctx_idx
+ 1];
384 BUG_ON(hctx_idx
>= ctrl
->queue_count
);
386 hctx
->driver_data
= queue
;
390 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
391 unsigned int hctx_idx
)
393 struct nvme_rdma_ctrl
*ctrl
= data
;
394 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[0];
396 BUG_ON(hctx_idx
!= 0);
398 hctx
->driver_data
= queue
;
402 static void nvme_rdma_free_dev(struct kref
*ref
)
404 struct nvme_rdma_device
*ndev
=
405 container_of(ref
, struct nvme_rdma_device
, ref
);
407 mutex_lock(&device_list_mutex
);
408 list_del(&ndev
->entry
);
409 mutex_unlock(&device_list_mutex
);
411 ib_dealloc_pd(ndev
->pd
);
415 static void nvme_rdma_dev_put(struct nvme_rdma_device
*dev
)
417 kref_put(&dev
->ref
, nvme_rdma_free_dev
);
420 static int nvme_rdma_dev_get(struct nvme_rdma_device
*dev
)
422 return kref_get_unless_zero(&dev
->ref
);
425 static struct nvme_rdma_device
*
426 nvme_rdma_find_get_device(struct rdma_cm_id
*cm_id
)
428 struct nvme_rdma_device
*ndev
;
430 mutex_lock(&device_list_mutex
);
431 list_for_each_entry(ndev
, &device_list
, entry
) {
432 if (ndev
->dev
->node_guid
== cm_id
->device
->node_guid
&&
433 nvme_rdma_dev_get(ndev
))
437 ndev
= kzalloc(sizeof(*ndev
), GFP_KERNEL
);
441 ndev
->dev
= cm_id
->device
;
442 kref_init(&ndev
->ref
);
444 ndev
->pd
= ib_alloc_pd(ndev
->dev
,
445 register_always
? 0 : IB_PD_UNSAFE_GLOBAL_RKEY
);
446 if (IS_ERR(ndev
->pd
))
449 if (!(ndev
->dev
->attrs
.device_cap_flags
&
450 IB_DEVICE_MEM_MGT_EXTENSIONS
)) {
451 dev_err(&ndev
->dev
->dev
,
452 "Memory registrations not supported.\n");
456 list_add(&ndev
->entry
, &device_list
);
458 mutex_unlock(&device_list_mutex
);
462 ib_dealloc_pd(ndev
->pd
);
466 mutex_unlock(&device_list_mutex
);
470 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue
*queue
)
472 struct nvme_rdma_device
*dev
;
473 struct ib_device
*ibdev
;
475 if (!test_and_clear_bit(NVME_RDMA_IB_QUEUE_ALLOCATED
, &queue
->flags
))
480 rdma_destroy_qp(queue
->cm_id
);
481 ib_free_cq(queue
->ib_cq
);
483 nvme_rdma_free_ring(ibdev
, queue
->rsp_ring
, queue
->queue_size
,
484 sizeof(struct nvme_completion
), DMA_FROM_DEVICE
);
486 nvme_rdma_dev_put(dev
);
489 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue
*queue
,
490 struct nvme_rdma_device
*dev
)
492 struct ib_device
*ibdev
= dev
->dev
;
493 const int send_wr_factor
= 3; /* MR, SEND, INV */
494 const int cq_factor
= send_wr_factor
+ 1; /* + RECV */
495 int comp_vector
, idx
= nvme_rdma_queue_idx(queue
);
502 * The admin queue is barely used once the controller is live, so don't
503 * bother to spread it out.
508 comp_vector
= idx
% ibdev
->num_comp_vectors
;
511 /* +1 for ib_stop_cq */
512 queue
->ib_cq
= ib_alloc_cq(dev
->dev
, queue
,
513 cq_factor
* queue
->queue_size
+ 1, comp_vector
,
515 if (IS_ERR(queue
->ib_cq
)) {
516 ret
= PTR_ERR(queue
->ib_cq
);
520 ret
= nvme_rdma_create_qp(queue
, send_wr_factor
);
522 goto out_destroy_ib_cq
;
524 queue
->rsp_ring
= nvme_rdma_alloc_ring(ibdev
, queue
->queue_size
,
525 sizeof(struct nvme_completion
), DMA_FROM_DEVICE
);
526 if (!queue
->rsp_ring
) {
530 set_bit(NVME_RDMA_IB_QUEUE_ALLOCATED
, &queue
->flags
);
535 ib_destroy_qp(queue
->qp
);
537 ib_free_cq(queue
->ib_cq
);
542 static int nvme_rdma_init_queue(struct nvme_rdma_ctrl
*ctrl
,
543 int idx
, size_t queue_size
)
545 struct nvme_rdma_queue
*queue
;
548 queue
= &ctrl
->queues
[idx
];
550 init_completion(&queue
->cm_done
);
553 queue
->cmnd_capsule_len
= ctrl
->ctrl
.ioccsz
* 16;
555 queue
->cmnd_capsule_len
= sizeof(struct nvme_command
);
557 queue
->queue_size
= queue_size
;
559 queue
->cm_id
= rdma_create_id(&init_net
, nvme_rdma_cm_handler
, queue
,
560 RDMA_PS_TCP
, IB_QPT_RC
);
561 if (IS_ERR(queue
->cm_id
)) {
562 dev_info(ctrl
->ctrl
.device
,
563 "failed to create CM ID: %ld\n", PTR_ERR(queue
->cm_id
));
564 return PTR_ERR(queue
->cm_id
);
567 queue
->cm_error
= -ETIMEDOUT
;
568 ret
= rdma_resolve_addr(queue
->cm_id
, NULL
, &ctrl
->addr
,
569 NVME_RDMA_CONNECT_TIMEOUT_MS
);
571 dev_info(ctrl
->ctrl
.device
,
572 "rdma_resolve_addr failed (%d).\n", ret
);
573 goto out_destroy_cm_id
;
576 ret
= nvme_rdma_wait_for_cm(queue
);
578 dev_info(ctrl
->ctrl
.device
,
579 "rdma_resolve_addr wait failed (%d).\n", ret
);
580 goto out_destroy_cm_id
;
583 clear_bit(NVME_RDMA_Q_DELETING
, &queue
->flags
);
584 set_bit(NVME_RDMA_Q_CONNECTED
, &queue
->flags
);
589 nvme_rdma_destroy_queue_ib(queue
);
590 rdma_destroy_id(queue
->cm_id
);
594 static void nvme_rdma_stop_queue(struct nvme_rdma_queue
*queue
)
596 rdma_disconnect(queue
->cm_id
);
597 ib_drain_qp(queue
->qp
);
600 static void nvme_rdma_free_queue(struct nvme_rdma_queue
*queue
)
602 nvme_rdma_destroy_queue_ib(queue
);
603 rdma_destroy_id(queue
->cm_id
);
606 static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue
*queue
)
608 if (test_and_set_bit(NVME_RDMA_Q_DELETING
, &queue
->flags
))
610 nvme_rdma_stop_queue(queue
);
611 nvme_rdma_free_queue(queue
);
614 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl
*ctrl
)
618 for (i
= 1; i
< ctrl
->queue_count
; i
++)
619 nvme_rdma_stop_and_free_queue(&ctrl
->queues
[i
]);
622 static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl
*ctrl
)
626 for (i
= 1; i
< ctrl
->queue_count
; i
++) {
627 ret
= nvmf_connect_io_queue(&ctrl
->ctrl
, i
);
635 static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl
*ctrl
)
639 for (i
= 1; i
< ctrl
->queue_count
; i
++) {
640 ret
= nvme_rdma_init_queue(ctrl
, i
,
641 ctrl
->ctrl
.opts
->queue_size
);
643 dev_info(ctrl
->ctrl
.device
,
644 "failed to initialize i/o queue: %d\n", ret
);
645 goto out_free_queues
;
652 for (i
--; i
>= 1; i
--)
653 nvme_rdma_stop_and_free_queue(&ctrl
->queues
[i
]);
658 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl
*ctrl
)
660 nvme_rdma_free_qe(ctrl
->queues
[0].device
->dev
, &ctrl
->async_event_sqe
,
661 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
662 nvme_rdma_stop_and_free_queue(&ctrl
->queues
[0]);
663 blk_cleanup_queue(ctrl
->ctrl
.admin_q
);
664 blk_mq_free_tag_set(&ctrl
->admin_tag_set
);
665 nvme_rdma_dev_put(ctrl
->device
);
668 static void nvme_rdma_free_ctrl(struct nvme_ctrl
*nctrl
)
670 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(nctrl
);
672 if (list_empty(&ctrl
->list
))
675 mutex_lock(&nvme_rdma_ctrl_mutex
);
676 list_del(&ctrl
->list
);
677 mutex_unlock(&nvme_rdma_ctrl_mutex
);
680 nvmf_free_options(nctrl
->opts
);
685 static void nvme_rdma_reconnect_ctrl_work(struct work_struct
*work
)
687 struct nvme_rdma_ctrl
*ctrl
= container_of(to_delayed_work(work
),
688 struct nvme_rdma_ctrl
, reconnect_work
);
692 if (ctrl
->queue_count
> 1) {
693 nvme_rdma_free_io_queues(ctrl
);
695 ret
= blk_mq_reinit_tagset(&ctrl
->tag_set
);
700 nvme_rdma_stop_and_free_queue(&ctrl
->queues
[0]);
702 ret
= blk_mq_reinit_tagset(&ctrl
->admin_tag_set
);
706 ret
= nvme_rdma_init_queue(ctrl
, 0, NVMF_AQ_DEPTH
);
710 blk_mq_start_stopped_hw_queues(ctrl
->ctrl
.admin_q
, true);
712 ret
= nvmf_connect_admin_queue(&ctrl
->ctrl
);
716 ret
= nvme_enable_ctrl(&ctrl
->ctrl
, ctrl
->cap
);
720 nvme_start_keep_alive(&ctrl
->ctrl
);
722 if (ctrl
->queue_count
> 1) {
723 ret
= nvme_rdma_init_io_queues(ctrl
);
727 ret
= nvme_rdma_connect_io_queues(ctrl
);
732 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_LIVE
);
733 WARN_ON_ONCE(!changed
);
735 if (ctrl
->queue_count
> 1) {
736 nvme_start_queues(&ctrl
->ctrl
);
737 nvme_queue_scan(&ctrl
->ctrl
);
738 nvme_queue_async_events(&ctrl
->ctrl
);
741 dev_info(ctrl
->ctrl
.device
, "Successfully reconnected\n");
746 blk_mq_stop_hw_queues(ctrl
->ctrl
.admin_q
);
748 /* Make sure we are not resetting/deleting */
749 if (ctrl
->ctrl
.state
== NVME_CTRL_RECONNECTING
) {
750 dev_info(ctrl
->ctrl
.device
,
751 "Failed reconnect attempt, requeueing...\n");
752 queue_delayed_work(nvme_rdma_wq
, &ctrl
->reconnect_work
,
753 ctrl
->reconnect_delay
* HZ
);
757 static void nvme_rdma_error_recovery_work(struct work_struct
*work
)
759 struct nvme_rdma_ctrl
*ctrl
= container_of(work
,
760 struct nvme_rdma_ctrl
, err_work
);
763 nvme_stop_keep_alive(&ctrl
->ctrl
);
765 for (i
= 0; i
< ctrl
->queue_count
; i
++)
766 clear_bit(NVME_RDMA_Q_CONNECTED
, &ctrl
->queues
[i
].flags
);
768 if (ctrl
->queue_count
> 1)
769 nvme_stop_queues(&ctrl
->ctrl
);
770 blk_mq_stop_hw_queues(ctrl
->ctrl
.admin_q
);
772 /* We must take care of fastfail/requeue all our inflight requests */
773 if (ctrl
->queue_count
> 1)
774 blk_mq_tagset_busy_iter(&ctrl
->tag_set
,
775 nvme_cancel_request
, &ctrl
->ctrl
);
776 blk_mq_tagset_busy_iter(&ctrl
->admin_tag_set
,
777 nvme_cancel_request
, &ctrl
->ctrl
);
779 dev_info(ctrl
->ctrl
.device
, "reconnecting in %d seconds\n",
780 ctrl
->reconnect_delay
);
782 queue_delayed_work(nvme_rdma_wq
, &ctrl
->reconnect_work
,
783 ctrl
->reconnect_delay
* HZ
);
786 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl
*ctrl
)
788 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_RECONNECTING
))
791 queue_work(nvme_rdma_wq
, &ctrl
->err_work
);
794 static void nvme_rdma_wr_error(struct ib_cq
*cq
, struct ib_wc
*wc
,
797 struct nvme_rdma_queue
*queue
= cq
->cq_context
;
798 struct nvme_rdma_ctrl
*ctrl
= queue
->ctrl
;
800 if (ctrl
->ctrl
.state
== NVME_CTRL_LIVE
)
801 dev_info(ctrl
->ctrl
.device
,
802 "%s for CQE 0x%p failed with status %s (%d)\n",
804 ib_wc_status_msg(wc
->status
), wc
->status
);
805 nvme_rdma_error_recovery(ctrl
);
808 static void nvme_rdma_memreg_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
810 if (unlikely(wc
->status
!= IB_WC_SUCCESS
))
811 nvme_rdma_wr_error(cq
, wc
, "MEMREG");
814 static void nvme_rdma_inv_rkey_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
816 if (unlikely(wc
->status
!= IB_WC_SUCCESS
))
817 nvme_rdma_wr_error(cq
, wc
, "LOCAL_INV");
820 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue
*queue
,
821 struct nvme_rdma_request
*req
)
823 struct ib_send_wr
*bad_wr
;
824 struct ib_send_wr wr
= {
825 .opcode
= IB_WR_LOCAL_INV
,
829 .ex
.invalidate_rkey
= req
->mr
->rkey
,
832 req
->reg_cqe
.done
= nvme_rdma_inv_rkey_done
;
833 wr
.wr_cqe
= &req
->reg_cqe
;
835 return ib_post_send(queue
->qp
, &wr
, &bad_wr
);
838 static void nvme_rdma_unmap_data(struct nvme_rdma_queue
*queue
,
841 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
842 struct nvme_rdma_ctrl
*ctrl
= queue
->ctrl
;
843 struct nvme_rdma_device
*dev
= queue
->device
;
844 struct ib_device
*ibdev
= dev
->dev
;
847 if (!blk_rq_bytes(rq
))
850 if (req
->mr
->need_inval
) {
851 res
= nvme_rdma_inv_rkey(queue
, req
);
853 dev_err(ctrl
->ctrl
.device
,
854 "Queueing INV WR for rkey %#x failed (%d)\n",
856 nvme_rdma_error_recovery(queue
->ctrl
);
860 ib_dma_unmap_sg(ibdev
, req
->sg_table
.sgl
,
861 req
->nents
, rq_data_dir(rq
) ==
862 WRITE
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
864 nvme_cleanup_cmd(rq
);
865 sg_free_table_chained(&req
->sg_table
, true);
868 static int nvme_rdma_set_sg_null(struct nvme_command
*c
)
870 struct nvme_keyed_sgl_desc
*sg
= &c
->common
.dptr
.ksgl
;
873 put_unaligned_le24(0, sg
->length
);
874 put_unaligned_le32(0, sg
->key
);
875 sg
->type
= NVME_KEY_SGL_FMT_DATA_DESC
<< 4;
879 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue
*queue
,
880 struct nvme_rdma_request
*req
, struct nvme_command
*c
)
882 struct nvme_sgl_desc
*sg
= &c
->common
.dptr
.sgl
;
884 req
->sge
[1].addr
= sg_dma_address(req
->sg_table
.sgl
);
885 req
->sge
[1].length
= sg_dma_len(req
->sg_table
.sgl
);
886 req
->sge
[1].lkey
= queue
->device
->pd
->local_dma_lkey
;
888 sg
->addr
= cpu_to_le64(queue
->ctrl
->ctrl
.icdoff
);
889 sg
->length
= cpu_to_le32(sg_dma_len(req
->sg_table
.sgl
));
890 sg
->type
= (NVME_SGL_FMT_DATA_DESC
<< 4) | NVME_SGL_FMT_OFFSET
;
892 req
->inline_data
= true;
897 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue
*queue
,
898 struct nvme_rdma_request
*req
, struct nvme_command
*c
)
900 struct nvme_keyed_sgl_desc
*sg
= &c
->common
.dptr
.ksgl
;
902 sg
->addr
= cpu_to_le64(sg_dma_address(req
->sg_table
.sgl
));
903 put_unaligned_le24(sg_dma_len(req
->sg_table
.sgl
), sg
->length
);
904 put_unaligned_le32(queue
->device
->pd
->unsafe_global_rkey
, sg
->key
);
905 sg
->type
= NVME_KEY_SGL_FMT_DATA_DESC
<< 4;
909 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue
*queue
,
910 struct nvme_rdma_request
*req
, struct nvme_command
*c
,
913 struct nvme_keyed_sgl_desc
*sg
= &c
->common
.dptr
.ksgl
;
916 nr
= ib_map_mr_sg(req
->mr
, req
->sg_table
.sgl
, count
, NULL
, PAGE_SIZE
);
923 ib_update_fast_reg_key(req
->mr
, ib_inc_rkey(req
->mr
->rkey
));
925 req
->reg_cqe
.done
= nvme_rdma_memreg_done
;
926 memset(&req
->reg_wr
, 0, sizeof(req
->reg_wr
));
927 req
->reg_wr
.wr
.opcode
= IB_WR_REG_MR
;
928 req
->reg_wr
.wr
.wr_cqe
= &req
->reg_cqe
;
929 req
->reg_wr
.wr
.num_sge
= 0;
930 req
->reg_wr
.mr
= req
->mr
;
931 req
->reg_wr
.key
= req
->mr
->rkey
;
932 req
->reg_wr
.access
= IB_ACCESS_LOCAL_WRITE
|
933 IB_ACCESS_REMOTE_READ
|
934 IB_ACCESS_REMOTE_WRITE
;
936 req
->mr
->need_inval
= true;
938 sg
->addr
= cpu_to_le64(req
->mr
->iova
);
939 put_unaligned_le24(req
->mr
->length
, sg
->length
);
940 put_unaligned_le32(req
->mr
->rkey
, sg
->key
);
941 sg
->type
= (NVME_KEY_SGL_FMT_DATA_DESC
<< 4) |
942 NVME_SGL_FMT_INVALIDATE
;
947 static int nvme_rdma_map_data(struct nvme_rdma_queue
*queue
,
948 struct request
*rq
, unsigned int map_len
,
949 struct nvme_command
*c
)
951 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
952 struct nvme_rdma_device
*dev
= queue
->device
;
953 struct ib_device
*ibdev
= dev
->dev
;
958 req
->inline_data
= false;
959 req
->mr
->need_inval
= false;
961 c
->common
.flags
|= NVME_CMD_SGL_METABUF
;
963 if (!blk_rq_bytes(rq
))
964 return nvme_rdma_set_sg_null(c
);
966 req
->sg_table
.sgl
= req
->first_sgl
;
967 ret
= sg_alloc_table_chained(&req
->sg_table
, rq
->nr_phys_segments
,
972 nents
= blk_rq_map_sg(rq
->q
, rq
, req
->sg_table
.sgl
);
973 BUG_ON(nents
> rq
->nr_phys_segments
);
976 count
= ib_dma_map_sg(ibdev
, req
->sg_table
.sgl
, nents
,
977 rq_data_dir(rq
) == WRITE
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
978 if (unlikely(count
<= 0)) {
979 sg_free_table_chained(&req
->sg_table
, true);
984 if (rq_data_dir(rq
) == WRITE
&&
985 map_len
<= nvme_rdma_inline_data_size(queue
) &&
986 nvme_rdma_queue_idx(queue
))
987 return nvme_rdma_map_sg_inline(queue
, req
, c
);
989 if (dev
->pd
->flags
& IB_PD_UNSAFE_GLOBAL_RKEY
)
990 return nvme_rdma_map_sg_single(queue
, req
, c
);
993 return nvme_rdma_map_sg_fr(queue
, req
, c
, count
);
996 static void nvme_rdma_send_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
998 if (unlikely(wc
->status
!= IB_WC_SUCCESS
))
999 nvme_rdma_wr_error(cq
, wc
, "SEND");
1002 static int nvme_rdma_post_send(struct nvme_rdma_queue
*queue
,
1003 struct nvme_rdma_qe
*qe
, struct ib_sge
*sge
, u32 num_sge
,
1004 struct ib_send_wr
*first
, bool flush
)
1006 struct ib_send_wr wr
, *bad_wr
;
1009 sge
->addr
= qe
->dma
;
1010 sge
->length
= sizeof(struct nvme_command
),
1011 sge
->lkey
= queue
->device
->pd
->local_dma_lkey
;
1013 qe
->cqe
.done
= nvme_rdma_send_done
;
1016 wr
.wr_cqe
= &qe
->cqe
;
1018 wr
.num_sge
= num_sge
;
1019 wr
.opcode
= IB_WR_SEND
;
1023 * Unsignalled send completions are another giant desaster in the
1024 * IB Verbs spec: If we don't regularly post signalled sends
1025 * the send queue will fill up and only a QP reset will rescue us.
1026 * Would have been way to obvious to handle this in hardware or
1027 * at least the RDMA stack..
1029 * This messy and racy code sniplet is copy and pasted from the iSER
1030 * initiator, and the magic '32' comes from there as well.
1032 * Always signal the flushes. The magic request used for the flush
1033 * sequencer is not allocated in our driver's tagset and it's
1034 * triggered to be freed by blk_cleanup_queue(). So we need to
1035 * always mark it as signaled to ensure that the "wr_cqe", which is
1036 * embeded in request's payload, is not freed when __ib_process_cq()
1037 * calls wr_cqe->done().
1039 if ((++queue
->sig_count
% 32) == 0 || flush
)
1040 wr
.send_flags
|= IB_SEND_SIGNALED
;
1047 ret
= ib_post_send(queue
->qp
, first
, &bad_wr
);
1049 dev_err(queue
->ctrl
->ctrl
.device
,
1050 "%s failed with error code %d\n", __func__
, ret
);
1055 static int nvme_rdma_post_recv(struct nvme_rdma_queue
*queue
,
1056 struct nvme_rdma_qe
*qe
)
1058 struct ib_recv_wr wr
, *bad_wr
;
1062 list
.addr
= qe
->dma
;
1063 list
.length
= sizeof(struct nvme_completion
);
1064 list
.lkey
= queue
->device
->pd
->local_dma_lkey
;
1066 qe
->cqe
.done
= nvme_rdma_recv_done
;
1069 wr
.wr_cqe
= &qe
->cqe
;
1073 ret
= ib_post_recv(queue
->qp
, &wr
, &bad_wr
);
1075 dev_err(queue
->ctrl
->ctrl
.device
,
1076 "%s failed with error code %d\n", __func__
, ret
);
1081 static struct blk_mq_tags
*nvme_rdma_tagset(struct nvme_rdma_queue
*queue
)
1083 u32 queue_idx
= nvme_rdma_queue_idx(queue
);
1086 return queue
->ctrl
->admin_tag_set
.tags
[queue_idx
];
1087 return queue
->ctrl
->tag_set
.tags
[queue_idx
- 1];
1090 static void nvme_rdma_submit_async_event(struct nvme_ctrl
*arg
, int aer_idx
)
1092 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(arg
);
1093 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[0];
1094 struct ib_device
*dev
= queue
->device
->dev
;
1095 struct nvme_rdma_qe
*sqe
= &ctrl
->async_event_sqe
;
1096 struct nvme_command
*cmd
= sqe
->data
;
1100 if (WARN_ON_ONCE(aer_idx
!= 0))
1103 ib_dma_sync_single_for_cpu(dev
, sqe
->dma
, sizeof(*cmd
), DMA_TO_DEVICE
);
1105 memset(cmd
, 0, sizeof(*cmd
));
1106 cmd
->common
.opcode
= nvme_admin_async_event
;
1107 cmd
->common
.command_id
= NVME_RDMA_AQ_BLKMQ_DEPTH
;
1108 cmd
->common
.flags
|= NVME_CMD_SGL_METABUF
;
1109 nvme_rdma_set_sg_null(cmd
);
1111 ib_dma_sync_single_for_device(dev
, sqe
->dma
, sizeof(*cmd
),
1114 ret
= nvme_rdma_post_send(queue
, sqe
, &sge
, 1, NULL
, false);
1118 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue
*queue
,
1119 struct nvme_completion
*cqe
, struct ib_wc
*wc
, int tag
)
1122 struct nvme_rdma_request
*req
;
1125 rq
= blk_mq_tag_to_rq(nvme_rdma_tagset(queue
), cqe
->command_id
);
1127 dev_err(queue
->ctrl
->ctrl
.device
,
1128 "tag 0x%x on QP %#x not found\n",
1129 cqe
->command_id
, queue
->qp
->qp_num
);
1130 nvme_rdma_error_recovery(queue
->ctrl
);
1133 req
= blk_mq_rq_to_pdu(rq
);
1138 if ((wc
->wc_flags
& IB_WC_WITH_INVALIDATE
) &&
1139 wc
->ex
.invalidate_rkey
== req
->mr
->rkey
)
1140 req
->mr
->need_inval
= false;
1142 req
->req
.result
= cqe
->result
;
1143 blk_mq_complete_request(rq
, le16_to_cpu(cqe
->status
) >> 1);
1147 static int __nvme_rdma_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
, int tag
)
1149 struct nvme_rdma_qe
*qe
=
1150 container_of(wc
->wr_cqe
, struct nvme_rdma_qe
, cqe
);
1151 struct nvme_rdma_queue
*queue
= cq
->cq_context
;
1152 struct ib_device
*ibdev
= queue
->device
->dev
;
1153 struct nvme_completion
*cqe
= qe
->data
;
1154 const size_t len
= sizeof(struct nvme_completion
);
1157 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
1158 nvme_rdma_wr_error(cq
, wc
, "RECV");
1162 ib_dma_sync_single_for_cpu(ibdev
, qe
->dma
, len
, DMA_FROM_DEVICE
);
1164 * AEN requests are special as they don't time out and can
1165 * survive any kind of queue freeze and often don't respond to
1166 * aborts. We don't even bother to allocate a struct request
1167 * for them but rather special case them here.
1169 if (unlikely(nvme_rdma_queue_idx(queue
) == 0 &&
1170 cqe
->command_id
>= NVME_RDMA_AQ_BLKMQ_DEPTH
))
1171 nvme_complete_async_event(&queue
->ctrl
->ctrl
, cqe
);
1173 ret
= nvme_rdma_process_nvme_rsp(queue
, cqe
, wc
, tag
);
1174 ib_dma_sync_single_for_device(ibdev
, qe
->dma
, len
, DMA_FROM_DEVICE
);
1176 nvme_rdma_post_recv(queue
, qe
);
1180 static void nvme_rdma_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
1182 __nvme_rdma_recv_done(cq
, wc
, -1);
1185 static int nvme_rdma_conn_established(struct nvme_rdma_queue
*queue
)
1189 for (i
= 0; i
< queue
->queue_size
; i
++) {
1190 ret
= nvme_rdma_post_recv(queue
, &queue
->rsp_ring
[i
]);
1192 goto out_destroy_queue_ib
;
1197 out_destroy_queue_ib
:
1198 nvme_rdma_destroy_queue_ib(queue
);
1202 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue
*queue
,
1203 struct rdma_cm_event
*ev
)
1205 if (ev
->param
.conn
.private_data_len
) {
1206 struct nvme_rdma_cm_rej
*rej
=
1207 (struct nvme_rdma_cm_rej
*)ev
->param
.conn
.private_data
;
1209 dev_err(queue
->ctrl
->ctrl
.device
,
1210 "Connect rejected, status %d.", le16_to_cpu(rej
->sts
));
1211 /* XXX: Think of something clever to do here... */
1213 dev_err(queue
->ctrl
->ctrl
.device
,
1214 "Connect rejected, no private data.\n");
1220 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue
*queue
)
1222 struct nvme_rdma_device
*dev
;
1225 dev
= nvme_rdma_find_get_device(queue
->cm_id
);
1227 dev_err(queue
->cm_id
->device
->dma_device
,
1228 "no client data found!\n");
1229 return -ECONNREFUSED
;
1232 ret
= nvme_rdma_create_queue_ib(queue
, dev
);
1234 nvme_rdma_dev_put(dev
);
1238 ret
= rdma_resolve_route(queue
->cm_id
, NVME_RDMA_CONNECT_TIMEOUT_MS
);
1240 dev_err(queue
->ctrl
->ctrl
.device
,
1241 "rdma_resolve_route failed (%d).\n",
1243 goto out_destroy_queue
;
1249 nvme_rdma_destroy_queue_ib(queue
);
1254 static int nvme_rdma_route_resolved(struct nvme_rdma_queue
*queue
)
1256 struct nvme_rdma_ctrl
*ctrl
= queue
->ctrl
;
1257 struct rdma_conn_param param
= { };
1258 struct nvme_rdma_cm_req priv
= { };
1261 param
.qp_num
= queue
->qp
->qp_num
;
1262 param
.flow_control
= 1;
1264 param
.responder_resources
= queue
->device
->dev
->attrs
.max_qp_rd_atom
;
1265 /* maximum retry count */
1266 param
.retry_count
= 7;
1267 param
.rnr_retry_count
= 7;
1268 param
.private_data
= &priv
;
1269 param
.private_data_len
= sizeof(priv
);
1271 priv
.recfmt
= cpu_to_le16(NVME_RDMA_CM_FMT_1_0
);
1272 priv
.qid
= cpu_to_le16(nvme_rdma_queue_idx(queue
));
1274 * set the admin queue depth to the minimum size
1275 * specified by the Fabrics standard.
1277 if (priv
.qid
== 0) {
1278 priv
.hrqsize
= cpu_to_le16(NVMF_AQ_DEPTH
);
1279 priv
.hsqsize
= cpu_to_le16(NVMF_AQ_DEPTH
- 1);
1282 * current interpretation of the fabrics spec
1283 * is at minimum you make hrqsize sqsize+1, or a
1284 * 1's based representation of sqsize.
1286 priv
.hrqsize
= cpu_to_le16(queue
->queue_size
);
1287 priv
.hsqsize
= cpu_to_le16(queue
->ctrl
->ctrl
.sqsize
);
1290 ret
= rdma_connect(queue
->cm_id
, ¶m
);
1292 dev_err(ctrl
->ctrl
.device
,
1293 "rdma_connect failed (%d).\n", ret
);
1294 goto out_destroy_queue_ib
;
1299 out_destroy_queue_ib
:
1300 nvme_rdma_destroy_queue_ib(queue
);
1304 static int nvme_rdma_cm_handler(struct rdma_cm_id
*cm_id
,
1305 struct rdma_cm_event
*ev
)
1307 struct nvme_rdma_queue
*queue
= cm_id
->context
;
1310 dev_dbg(queue
->ctrl
->ctrl
.device
, "%s (%d): status %d id %p\n",
1311 rdma_event_msg(ev
->event
), ev
->event
,
1314 switch (ev
->event
) {
1315 case RDMA_CM_EVENT_ADDR_RESOLVED
:
1316 cm_error
= nvme_rdma_addr_resolved(queue
);
1318 case RDMA_CM_EVENT_ROUTE_RESOLVED
:
1319 cm_error
= nvme_rdma_route_resolved(queue
);
1321 case RDMA_CM_EVENT_ESTABLISHED
:
1322 queue
->cm_error
= nvme_rdma_conn_established(queue
);
1323 /* complete cm_done regardless of success/failure */
1324 complete(&queue
->cm_done
);
1326 case RDMA_CM_EVENT_REJECTED
:
1327 cm_error
= nvme_rdma_conn_rejected(queue
, ev
);
1329 case RDMA_CM_EVENT_ADDR_ERROR
:
1330 case RDMA_CM_EVENT_ROUTE_ERROR
:
1331 case RDMA_CM_EVENT_CONNECT_ERROR
:
1332 case RDMA_CM_EVENT_UNREACHABLE
:
1333 dev_dbg(queue
->ctrl
->ctrl
.device
,
1334 "CM error event %d\n", ev
->event
);
1335 cm_error
= -ECONNRESET
;
1337 case RDMA_CM_EVENT_DISCONNECTED
:
1338 case RDMA_CM_EVENT_ADDR_CHANGE
:
1339 case RDMA_CM_EVENT_TIMEWAIT_EXIT
:
1340 dev_dbg(queue
->ctrl
->ctrl
.device
,
1341 "disconnect received - connection closed\n");
1342 nvme_rdma_error_recovery(queue
->ctrl
);
1344 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
1345 /* device removal is handled via the ib_client API */
1348 dev_err(queue
->ctrl
->ctrl
.device
,
1349 "Unexpected RDMA CM event (%d)\n", ev
->event
);
1350 nvme_rdma_error_recovery(queue
->ctrl
);
1355 queue
->cm_error
= cm_error
;
1356 complete(&queue
->cm_done
);
1362 static enum blk_eh_timer_return
1363 nvme_rdma_timeout(struct request
*rq
, bool reserved
)
1365 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1367 /* queue error recovery */
1368 nvme_rdma_error_recovery(req
->queue
->ctrl
);
1370 /* fail with DNR on cmd timeout */
1371 rq
->errors
= NVME_SC_ABORT_REQ
| NVME_SC_DNR
;
1373 return BLK_EH_HANDLED
;
1376 static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx
*hctx
,
1377 const struct blk_mq_queue_data
*bd
)
1379 struct nvme_ns
*ns
= hctx
->queue
->queuedata
;
1380 struct nvme_rdma_queue
*queue
= hctx
->driver_data
;
1381 struct request
*rq
= bd
->rq
;
1382 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1383 struct nvme_rdma_qe
*sqe
= &req
->sqe
;
1384 struct nvme_command
*c
= sqe
->data
;
1386 struct ib_device
*dev
;
1387 unsigned int map_len
;
1390 WARN_ON_ONCE(rq
->tag
< 0);
1392 dev
= queue
->device
->dev
;
1393 ib_dma_sync_single_for_cpu(dev
, sqe
->dma
,
1394 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
1396 ret
= nvme_setup_cmd(ns
, rq
, c
);
1400 c
->common
.command_id
= rq
->tag
;
1401 blk_mq_start_request(rq
);
1403 map_len
= nvme_map_len(rq
);
1404 ret
= nvme_rdma_map_data(queue
, rq
, map_len
, c
);
1406 dev_err(queue
->ctrl
->ctrl
.device
,
1407 "Failed to map data (%d)\n", ret
);
1408 nvme_cleanup_cmd(rq
);
1412 ib_dma_sync_single_for_device(dev
, sqe
->dma
,
1413 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
1415 if (rq
->cmd_type
== REQ_TYPE_FS
&& req_op(rq
) == REQ_OP_FLUSH
)
1417 ret
= nvme_rdma_post_send(queue
, sqe
, req
->sge
, req
->num_sge
,
1418 req
->mr
->need_inval
? &req
->reg_wr
.wr
: NULL
, flush
);
1420 nvme_rdma_unmap_data(queue
, rq
);
1424 return BLK_MQ_RQ_QUEUE_OK
;
1426 return (ret
== -ENOMEM
|| ret
== -EAGAIN
) ?
1427 BLK_MQ_RQ_QUEUE_BUSY
: BLK_MQ_RQ_QUEUE_ERROR
;
1430 static int nvme_rdma_poll(struct blk_mq_hw_ctx
*hctx
, unsigned int tag
)
1432 struct nvme_rdma_queue
*queue
= hctx
->driver_data
;
1433 struct ib_cq
*cq
= queue
->ib_cq
;
1437 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
1438 while (ib_poll_cq(cq
, 1, &wc
) > 0) {
1439 struct ib_cqe
*cqe
= wc
.wr_cqe
;
1442 if (cqe
->done
== nvme_rdma_recv_done
)
1443 found
|= __nvme_rdma_recv_done(cq
, &wc
, tag
);
1452 static void nvme_rdma_complete_rq(struct request
*rq
)
1454 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1455 struct nvme_rdma_queue
*queue
= req
->queue
;
1458 nvme_rdma_unmap_data(queue
, rq
);
1460 if (unlikely(rq
->errors
)) {
1461 if (nvme_req_needs_retry(rq
, rq
->errors
)) {
1462 nvme_requeue_req(rq
);
1466 if (rq
->cmd_type
== REQ_TYPE_DRV_PRIV
)
1469 error
= nvme_error_status(rq
->errors
);
1472 blk_mq_end_request(rq
, error
);
1475 static struct blk_mq_ops nvme_rdma_mq_ops
= {
1476 .queue_rq
= nvme_rdma_queue_rq
,
1477 .complete
= nvme_rdma_complete_rq
,
1478 .init_request
= nvme_rdma_init_request
,
1479 .exit_request
= nvme_rdma_exit_request
,
1480 .reinit_request
= nvme_rdma_reinit_request
,
1481 .init_hctx
= nvme_rdma_init_hctx
,
1482 .poll
= nvme_rdma_poll
,
1483 .timeout
= nvme_rdma_timeout
,
1486 static struct blk_mq_ops nvme_rdma_admin_mq_ops
= {
1487 .queue_rq
= nvme_rdma_queue_rq
,
1488 .complete
= nvme_rdma_complete_rq
,
1489 .init_request
= nvme_rdma_init_admin_request
,
1490 .exit_request
= nvme_rdma_exit_admin_request
,
1491 .reinit_request
= nvme_rdma_reinit_request
,
1492 .init_hctx
= nvme_rdma_init_admin_hctx
,
1493 .timeout
= nvme_rdma_timeout
,
1496 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl
*ctrl
)
1500 error
= nvme_rdma_init_queue(ctrl
, 0, NVMF_AQ_DEPTH
);
1504 ctrl
->device
= ctrl
->queues
[0].device
;
1507 * We need a reference on the device as long as the tag_set is alive,
1508 * as the MRs in the request structures need a valid ib_device.
1511 if (!nvme_rdma_dev_get(ctrl
->device
))
1512 goto out_free_queue
;
1514 ctrl
->max_fr_pages
= min_t(u32
, NVME_RDMA_MAX_SEGMENTS
,
1515 ctrl
->device
->dev
->attrs
.max_fast_reg_page_list_len
);
1517 memset(&ctrl
->admin_tag_set
, 0, sizeof(ctrl
->admin_tag_set
));
1518 ctrl
->admin_tag_set
.ops
= &nvme_rdma_admin_mq_ops
;
1519 ctrl
->admin_tag_set
.queue_depth
= NVME_RDMA_AQ_BLKMQ_DEPTH
;
1520 ctrl
->admin_tag_set
.reserved_tags
= 2; /* connect + keep-alive */
1521 ctrl
->admin_tag_set
.numa_node
= NUMA_NO_NODE
;
1522 ctrl
->admin_tag_set
.cmd_size
= sizeof(struct nvme_rdma_request
) +
1523 SG_CHUNK_SIZE
* sizeof(struct scatterlist
);
1524 ctrl
->admin_tag_set
.driver_data
= ctrl
;
1525 ctrl
->admin_tag_set
.nr_hw_queues
= 1;
1526 ctrl
->admin_tag_set
.timeout
= ADMIN_TIMEOUT
;
1528 error
= blk_mq_alloc_tag_set(&ctrl
->admin_tag_set
);
1532 ctrl
->ctrl
.admin_q
= blk_mq_init_queue(&ctrl
->admin_tag_set
);
1533 if (IS_ERR(ctrl
->ctrl
.admin_q
)) {
1534 error
= PTR_ERR(ctrl
->ctrl
.admin_q
);
1535 goto out_free_tagset
;
1538 error
= nvmf_connect_admin_queue(&ctrl
->ctrl
);
1540 goto out_cleanup_queue
;
1542 error
= nvmf_reg_read64(&ctrl
->ctrl
, NVME_REG_CAP
, &ctrl
->cap
);
1544 dev_err(ctrl
->ctrl
.device
,
1545 "prop_get NVME_REG_CAP failed\n");
1546 goto out_cleanup_queue
;
1550 min_t(int, NVME_CAP_MQES(ctrl
->cap
) + 1, ctrl
->ctrl
.sqsize
);
1552 error
= nvme_enable_ctrl(&ctrl
->ctrl
, ctrl
->cap
);
1554 goto out_cleanup_queue
;
1556 ctrl
->ctrl
.max_hw_sectors
=
1557 (ctrl
->max_fr_pages
- 1) << (PAGE_SHIFT
- 9);
1559 error
= nvme_init_identify(&ctrl
->ctrl
);
1561 goto out_cleanup_queue
;
1563 error
= nvme_rdma_alloc_qe(ctrl
->queues
[0].device
->dev
,
1564 &ctrl
->async_event_sqe
, sizeof(struct nvme_command
),
1567 goto out_cleanup_queue
;
1569 nvme_start_keep_alive(&ctrl
->ctrl
);
1574 blk_cleanup_queue(ctrl
->ctrl
.admin_q
);
1576 /* disconnect and drain the queue before freeing the tagset */
1577 nvme_rdma_stop_queue(&ctrl
->queues
[0]);
1578 blk_mq_free_tag_set(&ctrl
->admin_tag_set
);
1580 nvme_rdma_dev_put(ctrl
->device
);
1582 nvme_rdma_free_queue(&ctrl
->queues
[0]);
1586 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl
*ctrl
)
1588 nvme_stop_keep_alive(&ctrl
->ctrl
);
1589 cancel_work_sync(&ctrl
->err_work
);
1590 cancel_delayed_work_sync(&ctrl
->reconnect_work
);
1592 if (ctrl
->queue_count
> 1) {
1593 nvme_stop_queues(&ctrl
->ctrl
);
1594 blk_mq_tagset_busy_iter(&ctrl
->tag_set
,
1595 nvme_cancel_request
, &ctrl
->ctrl
);
1596 nvme_rdma_free_io_queues(ctrl
);
1599 if (test_bit(NVME_RDMA_Q_CONNECTED
, &ctrl
->queues
[0].flags
))
1600 nvme_shutdown_ctrl(&ctrl
->ctrl
);
1602 blk_mq_stop_hw_queues(ctrl
->ctrl
.admin_q
);
1603 blk_mq_tagset_busy_iter(&ctrl
->admin_tag_set
,
1604 nvme_cancel_request
, &ctrl
->ctrl
);
1605 nvme_rdma_destroy_admin_queue(ctrl
);
1608 static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl
*ctrl
, bool shutdown
)
1610 nvme_uninit_ctrl(&ctrl
->ctrl
);
1612 nvme_rdma_shutdown_ctrl(ctrl
);
1614 if (ctrl
->ctrl
.tagset
) {
1615 blk_cleanup_queue(ctrl
->ctrl
.connect_q
);
1616 blk_mq_free_tag_set(&ctrl
->tag_set
);
1617 nvme_rdma_dev_put(ctrl
->device
);
1620 nvme_put_ctrl(&ctrl
->ctrl
);
1623 static void nvme_rdma_del_ctrl_work(struct work_struct
*work
)
1625 struct nvme_rdma_ctrl
*ctrl
= container_of(work
,
1626 struct nvme_rdma_ctrl
, delete_work
);
1628 __nvme_rdma_remove_ctrl(ctrl
, true);
1631 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl
*ctrl
)
1633 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_DELETING
))
1636 if (!queue_work(nvme_rdma_wq
, &ctrl
->delete_work
))
1642 static int nvme_rdma_del_ctrl(struct nvme_ctrl
*nctrl
)
1644 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(nctrl
);
1648 * Keep a reference until all work is flushed since
1649 * __nvme_rdma_del_ctrl can free the ctrl mem
1651 if (!kref_get_unless_zero(&ctrl
->ctrl
.kref
))
1653 ret
= __nvme_rdma_del_ctrl(ctrl
);
1655 flush_work(&ctrl
->delete_work
);
1656 nvme_put_ctrl(&ctrl
->ctrl
);
1660 static void nvme_rdma_remove_ctrl_work(struct work_struct
*work
)
1662 struct nvme_rdma_ctrl
*ctrl
= container_of(work
,
1663 struct nvme_rdma_ctrl
, delete_work
);
1665 __nvme_rdma_remove_ctrl(ctrl
, false);
1668 static void nvme_rdma_reset_ctrl_work(struct work_struct
*work
)
1670 struct nvme_rdma_ctrl
*ctrl
= container_of(work
,
1671 struct nvme_rdma_ctrl
, reset_work
);
1675 nvme_rdma_shutdown_ctrl(ctrl
);
1677 ret
= nvme_rdma_configure_admin_queue(ctrl
);
1679 /* ctrl is already shutdown, just remove the ctrl */
1680 INIT_WORK(&ctrl
->delete_work
, nvme_rdma_remove_ctrl_work
);
1684 if (ctrl
->queue_count
> 1) {
1685 ret
= blk_mq_reinit_tagset(&ctrl
->tag_set
);
1689 ret
= nvme_rdma_init_io_queues(ctrl
);
1693 ret
= nvme_rdma_connect_io_queues(ctrl
);
1698 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_LIVE
);
1699 WARN_ON_ONCE(!changed
);
1701 if (ctrl
->queue_count
> 1) {
1702 nvme_start_queues(&ctrl
->ctrl
);
1703 nvme_queue_scan(&ctrl
->ctrl
);
1704 nvme_queue_async_events(&ctrl
->ctrl
);
1710 /* Deleting this dead controller... */
1711 dev_warn(ctrl
->ctrl
.device
, "Removing after reset failure\n");
1712 WARN_ON(!queue_work(nvme_rdma_wq
, &ctrl
->delete_work
));
1715 static int nvme_rdma_reset_ctrl(struct nvme_ctrl
*nctrl
)
1717 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(nctrl
);
1719 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_RESETTING
))
1722 if (!queue_work(nvme_rdma_wq
, &ctrl
->reset_work
))
1725 flush_work(&ctrl
->reset_work
);
1730 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops
= {
1732 .module
= THIS_MODULE
,
1734 .reg_read32
= nvmf_reg_read32
,
1735 .reg_read64
= nvmf_reg_read64
,
1736 .reg_write32
= nvmf_reg_write32
,
1737 .reset_ctrl
= nvme_rdma_reset_ctrl
,
1738 .free_ctrl
= nvme_rdma_free_ctrl
,
1739 .submit_async_event
= nvme_rdma_submit_async_event
,
1740 .delete_ctrl
= nvme_rdma_del_ctrl
,
1741 .get_subsysnqn
= nvmf_get_subsysnqn
,
1742 .get_address
= nvmf_get_address
,
1745 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl
*ctrl
)
1747 struct nvmf_ctrl_options
*opts
= ctrl
->ctrl
.opts
;
1750 ret
= nvme_set_queue_count(&ctrl
->ctrl
, &opts
->nr_io_queues
);
1754 ctrl
->queue_count
= opts
->nr_io_queues
+ 1;
1755 if (ctrl
->queue_count
< 2)
1758 dev_info(ctrl
->ctrl
.device
,
1759 "creating %d I/O queues.\n", opts
->nr_io_queues
);
1761 ret
= nvme_rdma_init_io_queues(ctrl
);
1766 * We need a reference on the device as long as the tag_set is alive,
1767 * as the MRs in the request structures need a valid ib_device.
1770 if (!nvme_rdma_dev_get(ctrl
->device
))
1771 goto out_free_io_queues
;
1773 memset(&ctrl
->tag_set
, 0, sizeof(ctrl
->tag_set
));
1774 ctrl
->tag_set
.ops
= &nvme_rdma_mq_ops
;
1775 ctrl
->tag_set
.queue_depth
= ctrl
->ctrl
.opts
->queue_size
;
1776 ctrl
->tag_set
.reserved_tags
= 1; /* fabric connect */
1777 ctrl
->tag_set
.numa_node
= NUMA_NO_NODE
;
1778 ctrl
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
;
1779 ctrl
->tag_set
.cmd_size
= sizeof(struct nvme_rdma_request
) +
1780 SG_CHUNK_SIZE
* sizeof(struct scatterlist
);
1781 ctrl
->tag_set
.driver_data
= ctrl
;
1782 ctrl
->tag_set
.nr_hw_queues
= ctrl
->queue_count
- 1;
1783 ctrl
->tag_set
.timeout
= NVME_IO_TIMEOUT
;
1785 ret
= blk_mq_alloc_tag_set(&ctrl
->tag_set
);
1788 ctrl
->ctrl
.tagset
= &ctrl
->tag_set
;
1790 ctrl
->ctrl
.connect_q
= blk_mq_init_queue(&ctrl
->tag_set
);
1791 if (IS_ERR(ctrl
->ctrl
.connect_q
)) {
1792 ret
= PTR_ERR(ctrl
->ctrl
.connect_q
);
1793 goto out_free_tag_set
;
1796 ret
= nvme_rdma_connect_io_queues(ctrl
);
1798 goto out_cleanup_connect_q
;
1802 out_cleanup_connect_q
:
1803 blk_cleanup_queue(ctrl
->ctrl
.connect_q
);
1805 blk_mq_free_tag_set(&ctrl
->tag_set
);
1807 nvme_rdma_dev_put(ctrl
->device
);
1809 nvme_rdma_free_io_queues(ctrl
);
1813 static int nvme_rdma_parse_ipaddr(struct sockaddr_in
*in_addr
, char *p
)
1815 u8
*addr
= (u8
*)&in_addr
->sin_addr
.s_addr
;
1816 size_t buflen
= strlen(p
);
1818 /* XXX: handle IPv6 addresses */
1820 if (buflen
> INET_ADDRSTRLEN
)
1822 if (in4_pton(p
, buflen
, addr
, '\0', NULL
) == 0)
1824 in_addr
->sin_family
= AF_INET
;
1828 static struct nvme_ctrl
*nvme_rdma_create_ctrl(struct device
*dev
,
1829 struct nvmf_ctrl_options
*opts
)
1831 struct nvme_rdma_ctrl
*ctrl
;
1835 ctrl
= kzalloc(sizeof(*ctrl
), GFP_KERNEL
);
1837 return ERR_PTR(-ENOMEM
);
1838 ctrl
->ctrl
.opts
= opts
;
1839 INIT_LIST_HEAD(&ctrl
->list
);
1841 ret
= nvme_rdma_parse_ipaddr(&ctrl
->addr_in
, opts
->traddr
);
1843 pr_err("malformed IP address passed: %s\n", opts
->traddr
);
1847 if (opts
->mask
& NVMF_OPT_TRSVCID
) {
1850 ret
= kstrtou16(opts
->trsvcid
, 0, &port
);
1854 ctrl
->addr_in
.sin_port
= cpu_to_be16(port
);
1856 ctrl
->addr_in
.sin_port
= cpu_to_be16(NVME_RDMA_IP_PORT
);
1859 ret
= nvme_init_ctrl(&ctrl
->ctrl
, dev
, &nvme_rdma_ctrl_ops
,
1860 0 /* no quirks, we're perfect! */);
1864 ctrl
->reconnect_delay
= opts
->reconnect_delay
;
1865 INIT_DELAYED_WORK(&ctrl
->reconnect_work
,
1866 nvme_rdma_reconnect_ctrl_work
);
1867 INIT_WORK(&ctrl
->err_work
, nvme_rdma_error_recovery_work
);
1868 INIT_WORK(&ctrl
->delete_work
, nvme_rdma_del_ctrl_work
);
1869 INIT_WORK(&ctrl
->reset_work
, nvme_rdma_reset_ctrl_work
);
1870 spin_lock_init(&ctrl
->lock
);
1872 ctrl
->queue_count
= opts
->nr_io_queues
+ 1; /* +1 for admin queue */
1873 ctrl
->ctrl
.sqsize
= opts
->queue_size
- 1;
1874 ctrl
->ctrl
.kato
= opts
->kato
;
1877 ctrl
->queues
= kcalloc(ctrl
->queue_count
, sizeof(*ctrl
->queues
),
1880 goto out_uninit_ctrl
;
1882 ret
= nvme_rdma_configure_admin_queue(ctrl
);
1884 goto out_kfree_queues
;
1886 /* sanity check icdoff */
1887 if (ctrl
->ctrl
.icdoff
) {
1888 dev_err(ctrl
->ctrl
.device
, "icdoff is not supported!\n");
1889 goto out_remove_admin_queue
;
1892 /* sanity check keyed sgls */
1893 if (!(ctrl
->ctrl
.sgls
& (1 << 20))) {
1894 dev_err(ctrl
->ctrl
.device
, "Mandatory keyed sgls are not support\n");
1895 goto out_remove_admin_queue
;
1898 if (opts
->queue_size
> ctrl
->ctrl
.maxcmd
) {
1899 /* warn if maxcmd is lower than queue_size */
1900 dev_warn(ctrl
->ctrl
.device
,
1901 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1902 opts
->queue_size
, ctrl
->ctrl
.maxcmd
);
1903 opts
->queue_size
= ctrl
->ctrl
.maxcmd
;
1906 if (opts
->nr_io_queues
) {
1907 ret
= nvme_rdma_create_io_queues(ctrl
);
1909 goto out_remove_admin_queue
;
1912 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_LIVE
);
1913 WARN_ON_ONCE(!changed
);
1915 dev_info(ctrl
->ctrl
.device
, "new ctrl: NQN \"%s\", addr %pISp\n",
1916 ctrl
->ctrl
.opts
->subsysnqn
, &ctrl
->addr
);
1918 kref_get(&ctrl
->ctrl
.kref
);
1920 mutex_lock(&nvme_rdma_ctrl_mutex
);
1921 list_add_tail(&ctrl
->list
, &nvme_rdma_ctrl_list
);
1922 mutex_unlock(&nvme_rdma_ctrl_mutex
);
1924 if (opts
->nr_io_queues
) {
1925 nvme_queue_scan(&ctrl
->ctrl
);
1926 nvme_queue_async_events(&ctrl
->ctrl
);
1931 out_remove_admin_queue
:
1932 nvme_stop_keep_alive(&ctrl
->ctrl
);
1933 nvme_rdma_destroy_admin_queue(ctrl
);
1935 kfree(ctrl
->queues
);
1937 nvme_uninit_ctrl(&ctrl
->ctrl
);
1938 nvme_put_ctrl(&ctrl
->ctrl
);
1941 return ERR_PTR(ret
);
1944 return ERR_PTR(ret
);
1947 static struct nvmf_transport_ops nvme_rdma_transport
= {
1949 .required_opts
= NVMF_OPT_TRADDR
,
1950 .allowed_opts
= NVMF_OPT_TRSVCID
| NVMF_OPT_RECONNECT_DELAY
,
1951 .create_ctrl
= nvme_rdma_create_ctrl
,
1954 static void nvme_rdma_add_one(struct ib_device
*ib_device
)
1958 static void nvme_rdma_remove_one(struct ib_device
*ib_device
, void *client_data
)
1960 struct nvme_rdma_ctrl
*ctrl
;
1962 /* Delete all controllers using this device */
1963 mutex_lock(&nvme_rdma_ctrl_mutex
);
1964 list_for_each_entry(ctrl
, &nvme_rdma_ctrl_list
, list
) {
1965 if (ctrl
->device
->dev
!= ib_device
)
1967 dev_info(ctrl
->ctrl
.device
,
1968 "Removing ctrl: NQN \"%s\", addr %pISp\n",
1969 ctrl
->ctrl
.opts
->subsysnqn
, &ctrl
->addr
);
1970 __nvme_rdma_del_ctrl(ctrl
);
1972 mutex_unlock(&nvme_rdma_ctrl_mutex
);
1974 flush_workqueue(nvme_rdma_wq
);
1977 static struct ib_client nvme_rdma_ib_client
= {
1978 .name
= "nvme_rdma",
1979 .add
= nvme_rdma_add_one
,
1980 .remove
= nvme_rdma_remove_one
1983 static int __init
nvme_rdma_init_module(void)
1987 nvme_rdma_wq
= create_workqueue("nvme_rdma_wq");
1991 ret
= ib_register_client(&nvme_rdma_ib_client
);
1993 destroy_workqueue(nvme_rdma_wq
);
1997 nvmf_register_transport(&nvme_rdma_transport
);
2001 static void __exit
nvme_rdma_cleanup_module(void)
2003 nvmf_unregister_transport(&nvme_rdma_transport
);
2004 ib_unregister_client(&nvme_rdma_ib_client
);
2005 destroy_workqueue(nvme_rdma_wq
);
2008 module_init(nvme_rdma_init_module
);
2009 module_exit(nvme_rdma_cleanup_module
);
2011 MODULE_LICENSE("GPL v2");