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