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
46 #define NVME_RDMA_MAX_PAGES_PER_MR 512
48 #define NVME_RDMA_DEF_RECONNECT_DELAY 20
51 * We handle AEN commands ourselves and don't even let the
52 * block layer know about them.
54 #define NVME_RDMA_NR_AEN_COMMANDS 1
55 #define NVME_RDMA_AQ_BLKMQ_DEPTH \
56 (NVMF_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
58 struct nvme_rdma_device
{
59 struct ib_device
*dev
;
63 struct list_head entry
;
72 struct nvme_rdma_queue
;
73 struct nvme_rdma_request
{
75 struct nvme_rdma_qe sqe
;
76 struct ib_sge sge
[1 + NVME_RDMA_MAX_INLINE_SEGMENTS
];
81 struct ib_reg_wr reg_wr
;
82 struct ib_cqe reg_cqe
;
83 struct nvme_rdma_queue
*queue
;
84 struct sg_table sg_table
;
85 struct scatterlist first_sgl
[];
88 enum nvme_rdma_queue_flags
{
89 NVME_RDMA_Q_CONNECTED
= (1 << 0),
92 struct nvme_rdma_queue
{
93 struct nvme_rdma_qe
*rsp_ring
;
96 size_t cmnd_capsule_len
;
97 struct nvme_rdma_ctrl
*ctrl
;
98 struct nvme_rdma_device
*device
;
103 struct rdma_cm_id
*cm_id
;
105 struct completion cm_done
;
108 struct nvme_rdma_ctrl
{
109 /* read and written in the hot path */
112 /* read only in the hot path */
113 struct nvme_rdma_queue
*queues
;
116 /* other member variables */
117 struct blk_mq_tag_set tag_set
;
118 struct work_struct delete_work
;
119 struct work_struct reset_work
;
120 struct work_struct err_work
;
122 struct nvme_rdma_qe async_event_sqe
;
125 struct delayed_work reconnect_work
;
127 struct list_head list
;
129 struct blk_mq_tag_set admin_tag_set
;
130 struct nvme_rdma_device
*device
;
136 struct sockaddr addr
;
137 struct sockaddr_in addr_in
;
140 struct nvme_ctrl ctrl
;
143 static inline struct nvme_rdma_ctrl
*to_rdma_ctrl(struct nvme_ctrl
*ctrl
)
145 return container_of(ctrl
, struct nvme_rdma_ctrl
, ctrl
);
148 static LIST_HEAD(device_list
);
149 static DEFINE_MUTEX(device_list_mutex
);
151 static LIST_HEAD(nvme_rdma_ctrl_list
);
152 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex
);
154 static struct workqueue_struct
*nvme_rdma_wq
;
157 * Disabling this option makes small I/O goes faster, but is fundamentally
158 * unsafe. With it turned off we will have to register a global rkey that
159 * allows read and write access to all physical memory.
161 static bool register_always
= true;
162 module_param(register_always
, bool, 0444);
163 MODULE_PARM_DESC(register_always
,
164 "Use memory registration even for contiguous memory regions");
166 static int nvme_rdma_cm_handler(struct rdma_cm_id
*cm_id
,
167 struct rdma_cm_event
*event
);
168 static void nvme_rdma_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
);
170 /* XXX: really should move to a generic header sooner or later.. */
171 static inline void put_unaligned_le24(u32 val
, u8
*p
)
178 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue
*queue
)
180 return queue
- queue
->ctrl
->queues
;
183 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue
*queue
)
185 return queue
->cmnd_capsule_len
- sizeof(struct nvme_command
);
188 static void nvme_rdma_free_qe(struct ib_device
*ibdev
, struct nvme_rdma_qe
*qe
,
189 size_t capsule_size
, enum dma_data_direction dir
)
191 ib_dma_unmap_single(ibdev
, qe
->dma
, capsule_size
, dir
);
195 static int nvme_rdma_alloc_qe(struct ib_device
*ibdev
, struct nvme_rdma_qe
*qe
,
196 size_t capsule_size
, enum dma_data_direction dir
)
198 qe
->data
= kzalloc(capsule_size
, GFP_KERNEL
);
202 qe
->dma
= ib_dma_map_single(ibdev
, qe
->data
, capsule_size
, dir
);
203 if (ib_dma_mapping_error(ibdev
, qe
->dma
)) {
211 static void nvme_rdma_free_ring(struct ib_device
*ibdev
,
212 struct nvme_rdma_qe
*ring
, size_t ib_queue_size
,
213 size_t capsule_size
, enum dma_data_direction dir
)
217 for (i
= 0; i
< ib_queue_size
; i
++)
218 nvme_rdma_free_qe(ibdev
, &ring
[i
], capsule_size
, dir
);
222 static struct nvme_rdma_qe
*nvme_rdma_alloc_ring(struct ib_device
*ibdev
,
223 size_t ib_queue_size
, size_t capsule_size
,
224 enum dma_data_direction dir
)
226 struct nvme_rdma_qe
*ring
;
229 ring
= kcalloc(ib_queue_size
, sizeof(struct nvme_rdma_qe
), GFP_KERNEL
);
233 for (i
= 0; i
< ib_queue_size
; i
++) {
234 if (nvme_rdma_alloc_qe(ibdev
, &ring
[i
], capsule_size
, dir
))
241 nvme_rdma_free_ring(ibdev
, ring
, i
, capsule_size
, dir
);
245 static void nvme_rdma_qp_event(struct ib_event
*event
, void *context
)
247 pr_debug("QP event %d\n", event
->event
);
250 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue
*queue
)
252 wait_for_completion_interruptible_timeout(&queue
->cm_done
,
253 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS
) + 1);
254 return queue
->cm_error
;
257 static int nvme_rdma_create_qp(struct nvme_rdma_queue
*queue
, const int factor
)
259 struct nvme_rdma_device
*dev
= queue
->device
;
260 struct ib_qp_init_attr init_attr
;
263 memset(&init_attr
, 0, sizeof(init_attr
));
264 init_attr
.event_handler
= nvme_rdma_qp_event
;
266 init_attr
.cap
.max_send_wr
= factor
* queue
->queue_size
+ 1;
268 init_attr
.cap
.max_recv_wr
= queue
->queue_size
+ 1;
269 init_attr
.cap
.max_recv_sge
= 1;
270 init_attr
.cap
.max_send_sge
= 1 + NVME_RDMA_MAX_INLINE_SEGMENTS
;
271 init_attr
.sq_sig_type
= IB_SIGNAL_REQ_WR
;
272 init_attr
.qp_type
= IB_QPT_RC
;
273 init_attr
.send_cq
= queue
->ib_cq
;
274 init_attr
.recv_cq
= queue
->ib_cq
;
276 ret
= rdma_create_qp(queue
->cm_id
, dev
->pd
, &init_attr
);
278 queue
->qp
= queue
->cm_id
->qp
;
282 static int nvme_rdma_reinit_request(void *data
, struct request
*rq
)
284 struct nvme_rdma_ctrl
*ctrl
= data
;
285 struct nvme_rdma_device
*dev
= ctrl
->device
;
286 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
289 if (!req
->need_inval
)
292 ib_dereg_mr(req
->mr
);
294 req
->mr
= ib_alloc_mr(dev
->pd
, IB_MR_TYPE_MEM_REG
,
296 if (IS_ERR(req
->mr
)) {
297 ret
= PTR_ERR(req
->mr
);
301 req
->need_inval
= false;
307 static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl
*ctrl
,
308 struct request
*rq
, unsigned int queue_idx
)
310 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
311 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[queue_idx
];
312 struct nvme_rdma_device
*dev
= queue
->device
;
315 ib_dereg_mr(req
->mr
);
317 nvme_rdma_free_qe(dev
->dev
, &req
->sqe
, sizeof(struct nvme_command
),
321 static void nvme_rdma_exit_request(void *data
, struct request
*rq
,
322 unsigned int hctx_idx
, unsigned int rq_idx
)
324 return __nvme_rdma_exit_request(data
, rq
, hctx_idx
+ 1);
327 static void nvme_rdma_exit_admin_request(void *data
, struct request
*rq
,
328 unsigned int hctx_idx
, unsigned int rq_idx
)
330 return __nvme_rdma_exit_request(data
, rq
, 0);
333 static int __nvme_rdma_init_request(struct nvme_rdma_ctrl
*ctrl
,
334 struct request
*rq
, unsigned int queue_idx
)
336 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
337 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[queue_idx
];
338 struct nvme_rdma_device
*dev
= queue
->device
;
339 struct ib_device
*ibdev
= dev
->dev
;
342 BUG_ON(queue_idx
>= ctrl
->queue_count
);
344 ret
= nvme_rdma_alloc_qe(ibdev
, &req
->sqe
, sizeof(struct nvme_command
),
349 req
->mr
= ib_alloc_mr(dev
->pd
, IB_MR_TYPE_MEM_REG
,
351 if (IS_ERR(req
->mr
)) {
352 ret
= PTR_ERR(req
->mr
);
361 nvme_rdma_free_qe(dev
->dev
, &req
->sqe
, sizeof(struct nvme_command
),
366 static int nvme_rdma_init_request(void *data
, struct request
*rq
,
367 unsigned int hctx_idx
, unsigned int rq_idx
,
368 unsigned int numa_node
)
370 return __nvme_rdma_init_request(data
, rq
, hctx_idx
+ 1);
373 static int nvme_rdma_init_admin_request(void *data
, struct request
*rq
,
374 unsigned int hctx_idx
, unsigned int rq_idx
,
375 unsigned int numa_node
)
377 return __nvme_rdma_init_request(data
, rq
, 0);
380 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
381 unsigned int hctx_idx
)
383 struct nvme_rdma_ctrl
*ctrl
= data
;
384 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[hctx_idx
+ 1];
386 BUG_ON(hctx_idx
>= ctrl
->queue_count
);
388 hctx
->driver_data
= queue
;
392 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
393 unsigned int hctx_idx
)
395 struct nvme_rdma_ctrl
*ctrl
= data
;
396 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[0];
398 BUG_ON(hctx_idx
!= 0);
400 hctx
->driver_data
= queue
;
404 static void nvme_rdma_free_dev(struct kref
*ref
)
406 struct nvme_rdma_device
*ndev
=
407 container_of(ref
, struct nvme_rdma_device
, ref
);
409 mutex_lock(&device_list_mutex
);
410 list_del(&ndev
->entry
);
411 mutex_unlock(&device_list_mutex
);
413 if (!register_always
)
414 ib_dereg_mr(ndev
->mr
);
415 ib_dealloc_pd(ndev
->pd
);
420 static void nvme_rdma_dev_put(struct nvme_rdma_device
*dev
)
422 kref_put(&dev
->ref
, nvme_rdma_free_dev
);
425 static int nvme_rdma_dev_get(struct nvme_rdma_device
*dev
)
427 return kref_get_unless_zero(&dev
->ref
);
430 static struct nvme_rdma_device
*
431 nvme_rdma_find_get_device(struct rdma_cm_id
*cm_id
)
433 struct nvme_rdma_device
*ndev
;
435 mutex_lock(&device_list_mutex
);
436 list_for_each_entry(ndev
, &device_list
, entry
) {
437 if (ndev
->dev
->node_guid
== cm_id
->device
->node_guid
&&
438 nvme_rdma_dev_get(ndev
))
442 ndev
= kzalloc(sizeof(*ndev
), GFP_KERNEL
);
446 ndev
->dev
= cm_id
->device
;
447 kref_init(&ndev
->ref
);
449 ndev
->pd
= ib_alloc_pd(ndev
->dev
);
450 if (IS_ERR(ndev
->pd
))
453 if (!register_always
) {
454 ndev
->mr
= ib_get_dma_mr(ndev
->pd
,
455 IB_ACCESS_LOCAL_WRITE
|
456 IB_ACCESS_REMOTE_READ
|
457 IB_ACCESS_REMOTE_WRITE
);
458 if (IS_ERR(ndev
->mr
))
462 if (!(ndev
->dev
->attrs
.device_cap_flags
&
463 IB_DEVICE_MEM_MGT_EXTENSIONS
)) {
464 dev_err(&ndev
->dev
->dev
,
465 "Memory registrations not supported.\n");
469 list_add(&ndev
->entry
, &device_list
);
471 mutex_unlock(&device_list_mutex
);
475 if (!register_always
)
476 ib_dereg_mr(ndev
->mr
);
478 ib_dealloc_pd(ndev
->pd
);
482 mutex_unlock(&device_list_mutex
);
486 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue
*queue
)
488 struct nvme_rdma_device
*dev
= queue
->device
;
489 struct ib_device
*ibdev
= dev
->dev
;
491 rdma_destroy_qp(queue
->cm_id
);
492 ib_free_cq(queue
->ib_cq
);
494 nvme_rdma_free_ring(ibdev
, queue
->rsp_ring
, queue
->queue_size
,
495 sizeof(struct nvme_completion
), DMA_FROM_DEVICE
);
497 nvme_rdma_dev_put(dev
);
500 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue
*queue
,
501 struct nvme_rdma_device
*dev
)
503 struct ib_device
*ibdev
= dev
->dev
;
504 const int send_wr_factor
= 3; /* MR, SEND, INV */
505 const int cq_factor
= send_wr_factor
+ 1; /* + RECV */
506 int comp_vector
, idx
= nvme_rdma_queue_idx(queue
);
513 * The admin queue is barely used once the controller is live, so don't
514 * bother to spread it out.
519 comp_vector
= idx
% ibdev
->num_comp_vectors
;
522 /* +1 for ib_stop_cq */
523 queue
->ib_cq
= ib_alloc_cq(dev
->dev
, queue
,
524 cq_factor
* queue
->queue_size
+ 1, comp_vector
,
526 if (IS_ERR(queue
->ib_cq
)) {
527 ret
= PTR_ERR(queue
->ib_cq
);
531 ret
= nvme_rdma_create_qp(queue
, send_wr_factor
);
533 goto out_destroy_ib_cq
;
535 queue
->rsp_ring
= nvme_rdma_alloc_ring(ibdev
, queue
->queue_size
,
536 sizeof(struct nvme_completion
), DMA_FROM_DEVICE
);
537 if (!queue
->rsp_ring
) {
545 ib_destroy_qp(queue
->qp
);
547 ib_free_cq(queue
->ib_cq
);
552 static int nvme_rdma_init_queue(struct nvme_rdma_ctrl
*ctrl
,
553 int idx
, size_t queue_size
)
555 struct nvme_rdma_queue
*queue
;
558 queue
= &ctrl
->queues
[idx
];
560 init_completion(&queue
->cm_done
);
563 queue
->cmnd_capsule_len
= ctrl
->ctrl
.ioccsz
* 16;
565 queue
->cmnd_capsule_len
= sizeof(struct nvme_command
);
567 queue
->queue_size
= queue_size
;
569 queue
->cm_id
= rdma_create_id(&init_net
, nvme_rdma_cm_handler
, queue
,
570 RDMA_PS_TCP
, IB_QPT_RC
);
571 if (IS_ERR(queue
->cm_id
)) {
572 dev_info(ctrl
->ctrl
.device
,
573 "failed to create CM ID: %ld\n", PTR_ERR(queue
->cm_id
));
574 return PTR_ERR(queue
->cm_id
);
577 queue
->cm_error
= -ETIMEDOUT
;
578 ret
= rdma_resolve_addr(queue
->cm_id
, NULL
, &ctrl
->addr
,
579 NVME_RDMA_CONNECT_TIMEOUT_MS
);
581 dev_info(ctrl
->ctrl
.device
,
582 "rdma_resolve_addr failed (%d).\n", ret
);
583 goto out_destroy_cm_id
;
586 ret
= nvme_rdma_wait_for_cm(queue
);
588 dev_info(ctrl
->ctrl
.device
,
589 "rdma_resolve_addr wait failed (%d).\n", ret
);
590 goto out_destroy_cm_id
;
593 set_bit(NVME_RDMA_Q_CONNECTED
, &queue
->flags
);
598 rdma_destroy_id(queue
->cm_id
);
602 static void nvme_rdma_stop_queue(struct nvme_rdma_queue
*queue
)
604 rdma_disconnect(queue
->cm_id
);
605 ib_drain_qp(queue
->qp
);
608 static void nvme_rdma_free_queue(struct nvme_rdma_queue
*queue
)
610 nvme_rdma_destroy_queue_ib(queue
);
611 rdma_destroy_id(queue
->cm_id
);
614 static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue
*queue
)
616 if (!test_and_clear_bit(NVME_RDMA_Q_CONNECTED
, &queue
->flags
))
618 nvme_rdma_stop_queue(queue
);
619 nvme_rdma_free_queue(queue
);
622 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl
*ctrl
)
626 for (i
= 1; i
< ctrl
->queue_count
; i
++)
627 nvme_rdma_stop_and_free_queue(&ctrl
->queues
[i
]);
630 static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl
*ctrl
)
634 for (i
= 1; i
< ctrl
->queue_count
; i
++) {
635 ret
= nvmf_connect_io_queue(&ctrl
->ctrl
, i
);
643 static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl
*ctrl
)
647 for (i
= 1; i
< ctrl
->queue_count
; i
++) {
648 ret
= nvme_rdma_init_queue(ctrl
, i
, ctrl
->ctrl
.sqsize
);
650 dev_info(ctrl
->ctrl
.device
,
651 "failed to initialize i/o queue: %d\n", ret
);
652 goto out_free_queues
;
660 nvme_rdma_stop_and_free_queue(&ctrl
->queues
[i
]);
665 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl
*ctrl
)
667 nvme_rdma_free_qe(ctrl
->queues
[0].device
->dev
, &ctrl
->async_event_sqe
,
668 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
669 nvme_rdma_stop_and_free_queue(&ctrl
->queues
[0]);
670 blk_cleanup_queue(ctrl
->ctrl
.admin_q
);
671 blk_mq_free_tag_set(&ctrl
->admin_tag_set
);
672 nvme_rdma_dev_put(ctrl
->device
);
675 static void nvme_rdma_free_ctrl(struct nvme_ctrl
*nctrl
)
677 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(nctrl
);
679 if (list_empty(&ctrl
->list
))
682 mutex_lock(&nvme_rdma_ctrl_mutex
);
683 list_del(&ctrl
->list
);
684 mutex_unlock(&nvme_rdma_ctrl_mutex
);
687 nvmf_free_options(nctrl
->opts
);
692 static void nvme_rdma_reconnect_ctrl_work(struct work_struct
*work
)
694 struct nvme_rdma_ctrl
*ctrl
= container_of(to_delayed_work(work
),
695 struct nvme_rdma_ctrl
, reconnect_work
);
699 if (ctrl
->queue_count
> 1) {
700 nvme_rdma_free_io_queues(ctrl
);
702 ret
= blk_mq_reinit_tagset(&ctrl
->tag_set
);
707 nvme_rdma_stop_and_free_queue(&ctrl
->queues
[0]);
709 ret
= blk_mq_reinit_tagset(&ctrl
->admin_tag_set
);
713 ret
= nvme_rdma_init_queue(ctrl
, 0, NVMF_AQ_DEPTH
);
717 blk_mq_start_stopped_hw_queues(ctrl
->ctrl
.admin_q
, true);
719 ret
= nvmf_connect_admin_queue(&ctrl
->ctrl
);
723 ret
= nvme_enable_ctrl(&ctrl
->ctrl
, ctrl
->cap
);
727 nvme_start_keep_alive(&ctrl
->ctrl
);
729 if (ctrl
->queue_count
> 1) {
730 ret
= nvme_rdma_init_io_queues(ctrl
);
734 ret
= nvme_rdma_connect_io_queues(ctrl
);
739 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_LIVE
);
740 WARN_ON_ONCE(!changed
);
742 if (ctrl
->queue_count
> 1) {
743 nvme_start_queues(&ctrl
->ctrl
);
744 nvme_queue_scan(&ctrl
->ctrl
);
745 nvme_queue_async_events(&ctrl
->ctrl
);
748 dev_info(ctrl
->ctrl
.device
, "Successfully reconnected\n");
753 blk_mq_stop_hw_queues(ctrl
->ctrl
.admin_q
);
755 /* Make sure we are not resetting/deleting */
756 if (ctrl
->ctrl
.state
== NVME_CTRL_RECONNECTING
) {
757 dev_info(ctrl
->ctrl
.device
,
758 "Failed reconnect attempt, requeueing...\n");
759 queue_delayed_work(nvme_rdma_wq
, &ctrl
->reconnect_work
,
760 ctrl
->reconnect_delay
* HZ
);
764 static void nvme_rdma_error_recovery_work(struct work_struct
*work
)
766 struct nvme_rdma_ctrl
*ctrl
= container_of(work
,
767 struct nvme_rdma_ctrl
, err_work
);
769 nvme_stop_keep_alive(&ctrl
->ctrl
);
770 if (ctrl
->queue_count
> 1)
771 nvme_stop_queues(&ctrl
->ctrl
);
772 blk_mq_stop_hw_queues(ctrl
->ctrl
.admin_q
);
774 /* We must take care of fastfail/requeue all our inflight requests */
775 if (ctrl
->queue_count
> 1)
776 blk_mq_tagset_busy_iter(&ctrl
->tag_set
,
777 nvme_cancel_request
, &ctrl
->ctrl
);
778 blk_mq_tagset_busy_iter(&ctrl
->admin_tag_set
,
779 nvme_cancel_request
, &ctrl
->ctrl
);
781 dev_info(ctrl
->ctrl
.device
, "reconnecting in %d seconds\n",
782 ctrl
->reconnect_delay
);
784 queue_delayed_work(nvme_rdma_wq
, &ctrl
->reconnect_work
,
785 ctrl
->reconnect_delay
* HZ
);
788 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl
*ctrl
)
790 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_RECONNECTING
))
793 queue_work(nvme_rdma_wq
, &ctrl
->err_work
);
796 static void nvme_rdma_wr_error(struct ib_cq
*cq
, struct ib_wc
*wc
,
799 struct nvme_rdma_queue
*queue
= cq
->cq_context
;
800 struct nvme_rdma_ctrl
*ctrl
= queue
->ctrl
;
802 if (ctrl
->ctrl
.state
== NVME_CTRL_LIVE
)
803 dev_info(ctrl
->ctrl
.device
,
804 "%s for CQE 0x%p failed with status %s (%d)\n",
806 ib_wc_status_msg(wc
->status
), wc
->status
);
807 nvme_rdma_error_recovery(ctrl
);
810 static void nvme_rdma_memreg_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
812 if (unlikely(wc
->status
!= IB_WC_SUCCESS
))
813 nvme_rdma_wr_error(cq
, wc
, "MEMREG");
816 static void nvme_rdma_inv_rkey_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
818 if (unlikely(wc
->status
!= IB_WC_SUCCESS
))
819 nvme_rdma_wr_error(cq
, wc
, "LOCAL_INV");
822 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue
*queue
,
823 struct nvme_rdma_request
*req
)
825 struct ib_send_wr
*bad_wr
;
826 struct ib_send_wr wr
= {
827 .opcode
= IB_WR_LOCAL_INV
,
831 .ex
.invalidate_rkey
= req
->mr
->rkey
,
834 req
->reg_cqe
.done
= nvme_rdma_inv_rkey_done
;
835 wr
.wr_cqe
= &req
->reg_cqe
;
837 return ib_post_send(queue
->qp
, &wr
, &bad_wr
);
840 static void nvme_rdma_unmap_data(struct nvme_rdma_queue
*queue
,
843 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
844 struct nvme_rdma_ctrl
*ctrl
= queue
->ctrl
;
845 struct nvme_rdma_device
*dev
= queue
->device
;
846 struct ib_device
*ibdev
= dev
->dev
;
849 if (!blk_rq_bytes(rq
))
852 if (req
->need_inval
) {
853 res
= nvme_rdma_inv_rkey(queue
, req
);
855 dev_err(ctrl
->ctrl
.device
,
856 "Queueing INV WR for rkey %#x failed (%d)\n",
858 nvme_rdma_error_recovery(queue
->ctrl
);
862 ib_dma_unmap_sg(ibdev
, req
->sg_table
.sgl
,
863 req
->nents
, rq_data_dir(rq
) ==
864 WRITE
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
866 nvme_cleanup_cmd(rq
);
867 sg_free_table_chained(&req
->sg_table
, true);
870 static int nvme_rdma_set_sg_null(struct nvme_command
*c
)
872 struct nvme_keyed_sgl_desc
*sg
= &c
->common
.dptr
.ksgl
;
875 put_unaligned_le24(0, sg
->length
);
876 put_unaligned_le32(0, sg
->key
);
877 sg
->type
= NVME_KEY_SGL_FMT_DATA_DESC
<< 4;
881 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue
*queue
,
882 struct nvme_rdma_request
*req
, struct nvme_command
*c
)
884 struct nvme_sgl_desc
*sg
= &c
->common
.dptr
.sgl
;
886 req
->sge
[1].addr
= sg_dma_address(req
->sg_table
.sgl
);
887 req
->sge
[1].length
= sg_dma_len(req
->sg_table
.sgl
);
888 req
->sge
[1].lkey
= queue
->device
->pd
->local_dma_lkey
;
890 sg
->addr
= cpu_to_le64(queue
->ctrl
->ctrl
.icdoff
);
891 sg
->length
= cpu_to_le32(sg_dma_len(req
->sg_table
.sgl
));
892 sg
->type
= (NVME_SGL_FMT_DATA_DESC
<< 4) | NVME_SGL_FMT_OFFSET
;
894 req
->inline_data
= true;
899 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue
*queue
,
900 struct nvme_rdma_request
*req
, struct nvme_command
*c
)
902 struct nvme_keyed_sgl_desc
*sg
= &c
->common
.dptr
.ksgl
;
904 sg
->addr
= cpu_to_le64(sg_dma_address(req
->sg_table
.sgl
));
905 put_unaligned_le24(sg_dma_len(req
->sg_table
.sgl
), sg
->length
);
906 put_unaligned_le32(queue
->device
->mr
->rkey
, sg
->key
);
907 sg
->type
= NVME_KEY_SGL_FMT_DATA_DESC
<< 4;
911 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue
*queue
,
912 struct nvme_rdma_request
*req
, struct nvme_command
*c
,
915 struct nvme_keyed_sgl_desc
*sg
= &c
->common
.dptr
.ksgl
;
918 nr
= ib_map_mr_sg(req
->mr
, req
->sg_table
.sgl
, count
, NULL
, PAGE_SIZE
);
925 ib_update_fast_reg_key(req
->mr
, ib_inc_rkey(req
->mr
->rkey
));
927 req
->reg_cqe
.done
= nvme_rdma_memreg_done
;
928 memset(&req
->reg_wr
, 0, sizeof(req
->reg_wr
));
929 req
->reg_wr
.wr
.opcode
= IB_WR_REG_MR
;
930 req
->reg_wr
.wr
.wr_cqe
= &req
->reg_cqe
;
931 req
->reg_wr
.wr
.num_sge
= 0;
932 req
->reg_wr
.mr
= req
->mr
;
933 req
->reg_wr
.key
= req
->mr
->rkey
;
934 req
->reg_wr
.access
= IB_ACCESS_LOCAL_WRITE
|
935 IB_ACCESS_REMOTE_READ
|
936 IB_ACCESS_REMOTE_WRITE
;
938 req
->need_inval
= true;
940 sg
->addr
= cpu_to_le64(req
->mr
->iova
);
941 put_unaligned_le24(req
->mr
->length
, sg
->length
);
942 put_unaligned_le32(req
->mr
->rkey
, sg
->key
);
943 sg
->type
= (NVME_KEY_SGL_FMT_DATA_DESC
<< 4) |
944 NVME_SGL_FMT_INVALIDATE
;
949 static int nvme_rdma_map_data(struct nvme_rdma_queue
*queue
,
950 struct request
*rq
, unsigned int map_len
,
951 struct nvme_command
*c
)
953 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
954 struct nvme_rdma_device
*dev
= queue
->device
;
955 struct ib_device
*ibdev
= dev
->dev
;
960 req
->inline_data
= false;
961 req
->need_inval
= false;
963 c
->common
.flags
|= NVME_CMD_SGL_METABUF
;
965 if (!blk_rq_bytes(rq
))
966 return nvme_rdma_set_sg_null(c
);
968 req
->sg_table
.sgl
= req
->first_sgl
;
969 ret
= sg_alloc_table_chained(&req
->sg_table
, rq
->nr_phys_segments
,
974 nents
= blk_rq_map_sg(rq
->q
, rq
, req
->sg_table
.sgl
);
975 BUG_ON(nents
> rq
->nr_phys_segments
);
978 count
= ib_dma_map_sg(ibdev
, req
->sg_table
.sgl
, nents
,
979 rq_data_dir(rq
) == WRITE
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
980 if (unlikely(count
<= 0)) {
981 sg_free_table_chained(&req
->sg_table
, true);
986 if (rq_data_dir(rq
) == WRITE
&&
987 map_len
<= nvme_rdma_inline_data_size(queue
) &&
988 nvme_rdma_queue_idx(queue
))
989 return nvme_rdma_map_sg_inline(queue
, req
, c
);
991 if (!register_always
)
992 return nvme_rdma_map_sg_single(queue
, req
, c
);
995 return nvme_rdma_map_sg_fr(queue
, req
, c
, count
);
998 static void nvme_rdma_send_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
1000 if (unlikely(wc
->status
!= IB_WC_SUCCESS
))
1001 nvme_rdma_wr_error(cq
, wc
, "SEND");
1004 static int nvme_rdma_post_send(struct nvme_rdma_queue
*queue
,
1005 struct nvme_rdma_qe
*qe
, struct ib_sge
*sge
, u32 num_sge
,
1006 struct ib_send_wr
*first
, bool flush
)
1008 struct ib_send_wr wr
, *bad_wr
;
1011 sge
->addr
= qe
->dma
;
1012 sge
->length
= sizeof(struct nvme_command
),
1013 sge
->lkey
= queue
->device
->pd
->local_dma_lkey
;
1015 qe
->cqe
.done
= nvme_rdma_send_done
;
1018 wr
.wr_cqe
= &qe
->cqe
;
1020 wr
.num_sge
= num_sge
;
1021 wr
.opcode
= IB_WR_SEND
;
1025 * Unsignalled send completions are another giant desaster in the
1026 * IB Verbs spec: If we don't regularly post signalled sends
1027 * the send queue will fill up and only a QP reset will rescue us.
1028 * Would have been way to obvious to handle this in hardware or
1029 * at least the RDMA stack..
1031 * This messy and racy code sniplet is copy and pasted from the iSER
1032 * initiator, and the magic '32' comes from there as well.
1034 * Always signal the flushes. The magic request used for the flush
1035 * sequencer is not allocated in our driver's tagset and it's
1036 * triggered to be freed by blk_cleanup_queue(). So we need to
1037 * always mark it as signaled to ensure that the "wr_cqe", which is
1038 * embeded in request's payload, is not freed when __ib_process_cq()
1039 * calls wr_cqe->done().
1041 if ((++queue
->sig_count
% 32) == 0 || flush
)
1042 wr
.send_flags
|= IB_SEND_SIGNALED
;
1049 ret
= ib_post_send(queue
->qp
, first
, &bad_wr
);
1051 dev_err(queue
->ctrl
->ctrl
.device
,
1052 "%s failed with error code %d\n", __func__
, ret
);
1057 static int nvme_rdma_post_recv(struct nvme_rdma_queue
*queue
,
1058 struct nvme_rdma_qe
*qe
)
1060 struct ib_recv_wr wr
, *bad_wr
;
1064 list
.addr
= qe
->dma
;
1065 list
.length
= sizeof(struct nvme_completion
);
1066 list
.lkey
= queue
->device
->pd
->local_dma_lkey
;
1068 qe
->cqe
.done
= nvme_rdma_recv_done
;
1071 wr
.wr_cqe
= &qe
->cqe
;
1075 ret
= ib_post_recv(queue
->qp
, &wr
, &bad_wr
);
1077 dev_err(queue
->ctrl
->ctrl
.device
,
1078 "%s failed with error code %d\n", __func__
, ret
);
1083 static struct blk_mq_tags
*nvme_rdma_tagset(struct nvme_rdma_queue
*queue
)
1085 u32 queue_idx
= nvme_rdma_queue_idx(queue
);
1088 return queue
->ctrl
->admin_tag_set
.tags
[queue_idx
];
1089 return queue
->ctrl
->tag_set
.tags
[queue_idx
- 1];
1092 static void nvme_rdma_submit_async_event(struct nvme_ctrl
*arg
, int aer_idx
)
1094 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(arg
);
1095 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[0];
1096 struct ib_device
*dev
= queue
->device
->dev
;
1097 struct nvme_rdma_qe
*sqe
= &ctrl
->async_event_sqe
;
1098 struct nvme_command
*cmd
= sqe
->data
;
1102 if (WARN_ON_ONCE(aer_idx
!= 0))
1105 ib_dma_sync_single_for_cpu(dev
, sqe
->dma
, sizeof(*cmd
), DMA_TO_DEVICE
);
1107 memset(cmd
, 0, sizeof(*cmd
));
1108 cmd
->common
.opcode
= nvme_admin_async_event
;
1109 cmd
->common
.command_id
= NVME_RDMA_AQ_BLKMQ_DEPTH
;
1110 cmd
->common
.flags
|= NVME_CMD_SGL_METABUF
;
1111 nvme_rdma_set_sg_null(cmd
);
1113 ib_dma_sync_single_for_device(dev
, sqe
->dma
, sizeof(*cmd
),
1116 ret
= nvme_rdma_post_send(queue
, sqe
, &sge
, 1, NULL
, false);
1120 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue
*queue
,
1121 struct nvme_completion
*cqe
, struct ib_wc
*wc
, int tag
)
1123 u16 status
= le16_to_cpu(cqe
->status
);
1125 struct nvme_rdma_request
*req
;
1130 rq
= blk_mq_tag_to_rq(nvme_rdma_tagset(queue
), cqe
->command_id
);
1132 dev_err(queue
->ctrl
->ctrl
.device
,
1133 "tag 0x%x on QP %#x not found\n",
1134 cqe
->command_id
, queue
->qp
->qp_num
);
1135 nvme_rdma_error_recovery(queue
->ctrl
);
1138 req
= blk_mq_rq_to_pdu(rq
);
1140 if (rq
->cmd_type
== REQ_TYPE_DRV_PRIV
&& rq
->special
)
1141 memcpy(rq
->special
, cqe
, sizeof(*cqe
));
1146 if ((wc
->wc_flags
& IB_WC_WITH_INVALIDATE
) &&
1147 wc
->ex
.invalidate_rkey
== req
->mr
->rkey
)
1148 req
->need_inval
= false;
1150 blk_mq_complete_request(rq
, status
);
1155 static int __nvme_rdma_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
, int tag
)
1157 struct nvme_rdma_qe
*qe
=
1158 container_of(wc
->wr_cqe
, struct nvme_rdma_qe
, cqe
);
1159 struct nvme_rdma_queue
*queue
= cq
->cq_context
;
1160 struct ib_device
*ibdev
= queue
->device
->dev
;
1161 struct nvme_completion
*cqe
= qe
->data
;
1162 const size_t len
= sizeof(struct nvme_completion
);
1165 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
1166 nvme_rdma_wr_error(cq
, wc
, "RECV");
1170 ib_dma_sync_single_for_cpu(ibdev
, qe
->dma
, len
, DMA_FROM_DEVICE
);
1172 * AEN requests are special as they don't time out and can
1173 * survive any kind of queue freeze and often don't respond to
1174 * aborts. We don't even bother to allocate a struct request
1175 * for them but rather special case them here.
1177 if (unlikely(nvme_rdma_queue_idx(queue
) == 0 &&
1178 cqe
->command_id
>= NVME_RDMA_AQ_BLKMQ_DEPTH
))
1179 nvme_complete_async_event(&queue
->ctrl
->ctrl
, cqe
);
1181 ret
= nvme_rdma_process_nvme_rsp(queue
, cqe
, wc
, tag
);
1182 ib_dma_sync_single_for_device(ibdev
, qe
->dma
, len
, DMA_FROM_DEVICE
);
1184 nvme_rdma_post_recv(queue
, qe
);
1188 static void nvme_rdma_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
1190 __nvme_rdma_recv_done(cq
, wc
, -1);
1193 static int nvme_rdma_conn_established(struct nvme_rdma_queue
*queue
)
1197 for (i
= 0; i
< queue
->queue_size
; i
++) {
1198 ret
= nvme_rdma_post_recv(queue
, &queue
->rsp_ring
[i
]);
1200 goto out_destroy_queue_ib
;
1205 out_destroy_queue_ib
:
1206 nvme_rdma_destroy_queue_ib(queue
);
1210 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue
*queue
,
1211 struct rdma_cm_event
*ev
)
1213 if (ev
->param
.conn
.private_data_len
) {
1214 struct nvme_rdma_cm_rej
*rej
=
1215 (struct nvme_rdma_cm_rej
*)ev
->param
.conn
.private_data
;
1217 dev_err(queue
->ctrl
->ctrl
.device
,
1218 "Connect rejected, status %d.", le16_to_cpu(rej
->sts
));
1219 /* XXX: Think of something clever to do here... */
1221 dev_err(queue
->ctrl
->ctrl
.device
,
1222 "Connect rejected, no private data.\n");
1228 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue
*queue
)
1230 struct nvme_rdma_device
*dev
;
1233 dev
= nvme_rdma_find_get_device(queue
->cm_id
);
1235 dev_err(queue
->cm_id
->device
->dma_device
,
1236 "no client data found!\n");
1237 return -ECONNREFUSED
;
1240 ret
= nvme_rdma_create_queue_ib(queue
, dev
);
1242 nvme_rdma_dev_put(dev
);
1246 ret
= rdma_resolve_route(queue
->cm_id
, NVME_RDMA_CONNECT_TIMEOUT_MS
);
1248 dev_err(queue
->ctrl
->ctrl
.device
,
1249 "rdma_resolve_route failed (%d).\n",
1251 goto out_destroy_queue
;
1257 nvme_rdma_destroy_queue_ib(queue
);
1262 static int nvme_rdma_route_resolved(struct nvme_rdma_queue
*queue
)
1264 struct nvme_rdma_ctrl
*ctrl
= queue
->ctrl
;
1265 struct rdma_conn_param param
= { };
1266 struct nvme_rdma_cm_req priv
= { };
1269 param
.qp_num
= queue
->qp
->qp_num
;
1270 param
.flow_control
= 1;
1272 param
.responder_resources
= queue
->device
->dev
->attrs
.max_qp_rd_atom
;
1273 /* maximum retry count */
1274 param
.retry_count
= 7;
1275 param
.rnr_retry_count
= 7;
1276 param
.private_data
= &priv
;
1277 param
.private_data_len
= sizeof(priv
);
1279 priv
.recfmt
= cpu_to_le16(NVME_RDMA_CM_FMT_1_0
);
1280 priv
.qid
= cpu_to_le16(nvme_rdma_queue_idx(queue
));
1281 priv
.hrqsize
= cpu_to_le16(queue
->queue_size
);
1282 priv
.hsqsize
= cpu_to_le16(queue
->queue_size
);
1284 ret
= rdma_connect(queue
->cm_id
, ¶m
);
1286 dev_err(ctrl
->ctrl
.device
,
1287 "rdma_connect failed (%d).\n", ret
);
1288 goto out_destroy_queue_ib
;
1293 out_destroy_queue_ib
:
1294 nvme_rdma_destroy_queue_ib(queue
);
1299 * nvme_rdma_device_unplug() - Handle RDMA device unplug
1300 * @queue: Queue that owns the cm_id that caught the event
1302 * DEVICE_REMOVAL event notifies us that the RDMA device is about
1303 * to unplug so we should take care of destroying our RDMA resources.
1304 * This event will be generated for each allocated cm_id.
1306 * In our case, the RDMA resources are managed per controller and not
1307 * only per queue. So the way we handle this is we trigger an implicit
1308 * controller deletion upon the first DEVICE_REMOVAL event we see, and
1309 * hold the event inflight until the controller deletion is completed.
1311 * One exception that we need to handle is the destruction of the cm_id
1312 * that caught the event. Since we hold the callout until the controller
1313 * deletion is completed, we'll deadlock if the controller deletion will
1314 * call rdma_destroy_id on this queue's cm_id. Thus, we claim ownership
1315 * of destroying this queue before-hand, destroy the queue resources,
1316 * then queue the controller deletion which won't destroy this queue and
1317 * we destroy the cm_id implicitely by returning a non-zero rc to the callout.
1319 static int nvme_rdma_device_unplug(struct nvme_rdma_queue
*queue
)
1321 struct nvme_rdma_ctrl
*ctrl
= queue
->ctrl
;
1324 /* Own the controller deletion */
1325 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_DELETING
))
1328 dev_warn(ctrl
->ctrl
.device
,
1329 "Got rdma device removal event, deleting ctrl\n");
1331 /* Get rid of reconnect work if its running */
1332 cancel_delayed_work_sync(&ctrl
->reconnect_work
);
1334 /* Disable the queue so ctrl delete won't free it */
1335 if (test_and_clear_bit(NVME_RDMA_Q_CONNECTED
, &queue
->flags
)) {
1336 /* Free this queue ourselves */
1337 nvme_rdma_stop_queue(queue
);
1338 nvme_rdma_destroy_queue_ib(queue
);
1340 /* Return non-zero so the cm_id will destroy implicitly */
1344 /* Queue controller deletion */
1345 queue_work(nvme_rdma_wq
, &ctrl
->delete_work
);
1346 flush_work(&ctrl
->delete_work
);
1350 static int nvme_rdma_cm_handler(struct rdma_cm_id
*cm_id
,
1351 struct rdma_cm_event
*ev
)
1353 struct nvme_rdma_queue
*queue
= cm_id
->context
;
1356 dev_dbg(queue
->ctrl
->ctrl
.device
, "%s (%d): status %d id %p\n",
1357 rdma_event_msg(ev
->event
), ev
->event
,
1360 switch (ev
->event
) {
1361 case RDMA_CM_EVENT_ADDR_RESOLVED
:
1362 cm_error
= nvme_rdma_addr_resolved(queue
);
1364 case RDMA_CM_EVENT_ROUTE_RESOLVED
:
1365 cm_error
= nvme_rdma_route_resolved(queue
);
1367 case RDMA_CM_EVENT_ESTABLISHED
:
1368 queue
->cm_error
= nvme_rdma_conn_established(queue
);
1369 /* complete cm_done regardless of success/failure */
1370 complete(&queue
->cm_done
);
1372 case RDMA_CM_EVENT_REJECTED
:
1373 cm_error
= nvme_rdma_conn_rejected(queue
, ev
);
1375 case RDMA_CM_EVENT_ADDR_ERROR
:
1376 case RDMA_CM_EVENT_ROUTE_ERROR
:
1377 case RDMA_CM_EVENT_CONNECT_ERROR
:
1378 case RDMA_CM_EVENT_UNREACHABLE
:
1379 dev_dbg(queue
->ctrl
->ctrl
.device
,
1380 "CM error event %d\n", ev
->event
);
1381 cm_error
= -ECONNRESET
;
1383 case RDMA_CM_EVENT_DISCONNECTED
:
1384 case RDMA_CM_EVENT_ADDR_CHANGE
:
1385 case RDMA_CM_EVENT_TIMEWAIT_EXIT
:
1386 dev_dbg(queue
->ctrl
->ctrl
.device
,
1387 "disconnect received - connection closed\n");
1388 nvme_rdma_error_recovery(queue
->ctrl
);
1390 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
1391 /* return 1 means impliciy CM ID destroy */
1392 return nvme_rdma_device_unplug(queue
);
1394 dev_err(queue
->ctrl
->ctrl
.device
,
1395 "Unexpected RDMA CM event (%d)\n", ev
->event
);
1396 nvme_rdma_error_recovery(queue
->ctrl
);
1401 queue
->cm_error
= cm_error
;
1402 complete(&queue
->cm_done
);
1408 static enum blk_eh_timer_return
1409 nvme_rdma_timeout(struct request
*rq
, bool reserved
)
1411 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1413 /* queue error recovery */
1414 nvme_rdma_error_recovery(req
->queue
->ctrl
);
1416 /* fail with DNR on cmd timeout */
1417 rq
->errors
= NVME_SC_ABORT_REQ
| NVME_SC_DNR
;
1419 return BLK_EH_HANDLED
;
1422 static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx
*hctx
,
1423 const struct blk_mq_queue_data
*bd
)
1425 struct nvme_ns
*ns
= hctx
->queue
->queuedata
;
1426 struct nvme_rdma_queue
*queue
= hctx
->driver_data
;
1427 struct request
*rq
= bd
->rq
;
1428 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1429 struct nvme_rdma_qe
*sqe
= &req
->sqe
;
1430 struct nvme_command
*c
= sqe
->data
;
1432 struct ib_device
*dev
;
1433 unsigned int map_len
;
1436 WARN_ON_ONCE(rq
->tag
< 0);
1438 dev
= queue
->device
->dev
;
1439 ib_dma_sync_single_for_cpu(dev
, sqe
->dma
,
1440 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
1442 ret
= nvme_setup_cmd(ns
, rq
, c
);
1446 c
->common
.command_id
= rq
->tag
;
1447 blk_mq_start_request(rq
);
1449 map_len
= nvme_map_len(rq
);
1450 ret
= nvme_rdma_map_data(queue
, rq
, map_len
, c
);
1452 dev_err(queue
->ctrl
->ctrl
.device
,
1453 "Failed to map data (%d)\n", ret
);
1454 nvme_cleanup_cmd(rq
);
1458 ib_dma_sync_single_for_device(dev
, sqe
->dma
,
1459 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
1461 if (rq
->cmd_type
== REQ_TYPE_FS
&& req_op(rq
) == REQ_OP_FLUSH
)
1463 ret
= nvme_rdma_post_send(queue
, sqe
, req
->sge
, req
->num_sge
,
1464 req
->need_inval
? &req
->reg_wr
.wr
: NULL
, flush
);
1466 nvme_rdma_unmap_data(queue
, rq
);
1470 return BLK_MQ_RQ_QUEUE_OK
;
1472 return (ret
== -ENOMEM
|| ret
== -EAGAIN
) ?
1473 BLK_MQ_RQ_QUEUE_BUSY
: BLK_MQ_RQ_QUEUE_ERROR
;
1476 static int nvme_rdma_poll(struct blk_mq_hw_ctx
*hctx
, unsigned int tag
)
1478 struct nvme_rdma_queue
*queue
= hctx
->driver_data
;
1479 struct ib_cq
*cq
= queue
->ib_cq
;
1483 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
1484 while (ib_poll_cq(cq
, 1, &wc
) > 0) {
1485 struct ib_cqe
*cqe
= wc
.wr_cqe
;
1488 if (cqe
->done
== nvme_rdma_recv_done
)
1489 found
|= __nvme_rdma_recv_done(cq
, &wc
, tag
);
1498 static void nvme_rdma_complete_rq(struct request
*rq
)
1500 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1501 struct nvme_rdma_queue
*queue
= req
->queue
;
1504 nvme_rdma_unmap_data(queue
, rq
);
1506 if (unlikely(rq
->errors
)) {
1507 if (nvme_req_needs_retry(rq
, rq
->errors
)) {
1508 nvme_requeue_req(rq
);
1512 if (rq
->cmd_type
== REQ_TYPE_DRV_PRIV
)
1515 error
= nvme_error_status(rq
->errors
);
1518 blk_mq_end_request(rq
, error
);
1521 static struct blk_mq_ops nvme_rdma_mq_ops
= {
1522 .queue_rq
= nvme_rdma_queue_rq
,
1523 .complete
= nvme_rdma_complete_rq
,
1524 .map_queue
= blk_mq_map_queue
,
1525 .init_request
= nvme_rdma_init_request
,
1526 .exit_request
= nvme_rdma_exit_request
,
1527 .reinit_request
= nvme_rdma_reinit_request
,
1528 .init_hctx
= nvme_rdma_init_hctx
,
1529 .poll
= nvme_rdma_poll
,
1530 .timeout
= nvme_rdma_timeout
,
1533 static struct blk_mq_ops nvme_rdma_admin_mq_ops
= {
1534 .queue_rq
= nvme_rdma_queue_rq
,
1535 .complete
= nvme_rdma_complete_rq
,
1536 .map_queue
= blk_mq_map_queue
,
1537 .init_request
= nvme_rdma_init_admin_request
,
1538 .exit_request
= nvme_rdma_exit_admin_request
,
1539 .reinit_request
= nvme_rdma_reinit_request
,
1540 .init_hctx
= nvme_rdma_init_admin_hctx
,
1541 .timeout
= nvme_rdma_timeout
,
1544 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl
*ctrl
)
1548 error
= nvme_rdma_init_queue(ctrl
, 0, NVMF_AQ_DEPTH
);
1552 ctrl
->device
= ctrl
->queues
[0].device
;
1555 * We need a reference on the device as long as the tag_set is alive,
1556 * as the MRs in the request structures need a valid ib_device.
1559 if (!nvme_rdma_dev_get(ctrl
->device
))
1560 goto out_free_queue
;
1562 ctrl
->max_fr_pages
= min_t(u32
, NVME_RDMA_MAX_SEGMENTS
,
1563 ctrl
->device
->dev
->attrs
.max_fast_reg_page_list_len
);
1565 memset(&ctrl
->admin_tag_set
, 0, sizeof(ctrl
->admin_tag_set
));
1566 ctrl
->admin_tag_set
.ops
= &nvme_rdma_admin_mq_ops
;
1567 ctrl
->admin_tag_set
.queue_depth
= NVME_RDMA_AQ_BLKMQ_DEPTH
;
1568 ctrl
->admin_tag_set
.reserved_tags
= 2; /* connect + keep-alive */
1569 ctrl
->admin_tag_set
.numa_node
= NUMA_NO_NODE
;
1570 ctrl
->admin_tag_set
.cmd_size
= sizeof(struct nvme_rdma_request
) +
1571 SG_CHUNK_SIZE
* sizeof(struct scatterlist
);
1572 ctrl
->admin_tag_set
.driver_data
= ctrl
;
1573 ctrl
->admin_tag_set
.nr_hw_queues
= 1;
1574 ctrl
->admin_tag_set
.timeout
= ADMIN_TIMEOUT
;
1576 error
= blk_mq_alloc_tag_set(&ctrl
->admin_tag_set
);
1580 ctrl
->ctrl
.admin_q
= blk_mq_init_queue(&ctrl
->admin_tag_set
);
1581 if (IS_ERR(ctrl
->ctrl
.admin_q
)) {
1582 error
= PTR_ERR(ctrl
->ctrl
.admin_q
);
1583 goto out_free_tagset
;
1586 error
= nvmf_connect_admin_queue(&ctrl
->ctrl
);
1588 goto out_cleanup_queue
;
1590 error
= nvmf_reg_read64(&ctrl
->ctrl
, NVME_REG_CAP
, &ctrl
->cap
);
1592 dev_err(ctrl
->ctrl
.device
,
1593 "prop_get NVME_REG_CAP failed\n");
1594 goto out_cleanup_queue
;
1598 min_t(int, NVME_CAP_MQES(ctrl
->cap
) + 1, ctrl
->ctrl
.sqsize
);
1600 error
= nvme_enable_ctrl(&ctrl
->ctrl
, ctrl
->cap
);
1602 goto out_cleanup_queue
;
1604 ctrl
->ctrl
.max_hw_sectors
=
1605 (ctrl
->max_fr_pages
- 1) << (PAGE_SHIFT
- 9);
1607 error
= nvme_init_identify(&ctrl
->ctrl
);
1609 goto out_cleanup_queue
;
1611 error
= nvme_rdma_alloc_qe(ctrl
->queues
[0].device
->dev
,
1612 &ctrl
->async_event_sqe
, sizeof(struct nvme_command
),
1615 goto out_cleanup_queue
;
1617 nvme_start_keep_alive(&ctrl
->ctrl
);
1622 blk_cleanup_queue(ctrl
->ctrl
.admin_q
);
1624 /* disconnect and drain the queue before freeing the tagset */
1625 nvme_rdma_stop_queue(&ctrl
->queues
[0]);
1626 blk_mq_free_tag_set(&ctrl
->admin_tag_set
);
1628 nvme_rdma_dev_put(ctrl
->device
);
1630 nvme_rdma_free_queue(&ctrl
->queues
[0]);
1634 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl
*ctrl
)
1636 nvme_stop_keep_alive(&ctrl
->ctrl
);
1637 cancel_work_sync(&ctrl
->err_work
);
1638 cancel_delayed_work_sync(&ctrl
->reconnect_work
);
1640 if (ctrl
->queue_count
> 1) {
1641 nvme_stop_queues(&ctrl
->ctrl
);
1642 blk_mq_tagset_busy_iter(&ctrl
->tag_set
,
1643 nvme_cancel_request
, &ctrl
->ctrl
);
1644 nvme_rdma_free_io_queues(ctrl
);
1647 if (test_bit(NVME_RDMA_Q_CONNECTED
, &ctrl
->queues
[0].flags
))
1648 nvme_shutdown_ctrl(&ctrl
->ctrl
);
1650 blk_mq_stop_hw_queues(ctrl
->ctrl
.admin_q
);
1651 blk_mq_tagset_busy_iter(&ctrl
->admin_tag_set
,
1652 nvme_cancel_request
, &ctrl
->ctrl
);
1653 nvme_rdma_destroy_admin_queue(ctrl
);
1656 static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl
*ctrl
, bool shutdown
)
1658 nvme_uninit_ctrl(&ctrl
->ctrl
);
1660 nvme_rdma_shutdown_ctrl(ctrl
);
1662 if (ctrl
->ctrl
.tagset
) {
1663 blk_cleanup_queue(ctrl
->ctrl
.connect_q
);
1664 blk_mq_free_tag_set(&ctrl
->tag_set
);
1665 nvme_rdma_dev_put(ctrl
->device
);
1668 nvme_put_ctrl(&ctrl
->ctrl
);
1671 static void nvme_rdma_del_ctrl_work(struct work_struct
*work
)
1673 struct nvme_rdma_ctrl
*ctrl
= container_of(work
,
1674 struct nvme_rdma_ctrl
, delete_work
);
1676 __nvme_rdma_remove_ctrl(ctrl
, true);
1679 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl
*ctrl
)
1681 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_DELETING
))
1684 if (!queue_work(nvme_rdma_wq
, &ctrl
->delete_work
))
1690 static int nvme_rdma_del_ctrl(struct nvme_ctrl
*nctrl
)
1692 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(nctrl
);
1695 ret
= __nvme_rdma_del_ctrl(ctrl
);
1699 flush_work(&ctrl
->delete_work
);
1704 static void nvme_rdma_remove_ctrl_work(struct work_struct
*work
)
1706 struct nvme_rdma_ctrl
*ctrl
= container_of(work
,
1707 struct nvme_rdma_ctrl
, delete_work
);
1709 __nvme_rdma_remove_ctrl(ctrl
, false);
1712 static void nvme_rdma_reset_ctrl_work(struct work_struct
*work
)
1714 struct nvme_rdma_ctrl
*ctrl
= container_of(work
,
1715 struct nvme_rdma_ctrl
, reset_work
);
1719 nvme_rdma_shutdown_ctrl(ctrl
);
1721 ret
= nvme_rdma_configure_admin_queue(ctrl
);
1723 /* ctrl is already shutdown, just remove the ctrl */
1724 INIT_WORK(&ctrl
->delete_work
, nvme_rdma_remove_ctrl_work
);
1728 if (ctrl
->queue_count
> 1) {
1729 ret
= blk_mq_reinit_tagset(&ctrl
->tag_set
);
1733 ret
= nvme_rdma_init_io_queues(ctrl
);
1737 ret
= nvme_rdma_connect_io_queues(ctrl
);
1742 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_LIVE
);
1743 WARN_ON_ONCE(!changed
);
1745 if (ctrl
->queue_count
> 1) {
1746 nvme_start_queues(&ctrl
->ctrl
);
1747 nvme_queue_scan(&ctrl
->ctrl
);
1748 nvme_queue_async_events(&ctrl
->ctrl
);
1754 /* Deleting this dead controller... */
1755 dev_warn(ctrl
->ctrl
.device
, "Removing after reset failure\n");
1756 WARN_ON(!queue_work(nvme_rdma_wq
, &ctrl
->delete_work
));
1759 static int nvme_rdma_reset_ctrl(struct nvme_ctrl
*nctrl
)
1761 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(nctrl
);
1763 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_RESETTING
))
1766 if (!queue_work(nvme_rdma_wq
, &ctrl
->reset_work
))
1769 flush_work(&ctrl
->reset_work
);
1774 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops
= {
1776 .module
= THIS_MODULE
,
1778 .reg_read32
= nvmf_reg_read32
,
1779 .reg_read64
= nvmf_reg_read64
,
1780 .reg_write32
= nvmf_reg_write32
,
1781 .reset_ctrl
= nvme_rdma_reset_ctrl
,
1782 .free_ctrl
= nvme_rdma_free_ctrl
,
1783 .submit_async_event
= nvme_rdma_submit_async_event
,
1784 .delete_ctrl
= nvme_rdma_del_ctrl
,
1785 .get_subsysnqn
= nvmf_get_subsysnqn
,
1786 .get_address
= nvmf_get_address
,
1789 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl
*ctrl
)
1791 struct nvmf_ctrl_options
*opts
= ctrl
->ctrl
.opts
;
1794 ret
= nvme_set_queue_count(&ctrl
->ctrl
, &opts
->nr_io_queues
);
1798 ctrl
->queue_count
= opts
->nr_io_queues
+ 1;
1799 if (ctrl
->queue_count
< 2)
1802 dev_info(ctrl
->ctrl
.device
,
1803 "creating %d I/O queues.\n", opts
->nr_io_queues
);
1805 ret
= nvme_rdma_init_io_queues(ctrl
);
1810 * We need a reference on the device as long as the tag_set is alive,
1811 * as the MRs in the request structures need a valid ib_device.
1814 if (!nvme_rdma_dev_get(ctrl
->device
))
1815 goto out_free_io_queues
;
1817 memset(&ctrl
->tag_set
, 0, sizeof(ctrl
->tag_set
));
1818 ctrl
->tag_set
.ops
= &nvme_rdma_mq_ops
;
1819 ctrl
->tag_set
.queue_depth
= ctrl
->ctrl
.sqsize
;
1820 ctrl
->tag_set
.reserved_tags
= 1; /* fabric connect */
1821 ctrl
->tag_set
.numa_node
= NUMA_NO_NODE
;
1822 ctrl
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
;
1823 ctrl
->tag_set
.cmd_size
= sizeof(struct nvme_rdma_request
) +
1824 SG_CHUNK_SIZE
* sizeof(struct scatterlist
);
1825 ctrl
->tag_set
.driver_data
= ctrl
;
1826 ctrl
->tag_set
.nr_hw_queues
= ctrl
->queue_count
- 1;
1827 ctrl
->tag_set
.timeout
= NVME_IO_TIMEOUT
;
1829 ret
= blk_mq_alloc_tag_set(&ctrl
->tag_set
);
1832 ctrl
->ctrl
.tagset
= &ctrl
->tag_set
;
1834 ctrl
->ctrl
.connect_q
= blk_mq_init_queue(&ctrl
->tag_set
);
1835 if (IS_ERR(ctrl
->ctrl
.connect_q
)) {
1836 ret
= PTR_ERR(ctrl
->ctrl
.connect_q
);
1837 goto out_free_tag_set
;
1840 ret
= nvme_rdma_connect_io_queues(ctrl
);
1842 goto out_cleanup_connect_q
;
1846 out_cleanup_connect_q
:
1847 blk_cleanup_queue(ctrl
->ctrl
.connect_q
);
1849 blk_mq_free_tag_set(&ctrl
->tag_set
);
1851 nvme_rdma_dev_put(ctrl
->device
);
1853 nvme_rdma_free_io_queues(ctrl
);
1857 static int nvme_rdma_parse_ipaddr(struct sockaddr_in
*in_addr
, char *p
)
1859 u8
*addr
= (u8
*)&in_addr
->sin_addr
.s_addr
;
1860 size_t buflen
= strlen(p
);
1862 /* XXX: handle IPv6 addresses */
1864 if (buflen
> INET_ADDRSTRLEN
)
1866 if (in4_pton(p
, buflen
, addr
, '\0', NULL
) == 0)
1868 in_addr
->sin_family
= AF_INET
;
1872 static struct nvme_ctrl
*nvme_rdma_create_ctrl(struct device
*dev
,
1873 struct nvmf_ctrl_options
*opts
)
1875 struct nvme_rdma_ctrl
*ctrl
;
1879 ctrl
= kzalloc(sizeof(*ctrl
), GFP_KERNEL
);
1881 return ERR_PTR(-ENOMEM
);
1882 ctrl
->ctrl
.opts
= opts
;
1883 INIT_LIST_HEAD(&ctrl
->list
);
1885 ret
= nvme_rdma_parse_ipaddr(&ctrl
->addr_in
, opts
->traddr
);
1887 pr_err("malformed IP address passed: %s\n", opts
->traddr
);
1891 if (opts
->mask
& NVMF_OPT_TRSVCID
) {
1894 ret
= kstrtou16(opts
->trsvcid
, 0, &port
);
1898 ctrl
->addr_in
.sin_port
= cpu_to_be16(port
);
1900 ctrl
->addr_in
.sin_port
= cpu_to_be16(NVME_RDMA_IP_PORT
);
1903 ret
= nvme_init_ctrl(&ctrl
->ctrl
, dev
, &nvme_rdma_ctrl_ops
,
1904 0 /* no quirks, we're perfect! */);
1908 ctrl
->reconnect_delay
= opts
->reconnect_delay
;
1909 INIT_DELAYED_WORK(&ctrl
->reconnect_work
,
1910 nvme_rdma_reconnect_ctrl_work
);
1911 INIT_WORK(&ctrl
->err_work
, nvme_rdma_error_recovery_work
);
1912 INIT_WORK(&ctrl
->delete_work
, nvme_rdma_del_ctrl_work
);
1913 INIT_WORK(&ctrl
->reset_work
, nvme_rdma_reset_ctrl_work
);
1914 spin_lock_init(&ctrl
->lock
);
1916 ctrl
->queue_count
= opts
->nr_io_queues
+ 1; /* +1 for admin queue */
1917 ctrl
->ctrl
.sqsize
= opts
->queue_size
;
1918 ctrl
->ctrl
.kato
= opts
->kato
;
1921 ctrl
->queues
= kcalloc(ctrl
->queue_count
, sizeof(*ctrl
->queues
),
1924 goto out_uninit_ctrl
;
1926 ret
= nvme_rdma_configure_admin_queue(ctrl
);
1928 goto out_kfree_queues
;
1930 /* sanity check icdoff */
1931 if (ctrl
->ctrl
.icdoff
) {
1932 dev_err(ctrl
->ctrl
.device
, "icdoff is not supported!\n");
1933 goto out_remove_admin_queue
;
1936 /* sanity check keyed sgls */
1937 if (!(ctrl
->ctrl
.sgls
& (1 << 20))) {
1938 dev_err(ctrl
->ctrl
.device
, "Mandatory keyed sgls are not support\n");
1939 goto out_remove_admin_queue
;
1942 if (opts
->queue_size
> ctrl
->ctrl
.maxcmd
) {
1943 /* warn if maxcmd is lower than queue_size */
1944 dev_warn(ctrl
->ctrl
.device
,
1945 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1946 opts
->queue_size
, ctrl
->ctrl
.maxcmd
);
1947 opts
->queue_size
= ctrl
->ctrl
.maxcmd
;
1950 if (opts
->nr_io_queues
) {
1951 ret
= nvme_rdma_create_io_queues(ctrl
);
1953 goto out_remove_admin_queue
;
1956 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_LIVE
);
1957 WARN_ON_ONCE(!changed
);
1959 dev_info(ctrl
->ctrl
.device
, "new ctrl: NQN \"%s\", addr %pISp\n",
1960 ctrl
->ctrl
.opts
->subsysnqn
, &ctrl
->addr
);
1962 kref_get(&ctrl
->ctrl
.kref
);
1964 mutex_lock(&nvme_rdma_ctrl_mutex
);
1965 list_add_tail(&ctrl
->list
, &nvme_rdma_ctrl_list
);
1966 mutex_unlock(&nvme_rdma_ctrl_mutex
);
1968 if (opts
->nr_io_queues
) {
1969 nvme_queue_scan(&ctrl
->ctrl
);
1970 nvme_queue_async_events(&ctrl
->ctrl
);
1975 out_remove_admin_queue
:
1976 nvme_stop_keep_alive(&ctrl
->ctrl
);
1977 nvme_rdma_destroy_admin_queue(ctrl
);
1979 kfree(ctrl
->queues
);
1981 nvme_uninit_ctrl(&ctrl
->ctrl
);
1982 nvme_put_ctrl(&ctrl
->ctrl
);
1985 return ERR_PTR(ret
);
1988 return ERR_PTR(ret
);
1991 static struct nvmf_transport_ops nvme_rdma_transport
= {
1993 .required_opts
= NVMF_OPT_TRADDR
,
1994 .allowed_opts
= NVMF_OPT_TRSVCID
| NVMF_OPT_RECONNECT_DELAY
,
1995 .create_ctrl
= nvme_rdma_create_ctrl
,
1998 static int __init
nvme_rdma_init_module(void)
2000 nvme_rdma_wq
= create_workqueue("nvme_rdma_wq");
2004 nvmf_register_transport(&nvme_rdma_transport
);
2008 static void __exit
nvme_rdma_cleanup_module(void)
2010 struct nvme_rdma_ctrl
*ctrl
;
2012 nvmf_unregister_transport(&nvme_rdma_transport
);
2014 mutex_lock(&nvme_rdma_ctrl_mutex
);
2015 list_for_each_entry(ctrl
, &nvme_rdma_ctrl_list
, list
)
2016 __nvme_rdma_del_ctrl(ctrl
);
2017 mutex_unlock(&nvme_rdma_ctrl_mutex
);
2019 destroy_workqueue(nvme_rdma_wq
);
2022 module_init(nvme_rdma_init_module
);
2023 module_exit(nvme_rdma_cleanup_module
);
2025 MODULE_LICENSE("GPL v2");