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 1000 /* 1 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;
122 struct delayed_work reconnect_work;
124 struct list_head list;
126 struct blk_mq_tag_set admin_tag_set;
127 struct nvme_rdma_device *device;
133 struct sockaddr addr;
134 struct sockaddr_in addr_in;
137 struct sockaddr src_addr;
138 struct sockaddr_in src_addr_in;
141 struct nvme_ctrl ctrl;
144 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
146 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
149 static LIST_HEAD(device_list);
150 static DEFINE_MUTEX(device_list_mutex);
152 static LIST_HEAD(nvme_rdma_ctrl_list);
153 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
155 static struct workqueue_struct *nvme_rdma_wq;
158 * Disabling this option makes small I/O goes faster, but is fundamentally
159 * unsafe. With it turned off we will have to register a global rkey that
160 * allows read and write access to all physical memory.
162 static bool register_always = true;
163 module_param(register_always, bool, 0444);
164 MODULE_PARM_DESC(register_always,
165 "Use memory registration even for contiguous memory regions");
167 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
168 struct rdma_cm_event *event);
169 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
171 /* XXX: really should move to a generic header sooner or later.. */
172 static inline void put_unaligned_le24(u32 val, u8 *p)
179 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
181 return queue - queue->ctrl->queues;
184 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
186 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
189 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
190 size_t capsule_size, enum dma_data_direction dir)
192 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
196 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
197 size_t capsule_size, enum dma_data_direction dir)
199 qe->data = kzalloc(capsule_size, GFP_KERNEL);
203 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
204 if (ib_dma_mapping_error(ibdev, qe->dma)) {
212 static void nvme_rdma_free_ring(struct ib_device *ibdev,
213 struct nvme_rdma_qe *ring, size_t ib_queue_size,
214 size_t capsule_size, enum dma_data_direction dir)
218 for (i = 0; i < ib_queue_size; i++)
219 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
223 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
224 size_t ib_queue_size, size_t capsule_size,
225 enum dma_data_direction dir)
227 struct nvme_rdma_qe *ring;
230 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
234 for (i = 0; i < ib_queue_size; i++) {
235 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
242 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
246 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
248 pr_debug("QP event %s (%d)\n",
249 ib_event_msg(event->event), event->event);
253 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
255 wait_for_completion_interruptible_timeout(&queue->cm_done,
256 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
257 return queue->cm_error;
260 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
262 struct nvme_rdma_device *dev = queue->device;
263 struct ib_qp_init_attr init_attr;
266 memset(&init_attr, 0, sizeof(init_attr));
267 init_attr.event_handler = nvme_rdma_qp_event;
269 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
271 init_attr.cap.max_recv_wr = queue->queue_size + 1;
272 init_attr.cap.max_recv_sge = 1;
273 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
274 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
275 init_attr.qp_type = IB_QPT_RC;
276 init_attr.send_cq = queue->ib_cq;
277 init_attr.recv_cq = queue->ib_cq;
279 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
281 queue->qp = queue->cm_id->qp;
285 static int nvme_rdma_reinit_request(void *data, struct request *rq)
287 struct nvme_rdma_ctrl *ctrl = data;
288 struct nvme_rdma_device *dev = ctrl->device;
289 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
292 if (!req->mr->need_inval)
295 ib_dereg_mr(req->mr);
297 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
299 if (IS_ERR(req->mr)) {
300 ret = PTR_ERR(req->mr);
305 req->mr->need_inval = false;
311 static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl *ctrl,
312 struct request *rq, unsigned int queue_idx)
314 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
315 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
316 struct nvme_rdma_device *dev = queue->device;
319 ib_dereg_mr(req->mr);
321 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
325 static void nvme_rdma_exit_request(void *data, struct request *rq,
326 unsigned int hctx_idx, unsigned int rq_idx)
328 return __nvme_rdma_exit_request(data, rq, hctx_idx + 1);
331 static void nvme_rdma_exit_admin_request(void *data, struct request *rq,
332 unsigned int hctx_idx, unsigned int rq_idx)
334 return __nvme_rdma_exit_request(data, rq, 0);
337 static int __nvme_rdma_init_request(struct nvme_rdma_ctrl *ctrl,
338 struct request *rq, unsigned int queue_idx)
340 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
341 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
342 struct nvme_rdma_device *dev = queue->device;
343 struct ib_device *ibdev = dev->dev;
346 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
351 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
353 if (IS_ERR(req->mr)) {
354 ret = PTR_ERR(req->mr);
363 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
368 static int nvme_rdma_init_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, hctx_idx + 1);
375 static int nvme_rdma_init_admin_request(void *data, struct request *rq,
376 unsigned int hctx_idx, unsigned int rq_idx,
377 unsigned int numa_node)
379 return __nvme_rdma_init_request(data, rq, 0);
382 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
383 unsigned int hctx_idx)
385 struct nvme_rdma_ctrl *ctrl = data;
386 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
388 BUG_ON(hctx_idx >= ctrl->queue_count);
390 hctx->driver_data = queue;
394 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
395 unsigned int hctx_idx)
397 struct nvme_rdma_ctrl *ctrl = data;
398 struct nvme_rdma_queue *queue = &ctrl->queues[0];
400 BUG_ON(hctx_idx != 0);
402 hctx->driver_data = queue;
406 static void nvme_rdma_free_dev(struct kref *ref)
408 struct nvme_rdma_device *ndev =
409 container_of(ref, struct nvme_rdma_device, ref);
411 mutex_lock(&device_list_mutex);
412 list_del(&ndev->entry);
413 mutex_unlock(&device_list_mutex);
415 ib_dealloc_pd(ndev->pd);
419 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
421 kref_put(&dev->ref, nvme_rdma_free_dev);
424 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
426 return kref_get_unless_zero(&dev->ref);
429 static struct nvme_rdma_device *
430 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
432 struct nvme_rdma_device *ndev;
434 mutex_lock(&device_list_mutex);
435 list_for_each_entry(ndev, &device_list, entry) {
436 if (ndev->dev->node_guid == cm_id->device->node_guid &&
437 nvme_rdma_dev_get(ndev))
441 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
445 ndev->dev = cm_id->device;
446 kref_init(&ndev->ref);
448 ndev->pd = ib_alloc_pd(ndev->dev,
449 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
450 if (IS_ERR(ndev->pd))
453 if (!(ndev->dev->attrs.device_cap_flags &
454 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
455 dev_err(&ndev->dev->dev,
456 "Memory registrations not supported.\n");
460 list_add(&ndev->entry, &device_list);
462 mutex_unlock(&device_list_mutex);
466 ib_dealloc_pd(ndev->pd);
470 mutex_unlock(&device_list_mutex);
474 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
476 struct nvme_rdma_device *dev;
477 struct ib_device *ibdev;
479 if (!test_and_clear_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags))
484 rdma_destroy_qp(queue->cm_id);
485 ib_free_cq(queue->ib_cq);
487 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
488 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
490 nvme_rdma_dev_put(dev);
493 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue,
494 struct nvme_rdma_device *dev)
496 struct ib_device *ibdev = dev->dev;
497 const int send_wr_factor = 3; /* MR, SEND, INV */
498 const int cq_factor = send_wr_factor + 1; /* + RECV */
499 int comp_vector, idx = nvme_rdma_queue_idx(queue);
506 * The admin queue is barely used once the controller is live, so don't
507 * bother to spread it out.
512 comp_vector = idx % ibdev->num_comp_vectors;
515 /* +1 for ib_stop_cq */
516 queue->ib_cq = ib_alloc_cq(dev->dev, queue,
517 cq_factor * queue->queue_size + 1, comp_vector,
519 if (IS_ERR(queue->ib_cq)) {
520 ret = PTR_ERR(queue->ib_cq);
524 ret = nvme_rdma_create_qp(queue, send_wr_factor);
526 goto out_destroy_ib_cq;
528 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
529 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
530 if (!queue->rsp_ring) {
534 set_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags);
539 ib_destroy_qp(queue->qp);
541 ib_free_cq(queue->ib_cq);
546 static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl,
547 int idx, size_t queue_size)
549 struct nvme_rdma_queue *queue;
550 struct sockaddr *src_addr = NULL;
553 queue = &ctrl->queues[idx];
555 init_completion(&queue->cm_done);
558 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
560 queue->cmnd_capsule_len = sizeof(struct nvme_command);
562 queue->queue_size = queue_size;
564 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
565 RDMA_PS_TCP, IB_QPT_RC);
566 if (IS_ERR(queue->cm_id)) {
567 dev_info(ctrl->ctrl.device,
568 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
569 return PTR_ERR(queue->cm_id);
572 queue->cm_error = -ETIMEDOUT;
573 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
574 src_addr = &ctrl->src_addr;
576 ret = rdma_resolve_addr(queue->cm_id, src_addr, &ctrl->addr,
577 NVME_RDMA_CONNECT_TIMEOUT_MS);
579 dev_info(ctrl->ctrl.device,
580 "rdma_resolve_addr failed (%d).\n", ret);
581 goto out_destroy_cm_id;
584 ret = nvme_rdma_wait_for_cm(queue);
586 dev_info(ctrl->ctrl.device,
587 "rdma_resolve_addr wait failed (%d).\n", ret);
588 goto out_destroy_cm_id;
591 clear_bit(NVME_RDMA_Q_DELETING, &queue->flags);
592 set_bit(NVME_RDMA_Q_CONNECTED, &queue->flags);
597 nvme_rdma_destroy_queue_ib(queue);
598 rdma_destroy_id(queue->cm_id);
602 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
604 rdma_disconnect(queue->cm_id);
605 ib_drain_qp(queue->qp);
608 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
610 nvme_rdma_destroy_queue_ib(queue);
611 rdma_destroy_id(queue->cm_id);
614 static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue)
616 if (test_and_set_bit(NVME_RDMA_Q_DELETING, &queue->flags))
618 nvme_rdma_stop_queue(queue);
619 nvme_rdma_free_queue(queue);
622 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
626 for (i = 1; i < ctrl->queue_count; i++)
627 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
630 static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl)
634 for (i = 1; i < ctrl->queue_count; i++) {
635 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
637 dev_info(ctrl->ctrl.device,
638 "failed to connect i/o queue: %d\n", ret);
639 goto out_free_queues;
641 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
647 nvme_rdma_free_io_queues(ctrl);
651 static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl)
653 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
654 unsigned int nr_io_queues;
657 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
658 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
662 ctrl->queue_count = nr_io_queues + 1;
663 if (ctrl->queue_count < 2)
666 dev_info(ctrl->ctrl.device,
667 "creating %d I/O queues.\n", nr_io_queues);
669 for (i = 1; i < ctrl->queue_count; i++) {
670 ret = nvme_rdma_init_queue(ctrl, i,
671 ctrl->ctrl.opts->queue_size);
673 dev_info(ctrl->ctrl.device,
674 "failed to initialize i/o queue: %d\n", ret);
675 goto out_free_queues;
682 for (i--; i >= 1; i--)
683 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
688 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl)
690 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
691 sizeof(struct nvme_command), DMA_TO_DEVICE);
692 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
693 blk_cleanup_queue(ctrl->ctrl.admin_q);
694 blk_mq_free_tag_set(&ctrl->admin_tag_set);
695 nvme_rdma_dev_put(ctrl->device);
698 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
700 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
702 if (list_empty(&ctrl->list))
705 mutex_lock(&nvme_rdma_ctrl_mutex);
706 list_del(&ctrl->list);
707 mutex_unlock(&nvme_rdma_ctrl_mutex);
710 nvmf_free_options(nctrl->opts);
715 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
717 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
718 struct nvme_rdma_ctrl, reconnect_work);
722 if (ctrl->queue_count > 1) {
723 nvme_rdma_free_io_queues(ctrl);
725 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
730 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
732 ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set);
736 ret = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
740 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
742 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
746 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
748 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
752 nvme_start_keep_alive(&ctrl->ctrl);
754 if (ctrl->queue_count > 1) {
755 ret = nvme_rdma_init_io_queues(ctrl);
759 ret = nvme_rdma_connect_io_queues(ctrl);
764 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
765 WARN_ON_ONCE(!changed);
767 if (ctrl->queue_count > 1) {
768 nvme_start_queues(&ctrl->ctrl);
769 nvme_queue_scan(&ctrl->ctrl);
770 nvme_queue_async_events(&ctrl->ctrl);
773 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
778 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
780 /* Make sure we are not resetting/deleting */
781 if (ctrl->ctrl.state == NVME_CTRL_RECONNECTING) {
782 dev_info(ctrl->ctrl.device,
783 "Failed reconnect attempt, requeueing...\n");
784 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
785 ctrl->reconnect_delay * HZ);
789 static void nvme_rdma_error_recovery_work(struct work_struct *work)
791 struct nvme_rdma_ctrl *ctrl = container_of(work,
792 struct nvme_rdma_ctrl, err_work);
795 nvme_stop_keep_alive(&ctrl->ctrl);
797 for (i = 0; i < ctrl->queue_count; i++) {
798 clear_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[i].flags);
799 clear_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
802 if (ctrl->queue_count > 1)
803 nvme_stop_queues(&ctrl->ctrl);
804 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
806 /* We must take care of fastfail/requeue all our inflight requests */
807 if (ctrl->queue_count > 1)
808 blk_mq_tagset_busy_iter(&ctrl->tag_set,
809 nvme_cancel_request, &ctrl->ctrl);
810 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
811 nvme_cancel_request, &ctrl->ctrl);
813 dev_info(ctrl->ctrl.device, "reconnecting in %d seconds\n",
814 ctrl->reconnect_delay);
816 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
817 ctrl->reconnect_delay * HZ);
820 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
822 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
825 queue_work(nvme_rdma_wq, &ctrl->err_work);
828 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
831 struct nvme_rdma_queue *queue = cq->cq_context;
832 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
834 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
835 dev_info(ctrl->ctrl.device,
836 "%s for CQE 0x%p failed with status %s (%d)\n",
838 ib_wc_status_msg(wc->status), wc->status);
839 nvme_rdma_error_recovery(ctrl);
842 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
844 if (unlikely(wc->status != IB_WC_SUCCESS))
845 nvme_rdma_wr_error(cq, wc, "MEMREG");
848 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
850 if (unlikely(wc->status != IB_WC_SUCCESS))
851 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
854 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
855 struct nvme_rdma_request *req)
857 struct ib_send_wr *bad_wr;
858 struct ib_send_wr wr = {
859 .opcode = IB_WR_LOCAL_INV,
863 .ex.invalidate_rkey = req->mr->rkey,
866 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
867 wr.wr_cqe = &req->reg_cqe;
869 return ib_post_send(queue->qp, &wr, &bad_wr);
872 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
875 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
876 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
877 struct nvme_rdma_device *dev = queue->device;
878 struct ib_device *ibdev = dev->dev;
881 if (!blk_rq_bytes(rq))
884 if (req->mr->need_inval) {
885 res = nvme_rdma_inv_rkey(queue, req);
887 dev_err(ctrl->ctrl.device,
888 "Queueing INV WR for rkey %#x failed (%d)\n",
890 nvme_rdma_error_recovery(queue->ctrl);
894 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
895 req->nents, rq_data_dir(rq) ==
896 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
898 nvme_cleanup_cmd(rq);
899 sg_free_table_chained(&req->sg_table, true);
902 static int nvme_rdma_set_sg_null(struct nvme_command *c)
904 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
907 put_unaligned_le24(0, sg->length);
908 put_unaligned_le32(0, sg->key);
909 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
913 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
914 struct nvme_rdma_request *req, struct nvme_command *c)
916 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
918 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
919 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
920 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
922 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
923 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
924 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
926 req->inline_data = true;
931 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
932 struct nvme_rdma_request *req, struct nvme_command *c)
934 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
936 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
937 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
938 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
939 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
943 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
944 struct nvme_rdma_request *req, struct nvme_command *c,
947 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
950 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE);
957 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
959 req->reg_cqe.done = nvme_rdma_memreg_done;
960 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
961 req->reg_wr.wr.opcode = IB_WR_REG_MR;
962 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
963 req->reg_wr.wr.num_sge = 0;
964 req->reg_wr.mr = req->mr;
965 req->reg_wr.key = req->mr->rkey;
966 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
967 IB_ACCESS_REMOTE_READ |
968 IB_ACCESS_REMOTE_WRITE;
970 req->mr->need_inval = true;
972 sg->addr = cpu_to_le64(req->mr->iova);
973 put_unaligned_le24(req->mr->length, sg->length);
974 put_unaligned_le32(req->mr->rkey, sg->key);
975 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
976 NVME_SGL_FMT_INVALIDATE;
981 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
982 struct request *rq, struct nvme_command *c)
984 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
985 struct nvme_rdma_device *dev = queue->device;
986 struct ib_device *ibdev = dev->dev;
990 req->inline_data = false;
991 req->mr->need_inval = false;
993 c->common.flags |= NVME_CMD_SGL_METABUF;
995 if (!blk_rq_bytes(rq))
996 return nvme_rdma_set_sg_null(c);
998 req->sg_table.sgl = req->first_sgl;
999 ret = sg_alloc_table_chained(&req->sg_table,
1000 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
1004 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
1006 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
1007 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1008 if (unlikely(count <= 0)) {
1009 sg_free_table_chained(&req->sg_table, true);
1014 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1015 blk_rq_payload_bytes(rq) <=
1016 nvme_rdma_inline_data_size(queue))
1017 return nvme_rdma_map_sg_inline(queue, req, c);
1019 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
1020 return nvme_rdma_map_sg_single(queue, req, c);
1023 return nvme_rdma_map_sg_fr(queue, req, c, count);
1026 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1028 if (unlikely(wc->status != IB_WC_SUCCESS))
1029 nvme_rdma_wr_error(cq, wc, "SEND");
1032 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1033 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1034 struct ib_send_wr *first, bool flush)
1036 struct ib_send_wr wr, *bad_wr;
1039 sge->addr = qe->dma;
1040 sge->length = sizeof(struct nvme_command),
1041 sge->lkey = queue->device->pd->local_dma_lkey;
1043 qe->cqe.done = nvme_rdma_send_done;
1046 wr.wr_cqe = &qe->cqe;
1048 wr.num_sge = num_sge;
1049 wr.opcode = IB_WR_SEND;
1053 * Unsignalled send completions are another giant desaster in the
1054 * IB Verbs spec: If we don't regularly post signalled sends
1055 * the send queue will fill up and only a QP reset will rescue us.
1056 * Would have been way to obvious to handle this in hardware or
1057 * at least the RDMA stack..
1059 * This messy and racy code sniplet is copy and pasted from the iSER
1060 * initiator, and the magic '32' comes from there as well.
1062 * Always signal the flushes. The magic request used for the flush
1063 * sequencer is not allocated in our driver's tagset and it's
1064 * triggered to be freed by blk_cleanup_queue(). So we need to
1065 * always mark it as signaled to ensure that the "wr_cqe", which is
1066 * embedded in request's payload, is not freed when __ib_process_cq()
1067 * calls wr_cqe->done().
1069 if ((++queue->sig_count % 32) == 0 || flush)
1070 wr.send_flags |= IB_SEND_SIGNALED;
1077 ret = ib_post_send(queue->qp, first, &bad_wr);
1079 dev_err(queue->ctrl->ctrl.device,
1080 "%s failed with error code %d\n", __func__, ret);
1085 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1086 struct nvme_rdma_qe *qe)
1088 struct ib_recv_wr wr, *bad_wr;
1092 list.addr = qe->dma;
1093 list.length = sizeof(struct nvme_completion);
1094 list.lkey = queue->device->pd->local_dma_lkey;
1096 qe->cqe.done = nvme_rdma_recv_done;
1099 wr.wr_cqe = &qe->cqe;
1103 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1105 dev_err(queue->ctrl->ctrl.device,
1106 "%s failed with error code %d\n", __func__, ret);
1111 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1113 u32 queue_idx = nvme_rdma_queue_idx(queue);
1116 return queue->ctrl->admin_tag_set.tags[queue_idx];
1117 return queue->ctrl->tag_set.tags[queue_idx - 1];
1120 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1122 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1123 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1124 struct ib_device *dev = queue->device->dev;
1125 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1126 struct nvme_command *cmd = sqe->data;
1130 if (WARN_ON_ONCE(aer_idx != 0))
1133 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1135 memset(cmd, 0, sizeof(*cmd));
1136 cmd->common.opcode = nvme_admin_async_event;
1137 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1138 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1139 nvme_rdma_set_sg_null(cmd);
1141 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1144 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1148 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1149 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1152 struct nvme_rdma_request *req;
1155 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1157 dev_err(queue->ctrl->ctrl.device,
1158 "tag 0x%x on QP %#x not found\n",
1159 cqe->command_id, queue->qp->qp_num);
1160 nvme_rdma_error_recovery(queue->ctrl);
1163 req = blk_mq_rq_to_pdu(rq);
1168 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1169 wc->ex.invalidate_rkey == req->mr->rkey)
1170 req->mr->need_inval = false;
1172 req->req.result = cqe->result;
1173 blk_mq_complete_request(rq, le16_to_cpu(cqe->status) >> 1);
1177 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1179 struct nvme_rdma_qe *qe =
1180 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1181 struct nvme_rdma_queue *queue = cq->cq_context;
1182 struct ib_device *ibdev = queue->device->dev;
1183 struct nvme_completion *cqe = qe->data;
1184 const size_t len = sizeof(struct nvme_completion);
1187 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1188 nvme_rdma_wr_error(cq, wc, "RECV");
1192 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1194 * AEN requests are special as they don't time out and can
1195 * survive any kind of queue freeze and often don't respond to
1196 * aborts. We don't even bother to allocate a struct request
1197 * for them but rather special case them here.
1199 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1200 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
1201 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1204 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1205 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1207 nvme_rdma_post_recv(queue, qe);
1211 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1213 __nvme_rdma_recv_done(cq, wc, -1);
1216 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1220 for (i = 0; i < queue->queue_size; i++) {
1221 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1223 goto out_destroy_queue_ib;
1228 out_destroy_queue_ib:
1229 nvme_rdma_destroy_queue_ib(queue);
1233 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1234 struct rdma_cm_event *ev)
1236 struct rdma_cm_id *cm_id = queue->cm_id;
1237 int status = ev->status;
1238 const char *rej_msg;
1239 const struct nvme_rdma_cm_rej *rej_data;
1242 rej_msg = rdma_reject_msg(cm_id, status);
1243 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1245 if (rej_data && rej_data_len >= sizeof(u16)) {
1246 u16 sts = le16_to_cpu(rej_data->sts);
1248 dev_err(queue->ctrl->ctrl.device,
1249 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1250 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1252 dev_err(queue->ctrl->ctrl.device,
1253 "Connect rejected: status %d (%s).\n", status, rej_msg);
1259 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1261 struct nvme_rdma_device *dev;
1264 dev = nvme_rdma_find_get_device(queue->cm_id);
1266 dev_err(queue->cm_id->device->dev.parent,
1267 "no client data found!\n");
1268 return -ECONNREFUSED;
1271 ret = nvme_rdma_create_queue_ib(queue, dev);
1273 nvme_rdma_dev_put(dev);
1277 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1279 dev_err(queue->ctrl->ctrl.device,
1280 "rdma_resolve_route failed (%d).\n",
1282 goto out_destroy_queue;
1288 nvme_rdma_destroy_queue_ib(queue);
1293 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1295 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1296 struct rdma_conn_param param = { };
1297 struct nvme_rdma_cm_req priv = { };
1300 param.qp_num = queue->qp->qp_num;
1301 param.flow_control = 1;
1303 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1304 /* maximum retry count */
1305 param.retry_count = 7;
1306 param.rnr_retry_count = 7;
1307 param.private_data = &priv;
1308 param.private_data_len = sizeof(priv);
1310 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1311 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1313 * set the admin queue depth to the minimum size
1314 * specified by the Fabrics standard.
1316 if (priv.qid == 0) {
1317 priv.hrqsize = cpu_to_le16(NVMF_AQ_DEPTH);
1318 priv.hsqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1);
1321 * current interpretation of the fabrics spec
1322 * is at minimum you make hrqsize sqsize+1, or a
1323 * 1's based representation of sqsize.
1325 priv.hrqsize = cpu_to_le16(queue->queue_size);
1326 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1329 ret = rdma_connect(queue->cm_id, ¶m);
1331 dev_err(ctrl->ctrl.device,
1332 "rdma_connect failed (%d).\n", ret);
1333 goto out_destroy_queue_ib;
1338 out_destroy_queue_ib:
1339 nvme_rdma_destroy_queue_ib(queue);
1343 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1344 struct rdma_cm_event *ev)
1346 struct nvme_rdma_queue *queue = cm_id->context;
1349 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1350 rdma_event_msg(ev->event), ev->event,
1353 switch (ev->event) {
1354 case RDMA_CM_EVENT_ADDR_RESOLVED:
1355 cm_error = nvme_rdma_addr_resolved(queue);
1357 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1358 cm_error = nvme_rdma_route_resolved(queue);
1360 case RDMA_CM_EVENT_ESTABLISHED:
1361 queue->cm_error = nvme_rdma_conn_established(queue);
1362 /* complete cm_done regardless of success/failure */
1363 complete(&queue->cm_done);
1365 case RDMA_CM_EVENT_REJECTED:
1366 cm_error = nvme_rdma_conn_rejected(queue, ev);
1368 case RDMA_CM_EVENT_ADDR_ERROR:
1369 case RDMA_CM_EVENT_ROUTE_ERROR:
1370 case RDMA_CM_EVENT_CONNECT_ERROR:
1371 case RDMA_CM_EVENT_UNREACHABLE:
1372 dev_dbg(queue->ctrl->ctrl.device,
1373 "CM error event %d\n", ev->event);
1374 cm_error = -ECONNRESET;
1376 case RDMA_CM_EVENT_DISCONNECTED:
1377 case RDMA_CM_EVENT_ADDR_CHANGE:
1378 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1379 dev_dbg(queue->ctrl->ctrl.device,
1380 "disconnect received - connection closed\n");
1381 nvme_rdma_error_recovery(queue->ctrl);
1383 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1384 /* device removal is handled via the ib_client API */
1387 dev_err(queue->ctrl->ctrl.device,
1388 "Unexpected RDMA CM event (%d)\n", ev->event);
1389 nvme_rdma_error_recovery(queue->ctrl);
1394 queue->cm_error = cm_error;
1395 complete(&queue->cm_done);
1401 static enum blk_eh_timer_return
1402 nvme_rdma_timeout(struct request *rq, bool reserved)
1404 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1406 /* queue error recovery */
1407 nvme_rdma_error_recovery(req->queue->ctrl);
1409 /* fail with DNR on cmd timeout */
1410 rq->errors = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1412 return BLK_EH_HANDLED;
1416 * We cannot accept any other command until the Connect command has completed.
1418 static inline bool nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue,
1421 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
1422 struct nvme_command *cmd = nvme_req(rq)->cmd;
1424 if (!blk_rq_is_passthrough(rq) ||
1425 cmd->common.opcode != nvme_fabrics_command ||
1426 cmd->fabrics.fctype != nvme_fabrics_type_connect)
1433 static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1434 const struct blk_mq_queue_data *bd)
1436 struct nvme_ns *ns = hctx->queue->queuedata;
1437 struct nvme_rdma_queue *queue = hctx->driver_data;
1438 struct request *rq = bd->rq;
1439 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1440 struct nvme_rdma_qe *sqe = &req->sqe;
1441 struct nvme_command *c = sqe->data;
1443 struct ib_device *dev;
1446 WARN_ON_ONCE(rq->tag < 0);
1448 if (!nvme_rdma_queue_is_ready(queue, rq))
1449 return BLK_MQ_RQ_QUEUE_BUSY;
1451 dev = queue->device->dev;
1452 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1453 sizeof(struct nvme_command), DMA_TO_DEVICE);
1455 ret = nvme_setup_cmd(ns, rq, c);
1456 if (ret != BLK_MQ_RQ_QUEUE_OK)
1459 blk_mq_start_request(rq);
1461 ret = nvme_rdma_map_data(queue, rq, c);
1463 dev_err(queue->ctrl->ctrl.device,
1464 "Failed to map data (%d)\n", ret);
1465 nvme_cleanup_cmd(rq);
1469 ib_dma_sync_single_for_device(dev, sqe->dma,
1470 sizeof(struct nvme_command), DMA_TO_DEVICE);
1472 if (req_op(rq) == REQ_OP_FLUSH)
1474 ret = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1475 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
1477 nvme_rdma_unmap_data(queue, rq);
1481 return BLK_MQ_RQ_QUEUE_OK;
1483 return (ret == -ENOMEM || ret == -EAGAIN) ?
1484 BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR;
1487 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1489 struct nvme_rdma_queue *queue = hctx->driver_data;
1490 struct ib_cq *cq = queue->ib_cq;
1494 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1495 while (ib_poll_cq(cq, 1, &wc) > 0) {
1496 struct ib_cqe *cqe = wc.wr_cqe;
1499 if (cqe->done == nvme_rdma_recv_done)
1500 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1509 static void nvme_rdma_complete_rq(struct request *rq)
1511 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1512 struct nvme_rdma_queue *queue = req->queue;
1515 nvme_rdma_unmap_data(queue, rq);
1517 if (unlikely(rq->errors)) {
1518 if (nvme_req_needs_retry(rq, rq->errors)) {
1519 nvme_requeue_req(rq);
1523 if (blk_rq_is_passthrough(rq))
1526 error = nvme_error_status(rq->errors);
1529 blk_mq_end_request(rq, error);
1532 static struct blk_mq_ops nvme_rdma_mq_ops = {
1533 .queue_rq = nvme_rdma_queue_rq,
1534 .complete = nvme_rdma_complete_rq,
1535 .init_request = nvme_rdma_init_request,
1536 .exit_request = nvme_rdma_exit_request,
1537 .reinit_request = nvme_rdma_reinit_request,
1538 .init_hctx = nvme_rdma_init_hctx,
1539 .poll = nvme_rdma_poll,
1540 .timeout = nvme_rdma_timeout,
1543 static struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1544 .queue_rq = nvme_rdma_queue_rq,
1545 .complete = nvme_rdma_complete_rq,
1546 .init_request = nvme_rdma_init_admin_request,
1547 .exit_request = nvme_rdma_exit_admin_request,
1548 .reinit_request = nvme_rdma_reinit_request,
1549 .init_hctx = nvme_rdma_init_admin_hctx,
1550 .timeout = nvme_rdma_timeout,
1553 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
1557 error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
1561 ctrl->device = ctrl->queues[0].device;
1564 * We need a reference on the device as long as the tag_set is alive,
1565 * as the MRs in the request structures need a valid ib_device.
1568 if (!nvme_rdma_dev_get(ctrl->device))
1569 goto out_free_queue;
1571 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
1572 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
1574 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1575 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
1576 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
1577 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
1578 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1579 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1580 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1581 ctrl->admin_tag_set.driver_data = ctrl;
1582 ctrl->admin_tag_set.nr_hw_queues = 1;
1583 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1585 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1589 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1590 if (IS_ERR(ctrl->ctrl.admin_q)) {
1591 error = PTR_ERR(ctrl->ctrl.admin_q);
1592 goto out_free_tagset;
1595 error = nvmf_connect_admin_queue(&ctrl->ctrl);
1597 goto out_cleanup_queue;
1599 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
1601 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
1603 dev_err(ctrl->ctrl.device,
1604 "prop_get NVME_REG_CAP failed\n");
1605 goto out_cleanup_queue;
1609 min_t(int, NVME_CAP_MQES(ctrl->cap), ctrl->ctrl.sqsize);
1611 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
1613 goto out_cleanup_queue;
1615 ctrl->ctrl.max_hw_sectors =
1616 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
1618 error = nvme_init_identify(&ctrl->ctrl);
1620 goto out_cleanup_queue;
1622 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
1623 &ctrl->async_event_sqe, sizeof(struct nvme_command),
1626 goto out_cleanup_queue;
1628 nvme_start_keep_alive(&ctrl->ctrl);
1633 blk_cleanup_queue(ctrl->ctrl.admin_q);
1635 /* disconnect and drain the queue before freeing the tagset */
1636 nvme_rdma_stop_queue(&ctrl->queues[0]);
1637 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1639 nvme_rdma_dev_put(ctrl->device);
1641 nvme_rdma_free_queue(&ctrl->queues[0]);
1645 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1647 nvme_stop_keep_alive(&ctrl->ctrl);
1648 cancel_work_sync(&ctrl->err_work);
1649 cancel_delayed_work_sync(&ctrl->reconnect_work);
1651 if (ctrl->queue_count > 1) {
1652 nvme_stop_queues(&ctrl->ctrl);
1653 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1654 nvme_cancel_request, &ctrl->ctrl);
1655 nvme_rdma_free_io_queues(ctrl);
1658 if (test_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[0].flags))
1659 nvme_shutdown_ctrl(&ctrl->ctrl);
1661 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
1662 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1663 nvme_cancel_request, &ctrl->ctrl);
1664 nvme_rdma_destroy_admin_queue(ctrl);
1667 static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1669 nvme_uninit_ctrl(&ctrl->ctrl);
1671 nvme_rdma_shutdown_ctrl(ctrl);
1673 if (ctrl->ctrl.tagset) {
1674 blk_cleanup_queue(ctrl->ctrl.connect_q);
1675 blk_mq_free_tag_set(&ctrl->tag_set);
1676 nvme_rdma_dev_put(ctrl->device);
1679 nvme_put_ctrl(&ctrl->ctrl);
1682 static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1684 struct nvme_rdma_ctrl *ctrl = container_of(work,
1685 struct nvme_rdma_ctrl, delete_work);
1687 __nvme_rdma_remove_ctrl(ctrl, true);
1690 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1692 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1695 if (!queue_work(nvme_rdma_wq, &ctrl->delete_work))
1701 static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1703 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1707 * Keep a reference until all work is flushed since
1708 * __nvme_rdma_del_ctrl can free the ctrl mem
1710 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1712 ret = __nvme_rdma_del_ctrl(ctrl);
1714 flush_work(&ctrl->delete_work);
1715 nvme_put_ctrl(&ctrl->ctrl);
1719 static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1721 struct nvme_rdma_ctrl *ctrl = container_of(work,
1722 struct nvme_rdma_ctrl, delete_work);
1724 __nvme_rdma_remove_ctrl(ctrl, false);
1727 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1729 struct nvme_rdma_ctrl *ctrl = container_of(work,
1730 struct nvme_rdma_ctrl, reset_work);
1734 nvme_rdma_shutdown_ctrl(ctrl);
1736 ret = nvme_rdma_configure_admin_queue(ctrl);
1738 /* ctrl is already shutdown, just remove the ctrl */
1739 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1743 if (ctrl->queue_count > 1) {
1744 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
1748 ret = nvme_rdma_init_io_queues(ctrl);
1752 ret = nvme_rdma_connect_io_queues(ctrl);
1757 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1758 WARN_ON_ONCE(!changed);
1760 if (ctrl->queue_count > 1) {
1761 nvme_start_queues(&ctrl->ctrl);
1762 nvme_queue_scan(&ctrl->ctrl);
1763 nvme_queue_async_events(&ctrl->ctrl);
1769 /* Deleting this dead controller... */
1770 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1771 WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work));
1774 static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl)
1776 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1778 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1781 if (!queue_work(nvme_rdma_wq, &ctrl->reset_work))
1784 flush_work(&ctrl->reset_work);
1789 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1791 .module = THIS_MODULE,
1793 .reg_read32 = nvmf_reg_read32,
1794 .reg_read64 = nvmf_reg_read64,
1795 .reg_write32 = nvmf_reg_write32,
1796 .reset_ctrl = nvme_rdma_reset_ctrl,
1797 .free_ctrl = nvme_rdma_free_ctrl,
1798 .submit_async_event = nvme_rdma_submit_async_event,
1799 .delete_ctrl = nvme_rdma_del_ctrl,
1800 .get_subsysnqn = nvmf_get_subsysnqn,
1801 .get_address = nvmf_get_address,
1804 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1808 ret = nvme_rdma_init_io_queues(ctrl);
1813 * We need a reference on the device as long as the tag_set is alive,
1814 * as the MRs in the request structures need a valid ib_device.
1817 if (!nvme_rdma_dev_get(ctrl->device))
1818 goto out_free_io_queues;
1820 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1821 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
1822 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
1823 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1824 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1825 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1826 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1827 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1828 ctrl->tag_set.driver_data = ctrl;
1829 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
1830 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1832 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1835 ctrl->ctrl.tagset = &ctrl->tag_set;
1837 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1838 if (IS_ERR(ctrl->ctrl.connect_q)) {
1839 ret = PTR_ERR(ctrl->ctrl.connect_q);
1840 goto out_free_tag_set;
1843 ret = nvme_rdma_connect_io_queues(ctrl);
1845 goto out_cleanup_connect_q;
1849 out_cleanup_connect_q:
1850 blk_cleanup_queue(ctrl->ctrl.connect_q);
1852 blk_mq_free_tag_set(&ctrl->tag_set);
1854 nvme_rdma_dev_put(ctrl->device);
1856 nvme_rdma_free_io_queues(ctrl);
1860 static int nvme_rdma_parse_ipaddr(struct sockaddr_in *in_addr, char *p)
1862 u8 *addr = (u8 *)&in_addr->sin_addr.s_addr;
1863 size_t buflen = strlen(p);
1865 /* XXX: handle IPv6 addresses */
1867 if (buflen > INET_ADDRSTRLEN)
1869 if (in4_pton(p, buflen, addr, '\0', NULL) == 0)
1871 in_addr->sin_family = AF_INET;
1875 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1876 struct nvmf_ctrl_options *opts)
1878 struct nvme_rdma_ctrl *ctrl;
1882 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1884 return ERR_PTR(-ENOMEM);
1885 ctrl->ctrl.opts = opts;
1886 INIT_LIST_HEAD(&ctrl->list);
1888 ret = nvme_rdma_parse_ipaddr(&ctrl->addr_in, opts->traddr);
1890 pr_err("malformed IP address passed: %s\n", opts->traddr);
1894 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
1895 ret = nvme_rdma_parse_ipaddr(&ctrl->src_addr_in,
1898 pr_err("malformed src IP address passed: %s\n",
1904 if (opts->mask & NVMF_OPT_TRSVCID) {
1907 ret = kstrtou16(opts->trsvcid, 0, &port);
1911 ctrl->addr_in.sin_port = cpu_to_be16(port);
1913 ctrl->addr_in.sin_port = cpu_to_be16(NVME_RDMA_IP_PORT);
1916 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1917 0 /* no quirks, we're perfect! */);
1921 ctrl->reconnect_delay = opts->reconnect_delay;
1922 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1923 nvme_rdma_reconnect_ctrl_work);
1924 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1925 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1926 INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work);
1927 spin_lock_init(&ctrl->lock);
1929 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1930 ctrl->ctrl.sqsize = opts->queue_size - 1;
1931 ctrl->ctrl.kato = opts->kato;
1934 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues),
1937 goto out_uninit_ctrl;
1939 ret = nvme_rdma_configure_admin_queue(ctrl);
1941 goto out_kfree_queues;
1943 /* sanity check icdoff */
1944 if (ctrl->ctrl.icdoff) {
1945 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1946 goto out_remove_admin_queue;
1949 /* sanity check keyed sgls */
1950 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1951 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1952 goto out_remove_admin_queue;
1955 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1956 /* warn if maxcmd is lower than queue_size */
1957 dev_warn(ctrl->ctrl.device,
1958 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1959 opts->queue_size, ctrl->ctrl.maxcmd);
1960 opts->queue_size = ctrl->ctrl.maxcmd;
1963 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1964 /* warn if sqsize is lower than queue_size */
1965 dev_warn(ctrl->ctrl.device,
1966 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1967 opts->queue_size, ctrl->ctrl.sqsize + 1);
1968 opts->queue_size = ctrl->ctrl.sqsize + 1;
1971 if (opts->nr_io_queues) {
1972 ret = nvme_rdma_create_io_queues(ctrl);
1974 goto out_remove_admin_queue;
1977 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1978 WARN_ON_ONCE(!changed);
1980 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
1981 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1983 kref_get(&ctrl->ctrl.kref);
1985 mutex_lock(&nvme_rdma_ctrl_mutex);
1986 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1987 mutex_unlock(&nvme_rdma_ctrl_mutex);
1989 if (opts->nr_io_queues) {
1990 nvme_queue_scan(&ctrl->ctrl);
1991 nvme_queue_async_events(&ctrl->ctrl);
1996 out_remove_admin_queue:
1997 nvme_stop_keep_alive(&ctrl->ctrl);
1998 nvme_rdma_destroy_admin_queue(ctrl);
2000 kfree(ctrl->queues);
2002 nvme_uninit_ctrl(&ctrl->ctrl);
2003 nvme_put_ctrl(&ctrl->ctrl);
2006 return ERR_PTR(ret);
2009 return ERR_PTR(ret);
2012 static struct nvmf_transport_ops nvme_rdma_transport = {
2014 .required_opts = NVMF_OPT_TRADDR,
2015 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2016 NVMF_OPT_HOST_TRADDR,
2017 .create_ctrl = nvme_rdma_create_ctrl,
2020 static void nvme_rdma_add_one(struct ib_device *ib_device)
2024 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2026 struct nvme_rdma_ctrl *ctrl;
2028 /* Delete all controllers using this device */
2029 mutex_lock(&nvme_rdma_ctrl_mutex);
2030 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2031 if (ctrl->device->dev != ib_device)
2033 dev_info(ctrl->ctrl.device,
2034 "Removing ctrl: NQN \"%s\", addr %pISp\n",
2035 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2036 __nvme_rdma_del_ctrl(ctrl);
2038 mutex_unlock(&nvme_rdma_ctrl_mutex);
2040 flush_workqueue(nvme_rdma_wq);
2043 static struct ib_client nvme_rdma_ib_client = {
2044 .name = "nvme_rdma",
2045 .add = nvme_rdma_add_one,
2046 .remove = nvme_rdma_remove_one
2049 static int __init nvme_rdma_init_module(void)
2053 nvme_rdma_wq = create_workqueue("nvme_rdma_wq");
2057 ret = ib_register_client(&nvme_rdma_ib_client);
2059 destroy_workqueue(nvme_rdma_wq);
2063 return nvmf_register_transport(&nvme_rdma_transport);
2066 static void __exit nvme_rdma_cleanup_module(void)
2068 nvmf_unregister_transport(&nvme_rdma_transport);
2069 ib_unregister_client(&nvme_rdma_ib_client);
2070 destroy_workqueue(nvme_rdma_wq);
2073 module_init(nvme_rdma_init_module);
2074 module_exit(nvme_rdma_cleanup_module);
2076 MODULE_LICENSE("GPL v2");