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, unsigned int map_len,
985 struct nvme_command *c)
987 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
988 struct nvme_rdma_device *dev = queue->device;
989 struct ib_device *ibdev = dev->dev;
993 req->inline_data = false;
994 req->mr->need_inval = false;
996 c->common.flags |= NVME_CMD_SGL_METABUF;
998 if (!blk_rq_bytes(rq))
999 return nvme_rdma_set_sg_null(c);
1001 req->sg_table.sgl = req->first_sgl;
1002 ret = sg_alloc_table_chained(&req->sg_table,
1003 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
1007 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
1009 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
1010 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1011 if (unlikely(count <= 0)) {
1012 sg_free_table_chained(&req->sg_table, true);
1017 if (rq_data_dir(rq) == WRITE &&
1018 map_len <= nvme_rdma_inline_data_size(queue) &&
1019 nvme_rdma_queue_idx(queue))
1020 return nvme_rdma_map_sg_inline(queue, req, c);
1022 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
1023 return nvme_rdma_map_sg_single(queue, req, c);
1026 return nvme_rdma_map_sg_fr(queue, req, c, count);
1029 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1031 if (unlikely(wc->status != IB_WC_SUCCESS))
1032 nvme_rdma_wr_error(cq, wc, "SEND");
1035 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1036 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1037 struct ib_send_wr *first, bool flush)
1039 struct ib_send_wr wr, *bad_wr;
1042 sge->addr = qe->dma;
1043 sge->length = sizeof(struct nvme_command),
1044 sge->lkey = queue->device->pd->local_dma_lkey;
1046 qe->cqe.done = nvme_rdma_send_done;
1049 wr.wr_cqe = &qe->cqe;
1051 wr.num_sge = num_sge;
1052 wr.opcode = IB_WR_SEND;
1056 * Unsignalled send completions are another giant desaster in the
1057 * IB Verbs spec: If we don't regularly post signalled sends
1058 * the send queue will fill up and only a QP reset will rescue us.
1059 * Would have been way to obvious to handle this in hardware or
1060 * at least the RDMA stack..
1062 * This messy and racy code sniplet is copy and pasted from the iSER
1063 * initiator, and the magic '32' comes from there as well.
1065 * Always signal the flushes. The magic request used for the flush
1066 * sequencer is not allocated in our driver's tagset and it's
1067 * triggered to be freed by blk_cleanup_queue(). So we need to
1068 * always mark it as signaled to ensure that the "wr_cqe", which is
1069 * embeded in request's payload, is not freed when __ib_process_cq()
1070 * calls wr_cqe->done().
1072 if ((++queue->sig_count % 32) == 0 || flush)
1073 wr.send_flags |= IB_SEND_SIGNALED;
1080 ret = ib_post_send(queue->qp, first, &bad_wr);
1082 dev_err(queue->ctrl->ctrl.device,
1083 "%s failed with error code %d\n", __func__, ret);
1088 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1089 struct nvme_rdma_qe *qe)
1091 struct ib_recv_wr wr, *bad_wr;
1095 list.addr = qe->dma;
1096 list.length = sizeof(struct nvme_completion);
1097 list.lkey = queue->device->pd->local_dma_lkey;
1099 qe->cqe.done = nvme_rdma_recv_done;
1102 wr.wr_cqe = &qe->cqe;
1106 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1108 dev_err(queue->ctrl->ctrl.device,
1109 "%s failed with error code %d\n", __func__, ret);
1114 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1116 u32 queue_idx = nvme_rdma_queue_idx(queue);
1119 return queue->ctrl->admin_tag_set.tags[queue_idx];
1120 return queue->ctrl->tag_set.tags[queue_idx - 1];
1123 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1125 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1126 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1127 struct ib_device *dev = queue->device->dev;
1128 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1129 struct nvme_command *cmd = sqe->data;
1133 if (WARN_ON_ONCE(aer_idx != 0))
1136 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1138 memset(cmd, 0, sizeof(*cmd));
1139 cmd->common.opcode = nvme_admin_async_event;
1140 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1141 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1142 nvme_rdma_set_sg_null(cmd);
1144 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1147 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1151 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1152 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1155 struct nvme_rdma_request *req;
1158 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1160 dev_err(queue->ctrl->ctrl.device,
1161 "tag 0x%x on QP %#x not found\n",
1162 cqe->command_id, queue->qp->qp_num);
1163 nvme_rdma_error_recovery(queue->ctrl);
1166 req = blk_mq_rq_to_pdu(rq);
1171 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1172 wc->ex.invalidate_rkey == req->mr->rkey)
1173 req->mr->need_inval = false;
1175 req->req.result = cqe->result;
1176 blk_mq_complete_request(rq, le16_to_cpu(cqe->status) >> 1);
1180 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1182 struct nvme_rdma_qe *qe =
1183 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1184 struct nvme_rdma_queue *queue = cq->cq_context;
1185 struct ib_device *ibdev = queue->device->dev;
1186 struct nvme_completion *cqe = qe->data;
1187 const size_t len = sizeof(struct nvme_completion);
1190 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1191 nvme_rdma_wr_error(cq, wc, "RECV");
1195 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1197 * AEN requests are special as they don't time out and can
1198 * survive any kind of queue freeze and often don't respond to
1199 * aborts. We don't even bother to allocate a struct request
1200 * for them but rather special case them here.
1202 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1203 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
1204 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1207 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1208 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1210 nvme_rdma_post_recv(queue, qe);
1214 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1216 __nvme_rdma_recv_done(cq, wc, -1);
1219 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1223 for (i = 0; i < queue->queue_size; i++) {
1224 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1226 goto out_destroy_queue_ib;
1231 out_destroy_queue_ib:
1232 nvme_rdma_destroy_queue_ib(queue);
1236 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1237 struct rdma_cm_event *ev)
1239 struct rdma_cm_id *cm_id = queue->cm_id;
1240 int status = ev->status;
1241 const char *rej_msg;
1242 const struct nvme_rdma_cm_rej *rej_data;
1245 rej_msg = rdma_reject_msg(cm_id, status);
1246 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1248 if (rej_data && rej_data_len >= sizeof(u16)) {
1249 u16 sts = le16_to_cpu(rej_data->sts);
1251 dev_err(queue->ctrl->ctrl.device,
1252 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1253 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1255 dev_err(queue->ctrl->ctrl.device,
1256 "Connect rejected: status %d (%s).\n", status, rej_msg);
1262 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1264 struct nvme_rdma_device *dev;
1267 dev = nvme_rdma_find_get_device(queue->cm_id);
1269 dev_err(queue->cm_id->device->dma_device,
1270 "no client data found!\n");
1271 return -ECONNREFUSED;
1274 ret = nvme_rdma_create_queue_ib(queue, dev);
1276 nvme_rdma_dev_put(dev);
1280 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1282 dev_err(queue->ctrl->ctrl.device,
1283 "rdma_resolve_route failed (%d).\n",
1285 goto out_destroy_queue;
1291 nvme_rdma_destroy_queue_ib(queue);
1296 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1298 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1299 struct rdma_conn_param param = { };
1300 struct nvme_rdma_cm_req priv = { };
1303 param.qp_num = queue->qp->qp_num;
1304 param.flow_control = 1;
1306 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1307 /* maximum retry count */
1308 param.retry_count = 7;
1309 param.rnr_retry_count = 7;
1310 param.private_data = &priv;
1311 param.private_data_len = sizeof(priv);
1313 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1314 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1316 * set the admin queue depth to the minimum size
1317 * specified by the Fabrics standard.
1319 if (priv.qid == 0) {
1320 priv.hrqsize = cpu_to_le16(NVMF_AQ_DEPTH);
1321 priv.hsqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1);
1324 * current interpretation of the fabrics spec
1325 * is at minimum you make hrqsize sqsize+1, or a
1326 * 1's based representation of sqsize.
1328 priv.hrqsize = cpu_to_le16(queue->queue_size);
1329 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1332 ret = rdma_connect(queue->cm_id, ¶m);
1334 dev_err(ctrl->ctrl.device,
1335 "rdma_connect failed (%d).\n", ret);
1336 goto out_destroy_queue_ib;
1341 out_destroy_queue_ib:
1342 nvme_rdma_destroy_queue_ib(queue);
1346 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1347 struct rdma_cm_event *ev)
1349 struct nvme_rdma_queue *queue = cm_id->context;
1352 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1353 rdma_event_msg(ev->event), ev->event,
1356 switch (ev->event) {
1357 case RDMA_CM_EVENT_ADDR_RESOLVED:
1358 cm_error = nvme_rdma_addr_resolved(queue);
1360 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1361 cm_error = nvme_rdma_route_resolved(queue);
1363 case RDMA_CM_EVENT_ESTABLISHED:
1364 queue->cm_error = nvme_rdma_conn_established(queue);
1365 /* complete cm_done regardless of success/failure */
1366 complete(&queue->cm_done);
1368 case RDMA_CM_EVENT_REJECTED:
1369 cm_error = nvme_rdma_conn_rejected(queue, ev);
1371 case RDMA_CM_EVENT_ADDR_ERROR:
1372 case RDMA_CM_EVENT_ROUTE_ERROR:
1373 case RDMA_CM_EVENT_CONNECT_ERROR:
1374 case RDMA_CM_EVENT_UNREACHABLE:
1375 dev_dbg(queue->ctrl->ctrl.device,
1376 "CM error event %d\n", ev->event);
1377 cm_error = -ECONNRESET;
1379 case RDMA_CM_EVENT_DISCONNECTED:
1380 case RDMA_CM_EVENT_ADDR_CHANGE:
1381 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1382 dev_dbg(queue->ctrl->ctrl.device,
1383 "disconnect received - connection closed\n");
1384 nvme_rdma_error_recovery(queue->ctrl);
1386 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1387 /* device removal is handled via the ib_client API */
1390 dev_err(queue->ctrl->ctrl.device,
1391 "Unexpected RDMA CM event (%d)\n", ev->event);
1392 nvme_rdma_error_recovery(queue->ctrl);
1397 queue->cm_error = cm_error;
1398 complete(&queue->cm_done);
1404 static enum blk_eh_timer_return
1405 nvme_rdma_timeout(struct request *rq, bool reserved)
1407 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1409 /* queue error recovery */
1410 nvme_rdma_error_recovery(req->queue->ctrl);
1412 /* fail with DNR on cmd timeout */
1413 rq->errors = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1415 return BLK_EH_HANDLED;
1419 * We cannot accept any other command until the Connect command has completed.
1421 static inline bool nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue,
1424 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
1425 struct nvme_command *cmd = (struct nvme_command *)rq->cmd;
1427 if (rq->cmd_type != REQ_TYPE_DRV_PRIV ||
1428 cmd->common.opcode != nvme_fabrics_command ||
1429 cmd->fabrics.fctype != nvme_fabrics_type_connect)
1436 static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1437 const struct blk_mq_queue_data *bd)
1439 struct nvme_ns *ns = hctx->queue->queuedata;
1440 struct nvme_rdma_queue *queue = hctx->driver_data;
1441 struct request *rq = bd->rq;
1442 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1443 struct nvme_rdma_qe *sqe = &req->sqe;
1444 struct nvme_command *c = sqe->data;
1446 struct ib_device *dev;
1447 unsigned int map_len;
1450 WARN_ON_ONCE(rq->tag < 0);
1452 if (!nvme_rdma_queue_is_ready(queue, rq))
1453 return BLK_MQ_RQ_QUEUE_BUSY;
1455 dev = queue->device->dev;
1456 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1457 sizeof(struct nvme_command), DMA_TO_DEVICE);
1459 ret = nvme_setup_cmd(ns, rq, c);
1460 if (ret != BLK_MQ_RQ_QUEUE_OK)
1463 blk_mq_start_request(rq);
1465 map_len = nvme_map_len(rq);
1466 ret = nvme_rdma_map_data(queue, rq, map_len, c);
1468 dev_err(queue->ctrl->ctrl.device,
1469 "Failed to map data (%d)\n", ret);
1470 nvme_cleanup_cmd(rq);
1474 ib_dma_sync_single_for_device(dev, sqe->dma,
1475 sizeof(struct nvme_command), DMA_TO_DEVICE);
1477 if (rq->cmd_type == REQ_TYPE_FS && req_op(rq) == REQ_OP_FLUSH)
1479 ret = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1480 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
1482 nvme_rdma_unmap_data(queue, rq);
1486 return BLK_MQ_RQ_QUEUE_OK;
1488 return (ret == -ENOMEM || ret == -EAGAIN) ?
1489 BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR;
1492 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1494 struct nvme_rdma_queue *queue = hctx->driver_data;
1495 struct ib_cq *cq = queue->ib_cq;
1499 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1500 while (ib_poll_cq(cq, 1, &wc) > 0) {
1501 struct ib_cqe *cqe = wc.wr_cqe;
1504 if (cqe->done == nvme_rdma_recv_done)
1505 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1514 static void nvme_rdma_complete_rq(struct request *rq)
1516 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1517 struct nvme_rdma_queue *queue = req->queue;
1520 nvme_rdma_unmap_data(queue, rq);
1522 if (unlikely(rq->errors)) {
1523 if (nvme_req_needs_retry(rq, rq->errors)) {
1524 nvme_requeue_req(rq);
1528 if (rq->cmd_type == REQ_TYPE_DRV_PRIV)
1531 error = nvme_error_status(rq->errors);
1534 blk_mq_end_request(rq, error);
1537 static struct blk_mq_ops nvme_rdma_mq_ops = {
1538 .queue_rq = nvme_rdma_queue_rq,
1539 .complete = nvme_rdma_complete_rq,
1540 .init_request = nvme_rdma_init_request,
1541 .exit_request = nvme_rdma_exit_request,
1542 .reinit_request = nvme_rdma_reinit_request,
1543 .init_hctx = nvme_rdma_init_hctx,
1544 .poll = nvme_rdma_poll,
1545 .timeout = nvme_rdma_timeout,
1548 static struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1549 .queue_rq = nvme_rdma_queue_rq,
1550 .complete = nvme_rdma_complete_rq,
1551 .init_request = nvme_rdma_init_admin_request,
1552 .exit_request = nvme_rdma_exit_admin_request,
1553 .reinit_request = nvme_rdma_reinit_request,
1554 .init_hctx = nvme_rdma_init_admin_hctx,
1555 .timeout = nvme_rdma_timeout,
1558 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
1562 error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
1566 ctrl->device = ctrl->queues[0].device;
1569 * We need a reference on the device as long as the tag_set is alive,
1570 * as the MRs in the request structures need a valid ib_device.
1573 if (!nvme_rdma_dev_get(ctrl->device))
1574 goto out_free_queue;
1576 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
1577 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
1579 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1580 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
1581 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
1582 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
1583 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1584 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1585 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1586 ctrl->admin_tag_set.driver_data = ctrl;
1587 ctrl->admin_tag_set.nr_hw_queues = 1;
1588 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1590 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1594 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1595 if (IS_ERR(ctrl->ctrl.admin_q)) {
1596 error = PTR_ERR(ctrl->ctrl.admin_q);
1597 goto out_free_tagset;
1600 error = nvmf_connect_admin_queue(&ctrl->ctrl);
1602 goto out_cleanup_queue;
1604 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
1606 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
1608 dev_err(ctrl->ctrl.device,
1609 "prop_get NVME_REG_CAP failed\n");
1610 goto out_cleanup_queue;
1614 min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize);
1616 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
1618 goto out_cleanup_queue;
1620 ctrl->ctrl.max_hw_sectors =
1621 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
1623 error = nvme_init_identify(&ctrl->ctrl);
1625 goto out_cleanup_queue;
1627 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
1628 &ctrl->async_event_sqe, sizeof(struct nvme_command),
1631 goto out_cleanup_queue;
1633 nvme_start_keep_alive(&ctrl->ctrl);
1638 blk_cleanup_queue(ctrl->ctrl.admin_q);
1640 /* disconnect and drain the queue before freeing the tagset */
1641 nvme_rdma_stop_queue(&ctrl->queues[0]);
1642 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1644 nvme_rdma_dev_put(ctrl->device);
1646 nvme_rdma_free_queue(&ctrl->queues[0]);
1650 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1652 nvme_stop_keep_alive(&ctrl->ctrl);
1653 cancel_work_sync(&ctrl->err_work);
1654 cancel_delayed_work_sync(&ctrl->reconnect_work);
1656 if (ctrl->queue_count > 1) {
1657 nvme_stop_queues(&ctrl->ctrl);
1658 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1659 nvme_cancel_request, &ctrl->ctrl);
1660 nvme_rdma_free_io_queues(ctrl);
1663 if (test_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[0].flags))
1664 nvme_shutdown_ctrl(&ctrl->ctrl);
1666 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
1667 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1668 nvme_cancel_request, &ctrl->ctrl);
1669 nvme_rdma_destroy_admin_queue(ctrl);
1672 static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1674 nvme_uninit_ctrl(&ctrl->ctrl);
1676 nvme_rdma_shutdown_ctrl(ctrl);
1678 if (ctrl->ctrl.tagset) {
1679 blk_cleanup_queue(ctrl->ctrl.connect_q);
1680 blk_mq_free_tag_set(&ctrl->tag_set);
1681 nvme_rdma_dev_put(ctrl->device);
1684 nvme_put_ctrl(&ctrl->ctrl);
1687 static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1689 struct nvme_rdma_ctrl *ctrl = container_of(work,
1690 struct nvme_rdma_ctrl, delete_work);
1692 __nvme_rdma_remove_ctrl(ctrl, true);
1695 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1697 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1700 if (!queue_work(nvme_rdma_wq, &ctrl->delete_work))
1706 static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1708 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1712 * Keep a reference until all work is flushed since
1713 * __nvme_rdma_del_ctrl can free the ctrl mem
1715 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1717 ret = __nvme_rdma_del_ctrl(ctrl);
1719 flush_work(&ctrl->delete_work);
1720 nvme_put_ctrl(&ctrl->ctrl);
1724 static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1726 struct nvme_rdma_ctrl *ctrl = container_of(work,
1727 struct nvme_rdma_ctrl, delete_work);
1729 __nvme_rdma_remove_ctrl(ctrl, false);
1732 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1734 struct nvme_rdma_ctrl *ctrl = container_of(work,
1735 struct nvme_rdma_ctrl, reset_work);
1739 nvme_rdma_shutdown_ctrl(ctrl);
1741 ret = nvme_rdma_configure_admin_queue(ctrl);
1743 /* ctrl is already shutdown, just remove the ctrl */
1744 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1748 if (ctrl->queue_count > 1) {
1749 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
1753 ret = nvme_rdma_init_io_queues(ctrl);
1757 ret = nvme_rdma_connect_io_queues(ctrl);
1762 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1763 WARN_ON_ONCE(!changed);
1765 if (ctrl->queue_count > 1) {
1766 nvme_start_queues(&ctrl->ctrl);
1767 nvme_queue_scan(&ctrl->ctrl);
1768 nvme_queue_async_events(&ctrl->ctrl);
1774 /* Deleting this dead controller... */
1775 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1776 WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work));
1779 static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl)
1781 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1783 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1786 if (!queue_work(nvme_rdma_wq, &ctrl->reset_work))
1789 flush_work(&ctrl->reset_work);
1794 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1796 .module = THIS_MODULE,
1798 .reg_read32 = nvmf_reg_read32,
1799 .reg_read64 = nvmf_reg_read64,
1800 .reg_write32 = nvmf_reg_write32,
1801 .reset_ctrl = nvme_rdma_reset_ctrl,
1802 .free_ctrl = nvme_rdma_free_ctrl,
1803 .submit_async_event = nvme_rdma_submit_async_event,
1804 .delete_ctrl = nvme_rdma_del_ctrl,
1805 .get_subsysnqn = nvmf_get_subsysnqn,
1806 .get_address = nvmf_get_address,
1809 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1811 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
1814 ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues);
1818 ctrl->queue_count = opts->nr_io_queues + 1;
1819 if (ctrl->queue_count < 2)
1822 dev_info(ctrl->ctrl.device,
1823 "creating %d I/O queues.\n", opts->nr_io_queues);
1825 ret = nvme_rdma_init_io_queues(ctrl);
1830 * We need a reference on the device as long as the tag_set is alive,
1831 * as the MRs in the request structures need a valid ib_device.
1834 if (!nvme_rdma_dev_get(ctrl->device))
1835 goto out_free_io_queues;
1837 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1838 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
1839 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
1840 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1841 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1842 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1843 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1844 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1845 ctrl->tag_set.driver_data = ctrl;
1846 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
1847 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1849 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1852 ctrl->ctrl.tagset = &ctrl->tag_set;
1854 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1855 if (IS_ERR(ctrl->ctrl.connect_q)) {
1856 ret = PTR_ERR(ctrl->ctrl.connect_q);
1857 goto out_free_tag_set;
1860 ret = nvme_rdma_connect_io_queues(ctrl);
1862 goto out_cleanup_connect_q;
1866 out_cleanup_connect_q:
1867 blk_cleanup_queue(ctrl->ctrl.connect_q);
1869 blk_mq_free_tag_set(&ctrl->tag_set);
1871 nvme_rdma_dev_put(ctrl->device);
1873 nvme_rdma_free_io_queues(ctrl);
1877 static int nvme_rdma_parse_ipaddr(struct sockaddr_in *in_addr, char *p)
1879 u8 *addr = (u8 *)&in_addr->sin_addr.s_addr;
1880 size_t buflen = strlen(p);
1882 /* XXX: handle IPv6 addresses */
1884 if (buflen > INET_ADDRSTRLEN)
1886 if (in4_pton(p, buflen, addr, '\0', NULL) == 0)
1888 in_addr->sin_family = AF_INET;
1892 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1893 struct nvmf_ctrl_options *opts)
1895 struct nvme_rdma_ctrl *ctrl;
1899 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1901 return ERR_PTR(-ENOMEM);
1902 ctrl->ctrl.opts = opts;
1903 INIT_LIST_HEAD(&ctrl->list);
1905 ret = nvme_rdma_parse_ipaddr(&ctrl->addr_in, opts->traddr);
1907 pr_err("malformed IP address passed: %s\n", opts->traddr);
1911 if (opts->mask & NVMF_OPT_TRSVCID) {
1914 ret = kstrtou16(opts->trsvcid, 0, &port);
1918 ctrl->addr_in.sin_port = cpu_to_be16(port);
1920 ctrl->addr_in.sin_port = cpu_to_be16(NVME_RDMA_IP_PORT);
1923 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1924 0 /* no quirks, we're perfect! */);
1928 ctrl->reconnect_delay = opts->reconnect_delay;
1929 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1930 nvme_rdma_reconnect_ctrl_work);
1931 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1932 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1933 INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work);
1934 spin_lock_init(&ctrl->lock);
1936 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1937 ctrl->ctrl.sqsize = opts->queue_size - 1;
1938 ctrl->ctrl.kato = opts->kato;
1941 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues),
1944 goto out_uninit_ctrl;
1946 ret = nvme_rdma_configure_admin_queue(ctrl);
1948 goto out_kfree_queues;
1950 /* sanity check icdoff */
1951 if (ctrl->ctrl.icdoff) {
1952 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1953 goto out_remove_admin_queue;
1956 /* sanity check keyed sgls */
1957 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1958 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1959 goto out_remove_admin_queue;
1962 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1963 /* warn if maxcmd is lower than queue_size */
1964 dev_warn(ctrl->ctrl.device,
1965 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1966 opts->queue_size, ctrl->ctrl.maxcmd);
1967 opts->queue_size = ctrl->ctrl.maxcmd;
1970 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1971 /* warn if sqsize is lower than queue_size */
1972 dev_warn(ctrl->ctrl.device,
1973 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1974 opts->queue_size, ctrl->ctrl.sqsize + 1);
1975 opts->queue_size = ctrl->ctrl.sqsize + 1;
1978 if (opts->nr_io_queues) {
1979 ret = nvme_rdma_create_io_queues(ctrl);
1981 goto out_remove_admin_queue;
1984 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1985 WARN_ON_ONCE(!changed);
1987 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
1988 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1990 kref_get(&ctrl->ctrl.kref);
1992 mutex_lock(&nvme_rdma_ctrl_mutex);
1993 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1994 mutex_unlock(&nvme_rdma_ctrl_mutex);
1996 if (opts->nr_io_queues) {
1997 nvme_queue_scan(&ctrl->ctrl);
1998 nvme_queue_async_events(&ctrl->ctrl);
2003 out_remove_admin_queue:
2004 nvme_stop_keep_alive(&ctrl->ctrl);
2005 nvme_rdma_destroy_admin_queue(ctrl);
2007 kfree(ctrl->queues);
2009 nvme_uninit_ctrl(&ctrl->ctrl);
2010 nvme_put_ctrl(&ctrl->ctrl);
2013 return ERR_PTR(ret);
2016 return ERR_PTR(ret);
2019 static struct nvmf_transport_ops nvme_rdma_transport = {
2021 .required_opts = NVMF_OPT_TRADDR,
2022 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY,
2023 .create_ctrl = nvme_rdma_create_ctrl,
2026 static void nvme_rdma_add_one(struct ib_device *ib_device)
2030 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2032 struct nvme_rdma_ctrl *ctrl;
2034 /* Delete all controllers using this device */
2035 mutex_lock(&nvme_rdma_ctrl_mutex);
2036 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2037 if (ctrl->device->dev != ib_device)
2039 dev_info(ctrl->ctrl.device,
2040 "Removing ctrl: NQN \"%s\", addr %pISp\n",
2041 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2042 __nvme_rdma_del_ctrl(ctrl);
2044 mutex_unlock(&nvme_rdma_ctrl_mutex);
2046 flush_workqueue(nvme_rdma_wq);
2049 static struct ib_client nvme_rdma_ib_client = {
2050 .name = "nvme_rdma",
2051 .add = nvme_rdma_add_one,
2052 .remove = nvme_rdma_remove_one
2055 static int __init nvme_rdma_init_module(void)
2059 nvme_rdma_wq = create_workqueue("nvme_rdma_wq");
2063 ret = ib_register_client(&nvme_rdma_ib_client);
2065 destroy_workqueue(nvme_rdma_wq);
2069 nvmf_register_transport(&nvme_rdma_transport);
2073 static void __exit nvme_rdma_cleanup_module(void)
2075 nvmf_unregister_transport(&nvme_rdma_transport);
2076 ib_unregister_client(&nvme_rdma_ib_client);
2077 destroy_workqueue(nvme_rdma_wq);
2080 module_init(nvme_rdma_init_module);
2081 module_exit(nvme_rdma_cleanup_module);
2083 MODULE_LICENSE("GPL v2");