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