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