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