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