xref: /openbmc/linux/drivers/infiniband/ulp/rtrs/rtrs.c (revision 1f012283)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * RDMA Transport Layer
4  *
5  * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
6  * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
7  * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
8  */
9 #undef pr_fmt
10 #define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt
11 
12 #include <linux/module.h>
13 #include <linux/inet.h>
14 
15 #include "rtrs-pri.h"
16 #include "rtrs-log.h"
17 
18 MODULE_DESCRIPTION("RDMA Transport Core");
19 MODULE_LICENSE("GPL");
20 
21 struct rtrs_iu *rtrs_iu_alloc(u32 iu_num, size_t size, gfp_t gfp_mask,
22 			      struct ib_device *dma_dev,
23 			      enum dma_data_direction dir,
24 			      void (*done)(struct ib_cq *cq, struct ib_wc *wc))
25 {
26 	struct rtrs_iu *ius, *iu;
27 	int i;
28 
29 	ius = kcalloc(iu_num, sizeof(*ius), gfp_mask);
30 	if (!ius)
31 		return NULL;
32 	for (i = 0; i < iu_num; i++) {
33 		iu = &ius[i];
34 		iu->direction = dir;
35 		iu->buf = kzalloc(size, gfp_mask);
36 		if (!iu->buf)
37 			goto err;
38 
39 		iu->dma_addr = ib_dma_map_single(dma_dev, iu->buf, size, dir);
40 		if (ib_dma_mapping_error(dma_dev, iu->dma_addr))
41 			goto err;
42 
43 		iu->cqe.done  = done;
44 		iu->size      = size;
45 	}
46 	return ius;
47 err:
48 	rtrs_iu_free(ius, dma_dev, i);
49 	return NULL;
50 }
51 EXPORT_SYMBOL_GPL(rtrs_iu_alloc);
52 
53 void rtrs_iu_free(struct rtrs_iu *ius, struct ib_device *ibdev, u32 queue_num)
54 {
55 	struct rtrs_iu *iu;
56 	int i;
57 
58 	if (!ius)
59 		return;
60 
61 	for (i = 0; i < queue_num; i++) {
62 		iu = &ius[i];
63 		ib_dma_unmap_single(ibdev, iu->dma_addr, iu->size, iu->direction);
64 		kfree(iu->buf);
65 	}
66 	kfree(ius);
67 }
68 EXPORT_SYMBOL_GPL(rtrs_iu_free);
69 
70 int rtrs_iu_post_recv(struct rtrs_con *con, struct rtrs_iu *iu)
71 {
72 	struct rtrs_sess *sess = con->sess;
73 	struct ib_recv_wr wr;
74 	struct ib_sge list;
75 
76 	list.addr   = iu->dma_addr;
77 	list.length = iu->size;
78 	list.lkey   = sess->dev->ib_pd->local_dma_lkey;
79 
80 	if (list.length == 0) {
81 		rtrs_wrn(con->sess,
82 			  "Posting receive work request failed, sg list is empty\n");
83 		return -EINVAL;
84 	}
85 	wr = (struct ib_recv_wr) {
86 		.wr_cqe  = &iu->cqe,
87 		.sg_list = &list,
88 		.num_sge = 1,
89 	};
90 
91 	return ib_post_recv(con->qp, &wr, NULL);
92 }
93 EXPORT_SYMBOL_GPL(rtrs_iu_post_recv);
94 
95 int rtrs_post_recv_empty(struct rtrs_con *con, struct ib_cqe *cqe)
96 {
97 	struct ib_recv_wr wr;
98 
99 	wr = (struct ib_recv_wr) {
100 		.wr_cqe  = cqe,
101 	};
102 
103 	return ib_post_recv(con->qp, &wr, NULL);
104 }
105 EXPORT_SYMBOL_GPL(rtrs_post_recv_empty);
106 
107 static int rtrs_post_send(struct ib_qp *qp, struct ib_send_wr *head,
108 			  struct ib_send_wr *wr, struct ib_send_wr *tail)
109 {
110 	if (head) {
111 		struct ib_send_wr *next = head;
112 
113 		while (next->next)
114 			next = next->next;
115 		next->next = wr;
116 	} else {
117 		head = wr;
118 	}
119 
120 	if (tail)
121 		wr->next = tail;
122 
123 	return ib_post_send(qp, head, NULL);
124 }
125 
126 int rtrs_iu_post_send(struct rtrs_con *con, struct rtrs_iu *iu, size_t size,
127 		       struct ib_send_wr *head)
128 {
129 	struct rtrs_sess *sess = con->sess;
130 	struct ib_send_wr wr;
131 	struct ib_sge list;
132 
133 	if (WARN_ON(size == 0))
134 		return -EINVAL;
135 
136 	list.addr   = iu->dma_addr;
137 	list.length = size;
138 	list.lkey   = sess->dev->ib_pd->local_dma_lkey;
139 
140 	wr = (struct ib_send_wr) {
141 		.wr_cqe     = &iu->cqe,
142 		.sg_list    = &list,
143 		.num_sge    = 1,
144 		.opcode     = IB_WR_SEND,
145 		.send_flags = IB_SEND_SIGNALED,
146 	};
147 
148 	return rtrs_post_send(con->qp, head, &wr, NULL);
149 }
150 EXPORT_SYMBOL_GPL(rtrs_iu_post_send);
151 
152 int rtrs_iu_post_rdma_write_imm(struct rtrs_con *con, struct rtrs_iu *iu,
153 				struct ib_sge *sge, unsigned int num_sge,
154 				u32 rkey, u64 rdma_addr, u32 imm_data,
155 				enum ib_send_flags flags,
156 				struct ib_send_wr *head,
157 				struct ib_send_wr *tail)
158 {
159 	struct ib_rdma_wr wr;
160 	int i;
161 
162 	wr = (struct ib_rdma_wr) {
163 		.wr.wr_cqe	  = &iu->cqe,
164 		.wr.sg_list	  = sge,
165 		.wr.num_sge	  = num_sge,
166 		.rkey		  = rkey,
167 		.remote_addr	  = rdma_addr,
168 		.wr.opcode	  = IB_WR_RDMA_WRITE_WITH_IMM,
169 		.wr.ex.imm_data = cpu_to_be32(imm_data),
170 		.wr.send_flags  = flags,
171 	};
172 
173 	/*
174 	 * If one of the sges has 0 size, the operation will fail with a
175 	 * length error
176 	 */
177 	for (i = 0; i < num_sge; i++)
178 		if (WARN_ON(sge[i].length == 0))
179 			return -EINVAL;
180 
181 	return rtrs_post_send(con->qp, head, &wr.wr, tail);
182 }
183 EXPORT_SYMBOL_GPL(rtrs_iu_post_rdma_write_imm);
184 
185 static int rtrs_post_rdma_write_imm_empty(struct rtrs_con *con,
186 					  struct ib_cqe *cqe,
187 					  u32 imm_data,
188 					  struct ib_send_wr *head)
189 {
190 	struct ib_rdma_wr wr;
191 	struct rtrs_sess *sess = con->sess;
192 	enum ib_send_flags sflags;
193 
194 	atomic_dec_if_positive(&con->sq_wr_avail);
195 	sflags = (atomic_inc_return(&con->wr_cnt) % sess->signal_interval) ?
196 		0 : IB_SEND_SIGNALED;
197 
198 	wr = (struct ib_rdma_wr) {
199 		.wr.wr_cqe	= cqe,
200 		.wr.send_flags	= sflags,
201 		.wr.opcode	= IB_WR_RDMA_WRITE_WITH_IMM,
202 		.wr.ex.imm_data	= cpu_to_be32(imm_data),
203 	};
204 
205 	return rtrs_post_send(con->qp, head, &wr.wr, NULL);
206 }
207 
208 static void qp_event_handler(struct ib_event *ev, void *ctx)
209 {
210 	struct rtrs_con *con = ctx;
211 
212 	switch (ev->event) {
213 	case IB_EVENT_COMM_EST:
214 		rtrs_info(con->sess, "QP event %s (%d) received\n",
215 			   ib_event_msg(ev->event), ev->event);
216 		rdma_notify(con->cm_id, IB_EVENT_COMM_EST);
217 		break;
218 	default:
219 		rtrs_info(con->sess, "Unhandled QP event %s (%d) received\n",
220 			   ib_event_msg(ev->event), ev->event);
221 		break;
222 	}
223 }
224 
225 static bool is_pollqueue(struct rtrs_con *con)
226 {
227 	return con->cid >= con->sess->irq_con_num;
228 }
229 
230 static int create_cq(struct rtrs_con *con, int cq_vector, int nr_cqe,
231 		     enum ib_poll_context poll_ctx)
232 {
233 	struct rdma_cm_id *cm_id = con->cm_id;
234 	struct ib_cq *cq;
235 
236 	if (is_pollqueue(con))
237 		cq = ib_alloc_cq(cm_id->device, con, nr_cqe, cq_vector,
238 				 poll_ctx);
239 	else
240 		cq = ib_cq_pool_get(cm_id->device, nr_cqe, cq_vector, poll_ctx);
241 
242 	if (IS_ERR(cq)) {
243 		rtrs_err(con->sess, "Creating completion queue failed, errno: %ld\n",
244 			  PTR_ERR(cq));
245 		return PTR_ERR(cq);
246 	}
247 	con->cq = cq;
248 	con->nr_cqe = nr_cqe;
249 
250 	return 0;
251 }
252 
253 static int create_qp(struct rtrs_con *con, struct ib_pd *pd,
254 		     u32 max_send_wr, u32 max_recv_wr, u32 max_sge)
255 {
256 	struct ib_qp_init_attr init_attr = {NULL};
257 	struct rdma_cm_id *cm_id = con->cm_id;
258 	int ret;
259 
260 	init_attr.cap.max_send_wr = max_send_wr;
261 	init_attr.cap.max_recv_wr = max_recv_wr;
262 	init_attr.cap.max_recv_sge = 1;
263 	init_attr.event_handler = qp_event_handler;
264 	init_attr.qp_context = con;
265 	init_attr.cap.max_send_sge = max_sge;
266 
267 	init_attr.qp_type = IB_QPT_RC;
268 	init_attr.send_cq = con->cq;
269 	init_attr.recv_cq = con->cq;
270 	init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
271 
272 	ret = rdma_create_qp(cm_id, pd, &init_attr);
273 	if (ret) {
274 		rtrs_err(con->sess, "Creating QP failed, err: %d\n", ret);
275 		return ret;
276 	}
277 	con->qp = cm_id->qp;
278 
279 	return ret;
280 }
281 
282 static void destroy_cq(struct rtrs_con *con)
283 {
284 	if (con->cq) {
285 		if (is_pollqueue(con))
286 			ib_free_cq(con->cq);
287 		else
288 			ib_cq_pool_put(con->cq, con->nr_cqe);
289 	}
290 	con->cq = NULL;
291 }
292 
293 int rtrs_cq_qp_create(struct rtrs_sess *sess, struct rtrs_con *con,
294 		       u32 max_send_sge, int cq_vector, int nr_cqe,
295 		       u32 max_send_wr, u32 max_recv_wr,
296 		       enum ib_poll_context poll_ctx)
297 {
298 	int err;
299 
300 	err = create_cq(con, cq_vector, nr_cqe, poll_ctx);
301 	if (err)
302 		return err;
303 
304 	err = create_qp(con, sess->dev->ib_pd, max_send_wr, max_recv_wr,
305 			max_send_sge);
306 	if (err) {
307 		destroy_cq(con);
308 		return err;
309 	}
310 	con->sess = sess;
311 
312 	return 0;
313 }
314 EXPORT_SYMBOL_GPL(rtrs_cq_qp_create);
315 
316 void rtrs_cq_qp_destroy(struct rtrs_con *con)
317 {
318 	if (con->qp) {
319 		rdma_destroy_qp(con->cm_id);
320 		con->qp = NULL;
321 	}
322 	destroy_cq(con);
323 }
324 EXPORT_SYMBOL_GPL(rtrs_cq_qp_destroy);
325 
326 static void schedule_hb(struct rtrs_sess *sess)
327 {
328 	queue_delayed_work(sess->hb_wq, &sess->hb_dwork,
329 			   msecs_to_jiffies(sess->hb_interval_ms));
330 }
331 
332 void rtrs_send_hb_ack(struct rtrs_sess *sess)
333 {
334 	struct rtrs_con *usr_con = sess->con[0];
335 	u32 imm;
336 	int err;
337 
338 	imm = rtrs_to_imm(RTRS_HB_ACK_IMM, 0);
339 	err = rtrs_post_rdma_write_imm_empty(usr_con, sess->hb_cqe, imm,
340 					     NULL);
341 	if (err) {
342 		rtrs_err(sess, "send HB ACK failed, errno: %d\n", err);
343 		sess->hb_err_handler(usr_con);
344 		return;
345 	}
346 }
347 EXPORT_SYMBOL_GPL(rtrs_send_hb_ack);
348 
349 static void hb_work(struct work_struct *work)
350 {
351 	struct rtrs_con *usr_con;
352 	struct rtrs_sess *sess;
353 	u32 imm;
354 	int err;
355 
356 	sess = container_of(to_delayed_work(work), typeof(*sess), hb_dwork);
357 	usr_con = sess->con[0];
358 
359 	if (sess->hb_missed_cnt > sess->hb_missed_max) {
360 		rtrs_err(sess, "HB missed max reached.\n");
361 		sess->hb_err_handler(usr_con);
362 		return;
363 	}
364 	if (sess->hb_missed_cnt++) {
365 		/* Reschedule work without sending hb */
366 		schedule_hb(sess);
367 		return;
368 	}
369 
370 	sess->hb_last_sent = ktime_get();
371 
372 	imm = rtrs_to_imm(RTRS_HB_MSG_IMM, 0);
373 	err = rtrs_post_rdma_write_imm_empty(usr_con, sess->hb_cqe, imm,
374 					     NULL);
375 	if (err) {
376 		rtrs_err(sess, "HB send failed, errno: %d\n", err);
377 		sess->hb_err_handler(usr_con);
378 		return;
379 	}
380 
381 	schedule_hb(sess);
382 }
383 
384 void rtrs_init_hb(struct rtrs_sess *sess, struct ib_cqe *cqe,
385 		  unsigned int interval_ms, unsigned int missed_max,
386 		  void (*err_handler)(struct rtrs_con *con),
387 		  struct workqueue_struct *wq)
388 {
389 	sess->hb_cqe = cqe;
390 	sess->hb_interval_ms = interval_ms;
391 	sess->hb_err_handler = err_handler;
392 	sess->hb_wq = wq;
393 	sess->hb_missed_max = missed_max;
394 	sess->hb_missed_cnt = 0;
395 	INIT_DELAYED_WORK(&sess->hb_dwork, hb_work);
396 }
397 EXPORT_SYMBOL_GPL(rtrs_init_hb);
398 
399 void rtrs_start_hb(struct rtrs_sess *sess)
400 {
401 	schedule_hb(sess);
402 }
403 EXPORT_SYMBOL_GPL(rtrs_start_hb);
404 
405 void rtrs_stop_hb(struct rtrs_sess *sess)
406 {
407 	cancel_delayed_work_sync(&sess->hb_dwork);
408 	sess->hb_missed_cnt = 0;
409 }
410 EXPORT_SYMBOL_GPL(rtrs_stop_hb);
411 
412 static int rtrs_str_gid_to_sockaddr(const char *addr, size_t len,
413 				     short port, struct sockaddr_storage *dst)
414 {
415 	struct sockaddr_ib *dst_ib = (struct sockaddr_ib *)dst;
416 	int ret;
417 
418 	/*
419 	 * We can use some of the IPv6 functions since GID is a valid
420 	 * IPv6 address format
421 	 */
422 	ret = in6_pton(addr, len, dst_ib->sib_addr.sib_raw, '\0', NULL);
423 	if (ret == 0)
424 		return -EINVAL;
425 
426 	dst_ib->sib_family = AF_IB;
427 	/*
428 	 * Use the same TCP server port number as the IB service ID
429 	 * on the IB port space range
430 	 */
431 	dst_ib->sib_sid = cpu_to_be64(RDMA_IB_IP_PS_IB | port);
432 	dst_ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
433 	dst_ib->sib_pkey = cpu_to_be16(0xffff);
434 
435 	return 0;
436 }
437 
438 /**
439  * rtrs_str_to_sockaddr() - Convert rtrs address string to sockaddr
440  * @addr:	String representation of an addr (IPv4, IPv6 or IB GID):
441  *              - "ip:192.168.1.1"
442  *              - "ip:fe80::200:5aee:feaa:20a2"
443  *              - "gid:fe80::200:5aee:feaa:20a2"
444  * @len:        String address length
445  * @port:	Destination port
446  * @dst:	Destination sockaddr structure
447  *
448  * Returns 0 if conversion successful. Non-zero on error.
449  */
450 static int rtrs_str_to_sockaddr(const char *addr, size_t len,
451 				u16 port, struct sockaddr_storage *dst)
452 {
453 	if (strncmp(addr, "gid:", 4) == 0) {
454 		return rtrs_str_gid_to_sockaddr(addr + 4, len - 4, port, dst);
455 	} else if (strncmp(addr, "ip:", 3) == 0) {
456 		char port_str[8];
457 		char *cpy;
458 		int err;
459 
460 		snprintf(port_str, sizeof(port_str), "%u", port);
461 		cpy = kstrndup(addr + 3, len - 3, GFP_KERNEL);
462 		err = cpy ? inet_pton_with_scope(&init_net, AF_UNSPEC,
463 						 cpy, port_str, dst) : -ENOMEM;
464 		kfree(cpy);
465 
466 		return err;
467 	}
468 	return -EPROTONOSUPPORT;
469 }
470 
471 /**
472  * sockaddr_to_str() - convert sockaddr to a string.
473  * @addr:	the sockadddr structure to be converted.
474  * @buf:	string containing socket addr.
475  * @len:	string length.
476  *
477  * The return value is the number of characters written into buf not
478  * including the trailing '\0'. If len is == 0 the function returns 0..
479  */
480 int sockaddr_to_str(const struct sockaddr *addr, char *buf, size_t len)
481 {
482 
483 	switch (addr->sa_family) {
484 	case AF_IB:
485 		return scnprintf(buf, len, "gid:%pI6",
486 			&((struct sockaddr_ib *)addr)->sib_addr.sib_raw);
487 	case AF_INET:
488 		return scnprintf(buf, len, "ip:%pI4",
489 			&((struct sockaddr_in *)addr)->sin_addr);
490 	case AF_INET6:
491 		return scnprintf(buf, len, "ip:%pI6c",
492 			  &((struct sockaddr_in6 *)addr)->sin6_addr);
493 	}
494 	return scnprintf(buf, len, "<invalid address family>");
495 }
496 EXPORT_SYMBOL(sockaddr_to_str);
497 
498 /**
499  * rtrs_addr_to_str() - convert rtrs_addr to a string "src@dst"
500  * @addr:	the rtrs_addr structure to be converted
501  * @buf:	string containing source and destination addr of a path
502  *		separated by '@' I.e. "ip:1.1.1.1@ip:1.1.1.2"
503  *		"ip:1.1.1.1@ip:1.1.1.2".
504  * @len:	string length
505  *
506  * The return value is the number of characters written into buf not
507  * including the trailing '\0'.
508  */
509 int rtrs_addr_to_str(const struct rtrs_addr *addr, char *buf, size_t len)
510 {
511 	int cnt;
512 
513 	cnt = sockaddr_to_str((struct sockaddr *)addr->src,
514 			      buf, len);
515 	cnt += scnprintf(buf + cnt, len - cnt, "@");
516 	sockaddr_to_str((struct sockaddr *)addr->dst,
517 			buf + cnt, len - cnt);
518 	return cnt;
519 }
520 EXPORT_SYMBOL(rtrs_addr_to_str);
521 
522 /**
523  * rtrs_addr_to_sockaddr() - convert path string "src,dst" or "src@dst"
524  * to sockaddreses
525  * @str:	string containing source and destination addr of a path
526  *		separated by ',' or '@' I.e. "ip:1.1.1.1,ip:1.1.1.2" or
527  *		"ip:1.1.1.1@ip:1.1.1.2". If str contains only one address it's
528  *		considered to be destination.
529  * @len:	string length
530  * @port:	Destination port number.
531  * @addr:	will be set to the source/destination address or to NULL
532  *		if str doesn't contain any source address.
533  *
534  * Returns zero if conversion successful. Non-zero otherwise.
535  */
536 int rtrs_addr_to_sockaddr(const char *str, size_t len, u16 port,
537 			  struct rtrs_addr *addr)
538 {
539 	const char *d;
540 
541 	d = strchr(str, ',');
542 	if (!d)
543 		d = strchr(str, '@');
544 	if (d) {
545 		if (rtrs_str_to_sockaddr(str, d - str, 0, addr->src))
546 			return -EINVAL;
547 		d += 1;
548 		len -= d - str;
549 		str  = d;
550 
551 	} else {
552 		addr->src = NULL;
553 	}
554 	return rtrs_str_to_sockaddr(str, len, port, addr->dst);
555 }
556 EXPORT_SYMBOL(rtrs_addr_to_sockaddr);
557 
558 void rtrs_rdma_dev_pd_init(enum ib_pd_flags pd_flags,
559 			    struct rtrs_rdma_dev_pd *pool)
560 {
561 	WARN_ON(pool->ops && (!pool->ops->alloc ^ !pool->ops->free));
562 	INIT_LIST_HEAD(&pool->list);
563 	mutex_init(&pool->mutex);
564 	pool->pd_flags = pd_flags;
565 }
566 EXPORT_SYMBOL(rtrs_rdma_dev_pd_init);
567 
568 void rtrs_rdma_dev_pd_deinit(struct rtrs_rdma_dev_pd *pool)
569 {
570 	mutex_destroy(&pool->mutex);
571 	WARN_ON(!list_empty(&pool->list));
572 }
573 EXPORT_SYMBOL(rtrs_rdma_dev_pd_deinit);
574 
575 static void dev_free(struct kref *ref)
576 {
577 	struct rtrs_rdma_dev_pd *pool;
578 	struct rtrs_ib_dev *dev;
579 
580 	dev = container_of(ref, typeof(*dev), ref);
581 	pool = dev->pool;
582 
583 	mutex_lock(&pool->mutex);
584 	list_del(&dev->entry);
585 	mutex_unlock(&pool->mutex);
586 
587 	if (pool->ops && pool->ops->deinit)
588 		pool->ops->deinit(dev);
589 
590 	ib_dealloc_pd(dev->ib_pd);
591 
592 	if (pool->ops && pool->ops->free)
593 		pool->ops->free(dev);
594 	else
595 		kfree(dev);
596 }
597 
598 int rtrs_ib_dev_put(struct rtrs_ib_dev *dev)
599 {
600 	return kref_put(&dev->ref, dev_free);
601 }
602 EXPORT_SYMBOL(rtrs_ib_dev_put);
603 
604 static int rtrs_ib_dev_get(struct rtrs_ib_dev *dev)
605 {
606 	return kref_get_unless_zero(&dev->ref);
607 }
608 
609 struct rtrs_ib_dev *
610 rtrs_ib_dev_find_or_add(struct ib_device *ib_dev,
611 			 struct rtrs_rdma_dev_pd *pool)
612 {
613 	struct rtrs_ib_dev *dev;
614 
615 	mutex_lock(&pool->mutex);
616 	list_for_each_entry(dev, &pool->list, entry) {
617 		if (dev->ib_dev->node_guid == ib_dev->node_guid &&
618 		    rtrs_ib_dev_get(dev))
619 			goto out_unlock;
620 	}
621 	mutex_unlock(&pool->mutex);
622 	if (pool->ops && pool->ops->alloc)
623 		dev = pool->ops->alloc();
624 	else
625 		dev = kzalloc(sizeof(*dev), GFP_KERNEL);
626 	if (IS_ERR_OR_NULL(dev))
627 		goto out_err;
628 
629 	kref_init(&dev->ref);
630 	dev->pool = pool;
631 	dev->ib_dev = ib_dev;
632 	dev->ib_pd = ib_alloc_pd(ib_dev, pool->pd_flags);
633 	if (IS_ERR(dev->ib_pd))
634 		goto out_free_dev;
635 
636 	if (pool->ops && pool->ops->init && pool->ops->init(dev))
637 		goto out_free_pd;
638 
639 	mutex_lock(&pool->mutex);
640 	list_add(&dev->entry, &pool->list);
641 out_unlock:
642 	mutex_unlock(&pool->mutex);
643 	return dev;
644 
645 out_free_pd:
646 	ib_dealloc_pd(dev->ib_pd);
647 out_free_dev:
648 	if (pool->ops && pool->ops->free)
649 		pool->ops->free(dev);
650 	else
651 		kfree(dev);
652 out_err:
653 	return NULL;
654 }
655 EXPORT_SYMBOL(rtrs_ib_dev_find_or_add);
656