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/blk-integrity.h>
17 #include <linux/types.h>
18 #include <linux/list.h>
19 #include <linux/mutex.h>
20 #include <linux/scatterlist.h>
21 #include <linux/nvme.h>
22 #include <asm/unaligned.h>
24 #include <rdma/ib_verbs.h>
25 #include <rdma/rdma_cm.h>
26 #include <linux/nvme-rdma.h>
32 #define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
34 #define NVME_RDMA_MAX_SEGMENTS 256
36 #define NVME_RDMA_MAX_INLINE_SEGMENTS 4
38 #define NVME_RDMA_DATA_SGL_SIZE \
39 (sizeof(struct scatterlist) * NVME_INLINE_SG_CNT)
40 #define NVME_RDMA_METADATA_SGL_SIZE \
41 (sizeof(struct scatterlist) * NVME_INLINE_METADATA_SG_CNT)
43 struct nvme_rdma_device {
44 struct ib_device *dev;
47 struct list_head entry;
48 unsigned int num_inline_segments;
57 struct nvme_rdma_sgl {
59 struct sg_table sg_table;
62 struct nvme_rdma_queue;
63 struct nvme_rdma_request {
64 struct nvme_request req;
66 struct nvme_rdma_qe sqe;
67 union nvme_result result;
70 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
72 struct ib_reg_wr reg_wr;
73 struct ib_cqe reg_cqe;
74 struct nvme_rdma_queue *queue;
75 struct nvme_rdma_sgl data_sgl;
76 struct nvme_rdma_sgl *metadata_sgl;
80 enum nvme_rdma_queue_flags {
81 NVME_RDMA_Q_ALLOCATED = 0,
83 NVME_RDMA_Q_TR_READY = 2,
86 struct nvme_rdma_queue {
87 struct nvme_rdma_qe *rsp_ring;
89 size_t cmnd_capsule_len;
90 struct nvme_rdma_ctrl *ctrl;
91 struct nvme_rdma_device *device;
96 struct rdma_cm_id *cm_id;
98 struct completion cm_done;
101 struct mutex queue_lock;
104 struct nvme_rdma_ctrl {
105 /* read only in the hot path */
106 struct nvme_rdma_queue *queues;
108 /* other member variables */
109 struct blk_mq_tag_set tag_set;
110 struct work_struct err_work;
112 struct nvme_rdma_qe async_event_sqe;
114 struct delayed_work reconnect_work;
116 struct list_head list;
118 struct blk_mq_tag_set admin_tag_set;
119 struct nvme_rdma_device *device;
123 struct sockaddr_storage addr;
124 struct sockaddr_storage src_addr;
126 struct nvme_ctrl ctrl;
127 bool use_inline_data;
128 u32 io_queues[HCTX_MAX_TYPES];
131 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
133 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
136 static LIST_HEAD(device_list);
137 static DEFINE_MUTEX(device_list_mutex);
139 static LIST_HEAD(nvme_rdma_ctrl_list);
140 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
143 * Disabling this option makes small I/O goes faster, but is fundamentally
144 * unsafe. With it turned off we will have to register a global rkey that
145 * allows read and write access to all physical memory.
147 static bool register_always = true;
148 module_param(register_always, bool, 0444);
149 MODULE_PARM_DESC(register_always,
150 "Use memory registration even for contiguous memory regions");
152 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
153 struct rdma_cm_event *event);
154 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
155 static void nvme_rdma_complete_rq(struct request *rq);
157 static const struct blk_mq_ops nvme_rdma_mq_ops;
158 static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
160 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
162 return queue - queue->ctrl->queues;
165 static bool nvme_rdma_poll_queue(struct nvme_rdma_queue *queue)
167 return nvme_rdma_queue_idx(queue) >
168 queue->ctrl->io_queues[HCTX_TYPE_DEFAULT] +
169 queue->ctrl->io_queues[HCTX_TYPE_READ];
172 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
174 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
177 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
178 size_t capsule_size, enum dma_data_direction dir)
180 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
184 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
185 size_t capsule_size, enum dma_data_direction dir)
187 qe->data = kzalloc(capsule_size, GFP_KERNEL);
191 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
192 if (ib_dma_mapping_error(ibdev, qe->dma)) {
201 static void nvme_rdma_free_ring(struct ib_device *ibdev,
202 struct nvme_rdma_qe *ring, size_t ib_queue_size,
203 size_t capsule_size, enum dma_data_direction dir)
207 for (i = 0; i < ib_queue_size; i++)
208 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
212 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
213 size_t ib_queue_size, size_t capsule_size,
214 enum dma_data_direction dir)
216 struct nvme_rdma_qe *ring;
219 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
224 * Bind the CQEs (post recv buffers) DMA mapping to the RDMA queue
225 * lifetime. It's safe, since any chage in the underlying RDMA device
226 * will issue error recovery and queue re-creation.
228 for (i = 0; i < ib_queue_size; i++) {
229 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
236 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
240 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
242 pr_debug("QP event %s (%d)\n",
243 ib_event_msg(event->event), event->event);
247 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
251 ret = wait_for_completion_interruptible_timeout(&queue->cm_done,
252 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
257 WARN_ON_ONCE(queue->cm_error > 0);
258 return queue->cm_error;
261 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
263 struct nvme_rdma_device *dev = queue->device;
264 struct ib_qp_init_attr init_attr;
267 memset(&init_attr, 0, sizeof(init_attr));
268 init_attr.event_handler = nvme_rdma_qp_event;
270 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
272 init_attr.cap.max_recv_wr = queue->queue_size + 1;
273 init_attr.cap.max_recv_sge = 1;
274 init_attr.cap.max_send_sge = 1 + dev->num_inline_segments;
275 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
276 init_attr.qp_type = IB_QPT_RC;
277 init_attr.send_cq = queue->ib_cq;
278 init_attr.recv_cq = queue->ib_cq;
279 if (queue->pi_support)
280 init_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN;
281 init_attr.qp_context = queue;
283 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
285 queue->qp = queue->cm_id->qp;
289 static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
290 struct request *rq, unsigned int hctx_idx)
292 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
294 kfree(req->sqe.data);
297 static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
298 struct request *rq, unsigned int hctx_idx,
299 unsigned int numa_node)
301 struct nvme_rdma_ctrl *ctrl = set->driver_data;
302 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
303 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
304 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
306 nvme_req(rq)->ctrl = &ctrl->ctrl;
307 req->sqe.data = kzalloc(sizeof(struct nvme_command), GFP_KERNEL);
311 /* metadata nvme_rdma_sgl struct is located after command's data SGL */
312 if (queue->pi_support)
313 req->metadata_sgl = (void *)nvme_req(rq) +
314 sizeof(struct nvme_rdma_request) +
315 NVME_RDMA_DATA_SGL_SIZE;
318 nvme_req(rq)->cmd = req->sqe.data;
323 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
324 unsigned int hctx_idx)
326 struct nvme_rdma_ctrl *ctrl = data;
327 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
329 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
331 hctx->driver_data = queue;
335 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
336 unsigned int hctx_idx)
338 struct nvme_rdma_ctrl *ctrl = data;
339 struct nvme_rdma_queue *queue = &ctrl->queues[0];
341 BUG_ON(hctx_idx != 0);
343 hctx->driver_data = queue;
347 static void nvme_rdma_free_dev(struct kref *ref)
349 struct nvme_rdma_device *ndev =
350 container_of(ref, struct nvme_rdma_device, ref);
352 mutex_lock(&device_list_mutex);
353 list_del(&ndev->entry);
354 mutex_unlock(&device_list_mutex);
356 ib_dealloc_pd(ndev->pd);
360 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
362 kref_put(&dev->ref, nvme_rdma_free_dev);
365 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
367 return kref_get_unless_zero(&dev->ref);
370 static struct nvme_rdma_device *
371 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
373 struct nvme_rdma_device *ndev;
375 mutex_lock(&device_list_mutex);
376 list_for_each_entry(ndev, &device_list, entry) {
377 if (ndev->dev->node_guid == cm_id->device->node_guid &&
378 nvme_rdma_dev_get(ndev))
382 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
386 ndev->dev = cm_id->device;
387 kref_init(&ndev->ref);
389 ndev->pd = ib_alloc_pd(ndev->dev,
390 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
391 if (IS_ERR(ndev->pd))
394 if (!(ndev->dev->attrs.device_cap_flags &
395 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
396 dev_err(&ndev->dev->dev,
397 "Memory registrations not supported.\n");
401 ndev->num_inline_segments = min(NVME_RDMA_MAX_INLINE_SEGMENTS,
402 ndev->dev->attrs.max_send_sge - 1);
403 list_add(&ndev->entry, &device_list);
405 mutex_unlock(&device_list_mutex);
409 ib_dealloc_pd(ndev->pd);
413 mutex_unlock(&device_list_mutex);
417 static void nvme_rdma_free_cq(struct nvme_rdma_queue *queue)
419 if (nvme_rdma_poll_queue(queue))
420 ib_free_cq(queue->ib_cq);
422 ib_cq_pool_put(queue->ib_cq, queue->cq_size);
425 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
427 struct nvme_rdma_device *dev;
428 struct ib_device *ibdev;
430 if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags))
436 if (queue->pi_support)
437 ib_mr_pool_destroy(queue->qp, &queue->qp->sig_mrs);
438 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
441 * The cm_id object might have been destroyed during RDMA connection
442 * establishment error flow to avoid getting other cma events, thus
443 * the destruction of the QP shouldn't use rdma_cm API.
445 ib_destroy_qp(queue->qp);
446 nvme_rdma_free_cq(queue);
448 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
449 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
451 nvme_rdma_dev_put(dev);
454 static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev, bool pi_support)
456 u32 max_page_list_len;
459 max_page_list_len = ibdev->attrs.max_pi_fast_reg_page_list_len;
461 max_page_list_len = ibdev->attrs.max_fast_reg_page_list_len;
463 return min_t(u32, NVME_RDMA_MAX_SEGMENTS, max_page_list_len - 1);
466 static int nvme_rdma_create_cq(struct ib_device *ibdev,
467 struct nvme_rdma_queue *queue)
469 int ret, comp_vector, idx = nvme_rdma_queue_idx(queue);
470 enum ib_poll_context poll_ctx;
473 * Spread I/O queues completion vectors according their queue index.
474 * Admin queues can always go on completion vector 0.
476 comp_vector = (idx == 0 ? idx : idx - 1) % ibdev->num_comp_vectors;
478 /* Polling queues need direct cq polling context */
479 if (nvme_rdma_poll_queue(queue)) {
480 poll_ctx = IB_POLL_DIRECT;
481 queue->ib_cq = ib_alloc_cq(ibdev, queue, queue->cq_size,
482 comp_vector, poll_ctx);
484 poll_ctx = IB_POLL_SOFTIRQ;
485 queue->ib_cq = ib_cq_pool_get(ibdev, queue->cq_size,
486 comp_vector, poll_ctx);
489 if (IS_ERR(queue->ib_cq)) {
490 ret = PTR_ERR(queue->ib_cq);
497 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
499 struct ib_device *ibdev;
500 const int send_wr_factor = 3; /* MR, SEND, INV */
501 const int cq_factor = send_wr_factor + 1; /* + RECV */
502 int ret, pages_per_mr;
504 queue->device = nvme_rdma_find_get_device(queue->cm_id);
505 if (!queue->device) {
506 dev_err(queue->cm_id->device->dev.parent,
507 "no client data found!\n");
508 return -ECONNREFUSED;
510 ibdev = queue->device->dev;
512 /* +1 for ib_stop_cq */
513 queue->cq_size = cq_factor * queue->queue_size + 1;
515 ret = nvme_rdma_create_cq(ibdev, queue);
519 ret = nvme_rdma_create_qp(queue, send_wr_factor);
521 goto out_destroy_ib_cq;
523 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
524 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
525 if (!queue->rsp_ring) {
531 * Currently we don't use SG_GAPS MR's so if the first entry is
532 * misaligned we'll end up using two entries for a single data page,
533 * so one additional entry is required.
535 pages_per_mr = nvme_rdma_get_max_fr_pages(ibdev, queue->pi_support) + 1;
536 ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs,
541 dev_err(queue->ctrl->ctrl.device,
542 "failed to initialize MR pool sized %d for QID %d\n",
543 queue->queue_size, nvme_rdma_queue_idx(queue));
544 goto out_destroy_ring;
547 if (queue->pi_support) {
548 ret = ib_mr_pool_init(queue->qp, &queue->qp->sig_mrs,
549 queue->queue_size, IB_MR_TYPE_INTEGRITY,
550 pages_per_mr, pages_per_mr);
552 dev_err(queue->ctrl->ctrl.device,
553 "failed to initialize PI MR pool sized %d for QID %d\n",
554 queue->queue_size, nvme_rdma_queue_idx(queue));
555 goto out_destroy_mr_pool;
559 set_bit(NVME_RDMA_Q_TR_READY, &queue->flags);
564 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
566 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
567 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
569 rdma_destroy_qp(queue->cm_id);
571 nvme_rdma_free_cq(queue);
573 nvme_rdma_dev_put(queue->device);
577 static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
578 int idx, size_t queue_size)
580 struct nvme_rdma_queue *queue;
581 struct sockaddr *src_addr = NULL;
584 queue = &ctrl->queues[idx];
585 mutex_init(&queue->queue_lock);
587 if (idx && ctrl->ctrl.max_integrity_segments)
588 queue->pi_support = true;
590 queue->pi_support = false;
591 init_completion(&queue->cm_done);
594 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
596 queue->cmnd_capsule_len = sizeof(struct nvme_command);
598 queue->queue_size = queue_size;
600 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
601 RDMA_PS_TCP, IB_QPT_RC);
602 if (IS_ERR(queue->cm_id)) {
603 dev_info(ctrl->ctrl.device,
604 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
605 ret = PTR_ERR(queue->cm_id);
606 goto out_destroy_mutex;
609 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
610 src_addr = (struct sockaddr *)&ctrl->src_addr;
612 queue->cm_error = -ETIMEDOUT;
613 ret = rdma_resolve_addr(queue->cm_id, src_addr,
614 (struct sockaddr *)&ctrl->addr,
615 NVME_RDMA_CONNECT_TIMEOUT_MS);
617 dev_info(ctrl->ctrl.device,
618 "rdma_resolve_addr failed (%d).\n", ret);
619 goto out_destroy_cm_id;
622 ret = nvme_rdma_wait_for_cm(queue);
624 dev_info(ctrl->ctrl.device,
625 "rdma connection establishment failed (%d)\n", ret);
626 goto out_destroy_cm_id;
629 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
634 rdma_destroy_id(queue->cm_id);
635 nvme_rdma_destroy_queue_ib(queue);
637 mutex_destroy(&queue->queue_lock);
641 static void __nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
643 rdma_disconnect(queue->cm_id);
644 ib_drain_qp(queue->qp);
647 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
649 mutex_lock(&queue->queue_lock);
650 if (test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
651 __nvme_rdma_stop_queue(queue);
652 mutex_unlock(&queue->queue_lock);
655 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
657 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
660 rdma_destroy_id(queue->cm_id);
661 nvme_rdma_destroy_queue_ib(queue);
662 mutex_destroy(&queue->queue_lock);
665 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
669 for (i = 1; i < ctrl->ctrl.queue_count; i++)
670 nvme_rdma_free_queue(&ctrl->queues[i]);
673 static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
677 for (i = 1; i < ctrl->ctrl.queue_count; i++)
678 nvme_rdma_stop_queue(&ctrl->queues[i]);
681 static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
683 struct nvme_rdma_queue *queue = &ctrl->queues[idx];
687 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx);
689 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
692 set_bit(NVME_RDMA_Q_LIVE, &queue->flags);
694 if (test_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
695 __nvme_rdma_stop_queue(queue);
696 dev_info(ctrl->ctrl.device,
697 "failed to connect queue: %d ret=%d\n", idx, ret);
702 static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
706 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
707 ret = nvme_rdma_start_queue(ctrl, i);
709 goto out_stop_queues;
715 for (i--; i >= 1; i--)
716 nvme_rdma_stop_queue(&ctrl->queues[i]);
720 static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
722 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
723 struct ib_device *ibdev = ctrl->device->dev;
724 unsigned int nr_io_queues, nr_default_queues;
725 unsigned int nr_read_queues, nr_poll_queues;
728 nr_read_queues = min_t(unsigned int, ibdev->num_comp_vectors,
729 min(opts->nr_io_queues, num_online_cpus()));
730 nr_default_queues = min_t(unsigned int, ibdev->num_comp_vectors,
731 min(opts->nr_write_queues, num_online_cpus()));
732 nr_poll_queues = min(opts->nr_poll_queues, num_online_cpus());
733 nr_io_queues = nr_read_queues + nr_default_queues + nr_poll_queues;
735 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
739 if (nr_io_queues == 0) {
740 dev_err(ctrl->ctrl.device,
741 "unable to set any I/O queues\n");
745 ctrl->ctrl.queue_count = nr_io_queues + 1;
746 dev_info(ctrl->ctrl.device,
747 "creating %d I/O queues.\n", nr_io_queues);
749 if (opts->nr_write_queues && nr_read_queues < nr_io_queues) {
751 * separate read/write queues
752 * hand out dedicated default queues only after we have
753 * sufficient read queues.
755 ctrl->io_queues[HCTX_TYPE_READ] = nr_read_queues;
756 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_READ];
757 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
758 min(nr_default_queues, nr_io_queues);
759 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
762 * shared read/write queues
763 * either no write queues were requested, or we don't have
764 * sufficient queue count to have dedicated default queues.
766 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
767 min(nr_read_queues, nr_io_queues);
768 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
771 if (opts->nr_poll_queues && nr_io_queues) {
772 /* map dedicated poll queues only if we have queues left */
773 ctrl->io_queues[HCTX_TYPE_POLL] =
774 min(nr_poll_queues, nr_io_queues);
777 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
778 ret = nvme_rdma_alloc_queue(ctrl, i,
779 ctrl->ctrl.sqsize + 1);
781 goto out_free_queues;
787 for (i--; i >= 1; i--)
788 nvme_rdma_free_queue(&ctrl->queues[i]);
793 static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
796 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
797 struct blk_mq_tag_set *set;
801 set = &ctrl->admin_tag_set;
802 memset(set, 0, sizeof(*set));
803 set->ops = &nvme_rdma_admin_mq_ops;
804 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
805 set->reserved_tags = NVMF_RESERVED_TAGS;
806 set->numa_node = nctrl->numa_node;
807 set->cmd_size = sizeof(struct nvme_rdma_request) +
808 NVME_RDMA_DATA_SGL_SIZE;
809 set->driver_data = ctrl;
810 set->nr_hw_queues = 1;
811 set->timeout = NVME_ADMIN_TIMEOUT;
812 set->flags = BLK_MQ_F_NO_SCHED;
814 set = &ctrl->tag_set;
815 memset(set, 0, sizeof(*set));
816 set->ops = &nvme_rdma_mq_ops;
817 set->queue_depth = nctrl->sqsize + 1;
818 set->reserved_tags = NVMF_RESERVED_TAGS;
819 set->numa_node = nctrl->numa_node;
820 set->flags = BLK_MQ_F_SHOULD_MERGE;
821 set->cmd_size = sizeof(struct nvme_rdma_request) +
822 NVME_RDMA_DATA_SGL_SIZE;
823 if (nctrl->max_integrity_segments)
824 set->cmd_size += sizeof(struct nvme_rdma_sgl) +
825 NVME_RDMA_METADATA_SGL_SIZE;
826 set->driver_data = ctrl;
827 set->nr_hw_queues = nctrl->queue_count - 1;
828 set->timeout = NVME_IO_TIMEOUT;
829 set->nr_maps = nctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2;
832 ret = blk_mq_alloc_tag_set(set);
839 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
843 blk_mq_destroy_queue(ctrl->ctrl.admin_q);
844 blk_mq_destroy_queue(ctrl->ctrl.fabrics_q);
845 blk_mq_free_tag_set(ctrl->ctrl.admin_tagset);
847 if (ctrl->async_event_sqe.data) {
848 cancel_work_sync(&ctrl->ctrl.async_event_work);
849 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
850 sizeof(struct nvme_command), DMA_TO_DEVICE);
851 ctrl->async_event_sqe.data = NULL;
853 nvme_rdma_free_queue(&ctrl->queues[0]);
856 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
859 bool pi_capable = false;
862 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
866 ctrl->device = ctrl->queues[0].device;
867 ctrl->ctrl.numa_node = ibdev_to_node(ctrl->device->dev);
870 if (ctrl->device->dev->attrs.kernel_cap_flags &
871 IBK_INTEGRITY_HANDOVER)
874 ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev,
878 * Bind the async event SQE DMA mapping to the admin queue lifetime.
879 * It's safe, since any chage in the underlying RDMA device will issue
880 * error recovery and queue re-creation.
882 error = nvme_rdma_alloc_qe(ctrl->device->dev, &ctrl->async_event_sqe,
883 sizeof(struct nvme_command), DMA_TO_DEVICE);
888 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
889 if (IS_ERR(ctrl->ctrl.admin_tagset)) {
890 error = PTR_ERR(ctrl->ctrl.admin_tagset);
891 goto out_free_async_qe;
894 ctrl->ctrl.fabrics_q = blk_mq_init_queue(&ctrl->admin_tag_set);
895 if (IS_ERR(ctrl->ctrl.fabrics_q)) {
896 error = PTR_ERR(ctrl->ctrl.fabrics_q);
897 goto out_free_tagset;
900 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
901 if (IS_ERR(ctrl->ctrl.admin_q)) {
902 error = PTR_ERR(ctrl->ctrl.admin_q);
903 goto out_cleanup_fabrics_q;
907 error = nvme_rdma_start_queue(ctrl, 0);
909 goto out_cleanup_queue;
911 error = nvme_enable_ctrl(&ctrl->ctrl);
915 ctrl->ctrl.max_segments = ctrl->max_fr_pages;
916 ctrl->ctrl.max_hw_sectors = ctrl->max_fr_pages << (ilog2(SZ_4K) - 9);
918 ctrl->ctrl.max_integrity_segments = ctrl->max_fr_pages;
920 ctrl->ctrl.max_integrity_segments = 0;
922 nvme_start_admin_queue(&ctrl->ctrl);
924 error = nvme_init_ctrl_finish(&ctrl->ctrl);
926 goto out_quiesce_queue;
931 nvme_stop_admin_queue(&ctrl->ctrl);
932 blk_sync_queue(ctrl->ctrl.admin_q);
934 nvme_rdma_stop_queue(&ctrl->queues[0]);
935 nvme_cancel_admin_tagset(&ctrl->ctrl);
938 blk_mq_destroy_queue(ctrl->ctrl.admin_q);
939 out_cleanup_fabrics_q:
941 blk_mq_destroy_queue(ctrl->ctrl.fabrics_q);
944 blk_mq_free_tag_set(ctrl->ctrl.admin_tagset);
946 if (ctrl->async_event_sqe.data) {
947 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
948 sizeof(struct nvme_command), DMA_TO_DEVICE);
949 ctrl->async_event_sqe.data = NULL;
952 nvme_rdma_free_queue(&ctrl->queues[0]);
956 static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
960 blk_mq_destroy_queue(ctrl->ctrl.connect_q);
961 blk_mq_free_tag_set(ctrl->ctrl.tagset);
963 nvme_rdma_free_io_queues(ctrl);
966 static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
970 ret = nvme_rdma_alloc_io_queues(ctrl);
975 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
976 if (IS_ERR(ctrl->ctrl.tagset)) {
977 ret = PTR_ERR(ctrl->ctrl.tagset);
978 goto out_free_io_queues;
981 ret = nvme_ctrl_init_connect_q(&(ctrl->ctrl));
983 goto out_free_tag_set;
986 ret = nvme_rdma_start_io_queues(ctrl);
988 goto out_cleanup_connect_q;
991 nvme_start_queues(&ctrl->ctrl);
992 if (!nvme_wait_freeze_timeout(&ctrl->ctrl, NVME_IO_TIMEOUT)) {
994 * If we timed out waiting for freeze we are likely to
995 * be stuck. Fail the controller initialization just
999 goto out_wait_freeze_timed_out;
1001 blk_mq_update_nr_hw_queues(ctrl->ctrl.tagset,
1002 ctrl->ctrl.queue_count - 1);
1003 nvme_unfreeze(&ctrl->ctrl);
1008 out_wait_freeze_timed_out:
1009 nvme_stop_queues(&ctrl->ctrl);
1010 nvme_sync_io_queues(&ctrl->ctrl);
1011 nvme_rdma_stop_io_queues(ctrl);
1012 out_cleanup_connect_q:
1013 nvme_cancel_tagset(&ctrl->ctrl);
1015 blk_mq_destroy_queue(ctrl->ctrl.connect_q);
1018 blk_mq_free_tag_set(ctrl->ctrl.tagset);
1020 nvme_rdma_free_io_queues(ctrl);
1024 static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl *ctrl,
1027 nvme_stop_admin_queue(&ctrl->ctrl);
1028 blk_sync_queue(ctrl->ctrl.admin_q);
1029 nvme_rdma_stop_queue(&ctrl->queues[0]);
1030 nvme_cancel_admin_tagset(&ctrl->ctrl);
1032 nvme_start_admin_queue(&ctrl->ctrl);
1033 nvme_rdma_destroy_admin_queue(ctrl, remove);
1036 static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl *ctrl,
1039 if (ctrl->ctrl.queue_count > 1) {
1040 nvme_start_freeze(&ctrl->ctrl);
1041 nvme_stop_queues(&ctrl->ctrl);
1042 nvme_sync_io_queues(&ctrl->ctrl);
1043 nvme_rdma_stop_io_queues(ctrl);
1044 nvme_cancel_tagset(&ctrl->ctrl);
1046 nvme_start_queues(&ctrl->ctrl);
1047 nvme_rdma_destroy_io_queues(ctrl, remove);
1051 static void nvme_rdma_stop_ctrl(struct nvme_ctrl *nctrl)
1053 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1055 cancel_work_sync(&ctrl->err_work);
1056 cancel_delayed_work_sync(&ctrl->reconnect_work);
1059 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
1061 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1063 if (list_empty(&ctrl->list))
1066 mutex_lock(&nvme_rdma_ctrl_mutex);
1067 list_del(&ctrl->list);
1068 mutex_unlock(&nvme_rdma_ctrl_mutex);
1070 nvmf_free_options(nctrl->opts);
1072 kfree(ctrl->queues);
1076 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
1078 /* If we are resetting/deleting then do nothing */
1079 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
1080 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
1081 ctrl->ctrl.state == NVME_CTRL_LIVE);
1085 if (nvmf_should_reconnect(&ctrl->ctrl)) {
1086 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
1087 ctrl->ctrl.opts->reconnect_delay);
1088 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
1089 ctrl->ctrl.opts->reconnect_delay * HZ);
1091 nvme_delete_ctrl(&ctrl->ctrl);
1095 static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new)
1100 ret = nvme_rdma_configure_admin_queue(ctrl, new);
1104 if (ctrl->ctrl.icdoff) {
1106 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1110 if (!(ctrl->ctrl.sgls & (1 << 2))) {
1112 dev_err(ctrl->ctrl.device,
1113 "Mandatory keyed sgls are not supported!\n");
1117 if (ctrl->ctrl.opts->queue_size > ctrl->ctrl.sqsize + 1) {
1118 dev_warn(ctrl->ctrl.device,
1119 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1120 ctrl->ctrl.opts->queue_size, ctrl->ctrl.sqsize + 1);
1123 if (ctrl->ctrl.sqsize + 1 > NVME_RDMA_MAX_QUEUE_SIZE) {
1124 dev_warn(ctrl->ctrl.device,
1125 "ctrl sqsize %u > max queue size %u, clamping down\n",
1126 ctrl->ctrl.sqsize + 1, NVME_RDMA_MAX_QUEUE_SIZE);
1127 ctrl->ctrl.sqsize = NVME_RDMA_MAX_QUEUE_SIZE - 1;
1130 if (ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) {
1131 dev_warn(ctrl->ctrl.device,
1132 "sqsize %u > ctrl maxcmd %u, clamping down\n",
1133 ctrl->ctrl.sqsize + 1, ctrl->ctrl.maxcmd);
1134 ctrl->ctrl.sqsize = ctrl->ctrl.maxcmd - 1;
1137 if (ctrl->ctrl.sgls & (1 << 20))
1138 ctrl->use_inline_data = true;
1140 if (ctrl->ctrl.queue_count > 1) {
1141 ret = nvme_rdma_configure_io_queues(ctrl, new);
1146 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1149 * state change failure is ok if we started ctrl delete,
1150 * unless we're during creation of a new controller to
1151 * avoid races with teardown flow.
1153 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING &&
1154 ctrl->ctrl.state != NVME_CTRL_DELETING_NOIO);
1160 nvme_start_ctrl(&ctrl->ctrl);
1164 if (ctrl->ctrl.queue_count > 1) {
1165 nvme_stop_queues(&ctrl->ctrl);
1166 nvme_sync_io_queues(&ctrl->ctrl);
1167 nvme_rdma_stop_io_queues(ctrl);
1168 nvme_cancel_tagset(&ctrl->ctrl);
1169 nvme_rdma_destroy_io_queues(ctrl, new);
1172 nvme_stop_admin_queue(&ctrl->ctrl);
1173 blk_sync_queue(ctrl->ctrl.admin_q);
1174 nvme_rdma_stop_queue(&ctrl->queues[0]);
1175 nvme_cancel_admin_tagset(&ctrl->ctrl);
1176 nvme_rdma_destroy_admin_queue(ctrl, new);
1180 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
1182 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
1183 struct nvme_rdma_ctrl, reconnect_work);
1185 ++ctrl->ctrl.nr_reconnects;
1187 if (nvme_rdma_setup_ctrl(ctrl, false))
1190 dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
1191 ctrl->ctrl.nr_reconnects);
1193 ctrl->ctrl.nr_reconnects = 0;
1198 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
1199 ctrl->ctrl.nr_reconnects);
1200 nvme_rdma_reconnect_or_remove(ctrl);
1203 static void nvme_rdma_error_recovery_work(struct work_struct *work)
1205 struct nvme_rdma_ctrl *ctrl = container_of(work,
1206 struct nvme_rdma_ctrl, err_work);
1208 nvme_stop_keep_alive(&ctrl->ctrl);
1209 flush_work(&ctrl->ctrl.async_event_work);
1210 nvme_rdma_teardown_io_queues(ctrl, false);
1211 nvme_start_queues(&ctrl->ctrl);
1212 nvme_rdma_teardown_admin_queue(ctrl, false);
1213 nvme_start_admin_queue(&ctrl->ctrl);
1215 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
1216 /* state change failure is ok if we started ctrl delete */
1217 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING &&
1218 ctrl->ctrl.state != NVME_CTRL_DELETING_NOIO);
1222 nvme_rdma_reconnect_or_remove(ctrl);
1225 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
1227 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1230 dev_warn(ctrl->ctrl.device, "starting error recovery\n");
1231 queue_work(nvme_reset_wq, &ctrl->err_work);
1234 static void nvme_rdma_end_request(struct nvme_rdma_request *req)
1236 struct request *rq = blk_mq_rq_from_pdu(req);
1238 if (!refcount_dec_and_test(&req->ref))
1240 if (!nvme_try_complete_req(rq, req->status, req->result))
1241 nvme_rdma_complete_rq(rq);
1244 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1247 struct nvme_rdma_queue *queue = wc->qp->qp_context;
1248 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1250 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1251 dev_info(ctrl->ctrl.device,
1252 "%s for CQE 0x%p failed with status %s (%d)\n",
1254 ib_wc_status_msg(wc->status), wc->status);
1255 nvme_rdma_error_recovery(ctrl);
1258 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1260 if (unlikely(wc->status != IB_WC_SUCCESS))
1261 nvme_rdma_wr_error(cq, wc, "MEMREG");
1264 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1266 struct nvme_rdma_request *req =
1267 container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe);
1269 if (unlikely(wc->status != IB_WC_SUCCESS))
1270 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
1272 nvme_rdma_end_request(req);
1275 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1276 struct nvme_rdma_request *req)
1278 struct ib_send_wr wr = {
1279 .opcode = IB_WR_LOCAL_INV,
1282 .send_flags = IB_SEND_SIGNALED,
1283 .ex.invalidate_rkey = req->mr->rkey,
1286 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1287 wr.wr_cqe = &req->reg_cqe;
1289 return ib_post_send(queue->qp, &wr, NULL);
1292 static void nvme_rdma_dma_unmap_req(struct ib_device *ibdev, struct request *rq)
1294 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1296 if (blk_integrity_rq(rq)) {
1297 ib_dma_unmap_sg(ibdev, req->metadata_sgl->sg_table.sgl,
1298 req->metadata_sgl->nents, rq_dma_dir(rq));
1299 sg_free_table_chained(&req->metadata_sgl->sg_table,
1300 NVME_INLINE_METADATA_SG_CNT);
1303 ib_dma_unmap_sg(ibdev, req->data_sgl.sg_table.sgl, req->data_sgl.nents,
1305 sg_free_table_chained(&req->data_sgl.sg_table, NVME_INLINE_SG_CNT);
1308 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1311 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1312 struct nvme_rdma_device *dev = queue->device;
1313 struct ib_device *ibdev = dev->dev;
1314 struct list_head *pool = &queue->qp->rdma_mrs;
1316 if (!blk_rq_nr_phys_segments(rq))
1319 if (req->use_sig_mr)
1320 pool = &queue->qp->sig_mrs;
1323 ib_mr_pool_put(queue->qp, pool, req->mr);
1327 nvme_rdma_dma_unmap_req(ibdev, rq);
1330 static int nvme_rdma_set_sg_null(struct nvme_command *c)
1332 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1335 put_unaligned_le24(0, sg->length);
1336 put_unaligned_le32(0, sg->key);
1337 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1341 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
1342 struct nvme_rdma_request *req, struct nvme_command *c,
1345 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1346 struct ib_sge *sge = &req->sge[1];
1347 struct scatterlist *sgl;
1351 for_each_sg(req->data_sgl.sg_table.sgl, sgl, count, i) {
1352 sge->addr = sg_dma_address(sgl);
1353 sge->length = sg_dma_len(sgl);
1354 sge->lkey = queue->device->pd->local_dma_lkey;
1359 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1360 sg->length = cpu_to_le32(len);
1361 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1363 req->num_sge += count;
1367 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1368 struct nvme_rdma_request *req, struct nvme_command *c)
1370 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1372 sg->addr = cpu_to_le64(sg_dma_address(req->data_sgl.sg_table.sgl));
1373 put_unaligned_le24(sg_dma_len(req->data_sgl.sg_table.sgl), sg->length);
1374 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
1375 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1379 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1380 struct nvme_rdma_request *req, struct nvme_command *c,
1383 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1386 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs);
1387 if (WARN_ON_ONCE(!req->mr))
1391 * Align the MR to a 4K page size to match the ctrl page size and
1392 * the block virtual boundary.
1394 nr = ib_map_mr_sg(req->mr, req->data_sgl.sg_table.sgl, count, NULL,
1396 if (unlikely(nr < count)) {
1397 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1404 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1406 req->reg_cqe.done = nvme_rdma_memreg_done;
1407 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1408 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1409 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1410 req->reg_wr.wr.num_sge = 0;
1411 req->reg_wr.mr = req->mr;
1412 req->reg_wr.key = req->mr->rkey;
1413 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1414 IB_ACCESS_REMOTE_READ |
1415 IB_ACCESS_REMOTE_WRITE;
1417 sg->addr = cpu_to_le64(req->mr->iova);
1418 put_unaligned_le24(req->mr->length, sg->length);
1419 put_unaligned_le32(req->mr->rkey, sg->key);
1420 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1421 NVME_SGL_FMT_INVALIDATE;
1426 static void nvme_rdma_set_sig_domain(struct blk_integrity *bi,
1427 struct nvme_command *cmd, struct ib_sig_domain *domain,
1428 u16 control, u8 pi_type)
1430 domain->sig_type = IB_SIG_TYPE_T10_DIF;
1431 domain->sig.dif.bg_type = IB_T10DIF_CRC;
1432 domain->sig.dif.pi_interval = 1 << bi->interval_exp;
1433 domain->sig.dif.ref_tag = le32_to_cpu(cmd->rw.reftag);
1434 if (control & NVME_RW_PRINFO_PRCHK_REF)
1435 domain->sig.dif.ref_remap = true;
1437 domain->sig.dif.app_tag = le16_to_cpu(cmd->rw.apptag);
1438 domain->sig.dif.apptag_check_mask = le16_to_cpu(cmd->rw.appmask);
1439 domain->sig.dif.app_escape = true;
1440 if (pi_type == NVME_NS_DPS_PI_TYPE3)
1441 domain->sig.dif.ref_escape = true;
1444 static void nvme_rdma_set_sig_attrs(struct blk_integrity *bi,
1445 struct nvme_command *cmd, struct ib_sig_attrs *sig_attrs,
1448 u16 control = le16_to_cpu(cmd->rw.control);
1450 memset(sig_attrs, 0, sizeof(*sig_attrs));
1451 if (control & NVME_RW_PRINFO_PRACT) {
1452 /* for WRITE_INSERT/READ_STRIP no memory domain */
1453 sig_attrs->mem.sig_type = IB_SIG_TYPE_NONE;
1454 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control,
1456 /* Clear the PRACT bit since HCA will generate/verify the PI */
1457 control &= ~NVME_RW_PRINFO_PRACT;
1458 cmd->rw.control = cpu_to_le16(control);
1460 /* for WRITE_PASS/READ_PASS both wire/memory domains exist */
1461 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control,
1463 nvme_rdma_set_sig_domain(bi, cmd, &sig_attrs->mem, control,
1468 static void nvme_rdma_set_prot_checks(struct nvme_command *cmd, u8 *mask)
1471 if (le16_to_cpu(cmd->rw.control) & NVME_RW_PRINFO_PRCHK_REF)
1472 *mask |= IB_SIG_CHECK_REFTAG;
1473 if (le16_to_cpu(cmd->rw.control) & NVME_RW_PRINFO_PRCHK_GUARD)
1474 *mask |= IB_SIG_CHECK_GUARD;
1477 static void nvme_rdma_sig_done(struct ib_cq *cq, struct ib_wc *wc)
1479 if (unlikely(wc->status != IB_WC_SUCCESS))
1480 nvme_rdma_wr_error(cq, wc, "SIG");
1483 static int nvme_rdma_map_sg_pi(struct nvme_rdma_queue *queue,
1484 struct nvme_rdma_request *req, struct nvme_command *c,
1485 int count, int pi_count)
1487 struct nvme_rdma_sgl *sgl = &req->data_sgl;
1488 struct ib_reg_wr *wr = &req->reg_wr;
1489 struct request *rq = blk_mq_rq_from_pdu(req);
1490 struct nvme_ns *ns = rq->q->queuedata;
1491 struct bio *bio = rq->bio;
1492 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1495 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->sig_mrs);
1496 if (WARN_ON_ONCE(!req->mr))
1499 nr = ib_map_mr_sg_pi(req->mr, sgl->sg_table.sgl, count, NULL,
1500 req->metadata_sgl->sg_table.sgl, pi_count, NULL,
1505 nvme_rdma_set_sig_attrs(blk_get_integrity(bio->bi_bdev->bd_disk), c,
1506 req->mr->sig_attrs, ns->pi_type);
1507 nvme_rdma_set_prot_checks(c, &req->mr->sig_attrs->check_mask);
1509 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1511 req->reg_cqe.done = nvme_rdma_sig_done;
1512 memset(wr, 0, sizeof(*wr));
1513 wr->wr.opcode = IB_WR_REG_MR_INTEGRITY;
1514 wr->wr.wr_cqe = &req->reg_cqe;
1516 wr->wr.send_flags = 0;
1518 wr->key = req->mr->rkey;
1519 wr->access = IB_ACCESS_LOCAL_WRITE |
1520 IB_ACCESS_REMOTE_READ |
1521 IB_ACCESS_REMOTE_WRITE;
1523 sg->addr = cpu_to_le64(req->mr->iova);
1524 put_unaligned_le24(req->mr->length, sg->length);
1525 put_unaligned_le32(req->mr->rkey, sg->key);
1526 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1531 ib_mr_pool_put(queue->qp, &queue->qp->sig_mrs, req->mr);
1538 static int nvme_rdma_dma_map_req(struct ib_device *ibdev, struct request *rq,
1539 int *count, int *pi_count)
1541 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1544 req->data_sgl.sg_table.sgl = (struct scatterlist *)(req + 1);
1545 ret = sg_alloc_table_chained(&req->data_sgl.sg_table,
1546 blk_rq_nr_phys_segments(rq), req->data_sgl.sg_table.sgl,
1547 NVME_INLINE_SG_CNT);
1551 req->data_sgl.nents = blk_rq_map_sg(rq->q, rq,
1552 req->data_sgl.sg_table.sgl);
1554 *count = ib_dma_map_sg(ibdev, req->data_sgl.sg_table.sgl,
1555 req->data_sgl.nents, rq_dma_dir(rq));
1556 if (unlikely(*count <= 0)) {
1558 goto out_free_table;
1561 if (blk_integrity_rq(rq)) {
1562 req->metadata_sgl->sg_table.sgl =
1563 (struct scatterlist *)(req->metadata_sgl + 1);
1564 ret = sg_alloc_table_chained(&req->metadata_sgl->sg_table,
1565 blk_rq_count_integrity_sg(rq->q, rq->bio),
1566 req->metadata_sgl->sg_table.sgl,
1567 NVME_INLINE_METADATA_SG_CNT);
1568 if (unlikely(ret)) {
1573 req->metadata_sgl->nents = blk_rq_map_integrity_sg(rq->q,
1574 rq->bio, req->metadata_sgl->sg_table.sgl);
1575 *pi_count = ib_dma_map_sg(ibdev,
1576 req->metadata_sgl->sg_table.sgl,
1577 req->metadata_sgl->nents,
1579 if (unlikely(*pi_count <= 0)) {
1581 goto out_free_pi_table;
1588 sg_free_table_chained(&req->metadata_sgl->sg_table,
1589 NVME_INLINE_METADATA_SG_CNT);
1591 ib_dma_unmap_sg(ibdev, req->data_sgl.sg_table.sgl, req->data_sgl.nents,
1594 sg_free_table_chained(&req->data_sgl.sg_table, NVME_INLINE_SG_CNT);
1598 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
1599 struct request *rq, struct nvme_command *c)
1601 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1602 struct nvme_rdma_device *dev = queue->device;
1603 struct ib_device *ibdev = dev->dev;
1608 refcount_set(&req->ref, 2); /* send and recv completions */
1610 c->common.flags |= NVME_CMD_SGL_METABUF;
1612 if (!blk_rq_nr_phys_segments(rq))
1613 return nvme_rdma_set_sg_null(c);
1615 ret = nvme_rdma_dma_map_req(ibdev, rq, &count, &pi_count);
1619 if (req->use_sig_mr) {
1620 ret = nvme_rdma_map_sg_pi(queue, req, c, count, pi_count);
1624 if (count <= dev->num_inline_segments) {
1625 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1626 queue->ctrl->use_inline_data &&
1627 blk_rq_payload_bytes(rq) <=
1628 nvme_rdma_inline_data_size(queue)) {
1629 ret = nvme_rdma_map_sg_inline(queue, req, c, count);
1633 if (count == 1 && dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
1634 ret = nvme_rdma_map_sg_single(queue, req, c);
1639 ret = nvme_rdma_map_sg_fr(queue, req, c, count);
1642 goto out_dma_unmap_req;
1647 nvme_rdma_dma_unmap_req(ibdev, rq);
1651 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1653 struct nvme_rdma_qe *qe =
1654 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1655 struct nvme_rdma_request *req =
1656 container_of(qe, struct nvme_rdma_request, sqe);
1658 if (unlikely(wc->status != IB_WC_SUCCESS))
1659 nvme_rdma_wr_error(cq, wc, "SEND");
1661 nvme_rdma_end_request(req);
1664 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1665 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1666 struct ib_send_wr *first)
1668 struct ib_send_wr wr;
1671 sge->addr = qe->dma;
1672 sge->length = sizeof(struct nvme_command);
1673 sge->lkey = queue->device->pd->local_dma_lkey;
1676 wr.wr_cqe = &qe->cqe;
1678 wr.num_sge = num_sge;
1679 wr.opcode = IB_WR_SEND;
1680 wr.send_flags = IB_SEND_SIGNALED;
1687 ret = ib_post_send(queue->qp, first, NULL);
1688 if (unlikely(ret)) {
1689 dev_err(queue->ctrl->ctrl.device,
1690 "%s failed with error code %d\n", __func__, ret);
1695 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1696 struct nvme_rdma_qe *qe)
1698 struct ib_recv_wr wr;
1702 list.addr = qe->dma;
1703 list.length = sizeof(struct nvme_completion);
1704 list.lkey = queue->device->pd->local_dma_lkey;
1706 qe->cqe.done = nvme_rdma_recv_done;
1709 wr.wr_cqe = &qe->cqe;
1713 ret = ib_post_recv(queue->qp, &wr, NULL);
1714 if (unlikely(ret)) {
1715 dev_err(queue->ctrl->ctrl.device,
1716 "%s failed with error code %d\n", __func__, ret);
1721 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1723 u32 queue_idx = nvme_rdma_queue_idx(queue);
1726 return queue->ctrl->admin_tag_set.tags[queue_idx];
1727 return queue->ctrl->tag_set.tags[queue_idx - 1];
1730 static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc)
1732 if (unlikely(wc->status != IB_WC_SUCCESS))
1733 nvme_rdma_wr_error(cq, wc, "ASYNC");
1736 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg)
1738 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1739 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1740 struct ib_device *dev = queue->device->dev;
1741 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1742 struct nvme_command *cmd = sqe->data;
1746 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1748 memset(cmd, 0, sizeof(*cmd));
1749 cmd->common.opcode = nvme_admin_async_event;
1750 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
1751 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1752 nvme_rdma_set_sg_null(cmd);
1754 sqe->cqe.done = nvme_rdma_async_done;
1756 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1759 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL);
1763 static void nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1764 struct nvme_completion *cqe, struct ib_wc *wc)
1767 struct nvme_rdma_request *req;
1769 rq = nvme_find_rq(nvme_rdma_tagset(queue), cqe->command_id);
1771 dev_err(queue->ctrl->ctrl.device,
1772 "got bad command_id %#x on QP %#x\n",
1773 cqe->command_id, queue->qp->qp_num);
1774 nvme_rdma_error_recovery(queue->ctrl);
1777 req = blk_mq_rq_to_pdu(rq);
1779 req->status = cqe->status;
1780 req->result = cqe->result;
1782 if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
1783 if (unlikely(!req->mr ||
1784 wc->ex.invalidate_rkey != req->mr->rkey)) {
1785 dev_err(queue->ctrl->ctrl.device,
1786 "Bogus remote invalidation for rkey %#x\n",
1787 req->mr ? req->mr->rkey : 0);
1788 nvme_rdma_error_recovery(queue->ctrl);
1790 } else if (req->mr) {
1793 ret = nvme_rdma_inv_rkey(queue, req);
1794 if (unlikely(ret < 0)) {
1795 dev_err(queue->ctrl->ctrl.device,
1796 "Queueing INV WR for rkey %#x failed (%d)\n",
1797 req->mr->rkey, ret);
1798 nvme_rdma_error_recovery(queue->ctrl);
1800 /* the local invalidation completion will end the request */
1804 nvme_rdma_end_request(req);
1807 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1809 struct nvme_rdma_qe *qe =
1810 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1811 struct nvme_rdma_queue *queue = wc->qp->qp_context;
1812 struct ib_device *ibdev = queue->device->dev;
1813 struct nvme_completion *cqe = qe->data;
1814 const size_t len = sizeof(struct nvme_completion);
1816 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1817 nvme_rdma_wr_error(cq, wc, "RECV");
1821 /* sanity checking for received data length */
1822 if (unlikely(wc->byte_len < len)) {
1823 dev_err(queue->ctrl->ctrl.device,
1824 "Unexpected nvme completion length(%d)\n", wc->byte_len);
1825 nvme_rdma_error_recovery(queue->ctrl);
1829 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1831 * AEN requests are special as they don't time out and can
1832 * survive any kind of queue freeze and often don't respond to
1833 * aborts. We don't even bother to allocate a struct request
1834 * for them but rather special case them here.
1836 if (unlikely(nvme_is_aen_req(nvme_rdma_queue_idx(queue),
1838 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1841 nvme_rdma_process_nvme_rsp(queue, cqe, wc);
1842 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1844 nvme_rdma_post_recv(queue, qe);
1847 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1851 for (i = 0; i < queue->queue_size; i++) {
1852 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1860 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1861 struct rdma_cm_event *ev)
1863 struct rdma_cm_id *cm_id = queue->cm_id;
1864 int status = ev->status;
1865 const char *rej_msg;
1866 const struct nvme_rdma_cm_rej *rej_data;
1869 rej_msg = rdma_reject_msg(cm_id, status);
1870 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1872 if (rej_data && rej_data_len >= sizeof(u16)) {
1873 u16 sts = le16_to_cpu(rej_data->sts);
1875 dev_err(queue->ctrl->ctrl.device,
1876 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1877 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1879 dev_err(queue->ctrl->ctrl.device,
1880 "Connect rejected: status %d (%s).\n", status, rej_msg);
1886 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1888 struct nvme_ctrl *ctrl = &queue->ctrl->ctrl;
1891 ret = nvme_rdma_create_queue_ib(queue);
1895 if (ctrl->opts->tos >= 0)
1896 rdma_set_service_type(queue->cm_id, ctrl->opts->tos);
1897 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1899 dev_err(ctrl->device, "rdma_resolve_route failed (%d).\n",
1901 goto out_destroy_queue;
1907 nvme_rdma_destroy_queue_ib(queue);
1911 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1913 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1914 struct rdma_conn_param param = { };
1915 struct nvme_rdma_cm_req priv = { };
1918 param.qp_num = queue->qp->qp_num;
1919 param.flow_control = 1;
1921 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1922 /* maximum retry count */
1923 param.retry_count = 7;
1924 param.rnr_retry_count = 7;
1925 param.private_data = &priv;
1926 param.private_data_len = sizeof(priv);
1928 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1929 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1931 * set the admin queue depth to the minimum size
1932 * specified by the Fabrics standard.
1934 if (priv.qid == 0) {
1935 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1936 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
1939 * current interpretation of the fabrics spec
1940 * is at minimum you make hrqsize sqsize+1, or a
1941 * 1's based representation of sqsize.
1943 priv.hrqsize = cpu_to_le16(queue->queue_size);
1944 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1947 ret = rdma_connect_locked(queue->cm_id, ¶m);
1949 dev_err(ctrl->ctrl.device,
1950 "rdma_connect_locked failed (%d).\n", ret);
1957 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1958 struct rdma_cm_event *ev)
1960 struct nvme_rdma_queue *queue = cm_id->context;
1963 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1964 rdma_event_msg(ev->event), ev->event,
1967 switch (ev->event) {
1968 case RDMA_CM_EVENT_ADDR_RESOLVED:
1969 cm_error = nvme_rdma_addr_resolved(queue);
1971 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1972 cm_error = nvme_rdma_route_resolved(queue);
1974 case RDMA_CM_EVENT_ESTABLISHED:
1975 queue->cm_error = nvme_rdma_conn_established(queue);
1976 /* complete cm_done regardless of success/failure */
1977 complete(&queue->cm_done);
1979 case RDMA_CM_EVENT_REJECTED:
1980 cm_error = nvme_rdma_conn_rejected(queue, ev);
1982 case RDMA_CM_EVENT_ROUTE_ERROR:
1983 case RDMA_CM_EVENT_CONNECT_ERROR:
1984 case RDMA_CM_EVENT_UNREACHABLE:
1985 case RDMA_CM_EVENT_ADDR_ERROR:
1986 dev_dbg(queue->ctrl->ctrl.device,
1987 "CM error event %d\n", ev->event);
1988 cm_error = -ECONNRESET;
1990 case RDMA_CM_EVENT_DISCONNECTED:
1991 case RDMA_CM_EVENT_ADDR_CHANGE:
1992 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1993 dev_dbg(queue->ctrl->ctrl.device,
1994 "disconnect received - connection closed\n");
1995 nvme_rdma_error_recovery(queue->ctrl);
1997 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1998 /* device removal is handled via the ib_client API */
2001 dev_err(queue->ctrl->ctrl.device,
2002 "Unexpected RDMA CM event (%d)\n", ev->event);
2003 nvme_rdma_error_recovery(queue->ctrl);
2008 queue->cm_error = cm_error;
2009 complete(&queue->cm_done);
2015 static void nvme_rdma_complete_timed_out(struct request *rq)
2017 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
2018 struct nvme_rdma_queue *queue = req->queue;
2020 nvme_rdma_stop_queue(queue);
2021 nvmf_complete_timed_out_request(rq);
2024 static enum blk_eh_timer_return nvme_rdma_timeout(struct request *rq)
2026 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
2027 struct nvme_rdma_queue *queue = req->queue;
2028 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
2030 dev_warn(ctrl->ctrl.device, "I/O %d QID %d timeout\n",
2031 rq->tag, nvme_rdma_queue_idx(queue));
2033 if (ctrl->ctrl.state != NVME_CTRL_LIVE) {
2035 * If we are resetting, connecting or deleting we should
2036 * complete immediately because we may block controller
2037 * teardown or setup sequence
2038 * - ctrl disable/shutdown fabrics requests
2039 * - connect requests
2040 * - initialization admin requests
2041 * - I/O requests that entered after unquiescing and
2042 * the controller stopped responding
2044 * All other requests should be cancelled by the error
2045 * recovery work, so it's fine that we fail it here.
2047 nvme_rdma_complete_timed_out(rq);
2052 * LIVE state should trigger the normal error recovery which will
2053 * handle completing this request.
2055 nvme_rdma_error_recovery(ctrl);
2056 return BLK_EH_RESET_TIMER;
2059 static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
2060 const struct blk_mq_queue_data *bd)
2062 struct nvme_ns *ns = hctx->queue->queuedata;
2063 struct nvme_rdma_queue *queue = hctx->driver_data;
2064 struct request *rq = bd->rq;
2065 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
2066 struct nvme_rdma_qe *sqe = &req->sqe;
2067 struct nvme_command *c = nvme_req(rq)->cmd;
2068 struct ib_device *dev;
2069 bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags);
2073 WARN_ON_ONCE(rq->tag < 0);
2075 if (!nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2076 return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
2078 dev = queue->device->dev;
2080 req->sqe.dma = ib_dma_map_single(dev, req->sqe.data,
2081 sizeof(struct nvme_command),
2083 err = ib_dma_mapping_error(dev, req->sqe.dma);
2085 return BLK_STS_RESOURCE;
2087 ib_dma_sync_single_for_cpu(dev, sqe->dma,
2088 sizeof(struct nvme_command), DMA_TO_DEVICE);
2090 ret = nvme_setup_cmd(ns, rq);
2094 blk_mq_start_request(rq);
2096 if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) &&
2097 queue->pi_support &&
2098 (c->common.opcode == nvme_cmd_write ||
2099 c->common.opcode == nvme_cmd_read) &&
2101 req->use_sig_mr = true;
2103 req->use_sig_mr = false;
2105 err = nvme_rdma_map_data(queue, rq, c);
2106 if (unlikely(err < 0)) {
2107 dev_err(queue->ctrl->ctrl.device,
2108 "Failed to map data (%d)\n", err);
2112 sqe->cqe.done = nvme_rdma_send_done;
2114 ib_dma_sync_single_for_device(dev, sqe->dma,
2115 sizeof(struct nvme_command), DMA_TO_DEVICE);
2117 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
2118 req->mr ? &req->reg_wr.wr : NULL);
2125 nvme_rdma_unmap_data(queue, rq);
2128 ret = nvme_host_path_error(rq);
2129 else if (err == -ENOMEM || err == -EAGAIN)
2130 ret = BLK_STS_RESOURCE;
2132 ret = BLK_STS_IOERR;
2133 nvme_cleanup_cmd(rq);
2135 ib_dma_unmap_single(dev, req->sqe.dma, sizeof(struct nvme_command),
2140 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
2142 struct nvme_rdma_queue *queue = hctx->driver_data;
2144 return ib_process_cq_direct(queue->ib_cq, -1);
2147 static void nvme_rdma_check_pi_status(struct nvme_rdma_request *req)
2149 struct request *rq = blk_mq_rq_from_pdu(req);
2150 struct ib_mr_status mr_status;
2153 ret = ib_check_mr_status(req->mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
2155 pr_err("ib_check_mr_status failed, ret %d\n", ret);
2156 nvme_req(rq)->status = NVME_SC_INVALID_PI;
2160 if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
2161 switch (mr_status.sig_err.err_type) {
2162 case IB_SIG_BAD_GUARD:
2163 nvme_req(rq)->status = NVME_SC_GUARD_CHECK;
2165 case IB_SIG_BAD_REFTAG:
2166 nvme_req(rq)->status = NVME_SC_REFTAG_CHECK;
2168 case IB_SIG_BAD_APPTAG:
2169 nvme_req(rq)->status = NVME_SC_APPTAG_CHECK;
2172 pr_err("PI error found type %d expected 0x%x vs actual 0x%x\n",
2173 mr_status.sig_err.err_type, mr_status.sig_err.expected,
2174 mr_status.sig_err.actual);
2178 static void nvme_rdma_complete_rq(struct request *rq)
2180 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
2181 struct nvme_rdma_queue *queue = req->queue;
2182 struct ib_device *ibdev = queue->device->dev;
2184 if (req->use_sig_mr)
2185 nvme_rdma_check_pi_status(req);
2187 nvme_rdma_unmap_data(queue, rq);
2188 ib_dma_unmap_single(ibdev, req->sqe.dma, sizeof(struct nvme_command),
2190 nvme_complete_rq(rq);
2193 static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
2195 struct nvme_rdma_ctrl *ctrl = set->driver_data;
2196 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2198 if (opts->nr_write_queues && ctrl->io_queues[HCTX_TYPE_READ]) {
2199 /* separate read/write queues */
2200 set->map[HCTX_TYPE_DEFAULT].nr_queues =
2201 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2202 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2203 set->map[HCTX_TYPE_READ].nr_queues =
2204 ctrl->io_queues[HCTX_TYPE_READ];
2205 set->map[HCTX_TYPE_READ].queue_offset =
2206 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2208 /* shared read/write queues */
2209 set->map[HCTX_TYPE_DEFAULT].nr_queues =
2210 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2211 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2212 set->map[HCTX_TYPE_READ].nr_queues =
2213 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2214 set->map[HCTX_TYPE_READ].queue_offset = 0;
2216 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_DEFAULT],
2217 ctrl->device->dev, 0);
2218 blk_mq_rdma_map_queues(&set->map[HCTX_TYPE_READ],
2219 ctrl->device->dev, 0);
2221 if (opts->nr_poll_queues && ctrl->io_queues[HCTX_TYPE_POLL]) {
2222 /* map dedicated poll queues only if we have queues left */
2223 set->map[HCTX_TYPE_POLL].nr_queues =
2224 ctrl->io_queues[HCTX_TYPE_POLL];
2225 set->map[HCTX_TYPE_POLL].queue_offset =
2226 ctrl->io_queues[HCTX_TYPE_DEFAULT] +
2227 ctrl->io_queues[HCTX_TYPE_READ];
2228 blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]);
2231 dev_info(ctrl->ctrl.device,
2232 "mapped %d/%d/%d default/read/poll queues.\n",
2233 ctrl->io_queues[HCTX_TYPE_DEFAULT],
2234 ctrl->io_queues[HCTX_TYPE_READ],
2235 ctrl->io_queues[HCTX_TYPE_POLL]);
2240 static const struct blk_mq_ops nvme_rdma_mq_ops = {
2241 .queue_rq = nvme_rdma_queue_rq,
2242 .complete = nvme_rdma_complete_rq,
2243 .init_request = nvme_rdma_init_request,
2244 .exit_request = nvme_rdma_exit_request,
2245 .init_hctx = nvme_rdma_init_hctx,
2246 .timeout = nvme_rdma_timeout,
2247 .map_queues = nvme_rdma_map_queues,
2248 .poll = nvme_rdma_poll,
2251 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
2252 .queue_rq = nvme_rdma_queue_rq,
2253 .complete = nvme_rdma_complete_rq,
2254 .init_request = nvme_rdma_init_request,
2255 .exit_request = nvme_rdma_exit_request,
2256 .init_hctx = nvme_rdma_init_admin_hctx,
2257 .timeout = nvme_rdma_timeout,
2260 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
2262 nvme_rdma_teardown_io_queues(ctrl, shutdown);
2263 nvme_stop_admin_queue(&ctrl->ctrl);
2265 nvme_shutdown_ctrl(&ctrl->ctrl);
2267 nvme_disable_ctrl(&ctrl->ctrl);
2268 nvme_rdma_teardown_admin_queue(ctrl, shutdown);
2271 static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
2273 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true);
2276 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
2278 struct nvme_rdma_ctrl *ctrl =
2279 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
2281 nvme_stop_ctrl(&ctrl->ctrl);
2282 nvme_rdma_shutdown_ctrl(ctrl, false);
2284 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
2285 /* state change failure should never happen */
2290 if (nvme_rdma_setup_ctrl(ctrl, false))
2296 ++ctrl->ctrl.nr_reconnects;
2297 nvme_rdma_reconnect_or_remove(ctrl);
2300 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
2302 .module = THIS_MODULE,
2303 .flags = NVME_F_FABRICS | NVME_F_METADATA_SUPPORTED,
2304 .reg_read32 = nvmf_reg_read32,
2305 .reg_read64 = nvmf_reg_read64,
2306 .reg_write32 = nvmf_reg_write32,
2307 .free_ctrl = nvme_rdma_free_ctrl,
2308 .submit_async_event = nvme_rdma_submit_async_event,
2309 .delete_ctrl = nvme_rdma_delete_ctrl,
2310 .get_address = nvmf_get_address,
2311 .stop_ctrl = nvme_rdma_stop_ctrl,
2315 * Fails a connection request if it matches an existing controller
2316 * (association) with the same tuple:
2317 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
2319 * if local address is not specified in the request, it will match an
2320 * existing controller with all the other parameters the same and no
2321 * local port address specified as well.
2323 * The ports don't need to be compared as they are intrinsically
2324 * already matched by the port pointers supplied.
2327 nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts)
2329 struct nvme_rdma_ctrl *ctrl;
2332 mutex_lock(&nvme_rdma_ctrl_mutex);
2333 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2334 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
2338 mutex_unlock(&nvme_rdma_ctrl_mutex);
2343 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
2344 struct nvmf_ctrl_options *opts)
2346 struct nvme_rdma_ctrl *ctrl;
2350 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2352 return ERR_PTR(-ENOMEM);
2353 ctrl->ctrl.opts = opts;
2354 INIT_LIST_HEAD(&ctrl->list);
2356 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
2358 kstrdup(__stringify(NVME_RDMA_IP_PORT), GFP_KERNEL);
2359 if (!opts->trsvcid) {
2363 opts->mask |= NVMF_OPT_TRSVCID;
2366 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2367 opts->traddr, opts->trsvcid, &ctrl->addr);
2369 pr_err("malformed address passed: %s:%s\n",
2370 opts->traddr, opts->trsvcid);
2374 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
2375 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2376 opts->host_traddr, NULL, &ctrl->src_addr);
2378 pr_err("malformed src address passed: %s\n",
2384 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) {
2389 INIT_DELAYED_WORK(&ctrl->reconnect_work,
2390 nvme_rdma_reconnect_ctrl_work);
2391 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
2392 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
2394 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
2395 opts->nr_poll_queues + 1;
2396 ctrl->ctrl.sqsize = opts->queue_size - 1;
2397 ctrl->ctrl.kato = opts->kato;
2400 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
2405 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
2406 0 /* no quirks, we're perfect! */);
2408 goto out_kfree_queues;
2410 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING);
2411 WARN_ON_ONCE(!changed);
2413 ret = nvme_rdma_setup_ctrl(ctrl, true);
2415 goto out_uninit_ctrl;
2417 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
2418 nvmf_ctrl_subsysnqn(&ctrl->ctrl), &ctrl->addr);
2420 mutex_lock(&nvme_rdma_ctrl_mutex);
2421 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
2422 mutex_unlock(&nvme_rdma_ctrl_mutex);
2427 nvme_uninit_ctrl(&ctrl->ctrl);
2428 nvme_put_ctrl(&ctrl->ctrl);
2431 return ERR_PTR(ret);
2433 kfree(ctrl->queues);
2436 return ERR_PTR(ret);
2439 static struct nvmf_transport_ops nvme_rdma_transport = {
2441 .module = THIS_MODULE,
2442 .required_opts = NVMF_OPT_TRADDR,
2443 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2444 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
2445 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
2447 .create_ctrl = nvme_rdma_create_ctrl,
2450 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2452 struct nvme_rdma_ctrl *ctrl;
2453 struct nvme_rdma_device *ndev;
2456 mutex_lock(&device_list_mutex);
2457 list_for_each_entry(ndev, &device_list, entry) {
2458 if (ndev->dev == ib_device) {
2463 mutex_unlock(&device_list_mutex);
2468 /* Delete all controllers using this device */
2469 mutex_lock(&nvme_rdma_ctrl_mutex);
2470 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2471 if (ctrl->device->dev != ib_device)
2473 nvme_delete_ctrl(&ctrl->ctrl);
2475 mutex_unlock(&nvme_rdma_ctrl_mutex);
2477 flush_workqueue(nvme_delete_wq);
2480 static struct ib_client nvme_rdma_ib_client = {
2481 .name = "nvme_rdma",
2482 .remove = nvme_rdma_remove_one
2485 static int __init nvme_rdma_init_module(void)
2489 ret = ib_register_client(&nvme_rdma_ib_client);
2493 ret = nvmf_register_transport(&nvme_rdma_transport);
2495 goto err_unreg_client;
2500 ib_unregister_client(&nvme_rdma_ib_client);
2504 static void __exit nvme_rdma_cleanup_module(void)
2506 struct nvme_rdma_ctrl *ctrl;
2508 nvmf_unregister_transport(&nvme_rdma_transport);
2509 ib_unregister_client(&nvme_rdma_ib_client);
2511 mutex_lock(&nvme_rdma_ctrl_mutex);
2512 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list)
2513 nvme_delete_ctrl(&ctrl->ctrl);
2514 mutex_unlock(&nvme_rdma_ctrl_mutex);
2515 flush_workqueue(nvme_delete_wq);
2518 module_init(nvme_rdma_init_module);
2519 module_exit(nvme_rdma_cleanup_module);
2521 MODULE_LICENSE("GPL v2");