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