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/delay.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/err.h>
20 #include <linux/string.h>
21 #include <linux/jiffies.h>
22 #include <linux/atomic.h>
23 #include <linux/blk-mq.h>
24 #include <linux/types.h>
25 #include <linux/list.h>
26 #include <linux/mutex.h>
27 #include <linux/scatterlist.h>
28 #include <linux/nvme.h>
29 #include <linux/t10-pi.h>
30 #include <asm/unaligned.h>
32 #include <rdma/ib_verbs.h>
33 #include <rdma/rdma_cm.h>
34 #include <rdma/ib_cm.h>
35 #include <linux/nvme-rdma.h>
41 #define NVME_RDMA_CONNECT_TIMEOUT_MS 1000 /* 1 second */
43 #define NVME_RDMA_MAX_SEGMENT_SIZE 0xffffff /* 24-bit SGL field */
45 #define NVME_RDMA_MAX_SEGMENTS 256
47 #define NVME_RDMA_MAX_INLINE_SEGMENTS 1
49 #define NVME_RDMA_MAX_PAGES_PER_MR 512
51 #define NVME_RDMA_DEF_RECONNECT_DELAY 20
54 * We handle AEN commands ourselves and don't even let the
55 * block layer know about them.
57 #define NVME_RDMA_NR_AEN_COMMANDS 1
58 #define NVME_RDMA_AQ_BLKMQ_DEPTH \
59 (NVMF_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
61 struct nvme_rdma_device {
62 struct ib_device *dev;
66 struct list_head entry;
75 struct nvme_rdma_queue;
76 struct nvme_rdma_request {
78 struct nvme_rdma_qe sqe;
79 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
84 struct ib_reg_wr reg_wr;
85 struct ib_cqe reg_cqe;
86 struct nvme_rdma_queue *queue;
87 struct sg_table sg_table;
88 struct scatterlist first_sgl[];
91 enum nvme_rdma_queue_flags {
92 NVME_RDMA_Q_CONNECTED = (1 << 0),
95 struct nvme_rdma_queue {
96 struct nvme_rdma_qe *rsp_ring;
99 size_t cmnd_capsule_len;
100 struct nvme_rdma_ctrl *ctrl;
101 struct nvme_rdma_device *device;
106 struct rdma_cm_id *cm_id;
108 struct completion cm_done;
111 struct nvme_rdma_ctrl {
112 /* read and written in the hot path */
115 /* read only in the hot path */
116 struct nvme_rdma_queue *queues;
119 /* other member variables */
120 struct blk_mq_tag_set tag_set;
121 struct work_struct delete_work;
122 struct work_struct reset_work;
123 struct work_struct err_work;
125 struct nvme_rdma_qe async_event_sqe;
128 struct delayed_work reconnect_work;
130 struct list_head list;
132 struct blk_mq_tag_set admin_tag_set;
133 struct nvme_rdma_device *device;
139 struct sockaddr addr;
140 struct sockaddr_in addr_in;
143 struct nvme_ctrl ctrl;
146 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
148 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
151 static LIST_HEAD(device_list);
152 static DEFINE_MUTEX(device_list_mutex);
154 static LIST_HEAD(nvme_rdma_ctrl_list);
155 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
157 static struct workqueue_struct *nvme_rdma_wq;
160 * Disabling this option makes small I/O goes faster, but is fundamentally
161 * unsafe. With it turned off we will have to register a global rkey that
162 * allows read and write access to all physical memory.
164 static bool register_always = true;
165 module_param(register_always, bool, 0444);
166 MODULE_PARM_DESC(register_always,
167 "Use memory registration even for contiguous memory regions");
169 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
170 struct rdma_cm_event *event);
171 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
173 /* XXX: really should move to a generic header sooner or later.. */
174 static inline void put_unaligned_le24(u32 val, u8 *p)
181 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
183 return queue - queue->ctrl->queues;
186 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
188 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
191 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
192 size_t capsule_size, enum dma_data_direction dir)
194 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
198 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
199 size_t capsule_size, enum dma_data_direction dir)
201 qe->data = kzalloc(capsule_size, GFP_KERNEL);
205 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
206 if (ib_dma_mapping_error(ibdev, qe->dma)) {
214 static void nvme_rdma_free_ring(struct ib_device *ibdev,
215 struct nvme_rdma_qe *ring, size_t ib_queue_size,
216 size_t capsule_size, enum dma_data_direction dir)
220 for (i = 0; i < ib_queue_size; i++)
221 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
225 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
226 size_t ib_queue_size, size_t capsule_size,
227 enum dma_data_direction dir)
229 struct nvme_rdma_qe *ring;
232 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
236 for (i = 0; i < ib_queue_size; i++) {
237 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
244 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
248 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
250 pr_debug("QP event %d\n", event->event);
253 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
255 wait_for_completion_interruptible_timeout(&queue->cm_done,
256 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
257 return queue->cm_error;
260 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
262 struct nvme_rdma_device *dev = queue->device;
263 struct ib_qp_init_attr init_attr;
266 memset(&init_attr, 0, sizeof(init_attr));
267 init_attr.event_handler = nvme_rdma_qp_event;
269 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
271 init_attr.cap.max_recv_wr = queue->queue_size + 1;
272 init_attr.cap.max_recv_sge = 1;
273 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
274 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
275 init_attr.qp_type = IB_QPT_RC;
276 init_attr.send_cq = queue->ib_cq;
277 init_attr.recv_cq = queue->ib_cq;
279 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
281 queue->qp = queue->cm_id->qp;
285 static int nvme_rdma_reinit_request(void *data, struct request *rq)
287 struct nvme_rdma_ctrl *ctrl = data;
288 struct nvme_rdma_device *dev = ctrl->device;
289 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
292 if (!req->need_inval)
295 ib_dereg_mr(req->mr);
297 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
299 if (IS_ERR(req->mr)) {
300 ret = PTR_ERR(req->mr);
304 req->need_inval = false;
310 static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl *ctrl,
311 struct request *rq, unsigned int queue_idx)
313 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
314 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
315 struct nvme_rdma_device *dev = queue->device;
318 ib_dereg_mr(req->mr);
320 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
324 static void nvme_rdma_exit_request(void *data, struct request *rq,
325 unsigned int hctx_idx, unsigned int rq_idx)
327 return __nvme_rdma_exit_request(data, rq, hctx_idx + 1);
330 static void nvme_rdma_exit_admin_request(void *data, struct request *rq,
331 unsigned int hctx_idx, unsigned int rq_idx)
333 return __nvme_rdma_exit_request(data, rq, 0);
336 static int __nvme_rdma_init_request(struct nvme_rdma_ctrl *ctrl,
337 struct request *rq, unsigned int queue_idx)
339 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
340 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
341 struct nvme_rdma_device *dev = queue->device;
342 struct ib_device *ibdev = dev->dev;
345 BUG_ON(queue_idx >= ctrl->queue_count);
347 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
352 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
354 if (IS_ERR(req->mr)) {
355 ret = PTR_ERR(req->mr);
364 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
369 static int nvme_rdma_init_request(void *data, struct request *rq,
370 unsigned int hctx_idx, unsigned int rq_idx,
371 unsigned int numa_node)
373 return __nvme_rdma_init_request(data, rq, hctx_idx + 1);
376 static int nvme_rdma_init_admin_request(void *data, struct request *rq,
377 unsigned int hctx_idx, unsigned int rq_idx,
378 unsigned int numa_node)
380 return __nvme_rdma_init_request(data, rq, 0);
383 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
384 unsigned int hctx_idx)
386 struct nvme_rdma_ctrl *ctrl = data;
387 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
389 BUG_ON(hctx_idx >= ctrl->queue_count);
391 hctx->driver_data = queue;
395 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
396 unsigned int hctx_idx)
398 struct nvme_rdma_ctrl *ctrl = data;
399 struct nvme_rdma_queue *queue = &ctrl->queues[0];
401 BUG_ON(hctx_idx != 0);
403 hctx->driver_data = queue;
407 static void nvme_rdma_free_dev(struct kref *ref)
409 struct nvme_rdma_device *ndev =
410 container_of(ref, struct nvme_rdma_device, ref);
412 mutex_lock(&device_list_mutex);
413 list_del(&ndev->entry);
414 mutex_unlock(&device_list_mutex);
416 if (!register_always)
417 ib_dereg_mr(ndev->mr);
418 ib_dealloc_pd(ndev->pd);
423 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
425 kref_put(&dev->ref, nvme_rdma_free_dev);
428 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
430 return kref_get_unless_zero(&dev->ref);
433 static struct nvme_rdma_device *
434 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
436 struct nvme_rdma_device *ndev;
438 mutex_lock(&device_list_mutex);
439 list_for_each_entry(ndev, &device_list, entry) {
440 if (ndev->dev->node_guid == cm_id->device->node_guid &&
441 nvme_rdma_dev_get(ndev))
445 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
449 ndev->dev = cm_id->device;
450 kref_init(&ndev->ref);
452 ndev->pd = ib_alloc_pd(ndev->dev);
453 if (IS_ERR(ndev->pd))
456 if (!register_always) {
457 ndev->mr = ib_get_dma_mr(ndev->pd,
458 IB_ACCESS_LOCAL_WRITE |
459 IB_ACCESS_REMOTE_READ |
460 IB_ACCESS_REMOTE_WRITE);
461 if (IS_ERR(ndev->mr))
465 if (!(ndev->dev->attrs.device_cap_flags &
466 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
467 dev_err(&ndev->dev->dev,
468 "Memory registrations not supported.\n");
472 list_add(&ndev->entry, &device_list);
474 mutex_unlock(&device_list_mutex);
478 if (!register_always)
479 ib_dereg_mr(ndev->mr);
481 ib_dealloc_pd(ndev->pd);
485 mutex_unlock(&device_list_mutex);
489 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
491 struct nvme_rdma_device *dev = queue->device;
492 struct ib_device *ibdev = dev->dev;
494 rdma_destroy_qp(queue->cm_id);
495 ib_free_cq(queue->ib_cq);
497 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
498 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
500 nvme_rdma_dev_put(dev);
503 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue,
504 struct nvme_rdma_device *dev)
506 struct ib_device *ibdev = dev->dev;
507 const int send_wr_factor = 3; /* MR, SEND, INV */
508 const int cq_factor = send_wr_factor + 1; /* + RECV */
509 int comp_vector, idx = nvme_rdma_queue_idx(queue);
516 * The admin queue is barely used once the controller is live, so don't
517 * bother to spread it out.
522 comp_vector = idx % ibdev->num_comp_vectors;
525 /* +1 for ib_stop_cq */
526 queue->ib_cq = ib_alloc_cq(dev->dev, queue,
527 cq_factor * queue->queue_size + 1, comp_vector,
529 if (IS_ERR(queue->ib_cq)) {
530 ret = PTR_ERR(queue->ib_cq);
534 ret = nvme_rdma_create_qp(queue, send_wr_factor);
536 goto out_destroy_ib_cq;
538 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
539 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
540 if (!queue->rsp_ring) {
548 ib_destroy_qp(queue->qp);
550 ib_free_cq(queue->ib_cq);
555 static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl,
556 int idx, size_t queue_size)
558 struct nvme_rdma_queue *queue;
561 queue = &ctrl->queues[idx];
563 init_completion(&queue->cm_done);
566 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
568 queue->cmnd_capsule_len = sizeof(struct nvme_command);
570 queue->queue_size = queue_size;
572 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
573 RDMA_PS_TCP, IB_QPT_RC);
574 if (IS_ERR(queue->cm_id)) {
575 dev_info(ctrl->ctrl.device,
576 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
577 return PTR_ERR(queue->cm_id);
580 queue->cm_error = -ETIMEDOUT;
581 ret = rdma_resolve_addr(queue->cm_id, NULL, &ctrl->addr,
582 NVME_RDMA_CONNECT_TIMEOUT_MS);
584 dev_info(ctrl->ctrl.device,
585 "rdma_resolve_addr failed (%d).\n", ret);
586 goto out_destroy_cm_id;
589 ret = nvme_rdma_wait_for_cm(queue);
591 dev_info(ctrl->ctrl.device,
592 "rdma_resolve_addr wait failed (%d).\n", ret);
593 goto out_destroy_cm_id;
596 set_bit(NVME_RDMA_Q_CONNECTED, &queue->flags);
601 rdma_destroy_id(queue->cm_id);
605 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
607 rdma_disconnect(queue->cm_id);
608 ib_drain_qp(queue->qp);
611 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
613 nvme_rdma_destroy_queue_ib(queue);
614 rdma_destroy_id(queue->cm_id);
617 static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue)
619 if (!test_and_clear_bit(NVME_RDMA_Q_CONNECTED, &queue->flags))
621 nvme_rdma_stop_queue(queue);
622 nvme_rdma_free_queue(queue);
625 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
629 for (i = 1; i < ctrl->queue_count; i++)
630 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
633 static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl)
637 for (i = 1; i < ctrl->queue_count; i++) {
638 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
646 static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl)
650 for (i = 1; i < ctrl->queue_count; i++) {
651 ret = nvme_rdma_init_queue(ctrl, i, ctrl->ctrl.sqsize);
653 dev_info(ctrl->ctrl.device,
654 "failed to initialize i/o queue: %d\n", ret);
655 goto out_free_queues;
663 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
668 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl)
670 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
671 sizeof(struct nvme_command), DMA_TO_DEVICE);
672 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
673 blk_cleanup_queue(ctrl->ctrl.admin_q);
674 blk_mq_free_tag_set(&ctrl->admin_tag_set);
675 nvme_rdma_dev_put(ctrl->device);
678 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
680 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
682 if (list_empty(&ctrl->list))
685 mutex_lock(&nvme_rdma_ctrl_mutex);
686 list_del(&ctrl->list);
687 mutex_unlock(&nvme_rdma_ctrl_mutex);
690 nvmf_free_options(nctrl->opts);
695 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
697 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
698 struct nvme_rdma_ctrl, reconnect_work);
702 if (ctrl->queue_count > 1) {
703 nvme_rdma_free_io_queues(ctrl);
705 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
710 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
712 ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set);
716 ret = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
720 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
722 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
726 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
730 nvme_start_keep_alive(&ctrl->ctrl);
732 if (ctrl->queue_count > 1) {
733 ret = nvme_rdma_init_io_queues(ctrl);
737 ret = nvme_rdma_connect_io_queues(ctrl);
742 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
743 WARN_ON_ONCE(!changed);
745 if (ctrl->queue_count > 1) {
746 nvme_start_queues(&ctrl->ctrl);
747 nvme_queue_scan(&ctrl->ctrl);
748 nvme_queue_async_events(&ctrl->ctrl);
751 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
756 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
758 /* Make sure we are not resetting/deleting */
759 if (ctrl->ctrl.state == NVME_CTRL_RECONNECTING) {
760 dev_info(ctrl->ctrl.device,
761 "Failed reconnect attempt, requeueing...\n");
762 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
763 ctrl->reconnect_delay * HZ);
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);
772 nvme_stop_keep_alive(&ctrl->ctrl);
773 if (ctrl->queue_count > 1)
774 nvme_stop_queues(&ctrl->ctrl);
775 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
777 /* We must take care of fastfail/requeue all our inflight requests */
778 if (ctrl->queue_count > 1)
779 blk_mq_tagset_busy_iter(&ctrl->tag_set,
780 nvme_cancel_request, &ctrl->ctrl);
781 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
782 nvme_cancel_request, &ctrl->ctrl);
784 dev_info(ctrl->ctrl.device, "reconnecting in %d seconds\n",
785 ctrl->reconnect_delay);
787 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
788 ctrl->reconnect_delay * HZ);
791 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
793 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
796 queue_work(nvme_rdma_wq, &ctrl->err_work);
799 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
802 struct nvme_rdma_queue *queue = cq->cq_context;
803 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
805 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
806 dev_info(ctrl->ctrl.device,
807 "%s for CQE 0x%p failed with status %s (%d)\n",
809 ib_wc_status_msg(wc->status), wc->status);
810 nvme_rdma_error_recovery(ctrl);
813 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
815 if (unlikely(wc->status != IB_WC_SUCCESS))
816 nvme_rdma_wr_error(cq, wc, "MEMREG");
819 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
821 if (unlikely(wc->status != IB_WC_SUCCESS))
822 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
825 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
826 struct nvme_rdma_request *req)
828 struct ib_send_wr *bad_wr;
829 struct ib_send_wr wr = {
830 .opcode = IB_WR_LOCAL_INV,
834 .ex.invalidate_rkey = req->mr->rkey,
837 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
838 wr.wr_cqe = &req->reg_cqe;
840 return ib_post_send(queue->qp, &wr, &bad_wr);
843 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
846 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
847 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
848 struct nvme_rdma_device *dev = queue->device;
849 struct ib_device *ibdev = dev->dev;
852 if (!blk_rq_bytes(rq))
855 if (req->need_inval) {
856 res = nvme_rdma_inv_rkey(queue, req);
858 dev_err(ctrl->ctrl.device,
859 "Queueing INV WR for rkey %#x failed (%d)\n",
861 nvme_rdma_error_recovery(queue->ctrl);
865 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
866 req->nents, rq_data_dir(rq) ==
867 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
869 nvme_cleanup_cmd(rq);
870 sg_free_table_chained(&req->sg_table, true);
873 static int nvme_rdma_set_sg_null(struct nvme_command *c)
875 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
878 put_unaligned_le24(0, sg->length);
879 put_unaligned_le32(0, sg->key);
880 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
884 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
885 struct nvme_rdma_request *req, struct nvme_command *c)
887 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
889 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
890 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
891 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
893 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
894 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
895 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
897 req->inline_data = true;
902 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
903 struct nvme_rdma_request *req, struct nvme_command *c)
905 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
907 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
908 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
909 put_unaligned_le32(queue->device->mr->rkey, sg->key);
910 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
914 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
915 struct nvme_rdma_request *req, struct nvme_command *c,
918 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
921 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE);
928 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
930 req->reg_cqe.done = nvme_rdma_memreg_done;
931 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
932 req->reg_wr.wr.opcode = IB_WR_REG_MR;
933 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
934 req->reg_wr.wr.num_sge = 0;
935 req->reg_wr.mr = req->mr;
936 req->reg_wr.key = req->mr->rkey;
937 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
938 IB_ACCESS_REMOTE_READ |
939 IB_ACCESS_REMOTE_WRITE;
941 req->need_inval = true;
943 sg->addr = cpu_to_le64(req->mr->iova);
944 put_unaligned_le24(req->mr->length, sg->length);
945 put_unaligned_le32(req->mr->rkey, sg->key);
946 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
947 NVME_SGL_FMT_INVALIDATE;
952 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
953 struct request *rq, unsigned int map_len,
954 struct nvme_command *c)
956 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
957 struct nvme_rdma_device *dev = queue->device;
958 struct ib_device *ibdev = dev->dev;
963 req->inline_data = false;
964 req->need_inval = false;
966 c->common.flags |= NVME_CMD_SGL_METABUF;
968 if (!blk_rq_bytes(rq))
969 return nvme_rdma_set_sg_null(c);
971 req->sg_table.sgl = req->first_sgl;
972 ret = sg_alloc_table_chained(&req->sg_table, rq->nr_phys_segments,
977 nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
978 BUG_ON(nents > rq->nr_phys_segments);
981 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, nents,
982 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
983 if (unlikely(count <= 0)) {
984 sg_free_table_chained(&req->sg_table, true);
989 if (rq_data_dir(rq) == WRITE &&
990 map_len <= nvme_rdma_inline_data_size(queue) &&
991 nvme_rdma_queue_idx(queue))
992 return nvme_rdma_map_sg_inline(queue, req, c);
994 if (!register_always)
995 return nvme_rdma_map_sg_single(queue, req, c);
998 return nvme_rdma_map_sg_fr(queue, req, c, count);
1001 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1003 if (unlikely(wc->status != IB_WC_SUCCESS))
1004 nvme_rdma_wr_error(cq, wc, "SEND");
1007 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1008 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1009 struct ib_send_wr *first, bool flush)
1011 struct ib_send_wr wr, *bad_wr;
1014 sge->addr = qe->dma;
1015 sge->length = sizeof(struct nvme_command),
1016 sge->lkey = queue->device->pd->local_dma_lkey;
1018 qe->cqe.done = nvme_rdma_send_done;
1021 wr.wr_cqe = &qe->cqe;
1023 wr.num_sge = num_sge;
1024 wr.opcode = IB_WR_SEND;
1028 * Unsignalled send completions are another giant desaster in the
1029 * IB Verbs spec: If we don't regularly post signalled sends
1030 * the send queue will fill up and only a QP reset will rescue us.
1031 * Would have been way to obvious to handle this in hardware or
1032 * at least the RDMA stack..
1034 * This messy and racy code sniplet is copy and pasted from the iSER
1035 * initiator, and the magic '32' comes from there as well.
1037 * Always signal the flushes. The magic request used for the flush
1038 * sequencer is not allocated in our driver's tagset and it's
1039 * triggered to be freed by blk_cleanup_queue(). So we need to
1040 * always mark it as signaled to ensure that the "wr_cqe", which is
1041 * embeded in request's payload, is not freed when __ib_process_cq()
1042 * calls wr_cqe->done().
1044 if ((++queue->sig_count % 32) == 0 || flush)
1045 wr.send_flags |= IB_SEND_SIGNALED;
1052 ret = ib_post_send(queue->qp, first, &bad_wr);
1054 dev_err(queue->ctrl->ctrl.device,
1055 "%s failed with error code %d\n", __func__, ret);
1060 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1061 struct nvme_rdma_qe *qe)
1063 struct ib_recv_wr wr, *bad_wr;
1067 list.addr = qe->dma;
1068 list.length = sizeof(struct nvme_completion);
1069 list.lkey = queue->device->pd->local_dma_lkey;
1071 qe->cqe.done = nvme_rdma_recv_done;
1074 wr.wr_cqe = &qe->cqe;
1078 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1080 dev_err(queue->ctrl->ctrl.device,
1081 "%s failed with error code %d\n", __func__, ret);
1086 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1088 u32 queue_idx = nvme_rdma_queue_idx(queue);
1091 return queue->ctrl->admin_tag_set.tags[queue_idx];
1092 return queue->ctrl->tag_set.tags[queue_idx - 1];
1095 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1097 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1098 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1099 struct ib_device *dev = queue->device->dev;
1100 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1101 struct nvme_command *cmd = sqe->data;
1105 if (WARN_ON_ONCE(aer_idx != 0))
1108 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1110 memset(cmd, 0, sizeof(*cmd));
1111 cmd->common.opcode = nvme_admin_async_event;
1112 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1113 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1114 nvme_rdma_set_sg_null(cmd);
1116 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1119 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1123 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1124 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1126 u16 status = le16_to_cpu(cqe->status);
1128 struct nvme_rdma_request *req;
1133 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1135 dev_err(queue->ctrl->ctrl.device,
1136 "tag 0x%x on QP %#x not found\n",
1137 cqe->command_id, queue->qp->qp_num);
1138 nvme_rdma_error_recovery(queue->ctrl);
1141 req = blk_mq_rq_to_pdu(rq);
1143 if (rq->cmd_type == REQ_TYPE_DRV_PRIV && rq->special)
1144 memcpy(rq->special, cqe, sizeof(*cqe));
1149 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1150 wc->ex.invalidate_rkey == req->mr->rkey)
1151 req->need_inval = false;
1153 blk_mq_complete_request(rq, status);
1158 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1160 struct nvme_rdma_qe *qe =
1161 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1162 struct nvme_rdma_queue *queue = cq->cq_context;
1163 struct ib_device *ibdev = queue->device->dev;
1164 struct nvme_completion *cqe = qe->data;
1165 const size_t len = sizeof(struct nvme_completion);
1168 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1169 nvme_rdma_wr_error(cq, wc, "RECV");
1173 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1175 * AEN requests are special as they don't time out and can
1176 * survive any kind of queue freeze and often don't respond to
1177 * aborts. We don't even bother to allocate a struct request
1178 * for them but rather special case them here.
1180 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1181 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
1182 nvme_complete_async_event(&queue->ctrl->ctrl, cqe);
1184 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1185 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1187 nvme_rdma_post_recv(queue, qe);
1191 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1193 __nvme_rdma_recv_done(cq, wc, -1);
1196 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1200 for (i = 0; i < queue->queue_size; i++) {
1201 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1203 goto out_destroy_queue_ib;
1208 out_destroy_queue_ib:
1209 nvme_rdma_destroy_queue_ib(queue);
1213 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1214 struct rdma_cm_event *ev)
1216 if (ev->param.conn.private_data_len) {
1217 struct nvme_rdma_cm_rej *rej =
1218 (struct nvme_rdma_cm_rej *)ev->param.conn.private_data;
1220 dev_err(queue->ctrl->ctrl.device,
1221 "Connect rejected, status %d.", le16_to_cpu(rej->sts));
1222 /* XXX: Think of something clever to do here... */
1224 dev_err(queue->ctrl->ctrl.device,
1225 "Connect rejected, no private data.\n");
1231 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1233 struct nvme_rdma_device *dev;
1236 dev = nvme_rdma_find_get_device(queue->cm_id);
1238 dev_err(queue->cm_id->device->dma_device,
1239 "no client data found!\n");
1240 return -ECONNREFUSED;
1243 ret = nvme_rdma_create_queue_ib(queue, dev);
1245 nvme_rdma_dev_put(dev);
1249 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1251 dev_err(queue->ctrl->ctrl.device,
1252 "rdma_resolve_route failed (%d).\n",
1254 goto out_destroy_queue;
1260 nvme_rdma_destroy_queue_ib(queue);
1265 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1267 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1268 struct rdma_conn_param param = { };
1269 struct nvme_rdma_cm_req priv = { };
1272 param.qp_num = queue->qp->qp_num;
1273 param.flow_control = 1;
1275 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1276 /* maximum retry count */
1277 param.retry_count = 7;
1278 param.rnr_retry_count = 7;
1279 param.private_data = &priv;
1280 param.private_data_len = sizeof(priv);
1282 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1283 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1284 priv.hrqsize = cpu_to_le16(queue->queue_size);
1285 priv.hsqsize = cpu_to_le16(queue->queue_size);
1287 ret = rdma_connect(queue->cm_id, ¶m);
1289 dev_err(ctrl->ctrl.device,
1290 "rdma_connect failed (%d).\n", ret);
1291 goto out_destroy_queue_ib;
1296 out_destroy_queue_ib:
1297 nvme_rdma_destroy_queue_ib(queue);
1302 * nvme_rdma_device_unplug() - Handle RDMA device unplug
1303 * @queue: Queue that owns the cm_id that caught the event
1305 * DEVICE_REMOVAL event notifies us that the RDMA device is about
1306 * to unplug so we should take care of destroying our RDMA resources.
1307 * This event will be generated for each allocated cm_id.
1309 * In our case, the RDMA resources are managed per controller and not
1310 * only per queue. So the way we handle this is we trigger an implicit
1311 * controller deletion upon the first DEVICE_REMOVAL event we see, and
1312 * hold the event inflight until the controller deletion is completed.
1314 * One exception that we need to handle is the destruction of the cm_id
1315 * that caught the event. Since we hold the callout until the controller
1316 * deletion is completed, we'll deadlock if the controller deletion will
1317 * call rdma_destroy_id on this queue's cm_id. Thus, we claim ownership
1318 * of destroying this queue before-hand, destroy the queue resources,
1319 * then queue the controller deletion which won't destroy this queue and
1320 * we destroy the cm_id implicitely by returning a non-zero rc to the callout.
1322 static int nvme_rdma_device_unplug(struct nvme_rdma_queue *queue)
1324 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1327 /* Own the controller deletion */
1328 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1331 dev_warn(ctrl->ctrl.device,
1332 "Got rdma device removal event, deleting ctrl\n");
1334 /* Get rid of reconnect work if its running */
1335 cancel_delayed_work_sync(&ctrl->reconnect_work);
1337 /* Disable the queue so ctrl delete won't free it */
1338 if (test_and_clear_bit(NVME_RDMA_Q_CONNECTED, &queue->flags)) {
1339 /* Free this queue ourselves */
1340 nvme_rdma_stop_queue(queue);
1341 nvme_rdma_destroy_queue_ib(queue);
1343 /* Return non-zero so the cm_id will destroy implicitly */
1347 /* Queue controller deletion */
1348 queue_work(nvme_rdma_wq, &ctrl->delete_work);
1349 flush_work(&ctrl->delete_work);
1353 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1354 struct rdma_cm_event *ev)
1356 struct nvme_rdma_queue *queue = cm_id->context;
1359 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1360 rdma_event_msg(ev->event), ev->event,
1363 switch (ev->event) {
1364 case RDMA_CM_EVENT_ADDR_RESOLVED:
1365 cm_error = nvme_rdma_addr_resolved(queue);
1367 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1368 cm_error = nvme_rdma_route_resolved(queue);
1370 case RDMA_CM_EVENT_ESTABLISHED:
1371 queue->cm_error = nvme_rdma_conn_established(queue);
1372 /* complete cm_done regardless of success/failure */
1373 complete(&queue->cm_done);
1375 case RDMA_CM_EVENT_REJECTED:
1376 cm_error = nvme_rdma_conn_rejected(queue, ev);
1378 case RDMA_CM_EVENT_ADDR_ERROR:
1379 case RDMA_CM_EVENT_ROUTE_ERROR:
1380 case RDMA_CM_EVENT_CONNECT_ERROR:
1381 case RDMA_CM_EVENT_UNREACHABLE:
1382 dev_dbg(queue->ctrl->ctrl.device,
1383 "CM error event %d\n", ev->event);
1384 cm_error = -ECONNRESET;
1386 case RDMA_CM_EVENT_DISCONNECTED:
1387 case RDMA_CM_EVENT_ADDR_CHANGE:
1388 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1389 dev_dbg(queue->ctrl->ctrl.device,
1390 "disconnect received - connection closed\n");
1391 nvme_rdma_error_recovery(queue->ctrl);
1393 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1394 /* return 1 means impliciy CM ID destroy */
1395 return nvme_rdma_device_unplug(queue);
1397 dev_err(queue->ctrl->ctrl.device,
1398 "Unexpected RDMA CM event (%d)\n", ev->event);
1399 nvme_rdma_error_recovery(queue->ctrl);
1404 queue->cm_error = cm_error;
1405 complete(&queue->cm_done);
1411 static enum blk_eh_timer_return
1412 nvme_rdma_timeout(struct request *rq, bool reserved)
1414 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1416 /* queue error recovery */
1417 nvme_rdma_error_recovery(req->queue->ctrl);
1419 /* fail with DNR on cmd timeout */
1420 rq->errors = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1422 return BLK_EH_HANDLED;
1425 static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1426 const struct blk_mq_queue_data *bd)
1428 struct nvme_ns *ns = hctx->queue->queuedata;
1429 struct nvme_rdma_queue *queue = hctx->driver_data;
1430 struct request *rq = bd->rq;
1431 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1432 struct nvme_rdma_qe *sqe = &req->sqe;
1433 struct nvme_command *c = sqe->data;
1435 struct ib_device *dev;
1436 unsigned int map_len;
1439 WARN_ON_ONCE(rq->tag < 0);
1441 dev = queue->device->dev;
1442 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1443 sizeof(struct nvme_command), DMA_TO_DEVICE);
1445 ret = nvme_setup_cmd(ns, rq, c);
1449 c->common.command_id = rq->tag;
1450 blk_mq_start_request(rq);
1452 map_len = nvme_map_len(rq);
1453 ret = nvme_rdma_map_data(queue, rq, map_len, c);
1455 dev_err(queue->ctrl->ctrl.device,
1456 "Failed to map data (%d)\n", ret);
1457 nvme_cleanup_cmd(rq);
1461 ib_dma_sync_single_for_device(dev, sqe->dma,
1462 sizeof(struct nvme_command), DMA_TO_DEVICE);
1464 if (rq->cmd_type == REQ_TYPE_FS && req_op(rq) == REQ_OP_FLUSH)
1466 ret = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1467 req->need_inval ? &req->reg_wr.wr : NULL, flush);
1469 nvme_rdma_unmap_data(queue, rq);
1473 return BLK_MQ_RQ_QUEUE_OK;
1475 return (ret == -ENOMEM || ret == -EAGAIN) ?
1476 BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR;
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 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1487 while (ib_poll_cq(cq, 1, &wc) > 0) {
1488 struct ib_cqe *cqe = wc.wr_cqe;
1491 if (cqe->done == nvme_rdma_recv_done)
1492 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1501 static void nvme_rdma_complete_rq(struct request *rq)
1503 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1504 struct nvme_rdma_queue *queue = req->queue;
1507 nvme_rdma_unmap_data(queue, rq);
1509 if (unlikely(rq->errors)) {
1510 if (nvme_req_needs_retry(rq, rq->errors)) {
1511 nvme_requeue_req(rq);
1515 if (rq->cmd_type == REQ_TYPE_DRV_PRIV)
1518 error = nvme_error_status(rq->errors);
1521 blk_mq_end_request(rq, error);
1524 static struct blk_mq_ops nvme_rdma_mq_ops = {
1525 .queue_rq = nvme_rdma_queue_rq,
1526 .complete = nvme_rdma_complete_rq,
1527 .map_queue = blk_mq_map_queue,
1528 .init_request = nvme_rdma_init_request,
1529 .exit_request = nvme_rdma_exit_request,
1530 .reinit_request = nvme_rdma_reinit_request,
1531 .init_hctx = nvme_rdma_init_hctx,
1532 .poll = nvme_rdma_poll,
1533 .timeout = nvme_rdma_timeout,
1536 static struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1537 .queue_rq = nvme_rdma_queue_rq,
1538 .complete = nvme_rdma_complete_rq,
1539 .map_queue = blk_mq_map_queue,
1540 .init_request = nvme_rdma_init_admin_request,
1541 .exit_request = nvme_rdma_exit_admin_request,
1542 .reinit_request = nvme_rdma_reinit_request,
1543 .init_hctx = nvme_rdma_init_admin_hctx,
1544 .timeout = nvme_rdma_timeout,
1547 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
1551 error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
1555 ctrl->device = ctrl->queues[0].device;
1558 * We need a reference on the device as long as the tag_set is alive,
1559 * as the MRs in the request structures need a valid ib_device.
1562 if (!nvme_rdma_dev_get(ctrl->device))
1563 goto out_free_queue;
1565 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
1566 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
1568 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1569 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
1570 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
1571 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
1572 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1573 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1574 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1575 ctrl->admin_tag_set.driver_data = ctrl;
1576 ctrl->admin_tag_set.nr_hw_queues = 1;
1577 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1579 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1583 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1584 if (IS_ERR(ctrl->ctrl.admin_q)) {
1585 error = PTR_ERR(ctrl->ctrl.admin_q);
1586 goto out_free_tagset;
1589 error = nvmf_connect_admin_queue(&ctrl->ctrl);
1591 goto out_cleanup_queue;
1593 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
1595 dev_err(ctrl->ctrl.device,
1596 "prop_get NVME_REG_CAP failed\n");
1597 goto out_cleanup_queue;
1601 min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize);
1603 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
1605 goto out_cleanup_queue;
1607 ctrl->ctrl.max_hw_sectors =
1608 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
1610 error = nvme_init_identify(&ctrl->ctrl);
1612 goto out_cleanup_queue;
1614 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
1615 &ctrl->async_event_sqe, sizeof(struct nvme_command),
1618 goto out_cleanup_queue;
1620 nvme_start_keep_alive(&ctrl->ctrl);
1625 blk_cleanup_queue(ctrl->ctrl.admin_q);
1627 /* disconnect and drain the queue before freeing the tagset */
1628 nvme_rdma_stop_queue(&ctrl->queues[0]);
1629 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1631 nvme_rdma_dev_put(ctrl->device);
1633 nvme_rdma_free_queue(&ctrl->queues[0]);
1637 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1639 nvme_stop_keep_alive(&ctrl->ctrl);
1640 cancel_work_sync(&ctrl->err_work);
1641 cancel_delayed_work_sync(&ctrl->reconnect_work);
1643 if (ctrl->queue_count > 1) {
1644 nvme_stop_queues(&ctrl->ctrl);
1645 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1646 nvme_cancel_request, &ctrl->ctrl);
1647 nvme_rdma_free_io_queues(ctrl);
1650 if (test_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[0].flags))
1651 nvme_shutdown_ctrl(&ctrl->ctrl);
1653 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
1654 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1655 nvme_cancel_request, &ctrl->ctrl);
1656 nvme_rdma_destroy_admin_queue(ctrl);
1659 static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1661 nvme_uninit_ctrl(&ctrl->ctrl);
1663 nvme_rdma_shutdown_ctrl(ctrl);
1665 if (ctrl->ctrl.tagset) {
1666 blk_cleanup_queue(ctrl->ctrl.connect_q);
1667 blk_mq_free_tag_set(&ctrl->tag_set);
1668 nvme_rdma_dev_put(ctrl->device);
1671 nvme_put_ctrl(&ctrl->ctrl);
1674 static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1676 struct nvme_rdma_ctrl *ctrl = container_of(work,
1677 struct nvme_rdma_ctrl, delete_work);
1679 __nvme_rdma_remove_ctrl(ctrl, true);
1682 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1684 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1687 if (!queue_work(nvme_rdma_wq, &ctrl->delete_work))
1693 static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1695 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1698 ret = __nvme_rdma_del_ctrl(ctrl);
1702 flush_work(&ctrl->delete_work);
1707 static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1709 struct nvme_rdma_ctrl *ctrl = container_of(work,
1710 struct nvme_rdma_ctrl, delete_work);
1712 __nvme_rdma_remove_ctrl(ctrl, false);
1715 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1717 struct nvme_rdma_ctrl *ctrl = container_of(work,
1718 struct nvme_rdma_ctrl, reset_work);
1722 nvme_rdma_shutdown_ctrl(ctrl);
1724 ret = nvme_rdma_configure_admin_queue(ctrl);
1726 /* ctrl is already shutdown, just remove the ctrl */
1727 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1731 if (ctrl->queue_count > 1) {
1732 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
1736 ret = nvme_rdma_init_io_queues(ctrl);
1740 ret = nvme_rdma_connect_io_queues(ctrl);
1745 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1746 WARN_ON_ONCE(!changed);
1748 if (ctrl->queue_count > 1) {
1749 nvme_start_queues(&ctrl->ctrl);
1750 nvme_queue_scan(&ctrl->ctrl);
1751 nvme_queue_async_events(&ctrl->ctrl);
1757 /* Deleting this dead controller... */
1758 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1759 WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work));
1762 static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl)
1764 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1766 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1769 if (!queue_work(nvme_rdma_wq, &ctrl->reset_work))
1772 flush_work(&ctrl->reset_work);
1777 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1779 .module = THIS_MODULE,
1781 .reg_read32 = nvmf_reg_read32,
1782 .reg_read64 = nvmf_reg_read64,
1783 .reg_write32 = nvmf_reg_write32,
1784 .reset_ctrl = nvme_rdma_reset_ctrl,
1785 .free_ctrl = nvme_rdma_free_ctrl,
1786 .submit_async_event = nvme_rdma_submit_async_event,
1787 .delete_ctrl = nvme_rdma_del_ctrl,
1788 .get_subsysnqn = nvmf_get_subsysnqn,
1789 .get_address = nvmf_get_address,
1792 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1794 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
1797 ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues);
1801 ctrl->queue_count = opts->nr_io_queues + 1;
1802 if (ctrl->queue_count < 2)
1805 dev_info(ctrl->ctrl.device,
1806 "creating %d I/O queues.\n", opts->nr_io_queues);
1808 ret = nvme_rdma_init_io_queues(ctrl);
1813 * We need a reference on the device as long as the tag_set is alive,
1814 * as the MRs in the request structures need a valid ib_device.
1817 if (!nvme_rdma_dev_get(ctrl->device))
1818 goto out_free_io_queues;
1820 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1821 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
1822 ctrl->tag_set.queue_depth = ctrl->ctrl.sqsize;
1823 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1824 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1825 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1826 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1827 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1828 ctrl->tag_set.driver_data = ctrl;
1829 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
1830 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1832 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1835 ctrl->ctrl.tagset = &ctrl->tag_set;
1837 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1838 if (IS_ERR(ctrl->ctrl.connect_q)) {
1839 ret = PTR_ERR(ctrl->ctrl.connect_q);
1840 goto out_free_tag_set;
1843 ret = nvme_rdma_connect_io_queues(ctrl);
1845 goto out_cleanup_connect_q;
1849 out_cleanup_connect_q:
1850 blk_cleanup_queue(ctrl->ctrl.connect_q);
1852 blk_mq_free_tag_set(&ctrl->tag_set);
1854 nvme_rdma_dev_put(ctrl->device);
1856 nvme_rdma_free_io_queues(ctrl);
1860 static int nvme_rdma_parse_ipaddr(struct sockaddr_in *in_addr, char *p)
1862 u8 *addr = (u8 *)&in_addr->sin_addr.s_addr;
1863 size_t buflen = strlen(p);
1865 /* XXX: handle IPv6 addresses */
1867 if (buflen > INET_ADDRSTRLEN)
1869 if (in4_pton(p, buflen, addr, '\0', NULL) == 0)
1871 in_addr->sin_family = AF_INET;
1875 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1876 struct nvmf_ctrl_options *opts)
1878 struct nvme_rdma_ctrl *ctrl;
1882 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1884 return ERR_PTR(-ENOMEM);
1885 ctrl->ctrl.opts = opts;
1886 INIT_LIST_HEAD(&ctrl->list);
1888 ret = nvme_rdma_parse_ipaddr(&ctrl->addr_in, opts->traddr);
1890 pr_err("malformed IP address passed: %s\n", opts->traddr);
1894 if (opts->mask & NVMF_OPT_TRSVCID) {
1897 ret = kstrtou16(opts->trsvcid, 0, &port);
1901 ctrl->addr_in.sin_port = cpu_to_be16(port);
1903 ctrl->addr_in.sin_port = cpu_to_be16(NVME_RDMA_IP_PORT);
1906 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1907 0 /* no quirks, we're perfect! */);
1911 ctrl->reconnect_delay = opts->reconnect_delay;
1912 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1913 nvme_rdma_reconnect_ctrl_work);
1914 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1915 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1916 INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work);
1917 spin_lock_init(&ctrl->lock);
1919 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1920 ctrl->ctrl.sqsize = opts->queue_size;
1921 ctrl->ctrl.kato = opts->kato;
1924 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues),
1927 goto out_uninit_ctrl;
1929 ret = nvme_rdma_configure_admin_queue(ctrl);
1931 goto out_kfree_queues;
1933 /* sanity check icdoff */
1934 if (ctrl->ctrl.icdoff) {
1935 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1936 goto out_remove_admin_queue;
1939 /* sanity check keyed sgls */
1940 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1941 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1942 goto out_remove_admin_queue;
1945 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1946 /* warn if maxcmd is lower than queue_size */
1947 dev_warn(ctrl->ctrl.device,
1948 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1949 opts->queue_size, ctrl->ctrl.maxcmd);
1950 opts->queue_size = ctrl->ctrl.maxcmd;
1953 if (opts->nr_io_queues) {
1954 ret = nvme_rdma_create_io_queues(ctrl);
1956 goto out_remove_admin_queue;
1959 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1960 WARN_ON_ONCE(!changed);
1962 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
1963 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1965 kref_get(&ctrl->ctrl.kref);
1967 mutex_lock(&nvme_rdma_ctrl_mutex);
1968 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1969 mutex_unlock(&nvme_rdma_ctrl_mutex);
1971 if (opts->nr_io_queues) {
1972 nvme_queue_scan(&ctrl->ctrl);
1973 nvme_queue_async_events(&ctrl->ctrl);
1978 out_remove_admin_queue:
1979 nvme_stop_keep_alive(&ctrl->ctrl);
1980 nvme_rdma_destroy_admin_queue(ctrl);
1982 kfree(ctrl->queues);
1984 nvme_uninit_ctrl(&ctrl->ctrl);
1985 nvme_put_ctrl(&ctrl->ctrl);
1988 return ERR_PTR(ret);
1991 return ERR_PTR(ret);
1994 static struct nvmf_transport_ops nvme_rdma_transport = {
1996 .required_opts = NVMF_OPT_TRADDR,
1997 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY,
1998 .create_ctrl = nvme_rdma_create_ctrl,
2001 static int __init nvme_rdma_init_module(void)
2003 nvme_rdma_wq = create_workqueue("nvme_rdma_wq");
2007 nvmf_register_transport(&nvme_rdma_transport);
2011 static void __exit nvme_rdma_cleanup_module(void)
2013 struct nvme_rdma_ctrl *ctrl;
2015 nvmf_unregister_transport(&nvme_rdma_transport);
2017 mutex_lock(&nvme_rdma_ctrl_mutex);
2018 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list)
2019 __nvme_rdma_del_ctrl(ctrl);
2020 mutex_unlock(&nvme_rdma_ctrl_mutex);
2022 destroy_workqueue(nvme_rdma_wq);
2025 module_init(nvme_rdma_init_module);
2026 module_exit(nvme_rdma_cleanup_module);
2028 MODULE_LICENSE("GPL v2");