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