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 <linux/nvme-rdma.h>
37 #define NVME_RDMA_CONNECT_TIMEOUT_MS 1000 /* 1 second */
39 #define NVME_RDMA_MAX_SEGMENT_SIZE 0xffffff /* 24-bit SGL field */
41 #define NVME_RDMA_MAX_SEGMENTS 256
43 #define NVME_RDMA_MAX_INLINE_SEGMENTS 1
45 static const char *const nvme_rdma_cm_status_strs[] = {
46 [NVME_RDMA_CM_INVALID_LEN] = "invalid length",
47 [NVME_RDMA_CM_INVALID_RECFMT] = "invalid record format",
48 [NVME_RDMA_CM_INVALID_QID] = "invalid queue ID",
49 [NVME_RDMA_CM_INVALID_HSQSIZE] = "invalid host SQ size",
50 [NVME_RDMA_CM_INVALID_HRQSIZE] = "invalid host RQ size",
51 [NVME_RDMA_CM_NO_RSC] = "resource not found",
52 [NVME_RDMA_CM_INVALID_IRD] = "invalid IRD",
53 [NVME_RDMA_CM_INVALID_ORD] = "Invalid ORD",
56 static const char *nvme_rdma_cm_msg(enum nvme_rdma_cm_status status)
58 size_t index = status;
60 if (index < ARRAY_SIZE(nvme_rdma_cm_status_strs) &&
61 nvme_rdma_cm_status_strs[index])
62 return nvme_rdma_cm_status_strs[index];
64 return "unrecognized reason";
68 * We handle AEN commands ourselves and don't even let the
69 * block layer know about them.
71 #define NVME_RDMA_NR_AEN_COMMANDS 1
72 #define NVME_RDMA_AQ_BLKMQ_DEPTH \
73 (NVMF_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
75 struct nvme_rdma_device {
76 struct ib_device *dev;
79 struct list_head entry;
88 struct nvme_rdma_queue;
89 struct nvme_rdma_request {
90 struct nvme_request req;
92 struct nvme_rdma_qe sqe;
93 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
97 struct ib_reg_wr reg_wr;
98 struct ib_cqe reg_cqe;
99 struct nvme_rdma_queue *queue;
100 struct sg_table sg_table;
101 struct scatterlist first_sgl[];
104 enum nvme_rdma_queue_flags {
105 NVME_RDMA_Q_CONNECTED = (1 << 0),
106 NVME_RDMA_IB_QUEUE_ALLOCATED = (1 << 1),
107 NVME_RDMA_Q_DELETING = (1 << 2),
108 NVME_RDMA_Q_LIVE = (1 << 3),
111 struct nvme_rdma_queue {
112 struct nvme_rdma_qe *rsp_ring;
115 size_t cmnd_capsule_len;
116 struct nvme_rdma_ctrl *ctrl;
117 struct nvme_rdma_device *device;
122 struct rdma_cm_id *cm_id;
124 struct completion cm_done;
127 struct nvme_rdma_ctrl {
128 /* read and written in the hot path */
131 /* read only in the hot path */
132 struct nvme_rdma_queue *queues;
135 /* other member variables */
136 struct blk_mq_tag_set tag_set;
137 struct work_struct delete_work;
138 struct work_struct reset_work;
139 struct work_struct err_work;
141 struct nvme_rdma_qe async_event_sqe;
144 struct delayed_work reconnect_work;
146 struct list_head list;
148 struct blk_mq_tag_set admin_tag_set;
149 struct nvme_rdma_device *device;
155 struct sockaddr addr;
156 struct sockaddr_in addr_in;
159 struct nvme_ctrl ctrl;
162 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
164 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
167 static LIST_HEAD(device_list);
168 static DEFINE_MUTEX(device_list_mutex);
170 static LIST_HEAD(nvme_rdma_ctrl_list);
171 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
173 static struct workqueue_struct *nvme_rdma_wq;
176 * Disabling this option makes small I/O goes faster, but is fundamentally
177 * unsafe. With it turned off we will have to register a global rkey that
178 * allows read and write access to all physical memory.
180 static bool register_always = true;
181 module_param(register_always, bool, 0444);
182 MODULE_PARM_DESC(register_always,
183 "Use memory registration even for contiguous memory regions");
185 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
186 struct rdma_cm_event *event);
187 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
189 /* XXX: really should move to a generic header sooner or later.. */
190 static inline void put_unaligned_le24(u32 val, u8 *p)
197 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
199 return queue - queue->ctrl->queues;
202 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
204 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
207 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
208 size_t capsule_size, enum dma_data_direction dir)
210 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
214 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
215 size_t capsule_size, enum dma_data_direction dir)
217 qe->data = kzalloc(capsule_size, GFP_KERNEL);
221 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
222 if (ib_dma_mapping_error(ibdev, qe->dma)) {
230 static void nvme_rdma_free_ring(struct ib_device *ibdev,
231 struct nvme_rdma_qe *ring, size_t ib_queue_size,
232 size_t capsule_size, enum dma_data_direction dir)
236 for (i = 0; i < ib_queue_size; i++)
237 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
241 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
242 size_t ib_queue_size, size_t capsule_size,
243 enum dma_data_direction dir)
245 struct nvme_rdma_qe *ring;
248 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
252 for (i = 0; i < ib_queue_size; i++) {
253 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
260 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
264 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
266 pr_debug("QP event %s (%d)\n",
267 ib_event_msg(event->event), event->event);
271 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
273 wait_for_completion_interruptible_timeout(&queue->cm_done,
274 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
275 return queue->cm_error;
278 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
280 struct nvme_rdma_device *dev = queue->device;
281 struct ib_qp_init_attr init_attr;
284 memset(&init_attr, 0, sizeof(init_attr));
285 init_attr.event_handler = nvme_rdma_qp_event;
287 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
289 init_attr.cap.max_recv_wr = queue->queue_size + 1;
290 init_attr.cap.max_recv_sge = 1;
291 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
292 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
293 init_attr.qp_type = IB_QPT_RC;
294 init_attr.send_cq = queue->ib_cq;
295 init_attr.recv_cq = queue->ib_cq;
297 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
299 queue->qp = queue->cm_id->qp;
303 static int nvme_rdma_reinit_request(void *data, struct request *rq)
305 struct nvme_rdma_ctrl *ctrl = data;
306 struct nvme_rdma_device *dev = ctrl->device;
307 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
310 if (!req->mr->need_inval)
313 ib_dereg_mr(req->mr);
315 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
317 if (IS_ERR(req->mr)) {
318 ret = PTR_ERR(req->mr);
323 req->mr->need_inval = false;
329 static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl *ctrl,
330 struct request *rq, unsigned int queue_idx)
332 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
333 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
334 struct nvme_rdma_device *dev = queue->device;
337 ib_dereg_mr(req->mr);
339 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
343 static void nvme_rdma_exit_request(void *data, struct request *rq,
344 unsigned int hctx_idx, unsigned int rq_idx)
346 return __nvme_rdma_exit_request(data, rq, hctx_idx + 1);
349 static void nvme_rdma_exit_admin_request(void *data, struct request *rq,
350 unsigned int hctx_idx, unsigned int rq_idx)
352 return __nvme_rdma_exit_request(data, rq, 0);
355 static int __nvme_rdma_init_request(struct nvme_rdma_ctrl *ctrl,
356 struct request *rq, unsigned int queue_idx)
358 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
359 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
360 struct nvme_rdma_device *dev = queue->device;
361 struct ib_device *ibdev = dev->dev;
364 BUG_ON(queue_idx >= ctrl->queue_count);
366 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
371 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
373 if (IS_ERR(req->mr)) {
374 ret = PTR_ERR(req->mr);
383 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
388 static int nvme_rdma_init_request(void *data, struct request *rq,
389 unsigned int hctx_idx, unsigned int rq_idx,
390 unsigned int numa_node)
392 return __nvme_rdma_init_request(data, rq, hctx_idx + 1);
395 static int nvme_rdma_init_admin_request(void *data, struct request *rq,
396 unsigned int hctx_idx, unsigned int rq_idx,
397 unsigned int numa_node)
399 return __nvme_rdma_init_request(data, rq, 0);
402 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
403 unsigned int hctx_idx)
405 struct nvme_rdma_ctrl *ctrl = data;
406 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
408 BUG_ON(hctx_idx >= ctrl->queue_count);
410 hctx->driver_data = queue;
414 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
415 unsigned int hctx_idx)
417 struct nvme_rdma_ctrl *ctrl = data;
418 struct nvme_rdma_queue *queue = &ctrl->queues[0];
420 BUG_ON(hctx_idx != 0);
422 hctx->driver_data = queue;
426 static void nvme_rdma_free_dev(struct kref *ref)
428 struct nvme_rdma_device *ndev =
429 container_of(ref, struct nvme_rdma_device, ref);
431 mutex_lock(&device_list_mutex);
432 list_del(&ndev->entry);
433 mutex_unlock(&device_list_mutex);
435 ib_dealloc_pd(ndev->pd);
439 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
441 kref_put(&dev->ref, nvme_rdma_free_dev);
444 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
446 return kref_get_unless_zero(&dev->ref);
449 static struct nvme_rdma_device *
450 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
452 struct nvme_rdma_device *ndev;
454 mutex_lock(&device_list_mutex);
455 list_for_each_entry(ndev, &device_list, entry) {
456 if (ndev->dev->node_guid == cm_id->device->node_guid &&
457 nvme_rdma_dev_get(ndev))
461 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
465 ndev->dev = cm_id->device;
466 kref_init(&ndev->ref);
468 ndev->pd = ib_alloc_pd(ndev->dev,
469 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
470 if (IS_ERR(ndev->pd))
473 if (!(ndev->dev->attrs.device_cap_flags &
474 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
475 dev_err(&ndev->dev->dev,
476 "Memory registrations not supported.\n");
480 list_add(&ndev->entry, &device_list);
482 mutex_unlock(&device_list_mutex);
486 ib_dealloc_pd(ndev->pd);
490 mutex_unlock(&device_list_mutex);
494 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
496 struct nvme_rdma_device *dev;
497 struct ib_device *ibdev;
499 if (!test_and_clear_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags))
504 rdma_destroy_qp(queue->cm_id);
505 ib_free_cq(queue->ib_cq);
507 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
508 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
510 nvme_rdma_dev_put(dev);
513 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue,
514 struct nvme_rdma_device *dev)
516 struct ib_device *ibdev = dev->dev;
517 const int send_wr_factor = 3; /* MR, SEND, INV */
518 const int cq_factor = send_wr_factor + 1; /* + RECV */
519 int comp_vector, idx = nvme_rdma_queue_idx(queue);
526 * The admin queue is barely used once the controller is live, so don't
527 * bother to spread it out.
532 comp_vector = idx % ibdev->num_comp_vectors;
535 /* +1 for ib_stop_cq */
536 queue->ib_cq = ib_alloc_cq(dev->dev, queue,
537 cq_factor * queue->queue_size + 1, comp_vector,
539 if (IS_ERR(queue->ib_cq)) {
540 ret = PTR_ERR(queue->ib_cq);
544 ret = nvme_rdma_create_qp(queue, send_wr_factor);
546 goto out_destroy_ib_cq;
548 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
549 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
550 if (!queue->rsp_ring) {
554 set_bit(NVME_RDMA_IB_QUEUE_ALLOCATED, &queue->flags);
559 ib_destroy_qp(queue->qp);
561 ib_free_cq(queue->ib_cq);
566 static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl,
567 int idx, size_t queue_size)
569 struct nvme_rdma_queue *queue;
572 queue = &ctrl->queues[idx];
574 init_completion(&queue->cm_done);
577 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
579 queue->cmnd_capsule_len = sizeof(struct nvme_command);
581 queue->queue_size = queue_size;
583 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
584 RDMA_PS_TCP, IB_QPT_RC);
585 if (IS_ERR(queue->cm_id)) {
586 dev_info(ctrl->ctrl.device,
587 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
588 return PTR_ERR(queue->cm_id);
591 queue->cm_error = -ETIMEDOUT;
592 ret = rdma_resolve_addr(queue->cm_id, NULL, &ctrl->addr,
593 NVME_RDMA_CONNECT_TIMEOUT_MS);
595 dev_info(ctrl->ctrl.device,
596 "rdma_resolve_addr failed (%d).\n", ret);
597 goto out_destroy_cm_id;
600 ret = nvme_rdma_wait_for_cm(queue);
602 dev_info(ctrl->ctrl.device,
603 "rdma_resolve_addr wait failed (%d).\n", ret);
604 goto out_destroy_cm_id;
607 clear_bit(NVME_RDMA_Q_DELETING, &queue->flags);
608 set_bit(NVME_RDMA_Q_CONNECTED, &queue->flags);
613 nvme_rdma_destroy_queue_ib(queue);
614 rdma_destroy_id(queue->cm_id);
618 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
620 rdma_disconnect(queue->cm_id);
621 ib_drain_qp(queue->qp);
624 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
626 nvme_rdma_destroy_queue_ib(queue);
627 rdma_destroy_id(queue->cm_id);
630 static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue)
632 if (test_and_set_bit(NVME_RDMA_Q_DELETING, &queue->flags))
634 nvme_rdma_stop_queue(queue);
635 nvme_rdma_free_queue(queue);
638 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
642 for (i = 1; i < ctrl->queue_count; i++)
643 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
646 static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl)
650 for (i = 1; i < ctrl->queue_count; i++) {
651 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
653 dev_info(ctrl->ctrl.device,
654 "failed to connect i/o queue: %d\n", ret);
655 goto out_free_queues;
657 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
663 nvme_rdma_free_io_queues(ctrl);
667 static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl)
671 for (i = 1; i < ctrl->queue_count; i++) {
672 ret = nvme_rdma_init_queue(ctrl, i,
673 ctrl->ctrl.opts->queue_size);
675 dev_info(ctrl->ctrl.device,
676 "failed to initialize i/o queue: %d\n", ret);
677 goto out_free_queues;
684 for (i--; i >= 1; i--)
685 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
690 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl)
692 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
693 sizeof(struct nvme_command), DMA_TO_DEVICE);
694 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
695 blk_cleanup_queue(ctrl->ctrl.admin_q);
696 blk_mq_free_tag_set(&ctrl->admin_tag_set);
697 nvme_rdma_dev_put(ctrl->device);
700 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
702 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
704 if (list_empty(&ctrl->list))
707 mutex_lock(&nvme_rdma_ctrl_mutex);
708 list_del(&ctrl->list);
709 mutex_unlock(&nvme_rdma_ctrl_mutex);
712 nvmf_free_options(nctrl->opts);
717 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
719 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
720 struct nvme_rdma_ctrl, reconnect_work);
724 if (ctrl->queue_count > 1) {
725 nvme_rdma_free_io_queues(ctrl);
727 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
732 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
734 ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set);
738 ret = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
742 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
744 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
748 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
750 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
754 nvme_start_keep_alive(&ctrl->ctrl);
756 if (ctrl->queue_count > 1) {
757 ret = nvme_rdma_init_io_queues(ctrl);
761 ret = nvme_rdma_connect_io_queues(ctrl);
766 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
767 WARN_ON_ONCE(!changed);
769 if (ctrl->queue_count > 1) {
770 nvme_start_queues(&ctrl->ctrl);
771 nvme_queue_scan(&ctrl->ctrl);
772 nvme_queue_async_events(&ctrl->ctrl);
775 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
780 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
782 /* Make sure we are not resetting/deleting */
783 if (ctrl->ctrl.state == NVME_CTRL_RECONNECTING) {
784 dev_info(ctrl->ctrl.device,
785 "Failed reconnect attempt, requeueing...\n");
786 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
787 ctrl->reconnect_delay * HZ);
791 static void nvme_rdma_error_recovery_work(struct work_struct *work)
793 struct nvme_rdma_ctrl *ctrl = container_of(work,
794 struct nvme_rdma_ctrl, err_work);
797 nvme_stop_keep_alive(&ctrl->ctrl);
799 for (i = 0; i < ctrl->queue_count; i++) {
800 clear_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[i].flags);
801 clear_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
804 if (ctrl->queue_count > 1)
805 nvme_stop_queues(&ctrl->ctrl);
806 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
808 /* We must take care of fastfail/requeue all our inflight requests */
809 if (ctrl->queue_count > 1)
810 blk_mq_tagset_busy_iter(&ctrl->tag_set,
811 nvme_cancel_request, &ctrl->ctrl);
812 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
813 nvme_cancel_request, &ctrl->ctrl);
815 dev_info(ctrl->ctrl.device, "reconnecting in %d seconds\n",
816 ctrl->reconnect_delay);
818 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
819 ctrl->reconnect_delay * HZ);
822 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
824 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
827 queue_work(nvme_rdma_wq, &ctrl->err_work);
830 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
833 struct nvme_rdma_queue *queue = cq->cq_context;
834 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
836 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
837 dev_info(ctrl->ctrl.device,
838 "%s for CQE 0x%p failed with status %s (%d)\n",
840 ib_wc_status_msg(wc->status), wc->status);
841 nvme_rdma_error_recovery(ctrl);
844 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
846 if (unlikely(wc->status != IB_WC_SUCCESS))
847 nvme_rdma_wr_error(cq, wc, "MEMREG");
850 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
852 if (unlikely(wc->status != IB_WC_SUCCESS))
853 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
856 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
857 struct nvme_rdma_request *req)
859 struct ib_send_wr *bad_wr;
860 struct ib_send_wr wr = {
861 .opcode = IB_WR_LOCAL_INV,
865 .ex.invalidate_rkey = req->mr->rkey,
868 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
869 wr.wr_cqe = &req->reg_cqe;
871 return ib_post_send(queue->qp, &wr, &bad_wr);
874 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
877 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
878 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
879 struct nvme_rdma_device *dev = queue->device;
880 struct ib_device *ibdev = dev->dev;
883 if (!blk_rq_bytes(rq))
886 if (req->mr->need_inval) {
887 res = nvme_rdma_inv_rkey(queue, req);
889 dev_err(ctrl->ctrl.device,
890 "Queueing INV WR for rkey %#x failed (%d)\n",
892 nvme_rdma_error_recovery(queue->ctrl);
896 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
897 req->nents, rq_data_dir(rq) ==
898 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
900 nvme_cleanup_cmd(rq);
901 sg_free_table_chained(&req->sg_table, true);
904 static int nvme_rdma_set_sg_null(struct nvme_command *c)
906 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
909 put_unaligned_le24(0, sg->length);
910 put_unaligned_le32(0, sg->key);
911 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
915 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
916 struct nvme_rdma_request *req, struct nvme_command *c)
918 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
920 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
921 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
922 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
924 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
925 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
926 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
928 req->inline_data = true;
933 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
934 struct nvme_rdma_request *req, struct nvme_command *c)
936 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
938 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
939 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
940 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
941 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
945 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
946 struct nvme_rdma_request *req, struct nvme_command *c,
949 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
952 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE);
959 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
961 req->reg_cqe.done = nvme_rdma_memreg_done;
962 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
963 req->reg_wr.wr.opcode = IB_WR_REG_MR;
964 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
965 req->reg_wr.wr.num_sge = 0;
966 req->reg_wr.mr = req->mr;
967 req->reg_wr.key = req->mr->rkey;
968 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
969 IB_ACCESS_REMOTE_READ |
970 IB_ACCESS_REMOTE_WRITE;
972 req->mr->need_inval = true;
974 sg->addr = cpu_to_le64(req->mr->iova);
975 put_unaligned_le24(req->mr->length, sg->length);
976 put_unaligned_le32(req->mr->rkey, sg->key);
977 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
978 NVME_SGL_FMT_INVALIDATE;
983 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
984 struct request *rq, struct nvme_command *c)
986 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
987 struct nvme_rdma_device *dev = queue->device;
988 struct ib_device *ibdev = dev->dev;
992 req->inline_data = false;
993 req->mr->need_inval = false;
995 c->common.flags |= NVME_CMD_SGL_METABUF;
997 if (!blk_rq_bytes(rq))
998 return nvme_rdma_set_sg_null(c);
1000 req->sg_table.sgl = req->first_sgl;
1001 ret = sg_alloc_table_chained(&req->sg_table,
1002 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
1006 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
1008 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
1009 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1010 if (unlikely(count <= 0)) {
1011 sg_free_table_chained(&req->sg_table, true);
1016 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1017 blk_rq_payload_bytes(rq) <=
1018 nvme_rdma_inline_data_size(queue))
1019 return nvme_rdma_map_sg_inline(queue, req, c);
1021 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
1022 return nvme_rdma_map_sg_single(queue, req, c);
1025 return nvme_rdma_map_sg_fr(queue, req, c, count);
1028 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1030 if (unlikely(wc->status != IB_WC_SUCCESS))
1031 nvme_rdma_wr_error(cq, wc, "SEND");
1034 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1035 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1036 struct ib_send_wr *first, bool flush)
1038 struct ib_send_wr wr, *bad_wr;
1041 sge->addr = qe->dma;
1042 sge->length = sizeof(struct nvme_command),
1043 sge->lkey = queue->device->pd->local_dma_lkey;
1045 qe->cqe.done = nvme_rdma_send_done;
1048 wr.wr_cqe = &qe->cqe;
1050 wr.num_sge = num_sge;
1051 wr.opcode = IB_WR_SEND;
1055 * Unsignalled send completions are another giant desaster in the
1056 * IB Verbs spec: If we don't regularly post signalled sends
1057 * the send queue will fill up and only a QP reset will rescue us.
1058 * Would have been way to obvious to handle this in hardware or
1059 * at least the RDMA stack..
1061 * This messy and racy code sniplet is copy and pasted from the iSER
1062 * initiator, and the magic '32' comes from there as well.
1064 * Always signal the flushes. The magic request used for the flush
1065 * sequencer is not allocated in our driver's tagset and it's
1066 * triggered to be freed by blk_cleanup_queue(). So we need to
1067 * always mark it as signaled to ensure that the "wr_cqe", which is
1068 * embeded in request's payload, is not freed when __ib_process_cq()
1069 * calls wr_cqe->done().
1071 if ((++queue->sig_count % 32) == 0 || flush)
1072 wr.send_flags |= IB_SEND_SIGNALED;
1079 ret = ib_post_send(queue->qp, first, &bad_wr);
1081 dev_err(queue->ctrl->ctrl.device,
1082 "%s failed with error code %d\n", __func__, ret);
1087 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1088 struct nvme_rdma_qe *qe)
1090 struct ib_recv_wr wr, *bad_wr;
1094 list.addr = qe->dma;
1095 list.length = sizeof(struct nvme_completion);
1096 list.lkey = queue->device->pd->local_dma_lkey;
1098 qe->cqe.done = nvme_rdma_recv_done;
1101 wr.wr_cqe = &qe->cqe;
1105 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1107 dev_err(queue->ctrl->ctrl.device,
1108 "%s failed with error code %d\n", __func__, ret);
1113 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1115 u32 queue_idx = nvme_rdma_queue_idx(queue);
1118 return queue->ctrl->admin_tag_set.tags[queue_idx];
1119 return queue->ctrl->tag_set.tags[queue_idx - 1];
1122 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1124 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1125 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1126 struct ib_device *dev = queue->device->dev;
1127 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1128 struct nvme_command *cmd = sqe->data;
1132 if (WARN_ON_ONCE(aer_idx != 0))
1135 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1137 memset(cmd, 0, sizeof(*cmd));
1138 cmd->common.opcode = nvme_admin_async_event;
1139 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1140 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1141 nvme_rdma_set_sg_null(cmd);
1143 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1146 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1150 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1151 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1154 struct nvme_rdma_request *req;
1157 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1159 dev_err(queue->ctrl->ctrl.device,
1160 "tag 0x%x on QP %#x not found\n",
1161 cqe->command_id, queue->qp->qp_num);
1162 nvme_rdma_error_recovery(queue->ctrl);
1165 req = blk_mq_rq_to_pdu(rq);
1170 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1171 wc->ex.invalidate_rkey == req->mr->rkey)
1172 req->mr->need_inval = false;
1174 req->req.result = cqe->result;
1175 blk_mq_complete_request(rq, le16_to_cpu(cqe->status) >> 1);
1179 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1181 struct nvme_rdma_qe *qe =
1182 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1183 struct nvme_rdma_queue *queue = cq->cq_context;
1184 struct ib_device *ibdev = queue->device->dev;
1185 struct nvme_completion *cqe = qe->data;
1186 const size_t len = sizeof(struct nvme_completion);
1189 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1190 nvme_rdma_wr_error(cq, wc, "RECV");
1194 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1196 * AEN requests are special as they don't time out and can
1197 * survive any kind of queue freeze and often don't respond to
1198 * aborts. We don't even bother to allocate a struct request
1199 * for them but rather special case them here.
1201 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1202 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
1203 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1206 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1207 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1209 nvme_rdma_post_recv(queue, qe);
1213 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1215 __nvme_rdma_recv_done(cq, wc, -1);
1218 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1222 for (i = 0; i < queue->queue_size; i++) {
1223 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1225 goto out_destroy_queue_ib;
1230 out_destroy_queue_ib:
1231 nvme_rdma_destroy_queue_ib(queue);
1235 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1236 struct rdma_cm_event *ev)
1238 struct rdma_cm_id *cm_id = queue->cm_id;
1239 int status = ev->status;
1240 const char *rej_msg;
1241 const struct nvme_rdma_cm_rej *rej_data;
1244 rej_msg = rdma_reject_msg(cm_id, status);
1245 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1247 if (rej_data && rej_data_len >= sizeof(u16)) {
1248 u16 sts = le16_to_cpu(rej_data->sts);
1250 dev_err(queue->ctrl->ctrl.device,
1251 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1252 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1254 dev_err(queue->ctrl->ctrl.device,
1255 "Connect rejected: status %d (%s).\n", status, rej_msg);
1261 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1263 struct nvme_rdma_device *dev;
1266 dev = nvme_rdma_find_get_device(queue->cm_id);
1268 dev_err(queue->cm_id->device->dma_device,
1269 "no client data found!\n");
1270 return -ECONNREFUSED;
1273 ret = nvme_rdma_create_queue_ib(queue, dev);
1275 nvme_rdma_dev_put(dev);
1279 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1281 dev_err(queue->ctrl->ctrl.device,
1282 "rdma_resolve_route failed (%d).\n",
1284 goto out_destroy_queue;
1290 nvme_rdma_destroy_queue_ib(queue);
1295 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1297 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1298 struct rdma_conn_param param = { };
1299 struct nvme_rdma_cm_req priv = { };
1302 param.qp_num = queue->qp->qp_num;
1303 param.flow_control = 1;
1305 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1306 /* maximum retry count */
1307 param.retry_count = 7;
1308 param.rnr_retry_count = 7;
1309 param.private_data = &priv;
1310 param.private_data_len = sizeof(priv);
1312 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1313 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1315 * set the admin queue depth to the minimum size
1316 * specified by the Fabrics standard.
1318 if (priv.qid == 0) {
1319 priv.hrqsize = cpu_to_le16(NVMF_AQ_DEPTH);
1320 priv.hsqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1);
1323 * current interpretation of the fabrics spec
1324 * is at minimum you make hrqsize sqsize+1, or a
1325 * 1's based representation of sqsize.
1327 priv.hrqsize = cpu_to_le16(queue->queue_size);
1328 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1331 ret = rdma_connect(queue->cm_id, ¶m);
1333 dev_err(ctrl->ctrl.device,
1334 "rdma_connect failed (%d).\n", ret);
1335 goto out_destroy_queue_ib;
1340 out_destroy_queue_ib:
1341 nvme_rdma_destroy_queue_ib(queue);
1345 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1346 struct rdma_cm_event *ev)
1348 struct nvme_rdma_queue *queue = cm_id->context;
1351 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1352 rdma_event_msg(ev->event), ev->event,
1355 switch (ev->event) {
1356 case RDMA_CM_EVENT_ADDR_RESOLVED:
1357 cm_error = nvme_rdma_addr_resolved(queue);
1359 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1360 cm_error = nvme_rdma_route_resolved(queue);
1362 case RDMA_CM_EVENT_ESTABLISHED:
1363 queue->cm_error = nvme_rdma_conn_established(queue);
1364 /* complete cm_done regardless of success/failure */
1365 complete(&queue->cm_done);
1367 case RDMA_CM_EVENT_REJECTED:
1368 cm_error = nvme_rdma_conn_rejected(queue, ev);
1370 case RDMA_CM_EVENT_ADDR_ERROR:
1371 case RDMA_CM_EVENT_ROUTE_ERROR:
1372 case RDMA_CM_EVENT_CONNECT_ERROR:
1373 case RDMA_CM_EVENT_UNREACHABLE:
1374 dev_dbg(queue->ctrl->ctrl.device,
1375 "CM error event %d\n", ev->event);
1376 cm_error = -ECONNRESET;
1378 case RDMA_CM_EVENT_DISCONNECTED:
1379 case RDMA_CM_EVENT_ADDR_CHANGE:
1380 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1381 dev_dbg(queue->ctrl->ctrl.device,
1382 "disconnect received - connection closed\n");
1383 nvme_rdma_error_recovery(queue->ctrl);
1385 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1386 /* device removal is handled via the ib_client API */
1389 dev_err(queue->ctrl->ctrl.device,
1390 "Unexpected RDMA CM event (%d)\n", ev->event);
1391 nvme_rdma_error_recovery(queue->ctrl);
1396 queue->cm_error = cm_error;
1397 complete(&queue->cm_done);
1403 static enum blk_eh_timer_return
1404 nvme_rdma_timeout(struct request *rq, bool reserved)
1406 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1408 /* queue error recovery */
1409 nvme_rdma_error_recovery(req->queue->ctrl);
1411 /* fail with DNR on cmd timeout */
1412 rq->errors = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1414 return BLK_EH_HANDLED;
1418 * We cannot accept any other command until the Connect command has completed.
1420 static inline bool nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue,
1423 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
1424 struct nvme_command *cmd = nvme_req(rq)->cmd;
1426 if (rq->cmd_type != REQ_TYPE_DRV_PRIV ||
1427 cmd->common.opcode != nvme_fabrics_command ||
1428 cmd->fabrics.fctype != nvme_fabrics_type_connect)
1435 static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1436 const struct blk_mq_queue_data *bd)
1438 struct nvme_ns *ns = hctx->queue->queuedata;
1439 struct nvme_rdma_queue *queue = hctx->driver_data;
1440 struct request *rq = bd->rq;
1441 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1442 struct nvme_rdma_qe *sqe = &req->sqe;
1443 struct nvme_command *c = sqe->data;
1445 struct ib_device *dev;
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);
1458 if (ret != BLK_MQ_RQ_QUEUE_OK)
1461 blk_mq_start_request(rq);
1463 ret = nvme_rdma_map_data(queue, rq, c);
1465 dev_err(queue->ctrl->ctrl.device,
1466 "Failed to map data (%d)\n", ret);
1467 nvme_cleanup_cmd(rq);
1471 ib_dma_sync_single_for_device(dev, sqe->dma,
1472 sizeof(struct nvme_command), DMA_TO_DEVICE);
1474 if (rq->cmd_type == REQ_TYPE_FS && req_op(rq) == REQ_OP_FLUSH)
1476 ret = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1477 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
1479 nvme_rdma_unmap_data(queue, rq);
1483 return BLK_MQ_RQ_QUEUE_OK;
1485 return (ret == -ENOMEM || ret == -EAGAIN) ?
1486 BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR;
1489 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1491 struct nvme_rdma_queue *queue = hctx->driver_data;
1492 struct ib_cq *cq = queue->ib_cq;
1496 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1497 while (ib_poll_cq(cq, 1, &wc) > 0) {
1498 struct ib_cqe *cqe = wc.wr_cqe;
1501 if (cqe->done == nvme_rdma_recv_done)
1502 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1511 static void nvme_rdma_complete_rq(struct request *rq)
1513 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1514 struct nvme_rdma_queue *queue = req->queue;
1517 nvme_rdma_unmap_data(queue, rq);
1519 if (unlikely(rq->errors)) {
1520 if (nvme_req_needs_retry(rq, rq->errors)) {
1521 nvme_requeue_req(rq);
1525 if (rq->cmd_type == REQ_TYPE_DRV_PRIV)
1528 error = nvme_error_status(rq->errors);
1531 blk_mq_end_request(rq, error);
1534 static struct blk_mq_ops nvme_rdma_mq_ops = {
1535 .queue_rq = nvme_rdma_queue_rq,
1536 .complete = nvme_rdma_complete_rq,
1537 .init_request = nvme_rdma_init_request,
1538 .exit_request = nvme_rdma_exit_request,
1539 .reinit_request = nvme_rdma_reinit_request,
1540 .init_hctx = nvme_rdma_init_hctx,
1541 .poll = nvme_rdma_poll,
1542 .timeout = nvme_rdma_timeout,
1545 static struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1546 .queue_rq = nvme_rdma_queue_rq,
1547 .complete = nvme_rdma_complete_rq,
1548 .init_request = nvme_rdma_init_admin_request,
1549 .exit_request = nvme_rdma_exit_admin_request,
1550 .reinit_request = nvme_rdma_reinit_request,
1551 .init_hctx = nvme_rdma_init_admin_hctx,
1552 .timeout = nvme_rdma_timeout,
1555 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
1559 error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
1563 ctrl->device = ctrl->queues[0].device;
1566 * We need a reference on the device as long as the tag_set is alive,
1567 * as the MRs in the request structures need a valid ib_device.
1570 if (!nvme_rdma_dev_get(ctrl->device))
1571 goto out_free_queue;
1573 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
1574 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
1576 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1577 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
1578 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
1579 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
1580 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1581 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1582 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1583 ctrl->admin_tag_set.driver_data = ctrl;
1584 ctrl->admin_tag_set.nr_hw_queues = 1;
1585 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1587 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1591 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1592 if (IS_ERR(ctrl->ctrl.admin_q)) {
1593 error = PTR_ERR(ctrl->ctrl.admin_q);
1594 goto out_free_tagset;
1597 error = nvmf_connect_admin_queue(&ctrl->ctrl);
1599 goto out_cleanup_queue;
1601 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
1603 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
1605 dev_err(ctrl->ctrl.device,
1606 "prop_get NVME_REG_CAP failed\n");
1607 goto out_cleanup_queue;
1611 min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize);
1613 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
1615 goto out_cleanup_queue;
1617 ctrl->ctrl.max_hw_sectors =
1618 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
1620 error = nvme_init_identify(&ctrl->ctrl);
1622 goto out_cleanup_queue;
1624 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
1625 &ctrl->async_event_sqe, sizeof(struct nvme_command),
1628 goto out_cleanup_queue;
1630 nvme_start_keep_alive(&ctrl->ctrl);
1635 blk_cleanup_queue(ctrl->ctrl.admin_q);
1637 /* disconnect and drain the queue before freeing the tagset */
1638 nvme_rdma_stop_queue(&ctrl->queues[0]);
1639 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1641 nvme_rdma_dev_put(ctrl->device);
1643 nvme_rdma_free_queue(&ctrl->queues[0]);
1647 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1649 nvme_stop_keep_alive(&ctrl->ctrl);
1650 cancel_work_sync(&ctrl->err_work);
1651 cancel_delayed_work_sync(&ctrl->reconnect_work);
1653 if (ctrl->queue_count > 1) {
1654 nvme_stop_queues(&ctrl->ctrl);
1655 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1656 nvme_cancel_request, &ctrl->ctrl);
1657 nvme_rdma_free_io_queues(ctrl);
1660 if (test_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[0].flags))
1661 nvme_shutdown_ctrl(&ctrl->ctrl);
1663 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
1664 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1665 nvme_cancel_request, &ctrl->ctrl);
1666 nvme_rdma_destroy_admin_queue(ctrl);
1669 static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1671 nvme_uninit_ctrl(&ctrl->ctrl);
1673 nvme_rdma_shutdown_ctrl(ctrl);
1675 if (ctrl->ctrl.tagset) {
1676 blk_cleanup_queue(ctrl->ctrl.connect_q);
1677 blk_mq_free_tag_set(&ctrl->tag_set);
1678 nvme_rdma_dev_put(ctrl->device);
1681 nvme_put_ctrl(&ctrl->ctrl);
1684 static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1686 struct nvme_rdma_ctrl *ctrl = container_of(work,
1687 struct nvme_rdma_ctrl, delete_work);
1689 __nvme_rdma_remove_ctrl(ctrl, true);
1692 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1694 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1697 if (!queue_work(nvme_rdma_wq, &ctrl->delete_work))
1703 static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1705 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1709 * Keep a reference until all work is flushed since
1710 * __nvme_rdma_del_ctrl can free the ctrl mem
1712 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1714 ret = __nvme_rdma_del_ctrl(ctrl);
1716 flush_work(&ctrl->delete_work);
1717 nvme_put_ctrl(&ctrl->ctrl);
1721 static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1723 struct nvme_rdma_ctrl *ctrl = container_of(work,
1724 struct nvme_rdma_ctrl, delete_work);
1726 __nvme_rdma_remove_ctrl(ctrl, false);
1729 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1731 struct nvme_rdma_ctrl *ctrl = container_of(work,
1732 struct nvme_rdma_ctrl, reset_work);
1736 nvme_rdma_shutdown_ctrl(ctrl);
1738 ret = nvme_rdma_configure_admin_queue(ctrl);
1740 /* ctrl is already shutdown, just remove the ctrl */
1741 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1745 if (ctrl->queue_count > 1) {
1746 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
1750 ret = nvme_rdma_init_io_queues(ctrl);
1754 ret = nvme_rdma_connect_io_queues(ctrl);
1759 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1760 WARN_ON_ONCE(!changed);
1762 if (ctrl->queue_count > 1) {
1763 nvme_start_queues(&ctrl->ctrl);
1764 nvme_queue_scan(&ctrl->ctrl);
1765 nvme_queue_async_events(&ctrl->ctrl);
1771 /* Deleting this dead controller... */
1772 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1773 WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work));
1776 static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl)
1778 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1780 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1783 if (!queue_work(nvme_rdma_wq, &ctrl->reset_work))
1786 flush_work(&ctrl->reset_work);
1791 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1793 .module = THIS_MODULE,
1795 .reg_read32 = nvmf_reg_read32,
1796 .reg_read64 = nvmf_reg_read64,
1797 .reg_write32 = nvmf_reg_write32,
1798 .reset_ctrl = nvme_rdma_reset_ctrl,
1799 .free_ctrl = nvme_rdma_free_ctrl,
1800 .submit_async_event = nvme_rdma_submit_async_event,
1801 .delete_ctrl = nvme_rdma_del_ctrl,
1802 .get_subsysnqn = nvmf_get_subsysnqn,
1803 .get_address = nvmf_get_address,
1806 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1808 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
1811 ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues);
1815 ctrl->queue_count = opts->nr_io_queues + 1;
1816 if (ctrl->queue_count < 2)
1819 dev_info(ctrl->ctrl.device,
1820 "creating %d I/O queues.\n", opts->nr_io_queues);
1822 ret = nvme_rdma_init_io_queues(ctrl);
1827 * We need a reference on the device as long as the tag_set is alive,
1828 * as the MRs in the request structures need a valid ib_device.
1831 if (!nvme_rdma_dev_get(ctrl->device))
1832 goto out_free_io_queues;
1834 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1835 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
1836 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
1837 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1838 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1839 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1840 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1841 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1842 ctrl->tag_set.driver_data = ctrl;
1843 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
1844 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1846 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1849 ctrl->ctrl.tagset = &ctrl->tag_set;
1851 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1852 if (IS_ERR(ctrl->ctrl.connect_q)) {
1853 ret = PTR_ERR(ctrl->ctrl.connect_q);
1854 goto out_free_tag_set;
1857 ret = nvme_rdma_connect_io_queues(ctrl);
1859 goto out_cleanup_connect_q;
1863 out_cleanup_connect_q:
1864 blk_cleanup_queue(ctrl->ctrl.connect_q);
1866 blk_mq_free_tag_set(&ctrl->tag_set);
1868 nvme_rdma_dev_put(ctrl->device);
1870 nvme_rdma_free_io_queues(ctrl);
1874 static int nvme_rdma_parse_ipaddr(struct sockaddr_in *in_addr, char *p)
1876 u8 *addr = (u8 *)&in_addr->sin_addr.s_addr;
1877 size_t buflen = strlen(p);
1879 /* XXX: handle IPv6 addresses */
1881 if (buflen > INET_ADDRSTRLEN)
1883 if (in4_pton(p, buflen, addr, '\0', NULL) == 0)
1885 in_addr->sin_family = AF_INET;
1889 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1890 struct nvmf_ctrl_options *opts)
1892 struct nvme_rdma_ctrl *ctrl;
1896 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1898 return ERR_PTR(-ENOMEM);
1899 ctrl->ctrl.opts = opts;
1900 INIT_LIST_HEAD(&ctrl->list);
1902 ret = nvme_rdma_parse_ipaddr(&ctrl->addr_in, opts->traddr);
1904 pr_err("malformed IP address passed: %s\n", opts->traddr);
1908 if (opts->mask & NVMF_OPT_TRSVCID) {
1911 ret = kstrtou16(opts->trsvcid, 0, &port);
1915 ctrl->addr_in.sin_port = cpu_to_be16(port);
1917 ctrl->addr_in.sin_port = cpu_to_be16(NVME_RDMA_IP_PORT);
1920 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1921 0 /* no quirks, we're perfect! */);
1925 ctrl->reconnect_delay = opts->reconnect_delay;
1926 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1927 nvme_rdma_reconnect_ctrl_work);
1928 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1929 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1930 INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work);
1931 spin_lock_init(&ctrl->lock);
1933 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1934 ctrl->ctrl.sqsize = opts->queue_size - 1;
1935 ctrl->ctrl.kato = opts->kato;
1938 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues),
1941 goto out_uninit_ctrl;
1943 ret = nvme_rdma_configure_admin_queue(ctrl);
1945 goto out_kfree_queues;
1947 /* sanity check icdoff */
1948 if (ctrl->ctrl.icdoff) {
1949 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1950 goto out_remove_admin_queue;
1953 /* sanity check keyed sgls */
1954 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1955 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1956 goto out_remove_admin_queue;
1959 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1960 /* warn if maxcmd is lower than queue_size */
1961 dev_warn(ctrl->ctrl.device,
1962 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1963 opts->queue_size, ctrl->ctrl.maxcmd);
1964 opts->queue_size = ctrl->ctrl.maxcmd;
1967 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1968 /* warn if sqsize is lower than queue_size */
1969 dev_warn(ctrl->ctrl.device,
1970 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1971 opts->queue_size, ctrl->ctrl.sqsize + 1);
1972 opts->queue_size = ctrl->ctrl.sqsize + 1;
1975 if (opts->nr_io_queues) {
1976 ret = nvme_rdma_create_io_queues(ctrl);
1978 goto out_remove_admin_queue;
1981 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1982 WARN_ON_ONCE(!changed);
1984 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
1985 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1987 kref_get(&ctrl->ctrl.kref);
1989 mutex_lock(&nvme_rdma_ctrl_mutex);
1990 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1991 mutex_unlock(&nvme_rdma_ctrl_mutex);
1993 if (opts->nr_io_queues) {
1994 nvme_queue_scan(&ctrl->ctrl);
1995 nvme_queue_async_events(&ctrl->ctrl);
2000 out_remove_admin_queue:
2001 nvme_stop_keep_alive(&ctrl->ctrl);
2002 nvme_rdma_destroy_admin_queue(ctrl);
2004 kfree(ctrl->queues);
2006 nvme_uninit_ctrl(&ctrl->ctrl);
2007 nvme_put_ctrl(&ctrl->ctrl);
2010 return ERR_PTR(ret);
2013 return ERR_PTR(ret);
2016 static struct nvmf_transport_ops nvme_rdma_transport = {
2018 .required_opts = NVMF_OPT_TRADDR,
2019 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY,
2020 .create_ctrl = nvme_rdma_create_ctrl,
2023 static void nvme_rdma_add_one(struct ib_device *ib_device)
2027 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2029 struct nvme_rdma_ctrl *ctrl;
2031 /* Delete all controllers using this device */
2032 mutex_lock(&nvme_rdma_ctrl_mutex);
2033 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2034 if (ctrl->device->dev != ib_device)
2036 dev_info(ctrl->ctrl.device,
2037 "Removing ctrl: NQN \"%s\", addr %pISp\n",
2038 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2039 __nvme_rdma_del_ctrl(ctrl);
2041 mutex_unlock(&nvme_rdma_ctrl_mutex);
2043 flush_workqueue(nvme_rdma_wq);
2046 static struct ib_client nvme_rdma_ib_client = {
2047 .name = "nvme_rdma",
2048 .add = nvme_rdma_add_one,
2049 .remove = nvme_rdma_remove_one
2052 static int __init nvme_rdma_init_module(void)
2056 nvme_rdma_wq = create_workqueue("nvme_rdma_wq");
2060 ret = ib_register_client(&nvme_rdma_ib_client);
2062 destroy_workqueue(nvme_rdma_wq);
2066 nvmf_register_transport(&nvme_rdma_transport);
2070 static void __exit nvme_rdma_cleanup_module(void)
2072 nvmf_unregister_transport(&nvme_rdma_transport);
2073 ib_unregister_client(&nvme_rdma_ib_client);
2074 destroy_workqueue(nvme_rdma_wq);
2077 module_init(nvme_rdma_init_module);
2078 module_exit(nvme_rdma_cleanup_module);
2080 MODULE_LICENSE("GPL v2");