2 * NVMe over Fabrics RDMA host code.
3 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/err.h>
19 #include <linux/string.h>
20 #include <linux/atomic.h>
21 #include <linux/blk-mq.h>
22 #include <linux/blk-mq-rdma.h>
23 #include <linux/types.h>
24 #include <linux/list.h>
25 #include <linux/mutex.h>
26 #include <linux/scatterlist.h>
27 #include <linux/nvme.h>
28 #include <asm/unaligned.h>
30 #include <rdma/ib_verbs.h>
31 #include <rdma/rdma_cm.h>
32 #include <linux/nvme-rdma.h>
38 #define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
40 #define NVME_RDMA_MAX_SEGMENTS 256
42 #define NVME_RDMA_MAX_INLINE_SEGMENTS 1
45 * We handle AEN commands ourselves and don't even let the
46 * block layer know about them.
48 #define NVME_RDMA_NR_AEN_COMMANDS 1
49 #define NVME_RDMA_AQ_BLKMQ_DEPTH \
50 (NVME_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
52 struct nvme_rdma_device {
53 struct ib_device *dev;
56 struct list_head entry;
65 struct nvme_rdma_queue;
66 struct nvme_rdma_request {
67 struct nvme_request req;
69 struct nvme_rdma_qe sqe;
70 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
74 struct ib_reg_wr reg_wr;
75 struct ib_cqe reg_cqe;
76 struct nvme_rdma_queue *queue;
77 struct sg_table sg_table;
78 struct scatterlist first_sgl[];
81 enum nvme_rdma_queue_flags {
83 NVME_RDMA_Q_DELETING = 1,
86 struct nvme_rdma_queue {
87 struct nvme_rdma_qe *rsp_ring;
90 size_t cmnd_capsule_len;
91 struct nvme_rdma_ctrl *ctrl;
92 struct nvme_rdma_device *device;
97 struct rdma_cm_id *cm_id;
99 struct completion cm_done;
102 struct nvme_rdma_ctrl {
103 /* read only in the hot path */
104 struct nvme_rdma_queue *queues;
106 /* other member variables */
107 struct blk_mq_tag_set tag_set;
108 struct work_struct delete_work;
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;
128 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
130 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
133 static LIST_HEAD(device_list);
134 static DEFINE_MUTEX(device_list_mutex);
136 static LIST_HEAD(nvme_rdma_ctrl_list);
137 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
140 * Disabling this option makes small I/O goes faster, but is fundamentally
141 * unsafe. With it turned off we will have to register a global rkey that
142 * allows read and write access to all physical memory.
144 static bool register_always = true;
145 module_param(register_always, bool, 0444);
146 MODULE_PARM_DESC(register_always,
147 "Use memory registration even for contiguous memory regions");
149 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
150 struct rdma_cm_event *event);
151 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
153 static const struct blk_mq_ops nvme_rdma_mq_ops;
154 static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
156 /* XXX: really should move to a generic header sooner or later.. */
157 static inline void put_unaligned_le24(u32 val, u8 *p)
164 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
166 return queue - queue->ctrl->queues;
169 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
171 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
174 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
175 size_t capsule_size, enum dma_data_direction dir)
177 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
181 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
182 size_t capsule_size, enum dma_data_direction dir)
184 qe->data = kzalloc(capsule_size, GFP_KERNEL);
188 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
189 if (ib_dma_mapping_error(ibdev, qe->dma)) {
197 static void nvme_rdma_free_ring(struct ib_device *ibdev,
198 struct nvme_rdma_qe *ring, size_t ib_queue_size,
199 size_t capsule_size, enum dma_data_direction dir)
203 for (i = 0; i < ib_queue_size; i++)
204 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
208 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
209 size_t ib_queue_size, size_t capsule_size,
210 enum dma_data_direction dir)
212 struct nvme_rdma_qe *ring;
215 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
219 for (i = 0; i < ib_queue_size; i++) {
220 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
227 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
231 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
233 pr_debug("QP event %s (%d)\n",
234 ib_event_msg(event->event), event->event);
238 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
240 wait_for_completion_interruptible_timeout(&queue->cm_done,
241 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
242 return queue->cm_error;
245 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
247 struct nvme_rdma_device *dev = queue->device;
248 struct ib_qp_init_attr init_attr;
251 memset(&init_attr, 0, sizeof(init_attr));
252 init_attr.event_handler = nvme_rdma_qp_event;
254 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
256 init_attr.cap.max_recv_wr = queue->queue_size + 1;
257 init_attr.cap.max_recv_sge = 1;
258 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
259 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
260 init_attr.qp_type = IB_QPT_RC;
261 init_attr.send_cq = queue->ib_cq;
262 init_attr.recv_cq = queue->ib_cq;
264 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
266 queue->qp = queue->cm_id->qp;
270 static int nvme_rdma_reinit_request(void *data, struct request *rq)
272 struct nvme_rdma_ctrl *ctrl = data;
273 struct nvme_rdma_device *dev = ctrl->device;
274 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
277 ib_dereg_mr(req->mr);
279 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
281 if (IS_ERR(req->mr)) {
282 ret = PTR_ERR(req->mr);
287 req->mr->need_inval = false;
293 static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
294 struct request *rq, unsigned int hctx_idx)
296 struct nvme_rdma_ctrl *ctrl = set->driver_data;
297 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
298 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
299 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
300 struct nvme_rdma_device *dev = queue->device;
303 ib_dereg_mr(req->mr);
305 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
309 static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
310 struct request *rq, unsigned int hctx_idx,
311 unsigned int numa_node)
313 struct nvme_rdma_ctrl *ctrl = set->driver_data;
314 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
315 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
316 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
317 struct nvme_rdma_device *dev = queue->device;
318 struct ib_device *ibdev = dev->dev;
321 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
326 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
328 if (IS_ERR(req->mr)) {
329 ret = PTR_ERR(req->mr);
338 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
343 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
344 unsigned int hctx_idx)
346 struct nvme_rdma_ctrl *ctrl = data;
347 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
349 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
351 hctx->driver_data = queue;
355 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
356 unsigned int hctx_idx)
358 struct nvme_rdma_ctrl *ctrl = data;
359 struct nvme_rdma_queue *queue = &ctrl->queues[0];
361 BUG_ON(hctx_idx != 0);
363 hctx->driver_data = queue;
367 static void nvme_rdma_free_dev(struct kref *ref)
369 struct nvme_rdma_device *ndev =
370 container_of(ref, struct nvme_rdma_device, ref);
372 mutex_lock(&device_list_mutex);
373 list_del(&ndev->entry);
374 mutex_unlock(&device_list_mutex);
376 ib_dealloc_pd(ndev->pd);
380 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
382 kref_put(&dev->ref, nvme_rdma_free_dev);
385 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
387 return kref_get_unless_zero(&dev->ref);
390 static struct nvme_rdma_device *
391 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
393 struct nvme_rdma_device *ndev;
395 mutex_lock(&device_list_mutex);
396 list_for_each_entry(ndev, &device_list, entry) {
397 if (ndev->dev->node_guid == cm_id->device->node_guid &&
398 nvme_rdma_dev_get(ndev))
402 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
406 ndev->dev = cm_id->device;
407 kref_init(&ndev->ref);
409 ndev->pd = ib_alloc_pd(ndev->dev,
410 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
411 if (IS_ERR(ndev->pd))
414 if (!(ndev->dev->attrs.device_cap_flags &
415 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
416 dev_err(&ndev->dev->dev,
417 "Memory registrations not supported.\n");
421 list_add(&ndev->entry, &device_list);
423 mutex_unlock(&device_list_mutex);
427 ib_dealloc_pd(ndev->pd);
431 mutex_unlock(&device_list_mutex);
435 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
437 struct nvme_rdma_device *dev;
438 struct ib_device *ibdev;
442 rdma_destroy_qp(queue->cm_id);
443 ib_free_cq(queue->ib_cq);
445 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
446 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
448 nvme_rdma_dev_put(dev);
451 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
453 struct ib_device *ibdev;
454 const int send_wr_factor = 3; /* MR, SEND, INV */
455 const int cq_factor = send_wr_factor + 1; /* + RECV */
456 int comp_vector, idx = nvme_rdma_queue_idx(queue);
459 queue->device = nvme_rdma_find_get_device(queue->cm_id);
460 if (!queue->device) {
461 dev_err(queue->cm_id->device->dev.parent,
462 "no client data found!\n");
463 return -ECONNREFUSED;
465 ibdev = queue->device->dev;
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;
473 /* +1 for ib_stop_cq */
474 queue->ib_cq = ib_alloc_cq(ibdev, queue,
475 cq_factor * queue->queue_size + 1,
476 comp_vector, IB_POLL_SOFTIRQ);
477 if (IS_ERR(queue->ib_cq)) {
478 ret = PTR_ERR(queue->ib_cq);
482 ret = nvme_rdma_create_qp(queue, send_wr_factor);
484 goto out_destroy_ib_cq;
486 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
487 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
488 if (!queue->rsp_ring) {
496 ib_destroy_qp(queue->qp);
498 ib_free_cq(queue->ib_cq);
500 nvme_rdma_dev_put(queue->device);
504 static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
505 int idx, size_t queue_size)
507 struct nvme_rdma_queue *queue;
508 struct sockaddr *src_addr = NULL;
511 queue = &ctrl->queues[idx];
513 init_completion(&queue->cm_done);
516 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
518 queue->cmnd_capsule_len = sizeof(struct nvme_command);
520 queue->queue_size = queue_size;
521 atomic_set(&queue->sig_count, 0);
523 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
524 RDMA_PS_TCP, IB_QPT_RC);
525 if (IS_ERR(queue->cm_id)) {
526 dev_info(ctrl->ctrl.device,
527 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
528 return PTR_ERR(queue->cm_id);
531 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
532 src_addr = (struct sockaddr *)&ctrl->src_addr;
534 queue->cm_error = -ETIMEDOUT;
535 ret = rdma_resolve_addr(queue->cm_id, src_addr,
536 (struct sockaddr *)&ctrl->addr,
537 NVME_RDMA_CONNECT_TIMEOUT_MS);
539 dev_info(ctrl->ctrl.device,
540 "rdma_resolve_addr failed (%d).\n", ret);
541 goto out_destroy_cm_id;
544 ret = nvme_rdma_wait_for_cm(queue);
546 dev_info(ctrl->ctrl.device,
547 "rdma_resolve_addr wait failed (%d).\n", ret);
548 goto out_destroy_cm_id;
551 clear_bit(NVME_RDMA_Q_DELETING, &queue->flags);
556 rdma_destroy_id(queue->cm_id);
560 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
562 if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
565 rdma_disconnect(queue->cm_id);
566 ib_drain_qp(queue->qp);
569 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
571 if (test_and_set_bit(NVME_RDMA_Q_DELETING, &queue->flags))
574 if (nvme_rdma_queue_idx(queue) == 0) {
575 nvme_rdma_free_qe(queue->device->dev,
576 &queue->ctrl->async_event_sqe,
577 sizeof(struct nvme_command), DMA_TO_DEVICE);
580 nvme_rdma_destroy_queue_ib(queue);
581 rdma_destroy_id(queue->cm_id);
584 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
588 for (i = 1; i < ctrl->ctrl.queue_count; i++)
589 nvme_rdma_free_queue(&ctrl->queues[i]);
592 static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
596 for (i = 1; i < ctrl->ctrl.queue_count; i++)
597 nvme_rdma_stop_queue(&ctrl->queues[i]);
600 static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
605 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx);
607 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
610 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[idx].flags);
612 dev_info(ctrl->ctrl.device,
613 "failed to connect queue: %d ret=%d\n", idx, ret);
617 static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
621 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
622 ret = nvme_rdma_start_queue(ctrl, i);
624 goto out_stop_queues;
630 for (i--; i >= 1; i--)
631 nvme_rdma_stop_queue(&ctrl->queues[i]);
635 static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
637 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
638 struct ib_device *ibdev = ctrl->device->dev;
639 unsigned int nr_io_queues;
642 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
645 * we map queues according to the device irq vectors for
646 * optimal locality so we don't need more queues than
647 * completion vectors.
649 nr_io_queues = min_t(unsigned int, nr_io_queues,
650 ibdev->num_comp_vectors);
652 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
656 ctrl->ctrl.queue_count = nr_io_queues + 1;
657 if (ctrl->ctrl.queue_count < 2)
660 dev_info(ctrl->ctrl.device,
661 "creating %d I/O queues.\n", nr_io_queues);
663 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
664 ret = nvme_rdma_alloc_queue(ctrl, i,
665 ctrl->ctrl.sqsize + 1);
667 goto out_free_queues;
673 for (i--; i >= 1; i--)
674 nvme_rdma_free_queue(&ctrl->queues[i]);
679 static void nvme_rdma_free_tagset(struct nvme_ctrl *nctrl, bool admin)
681 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
682 struct blk_mq_tag_set *set = admin ?
683 &ctrl->admin_tag_set : &ctrl->tag_set;
685 blk_mq_free_tag_set(set);
686 nvme_rdma_dev_put(ctrl->device);
689 static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
692 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
693 struct blk_mq_tag_set *set;
697 set = &ctrl->admin_tag_set;
698 memset(set, 0, sizeof(*set));
699 set->ops = &nvme_rdma_admin_mq_ops;
700 set->queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
701 set->reserved_tags = 2; /* connect + keep-alive */
702 set->numa_node = NUMA_NO_NODE;
703 set->cmd_size = sizeof(struct nvme_rdma_request) +
704 SG_CHUNK_SIZE * sizeof(struct scatterlist);
705 set->driver_data = ctrl;
706 set->nr_hw_queues = 1;
707 set->timeout = ADMIN_TIMEOUT;
709 set = &ctrl->tag_set;
710 memset(set, 0, sizeof(*set));
711 set->ops = &nvme_rdma_mq_ops;
712 set->queue_depth = nctrl->opts->queue_size;
713 set->reserved_tags = 1; /* fabric connect */
714 set->numa_node = NUMA_NO_NODE;
715 set->flags = BLK_MQ_F_SHOULD_MERGE;
716 set->cmd_size = sizeof(struct nvme_rdma_request) +
717 SG_CHUNK_SIZE * sizeof(struct scatterlist);
718 set->driver_data = ctrl;
719 set->nr_hw_queues = nctrl->queue_count - 1;
720 set->timeout = NVME_IO_TIMEOUT;
723 ret = blk_mq_alloc_tag_set(set);
728 * We need a reference on the device as long as the tag_set is alive,
729 * as the MRs in the request structures need a valid ib_device.
731 ret = nvme_rdma_dev_get(ctrl->device);
734 goto out_free_tagset;
740 blk_mq_free_tag_set(set);
745 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
748 nvme_rdma_stop_queue(&ctrl->queues[0]);
750 blk_cleanup_queue(ctrl->ctrl.admin_q);
751 nvme_rdma_free_tagset(&ctrl->ctrl, true);
753 nvme_rdma_free_queue(&ctrl->queues[0]);
756 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
761 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
765 ctrl->device = ctrl->queues[0].device;
767 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
768 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
771 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
772 if (IS_ERR(ctrl->ctrl.admin_tagset)) {
773 error = PTR_ERR(ctrl->ctrl.admin_tagset);
777 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
778 if (IS_ERR(ctrl->ctrl.admin_q)) {
779 error = PTR_ERR(ctrl->ctrl.admin_q);
780 goto out_free_tagset;
783 error = blk_mq_reinit_tagset(&ctrl->admin_tag_set,
784 nvme_rdma_reinit_request);
789 error = nvme_rdma_start_queue(ctrl, 0);
791 goto out_cleanup_queue;
793 error = ctrl->ctrl.ops->reg_read64(&ctrl->ctrl, NVME_REG_CAP,
796 dev_err(ctrl->ctrl.device,
797 "prop_get NVME_REG_CAP failed\n");
798 goto out_cleanup_queue;
802 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
804 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
806 goto out_cleanup_queue;
808 ctrl->ctrl.max_hw_sectors =
809 (ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9);
811 error = nvme_init_identify(&ctrl->ctrl);
813 goto out_cleanup_queue;
815 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
816 &ctrl->async_event_sqe, sizeof(struct nvme_command),
819 goto out_cleanup_queue;
825 blk_cleanup_queue(ctrl->ctrl.admin_q);
828 nvme_rdma_free_tagset(&ctrl->ctrl, true);
830 nvme_rdma_free_queue(&ctrl->queues[0]);
834 static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
837 nvme_rdma_stop_io_queues(ctrl);
839 blk_cleanup_queue(ctrl->ctrl.connect_q);
840 nvme_rdma_free_tagset(&ctrl->ctrl, false);
842 nvme_rdma_free_io_queues(ctrl);
845 static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
849 ret = nvme_rdma_alloc_io_queues(ctrl);
854 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
855 if (IS_ERR(ctrl->ctrl.tagset)) {
856 ret = PTR_ERR(ctrl->ctrl.tagset);
857 goto out_free_io_queues;
860 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
861 if (IS_ERR(ctrl->ctrl.connect_q)) {
862 ret = PTR_ERR(ctrl->ctrl.connect_q);
863 goto out_free_tag_set;
866 ret = blk_mq_reinit_tagset(&ctrl->tag_set,
867 nvme_rdma_reinit_request);
869 goto out_free_io_queues;
871 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
872 ctrl->ctrl.queue_count - 1);
875 ret = nvme_rdma_start_io_queues(ctrl);
877 goto out_cleanup_connect_q;
881 out_cleanup_connect_q:
883 blk_cleanup_queue(ctrl->ctrl.connect_q);
886 nvme_rdma_free_tagset(&ctrl->ctrl, false);
888 nvme_rdma_free_io_queues(ctrl);
892 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
894 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
896 if (list_empty(&ctrl->list))
899 mutex_lock(&nvme_rdma_ctrl_mutex);
900 list_del(&ctrl->list);
901 mutex_unlock(&nvme_rdma_ctrl_mutex);
904 nvmf_free_options(nctrl->opts);
909 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
911 /* If we are resetting/deleting then do nothing */
912 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
913 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
914 ctrl->ctrl.state == NVME_CTRL_LIVE);
918 if (nvmf_should_reconnect(&ctrl->ctrl)) {
919 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
920 ctrl->ctrl.opts->reconnect_delay);
921 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
922 ctrl->ctrl.opts->reconnect_delay * HZ);
924 dev_info(ctrl->ctrl.device, "Removing controller...\n");
925 queue_work(nvme_wq, &ctrl->delete_work);
929 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
931 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
932 struct nvme_rdma_ctrl, reconnect_work);
936 ++ctrl->ctrl.nr_reconnects;
938 if (ctrl->ctrl.queue_count > 1)
939 nvme_rdma_destroy_io_queues(ctrl, false);
941 nvme_rdma_destroy_admin_queue(ctrl, false);
942 ret = nvme_rdma_configure_admin_queue(ctrl, false);
946 if (ctrl->ctrl.queue_count > 1) {
947 ret = nvme_rdma_configure_io_queues(ctrl, false);
952 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
954 /* state change failure is ok if we're in DELETING state */
955 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
959 ctrl->ctrl.nr_reconnects = 0;
961 nvme_start_ctrl(&ctrl->ctrl);
963 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
968 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
969 ctrl->ctrl.nr_reconnects);
970 nvme_rdma_reconnect_or_remove(ctrl);
973 static void nvme_rdma_error_recovery_work(struct work_struct *work)
975 struct nvme_rdma_ctrl *ctrl = container_of(work,
976 struct nvme_rdma_ctrl, err_work);
978 nvme_stop_keep_alive(&ctrl->ctrl);
980 if (ctrl->ctrl.queue_count > 1) {
981 nvme_stop_queues(&ctrl->ctrl);
982 nvme_rdma_stop_io_queues(ctrl);
984 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
985 nvme_rdma_stop_queue(&ctrl->queues[0]);
987 /* We must take care of fastfail/requeue all our inflight requests */
988 if (ctrl->ctrl.queue_count > 1)
989 blk_mq_tagset_busy_iter(&ctrl->tag_set,
990 nvme_cancel_request, &ctrl->ctrl);
991 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
992 nvme_cancel_request, &ctrl->ctrl);
995 * queues are not a live anymore, so restart the queues to fail fast
998 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
999 nvme_start_queues(&ctrl->ctrl);
1001 nvme_rdma_reconnect_or_remove(ctrl);
1004 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
1006 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
1009 queue_work(nvme_wq, &ctrl->err_work);
1012 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1015 struct nvme_rdma_queue *queue = cq->cq_context;
1016 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1018 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1019 dev_info(ctrl->ctrl.device,
1020 "%s for CQE 0x%p failed with status %s (%d)\n",
1022 ib_wc_status_msg(wc->status), wc->status);
1023 nvme_rdma_error_recovery(ctrl);
1026 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1028 if (unlikely(wc->status != IB_WC_SUCCESS))
1029 nvme_rdma_wr_error(cq, wc, "MEMREG");
1032 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1034 if (unlikely(wc->status != IB_WC_SUCCESS))
1035 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
1038 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1039 struct nvme_rdma_request *req)
1041 struct ib_send_wr *bad_wr;
1042 struct ib_send_wr wr = {
1043 .opcode = IB_WR_LOCAL_INV,
1047 .ex.invalidate_rkey = req->mr->rkey,
1050 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1051 wr.wr_cqe = &req->reg_cqe;
1053 return ib_post_send(queue->qp, &wr, &bad_wr);
1056 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1059 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1060 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1061 struct nvme_rdma_device *dev = queue->device;
1062 struct ib_device *ibdev = dev->dev;
1065 if (!blk_rq_bytes(rq))
1068 if (req->mr->need_inval) {
1069 res = nvme_rdma_inv_rkey(queue, req);
1070 if (unlikely(res < 0)) {
1071 dev_err(ctrl->ctrl.device,
1072 "Queueing INV WR for rkey %#x failed (%d)\n",
1073 req->mr->rkey, res);
1074 nvme_rdma_error_recovery(queue->ctrl);
1078 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1079 req->nents, rq_data_dir(rq) ==
1080 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1082 nvme_cleanup_cmd(rq);
1083 sg_free_table_chained(&req->sg_table, true);
1086 static int nvme_rdma_set_sg_null(struct nvme_command *c)
1088 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1091 put_unaligned_le24(0, sg->length);
1092 put_unaligned_le32(0, sg->key);
1093 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1097 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
1098 struct nvme_rdma_request *req, struct nvme_command *c)
1100 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1102 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
1103 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
1104 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
1106 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1107 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
1108 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1110 req->inline_data = true;
1115 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1116 struct nvme_rdma_request *req, struct nvme_command *c)
1118 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1120 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
1121 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
1122 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
1123 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1127 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1128 struct nvme_rdma_request *req, struct nvme_command *c,
1131 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1135 * Align the MR to a 4K page size to match the ctrl page size and
1136 * the block virtual boundary.
1138 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K);
1139 if (unlikely(nr < count)) {
1145 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1147 req->reg_cqe.done = nvme_rdma_memreg_done;
1148 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1149 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1150 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1151 req->reg_wr.wr.num_sge = 0;
1152 req->reg_wr.mr = req->mr;
1153 req->reg_wr.key = req->mr->rkey;
1154 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1155 IB_ACCESS_REMOTE_READ |
1156 IB_ACCESS_REMOTE_WRITE;
1158 req->mr->need_inval = true;
1160 sg->addr = cpu_to_le64(req->mr->iova);
1161 put_unaligned_le24(req->mr->length, sg->length);
1162 put_unaligned_le32(req->mr->rkey, sg->key);
1163 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1164 NVME_SGL_FMT_INVALIDATE;
1169 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
1170 struct request *rq, struct nvme_command *c)
1172 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1173 struct nvme_rdma_device *dev = queue->device;
1174 struct ib_device *ibdev = dev->dev;
1178 req->inline_data = false;
1179 req->mr->need_inval = false;
1181 c->common.flags |= NVME_CMD_SGL_METABUF;
1183 if (!blk_rq_bytes(rq))
1184 return nvme_rdma_set_sg_null(c);
1186 req->sg_table.sgl = req->first_sgl;
1187 ret = sg_alloc_table_chained(&req->sg_table,
1188 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
1192 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
1194 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
1195 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1196 if (unlikely(count <= 0)) {
1197 sg_free_table_chained(&req->sg_table, true);
1202 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1203 blk_rq_payload_bytes(rq) <=
1204 nvme_rdma_inline_data_size(queue))
1205 return nvme_rdma_map_sg_inline(queue, req, c);
1207 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
1208 return nvme_rdma_map_sg_single(queue, req, c);
1211 return nvme_rdma_map_sg_fr(queue, req, c, count);
1214 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1216 if (unlikely(wc->status != IB_WC_SUCCESS))
1217 nvme_rdma_wr_error(cq, wc, "SEND");
1221 * We want to signal completion at least every queue depth/2. This returns the
1222 * largest power of two that is not above half of (queue size + 1) to optimize
1223 * (avoid divisions).
1225 static inline bool nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue)
1227 int limit = 1 << ilog2((queue->queue_size + 1) / 2);
1229 return (atomic_inc_return(&queue->sig_count) & (limit - 1)) == 0;
1232 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1233 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1234 struct ib_send_wr *first, bool flush)
1236 struct ib_send_wr wr, *bad_wr;
1239 sge->addr = qe->dma;
1240 sge->length = sizeof(struct nvme_command),
1241 sge->lkey = queue->device->pd->local_dma_lkey;
1243 qe->cqe.done = nvme_rdma_send_done;
1246 wr.wr_cqe = &qe->cqe;
1248 wr.num_sge = num_sge;
1249 wr.opcode = IB_WR_SEND;
1253 * Unsignalled send completions are another giant desaster in the
1254 * IB Verbs spec: If we don't regularly post signalled sends
1255 * the send queue will fill up and only a QP reset will rescue us.
1256 * Would have been way to obvious to handle this in hardware or
1257 * at least the RDMA stack..
1259 * Always signal the flushes. The magic request used for the flush
1260 * sequencer is not allocated in our driver's tagset and it's
1261 * triggered to be freed by blk_cleanup_queue(). So we need to
1262 * always mark it as signaled to ensure that the "wr_cqe", which is
1263 * embedded in request's payload, is not freed when __ib_process_cq()
1264 * calls wr_cqe->done().
1266 if (nvme_rdma_queue_sig_limit(queue) || flush)
1267 wr.send_flags |= IB_SEND_SIGNALED;
1274 ret = ib_post_send(queue->qp, first, &bad_wr);
1275 if (unlikely(ret)) {
1276 dev_err(queue->ctrl->ctrl.device,
1277 "%s failed with error code %d\n", __func__, ret);
1282 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1283 struct nvme_rdma_qe *qe)
1285 struct ib_recv_wr wr, *bad_wr;
1289 list.addr = qe->dma;
1290 list.length = sizeof(struct nvme_completion);
1291 list.lkey = queue->device->pd->local_dma_lkey;
1293 qe->cqe.done = nvme_rdma_recv_done;
1296 wr.wr_cqe = &qe->cqe;
1300 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1301 if (unlikely(ret)) {
1302 dev_err(queue->ctrl->ctrl.device,
1303 "%s failed with error code %d\n", __func__, ret);
1308 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1310 u32 queue_idx = nvme_rdma_queue_idx(queue);
1313 return queue->ctrl->admin_tag_set.tags[queue_idx];
1314 return queue->ctrl->tag_set.tags[queue_idx - 1];
1317 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1319 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1320 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1321 struct ib_device *dev = queue->device->dev;
1322 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1323 struct nvme_command *cmd = sqe->data;
1327 if (WARN_ON_ONCE(aer_idx != 0))
1330 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1332 memset(cmd, 0, sizeof(*cmd));
1333 cmd->common.opcode = nvme_admin_async_event;
1334 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1335 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1336 nvme_rdma_set_sg_null(cmd);
1338 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1341 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1345 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1346 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1349 struct nvme_rdma_request *req;
1352 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1354 dev_err(queue->ctrl->ctrl.device,
1355 "tag 0x%x on QP %#x not found\n",
1356 cqe->command_id, queue->qp->qp_num);
1357 nvme_rdma_error_recovery(queue->ctrl);
1360 req = blk_mq_rq_to_pdu(rq);
1365 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1366 wc->ex.invalidate_rkey == req->mr->rkey)
1367 req->mr->need_inval = false;
1369 nvme_end_request(rq, cqe->status, cqe->result);
1373 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1375 struct nvme_rdma_qe *qe =
1376 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1377 struct nvme_rdma_queue *queue = cq->cq_context;
1378 struct ib_device *ibdev = queue->device->dev;
1379 struct nvme_completion *cqe = qe->data;
1380 const size_t len = sizeof(struct nvme_completion);
1383 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1384 nvme_rdma_wr_error(cq, wc, "RECV");
1388 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1390 * AEN requests are special as they don't time out and can
1391 * survive any kind of queue freeze and often don't respond to
1392 * aborts. We don't even bother to allocate a struct request
1393 * for them but rather special case them here.
1395 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1396 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
1397 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1400 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1401 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1403 nvme_rdma_post_recv(queue, qe);
1407 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1409 __nvme_rdma_recv_done(cq, wc, -1);
1412 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1416 for (i = 0; i < queue->queue_size; i++) {
1417 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1419 goto out_destroy_queue_ib;
1424 out_destroy_queue_ib:
1425 nvme_rdma_destroy_queue_ib(queue);
1429 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1430 struct rdma_cm_event *ev)
1432 struct rdma_cm_id *cm_id = queue->cm_id;
1433 int status = ev->status;
1434 const char *rej_msg;
1435 const struct nvme_rdma_cm_rej *rej_data;
1438 rej_msg = rdma_reject_msg(cm_id, status);
1439 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1441 if (rej_data && rej_data_len >= sizeof(u16)) {
1442 u16 sts = le16_to_cpu(rej_data->sts);
1444 dev_err(queue->ctrl->ctrl.device,
1445 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1446 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1448 dev_err(queue->ctrl->ctrl.device,
1449 "Connect rejected: status %d (%s).\n", status, rej_msg);
1455 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1459 ret = nvme_rdma_create_queue_ib(queue);
1463 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1465 dev_err(queue->ctrl->ctrl.device,
1466 "rdma_resolve_route failed (%d).\n",
1468 goto out_destroy_queue;
1474 nvme_rdma_destroy_queue_ib(queue);
1478 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1480 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1481 struct rdma_conn_param param = { };
1482 struct nvme_rdma_cm_req priv = { };
1485 param.qp_num = queue->qp->qp_num;
1486 param.flow_control = 1;
1488 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1489 /* maximum retry count */
1490 param.retry_count = 7;
1491 param.rnr_retry_count = 7;
1492 param.private_data = &priv;
1493 param.private_data_len = sizeof(priv);
1495 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1496 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1498 * set the admin queue depth to the minimum size
1499 * specified by the Fabrics standard.
1501 if (priv.qid == 0) {
1502 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1503 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
1506 * current interpretation of the fabrics spec
1507 * is at minimum you make hrqsize sqsize+1, or a
1508 * 1's based representation of sqsize.
1510 priv.hrqsize = cpu_to_le16(queue->queue_size);
1511 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1514 ret = rdma_connect(queue->cm_id, ¶m);
1516 dev_err(ctrl->ctrl.device,
1517 "rdma_connect failed (%d).\n", ret);
1518 goto out_destroy_queue_ib;
1523 out_destroy_queue_ib:
1524 nvme_rdma_destroy_queue_ib(queue);
1528 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1529 struct rdma_cm_event *ev)
1531 struct nvme_rdma_queue *queue = cm_id->context;
1534 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1535 rdma_event_msg(ev->event), ev->event,
1538 switch (ev->event) {
1539 case RDMA_CM_EVENT_ADDR_RESOLVED:
1540 cm_error = nvme_rdma_addr_resolved(queue);
1542 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1543 cm_error = nvme_rdma_route_resolved(queue);
1545 case RDMA_CM_EVENT_ESTABLISHED:
1546 queue->cm_error = nvme_rdma_conn_established(queue);
1547 /* complete cm_done regardless of success/failure */
1548 complete(&queue->cm_done);
1550 case RDMA_CM_EVENT_REJECTED:
1551 nvme_rdma_destroy_queue_ib(queue);
1552 cm_error = nvme_rdma_conn_rejected(queue, ev);
1554 case RDMA_CM_EVENT_ROUTE_ERROR:
1555 case RDMA_CM_EVENT_CONNECT_ERROR:
1556 case RDMA_CM_EVENT_UNREACHABLE:
1557 nvme_rdma_destroy_queue_ib(queue);
1558 case RDMA_CM_EVENT_ADDR_ERROR:
1559 dev_dbg(queue->ctrl->ctrl.device,
1560 "CM error event %d\n", ev->event);
1561 cm_error = -ECONNRESET;
1563 case RDMA_CM_EVENT_DISCONNECTED:
1564 case RDMA_CM_EVENT_ADDR_CHANGE:
1565 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1566 dev_dbg(queue->ctrl->ctrl.device,
1567 "disconnect received - connection closed\n");
1568 nvme_rdma_error_recovery(queue->ctrl);
1570 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1571 /* device removal is handled via the ib_client API */
1574 dev_err(queue->ctrl->ctrl.device,
1575 "Unexpected RDMA CM event (%d)\n", ev->event);
1576 nvme_rdma_error_recovery(queue->ctrl);
1581 queue->cm_error = cm_error;
1582 complete(&queue->cm_done);
1588 static enum blk_eh_timer_return
1589 nvme_rdma_timeout(struct request *rq, bool reserved)
1591 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1593 /* queue error recovery */
1594 nvme_rdma_error_recovery(req->queue->ctrl);
1596 /* fail with DNR on cmd timeout */
1597 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1599 return BLK_EH_HANDLED;
1603 * We cannot accept any other command until the Connect command has completed.
1605 static inline blk_status_t
1606 nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue, struct request *rq)
1608 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
1609 struct nvme_command *cmd = nvme_req(rq)->cmd;
1611 if (!blk_rq_is_passthrough(rq) ||
1612 cmd->common.opcode != nvme_fabrics_command ||
1613 cmd->fabrics.fctype != nvme_fabrics_type_connect) {
1615 * reconnecting state means transport disruption, which
1616 * can take a long time and even might fail permanently,
1617 * fail fast to give upper layers a chance to failover.
1618 * deleting state means that the ctrl will never accept
1619 * commands again, fail it permanently.
1621 if (queue->ctrl->ctrl.state == NVME_CTRL_RECONNECTING ||
1622 queue->ctrl->ctrl.state == NVME_CTRL_DELETING) {
1623 nvme_req(rq)->status = NVME_SC_ABORT_REQ;
1624 return BLK_STS_IOERR;
1626 return BLK_STS_RESOURCE; /* try again later */
1633 static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1634 const struct blk_mq_queue_data *bd)
1636 struct nvme_ns *ns = hctx->queue->queuedata;
1637 struct nvme_rdma_queue *queue = hctx->driver_data;
1638 struct request *rq = bd->rq;
1639 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1640 struct nvme_rdma_qe *sqe = &req->sqe;
1641 struct nvme_command *c = sqe->data;
1643 struct ib_device *dev;
1647 WARN_ON_ONCE(rq->tag < 0);
1649 ret = nvme_rdma_queue_is_ready(queue, rq);
1653 dev = queue->device->dev;
1654 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1655 sizeof(struct nvme_command), DMA_TO_DEVICE);
1657 ret = nvme_setup_cmd(ns, rq, c);
1661 blk_mq_start_request(rq);
1663 err = nvme_rdma_map_data(queue, rq, c);
1664 if (unlikely(err < 0)) {
1665 dev_err(queue->ctrl->ctrl.device,
1666 "Failed to map data (%d)\n", err);
1667 nvme_cleanup_cmd(rq);
1671 ib_dma_sync_single_for_device(dev, sqe->dma,
1672 sizeof(struct nvme_command), DMA_TO_DEVICE);
1674 if (req_op(rq) == REQ_OP_FLUSH)
1676 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1677 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
1678 if (unlikely(err)) {
1679 nvme_rdma_unmap_data(queue, rq);
1685 if (err == -ENOMEM || err == -EAGAIN)
1686 return BLK_STS_RESOURCE;
1687 return BLK_STS_IOERR;
1690 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1692 struct nvme_rdma_queue *queue = hctx->driver_data;
1693 struct ib_cq *cq = queue->ib_cq;
1697 while (ib_poll_cq(cq, 1, &wc) > 0) {
1698 struct ib_cqe *cqe = wc.wr_cqe;
1701 if (cqe->done == nvme_rdma_recv_done)
1702 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1711 static void nvme_rdma_complete_rq(struct request *rq)
1713 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1715 nvme_rdma_unmap_data(req->queue, rq);
1716 nvme_complete_rq(rq);
1719 static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
1721 struct nvme_rdma_ctrl *ctrl = set->driver_data;
1723 return blk_mq_rdma_map_queues(set, ctrl->device->dev, 0);
1726 static const struct blk_mq_ops nvme_rdma_mq_ops = {
1727 .queue_rq = nvme_rdma_queue_rq,
1728 .complete = nvme_rdma_complete_rq,
1729 .init_request = nvme_rdma_init_request,
1730 .exit_request = nvme_rdma_exit_request,
1731 .init_hctx = nvme_rdma_init_hctx,
1732 .poll = nvme_rdma_poll,
1733 .timeout = nvme_rdma_timeout,
1734 .map_queues = nvme_rdma_map_queues,
1737 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1738 .queue_rq = nvme_rdma_queue_rq,
1739 .complete = nvme_rdma_complete_rq,
1740 .init_request = nvme_rdma_init_request,
1741 .exit_request = nvme_rdma_exit_request,
1742 .init_hctx = nvme_rdma_init_admin_hctx,
1743 .timeout = nvme_rdma_timeout,
1746 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1748 cancel_work_sync(&ctrl->err_work);
1749 cancel_delayed_work_sync(&ctrl->reconnect_work);
1751 if (ctrl->ctrl.queue_count > 1) {
1752 nvme_stop_queues(&ctrl->ctrl);
1753 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1754 nvme_cancel_request, &ctrl->ctrl);
1755 nvme_rdma_destroy_io_queues(ctrl, shutdown);
1759 nvme_shutdown_ctrl(&ctrl->ctrl);
1761 nvme_disable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
1763 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
1764 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1765 nvme_cancel_request, &ctrl->ctrl);
1766 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
1767 nvme_rdma_destroy_admin_queue(ctrl, shutdown);
1770 static void nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl)
1772 nvme_remove_namespaces(&ctrl->ctrl);
1773 nvme_rdma_shutdown_ctrl(ctrl, true);
1774 nvme_uninit_ctrl(&ctrl->ctrl);
1775 nvme_put_ctrl(&ctrl->ctrl);
1778 static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1780 struct nvme_rdma_ctrl *ctrl = container_of(work,
1781 struct nvme_rdma_ctrl, delete_work);
1783 nvme_stop_ctrl(&ctrl->ctrl);
1784 nvme_rdma_remove_ctrl(ctrl);
1787 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1789 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1792 if (!queue_work(nvme_wq, &ctrl->delete_work))
1798 static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1800 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1804 * Keep a reference until all work is flushed since
1805 * __nvme_rdma_del_ctrl can free the ctrl mem
1807 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1809 ret = __nvme_rdma_del_ctrl(ctrl);
1811 flush_work(&ctrl->delete_work);
1812 nvme_put_ctrl(&ctrl->ctrl);
1816 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1818 struct nvme_rdma_ctrl *ctrl =
1819 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
1823 nvme_stop_ctrl(&ctrl->ctrl);
1824 nvme_rdma_shutdown_ctrl(ctrl, false);
1826 ret = nvme_rdma_configure_admin_queue(ctrl, false);
1830 if (ctrl->ctrl.queue_count > 1) {
1831 ret = nvme_rdma_configure_io_queues(ctrl, false);
1836 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1837 WARN_ON_ONCE(!changed);
1839 nvme_start_ctrl(&ctrl->ctrl);
1844 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1845 nvme_rdma_remove_ctrl(ctrl);
1848 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1850 .module = THIS_MODULE,
1851 .flags = NVME_F_FABRICS,
1852 .reg_read32 = nvmf_reg_read32,
1853 .reg_read64 = nvmf_reg_read64,
1854 .reg_write32 = nvmf_reg_write32,
1855 .free_ctrl = nvme_rdma_free_ctrl,
1856 .submit_async_event = nvme_rdma_submit_async_event,
1857 .delete_ctrl = nvme_rdma_del_ctrl,
1858 .get_address = nvmf_get_address,
1861 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1862 struct nvmf_ctrl_options *opts)
1864 struct nvme_rdma_ctrl *ctrl;
1869 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1871 return ERR_PTR(-ENOMEM);
1872 ctrl->ctrl.opts = opts;
1873 INIT_LIST_HEAD(&ctrl->list);
1875 if (opts->mask & NVMF_OPT_TRSVCID)
1876 port = opts->trsvcid;
1878 port = __stringify(NVME_RDMA_IP_PORT);
1880 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1881 opts->traddr, port, &ctrl->addr);
1883 pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
1887 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
1888 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1889 opts->host_traddr, NULL, &ctrl->src_addr);
1891 pr_err("malformed src address passed: %s\n",
1897 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1898 0 /* no quirks, we're perfect! */);
1902 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1903 nvme_rdma_reconnect_ctrl_work);
1904 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1905 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1906 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
1908 ctrl->ctrl.queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1909 ctrl->ctrl.sqsize = opts->queue_size - 1;
1910 ctrl->ctrl.kato = opts->kato;
1913 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
1916 goto out_uninit_ctrl;
1918 ret = nvme_rdma_configure_admin_queue(ctrl, true);
1920 goto out_kfree_queues;
1922 /* sanity check icdoff */
1923 if (ctrl->ctrl.icdoff) {
1924 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1926 goto out_remove_admin_queue;
1929 /* sanity check keyed sgls */
1930 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1931 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1933 goto out_remove_admin_queue;
1936 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1937 /* warn if maxcmd is lower than queue_size */
1938 dev_warn(ctrl->ctrl.device,
1939 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1940 opts->queue_size, ctrl->ctrl.maxcmd);
1941 opts->queue_size = ctrl->ctrl.maxcmd;
1944 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1945 /* warn if sqsize is lower than queue_size */
1946 dev_warn(ctrl->ctrl.device,
1947 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1948 opts->queue_size, ctrl->ctrl.sqsize + 1);
1949 opts->queue_size = ctrl->ctrl.sqsize + 1;
1952 if (opts->nr_io_queues) {
1953 ret = nvme_rdma_configure_io_queues(ctrl, true);
1955 goto out_remove_admin_queue;
1958 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1959 WARN_ON_ONCE(!changed);
1961 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
1962 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1964 kref_get(&ctrl->ctrl.kref);
1966 mutex_lock(&nvme_rdma_ctrl_mutex);
1967 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1968 mutex_unlock(&nvme_rdma_ctrl_mutex);
1970 nvme_start_ctrl(&ctrl->ctrl);
1974 out_remove_admin_queue:
1975 nvme_rdma_destroy_admin_queue(ctrl, true);
1977 kfree(ctrl->queues);
1979 nvme_uninit_ctrl(&ctrl->ctrl);
1980 nvme_put_ctrl(&ctrl->ctrl);
1983 return ERR_PTR(ret);
1986 return ERR_PTR(ret);
1989 static struct nvmf_transport_ops nvme_rdma_transport = {
1991 .required_opts = NVMF_OPT_TRADDR,
1992 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
1993 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
1994 .create_ctrl = nvme_rdma_create_ctrl,
1997 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
1999 struct nvme_rdma_ctrl *ctrl;
2001 /* Delete all controllers using this device */
2002 mutex_lock(&nvme_rdma_ctrl_mutex);
2003 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2004 if (ctrl->device->dev != ib_device)
2006 dev_info(ctrl->ctrl.device,
2007 "Removing ctrl: NQN \"%s\", addr %pISp\n",
2008 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2009 __nvme_rdma_del_ctrl(ctrl);
2011 mutex_unlock(&nvme_rdma_ctrl_mutex);
2013 flush_workqueue(nvme_wq);
2016 static struct ib_client nvme_rdma_ib_client = {
2017 .name = "nvme_rdma",
2018 .remove = nvme_rdma_remove_one
2021 static int __init nvme_rdma_init_module(void)
2025 ret = ib_register_client(&nvme_rdma_ib_client);
2029 ret = nvmf_register_transport(&nvme_rdma_transport);
2031 goto err_unreg_client;
2036 ib_unregister_client(&nvme_rdma_ib_client);
2040 static void __exit nvme_rdma_cleanup_module(void)
2042 nvmf_unregister_transport(&nvme_rdma_transport);
2043 ib_unregister_client(&nvme_rdma_ib_client);
2046 module_init(nvme_rdma_init_module);
2047 module_exit(nvme_rdma_cleanup_module);
2049 MODULE_LICENSE("GPL v2");