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 3000 /* 3 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
46 * We handle AEN commands ourselves and don't even let the
47 * block layer know about them.
49 #define NVME_RDMA_NR_AEN_COMMANDS 1
50 #define NVME_RDMA_AQ_BLKMQ_DEPTH \
51 (NVME_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
53 struct nvme_rdma_device {
54 struct ib_device *dev;
57 struct list_head entry;
66 struct nvme_rdma_queue;
67 struct nvme_rdma_request {
68 struct nvme_request req;
70 struct nvme_rdma_qe sqe;
71 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
75 struct ib_reg_wr reg_wr;
76 struct ib_cqe reg_cqe;
77 struct nvme_rdma_queue *queue;
78 struct sg_table sg_table;
79 struct scatterlist first_sgl[];
82 enum nvme_rdma_queue_flags {
84 NVME_RDMA_Q_DELETING = 1,
87 struct nvme_rdma_queue {
88 struct nvme_rdma_qe *rsp_ring;
91 size_t cmnd_capsule_len;
92 struct nvme_rdma_ctrl *ctrl;
93 struct nvme_rdma_device *device;
98 struct rdma_cm_id *cm_id;
100 struct completion cm_done;
103 struct nvme_rdma_ctrl {
104 /* read only in the hot path */
105 struct nvme_rdma_queue *queues;
108 /* other member variables */
109 struct blk_mq_tag_set tag_set;
110 struct work_struct delete_work;
111 struct work_struct err_work;
113 struct nvme_rdma_qe async_event_sqe;
115 struct delayed_work reconnect_work;
117 struct list_head list;
119 struct blk_mq_tag_set admin_tag_set;
120 struct nvme_rdma_device *device;
125 struct sockaddr_storage addr;
126 struct sockaddr_storage src_addr;
128 struct nvme_ctrl ctrl;
131 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
133 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
136 static LIST_HEAD(device_list);
137 static DEFINE_MUTEX(device_list_mutex);
139 static LIST_HEAD(nvme_rdma_ctrl_list);
140 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
143 * Disabling this option makes small I/O goes faster, but is fundamentally
144 * unsafe. With it turned off we will have to register a global rkey that
145 * allows read and write access to all physical memory.
147 static bool register_always = true;
148 module_param(register_always, bool, 0444);
149 MODULE_PARM_DESC(register_always,
150 "Use memory registration even for contiguous memory regions");
152 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
153 struct rdma_cm_event *event);
154 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
156 /* XXX: really should move to a generic header sooner or later.. */
157 static inline void put_unaligned_le24(u32 val, u8 *p)
164 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
166 return queue - queue->ctrl->queues;
169 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
171 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
174 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
175 size_t capsule_size, enum dma_data_direction dir)
177 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
181 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
182 size_t capsule_size, enum dma_data_direction dir)
184 qe->data = kzalloc(capsule_size, GFP_KERNEL);
188 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
189 if (ib_dma_mapping_error(ibdev, qe->dma)) {
197 static void nvme_rdma_free_ring(struct ib_device *ibdev,
198 struct nvme_rdma_qe *ring, size_t ib_queue_size,
199 size_t capsule_size, enum dma_data_direction dir)
203 for (i = 0; i < ib_queue_size; i++)
204 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
208 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
209 size_t ib_queue_size, size_t capsule_size,
210 enum dma_data_direction dir)
212 struct nvme_rdma_qe *ring;
215 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
219 for (i = 0; i < ib_queue_size; i++) {
220 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
227 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
231 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
233 pr_debug("QP event %s (%d)\n",
234 ib_event_msg(event->event), event->event);
238 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
240 wait_for_completion_interruptible_timeout(&queue->cm_done,
241 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
242 return queue->cm_error;
245 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
247 struct nvme_rdma_device *dev = queue->device;
248 struct ib_qp_init_attr init_attr;
251 memset(&init_attr, 0, sizeof(init_attr));
252 init_attr.event_handler = nvme_rdma_qp_event;
254 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
256 init_attr.cap.max_recv_wr = queue->queue_size + 1;
257 init_attr.cap.max_recv_sge = 1;
258 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
259 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
260 init_attr.qp_type = IB_QPT_RC;
261 init_attr.send_cq = queue->ib_cq;
262 init_attr.recv_cq = queue->ib_cq;
264 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
266 queue->qp = queue->cm_id->qp;
270 static int nvme_rdma_reinit_request(void *data, struct request *rq)
272 struct nvme_rdma_ctrl *ctrl = data;
273 struct nvme_rdma_device *dev = ctrl->device;
274 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
277 if (!req->mr->need_inval)
280 ib_dereg_mr(req->mr);
282 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
284 if (IS_ERR(req->mr)) {
285 ret = PTR_ERR(req->mr);
290 req->mr->need_inval = false;
296 static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
297 struct request *rq, unsigned int hctx_idx)
299 struct nvme_rdma_ctrl *ctrl = set->driver_data;
300 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
301 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
302 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
303 struct nvme_rdma_device *dev = queue->device;
306 ib_dereg_mr(req->mr);
308 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
312 static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
313 struct request *rq, unsigned int hctx_idx,
314 unsigned int numa_node)
316 struct nvme_rdma_ctrl *ctrl = set->driver_data;
317 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
318 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
319 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
320 struct nvme_rdma_device *dev = queue->device;
321 struct ib_device *ibdev = dev->dev;
324 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
329 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
331 if (IS_ERR(req->mr)) {
332 ret = PTR_ERR(req->mr);
341 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
346 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
347 unsigned int hctx_idx)
349 struct nvme_rdma_ctrl *ctrl = data;
350 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
352 BUG_ON(hctx_idx >= ctrl->queue_count);
354 hctx->driver_data = queue;
358 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
359 unsigned int hctx_idx)
361 struct nvme_rdma_ctrl *ctrl = data;
362 struct nvme_rdma_queue *queue = &ctrl->queues[0];
364 BUG_ON(hctx_idx != 0);
366 hctx->driver_data = queue;
370 static void nvme_rdma_free_dev(struct kref *ref)
372 struct nvme_rdma_device *ndev =
373 container_of(ref, struct nvme_rdma_device, ref);
375 mutex_lock(&device_list_mutex);
376 list_del(&ndev->entry);
377 mutex_unlock(&device_list_mutex);
379 ib_dealloc_pd(ndev->pd);
383 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
385 kref_put(&dev->ref, nvme_rdma_free_dev);
388 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
390 return kref_get_unless_zero(&dev->ref);
393 static struct nvme_rdma_device *
394 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
396 struct nvme_rdma_device *ndev;
398 mutex_lock(&device_list_mutex);
399 list_for_each_entry(ndev, &device_list, entry) {
400 if (ndev->dev->node_guid == cm_id->device->node_guid &&
401 nvme_rdma_dev_get(ndev))
405 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
409 ndev->dev = cm_id->device;
410 kref_init(&ndev->ref);
412 ndev->pd = ib_alloc_pd(ndev->dev,
413 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
414 if (IS_ERR(ndev->pd))
417 if (!(ndev->dev->attrs.device_cap_flags &
418 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
419 dev_err(&ndev->dev->dev,
420 "Memory registrations not supported.\n");
424 list_add(&ndev->entry, &device_list);
426 mutex_unlock(&device_list_mutex);
430 ib_dealloc_pd(ndev->pd);
434 mutex_unlock(&device_list_mutex);
438 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
440 struct nvme_rdma_device *dev;
441 struct ib_device *ibdev;
445 rdma_destroy_qp(queue->cm_id);
446 ib_free_cq(queue->ib_cq);
448 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
449 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
451 nvme_rdma_dev_put(dev);
454 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
456 struct ib_device *ibdev;
457 const int send_wr_factor = 3; /* MR, SEND, INV */
458 const int cq_factor = send_wr_factor + 1; /* + RECV */
459 int comp_vector, idx = nvme_rdma_queue_idx(queue);
462 queue->device = nvme_rdma_find_get_device(queue->cm_id);
463 if (!queue->device) {
464 dev_err(queue->cm_id->device->dev.parent,
465 "no client data found!\n");
466 return -ECONNREFUSED;
468 ibdev = queue->device->dev;
471 * The admin queue is barely used once the controller is live, so don't
472 * bother to spread it out.
477 comp_vector = idx % ibdev->num_comp_vectors;
480 /* +1 for ib_stop_cq */
481 queue->ib_cq = ib_alloc_cq(ibdev, queue,
482 cq_factor * queue->queue_size + 1,
483 comp_vector, IB_POLL_SOFTIRQ);
484 if (IS_ERR(queue->ib_cq)) {
485 ret = PTR_ERR(queue->ib_cq);
489 ret = nvme_rdma_create_qp(queue, send_wr_factor);
491 goto out_destroy_ib_cq;
493 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
494 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
495 if (!queue->rsp_ring) {
503 ib_destroy_qp(queue->qp);
505 ib_free_cq(queue->ib_cq);
507 nvme_rdma_dev_put(queue->device);
511 static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl,
512 int idx, size_t queue_size)
514 struct nvme_rdma_queue *queue;
515 struct sockaddr *src_addr = NULL;
518 queue = &ctrl->queues[idx];
520 init_completion(&queue->cm_done);
523 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
525 queue->cmnd_capsule_len = sizeof(struct nvme_command);
527 queue->queue_size = queue_size;
529 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
530 RDMA_PS_TCP, IB_QPT_RC);
531 if (IS_ERR(queue->cm_id)) {
532 dev_info(ctrl->ctrl.device,
533 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
534 return PTR_ERR(queue->cm_id);
537 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
538 src_addr = (struct sockaddr *)&ctrl->src_addr;
540 queue->cm_error = -ETIMEDOUT;
541 ret = rdma_resolve_addr(queue->cm_id, src_addr,
542 (struct sockaddr *)&ctrl->addr,
543 NVME_RDMA_CONNECT_TIMEOUT_MS);
545 dev_info(ctrl->ctrl.device,
546 "rdma_resolve_addr failed (%d).\n", ret);
547 goto out_destroy_cm_id;
550 ret = nvme_rdma_wait_for_cm(queue);
552 dev_info(ctrl->ctrl.device,
553 "rdma_resolve_addr wait failed (%d).\n", ret);
554 goto out_destroy_cm_id;
557 clear_bit(NVME_RDMA_Q_DELETING, &queue->flags);
562 rdma_destroy_id(queue->cm_id);
566 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
568 rdma_disconnect(queue->cm_id);
569 ib_drain_qp(queue->qp);
572 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
574 nvme_rdma_destroy_queue_ib(queue);
575 rdma_destroy_id(queue->cm_id);
578 static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue)
580 if (test_and_set_bit(NVME_RDMA_Q_DELETING, &queue->flags))
582 nvme_rdma_stop_queue(queue);
583 nvme_rdma_free_queue(queue);
586 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
590 for (i = 1; i < ctrl->queue_count; i++)
591 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
594 static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl)
598 for (i = 1; i < ctrl->queue_count; i++) {
599 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
601 dev_info(ctrl->ctrl.device,
602 "failed to connect i/o queue: %d\n", ret);
603 goto out_free_queues;
605 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
611 nvme_rdma_free_io_queues(ctrl);
615 static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl)
617 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
618 unsigned int nr_io_queues;
621 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
622 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
626 ctrl->queue_count = nr_io_queues + 1;
627 if (ctrl->queue_count < 2)
630 dev_info(ctrl->ctrl.device,
631 "creating %d I/O queues.\n", nr_io_queues);
633 for (i = 1; i < ctrl->queue_count; i++) {
634 ret = nvme_rdma_init_queue(ctrl, i,
635 ctrl->ctrl.opts->queue_size);
637 dev_info(ctrl->ctrl.device,
638 "failed to initialize i/o queue: %d\n", ret);
639 goto out_free_queues;
646 for (i--; i >= 1; i--)
647 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
652 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl)
654 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
655 sizeof(struct nvme_command), DMA_TO_DEVICE);
656 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
657 blk_cleanup_queue(ctrl->ctrl.admin_q);
658 blk_mq_free_tag_set(&ctrl->admin_tag_set);
659 nvme_rdma_dev_put(ctrl->device);
662 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
664 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
666 if (list_empty(&ctrl->list))
669 mutex_lock(&nvme_rdma_ctrl_mutex);
670 list_del(&ctrl->list);
671 mutex_unlock(&nvme_rdma_ctrl_mutex);
674 nvmf_free_options(nctrl->opts);
679 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
681 /* If we are resetting/deleting then do nothing */
682 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
683 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
684 ctrl->ctrl.state == NVME_CTRL_LIVE);
688 if (nvmf_should_reconnect(&ctrl->ctrl)) {
689 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
690 ctrl->ctrl.opts->reconnect_delay);
691 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
692 ctrl->ctrl.opts->reconnect_delay * HZ);
694 dev_info(ctrl->ctrl.device, "Removing controller...\n");
695 queue_work(nvme_wq, &ctrl->delete_work);
699 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
701 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
702 struct nvme_rdma_ctrl, reconnect_work);
706 ++ctrl->ctrl.nr_reconnects;
708 if (ctrl->queue_count > 1) {
709 nvme_rdma_free_io_queues(ctrl);
711 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
716 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
718 ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set);
722 ret = nvme_rdma_init_queue(ctrl, 0, NVME_AQ_DEPTH);
726 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
730 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
732 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
736 nvme_start_keep_alive(&ctrl->ctrl);
738 if (ctrl->queue_count > 1) {
739 ret = nvme_rdma_init_io_queues(ctrl);
743 ret = nvme_rdma_connect_io_queues(ctrl);
748 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
749 WARN_ON_ONCE(!changed);
750 ctrl->ctrl.nr_reconnects = 0;
752 if (ctrl->queue_count > 1) {
753 nvme_queue_scan(&ctrl->ctrl);
754 nvme_queue_async_events(&ctrl->ctrl);
757 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
762 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
763 ctrl->ctrl.nr_reconnects);
764 nvme_rdma_reconnect_or_remove(ctrl);
767 static void nvme_rdma_error_recovery_work(struct work_struct *work)
769 struct nvme_rdma_ctrl *ctrl = container_of(work,
770 struct nvme_rdma_ctrl, err_work);
773 nvme_stop_keep_alive(&ctrl->ctrl);
775 for (i = 0; i < ctrl->queue_count; i++)
776 clear_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
778 if (ctrl->queue_count > 1)
779 nvme_stop_queues(&ctrl->ctrl);
780 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
782 /* We must take care of fastfail/requeue all our inflight requests */
783 if (ctrl->queue_count > 1)
784 blk_mq_tagset_busy_iter(&ctrl->tag_set,
785 nvme_cancel_request, &ctrl->ctrl);
786 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
787 nvme_cancel_request, &ctrl->ctrl);
790 * queues are not a live anymore, so restart the queues to fail fast
793 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
794 nvme_start_queues(&ctrl->ctrl);
796 nvme_rdma_reconnect_or_remove(ctrl);
799 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
801 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
804 queue_work(nvme_wq, &ctrl->err_work);
807 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
810 struct nvme_rdma_queue *queue = cq->cq_context;
811 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
813 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
814 dev_info(ctrl->ctrl.device,
815 "%s for CQE 0x%p failed with status %s (%d)\n",
817 ib_wc_status_msg(wc->status), wc->status);
818 nvme_rdma_error_recovery(ctrl);
821 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
823 if (unlikely(wc->status != IB_WC_SUCCESS))
824 nvme_rdma_wr_error(cq, wc, "MEMREG");
827 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
829 if (unlikely(wc->status != IB_WC_SUCCESS))
830 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
833 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
834 struct nvme_rdma_request *req)
836 struct ib_send_wr *bad_wr;
837 struct ib_send_wr wr = {
838 .opcode = IB_WR_LOCAL_INV,
842 .ex.invalidate_rkey = req->mr->rkey,
845 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
846 wr.wr_cqe = &req->reg_cqe;
848 return ib_post_send(queue->qp, &wr, &bad_wr);
851 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
854 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
855 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
856 struct nvme_rdma_device *dev = queue->device;
857 struct ib_device *ibdev = dev->dev;
860 if (!blk_rq_bytes(rq))
863 if (req->mr->need_inval) {
864 res = nvme_rdma_inv_rkey(queue, req);
866 dev_err(ctrl->ctrl.device,
867 "Queueing INV WR for rkey %#x failed (%d)\n",
869 nvme_rdma_error_recovery(queue->ctrl);
873 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
874 req->nents, rq_data_dir(rq) ==
875 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
877 nvme_cleanup_cmd(rq);
878 sg_free_table_chained(&req->sg_table, true);
881 static int nvme_rdma_set_sg_null(struct nvme_command *c)
883 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
886 put_unaligned_le24(0, sg->length);
887 put_unaligned_le32(0, sg->key);
888 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
892 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
893 struct nvme_rdma_request *req, struct nvme_command *c)
895 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
897 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
898 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
899 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
901 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
902 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
903 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
905 req->inline_data = true;
910 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
911 struct nvme_rdma_request *req, struct nvme_command *c)
913 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
915 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
916 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
917 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
918 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
922 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
923 struct nvme_rdma_request *req, struct nvme_command *c,
926 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
929 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE);
936 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
938 req->reg_cqe.done = nvme_rdma_memreg_done;
939 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
940 req->reg_wr.wr.opcode = IB_WR_REG_MR;
941 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
942 req->reg_wr.wr.num_sge = 0;
943 req->reg_wr.mr = req->mr;
944 req->reg_wr.key = req->mr->rkey;
945 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
946 IB_ACCESS_REMOTE_READ |
947 IB_ACCESS_REMOTE_WRITE;
949 req->mr->need_inval = true;
951 sg->addr = cpu_to_le64(req->mr->iova);
952 put_unaligned_le24(req->mr->length, sg->length);
953 put_unaligned_le32(req->mr->rkey, sg->key);
954 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
955 NVME_SGL_FMT_INVALIDATE;
960 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
961 struct request *rq, struct nvme_command *c)
963 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
964 struct nvme_rdma_device *dev = queue->device;
965 struct ib_device *ibdev = dev->dev;
969 req->inline_data = false;
970 req->mr->need_inval = false;
972 c->common.flags |= NVME_CMD_SGL_METABUF;
974 if (!blk_rq_bytes(rq))
975 return nvme_rdma_set_sg_null(c);
977 req->sg_table.sgl = req->first_sgl;
978 ret = sg_alloc_table_chained(&req->sg_table,
979 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
983 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
985 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
986 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
987 if (unlikely(count <= 0)) {
988 sg_free_table_chained(&req->sg_table, true);
993 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
994 blk_rq_payload_bytes(rq) <=
995 nvme_rdma_inline_data_size(queue))
996 return nvme_rdma_map_sg_inline(queue, req, c);
998 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
999 return nvme_rdma_map_sg_single(queue, req, c);
1002 return nvme_rdma_map_sg_fr(queue, req, c, count);
1005 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1007 if (unlikely(wc->status != IB_WC_SUCCESS))
1008 nvme_rdma_wr_error(cq, wc, "SEND");
1011 static inline int nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue)
1016 * We signal completion every queue depth/2 and also handle the
1017 * degenerated case of a device with queue_depth=1, where we
1018 * would need to signal every message.
1020 sig_limit = max(queue->queue_size / 2, 1);
1021 return (++queue->sig_count % sig_limit) == 0;
1024 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1025 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1026 struct ib_send_wr *first, bool flush)
1028 struct ib_send_wr wr, *bad_wr;
1031 sge->addr = qe->dma;
1032 sge->length = sizeof(struct nvme_command),
1033 sge->lkey = queue->device->pd->local_dma_lkey;
1035 qe->cqe.done = nvme_rdma_send_done;
1038 wr.wr_cqe = &qe->cqe;
1040 wr.num_sge = num_sge;
1041 wr.opcode = IB_WR_SEND;
1045 * Unsignalled send completions are another giant desaster in the
1046 * IB Verbs spec: If we don't regularly post signalled sends
1047 * the send queue will fill up and only a QP reset will rescue us.
1048 * Would have been way to obvious to handle this in hardware or
1049 * at least the RDMA stack..
1051 * Always signal the flushes. The magic request used for the flush
1052 * sequencer is not allocated in our driver's tagset and it's
1053 * triggered to be freed by blk_cleanup_queue(). So we need to
1054 * always mark it as signaled to ensure that the "wr_cqe", which is
1055 * embedded in request's payload, is not freed when __ib_process_cq()
1056 * calls wr_cqe->done().
1058 if (nvme_rdma_queue_sig_limit(queue) || flush)
1059 wr.send_flags |= IB_SEND_SIGNALED;
1066 ret = ib_post_send(queue->qp, first, &bad_wr);
1068 dev_err(queue->ctrl->ctrl.device,
1069 "%s failed with error code %d\n", __func__, ret);
1074 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1075 struct nvme_rdma_qe *qe)
1077 struct ib_recv_wr wr, *bad_wr;
1081 list.addr = qe->dma;
1082 list.length = sizeof(struct nvme_completion);
1083 list.lkey = queue->device->pd->local_dma_lkey;
1085 qe->cqe.done = nvme_rdma_recv_done;
1088 wr.wr_cqe = &qe->cqe;
1092 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1094 dev_err(queue->ctrl->ctrl.device,
1095 "%s failed with error code %d\n", __func__, ret);
1100 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1102 u32 queue_idx = nvme_rdma_queue_idx(queue);
1105 return queue->ctrl->admin_tag_set.tags[queue_idx];
1106 return queue->ctrl->tag_set.tags[queue_idx - 1];
1109 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1111 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1112 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1113 struct ib_device *dev = queue->device->dev;
1114 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1115 struct nvme_command *cmd = sqe->data;
1119 if (WARN_ON_ONCE(aer_idx != 0))
1122 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1124 memset(cmd, 0, sizeof(*cmd));
1125 cmd->common.opcode = nvme_admin_async_event;
1126 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1127 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1128 nvme_rdma_set_sg_null(cmd);
1130 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1133 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1137 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1138 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1141 struct nvme_rdma_request *req;
1144 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1146 dev_err(queue->ctrl->ctrl.device,
1147 "tag 0x%x on QP %#x not found\n",
1148 cqe->command_id, queue->qp->qp_num);
1149 nvme_rdma_error_recovery(queue->ctrl);
1152 req = blk_mq_rq_to_pdu(rq);
1157 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1158 wc->ex.invalidate_rkey == req->mr->rkey)
1159 req->mr->need_inval = false;
1161 nvme_end_request(rq, cqe->status, cqe->result);
1165 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1167 struct nvme_rdma_qe *qe =
1168 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1169 struct nvme_rdma_queue *queue = cq->cq_context;
1170 struct ib_device *ibdev = queue->device->dev;
1171 struct nvme_completion *cqe = qe->data;
1172 const size_t len = sizeof(struct nvme_completion);
1175 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1176 nvme_rdma_wr_error(cq, wc, "RECV");
1180 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1182 * AEN requests are special as they don't time out and can
1183 * survive any kind of queue freeze and often don't respond to
1184 * aborts. We don't even bother to allocate a struct request
1185 * for them but rather special case them here.
1187 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1188 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
1189 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1192 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1193 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1195 nvme_rdma_post_recv(queue, qe);
1199 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1201 __nvme_rdma_recv_done(cq, wc, -1);
1204 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1208 for (i = 0; i < queue->queue_size; i++) {
1209 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1211 goto out_destroy_queue_ib;
1216 out_destroy_queue_ib:
1217 nvme_rdma_destroy_queue_ib(queue);
1221 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1222 struct rdma_cm_event *ev)
1224 struct rdma_cm_id *cm_id = queue->cm_id;
1225 int status = ev->status;
1226 const char *rej_msg;
1227 const struct nvme_rdma_cm_rej *rej_data;
1230 rej_msg = rdma_reject_msg(cm_id, status);
1231 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1233 if (rej_data && rej_data_len >= sizeof(u16)) {
1234 u16 sts = le16_to_cpu(rej_data->sts);
1236 dev_err(queue->ctrl->ctrl.device,
1237 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1238 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1240 dev_err(queue->ctrl->ctrl.device,
1241 "Connect rejected: status %d (%s).\n", status, rej_msg);
1247 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1251 ret = nvme_rdma_create_queue_ib(queue);
1255 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1257 dev_err(queue->ctrl->ctrl.device,
1258 "rdma_resolve_route failed (%d).\n",
1260 goto out_destroy_queue;
1266 nvme_rdma_destroy_queue_ib(queue);
1270 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1272 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1273 struct rdma_conn_param param = { };
1274 struct nvme_rdma_cm_req priv = { };
1277 param.qp_num = queue->qp->qp_num;
1278 param.flow_control = 1;
1280 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1281 /* maximum retry count */
1282 param.retry_count = 7;
1283 param.rnr_retry_count = 7;
1284 param.private_data = &priv;
1285 param.private_data_len = sizeof(priv);
1287 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1288 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1290 * set the admin queue depth to the minimum size
1291 * specified by the Fabrics standard.
1293 if (priv.qid == 0) {
1294 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1295 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
1298 * current interpretation of the fabrics spec
1299 * is at minimum you make hrqsize sqsize+1, or a
1300 * 1's based representation of sqsize.
1302 priv.hrqsize = cpu_to_le16(queue->queue_size);
1303 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1306 ret = rdma_connect(queue->cm_id, ¶m);
1308 dev_err(ctrl->ctrl.device,
1309 "rdma_connect failed (%d).\n", ret);
1310 goto out_destroy_queue_ib;
1315 out_destroy_queue_ib:
1316 nvme_rdma_destroy_queue_ib(queue);
1320 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1321 struct rdma_cm_event *ev)
1323 struct nvme_rdma_queue *queue = cm_id->context;
1326 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1327 rdma_event_msg(ev->event), ev->event,
1330 switch (ev->event) {
1331 case RDMA_CM_EVENT_ADDR_RESOLVED:
1332 cm_error = nvme_rdma_addr_resolved(queue);
1334 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1335 cm_error = nvme_rdma_route_resolved(queue);
1337 case RDMA_CM_EVENT_ESTABLISHED:
1338 queue->cm_error = nvme_rdma_conn_established(queue);
1339 /* complete cm_done regardless of success/failure */
1340 complete(&queue->cm_done);
1342 case RDMA_CM_EVENT_REJECTED:
1343 nvme_rdma_destroy_queue_ib(queue);
1344 cm_error = nvme_rdma_conn_rejected(queue, ev);
1346 case RDMA_CM_EVENT_ROUTE_ERROR:
1347 case RDMA_CM_EVENT_CONNECT_ERROR:
1348 case RDMA_CM_EVENT_UNREACHABLE:
1349 nvme_rdma_destroy_queue_ib(queue);
1350 case RDMA_CM_EVENT_ADDR_ERROR:
1351 dev_dbg(queue->ctrl->ctrl.device,
1352 "CM error event %d\n", ev->event);
1353 cm_error = -ECONNRESET;
1355 case RDMA_CM_EVENT_DISCONNECTED:
1356 case RDMA_CM_EVENT_ADDR_CHANGE:
1357 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1358 dev_dbg(queue->ctrl->ctrl.device,
1359 "disconnect received - connection closed\n");
1360 nvme_rdma_error_recovery(queue->ctrl);
1362 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1363 /* device removal is handled via the ib_client API */
1366 dev_err(queue->ctrl->ctrl.device,
1367 "Unexpected RDMA CM event (%d)\n", ev->event);
1368 nvme_rdma_error_recovery(queue->ctrl);
1373 queue->cm_error = cm_error;
1374 complete(&queue->cm_done);
1380 static enum blk_eh_timer_return
1381 nvme_rdma_timeout(struct request *rq, bool reserved)
1383 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1385 /* queue error recovery */
1386 nvme_rdma_error_recovery(req->queue->ctrl);
1388 /* fail with DNR on cmd timeout */
1389 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1391 return BLK_EH_HANDLED;
1395 * We cannot accept any other command until the Connect command has completed.
1397 static inline blk_status_t
1398 nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue, struct request *rq)
1400 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
1401 struct nvme_command *cmd = nvme_req(rq)->cmd;
1403 if (!blk_rq_is_passthrough(rq) ||
1404 cmd->common.opcode != nvme_fabrics_command ||
1405 cmd->fabrics.fctype != nvme_fabrics_type_connect) {
1407 * reconnecting state means transport disruption, which
1408 * can take a long time and even might fail permanently,
1409 * so we can't let incoming I/O be requeued forever.
1410 * fail it fast to allow upper layers a chance to
1413 if (queue->ctrl->ctrl.state == NVME_CTRL_RECONNECTING)
1414 return BLK_STS_IOERR;
1415 return BLK_STS_RESOURCE; /* try again later */
1422 static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1423 const struct blk_mq_queue_data *bd)
1425 struct nvme_ns *ns = hctx->queue->queuedata;
1426 struct nvme_rdma_queue *queue = hctx->driver_data;
1427 struct request *rq = bd->rq;
1428 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1429 struct nvme_rdma_qe *sqe = &req->sqe;
1430 struct nvme_command *c = sqe->data;
1432 struct ib_device *dev;
1436 WARN_ON_ONCE(rq->tag < 0);
1438 ret = nvme_rdma_queue_is_ready(queue, rq);
1442 dev = queue->device->dev;
1443 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1444 sizeof(struct nvme_command), DMA_TO_DEVICE);
1446 ret = nvme_setup_cmd(ns, rq, c);
1450 blk_mq_start_request(rq);
1452 err = nvme_rdma_map_data(queue, rq, c);
1454 dev_err(queue->ctrl->ctrl.device,
1455 "Failed to map data (%d)\n", err);
1456 nvme_cleanup_cmd(rq);
1460 ib_dma_sync_single_for_device(dev, sqe->dma,
1461 sizeof(struct nvme_command), DMA_TO_DEVICE);
1463 if (req_op(rq) == REQ_OP_FLUSH)
1465 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1466 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
1468 nvme_rdma_unmap_data(queue, rq);
1474 if (err == -ENOMEM || err == -EAGAIN)
1475 return BLK_STS_RESOURCE;
1476 return BLK_STS_IOERR;
1479 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1481 struct nvme_rdma_queue *queue = hctx->driver_data;
1482 struct ib_cq *cq = queue->ib_cq;
1486 while (ib_poll_cq(cq, 1, &wc) > 0) {
1487 struct ib_cqe *cqe = wc.wr_cqe;
1490 if (cqe->done == nvme_rdma_recv_done)
1491 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1500 static void nvme_rdma_complete_rq(struct request *rq)
1502 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1504 nvme_rdma_unmap_data(req->queue, rq);
1505 nvme_complete_rq(rq);
1508 static const struct blk_mq_ops nvme_rdma_mq_ops = {
1509 .queue_rq = nvme_rdma_queue_rq,
1510 .complete = nvme_rdma_complete_rq,
1511 .init_request = nvme_rdma_init_request,
1512 .exit_request = nvme_rdma_exit_request,
1513 .reinit_request = nvme_rdma_reinit_request,
1514 .init_hctx = nvme_rdma_init_hctx,
1515 .poll = nvme_rdma_poll,
1516 .timeout = nvme_rdma_timeout,
1519 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1520 .queue_rq = nvme_rdma_queue_rq,
1521 .complete = nvme_rdma_complete_rq,
1522 .init_request = nvme_rdma_init_request,
1523 .exit_request = nvme_rdma_exit_request,
1524 .reinit_request = nvme_rdma_reinit_request,
1525 .init_hctx = nvme_rdma_init_admin_hctx,
1526 .timeout = nvme_rdma_timeout,
1529 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
1533 error = nvme_rdma_init_queue(ctrl, 0, NVME_AQ_DEPTH);
1537 ctrl->device = ctrl->queues[0].device;
1540 * We need a reference on the device as long as the tag_set is alive,
1541 * as the MRs in the request structures need a valid ib_device.
1544 if (!nvme_rdma_dev_get(ctrl->device))
1545 goto out_free_queue;
1547 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
1548 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
1550 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1551 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
1552 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
1553 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
1554 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1555 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1556 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1557 ctrl->admin_tag_set.driver_data = ctrl;
1558 ctrl->admin_tag_set.nr_hw_queues = 1;
1559 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1561 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1565 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1566 if (IS_ERR(ctrl->ctrl.admin_q)) {
1567 error = PTR_ERR(ctrl->ctrl.admin_q);
1568 goto out_free_tagset;
1571 error = nvmf_connect_admin_queue(&ctrl->ctrl);
1573 goto out_cleanup_queue;
1575 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
1577 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
1579 dev_err(ctrl->ctrl.device,
1580 "prop_get NVME_REG_CAP failed\n");
1581 goto out_cleanup_queue;
1585 min_t(int, NVME_CAP_MQES(ctrl->cap), ctrl->ctrl.sqsize);
1587 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
1589 goto out_cleanup_queue;
1591 ctrl->ctrl.max_hw_sectors =
1592 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
1594 error = nvme_init_identify(&ctrl->ctrl);
1596 goto out_cleanup_queue;
1598 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
1599 &ctrl->async_event_sqe, sizeof(struct nvme_command),
1602 goto out_cleanup_queue;
1604 nvme_start_keep_alive(&ctrl->ctrl);
1609 blk_cleanup_queue(ctrl->ctrl.admin_q);
1611 /* disconnect and drain the queue before freeing the tagset */
1612 nvme_rdma_stop_queue(&ctrl->queues[0]);
1613 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1615 nvme_rdma_dev_put(ctrl->device);
1617 nvme_rdma_free_queue(&ctrl->queues[0]);
1621 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1623 nvme_stop_keep_alive(&ctrl->ctrl);
1624 cancel_work_sync(&ctrl->err_work);
1625 cancel_delayed_work_sync(&ctrl->reconnect_work);
1627 if (ctrl->queue_count > 1) {
1628 nvme_stop_queues(&ctrl->ctrl);
1629 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1630 nvme_cancel_request, &ctrl->ctrl);
1631 nvme_rdma_free_io_queues(ctrl);
1634 if (test_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags))
1635 nvme_shutdown_ctrl(&ctrl->ctrl);
1637 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
1638 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1639 nvme_cancel_request, &ctrl->ctrl);
1640 nvme_rdma_destroy_admin_queue(ctrl);
1643 static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1645 nvme_uninit_ctrl(&ctrl->ctrl);
1647 nvme_rdma_shutdown_ctrl(ctrl);
1649 if (ctrl->ctrl.tagset) {
1650 blk_cleanup_queue(ctrl->ctrl.connect_q);
1651 blk_mq_free_tag_set(&ctrl->tag_set);
1652 nvme_rdma_dev_put(ctrl->device);
1655 nvme_put_ctrl(&ctrl->ctrl);
1658 static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1660 struct nvme_rdma_ctrl *ctrl = container_of(work,
1661 struct nvme_rdma_ctrl, delete_work);
1663 __nvme_rdma_remove_ctrl(ctrl, true);
1666 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1668 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1671 if (!queue_work(nvme_wq, &ctrl->delete_work))
1677 static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1679 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1683 * Keep a reference until all work is flushed since
1684 * __nvme_rdma_del_ctrl can free the ctrl mem
1686 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1688 ret = __nvme_rdma_del_ctrl(ctrl);
1690 flush_work(&ctrl->delete_work);
1691 nvme_put_ctrl(&ctrl->ctrl);
1695 static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1697 struct nvme_rdma_ctrl *ctrl = container_of(work,
1698 struct nvme_rdma_ctrl, delete_work);
1700 __nvme_rdma_remove_ctrl(ctrl, false);
1703 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1705 struct nvme_rdma_ctrl *ctrl =
1706 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
1710 nvme_rdma_shutdown_ctrl(ctrl);
1712 ret = nvme_rdma_configure_admin_queue(ctrl);
1714 /* ctrl is already shutdown, just remove the ctrl */
1715 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1719 if (ctrl->queue_count > 1) {
1720 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
1724 ret = nvme_rdma_init_io_queues(ctrl);
1728 ret = nvme_rdma_connect_io_queues(ctrl);
1733 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1734 WARN_ON_ONCE(!changed);
1736 if (ctrl->queue_count > 1) {
1737 nvme_start_queues(&ctrl->ctrl);
1738 nvme_queue_scan(&ctrl->ctrl);
1739 nvme_queue_async_events(&ctrl->ctrl);
1745 /* Deleting this dead controller... */
1746 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1747 WARN_ON(!queue_work(nvme_wq, &ctrl->delete_work));
1750 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1752 .module = THIS_MODULE,
1753 .flags = NVME_F_FABRICS,
1754 .reg_read32 = nvmf_reg_read32,
1755 .reg_read64 = nvmf_reg_read64,
1756 .reg_write32 = nvmf_reg_write32,
1757 .free_ctrl = nvme_rdma_free_ctrl,
1758 .submit_async_event = nvme_rdma_submit_async_event,
1759 .delete_ctrl = nvme_rdma_del_ctrl,
1760 .get_address = nvmf_get_address,
1763 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1767 ret = nvme_rdma_init_io_queues(ctrl);
1772 * We need a reference on the device as long as the tag_set is alive,
1773 * as the MRs in the request structures need a valid ib_device.
1776 if (!nvme_rdma_dev_get(ctrl->device))
1777 goto out_free_io_queues;
1779 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1780 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
1781 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
1782 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1783 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1784 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1785 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1786 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1787 ctrl->tag_set.driver_data = ctrl;
1788 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
1789 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1791 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1794 ctrl->ctrl.tagset = &ctrl->tag_set;
1796 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1797 if (IS_ERR(ctrl->ctrl.connect_q)) {
1798 ret = PTR_ERR(ctrl->ctrl.connect_q);
1799 goto out_free_tag_set;
1802 ret = nvme_rdma_connect_io_queues(ctrl);
1804 goto out_cleanup_connect_q;
1808 out_cleanup_connect_q:
1809 blk_cleanup_queue(ctrl->ctrl.connect_q);
1811 blk_mq_free_tag_set(&ctrl->tag_set);
1813 nvme_rdma_dev_put(ctrl->device);
1815 nvme_rdma_free_io_queues(ctrl);
1819 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1820 struct nvmf_ctrl_options *opts)
1822 struct nvme_rdma_ctrl *ctrl;
1827 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1829 return ERR_PTR(-ENOMEM);
1830 ctrl->ctrl.opts = opts;
1831 INIT_LIST_HEAD(&ctrl->list);
1833 if (opts->mask & NVMF_OPT_TRSVCID)
1834 port = opts->trsvcid;
1836 port = __stringify(NVME_RDMA_IP_PORT);
1838 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1839 opts->traddr, port, &ctrl->addr);
1841 pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
1845 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
1846 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1847 opts->host_traddr, NULL, &ctrl->src_addr);
1849 pr_err("malformed src address passed: %s\n",
1855 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1856 0 /* no quirks, we're perfect! */);
1860 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1861 nvme_rdma_reconnect_ctrl_work);
1862 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1863 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1864 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
1866 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1867 ctrl->ctrl.sqsize = opts->queue_size - 1;
1868 ctrl->ctrl.kato = opts->kato;
1871 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues),
1874 goto out_uninit_ctrl;
1876 ret = nvme_rdma_configure_admin_queue(ctrl);
1878 goto out_kfree_queues;
1880 /* sanity check icdoff */
1881 if (ctrl->ctrl.icdoff) {
1882 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1884 goto out_remove_admin_queue;
1887 /* sanity check keyed sgls */
1888 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1889 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1891 goto out_remove_admin_queue;
1894 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1895 /* warn if maxcmd is lower than queue_size */
1896 dev_warn(ctrl->ctrl.device,
1897 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1898 opts->queue_size, ctrl->ctrl.maxcmd);
1899 opts->queue_size = ctrl->ctrl.maxcmd;
1902 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1903 /* warn if sqsize is lower than queue_size */
1904 dev_warn(ctrl->ctrl.device,
1905 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1906 opts->queue_size, ctrl->ctrl.sqsize + 1);
1907 opts->queue_size = ctrl->ctrl.sqsize + 1;
1910 if (opts->nr_io_queues) {
1911 ret = nvme_rdma_create_io_queues(ctrl);
1913 goto out_remove_admin_queue;
1916 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1917 WARN_ON_ONCE(!changed);
1919 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
1920 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1922 kref_get(&ctrl->ctrl.kref);
1924 mutex_lock(&nvme_rdma_ctrl_mutex);
1925 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1926 mutex_unlock(&nvme_rdma_ctrl_mutex);
1928 if (opts->nr_io_queues) {
1929 nvme_queue_scan(&ctrl->ctrl);
1930 nvme_queue_async_events(&ctrl->ctrl);
1935 out_remove_admin_queue:
1936 nvme_stop_keep_alive(&ctrl->ctrl);
1937 nvme_rdma_destroy_admin_queue(ctrl);
1939 kfree(ctrl->queues);
1941 nvme_uninit_ctrl(&ctrl->ctrl);
1942 nvme_put_ctrl(&ctrl->ctrl);
1945 return ERR_PTR(ret);
1948 return ERR_PTR(ret);
1951 static struct nvmf_transport_ops nvme_rdma_transport = {
1953 .required_opts = NVMF_OPT_TRADDR,
1954 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
1955 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
1956 .create_ctrl = nvme_rdma_create_ctrl,
1959 static void nvme_rdma_add_one(struct ib_device *ib_device)
1963 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
1965 struct nvme_rdma_ctrl *ctrl;
1967 /* Delete all controllers using this device */
1968 mutex_lock(&nvme_rdma_ctrl_mutex);
1969 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
1970 if (ctrl->device->dev != ib_device)
1972 dev_info(ctrl->ctrl.device,
1973 "Removing ctrl: NQN \"%s\", addr %pISp\n",
1974 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1975 __nvme_rdma_del_ctrl(ctrl);
1977 mutex_unlock(&nvme_rdma_ctrl_mutex);
1979 flush_workqueue(nvme_wq);
1982 static struct ib_client nvme_rdma_ib_client = {
1983 .name = "nvme_rdma",
1984 .add = nvme_rdma_add_one,
1985 .remove = nvme_rdma_remove_one
1988 static int __init nvme_rdma_init_module(void)
1992 ret = ib_register_client(&nvme_rdma_ib_client);
1996 ret = nvmf_register_transport(&nvme_rdma_transport);
1998 goto err_unreg_client;
2003 ib_unregister_client(&nvme_rdma_ib_client);
2007 static void __exit nvme_rdma_cleanup_module(void)
2009 nvmf_unregister_transport(&nvme_rdma_transport);
2010 ib_unregister_client(&nvme_rdma_ib_client);
2013 module_init(nvme_rdma_init_module);
2014 module_exit(nvme_rdma_cleanup_module);
2016 MODULE_LICENSE("GPL v2");