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/types.h>
23 #include <linux/list.h>
24 #include <linux/mutex.h>
25 #include <linux/scatterlist.h>
26 #include <linux/nvme.h>
27 #include <asm/unaligned.h>
29 #include <rdma/ib_verbs.h>
30 #include <rdma/rdma_cm.h>
31 #include <rdma/ib_cm.h>
32 #include <linux/nvme-rdma.h>
38 #define NVME_RDMA_CONNECT_TIMEOUT_MS 1000 /* 1 second */
40 #define NVME_RDMA_MAX_SEGMENT_SIZE 0xffffff /* 24-bit SGL field */
42 #define NVME_RDMA_MAX_SEGMENTS 256
44 #define NVME_RDMA_MAX_INLINE_SEGMENTS 1
47 * We handle AEN commands ourselves and don't even let the
48 * block layer know about them.
50 #define NVME_RDMA_NR_AEN_COMMANDS 1
51 #define NVME_RDMA_AQ_BLKMQ_DEPTH \
52 (NVMF_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
54 struct nvme_rdma_device {
55 struct ib_device *dev;
58 struct list_head entry;
67 struct nvme_rdma_queue;
68 struct nvme_rdma_request {
70 struct nvme_rdma_qe sqe;
71 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
75 struct ib_reg_wr reg_wr;
76 struct ib_cqe reg_cqe;
77 struct nvme_rdma_queue *queue;
78 struct sg_table sg_table;
79 struct scatterlist first_sgl[];
82 enum nvme_rdma_queue_flags {
83 NVME_RDMA_Q_CONNECTED = (1 << 0),
84 NVME_RDMA_IB_QUEUE_ALLOCATED = (1 << 1),
85 NVME_RDMA_Q_DELETING = (1 << 2),
86 NVME_RDMA_Q_LIVE = (1 << 3),
89 struct nvme_rdma_queue {
90 struct nvme_rdma_qe *rsp_ring;
93 size_t cmnd_capsule_len;
94 struct nvme_rdma_ctrl *ctrl;
95 struct nvme_rdma_device *device;
100 struct rdma_cm_id *cm_id;
102 struct completion cm_done;
105 struct nvme_rdma_ctrl {
106 /* read and written in the hot path */
109 /* read only in the hot path */
110 struct nvme_rdma_queue *queues;
113 /* other member variables */
114 struct blk_mq_tag_set tag_set;
115 struct work_struct delete_work;
116 struct work_struct reset_work;
117 struct work_struct err_work;
119 struct nvme_rdma_qe async_event_sqe;
122 struct delayed_work reconnect_work;
124 struct list_head list;
126 struct blk_mq_tag_set admin_tag_set;
127 struct nvme_rdma_device *device;
133 struct sockaddr addr;
134 struct sockaddr_in addr_in;
137 struct nvme_ctrl ctrl;
140 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
142 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
145 static LIST_HEAD(device_list);
146 static DEFINE_MUTEX(device_list_mutex);
148 static LIST_HEAD(nvme_rdma_ctrl_list);
149 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
151 static struct workqueue_struct *nvme_rdma_wq;
154 * Disabling this option makes small I/O goes faster, but is fundamentally
155 * unsafe. With it turned off we will have to register a global rkey that
156 * allows read and write access to all physical memory.
158 static bool register_always = true;
159 module_param(register_always, bool, 0444);
160 MODULE_PARM_DESC(register_always,
161 "Use memory registration even for contiguous memory regions");
163 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
164 struct rdma_cm_event *event);
165 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
167 /* XXX: really should move to a generic header sooner or later.. */
168 static inline void put_unaligned_le24(u32 val, u8 *p)
175 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
177 return queue - queue->ctrl->queues;
180 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
182 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
185 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
186 size_t capsule_size, enum dma_data_direction dir)
188 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
192 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
193 size_t capsule_size, enum dma_data_direction dir)
195 qe->data = kzalloc(capsule_size, GFP_KERNEL);
199 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
200 if (ib_dma_mapping_error(ibdev, qe->dma)) {
208 static void nvme_rdma_free_ring(struct ib_device *ibdev,
209 struct nvme_rdma_qe *ring, size_t ib_queue_size,
210 size_t capsule_size, enum dma_data_direction dir)
214 for (i = 0; i < ib_queue_size; i++)
215 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
219 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
220 size_t ib_queue_size, size_t capsule_size,
221 enum dma_data_direction dir)
223 struct nvme_rdma_qe *ring;
226 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
230 for (i = 0; i < ib_queue_size; i++) {
231 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
238 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
242 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
244 pr_debug("QP event %d\n", event->event);
247 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
249 wait_for_completion_interruptible_timeout(&queue->cm_done,
250 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
251 return queue->cm_error;
254 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
256 struct nvme_rdma_device *dev = queue->device;
257 struct ib_qp_init_attr init_attr;
260 memset(&init_attr, 0, sizeof(init_attr));
261 init_attr.event_handler = nvme_rdma_qp_event;
263 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
265 init_attr.cap.max_recv_wr = queue->queue_size + 1;
266 init_attr.cap.max_recv_sge = 1;
267 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
268 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
269 init_attr.qp_type = IB_QPT_RC;
270 init_attr.send_cq = queue->ib_cq;
271 init_attr.recv_cq = queue->ib_cq;
273 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
275 queue->qp = queue->cm_id->qp;
279 static int nvme_rdma_reinit_request(void *data, struct request *rq)
281 struct nvme_rdma_ctrl *ctrl = data;
282 struct nvme_rdma_device *dev = ctrl->device;
283 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
286 if (!req->mr->need_inval)
289 ib_dereg_mr(req->mr);
291 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
293 if (IS_ERR(req->mr)) {
294 ret = PTR_ERR(req->mr);
299 req->mr->need_inval = false;
305 static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl *ctrl,
306 struct request *rq, unsigned int queue_idx)
308 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
309 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
310 struct nvme_rdma_device *dev = queue->device;
313 ib_dereg_mr(req->mr);
315 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
319 static void nvme_rdma_exit_request(void *data, struct request *rq,
320 unsigned int hctx_idx, unsigned int rq_idx)
322 return __nvme_rdma_exit_request(data, rq, hctx_idx + 1);
325 static void nvme_rdma_exit_admin_request(void *data, struct request *rq,
326 unsigned int hctx_idx, unsigned int rq_idx)
328 return __nvme_rdma_exit_request(data, rq, 0);
331 static int __nvme_rdma_init_request(struct nvme_rdma_ctrl *ctrl,
332 struct request *rq, unsigned int queue_idx)
334 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
335 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
336 struct nvme_rdma_device *dev = queue->device;
337 struct ib_device *ibdev = dev->dev;
340 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
345 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
347 if (IS_ERR(req->mr)) {
348 ret = PTR_ERR(req->mr);
357 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
362 static int nvme_rdma_init_request(void *data, struct request *rq,
363 unsigned int hctx_idx, unsigned int rq_idx,
364 unsigned int numa_node)
366 return __nvme_rdma_init_request(data, rq, hctx_idx + 1);
369 static int nvme_rdma_init_admin_request(void *data, struct request *rq,
370 unsigned int hctx_idx, unsigned int rq_idx,
371 unsigned int numa_node)
373 return __nvme_rdma_init_request(data, rq, 0);
376 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
377 unsigned int hctx_idx)
379 struct nvme_rdma_ctrl *ctrl = data;
380 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
382 BUG_ON(hctx_idx >= ctrl->queue_count);
384 hctx->driver_data = queue;
388 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
389 unsigned int hctx_idx)
391 struct nvme_rdma_ctrl *ctrl = data;
392 struct nvme_rdma_queue *queue = &ctrl->queues[0];
394 BUG_ON(hctx_idx != 0);
396 hctx->driver_data = queue;
400 static void nvme_rdma_free_dev(struct kref *ref)
402 struct nvme_rdma_device *ndev =
403 container_of(ref, struct nvme_rdma_device, ref);
405 mutex_lock(&device_list_mutex);
406 list_del(&ndev->entry);
407 mutex_unlock(&device_list_mutex);
409 ib_dealloc_pd(ndev->pd);
413 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
415 kref_put(&dev->ref, nvme_rdma_free_dev);
418 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
420 return kref_get_unless_zero(&dev->ref);
423 static struct nvme_rdma_device *
424 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
426 struct nvme_rdma_device *ndev;
428 mutex_lock(&device_list_mutex);
429 list_for_each_entry(ndev, &device_list, entry) {
430 if (ndev->dev->node_guid == cm_id->device->node_guid &&
431 nvme_rdma_dev_get(ndev))
435 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
439 ndev->dev = cm_id->device;
440 kref_init(&ndev->ref);
442 ndev->pd = ib_alloc_pd(ndev->dev,
443 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
444 if (IS_ERR(ndev->pd))
447 if (!(ndev->dev->attrs.device_cap_flags &
448 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
449 dev_err(&ndev->dev->dev,
450 "Memory registrations not supported.\n");
454 list_add(&ndev->entry, &device_list);
456 mutex_unlock(&device_list_mutex);
460 ib_dealloc_pd(ndev->pd);
464 mutex_unlock(&device_list_mutex);
468 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
470 struct nvme_rdma_device *dev;
471 struct ib_device *ibdev;
473 if (!test_and_clear_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags))
478 rdma_destroy_qp(queue->cm_id);
479 ib_free_cq(queue->ib_cq);
481 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
482 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
484 nvme_rdma_dev_put(dev);
487 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue,
488 struct nvme_rdma_device *dev)
490 struct ib_device *ibdev = dev->dev;
491 const int send_wr_factor = 3; /* MR, SEND, INV */
492 const int cq_factor = send_wr_factor + 1; /* + RECV */
493 int comp_vector, idx = nvme_rdma_queue_idx(queue);
500 * The admin queue is barely used once the controller is live, so don't
501 * bother to spread it out.
506 comp_vector = idx % ibdev->num_comp_vectors;
509 /* +1 for ib_stop_cq */
510 queue->ib_cq = ib_alloc_cq(dev->dev, queue,
511 cq_factor * queue->queue_size + 1, comp_vector,
513 if (IS_ERR(queue->ib_cq)) {
514 ret = PTR_ERR(queue->ib_cq);
518 ret = nvme_rdma_create_qp(queue, send_wr_factor);
520 goto out_destroy_ib_cq;
522 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
523 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
524 if (!queue->rsp_ring) {
528 set_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags);
533 ib_destroy_qp(queue->qp);
535 ib_free_cq(queue->ib_cq);
540 static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl,
541 int idx, size_t queue_size)
543 struct nvme_rdma_queue *queue;
546 queue = &ctrl->queues[idx];
548 init_completion(&queue->cm_done);
551 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
553 queue->cmnd_capsule_len = sizeof(struct nvme_command);
555 queue->queue_size = queue_size;
556 atomic_set(&queue->sig_count, 0);
558 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
559 RDMA_PS_TCP, IB_QPT_RC);
560 if (IS_ERR(queue->cm_id)) {
561 dev_info(ctrl->ctrl.device,
562 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
563 return PTR_ERR(queue->cm_id);
566 queue->cm_error = -ETIMEDOUT;
567 ret = rdma_resolve_addr(queue->cm_id, NULL, &ctrl->addr,
568 NVME_RDMA_CONNECT_TIMEOUT_MS);
570 dev_info(ctrl->ctrl.device,
571 "rdma_resolve_addr failed (%d).\n", ret);
572 goto out_destroy_cm_id;
575 ret = nvme_rdma_wait_for_cm(queue);
577 dev_info(ctrl->ctrl.device,
578 "rdma_resolve_addr wait failed (%d).\n", ret);
579 goto out_destroy_cm_id;
582 clear_bit(NVME_RDMA_Q_DELETING, &queue->flags);
583 set_bit(NVME_RDMA_Q_CONNECTED, &queue->flags);
588 nvme_rdma_destroy_queue_ib(queue);
589 rdma_destroy_id(queue->cm_id);
593 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
595 rdma_disconnect(queue->cm_id);
596 ib_drain_qp(queue->qp);
599 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
601 nvme_rdma_destroy_queue_ib(queue);
602 rdma_destroy_id(queue->cm_id);
605 static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue)
607 if (test_and_set_bit(NVME_RDMA_Q_DELETING, &queue->flags))
609 nvme_rdma_stop_queue(queue);
610 nvme_rdma_free_queue(queue);
613 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
617 for (i = 1; i < ctrl->queue_count; i++)
618 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
621 static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl)
625 for (i = 1; i < ctrl->queue_count; i++) {
626 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
628 dev_info(ctrl->ctrl.device,
629 "failed to connect i/o queue: %d\n", ret);
630 goto out_free_queues;
632 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
638 nvme_rdma_free_io_queues(ctrl);
642 static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl)
644 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
645 unsigned int nr_io_queues;
648 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
649 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
653 ctrl->queue_count = nr_io_queues + 1;
654 if (ctrl->queue_count < 2)
657 dev_info(ctrl->ctrl.device,
658 "creating %d I/O queues.\n", nr_io_queues);
660 for (i = 1; i < ctrl->queue_count; i++) {
661 ret = nvme_rdma_init_queue(ctrl, i,
662 ctrl->ctrl.opts->queue_size);
664 dev_info(ctrl->ctrl.device,
665 "failed to initialize i/o queue: %d\n", ret);
666 goto out_free_queues;
673 for (i--; i >= 1; i--)
674 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
679 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl)
681 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
682 sizeof(struct nvme_command), DMA_TO_DEVICE);
683 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
684 blk_cleanup_queue(ctrl->ctrl.admin_q);
685 blk_mq_free_tag_set(&ctrl->admin_tag_set);
686 nvme_rdma_dev_put(ctrl->device);
689 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
691 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
693 if (list_empty(&ctrl->list))
696 mutex_lock(&nvme_rdma_ctrl_mutex);
697 list_del(&ctrl->list);
698 mutex_unlock(&nvme_rdma_ctrl_mutex);
701 nvmf_free_options(nctrl->opts);
706 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
708 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
709 struct nvme_rdma_ctrl, reconnect_work);
713 if (ctrl->queue_count > 1) {
714 nvme_rdma_free_io_queues(ctrl);
716 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
721 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
723 ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set);
727 ret = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
731 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
733 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
737 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
739 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
743 nvme_start_keep_alive(&ctrl->ctrl);
745 if (ctrl->queue_count > 1) {
746 ret = nvme_rdma_init_io_queues(ctrl);
750 ret = nvme_rdma_connect_io_queues(ctrl);
755 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
756 WARN_ON_ONCE(!changed);
758 if (ctrl->queue_count > 1) {
759 nvme_start_queues(&ctrl->ctrl);
760 nvme_queue_scan(&ctrl->ctrl);
761 nvme_queue_async_events(&ctrl->ctrl);
764 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
769 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
771 /* Make sure we are not resetting/deleting */
772 if (ctrl->ctrl.state == NVME_CTRL_RECONNECTING) {
773 dev_info(ctrl->ctrl.device,
774 "Failed reconnect attempt, requeueing...\n");
775 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
776 ctrl->reconnect_delay * HZ);
780 static void nvme_rdma_error_recovery_work(struct work_struct *work)
782 struct nvme_rdma_ctrl *ctrl = container_of(work,
783 struct nvme_rdma_ctrl, err_work);
786 nvme_stop_keep_alive(&ctrl->ctrl);
788 for (i = 0; i < ctrl->queue_count; i++) {
789 clear_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[i].flags);
790 clear_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
793 if (ctrl->queue_count > 1)
794 nvme_stop_queues(&ctrl->ctrl);
795 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
797 /* We must take care of fastfail/requeue all our inflight requests */
798 if (ctrl->queue_count > 1)
799 blk_mq_tagset_busy_iter(&ctrl->tag_set,
800 nvme_cancel_request, &ctrl->ctrl);
801 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
802 nvme_cancel_request, &ctrl->ctrl);
804 dev_info(ctrl->ctrl.device, "reconnecting in %d seconds\n",
805 ctrl->reconnect_delay);
807 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
808 ctrl->reconnect_delay * HZ);
811 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
813 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
816 queue_work(nvme_rdma_wq, &ctrl->err_work);
819 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
822 struct nvme_rdma_queue *queue = cq->cq_context;
823 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
825 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
826 dev_info(ctrl->ctrl.device,
827 "%s for CQE 0x%p failed with status %s (%d)\n",
829 ib_wc_status_msg(wc->status), wc->status);
830 nvme_rdma_error_recovery(ctrl);
833 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
835 if (unlikely(wc->status != IB_WC_SUCCESS))
836 nvme_rdma_wr_error(cq, wc, "MEMREG");
839 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
841 if (unlikely(wc->status != IB_WC_SUCCESS))
842 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
845 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
846 struct nvme_rdma_request *req)
848 struct ib_send_wr *bad_wr;
849 struct ib_send_wr wr = {
850 .opcode = IB_WR_LOCAL_INV,
854 .ex.invalidate_rkey = req->mr->rkey,
857 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
858 wr.wr_cqe = &req->reg_cqe;
860 return ib_post_send(queue->qp, &wr, &bad_wr);
863 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
866 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
867 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
868 struct nvme_rdma_device *dev = queue->device;
869 struct ib_device *ibdev = dev->dev;
872 if (!blk_rq_bytes(rq))
875 if (req->mr->need_inval) {
876 res = nvme_rdma_inv_rkey(queue, req);
878 dev_err(ctrl->ctrl.device,
879 "Queueing INV WR for rkey %#x failed (%d)\n",
881 nvme_rdma_error_recovery(queue->ctrl);
885 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
886 req->nents, rq_data_dir(rq) ==
887 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
889 nvme_cleanup_cmd(rq);
890 sg_free_table_chained(&req->sg_table, true);
893 static int nvme_rdma_set_sg_null(struct nvme_command *c)
895 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
898 put_unaligned_le24(0, sg->length);
899 put_unaligned_le32(0, sg->key);
900 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
904 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
905 struct nvme_rdma_request *req, struct nvme_command *c)
907 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
909 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
910 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
911 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
913 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
914 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
915 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
917 req->inline_data = true;
922 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
923 struct nvme_rdma_request *req, struct nvme_command *c)
925 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
927 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
928 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
929 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
930 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
934 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
935 struct nvme_rdma_request *req, struct nvme_command *c,
938 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
941 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE);
948 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
950 req->reg_cqe.done = nvme_rdma_memreg_done;
951 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
952 req->reg_wr.wr.opcode = IB_WR_REG_MR;
953 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
954 req->reg_wr.wr.num_sge = 0;
955 req->reg_wr.mr = req->mr;
956 req->reg_wr.key = req->mr->rkey;
957 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
958 IB_ACCESS_REMOTE_READ |
959 IB_ACCESS_REMOTE_WRITE;
961 req->mr->need_inval = true;
963 sg->addr = cpu_to_le64(req->mr->iova);
964 put_unaligned_le24(req->mr->length, sg->length);
965 put_unaligned_le32(req->mr->rkey, sg->key);
966 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
967 NVME_SGL_FMT_INVALIDATE;
972 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
973 struct request *rq, unsigned int map_len,
974 struct nvme_command *c)
976 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
977 struct nvme_rdma_device *dev = queue->device;
978 struct ib_device *ibdev = dev->dev;
983 req->inline_data = false;
984 req->mr->need_inval = false;
986 c->common.flags |= NVME_CMD_SGL_METABUF;
988 if (!blk_rq_bytes(rq))
989 return nvme_rdma_set_sg_null(c);
991 req->sg_table.sgl = req->first_sgl;
992 ret = sg_alloc_table_chained(&req->sg_table, rq->nr_phys_segments,
997 nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
998 BUG_ON(nents > rq->nr_phys_segments);
1001 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, nents,
1002 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1003 if (unlikely(count <= 0)) {
1004 sg_free_table_chained(&req->sg_table, true);
1009 if (rq_data_dir(rq) == WRITE &&
1010 map_len <= nvme_rdma_inline_data_size(queue) &&
1011 nvme_rdma_queue_idx(queue))
1012 return nvme_rdma_map_sg_inline(queue, req, c);
1014 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
1015 return nvme_rdma_map_sg_single(queue, req, c);
1018 return nvme_rdma_map_sg_fr(queue, req, c, count);
1021 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1023 if (unlikely(wc->status != IB_WC_SUCCESS))
1024 nvme_rdma_wr_error(cq, wc, "SEND");
1028 * We want to signal completion at least every queue depth/2. This returns the
1029 * largest power of two that is not above half of (queue size + 1) to optimize
1030 * (avoid divisions).
1032 static inline bool nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue)
1034 int limit = 1 << ilog2((queue->queue_size + 1) / 2);
1036 return (atomic_inc_return(&queue->sig_count) & (limit - 1)) == 0;
1039 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1040 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1041 struct ib_send_wr *first, bool flush)
1043 struct ib_send_wr wr, *bad_wr;
1046 sge->addr = qe->dma;
1047 sge->length = sizeof(struct nvme_command),
1048 sge->lkey = queue->device->pd->local_dma_lkey;
1050 qe->cqe.done = nvme_rdma_send_done;
1053 wr.wr_cqe = &qe->cqe;
1055 wr.num_sge = num_sge;
1056 wr.opcode = IB_WR_SEND;
1060 * Unsignalled send completions are another giant desaster in the
1061 * IB Verbs spec: If we don't regularly post signalled sends
1062 * the send queue will fill up and only a QP reset will rescue us.
1063 * Would have been way to obvious to handle this in hardware or
1064 * at least the RDMA stack..
1066 * Always signal the flushes. The magic request used for the flush
1067 * sequencer is not allocated in our driver's tagset and it's
1068 * triggered to be freed by blk_cleanup_queue(). So we need to
1069 * always mark it as signaled to ensure that the "wr_cqe", which is
1070 * embeded in request's payload, is not freed when __ib_process_cq()
1071 * calls wr_cqe->done().
1073 if (nvme_rdma_queue_sig_limit(queue) || flush)
1074 wr.send_flags |= IB_SEND_SIGNALED;
1081 ret = ib_post_send(queue->qp, first, &bad_wr);
1083 dev_err(queue->ctrl->ctrl.device,
1084 "%s failed with error code %d\n", __func__, ret);
1089 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1090 struct nvme_rdma_qe *qe)
1092 struct ib_recv_wr wr, *bad_wr;
1096 list.addr = qe->dma;
1097 list.length = sizeof(struct nvme_completion);
1098 list.lkey = queue->device->pd->local_dma_lkey;
1100 qe->cqe.done = nvme_rdma_recv_done;
1103 wr.wr_cqe = &qe->cqe;
1107 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1109 dev_err(queue->ctrl->ctrl.device,
1110 "%s failed with error code %d\n", __func__, ret);
1115 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1117 u32 queue_idx = nvme_rdma_queue_idx(queue);
1120 return queue->ctrl->admin_tag_set.tags[queue_idx];
1121 return queue->ctrl->tag_set.tags[queue_idx - 1];
1124 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1126 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1127 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1128 struct ib_device *dev = queue->device->dev;
1129 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1130 struct nvme_command *cmd = sqe->data;
1134 if (WARN_ON_ONCE(aer_idx != 0))
1137 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1139 memset(cmd, 0, sizeof(*cmd));
1140 cmd->common.opcode = nvme_admin_async_event;
1141 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1142 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1143 nvme_rdma_set_sg_null(cmd);
1145 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1148 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1152 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1153 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1155 u16 status = le16_to_cpu(cqe->status);
1157 struct nvme_rdma_request *req;
1162 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1164 dev_err(queue->ctrl->ctrl.device,
1165 "tag 0x%x on QP %#x not found\n",
1166 cqe->command_id, queue->qp->qp_num);
1167 nvme_rdma_error_recovery(queue->ctrl);
1170 req = blk_mq_rq_to_pdu(rq);
1172 if (rq->cmd_type == REQ_TYPE_DRV_PRIV && rq->special)
1173 memcpy(rq->special, cqe, sizeof(*cqe));
1178 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1179 wc->ex.invalidate_rkey == req->mr->rkey)
1180 req->mr->need_inval = false;
1182 blk_mq_complete_request(rq, status);
1187 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1189 struct nvme_rdma_qe *qe =
1190 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1191 struct nvme_rdma_queue *queue = cq->cq_context;
1192 struct ib_device *ibdev = queue->device->dev;
1193 struct nvme_completion *cqe = qe->data;
1194 const size_t len = sizeof(struct nvme_completion);
1197 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1198 nvme_rdma_wr_error(cq, wc, "RECV");
1202 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1204 * AEN requests are special as they don't time out and can
1205 * survive any kind of queue freeze and often don't respond to
1206 * aborts. We don't even bother to allocate a struct request
1207 * for them but rather special case them here.
1209 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1210 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
1211 nvme_complete_async_event(&queue->ctrl->ctrl, cqe);
1213 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1214 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1216 nvme_rdma_post_recv(queue, qe);
1220 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1222 __nvme_rdma_recv_done(cq, wc, -1);
1225 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1229 for (i = 0; i < queue->queue_size; i++) {
1230 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1232 goto out_destroy_queue_ib;
1237 out_destroy_queue_ib:
1238 nvme_rdma_destroy_queue_ib(queue);
1242 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1243 struct rdma_cm_event *ev)
1245 if (ev->param.conn.private_data_len) {
1246 struct nvme_rdma_cm_rej *rej =
1247 (struct nvme_rdma_cm_rej *)ev->param.conn.private_data;
1249 dev_err(queue->ctrl->ctrl.device,
1250 "Connect rejected, status %d.", le16_to_cpu(rej->sts));
1251 /* XXX: Think of something clever to do here... */
1253 dev_err(queue->ctrl->ctrl.device,
1254 "Connect rejected, no private data.\n");
1260 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1262 struct nvme_rdma_device *dev;
1265 dev = nvme_rdma_find_get_device(queue->cm_id);
1267 dev_err(queue->cm_id->device->dma_device,
1268 "no client data found!\n");
1269 return -ECONNREFUSED;
1272 ret = nvme_rdma_create_queue_ib(queue, dev);
1274 nvme_rdma_dev_put(dev);
1278 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1280 dev_err(queue->ctrl->ctrl.device,
1281 "rdma_resolve_route failed (%d).\n",
1283 goto out_destroy_queue;
1289 nvme_rdma_destroy_queue_ib(queue);
1294 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1296 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1297 struct rdma_conn_param param = { };
1298 struct nvme_rdma_cm_req priv = { };
1301 param.qp_num = queue->qp->qp_num;
1302 param.flow_control = 1;
1304 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1305 /* maximum retry count */
1306 param.retry_count = 7;
1307 param.rnr_retry_count = 7;
1308 param.private_data = &priv;
1309 param.private_data_len = sizeof(priv);
1311 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1312 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1314 * set the admin queue depth to the minimum size
1315 * specified by the Fabrics standard.
1317 if (priv.qid == 0) {
1318 priv.hrqsize = cpu_to_le16(NVMF_AQ_DEPTH);
1319 priv.hsqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1);
1322 * current interpretation of the fabrics spec
1323 * is at minimum you make hrqsize sqsize+1, or a
1324 * 1's based representation of sqsize.
1326 priv.hrqsize = cpu_to_le16(queue->queue_size);
1327 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1330 ret = rdma_connect(queue->cm_id, ¶m);
1332 dev_err(ctrl->ctrl.device,
1333 "rdma_connect failed (%d).\n", ret);
1334 goto out_destroy_queue_ib;
1339 out_destroy_queue_ib:
1340 nvme_rdma_destroy_queue_ib(queue);
1344 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1345 struct rdma_cm_event *ev)
1347 struct nvme_rdma_queue *queue = cm_id->context;
1350 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1351 rdma_event_msg(ev->event), ev->event,
1354 switch (ev->event) {
1355 case RDMA_CM_EVENT_ADDR_RESOLVED:
1356 cm_error = nvme_rdma_addr_resolved(queue);
1358 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1359 cm_error = nvme_rdma_route_resolved(queue);
1361 case RDMA_CM_EVENT_ESTABLISHED:
1362 queue->cm_error = nvme_rdma_conn_established(queue);
1363 /* complete cm_done regardless of success/failure */
1364 complete(&queue->cm_done);
1366 case RDMA_CM_EVENT_REJECTED:
1367 cm_error = nvme_rdma_conn_rejected(queue, ev);
1369 case RDMA_CM_EVENT_ADDR_ERROR:
1370 case RDMA_CM_EVENT_ROUTE_ERROR:
1371 case RDMA_CM_EVENT_CONNECT_ERROR:
1372 case RDMA_CM_EVENT_UNREACHABLE:
1373 dev_dbg(queue->ctrl->ctrl.device,
1374 "CM error event %d\n", ev->event);
1375 cm_error = -ECONNRESET;
1377 case RDMA_CM_EVENT_DISCONNECTED:
1378 case RDMA_CM_EVENT_ADDR_CHANGE:
1379 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1380 dev_dbg(queue->ctrl->ctrl.device,
1381 "disconnect received - connection closed\n");
1382 nvme_rdma_error_recovery(queue->ctrl);
1384 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1385 /* device removal is handled via the ib_client API */
1388 dev_err(queue->ctrl->ctrl.device,
1389 "Unexpected RDMA CM event (%d)\n", ev->event);
1390 nvme_rdma_error_recovery(queue->ctrl);
1395 queue->cm_error = cm_error;
1396 complete(&queue->cm_done);
1402 static enum blk_eh_timer_return
1403 nvme_rdma_timeout(struct request *rq, bool reserved)
1405 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1407 /* queue error recovery */
1408 nvme_rdma_error_recovery(req->queue->ctrl);
1410 /* fail with DNR on cmd timeout */
1411 rq->errors = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1413 return BLK_EH_HANDLED;
1417 * We cannot accept any other command until the Connect command has completed.
1419 static inline bool nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue,
1422 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
1423 struct nvme_command *cmd = (struct nvme_command *)rq->cmd;
1425 if (rq->cmd_type != REQ_TYPE_DRV_PRIV ||
1426 cmd->common.opcode != nvme_fabrics_command ||
1427 cmd->fabrics.fctype != nvme_fabrics_type_connect)
1434 static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1435 const struct blk_mq_queue_data *bd)
1437 struct nvme_ns *ns = hctx->queue->queuedata;
1438 struct nvme_rdma_queue *queue = hctx->driver_data;
1439 struct request *rq = bd->rq;
1440 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1441 struct nvme_rdma_qe *sqe = &req->sqe;
1442 struct nvme_command *c = sqe->data;
1444 struct ib_device *dev;
1445 unsigned int map_len;
1448 WARN_ON_ONCE(rq->tag < 0);
1450 if (!nvme_rdma_queue_is_ready(queue, rq))
1451 return BLK_MQ_RQ_QUEUE_BUSY;
1453 dev = queue->device->dev;
1454 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1455 sizeof(struct nvme_command), DMA_TO_DEVICE);
1457 ret = nvme_setup_cmd(ns, rq, c);
1461 c->common.command_id = rq->tag;
1462 blk_mq_start_request(rq);
1464 map_len = nvme_map_len(rq);
1465 ret = nvme_rdma_map_data(queue, rq, map_len, c);
1467 dev_err(queue->ctrl->ctrl.device,
1468 "Failed to map data (%d)\n", ret);
1469 nvme_cleanup_cmd(rq);
1473 ib_dma_sync_single_for_device(dev, sqe->dma,
1474 sizeof(struct nvme_command), DMA_TO_DEVICE);
1476 if (rq->cmd_type == REQ_TYPE_FS && req_op(rq) == REQ_OP_FLUSH)
1478 ret = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1479 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
1481 nvme_rdma_unmap_data(queue, rq);
1485 return BLK_MQ_RQ_QUEUE_OK;
1487 return (ret == -ENOMEM || ret == -EAGAIN) ?
1488 BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR;
1491 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1493 struct nvme_rdma_queue *queue = hctx->driver_data;
1494 struct ib_cq *cq = queue->ib_cq;
1498 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1499 while (ib_poll_cq(cq, 1, &wc) > 0) {
1500 struct ib_cqe *cqe = wc.wr_cqe;
1503 if (cqe->done == nvme_rdma_recv_done)
1504 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1513 static void nvme_rdma_complete_rq(struct request *rq)
1515 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1516 struct nvme_rdma_queue *queue = req->queue;
1519 nvme_rdma_unmap_data(queue, rq);
1521 if (unlikely(rq->errors)) {
1522 if (nvme_req_needs_retry(rq, rq->errors)) {
1523 nvme_requeue_req(rq);
1527 if (rq->cmd_type == REQ_TYPE_DRV_PRIV)
1530 error = nvme_error_status(rq->errors);
1533 blk_mq_end_request(rq, error);
1536 static struct blk_mq_ops nvme_rdma_mq_ops = {
1537 .queue_rq = nvme_rdma_queue_rq,
1538 .complete = nvme_rdma_complete_rq,
1539 .init_request = nvme_rdma_init_request,
1540 .exit_request = nvme_rdma_exit_request,
1541 .reinit_request = nvme_rdma_reinit_request,
1542 .init_hctx = nvme_rdma_init_hctx,
1543 .poll = nvme_rdma_poll,
1544 .timeout = nvme_rdma_timeout,
1547 static struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1548 .queue_rq = nvme_rdma_queue_rq,
1549 .complete = nvme_rdma_complete_rq,
1550 .init_request = nvme_rdma_init_admin_request,
1551 .exit_request = nvme_rdma_exit_admin_request,
1552 .reinit_request = nvme_rdma_reinit_request,
1553 .init_hctx = nvme_rdma_init_admin_hctx,
1554 .timeout = nvme_rdma_timeout,
1557 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
1561 error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
1565 ctrl->device = ctrl->queues[0].device;
1568 * We need a reference on the device as long as the tag_set is alive,
1569 * as the MRs in the request structures need a valid ib_device.
1572 if (!nvme_rdma_dev_get(ctrl->device))
1573 goto out_free_queue;
1575 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
1576 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
1578 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1579 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
1580 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
1581 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
1582 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1583 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1584 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1585 ctrl->admin_tag_set.driver_data = ctrl;
1586 ctrl->admin_tag_set.nr_hw_queues = 1;
1587 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1589 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1593 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1594 if (IS_ERR(ctrl->ctrl.admin_q)) {
1595 error = PTR_ERR(ctrl->ctrl.admin_q);
1596 goto out_free_tagset;
1599 error = nvmf_connect_admin_queue(&ctrl->ctrl);
1601 goto out_cleanup_queue;
1603 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
1605 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
1607 dev_err(ctrl->ctrl.device,
1608 "prop_get NVME_REG_CAP failed\n");
1609 goto out_cleanup_queue;
1613 min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize);
1615 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
1617 goto out_cleanup_queue;
1619 ctrl->ctrl.max_hw_sectors =
1620 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
1622 error = nvme_init_identify(&ctrl->ctrl);
1624 goto out_cleanup_queue;
1626 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
1627 &ctrl->async_event_sqe, sizeof(struct nvme_command),
1630 goto out_cleanup_queue;
1632 nvme_start_keep_alive(&ctrl->ctrl);
1637 blk_cleanup_queue(ctrl->ctrl.admin_q);
1639 /* disconnect and drain the queue before freeing the tagset */
1640 nvme_rdma_stop_queue(&ctrl->queues[0]);
1641 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1643 nvme_rdma_dev_put(ctrl->device);
1645 nvme_rdma_free_queue(&ctrl->queues[0]);
1649 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1651 nvme_stop_keep_alive(&ctrl->ctrl);
1652 cancel_work_sync(&ctrl->err_work);
1653 cancel_delayed_work_sync(&ctrl->reconnect_work);
1655 if (ctrl->queue_count > 1) {
1656 nvme_stop_queues(&ctrl->ctrl);
1657 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1658 nvme_cancel_request, &ctrl->ctrl);
1659 nvme_rdma_free_io_queues(ctrl);
1662 if (test_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[0].flags))
1663 nvme_shutdown_ctrl(&ctrl->ctrl);
1665 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
1666 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1667 nvme_cancel_request, &ctrl->ctrl);
1668 nvme_rdma_destroy_admin_queue(ctrl);
1671 static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1673 nvme_uninit_ctrl(&ctrl->ctrl);
1675 nvme_rdma_shutdown_ctrl(ctrl);
1677 if (ctrl->ctrl.tagset) {
1678 blk_cleanup_queue(ctrl->ctrl.connect_q);
1679 blk_mq_free_tag_set(&ctrl->tag_set);
1680 nvme_rdma_dev_put(ctrl->device);
1683 nvme_put_ctrl(&ctrl->ctrl);
1686 static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1688 struct nvme_rdma_ctrl *ctrl = container_of(work,
1689 struct nvme_rdma_ctrl, delete_work);
1691 __nvme_rdma_remove_ctrl(ctrl, true);
1694 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1696 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1699 if (!queue_work(nvme_rdma_wq, &ctrl->delete_work))
1705 static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1707 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1711 * Keep a reference until all work is flushed since
1712 * __nvme_rdma_del_ctrl can free the ctrl mem
1714 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1716 ret = __nvme_rdma_del_ctrl(ctrl);
1718 flush_work(&ctrl->delete_work);
1719 nvme_put_ctrl(&ctrl->ctrl);
1723 static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1725 struct nvme_rdma_ctrl *ctrl = container_of(work,
1726 struct nvme_rdma_ctrl, delete_work);
1728 __nvme_rdma_remove_ctrl(ctrl, false);
1731 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1733 struct nvme_rdma_ctrl *ctrl = container_of(work,
1734 struct nvme_rdma_ctrl, reset_work);
1738 nvme_rdma_shutdown_ctrl(ctrl);
1740 ret = nvme_rdma_configure_admin_queue(ctrl);
1742 /* ctrl is already shutdown, just remove the ctrl */
1743 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1747 if (ctrl->queue_count > 1) {
1748 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
1752 ret = nvme_rdma_init_io_queues(ctrl);
1756 ret = nvme_rdma_connect_io_queues(ctrl);
1761 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1762 WARN_ON_ONCE(!changed);
1764 if (ctrl->queue_count > 1) {
1765 nvme_start_queues(&ctrl->ctrl);
1766 nvme_queue_scan(&ctrl->ctrl);
1767 nvme_queue_async_events(&ctrl->ctrl);
1773 /* Deleting this dead controller... */
1774 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1775 WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work));
1778 static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl)
1780 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1782 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1785 if (!queue_work(nvme_rdma_wq, &ctrl->reset_work))
1788 flush_work(&ctrl->reset_work);
1793 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1795 .module = THIS_MODULE,
1797 .reg_read32 = nvmf_reg_read32,
1798 .reg_read64 = nvmf_reg_read64,
1799 .reg_write32 = nvmf_reg_write32,
1800 .reset_ctrl = nvme_rdma_reset_ctrl,
1801 .free_ctrl = nvme_rdma_free_ctrl,
1802 .submit_async_event = nvme_rdma_submit_async_event,
1803 .delete_ctrl = nvme_rdma_del_ctrl,
1804 .get_subsysnqn = nvmf_get_subsysnqn,
1805 .get_address = nvmf_get_address,
1808 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1812 ret = nvme_rdma_init_io_queues(ctrl);
1817 * We need a reference on the device as long as the tag_set is alive,
1818 * as the MRs in the request structures need a valid ib_device.
1821 if (!nvme_rdma_dev_get(ctrl->device))
1822 goto out_free_io_queues;
1824 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1825 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
1826 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
1827 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1828 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1829 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1830 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1831 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1832 ctrl->tag_set.driver_data = ctrl;
1833 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
1834 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1836 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1839 ctrl->ctrl.tagset = &ctrl->tag_set;
1841 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1842 if (IS_ERR(ctrl->ctrl.connect_q)) {
1843 ret = PTR_ERR(ctrl->ctrl.connect_q);
1844 goto out_free_tag_set;
1847 ret = nvme_rdma_connect_io_queues(ctrl);
1849 goto out_cleanup_connect_q;
1853 out_cleanup_connect_q:
1854 blk_cleanup_queue(ctrl->ctrl.connect_q);
1856 blk_mq_free_tag_set(&ctrl->tag_set);
1858 nvme_rdma_dev_put(ctrl->device);
1860 nvme_rdma_free_io_queues(ctrl);
1864 static int nvme_rdma_parse_ipaddr(struct sockaddr_in *in_addr, char *p)
1866 u8 *addr = (u8 *)&in_addr->sin_addr.s_addr;
1867 size_t buflen = strlen(p);
1869 /* XXX: handle IPv6 addresses */
1871 if (buflen > INET_ADDRSTRLEN)
1873 if (in4_pton(p, buflen, addr, '\0', NULL) == 0)
1875 in_addr->sin_family = AF_INET;
1879 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1880 struct nvmf_ctrl_options *opts)
1882 struct nvme_rdma_ctrl *ctrl;
1886 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1888 return ERR_PTR(-ENOMEM);
1889 ctrl->ctrl.opts = opts;
1890 INIT_LIST_HEAD(&ctrl->list);
1892 ret = nvme_rdma_parse_ipaddr(&ctrl->addr_in, opts->traddr);
1894 pr_err("malformed IP address passed: %s\n", opts->traddr);
1898 if (opts->mask & NVMF_OPT_TRSVCID) {
1901 ret = kstrtou16(opts->trsvcid, 0, &port);
1905 ctrl->addr_in.sin_port = cpu_to_be16(port);
1907 ctrl->addr_in.sin_port = cpu_to_be16(NVME_RDMA_IP_PORT);
1910 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1911 0 /* no quirks, we're perfect! */);
1915 ctrl->reconnect_delay = opts->reconnect_delay;
1916 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1917 nvme_rdma_reconnect_ctrl_work);
1918 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1919 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1920 INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work);
1921 spin_lock_init(&ctrl->lock);
1923 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1924 ctrl->ctrl.sqsize = opts->queue_size - 1;
1925 ctrl->ctrl.kato = opts->kato;
1928 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues),
1931 goto out_uninit_ctrl;
1933 ret = nvme_rdma_configure_admin_queue(ctrl);
1935 goto out_kfree_queues;
1937 /* sanity check icdoff */
1938 if (ctrl->ctrl.icdoff) {
1939 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1940 goto out_remove_admin_queue;
1943 /* sanity check keyed sgls */
1944 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1945 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1946 goto out_remove_admin_queue;
1949 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1950 /* warn if maxcmd is lower than queue_size */
1951 dev_warn(ctrl->ctrl.device,
1952 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1953 opts->queue_size, ctrl->ctrl.maxcmd);
1954 opts->queue_size = ctrl->ctrl.maxcmd;
1957 if (opts->nr_io_queues) {
1958 ret = nvme_rdma_create_io_queues(ctrl);
1960 goto out_remove_admin_queue;
1963 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1964 WARN_ON_ONCE(!changed);
1966 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
1967 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1969 kref_get(&ctrl->ctrl.kref);
1971 mutex_lock(&nvme_rdma_ctrl_mutex);
1972 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1973 mutex_unlock(&nvme_rdma_ctrl_mutex);
1975 if (opts->nr_io_queues) {
1976 nvme_queue_scan(&ctrl->ctrl);
1977 nvme_queue_async_events(&ctrl->ctrl);
1982 out_remove_admin_queue:
1983 nvme_stop_keep_alive(&ctrl->ctrl);
1984 nvme_rdma_destroy_admin_queue(ctrl);
1986 kfree(ctrl->queues);
1988 nvme_uninit_ctrl(&ctrl->ctrl);
1989 nvme_put_ctrl(&ctrl->ctrl);
1992 return ERR_PTR(ret);
1995 return ERR_PTR(ret);
1998 static struct nvmf_transport_ops nvme_rdma_transport = {
2000 .required_opts = NVMF_OPT_TRADDR,
2001 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY,
2002 .create_ctrl = nvme_rdma_create_ctrl,
2005 static void nvme_rdma_add_one(struct ib_device *ib_device)
2009 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2011 struct nvme_rdma_ctrl *ctrl;
2013 /* Delete all controllers using this device */
2014 mutex_lock(&nvme_rdma_ctrl_mutex);
2015 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2016 if (ctrl->device->dev != ib_device)
2018 dev_info(ctrl->ctrl.device,
2019 "Removing ctrl: NQN \"%s\", addr %pISp\n",
2020 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2021 __nvme_rdma_del_ctrl(ctrl);
2023 mutex_unlock(&nvme_rdma_ctrl_mutex);
2025 flush_workqueue(nvme_rdma_wq);
2028 static struct ib_client nvme_rdma_ib_client = {
2029 .name = "nvme_rdma",
2030 .add = nvme_rdma_add_one,
2031 .remove = nvme_rdma_remove_one
2034 static int __init nvme_rdma_init_module(void)
2038 nvme_rdma_wq = create_workqueue("nvme_rdma_wq");
2042 ret = ib_register_client(&nvme_rdma_ib_client);
2044 destroy_workqueue(nvme_rdma_wq);
2048 nvmf_register_transport(&nvme_rdma_transport);
2052 static void __exit nvme_rdma_cleanup_module(void)
2054 nvmf_unregister_transport(&nvme_rdma_transport);
2055 ib_unregister_client(&nvme_rdma_ib_client);
2056 destroy_workqueue(nvme_rdma_wq);
2059 module_init(nvme_rdma_init_module);
2060 module_exit(nvme_rdma_cleanup_module);
2062 MODULE_LICENSE("GPL v2");