mm/hmm.c: remove superfluous RCU protection around radix tree lookup
[linux/fpc-iii.git] / drivers / nvme / host / rdma.c
blob758537e9ba07651c56be541578eb15139a73f65e
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 1
45 struct nvme_rdma_device {
46 struct ib_device *dev;
47 struct ib_pd *pd;
48 struct kref ref;
49 struct list_head entry;
52 struct nvme_rdma_qe {
53 struct ib_cqe cqe;
54 void *data;
55 u64 dma;
58 struct nvme_rdma_queue;
59 struct nvme_rdma_request {
60 struct nvme_request req;
61 struct ib_mr *mr;
62 struct nvme_rdma_qe sqe;
63 union nvme_result result;
64 __le16 status;
65 refcount_t ref;
66 struct ib_sge sge[1 + NVME_RDMA_MAX_INLINE_SEGMENTS];
67 u32 num_sge;
68 int nents;
69 struct ib_reg_wr reg_wr;
70 struct ib_cqe reg_cqe;
71 struct nvme_rdma_queue *queue;
72 struct sg_table sg_table;
73 struct scatterlist first_sgl[];
76 enum nvme_rdma_queue_flags {
77 NVME_RDMA_Q_ALLOCATED = 0,
78 NVME_RDMA_Q_LIVE = 1,
79 NVME_RDMA_Q_TR_READY = 2,
82 struct nvme_rdma_queue {
83 struct nvme_rdma_qe *rsp_ring;
84 int queue_size;
85 size_t cmnd_capsule_len;
86 struct nvme_rdma_ctrl *ctrl;
87 struct nvme_rdma_device *device;
88 struct ib_cq *ib_cq;
89 struct ib_qp *qp;
91 unsigned long flags;
92 struct rdma_cm_id *cm_id;
93 int cm_error;
94 struct completion cm_done;
97 struct nvme_rdma_ctrl {
98 /* read only in the hot path */
99 struct nvme_rdma_queue *queues;
101 /* other member variables */
102 struct blk_mq_tag_set tag_set;
103 struct work_struct err_work;
105 struct nvme_rdma_qe async_event_sqe;
107 struct delayed_work reconnect_work;
109 struct list_head list;
111 struct blk_mq_tag_set admin_tag_set;
112 struct nvme_rdma_device *device;
114 u32 max_fr_pages;
116 struct sockaddr_storage addr;
117 struct sockaddr_storage src_addr;
119 struct nvme_ctrl ctrl;
122 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
124 return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
127 static LIST_HEAD(device_list);
128 static DEFINE_MUTEX(device_list_mutex);
130 static LIST_HEAD(nvme_rdma_ctrl_list);
131 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
134 * Disabling this option makes small I/O goes faster, but is fundamentally
135 * unsafe. With it turned off we will have to register a global rkey that
136 * allows read and write access to all physical memory.
138 static bool register_always = true;
139 module_param(register_always, bool, 0444);
140 MODULE_PARM_DESC(register_always,
141 "Use memory registration even for contiguous memory regions");
143 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
144 struct rdma_cm_event *event);
145 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
147 static const struct blk_mq_ops nvme_rdma_mq_ops;
148 static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
150 /* XXX: really should move to a generic header sooner or later.. */
151 static inline void put_unaligned_le24(u32 val, u8 *p)
153 *p++ = val;
154 *p++ = val >> 8;
155 *p++ = val >> 16;
158 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
160 return queue - queue->ctrl->queues;
163 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
165 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
168 static void nvme_rdma_free_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
169 size_t capsule_size, enum dma_data_direction dir)
171 ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
172 kfree(qe->data);
175 static int nvme_rdma_alloc_qe(struct ib_device *ibdev, struct nvme_rdma_qe *qe,
176 size_t capsule_size, enum dma_data_direction dir)
178 qe->data = kzalloc(capsule_size, GFP_KERNEL);
179 if (!qe->data)
180 return -ENOMEM;
182 qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
183 if (ib_dma_mapping_error(ibdev, qe->dma)) {
184 kfree(qe->data);
185 return -ENOMEM;
188 return 0;
191 static void nvme_rdma_free_ring(struct ib_device *ibdev,
192 struct nvme_rdma_qe *ring, size_t ib_queue_size,
193 size_t capsule_size, enum dma_data_direction dir)
195 int i;
197 for (i = 0; i < ib_queue_size; i++)
198 nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
199 kfree(ring);
202 static struct nvme_rdma_qe *nvme_rdma_alloc_ring(struct ib_device *ibdev,
203 size_t ib_queue_size, size_t capsule_size,
204 enum dma_data_direction dir)
206 struct nvme_rdma_qe *ring;
207 int i;
209 ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
210 if (!ring)
211 return NULL;
213 for (i = 0; i < ib_queue_size; i++) {
214 if (nvme_rdma_alloc_qe(ibdev, &ring[i], capsule_size, dir))
215 goto out_free_ring;
218 return ring;
220 out_free_ring:
221 nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
222 return NULL;
225 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
227 pr_debug("QP event %s (%d)\n",
228 ib_event_msg(event->event), event->event);
232 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
234 wait_for_completion_interruptible_timeout(&queue->cm_done,
235 msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
236 return queue->cm_error;
239 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
241 struct nvme_rdma_device *dev = queue->device;
242 struct ib_qp_init_attr init_attr;
243 int ret;
245 memset(&init_attr, 0, sizeof(init_attr));
246 init_attr.event_handler = nvme_rdma_qp_event;
247 /* +1 for drain */
248 init_attr.cap.max_send_wr = factor * queue->queue_size + 1;
249 /* +1 for drain */
250 init_attr.cap.max_recv_wr = queue->queue_size + 1;
251 init_attr.cap.max_recv_sge = 1;
252 init_attr.cap.max_send_sge = 1 + NVME_RDMA_MAX_INLINE_SEGMENTS;
253 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
254 init_attr.qp_type = IB_QPT_RC;
255 init_attr.send_cq = queue->ib_cq;
256 init_attr.recv_cq = queue->ib_cq;
258 ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
260 queue->qp = queue->cm_id->qp;
261 return ret;
264 static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
265 struct request *rq, unsigned int hctx_idx)
267 struct nvme_rdma_ctrl *ctrl = set->driver_data;
268 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
269 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
270 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
271 struct nvme_rdma_device *dev = queue->device;
273 nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
274 DMA_TO_DEVICE);
277 static int nvme_rdma_init_request(struct blk_mq_tag_set *set,
278 struct request *rq, unsigned int hctx_idx,
279 unsigned int numa_node)
281 struct nvme_rdma_ctrl *ctrl = set->driver_data;
282 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
283 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
284 struct nvme_rdma_queue *queue = &ctrl->queues[queue_idx];
285 struct nvme_rdma_device *dev = queue->device;
286 struct ib_device *ibdev = dev->dev;
287 int ret;
289 ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
290 DMA_TO_DEVICE);
291 if (ret)
292 return ret;
294 req->queue = queue;
296 return 0;
299 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
300 unsigned int hctx_idx)
302 struct nvme_rdma_ctrl *ctrl = data;
303 struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
305 BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
307 hctx->driver_data = queue;
308 return 0;
311 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
312 unsigned int hctx_idx)
314 struct nvme_rdma_ctrl *ctrl = data;
315 struct nvme_rdma_queue *queue = &ctrl->queues[0];
317 BUG_ON(hctx_idx != 0);
319 hctx->driver_data = queue;
320 return 0;
323 static void nvme_rdma_free_dev(struct kref *ref)
325 struct nvme_rdma_device *ndev =
326 container_of(ref, struct nvme_rdma_device, ref);
328 mutex_lock(&device_list_mutex);
329 list_del(&ndev->entry);
330 mutex_unlock(&device_list_mutex);
332 ib_dealloc_pd(ndev->pd);
333 kfree(ndev);
336 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
338 kref_put(&dev->ref, nvme_rdma_free_dev);
341 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
343 return kref_get_unless_zero(&dev->ref);
346 static struct nvme_rdma_device *
347 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
349 struct nvme_rdma_device *ndev;
351 mutex_lock(&device_list_mutex);
352 list_for_each_entry(ndev, &device_list, entry) {
353 if (ndev->dev->node_guid == cm_id->device->node_guid &&
354 nvme_rdma_dev_get(ndev))
355 goto out_unlock;
358 ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
359 if (!ndev)
360 goto out_err;
362 ndev->dev = cm_id->device;
363 kref_init(&ndev->ref);
365 ndev->pd = ib_alloc_pd(ndev->dev,
366 register_always ? 0 : IB_PD_UNSAFE_GLOBAL_RKEY);
367 if (IS_ERR(ndev->pd))
368 goto out_free_dev;
370 if (!(ndev->dev->attrs.device_cap_flags &
371 IB_DEVICE_MEM_MGT_EXTENSIONS)) {
372 dev_err(&ndev->dev->dev,
373 "Memory registrations not supported.\n");
374 goto out_free_pd;
377 list_add(&ndev->entry, &device_list);
378 out_unlock:
379 mutex_unlock(&device_list_mutex);
380 return ndev;
382 out_free_pd:
383 ib_dealloc_pd(ndev->pd);
384 out_free_dev:
385 kfree(ndev);
386 out_err:
387 mutex_unlock(&device_list_mutex);
388 return NULL;
391 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
393 struct nvme_rdma_device *dev;
394 struct ib_device *ibdev;
396 if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags))
397 return;
399 dev = queue->device;
400 ibdev = dev->dev;
402 ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
405 * The cm_id object might have been destroyed during RDMA connection
406 * establishment error flow to avoid getting other cma events, thus
407 * the destruction of the QP shouldn't use rdma_cm API.
409 ib_destroy_qp(queue->qp);
410 ib_free_cq(queue->ib_cq);
412 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
413 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
415 nvme_rdma_dev_put(dev);
418 static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev)
420 return min_t(u32, NVME_RDMA_MAX_SEGMENTS,
421 ibdev->attrs.max_fast_reg_page_list_len);
424 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
426 struct ib_device *ibdev;
427 const int send_wr_factor = 3; /* MR, SEND, INV */
428 const int cq_factor = send_wr_factor + 1; /* + RECV */
429 int comp_vector, idx = nvme_rdma_queue_idx(queue);
430 int ret;
432 queue->device = nvme_rdma_find_get_device(queue->cm_id);
433 if (!queue->device) {
434 dev_err(queue->cm_id->device->dev.parent,
435 "no client data found!\n");
436 return -ECONNREFUSED;
438 ibdev = queue->device->dev;
441 * Spread I/O queues completion vectors according their queue index.
442 * Admin queues can always go on completion vector 0.
444 comp_vector = idx == 0 ? idx : idx - 1;
446 /* +1 for ib_stop_cq */
447 queue->ib_cq = ib_alloc_cq(ibdev, queue,
448 cq_factor * queue->queue_size + 1,
449 comp_vector, IB_POLL_SOFTIRQ);
450 if (IS_ERR(queue->ib_cq)) {
451 ret = PTR_ERR(queue->ib_cq);
452 goto out_put_dev;
455 ret = nvme_rdma_create_qp(queue, send_wr_factor);
456 if (ret)
457 goto out_destroy_ib_cq;
459 queue->rsp_ring = nvme_rdma_alloc_ring(ibdev, queue->queue_size,
460 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
461 if (!queue->rsp_ring) {
462 ret = -ENOMEM;
463 goto out_destroy_qp;
466 ret = ib_mr_pool_init(queue->qp, &queue->qp->rdma_mrs,
467 queue->queue_size,
468 IB_MR_TYPE_MEM_REG,
469 nvme_rdma_get_max_fr_pages(ibdev));
470 if (ret) {
471 dev_err(queue->ctrl->ctrl.device,
472 "failed to initialize MR pool sized %d for QID %d\n",
473 queue->queue_size, idx);
474 goto out_destroy_ring;
477 set_bit(NVME_RDMA_Q_TR_READY, &queue->flags);
479 return 0;
481 out_destroy_ring:
482 nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
483 sizeof(struct nvme_completion), DMA_FROM_DEVICE);
484 out_destroy_qp:
485 rdma_destroy_qp(queue->cm_id);
486 out_destroy_ib_cq:
487 ib_free_cq(queue->ib_cq);
488 out_put_dev:
489 nvme_rdma_dev_put(queue->device);
490 return ret;
493 static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
494 int idx, size_t queue_size)
496 struct nvme_rdma_queue *queue;
497 struct sockaddr *src_addr = NULL;
498 int ret;
500 queue = &ctrl->queues[idx];
501 queue->ctrl = ctrl;
502 init_completion(&queue->cm_done);
504 if (idx > 0)
505 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
506 else
507 queue->cmnd_capsule_len = sizeof(struct nvme_command);
509 queue->queue_size = queue_size;
511 queue->cm_id = rdma_create_id(&init_net, nvme_rdma_cm_handler, queue,
512 RDMA_PS_TCP, IB_QPT_RC);
513 if (IS_ERR(queue->cm_id)) {
514 dev_info(ctrl->ctrl.device,
515 "failed to create CM ID: %ld\n", PTR_ERR(queue->cm_id));
516 return PTR_ERR(queue->cm_id);
519 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
520 src_addr = (struct sockaddr *)&ctrl->src_addr;
522 queue->cm_error = -ETIMEDOUT;
523 ret = rdma_resolve_addr(queue->cm_id, src_addr,
524 (struct sockaddr *)&ctrl->addr,
525 NVME_RDMA_CONNECT_TIMEOUT_MS);
526 if (ret) {
527 dev_info(ctrl->ctrl.device,
528 "rdma_resolve_addr failed (%d).\n", ret);
529 goto out_destroy_cm_id;
532 ret = nvme_rdma_wait_for_cm(queue);
533 if (ret) {
534 dev_info(ctrl->ctrl.device,
535 "rdma connection establishment failed (%d)\n", ret);
536 goto out_destroy_cm_id;
539 set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
541 return 0;
543 out_destroy_cm_id:
544 rdma_destroy_id(queue->cm_id);
545 nvme_rdma_destroy_queue_ib(queue);
546 return ret;
549 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
551 if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
552 return;
554 rdma_disconnect(queue->cm_id);
555 ib_drain_qp(queue->qp);
558 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
560 if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
561 return;
563 if (nvme_rdma_queue_idx(queue) == 0) {
564 nvme_rdma_free_qe(queue->device->dev,
565 &queue->ctrl->async_event_sqe,
566 sizeof(struct nvme_command), DMA_TO_DEVICE);
569 nvme_rdma_destroy_queue_ib(queue);
570 rdma_destroy_id(queue->cm_id);
573 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
575 int i;
577 for (i = 1; i < ctrl->ctrl.queue_count; i++)
578 nvme_rdma_free_queue(&ctrl->queues[i]);
581 static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
583 int i;
585 for (i = 1; i < ctrl->ctrl.queue_count; i++)
586 nvme_rdma_stop_queue(&ctrl->queues[i]);
589 static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
591 int ret;
593 if (idx)
594 ret = nvmf_connect_io_queue(&ctrl->ctrl, idx);
595 else
596 ret = nvmf_connect_admin_queue(&ctrl->ctrl);
598 if (!ret)
599 set_bit(NVME_RDMA_Q_LIVE, &ctrl->queues[idx].flags);
600 else
601 dev_info(ctrl->ctrl.device,
602 "failed to connect queue: %d ret=%d\n", idx, ret);
603 return ret;
606 static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
608 int i, ret = 0;
610 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
611 ret = nvme_rdma_start_queue(ctrl, i);
612 if (ret)
613 goto out_stop_queues;
616 return 0;
618 out_stop_queues:
619 for (i--; i >= 1; i--)
620 nvme_rdma_stop_queue(&ctrl->queues[i]);
621 return ret;
624 static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
626 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
627 struct ib_device *ibdev = ctrl->device->dev;
628 unsigned int nr_io_queues;
629 int i, ret;
631 nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
634 * we map queues according to the device irq vectors for
635 * optimal locality so we don't need more queues than
636 * completion vectors.
638 nr_io_queues = min_t(unsigned int, nr_io_queues,
639 ibdev->num_comp_vectors);
641 ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
642 if (ret)
643 return ret;
645 ctrl->ctrl.queue_count = nr_io_queues + 1;
646 if (ctrl->ctrl.queue_count < 2)
647 return 0;
649 dev_info(ctrl->ctrl.device,
650 "creating %d I/O queues.\n", nr_io_queues);
652 for (i = 1; i < ctrl->ctrl.queue_count; i++) {
653 ret = nvme_rdma_alloc_queue(ctrl, i,
654 ctrl->ctrl.sqsize + 1);
655 if (ret)
656 goto out_free_queues;
659 return 0;
661 out_free_queues:
662 for (i--; i >= 1; i--)
663 nvme_rdma_free_queue(&ctrl->queues[i]);
665 return ret;
668 static void nvme_rdma_free_tagset(struct nvme_ctrl *nctrl,
669 struct blk_mq_tag_set *set)
671 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
673 blk_mq_free_tag_set(set);
674 nvme_rdma_dev_put(ctrl->device);
677 static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
678 bool admin)
680 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
681 struct blk_mq_tag_set *set;
682 int ret;
684 if (admin) {
685 set = &ctrl->admin_tag_set;
686 memset(set, 0, sizeof(*set));
687 set->ops = &nvme_rdma_admin_mq_ops;
688 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
689 set->reserved_tags = 2; /* connect + keep-alive */
690 set->numa_node = NUMA_NO_NODE;
691 set->cmd_size = sizeof(struct nvme_rdma_request) +
692 SG_CHUNK_SIZE * sizeof(struct scatterlist);
693 set->driver_data = ctrl;
694 set->nr_hw_queues = 1;
695 set->timeout = ADMIN_TIMEOUT;
696 set->flags = BLK_MQ_F_NO_SCHED;
697 } else {
698 set = &ctrl->tag_set;
699 memset(set, 0, sizeof(*set));
700 set->ops = &nvme_rdma_mq_ops;
701 set->queue_depth = nctrl->opts->queue_size;
702 set->reserved_tags = 1; /* fabric connect */
703 set->numa_node = NUMA_NO_NODE;
704 set->flags = BLK_MQ_F_SHOULD_MERGE;
705 set->cmd_size = sizeof(struct nvme_rdma_request) +
706 SG_CHUNK_SIZE * sizeof(struct scatterlist);
707 set->driver_data = ctrl;
708 set->nr_hw_queues = nctrl->queue_count - 1;
709 set->timeout = NVME_IO_TIMEOUT;
712 ret = blk_mq_alloc_tag_set(set);
713 if (ret)
714 goto out;
717 * We need a reference on the device as long as the tag_set is alive,
718 * as the MRs in the request structures need a valid ib_device.
720 ret = nvme_rdma_dev_get(ctrl->device);
721 if (!ret) {
722 ret = -EINVAL;
723 goto out_free_tagset;
726 return set;
728 out_free_tagset:
729 blk_mq_free_tag_set(set);
730 out:
731 return ERR_PTR(ret);
734 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
735 bool remove)
737 nvme_rdma_stop_queue(&ctrl->queues[0]);
738 if (remove) {
739 blk_cleanup_queue(ctrl->ctrl.admin_q);
740 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
742 nvme_rdma_free_queue(&ctrl->queues[0]);
745 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
746 bool new)
748 int error;
750 error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
751 if (error)
752 return error;
754 ctrl->device = ctrl->queues[0].device;
756 ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev);
758 if (new) {
759 ctrl->ctrl.admin_tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, true);
760 if (IS_ERR(ctrl->ctrl.admin_tagset)) {
761 error = PTR_ERR(ctrl->ctrl.admin_tagset);
762 goto out_free_queue;
765 ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
766 if (IS_ERR(ctrl->ctrl.admin_q)) {
767 error = PTR_ERR(ctrl->ctrl.admin_q);
768 goto out_free_tagset;
772 error = nvme_rdma_start_queue(ctrl, 0);
773 if (error)
774 goto out_cleanup_queue;
776 error = ctrl->ctrl.ops->reg_read64(&ctrl->ctrl, NVME_REG_CAP,
777 &ctrl->ctrl.cap);
778 if (error) {
779 dev_err(ctrl->ctrl.device,
780 "prop_get NVME_REG_CAP failed\n");
781 goto out_cleanup_queue;
784 ctrl->ctrl.sqsize =
785 min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
787 error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
788 if (error)
789 goto out_cleanup_queue;
791 ctrl->ctrl.max_hw_sectors =
792 (ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9);
794 error = nvme_init_identify(&ctrl->ctrl);
795 if (error)
796 goto out_cleanup_queue;
798 error = nvme_rdma_alloc_qe(ctrl->queues[0].device->dev,
799 &ctrl->async_event_sqe, sizeof(struct nvme_command),
800 DMA_TO_DEVICE);
801 if (error)
802 goto out_cleanup_queue;
804 return 0;
806 out_cleanup_queue:
807 if (new)
808 blk_cleanup_queue(ctrl->ctrl.admin_q);
809 out_free_tagset:
810 if (new)
811 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
812 out_free_queue:
813 nvme_rdma_free_queue(&ctrl->queues[0]);
814 return error;
817 static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
818 bool remove)
820 nvme_rdma_stop_io_queues(ctrl);
821 if (remove) {
822 blk_cleanup_queue(ctrl->ctrl.connect_q);
823 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
825 nvme_rdma_free_io_queues(ctrl);
828 static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
830 int ret;
832 ret = nvme_rdma_alloc_io_queues(ctrl);
833 if (ret)
834 return ret;
836 if (new) {
837 ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
838 if (IS_ERR(ctrl->ctrl.tagset)) {
839 ret = PTR_ERR(ctrl->ctrl.tagset);
840 goto out_free_io_queues;
843 ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
844 if (IS_ERR(ctrl->ctrl.connect_q)) {
845 ret = PTR_ERR(ctrl->ctrl.connect_q);
846 goto out_free_tag_set;
848 } else {
849 blk_mq_update_nr_hw_queues(&ctrl->tag_set,
850 ctrl->ctrl.queue_count - 1);
853 ret = nvme_rdma_start_io_queues(ctrl);
854 if (ret)
855 goto out_cleanup_connect_q;
857 return 0;
859 out_cleanup_connect_q:
860 if (new)
861 blk_cleanup_queue(ctrl->ctrl.connect_q);
862 out_free_tag_set:
863 if (new)
864 nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
865 out_free_io_queues:
866 nvme_rdma_free_io_queues(ctrl);
867 return ret;
870 static void nvme_rdma_stop_ctrl(struct nvme_ctrl *nctrl)
872 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
874 cancel_work_sync(&ctrl->err_work);
875 cancel_delayed_work_sync(&ctrl->reconnect_work);
878 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
880 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
882 if (list_empty(&ctrl->list))
883 goto free_ctrl;
885 mutex_lock(&nvme_rdma_ctrl_mutex);
886 list_del(&ctrl->list);
887 mutex_unlock(&nvme_rdma_ctrl_mutex);
889 kfree(ctrl->queues);
890 nvmf_free_options(nctrl->opts);
891 free_ctrl:
892 kfree(ctrl);
895 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
897 /* If we are resetting/deleting then do nothing */
898 if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
899 WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
900 ctrl->ctrl.state == NVME_CTRL_LIVE);
901 return;
904 if (nvmf_should_reconnect(&ctrl->ctrl)) {
905 dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
906 ctrl->ctrl.opts->reconnect_delay);
907 queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
908 ctrl->ctrl.opts->reconnect_delay * HZ);
909 } else {
910 nvme_delete_ctrl(&ctrl->ctrl);
914 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
916 struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
917 struct nvme_rdma_ctrl, reconnect_work);
918 bool changed;
919 int ret;
921 ++ctrl->ctrl.nr_reconnects;
923 ret = nvme_rdma_configure_admin_queue(ctrl, false);
924 if (ret)
925 goto requeue;
927 if (ctrl->ctrl.queue_count > 1) {
928 ret = nvme_rdma_configure_io_queues(ctrl, false);
929 if (ret)
930 goto destroy_admin;
933 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
934 if (!changed) {
935 /* state change failure is ok if we're in DELETING state */
936 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
937 return;
940 nvme_start_ctrl(&ctrl->ctrl);
942 dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
943 ctrl->ctrl.nr_reconnects);
945 ctrl->ctrl.nr_reconnects = 0;
947 return;
949 destroy_admin:
950 nvme_rdma_destroy_admin_queue(ctrl, false);
951 requeue:
952 dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
953 ctrl->ctrl.nr_reconnects);
954 nvme_rdma_reconnect_or_remove(ctrl);
957 static void nvme_rdma_error_recovery_work(struct work_struct *work)
959 struct nvme_rdma_ctrl *ctrl = container_of(work,
960 struct nvme_rdma_ctrl, err_work);
962 nvme_stop_keep_alive(&ctrl->ctrl);
964 if (ctrl->ctrl.queue_count > 1) {
965 nvme_stop_queues(&ctrl->ctrl);
966 blk_mq_tagset_busy_iter(&ctrl->tag_set,
967 nvme_cancel_request, &ctrl->ctrl);
968 nvme_rdma_destroy_io_queues(ctrl, false);
971 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
972 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
973 nvme_cancel_request, &ctrl->ctrl);
974 nvme_rdma_destroy_admin_queue(ctrl, false);
977 * queues are not a live anymore, so restart the queues to fail fast
978 * new IO
980 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
981 nvme_start_queues(&ctrl->ctrl);
983 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
984 /* state change failure is ok if we're in DELETING state */
985 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
986 return;
989 nvme_rdma_reconnect_or_remove(ctrl);
992 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
994 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
995 return;
997 queue_work(nvme_wq, &ctrl->err_work);
1000 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1001 const char *op)
1003 struct nvme_rdma_queue *queue = cq->cq_context;
1004 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1006 if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1007 dev_info(ctrl->ctrl.device,
1008 "%s for CQE 0x%p failed with status %s (%d)\n",
1009 op, wc->wr_cqe,
1010 ib_wc_status_msg(wc->status), wc->status);
1011 nvme_rdma_error_recovery(ctrl);
1014 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1016 if (unlikely(wc->status != IB_WC_SUCCESS))
1017 nvme_rdma_wr_error(cq, wc, "MEMREG");
1020 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1022 struct nvme_rdma_request *req =
1023 container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe);
1024 struct request *rq = blk_mq_rq_from_pdu(req);
1026 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1027 nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
1028 return;
1031 if (refcount_dec_and_test(&req->ref))
1032 nvme_end_request(rq, req->status, req->result);
1036 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1037 struct nvme_rdma_request *req)
1039 struct ib_send_wr *bad_wr;
1040 struct ib_send_wr wr = {
1041 .opcode = IB_WR_LOCAL_INV,
1042 .next = NULL,
1043 .num_sge = 0,
1044 .send_flags = IB_SEND_SIGNALED,
1045 .ex.invalidate_rkey = req->mr->rkey,
1048 req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1049 wr.wr_cqe = &req->reg_cqe;
1051 return ib_post_send(queue->qp, &wr, &bad_wr);
1054 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1055 struct request *rq)
1057 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1058 struct nvme_rdma_device *dev = queue->device;
1059 struct ib_device *ibdev = dev->dev;
1061 if (!blk_rq_payload_bytes(rq))
1062 return;
1064 if (req->mr) {
1065 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1066 req->mr = NULL;
1069 ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1070 req->nents, rq_data_dir(rq) ==
1071 WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1073 nvme_cleanup_cmd(rq);
1074 sg_free_table_chained(&req->sg_table, true);
1077 static int nvme_rdma_set_sg_null(struct nvme_command *c)
1079 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1081 sg->addr = 0;
1082 put_unaligned_le24(0, sg->length);
1083 put_unaligned_le32(0, sg->key);
1084 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1085 return 0;
1088 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
1089 struct nvme_rdma_request *req, struct nvme_command *c)
1091 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1093 req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
1094 req->sge[1].length = sg_dma_len(req->sg_table.sgl);
1095 req->sge[1].lkey = queue->device->pd->local_dma_lkey;
1097 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1098 sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
1099 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1101 req->num_sge++;
1102 return 0;
1105 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1106 struct nvme_rdma_request *req, struct nvme_command *c)
1108 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1110 sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
1111 put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
1112 put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
1113 sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1114 return 0;
1117 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1118 struct nvme_rdma_request *req, struct nvme_command *c,
1119 int count)
1121 struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1122 int nr;
1124 req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs);
1125 if (WARN_ON_ONCE(!req->mr))
1126 return -EAGAIN;
1129 * Align the MR to a 4K page size to match the ctrl page size and
1130 * the block virtual boundary.
1132 nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K);
1133 if (unlikely(nr < count)) {
1134 ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1135 req->mr = NULL;
1136 if (nr < 0)
1137 return nr;
1138 return -EINVAL;
1141 ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1143 req->reg_cqe.done = nvme_rdma_memreg_done;
1144 memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1145 req->reg_wr.wr.opcode = IB_WR_REG_MR;
1146 req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1147 req->reg_wr.wr.num_sge = 0;
1148 req->reg_wr.mr = req->mr;
1149 req->reg_wr.key = req->mr->rkey;
1150 req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1151 IB_ACCESS_REMOTE_READ |
1152 IB_ACCESS_REMOTE_WRITE;
1154 sg->addr = cpu_to_le64(req->mr->iova);
1155 put_unaligned_le24(req->mr->length, sg->length);
1156 put_unaligned_le32(req->mr->rkey, sg->key);
1157 sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1158 NVME_SGL_FMT_INVALIDATE;
1160 return 0;
1163 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
1164 struct request *rq, struct nvme_command *c)
1166 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1167 struct nvme_rdma_device *dev = queue->device;
1168 struct ib_device *ibdev = dev->dev;
1169 int count, ret;
1171 req->num_sge = 1;
1172 refcount_set(&req->ref, 2); /* send and recv completions */
1174 c->common.flags |= NVME_CMD_SGL_METABUF;
1176 if (!blk_rq_payload_bytes(rq))
1177 return nvme_rdma_set_sg_null(c);
1179 req->sg_table.sgl = req->first_sgl;
1180 ret = sg_alloc_table_chained(&req->sg_table,
1181 blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
1182 if (ret)
1183 return -ENOMEM;
1185 req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
1187 count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
1188 rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1189 if (unlikely(count <= 0)) {
1190 sg_free_table_chained(&req->sg_table, true);
1191 return -EIO;
1194 if (count == 1) {
1195 if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1196 blk_rq_payload_bytes(rq) <=
1197 nvme_rdma_inline_data_size(queue))
1198 return nvme_rdma_map_sg_inline(queue, req, c);
1200 if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY)
1201 return nvme_rdma_map_sg_single(queue, req, c);
1204 return nvme_rdma_map_sg_fr(queue, req, c, count);
1207 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1209 struct nvme_rdma_qe *qe =
1210 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1211 struct nvme_rdma_request *req =
1212 container_of(qe, struct nvme_rdma_request, sqe);
1213 struct request *rq = blk_mq_rq_from_pdu(req);
1215 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1216 nvme_rdma_wr_error(cq, wc, "SEND");
1217 return;
1220 if (refcount_dec_and_test(&req->ref))
1221 nvme_end_request(rq, req->status, req->result);
1224 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1225 struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1226 struct ib_send_wr *first)
1228 struct ib_send_wr wr, *bad_wr;
1229 int ret;
1231 sge->addr = qe->dma;
1232 sge->length = sizeof(struct nvme_command),
1233 sge->lkey = queue->device->pd->local_dma_lkey;
1235 wr.next = NULL;
1236 wr.wr_cqe = &qe->cqe;
1237 wr.sg_list = sge;
1238 wr.num_sge = num_sge;
1239 wr.opcode = IB_WR_SEND;
1240 wr.send_flags = IB_SEND_SIGNALED;
1242 if (first)
1243 first->next = &wr;
1244 else
1245 first = &wr;
1247 ret = ib_post_send(queue->qp, first, &bad_wr);
1248 if (unlikely(ret)) {
1249 dev_err(queue->ctrl->ctrl.device,
1250 "%s failed with error code %d\n", __func__, ret);
1252 return ret;
1255 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1256 struct nvme_rdma_qe *qe)
1258 struct ib_recv_wr wr, *bad_wr;
1259 struct ib_sge list;
1260 int ret;
1262 list.addr = qe->dma;
1263 list.length = sizeof(struct nvme_completion);
1264 list.lkey = queue->device->pd->local_dma_lkey;
1266 qe->cqe.done = nvme_rdma_recv_done;
1268 wr.next = NULL;
1269 wr.wr_cqe = &qe->cqe;
1270 wr.sg_list = &list;
1271 wr.num_sge = 1;
1273 ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1274 if (unlikely(ret)) {
1275 dev_err(queue->ctrl->ctrl.device,
1276 "%s failed with error code %d\n", __func__, ret);
1278 return ret;
1281 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1283 u32 queue_idx = nvme_rdma_queue_idx(queue);
1285 if (queue_idx == 0)
1286 return queue->ctrl->admin_tag_set.tags[queue_idx];
1287 return queue->ctrl->tag_set.tags[queue_idx - 1];
1290 static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc)
1292 if (unlikely(wc->status != IB_WC_SUCCESS))
1293 nvme_rdma_wr_error(cq, wc, "ASYNC");
1296 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg)
1298 struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1299 struct nvme_rdma_queue *queue = &ctrl->queues[0];
1300 struct ib_device *dev = queue->device->dev;
1301 struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1302 struct nvme_command *cmd = sqe->data;
1303 struct ib_sge sge;
1304 int ret;
1306 ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1308 memset(cmd, 0, sizeof(*cmd));
1309 cmd->common.opcode = nvme_admin_async_event;
1310 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
1311 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1312 nvme_rdma_set_sg_null(cmd);
1314 sqe->cqe.done = nvme_rdma_async_done;
1316 ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1317 DMA_TO_DEVICE);
1319 ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL);
1320 WARN_ON_ONCE(ret);
1323 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1324 struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1326 struct request *rq;
1327 struct nvme_rdma_request *req;
1328 int ret = 0;
1330 rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1331 if (!rq) {
1332 dev_err(queue->ctrl->ctrl.device,
1333 "tag 0x%x on QP %#x not found\n",
1334 cqe->command_id, queue->qp->qp_num);
1335 nvme_rdma_error_recovery(queue->ctrl);
1336 return ret;
1338 req = blk_mq_rq_to_pdu(rq);
1340 req->status = cqe->status;
1341 req->result = cqe->result;
1343 if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
1344 if (unlikely(wc->ex.invalidate_rkey != req->mr->rkey)) {
1345 dev_err(queue->ctrl->ctrl.device,
1346 "Bogus remote invalidation for rkey %#x\n",
1347 req->mr->rkey);
1348 nvme_rdma_error_recovery(queue->ctrl);
1350 } else if (req->mr) {
1351 ret = nvme_rdma_inv_rkey(queue, req);
1352 if (unlikely(ret < 0)) {
1353 dev_err(queue->ctrl->ctrl.device,
1354 "Queueing INV WR for rkey %#x failed (%d)\n",
1355 req->mr->rkey, ret);
1356 nvme_rdma_error_recovery(queue->ctrl);
1358 /* the local invalidation completion will end the request */
1359 return 0;
1362 if (refcount_dec_and_test(&req->ref)) {
1363 if (rq->tag == tag)
1364 ret = 1;
1365 nvme_end_request(rq, req->status, req->result);
1368 return ret;
1371 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1373 struct nvme_rdma_qe *qe =
1374 container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1375 struct nvme_rdma_queue *queue = cq->cq_context;
1376 struct ib_device *ibdev = queue->device->dev;
1377 struct nvme_completion *cqe = qe->data;
1378 const size_t len = sizeof(struct nvme_completion);
1379 int ret = 0;
1381 if (unlikely(wc->status != IB_WC_SUCCESS)) {
1382 nvme_rdma_wr_error(cq, wc, "RECV");
1383 return 0;
1386 ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1388 * AEN requests are special as they don't time out and can
1389 * survive any kind of queue freeze and often don't respond to
1390 * aborts. We don't even bother to allocate a struct request
1391 * for them but rather special case them here.
1393 if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1394 cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH))
1395 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1396 &cqe->result);
1397 else
1398 ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1399 ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1401 nvme_rdma_post_recv(queue, qe);
1402 return ret;
1405 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1407 __nvme_rdma_recv_done(cq, wc, -1);
1410 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1412 int ret, i;
1414 for (i = 0; i < queue->queue_size; i++) {
1415 ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1416 if (ret)
1417 goto out_destroy_queue_ib;
1420 return 0;
1422 out_destroy_queue_ib:
1423 nvme_rdma_destroy_queue_ib(queue);
1424 return ret;
1427 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1428 struct rdma_cm_event *ev)
1430 struct rdma_cm_id *cm_id = queue->cm_id;
1431 int status = ev->status;
1432 const char *rej_msg;
1433 const struct nvme_rdma_cm_rej *rej_data;
1434 u8 rej_data_len;
1436 rej_msg = rdma_reject_msg(cm_id, status);
1437 rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1439 if (rej_data && rej_data_len >= sizeof(u16)) {
1440 u16 sts = le16_to_cpu(rej_data->sts);
1442 dev_err(queue->ctrl->ctrl.device,
1443 "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1444 status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1445 } else {
1446 dev_err(queue->ctrl->ctrl.device,
1447 "Connect rejected: status %d (%s).\n", status, rej_msg);
1450 return -ECONNRESET;
1453 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1455 int ret;
1457 ret = nvme_rdma_create_queue_ib(queue);
1458 if (ret)
1459 return ret;
1461 ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1462 if (ret) {
1463 dev_err(queue->ctrl->ctrl.device,
1464 "rdma_resolve_route failed (%d).\n",
1465 queue->cm_error);
1466 goto out_destroy_queue;
1469 return 0;
1471 out_destroy_queue:
1472 nvme_rdma_destroy_queue_ib(queue);
1473 return ret;
1476 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1478 struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1479 struct rdma_conn_param param = { };
1480 struct nvme_rdma_cm_req priv = { };
1481 int ret;
1483 param.qp_num = queue->qp->qp_num;
1484 param.flow_control = 1;
1486 param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1487 /* maximum retry count */
1488 param.retry_count = 7;
1489 param.rnr_retry_count = 7;
1490 param.private_data = &priv;
1491 param.private_data_len = sizeof(priv);
1493 priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1494 priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1496 * set the admin queue depth to the minimum size
1497 * specified by the Fabrics standard.
1499 if (priv.qid == 0) {
1500 priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1501 priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
1502 } else {
1504 * current interpretation of the fabrics spec
1505 * is at minimum you make hrqsize sqsize+1, or a
1506 * 1's based representation of sqsize.
1508 priv.hrqsize = cpu_to_le16(queue->queue_size);
1509 priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1512 ret = rdma_connect(queue->cm_id, &param);
1513 if (ret) {
1514 dev_err(ctrl->ctrl.device,
1515 "rdma_connect failed (%d).\n", ret);
1516 goto out_destroy_queue_ib;
1519 return 0;
1521 out_destroy_queue_ib:
1522 nvme_rdma_destroy_queue_ib(queue);
1523 return ret;
1526 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1527 struct rdma_cm_event *ev)
1529 struct nvme_rdma_queue *queue = cm_id->context;
1530 int cm_error = 0;
1532 dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1533 rdma_event_msg(ev->event), ev->event,
1534 ev->status, cm_id);
1536 switch (ev->event) {
1537 case RDMA_CM_EVENT_ADDR_RESOLVED:
1538 cm_error = nvme_rdma_addr_resolved(queue);
1539 break;
1540 case RDMA_CM_EVENT_ROUTE_RESOLVED:
1541 cm_error = nvme_rdma_route_resolved(queue);
1542 break;
1543 case RDMA_CM_EVENT_ESTABLISHED:
1544 queue->cm_error = nvme_rdma_conn_established(queue);
1545 /* complete cm_done regardless of success/failure */
1546 complete(&queue->cm_done);
1547 return 0;
1548 case RDMA_CM_EVENT_REJECTED:
1549 nvme_rdma_destroy_queue_ib(queue);
1550 cm_error = nvme_rdma_conn_rejected(queue, ev);
1551 break;
1552 case RDMA_CM_EVENT_ROUTE_ERROR:
1553 case RDMA_CM_EVENT_CONNECT_ERROR:
1554 case RDMA_CM_EVENT_UNREACHABLE:
1555 nvme_rdma_destroy_queue_ib(queue);
1556 case RDMA_CM_EVENT_ADDR_ERROR:
1557 dev_dbg(queue->ctrl->ctrl.device,
1558 "CM error event %d\n", ev->event);
1559 cm_error = -ECONNRESET;
1560 break;
1561 case RDMA_CM_EVENT_DISCONNECTED:
1562 case RDMA_CM_EVENT_ADDR_CHANGE:
1563 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1564 dev_dbg(queue->ctrl->ctrl.device,
1565 "disconnect received - connection closed\n");
1566 nvme_rdma_error_recovery(queue->ctrl);
1567 break;
1568 case RDMA_CM_EVENT_DEVICE_REMOVAL:
1569 /* device removal is handled via the ib_client API */
1570 break;
1571 default:
1572 dev_err(queue->ctrl->ctrl.device,
1573 "Unexpected RDMA CM event (%d)\n", ev->event);
1574 nvme_rdma_error_recovery(queue->ctrl);
1575 break;
1578 if (cm_error) {
1579 queue->cm_error = cm_error;
1580 complete(&queue->cm_done);
1583 return 0;
1586 static enum blk_eh_timer_return
1587 nvme_rdma_timeout(struct request *rq, bool reserved)
1589 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1591 dev_warn(req->queue->ctrl->ctrl.device,
1592 "I/O %d QID %d timeout, reset controller\n",
1593 rq->tag, nvme_rdma_queue_idx(req->queue));
1595 /* queue error recovery */
1596 nvme_rdma_error_recovery(req->queue->ctrl);
1598 /* fail with DNR on cmd timeout */
1599 nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1601 return BLK_EH_HANDLED;
1605 * We cannot accept any other command until the Connect command has completed.
1607 static inline blk_status_t
1608 nvme_rdma_is_ready(struct nvme_rdma_queue *queue, struct request *rq)
1610 if (unlikely(!test_bit(NVME_RDMA_Q_LIVE, &queue->flags)))
1611 return nvmf_check_init_req(&queue->ctrl->ctrl, rq);
1612 return BLK_STS_OK;
1615 static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1616 const struct blk_mq_queue_data *bd)
1618 struct nvme_ns *ns = hctx->queue->queuedata;
1619 struct nvme_rdma_queue *queue = hctx->driver_data;
1620 struct request *rq = bd->rq;
1621 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1622 struct nvme_rdma_qe *sqe = &req->sqe;
1623 struct nvme_command *c = sqe->data;
1624 struct ib_device *dev;
1625 blk_status_t ret;
1626 int err;
1628 WARN_ON_ONCE(rq->tag < 0);
1630 ret = nvme_rdma_is_ready(queue, rq);
1631 if (unlikely(ret))
1632 return ret;
1634 dev = queue->device->dev;
1635 ib_dma_sync_single_for_cpu(dev, sqe->dma,
1636 sizeof(struct nvme_command), DMA_TO_DEVICE);
1638 ret = nvme_setup_cmd(ns, rq, c);
1639 if (ret)
1640 return ret;
1642 blk_mq_start_request(rq);
1644 err = nvme_rdma_map_data(queue, rq, c);
1645 if (unlikely(err < 0)) {
1646 dev_err(queue->ctrl->ctrl.device,
1647 "Failed to map data (%d)\n", err);
1648 nvme_cleanup_cmd(rq);
1649 goto err;
1652 sqe->cqe.done = nvme_rdma_send_done;
1654 ib_dma_sync_single_for_device(dev, sqe->dma,
1655 sizeof(struct nvme_command), DMA_TO_DEVICE);
1657 err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1658 req->mr ? &req->reg_wr.wr : NULL);
1659 if (unlikely(err)) {
1660 nvme_rdma_unmap_data(queue, rq);
1661 goto err;
1664 return BLK_STS_OK;
1665 err:
1666 if (err == -ENOMEM || err == -EAGAIN)
1667 return BLK_STS_RESOURCE;
1668 return BLK_STS_IOERR;
1671 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1673 struct nvme_rdma_queue *queue = hctx->driver_data;
1674 struct ib_cq *cq = queue->ib_cq;
1675 struct ib_wc wc;
1676 int found = 0;
1678 while (ib_poll_cq(cq, 1, &wc) > 0) {
1679 struct ib_cqe *cqe = wc.wr_cqe;
1681 if (cqe) {
1682 if (cqe->done == nvme_rdma_recv_done)
1683 found |= __nvme_rdma_recv_done(cq, &wc, tag);
1684 else
1685 cqe->done(cq, &wc);
1689 return found;
1692 static void nvme_rdma_complete_rq(struct request *rq)
1694 struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1696 nvme_rdma_unmap_data(req->queue, rq);
1697 nvme_complete_rq(rq);
1700 static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
1702 struct nvme_rdma_ctrl *ctrl = set->driver_data;
1704 return blk_mq_rdma_map_queues(set, ctrl->device->dev, 0);
1707 static const struct blk_mq_ops nvme_rdma_mq_ops = {
1708 .queue_rq = nvme_rdma_queue_rq,
1709 .complete = nvme_rdma_complete_rq,
1710 .init_request = nvme_rdma_init_request,
1711 .exit_request = nvme_rdma_exit_request,
1712 .init_hctx = nvme_rdma_init_hctx,
1713 .poll = nvme_rdma_poll,
1714 .timeout = nvme_rdma_timeout,
1715 .map_queues = nvme_rdma_map_queues,
1718 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1719 .queue_rq = nvme_rdma_queue_rq,
1720 .complete = nvme_rdma_complete_rq,
1721 .init_request = nvme_rdma_init_request,
1722 .exit_request = nvme_rdma_exit_request,
1723 .init_hctx = nvme_rdma_init_admin_hctx,
1724 .timeout = nvme_rdma_timeout,
1727 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1729 if (ctrl->ctrl.queue_count > 1) {
1730 nvme_stop_queues(&ctrl->ctrl);
1731 blk_mq_tagset_busy_iter(&ctrl->tag_set,
1732 nvme_cancel_request, &ctrl->ctrl);
1733 nvme_rdma_destroy_io_queues(ctrl, shutdown);
1736 if (shutdown)
1737 nvme_shutdown_ctrl(&ctrl->ctrl);
1738 else
1739 nvme_disable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
1741 blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
1742 blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1743 nvme_cancel_request, &ctrl->ctrl);
1744 blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
1745 nvme_rdma_destroy_admin_queue(ctrl, shutdown);
1748 static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
1750 nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true);
1753 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1755 struct nvme_rdma_ctrl *ctrl =
1756 container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
1757 int ret;
1758 bool changed;
1760 nvme_stop_ctrl(&ctrl->ctrl);
1761 nvme_rdma_shutdown_ctrl(ctrl, false);
1763 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
1764 /* state change failure should never happen */
1765 WARN_ON_ONCE(1);
1766 return;
1769 ret = nvme_rdma_configure_admin_queue(ctrl, false);
1770 if (ret)
1771 goto out_fail;
1773 if (ctrl->ctrl.queue_count > 1) {
1774 ret = nvme_rdma_configure_io_queues(ctrl, false);
1775 if (ret)
1776 goto out_fail;
1779 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1780 if (!changed) {
1781 /* state change failure is ok if we're in DELETING state */
1782 WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
1783 return;
1786 nvme_start_ctrl(&ctrl->ctrl);
1788 return;
1790 out_fail:
1791 ++ctrl->ctrl.nr_reconnects;
1792 nvme_rdma_reconnect_or_remove(ctrl);
1795 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1796 .name = "rdma",
1797 .module = THIS_MODULE,
1798 .flags = NVME_F_FABRICS,
1799 .reg_read32 = nvmf_reg_read32,
1800 .reg_read64 = nvmf_reg_read64,
1801 .reg_write32 = nvmf_reg_write32,
1802 .free_ctrl = nvme_rdma_free_ctrl,
1803 .submit_async_event = nvme_rdma_submit_async_event,
1804 .delete_ctrl = nvme_rdma_delete_ctrl,
1805 .get_address = nvmf_get_address,
1806 .stop_ctrl = nvme_rdma_stop_ctrl,
1809 static inline bool
1810 __nvme_rdma_options_match(struct nvme_rdma_ctrl *ctrl,
1811 struct nvmf_ctrl_options *opts)
1813 char *stdport = __stringify(NVME_RDMA_IP_PORT);
1816 if (!nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts) ||
1817 strcmp(opts->traddr, ctrl->ctrl.opts->traddr))
1818 return false;
1820 if (opts->mask & NVMF_OPT_TRSVCID &&
1821 ctrl->ctrl.opts->mask & NVMF_OPT_TRSVCID) {
1822 if (strcmp(opts->trsvcid, ctrl->ctrl.opts->trsvcid))
1823 return false;
1824 } else if (opts->mask & NVMF_OPT_TRSVCID) {
1825 if (strcmp(opts->trsvcid, stdport))
1826 return false;
1827 } else if (ctrl->ctrl.opts->mask & NVMF_OPT_TRSVCID) {
1828 if (strcmp(stdport, ctrl->ctrl.opts->trsvcid))
1829 return false;
1831 /* else, it's a match as both have stdport. Fall to next checks */
1834 * checking the local address is rough. In most cases, one
1835 * is not specified and the host port is selected by the stack.
1837 * Assume no match if:
1838 * local address is specified and address is not the same
1839 * local address is not specified but remote is, or vice versa
1840 * (admin using specific host_traddr when it matters).
1842 if (opts->mask & NVMF_OPT_HOST_TRADDR &&
1843 ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR) {
1844 if (strcmp(opts->host_traddr, ctrl->ctrl.opts->host_traddr))
1845 return false;
1846 } else if (opts->mask & NVMF_OPT_HOST_TRADDR ||
1847 ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
1848 return false;
1850 * if neither controller had an host port specified, assume it's
1851 * a match as everything else matched.
1854 return true;
1858 * Fails a connection request if it matches an existing controller
1859 * (association) with the same tuple:
1860 * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
1862 * if local address is not specified in the request, it will match an
1863 * existing controller with all the other parameters the same and no
1864 * local port address specified as well.
1866 * The ports don't need to be compared as they are intrinsically
1867 * already matched by the port pointers supplied.
1869 static bool
1870 nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts)
1872 struct nvme_rdma_ctrl *ctrl;
1873 bool found = false;
1875 mutex_lock(&nvme_rdma_ctrl_mutex);
1876 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
1877 found = __nvme_rdma_options_match(ctrl, opts);
1878 if (found)
1879 break;
1881 mutex_unlock(&nvme_rdma_ctrl_mutex);
1883 return found;
1886 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1887 struct nvmf_ctrl_options *opts)
1889 struct nvme_rdma_ctrl *ctrl;
1890 int ret;
1891 bool changed;
1892 char *port;
1894 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1895 if (!ctrl)
1896 return ERR_PTR(-ENOMEM);
1897 ctrl->ctrl.opts = opts;
1898 INIT_LIST_HEAD(&ctrl->list);
1900 if (opts->mask & NVMF_OPT_TRSVCID)
1901 port = opts->trsvcid;
1902 else
1903 port = __stringify(NVME_RDMA_IP_PORT);
1905 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1906 opts->traddr, port, &ctrl->addr);
1907 if (ret) {
1908 pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
1909 goto out_free_ctrl;
1912 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
1913 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1914 opts->host_traddr, NULL, &ctrl->src_addr);
1915 if (ret) {
1916 pr_err("malformed src address passed: %s\n",
1917 opts->host_traddr);
1918 goto out_free_ctrl;
1922 if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) {
1923 ret = -EALREADY;
1924 goto out_free_ctrl;
1927 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1928 0 /* no quirks, we're perfect! */);
1929 if (ret)
1930 goto out_free_ctrl;
1932 INIT_DELAYED_WORK(&ctrl->reconnect_work,
1933 nvme_rdma_reconnect_ctrl_work);
1934 INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1935 INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
1937 ctrl->ctrl.queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1938 ctrl->ctrl.sqsize = opts->queue_size - 1;
1939 ctrl->ctrl.kato = opts->kato;
1941 ret = -ENOMEM;
1942 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
1943 GFP_KERNEL);
1944 if (!ctrl->queues)
1945 goto out_uninit_ctrl;
1947 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING);
1948 WARN_ON_ONCE(!changed);
1950 ret = nvme_rdma_configure_admin_queue(ctrl, true);
1951 if (ret)
1952 goto out_kfree_queues;
1954 /* sanity check icdoff */
1955 if (ctrl->ctrl.icdoff) {
1956 dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1957 ret = -EINVAL;
1958 goto out_remove_admin_queue;
1961 /* sanity check keyed sgls */
1962 if (!(ctrl->ctrl.sgls & (1 << 20))) {
1963 dev_err(ctrl->ctrl.device, "Mandatory keyed sgls are not support\n");
1964 ret = -EINVAL;
1965 goto out_remove_admin_queue;
1968 if (opts->queue_size > ctrl->ctrl.maxcmd) {
1969 /* warn if maxcmd is lower than queue_size */
1970 dev_warn(ctrl->ctrl.device,
1971 "queue_size %zu > ctrl maxcmd %u, clamping down\n",
1972 opts->queue_size, ctrl->ctrl.maxcmd);
1973 opts->queue_size = ctrl->ctrl.maxcmd;
1976 if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1977 /* warn if sqsize is lower than queue_size */
1978 dev_warn(ctrl->ctrl.device,
1979 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1980 opts->queue_size, ctrl->ctrl.sqsize + 1);
1981 opts->queue_size = ctrl->ctrl.sqsize + 1;
1984 if (opts->nr_io_queues) {
1985 ret = nvme_rdma_configure_io_queues(ctrl, true);
1986 if (ret)
1987 goto out_remove_admin_queue;
1990 changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1991 WARN_ON_ONCE(!changed);
1993 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
1994 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
1996 nvme_get_ctrl(&ctrl->ctrl);
1998 mutex_lock(&nvme_rdma_ctrl_mutex);
1999 list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
2000 mutex_unlock(&nvme_rdma_ctrl_mutex);
2002 nvme_start_ctrl(&ctrl->ctrl);
2004 return &ctrl->ctrl;
2006 out_remove_admin_queue:
2007 nvme_rdma_destroy_admin_queue(ctrl, true);
2008 out_kfree_queues:
2009 kfree(ctrl->queues);
2010 out_uninit_ctrl:
2011 nvme_uninit_ctrl(&ctrl->ctrl);
2012 nvme_put_ctrl(&ctrl->ctrl);
2013 if (ret > 0)
2014 ret = -EIO;
2015 return ERR_PTR(ret);
2016 out_free_ctrl:
2017 kfree(ctrl);
2018 return ERR_PTR(ret);
2021 static struct nvmf_transport_ops nvme_rdma_transport = {
2022 .name = "rdma",
2023 .module = THIS_MODULE,
2024 .required_opts = NVMF_OPT_TRADDR,
2025 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2026 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
2027 .create_ctrl = nvme_rdma_create_ctrl,
2030 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2032 struct nvme_rdma_ctrl *ctrl;
2033 struct nvme_rdma_device *ndev;
2034 bool found = false;
2036 mutex_lock(&device_list_mutex);
2037 list_for_each_entry(ndev, &device_list, entry) {
2038 if (ndev->dev == ib_device) {
2039 found = true;
2040 break;
2043 mutex_unlock(&device_list_mutex);
2045 if (!found)
2046 return;
2048 /* Delete all controllers using this device */
2049 mutex_lock(&nvme_rdma_ctrl_mutex);
2050 list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2051 if (ctrl->device->dev != ib_device)
2052 continue;
2053 nvme_delete_ctrl(&ctrl->ctrl);
2055 mutex_unlock(&nvme_rdma_ctrl_mutex);
2057 flush_workqueue(nvme_delete_wq);
2060 static struct ib_client nvme_rdma_ib_client = {
2061 .name = "nvme_rdma",
2062 .remove = nvme_rdma_remove_one
2065 static int __init nvme_rdma_init_module(void)
2067 int ret;
2069 ret = ib_register_client(&nvme_rdma_ib_client);
2070 if (ret)
2071 return ret;
2073 ret = nvmf_register_transport(&nvme_rdma_transport);
2074 if (ret)
2075 goto err_unreg_client;
2077 return 0;
2079 err_unreg_client:
2080 ib_unregister_client(&nvme_rdma_ib_client);
2081 return ret;
2084 static void __exit nvme_rdma_cleanup_module(void)
2086 nvmf_unregister_transport(&nvme_rdma_transport);
2087 ib_unregister_client(&nvme_rdma_ib_client);
2090 module_init(nvme_rdma_init_module);
2091 module_exit(nvme_rdma_cleanup_module);
2093 MODULE_LICENSE("GPL v2");