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