2 * NVMe over Fabrics RDMA host code.
3 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/err.h>
19 #include <linux/string.h>
20 #include <linux/atomic.h>
21 #include <linux/blk-mq.h>
22 #include <linux/types.h>
23 #include <linux/list.h>
24 #include <linux/mutex.h>
25 #include <linux/scatterlist.h>
26 #include <linux/nvme.h>
27 #include <asm/unaligned.h>
29 #include <rdma/ib_verbs.h>
30 #include <rdma/rdma_cm.h>
31 #include <rdma/ib_cm.h>
32 #include <linux/nvme-rdma.h>
38 #define NVME_RDMA_CONNECT_TIMEOUT_MS 1000 /* 1 second */
40 #define NVME_RDMA_MAX_SEGMENT_SIZE 0xffffff /* 24-bit SGL field */
42 #define NVME_RDMA_MAX_SEGMENTS 256
44 #define NVME_RDMA_MAX_INLINE_SEGMENTS 1
46 #define NVME_RDMA_MAX_PAGES_PER_MR 512
48 #define NVME_RDMA_DEF_RECONNECT_DELAY 20
51 * We handle AEN commands ourselves and don't even let the
52 * block layer know about them.
54 #define NVME_RDMA_NR_AEN_COMMANDS 1
55 #define NVME_RDMA_AQ_BLKMQ_DEPTH \
56 (NVMF_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
58 struct nvme_rdma_device {
59 struct ib_device *dev;
62 struct list_head entry;
71 struct nvme_rdma_queue;
72 struct nvme_rdma_request {
74 struct nvme_rdma_qe sqe;
75 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
80 struct ib_reg_wr reg_wr;
81 struct ib_cqe reg_cqe;
82 struct nvme_rdma_queue *queue;
83 struct sg_table sg_table;
84 struct scatterlist first_sgl[];
87 enum nvme_rdma_queue_flags {
88 NVME_RDMA_Q_CONNECTED = (1 << 0),
91 struct nvme_rdma_queue {
92 struct nvme_rdma_qe *rsp_ring;
95 size_t cmnd_capsule_len;
96 struct nvme_rdma_ctrl *ctrl;
97 struct nvme_rdma_device *device;
102 struct rdma_cm_id *cm_id;
104 struct completion cm_done;
107 struct nvme_rdma_ctrl {
108 /* read and written in the hot path */
111 /* read only in the hot path */
112 struct nvme_rdma_queue *queues;
115 /* other member variables */
116 struct blk_mq_tag_set tag_set;
117 struct work_struct delete_work;
118 struct work_struct reset_work;
119 struct work_struct err_work;
121 struct nvme_rdma_qe async_event_sqe;
124 struct delayed_work reconnect_work;
126 struct list_head list;
128 struct blk_mq_tag_set admin_tag_set;
129 struct nvme_rdma_device *device;
135 struct sockaddr addr;
136 struct sockaddr_in addr_in;
139 struct nvme_ctrl ctrl;
142 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
144 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
147 static LIST_HEAD(device_list);
148 static DEFINE_MUTEX(device_list_mutex);
150 static LIST_HEAD(nvme_rdma_ctrl_list);
151 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
153 static struct workqueue_struct *nvme_rdma_wq;
156 * Disabling this option makes small I/O goes faster, but is fundamentally
157 * unsafe. With it turned off we will have to register a global rkey that
158 * allows read and write access to all physical memory.
160 static bool register_always = true;
161 module_param(register_always, bool, 0444);
162 MODULE_PARM_DESC(register_always,
163 "Use memory registration even for contiguous memory regions");
165 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
166 struct rdma_cm_event *event);
167 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
169 /* XXX: really should move to a generic header sooner or later.. */
170 static inline void put_unaligned_le24(u32 val, u8 *p)
177 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
179 return queue - queue->ctrl->queues;
182 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
184 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
187 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
188 size_t capsule_size, enum dma_data_direction dir)
190 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
194 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
195 size_t capsule_size, enum dma_data_direction dir)
197 qe->data = kzalloc(capsule_size, GFP_KERNEL);
201 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
202 if (ib_dma_mapping_error(ibdev, qe->dma)) {
210 static void nvme_rdma_free_ring(struct ib_device *ibdev,
211 struct nvme_rdma_qe *ring, size_t ib_queue_size,
212 size_t capsule_size, enum dma_data_direction dir)
216 for (i = 0; i < ib_queue_size; i++)
217 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
221 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
222 size_t ib_queue_size, size_t capsule_size,
223 enum dma_data_direction dir)
225 struct nvme_rdma_qe *ring;
228 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
232 for (i = 0; i < ib_queue_size; i++) {
233 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
240 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
244 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
246 pr_debug("QP event %d\n", event->event);
249 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
251 wait_for_completion_interruptible_timeout(&queue->cm_done,
252 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
253 return queue->cm_error;
256 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
258 struct nvme_rdma_device *dev = queue->device;
259 struct ib_qp_init_attr init_attr;
262 memset(&init_attr, 0, sizeof(init_attr));
263 init_attr.event_handler = nvme_rdma_qp_event;
265 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
267 init_attr.cap.max_recv_wr = queue->queue_size + 1;
268 init_attr.cap.max_recv_sge = 1;
269 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
270 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
271 init_attr.qp_type = IB_QPT_RC;
272 init_attr.send_cq = queue->ib_cq;
273 init_attr.recv_cq = queue->ib_cq;
275 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
277 queue->qp = queue->cm_id->qp;
281 static int nvme_rdma_reinit_request(void *data, struct request *rq)
283 struct nvme_rdma_ctrl *ctrl = data;
284 struct nvme_rdma_device *dev = ctrl->device;
285 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
288 if (!req->need_inval)
291 ib_dereg_mr(req->mr);
293 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
295 if (IS_ERR(req->mr)) {
296 ret = PTR_ERR(req->mr);
300 req->need_inval = false;
306 static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl *ctrl,
307 struct request *rq, unsigned int queue_idx)
309 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
310 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
311 struct nvme_rdma_device *dev = queue->device;
314 ib_dereg_mr(req->mr);
316 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
320 static void nvme_rdma_exit_request(void *data, struct request *rq,
321 unsigned int hctx_idx, unsigned int rq_idx)
323 return __nvme_rdma_exit_request(data, rq, hctx_idx + 1);
326 static void nvme_rdma_exit_admin_request(void *data, struct request *rq,
327 unsigned int hctx_idx, unsigned int rq_idx)
329 return __nvme_rdma_exit_request(data, rq, 0);
332 static int __nvme_rdma_init_request(struct nvme_rdma_ctrl *ctrl,
333 struct request *rq, unsigned int queue_idx)
335 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
336 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
337 struct nvme_rdma_device *dev = queue->device;
338 struct ib_device *ibdev = dev->dev;
341 BUG_ON(queue_idx >= ctrl->queue_count);
343 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
348 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
350 if (IS_ERR(req->mr)) {
351 ret = PTR_ERR(req->mr);
360 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
365 static int nvme_rdma_init_request(void *data, struct request *rq,
366 unsigned int hctx_idx, unsigned int rq_idx,
367 unsigned int numa_node)
369 return __nvme_rdma_init_request(data, rq, hctx_idx + 1);
372 static int nvme_rdma_init_admin_request(void *data, struct request *rq,
373 unsigned int hctx_idx, unsigned int rq_idx,
374 unsigned int numa_node)
376 return __nvme_rdma_init_request(data, rq, 0);
379 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
380 unsigned int hctx_idx)
382 struct nvme_rdma_ctrl *ctrl = data;
383 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
385 BUG_ON(hctx_idx >= ctrl->queue_count);
387 hctx->driver_data = queue;
391 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
392 unsigned int hctx_idx)
394 struct nvme_rdma_ctrl *ctrl = data;
395 struct nvme_rdma_queue *queue = &ctrl->queues[0];
397 BUG_ON(hctx_idx != 0);
399 hctx->driver_data = queue;
403 static void nvme_rdma_free_dev(struct kref *ref)
405 struct nvme_rdma_device *ndev =
406 container_of(ref, struct nvme_rdma_device, ref);
408 mutex_lock(&device_list_mutex);
409 list_del(&ndev->entry);
410 mutex_unlock(&device_list_mutex);
412 ib_dealloc_pd(ndev->pd);
416 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
418 kref_put(&dev->ref, nvme_rdma_free_dev);
421 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
423 return kref_get_unless_zero(&dev->ref);
426 static struct nvme_rdma_device *
427 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
429 struct nvme_rdma_device *ndev;
431 mutex_lock(&device_list_mutex);
432 list_for_each_entry(ndev, &device_list, entry) {
433 if (ndev->dev->node_guid == cm_id->device->node_guid &&
434 nvme_rdma_dev_get(ndev))
438 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
442 ndev->dev = cm_id->device;
443 kref_init(&ndev->ref);
445 ndev->pd = ib_alloc_pd(ndev->dev,
446 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
447 if (IS_ERR(ndev->pd))
450 if (!(ndev->dev->attrs.device_cap_flags &
451 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
452 dev_err(&ndev->dev->dev,
453 "Memory registrations not supported.\n");
457 list_add(&ndev->entry, &device_list);
459 mutex_unlock(&device_list_mutex);
463 ib_dealloc_pd(ndev->pd);
467 mutex_unlock(&device_list_mutex);
471 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
473 struct nvme_rdma_device *dev = queue->device;
474 struct ib_device *ibdev = dev->dev;
476 rdma_destroy_qp(queue->cm_id);
477 ib_free_cq(queue->ib_cq);
479 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
480 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
482 nvme_rdma_dev_put(dev);
485 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue,
486 struct nvme_rdma_device *dev)
488 struct ib_device *ibdev = dev->dev;
489 const int send_wr_factor = 3; /* MR, SEND, INV */
490 const int cq_factor = send_wr_factor + 1; /* + RECV */
491 int comp_vector, idx = nvme_rdma_queue_idx(queue);
498 * The admin queue is barely used once the controller is live, so don't
499 * bother to spread it out.
504 comp_vector = idx % ibdev->num_comp_vectors;
507 /* +1 for ib_stop_cq */
508 queue->ib_cq = ib_alloc_cq(dev->dev, queue,
509 cq_factor * queue->queue_size + 1, comp_vector,
511 if (IS_ERR(queue->ib_cq)) {
512 ret = PTR_ERR(queue->ib_cq);
516 ret = nvme_rdma_create_qp(queue, send_wr_factor);
518 goto out_destroy_ib_cq;
520 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
521 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
522 if (!queue->rsp_ring) {
530 ib_destroy_qp(queue->qp);
532 ib_free_cq(queue->ib_cq);
537 static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl,
538 int idx, size_t queue_size)
540 struct nvme_rdma_queue *queue;
543 queue = &ctrl->queues[idx];
545 init_completion(&queue->cm_done);
548 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
550 queue->cmnd_capsule_len = sizeof(struct nvme_command);
552 queue->queue_size = queue_size;
554 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
555 RDMA_PS_TCP, IB_QPT_RC);
556 if (IS_ERR(queue->cm_id)) {
557 dev_info(ctrl->ctrl.device,
558 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
559 return PTR_ERR(queue->cm_id);
562 queue->cm_error = -ETIMEDOUT;
563 ret = rdma_resolve_addr(queue->cm_id, NULL, &ctrl->addr,
564 NVME_RDMA_CONNECT_TIMEOUT_MS);
566 dev_info(ctrl->ctrl.device,
567 "rdma_resolve_addr failed (%d).\n", ret);
568 goto out_destroy_cm_id;
571 ret = nvme_rdma_wait_for_cm(queue);
573 dev_info(ctrl->ctrl.device,
574 "rdma_resolve_addr wait failed (%d).\n", ret);
575 goto out_destroy_cm_id;
578 set_bit(NVME_RDMA_Q_CONNECTED, &queue->flags);
583 rdma_destroy_id(queue->cm_id);
587 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
589 rdma_disconnect(queue->cm_id);
590 ib_drain_qp(queue->qp);
593 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
595 nvme_rdma_destroy_queue_ib(queue);
596 rdma_destroy_id(queue->cm_id);
599 static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue)
601 if (!test_and_clear_bit(NVME_RDMA_Q_CONNECTED, &queue->flags))
603 nvme_rdma_stop_queue(queue);
604 nvme_rdma_free_queue(queue);
607 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
611 for (i = 1; i < ctrl->queue_count; i++)
612 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
615 static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl)
619 for (i = 1; i < ctrl->queue_count; i++) {
620 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
628 static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl)
632 for (i = 1; i < ctrl->queue_count; i++) {
633 ret = nvme_rdma_init_queue(ctrl, i, ctrl->ctrl.sqsize);
635 dev_info(ctrl->ctrl.device,
636 "failed to initialize i/o queue: %d\n", ret);
637 goto out_free_queues;
645 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
650 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl)
652 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
653 sizeof(struct nvme_command), DMA_TO_DEVICE);
654 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
655 blk_cleanup_queue(ctrl->ctrl.admin_q);
656 blk_mq_free_tag_set(&ctrl->admin_tag_set);
657 nvme_rdma_dev_put(ctrl->device);
660 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
662 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
664 if (list_empty(&ctrl->list))
667 mutex_lock(&nvme_rdma_ctrl_mutex);
668 list_del(&ctrl->list);
669 mutex_unlock(&nvme_rdma_ctrl_mutex);
672 nvmf_free_options(nctrl->opts);
677 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
679 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
680 struct nvme_rdma_ctrl, reconnect_work);
684 if (ctrl->queue_count > 1) {
685 nvme_rdma_free_io_queues(ctrl);
687 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
692 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
694 ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set);
698 ret = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
702 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
704 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
708 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
712 nvme_start_keep_alive(&ctrl->ctrl);
714 if (ctrl->queue_count > 1) {
715 ret = nvme_rdma_init_io_queues(ctrl);
719 ret = nvme_rdma_connect_io_queues(ctrl);
724 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
725 WARN_ON_ONCE(!changed);
727 if (ctrl->queue_count > 1) {
728 nvme_start_queues(&ctrl->ctrl);
729 nvme_queue_scan(&ctrl->ctrl);
730 nvme_queue_async_events(&ctrl->ctrl);
733 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
738 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
740 /* Make sure we are not resetting/deleting */
741 if (ctrl->ctrl.state == NVME_CTRL_RECONNECTING) {
742 dev_info(ctrl->ctrl.device,
743 "Failed reconnect attempt, requeueing...\n");
744 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
745 ctrl->reconnect_delay * HZ);
749 static void nvme_rdma_error_recovery_work(struct work_struct *work)
751 struct nvme_rdma_ctrl *ctrl = container_of(work,
752 struct nvme_rdma_ctrl, err_work);
754 nvme_stop_keep_alive(&ctrl->ctrl);
755 if (ctrl->queue_count > 1)
756 nvme_stop_queues(&ctrl->ctrl);
757 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
759 /* We must take care of fastfail/requeue all our inflight requests */
760 if (ctrl->queue_count > 1)
761 blk_mq_tagset_busy_iter(&ctrl->tag_set,
762 nvme_cancel_request, &ctrl->ctrl);
763 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
764 nvme_cancel_request, &ctrl->ctrl);
766 dev_info(ctrl->ctrl.device, "reconnecting in %d seconds\n",
767 ctrl->reconnect_delay);
769 queue_delayed_work(nvme_rdma_wq, &ctrl->reconnect_work,
770 ctrl->reconnect_delay * HZ);
773 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
775 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
778 queue_work(nvme_rdma_wq, &ctrl->err_work);
781 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
784 struct nvme_rdma_queue *queue = cq->cq_context;
785 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
787 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
788 dev_info(ctrl->ctrl.device,
789 "%s for CQE 0x%p failed with status %s (%d)\n",
791 ib_wc_status_msg(wc->status), wc->status);
792 nvme_rdma_error_recovery(ctrl);
795 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
797 if (unlikely(wc->status != IB_WC_SUCCESS))
798 nvme_rdma_wr_error(cq, wc, "MEMREG");
801 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
803 if (unlikely(wc->status != IB_WC_SUCCESS))
804 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
807 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
808 struct nvme_rdma_request *req)
810 struct ib_send_wr *bad_wr;
811 struct ib_send_wr wr = {
812 .opcode = IB_WR_LOCAL_INV,
816 .ex.invalidate_rkey = req->mr->rkey,
819 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
820 wr.wr_cqe = &req->reg_cqe;
822 return ib_post_send(queue->qp, &wr, &bad_wr);
825 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
828 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
829 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
830 struct nvme_rdma_device *dev = queue->device;
831 struct ib_device *ibdev = dev->dev;
834 if (!blk_rq_bytes(rq))
837 if (req->need_inval) {
838 res = nvme_rdma_inv_rkey(queue, req);
840 dev_err(ctrl->ctrl.device,
841 "Queueing INV WR for rkey %#x failed (%d)\n",
843 nvme_rdma_error_recovery(queue->ctrl);
847 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
848 req->nents, rq_data_dir(rq) ==
849 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
851 nvme_cleanup_cmd(rq);
852 sg_free_table_chained(&req->sg_table, true);
855 static int nvme_rdma_set_sg_null(struct nvme_command *c)
857 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
860 put_unaligned_le24(0, sg->length);
861 put_unaligned_le32(0, sg->key);
862 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
866 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
867 struct nvme_rdma_request *req, struct nvme_command *c)
869 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
871 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
872 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
873 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
875 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
876 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
877 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
879 req->inline_data = true;
884 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
885 struct nvme_rdma_request *req, struct nvme_command *c)
887 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
889 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
890 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
891 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
892 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
896 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
897 struct nvme_rdma_request *req, struct nvme_command *c,
900 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
903 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE);
910 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
912 req->reg_cqe.done = nvme_rdma_memreg_done;
913 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
914 req->reg_wr.wr.opcode = IB_WR_REG_MR;
915 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
916 req->reg_wr.wr.num_sge = 0;
917 req->reg_wr.mr = req->mr;
918 req->reg_wr.key = req->mr->rkey;
919 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
920 IB_ACCESS_REMOTE_READ |
921 IB_ACCESS_REMOTE_WRITE;
923 req->need_inval = true;
925 sg->addr = cpu_to_le64(req->mr->iova);
926 put_unaligned_le24(req->mr->length, sg->length);
927 put_unaligned_le32(req->mr->rkey, sg->key);
928 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
929 NVME_SGL_FMT_INVALIDATE;
934 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
935 struct request *rq, unsigned int map_len,
936 struct nvme_command *c)
938 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
939 struct nvme_rdma_device *dev = queue->device;
940 struct ib_device *ibdev = dev->dev;
945 req->inline_data = false;
946 req->need_inval = false;
948 c->common.flags |= NVME_CMD_SGL_METABUF;
950 if (!blk_rq_bytes(rq))
951 return nvme_rdma_set_sg_null(c);
953 req->sg_table.sgl = req->first_sgl;
954 ret = sg_alloc_table_chained(&req->sg_table, rq->nr_phys_segments,
959 nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
960 BUG_ON(nents > rq->nr_phys_segments);
963 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, nents,
964 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
965 if (unlikely(count <= 0)) {
966 sg_free_table_chained(&req->sg_table, true);
971 if (rq_data_dir(rq) == WRITE &&
972 map_len <= nvme_rdma_inline_data_size(queue) &&
973 nvme_rdma_queue_idx(queue))
974 return nvme_rdma_map_sg_inline(queue, req, c);
976 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
977 return nvme_rdma_map_sg_single(queue, req, c);
980 return nvme_rdma_map_sg_fr(queue, req, c, count);
983 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
985 if (unlikely(wc->status != IB_WC_SUCCESS))
986 nvme_rdma_wr_error(cq, wc, "SEND");
989 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
990 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
991 struct ib_send_wr *first, bool flush)
993 struct ib_send_wr wr, *bad_wr;
997 sge->length = sizeof(struct nvme_command),
998 sge->lkey = queue->device->pd->local_dma_lkey;
1000 qe->cqe.done = nvme_rdma_send_done;
1003 wr.wr_cqe = &qe->cqe;
1005 wr.num_sge = num_sge;
1006 wr.opcode = IB_WR_SEND;
1010 * Unsignalled send completions are another giant desaster in the
1011 * IB Verbs spec: If we don't regularly post signalled sends
1012 * the send queue will fill up and only a QP reset will rescue us.
1013 * Would have been way to obvious to handle this in hardware or
1014 * at least the RDMA stack..
1016 * This messy and racy code sniplet is copy and pasted from the iSER
1017 * initiator, and the magic '32' comes from there as well.
1019 * Always signal the flushes. The magic request used for the flush
1020 * sequencer is not allocated in our driver's tagset and it's
1021 * triggered to be freed by blk_cleanup_queue(). So we need to
1022 * always mark it as signaled to ensure that the "wr_cqe", which is
1023 * embeded in request's payload, is not freed when __ib_process_cq()
1024 * calls wr_cqe->done().
1026 if ((++queue->sig_count % 32) == 0 || flush)
1027 wr.send_flags |= IB_SEND_SIGNALED;
1034 ret = ib_post_send(queue->qp, first, &bad_wr);
1036 dev_err(queue->ctrl->ctrl.device,
1037 "%s failed with error code %d\n", __func__, ret);
1042 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1043 struct nvme_rdma_qe *qe)
1045 struct ib_recv_wr wr, *bad_wr;
1049 list.addr = qe->dma;
1050 list.length = sizeof(struct nvme_completion);
1051 list.lkey = queue->device->pd->local_dma_lkey;
1053 qe->cqe.done = nvme_rdma_recv_done;
1056 wr.wr_cqe = &qe->cqe;
1060 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1062 dev_err(queue->ctrl->ctrl.device,
1063 "%s failed with error code %d\n", __func__, ret);
1068 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1070 u32 queue_idx = nvme_rdma_queue_idx(queue);
1073 return queue->ctrl->admin_tag_set.tags[queue_idx];
1074 return queue->ctrl->tag_set.tags[queue_idx - 1];
1077 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1079 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1080 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1081 struct ib_device *dev = queue->device->dev;
1082 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1083 struct nvme_command *cmd = sqe->data;
1087 if (WARN_ON_ONCE(aer_idx != 0))
1090 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1092 memset(cmd, 0, sizeof(*cmd));
1093 cmd->common.opcode = nvme_admin_async_event;
1094 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1095 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1096 nvme_rdma_set_sg_null(cmd);
1098 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1101 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1105 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1106 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1108 u16 status = le16_to_cpu(cqe->status);
1110 struct nvme_rdma_request *req;
1115 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1117 dev_err(queue->ctrl->ctrl.device,
1118 "tag 0x%x on QP %#x not found\n",
1119 cqe->command_id, queue->qp->qp_num);
1120 nvme_rdma_error_recovery(queue->ctrl);
1123 req = blk_mq_rq_to_pdu(rq);
1125 if (rq->cmd_type == REQ_TYPE_DRV_PRIV && rq->special)
1126 memcpy(rq->special, cqe, sizeof(*cqe));
1131 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1132 wc->ex.invalidate_rkey == req->mr->rkey)
1133 req->need_inval = false;
1135 blk_mq_complete_request(rq, status);
1140 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1142 struct nvme_rdma_qe *qe =
1143 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1144 struct nvme_rdma_queue *queue = cq->cq_context;
1145 struct ib_device *ibdev = queue->device->dev;
1146 struct nvme_completion *cqe = qe->data;
1147 const size_t len = sizeof(struct nvme_completion);
1150 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1151 nvme_rdma_wr_error(cq, wc, "RECV");
1155 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1157 * AEN requests are special as they don't time out and can
1158 * survive any kind of queue freeze and often don't respond to
1159 * aborts. We don't even bother to allocate a struct request
1160 * for them but rather special case them here.
1162 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1163 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
1164 nvme_complete_async_event(&queue->ctrl->ctrl, cqe);
1166 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1167 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1169 nvme_rdma_post_recv(queue, qe);
1173 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1175 __nvme_rdma_recv_done(cq, wc, -1);
1178 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1182 for (i = 0; i < queue->queue_size; i++) {
1183 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1185 goto out_destroy_queue_ib;
1190 out_destroy_queue_ib:
1191 nvme_rdma_destroy_queue_ib(queue);
1195 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1196 struct rdma_cm_event *ev)
1198 if (ev->param.conn.private_data_len) {
1199 struct nvme_rdma_cm_rej *rej =
1200 (struct nvme_rdma_cm_rej *)ev->param.conn.private_data;
1202 dev_err(queue->ctrl->ctrl.device,
1203 "Connect rejected, status %d.", le16_to_cpu(rej->sts));
1204 /* XXX: Think of something clever to do here... */
1206 dev_err(queue->ctrl->ctrl.device,
1207 "Connect rejected, no private data.\n");
1213 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1215 struct nvme_rdma_device *dev;
1218 dev = nvme_rdma_find_get_device(queue->cm_id);
1220 dev_err(queue->cm_id->device->dma_device,
1221 "no client data found!\n");
1222 return -ECONNREFUSED;
1225 ret = nvme_rdma_create_queue_ib(queue, dev);
1227 nvme_rdma_dev_put(dev);
1231 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1233 dev_err(queue->ctrl->ctrl.device,
1234 "rdma_resolve_route failed (%d).\n",
1236 goto out_destroy_queue;
1242 nvme_rdma_destroy_queue_ib(queue);
1247 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1249 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1250 struct rdma_conn_param param = { };
1251 struct nvme_rdma_cm_req priv = { };
1254 param.qp_num = queue->qp->qp_num;
1255 param.flow_control = 1;
1257 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1258 /* maximum retry count */
1259 param.retry_count = 7;
1260 param.rnr_retry_count = 7;
1261 param.private_data = &priv;
1262 param.private_data_len = sizeof(priv);
1264 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1265 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1266 priv.hrqsize = cpu_to_le16(queue->queue_size);
1267 priv.hsqsize = cpu_to_le16(queue->queue_size);
1269 ret = rdma_connect(queue->cm_id, ¶m);
1271 dev_err(ctrl->ctrl.device,
1272 "rdma_connect failed (%d).\n", ret);
1273 goto out_destroy_queue_ib;
1278 out_destroy_queue_ib:
1279 nvme_rdma_destroy_queue_ib(queue);
1284 * nvme_rdma_device_unplug() - Handle RDMA device unplug
1285 * @queue: Queue that owns the cm_id that caught the event
1287 * DEVICE_REMOVAL event notifies us that the RDMA device is about
1288 * to unplug so we should take care of destroying our RDMA resources.
1289 * This event will be generated for each allocated cm_id.
1291 * In our case, the RDMA resources are managed per controller and not
1292 * only per queue. So the way we handle this is we trigger an implicit
1293 * controller deletion upon the first DEVICE_REMOVAL event we see, and
1294 * hold the event inflight until the controller deletion is completed.
1296 * One exception that we need to handle is the destruction of the cm_id
1297 * that caught the event. Since we hold the callout until the controller
1298 * deletion is completed, we'll deadlock if the controller deletion will
1299 * call rdma_destroy_id on this queue's cm_id. Thus, we claim ownership
1300 * of destroying this queue before-hand, destroy the queue resources,
1301 * then queue the controller deletion which won't destroy this queue and
1302 * we destroy the cm_id implicitely by returning a non-zero rc to the callout.
1304 static int nvme_rdma_device_unplug(struct nvme_rdma_queue *queue)
1306 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1309 /* Own the controller deletion */
1310 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1313 dev_warn(ctrl->ctrl.device,
1314 "Got rdma device removal event, deleting ctrl\n");
1316 /* Get rid of reconnect work if its running */
1317 cancel_delayed_work_sync(&ctrl->reconnect_work);
1319 /* Disable the queue so ctrl delete won't free it */
1320 if (test_and_clear_bit(NVME_RDMA_Q_CONNECTED, &queue->flags)) {
1321 /* Free this queue ourselves */
1322 nvme_rdma_stop_queue(queue);
1323 nvme_rdma_destroy_queue_ib(queue);
1325 /* Return non-zero so the cm_id will destroy implicitly */
1329 /* Queue controller deletion */
1330 queue_work(nvme_rdma_wq, &ctrl->delete_work);
1331 flush_work(&ctrl->delete_work);
1335 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1336 struct rdma_cm_event *ev)
1338 struct nvme_rdma_queue *queue = cm_id->context;
1341 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1342 rdma_event_msg(ev->event), ev->event,
1345 switch (ev->event) {
1346 case RDMA_CM_EVENT_ADDR_RESOLVED:
1347 cm_error = nvme_rdma_addr_resolved(queue);
1349 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1350 cm_error = nvme_rdma_route_resolved(queue);
1352 case RDMA_CM_EVENT_ESTABLISHED:
1353 queue->cm_error = nvme_rdma_conn_established(queue);
1354 /* complete cm_done regardless of success/failure */
1355 complete(&queue->cm_done);
1357 case RDMA_CM_EVENT_REJECTED:
1358 cm_error = nvme_rdma_conn_rejected(queue, ev);
1360 case RDMA_CM_EVENT_ADDR_ERROR:
1361 case RDMA_CM_EVENT_ROUTE_ERROR:
1362 case RDMA_CM_EVENT_CONNECT_ERROR:
1363 case RDMA_CM_EVENT_UNREACHABLE:
1364 dev_dbg(queue->ctrl->ctrl.device,
1365 "CM error event %d\n", ev->event);
1366 cm_error = -ECONNRESET;
1368 case RDMA_CM_EVENT_DISCONNECTED:
1369 case RDMA_CM_EVENT_ADDR_CHANGE:
1370 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1371 dev_dbg(queue->ctrl->ctrl.device,
1372 "disconnect received - connection closed\n");
1373 nvme_rdma_error_recovery(queue->ctrl);
1375 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1376 /* return 1 means impliciy CM ID destroy */
1377 return nvme_rdma_device_unplug(queue);
1379 dev_err(queue->ctrl->ctrl.device,
1380 "Unexpected RDMA CM event (%d)\n", ev->event);
1381 nvme_rdma_error_recovery(queue->ctrl);
1386 queue->cm_error = cm_error;
1387 complete(&queue->cm_done);
1393 static enum blk_eh_timer_return
1394 nvme_rdma_timeout(struct request *rq, bool reserved)
1396 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1398 /* queue error recovery */
1399 nvme_rdma_error_recovery(req->queue->ctrl);
1401 /* fail with DNR on cmd timeout */
1402 rq->errors = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1404 return BLK_EH_HANDLED;
1407 static int nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1408 const struct blk_mq_queue_data *bd)
1410 struct nvme_ns *ns = hctx->queue->queuedata;
1411 struct nvme_rdma_queue *queue = hctx->driver_data;
1412 struct request *rq = bd->rq;
1413 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1414 struct nvme_rdma_qe *sqe = &req->sqe;
1415 struct nvme_command *c = sqe->data;
1417 struct ib_device *dev;
1418 unsigned int map_len;
1421 WARN_ON_ONCE(rq->tag < 0);
1423 dev = queue->device->dev;
1424 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1425 sizeof(struct nvme_command), DMA_TO_DEVICE);
1427 ret = nvme_setup_cmd(ns, rq, c);
1431 c->common.command_id = rq->tag;
1432 blk_mq_start_request(rq);
1434 map_len = nvme_map_len(rq);
1435 ret = nvme_rdma_map_data(queue, rq, map_len, c);
1437 dev_err(queue->ctrl->ctrl.device,
1438 "Failed to map data (%d)\n", ret);
1439 nvme_cleanup_cmd(rq);
1443 ib_dma_sync_single_for_device(dev, sqe->dma,
1444 sizeof(struct nvme_command), DMA_TO_DEVICE);
1446 if (rq->cmd_type == REQ_TYPE_FS && req_op(rq) == REQ_OP_FLUSH)
1448 ret = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1449 req->need_inval ? &req->reg_wr.wr : NULL, flush);
1451 nvme_rdma_unmap_data(queue, rq);
1455 return BLK_MQ_RQ_QUEUE_OK;
1457 return (ret == -ENOMEM || ret == -EAGAIN) ?
1458 BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR;
1461 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1463 struct nvme_rdma_queue *queue = hctx->driver_data;
1464 struct ib_cq *cq = queue->ib_cq;
1468 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1469 while (ib_poll_cq(cq, 1, &wc) > 0) {
1470 struct ib_cqe *cqe = wc.wr_cqe;
1473 if (cqe->done == nvme_rdma_recv_done)
1474 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1483 static void nvme_rdma_complete_rq(struct request *rq)
1485 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1486 struct nvme_rdma_queue *queue = req->queue;
1489 nvme_rdma_unmap_data(queue, rq);
1491 if (unlikely(rq->errors)) {
1492 if (nvme_req_needs_retry(rq, rq->errors)) {
1493 nvme_requeue_req(rq);
1497 if (rq->cmd_type == REQ_TYPE_DRV_PRIV)
1500 error = nvme_error_status(rq->errors);
1503 blk_mq_end_request(rq, error);
1506 static struct blk_mq_ops nvme_rdma_mq_ops = {
1507 .queue_rq = nvme_rdma_queue_rq,
1508 .complete = nvme_rdma_complete_rq,
1509 .map_queue = blk_mq_map_queue,
1510 .init_request = nvme_rdma_init_request,
1511 .exit_request = nvme_rdma_exit_request,
1512 .reinit_request = nvme_rdma_reinit_request,
1513 .init_hctx = nvme_rdma_init_hctx,
1514 .poll = nvme_rdma_poll,
1515 .timeout = nvme_rdma_timeout,
1518 static struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1519 .queue_rq = nvme_rdma_queue_rq,
1520 .complete = nvme_rdma_complete_rq,
1521 .map_queue = blk_mq_map_queue,
1522 .init_request = nvme_rdma_init_admin_request,
1523 .exit_request = nvme_rdma_exit_admin_request,
1524 .reinit_request = nvme_rdma_reinit_request,
1525 .init_hctx = nvme_rdma_init_admin_hctx,
1526 .timeout = nvme_rdma_timeout,
1529 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
1533 error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
1537 ctrl->device = ctrl->queues[0].device;
1540 * We need a reference on the device as long as the tag_set is alive,
1541 * as the MRs in the request structures need a valid ib_device.
1544 if (!nvme_rdma_dev_get(ctrl->device))
1545 goto out_free_queue;
1547 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
1548 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
1550 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1551 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
1552 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
1553 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
1554 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1555 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1556 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1557 ctrl->admin_tag_set.driver_data = ctrl;
1558 ctrl->admin_tag_set.nr_hw_queues = 1;
1559 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1561 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1565 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1566 if (IS_ERR(ctrl->ctrl.admin_q)) {
1567 error = PTR_ERR(ctrl->ctrl.admin_q);
1568 goto out_free_tagset;
1571 error = nvmf_connect_admin_queue(&ctrl->ctrl);
1573 goto out_cleanup_queue;
1575 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
1577 dev_err(ctrl->ctrl.device,
1578 "prop_get NVME_REG_CAP failed\n");
1579 goto out_cleanup_queue;
1583 min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize);
1585 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
1587 goto out_cleanup_queue;
1589 ctrl->ctrl.max_hw_sectors =
1590 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
1592 error = nvme_init_identify(&ctrl->ctrl);
1594 goto out_cleanup_queue;
1596 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
1597 &ctrl->async_event_sqe, sizeof(struct nvme_command),
1600 goto out_cleanup_queue;
1602 nvme_start_keep_alive(&ctrl->ctrl);
1607 blk_cleanup_queue(ctrl->ctrl.admin_q);
1609 /* disconnect and drain the queue before freeing the tagset */
1610 nvme_rdma_stop_queue(&ctrl->queues[0]);
1611 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1613 nvme_rdma_dev_put(ctrl->device);
1615 nvme_rdma_free_queue(&ctrl->queues[0]);
1619 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1621 nvme_stop_keep_alive(&ctrl->ctrl);
1622 cancel_work_sync(&ctrl->err_work);
1623 cancel_delayed_work_sync(&ctrl->reconnect_work);
1625 if (ctrl->queue_count > 1) {
1626 nvme_stop_queues(&ctrl->ctrl);
1627 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1628 nvme_cancel_request, &ctrl->ctrl);
1629 nvme_rdma_free_io_queues(ctrl);
1632 if (test_bit(NVME_RDMA_Q_CONNECTED, &ctrl->queues[0].flags))
1633 nvme_shutdown_ctrl(&ctrl->ctrl);
1635 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
1636 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1637 nvme_cancel_request, &ctrl->ctrl);
1638 nvme_rdma_destroy_admin_queue(ctrl);
1641 static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1643 nvme_uninit_ctrl(&ctrl->ctrl);
1645 nvme_rdma_shutdown_ctrl(ctrl);
1647 if (ctrl->ctrl.tagset) {
1648 blk_cleanup_queue(ctrl->ctrl.connect_q);
1649 blk_mq_free_tag_set(&ctrl->tag_set);
1650 nvme_rdma_dev_put(ctrl->device);
1653 nvme_put_ctrl(&ctrl->ctrl);
1656 static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1658 struct nvme_rdma_ctrl *ctrl = container_of(work,
1659 struct nvme_rdma_ctrl, delete_work);
1661 __nvme_rdma_remove_ctrl(ctrl, true);
1664 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1666 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1669 if (!queue_work(nvme_rdma_wq, &ctrl->delete_work))
1675 static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1677 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1680 ret = __nvme_rdma_del_ctrl(ctrl);
1684 flush_work(&ctrl->delete_work);
1689 static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1691 struct nvme_rdma_ctrl *ctrl = container_of(work,
1692 struct nvme_rdma_ctrl, delete_work);
1694 __nvme_rdma_remove_ctrl(ctrl, false);
1697 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1699 struct nvme_rdma_ctrl *ctrl = container_of(work,
1700 struct nvme_rdma_ctrl, reset_work);
1704 nvme_rdma_shutdown_ctrl(ctrl);
1706 ret = nvme_rdma_configure_admin_queue(ctrl);
1708 /* ctrl is already shutdown, just remove the ctrl */
1709 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1713 if (ctrl->queue_count > 1) {
1714 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
1718 ret = nvme_rdma_init_io_queues(ctrl);
1722 ret = nvme_rdma_connect_io_queues(ctrl);
1727 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1728 WARN_ON_ONCE(!changed);
1730 if (ctrl->queue_count > 1) {
1731 nvme_start_queues(&ctrl->ctrl);
1732 nvme_queue_scan(&ctrl->ctrl);
1733 nvme_queue_async_events(&ctrl->ctrl);
1739 /* Deleting this dead controller... */
1740 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1741 WARN_ON(!queue_work(nvme_rdma_wq, &ctrl->delete_work));
1744 static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl)
1746 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1748 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1751 if (!queue_work(nvme_rdma_wq, &ctrl->reset_work))
1754 flush_work(&ctrl->reset_work);
1759 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1761 .module = THIS_MODULE,
1763 .reg_read32 = nvmf_reg_read32,
1764 .reg_read64 = nvmf_reg_read64,
1765 .reg_write32 = nvmf_reg_write32,
1766 .reset_ctrl = nvme_rdma_reset_ctrl,
1767 .free_ctrl = nvme_rdma_free_ctrl,
1768 .submit_async_event = nvme_rdma_submit_async_event,
1769 .delete_ctrl = nvme_rdma_del_ctrl,
1770 .get_subsysnqn = nvmf_get_subsysnqn,
1771 .get_address = nvmf_get_address,
1774 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1776 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
1779 ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues);
1783 ctrl->queue_count = opts->nr_io_queues + 1;
1784 if (ctrl->queue_count < 2)
1787 dev_info(ctrl->ctrl.device,
1788 "creating %d I/O queues.\n", opts->nr_io_queues);
1790 ret = nvme_rdma_init_io_queues(ctrl);
1795 * We need a reference on the device as long as the tag_set is alive,
1796 * as the MRs in the request structures need a valid ib_device.
1799 if (!nvme_rdma_dev_get(ctrl->device))
1800 goto out_free_io_queues;
1802 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1803 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
1804 ctrl->tag_set.queue_depth = ctrl->ctrl.sqsize;
1805 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1806 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1807 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1808 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1809 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1810 ctrl->tag_set.driver_data = ctrl;
1811 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
1812 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1814 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1817 ctrl->ctrl.tagset = &ctrl->tag_set;
1819 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1820 if (IS_ERR(ctrl->ctrl.connect_q)) {
1821 ret = PTR_ERR(ctrl->ctrl.connect_q);
1822 goto out_free_tag_set;
1825 ret = nvme_rdma_connect_io_queues(ctrl);
1827 goto out_cleanup_connect_q;
1831 out_cleanup_connect_q:
1832 blk_cleanup_queue(ctrl->ctrl.connect_q);
1834 blk_mq_free_tag_set(&ctrl->tag_set);
1836 nvme_rdma_dev_put(ctrl->device);
1838 nvme_rdma_free_io_queues(ctrl);
1842 static int nvme_rdma_parse_ipaddr(struct sockaddr_in *in_addr, char *p)
1844 u8 *addr = (u8 *)&in_addr->sin_addr.s_addr;
1845 size_t buflen = strlen(p);
1847 /* XXX: handle IPv6 addresses */
1849 if (buflen > INET_ADDRSTRLEN)
1851 if (in4_pton(p, buflen, addr, '\0', NULL) == 0)
1853 in_addr->sin_family = AF_INET;
1857 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1858 struct nvmf_ctrl_options *opts)
1860 struct nvme_rdma_ctrl *ctrl;
1864 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1866 return ERR_PTR(-ENOMEM);
1867 ctrl->ctrl.opts = opts;
1868 INIT_LIST_HEAD(&ctrl->list);
1870 ret = nvme_rdma_parse_ipaddr(&ctrl->addr_in, opts->traddr);
1872 pr_err("malformed IP address passed: %s\n", opts->traddr);
1876 if (opts->mask & NVMF_OPT_TRSVCID) {
1879 ret = kstrtou16(opts->trsvcid, 0, &port);
1883 ctrl->addr_in.sin_port = cpu_to_be16(port);
1885 ctrl->addr_in.sin_port = cpu_to_be16(NVME_RDMA_IP_PORT);
1888 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1889 0 /* no quirks, we're perfect! */);
1893 ctrl->reconnect_delay = opts->reconnect_delay;
1894 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1895 nvme_rdma_reconnect_ctrl_work);
1896 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1897 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1898 INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work);
1899 spin_lock_init(&ctrl->lock);
1901 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1902 ctrl->ctrl.sqsize = opts->queue_size;
1903 ctrl->ctrl.kato = opts->kato;
1906 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues),
1909 goto out_uninit_ctrl;
1911 ret = nvme_rdma_configure_admin_queue(ctrl);
1913 goto out_kfree_queues;
1915 /* sanity check icdoff */
1916 if (ctrl->ctrl.icdoff) {
1917 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1918 goto out_remove_admin_queue;
1921 /* sanity check keyed sgls */
1922 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1923 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1924 goto out_remove_admin_queue;
1927 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1928 /* warn if maxcmd is lower than queue_size */
1929 dev_warn(ctrl->ctrl.device,
1930 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1931 opts->queue_size, ctrl->ctrl.maxcmd);
1932 opts->queue_size = ctrl->ctrl.maxcmd;
1935 if (opts->nr_io_queues) {
1936 ret = nvme_rdma_create_io_queues(ctrl);
1938 goto out_remove_admin_queue;
1941 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1942 WARN_ON_ONCE(!changed);
1944 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
1945 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1947 kref_get(&ctrl->ctrl.kref);
1949 mutex_lock(&nvme_rdma_ctrl_mutex);
1950 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1951 mutex_unlock(&nvme_rdma_ctrl_mutex);
1953 if (opts->nr_io_queues) {
1954 nvme_queue_scan(&ctrl->ctrl);
1955 nvme_queue_async_events(&ctrl->ctrl);
1960 out_remove_admin_queue:
1961 nvme_stop_keep_alive(&ctrl->ctrl);
1962 nvme_rdma_destroy_admin_queue(ctrl);
1964 kfree(ctrl->queues);
1966 nvme_uninit_ctrl(&ctrl->ctrl);
1967 nvme_put_ctrl(&ctrl->ctrl);
1970 return ERR_PTR(ret);
1973 return ERR_PTR(ret);
1976 static struct nvmf_transport_ops nvme_rdma_transport = {
1978 .required_opts = NVMF_OPT_TRADDR,
1979 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY,
1980 .create_ctrl = nvme_rdma_create_ctrl,
1983 static int __init nvme_rdma_init_module(void)
1985 nvme_rdma_wq = create_workqueue("nvme_rdma_wq");
1989 nvmf_register_transport(&nvme_rdma_transport);
1993 static void __exit nvme_rdma_cleanup_module(void)
1995 struct nvme_rdma_ctrl *ctrl;
1997 nvmf_unregister_transport(&nvme_rdma_transport);
1999 mutex_lock(&nvme_rdma_ctrl_mutex);
2000 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list)
2001 __nvme_rdma_del_ctrl(ctrl);
2002 mutex_unlock(&nvme_rdma_ctrl_mutex);
2004 destroy_workqueue(nvme_rdma_wq);
2007 module_init(nvme_rdma_init_module);
2008 module_exit(nvme_rdma_cleanup_module);
2010 MODULE_LICENSE("GPL v2");