xref: /openbmc/linux/drivers/nvme/host/rdma.c (revision faa16bc4)
1 /*
2  * NVMe over Fabrics RDMA host code.
3  * Copyright (c) 2015-2016 HGST, a Western Digital Company.
4  *
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.
8  *
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.
13  */
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>
30 
31 #include <rdma/ib_verbs.h>
32 #include <rdma/rdma_cm.h>
33 #include <linux/nvme-rdma.h>
34 
35 #include "nvme.h"
36 #include "fabrics.h"
37 
38 
39 #define NVME_RDMA_CONNECT_TIMEOUT_MS	3000		/* 3 second */
40 
41 #define NVME_RDMA_MAX_SEGMENTS		256
42 
43 #define NVME_RDMA_MAX_INLINE_SEGMENTS	1
44 
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 };
51 
52 struct nvme_rdma_qe {
53 	struct ib_cqe		cqe;
54 	void			*data;
55 	u64			dma;
56 };
57 
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[];
74 };
75 
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,
80 };
81 
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;
90 
91 	unsigned long		flags;
92 	struct rdma_cm_id	*cm_id;
93 	int			cm_error;
94 	struct completion	cm_done;
95 };
96 
97 struct nvme_rdma_ctrl {
98 	/* read only in the hot path */
99 	struct nvme_rdma_queue	*queues;
100 
101 	/* other member variables */
102 	struct blk_mq_tag_set	tag_set;
103 	struct work_struct	err_work;
104 
105 	struct nvme_rdma_qe	async_event_sqe;
106 
107 	struct delayed_work	reconnect_work;
108 
109 	struct list_head	list;
110 
111 	struct blk_mq_tag_set	admin_tag_set;
112 	struct nvme_rdma_device	*device;
113 
114 	u32			max_fr_pages;
115 
116 	struct sockaddr_storage addr;
117 	struct sockaddr_storage src_addr;
118 
119 	struct nvme_ctrl	ctrl;
120 };
121 
122 static inline struct nvme_rdma_ctrl *to_rdma_ctrl(struct nvme_ctrl *ctrl)
123 {
124 	return container_of(ctrl, struct nvme_rdma_ctrl, ctrl);
125 }
126 
127 static LIST_HEAD(device_list);
128 static DEFINE_MUTEX(device_list_mutex);
129 
130 static LIST_HEAD(nvme_rdma_ctrl_list);
131 static DEFINE_MUTEX(nvme_rdma_ctrl_mutex);
132 
133 /*
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.
137  */
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");
142 
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);
146 
147 static const struct blk_mq_ops nvme_rdma_mq_ops;
148 static const struct blk_mq_ops nvme_rdma_admin_mq_ops;
149 
150 /* XXX: really should move to a generic header sooner or later.. */
151 static inline void put_unaligned_le24(u32 val, u8 *p)
152 {
153 	*p++ = val;
154 	*p++ = val >> 8;
155 	*p++ = val >> 16;
156 }
157 
158 static inline int nvme_rdma_queue_idx(struct nvme_rdma_queue *queue)
159 {
160 	return queue - queue->ctrl->queues;
161 }
162 
163 static inline size_t nvme_rdma_inline_data_size(struct nvme_rdma_queue *queue)
164 {
165 	return queue->cmnd_capsule_len - sizeof(struct nvme_command);
166 }
167 
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)
170 {
171 	ib_dma_unmap_single(ibdev, qe->dma, capsule_size, dir);
172 	kfree(qe->data);
173 }
174 
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)
177 {
178 	qe->data = kzalloc(capsule_size, GFP_KERNEL);
179 	if (!qe->data)
180 		return -ENOMEM;
181 
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;
186 	}
187 
188 	return 0;
189 }
190 
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)
194 {
195 	int i;
196 
197 	for (i = 0; i < ib_queue_size; i++)
198 		nvme_rdma_free_qe(ibdev, &ring[i], capsule_size, dir);
199 	kfree(ring);
200 }
201 
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)
205 {
206 	struct nvme_rdma_qe *ring;
207 	int i;
208 
209 	ring = kcalloc(ib_queue_size, sizeof(struct nvme_rdma_qe), GFP_KERNEL);
210 	if (!ring)
211 		return NULL;
212 
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;
216 	}
217 
218 	return ring;
219 
220 out_free_ring:
221 	nvme_rdma_free_ring(ibdev, ring, i, capsule_size, dir);
222 	return NULL;
223 }
224 
225 static void nvme_rdma_qp_event(struct ib_event *event, void *context)
226 {
227 	pr_debug("QP event %s (%d)\n",
228 		 ib_event_msg(event->event), event->event);
229 
230 }
231 
232 static int nvme_rdma_wait_for_cm(struct nvme_rdma_queue *queue)
233 {
234 	wait_for_completion_interruptible_timeout(&queue->cm_done,
235 			msecs_to_jiffies(NVME_RDMA_CONNECT_TIMEOUT_MS) + 1);
236 	return queue->cm_error;
237 }
238 
239 static int nvme_rdma_create_qp(struct nvme_rdma_queue *queue, const int factor)
240 {
241 	struct nvme_rdma_device *dev = queue->device;
242 	struct ib_qp_init_attr init_attr;
243 	int ret;
244 
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;
257 
258 	ret = rdma_create_qp(queue->cm_id, dev->pd, &init_attr);
259 
260 	queue->qp = queue->cm_id->qp;
261 	return ret;
262 }
263 
264 static void nvme_rdma_exit_request(struct blk_mq_tag_set *set,
265 		struct request *rq, unsigned int hctx_idx)
266 {
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;
272 
273 	nvme_rdma_free_qe(dev->dev, &req->sqe, sizeof(struct nvme_command),
274 			DMA_TO_DEVICE);
275 }
276 
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)
280 {
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;
288 
289 	ret = nvme_rdma_alloc_qe(ibdev, &req->sqe, sizeof(struct nvme_command),
290 			DMA_TO_DEVICE);
291 	if (ret)
292 		return ret;
293 
294 	req->queue = queue;
295 
296 	return 0;
297 }
298 
299 static int nvme_rdma_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
300 		unsigned int hctx_idx)
301 {
302 	struct nvme_rdma_ctrl *ctrl = data;
303 	struct nvme_rdma_queue *queue = &ctrl->queues[hctx_idx + 1];
304 
305 	BUG_ON(hctx_idx >= ctrl->ctrl.queue_count);
306 
307 	hctx->driver_data = queue;
308 	return 0;
309 }
310 
311 static int nvme_rdma_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
312 		unsigned int hctx_idx)
313 {
314 	struct nvme_rdma_ctrl *ctrl = data;
315 	struct nvme_rdma_queue *queue = &ctrl->queues[0];
316 
317 	BUG_ON(hctx_idx != 0);
318 
319 	hctx->driver_data = queue;
320 	return 0;
321 }
322 
323 static void nvme_rdma_free_dev(struct kref *ref)
324 {
325 	struct nvme_rdma_device *ndev =
326 		container_of(ref, struct nvme_rdma_device, ref);
327 
328 	mutex_lock(&device_list_mutex);
329 	list_del(&ndev->entry);
330 	mutex_unlock(&device_list_mutex);
331 
332 	ib_dealloc_pd(ndev->pd);
333 	kfree(ndev);
334 }
335 
336 static void nvme_rdma_dev_put(struct nvme_rdma_device *dev)
337 {
338 	kref_put(&dev->ref, nvme_rdma_free_dev);
339 }
340 
341 static int nvme_rdma_dev_get(struct nvme_rdma_device *dev)
342 {
343 	return kref_get_unless_zero(&dev->ref);
344 }
345 
346 static struct nvme_rdma_device *
347 nvme_rdma_find_get_device(struct rdma_cm_id *cm_id)
348 {
349 	struct nvme_rdma_device *ndev;
350 
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;
356 	}
357 
358 	ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
359 	if (!ndev)
360 		goto out_err;
361 
362 	ndev->dev = cm_id->device;
363 	kref_init(&ndev->ref);
364 
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;
369 
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;
375 	}
376 
377 	list_add(&ndev->entry, &device_list);
378 out_unlock:
379 	mutex_unlock(&device_list_mutex);
380 	return ndev;
381 
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;
389 }
390 
391 static void nvme_rdma_destroy_queue_ib(struct nvme_rdma_queue *queue)
392 {
393 	struct nvme_rdma_device *dev;
394 	struct ib_device *ibdev;
395 
396 	if (!test_and_clear_bit(NVME_RDMA_Q_TR_READY, &queue->flags))
397 		return;
398 
399 	dev = queue->device;
400 	ibdev = dev->dev;
401 
402 	ib_mr_pool_destroy(queue->qp, &queue->qp->rdma_mrs);
403 
404 	/*
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.
408 	 */
409 	ib_destroy_qp(queue->qp);
410 	ib_free_cq(queue->ib_cq);
411 
412 	nvme_rdma_free_ring(ibdev, queue->rsp_ring, queue->queue_size,
413 			sizeof(struct nvme_completion), DMA_FROM_DEVICE);
414 
415 	nvme_rdma_dev_put(dev);
416 }
417 
418 static int nvme_rdma_get_max_fr_pages(struct ib_device *ibdev)
419 {
420 	return min_t(u32, NVME_RDMA_MAX_SEGMENTS,
421 		     ibdev->attrs.max_fast_reg_page_list_len);
422 }
423 
424 static int nvme_rdma_create_queue_ib(struct nvme_rdma_queue *queue)
425 {
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;
431 
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;
437 	}
438 	ibdev = queue->device->dev;
439 
440 	/*
441 	 * Spread I/O queues completion vectors according their queue index.
442 	 * Admin queues can always go on completion vector 0.
443 	 */
444 	comp_vector = idx == 0 ? idx : idx - 1;
445 
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;
453 	}
454 
455 	ret = nvme_rdma_create_qp(queue, send_wr_factor);
456 	if (ret)
457 		goto out_destroy_ib_cq;
458 
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;
464 	}
465 
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;
475 	}
476 
477 	set_bit(NVME_RDMA_Q_TR_READY, &queue->flags);
478 
479 	return 0;
480 
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;
491 }
492 
493 static int nvme_rdma_alloc_queue(struct nvme_rdma_ctrl *ctrl,
494 		int idx, size_t queue_size)
495 {
496 	struct nvme_rdma_queue *queue;
497 	struct sockaddr *src_addr = NULL;
498 	int ret;
499 
500 	queue = &ctrl->queues[idx];
501 	queue->ctrl = ctrl;
502 	init_completion(&queue->cm_done);
503 
504 	if (idx > 0)
505 		queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
506 	else
507 		queue->cmnd_capsule_len = sizeof(struct nvme_command);
508 
509 	queue->queue_size = queue_size;
510 
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);
517 	}
518 
519 	if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
520 		src_addr = (struct sockaddr *)&ctrl->src_addr;
521 
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;
530 	}
531 
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;
537 	}
538 
539 	set_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags);
540 
541 	return 0;
542 
543 out_destroy_cm_id:
544 	rdma_destroy_id(queue->cm_id);
545 	nvme_rdma_destroy_queue_ib(queue);
546 	return ret;
547 }
548 
549 static void nvme_rdma_stop_queue(struct nvme_rdma_queue *queue)
550 {
551 	if (!test_and_clear_bit(NVME_RDMA_Q_LIVE, &queue->flags))
552 		return;
553 
554 	rdma_disconnect(queue->cm_id);
555 	ib_drain_qp(queue->qp);
556 }
557 
558 static void nvme_rdma_free_queue(struct nvme_rdma_queue *queue)
559 {
560 	if (!test_and_clear_bit(NVME_RDMA_Q_ALLOCATED, &queue->flags))
561 		return;
562 
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);
567 	}
568 
569 	nvme_rdma_destroy_queue_ib(queue);
570 	rdma_destroy_id(queue->cm_id);
571 }
572 
573 static void nvme_rdma_free_io_queues(struct nvme_rdma_ctrl *ctrl)
574 {
575 	int i;
576 
577 	for (i = 1; i < ctrl->ctrl.queue_count; i++)
578 		nvme_rdma_free_queue(&ctrl->queues[i]);
579 }
580 
581 static void nvme_rdma_stop_io_queues(struct nvme_rdma_ctrl *ctrl)
582 {
583 	int i;
584 
585 	for (i = 1; i < ctrl->ctrl.queue_count; i++)
586 		nvme_rdma_stop_queue(&ctrl->queues[i]);
587 }
588 
589 static int nvme_rdma_start_queue(struct nvme_rdma_ctrl *ctrl, int idx)
590 {
591 	int ret;
592 
593 	if (idx)
594 		ret = nvmf_connect_io_queue(&ctrl->ctrl, idx);
595 	else
596 		ret = nvmf_connect_admin_queue(&ctrl->ctrl);
597 
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;
604 }
605 
606 static int nvme_rdma_start_io_queues(struct nvme_rdma_ctrl *ctrl)
607 {
608 	int i, ret = 0;
609 
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;
614 	}
615 
616 	return 0;
617 
618 out_stop_queues:
619 	for (i--; i >= 1; i--)
620 		nvme_rdma_stop_queue(&ctrl->queues[i]);
621 	return ret;
622 }
623 
624 static int nvme_rdma_alloc_io_queues(struct nvme_rdma_ctrl *ctrl)
625 {
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;
630 
631 	nr_io_queues = min(opts->nr_io_queues, num_online_cpus());
632 
633 	/*
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.
637 	 */
638 	nr_io_queues = min_t(unsigned int, nr_io_queues,
639 				ibdev->num_comp_vectors);
640 
641 	ret = nvme_set_queue_count(&ctrl->ctrl, &nr_io_queues);
642 	if (ret)
643 		return ret;
644 
645 	ctrl->ctrl.queue_count = nr_io_queues + 1;
646 	if (ctrl->ctrl.queue_count < 2)
647 		return 0;
648 
649 	dev_info(ctrl->ctrl.device,
650 		"creating %d I/O queues.\n", nr_io_queues);
651 
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;
657 	}
658 
659 	return 0;
660 
661 out_free_queues:
662 	for (i--; i >= 1; i--)
663 		nvme_rdma_free_queue(&ctrl->queues[i]);
664 
665 	return ret;
666 }
667 
668 static void nvme_rdma_free_tagset(struct nvme_ctrl *nctrl,
669 		struct blk_mq_tag_set *set)
670 {
671 	struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
672 
673 	blk_mq_free_tag_set(set);
674 	nvme_rdma_dev_put(ctrl->device);
675 }
676 
677 static struct blk_mq_tag_set *nvme_rdma_alloc_tagset(struct nvme_ctrl *nctrl,
678 		bool admin)
679 {
680 	struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
681 	struct blk_mq_tag_set *set;
682 	int ret;
683 
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;
710 	}
711 
712 	ret = blk_mq_alloc_tag_set(set);
713 	if (ret)
714 		goto out;
715 
716 	/*
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.
719 	 */
720 	ret = nvme_rdma_dev_get(ctrl->device);
721 	if (!ret) {
722 		ret = -EINVAL;
723 		goto out_free_tagset;
724 	}
725 
726 	return set;
727 
728 out_free_tagset:
729 	blk_mq_free_tag_set(set);
730 out:
731 	return ERR_PTR(ret);
732 }
733 
734 static void nvme_rdma_destroy_admin_queue(struct nvme_rdma_ctrl *ctrl,
735 		bool remove)
736 {
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);
741 	}
742 	nvme_rdma_free_queue(&ctrl->queues[0]);
743 }
744 
745 static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
746 		bool new)
747 {
748 	int error;
749 
750 	error = nvme_rdma_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
751 	if (error)
752 		return error;
753 
754 	ctrl->device = ctrl->queues[0].device;
755 
756 	ctrl->max_fr_pages = nvme_rdma_get_max_fr_pages(ctrl->device->dev);
757 
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;
763 		}
764 
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;
769 		}
770 	}
771 
772 	error = nvme_rdma_start_queue(ctrl, 0);
773 	if (error)
774 		goto out_cleanup_queue;
775 
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_stop_queue;
782 	}
783 
784 	ctrl->ctrl.sqsize =
785 		min_t(int, NVME_CAP_MQES(ctrl->ctrl.cap), ctrl->ctrl.sqsize);
786 
787 	error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
788 	if (error)
789 		goto out_stop_queue;
790 
791 	ctrl->ctrl.max_hw_sectors =
792 		(ctrl->max_fr_pages - 1) << (ilog2(SZ_4K) - 9);
793 
794 	error = nvme_init_identify(&ctrl->ctrl);
795 	if (error)
796 		goto out_stop_queue;
797 
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_stop_queue;
803 
804 	return 0;
805 
806 out_stop_queue:
807 	nvme_rdma_stop_queue(&ctrl->queues[0]);
808 out_cleanup_queue:
809 	if (new)
810 		blk_cleanup_queue(ctrl->ctrl.admin_q);
811 out_free_tagset:
812 	if (new)
813 		nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.admin_tagset);
814 out_free_queue:
815 	nvme_rdma_free_queue(&ctrl->queues[0]);
816 	return error;
817 }
818 
819 static void nvme_rdma_destroy_io_queues(struct nvme_rdma_ctrl *ctrl,
820 		bool remove)
821 {
822 	nvme_rdma_stop_io_queues(ctrl);
823 	if (remove) {
824 		blk_cleanup_queue(ctrl->ctrl.connect_q);
825 		nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
826 	}
827 	nvme_rdma_free_io_queues(ctrl);
828 }
829 
830 static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
831 {
832 	int ret;
833 
834 	ret = nvme_rdma_alloc_io_queues(ctrl);
835 	if (ret)
836 		return ret;
837 
838 	if (new) {
839 		ctrl->ctrl.tagset = nvme_rdma_alloc_tagset(&ctrl->ctrl, false);
840 		if (IS_ERR(ctrl->ctrl.tagset)) {
841 			ret = PTR_ERR(ctrl->ctrl.tagset);
842 			goto out_free_io_queues;
843 		}
844 
845 		ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
846 		if (IS_ERR(ctrl->ctrl.connect_q)) {
847 			ret = PTR_ERR(ctrl->ctrl.connect_q);
848 			goto out_free_tag_set;
849 		}
850 	} else {
851 		blk_mq_update_nr_hw_queues(&ctrl->tag_set,
852 			ctrl->ctrl.queue_count - 1);
853 	}
854 
855 	ret = nvme_rdma_start_io_queues(ctrl);
856 	if (ret)
857 		goto out_cleanup_connect_q;
858 
859 	return 0;
860 
861 out_cleanup_connect_q:
862 	if (new)
863 		blk_cleanup_queue(ctrl->ctrl.connect_q);
864 out_free_tag_set:
865 	if (new)
866 		nvme_rdma_free_tagset(&ctrl->ctrl, ctrl->ctrl.tagset);
867 out_free_io_queues:
868 	nvme_rdma_free_io_queues(ctrl);
869 	return ret;
870 }
871 
872 static void nvme_rdma_stop_ctrl(struct nvme_ctrl *nctrl)
873 {
874 	struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
875 
876 	cancel_work_sync(&ctrl->err_work);
877 	cancel_delayed_work_sync(&ctrl->reconnect_work);
878 }
879 
880 static void nvme_rdma_free_ctrl(struct nvme_ctrl *nctrl)
881 {
882 	struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
883 
884 	if (list_empty(&ctrl->list))
885 		goto free_ctrl;
886 
887 	mutex_lock(&nvme_rdma_ctrl_mutex);
888 	list_del(&ctrl->list);
889 	mutex_unlock(&nvme_rdma_ctrl_mutex);
890 
891 	kfree(ctrl->queues);
892 	nvmf_free_options(nctrl->opts);
893 free_ctrl:
894 	kfree(ctrl);
895 }
896 
897 static void nvme_rdma_reconnect_or_remove(struct nvme_rdma_ctrl *ctrl)
898 {
899 	/* If we are resetting/deleting then do nothing */
900 	if (ctrl->ctrl.state != NVME_CTRL_CONNECTING) {
901 		WARN_ON_ONCE(ctrl->ctrl.state == NVME_CTRL_NEW ||
902 			ctrl->ctrl.state == NVME_CTRL_LIVE);
903 		return;
904 	}
905 
906 	if (nvmf_should_reconnect(&ctrl->ctrl)) {
907 		dev_info(ctrl->ctrl.device, "Reconnecting in %d seconds...\n",
908 			ctrl->ctrl.opts->reconnect_delay);
909 		queue_delayed_work(nvme_wq, &ctrl->reconnect_work,
910 				ctrl->ctrl.opts->reconnect_delay * HZ);
911 	} else {
912 		nvme_delete_ctrl(&ctrl->ctrl);
913 	}
914 }
915 
916 static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
917 {
918 	struct nvme_rdma_ctrl *ctrl = container_of(to_delayed_work(work),
919 			struct nvme_rdma_ctrl, reconnect_work);
920 	bool changed;
921 	int ret;
922 
923 	++ctrl->ctrl.nr_reconnects;
924 
925 	ret = nvme_rdma_configure_admin_queue(ctrl, false);
926 	if (ret)
927 		goto requeue;
928 
929 	if (ctrl->ctrl.queue_count > 1) {
930 		ret = nvme_rdma_configure_io_queues(ctrl, false);
931 		if (ret)
932 			goto destroy_admin;
933 	}
934 
935 	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
936 	if (!changed) {
937 		/* state change failure is ok if we're in DELETING state */
938 		WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
939 		return;
940 	}
941 
942 	nvme_start_ctrl(&ctrl->ctrl);
943 
944 	dev_info(ctrl->ctrl.device, "Successfully reconnected (%d attempts)\n",
945 			ctrl->ctrl.nr_reconnects);
946 
947 	ctrl->ctrl.nr_reconnects = 0;
948 
949 	return;
950 
951 destroy_admin:
952 	nvme_rdma_destroy_admin_queue(ctrl, false);
953 requeue:
954 	dev_info(ctrl->ctrl.device, "Failed reconnect attempt %d\n",
955 			ctrl->ctrl.nr_reconnects);
956 	nvme_rdma_reconnect_or_remove(ctrl);
957 }
958 
959 static void nvme_rdma_error_recovery_work(struct work_struct *work)
960 {
961 	struct nvme_rdma_ctrl *ctrl = container_of(work,
962 			struct nvme_rdma_ctrl, err_work);
963 
964 	nvme_stop_keep_alive(&ctrl->ctrl);
965 
966 	if (ctrl->ctrl.queue_count > 1) {
967 		nvme_stop_queues(&ctrl->ctrl);
968 		blk_mq_tagset_busy_iter(&ctrl->tag_set,
969 					nvme_cancel_request, &ctrl->ctrl);
970 		nvme_rdma_destroy_io_queues(ctrl, false);
971 	}
972 
973 	blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
974 	blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
975 				nvme_cancel_request, &ctrl->ctrl);
976 	nvme_rdma_destroy_admin_queue(ctrl, false);
977 
978 	/*
979 	 * queues are not a live anymore, so restart the queues to fail fast
980 	 * new IO
981 	 */
982 	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
983 	nvme_start_queues(&ctrl->ctrl);
984 
985 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
986 		/* state change failure is ok if we're in DELETING state */
987 		WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
988 		return;
989 	}
990 
991 	nvme_rdma_reconnect_or_remove(ctrl);
992 }
993 
994 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
995 {
996 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
997 		return;
998 
999 	queue_work(nvme_wq, &ctrl->err_work);
1000 }
1001 
1002 static void nvme_rdma_wr_error(struct ib_cq *cq, struct ib_wc *wc,
1003 		const char *op)
1004 {
1005 	struct nvme_rdma_queue *queue = cq->cq_context;
1006 	struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1007 
1008 	if (ctrl->ctrl.state == NVME_CTRL_LIVE)
1009 		dev_info(ctrl->ctrl.device,
1010 			     "%s for CQE 0x%p failed with status %s (%d)\n",
1011 			     op, wc->wr_cqe,
1012 			     ib_wc_status_msg(wc->status), wc->status);
1013 	nvme_rdma_error_recovery(ctrl);
1014 }
1015 
1016 static void nvme_rdma_memreg_done(struct ib_cq *cq, struct ib_wc *wc)
1017 {
1018 	if (unlikely(wc->status != IB_WC_SUCCESS))
1019 		nvme_rdma_wr_error(cq, wc, "MEMREG");
1020 }
1021 
1022 static void nvme_rdma_inv_rkey_done(struct ib_cq *cq, struct ib_wc *wc)
1023 {
1024 	struct nvme_rdma_request *req =
1025 		container_of(wc->wr_cqe, struct nvme_rdma_request, reg_cqe);
1026 	struct request *rq = blk_mq_rq_from_pdu(req);
1027 
1028 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
1029 		nvme_rdma_wr_error(cq, wc, "LOCAL_INV");
1030 		return;
1031 	}
1032 
1033 	if (refcount_dec_and_test(&req->ref))
1034 		nvme_end_request(rq, req->status, req->result);
1035 
1036 }
1037 
1038 static int nvme_rdma_inv_rkey(struct nvme_rdma_queue *queue,
1039 		struct nvme_rdma_request *req)
1040 {
1041 	struct ib_send_wr *bad_wr;
1042 	struct ib_send_wr wr = {
1043 		.opcode		    = IB_WR_LOCAL_INV,
1044 		.next		    = NULL,
1045 		.num_sge	    = 0,
1046 		.send_flags	    = IB_SEND_SIGNALED,
1047 		.ex.invalidate_rkey = req->mr->rkey,
1048 	};
1049 
1050 	req->reg_cqe.done = nvme_rdma_inv_rkey_done;
1051 	wr.wr_cqe = &req->reg_cqe;
1052 
1053 	return ib_post_send(queue->qp, &wr, &bad_wr);
1054 }
1055 
1056 static void nvme_rdma_unmap_data(struct nvme_rdma_queue *queue,
1057 		struct request *rq)
1058 {
1059 	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1060 	struct nvme_rdma_device *dev = queue->device;
1061 	struct ib_device *ibdev = dev->dev;
1062 
1063 	if (!blk_rq_payload_bytes(rq))
1064 		return;
1065 
1066 	if (req->mr) {
1067 		ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1068 		req->mr = NULL;
1069 	}
1070 
1071 	ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1072 			req->nents, rq_data_dir(rq) ==
1073 				    WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1074 
1075 	nvme_cleanup_cmd(rq);
1076 	sg_free_table_chained(&req->sg_table, true);
1077 }
1078 
1079 static int nvme_rdma_set_sg_null(struct nvme_command *c)
1080 {
1081 	struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1082 
1083 	sg->addr = 0;
1084 	put_unaligned_le24(0, sg->length);
1085 	put_unaligned_le32(0, sg->key);
1086 	sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1087 	return 0;
1088 }
1089 
1090 static int nvme_rdma_map_sg_inline(struct nvme_rdma_queue *queue,
1091 		struct nvme_rdma_request *req, struct nvme_command *c)
1092 {
1093 	struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1094 
1095 	req->sge[1].addr = sg_dma_address(req->sg_table.sgl);
1096 	req->sge[1].length = sg_dma_len(req->sg_table.sgl);
1097 	req->sge[1].lkey = queue->device->pd->local_dma_lkey;
1098 
1099 	sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1100 	sg->length = cpu_to_le32(sg_dma_len(req->sg_table.sgl));
1101 	sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1102 
1103 	req->num_sge++;
1104 	return 0;
1105 }
1106 
1107 static int nvme_rdma_map_sg_single(struct nvme_rdma_queue *queue,
1108 		struct nvme_rdma_request *req, struct nvme_command *c)
1109 {
1110 	struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1111 
1112 	sg->addr = cpu_to_le64(sg_dma_address(req->sg_table.sgl));
1113 	put_unaligned_le24(sg_dma_len(req->sg_table.sgl), sg->length);
1114 	put_unaligned_le32(queue->device->pd->unsafe_global_rkey, sg->key);
1115 	sg->type = NVME_KEY_SGL_FMT_DATA_DESC << 4;
1116 	return 0;
1117 }
1118 
1119 static int nvme_rdma_map_sg_fr(struct nvme_rdma_queue *queue,
1120 		struct nvme_rdma_request *req, struct nvme_command *c,
1121 		int count)
1122 {
1123 	struct nvme_keyed_sgl_desc *sg = &c->common.dptr.ksgl;
1124 	int nr;
1125 
1126 	req->mr = ib_mr_pool_get(queue->qp, &queue->qp->rdma_mrs);
1127 	if (WARN_ON_ONCE(!req->mr))
1128 		return -EAGAIN;
1129 
1130 	/*
1131 	 * Align the MR to a 4K page size to match the ctrl page size and
1132 	 * the block virtual boundary.
1133 	 */
1134 	nr = ib_map_mr_sg(req->mr, req->sg_table.sgl, count, NULL, SZ_4K);
1135 	if (unlikely(nr < count)) {
1136 		ib_mr_pool_put(queue->qp, &queue->qp->rdma_mrs, req->mr);
1137 		req->mr = NULL;
1138 		if (nr < 0)
1139 			return nr;
1140 		return -EINVAL;
1141 	}
1142 
1143 	ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
1144 
1145 	req->reg_cqe.done = nvme_rdma_memreg_done;
1146 	memset(&req->reg_wr, 0, sizeof(req->reg_wr));
1147 	req->reg_wr.wr.opcode = IB_WR_REG_MR;
1148 	req->reg_wr.wr.wr_cqe = &req->reg_cqe;
1149 	req->reg_wr.wr.num_sge = 0;
1150 	req->reg_wr.mr = req->mr;
1151 	req->reg_wr.key = req->mr->rkey;
1152 	req->reg_wr.access = IB_ACCESS_LOCAL_WRITE |
1153 			     IB_ACCESS_REMOTE_READ |
1154 			     IB_ACCESS_REMOTE_WRITE;
1155 
1156 	sg->addr = cpu_to_le64(req->mr->iova);
1157 	put_unaligned_le24(req->mr->length, sg->length);
1158 	put_unaligned_le32(req->mr->rkey, sg->key);
1159 	sg->type = (NVME_KEY_SGL_FMT_DATA_DESC << 4) |
1160 			NVME_SGL_FMT_INVALIDATE;
1161 
1162 	return 0;
1163 }
1164 
1165 static int nvme_rdma_map_data(struct nvme_rdma_queue *queue,
1166 		struct request *rq, struct nvme_command *c)
1167 {
1168 	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1169 	struct nvme_rdma_device *dev = queue->device;
1170 	struct ib_device *ibdev = dev->dev;
1171 	int count, ret;
1172 
1173 	req->num_sge = 1;
1174 	refcount_set(&req->ref, 2); /* send and recv completions */
1175 
1176 	c->common.flags |= NVME_CMD_SGL_METABUF;
1177 
1178 	if (!blk_rq_payload_bytes(rq))
1179 		return nvme_rdma_set_sg_null(c);
1180 
1181 	req->sg_table.sgl = req->first_sgl;
1182 	ret = sg_alloc_table_chained(&req->sg_table,
1183 			blk_rq_nr_phys_segments(rq), req->sg_table.sgl);
1184 	if (ret)
1185 		return -ENOMEM;
1186 
1187 	req->nents = blk_rq_map_sg(rq->q, rq, req->sg_table.sgl);
1188 
1189 	count = ib_dma_map_sg(ibdev, req->sg_table.sgl, req->nents,
1190 		    rq_data_dir(rq) == WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1191 	if (unlikely(count <= 0)) {
1192 		ret = -EIO;
1193 		goto out_free_table;
1194 	}
1195 
1196 	if (count == 1) {
1197 		if (rq_data_dir(rq) == WRITE && nvme_rdma_queue_idx(queue) &&
1198 		    blk_rq_payload_bytes(rq) <=
1199 				nvme_rdma_inline_data_size(queue)) {
1200 			ret = nvme_rdma_map_sg_inline(queue, req, c);
1201 			goto out;
1202 		}
1203 
1204 		if (dev->pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
1205 			ret = nvme_rdma_map_sg_single(queue, req, c);
1206 			goto out;
1207 		}
1208 	}
1209 
1210 	ret = nvme_rdma_map_sg_fr(queue, req, c, count);
1211 out:
1212 	if (unlikely(ret))
1213 		goto out_unmap_sg;
1214 
1215 	return 0;
1216 
1217 out_unmap_sg:
1218 	ib_dma_unmap_sg(ibdev, req->sg_table.sgl,
1219 			req->nents, rq_data_dir(rq) ==
1220 			WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1221 out_free_table:
1222 	sg_free_table_chained(&req->sg_table, true);
1223 	return ret;
1224 }
1225 
1226 static void nvme_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
1227 {
1228 	struct nvme_rdma_qe *qe =
1229 		container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1230 	struct nvme_rdma_request *req =
1231 		container_of(qe, struct nvme_rdma_request, sqe);
1232 	struct request *rq = blk_mq_rq_from_pdu(req);
1233 
1234 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
1235 		nvme_rdma_wr_error(cq, wc, "SEND");
1236 		return;
1237 	}
1238 
1239 	if (refcount_dec_and_test(&req->ref))
1240 		nvme_end_request(rq, req->status, req->result);
1241 }
1242 
1243 static int nvme_rdma_post_send(struct nvme_rdma_queue *queue,
1244 		struct nvme_rdma_qe *qe, struct ib_sge *sge, u32 num_sge,
1245 		struct ib_send_wr *first)
1246 {
1247 	struct ib_send_wr wr, *bad_wr;
1248 	int ret;
1249 
1250 	sge->addr   = qe->dma;
1251 	sge->length = sizeof(struct nvme_command),
1252 	sge->lkey   = queue->device->pd->local_dma_lkey;
1253 
1254 	wr.next       = NULL;
1255 	wr.wr_cqe     = &qe->cqe;
1256 	wr.sg_list    = sge;
1257 	wr.num_sge    = num_sge;
1258 	wr.opcode     = IB_WR_SEND;
1259 	wr.send_flags = IB_SEND_SIGNALED;
1260 
1261 	if (first)
1262 		first->next = &wr;
1263 	else
1264 		first = &wr;
1265 
1266 	ret = ib_post_send(queue->qp, first, &bad_wr);
1267 	if (unlikely(ret)) {
1268 		dev_err(queue->ctrl->ctrl.device,
1269 			     "%s failed with error code %d\n", __func__, ret);
1270 	}
1271 	return ret;
1272 }
1273 
1274 static int nvme_rdma_post_recv(struct nvme_rdma_queue *queue,
1275 		struct nvme_rdma_qe *qe)
1276 {
1277 	struct ib_recv_wr wr, *bad_wr;
1278 	struct ib_sge list;
1279 	int ret;
1280 
1281 	list.addr   = qe->dma;
1282 	list.length = sizeof(struct nvme_completion);
1283 	list.lkey   = queue->device->pd->local_dma_lkey;
1284 
1285 	qe->cqe.done = nvme_rdma_recv_done;
1286 
1287 	wr.next     = NULL;
1288 	wr.wr_cqe   = &qe->cqe;
1289 	wr.sg_list  = &list;
1290 	wr.num_sge  = 1;
1291 
1292 	ret = ib_post_recv(queue->qp, &wr, &bad_wr);
1293 	if (unlikely(ret)) {
1294 		dev_err(queue->ctrl->ctrl.device,
1295 			"%s failed with error code %d\n", __func__, ret);
1296 	}
1297 	return ret;
1298 }
1299 
1300 static struct blk_mq_tags *nvme_rdma_tagset(struct nvme_rdma_queue *queue)
1301 {
1302 	u32 queue_idx = nvme_rdma_queue_idx(queue);
1303 
1304 	if (queue_idx == 0)
1305 		return queue->ctrl->admin_tag_set.tags[queue_idx];
1306 	return queue->ctrl->tag_set.tags[queue_idx - 1];
1307 }
1308 
1309 static void nvme_rdma_async_done(struct ib_cq *cq, struct ib_wc *wc)
1310 {
1311 	if (unlikely(wc->status != IB_WC_SUCCESS))
1312 		nvme_rdma_wr_error(cq, wc, "ASYNC");
1313 }
1314 
1315 static void nvme_rdma_submit_async_event(struct nvme_ctrl *arg)
1316 {
1317 	struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(arg);
1318 	struct nvme_rdma_queue *queue = &ctrl->queues[0];
1319 	struct ib_device *dev = queue->device->dev;
1320 	struct nvme_rdma_qe *sqe = &ctrl->async_event_sqe;
1321 	struct nvme_command *cmd = sqe->data;
1322 	struct ib_sge sge;
1323 	int ret;
1324 
1325 	ib_dma_sync_single_for_cpu(dev, sqe->dma, sizeof(*cmd), DMA_TO_DEVICE);
1326 
1327 	memset(cmd, 0, sizeof(*cmd));
1328 	cmd->common.opcode = nvme_admin_async_event;
1329 	cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
1330 	cmd->common.flags |= NVME_CMD_SGL_METABUF;
1331 	nvme_rdma_set_sg_null(cmd);
1332 
1333 	sqe->cqe.done = nvme_rdma_async_done;
1334 
1335 	ib_dma_sync_single_for_device(dev, sqe->dma, sizeof(*cmd),
1336 			DMA_TO_DEVICE);
1337 
1338 	ret = nvme_rdma_post_send(queue, sqe, &sge, 1, NULL);
1339 	WARN_ON_ONCE(ret);
1340 }
1341 
1342 static int nvme_rdma_process_nvme_rsp(struct nvme_rdma_queue *queue,
1343 		struct nvme_completion *cqe, struct ib_wc *wc, int tag)
1344 {
1345 	struct request *rq;
1346 	struct nvme_rdma_request *req;
1347 	int ret = 0;
1348 
1349 	rq = blk_mq_tag_to_rq(nvme_rdma_tagset(queue), cqe->command_id);
1350 	if (!rq) {
1351 		dev_err(queue->ctrl->ctrl.device,
1352 			"tag 0x%x on QP %#x not found\n",
1353 			cqe->command_id, queue->qp->qp_num);
1354 		nvme_rdma_error_recovery(queue->ctrl);
1355 		return ret;
1356 	}
1357 	req = blk_mq_rq_to_pdu(rq);
1358 
1359 	req->status = cqe->status;
1360 	req->result = cqe->result;
1361 
1362 	if (wc->wc_flags & IB_WC_WITH_INVALIDATE) {
1363 		if (unlikely(wc->ex.invalidate_rkey != req->mr->rkey)) {
1364 			dev_err(queue->ctrl->ctrl.device,
1365 				"Bogus remote invalidation for rkey %#x\n",
1366 				req->mr->rkey);
1367 			nvme_rdma_error_recovery(queue->ctrl);
1368 		}
1369 	} else if (req->mr) {
1370 		ret = nvme_rdma_inv_rkey(queue, req);
1371 		if (unlikely(ret < 0)) {
1372 			dev_err(queue->ctrl->ctrl.device,
1373 				"Queueing INV WR for rkey %#x failed (%d)\n",
1374 				req->mr->rkey, ret);
1375 			nvme_rdma_error_recovery(queue->ctrl);
1376 		}
1377 		/* the local invalidation completion will end the request */
1378 		return 0;
1379 	}
1380 
1381 	if (refcount_dec_and_test(&req->ref)) {
1382 		if (rq->tag == tag)
1383 			ret = 1;
1384 		nvme_end_request(rq, req->status, req->result);
1385 	}
1386 
1387 	return ret;
1388 }
1389 
1390 static int __nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc, int tag)
1391 {
1392 	struct nvme_rdma_qe *qe =
1393 		container_of(wc->wr_cqe, struct nvme_rdma_qe, cqe);
1394 	struct nvme_rdma_queue *queue = cq->cq_context;
1395 	struct ib_device *ibdev = queue->device->dev;
1396 	struct nvme_completion *cqe = qe->data;
1397 	const size_t len = sizeof(struct nvme_completion);
1398 	int ret = 0;
1399 
1400 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
1401 		nvme_rdma_wr_error(cq, wc, "RECV");
1402 		return 0;
1403 	}
1404 
1405 	ib_dma_sync_single_for_cpu(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1406 	/*
1407 	 * AEN requests are special as they don't time out and can
1408 	 * survive any kind of queue freeze and often don't respond to
1409 	 * aborts.  We don't even bother to allocate a struct request
1410 	 * for them but rather special case them here.
1411 	 */
1412 	if (unlikely(nvme_rdma_queue_idx(queue) == 0 &&
1413 			cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH))
1414 		nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
1415 				&cqe->result);
1416 	else
1417 		ret = nvme_rdma_process_nvme_rsp(queue, cqe, wc, tag);
1418 	ib_dma_sync_single_for_device(ibdev, qe->dma, len, DMA_FROM_DEVICE);
1419 
1420 	nvme_rdma_post_recv(queue, qe);
1421 	return ret;
1422 }
1423 
1424 static void nvme_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1425 {
1426 	__nvme_rdma_recv_done(cq, wc, -1);
1427 }
1428 
1429 static int nvme_rdma_conn_established(struct nvme_rdma_queue *queue)
1430 {
1431 	int ret, i;
1432 
1433 	for (i = 0; i < queue->queue_size; i++) {
1434 		ret = nvme_rdma_post_recv(queue, &queue->rsp_ring[i]);
1435 		if (ret)
1436 			goto out_destroy_queue_ib;
1437 	}
1438 
1439 	return 0;
1440 
1441 out_destroy_queue_ib:
1442 	nvme_rdma_destroy_queue_ib(queue);
1443 	return ret;
1444 }
1445 
1446 static int nvme_rdma_conn_rejected(struct nvme_rdma_queue *queue,
1447 		struct rdma_cm_event *ev)
1448 {
1449 	struct rdma_cm_id *cm_id = queue->cm_id;
1450 	int status = ev->status;
1451 	const char *rej_msg;
1452 	const struct nvme_rdma_cm_rej *rej_data;
1453 	u8 rej_data_len;
1454 
1455 	rej_msg = rdma_reject_msg(cm_id, status);
1456 	rej_data = rdma_consumer_reject_data(cm_id, ev, &rej_data_len);
1457 
1458 	if (rej_data && rej_data_len >= sizeof(u16)) {
1459 		u16 sts = le16_to_cpu(rej_data->sts);
1460 
1461 		dev_err(queue->ctrl->ctrl.device,
1462 		      "Connect rejected: status %d (%s) nvme status %d (%s).\n",
1463 		      status, rej_msg, sts, nvme_rdma_cm_msg(sts));
1464 	} else {
1465 		dev_err(queue->ctrl->ctrl.device,
1466 			"Connect rejected: status %d (%s).\n", status, rej_msg);
1467 	}
1468 
1469 	return -ECONNRESET;
1470 }
1471 
1472 static int nvme_rdma_addr_resolved(struct nvme_rdma_queue *queue)
1473 {
1474 	int ret;
1475 
1476 	ret = nvme_rdma_create_queue_ib(queue);
1477 	if (ret)
1478 		return ret;
1479 
1480 	ret = rdma_resolve_route(queue->cm_id, NVME_RDMA_CONNECT_TIMEOUT_MS);
1481 	if (ret) {
1482 		dev_err(queue->ctrl->ctrl.device,
1483 			"rdma_resolve_route failed (%d).\n",
1484 			queue->cm_error);
1485 		goto out_destroy_queue;
1486 	}
1487 
1488 	return 0;
1489 
1490 out_destroy_queue:
1491 	nvme_rdma_destroy_queue_ib(queue);
1492 	return ret;
1493 }
1494 
1495 static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
1496 {
1497 	struct nvme_rdma_ctrl *ctrl = queue->ctrl;
1498 	struct rdma_conn_param param = { };
1499 	struct nvme_rdma_cm_req priv = { };
1500 	int ret;
1501 
1502 	param.qp_num = queue->qp->qp_num;
1503 	param.flow_control = 1;
1504 
1505 	param.responder_resources = queue->device->dev->attrs.max_qp_rd_atom;
1506 	/* maximum retry count */
1507 	param.retry_count = 7;
1508 	param.rnr_retry_count = 7;
1509 	param.private_data = &priv;
1510 	param.private_data_len = sizeof(priv);
1511 
1512 	priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1513 	priv.qid = cpu_to_le16(nvme_rdma_queue_idx(queue));
1514 	/*
1515 	 * set the admin queue depth to the minimum size
1516 	 * specified by the Fabrics standard.
1517 	 */
1518 	if (priv.qid == 0) {
1519 		priv.hrqsize = cpu_to_le16(NVME_AQ_DEPTH);
1520 		priv.hsqsize = cpu_to_le16(NVME_AQ_DEPTH - 1);
1521 	} else {
1522 		/*
1523 		 * current interpretation of the fabrics spec
1524 		 * is at minimum you make hrqsize sqsize+1, or a
1525 		 * 1's based representation of sqsize.
1526 		 */
1527 		priv.hrqsize = cpu_to_le16(queue->queue_size);
1528 		priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
1529 	}
1530 
1531 	ret = rdma_connect(queue->cm_id, &param);
1532 	if (ret) {
1533 		dev_err(ctrl->ctrl.device,
1534 			"rdma_connect failed (%d).\n", ret);
1535 		goto out_destroy_queue_ib;
1536 	}
1537 
1538 	return 0;
1539 
1540 out_destroy_queue_ib:
1541 	nvme_rdma_destroy_queue_ib(queue);
1542 	return ret;
1543 }
1544 
1545 static int nvme_rdma_cm_handler(struct rdma_cm_id *cm_id,
1546 		struct rdma_cm_event *ev)
1547 {
1548 	struct nvme_rdma_queue *queue = cm_id->context;
1549 	int cm_error = 0;
1550 
1551 	dev_dbg(queue->ctrl->ctrl.device, "%s (%d): status %d id %p\n",
1552 		rdma_event_msg(ev->event), ev->event,
1553 		ev->status, cm_id);
1554 
1555 	switch (ev->event) {
1556 	case RDMA_CM_EVENT_ADDR_RESOLVED:
1557 		cm_error = nvme_rdma_addr_resolved(queue);
1558 		break;
1559 	case RDMA_CM_EVENT_ROUTE_RESOLVED:
1560 		cm_error = nvme_rdma_route_resolved(queue);
1561 		break;
1562 	case RDMA_CM_EVENT_ESTABLISHED:
1563 		queue->cm_error = nvme_rdma_conn_established(queue);
1564 		/* complete cm_done regardless of success/failure */
1565 		complete(&queue->cm_done);
1566 		return 0;
1567 	case RDMA_CM_EVENT_REJECTED:
1568 		nvme_rdma_destroy_queue_ib(queue);
1569 		cm_error = nvme_rdma_conn_rejected(queue, ev);
1570 		break;
1571 	case RDMA_CM_EVENT_ROUTE_ERROR:
1572 	case RDMA_CM_EVENT_CONNECT_ERROR:
1573 	case RDMA_CM_EVENT_UNREACHABLE:
1574 		nvme_rdma_destroy_queue_ib(queue);
1575 	case RDMA_CM_EVENT_ADDR_ERROR:
1576 		dev_dbg(queue->ctrl->ctrl.device,
1577 			"CM error event %d\n", ev->event);
1578 		cm_error = -ECONNRESET;
1579 		break;
1580 	case RDMA_CM_EVENT_DISCONNECTED:
1581 	case RDMA_CM_EVENT_ADDR_CHANGE:
1582 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1583 		dev_dbg(queue->ctrl->ctrl.device,
1584 			"disconnect received - connection closed\n");
1585 		nvme_rdma_error_recovery(queue->ctrl);
1586 		break;
1587 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
1588 		/* device removal is handled via the ib_client API */
1589 		break;
1590 	default:
1591 		dev_err(queue->ctrl->ctrl.device,
1592 			"Unexpected RDMA CM event (%d)\n", ev->event);
1593 		nvme_rdma_error_recovery(queue->ctrl);
1594 		break;
1595 	}
1596 
1597 	if (cm_error) {
1598 		queue->cm_error = cm_error;
1599 		complete(&queue->cm_done);
1600 	}
1601 
1602 	return 0;
1603 }
1604 
1605 static enum blk_eh_timer_return
1606 nvme_rdma_timeout(struct request *rq, bool reserved)
1607 {
1608 	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1609 
1610 	dev_warn(req->queue->ctrl->ctrl.device,
1611 		 "I/O %d QID %d timeout, reset controller\n",
1612 		 rq->tag, nvme_rdma_queue_idx(req->queue));
1613 
1614 	/* queue error recovery */
1615 	nvme_rdma_error_recovery(req->queue->ctrl);
1616 
1617 	/* fail with DNR on cmd timeout */
1618 	nvme_req(rq)->status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
1619 
1620 	return BLK_EH_DONE;
1621 }
1622 
1623 static blk_status_t nvme_rdma_queue_rq(struct blk_mq_hw_ctx *hctx,
1624 		const struct blk_mq_queue_data *bd)
1625 {
1626 	struct nvme_ns *ns = hctx->queue->queuedata;
1627 	struct nvme_rdma_queue *queue = hctx->driver_data;
1628 	struct request *rq = bd->rq;
1629 	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1630 	struct nvme_rdma_qe *sqe = &req->sqe;
1631 	struct nvme_command *c = sqe->data;
1632 	struct ib_device *dev;
1633 	bool queue_ready = test_bit(NVME_RDMA_Q_LIVE, &queue->flags);
1634 	blk_status_t ret;
1635 	int err;
1636 
1637 	WARN_ON_ONCE(rq->tag < 0);
1638 
1639 	if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
1640 		return nvmf_fail_nonready_command(rq);
1641 
1642 	dev = queue->device->dev;
1643 	ib_dma_sync_single_for_cpu(dev, sqe->dma,
1644 			sizeof(struct nvme_command), DMA_TO_DEVICE);
1645 
1646 	ret = nvme_setup_cmd(ns, rq, c);
1647 	if (ret)
1648 		return ret;
1649 
1650 	blk_mq_start_request(rq);
1651 
1652 	err = nvme_rdma_map_data(queue, rq, c);
1653 	if (unlikely(err < 0)) {
1654 		dev_err(queue->ctrl->ctrl.device,
1655 			     "Failed to map data (%d)\n", err);
1656 		nvme_cleanup_cmd(rq);
1657 		goto err;
1658 	}
1659 
1660 	sqe->cqe.done = nvme_rdma_send_done;
1661 
1662 	ib_dma_sync_single_for_device(dev, sqe->dma,
1663 			sizeof(struct nvme_command), DMA_TO_DEVICE);
1664 
1665 	err = nvme_rdma_post_send(queue, sqe, req->sge, req->num_sge,
1666 			req->mr ? &req->reg_wr.wr : NULL);
1667 	if (unlikely(err)) {
1668 		nvme_rdma_unmap_data(queue, rq);
1669 		goto err;
1670 	}
1671 
1672 	return BLK_STS_OK;
1673 err:
1674 	if (err == -ENOMEM || err == -EAGAIN)
1675 		return BLK_STS_RESOURCE;
1676 	return BLK_STS_IOERR;
1677 }
1678 
1679 static int nvme_rdma_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
1680 {
1681 	struct nvme_rdma_queue *queue = hctx->driver_data;
1682 	struct ib_cq *cq = queue->ib_cq;
1683 	struct ib_wc wc;
1684 	int found = 0;
1685 
1686 	while (ib_poll_cq(cq, 1, &wc) > 0) {
1687 		struct ib_cqe *cqe = wc.wr_cqe;
1688 
1689 		if (cqe) {
1690 			if (cqe->done == nvme_rdma_recv_done)
1691 				found |= __nvme_rdma_recv_done(cq, &wc, tag);
1692 			else
1693 				cqe->done(cq, &wc);
1694 		}
1695 	}
1696 
1697 	return found;
1698 }
1699 
1700 static void nvme_rdma_complete_rq(struct request *rq)
1701 {
1702 	struct nvme_rdma_request *req = blk_mq_rq_to_pdu(rq);
1703 
1704 	nvme_rdma_unmap_data(req->queue, rq);
1705 	nvme_complete_rq(rq);
1706 }
1707 
1708 static int nvme_rdma_map_queues(struct blk_mq_tag_set *set)
1709 {
1710 	struct nvme_rdma_ctrl *ctrl = set->driver_data;
1711 
1712 	return blk_mq_rdma_map_queues(set, ctrl->device->dev, 0);
1713 }
1714 
1715 static const struct blk_mq_ops nvme_rdma_mq_ops = {
1716 	.queue_rq	= nvme_rdma_queue_rq,
1717 	.complete	= nvme_rdma_complete_rq,
1718 	.init_request	= nvme_rdma_init_request,
1719 	.exit_request	= nvme_rdma_exit_request,
1720 	.init_hctx	= nvme_rdma_init_hctx,
1721 	.poll		= nvme_rdma_poll,
1722 	.timeout	= nvme_rdma_timeout,
1723 	.map_queues	= nvme_rdma_map_queues,
1724 };
1725 
1726 static const struct blk_mq_ops nvme_rdma_admin_mq_ops = {
1727 	.queue_rq	= nvme_rdma_queue_rq,
1728 	.complete	= nvme_rdma_complete_rq,
1729 	.init_request	= nvme_rdma_init_request,
1730 	.exit_request	= nvme_rdma_exit_request,
1731 	.init_hctx	= nvme_rdma_init_admin_hctx,
1732 	.timeout	= nvme_rdma_timeout,
1733 };
1734 
1735 static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
1736 {
1737 	if (ctrl->ctrl.queue_count > 1) {
1738 		nvme_stop_queues(&ctrl->ctrl);
1739 		blk_mq_tagset_busy_iter(&ctrl->tag_set,
1740 					nvme_cancel_request, &ctrl->ctrl);
1741 		nvme_rdma_destroy_io_queues(ctrl, shutdown);
1742 	}
1743 
1744 	if (shutdown)
1745 		nvme_shutdown_ctrl(&ctrl->ctrl);
1746 	else
1747 		nvme_disable_ctrl(&ctrl->ctrl, ctrl->ctrl.cap);
1748 
1749 	blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
1750 	blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
1751 				nvme_cancel_request, &ctrl->ctrl);
1752 	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
1753 	nvme_rdma_destroy_admin_queue(ctrl, shutdown);
1754 }
1755 
1756 static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
1757 {
1758 	nvme_rdma_shutdown_ctrl(to_rdma_ctrl(ctrl), true);
1759 }
1760 
1761 static void nvme_rdma_reset_ctrl_work(struct work_struct *work)
1762 {
1763 	struct nvme_rdma_ctrl *ctrl =
1764 		container_of(work, struct nvme_rdma_ctrl, ctrl.reset_work);
1765 	int ret;
1766 	bool changed;
1767 
1768 	nvme_stop_ctrl(&ctrl->ctrl);
1769 	nvme_rdma_shutdown_ctrl(ctrl, false);
1770 
1771 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
1772 		/* state change failure should never happen */
1773 		WARN_ON_ONCE(1);
1774 		return;
1775 	}
1776 
1777 	ret = nvme_rdma_configure_admin_queue(ctrl, false);
1778 	if (ret)
1779 		goto out_fail;
1780 
1781 	if (ctrl->ctrl.queue_count > 1) {
1782 		ret = nvme_rdma_configure_io_queues(ctrl, false);
1783 		if (ret)
1784 			goto out_fail;
1785 	}
1786 
1787 	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
1788 	if (!changed) {
1789 		/* state change failure is ok if we're in DELETING state */
1790 		WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
1791 		return;
1792 	}
1793 
1794 	nvme_start_ctrl(&ctrl->ctrl);
1795 
1796 	return;
1797 
1798 out_fail:
1799 	++ctrl->ctrl.nr_reconnects;
1800 	nvme_rdma_reconnect_or_remove(ctrl);
1801 }
1802 
1803 static const struct nvme_ctrl_ops nvme_rdma_ctrl_ops = {
1804 	.name			= "rdma",
1805 	.module			= THIS_MODULE,
1806 	.flags			= NVME_F_FABRICS,
1807 	.reg_read32		= nvmf_reg_read32,
1808 	.reg_read64		= nvmf_reg_read64,
1809 	.reg_write32		= nvmf_reg_write32,
1810 	.free_ctrl		= nvme_rdma_free_ctrl,
1811 	.submit_async_event	= nvme_rdma_submit_async_event,
1812 	.delete_ctrl		= nvme_rdma_delete_ctrl,
1813 	.get_address		= nvmf_get_address,
1814 	.stop_ctrl		= nvme_rdma_stop_ctrl,
1815 };
1816 
1817 static inline bool
1818 __nvme_rdma_options_match(struct nvme_rdma_ctrl *ctrl,
1819 	struct nvmf_ctrl_options *opts)
1820 {
1821 	char *stdport = __stringify(NVME_RDMA_IP_PORT);
1822 
1823 
1824 	if (!nvmf_ctlr_matches_baseopts(&ctrl->ctrl, opts) ||
1825 	    strcmp(opts->traddr, ctrl->ctrl.opts->traddr))
1826 		return false;
1827 
1828 	if (opts->mask & NVMF_OPT_TRSVCID &&
1829 	    ctrl->ctrl.opts->mask & NVMF_OPT_TRSVCID) {
1830 		if (strcmp(opts->trsvcid, ctrl->ctrl.opts->trsvcid))
1831 			return false;
1832 	} else if (opts->mask & NVMF_OPT_TRSVCID) {
1833 		if (strcmp(opts->trsvcid, stdport))
1834 			return false;
1835 	} else if (ctrl->ctrl.opts->mask & NVMF_OPT_TRSVCID) {
1836 		if (strcmp(stdport, ctrl->ctrl.opts->trsvcid))
1837 			return false;
1838 	}
1839 	/* else, it's a match as both have stdport. Fall to next checks */
1840 
1841 	/*
1842 	 * checking the local address is rough. In most cases, one
1843 	 * is not specified and the host port is selected by the stack.
1844 	 *
1845 	 * Assume no match if:
1846 	 *  local address is specified and address is not the same
1847 	 *  local address is not specified but remote is, or vice versa
1848 	 *    (admin using specific host_traddr when it matters).
1849 	 */
1850 	if (opts->mask & NVMF_OPT_HOST_TRADDR &&
1851 	    ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR) {
1852 		if (strcmp(opts->host_traddr, ctrl->ctrl.opts->host_traddr))
1853 			return false;
1854 	} else if (opts->mask & NVMF_OPT_HOST_TRADDR ||
1855 		   ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR)
1856 		return false;
1857 	/*
1858 	 * if neither controller had an host port specified, assume it's
1859 	 * a match as everything else matched.
1860 	 */
1861 
1862 	return true;
1863 }
1864 
1865 /*
1866  * Fails a connection request if it matches an existing controller
1867  * (association) with the same tuple:
1868  * <Host NQN, Host ID, local address, remote address, remote port, SUBSYS NQN>
1869  *
1870  * if local address is not specified in the request, it will match an
1871  * existing controller with all the other parameters the same and no
1872  * local port address specified as well.
1873  *
1874  * The ports don't need to be compared as they are intrinsically
1875  * already matched by the port pointers supplied.
1876  */
1877 static bool
1878 nvme_rdma_existing_controller(struct nvmf_ctrl_options *opts)
1879 {
1880 	struct nvme_rdma_ctrl *ctrl;
1881 	bool found = false;
1882 
1883 	mutex_lock(&nvme_rdma_ctrl_mutex);
1884 	list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
1885 		found = __nvme_rdma_options_match(ctrl, opts);
1886 		if (found)
1887 			break;
1888 	}
1889 	mutex_unlock(&nvme_rdma_ctrl_mutex);
1890 
1891 	return found;
1892 }
1893 
1894 static struct nvme_ctrl *nvme_rdma_create_ctrl(struct device *dev,
1895 		struct nvmf_ctrl_options *opts)
1896 {
1897 	struct nvme_rdma_ctrl *ctrl;
1898 	int ret;
1899 	bool changed;
1900 	char *port;
1901 
1902 	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1903 	if (!ctrl)
1904 		return ERR_PTR(-ENOMEM);
1905 	ctrl->ctrl.opts = opts;
1906 	INIT_LIST_HEAD(&ctrl->list);
1907 
1908 	if (opts->mask & NVMF_OPT_TRSVCID)
1909 		port = opts->trsvcid;
1910 	else
1911 		port = __stringify(NVME_RDMA_IP_PORT);
1912 
1913 	ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1914 			opts->traddr, port, &ctrl->addr);
1915 	if (ret) {
1916 		pr_err("malformed address passed: %s:%s\n", opts->traddr, port);
1917 		goto out_free_ctrl;
1918 	}
1919 
1920 	if (opts->mask & NVMF_OPT_HOST_TRADDR) {
1921 		ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
1922 			opts->host_traddr, NULL, &ctrl->src_addr);
1923 		if (ret) {
1924 			pr_err("malformed src address passed: %s\n",
1925 			       opts->host_traddr);
1926 			goto out_free_ctrl;
1927 		}
1928 	}
1929 
1930 	if (!opts->duplicate_connect && nvme_rdma_existing_controller(opts)) {
1931 		ret = -EALREADY;
1932 		goto out_free_ctrl;
1933 	}
1934 
1935 	ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_rdma_ctrl_ops,
1936 				0 /* no quirks, we're perfect! */);
1937 	if (ret)
1938 		goto out_free_ctrl;
1939 
1940 	INIT_DELAYED_WORK(&ctrl->reconnect_work,
1941 			nvme_rdma_reconnect_ctrl_work);
1942 	INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
1943 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
1944 
1945 	ctrl->ctrl.queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
1946 	ctrl->ctrl.sqsize = opts->queue_size - 1;
1947 	ctrl->ctrl.kato = opts->kato;
1948 
1949 	ret = -ENOMEM;
1950 	ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
1951 				GFP_KERNEL);
1952 	if (!ctrl->queues)
1953 		goto out_uninit_ctrl;
1954 
1955 	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING);
1956 	WARN_ON_ONCE(!changed);
1957 
1958 	ret = nvme_rdma_configure_admin_queue(ctrl, true);
1959 	if (ret)
1960 		goto out_kfree_queues;
1961 
1962 	/* sanity check icdoff */
1963 	if (ctrl->ctrl.icdoff) {
1964 		dev_err(ctrl->ctrl.device, "icdoff is not supported!\n");
1965 		ret = -EINVAL;
1966 		goto out_remove_admin_queue;
1967 	}
1968 
1969 	/* sanity check keyed sgls */
1970 	if (!(ctrl->ctrl.sgls & (1 << 2))) {
1971 		dev_err(ctrl->ctrl.device,
1972 			"Mandatory keyed sgls are not supported!\n");
1973 		ret = -EINVAL;
1974 		goto out_remove_admin_queue;
1975 	}
1976 
1977 	if (opts->queue_size > ctrl->ctrl.maxcmd) {
1978 		/* warn if maxcmd is lower than queue_size */
1979 		dev_warn(ctrl->ctrl.device,
1980 			"queue_size %zu > ctrl maxcmd %u, clamping down\n",
1981 			opts->queue_size, ctrl->ctrl.maxcmd);
1982 		opts->queue_size = ctrl->ctrl.maxcmd;
1983 	}
1984 
1985 	if (opts->queue_size > ctrl->ctrl.sqsize + 1) {
1986 		/* warn if sqsize is lower than queue_size */
1987 		dev_warn(ctrl->ctrl.device,
1988 			"queue_size %zu > ctrl sqsize %u, clamping down\n",
1989 			opts->queue_size, ctrl->ctrl.sqsize + 1);
1990 		opts->queue_size = ctrl->ctrl.sqsize + 1;
1991 	}
1992 
1993 	if (opts->nr_io_queues) {
1994 		ret = nvme_rdma_configure_io_queues(ctrl, true);
1995 		if (ret)
1996 			goto out_remove_admin_queue;
1997 	}
1998 
1999 	changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
2000 	WARN_ON_ONCE(!changed);
2001 
2002 	dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISpcs\n",
2003 		ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2004 
2005 	nvme_get_ctrl(&ctrl->ctrl);
2006 
2007 	mutex_lock(&nvme_rdma_ctrl_mutex);
2008 	list_add_tail(&ctrl->list, &nvme_rdma_ctrl_list);
2009 	mutex_unlock(&nvme_rdma_ctrl_mutex);
2010 
2011 	nvme_start_ctrl(&ctrl->ctrl);
2012 
2013 	return &ctrl->ctrl;
2014 
2015 out_remove_admin_queue:
2016 	nvme_rdma_destroy_admin_queue(ctrl, true);
2017 out_kfree_queues:
2018 	kfree(ctrl->queues);
2019 out_uninit_ctrl:
2020 	nvme_uninit_ctrl(&ctrl->ctrl);
2021 	nvme_put_ctrl(&ctrl->ctrl);
2022 	if (ret > 0)
2023 		ret = -EIO;
2024 	return ERR_PTR(ret);
2025 out_free_ctrl:
2026 	kfree(ctrl);
2027 	return ERR_PTR(ret);
2028 }
2029 
2030 static struct nvmf_transport_ops nvme_rdma_transport = {
2031 	.name		= "rdma",
2032 	.module		= THIS_MODULE,
2033 	.required_opts	= NVMF_OPT_TRADDR,
2034 	.allowed_opts	= NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2035 			  NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO,
2036 	.create_ctrl	= nvme_rdma_create_ctrl,
2037 };
2038 
2039 static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
2040 {
2041 	struct nvme_rdma_ctrl *ctrl;
2042 	struct nvme_rdma_device *ndev;
2043 	bool found = false;
2044 
2045 	mutex_lock(&device_list_mutex);
2046 	list_for_each_entry(ndev, &device_list, entry) {
2047 		if (ndev->dev == ib_device) {
2048 			found = true;
2049 			break;
2050 		}
2051 	}
2052 	mutex_unlock(&device_list_mutex);
2053 
2054 	if (!found)
2055 		return;
2056 
2057 	/* Delete all controllers using this device */
2058 	mutex_lock(&nvme_rdma_ctrl_mutex);
2059 	list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
2060 		if (ctrl->device->dev != ib_device)
2061 			continue;
2062 		nvme_delete_ctrl(&ctrl->ctrl);
2063 	}
2064 	mutex_unlock(&nvme_rdma_ctrl_mutex);
2065 
2066 	flush_workqueue(nvme_delete_wq);
2067 }
2068 
2069 static struct ib_client nvme_rdma_ib_client = {
2070 	.name   = "nvme_rdma",
2071 	.remove = nvme_rdma_remove_one
2072 };
2073 
2074 static int __init nvme_rdma_init_module(void)
2075 {
2076 	int ret;
2077 
2078 	ret = ib_register_client(&nvme_rdma_ib_client);
2079 	if (ret)
2080 		return ret;
2081 
2082 	ret = nvmf_register_transport(&nvme_rdma_transport);
2083 	if (ret)
2084 		goto err_unreg_client;
2085 
2086 	return 0;
2087 
2088 err_unreg_client:
2089 	ib_unregister_client(&nvme_rdma_ib_client);
2090 	return ret;
2091 }
2092 
2093 static void __exit nvme_rdma_cleanup_module(void)
2094 {
2095 	nvmf_unregister_transport(&nvme_rdma_transport);
2096 	ib_unregister_client(&nvme_rdma_ib_client);
2097 }
2098 
2099 module_init(nvme_rdma_init_module);
2100 module_exit(nvme_rdma_cleanup_module);
2101 
2102 MODULE_LICENSE("GPL v2");
2103