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