ib_cma.c revision 6c898cf89354bce07394c040a9cbd40f790c86ee
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_len = sizeof(struct sockaddr_in6),
1122			.sin6_family = AF_INET6,
1123			.sin6_addr = hdr->dst_addr.ip6,
1124			.sin6_port = local_port,
1125		};
1126	}
1127
1128	if (dst_addr) {
1129		*dst_addr = (struct sockaddr_in6) {
1130			.sin6_len = sizeof(struct sockaddr_in6),
1131			.sin6_family = AF_INET6,
1132			.sin6_addr = hdr->src_addr.ip6,
1133			.sin6_port = hdr->port,
1134		};
1135	}
1136}
1137
1138static u16 cma_port_from_service_id(__be64 service_id)
1139{
1140	return (u16)be64_to_cpu(service_id);
1141}
1142
1143static int cma_save_ip_info(struct sockaddr *src_addr,
1144			    struct sockaddr *dst_addr,
1145			    struct ib_cm_event *ib_event,
1146			    __be64 service_id)
1147{
1148	struct cma_hdr *hdr;
1149	__be16 port;
1150
1151	hdr = ib_event->private_data;
1152	if (hdr->cma_version != CMA_VERSION)
1153		return -EINVAL;
1154
1155	port = htons(cma_port_from_service_id(service_id));
1156
1157	switch (cma_get_ip_ver(hdr)) {
1158	case 4:
1159		cma_save_ip4_info((struct sockaddr_in *)src_addr,
1160				  (struct sockaddr_in *)dst_addr, hdr, port);
1161		break;
1162	case 6:
1163		cma_save_ip6_info((struct sockaddr_in6 *)src_addr,
1164				  (struct sockaddr_in6 *)dst_addr, hdr, port);
1165		break;
1166	default:
1167		return -EAFNOSUPPORT;
1168	}
1169
1170	return 0;
1171}
1172
1173static int cma_save_net_info(struct sockaddr *src_addr,
1174			     struct sockaddr *dst_addr,
1175			     struct rdma_cm_id *listen_id,
1176			     struct ib_cm_event *ib_event,
1177			     sa_family_t sa_family, __be64 service_id)
1178{
1179	if (sa_family == AF_IB) {
1180		if (ib_event->event == IB_CM_REQ_RECEIVED)
1181			cma_save_ib_info(src_addr, dst_addr, listen_id,
1182					 ib_event->param.req_rcvd.primary_path);
1183		else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1184			cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
1185		return 0;
1186	}
1187
1188	return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
1189}
1190
1191static int cma_save_req_info(const struct ib_cm_event *ib_event,
1192			     struct cma_req_info *req)
1193{
1194	const struct ib_cm_req_event_param *req_param =
1195		&ib_event->param.req_rcvd;
1196	const struct ib_cm_sidr_req_event_param *sidr_param =
1197		&ib_event->param.sidr_req_rcvd;
1198
1199	switch (ib_event->event) {
1200	case IB_CM_REQ_RECEIVED:
1201		req->device	= req_param->listen_id->device;
1202		req->port	= req_param->port;
1203		memcpy(&req->local_gid, &req_param->primary_path->sgid,
1204		       sizeof(req->local_gid));
1205		req->has_gid	= true;
1206		req->service_id	= req_param->primary_path->service_id;
1207		req->pkey	= be16_to_cpu(req_param->primary_path->pkey);
1208		if (req->pkey != req_param->bth_pkey)
1209			pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1210					    "RDMA CMA: in the future this may cause the request to be dropped\n",
1211					    req_param->bth_pkey, req->pkey);
1212		break;
1213	case IB_CM_SIDR_REQ_RECEIVED:
1214		req->device	= sidr_param->listen_id->device;
1215		req->port	= sidr_param->port;
1216		req->has_gid	= false;
1217		req->service_id	= sidr_param->service_id;
1218		req->pkey	= sidr_param->pkey;
1219		if (req->pkey != sidr_param->bth_pkey)
1220			pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1221					    "RDMA CMA: in the future this may cause the request to be dropped\n",
1222					    sidr_param->bth_pkey, req->pkey);
1223		break;
1224	default:
1225		return -EINVAL;
1226	}
1227
1228	return 0;
1229}
1230
1231static bool validate_ipv4_net_dev(struct net_device *net_dev,
1232				  const struct sockaddr_in *dst_addr,
1233				  const struct sockaddr_in *src_addr)
1234{
1235#ifdef INET
1236	struct sockaddr_in dst_tmp = *dst_addr;
1237	__be32 daddr = dst_addr->sin_addr.s_addr,
1238	       saddr = src_addr->sin_addr.s_addr;
1239	struct net_device *src_dev;
1240	struct rtentry *rte;
1241	bool ret;
1242
1243	if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1244	    ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) ||
1245	    ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) ||
1246	    ipv4_is_loopback(saddr))
1247		return false;
1248
1249	src_dev = ip_dev_find(net_dev->if_vnet, saddr);
1250	if (src_dev != net_dev) {
1251		if (src_dev != NULL)
1252			dev_put(src_dev);
1253		return false;
1254	}
1255	dev_put(src_dev);
1256
1257	/*
1258	 * Make sure the socket address length field
1259	 * is set, else rtalloc1() will fail.
1260	 */
1261	dst_tmp.sin_len = sizeof(dst_tmp);
1262
1263	CURVNET_SET(net_dev->if_vnet);
1264	rte = rtalloc1((struct sockaddr *)&dst_tmp, 1, 0);
1265	CURVNET_RESTORE();
1266	if (rte != NULL) {
1267		ret = (rte->rt_ifp == net_dev);
1268		RTFREE_LOCKED(rte);
1269	} else {
1270		ret = false;
1271	}
1272	return ret;
1273#else
1274	return false;
1275#endif
1276}
1277
1278static bool validate_ipv6_net_dev(struct net_device *net_dev,
1279				  const struct sockaddr_in6 *dst_addr,
1280				  const struct sockaddr_in6 *src_addr)
1281{
1282#ifdef INET6
1283	struct sockaddr_in6 dst_tmp = *dst_addr;
1284	struct in6_addr in6_addr = src_addr->sin6_addr;
1285	struct net_device *src_dev;
1286	struct rtentry *rte;
1287	bool ret;
1288
1289	/* embed scope ID */
1290	in6_addr.s6_addr[3] = src_addr->sin6_scope_id;
1291
1292	src_dev = ip6_dev_find(net_dev->if_vnet, in6_addr);
1293	if (src_dev != net_dev)
1294		return false;
1295
1296	/*
1297	 * Make sure the socket address length field
1298	 * is set, else rtalloc1() will fail.
1299	 */
1300	dst_tmp.sin6_len = sizeof(dst_tmp);
1301
1302	CURVNET_SET(net_dev->if_vnet);
1303	rte = rtalloc1((struct sockaddr *)&dst_tmp, 1, 0);
1304	CURVNET_RESTORE();
1305	if (rte != NULL) {
1306		ret = (rte->rt_ifp == net_dev);
1307		RTFREE_LOCKED(rte);
1308	} else {
1309		ret = false;
1310	}
1311	return ret;
1312#else
1313	return false;
1314#endif
1315}
1316
1317static bool validate_net_dev(struct net_device *net_dev,
1318			     const struct sockaddr *daddr,
1319			     const struct sockaddr *saddr)
1320{
1321	const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
1322	const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
1323	const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1324	const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
1325
1326	switch (daddr->sa_family) {
1327	case AF_INET:
1328		return saddr->sa_family == AF_INET &&
1329		       validate_ipv4_net_dev(net_dev, daddr4, saddr4);
1330
1331	case AF_INET6:
1332		return saddr->sa_family == AF_INET6 &&
1333		       validate_ipv6_net_dev(net_dev, daddr6, saddr6);
1334
1335	default:
1336		return false;
1337	}
1338}
1339
1340static struct net_device *cma_get_net_dev(struct ib_cm_event *ib_event,
1341					  const struct cma_req_info *req)
1342{
1343	struct sockaddr_storage listen_addr_storage, src_addr_storage;
1344	struct sockaddr *listen_addr = (struct sockaddr *)&listen_addr_storage,
1345			*src_addr = (struct sockaddr *)&src_addr_storage;
1346	struct net_device *net_dev;
1347	const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
1348	int err;
1349
1350	err = cma_save_ip_info(listen_addr, src_addr, ib_event,
1351			       req->service_id);
1352	if (err)
1353		return ERR_PTR(err);
1354
1355	net_dev = ib_get_net_dev_by_params(req->device, req->port, req->pkey,
1356					   gid, listen_addr);
1357	if (!net_dev)
1358		return ERR_PTR(-ENODEV);
1359
1360	if (!validate_net_dev(net_dev, listen_addr, src_addr)) {
1361		dev_put(net_dev);
1362		return ERR_PTR(-EHOSTUNREACH);
1363	}
1364
1365	return net_dev;
1366}
1367
1368static enum rdma_port_space rdma_ps_from_service_id(__be64 service_id)
1369{
1370	return (be64_to_cpu(service_id) >> 16) & 0xffff;
1371}
1372
1373static bool cma_match_private_data(struct rdma_id_private *id_priv,
1374				   const struct cma_hdr *hdr)
1375{
1376	struct sockaddr *addr = cma_src_addr(id_priv);
1377	__be32 ip4_addr;
1378	struct in6_addr ip6_addr;
1379
1380	if (cma_any_addr(addr) && !id_priv->afonly)
1381		return true;
1382
1383	switch (addr->sa_family) {
1384	case AF_INET:
1385		ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
1386		if (cma_get_ip_ver(hdr) != 4)
1387			return false;
1388		if (!cma_any_addr(addr) &&
1389		    hdr->dst_addr.ip4.addr != ip4_addr)
1390			return false;
1391		break;
1392	case AF_INET6:
1393		ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
1394		if (cma_get_ip_ver(hdr) != 6)
1395			return false;
1396		if (!cma_any_addr(addr) &&
1397		    memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
1398			return false;
1399		break;
1400	case AF_IB:
1401		return true;
1402	default:
1403		return false;
1404	}
1405
1406	return true;
1407}
1408
1409static bool cma_protocol_roce_dev_port(struct ib_device *device, int port_num)
1410{
1411	enum rdma_link_layer ll = rdma_port_get_link_layer(device, port_num);
1412	enum rdma_transport_type transport =
1413		rdma_node_get_transport(device->node_type);
1414
1415	return ll == IB_LINK_LAYER_ETHERNET && transport == RDMA_TRANSPORT_IB;
1416}
1417
1418static bool cma_protocol_roce(const struct rdma_cm_id *id)
1419{
1420	struct ib_device *device = id->device;
1421	const int port_num = id->port_num ?: rdma_start_port(device);
1422
1423	return cma_protocol_roce_dev_port(device, port_num);
1424}
1425
1426static bool cma_match_net_dev(const struct rdma_cm_id *id,
1427			      const struct net_device *net_dev,
1428			      u8 port_num)
1429{
1430	const struct rdma_addr *addr = &id->route.addr;
1431
1432	if (!net_dev)
1433		/* This request is an AF_IB request or a RoCE request */
1434		return (!id->port_num || id->port_num == port_num) &&
1435		       (addr->src_addr.ss_family == AF_IB ||
1436			cma_protocol_roce_dev_port(id->device, port_num));
1437
1438	return !addr->dev_addr.bound_dev_if ||
1439	       (net_eq(dev_net(net_dev), addr->dev_addr.net) &&
1440		addr->dev_addr.bound_dev_if == net_dev->if_index);
1441}
1442
1443static struct rdma_id_private *cma_find_listener(
1444		const struct rdma_bind_list *bind_list,
1445		const struct ib_cm_id *cm_id,
1446		const struct ib_cm_event *ib_event,
1447		const struct cma_req_info *req,
1448		const struct net_device *net_dev)
1449{
1450	struct rdma_id_private *id_priv, *id_priv_dev;
1451
1452	if (!bind_list)
1453		return ERR_PTR(-EINVAL);
1454
1455	hlist_for_each_entry(id_priv, &bind_list->owners, node) {
1456		if (cma_match_private_data(id_priv, ib_event->private_data)) {
1457			if (id_priv->id.device == cm_id->device &&
1458			    cma_match_net_dev(&id_priv->id, net_dev, req->port))
1459				return id_priv;
1460			list_for_each_entry(id_priv_dev,
1461					    &id_priv->listen_list,
1462					    listen_list) {
1463				if (id_priv_dev->id.device == cm_id->device &&
1464				    cma_match_net_dev(&id_priv_dev->id, net_dev, req->port))
1465					return id_priv_dev;
1466			}
1467		}
1468	}
1469
1470	return ERR_PTR(-EINVAL);
1471}
1472
1473static struct rdma_id_private *cma_id_from_event(struct ib_cm_id *cm_id,
1474						 struct ib_cm_event *ib_event,
1475						 struct net_device **net_dev)
1476{
1477	struct cma_req_info req;
1478	struct rdma_bind_list *bind_list;
1479	struct rdma_id_private *id_priv;
1480	int err;
1481
1482	err = cma_save_req_info(ib_event, &req);
1483	if (err)
1484		return ERR_PTR(err);
1485
1486	*net_dev = cma_get_net_dev(ib_event, &req);
1487	if (IS_ERR(*net_dev)) {
1488		if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
1489			/* Assuming the protocol is AF_IB */
1490			*net_dev = NULL;
1491		} else if (cma_protocol_roce_dev_port(req.device, req.port)) {
1492			/* TODO find the net dev matching the request parameters
1493			 * through the RoCE GID table */
1494			*net_dev = NULL;
1495		} else {
1496			return ERR_CAST(*net_dev);
1497		}
1498	}
1499
1500	bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
1501				rdma_ps_from_service_id(req.service_id),
1502				cma_port_from_service_id(req.service_id));
1503	id_priv = cma_find_listener(bind_list, cm_id, ib_event, &req, *net_dev);
1504	if (IS_ERR(id_priv) && *net_dev) {
1505		dev_put(*net_dev);
1506		*net_dev = NULL;
1507	}
1508
1509	return id_priv;
1510}
1511
1512static inline int cma_user_data_offset(struct rdma_id_private *id_priv)
1513{
1514	return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
1515}
1516
1517static void cma_cancel_route(struct rdma_id_private *id_priv)
1518{
1519	if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
1520		if (id_priv->query)
1521			ib_sa_cancel_query(id_priv->query_id, id_priv->query);
1522	}
1523}
1524
1525static void cma_cancel_listens(struct rdma_id_private *id_priv)
1526{
1527	struct rdma_id_private *dev_id_priv;
1528
1529	/*
1530	 * Remove from listen_any_list to prevent added devices from spawning
1531	 * additional listen requests.
1532	 */
1533	mutex_lock(&lock);
1534	list_del(&id_priv->list);
1535
1536	while (!list_empty(&id_priv->listen_list)) {
1537		dev_id_priv = list_entry(id_priv->listen_list.next,
1538					 struct rdma_id_private, listen_list);
1539		/* sync with device removal to avoid duplicate destruction */
1540		list_del_init(&dev_id_priv->list);
1541		list_del(&dev_id_priv->listen_list);
1542		mutex_unlock(&lock);
1543
1544		rdma_destroy_id(&dev_id_priv->id);
1545		mutex_lock(&lock);
1546	}
1547	mutex_unlock(&lock);
1548}
1549
1550static void cma_cancel_operation(struct rdma_id_private *id_priv,
1551				 enum rdma_cm_state state)
1552{
1553	switch (state) {
1554	case RDMA_CM_ADDR_QUERY:
1555		rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1556		break;
1557	case RDMA_CM_ROUTE_QUERY:
1558		cma_cancel_route(id_priv);
1559		break;
1560	case RDMA_CM_LISTEN:
1561		if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
1562			cma_cancel_listens(id_priv);
1563		break;
1564	default:
1565		break;
1566	}
1567}
1568
1569static void cma_release_port(struct rdma_id_private *id_priv)
1570{
1571	struct rdma_bind_list *bind_list = id_priv->bind_list;
1572	struct vnet *net = id_priv->id.route.addr.dev_addr.net;
1573
1574	if (!bind_list)
1575		return;
1576
1577	mutex_lock(&lock);
1578	hlist_del(&id_priv->node);
1579	if (hlist_empty(&bind_list->owners)) {
1580		cma_ps_remove(net, bind_list->ps, bind_list->port);
1581		kfree(bind_list);
1582	}
1583	mutex_unlock(&lock);
1584}
1585
1586static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1587{
1588	struct cma_multicast *mc;
1589
1590	while (!list_empty(&id_priv->mc_list)) {
1591		mc = container_of(id_priv->mc_list.next,
1592				  struct cma_multicast, list);
1593		list_del(&mc->list);
1594		if (rdma_cap_ib_mcast(id_priv->cma_dev->device,
1595				      id_priv->id.port_num)) {
1596			ib_sa_free_multicast(mc->multicast.ib);
1597			kfree(mc);
1598		} else {
1599			if (mc->igmp_joined) {
1600				struct rdma_dev_addr *dev_addr =
1601					&id_priv->id.route.addr.dev_addr;
1602				struct net_device *ndev = NULL;
1603
1604				if (dev_addr->bound_dev_if)
1605					ndev = dev_get_by_index(dev_addr->net,
1606								dev_addr->bound_dev_if);
1607				if (ndev) {
1608					dev_put(ndev);
1609				}
1610			}
1611			kref_put(&mc->mcref, release_mc);
1612		}
1613	}
1614}
1615
1616void rdma_destroy_id(struct rdma_cm_id *id)
1617{
1618	struct rdma_id_private *id_priv;
1619	enum rdma_cm_state state;
1620
1621	id_priv = container_of(id, struct rdma_id_private, id);
1622	state = cma_exch(id_priv, RDMA_CM_DESTROYING);
1623	cma_cancel_operation(id_priv, state);
1624
1625	/*
1626	 * Wait for any active callback to finish.  New callbacks will find
1627	 * the id_priv state set to destroying and abort.
1628	 */
1629	mutex_lock(&id_priv->handler_mutex);
1630	mutex_unlock(&id_priv->handler_mutex);
1631
1632	if (id_priv->cma_dev) {
1633		if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
1634			if (id_priv->cm_id.ib)
1635				ib_destroy_cm_id(id_priv->cm_id.ib);
1636		} else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
1637			if (id_priv->cm_id.iw)
1638				iw_destroy_cm_id(id_priv->cm_id.iw);
1639		}
1640		cma_leave_mc_groups(id_priv);
1641		cma_release_dev(id_priv);
1642	}
1643
1644	cma_release_port(id_priv);
1645	cma_deref_id(id_priv);
1646	wait_for_completion(&id_priv->comp);
1647
1648	if (id_priv->internal_id)
1649		cma_deref_id(id_priv->id.context);
1650
1651	kfree(id_priv->id.route.path_rec);
1652	kfree(id_priv);
1653}
1654EXPORT_SYMBOL(rdma_destroy_id);
1655
1656static int cma_rep_recv(struct rdma_id_private *id_priv)
1657{
1658	int ret;
1659
1660	ret = cma_modify_qp_rtr(id_priv, NULL);
1661	if (ret)
1662		goto reject;
1663
1664	ret = cma_modify_qp_rts(id_priv, NULL);
1665	if (ret)
1666		goto reject;
1667
1668	ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1669	if (ret)
1670		goto reject;
1671
1672	return 0;
1673reject:
1674	cma_modify_qp_err(id_priv);
1675	ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1676		       NULL, 0, NULL, 0);
1677	return ret;
1678}
1679
1680static void cma_set_rep_event_data(struct rdma_cm_event *event,
1681				   struct ib_cm_rep_event_param *rep_data,
1682				   void *private_data)
1683{
1684	event->param.conn.private_data = private_data;
1685	event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1686	event->param.conn.responder_resources = rep_data->responder_resources;
1687	event->param.conn.initiator_depth = rep_data->initiator_depth;
1688	event->param.conn.flow_control = rep_data->flow_control;
1689	event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1690	event->param.conn.srq = rep_data->srq;
1691	event->param.conn.qp_num = rep_data->remote_qpn;
1692}
1693
1694static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1695{
1696	struct rdma_id_private *id_priv = cm_id->context;
1697	struct rdma_cm_event event;
1698	int ret = 0;
1699
1700	mutex_lock(&id_priv->handler_mutex);
1701	if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
1702	     id_priv->state != RDMA_CM_CONNECT) ||
1703	    (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
1704	     id_priv->state != RDMA_CM_DISCONNECT))
1705		goto out;
1706
1707	memset(&event, 0, sizeof event);
1708	switch (ib_event->event) {
1709	case IB_CM_REQ_ERROR:
1710	case IB_CM_REP_ERROR:
1711		event.event = RDMA_CM_EVENT_UNREACHABLE;
1712		event.status = -ETIMEDOUT;
1713		break;
1714	case IB_CM_REP_RECEIVED:
1715		if (id_priv->id.qp) {
1716			event.status = cma_rep_recv(id_priv);
1717			event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1718						     RDMA_CM_EVENT_ESTABLISHED;
1719		} else {
1720			event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
1721		}
1722		cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1723				       ib_event->private_data);
1724		break;
1725	case IB_CM_RTU_RECEIVED:
1726	case IB_CM_USER_ESTABLISHED:
1727		event.event = RDMA_CM_EVENT_ESTABLISHED;
1728		break;
1729	case IB_CM_DREQ_ERROR:
1730		event.status = -ETIMEDOUT; /* fall through */
1731	case IB_CM_DREQ_RECEIVED:
1732	case IB_CM_DREP_RECEIVED:
1733		if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1734				   RDMA_CM_DISCONNECT))
1735			goto out;
1736		event.event = RDMA_CM_EVENT_DISCONNECTED;
1737		break;
1738	case IB_CM_TIMEWAIT_EXIT:
1739		event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1740		break;
1741	case IB_CM_MRA_RECEIVED:
1742		/* ignore event */
1743		goto out;
1744	case IB_CM_REJ_RECEIVED:
1745		cma_modify_qp_err(id_priv);
1746		event.status = ib_event->param.rej_rcvd.reason;
1747		event.event = RDMA_CM_EVENT_REJECTED;
1748		event.param.conn.private_data = ib_event->private_data;
1749		event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
1750		break;
1751	default:
1752		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
1753		       ib_event->event);
1754		goto out;
1755	}
1756
1757	ret = id_priv->id.event_handler(&id_priv->id, &event);
1758	if (ret) {
1759		/* Destroy the CM ID by returning a non-zero value. */
1760		id_priv->cm_id.ib = NULL;
1761		cma_exch(id_priv, RDMA_CM_DESTROYING);
1762		mutex_unlock(&id_priv->handler_mutex);
1763		rdma_destroy_id(&id_priv->id);
1764		return ret;
1765	}
1766out:
1767	mutex_unlock(&id_priv->handler_mutex);
1768	return ret;
1769}
1770
1771static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
1772					       struct ib_cm_event *ib_event,
1773					       struct net_device *net_dev)
1774{
1775	struct rdma_id_private *id_priv;
1776	struct rdma_cm_id *id;
1777	struct rdma_route *rt;
1778	const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
1779	const __be64 service_id =
1780		      ib_event->param.req_rcvd.primary_path->service_id;
1781	int ret;
1782
1783	id = rdma_create_id(listen_id->route.addr.dev_addr.net,
1784			    listen_id->event_handler, listen_id->context,
1785			    listen_id->ps, ib_event->param.req_rcvd.qp_type);
1786	if (IS_ERR(id))
1787		return NULL;
1788
1789	id_priv = container_of(id, struct rdma_id_private, id);
1790	if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1791			      (struct sockaddr *)&id->route.addr.dst_addr,
1792			      listen_id, ib_event, ss_family, service_id))
1793		goto err;
1794
1795	rt = &id->route;
1796	rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1797	rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1798			       GFP_KERNEL);
1799	if (!rt->path_rec)
1800		goto err;
1801
1802	rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1803	if (rt->num_paths == 2)
1804		rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1805
1806	if (net_dev) {
1807		ret = rdma_copy_addr(&rt->addr.dev_addr, net_dev, NULL);
1808		if (ret)
1809			goto err;
1810	} else {
1811		if (!cma_protocol_roce(listen_id) &&
1812		    cma_any_addr(cma_src_addr(id_priv))) {
1813			rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1814			rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1815			ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
1816		} else if (!cma_any_addr(cma_src_addr(id_priv))) {
1817			ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
1818			if (ret)
1819				goto err;
1820		}
1821	}
1822	rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
1823
1824	id_priv->state = RDMA_CM_CONNECT;
1825	return id_priv;
1826
1827err:
1828	rdma_destroy_id(id);
1829	return NULL;
1830}
1831
1832static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1833					      struct ib_cm_event *ib_event,
1834					      struct net_device *net_dev)
1835{
1836	struct rdma_id_private *id_priv;
1837	struct rdma_cm_id *id;
1838	const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
1839	struct vnet *net = listen_id->route.addr.dev_addr.net;
1840	int ret;
1841
1842	id = rdma_create_id(net, listen_id->event_handler, listen_id->context,
1843			    listen_id->ps, IB_QPT_UD);
1844	if (IS_ERR(id))
1845		return NULL;
1846
1847	id_priv = container_of(id, struct rdma_id_private, id);
1848	if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1849			      (struct sockaddr *)&id->route.addr.dst_addr,
1850			      listen_id, ib_event, ss_family,
1851			      ib_event->param.sidr_req_rcvd.service_id))
1852		goto err;
1853
1854	if (net_dev) {
1855		ret = rdma_copy_addr(&id->route.addr.dev_addr, net_dev, NULL);
1856		if (ret)
1857			goto err;
1858	} else {
1859		if (!cma_any_addr(cma_src_addr(id_priv))) {
1860			ret = cma_translate_addr(cma_src_addr(id_priv),
1861						 &id->route.addr.dev_addr);
1862			if (ret)
1863				goto err;
1864		}
1865	}
1866
1867	id_priv->state = RDMA_CM_CONNECT;
1868	return id_priv;
1869err:
1870	rdma_destroy_id(id);
1871	return NULL;
1872}
1873
1874static void cma_set_req_event_data(struct rdma_cm_event *event,
1875				   struct ib_cm_req_event_param *req_data,
1876				   void *private_data, int offset)
1877{
1878	event->param.conn.private_data = (char *)private_data + offset;
1879	event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1880	event->param.conn.responder_resources = req_data->responder_resources;
1881	event->param.conn.initiator_depth = req_data->initiator_depth;
1882	event->param.conn.flow_control = req_data->flow_control;
1883	event->param.conn.retry_count = req_data->retry_count;
1884	event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1885	event->param.conn.srq = req_data->srq;
1886	event->param.conn.qp_num = req_data->remote_qpn;
1887}
1888
1889static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
1890{
1891	return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
1892		 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
1893		((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
1894		 (id->qp_type == IB_QPT_UD)) ||
1895		(!id->qp_type));
1896}
1897
1898static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1899{
1900	struct rdma_id_private *listen_id, *conn_id = NULL;
1901	struct rdma_cm_event event;
1902	struct net_device *net_dev;
1903	int offset, ret;
1904
1905	listen_id = cma_id_from_event(cm_id, ib_event, &net_dev);
1906	if (IS_ERR(listen_id))
1907		return PTR_ERR(listen_id);
1908
1909	if (!cma_check_req_qp_type(&listen_id->id, ib_event)) {
1910		ret = -EINVAL;
1911		goto net_dev_put;
1912	}
1913
1914	mutex_lock(&listen_id->handler_mutex);
1915	if (listen_id->state != RDMA_CM_LISTEN) {
1916		ret = -ECONNABORTED;
1917		goto err1;
1918	}
1919
1920	memset(&event, 0, sizeof event);
1921	offset = cma_user_data_offset(listen_id);
1922	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1923	if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
1924		conn_id = cma_new_udp_id(&listen_id->id, ib_event, net_dev);
1925		event.param.ud.private_data = (char *)ib_event->private_data + offset;
1926		event.param.ud.private_data_len =
1927				IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1928	} else {
1929		conn_id = cma_new_conn_id(&listen_id->id, ib_event, net_dev);
1930		cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1931				       ib_event->private_data, offset);
1932	}
1933	if (!conn_id) {
1934		ret = -ENOMEM;
1935		goto err1;
1936	}
1937
1938	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
1939	ret = cma_acquire_dev(conn_id, listen_id);
1940	if (ret)
1941		goto err2;
1942
1943	conn_id->cm_id.ib = cm_id;
1944	cm_id->context = conn_id;
1945	cm_id->cm_handler = cma_ib_handler;
1946
1947	/*
1948	 * Protect against the user destroying conn_id from another thread
1949	 * until we're done accessing it.
1950	 */
1951	atomic_inc(&conn_id->refcount);
1952	ret = conn_id->id.event_handler(&conn_id->id, &event);
1953	if (ret)
1954		goto err3;
1955	/*
1956	 * Acquire mutex to prevent user executing rdma_destroy_id()
1957	 * while we're accessing the cm_id.
1958	 */
1959	mutex_lock(&lock);
1960	if (cma_comp(conn_id, RDMA_CM_CONNECT) &&
1961	    (conn_id->id.qp_type != IB_QPT_UD))
1962		ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1963	mutex_unlock(&lock);
1964	mutex_unlock(&conn_id->handler_mutex);
1965	mutex_unlock(&listen_id->handler_mutex);
1966	cma_deref_id(conn_id);
1967	if (net_dev)
1968		dev_put(net_dev);
1969	return 0;
1970
1971err3:
1972	cma_deref_id(conn_id);
1973	/* Destroy the CM ID by returning a non-zero value. */
1974	conn_id->cm_id.ib = NULL;
1975err2:
1976	cma_exch(conn_id, RDMA_CM_DESTROYING);
1977	mutex_unlock(&conn_id->handler_mutex);
1978err1:
1979	mutex_unlock(&listen_id->handler_mutex);
1980	if (conn_id)
1981		rdma_destroy_id(&conn_id->id);
1982
1983net_dev_put:
1984	if (net_dev)
1985		dev_put(net_dev);
1986
1987	return ret;
1988}
1989
1990__be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
1991{
1992	if (addr->sa_family == AF_IB)
1993		return ((struct sockaddr_ib *) addr)->sib_sid;
1994
1995	return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
1996}
1997EXPORT_SYMBOL(rdma_get_service_id);
1998
1999static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
2000{
2001	struct rdma_id_private *id_priv = iw_id->context;
2002	struct rdma_cm_event event;
2003	int ret = 0;
2004	struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2005	struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2006
2007	mutex_lock(&id_priv->handler_mutex);
2008	if (id_priv->state != RDMA_CM_CONNECT)
2009		goto out;
2010
2011	memset(&event, 0, sizeof event);
2012	switch (iw_event->event) {
2013	case IW_CM_EVENT_CLOSE:
2014		event.event = RDMA_CM_EVENT_DISCONNECTED;
2015		break;
2016	case IW_CM_EVENT_CONNECT_REPLY:
2017		memcpy(cma_src_addr(id_priv), laddr,
2018		       rdma_addr_size(laddr));
2019		memcpy(cma_dst_addr(id_priv), raddr,
2020		       rdma_addr_size(raddr));
2021		switch (iw_event->status) {
2022		case 0:
2023			event.event = RDMA_CM_EVENT_ESTABLISHED;
2024			event.param.conn.initiator_depth = iw_event->ird;
2025			event.param.conn.responder_resources = iw_event->ord;
2026			break;
2027		case -ECONNRESET:
2028		case -ECONNREFUSED:
2029			event.event = RDMA_CM_EVENT_REJECTED;
2030			break;
2031		case -ETIMEDOUT:
2032			event.event = RDMA_CM_EVENT_UNREACHABLE;
2033			break;
2034		default:
2035			event.event = RDMA_CM_EVENT_CONNECT_ERROR;
2036			break;
2037		}
2038		break;
2039	case IW_CM_EVENT_ESTABLISHED:
2040		event.event = RDMA_CM_EVENT_ESTABLISHED;
2041		event.param.conn.initiator_depth = iw_event->ird;
2042		event.param.conn.responder_resources = iw_event->ord;
2043		break;
2044	default:
2045		BUG_ON(1);
2046	}
2047
2048	event.status = iw_event->status;
2049	event.param.conn.private_data = iw_event->private_data;
2050	event.param.conn.private_data_len = iw_event->private_data_len;
2051	ret = id_priv->id.event_handler(&id_priv->id, &event);
2052	if (ret) {
2053		/* Destroy the CM ID by returning a non-zero value. */
2054		id_priv->cm_id.iw = NULL;
2055		cma_exch(id_priv, RDMA_CM_DESTROYING);
2056		mutex_unlock(&id_priv->handler_mutex);
2057		rdma_destroy_id(&id_priv->id);
2058		return ret;
2059	}
2060
2061out:
2062	mutex_unlock(&id_priv->handler_mutex);
2063	return ret;
2064}
2065
2066static int iw_conn_req_handler(struct iw_cm_id *cm_id,
2067			       struct iw_cm_event *iw_event)
2068{
2069	struct rdma_cm_id *new_cm_id;
2070	struct rdma_id_private *listen_id, *conn_id;
2071	struct rdma_cm_event event;
2072	int ret = -ECONNABORTED;
2073	struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2074	struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2075
2076	listen_id = cm_id->context;
2077
2078	mutex_lock(&listen_id->handler_mutex);
2079	if (listen_id->state != RDMA_CM_LISTEN)
2080		goto out;
2081
2082	/* Create a new RDMA id for the new IW CM ID */
2083	new_cm_id = rdma_create_id(listen_id->id.route.addr.dev_addr.net,
2084				   listen_id->id.event_handler,
2085				   listen_id->id.context,
2086				   RDMA_PS_TCP, IB_QPT_RC);
2087	if (IS_ERR(new_cm_id)) {
2088		ret = -ENOMEM;
2089		goto out;
2090	}
2091	conn_id = container_of(new_cm_id, struct rdma_id_private, id);
2092	mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2093	conn_id->state = RDMA_CM_CONNECT;
2094
2095	ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr, NULL);
2096	if (ret) {
2097		mutex_unlock(&conn_id->handler_mutex);
2098		rdma_destroy_id(new_cm_id);
2099		goto out;
2100	}
2101
2102	ret = cma_acquire_dev(conn_id, listen_id);
2103	if (ret) {
2104		mutex_unlock(&conn_id->handler_mutex);
2105		rdma_destroy_id(new_cm_id);
2106		goto out;
2107	}
2108
2109	conn_id->cm_id.iw = cm_id;
2110	cm_id->context = conn_id;
2111	cm_id->cm_handler = cma_iw_handler;
2112
2113	memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
2114	memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
2115
2116	memset(&event, 0, sizeof event);
2117	event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2118	event.param.conn.private_data = iw_event->private_data;
2119	event.param.conn.private_data_len = iw_event->private_data_len;
2120	event.param.conn.initiator_depth = iw_event->ird;
2121	event.param.conn.responder_resources = iw_event->ord;
2122
2123	/*
2124	 * Protect against the user destroying conn_id from another thread
2125	 * until we're done accessing it.
2126	 */
2127	atomic_inc(&conn_id->refcount);
2128	ret = conn_id->id.event_handler(&conn_id->id, &event);
2129	if (ret) {
2130		/* User wants to destroy the CM ID */
2131		conn_id->cm_id.iw = NULL;
2132		cma_exch(conn_id, RDMA_CM_DESTROYING);
2133		mutex_unlock(&conn_id->handler_mutex);
2134		cma_deref_id(conn_id);
2135		rdma_destroy_id(&conn_id->id);
2136		goto out;
2137	}
2138
2139	mutex_unlock(&conn_id->handler_mutex);
2140	cma_deref_id(conn_id);
2141
2142out:
2143	mutex_unlock(&listen_id->handler_mutex);
2144	return ret;
2145}
2146
2147static int cma_ib_listen(struct rdma_id_private *id_priv)
2148{
2149	struct sockaddr *addr;
2150	struct ib_cm_id	*id;
2151	__be64 svc_id;
2152
2153	addr = cma_src_addr(id_priv);
2154	svc_id = rdma_get_service_id(&id_priv->id, addr);
2155	id = ib_cm_insert_listen(id_priv->id.device, cma_req_handler, svc_id);
2156	if (IS_ERR(id))
2157		return PTR_ERR(id);
2158	id_priv->cm_id.ib = id;
2159
2160	return 0;
2161}
2162
2163static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
2164{
2165	int ret;
2166	struct iw_cm_id	*id;
2167
2168	id = iw_create_cm_id(id_priv->id.device,
2169			     iw_conn_req_handler,
2170			     id_priv);
2171	if (IS_ERR(id))
2172		return PTR_ERR(id);
2173
2174	id->tos = id_priv->tos;
2175	id_priv->cm_id.iw = id;
2176
2177	memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
2178	       rdma_addr_size(cma_src_addr(id_priv)));
2179
2180	ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
2181
2182	if (ret) {
2183		iw_destroy_cm_id(id_priv->cm_id.iw);
2184		id_priv->cm_id.iw = NULL;
2185	}
2186
2187	return ret;
2188}
2189
2190static int cma_listen_handler(struct rdma_cm_id *id,
2191			      struct rdma_cm_event *event)
2192{
2193	struct rdma_id_private *id_priv = id->context;
2194
2195	id->context = id_priv->id.context;
2196	id->event_handler = id_priv->id.event_handler;
2197	return id_priv->id.event_handler(id, event);
2198}
2199
2200static void cma_listen_on_dev(struct rdma_id_private *id_priv,
2201			      struct cma_device *cma_dev)
2202{
2203	struct rdma_id_private *dev_id_priv;
2204	struct rdma_cm_id *id;
2205	struct vnet *net = id_priv->id.route.addr.dev_addr.net;
2206	int ret;
2207
2208	if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
2209		return;
2210
2211	id = rdma_create_id(net, cma_listen_handler, id_priv, id_priv->id.ps,
2212			    id_priv->id.qp_type);
2213	if (IS_ERR(id))
2214		return;
2215
2216	dev_id_priv = container_of(id, struct rdma_id_private, id);
2217
2218	dev_id_priv->state = RDMA_CM_ADDR_BOUND;
2219	memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2220	       rdma_addr_size(cma_src_addr(id_priv)));
2221
2222	_cma_attach_to_dev(dev_id_priv, cma_dev);
2223	list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
2224	atomic_inc(&id_priv->refcount);
2225	dev_id_priv->internal_id = 1;
2226	dev_id_priv->afonly = id_priv->afonly;
2227
2228	ret = rdma_listen(id, id_priv->backlog);
2229	if (ret)
2230		pr_warn("RDMA CMA: cma_listen_on_dev, error %d, listening on device %s\n",
2231			ret, cma_dev->device->name);
2232}
2233
2234static void cma_listen_on_all(struct rdma_id_private *id_priv)
2235{
2236	struct cma_device *cma_dev;
2237
2238	mutex_lock(&lock);
2239	list_add_tail(&id_priv->list, &listen_any_list);
2240	list_for_each_entry(cma_dev, &dev_list, list)
2241		cma_listen_on_dev(id_priv, cma_dev);
2242	mutex_unlock(&lock);
2243}
2244
2245void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2246{
2247	struct rdma_id_private *id_priv;
2248
2249	id_priv = container_of(id, struct rdma_id_private, id);
2250	id_priv->tos = (u8) tos;
2251}
2252EXPORT_SYMBOL(rdma_set_service_type);
2253
2254static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
2255			      void *context)
2256{
2257	struct cma_work *work = context;
2258	struct rdma_route *route;
2259
2260	route = &work->id->id.route;
2261
2262	if (!status) {
2263		route->num_paths = 1;
2264		*route->path_rec = *path_rec;
2265	} else {
2266		work->old_state = RDMA_CM_ROUTE_QUERY;
2267		work->new_state = RDMA_CM_ADDR_RESOLVED;
2268		work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
2269		work->event.status = status;
2270	}
2271
2272	queue_work(cma_wq, &work->work);
2273}
2274
2275static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
2276			      struct cma_work *work)
2277{
2278	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2279	struct ib_sa_path_rec path_rec;
2280	ib_sa_comp_mask comp_mask;
2281	struct sockaddr_in6 *sin6;
2282	struct sockaddr_ib *sib;
2283
2284	memset(&path_rec, 0, sizeof path_rec);
2285	rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2286	rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2287	path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2288	path_rec.numb_path = 1;
2289	path_rec.reversible = 1;
2290	path_rec.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
2291
2292	comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2293		    IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2294		    IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2295
2296	switch (cma_family(id_priv)) {
2297	case AF_INET:
2298		path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2299		comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
2300		break;
2301	case AF_INET6:
2302		sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2303		path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2304		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2305		break;
2306	case AF_IB:
2307		sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2308		path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2309		comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2310		break;
2311	}
2312
2313	id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
2314					       id_priv->id.port_num, &path_rec,
2315					       comp_mask, timeout_ms,
2316					       GFP_KERNEL, cma_query_handler,
2317					       work, &id_priv->query);
2318
2319	return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2320}
2321
2322static void cma_work_handler(struct work_struct *_work)
2323{
2324	struct cma_work *work = container_of(_work, struct cma_work, work);
2325	struct rdma_id_private *id_priv = work->id;
2326	int destroy = 0;
2327
2328	mutex_lock(&id_priv->handler_mutex);
2329	if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2330		goto out;
2331
2332	if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
2333		cma_exch(id_priv, RDMA_CM_DESTROYING);
2334		destroy = 1;
2335	}
2336out:
2337	mutex_unlock(&id_priv->handler_mutex);
2338	cma_deref_id(id_priv);
2339	if (destroy)
2340		rdma_destroy_id(&id_priv->id);
2341	kfree(work);
2342}
2343
2344static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
2345{
2346	struct rdma_route *route = &id_priv->id.route;
2347	struct cma_work *work;
2348	int ret;
2349
2350	work = kzalloc(sizeof *work, GFP_KERNEL);
2351	if (!work)
2352		return -ENOMEM;
2353
2354	work->id = id_priv;
2355	INIT_WORK(&work->work, cma_work_handler);
2356	work->old_state = RDMA_CM_ROUTE_QUERY;
2357	work->new_state = RDMA_CM_ROUTE_RESOLVED;
2358	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2359
2360	route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
2361	if (!route->path_rec) {
2362		ret = -ENOMEM;
2363		goto err1;
2364	}
2365
2366	ret = cma_query_ib_route(id_priv, timeout_ms, work);
2367	if (ret)
2368		goto err2;
2369
2370	return 0;
2371err2:
2372	kfree(route->path_rec);
2373	route->path_rec = NULL;
2374err1:
2375	kfree(work);
2376	return ret;
2377}
2378
2379int rdma_set_ib_paths(struct rdma_cm_id *id,
2380		      struct ib_sa_path_rec *path_rec, int num_paths)
2381{
2382	struct rdma_id_private *id_priv;
2383	int ret;
2384
2385	id_priv = container_of(id, struct rdma_id_private, id);
2386	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2387			   RDMA_CM_ROUTE_RESOLVED))
2388		return -EINVAL;
2389
2390	id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
2391				     GFP_KERNEL);
2392	if (!id->route.path_rec) {
2393		ret = -ENOMEM;
2394		goto err;
2395	}
2396
2397	id->route.num_paths = num_paths;
2398	return 0;
2399err:
2400	cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
2401	return ret;
2402}
2403EXPORT_SYMBOL(rdma_set_ib_paths);
2404
2405static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
2406{
2407	struct cma_work *work;
2408
2409	work = kzalloc(sizeof *work, GFP_KERNEL);
2410	if (!work)
2411		return -ENOMEM;
2412
2413	work->id = id_priv;
2414	INIT_WORK(&work->work, cma_work_handler);
2415	work->old_state = RDMA_CM_ROUTE_QUERY;
2416	work->new_state = RDMA_CM_ROUTE_RESOLVED;
2417	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2418	queue_work(cma_wq, &work->work);
2419	return 0;
2420}
2421
2422static int iboe_tos_to_sl(struct net_device *ndev, int tos)
2423{
2424	/* TODO: Implement this function */
2425	return 0;
2426}
2427
2428static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
2429					   unsigned long supported_gids,
2430					   enum ib_gid_type default_gid)
2431{
2432	if ((network_type == RDMA_NETWORK_IPV4 ||
2433	     network_type == RDMA_NETWORK_IPV6) &&
2434	    test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
2435		return IB_GID_TYPE_ROCE_UDP_ENCAP;
2436
2437	return default_gid;
2438}
2439
2440static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
2441{
2442	struct rdma_route *route = &id_priv->id.route;
2443	struct rdma_addr *addr = &route->addr;
2444	struct cma_work *work;
2445	int ret;
2446	struct net_device *ndev = NULL;
2447
2448
2449	work = kzalloc(sizeof *work, GFP_KERNEL);
2450	if (!work)
2451		return -ENOMEM;
2452
2453	work->id = id_priv;
2454	INIT_WORK(&work->work, cma_work_handler);
2455
2456	route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
2457	if (!route->path_rec) {
2458		ret = -ENOMEM;
2459		goto err1;
2460	}
2461
2462	route->num_paths = 1;
2463
2464	if (addr->dev_addr.bound_dev_if) {
2465		unsigned long supported_gids;
2466
2467		ndev = dev_get_by_index(addr->dev_addr.net,
2468					addr->dev_addr.bound_dev_if);
2469		if (!ndev) {
2470			ret = -ENODEV;
2471			goto err2;
2472		}
2473
2474		if (ndev->if_flags & IFF_LOOPBACK) {
2475			dev_put(ndev);
2476			if (!id_priv->id.device->get_netdev) {
2477				ret = -EOPNOTSUPP;
2478				goto err2;
2479			}
2480
2481			ndev = id_priv->id.device->get_netdev(id_priv->id.device,
2482							      id_priv->id.port_num);
2483			if (!ndev) {
2484				ret = -ENODEV;
2485				goto err2;
2486			}
2487		}
2488
2489		route->path_rec->net = ndev->if_vnet;
2490		route->path_rec->ifindex = ndev->if_index;
2491		supported_gids = roce_gid_type_mask_support(id_priv->id.device,
2492							    id_priv->id.port_num);
2493		route->path_rec->gid_type =
2494			cma_route_gid_type(addr->dev_addr.network,
2495					   supported_gids,
2496					   id_priv->gid_type);
2497	}
2498	if (!ndev) {
2499		ret = -ENODEV;
2500		goto err2;
2501	}
2502
2503	memcpy(route->path_rec->dmac, addr->dev_addr.dst_dev_addr, ETH_ALEN);
2504
2505	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
2506		    &route->path_rec->sgid);
2507	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
2508		    &route->path_rec->dgid);
2509
2510	/* Use the hint from IP Stack to select GID Type */
2511	if (route->path_rec->gid_type < ib_network_to_gid_type(addr->dev_addr.network))
2512		route->path_rec->gid_type = ib_network_to_gid_type(addr->dev_addr.network);
2513	if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
2514		/* TODO: get the hoplimit from the inet/inet6 device */
2515		route->path_rec->hop_limit = addr->dev_addr.hoplimit;
2516	else
2517		route->path_rec->hop_limit = 1;
2518	route->path_rec->reversible = 1;
2519	route->path_rec->pkey = cpu_to_be16(0xffff);
2520	route->path_rec->mtu_selector = IB_SA_EQ;
2521	route->path_rec->sl = iboe_tos_to_sl(ndev, id_priv->tos);
2522	route->path_rec->mtu = iboe_get_mtu(ndev->if_mtu);
2523	route->path_rec->rate_selector = IB_SA_EQ;
2524	route->path_rec->rate = iboe_get_rate(ndev);
2525	dev_put(ndev);
2526	route->path_rec->packet_life_time_selector = IB_SA_EQ;
2527	route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
2528	if (!route->path_rec->mtu) {
2529		ret = -EINVAL;
2530		goto err2;
2531	}
2532
2533	work->old_state = RDMA_CM_ROUTE_QUERY;
2534	work->new_state = RDMA_CM_ROUTE_RESOLVED;
2535	work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2536	work->event.status = 0;
2537
2538	queue_work(cma_wq, &work->work);
2539
2540	return 0;
2541
2542err2:
2543	kfree(route->path_rec);
2544	route->path_rec = NULL;
2545err1:
2546	kfree(work);
2547	return ret;
2548}
2549
2550int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
2551{
2552	struct rdma_id_private *id_priv;
2553	int ret;
2554
2555	id_priv = container_of(id, struct rdma_id_private, id);
2556	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
2557		return -EINVAL;
2558
2559	atomic_inc(&id_priv->refcount);
2560	if (rdma_cap_ib_sa(id->device, id->port_num))
2561		ret = cma_resolve_ib_route(id_priv, timeout_ms);
2562	else if (rdma_protocol_roce(id->device, id->port_num))
2563		ret = cma_resolve_iboe_route(id_priv);
2564	else if (rdma_protocol_iwarp(id->device, id->port_num))
2565		ret = cma_resolve_iw_route(id_priv, timeout_ms);
2566	else
2567		ret = -ENOSYS;
2568
2569	if (ret)
2570		goto err;
2571
2572	return 0;
2573err:
2574	cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
2575	cma_deref_id(id_priv);
2576	return ret;
2577}
2578EXPORT_SYMBOL(rdma_resolve_route);
2579
2580static void cma_set_loopback(struct sockaddr *addr)
2581{
2582	switch (addr->sa_family) {
2583	case AF_INET:
2584		((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2585		break;
2586	case AF_INET6:
2587		ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
2588			      0, 0, 0, htonl(1));
2589		break;
2590	default:
2591		ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
2592			    0, 0, 0, htonl(1));
2593		break;
2594	}
2595}
2596
2597static int cma_bind_loopback(struct rdma_id_private *id_priv)
2598{
2599	struct cma_device *cma_dev, *cur_dev;
2600	struct ib_port_attr port_attr;
2601	union ib_gid gid;
2602	u16 pkey;
2603	int ret;
2604	u8 p;
2605
2606	cma_dev = NULL;
2607	mutex_lock(&lock);
2608	list_for_each_entry(cur_dev, &dev_list, list) {
2609		if (cma_family(id_priv) == AF_IB &&
2610		    !rdma_cap_ib_cm(cur_dev->device, 1))
2611			continue;
2612
2613		if (!cma_dev)
2614			cma_dev = cur_dev;
2615
2616		for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
2617			if (!ib_query_port(cur_dev->device, p, &port_attr) &&
2618			    port_attr.state == IB_PORT_ACTIVE) {
2619				cma_dev = cur_dev;
2620				goto port_found;
2621			}
2622		}
2623	}
2624
2625	if (!cma_dev) {
2626		ret = -ENODEV;
2627		goto out;
2628	}
2629
2630	p = 1;
2631
2632port_found:
2633	ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid, NULL);
2634	if (ret)
2635		goto out;
2636
2637	ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
2638	if (ret)
2639		goto out;
2640
2641	id_priv->id.route.addr.dev_addr.dev_type =
2642		(rdma_protocol_ib(cma_dev->device, p)) ?
2643		ARPHRD_INFINIBAND : ARPHRD_ETHER;
2644
2645	rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2646	ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
2647	id_priv->id.port_num = p;
2648	cma_attach_to_dev(id_priv, cma_dev);
2649	cma_set_loopback(cma_src_addr(id_priv));
2650out:
2651	mutex_unlock(&lock);
2652	return ret;
2653}
2654
2655static void addr_handler(int status, struct sockaddr *src_addr,
2656			 struct rdma_dev_addr *dev_addr, void *context)
2657{
2658	struct rdma_id_private *id_priv = context;
2659	struct rdma_cm_event event;
2660
2661	memset(&event, 0, sizeof event);
2662	mutex_lock(&id_priv->handler_mutex);
2663	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
2664			   RDMA_CM_ADDR_RESOLVED))
2665		goto out;
2666
2667	memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr));
2668	if (!status && !id_priv->cma_dev)
2669		status = cma_acquire_dev(id_priv, NULL);
2670
2671	if (status) {
2672		if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2673				   RDMA_CM_ADDR_BOUND))
2674			goto out;
2675		event.event = RDMA_CM_EVENT_ADDR_ERROR;
2676		event.status = status;
2677	} else
2678		event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2679
2680	if (id_priv->id.event_handler(&id_priv->id, &event)) {
2681		cma_exch(id_priv, RDMA_CM_DESTROYING);
2682		mutex_unlock(&id_priv->handler_mutex);
2683		cma_deref_id(id_priv);
2684		rdma_destroy_id(&id_priv->id);
2685		return;
2686	}
2687out:
2688	mutex_unlock(&id_priv->handler_mutex);
2689	cma_deref_id(id_priv);
2690}
2691
2692static int cma_resolve_loopback(struct rdma_id_private *id_priv)
2693{
2694	struct cma_work *work;
2695	union ib_gid gid;
2696	int ret;
2697
2698	work = kzalloc(sizeof *work, GFP_KERNEL);
2699	if (!work)
2700		return -ENOMEM;
2701
2702	if (!id_priv->cma_dev) {
2703		ret = cma_bind_loopback(id_priv);
2704		if (ret)
2705			goto err;
2706	}
2707
2708	rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2709	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
2710
2711	work->id = id_priv;
2712	INIT_WORK(&work->work, cma_work_handler);
2713	work->old_state = RDMA_CM_ADDR_QUERY;
2714	work->new_state = RDMA_CM_ADDR_RESOLVED;
2715	work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2716	queue_work(cma_wq, &work->work);
2717	return 0;
2718err:
2719	kfree(work);
2720	return ret;
2721}
2722
2723static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
2724{
2725	struct cma_work *work;
2726	int ret;
2727
2728	work = kzalloc(sizeof *work, GFP_KERNEL);
2729	if (!work)
2730		return -ENOMEM;
2731
2732	if (!id_priv->cma_dev) {
2733		ret = cma_resolve_ib_dev(id_priv);
2734		if (ret)
2735			goto err;
2736	}
2737
2738	rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
2739		&(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
2740
2741	work->id = id_priv;
2742	INIT_WORK(&work->work, cma_work_handler);
2743	work->old_state = RDMA_CM_ADDR_QUERY;
2744	work->new_state = RDMA_CM_ADDR_RESOLVED;
2745	work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2746	queue_work(cma_wq, &work->work);
2747	return 0;
2748err:
2749	kfree(work);
2750	return ret;
2751}
2752
2753static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2754			 struct sockaddr *dst_addr)
2755{
2756	if (!src_addr || !src_addr->sa_family) {
2757		src_addr = (struct sockaddr *) &id->route.addr.src_addr;
2758		src_addr->sa_family = dst_addr->sa_family;
2759		if (dst_addr->sa_family == AF_INET6) {
2760			struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
2761			struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
2762			src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
2763			if (IN6_IS_SCOPE_LINKLOCAL(&dst_addr6->sin6_addr))
2764				id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
2765		} else if (dst_addr->sa_family == AF_IB) {
2766			((struct sockaddr_ib *) src_addr)->sib_pkey =
2767				((struct sockaddr_ib *) dst_addr)->sib_pkey;
2768		}
2769	}
2770	return rdma_bind_addr(id, src_addr);
2771}
2772
2773int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2774		      struct sockaddr *dst_addr, int timeout_ms)
2775{
2776	struct rdma_id_private *id_priv;
2777	int ret;
2778
2779	id_priv = container_of(id, struct rdma_id_private, id);
2780	if (id_priv->state == RDMA_CM_IDLE) {
2781		ret = cma_bind_addr(id, src_addr, dst_addr);
2782		if (ret)
2783			return ret;
2784	}
2785
2786	if (cma_family(id_priv) != dst_addr->sa_family)
2787		return -EINVAL;
2788
2789	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
2790		return -EINVAL;
2791
2792	atomic_inc(&id_priv->refcount);
2793	memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
2794	if (cma_any_addr(dst_addr)) {
2795		ret = cma_resolve_loopback(id_priv);
2796	} else {
2797		if (dst_addr->sa_family == AF_IB) {
2798			ret = cma_resolve_ib_addr(id_priv);
2799		} else {
2800			ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv),
2801					      dst_addr, &id->route.addr.dev_addr,
2802					      timeout_ms, addr_handler, id_priv);
2803		}
2804	}
2805	if (ret)
2806		goto err;
2807
2808	return 0;
2809err:
2810	cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
2811	cma_deref_id(id_priv);
2812	return ret;
2813}
2814EXPORT_SYMBOL(rdma_resolve_addr);
2815
2816int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2817{
2818	struct rdma_id_private *id_priv;
2819	unsigned long flags;
2820	int ret;
2821
2822	id_priv = container_of(id, struct rdma_id_private, id);
2823	spin_lock_irqsave(&id_priv->lock, flags);
2824	if (reuse || id_priv->state == RDMA_CM_IDLE) {
2825		id_priv->reuseaddr = reuse;
2826		ret = 0;
2827	} else {
2828		ret = -EINVAL;
2829	}
2830	spin_unlock_irqrestore(&id_priv->lock, flags);
2831	return ret;
2832}
2833EXPORT_SYMBOL(rdma_set_reuseaddr);
2834
2835int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
2836{
2837	struct rdma_id_private *id_priv;
2838	unsigned long flags;
2839	int ret;
2840
2841	id_priv = container_of(id, struct rdma_id_private, id);
2842	spin_lock_irqsave(&id_priv->lock, flags);
2843	if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
2844		id_priv->options |= (1 << CMA_OPTION_AFONLY);
2845		id_priv->afonly = afonly;
2846		ret = 0;
2847	} else {
2848		ret = -EINVAL;
2849	}
2850	spin_unlock_irqrestore(&id_priv->lock, flags);
2851	return ret;
2852}
2853EXPORT_SYMBOL(rdma_set_afonly);
2854
2855static void cma_bind_port(struct rdma_bind_list *bind_list,
2856			  struct rdma_id_private *id_priv)
2857{
2858	struct sockaddr *addr;
2859	struct sockaddr_ib *sib;
2860	u64 sid, mask;
2861	__be16 port;
2862
2863	addr = cma_src_addr(id_priv);
2864	port = htons(bind_list->port);
2865
2866	switch (addr->sa_family) {
2867	case AF_INET:
2868		((struct sockaddr_in *) addr)->sin_port = port;
2869		break;
2870	case AF_INET6:
2871		((struct sockaddr_in6 *) addr)->sin6_port = port;
2872		break;
2873	case AF_IB:
2874		sib = (struct sockaddr_ib *) addr;
2875		sid = be64_to_cpu(sib->sib_sid);
2876		mask = be64_to_cpu(sib->sib_sid_mask);
2877		sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
2878		sib->sib_sid_mask = cpu_to_be64(~0ULL);
2879		break;
2880	}
2881	id_priv->bind_list = bind_list;
2882	hlist_add_head(&id_priv->node, &bind_list->owners);
2883}
2884
2885static int cma_alloc_port(enum rdma_port_space ps,
2886			  struct rdma_id_private *id_priv, unsigned short snum)
2887{
2888	struct rdma_bind_list *bind_list;
2889	int ret;
2890
2891	bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
2892	if (!bind_list)
2893		return -ENOMEM;
2894
2895	ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
2896			   snum);
2897	if (ret < 0)
2898		goto err;
2899
2900	bind_list->ps = ps;
2901	bind_list->port = (unsigned short)ret;
2902	cma_bind_port(bind_list, id_priv);
2903	return 0;
2904err:
2905	kfree(bind_list);
2906	return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
2907}
2908
2909static int cma_alloc_any_port(enum rdma_port_space ps,
2910			      struct rdma_id_private *id_priv)
2911{
2912	static unsigned int last_used_port;
2913	int low, high, remaining;
2914	unsigned int rover;
2915	struct vnet *net = id_priv->id.route.addr.dev_addr.net;
2916	u32 rand;
2917
2918	inet_get_local_port_range(net, &low, &high);
2919	remaining = (high - low) + 1;
2920	get_random_bytes(&rand, sizeof(rand));
2921	rover = rand % remaining + low;
2922retry:
2923	if (last_used_port != rover &&
2924	    !cma_ps_find(net, ps, (unsigned short)rover)) {
2925		int ret = cma_alloc_port(ps, id_priv, rover);
2926		/*
2927		 * Remember previously used port number in order to avoid
2928		 * re-using same port immediately after it is closed.
2929		 */
2930		if (!ret)
2931			last_used_port = rover;
2932		if (ret != -EADDRNOTAVAIL)
2933			return ret;
2934	}
2935	if (--remaining) {
2936		rover++;
2937		if ((rover < low) || (rover > high))
2938			rover = low;
2939		goto retry;
2940	}
2941	return -EADDRNOTAVAIL;
2942}
2943
2944/*
2945 * Check that the requested port is available.  This is called when trying to
2946 * bind to a specific port, or when trying to listen on a bound port.  In
2947 * the latter case, the provided id_priv may already be on the bind_list, but
2948 * we still need to check that it's okay to start listening.
2949 */
2950static int cma_check_port(struct rdma_bind_list *bind_list,
2951			  struct rdma_id_private *id_priv, uint8_t reuseaddr)
2952{
2953	struct rdma_id_private *cur_id;
2954	struct sockaddr *addr, *cur_addr;
2955
2956	addr = cma_src_addr(id_priv);
2957	hlist_for_each_entry(cur_id, &bind_list->owners, node) {
2958		if (id_priv == cur_id)
2959			continue;
2960
2961		if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
2962		    cur_id->reuseaddr)
2963			continue;
2964
2965		cur_addr = cma_src_addr(cur_id);
2966		if (id_priv->afonly && cur_id->afonly &&
2967		    (addr->sa_family != cur_addr->sa_family))
2968			continue;
2969
2970		if (cma_any_addr(addr) || cma_any_addr(cur_addr))
2971			return -EADDRNOTAVAIL;
2972
2973		if (!cma_addr_cmp(addr, cur_addr))
2974			return -EADDRINUSE;
2975	}
2976	return 0;
2977}
2978
2979static int cma_use_port(enum rdma_port_space ps,
2980			struct rdma_id_private *id_priv)
2981{
2982	struct rdma_bind_list *bind_list;
2983	unsigned short snum;
2984	int ret;
2985
2986	snum = ntohs(cma_port(cma_src_addr(id_priv)));
2987	if (snum < IPPORT_RESERVED &&
2988	    priv_check(curthread, PRIV_NETINET_BINDANY) != 0)
2989		return -EACCES;
2990
2991	bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
2992	if (!bind_list) {
2993		ret = cma_alloc_port(ps, id_priv, snum);
2994	} else {
2995		ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
2996		if (!ret)
2997			cma_bind_port(bind_list, id_priv);
2998	}
2999	return ret;
3000}
3001
3002static int cma_bind_listen(struct rdma_id_private *id_priv)
3003{
3004	struct rdma_bind_list *bind_list = id_priv->bind_list;
3005	int ret = 0;
3006
3007	mutex_lock(&lock);
3008	if (bind_list->owners.first->next)
3009		ret = cma_check_port(bind_list, id_priv, 0);
3010	mutex_unlock(&lock);
3011	return ret;
3012}
3013
3014static enum rdma_port_space cma_select_inet_ps(
3015		struct rdma_id_private *id_priv)
3016{
3017	switch (id_priv->id.ps) {
3018	case RDMA_PS_TCP:
3019	case RDMA_PS_UDP:
3020	case RDMA_PS_IPOIB:
3021	case RDMA_PS_IB:
3022		return id_priv->id.ps;
3023	default:
3024
3025		return 0;
3026	}
3027}
3028
3029static enum rdma_port_space cma_select_ib_ps(struct rdma_id_private *id_priv)
3030{
3031	enum rdma_port_space ps = 0;
3032	struct sockaddr_ib *sib;
3033	u64 sid_ps, mask, sid;
3034
3035	sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
3036	mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
3037	sid = be64_to_cpu(sib->sib_sid) & mask;
3038
3039	if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
3040		sid_ps = RDMA_IB_IP_PS_IB;
3041		ps = RDMA_PS_IB;
3042	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
3043		   (sid == (RDMA_IB_IP_PS_TCP & mask))) {
3044		sid_ps = RDMA_IB_IP_PS_TCP;
3045		ps = RDMA_PS_TCP;
3046	} else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
3047		   (sid == (RDMA_IB_IP_PS_UDP & mask))) {
3048		sid_ps = RDMA_IB_IP_PS_UDP;
3049		ps = RDMA_PS_UDP;
3050	}
3051
3052	if (ps) {
3053		sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
3054		sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
3055						be64_to_cpu(sib->sib_sid_mask));
3056	}
3057	return ps;
3058}
3059
3060static int cma_get_port(struct rdma_id_private *id_priv)
3061{
3062	enum rdma_port_space ps;
3063	int ret;
3064
3065	if (cma_family(id_priv) != AF_IB)
3066		ps = cma_select_inet_ps(id_priv);
3067	else
3068		ps = cma_select_ib_ps(id_priv);
3069	if (!ps)
3070		return -EPROTONOSUPPORT;
3071
3072	mutex_lock(&lock);
3073	if (cma_any_port(cma_src_addr(id_priv)))
3074		ret = cma_alloc_any_port(ps, id_priv);
3075	else
3076		ret = cma_use_port(ps, id_priv);
3077	mutex_unlock(&lock);
3078
3079	return ret;
3080}
3081
3082static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3083			       struct sockaddr *addr)
3084{
3085#ifdef INET6
3086	struct sockaddr_in6 sin6;
3087
3088	if (addr->sa_family != AF_INET6)
3089		return 0;
3090
3091	sin6 = *(struct sockaddr_in6 *)addr;
3092
3093	if (!(IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr)))
3094		return 0;
3095
3096	if (sa6_recoverscope(&sin6) || sin6.sin6_scope_id == 0)
3097		return -EINVAL;
3098
3099	dev_addr->bound_dev_if = sin6.sin6_scope_id;
3100#endif
3101	return 0;
3102}
3103
3104int rdma_listen(struct rdma_cm_id *id, int backlog)
3105{
3106	struct rdma_id_private *id_priv;
3107	int ret;
3108
3109	id_priv = container_of(id, struct rdma_id_private, id);
3110	if (id_priv->state == RDMA_CM_IDLE) {
3111		id->route.addr.src_addr.ss_family = AF_INET;
3112		ret = rdma_bind_addr(id, cma_src_addr(id_priv));
3113		if (ret)
3114			return ret;
3115	}
3116
3117	if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
3118		return -EINVAL;
3119
3120	if (id_priv->reuseaddr) {
3121		ret = cma_bind_listen(id_priv);
3122		if (ret)
3123			goto err;
3124	}
3125
3126	id_priv->backlog = backlog;
3127	if (id->device) {
3128		if (rdma_cap_ib_cm(id->device, 1)) {
3129			ret = cma_ib_listen(id_priv);
3130			if (ret)
3131				goto err;
3132		} else if (rdma_cap_iw_cm(id->device, 1)) {
3133			ret = cma_iw_listen(id_priv, backlog);
3134			if (ret)
3135				goto err;
3136		} else {
3137			ret = -ENOSYS;
3138			goto err;
3139		}
3140	} else
3141		cma_listen_on_all(id_priv);
3142
3143	return 0;
3144err:
3145	id_priv->backlog = 0;
3146	cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
3147	return ret;
3148}
3149EXPORT_SYMBOL(rdma_listen);
3150
3151int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
3152{
3153	struct rdma_id_private *id_priv;
3154	int ret;
3155
3156	if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
3157	    addr->sa_family != AF_IB)
3158		return -EAFNOSUPPORT;
3159
3160	id_priv = container_of(id, struct rdma_id_private, id);
3161	if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
3162		return -EINVAL;
3163
3164	ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
3165	if (ret)
3166		goto err1;
3167
3168	memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
3169	if (!cma_any_addr(addr)) {
3170		ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
3171		if (ret)
3172			goto err1;
3173
3174		ret = cma_acquire_dev(id_priv, NULL);
3175		if (ret)
3176			goto err1;
3177	}
3178
3179	if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
3180		if (addr->sa_family == AF_INET)
3181			id_priv->afonly = 1;
3182#ifdef INET6
3183		else if (addr->sa_family == AF_INET6) {
3184			CURVNET_SET_QUIET(id_priv->id.route.addr.dev_addr.net);
3185			id_priv->afonly = V_ip6_v6only;
3186			CURVNET_RESTORE();
3187		}
3188#endif
3189	}
3190	ret = cma_get_port(id_priv);
3191	if (ret)
3192		goto err2;
3193
3194	return 0;
3195err2:
3196	if (id_priv->cma_dev)
3197		cma_release_dev(id_priv);
3198err1:
3199	cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
3200	return ret;
3201}
3202EXPORT_SYMBOL(rdma_bind_addr);
3203
3204static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
3205{
3206	struct cma_hdr *cma_hdr;
3207
3208	cma_hdr = hdr;
3209	cma_hdr->cma_version = CMA_VERSION;
3210	if (cma_family(id_priv) == AF_INET) {
3211		struct sockaddr_in *src4, *dst4;
3212
3213		src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
3214		dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
3215
3216		cma_set_ip_ver(cma_hdr, 4);
3217		cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
3218		cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
3219		cma_hdr->port = src4->sin_port;
3220	} else if (cma_family(id_priv) == AF_INET6) {
3221		struct sockaddr_in6 *src6, *dst6;
3222
3223		src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
3224		dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
3225
3226		cma_set_ip_ver(cma_hdr, 6);
3227		cma_hdr->src_addr.ip6 = src6->sin6_addr;
3228		cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
3229		cma_hdr->port = src6->sin6_port;
3230	}
3231	return 0;
3232}
3233
3234static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
3235				struct ib_cm_event *ib_event)
3236{
3237	struct rdma_id_private *id_priv = cm_id->context;
3238	struct rdma_cm_event event;
3239	struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
3240	int ret = 0;
3241
3242	mutex_lock(&id_priv->handler_mutex);
3243	if (id_priv->state != RDMA_CM_CONNECT)
3244		goto out;
3245
3246	memset(&event, 0, sizeof event);
3247	switch (ib_event->event) {
3248	case IB_CM_SIDR_REQ_ERROR:
3249		event.event = RDMA_CM_EVENT_UNREACHABLE;
3250		event.status = -ETIMEDOUT;
3251		break;
3252	case IB_CM_SIDR_REP_RECEIVED:
3253		event.param.ud.private_data = ib_event->private_data;
3254		event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
3255		if (rep->status != IB_SIDR_SUCCESS) {
3256			event.event = RDMA_CM_EVENT_UNREACHABLE;
3257			event.status = ib_event->param.sidr_rep_rcvd.status;
3258			break;
3259		}
3260		ret = cma_set_qkey(id_priv, rep->qkey);
3261		if (ret) {
3262			event.event = RDMA_CM_EVENT_ADDR_ERROR;
3263			event.status = ret;
3264			break;
3265		}
3266		ret = ib_init_ah_from_path(id_priv->id.device,
3267					   id_priv->id.port_num,
3268					   id_priv->id.route.path_rec,
3269					   &event.param.ud.ah_attr);
3270		if (ret) {
3271			event.event = RDMA_CM_EVENT_ADDR_ERROR;
3272			event.status = ret;
3273			break;
3274		}
3275		event.param.ud.qp_num = rep->qpn;
3276		event.param.ud.qkey = rep->qkey;
3277		event.event = RDMA_CM_EVENT_ESTABLISHED;
3278		event.status = 0;
3279		break;
3280	default:
3281		pr_err("RDMA CMA: unexpected IB CM event: %d\n",
3282		       ib_event->event);
3283		goto out;
3284	}
3285
3286	ret = id_priv->id.event_handler(&id_priv->id, &event);
3287	if (ret) {
3288		/* Destroy the CM ID by returning a non-zero value. */
3289		id_priv->cm_id.ib = NULL;
3290		cma_exch(id_priv, RDMA_CM_DESTROYING);
3291		mutex_unlock(&id_priv->handler_mutex);
3292		rdma_destroy_id(&id_priv->id);
3293		return ret;
3294	}
3295out:
3296	mutex_unlock(&id_priv->handler_mutex);
3297	return ret;
3298}
3299
3300static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
3301			      struct rdma_conn_param *conn_param)
3302{
3303	struct ib_cm_sidr_req_param req;
3304	struct ib_cm_id	*id;
3305	void *private_data;
3306	int offset, ret;
3307
3308	memset(&req, 0, sizeof req);
3309	offset = cma_user_data_offset(id_priv);
3310	req.private_data_len = offset + conn_param->private_data_len;
3311	if (req.private_data_len < conn_param->private_data_len)
3312		return -EINVAL;
3313
3314	if (req.private_data_len) {
3315		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3316		if (!private_data)
3317			return -ENOMEM;
3318	} else {
3319		private_data = NULL;
3320	}
3321
3322	if (conn_param->private_data && conn_param->private_data_len)
3323		memcpy((char *)private_data + offset, conn_param->private_data,
3324		       conn_param->private_data_len);
3325
3326	if (private_data) {
3327		ret = cma_format_hdr(private_data, id_priv);
3328		if (ret)
3329			goto out;
3330		req.private_data = private_data;
3331	}
3332
3333	id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
3334			     id_priv);
3335	if (IS_ERR(id)) {
3336		ret = PTR_ERR(id);
3337		goto out;
3338	}
3339	id_priv->cm_id.ib = id;
3340
3341	req.path = id_priv->id.route.path_rec;
3342	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
3343	req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
3344	req.max_cm_retries = CMA_MAX_CM_RETRIES;
3345
3346	ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
3347	if (ret) {
3348		ib_destroy_cm_id(id_priv->cm_id.ib);
3349		id_priv->cm_id.ib = NULL;
3350	}
3351out:
3352	kfree(private_data);
3353	return ret;
3354}
3355
3356static int cma_connect_ib(struct rdma_id_private *id_priv,
3357			  struct rdma_conn_param *conn_param)
3358{
3359	struct ib_cm_req_param req;
3360	struct rdma_route *route;
3361	void *private_data;
3362	struct ib_cm_id	*id;
3363	int offset, ret;
3364
3365	memset(&req, 0, sizeof req);
3366	offset = cma_user_data_offset(id_priv);
3367	req.private_data_len = offset + conn_param->private_data_len;
3368	if (req.private_data_len < conn_param->private_data_len)
3369		return -EINVAL;
3370
3371	if (req.private_data_len) {
3372		private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3373		if (!private_data)
3374			return -ENOMEM;
3375	} else {
3376		private_data = NULL;
3377	}
3378
3379	if (conn_param->private_data && conn_param->private_data_len)
3380		memcpy((char *)private_data + offset, conn_param->private_data,
3381		       conn_param->private_data_len);
3382
3383	id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
3384	if (IS_ERR(id)) {
3385		ret = PTR_ERR(id);
3386		goto out;
3387	}
3388	id_priv->cm_id.ib = id;
3389
3390	route = &id_priv->id.route;
3391	if (private_data) {
3392		ret = cma_format_hdr(private_data, id_priv);
3393		if (ret)
3394			goto out;
3395		req.private_data = private_data;
3396	}
3397
3398	req.primary_path = &route->path_rec[0];
3399	if (route->num_paths == 2)
3400		req.alternate_path = &route->path_rec[1];
3401
3402	req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
3403	req.qp_num = id_priv->qp_num;
3404	req.qp_type = id_priv->id.qp_type;
3405	req.starting_psn = id_priv->seq_num;
3406	req.responder_resources = conn_param->responder_resources;
3407	req.initiator_depth = conn_param->initiator_depth;
3408	req.flow_control = conn_param->flow_control;
3409	req.retry_count = min_t(u8, 7, conn_param->retry_count);
3410	req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
3411	req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3412	req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3413	req.max_cm_retries = CMA_MAX_CM_RETRIES;
3414	req.srq = id_priv->srq ? 1 : 0;
3415
3416	ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
3417out:
3418	if (ret && !IS_ERR(id)) {
3419		ib_destroy_cm_id(id);
3420		id_priv->cm_id.ib = NULL;
3421	}
3422
3423	kfree(private_data);
3424	return ret;
3425}
3426
3427static int cma_connect_iw(struct rdma_id_private *id_priv,
3428			  struct rdma_conn_param *conn_param)
3429{
3430	struct iw_cm_id *cm_id;
3431	int ret;
3432	struct iw_cm_conn_param iw_param;
3433
3434	cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
3435	if (IS_ERR(cm_id))
3436		return PTR_ERR(cm_id);
3437
3438	cm_id->tos = id_priv->tos;
3439	id_priv->cm_id.iw = cm_id;
3440
3441	memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
3442	       rdma_addr_size(cma_src_addr(id_priv)));
3443	memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
3444	       rdma_addr_size(cma_dst_addr(id_priv)));
3445
3446	ret = cma_modify_qp_rtr(id_priv, conn_param);
3447	if (ret)
3448		goto out;
3449
3450	if (conn_param) {
3451		iw_param.ord = conn_param->initiator_depth;
3452		iw_param.ird = conn_param->responder_resources;
3453		iw_param.private_data = conn_param->private_data;
3454		iw_param.private_data_len = conn_param->private_data_len;
3455		iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
3456	} else {
3457		memset(&iw_param, 0, sizeof iw_param);
3458		iw_param.qpn = id_priv->qp_num;
3459	}
3460	ret = iw_cm_connect(cm_id, &iw_param);
3461out:
3462	if (ret) {
3463		iw_destroy_cm_id(cm_id);
3464		id_priv->cm_id.iw = NULL;
3465	}
3466	return ret;
3467}
3468
3469int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3470{
3471	struct rdma_id_private *id_priv;
3472	int ret;
3473
3474	id_priv = container_of(id, struct rdma_id_private, id);
3475	if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
3476		return -EINVAL;
3477
3478	if (!id->qp) {
3479		id_priv->qp_num = conn_param->qp_num;
3480		id_priv->srq = conn_param->srq;
3481	}
3482
3483	if (rdma_cap_ib_cm(id->device, id->port_num)) {
3484		if (id->qp_type == IB_QPT_UD)
3485			ret = cma_resolve_ib_udp(id_priv, conn_param);
3486		else
3487			ret = cma_connect_ib(id_priv, conn_param);
3488	} else if (rdma_cap_iw_cm(id->device, id->port_num))
3489		ret = cma_connect_iw(id_priv, conn_param);
3490	else
3491		ret = -ENOSYS;
3492	if (ret)
3493		goto err;
3494
3495	return 0;
3496err:
3497	cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
3498	return ret;
3499}
3500EXPORT_SYMBOL(rdma_connect);
3501
3502static int cma_accept_ib(struct rdma_id_private *id_priv,
3503			 struct rdma_conn_param *conn_param)
3504{
3505	struct ib_cm_rep_param rep;
3506	int ret;
3507
3508	ret = cma_modify_qp_rtr(id_priv, conn_param);
3509	if (ret)
3510		goto out;
3511
3512	ret = cma_modify_qp_rts(id_priv, conn_param);
3513	if (ret)
3514		goto out;
3515
3516	memset(&rep, 0, sizeof rep);
3517	rep.qp_num = id_priv->qp_num;
3518	rep.starting_psn = id_priv->seq_num;
3519	rep.private_data = conn_param->private_data;
3520	rep.private_data_len = conn_param->private_data_len;
3521	rep.responder_resources = conn_param->responder_resources;
3522	rep.initiator_depth = conn_param->initiator_depth;
3523	rep.failover_accepted = 0;
3524	rep.flow_control = conn_param->flow_control;
3525	rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
3526	rep.srq = id_priv->srq ? 1 : 0;
3527
3528	ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
3529out:
3530	return ret;
3531}
3532
3533static int cma_accept_iw(struct rdma_id_private *id_priv,
3534		  struct rdma_conn_param *conn_param)
3535{
3536	struct iw_cm_conn_param iw_param;
3537	int ret;
3538
3539	ret = cma_modify_qp_rtr(id_priv, conn_param);
3540	if (ret)
3541		return ret;
3542
3543	iw_param.ord = conn_param->initiator_depth;
3544	iw_param.ird = conn_param->responder_resources;
3545	iw_param.private_data = conn_param->private_data;
3546	iw_param.private_data_len = conn_param->private_data_len;
3547	if (id_priv->id.qp) {
3548		iw_param.qpn = id_priv->qp_num;
3549	} else
3550		iw_param.qpn = conn_param->qp_num;
3551
3552	return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
3553}
3554
3555static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
3556			     enum ib_cm_sidr_status status, u32 qkey,
3557			     const void *private_data, int private_data_len)
3558{
3559	struct ib_cm_sidr_rep_param rep;
3560	int ret;
3561
3562	memset(&rep, 0, sizeof rep);
3563	rep.status = status;
3564	if (status == IB_SIDR_SUCCESS) {
3565		ret = cma_set_qkey(id_priv, qkey);
3566		if (ret)
3567			return ret;
3568		rep.qp_num = id_priv->qp_num;
3569		rep.qkey = id_priv->qkey;
3570	}
3571	rep.private_data = private_data;
3572	rep.private_data_len = private_data_len;
3573
3574	return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
3575}
3576
3577int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3578{
3579	struct rdma_id_private *id_priv;
3580	int ret;
3581
3582	id_priv = container_of(id, struct rdma_id_private, id);
3583
3584	id_priv->owner = task_pid_nr(current);
3585
3586	if (!cma_comp(id_priv, RDMA_CM_CONNECT))
3587		return -EINVAL;
3588
3589	if (!id->qp && conn_param) {
3590		id_priv->qp_num = conn_param->qp_num;
3591		id_priv->srq = conn_param->srq;
3592	}
3593
3594	if (rdma_cap_ib_cm(id->device, id->port_num)) {
3595		if (id->qp_type == IB_QPT_UD) {
3596			if (conn_param)
3597				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
3598							conn_param->qkey,
3599							conn_param->private_data,
3600							conn_param->private_data_len);
3601			else
3602				ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
3603							0, NULL, 0);
3604		} else {
3605			if (conn_param)
3606				ret = cma_accept_ib(id_priv, conn_param);
3607			else
3608				ret = cma_rep_recv(id_priv);
3609		}
3610	} else if (rdma_cap_iw_cm(id->device, id->port_num))
3611		ret = cma_accept_iw(id_priv, conn_param);
3612	else
3613		ret = -ENOSYS;
3614
3615	if (ret)
3616		goto reject;
3617
3618	return 0;
3619reject:
3620	cma_modify_qp_err(id_priv);
3621	rdma_reject(id, NULL, 0);
3622	return ret;
3623}
3624EXPORT_SYMBOL(rdma_accept);
3625
3626int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
3627{
3628	struct rdma_id_private *id_priv;
3629	int ret;
3630
3631	id_priv = container_of(id, struct rdma_id_private, id);
3632	if (!id_priv->cm_id.ib)
3633		return -EINVAL;
3634
3635	switch (id->device->node_type) {
3636	case RDMA_NODE_IB_CA:
3637		ret = ib_cm_notify(id_priv->cm_id.ib, event);
3638		break;
3639	default:
3640		ret = 0;
3641		break;
3642	}
3643	return ret;
3644}
3645EXPORT_SYMBOL(rdma_notify);
3646
3647int rdma_reject(struct rdma_cm_id *id, const void *private_data,
3648		u8 private_data_len)
3649{
3650	struct rdma_id_private *id_priv;
3651	int ret;
3652
3653	id_priv = container_of(id, struct rdma_id_private, id);
3654	if (!id_priv->cm_id.ib)
3655		return -EINVAL;
3656
3657	if (rdma_cap_ib_cm(id->device, id->port_num)) {
3658		if (id->qp_type == IB_QPT_UD)
3659			ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
3660						private_data, private_data_len);
3661		else
3662			ret = ib_send_cm_rej(id_priv->cm_id.ib,
3663					     IB_CM_REJ_CONSUMER_DEFINED, NULL,
3664					     0, private_data, private_data_len);
3665	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
3666		ret = iw_cm_reject(id_priv->cm_id.iw,
3667				   private_data, private_data_len);
3668	} else
3669		ret = -ENOSYS;
3670
3671	return ret;
3672}
3673EXPORT_SYMBOL(rdma_reject);
3674
3675int rdma_disconnect(struct rdma_cm_id *id)
3676{
3677	struct rdma_id_private *id_priv;
3678	int ret;
3679
3680	id_priv = container_of(id, struct rdma_id_private, id);
3681	if (!id_priv->cm_id.ib)
3682		return -EINVAL;
3683
3684	if (rdma_cap_ib_cm(id->device, id->port_num)) {
3685		ret = cma_modify_qp_err(id_priv);
3686		if (ret)
3687			goto out;
3688		/* Initiate or respond to a disconnect. */
3689		if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
3690			ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
3691	} else if (rdma_cap_iw_cm(id->device, id->port_num)) {
3692		ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
3693	} else
3694		ret = -EINVAL;
3695
3696out:
3697	return ret;
3698}
3699EXPORT_SYMBOL(rdma_disconnect);
3700
3701static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
3702{
3703	struct rdma_id_private *id_priv;
3704	struct cma_multicast *mc = multicast->context;
3705	struct rdma_cm_event event;
3706	int ret = 0;
3707
3708	id_priv = mc->id_priv;
3709	mutex_lock(&id_priv->handler_mutex);
3710	if (id_priv->state != RDMA_CM_ADDR_BOUND &&
3711	    id_priv->state != RDMA_CM_ADDR_RESOLVED)
3712		goto out;
3713
3714	if (!status)
3715		status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
3716	mutex_lock(&id_priv->qp_mutex);
3717	if (!status && id_priv->id.qp)
3718		status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
3719					 be16_to_cpu(multicast->rec.mlid));
3720	mutex_unlock(&id_priv->qp_mutex);
3721
3722	memset(&event, 0, sizeof event);
3723	event.status = status;
3724	event.param.ud.private_data = mc->context;
3725	if (!status) {
3726		struct rdma_dev_addr *dev_addr =
3727			&id_priv->id.route.addr.dev_addr;
3728		struct net_device *ndev =
3729			dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
3730		enum ib_gid_type gid_type =
3731			id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
3732			rdma_start_port(id_priv->cma_dev->device)];
3733
3734		event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
3735		ib_init_ah_from_mcmember(id_priv->id.device,
3736					 id_priv->id.port_num, &multicast->rec,
3737					 ndev, gid_type,
3738					 &event.param.ud.ah_attr);
3739		event.param.ud.qp_num = 0xFFFFFF;
3740		event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
3741		if (ndev)
3742			dev_put(ndev);
3743	} else
3744		event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
3745
3746	ret = id_priv->id.event_handler(&id_priv->id, &event);
3747	if (ret) {
3748		cma_exch(id_priv, RDMA_CM_DESTROYING);
3749		mutex_unlock(&id_priv->handler_mutex);
3750		rdma_destroy_id(&id_priv->id);
3751		return 0;
3752	}
3753
3754out:
3755	mutex_unlock(&id_priv->handler_mutex);
3756	return 0;
3757}
3758
3759static void cma_set_mgid(struct rdma_id_private *id_priv,
3760			 struct sockaddr *addr, union ib_gid *mgid)
3761{
3762	unsigned char mc_map[MAX_ADDR_LEN];
3763	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3764	struct sockaddr_in *sin = (struct sockaddr_in *) addr;
3765	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
3766
3767	if (cma_any_addr(addr)) {
3768		memset(mgid, 0, sizeof *mgid);
3769	} else if ((addr->sa_family == AF_INET6) &&
3770		   ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
3771								 0xFF10A01B)) {
3772		/* IPv6 address is an SA assigned MGID. */
3773		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3774	} else if (addr->sa_family == AF_IB) {
3775		memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
3776	} else if (addr->sa_family == AF_INET6) {
3777		ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
3778		if (id_priv->id.ps == RDMA_PS_UDP)
3779			mc_map[7] = 0x01;	/* Use RDMA CM signature */
3780		*mgid = *(union ib_gid *) (mc_map + 4);
3781	} else {
3782		ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
3783		if (id_priv->id.ps == RDMA_PS_UDP)
3784			mc_map[7] = 0x01;	/* Use RDMA CM signature */
3785		*mgid = *(union ib_gid *) (mc_map + 4);
3786	}
3787}
3788
3789static void cma_query_sa_classport_info_cb(int status,
3790					   struct ib_class_port_info *rec,
3791					   void *context)
3792{
3793	struct class_port_info_context *cb_ctx = context;
3794
3795	WARN_ON(!context);
3796
3797	if (status || !rec) {
3798		pr_debug("RDMA CM: %s port %u failed query ClassPortInfo status: %d\n",
3799			 cb_ctx->device->name, cb_ctx->port_num, status);
3800		goto out;
3801	}
3802
3803	memcpy(cb_ctx->class_port_info, rec, sizeof(struct ib_class_port_info));
3804
3805out:
3806	complete(&cb_ctx->done);
3807}
3808
3809static int cma_query_sa_classport_info(struct ib_device *device, u8 port_num,
3810				       struct ib_class_port_info *class_port_info)
3811{
3812	struct class_port_info_context *cb_ctx;
3813	int ret;
3814
3815	cb_ctx = kmalloc(sizeof(*cb_ctx), GFP_KERNEL);
3816	if (!cb_ctx)
3817		return -ENOMEM;
3818
3819	cb_ctx->device = device;
3820	cb_ctx->class_port_info = class_port_info;
3821	cb_ctx->port_num = port_num;
3822	init_completion(&cb_ctx->done);
3823
3824	ret = ib_sa_classport_info_rec_query(&sa_client, device, port_num,
3825					     CMA_QUERY_CLASSPORT_INFO_TIMEOUT,
3826					     GFP_KERNEL, cma_query_sa_classport_info_cb,
3827					     cb_ctx, &cb_ctx->sa_query);
3828	if (ret < 0) {
3829		pr_err("RDMA CM: %s port %u failed to send ClassPortInfo query, ret: %d\n",
3830		       device->name, port_num, ret);
3831		goto out;
3832	}
3833
3834	wait_for_completion(&cb_ctx->done);
3835
3836out:
3837	kfree(cb_ctx);
3838	return ret;
3839}
3840
3841static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
3842				 struct cma_multicast *mc)
3843{
3844	struct ib_sa_mcmember_rec rec;
3845	struct ib_class_port_info class_port_info;
3846	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3847	ib_sa_comp_mask comp_mask;
3848	int ret;
3849
3850	ib_addr_get_mgid(dev_addr, &rec.mgid);
3851	ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
3852				     &rec.mgid, &rec);
3853	if (ret)
3854		return ret;
3855
3856	ret = cma_set_qkey(id_priv, 0);
3857	if (ret)
3858		return ret;
3859
3860	cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
3861	rec.qkey = cpu_to_be32(id_priv->qkey);
3862	rdma_addr_get_sgid(dev_addr, &rec.port_gid);
3863	rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
3864	rec.join_state = mc->join_state;
3865
3866	if (rec.join_state == BIT(SENDONLY_FULLMEMBER_JOIN)) {
3867		ret = cma_query_sa_classport_info(id_priv->id.device,
3868						  id_priv->id.port_num,
3869						  &class_port_info);
3870
3871		if (ret)
3872			return ret;
3873
3874		if (!(ib_get_cpi_capmask2(&class_port_info) &
3875		      IB_SA_CAP_MASK2_SENDONLY_FULL_MEM_SUPPORT)) {
3876			pr_warn("RDMA CM: %s port %u Unable to multicast join\n"
3877				"RDMA CM: SM doesn't support Send Only Full Member option\n",
3878				id_priv->id.device->name, id_priv->id.port_num);
3879			return -EOPNOTSUPP;
3880		}
3881	}
3882
3883	comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
3884		    IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
3885		    IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
3886		    IB_SA_MCMEMBER_REC_FLOW_LABEL |
3887		    IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
3888
3889	if (id_priv->id.ps == RDMA_PS_IPOIB)
3890		comp_mask |= IB_SA_MCMEMBER_REC_RATE |
3891			     IB_SA_MCMEMBER_REC_RATE_SELECTOR |
3892			     IB_SA_MCMEMBER_REC_MTU_SELECTOR |
3893			     IB_SA_MCMEMBER_REC_MTU |
3894			     IB_SA_MCMEMBER_REC_HOP_LIMIT;
3895
3896	mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
3897						id_priv->id.port_num, &rec,
3898						comp_mask, GFP_KERNEL,
3899						cma_ib_mc_handler, mc);
3900	return PTR_ERR_OR_ZERO(mc->multicast.ib);
3901}
3902
3903static void iboe_mcast_work_handler(struct work_struct *work)
3904{
3905	struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
3906	struct cma_multicast *mc = mw->mc;
3907	struct ib_sa_multicast *m = mc->multicast.ib;
3908
3909	mc->multicast.ib->context = mc;
3910	cma_ib_mc_handler(0, m);
3911	kref_put(&mc->mcref, release_mc);
3912	kfree(mw);
3913}
3914
3915static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
3916{
3917	struct sockaddr_in *sin = (struct sockaddr_in *)addr;
3918	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
3919
3920	if (cma_any_addr(addr)) {
3921		memset(mgid, 0, sizeof *mgid);
3922	} else if (addr->sa_family == AF_INET6) {
3923		memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3924	} else {
3925		mgid->raw[0] = 0xff;
3926		mgid->raw[1] = 0x0e;
3927		mgid->raw[2] = 0;
3928		mgid->raw[3] = 0;
3929		mgid->raw[4] = 0;
3930		mgid->raw[5] = 0;
3931		mgid->raw[6] = 0;
3932		mgid->raw[7] = 0;
3933		mgid->raw[8] = 0;
3934		mgid->raw[9] = 0;
3935		mgid->raw[10] = 0xff;
3936		mgid->raw[11] = 0xff;
3937		*(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
3938	}
3939}
3940
3941static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
3942				   struct cma_multicast *mc)
3943{
3944	struct iboe_mcast_work *work;
3945	struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3946	int err = 0;
3947	struct sockaddr *addr = (struct sockaddr *)&mc->addr;
3948	struct net_device *ndev = NULL;
3949	enum ib_gid_type gid_type;
3950	bool send_only;
3951
3952	send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
3953
3954	if (cma_zero_addr((struct sockaddr *)&mc->addr))
3955		return -EINVAL;
3956
3957	work = kzalloc(sizeof *work, GFP_KERNEL);
3958	if (!work)
3959		return -ENOMEM;
3960
3961	mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
3962	if (!mc->multicast.ib) {
3963		err = -ENOMEM;
3964		goto out1;
3965	}
3966
3967	cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
3968
3969	mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
3970	if (id_priv->id.ps == RDMA_PS_UDP)
3971		mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
3972
3973	if (dev_addr->bound_dev_if)
3974		ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
3975	if (!ndev) {
3976		err = -ENODEV;
3977		goto out2;
3978	}
3979	mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
3980	mc->multicast.ib->rec.hop_limit = 1;
3981	mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->if_mtu);
3982
3983	gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
3984		   rdma_start_port(id_priv->cma_dev->device)];
3985	if (addr->sa_family == AF_INET) {
3986		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
3987			mc->multicast.ib->rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
3988			if (!send_only) {
3989				mc->igmp_joined = true;
3990			}
3991		}
3992	} else {
3993		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
3994			err = -ENOTSUPP;
3995	}
3996	dev_put(ndev);
3997	if (err || !mc->multicast.ib->rec.mtu) {
3998		if (!err)
3999			err = -EINVAL;
4000		goto out2;
4001	}
4002	rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
4003		    &mc->multicast.ib->rec.port_gid);
4004	work->id = id_priv;
4005	work->mc = mc;
4006	INIT_WORK(&work->work, iboe_mcast_work_handler);
4007	kref_get(&mc->mcref);
4008	queue_work(cma_wq, &work->work);
4009
4010	return 0;
4011
4012out2:
4013	kfree(mc->multicast.ib);
4014out1:
4015	kfree(work);
4016	return err;
4017}
4018
4019int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
4020			u8 join_state, void *context)
4021{
4022	struct rdma_id_private *id_priv;
4023	struct cma_multicast *mc;
4024	int ret;
4025
4026	id_priv = container_of(id, struct rdma_id_private, id);
4027	if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
4028	    !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
4029		return -EINVAL;
4030
4031	mc = kmalloc(sizeof *mc, GFP_KERNEL);
4032	if (!mc)
4033		return -ENOMEM;
4034
4035	memcpy(&mc->addr, addr, rdma_addr_size(addr));
4036	mc->context = context;
4037	mc->id_priv = id_priv;
4038	mc->igmp_joined = false;
4039	mc->join_state = join_state;
4040	spin_lock(&id_priv->lock);
4041	list_add(&mc->list, &id_priv->mc_list);
4042	spin_unlock(&id_priv->lock);
4043
4044	if (rdma_protocol_roce(id->device, id->port_num)) {
4045		kref_init(&mc->mcref);
4046		ret = cma_iboe_join_multicast(id_priv, mc);
4047	} else if (rdma_cap_ib_mcast(id->device, id->port_num))
4048		ret = cma_join_ib_multicast(id_priv, mc);
4049	else
4050		ret = -ENOSYS;
4051
4052	if (ret) {
4053		spin_lock_irq(&id_priv->lock);
4054		list_del(&mc->list);
4055		spin_unlock_irq(&id_priv->lock);
4056		kfree(mc);
4057	}
4058	return ret;
4059}
4060EXPORT_SYMBOL(rdma_join_multicast);
4061
4062void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
4063{
4064	struct rdma_id_private *id_priv;
4065	struct cma_multicast *mc;
4066
4067	id_priv = container_of(id, struct rdma_id_private, id);
4068	spin_lock_irq(&id_priv->lock);
4069	list_for_each_entry(mc, &id_priv->mc_list, list) {
4070		if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) {
4071			list_del(&mc->list);
4072			spin_unlock_irq(&id_priv->lock);
4073
4074			if (id->qp)
4075				ib_detach_mcast(id->qp,
4076						&mc->multicast.ib->rec.mgid,
4077						be16_to_cpu(mc->multicast.ib->rec.mlid));
4078
4079			BUG_ON(id_priv->cma_dev->device != id->device);
4080
4081			if (rdma_cap_ib_mcast(id->device, id->port_num)) {
4082				ib_sa_free_multicast(mc->multicast.ib);
4083				kfree(mc);
4084			} else if (rdma_protocol_roce(id->device, id->port_num)) {
4085				if (mc->igmp_joined) {
4086					struct rdma_dev_addr *dev_addr =
4087						&id->route.addr.dev_addr;
4088					struct net_device *ndev = NULL;
4089
4090					if (dev_addr->bound_dev_if)
4091						ndev = dev_get_by_index(dev_addr->net,
4092									dev_addr->bound_dev_if);
4093					if (ndev) {
4094						dev_put(ndev);
4095					}
4096					mc->igmp_joined = false;
4097				}
4098				kref_put(&mc->mcref, release_mc);
4099			}
4100			return;
4101		}
4102	}
4103	spin_unlock_irq(&id_priv->lock);
4104}
4105EXPORT_SYMBOL(rdma_leave_multicast);
4106
4107static int
4108sysctl_cma_default_roce_mode(SYSCTL_HANDLER_ARGS)
4109{
4110	struct cma_device *cma_dev = arg1;
4111	const int port = arg2;
4112	char buf[64];
4113	int error;
4114
4115	strlcpy(buf, ib_cache_gid_type_str(
4116	    cma_get_default_gid_type(cma_dev, port)), sizeof(buf));
4117
4118	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
4119	if (error != 0 || req->newptr == NULL)
4120		goto done;
4121
4122	error = ib_cache_gid_parse_type_str(buf);
4123	if (error < 0) {
4124		error = EINVAL;
4125		goto done;
4126	}
4127
4128	cma_set_default_gid_type(cma_dev, port, error);
4129	error = 0;
4130done:
4131	return (error);
4132}
4133
4134static void cma_add_one(struct ib_device *device)
4135{
4136	struct cma_device *cma_dev;
4137	struct rdma_id_private *id_priv;
4138	unsigned int i;
4139	unsigned long supported_gids = 0;
4140
4141	cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
4142	if (!cma_dev)
4143		return;
4144
4145	sysctl_ctx_init(&cma_dev->sysctl_ctx);
4146
4147	cma_dev->device = device;
4148	cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
4149					    sizeof(*cma_dev->default_gid_type),
4150					    GFP_KERNEL);
4151	if (!cma_dev->default_gid_type) {
4152		kfree(cma_dev);
4153		return;
4154	}
4155	for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
4156		supported_gids = roce_gid_type_mask_support(device, i);
4157		WARN_ON(!supported_gids);
4158		cma_dev->default_gid_type[i - rdma_start_port(device)] =
4159			find_first_bit(&supported_gids, BITS_PER_LONG);
4160	}
4161
4162	init_completion(&cma_dev->comp);
4163	atomic_set(&cma_dev->refcount, 1);
4164	INIT_LIST_HEAD(&cma_dev->id_list);
4165	ib_set_client_data(device, &cma_client, cma_dev);
4166
4167	mutex_lock(&lock);
4168	list_add_tail(&cma_dev->list, &dev_list);
4169	list_for_each_entry(id_priv, &listen_any_list, list)
4170		cma_listen_on_dev(id_priv, cma_dev);
4171	mutex_unlock(&lock);
4172
4173	for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
4174		char buf[64];
4175
4176		snprintf(buf, sizeof(buf), "default_roce_mode_port%d", i);
4177
4178		(void) SYSCTL_ADD_PROC(&cma_dev->sysctl_ctx,
4179		    SYSCTL_CHILDREN(device->ports_parent->parent->oidp),
4180		    OID_AUTO, buf, CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
4181		    cma_dev, i, &sysctl_cma_default_roce_mode, "A",
4182		    "Default RoCE mode. Valid values: IB/RoCE v1 and RoCE v2");
4183	}
4184}
4185
4186static int cma_remove_id_dev(struct rdma_id_private *id_priv)
4187{
4188	struct rdma_cm_event event;
4189	enum rdma_cm_state state;
4190	int ret = 0;
4191
4192	/* Record that we want to remove the device */
4193	state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
4194	if (state == RDMA_CM_DESTROYING)
4195		return 0;
4196
4197	cma_cancel_operation(id_priv, state);
4198	mutex_lock(&id_priv->handler_mutex);
4199
4200	/* Check for destruction from another callback. */
4201	if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
4202		goto out;
4203
4204	memset(&event, 0, sizeof event);
4205	event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
4206	ret = id_priv->id.event_handler(&id_priv->id, &event);
4207out:
4208	mutex_unlock(&id_priv->handler_mutex);
4209	return ret;
4210}
4211
4212static void cma_process_remove(struct cma_device *cma_dev)
4213{
4214	struct rdma_id_private *id_priv;
4215	int ret;
4216
4217	mutex_lock(&lock);
4218	while (!list_empty(&cma_dev->id_list)) {
4219		id_priv = list_entry(cma_dev->id_list.next,
4220				     struct rdma_id_private, list);
4221
4222		list_del(&id_priv->listen_list);
4223		list_del_init(&id_priv->list);
4224		atomic_inc(&id_priv->refcount);
4225		mutex_unlock(&lock);
4226
4227		ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
4228		cma_deref_id(id_priv);
4229		if (ret)
4230			rdma_destroy_id(&id_priv->id);
4231
4232		mutex_lock(&lock);
4233	}
4234	mutex_unlock(&lock);
4235
4236	cma_deref_dev(cma_dev);
4237	wait_for_completion(&cma_dev->comp);
4238}
4239
4240static void cma_remove_one(struct ib_device *device, void *client_data)
4241{
4242	struct cma_device *cma_dev = client_data;
4243
4244	if (!cma_dev)
4245		return;
4246
4247	mutex_lock(&lock);
4248	list_del(&cma_dev->list);
4249	mutex_unlock(&lock);
4250
4251	cma_process_remove(cma_dev);
4252	sysctl_ctx_free(&cma_dev->sysctl_ctx);
4253	kfree(cma_dev->default_gid_type);
4254	kfree(cma_dev);
4255}
4256
4257static void cma_init_vnet(void *arg)
4258{
4259	struct cma_pernet *pernet = &VNET(cma_pernet);
4260
4261	idr_init(&pernet->tcp_ps);
4262	idr_init(&pernet->udp_ps);
4263	idr_init(&pernet->ipoib_ps);
4264	idr_init(&pernet->ib_ps);
4265}
4266VNET_SYSINIT(cma_init_vnet, SI_SUB_OFED_MODINIT - 1, SI_ORDER_FIRST, cma_init_vnet, NULL);
4267
4268static void cma_destroy_vnet(void *arg)
4269{
4270	struct cma_pernet *pernet = &VNET(cma_pernet);
4271
4272	idr_destroy(&pernet->tcp_ps);
4273	idr_destroy(&pernet->udp_ps);
4274	idr_destroy(&pernet->ipoib_ps);
4275	idr_destroy(&pernet->ib_ps);
4276}
4277VNET_SYSUNINIT(cma_destroy_vnet, SI_SUB_OFED_MODINIT - 1, SI_ORDER_SECOND, cma_destroy_vnet, NULL);
4278
4279static int __init cma_init(void)
4280{
4281	int ret;
4282
4283	cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
4284	if (!cma_wq)
4285		return -ENOMEM;
4286
4287	ib_sa_register_client(&sa_client);
4288	rdma_addr_register_client(&addr_client);
4289
4290	ret = ib_register_client(&cma_client);
4291	if (ret)
4292		goto err;
4293
4294	cma_configfs_init();
4295
4296	return 0;
4297
4298err:
4299	rdma_addr_unregister_client(&addr_client);
4300	ib_sa_unregister_client(&sa_client);
4301	destroy_workqueue(cma_wq);
4302	return ret;
4303}
4304
4305static void __exit cma_cleanup(void)
4306{
4307	cma_configfs_exit();
4308	ib_unregister_client(&cma_client);
4309	rdma_addr_unregister_client(&addr_client);
4310	ib_sa_unregister_client(&sa_client);
4311	destroy_workqueue(cma_wq);
4312}
4313
4314module_init(cma_init);
4315module_exit(cma_cleanup);
4316