perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / nvme / host / rdma.c
blobd181cafedc584916d0b04db2e08dd9e0802cba0c
1 /*
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
12 * more details.
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 <rdma/mr_pool.h>
19 #include <linux/err.h>
20 #include <linux/string.h>
21 #include <linux/atomic.h>
22 #include <linux/blk-mq.h>
23 #include <linux/blk-mq-rdma.h>
24 #include <linux/types.h>
25 #include <linux/list.h>
26 #include <linux/mutex.h>
27 #include <linux/scatterlist.h>
28 #include <linux/nvme.h>
29 #include <asm/unaligned.h>
31 #include <rdma/ib_verbs.h>
32 #include <rdma/rdma_cm.h>
33 #include <linux/nvme-rdma.h>
35 #include "nvme.h"
36 #include "fabrics.h"
39 #define NVME_RDMA_CONNECT_TIMEOUT_MS 3000 /* 3 second */
41 #define NVME_RDMA_MAX_SEGMENTS 256
43 #define NVME_RDMA_MAX_INLINE_SEGMENTS 4
45 struct nvme_rdma_device {
46 struct ib_device *dev;
47 struct ib_pd *pd;
48 struct kref ref;
49 struct list_head entry;
50 unsigned int num_inline_segments;
53 struct nvme_rdma_qe {
54 struct ib_cqe cqe;
55 void *data;
56 u64 dma;
59 struct nvme_rdma_queue;
60 struct nvme_rdma_request {
61 struct nvme_request req;
62 struct ib_mr *mr;
63 struct nvme_rdma_qe sqe;
64 union nvme_result result;
65 __le16 status;
66 refcount_t ref;
67 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
68 u32 num_sge;
69 int nents;
70 struct ib_reg_wr reg_wr;
71 struct ib_cqe reg_cqe;
72 struct nvme_rdma_queue *queue;
73 struct sg_table sg_table;
74 struct scatterlist first_sgl[];
77 enum nvme_rdma_queue_flags {
78 NVME_RDMA_Q_ALLOCATED = 0,
79 NVME_RDMA_Q_LIVE = 1,
80 NVME_RDMA_Q_TR_READY = 2,
83 struct nvme_rdma_queue {
84 struct nvme_rdma_qe *rsp_ring;
85 int queue_size;
86 size_t cmnd_capsule_len;
87 struct nvme_rdma_ctrl *ctrl;
88 struct nvme_rdma_device *device;
89 struct ib_cq *ib_cq;
90 struct ib_qp *qp;
92 unsigned long flags;
93 struct rdma_cm_id *cm_id;
94 int cm_error;
95 struct completion cm_done;
98 struct nvme_rdma_ctrl {
99 /* read only in the hot path */
100 struct nvme_rdma_queue *queues;
102 /* other member variables */
103 struct blk_mq_tag_set tag_set;
104 struct work_struct err_work;
106 struct nvme_rdma_qe async_event_sqe;
108 struct delayed_work reconnect_work;
110 struct list_head list;
112 struct blk_mq_tag_set admin_tag_set;
113 struct nvme_rdma_device *device;
115 u32 max_fr_pages;
117 struct sockaddr_storage addr;
118 struct sockaddr_storage src_addr;
120 struct nvme_ctrl ctrl;
121 bool use_inline_data;
124 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
126 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
129 static LIST_HEAD(device_list);
130 static DEFINE_MUTEX(device_list_mutex);
132 static LIST_HEAD(nvme_rdma_ctrl_list);
133 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
136 * Disabling this option makes small I/O goes faster, but is fundamentally
137 * unsafe. With it turned off we will have to register a global rkey that
138 * allows read and write access to all physical memory.
140 static bool register_always = true;
141 module_param(register_always, bool, 0444);
142 MODULE_PARM_DESC(register_always,
143 "Use memory registration even for contiguous memory regions");
145 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
146 struct rdma_cm_event *event);
147 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
149 static const struct blk_mq_ops nvme_rdma_mq_ops;
150 static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
152 /* XXX: really should move to a generic header sooner or later.. */
153 static inline void put_unaligned_le24(u32 val, u8 *p)
155 *p++ = val;
156 *p++ = val >> 8;
157 *p++ = val >> 16;
160 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
162 return queue - queue->ctrl->queues;
165 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
167 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
170 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
171 size_t capsule_size, enum dma_data_direction dir)
173 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
174 kfree(qe->data);
177 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
178 size_t capsule_size, enum dma_data_direction dir)
180 qe->data = kzalloc(capsule_size, GFP_KERNEL);
181 if (!qe->data)
182 return -ENOMEM;
184 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
185 if (ib_dma_mapping_error(ibdev, qe->dma)) {
186 kfree(qe->data);
187 return -ENOMEM;
190 return 0;
193 static void nvme_rdma_free_ring(struct ib_device *ibdev,
194 struct nvme_rdma_qe *ring, size_t ib_queue_size,
195 size_t capsule_size, enum dma_data_direction dir)
197 int i;
199 for (i = 0; i < ib_queue_size; i++)
200 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
201 kfree(ring);
204 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
205 size_t ib_queue_size, size_t capsule_size,
206 enum dma_data_direction dir)
208 struct nvme_rdma_qe *ring;
209 int i;
211 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
212 if (!ring)
213 return NULL;
215 for (i = 0; i < ib_queue_size; i++) {
216 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
217 goto out_free_ring;
220 return ring;
222 out_free_ring:
223 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
224 return NULL;
227 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
229 pr_debug("QP event %s (%d)\n",
230 ib_event_msg(event->event), event->event);
234 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
236 int ret;
238 ret = wait_for_completion_interruptible_timeout(&queue->cm_done,
239 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
240 if (ret < 0)
241 return ret;
242 if (ret == 0)
243 return -ETIMEDOUT;
244 WARN_ON_ONCE(queue->cm_error > 0);
245 return queue->cm_error;
248 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
250 struct nvme_rdma_device *dev = queue->device;
251 struct ib_qp_init_attr init_attr;
252 int ret;
254 memset(&init_attr, 0, sizeof(init_attr));
255 init_attr.event_handler = nvme_rdma_qp_event;
256 /* +1 for drain */
257 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
258 /* +1 for drain */
259 init_attr.cap.max_recv_wr = queue->queue_size + 1;
260 init_attr.cap.max_recv_sge = 1;
261 init_attr.cap.max_send_sge = 1 + dev->num_inline_segments;
262 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
263 init_attr.qp_type = IB_QPT_RC;
264 init_attr.send_cq = queue->ib_cq;
265 init_attr.recv_cq = queue->ib_cq;
267 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
269 queue->qp = queue->cm_id->qp;
270 return ret;
273 static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
274 struct request *rq, unsigned int hctx_idx)
276 struct nvme_rdma_ctrl *ctrl = set->driver_data;
277 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
278 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
279 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
280 struct nvme_rdma_device *dev = queue->device;
282 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
283 DMA_TO_DEVICE);
286 static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
287 struct request *rq, unsigned int hctx_idx,
288 unsigned int numa_node)
290 struct nvme_rdma_ctrl *ctrl = set->driver_data;
291 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
292 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
293 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
294 struct nvme_rdma_device *dev = queue->device;
295 struct ib_device *ibdev = dev->dev;
296 int ret;
298 nvme_req(rq)->ctrl = &ctrl->ctrl;
299 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
300 DMA_TO_DEVICE);
301 if (ret)
302 return ret;
304 req->queue = queue;
306 return 0;
309 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
310 unsigned int hctx_idx)
312 struct nvme_rdma_ctrl *ctrl = data;
313 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
315 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
317 hctx->driver_data = queue;
318 return 0;
321 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
322 unsigned int hctx_idx)
324 struct nvme_rdma_ctrl *ctrl = data;
325 struct nvme_rdma_queue *queue = &ctrl->queues[0];
327 BUG_ON(hctx_idx != 0);
329 hctx->driver_data = queue;
330 return 0;
333 static void nvme_rdma_free_dev(struct kref *ref)
335 struct nvme_rdma_device *ndev =
336 container_of(ref, struct nvme_rdma_device, ref);
338 mutex_lock(&device_list_mutex);
339 list_del(&ndev->entry);
340 mutex_unlock(&device_list_mutex);
342 ib_dealloc_pd(ndev->pd);
343 kfree(ndev);
346 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
348 kref_put(&dev->ref, nvme_rdma_free_dev);
351 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
353 return kref_get_unless_zero(&dev->ref);
356 static struct nvme_rdma_device *
357 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
359 struct nvme_rdma_device *ndev;
361 mutex_lock(&device_list_mutex);
362 list_for_each_entry(ndev, &device_list, entry) {
363 if (ndev->dev->node_guid == cm_id->device->node_guid &&
364 nvme_rdma_dev_get(ndev))
365 goto out_unlock;
368 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
369 if (!ndev)
370 goto out_err;
372 ndev->dev = cm_id->device;
373 kref_init(&ndev->ref);
375 ndev->pd = ib_alloc_pd(ndev->dev,
376 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
377 if (IS_ERR(ndev->pd))
378 goto out_free_dev;
380 if (!(ndev->dev->attrs.device_cap_flags &
381 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
382 dev_err(&ndev->dev->dev,
383 "Memory registrations not supported.\n");
384 goto out_free_pd;
387 ndev->num_inline_segments = min(NVME_RDMA_MAX_INLINE_SEGMENTS,
388 ndev->dev->attrs.max_send_sge - 1);
389 list_add(&ndev->entry, &device_list);
390 out_unlock:
391 mutex_unlock(&device_list_mutex);
392 return ndev;
394 out_free_pd:
395 ib_dealloc_pd(ndev->pd);
396 out_free_dev:
397 kfree(ndev);
398 out_err:
399 mutex_unlock(&device_list_mutex);
400 return NULL;
403 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
405 struct nvme_rdma_device *dev;
406 struct ib_device *ibdev;
408 if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags))
409 return;
411 dev = queue->device;
412 ibdev = dev->dev;
414 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
417 * The cm_id object might have been destroyed during RDMA connection
418 * establishment error flow to avoid getting other cma events, thus
419 * the destruction of the QP shouldn't use rdma_cm API.
421 ib_destroy_qp(queue->qp);
422 ib_free_cq(queue->ib_cq);
424 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
425 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
427 nvme_rdma_dev_put(dev);
430 static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev)
432 return min_t(u32, NVME_RDMA_MAX_SEGMENTS,
433 ibdev->attrs.max_fast_reg_page_list_len);
436 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
438 struct ib_device *ibdev;
439 const int send_wr_factor = 3; /* MR, SEND, INV */
440 const int cq_factor = send_wr_factor + 1; /* + RECV */
441 int comp_vector, idx = nvme_rdma_queue_idx(queue);
442 int ret;
444 queue->device = nvme_rdma_find_get_device(queue->cm_id);
445 if (!queue->device) {
446 dev_err(queue->cm_id->device->dev.parent,
447 "no client data found!\n");
448 return -ECONNREFUSED;
450 ibdev = queue->device->dev;
453 * Spread I/O queues completion vectors according their queue index.
454 * Admin queues can always go on completion vector 0.
456 comp_vector = idx == 0 ? idx : idx - 1;
458 /* +1 for ib_stop_cq */
459 queue->ib_cq = ib_alloc_cq(ibdev, queue,
460 cq_factor * queue->queue_size + 1,
461 comp_vector, IB_POLL_SOFTIRQ);
462 if (IS_ERR(queue->ib_cq)) {
463 ret = PTR_ERR(queue->ib_cq);
464 goto out_put_dev;
467 ret = nvme_rdma_create_qp(queue, send_wr_factor);
468 if (ret)
469 goto out_destroy_ib_cq;
471 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
472 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
473 if (!queue->rsp_ring) {
474 ret = -ENOMEM;
475 goto out_destroy_qp;
478 ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs,
479 queue->queue_size,
480 IB_MR_TYPE_MEM_REG,
481 nvme_rdma_get_max_fr_pages(ibdev));
482 if (ret) {
483 dev_err(queue->ctrl->ctrl.device,
484 "failed to initialize MR pool sized %d for QID %d\n",
485 queue->queue_size, idx);
486 goto out_destroy_ring;
489 set_bit(NVME_RDMA_Q_TR_READY, &queue->flags);
491 return 0;
493 out_destroy_ring:
494 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
495 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
496 out_destroy_qp:
497 rdma_destroy_qp(queue->cm_id);
498 out_destroy_ib_cq:
499 ib_free_cq(queue->ib_cq);
500 out_put_dev:
501 nvme_rdma_dev_put(queue->device);
502 return ret;
505 static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
506 int idx, size_t queue_size)
508 struct nvme_rdma_queue *queue;
509 struct sockaddr *src_addr = NULL;
510 int ret;
512 queue = &ctrl->queues[idx];
513 queue->ctrl = ctrl;
514 init_completion(&queue->cm_done);
516 if (idx > 0)
517 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
518 else
519 queue->cmnd_capsule_len = sizeof(struct nvme_command);
521 queue->queue_size = queue_size;
523 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
524 RDMA_PS_TCP, IB_QPT_RC);
525 if (IS_ERR(queue->cm_id)) {
526 dev_info(ctrl->ctrl.device,
527 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
528 return PTR_ERR(queue->cm_id);
531 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
532 src_addr = (struct sockaddr *)&ctrl->src_addr;
534 queue->cm_error = -ETIMEDOUT;
535 ret = rdma_resolve_addr(queue->cm_id, src_addr,
536 (struct sockaddr *)&ctrl->addr,
537 NVME_RDMA_CONNECT_TIMEOUT_MS);
538 if (ret) {
539 dev_info(ctrl->ctrl.device,
540 "rdma_resolve_addr failed (%d).\n", ret);
541 goto out_destroy_cm_id;
544 ret = nvme_rdma_wait_for_cm(queue);
545 if (ret) {
546 dev_info(ctrl->ctrl.device,
547 "rdma connection establishment failed (%d)\n", ret);
548 goto out_destroy_cm_id;
551 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
553 return 0;
555 out_destroy_cm_id:
556 rdma_destroy_id(queue->cm_id);
557 nvme_rdma_destroy_queue_ib(queue);
558 return ret;
561 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
563 if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
564 return;
566 rdma_disconnect(queue->cm_id);
567 ib_drain_qp(queue->qp);
570 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
572 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
573 return;
575 nvme_rdma_destroy_queue_ib(queue);
576 rdma_destroy_id(queue->cm_id);
579 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
581 int i;
583 for (i = 1; i < ctrl->ctrl.queue_count; i++)
584 nvme_rdma_free_queue(&ctrl->queues[i]);
587 static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
589 int i;
591 for (i = 1; i < ctrl->ctrl.queue_count; i++)
592 nvme_rdma_stop_queue(&ctrl->queues[i]);
595 static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
597 int ret;
599 if (idx)
600 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx);
601 else
602 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
604 if (!ret)
605 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[idx].flags);
606 else
607 dev_info(ctrl->ctrl.device,
608 "failed to connect queue: %d ret=%d\n", idx, ret);
609 return ret;
612 static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
614 int i, ret = 0;
616 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
617 ret = nvme_rdma_start_queue(ctrl, i);
618 if (ret)
619 goto out_stop_queues;
622 return 0;
624 out_stop_queues:
625 for (i--; i >= 1; i--)
626 nvme_rdma_stop_queue(&ctrl->queues[i]);
627 return ret;
630 static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
632 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
633 struct ib_device *ibdev = ctrl->device->dev;
634 unsigned int nr_io_queues;
635 int i, ret;
637 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
640 * we map queues according to the device irq vectors for
641 * optimal locality so we don't need more queues than
642 * completion vectors.
644 nr_io_queues = min_t(unsigned int, nr_io_queues,
645 ibdev->num_comp_vectors);
647 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
648 if (ret)
649 return ret;
651 ctrl->ctrl.queue_count = nr_io_queues + 1;
652 if (ctrl->ctrl.queue_count < 2)
653 return 0;
655 dev_info(ctrl->ctrl.device,
656 "creating %d I/O queues.\n", nr_io_queues);
658 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
659 ret = nvme_rdma_alloc_queue(ctrl, i,
660 ctrl->ctrl.sqsize + 1);
661 if (ret)
662 goto out_free_queues;
665 return 0;
667 out_free_queues:
668 for (i--; i >= 1; i--)
669 nvme_rdma_free_queue(&ctrl->queues[i]);
671 return ret;
674 static void nvme_rdma_free_tagset(struct nvme_ctrl *nctrl,
675 struct blk_mq_tag_set *set)
677 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
679 blk_mq_free_tag_set(set);
680 nvme_rdma_dev_put(ctrl->device);
683 static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
684 bool admin)
686 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
687 struct blk_mq_tag_set *set;
688 int ret;
690 if (admin) {
691 set = &ctrl->admin_tag_set;
692 memset(set, 0, sizeof(*set));
693 set->ops = &nvme_rdma_admin_mq_ops;
694 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
695 set->reserved_tags = 2; /* connect + keep-alive */
696 set->numa_node = NUMA_NO_NODE;
697 set->cmd_size = sizeof(struct nvme_rdma_request) +
698 SG_CHUNK_SIZE * sizeof(struct scatterlist);
699 set->driver_data = ctrl;
700 set->nr_hw_queues = 1;
701 set->timeout = ADMIN_TIMEOUT;
702 set->flags = BLK_MQ_F_NO_SCHED;
703 } else {
704 set = &ctrl->tag_set;
705 memset(set, 0, sizeof(*set));
706 set->ops = &nvme_rdma_mq_ops;
707 set->queue_depth = nctrl->sqsize + 1;
708 set->reserved_tags = 1; /* fabric connect */
709 set->numa_node = NUMA_NO_NODE;
710 set->flags = BLK_MQ_F_SHOULD_MERGE;
711 set->cmd_size = sizeof(struct nvme_rdma_request) +
712 SG_CHUNK_SIZE * sizeof(struct scatterlist);
713 set->driver_data = ctrl;
714 set->nr_hw_queues = nctrl->queue_count - 1;
715 set->timeout = NVME_IO_TIMEOUT;
718 ret = blk_mq_alloc_tag_set(set);
719 if (ret)
720 goto out;
723 * We need a reference on the device as long as the tag_set is alive,
724 * as the MRs in the request structures need a valid ib_device.
726 ret = nvme_rdma_dev_get(ctrl->device);
727 if (!ret) {
728 ret = -EINVAL;
729 goto out_free_tagset;
732 return set;
734 out_free_tagset:
735 blk_mq_free_tag_set(set);
736 out:
737 return ERR_PTR(ret);
740 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
741 bool remove)
743 if (remove) {
744 blk_cleanup_queue(ctrl->ctrl.admin_q);
745 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
747 if (ctrl->async_event_sqe.data) {
748 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
749 sizeof(struct nvme_command), DMA_TO_DEVICE);
750 ctrl->async_event_sqe.data = NULL;
752 nvme_rdma_free_queue(&ctrl->queues[0]);
755 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
756 bool new)
758 int error;
760 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
761 if (error)
762 return error;
764 ctrl->device = ctrl->queues[0].device;
766 ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev);
768 error = nvme_rdma_alloc_qe(ctrl->device->dev, &ctrl->async_event_sqe,
769 sizeof(struct nvme_command), DMA_TO_DEVICE);
770 if (error)
771 goto out_free_queue;
773 if (new) {
774 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
775 if (IS_ERR(ctrl->ctrl.admin_tagset)) {
776 error = PTR_ERR(ctrl->ctrl.admin_tagset);
777 goto out_free_async_qe;
780 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
781 if (IS_ERR(ctrl->ctrl.admin_q)) {
782 error = PTR_ERR(ctrl->ctrl.admin_q);
783 goto out_free_tagset;
787 error = nvme_rdma_start_queue(ctrl, 0);
788 if (error)
789 goto out_cleanup_queue;
791 error = ctrl->ctrl.ops->reg_read64(&ctrl->ctrl, NVME_REG_CAP,
792 &ctrl->ctrl.cap);
793 if (error) {
794 dev_err(ctrl->ctrl.device,
795 "prop_get NVME_REG_CAP failed\n");
796 goto out_stop_queue;
799 ctrl->ctrl.sqsize =
800 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
802 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
803 if (error)
804 goto out_stop_queue;
806 ctrl->ctrl.max_hw_sectors =
807 (ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9);
809 error = nvme_init_identify(&ctrl->ctrl);
810 if (error)
811 goto out_stop_queue;
813 return 0;
815 out_stop_queue:
816 nvme_rdma_stop_queue(&ctrl->queues[0]);
817 out_cleanup_queue:
818 if (new)
819 blk_cleanup_queue(ctrl->ctrl.admin_q);
820 out_free_tagset:
821 if (new)
822 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
823 out_free_async_qe:
824 nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
825 sizeof(struct nvme_command), DMA_TO_DEVICE);
826 out_free_queue:
827 nvme_rdma_free_queue(&ctrl->queues[0]);
828 return error;
831 static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
832 bool remove)
834 if (remove) {
835 blk_cleanup_queue(ctrl->ctrl.connect_q);
836 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
838 nvme_rdma_free_io_queues(ctrl);
841 static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
843 int ret;
845 ret = nvme_rdma_alloc_io_queues(ctrl);
846 if (ret)
847 return ret;
849 if (new) {
850 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
851 if (IS_ERR(ctrl->ctrl.tagset)) {
852 ret = PTR_ERR(ctrl->ctrl.tagset);
853 goto out_free_io_queues;
856 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
857 if (IS_ERR(ctrl->ctrl.connect_q)) {
858 ret = PTR_ERR(ctrl->ctrl.connect_q);
859 goto out_free_tag_set;
861 } else {
862 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
863 ctrl->ctrl.queue_count - 1);
866 ret = nvme_rdma_start_io_queues(ctrl);
867 if (ret)
868 goto out_cleanup_connect_q;
870 return 0;
872 out_cleanup_connect_q:
873 if (new)
874 blk_cleanup_queue(ctrl->ctrl.connect_q);
875 out_free_tag_set:
876 if (new)
877 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
878 out_free_io_queues:
879 nvme_rdma_free_io_queues(ctrl);
880 return ret;
883 static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl *ctrl,
884 bool remove)
886 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
887 nvme_rdma_stop_queue(&ctrl->queues[0]);
888 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set, nvme_cancel_request,
889 &ctrl->ctrl);
890 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
891 nvme_rdma_destroy_admin_queue(ctrl, remove);
894 static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl *ctrl,
895 bool remove)
897 if (ctrl->ctrl.queue_count > 1) {
898 nvme_stop_queues(&ctrl->ctrl);
899 nvme_rdma_stop_io_queues(ctrl);
900 blk_mq_tagset_busy_iter(&ctrl->tag_set, nvme_cancel_request,
901 &ctrl->ctrl);
902 if (remove)
903 nvme_start_queues(&ctrl->ctrl);
904 nvme_rdma_destroy_io_queues(ctrl, remove);
908 static void nvme_rdma_stop_ctrl(struct nvme_ctrl *nctrl)
910 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
912 cancel_work_sync(&ctrl->err_work);
913 cancel_delayed_work_sync(&ctrl->reconnect_work);
916 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
918 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
920 if (list_empty(&ctrl->list))
921 goto free_ctrl;
923 mutex_lock(&nvme_rdma_ctrl_mutex);
924 list_del(&ctrl->list);
925 mutex_unlock(&nvme_rdma_ctrl_mutex);
927 nvmf_free_options(nctrl->opts);
928 free_ctrl:
929 kfree(ctrl->queues);
930 kfree(ctrl);
933 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
935 /* If we are resetting/deleting then do nothing */
936 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
937 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
938 ctrl->ctrl.state == NVME_CTRL_LIVE);
939 return;
942 if (nvmf_should_reconnect(&ctrl->ctrl)) {
943 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
944 ctrl->ctrl.opts->reconnect_delay);
945 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
946 ctrl->ctrl.opts->reconnect_delay * HZ);
947 } else {
948 nvme_delete_ctrl(&ctrl->ctrl);
952 static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new)
954 int ret = -EINVAL;
955 bool changed;
957 ret = nvme_rdma_configure_admin_queue(ctrl, new);
958 if (ret)
959 return ret;
961 if (ctrl->ctrl.icdoff) {
962 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
963 goto destroy_admin;
966 if (!(ctrl->ctrl.sgls & (1 << 2))) {
967 dev_err(ctrl->ctrl.device,
968 "Mandatory keyed sgls are not supported!\n");
969 goto destroy_admin;
972 if (ctrl->ctrl.opts->queue_size > ctrl->ctrl.sqsize + 1) {
973 dev_warn(ctrl->ctrl.device,
974 "queue_size %zu > ctrl sqsize %u, clamping down\n",
975 ctrl->ctrl.opts->queue_size, ctrl->ctrl.sqsize + 1);
978 if (ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) {
979 dev_warn(ctrl->ctrl.device,
980 "sqsize %u > ctrl maxcmd %u, clamping down\n",
981 ctrl->ctrl.sqsize + 1, ctrl->ctrl.maxcmd);
982 ctrl->ctrl.sqsize = ctrl->ctrl.maxcmd - 1;
985 if (ctrl->ctrl.sgls & (1 << 20))
986 ctrl->use_inline_data = true;
988 if (ctrl->ctrl.queue_count > 1) {
989 ret = nvme_rdma_configure_io_queues(ctrl, new);
990 if (ret)
991 goto destroy_admin;
994 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
995 if (!changed) {
996 /* state change failure is ok if we're in DELETING state */
997 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
998 ret = -EINVAL;
999 goto destroy_io;
1002 nvme_start_ctrl(&ctrl->ctrl);
1003 return 0;
1005 destroy_io:
1006 if (ctrl->ctrl.queue_count > 1)
1007 nvme_rdma_destroy_io_queues(ctrl, new);
1008 destroy_admin:
1009 nvme_rdma_stop_queue(&ctrl->queues[0]);
1010 nvme_rdma_destroy_admin_queue(ctrl, new);
1011 return ret;
1014 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
1016 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
1017 struct nvme_rdma_ctrl, reconnect_work);
1019 ++ctrl->ctrl.nr_reconnects;
1021 if (nvme_rdma_setup_ctrl(ctrl, false))
1022 goto requeue;
1024 dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
1025 ctrl->ctrl.nr_reconnects);
1027 ctrl->ctrl.nr_reconnects = 0;
1029 return;
1031 requeue:
1032 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
1033 ctrl->ctrl.nr_reconnects);
1034 nvme_rdma_reconnect_or_remove(ctrl);
1037 static void nvme_rdma_error_recovery_work(struct work_struct *work)
1039 struct nvme_rdma_ctrl *ctrl = container_of(work,
1040 struct nvme_rdma_ctrl, err_work);
1042 nvme_stop_keep_alive(&ctrl->ctrl);
1043 nvme_rdma_teardown_io_queues(ctrl, false);
1044 nvme_start_queues(&ctrl->ctrl);
1045 nvme_rdma_teardown_admin_queue(ctrl, false);
1047 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
1048 /* state change failure is ok if we're in DELETING state */
1049 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
1050 return;
1053 nvme_rdma_reconnect_or_remove(ctrl);
1056 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
1058 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
1059 return;
1061 queue_work(nvme_wq, &ctrl->err_work);
1064 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1065 const char *op)
1067 struct nvme_rdma_queue *queue = cq->cq_context;
1068 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1070 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1071 dev_info(ctrl->ctrl.device,
1072 "%s for CQE 0x%p failed with status %s (%d)\n",
1073 op, wc->wr_cqe,
1074 ib_wc_status_msg(wc->status), wc->status);
1075 nvme_rdma_error_recovery(ctrl);
1078 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1080 if (unlikely(wc->status != IB_WC_SUCCESS))
1081 nvme_rdma_wr_error(cq, wc, "MEMREG");
1084 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1086 struct nvme_rdma_request *req =
1087 container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe);
1088 struct request *rq = blk_mq_rq_from_pdu(req);
1090 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1091 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
1092 return;
1095 if (refcount_dec_and_test(&req->ref))
1096 nvme_end_request(rq, req->status, req->result);
1100 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1101 struct nvme_rdma_request *req)
1103 struct ib_send_wr wr = {
1104 .opcode = IB_WR_LOCAL_INV,
1105 .next = NULL,
1106 .num_sge = 0,
1107 .send_flags = IB_SEND_SIGNALED,
1108 .ex.invalidate_rkey = req->mr->rkey,
1111 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1112 wr.wr_cqe = &req->reg_cqe;
1114 return ib_post_send(queue->qp, &wr, NULL);
1117 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1118 struct request *rq)
1120 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1121 struct nvme_rdma_device *dev = queue->device;
1122 struct ib_device *ibdev = dev->dev;
1124 if (!blk_rq_payload_bytes(rq))
1125 return;
1127 if (req->mr) {
1128 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1129 req->mr = NULL;
1132 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1133 req->nents, rq_data_dir(rq) ==
1134 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1136 nvme_cleanup_cmd(rq);
1137 sg_free_table_chained(&req->sg_table, true);
1140 static int nvme_rdma_set_sg_null(struct nvme_command *c)
1142 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1144 sg->addr = 0;
1145 put_unaligned_le24(0, sg->length);
1146 put_unaligned_le32(0, sg->key);
1147 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1148 return 0;
1151 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
1152 struct nvme_rdma_request *req, struct nvme_command *c,
1153 int count)
1155 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1156 struct scatterlist *sgl = req->sg_table.sgl;
1157 struct ib_sge *sge = &req->sge[1];
1158 u32 len = 0;
1159 int i;
1161 for (i = 0; i < count; i++, sgl++, sge++) {
1162 sge->addr = sg_dma_address(sgl);
1163 sge->length = sg_dma_len(sgl);
1164 sge->lkey = queue->device->pd->local_dma_lkey;
1165 len += sge->length;
1168 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1169 sg->length = cpu_to_le32(len);
1170 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1172 req->num_sge += count;
1173 return 0;
1176 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1177 struct nvme_rdma_request *req, struct nvme_command *c)
1179 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1181 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
1182 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
1183 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
1184 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1185 return 0;
1188 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1189 struct nvme_rdma_request *req, struct nvme_command *c,
1190 int count)
1192 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1193 int nr;
1195 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs);
1196 if (WARN_ON_ONCE(!req->mr))
1197 return -EAGAIN;
1200 * Align the MR to a 4K page size to match the ctrl page size and
1201 * the block virtual boundary.
1203 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K);
1204 if (unlikely(nr < count)) {
1205 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1206 req->mr = NULL;
1207 if (nr < 0)
1208 return nr;
1209 return -EINVAL;
1212 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1214 req->reg_cqe.done = nvme_rdma_memreg_done;
1215 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1216 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1217 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1218 req->reg_wr.wr.num_sge = 0;
1219 req->reg_wr.mr = req->mr;
1220 req->reg_wr.key = req->mr->rkey;
1221 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1222 IB_ACCESS_REMOTE_READ |
1223 IB_ACCESS_REMOTE_WRITE;
1225 sg->addr = cpu_to_le64(req->mr->iova);
1226 put_unaligned_le24(req->mr->length, sg->length);
1227 put_unaligned_le32(req->mr->rkey, sg->key);
1228 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1229 NVME_SGL_FMT_INVALIDATE;
1231 return 0;
1234 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
1235 struct request *rq, struct nvme_command *c)
1237 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1238 struct nvme_rdma_device *dev = queue->device;
1239 struct ib_device *ibdev = dev->dev;
1240 int count, ret;
1242 req->num_sge = 1;
1243 refcount_set(&req->ref, 2); /* send and recv completions */
1245 c->common.flags |= NVME_CMD_SGL_METABUF;
1247 if (!blk_rq_payload_bytes(rq))
1248 return nvme_rdma_set_sg_null(c);
1250 req->sg_table.sgl = req->first_sgl;
1251 ret = sg_alloc_table_chained(&req->sg_table,
1252 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
1253 if (ret)
1254 return -ENOMEM;
1256 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
1258 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
1259 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1260 if (unlikely(count <= 0)) {
1261 ret = -EIO;
1262 goto out_free_table;
1265 if (count <= dev->num_inline_segments) {
1266 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1267 queue->ctrl->use_inline_data &&
1268 blk_rq_payload_bytes(rq) <=
1269 nvme_rdma_inline_data_size(queue)) {
1270 ret = nvme_rdma_map_sg_inline(queue, req, c, count);
1271 goto out;
1274 if (count == 1 && dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
1275 ret = nvme_rdma_map_sg_single(queue, req, c);
1276 goto out;
1280 ret = nvme_rdma_map_sg_fr(queue, req, c, count);
1281 out:
1282 if (unlikely(ret))
1283 goto out_unmap_sg;
1285 return 0;
1287 out_unmap_sg:
1288 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1289 req->nents, rq_data_dir(rq) ==
1290 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1291 out_free_table:
1292 sg_free_table_chained(&req->sg_table, true);
1293 return ret;
1296 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1298 struct nvme_rdma_qe *qe =
1299 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1300 struct nvme_rdma_request *req =
1301 container_of(qe, struct nvme_rdma_request, sqe);
1302 struct request *rq = blk_mq_rq_from_pdu(req);
1304 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1305 nvme_rdma_wr_error(cq, wc, "SEND");
1306 return;
1309 if (refcount_dec_and_test(&req->ref))
1310 nvme_end_request(rq, req->status, req->result);
1313 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1314 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1315 struct ib_send_wr *first)
1317 struct ib_send_wr wr;
1318 int ret;
1320 sge->addr = qe->dma;
1321 sge->length = sizeof(struct nvme_command),
1322 sge->lkey = queue->device->pd->local_dma_lkey;
1324 wr.next = NULL;
1325 wr.wr_cqe = &qe->cqe;
1326 wr.sg_list = sge;
1327 wr.num_sge = num_sge;
1328 wr.opcode = IB_WR_SEND;
1329 wr.send_flags = IB_SEND_SIGNALED;
1331 if (first)
1332 first->next = &wr;
1333 else
1334 first = &wr;
1336 ret = ib_post_send(queue->qp, first, NULL);
1337 if (unlikely(ret)) {
1338 dev_err(queue->ctrl->ctrl.device,
1339 "%s failed with error code %d\n", __func__, ret);
1341 return ret;
1344 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1345 struct nvme_rdma_qe *qe)
1347 struct ib_recv_wr wr;
1348 struct ib_sge list;
1349 int ret;
1351 list.addr = qe->dma;
1352 list.length = sizeof(struct nvme_completion);
1353 list.lkey = queue->device->pd->local_dma_lkey;
1355 qe->cqe.done = nvme_rdma_recv_done;
1357 wr.next = NULL;
1358 wr.wr_cqe = &qe->cqe;
1359 wr.sg_list = &list;
1360 wr.num_sge = 1;
1362 ret = ib_post_recv(queue->qp, &wr, NULL);
1363 if (unlikely(ret)) {
1364 dev_err(queue->ctrl->ctrl.device,
1365 "%s failed with error code %d\n", __func__, ret);
1367 return ret;
1370 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1372 u32 queue_idx = nvme_rdma_queue_idx(queue);
1374 if (queue_idx == 0)
1375 return queue->ctrl->admin_tag_set.tags[queue_idx];
1376 return queue->ctrl->tag_set.tags[queue_idx - 1];
1379 static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc)
1381 if (unlikely(wc->status != IB_WC_SUCCESS))
1382 nvme_rdma_wr_error(cq, wc, "ASYNC");
1385 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg)
1387 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1388 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1389 struct ib_device *dev = queue->device->dev;
1390 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1391 struct nvme_command *cmd = sqe->data;
1392 struct ib_sge sge;
1393 int ret;
1395 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1397 memset(cmd, 0, sizeof(*cmd));
1398 cmd->common.opcode = nvme_admin_async_event;
1399 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
1400 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1401 nvme_rdma_set_sg_null(cmd);
1403 sqe->cqe.done = nvme_rdma_async_done;
1405 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1406 DMA_TO_DEVICE);
1408 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL);
1409 WARN_ON_ONCE(ret);
1412 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1413 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1415 struct request *rq;
1416 struct nvme_rdma_request *req;
1417 int ret = 0;
1419 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1420 if (!rq) {
1421 dev_err(queue->ctrl->ctrl.device,
1422 "tag 0x%x on QP %#x not found\n",
1423 cqe->command_id, queue->qp->qp_num);
1424 nvme_rdma_error_recovery(queue->ctrl);
1425 return ret;
1427 req = blk_mq_rq_to_pdu(rq);
1429 req->status = cqe->status;
1430 req->result = cqe->result;
1432 if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
1433 if (unlikely(wc->ex.invalidate_rkey != req->mr->rkey)) {
1434 dev_err(queue->ctrl->ctrl.device,
1435 "Bogus remote invalidation for rkey %#x\n",
1436 req->mr->rkey);
1437 nvme_rdma_error_recovery(queue->ctrl);
1439 } else if (req->mr) {
1440 ret = nvme_rdma_inv_rkey(queue, req);
1441 if (unlikely(ret < 0)) {
1442 dev_err(queue->ctrl->ctrl.device,
1443 "Queueing INV WR for rkey %#x failed (%d)\n",
1444 req->mr->rkey, ret);
1445 nvme_rdma_error_recovery(queue->ctrl);
1447 /* the local invalidation completion will end the request */
1448 return 0;
1451 if (refcount_dec_and_test(&req->ref)) {
1452 if (rq->tag == tag)
1453 ret = 1;
1454 nvme_end_request(rq, req->status, req->result);
1457 return ret;
1460 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1462 struct nvme_rdma_qe *qe =
1463 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1464 struct nvme_rdma_queue *queue = cq->cq_context;
1465 struct ib_device *ibdev = queue->device->dev;
1466 struct nvme_completion *cqe = qe->data;
1467 const size_t len = sizeof(struct nvme_completion);
1468 int ret = 0;
1470 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1471 nvme_rdma_wr_error(cq, wc, "RECV");
1472 return 0;
1475 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1477 * AEN requests are special as they don't time out and can
1478 * survive any kind of queue freeze and often don't respond to
1479 * aborts. We don't even bother to allocate a struct request
1480 * for them but rather special case them here.
1482 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1483 cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH))
1484 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1485 &cqe->result);
1486 else
1487 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1488 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1490 nvme_rdma_post_recv(queue, qe);
1491 return ret;
1494 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1496 __nvme_rdma_recv_done(cq, wc, -1);
1499 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1501 int ret, i;
1503 for (i = 0; i < queue->queue_size; i++) {
1504 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1505 if (ret)
1506 goto out_destroy_queue_ib;
1509 return 0;
1511 out_destroy_queue_ib:
1512 nvme_rdma_destroy_queue_ib(queue);
1513 return ret;
1516 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1517 struct rdma_cm_event *ev)
1519 struct rdma_cm_id *cm_id = queue->cm_id;
1520 int status = ev->status;
1521 const char *rej_msg;
1522 const struct nvme_rdma_cm_rej *rej_data;
1523 u8 rej_data_len;
1525 rej_msg = rdma_reject_msg(cm_id, status);
1526 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1528 if (rej_data && rej_data_len >= sizeof(u16)) {
1529 u16 sts = le16_to_cpu(rej_data->sts);
1531 dev_err(queue->ctrl->ctrl.device,
1532 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1533 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1534 } else {
1535 dev_err(queue->ctrl->ctrl.device,
1536 "Connect rejected: status %d (%s).\n", status, rej_msg);
1539 return -ECONNRESET;
1542 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1544 int ret;
1546 ret = nvme_rdma_create_queue_ib(queue);
1547 if (ret)
1548 return ret;
1550 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1551 if (ret) {
1552 dev_err(queue->ctrl->ctrl.device,
1553 "rdma_resolve_route failed (%d).\n",
1554 queue->cm_error);
1555 goto out_destroy_queue;
1558 return 0;
1560 out_destroy_queue:
1561 nvme_rdma_destroy_queue_ib(queue);
1562 return ret;
1565 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1567 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1568 struct rdma_conn_param param = { };
1569 struct nvme_rdma_cm_req priv = { };
1570 int ret;
1572 param.qp_num = queue->qp->qp_num;
1573 param.flow_control = 1;
1575 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1576 /* maximum retry count */
1577 param.retry_count = 7;
1578 param.rnr_retry_count = 7;
1579 param.private_data = &priv;
1580 param.private_data_len = sizeof(priv);
1582 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1583 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1585 * set the admin queue depth to the minimum size
1586 * specified by the Fabrics standard.
1588 if (priv.qid == 0) {
1589 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1590 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
1591 } else {
1593 * current interpretation of the fabrics spec
1594 * is at minimum you make hrqsize sqsize+1, or a
1595 * 1's based representation of sqsize.
1597 priv.hrqsize = cpu_to_le16(queue->queue_size);
1598 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1601 ret = rdma_connect(queue->cm_id, &param);
1602 if (ret) {
1603 dev_err(ctrl->ctrl.device,
1604 "rdma_connect failed (%d).\n", ret);
1605 goto out_destroy_queue_ib;
1608 return 0;
1610 out_destroy_queue_ib:
1611 nvme_rdma_destroy_queue_ib(queue);
1612 return ret;
1615 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1616 struct rdma_cm_event *ev)
1618 struct nvme_rdma_queue *queue = cm_id->context;
1619 int cm_error = 0;
1621 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1622 rdma_event_msg(ev->event), ev->event,
1623 ev->status, cm_id);
1625 switch (ev->event) {
1626 case RDMA_CM_EVENT_ADDR_RESOLVED:
1627 cm_error = nvme_rdma_addr_resolved(queue);
1628 break;
1629 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1630 cm_error = nvme_rdma_route_resolved(queue);
1631 break;
1632 case RDMA_CM_EVENT_ESTABLISHED:
1633 queue->cm_error = nvme_rdma_conn_established(queue);
1634 /* complete cm_done regardless of success/failure */
1635 complete(&queue->cm_done);
1636 return 0;
1637 case RDMA_CM_EVENT_REJECTED:
1638 nvme_rdma_destroy_queue_ib(queue);
1639 cm_error = nvme_rdma_conn_rejected(queue, ev);
1640 break;
1641 case RDMA_CM_EVENT_ROUTE_ERROR:
1642 case RDMA_CM_EVENT_CONNECT_ERROR:
1643 case RDMA_CM_EVENT_UNREACHABLE:
1644 nvme_rdma_destroy_queue_ib(queue);
1645 /* fall through */
1646 case RDMA_CM_EVENT_ADDR_ERROR:
1647 dev_dbg(queue->ctrl->ctrl.device,
1648 "CM error event %d\n", ev->event);
1649 cm_error = -ECONNRESET;
1650 break;
1651 case RDMA_CM_EVENT_DISCONNECTED:
1652 case RDMA_CM_EVENT_ADDR_CHANGE:
1653 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1654 dev_dbg(queue->ctrl->ctrl.device,
1655 "disconnect received - connection closed\n");
1656 nvme_rdma_error_recovery(queue->ctrl);
1657 break;
1658 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1659 /* device removal is handled via the ib_client API */
1660 break;
1661 default:
1662 dev_err(queue->ctrl->ctrl.device,
1663 "Unexpected RDMA CM event (%d)\n", ev->event);
1664 nvme_rdma_error_recovery(queue->ctrl);
1665 break;
1668 if (cm_error) {
1669 queue->cm_error = cm_error;
1670 complete(&queue->cm_done);
1673 return 0;
1676 static enum blk_eh_timer_return
1677 nvme_rdma_timeout(struct request *rq, bool reserved)
1679 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1681 dev_warn(req->queue->ctrl->ctrl.device,
1682 "I/O %d QID %d timeout, reset controller\n",
1683 rq->tag, nvme_rdma_queue_idx(req->queue));
1685 /* queue error recovery */
1686 nvme_rdma_error_recovery(req->queue->ctrl);
1688 /* fail with DNR on cmd timeout */
1689 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1691 return BLK_EH_DONE;
1694 static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1695 const struct blk_mq_queue_data *bd)
1697 struct nvme_ns *ns = hctx->queue->queuedata;
1698 struct nvme_rdma_queue *queue = hctx->driver_data;
1699 struct request *rq = bd->rq;
1700 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1701 struct nvme_rdma_qe *sqe = &req->sqe;
1702 struct nvme_command *c = sqe->data;
1703 struct ib_device *dev;
1704 bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags);
1705 blk_status_t ret;
1706 int err;
1708 WARN_ON_ONCE(rq->tag < 0);
1710 if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
1711 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
1713 dev = queue->device->dev;
1714 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1715 sizeof(struct nvme_command), DMA_TO_DEVICE);
1717 ret = nvme_setup_cmd(ns, rq, c);
1718 if (ret)
1719 return ret;
1721 blk_mq_start_request(rq);
1723 err = nvme_rdma_map_data(queue, rq, c);
1724 if (unlikely(err < 0)) {
1725 dev_err(queue->ctrl->ctrl.device,
1726 "Failed to map data (%d)\n", err);
1727 nvme_cleanup_cmd(rq);
1728 goto err;
1731 sqe->cqe.done = nvme_rdma_send_done;
1733 ib_dma_sync_single_for_device(dev, sqe->dma,
1734 sizeof(struct nvme_command), DMA_TO_DEVICE);
1736 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1737 req->mr ? &req->reg_wr.wr : NULL);
1738 if (unlikely(err)) {
1739 nvme_rdma_unmap_data(queue, rq);
1740 goto err;
1743 return BLK_STS_OK;
1744 err:
1745 if (err == -ENOMEM || err == -EAGAIN)
1746 return BLK_STS_RESOURCE;
1747 return BLK_STS_IOERR;
1750 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1752 struct nvme_rdma_queue *queue = hctx->driver_data;
1753 struct ib_cq *cq = queue->ib_cq;
1754 struct ib_wc wc;
1755 int found = 0;
1757 while (ib_poll_cq(cq, 1, &wc) > 0) {
1758 struct ib_cqe *cqe = wc.wr_cqe;
1760 if (cqe) {
1761 if (cqe->done == nvme_rdma_recv_done)
1762 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1763 else
1764 cqe->done(cq, &wc);
1768 return found;
1771 static void nvme_rdma_complete_rq(struct request *rq)
1773 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1775 nvme_rdma_unmap_data(req->queue, rq);
1776 nvme_complete_rq(rq);
1779 static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
1781 struct nvme_rdma_ctrl *ctrl = set->driver_data;
1783 return blk_mq_rdma_map_queues(set, ctrl->device->dev, 0);
1786 static const struct blk_mq_ops nvme_rdma_mq_ops = {
1787 .queue_rq = nvme_rdma_queue_rq,
1788 .complete = nvme_rdma_complete_rq,
1789 .init_request = nvme_rdma_init_request,
1790 .exit_request = nvme_rdma_exit_request,
1791 .init_hctx = nvme_rdma_init_hctx,
1792 .poll = nvme_rdma_poll,
1793 .timeout = nvme_rdma_timeout,
1794 .map_queues = nvme_rdma_map_queues,
1797 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1798 .queue_rq = nvme_rdma_queue_rq,
1799 .complete = nvme_rdma_complete_rq,
1800 .init_request = nvme_rdma_init_request,
1801 .exit_request = nvme_rdma_exit_request,
1802 .init_hctx = nvme_rdma_init_admin_hctx,
1803 .timeout = nvme_rdma_timeout,
1806 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1808 nvme_rdma_teardown_io_queues(ctrl, shutdown);
1809 if (shutdown)
1810 nvme_shutdown_ctrl(&ctrl->ctrl);
1811 else
1812 nvme_disable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
1813 nvme_rdma_teardown_admin_queue(ctrl, shutdown);
1816 static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
1818 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true);
1821 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1823 struct nvme_rdma_ctrl *ctrl =
1824 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
1826 nvme_stop_ctrl(&ctrl->ctrl);
1827 nvme_rdma_shutdown_ctrl(ctrl, false);
1829 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
1830 /* state change failure should never happen */
1831 WARN_ON_ONCE(1);
1832 return;
1835 if (nvme_rdma_setup_ctrl(ctrl, false))
1836 goto out_fail;
1838 return;
1840 out_fail:
1841 ++ctrl->ctrl.nr_reconnects;
1842 nvme_rdma_reconnect_or_remove(ctrl);
1845 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1846 .name = "rdma",
1847 .module = THIS_MODULE,
1848 .flags = NVME_F_FABRICS,
1849 .reg_read32 = nvmf_reg_read32,
1850 .reg_read64 = nvmf_reg_read64,
1851 .reg_write32 = nvmf_reg_write32,
1852 .free_ctrl = nvme_rdma_free_ctrl,
1853 .submit_async_event = nvme_rdma_submit_async_event,
1854 .delete_ctrl = nvme_rdma_delete_ctrl,
1855 .get_address = nvmf_get_address,
1856 .stop_ctrl = nvme_rdma_stop_ctrl,
1860 * Fails a connection request if it matches an existing controller
1861 * (association) with the same tuple:
1862 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
1864 * if local address is not specified in the request, it will match an
1865 * existing controller with all the other parameters the same and no
1866 * local port address specified as well.
1868 * The ports don't need to be compared as they are intrinsically
1869 * already matched by the port pointers supplied.
1871 static bool
1872 nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts)
1874 struct nvme_rdma_ctrl *ctrl;
1875 bool found = false;
1877 mutex_lock(&nvme_rdma_ctrl_mutex);
1878 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
1879 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
1880 if (found)
1881 break;
1883 mutex_unlock(&nvme_rdma_ctrl_mutex);
1885 return found;
1888 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1889 struct nvmf_ctrl_options *opts)
1891 struct nvme_rdma_ctrl *ctrl;
1892 int ret;
1893 bool changed;
1895 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1896 if (!ctrl)
1897 return ERR_PTR(-ENOMEM);
1898 ctrl->ctrl.opts = opts;
1899 INIT_LIST_HEAD(&ctrl->list);
1901 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
1902 opts->trsvcid =
1903 kstrdup(__stringify(NVME_RDMA_IP_PORT), GFP_KERNEL);
1904 if (!opts->trsvcid) {
1905 ret = -ENOMEM;
1906 goto out_free_ctrl;
1908 opts->mask |= NVMF_OPT_TRSVCID;
1911 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1912 opts->traddr, opts->trsvcid, &ctrl->addr);
1913 if (ret) {
1914 pr_err("malformed address passed: %s:%s\n",
1915 opts->traddr, opts->trsvcid);
1916 goto out_free_ctrl;
1919 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
1920 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1921 opts->host_traddr, NULL, &ctrl->src_addr);
1922 if (ret) {
1923 pr_err("malformed src address passed: %s\n",
1924 opts->host_traddr);
1925 goto out_free_ctrl;
1929 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) {
1930 ret = -EALREADY;
1931 goto out_free_ctrl;
1934 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1935 nvme_rdma_reconnect_ctrl_work);
1936 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1937 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
1939 ctrl->ctrl.queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1940 ctrl->ctrl.sqsize = opts->queue_size - 1;
1941 ctrl->ctrl.kato = opts->kato;
1943 ret = -ENOMEM;
1944 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
1945 GFP_KERNEL);
1946 if (!ctrl->queues)
1947 goto out_free_ctrl;
1949 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1950 0 /* no quirks, we're perfect! */);
1951 if (ret)
1952 goto out_kfree_queues;
1954 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING);
1955 WARN_ON_ONCE(!changed);
1957 ret = nvme_rdma_setup_ctrl(ctrl, true);
1958 if (ret)
1959 goto out_uninit_ctrl;
1961 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
1962 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1964 nvme_get_ctrl(&ctrl->ctrl);
1966 mutex_lock(&nvme_rdma_ctrl_mutex);
1967 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
1968 mutex_unlock(&nvme_rdma_ctrl_mutex);
1970 return &ctrl->ctrl;
1972 out_uninit_ctrl:
1973 nvme_uninit_ctrl(&ctrl->ctrl);
1974 nvme_put_ctrl(&ctrl->ctrl);
1975 if (ret > 0)
1976 ret = -EIO;
1977 return ERR_PTR(ret);
1978 out_kfree_queues:
1979 kfree(ctrl->queues);
1980 out_free_ctrl:
1981 kfree(ctrl);
1982 return ERR_PTR(ret);
1985 static struct nvmf_transport_ops nvme_rdma_transport = {
1986 .name = "rdma",
1987 .module = THIS_MODULE,
1988 .required_opts = NVMF_OPT_TRADDR,
1989 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
1990 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
1991 .create_ctrl = nvme_rdma_create_ctrl,
1994 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
1996 struct nvme_rdma_ctrl *ctrl;
1997 struct nvme_rdma_device *ndev;
1998 bool found = false;
2000 mutex_lock(&device_list_mutex);
2001 list_for_each_entry(ndev, &device_list, entry) {
2002 if (ndev->dev == ib_device) {
2003 found = true;
2004 break;
2007 mutex_unlock(&device_list_mutex);
2009 if (!found)
2010 return;
2012 /* Delete all controllers using this device */
2013 mutex_lock(&nvme_rdma_ctrl_mutex);
2014 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2015 if (ctrl->device->dev != ib_device)
2016 continue;
2017 nvme_delete_ctrl(&ctrl->ctrl);
2019 mutex_unlock(&nvme_rdma_ctrl_mutex);
2021 flush_workqueue(nvme_delete_wq);
2024 static struct ib_client nvme_rdma_ib_client = {
2025 .name = "nvme_rdma",
2026 .remove = nvme_rdma_remove_one
2029 static int __init nvme_rdma_init_module(void)
2031 int ret;
2033 ret = ib_register_client(&nvme_rdma_ib_client);
2034 if (ret)
2035 return ret;
2037 ret = nvmf_register_transport(&nvme_rdma_transport);
2038 if (ret)
2039 goto err_unreg_client;
2041 return 0;
2043 err_unreg_client:
2044 ib_unregister_client(&nvme_rdma_ib_client);
2045 return ret;
2048 static void __exit nvme_rdma_cleanup_module(void)
2050 nvmf_unregister_transport(&nvme_rdma_transport);
2051 ib_unregister_client(&nvme_rdma_ib_client);
2054 module_init(nvme_rdma_init_module);
2055 module_exit(nvme_rdma_cleanup_module);
2057 MODULE_LICENSE("GPL v2");