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