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-integrity.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_CM_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;
100 struct mutex queue_lock;
103 struct nvme_rdma_ctrl {
104 /* read only in the hot path */
105 struct nvme_rdma_queue *queues;
107 /* other member variables */
108 struct blk_mq_tag_set tag_set;
109 struct work_struct err_work;
111 struct nvme_rdma_qe async_event_sqe;
113 struct delayed_work reconnect_work;
115 struct list_head list;
117 struct blk_mq_tag_set admin_tag_set;
118 struct nvme_rdma_device *device;
122 struct sockaddr_storage addr;
123 struct sockaddr_storage src_addr;
125 struct nvme_ctrl ctrl;
126 bool use_inline_data;
127 u32 io_queues[HCTX_MAX_TYPES];
130 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
132 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
135 static LIST_HEAD(device_list);
136 static DEFINE_MUTEX(device_list_mutex);
138 static LIST_HEAD(nvme_rdma_ctrl_list);
139 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
142 * Disabling this option makes small I/O goes faster, but is fundamentally
143 * unsafe. With it turned off we will have to register a global rkey that
144 * allows read and write access to all physical memory.
146 static bool register_always = true;
147 module_param(register_always, bool, 0444);
148 MODULE_PARM_DESC(register_always,
149 "Use memory registration even for contiguous memory regions");
151 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
152 struct rdma_cm_event *event);
153 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
154 static void nvme_rdma_complete_rq(struct request *rq);
156 static const struct blk_mq_ops nvme_rdma_mq_ops;
157 static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
159 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
161 return queue - queue->ctrl->queues;
164 static bool nvme_rdma_poll_queue(struct nvme_rdma_queue *queue)
166 return nvme_rdma_queue_idx(queue) >
167 queue->ctrl->io_queues[HCTX_TYPE_DEFAULT] +
168 queue->ctrl->io_queues[HCTX_TYPE_READ];
171 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
173 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
176 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
177 size_t capsule_size, enum dma_data_direction dir)
179 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
183 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
184 size_t capsule_size, enum dma_data_direction dir)
186 qe->data = kzalloc(capsule_size, GFP_KERNEL);
190 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
191 if (ib_dma_mapping_error(ibdev, qe->dma)) {
200 static void nvme_rdma_free_ring(struct ib_device *ibdev,
201 struct nvme_rdma_qe *ring, size_t ib_queue_size,
202 size_t capsule_size, enum dma_data_direction dir)
206 for (i = 0; i < ib_queue_size; i++)
207 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
211 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
212 size_t ib_queue_size, size_t capsule_size,
213 enum dma_data_direction dir)
215 struct nvme_rdma_qe *ring;
218 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
223 * Bind the CQEs (post recv buffers) DMA mapping to the RDMA queue
224 * lifetime. It's safe, since any chage in the underlying RDMA device
225 * will issue error recovery and queue re-creation.
227 for (i = 0; i < ib_queue_size; i++) {
228 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
235 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
239 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
241 pr_debug("QP event %s (%d)\n",
242 ib_event_msg(event->event), event->event);
246 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
250 ret = wait_for_completion_interruptible(&queue->cm_done);
253 WARN_ON_ONCE(queue->cm_error > 0);
254 return queue->cm_error;
257 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
259 struct nvme_rdma_device *dev = queue->device;
260 struct ib_qp_init_attr init_attr;
263 memset(&init_attr, 0, sizeof(init_attr));
264 init_attr.event_handler = nvme_rdma_qp_event;
266 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
268 init_attr.cap.max_recv_wr = queue->queue_size + 1;
269 init_attr.cap.max_recv_sge = 1;
270 init_attr.cap.max_send_sge = 1 + dev->num_inline_segments;
271 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
272 init_attr.qp_type = IB_QPT_RC;
273 init_attr.send_cq = queue->ib_cq;
274 init_attr.recv_cq = queue->ib_cq;
275 if (queue->pi_support)
276 init_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN;
277 init_attr.qp_context = queue;
279 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
281 queue->qp = queue->cm_id->qp;
285 static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
286 struct request *rq, unsigned int hctx_idx)
288 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
290 kfree(req->sqe.data);
293 static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
294 struct request *rq, unsigned int hctx_idx,
295 unsigned int numa_node)
297 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(set->driver_data);
298 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
299 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
300 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
302 nvme_req(rq)->ctrl = &ctrl->ctrl;
303 req->sqe.data = kzalloc(sizeof(struct nvme_command), GFP_KERNEL);
307 /* metadata nvme_rdma_sgl struct is located after command's data SGL */
308 if (queue->pi_support)
309 req->metadata_sgl = (void *)nvme_req(rq) +
310 sizeof(struct nvme_rdma_request) +
311 NVME_RDMA_DATA_SGL_SIZE;
314 nvme_req(rq)->cmd = req->sqe.data;
319 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
320 unsigned int hctx_idx)
322 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(data);
323 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
325 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
327 hctx->driver_data = queue;
331 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
332 unsigned int hctx_idx)
334 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(data);
335 struct nvme_rdma_queue *queue = &ctrl->queues[0];
337 BUG_ON(hctx_idx != 0);
339 hctx->driver_data = queue;
343 static void nvme_rdma_free_dev(struct kref *ref)
345 struct nvme_rdma_device *ndev =
346 container_of(ref, struct nvme_rdma_device, ref);
348 mutex_lock(&device_list_mutex);
349 list_del(&ndev->entry);
350 mutex_unlock(&device_list_mutex);
352 ib_dealloc_pd(ndev->pd);
356 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
358 kref_put(&dev->ref, nvme_rdma_free_dev);
361 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
363 return kref_get_unless_zero(&dev->ref);
366 static struct nvme_rdma_device *
367 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
369 struct nvme_rdma_device *ndev;
371 mutex_lock(&device_list_mutex);
372 list_for_each_entry(ndev, &device_list, entry) {
373 if (ndev->dev->node_guid == cm_id->device->node_guid &&
374 nvme_rdma_dev_get(ndev))
378 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
382 ndev->dev = cm_id->device;
383 kref_init(&ndev->ref);
385 ndev->pd = ib_alloc_pd(ndev->dev,
386 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
387 if (IS_ERR(ndev->pd))
390 if (!(ndev->dev->attrs.device_cap_flags &
391 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
392 dev_err(&ndev->dev->dev,
393 "Memory registrations not supported.\n");
397 ndev->num_inline_segments = min(NVME_RDMA_MAX_INLINE_SEGMENTS,
398 ndev->dev->attrs.max_send_sge - 1);
399 list_add(&ndev->entry, &device_list);
401 mutex_unlock(&device_list_mutex);
405 ib_dealloc_pd(ndev->pd);
409 mutex_unlock(&device_list_mutex);
413 static void nvme_rdma_free_cq(struct nvme_rdma_queue *queue)
415 if (nvme_rdma_poll_queue(queue))
416 ib_free_cq(queue->ib_cq);
418 ib_cq_pool_put(queue->ib_cq, queue->cq_size);
421 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
423 struct nvme_rdma_device *dev;
424 struct ib_device *ibdev;
426 if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags))
432 if (queue->pi_support)
433 ib_mr_pool_destroy(queue->qp, &queue->qp->sig_mrs);
434 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
437 * The cm_id object might have been destroyed during RDMA connection
438 * establishment error flow to avoid getting other cma events, thus
439 * the destruction of the QP shouldn't use rdma_cm API.
441 ib_destroy_qp(queue->qp);
442 nvme_rdma_free_cq(queue);
444 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
445 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
447 nvme_rdma_dev_put(dev);
450 static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev, bool pi_support)
452 u32 max_page_list_len;
455 max_page_list_len = ibdev->attrs.max_pi_fast_reg_page_list_len;
457 max_page_list_len = ibdev->attrs.max_fast_reg_page_list_len;
459 return min_t(u32, NVME_RDMA_MAX_SEGMENTS, max_page_list_len - 1);
462 static int nvme_rdma_create_cq(struct ib_device *ibdev,
463 struct nvme_rdma_queue *queue)
465 int ret, comp_vector, idx = nvme_rdma_queue_idx(queue);
468 * Spread I/O queues completion vectors according their queue index.
469 * Admin queues can always go on completion vector 0.
471 comp_vector = (idx == 0 ? idx : idx - 1) % ibdev->num_comp_vectors;
473 /* Polling queues need direct cq polling context */
474 if (nvme_rdma_poll_queue(queue))
475 queue->ib_cq = ib_alloc_cq(ibdev, queue, queue->cq_size,
476 comp_vector, IB_POLL_DIRECT);
478 queue->ib_cq = ib_cq_pool_get(ibdev, queue->cq_size,
479 comp_vector, IB_POLL_SOFTIRQ);
481 if (IS_ERR(queue->ib_cq)) {
482 ret = PTR_ERR(queue->ib_cq);
489 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
491 struct ib_device *ibdev;
492 const int send_wr_factor = 3; /* MR, SEND, INV */
493 const int cq_factor = send_wr_factor + 1; /* + RECV */
494 int ret, pages_per_mr;
496 queue->device = nvme_rdma_find_get_device(queue->cm_id);
497 if (!queue->device) {
498 dev_err(queue->cm_id->device->dev.parent,
499 "no client data found!\n");
500 return -ECONNREFUSED;
502 ibdev = queue->device->dev;
504 /* +1 for ib_drain_qp */
505 queue->cq_size = cq_factor * queue->queue_size + 1;
507 ret = nvme_rdma_create_cq(ibdev, queue);
511 ret = nvme_rdma_create_qp(queue, send_wr_factor);
513 goto out_destroy_ib_cq;
515 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
516 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
517 if (!queue->rsp_ring) {
523 * Currently we don't use SG_GAPS MR's so if the first entry is
524 * misaligned we'll end up using two entries for a single data page,
525 * so one additional entry is required.
527 pages_per_mr = nvme_rdma_get_max_fr_pages(ibdev, queue->pi_support) + 1;
528 ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs,
533 dev_err(queue->ctrl->ctrl.device,
534 "failed to initialize MR pool sized %d for QID %d\n",
535 queue->queue_size, nvme_rdma_queue_idx(queue));
536 goto out_destroy_ring;
539 if (queue->pi_support) {
540 ret = ib_mr_pool_init(queue->qp, &queue->qp->sig_mrs,
541 queue->queue_size, IB_MR_TYPE_INTEGRITY,
542 pages_per_mr, pages_per_mr);
544 dev_err(queue->ctrl->ctrl.device,
545 "failed to initialize PI MR pool sized %d for QID %d\n",
546 queue->queue_size, nvme_rdma_queue_idx(queue));
547 goto out_destroy_mr_pool;
551 set_bit(NVME_RDMA_Q_TR_READY, &queue->flags);
556 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
558 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
559 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
561 rdma_destroy_qp(queue->cm_id);
563 nvme_rdma_free_cq(queue);
565 nvme_rdma_dev_put(queue->device);
569 static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
570 int idx, size_t queue_size)
572 struct nvme_rdma_queue *queue;
573 struct sockaddr *src_addr = NULL;
576 queue = &ctrl->queues[idx];
577 mutex_init(&queue->queue_lock);
579 if (idx && ctrl->ctrl.max_integrity_segments)
580 queue->pi_support = true;
582 queue->pi_support = false;
583 init_completion(&queue->cm_done);
586 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
588 queue->cmnd_capsule_len = sizeof(struct nvme_command);
590 queue->queue_size = queue_size;
592 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
593 RDMA_PS_TCP, IB_QPT_RC);
594 if (IS_ERR(queue->cm_id)) {
595 dev_info(ctrl->ctrl.device,
596 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
597 ret = PTR_ERR(queue->cm_id);
598 goto out_destroy_mutex;
601 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
602 src_addr = (struct sockaddr *)&ctrl->src_addr;
604 queue->cm_error = -ETIMEDOUT;
605 ret = rdma_resolve_addr(queue->cm_id, src_addr,
606 (struct sockaddr *)&ctrl->addr,
607 NVME_RDMA_CM_TIMEOUT_MS);
609 dev_info(ctrl->ctrl.device,
610 "rdma_resolve_addr failed (%d).\n", ret);
611 goto out_destroy_cm_id;
614 ret = nvme_rdma_wait_for_cm(queue);
616 dev_info(ctrl->ctrl.device,
617 "rdma connection establishment failed (%d)\n", ret);
618 goto out_destroy_cm_id;
621 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
626 rdma_destroy_id(queue->cm_id);
627 nvme_rdma_destroy_queue_ib(queue);
629 mutex_destroy(&queue->queue_lock);
633 static void __nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
635 rdma_disconnect(queue->cm_id);
636 ib_drain_qp(queue->qp);
639 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
641 mutex_lock(&queue->queue_lock);
642 if (test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
643 __nvme_rdma_stop_queue(queue);
644 mutex_unlock(&queue->queue_lock);
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 rdma_destroy_id(queue->cm_id);
653 nvme_rdma_destroy_queue_ib(queue);
654 mutex_destroy(&queue->queue_lock);
657 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
661 for (i = 1; i < ctrl->ctrl.queue_count; i++)
662 nvme_rdma_free_queue(&ctrl->queues[i]);
665 static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
669 for (i = 1; i < ctrl->ctrl.queue_count; i++)
670 nvme_rdma_stop_queue(&ctrl->queues[i]);
673 static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
675 struct nvme_rdma_queue *queue = &ctrl->queues[idx];
679 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx);
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,
699 for (i = first; i < last; i++) {
700 ret = nvme_rdma_start_queue(ctrl, i);
702 goto out_stop_queues;
708 for (i--; i >= first; i--)
709 nvme_rdma_stop_queue(&ctrl->queues[i]);
713 static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
715 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
716 unsigned int nr_io_queues;
719 nr_io_queues = nvmf_nr_io_queues(opts);
720 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
724 if (nr_io_queues == 0) {
725 dev_err(ctrl->ctrl.device,
726 "unable to set any I/O queues\n");
730 ctrl->ctrl.queue_count = nr_io_queues + 1;
731 dev_info(ctrl->ctrl.device,
732 "creating %d I/O queues.\n", nr_io_queues);
734 nvmf_set_io_queues(opts, nr_io_queues, ctrl->io_queues);
735 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
736 ret = nvme_rdma_alloc_queue(ctrl, i,
737 ctrl->ctrl.sqsize + 1);
739 goto out_free_queues;
745 for (i--; i >= 1; i--)
746 nvme_rdma_free_queue(&ctrl->queues[i]);
751 static int nvme_rdma_alloc_tag_set(struct nvme_ctrl *ctrl)
753 unsigned int cmd_size = sizeof(struct nvme_rdma_request) +
754 NVME_RDMA_DATA_SGL_SIZE;
756 if (ctrl->max_integrity_segments)
757 cmd_size += sizeof(struct nvme_rdma_sgl) +
758 NVME_RDMA_METADATA_SGL_SIZE;
760 return nvme_alloc_io_tag_set(ctrl, &to_rdma_ctrl(ctrl)->tag_set,
762 ctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2,
766 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl)
768 if (ctrl->async_event_sqe.data) {
769 cancel_work_sync(&ctrl->ctrl.async_event_work);
770 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
771 sizeof(struct nvme_command), DMA_TO_DEVICE);
772 ctrl->async_event_sqe.data = NULL;
774 nvme_rdma_free_queue(&ctrl->queues[0]);
777 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
780 bool pi_capable = false;
783 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
787 ctrl->device = ctrl->queues[0].device;
788 ctrl->ctrl.numa_node = ibdev_to_node(ctrl->device->dev);
791 if (ctrl->device->dev->attrs.kernel_cap_flags &
792 IBK_INTEGRITY_HANDOVER)
795 ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev,
799 * Bind the async event SQE DMA mapping to the admin queue lifetime.
800 * It's safe, since any chage in the underlying RDMA device will issue
801 * error recovery and queue re-creation.
803 error = nvme_rdma_alloc_qe(ctrl->device->dev, &ctrl->async_event_sqe,
804 sizeof(struct nvme_command), DMA_TO_DEVICE);
809 error = nvme_alloc_admin_tag_set(&ctrl->ctrl,
810 &ctrl->admin_tag_set, &nvme_rdma_admin_mq_ops,
811 sizeof(struct nvme_rdma_request) +
812 NVME_RDMA_DATA_SGL_SIZE);
814 goto out_free_async_qe;
818 error = nvme_rdma_start_queue(ctrl, 0);
820 goto out_remove_admin_tag_set;
822 error = nvme_enable_ctrl(&ctrl->ctrl);
826 ctrl->ctrl.max_segments = ctrl->max_fr_pages;
827 ctrl->ctrl.max_hw_sectors = ctrl->max_fr_pages << (ilog2(SZ_4K) - 9);
829 ctrl->ctrl.max_integrity_segments = ctrl->max_fr_pages;
831 ctrl->ctrl.max_integrity_segments = 0;
833 nvme_unquiesce_admin_queue(&ctrl->ctrl);
835 error = nvme_init_ctrl_finish(&ctrl->ctrl, false);
837 goto out_quiesce_queue;
842 nvme_quiesce_admin_queue(&ctrl->ctrl);
843 blk_sync_queue(ctrl->ctrl.admin_q);
845 nvme_rdma_stop_queue(&ctrl->queues[0]);
846 nvme_cancel_admin_tagset(&ctrl->ctrl);
847 out_remove_admin_tag_set:
849 nvme_remove_admin_tag_set(&ctrl->ctrl);
851 if (ctrl->async_event_sqe.data) {
852 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
853 sizeof(struct nvme_command), DMA_TO_DEVICE);
854 ctrl->async_event_sqe.data = NULL;
857 nvme_rdma_free_queue(&ctrl->queues[0]);
861 static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
865 ret = nvme_rdma_alloc_io_queues(ctrl);
870 ret = nvme_rdma_alloc_tag_set(&ctrl->ctrl);
872 goto out_free_io_queues;
876 * Only start IO queues for which we have allocated the tagset
877 * and limitted it to the available queues. On reconnects, the
878 * queue number might have changed.
880 nr_queues = min(ctrl->tag_set.nr_hw_queues + 1, ctrl->ctrl.queue_count);
881 ret = nvme_rdma_start_io_queues(ctrl, 1, nr_queues);
883 goto out_cleanup_tagset;
886 nvme_start_freeze(&ctrl->ctrl);
887 nvme_unquiesce_io_queues(&ctrl->ctrl);
888 if (!nvme_wait_freeze_timeout(&ctrl->ctrl, NVME_IO_TIMEOUT)) {
890 * If we timed out waiting for freeze we are likely to
891 * be stuck. Fail the controller initialization just
895 nvme_unfreeze(&ctrl->ctrl);
896 goto out_wait_freeze_timed_out;
898 blk_mq_update_nr_hw_queues(ctrl->ctrl.tagset,
899 ctrl->ctrl.queue_count - 1);
900 nvme_unfreeze(&ctrl->ctrl);
904 * If the number of queues has increased (reconnect case)
905 * start all new queues now.
907 ret = nvme_rdma_start_io_queues(ctrl, nr_queues,
908 ctrl->tag_set.nr_hw_queues + 1);
910 goto out_wait_freeze_timed_out;
914 out_wait_freeze_timed_out:
915 nvme_quiesce_io_queues(&ctrl->ctrl);
916 nvme_sync_io_queues(&ctrl->ctrl);
917 nvme_rdma_stop_io_queues(ctrl);
919 nvme_cancel_tagset(&ctrl->ctrl);
921 nvme_remove_io_tag_set(&ctrl->ctrl);
923 nvme_rdma_free_io_queues(ctrl);
927 static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl *ctrl,
930 nvme_quiesce_admin_queue(&ctrl->ctrl);
931 blk_sync_queue(ctrl->ctrl.admin_q);
932 nvme_rdma_stop_queue(&ctrl->queues[0]);
933 nvme_cancel_admin_tagset(&ctrl->ctrl);
935 nvme_unquiesce_admin_queue(&ctrl->ctrl);
936 nvme_remove_admin_tag_set(&ctrl->ctrl);
938 nvme_rdma_destroy_admin_queue(ctrl);
941 static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl *ctrl,
944 if (ctrl->ctrl.queue_count > 1) {
945 nvme_quiesce_io_queues(&ctrl->ctrl);
946 nvme_sync_io_queues(&ctrl->ctrl);
947 nvme_rdma_stop_io_queues(ctrl);
948 nvme_cancel_tagset(&ctrl->ctrl);
950 nvme_unquiesce_io_queues(&ctrl->ctrl);
951 nvme_remove_io_tag_set(&ctrl->ctrl);
953 nvme_rdma_free_io_queues(ctrl);
957 static void nvme_rdma_stop_ctrl(struct nvme_ctrl *nctrl)
959 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
961 flush_work(&ctrl->err_work);
962 cancel_delayed_work_sync(&ctrl->reconnect_work);
965 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
967 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
969 if (list_empty(&ctrl->list))
972 mutex_lock(&nvme_rdma_ctrl_mutex);
973 list_del(&ctrl->list);
974 mutex_unlock(&nvme_rdma_ctrl_mutex);
976 nvmf_free_options(nctrl->opts);
982 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
984 /* If we are resetting/deleting then do nothing */
985 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
986 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
987 ctrl->ctrl.state == NVME_CTRL_LIVE);
991 if (nvmf_should_reconnect(&ctrl->ctrl)) {
992 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
993 ctrl->ctrl.opts->reconnect_delay);
994 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
995 ctrl->ctrl.opts->reconnect_delay * HZ);
997 nvme_delete_ctrl(&ctrl->ctrl);
1001 static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new)
1006 ret = nvme_rdma_configure_admin_queue(ctrl, new);
1010 if (ctrl->ctrl.icdoff) {
1012 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1016 if (!(ctrl->ctrl.sgls & (1 << 2))) {
1018 dev_err(ctrl->ctrl.device,
1019 "Mandatory keyed sgls are not supported!\n");
1023 if (ctrl->ctrl.opts->queue_size > ctrl->ctrl.sqsize + 1) {
1024 dev_warn(ctrl->ctrl.device,
1025 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1026 ctrl->ctrl.opts->queue_size, ctrl->ctrl.sqsize + 1);
1029 if (ctrl->ctrl.sqsize + 1 > NVME_RDMA_MAX_QUEUE_SIZE) {
1030 dev_warn(ctrl->ctrl.device,
1031 "ctrl sqsize %u > max queue size %u, clamping down\n",
1032 ctrl->ctrl.sqsize + 1, NVME_RDMA_MAX_QUEUE_SIZE);
1033 ctrl->ctrl.sqsize = NVME_RDMA_MAX_QUEUE_SIZE - 1;
1036 if (ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) {
1037 dev_warn(ctrl->ctrl.device,
1038 "sqsize %u > ctrl maxcmd %u, clamping down\n",
1039 ctrl->ctrl.sqsize + 1, ctrl->ctrl.maxcmd);
1040 ctrl->ctrl.sqsize = ctrl->ctrl.maxcmd - 1;
1043 if (ctrl->ctrl.sgls & (1 << 20))
1044 ctrl->use_inline_data = true;
1046 if (ctrl->ctrl.queue_count > 1) {
1047 ret = nvme_rdma_configure_io_queues(ctrl, new);
1052 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1055 * state change failure is ok if we started ctrl delete,
1056 * unless we're during creation of a new controller to
1057 * avoid races with teardown flow.
1059 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING &&
1060 ctrl->ctrl.state != NVME_CTRL_DELETING_NOIO);
1066 nvme_start_ctrl(&ctrl->ctrl);
1070 if (ctrl->ctrl.queue_count > 1) {
1071 nvme_quiesce_io_queues(&ctrl->ctrl);
1072 nvme_sync_io_queues(&ctrl->ctrl);
1073 nvme_rdma_stop_io_queues(ctrl);
1074 nvme_cancel_tagset(&ctrl->ctrl);
1076 nvme_remove_io_tag_set(&ctrl->ctrl);
1077 nvme_rdma_free_io_queues(ctrl);
1080 nvme_quiesce_admin_queue(&ctrl->ctrl);
1081 blk_sync_queue(ctrl->ctrl.admin_q);
1082 nvme_rdma_stop_queue(&ctrl->queues[0]);
1083 nvme_cancel_admin_tagset(&ctrl->ctrl);
1085 nvme_remove_admin_tag_set(&ctrl->ctrl);
1086 nvme_rdma_destroy_admin_queue(ctrl);
1090 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
1092 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
1093 struct nvme_rdma_ctrl, reconnect_work);
1095 ++ctrl->ctrl.nr_reconnects;
1097 if (nvme_rdma_setup_ctrl(ctrl, false))
1100 dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
1101 ctrl->ctrl.nr_reconnects);
1103 ctrl->ctrl.nr_reconnects = 0;
1108 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
1109 ctrl->ctrl.nr_reconnects);
1110 nvme_rdma_reconnect_or_remove(ctrl);
1113 static void nvme_rdma_error_recovery_work(struct work_struct *work)
1115 struct nvme_rdma_ctrl *ctrl = container_of(work,
1116 struct nvme_rdma_ctrl, err_work);
1118 nvme_stop_keep_alive(&ctrl->ctrl);
1119 flush_work(&ctrl->ctrl.async_event_work);
1120 nvme_rdma_teardown_io_queues(ctrl, false);
1121 nvme_unquiesce_io_queues(&ctrl->ctrl);
1122 nvme_rdma_teardown_admin_queue(ctrl, false);
1123 nvme_unquiesce_admin_queue(&ctrl->ctrl);
1124 nvme_auth_stop(&ctrl->ctrl);
1126 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
1127 /* state change failure is ok if we started ctrl delete */
1128 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING &&
1129 ctrl->ctrl.state != NVME_CTRL_DELETING_NOIO);
1133 nvme_rdma_reconnect_or_remove(ctrl);
1136 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
1138 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1141 dev_warn(ctrl->ctrl.device, "starting error recovery\n");
1142 queue_work(nvme_reset_wq, &ctrl->err_work);
1145 static void nvme_rdma_end_request(struct nvme_rdma_request *req)
1147 struct request *rq = blk_mq_rq_from_pdu(req);
1149 if (!refcount_dec_and_test(&req->ref))
1151 if (!nvme_try_complete_req(rq, req->status, req->result))
1152 nvme_rdma_complete_rq(rq);
1155 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1158 struct nvme_rdma_queue *queue = wc->qp->qp_context;
1159 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1161 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1162 dev_info(ctrl->ctrl.device,
1163 "%s for CQE 0x%p failed with status %s (%d)\n",
1165 ib_wc_status_msg(wc->status), wc->status);
1166 nvme_rdma_error_recovery(ctrl);
1169 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1171 if (unlikely(wc->status != IB_WC_SUCCESS))
1172 nvme_rdma_wr_error(cq, wc, "MEMREG");
1175 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1177 struct nvme_rdma_request *req =
1178 container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe);
1180 if (unlikely(wc->status != IB_WC_SUCCESS))
1181 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
1183 nvme_rdma_end_request(req);
1186 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1187 struct nvme_rdma_request *req)
1189 struct ib_send_wr wr = {
1190 .opcode = IB_WR_LOCAL_INV,
1193 .send_flags = IB_SEND_SIGNALED,
1194 .ex.invalidate_rkey = req->mr->rkey,
1197 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1198 wr.wr_cqe = &req->reg_cqe;
1200 return ib_post_send(queue->qp, &wr, NULL);
1203 static void nvme_rdma_dma_unmap_req(struct ib_device *ibdev, struct request *rq)
1205 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1207 if (blk_integrity_rq(rq)) {
1208 ib_dma_unmap_sg(ibdev, req->metadata_sgl->sg_table.sgl,
1209 req->metadata_sgl->nents, rq_dma_dir(rq));
1210 sg_free_table_chained(&req->metadata_sgl->sg_table,
1211 NVME_INLINE_METADATA_SG_CNT);
1214 ib_dma_unmap_sg(ibdev, req->data_sgl.sg_table.sgl, req->data_sgl.nents,
1216 sg_free_table_chained(&req->data_sgl.sg_table, NVME_INLINE_SG_CNT);
1219 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1222 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1223 struct nvme_rdma_device *dev = queue->device;
1224 struct ib_device *ibdev = dev->dev;
1225 struct list_head *pool = &queue->qp->rdma_mrs;
1227 if (!blk_rq_nr_phys_segments(rq))
1230 if (req->use_sig_mr)
1231 pool = &queue->qp->sig_mrs;
1234 ib_mr_pool_put(queue->qp, pool, req->mr);
1238 nvme_rdma_dma_unmap_req(ibdev, rq);
1241 static int nvme_rdma_set_sg_null(struct nvme_command *c)
1243 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1246 put_unaligned_le24(0, sg->length);
1247 put_unaligned_le32(0, sg->key);
1248 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1252 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
1253 struct nvme_rdma_request *req, struct nvme_command *c,
1256 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1257 struct ib_sge *sge = &req->sge[1];
1258 struct scatterlist *sgl;
1262 for_each_sg(req->data_sgl.sg_table.sgl, sgl, count, i) {
1263 sge->addr = sg_dma_address(sgl);
1264 sge->length = sg_dma_len(sgl);
1265 sge->lkey = queue->device->pd->local_dma_lkey;
1270 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1271 sg->length = cpu_to_le32(len);
1272 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1274 req->num_sge += count;
1278 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1279 struct nvme_rdma_request *req, struct nvme_command *c)
1281 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1283 sg->addr = cpu_to_le64(sg_dma_address(req->data_sgl.sg_table.sgl));
1284 put_unaligned_le24(sg_dma_len(req->data_sgl.sg_table.sgl), sg->length);
1285 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
1286 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1290 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1291 struct nvme_rdma_request *req, struct nvme_command *c,
1294 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1297 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs);
1298 if (WARN_ON_ONCE(!req->mr))
1302 * Align the MR to a 4K page size to match the ctrl page size and
1303 * the block virtual boundary.
1305 nr = ib_map_mr_sg(req->mr, req->data_sgl.sg_table.sgl, count, NULL,
1307 if (unlikely(nr < count)) {
1308 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1315 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1317 req->reg_cqe.done = nvme_rdma_memreg_done;
1318 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1319 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1320 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1321 req->reg_wr.wr.num_sge = 0;
1322 req->reg_wr.mr = req->mr;
1323 req->reg_wr.key = req->mr->rkey;
1324 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1325 IB_ACCESS_REMOTE_READ |
1326 IB_ACCESS_REMOTE_WRITE;
1328 sg->addr = cpu_to_le64(req->mr->iova);
1329 put_unaligned_le24(req->mr->length, sg->length);
1330 put_unaligned_le32(req->mr->rkey, sg->key);
1331 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1332 NVME_SGL_FMT_INVALIDATE;
1337 static void nvme_rdma_set_sig_domain(struct blk_integrity *bi,
1338 struct nvme_command *cmd, struct ib_sig_domain *domain,
1339 u16 control, u8 pi_type)
1341 domain->sig_type = IB_SIG_TYPE_T10_DIF;
1342 domain->sig.dif.bg_type = IB_T10DIF_CRC;
1343 domain->sig.dif.pi_interval = 1 << bi->interval_exp;
1344 domain->sig.dif.ref_tag = le32_to_cpu(cmd->rw.reftag);
1345 if (control & NVME_RW_PRINFO_PRCHK_REF)
1346 domain->sig.dif.ref_remap = true;
1348 domain->sig.dif.app_tag = le16_to_cpu(cmd->rw.apptag);
1349 domain->sig.dif.apptag_check_mask = le16_to_cpu(cmd->rw.appmask);
1350 domain->sig.dif.app_escape = true;
1351 if (pi_type == NVME_NS_DPS_PI_TYPE3)
1352 domain->sig.dif.ref_escape = true;
1355 static void nvme_rdma_set_sig_attrs(struct blk_integrity *bi,
1356 struct nvme_command *cmd, struct ib_sig_attrs *sig_attrs,
1359 u16 control = le16_to_cpu(cmd->rw.control);
1361 memset(sig_attrs, 0, sizeof(*sig_attrs));
1362 if (control & NVME_RW_PRINFO_PRACT) {
1363 /* for WRITE_INSERT/READ_STRIP no memory domain */
1364 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
1365 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control,
1367 /* Clear the PRACT bit since HCA will generate/verify the PI */
1368 control &= ~NVME_RW_PRINFO_PRACT;
1369 cmd->rw.control = cpu_to_le16(control);
1371 /* for WRITE_PASS/READ_PASS both wire/memory domains exist */
1372 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control,
1374 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->mem, control,
1379 static void nvme_rdma_set_prot_checks(struct nvme_command *cmd, u8 *mask)
1382 if (le16_to_cpu(cmd->rw.control) & NVME_RW_PRINFO_PRCHK_REF)
1383 *mask |= IB_SIG_CHECK_REFTAG;
1384 if (le16_to_cpu(cmd->rw.control) & NVME_RW_PRINFO_PRCHK_GUARD)
1385 *mask |= IB_SIG_CHECK_GUARD;
1388 static void nvme_rdma_sig_done(struct ib_cq *cq, struct ib_wc *wc)
1390 if (unlikely(wc->status != IB_WC_SUCCESS))
1391 nvme_rdma_wr_error(cq, wc, "SIG");
1394 static int nvme_rdma_map_sg_pi(struct nvme_rdma_queue *queue,
1395 struct nvme_rdma_request *req, struct nvme_command *c,
1396 int count, int pi_count)
1398 struct nvme_rdma_sgl *sgl = &req->data_sgl;
1399 struct ib_reg_wr *wr = &req->reg_wr;
1400 struct request *rq = blk_mq_rq_from_pdu(req);
1401 struct nvme_ns *ns = rq->q->queuedata;
1402 struct bio *bio = rq->bio;
1403 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1406 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->sig_mrs);
1407 if (WARN_ON_ONCE(!req->mr))
1410 nr = ib_map_mr_sg_pi(req->mr, sgl->sg_table.sgl, count, NULL,
1411 req->metadata_sgl->sg_table.sgl, pi_count, NULL,
1416 nvme_rdma_set_sig_attrs(blk_get_integrity(bio->bi_bdev->bd_disk), c,
1417 req->mr->sig_attrs, ns->pi_type);
1418 nvme_rdma_set_prot_checks(c, &req->mr->sig_attrs->check_mask);
1420 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1422 req->reg_cqe.done = nvme_rdma_sig_done;
1423 memset(wr, 0, sizeof(*wr));
1424 wr->wr.opcode = IB_WR_REG_MR_INTEGRITY;
1425 wr->wr.wr_cqe = &req->reg_cqe;
1427 wr->wr.send_flags = 0;
1429 wr->key = req->mr->rkey;
1430 wr->access = IB_ACCESS_LOCAL_WRITE |
1431 IB_ACCESS_REMOTE_READ |
1432 IB_ACCESS_REMOTE_WRITE;
1434 sg->addr = cpu_to_le64(req->mr->iova);
1435 put_unaligned_le24(req->mr->length, sg->length);
1436 put_unaligned_le32(req->mr->rkey, sg->key);
1437 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1442 ib_mr_pool_put(queue->qp, &queue->qp->sig_mrs, req->mr);
1449 static int nvme_rdma_dma_map_req(struct ib_device *ibdev, struct request *rq,
1450 int *count, int *pi_count)
1452 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1455 req->data_sgl.sg_table.sgl = (struct scatterlist *)(req + 1);
1456 ret = sg_alloc_table_chained(&req->data_sgl.sg_table,
1457 blk_rq_nr_phys_segments(rq), req->data_sgl.sg_table.sgl,
1458 NVME_INLINE_SG_CNT);
1462 req->data_sgl.nents = blk_rq_map_sg(rq->q, rq,
1463 req->data_sgl.sg_table.sgl);
1465 *count = ib_dma_map_sg(ibdev, req->data_sgl.sg_table.sgl,
1466 req->data_sgl.nents, rq_dma_dir(rq));
1467 if (unlikely(*count <= 0)) {
1469 goto out_free_table;
1472 if (blk_integrity_rq(rq)) {
1473 req->metadata_sgl->sg_table.sgl =
1474 (struct scatterlist *)(req->metadata_sgl + 1);
1475 ret = sg_alloc_table_chained(&req->metadata_sgl->sg_table,
1476 blk_rq_count_integrity_sg(rq->q, rq->bio),
1477 req->metadata_sgl->sg_table.sgl,
1478 NVME_INLINE_METADATA_SG_CNT);
1479 if (unlikely(ret)) {
1484 req->metadata_sgl->nents = blk_rq_map_integrity_sg(rq->q,
1485 rq->bio, req->metadata_sgl->sg_table.sgl);
1486 *pi_count = ib_dma_map_sg(ibdev,
1487 req->metadata_sgl->sg_table.sgl,
1488 req->metadata_sgl->nents,
1490 if (unlikely(*pi_count <= 0)) {
1492 goto out_free_pi_table;
1499 sg_free_table_chained(&req->metadata_sgl->sg_table,
1500 NVME_INLINE_METADATA_SG_CNT);
1502 ib_dma_unmap_sg(ibdev, req->data_sgl.sg_table.sgl, req->data_sgl.nents,
1505 sg_free_table_chained(&req->data_sgl.sg_table, NVME_INLINE_SG_CNT);
1509 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
1510 struct request *rq, struct nvme_command *c)
1512 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1513 struct nvme_rdma_device *dev = queue->device;
1514 struct ib_device *ibdev = dev->dev;
1519 refcount_set(&req->ref, 2); /* send and recv completions */
1521 c->common.flags |= NVME_CMD_SGL_METABUF;
1523 if (!blk_rq_nr_phys_segments(rq))
1524 return nvme_rdma_set_sg_null(c);
1526 ret = nvme_rdma_dma_map_req(ibdev, rq, &count, &pi_count);
1530 if (req->use_sig_mr) {
1531 ret = nvme_rdma_map_sg_pi(queue, req, c, count, pi_count);
1535 if (count <= dev->num_inline_segments) {
1536 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1537 queue->ctrl->use_inline_data &&
1538 blk_rq_payload_bytes(rq) <=
1539 nvme_rdma_inline_data_size(queue)) {
1540 ret = nvme_rdma_map_sg_inline(queue, req, c, count);
1544 if (count == 1 && dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
1545 ret = nvme_rdma_map_sg_single(queue, req, c);
1550 ret = nvme_rdma_map_sg_fr(queue, req, c, count);
1553 goto out_dma_unmap_req;
1558 nvme_rdma_dma_unmap_req(ibdev, rq);
1562 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1564 struct nvme_rdma_qe *qe =
1565 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1566 struct nvme_rdma_request *req =
1567 container_of(qe, struct nvme_rdma_request, sqe);
1569 if (unlikely(wc->status != IB_WC_SUCCESS))
1570 nvme_rdma_wr_error(cq, wc, "SEND");
1572 nvme_rdma_end_request(req);
1575 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1576 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1577 struct ib_send_wr *first)
1579 struct ib_send_wr wr;
1582 sge->addr = qe->dma;
1583 sge->length = sizeof(struct nvme_command);
1584 sge->lkey = queue->device->pd->local_dma_lkey;
1587 wr.wr_cqe = &qe->cqe;
1589 wr.num_sge = num_sge;
1590 wr.opcode = IB_WR_SEND;
1591 wr.send_flags = IB_SEND_SIGNALED;
1598 ret = ib_post_send(queue->qp, first, NULL);
1599 if (unlikely(ret)) {
1600 dev_err(queue->ctrl->ctrl.device,
1601 "%s failed with error code %d\n", __func__, ret);
1606 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1607 struct nvme_rdma_qe *qe)
1609 struct ib_recv_wr wr;
1613 list.addr = qe->dma;
1614 list.length = sizeof(struct nvme_completion);
1615 list.lkey = queue->device->pd->local_dma_lkey;
1617 qe->cqe.done = nvme_rdma_recv_done;
1620 wr.wr_cqe = &qe->cqe;
1624 ret = ib_post_recv(queue->qp, &wr, NULL);
1625 if (unlikely(ret)) {
1626 dev_err(queue->ctrl->ctrl.device,
1627 "%s failed with error code %d\n", __func__, ret);
1632 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1634 u32 queue_idx = nvme_rdma_queue_idx(queue);
1637 return queue->ctrl->admin_tag_set.tags[queue_idx];
1638 return queue->ctrl->tag_set.tags[queue_idx - 1];
1641 static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc)
1643 if (unlikely(wc->status != IB_WC_SUCCESS))
1644 nvme_rdma_wr_error(cq, wc, "ASYNC");
1647 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg)
1649 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1650 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1651 struct ib_device *dev = queue->device->dev;
1652 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1653 struct nvme_command *cmd = sqe->data;
1657 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1659 memset(cmd, 0, sizeof(*cmd));
1660 cmd->common.opcode = nvme_admin_async_event;
1661 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
1662 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1663 nvme_rdma_set_sg_null(cmd);
1665 sqe->cqe.done = nvme_rdma_async_done;
1667 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1670 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL);
1674 static void nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1675 struct nvme_completion *cqe, struct ib_wc *wc)
1678 struct nvme_rdma_request *req;
1680 rq = nvme_find_rq(nvme_rdma_tagset(queue), cqe->command_id);
1682 dev_err(queue->ctrl->ctrl.device,
1683 "got bad command_id %#x on QP %#x\n",
1684 cqe->command_id, queue->qp->qp_num);
1685 nvme_rdma_error_recovery(queue->ctrl);
1688 req = blk_mq_rq_to_pdu(rq);
1690 req->status = cqe->status;
1691 req->result = cqe->result;
1693 if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
1694 if (unlikely(!req->mr ||
1695 wc->ex.invalidate_rkey != req->mr->rkey)) {
1696 dev_err(queue->ctrl->ctrl.device,
1697 "Bogus remote invalidation for rkey %#x\n",
1698 req->mr ? req->mr->rkey : 0);
1699 nvme_rdma_error_recovery(queue->ctrl);
1701 } else if (req->mr) {
1704 ret = nvme_rdma_inv_rkey(queue, req);
1705 if (unlikely(ret < 0)) {
1706 dev_err(queue->ctrl->ctrl.device,
1707 "Queueing INV WR for rkey %#x failed (%d)\n",
1708 req->mr->rkey, ret);
1709 nvme_rdma_error_recovery(queue->ctrl);
1711 /* the local invalidation completion will end the request */
1715 nvme_rdma_end_request(req);
1718 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1720 struct nvme_rdma_qe *qe =
1721 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1722 struct nvme_rdma_queue *queue = wc->qp->qp_context;
1723 struct ib_device *ibdev = queue->device->dev;
1724 struct nvme_completion *cqe = qe->data;
1725 const size_t len = sizeof(struct nvme_completion);
1727 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1728 nvme_rdma_wr_error(cq, wc, "RECV");
1732 /* sanity checking for received data length */
1733 if (unlikely(wc->byte_len < len)) {
1734 dev_err(queue->ctrl->ctrl.device,
1735 "Unexpected nvme completion length(%d)\n", wc->byte_len);
1736 nvme_rdma_error_recovery(queue->ctrl);
1740 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1742 * AEN requests are special as they don't time out and can
1743 * survive any kind of queue freeze and often don't respond to
1744 * aborts. We don't even bother to allocate a struct request
1745 * for them but rather special case them here.
1747 if (unlikely(nvme_is_aen_req(nvme_rdma_queue_idx(queue),
1749 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1752 nvme_rdma_process_nvme_rsp(queue, cqe, wc);
1753 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1755 nvme_rdma_post_recv(queue, qe);
1758 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1762 for (i = 0; i < queue->queue_size; i++) {
1763 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1771 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1772 struct rdma_cm_event *ev)
1774 struct rdma_cm_id *cm_id = queue->cm_id;
1775 int status = ev->status;
1776 const char *rej_msg;
1777 const struct nvme_rdma_cm_rej *rej_data;
1780 rej_msg = rdma_reject_msg(cm_id, status);
1781 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1783 if (rej_data && rej_data_len >= sizeof(u16)) {
1784 u16 sts = le16_to_cpu(rej_data->sts);
1786 dev_err(queue->ctrl->ctrl.device,
1787 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1788 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1790 dev_err(queue->ctrl->ctrl.device,
1791 "Connect rejected: status %d (%s).\n", status, rej_msg);
1797 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1799 struct nvme_ctrl *ctrl = &queue->ctrl->ctrl;
1802 ret = nvme_rdma_create_queue_ib(queue);
1806 if (ctrl->opts->tos >= 0)
1807 rdma_set_service_type(queue->cm_id, ctrl->opts->tos);
1808 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CM_TIMEOUT_MS);
1810 dev_err(ctrl->device, "rdma_resolve_route failed (%d).\n",
1812 goto out_destroy_queue;
1818 nvme_rdma_destroy_queue_ib(queue);
1822 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1824 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1825 struct rdma_conn_param param = { };
1826 struct nvme_rdma_cm_req priv = { };
1829 param.qp_num = queue->qp->qp_num;
1830 param.flow_control = 1;
1832 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1833 /* maximum retry count */
1834 param.retry_count = 7;
1835 param.rnr_retry_count = 7;
1836 param.private_data = &priv;
1837 param.private_data_len = sizeof(priv);
1839 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1840 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1842 * set the admin queue depth to the minimum size
1843 * specified by the Fabrics standard.
1845 if (priv.qid == 0) {
1846 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1847 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
1850 * current interpretation of the fabrics spec
1851 * is at minimum you make hrqsize sqsize+1, or a
1852 * 1's based representation of sqsize.
1854 priv.hrqsize = cpu_to_le16(queue->queue_size);
1855 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1858 ret = rdma_connect_locked(queue->cm_id, ¶m);
1860 dev_err(ctrl->ctrl.device,
1861 "rdma_connect_locked failed (%d).\n", ret);
1868 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1869 struct rdma_cm_event *ev)
1871 struct nvme_rdma_queue *queue = cm_id->context;
1874 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1875 rdma_event_msg(ev->event), ev->event,
1878 switch (ev->event) {
1879 case RDMA_CM_EVENT_ADDR_RESOLVED:
1880 cm_error = nvme_rdma_addr_resolved(queue);
1882 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1883 cm_error = nvme_rdma_route_resolved(queue);
1885 case RDMA_CM_EVENT_ESTABLISHED:
1886 queue->cm_error = nvme_rdma_conn_established(queue);
1887 /* complete cm_done regardless of success/failure */
1888 complete(&queue->cm_done);
1890 case RDMA_CM_EVENT_REJECTED:
1891 cm_error = nvme_rdma_conn_rejected(queue, ev);
1893 case RDMA_CM_EVENT_ROUTE_ERROR:
1894 case RDMA_CM_EVENT_CONNECT_ERROR:
1895 case RDMA_CM_EVENT_UNREACHABLE:
1896 case RDMA_CM_EVENT_ADDR_ERROR:
1897 dev_dbg(queue->ctrl->ctrl.device,
1898 "CM error event %d\n", ev->event);
1899 cm_error = -ECONNRESET;
1901 case RDMA_CM_EVENT_DISCONNECTED:
1902 case RDMA_CM_EVENT_ADDR_CHANGE:
1903 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1904 dev_dbg(queue->ctrl->ctrl.device,
1905 "disconnect received - connection closed\n");
1906 nvme_rdma_error_recovery(queue->ctrl);
1908 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1909 /* device removal is handled via the ib_client API */
1912 dev_err(queue->ctrl->ctrl.device,
1913 "Unexpected RDMA CM event (%d)\n", ev->event);
1914 nvme_rdma_error_recovery(queue->ctrl);
1919 queue->cm_error = cm_error;
1920 complete(&queue->cm_done);
1926 static void nvme_rdma_complete_timed_out(struct request *rq)
1928 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1929 struct nvme_rdma_queue *queue = req->queue;
1931 nvme_rdma_stop_queue(queue);
1932 nvmf_complete_timed_out_request(rq);
1935 static enum blk_eh_timer_return nvme_rdma_timeout(struct request *rq)
1937 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1938 struct nvme_rdma_queue *queue = req->queue;
1939 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1941 dev_warn(ctrl->ctrl.device, "I/O %d QID %d timeout\n",
1942 rq->tag, nvme_rdma_queue_idx(queue));
1944 if (ctrl->ctrl.state != NVME_CTRL_LIVE) {
1946 * If we are resetting, connecting or deleting we should
1947 * complete immediately because we may block controller
1948 * teardown or setup sequence
1949 * - ctrl disable/shutdown fabrics requests
1950 * - connect requests
1951 * - initialization admin requests
1952 * - I/O requests that entered after unquiescing and
1953 * the controller stopped responding
1955 * All other requests should be cancelled by the error
1956 * recovery work, so it's fine that we fail it here.
1958 nvme_rdma_complete_timed_out(rq);
1963 * LIVE state should trigger the normal error recovery which will
1964 * handle completing this request.
1966 nvme_rdma_error_recovery(ctrl);
1967 return BLK_EH_RESET_TIMER;
1970 static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1971 const struct blk_mq_queue_data *bd)
1973 struct nvme_ns *ns = hctx->queue->queuedata;
1974 struct nvme_rdma_queue *queue = hctx->driver_data;
1975 struct request *rq = bd->rq;
1976 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1977 struct nvme_rdma_qe *sqe = &req->sqe;
1978 struct nvme_command *c = nvme_req(rq)->cmd;
1979 struct ib_device *dev;
1980 bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags);
1984 WARN_ON_ONCE(rq->tag < 0);
1986 if (!nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
1987 return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
1989 dev = queue->device->dev;
1991 req->sqe.dma = ib_dma_map_single(dev, req->sqe.data,
1992 sizeof(struct nvme_command),
1994 err = ib_dma_mapping_error(dev, req->sqe.dma);
1996 return BLK_STS_RESOURCE;
1998 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1999 sizeof(struct nvme_command), DMA_TO_DEVICE);
2001 ret = nvme_setup_cmd(ns, rq);
2005 nvme_start_request(rq);
2007 if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) &&
2008 queue->pi_support &&
2009 (c->common.opcode == nvme_cmd_write ||
2010 c->common.opcode == nvme_cmd_read) &&
2012 req->use_sig_mr = true;
2014 req->use_sig_mr = false;
2016 err = nvme_rdma_map_data(queue, rq, c);
2017 if (unlikely(err < 0)) {
2018 dev_err(queue->ctrl->ctrl.device,
2019 "Failed to map data (%d)\n", err);
2023 sqe->cqe.done = nvme_rdma_send_done;
2025 ib_dma_sync_single_for_device(dev, sqe->dma,
2026 sizeof(struct nvme_command), DMA_TO_DEVICE);
2028 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
2029 req->mr ? &req->reg_wr.wr : NULL);
2036 nvme_rdma_unmap_data(queue, rq);
2039 ret = nvme_host_path_error(rq);
2040 else if (err == -ENOMEM || err == -EAGAIN)
2041 ret = BLK_STS_RESOURCE;
2043 ret = BLK_STS_IOERR;
2044 nvme_cleanup_cmd(rq);
2046 ib_dma_unmap_single(dev, req->sqe.dma, sizeof(struct nvme_command),
2051 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
2053 struct nvme_rdma_queue *queue = hctx->driver_data;
2055 return ib_process_cq_direct(queue->ib_cq, -1);
2058 static void nvme_rdma_check_pi_status(struct nvme_rdma_request *req)
2060 struct request *rq = blk_mq_rq_from_pdu(req);
2061 struct ib_mr_status mr_status;
2064 ret = ib_check_mr_status(req->mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
2066 pr_err("ib_check_mr_status failed, ret %d\n", ret);
2067 nvme_req(rq)->status = NVME_SC_INVALID_PI;
2071 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
2072 switch (mr_status.sig_err.err_type) {
2073 case IB_SIG_BAD_GUARD:
2074 nvme_req(rq)->status = NVME_SC_GUARD_CHECK;
2076 case IB_SIG_BAD_REFTAG:
2077 nvme_req(rq)->status = NVME_SC_REFTAG_CHECK;
2079 case IB_SIG_BAD_APPTAG:
2080 nvme_req(rq)->status = NVME_SC_APPTAG_CHECK;
2083 pr_err("PI error found type %d expected 0x%x vs actual 0x%x\n",
2084 mr_status.sig_err.err_type, mr_status.sig_err.expected,
2085 mr_status.sig_err.actual);
2089 static void nvme_rdma_complete_rq(struct request *rq)
2091 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
2092 struct nvme_rdma_queue *queue = req->queue;
2093 struct ib_device *ibdev = queue->device->dev;
2095 if (req->use_sig_mr)
2096 nvme_rdma_check_pi_status(req);
2098 nvme_rdma_unmap_data(queue, rq);
2099 ib_dma_unmap_single(ibdev, req->sqe.dma, sizeof(struct nvme_command),
2101 nvme_complete_rq(rq);
2104 static void nvme_rdma_map_queues(struct blk_mq_tag_set *set)
2106 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(set->driver_data);
2108 nvmf_map_queues(set, &ctrl->ctrl, ctrl->io_queues);
2111 static const struct blk_mq_ops nvme_rdma_mq_ops = {
2112 .queue_rq = nvme_rdma_queue_rq,
2113 .complete = nvme_rdma_complete_rq,
2114 .init_request = nvme_rdma_init_request,
2115 .exit_request = nvme_rdma_exit_request,
2116 .init_hctx = nvme_rdma_init_hctx,
2117 .timeout = nvme_rdma_timeout,
2118 .map_queues = nvme_rdma_map_queues,
2119 .poll = nvme_rdma_poll,
2122 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
2123 .queue_rq = nvme_rdma_queue_rq,
2124 .complete = nvme_rdma_complete_rq,
2125 .init_request = nvme_rdma_init_request,
2126 .exit_request = nvme_rdma_exit_request,
2127 .init_hctx = nvme_rdma_init_admin_hctx,
2128 .timeout = nvme_rdma_timeout,
2131 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
2133 nvme_rdma_teardown_io_queues(ctrl, shutdown);
2134 nvme_quiesce_admin_queue(&ctrl->ctrl);
2135 nvme_disable_ctrl(&ctrl->ctrl, shutdown);
2136 nvme_rdma_teardown_admin_queue(ctrl, shutdown);
2139 static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
2141 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true);
2144 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
2146 struct nvme_rdma_ctrl *ctrl =
2147 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
2149 nvme_stop_ctrl(&ctrl->ctrl);
2150 nvme_rdma_shutdown_ctrl(ctrl, false);
2152 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
2153 /* state change failure should never happen */
2158 if (nvme_rdma_setup_ctrl(ctrl, false))
2164 ++ctrl->ctrl.nr_reconnects;
2165 nvme_rdma_reconnect_or_remove(ctrl);
2168 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
2170 .module = THIS_MODULE,
2171 .flags = NVME_F_FABRICS | NVME_F_METADATA_SUPPORTED,
2172 .reg_read32 = nvmf_reg_read32,
2173 .reg_read64 = nvmf_reg_read64,
2174 .reg_write32 = nvmf_reg_write32,
2175 .free_ctrl = nvme_rdma_free_ctrl,
2176 .submit_async_event = nvme_rdma_submit_async_event,
2177 .delete_ctrl = nvme_rdma_delete_ctrl,
2178 .get_address = nvmf_get_address,
2179 .stop_ctrl = nvme_rdma_stop_ctrl,
2183 * Fails a connection request if it matches an existing controller
2184 * (association) with the same tuple:
2185 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
2187 * if local address is not specified in the request, it will match an
2188 * existing controller with all the other parameters the same and no
2189 * local port address specified as well.
2191 * The ports don't need to be compared as they are intrinsically
2192 * already matched by the port pointers supplied.
2195 nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts)
2197 struct nvme_rdma_ctrl *ctrl;
2200 mutex_lock(&nvme_rdma_ctrl_mutex);
2201 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2202 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
2206 mutex_unlock(&nvme_rdma_ctrl_mutex);
2211 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
2212 struct nvmf_ctrl_options *opts)
2214 struct nvme_rdma_ctrl *ctrl;
2218 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2220 return ERR_PTR(-ENOMEM);
2221 ctrl->ctrl.opts = opts;
2222 INIT_LIST_HEAD(&ctrl->list);
2224 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
2226 kstrdup(__stringify(NVME_RDMA_IP_PORT), GFP_KERNEL);
2227 if (!opts->trsvcid) {
2231 opts->mask |= NVMF_OPT_TRSVCID;
2234 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2235 opts->traddr, opts->trsvcid, &ctrl->addr);
2237 pr_err("malformed address passed: %s:%s\n",
2238 opts->traddr, opts->trsvcid);
2242 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
2243 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2244 opts->host_traddr, NULL, &ctrl->src_addr);
2246 pr_err("malformed src address passed: %s\n",
2252 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) {
2257 INIT_DELAYED_WORK(&ctrl->reconnect_work,
2258 nvme_rdma_reconnect_ctrl_work);
2259 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
2260 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
2262 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
2263 opts->nr_poll_queues + 1;
2264 ctrl->ctrl.sqsize = opts->queue_size - 1;
2265 ctrl->ctrl.kato = opts->kato;
2268 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
2273 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
2274 0 /* no quirks, we're perfect! */);
2276 goto out_kfree_queues;
2278 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING);
2279 WARN_ON_ONCE(!changed);
2281 ret = nvme_rdma_setup_ctrl(ctrl, true);
2283 goto out_uninit_ctrl;
2285 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
2286 nvmf_ctrl_subsysnqn(&ctrl->ctrl), &ctrl->addr);
2288 mutex_lock(&nvme_rdma_ctrl_mutex);
2289 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
2290 mutex_unlock(&nvme_rdma_ctrl_mutex);
2295 nvme_uninit_ctrl(&ctrl->ctrl);
2296 nvme_put_ctrl(&ctrl->ctrl);
2299 return ERR_PTR(ret);
2301 kfree(ctrl->queues);
2304 return ERR_PTR(ret);
2307 static struct nvmf_transport_ops nvme_rdma_transport = {
2309 .module = THIS_MODULE,
2310 .required_opts = NVMF_OPT_TRADDR,
2311 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2312 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
2313 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
2315 .create_ctrl = nvme_rdma_create_ctrl,
2318 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2320 struct nvme_rdma_ctrl *ctrl;
2321 struct nvme_rdma_device *ndev;
2324 mutex_lock(&device_list_mutex);
2325 list_for_each_entry(ndev, &device_list, entry) {
2326 if (ndev->dev == ib_device) {
2331 mutex_unlock(&device_list_mutex);
2336 /* Delete all controllers using this device */
2337 mutex_lock(&nvme_rdma_ctrl_mutex);
2338 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2339 if (ctrl->device->dev != ib_device)
2341 nvme_delete_ctrl(&ctrl->ctrl);
2343 mutex_unlock(&nvme_rdma_ctrl_mutex);
2345 flush_workqueue(nvme_delete_wq);
2348 static struct ib_client nvme_rdma_ib_client = {
2349 .name = "nvme_rdma",
2350 .remove = nvme_rdma_remove_one
2353 static int __init nvme_rdma_init_module(void)
2357 ret = ib_register_client(&nvme_rdma_ib_client);
2361 ret = nvmf_register_transport(&nvme_rdma_transport);
2363 goto err_unreg_client;
2368 ib_unregister_client(&nvme_rdma_ib_client);
2372 static void __exit nvme_rdma_cleanup_module(void)
2374 struct nvme_rdma_ctrl *ctrl;
2376 nvmf_unregister_transport(&nvme_rdma_transport);
2377 ib_unregister_client(&nvme_rdma_ib_client);
2379 mutex_lock(&nvme_rdma_ctrl_mutex);
2380 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list)
2381 nvme_delete_ctrl(&ctrl->ctrl);
2382 mutex_unlock(&nvme_rdma_ctrl_mutex);
2383 flush_workqueue(nvme_delete_wq);
2386 module_init(nvme_rdma_init_module);
2387 module_exit(nvme_rdma_cleanup_module);
2389 MODULE_LICENSE("GPL v2");