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