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