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 #define NVME_RDMA_DATA_SGL_SIZE \
38 (sizeof(struct scatterlist) * NVME_INLINE_SG_CNT)
39 #define NVME_RDMA_METADATA_SGL_SIZE \
40 (sizeof(struct scatterlist) * NVME_INLINE_METADATA_SG_CNT)
42 struct nvme_rdma_device {
43 struct ib_device *dev;
46 struct list_head entry;
47 unsigned int num_inline_segments;
56 struct nvme_rdma_sgl {
58 struct sg_table sg_table;
61 struct nvme_rdma_queue;
62 struct nvme_rdma_request {
63 struct nvme_request req;
65 struct nvme_rdma_qe sqe;
66 union nvme_result result;
69 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
71 struct ib_reg_wr reg_wr;
72 struct ib_cqe reg_cqe;
73 struct nvme_rdma_queue *queue;
74 struct nvme_rdma_sgl data_sgl;
75 struct nvme_rdma_sgl *metadata_sgl;
79 enum nvme_rdma_queue_flags {
80 NVME_RDMA_Q_ALLOCATED = 0,
82 NVME_RDMA_Q_TR_READY = 2,
85 struct nvme_rdma_queue {
86 struct nvme_rdma_qe *rsp_ring;
88 size_t cmnd_capsule_len;
89 struct nvme_rdma_ctrl *ctrl;
90 struct nvme_rdma_device *device;
95 struct rdma_cm_id *cm_id;
97 struct completion cm_done;
102 struct nvme_rdma_ctrl {
103 /* read only in the hot path */
104 struct nvme_rdma_queue *queues;
106 /* other member variables */
107 struct blk_mq_tag_set tag_set;
108 struct work_struct err_work;
110 struct nvme_rdma_qe async_event_sqe;
112 struct delayed_work reconnect_work;
114 struct list_head list;
116 struct blk_mq_tag_set admin_tag_set;
117 struct nvme_rdma_device *device;
121 struct sockaddr_storage addr;
122 struct sockaddr_storage src_addr;
124 struct nvme_ctrl ctrl;
125 bool use_inline_data;
126 u32 io_queues[HCTX_MAX_TYPES];
129 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
131 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
134 static LIST_HEAD(device_list);
135 static DEFINE_MUTEX(device_list_mutex);
137 static LIST_HEAD(nvme_rdma_ctrl_list);
138 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
141 * Disabling this option makes small I/O goes faster, but is fundamentally
142 * unsafe. With it turned off we will have to register a global rkey that
143 * allows read and write access to all physical memory.
145 static bool register_always = true;
146 module_param(register_always, bool, 0444);
147 MODULE_PARM_DESC(register_always,
148 "Use memory registration even for contiguous memory regions");
150 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
151 struct rdma_cm_event *event);
152 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
153 static void nvme_rdma_complete_rq(struct request *rq);
155 static const struct blk_mq_ops nvme_rdma_mq_ops;
156 static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
158 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
160 return queue - queue->ctrl->queues;
163 static bool nvme_rdma_poll_queue(struct nvme_rdma_queue *queue)
165 return nvme_rdma_queue_idx(queue) >
166 queue->ctrl->io_queues[HCTX_TYPE_DEFAULT] +
167 queue->ctrl->io_queues[HCTX_TYPE_READ];
170 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
172 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
175 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
176 size_t capsule_size, enum dma_data_direction dir)
178 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
182 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
183 size_t capsule_size, enum dma_data_direction dir)
185 qe->data = kzalloc(capsule_size, GFP_KERNEL);
189 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
190 if (ib_dma_mapping_error(ibdev, qe->dma)) {
199 static void nvme_rdma_free_ring(struct ib_device *ibdev,
200 struct nvme_rdma_qe *ring, size_t ib_queue_size,
201 size_t capsule_size, enum dma_data_direction dir)
205 for (i = 0; i < ib_queue_size; i++)
206 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
210 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
211 size_t ib_queue_size, size_t capsule_size,
212 enum dma_data_direction dir)
214 struct nvme_rdma_qe *ring;
217 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
222 * Bind the CQEs (post recv buffers) DMA mapping to the RDMA queue
223 * lifetime. It's safe, since any chage in the underlying RDMA device
224 * will issue error recovery and queue re-creation.
226 for (i = 0; i < ib_queue_size; i++) {
227 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
234 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
238 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
240 pr_debug("QP event %s (%d)\n",
241 ib_event_msg(event->event), event->event);
245 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
249 ret = wait_for_completion_interruptible_timeout(&queue->cm_done,
250 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
255 WARN_ON_ONCE(queue->cm_error > 0);
256 return queue->cm_error;
259 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
261 struct nvme_rdma_device *dev = queue->device;
262 struct ib_qp_init_attr init_attr;
265 memset(&init_attr, 0, sizeof(init_attr));
266 init_attr.event_handler = nvme_rdma_qp_event;
268 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
270 init_attr.cap.max_recv_wr = queue->queue_size + 1;
271 init_attr.cap.max_recv_sge = 1;
272 init_attr.cap.max_send_sge = 1 + dev->num_inline_segments;
273 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
274 init_attr.qp_type = IB_QPT_RC;
275 init_attr.send_cq = queue->ib_cq;
276 init_attr.recv_cq = queue->ib_cq;
277 if (queue->pi_support)
278 init_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN;
279 init_attr.qp_context = queue;
281 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
283 queue->qp = queue->cm_id->qp;
287 static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
288 struct request *rq, unsigned int hctx_idx)
290 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
292 kfree(req->sqe.data);
295 static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
296 struct request *rq, unsigned int hctx_idx,
297 unsigned int numa_node)
299 struct nvme_rdma_ctrl *ctrl = set->driver_data;
300 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
301 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
302 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
304 nvme_req(rq)->ctrl = &ctrl->ctrl;
305 req->sqe.data = kzalloc(sizeof(struct nvme_command), GFP_KERNEL);
309 /* metadata nvme_rdma_sgl struct is located after command's data SGL */
310 if (queue->pi_support)
311 req->metadata_sgl = (void *)nvme_req(rq) +
312 sizeof(struct nvme_rdma_request) +
313 NVME_RDMA_DATA_SGL_SIZE;
320 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
321 unsigned int hctx_idx)
323 struct nvme_rdma_ctrl *ctrl = data;
324 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
326 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
328 hctx->driver_data = queue;
332 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
333 unsigned int hctx_idx)
335 struct nvme_rdma_ctrl *ctrl = data;
336 struct nvme_rdma_queue *queue = &ctrl->queues[0];
338 BUG_ON(hctx_idx != 0);
340 hctx->driver_data = queue;
344 static void nvme_rdma_free_dev(struct kref *ref)
346 struct nvme_rdma_device *ndev =
347 container_of(ref, struct nvme_rdma_device, ref);
349 mutex_lock(&device_list_mutex);
350 list_del(&ndev->entry);
351 mutex_unlock(&device_list_mutex);
353 ib_dealloc_pd(ndev->pd);
357 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
359 kref_put(&dev->ref, nvme_rdma_free_dev);
362 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
364 return kref_get_unless_zero(&dev->ref);
367 static struct nvme_rdma_device *
368 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
370 struct nvme_rdma_device *ndev;
372 mutex_lock(&device_list_mutex);
373 list_for_each_entry(ndev, &device_list, entry) {
374 if (ndev->dev->node_guid == cm_id->device->node_guid &&
375 nvme_rdma_dev_get(ndev))
379 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
383 ndev->dev = cm_id->device;
384 kref_init(&ndev->ref);
386 ndev->pd = ib_alloc_pd(ndev->dev,
387 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
388 if (IS_ERR(ndev->pd))
391 if (!(ndev->dev->attrs.device_cap_flags &
392 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
393 dev_err(&ndev->dev->dev,
394 "Memory registrations not supported.\n");
398 ndev->num_inline_segments = min(NVME_RDMA_MAX_INLINE_SEGMENTS,
399 ndev->dev->attrs.max_send_sge - 1);
400 list_add(&ndev->entry, &device_list);
402 mutex_unlock(&device_list_mutex);
406 ib_dealloc_pd(ndev->pd);
410 mutex_unlock(&device_list_mutex);
414 static void nvme_rdma_free_cq(struct nvme_rdma_queue *queue)
416 if (nvme_rdma_poll_queue(queue))
417 ib_free_cq(queue->ib_cq);
419 ib_cq_pool_put(queue->ib_cq, queue->cq_size);
422 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
424 struct nvme_rdma_device *dev;
425 struct ib_device *ibdev;
427 if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags))
433 if (queue->pi_support)
434 ib_mr_pool_destroy(queue->qp, &queue->qp->sig_mrs);
435 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
438 * The cm_id object might have been destroyed during RDMA connection
439 * establishment error flow to avoid getting other cma events, thus
440 * the destruction of the QP shouldn't use rdma_cm API.
442 ib_destroy_qp(queue->qp);
443 nvme_rdma_free_cq(queue);
445 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
446 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
448 nvme_rdma_dev_put(dev);
451 static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev, bool pi_support)
453 u32 max_page_list_len;
456 max_page_list_len = ibdev->attrs.max_pi_fast_reg_page_list_len;
458 max_page_list_len = ibdev->attrs.max_fast_reg_page_list_len;
460 return min_t(u32, NVME_RDMA_MAX_SEGMENTS, max_page_list_len - 1);
463 static int nvme_rdma_create_cq(struct ib_device *ibdev,
464 struct nvme_rdma_queue *queue)
466 int ret, comp_vector, idx = nvme_rdma_queue_idx(queue);
467 enum ib_poll_context poll_ctx;
470 * Spread I/O queues completion vectors according their queue index.
471 * Admin queues can always go on completion vector 0.
473 comp_vector = (idx == 0 ? idx : idx - 1) % ibdev->num_comp_vectors;
475 /* Polling queues need direct cq polling context */
476 if (nvme_rdma_poll_queue(queue)) {
477 poll_ctx = IB_POLL_DIRECT;
478 queue->ib_cq = ib_alloc_cq(ibdev, queue, queue->cq_size,
479 comp_vector, poll_ctx);
481 poll_ctx = IB_POLL_SOFTIRQ;
482 queue->ib_cq = ib_cq_pool_get(ibdev, queue->cq_size,
483 comp_vector, poll_ctx);
486 if (IS_ERR(queue->ib_cq)) {
487 ret = PTR_ERR(queue->ib_cq);
494 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
496 struct ib_device *ibdev;
497 const int send_wr_factor = 3; /* MR, SEND, INV */
498 const int cq_factor = send_wr_factor + 1; /* + RECV */
499 int ret, pages_per_mr;
501 queue->device = nvme_rdma_find_get_device(queue->cm_id);
502 if (!queue->device) {
503 dev_err(queue->cm_id->device->dev.parent,
504 "no client data found!\n");
505 return -ECONNREFUSED;
507 ibdev = queue->device->dev;
509 /* +1 for ib_stop_cq */
510 queue->cq_size = cq_factor * queue->queue_size + 1;
512 ret = nvme_rdma_create_cq(ibdev, queue);
516 ret = nvme_rdma_create_qp(queue, send_wr_factor);
518 goto out_destroy_ib_cq;
520 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
521 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
522 if (!queue->rsp_ring) {
528 * Currently we don't use SG_GAPS MR's so if the first entry is
529 * misaligned we'll end up using two entries for a single data page,
530 * so one additional entry is required.
532 pages_per_mr = nvme_rdma_get_max_fr_pages(ibdev, queue->pi_support) + 1;
533 ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs,
538 dev_err(queue->ctrl->ctrl.device,
539 "failed to initialize MR pool sized %d for QID %d\n",
540 queue->queue_size, nvme_rdma_queue_idx(queue));
541 goto out_destroy_ring;
544 if (queue->pi_support) {
545 ret = ib_mr_pool_init(queue->qp, &queue->qp->sig_mrs,
546 queue->queue_size, IB_MR_TYPE_INTEGRITY,
547 pages_per_mr, pages_per_mr);
549 dev_err(queue->ctrl->ctrl.device,
550 "failed to initialize PI MR pool sized %d for QID %d\n",
551 queue->queue_size, nvme_rdma_queue_idx(queue));
552 goto out_destroy_mr_pool;
556 set_bit(NVME_RDMA_Q_TR_READY, &queue->flags);
561 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
563 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
564 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
566 rdma_destroy_qp(queue->cm_id);
568 nvme_rdma_free_cq(queue);
570 nvme_rdma_dev_put(queue->device);
574 static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
575 int idx, size_t queue_size)
577 struct nvme_rdma_queue *queue;
578 struct sockaddr *src_addr = NULL;
581 queue = &ctrl->queues[idx];
583 if (idx && ctrl->ctrl.max_integrity_segments)
584 queue->pi_support = true;
586 queue->pi_support = false;
587 init_completion(&queue->cm_done);
590 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
592 queue->cmnd_capsule_len = sizeof(struct nvme_command);
594 queue->queue_size = queue_size;
596 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
597 RDMA_PS_TCP, IB_QPT_RC);
598 if (IS_ERR(queue->cm_id)) {
599 dev_info(ctrl->ctrl.device,
600 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
601 return PTR_ERR(queue->cm_id);
604 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
605 src_addr = (struct sockaddr *)&ctrl->src_addr;
607 queue->cm_error = -ETIMEDOUT;
608 ret = rdma_resolve_addr(queue->cm_id, src_addr,
609 (struct sockaddr *)&ctrl->addr,
610 NVME_RDMA_CONNECT_TIMEOUT_MS);
612 dev_info(ctrl->ctrl.device,
613 "rdma_resolve_addr failed (%d).\n", ret);
614 goto out_destroy_cm_id;
617 ret = nvme_rdma_wait_for_cm(queue);
619 dev_info(ctrl->ctrl.device,
620 "rdma connection establishment failed (%d)\n", ret);
621 goto out_destroy_cm_id;
624 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
629 rdma_destroy_id(queue->cm_id);
630 nvme_rdma_destroy_queue_ib(queue);
634 static void __nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
636 rdma_disconnect(queue->cm_id);
637 ib_drain_qp(queue->qp);
640 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
642 if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
644 __nvme_rdma_stop_queue(queue);
647 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
649 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
652 nvme_rdma_destroy_queue_ib(queue);
653 rdma_destroy_id(queue->cm_id);
656 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
660 for (i = 1; i < ctrl->ctrl.queue_count; i++)
661 nvme_rdma_free_queue(&ctrl->queues[i]);
664 static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
668 for (i = 1; i < ctrl->ctrl.queue_count; i++)
669 nvme_rdma_stop_queue(&ctrl->queues[i]);
672 static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
674 struct nvme_rdma_queue *queue = &ctrl->queues[idx];
675 bool poll = nvme_rdma_poll_queue(queue);
679 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx, poll);
681 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
684 set_bit(NVME_RDMA_Q_LIVE, &queue->flags);
686 if (test_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
687 __nvme_rdma_stop_queue(queue);
688 dev_info(ctrl->ctrl.device,
689 "failed to connect queue: %d ret=%d\n", idx, ret);
694 static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
698 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
699 ret = nvme_rdma_start_queue(ctrl, i);
701 goto out_stop_queues;
707 for (i--; i >= 1; i--)
708 nvme_rdma_stop_queue(&ctrl->queues[i]);
712 static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
714 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
715 struct ib_device *ibdev = ctrl->device->dev;
716 unsigned int nr_io_queues, nr_default_queues;
717 unsigned int nr_read_queues, nr_poll_queues;
720 nr_read_queues = min_t(unsigned int, ibdev->num_comp_vectors,
721 min(opts->nr_io_queues, num_online_cpus()));
722 nr_default_queues = min_t(unsigned int, ibdev->num_comp_vectors,
723 min(opts->nr_write_queues, num_online_cpus()));
724 nr_poll_queues = min(opts->nr_poll_queues, num_online_cpus());
725 nr_io_queues = nr_read_queues + nr_default_queues + nr_poll_queues;
727 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
731 ctrl->ctrl.queue_count = nr_io_queues + 1;
732 if (ctrl->ctrl.queue_count < 2)
735 dev_info(ctrl->ctrl.device,
736 "creating %d I/O queues.\n", nr_io_queues);
738 if (opts->nr_write_queues && nr_read_queues < nr_io_queues) {
740 * separate read/write queues
741 * hand out dedicated default queues only after we have
742 * sufficient read queues.
744 ctrl->io_queues[HCTX_TYPE_READ] = nr_read_queues;
745 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_READ];
746 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
747 min(nr_default_queues, nr_io_queues);
748 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
751 * shared read/write queues
752 * either no write queues were requested, or we don't have
753 * sufficient queue count to have dedicated default queues.
755 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
756 min(nr_read_queues, nr_io_queues);
757 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
760 if (opts->nr_poll_queues && nr_io_queues) {
761 /* map dedicated poll queues only if we have queues left */
762 ctrl->io_queues[HCTX_TYPE_POLL] =
763 min(nr_poll_queues, nr_io_queues);
766 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
767 ret = nvme_rdma_alloc_queue(ctrl, i,
768 ctrl->ctrl.sqsize + 1);
770 goto out_free_queues;
776 for (i--; i >= 1; i--)
777 nvme_rdma_free_queue(&ctrl->queues[i]);
782 static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
785 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
786 struct blk_mq_tag_set *set;
790 set = &ctrl->admin_tag_set;
791 memset(set, 0, sizeof(*set));
792 set->ops = &nvme_rdma_admin_mq_ops;
793 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
794 set->reserved_tags = 2; /* connect + keep-alive */
795 set->numa_node = nctrl->numa_node;
796 set->cmd_size = sizeof(struct nvme_rdma_request) +
797 NVME_RDMA_DATA_SGL_SIZE;
798 set->driver_data = ctrl;
799 set->nr_hw_queues = 1;
800 set->timeout = ADMIN_TIMEOUT;
801 set->flags = BLK_MQ_F_NO_SCHED;
803 set = &ctrl->tag_set;
804 memset(set, 0, sizeof(*set));
805 set->ops = &nvme_rdma_mq_ops;
806 set->queue_depth = nctrl->sqsize + 1;
807 set->reserved_tags = 1; /* fabric connect */
808 set->numa_node = nctrl->numa_node;
809 set->flags = BLK_MQ_F_SHOULD_MERGE;
810 set->cmd_size = sizeof(struct nvme_rdma_request) +
811 NVME_RDMA_DATA_SGL_SIZE;
812 if (nctrl->max_integrity_segments)
813 set->cmd_size += sizeof(struct nvme_rdma_sgl) +
814 NVME_RDMA_METADATA_SGL_SIZE;
815 set->driver_data = ctrl;
816 set->nr_hw_queues = nctrl->queue_count - 1;
817 set->timeout = NVME_IO_TIMEOUT;
818 set->nr_maps = nctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2;
821 ret = blk_mq_alloc_tag_set(set);
828 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
832 blk_cleanup_queue(ctrl->ctrl.admin_q);
833 blk_cleanup_queue(ctrl->ctrl.fabrics_q);
834 blk_mq_free_tag_set(ctrl->ctrl.admin_tagset);
836 if (ctrl->async_event_sqe.data) {
837 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
838 sizeof(struct nvme_command), DMA_TO_DEVICE);
839 ctrl->async_event_sqe.data = NULL;
841 nvme_rdma_free_queue(&ctrl->queues[0]);
844 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
847 bool pi_capable = false;
850 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
854 ctrl->device = ctrl->queues[0].device;
855 ctrl->ctrl.numa_node = dev_to_node(ctrl->device->dev->dma_device);
858 if (ctrl->device->dev->attrs.device_cap_flags &
859 IB_DEVICE_INTEGRITY_HANDOVER)
862 ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev,
866 * Bind the async event SQE DMA mapping to the admin queue lifetime.
867 * It's safe, since any chage in the underlying RDMA device will issue
868 * error recovery and queue re-creation.
870 error = nvme_rdma_alloc_qe(ctrl->device->dev, &ctrl->async_event_sqe,
871 sizeof(struct nvme_command), DMA_TO_DEVICE);
876 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
877 if (IS_ERR(ctrl->ctrl.admin_tagset)) {
878 error = PTR_ERR(ctrl->ctrl.admin_tagset);
879 goto out_free_async_qe;
882 ctrl->ctrl.fabrics_q = blk_mq_init_queue(&ctrl->admin_tag_set);
883 if (IS_ERR(ctrl->ctrl.fabrics_q)) {
884 error = PTR_ERR(ctrl->ctrl.fabrics_q);
885 goto out_free_tagset;
888 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
889 if (IS_ERR(ctrl->ctrl.admin_q)) {
890 error = PTR_ERR(ctrl->ctrl.admin_q);
891 goto out_cleanup_fabrics_q;
895 error = nvme_rdma_start_queue(ctrl, 0);
897 goto out_cleanup_queue;
899 error = nvme_enable_ctrl(&ctrl->ctrl);
903 ctrl->ctrl.max_segments = ctrl->max_fr_pages;
904 ctrl->ctrl.max_hw_sectors = ctrl->max_fr_pages << (ilog2(SZ_4K) - 9);
906 ctrl->ctrl.max_integrity_segments = ctrl->max_fr_pages;
908 ctrl->ctrl.max_integrity_segments = 0;
910 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
912 error = nvme_init_identify(&ctrl->ctrl);
919 nvme_rdma_stop_queue(&ctrl->queues[0]);
922 blk_cleanup_queue(ctrl->ctrl.admin_q);
923 out_cleanup_fabrics_q:
925 blk_cleanup_queue(ctrl->ctrl.fabrics_q);
928 blk_mq_free_tag_set(ctrl->ctrl.admin_tagset);
930 if (ctrl->async_event_sqe.data) {
931 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
932 sizeof(struct nvme_command), DMA_TO_DEVICE);
933 ctrl->async_event_sqe.data = NULL;
936 nvme_rdma_free_queue(&ctrl->queues[0]);
940 static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
944 blk_cleanup_queue(ctrl->ctrl.connect_q);
945 blk_mq_free_tag_set(ctrl->ctrl.tagset);
947 nvme_rdma_free_io_queues(ctrl);
950 static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
954 ret = nvme_rdma_alloc_io_queues(ctrl);
959 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
960 if (IS_ERR(ctrl->ctrl.tagset)) {
961 ret = PTR_ERR(ctrl->ctrl.tagset);
962 goto out_free_io_queues;
965 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
966 if (IS_ERR(ctrl->ctrl.connect_q)) {
967 ret = PTR_ERR(ctrl->ctrl.connect_q);
968 goto out_free_tag_set;
972 ret = nvme_rdma_start_io_queues(ctrl);
974 goto out_cleanup_connect_q;
977 nvme_start_queues(&ctrl->ctrl);
978 nvme_wait_freeze(&ctrl->ctrl);
979 blk_mq_update_nr_hw_queues(ctrl->ctrl.tagset,
980 ctrl->ctrl.queue_count - 1);
981 nvme_unfreeze(&ctrl->ctrl);
986 out_cleanup_connect_q:
988 blk_cleanup_queue(ctrl->ctrl.connect_q);
991 blk_mq_free_tag_set(ctrl->ctrl.tagset);
993 nvme_rdma_free_io_queues(ctrl);
997 static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl *ctrl,
1000 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
1001 nvme_rdma_stop_queue(&ctrl->queues[0]);
1002 if (ctrl->ctrl.admin_tagset) {
1003 blk_mq_tagset_busy_iter(ctrl->ctrl.admin_tagset,
1004 nvme_cancel_request, &ctrl->ctrl);
1005 blk_mq_tagset_wait_completed_request(ctrl->ctrl.admin_tagset);
1008 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
1009 nvme_rdma_destroy_admin_queue(ctrl, remove);
1012 static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl *ctrl,
1015 if (ctrl->ctrl.queue_count > 1) {
1016 nvme_start_freeze(&ctrl->ctrl);
1017 nvme_stop_queues(&ctrl->ctrl);
1018 nvme_rdma_stop_io_queues(ctrl);
1019 if (ctrl->ctrl.tagset) {
1020 blk_mq_tagset_busy_iter(ctrl->ctrl.tagset,
1021 nvme_cancel_request, &ctrl->ctrl);
1022 blk_mq_tagset_wait_completed_request(ctrl->ctrl.tagset);
1025 nvme_start_queues(&ctrl->ctrl);
1026 nvme_rdma_destroy_io_queues(ctrl, remove);
1030 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
1032 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1034 if (list_empty(&ctrl->list))
1037 mutex_lock(&nvme_rdma_ctrl_mutex);
1038 list_del(&ctrl->list);
1039 mutex_unlock(&nvme_rdma_ctrl_mutex);
1041 nvmf_free_options(nctrl->opts);
1043 kfree(ctrl->queues);
1047 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
1049 /* If we are resetting/deleting then do nothing */
1050 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
1051 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
1052 ctrl->ctrl.state == NVME_CTRL_LIVE);
1056 if (nvmf_should_reconnect(&ctrl->ctrl)) {
1057 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
1058 ctrl->ctrl.opts->reconnect_delay);
1059 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
1060 ctrl->ctrl.opts->reconnect_delay * HZ);
1062 nvme_delete_ctrl(&ctrl->ctrl);
1066 static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new)
1071 ret = nvme_rdma_configure_admin_queue(ctrl, new);
1075 if (ctrl->ctrl.icdoff) {
1076 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1080 if (!(ctrl->ctrl.sgls & (1 << 2))) {
1081 dev_err(ctrl->ctrl.device,
1082 "Mandatory keyed sgls are not supported!\n");
1086 if (ctrl->ctrl.opts->queue_size > ctrl->ctrl.sqsize + 1) {
1087 dev_warn(ctrl->ctrl.device,
1088 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1089 ctrl->ctrl.opts->queue_size, ctrl->ctrl.sqsize + 1);
1092 if (ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) {
1093 dev_warn(ctrl->ctrl.device,
1094 "sqsize %u > ctrl maxcmd %u, clamping down\n",
1095 ctrl->ctrl.sqsize + 1, ctrl->ctrl.maxcmd);
1096 ctrl->ctrl.sqsize = ctrl->ctrl.maxcmd - 1;
1099 if (ctrl->ctrl.sgls & (1 << 20))
1100 ctrl->use_inline_data = true;
1102 if (ctrl->ctrl.queue_count > 1) {
1103 ret = nvme_rdma_configure_io_queues(ctrl, new);
1108 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1111 * state change failure is ok if we started ctrl delete,
1112 * unless we're during creation of a new controller to
1113 * avoid races with teardown flow.
1115 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING &&
1116 ctrl->ctrl.state != NVME_CTRL_DELETING_NOIO);
1122 nvme_start_ctrl(&ctrl->ctrl);
1126 if (ctrl->ctrl.queue_count > 1)
1127 nvme_rdma_destroy_io_queues(ctrl, new);
1129 nvme_rdma_stop_queue(&ctrl->queues[0]);
1130 nvme_rdma_destroy_admin_queue(ctrl, new);
1134 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
1136 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
1137 struct nvme_rdma_ctrl, reconnect_work);
1139 ++ctrl->ctrl.nr_reconnects;
1141 if (nvme_rdma_setup_ctrl(ctrl, false))
1144 dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
1145 ctrl->ctrl.nr_reconnects);
1147 ctrl->ctrl.nr_reconnects = 0;
1152 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
1153 ctrl->ctrl.nr_reconnects);
1154 nvme_rdma_reconnect_or_remove(ctrl);
1157 static void nvme_rdma_error_recovery_work(struct work_struct *work)
1159 struct nvme_rdma_ctrl *ctrl = container_of(work,
1160 struct nvme_rdma_ctrl, err_work);
1162 nvme_stop_keep_alive(&ctrl->ctrl);
1163 nvme_rdma_teardown_io_queues(ctrl, false);
1164 nvme_start_queues(&ctrl->ctrl);
1165 nvme_rdma_teardown_admin_queue(ctrl, false);
1166 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
1168 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
1169 /* state change failure is ok if we started ctrl delete */
1170 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING &&
1171 ctrl->ctrl.state != NVME_CTRL_DELETING_NOIO);
1175 nvme_rdma_reconnect_or_remove(ctrl);
1178 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
1180 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1183 queue_work(nvme_reset_wq, &ctrl->err_work);
1186 static void nvme_rdma_end_request(struct nvme_rdma_request *req)
1188 struct request *rq = blk_mq_rq_from_pdu(req);
1190 if (!refcount_dec_and_test(&req->ref))
1192 if (!nvme_end_request(rq, req->status, req->result))
1193 nvme_rdma_complete_rq(rq);
1196 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1199 struct nvme_rdma_queue *queue = wc->qp->qp_context;
1200 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1202 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1203 dev_info(ctrl->ctrl.device,
1204 "%s for CQE 0x%p failed with status %s (%d)\n",
1206 ib_wc_status_msg(wc->status), wc->status);
1207 nvme_rdma_error_recovery(ctrl);
1210 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1212 if (unlikely(wc->status != IB_WC_SUCCESS))
1213 nvme_rdma_wr_error(cq, wc, "MEMREG");
1216 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1218 struct nvme_rdma_request *req =
1219 container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe);
1221 if (unlikely(wc->status != IB_WC_SUCCESS))
1222 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
1224 nvme_rdma_end_request(req);
1227 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1228 struct nvme_rdma_request *req)
1230 struct ib_send_wr wr = {
1231 .opcode = IB_WR_LOCAL_INV,
1234 .send_flags = IB_SEND_SIGNALED,
1235 .ex.invalidate_rkey = req->mr->rkey,
1238 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1239 wr.wr_cqe = &req->reg_cqe;
1241 return ib_post_send(queue->qp, &wr, NULL);
1244 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1247 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1248 struct nvme_rdma_device *dev = queue->device;
1249 struct ib_device *ibdev = dev->dev;
1250 struct list_head *pool = &queue->qp->rdma_mrs;
1252 if (!blk_rq_nr_phys_segments(rq))
1255 if (blk_integrity_rq(rq)) {
1256 ib_dma_unmap_sg(ibdev, req->metadata_sgl->sg_table.sgl,
1257 req->metadata_sgl->nents, rq_dma_dir(rq));
1258 sg_free_table_chained(&req->metadata_sgl->sg_table,
1259 NVME_INLINE_METADATA_SG_CNT);
1262 if (req->use_sig_mr)
1263 pool = &queue->qp->sig_mrs;
1266 ib_mr_pool_put(queue->qp, pool, req->mr);
1270 ib_dma_unmap_sg(ibdev, req->data_sgl.sg_table.sgl, req->data_sgl.nents,
1272 sg_free_table_chained(&req->data_sgl.sg_table, NVME_INLINE_SG_CNT);
1275 static int nvme_rdma_set_sg_null(struct nvme_command *c)
1277 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1280 put_unaligned_le24(0, sg->length);
1281 put_unaligned_le32(0, sg->key);
1282 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1286 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
1287 struct nvme_rdma_request *req, struct nvme_command *c,
1290 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1291 struct scatterlist *sgl = req->data_sgl.sg_table.sgl;
1292 struct ib_sge *sge = &req->sge[1];
1296 for (i = 0; i < count; i++, sgl++, sge++) {
1297 sge->addr = sg_dma_address(sgl);
1298 sge->length = sg_dma_len(sgl);
1299 sge->lkey = queue->device->pd->local_dma_lkey;
1303 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1304 sg->length = cpu_to_le32(len);
1305 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1307 req->num_sge += count;
1311 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1312 struct nvme_rdma_request *req, struct nvme_command *c)
1314 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1316 sg->addr = cpu_to_le64(sg_dma_address(req->data_sgl.sg_table.sgl));
1317 put_unaligned_le24(sg_dma_len(req->data_sgl.sg_table.sgl), sg->length);
1318 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
1319 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1323 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1324 struct nvme_rdma_request *req, struct nvme_command *c,
1327 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1330 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs);
1331 if (WARN_ON_ONCE(!req->mr))
1335 * Align the MR to a 4K page size to match the ctrl page size and
1336 * the block virtual boundary.
1338 nr = ib_map_mr_sg(req->mr, req->data_sgl.sg_table.sgl, count, NULL,
1340 if (unlikely(nr < count)) {
1341 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1348 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1350 req->reg_cqe.done = nvme_rdma_memreg_done;
1351 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1352 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1353 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1354 req->reg_wr.wr.num_sge = 0;
1355 req->reg_wr.mr = req->mr;
1356 req->reg_wr.key = req->mr->rkey;
1357 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1358 IB_ACCESS_REMOTE_READ |
1359 IB_ACCESS_REMOTE_WRITE;
1361 sg->addr = cpu_to_le64(req->mr->iova);
1362 put_unaligned_le24(req->mr->length, sg->length);
1363 put_unaligned_le32(req->mr->rkey, sg->key);
1364 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1365 NVME_SGL_FMT_INVALIDATE;
1370 static void nvme_rdma_set_sig_domain(struct blk_integrity *bi,
1371 struct nvme_command *cmd, struct ib_sig_domain *domain,
1372 u16 control, u8 pi_type)
1374 domain->sig_type = IB_SIG_TYPE_T10_DIF;
1375 domain->sig.dif.bg_type = IB_T10DIF_CRC;
1376 domain->sig.dif.pi_interval = 1 << bi->interval_exp;
1377 domain->sig.dif.ref_tag = le32_to_cpu(cmd->rw.reftag);
1378 if (control & NVME_RW_PRINFO_PRCHK_REF)
1379 domain->sig.dif.ref_remap = true;
1381 domain->sig.dif.app_tag = le16_to_cpu(cmd->rw.apptag);
1382 domain->sig.dif.apptag_check_mask = le16_to_cpu(cmd->rw.appmask);
1383 domain->sig.dif.app_escape = true;
1384 if (pi_type == NVME_NS_DPS_PI_TYPE3)
1385 domain->sig.dif.ref_escape = true;
1388 static void nvme_rdma_set_sig_attrs(struct blk_integrity *bi,
1389 struct nvme_command *cmd, struct ib_sig_attrs *sig_attrs,
1392 u16 control = le16_to_cpu(cmd->rw.control);
1394 memset(sig_attrs, 0, sizeof(*sig_attrs));
1395 if (control & NVME_RW_PRINFO_PRACT) {
1396 /* for WRITE_INSERT/READ_STRIP no memory domain */
1397 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
1398 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control,
1400 /* Clear the PRACT bit since HCA will generate/verify the PI */
1401 control &= ~NVME_RW_PRINFO_PRACT;
1402 cmd->rw.control = cpu_to_le16(control);
1404 /* for WRITE_PASS/READ_PASS both wire/memory domains exist */
1405 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control,
1407 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->mem, control,
1412 static void nvme_rdma_set_prot_checks(struct nvme_command *cmd, u8 *mask)
1415 if (le16_to_cpu(cmd->rw.control) & NVME_RW_PRINFO_PRCHK_REF)
1416 *mask |= IB_SIG_CHECK_REFTAG;
1417 if (le16_to_cpu(cmd->rw.control) & NVME_RW_PRINFO_PRCHK_GUARD)
1418 *mask |= IB_SIG_CHECK_GUARD;
1421 static void nvme_rdma_sig_done(struct ib_cq *cq, struct ib_wc *wc)
1423 if (unlikely(wc->status != IB_WC_SUCCESS))
1424 nvme_rdma_wr_error(cq, wc, "SIG");
1427 static int nvme_rdma_map_sg_pi(struct nvme_rdma_queue *queue,
1428 struct nvme_rdma_request *req, struct nvme_command *c,
1429 int count, int pi_count)
1431 struct nvme_rdma_sgl *sgl = &req->data_sgl;
1432 struct ib_reg_wr *wr = &req->reg_wr;
1433 struct request *rq = blk_mq_rq_from_pdu(req);
1434 struct nvme_ns *ns = rq->q->queuedata;
1435 struct bio *bio = rq->bio;
1436 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1439 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->sig_mrs);
1440 if (WARN_ON_ONCE(!req->mr))
1443 nr = ib_map_mr_sg_pi(req->mr, sgl->sg_table.sgl, count, NULL,
1444 req->metadata_sgl->sg_table.sgl, pi_count, NULL,
1449 nvme_rdma_set_sig_attrs(blk_get_integrity(bio->bi_disk), c,
1450 req->mr->sig_attrs, ns->pi_type);
1451 nvme_rdma_set_prot_checks(c, &req->mr->sig_attrs->check_mask);
1453 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1455 req->reg_cqe.done = nvme_rdma_sig_done;
1456 memset(wr, 0, sizeof(*wr));
1457 wr->wr.opcode = IB_WR_REG_MR_INTEGRITY;
1458 wr->wr.wr_cqe = &req->reg_cqe;
1460 wr->wr.send_flags = 0;
1462 wr->key = req->mr->rkey;
1463 wr->access = IB_ACCESS_LOCAL_WRITE |
1464 IB_ACCESS_REMOTE_READ |
1465 IB_ACCESS_REMOTE_WRITE;
1467 sg->addr = cpu_to_le64(req->mr->iova);
1468 put_unaligned_le24(req->mr->length, sg->length);
1469 put_unaligned_le32(req->mr->rkey, sg->key);
1470 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1475 ib_mr_pool_put(queue->qp, &queue->qp->sig_mrs, req->mr);
1482 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
1483 struct request *rq, struct nvme_command *c)
1485 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1486 struct nvme_rdma_device *dev = queue->device;
1487 struct ib_device *ibdev = dev->dev;
1492 refcount_set(&req->ref, 2); /* send and recv completions */
1494 c->common.flags |= NVME_CMD_SGL_METABUF;
1496 if (!blk_rq_nr_phys_segments(rq))
1497 return nvme_rdma_set_sg_null(c);
1499 req->data_sgl.sg_table.sgl = (struct scatterlist *)(req + 1);
1500 ret = sg_alloc_table_chained(&req->data_sgl.sg_table,
1501 blk_rq_nr_phys_segments(rq), req->data_sgl.sg_table.sgl,
1502 NVME_INLINE_SG_CNT);
1506 req->data_sgl.nents = blk_rq_map_sg(rq->q, rq,
1507 req->data_sgl.sg_table.sgl);
1509 count = ib_dma_map_sg(ibdev, req->data_sgl.sg_table.sgl,
1510 req->data_sgl.nents, rq_dma_dir(rq));
1511 if (unlikely(count <= 0)) {
1513 goto out_free_table;
1516 if (blk_integrity_rq(rq)) {
1517 req->metadata_sgl->sg_table.sgl =
1518 (struct scatterlist *)(req->metadata_sgl + 1);
1519 ret = sg_alloc_table_chained(&req->metadata_sgl->sg_table,
1520 blk_rq_count_integrity_sg(rq->q, rq->bio),
1521 req->metadata_sgl->sg_table.sgl,
1522 NVME_INLINE_METADATA_SG_CNT);
1523 if (unlikely(ret)) {
1528 req->metadata_sgl->nents = blk_rq_map_integrity_sg(rq->q,
1529 rq->bio, req->metadata_sgl->sg_table.sgl);
1530 pi_count = ib_dma_map_sg(ibdev,
1531 req->metadata_sgl->sg_table.sgl,
1532 req->metadata_sgl->nents,
1534 if (unlikely(pi_count <= 0)) {
1536 goto out_free_pi_table;
1540 if (req->use_sig_mr) {
1541 ret = nvme_rdma_map_sg_pi(queue, req, c, count, pi_count);
1545 if (count <= dev->num_inline_segments) {
1546 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1547 queue->ctrl->use_inline_data &&
1548 blk_rq_payload_bytes(rq) <=
1549 nvme_rdma_inline_data_size(queue)) {
1550 ret = nvme_rdma_map_sg_inline(queue, req, c, count);
1554 if (count == 1 && dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
1555 ret = nvme_rdma_map_sg_single(queue, req, c);
1560 ret = nvme_rdma_map_sg_fr(queue, req, c, count);
1563 goto out_unmap_pi_sg;
1568 if (blk_integrity_rq(rq))
1569 ib_dma_unmap_sg(ibdev, req->metadata_sgl->sg_table.sgl,
1570 req->metadata_sgl->nents, rq_dma_dir(rq));
1572 if (blk_integrity_rq(rq))
1573 sg_free_table_chained(&req->metadata_sgl->sg_table,
1574 NVME_INLINE_METADATA_SG_CNT);
1576 ib_dma_unmap_sg(ibdev, req->data_sgl.sg_table.sgl, req->data_sgl.nents,
1579 sg_free_table_chained(&req->data_sgl.sg_table, NVME_INLINE_SG_CNT);
1583 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1585 struct nvme_rdma_qe *qe =
1586 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1587 struct nvme_rdma_request *req =
1588 container_of(qe, struct nvme_rdma_request, sqe);
1590 if (unlikely(wc->status != IB_WC_SUCCESS))
1591 nvme_rdma_wr_error(cq, wc, "SEND");
1593 nvme_rdma_end_request(req);
1596 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1597 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1598 struct ib_send_wr *first)
1600 struct ib_send_wr wr;
1603 sge->addr = qe->dma;
1604 sge->length = sizeof(struct nvme_command);
1605 sge->lkey = queue->device->pd->local_dma_lkey;
1608 wr.wr_cqe = &qe->cqe;
1610 wr.num_sge = num_sge;
1611 wr.opcode = IB_WR_SEND;
1612 wr.send_flags = IB_SEND_SIGNALED;
1619 ret = ib_post_send(queue->qp, first, NULL);
1620 if (unlikely(ret)) {
1621 dev_err(queue->ctrl->ctrl.device,
1622 "%s failed with error code %d\n", __func__, ret);
1627 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1628 struct nvme_rdma_qe *qe)
1630 struct ib_recv_wr wr;
1634 list.addr = qe->dma;
1635 list.length = sizeof(struct nvme_completion);
1636 list.lkey = queue->device->pd->local_dma_lkey;
1638 qe->cqe.done = nvme_rdma_recv_done;
1641 wr.wr_cqe = &qe->cqe;
1645 ret = ib_post_recv(queue->qp, &wr, NULL);
1646 if (unlikely(ret)) {
1647 dev_err(queue->ctrl->ctrl.device,
1648 "%s failed with error code %d\n", __func__, ret);
1653 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1655 u32 queue_idx = nvme_rdma_queue_idx(queue);
1658 return queue->ctrl->admin_tag_set.tags[queue_idx];
1659 return queue->ctrl->tag_set.tags[queue_idx - 1];
1662 static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc)
1664 if (unlikely(wc->status != IB_WC_SUCCESS))
1665 nvme_rdma_wr_error(cq, wc, "ASYNC");
1668 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg)
1670 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1671 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1672 struct ib_device *dev = queue->device->dev;
1673 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1674 struct nvme_command *cmd = sqe->data;
1678 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1680 memset(cmd, 0, sizeof(*cmd));
1681 cmd->common.opcode = nvme_admin_async_event;
1682 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
1683 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1684 nvme_rdma_set_sg_null(cmd);
1686 sqe->cqe.done = nvme_rdma_async_done;
1688 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1691 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL);
1695 static void nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1696 struct nvme_completion *cqe, struct ib_wc *wc)
1699 struct nvme_rdma_request *req;
1701 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1703 dev_err(queue->ctrl->ctrl.device,
1704 "tag 0x%x on QP %#x not found\n",
1705 cqe->command_id, queue->qp->qp_num);
1706 nvme_rdma_error_recovery(queue->ctrl);
1709 req = blk_mq_rq_to_pdu(rq);
1711 req->status = cqe->status;
1712 req->result = cqe->result;
1714 if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
1715 if (unlikely(wc->ex.invalidate_rkey != req->mr->rkey)) {
1716 dev_err(queue->ctrl->ctrl.device,
1717 "Bogus remote invalidation for rkey %#x\n",
1719 nvme_rdma_error_recovery(queue->ctrl);
1721 } else if (req->mr) {
1724 ret = nvme_rdma_inv_rkey(queue, req);
1725 if (unlikely(ret < 0)) {
1726 dev_err(queue->ctrl->ctrl.device,
1727 "Queueing INV WR for rkey %#x failed (%d)\n",
1728 req->mr->rkey, ret);
1729 nvme_rdma_error_recovery(queue->ctrl);
1731 /* the local invalidation completion will end the request */
1735 nvme_rdma_end_request(req);
1738 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1740 struct nvme_rdma_qe *qe =
1741 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1742 struct nvme_rdma_queue *queue = wc->qp->qp_context;
1743 struct ib_device *ibdev = queue->device->dev;
1744 struct nvme_completion *cqe = qe->data;
1745 const size_t len = sizeof(struct nvme_completion);
1747 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1748 nvme_rdma_wr_error(cq, wc, "RECV");
1752 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1754 * AEN requests are special as they don't time out and can
1755 * survive any kind of queue freeze and often don't respond to
1756 * aborts. We don't even bother to allocate a struct request
1757 * for them but rather special case them here.
1759 if (unlikely(nvme_is_aen_req(nvme_rdma_queue_idx(queue),
1761 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1764 nvme_rdma_process_nvme_rsp(queue, cqe, wc);
1765 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1767 nvme_rdma_post_recv(queue, qe);
1770 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1774 for (i = 0; i < queue->queue_size; i++) {
1775 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1777 goto out_destroy_queue_ib;
1782 out_destroy_queue_ib:
1783 nvme_rdma_destroy_queue_ib(queue);
1787 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1788 struct rdma_cm_event *ev)
1790 struct rdma_cm_id *cm_id = queue->cm_id;
1791 int status = ev->status;
1792 const char *rej_msg;
1793 const struct nvme_rdma_cm_rej *rej_data;
1796 rej_msg = rdma_reject_msg(cm_id, status);
1797 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1799 if (rej_data && rej_data_len >= sizeof(u16)) {
1800 u16 sts = le16_to_cpu(rej_data->sts);
1802 dev_err(queue->ctrl->ctrl.device,
1803 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1804 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1806 dev_err(queue->ctrl->ctrl.device,
1807 "Connect rejected: status %d (%s).\n", status, rej_msg);
1813 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1815 struct nvme_ctrl *ctrl = &queue->ctrl->ctrl;
1818 ret = nvme_rdma_create_queue_ib(queue);
1822 if (ctrl->opts->tos >= 0)
1823 rdma_set_service_type(queue->cm_id, ctrl->opts->tos);
1824 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1826 dev_err(ctrl->device, "rdma_resolve_route failed (%d).\n",
1828 goto out_destroy_queue;
1834 nvme_rdma_destroy_queue_ib(queue);
1838 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1840 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1841 struct rdma_conn_param param = { };
1842 struct nvme_rdma_cm_req priv = { };
1845 param.qp_num = queue->qp->qp_num;
1846 param.flow_control = 1;
1848 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1849 /* maximum retry count */
1850 param.retry_count = 7;
1851 param.rnr_retry_count = 7;
1852 param.private_data = &priv;
1853 param.private_data_len = sizeof(priv);
1855 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1856 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1858 * set the admin queue depth to the minimum size
1859 * specified by the Fabrics standard.
1861 if (priv.qid == 0) {
1862 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1863 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
1866 * current interpretation of the fabrics spec
1867 * is at minimum you make hrqsize sqsize+1, or a
1868 * 1's based representation of sqsize.
1870 priv.hrqsize = cpu_to_le16(queue->queue_size);
1871 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1874 ret = rdma_connect(queue->cm_id, ¶m);
1876 dev_err(ctrl->ctrl.device,
1877 "rdma_connect failed (%d).\n", ret);
1878 goto out_destroy_queue_ib;
1883 out_destroy_queue_ib:
1884 nvme_rdma_destroy_queue_ib(queue);
1888 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1889 struct rdma_cm_event *ev)
1891 struct nvme_rdma_queue *queue = cm_id->context;
1894 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1895 rdma_event_msg(ev->event), ev->event,
1898 switch (ev->event) {
1899 case RDMA_CM_EVENT_ADDR_RESOLVED:
1900 cm_error = nvme_rdma_addr_resolved(queue);
1902 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1903 cm_error = nvme_rdma_route_resolved(queue);
1905 case RDMA_CM_EVENT_ESTABLISHED:
1906 queue->cm_error = nvme_rdma_conn_established(queue);
1907 /* complete cm_done regardless of success/failure */
1908 complete(&queue->cm_done);
1910 case RDMA_CM_EVENT_REJECTED:
1911 nvme_rdma_destroy_queue_ib(queue);
1912 cm_error = nvme_rdma_conn_rejected(queue, ev);
1914 case RDMA_CM_EVENT_ROUTE_ERROR:
1915 case RDMA_CM_EVENT_CONNECT_ERROR:
1916 case RDMA_CM_EVENT_UNREACHABLE:
1917 nvme_rdma_destroy_queue_ib(queue);
1919 case RDMA_CM_EVENT_ADDR_ERROR:
1920 dev_dbg(queue->ctrl->ctrl.device,
1921 "CM error event %d\n", ev->event);
1922 cm_error = -ECONNRESET;
1924 case RDMA_CM_EVENT_DISCONNECTED:
1925 case RDMA_CM_EVENT_ADDR_CHANGE:
1926 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1927 dev_dbg(queue->ctrl->ctrl.device,
1928 "disconnect received - connection closed\n");
1929 nvme_rdma_error_recovery(queue->ctrl);
1931 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1932 /* device removal is handled via the ib_client API */
1935 dev_err(queue->ctrl->ctrl.device,
1936 "Unexpected RDMA CM event (%d)\n", ev->event);
1937 nvme_rdma_error_recovery(queue->ctrl);
1942 queue->cm_error = cm_error;
1943 complete(&queue->cm_done);
1949 static enum blk_eh_timer_return
1950 nvme_rdma_timeout(struct request *rq, bool reserved)
1952 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1953 struct nvme_rdma_queue *queue = req->queue;
1954 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1956 dev_warn(ctrl->ctrl.device, "I/O %d QID %d timeout\n",
1957 rq->tag, nvme_rdma_queue_idx(queue));
1960 * Restart the timer if a controller reset is already scheduled. Any
1961 * timed out commands would be handled before entering the connecting
1964 if (ctrl->ctrl.state == NVME_CTRL_RESETTING)
1965 return BLK_EH_RESET_TIMER;
1967 if (ctrl->ctrl.state != NVME_CTRL_LIVE) {
1969 * Teardown immediately if controller times out while starting
1970 * or we are already started error recovery. all outstanding
1971 * requests are completed on shutdown, so we return BLK_EH_DONE.
1973 flush_work(&ctrl->err_work);
1974 nvme_rdma_teardown_io_queues(ctrl, false);
1975 nvme_rdma_teardown_admin_queue(ctrl, false);
1979 dev_warn(ctrl->ctrl.device, "starting error recovery\n");
1980 nvme_rdma_error_recovery(ctrl);
1982 return BLK_EH_RESET_TIMER;
1985 static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1986 const struct blk_mq_queue_data *bd)
1988 struct nvme_ns *ns = hctx->queue->queuedata;
1989 struct nvme_rdma_queue *queue = hctx->driver_data;
1990 struct request *rq = bd->rq;
1991 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1992 struct nvme_rdma_qe *sqe = &req->sqe;
1993 struct nvme_command *c = sqe->data;
1994 struct ib_device *dev;
1995 bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags);
1999 WARN_ON_ONCE(rq->tag < 0);
2001 if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2002 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
2004 dev = queue->device->dev;
2006 req->sqe.dma = ib_dma_map_single(dev, req->sqe.data,
2007 sizeof(struct nvme_command),
2009 err = ib_dma_mapping_error(dev, req->sqe.dma);
2011 return BLK_STS_RESOURCE;
2013 ib_dma_sync_single_for_cpu(dev, sqe->dma,
2014 sizeof(struct nvme_command), DMA_TO_DEVICE);
2016 ret = nvme_setup_cmd(ns, rq, c);
2020 blk_mq_start_request(rq);
2022 if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) &&
2023 queue->pi_support &&
2024 (c->common.opcode == nvme_cmd_write ||
2025 c->common.opcode == nvme_cmd_read) &&
2027 req->use_sig_mr = true;
2029 req->use_sig_mr = false;
2031 err = nvme_rdma_map_data(queue, rq, c);
2032 if (unlikely(err < 0)) {
2033 dev_err(queue->ctrl->ctrl.device,
2034 "Failed to map data (%d)\n", err);
2038 sqe->cqe.done = nvme_rdma_send_done;
2040 ib_dma_sync_single_for_device(dev, sqe->dma,
2041 sizeof(struct nvme_command), DMA_TO_DEVICE);
2043 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
2044 req->mr ? &req->reg_wr.wr : NULL);
2051 nvme_rdma_unmap_data(queue, rq);
2053 if (err == -ENOMEM || err == -EAGAIN)
2054 ret = BLK_STS_RESOURCE;
2056 ret = BLK_STS_IOERR;
2057 nvme_cleanup_cmd(rq);
2059 ib_dma_unmap_single(dev, req->sqe.dma, sizeof(struct nvme_command),
2064 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx)
2066 struct nvme_rdma_queue *queue = hctx->driver_data;
2068 return ib_process_cq_direct(queue->ib_cq, -1);
2071 static void nvme_rdma_check_pi_status(struct nvme_rdma_request *req)
2073 struct request *rq = blk_mq_rq_from_pdu(req);
2074 struct ib_mr_status mr_status;
2077 ret = ib_check_mr_status(req->mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
2079 pr_err("ib_check_mr_status failed, ret %d\n", ret);
2080 nvme_req(rq)->status = NVME_SC_INVALID_PI;
2084 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
2085 switch (mr_status.sig_err.err_type) {
2086 case IB_SIG_BAD_GUARD:
2087 nvme_req(rq)->status = NVME_SC_GUARD_CHECK;
2089 case IB_SIG_BAD_REFTAG:
2090 nvme_req(rq)->status = NVME_SC_REFTAG_CHECK;
2092 case IB_SIG_BAD_APPTAG:
2093 nvme_req(rq)->status = NVME_SC_APPTAG_CHECK;
2096 pr_err("PI error found type %d expected 0x%x vs actual 0x%x\n",
2097 mr_status.sig_err.err_type, mr_status.sig_err.expected,
2098 mr_status.sig_err.actual);
2102 static void nvme_rdma_complete_rq(struct request *rq)
2104 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
2105 struct nvme_rdma_queue *queue = req->queue;
2106 struct ib_device *ibdev = queue->device->dev;
2108 if (req->use_sig_mr)
2109 nvme_rdma_check_pi_status(req);
2111 nvme_rdma_unmap_data(queue, rq);
2112 ib_dma_unmap_single(ibdev, req->sqe.dma, sizeof(struct nvme_command),
2114 nvme_complete_rq(rq);
2117 static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
2119 struct nvme_rdma_ctrl *ctrl = set->driver_data;
2120 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2122 if (opts->nr_write_queues && ctrl->io_queues[HCTX_TYPE_READ]) {
2123 /* separate read/write queues */
2124 set->map[HCTX_TYPE_DEFAULT].nr_queues =
2125 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2126 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2127 set->map[HCTX_TYPE_READ].nr_queues =
2128 ctrl->io_queues[HCTX_TYPE_READ];
2129 set->map[HCTX_TYPE_READ].queue_offset =
2130 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2132 /* shared read/write queues */
2133 set->map[HCTX_TYPE_DEFAULT].nr_queues =
2134 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2135 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2136 set->map[HCTX_TYPE_READ].nr_queues =
2137 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2138 set->map[HCTX_TYPE_READ].queue_offset = 0;
2140 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_DEFAULT],
2141 ctrl->device->dev, 0);
2142 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_READ],
2143 ctrl->device->dev, 0);
2145 if (opts->nr_poll_queues && ctrl->io_queues[HCTX_TYPE_POLL]) {
2146 /* map dedicated poll queues only if we have queues left */
2147 set->map[HCTX_TYPE_POLL].nr_queues =
2148 ctrl->io_queues[HCTX_TYPE_POLL];
2149 set->map[HCTX_TYPE_POLL].queue_offset =
2150 ctrl->io_queues[HCTX_TYPE_DEFAULT] +
2151 ctrl->io_queues[HCTX_TYPE_READ];
2152 blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]);
2155 dev_info(ctrl->ctrl.device,
2156 "mapped %d/%d/%d default/read/poll queues.\n",
2157 ctrl->io_queues[HCTX_TYPE_DEFAULT],
2158 ctrl->io_queues[HCTX_TYPE_READ],
2159 ctrl->io_queues[HCTX_TYPE_POLL]);
2164 static const struct blk_mq_ops nvme_rdma_mq_ops = {
2165 .queue_rq = nvme_rdma_queue_rq,
2166 .complete = nvme_rdma_complete_rq,
2167 .init_request = nvme_rdma_init_request,
2168 .exit_request = nvme_rdma_exit_request,
2169 .init_hctx = nvme_rdma_init_hctx,
2170 .timeout = nvme_rdma_timeout,
2171 .map_queues = nvme_rdma_map_queues,
2172 .poll = nvme_rdma_poll,
2175 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
2176 .queue_rq = nvme_rdma_queue_rq,
2177 .complete = nvme_rdma_complete_rq,
2178 .init_request = nvme_rdma_init_request,
2179 .exit_request = nvme_rdma_exit_request,
2180 .init_hctx = nvme_rdma_init_admin_hctx,
2181 .timeout = nvme_rdma_timeout,
2184 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
2186 cancel_work_sync(&ctrl->err_work);
2187 cancel_delayed_work_sync(&ctrl->reconnect_work);
2189 nvme_rdma_teardown_io_queues(ctrl, shutdown);
2190 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
2192 nvme_shutdown_ctrl(&ctrl->ctrl);
2194 nvme_disable_ctrl(&ctrl->ctrl);
2195 nvme_rdma_teardown_admin_queue(ctrl, shutdown);
2198 static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
2200 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true);
2203 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
2205 struct nvme_rdma_ctrl *ctrl =
2206 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
2208 nvme_stop_ctrl(&ctrl->ctrl);
2209 nvme_rdma_shutdown_ctrl(ctrl, false);
2211 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
2212 /* state change failure should never happen */
2217 if (nvme_rdma_setup_ctrl(ctrl, false))
2223 ++ctrl->ctrl.nr_reconnects;
2224 nvme_rdma_reconnect_or_remove(ctrl);
2227 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
2229 .module = THIS_MODULE,
2230 .flags = NVME_F_FABRICS | NVME_F_METADATA_SUPPORTED,
2231 .reg_read32 = nvmf_reg_read32,
2232 .reg_read64 = nvmf_reg_read64,
2233 .reg_write32 = nvmf_reg_write32,
2234 .free_ctrl = nvme_rdma_free_ctrl,
2235 .submit_async_event = nvme_rdma_submit_async_event,
2236 .delete_ctrl = nvme_rdma_delete_ctrl,
2237 .get_address = nvmf_get_address,
2241 * Fails a connection request if it matches an existing controller
2242 * (association) with the same tuple:
2243 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
2245 * if local address is not specified in the request, it will match an
2246 * existing controller with all the other parameters the same and no
2247 * local port address specified as well.
2249 * The ports don't need to be compared as they are intrinsically
2250 * already matched by the port pointers supplied.
2253 nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts)
2255 struct nvme_rdma_ctrl *ctrl;
2258 mutex_lock(&nvme_rdma_ctrl_mutex);
2259 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2260 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
2264 mutex_unlock(&nvme_rdma_ctrl_mutex);
2269 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
2270 struct nvmf_ctrl_options *opts)
2272 struct nvme_rdma_ctrl *ctrl;
2276 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2278 return ERR_PTR(-ENOMEM);
2279 ctrl->ctrl.opts = opts;
2280 INIT_LIST_HEAD(&ctrl->list);
2282 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
2284 kstrdup(__stringify(NVME_RDMA_IP_PORT), GFP_KERNEL);
2285 if (!opts->trsvcid) {
2289 opts->mask |= NVMF_OPT_TRSVCID;
2292 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2293 opts->traddr, opts->trsvcid, &ctrl->addr);
2295 pr_err("malformed address passed: %s:%s\n",
2296 opts->traddr, opts->trsvcid);
2300 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
2301 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2302 opts->host_traddr, NULL, &ctrl->src_addr);
2304 pr_err("malformed src address passed: %s\n",
2310 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) {
2315 INIT_DELAYED_WORK(&ctrl->reconnect_work,
2316 nvme_rdma_reconnect_ctrl_work);
2317 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
2318 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
2320 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
2321 opts->nr_poll_queues + 1;
2322 ctrl->ctrl.sqsize = opts->queue_size - 1;
2323 ctrl->ctrl.kato = opts->kato;
2326 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
2331 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
2332 0 /* no quirks, we're perfect! */);
2334 goto out_kfree_queues;
2336 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING);
2337 WARN_ON_ONCE(!changed);
2339 ret = nvme_rdma_setup_ctrl(ctrl, true);
2341 goto out_uninit_ctrl;
2343 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
2344 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2346 mutex_lock(&nvme_rdma_ctrl_mutex);
2347 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
2348 mutex_unlock(&nvme_rdma_ctrl_mutex);
2353 nvme_uninit_ctrl(&ctrl->ctrl);
2354 nvme_put_ctrl(&ctrl->ctrl);
2357 return ERR_PTR(ret);
2359 kfree(ctrl->queues);
2362 return ERR_PTR(ret);
2365 static struct nvmf_transport_ops nvme_rdma_transport = {
2367 .module = THIS_MODULE,
2368 .required_opts = NVMF_OPT_TRADDR,
2369 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2370 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
2371 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
2373 .create_ctrl = nvme_rdma_create_ctrl,
2376 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2378 struct nvme_rdma_ctrl *ctrl;
2379 struct nvme_rdma_device *ndev;
2382 mutex_lock(&device_list_mutex);
2383 list_for_each_entry(ndev, &device_list, entry) {
2384 if (ndev->dev == ib_device) {
2389 mutex_unlock(&device_list_mutex);
2394 /* Delete all controllers using this device */
2395 mutex_lock(&nvme_rdma_ctrl_mutex);
2396 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2397 if (ctrl->device->dev != ib_device)
2399 nvme_delete_ctrl(&ctrl->ctrl);
2401 mutex_unlock(&nvme_rdma_ctrl_mutex);
2403 flush_workqueue(nvme_delete_wq);
2406 static struct ib_client nvme_rdma_ib_client = {
2407 .name = "nvme_rdma",
2408 .remove = nvme_rdma_remove_one
2411 static int __init nvme_rdma_init_module(void)
2415 ret = ib_register_client(&nvme_rdma_ib_client);
2419 ret = nvmf_register_transport(&nvme_rdma_transport);
2421 goto err_unreg_client;
2426 ib_unregister_client(&nvme_rdma_ib_client);
2430 static void __exit nvme_rdma_cleanup_module(void)
2432 struct nvme_rdma_ctrl *ctrl;
2434 nvmf_unregister_transport(&nvme_rdma_transport);
2435 ib_unregister_client(&nvme_rdma_ib_client);
2437 mutex_lock(&nvme_rdma_ctrl_mutex);
2438 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list)
2439 nvme_delete_ctrl(&ctrl->ctrl);
2440 mutex_unlock(&nvme_rdma_ctrl_mutex);
2441 flush_workqueue(nvme_delete_wq);
2444 module_init(nvme_rdma_init_module);
2445 module_exit(nvme_rdma_cleanup_module);
2447 MODULE_LICENSE("GPL v2");