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