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