2 * NVMe over Fabrics RDMA host code.
3 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/err.h>
19 #include <linux/string.h>
20 #include <linux/atomic.h>
21 #include <linux/blk-mq.h>
22 #include <linux/types.h>
23 #include <linux/list.h>
24 #include <linux/mutex.h>
25 #include <linux/scatterlist.h>
26 #include <linux/nvme.h>
27 #include <asm/unaligned.h>
29 #include <rdma/ib_verbs.h>
30 #include <rdma/rdma_cm.h>
31 #include <linux/nvme-rdma.h>
37 #define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
39 #define NVME_RDMA_MAX_SEGMENT_SIZE 0xffffff /* 24-bit SGL field */
41 #define NVME_RDMA_MAX_SEGMENTS 256
43 #define NVME_RDMA_MAX_INLINE_SEGMENTS 1
46 * We handle AEN commands ourselves and don't even let the
47 * block layer know about them.
49 #define NVME_RDMA_NR_AEN_COMMANDS 1
50 #define NVME_RDMA_AQ_BLKMQ_DEPTH \
51 (NVMF_AQ_DEPTH - NVME_RDMA_NR_AEN_COMMANDS)
53 struct nvme_rdma_device {
54 struct ib_device *dev;
57 struct list_head entry;
66 struct nvme_rdma_queue;
67 struct nvme_rdma_request {
68 struct nvme_request req;
70 struct nvme_rdma_qe sqe;
71 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
75 struct ib_reg_wr reg_wr;
76 struct ib_cqe reg_cqe;
77 struct nvme_rdma_queue *queue;
78 struct sg_table sg_table;
79 struct scatterlist first_sgl[];
82 enum nvme_rdma_queue_flags {
84 NVME_RDMA_Q_DELETING = 1,
87 struct nvme_rdma_queue {
88 struct nvme_rdma_qe *rsp_ring;
91 size_t cmnd_capsule_len;
92 struct nvme_rdma_ctrl *ctrl;
93 struct nvme_rdma_device *device;
98 struct rdma_cm_id *cm_id;
100 struct completion cm_done;
103 struct nvme_rdma_ctrl {
104 /* read only in the hot path */
105 struct nvme_rdma_queue *queues;
108 /* other member variables */
109 struct blk_mq_tag_set tag_set;
110 struct work_struct delete_work;
111 struct work_struct reset_work;
112 struct work_struct err_work;
114 struct nvme_rdma_qe async_event_sqe;
116 struct delayed_work reconnect_work;
118 struct list_head list;
120 struct blk_mq_tag_set admin_tag_set;
121 struct nvme_rdma_device *device;
126 struct sockaddr_storage addr;
127 struct sockaddr_storage src_addr;
129 struct nvme_ctrl ctrl;
132 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
134 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
137 static LIST_HEAD(device_list);
138 static DEFINE_MUTEX(device_list_mutex);
140 static LIST_HEAD(nvme_rdma_ctrl_list);
141 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
144 * Disabling this option makes small I/O goes faster, but is fundamentally
145 * unsafe. With it turned off we will have to register a global rkey that
146 * allows read and write access to all physical memory.
148 static bool register_always = true;
149 module_param(register_always, bool, 0444);
150 MODULE_PARM_DESC(register_always,
151 "Use memory registration even for contiguous memory regions");
153 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
154 struct rdma_cm_event *event);
155 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
157 /* XXX: really should move to a generic header sooner or later.. */
158 static inline void put_unaligned_le24(u32 val, u8 *p)
165 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
167 return queue - queue->ctrl->queues;
170 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
172 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
175 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
176 size_t capsule_size, enum dma_data_direction dir)
178 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
182 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
183 size_t capsule_size, enum dma_data_direction dir)
185 qe->data = kzalloc(capsule_size, GFP_KERNEL);
189 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
190 if (ib_dma_mapping_error(ibdev, qe->dma)) {
198 static void nvme_rdma_free_ring(struct ib_device *ibdev,
199 struct nvme_rdma_qe *ring, size_t ib_queue_size,
200 size_t capsule_size, enum dma_data_direction dir)
204 for (i = 0; i < ib_queue_size; i++)
205 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
209 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
210 size_t ib_queue_size, size_t capsule_size,
211 enum dma_data_direction dir)
213 struct nvme_rdma_qe *ring;
216 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
220 for (i = 0; i < ib_queue_size; i++) {
221 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
228 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
232 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
234 pr_debug("QP event %s (%d)\n",
235 ib_event_msg(event->event), event->event);
239 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
241 wait_for_completion_interruptible_timeout(&queue->cm_done,
242 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
243 return queue->cm_error;
246 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
248 struct nvme_rdma_device *dev = queue->device;
249 struct ib_qp_init_attr init_attr;
252 memset(&init_attr, 0, sizeof(init_attr));
253 init_attr.event_handler = nvme_rdma_qp_event;
255 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
257 init_attr.cap.max_recv_wr = queue->queue_size + 1;
258 init_attr.cap.max_recv_sge = 1;
259 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
260 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
261 init_attr.qp_type = IB_QPT_RC;
262 init_attr.send_cq = queue->ib_cq;
263 init_attr.recv_cq = queue->ib_cq;
265 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
267 queue->qp = queue->cm_id->qp;
271 static int nvme_rdma_reinit_request(void *data, struct request *rq)
273 struct nvme_rdma_ctrl *ctrl = data;
274 struct nvme_rdma_device *dev = ctrl->device;
275 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
278 if (!req->mr->need_inval)
281 ib_dereg_mr(req->mr);
283 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
285 if (IS_ERR(req->mr)) {
286 ret = PTR_ERR(req->mr);
291 req->mr->need_inval = false;
297 static void __nvme_rdma_exit_request(struct nvme_rdma_ctrl *ctrl,
298 struct request *rq, unsigned int queue_idx)
300 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
301 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
302 struct nvme_rdma_device *dev = queue->device;
305 ib_dereg_mr(req->mr);
307 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
311 static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
312 struct request *rq, unsigned int hctx_idx)
314 return __nvme_rdma_exit_request(set->driver_data, rq, hctx_idx + 1);
317 static void nvme_rdma_exit_admin_request(struct blk_mq_tag_set *set,
318 struct request *rq, unsigned int hctx_idx)
320 return __nvme_rdma_exit_request(set->driver_data, rq, 0);
323 static int __nvme_rdma_init_request(struct nvme_rdma_ctrl *ctrl,
324 struct request *rq, unsigned int queue_idx)
326 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
327 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
328 struct nvme_rdma_device *dev = queue->device;
329 struct ib_device *ibdev = dev->dev;
332 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
337 req->mr = ib_alloc_mr(dev->pd, IB_MR_TYPE_MEM_REG,
339 if (IS_ERR(req->mr)) {
340 ret = PTR_ERR(req->mr);
349 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
354 static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
355 struct request *rq, unsigned int hctx_idx,
356 unsigned int numa_node)
358 return __nvme_rdma_init_request(set->driver_data, rq, hctx_idx + 1);
361 static int nvme_rdma_init_admin_request(struct blk_mq_tag_set *set,
362 struct request *rq, unsigned int hctx_idx,
363 unsigned int numa_node)
365 return __nvme_rdma_init_request(set->driver_data, rq, 0);
368 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
369 unsigned int hctx_idx)
371 struct nvme_rdma_ctrl *ctrl = data;
372 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
374 BUG_ON(hctx_idx >= ctrl->queue_count);
376 hctx->driver_data = queue;
380 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
381 unsigned int hctx_idx)
383 struct nvme_rdma_ctrl *ctrl = data;
384 struct nvme_rdma_queue *queue = &ctrl->queues[0];
386 BUG_ON(hctx_idx != 0);
388 hctx->driver_data = queue;
392 static void nvme_rdma_free_dev(struct kref *ref)
394 struct nvme_rdma_device *ndev =
395 container_of(ref, struct nvme_rdma_device, ref);
397 mutex_lock(&device_list_mutex);
398 list_del(&ndev->entry);
399 mutex_unlock(&device_list_mutex);
401 ib_dealloc_pd(ndev->pd);
405 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
407 kref_put(&dev->ref, nvme_rdma_free_dev);
410 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
412 return kref_get_unless_zero(&dev->ref);
415 static struct nvme_rdma_device *
416 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
418 struct nvme_rdma_device *ndev;
420 mutex_lock(&device_list_mutex);
421 list_for_each_entry(ndev, &device_list, entry) {
422 if (ndev->dev->node_guid == cm_id->device->node_guid &&
423 nvme_rdma_dev_get(ndev))
427 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
431 ndev->dev = cm_id->device;
432 kref_init(&ndev->ref);
434 ndev->pd = ib_alloc_pd(ndev->dev,
435 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
436 if (IS_ERR(ndev->pd))
439 if (!(ndev->dev->attrs.device_cap_flags &
440 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
441 dev_err(&ndev->dev->dev,
442 "Memory registrations not supported.\n");
446 list_add(&ndev->entry, &device_list);
448 mutex_unlock(&device_list_mutex);
452 ib_dealloc_pd(ndev->pd);
456 mutex_unlock(&device_list_mutex);
460 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
462 struct nvme_rdma_device *dev;
463 struct ib_device *ibdev;
467 rdma_destroy_qp(queue->cm_id);
468 ib_free_cq(queue->ib_cq);
470 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
471 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
473 nvme_rdma_dev_put(dev);
476 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
478 struct ib_device *ibdev;
479 const int send_wr_factor = 3; /* MR, SEND, INV */
480 const int cq_factor = send_wr_factor + 1; /* + RECV */
481 int comp_vector, idx = nvme_rdma_queue_idx(queue);
484 queue->device = nvme_rdma_find_get_device(queue->cm_id);
485 if (!queue->device) {
486 dev_err(queue->cm_id->device->dev.parent,
487 "no client data found!\n");
488 return -ECONNREFUSED;
490 ibdev = queue->device->dev;
493 * The admin queue is barely used once the controller is live, so don't
494 * bother to spread it out.
499 comp_vector = idx % ibdev->num_comp_vectors;
502 /* +1 for ib_stop_cq */
503 queue->ib_cq = ib_alloc_cq(ibdev, queue,
504 cq_factor * queue->queue_size + 1,
505 comp_vector, IB_POLL_SOFTIRQ);
506 if (IS_ERR(queue->ib_cq)) {
507 ret = PTR_ERR(queue->ib_cq);
511 ret = nvme_rdma_create_qp(queue, send_wr_factor);
513 goto out_destroy_ib_cq;
515 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
516 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
517 if (!queue->rsp_ring) {
525 ib_destroy_qp(queue->qp);
527 ib_free_cq(queue->ib_cq);
529 nvme_rdma_dev_put(queue->device);
533 static int nvme_rdma_init_queue(struct nvme_rdma_ctrl *ctrl,
534 int idx, size_t queue_size)
536 struct nvme_rdma_queue *queue;
537 struct sockaddr *src_addr = NULL;
540 queue = &ctrl->queues[idx];
542 init_completion(&queue->cm_done);
545 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
547 queue->cmnd_capsule_len = sizeof(struct nvme_command);
549 queue->queue_size = queue_size;
551 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
552 RDMA_PS_TCP, IB_QPT_RC);
553 if (IS_ERR(queue->cm_id)) {
554 dev_info(ctrl->ctrl.device,
555 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
556 return PTR_ERR(queue->cm_id);
559 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
560 src_addr = (struct sockaddr *)&ctrl->src_addr;
562 queue->cm_error = -ETIMEDOUT;
563 ret = rdma_resolve_addr(queue->cm_id, src_addr,
564 (struct sockaddr *)&ctrl->addr,
565 NVME_RDMA_CONNECT_TIMEOUT_MS);
567 dev_info(ctrl->ctrl.device,
568 "rdma_resolve_addr failed (%d).\n", ret);
569 goto out_destroy_cm_id;
572 ret = nvme_rdma_wait_for_cm(queue);
574 dev_info(ctrl->ctrl.device,
575 "rdma_resolve_addr wait failed (%d).\n", ret);
576 goto out_destroy_cm_id;
579 clear_bit(NVME_RDMA_Q_DELETING, &queue->flags);
584 rdma_destroy_id(queue->cm_id);
588 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
590 rdma_disconnect(queue->cm_id);
591 ib_drain_qp(queue->qp);
594 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
596 nvme_rdma_destroy_queue_ib(queue);
597 rdma_destroy_id(queue->cm_id);
600 static void nvme_rdma_stop_and_free_queue(struct nvme_rdma_queue *queue)
602 if (test_and_set_bit(NVME_RDMA_Q_DELETING, &queue->flags))
604 nvme_rdma_stop_queue(queue);
605 nvme_rdma_free_queue(queue);
608 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
612 for (i = 1; i < ctrl->queue_count; i++)
613 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
616 static int nvme_rdma_connect_io_queues(struct nvme_rdma_ctrl *ctrl)
620 for (i = 1; i < ctrl->queue_count; i++) {
621 ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
623 dev_info(ctrl->ctrl.device,
624 "failed to connect i/o queue: %d\n", ret);
625 goto out_free_queues;
627 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
633 nvme_rdma_free_io_queues(ctrl);
637 static int nvme_rdma_init_io_queues(struct nvme_rdma_ctrl *ctrl)
639 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
640 unsigned int nr_io_queues;
643 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
644 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
648 ctrl->queue_count = nr_io_queues + 1;
649 if (ctrl->queue_count < 2)
652 dev_info(ctrl->ctrl.device,
653 "creating %d I/O queues.\n", nr_io_queues);
655 for (i = 1; i < ctrl->queue_count; i++) {
656 ret = nvme_rdma_init_queue(ctrl, i,
657 ctrl->ctrl.opts->queue_size);
659 dev_info(ctrl->ctrl.device,
660 "failed to initialize i/o queue: %d\n", ret);
661 goto out_free_queues;
668 for (i--; i >= 1; i--)
669 nvme_rdma_stop_and_free_queue(&ctrl->queues[i]);
674 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl)
676 nvme_rdma_free_qe(ctrl->queues[0].device->dev, &ctrl->async_event_sqe,
677 sizeof(struct nvme_command), DMA_TO_DEVICE);
678 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
679 blk_cleanup_queue(ctrl->ctrl.admin_q);
680 blk_mq_free_tag_set(&ctrl->admin_tag_set);
681 nvme_rdma_dev_put(ctrl->device);
684 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
686 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
688 if (list_empty(&ctrl->list))
691 mutex_lock(&nvme_rdma_ctrl_mutex);
692 list_del(&ctrl->list);
693 mutex_unlock(&nvme_rdma_ctrl_mutex);
696 nvmf_free_options(nctrl->opts);
701 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
703 /* If we are resetting/deleting then do nothing */
704 if (ctrl->ctrl.state != NVME_CTRL_RECONNECTING) {
705 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
706 ctrl->ctrl.state == NVME_CTRL_LIVE);
710 if (nvmf_should_reconnect(&ctrl->ctrl)) {
711 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
712 ctrl->ctrl.opts->reconnect_delay);
713 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
714 ctrl->ctrl.opts->reconnect_delay * HZ);
716 dev_info(ctrl->ctrl.device, "Removing controller...\n");
717 queue_work(nvme_wq, &ctrl->delete_work);
721 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
723 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
724 struct nvme_rdma_ctrl, reconnect_work);
728 ++ctrl->ctrl.nr_reconnects;
730 if (ctrl->queue_count > 1) {
731 nvme_rdma_free_io_queues(ctrl);
733 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
738 nvme_rdma_stop_and_free_queue(&ctrl->queues[0]);
740 ret = blk_mq_reinit_tagset(&ctrl->admin_tag_set);
744 ret = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
748 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
752 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
754 ret = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
758 nvme_start_keep_alive(&ctrl->ctrl);
760 if (ctrl->queue_count > 1) {
761 ret = nvme_rdma_init_io_queues(ctrl);
765 ret = nvme_rdma_connect_io_queues(ctrl);
770 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
771 WARN_ON_ONCE(!changed);
772 ctrl->ctrl.nr_reconnects = 0;
774 if (ctrl->queue_count > 1) {
775 nvme_queue_scan(&ctrl->ctrl);
776 nvme_queue_async_events(&ctrl->ctrl);
779 dev_info(ctrl->ctrl.device, "Successfully reconnected\n");
784 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
785 ctrl->ctrl.nr_reconnects);
786 nvme_rdma_reconnect_or_remove(ctrl);
789 static void nvme_rdma_error_recovery_work(struct work_struct *work)
791 struct nvme_rdma_ctrl *ctrl = container_of(work,
792 struct nvme_rdma_ctrl, err_work);
795 nvme_stop_keep_alive(&ctrl->ctrl);
797 for (i = 0; i < ctrl->queue_count; i++)
798 clear_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[i].flags);
800 if (ctrl->queue_count > 1)
801 nvme_stop_queues(&ctrl->ctrl);
802 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
804 /* We must take care of fastfail/requeue all our inflight requests */
805 if (ctrl->queue_count > 1)
806 blk_mq_tagset_busy_iter(&ctrl->tag_set,
807 nvme_cancel_request, &ctrl->ctrl);
808 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
809 nvme_cancel_request, &ctrl->ctrl);
812 * queues are not a live anymore, so restart the queues to fail fast
815 blk_mq_start_stopped_hw_queues(ctrl->ctrl.admin_q, true);
816 nvme_start_queues(&ctrl->ctrl);
818 nvme_rdma_reconnect_or_remove(ctrl);
821 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
823 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RECONNECTING))
826 queue_work(nvme_wq, &ctrl->err_work);
829 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
832 struct nvme_rdma_queue *queue = cq->cq_context;
833 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
835 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
836 dev_info(ctrl->ctrl.device,
837 "%s for CQE 0x%p failed with status %s (%d)\n",
839 ib_wc_status_msg(wc->status), wc->status);
840 nvme_rdma_error_recovery(ctrl);
843 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
845 if (unlikely(wc->status != IB_WC_SUCCESS))
846 nvme_rdma_wr_error(cq, wc, "MEMREG");
849 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
851 if (unlikely(wc->status != IB_WC_SUCCESS))
852 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
855 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
856 struct nvme_rdma_request *req)
858 struct ib_send_wr *bad_wr;
859 struct ib_send_wr wr = {
860 .opcode = IB_WR_LOCAL_INV,
864 .ex.invalidate_rkey = req->mr->rkey,
867 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
868 wr.wr_cqe = &req->reg_cqe;
870 return ib_post_send(queue->qp, &wr, &bad_wr);
873 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
876 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
877 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
878 struct nvme_rdma_device *dev = queue->device;
879 struct ib_device *ibdev = dev->dev;
882 if (!blk_rq_bytes(rq))
885 if (req->mr->need_inval) {
886 res = nvme_rdma_inv_rkey(queue, req);
888 dev_err(ctrl->ctrl.device,
889 "Queueing INV WR for rkey %#x failed (%d)\n",
891 nvme_rdma_error_recovery(queue->ctrl);
895 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
896 req->nents, rq_data_dir(rq) ==
897 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
899 nvme_cleanup_cmd(rq);
900 sg_free_table_chained(&req->sg_table, true);
903 static int nvme_rdma_set_sg_null(struct nvme_command *c)
905 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
908 put_unaligned_le24(0, sg->length);
909 put_unaligned_le32(0, sg->key);
910 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
914 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
915 struct nvme_rdma_request *req, struct nvme_command *c)
917 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
919 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
920 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
921 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
923 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
924 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
925 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
927 req->inline_data = true;
932 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
933 struct nvme_rdma_request *req, struct nvme_command *c)
935 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
937 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
938 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
939 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
940 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
944 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
945 struct nvme_rdma_request *req, struct nvme_command *c,
948 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
951 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, PAGE_SIZE);
958 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
960 req->reg_cqe.done = nvme_rdma_memreg_done;
961 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
962 req->reg_wr.wr.opcode = IB_WR_REG_MR;
963 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
964 req->reg_wr.wr.num_sge = 0;
965 req->reg_wr.mr = req->mr;
966 req->reg_wr.key = req->mr->rkey;
967 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
968 IB_ACCESS_REMOTE_READ |
969 IB_ACCESS_REMOTE_WRITE;
971 req->mr->need_inval = true;
973 sg->addr = cpu_to_le64(req->mr->iova);
974 put_unaligned_le24(req->mr->length, sg->length);
975 put_unaligned_le32(req->mr->rkey, sg->key);
976 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
977 NVME_SGL_FMT_INVALIDATE;
982 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
983 struct request *rq, struct nvme_command *c)
985 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
986 struct nvme_rdma_device *dev = queue->device;
987 struct ib_device *ibdev = dev->dev;
991 req->inline_data = false;
992 req->mr->need_inval = false;
994 c->common.flags |= NVME_CMD_SGL_METABUF;
996 if (!blk_rq_bytes(rq))
997 return nvme_rdma_set_sg_null(c);
999 req->sg_table.sgl = req->first_sgl;
1000 ret = sg_alloc_table_chained(&req->sg_table,
1001 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
1005 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
1007 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
1008 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1009 if (unlikely(count <= 0)) {
1010 sg_free_table_chained(&req->sg_table, true);
1015 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1016 blk_rq_payload_bytes(rq) <=
1017 nvme_rdma_inline_data_size(queue))
1018 return nvme_rdma_map_sg_inline(queue, req, c);
1020 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
1021 return nvme_rdma_map_sg_single(queue, req, c);
1024 return nvme_rdma_map_sg_fr(queue, req, c, count);
1027 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1029 if (unlikely(wc->status != IB_WC_SUCCESS))
1030 nvme_rdma_wr_error(cq, wc, "SEND");
1033 static inline int nvme_rdma_queue_sig_limit(struct nvme_rdma_queue *queue)
1038 * We signal completion every queue depth/2 and also handle the
1039 * degenerated case of a device with queue_depth=1, where we
1040 * would need to signal every message.
1042 sig_limit = max(queue->queue_size / 2, 1);
1043 return (++queue->sig_count % sig_limit) == 0;
1046 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1047 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1048 struct ib_send_wr *first, bool flush)
1050 struct ib_send_wr wr, *bad_wr;
1053 sge->addr = qe->dma;
1054 sge->length = sizeof(struct nvme_command),
1055 sge->lkey = queue->device->pd->local_dma_lkey;
1057 qe->cqe.done = nvme_rdma_send_done;
1060 wr.wr_cqe = &qe->cqe;
1062 wr.num_sge = num_sge;
1063 wr.opcode = IB_WR_SEND;
1067 * Unsignalled send completions are another giant desaster in the
1068 * IB Verbs spec: If we don't regularly post signalled sends
1069 * the send queue will fill up and only a QP reset will rescue us.
1070 * Would have been way to obvious to handle this in hardware or
1071 * at least the RDMA stack..
1073 * Always signal the flushes. The magic request used for the flush
1074 * sequencer is not allocated in our driver's tagset and it's
1075 * triggered to be freed by blk_cleanup_queue(). So we need to
1076 * always mark it as signaled to ensure that the "wr_cqe", which is
1077 * embedded in request's payload, is not freed when __ib_process_cq()
1078 * calls wr_cqe->done().
1080 if (nvme_rdma_queue_sig_limit(queue) || flush)
1081 wr.send_flags |= IB_SEND_SIGNALED;
1088 ret = ib_post_send(queue->qp, first, &bad_wr);
1090 dev_err(queue->ctrl->ctrl.device,
1091 "%s failed with error code %d\n", __func__, ret);
1096 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1097 struct nvme_rdma_qe *qe)
1099 struct ib_recv_wr wr, *bad_wr;
1103 list.addr = qe->dma;
1104 list.length = sizeof(struct nvme_completion);
1105 list.lkey = queue->device->pd->local_dma_lkey;
1107 qe->cqe.done = nvme_rdma_recv_done;
1110 wr.wr_cqe = &qe->cqe;
1114 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1116 dev_err(queue->ctrl->ctrl.device,
1117 "%s failed with error code %d\n", __func__, ret);
1122 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1124 u32 queue_idx = nvme_rdma_queue_idx(queue);
1127 return queue->ctrl->admin_tag_set.tags[queue_idx];
1128 return queue->ctrl->tag_set.tags[queue_idx - 1];
1131 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
1133 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1134 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1135 struct ib_device *dev = queue->device->dev;
1136 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1137 struct nvme_command *cmd = sqe->data;
1141 if (WARN_ON_ONCE(aer_idx != 0))
1144 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1146 memset(cmd, 0, sizeof(*cmd));
1147 cmd->common.opcode = nvme_admin_async_event;
1148 cmd->common.command_id = NVME_RDMA_AQ_BLKMQ_DEPTH;
1149 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1150 nvme_rdma_set_sg_null(cmd);
1152 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1155 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL, false);
1159 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1160 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1163 struct nvme_rdma_request *req;
1166 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1168 dev_err(queue->ctrl->ctrl.device,
1169 "tag 0x%x on QP %#x not found\n",
1170 cqe->command_id, queue->qp->qp_num);
1171 nvme_rdma_error_recovery(queue->ctrl);
1174 req = blk_mq_rq_to_pdu(rq);
1179 if ((wc->wc_flags & IB_WC_WITH_INVALIDATE) &&
1180 wc->ex.invalidate_rkey == req->mr->rkey)
1181 req->mr->need_inval = false;
1183 nvme_end_request(rq, cqe->status, cqe->result);
1187 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1189 struct nvme_rdma_qe *qe =
1190 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1191 struct nvme_rdma_queue *queue = cq->cq_context;
1192 struct ib_device *ibdev = queue->device->dev;
1193 struct nvme_completion *cqe = qe->data;
1194 const size_t len = sizeof(struct nvme_completion);
1197 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1198 nvme_rdma_wr_error(cq, wc, "RECV");
1202 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1204 * AEN requests are special as they don't time out and can
1205 * survive any kind of queue freeze and often don't respond to
1206 * aborts. We don't even bother to allocate a struct request
1207 * for them but rather special case them here.
1209 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1210 cqe->command_id >= NVME_RDMA_AQ_BLKMQ_DEPTH))
1211 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1214 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1215 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1217 nvme_rdma_post_recv(queue, qe);
1221 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1223 __nvme_rdma_recv_done(cq, wc, -1);
1226 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1230 for (i = 0; i < queue->queue_size; i++) {
1231 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1233 goto out_destroy_queue_ib;
1238 out_destroy_queue_ib:
1239 nvme_rdma_destroy_queue_ib(queue);
1243 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1244 struct rdma_cm_event *ev)
1246 struct rdma_cm_id *cm_id = queue->cm_id;
1247 int status = ev->status;
1248 const char *rej_msg;
1249 const struct nvme_rdma_cm_rej *rej_data;
1252 rej_msg = rdma_reject_msg(cm_id, status);
1253 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1255 if (rej_data && rej_data_len >= sizeof(u16)) {
1256 u16 sts = le16_to_cpu(rej_data->sts);
1258 dev_err(queue->ctrl->ctrl.device,
1259 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1260 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1262 dev_err(queue->ctrl->ctrl.device,
1263 "Connect rejected: status %d (%s).\n", status, rej_msg);
1269 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1273 ret = nvme_rdma_create_queue_ib(queue);
1277 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1279 dev_err(queue->ctrl->ctrl.device,
1280 "rdma_resolve_route failed (%d).\n",
1282 goto out_destroy_queue;
1288 nvme_rdma_destroy_queue_ib(queue);
1292 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1294 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1295 struct rdma_conn_param param = { };
1296 struct nvme_rdma_cm_req priv = { };
1299 param.qp_num = queue->qp->qp_num;
1300 param.flow_control = 1;
1302 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1303 /* maximum retry count */
1304 param.retry_count = 7;
1305 param.rnr_retry_count = 7;
1306 param.private_data = &priv;
1307 param.private_data_len = sizeof(priv);
1309 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1310 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1312 * set the admin queue depth to the minimum size
1313 * specified by the Fabrics standard.
1315 if (priv.qid == 0) {
1316 priv.hrqsize = cpu_to_le16(NVMF_AQ_DEPTH);
1317 priv.hsqsize = cpu_to_le16(NVMF_AQ_DEPTH - 1);
1320 * current interpretation of the fabrics spec
1321 * is at minimum you make hrqsize sqsize+1, or a
1322 * 1's based representation of sqsize.
1324 priv.hrqsize = cpu_to_le16(queue->queue_size);
1325 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1328 ret = rdma_connect(queue->cm_id, ¶m);
1330 dev_err(ctrl->ctrl.device,
1331 "rdma_connect failed (%d).\n", ret);
1332 goto out_destroy_queue_ib;
1337 out_destroy_queue_ib:
1338 nvme_rdma_destroy_queue_ib(queue);
1342 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1343 struct rdma_cm_event *ev)
1345 struct nvme_rdma_queue *queue = cm_id->context;
1348 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1349 rdma_event_msg(ev->event), ev->event,
1352 switch (ev->event) {
1353 case RDMA_CM_EVENT_ADDR_RESOLVED:
1354 cm_error = nvme_rdma_addr_resolved(queue);
1356 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1357 cm_error = nvme_rdma_route_resolved(queue);
1359 case RDMA_CM_EVENT_ESTABLISHED:
1360 queue->cm_error = nvme_rdma_conn_established(queue);
1361 /* complete cm_done regardless of success/failure */
1362 complete(&queue->cm_done);
1364 case RDMA_CM_EVENT_REJECTED:
1365 nvme_rdma_destroy_queue_ib(queue);
1366 cm_error = nvme_rdma_conn_rejected(queue, ev);
1368 case RDMA_CM_EVENT_ROUTE_ERROR:
1369 case RDMA_CM_EVENT_CONNECT_ERROR:
1370 case RDMA_CM_EVENT_UNREACHABLE:
1371 nvme_rdma_destroy_queue_ib(queue);
1372 case RDMA_CM_EVENT_ADDR_ERROR:
1373 dev_dbg(queue->ctrl->ctrl.device,
1374 "CM error event %d\n", ev->event);
1375 cm_error = -ECONNRESET;
1377 case RDMA_CM_EVENT_DISCONNECTED:
1378 case RDMA_CM_EVENT_ADDR_CHANGE:
1379 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1380 dev_dbg(queue->ctrl->ctrl.device,
1381 "disconnect received - connection closed\n");
1382 nvme_rdma_error_recovery(queue->ctrl);
1384 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1385 /* device removal is handled via the ib_client API */
1388 dev_err(queue->ctrl->ctrl.device,
1389 "Unexpected RDMA CM event (%d)\n", ev->event);
1390 nvme_rdma_error_recovery(queue->ctrl);
1395 queue->cm_error = cm_error;
1396 complete(&queue->cm_done);
1402 static enum blk_eh_timer_return
1403 nvme_rdma_timeout(struct request *rq, bool reserved)
1405 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1407 /* queue error recovery */
1408 nvme_rdma_error_recovery(req->queue->ctrl);
1410 /* fail with DNR on cmd timeout */
1411 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1413 return BLK_EH_HANDLED;
1417 * We cannot accept any other command until the Connect command has completed.
1419 static inline blk_status_t
1420 nvme_rdma_queue_is_ready(struct nvme_rdma_queue *queue, struct request *rq)
1422 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags))) {
1423 struct nvme_command *cmd = nvme_req(rq)->cmd;
1425 if (!blk_rq_is_passthrough(rq) ||
1426 cmd->common.opcode != nvme_fabrics_command ||
1427 cmd->fabrics.fctype != nvme_fabrics_type_connect) {
1429 * reconnecting state means transport disruption, which
1430 * can take a long time and even might fail permanently,
1431 * so we can't let incoming I/O be requeued forever.
1432 * fail it fast to allow upper layers a chance to
1435 if (queue->ctrl->ctrl.state == NVME_CTRL_RECONNECTING)
1436 return BLK_STS_IOERR;
1437 return BLK_STS_RESOURCE; /* try again later */
1444 static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1445 const struct blk_mq_queue_data *bd)
1447 struct nvme_ns *ns = hctx->queue->queuedata;
1448 struct nvme_rdma_queue *queue = hctx->driver_data;
1449 struct request *rq = bd->rq;
1450 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1451 struct nvme_rdma_qe *sqe = &req->sqe;
1452 struct nvme_command *c = sqe->data;
1454 struct ib_device *dev;
1458 WARN_ON_ONCE(rq->tag < 0);
1460 ret = nvme_rdma_queue_is_ready(queue, rq);
1464 dev = queue->device->dev;
1465 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1466 sizeof(struct nvme_command), DMA_TO_DEVICE);
1468 ret = nvme_setup_cmd(ns, rq, c);
1472 blk_mq_start_request(rq);
1474 err = nvme_rdma_map_data(queue, rq, c);
1476 dev_err(queue->ctrl->ctrl.device,
1477 "Failed to map data (%d)\n", err);
1478 nvme_cleanup_cmd(rq);
1482 ib_dma_sync_single_for_device(dev, sqe->dma,
1483 sizeof(struct nvme_command), DMA_TO_DEVICE);
1485 if (req_op(rq) == REQ_OP_FLUSH)
1487 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1488 req->mr->need_inval ? &req->reg_wr.wr : NULL, flush);
1490 nvme_rdma_unmap_data(queue, rq);
1496 if (err == -ENOMEM || err == -EAGAIN)
1497 return BLK_STS_RESOURCE;
1498 return BLK_STS_IOERR;
1501 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1503 struct nvme_rdma_queue *queue = hctx->driver_data;
1504 struct ib_cq *cq = queue->ib_cq;
1508 while (ib_poll_cq(cq, 1, &wc) > 0) {
1509 struct ib_cqe *cqe = wc.wr_cqe;
1512 if (cqe->done == nvme_rdma_recv_done)
1513 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1522 static void nvme_rdma_complete_rq(struct request *rq)
1524 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1526 nvme_rdma_unmap_data(req->queue, rq);
1527 nvme_complete_rq(rq);
1530 static const struct blk_mq_ops nvme_rdma_mq_ops = {
1531 .queue_rq = nvme_rdma_queue_rq,
1532 .complete = nvme_rdma_complete_rq,
1533 .init_request = nvme_rdma_init_request,
1534 .exit_request = nvme_rdma_exit_request,
1535 .reinit_request = nvme_rdma_reinit_request,
1536 .init_hctx = nvme_rdma_init_hctx,
1537 .poll = nvme_rdma_poll,
1538 .timeout = nvme_rdma_timeout,
1541 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1542 .queue_rq = nvme_rdma_queue_rq,
1543 .complete = nvme_rdma_complete_rq,
1544 .init_request = nvme_rdma_init_admin_request,
1545 .exit_request = nvme_rdma_exit_admin_request,
1546 .reinit_request = nvme_rdma_reinit_request,
1547 .init_hctx = nvme_rdma_init_admin_hctx,
1548 .timeout = nvme_rdma_timeout,
1551 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl)
1555 error = nvme_rdma_init_queue(ctrl, 0, NVMF_AQ_DEPTH);
1559 ctrl->device = ctrl->queues[0].device;
1562 * We need a reference on the device as long as the tag_set is alive,
1563 * as the MRs in the request structures need a valid ib_device.
1566 if (!nvme_rdma_dev_get(ctrl->device))
1567 goto out_free_queue;
1569 ctrl->max_fr_pages = min_t(u32, NVME_RDMA_MAX_SEGMENTS,
1570 ctrl->device->dev->attrs.max_fast_reg_page_list_len);
1572 memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
1573 ctrl->admin_tag_set.ops = &nvme_rdma_admin_mq_ops;
1574 ctrl->admin_tag_set.queue_depth = NVME_RDMA_AQ_BLKMQ_DEPTH;
1575 ctrl->admin_tag_set.reserved_tags = 2; /* connect + keep-alive */
1576 ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
1577 ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1578 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1579 ctrl->admin_tag_set.driver_data = ctrl;
1580 ctrl->admin_tag_set.nr_hw_queues = 1;
1581 ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
1583 error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
1587 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
1588 if (IS_ERR(ctrl->ctrl.admin_q)) {
1589 error = PTR_ERR(ctrl->ctrl.admin_q);
1590 goto out_free_tagset;
1593 error = nvmf_connect_admin_queue(&ctrl->ctrl);
1595 goto out_cleanup_queue;
1597 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags);
1599 error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
1601 dev_err(ctrl->ctrl.device,
1602 "prop_get NVME_REG_CAP failed\n");
1603 goto out_cleanup_queue;
1607 min_t(int, NVME_CAP_MQES(ctrl->cap), ctrl->ctrl.sqsize);
1609 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
1611 goto out_cleanup_queue;
1613 ctrl->ctrl.max_hw_sectors =
1614 (ctrl->max_fr_pages - 1) << (PAGE_SHIFT - 9);
1616 error = nvme_init_identify(&ctrl->ctrl);
1618 goto out_cleanup_queue;
1620 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
1621 &ctrl->async_event_sqe, sizeof(struct nvme_command),
1624 goto out_cleanup_queue;
1626 nvme_start_keep_alive(&ctrl->ctrl);
1631 blk_cleanup_queue(ctrl->ctrl.admin_q);
1633 /* disconnect and drain the queue before freeing the tagset */
1634 nvme_rdma_stop_queue(&ctrl->queues[0]);
1635 blk_mq_free_tag_set(&ctrl->admin_tag_set);
1637 nvme_rdma_dev_put(ctrl->device);
1639 nvme_rdma_free_queue(&ctrl->queues[0]);
1643 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl)
1645 nvme_stop_keep_alive(&ctrl->ctrl);
1646 cancel_work_sync(&ctrl->err_work);
1647 cancel_delayed_work_sync(&ctrl->reconnect_work);
1649 if (ctrl->queue_count > 1) {
1650 nvme_stop_queues(&ctrl->ctrl);
1651 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1652 nvme_cancel_request, &ctrl->ctrl);
1653 nvme_rdma_free_io_queues(ctrl);
1656 if (test_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[0].flags))
1657 nvme_shutdown_ctrl(&ctrl->ctrl);
1659 blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
1660 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1661 nvme_cancel_request, &ctrl->ctrl);
1662 nvme_rdma_destroy_admin_queue(ctrl);
1665 static void __nvme_rdma_remove_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1667 nvme_uninit_ctrl(&ctrl->ctrl);
1669 nvme_rdma_shutdown_ctrl(ctrl);
1671 if (ctrl->ctrl.tagset) {
1672 blk_cleanup_queue(ctrl->ctrl.connect_q);
1673 blk_mq_free_tag_set(&ctrl->tag_set);
1674 nvme_rdma_dev_put(ctrl->device);
1677 nvme_put_ctrl(&ctrl->ctrl);
1680 static void nvme_rdma_del_ctrl_work(struct work_struct *work)
1682 struct nvme_rdma_ctrl *ctrl = container_of(work,
1683 struct nvme_rdma_ctrl, delete_work);
1685 __nvme_rdma_remove_ctrl(ctrl, true);
1688 static int __nvme_rdma_del_ctrl(struct nvme_rdma_ctrl *ctrl)
1690 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
1693 if (!queue_work(nvme_wq, &ctrl->delete_work))
1699 static int nvme_rdma_del_ctrl(struct nvme_ctrl *nctrl)
1701 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1705 * Keep a reference until all work is flushed since
1706 * __nvme_rdma_del_ctrl can free the ctrl mem
1708 if (!kref_get_unless_zero(&ctrl->ctrl.kref))
1710 ret = __nvme_rdma_del_ctrl(ctrl);
1712 flush_work(&ctrl->delete_work);
1713 nvme_put_ctrl(&ctrl->ctrl);
1717 static void nvme_rdma_remove_ctrl_work(struct work_struct *work)
1719 struct nvme_rdma_ctrl *ctrl = container_of(work,
1720 struct nvme_rdma_ctrl, delete_work);
1722 __nvme_rdma_remove_ctrl(ctrl, false);
1725 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1727 struct nvme_rdma_ctrl *ctrl = container_of(work,
1728 struct nvme_rdma_ctrl, reset_work);
1732 nvme_rdma_shutdown_ctrl(ctrl);
1734 ret = nvme_rdma_configure_admin_queue(ctrl);
1736 /* ctrl is already shutdown, just remove the ctrl */
1737 INIT_WORK(&ctrl->delete_work, nvme_rdma_remove_ctrl_work);
1741 if (ctrl->queue_count > 1) {
1742 ret = blk_mq_reinit_tagset(&ctrl->tag_set);
1746 ret = nvme_rdma_init_io_queues(ctrl);
1750 ret = nvme_rdma_connect_io_queues(ctrl);
1755 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1756 WARN_ON_ONCE(!changed);
1758 if (ctrl->queue_count > 1) {
1759 nvme_start_queues(&ctrl->ctrl);
1760 nvme_queue_scan(&ctrl->ctrl);
1761 nvme_queue_async_events(&ctrl->ctrl);
1767 /* Deleting this dead controller... */
1768 dev_warn(ctrl->ctrl.device, "Removing after reset failure\n");
1769 WARN_ON(!queue_work(nvme_wq, &ctrl->delete_work));
1772 static int nvme_rdma_reset_ctrl(struct nvme_ctrl *nctrl)
1774 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
1776 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1779 if (!queue_work(nvme_wq, &ctrl->reset_work))
1782 flush_work(&ctrl->reset_work);
1787 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1789 .module = THIS_MODULE,
1790 .flags = NVME_F_FABRICS,
1791 .reg_read32 = nvmf_reg_read32,
1792 .reg_read64 = nvmf_reg_read64,
1793 .reg_write32 = nvmf_reg_write32,
1794 .reset_ctrl = nvme_rdma_reset_ctrl,
1795 .free_ctrl = nvme_rdma_free_ctrl,
1796 .submit_async_event = nvme_rdma_submit_async_event,
1797 .delete_ctrl = nvme_rdma_del_ctrl,
1798 .get_subsysnqn = nvmf_get_subsysnqn,
1799 .get_address = nvmf_get_address,
1802 static int nvme_rdma_create_io_queues(struct nvme_rdma_ctrl *ctrl)
1806 ret = nvme_rdma_init_io_queues(ctrl);
1811 * We need a reference on the device as long as the tag_set is alive,
1812 * as the MRs in the request structures need a valid ib_device.
1815 if (!nvme_rdma_dev_get(ctrl->device))
1816 goto out_free_io_queues;
1818 memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
1819 ctrl->tag_set.ops = &nvme_rdma_mq_ops;
1820 ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
1821 ctrl->tag_set.reserved_tags = 1; /* fabric connect */
1822 ctrl->tag_set.numa_node = NUMA_NO_NODE;
1823 ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1824 ctrl->tag_set.cmd_size = sizeof(struct nvme_rdma_request) +
1825 SG_CHUNK_SIZE * sizeof(struct scatterlist);
1826 ctrl->tag_set.driver_data = ctrl;
1827 ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
1828 ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
1830 ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
1833 ctrl->ctrl.tagset = &ctrl->tag_set;
1835 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
1836 if (IS_ERR(ctrl->ctrl.connect_q)) {
1837 ret = PTR_ERR(ctrl->ctrl.connect_q);
1838 goto out_free_tag_set;
1841 ret = nvme_rdma_connect_io_queues(ctrl);
1843 goto out_cleanup_connect_q;
1847 out_cleanup_connect_q:
1848 blk_cleanup_queue(ctrl->ctrl.connect_q);
1850 blk_mq_free_tag_set(&ctrl->tag_set);
1852 nvme_rdma_dev_put(ctrl->device);
1854 nvme_rdma_free_io_queues(ctrl);
1858 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1859 struct nvmf_ctrl_options *opts)
1861 struct nvme_rdma_ctrl *ctrl;
1866 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1868 return ERR_PTR(-ENOMEM);
1869 ctrl->ctrl.opts = opts;
1870 INIT_LIST_HEAD(&ctrl->list);
1872 if (opts->mask & NVMF_OPT_TRSVCID)
1873 port = opts->trsvcid;
1875 port = __stringify(NVME_RDMA_IP_PORT);
1877 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1878 opts->traddr, port, &ctrl->addr);
1880 pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
1884 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
1885 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1886 opts->host_traddr, NULL, &ctrl->src_addr);
1888 pr_err("malformed src address passed: %s\n",
1894 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1895 0 /* no quirks, we're perfect! */);
1899 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1900 nvme_rdma_reconnect_ctrl_work);
1901 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1902 INIT_WORK(&ctrl->delete_work, nvme_rdma_del_ctrl_work);
1903 INIT_WORK(&ctrl->reset_work, nvme_rdma_reset_ctrl_work);
1905 ctrl->queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1906 ctrl->ctrl.sqsize = opts->queue_size - 1;
1907 ctrl->ctrl.kato = opts->kato;
1910 ctrl->queues = kcalloc(ctrl->queue_count, sizeof(*ctrl->queues),
1913 goto out_uninit_ctrl;
1915 ret = nvme_rdma_configure_admin_queue(ctrl);
1917 goto out_kfree_queues;
1919 /* sanity check icdoff */
1920 if (ctrl->ctrl.icdoff) {
1921 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1922 goto out_remove_admin_queue;
1925 /* sanity check keyed sgls */
1926 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1927 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1928 goto out_remove_admin_queue;
1931 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1932 /* warn if maxcmd is lower than queue_size */
1933 dev_warn(ctrl->ctrl.device,
1934 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1935 opts->queue_size, ctrl->ctrl.maxcmd);
1936 opts->queue_size = ctrl->ctrl.maxcmd;
1939 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1940 /* warn if sqsize is lower than queue_size */
1941 dev_warn(ctrl->ctrl.device,
1942 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1943 opts->queue_size, ctrl->ctrl.sqsize + 1);
1944 opts->queue_size = ctrl->ctrl.sqsize + 1;
1947 if (opts->nr_io_queues) {
1948 ret = nvme_rdma_create_io_queues(ctrl);
1950 goto out_remove_admin_queue;
1953 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1954 WARN_ON_ONCE(!changed);
1956 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
1957 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1959 kref_get(&ctrl->ctrl.kref);
1961 mutex_lock(&nvme_rdma_ctrl_mutex);
1962 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1963 mutex_unlock(&nvme_rdma_ctrl_mutex);
1965 if (opts->nr_io_queues) {
1966 nvme_queue_scan(&ctrl->ctrl);
1967 nvme_queue_async_events(&ctrl->ctrl);
1972 out_remove_admin_queue:
1973 nvme_stop_keep_alive(&ctrl->ctrl);
1974 nvme_rdma_destroy_admin_queue(ctrl);
1976 kfree(ctrl->queues);
1978 nvme_uninit_ctrl(&ctrl->ctrl);
1979 nvme_put_ctrl(&ctrl->ctrl);
1982 return ERR_PTR(ret);
1985 return ERR_PTR(ret);
1988 static struct nvmf_transport_ops nvme_rdma_transport = {
1990 .required_opts = NVMF_OPT_TRADDR,
1991 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
1992 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
1993 .create_ctrl = nvme_rdma_create_ctrl,
1996 static void nvme_rdma_add_one(struct ib_device *ib_device)
2000 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2002 struct nvme_rdma_ctrl *ctrl;
2004 /* Delete all controllers using this device */
2005 mutex_lock(&nvme_rdma_ctrl_mutex);
2006 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2007 if (ctrl->device->dev != ib_device)
2009 dev_info(ctrl->ctrl.device,
2010 "Removing ctrl: NQN \"%s\", addr %pISp\n",
2011 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2012 __nvme_rdma_del_ctrl(ctrl);
2014 mutex_unlock(&nvme_rdma_ctrl_mutex);
2016 flush_workqueue(nvme_wq);
2019 static struct ib_client nvme_rdma_ib_client = {
2020 .name = "nvme_rdma",
2021 .add = nvme_rdma_add_one,
2022 .remove = nvme_rdma_remove_one
2025 static int __init nvme_rdma_init_module(void)
2029 ret = ib_register_client(&nvme_rdma_ib_client);
2033 ret = nvmf_register_transport(&nvme_rdma_transport);
2035 goto err_unreg_client;
2040 ib_unregister_client(&nvme_rdma_ib_client);
2044 static void __exit nvme_rdma_cleanup_module(void)
2046 nvmf_unregister_transport(&nvme_rdma_transport);
2047 ib_unregister_client(&nvme_rdma_ib_client);
2050 module_init(nvme_rdma_init_module);
2051 module_exit(nvme_rdma_cleanup_module);
2053 MODULE_LICENSE("GPL v2");