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 = (1 << 0),
84 NVME_RDMA_IB_QUEUE_ALLOCATED = (1 << 1),
85 NVME_RDMA_Q_DELETING = (1 << 2),
86 NVME_RDMA_Q_LIVE = (1 << 3),
89 struct nvme_rdma_queue {
90 struct nvme_rdma_qe *rsp_ring;
93 size_t cmnd_capsule_len;
94 struct nvme_rdma_ctrl *ctrl;
95 struct nvme_rdma_device *device;
100 struct rdma_cm_id *cm_id;
102 struct completion cm_done;
105 struct nvme_rdma_ctrl {
106 /* read and written in the hot path */
109 /* read only in the hot path */
110 struct nvme_rdma_queue *queues;
113 /* other member variables */
114 struct blk_mq_tag_set tag_set;
115 struct work_struct delete_work;
116 struct work_struct reset_work;
117 struct work_struct err_work;
119 struct nvme_rdma_qe async_event_sqe;
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;
131 struct sockaddr_storage addr;
132 struct sockaddr_storage src_addr;
134 struct nvme_ctrl ctrl;
137 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
139 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
142 static LIST_HEAD(device_list);
143 static DEFINE_MUTEX(device_list_mutex);
145 static LIST_HEAD(nvme_rdma_ctrl_list);
146 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
148 static struct workqueue_struct *nvme_rdma_wq;
151 * Disabling this option makes small I/O goes faster, but is fundamentally
152 * unsafe. With it turned off we will have to register a global rkey that
153 * allows read and write access to all physical memory.
155 static bool register_always = true;
156 module_param(register_always, bool, 0444);
157 MODULE_PARM_DESC(register_always,
158 "Use memory registration even for contiguous memory regions");
160 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
161 struct rdma_cm_event *event);
162 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
164 /* XXX: really should move to a generic header sooner or later.. */
165 static inline void put_unaligned_le24(u32 val, u8 *p)
172 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
174 return queue - queue->ctrl->queues;
177 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
179 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
182 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
183 size_t capsule_size, enum dma_data_direction dir)
185 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
189 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
190 size_t capsule_size, enum dma_data_direction dir)
192 qe->data = kzalloc(capsule_size, GFP_KERNEL);
196 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
197 if (ib_dma_mapping_error(ibdev, qe->dma)) {
205 static void nvme_rdma_free_ring(struct ib_device *ibdev,
206 struct nvme_rdma_qe *ring, size_t ib_queue_size,
207 size_t capsule_size, enum dma_data_direction dir)
211 for (i = 0; i < ib_queue_size; i++)
212 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
216 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
217 size_t ib_queue_size, size_t capsule_size,
218 enum dma_data_direction dir)
220 struct nvme_rdma_qe *ring;
223 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
227 for (i = 0; i < ib_queue_size; i++) {
228 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
235 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
239 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
241 pr_debug("QP event %s (%d)\n",
242 ib_event_msg(event->event), 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(struct blk_mq_tag_set *set,
319 struct request *rq, unsigned int hctx_idx)
321 return __nvme_rdma_exit_request(set->driver_data, rq, hctx_idx + 1);
324 static void nvme_rdma_exit_admin_request(struct blk_mq_tag_set *set,
325 struct request *rq, unsigned int hctx_idx)
327 return __nvme_rdma_exit_request(set->driver_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 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
344 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
346 if (IS_ERR(req->mr)) {
347 ret = PTR_ERR(req->mr);
356 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
361 static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
362 struct request *rq, unsigned int hctx_idx,
363 unsigned int numa_node)
365 return __nvme_rdma_init_request(set->driver_data, rq, hctx_idx + 1);
368 static int nvme_rdma_init_admin_request(struct blk_mq_tag_set *set,
369 struct request *rq, unsigned int hctx_idx,
370 unsigned int numa_node)
372 return __nvme_rdma_init_request(set->driver_data, rq, 0);
375 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
376 unsigned int hctx_idx)
378 struct nvme_rdma_ctrl *ctrl = data;
379 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
381 BUG_ON(hctx_idx >= ctrl->queue_count);
383 hctx->driver_data = queue;
387 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
388 unsigned int hctx_idx)
390 struct nvme_rdma_ctrl *ctrl = data;
391 struct nvme_rdma_queue *queue = &ctrl->queues[0];
393 BUG_ON(hctx_idx != 0);
395 hctx->driver_data = queue;
399 static void nvme_rdma_free_dev(struct kref *ref)
401 struct nvme_rdma_device *ndev =
402 container_of(ref, struct nvme_rdma_device, ref);
404 mutex_lock(&device_list_mutex);
405 list_del(&ndev->entry);
406 mutex_unlock(&device_list_mutex);
408 ib_dealloc_pd(ndev->pd);
412 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
414 kref_put(&dev->ref, nvme_rdma_free_dev);
417 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
419 return kref_get_unless_zero(&dev->ref);
422 static struct nvme_rdma_device *
423 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
425 struct nvme_rdma_device *ndev;
427 mutex_lock(&device_list_mutex);
428 list_for_each_entry(ndev, &device_list, entry) {
429 if (ndev->dev->node_guid == cm_id->device->node_guid &&
430 nvme_rdma_dev_get(ndev))
434 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
438 ndev->dev = cm_id->device;
439 kref_init(&ndev->ref);
441 ndev->pd = ib_alloc_pd(ndev->dev,
442 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
443 if (IS_ERR(ndev->pd))
446 if (!(ndev->dev->attrs.device_cap_flags &
447 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
448 dev_err(&ndev->dev->dev,
449 "Memory registrations not supported.\n");
453 list_add(&ndev->entry, &device_list);
455 mutex_unlock(&device_list_mutex);
459 ib_dealloc_pd(ndev->pd);
463 mutex_unlock(&device_list_mutex);
467 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
469 struct nvme_rdma_device *dev;
470 struct ib_device *ibdev;
472 if (!test_and_clear_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags))
477 rdma_destroy_qp(queue->cm_id);
478 ib_free_cq(queue->ib_cq);
480 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
481 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
483 nvme_rdma_dev_put(dev);
486 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue,
487 struct nvme_rdma_device *dev)
489 struct ib_device *ibdev = dev->dev;
490 const int send_wr_factor = 3; /* MR, SEND, INV */
491 const int cq_factor = send_wr_factor + 1; /* + RECV */
492 int comp_vector, idx = nvme_rdma_queue_idx(queue);
499 * The admin queue is barely used once the controller is live, so don't
500 * bother to spread it out.
505 comp_vector = idx % ibdev->num_comp_vectors;
508 /* +1 for ib_stop_cq */
509 queue->ib_cq = ib_alloc_cq(dev->dev, queue,
510 cq_factor * queue->queue_size + 1, comp_vector,
512 if (IS_ERR(queue->ib_cq)) {
513 ret = PTR_ERR(queue->ib_cq);
517 ret = nvme_rdma_create_qp(queue, send_wr_factor);
519 goto out_destroy_ib_cq;
521 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
522 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
523 if (!queue->rsp_ring) {
527 set_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags);
532 ib_destroy_qp(queue->qp);
534 ib_free_cq(queue->ib_cq);
539 static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl,
540 int idx, size_t queue_size)
542 struct nvme_rdma_queue *queue;
543 struct sockaddr *src_addr = NULL;
546 queue = &ctrl->queues[idx];
548 init_completion(&queue->cm_done);
551 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
553 queue->cmnd_capsule_len = sizeof(struct nvme_command);
555 queue->queue_size = queue_size;
557 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
558 RDMA_PS_TCP, IB_QPT_RC);
559 if (IS_ERR(queue->cm_id)) {
560 dev_info(ctrl->ctrl.device,
561 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
562 return PTR_ERR(queue->cm_id);
565 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
566 src_addr = (struct sockaddr *)&ctrl->src_addr;
568 queue->cm_error = -ETIMEDOUT;
569 ret = rdma_resolve_addr(queue->cm_id, src_addr,
570 (struct sockaddr *)&ctrl->addr,
571 NVME_RDMA_CONNECT_TIMEOUT_MS);
573 dev_info(ctrl->ctrl.device,
574 "rdma_resolve_addr failed (%d).\n", ret);
575 goto out_destroy_cm_id;
578 ret = nvme_rdma_wait_for_cm(queue);
580 dev_info(ctrl->ctrl.device,
581 "rdma_resolve_addr wait failed (%d).\n", ret);
582 goto out_destroy_cm_id;
585 clear_bit(NVME_RDMA_Q_DELETING, &queue->flags);
586 set_bit(NVME_RDMA_Q_CONNECTED, &queue->flags);
591 nvme_rdma_destroy_queue_ib(queue);
592 rdma_destroy_id(queue->cm_id);
596 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
598 rdma_disconnect(queue->cm_id);
599 ib_drain_qp(queue->qp);
602 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
604 nvme_rdma_destroy_queue_ib(queue);
605 rdma_destroy_id(queue->cm_id);
608 static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue)
610 if (test_and_set_bit(NVME_RDMA_Q_DELETING, &queue->flags))
612 nvme_rdma_stop_queue(queue);
613 nvme_rdma_free_queue(queue);
616 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
620 for (i = 1; i < ctrl->queue_count; i++)
621 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
624 static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl)
628 for (i = 1; i < ctrl->queue_count; i++) {
629 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
631 dev_info(ctrl->ctrl.device,
632 "failed to connect i/o queue: %d\n", ret);
633 goto out_free_queues;
635 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
641 nvme_rdma_free_io_queues(ctrl);
645 static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl)
647 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
648 unsigned int nr_io_queues;
651 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
652 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
656 ctrl->queue_count = nr_io_queues + 1;
657 if (ctrl->queue_count < 2)
660 dev_info(ctrl->ctrl.device,
661 "creating %d I/O queues.\n", nr_io_queues);
663 for (i = 1; i < ctrl->queue_count; i++) {
664 ret = nvme_rdma_init_queue(ctrl, i,
665 ctrl->ctrl.opts->queue_size);
667 dev_info(ctrl->ctrl.device,
668 "failed to initialize i/o queue: %d\n", ret);
669 goto out_free_queues;
676 for (i--; i >= 1; i--)
677 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
682 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl)
684 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
685 sizeof(struct nvme_command), DMA_TO_DEVICE);
686 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
687 blk_cleanup_queue(ctrl->ctrl.admin_q);
688 blk_mq_free_tag_set(&ctrl->admin_tag_set);
689 nvme_rdma_dev_put(ctrl->device);
692 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
694 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
696 if (list_empty(&ctrl->list))
699 mutex_lock(&nvme_rdma_ctrl_mutex);
700 list_del(&ctrl->list);
701 mutex_unlock(&nvme_rdma_ctrl_mutex);
704 nvmf_free_options(nctrl->opts);
709 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
711 /* If we are resetting/deleting then do nothing */
712 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
713 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
714 ctrl->ctrl.state == NVME_CTRL_LIVE);
718 if (nvmf_should_reconnect(&ctrl->ctrl)) {
719 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
720 ctrl->ctrl.opts->reconnect_delay);
721 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
722 ctrl->ctrl.opts->reconnect_delay * HZ);
724 dev_info(ctrl->ctrl.device, "Removing controller...\n");
725 queue_work(nvme_rdma_wq, &ctrl->delete_work);
729 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
731 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
732 struct nvme_rdma_ctrl, reconnect_work);
736 ++ctrl->ctrl.opts->nr_reconnects;
738 if (ctrl->queue_count > 1) {
739 nvme_rdma_free_io_queues(ctrl);
741 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
746 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
748 ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set);
752 ret = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
756 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
760 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
762 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
766 nvme_start_keep_alive(&ctrl->ctrl);
768 if (ctrl->queue_count > 1) {
769 ret = nvme_rdma_init_io_queues(ctrl);
773 ret = nvme_rdma_connect_io_queues(ctrl);
778 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
779 WARN_ON_ONCE(!changed);
780 ctrl->ctrl.opts->nr_reconnects = 0;
782 if (ctrl->queue_count > 1) {
783 nvme_queue_scan(&ctrl->ctrl);
784 nvme_queue_async_events(&ctrl->ctrl);
787 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
792 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
793 ctrl->ctrl.opts->nr_reconnects);
794 nvme_rdma_reconnect_or_remove(ctrl);
797 static void nvme_rdma_error_recovery_work(struct work_struct *work)
799 struct nvme_rdma_ctrl *ctrl = container_of(work,
800 struct nvme_rdma_ctrl, err_work);
803 nvme_stop_keep_alive(&ctrl->ctrl);
805 for (i = 0; i < ctrl->queue_count; i++) {
806 clear_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[i].flags);
807 clear_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
810 if (ctrl->queue_count > 1)
811 nvme_stop_queues(&ctrl->ctrl);
812 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
814 /* We must take care of fastfail/requeue all our inflight requests */
815 if (ctrl->queue_count > 1)
816 blk_mq_tagset_busy_iter(&ctrl->tag_set,
817 nvme_cancel_request, &ctrl->ctrl);
818 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
819 nvme_cancel_request, &ctrl->ctrl);
822 * queues are not a live anymore, so restart the queues to fail fast
825 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
826 nvme_start_queues(&ctrl->ctrl);
828 nvme_rdma_reconnect_or_remove(ctrl);
831 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
833 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
836 queue_work(nvme_rdma_wq, &ctrl->err_work);
839 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
842 struct nvme_rdma_queue *queue = cq->cq_context;
843 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
845 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
846 dev_info(ctrl->ctrl.device,
847 "%s for CQE 0x%p failed with status %s (%d)\n",
849 ib_wc_status_msg(wc->status), wc->status);
850 nvme_rdma_error_recovery(ctrl);
853 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
855 if (unlikely(wc->status != IB_WC_SUCCESS))
856 nvme_rdma_wr_error(cq, wc, "MEMREG");
859 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
861 if (unlikely(wc->status != IB_WC_SUCCESS))
862 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
865 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
866 struct nvme_rdma_request *req)
868 struct ib_send_wr *bad_wr;
869 struct ib_send_wr wr = {
870 .opcode = IB_WR_LOCAL_INV,
874 .ex.invalidate_rkey = req->mr->rkey,
877 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
878 wr.wr_cqe = &req->reg_cqe;
880 return ib_post_send(queue->qp, &wr, &bad_wr);
883 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
886 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
887 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
888 struct nvme_rdma_device *dev = queue->device;
889 struct ib_device *ibdev = dev->dev;
892 if (!blk_rq_bytes(rq))
895 if (req->mr->need_inval) {
896 res = nvme_rdma_inv_rkey(queue, req);
898 dev_err(ctrl->ctrl.device,
899 "Queueing INV WR for rkey %#x failed (%d)\n",
901 nvme_rdma_error_recovery(queue->ctrl);
905 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
906 req->nents, rq_data_dir(rq) ==
907 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
909 nvme_cleanup_cmd(rq);
910 sg_free_table_chained(&req->sg_table, true);
913 static int nvme_rdma_set_sg_null(struct nvme_command *c)
915 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
918 put_unaligned_le24(0, sg->length);
919 put_unaligned_le32(0, sg->key);
920 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
924 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
925 struct nvme_rdma_request *req, struct nvme_command *c)
927 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
929 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
930 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
931 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
933 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
934 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
935 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
937 req->inline_data = true;
942 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
943 struct nvme_rdma_request *req, struct nvme_command *c)
945 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
947 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
948 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
949 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
950 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
954 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
955 struct nvme_rdma_request *req, struct nvme_command *c,
958 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
961 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE);
968 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
970 req->reg_cqe.done = nvme_rdma_memreg_done;
971 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
972 req->reg_wr.wr.opcode = IB_WR_REG_MR;
973 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
974 req->reg_wr.wr.num_sge = 0;
975 req->reg_wr.mr = req->mr;
976 req->reg_wr.key = req->mr->rkey;
977 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
978 IB_ACCESS_REMOTE_READ |
979 IB_ACCESS_REMOTE_WRITE;
981 req->mr->need_inval = true;
983 sg->addr = cpu_to_le64(req->mr->iova);
984 put_unaligned_le24(req->mr->length, sg->length);
985 put_unaligned_le32(req->mr->rkey, sg->key);
986 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
987 NVME_SGL_FMT_INVALIDATE;
992 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
993 struct request *rq, struct nvme_command *c)
995 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
996 struct nvme_rdma_device *dev = queue->device;
997 struct ib_device *ibdev = dev->dev;
1001 req->inline_data = false;
1002 req->mr->need_inval = false;
1004 c->common.flags |= NVME_CMD_SGL_METABUF;
1006 if (!blk_rq_bytes(rq))
1007 return nvme_rdma_set_sg_null(c);
1009 req->sg_table.sgl = req->first_sgl;
1010 ret = sg_alloc_table_chained(&req->sg_table,
1011 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
1015 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
1017 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
1018 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1019 if (unlikely(count <= 0)) {
1020 sg_free_table_chained(&req->sg_table, true);
1025 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1026 blk_rq_payload_bytes(rq) <=
1027 nvme_rdma_inline_data_size(queue))
1028 return nvme_rdma_map_sg_inline(queue, req, c);
1030 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
1031 return nvme_rdma_map_sg_single(queue, req, c);
1034 return nvme_rdma_map_sg_fr(queue, req, c, count);
1037 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1039 if (unlikely(wc->status != IB_WC_SUCCESS))
1040 nvme_rdma_wr_error(cq, wc, "SEND");
1043 static inline int nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue)
1048 * We signal completion every queue depth/2 and also handle the
1049 * degenerated case of a device with queue_depth=1, where we
1050 * would need to signal every message.
1052 sig_limit = max(queue->queue_size / 2, 1);
1053 return (++queue->sig_count % sig_limit) == 0;
1056 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1057 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1058 struct ib_send_wr *first, bool flush)
1060 struct ib_send_wr wr, *bad_wr;
1063 sge->addr = qe->dma;
1064 sge->length = sizeof(struct nvme_command),
1065 sge->lkey = queue->device->pd->local_dma_lkey;
1067 qe->cqe.done = nvme_rdma_send_done;
1070 wr.wr_cqe = &qe->cqe;
1072 wr.num_sge = num_sge;
1073 wr.opcode = IB_WR_SEND;
1077 * Unsignalled send completions are another giant desaster in the
1078 * IB Verbs spec: If we don't regularly post signalled sends
1079 * the send queue will fill up and only a QP reset will rescue us.
1080 * Would have been way to obvious to handle this in hardware or
1081 * at least the RDMA stack..
1083 * Always signal the flushes. The magic request used for the flush
1084 * sequencer is not allocated in our driver's tagset and it's
1085 * triggered to be freed by blk_cleanup_queue(). So we need to
1086 * always mark it as signaled to ensure that the "wr_cqe", which is
1087 * embedded in request's payload, is not freed when __ib_process_cq()
1088 * calls wr_cqe->done().
1090 if (nvme_rdma_queue_sig_limit(queue) || flush)
1091 wr.send_flags |= IB_SEND_SIGNALED;
1098 ret = ib_post_send(queue->qp, first, &bad_wr);
1100 dev_err(queue->ctrl->ctrl.device,
1101 "%s failed with error code %d\n", __func__, ret);
1106 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1107 struct nvme_rdma_qe *qe)
1109 struct ib_recv_wr wr, *bad_wr;
1113 list.addr = qe->dma;
1114 list.length = sizeof(struct nvme_completion);
1115 list.lkey = queue->device->pd->local_dma_lkey;
1117 qe->cqe.done = nvme_rdma_recv_done;
1120 wr.wr_cqe = &qe->cqe;
1124 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1126 dev_err(queue->ctrl->ctrl.device,
1127 "%s failed with error code %d\n", __func__, ret);
1132 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1134 u32 queue_idx = nvme_rdma_queue_idx(queue);
1137 return queue->ctrl->admin_tag_set.tags[queue_idx];
1138 return queue->ctrl->tag_set.tags[queue_idx - 1];
1141 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1143 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1144 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1145 struct ib_device *dev = queue->device->dev;
1146 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1147 struct nvme_command *cmd = sqe->data;
1151 if (WARN_ON_ONCE(aer_idx != 0))
1154 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1156 memset(cmd, 0, sizeof(*cmd));
1157 cmd->common.opcode = nvme_admin_async_event;
1158 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1159 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1160 nvme_rdma_set_sg_null(cmd);
1162 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1165 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1169 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1170 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1173 struct nvme_rdma_request *req;
1176 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1178 dev_err(queue->ctrl->ctrl.device,
1179 "tag 0x%x on QP %#x not found\n",
1180 cqe->command_id, queue->qp->qp_num);
1181 nvme_rdma_error_recovery(queue->ctrl);
1184 req = blk_mq_rq_to_pdu(rq);
1189 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1190 wc->ex.invalidate_rkey == req->mr->rkey)
1191 req->mr->need_inval = false;
1193 nvme_end_request(rq, cqe->status, cqe->result);
1197 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1199 struct nvme_rdma_qe *qe =
1200 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1201 struct nvme_rdma_queue *queue = cq->cq_context;
1202 struct ib_device *ibdev = queue->device->dev;
1203 struct nvme_completion *cqe = qe->data;
1204 const size_t len = sizeof(struct nvme_completion);
1207 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1208 nvme_rdma_wr_error(cq, wc, "RECV");
1212 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1214 * AEN requests are special as they don't time out and can
1215 * survive any kind of queue freeze and often don't respond to
1216 * aborts. We don't even bother to allocate a struct request
1217 * for them but rather special case them here.
1219 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1220 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
1221 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1224 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1225 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1227 nvme_rdma_post_recv(queue, qe);
1231 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1233 __nvme_rdma_recv_done(cq, wc, -1);
1236 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1240 for (i = 0; i < queue->queue_size; i++) {
1241 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1243 goto out_destroy_queue_ib;
1248 out_destroy_queue_ib:
1249 nvme_rdma_destroy_queue_ib(queue);
1253 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1254 struct rdma_cm_event *ev)
1256 struct rdma_cm_id *cm_id = queue->cm_id;
1257 int status = ev->status;
1258 const char *rej_msg;
1259 const struct nvme_rdma_cm_rej *rej_data;
1262 rej_msg = rdma_reject_msg(cm_id, status);
1263 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1265 if (rej_data && rej_data_len >= sizeof(u16)) {
1266 u16 sts = le16_to_cpu(rej_data->sts);
1268 dev_err(queue->ctrl->ctrl.device,
1269 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1270 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1272 dev_err(queue->ctrl->ctrl.device,
1273 "Connect rejected: status %d (%s).\n", status, rej_msg);
1279 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1281 struct nvme_rdma_device *dev;
1284 dev = nvme_rdma_find_get_device(queue->cm_id);
1286 dev_err(queue->cm_id->device->dev.parent,
1287 "no client data found!\n");
1288 return -ECONNREFUSED;
1291 ret = nvme_rdma_create_queue_ib(queue, dev);
1293 nvme_rdma_dev_put(dev);
1297 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1299 dev_err(queue->ctrl->ctrl.device,
1300 "rdma_resolve_route failed (%d).\n",
1302 goto out_destroy_queue;
1308 nvme_rdma_destroy_queue_ib(queue);
1313 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1315 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1316 struct rdma_conn_param param = { };
1317 struct nvme_rdma_cm_req priv = { };
1320 param.qp_num = queue->qp->qp_num;
1321 param.flow_control = 1;
1323 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1324 /* maximum retry count */
1325 param.retry_count = 7;
1326 param.rnr_retry_count = 7;
1327 param.private_data = &priv;
1328 param.private_data_len = sizeof(priv);
1330 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1331 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1333 * set the admin queue depth to the minimum size
1334 * specified by the Fabrics standard.
1336 if (priv.qid == 0) {
1337 priv.hrqsize = cpu_to_le16(NVMF_AQ_DEPTH);
1338 priv.hsqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1);
1341 * current interpretation of the fabrics spec
1342 * is at minimum you make hrqsize sqsize+1, or a
1343 * 1's based representation of sqsize.
1345 priv.hrqsize = cpu_to_le16(queue->queue_size);
1346 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1349 ret = rdma_connect(queue->cm_id, ¶m);
1351 dev_err(ctrl->ctrl.device,
1352 "rdma_connect failed (%d).\n", ret);
1353 goto out_destroy_queue_ib;
1358 out_destroy_queue_ib:
1359 nvme_rdma_destroy_queue_ib(queue);
1363 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1364 struct rdma_cm_event *ev)
1366 struct nvme_rdma_queue *queue = cm_id->context;
1369 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1370 rdma_event_msg(ev->event), ev->event,
1373 switch (ev->event) {
1374 case RDMA_CM_EVENT_ADDR_RESOLVED:
1375 cm_error = nvme_rdma_addr_resolved(queue);
1377 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1378 cm_error = nvme_rdma_route_resolved(queue);
1380 case RDMA_CM_EVENT_ESTABLISHED:
1381 queue->cm_error = nvme_rdma_conn_established(queue);
1382 /* complete cm_done regardless of success/failure */
1383 complete(&queue->cm_done);
1385 case RDMA_CM_EVENT_REJECTED:
1386 cm_error = nvme_rdma_conn_rejected(queue, ev);
1388 case RDMA_CM_EVENT_ADDR_ERROR:
1389 case RDMA_CM_EVENT_ROUTE_ERROR:
1390 case RDMA_CM_EVENT_CONNECT_ERROR:
1391 case RDMA_CM_EVENT_UNREACHABLE:
1392 dev_dbg(queue->ctrl->ctrl.device,
1393 "CM error event %d\n", ev->event);
1394 cm_error = -ECONNRESET;
1396 case RDMA_CM_EVENT_DISCONNECTED:
1397 case RDMA_CM_EVENT_ADDR_CHANGE:
1398 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1399 dev_dbg(queue->ctrl->ctrl.device,
1400 "disconnect received - connection closed\n");
1401 nvme_rdma_error_recovery(queue->ctrl);
1403 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1404 /* device removal is handled via the ib_client API */
1407 dev_err(queue->ctrl->ctrl.device,
1408 "Unexpected RDMA CM event (%d)\n", ev->event);
1409 nvme_rdma_error_recovery(queue->ctrl);
1414 queue->cm_error = cm_error;
1415 complete(&queue->cm_done);
1421 static enum blk_eh_timer_return
1422 nvme_rdma_timeout(struct request *rq, bool reserved)
1424 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1426 /* queue error recovery */
1427 nvme_rdma_error_recovery(req->queue->ctrl);
1429 /* fail with DNR on cmd timeout */
1430 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1432 return BLK_EH_HANDLED;
1436 * We cannot accept any other command until the Connect command has completed.
1438 static inline int nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue,
1441 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
1442 struct nvme_command *cmd = nvme_req(rq)->cmd;
1444 if (!blk_rq_is_passthrough(rq) ||
1445 cmd->common.opcode != nvme_fabrics_command ||
1446 cmd->fabrics.fctype != nvme_fabrics_type_connect) {
1448 * reconnecting state means transport disruption, which
1449 * can take a long time and even might fail permanently,
1450 * so we can't let incoming I/O be requeued forever.
1451 * fail it fast to allow upper layers a chance to
1454 if (queue->ctrl->ctrl.state == NVME_CTRL_RECONNECTING)
1464 static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1465 const struct blk_mq_queue_data *bd)
1467 struct nvme_ns *ns = hctx->queue->queuedata;
1468 struct nvme_rdma_queue *queue = hctx->driver_data;
1469 struct request *rq = bd->rq;
1470 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1471 struct nvme_rdma_qe *sqe = &req->sqe;
1472 struct nvme_command *c = sqe->data;
1474 struct ib_device *dev;
1477 WARN_ON_ONCE(rq->tag < 0);
1479 ret = nvme_rdma_queue_is_ready(queue, rq);
1483 dev = queue->device->dev;
1484 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1485 sizeof(struct nvme_command), DMA_TO_DEVICE);
1487 ret = nvme_setup_cmd(ns, rq, c);
1488 if (ret != BLK_MQ_RQ_QUEUE_OK)
1491 blk_mq_start_request(rq);
1493 ret = nvme_rdma_map_data(queue, rq, c);
1495 dev_err(queue->ctrl->ctrl.device,
1496 "Failed to map data (%d)\n", ret);
1497 nvme_cleanup_cmd(rq);
1501 ib_dma_sync_single_for_device(dev, sqe->dma,
1502 sizeof(struct nvme_command), DMA_TO_DEVICE);
1504 if (req_op(rq) == REQ_OP_FLUSH)
1506 ret = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1507 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
1509 nvme_rdma_unmap_data(queue, rq);
1513 return BLK_MQ_RQ_QUEUE_OK;
1515 return (ret == -ENOMEM || ret == -EAGAIN) ?
1516 BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR;
1519 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1521 struct nvme_rdma_queue *queue = hctx->driver_data;
1522 struct ib_cq *cq = queue->ib_cq;
1526 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1527 while (ib_poll_cq(cq, 1, &wc) > 0) {
1528 struct ib_cqe *cqe = wc.wr_cqe;
1531 if (cqe->done == nvme_rdma_recv_done)
1532 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1541 static void nvme_rdma_complete_rq(struct request *rq)
1543 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1545 nvme_rdma_unmap_data(req->queue, rq);
1546 nvme_complete_rq(rq);
1549 static const struct blk_mq_ops nvme_rdma_mq_ops = {
1550 .queue_rq = nvme_rdma_queue_rq,
1551 .complete = nvme_rdma_complete_rq,
1552 .init_request = nvme_rdma_init_request,
1553 .exit_request = nvme_rdma_exit_request,
1554 .reinit_request = nvme_rdma_reinit_request,
1555 .init_hctx = nvme_rdma_init_hctx,
1556 .poll = nvme_rdma_poll,
1557 .timeout = nvme_rdma_timeout,
1560 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1561 .queue_rq = nvme_rdma_queue_rq,
1562 .complete = nvme_rdma_complete_rq,
1563 .init_request = nvme_rdma_init_admin_request,
1564 .exit_request = nvme_rdma_exit_admin_request,
1565 .reinit_request = nvme_rdma_reinit_request,
1566 .init_hctx = nvme_rdma_init_admin_hctx,
1567 .timeout = nvme_rdma_timeout,
1570 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
1574 error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
1578 ctrl->device = ctrl->queues[0].device;
1581 * We need a reference on the device as long as the tag_set is alive,
1582 * as the MRs in the request structures need a valid ib_device.
1585 if (!nvme_rdma_dev_get(ctrl->device))
1586 goto out_free_queue;
1588 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
1589 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
1591 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1592 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
1593 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
1594 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
1595 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1596 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1597 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1598 ctrl->admin_tag_set.driver_data = ctrl;
1599 ctrl->admin_tag_set.nr_hw_queues = 1;
1600 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1602 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1606 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1607 if (IS_ERR(ctrl->ctrl.admin_q)) {
1608 error = PTR_ERR(ctrl->ctrl.admin_q);
1609 goto out_free_tagset;
1612 error = nvmf_connect_admin_queue(&ctrl->ctrl);
1614 goto out_cleanup_queue;
1616 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
1618 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
1620 dev_err(ctrl->ctrl.device,
1621 "prop_get NVME_REG_CAP failed\n");
1622 goto out_cleanup_queue;
1626 min_t(int, NVME_CAP_MQES(ctrl->cap), ctrl->ctrl.sqsize);
1628 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
1630 goto out_cleanup_queue;
1632 ctrl->ctrl.max_hw_sectors =
1633 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
1635 error = nvme_init_identify(&ctrl->ctrl);
1637 goto out_cleanup_queue;
1639 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
1640 &ctrl->async_event_sqe, sizeof(struct nvme_command),
1643 goto out_cleanup_queue;
1645 nvme_start_keep_alive(&ctrl->ctrl);
1650 blk_cleanup_queue(ctrl->ctrl.admin_q);
1652 /* disconnect and drain the queue before freeing the tagset */
1653 nvme_rdma_stop_queue(&ctrl->queues[0]);
1654 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1656 nvme_rdma_dev_put(ctrl->device);
1658 nvme_rdma_free_queue(&ctrl->queues[0]);
1662 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1664 nvme_stop_keep_alive(&ctrl->ctrl);
1665 cancel_work_sync(&ctrl->err_work);
1666 cancel_delayed_work_sync(&ctrl->reconnect_work);
1668 if (ctrl->queue_count > 1) {
1669 nvme_stop_queues(&ctrl->ctrl);
1670 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1671 nvme_cancel_request, &ctrl->ctrl);
1672 nvme_rdma_free_io_queues(ctrl);
1675 if (test_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[0].flags))
1676 nvme_shutdown_ctrl(&ctrl->ctrl);
1678 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
1679 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1680 nvme_cancel_request, &ctrl->ctrl);
1681 nvme_rdma_destroy_admin_queue(ctrl);
1684 static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1686 nvme_uninit_ctrl(&ctrl->ctrl);
1688 nvme_rdma_shutdown_ctrl(ctrl);
1690 if (ctrl->ctrl.tagset) {
1691 blk_cleanup_queue(ctrl->ctrl.connect_q);
1692 blk_mq_free_tag_set(&ctrl->tag_set);
1693 nvme_rdma_dev_put(ctrl->device);
1696 nvme_put_ctrl(&ctrl->ctrl);
1699 static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1701 struct nvme_rdma_ctrl *ctrl = container_of(work,
1702 struct nvme_rdma_ctrl, delete_work);
1704 __nvme_rdma_remove_ctrl(ctrl, true);
1707 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1709 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1712 if (!queue_work(nvme_rdma_wq, &ctrl->delete_work))
1718 static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1720 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1724 * Keep a reference until all work is flushed since
1725 * __nvme_rdma_del_ctrl can free the ctrl mem
1727 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1729 ret = __nvme_rdma_del_ctrl(ctrl);
1731 flush_work(&ctrl->delete_work);
1732 nvme_put_ctrl(&ctrl->ctrl);
1736 static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1738 struct nvme_rdma_ctrl *ctrl = container_of(work,
1739 struct nvme_rdma_ctrl, delete_work);
1741 __nvme_rdma_remove_ctrl(ctrl, false);
1744 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1746 struct nvme_rdma_ctrl *ctrl = container_of(work,
1747 struct nvme_rdma_ctrl, reset_work);
1751 nvme_rdma_shutdown_ctrl(ctrl);
1753 ret = nvme_rdma_configure_admin_queue(ctrl);
1755 /* ctrl is already shutdown, just remove the ctrl */
1756 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1760 if (ctrl->queue_count > 1) {
1761 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
1765 ret = nvme_rdma_init_io_queues(ctrl);
1769 ret = nvme_rdma_connect_io_queues(ctrl);
1774 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1775 WARN_ON_ONCE(!changed);
1777 if (ctrl->queue_count > 1) {
1778 nvme_start_queues(&ctrl->ctrl);
1779 nvme_queue_scan(&ctrl->ctrl);
1780 nvme_queue_async_events(&ctrl->ctrl);
1786 /* Deleting this dead controller... */
1787 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1788 WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work));
1791 static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl)
1793 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1795 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1798 if (!queue_work(nvme_rdma_wq, &ctrl->reset_work))
1801 flush_work(&ctrl->reset_work);
1806 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1808 .module = THIS_MODULE,
1809 .flags = NVME_F_FABRICS,
1810 .reg_read32 = nvmf_reg_read32,
1811 .reg_read64 = nvmf_reg_read64,
1812 .reg_write32 = nvmf_reg_write32,
1813 .reset_ctrl = nvme_rdma_reset_ctrl,
1814 .free_ctrl = nvme_rdma_free_ctrl,
1815 .submit_async_event = nvme_rdma_submit_async_event,
1816 .delete_ctrl = nvme_rdma_del_ctrl,
1817 .get_subsysnqn = nvmf_get_subsysnqn,
1818 .get_address = nvmf_get_address,
1821 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1825 ret = nvme_rdma_init_io_queues(ctrl);
1830 * We need a reference on the device as long as the tag_set is alive,
1831 * as the MRs in the request structures need a valid ib_device.
1834 if (!nvme_rdma_dev_get(ctrl->device))
1835 goto out_free_io_queues;
1837 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1838 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
1839 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
1840 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1841 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1842 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1843 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1844 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1845 ctrl->tag_set.driver_data = ctrl;
1846 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
1847 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1849 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1852 ctrl->ctrl.tagset = &ctrl->tag_set;
1854 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1855 if (IS_ERR(ctrl->ctrl.connect_q)) {
1856 ret = PTR_ERR(ctrl->ctrl.connect_q);
1857 goto out_free_tag_set;
1860 ret = nvme_rdma_connect_io_queues(ctrl);
1862 goto out_cleanup_connect_q;
1866 out_cleanup_connect_q:
1867 blk_cleanup_queue(ctrl->ctrl.connect_q);
1869 blk_mq_free_tag_set(&ctrl->tag_set);
1871 nvme_rdma_dev_put(ctrl->device);
1873 nvme_rdma_free_io_queues(ctrl);
1877 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1878 struct nvmf_ctrl_options *opts)
1880 struct nvme_rdma_ctrl *ctrl;
1885 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1887 return ERR_PTR(-ENOMEM);
1888 ctrl->ctrl.opts = opts;
1889 INIT_LIST_HEAD(&ctrl->list);
1891 if (opts->mask & NVMF_OPT_TRSVCID)
1892 port = opts->trsvcid;
1894 port = __stringify(NVME_RDMA_IP_PORT);
1896 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1897 opts->traddr, port, &ctrl->addr);
1899 pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
1903 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
1904 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1905 opts->host_traddr, NULL, &ctrl->src_addr);
1907 pr_err("malformed src address passed: %s\n",
1913 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1914 0 /* no quirks, we're perfect! */);
1918 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1919 nvme_rdma_reconnect_ctrl_work);
1920 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1921 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1922 INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work);
1923 spin_lock_init(&ctrl->lock);
1925 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1926 ctrl->ctrl.sqsize = opts->queue_size - 1;
1927 ctrl->ctrl.kato = opts->kato;
1930 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues),
1933 goto out_uninit_ctrl;
1935 ret = nvme_rdma_configure_admin_queue(ctrl);
1937 goto out_kfree_queues;
1939 /* sanity check icdoff */
1940 if (ctrl->ctrl.icdoff) {
1941 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1942 goto out_remove_admin_queue;
1945 /* sanity check keyed sgls */
1946 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1947 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1948 goto out_remove_admin_queue;
1951 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1952 /* warn if maxcmd is lower than queue_size */
1953 dev_warn(ctrl->ctrl.device,
1954 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1955 opts->queue_size, ctrl->ctrl.maxcmd);
1956 opts->queue_size = ctrl->ctrl.maxcmd;
1959 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1960 /* warn if sqsize is lower than queue_size */
1961 dev_warn(ctrl->ctrl.device,
1962 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1963 opts->queue_size, ctrl->ctrl.sqsize + 1);
1964 opts->queue_size = ctrl->ctrl.sqsize + 1;
1967 if (opts->nr_io_queues) {
1968 ret = nvme_rdma_create_io_queues(ctrl);
1970 goto out_remove_admin_queue;
1973 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1974 WARN_ON_ONCE(!changed);
1976 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
1977 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1979 kref_get(&ctrl->ctrl.kref);
1981 mutex_lock(&nvme_rdma_ctrl_mutex);
1982 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1983 mutex_unlock(&nvme_rdma_ctrl_mutex);
1985 if (opts->nr_io_queues) {
1986 nvme_queue_scan(&ctrl->ctrl);
1987 nvme_queue_async_events(&ctrl->ctrl);
1992 out_remove_admin_queue:
1993 nvme_stop_keep_alive(&ctrl->ctrl);
1994 nvme_rdma_destroy_admin_queue(ctrl);
1996 kfree(ctrl->queues);
1998 nvme_uninit_ctrl(&ctrl->ctrl);
1999 nvme_put_ctrl(&ctrl->ctrl);
2002 return ERR_PTR(ret);
2005 return ERR_PTR(ret);
2008 static struct nvmf_transport_ops nvme_rdma_transport = {
2010 .required_opts = NVMF_OPT_TRADDR,
2011 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2012 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
2013 .create_ctrl = nvme_rdma_create_ctrl,
2016 static void nvme_rdma_add_one(struct ib_device *ib_device)
2020 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2022 struct nvme_rdma_ctrl *ctrl;
2024 /* Delete all controllers using this device */
2025 mutex_lock(&nvme_rdma_ctrl_mutex);
2026 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2027 if (ctrl->device->dev != ib_device)
2029 dev_info(ctrl->ctrl.device,
2030 "Removing ctrl: NQN \"%s\", addr %pISp\n",
2031 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2032 __nvme_rdma_del_ctrl(ctrl);
2034 mutex_unlock(&nvme_rdma_ctrl_mutex);
2036 flush_workqueue(nvme_rdma_wq);
2039 static struct ib_client nvme_rdma_ib_client = {
2040 .name = "nvme_rdma",
2041 .add = nvme_rdma_add_one,
2042 .remove = nvme_rdma_remove_one
2045 static int __init nvme_rdma_init_module(void)
2049 nvme_rdma_wq = create_workqueue("nvme_rdma_wq");
2053 ret = ib_register_client(&nvme_rdma_ib_client);
2055 goto err_destroy_wq;
2057 ret = nvmf_register_transport(&nvme_rdma_transport);
2059 goto err_unreg_client;
2064 ib_unregister_client(&nvme_rdma_ib_client);
2066 destroy_workqueue(nvme_rdma_wq);
2070 static void __exit nvme_rdma_cleanup_module(void)
2072 nvmf_unregister_transport(&nvme_rdma_transport);
2073 ib_unregister_client(&nvme_rdma_ib_client);
2074 destroy_workqueue(nvme_rdma_wq);
2077 module_init(nvme_rdma_init_module);
2078 module_exit(nvme_rdma_cleanup_module);
2080 MODULE_LICENSE("GPL v2");