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