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