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);
172 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl);
174 /* XXX: really should move to a generic header sooner or later.. */
175 static inline void put_unaligned_le24(u32 val, u8 *p)
182 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
184 return queue - queue->ctrl->queues;
187 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
189 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
192 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
193 size_t capsule_size, enum dma_data_direction dir)
195 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
199 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
200 size_t capsule_size, enum dma_data_direction dir)
202 qe->data = kzalloc(capsule_size, GFP_KERNEL);
206 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
207 if (ib_dma_mapping_error(ibdev, qe->dma)) {
215 static void nvme_rdma_free_ring(struct ib_device *ibdev,
216 struct nvme_rdma_qe *ring, size_t ib_queue_size,
217 size_t capsule_size, enum dma_data_direction dir)
221 for (i = 0; i < ib_queue_size; i++)
222 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
226 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
227 size_t ib_queue_size, size_t capsule_size,
228 enum dma_data_direction dir)
230 struct nvme_rdma_qe *ring;
233 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
237 for (i = 0; i < ib_queue_size; i++) {
238 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
245 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
249 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
251 pr_debug("QP event %d\n", event->event);
254 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
256 wait_for_completion_interruptible_timeout(&queue->cm_done,
257 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
258 return queue->cm_error;
261 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
263 struct nvme_rdma_device *dev = queue->device;
264 struct ib_qp_init_attr init_attr;
267 memset(&init_attr, 0, sizeof(init_attr));
268 init_attr.event_handler = nvme_rdma_qp_event;
270 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
272 init_attr.cap.max_recv_wr = queue->queue_size + 1;
273 init_attr.cap.max_recv_sge = 1;
274 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
275 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
276 init_attr.qp_type = IB_QPT_RC;
277 init_attr.send_cq = queue->ib_cq;
278 init_attr.recv_cq = queue->ib_cq;
280 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
282 queue->qp = queue->cm_id->qp;
286 static int nvme_rdma_reinit_request(void *data, struct request *rq)
288 struct nvme_rdma_ctrl *ctrl = data;
289 struct nvme_rdma_device *dev = ctrl->device;
290 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
293 if (!req->need_inval)
296 ib_dereg_mr(req->mr);
298 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
300 if (IS_ERR(req->mr)) {
301 ret = PTR_ERR(req->mr);
305 req->need_inval = false;
311 static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl *ctrl,
312 struct request *rq, unsigned int queue_idx)
314 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
315 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
316 struct nvme_rdma_device *dev = queue->device;
319 ib_dereg_mr(req->mr);
321 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
325 static void nvme_rdma_exit_request(void *data, struct request *rq,
326 unsigned int hctx_idx, unsigned int rq_idx)
328 return __nvme_rdma_exit_request(data, rq, hctx_idx + 1);
331 static void nvme_rdma_exit_admin_request(void *data, struct request *rq,
332 unsigned int hctx_idx, unsigned int rq_idx)
334 return __nvme_rdma_exit_request(data, rq, 0);
337 static int __nvme_rdma_init_request(struct nvme_rdma_ctrl *ctrl,
338 struct request *rq, unsigned int queue_idx)
340 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
341 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
342 struct nvme_rdma_device *dev = queue->device;
343 struct ib_device *ibdev = dev->dev;
346 BUG_ON(queue_idx >= ctrl->queue_count);
348 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
353 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
355 if (IS_ERR(req->mr)) {
356 ret = PTR_ERR(req->mr);
365 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
370 static int nvme_rdma_init_request(void *data, struct request *rq,
371 unsigned int hctx_idx, unsigned int rq_idx,
372 unsigned int numa_node)
374 return __nvme_rdma_init_request(data, rq, hctx_idx + 1);
377 static int nvme_rdma_init_admin_request(void *data, struct request *rq,
378 unsigned int hctx_idx, unsigned int rq_idx,
379 unsigned int numa_node)
381 return __nvme_rdma_init_request(data, rq, 0);
384 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
385 unsigned int hctx_idx)
387 struct nvme_rdma_ctrl *ctrl = data;
388 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
390 BUG_ON(hctx_idx >= ctrl->queue_count);
392 hctx->driver_data = queue;
396 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
397 unsigned int hctx_idx)
399 struct nvme_rdma_ctrl *ctrl = data;
400 struct nvme_rdma_queue *queue = &ctrl->queues[0];
402 BUG_ON(hctx_idx != 0);
404 hctx->driver_data = queue;
408 static void nvme_rdma_free_dev(struct kref *ref)
410 struct nvme_rdma_device *ndev =
411 container_of(ref, struct nvme_rdma_device, ref);
413 mutex_lock(&device_list_mutex);
414 list_del(&ndev->entry);
415 mutex_unlock(&device_list_mutex);
417 if (!register_always)
418 ib_dereg_mr(ndev->mr);
419 ib_dealloc_pd(ndev->pd);
424 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
426 kref_put(&dev->ref, nvme_rdma_free_dev);
429 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
431 return kref_get_unless_zero(&dev->ref);
434 static struct nvme_rdma_device *
435 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
437 struct nvme_rdma_device *ndev;
439 mutex_lock(&device_list_mutex);
440 list_for_each_entry(ndev, &device_list, entry) {
441 if (ndev->dev->node_guid == cm_id->device->node_guid &&
442 nvme_rdma_dev_get(ndev))
446 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
450 ndev->dev = cm_id->device;
451 kref_init(&ndev->ref);
453 ndev->pd = ib_alloc_pd(ndev->dev);
454 if (IS_ERR(ndev->pd))
457 if (!register_always) {
458 ndev->mr = ib_get_dma_mr(ndev->pd,
459 IB_ACCESS_LOCAL_WRITE |
460 IB_ACCESS_REMOTE_READ |
461 IB_ACCESS_REMOTE_WRITE);
462 if (IS_ERR(ndev->mr))
466 if (!(ndev->dev->attrs.device_cap_flags &
467 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
468 dev_err(&ndev->dev->dev,
469 "Memory registrations not supported.\n");
473 list_add(&ndev->entry, &device_list);
475 mutex_unlock(&device_list_mutex);
479 if (!register_always)
480 ib_dereg_mr(ndev->mr);
482 ib_dealloc_pd(ndev->pd);
486 mutex_unlock(&device_list_mutex);
490 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
492 struct nvme_rdma_device *dev = queue->device;
493 struct ib_device *ibdev = dev->dev;
495 rdma_destroy_qp(queue->cm_id);
496 ib_free_cq(queue->ib_cq);
498 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
499 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
501 nvme_rdma_dev_put(dev);
504 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue,
505 struct nvme_rdma_device *dev)
507 struct ib_device *ibdev = dev->dev;
508 const int send_wr_factor = 3; /* MR, SEND, INV */
509 const int cq_factor = send_wr_factor + 1; /* + RECV */
510 int comp_vector, idx = nvme_rdma_queue_idx(queue);
517 * The admin queue is barely used once the controller is live, so don't
518 * bother to spread it out.
523 comp_vector = idx % ibdev->num_comp_vectors;
526 /* +1 for ib_stop_cq */
527 queue->ib_cq = ib_alloc_cq(dev->dev, queue,
528 cq_factor * queue->queue_size + 1, comp_vector,
530 if (IS_ERR(queue->ib_cq)) {
531 ret = PTR_ERR(queue->ib_cq);
535 ret = nvme_rdma_create_qp(queue, send_wr_factor);
537 goto out_destroy_ib_cq;
539 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
540 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
541 if (!queue->rsp_ring) {
549 ib_destroy_qp(queue->qp);
551 ib_free_cq(queue->ib_cq);
556 static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl,
557 int idx, size_t queue_size)
559 struct nvme_rdma_queue *queue;
562 queue = &ctrl->queues[idx];
564 init_completion(&queue->cm_done);
567 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
569 queue->cmnd_capsule_len = sizeof(struct nvme_command);
571 queue->queue_size = queue_size;
573 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
574 RDMA_PS_TCP, IB_QPT_RC);
575 if (IS_ERR(queue->cm_id)) {
576 dev_info(ctrl->ctrl.device,
577 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
578 return PTR_ERR(queue->cm_id);
581 queue->cm_error = -ETIMEDOUT;
582 ret = rdma_resolve_addr(queue->cm_id, NULL, &ctrl->addr,
583 NVME_RDMA_CONNECT_TIMEOUT_MS);
585 dev_info(ctrl->ctrl.device,
586 "rdma_resolve_addr failed (%d).\n", ret);
587 goto out_destroy_cm_id;
590 ret = nvme_rdma_wait_for_cm(queue);
592 dev_info(ctrl->ctrl.device,
593 "rdma_resolve_addr wait failed (%d).\n", ret);
594 goto out_destroy_cm_id;
597 set_bit(NVME_RDMA_Q_CONNECTED, &queue->flags);
602 rdma_destroy_id(queue->cm_id);
606 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
608 rdma_disconnect(queue->cm_id);
609 ib_drain_qp(queue->qp);
612 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
614 nvme_rdma_destroy_queue_ib(queue);
615 rdma_destroy_id(queue->cm_id);
618 static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue)
620 if (!test_and_clear_bit(NVME_RDMA_Q_CONNECTED, &queue->flags))
622 nvme_rdma_stop_queue(queue);
623 nvme_rdma_free_queue(queue);
626 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
630 for (i = 1; i < ctrl->queue_count; i++)
631 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
634 static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl)
638 for (i = 1; i < ctrl->queue_count; i++) {
639 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
647 static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl)
651 for (i = 1; i < ctrl->queue_count; i++) {
652 ret = nvme_rdma_init_queue(ctrl, i, ctrl->ctrl.sqsize);
654 dev_info(ctrl->ctrl.device,
655 "failed to initialize i/o queue: %d\n", ret);
656 goto out_free_queues;
664 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
669 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl)
671 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
672 sizeof(struct nvme_command), DMA_TO_DEVICE);
673 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
674 blk_cleanup_queue(ctrl->ctrl.admin_q);
675 blk_mq_free_tag_set(&ctrl->admin_tag_set);
676 nvme_rdma_dev_put(ctrl->device);
679 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
681 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
683 if (list_empty(&ctrl->list))
686 mutex_lock(&nvme_rdma_ctrl_mutex);
687 list_del(&ctrl->list);
688 mutex_unlock(&nvme_rdma_ctrl_mutex);
690 if (ctrl->ctrl.tagset) {
691 blk_cleanup_queue(ctrl->ctrl.connect_q);
692 blk_mq_free_tag_set(&ctrl->tag_set);
693 nvme_rdma_dev_put(ctrl->device);
696 nvmf_free_options(nctrl->opts);
701 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
703 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
704 struct nvme_rdma_ctrl, reconnect_work);
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, NVMF_AQ_DEPTH);
726 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
728 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
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);
751 if (ctrl->queue_count > 1)
752 nvme_start_queues(&ctrl->ctrl);
754 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
759 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
761 /* Make sure we are not resetting/deleting */
762 if (ctrl->ctrl.state == NVME_CTRL_RECONNECTING) {
763 dev_info(ctrl->ctrl.device,
764 "Failed reconnect attempt, requeueing...\n");
765 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
766 ctrl->reconnect_delay * HZ);
770 static void nvme_rdma_error_recovery_work(struct work_struct *work)
772 struct nvme_rdma_ctrl *ctrl = container_of(work,
773 struct nvme_rdma_ctrl, err_work);
775 nvme_stop_keep_alive(&ctrl->ctrl);
776 if (ctrl->queue_count > 1)
777 nvme_stop_queues(&ctrl->ctrl);
778 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
780 /* We must take care of fastfail/requeue all our inflight requests */
781 if (ctrl->queue_count > 1)
782 blk_mq_tagset_busy_iter(&ctrl->tag_set,
783 nvme_cancel_request, &ctrl->ctrl);
784 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
785 nvme_cancel_request, &ctrl->ctrl);
787 dev_info(ctrl->ctrl.device, "reconnecting in %d seconds\n",
788 ctrl->reconnect_delay);
790 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
791 ctrl->reconnect_delay * HZ);
794 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
796 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
799 queue_work(nvme_rdma_wq, &ctrl->err_work);
802 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
805 struct nvme_rdma_queue *queue = cq->cq_context;
806 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
808 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
809 dev_info(ctrl->ctrl.device,
810 "%s for CQE 0x%p failed with status %s (%d)\n",
812 ib_wc_status_msg(wc->status), wc->status);
813 nvme_rdma_error_recovery(ctrl);
816 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
818 if (unlikely(wc->status != IB_WC_SUCCESS))
819 nvme_rdma_wr_error(cq, wc, "MEMREG");
822 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
824 if (unlikely(wc->status != IB_WC_SUCCESS))
825 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
828 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
829 struct nvme_rdma_request *req)
831 struct ib_send_wr *bad_wr;
832 struct ib_send_wr wr = {
833 .opcode = IB_WR_LOCAL_INV,
837 .ex.invalidate_rkey = req->mr->rkey,
840 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
841 wr.wr_cqe = &req->reg_cqe;
843 return ib_post_send(queue->qp, &wr, &bad_wr);
846 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
849 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
850 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
851 struct nvme_rdma_device *dev = queue->device;
852 struct ib_device *ibdev = dev->dev;
855 if (!blk_rq_bytes(rq))
858 if (req->need_inval) {
859 res = nvme_rdma_inv_rkey(queue, req);
861 dev_err(ctrl->ctrl.device,
862 "Queueing INV WR for rkey %#x failed (%d)\n",
864 nvme_rdma_error_recovery(queue->ctrl);
868 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
869 req->nents, rq_data_dir(rq) ==
870 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
872 nvme_cleanup_cmd(rq);
873 sg_free_table_chained(&req->sg_table, true);
876 static int nvme_rdma_set_sg_null(struct nvme_command *c)
878 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
881 put_unaligned_le24(0, sg->length);
882 put_unaligned_le32(0, sg->key);
883 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
887 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
888 struct nvme_rdma_request *req, struct nvme_command *c)
890 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
892 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
893 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
894 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
896 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
897 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
898 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
900 req->inline_data = true;
905 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
906 struct nvme_rdma_request *req, struct nvme_command *c)
908 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
910 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
911 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
912 put_unaligned_le32(queue->device->mr->rkey, sg->key);
913 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
917 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
918 struct nvme_rdma_request *req, struct nvme_command *c,
921 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
924 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE);
931 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
933 req->reg_cqe.done = nvme_rdma_memreg_done;
934 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
935 req->reg_wr.wr.opcode = IB_WR_REG_MR;
936 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
937 req->reg_wr.wr.num_sge = 0;
938 req->reg_wr.mr = req->mr;
939 req->reg_wr.key = req->mr->rkey;
940 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
941 IB_ACCESS_REMOTE_READ |
942 IB_ACCESS_REMOTE_WRITE;
944 req->need_inval = true;
946 sg->addr = cpu_to_le64(req->mr->iova);
947 put_unaligned_le24(req->mr->length, sg->length);
948 put_unaligned_le32(req->mr->rkey, sg->key);
949 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
950 NVME_SGL_FMT_INVALIDATE;
955 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
956 struct request *rq, unsigned int map_len,
957 struct nvme_command *c)
959 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
960 struct nvme_rdma_device *dev = queue->device;
961 struct ib_device *ibdev = dev->dev;
966 req->inline_data = false;
967 req->need_inval = false;
969 c->common.flags |= NVME_CMD_SGL_METABUF;
971 if (!blk_rq_bytes(rq))
972 return nvme_rdma_set_sg_null(c);
974 req->sg_table.sgl = req->first_sgl;
975 ret = sg_alloc_table_chained(&req->sg_table, rq->nr_phys_segments,
980 nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
981 BUG_ON(nents > rq->nr_phys_segments);
984 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, nents,
985 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
986 if (unlikely(count <= 0)) {
987 sg_free_table_chained(&req->sg_table, true);
992 if (rq_data_dir(rq) == WRITE &&
993 map_len <= nvme_rdma_inline_data_size(queue) &&
994 nvme_rdma_queue_idx(queue))
995 return nvme_rdma_map_sg_inline(queue, req, c);
997 if (!register_always)
998 return nvme_rdma_map_sg_single(queue, req, c);
1001 return nvme_rdma_map_sg_fr(queue, req, c, count);
1004 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1006 if (unlikely(wc->status != IB_WC_SUCCESS))
1007 nvme_rdma_wr_error(cq, wc, "SEND");
1010 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1011 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1012 struct ib_send_wr *first, bool flush)
1014 struct ib_send_wr wr, *bad_wr;
1017 sge->addr = qe->dma;
1018 sge->length = sizeof(struct nvme_command),
1019 sge->lkey = queue->device->pd->local_dma_lkey;
1021 qe->cqe.done = nvme_rdma_send_done;
1024 wr.wr_cqe = &qe->cqe;
1026 wr.num_sge = num_sge;
1027 wr.opcode = IB_WR_SEND;
1031 * Unsignalled send completions are another giant desaster in the
1032 * IB Verbs spec: If we don't regularly post signalled sends
1033 * the send queue will fill up and only a QP reset will rescue us.
1034 * Would have been way to obvious to handle this in hardware or
1035 * at least the RDMA stack..
1037 * This messy and racy code sniplet is copy and pasted from the iSER
1038 * initiator, and the magic '32' comes from there as well.
1040 * Always signal the flushes. The magic request used for the flush
1041 * sequencer is not allocated in our driver's tagset and it's
1042 * triggered to be freed by blk_cleanup_queue(). So we need to
1043 * always mark it as signaled to ensure that the "wr_cqe", which is
1044 * embeded in request's payload, is not freed when __ib_process_cq()
1045 * calls wr_cqe->done().
1047 if ((++queue->sig_count % 32) == 0 || flush)
1048 wr.send_flags |= IB_SEND_SIGNALED;
1055 ret = ib_post_send(queue->qp, first, &bad_wr);
1057 dev_err(queue->ctrl->ctrl.device,
1058 "%s failed with error code %d\n", __func__, ret);
1063 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1064 struct nvme_rdma_qe *qe)
1066 struct ib_recv_wr wr, *bad_wr;
1070 list.addr = qe->dma;
1071 list.length = sizeof(struct nvme_completion);
1072 list.lkey = queue->device->pd->local_dma_lkey;
1074 qe->cqe.done = nvme_rdma_recv_done;
1077 wr.wr_cqe = &qe->cqe;
1081 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1083 dev_err(queue->ctrl->ctrl.device,
1084 "%s failed with error code %d\n", __func__, ret);
1089 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1091 u32 queue_idx = nvme_rdma_queue_idx(queue);
1094 return queue->ctrl->admin_tag_set.tags[queue_idx];
1095 return queue->ctrl->tag_set.tags[queue_idx - 1];
1098 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1100 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1101 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1102 struct ib_device *dev = queue->device->dev;
1103 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1104 struct nvme_command *cmd = sqe->data;
1108 if (WARN_ON_ONCE(aer_idx != 0))
1111 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1113 memset(cmd, 0, sizeof(*cmd));
1114 cmd->common.opcode = nvme_admin_async_event;
1115 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1116 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1117 nvme_rdma_set_sg_null(cmd);
1119 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1122 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1126 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1127 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1129 u16 status = le16_to_cpu(cqe->status);
1131 struct nvme_rdma_request *req;
1136 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1138 dev_err(queue->ctrl->ctrl.device,
1139 "tag 0x%x on QP %#x not found\n",
1140 cqe->command_id, queue->qp->qp_num);
1141 nvme_rdma_error_recovery(queue->ctrl);
1144 req = blk_mq_rq_to_pdu(rq);
1146 if (rq->cmd_type == REQ_TYPE_DRV_PRIV && rq->special)
1147 memcpy(rq->special, cqe, sizeof(*cqe));
1152 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1153 wc->ex.invalidate_rkey == req->mr->rkey)
1154 req->need_inval = false;
1156 blk_mq_complete_request(rq, status);
1161 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1163 struct nvme_rdma_qe *qe =
1164 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1165 struct nvme_rdma_queue *queue = cq->cq_context;
1166 struct ib_device *ibdev = queue->device->dev;
1167 struct nvme_completion *cqe = qe->data;
1168 const size_t len = sizeof(struct nvme_completion);
1171 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1172 nvme_rdma_wr_error(cq, wc, "RECV");
1176 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1178 * AEN requests are special as they don't time out and can
1179 * survive any kind of queue freeze and often don't respond to
1180 * aborts. We don't even bother to allocate a struct request
1181 * for them but rather special case them here.
1183 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1184 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
1185 nvme_complete_async_event(&queue->ctrl->ctrl, cqe);
1187 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1188 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1190 nvme_rdma_post_recv(queue, qe);
1194 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1196 __nvme_rdma_recv_done(cq, wc, -1);
1199 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1203 for (i = 0; i < queue->queue_size; i++) {
1204 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1206 goto out_destroy_queue_ib;
1211 out_destroy_queue_ib:
1212 nvme_rdma_destroy_queue_ib(queue);
1216 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1217 struct rdma_cm_event *ev)
1219 if (ev->param.conn.private_data_len) {
1220 struct nvme_rdma_cm_rej *rej =
1221 (struct nvme_rdma_cm_rej *)ev->param.conn.private_data;
1223 dev_err(queue->ctrl->ctrl.device,
1224 "Connect rejected, status %d.", le16_to_cpu(rej->sts));
1225 /* XXX: Think of something clever to do here... */
1227 dev_err(queue->ctrl->ctrl.device,
1228 "Connect rejected, no private data.\n");
1234 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1236 struct nvme_rdma_device *dev;
1239 dev = nvme_rdma_find_get_device(queue->cm_id);
1241 dev_err(queue->cm_id->device->dma_device,
1242 "no client data found!\n");
1243 return -ECONNREFUSED;
1246 ret = nvme_rdma_create_queue_ib(queue, dev);
1248 nvme_rdma_dev_put(dev);
1252 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1254 dev_err(queue->ctrl->ctrl.device,
1255 "rdma_resolve_route failed (%d).\n",
1257 goto out_destroy_queue;
1263 nvme_rdma_destroy_queue_ib(queue);
1268 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1270 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1271 struct rdma_conn_param param = { };
1272 struct nvme_rdma_cm_req priv;
1275 param.qp_num = queue->qp->qp_num;
1276 param.flow_control = 1;
1278 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1279 /* maximum retry count */
1280 param.retry_count = 7;
1281 param.rnr_retry_count = 7;
1282 param.private_data = &priv;
1283 param.private_data_len = sizeof(priv);
1285 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1286 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1287 priv.hrqsize = cpu_to_le16(queue->queue_size);
1288 priv.hsqsize = cpu_to_le16(queue->queue_size);
1290 ret = rdma_connect(queue->cm_id, ¶m);
1292 dev_err(ctrl->ctrl.device,
1293 "rdma_connect failed (%d).\n", ret);
1294 goto out_destroy_queue_ib;
1299 out_destroy_queue_ib:
1300 nvme_rdma_destroy_queue_ib(queue);
1305 * nvme_rdma_device_unplug() - Handle RDMA device unplug
1306 * @queue: Queue that owns the cm_id that caught the event
1308 * DEVICE_REMOVAL event notifies us that the RDMA device is about
1309 * to unplug so we should take care of destroying our RDMA resources.
1310 * This event will be generated for each allocated cm_id.
1312 * In our case, the RDMA resources are managed per controller and not
1313 * only per queue. So the way we handle this is we trigger an implicit
1314 * controller deletion upon the first DEVICE_REMOVAL event we see, and
1315 * hold the event inflight until the controller deletion is completed.
1317 * One exception that we need to handle is the destruction of the cm_id
1318 * that caught the event. Since we hold the callout until the controller
1319 * deletion is completed, we'll deadlock if the controller deletion will
1320 * call rdma_destroy_id on this queue's cm_id. Thus, we claim ownership
1321 * of destroying this queue before-hand, destroy the queue resources
1322 * after the controller deletion completed with the exception of destroying
1323 * the cm_id implicitely by returning a non-zero rc to the callout.
1325 static int nvme_rdma_device_unplug(struct nvme_rdma_queue *queue)
1327 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1328 int ret, ctrl_deleted = 0;
1330 /* First disable the queue so ctrl delete won't free it */
1331 if (!test_and_clear_bit(NVME_RDMA_Q_CONNECTED, &queue->flags))
1334 /* delete the controller */
1335 ret = __nvme_rdma_del_ctrl(ctrl);
1337 dev_warn(ctrl->ctrl.device,
1338 "Got rdma device removal event, deleting ctrl\n");
1339 flush_work(&ctrl->delete_work);
1341 /* Return non-zero so the cm_id will destroy implicitly */
1344 /* Free this queue ourselves */
1345 rdma_disconnect(queue->cm_id);
1346 ib_drain_qp(queue->qp);
1347 nvme_rdma_destroy_queue_ib(queue);
1351 return ctrl_deleted;
1354 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1355 struct rdma_cm_event *ev)
1357 struct nvme_rdma_queue *queue = cm_id->context;
1360 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1361 rdma_event_msg(ev->event), ev->event,
1364 switch (ev->event) {
1365 case RDMA_CM_EVENT_ADDR_RESOLVED:
1366 cm_error = nvme_rdma_addr_resolved(queue);
1368 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1369 cm_error = nvme_rdma_route_resolved(queue);
1371 case RDMA_CM_EVENT_ESTABLISHED:
1372 queue->cm_error = nvme_rdma_conn_established(queue);
1373 /* complete cm_done regardless of success/failure */
1374 complete(&queue->cm_done);
1376 case RDMA_CM_EVENT_REJECTED:
1377 cm_error = nvme_rdma_conn_rejected(queue, ev);
1379 case RDMA_CM_EVENT_ADDR_ERROR:
1380 case RDMA_CM_EVENT_ROUTE_ERROR:
1381 case RDMA_CM_EVENT_CONNECT_ERROR:
1382 case RDMA_CM_EVENT_UNREACHABLE:
1383 dev_dbg(queue->ctrl->ctrl.device,
1384 "CM error event %d\n", ev->event);
1385 cm_error = -ECONNRESET;
1387 case RDMA_CM_EVENT_DISCONNECTED:
1388 case RDMA_CM_EVENT_ADDR_CHANGE:
1389 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1390 dev_dbg(queue->ctrl->ctrl.device,
1391 "disconnect received - connection closed\n");
1392 nvme_rdma_error_recovery(queue->ctrl);
1394 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1395 /* return 1 means impliciy CM ID destroy */
1396 return nvme_rdma_device_unplug(queue);
1398 dev_err(queue->ctrl->ctrl.device,
1399 "Unexpected RDMA CM event (%d)\n", ev->event);
1400 nvme_rdma_error_recovery(queue->ctrl);
1405 queue->cm_error = cm_error;
1406 complete(&queue->cm_done);
1412 static enum blk_eh_timer_return
1413 nvme_rdma_timeout(struct request *rq, bool reserved)
1415 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1417 /* queue error recovery */
1418 nvme_rdma_error_recovery(req->queue->ctrl);
1420 /* fail with DNR on cmd timeout */
1421 rq->errors = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1423 return BLK_EH_HANDLED;
1426 static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1427 const struct blk_mq_queue_data *bd)
1429 struct nvme_ns *ns = hctx->queue->queuedata;
1430 struct nvme_rdma_queue *queue = hctx->driver_data;
1431 struct request *rq = bd->rq;
1432 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1433 struct nvme_rdma_qe *sqe = &req->sqe;
1434 struct nvme_command *c = sqe->data;
1436 struct ib_device *dev;
1437 unsigned int map_len;
1440 WARN_ON_ONCE(rq->tag < 0);
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 c->common.command_id = rq->tag;
1451 blk_mq_start_request(rq);
1453 map_len = nvme_map_len(rq);
1454 ret = nvme_rdma_map_data(queue, rq, map_len, c);
1456 dev_err(queue->ctrl->ctrl.device,
1457 "Failed to map data (%d)\n", ret);
1458 nvme_cleanup_cmd(rq);
1462 ib_dma_sync_single_for_device(dev, sqe->dma,
1463 sizeof(struct nvme_command), DMA_TO_DEVICE);
1465 if (rq->cmd_type == REQ_TYPE_FS && req_op(rq) == REQ_OP_FLUSH)
1467 ret = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1468 req->need_inval ? &req->reg_wr.wr : NULL, flush);
1470 nvme_rdma_unmap_data(queue, rq);
1474 return BLK_MQ_RQ_QUEUE_OK;
1476 return (ret == -ENOMEM || ret == -EAGAIN) ?
1477 BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR;
1480 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1482 struct nvme_rdma_queue *queue = hctx->driver_data;
1483 struct ib_cq *cq = queue->ib_cq;
1487 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1488 while (ib_poll_cq(cq, 1, &wc) > 0) {
1489 struct ib_cqe *cqe = wc.wr_cqe;
1492 if (cqe->done == nvme_rdma_recv_done)
1493 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1502 static void nvme_rdma_complete_rq(struct request *rq)
1504 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1505 struct nvme_rdma_queue *queue = req->queue;
1508 nvme_rdma_unmap_data(queue, rq);
1510 if (unlikely(rq->errors)) {
1511 if (nvme_req_needs_retry(rq, rq->errors)) {
1512 nvme_requeue_req(rq);
1516 if (rq->cmd_type == REQ_TYPE_DRV_PRIV)
1519 error = nvme_error_status(rq->errors);
1522 blk_mq_end_request(rq, error);
1525 static struct blk_mq_ops nvme_rdma_mq_ops = {
1526 .queue_rq = nvme_rdma_queue_rq,
1527 .complete = nvme_rdma_complete_rq,
1528 .map_queue = blk_mq_map_queue,
1529 .init_request = nvme_rdma_init_request,
1530 .exit_request = nvme_rdma_exit_request,
1531 .reinit_request = nvme_rdma_reinit_request,
1532 .init_hctx = nvme_rdma_init_hctx,
1533 .poll = nvme_rdma_poll,
1534 .timeout = nvme_rdma_timeout,
1537 static struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1538 .queue_rq = nvme_rdma_queue_rq,
1539 .complete = nvme_rdma_complete_rq,
1540 .map_queue = blk_mq_map_queue,
1541 .init_request = nvme_rdma_init_admin_request,
1542 .exit_request = nvme_rdma_exit_admin_request,
1543 .reinit_request = nvme_rdma_reinit_request,
1544 .init_hctx = nvme_rdma_init_admin_hctx,
1545 .timeout = nvme_rdma_timeout,
1548 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
1552 error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
1556 ctrl->device = ctrl->queues[0].device;
1559 * We need a reference on the device as long as the tag_set is alive,
1560 * as the MRs in the request structures need a valid ib_device.
1563 if (!nvme_rdma_dev_get(ctrl->device))
1564 goto out_free_queue;
1566 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
1567 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
1569 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1570 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
1571 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
1572 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
1573 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1574 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1575 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1576 ctrl->admin_tag_set.driver_data = ctrl;
1577 ctrl->admin_tag_set.nr_hw_queues = 1;
1578 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1580 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1584 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1585 if (IS_ERR(ctrl->ctrl.admin_q)) {
1586 error = PTR_ERR(ctrl->ctrl.admin_q);
1587 goto out_free_tagset;
1590 error = nvmf_connect_admin_queue(&ctrl->ctrl);
1592 goto out_cleanup_queue;
1594 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
1596 dev_err(ctrl->ctrl.device,
1597 "prop_get NVME_REG_CAP failed\n");
1598 goto out_cleanup_queue;
1602 min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize);
1604 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
1606 goto out_cleanup_queue;
1608 ctrl->ctrl.max_hw_sectors =
1609 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
1611 error = nvme_init_identify(&ctrl->ctrl);
1613 goto out_cleanup_queue;
1615 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
1616 &ctrl->async_event_sqe, sizeof(struct nvme_command),
1619 goto out_cleanup_queue;
1621 nvme_start_keep_alive(&ctrl->ctrl);
1626 blk_cleanup_queue(ctrl->ctrl.admin_q);
1628 /* disconnect and drain the queue before freeing the tagset */
1629 nvme_rdma_stop_queue(&ctrl->queues[0]);
1630 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1632 nvme_rdma_dev_put(ctrl->device);
1634 nvme_rdma_free_queue(&ctrl->queues[0]);
1638 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1640 nvme_stop_keep_alive(&ctrl->ctrl);
1641 cancel_work_sync(&ctrl->err_work);
1642 cancel_delayed_work_sync(&ctrl->reconnect_work);
1644 if (ctrl->queue_count > 1) {
1645 nvme_stop_queues(&ctrl->ctrl);
1646 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1647 nvme_cancel_request, &ctrl->ctrl);
1648 nvme_rdma_free_io_queues(ctrl);
1651 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1652 nvme_shutdown_ctrl(&ctrl->ctrl);
1654 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
1655 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1656 nvme_cancel_request, &ctrl->ctrl);
1657 nvme_rdma_destroy_admin_queue(ctrl);
1660 static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1662 struct nvme_rdma_ctrl *ctrl = container_of(work,
1663 struct nvme_rdma_ctrl, delete_work);
1665 nvme_remove_namespaces(&ctrl->ctrl);
1666 nvme_rdma_shutdown_ctrl(ctrl);
1667 nvme_uninit_ctrl(&ctrl->ctrl);
1668 nvme_put_ctrl(&ctrl->ctrl);
1671 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1673 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1676 if (!queue_work(nvme_rdma_wq, &ctrl->delete_work))
1682 static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1684 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1687 ret = __nvme_rdma_del_ctrl(ctrl);
1691 flush_work(&ctrl->delete_work);
1696 static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1698 struct nvme_rdma_ctrl *ctrl = container_of(work,
1699 struct nvme_rdma_ctrl, delete_work);
1701 nvme_remove_namespaces(&ctrl->ctrl);
1702 nvme_uninit_ctrl(&ctrl->ctrl);
1703 nvme_put_ctrl(&ctrl->ctrl);
1706 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1708 struct nvme_rdma_ctrl *ctrl = container_of(work,
1709 struct nvme_rdma_ctrl, reset_work);
1713 nvme_rdma_shutdown_ctrl(ctrl);
1715 ret = nvme_rdma_configure_admin_queue(ctrl);
1717 /* ctrl is already shutdown, just remove the ctrl */
1718 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1722 if (ctrl->queue_count > 1) {
1723 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
1727 ret = nvme_rdma_init_io_queues(ctrl);
1731 ret = nvme_rdma_connect_io_queues(ctrl);
1736 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1737 WARN_ON_ONCE(!changed);
1739 if (ctrl->queue_count > 1) {
1740 nvme_start_queues(&ctrl->ctrl);
1741 nvme_queue_scan(&ctrl->ctrl);
1747 /* Deleting this dead controller... */
1748 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1749 WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work));
1752 static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl)
1754 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1756 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1759 if (!queue_work(nvme_rdma_wq, &ctrl->reset_work))
1762 flush_work(&ctrl->reset_work);
1767 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1769 .module = THIS_MODULE,
1771 .reg_read32 = nvmf_reg_read32,
1772 .reg_read64 = nvmf_reg_read64,
1773 .reg_write32 = nvmf_reg_write32,
1774 .reset_ctrl = nvme_rdma_reset_ctrl,
1775 .free_ctrl = nvme_rdma_free_ctrl,
1776 .submit_async_event = nvme_rdma_submit_async_event,
1777 .delete_ctrl = nvme_rdma_del_ctrl,
1778 .get_subsysnqn = nvmf_get_subsysnqn,
1779 .get_address = nvmf_get_address,
1782 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1784 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
1787 ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues);
1791 ctrl->queue_count = opts->nr_io_queues + 1;
1792 if (ctrl->queue_count < 2)
1795 dev_info(ctrl->ctrl.device,
1796 "creating %d I/O queues.\n", opts->nr_io_queues);
1798 ret = nvme_rdma_init_io_queues(ctrl);
1803 * We need a reference on the device as long as the tag_set is alive,
1804 * as the MRs in the request structures need a valid ib_device.
1807 if (!nvme_rdma_dev_get(ctrl->device))
1808 goto out_free_io_queues;
1810 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1811 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
1812 ctrl->tag_set.queue_depth = ctrl->ctrl.sqsize;
1813 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1814 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1815 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1816 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1817 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1818 ctrl->tag_set.driver_data = ctrl;
1819 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
1820 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1822 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1825 ctrl->ctrl.tagset = &ctrl->tag_set;
1827 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1828 if (IS_ERR(ctrl->ctrl.connect_q)) {
1829 ret = PTR_ERR(ctrl->ctrl.connect_q);
1830 goto out_free_tag_set;
1833 ret = nvme_rdma_connect_io_queues(ctrl);
1835 goto out_cleanup_connect_q;
1839 out_cleanup_connect_q:
1840 blk_cleanup_queue(ctrl->ctrl.connect_q);
1842 blk_mq_free_tag_set(&ctrl->tag_set);
1844 nvme_rdma_dev_put(ctrl->device);
1846 nvme_rdma_free_io_queues(ctrl);
1850 static int nvme_rdma_parse_ipaddr(struct sockaddr_in *in_addr, char *p)
1852 u8 *addr = (u8 *)&in_addr->sin_addr.s_addr;
1853 size_t buflen = strlen(p);
1855 /* XXX: handle IPv6 addresses */
1857 if (buflen > INET_ADDRSTRLEN)
1859 if (in4_pton(p, buflen, addr, '\0', NULL) == 0)
1861 in_addr->sin_family = AF_INET;
1865 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1866 struct nvmf_ctrl_options *opts)
1868 struct nvme_rdma_ctrl *ctrl;
1872 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1874 return ERR_PTR(-ENOMEM);
1875 ctrl->ctrl.opts = opts;
1876 INIT_LIST_HEAD(&ctrl->list);
1878 ret = nvme_rdma_parse_ipaddr(&ctrl->addr_in, opts->traddr);
1880 pr_err("malformed IP address passed: %s\n", opts->traddr);
1884 if (opts->mask & NVMF_OPT_TRSVCID) {
1887 ret = kstrtou16(opts->trsvcid, 0, &port);
1891 ctrl->addr_in.sin_port = cpu_to_be16(port);
1893 ctrl->addr_in.sin_port = cpu_to_be16(NVME_RDMA_IP_PORT);
1896 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1897 0 /* no quirks, we're perfect! */);
1901 ctrl->reconnect_delay = opts->reconnect_delay;
1902 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1903 nvme_rdma_reconnect_ctrl_work);
1904 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1905 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1906 INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work);
1907 spin_lock_init(&ctrl->lock);
1909 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1910 ctrl->ctrl.sqsize = opts->queue_size;
1911 ctrl->ctrl.kato = opts->kato;
1914 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues),
1917 goto out_uninit_ctrl;
1919 ret = nvme_rdma_configure_admin_queue(ctrl);
1921 goto out_kfree_queues;
1923 /* sanity check icdoff */
1924 if (ctrl->ctrl.icdoff) {
1925 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1926 goto out_remove_admin_queue;
1929 /* sanity check keyed sgls */
1930 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1931 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1932 goto out_remove_admin_queue;
1935 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1936 /* warn if maxcmd is lower than queue_size */
1937 dev_warn(ctrl->ctrl.device,
1938 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1939 opts->queue_size, ctrl->ctrl.maxcmd);
1940 opts->queue_size = ctrl->ctrl.maxcmd;
1943 if (opts->nr_io_queues) {
1944 ret = nvme_rdma_create_io_queues(ctrl);
1946 goto out_remove_admin_queue;
1949 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1950 WARN_ON_ONCE(!changed);
1952 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
1953 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1955 kref_get(&ctrl->ctrl.kref);
1957 mutex_lock(&nvme_rdma_ctrl_mutex);
1958 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1959 mutex_unlock(&nvme_rdma_ctrl_mutex);
1961 if (opts->nr_io_queues) {
1962 nvme_queue_scan(&ctrl->ctrl);
1963 nvme_queue_async_events(&ctrl->ctrl);
1968 out_remove_admin_queue:
1969 nvme_stop_keep_alive(&ctrl->ctrl);
1970 nvme_rdma_destroy_admin_queue(ctrl);
1972 kfree(ctrl->queues);
1974 nvme_uninit_ctrl(&ctrl->ctrl);
1975 nvme_put_ctrl(&ctrl->ctrl);
1978 return ERR_PTR(ret);
1981 return ERR_PTR(ret);
1984 static struct nvmf_transport_ops nvme_rdma_transport = {
1986 .required_opts = NVMF_OPT_TRADDR,
1987 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY,
1988 .create_ctrl = nvme_rdma_create_ctrl,
1991 static int __init nvme_rdma_init_module(void)
1993 nvme_rdma_wq = create_workqueue("nvme_rdma_wq");
1997 nvmf_register_transport(&nvme_rdma_transport);
2001 static void __exit nvme_rdma_cleanup_module(void)
2003 struct nvme_rdma_ctrl *ctrl;
2005 nvmf_unregister_transport(&nvme_rdma_transport);
2007 mutex_lock(&nvme_rdma_ctrl_mutex);
2008 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list)
2009 __nvme_rdma_del_ctrl(ctrl);
2010 mutex_unlock(&nvme_rdma_ctrl_mutex);
2012 destroy_workqueue(nvme_rdma_wq);
2015 module_init(nvme_rdma_init_module);
2016 module_exit(nvme_rdma_cleanup_module);
2018 MODULE_LICENSE("GPL v2");