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(void *data, struct request *rq,
319 unsigned int hctx_idx, unsigned int rq_idx)
321 return __nvme_rdma_exit_request(data, rq, hctx_idx + 1);
324 static void nvme_rdma_exit_admin_request(void *data, struct request *rq,
325 unsigned int hctx_idx, unsigned int rq_idx)
327 return __nvme_rdma_exit_request(data, rq, 0);
330 static int __nvme_rdma_init_request(struct nvme_rdma_ctrl *ctrl,
331 struct request *rq, unsigned int queue_idx)
333 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
334 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
335 struct nvme_rdma_device *dev = queue->device;
336 struct ib_device *ibdev = dev->dev;
339 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(void *data, struct request *rq,
362 unsigned int hctx_idx, unsigned int rq_idx,
363 unsigned int numa_node)
365 return __nvme_rdma_init_request(data, rq, hctx_idx + 1);
368 static int nvme_rdma_init_admin_request(void *data, struct request *rq,
369 unsigned int hctx_idx, unsigned int rq_idx,
370 unsigned int numa_node)
372 return __nvme_rdma_init_request(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 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
758 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
762 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
764 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
768 nvme_start_keep_alive(&ctrl->ctrl);
770 if (ctrl->queue_count > 1) {
771 ret = nvme_rdma_init_io_queues(ctrl);
775 ret = nvme_rdma_connect_io_queues(ctrl);
780 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
781 WARN_ON_ONCE(!changed);
782 ctrl->ctrl.opts->nr_reconnects = 0;
784 if (ctrl->queue_count > 1) {
785 nvme_start_queues(&ctrl->ctrl);
786 nvme_queue_scan(&ctrl->ctrl);
787 nvme_queue_async_events(&ctrl->ctrl);
790 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
795 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
797 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
798 ctrl->ctrl.opts->nr_reconnects);
799 nvme_rdma_reconnect_or_remove(ctrl);
802 static void nvme_rdma_error_recovery_work(struct work_struct *work)
804 struct nvme_rdma_ctrl *ctrl = container_of(work,
805 struct nvme_rdma_ctrl, err_work);
808 nvme_stop_keep_alive(&ctrl->ctrl);
810 for (i = 0; i < ctrl->queue_count; i++) {
811 clear_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[i].flags);
812 clear_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
815 if (ctrl->queue_count > 1)
816 nvme_stop_queues(&ctrl->ctrl);
817 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
819 /* We must take care of fastfail/requeue all our inflight requests */
820 if (ctrl->queue_count > 1)
821 blk_mq_tagset_busy_iter(&ctrl->tag_set,
822 nvme_cancel_request, &ctrl->ctrl);
823 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
824 nvme_cancel_request, &ctrl->ctrl);
826 nvme_rdma_reconnect_or_remove(ctrl);
829 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
831 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
834 queue_work(nvme_rdma_wq, &ctrl->err_work);
837 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
840 struct nvme_rdma_queue *queue = cq->cq_context;
841 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
843 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
844 dev_info(ctrl->ctrl.device,
845 "%s for CQE 0x%p failed with status %s (%d)\n",
847 ib_wc_status_msg(wc->status), wc->status);
848 nvme_rdma_error_recovery(ctrl);
851 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
853 if (unlikely(wc->status != IB_WC_SUCCESS))
854 nvme_rdma_wr_error(cq, wc, "MEMREG");
857 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
859 if (unlikely(wc->status != IB_WC_SUCCESS))
860 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
863 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
864 struct nvme_rdma_request *req)
866 struct ib_send_wr *bad_wr;
867 struct ib_send_wr wr = {
868 .opcode = IB_WR_LOCAL_INV,
872 .ex.invalidate_rkey = req->mr->rkey,
875 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
876 wr.wr_cqe = &req->reg_cqe;
878 return ib_post_send(queue->qp, &wr, &bad_wr);
881 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
884 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
885 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
886 struct nvme_rdma_device *dev = queue->device;
887 struct ib_device *ibdev = dev->dev;
890 if (!blk_rq_bytes(rq))
893 if (req->mr->need_inval) {
894 res = nvme_rdma_inv_rkey(queue, req);
896 dev_err(ctrl->ctrl.device,
897 "Queueing INV WR for rkey %#x failed (%d)\n",
899 nvme_rdma_error_recovery(queue->ctrl);
903 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
904 req->nents, rq_data_dir(rq) ==
905 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
907 nvme_cleanup_cmd(rq);
908 sg_free_table_chained(&req->sg_table, true);
911 static int nvme_rdma_set_sg_null(struct nvme_command *c)
913 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
916 put_unaligned_le24(0, sg->length);
917 put_unaligned_le32(0, sg->key);
918 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
922 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
923 struct nvme_rdma_request *req, struct nvme_command *c)
925 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
927 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
928 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
929 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
931 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
932 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
933 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
935 req->inline_data = true;
940 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
941 struct nvme_rdma_request *req, struct nvme_command *c)
943 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
945 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
946 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
947 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
948 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
952 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
953 struct nvme_rdma_request *req, struct nvme_command *c,
956 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
959 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE);
966 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
968 req->reg_cqe.done = nvme_rdma_memreg_done;
969 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
970 req->reg_wr.wr.opcode = IB_WR_REG_MR;
971 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
972 req->reg_wr.wr.num_sge = 0;
973 req->reg_wr.mr = req->mr;
974 req->reg_wr.key = req->mr->rkey;
975 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
976 IB_ACCESS_REMOTE_READ |
977 IB_ACCESS_REMOTE_WRITE;
979 req->mr->need_inval = true;
981 sg->addr = cpu_to_le64(req->mr->iova);
982 put_unaligned_le24(req->mr->length, sg->length);
983 put_unaligned_le32(req->mr->rkey, sg->key);
984 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
985 NVME_SGL_FMT_INVALIDATE;
990 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
991 struct request *rq, struct nvme_command *c)
993 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
994 struct nvme_rdma_device *dev = queue->device;
995 struct ib_device *ibdev = dev->dev;
999 req->inline_data = false;
1000 req->mr->need_inval = false;
1002 c->common.flags |= NVME_CMD_SGL_METABUF;
1004 if (!blk_rq_bytes(rq))
1005 return nvme_rdma_set_sg_null(c);
1007 req->sg_table.sgl = req->first_sgl;
1008 ret = sg_alloc_table_chained(&req->sg_table,
1009 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
1013 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
1015 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
1016 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1017 if (unlikely(count <= 0)) {
1018 sg_free_table_chained(&req->sg_table, true);
1023 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1024 blk_rq_payload_bytes(rq) <=
1025 nvme_rdma_inline_data_size(queue))
1026 return nvme_rdma_map_sg_inline(queue, req, c);
1028 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
1029 return nvme_rdma_map_sg_single(queue, req, c);
1032 return nvme_rdma_map_sg_fr(queue, req, c, count);
1035 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1037 if (unlikely(wc->status != IB_WC_SUCCESS))
1038 nvme_rdma_wr_error(cq, wc, "SEND");
1041 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1042 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1043 struct ib_send_wr *first, bool flush)
1045 struct ib_send_wr wr, *bad_wr;
1048 sge->addr = qe->dma;
1049 sge->length = sizeof(struct nvme_command),
1050 sge->lkey = queue->device->pd->local_dma_lkey;
1052 qe->cqe.done = nvme_rdma_send_done;
1055 wr.wr_cqe = &qe->cqe;
1057 wr.num_sge = num_sge;
1058 wr.opcode = IB_WR_SEND;
1062 * Unsignalled send completions are another giant desaster in the
1063 * IB Verbs spec: If we don't regularly post signalled sends
1064 * the send queue will fill up and only a QP reset will rescue us.
1065 * Would have been way to obvious to handle this in hardware or
1066 * at least the RDMA stack..
1068 * This messy and racy code sniplet is copy and pasted from the iSER
1069 * initiator, and the magic '32' comes from there as well.
1071 * Always signal the flushes. The magic request used for the flush
1072 * sequencer is not allocated in our driver's tagset and it's
1073 * triggered to be freed by blk_cleanup_queue(). So we need to
1074 * always mark it as signaled to ensure that the "wr_cqe", which is
1075 * embedded in request's payload, is not freed when __ib_process_cq()
1076 * calls wr_cqe->done().
1078 if ((++queue->sig_count % 32) == 0 || flush)
1079 wr.send_flags |= IB_SEND_SIGNALED;
1086 ret = ib_post_send(queue->qp, first, &bad_wr);
1088 dev_err(queue->ctrl->ctrl.device,
1089 "%s failed with error code %d\n", __func__, ret);
1094 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1095 struct nvme_rdma_qe *qe)
1097 struct ib_recv_wr wr, *bad_wr;
1101 list.addr = qe->dma;
1102 list.length = sizeof(struct nvme_completion);
1103 list.lkey = queue->device->pd->local_dma_lkey;
1105 qe->cqe.done = nvme_rdma_recv_done;
1108 wr.wr_cqe = &qe->cqe;
1112 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1114 dev_err(queue->ctrl->ctrl.device,
1115 "%s failed with error code %d\n", __func__, ret);
1120 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1122 u32 queue_idx = nvme_rdma_queue_idx(queue);
1125 return queue->ctrl->admin_tag_set.tags[queue_idx];
1126 return queue->ctrl->tag_set.tags[queue_idx - 1];
1129 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1131 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1132 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1133 struct ib_device *dev = queue->device->dev;
1134 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1135 struct nvme_command *cmd = sqe->data;
1139 if (WARN_ON_ONCE(aer_idx != 0))
1142 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1144 memset(cmd, 0, sizeof(*cmd));
1145 cmd->common.opcode = nvme_admin_async_event;
1146 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1147 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1148 nvme_rdma_set_sg_null(cmd);
1150 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1153 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1157 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1158 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1161 struct nvme_rdma_request *req;
1164 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1166 dev_err(queue->ctrl->ctrl.device,
1167 "tag 0x%x on QP %#x not found\n",
1168 cqe->command_id, queue->qp->qp_num);
1169 nvme_rdma_error_recovery(queue->ctrl);
1172 req = blk_mq_rq_to_pdu(rq);
1177 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1178 wc->ex.invalidate_rkey == req->mr->rkey)
1179 req->mr->need_inval = false;
1181 nvme_end_request(rq, cqe->status, cqe->result);
1185 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1187 struct nvme_rdma_qe *qe =
1188 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1189 struct nvme_rdma_queue *queue = cq->cq_context;
1190 struct ib_device *ibdev = queue->device->dev;
1191 struct nvme_completion *cqe = qe->data;
1192 const size_t len = sizeof(struct nvme_completion);
1195 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1196 nvme_rdma_wr_error(cq, wc, "RECV");
1200 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1202 * AEN requests are special as they don't time out and can
1203 * survive any kind of queue freeze and often don't respond to
1204 * aborts. We don't even bother to allocate a struct request
1205 * for them but rather special case them here.
1207 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1208 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
1209 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1212 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1213 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1215 nvme_rdma_post_recv(queue, qe);
1219 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1221 __nvme_rdma_recv_done(cq, wc, -1);
1224 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1228 for (i = 0; i < queue->queue_size; i++) {
1229 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1231 goto out_destroy_queue_ib;
1236 out_destroy_queue_ib:
1237 nvme_rdma_destroy_queue_ib(queue);
1241 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1242 struct rdma_cm_event *ev)
1244 struct rdma_cm_id *cm_id = queue->cm_id;
1245 int status = ev->status;
1246 const char *rej_msg;
1247 const struct nvme_rdma_cm_rej *rej_data;
1250 rej_msg = rdma_reject_msg(cm_id, status);
1251 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1253 if (rej_data && rej_data_len >= sizeof(u16)) {
1254 u16 sts = le16_to_cpu(rej_data->sts);
1256 dev_err(queue->ctrl->ctrl.device,
1257 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1258 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1260 dev_err(queue->ctrl->ctrl.device,
1261 "Connect rejected: status %d (%s).\n", status, rej_msg);
1267 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1269 struct nvme_rdma_device *dev;
1272 dev = nvme_rdma_find_get_device(queue->cm_id);
1274 dev_err(queue->cm_id->device->dev.parent,
1275 "no client data found!\n");
1276 return -ECONNREFUSED;
1279 ret = nvme_rdma_create_queue_ib(queue, dev);
1281 nvme_rdma_dev_put(dev);
1285 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1287 dev_err(queue->ctrl->ctrl.device,
1288 "rdma_resolve_route failed (%d).\n",
1290 goto out_destroy_queue;
1296 nvme_rdma_destroy_queue_ib(queue);
1301 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1303 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1304 struct rdma_conn_param param = { };
1305 struct nvme_rdma_cm_req priv = { };
1308 param.qp_num = queue->qp->qp_num;
1309 param.flow_control = 1;
1311 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1312 /* maximum retry count */
1313 param.retry_count = 7;
1314 param.rnr_retry_count = 7;
1315 param.private_data = &priv;
1316 param.private_data_len = sizeof(priv);
1318 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1319 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1321 * set the admin queue depth to the minimum size
1322 * specified by the Fabrics standard.
1324 if (priv.qid == 0) {
1325 priv.hrqsize = cpu_to_le16(NVMF_AQ_DEPTH);
1326 priv.hsqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1);
1329 * current interpretation of the fabrics spec
1330 * is at minimum you make hrqsize sqsize+1, or a
1331 * 1's based representation of sqsize.
1333 priv.hrqsize = cpu_to_le16(queue->queue_size);
1334 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1337 ret = rdma_connect(queue->cm_id, ¶m);
1339 dev_err(ctrl->ctrl.device,
1340 "rdma_connect failed (%d).\n", ret);
1341 goto out_destroy_queue_ib;
1346 out_destroy_queue_ib:
1347 nvme_rdma_destroy_queue_ib(queue);
1351 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1352 struct rdma_cm_event *ev)
1354 struct nvme_rdma_queue *queue = cm_id->context;
1357 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1358 rdma_event_msg(ev->event), ev->event,
1361 switch (ev->event) {
1362 case RDMA_CM_EVENT_ADDR_RESOLVED:
1363 cm_error = nvme_rdma_addr_resolved(queue);
1365 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1366 cm_error = nvme_rdma_route_resolved(queue);
1368 case RDMA_CM_EVENT_ESTABLISHED:
1369 queue->cm_error = nvme_rdma_conn_established(queue);
1370 /* complete cm_done regardless of success/failure */
1371 complete(&queue->cm_done);
1373 case RDMA_CM_EVENT_REJECTED:
1374 cm_error = nvme_rdma_conn_rejected(queue, ev);
1376 case RDMA_CM_EVENT_ADDR_ERROR:
1377 case RDMA_CM_EVENT_ROUTE_ERROR:
1378 case RDMA_CM_EVENT_CONNECT_ERROR:
1379 case RDMA_CM_EVENT_UNREACHABLE:
1380 dev_dbg(queue->ctrl->ctrl.device,
1381 "CM error event %d\n", ev->event);
1382 cm_error = -ECONNRESET;
1384 case RDMA_CM_EVENT_DISCONNECTED:
1385 case RDMA_CM_EVENT_ADDR_CHANGE:
1386 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1387 dev_dbg(queue->ctrl->ctrl.device,
1388 "disconnect received - connection closed\n");
1389 nvme_rdma_error_recovery(queue->ctrl);
1391 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1392 /* device removal is handled via the ib_client API */
1395 dev_err(queue->ctrl->ctrl.device,
1396 "Unexpected RDMA CM event (%d)\n", ev->event);
1397 nvme_rdma_error_recovery(queue->ctrl);
1402 queue->cm_error = cm_error;
1403 complete(&queue->cm_done);
1409 static enum blk_eh_timer_return
1410 nvme_rdma_timeout(struct request *rq, bool reserved)
1412 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1414 /* queue error recovery */
1415 nvme_rdma_error_recovery(req->queue->ctrl);
1417 /* fail with DNR on cmd timeout */
1418 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1420 return BLK_EH_HANDLED;
1424 * We cannot accept any other command until the Connect command has completed.
1426 static inline bool nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue,
1429 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
1430 struct nvme_command *cmd = nvme_req(rq)->cmd;
1432 if (!blk_rq_is_passthrough(rq) ||
1433 cmd->common.opcode != nvme_fabrics_command ||
1434 cmd->fabrics.fctype != nvme_fabrics_type_connect)
1441 static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1442 const struct blk_mq_queue_data *bd)
1444 struct nvme_ns *ns = hctx->queue->queuedata;
1445 struct nvme_rdma_queue *queue = hctx->driver_data;
1446 struct request *rq = bd->rq;
1447 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1448 struct nvme_rdma_qe *sqe = &req->sqe;
1449 struct nvme_command *c = sqe->data;
1451 struct ib_device *dev;
1454 WARN_ON_ONCE(rq->tag < 0);
1456 if (!nvme_rdma_queue_is_ready(queue, rq))
1457 return BLK_MQ_RQ_QUEUE_BUSY;
1459 dev = queue->device->dev;
1460 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1461 sizeof(struct nvme_command), DMA_TO_DEVICE);
1463 ret = nvme_setup_cmd(ns, rq, c);
1464 if (ret != BLK_MQ_RQ_QUEUE_OK)
1467 blk_mq_start_request(rq);
1469 ret = nvme_rdma_map_data(queue, rq, c);
1471 dev_err(queue->ctrl->ctrl.device,
1472 "Failed to map data (%d)\n", ret);
1473 nvme_cleanup_cmd(rq);
1477 ib_dma_sync_single_for_device(dev, sqe->dma,
1478 sizeof(struct nvme_command), DMA_TO_DEVICE);
1480 if (req_op(rq) == REQ_OP_FLUSH)
1482 ret = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1483 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
1485 nvme_rdma_unmap_data(queue, rq);
1489 return BLK_MQ_RQ_QUEUE_OK;
1491 return (ret == -ENOMEM || ret == -EAGAIN) ?
1492 BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR;
1495 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1497 struct nvme_rdma_queue *queue = hctx->driver_data;
1498 struct ib_cq *cq = queue->ib_cq;
1502 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1503 while (ib_poll_cq(cq, 1, &wc) > 0) {
1504 struct ib_cqe *cqe = wc.wr_cqe;
1507 if (cqe->done == nvme_rdma_recv_done)
1508 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1517 static void nvme_rdma_complete_rq(struct request *rq)
1519 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1521 nvme_rdma_unmap_data(req->queue, rq);
1522 nvme_complete_rq(rq);
1525 static const struct blk_mq_ops nvme_rdma_mq_ops = {
1526 .queue_rq = nvme_rdma_queue_rq,
1527 .complete = nvme_rdma_complete_rq,
1528 .init_request = nvme_rdma_init_request,
1529 .exit_request = nvme_rdma_exit_request,
1530 .reinit_request = nvme_rdma_reinit_request,
1531 .init_hctx = nvme_rdma_init_hctx,
1532 .poll = nvme_rdma_poll,
1533 .timeout = nvme_rdma_timeout,
1536 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1537 .queue_rq = nvme_rdma_queue_rq,
1538 .complete = nvme_rdma_complete_rq,
1539 .init_request = nvme_rdma_init_admin_request,
1540 .exit_request = nvme_rdma_exit_admin_request,
1541 .reinit_request = nvme_rdma_reinit_request,
1542 .init_hctx = nvme_rdma_init_admin_hctx,
1543 .timeout = nvme_rdma_timeout,
1546 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
1550 error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
1554 ctrl->device = ctrl->queues[0].device;
1557 * We need a reference on the device as long as the tag_set is alive,
1558 * as the MRs in the request structures need a valid ib_device.
1561 if (!nvme_rdma_dev_get(ctrl->device))
1562 goto out_free_queue;
1564 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
1565 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
1567 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1568 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
1569 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
1570 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
1571 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1572 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1573 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1574 ctrl->admin_tag_set.driver_data = ctrl;
1575 ctrl->admin_tag_set.nr_hw_queues = 1;
1576 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1578 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1582 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1583 if (IS_ERR(ctrl->ctrl.admin_q)) {
1584 error = PTR_ERR(ctrl->ctrl.admin_q);
1585 goto out_free_tagset;
1588 error = nvmf_connect_admin_queue(&ctrl->ctrl);
1590 goto out_cleanup_queue;
1592 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
1594 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
1596 dev_err(ctrl->ctrl.device,
1597 "prop_get NVME_REG_CAP failed\n");
1598 goto out_cleanup_queue;
1602 min_t(int, NVME_CAP_MQES(ctrl->cap), ctrl->ctrl.sqsize);
1604 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
1606 goto out_cleanup_queue;
1608 ctrl->ctrl.max_hw_sectors =
1609 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
1611 error = nvme_init_identify(&ctrl->ctrl);
1613 goto out_cleanup_queue;
1615 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
1616 &ctrl->async_event_sqe, sizeof(struct nvme_command),
1619 goto out_cleanup_queue;
1621 nvme_start_keep_alive(&ctrl->ctrl);
1626 blk_cleanup_queue(ctrl->ctrl.admin_q);
1628 /* disconnect and drain the queue before freeing the tagset */
1629 nvme_rdma_stop_queue(&ctrl->queues[0]);
1630 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1632 nvme_rdma_dev_put(ctrl->device);
1634 nvme_rdma_free_queue(&ctrl->queues[0]);
1638 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1640 nvme_stop_keep_alive(&ctrl->ctrl);
1641 cancel_work_sync(&ctrl->err_work);
1642 cancel_delayed_work_sync(&ctrl->reconnect_work);
1644 if (ctrl->queue_count > 1) {
1645 nvme_stop_queues(&ctrl->ctrl);
1646 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1647 nvme_cancel_request, &ctrl->ctrl);
1648 nvme_rdma_free_io_queues(ctrl);
1651 if (test_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[0].flags))
1652 nvme_shutdown_ctrl(&ctrl->ctrl);
1654 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
1655 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1656 nvme_cancel_request, &ctrl->ctrl);
1657 nvme_rdma_destroy_admin_queue(ctrl);
1660 static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1662 nvme_uninit_ctrl(&ctrl->ctrl);
1664 nvme_rdma_shutdown_ctrl(ctrl);
1666 if (ctrl->ctrl.tagset) {
1667 blk_cleanup_queue(ctrl->ctrl.connect_q);
1668 blk_mq_free_tag_set(&ctrl->tag_set);
1669 nvme_rdma_dev_put(ctrl->device);
1672 nvme_put_ctrl(&ctrl->ctrl);
1675 static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1677 struct nvme_rdma_ctrl *ctrl = container_of(work,
1678 struct nvme_rdma_ctrl, delete_work);
1680 __nvme_rdma_remove_ctrl(ctrl, true);
1683 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1685 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1688 if (!queue_work(nvme_rdma_wq, &ctrl->delete_work))
1694 static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1696 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1700 * Keep a reference until all work is flushed since
1701 * __nvme_rdma_del_ctrl can free the ctrl mem
1703 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1705 ret = __nvme_rdma_del_ctrl(ctrl);
1707 flush_work(&ctrl->delete_work);
1708 nvme_put_ctrl(&ctrl->ctrl);
1712 static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1714 struct nvme_rdma_ctrl *ctrl = container_of(work,
1715 struct nvme_rdma_ctrl, delete_work);
1717 __nvme_rdma_remove_ctrl(ctrl, false);
1720 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1722 struct nvme_rdma_ctrl *ctrl = container_of(work,
1723 struct nvme_rdma_ctrl, reset_work);
1727 nvme_rdma_shutdown_ctrl(ctrl);
1729 ret = nvme_rdma_configure_admin_queue(ctrl);
1731 /* ctrl is already shutdown, just remove the ctrl */
1732 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1736 if (ctrl->queue_count > 1) {
1737 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
1741 ret = nvme_rdma_init_io_queues(ctrl);
1745 ret = nvme_rdma_connect_io_queues(ctrl);
1750 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1751 WARN_ON_ONCE(!changed);
1753 if (ctrl->queue_count > 1) {
1754 nvme_start_queues(&ctrl->ctrl);
1755 nvme_queue_scan(&ctrl->ctrl);
1756 nvme_queue_async_events(&ctrl->ctrl);
1762 /* Deleting this dead controller... */
1763 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1764 WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work));
1767 static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl)
1769 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1771 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1774 if (!queue_work(nvme_rdma_wq, &ctrl->reset_work))
1777 flush_work(&ctrl->reset_work);
1782 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1784 .module = THIS_MODULE,
1786 .reg_read32 = nvmf_reg_read32,
1787 .reg_read64 = nvmf_reg_read64,
1788 .reg_write32 = nvmf_reg_write32,
1789 .reset_ctrl = nvme_rdma_reset_ctrl,
1790 .free_ctrl = nvme_rdma_free_ctrl,
1791 .submit_async_event = nvme_rdma_submit_async_event,
1792 .delete_ctrl = nvme_rdma_del_ctrl,
1793 .get_subsysnqn = nvmf_get_subsysnqn,
1794 .get_address = nvmf_get_address,
1797 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1801 ret = nvme_rdma_init_io_queues(ctrl);
1806 * We need a reference on the device as long as the tag_set is alive,
1807 * as the MRs in the request structures need a valid ib_device.
1810 if (!nvme_rdma_dev_get(ctrl->device))
1811 goto out_free_io_queues;
1813 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1814 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
1815 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
1816 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1817 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1818 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1819 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1820 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1821 ctrl->tag_set.driver_data = ctrl;
1822 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
1823 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1825 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1828 ctrl->ctrl.tagset = &ctrl->tag_set;
1830 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1831 if (IS_ERR(ctrl->ctrl.connect_q)) {
1832 ret = PTR_ERR(ctrl->ctrl.connect_q);
1833 goto out_free_tag_set;
1836 ret = nvme_rdma_connect_io_queues(ctrl);
1838 goto out_cleanup_connect_q;
1842 out_cleanup_connect_q:
1843 blk_cleanup_queue(ctrl->ctrl.connect_q);
1845 blk_mq_free_tag_set(&ctrl->tag_set);
1847 nvme_rdma_dev_put(ctrl->device);
1849 nvme_rdma_free_io_queues(ctrl);
1853 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1854 struct nvmf_ctrl_options *opts)
1856 struct nvme_rdma_ctrl *ctrl;
1861 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1863 return ERR_PTR(-ENOMEM);
1864 ctrl->ctrl.opts = opts;
1865 INIT_LIST_HEAD(&ctrl->list);
1867 if (opts->mask & NVMF_OPT_TRSVCID)
1868 port = opts->trsvcid;
1870 port = __stringify(NVME_RDMA_IP_PORT);
1872 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1873 opts->traddr, port, &ctrl->addr);
1875 pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
1879 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
1880 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1881 opts->host_traddr, NULL, &ctrl->src_addr);
1883 pr_err("malformed src address passed: %s\n",
1889 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1890 0 /* no quirks, we're perfect! */);
1894 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1895 nvme_rdma_reconnect_ctrl_work);
1896 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1897 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1898 INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work);
1899 spin_lock_init(&ctrl->lock);
1901 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1902 ctrl->ctrl.sqsize = opts->queue_size - 1;
1903 ctrl->ctrl.kato = opts->kato;
1906 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues),
1909 goto out_uninit_ctrl;
1911 ret = nvme_rdma_configure_admin_queue(ctrl);
1913 goto out_kfree_queues;
1915 /* sanity check icdoff */
1916 if (ctrl->ctrl.icdoff) {
1917 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1918 goto out_remove_admin_queue;
1921 /* sanity check keyed sgls */
1922 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1923 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1924 goto out_remove_admin_queue;
1927 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1928 /* warn if maxcmd is lower than queue_size */
1929 dev_warn(ctrl->ctrl.device,
1930 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1931 opts->queue_size, ctrl->ctrl.maxcmd);
1932 opts->queue_size = ctrl->ctrl.maxcmd;
1935 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1936 /* warn if sqsize is lower than queue_size */
1937 dev_warn(ctrl->ctrl.device,
1938 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1939 opts->queue_size, ctrl->ctrl.sqsize + 1);
1940 opts->queue_size = ctrl->ctrl.sqsize + 1;
1943 if (opts->nr_io_queues) {
1944 ret = nvme_rdma_create_io_queues(ctrl);
1946 goto out_remove_admin_queue;
1949 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1950 WARN_ON_ONCE(!changed);
1952 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
1953 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1955 kref_get(&ctrl->ctrl.kref);
1957 mutex_lock(&nvme_rdma_ctrl_mutex);
1958 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1959 mutex_unlock(&nvme_rdma_ctrl_mutex);
1961 if (opts->nr_io_queues) {
1962 nvme_queue_scan(&ctrl->ctrl);
1963 nvme_queue_async_events(&ctrl->ctrl);
1968 out_remove_admin_queue:
1969 nvme_stop_keep_alive(&ctrl->ctrl);
1970 nvme_rdma_destroy_admin_queue(ctrl);
1972 kfree(ctrl->queues);
1974 nvme_uninit_ctrl(&ctrl->ctrl);
1975 nvme_put_ctrl(&ctrl->ctrl);
1978 return ERR_PTR(ret);
1981 return ERR_PTR(ret);
1984 static struct nvmf_transport_ops nvme_rdma_transport = {
1986 .required_opts = NVMF_OPT_TRADDR,
1987 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
1988 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
1989 .create_ctrl = nvme_rdma_create_ctrl,
1992 static void nvme_rdma_add_one(struct ib_device *ib_device)
1996 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
1998 struct nvme_rdma_ctrl *ctrl;
2000 /* Delete all controllers using this device */
2001 mutex_lock(&nvme_rdma_ctrl_mutex);
2002 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2003 if (ctrl->device->dev != ib_device)
2005 dev_info(ctrl->ctrl.device,
2006 "Removing ctrl: NQN \"%s\", addr %pISp\n",
2007 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2008 __nvme_rdma_del_ctrl(ctrl);
2010 mutex_unlock(&nvme_rdma_ctrl_mutex);
2012 flush_workqueue(nvme_rdma_wq);
2015 static struct ib_client nvme_rdma_ib_client = {
2016 .name = "nvme_rdma",
2017 .add = nvme_rdma_add_one,
2018 .remove = nvme_rdma_remove_one
2021 static int __init nvme_rdma_init_module(void)
2025 nvme_rdma_wq = create_workqueue("nvme_rdma_wq");
2029 ret = ib_register_client(&nvme_rdma_ib_client);
2031 goto err_destroy_wq;
2033 ret = nvmf_register_transport(&nvme_rdma_transport);
2035 goto err_unreg_client;
2040 ib_unregister_client(&nvme_rdma_ib_client);
2042 destroy_workqueue(nvme_rdma_wq);
2046 static void __exit nvme_rdma_cleanup_module(void)
2048 nvmf_unregister_transport(&nvme_rdma_transport);
2049 ib_unregister_client(&nvme_rdma_ib_client);
2050 destroy_workqueue(nvme_rdma_wq);
2053 module_init(nvme_rdma_init_module);
2054 module_exit(nvme_rdma_cleanup_module);
2056 MODULE_LICENSE("GPL v2");