1 // SPDX-License-Identifier: GPL-2.0
3 * NVMe over Fabrics RDMA host code.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <rdma/mr_pool.h>
11 #include <linux/err.h>
12 #include <linux/string.h>
13 #include <linux/atomic.h>
14 #include <linux/blk-mq.h>
15 #include <linux/blk-mq-rdma.h>
16 #include <linux/types.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <linux/scatterlist.h>
20 #include <linux/nvme.h>
21 #include <asm/unaligned.h>
23 #include <rdma/ib_verbs.h>
24 #include <rdma/rdma_cm.h>
25 #include <linux/nvme-rdma.h>
31 #define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
33 #define NVME_RDMA_MAX_SEGMENTS 256
35 #define NVME_RDMA_MAX_INLINE_SEGMENTS 4
37 struct nvme_rdma_device {
38 struct ib_device *dev;
41 struct list_head entry;
42 unsigned int num_inline_segments;
51 struct nvme_rdma_queue;
52 struct nvme_rdma_request {
53 struct nvme_request req;
55 struct nvme_rdma_qe sqe;
56 union nvme_result result;
59 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
62 struct ib_reg_wr reg_wr;
63 struct ib_cqe reg_cqe;
64 struct nvme_rdma_queue *queue;
65 struct sg_table sg_table;
66 struct scatterlist first_sgl[];
69 enum nvme_rdma_queue_flags {
70 NVME_RDMA_Q_ALLOCATED = 0,
72 NVME_RDMA_Q_TR_READY = 2,
75 struct nvme_rdma_queue {
76 struct nvme_rdma_qe *rsp_ring;
78 size_t cmnd_capsule_len;
79 struct nvme_rdma_ctrl *ctrl;
80 struct nvme_rdma_device *device;
85 struct rdma_cm_id *cm_id;
87 struct completion cm_done;
90 struct nvme_rdma_ctrl {
91 /* read only in the hot path */
92 struct nvme_rdma_queue *queues;
94 /* other member variables */
95 struct blk_mq_tag_set tag_set;
96 struct work_struct err_work;
98 struct nvme_rdma_qe async_event_sqe;
100 struct delayed_work reconnect_work;
102 struct list_head list;
104 struct blk_mq_tag_set admin_tag_set;
105 struct nvme_rdma_device *device;
109 struct sockaddr_storage addr;
110 struct sockaddr_storage src_addr;
112 struct nvme_ctrl ctrl;
113 bool use_inline_data;
114 u32 io_queues[HCTX_MAX_TYPES];
117 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
119 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
122 static LIST_HEAD(device_list);
123 static DEFINE_MUTEX(device_list_mutex);
125 static LIST_HEAD(nvme_rdma_ctrl_list);
126 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
129 * Disabling this option makes small I/O goes faster, but is fundamentally
130 * unsafe. With it turned off we will have to register a global rkey that
131 * allows read and write access to all physical memory.
133 static bool register_always = true;
134 module_param(register_always, bool, 0444);
135 MODULE_PARM_DESC(register_always,
136 "Use memory registration even for contiguous memory regions");
138 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
139 struct rdma_cm_event *event);
140 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
142 static const struct blk_mq_ops nvme_rdma_mq_ops;
143 static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
145 /* XXX: really should move to a generic header sooner or later.. */
146 static inline void put_unaligned_le24(u32 val, u8 *p)
153 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
155 return queue - queue->ctrl->queues;
158 static bool nvme_rdma_poll_queue(struct nvme_rdma_queue *queue)
160 return nvme_rdma_queue_idx(queue) >
161 queue->ctrl->io_queues[HCTX_TYPE_DEFAULT] +
162 queue->ctrl->io_queues[HCTX_TYPE_READ];
165 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
167 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
170 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
171 size_t capsule_size, enum dma_data_direction dir)
173 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
177 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
178 size_t capsule_size, enum dma_data_direction dir)
180 qe->data = kzalloc(capsule_size, GFP_KERNEL);
184 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
185 if (ib_dma_mapping_error(ibdev, qe->dma)) {
194 static void nvme_rdma_free_ring(struct ib_device *ibdev,
195 struct nvme_rdma_qe *ring, size_t ib_queue_size,
196 size_t capsule_size, enum dma_data_direction dir)
200 for (i = 0; i < ib_queue_size; i++)
201 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
205 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
206 size_t ib_queue_size, size_t capsule_size,
207 enum dma_data_direction dir)
209 struct nvme_rdma_qe *ring;
212 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
217 * Bind the CQEs (post recv buffers) DMA mapping to the RDMA queue
218 * lifetime. It's safe, since any chage in the underlying RDMA device
219 * will issue error recovery and queue re-creation.
221 for (i = 0; i < ib_queue_size; i++) {
222 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
229 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
233 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
235 pr_debug("QP event %s (%d)\n",
236 ib_event_msg(event->event), event->event);
240 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
244 ret = wait_for_completion_interruptible_timeout(&queue->cm_done,
245 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
250 WARN_ON_ONCE(queue->cm_error > 0);
251 return queue->cm_error;
254 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
256 struct nvme_rdma_device *dev = queue->device;
257 struct ib_qp_init_attr init_attr;
260 memset(&init_attr, 0, sizeof(init_attr));
261 init_attr.event_handler = nvme_rdma_qp_event;
263 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
265 init_attr.cap.max_recv_wr = queue->queue_size + 1;
266 init_attr.cap.max_recv_sge = 1;
267 init_attr.cap.max_send_sge = 1 + dev->num_inline_segments;
268 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
269 init_attr.qp_type = IB_QPT_RC;
270 init_attr.send_cq = queue->ib_cq;
271 init_attr.recv_cq = queue->ib_cq;
273 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
275 queue->qp = queue->cm_id->qp;
279 static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
280 struct request *rq, unsigned int hctx_idx)
282 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
284 kfree(req->sqe.data);
287 static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
288 struct request *rq, unsigned int hctx_idx,
289 unsigned int numa_node)
291 struct nvme_rdma_ctrl *ctrl = set->driver_data;
292 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
293 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
294 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
296 nvme_req(rq)->ctrl = &ctrl->ctrl;
297 req->sqe.data = kzalloc(sizeof(struct nvme_command), GFP_KERNEL);
306 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
307 unsigned int hctx_idx)
309 struct nvme_rdma_ctrl *ctrl = data;
310 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
312 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
314 hctx->driver_data = queue;
318 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
319 unsigned int hctx_idx)
321 struct nvme_rdma_ctrl *ctrl = data;
322 struct nvme_rdma_queue *queue = &ctrl->queues[0];
324 BUG_ON(hctx_idx != 0);
326 hctx->driver_data = queue;
330 static void nvme_rdma_free_dev(struct kref *ref)
332 struct nvme_rdma_device *ndev =
333 container_of(ref, struct nvme_rdma_device, ref);
335 mutex_lock(&device_list_mutex);
336 list_del(&ndev->entry);
337 mutex_unlock(&device_list_mutex);
339 ib_dealloc_pd(ndev->pd);
343 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
345 kref_put(&dev->ref, nvme_rdma_free_dev);
348 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
350 return kref_get_unless_zero(&dev->ref);
353 static struct nvme_rdma_device *
354 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
356 struct nvme_rdma_device *ndev;
358 mutex_lock(&device_list_mutex);
359 list_for_each_entry(ndev, &device_list, entry) {
360 if (ndev->dev->node_guid == cm_id->device->node_guid &&
361 nvme_rdma_dev_get(ndev))
365 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
369 ndev->dev = cm_id->device;
370 kref_init(&ndev->ref);
372 ndev->pd = ib_alloc_pd(ndev->dev,
373 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
374 if (IS_ERR(ndev->pd))
377 if (!(ndev->dev->attrs.device_cap_flags &
378 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
379 dev_err(&ndev->dev->dev,
380 "Memory registrations not supported.\n");
384 ndev->num_inline_segments = min(NVME_RDMA_MAX_INLINE_SEGMENTS,
385 ndev->dev->attrs.max_send_sge - 1);
386 list_add(&ndev->entry, &device_list);
388 mutex_unlock(&device_list_mutex);
392 ib_dealloc_pd(ndev->pd);
396 mutex_unlock(&device_list_mutex);
400 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
402 struct nvme_rdma_device *dev;
403 struct ib_device *ibdev;
405 if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags))
411 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
414 * The cm_id object might have been destroyed during RDMA connection
415 * establishment error flow to avoid getting other cma events, thus
416 * the destruction of the QP shouldn't use rdma_cm API.
418 ib_destroy_qp(queue->qp);
419 ib_free_cq(queue->ib_cq);
421 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
422 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
424 nvme_rdma_dev_put(dev);
427 static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev)
429 return min_t(u32, NVME_RDMA_MAX_SEGMENTS,
430 ibdev->attrs.max_fast_reg_page_list_len);
433 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
435 struct ib_device *ibdev;
436 const int send_wr_factor = 3; /* MR, SEND, INV */
437 const int cq_factor = send_wr_factor + 1; /* + RECV */
438 int comp_vector, idx = nvme_rdma_queue_idx(queue);
439 enum ib_poll_context poll_ctx;
442 queue->device = nvme_rdma_find_get_device(queue->cm_id);
443 if (!queue->device) {
444 dev_err(queue->cm_id->device->dev.parent,
445 "no client data found!\n");
446 return -ECONNREFUSED;
448 ibdev = queue->device->dev;
451 * Spread I/O queues completion vectors according their queue index.
452 * Admin queues can always go on completion vector 0.
454 comp_vector = idx == 0 ? idx : idx - 1;
456 /* Polling queues need direct cq polling context */
457 if (nvme_rdma_poll_queue(queue))
458 poll_ctx = IB_POLL_DIRECT;
460 poll_ctx = IB_POLL_SOFTIRQ;
462 /* +1 for ib_stop_cq */
463 queue->ib_cq = ib_alloc_cq(ibdev, queue,
464 cq_factor * queue->queue_size + 1,
465 comp_vector, poll_ctx);
466 if (IS_ERR(queue->ib_cq)) {
467 ret = PTR_ERR(queue->ib_cq);
471 ret = nvme_rdma_create_qp(queue, send_wr_factor);
473 goto out_destroy_ib_cq;
475 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
476 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
477 if (!queue->rsp_ring) {
482 ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs,
485 nvme_rdma_get_max_fr_pages(ibdev), 0);
487 dev_err(queue->ctrl->ctrl.device,
488 "failed to initialize MR pool sized %d for QID %d\n",
489 queue->queue_size, idx);
490 goto out_destroy_ring;
493 set_bit(NVME_RDMA_Q_TR_READY, &queue->flags);
498 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
499 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
501 rdma_destroy_qp(queue->cm_id);
503 ib_free_cq(queue->ib_cq);
505 nvme_rdma_dev_put(queue->device);
509 static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
510 int idx, size_t queue_size)
512 struct nvme_rdma_queue *queue;
513 struct sockaddr *src_addr = NULL;
516 queue = &ctrl->queues[idx];
518 init_completion(&queue->cm_done);
521 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
523 queue->cmnd_capsule_len = sizeof(struct nvme_command);
525 queue->queue_size = queue_size;
527 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
528 RDMA_PS_TCP, IB_QPT_RC);
529 if (IS_ERR(queue->cm_id)) {
530 dev_info(ctrl->ctrl.device,
531 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
532 return PTR_ERR(queue->cm_id);
535 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
536 src_addr = (struct sockaddr *)&ctrl->src_addr;
538 queue->cm_error = -ETIMEDOUT;
539 ret = rdma_resolve_addr(queue->cm_id, src_addr,
540 (struct sockaddr *)&ctrl->addr,
541 NVME_RDMA_CONNECT_TIMEOUT_MS);
543 dev_info(ctrl->ctrl.device,
544 "rdma_resolve_addr failed (%d).\n", ret);
545 goto out_destroy_cm_id;
548 ret = nvme_rdma_wait_for_cm(queue);
550 dev_info(ctrl->ctrl.device,
551 "rdma connection establishment failed (%d)\n", ret);
552 goto out_destroy_cm_id;
555 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
560 rdma_destroy_id(queue->cm_id);
561 nvme_rdma_destroy_queue_ib(queue);
565 static void __nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
567 rdma_disconnect(queue->cm_id);
568 ib_drain_qp(queue->qp);
571 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
573 if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
575 __nvme_rdma_stop_queue(queue);
578 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
580 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
583 nvme_rdma_destroy_queue_ib(queue);
584 rdma_destroy_id(queue->cm_id);
587 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
591 for (i = 1; i < ctrl->ctrl.queue_count; i++)
592 nvme_rdma_free_queue(&ctrl->queues[i]);
595 static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
599 for (i = 1; i < ctrl->ctrl.queue_count; i++)
600 nvme_rdma_stop_queue(&ctrl->queues[i]);
603 static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
605 struct nvme_rdma_queue *queue = &ctrl->queues[idx];
606 bool poll = nvme_rdma_poll_queue(queue);
610 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx, poll);
612 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
615 set_bit(NVME_RDMA_Q_LIVE, &queue->flags);
617 __nvme_rdma_stop_queue(queue);
618 dev_info(ctrl->ctrl.device,
619 "failed to connect queue: %d ret=%d\n", idx, ret);
624 static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
628 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
629 ret = nvme_rdma_start_queue(ctrl, i);
631 goto out_stop_queues;
637 for (i--; i >= 1; i--)
638 nvme_rdma_stop_queue(&ctrl->queues[i]);
642 static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
644 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
645 struct ib_device *ibdev = ctrl->device->dev;
646 unsigned int nr_io_queues, nr_default_queues;
647 unsigned int nr_read_queues, nr_poll_queues;
650 nr_read_queues = min_t(unsigned int, ibdev->num_comp_vectors,
651 min(opts->nr_io_queues, num_online_cpus()));
652 nr_default_queues = min_t(unsigned int, ibdev->num_comp_vectors,
653 min(opts->nr_write_queues, num_online_cpus()));
654 nr_poll_queues = min(opts->nr_poll_queues, num_online_cpus());
655 nr_io_queues = nr_read_queues + nr_default_queues + nr_poll_queues;
657 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
661 ctrl->ctrl.queue_count = nr_io_queues + 1;
662 if (ctrl->ctrl.queue_count < 2)
665 dev_info(ctrl->ctrl.device,
666 "creating %d I/O queues.\n", nr_io_queues);
668 if (opts->nr_write_queues && nr_read_queues < nr_io_queues) {
670 * separate read/write queues
671 * hand out dedicated default queues only after we have
672 * sufficient read queues.
674 ctrl->io_queues[HCTX_TYPE_READ] = nr_read_queues;
675 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_READ];
676 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
677 min(nr_default_queues, nr_io_queues);
678 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
681 * shared read/write queues
682 * either no write queues were requested, or we don't have
683 * sufficient queue count to have dedicated default queues.
685 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
686 min(nr_read_queues, nr_io_queues);
687 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
690 if (opts->nr_poll_queues && nr_io_queues) {
691 /* map dedicated poll queues only if we have queues left */
692 ctrl->io_queues[HCTX_TYPE_POLL] =
693 min(nr_poll_queues, nr_io_queues);
696 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
697 ret = nvme_rdma_alloc_queue(ctrl, i,
698 ctrl->ctrl.sqsize + 1);
700 goto out_free_queues;
706 for (i--; i >= 1; i--)
707 nvme_rdma_free_queue(&ctrl->queues[i]);
712 static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
715 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
716 struct blk_mq_tag_set *set;
720 set = &ctrl->admin_tag_set;
721 memset(set, 0, sizeof(*set));
722 set->ops = &nvme_rdma_admin_mq_ops;
723 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
724 set->reserved_tags = 2; /* connect + keep-alive */
725 set->numa_node = nctrl->numa_node;
726 set->cmd_size = sizeof(struct nvme_rdma_request) +
727 SG_CHUNK_SIZE * sizeof(struct scatterlist);
728 set->driver_data = ctrl;
729 set->nr_hw_queues = 1;
730 set->timeout = ADMIN_TIMEOUT;
731 set->flags = BLK_MQ_F_NO_SCHED;
733 set = &ctrl->tag_set;
734 memset(set, 0, sizeof(*set));
735 set->ops = &nvme_rdma_mq_ops;
736 set->queue_depth = nctrl->sqsize + 1;
737 set->reserved_tags = 1; /* fabric connect */
738 set->numa_node = nctrl->numa_node;
739 set->flags = BLK_MQ_F_SHOULD_MERGE;
740 set->cmd_size = sizeof(struct nvme_rdma_request) +
741 SG_CHUNK_SIZE * sizeof(struct scatterlist);
742 set->driver_data = ctrl;
743 set->nr_hw_queues = nctrl->queue_count - 1;
744 set->timeout = NVME_IO_TIMEOUT;
745 set->nr_maps = nctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2;
748 ret = blk_mq_alloc_tag_set(set);
755 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
759 blk_cleanup_queue(ctrl->ctrl.admin_q);
760 blk_mq_free_tag_set(ctrl->ctrl.admin_tagset);
762 if (ctrl->async_event_sqe.data) {
763 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
764 sizeof(struct nvme_command), DMA_TO_DEVICE);
765 ctrl->async_event_sqe.data = NULL;
767 nvme_rdma_free_queue(&ctrl->queues[0]);
770 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
775 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
779 ctrl->device = ctrl->queues[0].device;
780 ctrl->ctrl.numa_node = dev_to_node(ctrl->device->dev->dma_device);
782 ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev);
785 * Bind the async event SQE DMA mapping to the admin queue lifetime.
786 * It's safe, since any chage in the underlying RDMA device will issue
787 * error recovery and queue re-creation.
789 error = nvme_rdma_alloc_qe(ctrl->device->dev, &ctrl->async_event_sqe,
790 sizeof(struct nvme_command), DMA_TO_DEVICE);
795 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
796 if (IS_ERR(ctrl->ctrl.admin_tagset)) {
797 error = PTR_ERR(ctrl->ctrl.admin_tagset);
798 goto out_free_async_qe;
801 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
802 if (IS_ERR(ctrl->ctrl.admin_q)) {
803 error = PTR_ERR(ctrl->ctrl.admin_q);
804 goto out_free_tagset;
808 error = nvme_rdma_start_queue(ctrl, 0);
810 goto out_cleanup_queue;
812 error = ctrl->ctrl.ops->reg_read64(&ctrl->ctrl, NVME_REG_CAP,
815 dev_err(ctrl->ctrl.device,
816 "prop_get NVME_REG_CAP failed\n");
821 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
823 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
827 ctrl->ctrl.max_hw_sectors =
828 (ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9);
830 error = nvme_init_identify(&ctrl->ctrl);
837 nvme_rdma_stop_queue(&ctrl->queues[0]);
840 blk_cleanup_queue(ctrl->ctrl.admin_q);
843 blk_mq_free_tag_set(ctrl->ctrl.admin_tagset);
845 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
846 sizeof(struct nvme_command), DMA_TO_DEVICE);
847 ctrl->async_event_sqe.data = NULL;
849 nvme_rdma_free_queue(&ctrl->queues[0]);
853 static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
857 blk_cleanup_queue(ctrl->ctrl.connect_q);
858 blk_mq_free_tag_set(ctrl->ctrl.tagset);
860 nvme_rdma_free_io_queues(ctrl);
863 static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
867 ret = nvme_rdma_alloc_io_queues(ctrl);
872 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
873 if (IS_ERR(ctrl->ctrl.tagset)) {
874 ret = PTR_ERR(ctrl->ctrl.tagset);
875 goto out_free_io_queues;
878 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
879 if (IS_ERR(ctrl->ctrl.connect_q)) {
880 ret = PTR_ERR(ctrl->ctrl.connect_q);
881 goto out_free_tag_set;
884 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
885 ctrl->ctrl.queue_count - 1);
888 ret = nvme_rdma_start_io_queues(ctrl);
890 goto out_cleanup_connect_q;
894 out_cleanup_connect_q:
896 blk_cleanup_queue(ctrl->ctrl.connect_q);
899 blk_mq_free_tag_set(ctrl->ctrl.tagset);
901 nvme_rdma_free_io_queues(ctrl);
905 static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl *ctrl,
908 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
909 nvme_rdma_stop_queue(&ctrl->queues[0]);
910 if (ctrl->ctrl.admin_tagset)
911 blk_mq_tagset_busy_iter(ctrl->ctrl.admin_tagset,
912 nvme_cancel_request, &ctrl->ctrl);
913 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
914 nvme_rdma_destroy_admin_queue(ctrl, remove);
917 static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl *ctrl,
920 if (ctrl->ctrl.queue_count > 1) {
921 nvme_stop_queues(&ctrl->ctrl);
922 nvme_rdma_stop_io_queues(ctrl);
923 if (ctrl->ctrl.tagset)
924 blk_mq_tagset_busy_iter(ctrl->ctrl.tagset,
925 nvme_cancel_request, &ctrl->ctrl);
927 nvme_start_queues(&ctrl->ctrl);
928 nvme_rdma_destroy_io_queues(ctrl, remove);
932 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
934 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
936 if (list_empty(&ctrl->list))
939 mutex_lock(&nvme_rdma_ctrl_mutex);
940 list_del(&ctrl->list);
941 mutex_unlock(&nvme_rdma_ctrl_mutex);
943 nvmf_free_options(nctrl->opts);
949 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
951 /* If we are resetting/deleting then do nothing */
952 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
953 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
954 ctrl->ctrl.state == NVME_CTRL_LIVE);
958 if (nvmf_should_reconnect(&ctrl->ctrl)) {
959 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
960 ctrl->ctrl.opts->reconnect_delay);
961 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
962 ctrl->ctrl.opts->reconnect_delay * HZ);
964 nvme_delete_ctrl(&ctrl->ctrl);
968 static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new)
973 ret = nvme_rdma_configure_admin_queue(ctrl, new);
977 if (ctrl->ctrl.icdoff) {
978 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
982 if (!(ctrl->ctrl.sgls & (1 << 2))) {
983 dev_err(ctrl->ctrl.device,
984 "Mandatory keyed sgls are not supported!\n");
988 if (ctrl->ctrl.opts->queue_size > ctrl->ctrl.sqsize + 1) {
989 dev_warn(ctrl->ctrl.device,
990 "queue_size %zu > ctrl sqsize %u, clamping down\n",
991 ctrl->ctrl.opts->queue_size, ctrl->ctrl.sqsize + 1);
994 if (ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) {
995 dev_warn(ctrl->ctrl.device,
996 "sqsize %u > ctrl maxcmd %u, clamping down\n",
997 ctrl->ctrl.sqsize + 1, ctrl->ctrl.maxcmd);
998 ctrl->ctrl.sqsize = ctrl->ctrl.maxcmd - 1;
1001 if (ctrl->ctrl.sgls & (1 << 20))
1002 ctrl->use_inline_data = true;
1004 if (ctrl->ctrl.queue_count > 1) {
1005 ret = nvme_rdma_configure_io_queues(ctrl, new);
1010 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1012 /* state change failure is ok if we're in DELETING state */
1013 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
1018 nvme_start_ctrl(&ctrl->ctrl);
1022 if (ctrl->ctrl.queue_count > 1)
1023 nvme_rdma_destroy_io_queues(ctrl, new);
1025 nvme_rdma_stop_queue(&ctrl->queues[0]);
1026 nvme_rdma_destroy_admin_queue(ctrl, new);
1030 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
1032 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
1033 struct nvme_rdma_ctrl, reconnect_work);
1035 ++ctrl->ctrl.nr_reconnects;
1037 if (nvme_rdma_setup_ctrl(ctrl, false))
1040 dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
1041 ctrl->ctrl.nr_reconnects);
1043 ctrl->ctrl.nr_reconnects = 0;
1048 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
1049 ctrl->ctrl.nr_reconnects);
1050 nvme_rdma_reconnect_or_remove(ctrl);
1053 static void nvme_rdma_error_recovery_work(struct work_struct *work)
1055 struct nvme_rdma_ctrl *ctrl = container_of(work,
1056 struct nvme_rdma_ctrl, err_work);
1058 nvme_stop_keep_alive(&ctrl->ctrl);
1059 nvme_rdma_teardown_io_queues(ctrl, false);
1060 nvme_start_queues(&ctrl->ctrl);
1061 nvme_rdma_teardown_admin_queue(ctrl, false);
1063 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
1064 /* state change failure is ok if we're in DELETING state */
1065 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
1069 nvme_rdma_reconnect_or_remove(ctrl);
1072 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
1074 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1077 queue_work(nvme_wq, &ctrl->err_work);
1080 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1083 struct nvme_rdma_queue *queue = cq->cq_context;
1084 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1086 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1087 dev_info(ctrl->ctrl.device,
1088 "%s for CQE 0x%p failed with status %s (%d)\n",
1090 ib_wc_status_msg(wc->status), wc->status);
1091 nvme_rdma_error_recovery(ctrl);
1094 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1096 if (unlikely(wc->status != IB_WC_SUCCESS))
1097 nvme_rdma_wr_error(cq, wc, "MEMREG");
1100 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1102 struct nvme_rdma_request *req =
1103 container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe);
1104 struct request *rq = blk_mq_rq_from_pdu(req);
1106 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1107 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
1111 if (refcount_dec_and_test(&req->ref))
1112 nvme_end_request(rq, req->status, req->result);
1116 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1117 struct nvme_rdma_request *req)
1119 struct ib_send_wr wr = {
1120 .opcode = IB_WR_LOCAL_INV,
1123 .send_flags = IB_SEND_SIGNALED,
1124 .ex.invalidate_rkey = req->mr->rkey,
1127 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1128 wr.wr_cqe = &req->reg_cqe;
1130 return ib_post_send(queue->qp, &wr, NULL);
1133 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1136 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1137 struct nvme_rdma_device *dev = queue->device;
1138 struct ib_device *ibdev = dev->dev;
1140 if (!blk_rq_nr_phys_segments(rq))
1144 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1148 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1149 req->nents, rq_data_dir(rq) ==
1150 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1152 nvme_cleanup_cmd(rq);
1153 sg_free_table_chained(&req->sg_table, SG_CHUNK_SIZE);
1156 static int nvme_rdma_set_sg_null(struct nvme_command *c)
1158 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1161 put_unaligned_le24(0, sg->length);
1162 put_unaligned_le32(0, sg->key);
1163 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1167 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
1168 struct nvme_rdma_request *req, struct nvme_command *c,
1171 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1172 struct scatterlist *sgl = req->sg_table.sgl;
1173 struct ib_sge *sge = &req->sge[1];
1177 for (i = 0; i < count; i++, sgl++, sge++) {
1178 sge->addr = sg_dma_address(sgl);
1179 sge->length = sg_dma_len(sgl);
1180 sge->lkey = queue->device->pd->local_dma_lkey;
1184 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1185 sg->length = cpu_to_le32(len);
1186 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1188 req->num_sge += count;
1192 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1193 struct nvme_rdma_request *req, struct nvme_command *c)
1195 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1197 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
1198 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
1199 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
1200 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1204 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1205 struct nvme_rdma_request *req, struct nvme_command *c,
1208 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1211 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs);
1212 if (WARN_ON_ONCE(!req->mr))
1216 * Align the MR to a 4K page size to match the ctrl page size and
1217 * the block virtual boundary.
1219 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K);
1220 if (unlikely(nr < count)) {
1221 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1228 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1230 req->reg_cqe.done = nvme_rdma_memreg_done;
1231 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1232 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1233 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1234 req->reg_wr.wr.num_sge = 0;
1235 req->reg_wr.mr = req->mr;
1236 req->reg_wr.key = req->mr->rkey;
1237 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1238 IB_ACCESS_REMOTE_READ |
1239 IB_ACCESS_REMOTE_WRITE;
1241 sg->addr = cpu_to_le64(req->mr->iova);
1242 put_unaligned_le24(req->mr->length, sg->length);
1243 put_unaligned_le32(req->mr->rkey, sg->key);
1244 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1245 NVME_SGL_FMT_INVALIDATE;
1250 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
1251 struct request *rq, struct nvme_command *c)
1253 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1254 struct nvme_rdma_device *dev = queue->device;
1255 struct ib_device *ibdev = dev->dev;
1259 refcount_set(&req->ref, 2); /* send and recv completions */
1261 c->common.flags |= NVME_CMD_SGL_METABUF;
1263 if (!blk_rq_nr_phys_segments(rq))
1264 return nvme_rdma_set_sg_null(c);
1266 req->sg_table.sgl = req->first_sgl;
1267 ret = sg_alloc_table_chained(&req->sg_table,
1268 blk_rq_nr_phys_segments(rq), req->sg_table.sgl,
1273 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
1275 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
1276 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1277 if (unlikely(count <= 0)) {
1279 goto out_free_table;
1282 if (count <= dev->num_inline_segments) {
1283 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1284 queue->ctrl->use_inline_data &&
1285 blk_rq_payload_bytes(rq) <=
1286 nvme_rdma_inline_data_size(queue)) {
1287 ret = nvme_rdma_map_sg_inline(queue, req, c, count);
1291 if (count == 1 && dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
1292 ret = nvme_rdma_map_sg_single(queue, req, c);
1297 ret = nvme_rdma_map_sg_fr(queue, req, c, count);
1305 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1306 req->nents, rq_data_dir(rq) ==
1307 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1309 sg_free_table_chained(&req->sg_table, SG_CHUNK_SIZE);
1313 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1315 struct nvme_rdma_qe *qe =
1316 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1317 struct nvme_rdma_request *req =
1318 container_of(qe, struct nvme_rdma_request, sqe);
1319 struct request *rq = blk_mq_rq_from_pdu(req);
1321 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1322 nvme_rdma_wr_error(cq, wc, "SEND");
1326 if (refcount_dec_and_test(&req->ref))
1327 nvme_end_request(rq, req->status, req->result);
1330 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1331 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1332 struct ib_send_wr *first)
1334 struct ib_send_wr wr;
1337 sge->addr = qe->dma;
1338 sge->length = sizeof(struct nvme_command),
1339 sge->lkey = queue->device->pd->local_dma_lkey;
1342 wr.wr_cqe = &qe->cqe;
1344 wr.num_sge = num_sge;
1345 wr.opcode = IB_WR_SEND;
1346 wr.send_flags = IB_SEND_SIGNALED;
1353 ret = ib_post_send(queue->qp, first, NULL);
1354 if (unlikely(ret)) {
1355 dev_err(queue->ctrl->ctrl.device,
1356 "%s failed with error code %d\n", __func__, ret);
1361 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1362 struct nvme_rdma_qe *qe)
1364 struct ib_recv_wr wr;
1368 list.addr = qe->dma;
1369 list.length = sizeof(struct nvme_completion);
1370 list.lkey = queue->device->pd->local_dma_lkey;
1372 qe->cqe.done = nvme_rdma_recv_done;
1375 wr.wr_cqe = &qe->cqe;
1379 ret = ib_post_recv(queue->qp, &wr, NULL);
1380 if (unlikely(ret)) {
1381 dev_err(queue->ctrl->ctrl.device,
1382 "%s failed with error code %d\n", __func__, ret);
1387 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1389 u32 queue_idx = nvme_rdma_queue_idx(queue);
1392 return queue->ctrl->admin_tag_set.tags[queue_idx];
1393 return queue->ctrl->tag_set.tags[queue_idx - 1];
1396 static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc)
1398 if (unlikely(wc->status != IB_WC_SUCCESS))
1399 nvme_rdma_wr_error(cq, wc, "ASYNC");
1402 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg)
1404 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1405 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1406 struct ib_device *dev = queue->device->dev;
1407 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1408 struct nvme_command *cmd = sqe->data;
1412 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1414 memset(cmd, 0, sizeof(*cmd));
1415 cmd->common.opcode = nvme_admin_async_event;
1416 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
1417 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1418 nvme_rdma_set_sg_null(cmd);
1420 sqe->cqe.done = nvme_rdma_async_done;
1422 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1425 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL);
1429 static void nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1430 struct nvme_completion *cqe, struct ib_wc *wc)
1433 struct nvme_rdma_request *req;
1435 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1437 dev_err(queue->ctrl->ctrl.device,
1438 "tag 0x%x on QP %#x not found\n",
1439 cqe->command_id, queue->qp->qp_num);
1440 nvme_rdma_error_recovery(queue->ctrl);
1443 req = blk_mq_rq_to_pdu(rq);
1445 req->status = cqe->status;
1446 req->result = cqe->result;
1448 if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
1449 if (unlikely(wc->ex.invalidate_rkey != req->mr->rkey)) {
1450 dev_err(queue->ctrl->ctrl.device,
1451 "Bogus remote invalidation for rkey %#x\n",
1453 nvme_rdma_error_recovery(queue->ctrl);
1455 } else if (req->mr) {
1458 ret = nvme_rdma_inv_rkey(queue, req);
1459 if (unlikely(ret < 0)) {
1460 dev_err(queue->ctrl->ctrl.device,
1461 "Queueing INV WR for rkey %#x failed (%d)\n",
1462 req->mr->rkey, ret);
1463 nvme_rdma_error_recovery(queue->ctrl);
1465 /* the local invalidation completion will end the request */
1469 if (refcount_dec_and_test(&req->ref))
1470 nvme_end_request(rq, req->status, req->result);
1473 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1475 struct nvme_rdma_qe *qe =
1476 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1477 struct nvme_rdma_queue *queue = cq->cq_context;
1478 struct ib_device *ibdev = queue->device->dev;
1479 struct nvme_completion *cqe = qe->data;
1480 const size_t len = sizeof(struct nvme_completion);
1482 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1483 nvme_rdma_wr_error(cq, wc, "RECV");
1487 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1489 * AEN requests are special as they don't time out and can
1490 * survive any kind of queue freeze and often don't respond to
1491 * aborts. We don't even bother to allocate a struct request
1492 * for them but rather special case them here.
1494 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1495 cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH))
1496 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1499 nvme_rdma_process_nvme_rsp(queue, cqe, wc);
1500 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1502 nvme_rdma_post_recv(queue, qe);
1505 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1509 for (i = 0; i < queue->queue_size; i++) {
1510 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1512 goto out_destroy_queue_ib;
1517 out_destroy_queue_ib:
1518 nvme_rdma_destroy_queue_ib(queue);
1522 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1523 struct rdma_cm_event *ev)
1525 struct rdma_cm_id *cm_id = queue->cm_id;
1526 int status = ev->status;
1527 const char *rej_msg;
1528 const struct nvme_rdma_cm_rej *rej_data;
1531 rej_msg = rdma_reject_msg(cm_id, status);
1532 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1534 if (rej_data && rej_data_len >= sizeof(u16)) {
1535 u16 sts = le16_to_cpu(rej_data->sts);
1537 dev_err(queue->ctrl->ctrl.device,
1538 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1539 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1541 dev_err(queue->ctrl->ctrl.device,
1542 "Connect rejected: status %d (%s).\n", status, rej_msg);
1548 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1552 ret = nvme_rdma_create_queue_ib(queue);
1556 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1558 dev_err(queue->ctrl->ctrl.device,
1559 "rdma_resolve_route failed (%d).\n",
1561 goto out_destroy_queue;
1567 nvme_rdma_destroy_queue_ib(queue);
1571 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1573 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1574 struct rdma_conn_param param = { };
1575 struct nvme_rdma_cm_req priv = { };
1578 param.qp_num = queue->qp->qp_num;
1579 param.flow_control = 1;
1581 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1582 /* maximum retry count */
1583 param.retry_count = 7;
1584 param.rnr_retry_count = 7;
1585 param.private_data = &priv;
1586 param.private_data_len = sizeof(priv);
1588 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1589 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1591 * set the admin queue depth to the minimum size
1592 * specified by the Fabrics standard.
1594 if (priv.qid == 0) {
1595 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1596 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
1599 * current interpretation of the fabrics spec
1600 * is at minimum you make hrqsize sqsize+1, or a
1601 * 1's based representation of sqsize.
1603 priv.hrqsize = cpu_to_le16(queue->queue_size);
1604 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1607 ret = rdma_connect(queue->cm_id, ¶m);
1609 dev_err(ctrl->ctrl.device,
1610 "rdma_connect failed (%d).\n", ret);
1611 goto out_destroy_queue_ib;
1616 out_destroy_queue_ib:
1617 nvme_rdma_destroy_queue_ib(queue);
1621 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1622 struct rdma_cm_event *ev)
1624 struct nvme_rdma_queue *queue = cm_id->context;
1627 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1628 rdma_event_msg(ev->event), ev->event,
1631 switch (ev->event) {
1632 case RDMA_CM_EVENT_ADDR_RESOLVED:
1633 cm_error = nvme_rdma_addr_resolved(queue);
1635 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1636 cm_error = nvme_rdma_route_resolved(queue);
1638 case RDMA_CM_EVENT_ESTABLISHED:
1639 queue->cm_error = nvme_rdma_conn_established(queue);
1640 /* complete cm_done regardless of success/failure */
1641 complete(&queue->cm_done);
1643 case RDMA_CM_EVENT_REJECTED:
1644 nvme_rdma_destroy_queue_ib(queue);
1645 cm_error = nvme_rdma_conn_rejected(queue, ev);
1647 case RDMA_CM_EVENT_ROUTE_ERROR:
1648 case RDMA_CM_EVENT_CONNECT_ERROR:
1649 case RDMA_CM_EVENT_UNREACHABLE:
1650 nvme_rdma_destroy_queue_ib(queue);
1652 case RDMA_CM_EVENT_ADDR_ERROR:
1653 dev_dbg(queue->ctrl->ctrl.device,
1654 "CM error event %d\n", ev->event);
1655 cm_error = -ECONNRESET;
1657 case RDMA_CM_EVENT_DISCONNECTED:
1658 case RDMA_CM_EVENT_ADDR_CHANGE:
1659 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1660 dev_dbg(queue->ctrl->ctrl.device,
1661 "disconnect received - connection closed\n");
1662 nvme_rdma_error_recovery(queue->ctrl);
1664 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1665 /* device removal is handled via the ib_client API */
1668 dev_err(queue->ctrl->ctrl.device,
1669 "Unexpected RDMA CM event (%d)\n", ev->event);
1670 nvme_rdma_error_recovery(queue->ctrl);
1675 queue->cm_error = cm_error;
1676 complete(&queue->cm_done);
1682 static enum blk_eh_timer_return
1683 nvme_rdma_timeout(struct request *rq, bool reserved)
1685 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1686 struct nvme_rdma_queue *queue = req->queue;
1687 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1689 dev_warn(ctrl->ctrl.device, "I/O %d QID %d timeout\n",
1690 rq->tag, nvme_rdma_queue_idx(queue));
1692 if (ctrl->ctrl.state != NVME_CTRL_LIVE) {
1694 * Teardown immediately if controller times out while starting
1695 * or we are already started error recovery. all outstanding
1696 * requests are completed on shutdown, so we return BLK_EH_DONE.
1698 flush_work(&ctrl->err_work);
1699 nvme_rdma_teardown_io_queues(ctrl, false);
1700 nvme_rdma_teardown_admin_queue(ctrl, false);
1704 dev_warn(ctrl->ctrl.device, "starting error recovery\n");
1705 nvme_rdma_error_recovery(ctrl);
1707 return BLK_EH_RESET_TIMER;
1710 static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1711 const struct blk_mq_queue_data *bd)
1713 struct nvme_ns *ns = hctx->queue->queuedata;
1714 struct nvme_rdma_queue *queue = hctx->driver_data;
1715 struct request *rq = bd->rq;
1716 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1717 struct nvme_rdma_qe *sqe = &req->sqe;
1718 struct nvme_command *c = sqe->data;
1719 struct ib_device *dev;
1720 bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags);
1724 WARN_ON_ONCE(rq->tag < 0);
1726 if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
1727 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
1729 dev = queue->device->dev;
1731 req->sqe.dma = ib_dma_map_single(dev, req->sqe.data,
1732 sizeof(struct nvme_command),
1734 err = ib_dma_mapping_error(dev, req->sqe.dma);
1736 return BLK_STS_RESOURCE;
1738 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1739 sizeof(struct nvme_command), DMA_TO_DEVICE);
1741 ret = nvme_setup_cmd(ns, rq, c);
1745 blk_mq_start_request(rq);
1747 err = nvme_rdma_map_data(queue, rq, c);
1748 if (unlikely(err < 0)) {
1749 dev_err(queue->ctrl->ctrl.device,
1750 "Failed to map data (%d)\n", err);
1751 nvme_cleanup_cmd(rq);
1755 sqe->cqe.done = nvme_rdma_send_done;
1757 ib_dma_sync_single_for_device(dev, sqe->dma,
1758 sizeof(struct nvme_command), DMA_TO_DEVICE);
1760 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1761 req->mr ? &req->reg_wr.wr : NULL);
1762 if (unlikely(err)) {
1763 nvme_rdma_unmap_data(queue, rq);
1770 if (err == -ENOMEM || err == -EAGAIN)
1771 ret = BLK_STS_RESOURCE;
1773 ret = BLK_STS_IOERR;
1775 ib_dma_unmap_single(dev, req->sqe.dma, sizeof(struct nvme_command),
1780 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx)
1782 struct nvme_rdma_queue *queue = hctx->driver_data;
1784 return ib_process_cq_direct(queue->ib_cq, -1);
1787 static void nvme_rdma_complete_rq(struct request *rq)
1789 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1790 struct nvme_rdma_queue *queue = req->queue;
1791 struct ib_device *ibdev = queue->device->dev;
1793 nvme_rdma_unmap_data(queue, rq);
1794 ib_dma_unmap_single(ibdev, req->sqe.dma, sizeof(struct nvme_command),
1796 nvme_complete_rq(rq);
1799 static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
1801 struct nvme_rdma_ctrl *ctrl = set->driver_data;
1802 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
1804 if (opts->nr_write_queues && ctrl->io_queues[HCTX_TYPE_READ]) {
1805 /* separate read/write queues */
1806 set->map[HCTX_TYPE_DEFAULT].nr_queues =
1807 ctrl->io_queues[HCTX_TYPE_DEFAULT];
1808 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
1809 set->map[HCTX_TYPE_READ].nr_queues =
1810 ctrl->io_queues[HCTX_TYPE_READ];
1811 set->map[HCTX_TYPE_READ].queue_offset =
1812 ctrl->io_queues[HCTX_TYPE_DEFAULT];
1814 /* shared read/write queues */
1815 set->map[HCTX_TYPE_DEFAULT].nr_queues =
1816 ctrl->io_queues[HCTX_TYPE_DEFAULT];
1817 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
1818 set->map[HCTX_TYPE_READ].nr_queues =
1819 ctrl->io_queues[HCTX_TYPE_DEFAULT];
1820 set->map[HCTX_TYPE_READ].queue_offset = 0;
1822 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_DEFAULT],
1823 ctrl->device->dev, 0);
1824 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_READ],
1825 ctrl->device->dev, 0);
1827 if (opts->nr_poll_queues && ctrl->io_queues[HCTX_TYPE_POLL]) {
1828 /* map dedicated poll queues only if we have queues left */
1829 set->map[HCTX_TYPE_POLL].nr_queues =
1830 ctrl->io_queues[HCTX_TYPE_POLL];
1831 set->map[HCTX_TYPE_POLL].queue_offset =
1832 ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1833 ctrl->io_queues[HCTX_TYPE_READ];
1834 blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]);
1837 dev_info(ctrl->ctrl.device,
1838 "mapped %d/%d/%d default/read/poll queues.\n",
1839 ctrl->io_queues[HCTX_TYPE_DEFAULT],
1840 ctrl->io_queues[HCTX_TYPE_READ],
1841 ctrl->io_queues[HCTX_TYPE_POLL]);
1846 static const struct blk_mq_ops nvme_rdma_mq_ops = {
1847 .queue_rq = nvme_rdma_queue_rq,
1848 .complete = nvme_rdma_complete_rq,
1849 .init_request = nvme_rdma_init_request,
1850 .exit_request = nvme_rdma_exit_request,
1851 .init_hctx = nvme_rdma_init_hctx,
1852 .timeout = nvme_rdma_timeout,
1853 .map_queues = nvme_rdma_map_queues,
1854 .poll = nvme_rdma_poll,
1857 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1858 .queue_rq = nvme_rdma_queue_rq,
1859 .complete = nvme_rdma_complete_rq,
1860 .init_request = nvme_rdma_init_request,
1861 .exit_request = nvme_rdma_exit_request,
1862 .init_hctx = nvme_rdma_init_admin_hctx,
1863 .timeout = nvme_rdma_timeout,
1866 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1868 cancel_work_sync(&ctrl->err_work);
1869 cancel_delayed_work_sync(&ctrl->reconnect_work);
1871 nvme_rdma_teardown_io_queues(ctrl, shutdown);
1873 nvme_shutdown_ctrl(&ctrl->ctrl);
1875 nvme_disable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
1876 nvme_rdma_teardown_admin_queue(ctrl, shutdown);
1879 static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
1881 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true);
1884 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1886 struct nvme_rdma_ctrl *ctrl =
1887 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
1889 nvme_stop_ctrl(&ctrl->ctrl);
1890 nvme_rdma_shutdown_ctrl(ctrl, false);
1892 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
1893 /* state change failure should never happen */
1898 if (nvme_rdma_setup_ctrl(ctrl, false))
1904 ++ctrl->ctrl.nr_reconnects;
1905 nvme_rdma_reconnect_or_remove(ctrl);
1908 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1910 .module = THIS_MODULE,
1911 .flags = NVME_F_FABRICS,
1912 .reg_read32 = nvmf_reg_read32,
1913 .reg_read64 = nvmf_reg_read64,
1914 .reg_write32 = nvmf_reg_write32,
1915 .free_ctrl = nvme_rdma_free_ctrl,
1916 .submit_async_event = nvme_rdma_submit_async_event,
1917 .delete_ctrl = nvme_rdma_delete_ctrl,
1918 .get_address = nvmf_get_address,
1922 * Fails a connection request if it matches an existing controller
1923 * (association) with the same tuple:
1924 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
1926 * if local address is not specified in the request, it will match an
1927 * existing controller with all the other parameters the same and no
1928 * local port address specified as well.
1930 * The ports don't need to be compared as they are intrinsically
1931 * already matched by the port pointers supplied.
1934 nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts)
1936 struct nvme_rdma_ctrl *ctrl;
1939 mutex_lock(&nvme_rdma_ctrl_mutex);
1940 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
1941 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
1945 mutex_unlock(&nvme_rdma_ctrl_mutex);
1950 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1951 struct nvmf_ctrl_options *opts)
1953 struct nvme_rdma_ctrl *ctrl;
1957 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1959 return ERR_PTR(-ENOMEM);
1960 ctrl->ctrl.opts = opts;
1961 INIT_LIST_HEAD(&ctrl->list);
1963 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
1965 kstrdup(__stringify(NVME_RDMA_IP_PORT), GFP_KERNEL);
1966 if (!opts->trsvcid) {
1970 opts->mask |= NVMF_OPT_TRSVCID;
1973 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1974 opts->traddr, opts->trsvcid, &ctrl->addr);
1976 pr_err("malformed address passed: %s:%s\n",
1977 opts->traddr, opts->trsvcid);
1981 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
1982 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1983 opts->host_traddr, NULL, &ctrl->src_addr);
1985 pr_err("malformed src address passed: %s\n",
1991 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) {
1996 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1997 nvme_rdma_reconnect_ctrl_work);
1998 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1999 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
2001 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
2002 opts->nr_poll_queues + 1;
2003 ctrl->ctrl.sqsize = opts->queue_size - 1;
2004 ctrl->ctrl.kato = opts->kato;
2007 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
2012 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
2013 0 /* no quirks, we're perfect! */);
2015 goto out_kfree_queues;
2017 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING);
2018 WARN_ON_ONCE(!changed);
2020 ret = nvme_rdma_setup_ctrl(ctrl, true);
2022 goto out_uninit_ctrl;
2024 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
2025 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2027 nvme_get_ctrl(&ctrl->ctrl);
2029 mutex_lock(&nvme_rdma_ctrl_mutex);
2030 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
2031 mutex_unlock(&nvme_rdma_ctrl_mutex);
2036 nvme_uninit_ctrl(&ctrl->ctrl);
2037 nvme_put_ctrl(&ctrl->ctrl);
2040 return ERR_PTR(ret);
2042 kfree(ctrl->queues);
2045 return ERR_PTR(ret);
2048 static struct nvmf_transport_ops nvme_rdma_transport = {
2050 .module = THIS_MODULE,
2051 .required_opts = NVMF_OPT_TRADDR,
2052 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2053 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
2054 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES,
2055 .create_ctrl = nvme_rdma_create_ctrl,
2058 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2060 struct nvme_rdma_ctrl *ctrl;
2061 struct nvme_rdma_device *ndev;
2064 mutex_lock(&device_list_mutex);
2065 list_for_each_entry(ndev, &device_list, entry) {
2066 if (ndev->dev == ib_device) {
2071 mutex_unlock(&device_list_mutex);
2076 /* Delete all controllers using this device */
2077 mutex_lock(&nvme_rdma_ctrl_mutex);
2078 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2079 if (ctrl->device->dev != ib_device)
2081 nvme_delete_ctrl(&ctrl->ctrl);
2083 mutex_unlock(&nvme_rdma_ctrl_mutex);
2085 flush_workqueue(nvme_delete_wq);
2088 static struct ib_client nvme_rdma_ib_client = {
2089 .name = "nvme_rdma",
2090 .remove = nvme_rdma_remove_one
2093 static int __init nvme_rdma_init_module(void)
2097 ret = ib_register_client(&nvme_rdma_ib_client);
2101 ret = nvmf_register_transport(&nvme_rdma_transport);
2103 goto err_unreg_client;
2108 ib_unregister_client(&nvme_rdma_ib_client);
2112 static void __exit nvme_rdma_cleanup_module(void)
2114 nvmf_unregister_transport(&nvme_rdma_transport);
2115 ib_unregister_client(&nvme_rdma_ib_client);
2118 module_init(nvme_rdma_init_module);
2119 module_exit(nvme_rdma_cleanup_module);
2121 MODULE_LICENSE("GPL v2");