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 <linux/nvme-rdma.h>
37 #define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
39 #define NVME_RDMA_MAX_SEGMENT_SIZE 0xffffff /* 24-bit SGL field */
41 #define NVME_RDMA_MAX_SEGMENTS 256
43 #define NVME_RDMA_MAX_INLINE_SEGMENTS 1
46 * We handle AEN commands ourselves and don't even let the
47 * block layer know about them.
49 #define NVME_RDMA_NR_AEN_COMMANDS 1
50 #define NVME_RDMA_AQ_BLKMQ_DEPTH \
51 (NVMF_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
53 struct nvme_rdma_device {
54 struct ib_device *dev;
57 struct list_head entry;
66 struct nvme_rdma_queue;
67 struct nvme_rdma_request {
68 struct nvme_request req;
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 = 0,
84 NVME_RDMA_IB_QUEUE_ALLOCATED = 1,
85 NVME_RDMA_Q_DELETING = 2,
89 struct nvme_rdma_queue {
90 struct nvme_rdma_qe *rsp_ring;
93 size_t cmnd_capsule_len;
94 struct nvme_rdma_ctrl *ctrl;
95 struct nvme_rdma_device *device;
100 struct rdma_cm_id *cm_id;
102 struct completion cm_done;
105 struct nvme_rdma_ctrl {
106 /* read only in the hot path */
107 struct nvme_rdma_queue *queues;
110 /* other member variables */
111 struct blk_mq_tag_set tag_set;
112 struct work_struct delete_work;
113 struct work_struct reset_work;
114 struct work_struct err_work;
116 struct nvme_rdma_qe async_event_sqe;
118 struct delayed_work reconnect_work;
120 struct list_head list;
122 struct blk_mq_tag_set admin_tag_set;
123 struct nvme_rdma_device *device;
128 struct sockaddr_storage addr;
129 struct sockaddr_storage src_addr;
131 struct nvme_ctrl ctrl;
134 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
136 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
139 static LIST_HEAD(device_list);
140 static DEFINE_MUTEX(device_list_mutex);
142 static LIST_HEAD(nvme_rdma_ctrl_list);
143 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
145 static struct workqueue_struct *nvme_rdma_wq;
148 * Disabling this option makes small I/O goes faster, but is fundamentally
149 * unsafe. With it turned off we will have to register a global rkey that
150 * allows read and write access to all physical memory.
152 static bool register_always = true;
153 module_param(register_always, bool, 0444);
154 MODULE_PARM_DESC(register_always,
155 "Use memory registration even for contiguous memory regions");
157 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
158 struct rdma_cm_event *event);
159 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
161 /* XXX: really should move to a generic header sooner or later.. */
162 static inline void put_unaligned_le24(u32 val, u8 *p)
169 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
171 return queue - queue->ctrl->queues;
174 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
176 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
179 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
180 size_t capsule_size, enum dma_data_direction dir)
182 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
186 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
187 size_t capsule_size, enum dma_data_direction dir)
189 qe->data = kzalloc(capsule_size, GFP_KERNEL);
193 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
194 if (ib_dma_mapping_error(ibdev, qe->dma)) {
202 static void nvme_rdma_free_ring(struct ib_device *ibdev,
203 struct nvme_rdma_qe *ring, size_t ib_queue_size,
204 size_t capsule_size, enum dma_data_direction dir)
208 for (i = 0; i < ib_queue_size; i++)
209 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
213 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
214 size_t ib_queue_size, size_t capsule_size,
215 enum dma_data_direction dir)
217 struct nvme_rdma_qe *ring;
220 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
224 for (i = 0; i < ib_queue_size; i++) {
225 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
232 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
236 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
238 pr_debug("QP event %s (%d)\n",
239 ib_event_msg(event->event), event->event);
243 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
245 wait_for_completion_interruptible_timeout(&queue->cm_done,
246 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
247 return queue->cm_error;
250 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
252 struct nvme_rdma_device *dev = queue->device;
253 struct ib_qp_init_attr init_attr;
256 memset(&init_attr, 0, sizeof(init_attr));
257 init_attr.event_handler = nvme_rdma_qp_event;
259 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
261 init_attr.cap.max_recv_wr = queue->queue_size + 1;
262 init_attr.cap.max_recv_sge = 1;
263 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
264 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
265 init_attr.qp_type = IB_QPT_RC;
266 init_attr.send_cq = queue->ib_cq;
267 init_attr.recv_cq = queue->ib_cq;
269 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
271 queue->qp = queue->cm_id->qp;
275 static int nvme_rdma_reinit_request(void *data, struct request *rq)
277 struct nvme_rdma_ctrl *ctrl = data;
278 struct nvme_rdma_device *dev = ctrl->device;
279 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
282 if (!req->mr->need_inval)
285 ib_dereg_mr(req->mr);
287 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
289 if (IS_ERR(req->mr)) {
290 ret = PTR_ERR(req->mr);
295 req->mr->need_inval = false;
301 static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl *ctrl,
302 struct request *rq, unsigned int queue_idx)
304 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
305 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
306 struct nvme_rdma_device *dev = queue->device;
309 ib_dereg_mr(req->mr);
311 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
315 static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
316 struct request *rq, unsigned int hctx_idx)
318 return __nvme_rdma_exit_request(set->driver_data, rq, hctx_idx + 1);
321 static void nvme_rdma_exit_admin_request(struct blk_mq_tag_set *set,
322 struct request *rq, unsigned int hctx_idx)
324 return __nvme_rdma_exit_request(set->driver_data, rq, 0);
327 static int __nvme_rdma_init_request(struct nvme_rdma_ctrl *ctrl,
328 struct request *rq, unsigned int queue_idx)
330 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
331 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
332 struct nvme_rdma_device *dev = queue->device;
333 struct ib_device *ibdev = dev->dev;
336 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
341 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
343 if (IS_ERR(req->mr)) {
344 ret = PTR_ERR(req->mr);
353 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
358 static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
359 struct request *rq, unsigned int hctx_idx,
360 unsigned int numa_node)
362 return __nvme_rdma_init_request(set->driver_data, rq, hctx_idx + 1);
365 static int nvme_rdma_init_admin_request(struct blk_mq_tag_set *set,
366 struct request *rq, unsigned int hctx_idx,
367 unsigned int numa_node)
369 return __nvme_rdma_init_request(set->driver_data, rq, 0);
372 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
373 unsigned int hctx_idx)
375 struct nvme_rdma_ctrl *ctrl = data;
376 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
378 BUG_ON(hctx_idx >= ctrl->queue_count);
380 hctx->driver_data = queue;
384 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
385 unsigned int hctx_idx)
387 struct nvme_rdma_ctrl *ctrl = data;
388 struct nvme_rdma_queue *queue = &ctrl->queues[0];
390 BUG_ON(hctx_idx != 0);
392 hctx->driver_data = queue;
396 static void nvme_rdma_free_dev(struct kref *ref)
398 struct nvme_rdma_device *ndev =
399 container_of(ref, struct nvme_rdma_device, ref);
401 mutex_lock(&device_list_mutex);
402 list_del(&ndev->entry);
403 mutex_unlock(&device_list_mutex);
405 ib_dealloc_pd(ndev->pd);
409 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
411 kref_put(&dev->ref, nvme_rdma_free_dev);
414 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
416 return kref_get_unless_zero(&dev->ref);
419 static struct nvme_rdma_device *
420 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
422 struct nvme_rdma_device *ndev;
424 mutex_lock(&device_list_mutex);
425 list_for_each_entry(ndev, &device_list, entry) {
426 if (ndev->dev->node_guid == cm_id->device->node_guid &&
427 nvme_rdma_dev_get(ndev))
431 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
435 ndev->dev = cm_id->device;
436 kref_init(&ndev->ref);
438 ndev->pd = ib_alloc_pd(ndev->dev,
439 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
440 if (IS_ERR(ndev->pd))
443 if (!(ndev->dev->attrs.device_cap_flags &
444 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
445 dev_err(&ndev->dev->dev,
446 "Memory registrations not supported.\n");
450 list_add(&ndev->entry, &device_list);
452 mutex_unlock(&device_list_mutex);
456 ib_dealloc_pd(ndev->pd);
460 mutex_unlock(&device_list_mutex);
464 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
466 struct nvme_rdma_device *dev;
467 struct ib_device *ibdev;
469 if (!test_and_clear_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags))
474 rdma_destroy_qp(queue->cm_id);
475 ib_free_cq(queue->ib_cq);
477 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
478 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
480 nvme_rdma_dev_put(dev);
483 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue,
484 struct nvme_rdma_device *dev)
486 struct ib_device *ibdev = dev->dev;
487 const int send_wr_factor = 3; /* MR, SEND, INV */
488 const int cq_factor = send_wr_factor + 1; /* + RECV */
489 int comp_vector, idx = nvme_rdma_queue_idx(queue);
496 * The admin queue is barely used once the controller is live, so don't
497 * bother to spread it out.
502 comp_vector = idx % ibdev->num_comp_vectors;
505 /* +1 for ib_stop_cq */
506 queue->ib_cq = ib_alloc_cq(dev->dev, queue,
507 cq_factor * queue->queue_size + 1, comp_vector,
509 if (IS_ERR(queue->ib_cq)) {
510 ret = PTR_ERR(queue->ib_cq);
514 ret = nvme_rdma_create_qp(queue, send_wr_factor);
516 goto out_destroy_ib_cq;
518 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
519 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
520 if (!queue->rsp_ring) {
524 set_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags);
529 ib_destroy_qp(queue->qp);
531 ib_free_cq(queue->ib_cq);
536 static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl,
537 int idx, size_t queue_size)
539 struct nvme_rdma_queue *queue;
540 struct sockaddr *src_addr = NULL;
543 queue = &ctrl->queues[idx];
545 init_completion(&queue->cm_done);
548 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
550 queue->cmnd_capsule_len = sizeof(struct nvme_command);
552 queue->queue_size = queue_size;
554 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
555 RDMA_PS_TCP, IB_QPT_RC);
556 if (IS_ERR(queue->cm_id)) {
557 dev_info(ctrl->ctrl.device,
558 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
559 return PTR_ERR(queue->cm_id);
562 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
563 src_addr = (struct sockaddr *)&ctrl->src_addr;
565 queue->cm_error = -ETIMEDOUT;
566 ret = rdma_resolve_addr(queue->cm_id, src_addr,
567 (struct sockaddr *)&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);
628 dev_info(ctrl->ctrl.device,
629 "failed to connect i/o queue: %d\n", ret);
630 goto out_free_queues;
632 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
638 nvme_rdma_free_io_queues(ctrl);
642 static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl)
644 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
645 unsigned int nr_io_queues;
648 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
649 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
653 ctrl->queue_count = nr_io_queues + 1;
654 if (ctrl->queue_count < 2)
657 dev_info(ctrl->ctrl.device,
658 "creating %d I/O queues.\n", nr_io_queues);
660 for (i = 1; i < ctrl->queue_count; i++) {
661 ret = nvme_rdma_init_queue(ctrl, i,
662 ctrl->ctrl.opts->queue_size);
664 dev_info(ctrl->ctrl.device,
665 "failed to initialize i/o queue: %d\n", ret);
666 goto out_free_queues;
673 for (i--; i >= 1; i--)
674 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
679 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl)
681 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
682 sizeof(struct nvme_command), DMA_TO_DEVICE);
683 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
684 blk_cleanup_queue(ctrl->ctrl.admin_q);
685 blk_mq_free_tag_set(&ctrl->admin_tag_set);
686 nvme_rdma_dev_put(ctrl->device);
689 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
691 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
693 if (list_empty(&ctrl->list))
696 mutex_lock(&nvme_rdma_ctrl_mutex);
697 list_del(&ctrl->list);
698 mutex_unlock(&nvme_rdma_ctrl_mutex);
701 nvmf_free_options(nctrl->opts);
706 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
708 /* If we are resetting/deleting then do nothing */
709 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
710 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
711 ctrl->ctrl.state == NVME_CTRL_LIVE);
715 if (nvmf_should_reconnect(&ctrl->ctrl)) {
716 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
717 ctrl->ctrl.opts->reconnect_delay);
718 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
719 ctrl->ctrl.opts->reconnect_delay * HZ);
721 dev_info(ctrl->ctrl.device, "Removing controller...\n");
722 queue_work(nvme_rdma_wq, &ctrl->delete_work);
726 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
728 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
729 struct nvme_rdma_ctrl, reconnect_work);
733 ++ctrl->ctrl.opts->nr_reconnects;
735 if (ctrl->queue_count > 1) {
736 nvme_rdma_free_io_queues(ctrl);
738 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
743 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
745 ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set);
749 ret = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
753 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
757 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
759 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
763 nvme_start_keep_alive(&ctrl->ctrl);
765 if (ctrl->queue_count > 1) {
766 ret = nvme_rdma_init_io_queues(ctrl);
770 ret = nvme_rdma_connect_io_queues(ctrl);
775 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
776 WARN_ON_ONCE(!changed);
777 ctrl->ctrl.opts->nr_reconnects = 0;
779 if (ctrl->queue_count > 1) {
780 nvme_queue_scan(&ctrl->ctrl);
781 nvme_queue_async_events(&ctrl->ctrl);
784 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
789 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
790 ctrl->ctrl.opts->nr_reconnects);
791 nvme_rdma_reconnect_or_remove(ctrl);
794 static void nvme_rdma_error_recovery_work(struct work_struct *work)
796 struct nvme_rdma_ctrl *ctrl = container_of(work,
797 struct nvme_rdma_ctrl, err_work);
800 nvme_stop_keep_alive(&ctrl->ctrl);
802 for (i = 0; i < ctrl->queue_count; i++) {
803 clear_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[i].flags);
804 clear_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
807 if (ctrl->queue_count > 1)
808 nvme_stop_queues(&ctrl->ctrl);
809 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
811 /* We must take care of fastfail/requeue all our inflight requests */
812 if (ctrl->queue_count > 1)
813 blk_mq_tagset_busy_iter(&ctrl->tag_set,
814 nvme_cancel_request, &ctrl->ctrl);
815 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
816 nvme_cancel_request, &ctrl->ctrl);
819 * queues are not a live anymore, so restart the queues to fail fast
822 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
823 nvme_start_queues(&ctrl->ctrl);
825 nvme_rdma_reconnect_or_remove(ctrl);
828 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
830 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
833 queue_work(nvme_rdma_wq, &ctrl->err_work);
836 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
839 struct nvme_rdma_queue *queue = cq->cq_context;
840 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
842 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
843 dev_info(ctrl->ctrl.device,
844 "%s for CQE 0x%p failed with status %s (%d)\n",
846 ib_wc_status_msg(wc->status), wc->status);
847 nvme_rdma_error_recovery(ctrl);
850 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
852 if (unlikely(wc->status != IB_WC_SUCCESS))
853 nvme_rdma_wr_error(cq, wc, "MEMREG");
856 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
858 if (unlikely(wc->status != IB_WC_SUCCESS))
859 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
862 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
863 struct nvme_rdma_request *req)
865 struct ib_send_wr *bad_wr;
866 struct ib_send_wr wr = {
867 .opcode = IB_WR_LOCAL_INV,
871 .ex.invalidate_rkey = req->mr->rkey,
874 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
875 wr.wr_cqe = &req->reg_cqe;
877 return ib_post_send(queue->qp, &wr, &bad_wr);
880 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
883 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
884 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
885 struct nvme_rdma_device *dev = queue->device;
886 struct ib_device *ibdev = dev->dev;
889 if (!blk_rq_bytes(rq))
892 if (req->mr->need_inval) {
893 res = nvme_rdma_inv_rkey(queue, req);
895 dev_err(ctrl->ctrl.device,
896 "Queueing INV WR for rkey %#x failed (%d)\n",
898 nvme_rdma_error_recovery(queue->ctrl);
902 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
903 req->nents, rq_data_dir(rq) ==
904 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
906 nvme_cleanup_cmd(rq);
907 sg_free_table_chained(&req->sg_table, true);
910 static int nvme_rdma_set_sg_null(struct nvme_command *c)
912 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
915 put_unaligned_le24(0, sg->length);
916 put_unaligned_le32(0, sg->key);
917 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
921 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
922 struct nvme_rdma_request *req, struct nvme_command *c)
924 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
926 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
927 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
928 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
930 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
931 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
932 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
934 req->inline_data = true;
939 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
940 struct nvme_rdma_request *req, struct nvme_command *c)
942 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
944 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
945 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
946 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
947 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
951 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
952 struct nvme_rdma_request *req, struct nvme_command *c,
955 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
958 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE);
965 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
967 req->reg_cqe.done = nvme_rdma_memreg_done;
968 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
969 req->reg_wr.wr.opcode = IB_WR_REG_MR;
970 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
971 req->reg_wr.wr.num_sge = 0;
972 req->reg_wr.mr = req->mr;
973 req->reg_wr.key = req->mr->rkey;
974 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
975 IB_ACCESS_REMOTE_READ |
976 IB_ACCESS_REMOTE_WRITE;
978 req->mr->need_inval = true;
980 sg->addr = cpu_to_le64(req->mr->iova);
981 put_unaligned_le24(req->mr->length, sg->length);
982 put_unaligned_le32(req->mr->rkey, sg->key);
983 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
984 NVME_SGL_FMT_INVALIDATE;
989 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
990 struct request *rq, struct nvme_command *c)
992 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
993 struct nvme_rdma_device *dev = queue->device;
994 struct ib_device *ibdev = dev->dev;
998 req->inline_data = false;
999 req->mr->need_inval = false;
1001 c->common.flags |= NVME_CMD_SGL_METABUF;
1003 if (!blk_rq_bytes(rq))
1004 return nvme_rdma_set_sg_null(c);
1006 req->sg_table.sgl = req->first_sgl;
1007 ret = sg_alloc_table_chained(&req->sg_table,
1008 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
1012 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
1014 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
1015 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1016 if (unlikely(count <= 0)) {
1017 sg_free_table_chained(&req->sg_table, true);
1022 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1023 blk_rq_payload_bytes(rq) <=
1024 nvme_rdma_inline_data_size(queue))
1025 return nvme_rdma_map_sg_inline(queue, req, c);
1027 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
1028 return nvme_rdma_map_sg_single(queue, req, c);
1031 return nvme_rdma_map_sg_fr(queue, req, c, count);
1034 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1036 if (unlikely(wc->status != IB_WC_SUCCESS))
1037 nvme_rdma_wr_error(cq, wc, "SEND");
1040 static inline int nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue)
1045 * We signal completion every queue depth/2 and also handle the
1046 * degenerated case of a device with queue_depth=1, where we
1047 * would need to signal every message.
1049 sig_limit = max(queue->queue_size / 2, 1);
1050 return (++queue->sig_count % sig_limit) == 0;
1053 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1054 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1055 struct ib_send_wr *first, bool flush)
1057 struct ib_send_wr wr, *bad_wr;
1060 sge->addr = qe->dma;
1061 sge->length = sizeof(struct nvme_command),
1062 sge->lkey = queue->device->pd->local_dma_lkey;
1064 qe->cqe.done = nvme_rdma_send_done;
1067 wr.wr_cqe = &qe->cqe;
1069 wr.num_sge = num_sge;
1070 wr.opcode = IB_WR_SEND;
1074 * Unsignalled send completions are another giant desaster in the
1075 * IB Verbs spec: If we don't regularly post signalled sends
1076 * the send queue will fill up and only a QP reset will rescue us.
1077 * Would have been way to obvious to handle this in hardware or
1078 * at least the RDMA stack..
1080 * Always signal the flushes. The magic request used for the flush
1081 * sequencer is not allocated in our driver's tagset and it's
1082 * triggered to be freed by blk_cleanup_queue(). So we need to
1083 * always mark it as signaled to ensure that the "wr_cqe", which is
1084 * embedded in request's payload, is not freed when __ib_process_cq()
1085 * calls wr_cqe->done().
1087 if (nvme_rdma_queue_sig_limit(queue) || flush)
1088 wr.send_flags |= IB_SEND_SIGNALED;
1095 ret = ib_post_send(queue->qp, first, &bad_wr);
1097 dev_err(queue->ctrl->ctrl.device,
1098 "%s failed with error code %d\n", __func__, ret);
1103 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1104 struct nvme_rdma_qe *qe)
1106 struct ib_recv_wr wr, *bad_wr;
1110 list.addr = qe->dma;
1111 list.length = sizeof(struct nvme_completion);
1112 list.lkey = queue->device->pd->local_dma_lkey;
1114 qe->cqe.done = nvme_rdma_recv_done;
1117 wr.wr_cqe = &qe->cqe;
1121 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1123 dev_err(queue->ctrl->ctrl.device,
1124 "%s failed with error code %d\n", __func__, ret);
1129 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1131 u32 queue_idx = nvme_rdma_queue_idx(queue);
1134 return queue->ctrl->admin_tag_set.tags[queue_idx];
1135 return queue->ctrl->tag_set.tags[queue_idx - 1];
1138 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1140 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1141 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1142 struct ib_device *dev = queue->device->dev;
1143 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1144 struct nvme_command *cmd = sqe->data;
1148 if (WARN_ON_ONCE(aer_idx != 0))
1151 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1153 memset(cmd, 0, sizeof(*cmd));
1154 cmd->common.opcode = nvme_admin_async_event;
1155 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1156 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1157 nvme_rdma_set_sg_null(cmd);
1159 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1162 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1166 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1167 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1170 struct nvme_rdma_request *req;
1173 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1175 dev_err(queue->ctrl->ctrl.device,
1176 "tag 0x%x on QP %#x not found\n",
1177 cqe->command_id, queue->qp->qp_num);
1178 nvme_rdma_error_recovery(queue->ctrl);
1181 req = blk_mq_rq_to_pdu(rq);
1186 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1187 wc->ex.invalidate_rkey == req->mr->rkey)
1188 req->mr->need_inval = false;
1190 nvme_end_request(rq, cqe->status, cqe->result);
1194 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1196 struct nvme_rdma_qe *qe =
1197 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1198 struct nvme_rdma_queue *queue = cq->cq_context;
1199 struct ib_device *ibdev = queue->device->dev;
1200 struct nvme_completion *cqe = qe->data;
1201 const size_t len = sizeof(struct nvme_completion);
1204 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1205 nvme_rdma_wr_error(cq, wc, "RECV");
1209 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1211 * AEN requests are special as they don't time out and can
1212 * survive any kind of queue freeze and often don't respond to
1213 * aborts. We don't even bother to allocate a struct request
1214 * for them but rather special case them here.
1216 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1217 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
1218 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1221 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1222 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1224 nvme_rdma_post_recv(queue, qe);
1228 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1230 __nvme_rdma_recv_done(cq, wc, -1);
1233 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1237 for (i = 0; i < queue->queue_size; i++) {
1238 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1240 goto out_destroy_queue_ib;
1245 out_destroy_queue_ib:
1246 nvme_rdma_destroy_queue_ib(queue);
1250 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1251 struct rdma_cm_event *ev)
1253 struct rdma_cm_id *cm_id = queue->cm_id;
1254 int status = ev->status;
1255 const char *rej_msg;
1256 const struct nvme_rdma_cm_rej *rej_data;
1259 rej_msg = rdma_reject_msg(cm_id, status);
1260 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1262 if (rej_data && rej_data_len >= sizeof(u16)) {
1263 u16 sts = le16_to_cpu(rej_data->sts);
1265 dev_err(queue->ctrl->ctrl.device,
1266 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1267 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1269 dev_err(queue->ctrl->ctrl.device,
1270 "Connect rejected: status %d (%s).\n", status, rej_msg);
1276 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1278 struct nvme_rdma_device *dev;
1281 dev = nvme_rdma_find_get_device(queue->cm_id);
1283 dev_err(queue->cm_id->device->dev.parent,
1284 "no client data found!\n");
1285 return -ECONNREFUSED;
1288 ret = nvme_rdma_create_queue_ib(queue, dev);
1290 nvme_rdma_dev_put(dev);
1294 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1296 dev_err(queue->ctrl->ctrl.device,
1297 "rdma_resolve_route failed (%d).\n",
1299 goto out_destroy_queue;
1305 nvme_rdma_destroy_queue_ib(queue);
1310 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1312 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1313 struct rdma_conn_param param = { };
1314 struct nvme_rdma_cm_req priv = { };
1317 param.qp_num = queue->qp->qp_num;
1318 param.flow_control = 1;
1320 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1321 /* maximum retry count */
1322 param.retry_count = 7;
1323 param.rnr_retry_count = 7;
1324 param.private_data = &priv;
1325 param.private_data_len = sizeof(priv);
1327 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1328 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1330 * set the admin queue depth to the minimum size
1331 * specified by the Fabrics standard.
1333 if (priv.qid == 0) {
1334 priv.hrqsize = cpu_to_le16(NVMF_AQ_DEPTH);
1335 priv.hsqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1);
1338 * current interpretation of the fabrics spec
1339 * is at minimum you make hrqsize sqsize+1, or a
1340 * 1's based representation of sqsize.
1342 priv.hrqsize = cpu_to_le16(queue->queue_size);
1343 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1346 ret = rdma_connect(queue->cm_id, ¶m);
1348 dev_err(ctrl->ctrl.device,
1349 "rdma_connect failed (%d).\n", ret);
1350 goto out_destroy_queue_ib;
1355 out_destroy_queue_ib:
1356 nvme_rdma_destroy_queue_ib(queue);
1360 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1361 struct rdma_cm_event *ev)
1363 struct nvme_rdma_queue *queue = cm_id->context;
1366 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1367 rdma_event_msg(ev->event), ev->event,
1370 switch (ev->event) {
1371 case RDMA_CM_EVENT_ADDR_RESOLVED:
1372 cm_error = nvme_rdma_addr_resolved(queue);
1374 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1375 cm_error = nvme_rdma_route_resolved(queue);
1377 case RDMA_CM_EVENT_ESTABLISHED:
1378 queue->cm_error = nvme_rdma_conn_established(queue);
1379 /* complete cm_done regardless of success/failure */
1380 complete(&queue->cm_done);
1382 case RDMA_CM_EVENT_REJECTED:
1383 cm_error = nvme_rdma_conn_rejected(queue, ev);
1385 case RDMA_CM_EVENT_ADDR_ERROR:
1386 case RDMA_CM_EVENT_ROUTE_ERROR:
1387 case RDMA_CM_EVENT_CONNECT_ERROR:
1388 case RDMA_CM_EVENT_UNREACHABLE:
1389 dev_dbg(queue->ctrl->ctrl.device,
1390 "CM error event %d\n", ev->event);
1391 cm_error = -ECONNRESET;
1393 case RDMA_CM_EVENT_DISCONNECTED:
1394 case RDMA_CM_EVENT_ADDR_CHANGE:
1395 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1396 dev_dbg(queue->ctrl->ctrl.device,
1397 "disconnect received - connection closed\n");
1398 nvme_rdma_error_recovery(queue->ctrl);
1400 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1401 /* device removal is handled via the ib_client API */
1404 dev_err(queue->ctrl->ctrl.device,
1405 "Unexpected RDMA CM event (%d)\n", ev->event);
1406 nvme_rdma_error_recovery(queue->ctrl);
1411 queue->cm_error = cm_error;
1412 complete(&queue->cm_done);
1418 static enum blk_eh_timer_return
1419 nvme_rdma_timeout(struct request *rq, bool reserved)
1421 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1423 /* queue error recovery */
1424 nvme_rdma_error_recovery(req->queue->ctrl);
1426 /* fail with DNR on cmd timeout */
1427 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1429 return BLK_EH_HANDLED;
1433 * We cannot accept any other command until the Connect command has completed.
1435 static inline blk_status_t
1436 nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue, struct request *rq)
1438 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
1439 struct nvme_command *cmd = nvme_req(rq)->cmd;
1441 if (!blk_rq_is_passthrough(rq) ||
1442 cmd->common.opcode != nvme_fabrics_command ||
1443 cmd->fabrics.fctype != nvme_fabrics_type_connect) {
1445 * reconnecting state means transport disruption, which
1446 * can take a long time and even might fail permanently,
1447 * so we can't let incoming I/O be requeued forever.
1448 * fail it fast to allow upper layers a chance to
1451 if (queue->ctrl->ctrl.state == NVME_CTRL_RECONNECTING)
1452 return BLK_STS_IOERR;
1453 return BLK_STS_RESOURCE; /* try again later */
1460 static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1461 const struct blk_mq_queue_data *bd)
1463 struct nvme_ns *ns = hctx->queue->queuedata;
1464 struct nvme_rdma_queue *queue = hctx->driver_data;
1465 struct request *rq = bd->rq;
1466 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1467 struct nvme_rdma_qe *sqe = &req->sqe;
1468 struct nvme_command *c = sqe->data;
1470 struct ib_device *dev;
1474 WARN_ON_ONCE(rq->tag < 0);
1476 ret = nvme_rdma_queue_is_ready(queue, rq);
1480 dev = queue->device->dev;
1481 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1482 sizeof(struct nvme_command), DMA_TO_DEVICE);
1484 ret = nvme_setup_cmd(ns, rq, c);
1488 blk_mq_start_request(rq);
1490 err = nvme_rdma_map_data(queue, rq, c);
1492 dev_err(queue->ctrl->ctrl.device,
1493 "Failed to map data (%d)\n", err);
1494 nvme_cleanup_cmd(rq);
1498 ib_dma_sync_single_for_device(dev, sqe->dma,
1499 sizeof(struct nvme_command), DMA_TO_DEVICE);
1501 if (req_op(rq) == REQ_OP_FLUSH)
1503 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1504 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
1506 nvme_rdma_unmap_data(queue, rq);
1512 if (err == -ENOMEM || err == -EAGAIN)
1513 return BLK_STS_RESOURCE;
1514 return BLK_STS_IOERR;
1517 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1519 struct nvme_rdma_queue *queue = hctx->driver_data;
1520 struct ib_cq *cq = queue->ib_cq;
1524 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1525 while (ib_poll_cq(cq, 1, &wc) > 0) {
1526 struct ib_cqe *cqe = wc.wr_cqe;
1529 if (cqe->done == nvme_rdma_recv_done)
1530 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1539 static void nvme_rdma_complete_rq(struct request *rq)
1541 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1543 nvme_rdma_unmap_data(req->queue, rq);
1544 nvme_complete_rq(rq);
1547 static const struct blk_mq_ops nvme_rdma_mq_ops = {
1548 .queue_rq = nvme_rdma_queue_rq,
1549 .complete = nvme_rdma_complete_rq,
1550 .init_request = nvme_rdma_init_request,
1551 .exit_request = nvme_rdma_exit_request,
1552 .reinit_request = nvme_rdma_reinit_request,
1553 .init_hctx = nvme_rdma_init_hctx,
1554 .poll = nvme_rdma_poll,
1555 .timeout = nvme_rdma_timeout,
1558 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1559 .queue_rq = nvme_rdma_queue_rq,
1560 .complete = nvme_rdma_complete_rq,
1561 .init_request = nvme_rdma_init_admin_request,
1562 .exit_request = nvme_rdma_exit_admin_request,
1563 .reinit_request = nvme_rdma_reinit_request,
1564 .init_hctx = nvme_rdma_init_admin_hctx,
1565 .timeout = nvme_rdma_timeout,
1568 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
1572 error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
1576 ctrl->device = ctrl->queues[0].device;
1579 * We need a reference on the device as long as the tag_set is alive,
1580 * as the MRs in the request structures need a valid ib_device.
1583 if (!nvme_rdma_dev_get(ctrl->device))
1584 goto out_free_queue;
1586 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
1587 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
1589 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1590 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
1591 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
1592 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
1593 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1594 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1595 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1596 ctrl->admin_tag_set.driver_data = ctrl;
1597 ctrl->admin_tag_set.nr_hw_queues = 1;
1598 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1600 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1604 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1605 if (IS_ERR(ctrl->ctrl.admin_q)) {
1606 error = PTR_ERR(ctrl->ctrl.admin_q);
1607 goto out_free_tagset;
1610 error = nvmf_connect_admin_queue(&ctrl->ctrl);
1612 goto out_cleanup_queue;
1614 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
1616 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
1618 dev_err(ctrl->ctrl.device,
1619 "prop_get NVME_REG_CAP failed\n");
1620 goto out_cleanup_queue;
1624 min_t(int, NVME_CAP_MQES(ctrl->cap), ctrl->ctrl.sqsize);
1626 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
1628 goto out_cleanup_queue;
1630 ctrl->ctrl.max_hw_sectors =
1631 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
1633 error = nvme_init_identify(&ctrl->ctrl);
1635 goto out_cleanup_queue;
1637 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
1638 &ctrl->async_event_sqe, sizeof(struct nvme_command),
1641 goto out_cleanup_queue;
1643 nvme_start_keep_alive(&ctrl->ctrl);
1648 blk_cleanup_queue(ctrl->ctrl.admin_q);
1650 /* disconnect and drain the queue before freeing the tagset */
1651 nvme_rdma_stop_queue(&ctrl->queues[0]);
1652 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1654 nvme_rdma_dev_put(ctrl->device);
1656 nvme_rdma_free_queue(&ctrl->queues[0]);
1660 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1662 nvme_stop_keep_alive(&ctrl->ctrl);
1663 cancel_work_sync(&ctrl->err_work);
1664 cancel_delayed_work_sync(&ctrl->reconnect_work);
1666 if (ctrl->queue_count > 1) {
1667 nvme_stop_queues(&ctrl->ctrl);
1668 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1669 nvme_cancel_request, &ctrl->ctrl);
1670 nvme_rdma_free_io_queues(ctrl);
1673 if (test_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[0].flags))
1674 nvme_shutdown_ctrl(&ctrl->ctrl);
1676 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
1677 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1678 nvme_cancel_request, &ctrl->ctrl);
1679 nvme_rdma_destroy_admin_queue(ctrl);
1682 static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1684 nvme_uninit_ctrl(&ctrl->ctrl);
1686 nvme_rdma_shutdown_ctrl(ctrl);
1688 if (ctrl->ctrl.tagset) {
1689 blk_cleanup_queue(ctrl->ctrl.connect_q);
1690 blk_mq_free_tag_set(&ctrl->tag_set);
1691 nvme_rdma_dev_put(ctrl->device);
1694 nvme_put_ctrl(&ctrl->ctrl);
1697 static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1699 struct nvme_rdma_ctrl *ctrl = container_of(work,
1700 struct nvme_rdma_ctrl, delete_work);
1702 __nvme_rdma_remove_ctrl(ctrl, true);
1705 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1707 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1710 if (!queue_work(nvme_rdma_wq, &ctrl->delete_work))
1716 static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1718 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1722 * Keep a reference until all work is flushed since
1723 * __nvme_rdma_del_ctrl can free the ctrl mem
1725 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1727 ret = __nvme_rdma_del_ctrl(ctrl);
1729 flush_work(&ctrl->delete_work);
1730 nvme_put_ctrl(&ctrl->ctrl);
1734 static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1736 struct nvme_rdma_ctrl *ctrl = container_of(work,
1737 struct nvme_rdma_ctrl, delete_work);
1739 __nvme_rdma_remove_ctrl(ctrl, false);
1742 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1744 struct nvme_rdma_ctrl *ctrl = container_of(work,
1745 struct nvme_rdma_ctrl, reset_work);
1749 nvme_rdma_shutdown_ctrl(ctrl);
1751 ret = nvme_rdma_configure_admin_queue(ctrl);
1753 /* ctrl is already shutdown, just remove the ctrl */
1754 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1758 if (ctrl->queue_count > 1) {
1759 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
1763 ret = nvme_rdma_init_io_queues(ctrl);
1767 ret = nvme_rdma_connect_io_queues(ctrl);
1772 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1773 WARN_ON_ONCE(!changed);
1775 if (ctrl->queue_count > 1) {
1776 nvme_start_queues(&ctrl->ctrl);
1777 nvme_queue_scan(&ctrl->ctrl);
1778 nvme_queue_async_events(&ctrl->ctrl);
1784 /* Deleting this dead controller... */
1785 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1786 WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work));
1789 static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl)
1791 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1793 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1796 if (!queue_work(nvme_rdma_wq, &ctrl->reset_work))
1799 flush_work(&ctrl->reset_work);
1804 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1806 .module = THIS_MODULE,
1807 .flags = NVME_F_FABRICS,
1808 .reg_read32 = nvmf_reg_read32,
1809 .reg_read64 = nvmf_reg_read64,
1810 .reg_write32 = nvmf_reg_write32,
1811 .reset_ctrl = nvme_rdma_reset_ctrl,
1812 .free_ctrl = nvme_rdma_free_ctrl,
1813 .submit_async_event = nvme_rdma_submit_async_event,
1814 .delete_ctrl = nvme_rdma_del_ctrl,
1815 .get_subsysnqn = nvmf_get_subsysnqn,
1816 .get_address = nvmf_get_address,
1819 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1823 ret = nvme_rdma_init_io_queues(ctrl);
1828 * We need a reference on the device as long as the tag_set is alive,
1829 * as the MRs in the request structures need a valid ib_device.
1832 if (!nvme_rdma_dev_get(ctrl->device))
1833 goto out_free_io_queues;
1835 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1836 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
1837 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
1838 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1839 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1840 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1841 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1842 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1843 ctrl->tag_set.driver_data = ctrl;
1844 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
1845 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1847 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1850 ctrl->ctrl.tagset = &ctrl->tag_set;
1852 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1853 if (IS_ERR(ctrl->ctrl.connect_q)) {
1854 ret = PTR_ERR(ctrl->ctrl.connect_q);
1855 goto out_free_tag_set;
1858 ret = nvme_rdma_connect_io_queues(ctrl);
1860 goto out_cleanup_connect_q;
1864 out_cleanup_connect_q:
1865 blk_cleanup_queue(ctrl->ctrl.connect_q);
1867 blk_mq_free_tag_set(&ctrl->tag_set);
1869 nvme_rdma_dev_put(ctrl->device);
1871 nvme_rdma_free_io_queues(ctrl);
1875 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1876 struct nvmf_ctrl_options *opts)
1878 struct nvme_rdma_ctrl *ctrl;
1883 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1885 return ERR_PTR(-ENOMEM);
1886 ctrl->ctrl.opts = opts;
1887 INIT_LIST_HEAD(&ctrl->list);
1889 if (opts->mask & NVMF_OPT_TRSVCID)
1890 port = opts->trsvcid;
1892 port = __stringify(NVME_RDMA_IP_PORT);
1894 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1895 opts->traddr, port, &ctrl->addr);
1897 pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
1901 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
1902 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1903 opts->host_traddr, NULL, &ctrl->src_addr);
1905 pr_err("malformed src address passed: %s\n",
1911 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1912 0 /* no quirks, we're perfect! */);
1916 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1917 nvme_rdma_reconnect_ctrl_work);
1918 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1919 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1920 INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work);
1922 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1923 ctrl->ctrl.sqsize = opts->queue_size - 1;
1924 ctrl->ctrl.kato = opts->kato;
1927 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues),
1930 goto out_uninit_ctrl;
1932 ret = nvme_rdma_configure_admin_queue(ctrl);
1934 goto out_kfree_queues;
1936 /* sanity check icdoff */
1937 if (ctrl->ctrl.icdoff) {
1938 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1939 goto out_remove_admin_queue;
1942 /* sanity check keyed sgls */
1943 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1944 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1945 goto out_remove_admin_queue;
1948 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1949 /* warn if maxcmd is lower than queue_size */
1950 dev_warn(ctrl->ctrl.device,
1951 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1952 opts->queue_size, ctrl->ctrl.maxcmd);
1953 opts->queue_size = ctrl->ctrl.maxcmd;
1956 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1957 /* warn if sqsize is lower than queue_size */
1958 dev_warn(ctrl->ctrl.device,
1959 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1960 opts->queue_size, ctrl->ctrl.sqsize + 1);
1961 opts->queue_size = ctrl->ctrl.sqsize + 1;
1964 if (opts->nr_io_queues) {
1965 ret = nvme_rdma_create_io_queues(ctrl);
1967 goto out_remove_admin_queue;
1970 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1971 WARN_ON_ONCE(!changed);
1973 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
1974 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1976 kref_get(&ctrl->ctrl.kref);
1978 mutex_lock(&nvme_rdma_ctrl_mutex);
1979 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1980 mutex_unlock(&nvme_rdma_ctrl_mutex);
1982 if (opts->nr_io_queues) {
1983 nvme_queue_scan(&ctrl->ctrl);
1984 nvme_queue_async_events(&ctrl->ctrl);
1989 out_remove_admin_queue:
1990 nvme_stop_keep_alive(&ctrl->ctrl);
1991 nvme_rdma_destroy_admin_queue(ctrl);
1993 kfree(ctrl->queues);
1995 nvme_uninit_ctrl(&ctrl->ctrl);
1996 nvme_put_ctrl(&ctrl->ctrl);
1999 return ERR_PTR(ret);
2002 return ERR_PTR(ret);
2005 static struct nvmf_transport_ops nvme_rdma_transport = {
2007 .required_opts = NVMF_OPT_TRADDR,
2008 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2009 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
2010 .create_ctrl = nvme_rdma_create_ctrl,
2013 static void nvme_rdma_add_one(struct ib_device *ib_device)
2017 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2019 struct nvme_rdma_ctrl *ctrl;
2021 /* Delete all controllers using this device */
2022 mutex_lock(&nvme_rdma_ctrl_mutex);
2023 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2024 if (ctrl->device->dev != ib_device)
2026 dev_info(ctrl->ctrl.device,
2027 "Removing ctrl: NQN \"%s\", addr %pISp\n",
2028 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2029 __nvme_rdma_del_ctrl(ctrl);
2031 mutex_unlock(&nvme_rdma_ctrl_mutex);
2033 flush_workqueue(nvme_rdma_wq);
2036 static struct ib_client nvme_rdma_ib_client = {
2037 .name = "nvme_rdma",
2038 .add = nvme_rdma_add_one,
2039 .remove = nvme_rdma_remove_one
2042 static int __init nvme_rdma_init_module(void)
2046 nvme_rdma_wq = create_workqueue("nvme_rdma_wq");
2050 ret = ib_register_client(&nvme_rdma_ib_client);
2052 goto err_destroy_wq;
2054 ret = nvmf_register_transport(&nvme_rdma_transport);
2056 goto err_unreg_client;
2061 ib_unregister_client(&nvme_rdma_ib_client);
2063 destroy_workqueue(nvme_rdma_wq);
2067 static void __exit nvme_rdma_cleanup_module(void)
2069 nvmf_unregister_transport(&nvme_rdma_transport);
2070 ib_unregister_client(&nvme_rdma_ib_client);
2071 destroy_workqueue(nvme_rdma_wq);
2074 module_init(nvme_rdma_init_module);
2075 module_exit(nvme_rdma_cleanup_module);
2077 MODULE_LICENSE("GPL v2");