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
{
70 struct nvme_rdma_qe sqe
;
71 struct ib_sge sge
[1 + NVME_RDMA_MAX_INLINE_SEGMENTS
];
75 struct ib_reg_wr reg_wr
;
76 struct ib_cqe reg_cqe
;
77 struct nvme_rdma_queue
*queue
;
78 struct sg_table sg_table
;
79 struct scatterlist first_sgl
[];
82 enum nvme_rdma_queue_flags
{
83 NVME_RDMA_Q_CONNECTED
= (1 << 0),
84 NVME_RDMA_IB_QUEUE_ALLOCATED
= (1 << 1),
85 NVME_RDMA_Q_DELETING
= (1 << 2),
88 struct nvme_rdma_queue
{
89 struct nvme_rdma_qe
*rsp_ring
;
92 size_t cmnd_capsule_len
;
93 struct nvme_rdma_ctrl
*ctrl
;
94 struct nvme_rdma_device
*device
;
99 struct rdma_cm_id
*cm_id
;
101 struct completion cm_done
;
104 struct nvme_rdma_ctrl
{
105 /* read and written in the hot path */
108 /* read only in the hot path */
109 struct nvme_rdma_queue
*queues
;
112 /* other member variables */
113 struct blk_mq_tag_set tag_set
;
114 struct work_struct delete_work
;
115 struct work_struct reset_work
;
116 struct work_struct err_work
;
118 struct nvme_rdma_qe async_event_sqe
;
121 struct delayed_work reconnect_work
;
123 struct list_head list
;
125 struct blk_mq_tag_set admin_tag_set
;
126 struct nvme_rdma_device
*device
;
132 struct sockaddr addr
;
133 struct sockaddr_in addr_in
;
136 struct nvme_ctrl ctrl
;
139 static inline struct nvme_rdma_ctrl
*to_rdma_ctrl(struct nvme_ctrl
*ctrl
)
141 return container_of(ctrl
, struct nvme_rdma_ctrl
, ctrl
);
144 static LIST_HEAD(device_list
);
145 static DEFINE_MUTEX(device_list_mutex
);
147 static LIST_HEAD(nvme_rdma_ctrl_list
);
148 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex
);
150 static struct workqueue_struct
*nvme_rdma_wq
;
153 * Disabling this option makes small I/O goes faster, but is fundamentally
154 * unsafe. With it turned off we will have to register a global rkey that
155 * allows read and write access to all physical memory.
157 static bool register_always
= true;
158 module_param(register_always
, bool, 0444);
159 MODULE_PARM_DESC(register_always
,
160 "Use memory registration even for contiguous memory regions");
162 static int nvme_rdma_cm_handler(struct rdma_cm_id
*cm_id
,
163 struct rdma_cm_event
*event
);
164 static void nvme_rdma_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
);
166 /* XXX: really should move to a generic header sooner or later.. */
167 static inline void put_unaligned_le24(u32 val
, u8
*p
)
174 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue
*queue
)
176 return queue
- queue
->ctrl
->queues
;
179 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue
*queue
)
181 return queue
->cmnd_capsule_len
- sizeof(struct nvme_command
);
184 static void nvme_rdma_free_qe(struct ib_device
*ibdev
, struct nvme_rdma_qe
*qe
,
185 size_t capsule_size
, enum dma_data_direction dir
)
187 ib_dma_unmap_single(ibdev
, qe
->dma
, capsule_size
, dir
);
191 static int nvme_rdma_alloc_qe(struct ib_device
*ibdev
, struct nvme_rdma_qe
*qe
,
192 size_t capsule_size
, enum dma_data_direction dir
)
194 qe
->data
= kzalloc(capsule_size
, GFP_KERNEL
);
198 qe
->dma
= ib_dma_map_single(ibdev
, qe
->data
, capsule_size
, dir
);
199 if (ib_dma_mapping_error(ibdev
, qe
->dma
)) {
207 static void nvme_rdma_free_ring(struct ib_device
*ibdev
,
208 struct nvme_rdma_qe
*ring
, size_t ib_queue_size
,
209 size_t capsule_size
, enum dma_data_direction dir
)
213 for (i
= 0; i
< ib_queue_size
; i
++)
214 nvme_rdma_free_qe(ibdev
, &ring
[i
], capsule_size
, dir
);
218 static struct nvme_rdma_qe
*nvme_rdma_alloc_ring(struct ib_device
*ibdev
,
219 size_t ib_queue_size
, size_t capsule_size
,
220 enum dma_data_direction dir
)
222 struct nvme_rdma_qe
*ring
;
225 ring
= kcalloc(ib_queue_size
, sizeof(struct nvme_rdma_qe
), GFP_KERNEL
);
229 for (i
= 0; i
< ib_queue_size
; i
++) {
230 if (nvme_rdma_alloc_qe(ibdev
, &ring
[i
], capsule_size
, dir
))
237 nvme_rdma_free_ring(ibdev
, ring
, i
, capsule_size
, dir
);
241 static void nvme_rdma_qp_event(struct ib_event
*event
, void *context
)
243 pr_debug("QP event %d\n", event
->event
);
246 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue
*queue
)
248 wait_for_completion_interruptible_timeout(&queue
->cm_done
,
249 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS
) + 1);
250 return queue
->cm_error
;
253 static int nvme_rdma_create_qp(struct nvme_rdma_queue
*queue
, const int factor
)
255 struct nvme_rdma_device
*dev
= queue
->device
;
256 struct ib_qp_init_attr init_attr
;
259 memset(&init_attr
, 0, sizeof(init_attr
));
260 init_attr
.event_handler
= nvme_rdma_qp_event
;
262 init_attr
.cap
.max_send_wr
= factor
* queue
->queue_size
+ 1;
264 init_attr
.cap
.max_recv_wr
= queue
->queue_size
+ 1;
265 init_attr
.cap
.max_recv_sge
= 1;
266 init_attr
.cap
.max_send_sge
= 1 + NVME_RDMA_MAX_INLINE_SEGMENTS
;
267 init_attr
.sq_sig_type
= IB_SIGNAL_REQ_WR
;
268 init_attr
.qp_type
= IB_QPT_RC
;
269 init_attr
.send_cq
= queue
->ib_cq
;
270 init_attr
.recv_cq
= queue
->ib_cq
;
272 ret
= rdma_create_qp(queue
->cm_id
, dev
->pd
, &init_attr
);
274 queue
->qp
= queue
->cm_id
->qp
;
278 static int nvme_rdma_reinit_request(void *data
, struct request
*rq
)
280 struct nvme_rdma_ctrl
*ctrl
= data
;
281 struct nvme_rdma_device
*dev
= ctrl
->device
;
282 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
285 if (!req
->mr
->need_inval
)
288 ib_dereg_mr(req
->mr
);
290 req
->mr
= ib_alloc_mr(dev
->pd
, IB_MR_TYPE_MEM_REG
,
292 if (IS_ERR(req
->mr
)) {
293 ret
= PTR_ERR(req
->mr
);
298 req
->mr
->need_inval
= false;
304 static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl
*ctrl
,
305 struct request
*rq
, unsigned int queue_idx
)
307 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
308 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[queue_idx
];
309 struct nvme_rdma_device
*dev
= queue
->device
;
312 ib_dereg_mr(req
->mr
);
314 nvme_rdma_free_qe(dev
->dev
, &req
->sqe
, sizeof(struct nvme_command
),
318 static void nvme_rdma_exit_request(void *data
, struct request
*rq
,
319 unsigned int hctx_idx
, unsigned int rq_idx
)
321 return __nvme_rdma_exit_request(data
, rq
, hctx_idx
+ 1);
324 static void nvme_rdma_exit_admin_request(void *data
, struct request
*rq
,
325 unsigned int hctx_idx
, unsigned int rq_idx
)
327 return __nvme_rdma_exit_request(data
, rq
, 0);
330 static int __nvme_rdma_init_request(struct nvme_rdma_ctrl
*ctrl
,
331 struct request
*rq
, unsigned int queue_idx
)
333 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
334 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[queue_idx
];
335 struct nvme_rdma_device
*dev
= queue
->device
;
336 struct ib_device
*ibdev
= dev
->dev
;
339 BUG_ON(queue_idx
>= ctrl
->queue_count
);
341 ret
= nvme_rdma_alloc_qe(ibdev
, &req
->sqe
, sizeof(struct nvme_command
),
346 req
->mr
= ib_alloc_mr(dev
->pd
, IB_MR_TYPE_MEM_REG
,
348 if (IS_ERR(req
->mr
)) {
349 ret
= PTR_ERR(req
->mr
);
358 nvme_rdma_free_qe(dev
->dev
, &req
->sqe
, sizeof(struct nvme_command
),
363 static int nvme_rdma_init_request(void *data
, struct request
*rq
,
364 unsigned int hctx_idx
, unsigned int rq_idx
,
365 unsigned int numa_node
)
367 return __nvme_rdma_init_request(data
, rq
, hctx_idx
+ 1);
370 static int nvme_rdma_init_admin_request(void *data
, struct request
*rq
,
371 unsigned int hctx_idx
, unsigned int rq_idx
,
372 unsigned int numa_node
)
374 return __nvme_rdma_init_request(data
, rq
, 0);
377 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
378 unsigned int hctx_idx
)
380 struct nvme_rdma_ctrl
*ctrl
= data
;
381 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[hctx_idx
+ 1];
383 BUG_ON(hctx_idx
>= ctrl
->queue_count
);
385 hctx
->driver_data
= queue
;
389 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
390 unsigned int hctx_idx
)
392 struct nvme_rdma_ctrl
*ctrl
= data
;
393 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[0];
395 BUG_ON(hctx_idx
!= 0);
397 hctx
->driver_data
= queue
;
401 static void nvme_rdma_free_dev(struct kref
*ref
)
403 struct nvme_rdma_device
*ndev
=
404 container_of(ref
, struct nvme_rdma_device
, ref
);
406 mutex_lock(&device_list_mutex
);
407 list_del(&ndev
->entry
);
408 mutex_unlock(&device_list_mutex
);
410 ib_dealloc_pd(ndev
->pd
);
414 static void nvme_rdma_dev_put(struct nvme_rdma_device
*dev
)
416 kref_put(&dev
->ref
, nvme_rdma_free_dev
);
419 static int nvme_rdma_dev_get(struct nvme_rdma_device
*dev
)
421 return kref_get_unless_zero(&dev
->ref
);
424 static struct nvme_rdma_device
*
425 nvme_rdma_find_get_device(struct rdma_cm_id
*cm_id
)
427 struct nvme_rdma_device
*ndev
;
429 mutex_lock(&device_list_mutex
);
430 list_for_each_entry(ndev
, &device_list
, entry
) {
431 if (ndev
->dev
->node_guid
== cm_id
->device
->node_guid
&&
432 nvme_rdma_dev_get(ndev
))
436 ndev
= kzalloc(sizeof(*ndev
), GFP_KERNEL
);
440 ndev
->dev
= cm_id
->device
;
441 kref_init(&ndev
->ref
);
443 ndev
->pd
= ib_alloc_pd(ndev
->dev
,
444 register_always
? 0 : IB_PD_UNSAFE_GLOBAL_RKEY
);
445 if (IS_ERR(ndev
->pd
))
448 if (!(ndev
->dev
->attrs
.device_cap_flags
&
449 IB_DEVICE_MEM_MGT_EXTENSIONS
)) {
450 dev_err(&ndev
->dev
->dev
,
451 "Memory registrations not supported.\n");
455 list_add(&ndev
->entry
, &device_list
);
457 mutex_unlock(&device_list_mutex
);
461 ib_dealloc_pd(ndev
->pd
);
465 mutex_unlock(&device_list_mutex
);
469 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue
*queue
)
471 struct nvme_rdma_device
*dev
;
472 struct ib_device
*ibdev
;
474 if (!test_and_clear_bit(NVME_RDMA_IB_QUEUE_ALLOCATED
, &queue
->flags
))
479 rdma_destroy_qp(queue
->cm_id
);
480 ib_free_cq(queue
->ib_cq
);
482 nvme_rdma_free_ring(ibdev
, queue
->rsp_ring
, queue
->queue_size
,
483 sizeof(struct nvme_completion
), DMA_FROM_DEVICE
);
485 nvme_rdma_dev_put(dev
);
488 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue
*queue
,
489 struct nvme_rdma_device
*dev
)
491 struct ib_device
*ibdev
= dev
->dev
;
492 const int send_wr_factor
= 3; /* MR, SEND, INV */
493 const int cq_factor
= send_wr_factor
+ 1; /* + RECV */
494 int comp_vector
, idx
= nvme_rdma_queue_idx(queue
);
501 * The admin queue is barely used once the controller is live, so don't
502 * bother to spread it out.
507 comp_vector
= idx
% ibdev
->num_comp_vectors
;
510 /* +1 for ib_stop_cq */
511 queue
->ib_cq
= ib_alloc_cq(dev
->dev
, queue
,
512 cq_factor
* queue
->queue_size
+ 1, comp_vector
,
514 if (IS_ERR(queue
->ib_cq
)) {
515 ret
= PTR_ERR(queue
->ib_cq
);
519 ret
= nvme_rdma_create_qp(queue
, send_wr_factor
);
521 goto out_destroy_ib_cq
;
523 queue
->rsp_ring
= nvme_rdma_alloc_ring(ibdev
, queue
->queue_size
,
524 sizeof(struct nvme_completion
), DMA_FROM_DEVICE
);
525 if (!queue
->rsp_ring
) {
529 set_bit(NVME_RDMA_IB_QUEUE_ALLOCATED
, &queue
->flags
);
534 ib_destroy_qp(queue
->qp
);
536 ib_free_cq(queue
->ib_cq
);
541 static int nvme_rdma_init_queue(struct nvme_rdma_ctrl
*ctrl
,
542 int idx
, size_t queue_size
)
544 struct nvme_rdma_queue
*queue
;
547 queue
= &ctrl
->queues
[idx
];
549 init_completion(&queue
->cm_done
);
552 queue
->cmnd_capsule_len
= ctrl
->ctrl
.ioccsz
* 16;
554 queue
->cmnd_capsule_len
= sizeof(struct nvme_command
);
556 queue
->queue_size
= queue_size
;
558 queue
->cm_id
= rdma_create_id(&init_net
, nvme_rdma_cm_handler
, queue
,
559 RDMA_PS_TCP
, IB_QPT_RC
);
560 if (IS_ERR(queue
->cm_id
)) {
561 dev_info(ctrl
->ctrl
.device
,
562 "failed to create CM ID: %ld\n", PTR_ERR(queue
->cm_id
));
563 return PTR_ERR(queue
->cm_id
);
566 queue
->cm_error
= -ETIMEDOUT
;
567 ret
= rdma_resolve_addr(queue
->cm_id
, NULL
, &ctrl
->addr
,
568 NVME_RDMA_CONNECT_TIMEOUT_MS
);
570 dev_info(ctrl
->ctrl
.device
,
571 "rdma_resolve_addr failed (%d).\n", ret
);
572 goto out_destroy_cm_id
;
575 ret
= nvme_rdma_wait_for_cm(queue
);
577 dev_info(ctrl
->ctrl
.device
,
578 "rdma_resolve_addr wait failed (%d).\n", ret
);
579 goto out_destroy_cm_id
;
582 clear_bit(NVME_RDMA_Q_DELETING
, &queue
->flags
);
583 set_bit(NVME_RDMA_Q_CONNECTED
, &queue
->flags
);
588 nvme_rdma_destroy_queue_ib(queue
);
589 rdma_destroy_id(queue
->cm_id
);
593 static void nvme_rdma_stop_queue(struct nvme_rdma_queue
*queue
)
595 rdma_disconnect(queue
->cm_id
);
596 ib_drain_qp(queue
->qp
);
599 static void nvme_rdma_free_queue(struct nvme_rdma_queue
*queue
)
601 nvme_rdma_destroy_queue_ib(queue
);
602 rdma_destroy_id(queue
->cm_id
);
605 static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue
*queue
)
607 if (test_and_set_bit(NVME_RDMA_Q_DELETING
, &queue
->flags
))
609 nvme_rdma_stop_queue(queue
);
610 nvme_rdma_free_queue(queue
);
613 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl
*ctrl
)
617 for (i
= 1; i
< ctrl
->queue_count
; i
++)
618 nvme_rdma_stop_and_free_queue(&ctrl
->queues
[i
]);
621 static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl
*ctrl
)
625 for (i
= 1; i
< ctrl
->queue_count
; i
++) {
626 ret
= nvmf_connect_io_queue(&ctrl
->ctrl
, i
);
634 static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl
*ctrl
)
638 for (i
= 1; i
< ctrl
->queue_count
; i
++) {
639 ret
= nvme_rdma_init_queue(ctrl
, i
,
640 ctrl
->ctrl
.opts
->queue_size
);
642 dev_info(ctrl
->ctrl
.device
,
643 "failed to initialize i/o queue: %d\n", ret
);
644 goto out_free_queues
;
651 for (i
--; i
>= 1; i
--)
652 nvme_rdma_stop_and_free_queue(&ctrl
->queues
[i
]);
657 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl
*ctrl
)
659 nvme_rdma_free_qe(ctrl
->queues
[0].device
->dev
, &ctrl
->async_event_sqe
,
660 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
661 nvme_rdma_stop_and_free_queue(&ctrl
->queues
[0]);
662 blk_cleanup_queue(ctrl
->ctrl
.admin_q
);
663 blk_mq_free_tag_set(&ctrl
->admin_tag_set
);
664 nvme_rdma_dev_put(ctrl
->device
);
667 static void nvme_rdma_free_ctrl(struct nvme_ctrl
*nctrl
)
669 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(nctrl
);
671 if (list_empty(&ctrl
->list
))
674 mutex_lock(&nvme_rdma_ctrl_mutex
);
675 list_del(&ctrl
->list
);
676 mutex_unlock(&nvme_rdma_ctrl_mutex
);
679 nvmf_free_options(nctrl
->opts
);
684 static void nvme_rdma_reconnect_ctrl_work(struct work_struct
*work
)
686 struct nvme_rdma_ctrl
*ctrl
= container_of(to_delayed_work(work
),
687 struct nvme_rdma_ctrl
, reconnect_work
);
691 if (ctrl
->queue_count
> 1) {
692 nvme_rdma_free_io_queues(ctrl
);
694 ret
= blk_mq_reinit_tagset(&ctrl
->tag_set
);
699 nvme_rdma_stop_and_free_queue(&ctrl
->queues
[0]);
701 ret
= blk_mq_reinit_tagset(&ctrl
->admin_tag_set
);
705 ret
= nvme_rdma_init_queue(ctrl
, 0, NVMF_AQ_DEPTH
);
709 blk_mq_start_stopped_hw_queues(ctrl
->ctrl
.admin_q
, true);
711 ret
= nvmf_connect_admin_queue(&ctrl
->ctrl
);
715 ret
= nvme_enable_ctrl(&ctrl
->ctrl
, ctrl
->cap
);
719 nvme_start_keep_alive(&ctrl
->ctrl
);
721 if (ctrl
->queue_count
> 1) {
722 ret
= nvme_rdma_init_io_queues(ctrl
);
726 ret
= nvme_rdma_connect_io_queues(ctrl
);
731 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_LIVE
);
732 WARN_ON_ONCE(!changed
);
734 if (ctrl
->queue_count
> 1) {
735 nvme_start_queues(&ctrl
->ctrl
);
736 nvme_queue_scan(&ctrl
->ctrl
);
737 nvme_queue_async_events(&ctrl
->ctrl
);
740 dev_info(ctrl
->ctrl
.device
, "Successfully reconnected\n");
745 blk_mq_stop_hw_queues(ctrl
->ctrl
.admin_q
);
747 /* Make sure we are not resetting/deleting */
748 if (ctrl
->ctrl
.state
== NVME_CTRL_RECONNECTING
) {
749 dev_info(ctrl
->ctrl
.device
,
750 "Failed reconnect attempt, requeueing...\n");
751 queue_delayed_work(nvme_rdma_wq
, &ctrl
->reconnect_work
,
752 ctrl
->reconnect_delay
* HZ
);
756 static void nvme_rdma_error_recovery_work(struct work_struct
*work
)
758 struct nvme_rdma_ctrl
*ctrl
= container_of(work
,
759 struct nvme_rdma_ctrl
, err_work
);
762 nvme_stop_keep_alive(&ctrl
->ctrl
);
764 for (i
= 0; i
< ctrl
->queue_count
; i
++)
765 clear_bit(NVME_RDMA_Q_CONNECTED
, &ctrl
->queues
[i
].flags
);
767 if (ctrl
->queue_count
> 1)
768 nvme_stop_queues(&ctrl
->ctrl
);
769 blk_mq_stop_hw_queues(ctrl
->ctrl
.admin_q
);
771 /* We must take care of fastfail/requeue all our inflight requests */
772 if (ctrl
->queue_count
> 1)
773 blk_mq_tagset_busy_iter(&ctrl
->tag_set
,
774 nvme_cancel_request
, &ctrl
->ctrl
);
775 blk_mq_tagset_busy_iter(&ctrl
->admin_tag_set
,
776 nvme_cancel_request
, &ctrl
->ctrl
);
778 dev_info(ctrl
->ctrl
.device
, "reconnecting in %d seconds\n",
779 ctrl
->reconnect_delay
);
781 queue_delayed_work(nvme_rdma_wq
, &ctrl
->reconnect_work
,
782 ctrl
->reconnect_delay
* HZ
);
785 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl
*ctrl
)
787 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_RECONNECTING
))
790 queue_work(nvme_rdma_wq
, &ctrl
->err_work
);
793 static void nvme_rdma_wr_error(struct ib_cq
*cq
, struct ib_wc
*wc
,
796 struct nvme_rdma_queue
*queue
= cq
->cq_context
;
797 struct nvme_rdma_ctrl
*ctrl
= queue
->ctrl
;
799 if (ctrl
->ctrl
.state
== NVME_CTRL_LIVE
)
800 dev_info(ctrl
->ctrl
.device
,
801 "%s for CQE 0x%p failed with status %s (%d)\n",
803 ib_wc_status_msg(wc
->status
), wc
->status
);
804 nvme_rdma_error_recovery(ctrl
);
807 static void nvme_rdma_memreg_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
809 if (unlikely(wc
->status
!= IB_WC_SUCCESS
))
810 nvme_rdma_wr_error(cq
, wc
, "MEMREG");
813 static void nvme_rdma_inv_rkey_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
815 if (unlikely(wc
->status
!= IB_WC_SUCCESS
))
816 nvme_rdma_wr_error(cq
, wc
, "LOCAL_INV");
819 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue
*queue
,
820 struct nvme_rdma_request
*req
)
822 struct ib_send_wr
*bad_wr
;
823 struct ib_send_wr wr
= {
824 .opcode
= IB_WR_LOCAL_INV
,
828 .ex
.invalidate_rkey
= req
->mr
->rkey
,
831 req
->reg_cqe
.done
= nvme_rdma_inv_rkey_done
;
832 wr
.wr_cqe
= &req
->reg_cqe
;
834 return ib_post_send(queue
->qp
, &wr
, &bad_wr
);
837 static void nvme_rdma_unmap_data(struct nvme_rdma_queue
*queue
,
840 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
841 struct nvme_rdma_ctrl
*ctrl
= queue
->ctrl
;
842 struct nvme_rdma_device
*dev
= queue
->device
;
843 struct ib_device
*ibdev
= dev
->dev
;
846 if (!blk_rq_bytes(rq
))
849 if (req
->mr
->need_inval
) {
850 res
= nvme_rdma_inv_rkey(queue
, req
);
852 dev_err(ctrl
->ctrl
.device
,
853 "Queueing INV WR for rkey %#x failed (%d)\n",
855 nvme_rdma_error_recovery(queue
->ctrl
);
859 ib_dma_unmap_sg(ibdev
, req
->sg_table
.sgl
,
860 req
->nents
, rq_data_dir(rq
) ==
861 WRITE
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
863 nvme_cleanup_cmd(rq
);
864 sg_free_table_chained(&req
->sg_table
, true);
867 static int nvme_rdma_set_sg_null(struct nvme_command
*c
)
869 struct nvme_keyed_sgl_desc
*sg
= &c
->common
.dptr
.ksgl
;
872 put_unaligned_le24(0, sg
->length
);
873 put_unaligned_le32(0, sg
->key
);
874 sg
->type
= NVME_KEY_SGL_FMT_DATA_DESC
<< 4;
878 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue
*queue
,
879 struct nvme_rdma_request
*req
, struct nvme_command
*c
)
881 struct nvme_sgl_desc
*sg
= &c
->common
.dptr
.sgl
;
883 req
->sge
[1].addr
= sg_dma_address(req
->sg_table
.sgl
);
884 req
->sge
[1].length
= sg_dma_len(req
->sg_table
.sgl
);
885 req
->sge
[1].lkey
= queue
->device
->pd
->local_dma_lkey
;
887 sg
->addr
= cpu_to_le64(queue
->ctrl
->ctrl
.icdoff
);
888 sg
->length
= cpu_to_le32(sg_dma_len(req
->sg_table
.sgl
));
889 sg
->type
= (NVME_SGL_FMT_DATA_DESC
<< 4) | NVME_SGL_FMT_OFFSET
;
891 req
->inline_data
= true;
896 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue
*queue
,
897 struct nvme_rdma_request
*req
, struct nvme_command
*c
)
899 struct nvme_keyed_sgl_desc
*sg
= &c
->common
.dptr
.ksgl
;
901 sg
->addr
= cpu_to_le64(sg_dma_address(req
->sg_table
.sgl
));
902 put_unaligned_le24(sg_dma_len(req
->sg_table
.sgl
), sg
->length
);
903 put_unaligned_le32(queue
->device
->pd
->unsafe_global_rkey
, sg
->key
);
904 sg
->type
= NVME_KEY_SGL_FMT_DATA_DESC
<< 4;
908 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue
*queue
,
909 struct nvme_rdma_request
*req
, struct nvme_command
*c
,
912 struct nvme_keyed_sgl_desc
*sg
= &c
->common
.dptr
.ksgl
;
915 nr
= ib_map_mr_sg(req
->mr
, req
->sg_table
.sgl
, count
, NULL
, PAGE_SIZE
);
922 ib_update_fast_reg_key(req
->mr
, ib_inc_rkey(req
->mr
->rkey
));
924 req
->reg_cqe
.done
= nvme_rdma_memreg_done
;
925 memset(&req
->reg_wr
, 0, sizeof(req
->reg_wr
));
926 req
->reg_wr
.wr
.opcode
= IB_WR_REG_MR
;
927 req
->reg_wr
.wr
.wr_cqe
= &req
->reg_cqe
;
928 req
->reg_wr
.wr
.num_sge
= 0;
929 req
->reg_wr
.mr
= req
->mr
;
930 req
->reg_wr
.key
= req
->mr
->rkey
;
931 req
->reg_wr
.access
= IB_ACCESS_LOCAL_WRITE
|
932 IB_ACCESS_REMOTE_READ
|
933 IB_ACCESS_REMOTE_WRITE
;
935 req
->mr
->need_inval
= true;
937 sg
->addr
= cpu_to_le64(req
->mr
->iova
);
938 put_unaligned_le24(req
->mr
->length
, sg
->length
);
939 put_unaligned_le32(req
->mr
->rkey
, sg
->key
);
940 sg
->type
= (NVME_KEY_SGL_FMT_DATA_DESC
<< 4) |
941 NVME_SGL_FMT_INVALIDATE
;
946 static int nvme_rdma_map_data(struct nvme_rdma_queue
*queue
,
947 struct request
*rq
, unsigned int map_len
,
948 struct nvme_command
*c
)
950 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
951 struct nvme_rdma_device
*dev
= queue
->device
;
952 struct ib_device
*ibdev
= dev
->dev
;
957 req
->inline_data
= false;
958 req
->mr
->need_inval
= false;
960 c
->common
.flags
|= NVME_CMD_SGL_METABUF
;
962 if (!blk_rq_bytes(rq
))
963 return nvme_rdma_set_sg_null(c
);
965 req
->sg_table
.sgl
= req
->first_sgl
;
966 ret
= sg_alloc_table_chained(&req
->sg_table
, rq
->nr_phys_segments
,
971 nents
= blk_rq_map_sg(rq
->q
, rq
, req
->sg_table
.sgl
);
972 BUG_ON(nents
> rq
->nr_phys_segments
);
975 count
= ib_dma_map_sg(ibdev
, req
->sg_table
.sgl
, nents
,
976 rq_data_dir(rq
) == WRITE
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
977 if (unlikely(count
<= 0)) {
978 sg_free_table_chained(&req
->sg_table
, true);
983 if (rq_data_dir(rq
) == WRITE
&&
984 map_len
<= nvme_rdma_inline_data_size(queue
) &&
985 nvme_rdma_queue_idx(queue
))
986 return nvme_rdma_map_sg_inline(queue
, req
, c
);
988 if (dev
->pd
->flags
& IB_PD_UNSAFE_GLOBAL_RKEY
)
989 return nvme_rdma_map_sg_single(queue
, req
, c
);
992 return nvme_rdma_map_sg_fr(queue
, req
, c
, count
);
995 static void nvme_rdma_send_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
997 if (unlikely(wc
->status
!= IB_WC_SUCCESS
))
998 nvme_rdma_wr_error(cq
, wc
, "SEND");
1001 static int nvme_rdma_post_send(struct nvme_rdma_queue
*queue
,
1002 struct nvme_rdma_qe
*qe
, struct ib_sge
*sge
, u32 num_sge
,
1003 struct ib_send_wr
*first
, bool flush
)
1005 struct ib_send_wr wr
, *bad_wr
;
1008 sge
->addr
= qe
->dma
;
1009 sge
->length
= sizeof(struct nvme_command
),
1010 sge
->lkey
= queue
->device
->pd
->local_dma_lkey
;
1012 qe
->cqe
.done
= nvme_rdma_send_done
;
1015 wr
.wr_cqe
= &qe
->cqe
;
1017 wr
.num_sge
= num_sge
;
1018 wr
.opcode
= IB_WR_SEND
;
1022 * Unsignalled send completions are another giant desaster in the
1023 * IB Verbs spec: If we don't regularly post signalled sends
1024 * the send queue will fill up and only a QP reset will rescue us.
1025 * Would have been way to obvious to handle this in hardware or
1026 * at least the RDMA stack..
1028 * This messy and racy code sniplet is copy and pasted from the iSER
1029 * initiator, and the magic '32' comes from there as well.
1031 * Always signal the flushes. The magic request used for the flush
1032 * sequencer is not allocated in our driver's tagset and it's
1033 * triggered to be freed by blk_cleanup_queue(). So we need to
1034 * always mark it as signaled to ensure that the "wr_cqe", which is
1035 * embeded in request's payload, is not freed when __ib_process_cq()
1036 * calls wr_cqe->done().
1038 if ((++queue
->sig_count
% 32) == 0 || flush
)
1039 wr
.send_flags
|= IB_SEND_SIGNALED
;
1046 ret
= ib_post_send(queue
->qp
, first
, &bad_wr
);
1048 dev_err(queue
->ctrl
->ctrl
.device
,
1049 "%s failed with error code %d\n", __func__
, ret
);
1054 static int nvme_rdma_post_recv(struct nvme_rdma_queue
*queue
,
1055 struct nvme_rdma_qe
*qe
)
1057 struct ib_recv_wr wr
, *bad_wr
;
1061 list
.addr
= qe
->dma
;
1062 list
.length
= sizeof(struct nvme_completion
);
1063 list
.lkey
= queue
->device
->pd
->local_dma_lkey
;
1065 qe
->cqe
.done
= nvme_rdma_recv_done
;
1068 wr
.wr_cqe
= &qe
->cqe
;
1072 ret
= ib_post_recv(queue
->qp
, &wr
, &bad_wr
);
1074 dev_err(queue
->ctrl
->ctrl
.device
,
1075 "%s failed with error code %d\n", __func__
, ret
);
1080 static struct blk_mq_tags
*nvme_rdma_tagset(struct nvme_rdma_queue
*queue
)
1082 u32 queue_idx
= nvme_rdma_queue_idx(queue
);
1085 return queue
->ctrl
->admin_tag_set
.tags
[queue_idx
];
1086 return queue
->ctrl
->tag_set
.tags
[queue_idx
- 1];
1089 static void nvme_rdma_submit_async_event(struct nvme_ctrl
*arg
, int aer_idx
)
1091 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(arg
);
1092 struct nvme_rdma_queue
*queue
= &ctrl
->queues
[0];
1093 struct ib_device
*dev
= queue
->device
->dev
;
1094 struct nvme_rdma_qe
*sqe
= &ctrl
->async_event_sqe
;
1095 struct nvme_command
*cmd
= sqe
->data
;
1099 if (WARN_ON_ONCE(aer_idx
!= 0))
1102 ib_dma_sync_single_for_cpu(dev
, sqe
->dma
, sizeof(*cmd
), DMA_TO_DEVICE
);
1104 memset(cmd
, 0, sizeof(*cmd
));
1105 cmd
->common
.opcode
= nvme_admin_async_event
;
1106 cmd
->common
.command_id
= NVME_RDMA_AQ_BLKMQ_DEPTH
;
1107 cmd
->common
.flags
|= NVME_CMD_SGL_METABUF
;
1108 nvme_rdma_set_sg_null(cmd
);
1110 ib_dma_sync_single_for_device(dev
, sqe
->dma
, sizeof(*cmd
),
1113 ret
= nvme_rdma_post_send(queue
, sqe
, &sge
, 1, NULL
, false);
1117 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue
*queue
,
1118 struct nvme_completion
*cqe
, struct ib_wc
*wc
, int tag
)
1120 u16 status
= le16_to_cpu(cqe
->status
);
1122 struct nvme_rdma_request
*req
;
1127 rq
= blk_mq_tag_to_rq(nvme_rdma_tagset(queue
), cqe
->command_id
);
1129 dev_err(queue
->ctrl
->ctrl
.device
,
1130 "tag 0x%x on QP %#x not found\n",
1131 cqe
->command_id
, queue
->qp
->qp_num
);
1132 nvme_rdma_error_recovery(queue
->ctrl
);
1135 req
= blk_mq_rq_to_pdu(rq
);
1137 if (rq
->cmd_type
== REQ_TYPE_DRV_PRIV
&& rq
->special
)
1138 memcpy(rq
->special
, cqe
, sizeof(*cqe
));
1143 if ((wc
->wc_flags
& IB_WC_WITH_INVALIDATE
) &&
1144 wc
->ex
.invalidate_rkey
== req
->mr
->rkey
)
1145 req
->mr
->need_inval
= false;
1147 blk_mq_complete_request(rq
, status
);
1152 static int __nvme_rdma_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
, int tag
)
1154 struct nvme_rdma_qe
*qe
=
1155 container_of(wc
->wr_cqe
, struct nvme_rdma_qe
, cqe
);
1156 struct nvme_rdma_queue
*queue
= cq
->cq_context
;
1157 struct ib_device
*ibdev
= queue
->device
->dev
;
1158 struct nvme_completion
*cqe
= qe
->data
;
1159 const size_t len
= sizeof(struct nvme_completion
);
1162 if (unlikely(wc
->status
!= IB_WC_SUCCESS
)) {
1163 nvme_rdma_wr_error(cq
, wc
, "RECV");
1167 ib_dma_sync_single_for_cpu(ibdev
, qe
->dma
, len
, DMA_FROM_DEVICE
);
1169 * AEN requests are special as they don't time out and can
1170 * survive any kind of queue freeze and often don't respond to
1171 * aborts. We don't even bother to allocate a struct request
1172 * for them but rather special case them here.
1174 if (unlikely(nvme_rdma_queue_idx(queue
) == 0 &&
1175 cqe
->command_id
>= NVME_RDMA_AQ_BLKMQ_DEPTH
))
1176 nvme_complete_async_event(&queue
->ctrl
->ctrl
, cqe
);
1178 ret
= nvme_rdma_process_nvme_rsp(queue
, cqe
, wc
, tag
);
1179 ib_dma_sync_single_for_device(ibdev
, qe
->dma
, len
, DMA_FROM_DEVICE
);
1181 nvme_rdma_post_recv(queue
, qe
);
1185 static void nvme_rdma_recv_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
1187 __nvme_rdma_recv_done(cq
, wc
, -1);
1190 static int nvme_rdma_conn_established(struct nvme_rdma_queue
*queue
)
1194 for (i
= 0; i
< queue
->queue_size
; i
++) {
1195 ret
= nvme_rdma_post_recv(queue
, &queue
->rsp_ring
[i
]);
1197 goto out_destroy_queue_ib
;
1202 out_destroy_queue_ib
:
1203 nvme_rdma_destroy_queue_ib(queue
);
1207 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue
*queue
,
1208 struct rdma_cm_event
*ev
)
1210 if (ev
->param
.conn
.private_data_len
) {
1211 struct nvme_rdma_cm_rej
*rej
=
1212 (struct nvme_rdma_cm_rej
*)ev
->param
.conn
.private_data
;
1214 dev_err(queue
->ctrl
->ctrl
.device
,
1215 "Connect rejected, status %d.", le16_to_cpu(rej
->sts
));
1216 /* XXX: Think of something clever to do here... */
1218 dev_err(queue
->ctrl
->ctrl
.device
,
1219 "Connect rejected, no private data.\n");
1225 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue
*queue
)
1227 struct nvme_rdma_device
*dev
;
1230 dev
= nvme_rdma_find_get_device(queue
->cm_id
);
1232 dev_err(queue
->cm_id
->device
->dma_device
,
1233 "no client data found!\n");
1234 return -ECONNREFUSED
;
1237 ret
= nvme_rdma_create_queue_ib(queue
, dev
);
1239 nvme_rdma_dev_put(dev
);
1243 ret
= rdma_resolve_route(queue
->cm_id
, NVME_RDMA_CONNECT_TIMEOUT_MS
);
1245 dev_err(queue
->ctrl
->ctrl
.device
,
1246 "rdma_resolve_route failed (%d).\n",
1248 goto out_destroy_queue
;
1254 nvme_rdma_destroy_queue_ib(queue
);
1259 static int nvme_rdma_route_resolved(struct nvme_rdma_queue
*queue
)
1261 struct nvme_rdma_ctrl
*ctrl
= queue
->ctrl
;
1262 struct rdma_conn_param param
= { };
1263 struct nvme_rdma_cm_req priv
= { };
1266 param
.qp_num
= queue
->qp
->qp_num
;
1267 param
.flow_control
= 1;
1269 param
.responder_resources
= queue
->device
->dev
->attrs
.max_qp_rd_atom
;
1270 /* maximum retry count */
1271 param
.retry_count
= 7;
1272 param
.rnr_retry_count
= 7;
1273 param
.private_data
= &priv
;
1274 param
.private_data_len
= sizeof(priv
);
1276 priv
.recfmt
= cpu_to_le16(NVME_RDMA_CM_FMT_1_0
);
1277 priv
.qid
= cpu_to_le16(nvme_rdma_queue_idx(queue
));
1279 * set the admin queue depth to the minimum size
1280 * specified by the Fabrics standard.
1282 if (priv
.qid
== 0) {
1283 priv
.hrqsize
= cpu_to_le16(NVMF_AQ_DEPTH
);
1284 priv
.hsqsize
= cpu_to_le16(NVMF_AQ_DEPTH
- 1);
1287 * current interpretation of the fabrics spec
1288 * is at minimum you make hrqsize sqsize+1, or a
1289 * 1's based representation of sqsize.
1291 priv
.hrqsize
= cpu_to_le16(queue
->queue_size
);
1292 priv
.hsqsize
= cpu_to_le16(queue
->ctrl
->ctrl
.sqsize
);
1295 ret
= rdma_connect(queue
->cm_id
, ¶m
);
1297 dev_err(ctrl
->ctrl
.device
,
1298 "rdma_connect failed (%d).\n", ret
);
1299 goto out_destroy_queue_ib
;
1304 out_destroy_queue_ib
:
1305 nvme_rdma_destroy_queue_ib(queue
);
1309 static int nvme_rdma_cm_handler(struct rdma_cm_id
*cm_id
,
1310 struct rdma_cm_event
*ev
)
1312 struct nvme_rdma_queue
*queue
= cm_id
->context
;
1315 dev_dbg(queue
->ctrl
->ctrl
.device
, "%s (%d): status %d id %p\n",
1316 rdma_event_msg(ev
->event
), ev
->event
,
1319 switch (ev
->event
) {
1320 case RDMA_CM_EVENT_ADDR_RESOLVED
:
1321 cm_error
= nvme_rdma_addr_resolved(queue
);
1323 case RDMA_CM_EVENT_ROUTE_RESOLVED
:
1324 cm_error
= nvme_rdma_route_resolved(queue
);
1326 case RDMA_CM_EVENT_ESTABLISHED
:
1327 queue
->cm_error
= nvme_rdma_conn_established(queue
);
1328 /* complete cm_done regardless of success/failure */
1329 complete(&queue
->cm_done
);
1331 case RDMA_CM_EVENT_REJECTED
:
1332 cm_error
= nvme_rdma_conn_rejected(queue
, ev
);
1334 case RDMA_CM_EVENT_ADDR_ERROR
:
1335 case RDMA_CM_EVENT_ROUTE_ERROR
:
1336 case RDMA_CM_EVENT_CONNECT_ERROR
:
1337 case RDMA_CM_EVENT_UNREACHABLE
:
1338 dev_dbg(queue
->ctrl
->ctrl
.device
,
1339 "CM error event %d\n", ev
->event
);
1340 cm_error
= -ECONNRESET
;
1342 case RDMA_CM_EVENT_DISCONNECTED
:
1343 case RDMA_CM_EVENT_ADDR_CHANGE
:
1344 case RDMA_CM_EVENT_TIMEWAIT_EXIT
:
1345 dev_dbg(queue
->ctrl
->ctrl
.device
,
1346 "disconnect received - connection closed\n");
1347 nvme_rdma_error_recovery(queue
->ctrl
);
1349 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
1350 /* device removal is handled via the ib_client API */
1353 dev_err(queue
->ctrl
->ctrl
.device
,
1354 "Unexpected RDMA CM event (%d)\n", ev
->event
);
1355 nvme_rdma_error_recovery(queue
->ctrl
);
1360 queue
->cm_error
= cm_error
;
1361 complete(&queue
->cm_done
);
1367 static enum blk_eh_timer_return
1368 nvme_rdma_timeout(struct request
*rq
, bool reserved
)
1370 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1372 /* queue error recovery */
1373 nvme_rdma_error_recovery(req
->queue
->ctrl
);
1375 /* fail with DNR on cmd timeout */
1376 rq
->errors
= NVME_SC_ABORT_REQ
| NVME_SC_DNR
;
1378 return BLK_EH_HANDLED
;
1381 static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx
*hctx
,
1382 const struct blk_mq_queue_data
*bd
)
1384 struct nvme_ns
*ns
= hctx
->queue
->queuedata
;
1385 struct nvme_rdma_queue
*queue
= hctx
->driver_data
;
1386 struct request
*rq
= bd
->rq
;
1387 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1388 struct nvme_rdma_qe
*sqe
= &req
->sqe
;
1389 struct nvme_command
*c
= sqe
->data
;
1391 struct ib_device
*dev
;
1392 unsigned int map_len
;
1395 WARN_ON_ONCE(rq
->tag
< 0);
1397 dev
= queue
->device
->dev
;
1398 ib_dma_sync_single_for_cpu(dev
, sqe
->dma
,
1399 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
1401 ret
= nvme_setup_cmd(ns
, rq
, c
);
1405 c
->common
.command_id
= rq
->tag
;
1406 blk_mq_start_request(rq
);
1408 map_len
= nvme_map_len(rq
);
1409 ret
= nvme_rdma_map_data(queue
, rq
, map_len
, c
);
1411 dev_err(queue
->ctrl
->ctrl
.device
,
1412 "Failed to map data (%d)\n", ret
);
1413 nvme_cleanup_cmd(rq
);
1417 ib_dma_sync_single_for_device(dev
, sqe
->dma
,
1418 sizeof(struct nvme_command
), DMA_TO_DEVICE
);
1420 if (rq
->cmd_type
== REQ_TYPE_FS
&& req_op(rq
) == REQ_OP_FLUSH
)
1422 ret
= nvme_rdma_post_send(queue
, sqe
, req
->sge
, req
->num_sge
,
1423 req
->mr
->need_inval
? &req
->reg_wr
.wr
: NULL
, flush
);
1425 nvme_rdma_unmap_data(queue
, rq
);
1429 return BLK_MQ_RQ_QUEUE_OK
;
1431 return (ret
== -ENOMEM
|| ret
== -EAGAIN
) ?
1432 BLK_MQ_RQ_QUEUE_BUSY
: BLK_MQ_RQ_QUEUE_ERROR
;
1435 static int nvme_rdma_poll(struct blk_mq_hw_ctx
*hctx
, unsigned int tag
)
1437 struct nvme_rdma_queue
*queue
= hctx
->driver_data
;
1438 struct ib_cq
*cq
= queue
->ib_cq
;
1442 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
1443 while (ib_poll_cq(cq
, 1, &wc
) > 0) {
1444 struct ib_cqe
*cqe
= wc
.wr_cqe
;
1447 if (cqe
->done
== nvme_rdma_recv_done
)
1448 found
|= __nvme_rdma_recv_done(cq
, &wc
, tag
);
1457 static void nvme_rdma_complete_rq(struct request
*rq
)
1459 struct nvme_rdma_request
*req
= blk_mq_rq_to_pdu(rq
);
1460 struct nvme_rdma_queue
*queue
= req
->queue
;
1463 nvme_rdma_unmap_data(queue
, rq
);
1465 if (unlikely(rq
->errors
)) {
1466 if (nvme_req_needs_retry(rq
, rq
->errors
)) {
1467 nvme_requeue_req(rq
);
1471 if (rq
->cmd_type
== REQ_TYPE_DRV_PRIV
)
1474 error
= nvme_error_status(rq
->errors
);
1477 blk_mq_end_request(rq
, error
);
1480 static struct blk_mq_ops nvme_rdma_mq_ops
= {
1481 .queue_rq
= nvme_rdma_queue_rq
,
1482 .complete
= nvme_rdma_complete_rq
,
1483 .map_queue
= blk_mq_map_queue
,
1484 .init_request
= nvme_rdma_init_request
,
1485 .exit_request
= nvme_rdma_exit_request
,
1486 .reinit_request
= nvme_rdma_reinit_request
,
1487 .init_hctx
= nvme_rdma_init_hctx
,
1488 .poll
= nvme_rdma_poll
,
1489 .timeout
= nvme_rdma_timeout
,
1492 static struct blk_mq_ops nvme_rdma_admin_mq_ops
= {
1493 .queue_rq
= nvme_rdma_queue_rq
,
1494 .complete
= nvme_rdma_complete_rq
,
1495 .map_queue
= blk_mq_map_queue
,
1496 .init_request
= nvme_rdma_init_admin_request
,
1497 .exit_request
= nvme_rdma_exit_admin_request
,
1498 .reinit_request
= nvme_rdma_reinit_request
,
1499 .init_hctx
= nvme_rdma_init_admin_hctx
,
1500 .timeout
= nvme_rdma_timeout
,
1503 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl
*ctrl
)
1507 error
= nvme_rdma_init_queue(ctrl
, 0, NVMF_AQ_DEPTH
);
1511 ctrl
->device
= ctrl
->queues
[0].device
;
1514 * We need a reference on the device as long as the tag_set is alive,
1515 * as the MRs in the request structures need a valid ib_device.
1518 if (!nvme_rdma_dev_get(ctrl
->device
))
1519 goto out_free_queue
;
1521 ctrl
->max_fr_pages
= min_t(u32
, NVME_RDMA_MAX_SEGMENTS
,
1522 ctrl
->device
->dev
->attrs
.max_fast_reg_page_list_len
);
1524 memset(&ctrl
->admin_tag_set
, 0, sizeof(ctrl
->admin_tag_set
));
1525 ctrl
->admin_tag_set
.ops
= &nvme_rdma_admin_mq_ops
;
1526 ctrl
->admin_tag_set
.queue_depth
= NVME_RDMA_AQ_BLKMQ_DEPTH
;
1527 ctrl
->admin_tag_set
.reserved_tags
= 2; /* connect + keep-alive */
1528 ctrl
->admin_tag_set
.numa_node
= NUMA_NO_NODE
;
1529 ctrl
->admin_tag_set
.cmd_size
= sizeof(struct nvme_rdma_request
) +
1530 SG_CHUNK_SIZE
* sizeof(struct scatterlist
);
1531 ctrl
->admin_tag_set
.driver_data
= ctrl
;
1532 ctrl
->admin_tag_set
.nr_hw_queues
= 1;
1533 ctrl
->admin_tag_set
.timeout
= ADMIN_TIMEOUT
;
1535 error
= blk_mq_alloc_tag_set(&ctrl
->admin_tag_set
);
1539 ctrl
->ctrl
.admin_q
= blk_mq_init_queue(&ctrl
->admin_tag_set
);
1540 if (IS_ERR(ctrl
->ctrl
.admin_q
)) {
1541 error
= PTR_ERR(ctrl
->ctrl
.admin_q
);
1542 goto out_free_tagset
;
1545 error
= nvmf_connect_admin_queue(&ctrl
->ctrl
);
1547 goto out_cleanup_queue
;
1549 error
= nvmf_reg_read64(&ctrl
->ctrl
, NVME_REG_CAP
, &ctrl
->cap
);
1551 dev_err(ctrl
->ctrl
.device
,
1552 "prop_get NVME_REG_CAP failed\n");
1553 goto out_cleanup_queue
;
1557 min_t(int, NVME_CAP_MQES(ctrl
->cap
) + 1, ctrl
->ctrl
.sqsize
);
1559 error
= nvme_enable_ctrl(&ctrl
->ctrl
, ctrl
->cap
);
1561 goto out_cleanup_queue
;
1563 ctrl
->ctrl
.max_hw_sectors
=
1564 (ctrl
->max_fr_pages
- 1) << (PAGE_SHIFT
- 9);
1566 error
= nvme_init_identify(&ctrl
->ctrl
);
1568 goto out_cleanup_queue
;
1570 error
= nvme_rdma_alloc_qe(ctrl
->queues
[0].device
->dev
,
1571 &ctrl
->async_event_sqe
, sizeof(struct nvme_command
),
1574 goto out_cleanup_queue
;
1576 nvme_start_keep_alive(&ctrl
->ctrl
);
1581 blk_cleanup_queue(ctrl
->ctrl
.admin_q
);
1583 /* disconnect and drain the queue before freeing the tagset */
1584 nvme_rdma_stop_queue(&ctrl
->queues
[0]);
1585 blk_mq_free_tag_set(&ctrl
->admin_tag_set
);
1587 nvme_rdma_dev_put(ctrl
->device
);
1589 nvme_rdma_free_queue(&ctrl
->queues
[0]);
1593 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl
*ctrl
)
1595 nvme_stop_keep_alive(&ctrl
->ctrl
);
1596 cancel_work_sync(&ctrl
->err_work
);
1597 cancel_delayed_work_sync(&ctrl
->reconnect_work
);
1599 if (ctrl
->queue_count
> 1) {
1600 nvme_stop_queues(&ctrl
->ctrl
);
1601 blk_mq_tagset_busy_iter(&ctrl
->tag_set
,
1602 nvme_cancel_request
, &ctrl
->ctrl
);
1603 nvme_rdma_free_io_queues(ctrl
);
1606 if (test_bit(NVME_RDMA_Q_CONNECTED
, &ctrl
->queues
[0].flags
))
1607 nvme_shutdown_ctrl(&ctrl
->ctrl
);
1609 blk_mq_stop_hw_queues(ctrl
->ctrl
.admin_q
);
1610 blk_mq_tagset_busy_iter(&ctrl
->admin_tag_set
,
1611 nvme_cancel_request
, &ctrl
->ctrl
);
1612 nvme_rdma_destroy_admin_queue(ctrl
);
1615 static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl
*ctrl
, bool shutdown
)
1617 nvme_uninit_ctrl(&ctrl
->ctrl
);
1619 nvme_rdma_shutdown_ctrl(ctrl
);
1621 if (ctrl
->ctrl
.tagset
) {
1622 blk_cleanup_queue(ctrl
->ctrl
.connect_q
);
1623 blk_mq_free_tag_set(&ctrl
->tag_set
);
1624 nvme_rdma_dev_put(ctrl
->device
);
1627 nvme_put_ctrl(&ctrl
->ctrl
);
1630 static void nvme_rdma_del_ctrl_work(struct work_struct
*work
)
1632 struct nvme_rdma_ctrl
*ctrl
= container_of(work
,
1633 struct nvme_rdma_ctrl
, delete_work
);
1635 __nvme_rdma_remove_ctrl(ctrl
, true);
1638 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl
*ctrl
)
1640 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_DELETING
))
1643 if (!queue_work(nvme_rdma_wq
, &ctrl
->delete_work
))
1649 static int nvme_rdma_del_ctrl(struct nvme_ctrl
*nctrl
)
1651 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(nctrl
);
1655 * Keep a reference until all work is flushed since
1656 * __nvme_rdma_del_ctrl can free the ctrl mem
1658 if (!kref_get_unless_zero(&ctrl
->ctrl
.kref
))
1660 ret
= __nvme_rdma_del_ctrl(ctrl
);
1662 flush_work(&ctrl
->delete_work
);
1663 nvme_put_ctrl(&ctrl
->ctrl
);
1667 static void nvme_rdma_remove_ctrl_work(struct work_struct
*work
)
1669 struct nvme_rdma_ctrl
*ctrl
= container_of(work
,
1670 struct nvme_rdma_ctrl
, delete_work
);
1672 __nvme_rdma_remove_ctrl(ctrl
, false);
1675 static void nvme_rdma_reset_ctrl_work(struct work_struct
*work
)
1677 struct nvme_rdma_ctrl
*ctrl
= container_of(work
,
1678 struct nvme_rdma_ctrl
, reset_work
);
1682 nvme_rdma_shutdown_ctrl(ctrl
);
1684 ret
= nvme_rdma_configure_admin_queue(ctrl
);
1686 /* ctrl is already shutdown, just remove the ctrl */
1687 INIT_WORK(&ctrl
->delete_work
, nvme_rdma_remove_ctrl_work
);
1691 if (ctrl
->queue_count
> 1) {
1692 ret
= blk_mq_reinit_tagset(&ctrl
->tag_set
);
1696 ret
= nvme_rdma_init_io_queues(ctrl
);
1700 ret
= nvme_rdma_connect_io_queues(ctrl
);
1705 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_LIVE
);
1706 WARN_ON_ONCE(!changed
);
1708 if (ctrl
->queue_count
> 1) {
1709 nvme_start_queues(&ctrl
->ctrl
);
1710 nvme_queue_scan(&ctrl
->ctrl
);
1711 nvme_queue_async_events(&ctrl
->ctrl
);
1717 /* Deleting this dead controller... */
1718 dev_warn(ctrl
->ctrl
.device
, "Removing after reset failure\n");
1719 WARN_ON(!queue_work(nvme_rdma_wq
, &ctrl
->delete_work
));
1722 static int nvme_rdma_reset_ctrl(struct nvme_ctrl
*nctrl
)
1724 struct nvme_rdma_ctrl
*ctrl
= to_rdma_ctrl(nctrl
);
1726 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_RESETTING
))
1729 if (!queue_work(nvme_rdma_wq
, &ctrl
->reset_work
))
1732 flush_work(&ctrl
->reset_work
);
1737 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops
= {
1739 .module
= THIS_MODULE
,
1741 .reg_read32
= nvmf_reg_read32
,
1742 .reg_read64
= nvmf_reg_read64
,
1743 .reg_write32
= nvmf_reg_write32
,
1744 .reset_ctrl
= nvme_rdma_reset_ctrl
,
1745 .free_ctrl
= nvme_rdma_free_ctrl
,
1746 .submit_async_event
= nvme_rdma_submit_async_event
,
1747 .delete_ctrl
= nvme_rdma_del_ctrl
,
1748 .get_subsysnqn
= nvmf_get_subsysnqn
,
1749 .get_address
= nvmf_get_address
,
1752 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl
*ctrl
)
1754 struct nvmf_ctrl_options
*opts
= ctrl
->ctrl
.opts
;
1757 ret
= nvme_set_queue_count(&ctrl
->ctrl
, &opts
->nr_io_queues
);
1761 ctrl
->queue_count
= opts
->nr_io_queues
+ 1;
1762 if (ctrl
->queue_count
< 2)
1765 dev_info(ctrl
->ctrl
.device
,
1766 "creating %d I/O queues.\n", opts
->nr_io_queues
);
1768 ret
= nvme_rdma_init_io_queues(ctrl
);
1773 * We need a reference on the device as long as the tag_set is alive,
1774 * as the MRs in the request structures need a valid ib_device.
1777 if (!nvme_rdma_dev_get(ctrl
->device
))
1778 goto out_free_io_queues
;
1780 memset(&ctrl
->tag_set
, 0, sizeof(ctrl
->tag_set
));
1781 ctrl
->tag_set
.ops
= &nvme_rdma_mq_ops
;
1782 ctrl
->tag_set
.queue_depth
= ctrl
->ctrl
.opts
->queue_size
;
1783 ctrl
->tag_set
.reserved_tags
= 1; /* fabric connect */
1784 ctrl
->tag_set
.numa_node
= NUMA_NO_NODE
;
1785 ctrl
->tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
;
1786 ctrl
->tag_set
.cmd_size
= sizeof(struct nvme_rdma_request
) +
1787 SG_CHUNK_SIZE
* sizeof(struct scatterlist
);
1788 ctrl
->tag_set
.driver_data
= ctrl
;
1789 ctrl
->tag_set
.nr_hw_queues
= ctrl
->queue_count
- 1;
1790 ctrl
->tag_set
.timeout
= NVME_IO_TIMEOUT
;
1792 ret
= blk_mq_alloc_tag_set(&ctrl
->tag_set
);
1795 ctrl
->ctrl
.tagset
= &ctrl
->tag_set
;
1797 ctrl
->ctrl
.connect_q
= blk_mq_init_queue(&ctrl
->tag_set
);
1798 if (IS_ERR(ctrl
->ctrl
.connect_q
)) {
1799 ret
= PTR_ERR(ctrl
->ctrl
.connect_q
);
1800 goto out_free_tag_set
;
1803 ret
= nvme_rdma_connect_io_queues(ctrl
);
1805 goto out_cleanup_connect_q
;
1809 out_cleanup_connect_q
:
1810 blk_cleanup_queue(ctrl
->ctrl
.connect_q
);
1812 blk_mq_free_tag_set(&ctrl
->tag_set
);
1814 nvme_rdma_dev_put(ctrl
->device
);
1816 nvme_rdma_free_io_queues(ctrl
);
1820 static int nvme_rdma_parse_ipaddr(struct sockaddr_in
*in_addr
, char *p
)
1822 u8
*addr
= (u8
*)&in_addr
->sin_addr
.s_addr
;
1823 size_t buflen
= strlen(p
);
1825 /* XXX: handle IPv6 addresses */
1827 if (buflen
> INET_ADDRSTRLEN
)
1829 if (in4_pton(p
, buflen
, addr
, '\0', NULL
) == 0)
1831 in_addr
->sin_family
= AF_INET
;
1835 static struct nvme_ctrl
*nvme_rdma_create_ctrl(struct device
*dev
,
1836 struct nvmf_ctrl_options
*opts
)
1838 struct nvme_rdma_ctrl
*ctrl
;
1842 ctrl
= kzalloc(sizeof(*ctrl
), GFP_KERNEL
);
1844 return ERR_PTR(-ENOMEM
);
1845 ctrl
->ctrl
.opts
= opts
;
1846 INIT_LIST_HEAD(&ctrl
->list
);
1848 ret
= nvme_rdma_parse_ipaddr(&ctrl
->addr_in
, opts
->traddr
);
1850 pr_err("malformed IP address passed: %s\n", opts
->traddr
);
1854 if (opts
->mask
& NVMF_OPT_TRSVCID
) {
1857 ret
= kstrtou16(opts
->trsvcid
, 0, &port
);
1861 ctrl
->addr_in
.sin_port
= cpu_to_be16(port
);
1863 ctrl
->addr_in
.sin_port
= cpu_to_be16(NVME_RDMA_IP_PORT
);
1866 ret
= nvme_init_ctrl(&ctrl
->ctrl
, dev
, &nvme_rdma_ctrl_ops
,
1867 0 /* no quirks, we're perfect! */);
1871 ctrl
->reconnect_delay
= opts
->reconnect_delay
;
1872 INIT_DELAYED_WORK(&ctrl
->reconnect_work
,
1873 nvme_rdma_reconnect_ctrl_work
);
1874 INIT_WORK(&ctrl
->err_work
, nvme_rdma_error_recovery_work
);
1875 INIT_WORK(&ctrl
->delete_work
, nvme_rdma_del_ctrl_work
);
1876 INIT_WORK(&ctrl
->reset_work
, nvme_rdma_reset_ctrl_work
);
1877 spin_lock_init(&ctrl
->lock
);
1879 ctrl
->queue_count
= opts
->nr_io_queues
+ 1; /* +1 for admin queue */
1880 ctrl
->ctrl
.sqsize
= opts
->queue_size
- 1;
1881 ctrl
->ctrl
.kato
= opts
->kato
;
1884 ctrl
->queues
= kcalloc(ctrl
->queue_count
, sizeof(*ctrl
->queues
),
1887 goto out_uninit_ctrl
;
1889 ret
= nvme_rdma_configure_admin_queue(ctrl
);
1891 goto out_kfree_queues
;
1893 /* sanity check icdoff */
1894 if (ctrl
->ctrl
.icdoff
) {
1895 dev_err(ctrl
->ctrl
.device
, "icdoff is not supported!\n");
1896 goto out_remove_admin_queue
;
1899 /* sanity check keyed sgls */
1900 if (!(ctrl
->ctrl
.sgls
& (1 << 20))) {
1901 dev_err(ctrl
->ctrl
.device
, "Mandatory keyed sgls are not support\n");
1902 goto out_remove_admin_queue
;
1905 if (opts
->queue_size
> ctrl
->ctrl
.maxcmd
) {
1906 /* warn if maxcmd is lower than queue_size */
1907 dev_warn(ctrl
->ctrl
.device
,
1908 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1909 opts
->queue_size
, ctrl
->ctrl
.maxcmd
);
1910 opts
->queue_size
= ctrl
->ctrl
.maxcmd
;
1913 if (opts
->nr_io_queues
) {
1914 ret
= nvme_rdma_create_io_queues(ctrl
);
1916 goto out_remove_admin_queue
;
1919 changed
= nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_LIVE
);
1920 WARN_ON_ONCE(!changed
);
1922 dev_info(ctrl
->ctrl
.device
, "new ctrl: NQN \"%s\", addr %pISp\n",
1923 ctrl
->ctrl
.opts
->subsysnqn
, &ctrl
->addr
);
1925 kref_get(&ctrl
->ctrl
.kref
);
1927 mutex_lock(&nvme_rdma_ctrl_mutex
);
1928 list_add_tail(&ctrl
->list
, &nvme_rdma_ctrl_list
);
1929 mutex_unlock(&nvme_rdma_ctrl_mutex
);
1931 if (opts
->nr_io_queues
) {
1932 nvme_queue_scan(&ctrl
->ctrl
);
1933 nvme_queue_async_events(&ctrl
->ctrl
);
1938 out_remove_admin_queue
:
1939 nvme_stop_keep_alive(&ctrl
->ctrl
);
1940 nvme_rdma_destroy_admin_queue(ctrl
);
1942 kfree(ctrl
->queues
);
1944 nvme_uninit_ctrl(&ctrl
->ctrl
);
1945 nvme_put_ctrl(&ctrl
->ctrl
);
1948 return ERR_PTR(ret
);
1951 return ERR_PTR(ret
);
1954 static struct nvmf_transport_ops nvme_rdma_transport
= {
1956 .required_opts
= NVMF_OPT_TRADDR
,
1957 .allowed_opts
= NVMF_OPT_TRSVCID
| NVMF_OPT_RECONNECT_DELAY
,
1958 .create_ctrl
= nvme_rdma_create_ctrl
,
1961 static void nvme_rdma_add_one(struct ib_device
*ib_device
)
1965 static void nvme_rdma_remove_one(struct ib_device
*ib_device
, void *client_data
)
1967 struct nvme_rdma_ctrl
*ctrl
;
1969 /* Delete all controllers using this device */
1970 mutex_lock(&nvme_rdma_ctrl_mutex
);
1971 list_for_each_entry(ctrl
, &nvme_rdma_ctrl_list
, list
) {
1972 if (ctrl
->device
->dev
!= ib_device
)
1974 dev_info(ctrl
->ctrl
.device
,
1975 "Removing ctrl: NQN \"%s\", addr %pISp\n",
1976 ctrl
->ctrl
.opts
->subsysnqn
, &ctrl
->addr
);
1977 __nvme_rdma_del_ctrl(ctrl
);
1979 mutex_unlock(&nvme_rdma_ctrl_mutex
);
1981 flush_workqueue(nvme_rdma_wq
);
1984 static struct ib_client nvme_rdma_ib_client
= {
1985 .name
= "nvme_rdma",
1986 .add
= nvme_rdma_add_one
,
1987 .remove
= nvme_rdma_remove_one
1990 static int __init
nvme_rdma_init_module(void)
1994 nvme_rdma_wq
= create_workqueue("nvme_rdma_wq");
1998 ret
= ib_register_client(&nvme_rdma_ib_client
);
2000 destroy_workqueue(nvme_rdma_wq
);
2004 nvmf_register_transport(&nvme_rdma_transport
);
2008 static void __exit
nvme_rdma_cleanup_module(void)
2010 nvmf_unregister_transport(&nvme_rdma_transport
);
2011 ib_unregister_client(&nvme_rdma_ib_client
);
2012 destroy_workqueue(nvme_rdma_wq
);
2015 module_init(nvme_rdma_init_module
);
2016 module_exit(nvme_rdma_cleanup_module
);
2018 MODULE_LICENSE("GPL v2");