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