xref: /linux/drivers/infiniband/core/cma.c (revision dea3165f989b259bc1293d9dfc50053223f5f4b2)
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3  * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
4  * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
5  * Copyright (c) 1999-2019, Mellanox Technologies, Inc. All rights reserved.
6  * Copyright (c) 2005-2006 Intel Corporation.  All rights reserved.
7  */
8 
9 #include <linux/completion.h>
10 #include <linux/in.h>
11 #include <linux/in6.h>
12 #include <linux/mutex.h>
13 #include <linux/random.h>
14 #include <linux/rbtree.h>
15 #include <linux/igmp.h>
16 #include <linux/xarray.h>
17 #include <linux/inetdevice.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
20 #include <net/route.h>
21 
22 #include <net/net_namespace.h>
23 #include <net/netns/generic.h>
24 #include <net/netevent.h>
25 #include <net/tcp.h>
26 #include <net/ipv6.h>
27 #include <net/ip_fib.h>
28 #include <net/ip6_route.h>
29 
30 #include <rdma/rdma_cm.h>
31 #include <rdma/rdma_cm_ib.h>
32 #include <rdma/rdma_netlink.h>
33 #include <rdma/ib.h>
34 #include <rdma/ib_cache.h>
35 #include <rdma/ib_cm.h>
36 #include <rdma/ib_sa.h>
37 #include <rdma/iw_cm.h>
38 
39 #include "core_priv.h"
40 #include "cma_priv.h"
41 #include "cma_trace.h"
42 
43 MODULE_AUTHOR("Sean Hefty");
44 MODULE_DESCRIPTION("Generic RDMA CM Agent");
45 MODULE_LICENSE("Dual BSD/GPL");
46 
47 #define CMA_CM_RESPONSE_TIMEOUT 20
48 #define CMA_MAX_CM_RETRIES 15
49 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
50 #define CMA_IBOE_PACKET_LIFETIME 16
51 #define CMA_PREFERRED_ROCE_GID_TYPE IB_GID_TYPE_ROCE_UDP_ENCAP
52 
53 static const char * const cma_events[] = {
54 	[RDMA_CM_EVENT_ADDR_RESOLVED]	 = "address resolved",
55 	[RDMA_CM_EVENT_ADDR_ERROR]	 = "address error",
56 	[RDMA_CM_EVENT_ROUTE_RESOLVED]	 = "route resolved ",
57 	[RDMA_CM_EVENT_ROUTE_ERROR]	 = "route error",
58 	[RDMA_CM_EVENT_CONNECT_REQUEST]	 = "connect request",
59 	[RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response",
60 	[RDMA_CM_EVENT_CONNECT_ERROR]	 = "connect error",
61 	[RDMA_CM_EVENT_UNREACHABLE]	 = "unreachable",
62 	[RDMA_CM_EVENT_REJECTED]	 = "rejected",
63 	[RDMA_CM_EVENT_ESTABLISHED]	 = "established",
64 	[RDMA_CM_EVENT_DISCONNECTED]	 = "disconnected",
65 	[RDMA_CM_EVENT_DEVICE_REMOVAL]	 = "device removal",
66 	[RDMA_CM_EVENT_MULTICAST_JOIN]	 = "multicast join",
67 	[RDMA_CM_EVENT_MULTICAST_ERROR]	 = "multicast error",
68 	[RDMA_CM_EVENT_ADDR_CHANGE]	 = "address change",
69 	[RDMA_CM_EVENT_TIMEWAIT_EXIT]	 = "timewait exit",
70 };
71 
72 static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid,
73 			      enum ib_gid_type gid_type);
74 
rdma_event_msg(enum rdma_cm_event_type event)75 const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event)
76 {
77 	size_t index = event;
78 
79 	return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ?
80 			cma_events[index] : "unrecognized event";
81 }
82 EXPORT_SYMBOL(rdma_event_msg);
83 
rdma_reject_msg(struct rdma_cm_id * id,int reason)84 const char *__attribute_const__ rdma_reject_msg(struct rdma_cm_id *id,
85 						int reason)
86 {
87 	if (rdma_ib_or_roce(id->device, id->port_num))
88 		return ibcm_reject_msg(reason);
89 
90 	if (rdma_protocol_iwarp(id->device, id->port_num))
91 		return iwcm_reject_msg(reason);
92 
93 	WARN_ON_ONCE(1);
94 	return "unrecognized transport";
95 }
96 EXPORT_SYMBOL(rdma_reject_msg);
97 
98 /**
99  * rdma_is_consumer_reject - return true if the consumer rejected the connect
100  *                           request.
101  * @id: Communication identifier that received the REJECT event.
102  * @reason: Value returned in the REJECT event status field.
103  */
rdma_is_consumer_reject(struct rdma_cm_id * id,int reason)104 static bool rdma_is_consumer_reject(struct rdma_cm_id *id, int reason)
105 {
106 	if (rdma_ib_or_roce(id->device, id->port_num))
107 		return reason == IB_CM_REJ_CONSUMER_DEFINED;
108 
109 	if (rdma_protocol_iwarp(id->device, id->port_num))
110 		return reason == -ECONNREFUSED;
111 
112 	WARN_ON_ONCE(1);
113 	return false;
114 }
115 
rdma_consumer_reject_data(struct rdma_cm_id * id,struct rdma_cm_event * ev,u8 * data_len)116 const void *rdma_consumer_reject_data(struct rdma_cm_id *id,
117 				      struct rdma_cm_event *ev, u8 *data_len)
118 {
119 	const void *p;
120 
121 	if (rdma_is_consumer_reject(id, ev->status)) {
122 		*data_len = ev->param.conn.private_data_len;
123 		p = ev->param.conn.private_data;
124 	} else {
125 		*data_len = 0;
126 		p = NULL;
127 	}
128 	return p;
129 }
130 EXPORT_SYMBOL(rdma_consumer_reject_data);
131 
132 /**
133  * rdma_iw_cm_id() - return the iw_cm_id pointer for this cm_id.
134  * @id: Communication Identifier
135  */
rdma_iw_cm_id(struct rdma_cm_id * id)136 struct iw_cm_id *rdma_iw_cm_id(struct rdma_cm_id *id)
137 {
138 	struct rdma_id_private *id_priv;
139 
140 	id_priv = container_of(id, struct rdma_id_private, id);
141 	if (id->device->node_type == RDMA_NODE_RNIC)
142 		return id_priv->cm_id.iw;
143 	return NULL;
144 }
145 EXPORT_SYMBOL(rdma_iw_cm_id);
146 
147 /**
148  * rdma_res_to_id() - return the rdma_cm_id pointer for this restrack.
149  * @res: rdma resource tracking entry pointer
150  */
rdma_res_to_id(struct rdma_restrack_entry * res)151 struct rdma_cm_id *rdma_res_to_id(struct rdma_restrack_entry *res)
152 {
153 	struct rdma_id_private *id_priv =
154 		container_of(res, struct rdma_id_private, res);
155 
156 	return &id_priv->id;
157 }
158 EXPORT_SYMBOL(rdma_res_to_id);
159 
160 static int cma_add_one(struct ib_device *device);
161 static void cma_remove_one(struct ib_device *device, void *client_data);
162 
163 static struct ib_client cma_client = {
164 	.name   = "cma",
165 	.add    = cma_add_one,
166 	.remove = cma_remove_one
167 };
168 
169 static struct ib_sa_client sa_client;
170 static LIST_HEAD(dev_list);
171 static LIST_HEAD(listen_any_list);
172 static DEFINE_MUTEX(lock);
173 static struct rb_root id_table = RB_ROOT;
174 /* Serialize operations of id_table tree */
175 static DEFINE_SPINLOCK(id_table_lock);
176 static struct workqueue_struct *cma_wq;
177 static unsigned int cma_pernet_id;
178 
179 struct cma_pernet {
180 	struct xarray tcp_ps;
181 	struct xarray udp_ps;
182 	struct xarray ipoib_ps;
183 	struct xarray ib_ps;
184 };
185 
cma_pernet(struct net * net)186 static struct cma_pernet *cma_pernet(struct net *net)
187 {
188 	return net_generic(net, cma_pernet_id);
189 }
190 
191 static
cma_pernet_xa(struct net * net,enum rdma_ucm_port_space ps)192 struct xarray *cma_pernet_xa(struct net *net, enum rdma_ucm_port_space ps)
193 {
194 	struct cma_pernet *pernet = cma_pernet(net);
195 
196 	switch (ps) {
197 	case RDMA_PS_TCP:
198 		return &pernet->tcp_ps;
199 	case RDMA_PS_UDP:
200 		return &pernet->udp_ps;
201 	case RDMA_PS_IPOIB:
202 		return &pernet->ipoib_ps;
203 	case RDMA_PS_IB:
204 		return &pernet->ib_ps;
205 	default:
206 		return NULL;
207 	}
208 }
209 
210 struct id_table_entry {
211 	struct list_head id_list;
212 	struct rb_node rb_node;
213 };
214 
215 struct cma_device {
216 	struct list_head	list;
217 	struct ib_device	*device;
218 	struct completion	comp;
219 	refcount_t refcount;
220 	struct list_head	id_list;
221 	enum ib_gid_type	*default_gid_type;
222 	u8			*default_roce_tos;
223 };
224 
225 struct rdma_bind_list {
226 	enum rdma_ucm_port_space ps;
227 	struct hlist_head	owners;
228 	unsigned short		port;
229 };
230 
cma_ps_alloc(struct net * net,enum rdma_ucm_port_space ps,struct rdma_bind_list * bind_list,int snum)231 static int cma_ps_alloc(struct net *net, enum rdma_ucm_port_space ps,
232 			struct rdma_bind_list *bind_list, int snum)
233 {
234 	struct xarray *xa = cma_pernet_xa(net, ps);
235 
236 	return xa_insert(xa, snum, bind_list, GFP_KERNEL);
237 }
238 
cma_ps_find(struct net * net,enum rdma_ucm_port_space ps,int snum)239 static struct rdma_bind_list *cma_ps_find(struct net *net,
240 					  enum rdma_ucm_port_space ps, int snum)
241 {
242 	struct xarray *xa = cma_pernet_xa(net, ps);
243 
244 	return xa_load(xa, snum);
245 }
246 
cma_ps_remove(struct net * net,enum rdma_ucm_port_space ps,int snum)247 static void cma_ps_remove(struct net *net, enum rdma_ucm_port_space ps,
248 			  int snum)
249 {
250 	struct xarray *xa = cma_pernet_xa(net, ps);
251 
252 	xa_erase(xa, snum);
253 }
254 
255 enum {
256 	CMA_OPTION_AFONLY,
257 };
258 
cma_dev_get(struct cma_device * cma_dev)259 void cma_dev_get(struct cma_device *cma_dev)
260 {
261 	refcount_inc(&cma_dev->refcount);
262 }
263 
cma_dev_put(struct cma_device * cma_dev)264 void cma_dev_put(struct cma_device *cma_dev)
265 {
266 	if (refcount_dec_and_test(&cma_dev->refcount))
267 		complete(&cma_dev->comp);
268 }
269 
cma_enum_devices_by_ibdev(cma_device_filter filter,void * cookie)270 struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter	filter,
271 					     void		*cookie)
272 {
273 	struct cma_device *cma_dev;
274 	struct cma_device *found_cma_dev = NULL;
275 
276 	mutex_lock(&lock);
277 
278 	list_for_each_entry(cma_dev, &dev_list, list)
279 		if (filter(cma_dev->device, cookie)) {
280 			found_cma_dev = cma_dev;
281 			break;
282 		}
283 
284 	if (found_cma_dev)
285 		cma_dev_get(found_cma_dev);
286 	mutex_unlock(&lock);
287 	return found_cma_dev;
288 }
289 
cma_get_default_gid_type(struct cma_device * cma_dev,u32 port)290 int cma_get_default_gid_type(struct cma_device *cma_dev,
291 			     u32 port)
292 {
293 	if (!rdma_is_port_valid(cma_dev->device, port))
294 		return -EINVAL;
295 
296 	return cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)];
297 }
298 
cma_set_default_gid_type(struct cma_device * cma_dev,u32 port,enum ib_gid_type default_gid_type)299 int cma_set_default_gid_type(struct cma_device *cma_dev,
300 			     u32 port,
301 			     enum ib_gid_type default_gid_type)
302 {
303 	unsigned long supported_gids;
304 
305 	if (!rdma_is_port_valid(cma_dev->device, port))
306 		return -EINVAL;
307 
308 	if (default_gid_type == IB_GID_TYPE_IB &&
309 	    rdma_protocol_roce_eth_encap(cma_dev->device, port))
310 		default_gid_type = IB_GID_TYPE_ROCE;
311 
312 	supported_gids = roce_gid_type_mask_support(cma_dev->device, port);
313 
314 	if (!(supported_gids & 1 << default_gid_type))
315 		return -EINVAL;
316 
317 	cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)] =
318 		default_gid_type;
319 
320 	return 0;
321 }
322 
cma_get_default_roce_tos(struct cma_device * cma_dev,u32 port)323 int cma_get_default_roce_tos(struct cma_device *cma_dev, u32 port)
324 {
325 	if (!rdma_is_port_valid(cma_dev->device, port))
326 		return -EINVAL;
327 
328 	return cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)];
329 }
330 
cma_set_default_roce_tos(struct cma_device * cma_dev,u32 port,u8 default_roce_tos)331 int cma_set_default_roce_tos(struct cma_device *cma_dev, u32 port,
332 			     u8 default_roce_tos)
333 {
334 	if (!rdma_is_port_valid(cma_dev->device, port))
335 		return -EINVAL;
336 
337 	cma_dev->default_roce_tos[port - rdma_start_port(cma_dev->device)] =
338 		 default_roce_tos;
339 
340 	return 0;
341 }
cma_get_ib_dev(struct cma_device * cma_dev)342 struct ib_device *cma_get_ib_dev(struct cma_device *cma_dev)
343 {
344 	return cma_dev->device;
345 }
346 
347 /*
348  * Device removal can occur at anytime, so we need extra handling to
349  * serialize notifying the user of device removal with other callbacks.
350  * We do this by disabling removal notification while a callback is in process,
351  * and reporting it after the callback completes.
352  */
353 
354 struct cma_multicast {
355 	struct rdma_id_private *id_priv;
356 	union {
357 		struct ib_sa_multicast *sa_mc;
358 		struct {
359 			struct work_struct work;
360 			struct rdma_cm_event event;
361 		} iboe_join;
362 	};
363 	struct list_head	list;
364 	void			*context;
365 	struct sockaddr_storage	addr;
366 	u8			join_state;
367 };
368 
369 struct cma_work {
370 	struct work_struct	work;
371 	struct rdma_id_private	*id;
372 	enum rdma_cm_state	old_state;
373 	enum rdma_cm_state	new_state;
374 	struct rdma_cm_event	event;
375 };
376 
377 union cma_ip_addr {
378 	struct in6_addr ip6;
379 	struct {
380 		__be32 pad[3];
381 		__be32 addr;
382 	} ip4;
383 };
384 
385 struct cma_hdr {
386 	u8 cma_version;
387 	u8 ip_version;	/* IP version: 7:4 */
388 	__be16 port;
389 	union cma_ip_addr src_addr;
390 	union cma_ip_addr dst_addr;
391 };
392 
393 #define CMA_VERSION 0x00
394 
395 struct cma_req_info {
396 	struct sockaddr_storage listen_addr_storage;
397 	struct sockaddr_storage src_addr_storage;
398 	struct ib_device *device;
399 	union ib_gid local_gid;
400 	__be64 service_id;
401 	int port;
402 	bool has_gid;
403 	u16 pkey;
404 };
405 
cma_comp_exch(struct rdma_id_private * id_priv,enum rdma_cm_state comp,enum rdma_cm_state exch)406 static int cma_comp_exch(struct rdma_id_private *id_priv,
407 			 enum rdma_cm_state comp, enum rdma_cm_state exch)
408 {
409 	unsigned long flags;
410 	int ret;
411 
412 	/*
413 	 * The FSM uses a funny double locking where state is protected by both
414 	 * the handler_mutex and the spinlock. State is not allowed to change
415 	 * to/from a handler_mutex protected value without also holding
416 	 * handler_mutex.
417 	 */
418 	if (comp == RDMA_CM_CONNECT || exch == RDMA_CM_CONNECT)
419 		lockdep_assert_held(&id_priv->handler_mutex);
420 
421 	spin_lock_irqsave(&id_priv->lock, flags);
422 	if ((ret = (id_priv->state == comp)))
423 		id_priv->state = exch;
424 	spin_unlock_irqrestore(&id_priv->lock, flags);
425 	return ret;
426 }
427 
cma_get_ip_ver(const struct cma_hdr * hdr)428 static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr)
429 {
430 	return hdr->ip_version >> 4;
431 }
432 
cma_set_ip_ver(struct cma_hdr * hdr,u8 ip_ver)433 static void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
434 {
435 	hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
436 }
437 
cma_src_addr(struct rdma_id_private * id_priv)438 static struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
439 {
440 	return (struct sockaddr *)&id_priv->id.route.addr.src_addr;
441 }
442 
cma_dst_addr(struct rdma_id_private * id_priv)443 static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
444 {
445 	return (struct sockaddr *)&id_priv->id.route.addr.dst_addr;
446 }
447 
cma_igmp_send(struct net_device * ndev,union ib_gid * mgid,bool join)448 static int cma_igmp_send(struct net_device *ndev, union ib_gid *mgid, bool join)
449 {
450 	struct in_device *in_dev = NULL;
451 
452 	if (ndev) {
453 		rtnl_lock();
454 		in_dev = __in_dev_get_rtnl(ndev);
455 		if (in_dev) {
456 			if (join)
457 				ip_mc_inc_group(in_dev,
458 						*(__be32 *)(mgid->raw + 12));
459 			else
460 				ip_mc_dec_group(in_dev,
461 						*(__be32 *)(mgid->raw + 12));
462 		}
463 		rtnl_unlock();
464 	}
465 	return (in_dev) ? 0 : -ENODEV;
466 }
467 
compare_netdev_and_ip(int ifindex_a,struct sockaddr * sa,struct id_table_entry * entry_b)468 static int compare_netdev_and_ip(int ifindex_a, struct sockaddr *sa,
469 				 struct id_table_entry *entry_b)
470 {
471 	struct rdma_id_private *id_priv = list_first_entry(
472 		&entry_b->id_list, struct rdma_id_private, id_list_entry);
473 	int ifindex_b = id_priv->id.route.addr.dev_addr.bound_dev_if;
474 	struct sockaddr *sb = cma_dst_addr(id_priv);
475 
476 	if (ifindex_a != ifindex_b)
477 		return (ifindex_a > ifindex_b) ? 1 : -1;
478 
479 	if (sa->sa_family != sb->sa_family)
480 		return sa->sa_family - sb->sa_family;
481 
482 	if (sa->sa_family == AF_INET &&
483 	    __builtin_object_size(sa, 0) >= sizeof(struct sockaddr_in)) {
484 		return memcmp(&((struct sockaddr_in *)sa)->sin_addr,
485 			      &((struct sockaddr_in *)sb)->sin_addr,
486 			      sizeof(((struct sockaddr_in *)sa)->sin_addr));
487 	}
488 
489 	if (sa->sa_family == AF_INET6 &&
490 	    __builtin_object_size(sa, 0) >= sizeof(struct sockaddr_in6)) {
491 		return ipv6_addr_cmp(&((struct sockaddr_in6 *)sa)->sin6_addr,
492 				     &((struct sockaddr_in6 *)sb)->sin6_addr);
493 	}
494 
495 	return -1;
496 }
497 
cma_add_id_to_tree(struct rdma_id_private * node_id_priv)498 static int cma_add_id_to_tree(struct rdma_id_private *node_id_priv)
499 {
500 	struct rb_node **new, *parent = NULL;
501 	struct id_table_entry *this, *node;
502 	unsigned long flags;
503 	int result;
504 
505 	node = kzalloc(sizeof(*node), GFP_KERNEL);
506 	if (!node)
507 		return -ENOMEM;
508 
509 	spin_lock_irqsave(&id_table_lock, flags);
510 	new = &id_table.rb_node;
511 	while (*new) {
512 		this = container_of(*new, struct id_table_entry, rb_node);
513 		result = compare_netdev_and_ip(
514 			node_id_priv->id.route.addr.dev_addr.bound_dev_if,
515 			cma_dst_addr(node_id_priv), this);
516 
517 		parent = *new;
518 		if (result < 0)
519 			new = &((*new)->rb_left);
520 		else if (result > 0)
521 			new = &((*new)->rb_right);
522 		else {
523 			list_add_tail(&node_id_priv->id_list_entry,
524 				      &this->id_list);
525 			kfree(node);
526 			goto unlock;
527 		}
528 	}
529 
530 	INIT_LIST_HEAD(&node->id_list);
531 	list_add_tail(&node_id_priv->id_list_entry, &node->id_list);
532 
533 	rb_link_node(&node->rb_node, parent, new);
534 	rb_insert_color(&node->rb_node, &id_table);
535 
536 unlock:
537 	spin_unlock_irqrestore(&id_table_lock, flags);
538 	return 0;
539 }
540 
541 static struct id_table_entry *
node_from_ndev_ip(struct rb_root * root,int ifindex,struct sockaddr * sa)542 node_from_ndev_ip(struct rb_root *root, int ifindex, struct sockaddr *sa)
543 {
544 	struct rb_node *node = root->rb_node;
545 	struct id_table_entry *data;
546 	int result;
547 
548 	while (node) {
549 		data = container_of(node, struct id_table_entry, rb_node);
550 		result = compare_netdev_and_ip(ifindex, sa, data);
551 		if (result < 0)
552 			node = node->rb_left;
553 		else if (result > 0)
554 			node = node->rb_right;
555 		else
556 			return data;
557 	}
558 
559 	return NULL;
560 }
561 
cma_remove_id_from_tree(struct rdma_id_private * id_priv)562 static void cma_remove_id_from_tree(struct rdma_id_private *id_priv)
563 {
564 	struct id_table_entry *data;
565 	unsigned long flags;
566 
567 	spin_lock_irqsave(&id_table_lock, flags);
568 	if (list_empty(&id_priv->id_list_entry))
569 		goto out;
570 
571 	data = node_from_ndev_ip(&id_table,
572 				 id_priv->id.route.addr.dev_addr.bound_dev_if,
573 				 cma_dst_addr(id_priv));
574 	if (!data)
575 		goto out;
576 
577 	list_del_init(&id_priv->id_list_entry);
578 	if (list_empty(&data->id_list)) {
579 		rb_erase(&data->rb_node, &id_table);
580 		kfree(data);
581 	}
582 out:
583 	spin_unlock_irqrestore(&id_table_lock, flags);
584 }
585 
_cma_attach_to_dev(struct rdma_id_private * id_priv,struct cma_device * cma_dev)586 static void _cma_attach_to_dev(struct rdma_id_private *id_priv,
587 			       struct cma_device *cma_dev)
588 {
589 	cma_dev_get(cma_dev);
590 	id_priv->cma_dev = cma_dev;
591 	id_priv->id.device = cma_dev->device;
592 	id_priv->id.route.addr.dev_addr.transport =
593 		rdma_node_get_transport(cma_dev->device->node_type);
594 	list_add_tail(&id_priv->device_item, &cma_dev->id_list);
595 
596 	trace_cm_id_attach(id_priv, cma_dev->device);
597 }
598 
cma_attach_to_dev(struct rdma_id_private * id_priv,struct cma_device * cma_dev)599 static void cma_attach_to_dev(struct rdma_id_private *id_priv,
600 			      struct cma_device *cma_dev)
601 {
602 	_cma_attach_to_dev(id_priv, cma_dev);
603 	id_priv->gid_type =
604 		cma_dev->default_gid_type[id_priv->id.port_num -
605 					  rdma_start_port(cma_dev->device)];
606 }
607 
cma_release_dev(struct rdma_id_private * id_priv)608 static void cma_release_dev(struct rdma_id_private *id_priv)
609 {
610 	mutex_lock(&lock);
611 	list_del_init(&id_priv->device_item);
612 	cma_dev_put(id_priv->cma_dev);
613 	id_priv->cma_dev = NULL;
614 	id_priv->id.device = NULL;
615 	if (id_priv->id.route.addr.dev_addr.sgid_attr) {
616 		rdma_put_gid_attr(id_priv->id.route.addr.dev_addr.sgid_attr);
617 		id_priv->id.route.addr.dev_addr.sgid_attr = NULL;
618 	}
619 	mutex_unlock(&lock);
620 }
621 
cma_family(struct rdma_id_private * id_priv)622 static inline unsigned short cma_family(struct rdma_id_private *id_priv)
623 {
624 	return id_priv->id.route.addr.src_addr.ss_family;
625 }
626 
cma_set_default_qkey(struct rdma_id_private * id_priv)627 static int cma_set_default_qkey(struct rdma_id_private *id_priv)
628 {
629 	struct ib_sa_mcmember_rec rec;
630 	int ret = 0;
631 
632 	switch (id_priv->id.ps) {
633 	case RDMA_PS_UDP:
634 	case RDMA_PS_IB:
635 		id_priv->qkey = RDMA_UDP_QKEY;
636 		break;
637 	case RDMA_PS_IPOIB:
638 		ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
639 		ret = ib_sa_get_mcmember_rec(id_priv->id.device,
640 					     id_priv->id.port_num, &rec.mgid,
641 					     &rec);
642 		if (!ret)
643 			id_priv->qkey = be32_to_cpu(rec.qkey);
644 		break;
645 	default:
646 		break;
647 	}
648 	return ret;
649 }
650 
cma_set_qkey(struct rdma_id_private * id_priv,u32 qkey)651 static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
652 {
653 	if (!qkey ||
654 	    (id_priv->qkey && (id_priv->qkey != qkey)))
655 		return -EINVAL;
656 
657 	id_priv->qkey = qkey;
658 	return 0;
659 }
660 
cma_translate_ib(struct sockaddr_ib * sib,struct rdma_dev_addr * dev_addr)661 static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
662 {
663 	dev_addr->dev_type = ARPHRD_INFINIBAND;
664 	rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
665 	ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
666 }
667 
cma_translate_addr(struct sockaddr * addr,struct rdma_dev_addr * dev_addr)668 static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
669 {
670 	int ret;
671 
672 	if (addr->sa_family != AF_IB) {
673 		ret = rdma_translate_ip(addr, dev_addr);
674 	} else {
675 		cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
676 		ret = 0;
677 	}
678 
679 	return ret;
680 }
681 
682 static const struct ib_gid_attr *
cma_validate_port(struct ib_device * device,u32 port,enum ib_gid_type gid_type,union ib_gid * gid,struct rdma_id_private * id_priv)683 cma_validate_port(struct ib_device *device, u32 port,
684 		  enum ib_gid_type gid_type,
685 		  union ib_gid *gid,
686 		  struct rdma_id_private *id_priv)
687 {
688 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
689 	const struct ib_gid_attr *sgid_attr = ERR_PTR(-ENODEV);
690 	int bound_if_index = dev_addr->bound_dev_if;
691 	int dev_type = dev_addr->dev_type;
692 	struct net_device *ndev = NULL;
693 	struct net_device *pdev = NULL;
694 
695 	if (!rdma_dev_access_netns(device, id_priv->id.route.addr.dev_addr.net))
696 		goto out;
697 
698 	if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port))
699 		goto out;
700 
701 	if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port))
702 		goto out;
703 
704 	/*
705 	 * For drivers that do not associate more than one net device with
706 	 * their gid tables, such as iWARP drivers, it is sufficient to
707 	 * return the first table entry.
708 	 *
709 	 * Other driver classes might be included in the future.
710 	 */
711 	if (rdma_protocol_iwarp(device, port)) {
712 		sgid_attr = rdma_get_gid_attr(device, port, 0);
713 		if (IS_ERR(sgid_attr))
714 			goto out;
715 
716 		rcu_read_lock();
717 		ndev = rcu_dereference(sgid_attr->ndev);
718 		if (ndev->ifindex != bound_if_index) {
719 			pdev = dev_get_by_index_rcu(dev_addr->net, bound_if_index);
720 			if (pdev) {
721 				if (is_vlan_dev(pdev)) {
722 					pdev = vlan_dev_real_dev(pdev);
723 					if (ndev->ifindex == pdev->ifindex)
724 						bound_if_index = pdev->ifindex;
725 				}
726 				if (is_vlan_dev(ndev)) {
727 					pdev = vlan_dev_real_dev(ndev);
728 					if (bound_if_index == pdev->ifindex)
729 						bound_if_index = ndev->ifindex;
730 				}
731 			}
732 		}
733 		if (!net_eq(dev_net(ndev), dev_addr->net) ||
734 		    ndev->ifindex != bound_if_index) {
735 			rdma_put_gid_attr(sgid_attr);
736 			sgid_attr = ERR_PTR(-ENODEV);
737 		}
738 		rcu_read_unlock();
739 		goto out;
740 	}
741 
742 	if (dev_type == ARPHRD_ETHER && rdma_protocol_roce(device, port)) {
743 		ndev = dev_get_by_index(dev_addr->net, bound_if_index);
744 		if (!ndev)
745 			goto out;
746 	} else {
747 		gid_type = IB_GID_TYPE_IB;
748 	}
749 
750 	sgid_attr = rdma_find_gid_by_port(device, gid, gid_type, port, ndev);
751 	dev_put(ndev);
752 out:
753 	return sgid_attr;
754 }
755 
cma_bind_sgid_attr(struct rdma_id_private * id_priv,const struct ib_gid_attr * sgid_attr)756 static void cma_bind_sgid_attr(struct rdma_id_private *id_priv,
757 			       const struct ib_gid_attr *sgid_attr)
758 {
759 	WARN_ON(id_priv->id.route.addr.dev_addr.sgid_attr);
760 	id_priv->id.route.addr.dev_addr.sgid_attr = sgid_attr;
761 }
762 
763 /**
764  * cma_acquire_dev_by_src_ip - Acquire cma device, port, gid attribute
765  * based on source ip address.
766  * @id_priv:	cm_id which should be bound to cma device
767  *
768  * cma_acquire_dev_by_src_ip() binds cm id to cma device, port and GID attribute
769  * based on source IP address. It returns 0 on success or error code otherwise.
770  * It is applicable to active and passive side cm_id.
771  */
cma_acquire_dev_by_src_ip(struct rdma_id_private * id_priv)772 static int cma_acquire_dev_by_src_ip(struct rdma_id_private *id_priv)
773 {
774 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
775 	const struct ib_gid_attr *sgid_attr;
776 	union ib_gid gid, iboe_gid, *gidp;
777 	struct cma_device *cma_dev;
778 	enum ib_gid_type gid_type;
779 	int ret = -ENODEV;
780 	u32 port;
781 
782 	if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
783 	    id_priv->id.ps == RDMA_PS_IPOIB)
784 		return -EINVAL;
785 
786 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
787 		    &iboe_gid);
788 
789 	memcpy(&gid, dev_addr->src_dev_addr +
790 	       rdma_addr_gid_offset(dev_addr), sizeof(gid));
791 
792 	mutex_lock(&lock);
793 	list_for_each_entry(cma_dev, &dev_list, list) {
794 		rdma_for_each_port (cma_dev->device, port) {
795 			gidp = rdma_protocol_roce(cma_dev->device, port) ?
796 			       &iboe_gid : &gid;
797 			gid_type = cma_dev->default_gid_type[port - 1];
798 			sgid_attr = cma_validate_port(cma_dev->device, port,
799 						      gid_type, gidp, id_priv);
800 			if (!IS_ERR(sgid_attr)) {
801 				id_priv->id.port_num = port;
802 				cma_bind_sgid_attr(id_priv, sgid_attr);
803 				cma_attach_to_dev(id_priv, cma_dev);
804 				ret = 0;
805 				goto out;
806 			}
807 		}
808 	}
809 out:
810 	mutex_unlock(&lock);
811 	return ret;
812 }
813 
814 /**
815  * cma_ib_acquire_dev - Acquire cma device, port and SGID attribute
816  * @id_priv:		cm id to bind to cma device
817  * @listen_id_priv:	listener cm id to match against
818  * @req:		Pointer to req structure containaining incoming
819  *			request information
820  * cma_ib_acquire_dev() acquires cma device, port and SGID attribute when
821  * rdma device matches for listen_id and incoming request. It also verifies
822  * that a GID table entry is present for the source address.
823  * Returns 0 on success, or returns error code otherwise.
824  */
cma_ib_acquire_dev(struct rdma_id_private * id_priv,const struct rdma_id_private * listen_id_priv,struct cma_req_info * req)825 static int cma_ib_acquire_dev(struct rdma_id_private *id_priv,
826 			      const struct rdma_id_private *listen_id_priv,
827 			      struct cma_req_info *req)
828 {
829 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
830 	const struct ib_gid_attr *sgid_attr;
831 	enum ib_gid_type gid_type;
832 	union ib_gid gid;
833 
834 	if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
835 	    id_priv->id.ps == RDMA_PS_IPOIB)
836 		return -EINVAL;
837 
838 	if (rdma_protocol_roce(req->device, req->port))
839 		rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
840 			    &gid);
841 	else
842 		memcpy(&gid, dev_addr->src_dev_addr +
843 		       rdma_addr_gid_offset(dev_addr), sizeof(gid));
844 
845 	gid_type = listen_id_priv->cma_dev->default_gid_type[req->port - 1];
846 	sgid_attr = cma_validate_port(req->device, req->port,
847 				      gid_type, &gid, id_priv);
848 	if (IS_ERR(sgid_attr))
849 		return PTR_ERR(sgid_attr);
850 
851 	id_priv->id.port_num = req->port;
852 	cma_bind_sgid_attr(id_priv, sgid_attr);
853 	/* Need to acquire lock to protect against reader
854 	 * of cma_dev->id_list such as cma_netdev_callback() and
855 	 * cma_process_remove().
856 	 */
857 	mutex_lock(&lock);
858 	cma_attach_to_dev(id_priv, listen_id_priv->cma_dev);
859 	mutex_unlock(&lock);
860 	rdma_restrack_add(&id_priv->res);
861 	return 0;
862 }
863 
cma_iw_acquire_dev(struct rdma_id_private * id_priv,const struct rdma_id_private * listen_id_priv)864 static int cma_iw_acquire_dev(struct rdma_id_private *id_priv,
865 			      const struct rdma_id_private *listen_id_priv)
866 {
867 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
868 	const struct ib_gid_attr *sgid_attr;
869 	struct cma_device *cma_dev;
870 	enum ib_gid_type gid_type;
871 	int ret = -ENODEV;
872 	union ib_gid gid;
873 	u32 port;
874 
875 	if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
876 	    id_priv->id.ps == RDMA_PS_IPOIB)
877 		return -EINVAL;
878 
879 	memcpy(&gid, dev_addr->src_dev_addr +
880 	       rdma_addr_gid_offset(dev_addr), sizeof(gid));
881 
882 	mutex_lock(&lock);
883 
884 	cma_dev = listen_id_priv->cma_dev;
885 	port = listen_id_priv->id.port_num;
886 	gid_type = listen_id_priv->gid_type;
887 	sgid_attr = cma_validate_port(cma_dev->device, port,
888 				      gid_type, &gid, id_priv);
889 	if (!IS_ERR(sgid_attr)) {
890 		id_priv->id.port_num = port;
891 		cma_bind_sgid_attr(id_priv, sgid_attr);
892 		ret = 0;
893 		goto out;
894 	}
895 
896 	list_for_each_entry(cma_dev, &dev_list, list) {
897 		rdma_for_each_port (cma_dev->device, port) {
898 			if (listen_id_priv->cma_dev == cma_dev &&
899 			    listen_id_priv->id.port_num == port)
900 				continue;
901 
902 			gid_type = cma_dev->default_gid_type[port - 1];
903 			sgid_attr = cma_validate_port(cma_dev->device, port,
904 						      gid_type, &gid, id_priv);
905 			if (!IS_ERR(sgid_attr)) {
906 				id_priv->id.port_num = port;
907 				cma_bind_sgid_attr(id_priv, sgid_attr);
908 				ret = 0;
909 				goto out;
910 			}
911 		}
912 	}
913 
914 out:
915 	if (!ret) {
916 		cma_attach_to_dev(id_priv, cma_dev);
917 		rdma_restrack_add(&id_priv->res);
918 	}
919 
920 	mutex_unlock(&lock);
921 	return ret;
922 }
923 
924 /*
925  * Select the source IB device and address to reach the destination IB address.
926  */
cma_resolve_ib_dev(struct rdma_id_private * id_priv)927 static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
928 {
929 	struct cma_device *cma_dev, *cur_dev;
930 	struct sockaddr_ib *addr;
931 	union ib_gid gid, sgid, *dgid;
932 	unsigned int p;
933 	u16 pkey, index;
934 	enum ib_port_state port_state;
935 	int ret;
936 	int i;
937 
938 	cma_dev = NULL;
939 	addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
940 	dgid = (union ib_gid *) &addr->sib_addr;
941 	pkey = ntohs(addr->sib_pkey);
942 
943 	mutex_lock(&lock);
944 	list_for_each_entry(cur_dev, &dev_list, list) {
945 		rdma_for_each_port (cur_dev->device, p) {
946 			if (!rdma_cap_af_ib(cur_dev->device, p))
947 				continue;
948 
949 			if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
950 				continue;
951 
952 			if (ib_get_cached_port_state(cur_dev->device, p, &port_state))
953 				continue;
954 
955 			for (i = 0; i < cur_dev->device->port_data[p].immutable.gid_tbl_len;
956 			     ++i) {
957 				ret = rdma_query_gid(cur_dev->device, p, i,
958 						     &gid);
959 				if (ret)
960 					continue;
961 
962 				if (!memcmp(&gid, dgid, sizeof(gid))) {
963 					cma_dev = cur_dev;
964 					sgid = gid;
965 					id_priv->id.port_num = p;
966 					goto found;
967 				}
968 
969 				if (!cma_dev && (gid.global.subnet_prefix ==
970 				    dgid->global.subnet_prefix) &&
971 				    port_state == IB_PORT_ACTIVE) {
972 					cma_dev = cur_dev;
973 					sgid = gid;
974 					id_priv->id.port_num = p;
975 					goto found;
976 				}
977 			}
978 		}
979 	}
980 	mutex_unlock(&lock);
981 	return -ENODEV;
982 
983 found:
984 	cma_attach_to_dev(id_priv, cma_dev);
985 	rdma_restrack_add(&id_priv->res);
986 	mutex_unlock(&lock);
987 	addr = (struct sockaddr_ib *)cma_src_addr(id_priv);
988 	memcpy(&addr->sib_addr, &sgid, sizeof(sgid));
989 	cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
990 	return 0;
991 }
992 
cma_id_get(struct rdma_id_private * id_priv)993 static void cma_id_get(struct rdma_id_private *id_priv)
994 {
995 	refcount_inc(&id_priv->refcount);
996 }
997 
cma_id_put(struct rdma_id_private * id_priv)998 static void cma_id_put(struct rdma_id_private *id_priv)
999 {
1000 	if (refcount_dec_and_test(&id_priv->refcount))
1001 		complete(&id_priv->comp);
1002 }
1003 
1004 static struct rdma_id_private *
__rdma_create_id(struct net * net,rdma_cm_event_handler event_handler,void * context,enum rdma_ucm_port_space ps,enum ib_qp_type qp_type,const struct rdma_id_private * parent)1005 __rdma_create_id(struct net *net, rdma_cm_event_handler event_handler,
1006 		 void *context, enum rdma_ucm_port_space ps,
1007 		 enum ib_qp_type qp_type, const struct rdma_id_private *parent)
1008 {
1009 	struct rdma_id_private *id_priv;
1010 
1011 	id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
1012 	if (!id_priv)
1013 		return ERR_PTR(-ENOMEM);
1014 
1015 	id_priv->state = RDMA_CM_IDLE;
1016 	id_priv->id.context = context;
1017 	id_priv->id.event_handler = event_handler;
1018 	id_priv->id.ps = ps;
1019 	id_priv->id.qp_type = qp_type;
1020 	id_priv->tos_set = false;
1021 	id_priv->timeout_set = false;
1022 	id_priv->min_rnr_timer_set = false;
1023 	id_priv->gid_type = IB_GID_TYPE_IB;
1024 	spin_lock_init(&id_priv->lock);
1025 	mutex_init(&id_priv->qp_mutex);
1026 	init_completion(&id_priv->comp);
1027 	refcount_set(&id_priv->refcount, 1);
1028 	mutex_init(&id_priv->handler_mutex);
1029 	INIT_LIST_HEAD(&id_priv->device_item);
1030 	INIT_LIST_HEAD(&id_priv->id_list_entry);
1031 	INIT_LIST_HEAD(&id_priv->listen_list);
1032 	INIT_LIST_HEAD(&id_priv->mc_list);
1033 	get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
1034 	id_priv->id.route.addr.dev_addr.net = get_net(net);
1035 	id_priv->seq_num &= 0x00ffffff;
1036 
1037 	rdma_restrack_new(&id_priv->res, RDMA_RESTRACK_CM_ID);
1038 	if (parent)
1039 		rdma_restrack_parent_name(&id_priv->res, &parent->res);
1040 
1041 	return id_priv;
1042 }
1043 
1044 struct rdma_cm_id *
__rdma_create_kernel_id(struct net * net,rdma_cm_event_handler event_handler,void * context,enum rdma_ucm_port_space ps,enum ib_qp_type qp_type,const char * caller)1045 __rdma_create_kernel_id(struct net *net, rdma_cm_event_handler event_handler,
1046 			void *context, enum rdma_ucm_port_space ps,
1047 			enum ib_qp_type qp_type, const char *caller)
1048 {
1049 	struct rdma_id_private *ret;
1050 
1051 	ret = __rdma_create_id(net, event_handler, context, ps, qp_type, NULL);
1052 	if (IS_ERR(ret))
1053 		return ERR_CAST(ret);
1054 
1055 	rdma_restrack_set_name(&ret->res, caller);
1056 	return &ret->id;
1057 }
1058 EXPORT_SYMBOL(__rdma_create_kernel_id);
1059 
rdma_create_user_id(rdma_cm_event_handler event_handler,void * context,enum rdma_ucm_port_space ps,enum ib_qp_type qp_type)1060 struct rdma_cm_id *rdma_create_user_id(rdma_cm_event_handler event_handler,
1061 				       void *context,
1062 				       enum rdma_ucm_port_space ps,
1063 				       enum ib_qp_type qp_type)
1064 {
1065 	struct rdma_id_private *ret;
1066 
1067 	ret = __rdma_create_id(current->nsproxy->net_ns, event_handler, context,
1068 			       ps, qp_type, NULL);
1069 	if (IS_ERR(ret))
1070 		return ERR_CAST(ret);
1071 
1072 	rdma_restrack_set_name(&ret->res, NULL);
1073 	return &ret->id;
1074 }
1075 EXPORT_SYMBOL(rdma_create_user_id);
1076 
cma_init_ud_qp(struct rdma_id_private * id_priv,struct ib_qp * qp)1077 static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
1078 {
1079 	struct ib_qp_attr qp_attr;
1080 	int qp_attr_mask, ret;
1081 
1082 	qp_attr.qp_state = IB_QPS_INIT;
1083 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1084 	if (ret)
1085 		return ret;
1086 
1087 	ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
1088 	if (ret)
1089 		return ret;
1090 
1091 	qp_attr.qp_state = IB_QPS_RTR;
1092 	ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
1093 	if (ret)
1094 		return ret;
1095 
1096 	qp_attr.qp_state = IB_QPS_RTS;
1097 	qp_attr.sq_psn = 0;
1098 	ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
1099 
1100 	return ret;
1101 }
1102 
cma_init_conn_qp(struct rdma_id_private * id_priv,struct ib_qp * qp)1103 static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
1104 {
1105 	struct ib_qp_attr qp_attr;
1106 	int qp_attr_mask, ret;
1107 
1108 	qp_attr.qp_state = IB_QPS_INIT;
1109 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1110 	if (ret)
1111 		return ret;
1112 
1113 	return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
1114 }
1115 
rdma_create_qp(struct rdma_cm_id * id,struct ib_pd * pd,struct ib_qp_init_attr * qp_init_attr)1116 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
1117 		   struct ib_qp_init_attr *qp_init_attr)
1118 {
1119 	struct rdma_id_private *id_priv;
1120 	struct ib_qp *qp;
1121 	int ret;
1122 
1123 	id_priv = container_of(id, struct rdma_id_private, id);
1124 	if (id->device != pd->device) {
1125 		ret = -EINVAL;
1126 		goto out_err;
1127 	}
1128 
1129 	qp_init_attr->port_num = id->port_num;
1130 	qp = ib_create_qp(pd, qp_init_attr);
1131 	if (IS_ERR(qp)) {
1132 		ret = PTR_ERR(qp);
1133 		goto out_err;
1134 	}
1135 
1136 	if (id->qp_type == IB_QPT_UD)
1137 		ret = cma_init_ud_qp(id_priv, qp);
1138 	else
1139 		ret = cma_init_conn_qp(id_priv, qp);
1140 	if (ret)
1141 		goto out_destroy;
1142 
1143 	id->qp = qp;
1144 	id_priv->qp_num = qp->qp_num;
1145 	id_priv->srq = (qp->srq != NULL);
1146 	trace_cm_qp_create(id_priv, pd, qp_init_attr, 0);
1147 	return 0;
1148 out_destroy:
1149 	ib_destroy_qp(qp);
1150 out_err:
1151 	trace_cm_qp_create(id_priv, pd, qp_init_attr, ret);
1152 	return ret;
1153 }
1154 EXPORT_SYMBOL(rdma_create_qp);
1155 
rdma_destroy_qp(struct rdma_cm_id * id)1156 void rdma_destroy_qp(struct rdma_cm_id *id)
1157 {
1158 	struct rdma_id_private *id_priv;
1159 
1160 	id_priv = container_of(id, struct rdma_id_private, id);
1161 	trace_cm_qp_destroy(id_priv);
1162 	mutex_lock(&id_priv->qp_mutex);
1163 	ib_destroy_qp(id_priv->id.qp);
1164 	id_priv->id.qp = NULL;
1165 	mutex_unlock(&id_priv->qp_mutex);
1166 }
1167 EXPORT_SYMBOL(rdma_destroy_qp);
1168 
cma_modify_qp_rtr(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)1169 static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
1170 			     struct rdma_conn_param *conn_param)
1171 {
1172 	struct ib_qp_attr qp_attr;
1173 	int qp_attr_mask, ret;
1174 
1175 	mutex_lock(&id_priv->qp_mutex);
1176 	if (!id_priv->id.qp) {
1177 		ret = 0;
1178 		goto out;
1179 	}
1180 
1181 	/* Need to update QP attributes from default values. */
1182 	qp_attr.qp_state = IB_QPS_INIT;
1183 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1184 	if (ret)
1185 		goto out;
1186 
1187 	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
1188 	if (ret)
1189 		goto out;
1190 
1191 	qp_attr.qp_state = IB_QPS_RTR;
1192 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1193 	if (ret)
1194 		goto out;
1195 
1196 	BUG_ON(id_priv->cma_dev->device != id_priv->id.device);
1197 
1198 	if (conn_param)
1199 		qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
1200 	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
1201 out:
1202 	mutex_unlock(&id_priv->qp_mutex);
1203 	return ret;
1204 }
1205 
cma_modify_qp_rts(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)1206 static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
1207 			     struct rdma_conn_param *conn_param)
1208 {
1209 	struct ib_qp_attr qp_attr;
1210 	int qp_attr_mask, ret;
1211 
1212 	mutex_lock(&id_priv->qp_mutex);
1213 	if (!id_priv->id.qp) {
1214 		ret = 0;
1215 		goto out;
1216 	}
1217 
1218 	qp_attr.qp_state = IB_QPS_RTS;
1219 	ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
1220 	if (ret)
1221 		goto out;
1222 
1223 	if (conn_param)
1224 		qp_attr.max_rd_atomic = conn_param->initiator_depth;
1225 	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
1226 out:
1227 	mutex_unlock(&id_priv->qp_mutex);
1228 	return ret;
1229 }
1230 
cma_modify_qp_err(struct rdma_id_private * id_priv)1231 static int cma_modify_qp_err(struct rdma_id_private *id_priv)
1232 {
1233 	struct ib_qp_attr qp_attr;
1234 	int ret;
1235 
1236 	mutex_lock(&id_priv->qp_mutex);
1237 	if (!id_priv->id.qp) {
1238 		ret = 0;
1239 		goto out;
1240 	}
1241 
1242 	qp_attr.qp_state = IB_QPS_ERR;
1243 	ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
1244 out:
1245 	mutex_unlock(&id_priv->qp_mutex);
1246 	return ret;
1247 }
1248 
cma_ib_init_qp_attr(struct rdma_id_private * id_priv,struct ib_qp_attr * qp_attr,int * qp_attr_mask)1249 static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
1250 			       struct ib_qp_attr *qp_attr, int *qp_attr_mask)
1251 {
1252 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
1253 	int ret;
1254 	u16 pkey;
1255 
1256 	if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num))
1257 		pkey = 0xffff;
1258 	else
1259 		pkey = ib_addr_get_pkey(dev_addr);
1260 
1261 	ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
1262 				  pkey, &qp_attr->pkey_index);
1263 	if (ret)
1264 		return ret;
1265 
1266 	qp_attr->port_num = id_priv->id.port_num;
1267 	*qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
1268 
1269 	if (id_priv->id.qp_type == IB_QPT_UD) {
1270 		ret = cma_set_default_qkey(id_priv);
1271 		if (ret)
1272 			return ret;
1273 
1274 		qp_attr->qkey = id_priv->qkey;
1275 		*qp_attr_mask |= IB_QP_QKEY;
1276 	} else {
1277 		qp_attr->qp_access_flags = 0;
1278 		*qp_attr_mask |= IB_QP_ACCESS_FLAGS;
1279 	}
1280 	return 0;
1281 }
1282 
rdma_init_qp_attr(struct rdma_cm_id * id,struct ib_qp_attr * qp_attr,int * qp_attr_mask)1283 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
1284 		       int *qp_attr_mask)
1285 {
1286 	struct rdma_id_private *id_priv;
1287 	int ret = 0;
1288 
1289 	id_priv = container_of(id, struct rdma_id_private, id);
1290 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
1291 		if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
1292 			ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
1293 		else
1294 			ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
1295 						 qp_attr_mask);
1296 
1297 		if (qp_attr->qp_state == IB_QPS_RTR)
1298 			qp_attr->rq_psn = id_priv->seq_num;
1299 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
1300 		if (!id_priv->cm_id.iw) {
1301 			qp_attr->qp_access_flags = 0;
1302 			*qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
1303 		} else
1304 			ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
1305 						 qp_attr_mask);
1306 		qp_attr->port_num = id_priv->id.port_num;
1307 		*qp_attr_mask |= IB_QP_PORT;
1308 	} else {
1309 		ret = -ENOSYS;
1310 	}
1311 
1312 	if ((*qp_attr_mask & IB_QP_TIMEOUT) && id_priv->timeout_set)
1313 		qp_attr->timeout = id_priv->timeout;
1314 
1315 	if ((*qp_attr_mask & IB_QP_MIN_RNR_TIMER) && id_priv->min_rnr_timer_set)
1316 		qp_attr->min_rnr_timer = id_priv->min_rnr_timer;
1317 
1318 	return ret;
1319 }
1320 EXPORT_SYMBOL(rdma_init_qp_attr);
1321 
cma_zero_addr(const struct sockaddr * addr)1322 static inline bool cma_zero_addr(const struct sockaddr *addr)
1323 {
1324 	switch (addr->sa_family) {
1325 	case AF_INET:
1326 		return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
1327 	case AF_INET6:
1328 		return ipv6_addr_any(&((struct sockaddr_in6 *)addr)->sin6_addr);
1329 	case AF_IB:
1330 		return ib_addr_any(&((struct sockaddr_ib *)addr)->sib_addr);
1331 	default:
1332 		return false;
1333 	}
1334 }
1335 
cma_loopback_addr(const struct sockaddr * addr)1336 static inline bool cma_loopback_addr(const struct sockaddr *addr)
1337 {
1338 	switch (addr->sa_family) {
1339 	case AF_INET:
1340 		return ipv4_is_loopback(
1341 			((struct sockaddr_in *)addr)->sin_addr.s_addr);
1342 	case AF_INET6:
1343 		return ipv6_addr_loopback(
1344 			&((struct sockaddr_in6 *)addr)->sin6_addr);
1345 	case AF_IB:
1346 		return ib_addr_loopback(
1347 			&((struct sockaddr_ib *)addr)->sib_addr);
1348 	default:
1349 		return false;
1350 	}
1351 }
1352 
cma_any_addr(const struct sockaddr * addr)1353 static inline bool cma_any_addr(const struct sockaddr *addr)
1354 {
1355 	return cma_zero_addr(addr) || cma_loopback_addr(addr);
1356 }
1357 
cma_addr_cmp(const struct sockaddr * src,const struct sockaddr * dst)1358 static int cma_addr_cmp(const struct sockaddr *src, const struct sockaddr *dst)
1359 {
1360 	if (src->sa_family != dst->sa_family)
1361 		return -1;
1362 
1363 	switch (src->sa_family) {
1364 	case AF_INET:
1365 		return ((struct sockaddr_in *)src)->sin_addr.s_addr !=
1366 		       ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1367 	case AF_INET6: {
1368 		struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *)src;
1369 		struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *)dst;
1370 		bool link_local;
1371 
1372 		if (ipv6_addr_cmp(&src_addr6->sin6_addr,
1373 					  &dst_addr6->sin6_addr))
1374 			return 1;
1375 		link_local = ipv6_addr_type(&dst_addr6->sin6_addr) &
1376 			     IPV6_ADDR_LINKLOCAL;
1377 		/* Link local must match their scope_ids */
1378 		return link_local ? (src_addr6->sin6_scope_id !=
1379 				     dst_addr6->sin6_scope_id) :
1380 				    0;
1381 	}
1382 
1383 	default:
1384 		return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
1385 				   &((struct sockaddr_ib *) dst)->sib_addr);
1386 	}
1387 }
1388 
cma_port(const struct sockaddr * addr)1389 static __be16 cma_port(const struct sockaddr *addr)
1390 {
1391 	struct sockaddr_ib *sib;
1392 
1393 	switch (addr->sa_family) {
1394 	case AF_INET:
1395 		return ((struct sockaddr_in *) addr)->sin_port;
1396 	case AF_INET6:
1397 		return ((struct sockaddr_in6 *) addr)->sin6_port;
1398 	case AF_IB:
1399 		sib = (struct sockaddr_ib *) addr;
1400 		return htons((u16) (be64_to_cpu(sib->sib_sid) &
1401 				    be64_to_cpu(sib->sib_sid_mask)));
1402 	default:
1403 		return 0;
1404 	}
1405 }
1406 
cma_any_port(const struct sockaddr * addr)1407 static inline int cma_any_port(const struct sockaddr *addr)
1408 {
1409 	return !cma_port(addr);
1410 }
1411 
cma_save_ib_info(struct sockaddr * src_addr,struct sockaddr * dst_addr,const struct rdma_cm_id * listen_id,const struct sa_path_rec * path)1412 static void cma_save_ib_info(struct sockaddr *src_addr,
1413 			     struct sockaddr *dst_addr,
1414 			     const struct rdma_cm_id *listen_id,
1415 			     const struct sa_path_rec *path)
1416 {
1417 	struct sockaddr_ib *listen_ib, *ib;
1418 
1419 	listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
1420 	if (src_addr) {
1421 		ib = (struct sockaddr_ib *)src_addr;
1422 		ib->sib_family = AF_IB;
1423 		if (path) {
1424 			ib->sib_pkey = path->pkey;
1425 			ib->sib_flowinfo = path->flow_label;
1426 			memcpy(&ib->sib_addr, &path->sgid, 16);
1427 			ib->sib_sid = path->service_id;
1428 			ib->sib_scope_id = 0;
1429 		} else {
1430 			ib->sib_pkey = listen_ib->sib_pkey;
1431 			ib->sib_flowinfo = listen_ib->sib_flowinfo;
1432 			ib->sib_addr = listen_ib->sib_addr;
1433 			ib->sib_sid = listen_ib->sib_sid;
1434 			ib->sib_scope_id = listen_ib->sib_scope_id;
1435 		}
1436 		ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
1437 	}
1438 	if (dst_addr) {
1439 		ib = (struct sockaddr_ib *)dst_addr;
1440 		ib->sib_family = AF_IB;
1441 		if (path) {
1442 			ib->sib_pkey = path->pkey;
1443 			ib->sib_flowinfo = path->flow_label;
1444 			memcpy(&ib->sib_addr, &path->dgid, 16);
1445 		}
1446 	}
1447 }
1448 
cma_save_ip4_info(struct sockaddr_in * src_addr,struct sockaddr_in * dst_addr,struct cma_hdr * hdr,__be16 local_port)1449 static void cma_save_ip4_info(struct sockaddr_in *src_addr,
1450 			      struct sockaddr_in *dst_addr,
1451 			      struct cma_hdr *hdr,
1452 			      __be16 local_port)
1453 {
1454 	if (src_addr) {
1455 		*src_addr = (struct sockaddr_in) {
1456 			.sin_family = AF_INET,
1457 			.sin_addr.s_addr = hdr->dst_addr.ip4.addr,
1458 			.sin_port = local_port,
1459 		};
1460 	}
1461 
1462 	if (dst_addr) {
1463 		*dst_addr = (struct sockaddr_in) {
1464 			.sin_family = AF_INET,
1465 			.sin_addr.s_addr = hdr->src_addr.ip4.addr,
1466 			.sin_port = hdr->port,
1467 		};
1468 	}
1469 }
1470 
cma_save_ip6_info(struct sockaddr_in6 * src_addr,struct sockaddr_in6 * dst_addr,struct cma_hdr * hdr,__be16 local_port)1471 static void cma_save_ip6_info(struct sockaddr_in6 *src_addr,
1472 			      struct sockaddr_in6 *dst_addr,
1473 			      struct cma_hdr *hdr,
1474 			      __be16 local_port)
1475 {
1476 	if (src_addr) {
1477 		*src_addr = (struct sockaddr_in6) {
1478 			.sin6_family = AF_INET6,
1479 			.sin6_addr = hdr->dst_addr.ip6,
1480 			.sin6_port = local_port,
1481 		};
1482 	}
1483 
1484 	if (dst_addr) {
1485 		*dst_addr = (struct sockaddr_in6) {
1486 			.sin6_family = AF_INET6,
1487 			.sin6_addr = hdr->src_addr.ip6,
1488 			.sin6_port = hdr->port,
1489 		};
1490 	}
1491 }
1492 
cma_port_from_service_id(__be64 service_id)1493 static u16 cma_port_from_service_id(__be64 service_id)
1494 {
1495 	return (u16)be64_to_cpu(service_id);
1496 }
1497 
cma_save_ip_info(struct sockaddr * src_addr,struct sockaddr * dst_addr,const struct ib_cm_event * ib_event,__be64 service_id)1498 static int cma_save_ip_info(struct sockaddr *src_addr,
1499 			    struct sockaddr *dst_addr,
1500 			    const struct ib_cm_event *ib_event,
1501 			    __be64 service_id)
1502 {
1503 	struct cma_hdr *hdr;
1504 	__be16 port;
1505 
1506 	hdr = ib_event->private_data;
1507 	if (hdr->cma_version != CMA_VERSION)
1508 		return -EINVAL;
1509 
1510 	port = htons(cma_port_from_service_id(service_id));
1511 
1512 	switch (cma_get_ip_ver(hdr)) {
1513 	case 4:
1514 		cma_save_ip4_info((struct sockaddr_in *)src_addr,
1515 				  (struct sockaddr_in *)dst_addr, hdr, port);
1516 		break;
1517 	case 6:
1518 		cma_save_ip6_info((struct sockaddr_in6 *)src_addr,
1519 				  (struct sockaddr_in6 *)dst_addr, hdr, port);
1520 		break;
1521 	default:
1522 		return -EAFNOSUPPORT;
1523 	}
1524 
1525 	return 0;
1526 }
1527 
cma_save_net_info(struct sockaddr * src_addr,struct sockaddr * dst_addr,const struct rdma_cm_id * listen_id,const struct ib_cm_event * ib_event,sa_family_t sa_family,__be64 service_id)1528 static int cma_save_net_info(struct sockaddr *src_addr,
1529 			     struct sockaddr *dst_addr,
1530 			     const struct rdma_cm_id *listen_id,
1531 			     const struct ib_cm_event *ib_event,
1532 			     sa_family_t sa_family, __be64 service_id)
1533 {
1534 	if (sa_family == AF_IB) {
1535 		if (ib_event->event == IB_CM_REQ_RECEIVED)
1536 			cma_save_ib_info(src_addr, dst_addr, listen_id,
1537 					 ib_event->param.req_rcvd.primary_path);
1538 		else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1539 			cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
1540 		return 0;
1541 	}
1542 
1543 	return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
1544 }
1545 
cma_save_req_info(const struct ib_cm_event * ib_event,struct cma_req_info * req)1546 static int cma_save_req_info(const struct ib_cm_event *ib_event,
1547 			     struct cma_req_info *req)
1548 {
1549 	const struct ib_cm_req_event_param *req_param =
1550 		&ib_event->param.req_rcvd;
1551 	const struct ib_cm_sidr_req_event_param *sidr_param =
1552 		&ib_event->param.sidr_req_rcvd;
1553 
1554 	switch (ib_event->event) {
1555 	case IB_CM_REQ_RECEIVED:
1556 		req->device	= req_param->listen_id->device;
1557 		req->port	= req_param->port;
1558 		memcpy(&req->local_gid, &req_param->primary_path->sgid,
1559 		       sizeof(req->local_gid));
1560 		req->has_gid	= true;
1561 		req->service_id = req_param->primary_path->service_id;
1562 		req->pkey	= be16_to_cpu(req_param->primary_path->pkey);
1563 		if (req->pkey != req_param->bth_pkey)
1564 			pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1565 					    "RDMA CMA: in the future this may cause the request to be dropped\n",
1566 					    req_param->bth_pkey, req->pkey);
1567 		break;
1568 	case IB_CM_SIDR_REQ_RECEIVED:
1569 		req->device	= sidr_param->listen_id->device;
1570 		req->port	= sidr_param->port;
1571 		req->has_gid	= false;
1572 		req->service_id	= sidr_param->service_id;
1573 		req->pkey	= sidr_param->pkey;
1574 		if (req->pkey != sidr_param->bth_pkey)
1575 			pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1576 					    "RDMA CMA: in the future this may cause the request to be dropped\n",
1577 					    sidr_param->bth_pkey, req->pkey);
1578 		break;
1579 	default:
1580 		return -EINVAL;
1581 	}
1582 
1583 	return 0;
1584 }
1585 
validate_ipv4_net_dev(struct net_device * net_dev,const struct sockaddr_in * dst_addr,const struct sockaddr_in * src_addr)1586 static bool validate_ipv4_net_dev(struct net_device *net_dev,
1587 				  const struct sockaddr_in *dst_addr,
1588 				  const struct sockaddr_in *src_addr)
1589 {
1590 	__be32 daddr = dst_addr->sin_addr.s_addr,
1591 	       saddr = src_addr->sin_addr.s_addr;
1592 	struct fib_result res;
1593 	struct flowi4 fl4;
1594 	int err;
1595 	bool ret;
1596 
1597 	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1598 	    ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) ||
1599 	    ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) ||
1600 	    ipv4_is_loopback(saddr))
1601 		return false;
1602 
1603 	memset(&fl4, 0, sizeof(fl4));
1604 	fl4.flowi4_oif = net_dev->ifindex;
1605 	fl4.daddr = daddr;
1606 	fl4.saddr = saddr;
1607 
1608 	rcu_read_lock();
1609 	err = fib_lookup(dev_net(net_dev), &fl4, &res, 0);
1610 	ret = err == 0 && FIB_RES_DEV(res) == net_dev;
1611 	rcu_read_unlock();
1612 
1613 	return ret;
1614 }
1615 
validate_ipv6_net_dev(struct net_device * net_dev,const struct sockaddr_in6 * dst_addr,const struct sockaddr_in6 * src_addr)1616 static bool validate_ipv6_net_dev(struct net_device *net_dev,
1617 				  const struct sockaddr_in6 *dst_addr,
1618 				  const struct sockaddr_in6 *src_addr)
1619 {
1620 #if IS_ENABLED(CONFIG_IPV6)
1621 	const int strict = ipv6_addr_type(&dst_addr->sin6_addr) &
1622 			   IPV6_ADDR_LINKLOCAL;
1623 	struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr,
1624 					 &src_addr->sin6_addr, net_dev->ifindex,
1625 					 NULL, strict);
1626 	bool ret;
1627 
1628 	if (!rt)
1629 		return false;
1630 
1631 	ret = rt->rt6i_idev->dev == net_dev;
1632 	ip6_rt_put(rt);
1633 
1634 	return ret;
1635 #else
1636 	return false;
1637 #endif
1638 }
1639 
validate_net_dev(struct net_device * net_dev,const struct sockaddr * daddr,const struct sockaddr * saddr)1640 static bool validate_net_dev(struct net_device *net_dev,
1641 			     const struct sockaddr *daddr,
1642 			     const struct sockaddr *saddr)
1643 {
1644 	const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
1645 	const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
1646 	const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1647 	const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
1648 
1649 	switch (daddr->sa_family) {
1650 	case AF_INET:
1651 		return saddr->sa_family == AF_INET &&
1652 		       validate_ipv4_net_dev(net_dev, daddr4, saddr4);
1653 
1654 	case AF_INET6:
1655 		return saddr->sa_family == AF_INET6 &&
1656 		       validate_ipv6_net_dev(net_dev, daddr6, saddr6);
1657 
1658 	default:
1659 		return false;
1660 	}
1661 }
1662 
1663 static struct net_device *
roce_get_net_dev_by_cm_event(const struct ib_cm_event * ib_event)1664 roce_get_net_dev_by_cm_event(const struct ib_cm_event *ib_event)
1665 {
1666 	const struct ib_gid_attr *sgid_attr = NULL;
1667 	struct net_device *ndev;
1668 
1669 	if (ib_event->event == IB_CM_REQ_RECEIVED)
1670 		sgid_attr = ib_event->param.req_rcvd.ppath_sgid_attr;
1671 	else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1672 		sgid_attr = ib_event->param.sidr_req_rcvd.sgid_attr;
1673 
1674 	if (!sgid_attr)
1675 		return NULL;
1676 
1677 	rcu_read_lock();
1678 	ndev = rdma_read_gid_attr_ndev_rcu(sgid_attr);
1679 	if (IS_ERR(ndev))
1680 		ndev = NULL;
1681 	else
1682 		dev_hold(ndev);
1683 	rcu_read_unlock();
1684 	return ndev;
1685 }
1686 
cma_get_net_dev(const struct ib_cm_event * ib_event,struct cma_req_info * req)1687 static struct net_device *cma_get_net_dev(const struct ib_cm_event *ib_event,
1688 					  struct cma_req_info *req)
1689 {
1690 	struct sockaddr *listen_addr =
1691 			(struct sockaddr *)&req->listen_addr_storage;
1692 	struct sockaddr *src_addr = (struct sockaddr *)&req->src_addr_storage;
1693 	struct net_device *net_dev;
1694 	const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
1695 	int err;
1696 
1697 	err = cma_save_ip_info(listen_addr, src_addr, ib_event,
1698 			       req->service_id);
1699 	if (err)
1700 		return ERR_PTR(err);
1701 
1702 	if (rdma_protocol_roce(req->device, req->port))
1703 		net_dev = roce_get_net_dev_by_cm_event(ib_event);
1704 	else
1705 		net_dev = ib_get_net_dev_by_params(req->device, req->port,
1706 						   req->pkey,
1707 						   gid, listen_addr);
1708 	if (!net_dev)
1709 		return ERR_PTR(-ENODEV);
1710 
1711 	return net_dev;
1712 }
1713 
rdma_ps_from_service_id(__be64 service_id)1714 static enum rdma_ucm_port_space rdma_ps_from_service_id(__be64 service_id)
1715 {
1716 	return (be64_to_cpu(service_id) >> 16) & 0xffff;
1717 }
1718 
cma_match_private_data(struct rdma_id_private * id_priv,const struct cma_hdr * hdr)1719 static bool cma_match_private_data(struct rdma_id_private *id_priv,
1720 				   const struct cma_hdr *hdr)
1721 {
1722 	struct sockaddr *addr = cma_src_addr(id_priv);
1723 	__be32 ip4_addr;
1724 	struct in6_addr ip6_addr;
1725 
1726 	if (cma_any_addr(addr) && !id_priv->afonly)
1727 		return true;
1728 
1729 	switch (addr->sa_family) {
1730 	case AF_INET:
1731 		ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
1732 		if (cma_get_ip_ver(hdr) != 4)
1733 			return false;
1734 		if (!cma_any_addr(addr) &&
1735 		    hdr->dst_addr.ip4.addr != ip4_addr)
1736 			return false;
1737 		break;
1738 	case AF_INET6:
1739 		ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
1740 		if (cma_get_ip_ver(hdr) != 6)
1741 			return false;
1742 		if (!cma_any_addr(addr) &&
1743 		    memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
1744 			return false;
1745 		break;
1746 	case AF_IB:
1747 		return true;
1748 	default:
1749 		return false;
1750 	}
1751 
1752 	return true;
1753 }
1754 
cma_protocol_roce(const struct rdma_cm_id * id)1755 static bool cma_protocol_roce(const struct rdma_cm_id *id)
1756 {
1757 	struct ib_device *device = id->device;
1758 	const u32 port_num = id->port_num ?: rdma_start_port(device);
1759 
1760 	return rdma_protocol_roce(device, port_num);
1761 }
1762 
cma_is_req_ipv6_ll(const struct cma_req_info * req)1763 static bool cma_is_req_ipv6_ll(const struct cma_req_info *req)
1764 {
1765 	const struct sockaddr *daddr =
1766 			(const struct sockaddr *)&req->listen_addr_storage;
1767 	const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1768 
1769 	/* Returns true if the req is for IPv6 link local */
1770 	return (daddr->sa_family == AF_INET6 &&
1771 		(ipv6_addr_type(&daddr6->sin6_addr) & IPV6_ADDR_LINKLOCAL));
1772 }
1773 
cma_match_net_dev(const struct rdma_cm_id * id,const struct net_device * net_dev,const struct cma_req_info * req)1774 static bool cma_match_net_dev(const struct rdma_cm_id *id,
1775 			      const struct net_device *net_dev,
1776 			      const struct cma_req_info *req)
1777 {
1778 	const struct rdma_addr *addr = &id->route.addr;
1779 
1780 	if (!net_dev)
1781 		/* This request is an AF_IB request */
1782 		return (!id->port_num || id->port_num == req->port) &&
1783 		       (addr->src_addr.ss_family == AF_IB);
1784 
1785 	/*
1786 	 * If the request is not for IPv6 link local, allow matching
1787 	 * request to any netdevice of the one or multiport rdma device.
1788 	 */
1789 	if (!cma_is_req_ipv6_ll(req))
1790 		return true;
1791 	/*
1792 	 * Net namespaces must match, and if the listner is listening
1793 	 * on a specific netdevice than netdevice must match as well.
1794 	 */
1795 	if (net_eq(dev_net(net_dev), addr->dev_addr.net) &&
1796 	    (!!addr->dev_addr.bound_dev_if ==
1797 	     (addr->dev_addr.bound_dev_if == net_dev->ifindex)))
1798 		return true;
1799 	else
1800 		return false;
1801 }
1802 
cma_find_listener(const struct rdma_bind_list * bind_list,const struct ib_cm_id * cm_id,const struct ib_cm_event * ib_event,const struct cma_req_info * req,const struct net_device * net_dev)1803 static struct rdma_id_private *cma_find_listener(
1804 		const struct rdma_bind_list *bind_list,
1805 		const struct ib_cm_id *cm_id,
1806 		const struct ib_cm_event *ib_event,
1807 		const struct cma_req_info *req,
1808 		const struct net_device *net_dev)
1809 {
1810 	struct rdma_id_private *id_priv, *id_priv_dev;
1811 
1812 	lockdep_assert_held(&lock);
1813 
1814 	if (!bind_list)
1815 		return ERR_PTR(-EINVAL);
1816 
1817 	hlist_for_each_entry(id_priv, &bind_list->owners, node) {
1818 		if (cma_match_private_data(id_priv, ib_event->private_data)) {
1819 			if (id_priv->id.device == cm_id->device &&
1820 			    cma_match_net_dev(&id_priv->id, net_dev, req))
1821 				return id_priv;
1822 			list_for_each_entry(id_priv_dev,
1823 					    &id_priv->listen_list,
1824 					    listen_item) {
1825 				if (id_priv_dev->id.device == cm_id->device &&
1826 				    cma_match_net_dev(&id_priv_dev->id,
1827 						      net_dev, req))
1828 					return id_priv_dev;
1829 			}
1830 		}
1831 	}
1832 
1833 	return ERR_PTR(-EINVAL);
1834 }
1835 
1836 static struct rdma_id_private *
cma_ib_id_from_event(struct ib_cm_id * cm_id,const struct ib_cm_event * ib_event,struct cma_req_info * req,struct net_device ** net_dev)1837 cma_ib_id_from_event(struct ib_cm_id *cm_id,
1838 		     const struct ib_cm_event *ib_event,
1839 		     struct cma_req_info *req,
1840 		     struct net_device **net_dev)
1841 {
1842 	struct rdma_bind_list *bind_list;
1843 	struct rdma_id_private *id_priv;
1844 	int err;
1845 
1846 	err = cma_save_req_info(ib_event, req);
1847 	if (err)
1848 		return ERR_PTR(err);
1849 
1850 	*net_dev = cma_get_net_dev(ib_event, req);
1851 	if (IS_ERR(*net_dev)) {
1852 		if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
1853 			/* Assuming the protocol is AF_IB */
1854 			*net_dev = NULL;
1855 		} else {
1856 			return ERR_CAST(*net_dev);
1857 		}
1858 	}
1859 
1860 	mutex_lock(&lock);
1861 	/*
1862 	 * Net namespace might be getting deleted while route lookup,
1863 	 * cm_id lookup is in progress. Therefore, perform netdevice
1864 	 * validation, cm_id lookup under rcu lock.
1865 	 * RCU lock along with netdevice state check, synchronizes with
1866 	 * netdevice migrating to different net namespace and also avoids
1867 	 * case where net namespace doesn't get deleted while lookup is in
1868 	 * progress.
1869 	 * If the device state is not IFF_UP, its properties such as ifindex
1870 	 * and nd_net cannot be trusted to remain valid without rcu lock.
1871 	 * net/core/dev.c change_net_namespace() ensures to synchronize with
1872 	 * ongoing operations on net device after device is closed using
1873 	 * synchronize_net().
1874 	 */
1875 	rcu_read_lock();
1876 	if (*net_dev) {
1877 		/*
1878 		 * If netdevice is down, it is likely that it is administratively
1879 		 * down or it might be migrating to different namespace.
1880 		 * In that case avoid further processing, as the net namespace
1881 		 * or ifindex may change.
1882 		 */
1883 		if (((*net_dev)->flags & IFF_UP) == 0) {
1884 			id_priv = ERR_PTR(-EHOSTUNREACH);
1885 			goto err;
1886 		}
1887 
1888 		if (!validate_net_dev(*net_dev,
1889 				 (struct sockaddr *)&req->src_addr_storage,
1890 				 (struct sockaddr *)&req->listen_addr_storage)) {
1891 			id_priv = ERR_PTR(-EHOSTUNREACH);
1892 			goto err;
1893 		}
1894 	}
1895 
1896 	bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
1897 				rdma_ps_from_service_id(req->service_id),
1898 				cma_port_from_service_id(req->service_id));
1899 	id_priv = cma_find_listener(bind_list, cm_id, ib_event, req, *net_dev);
1900 err:
1901 	rcu_read_unlock();
1902 	mutex_unlock(&lock);
1903 	if (IS_ERR(id_priv) && *net_dev) {
1904 		dev_put(*net_dev);
1905 		*net_dev = NULL;
1906 	}
1907 	return id_priv;
1908 }
1909 
cma_user_data_offset(struct rdma_id_private * id_priv)1910 static inline u8 cma_user_data_offset(struct rdma_id_private *id_priv)
1911 {
1912 	return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
1913 }
1914 
cma_cancel_route(struct rdma_id_private * id_priv)1915 static void cma_cancel_route(struct rdma_id_private *id_priv)
1916 {
1917 	if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
1918 		if (id_priv->query)
1919 			ib_sa_cancel_query(id_priv->query_id, id_priv->query);
1920 	}
1921 }
1922 
_cma_cancel_listens(struct rdma_id_private * id_priv)1923 static void _cma_cancel_listens(struct rdma_id_private *id_priv)
1924 {
1925 	struct rdma_id_private *dev_id_priv;
1926 
1927 	lockdep_assert_held(&lock);
1928 
1929 	/*
1930 	 * Remove from listen_any_list to prevent added devices from spawning
1931 	 * additional listen requests.
1932 	 */
1933 	list_del_init(&id_priv->listen_any_item);
1934 
1935 	while (!list_empty(&id_priv->listen_list)) {
1936 		dev_id_priv =
1937 			list_first_entry(&id_priv->listen_list,
1938 					 struct rdma_id_private, listen_item);
1939 		/* sync with device removal to avoid duplicate destruction */
1940 		list_del_init(&dev_id_priv->device_item);
1941 		list_del_init(&dev_id_priv->listen_item);
1942 		mutex_unlock(&lock);
1943 
1944 		rdma_destroy_id(&dev_id_priv->id);
1945 		mutex_lock(&lock);
1946 	}
1947 }
1948 
cma_cancel_listens(struct rdma_id_private * id_priv)1949 static void cma_cancel_listens(struct rdma_id_private *id_priv)
1950 {
1951 	mutex_lock(&lock);
1952 	_cma_cancel_listens(id_priv);
1953 	mutex_unlock(&lock);
1954 }
1955 
cma_cancel_operation(struct rdma_id_private * id_priv,enum rdma_cm_state state)1956 static void cma_cancel_operation(struct rdma_id_private *id_priv,
1957 				 enum rdma_cm_state state)
1958 {
1959 	switch (state) {
1960 	case RDMA_CM_ADDR_QUERY:
1961 		/*
1962 		 * We can avoid doing the rdma_addr_cancel() based on state,
1963 		 * only RDMA_CM_ADDR_QUERY has a work that could still execute.
1964 		 * Notice that the addr_handler work could still be exiting
1965 		 * outside this state, however due to the interaction with the
1966 		 * handler_mutex the work is guaranteed not to touch id_priv
1967 		 * during exit.
1968 		 */
1969 		rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1970 		break;
1971 	case RDMA_CM_ROUTE_QUERY:
1972 		cma_cancel_route(id_priv);
1973 		break;
1974 	case RDMA_CM_LISTEN:
1975 		if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
1976 			cma_cancel_listens(id_priv);
1977 		break;
1978 	default:
1979 		break;
1980 	}
1981 }
1982 
cma_release_port(struct rdma_id_private * id_priv)1983 static void cma_release_port(struct rdma_id_private *id_priv)
1984 {
1985 	struct rdma_bind_list *bind_list = id_priv->bind_list;
1986 	struct net *net = id_priv->id.route.addr.dev_addr.net;
1987 
1988 	if (!bind_list)
1989 		return;
1990 
1991 	mutex_lock(&lock);
1992 	hlist_del(&id_priv->node);
1993 	if (hlist_empty(&bind_list->owners)) {
1994 		cma_ps_remove(net, bind_list->ps, bind_list->port);
1995 		kfree(bind_list);
1996 	}
1997 	mutex_unlock(&lock);
1998 }
1999 
destroy_mc(struct rdma_id_private * id_priv,struct cma_multicast * mc)2000 static void destroy_mc(struct rdma_id_private *id_priv,
2001 		       struct cma_multicast *mc)
2002 {
2003 	bool send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
2004 
2005 	if (rdma_cap_ib_mcast(id_priv->id.device, id_priv->id.port_num))
2006 		ib_sa_free_multicast(mc->sa_mc);
2007 
2008 	if (rdma_protocol_roce(id_priv->id.device, id_priv->id.port_num)) {
2009 		struct rdma_dev_addr *dev_addr =
2010 			&id_priv->id.route.addr.dev_addr;
2011 		struct net_device *ndev = NULL;
2012 
2013 		if (dev_addr->bound_dev_if)
2014 			ndev = dev_get_by_index(dev_addr->net,
2015 						dev_addr->bound_dev_if);
2016 		if (ndev && !send_only) {
2017 			enum ib_gid_type gid_type;
2018 			union ib_gid mgid;
2019 
2020 			gid_type = id_priv->cma_dev->default_gid_type
2021 					   [id_priv->id.port_num -
2022 					    rdma_start_port(
2023 						    id_priv->cma_dev->device)];
2024 			cma_iboe_set_mgid((struct sockaddr *)&mc->addr, &mgid,
2025 					  gid_type);
2026 			cma_igmp_send(ndev, &mgid, false);
2027 		}
2028 		dev_put(ndev);
2029 
2030 		cancel_work_sync(&mc->iboe_join.work);
2031 	}
2032 	kfree(mc);
2033 }
2034 
cma_leave_mc_groups(struct rdma_id_private * id_priv)2035 static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
2036 {
2037 	struct cma_multicast *mc;
2038 
2039 	while (!list_empty(&id_priv->mc_list)) {
2040 		mc = list_first_entry(&id_priv->mc_list, struct cma_multicast,
2041 				      list);
2042 		list_del(&mc->list);
2043 		destroy_mc(id_priv, mc);
2044 	}
2045 }
2046 
_destroy_id(struct rdma_id_private * id_priv,enum rdma_cm_state state)2047 static void _destroy_id(struct rdma_id_private *id_priv,
2048 			enum rdma_cm_state state)
2049 {
2050 	cma_cancel_operation(id_priv, state);
2051 
2052 	rdma_restrack_del(&id_priv->res);
2053 	cma_remove_id_from_tree(id_priv);
2054 	if (id_priv->cma_dev) {
2055 		if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
2056 			if (id_priv->cm_id.ib)
2057 				ib_destroy_cm_id(id_priv->cm_id.ib);
2058 		} else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
2059 			if (id_priv->cm_id.iw)
2060 				iw_destroy_cm_id(id_priv->cm_id.iw);
2061 		}
2062 		cma_leave_mc_groups(id_priv);
2063 		cma_release_dev(id_priv);
2064 	}
2065 
2066 	cma_release_port(id_priv);
2067 	cma_id_put(id_priv);
2068 	wait_for_completion(&id_priv->comp);
2069 
2070 	if (id_priv->internal_id)
2071 		cma_id_put(id_priv->id.context);
2072 
2073 	kfree(id_priv->id.route.path_rec);
2074 	kfree(id_priv->id.route.path_rec_inbound);
2075 	kfree(id_priv->id.route.path_rec_outbound);
2076 
2077 	put_net(id_priv->id.route.addr.dev_addr.net);
2078 	kfree(id_priv);
2079 }
2080 
2081 /*
2082  * destroy an ID from within the handler_mutex. This ensures that no other
2083  * handlers can start running concurrently.
2084  */
destroy_id_handler_unlock(struct rdma_id_private * id_priv)2085 static void destroy_id_handler_unlock(struct rdma_id_private *id_priv)
2086 	__releases(&idprv->handler_mutex)
2087 {
2088 	enum rdma_cm_state state;
2089 	unsigned long flags;
2090 
2091 	trace_cm_id_destroy(id_priv);
2092 
2093 	/*
2094 	 * Setting the state to destroyed under the handler mutex provides a
2095 	 * fence against calling handler callbacks. If this is invoked due to
2096 	 * the failure of a handler callback then it guarentees that no future
2097 	 * handlers will be called.
2098 	 */
2099 	lockdep_assert_held(&id_priv->handler_mutex);
2100 	spin_lock_irqsave(&id_priv->lock, flags);
2101 	state = id_priv->state;
2102 	id_priv->state = RDMA_CM_DESTROYING;
2103 	spin_unlock_irqrestore(&id_priv->lock, flags);
2104 	mutex_unlock(&id_priv->handler_mutex);
2105 	_destroy_id(id_priv, state);
2106 }
2107 
rdma_destroy_id(struct rdma_cm_id * id)2108 void rdma_destroy_id(struct rdma_cm_id *id)
2109 {
2110 	struct rdma_id_private *id_priv =
2111 		container_of(id, struct rdma_id_private, id);
2112 
2113 	mutex_lock(&id_priv->handler_mutex);
2114 	destroy_id_handler_unlock(id_priv);
2115 }
2116 EXPORT_SYMBOL(rdma_destroy_id);
2117 
cma_rep_recv(struct rdma_id_private * id_priv)2118 static int cma_rep_recv(struct rdma_id_private *id_priv)
2119 {
2120 	int ret;
2121 
2122 	ret = cma_modify_qp_rtr(id_priv, NULL);
2123 	if (ret)
2124 		goto reject;
2125 
2126 	ret = cma_modify_qp_rts(id_priv, NULL);
2127 	if (ret)
2128 		goto reject;
2129 
2130 	trace_cm_send_rtu(id_priv);
2131 	ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
2132 	if (ret)
2133 		goto reject;
2134 
2135 	return 0;
2136 reject:
2137 	pr_debug_ratelimited("RDMA CM: CONNECT_ERROR: failed to handle reply. status %d\n", ret);
2138 	cma_modify_qp_err(id_priv);
2139 	trace_cm_send_rej(id_priv);
2140 	ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
2141 		       NULL, 0, NULL, 0);
2142 	return ret;
2143 }
2144 
cma_set_rep_event_data(struct rdma_cm_event * event,const struct ib_cm_rep_event_param * rep_data,void * private_data)2145 static void cma_set_rep_event_data(struct rdma_cm_event *event,
2146 				   const struct ib_cm_rep_event_param *rep_data,
2147 				   void *private_data)
2148 {
2149 	event->param.conn.private_data = private_data;
2150 	event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
2151 	event->param.conn.responder_resources = rep_data->responder_resources;
2152 	event->param.conn.initiator_depth = rep_data->initiator_depth;
2153 	event->param.conn.flow_control = rep_data->flow_control;
2154 	event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
2155 	event->param.conn.srq = rep_data->srq;
2156 	event->param.conn.qp_num = rep_data->remote_qpn;
2157 
2158 	event->ece.vendor_id = rep_data->ece.vendor_id;
2159 	event->ece.attr_mod = rep_data->ece.attr_mod;
2160 }
2161 
cma_cm_event_handler(struct rdma_id_private * id_priv,struct rdma_cm_event * event)2162 static int cma_cm_event_handler(struct rdma_id_private *id_priv,
2163 				struct rdma_cm_event *event)
2164 {
2165 	int ret;
2166 
2167 	lockdep_assert_held(&id_priv->handler_mutex);
2168 
2169 	trace_cm_event_handler(id_priv, event);
2170 	ret = id_priv->id.event_handler(&id_priv->id, event);
2171 	trace_cm_event_done(id_priv, event, ret);
2172 	return ret;
2173 }
2174 
cma_ib_handler(struct ib_cm_id * cm_id,const struct ib_cm_event * ib_event)2175 static int cma_ib_handler(struct ib_cm_id *cm_id,
2176 			  const struct ib_cm_event *ib_event)
2177 {
2178 	struct rdma_id_private *id_priv = cm_id->context;
2179 	struct rdma_cm_event event = {};
2180 	enum rdma_cm_state state;
2181 	int ret;
2182 
2183 	mutex_lock(&id_priv->handler_mutex);
2184 	state = READ_ONCE(id_priv->state);
2185 	if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
2186 	     state != RDMA_CM_CONNECT) ||
2187 	    (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
2188 	     state != RDMA_CM_DISCONNECT))
2189 		goto out;
2190 
2191 	switch (ib_event->event) {
2192 	case IB_CM_REQ_ERROR:
2193 	case IB_CM_REP_ERROR:
2194 		event.event = RDMA_CM_EVENT_UNREACHABLE;
2195 		event.status = -ETIMEDOUT;
2196 		break;
2197 	case IB_CM_REP_RECEIVED:
2198 		if (state == RDMA_CM_CONNECT &&
2199 		    (id_priv->id.qp_type != IB_QPT_UD)) {
2200 			trace_cm_send_mra(id_priv);
2201 			ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
2202 		}
2203 		if (id_priv->id.qp) {
2204 			event.status = cma_rep_recv(id_priv);
2205 			event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
2206 						     RDMA_CM_EVENT_ESTABLISHED;
2207 		} else {
2208 			event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
2209 		}
2210 		cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
2211 				       ib_event->private_data);
2212 		break;
2213 	case IB_CM_RTU_RECEIVED:
2214 	case IB_CM_USER_ESTABLISHED:
2215 		event.event = RDMA_CM_EVENT_ESTABLISHED;
2216 		break;
2217 	case IB_CM_DREQ_ERROR:
2218 		event.status = -ETIMEDOUT;
2219 		fallthrough;
2220 	case IB_CM_DREQ_RECEIVED:
2221 	case IB_CM_DREP_RECEIVED:
2222 		if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
2223 				   RDMA_CM_DISCONNECT))
2224 			goto out;
2225 		event.event = RDMA_CM_EVENT_DISCONNECTED;
2226 		break;
2227 	case IB_CM_TIMEWAIT_EXIT:
2228 		event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
2229 		break;
2230 	case IB_CM_MRA_RECEIVED:
2231 		/* ignore event */
2232 		goto out;
2233 	case IB_CM_REJ_RECEIVED:
2234 		pr_debug_ratelimited("RDMA CM: REJECTED: %s\n", rdma_reject_msg(&id_priv->id,
2235 										ib_event->param.rej_rcvd.reason));
2236 		cma_modify_qp_err(id_priv);
2237 		event.status = ib_event->param.rej_rcvd.reason;
2238 		event.event = RDMA_CM_EVENT_REJECTED;
2239 		event.param.conn.private_data = ib_event->private_data;
2240 		event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
2241 		break;
2242 	default:
2243 		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
2244 		       ib_event->event);
2245 		goto out;
2246 	}
2247 
2248 	ret = cma_cm_event_handler(id_priv, &event);
2249 	if (ret) {
2250 		/* Destroy the CM ID by returning a non-zero value. */
2251 		id_priv->cm_id.ib = NULL;
2252 		destroy_id_handler_unlock(id_priv);
2253 		return ret;
2254 	}
2255 out:
2256 	mutex_unlock(&id_priv->handler_mutex);
2257 	return 0;
2258 }
2259 
2260 static struct rdma_id_private *
cma_ib_new_conn_id(const struct rdma_cm_id * listen_id,const struct ib_cm_event * ib_event,struct net_device * net_dev)2261 cma_ib_new_conn_id(const struct rdma_cm_id *listen_id,
2262 		   const struct ib_cm_event *ib_event,
2263 		   struct net_device *net_dev)
2264 {
2265 	struct rdma_id_private *listen_id_priv;
2266 	struct rdma_id_private *id_priv;
2267 	struct rdma_cm_id *id;
2268 	struct rdma_route *rt;
2269 	const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
2270 	struct sa_path_rec *path = ib_event->param.req_rcvd.primary_path;
2271 	const __be64 service_id =
2272 		ib_event->param.req_rcvd.primary_path->service_id;
2273 	int ret;
2274 
2275 	listen_id_priv = container_of(listen_id, struct rdma_id_private, id);
2276 	id_priv = __rdma_create_id(listen_id->route.addr.dev_addr.net,
2277 				   listen_id->event_handler, listen_id->context,
2278 				   listen_id->ps,
2279 				   ib_event->param.req_rcvd.qp_type,
2280 				   listen_id_priv);
2281 	if (IS_ERR(id_priv))
2282 		return NULL;
2283 
2284 	id = &id_priv->id;
2285 	if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
2286 			      (struct sockaddr *)&id->route.addr.dst_addr,
2287 			      listen_id, ib_event, ss_family, service_id))
2288 		goto err;
2289 
2290 	rt = &id->route;
2291 	rt->num_pri_alt_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
2292 	rt->path_rec = kmalloc_array(rt->num_pri_alt_paths,
2293 				     sizeof(*rt->path_rec), GFP_KERNEL);
2294 	if (!rt->path_rec)
2295 		goto err;
2296 
2297 	rt->path_rec[0] = *path;
2298 	if (rt->num_pri_alt_paths == 2)
2299 		rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
2300 
2301 	if (net_dev) {
2302 		rdma_copy_src_l2_addr(&rt->addr.dev_addr, net_dev);
2303 	} else {
2304 		if (!cma_protocol_roce(listen_id) &&
2305 		    cma_any_addr(cma_src_addr(id_priv))) {
2306 			rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
2307 			rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
2308 			ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
2309 		} else if (!cma_any_addr(cma_src_addr(id_priv))) {
2310 			ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
2311 			if (ret)
2312 				goto err;
2313 		}
2314 	}
2315 	rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
2316 
2317 	id_priv->state = RDMA_CM_CONNECT;
2318 	return id_priv;
2319 
2320 err:
2321 	rdma_destroy_id(id);
2322 	return NULL;
2323 }
2324 
2325 static struct rdma_id_private *
cma_ib_new_udp_id(const struct rdma_cm_id * listen_id,const struct ib_cm_event * ib_event,struct net_device * net_dev)2326 cma_ib_new_udp_id(const struct rdma_cm_id *listen_id,
2327 		  const struct ib_cm_event *ib_event,
2328 		  struct net_device *net_dev)
2329 {
2330 	const struct rdma_id_private *listen_id_priv;
2331 	struct rdma_id_private *id_priv;
2332 	struct rdma_cm_id *id;
2333 	const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
2334 	struct net *net = listen_id->route.addr.dev_addr.net;
2335 	int ret;
2336 
2337 	listen_id_priv = container_of(listen_id, struct rdma_id_private, id);
2338 	id_priv = __rdma_create_id(net, listen_id->event_handler,
2339 				   listen_id->context, listen_id->ps, IB_QPT_UD,
2340 				   listen_id_priv);
2341 	if (IS_ERR(id_priv))
2342 		return NULL;
2343 
2344 	id = &id_priv->id;
2345 	if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
2346 			      (struct sockaddr *)&id->route.addr.dst_addr,
2347 			      listen_id, ib_event, ss_family,
2348 			      ib_event->param.sidr_req_rcvd.service_id))
2349 		goto err;
2350 
2351 	if (net_dev) {
2352 		rdma_copy_src_l2_addr(&id->route.addr.dev_addr, net_dev);
2353 	} else {
2354 		if (!cma_any_addr(cma_src_addr(id_priv))) {
2355 			ret = cma_translate_addr(cma_src_addr(id_priv),
2356 						 &id->route.addr.dev_addr);
2357 			if (ret)
2358 				goto err;
2359 		}
2360 	}
2361 
2362 	id_priv->state = RDMA_CM_CONNECT;
2363 	return id_priv;
2364 err:
2365 	rdma_destroy_id(id);
2366 	return NULL;
2367 }
2368 
cma_set_req_event_data(struct rdma_cm_event * event,const struct ib_cm_req_event_param * req_data,void * private_data,int offset)2369 static void cma_set_req_event_data(struct rdma_cm_event *event,
2370 				   const struct ib_cm_req_event_param *req_data,
2371 				   void *private_data, int offset)
2372 {
2373 	event->param.conn.private_data = private_data + offset;
2374 	event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
2375 	event->param.conn.responder_resources = req_data->responder_resources;
2376 	event->param.conn.initiator_depth = req_data->initiator_depth;
2377 	event->param.conn.flow_control = req_data->flow_control;
2378 	event->param.conn.retry_count = req_data->retry_count;
2379 	event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
2380 	event->param.conn.srq = req_data->srq;
2381 	event->param.conn.qp_num = req_data->remote_qpn;
2382 
2383 	event->ece.vendor_id = req_data->ece.vendor_id;
2384 	event->ece.attr_mod = req_data->ece.attr_mod;
2385 }
2386 
cma_ib_check_req_qp_type(const struct rdma_cm_id * id,const struct ib_cm_event * ib_event)2387 static int cma_ib_check_req_qp_type(const struct rdma_cm_id *id,
2388 				    const struct ib_cm_event *ib_event)
2389 {
2390 	return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
2391 		 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
2392 		((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
2393 		 (id->qp_type == IB_QPT_UD)) ||
2394 		(!id->qp_type));
2395 }
2396 
cma_ib_req_handler(struct ib_cm_id * cm_id,const struct ib_cm_event * ib_event)2397 static int cma_ib_req_handler(struct ib_cm_id *cm_id,
2398 			      const struct ib_cm_event *ib_event)
2399 {
2400 	struct rdma_id_private *listen_id, *conn_id = NULL;
2401 	struct rdma_cm_event event = {};
2402 	struct cma_req_info req = {};
2403 	struct net_device *net_dev;
2404 	u8 offset;
2405 	int ret;
2406 
2407 	listen_id = cma_ib_id_from_event(cm_id, ib_event, &req, &net_dev);
2408 	if (IS_ERR(listen_id))
2409 		return PTR_ERR(listen_id);
2410 
2411 	trace_cm_req_handler(listen_id, ib_event->event);
2412 	if (!cma_ib_check_req_qp_type(&listen_id->id, ib_event)) {
2413 		ret = -EINVAL;
2414 		goto net_dev_put;
2415 	}
2416 
2417 	mutex_lock(&listen_id->handler_mutex);
2418 	if (READ_ONCE(listen_id->state) != RDMA_CM_LISTEN) {
2419 		ret = -ECONNABORTED;
2420 		goto err_unlock;
2421 	}
2422 
2423 	offset = cma_user_data_offset(listen_id);
2424 	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2425 	if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
2426 		conn_id = cma_ib_new_udp_id(&listen_id->id, ib_event, net_dev);
2427 		event.param.ud.private_data = ib_event->private_data + offset;
2428 		event.param.ud.private_data_len =
2429 				IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
2430 	} else {
2431 		conn_id = cma_ib_new_conn_id(&listen_id->id, ib_event, net_dev);
2432 		cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
2433 				       ib_event->private_data, offset);
2434 	}
2435 	if (!conn_id) {
2436 		ret = -ENOMEM;
2437 		goto err_unlock;
2438 	}
2439 
2440 	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2441 	ret = cma_ib_acquire_dev(conn_id, listen_id, &req);
2442 	if (ret) {
2443 		destroy_id_handler_unlock(conn_id);
2444 		goto err_unlock;
2445 	}
2446 
2447 	conn_id->cm_id.ib = cm_id;
2448 	cm_id->context = conn_id;
2449 	cm_id->cm_handler = cma_ib_handler;
2450 
2451 	ret = cma_cm_event_handler(conn_id, &event);
2452 	if (ret) {
2453 		/* Destroy the CM ID by returning a non-zero value. */
2454 		conn_id->cm_id.ib = NULL;
2455 		mutex_unlock(&listen_id->handler_mutex);
2456 		destroy_id_handler_unlock(conn_id);
2457 		goto net_dev_put;
2458 	}
2459 
2460 	if (READ_ONCE(conn_id->state) == RDMA_CM_CONNECT &&
2461 	    conn_id->id.qp_type != IB_QPT_UD) {
2462 		trace_cm_send_mra(cm_id->context);
2463 		ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
2464 	}
2465 	mutex_unlock(&conn_id->handler_mutex);
2466 
2467 err_unlock:
2468 	mutex_unlock(&listen_id->handler_mutex);
2469 
2470 net_dev_put:
2471 	dev_put(net_dev);
2472 
2473 	return ret;
2474 }
2475 
rdma_get_service_id(struct rdma_cm_id * id,struct sockaddr * addr)2476 __be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
2477 {
2478 	if (addr->sa_family == AF_IB)
2479 		return ((struct sockaddr_ib *) addr)->sib_sid;
2480 
2481 	return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
2482 }
2483 EXPORT_SYMBOL(rdma_get_service_id);
2484 
rdma_read_gids(struct rdma_cm_id * cm_id,union ib_gid * sgid,union ib_gid * dgid)2485 void rdma_read_gids(struct rdma_cm_id *cm_id, union ib_gid *sgid,
2486 		    union ib_gid *dgid)
2487 {
2488 	struct rdma_addr *addr = &cm_id->route.addr;
2489 
2490 	if (!cm_id->device) {
2491 		if (sgid)
2492 			memset(sgid, 0, sizeof(*sgid));
2493 		if (dgid)
2494 			memset(dgid, 0, sizeof(*dgid));
2495 		return;
2496 	}
2497 
2498 	if (rdma_protocol_roce(cm_id->device, cm_id->port_num)) {
2499 		if (sgid)
2500 			rdma_ip2gid((struct sockaddr *)&addr->src_addr, sgid);
2501 		if (dgid)
2502 			rdma_ip2gid((struct sockaddr *)&addr->dst_addr, dgid);
2503 	} else {
2504 		if (sgid)
2505 			rdma_addr_get_sgid(&addr->dev_addr, sgid);
2506 		if (dgid)
2507 			rdma_addr_get_dgid(&addr->dev_addr, dgid);
2508 	}
2509 }
2510 EXPORT_SYMBOL(rdma_read_gids);
2511 
cma_iw_handler(struct iw_cm_id * iw_id,struct iw_cm_event * iw_event)2512 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
2513 {
2514 	struct rdma_id_private *id_priv = iw_id->context;
2515 	struct rdma_cm_event event = {};
2516 	int ret = 0;
2517 	struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2518 	struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2519 
2520 	mutex_lock(&id_priv->handler_mutex);
2521 	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
2522 		goto out;
2523 
2524 	switch (iw_event->event) {
2525 	case IW_CM_EVENT_CLOSE:
2526 		event.event = RDMA_CM_EVENT_DISCONNECTED;
2527 		break;
2528 	case IW_CM_EVENT_CONNECT_REPLY:
2529 		memcpy(cma_src_addr(id_priv), laddr,
2530 		       rdma_addr_size(laddr));
2531 		memcpy(cma_dst_addr(id_priv), raddr,
2532 		       rdma_addr_size(raddr));
2533 		switch (iw_event->status) {
2534 		case 0:
2535 			event.event = RDMA_CM_EVENT_ESTABLISHED;
2536 			event.param.conn.initiator_depth = iw_event->ird;
2537 			event.param.conn.responder_resources = iw_event->ord;
2538 			break;
2539 		case -ECONNRESET:
2540 		case -ECONNREFUSED:
2541 			event.event = RDMA_CM_EVENT_REJECTED;
2542 			break;
2543 		case -ETIMEDOUT:
2544 			event.event = RDMA_CM_EVENT_UNREACHABLE;
2545 			break;
2546 		default:
2547 			event.event = RDMA_CM_EVENT_CONNECT_ERROR;
2548 			break;
2549 		}
2550 		break;
2551 	case IW_CM_EVENT_ESTABLISHED:
2552 		event.event = RDMA_CM_EVENT_ESTABLISHED;
2553 		event.param.conn.initiator_depth = iw_event->ird;
2554 		event.param.conn.responder_resources = iw_event->ord;
2555 		break;
2556 	default:
2557 		goto out;
2558 	}
2559 
2560 	event.status = iw_event->status;
2561 	event.param.conn.private_data = iw_event->private_data;
2562 	event.param.conn.private_data_len = iw_event->private_data_len;
2563 	ret = cma_cm_event_handler(id_priv, &event);
2564 	if (ret) {
2565 		/* Destroy the CM ID by returning a non-zero value. */
2566 		id_priv->cm_id.iw = NULL;
2567 		destroy_id_handler_unlock(id_priv);
2568 		return ret;
2569 	}
2570 
2571 out:
2572 	mutex_unlock(&id_priv->handler_mutex);
2573 	return ret;
2574 }
2575 
iw_conn_req_handler(struct iw_cm_id * cm_id,struct iw_cm_event * iw_event)2576 static int iw_conn_req_handler(struct iw_cm_id *cm_id,
2577 			       struct iw_cm_event *iw_event)
2578 {
2579 	struct rdma_id_private *listen_id, *conn_id;
2580 	struct rdma_cm_event event = {};
2581 	int ret = -ECONNABORTED;
2582 	struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2583 	struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2584 
2585 	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2586 	event.param.conn.private_data = iw_event->private_data;
2587 	event.param.conn.private_data_len = iw_event->private_data_len;
2588 	event.param.conn.initiator_depth = iw_event->ird;
2589 	event.param.conn.responder_resources = iw_event->ord;
2590 
2591 	listen_id = cm_id->context;
2592 
2593 	mutex_lock(&listen_id->handler_mutex);
2594 	if (READ_ONCE(listen_id->state) != RDMA_CM_LISTEN)
2595 		goto out;
2596 
2597 	/* Create a new RDMA id for the new IW CM ID */
2598 	conn_id = __rdma_create_id(listen_id->id.route.addr.dev_addr.net,
2599 				   listen_id->id.event_handler,
2600 				   listen_id->id.context, RDMA_PS_TCP,
2601 				   IB_QPT_RC, listen_id);
2602 	if (IS_ERR(conn_id)) {
2603 		ret = -ENOMEM;
2604 		goto out;
2605 	}
2606 	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2607 	conn_id->state = RDMA_CM_CONNECT;
2608 
2609 	ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr);
2610 	if (ret) {
2611 		mutex_unlock(&listen_id->handler_mutex);
2612 		destroy_id_handler_unlock(conn_id);
2613 		return ret;
2614 	}
2615 
2616 	ret = cma_iw_acquire_dev(conn_id, listen_id);
2617 	if (ret) {
2618 		mutex_unlock(&listen_id->handler_mutex);
2619 		destroy_id_handler_unlock(conn_id);
2620 		return ret;
2621 	}
2622 
2623 	conn_id->cm_id.iw = cm_id;
2624 	cm_id->context = conn_id;
2625 	cm_id->cm_handler = cma_iw_handler;
2626 
2627 	memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
2628 	memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
2629 
2630 	ret = cma_cm_event_handler(conn_id, &event);
2631 	if (ret) {
2632 		/* User wants to destroy the CM ID */
2633 		conn_id->cm_id.iw = NULL;
2634 		mutex_unlock(&listen_id->handler_mutex);
2635 		destroy_id_handler_unlock(conn_id);
2636 		return ret;
2637 	}
2638 
2639 	mutex_unlock(&conn_id->handler_mutex);
2640 
2641 out:
2642 	mutex_unlock(&listen_id->handler_mutex);
2643 	return ret;
2644 }
2645 
cma_ib_listen(struct rdma_id_private * id_priv)2646 static int cma_ib_listen(struct rdma_id_private *id_priv)
2647 {
2648 	struct sockaddr *addr;
2649 	struct ib_cm_id	*id;
2650 	__be64 svc_id;
2651 
2652 	addr = cma_src_addr(id_priv);
2653 	svc_id = rdma_get_service_id(&id_priv->id, addr);
2654 	id = ib_cm_insert_listen(id_priv->id.device,
2655 				 cma_ib_req_handler, svc_id);
2656 	if (IS_ERR(id))
2657 		return PTR_ERR(id);
2658 	id_priv->cm_id.ib = id;
2659 
2660 	return 0;
2661 }
2662 
cma_iw_listen(struct rdma_id_private * id_priv,int backlog)2663 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
2664 {
2665 	int ret;
2666 	struct iw_cm_id	*id;
2667 
2668 	id = iw_create_cm_id(id_priv->id.device,
2669 			     iw_conn_req_handler,
2670 			     id_priv);
2671 	if (IS_ERR(id))
2672 		return PTR_ERR(id);
2673 
2674 	mutex_lock(&id_priv->qp_mutex);
2675 	id->tos = id_priv->tos;
2676 	id->tos_set = id_priv->tos_set;
2677 	mutex_unlock(&id_priv->qp_mutex);
2678 	id->afonly = id_priv->afonly;
2679 	id_priv->cm_id.iw = id;
2680 
2681 	memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
2682 	       rdma_addr_size(cma_src_addr(id_priv)));
2683 
2684 	ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
2685 
2686 	if (ret) {
2687 		iw_destroy_cm_id(id_priv->cm_id.iw);
2688 		id_priv->cm_id.iw = NULL;
2689 	}
2690 
2691 	return ret;
2692 }
2693 
cma_listen_handler(struct rdma_cm_id * id,struct rdma_cm_event * event)2694 static int cma_listen_handler(struct rdma_cm_id *id,
2695 			      struct rdma_cm_event *event)
2696 {
2697 	struct rdma_id_private *id_priv = id->context;
2698 
2699 	/* Listening IDs are always destroyed on removal */
2700 	if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
2701 		return -1;
2702 
2703 	id->context = id_priv->id.context;
2704 	id->event_handler = id_priv->id.event_handler;
2705 	trace_cm_event_handler(id_priv, event);
2706 	return id_priv->id.event_handler(id, event);
2707 }
2708 
cma_listen_on_dev(struct rdma_id_private * id_priv,struct cma_device * cma_dev,struct rdma_id_private ** to_destroy)2709 static int cma_listen_on_dev(struct rdma_id_private *id_priv,
2710 			     struct cma_device *cma_dev,
2711 			     struct rdma_id_private **to_destroy)
2712 {
2713 	struct rdma_id_private *dev_id_priv;
2714 	struct net *net = id_priv->id.route.addr.dev_addr.net;
2715 	int ret;
2716 
2717 	lockdep_assert_held(&lock);
2718 
2719 	*to_destroy = NULL;
2720 	if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
2721 		return 0;
2722 
2723 	dev_id_priv =
2724 		__rdma_create_id(net, cma_listen_handler, id_priv,
2725 				 id_priv->id.ps, id_priv->id.qp_type, id_priv);
2726 	if (IS_ERR(dev_id_priv))
2727 		return PTR_ERR(dev_id_priv);
2728 
2729 	dev_id_priv->state = RDMA_CM_ADDR_BOUND;
2730 	memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2731 	       rdma_addr_size(cma_src_addr(id_priv)));
2732 
2733 	_cma_attach_to_dev(dev_id_priv, cma_dev);
2734 	rdma_restrack_add(&dev_id_priv->res);
2735 	cma_id_get(id_priv);
2736 	dev_id_priv->internal_id = 1;
2737 	dev_id_priv->afonly = id_priv->afonly;
2738 	mutex_lock(&id_priv->qp_mutex);
2739 	dev_id_priv->tos_set = id_priv->tos_set;
2740 	dev_id_priv->tos = id_priv->tos;
2741 	mutex_unlock(&id_priv->qp_mutex);
2742 
2743 	ret = rdma_listen(&dev_id_priv->id, id_priv->backlog);
2744 	if (ret)
2745 		goto err_listen;
2746 	list_add_tail(&dev_id_priv->listen_item, &id_priv->listen_list);
2747 	return 0;
2748 err_listen:
2749 	/* Caller must destroy this after releasing lock */
2750 	*to_destroy = dev_id_priv;
2751 	dev_warn(&cma_dev->device->dev, "RDMA CMA: %s, error %d\n", __func__, ret);
2752 	return ret;
2753 }
2754 
cma_listen_on_all(struct rdma_id_private * id_priv)2755 static int cma_listen_on_all(struct rdma_id_private *id_priv)
2756 {
2757 	struct rdma_id_private *to_destroy;
2758 	struct cma_device *cma_dev;
2759 	int ret;
2760 
2761 	mutex_lock(&lock);
2762 	list_add_tail(&id_priv->listen_any_item, &listen_any_list);
2763 	list_for_each_entry(cma_dev, &dev_list, list) {
2764 		ret = cma_listen_on_dev(id_priv, cma_dev, &to_destroy);
2765 		if (ret) {
2766 			/* Prevent racing with cma_process_remove() */
2767 			if (to_destroy)
2768 				list_del_init(&to_destroy->device_item);
2769 			goto err_listen;
2770 		}
2771 	}
2772 	mutex_unlock(&lock);
2773 	return 0;
2774 
2775 err_listen:
2776 	_cma_cancel_listens(id_priv);
2777 	mutex_unlock(&lock);
2778 	if (to_destroy)
2779 		rdma_destroy_id(&to_destroy->id);
2780 	return ret;
2781 }
2782 
rdma_set_service_type(struct rdma_cm_id * id,int tos)2783 void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2784 {
2785 	struct rdma_id_private *id_priv;
2786 
2787 	id_priv = container_of(id, struct rdma_id_private, id);
2788 	mutex_lock(&id_priv->qp_mutex);
2789 	id_priv->tos = (u8) tos;
2790 	id_priv->tos_set = true;
2791 	mutex_unlock(&id_priv->qp_mutex);
2792 }
2793 EXPORT_SYMBOL(rdma_set_service_type);
2794 
2795 /**
2796  * rdma_set_ack_timeout() - Set the ack timeout of QP associated
2797  *                          with a connection identifier.
2798  * @id: Communication identifier to associated with service type.
2799  * @timeout: Ack timeout to set a QP, expressed as 4.096 * 2^(timeout) usec.
2800  *
2801  * This function should be called before rdma_connect() on active side,
2802  * and on passive side before rdma_accept(). It is applicable to primary
2803  * path only. The timeout will affect the local side of the QP, it is not
2804  * negotiated with remote side and zero disables the timer. In case it is
2805  * set before rdma_resolve_route, the value will also be used to determine
2806  * PacketLifeTime for RoCE.
2807  *
2808  * Return: 0 for success
2809  */
rdma_set_ack_timeout(struct rdma_cm_id * id,u8 timeout)2810 int rdma_set_ack_timeout(struct rdma_cm_id *id, u8 timeout)
2811 {
2812 	struct rdma_id_private *id_priv;
2813 
2814 	if (id->qp_type != IB_QPT_RC && id->qp_type != IB_QPT_XRC_INI)
2815 		return -EINVAL;
2816 
2817 	id_priv = container_of(id, struct rdma_id_private, id);
2818 	mutex_lock(&id_priv->qp_mutex);
2819 	id_priv->timeout = timeout;
2820 	id_priv->timeout_set = true;
2821 	mutex_unlock(&id_priv->qp_mutex);
2822 
2823 	return 0;
2824 }
2825 EXPORT_SYMBOL(rdma_set_ack_timeout);
2826 
2827 /**
2828  * rdma_set_min_rnr_timer() - Set the minimum RNR Retry timer of the
2829  *			      QP associated with a connection identifier.
2830  * @id: Communication identifier to associated with service type.
2831  * @min_rnr_timer: 5-bit value encoded as Table 45: "Encoding for RNR NAK
2832  *		   Timer Field" in the IBTA specification.
2833  *
2834  * This function should be called before rdma_connect() on active
2835  * side, and on passive side before rdma_accept(). The timer value
2836  * will be associated with the local QP. When it receives a send it is
2837  * not read to handle, typically if the receive queue is empty, an RNR
2838  * Retry NAK is returned to the requester with the min_rnr_timer
2839  * encoded. The requester will then wait at least the time specified
2840  * in the NAK before retrying. The default is zero, which translates
2841  * to a minimum RNR Timer value of 655 ms.
2842  *
2843  * Return: 0 for success
2844  */
rdma_set_min_rnr_timer(struct rdma_cm_id * id,u8 min_rnr_timer)2845 int rdma_set_min_rnr_timer(struct rdma_cm_id *id, u8 min_rnr_timer)
2846 {
2847 	struct rdma_id_private *id_priv;
2848 
2849 	/* It is a five-bit value */
2850 	if (min_rnr_timer & 0xe0)
2851 		return -EINVAL;
2852 
2853 	if (WARN_ON(id->qp_type != IB_QPT_RC && id->qp_type != IB_QPT_XRC_TGT))
2854 		return -EINVAL;
2855 
2856 	id_priv = container_of(id, struct rdma_id_private, id);
2857 	mutex_lock(&id_priv->qp_mutex);
2858 	id_priv->min_rnr_timer = min_rnr_timer;
2859 	id_priv->min_rnr_timer_set = true;
2860 	mutex_unlock(&id_priv->qp_mutex);
2861 
2862 	return 0;
2863 }
2864 EXPORT_SYMBOL(rdma_set_min_rnr_timer);
2865 
route_set_path_rec_inbound(struct cma_work * work,struct sa_path_rec * path_rec)2866 static int route_set_path_rec_inbound(struct cma_work *work,
2867 				      struct sa_path_rec *path_rec)
2868 {
2869 	struct rdma_route *route = &work->id->id.route;
2870 
2871 	if (!route->path_rec_inbound) {
2872 		route->path_rec_inbound =
2873 			kzalloc(sizeof(*route->path_rec_inbound), GFP_KERNEL);
2874 		if (!route->path_rec_inbound)
2875 			return -ENOMEM;
2876 	}
2877 
2878 	*route->path_rec_inbound = *path_rec;
2879 	return 0;
2880 }
2881 
route_set_path_rec_outbound(struct cma_work * work,struct sa_path_rec * path_rec)2882 static int route_set_path_rec_outbound(struct cma_work *work,
2883 				       struct sa_path_rec *path_rec)
2884 {
2885 	struct rdma_route *route = &work->id->id.route;
2886 
2887 	if (!route->path_rec_outbound) {
2888 		route->path_rec_outbound =
2889 			kzalloc(sizeof(*route->path_rec_outbound), GFP_KERNEL);
2890 		if (!route->path_rec_outbound)
2891 			return -ENOMEM;
2892 	}
2893 
2894 	*route->path_rec_outbound = *path_rec;
2895 	return 0;
2896 }
2897 
cma_query_handler(int status,struct sa_path_rec * path_rec,unsigned int num_prs,void * context)2898 static void cma_query_handler(int status, struct sa_path_rec *path_rec,
2899 			      unsigned int num_prs, void *context)
2900 {
2901 	struct cma_work *work = context;
2902 	struct rdma_route *route;
2903 	int i;
2904 
2905 	route = &work->id->id.route;
2906 
2907 	if (status)
2908 		goto fail;
2909 
2910 	for (i = 0; i < num_prs; i++) {
2911 		if (!path_rec[i].flags || (path_rec[i].flags & IB_PATH_GMP))
2912 			*route->path_rec = path_rec[i];
2913 		else if (path_rec[i].flags & IB_PATH_INBOUND)
2914 			status = route_set_path_rec_inbound(work, &path_rec[i]);
2915 		else if (path_rec[i].flags & IB_PATH_OUTBOUND)
2916 			status = route_set_path_rec_outbound(work,
2917 							     &path_rec[i]);
2918 		else
2919 			status = -EINVAL;
2920 
2921 		if (status)
2922 			goto fail;
2923 	}
2924 
2925 	route->num_pri_alt_paths = 1;
2926 	queue_work(cma_wq, &work->work);
2927 	return;
2928 
2929 fail:
2930 	work->old_state = RDMA_CM_ROUTE_QUERY;
2931 	work->new_state = RDMA_CM_ADDR_RESOLVED;
2932 	work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
2933 	work->event.status = status;
2934 	pr_debug_ratelimited("RDMA CM: ROUTE_ERROR: failed to query path. status %d\n",
2935 			     status);
2936 	queue_work(cma_wq, &work->work);
2937 }
2938 
cma_query_ib_route(struct rdma_id_private * id_priv,unsigned long timeout_ms,struct cma_work * work)2939 static int cma_query_ib_route(struct rdma_id_private *id_priv,
2940 			      unsigned long timeout_ms, struct cma_work *work)
2941 {
2942 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2943 	struct sa_path_rec path_rec;
2944 	ib_sa_comp_mask comp_mask;
2945 	struct sockaddr_in6 *sin6;
2946 	struct sockaddr_ib *sib;
2947 
2948 	memset(&path_rec, 0, sizeof path_rec);
2949 
2950 	if (rdma_cap_opa_ah(id_priv->id.device, id_priv->id.port_num))
2951 		path_rec.rec_type = SA_PATH_REC_TYPE_OPA;
2952 	else
2953 		path_rec.rec_type = SA_PATH_REC_TYPE_IB;
2954 	rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2955 	rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2956 	path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2957 	path_rec.numb_path = 1;
2958 	path_rec.reversible = 1;
2959 	path_rec.service_id = rdma_get_service_id(&id_priv->id,
2960 						  cma_dst_addr(id_priv));
2961 
2962 	comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2963 		    IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2964 		    IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2965 
2966 	switch (cma_family(id_priv)) {
2967 	case AF_INET:
2968 		path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2969 		comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
2970 		break;
2971 	case AF_INET6:
2972 		sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2973 		path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2974 		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2975 		break;
2976 	case AF_IB:
2977 		sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2978 		path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2979 		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2980 		break;
2981 	}
2982 
2983 	id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
2984 					       id_priv->id.port_num, &path_rec,
2985 					       comp_mask, timeout_ms,
2986 					       GFP_KERNEL, cma_query_handler,
2987 					       work, &id_priv->query);
2988 
2989 	return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2990 }
2991 
cma_iboe_join_work_handler(struct work_struct * work)2992 static void cma_iboe_join_work_handler(struct work_struct *work)
2993 {
2994 	struct cma_multicast *mc =
2995 		container_of(work, struct cma_multicast, iboe_join.work);
2996 	struct rdma_cm_event *event = &mc->iboe_join.event;
2997 	struct rdma_id_private *id_priv = mc->id_priv;
2998 	int ret;
2999 
3000 	mutex_lock(&id_priv->handler_mutex);
3001 	if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
3002 	    READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
3003 		goto out_unlock;
3004 
3005 	ret = cma_cm_event_handler(id_priv, event);
3006 	WARN_ON(ret);
3007 
3008 out_unlock:
3009 	mutex_unlock(&id_priv->handler_mutex);
3010 	if (event->event == RDMA_CM_EVENT_MULTICAST_JOIN)
3011 		rdma_destroy_ah_attr(&event->param.ud.ah_attr);
3012 }
3013 
cma_work_handler(struct work_struct * _work)3014 static void cma_work_handler(struct work_struct *_work)
3015 {
3016 	struct cma_work *work = container_of(_work, struct cma_work, work);
3017 	struct rdma_id_private *id_priv = work->id;
3018 
3019 	mutex_lock(&id_priv->handler_mutex);
3020 	if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
3021 	    READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
3022 		goto out_unlock;
3023 	if (work->old_state != 0 || work->new_state != 0) {
3024 		if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
3025 			goto out_unlock;
3026 	}
3027 
3028 	if (cma_cm_event_handler(id_priv, &work->event)) {
3029 		cma_id_put(id_priv);
3030 		destroy_id_handler_unlock(id_priv);
3031 		goto out_free;
3032 	}
3033 
3034 out_unlock:
3035 	mutex_unlock(&id_priv->handler_mutex);
3036 	cma_id_put(id_priv);
3037 out_free:
3038 	if (work->event.event == RDMA_CM_EVENT_MULTICAST_JOIN)
3039 		rdma_destroy_ah_attr(&work->event.param.ud.ah_attr);
3040 	kfree(work);
3041 }
3042 
cma_init_resolve_route_work(struct cma_work * work,struct rdma_id_private * id_priv)3043 static void cma_init_resolve_route_work(struct cma_work *work,
3044 					struct rdma_id_private *id_priv)
3045 {
3046 	work->id = id_priv;
3047 	INIT_WORK(&work->work, cma_work_handler);
3048 	work->old_state = RDMA_CM_ROUTE_QUERY;
3049 	work->new_state = RDMA_CM_ROUTE_RESOLVED;
3050 	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
3051 }
3052 
enqueue_resolve_addr_work(struct cma_work * work,struct rdma_id_private * id_priv)3053 static void enqueue_resolve_addr_work(struct cma_work *work,
3054 				      struct rdma_id_private *id_priv)
3055 {
3056 	/* Balances with cma_id_put() in cma_work_handler */
3057 	cma_id_get(id_priv);
3058 
3059 	work->id = id_priv;
3060 	INIT_WORK(&work->work, cma_work_handler);
3061 	work->old_state = RDMA_CM_ADDR_QUERY;
3062 	work->new_state = RDMA_CM_ADDR_RESOLVED;
3063 	work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
3064 
3065 	queue_work(cma_wq, &work->work);
3066 }
3067 
cma_resolve_ib_route(struct rdma_id_private * id_priv,unsigned long timeout_ms)3068 static int cma_resolve_ib_route(struct rdma_id_private *id_priv,
3069 				unsigned long timeout_ms)
3070 {
3071 	struct rdma_route *route = &id_priv->id.route;
3072 	struct cma_work *work;
3073 	int ret;
3074 
3075 	work = kzalloc(sizeof *work, GFP_KERNEL);
3076 	if (!work)
3077 		return -ENOMEM;
3078 
3079 	cma_init_resolve_route_work(work, id_priv);
3080 
3081 	if (!route->path_rec)
3082 		route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
3083 	if (!route->path_rec) {
3084 		ret = -ENOMEM;
3085 		goto err1;
3086 	}
3087 
3088 	ret = cma_query_ib_route(id_priv, timeout_ms, work);
3089 	if (ret)
3090 		goto err2;
3091 
3092 	return 0;
3093 err2:
3094 	kfree(route->path_rec);
3095 	route->path_rec = NULL;
3096 err1:
3097 	kfree(work);
3098 	return ret;
3099 }
3100 
cma_route_gid_type(enum rdma_network_type network_type,unsigned long supported_gids,enum ib_gid_type default_gid)3101 static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
3102 					   unsigned long supported_gids,
3103 					   enum ib_gid_type default_gid)
3104 {
3105 	if ((network_type == RDMA_NETWORK_IPV4 ||
3106 	     network_type == RDMA_NETWORK_IPV6) &&
3107 	    test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
3108 		return IB_GID_TYPE_ROCE_UDP_ENCAP;
3109 
3110 	return default_gid;
3111 }
3112 
3113 /*
3114  * cma_iboe_set_path_rec_l2_fields() is helper function which sets
3115  * path record type based on GID type.
3116  * It also sets up other L2 fields which includes destination mac address
3117  * netdev ifindex, of the path record.
3118  * It returns the netdev of the bound interface for this path record entry.
3119  */
3120 static struct net_device *
cma_iboe_set_path_rec_l2_fields(struct rdma_id_private * id_priv)3121 cma_iboe_set_path_rec_l2_fields(struct rdma_id_private *id_priv)
3122 {
3123 	struct rdma_route *route = &id_priv->id.route;
3124 	enum ib_gid_type gid_type = IB_GID_TYPE_ROCE;
3125 	struct rdma_addr *addr = &route->addr;
3126 	unsigned long supported_gids;
3127 	struct net_device *ndev;
3128 
3129 	if (!addr->dev_addr.bound_dev_if)
3130 		return NULL;
3131 
3132 	ndev = dev_get_by_index(addr->dev_addr.net,
3133 				addr->dev_addr.bound_dev_if);
3134 	if (!ndev)
3135 		return NULL;
3136 
3137 	supported_gids = roce_gid_type_mask_support(id_priv->id.device,
3138 						    id_priv->id.port_num);
3139 	gid_type = cma_route_gid_type(addr->dev_addr.network,
3140 				      supported_gids,
3141 				      id_priv->gid_type);
3142 	/* Use the hint from IP Stack to select GID Type */
3143 	if (gid_type < ib_network_to_gid_type(addr->dev_addr.network))
3144 		gid_type = ib_network_to_gid_type(addr->dev_addr.network);
3145 	route->path_rec->rec_type = sa_conv_gid_to_pathrec_type(gid_type);
3146 
3147 	route->path_rec->roce.route_resolved = true;
3148 	sa_path_set_dmac(route->path_rec, addr->dev_addr.dst_dev_addr);
3149 	return ndev;
3150 }
3151 
rdma_set_ib_path(struct rdma_cm_id * id,struct sa_path_rec * path_rec)3152 int rdma_set_ib_path(struct rdma_cm_id *id,
3153 		     struct sa_path_rec *path_rec)
3154 {
3155 	struct rdma_id_private *id_priv;
3156 	struct net_device *ndev;
3157 	int ret;
3158 
3159 	id_priv = container_of(id, struct rdma_id_private, id);
3160 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
3161 			   RDMA_CM_ROUTE_RESOLVED))
3162 		return -EINVAL;
3163 
3164 	id->route.path_rec = kmemdup(path_rec, sizeof(*path_rec),
3165 				     GFP_KERNEL);
3166 	if (!id->route.path_rec) {
3167 		ret = -ENOMEM;
3168 		goto err;
3169 	}
3170 
3171 	if (rdma_protocol_roce(id->device, id->port_num)) {
3172 		ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
3173 		if (!ndev) {
3174 			ret = -ENODEV;
3175 			goto err_free;
3176 		}
3177 		dev_put(ndev);
3178 	}
3179 
3180 	id->route.num_pri_alt_paths = 1;
3181 	return 0;
3182 
3183 err_free:
3184 	kfree(id->route.path_rec);
3185 	id->route.path_rec = NULL;
3186 err:
3187 	cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
3188 	return ret;
3189 }
3190 EXPORT_SYMBOL(rdma_set_ib_path);
3191 
cma_resolve_iw_route(struct rdma_id_private * id_priv)3192 static int cma_resolve_iw_route(struct rdma_id_private *id_priv)
3193 {
3194 	struct cma_work *work;
3195 
3196 	work = kzalloc(sizeof *work, GFP_KERNEL);
3197 	if (!work)
3198 		return -ENOMEM;
3199 
3200 	cma_init_resolve_route_work(work, id_priv);
3201 	queue_work(cma_wq, &work->work);
3202 	return 0;
3203 }
3204 
get_vlan_ndev_tc(struct net_device * vlan_ndev,int prio)3205 static int get_vlan_ndev_tc(struct net_device *vlan_ndev, int prio)
3206 {
3207 	struct net_device *dev;
3208 
3209 	dev = vlan_dev_real_dev(vlan_ndev);
3210 	if (dev->num_tc)
3211 		return netdev_get_prio_tc_map(dev, prio);
3212 
3213 	return (vlan_dev_get_egress_qos_mask(vlan_ndev, prio) &
3214 		VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
3215 }
3216 
3217 struct iboe_prio_tc_map {
3218 	int input_prio;
3219 	int output_tc;
3220 	bool found;
3221 };
3222 
get_lower_vlan_dev_tc(struct net_device * dev,struct netdev_nested_priv * priv)3223 static int get_lower_vlan_dev_tc(struct net_device *dev,
3224 				 struct netdev_nested_priv *priv)
3225 {
3226 	struct iboe_prio_tc_map *map = (struct iboe_prio_tc_map *)priv->data;
3227 
3228 	if (is_vlan_dev(dev))
3229 		map->output_tc = get_vlan_ndev_tc(dev, map->input_prio);
3230 	else if (dev->num_tc)
3231 		map->output_tc = netdev_get_prio_tc_map(dev, map->input_prio);
3232 	else
3233 		map->output_tc = 0;
3234 	/* We are interested only in first level VLAN device, so always
3235 	 * return 1 to stop iterating over next level devices.
3236 	 */
3237 	map->found = true;
3238 	return 1;
3239 }
3240 
iboe_tos_to_sl(struct net_device * ndev,int tos)3241 static int iboe_tos_to_sl(struct net_device *ndev, int tos)
3242 {
3243 	struct iboe_prio_tc_map prio_tc_map = {};
3244 	int prio = rt_tos2priority(tos);
3245 	struct netdev_nested_priv priv;
3246 
3247 	/* If VLAN device, get it directly from the VLAN netdev */
3248 	if (is_vlan_dev(ndev))
3249 		return get_vlan_ndev_tc(ndev, prio);
3250 
3251 	prio_tc_map.input_prio = prio;
3252 	priv.data = (void *)&prio_tc_map;
3253 	rcu_read_lock();
3254 	netdev_walk_all_lower_dev_rcu(ndev,
3255 				      get_lower_vlan_dev_tc,
3256 				      &priv);
3257 	rcu_read_unlock();
3258 	/* If map is found from lower device, use it; Otherwise
3259 	 * continue with the current netdevice to get priority to tc map.
3260 	 */
3261 	if (prio_tc_map.found)
3262 		return prio_tc_map.output_tc;
3263 	else if (ndev->num_tc)
3264 		return netdev_get_prio_tc_map(ndev, prio);
3265 	else
3266 		return 0;
3267 }
3268 
cma_get_roce_udp_flow_label(struct rdma_id_private * id_priv)3269 static __be32 cma_get_roce_udp_flow_label(struct rdma_id_private *id_priv)
3270 {
3271 	struct sockaddr_in6 *addr6;
3272 	u16 dport, sport;
3273 	u32 hash, fl;
3274 
3275 	addr6 = (struct sockaddr_in6 *)cma_src_addr(id_priv);
3276 	fl = be32_to_cpu(addr6->sin6_flowinfo) & IB_GRH_FLOWLABEL_MASK;
3277 	if ((cma_family(id_priv) != AF_INET6) || !fl) {
3278 		dport = be16_to_cpu(cma_port(cma_dst_addr(id_priv)));
3279 		sport = be16_to_cpu(cma_port(cma_src_addr(id_priv)));
3280 		hash = (u32)sport * 31 + dport;
3281 		fl = hash & IB_GRH_FLOWLABEL_MASK;
3282 	}
3283 
3284 	return cpu_to_be32(fl);
3285 }
3286 
cma_resolve_iboe_route(struct rdma_id_private * id_priv)3287 static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
3288 {
3289 	struct rdma_route *route = &id_priv->id.route;
3290 	struct rdma_addr *addr = &route->addr;
3291 	struct cma_work *work;
3292 	int ret;
3293 	struct net_device *ndev;
3294 
3295 	u8 default_roce_tos = id_priv->cma_dev->default_roce_tos[id_priv->id.port_num -
3296 					rdma_start_port(id_priv->cma_dev->device)];
3297 	u8 tos;
3298 
3299 	mutex_lock(&id_priv->qp_mutex);
3300 	tos = id_priv->tos_set ? id_priv->tos : default_roce_tos;
3301 	mutex_unlock(&id_priv->qp_mutex);
3302 
3303 	work = kzalloc(sizeof *work, GFP_KERNEL);
3304 	if (!work)
3305 		return -ENOMEM;
3306 
3307 	route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
3308 	if (!route->path_rec) {
3309 		ret = -ENOMEM;
3310 		goto err1;
3311 	}
3312 
3313 	route->num_pri_alt_paths = 1;
3314 
3315 	ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
3316 	if (!ndev) {
3317 		ret = -ENODEV;
3318 		goto err2;
3319 	}
3320 
3321 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
3322 		    &route->path_rec->sgid);
3323 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
3324 		    &route->path_rec->dgid);
3325 
3326 	if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
3327 		/* TODO: get the hoplimit from the inet/inet6 device */
3328 		route->path_rec->hop_limit = addr->dev_addr.hoplimit;
3329 	else
3330 		route->path_rec->hop_limit = 1;
3331 	route->path_rec->reversible = 1;
3332 	route->path_rec->pkey = cpu_to_be16(0xffff);
3333 	route->path_rec->mtu_selector = IB_SA_EQ;
3334 	route->path_rec->sl = iboe_tos_to_sl(ndev, tos);
3335 	route->path_rec->traffic_class = tos;
3336 	route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
3337 	route->path_rec->rate_selector = IB_SA_EQ;
3338 	route->path_rec->rate = IB_RATE_PORT_CURRENT;
3339 	dev_put(ndev);
3340 	route->path_rec->packet_life_time_selector = IB_SA_EQ;
3341 	/* In case ACK timeout is set, use this value to calculate
3342 	 * PacketLifeTime.  As per IBTA 12.7.34,
3343 	 * local ACK timeout = (2 * PacketLifeTime + Local CA’s ACK delay).
3344 	 * Assuming a negligible local ACK delay, we can use
3345 	 * PacketLifeTime = local ACK timeout/2
3346 	 * as a reasonable approximation for RoCE networks.
3347 	 */
3348 	mutex_lock(&id_priv->qp_mutex);
3349 	if (id_priv->timeout_set && id_priv->timeout)
3350 		route->path_rec->packet_life_time = id_priv->timeout - 1;
3351 	else
3352 		route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
3353 	mutex_unlock(&id_priv->qp_mutex);
3354 
3355 	if (!route->path_rec->mtu) {
3356 		ret = -EINVAL;
3357 		goto err2;
3358 	}
3359 
3360 	if (rdma_protocol_roce_udp_encap(id_priv->id.device,
3361 					 id_priv->id.port_num))
3362 		route->path_rec->flow_label =
3363 			cma_get_roce_udp_flow_label(id_priv);
3364 
3365 	cma_init_resolve_route_work(work, id_priv);
3366 	queue_work(cma_wq, &work->work);
3367 
3368 	return 0;
3369 
3370 err2:
3371 	kfree(route->path_rec);
3372 	route->path_rec = NULL;
3373 	route->num_pri_alt_paths = 0;
3374 err1:
3375 	kfree(work);
3376 	return ret;
3377 }
3378 
rdma_resolve_route(struct rdma_cm_id * id,unsigned long timeout_ms)3379 int rdma_resolve_route(struct rdma_cm_id *id, unsigned long timeout_ms)
3380 {
3381 	struct rdma_id_private *id_priv;
3382 	int ret;
3383 
3384 	if (!timeout_ms)
3385 		return -EINVAL;
3386 
3387 	id_priv = container_of(id, struct rdma_id_private, id);
3388 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
3389 		return -EINVAL;
3390 
3391 	cma_id_get(id_priv);
3392 	if (rdma_cap_ib_sa(id->device, id->port_num))
3393 		ret = cma_resolve_ib_route(id_priv, timeout_ms);
3394 	else if (rdma_protocol_roce(id->device, id->port_num)) {
3395 		ret = cma_resolve_iboe_route(id_priv);
3396 		if (!ret)
3397 			cma_add_id_to_tree(id_priv);
3398 	}
3399 	else if (rdma_protocol_iwarp(id->device, id->port_num))
3400 		ret = cma_resolve_iw_route(id_priv);
3401 	else
3402 		ret = -ENOSYS;
3403 
3404 	if (ret)
3405 		goto err;
3406 
3407 	return 0;
3408 err:
3409 	cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
3410 	cma_id_put(id_priv);
3411 	return ret;
3412 }
3413 EXPORT_SYMBOL(rdma_resolve_route);
3414 
cma_set_loopback(struct sockaddr * addr)3415 static void cma_set_loopback(struct sockaddr *addr)
3416 {
3417 	switch (addr->sa_family) {
3418 	case AF_INET:
3419 		((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
3420 		break;
3421 	case AF_INET6:
3422 		ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
3423 			      0, 0, 0, htonl(1));
3424 		break;
3425 	default:
3426 		ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
3427 			    0, 0, 0, htonl(1));
3428 		break;
3429 	}
3430 }
3431 
cma_bind_loopback(struct rdma_id_private * id_priv)3432 static int cma_bind_loopback(struct rdma_id_private *id_priv)
3433 {
3434 	struct cma_device *cma_dev, *cur_dev;
3435 	union ib_gid gid;
3436 	enum ib_port_state port_state;
3437 	unsigned int p;
3438 	u16 pkey;
3439 	int ret;
3440 
3441 	cma_dev = NULL;
3442 	mutex_lock(&lock);
3443 	list_for_each_entry(cur_dev, &dev_list, list) {
3444 		if (cma_family(id_priv) == AF_IB &&
3445 		    !rdma_cap_ib_cm(cur_dev->device, 1))
3446 			continue;
3447 
3448 		if (!cma_dev)
3449 			cma_dev = cur_dev;
3450 
3451 		rdma_for_each_port (cur_dev->device, p) {
3452 			if (!ib_get_cached_port_state(cur_dev->device, p, &port_state) &&
3453 			    port_state == IB_PORT_ACTIVE) {
3454 				cma_dev = cur_dev;
3455 				goto port_found;
3456 			}
3457 		}
3458 	}
3459 
3460 	if (!cma_dev) {
3461 		ret = -ENODEV;
3462 		goto out;
3463 	}
3464 
3465 	p = 1;
3466 
3467 port_found:
3468 	ret = rdma_query_gid(cma_dev->device, p, 0, &gid);
3469 	if (ret)
3470 		goto out;
3471 
3472 	ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
3473 	if (ret)
3474 		goto out;
3475 
3476 	id_priv->id.route.addr.dev_addr.dev_type =
3477 		(rdma_protocol_ib(cma_dev->device, p)) ?
3478 		ARPHRD_INFINIBAND : ARPHRD_ETHER;
3479 
3480 	rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3481 	ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
3482 	id_priv->id.port_num = p;
3483 	cma_attach_to_dev(id_priv, cma_dev);
3484 	rdma_restrack_add(&id_priv->res);
3485 	cma_set_loopback(cma_src_addr(id_priv));
3486 out:
3487 	mutex_unlock(&lock);
3488 	return ret;
3489 }
3490 
addr_handler(int status,struct sockaddr * src_addr,struct rdma_dev_addr * dev_addr,void * context)3491 static void addr_handler(int status, struct sockaddr *src_addr,
3492 			 struct rdma_dev_addr *dev_addr, void *context)
3493 {
3494 	struct rdma_id_private *id_priv = context;
3495 	struct rdma_cm_event event = {};
3496 	struct sockaddr *addr;
3497 	struct sockaddr_storage old_addr;
3498 
3499 	mutex_lock(&id_priv->handler_mutex);
3500 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
3501 			   RDMA_CM_ADDR_RESOLVED))
3502 		goto out;
3503 
3504 	/*
3505 	 * Store the previous src address, so that if we fail to acquire
3506 	 * matching rdma device, old address can be restored back, which helps
3507 	 * to cancel the cma listen operation correctly.
3508 	 */
3509 	addr = cma_src_addr(id_priv);
3510 	memcpy(&old_addr, addr, rdma_addr_size(addr));
3511 	memcpy(addr, src_addr, rdma_addr_size(src_addr));
3512 	if (!status && !id_priv->cma_dev) {
3513 		status = cma_acquire_dev_by_src_ip(id_priv);
3514 		if (status)
3515 			pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to acquire device. status %d\n",
3516 					     status);
3517 		rdma_restrack_add(&id_priv->res);
3518 	} else if (status) {
3519 		pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to resolve IP. status %d\n", status);
3520 	}
3521 
3522 	if (status) {
3523 		memcpy(addr, &old_addr,
3524 		       rdma_addr_size((struct sockaddr *)&old_addr));
3525 		if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
3526 				   RDMA_CM_ADDR_BOUND))
3527 			goto out;
3528 		event.event = RDMA_CM_EVENT_ADDR_ERROR;
3529 		event.status = status;
3530 	} else
3531 		event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
3532 
3533 	if (cma_cm_event_handler(id_priv, &event)) {
3534 		destroy_id_handler_unlock(id_priv);
3535 		return;
3536 	}
3537 out:
3538 	mutex_unlock(&id_priv->handler_mutex);
3539 }
3540 
cma_resolve_loopback(struct rdma_id_private * id_priv)3541 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
3542 {
3543 	struct cma_work *work;
3544 	union ib_gid gid;
3545 	int ret;
3546 
3547 	work = kzalloc(sizeof *work, GFP_KERNEL);
3548 	if (!work)
3549 		return -ENOMEM;
3550 
3551 	if (!id_priv->cma_dev) {
3552 		ret = cma_bind_loopback(id_priv);
3553 		if (ret)
3554 			goto err;
3555 	}
3556 
3557 	rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3558 	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
3559 
3560 	enqueue_resolve_addr_work(work, id_priv);
3561 	return 0;
3562 err:
3563 	kfree(work);
3564 	return ret;
3565 }
3566 
cma_resolve_ib_addr(struct rdma_id_private * id_priv)3567 static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
3568 {
3569 	struct cma_work *work;
3570 	int ret;
3571 
3572 	work = kzalloc(sizeof *work, GFP_KERNEL);
3573 	if (!work)
3574 		return -ENOMEM;
3575 
3576 	if (!id_priv->cma_dev) {
3577 		ret = cma_resolve_ib_dev(id_priv);
3578 		if (ret)
3579 			goto err;
3580 	}
3581 
3582 	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
3583 		&(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
3584 
3585 	enqueue_resolve_addr_work(work, id_priv);
3586 	return 0;
3587 err:
3588 	kfree(work);
3589 	return ret;
3590 }
3591 
rdma_set_reuseaddr(struct rdma_cm_id * id,int reuse)3592 int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
3593 {
3594 	struct rdma_id_private *id_priv;
3595 	unsigned long flags;
3596 	int ret;
3597 
3598 	id_priv = container_of(id, struct rdma_id_private, id);
3599 	spin_lock_irqsave(&id_priv->lock, flags);
3600 	if ((reuse && id_priv->state != RDMA_CM_LISTEN) ||
3601 	    id_priv->state == RDMA_CM_IDLE) {
3602 		id_priv->reuseaddr = reuse;
3603 		ret = 0;
3604 	} else {
3605 		ret = -EINVAL;
3606 	}
3607 	spin_unlock_irqrestore(&id_priv->lock, flags);
3608 	return ret;
3609 }
3610 EXPORT_SYMBOL(rdma_set_reuseaddr);
3611 
rdma_set_afonly(struct rdma_cm_id * id,int afonly)3612 int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
3613 {
3614 	struct rdma_id_private *id_priv;
3615 	unsigned long flags;
3616 	int ret;
3617 
3618 	id_priv = container_of(id, struct rdma_id_private, id);
3619 	spin_lock_irqsave(&id_priv->lock, flags);
3620 	if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
3621 		id_priv->options |= (1 << CMA_OPTION_AFONLY);
3622 		id_priv->afonly = afonly;
3623 		ret = 0;
3624 	} else {
3625 		ret = -EINVAL;
3626 	}
3627 	spin_unlock_irqrestore(&id_priv->lock, flags);
3628 	return ret;
3629 }
3630 EXPORT_SYMBOL(rdma_set_afonly);
3631 
cma_bind_port(struct rdma_bind_list * bind_list,struct rdma_id_private * id_priv)3632 static void cma_bind_port(struct rdma_bind_list *bind_list,
3633 			  struct rdma_id_private *id_priv)
3634 {
3635 	struct sockaddr *addr;
3636 	struct sockaddr_ib *sib;
3637 	u64 sid, mask;
3638 	__be16 port;
3639 
3640 	lockdep_assert_held(&lock);
3641 
3642 	addr = cma_src_addr(id_priv);
3643 	port = htons(bind_list->port);
3644 
3645 	switch (addr->sa_family) {
3646 	case AF_INET:
3647 		((struct sockaddr_in *) addr)->sin_port = port;
3648 		break;
3649 	case AF_INET6:
3650 		((struct sockaddr_in6 *) addr)->sin6_port = port;
3651 		break;
3652 	case AF_IB:
3653 		sib = (struct sockaddr_ib *) addr;
3654 		sid = be64_to_cpu(sib->sib_sid);
3655 		mask = be64_to_cpu(sib->sib_sid_mask);
3656 		sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
3657 		sib->sib_sid_mask = cpu_to_be64(~0ULL);
3658 		break;
3659 	}
3660 	id_priv->bind_list = bind_list;
3661 	hlist_add_head(&id_priv->node, &bind_list->owners);
3662 }
3663 
cma_alloc_port(enum rdma_ucm_port_space ps,struct rdma_id_private * id_priv,unsigned short snum)3664 static int cma_alloc_port(enum rdma_ucm_port_space ps,
3665 			  struct rdma_id_private *id_priv, unsigned short snum)
3666 {
3667 	struct rdma_bind_list *bind_list;
3668 	int ret;
3669 
3670 	lockdep_assert_held(&lock);
3671 
3672 	bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
3673 	if (!bind_list)
3674 		return -ENOMEM;
3675 
3676 	ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
3677 			   snum);
3678 	if (ret < 0)
3679 		goto err;
3680 
3681 	bind_list->ps = ps;
3682 	bind_list->port = snum;
3683 	cma_bind_port(bind_list, id_priv);
3684 	return 0;
3685 err:
3686 	kfree(bind_list);
3687 	return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
3688 }
3689 
cma_port_is_unique(struct rdma_bind_list * bind_list,struct rdma_id_private * id_priv)3690 static int cma_port_is_unique(struct rdma_bind_list *bind_list,
3691 			      struct rdma_id_private *id_priv)
3692 {
3693 	struct rdma_id_private *cur_id;
3694 	struct sockaddr  *daddr = cma_dst_addr(id_priv);
3695 	struct sockaddr  *saddr = cma_src_addr(id_priv);
3696 	__be16 dport = cma_port(daddr);
3697 
3698 	lockdep_assert_held(&lock);
3699 
3700 	hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3701 		struct sockaddr  *cur_daddr = cma_dst_addr(cur_id);
3702 		struct sockaddr  *cur_saddr = cma_src_addr(cur_id);
3703 		__be16 cur_dport = cma_port(cur_daddr);
3704 
3705 		if (id_priv == cur_id)
3706 			continue;
3707 
3708 		/* different dest port -> unique */
3709 		if (!cma_any_port(daddr) &&
3710 		    !cma_any_port(cur_daddr) &&
3711 		    (dport != cur_dport))
3712 			continue;
3713 
3714 		/* different src address -> unique */
3715 		if (!cma_any_addr(saddr) &&
3716 		    !cma_any_addr(cur_saddr) &&
3717 		    cma_addr_cmp(saddr, cur_saddr))
3718 			continue;
3719 
3720 		/* different dst address -> unique */
3721 		if (!cma_any_addr(daddr) &&
3722 		    !cma_any_addr(cur_daddr) &&
3723 		    cma_addr_cmp(daddr, cur_daddr))
3724 			continue;
3725 
3726 		return -EADDRNOTAVAIL;
3727 	}
3728 	return 0;
3729 }
3730 
cma_alloc_any_port(enum rdma_ucm_port_space ps,struct rdma_id_private * id_priv)3731 static int cma_alloc_any_port(enum rdma_ucm_port_space ps,
3732 			      struct rdma_id_private *id_priv)
3733 {
3734 	static unsigned int last_used_port;
3735 	int low, high, remaining;
3736 	unsigned int rover;
3737 	struct net *net = id_priv->id.route.addr.dev_addr.net;
3738 
3739 	lockdep_assert_held(&lock);
3740 
3741 	inet_get_local_port_range(net, &low, &high);
3742 	remaining = (high - low) + 1;
3743 	rover = get_random_u32_inclusive(low, remaining + low - 1);
3744 retry:
3745 	if (last_used_port != rover) {
3746 		struct rdma_bind_list *bind_list;
3747 		int ret;
3748 
3749 		bind_list = cma_ps_find(net, ps, (unsigned short)rover);
3750 
3751 		if (!bind_list) {
3752 			ret = cma_alloc_port(ps, id_priv, rover);
3753 		} else {
3754 			ret = cma_port_is_unique(bind_list, id_priv);
3755 			if (!ret)
3756 				cma_bind_port(bind_list, id_priv);
3757 		}
3758 		/*
3759 		 * Remember previously used port number in order to avoid
3760 		 * re-using same port immediately after it is closed.
3761 		 */
3762 		if (!ret)
3763 			last_used_port = rover;
3764 		if (ret != -EADDRNOTAVAIL)
3765 			return ret;
3766 	}
3767 	if (--remaining) {
3768 		rover++;
3769 		if ((rover < low) || (rover > high))
3770 			rover = low;
3771 		goto retry;
3772 	}
3773 	return -EADDRNOTAVAIL;
3774 }
3775 
3776 /*
3777  * Check that the requested port is available.  This is called when trying to
3778  * bind to a specific port, or when trying to listen on a bound port.  In
3779  * the latter case, the provided id_priv may already be on the bind_list, but
3780  * we still need to check that it's okay to start listening.
3781  */
cma_check_port(struct rdma_bind_list * bind_list,struct rdma_id_private * id_priv,uint8_t reuseaddr)3782 static int cma_check_port(struct rdma_bind_list *bind_list,
3783 			  struct rdma_id_private *id_priv, uint8_t reuseaddr)
3784 {
3785 	struct rdma_id_private *cur_id;
3786 	struct sockaddr *addr, *cur_addr;
3787 
3788 	lockdep_assert_held(&lock);
3789 
3790 	addr = cma_src_addr(id_priv);
3791 	hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3792 		if (id_priv == cur_id)
3793 			continue;
3794 
3795 		if (reuseaddr && cur_id->reuseaddr)
3796 			continue;
3797 
3798 		cur_addr = cma_src_addr(cur_id);
3799 		if (id_priv->afonly && cur_id->afonly &&
3800 		    (addr->sa_family != cur_addr->sa_family))
3801 			continue;
3802 
3803 		if (cma_any_addr(addr) || cma_any_addr(cur_addr))
3804 			return -EADDRNOTAVAIL;
3805 
3806 		if (!cma_addr_cmp(addr, cur_addr))
3807 			return -EADDRINUSE;
3808 	}
3809 	return 0;
3810 }
3811 
cma_use_port(enum rdma_ucm_port_space ps,struct rdma_id_private * id_priv)3812 static int cma_use_port(enum rdma_ucm_port_space ps,
3813 			struct rdma_id_private *id_priv)
3814 {
3815 	struct rdma_bind_list *bind_list;
3816 	unsigned short snum;
3817 	int ret;
3818 
3819 	lockdep_assert_held(&lock);
3820 
3821 	snum = ntohs(cma_port(cma_src_addr(id_priv)));
3822 	if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
3823 		return -EACCES;
3824 
3825 	bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
3826 	if (!bind_list) {
3827 		ret = cma_alloc_port(ps, id_priv, snum);
3828 	} else {
3829 		ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
3830 		if (!ret)
3831 			cma_bind_port(bind_list, id_priv);
3832 	}
3833 	return ret;
3834 }
3835 
3836 static enum rdma_ucm_port_space
cma_select_inet_ps(struct rdma_id_private * id_priv)3837 cma_select_inet_ps(struct rdma_id_private *id_priv)
3838 {
3839 	switch (id_priv->id.ps) {
3840 	case RDMA_PS_TCP:
3841 	case RDMA_PS_UDP:
3842 	case RDMA_PS_IPOIB:
3843 	case RDMA_PS_IB:
3844 		return id_priv->id.ps;
3845 	default:
3846 
3847 		return 0;
3848 	}
3849 }
3850 
3851 static enum rdma_ucm_port_space
cma_select_ib_ps(struct rdma_id_private * id_priv)3852 cma_select_ib_ps(struct rdma_id_private *id_priv)
3853 {
3854 	enum rdma_ucm_port_space ps = 0;
3855 	struct sockaddr_ib *sib;
3856 	u64 sid_ps, mask, sid;
3857 
3858 	sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
3859 	mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
3860 	sid = be64_to_cpu(sib->sib_sid) & mask;
3861 
3862 	if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
3863 		sid_ps = RDMA_IB_IP_PS_IB;
3864 		ps = RDMA_PS_IB;
3865 	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
3866 		   (sid == (RDMA_IB_IP_PS_TCP & mask))) {
3867 		sid_ps = RDMA_IB_IP_PS_TCP;
3868 		ps = RDMA_PS_TCP;
3869 	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
3870 		   (sid == (RDMA_IB_IP_PS_UDP & mask))) {
3871 		sid_ps = RDMA_IB_IP_PS_UDP;
3872 		ps = RDMA_PS_UDP;
3873 	}
3874 
3875 	if (ps) {
3876 		sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
3877 		sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
3878 						be64_to_cpu(sib->sib_sid_mask));
3879 	}
3880 	return ps;
3881 }
3882 
cma_get_port(struct rdma_id_private * id_priv)3883 static int cma_get_port(struct rdma_id_private *id_priv)
3884 {
3885 	enum rdma_ucm_port_space ps;
3886 	int ret;
3887 
3888 	if (cma_family(id_priv) != AF_IB)
3889 		ps = cma_select_inet_ps(id_priv);
3890 	else
3891 		ps = cma_select_ib_ps(id_priv);
3892 	if (!ps)
3893 		return -EPROTONOSUPPORT;
3894 
3895 	mutex_lock(&lock);
3896 	if (cma_any_port(cma_src_addr(id_priv)))
3897 		ret = cma_alloc_any_port(ps, id_priv);
3898 	else
3899 		ret = cma_use_port(ps, id_priv);
3900 	mutex_unlock(&lock);
3901 
3902 	return ret;
3903 }
3904 
cma_check_linklocal(struct rdma_dev_addr * dev_addr,struct sockaddr * addr)3905 static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3906 			       struct sockaddr *addr)
3907 {
3908 #if IS_ENABLED(CONFIG_IPV6)
3909 	struct sockaddr_in6 *sin6;
3910 
3911 	if (addr->sa_family != AF_INET6)
3912 		return 0;
3913 
3914 	sin6 = (struct sockaddr_in6 *) addr;
3915 
3916 	if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
3917 		return 0;
3918 
3919 	if (!sin6->sin6_scope_id)
3920 			return -EINVAL;
3921 
3922 	dev_addr->bound_dev_if = sin6->sin6_scope_id;
3923 #endif
3924 	return 0;
3925 }
3926 
rdma_listen(struct rdma_cm_id * id,int backlog)3927 int rdma_listen(struct rdma_cm_id *id, int backlog)
3928 {
3929 	struct rdma_id_private *id_priv =
3930 		container_of(id, struct rdma_id_private, id);
3931 	int ret;
3932 
3933 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN)) {
3934 		struct sockaddr_in any_in = {
3935 			.sin_family = AF_INET,
3936 			.sin_addr.s_addr = htonl(INADDR_ANY),
3937 		};
3938 
3939 		/* For a well behaved ULP state will be RDMA_CM_IDLE */
3940 		ret = rdma_bind_addr(id, (struct sockaddr *)&any_in);
3941 		if (ret)
3942 			return ret;
3943 		if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
3944 					   RDMA_CM_LISTEN)))
3945 			return -EINVAL;
3946 	}
3947 
3948 	/*
3949 	 * Once the ID reaches RDMA_CM_LISTEN it is not allowed to be reusable
3950 	 * any more, and has to be unique in the bind list.
3951 	 */
3952 	if (id_priv->reuseaddr) {
3953 		mutex_lock(&lock);
3954 		ret = cma_check_port(id_priv->bind_list, id_priv, 0);
3955 		if (!ret)
3956 			id_priv->reuseaddr = 0;
3957 		mutex_unlock(&lock);
3958 		if (ret)
3959 			goto err;
3960 	}
3961 
3962 	id_priv->backlog = backlog;
3963 	if (id_priv->cma_dev) {
3964 		if (rdma_cap_ib_cm(id->device, 1)) {
3965 			ret = cma_ib_listen(id_priv);
3966 			if (ret)
3967 				goto err;
3968 		} else if (rdma_cap_iw_cm(id->device, 1)) {
3969 			ret = cma_iw_listen(id_priv, backlog);
3970 			if (ret)
3971 				goto err;
3972 		} else {
3973 			ret = -ENOSYS;
3974 			goto err;
3975 		}
3976 	} else {
3977 		ret = cma_listen_on_all(id_priv);
3978 		if (ret)
3979 			goto err;
3980 	}
3981 
3982 	return 0;
3983 err:
3984 	id_priv->backlog = 0;
3985 	/*
3986 	 * All the failure paths that lead here will not allow the req_handler's
3987 	 * to have run.
3988 	 */
3989 	cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
3990 	return ret;
3991 }
3992 EXPORT_SYMBOL(rdma_listen);
3993 
rdma_bind_addr_dst(struct rdma_id_private * id_priv,struct sockaddr * addr,const struct sockaddr * daddr)3994 static int rdma_bind_addr_dst(struct rdma_id_private *id_priv,
3995 			      struct sockaddr *addr, const struct sockaddr *daddr)
3996 {
3997 	struct sockaddr *id_daddr;
3998 	int ret;
3999 
4000 	if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
4001 	    addr->sa_family != AF_IB)
4002 		return -EAFNOSUPPORT;
4003 
4004 	if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
4005 		return -EINVAL;
4006 
4007 	ret = cma_check_linklocal(&id_priv->id.route.addr.dev_addr, addr);
4008 	if (ret)
4009 		goto err1;
4010 
4011 	memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
4012 	if (!cma_any_addr(addr)) {
4013 		ret = cma_translate_addr(addr, &id_priv->id.route.addr.dev_addr);
4014 		if (ret)
4015 			goto err1;
4016 
4017 		ret = cma_acquire_dev_by_src_ip(id_priv);
4018 		if (ret)
4019 			goto err1;
4020 	}
4021 
4022 	if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
4023 		if (addr->sa_family == AF_INET)
4024 			id_priv->afonly = 1;
4025 #if IS_ENABLED(CONFIG_IPV6)
4026 		else if (addr->sa_family == AF_INET6) {
4027 			struct net *net = id_priv->id.route.addr.dev_addr.net;
4028 
4029 			id_priv->afonly = net->ipv6.sysctl.bindv6only;
4030 		}
4031 #endif
4032 	}
4033 	id_daddr = cma_dst_addr(id_priv);
4034 	if (daddr != id_daddr)
4035 		memcpy(id_daddr, daddr, rdma_addr_size(addr));
4036 	id_daddr->sa_family = addr->sa_family;
4037 
4038 	ret = cma_get_port(id_priv);
4039 	if (ret)
4040 		goto err2;
4041 
4042 	if (!cma_any_addr(addr))
4043 		rdma_restrack_add(&id_priv->res);
4044 	return 0;
4045 err2:
4046 	if (id_priv->cma_dev)
4047 		cma_release_dev(id_priv);
4048 err1:
4049 	cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
4050 	return ret;
4051 }
4052 
cma_bind_addr(struct rdma_cm_id * id,struct sockaddr * src_addr,const struct sockaddr * dst_addr)4053 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
4054 			 const struct sockaddr *dst_addr)
4055 {
4056 	struct rdma_id_private *id_priv =
4057 		container_of(id, struct rdma_id_private, id);
4058 	struct sockaddr_storage zero_sock = {};
4059 
4060 	if (src_addr && src_addr->sa_family)
4061 		return rdma_bind_addr_dst(id_priv, src_addr, dst_addr);
4062 
4063 	/*
4064 	 * When the src_addr is not specified, automatically supply an any addr
4065 	 */
4066 	zero_sock.ss_family = dst_addr->sa_family;
4067 	if (IS_ENABLED(CONFIG_IPV6) && dst_addr->sa_family == AF_INET6) {
4068 		struct sockaddr_in6 *src_addr6 =
4069 			(struct sockaddr_in6 *)&zero_sock;
4070 		struct sockaddr_in6 *dst_addr6 =
4071 			(struct sockaddr_in6 *)dst_addr;
4072 
4073 		src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
4074 		if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
4075 			id->route.addr.dev_addr.bound_dev_if =
4076 				dst_addr6->sin6_scope_id;
4077 	} else if (dst_addr->sa_family == AF_IB) {
4078 		((struct sockaddr_ib *)&zero_sock)->sib_pkey =
4079 			((struct sockaddr_ib *)dst_addr)->sib_pkey;
4080 	}
4081 	return rdma_bind_addr_dst(id_priv, (struct sockaddr *)&zero_sock, dst_addr);
4082 }
4083 
4084 /*
4085  * If required, resolve the source address for bind and leave the id_priv in
4086  * state RDMA_CM_ADDR_BOUND. This oddly uses the state to determine the prior
4087  * calls made by ULP, a previously bound ID will not be re-bound and src_addr is
4088  * ignored.
4089  */
resolve_prepare_src(struct rdma_id_private * id_priv,struct sockaddr * src_addr,const struct sockaddr * dst_addr)4090 static int resolve_prepare_src(struct rdma_id_private *id_priv,
4091 			       struct sockaddr *src_addr,
4092 			       const struct sockaddr *dst_addr)
4093 {
4094 	int ret;
4095 
4096 	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) {
4097 		/* For a well behaved ULP state will be RDMA_CM_IDLE */
4098 		ret = cma_bind_addr(&id_priv->id, src_addr, dst_addr);
4099 		if (ret)
4100 			return ret;
4101 		if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
4102 					   RDMA_CM_ADDR_QUERY)))
4103 			return -EINVAL;
4104 
4105 	} else {
4106 		memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
4107 	}
4108 
4109 	if (cma_family(id_priv) != dst_addr->sa_family) {
4110 		ret = -EINVAL;
4111 		goto err_state;
4112 	}
4113 	return 0;
4114 
4115 err_state:
4116 	cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
4117 	return ret;
4118 }
4119 
rdma_resolve_addr(struct rdma_cm_id * id,struct sockaddr * src_addr,const struct sockaddr * dst_addr,unsigned long timeout_ms)4120 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
4121 		      const struct sockaddr *dst_addr, unsigned long timeout_ms)
4122 {
4123 	struct rdma_id_private *id_priv =
4124 		container_of(id, struct rdma_id_private, id);
4125 	int ret;
4126 
4127 	ret = resolve_prepare_src(id_priv, src_addr, dst_addr);
4128 	if (ret)
4129 		return ret;
4130 
4131 	if (cma_any_addr(dst_addr)) {
4132 		ret = cma_resolve_loopback(id_priv);
4133 	} else {
4134 		if (dst_addr->sa_family == AF_IB) {
4135 			ret = cma_resolve_ib_addr(id_priv);
4136 		} else {
4137 			/*
4138 			 * The FSM can return back to RDMA_CM_ADDR_BOUND after
4139 			 * rdma_resolve_ip() is called, eg through the error
4140 			 * path in addr_handler(). If this happens the existing
4141 			 * request must be canceled before issuing a new one.
4142 			 * Since canceling a request is a bit slow and this
4143 			 * oddball path is rare, keep track once a request has
4144 			 * been issued. The track turns out to be a permanent
4145 			 * state since this is the only cancel as it is
4146 			 * immediately before rdma_resolve_ip().
4147 			 */
4148 			if (id_priv->used_resolve_ip)
4149 				rdma_addr_cancel(&id->route.addr.dev_addr);
4150 			else
4151 				id_priv->used_resolve_ip = 1;
4152 			ret = rdma_resolve_ip(cma_src_addr(id_priv), dst_addr,
4153 					      &id->route.addr.dev_addr,
4154 					      timeout_ms, addr_handler,
4155 					      false, id_priv);
4156 		}
4157 	}
4158 	if (ret)
4159 		goto err;
4160 
4161 	return 0;
4162 err:
4163 	cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
4164 	return ret;
4165 }
4166 EXPORT_SYMBOL(rdma_resolve_addr);
4167 
rdma_bind_addr(struct rdma_cm_id * id,struct sockaddr * addr)4168 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
4169 {
4170 	struct rdma_id_private *id_priv =
4171 		container_of(id, struct rdma_id_private, id);
4172 
4173 	return rdma_bind_addr_dst(id_priv, addr, cma_dst_addr(id_priv));
4174 }
4175 EXPORT_SYMBOL(rdma_bind_addr);
4176 
cma_format_hdr(void * hdr,struct rdma_id_private * id_priv)4177 static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
4178 {
4179 	struct cma_hdr *cma_hdr;
4180 
4181 	cma_hdr = hdr;
4182 	cma_hdr->cma_version = CMA_VERSION;
4183 	if (cma_family(id_priv) == AF_INET) {
4184 		struct sockaddr_in *src4, *dst4;
4185 
4186 		src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
4187 		dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
4188 
4189 		cma_set_ip_ver(cma_hdr, 4);
4190 		cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
4191 		cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
4192 		cma_hdr->port = src4->sin_port;
4193 	} else if (cma_family(id_priv) == AF_INET6) {
4194 		struct sockaddr_in6 *src6, *dst6;
4195 
4196 		src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
4197 		dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
4198 
4199 		cma_set_ip_ver(cma_hdr, 6);
4200 		cma_hdr->src_addr.ip6 = src6->sin6_addr;
4201 		cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
4202 		cma_hdr->port = src6->sin6_port;
4203 	}
4204 	return 0;
4205 }
4206 
cma_sidr_rep_handler(struct ib_cm_id * cm_id,const struct ib_cm_event * ib_event)4207 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
4208 				const struct ib_cm_event *ib_event)
4209 {
4210 	struct rdma_id_private *id_priv = cm_id->context;
4211 	struct rdma_cm_event event = {};
4212 	const struct ib_cm_sidr_rep_event_param *rep =
4213 				&ib_event->param.sidr_rep_rcvd;
4214 	int ret;
4215 
4216 	mutex_lock(&id_priv->handler_mutex);
4217 	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
4218 		goto out;
4219 
4220 	switch (ib_event->event) {
4221 	case IB_CM_SIDR_REQ_ERROR:
4222 		event.event = RDMA_CM_EVENT_UNREACHABLE;
4223 		event.status = -ETIMEDOUT;
4224 		break;
4225 	case IB_CM_SIDR_REP_RECEIVED:
4226 		event.param.ud.private_data = ib_event->private_data;
4227 		event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
4228 		if (rep->status != IB_SIDR_SUCCESS) {
4229 			event.event = RDMA_CM_EVENT_UNREACHABLE;
4230 			event.status = ib_event->param.sidr_rep_rcvd.status;
4231 			pr_debug_ratelimited("RDMA CM: UNREACHABLE: bad SIDR reply. status %d\n",
4232 					     event.status);
4233 			break;
4234 		}
4235 		ret = cma_set_qkey(id_priv, rep->qkey);
4236 		if (ret) {
4237 			pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to set qkey. status %d\n", ret);
4238 			event.event = RDMA_CM_EVENT_ADDR_ERROR;
4239 			event.status = ret;
4240 			break;
4241 		}
4242 		ib_init_ah_attr_from_path(id_priv->id.device,
4243 					  id_priv->id.port_num,
4244 					  id_priv->id.route.path_rec,
4245 					  &event.param.ud.ah_attr,
4246 					  rep->sgid_attr);
4247 		event.param.ud.qp_num = rep->qpn;
4248 		event.param.ud.qkey = rep->qkey;
4249 		event.event = RDMA_CM_EVENT_ESTABLISHED;
4250 		event.status = 0;
4251 		break;
4252 	default:
4253 		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
4254 		       ib_event->event);
4255 		goto out;
4256 	}
4257 
4258 	ret = cma_cm_event_handler(id_priv, &event);
4259 
4260 	rdma_destroy_ah_attr(&event.param.ud.ah_attr);
4261 	if (ret) {
4262 		/* Destroy the CM ID by returning a non-zero value. */
4263 		id_priv->cm_id.ib = NULL;
4264 		destroy_id_handler_unlock(id_priv);
4265 		return ret;
4266 	}
4267 out:
4268 	mutex_unlock(&id_priv->handler_mutex);
4269 	return 0;
4270 }
4271 
cma_resolve_ib_udp(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)4272 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
4273 			      struct rdma_conn_param *conn_param)
4274 {
4275 	struct ib_cm_sidr_req_param req;
4276 	struct ib_cm_id	*id;
4277 	void *private_data;
4278 	u8 offset;
4279 	int ret;
4280 
4281 	memset(&req, 0, sizeof req);
4282 	offset = cma_user_data_offset(id_priv);
4283 	if (check_add_overflow(offset, conn_param->private_data_len, &req.private_data_len))
4284 		return -EINVAL;
4285 
4286 	if (req.private_data_len) {
4287 		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
4288 		if (!private_data)
4289 			return -ENOMEM;
4290 	} else {
4291 		private_data = NULL;
4292 	}
4293 
4294 	if (conn_param->private_data && conn_param->private_data_len)
4295 		memcpy(private_data + offset, conn_param->private_data,
4296 		       conn_param->private_data_len);
4297 
4298 	if (private_data) {
4299 		ret = cma_format_hdr(private_data, id_priv);
4300 		if (ret)
4301 			goto out;
4302 		req.private_data = private_data;
4303 	}
4304 
4305 	id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
4306 			     id_priv);
4307 	if (IS_ERR(id)) {
4308 		ret = PTR_ERR(id);
4309 		goto out;
4310 	}
4311 	id_priv->cm_id.ib = id;
4312 
4313 	req.path = id_priv->id.route.path_rec;
4314 	req.sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
4315 	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
4316 	req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
4317 	req.max_cm_retries = CMA_MAX_CM_RETRIES;
4318 
4319 	trace_cm_send_sidr_req(id_priv);
4320 	ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
4321 	if (ret) {
4322 		ib_destroy_cm_id(id_priv->cm_id.ib);
4323 		id_priv->cm_id.ib = NULL;
4324 	}
4325 out:
4326 	kfree(private_data);
4327 	return ret;
4328 }
4329 
cma_connect_ib(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)4330 static int cma_connect_ib(struct rdma_id_private *id_priv,
4331 			  struct rdma_conn_param *conn_param)
4332 {
4333 	struct ib_cm_req_param req;
4334 	struct rdma_route *route;
4335 	void *private_data;
4336 	struct ib_cm_id	*id;
4337 	u8 offset;
4338 	int ret;
4339 
4340 	memset(&req, 0, sizeof req);
4341 	offset = cma_user_data_offset(id_priv);
4342 	if (check_add_overflow(offset, conn_param->private_data_len, &req.private_data_len))
4343 		return -EINVAL;
4344 
4345 	if (req.private_data_len) {
4346 		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
4347 		if (!private_data)
4348 			return -ENOMEM;
4349 	} else {
4350 		private_data = NULL;
4351 	}
4352 
4353 	if (conn_param->private_data && conn_param->private_data_len)
4354 		memcpy(private_data + offset, conn_param->private_data,
4355 		       conn_param->private_data_len);
4356 
4357 	id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
4358 	if (IS_ERR(id)) {
4359 		ret = PTR_ERR(id);
4360 		goto out;
4361 	}
4362 	id_priv->cm_id.ib = id;
4363 
4364 	route = &id_priv->id.route;
4365 	if (private_data) {
4366 		ret = cma_format_hdr(private_data, id_priv);
4367 		if (ret)
4368 			goto out;
4369 		req.private_data = private_data;
4370 	}
4371 
4372 	req.primary_path = &route->path_rec[0];
4373 	req.primary_path_inbound = route->path_rec_inbound;
4374 	req.primary_path_outbound = route->path_rec_outbound;
4375 	if (route->num_pri_alt_paths == 2)
4376 		req.alternate_path = &route->path_rec[1];
4377 
4378 	req.ppath_sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
4379 	/* Alternate path SGID attribute currently unsupported */
4380 	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
4381 	req.qp_num = id_priv->qp_num;
4382 	req.qp_type = id_priv->id.qp_type;
4383 	req.starting_psn = id_priv->seq_num;
4384 	req.responder_resources = conn_param->responder_resources;
4385 	req.initiator_depth = conn_param->initiator_depth;
4386 	req.flow_control = conn_param->flow_control;
4387 	req.retry_count = min_t(u8, 7, conn_param->retry_count);
4388 	req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
4389 	req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
4390 	req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
4391 	req.max_cm_retries = CMA_MAX_CM_RETRIES;
4392 	req.srq = id_priv->srq ? 1 : 0;
4393 	req.ece.vendor_id = id_priv->ece.vendor_id;
4394 	req.ece.attr_mod = id_priv->ece.attr_mod;
4395 
4396 	trace_cm_send_req(id_priv);
4397 	ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
4398 out:
4399 	if (ret && !IS_ERR(id)) {
4400 		ib_destroy_cm_id(id);
4401 		id_priv->cm_id.ib = NULL;
4402 	}
4403 
4404 	kfree(private_data);
4405 	return ret;
4406 }
4407 
cma_connect_iw(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)4408 static int cma_connect_iw(struct rdma_id_private *id_priv,
4409 			  struct rdma_conn_param *conn_param)
4410 {
4411 	struct iw_cm_id *cm_id;
4412 	int ret;
4413 	struct iw_cm_conn_param iw_param;
4414 
4415 	cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
4416 	if (IS_ERR(cm_id))
4417 		return PTR_ERR(cm_id);
4418 
4419 	mutex_lock(&id_priv->qp_mutex);
4420 	cm_id->tos = id_priv->tos;
4421 	cm_id->tos_set = id_priv->tos_set;
4422 	mutex_unlock(&id_priv->qp_mutex);
4423 
4424 	id_priv->cm_id.iw = cm_id;
4425 
4426 	memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
4427 	       rdma_addr_size(cma_src_addr(id_priv)));
4428 	memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
4429 	       rdma_addr_size(cma_dst_addr(id_priv)));
4430 
4431 	ret = cma_modify_qp_rtr(id_priv, conn_param);
4432 	if (ret)
4433 		goto out;
4434 
4435 	if (conn_param) {
4436 		iw_param.ord = conn_param->initiator_depth;
4437 		iw_param.ird = conn_param->responder_resources;
4438 		iw_param.private_data = conn_param->private_data;
4439 		iw_param.private_data_len = conn_param->private_data_len;
4440 		iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
4441 	} else {
4442 		memset(&iw_param, 0, sizeof iw_param);
4443 		iw_param.qpn = id_priv->qp_num;
4444 	}
4445 	ret = iw_cm_connect(cm_id, &iw_param);
4446 out:
4447 	if (ret) {
4448 		iw_destroy_cm_id(cm_id);
4449 		id_priv->cm_id.iw = NULL;
4450 	}
4451 	return ret;
4452 }
4453 
4454 /**
4455  * rdma_connect_locked - Initiate an active connection request.
4456  * @id: Connection identifier to connect.
4457  * @conn_param: Connection information used for connected QPs.
4458  *
4459  * Same as rdma_connect() but can only be called from the
4460  * RDMA_CM_EVENT_ROUTE_RESOLVED handler callback.
4461  */
rdma_connect_locked(struct rdma_cm_id * id,struct rdma_conn_param * conn_param)4462 int rdma_connect_locked(struct rdma_cm_id *id,
4463 			struct rdma_conn_param *conn_param)
4464 {
4465 	struct rdma_id_private *id_priv =
4466 		container_of(id, struct rdma_id_private, id);
4467 	int ret;
4468 
4469 	if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
4470 		return -EINVAL;
4471 
4472 	if (!id->qp) {
4473 		id_priv->qp_num = conn_param->qp_num;
4474 		id_priv->srq = conn_param->srq;
4475 	}
4476 
4477 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4478 		if (id->qp_type == IB_QPT_UD)
4479 			ret = cma_resolve_ib_udp(id_priv, conn_param);
4480 		else
4481 			ret = cma_connect_ib(id_priv, conn_param);
4482 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4483 		ret = cma_connect_iw(id_priv, conn_param);
4484 	} else {
4485 		ret = -ENOSYS;
4486 	}
4487 	if (ret)
4488 		goto err_state;
4489 	return 0;
4490 err_state:
4491 	cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
4492 	return ret;
4493 }
4494 EXPORT_SYMBOL(rdma_connect_locked);
4495 
4496 /**
4497  * rdma_connect - Initiate an active connection request.
4498  * @id: Connection identifier to connect.
4499  * @conn_param: Connection information used for connected QPs.
4500  *
4501  * Users must have resolved a route for the rdma_cm_id to connect with by having
4502  * called rdma_resolve_route before calling this routine.
4503  *
4504  * This call will either connect to a remote QP or obtain remote QP information
4505  * for unconnected rdma_cm_id's.  The actual operation is based on the
4506  * rdma_cm_id's port space.
4507  */
rdma_connect(struct rdma_cm_id * id,struct rdma_conn_param * conn_param)4508 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4509 {
4510 	struct rdma_id_private *id_priv =
4511 		container_of(id, struct rdma_id_private, id);
4512 	int ret;
4513 
4514 	mutex_lock(&id_priv->handler_mutex);
4515 	ret = rdma_connect_locked(id, conn_param);
4516 	mutex_unlock(&id_priv->handler_mutex);
4517 	return ret;
4518 }
4519 EXPORT_SYMBOL(rdma_connect);
4520 
4521 /**
4522  * rdma_connect_ece - Initiate an active connection request with ECE data.
4523  * @id: Connection identifier to connect.
4524  * @conn_param: Connection information used for connected QPs.
4525  * @ece: ECE parameters
4526  *
4527  * See rdma_connect() explanation.
4528  */
rdma_connect_ece(struct rdma_cm_id * id,struct rdma_conn_param * conn_param,struct rdma_ucm_ece * ece)4529 int rdma_connect_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
4530 		     struct rdma_ucm_ece *ece)
4531 {
4532 	struct rdma_id_private *id_priv =
4533 		container_of(id, struct rdma_id_private, id);
4534 
4535 	id_priv->ece.vendor_id = ece->vendor_id;
4536 	id_priv->ece.attr_mod = ece->attr_mod;
4537 
4538 	return rdma_connect(id, conn_param);
4539 }
4540 EXPORT_SYMBOL(rdma_connect_ece);
4541 
cma_accept_ib(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)4542 static int cma_accept_ib(struct rdma_id_private *id_priv,
4543 			 struct rdma_conn_param *conn_param)
4544 {
4545 	struct ib_cm_rep_param rep;
4546 	int ret;
4547 
4548 	ret = cma_modify_qp_rtr(id_priv, conn_param);
4549 	if (ret)
4550 		goto out;
4551 
4552 	ret = cma_modify_qp_rts(id_priv, conn_param);
4553 	if (ret)
4554 		goto out;
4555 
4556 	memset(&rep, 0, sizeof rep);
4557 	rep.qp_num = id_priv->qp_num;
4558 	rep.starting_psn = id_priv->seq_num;
4559 	rep.private_data = conn_param->private_data;
4560 	rep.private_data_len = conn_param->private_data_len;
4561 	rep.responder_resources = conn_param->responder_resources;
4562 	rep.initiator_depth = conn_param->initiator_depth;
4563 	rep.failover_accepted = 0;
4564 	rep.flow_control = conn_param->flow_control;
4565 	rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
4566 	rep.srq = id_priv->srq ? 1 : 0;
4567 	rep.ece.vendor_id = id_priv->ece.vendor_id;
4568 	rep.ece.attr_mod = id_priv->ece.attr_mod;
4569 
4570 	trace_cm_send_rep(id_priv);
4571 	ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
4572 out:
4573 	return ret;
4574 }
4575 
cma_accept_iw(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)4576 static int cma_accept_iw(struct rdma_id_private *id_priv,
4577 		  struct rdma_conn_param *conn_param)
4578 {
4579 	struct iw_cm_conn_param iw_param;
4580 	int ret;
4581 
4582 	if (!conn_param)
4583 		return -EINVAL;
4584 
4585 	ret = cma_modify_qp_rtr(id_priv, conn_param);
4586 	if (ret)
4587 		return ret;
4588 
4589 	iw_param.ord = conn_param->initiator_depth;
4590 	iw_param.ird = conn_param->responder_resources;
4591 	iw_param.private_data = conn_param->private_data;
4592 	iw_param.private_data_len = conn_param->private_data_len;
4593 	if (id_priv->id.qp)
4594 		iw_param.qpn = id_priv->qp_num;
4595 	else
4596 		iw_param.qpn = conn_param->qp_num;
4597 
4598 	return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
4599 }
4600 
cma_send_sidr_rep(struct rdma_id_private * id_priv,enum ib_cm_sidr_status status,u32 qkey,const void * private_data,int private_data_len)4601 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
4602 			     enum ib_cm_sidr_status status, u32 qkey,
4603 			     const void *private_data, int private_data_len)
4604 {
4605 	struct ib_cm_sidr_rep_param rep;
4606 	int ret;
4607 
4608 	memset(&rep, 0, sizeof rep);
4609 	rep.status = status;
4610 	if (status == IB_SIDR_SUCCESS) {
4611 		if (qkey)
4612 			ret = cma_set_qkey(id_priv, qkey);
4613 		else
4614 			ret = cma_set_default_qkey(id_priv);
4615 		if (ret)
4616 			return ret;
4617 		rep.qp_num = id_priv->qp_num;
4618 		rep.qkey = id_priv->qkey;
4619 
4620 		rep.ece.vendor_id = id_priv->ece.vendor_id;
4621 		rep.ece.attr_mod = id_priv->ece.attr_mod;
4622 	}
4623 
4624 	rep.private_data = private_data;
4625 	rep.private_data_len = private_data_len;
4626 
4627 	trace_cm_send_sidr_rep(id_priv);
4628 	return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
4629 }
4630 
4631 /**
4632  * rdma_accept - Called to accept a connection request or response.
4633  * @id: Connection identifier associated with the request.
4634  * @conn_param: Information needed to establish the connection.  This must be
4635  *   provided if accepting a connection request.  If accepting a connection
4636  *   response, this parameter must be NULL.
4637  *
4638  * Typically, this routine is only called by the listener to accept a connection
4639  * request.  It must also be called on the active side of a connection if the
4640  * user is performing their own QP transitions.
4641  *
4642  * In the case of error, a reject message is sent to the remote side and the
4643  * state of the qp associated with the id is modified to error, such that any
4644  * previously posted receive buffers would be flushed.
4645  *
4646  * This function is for use by kernel ULPs and must be called from under the
4647  * handler callback.
4648  */
rdma_accept(struct rdma_cm_id * id,struct rdma_conn_param * conn_param)4649 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4650 {
4651 	struct rdma_id_private *id_priv =
4652 		container_of(id, struct rdma_id_private, id);
4653 	int ret;
4654 
4655 	lockdep_assert_held(&id_priv->handler_mutex);
4656 
4657 	if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
4658 		return -EINVAL;
4659 
4660 	if (!id->qp && conn_param) {
4661 		id_priv->qp_num = conn_param->qp_num;
4662 		id_priv->srq = conn_param->srq;
4663 	}
4664 
4665 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4666 		if (id->qp_type == IB_QPT_UD) {
4667 			if (conn_param)
4668 				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
4669 							conn_param->qkey,
4670 							conn_param->private_data,
4671 							conn_param->private_data_len);
4672 			else
4673 				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
4674 							0, NULL, 0);
4675 		} else {
4676 			if (conn_param)
4677 				ret = cma_accept_ib(id_priv, conn_param);
4678 			else
4679 				ret = cma_rep_recv(id_priv);
4680 		}
4681 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4682 		ret = cma_accept_iw(id_priv, conn_param);
4683 	} else {
4684 		ret = -ENOSYS;
4685 	}
4686 	if (ret)
4687 		goto reject;
4688 
4689 	return 0;
4690 reject:
4691 	cma_modify_qp_err(id_priv);
4692 	rdma_reject(id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED);
4693 	return ret;
4694 }
4695 EXPORT_SYMBOL(rdma_accept);
4696 
rdma_accept_ece(struct rdma_cm_id * id,struct rdma_conn_param * conn_param,struct rdma_ucm_ece * ece)4697 int rdma_accept_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
4698 		    struct rdma_ucm_ece *ece)
4699 {
4700 	struct rdma_id_private *id_priv =
4701 		container_of(id, struct rdma_id_private, id);
4702 
4703 	id_priv->ece.vendor_id = ece->vendor_id;
4704 	id_priv->ece.attr_mod = ece->attr_mod;
4705 
4706 	return rdma_accept(id, conn_param);
4707 }
4708 EXPORT_SYMBOL(rdma_accept_ece);
4709 
rdma_lock_handler(struct rdma_cm_id * id)4710 void rdma_lock_handler(struct rdma_cm_id *id)
4711 {
4712 	struct rdma_id_private *id_priv =
4713 		container_of(id, struct rdma_id_private, id);
4714 
4715 	mutex_lock(&id_priv->handler_mutex);
4716 }
4717 EXPORT_SYMBOL(rdma_lock_handler);
4718 
rdma_unlock_handler(struct rdma_cm_id * id)4719 void rdma_unlock_handler(struct rdma_cm_id *id)
4720 {
4721 	struct rdma_id_private *id_priv =
4722 		container_of(id, struct rdma_id_private, id);
4723 
4724 	mutex_unlock(&id_priv->handler_mutex);
4725 }
4726 EXPORT_SYMBOL(rdma_unlock_handler);
4727 
rdma_notify(struct rdma_cm_id * id,enum ib_event_type event)4728 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
4729 {
4730 	struct rdma_id_private *id_priv;
4731 	int ret;
4732 
4733 	id_priv = container_of(id, struct rdma_id_private, id);
4734 	if (!id_priv->cm_id.ib)
4735 		return -EINVAL;
4736 
4737 	switch (id->device->node_type) {
4738 	case RDMA_NODE_IB_CA:
4739 		ret = ib_cm_notify(id_priv->cm_id.ib, event);
4740 		break;
4741 	default:
4742 		ret = 0;
4743 		break;
4744 	}
4745 	return ret;
4746 }
4747 EXPORT_SYMBOL(rdma_notify);
4748 
rdma_reject(struct rdma_cm_id * id,const void * private_data,u8 private_data_len,u8 reason)4749 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
4750 		u8 private_data_len, u8 reason)
4751 {
4752 	struct rdma_id_private *id_priv;
4753 	int ret;
4754 
4755 	id_priv = container_of(id, struct rdma_id_private, id);
4756 	if (!id_priv->cm_id.ib)
4757 		return -EINVAL;
4758 
4759 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4760 		if (id->qp_type == IB_QPT_UD) {
4761 			ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
4762 						private_data, private_data_len);
4763 		} else {
4764 			trace_cm_send_rej(id_priv);
4765 			ret = ib_send_cm_rej(id_priv->cm_id.ib, reason, NULL, 0,
4766 					     private_data, private_data_len);
4767 		}
4768 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4769 		ret = iw_cm_reject(id_priv->cm_id.iw,
4770 				   private_data, private_data_len);
4771 	} else {
4772 		ret = -ENOSYS;
4773 	}
4774 
4775 	return ret;
4776 }
4777 EXPORT_SYMBOL(rdma_reject);
4778 
rdma_disconnect(struct rdma_cm_id * id)4779 int rdma_disconnect(struct rdma_cm_id *id)
4780 {
4781 	struct rdma_id_private *id_priv;
4782 	int ret;
4783 
4784 	id_priv = container_of(id, struct rdma_id_private, id);
4785 	if (!id_priv->cm_id.ib)
4786 		return -EINVAL;
4787 
4788 	if (rdma_cap_ib_cm(id->device, id->port_num)) {
4789 		ret = cma_modify_qp_err(id_priv);
4790 		if (ret)
4791 			goto out;
4792 		/* Initiate or respond to a disconnect. */
4793 		trace_cm_disconnect(id_priv);
4794 		if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) {
4795 			if (!ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0))
4796 				trace_cm_sent_drep(id_priv);
4797 		} else {
4798 			trace_cm_sent_dreq(id_priv);
4799 		}
4800 	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4801 		ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
4802 	} else
4803 		ret = -EINVAL;
4804 
4805 out:
4806 	return ret;
4807 }
4808 EXPORT_SYMBOL(rdma_disconnect);
4809 
cma_make_mc_event(int status,struct rdma_id_private * id_priv,struct ib_sa_multicast * multicast,struct rdma_cm_event * event,struct cma_multicast * mc)4810 static void cma_make_mc_event(int status, struct rdma_id_private *id_priv,
4811 			      struct ib_sa_multicast *multicast,
4812 			      struct rdma_cm_event *event,
4813 			      struct cma_multicast *mc)
4814 {
4815 	struct rdma_dev_addr *dev_addr;
4816 	enum ib_gid_type gid_type;
4817 	struct net_device *ndev;
4818 
4819 	if (status)
4820 		pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to join multicast. status %d\n",
4821 				     status);
4822 
4823 	event->status = status;
4824 	event->param.ud.private_data = mc->context;
4825 	if (status) {
4826 		event->event = RDMA_CM_EVENT_MULTICAST_ERROR;
4827 		return;
4828 	}
4829 
4830 	dev_addr = &id_priv->id.route.addr.dev_addr;
4831 	ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4832 	gid_type =
4833 		id_priv->cma_dev
4834 			->default_gid_type[id_priv->id.port_num -
4835 					   rdma_start_port(
4836 						   id_priv->cma_dev->device)];
4837 
4838 	event->event = RDMA_CM_EVENT_MULTICAST_JOIN;
4839 	if (ib_init_ah_from_mcmember(id_priv->id.device, id_priv->id.port_num,
4840 				     &multicast->rec, ndev, gid_type,
4841 				     &event->param.ud.ah_attr)) {
4842 		event->event = RDMA_CM_EVENT_MULTICAST_ERROR;
4843 		goto out;
4844 	}
4845 
4846 	event->param.ud.qp_num = 0xFFFFFF;
4847 	event->param.ud.qkey = id_priv->qkey;
4848 
4849 out:
4850 	dev_put(ndev);
4851 }
4852 
cma_ib_mc_handler(int status,struct ib_sa_multicast * multicast)4853 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
4854 {
4855 	struct cma_multicast *mc = multicast->context;
4856 	struct rdma_id_private *id_priv = mc->id_priv;
4857 	struct rdma_cm_event event = {};
4858 	int ret = 0;
4859 
4860 	mutex_lock(&id_priv->handler_mutex);
4861 	if (READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL ||
4862 	    READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING)
4863 		goto out;
4864 
4865 	ret = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
4866 	if (!ret) {
4867 		cma_make_mc_event(status, id_priv, multicast, &event, mc);
4868 		ret = cma_cm_event_handler(id_priv, &event);
4869 	}
4870 	rdma_destroy_ah_attr(&event.param.ud.ah_attr);
4871 	WARN_ON(ret);
4872 
4873 out:
4874 	mutex_unlock(&id_priv->handler_mutex);
4875 	return 0;
4876 }
4877 
cma_set_mgid(struct rdma_id_private * id_priv,struct sockaddr * addr,union ib_gid * mgid)4878 static void cma_set_mgid(struct rdma_id_private *id_priv,
4879 			 struct sockaddr *addr, union ib_gid *mgid)
4880 {
4881 	unsigned char mc_map[MAX_ADDR_LEN];
4882 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4883 	struct sockaddr_in *sin = (struct sockaddr_in *) addr;
4884 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
4885 
4886 	if (cma_any_addr(addr)) {
4887 		memset(mgid, 0, sizeof *mgid);
4888 	} else if ((addr->sa_family == AF_INET6) &&
4889 		   ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
4890 								 0xFF10A01B)) {
4891 		/* IPv6 address is an SA assigned MGID. */
4892 		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4893 	} else if (addr->sa_family == AF_IB) {
4894 		memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
4895 	} else if (addr->sa_family == AF_INET6) {
4896 		ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
4897 		if (id_priv->id.ps == RDMA_PS_UDP)
4898 			mc_map[7] = 0x01;	/* Use RDMA CM signature */
4899 		*mgid = *(union ib_gid *) (mc_map + 4);
4900 	} else {
4901 		ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
4902 		if (id_priv->id.ps == RDMA_PS_UDP)
4903 			mc_map[7] = 0x01;	/* Use RDMA CM signature */
4904 		*mgid = *(union ib_gid *) (mc_map + 4);
4905 	}
4906 }
4907 
cma_join_ib_multicast(struct rdma_id_private * id_priv,struct cma_multicast * mc)4908 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
4909 				 struct cma_multicast *mc)
4910 {
4911 	struct ib_sa_mcmember_rec rec;
4912 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4913 	ib_sa_comp_mask comp_mask;
4914 	int ret;
4915 
4916 	ib_addr_get_mgid(dev_addr, &rec.mgid);
4917 	ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
4918 				     &rec.mgid, &rec);
4919 	if (ret)
4920 		return ret;
4921 
4922 	if (!id_priv->qkey) {
4923 		ret = cma_set_default_qkey(id_priv);
4924 		if (ret)
4925 			return ret;
4926 	}
4927 
4928 	cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
4929 	rec.qkey = cpu_to_be32(id_priv->qkey);
4930 	rdma_addr_get_sgid(dev_addr, &rec.port_gid);
4931 	rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
4932 	rec.join_state = mc->join_state;
4933 
4934 	comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
4935 		    IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
4936 		    IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
4937 		    IB_SA_MCMEMBER_REC_FLOW_LABEL |
4938 		    IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
4939 
4940 	if (id_priv->id.ps == RDMA_PS_IPOIB)
4941 		comp_mask |= IB_SA_MCMEMBER_REC_RATE |
4942 			     IB_SA_MCMEMBER_REC_RATE_SELECTOR |
4943 			     IB_SA_MCMEMBER_REC_MTU_SELECTOR |
4944 			     IB_SA_MCMEMBER_REC_MTU |
4945 			     IB_SA_MCMEMBER_REC_HOP_LIMIT;
4946 
4947 	mc->sa_mc = ib_sa_join_multicast(&sa_client, id_priv->id.device,
4948 					 id_priv->id.port_num, &rec, comp_mask,
4949 					 GFP_KERNEL, cma_ib_mc_handler, mc);
4950 	return PTR_ERR_OR_ZERO(mc->sa_mc);
4951 }
4952 
cma_iboe_set_mgid(struct sockaddr * addr,union ib_gid * mgid,enum ib_gid_type gid_type)4953 static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid,
4954 			      enum ib_gid_type gid_type)
4955 {
4956 	struct sockaddr_in *sin = (struct sockaddr_in *)addr;
4957 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
4958 
4959 	if (cma_any_addr(addr)) {
4960 		memset(mgid, 0, sizeof *mgid);
4961 	} else if (addr->sa_family == AF_INET6) {
4962 		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4963 	} else {
4964 		mgid->raw[0] =
4965 			(gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0xff;
4966 		mgid->raw[1] =
4967 			(gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0x0e;
4968 		mgid->raw[2] = 0;
4969 		mgid->raw[3] = 0;
4970 		mgid->raw[4] = 0;
4971 		mgid->raw[5] = 0;
4972 		mgid->raw[6] = 0;
4973 		mgid->raw[7] = 0;
4974 		mgid->raw[8] = 0;
4975 		mgid->raw[9] = 0;
4976 		mgid->raw[10] = 0xff;
4977 		mgid->raw[11] = 0xff;
4978 		*(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
4979 	}
4980 }
4981 
cma_iboe_join_multicast(struct rdma_id_private * id_priv,struct cma_multicast * mc)4982 static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
4983 				   struct cma_multicast *mc)
4984 {
4985 	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4986 	int err = 0;
4987 	struct sockaddr *addr = (struct sockaddr *)&mc->addr;
4988 	struct net_device *ndev = NULL;
4989 	struct ib_sa_multicast ib = {};
4990 	enum ib_gid_type gid_type;
4991 	bool send_only;
4992 
4993 	send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
4994 
4995 	if (cma_zero_addr(addr))
4996 		return -EINVAL;
4997 
4998 	gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
4999 		   rdma_start_port(id_priv->cma_dev->device)];
5000 	cma_iboe_set_mgid(addr, &ib.rec.mgid, gid_type);
5001 
5002 	ib.rec.pkey = cpu_to_be16(0xffff);
5003 	if (dev_addr->bound_dev_if)
5004 		ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
5005 	if (!ndev)
5006 		return -ENODEV;
5007 
5008 	ib.rec.rate = IB_RATE_PORT_CURRENT;
5009 	ib.rec.hop_limit = 1;
5010 	ib.rec.mtu = iboe_get_mtu(ndev->mtu);
5011 
5012 	if (addr->sa_family == AF_INET) {
5013 		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
5014 			ib.rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
5015 			if (!send_only) {
5016 				err = cma_igmp_send(ndev, &ib.rec.mgid,
5017 						    true);
5018 			}
5019 		}
5020 	} else {
5021 		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
5022 			err = -ENOTSUPP;
5023 	}
5024 	dev_put(ndev);
5025 	if (err || !ib.rec.mtu)
5026 		return err ?: -EINVAL;
5027 
5028 	if (!id_priv->qkey)
5029 		cma_set_default_qkey(id_priv);
5030 
5031 	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
5032 		    &ib.rec.port_gid);
5033 	INIT_WORK(&mc->iboe_join.work, cma_iboe_join_work_handler);
5034 	cma_make_mc_event(0, id_priv, &ib, &mc->iboe_join.event, mc);
5035 	queue_work(cma_wq, &mc->iboe_join.work);
5036 	return 0;
5037 }
5038 
rdma_join_multicast(struct rdma_cm_id * id,struct sockaddr * addr,u8 join_state,void * context)5039 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
5040 			u8 join_state, void *context)
5041 {
5042 	struct rdma_id_private *id_priv =
5043 		container_of(id, struct rdma_id_private, id);
5044 	struct cma_multicast *mc;
5045 	int ret;
5046 
5047 	/* Not supported for kernel QPs */
5048 	if (WARN_ON(id->qp))
5049 		return -EINVAL;
5050 
5051 	/* ULP is calling this wrong. */
5052 	if (!id->device || (READ_ONCE(id_priv->state) != RDMA_CM_ADDR_BOUND &&
5053 			    READ_ONCE(id_priv->state) != RDMA_CM_ADDR_RESOLVED))
5054 		return -EINVAL;
5055 
5056 	if (id_priv->id.qp_type != IB_QPT_UD)
5057 		return -EINVAL;
5058 
5059 	mc = kzalloc(sizeof(*mc), GFP_KERNEL);
5060 	if (!mc)
5061 		return -ENOMEM;
5062 
5063 	memcpy(&mc->addr, addr, rdma_addr_size(addr));
5064 	mc->context = context;
5065 	mc->id_priv = id_priv;
5066 	mc->join_state = join_state;
5067 
5068 	if (rdma_protocol_roce(id->device, id->port_num)) {
5069 		ret = cma_iboe_join_multicast(id_priv, mc);
5070 		if (ret)
5071 			goto out_err;
5072 	} else if (rdma_cap_ib_mcast(id->device, id->port_num)) {
5073 		ret = cma_join_ib_multicast(id_priv, mc);
5074 		if (ret)
5075 			goto out_err;
5076 	} else {
5077 		ret = -ENOSYS;
5078 		goto out_err;
5079 	}
5080 
5081 	spin_lock(&id_priv->lock);
5082 	list_add(&mc->list, &id_priv->mc_list);
5083 	spin_unlock(&id_priv->lock);
5084 
5085 	return 0;
5086 out_err:
5087 	kfree(mc);
5088 	return ret;
5089 }
5090 EXPORT_SYMBOL(rdma_join_multicast);
5091 
rdma_leave_multicast(struct rdma_cm_id * id,struct sockaddr * addr)5092 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
5093 {
5094 	struct rdma_id_private *id_priv;
5095 	struct cma_multicast *mc;
5096 
5097 	id_priv = container_of(id, struct rdma_id_private, id);
5098 	spin_lock_irq(&id_priv->lock);
5099 	list_for_each_entry(mc, &id_priv->mc_list, list) {
5100 		if (memcmp(&mc->addr, addr, rdma_addr_size(addr)) != 0)
5101 			continue;
5102 		list_del(&mc->list);
5103 		spin_unlock_irq(&id_priv->lock);
5104 
5105 		WARN_ON(id_priv->cma_dev->device != id->device);
5106 		destroy_mc(id_priv, mc);
5107 		return;
5108 	}
5109 	spin_unlock_irq(&id_priv->lock);
5110 }
5111 EXPORT_SYMBOL(rdma_leave_multicast);
5112 
cma_netdev_change(struct net_device * ndev,struct rdma_id_private * id_priv)5113 static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
5114 {
5115 	struct rdma_dev_addr *dev_addr;
5116 	struct cma_work *work;
5117 
5118 	dev_addr = &id_priv->id.route.addr.dev_addr;
5119 
5120 	if ((dev_addr->bound_dev_if == ndev->ifindex) &&
5121 	    (net_eq(dev_net(ndev), dev_addr->net)) &&
5122 	    memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
5123 		pr_info("RDMA CM addr change for ndev %s used by id %p\n",
5124 			ndev->name, &id_priv->id);
5125 		work = kzalloc(sizeof *work, GFP_KERNEL);
5126 		if (!work)
5127 			return -ENOMEM;
5128 
5129 		INIT_WORK(&work->work, cma_work_handler);
5130 		work->id = id_priv;
5131 		work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
5132 		cma_id_get(id_priv);
5133 		queue_work(cma_wq, &work->work);
5134 	}
5135 
5136 	return 0;
5137 }
5138 
cma_netdev_callback(struct notifier_block * self,unsigned long event,void * ptr)5139 static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
5140 			       void *ptr)
5141 {
5142 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
5143 	struct cma_device *cma_dev;
5144 	struct rdma_id_private *id_priv;
5145 	int ret = NOTIFY_DONE;
5146 
5147 	if (event != NETDEV_BONDING_FAILOVER)
5148 		return NOTIFY_DONE;
5149 
5150 	if (!netif_is_bond_master(ndev))
5151 		return NOTIFY_DONE;
5152 
5153 	mutex_lock(&lock);
5154 	list_for_each_entry(cma_dev, &dev_list, list)
5155 		list_for_each_entry(id_priv, &cma_dev->id_list, device_item) {
5156 			ret = cma_netdev_change(ndev, id_priv);
5157 			if (ret)
5158 				goto out;
5159 		}
5160 
5161 out:
5162 	mutex_unlock(&lock);
5163 	return ret;
5164 }
5165 
cma_netevent_work_handler(struct work_struct * _work)5166 static void cma_netevent_work_handler(struct work_struct *_work)
5167 {
5168 	struct rdma_id_private *id_priv =
5169 		container_of(_work, struct rdma_id_private, id.net_work);
5170 	struct rdma_cm_event event = {};
5171 
5172 	mutex_lock(&id_priv->handler_mutex);
5173 
5174 	if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
5175 	    READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
5176 		goto out_unlock;
5177 
5178 	event.event = RDMA_CM_EVENT_UNREACHABLE;
5179 	event.status = -ETIMEDOUT;
5180 
5181 	if (cma_cm_event_handler(id_priv, &event)) {
5182 		__acquire(&id_priv->handler_mutex);
5183 		id_priv->cm_id.ib = NULL;
5184 		cma_id_put(id_priv);
5185 		destroy_id_handler_unlock(id_priv);
5186 		return;
5187 	}
5188 
5189 out_unlock:
5190 	mutex_unlock(&id_priv->handler_mutex);
5191 	cma_id_put(id_priv);
5192 }
5193 
cma_netevent_callback(struct notifier_block * self,unsigned long event,void * ctx)5194 static int cma_netevent_callback(struct notifier_block *self,
5195 				 unsigned long event, void *ctx)
5196 {
5197 	struct id_table_entry *ips_node = NULL;
5198 	struct rdma_id_private *current_id;
5199 	struct neighbour *neigh = ctx;
5200 	unsigned long flags;
5201 
5202 	if (event != NETEVENT_NEIGH_UPDATE)
5203 		return NOTIFY_DONE;
5204 
5205 	spin_lock_irqsave(&id_table_lock, flags);
5206 	if (neigh->tbl->family == AF_INET6) {
5207 		struct sockaddr_in6 neigh_sock_6;
5208 
5209 		neigh_sock_6.sin6_family = AF_INET6;
5210 		neigh_sock_6.sin6_addr = *(struct in6_addr *)neigh->primary_key;
5211 		ips_node = node_from_ndev_ip(&id_table, neigh->dev->ifindex,
5212 					     (struct sockaddr *)&neigh_sock_6);
5213 	} else if (neigh->tbl->family == AF_INET) {
5214 		struct sockaddr_in neigh_sock_4;
5215 
5216 		neigh_sock_4.sin_family = AF_INET;
5217 		neigh_sock_4.sin_addr.s_addr = *(__be32 *)(neigh->primary_key);
5218 		ips_node = node_from_ndev_ip(&id_table, neigh->dev->ifindex,
5219 					     (struct sockaddr *)&neigh_sock_4);
5220 	} else
5221 		goto out;
5222 
5223 	if (!ips_node)
5224 		goto out;
5225 
5226 	list_for_each_entry(current_id, &ips_node->id_list, id_list_entry) {
5227 		if (!memcmp(current_id->id.route.addr.dev_addr.dst_dev_addr,
5228 			   neigh->ha, ETH_ALEN))
5229 			continue;
5230 		INIT_WORK(&current_id->id.net_work, cma_netevent_work_handler);
5231 		cma_id_get(current_id);
5232 		queue_work(cma_wq, &current_id->id.net_work);
5233 	}
5234 out:
5235 	spin_unlock_irqrestore(&id_table_lock, flags);
5236 	return NOTIFY_DONE;
5237 }
5238 
5239 static struct notifier_block cma_nb = {
5240 	.notifier_call = cma_netdev_callback
5241 };
5242 
5243 static struct notifier_block cma_netevent_cb = {
5244 	.notifier_call = cma_netevent_callback
5245 };
5246 
cma_send_device_removal_put(struct rdma_id_private * id_priv)5247 static void cma_send_device_removal_put(struct rdma_id_private *id_priv)
5248 {
5249 	struct rdma_cm_event event = { .event = RDMA_CM_EVENT_DEVICE_REMOVAL };
5250 	enum rdma_cm_state state;
5251 	unsigned long flags;
5252 
5253 	mutex_lock(&id_priv->handler_mutex);
5254 	/* Record that we want to remove the device */
5255 	spin_lock_irqsave(&id_priv->lock, flags);
5256 	state = id_priv->state;
5257 	if (state == RDMA_CM_DESTROYING || state == RDMA_CM_DEVICE_REMOVAL) {
5258 		spin_unlock_irqrestore(&id_priv->lock, flags);
5259 		mutex_unlock(&id_priv->handler_mutex);
5260 		cma_id_put(id_priv);
5261 		return;
5262 	}
5263 	id_priv->state = RDMA_CM_DEVICE_REMOVAL;
5264 	spin_unlock_irqrestore(&id_priv->lock, flags);
5265 
5266 	if (cma_cm_event_handler(id_priv, &event)) {
5267 		/*
5268 		 * At this point the ULP promises it won't call
5269 		 * rdma_destroy_id() concurrently
5270 		 */
5271 		cma_id_put(id_priv);
5272 		mutex_unlock(&id_priv->handler_mutex);
5273 		trace_cm_id_destroy(id_priv);
5274 		_destroy_id(id_priv, state);
5275 		return;
5276 	}
5277 	mutex_unlock(&id_priv->handler_mutex);
5278 
5279 	/*
5280 	 * If this races with destroy then the thread that first assigns state
5281 	 * to a destroying does the cancel.
5282 	 */
5283 	cma_cancel_operation(id_priv, state);
5284 	cma_id_put(id_priv);
5285 }
5286 
cma_process_remove(struct cma_device * cma_dev)5287 static void cma_process_remove(struct cma_device *cma_dev)
5288 {
5289 	mutex_lock(&lock);
5290 	while (!list_empty(&cma_dev->id_list)) {
5291 		struct rdma_id_private *id_priv = list_first_entry(
5292 			&cma_dev->id_list, struct rdma_id_private, device_item);
5293 
5294 		list_del_init(&id_priv->listen_item);
5295 		list_del_init(&id_priv->device_item);
5296 		cma_id_get(id_priv);
5297 		mutex_unlock(&lock);
5298 
5299 		cma_send_device_removal_put(id_priv);
5300 
5301 		mutex_lock(&lock);
5302 	}
5303 	mutex_unlock(&lock);
5304 
5305 	cma_dev_put(cma_dev);
5306 	wait_for_completion(&cma_dev->comp);
5307 }
5308 
cma_supported(struct ib_device * device)5309 static bool cma_supported(struct ib_device *device)
5310 {
5311 	u32 i;
5312 
5313 	rdma_for_each_port(device, i) {
5314 		if (rdma_cap_ib_cm(device, i) || rdma_cap_iw_cm(device, i))
5315 			return true;
5316 	}
5317 	return false;
5318 }
5319 
cma_add_one(struct ib_device * device)5320 static int cma_add_one(struct ib_device *device)
5321 {
5322 	struct rdma_id_private *to_destroy;
5323 	struct cma_device *cma_dev;
5324 	struct rdma_id_private *id_priv;
5325 	unsigned long supported_gids = 0;
5326 	int ret;
5327 	u32 i;
5328 
5329 	if (!cma_supported(device))
5330 		return -EOPNOTSUPP;
5331 
5332 	cma_dev = kmalloc(sizeof(*cma_dev), GFP_KERNEL);
5333 	if (!cma_dev)
5334 		return -ENOMEM;
5335 
5336 	cma_dev->device = device;
5337 	cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
5338 					    sizeof(*cma_dev->default_gid_type),
5339 					    GFP_KERNEL);
5340 	if (!cma_dev->default_gid_type) {
5341 		ret = -ENOMEM;
5342 		goto free_cma_dev;
5343 	}
5344 
5345 	cma_dev->default_roce_tos = kcalloc(device->phys_port_cnt,
5346 					    sizeof(*cma_dev->default_roce_tos),
5347 					    GFP_KERNEL);
5348 	if (!cma_dev->default_roce_tos) {
5349 		ret = -ENOMEM;
5350 		goto free_gid_type;
5351 	}
5352 
5353 	rdma_for_each_port (device, i) {
5354 		supported_gids = roce_gid_type_mask_support(device, i);
5355 		WARN_ON(!supported_gids);
5356 		if (supported_gids & (1 << CMA_PREFERRED_ROCE_GID_TYPE))
5357 			cma_dev->default_gid_type[i - rdma_start_port(device)] =
5358 				CMA_PREFERRED_ROCE_GID_TYPE;
5359 		else
5360 			cma_dev->default_gid_type[i - rdma_start_port(device)] =
5361 				find_first_bit(&supported_gids, BITS_PER_LONG);
5362 		cma_dev->default_roce_tos[i - rdma_start_port(device)] = 0;
5363 	}
5364 
5365 	init_completion(&cma_dev->comp);
5366 	refcount_set(&cma_dev->refcount, 1);
5367 	INIT_LIST_HEAD(&cma_dev->id_list);
5368 	ib_set_client_data(device, &cma_client, cma_dev);
5369 
5370 	mutex_lock(&lock);
5371 	list_add_tail(&cma_dev->list, &dev_list);
5372 	list_for_each_entry(id_priv, &listen_any_list, listen_any_item) {
5373 		ret = cma_listen_on_dev(id_priv, cma_dev, &to_destroy);
5374 		if (ret)
5375 			goto free_listen;
5376 	}
5377 	mutex_unlock(&lock);
5378 
5379 	trace_cm_add_one(device);
5380 	return 0;
5381 
5382 free_listen:
5383 	list_del(&cma_dev->list);
5384 	mutex_unlock(&lock);
5385 
5386 	/* cma_process_remove() will delete to_destroy */
5387 	cma_process_remove(cma_dev);
5388 	kfree(cma_dev->default_roce_tos);
5389 free_gid_type:
5390 	kfree(cma_dev->default_gid_type);
5391 
5392 free_cma_dev:
5393 	kfree(cma_dev);
5394 	return ret;
5395 }
5396 
cma_remove_one(struct ib_device * device,void * client_data)5397 static void cma_remove_one(struct ib_device *device, void *client_data)
5398 {
5399 	struct cma_device *cma_dev = client_data;
5400 
5401 	trace_cm_remove_one(device);
5402 
5403 	mutex_lock(&lock);
5404 	list_del(&cma_dev->list);
5405 	mutex_unlock(&lock);
5406 
5407 	cma_process_remove(cma_dev);
5408 	kfree(cma_dev->default_roce_tos);
5409 	kfree(cma_dev->default_gid_type);
5410 	kfree(cma_dev);
5411 }
5412 
cma_init_net(struct net * net)5413 static int cma_init_net(struct net *net)
5414 {
5415 	struct cma_pernet *pernet = cma_pernet(net);
5416 
5417 	xa_init(&pernet->tcp_ps);
5418 	xa_init(&pernet->udp_ps);
5419 	xa_init(&pernet->ipoib_ps);
5420 	xa_init(&pernet->ib_ps);
5421 
5422 	return 0;
5423 }
5424 
cma_exit_net(struct net * net)5425 static void cma_exit_net(struct net *net)
5426 {
5427 	struct cma_pernet *pernet = cma_pernet(net);
5428 
5429 	WARN_ON(!xa_empty(&pernet->tcp_ps));
5430 	WARN_ON(!xa_empty(&pernet->udp_ps));
5431 	WARN_ON(!xa_empty(&pernet->ipoib_ps));
5432 	WARN_ON(!xa_empty(&pernet->ib_ps));
5433 }
5434 
5435 static struct pernet_operations cma_pernet_operations = {
5436 	.init = cma_init_net,
5437 	.exit = cma_exit_net,
5438 	.id = &cma_pernet_id,
5439 	.size = sizeof(struct cma_pernet),
5440 };
5441 
cma_init(void)5442 static int __init cma_init(void)
5443 {
5444 	int ret;
5445 
5446 	/*
5447 	 * There is a rare lock ordering dependency in cma_netdev_callback()
5448 	 * that only happens when bonding is enabled. Teach lockdep that rtnl
5449 	 * must never be nested under lock so it can find these without having
5450 	 * to test with bonding.
5451 	 */
5452 	if (IS_ENABLED(CONFIG_LOCKDEP)) {
5453 		rtnl_lock();
5454 		mutex_lock(&lock);
5455 		mutex_unlock(&lock);
5456 		rtnl_unlock();
5457 	}
5458 
5459 	cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
5460 	if (!cma_wq)
5461 		return -ENOMEM;
5462 
5463 	ret = register_pernet_subsys(&cma_pernet_operations);
5464 	if (ret)
5465 		goto err_wq;
5466 
5467 	ib_sa_register_client(&sa_client);
5468 	register_netdevice_notifier(&cma_nb);
5469 	register_netevent_notifier(&cma_netevent_cb);
5470 
5471 	ret = ib_register_client(&cma_client);
5472 	if (ret)
5473 		goto err;
5474 
5475 	ret = cma_configfs_init();
5476 	if (ret)
5477 		goto err_ib;
5478 
5479 	return 0;
5480 
5481 err_ib:
5482 	ib_unregister_client(&cma_client);
5483 err:
5484 	unregister_netevent_notifier(&cma_netevent_cb);
5485 	unregister_netdevice_notifier(&cma_nb);
5486 	ib_sa_unregister_client(&sa_client);
5487 	unregister_pernet_subsys(&cma_pernet_operations);
5488 err_wq:
5489 	destroy_workqueue(cma_wq);
5490 	return ret;
5491 }
5492 
cma_cleanup(void)5493 static void __exit cma_cleanup(void)
5494 {
5495 	cma_configfs_exit();
5496 	ib_unregister_client(&cma_client);
5497 	unregister_netevent_notifier(&cma_netevent_cb);
5498 	unregister_netdevice_notifier(&cma_nb);
5499 	ib_sa_unregister_client(&sa_client);
5500 	unregister_pernet_subsys(&cma_pernet_operations);
5501 	destroy_workqueue(cma_wq);
5502 }
5503 
5504 module_init(cma_init);
5505 module_exit(cma_cleanup);
5506