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