xref: /openbmc/linux/drivers/nvme/target/rdma.c (revision 83c4a4ee)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NVMe over Fabrics RDMA target.
4  * Copyright (c) 2015-2016 HGST, a Western Digital Company.
5  */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/atomic.h>
8 #include <linux/ctype.h>
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/nvme.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 #include <linux/wait.h>
17 #include <linux/inet.h>
18 #include <asm/unaligned.h>
19 
20 #include <rdma/ib_verbs.h>
21 #include <rdma/rdma_cm.h>
22 #include <rdma/rw.h>
23 #include <rdma/ib_cm.h>
24 
25 #include <linux/nvme-rdma.h>
26 #include "nvmet.h"
27 
28 /*
29  * We allow at least 1 page, up to 4 SGEs, and up to 16KB of inline data
30  */
31 #define NVMET_RDMA_DEFAULT_INLINE_DATA_SIZE	PAGE_SIZE
32 #define NVMET_RDMA_MAX_INLINE_SGE		4
33 #define NVMET_RDMA_MAX_INLINE_DATA_SIZE		max_t(int, SZ_16K, PAGE_SIZE)
34 
35 /* Assume mpsmin == device_page_size == 4KB */
36 #define NVMET_RDMA_MAX_MDTS			8
37 #define NVMET_RDMA_MAX_METADATA_MDTS		5
38 
39 struct nvmet_rdma_srq;
40 
41 struct nvmet_rdma_cmd {
42 	struct ib_sge		sge[NVMET_RDMA_MAX_INLINE_SGE + 1];
43 	struct ib_cqe		cqe;
44 	struct ib_recv_wr	wr;
45 	struct scatterlist	inline_sg[NVMET_RDMA_MAX_INLINE_SGE];
46 	struct nvme_command     *nvme_cmd;
47 	struct nvmet_rdma_queue	*queue;
48 	struct nvmet_rdma_srq   *nsrq;
49 };
50 
51 enum {
52 	NVMET_RDMA_REQ_INLINE_DATA	= (1 << 0),
53 	NVMET_RDMA_REQ_INVALIDATE_RKEY	= (1 << 1),
54 };
55 
56 struct nvmet_rdma_rsp {
57 	struct ib_sge		send_sge;
58 	struct ib_cqe		send_cqe;
59 	struct ib_send_wr	send_wr;
60 
61 	struct nvmet_rdma_cmd	*cmd;
62 	struct nvmet_rdma_queue	*queue;
63 
64 	struct ib_cqe		read_cqe;
65 	struct ib_cqe		write_cqe;
66 	struct rdma_rw_ctx	rw;
67 
68 	struct nvmet_req	req;
69 
70 	bool			allocated;
71 	u8			n_rdma;
72 	u32			flags;
73 	u32			invalidate_rkey;
74 
75 	struct list_head	wait_list;
76 	struct list_head	free_list;
77 };
78 
79 enum nvmet_rdma_queue_state {
80 	NVMET_RDMA_Q_CONNECTING,
81 	NVMET_RDMA_Q_LIVE,
82 	NVMET_RDMA_Q_DISCONNECTING,
83 };
84 
85 struct nvmet_rdma_queue {
86 	struct rdma_cm_id	*cm_id;
87 	struct ib_qp		*qp;
88 	struct nvmet_port	*port;
89 	struct ib_cq		*cq;
90 	atomic_t		sq_wr_avail;
91 	struct nvmet_rdma_device *dev;
92 	struct nvmet_rdma_srq   *nsrq;
93 	spinlock_t		state_lock;
94 	enum nvmet_rdma_queue_state state;
95 	struct nvmet_cq		nvme_cq;
96 	struct nvmet_sq		nvme_sq;
97 
98 	struct nvmet_rdma_rsp	*rsps;
99 	struct list_head	free_rsps;
100 	spinlock_t		rsps_lock;
101 	struct nvmet_rdma_cmd	*cmds;
102 
103 	struct work_struct	release_work;
104 	struct list_head	rsp_wait_list;
105 	struct list_head	rsp_wr_wait_list;
106 	spinlock_t		rsp_wr_wait_lock;
107 
108 	int			idx;
109 	int			host_qid;
110 	int			comp_vector;
111 	int			recv_queue_size;
112 	int			send_queue_size;
113 
114 	struct list_head	queue_list;
115 };
116 
117 struct nvmet_rdma_port {
118 	struct nvmet_port	*nport;
119 	struct sockaddr_storage addr;
120 	struct rdma_cm_id	*cm_id;
121 	struct delayed_work	repair_work;
122 };
123 
124 struct nvmet_rdma_srq {
125 	struct ib_srq            *srq;
126 	struct nvmet_rdma_cmd    *cmds;
127 	struct nvmet_rdma_device *ndev;
128 };
129 
130 struct nvmet_rdma_device {
131 	struct ib_device	*device;
132 	struct ib_pd		*pd;
133 	struct nvmet_rdma_srq	**srqs;
134 	int			srq_count;
135 	size_t			srq_size;
136 	struct kref		ref;
137 	struct list_head	entry;
138 	int			inline_data_size;
139 	int			inline_page_count;
140 };
141 
142 static bool nvmet_rdma_use_srq;
143 module_param_named(use_srq, nvmet_rdma_use_srq, bool, 0444);
144 MODULE_PARM_DESC(use_srq, "Use shared receive queue.");
145 
146 static int srq_size_set(const char *val, const struct kernel_param *kp);
147 static const struct kernel_param_ops srq_size_ops = {
148 	.set = srq_size_set,
149 	.get = param_get_int,
150 };
151 
152 static int nvmet_rdma_srq_size = 1024;
153 module_param_cb(srq_size, &srq_size_ops, &nvmet_rdma_srq_size, 0644);
154 MODULE_PARM_DESC(srq_size, "set Shared Receive Queue (SRQ) size, should >= 256 (default: 1024)");
155 
156 static DEFINE_IDA(nvmet_rdma_queue_ida);
157 static LIST_HEAD(nvmet_rdma_queue_list);
158 static DEFINE_MUTEX(nvmet_rdma_queue_mutex);
159 
160 static LIST_HEAD(device_list);
161 static DEFINE_MUTEX(device_list_mutex);
162 
163 static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp);
164 static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc);
165 static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc);
166 static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc);
167 static void nvmet_rdma_write_data_done(struct ib_cq *cq, struct ib_wc *wc);
168 static void nvmet_rdma_qp_event(struct ib_event *event, void *priv);
169 static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue);
170 static void nvmet_rdma_free_rsp(struct nvmet_rdma_device *ndev,
171 				struct nvmet_rdma_rsp *r);
172 static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev,
173 				struct nvmet_rdma_rsp *r);
174 
175 static const struct nvmet_fabrics_ops nvmet_rdma_ops;
176 
177 static int srq_size_set(const char *val, const struct kernel_param *kp)
178 {
179 	int n = 0, ret;
180 
181 	ret = kstrtoint(val, 10, &n);
182 	if (ret != 0 || n < 256)
183 		return -EINVAL;
184 
185 	return param_set_int(val, kp);
186 }
187 
188 static int num_pages(int len)
189 {
190 	return 1 + (((len - 1) & PAGE_MASK) >> PAGE_SHIFT);
191 }
192 
193 static inline bool nvmet_rdma_need_data_in(struct nvmet_rdma_rsp *rsp)
194 {
195 	return nvme_is_write(rsp->req.cmd) &&
196 		rsp->req.transfer_len &&
197 		!(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA);
198 }
199 
200 static inline bool nvmet_rdma_need_data_out(struct nvmet_rdma_rsp *rsp)
201 {
202 	return !nvme_is_write(rsp->req.cmd) &&
203 		rsp->req.transfer_len &&
204 		!rsp->req.cqe->status &&
205 		!(rsp->flags & NVMET_RDMA_REQ_INLINE_DATA);
206 }
207 
208 static inline struct nvmet_rdma_rsp *
209 nvmet_rdma_get_rsp(struct nvmet_rdma_queue *queue)
210 {
211 	struct nvmet_rdma_rsp *rsp;
212 	unsigned long flags;
213 
214 	spin_lock_irqsave(&queue->rsps_lock, flags);
215 	rsp = list_first_entry_or_null(&queue->free_rsps,
216 				struct nvmet_rdma_rsp, free_list);
217 	if (likely(rsp))
218 		list_del(&rsp->free_list);
219 	spin_unlock_irqrestore(&queue->rsps_lock, flags);
220 
221 	if (unlikely(!rsp)) {
222 		int ret;
223 
224 		rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
225 		if (unlikely(!rsp))
226 			return NULL;
227 		ret = nvmet_rdma_alloc_rsp(queue->dev, rsp);
228 		if (unlikely(ret)) {
229 			kfree(rsp);
230 			return NULL;
231 		}
232 
233 		rsp->allocated = true;
234 	}
235 
236 	return rsp;
237 }
238 
239 static inline void
240 nvmet_rdma_put_rsp(struct nvmet_rdma_rsp *rsp)
241 {
242 	unsigned long flags;
243 
244 	if (unlikely(rsp->allocated)) {
245 		nvmet_rdma_free_rsp(rsp->queue->dev, rsp);
246 		kfree(rsp);
247 		return;
248 	}
249 
250 	spin_lock_irqsave(&rsp->queue->rsps_lock, flags);
251 	list_add_tail(&rsp->free_list, &rsp->queue->free_rsps);
252 	spin_unlock_irqrestore(&rsp->queue->rsps_lock, flags);
253 }
254 
255 static void nvmet_rdma_free_inline_pages(struct nvmet_rdma_device *ndev,
256 				struct nvmet_rdma_cmd *c)
257 {
258 	struct scatterlist *sg;
259 	struct ib_sge *sge;
260 	int i;
261 
262 	if (!ndev->inline_data_size)
263 		return;
264 
265 	sg = c->inline_sg;
266 	sge = &c->sge[1];
267 
268 	for (i = 0; i < ndev->inline_page_count; i++, sg++, sge++) {
269 		if (sge->length)
270 			ib_dma_unmap_page(ndev->device, sge->addr,
271 					sge->length, DMA_FROM_DEVICE);
272 		if (sg_page(sg))
273 			__free_page(sg_page(sg));
274 	}
275 }
276 
277 static int nvmet_rdma_alloc_inline_pages(struct nvmet_rdma_device *ndev,
278 				struct nvmet_rdma_cmd *c)
279 {
280 	struct scatterlist *sg;
281 	struct ib_sge *sge;
282 	struct page *pg;
283 	int len;
284 	int i;
285 
286 	if (!ndev->inline_data_size)
287 		return 0;
288 
289 	sg = c->inline_sg;
290 	sg_init_table(sg, ndev->inline_page_count);
291 	sge = &c->sge[1];
292 	len = ndev->inline_data_size;
293 
294 	for (i = 0; i < ndev->inline_page_count; i++, sg++, sge++) {
295 		pg = alloc_page(GFP_KERNEL);
296 		if (!pg)
297 			goto out_err;
298 		sg_assign_page(sg, pg);
299 		sge->addr = ib_dma_map_page(ndev->device,
300 			pg, 0, PAGE_SIZE, DMA_FROM_DEVICE);
301 		if (ib_dma_mapping_error(ndev->device, sge->addr))
302 			goto out_err;
303 		sge->length = min_t(int, len, PAGE_SIZE);
304 		sge->lkey = ndev->pd->local_dma_lkey;
305 		len -= sge->length;
306 	}
307 
308 	return 0;
309 out_err:
310 	for (; i >= 0; i--, sg--, sge--) {
311 		if (sge->length)
312 			ib_dma_unmap_page(ndev->device, sge->addr,
313 					sge->length, DMA_FROM_DEVICE);
314 		if (sg_page(sg))
315 			__free_page(sg_page(sg));
316 	}
317 	return -ENOMEM;
318 }
319 
320 static int nvmet_rdma_alloc_cmd(struct nvmet_rdma_device *ndev,
321 			struct nvmet_rdma_cmd *c, bool admin)
322 {
323 	/* NVMe command / RDMA RECV */
324 	c->nvme_cmd = kmalloc(sizeof(*c->nvme_cmd), GFP_KERNEL);
325 	if (!c->nvme_cmd)
326 		goto out;
327 
328 	c->sge[0].addr = ib_dma_map_single(ndev->device, c->nvme_cmd,
329 			sizeof(*c->nvme_cmd), DMA_FROM_DEVICE);
330 	if (ib_dma_mapping_error(ndev->device, c->sge[0].addr))
331 		goto out_free_cmd;
332 
333 	c->sge[0].length = sizeof(*c->nvme_cmd);
334 	c->sge[0].lkey = ndev->pd->local_dma_lkey;
335 
336 	if (!admin && nvmet_rdma_alloc_inline_pages(ndev, c))
337 		goto out_unmap_cmd;
338 
339 	c->cqe.done = nvmet_rdma_recv_done;
340 
341 	c->wr.wr_cqe = &c->cqe;
342 	c->wr.sg_list = c->sge;
343 	c->wr.num_sge = admin ? 1 : ndev->inline_page_count + 1;
344 
345 	return 0;
346 
347 out_unmap_cmd:
348 	ib_dma_unmap_single(ndev->device, c->sge[0].addr,
349 			sizeof(*c->nvme_cmd), DMA_FROM_DEVICE);
350 out_free_cmd:
351 	kfree(c->nvme_cmd);
352 
353 out:
354 	return -ENOMEM;
355 }
356 
357 static void nvmet_rdma_free_cmd(struct nvmet_rdma_device *ndev,
358 		struct nvmet_rdma_cmd *c, bool admin)
359 {
360 	if (!admin)
361 		nvmet_rdma_free_inline_pages(ndev, c);
362 	ib_dma_unmap_single(ndev->device, c->sge[0].addr,
363 				sizeof(*c->nvme_cmd), DMA_FROM_DEVICE);
364 	kfree(c->nvme_cmd);
365 }
366 
367 static struct nvmet_rdma_cmd *
368 nvmet_rdma_alloc_cmds(struct nvmet_rdma_device *ndev,
369 		int nr_cmds, bool admin)
370 {
371 	struct nvmet_rdma_cmd *cmds;
372 	int ret = -EINVAL, i;
373 
374 	cmds = kcalloc(nr_cmds, sizeof(struct nvmet_rdma_cmd), GFP_KERNEL);
375 	if (!cmds)
376 		goto out;
377 
378 	for (i = 0; i < nr_cmds; i++) {
379 		ret = nvmet_rdma_alloc_cmd(ndev, cmds + i, admin);
380 		if (ret)
381 			goto out_free;
382 	}
383 
384 	return cmds;
385 
386 out_free:
387 	while (--i >= 0)
388 		nvmet_rdma_free_cmd(ndev, cmds + i, admin);
389 	kfree(cmds);
390 out:
391 	return ERR_PTR(ret);
392 }
393 
394 static void nvmet_rdma_free_cmds(struct nvmet_rdma_device *ndev,
395 		struct nvmet_rdma_cmd *cmds, int nr_cmds, bool admin)
396 {
397 	int i;
398 
399 	for (i = 0; i < nr_cmds; i++)
400 		nvmet_rdma_free_cmd(ndev, cmds + i, admin);
401 	kfree(cmds);
402 }
403 
404 static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev,
405 		struct nvmet_rdma_rsp *r)
406 {
407 	/* NVMe CQE / RDMA SEND */
408 	r->req.cqe = kmalloc(sizeof(*r->req.cqe), GFP_KERNEL);
409 	if (!r->req.cqe)
410 		goto out;
411 
412 	r->send_sge.addr = ib_dma_map_single(ndev->device, r->req.cqe,
413 			sizeof(*r->req.cqe), DMA_TO_DEVICE);
414 	if (ib_dma_mapping_error(ndev->device, r->send_sge.addr))
415 		goto out_free_rsp;
416 
417 	if (!ib_uses_virt_dma(ndev->device))
418 		r->req.p2p_client = &ndev->device->dev;
419 	r->send_sge.length = sizeof(*r->req.cqe);
420 	r->send_sge.lkey = ndev->pd->local_dma_lkey;
421 
422 	r->send_cqe.done = nvmet_rdma_send_done;
423 
424 	r->send_wr.wr_cqe = &r->send_cqe;
425 	r->send_wr.sg_list = &r->send_sge;
426 	r->send_wr.num_sge = 1;
427 	r->send_wr.send_flags = IB_SEND_SIGNALED;
428 
429 	/* Data In / RDMA READ */
430 	r->read_cqe.done = nvmet_rdma_read_data_done;
431 	/* Data Out / RDMA WRITE */
432 	r->write_cqe.done = nvmet_rdma_write_data_done;
433 
434 	return 0;
435 
436 out_free_rsp:
437 	kfree(r->req.cqe);
438 out:
439 	return -ENOMEM;
440 }
441 
442 static void nvmet_rdma_free_rsp(struct nvmet_rdma_device *ndev,
443 		struct nvmet_rdma_rsp *r)
444 {
445 	ib_dma_unmap_single(ndev->device, r->send_sge.addr,
446 				sizeof(*r->req.cqe), DMA_TO_DEVICE);
447 	kfree(r->req.cqe);
448 }
449 
450 static int
451 nvmet_rdma_alloc_rsps(struct nvmet_rdma_queue *queue)
452 {
453 	struct nvmet_rdma_device *ndev = queue->dev;
454 	int nr_rsps = queue->recv_queue_size * 2;
455 	int ret = -EINVAL, i;
456 
457 	queue->rsps = kcalloc(nr_rsps, sizeof(struct nvmet_rdma_rsp),
458 			GFP_KERNEL);
459 	if (!queue->rsps)
460 		goto out;
461 
462 	for (i = 0; i < nr_rsps; i++) {
463 		struct nvmet_rdma_rsp *rsp = &queue->rsps[i];
464 
465 		ret = nvmet_rdma_alloc_rsp(ndev, rsp);
466 		if (ret)
467 			goto out_free;
468 
469 		list_add_tail(&rsp->free_list, &queue->free_rsps);
470 	}
471 
472 	return 0;
473 
474 out_free:
475 	while (--i >= 0) {
476 		struct nvmet_rdma_rsp *rsp = &queue->rsps[i];
477 
478 		list_del(&rsp->free_list);
479 		nvmet_rdma_free_rsp(ndev, rsp);
480 	}
481 	kfree(queue->rsps);
482 out:
483 	return ret;
484 }
485 
486 static void nvmet_rdma_free_rsps(struct nvmet_rdma_queue *queue)
487 {
488 	struct nvmet_rdma_device *ndev = queue->dev;
489 	int i, nr_rsps = queue->recv_queue_size * 2;
490 
491 	for (i = 0; i < nr_rsps; i++) {
492 		struct nvmet_rdma_rsp *rsp = &queue->rsps[i];
493 
494 		list_del(&rsp->free_list);
495 		nvmet_rdma_free_rsp(ndev, rsp);
496 	}
497 	kfree(queue->rsps);
498 }
499 
500 static int nvmet_rdma_post_recv(struct nvmet_rdma_device *ndev,
501 		struct nvmet_rdma_cmd *cmd)
502 {
503 	int ret;
504 
505 	ib_dma_sync_single_for_device(ndev->device,
506 		cmd->sge[0].addr, cmd->sge[0].length,
507 		DMA_FROM_DEVICE);
508 
509 	if (cmd->nsrq)
510 		ret = ib_post_srq_recv(cmd->nsrq->srq, &cmd->wr, NULL);
511 	else
512 		ret = ib_post_recv(cmd->queue->qp, &cmd->wr, NULL);
513 
514 	if (unlikely(ret))
515 		pr_err("post_recv cmd failed\n");
516 
517 	return ret;
518 }
519 
520 static void nvmet_rdma_process_wr_wait_list(struct nvmet_rdma_queue *queue)
521 {
522 	spin_lock(&queue->rsp_wr_wait_lock);
523 	while (!list_empty(&queue->rsp_wr_wait_list)) {
524 		struct nvmet_rdma_rsp *rsp;
525 		bool ret;
526 
527 		rsp = list_entry(queue->rsp_wr_wait_list.next,
528 				struct nvmet_rdma_rsp, wait_list);
529 		list_del(&rsp->wait_list);
530 
531 		spin_unlock(&queue->rsp_wr_wait_lock);
532 		ret = nvmet_rdma_execute_command(rsp);
533 		spin_lock(&queue->rsp_wr_wait_lock);
534 
535 		if (!ret) {
536 			list_add(&rsp->wait_list, &queue->rsp_wr_wait_list);
537 			break;
538 		}
539 	}
540 	spin_unlock(&queue->rsp_wr_wait_lock);
541 }
542 
543 static u16 nvmet_rdma_check_pi_status(struct ib_mr *sig_mr)
544 {
545 	struct ib_mr_status mr_status;
546 	int ret;
547 	u16 status = 0;
548 
549 	ret = ib_check_mr_status(sig_mr, IB_MR_CHECK_SIG_STATUS, &mr_status);
550 	if (ret) {
551 		pr_err("ib_check_mr_status failed, ret %d\n", ret);
552 		return NVME_SC_INVALID_PI;
553 	}
554 
555 	if (mr_status.fail_status & IB_MR_CHECK_SIG_STATUS) {
556 		switch (mr_status.sig_err.err_type) {
557 		case IB_SIG_BAD_GUARD:
558 			status = NVME_SC_GUARD_CHECK;
559 			break;
560 		case IB_SIG_BAD_REFTAG:
561 			status = NVME_SC_REFTAG_CHECK;
562 			break;
563 		case IB_SIG_BAD_APPTAG:
564 			status = NVME_SC_APPTAG_CHECK;
565 			break;
566 		}
567 		pr_err("PI error found type %d expected 0x%x vs actual 0x%x\n",
568 		       mr_status.sig_err.err_type,
569 		       mr_status.sig_err.expected,
570 		       mr_status.sig_err.actual);
571 	}
572 
573 	return status;
574 }
575 
576 static void nvmet_rdma_set_sig_domain(struct blk_integrity *bi,
577 		struct nvme_command *cmd, struct ib_sig_domain *domain,
578 		u16 control, u8 pi_type)
579 {
580 	domain->sig_type = IB_SIG_TYPE_T10_DIF;
581 	domain->sig.dif.bg_type = IB_T10DIF_CRC;
582 	domain->sig.dif.pi_interval = 1 << bi->interval_exp;
583 	domain->sig.dif.ref_tag = le32_to_cpu(cmd->rw.reftag);
584 	if (control & NVME_RW_PRINFO_PRCHK_REF)
585 		domain->sig.dif.ref_remap = true;
586 
587 	domain->sig.dif.app_tag = le16_to_cpu(cmd->rw.apptag);
588 	domain->sig.dif.apptag_check_mask = le16_to_cpu(cmd->rw.appmask);
589 	domain->sig.dif.app_escape = true;
590 	if (pi_type == NVME_NS_DPS_PI_TYPE3)
591 		domain->sig.dif.ref_escape = true;
592 }
593 
594 static void nvmet_rdma_set_sig_attrs(struct nvmet_req *req,
595 				     struct ib_sig_attrs *sig_attrs)
596 {
597 	struct nvme_command *cmd = req->cmd;
598 	u16 control = le16_to_cpu(cmd->rw.control);
599 	u8 pi_type = req->ns->pi_type;
600 	struct blk_integrity *bi;
601 
602 	bi = bdev_get_integrity(req->ns->bdev);
603 
604 	memset(sig_attrs, 0, sizeof(*sig_attrs));
605 
606 	if (control & NVME_RW_PRINFO_PRACT) {
607 		/* for WRITE_INSERT/READ_STRIP no wire domain */
608 		sig_attrs->wire.sig_type = IB_SIG_TYPE_NONE;
609 		nvmet_rdma_set_sig_domain(bi, cmd, &sig_attrs->mem, control,
610 					  pi_type);
611 		/* Clear the PRACT bit since HCA will generate/verify the PI */
612 		control &= ~NVME_RW_PRINFO_PRACT;
613 		cmd->rw.control = cpu_to_le16(control);
614 		/* PI is added by the HW */
615 		req->transfer_len += req->metadata_len;
616 	} else {
617 		/* for WRITE_PASS/READ_PASS both wire/memory domains exist */
618 		nvmet_rdma_set_sig_domain(bi, cmd, &sig_attrs->wire, control,
619 					  pi_type);
620 		nvmet_rdma_set_sig_domain(bi, cmd, &sig_attrs->mem, control,
621 					  pi_type);
622 	}
623 
624 	if (control & NVME_RW_PRINFO_PRCHK_REF)
625 		sig_attrs->check_mask |= IB_SIG_CHECK_REFTAG;
626 	if (control & NVME_RW_PRINFO_PRCHK_GUARD)
627 		sig_attrs->check_mask |= IB_SIG_CHECK_GUARD;
628 	if (control & NVME_RW_PRINFO_PRCHK_APP)
629 		sig_attrs->check_mask |= IB_SIG_CHECK_APPTAG;
630 }
631 
632 static int nvmet_rdma_rw_ctx_init(struct nvmet_rdma_rsp *rsp, u64 addr, u32 key,
633 				  struct ib_sig_attrs *sig_attrs)
634 {
635 	struct rdma_cm_id *cm_id = rsp->queue->cm_id;
636 	struct nvmet_req *req = &rsp->req;
637 	int ret;
638 
639 	if (req->metadata_len)
640 		ret = rdma_rw_ctx_signature_init(&rsp->rw, cm_id->qp,
641 			cm_id->port_num, req->sg, req->sg_cnt,
642 			req->metadata_sg, req->metadata_sg_cnt, sig_attrs,
643 			addr, key, nvmet_data_dir(req));
644 	else
645 		ret = rdma_rw_ctx_init(&rsp->rw, cm_id->qp, cm_id->port_num,
646 				       req->sg, req->sg_cnt, 0, addr, key,
647 				       nvmet_data_dir(req));
648 
649 	return ret;
650 }
651 
652 static void nvmet_rdma_rw_ctx_destroy(struct nvmet_rdma_rsp *rsp)
653 {
654 	struct rdma_cm_id *cm_id = rsp->queue->cm_id;
655 	struct nvmet_req *req = &rsp->req;
656 
657 	if (req->metadata_len)
658 		rdma_rw_ctx_destroy_signature(&rsp->rw, cm_id->qp,
659 			cm_id->port_num, req->sg, req->sg_cnt,
660 			req->metadata_sg, req->metadata_sg_cnt,
661 			nvmet_data_dir(req));
662 	else
663 		rdma_rw_ctx_destroy(&rsp->rw, cm_id->qp, cm_id->port_num,
664 				    req->sg, req->sg_cnt, nvmet_data_dir(req));
665 }
666 
667 static void nvmet_rdma_release_rsp(struct nvmet_rdma_rsp *rsp)
668 {
669 	struct nvmet_rdma_queue *queue = rsp->queue;
670 
671 	atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail);
672 
673 	if (rsp->n_rdma)
674 		nvmet_rdma_rw_ctx_destroy(rsp);
675 
676 	if (rsp->req.sg != rsp->cmd->inline_sg)
677 		nvmet_req_free_sgls(&rsp->req);
678 
679 	if (unlikely(!list_empty_careful(&queue->rsp_wr_wait_list)))
680 		nvmet_rdma_process_wr_wait_list(queue);
681 
682 	nvmet_rdma_put_rsp(rsp);
683 }
684 
685 static void nvmet_rdma_error_comp(struct nvmet_rdma_queue *queue)
686 {
687 	if (queue->nvme_sq.ctrl) {
688 		nvmet_ctrl_fatal_error(queue->nvme_sq.ctrl);
689 	} else {
690 		/*
691 		 * we didn't setup the controller yet in case
692 		 * of admin connect error, just disconnect and
693 		 * cleanup the queue
694 		 */
695 		nvmet_rdma_queue_disconnect(queue);
696 	}
697 }
698 
699 static void nvmet_rdma_send_done(struct ib_cq *cq, struct ib_wc *wc)
700 {
701 	struct nvmet_rdma_rsp *rsp =
702 		container_of(wc->wr_cqe, struct nvmet_rdma_rsp, send_cqe);
703 	struct nvmet_rdma_queue *queue = cq->cq_context;
704 
705 	nvmet_rdma_release_rsp(rsp);
706 
707 	if (unlikely(wc->status != IB_WC_SUCCESS &&
708 		     wc->status != IB_WC_WR_FLUSH_ERR)) {
709 		pr_err("SEND for CQE 0x%p failed with status %s (%d).\n",
710 			wc->wr_cqe, ib_wc_status_msg(wc->status), wc->status);
711 		nvmet_rdma_error_comp(queue);
712 	}
713 }
714 
715 static void nvmet_rdma_queue_response(struct nvmet_req *req)
716 {
717 	struct nvmet_rdma_rsp *rsp =
718 		container_of(req, struct nvmet_rdma_rsp, req);
719 	struct rdma_cm_id *cm_id = rsp->queue->cm_id;
720 	struct ib_send_wr *first_wr;
721 
722 	if (rsp->flags & NVMET_RDMA_REQ_INVALIDATE_RKEY) {
723 		rsp->send_wr.opcode = IB_WR_SEND_WITH_INV;
724 		rsp->send_wr.ex.invalidate_rkey = rsp->invalidate_rkey;
725 	} else {
726 		rsp->send_wr.opcode = IB_WR_SEND;
727 	}
728 
729 	if (nvmet_rdma_need_data_out(rsp)) {
730 		if (rsp->req.metadata_len)
731 			first_wr = rdma_rw_ctx_wrs(&rsp->rw, cm_id->qp,
732 					cm_id->port_num, &rsp->write_cqe, NULL);
733 		else
734 			first_wr = rdma_rw_ctx_wrs(&rsp->rw, cm_id->qp,
735 					cm_id->port_num, NULL, &rsp->send_wr);
736 	} else {
737 		first_wr = &rsp->send_wr;
738 	}
739 
740 	nvmet_rdma_post_recv(rsp->queue->dev, rsp->cmd);
741 
742 	ib_dma_sync_single_for_device(rsp->queue->dev->device,
743 		rsp->send_sge.addr, rsp->send_sge.length,
744 		DMA_TO_DEVICE);
745 
746 	if (unlikely(ib_post_send(cm_id->qp, first_wr, NULL))) {
747 		pr_err("sending cmd response failed\n");
748 		nvmet_rdma_release_rsp(rsp);
749 	}
750 }
751 
752 static void nvmet_rdma_read_data_done(struct ib_cq *cq, struct ib_wc *wc)
753 {
754 	struct nvmet_rdma_rsp *rsp =
755 		container_of(wc->wr_cqe, struct nvmet_rdma_rsp, read_cqe);
756 	struct nvmet_rdma_queue *queue = wc->qp->qp_context;
757 	u16 status = 0;
758 
759 	WARN_ON(rsp->n_rdma <= 0);
760 	atomic_add(rsp->n_rdma, &queue->sq_wr_avail);
761 	rsp->n_rdma = 0;
762 
763 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
764 		nvmet_rdma_rw_ctx_destroy(rsp);
765 		nvmet_req_uninit(&rsp->req);
766 		nvmet_rdma_release_rsp(rsp);
767 		if (wc->status != IB_WC_WR_FLUSH_ERR) {
768 			pr_info("RDMA READ for CQE 0x%p failed with status %s (%d).\n",
769 				wc->wr_cqe, ib_wc_status_msg(wc->status), wc->status);
770 			nvmet_rdma_error_comp(queue);
771 		}
772 		return;
773 	}
774 
775 	if (rsp->req.metadata_len)
776 		status = nvmet_rdma_check_pi_status(rsp->rw.reg->mr);
777 	nvmet_rdma_rw_ctx_destroy(rsp);
778 
779 	if (unlikely(status))
780 		nvmet_req_complete(&rsp->req, status);
781 	else
782 		rsp->req.execute(&rsp->req);
783 }
784 
785 static void nvmet_rdma_write_data_done(struct ib_cq *cq, struct ib_wc *wc)
786 {
787 	struct nvmet_rdma_rsp *rsp =
788 		container_of(wc->wr_cqe, struct nvmet_rdma_rsp, write_cqe);
789 	struct nvmet_rdma_queue *queue = cq->cq_context;
790 	struct rdma_cm_id *cm_id = rsp->queue->cm_id;
791 	u16 status;
792 
793 	if (!IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
794 		return;
795 
796 	WARN_ON(rsp->n_rdma <= 0);
797 	atomic_add(rsp->n_rdma, &queue->sq_wr_avail);
798 	rsp->n_rdma = 0;
799 
800 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
801 		nvmet_rdma_rw_ctx_destroy(rsp);
802 		nvmet_req_uninit(&rsp->req);
803 		nvmet_rdma_release_rsp(rsp);
804 		if (wc->status != IB_WC_WR_FLUSH_ERR) {
805 			pr_info("RDMA WRITE for CQE 0x%p failed with status %s (%d).\n",
806 				wc->wr_cqe, ib_wc_status_msg(wc->status),
807 				wc->status);
808 			nvmet_rdma_error_comp(queue);
809 		}
810 		return;
811 	}
812 
813 	/*
814 	 * Upon RDMA completion check the signature status
815 	 * - if succeeded send good NVMe response
816 	 * - if failed send bad NVMe response with appropriate error
817 	 */
818 	status = nvmet_rdma_check_pi_status(rsp->rw.reg->mr);
819 	if (unlikely(status))
820 		rsp->req.cqe->status = cpu_to_le16(status << 1);
821 	nvmet_rdma_rw_ctx_destroy(rsp);
822 
823 	if (unlikely(ib_post_send(cm_id->qp, &rsp->send_wr, NULL))) {
824 		pr_err("sending cmd response failed\n");
825 		nvmet_rdma_release_rsp(rsp);
826 	}
827 }
828 
829 static void nvmet_rdma_use_inline_sg(struct nvmet_rdma_rsp *rsp, u32 len,
830 		u64 off)
831 {
832 	int sg_count = num_pages(len);
833 	struct scatterlist *sg;
834 	int i;
835 
836 	sg = rsp->cmd->inline_sg;
837 	for (i = 0; i < sg_count; i++, sg++) {
838 		if (i < sg_count - 1)
839 			sg_unmark_end(sg);
840 		else
841 			sg_mark_end(sg);
842 		sg->offset = off;
843 		sg->length = min_t(int, len, PAGE_SIZE - off);
844 		len -= sg->length;
845 		if (!i)
846 			off = 0;
847 	}
848 
849 	rsp->req.sg = rsp->cmd->inline_sg;
850 	rsp->req.sg_cnt = sg_count;
851 }
852 
853 static u16 nvmet_rdma_map_sgl_inline(struct nvmet_rdma_rsp *rsp)
854 {
855 	struct nvme_sgl_desc *sgl = &rsp->req.cmd->common.dptr.sgl;
856 	u64 off = le64_to_cpu(sgl->addr);
857 	u32 len = le32_to_cpu(sgl->length);
858 
859 	if (!nvme_is_write(rsp->req.cmd)) {
860 		rsp->req.error_loc =
861 			offsetof(struct nvme_common_command, opcode);
862 		return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
863 	}
864 
865 	if (off + len > rsp->queue->dev->inline_data_size) {
866 		pr_err("invalid inline data offset!\n");
867 		return NVME_SC_SGL_INVALID_OFFSET | NVME_SC_DNR;
868 	}
869 
870 	/* no data command? */
871 	if (!len)
872 		return 0;
873 
874 	nvmet_rdma_use_inline_sg(rsp, len, off);
875 	rsp->flags |= NVMET_RDMA_REQ_INLINE_DATA;
876 	rsp->req.transfer_len += len;
877 	return 0;
878 }
879 
880 static u16 nvmet_rdma_map_sgl_keyed(struct nvmet_rdma_rsp *rsp,
881 		struct nvme_keyed_sgl_desc *sgl, bool invalidate)
882 {
883 	u64 addr = le64_to_cpu(sgl->addr);
884 	u32 key = get_unaligned_le32(sgl->key);
885 	struct ib_sig_attrs sig_attrs;
886 	int ret;
887 
888 	rsp->req.transfer_len = get_unaligned_le24(sgl->length);
889 
890 	/* no data command? */
891 	if (!rsp->req.transfer_len)
892 		return 0;
893 
894 	if (rsp->req.metadata_len)
895 		nvmet_rdma_set_sig_attrs(&rsp->req, &sig_attrs);
896 
897 	ret = nvmet_req_alloc_sgls(&rsp->req);
898 	if (unlikely(ret < 0))
899 		goto error_out;
900 
901 	ret = nvmet_rdma_rw_ctx_init(rsp, addr, key, &sig_attrs);
902 	if (unlikely(ret < 0))
903 		goto error_out;
904 	rsp->n_rdma += ret;
905 
906 	if (invalidate) {
907 		rsp->invalidate_rkey = key;
908 		rsp->flags |= NVMET_RDMA_REQ_INVALIDATE_RKEY;
909 	}
910 
911 	return 0;
912 
913 error_out:
914 	rsp->req.transfer_len = 0;
915 	return NVME_SC_INTERNAL;
916 }
917 
918 static u16 nvmet_rdma_map_sgl(struct nvmet_rdma_rsp *rsp)
919 {
920 	struct nvme_keyed_sgl_desc *sgl = &rsp->req.cmd->common.dptr.ksgl;
921 
922 	switch (sgl->type >> 4) {
923 	case NVME_SGL_FMT_DATA_DESC:
924 		switch (sgl->type & 0xf) {
925 		case NVME_SGL_FMT_OFFSET:
926 			return nvmet_rdma_map_sgl_inline(rsp);
927 		default:
928 			pr_err("invalid SGL subtype: %#x\n", sgl->type);
929 			rsp->req.error_loc =
930 				offsetof(struct nvme_common_command, dptr);
931 			return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
932 		}
933 	case NVME_KEY_SGL_FMT_DATA_DESC:
934 		switch (sgl->type & 0xf) {
935 		case NVME_SGL_FMT_ADDRESS | NVME_SGL_FMT_INVALIDATE:
936 			return nvmet_rdma_map_sgl_keyed(rsp, sgl, true);
937 		case NVME_SGL_FMT_ADDRESS:
938 			return nvmet_rdma_map_sgl_keyed(rsp, sgl, false);
939 		default:
940 			pr_err("invalid SGL subtype: %#x\n", sgl->type);
941 			rsp->req.error_loc =
942 				offsetof(struct nvme_common_command, dptr);
943 			return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
944 		}
945 	default:
946 		pr_err("invalid SGL type: %#x\n", sgl->type);
947 		rsp->req.error_loc = offsetof(struct nvme_common_command, dptr);
948 		return NVME_SC_SGL_INVALID_TYPE | NVME_SC_DNR;
949 	}
950 }
951 
952 static bool nvmet_rdma_execute_command(struct nvmet_rdma_rsp *rsp)
953 {
954 	struct nvmet_rdma_queue *queue = rsp->queue;
955 
956 	if (unlikely(atomic_sub_return(1 + rsp->n_rdma,
957 			&queue->sq_wr_avail) < 0)) {
958 		pr_debug("IB send queue full (needed %d): queue %u cntlid %u\n",
959 				1 + rsp->n_rdma, queue->idx,
960 				queue->nvme_sq.ctrl->cntlid);
961 		atomic_add(1 + rsp->n_rdma, &queue->sq_wr_avail);
962 		return false;
963 	}
964 
965 	if (nvmet_rdma_need_data_in(rsp)) {
966 		if (rdma_rw_ctx_post(&rsp->rw, queue->qp,
967 				queue->cm_id->port_num, &rsp->read_cqe, NULL))
968 			nvmet_req_complete(&rsp->req, NVME_SC_DATA_XFER_ERROR);
969 	} else {
970 		rsp->req.execute(&rsp->req);
971 	}
972 
973 	return true;
974 }
975 
976 static void nvmet_rdma_handle_command(struct nvmet_rdma_queue *queue,
977 		struct nvmet_rdma_rsp *cmd)
978 {
979 	u16 status;
980 
981 	ib_dma_sync_single_for_cpu(queue->dev->device,
982 		cmd->cmd->sge[0].addr, cmd->cmd->sge[0].length,
983 		DMA_FROM_DEVICE);
984 	ib_dma_sync_single_for_cpu(queue->dev->device,
985 		cmd->send_sge.addr, cmd->send_sge.length,
986 		DMA_TO_DEVICE);
987 
988 	if (!nvmet_req_init(&cmd->req, &queue->nvme_cq,
989 			&queue->nvme_sq, &nvmet_rdma_ops))
990 		return;
991 
992 	status = nvmet_rdma_map_sgl(cmd);
993 	if (status)
994 		goto out_err;
995 
996 	if (unlikely(!nvmet_rdma_execute_command(cmd))) {
997 		spin_lock(&queue->rsp_wr_wait_lock);
998 		list_add_tail(&cmd->wait_list, &queue->rsp_wr_wait_list);
999 		spin_unlock(&queue->rsp_wr_wait_lock);
1000 	}
1001 
1002 	return;
1003 
1004 out_err:
1005 	nvmet_req_complete(&cmd->req, status);
1006 }
1007 
1008 static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
1009 {
1010 	struct nvmet_rdma_cmd *cmd =
1011 		container_of(wc->wr_cqe, struct nvmet_rdma_cmd, cqe);
1012 	struct nvmet_rdma_queue *queue = wc->qp->qp_context;
1013 	struct nvmet_rdma_rsp *rsp;
1014 
1015 	if (unlikely(wc->status != IB_WC_SUCCESS)) {
1016 		if (wc->status != IB_WC_WR_FLUSH_ERR) {
1017 			pr_err("RECV for CQE 0x%p failed with status %s (%d)\n",
1018 				wc->wr_cqe, ib_wc_status_msg(wc->status),
1019 				wc->status);
1020 			nvmet_rdma_error_comp(queue);
1021 		}
1022 		return;
1023 	}
1024 
1025 	if (unlikely(wc->byte_len < sizeof(struct nvme_command))) {
1026 		pr_err("Ctrl Fatal Error: capsule size less than 64 bytes\n");
1027 		nvmet_rdma_error_comp(queue);
1028 		return;
1029 	}
1030 
1031 	cmd->queue = queue;
1032 	rsp = nvmet_rdma_get_rsp(queue);
1033 	if (unlikely(!rsp)) {
1034 		/*
1035 		 * we get here only under memory pressure,
1036 		 * silently drop and have the host retry
1037 		 * as we can't even fail it.
1038 		 */
1039 		nvmet_rdma_post_recv(queue->dev, cmd);
1040 		return;
1041 	}
1042 	rsp->queue = queue;
1043 	rsp->cmd = cmd;
1044 	rsp->flags = 0;
1045 	rsp->req.cmd = cmd->nvme_cmd;
1046 	rsp->req.port = queue->port;
1047 	rsp->n_rdma = 0;
1048 
1049 	if (unlikely(queue->state != NVMET_RDMA_Q_LIVE)) {
1050 		unsigned long flags;
1051 
1052 		spin_lock_irqsave(&queue->state_lock, flags);
1053 		if (queue->state == NVMET_RDMA_Q_CONNECTING)
1054 			list_add_tail(&rsp->wait_list, &queue->rsp_wait_list);
1055 		else
1056 			nvmet_rdma_put_rsp(rsp);
1057 		spin_unlock_irqrestore(&queue->state_lock, flags);
1058 		return;
1059 	}
1060 
1061 	nvmet_rdma_handle_command(queue, rsp);
1062 }
1063 
1064 static void nvmet_rdma_destroy_srq(struct nvmet_rdma_srq *nsrq)
1065 {
1066 	nvmet_rdma_free_cmds(nsrq->ndev, nsrq->cmds, nsrq->ndev->srq_size,
1067 			     false);
1068 	ib_destroy_srq(nsrq->srq);
1069 
1070 	kfree(nsrq);
1071 }
1072 
1073 static void nvmet_rdma_destroy_srqs(struct nvmet_rdma_device *ndev)
1074 {
1075 	int i;
1076 
1077 	if (!ndev->srqs)
1078 		return;
1079 
1080 	for (i = 0; i < ndev->srq_count; i++)
1081 		nvmet_rdma_destroy_srq(ndev->srqs[i]);
1082 
1083 	kfree(ndev->srqs);
1084 }
1085 
1086 static struct nvmet_rdma_srq *
1087 nvmet_rdma_init_srq(struct nvmet_rdma_device *ndev)
1088 {
1089 	struct ib_srq_init_attr srq_attr = { NULL, };
1090 	size_t srq_size = ndev->srq_size;
1091 	struct nvmet_rdma_srq *nsrq;
1092 	struct ib_srq *srq;
1093 	int ret, i;
1094 
1095 	nsrq = kzalloc(sizeof(*nsrq), GFP_KERNEL);
1096 	if (!nsrq)
1097 		return ERR_PTR(-ENOMEM);
1098 
1099 	srq_attr.attr.max_wr = srq_size;
1100 	srq_attr.attr.max_sge = 1 + ndev->inline_page_count;
1101 	srq_attr.attr.srq_limit = 0;
1102 	srq_attr.srq_type = IB_SRQT_BASIC;
1103 	srq = ib_create_srq(ndev->pd, &srq_attr);
1104 	if (IS_ERR(srq)) {
1105 		ret = PTR_ERR(srq);
1106 		goto out_free;
1107 	}
1108 
1109 	nsrq->cmds = nvmet_rdma_alloc_cmds(ndev, srq_size, false);
1110 	if (IS_ERR(nsrq->cmds)) {
1111 		ret = PTR_ERR(nsrq->cmds);
1112 		goto out_destroy_srq;
1113 	}
1114 
1115 	nsrq->srq = srq;
1116 	nsrq->ndev = ndev;
1117 
1118 	for (i = 0; i < srq_size; i++) {
1119 		nsrq->cmds[i].nsrq = nsrq;
1120 		ret = nvmet_rdma_post_recv(ndev, &nsrq->cmds[i]);
1121 		if (ret)
1122 			goto out_free_cmds;
1123 	}
1124 
1125 	return nsrq;
1126 
1127 out_free_cmds:
1128 	nvmet_rdma_free_cmds(ndev, nsrq->cmds, srq_size, false);
1129 out_destroy_srq:
1130 	ib_destroy_srq(srq);
1131 out_free:
1132 	kfree(nsrq);
1133 	return ERR_PTR(ret);
1134 }
1135 
1136 static int nvmet_rdma_init_srqs(struct nvmet_rdma_device *ndev)
1137 {
1138 	int i, ret;
1139 
1140 	if (!ndev->device->attrs.max_srq_wr || !ndev->device->attrs.max_srq) {
1141 		/*
1142 		 * If SRQs aren't supported we just go ahead and use normal
1143 		 * non-shared receive queues.
1144 		 */
1145 		pr_info("SRQ requested but not supported.\n");
1146 		return 0;
1147 	}
1148 
1149 	ndev->srq_size = min(ndev->device->attrs.max_srq_wr,
1150 			     nvmet_rdma_srq_size);
1151 	ndev->srq_count = min(ndev->device->num_comp_vectors,
1152 			      ndev->device->attrs.max_srq);
1153 
1154 	ndev->srqs = kcalloc(ndev->srq_count, sizeof(*ndev->srqs), GFP_KERNEL);
1155 	if (!ndev->srqs)
1156 		return -ENOMEM;
1157 
1158 	for (i = 0; i < ndev->srq_count; i++) {
1159 		ndev->srqs[i] = nvmet_rdma_init_srq(ndev);
1160 		if (IS_ERR(ndev->srqs[i])) {
1161 			ret = PTR_ERR(ndev->srqs[i]);
1162 			goto err_srq;
1163 		}
1164 	}
1165 
1166 	return 0;
1167 
1168 err_srq:
1169 	while (--i >= 0)
1170 		nvmet_rdma_destroy_srq(ndev->srqs[i]);
1171 	kfree(ndev->srqs);
1172 	return ret;
1173 }
1174 
1175 static void nvmet_rdma_free_dev(struct kref *ref)
1176 {
1177 	struct nvmet_rdma_device *ndev =
1178 		container_of(ref, struct nvmet_rdma_device, ref);
1179 
1180 	mutex_lock(&device_list_mutex);
1181 	list_del(&ndev->entry);
1182 	mutex_unlock(&device_list_mutex);
1183 
1184 	nvmet_rdma_destroy_srqs(ndev);
1185 	ib_dealloc_pd(ndev->pd);
1186 
1187 	kfree(ndev);
1188 }
1189 
1190 static struct nvmet_rdma_device *
1191 nvmet_rdma_find_get_device(struct rdma_cm_id *cm_id)
1192 {
1193 	struct nvmet_rdma_port *port = cm_id->context;
1194 	struct nvmet_port *nport = port->nport;
1195 	struct nvmet_rdma_device *ndev;
1196 	int inline_page_count;
1197 	int inline_sge_count;
1198 	int ret;
1199 
1200 	mutex_lock(&device_list_mutex);
1201 	list_for_each_entry(ndev, &device_list, entry) {
1202 		if (ndev->device->node_guid == cm_id->device->node_guid &&
1203 		    kref_get_unless_zero(&ndev->ref))
1204 			goto out_unlock;
1205 	}
1206 
1207 	ndev = kzalloc(sizeof(*ndev), GFP_KERNEL);
1208 	if (!ndev)
1209 		goto out_err;
1210 
1211 	inline_page_count = num_pages(nport->inline_data_size);
1212 	inline_sge_count = max(cm_id->device->attrs.max_sge_rd,
1213 				cm_id->device->attrs.max_recv_sge) - 1;
1214 	if (inline_page_count > inline_sge_count) {
1215 		pr_warn("inline_data_size %d cannot be supported by device %s. Reducing to %lu.\n",
1216 			nport->inline_data_size, cm_id->device->name,
1217 			inline_sge_count * PAGE_SIZE);
1218 		nport->inline_data_size = inline_sge_count * PAGE_SIZE;
1219 		inline_page_count = inline_sge_count;
1220 	}
1221 	ndev->inline_data_size = nport->inline_data_size;
1222 	ndev->inline_page_count = inline_page_count;
1223 	ndev->device = cm_id->device;
1224 	kref_init(&ndev->ref);
1225 
1226 	ndev->pd = ib_alloc_pd(ndev->device, 0);
1227 	if (IS_ERR(ndev->pd))
1228 		goto out_free_dev;
1229 
1230 	if (nvmet_rdma_use_srq) {
1231 		ret = nvmet_rdma_init_srqs(ndev);
1232 		if (ret)
1233 			goto out_free_pd;
1234 	}
1235 
1236 	list_add(&ndev->entry, &device_list);
1237 out_unlock:
1238 	mutex_unlock(&device_list_mutex);
1239 	pr_debug("added %s.\n", ndev->device->name);
1240 	return ndev;
1241 
1242 out_free_pd:
1243 	ib_dealloc_pd(ndev->pd);
1244 out_free_dev:
1245 	kfree(ndev);
1246 out_err:
1247 	mutex_unlock(&device_list_mutex);
1248 	return NULL;
1249 }
1250 
1251 static int nvmet_rdma_create_queue_ib(struct nvmet_rdma_queue *queue)
1252 {
1253 	struct ib_qp_init_attr qp_attr;
1254 	struct nvmet_rdma_device *ndev = queue->dev;
1255 	int nr_cqe, ret, i, factor;
1256 
1257 	/*
1258 	 * Reserve CQ slots for RECV + RDMA_READ/RDMA_WRITE + RDMA_SEND.
1259 	 */
1260 	nr_cqe = queue->recv_queue_size + 2 * queue->send_queue_size;
1261 
1262 	queue->cq = ib_cq_pool_get(ndev->device, nr_cqe + 1,
1263 				   queue->comp_vector, IB_POLL_WORKQUEUE);
1264 	if (IS_ERR(queue->cq)) {
1265 		ret = PTR_ERR(queue->cq);
1266 		pr_err("failed to create CQ cqe= %d ret= %d\n",
1267 		       nr_cqe + 1, ret);
1268 		goto out;
1269 	}
1270 
1271 	memset(&qp_attr, 0, sizeof(qp_attr));
1272 	qp_attr.qp_context = queue;
1273 	qp_attr.event_handler = nvmet_rdma_qp_event;
1274 	qp_attr.send_cq = queue->cq;
1275 	qp_attr.recv_cq = queue->cq;
1276 	qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
1277 	qp_attr.qp_type = IB_QPT_RC;
1278 	/* +1 for drain */
1279 	qp_attr.cap.max_send_wr = queue->send_queue_size + 1;
1280 	factor = rdma_rw_mr_factor(ndev->device, queue->cm_id->port_num,
1281 				   1 << NVMET_RDMA_MAX_MDTS);
1282 	qp_attr.cap.max_rdma_ctxs = queue->send_queue_size * factor;
1283 	qp_attr.cap.max_send_sge = max(ndev->device->attrs.max_sge_rd,
1284 					ndev->device->attrs.max_send_sge);
1285 
1286 	if (queue->nsrq) {
1287 		qp_attr.srq = queue->nsrq->srq;
1288 	} else {
1289 		/* +1 for drain */
1290 		qp_attr.cap.max_recv_wr = 1 + queue->recv_queue_size;
1291 		qp_attr.cap.max_recv_sge = 1 + ndev->inline_page_count;
1292 	}
1293 
1294 	if (queue->port->pi_enable && queue->host_qid)
1295 		qp_attr.create_flags |= IB_QP_CREATE_INTEGRITY_EN;
1296 
1297 	ret = rdma_create_qp(queue->cm_id, ndev->pd, &qp_attr);
1298 	if (ret) {
1299 		pr_err("failed to create_qp ret= %d\n", ret);
1300 		goto err_destroy_cq;
1301 	}
1302 	queue->qp = queue->cm_id->qp;
1303 
1304 	atomic_set(&queue->sq_wr_avail, qp_attr.cap.max_send_wr);
1305 
1306 	pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
1307 		 __func__, queue->cq->cqe, qp_attr.cap.max_send_sge,
1308 		 qp_attr.cap.max_send_wr, queue->cm_id);
1309 
1310 	if (!queue->nsrq) {
1311 		for (i = 0; i < queue->recv_queue_size; i++) {
1312 			queue->cmds[i].queue = queue;
1313 			ret = nvmet_rdma_post_recv(ndev, &queue->cmds[i]);
1314 			if (ret)
1315 				goto err_destroy_qp;
1316 		}
1317 	}
1318 
1319 out:
1320 	return ret;
1321 
1322 err_destroy_qp:
1323 	rdma_destroy_qp(queue->cm_id);
1324 err_destroy_cq:
1325 	ib_cq_pool_put(queue->cq, nr_cqe + 1);
1326 	goto out;
1327 }
1328 
1329 static void nvmet_rdma_destroy_queue_ib(struct nvmet_rdma_queue *queue)
1330 {
1331 	ib_drain_qp(queue->qp);
1332 	if (queue->cm_id)
1333 		rdma_destroy_id(queue->cm_id);
1334 	ib_destroy_qp(queue->qp);
1335 	ib_cq_pool_put(queue->cq, queue->recv_queue_size + 2 *
1336 		       queue->send_queue_size + 1);
1337 }
1338 
1339 static void nvmet_rdma_free_queue(struct nvmet_rdma_queue *queue)
1340 {
1341 	pr_debug("freeing queue %d\n", queue->idx);
1342 
1343 	nvmet_sq_destroy(&queue->nvme_sq);
1344 
1345 	nvmet_rdma_destroy_queue_ib(queue);
1346 	if (!queue->nsrq) {
1347 		nvmet_rdma_free_cmds(queue->dev, queue->cmds,
1348 				queue->recv_queue_size,
1349 				!queue->host_qid);
1350 	}
1351 	nvmet_rdma_free_rsps(queue);
1352 	ida_simple_remove(&nvmet_rdma_queue_ida, queue->idx);
1353 	kfree(queue);
1354 }
1355 
1356 static void nvmet_rdma_release_queue_work(struct work_struct *w)
1357 {
1358 	struct nvmet_rdma_queue *queue =
1359 		container_of(w, struct nvmet_rdma_queue, release_work);
1360 	struct nvmet_rdma_device *dev = queue->dev;
1361 
1362 	nvmet_rdma_free_queue(queue);
1363 
1364 	kref_put(&dev->ref, nvmet_rdma_free_dev);
1365 }
1366 
1367 static int
1368 nvmet_rdma_parse_cm_connect_req(struct rdma_conn_param *conn,
1369 				struct nvmet_rdma_queue *queue)
1370 {
1371 	struct nvme_rdma_cm_req *req;
1372 
1373 	req = (struct nvme_rdma_cm_req *)conn->private_data;
1374 	if (!req || conn->private_data_len == 0)
1375 		return NVME_RDMA_CM_INVALID_LEN;
1376 
1377 	if (le16_to_cpu(req->recfmt) != NVME_RDMA_CM_FMT_1_0)
1378 		return NVME_RDMA_CM_INVALID_RECFMT;
1379 
1380 	queue->host_qid = le16_to_cpu(req->qid);
1381 
1382 	/*
1383 	 * req->hsqsize corresponds to our recv queue size plus 1
1384 	 * req->hrqsize corresponds to our send queue size
1385 	 */
1386 	queue->recv_queue_size = le16_to_cpu(req->hsqsize) + 1;
1387 	queue->send_queue_size = le16_to_cpu(req->hrqsize);
1388 
1389 	if (!queue->host_qid && queue->recv_queue_size > NVME_AQ_DEPTH)
1390 		return NVME_RDMA_CM_INVALID_HSQSIZE;
1391 
1392 	/* XXX: Should we enforce some kind of max for IO queues? */
1393 
1394 	return 0;
1395 }
1396 
1397 static int nvmet_rdma_cm_reject(struct rdma_cm_id *cm_id,
1398 				enum nvme_rdma_cm_status status)
1399 {
1400 	struct nvme_rdma_cm_rej rej;
1401 
1402 	pr_debug("rejecting connect request: status %d (%s)\n",
1403 		 status, nvme_rdma_cm_msg(status));
1404 
1405 	rej.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1406 	rej.sts = cpu_to_le16(status);
1407 
1408 	return rdma_reject(cm_id, (void *)&rej, sizeof(rej),
1409 			   IB_CM_REJ_CONSUMER_DEFINED);
1410 }
1411 
1412 static struct nvmet_rdma_queue *
1413 nvmet_rdma_alloc_queue(struct nvmet_rdma_device *ndev,
1414 		struct rdma_cm_id *cm_id,
1415 		struct rdma_cm_event *event)
1416 {
1417 	struct nvmet_rdma_port *port = cm_id->context;
1418 	struct nvmet_rdma_queue *queue;
1419 	int ret;
1420 
1421 	queue = kzalloc(sizeof(*queue), GFP_KERNEL);
1422 	if (!queue) {
1423 		ret = NVME_RDMA_CM_NO_RSC;
1424 		goto out_reject;
1425 	}
1426 
1427 	ret = nvmet_sq_init(&queue->nvme_sq);
1428 	if (ret) {
1429 		ret = NVME_RDMA_CM_NO_RSC;
1430 		goto out_free_queue;
1431 	}
1432 
1433 	ret = nvmet_rdma_parse_cm_connect_req(&event->param.conn, queue);
1434 	if (ret)
1435 		goto out_destroy_sq;
1436 
1437 	/*
1438 	 * Schedules the actual release because calling rdma_destroy_id from
1439 	 * inside a CM callback would trigger a deadlock. (great API design..)
1440 	 */
1441 	INIT_WORK(&queue->release_work, nvmet_rdma_release_queue_work);
1442 	queue->dev = ndev;
1443 	queue->cm_id = cm_id;
1444 	queue->port = port->nport;
1445 
1446 	spin_lock_init(&queue->state_lock);
1447 	queue->state = NVMET_RDMA_Q_CONNECTING;
1448 	INIT_LIST_HEAD(&queue->rsp_wait_list);
1449 	INIT_LIST_HEAD(&queue->rsp_wr_wait_list);
1450 	spin_lock_init(&queue->rsp_wr_wait_lock);
1451 	INIT_LIST_HEAD(&queue->free_rsps);
1452 	spin_lock_init(&queue->rsps_lock);
1453 	INIT_LIST_HEAD(&queue->queue_list);
1454 
1455 	queue->idx = ida_simple_get(&nvmet_rdma_queue_ida, 0, 0, GFP_KERNEL);
1456 	if (queue->idx < 0) {
1457 		ret = NVME_RDMA_CM_NO_RSC;
1458 		goto out_destroy_sq;
1459 	}
1460 
1461 	/*
1462 	 * Spread the io queues across completion vectors,
1463 	 * but still keep all admin queues on vector 0.
1464 	 */
1465 	queue->comp_vector = !queue->host_qid ? 0 :
1466 		queue->idx % ndev->device->num_comp_vectors;
1467 
1468 
1469 	ret = nvmet_rdma_alloc_rsps(queue);
1470 	if (ret) {
1471 		ret = NVME_RDMA_CM_NO_RSC;
1472 		goto out_ida_remove;
1473 	}
1474 
1475 	if (ndev->srqs) {
1476 		queue->nsrq = ndev->srqs[queue->comp_vector % ndev->srq_count];
1477 	} else {
1478 		queue->cmds = nvmet_rdma_alloc_cmds(ndev,
1479 				queue->recv_queue_size,
1480 				!queue->host_qid);
1481 		if (IS_ERR(queue->cmds)) {
1482 			ret = NVME_RDMA_CM_NO_RSC;
1483 			goto out_free_responses;
1484 		}
1485 	}
1486 
1487 	ret = nvmet_rdma_create_queue_ib(queue);
1488 	if (ret) {
1489 		pr_err("%s: creating RDMA queue failed (%d).\n",
1490 			__func__, ret);
1491 		ret = NVME_RDMA_CM_NO_RSC;
1492 		goto out_free_cmds;
1493 	}
1494 
1495 	return queue;
1496 
1497 out_free_cmds:
1498 	if (!queue->nsrq) {
1499 		nvmet_rdma_free_cmds(queue->dev, queue->cmds,
1500 				queue->recv_queue_size,
1501 				!queue->host_qid);
1502 	}
1503 out_free_responses:
1504 	nvmet_rdma_free_rsps(queue);
1505 out_ida_remove:
1506 	ida_simple_remove(&nvmet_rdma_queue_ida, queue->idx);
1507 out_destroy_sq:
1508 	nvmet_sq_destroy(&queue->nvme_sq);
1509 out_free_queue:
1510 	kfree(queue);
1511 out_reject:
1512 	nvmet_rdma_cm_reject(cm_id, ret);
1513 	return NULL;
1514 }
1515 
1516 static void nvmet_rdma_qp_event(struct ib_event *event, void *priv)
1517 {
1518 	struct nvmet_rdma_queue *queue = priv;
1519 
1520 	switch (event->event) {
1521 	case IB_EVENT_COMM_EST:
1522 		rdma_notify(queue->cm_id, event->event);
1523 		break;
1524 	case IB_EVENT_QP_LAST_WQE_REACHED:
1525 		pr_debug("received last WQE reached event for queue=0x%p\n",
1526 			 queue);
1527 		break;
1528 	default:
1529 		pr_err("received IB QP event: %s (%d)\n",
1530 		       ib_event_msg(event->event), event->event);
1531 		break;
1532 	}
1533 }
1534 
1535 static int nvmet_rdma_cm_accept(struct rdma_cm_id *cm_id,
1536 		struct nvmet_rdma_queue *queue,
1537 		struct rdma_conn_param *p)
1538 {
1539 	struct rdma_conn_param  param = { };
1540 	struct nvme_rdma_cm_rep priv = { };
1541 	int ret = -ENOMEM;
1542 
1543 	param.rnr_retry_count = 7;
1544 	param.flow_control = 1;
1545 	param.initiator_depth = min_t(u8, p->initiator_depth,
1546 		queue->dev->device->attrs.max_qp_init_rd_atom);
1547 	param.private_data = &priv;
1548 	param.private_data_len = sizeof(priv);
1549 	priv.recfmt = cpu_to_le16(NVME_RDMA_CM_FMT_1_0);
1550 	priv.crqsize = cpu_to_le16(queue->recv_queue_size);
1551 
1552 	ret = rdma_accept(cm_id, &param);
1553 	if (ret)
1554 		pr_err("rdma_accept failed (error code = %d)\n", ret);
1555 
1556 	return ret;
1557 }
1558 
1559 static int nvmet_rdma_queue_connect(struct rdma_cm_id *cm_id,
1560 		struct rdma_cm_event *event)
1561 {
1562 	struct nvmet_rdma_device *ndev;
1563 	struct nvmet_rdma_queue *queue;
1564 	int ret = -EINVAL;
1565 
1566 	ndev = nvmet_rdma_find_get_device(cm_id);
1567 	if (!ndev) {
1568 		nvmet_rdma_cm_reject(cm_id, NVME_RDMA_CM_NO_RSC);
1569 		return -ECONNREFUSED;
1570 	}
1571 
1572 	queue = nvmet_rdma_alloc_queue(ndev, cm_id, event);
1573 	if (!queue) {
1574 		ret = -ENOMEM;
1575 		goto put_device;
1576 	}
1577 
1578 	if (queue->host_qid == 0) {
1579 		/* Let inflight controller teardown complete */
1580 		flush_scheduled_work();
1581 	}
1582 
1583 	ret = nvmet_rdma_cm_accept(cm_id, queue, &event->param.conn);
1584 	if (ret) {
1585 		/*
1586 		 * Don't destroy the cm_id in free path, as we implicitly
1587 		 * destroy the cm_id here with non-zero ret code.
1588 		 */
1589 		queue->cm_id = NULL;
1590 		goto free_queue;
1591 	}
1592 
1593 	mutex_lock(&nvmet_rdma_queue_mutex);
1594 	list_add_tail(&queue->queue_list, &nvmet_rdma_queue_list);
1595 	mutex_unlock(&nvmet_rdma_queue_mutex);
1596 
1597 	return 0;
1598 
1599 free_queue:
1600 	nvmet_rdma_free_queue(queue);
1601 put_device:
1602 	kref_put(&ndev->ref, nvmet_rdma_free_dev);
1603 
1604 	return ret;
1605 }
1606 
1607 static void nvmet_rdma_queue_established(struct nvmet_rdma_queue *queue)
1608 {
1609 	unsigned long flags;
1610 
1611 	spin_lock_irqsave(&queue->state_lock, flags);
1612 	if (queue->state != NVMET_RDMA_Q_CONNECTING) {
1613 		pr_warn("trying to establish a connected queue\n");
1614 		goto out_unlock;
1615 	}
1616 	queue->state = NVMET_RDMA_Q_LIVE;
1617 
1618 	while (!list_empty(&queue->rsp_wait_list)) {
1619 		struct nvmet_rdma_rsp *cmd;
1620 
1621 		cmd = list_first_entry(&queue->rsp_wait_list,
1622 					struct nvmet_rdma_rsp, wait_list);
1623 		list_del(&cmd->wait_list);
1624 
1625 		spin_unlock_irqrestore(&queue->state_lock, flags);
1626 		nvmet_rdma_handle_command(queue, cmd);
1627 		spin_lock_irqsave(&queue->state_lock, flags);
1628 	}
1629 
1630 out_unlock:
1631 	spin_unlock_irqrestore(&queue->state_lock, flags);
1632 }
1633 
1634 static void __nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue)
1635 {
1636 	bool disconnect = false;
1637 	unsigned long flags;
1638 
1639 	pr_debug("cm_id= %p queue->state= %d\n", queue->cm_id, queue->state);
1640 
1641 	spin_lock_irqsave(&queue->state_lock, flags);
1642 	switch (queue->state) {
1643 	case NVMET_RDMA_Q_CONNECTING:
1644 	case NVMET_RDMA_Q_LIVE:
1645 		queue->state = NVMET_RDMA_Q_DISCONNECTING;
1646 		disconnect = true;
1647 		break;
1648 	case NVMET_RDMA_Q_DISCONNECTING:
1649 		break;
1650 	}
1651 	spin_unlock_irqrestore(&queue->state_lock, flags);
1652 
1653 	if (disconnect) {
1654 		rdma_disconnect(queue->cm_id);
1655 		schedule_work(&queue->release_work);
1656 	}
1657 }
1658 
1659 static void nvmet_rdma_queue_disconnect(struct nvmet_rdma_queue *queue)
1660 {
1661 	bool disconnect = false;
1662 
1663 	mutex_lock(&nvmet_rdma_queue_mutex);
1664 	if (!list_empty(&queue->queue_list)) {
1665 		list_del_init(&queue->queue_list);
1666 		disconnect = true;
1667 	}
1668 	mutex_unlock(&nvmet_rdma_queue_mutex);
1669 
1670 	if (disconnect)
1671 		__nvmet_rdma_queue_disconnect(queue);
1672 }
1673 
1674 static void nvmet_rdma_queue_connect_fail(struct rdma_cm_id *cm_id,
1675 		struct nvmet_rdma_queue *queue)
1676 {
1677 	WARN_ON_ONCE(queue->state != NVMET_RDMA_Q_CONNECTING);
1678 
1679 	mutex_lock(&nvmet_rdma_queue_mutex);
1680 	if (!list_empty(&queue->queue_list))
1681 		list_del_init(&queue->queue_list);
1682 	mutex_unlock(&nvmet_rdma_queue_mutex);
1683 
1684 	pr_err("failed to connect queue %d\n", queue->idx);
1685 	schedule_work(&queue->release_work);
1686 }
1687 
1688 /**
1689  * nvme_rdma_device_removal() - Handle RDMA device removal
1690  * @cm_id:	rdma_cm id, used for nvmet port
1691  * @queue:      nvmet rdma queue (cm id qp_context)
1692  *
1693  * DEVICE_REMOVAL event notifies us that the RDMA device is about
1694  * to unplug. Note that this event can be generated on a normal
1695  * queue cm_id and/or a device bound listener cm_id (where in this
1696  * case queue will be null).
1697  *
1698  * We registered an ib_client to handle device removal for queues,
1699  * so we only need to handle the listening port cm_ids. In this case
1700  * we nullify the priv to prevent double cm_id destruction and destroying
1701  * the cm_id implicitely by returning a non-zero rc to the callout.
1702  */
1703 static int nvmet_rdma_device_removal(struct rdma_cm_id *cm_id,
1704 		struct nvmet_rdma_queue *queue)
1705 {
1706 	struct nvmet_rdma_port *port;
1707 
1708 	if (queue) {
1709 		/*
1710 		 * This is a queue cm_id. we have registered
1711 		 * an ib_client to handle queues removal
1712 		 * so don't interfear and just return.
1713 		 */
1714 		return 0;
1715 	}
1716 
1717 	port = cm_id->context;
1718 
1719 	/*
1720 	 * This is a listener cm_id. Make sure that
1721 	 * future remove_port won't invoke a double
1722 	 * cm_id destroy. use atomic xchg to make sure
1723 	 * we don't compete with remove_port.
1724 	 */
1725 	if (xchg(&port->cm_id, NULL) != cm_id)
1726 		return 0;
1727 
1728 	/*
1729 	 * We need to return 1 so that the core will destroy
1730 	 * it's own ID.  What a great API design..
1731 	 */
1732 	return 1;
1733 }
1734 
1735 static int nvmet_rdma_cm_handler(struct rdma_cm_id *cm_id,
1736 		struct rdma_cm_event *event)
1737 {
1738 	struct nvmet_rdma_queue *queue = NULL;
1739 	int ret = 0;
1740 
1741 	if (cm_id->qp)
1742 		queue = cm_id->qp->qp_context;
1743 
1744 	pr_debug("%s (%d): status %d id %p\n",
1745 		rdma_event_msg(event->event), event->event,
1746 		event->status, cm_id);
1747 
1748 	switch (event->event) {
1749 	case RDMA_CM_EVENT_CONNECT_REQUEST:
1750 		ret = nvmet_rdma_queue_connect(cm_id, event);
1751 		break;
1752 	case RDMA_CM_EVENT_ESTABLISHED:
1753 		nvmet_rdma_queue_established(queue);
1754 		break;
1755 	case RDMA_CM_EVENT_ADDR_CHANGE:
1756 		if (!queue) {
1757 			struct nvmet_rdma_port *port = cm_id->context;
1758 
1759 			schedule_delayed_work(&port->repair_work, 0);
1760 			break;
1761 		}
1762 		fallthrough;
1763 	case RDMA_CM_EVENT_DISCONNECTED:
1764 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:
1765 		nvmet_rdma_queue_disconnect(queue);
1766 		break;
1767 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
1768 		ret = nvmet_rdma_device_removal(cm_id, queue);
1769 		break;
1770 	case RDMA_CM_EVENT_REJECTED:
1771 		pr_debug("Connection rejected: %s\n",
1772 			 rdma_reject_msg(cm_id, event->status));
1773 		fallthrough;
1774 	case RDMA_CM_EVENT_UNREACHABLE:
1775 	case RDMA_CM_EVENT_CONNECT_ERROR:
1776 		nvmet_rdma_queue_connect_fail(cm_id, queue);
1777 		break;
1778 	default:
1779 		pr_err("received unrecognized RDMA CM event %d\n",
1780 			event->event);
1781 		break;
1782 	}
1783 
1784 	return ret;
1785 }
1786 
1787 static void nvmet_rdma_delete_ctrl(struct nvmet_ctrl *ctrl)
1788 {
1789 	struct nvmet_rdma_queue *queue;
1790 
1791 restart:
1792 	mutex_lock(&nvmet_rdma_queue_mutex);
1793 	list_for_each_entry(queue, &nvmet_rdma_queue_list, queue_list) {
1794 		if (queue->nvme_sq.ctrl == ctrl) {
1795 			list_del_init(&queue->queue_list);
1796 			mutex_unlock(&nvmet_rdma_queue_mutex);
1797 
1798 			__nvmet_rdma_queue_disconnect(queue);
1799 			goto restart;
1800 		}
1801 	}
1802 	mutex_unlock(&nvmet_rdma_queue_mutex);
1803 }
1804 
1805 static void nvmet_rdma_disable_port(struct nvmet_rdma_port *port)
1806 {
1807 	struct rdma_cm_id *cm_id = xchg(&port->cm_id, NULL);
1808 
1809 	if (cm_id)
1810 		rdma_destroy_id(cm_id);
1811 }
1812 
1813 static int nvmet_rdma_enable_port(struct nvmet_rdma_port *port)
1814 {
1815 	struct sockaddr *addr = (struct sockaddr *)&port->addr;
1816 	struct rdma_cm_id *cm_id;
1817 	int ret;
1818 
1819 	cm_id = rdma_create_id(&init_net, nvmet_rdma_cm_handler, port,
1820 			RDMA_PS_TCP, IB_QPT_RC);
1821 	if (IS_ERR(cm_id)) {
1822 		pr_err("CM ID creation failed\n");
1823 		return PTR_ERR(cm_id);
1824 	}
1825 
1826 	/*
1827 	 * Allow both IPv4 and IPv6 sockets to bind a single port
1828 	 * at the same time.
1829 	 */
1830 	ret = rdma_set_afonly(cm_id, 1);
1831 	if (ret) {
1832 		pr_err("rdma_set_afonly failed (%d)\n", ret);
1833 		goto out_destroy_id;
1834 	}
1835 
1836 	ret = rdma_bind_addr(cm_id, addr);
1837 	if (ret) {
1838 		pr_err("binding CM ID to %pISpcs failed (%d)\n", addr, ret);
1839 		goto out_destroy_id;
1840 	}
1841 
1842 	ret = rdma_listen(cm_id, 128);
1843 	if (ret) {
1844 		pr_err("listening to %pISpcs failed (%d)\n", addr, ret);
1845 		goto out_destroy_id;
1846 	}
1847 
1848 	if (port->nport->pi_enable &&
1849 	    !(cm_id->device->attrs.device_cap_flags &
1850 	      IB_DEVICE_INTEGRITY_HANDOVER)) {
1851 		pr_err("T10-PI is not supported for %pISpcs\n", addr);
1852 		ret = -EINVAL;
1853 		goto out_destroy_id;
1854 	}
1855 
1856 	port->cm_id = cm_id;
1857 	return 0;
1858 
1859 out_destroy_id:
1860 	rdma_destroy_id(cm_id);
1861 	return ret;
1862 }
1863 
1864 static void nvmet_rdma_repair_port_work(struct work_struct *w)
1865 {
1866 	struct nvmet_rdma_port *port = container_of(to_delayed_work(w),
1867 			struct nvmet_rdma_port, repair_work);
1868 	int ret;
1869 
1870 	nvmet_rdma_disable_port(port);
1871 	ret = nvmet_rdma_enable_port(port);
1872 	if (ret)
1873 		schedule_delayed_work(&port->repair_work, 5 * HZ);
1874 }
1875 
1876 static int nvmet_rdma_add_port(struct nvmet_port *nport)
1877 {
1878 	struct nvmet_rdma_port *port;
1879 	__kernel_sa_family_t af;
1880 	int ret;
1881 
1882 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1883 	if (!port)
1884 		return -ENOMEM;
1885 
1886 	nport->priv = port;
1887 	port->nport = nport;
1888 	INIT_DELAYED_WORK(&port->repair_work, nvmet_rdma_repair_port_work);
1889 
1890 	switch (nport->disc_addr.adrfam) {
1891 	case NVMF_ADDR_FAMILY_IP4:
1892 		af = AF_INET;
1893 		break;
1894 	case NVMF_ADDR_FAMILY_IP6:
1895 		af = AF_INET6;
1896 		break;
1897 	default:
1898 		pr_err("address family %d not supported\n",
1899 			nport->disc_addr.adrfam);
1900 		ret = -EINVAL;
1901 		goto out_free_port;
1902 	}
1903 
1904 	if (nport->inline_data_size < 0) {
1905 		nport->inline_data_size = NVMET_RDMA_DEFAULT_INLINE_DATA_SIZE;
1906 	} else if (nport->inline_data_size > NVMET_RDMA_MAX_INLINE_DATA_SIZE) {
1907 		pr_warn("inline_data_size %u is too large, reducing to %u\n",
1908 			nport->inline_data_size,
1909 			NVMET_RDMA_MAX_INLINE_DATA_SIZE);
1910 		nport->inline_data_size = NVMET_RDMA_MAX_INLINE_DATA_SIZE;
1911 	}
1912 
1913 	ret = inet_pton_with_scope(&init_net, af, nport->disc_addr.traddr,
1914 			nport->disc_addr.trsvcid, &port->addr);
1915 	if (ret) {
1916 		pr_err("malformed ip/port passed: %s:%s\n",
1917 			nport->disc_addr.traddr, nport->disc_addr.trsvcid);
1918 		goto out_free_port;
1919 	}
1920 
1921 	ret = nvmet_rdma_enable_port(port);
1922 	if (ret)
1923 		goto out_free_port;
1924 
1925 	pr_info("enabling port %d (%pISpcs)\n",
1926 		le16_to_cpu(nport->disc_addr.portid),
1927 		(struct sockaddr *)&port->addr);
1928 
1929 	return 0;
1930 
1931 out_free_port:
1932 	kfree(port);
1933 	return ret;
1934 }
1935 
1936 static void nvmet_rdma_remove_port(struct nvmet_port *nport)
1937 {
1938 	struct nvmet_rdma_port *port = nport->priv;
1939 
1940 	cancel_delayed_work_sync(&port->repair_work);
1941 	nvmet_rdma_disable_port(port);
1942 	kfree(port);
1943 }
1944 
1945 static void nvmet_rdma_disc_port_addr(struct nvmet_req *req,
1946 		struct nvmet_port *nport, char *traddr)
1947 {
1948 	struct nvmet_rdma_port *port = nport->priv;
1949 	struct rdma_cm_id *cm_id = port->cm_id;
1950 
1951 	if (inet_addr_is_any((struct sockaddr *)&cm_id->route.addr.src_addr)) {
1952 		struct nvmet_rdma_rsp *rsp =
1953 			container_of(req, struct nvmet_rdma_rsp, req);
1954 		struct rdma_cm_id *req_cm_id = rsp->queue->cm_id;
1955 		struct sockaddr *addr = (void *)&req_cm_id->route.addr.src_addr;
1956 
1957 		sprintf(traddr, "%pISc", addr);
1958 	} else {
1959 		memcpy(traddr, nport->disc_addr.traddr, NVMF_TRADDR_SIZE);
1960 	}
1961 }
1962 
1963 static u8 nvmet_rdma_get_mdts(const struct nvmet_ctrl *ctrl)
1964 {
1965 	if (ctrl->pi_support)
1966 		return NVMET_RDMA_MAX_METADATA_MDTS;
1967 	return NVMET_RDMA_MAX_MDTS;
1968 }
1969 
1970 static const struct nvmet_fabrics_ops nvmet_rdma_ops = {
1971 	.owner			= THIS_MODULE,
1972 	.type			= NVMF_TRTYPE_RDMA,
1973 	.msdbd			= 1,
1974 	.flags			= NVMF_KEYED_SGLS | NVMF_METADATA_SUPPORTED,
1975 	.add_port		= nvmet_rdma_add_port,
1976 	.remove_port		= nvmet_rdma_remove_port,
1977 	.queue_response		= nvmet_rdma_queue_response,
1978 	.delete_ctrl		= nvmet_rdma_delete_ctrl,
1979 	.disc_traddr		= nvmet_rdma_disc_port_addr,
1980 	.get_mdts		= nvmet_rdma_get_mdts,
1981 };
1982 
1983 static void nvmet_rdma_remove_one(struct ib_device *ib_device, void *client_data)
1984 {
1985 	struct nvmet_rdma_queue *queue, *tmp;
1986 	struct nvmet_rdma_device *ndev;
1987 	bool found = false;
1988 
1989 	mutex_lock(&device_list_mutex);
1990 	list_for_each_entry(ndev, &device_list, entry) {
1991 		if (ndev->device == ib_device) {
1992 			found = true;
1993 			break;
1994 		}
1995 	}
1996 	mutex_unlock(&device_list_mutex);
1997 
1998 	if (!found)
1999 		return;
2000 
2001 	/*
2002 	 * IB Device that is used by nvmet controllers is being removed,
2003 	 * delete all queues using this device.
2004 	 */
2005 	mutex_lock(&nvmet_rdma_queue_mutex);
2006 	list_for_each_entry_safe(queue, tmp, &nvmet_rdma_queue_list,
2007 				 queue_list) {
2008 		if (queue->dev->device != ib_device)
2009 			continue;
2010 
2011 		pr_info("Removing queue %d\n", queue->idx);
2012 		list_del_init(&queue->queue_list);
2013 		__nvmet_rdma_queue_disconnect(queue);
2014 	}
2015 	mutex_unlock(&nvmet_rdma_queue_mutex);
2016 
2017 	flush_scheduled_work();
2018 }
2019 
2020 static struct ib_client nvmet_rdma_ib_client = {
2021 	.name   = "nvmet_rdma",
2022 	.remove = nvmet_rdma_remove_one
2023 };
2024 
2025 static int __init nvmet_rdma_init(void)
2026 {
2027 	int ret;
2028 
2029 	ret = ib_register_client(&nvmet_rdma_ib_client);
2030 	if (ret)
2031 		return ret;
2032 
2033 	ret = nvmet_register_transport(&nvmet_rdma_ops);
2034 	if (ret)
2035 		goto err_ib_client;
2036 
2037 	return 0;
2038 
2039 err_ib_client:
2040 	ib_unregister_client(&nvmet_rdma_ib_client);
2041 	return ret;
2042 }
2043 
2044 static void __exit nvmet_rdma_exit(void)
2045 {
2046 	nvmet_unregister_transport(&nvmet_rdma_ops);
2047 	ib_unregister_client(&nvmet_rdma_ib_client);
2048 	WARN_ON_ONCE(!list_empty(&nvmet_rdma_queue_list));
2049 	ida_destroy(&nvmet_rdma_queue_ida);
2050 }
2051 
2052 module_init(nvmet_rdma_init);
2053 module_exit(nvmet_rdma_exit);
2054 
2055 MODULE_LICENSE("GPL v2");
2056 MODULE_ALIAS("nvmet-transport-1"); /* 1 == NVMF_TRTYPE_RDMA */
2057